blob: 0acf6db0a08d7ff3a60d13dc4cccec0e189ae0da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
20 */
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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020035static int zfcp_task_management_function(struct zfcp_unit *, u8,
36 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Andreas Herrmann06506d02006-05-22 18:18:19 +020038static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
39 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static struct device_attribute *zfcp_sysfs_sdev_attrs[];
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043struct zfcp_data zfcp_data = {
44 .scsi_host_template = {
Heiko Carstensd2c993d2006-07-12 16:41:55 +020045 .name = ZFCP_NAME,
Heiko Carstensdd52e0e2006-09-18 22:28:49 +020046 .module = THIS_MODULE,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020047 .proc_name = "zfcp",
Heiko Carstensd2c993d2006-07-12 16:41:55 +020048 .slave_alloc = zfcp_scsi_slave_alloc,
49 .slave_configure = zfcp_scsi_slave_configure,
50 .slave_destroy = zfcp_scsi_slave_destroy,
51 .queuecommand = zfcp_scsi_queuecommand,
52 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
53 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +020054 .eh_bus_reset_handler = zfcp_scsi_eh_host_reset_handler,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020055 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
56 .can_queue = 4096,
57 .this_id = -1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020058 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
59 .cmd_per_lun = 1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020060 .use_clustering = 1,
61 .sdev_attrs = zfcp_sysfs_sdev_attrs,
Swen Schillig8d1a0062006-10-12 11:43:44 +020062 .max_sectors = ZFCP_MAX_SECTORS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 },
64 .driver_version = ZFCP_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67/* Find start of Response Information in FCP response unit*/
68char *
69zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
70{
71 char *fcp_rsp_info_ptr;
72
73 fcp_rsp_info_ptr =
74 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
75
76 return fcp_rsp_info_ptr;
77}
78
79/* Find start of Sense Information in FCP response unit*/
80char *
81zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
82{
83 char *fcp_sns_info_ptr;
84
85 fcp_sns_info_ptr =
86 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
87 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
88 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
89 fcp_rsp_iu->fcp_rsp_len;
90
91 return fcp_sns_info_ptr;
92}
93
Heiko Carstens2b67fc42007-02-05 21:16:47 +010094static fcp_dl_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070095zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
96{
97 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
98 fcp_dl_t *fcp_dl_addr;
99
100 fcp_dl_addr = (fcp_dl_t *)
101 ((unsigned char *) fcp_cmd +
102 sizeof (struct fcp_cmnd_iu) + additional_length);
103 /*
104 * fcp_dl_addr = start address of fcp_cmnd structure +
105 * size of fixed part + size of dynamically sized add_dcp_cdb field
106 * SEE FCP-2 documentation
107 */
108 return fcp_dl_addr;
109}
110
111fcp_dl_t
112zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
113{
114 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
115}
116
117void
118zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
119{
120 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
121}
122
123/*
124 * note: it's a bit-or operation not an assignment
125 * regarding the specified byte
126 */
127static inline void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100128set_byte(int *result, char status, char pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 *result |= status << (pos * 8);
131}
132
133void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100134set_host_byte(int *result, char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 set_byte(result, status, 2);
137}
138
139void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100140set_driver_byte(int *result, char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 set_byte(result, status, 3);
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static int
146zfcp_scsi_slave_alloc(struct scsi_device *sdp)
147{
148 struct zfcp_adapter *adapter;
149 struct zfcp_unit *unit;
150 unsigned long flags;
Andreas Herrmannfb121b02005-12-01 02:49:29 +0100151 int retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
154 if (!adapter)
155 goto out;
156
157 read_lock_irqsave(&zfcp_data.config_lock, flags);
158 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100159 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
160 &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 sdp->hostdata = unit;
162 unit->device = sdp;
163 zfcp_unit_get(unit);
164 retval = 0;
165 }
166 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
167 out:
168 return retval;
169}
170
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200171/**
172 * zfcp_scsi_slave_destroy - called when scsi device is removed
173 *
174 * Remove reference to associated scsi device for an zfcp_unit.
175 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
176 * or a scan for this device might have failed.
177 */
178static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
181
182 if (unit) {
Christof Schmitt5f852be2007-05-08 11:16:52 +0200183 zfcp_erp_wait(unit->port->adapter);
184 wait_event(unit->scsi_scan_wq,
185 atomic_test_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING,
186 &unit->status) == 0);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100187 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 sdpnt->hostdata = NULL;
189 unit->device = NULL;
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200190 zfcp_erp_unit_failed(unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 zfcp_unit_put(unit);
192 } else {
193 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
194 "address %p\n", sdpnt);
195 }
196}
197
198/*
199 * called from scsi midlayer to allow finetuning of a device.
200 */
201static int
202zfcp_scsi_slave_configure(struct scsi_device *sdp)
203{
204 if (sdp->tagged_supported)
205 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
206 else
207 scsi_adjust_queue_depth(sdp, 0, 1);
208 return 0;
209}
210
211/**
212 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
213 * @scpnt: pointer to struct scsi_cmnd where result is set
214 * @result: result to be set in scpnt (e.g. DID_ERROR)
215 */
216static void
217zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
218{
219 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200220 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
221 zfcp_scsi_dbf_event_result("fail", 4,
222 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100223 scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* return directly */
225 scpnt->scsi_done(scpnt);
226}
227
228/**
229 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
230 * zfcp_scsi_command_sync
231 * @adapter: adapter where scsi command is issued
232 * @unit: unit to which scsi command is sent
233 * @scpnt: scsi command to be sent
234 * @timer: timer to be started if request is successfully initiated
235 *
236 * Note: In scsi_done function must be set in scpnt.
237 */
238int
239zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200240 struct scsi_cmnd *scpnt, int use_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int tmp;
243 int retval;
244
245 retval = 0;
246
247 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
248 BUG_ON(scpnt->scsi_done == NULL);
249
250 if (unlikely(NULL == unit)) {
251 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
252 goto out;
253 }
254
255 if (unlikely(
256 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
257 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
258 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
259 "0x%016Lx on adapter %s\n",
260 unit->fcp_lun, unit->port->wwpn,
261 zfcp_get_busid_by_adapter(adapter));
262 zfcp_scsi_command_fail(scpnt, DID_ERROR);
263 goto out;
264 }
265
266 if (unlikely(
267 !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
268 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
269 "on port 0x%016Lx in recovery\n",
270 zfcp_get_busid_by_unit(unit),
271 unit->fcp_lun, unit->port->wwpn);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100272 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto out;
274 }
275
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200276 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 ZFCP_REQ_AUTO_CLEANUP);
278
279 if (unlikely(tmp < 0)) {
280 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
281 retval = SCSI_MLQUEUE_HOST_BUSY;
282 }
283
284out:
285 return retval;
286}
287
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100288static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
290{
291 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
292 complete(wait);
293}
294
295
296/**
297 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
298 * @unit: unit where command is sent to
299 * @scpnt: scsi command to be sent
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200300 * @use_timer: indicates whether timer should be setup or not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * Return: 0
302 *
303 * Errors are indicated in scpnt->result
304 */
305int
306zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200307 int use_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 int ret;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700310 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
313 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200314 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
315 use_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (ret == 0)
317 wait_for_completion(&wait);
318
319 scpnt->SCp.ptr = NULL;
320
321 return 0;
322}
323
324/*
325 * function: zfcp_scsi_queuecommand
326 *
327 * purpose: enqueues a SCSI command to the specified target device
328 *
329 * returns: 0 - success, SCSI command enqueued
330 * !0 - failure
331 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100332static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
334 void (*done) (struct scsi_cmnd *))
335{
336 struct zfcp_unit *unit;
337 struct zfcp_adapter *adapter;
338
339 /* reset the status for this request */
340 scpnt->result = 0;
341 scpnt->host_scribble = NULL;
342 scpnt->scsi_done = done;
343
344 /*
345 * figure out adapter and target device
346 * (stored there by zfcp_scsi_slave_alloc)
347 */
348 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
349 unit = (struct zfcp_unit *) scpnt->device->hostdata;
350
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200351 return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static struct zfcp_unit *
Andreas Herrmann06506d02006-05-22 18:18:19 +0200355zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
356 unsigned int lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct zfcp_port *port;
359 struct zfcp_unit *unit, *retval = NULL;
360
361 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700362 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 continue;
364 list_for_each_entry(unit, &port->unit_list_head, list) {
365 if (lun == unit->scsi_lun) {
366 retval = unit;
367 goto out;
368 }
369 }
370 }
371 out:
372 return retval;
373}
374
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200375/**
376 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
377 * @scpnt: pointer to scsi_cmnd to be aborted
378 * Return: SUCCESS - command has been aborted and cleaned up in internal
379 * bookkeeping, SCSI stack won't be called for aborted command
380 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200382 * We do not need to care for a SCSI command which completes normally
383 * but late during this abort routine runs. We are allowed to return
384 * late commands to the SCSI stack. It tracks the state of commands and
385 * will handle late commands. (Usually, the normal completion of late
386 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100388static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200390 struct Scsi_Host *scsi_host;
391 struct zfcp_adapter *adapter;
392 struct zfcp_unit *unit;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200393 struct zfcp_fsf_req *fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned long flags;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200395 unsigned long old_req_id;
396 int retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200398 scsi_host = scpnt->device->host;
399 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
400 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
403 scpnt, zfcp_get_busid_by_adapter(adapter));
404
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200405 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 write_lock_irqsave(&adapter->abort_lock, flags);
407
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200408 /* Check whether corresponding fsf_req is still pending */
409 spin_lock(&adapter->req_list_lock);
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200410 fsf_req = zfcp_reqlist_find(adapter,
411 (unsigned long) scpnt->host_scribble);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200412 spin_unlock(&adapter->req_list_lock);
413 if (!fsf_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200415 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200416 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 goto out;
418 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200419 fsf_req->data = 0;
420 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
421 old_req_id = fsf_req->req_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200423 /* don't access old fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200425
426 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
427 if (!fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200428 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100429 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200430 old_req_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto out;
433 }
434
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200435 __wait_event(fsf_req->completion_wq,
436 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200437
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200438 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
439 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 retval = SUCCESS;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200441 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
442 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else {
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200445 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200448 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return retval;
451}
452
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100453static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
455{
456 int retval;
457 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!unit) {
460 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
461 retval = SUCCESS;
462 goto out;
463 }
464 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
465
466 /*
467 * If we do not know whether the unit supports 'logical unit reset'
468 * then try 'logical unit reset' and proceed with 'target reset'
469 * if 'logical unit reset' fails.
470 * If the unit is known not to support 'logical unit reset' then
471 * skip 'logical unit reset' and try 'target reset' immediately.
472 */
473 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
474 &unit->status)) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200475 retval = zfcp_task_management_function(unit,
476 FCP_LOGICAL_UNIT_RESET,
477 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (retval) {
479 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
480 if (retval == -ENOTSUPP)
481 atomic_set_mask
482 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
483 &unit->status);
484 /* fall through and try 'target reset' next */
485 } else {
486 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
487 unit);
488 /* avoid 'target reset' */
489 retval = SUCCESS;
490 goto out;
491 }
492 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200493 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (retval) {
495 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
496 retval = FAILED;
497 } else {
498 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
499 retval = SUCCESS;
500 }
501 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return retval;
503}
504
505static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200506zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
507 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200511 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* issue task management function */
514 fsf_req = zfcp_fsf_send_fcp_command_task_management
515 (adapter, unit, tm_flags, 0);
516 if (!fsf_req) {
517 ZFCP_LOG_INFO("error: creation of task management request "
518 "failed for unit 0x%016Lx on port 0x%016Lx on "
519 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
520 zfcp_get_busid_by_adapter(adapter));
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200521 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 retval = -ENOMEM;
523 goto out;
524 }
525
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200526 __wait_event(fsf_req->completion_wq,
527 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
528
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200529 /*
530 * check completion status of task management function
531 */
532 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
533 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200535 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
536 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200538 } else
539 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200540
541 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 out:
543 return retval;
544}
545
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200546/**
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200547 * zfcp_scsi_eh_host_reset_handler - handler for host and bus reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100549static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200551 struct zfcp_unit *unit;
552 struct zfcp_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200554 unit = (struct zfcp_unit*) scpnt->device->hostdata;
555 adapter = unit->port->adapter;
556
557 ZFCP_LOG_NORMAL("host/bus reset because of problems with "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 "unit 0x%016Lx\n", unit->fcp_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Andreas Herrmann81654282006-09-18 22:30:36 +0200560 zfcp_erp_adapter_reopen(adapter, 0);
561 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200563 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566int
567zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
568{
569 int retval = 0;
570 static unsigned int unique_id = 0;
571
Michael Loehr9f287452007-05-09 11:01:24 +0200572 if (adapter->scsi_host)
573 goto out;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* register adapter as SCSI host with mid layer of SCSI stack */
576 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
577 sizeof (struct zfcp_adapter *));
578 if (!adapter->scsi_host) {
579 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
580 "for adapter %s ",
581 zfcp_get_busid_by_adapter(adapter));
582 retval = -EIO;
583 goto out;
584 }
585 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
586
587 /* tell the SCSI stack some characteristics of this adapter */
588 adapter->scsi_host->max_id = 1;
589 adapter->scsi_host->max_lun = 1;
590 adapter->scsi_host->max_channel = 0;
591 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
592 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200593 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /*
596 * save a pointer to our own adapter data structure within
597 * hostdata field of SCSI host data structure
598 */
599 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
600
601 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
602 scsi_host_put(adapter->scsi_host);
603 retval = -EIO;
604 goto out;
605 }
606 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
607 out:
608 return retval;
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611void
612zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
613{
614 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200615 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 shost = adapter->scsi_host;
618 if (!shost)
619 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200620 read_lock_irq(&zfcp_data.config_lock);
621 list_for_each_entry(port, &adapter->port_list_head, list)
622 if (port->rport)
623 port->rport = NULL;
624 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700625 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 scsi_remove_host(shost);
627 scsi_host_put(shost);
628 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
630
631 return;
632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
635 * Support functions for FC transport class
636 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100637static struct fc_host_statistics*
638zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100640 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100642 if (!adapter->fc_stats) {
643 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
644 if (!fc_stats)
645 return NULL;
646 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
647 }
648 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
649 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100653zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
654 struct fsf_qtcb_bottom_port *data,
655 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100657 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
658 old->seconds_since_last_reset;
659 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
660 fc_stats->tx_words = data->tx_words - old->tx_words;
661 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
662 fc_stats->rx_words = data->rx_words - old->rx_words;
663 fc_stats->lip_count = data->lip - old->lip;
664 fc_stats->nos_count = data->nos - old->nos;
665 fc_stats->error_frames = data->error_frames - old->error_frames;
666 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
667 fc_stats->link_failure_count = data->link_failure - old->link_failure;
668 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
669 fc_stats->loss_of_signal_count = data->loss_of_signal -
670 old->loss_of_signal;
671 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
672 old->psp_error_counts;
673 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
674 old->invalid_tx_words;
675 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
676 fc_stats->fcp_input_requests = data->input_requests -
677 old->input_requests;
678 fc_stats->fcp_output_requests = data->output_requests -
679 old->output_requests;
680 fc_stats->fcp_control_requests = data->control_requests -
681 old->control_requests;
682 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
683 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100687zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
688 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100690 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
691 fc_stats->tx_frames = data->tx_frames;
692 fc_stats->tx_words = data->tx_words;
693 fc_stats->rx_frames = data->rx_frames;
694 fc_stats->rx_words = data->rx_words;
695 fc_stats->lip_count = data->lip;
696 fc_stats->nos_count = data->nos;
697 fc_stats->error_frames = data->error_frames;
698 fc_stats->dumped_frames = data->dumped_frames;
699 fc_stats->link_failure_count = data->link_failure;
700 fc_stats->loss_of_sync_count = data->loss_of_sync;
701 fc_stats->loss_of_signal_count = data->loss_of_signal;
702 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
703 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
704 fc_stats->invalid_crc_count = data->invalid_crcs;
705 fc_stats->fcp_input_requests = data->input_requests;
706 fc_stats->fcp_output_requests = data->output_requests;
707 fc_stats->fcp_control_requests = data->control_requests;
708 fc_stats->fcp_input_megabytes = data->input_mb;
709 fc_stats->fcp_output_megabytes = data->output_mb;
710}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100712/**
713 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
714 *
715 * assumption: scsi_transport_fc synchronizes calls of
716 * get_fc_host_stats and reset_fc_host_stats
717 * (XXX to be checked otherwise introduce locking)
718 */
719static struct fc_host_statistics *
720zfcp_get_fc_host_stats(struct Scsi_Host *shost)
721{
722 struct zfcp_adapter *adapter;
723 struct fc_host_statistics *fc_stats;
724 struct fsf_qtcb_bottom_port *data;
725 int ret;
726
727 adapter = (struct zfcp_adapter *)shost->hostdata[0];
728 fc_stats = zfcp_init_fc_host_stats(adapter);
729 if (!fc_stats)
730 return NULL;
731
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200732 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100733 if (!data)
734 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100735
736 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
737 if (ret) {
738 kfree(data);
739 return NULL; /* XXX return zeroed fc_stats? */
740 }
741
742 if (adapter->stats_reset &&
743 ((jiffies/HZ - adapter->stats_reset) <
744 data->seconds_since_last_reset)) {
745 zfcp_adjust_fc_host_stats(fc_stats, data,
746 adapter->stats_reset_data);
747 } else
748 zfcp_set_fc_host_stats(fc_stats, data);
749
750 kfree(data);
751 return fc_stats;
752}
753
754static void
755zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
756{
757 struct zfcp_adapter *adapter;
758 struct fsf_qtcb_bottom_port *data, *old_data;
759 int ret;
760
761 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200762 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100763 if (!data)
764 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100765
766 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
767 if (ret == 0) {
768 adapter->stats_reset = jiffies/HZ;
769 old_data = adapter->stats_reset_data;
770 adapter->stats_reset_data = data; /* finally freed in
771 adater_dequeue */
772 kfree(old_data);
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Andreas Herrmann338151e2006-05-22 18:25:56 +0200776static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
777{
778 rport->dev_loss_tmo = timeout;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 .show_starget_port_id = 1,
783 .show_starget_port_name = 1,
784 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700785 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200786 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200787 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700788 .show_host_node_name = 1,
789 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100790 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700791 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100792 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200793 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700794 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100795 .get_fc_host_stats = zfcp_get_fc_host_stats,
796 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200797 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100798 /* no functions registered for following dynamic attributes but
799 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100800 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200801 .show_host_speed = 1,
802 .show_host_port_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
804
805/**
806 * ZFCP_DEFINE_SCSI_ATTR
807 * @_name: name of show attribute
808 * @_format: format string
809 * @_value: value to print
810 *
811 * Generates attribute for a unit.
812 */
813#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400814static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 char *buf) \
816{ \
817 struct scsi_device *sdev; \
818 struct zfcp_unit *unit; \
819 \
820 sdev = to_scsi_device(dev); \
821 unit = sdev->hostdata; \
822 return sprintf(buf, _format, _value); \
823} \
824 \
825static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
826
827ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
828ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
829ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
830
831static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
832 &dev_attr_fcp_lun,
833 &dev_attr_wwpn,
834 &dev_attr_hba_id,
835 NULL
836};
837
838#undef ZFCP_LOG_AREA