blob: e0803757c0fa054c8f23df5f43f477a501518b14 [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,
71 this_id: 0,
72 /*
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);
186 if (unit) {
187 sdp->hostdata = unit;
188 unit->device = sdp;
189 zfcp_unit_get(unit);
190 retval = 0;
191 }
192 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
193 out:
194 return retval;
195}
196
197/*
198 * function: zfcp_scsi_slave_destroy
199 *
200 * purpose:
201 *
202 * returns:
203 */
204
205static void
206zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
207{
208 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
209
210 if (unit) {
211 sdpnt->hostdata = NULL;
212 unit->device = NULL;
213 zfcp_unit_put(unit);
214 } else {
215 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
216 "address %p\n", sdpnt);
217 }
218}
219
220/*
221 * called from scsi midlayer to allow finetuning of a device.
222 */
223static int
224zfcp_scsi_slave_configure(struct scsi_device *sdp)
225{
226 if (sdp->tagged_supported)
227 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
228 else
229 scsi_adjust_queue_depth(sdp, 0, 1);
230 return 0;
231}
232
233/**
234 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
235 * @scpnt: pointer to struct scsi_cmnd where result is set
236 * @result: result to be set in scpnt (e.g. DID_ERROR)
237 */
238static void
239zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
240{
241 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200242 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
243 zfcp_scsi_dbf_event_result("fail", 4,
244 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
245 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* return directly */
247 scpnt->scsi_done(scpnt);
248}
249
250/**
251 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
252 * zfcp_scsi_command_sync
253 * @adapter: adapter where scsi command is issued
254 * @unit: unit to which scsi command is sent
255 * @scpnt: scsi command to be sent
256 * @timer: timer to be started if request is successfully initiated
257 *
258 * Note: In scsi_done function must be set in scpnt.
259 */
260int
261zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
262 struct scsi_cmnd *scpnt, struct timer_list *timer)
263{
264 int tmp;
265 int retval;
266
267 retval = 0;
268
269 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
270 BUG_ON(scpnt->scsi_done == NULL);
271
272 if (unlikely(NULL == unit)) {
273 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
274 goto out;
275 }
276
277 if (unlikely(
278 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
279 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
280 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
281 "0x%016Lx on adapter %s\n",
282 unit->fcp_lun, unit->port->wwpn,
283 zfcp_get_busid_by_adapter(adapter));
284 zfcp_scsi_command_fail(scpnt, DID_ERROR);
285 goto out;
286 }
287
288 if (unlikely(
289 !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
290 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
291 "on port 0x%016Lx in recovery\n",
292 zfcp_get_busid_by_unit(unit),
293 unit->fcp_lun, unit->port->wwpn);
294 retval = SCSI_MLQUEUE_DEVICE_BUSY;
295 goto out;
296 }
297
298 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
299 ZFCP_REQ_AUTO_CLEANUP);
300
301 if (unlikely(tmp < 0)) {
302 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
303 retval = SCSI_MLQUEUE_HOST_BUSY;
304 }
305
306out:
307 return retval;
308}
309
310void
311zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
312{
313 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
314 complete(wait);
315}
316
317
318/**
319 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
320 * @unit: unit where command is sent to
321 * @scpnt: scsi command to be sent
322 * @timer: timer to be started if request is successfully initiated
323 * Return: 0
324 *
325 * Errors are indicated in scpnt->result
326 */
327int
328zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
329 struct timer_list *timer)
330{
331 int ret;
332 DECLARE_COMPLETION(wait);
333
334 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
335 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
336 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
337 if (ret == 0)
338 wait_for_completion(&wait);
339
340 scpnt->SCp.ptr = NULL;
341
342 return 0;
343}
344
345/*
346 * function: zfcp_scsi_queuecommand
347 *
348 * purpose: enqueues a SCSI command to the specified target device
349 *
350 * returns: 0 - success, SCSI command enqueued
351 * !0 - failure
352 */
353int
354zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
355 void (*done) (struct scsi_cmnd *))
356{
357 struct zfcp_unit *unit;
358 struct zfcp_adapter *adapter;
359
360 /* reset the status for this request */
361 scpnt->result = 0;
362 scpnt->host_scribble = NULL;
363 scpnt->scsi_done = done;
364
365 /*
366 * figure out adapter and target device
367 * (stored there by zfcp_scsi_slave_alloc)
368 */
369 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
370 unit = (struct zfcp_unit *) scpnt->device->hostdata;
371
372 return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
373}
374
375/*
376 * function: zfcp_unit_lookup
377 *
378 * purpose:
379 *
380 * returns:
381 *
382 * context:
383 */
384static struct zfcp_unit *
385zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
386 scsi_lun_t lun)
387{
388 struct zfcp_port *port;
389 struct zfcp_unit *unit, *retval = NULL;
390
391 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700392 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 continue;
394 list_for_each_entry(unit, &port->unit_list_head, list) {
395 if (lun == unit->scsi_lun) {
396 retval = unit;
397 goto out;
398 }
399 }
400 }
401 out:
402 return retval;
403}
404
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200405/**
406 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
407 * @scpnt: pointer to scsi_cmnd to be aborted
408 * Return: SUCCESS - command has been aborted and cleaned up in internal
409 * bookkeeping, SCSI stack won't be called for aborted command
410 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200412 * We do not need to care for a SCSI command which completes normally
413 * but late during this abort routine runs. We are allowed to return
414 * late commands to the SCSI stack. It tracks the state of commands and
415 * will handle late commands. (Usually, the normal completion of late
416 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
418int
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200419zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200421 struct Scsi_Host *scsi_host;
422 struct zfcp_adapter *adapter;
423 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int retval = SUCCESS;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200425 struct zfcp_fsf_req *new_fsf_req = NULL;
426 struct zfcp_fsf_req *old_fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200429 scsi_host = scpnt->device->host;
430 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
431 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
434 scpnt, zfcp_get_busid_by_adapter(adapter));
435
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200436 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 write_lock_irqsave(&adapter->abort_lock, flags);
438
439 /*
440 * Check whether command has just completed and can not be aborted.
441 * Even if the command has just been completed late, we can access
442 * scpnt since the SCSI stack does not release it at least until
443 * this routine returns. (scpnt is parameter passed to this routine
444 * and must not disappear during abort even on late completion.)
445 */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200446 old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!old_fsf_req) {
448 write_unlock_irqrestore(&adapter->abort_lock, flags);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200449 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, new_fsf_req);
450 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto out;
452 }
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200453 old_fsf_req->data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200456 /* don't access old_fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 write_unlock_irqrestore(&adapter->abort_lock, flags);
458 /* call FSF routine which does the abort */
459 new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
460 adapter, unit, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!new_fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200462 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out;
465 }
466
467 /* wait for completion of abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 __wait_event(new_fsf_req->completion_wq,
469 new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* status should be valid since signals were not permitted */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200472 if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200473 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 retval = SUCCESS;
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200475 } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200476 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 } else {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200479 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200482 zfcp_fsf_req_free(new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return retval;
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * function: zfcp_scsi_eh_device_reset_handler
489 *
490 * purpose:
491 *
492 * returns:
493 */
494int
495zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
496{
497 int retval;
498 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!unit) {
501 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
502 retval = SUCCESS;
503 goto out;
504 }
505 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
506
507 /*
508 * If we do not know whether the unit supports 'logical unit reset'
509 * then try 'logical unit reset' and proceed with 'target reset'
510 * if 'logical unit reset' fails.
511 * If the unit is known not to support 'logical unit reset' then
512 * skip 'logical unit reset' and try 'target reset' immediately.
513 */
514 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
515 &unit->status)) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200516 retval = zfcp_task_management_function(unit,
517 FCP_LOGICAL_UNIT_RESET,
518 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (retval) {
520 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
521 if (retval == -ENOTSUPP)
522 atomic_set_mask
523 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
524 &unit->status);
525 /* fall through and try 'target reset' next */
526 } else {
527 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
528 unit);
529 /* avoid 'target reset' */
530 retval = SUCCESS;
531 goto out;
532 }
533 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200534 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (retval) {
536 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
537 retval = FAILED;
538 } else {
539 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
540 retval = SUCCESS;
541 }
542 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return retval;
544}
545
546static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200547zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
548 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200552 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* issue task management function */
555 fsf_req = zfcp_fsf_send_fcp_command_task_management
556 (adapter, unit, tm_flags, 0);
557 if (!fsf_req) {
558 ZFCP_LOG_INFO("error: creation of task management request "
559 "failed for unit 0x%016Lx on port 0x%016Lx on "
560 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
561 zfcp_get_busid_by_adapter(adapter));
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200562 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 retval = -ENOMEM;
564 goto out;
565 }
566
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200567 __wait_event(fsf_req->completion_wq,
568 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
569
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200570 /*
571 * check completion status of task management function
572 */
573 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
574 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200576 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
577 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200579 } else
580 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200581
582 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 out:
584 return retval;
585}
586
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200587/**
588 * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
590int
591zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
592{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200593 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
594 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ZFCP_LOG_NORMAL("bus reset because of problems with "
597 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200598 zfcp_erp_adapter_reopen(adapter, 0);
599 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200601 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200604/**
605 * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
607int
608zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
609{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200610 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
611 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ZFCP_LOG_NORMAL("host reset because of problems with "
614 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200615 zfcp_erp_adapter_reopen(adapter, 0);
616 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200618 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621/*
622 * function:
623 *
624 * purpose:
625 *
626 * returns:
627 */
628int
629zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
630{
631 int retval = 0;
632 static unsigned int unique_id = 0;
633
634 /* register adapter as SCSI host with mid layer of SCSI stack */
635 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
636 sizeof (struct zfcp_adapter *));
637 if (!adapter->scsi_host) {
638 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
639 "for adapter %s ",
640 zfcp_get_busid_by_adapter(adapter));
641 retval = -EIO;
642 goto out;
643 }
644 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
645
646 /* tell the SCSI stack some characteristics of this adapter */
647 adapter->scsi_host->max_id = 1;
648 adapter->scsi_host->max_lun = 1;
649 adapter->scsi_host->max_channel = 0;
650 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
651 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
652 adapter->scsi_host->transportt = zfcp_transport_template;
653 /*
654 * Reverse mapping of the host number to avoid race condition
655 */
656 adapter->scsi_host_no = adapter->scsi_host->host_no;
657
658 /*
659 * save a pointer to our own adapter data structure within
660 * hostdata field of SCSI host data structure
661 */
662 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
663
664 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
665 scsi_host_put(adapter->scsi_host);
666 retval = -EIO;
667 goto out;
668 }
669 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
670 out:
671 return retval;
672}
673
674/*
675 * function:
676 *
677 * purpose:
678 *
679 * returns:
680 */
681void
682zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
683{
684 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200685 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 shost = adapter->scsi_host;
688 if (!shost)
689 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200690 read_lock_irq(&zfcp_data.config_lock);
691 list_for_each_entry(port, &adapter->port_list_head, list)
692 if (port->rport)
693 port->rport = NULL;
694 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700695 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 scsi_remove_host(shost);
697 scsi_host_put(shost);
698 adapter->scsi_host = NULL;
699 adapter->scsi_host_no = 0;
700 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
701
702 return;
703}
704
705
706void
707zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
708{
709 adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
710 adapter->scsi_er_timer.data = (unsigned long) adapter;
711 adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
712 add_timer(&adapter->scsi_er_timer);
713}
714
715/*
716 * Support functions for FC transport class
717 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100718static struct fc_host_statistics*
719zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100721 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100723 if (!adapter->fc_stats) {
724 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
725 if (!fc_stats)
726 return NULL;
727 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
728 }
729 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
730 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100734zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
735 struct fsf_qtcb_bottom_port *data,
736 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100738 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
739 old->seconds_since_last_reset;
740 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
741 fc_stats->tx_words = data->tx_words - old->tx_words;
742 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
743 fc_stats->rx_words = data->rx_words - old->rx_words;
744 fc_stats->lip_count = data->lip - old->lip;
745 fc_stats->nos_count = data->nos - old->nos;
746 fc_stats->error_frames = data->error_frames - old->error_frames;
747 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
748 fc_stats->link_failure_count = data->link_failure - old->link_failure;
749 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
750 fc_stats->loss_of_signal_count = data->loss_of_signal -
751 old->loss_of_signal;
752 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
753 old->psp_error_counts;
754 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
755 old->invalid_tx_words;
756 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
757 fc_stats->fcp_input_requests = data->input_requests -
758 old->input_requests;
759 fc_stats->fcp_output_requests = data->output_requests -
760 old->output_requests;
761 fc_stats->fcp_control_requests = data->control_requests -
762 old->control_requests;
763 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
764 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100768zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
769 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100771 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
772 fc_stats->tx_frames = data->tx_frames;
773 fc_stats->tx_words = data->tx_words;
774 fc_stats->rx_frames = data->rx_frames;
775 fc_stats->rx_words = data->rx_words;
776 fc_stats->lip_count = data->lip;
777 fc_stats->nos_count = data->nos;
778 fc_stats->error_frames = data->error_frames;
779 fc_stats->dumped_frames = data->dumped_frames;
780 fc_stats->link_failure_count = data->link_failure;
781 fc_stats->loss_of_sync_count = data->loss_of_sync;
782 fc_stats->loss_of_signal_count = data->loss_of_signal;
783 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
784 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
785 fc_stats->invalid_crc_count = data->invalid_crcs;
786 fc_stats->fcp_input_requests = data->input_requests;
787 fc_stats->fcp_output_requests = data->output_requests;
788 fc_stats->fcp_control_requests = data->control_requests;
789 fc_stats->fcp_input_megabytes = data->input_mb;
790 fc_stats->fcp_output_megabytes = data->output_mb;
791}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100793/**
794 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
795 *
796 * assumption: scsi_transport_fc synchronizes calls of
797 * get_fc_host_stats and reset_fc_host_stats
798 * (XXX to be checked otherwise introduce locking)
799 */
800static struct fc_host_statistics *
801zfcp_get_fc_host_stats(struct Scsi_Host *shost)
802{
803 struct zfcp_adapter *adapter;
804 struct fc_host_statistics *fc_stats;
805 struct fsf_qtcb_bottom_port *data;
806 int ret;
807
808 adapter = (struct zfcp_adapter *)shost->hostdata[0];
809 fc_stats = zfcp_init_fc_host_stats(adapter);
810 if (!fc_stats)
811 return NULL;
812
813 data = kmalloc(sizeof(*data), GFP_KERNEL);
814 if (!data)
815 return NULL;
816 memset(data, 0, sizeof(*data));
817
818 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
819 if (ret) {
820 kfree(data);
821 return NULL; /* XXX return zeroed fc_stats? */
822 }
823
824 if (adapter->stats_reset &&
825 ((jiffies/HZ - adapter->stats_reset) <
826 data->seconds_since_last_reset)) {
827 zfcp_adjust_fc_host_stats(fc_stats, data,
828 adapter->stats_reset_data);
829 } else
830 zfcp_set_fc_host_stats(fc_stats, data);
831
832 kfree(data);
833 return fc_stats;
834}
835
836static void
837zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
838{
839 struct zfcp_adapter *adapter;
840 struct fsf_qtcb_bottom_port *data, *old_data;
841 int ret;
842
843 adapter = (struct zfcp_adapter *)shost->hostdata[0];
844 data = kmalloc(sizeof(*data), GFP_KERNEL);
845 if (!data)
846 return;
847 memset(data, 0, sizeof(*data));
848
849 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
850 if (ret == 0) {
851 adapter->stats_reset = jiffies/HZ;
852 old_data = adapter->stats_reset_data;
853 adapter->stats_reset_data = data; /* finally freed in
854 adater_dequeue */
855 kfree(old_data);
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .show_starget_port_id = 1,
861 .show_starget_port_name = 1,
862 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700863 .show_rport_supported_classes = 1,
864 .show_host_node_name = 1,
865 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100866 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700867 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100868 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200869 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700870 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100871 .get_fc_host_stats = zfcp_get_fc_host_stats,
872 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
873 /* no functions registered for following dynamic attributes but
874 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100875 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200876 .show_host_speed = 1,
877 .show_host_port_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878};
879
880/**
881 * ZFCP_DEFINE_SCSI_ATTR
882 * @_name: name of show attribute
883 * @_format: format string
884 * @_value: value to print
885 *
886 * Generates attribute for a unit.
887 */
888#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400889static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 char *buf) \
891{ \
892 struct scsi_device *sdev; \
893 struct zfcp_unit *unit; \
894 \
895 sdev = to_scsi_device(dev); \
896 unit = sdev->hostdata; \
897 return sprintf(buf, _format, _value); \
898} \
899 \
900static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
901
902ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
903ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
904ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
905
906static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
907 &dev_attr_fcp_lun,
908 &dev_attr_wwpn,
909 &dev_attr_hba_id,
910 NULL
911};
912
913#undef ZFCP_LOG_AREA