vserver 1.9.3
[linux-2.6.git] / drivers / s390 / scsi / zfcp_scsi.c
1 /* 
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  * 
16  * This program is free software; you can redistribute it and/or modify 
17  * it under the terms of the GNU General Public License as published by 
18  * the Free Software Foundation; either version 2, or (at your option) 
19  * any later version. 
20  * 
21  * This program is distributed in the hope that it will be useful, 
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
24  * GNU General Public License for more details. 
25  * 
26  * You should have received a copy of the GNU General Public License 
27  * along with this program; if not, write to the Free Software 
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
29  */
30
31 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_SCSI
32
33 /* this drivers version (do not edit !!! generated and updated by cvs) */
34 #define ZFCP_SCSI_REVISION "$Revision: 1.68 $"
35
36 #include "zfcp_ext.h"
37
38 static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
39 static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
40 static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
41 static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
42                                   void (*done) (struct scsi_cmnd *));
43 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
44 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
45 static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
46 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
47 static int zfcp_task_management_function(struct zfcp_unit *, u8);
48
49 static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int, scsi_id_t,
50                                           scsi_lun_t);
51
52 static struct device_attribute *zfcp_sysfs_sdev_attrs[];
53
54 struct scsi_transport_template *zfcp_transport_template;
55
56 struct zfcp_data zfcp_data = {
57         .scsi_host_template = {
58               name:                    ZFCP_NAME,
59               proc_name:               "zfcp",
60               proc_info:               NULL,
61               detect:                  NULL,
62               slave_alloc:             zfcp_scsi_slave_alloc,
63               slave_configure:         zfcp_scsi_slave_configure,
64               slave_destroy:           zfcp_scsi_slave_destroy,
65               queuecommand:            zfcp_scsi_queuecommand,
66               eh_abort_handler:        zfcp_scsi_eh_abort_handler,
67               eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
68               eh_bus_reset_handler:    zfcp_scsi_eh_bus_reset_handler,
69               eh_host_reset_handler:   zfcp_scsi_eh_host_reset_handler,
70                                        /* FIXME(openfcp): Tune */
71               can_queue:               4096,
72               this_id:                 0,
73               /*
74                * FIXME:
75                * one less? can zfcp_create_sbale cope with it?
76                */
77               sg_tablesize:            ZFCP_MAX_SBALES_PER_REQ,
78               cmd_per_lun:             1,
79               unchecked_isa_dma:       0,
80               use_clustering:          1,
81               sdev_attrs:              zfcp_sysfs_sdev_attrs,
82         }
83         /* rest initialised with zeros */
84 };
85
86 /* Find start of Response Information in FCP response unit*/
87 char *
88 zfcp_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*/
99 char *
100 zfcp_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
113 fcp_dl_t *
114 zfcp_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
130 fcp_dl_t
131 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
132 {
133         return *zfcp_get_fcp_dl_ptr(fcp_cmd);
134 }
135
136 void
137 zfcp_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  */
146 static inline void
147 set_byte(u32 * result, char status, char pos)
148 {
149         *result |= status << (pos * 8);
150 }
151
152 void
153 set_host_byte(u32 * result, char status)
154 {
155         set_byte(result, status, 2);
156 }
157
158 void
159 set_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
172 static int
173 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
174 {
175         struct zfcp_adapter *adapter;
176         struct zfcp_unit *unit;
177         unsigned long flags;
178         int retval = -ENODEV;
179
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
205 static void
206 zfcp_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  */
223 static int
224 zfcp_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  */
238 static void
239 zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
240 {
241         set_host_byte(&scpnt->result, result);
242         zfcp_cmd_dbf_event_scsi("failing", scpnt);
243         /* return directly */
244         scpnt->scsi_done(scpnt);
245 }
246
247 /**
248  * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
249  *      zfcp_scsi_command_sync
250  * @adapter: adapter where scsi command is issued
251  * @unit: unit to which scsi command is sent
252  * @scpnt: scsi command to be sent
253  * @timer: timer to be started if request is successfully initiated
254  *
255  * Note: In scsi_done function must be set in scpnt.
256  */
257 int
258 zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
259                         struct scsi_cmnd *scpnt, struct timer_list *timer)
260 {
261         int tmp;
262         int retval;
263
264         retval = 0;
265
266         BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
267         BUG_ON(scpnt->scsi_done == NULL);
268
269         if (unlikely(NULL == unit)) {
270                 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
271                 goto out;
272         }
273
274         if (unlikely(
275               atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
276              !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
277                 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
278                                "0x%016Lx on adapter %s\n",
279                                unit->fcp_lun, unit->port->wwpn,
280                                zfcp_get_busid_by_adapter(adapter));
281                 zfcp_scsi_command_fail(scpnt, DID_ERROR);
282                 goto out;
283         }
284
285         if (unlikely(
286              !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
287                 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
288                                "on port 0x%016Lx in recovery\n",
289                                zfcp_get_busid_by_unit(unit),
290                                unit->fcp_lun, unit->port->wwpn);
291                 retval = SCSI_MLQUEUE_DEVICE_BUSY;
292                 goto out;
293         }
294
295         tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
296                                              ZFCP_REQ_AUTO_CLEANUP);
297
298         if (unlikely(tmp < 0)) {
299                 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
300                 retval = SCSI_MLQUEUE_HOST_BUSY;
301         }
302
303 out:
304         return retval;
305 }
306
307 void
308 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
309 {
310         struct completion *wait = (struct completion *) scpnt->SCp.ptr;
311         complete(wait);
312 }
313
314
315 /**
316  * zfcp_scsi_command_sync - send a SCSI command and wait for completion
317  * @unit: unit where command is sent to
318  * @scpnt: scsi command to be sent
319  * @timer: timer to be started if request is successfully initiated
320  * Return: 0
321  *
322  * Errors are indicated in scpnt->result
323  */
324 int
325 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
326                        struct timer_list *timer)
327 {
328         int ret;
329         DECLARE_COMPLETION(wait);
330
331         scpnt->SCp.ptr = (void *) &wait;  /* silent re-use */
332         scpnt->scsi_done = zfcp_scsi_command_sync_handler;
333         ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
334         if ((ret == 0) && (scpnt->result == 0))
335         wait_for_completion(&wait);
336
337         scpnt->SCp.ptr = NULL;
338
339         return 0;
340 }
341
342 /*
343  * function:    zfcp_scsi_queuecommand
344  *
345  * purpose:     enqueues a SCSI command to the specified target device
346  *
347  * returns:     0 - success, SCSI command enqueued
348  *              !0 - failure
349  */
350 int
351 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
352                        void (*done) (struct scsi_cmnd *))
353 {
354         struct zfcp_unit *unit;
355         struct zfcp_adapter *adapter;
356
357         /* reset the status for this request */
358         scpnt->result = 0;
359         scpnt->host_scribble = NULL;
360         scpnt->scsi_done = done;
361
362         /*
363          * figure out adapter and target device
364          * (stored there by zfcp_scsi_slave_alloc)
365          */
366         adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
367         unit = (struct zfcp_unit *) scpnt->device->hostdata;
368
369         return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
370 }
371
372 /*
373  * function:    zfcp_unit_lookup
374  *
375  * purpose:
376  *
377  * returns:
378  *
379  * context:     
380  */
381 static struct zfcp_unit *
382 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
383                  scsi_lun_t lun)
384 {
385         struct zfcp_port *port;
386         struct zfcp_unit *unit, *retval = NULL;
387
388         list_for_each_entry(port, &adapter->port_list_head, list) {
389                 if (id != port->scsi_id)
390                         continue;
391                 list_for_each_entry(unit, &port->unit_list_head, list) {
392                         if (lun == unit->scsi_lun) {
393                                 retval = unit;
394                                 goto out;
395                         }
396                 }
397         }
398  out:
399         return retval;
400 }
401
402 /*
403  * function:    zfcp_scsi_eh_abort_handler
404  *
405  * purpose:     tries to abort the specified (timed out) SCSI command
406  *
407  * note:        We do not need to care for a SCSI command which completes
408  *              normally but late during this abort routine runs.
409  *              We are allowed to return late commands to the SCSI stack.
410  *              It tracks the state of commands and will handle late commands.
411  *              (Usually, the normal completion of late commands is ignored with
412  *              respect to the running abort operation. Grep for 'done_late'
413  *              in the SCSI stacks sources.)
414  *
415  * returns:     SUCCESS - command has been aborted and cleaned up in internal
416  *                        bookkeeping,
417  *                        SCSI stack won't be called for aborted command
418  *              FAILED  - otherwise
419  */
420 int
421 zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
422 {
423         int retval = SUCCESS;
424         struct zfcp_fsf_req *new_fsf_req, *old_fsf_req;
425         struct zfcp_adapter *adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
426         struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
427         struct zfcp_port *port = unit->port;
428         struct Scsi_Host *scsi_host = scpnt->device->host;
429         union zfcp_req_data *req_data = NULL;
430         unsigned long flags;
431         u32 status = 0;
432
433         /* the components of a abort_dbf record (fixed size record) */
434         u64 dbf_scsi_cmnd = (unsigned long) scpnt;
435         char dbf_opcode[ZFCP_ABORT_DBF_LENGTH];
436         wwn_t dbf_wwn = port->wwpn;
437         fcp_lun_t dbf_fcp_lun = unit->fcp_lun;
438         u64 dbf_retries = scpnt->retries;
439         u64 dbf_allowed = scpnt->allowed;
440         u64 dbf_timeout = 0;
441         u64 dbf_fsf_req = 0;
442         u64 dbf_fsf_status = 0;
443         u64 dbf_fsf_qual[2] = { 0, 0 };
444         char dbf_result[ZFCP_ABORT_DBF_LENGTH] = "##undef";
445
446         memset(dbf_opcode, 0, ZFCP_ABORT_DBF_LENGTH);
447         memcpy(dbf_opcode,
448                scpnt->cmnd,
449                min(scpnt->cmd_len, (unsigned char) ZFCP_ABORT_DBF_LENGTH));
450
451         ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
452                       scpnt, zfcp_get_busid_by_adapter(adapter));
453
454         spin_unlock_irq(scsi_host->host_lock);
455
456         /*
457          * Race condition between normal (late) completion and abort has
458          * to be avoided.
459          * The entirity of all accesses to scsi_req have to be atomic.
460          * scsi_req is usually part of the fsf_req and thus we block the
461          * release of fsf_req as long as we need to access scsi_req.
462          */
463         write_lock_irqsave(&adapter->abort_lock, flags);
464
465         /*
466          * Check whether command has just completed and can not be aborted.
467          * Even if the command has just been completed late, we can access
468          * scpnt since the SCSI stack does not release it at least until
469          * this routine returns. (scpnt is parameter passed to this routine
470          * and must not disappear during abort even on late completion.)
471          */
472         req_data = (union zfcp_req_data *) scpnt->host_scribble;
473         /* DEBUG */
474         ZFCP_LOG_DEBUG("req_data=%p\n", req_data);
475         if (!req_data) {
476                 ZFCP_LOG_DEBUG("late command completion overtook abort\n");
477                 /*
478                  * That's it.
479                  * Do not initiate abort but return SUCCESS.
480                  */
481                 write_unlock_irqrestore(&adapter->abort_lock, flags);
482                 retval = SUCCESS;
483                 strncpy(dbf_result, "##late1", ZFCP_ABORT_DBF_LENGTH);
484                 goto out;
485         }
486
487         /* Figure out which fsf_req needs to be aborted. */
488         old_fsf_req = req_data->send_fcp_command_task.fsf_req;
489
490         dbf_fsf_req = (unsigned long) old_fsf_req;
491         dbf_timeout =
492             (jiffies - req_data->send_fcp_command_task.start_jiffies) / HZ;
493
494         ZFCP_LOG_DEBUG("old_fsf_req=%p\n", old_fsf_req);
495         if (!old_fsf_req) {
496                 write_unlock_irqrestore(&adapter->abort_lock, flags);
497                 ZFCP_LOG_NORMAL("bug: no old fsf request found\n");
498                 ZFCP_LOG_NORMAL("req_data:\n");
499                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
500                               (char *) req_data, sizeof (union zfcp_req_data));
501                 ZFCP_LOG_NORMAL("scsi_cmnd:\n");
502                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
503                               (char *) scpnt, sizeof (struct scsi_cmnd));
504                 retval = FAILED;
505                 strncpy(dbf_result, "##bug:r", ZFCP_ABORT_DBF_LENGTH);
506                 goto out;
507         }
508         old_fsf_req->data.send_fcp_command_task.scsi_cmnd = NULL;
509         /* mark old request as being aborted */
510         old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
511         /*
512          * We have to collect all information (e.g. unit) needed by 
513          * zfcp_fsf_abort_fcp_command before calling that routine
514          * since that routine is not allowed to access
515          * fsf_req which it is going to abort.
516          * This is because of we need to release fsf_req_list_lock
517          * before calling zfcp_fsf_abort_fcp_command.
518          * Since this lock will not be held, fsf_req may complete
519          * late and may be released meanwhile.
520          */
521         ZFCP_LOG_DEBUG("unit 0x%016Lx (%p)\n", unit->fcp_lun, unit);
522
523         /*
524          * We block (call schedule)
525          * That's why we must release the lock and enable the
526          * interrupts before.
527          * On the other hand we do not need the lock anymore since
528          * all critical accesses to scsi_req are done.
529          */
530         write_unlock_irqrestore(&adapter->abort_lock, flags);
531         /* call FSF routine which does the abort */
532         new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
533                                                  adapter, unit, 0);
534         ZFCP_LOG_DEBUG("new_fsf_req=%p\n", new_fsf_req);
535         if (!new_fsf_req) {
536                 retval = FAILED;
537                 ZFCP_LOG_NORMAL("error: initiation of Abort FCP Cmnd "
538                                 "failed\n");
539                 strncpy(dbf_result, "##nores", ZFCP_ABORT_DBF_LENGTH);
540                 goto out;
541         }
542
543         /* wait for completion of abort */
544         ZFCP_LOG_DEBUG("waiting for cleanup...\n");
545 #if 1
546         /*
547          * FIXME:
548          * copying zfcp_fsf_req_wait_and_cleanup code is not really nice
549          */
550         __wait_event(new_fsf_req->completion_wq,
551                      new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
552         status = new_fsf_req->status;
553         dbf_fsf_status = new_fsf_req->qtcb->header.fsf_status;
554         /*
555          * Ralphs special debug load provides timestamps in the FSF
556          * status qualifier. This might be specified later if being
557          * useful for debugging aborts.
558          */
559         dbf_fsf_qual[0] =
560             *(u64 *) & new_fsf_req->qtcb->header.fsf_status_qual.word[0];
561         dbf_fsf_qual[1] =
562             *(u64 *) & new_fsf_req->qtcb->header.fsf_status_qual.word[2];
563         zfcp_fsf_req_cleanup(new_fsf_req);
564 #else
565         retval = zfcp_fsf_req_wait_and_cleanup(new_fsf_req,
566                                                ZFCP_UNINTERRUPTIBLE, &status);
567 #endif
568         ZFCP_LOG_DEBUG("Waiting for cleanup complete, status=0x%x\n", status);
569         /* status should be valid since signals were not permitted */
570         if (status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
571                 retval = SUCCESS;
572                 strncpy(dbf_result, "##succ", ZFCP_ABORT_DBF_LENGTH);
573         } else if (status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
574                 retval = SUCCESS;
575                 strncpy(dbf_result, "##late2", ZFCP_ABORT_DBF_LENGTH);
576         } else {
577                 retval = FAILED;
578                 strncpy(dbf_result, "##fail", ZFCP_ABORT_DBF_LENGTH);
579         }
580
581  out:
582         debug_event(adapter->abort_dbf, 1, &dbf_scsi_cmnd, sizeof (u64));
583         debug_event(adapter->abort_dbf, 1, &dbf_opcode, ZFCP_ABORT_DBF_LENGTH);
584         debug_event(adapter->abort_dbf, 1, &dbf_wwn, sizeof (wwn_t));
585         debug_event(adapter->abort_dbf, 1, &dbf_fcp_lun, sizeof (fcp_lun_t));
586         debug_event(adapter->abort_dbf, 1, &dbf_retries, sizeof (u64));
587         debug_event(adapter->abort_dbf, 1, &dbf_allowed, sizeof (u64));
588         debug_event(adapter->abort_dbf, 1, &dbf_timeout, sizeof (u64));
589         debug_event(adapter->abort_dbf, 1, &dbf_fsf_req, sizeof (u64));
590         debug_event(adapter->abort_dbf, 1, &dbf_fsf_status, sizeof (u64));
591         debug_event(adapter->abort_dbf, 1, &dbf_fsf_qual[0], sizeof (u64));
592         debug_event(adapter->abort_dbf, 1, &dbf_fsf_qual[1], sizeof (u64));
593         debug_text_event(adapter->abort_dbf, 1, dbf_result);
594
595         spin_lock_irq(scsi_host->host_lock);
596         return retval;
597 }
598
599 /*
600  * function:    zfcp_scsi_eh_device_reset_handler
601  *
602  * purpose:
603  *
604  * returns:
605  */
606 int
607 zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
608 {
609         int retval;
610         struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
611         struct Scsi_Host *scsi_host = scpnt->device->host;
612
613         spin_unlock_irq(scsi_host->host_lock);
614
615         if (!unit) {
616                 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
617                 retval = SUCCESS;
618                 goto out;
619         }
620         ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
621
622         /*
623          * If we do not know whether the unit supports 'logical unit reset'
624          * then try 'logical unit reset' and proceed with 'target reset'
625          * if 'logical unit reset' fails.
626          * If the unit is known not to support 'logical unit reset' then
627          * skip 'logical unit reset' and try 'target reset' immediately.
628          */
629         if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
630                               &unit->status)) {
631                 retval =
632                     zfcp_task_management_function(unit, LOGICAL_UNIT_RESET);
633                 if (retval) {
634                         ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
635                         if (retval == -ENOTSUPP)
636                                 atomic_set_mask
637                                     (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
638                                      &unit->status);
639                         /* fall through and try 'target reset' next */
640                 } else {
641                         ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
642                                        unit);
643                         /* avoid 'target reset' */
644                         retval = SUCCESS;
645                         goto out;
646                 }
647         }
648         retval = zfcp_task_management_function(unit, TARGET_RESET);
649         if (retval) {
650                 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
651                 retval = FAILED;
652         } else {
653                 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
654                 retval = SUCCESS;
655         }
656  out:
657         spin_lock_irq(scsi_host->host_lock);
658         return retval;
659 }
660
661 static int
662 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags)
663 {
664         struct zfcp_adapter *adapter = unit->port->adapter;
665         int retval;
666         int status;
667         struct zfcp_fsf_req *fsf_req;
668
669         /* issue task management function */
670         fsf_req = zfcp_fsf_send_fcp_command_task_management
671                 (adapter, unit, tm_flags, 0);
672         if (!fsf_req) {
673                 ZFCP_LOG_INFO("error: creation of task management request "
674                               "failed for unit 0x%016Lx on port 0x%016Lx on  "
675                               "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
676                               zfcp_get_busid_by_adapter(adapter));
677                 retval = -ENOMEM;
678                 goto out;
679         }
680
681         retval = zfcp_fsf_req_wait_and_cleanup(fsf_req,
682                                                ZFCP_UNINTERRUPTIBLE, &status);
683         /*
684          * check completion status of task management function
685          * (status should always be valid since no signals permitted)
686          */
687         if (status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED)
688                 retval = -EIO;
689         else if (status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP)
690                 retval = -ENOTSUPP;
691         else
692                 retval = 0;
693  out:
694         return retval;
695 }
696
697 /*
698  * function:    zfcp_scsi_eh_bus_reset_handler
699  *
700  * purpose:
701  *
702  * returns:
703  */
704 int
705 zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
706 {
707         int retval = 0;
708         struct zfcp_unit *unit;
709         struct Scsi_Host *scsi_host = scpnt->device->host;
710
711         spin_unlock_irq(scsi_host->host_lock);
712
713         unit = (struct zfcp_unit *) scpnt->device->hostdata;
714         ZFCP_LOG_NORMAL("bus reset because of problems with "
715                         "unit 0x%016Lx\n", unit->fcp_lun);
716         zfcp_erp_adapter_reopen(unit->port->adapter, 0);
717         zfcp_erp_wait(unit->port->adapter);
718         retval = SUCCESS;
719
720         spin_lock_irq(scsi_host->host_lock);
721         return retval;
722 }
723
724 /*
725  * function:    zfcp_scsi_eh_host_reset_handler
726  *
727  * purpose:
728  *
729  * returns:
730  */
731 int
732 zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
733 {
734         int retval = 0;
735         struct zfcp_unit *unit;
736         struct Scsi_Host *scsi_host = scpnt->device->host;
737
738         spin_unlock_irq(scsi_host->host_lock);
739
740         unit = (struct zfcp_unit *) scpnt->device->hostdata;
741         ZFCP_LOG_NORMAL("host reset because of problems with "
742                         "unit 0x%016Lx\n", unit->fcp_lun);
743         zfcp_erp_adapter_reopen(unit->port->adapter, 0);
744         zfcp_erp_wait(unit->port->adapter);
745         retval = SUCCESS;
746
747         spin_lock_irq(scsi_host->host_lock);
748         return retval;
749 }
750
751 /*
752  * function:    
753  *
754  * purpose:     
755  *
756  * returns:
757  */
758 int
759 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
760 {
761         int retval = 0;
762         static unsigned int unique_id = 0;
763
764         /* register adapter as SCSI host with mid layer of SCSI stack */
765         adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
766                                              sizeof (struct zfcp_adapter *));
767         if (!adapter->scsi_host) {
768                 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
769                                 "for adapter %s ",
770                                 zfcp_get_busid_by_adapter(adapter));
771                 retval = -EIO;
772                 goto out;
773         }
774         ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
775
776         /* tell the SCSI stack some characteristics of this adapter */
777         adapter->scsi_host->max_id = 1;
778         adapter->scsi_host->max_lun = 1;
779         adapter->scsi_host->max_channel = 0;
780         adapter->scsi_host->unique_id = unique_id++;    /* FIXME */
781         adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
782         adapter->scsi_host->transportt = zfcp_transport_template;
783         /*
784          * Reverse mapping of the host number to avoid race condition
785          */
786         adapter->scsi_host_no = adapter->scsi_host->host_no;
787
788         /*
789          * save a pointer to our own adapter data structure within
790          * hostdata field of SCSI host data structure
791          */
792         adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
793
794         if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
795                 scsi_host_put(adapter->scsi_host);
796                 retval = -EIO;
797                 goto out;
798         }
799         atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
800  out:
801         return retval;
802 }
803
804 /*
805  * function:    
806  *
807  * purpose:     
808  *
809  * returns:
810  */
811 void
812 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
813 {
814         struct Scsi_Host *shost;
815
816         shost = adapter->scsi_host;
817         if (!shost)
818                 return;
819         scsi_remove_host(shost);
820         scsi_host_put(shost);
821         adapter->scsi_host = NULL;
822         adapter->scsi_host_no = 0;
823         atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
824
825         return;
826 }
827
828
829 void
830 zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
831 {
832         adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
833         adapter->scsi_er_timer.data = (unsigned long) adapter;
834         adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
835         add_timer(&adapter->scsi_er_timer);
836 }
837
838 /*
839  * Support functions for FC transport class
840  */
841 static void
842 zfcp_get_port_id(struct scsi_device *sdev)
843 {
844         struct zfcp_unit *unit;
845
846         unit = (struct zfcp_unit *) sdev->hostdata;
847         fc_port_id(sdev) = unit->port->d_id;
848 }
849
850 static void
851 zfcp_get_port_name(struct scsi_device *sdev)
852 {
853         struct zfcp_unit *unit;
854
855         unit = (struct zfcp_unit *) sdev->hostdata;
856         fc_port_name(sdev) = unit->port->wwpn;
857 }
858
859 static void
860 zfcp_get_node_name(struct scsi_device *sdev)
861 {
862         struct zfcp_unit *unit;
863
864         unit = (struct zfcp_unit *) sdev->hostdata;
865         fc_node_name(sdev) = unit->port->wwnn;
866 }
867
868 struct fc_function_template zfcp_transport_functions = {
869         .get_port_id = zfcp_get_port_id,
870         .get_port_name = zfcp_get_port_name,
871         .get_node_name = zfcp_get_node_name,
872         .show_port_id = 1,
873         .show_port_name = 1,
874         .show_node_name = 1,
875 };
876
877 /**
878  * ZFCP_DEFINE_SCSI_ATTR
879  * @_name:   name of show attribute
880  * @_format: format string
881  * @_value:  value to print
882  *
883  * Generates attribute for a unit.
884  */
885 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)                    \
886 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev,        \
887                                               char *buf)                 \
888 {                                                                        \
889         struct scsi_device *sdev;                                        \
890         struct zfcp_unit *unit;                                          \
891                                                                          \
892         sdev = to_scsi_device(dev);                                      \
893         unit = sdev->hostdata;                                           \
894         return sprintf(buf, _format, _value);                            \
895 }                                                                        \
896                                                                          \
897 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
898
899 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
900 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
901 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
902
903 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
904         &dev_attr_fcp_lun,
905         &dev_attr_wwpn,
906         &dev_attr_hba_id,
907         NULL
908 };
909
910 #undef ZFCP_LOG_AREA