blob: eeb5152854ed1d30b34633590efada8595815aa0 [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
34#define ZFCP_SCSI_REVISION "$Revision: 1.74 $"
35
36#include "zfcp_ext.h"
37
38static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
39static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
40static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
41static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
42 void (*done) (struct scsi_cmnd *));
43static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
44static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
45static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
46static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
47static int zfcp_task_management_function(struct zfcp_unit *, u8);
48
49static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int, scsi_id_t,
50 scsi_lun_t);
51static struct zfcp_port *zfcp_port_lookup(struct zfcp_adapter *, int,
52 scsi_id_t);
53
54static struct device_attribute *zfcp_sysfs_sdev_attrs[];
55
56struct scsi_transport_template *zfcp_transport_template;
57
58struct zfcp_data zfcp_data = {
59 .scsi_host_template = {
60 name: ZFCP_NAME,
61 proc_name: "zfcp",
62 proc_info: NULL,
63 detect: NULL,
64 slave_alloc: zfcp_scsi_slave_alloc,
65 slave_configure: zfcp_scsi_slave_configure,
66 slave_destroy: zfcp_scsi_slave_destroy,
67 queuecommand: zfcp_scsi_queuecommand,
68 eh_abort_handler: zfcp_scsi_eh_abort_handler,
69 eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
70 eh_bus_reset_handler: zfcp_scsi_eh_bus_reset_handler,
71 eh_host_reset_handler: zfcp_scsi_eh_host_reset_handler,
72 /* FIXME(openfcp): Tune */
73 can_queue: 4096,
74 this_id: 0,
75 /*
76 * FIXME:
77 * one less? can zfcp_create_sbale cope with it?
78 */
79 sg_tablesize: ZFCP_MAX_SBALES_PER_REQ,
80 cmd_per_lun: 1,
81 unchecked_isa_dma: 0,
82 use_clustering: 1,
83 sdev_attrs: zfcp_sysfs_sdev_attrs,
84 },
85 .driver_version = ZFCP_VERSION,
86 /* rest initialised with zeros */
87};
88
89/* Find start of Response Information in FCP response unit*/
90char *
91zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
92{
93 char *fcp_rsp_info_ptr;
94
95 fcp_rsp_info_ptr =
96 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
97
98 return fcp_rsp_info_ptr;
99}
100
101/* Find start of Sense Information in FCP response unit*/
102char *
103zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
104{
105 char *fcp_sns_info_ptr;
106
107 fcp_sns_info_ptr =
108 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
109 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
110 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
111 fcp_rsp_iu->fcp_rsp_len;
112
113 return fcp_sns_info_ptr;
114}
115
116fcp_dl_t *
117zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
118{
119 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
120 fcp_dl_t *fcp_dl_addr;
121
122 fcp_dl_addr = (fcp_dl_t *)
123 ((unsigned char *) fcp_cmd +
124 sizeof (struct fcp_cmnd_iu) + additional_length);
125 /*
126 * fcp_dl_addr = start address of fcp_cmnd structure +
127 * size of fixed part + size of dynamically sized add_dcp_cdb field
128 * SEE FCP-2 documentation
129 */
130 return fcp_dl_addr;
131}
132
133fcp_dl_t
134zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
135{
136 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
137}
138
139void
140zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
141{
142 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
143}
144
145/*
146 * note: it's a bit-or operation not an assignment
147 * regarding the specified byte
148 */
149static inline void
150set_byte(u32 * result, char status, char pos)
151{
152 *result |= status << (pos * 8);
153}
154
155void
156set_host_byte(u32 * result, char status)
157{
158 set_byte(result, status, 2);
159}
160
161void
162set_driver_byte(u32 * result, char status)
163{
164 set_byte(result, status, 3);
165}
166
167/*
168 * function: zfcp_scsi_slave_alloc
169 *
170 * purpose:
171 *
172 * returns:
173 */
174
175static int
176zfcp_scsi_slave_alloc(struct scsi_device *sdp)
177{
178 struct zfcp_adapter *adapter;
179 struct zfcp_unit *unit;
180 unsigned long flags;
181 int retval = -ENODEV;
182
183 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
184 if (!adapter)
185 goto out;
186
187 read_lock_irqsave(&zfcp_data.config_lock, flags);
188 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
189 if (unit) {
190 sdp->hostdata = unit;
191 unit->device = sdp;
192 zfcp_unit_get(unit);
193 retval = 0;
194 }
195 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
196 out:
197 return retval;
198}
199
200/*
201 * function: zfcp_scsi_slave_destroy
202 *
203 * purpose:
204 *
205 * returns:
206 */
207
208static void
209zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
210{
211 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
212
213 if (unit) {
214 sdpnt->hostdata = NULL;
215 unit->device = NULL;
216 zfcp_unit_put(unit);
217 } else {
218 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
219 "address %p\n", sdpnt);
220 }
221}
222
223/*
224 * called from scsi midlayer to allow finetuning of a device.
225 */
226static int
227zfcp_scsi_slave_configure(struct scsi_device *sdp)
228{
229 if (sdp->tagged_supported)
230 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
231 else
232 scsi_adjust_queue_depth(sdp, 0, 1);
233 return 0;
234}
235
236/**
237 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
238 * @scpnt: pointer to struct scsi_cmnd where result is set
239 * @result: result to be set in scpnt (e.g. DID_ERROR)
240 */
241static void
242zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
243{
244 set_host_byte(&scpnt->result, result);
245 zfcp_cmd_dbf_event_scsi("failing", scpnt);
246 /* 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
405static struct zfcp_port *
406zfcp_port_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id)
407{
408 struct zfcp_port *port;
409
410 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700411 if (port->rport && (id == port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return port;
413 }
414 return (struct zfcp_port *) NULL;
415}
416
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200417/**
418 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
419 * @scpnt: pointer to scsi_cmnd to be aborted
420 * Return: SUCCESS - command has been aborted and cleaned up in internal
421 * bookkeeping, SCSI stack won't be called for aborted command
422 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200424 * We do not need to care for a SCSI command which completes normally
425 * but late during this abort routine runs. We are allowed to return
426 * late commands to the SCSI stack. It tracks the state of commands and
427 * will handle late commands. (Usually, the normal completion of late
428 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
430int
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200431zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200433 struct Scsi_Host *scsi_host;
434 struct zfcp_adapter *adapter;
435 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int retval = SUCCESS;
437 struct zfcp_fsf_req *new_fsf_req, *old_fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200440 scsi_host = scpnt->device->host;
441 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
442 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
445 scpnt, zfcp_get_busid_by_adapter(adapter));
446
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200447 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 write_lock_irqsave(&adapter->abort_lock, flags);
449
450 /*
451 * Check whether command has just completed and can not be aborted.
452 * Even if the command has just been completed late, we can access
453 * scpnt since the SCSI stack does not release it at least until
454 * this routine returns. (scpnt is parameter passed to this routine
455 * and must not disappear during abort even on late completion.)
456 */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200457 old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (!old_fsf_req) {
459 write_unlock_irqrestore(&adapter->abort_lock, flags);
460 ZFCP_LOG_NORMAL("bug: no old fsf request found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 ZFCP_LOG_NORMAL("scsi_cmnd:\n");
462 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
463 (char *) scpnt, sizeof (struct scsi_cmnd));
464 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto out;
466 }
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200467 old_fsf_req->data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200470 /* don't access old_fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 write_unlock_irqrestore(&adapter->abort_lock, flags);
472 /* call FSF routine which does the abort */
473 new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
474 adapter, unit, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!new_fsf_req) {
476 retval = FAILED;
477 ZFCP_LOG_NORMAL("error: initiation of Abort FCP Cmnd "
478 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto out;
480 }
481
482 /* wait for completion of abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 __wait_event(new_fsf_req->completion_wq,
484 new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann1db2c9c2005-06-13 13:20:35 +0200485 zfcp_fsf_req_free(new_fsf_req);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* status should be valid since signals were not permitted */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200488 if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 retval = SUCCESS;
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200490 } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
493 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return retval;
497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/*
500 * function: zfcp_scsi_eh_device_reset_handler
501 *
502 * purpose:
503 *
504 * returns:
505 */
506int
507zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
508{
509 int retval;
510 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!unit) {
513 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
514 retval = SUCCESS;
515 goto out;
516 }
517 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
518
519 /*
520 * If we do not know whether the unit supports 'logical unit reset'
521 * then try 'logical unit reset' and proceed with 'target reset'
522 * if 'logical unit reset' fails.
523 * If the unit is known not to support 'logical unit reset' then
524 * skip 'logical unit reset' and try 'target reset' immediately.
525 */
526 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
527 &unit->status)) {
528 retval =
529 zfcp_task_management_function(unit, FCP_LOGICAL_UNIT_RESET);
530 if (retval) {
531 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
532 if (retval == -ENOTSUPP)
533 atomic_set_mask
534 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
535 &unit->status);
536 /* fall through and try 'target reset' next */
537 } else {
538 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
539 unit);
540 /* avoid 'target reset' */
541 retval = SUCCESS;
542 goto out;
543 }
544 }
545 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET);
546 if (retval) {
547 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
548 retval = FAILED;
549 } else {
550 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
551 retval = SUCCESS;
552 }
553 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return retval;
555}
556
557static int
558zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags)
559{
560 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200562 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* issue task management function */
565 fsf_req = zfcp_fsf_send_fcp_command_task_management
566 (adapter, unit, tm_flags, 0);
567 if (!fsf_req) {
568 ZFCP_LOG_INFO("error: creation of task management request "
569 "failed for unit 0x%016Lx on port 0x%016Lx on "
570 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
571 zfcp_get_busid_by_adapter(adapter));
572 retval = -ENOMEM;
573 goto out;
574 }
575
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200576 __wait_event(fsf_req->completion_wq,
577 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
578
579 /* check completion status of task management function */
580 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 retval = -EIO;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200582 else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 retval = -ENOTSUPP;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200584
585 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 out:
587 return retval;
588}
589
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200590/**
591 * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
593int
594zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
595{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200596 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
597 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ZFCP_LOG_NORMAL("bus reset because of problems with "
600 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200601 zfcp_erp_adapter_reopen(adapter, 0);
602 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200604 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200607/**
608 * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
610int
611zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
612{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200613 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
614 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ZFCP_LOG_NORMAL("host reset because of problems with "
617 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200618 zfcp_erp_adapter_reopen(adapter, 0);
619 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200621 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
625 * function:
626 *
627 * purpose:
628 *
629 * returns:
630 */
631int
632zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
633{
634 int retval = 0;
635 static unsigned int unique_id = 0;
636
637 /* register adapter as SCSI host with mid layer of SCSI stack */
638 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
639 sizeof (struct zfcp_adapter *));
640 if (!adapter->scsi_host) {
641 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
642 "for adapter %s ",
643 zfcp_get_busid_by_adapter(adapter));
644 retval = -EIO;
645 goto out;
646 }
647 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
648
649 /* tell the SCSI stack some characteristics of this adapter */
650 adapter->scsi_host->max_id = 1;
651 adapter->scsi_host->max_lun = 1;
652 adapter->scsi_host->max_channel = 0;
653 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
654 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
655 adapter->scsi_host->transportt = zfcp_transport_template;
656 /*
657 * Reverse mapping of the host number to avoid race condition
658 */
659 adapter->scsi_host_no = adapter->scsi_host->host_no;
660
661 /*
662 * save a pointer to our own adapter data structure within
663 * hostdata field of SCSI host data structure
664 */
665 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
666
667 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
668 scsi_host_put(adapter->scsi_host);
669 retval = -EIO;
670 goto out;
671 }
672 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
673 out:
674 return retval;
675}
676
677/*
678 * function:
679 *
680 * purpose:
681 *
682 * returns:
683 */
684void
685zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
686{
687 struct Scsi_Host *shost;
688
689 shost = adapter->scsi_host;
690 if (!shost)
691 return;
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700692 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 scsi_remove_host(shost);
694 scsi_host_put(shost);
695 adapter->scsi_host = NULL;
696 adapter->scsi_host_no = 0;
697 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
698
699 return;
700}
701
702
703void
704zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
705{
706 adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
707 adapter->scsi_er_timer.data = (unsigned long) adapter;
708 adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
709 add_timer(&adapter->scsi_er_timer);
710}
711
712/*
713 * Support functions for FC transport class
714 */
715static void
716zfcp_get_port_id(struct scsi_target *starget)
717{
718 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
719 struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
720 struct zfcp_port *port;
721 unsigned long flags;
722
723 read_lock_irqsave(&zfcp_data.config_lock, flags);
724 port = zfcp_port_lookup(adapter, starget->channel, starget->id);
725 if (port)
726 fc_starget_port_id(starget) = port->d_id;
727 else
728 fc_starget_port_id(starget) = -1;
729 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
730}
731
732static void
733zfcp_get_port_name(struct scsi_target *starget)
734{
735 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
736 struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
737 struct zfcp_port *port;
738 unsigned long flags;
739
740 read_lock_irqsave(&zfcp_data.config_lock, flags);
741 port = zfcp_port_lookup(adapter, starget->channel, starget->id);
742 if (port)
743 fc_starget_port_name(starget) = port->wwpn;
744 else
745 fc_starget_port_name(starget) = -1;
746 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
747}
748
749static void
750zfcp_get_node_name(struct scsi_target *starget)
751{
752 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
753 struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
754 struct zfcp_port *port;
755 unsigned long flags;
756
757 read_lock_irqsave(&zfcp_data.config_lock, flags);
758 port = zfcp_port_lookup(adapter, starget->channel, starget->id);
759 if (port)
760 fc_starget_node_name(starget) = port->wwnn;
761 else
762 fc_starget_node_name(starget) = -1;
763 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
764}
765
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700766void
767zfcp_set_fc_host_attrs(struct zfcp_adapter *adapter)
768{
769 struct Scsi_Host *shost = adapter->scsi_host;
770
771 fc_host_node_name(shost) = adapter->wwnn;
772 fc_host_port_name(shost) = adapter->wwpn;
773 strncpy(fc_host_serial_number(shost), adapter->serial_number,
774 min(FC_SERIAL_NUMBER_SIZE, 32));
775 fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778struct fc_function_template zfcp_transport_functions = {
779 .get_starget_port_id = zfcp_get_port_id,
780 .get_starget_port_name = zfcp_get_port_name,
781 .get_starget_node_name = zfcp_get_node_name,
782 .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,
786 .show_host_node_name = 1,
787 .show_host_port_name = 1,
788 .show_host_supported_classes = 1,
789 .show_host_serial_number = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790};
791
792/**
793 * ZFCP_DEFINE_SCSI_ATTR
794 * @_name: name of show attribute
795 * @_format: format string
796 * @_value: value to print
797 *
798 * Generates attribute for a unit.
799 */
800#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400801static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 char *buf) \
803{ \
804 struct scsi_device *sdev; \
805 struct zfcp_unit *unit; \
806 \
807 sdev = to_scsi_device(dev); \
808 unit = sdev->hostdata; \
809 return sprintf(buf, _format, _value); \
810} \
811 \
812static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
813
814ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
815ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
816ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
817
818static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
819 &dev_attr_fcp_lun,
820 &dev_attr_wwpn,
821 &dev_attr_hba_id,
822 NULL
823};
824
825#undef ZFCP_LOG_AREA