blob: 9e6d07d7b3c80cc5d77866e80c4eb38b273e167a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * linux/drivers/s390/scsi/zfcp_scsi.c
4 *
5 * FCP adapter driver for IBM eServer zSeries
6 *
7 * (C) Copyright IBM Corp. 2002, 2004
8 *
9 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
10 * Raimund Schroeder <raimund.schroeder@de.ibm.com>
11 * Aron Zeh
12 * Wolfgang Taphorn
13 * Stefan Bader <stefan.bader@de.ibm.com>
14 * Heiko Carstens <heiko.carstens@de.ibm.com>
15 * Andreas Herrmann <aherrman@de.ibm.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2, or (at your option)
20 * any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 */
31
32#define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "zfcp_ext.h"
35
36static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
37static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
38static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
39static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
40 void (*done) (struct scsi_cmnd *));
41static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
42static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
43static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
44static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020045static int zfcp_task_management_function(struct zfcp_unit *, u8,
46 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int, scsi_id_t,
49 scsi_lun_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static struct device_attribute *zfcp_sysfs_sdev_attrs[];
52
53struct scsi_transport_template *zfcp_transport_template;
54
55struct zfcp_data zfcp_data = {
56 .scsi_host_template = {
57 name: ZFCP_NAME,
58 proc_name: "zfcp",
59 proc_info: NULL,
60 detect: NULL,
61 slave_alloc: zfcp_scsi_slave_alloc,
62 slave_configure: zfcp_scsi_slave_configure,
63 slave_destroy: zfcp_scsi_slave_destroy,
64 queuecommand: zfcp_scsi_queuecommand,
65 eh_abort_handler: zfcp_scsi_eh_abort_handler,
66 eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
67 eh_bus_reset_handler: zfcp_scsi_eh_bus_reset_handler,
68 eh_host_reset_handler: zfcp_scsi_eh_host_reset_handler,
69 /* FIXME(openfcp): Tune */
70 can_queue: 4096,
Andreas Herrmann35dc25852006-03-02 21:28:54 +010071 this_id: -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /*
73 * FIXME:
74 * one less? can zfcp_create_sbale cope with it?
75 */
76 sg_tablesize: ZFCP_MAX_SBALES_PER_REQ,
77 cmd_per_lun: 1,
78 unchecked_isa_dma: 0,
79 use_clustering: 1,
80 sdev_attrs: zfcp_sysfs_sdev_attrs,
81 },
82 .driver_version = ZFCP_VERSION,
83 /* rest initialised with zeros */
84};
85
86/* Find start of Response Information in FCP response unit*/
87char *
88zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
89{
90 char *fcp_rsp_info_ptr;
91
92 fcp_rsp_info_ptr =
93 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
94
95 return fcp_rsp_info_ptr;
96}
97
98/* Find start of Sense Information in FCP response unit*/
99char *
100zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
101{
102 char *fcp_sns_info_ptr;
103
104 fcp_sns_info_ptr =
105 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
106 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
107 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
108 fcp_rsp_iu->fcp_rsp_len;
109
110 return fcp_sns_info_ptr;
111}
112
113fcp_dl_t *
114zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
115{
116 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
117 fcp_dl_t *fcp_dl_addr;
118
119 fcp_dl_addr = (fcp_dl_t *)
120 ((unsigned char *) fcp_cmd +
121 sizeof (struct fcp_cmnd_iu) + additional_length);
122 /*
123 * fcp_dl_addr = start address of fcp_cmnd structure +
124 * size of fixed part + size of dynamically sized add_dcp_cdb field
125 * SEE FCP-2 documentation
126 */
127 return fcp_dl_addr;
128}
129
130fcp_dl_t
131zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
132{
133 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
134}
135
136void
137zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
138{
139 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
140}
141
142/*
143 * note: it's a bit-or operation not an assignment
144 * regarding the specified byte
145 */
146static inline void
147set_byte(u32 * result, char status, char pos)
148{
149 *result |= status << (pos * 8);
150}
151
152void
153set_host_byte(u32 * result, char status)
154{
155 set_byte(result, status, 2);
156}
157
158void
159set_driver_byte(u32 * result, char status)
160{
161 set_byte(result, status, 3);
162}
163
164/*
165 * function: zfcp_scsi_slave_alloc
166 *
167 * purpose:
168 *
169 * returns:
170 */
171
172static int
173zfcp_scsi_slave_alloc(struct scsi_device *sdp)
174{
175 struct zfcp_adapter *adapter;
176 struct zfcp_unit *unit;
177 unsigned long flags;
Andreas Herrmannfb121b02005-12-01 02:49:29 +0100178 int retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
181 if (!adapter)
182 goto out;
183
184 read_lock_irqsave(&zfcp_data.config_lock, flags);
185 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100186 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
187 &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 sdp->hostdata = unit;
189 unit->device = sdp;
190 zfcp_unit_get(unit);
191 retval = 0;
192 }
193 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
194 out:
195 return retval;
196}
197
198/*
199 * function: zfcp_scsi_slave_destroy
200 *
201 * purpose:
202 *
203 * returns:
204 */
205
206static void
207zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
208{
209 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
210
211 if (unit) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100212 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 sdpnt->hostdata = NULL;
214 unit->device = NULL;
215 zfcp_unit_put(unit);
216 } else {
217 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
218 "address %p\n", sdpnt);
219 }
220}
221
222/*
223 * called from scsi midlayer to allow finetuning of a device.
224 */
225static int
226zfcp_scsi_slave_configure(struct scsi_device *sdp)
227{
228 if (sdp->tagged_supported)
229 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
230 else
231 scsi_adjust_queue_depth(sdp, 0, 1);
232 return 0;
233}
234
235/**
236 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
237 * @scpnt: pointer to struct scsi_cmnd where result is set
238 * @result: result to be set in scpnt (e.g. DID_ERROR)
239 */
240static void
241zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
242{
243 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200244 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
245 zfcp_scsi_dbf_event_result("fail", 4,
246 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100247 scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* return directly */
249 scpnt->scsi_done(scpnt);
250}
251
252/**
253 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
254 * zfcp_scsi_command_sync
255 * @adapter: adapter where scsi command is issued
256 * @unit: unit to which scsi command is sent
257 * @scpnt: scsi command to be sent
258 * @timer: timer to be started if request is successfully initiated
259 *
260 * Note: In scsi_done function must be set in scpnt.
261 */
262int
263zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
264 struct scsi_cmnd *scpnt, struct timer_list *timer)
265{
266 int tmp;
267 int retval;
268
269 retval = 0;
270
271 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
272 BUG_ON(scpnt->scsi_done == NULL);
273
274 if (unlikely(NULL == unit)) {
275 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
276 goto out;
277 }
278
279 if (unlikely(
280 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
281 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
282 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
283 "0x%016Lx on adapter %s\n",
284 unit->fcp_lun, unit->port->wwpn,
285 zfcp_get_busid_by_adapter(adapter));
286 zfcp_scsi_command_fail(scpnt, DID_ERROR);
287 goto out;
288 }
289
290 if (unlikely(
291 !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
292 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
293 "on port 0x%016Lx in recovery\n",
294 zfcp_get_busid_by_unit(unit),
295 unit->fcp_lun, unit->port->wwpn);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100296 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto out;
298 }
299
300 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
301 ZFCP_REQ_AUTO_CLEANUP);
302
303 if (unlikely(tmp < 0)) {
304 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
305 retval = SCSI_MLQUEUE_HOST_BUSY;
306 }
307
308out:
309 return retval;
310}
311
312void
313zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
314{
315 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
316 complete(wait);
317}
318
319
320/**
321 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
322 * @unit: unit where command is sent to
323 * @scpnt: scsi command to be sent
324 * @timer: timer to be started if request is successfully initiated
325 * Return: 0
326 *
327 * Errors are indicated in scpnt->result
328 */
329int
330zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
331 struct timer_list *timer)
332{
333 int ret;
334 DECLARE_COMPLETION(wait);
335
336 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
337 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
338 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
339 if (ret == 0)
340 wait_for_completion(&wait);
341
342 scpnt->SCp.ptr = NULL;
343
344 return 0;
345}
346
347/*
348 * function: zfcp_scsi_queuecommand
349 *
350 * purpose: enqueues a SCSI command to the specified target device
351 *
352 * returns: 0 - success, SCSI command enqueued
353 * !0 - failure
354 */
355int
356zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
357 void (*done) (struct scsi_cmnd *))
358{
359 struct zfcp_unit *unit;
360 struct zfcp_adapter *adapter;
361
362 /* reset the status for this request */
363 scpnt->result = 0;
364 scpnt->host_scribble = NULL;
365 scpnt->scsi_done = done;
366
367 /*
368 * figure out adapter and target device
369 * (stored there by zfcp_scsi_slave_alloc)
370 */
371 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
372 unit = (struct zfcp_unit *) scpnt->device->hostdata;
373
374 return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
375}
376
377/*
378 * function: zfcp_unit_lookup
379 *
380 * purpose:
381 *
382 * returns:
383 *
384 * context:
385 */
386static struct zfcp_unit *
387zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
388 scsi_lun_t lun)
389{
390 struct zfcp_port *port;
391 struct zfcp_unit *unit, *retval = NULL;
392
393 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700394 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 continue;
396 list_for_each_entry(unit, &port->unit_list_head, list) {
397 if (lun == unit->scsi_lun) {
398 retval = unit;
399 goto out;
400 }
401 }
402 }
403 out:
404 return retval;
405}
406
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200407/**
408 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
409 * @scpnt: pointer to scsi_cmnd to be aborted
410 * Return: SUCCESS - command has been aborted and cleaned up in internal
411 * bookkeeping, SCSI stack won't be called for aborted command
412 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200414 * We do not need to care for a SCSI command which completes normally
415 * but late during this abort routine runs. We are allowed to return
416 * late commands to the SCSI stack. It tracks the state of commands and
417 * will handle late commands. (Usually, the normal completion of late
418 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
420int
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200421zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200423 struct Scsi_Host *scsi_host;
424 struct zfcp_adapter *adapter;
425 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int retval = SUCCESS;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200427 struct zfcp_fsf_req *new_fsf_req = NULL;
428 struct zfcp_fsf_req *old_fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200431 scsi_host = scpnt->device->host;
432 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
433 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
436 scpnt, zfcp_get_busid_by_adapter(adapter));
437
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200438 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 write_lock_irqsave(&adapter->abort_lock, flags);
440
441 /*
442 * Check whether command has just completed and can not be aborted.
443 * Even if the command has just been completed late, we can access
444 * scpnt since the SCSI stack does not release it at least until
445 * this routine returns. (scpnt is parameter passed to this routine
446 * and must not disappear during abort even on late completion.)
447 */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200448 old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!old_fsf_req) {
450 write_unlock_irqrestore(&adapter->abort_lock, flags);
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100451 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, NULL);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200452 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto out;
454 }
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200455 old_fsf_req->data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200458 /* don't access old_fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 write_unlock_irqrestore(&adapter->abort_lock, flags);
460 /* call FSF routine which does the abort */
461 new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
462 adapter, unit, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!new_fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200464 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100465 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
466 old_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto out;
469 }
470
471 /* wait for completion of abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 __wait_event(new_fsf_req->completion_wq,
473 new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /* status should be valid since signals were not permitted */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200476 if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100477 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req,
478 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 retval = SUCCESS;
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200480 } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100481 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req,
482 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100485 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req,
486 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200489 zfcp_fsf_req_free(new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return retval;
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * function: zfcp_scsi_eh_device_reset_handler
496 *
497 * purpose:
498 *
499 * returns:
500 */
501int
502zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
503{
504 int retval;
505 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!unit) {
508 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
509 retval = SUCCESS;
510 goto out;
511 }
512 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
513
514 /*
515 * If we do not know whether the unit supports 'logical unit reset'
516 * then try 'logical unit reset' and proceed with 'target reset'
517 * if 'logical unit reset' fails.
518 * If the unit is known not to support 'logical unit reset' then
519 * skip 'logical unit reset' and try 'target reset' immediately.
520 */
521 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
522 &unit->status)) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200523 retval = zfcp_task_management_function(unit,
524 FCP_LOGICAL_UNIT_RESET,
525 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (retval) {
527 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
528 if (retval == -ENOTSUPP)
529 atomic_set_mask
530 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
531 &unit->status);
532 /* fall through and try 'target reset' next */
533 } else {
534 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
535 unit);
536 /* avoid 'target reset' */
537 retval = SUCCESS;
538 goto out;
539 }
540 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200541 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (retval) {
543 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
544 retval = FAILED;
545 } else {
546 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
547 retval = SUCCESS;
548 }
549 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return retval;
551}
552
553static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200554zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
555 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200559 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* issue task management function */
562 fsf_req = zfcp_fsf_send_fcp_command_task_management
563 (adapter, unit, tm_flags, 0);
564 if (!fsf_req) {
565 ZFCP_LOG_INFO("error: creation of task management request "
566 "failed for unit 0x%016Lx on port 0x%016Lx on "
567 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
568 zfcp_get_busid_by_adapter(adapter));
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200569 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 retval = -ENOMEM;
571 goto out;
572 }
573
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200574 __wait_event(fsf_req->completion_wq,
575 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
576
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200577 /*
578 * check completion status of task management function
579 */
580 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
581 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200583 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
584 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200586 } else
587 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200588
589 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 out:
591 return retval;
592}
593
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200594/**
595 * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
597int
598zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
599{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200600 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
601 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 ZFCP_LOG_NORMAL("bus reset because of problems with "
604 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200605 zfcp_erp_adapter_reopen(adapter, 0);
606 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200608 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200611/**
612 * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
614int
615zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
616{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200617 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
618 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ZFCP_LOG_NORMAL("host reset because of problems with "
621 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200622 zfcp_erp_adapter_reopen(adapter, 0);
623 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200625 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
629 * function:
630 *
631 * purpose:
632 *
633 * returns:
634 */
635int
636zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
637{
638 int retval = 0;
639 static unsigned int unique_id = 0;
640
641 /* register adapter as SCSI host with mid layer of SCSI stack */
642 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
643 sizeof (struct zfcp_adapter *));
644 if (!adapter->scsi_host) {
645 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
646 "for adapter %s ",
647 zfcp_get_busid_by_adapter(adapter));
648 retval = -EIO;
649 goto out;
650 }
651 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
652
653 /* tell the SCSI stack some characteristics of this adapter */
654 adapter->scsi_host->max_id = 1;
655 adapter->scsi_host->max_lun = 1;
656 adapter->scsi_host->max_channel = 0;
657 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
658 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
659 adapter->scsi_host->transportt = zfcp_transport_template;
660 /*
661 * Reverse mapping of the host number to avoid race condition
662 */
663 adapter->scsi_host_no = adapter->scsi_host->host_no;
664
665 /*
666 * save a pointer to our own adapter data structure within
667 * hostdata field of SCSI host data structure
668 */
669 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
670
671 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
672 scsi_host_put(adapter->scsi_host);
673 retval = -EIO;
674 goto out;
675 }
676 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
677 out:
678 return retval;
679}
680
681/*
682 * function:
683 *
684 * purpose:
685 *
686 * returns:
687 */
688void
689zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
690{
691 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200692 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 shost = adapter->scsi_host;
695 if (!shost)
696 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200697 read_lock_irq(&zfcp_data.config_lock);
698 list_for_each_entry(port, &adapter->port_list_head, list)
699 if (port->rport)
700 port->rport = NULL;
701 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700702 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 scsi_remove_host(shost);
704 scsi_host_put(shost);
705 adapter->scsi_host = NULL;
706 adapter->scsi_host_no = 0;
707 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
708
709 return;
710}
711
712
713void
714zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
715{
716 adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
717 adapter->scsi_er_timer.data = (unsigned long) adapter;
718 adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
719 add_timer(&adapter->scsi_er_timer);
720}
721
722/*
723 * Support functions for FC transport class
724 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100725static struct fc_host_statistics*
726zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100728 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100730 if (!adapter->fc_stats) {
731 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
732 if (!fc_stats)
733 return NULL;
734 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
735 }
736 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
737 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100741zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
742 struct fsf_qtcb_bottom_port *data,
743 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100745 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
746 old->seconds_since_last_reset;
747 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
748 fc_stats->tx_words = data->tx_words - old->tx_words;
749 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
750 fc_stats->rx_words = data->rx_words - old->rx_words;
751 fc_stats->lip_count = data->lip - old->lip;
752 fc_stats->nos_count = data->nos - old->nos;
753 fc_stats->error_frames = data->error_frames - old->error_frames;
754 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
755 fc_stats->link_failure_count = data->link_failure - old->link_failure;
756 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
757 fc_stats->loss_of_signal_count = data->loss_of_signal -
758 old->loss_of_signal;
759 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
760 old->psp_error_counts;
761 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
762 old->invalid_tx_words;
763 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
764 fc_stats->fcp_input_requests = data->input_requests -
765 old->input_requests;
766 fc_stats->fcp_output_requests = data->output_requests -
767 old->output_requests;
768 fc_stats->fcp_control_requests = data->control_requests -
769 old->control_requests;
770 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
771 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100775zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
776 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100778 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
779 fc_stats->tx_frames = data->tx_frames;
780 fc_stats->tx_words = data->tx_words;
781 fc_stats->rx_frames = data->rx_frames;
782 fc_stats->rx_words = data->rx_words;
783 fc_stats->lip_count = data->lip;
784 fc_stats->nos_count = data->nos;
785 fc_stats->error_frames = data->error_frames;
786 fc_stats->dumped_frames = data->dumped_frames;
787 fc_stats->link_failure_count = data->link_failure;
788 fc_stats->loss_of_sync_count = data->loss_of_sync;
789 fc_stats->loss_of_signal_count = data->loss_of_signal;
790 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
791 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
792 fc_stats->invalid_crc_count = data->invalid_crcs;
793 fc_stats->fcp_input_requests = data->input_requests;
794 fc_stats->fcp_output_requests = data->output_requests;
795 fc_stats->fcp_control_requests = data->control_requests;
796 fc_stats->fcp_input_megabytes = data->input_mb;
797 fc_stats->fcp_output_megabytes = data->output_mb;
798}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100800/**
801 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
802 *
803 * assumption: scsi_transport_fc synchronizes calls of
804 * get_fc_host_stats and reset_fc_host_stats
805 * (XXX to be checked otherwise introduce locking)
806 */
807static struct fc_host_statistics *
808zfcp_get_fc_host_stats(struct Scsi_Host *shost)
809{
810 struct zfcp_adapter *adapter;
811 struct fc_host_statistics *fc_stats;
812 struct fsf_qtcb_bottom_port *data;
813 int ret;
814
815 adapter = (struct zfcp_adapter *)shost->hostdata[0];
816 fc_stats = zfcp_init_fc_host_stats(adapter);
817 if (!fc_stats)
818 return NULL;
819
820 data = kmalloc(sizeof(*data), GFP_KERNEL);
821 if (!data)
822 return NULL;
823 memset(data, 0, sizeof(*data));
824
825 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
826 if (ret) {
827 kfree(data);
828 return NULL; /* XXX return zeroed fc_stats? */
829 }
830
831 if (adapter->stats_reset &&
832 ((jiffies/HZ - adapter->stats_reset) <
833 data->seconds_since_last_reset)) {
834 zfcp_adjust_fc_host_stats(fc_stats, data,
835 adapter->stats_reset_data);
836 } else
837 zfcp_set_fc_host_stats(fc_stats, data);
838
839 kfree(data);
840 return fc_stats;
841}
842
843static void
844zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
845{
846 struct zfcp_adapter *adapter;
847 struct fsf_qtcb_bottom_port *data, *old_data;
848 int ret;
849
850 adapter = (struct zfcp_adapter *)shost->hostdata[0];
851 data = kmalloc(sizeof(*data), GFP_KERNEL);
852 if (!data)
853 return;
854 memset(data, 0, sizeof(*data));
855
856 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
857 if (ret == 0) {
858 adapter->stats_reset = jiffies/HZ;
859 old_data = adapter->stats_reset_data;
860 adapter->stats_reset_data = data; /* finally freed in
861 adater_dequeue */
862 kfree(old_data);
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .show_starget_port_id = 1,
868 .show_starget_port_name = 1,
869 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700870 .show_rport_supported_classes = 1,
871 .show_host_node_name = 1,
872 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100873 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700874 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100875 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200876 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700877 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100878 .get_fc_host_stats = zfcp_get_fc_host_stats,
879 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
880 /* no functions registered for following dynamic attributes but
881 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100882 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200883 .show_host_speed = 1,
884 .show_host_port_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885};
886
887/**
888 * ZFCP_DEFINE_SCSI_ATTR
889 * @_name: name of show attribute
890 * @_format: format string
891 * @_value: value to print
892 *
893 * Generates attribute for a unit.
894 */
895#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400896static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 char *buf) \
898{ \
899 struct scsi_device *sdev; \
900 struct zfcp_unit *unit; \
901 \
902 sdev = to_scsi_device(dev); \
903 unit = sdev->hostdata; \
904 return sprintf(buf, _format, _value); \
905} \
906 \
907static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
908
909ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
910ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
911ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
912
913static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
914 &dev_attr_fcp_lun,
915 &dev_attr_wwpn,
916 &dev_attr_hba_id,
917 NULL
918};
919
920#undef ZFCP_LOG_AREA