blob: ff97a61ad964c37098c6d7b194d5abf8ea0e04fc [file] [log] [blame]
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001/*
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02002 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02005 * (C) Copyright IBM Corp. 2002, 2006
Swen Schillig41fa2ad2007-09-07 09:15:31 +02006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22#define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "zfcp_ext.h"
Christof Schmitt5f852be2007-05-08 11:16:52 +020025#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
28static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
29static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
30static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
31 void (*done) (struct scsi_cmnd *));
32static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
33static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
Christof Schmitt74b2e042008-03-03 12:19:28 +010034static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020036static int zfcp_task_management_function(struct zfcp_unit *, u8,
37 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Andreas Herrmann06506d02006-05-22 18:18:19 +020039static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
40 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static struct device_attribute *zfcp_sysfs_sdev_attrs[];
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044struct zfcp_data zfcp_data = {
45 .scsi_host_template = {
Heiko Carstensd2c993d2006-07-12 16:41:55 +020046 .name = ZFCP_NAME,
Heiko Carstensdd52e0e2006-09-18 22:28:49 +020047 .module = THIS_MODULE,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020048 .proc_name = "zfcp",
Heiko Carstensd2c993d2006-07-12 16:41:55 +020049 .slave_alloc = zfcp_scsi_slave_alloc,
50 .slave_configure = zfcp_scsi_slave_configure,
51 .slave_destroy = zfcp_scsi_slave_destroy,
52 .queuecommand = zfcp_scsi_queuecommand,
53 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
54 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
Christof Schmitt74b2e042008-03-03 12:19:28 +010055 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020056 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
57 .can_queue = 4096,
58 .this_id = -1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020059 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
60 .cmd_per_lun = 1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020061 .use_clustering = 1,
62 .sdev_attrs = zfcp_sysfs_sdev_attrs,
Swen Schillig8d1a0062006-10-12 11:43:44 +020063 .max_sectors = ZFCP_MAX_SECTORS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 },
65 .driver_version = ZFCP_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68/* Find start of Response Information in FCP response unit*/
69char *
70zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
71{
72 char *fcp_rsp_info_ptr;
73
74 fcp_rsp_info_ptr =
75 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
76
77 return fcp_rsp_info_ptr;
78}
79
80/* Find start of Sense Information in FCP response unit*/
81char *
82zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
83{
84 char *fcp_sns_info_ptr;
85
86 fcp_sns_info_ptr =
87 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
88 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
89 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
90 fcp_rsp_iu->fcp_rsp_len;
91
92 return fcp_sns_info_ptr;
93}
94
Heiko Carstens2b67fc42007-02-05 21:16:47 +010095static fcp_dl_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070096zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
97{
98 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
99 fcp_dl_t *fcp_dl_addr;
100
101 fcp_dl_addr = (fcp_dl_t *)
102 ((unsigned char *) fcp_cmd +
103 sizeof (struct fcp_cmnd_iu) + additional_length);
104 /*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200105 * fcp_dl_addr = start address of fcp_cmnd structure +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * size of fixed part + size of dynamically sized add_dcp_cdb field
107 * SEE FCP-2 documentation
108 */
109 return fcp_dl_addr;
110}
111
112fcp_dl_t
113zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
114{
115 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
116}
117
118void
119zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
120{
121 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
122}
123
124/*
125 * note: it's a bit-or operation not an assignment
126 * regarding the specified byte
127 */
128static inline void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100129set_byte(int *result, char status, char pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 *result |= status << (pos * 8);
132}
133
134void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100135set_host_byte(int *result, char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 set_byte(result, status, 2);
138}
139
140void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100141set_driver_byte(int *result, char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 set_byte(result, status, 3);
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static int
147zfcp_scsi_slave_alloc(struct scsi_device *sdp)
148{
149 struct zfcp_adapter *adapter;
150 struct zfcp_unit *unit;
151 unsigned long flags;
Andreas Herrmannfb121b02005-12-01 02:49:29 +0100152 int retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
155 if (!adapter)
156 goto out;
157
158 read_lock_irqsave(&zfcp_data.config_lock, flags);
159 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100160 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
161 &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 sdp->hostdata = unit;
163 unit->device = sdp;
164 zfcp_unit_get(unit);
165 retval = 0;
166 }
167 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
168 out:
169 return retval;
170}
171
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200172/**
173 * zfcp_scsi_slave_destroy - called when scsi device is removed
174 *
175 * Remove reference to associated scsi device for an zfcp_unit.
176 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
177 * or a scan for this device might have failed.
178 */
179static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
182
183 if (unit) {
Christof Schmitt5f852be2007-05-08 11:16:52 +0200184 zfcp_erp_wait(unit->port->adapter);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100185 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 sdpnt->hostdata = NULL;
187 unit->device = NULL;
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200188 zfcp_erp_unit_failed(unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 zfcp_unit_put(unit);
Christof Schmitt6b76a722007-08-28 09:30:59 +0200190 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
192 "address %p\n", sdpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200195/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * called from scsi midlayer to allow finetuning of a device.
197 */
198static int
199zfcp_scsi_slave_configure(struct scsi_device *sdp)
200{
201 if (sdp->tagged_supported)
202 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
203 else
204 scsi_adjust_queue_depth(sdp, 0, 1);
205 return 0;
206}
207
208/**
209 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
210 * @scpnt: pointer to struct scsi_cmnd where result is set
211 * @result: result to be set in scpnt (e.g. DID_ERROR)
212 */
213static void
214zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
215{
216 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200217 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
218 zfcp_scsi_dbf_event_result("fail", 4,
219 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100220 scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* return directly */
222 scpnt->scsi_done(scpnt);
223}
224
225/**
226 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
227 * zfcp_scsi_command_sync
228 * @adapter: adapter where scsi command is issued
229 * @unit: unit to which scsi command is sent
230 * @scpnt: scsi command to be sent
231 * @timer: timer to be started if request is successfully initiated
232 *
233 * Note: In scsi_done function must be set in scpnt.
234 */
235int
236zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200237 struct scsi_cmnd *scpnt, int use_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int tmp;
240 int retval;
241
242 retval = 0;
243
244 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
245 BUG_ON(scpnt->scsi_done == NULL);
246
247 if (unlikely(NULL == unit)) {
248 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
249 goto out;
250 }
251
252 if (unlikely(
253 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
254 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
255 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
256 "0x%016Lx on adapter %s\n",
257 unit->fcp_lun, unit->port->wwpn,
258 zfcp_get_busid_by_adapter(adapter));
259 zfcp_scsi_command_fail(scpnt, DID_ERROR);
260 goto out;
261 }
262
Christof Schmittba172422007-12-20 12:30:26 +0100263 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
264 ZFCP_REQ_AUTO_CLEANUP);
265 if (unlikely(tmp == -EBUSY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
267 "on port 0x%016Lx in recovery\n",
268 zfcp_get_busid_by_unit(unit),
269 unit->fcp_lun, unit->port->wwpn);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100270 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto out;
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (unlikely(tmp < 0)) {
275 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
276 retval = SCSI_MLQUEUE_HOST_BUSY;
277 }
278
279out:
280 return retval;
281}
282
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100283static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
285{
286 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
287 complete(wait);
288}
289
290
291/**
292 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
293 * @unit: unit where command is sent to
294 * @scpnt: scsi command to be sent
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200295 * @use_timer: indicates whether timer should be setup or not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * Return: 0
297 *
298 * Errors are indicated in scpnt->result
299 */
300int
301zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200302 int use_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int ret;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700305 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
308 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200309 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
310 use_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (ret == 0)
312 wait_for_completion(&wait);
313
314 scpnt->SCp.ptr = NULL;
315
316 return 0;
317}
318
319/*
320 * function: zfcp_scsi_queuecommand
321 *
322 * purpose: enqueues a SCSI command to the specified target device
323 *
324 * returns: 0 - success, SCSI command enqueued
325 * !0 - failure
326 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100327static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
329 void (*done) (struct scsi_cmnd *))
330{
331 struct zfcp_unit *unit;
332 struct zfcp_adapter *adapter;
333
334 /* reset the status for this request */
335 scpnt->result = 0;
336 scpnt->host_scribble = NULL;
337 scpnt->scsi_done = done;
338
339 /*
340 * figure out adapter and target device
341 * (stored there by zfcp_scsi_slave_alloc)
342 */
343 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
344 unit = (struct zfcp_unit *) scpnt->device->hostdata;
345
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200346 return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static struct zfcp_unit *
Andreas Herrmann06506d02006-05-22 18:18:19 +0200350zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
351 unsigned int lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct zfcp_port *port;
354 struct zfcp_unit *unit, *retval = NULL;
355
356 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700357 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 continue;
Christof Schmitt6b76a722007-08-28 09:30:59 +0200359 list_for_each_entry(unit, &port->unit_list_head, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (lun == unit->scsi_lun) {
361 retval = unit;
362 goto out;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 out:
366 return retval;
367}
368
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200369/**
370 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200371 * @scpnt: pointer to scsi_cmnd to be aborted
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200372 * Return: SUCCESS - command has been aborted and cleaned up in internal
373 * bookkeeping, SCSI stack won't be called for aborted command
374 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200376 * We do not need to care for a SCSI command which completes normally
377 * but late during this abort routine runs. We are allowed to return
378 * late commands to the SCSI stack. It tracks the state of commands and
379 * will handle late commands. (Usually, the normal completion of late
380 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100382static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200384 struct Scsi_Host *scsi_host;
385 struct zfcp_adapter *adapter;
386 struct zfcp_unit *unit;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200387 struct zfcp_fsf_req *fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned long flags;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200389 unsigned long old_req_id;
390 int retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200392 scsi_host = scpnt->device->host;
393 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
394 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
397 scpnt, zfcp_get_busid_by_adapter(adapter));
398
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200399 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 write_lock_irqsave(&adapter->abort_lock, flags);
401
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200402 /* Check whether corresponding fsf_req is still pending */
403 spin_lock(&adapter->req_list_lock);
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200404 fsf_req = zfcp_reqlist_find(adapter,
405 (unsigned long) scpnt->host_scribble);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200406 spin_unlock(&adapter->req_list_lock);
407 if (!fsf_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200409 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200410 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 goto out;
412 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200413 fsf_req->data = 0;
414 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
415 old_req_id = fsf_req->req_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200417 /* don't access old fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200419
420 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
421 if (!fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200422 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100423 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200424 old_req_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto out;
427 }
428
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200429 __wait_event(fsf_req->completion_wq,
430 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200431
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200432 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
433 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 retval = SUCCESS;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200435 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
436 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 } else {
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200439 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200442 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return retval;
445}
446
Christof Schmitt74b2e042008-03-03 12:19:28 +0100447static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 int retval;
Christof Schmitt74b2e042008-03-03 12:19:28 +0100450 struct zfcp_unit *unit = scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!unit) {
Christof Schmitt74b2e042008-03-03 12:19:28 +0100453 WARN_ON(1);
454 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Christof Schmitt74b2e042008-03-03 12:19:28 +0100456 retval = zfcp_task_management_function(unit,
457 FCP_LOGICAL_UNIT_RESET,
458 scpnt);
459 return retval ? FAILED : SUCCESS;
460}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Christof Schmitt74b2e042008-03-03 12:19:28 +0100462static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
463{
464 int retval;
465 struct zfcp_unit *unit = scpnt->device->hostdata;
466
467 if (!unit) {
468 WARN_ON(1);
469 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200471 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Christof Schmitt74b2e042008-03-03 12:19:28 +0100472 return retval ? FAILED : SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200476zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
477 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200481 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* issue task management function */
484 fsf_req = zfcp_fsf_send_fcp_command_task_management
485 (adapter, unit, tm_flags, 0);
486 if (!fsf_req) {
487 ZFCP_LOG_INFO("error: creation of task management request "
488 "failed for unit 0x%016Lx on port 0x%016Lx on "
489 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
490 zfcp_get_busid_by_adapter(adapter));
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200491 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 retval = -ENOMEM;
493 goto out;
494 }
495
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200496 __wait_event(fsf_req->completion_wq,
497 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
498
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200499 /*
500 * check completion status of task management function
501 */
502 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
503 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200505 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
506 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200508 } else
509 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200510
511 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 out:
513 return retval;
514}
515
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200516/**
Christof Schmitta11b4742007-11-05 12:37:43 +0100517 * zfcp_scsi_eh_host_reset_handler - handler for host reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100519static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200521 struct zfcp_unit *unit;
522 struct zfcp_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200524 unit = (struct zfcp_unit*) scpnt->device->hostdata;
525 adapter = unit->port->adapter;
526
Christof Schmitta11b4742007-11-05 12:37:43 +0100527 ZFCP_LOG_NORMAL("host reset because of problems with "
Christof Schmitt345bfea2007-11-05 12:37:44 +0100528 "unit 0x%016Lx on port 0x%016Lx, adapter %s\n",
529 unit->fcp_lun, unit->port->wwpn,
530 zfcp_get_busid_by_adapter(unit->port->adapter));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Andreas Herrmann81654282006-09-18 22:30:36 +0200532 zfcp_erp_adapter_reopen(adapter, 0);
533 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200535 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538int
539zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
540{
541 int retval = 0;
542 static unsigned int unique_id = 0;
543
Michael Loehr9f287452007-05-09 11:01:24 +0200544 if (adapter->scsi_host)
545 goto out;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* register adapter as SCSI host with mid layer of SCSI stack */
548 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
549 sizeof (struct zfcp_adapter *));
550 if (!adapter->scsi_host) {
551 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
552 "for adapter %s ",
553 zfcp_get_busid_by_adapter(adapter));
554 retval = -EIO;
555 goto out;
556 }
557 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
558
559 /* tell the SCSI stack some characteristics of this adapter */
560 adapter->scsi_host->max_id = 1;
561 adapter->scsi_host->max_lun = 1;
562 adapter->scsi_host->max_channel = 0;
563 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
564 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200565 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /*
568 * save a pointer to our own adapter data structure within
569 * hostdata field of SCSI host data structure
570 */
571 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
572
573 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
574 scsi_host_put(adapter->scsi_host);
575 retval = -EIO;
576 goto out;
577 }
578 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
579 out:
580 return retval;
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583void
584zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
585{
586 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200587 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 shost = adapter->scsi_host;
590 if (!shost)
591 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200592 read_lock_irq(&zfcp_data.config_lock);
593 list_for_each_entry(port, &adapter->port_list_head, list)
594 if (port->rport)
595 port->rport = NULL;
596 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700597 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 scsi_remove_host(shost);
599 scsi_host_put(shost);
600 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
602
603 return;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
607 * Support functions for FC transport class
608 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100609static struct fc_host_statistics*
610zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100612 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100614 if (!adapter->fc_stats) {
615 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
616 if (!fc_stats)
617 return NULL;
618 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
619 }
620 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
621 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100625zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
626 struct fsf_qtcb_bottom_port *data,
627 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100629 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
630 old->seconds_since_last_reset;
631 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
632 fc_stats->tx_words = data->tx_words - old->tx_words;
633 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
634 fc_stats->rx_words = data->rx_words - old->rx_words;
635 fc_stats->lip_count = data->lip - old->lip;
636 fc_stats->nos_count = data->nos - old->nos;
637 fc_stats->error_frames = data->error_frames - old->error_frames;
638 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
639 fc_stats->link_failure_count = data->link_failure - old->link_failure;
640 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
641 fc_stats->loss_of_signal_count = data->loss_of_signal -
642 old->loss_of_signal;
643 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
644 old->psp_error_counts;
645 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
646 old->invalid_tx_words;
647 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
648 fc_stats->fcp_input_requests = data->input_requests -
649 old->input_requests;
650 fc_stats->fcp_output_requests = data->output_requests -
651 old->output_requests;
652 fc_stats->fcp_control_requests = data->control_requests -
653 old->control_requests;
654 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
655 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100659zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
660 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100662 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
663 fc_stats->tx_frames = data->tx_frames;
664 fc_stats->tx_words = data->tx_words;
665 fc_stats->rx_frames = data->rx_frames;
666 fc_stats->rx_words = data->rx_words;
667 fc_stats->lip_count = data->lip;
668 fc_stats->nos_count = data->nos;
669 fc_stats->error_frames = data->error_frames;
670 fc_stats->dumped_frames = data->dumped_frames;
671 fc_stats->link_failure_count = data->link_failure;
672 fc_stats->loss_of_sync_count = data->loss_of_sync;
673 fc_stats->loss_of_signal_count = data->loss_of_signal;
674 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
675 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
676 fc_stats->invalid_crc_count = data->invalid_crcs;
677 fc_stats->fcp_input_requests = data->input_requests;
678 fc_stats->fcp_output_requests = data->output_requests;
679 fc_stats->fcp_control_requests = data->control_requests;
680 fc_stats->fcp_input_megabytes = data->input_mb;
681 fc_stats->fcp_output_megabytes = data->output_mb;
682}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100684/**
685 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
686 *
687 * assumption: scsi_transport_fc synchronizes calls of
688 * get_fc_host_stats and reset_fc_host_stats
689 * (XXX to be checked otherwise introduce locking)
690 */
691static struct fc_host_statistics *
692zfcp_get_fc_host_stats(struct Scsi_Host *shost)
693{
694 struct zfcp_adapter *adapter;
695 struct fc_host_statistics *fc_stats;
696 struct fsf_qtcb_bottom_port *data;
697 int ret;
698
699 adapter = (struct zfcp_adapter *)shost->hostdata[0];
700 fc_stats = zfcp_init_fc_host_stats(adapter);
701 if (!fc_stats)
702 return NULL;
703
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200704 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100705 if (!data)
706 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100707
Swen Schillig52ef11a2007-08-28 09:31:09 +0200708 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100709 if (ret) {
710 kfree(data);
711 return NULL; /* XXX return zeroed fc_stats? */
712 }
713
714 if (adapter->stats_reset &&
715 ((jiffies/HZ - adapter->stats_reset) <
716 data->seconds_since_last_reset)) {
717 zfcp_adjust_fc_host_stats(fc_stats, data,
718 adapter->stats_reset_data);
719 } else
720 zfcp_set_fc_host_stats(fc_stats, data);
721
722 kfree(data);
723 return fc_stats;
724}
725
726static void
727zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
728{
729 struct zfcp_adapter *adapter;
730 struct fsf_qtcb_bottom_port *data, *old_data;
731 int ret;
732
733 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200734 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100735 if (!data)
736 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100737
Swen Schillig52ef11a2007-08-28 09:31:09 +0200738 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
Heiko Carstens83f6d6d2007-08-08 10:47:02 +0200739 if (ret) {
740 kfree(data);
741 } else {
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100742 adapter->stats_reset = jiffies/HZ;
743 old_data = adapter->stats_reset_data;
744 adapter->stats_reset_data = data; /* finally freed in
745 adater_dequeue */
746 kfree(old_data);
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Andreas Herrmann338151e2006-05-22 18:25:56 +0200750static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
751{
752 rport->dev_loss_tmo = timeout;
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .show_starget_port_id = 1,
757 .show_starget_port_name = 1,
758 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700759 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200760 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200761 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700762 .show_host_node_name = 1,
763 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100764 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700765 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100766 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200767 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700768 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100769 .get_fc_host_stats = zfcp_get_fc_host_stats,
770 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200771 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100772 /* no functions registered for following dynamic attributes but
773 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100774 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200775 .show_host_speed = 1,
776 .show_host_port_id = 1,
Swen Schillig52ef11a2007-08-28 09:31:09 +0200777 .disable_target_scan = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
780/**
781 * ZFCP_DEFINE_SCSI_ATTR
782 * @_name: name of show attribute
783 * @_format: format string
784 * @_value: value to print
785 *
786 * Generates attribute for a unit.
787 */
788#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400789static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 char *buf) \
791{ \
792 struct scsi_device *sdev; \
793 struct zfcp_unit *unit; \
794 \
795 sdev = to_scsi_device(dev); \
796 unit = sdev->hostdata; \
797 return sprintf(buf, _format, _value); \
798} \
799 \
800static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
801
802ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
803ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
804ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
805
806static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
807 &dev_attr_fcp_lun,
808 &dev_attr_wwpn,
809 &dev_attr_hba_id,
810 NULL
811};
812
813#undef ZFCP_LOG_AREA