This commit was manufactured by cvs2svn to create tag
[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 static struct zfcp_port * zfcp_port_lookup(struct zfcp_adapter *, int,
52                                          scsi_id_t);
53
54 static struct device_attribute *zfcp_sysfs_sdev_attrs[];
55
56 struct scsi_transport_template *zfcp_transport_template;
57
58 struct 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         /* rest initialised with zeros */
86 };
87
88 /* Find start of Response Information in FCP response unit*/
89 char *
90 zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
91 {
92         char *fcp_rsp_info_ptr;
93
94         fcp_rsp_info_ptr =
95             (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
96
97         return fcp_rsp_info_ptr;
98 }
99
100 /* Find start of Sense Information in FCP response unit*/
101 char *
102 zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
103 {
104         char *fcp_sns_info_ptr;
105
106         fcp_sns_info_ptr =
107             (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
108         if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
109                 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
110                     fcp_rsp_iu->fcp_rsp_len;
111
112         return fcp_sns_info_ptr;
113 }
114
115 fcp_dl_t *
116 zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
117 {
118         int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
119         fcp_dl_t *fcp_dl_addr;
120
121         fcp_dl_addr = (fcp_dl_t *)
122                 ((unsigned char *) fcp_cmd +
123                  sizeof (struct fcp_cmnd_iu) + additional_length);
124         /*
125          * fcp_dl_addr = start address of fcp_cmnd structure + 
126          * size of fixed part + size of dynamically sized add_dcp_cdb field
127          * SEE FCP-2 documentation
128          */
129         return fcp_dl_addr;
130 }
131
132 fcp_dl_t
133 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
134 {
135         return *zfcp_get_fcp_dl_ptr(fcp_cmd);
136 }
137
138 void
139 zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
140 {
141         *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
142 }
143
144 /*
145  * note: it's a bit-or operation not an assignment
146  * regarding the specified byte
147  */
148 static inline void
149 set_byte(u32 * result, char status, char pos)
150 {
151         *result |= status << (pos * 8);
152 }
153
154 void
155 set_host_byte(u32 * result, char status)
156 {
157         set_byte(result, status, 2);
158 }
159
160 void
161 set_driver_byte(u32 * result, char status)
162 {
163         set_byte(result, status, 3);
164 }
165
166 /*
167  * function:    zfcp_scsi_slave_alloc
168  *
169  * purpose:
170  *
171  * returns:
172  */
173
174 static int
175 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
176 {
177         struct zfcp_adapter *adapter;
178         struct zfcp_unit *unit;
179         unsigned long flags;
180         int retval = -ENODEV;
181
182         adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
183         if (!adapter)
184                 goto out;
185
186         read_lock_irqsave(&zfcp_data.config_lock, flags);
187         unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
188         if (unit) {
189                 sdp->hostdata = unit;
190                 unit->device = sdp;
191                 zfcp_unit_get(unit);
192                 retval = 0;
193         }
194         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
195  out:
196         return retval;
197 }
198
199 /*
200  * function:    zfcp_scsi_slave_destroy
201  *
202  * purpose:
203  *
204  * returns:
205  */
206
207 static void
208 zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
209 {
210         struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
211
212         if (unit) {
213                 sdpnt->hostdata = NULL;
214                 unit->device = NULL;
215                 zfcp_unit_put(unit);
216         } else {
217                 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
218                                 "address %p\n", sdpnt);
219         }
220 }
221
222 /* 
223  * called from scsi midlayer to allow finetuning of a device.
224  */
225 static int
226 zfcp_scsi_slave_configure(struct scsi_device *sdp)
227 {
228         if (sdp->tagged_supported)
229                 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
230         else
231                 scsi_adjust_queue_depth(sdp, 0, 1);
232         return 0;
233 }
234
235 /**
236  * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
237  * @scpnt: pointer to struct scsi_cmnd where result is set
238  * @result: result to be set in scpnt (e.g. DID_ERROR)
239  */
240 static void
241 zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
242 {
243         set_host_byte(&scpnt->result, result);
244         zfcp_cmd_dbf_event_scsi("failing", scpnt);
245         /* return directly */
246         scpnt->scsi_done(scpnt);
247 }
248
249 /**
250  * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
251  *      zfcp_scsi_command_sync
252  * @adapter: adapter where scsi command is issued
253  * @unit: unit to which scsi command is sent
254  * @scpnt: scsi command to be sent
255  * @timer: timer to be started if request is successfully initiated
256  *
257  * Note: In scsi_done function must be set in scpnt.
258  */
259 int
260 zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
261                         struct scsi_cmnd *scpnt, struct timer_list *timer)
262 {
263         int tmp;
264         int retval;
265
266         retval = 0;
267
268         BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
269         BUG_ON(scpnt->scsi_done == NULL);
270
271         if (unlikely(NULL == unit)) {
272                 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
273                 goto out;
274         }
275
276         if (unlikely(
277               atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
278              !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
279                 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
280                                "0x%016Lx on adapter %s\n",
281                                unit->fcp_lun, unit->port->wwpn,
282                                zfcp_get_busid_by_adapter(adapter));
283                 zfcp_scsi_command_fail(scpnt, DID_ERROR);
284                 goto out;
285         }
286
287         if (unlikely(
288              !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
289                 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
290                                "on port 0x%016Lx in recovery\n",
291                                zfcp_get_busid_by_unit(unit),
292                                unit->fcp_lun, unit->port->wwpn);
293                 retval = SCSI_MLQUEUE_DEVICE_BUSY;
294                 goto out;
295         }
296
297         tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
298                                              ZFCP_REQ_AUTO_CLEANUP);
299
300         if (unlikely(tmp < 0)) {
301                 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
302                 retval = SCSI_MLQUEUE_HOST_BUSY;
303         }
304
305 out:
306         return retval;
307 }
308
309 void
310 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
311 {
312         struct completion *wait = (struct completion *) scpnt->SCp.ptr;
313         complete(wait);
314 }
315
316
317 /**
318  * zfcp_scsi_command_sync - send a SCSI command and wait for completion
319  * @unit: unit where command is sent to
320  * @scpnt: scsi command to be sent
321  * @timer: timer to be started if request is successfully initiated
322  * Return: 0
323  *
324  * Errors are indicated in scpnt->result
325  */
326 int
327 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
328                        struct timer_list *timer)
329 {
330         int ret;
331         DECLARE_COMPLETION(wait);
332
333         scpnt->SCp.ptr = (void *) &wait;  /* silent re-use */
334         scpnt->scsi_done = zfcp_scsi_command_sync_handler;
335         ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
336         if ((ret == 0) && (scpnt->result == 0))
337         wait_for_completion(&wait);
338
339         scpnt->SCp.ptr = NULL;
340
341         return 0;
342 }
343
344 /*
345  * function:    zfcp_scsi_queuecommand
346  *
347  * purpose:     enqueues a SCSI command to the specified target device
348  *
349  * returns:     0 - success, SCSI command enqueued
350  *              !0 - failure
351  */
352 int
353 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
354                        void (*done) (struct scsi_cmnd *))
355 {
356         struct zfcp_unit *unit;
357         struct zfcp_adapter *adapter;
358
359         /* reset the status for this request */
360         scpnt->result = 0;
361         scpnt->host_scribble = NULL;
362         scpnt->scsi_done = done;
363
364         /*
365          * figure out adapter and target device
366          * (stored there by zfcp_scsi_slave_alloc)
367          */
368         adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
369         unit = (struct zfcp_unit *) scpnt->device->hostdata;
370
371         return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
372 }
373
374 /*
375  * function:    zfcp_unit_lookup
376  *
377  * purpose:
378  *
379  * returns:
380  *
381  * context:     
382  */
383 static struct zfcp_unit *
384 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
385                  scsi_lun_t lun)
386 {
387         struct zfcp_port *port;
388         struct zfcp_unit *unit, *retval = NULL;
389
390         list_for_each_entry(port, &adapter->port_list_head, list) {
391                 if (id != port->scsi_id)
392                         continue;
393                 list_for_each_entry(unit, &port->unit_list_head, list) {
394                         if (lun == unit->scsi_lun) {
395                                 retval = unit;
396                                 goto out;
397                         }
398                 }
399         }
400  out:
401         return retval;
402 }
403 /*
404  * function:    zfcp_unit_tgt_lookup
405  *
406  * purpose:
407  *
408  * returns:
409  *
410  * context:     
411  */
412 static struct zfcp_port *
413 zfcp_port_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id)
414 {
415         struct zfcp_port *port;
416
417         list_for_each_entry(port, &adapter->port_list_head, list) {
418                 if (id == port->scsi_id)
419                         return port;
420         }
421         return (struct zfcp_port *)NULL;
422 }
423
424 /*
425  * function:    zfcp_scsi_eh_abort_handler
426  *
427  * purpose:     tries to abort the specified (timed out) SCSI command
428  *
429  * note:        We do not need to care for a SCSI command which completes
430  *              normally but late during this abort routine runs.
431  *              We are allowed to return late commands to the SCSI stack.
432  *              It tracks the state of commands and will handle late commands.
433  *              (Usually, the normal completion of late commands is ignored with
434  *              respect to the running abort operation. Grep for 'done_late'
435  *              in the SCSI stacks sources.)
436  *
437  * returns:     SUCCESS - command has been aborted and cleaned up in internal
438  *                        bookkeeping,
439  *                        SCSI stack won't be called for aborted command
440  *              FAILED  - otherwise
441  */
442 int
443 zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
444 {
445         int retval = SUCCESS;
446         struct zfcp_fsf_req *new_fsf_req, *old_fsf_req;
447         struct zfcp_adapter *adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
448         struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
449         struct zfcp_port *port = unit->port;
450         struct Scsi_Host *scsi_host = scpnt->device->host;
451         union zfcp_req_data *req_data = NULL;
452         unsigned long flags;
453         u32 status = 0;
454
455         /* the components of a abort_dbf record (fixed size record) */
456         u64 dbf_scsi_cmnd = (unsigned long) scpnt;
457         char dbf_opcode[ZFCP_ABORT_DBF_LENGTH];
458         wwn_t dbf_wwn = port->wwpn;
459         fcp_lun_t dbf_fcp_lun = unit->fcp_lun;
460         u64 dbf_retries = scpnt->retries;
461         u64 dbf_allowed = scpnt->allowed;
462         u64 dbf_timeout = 0;
463         u64 dbf_fsf_req = 0;
464         u64 dbf_fsf_status = 0;
465         u64 dbf_fsf_qual[2] = { 0, 0 };
466         char dbf_result[ZFCP_ABORT_DBF_LENGTH] = "##undef";
467
468         memset(dbf_opcode, 0, ZFCP_ABORT_DBF_LENGTH);
469         memcpy(dbf_opcode,
470                scpnt->cmnd,
471                min(scpnt->cmd_len, (unsigned char) ZFCP_ABORT_DBF_LENGTH));
472
473         ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
474                       scpnt, zfcp_get_busid_by_adapter(adapter));
475
476         spin_unlock_irq(scsi_host->host_lock);
477
478         /*
479          * Race condition between normal (late) completion and abort has
480          * to be avoided.
481          * The entirity of all accesses to scsi_req have to be atomic.
482          * scsi_req is usually part of the fsf_req and thus we block the
483          * release of fsf_req as long as we need to access scsi_req.
484          */
485         write_lock_irqsave(&adapter->abort_lock, flags);
486
487         /*
488          * Check whether command has just completed and can not be aborted.
489          * Even if the command has just been completed late, we can access
490          * scpnt since the SCSI stack does not release it at least until
491          * this routine returns. (scpnt is parameter passed to this routine
492          * and must not disappear during abort even on late completion.)
493          */
494         req_data = (union zfcp_req_data *) scpnt->host_scribble;
495         /* DEBUG */
496         ZFCP_LOG_DEBUG("req_data=%p\n", req_data);
497         if (!req_data) {
498                 ZFCP_LOG_DEBUG("late command completion overtook abort\n");
499                 /*
500                  * That's it.
501                  * Do not initiate abort but return SUCCESS.
502                  */
503                 write_unlock_irqrestore(&adapter->abort_lock, flags);
504                 retval = SUCCESS;
505                 strncpy(dbf_result, "##late1", ZFCP_ABORT_DBF_LENGTH);
506                 goto out;
507         }
508
509         /* Figure out which fsf_req needs to be aborted. */
510         old_fsf_req = req_data->send_fcp_command_task.fsf_req;
511
512         dbf_fsf_req = (unsigned long) old_fsf_req;
513         dbf_timeout =
514             (jiffies - req_data->send_fcp_command_task.start_jiffies) / HZ;
515
516         ZFCP_LOG_DEBUG("old_fsf_req=%p\n", old_fsf_req);
517         if (!old_fsf_req) {
518                 write_unlock_irqrestore(&adapter->abort_lock, flags);
519                 ZFCP_LOG_NORMAL("bug: no old fsf request found\n");
520                 ZFCP_LOG_NORMAL("req_data:\n");
521                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
522                               (char *) req_data, sizeof (union zfcp_req_data));
523                 ZFCP_LOG_NORMAL("scsi_cmnd:\n");
524                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
525                               (char *) scpnt, sizeof (struct scsi_cmnd));
526                 retval = FAILED;
527                 strncpy(dbf_result, "##bug:r", ZFCP_ABORT_DBF_LENGTH);
528                 goto out;
529         }
530         old_fsf_req->data.send_fcp_command_task.scsi_cmnd = NULL;
531         /* mark old request as being aborted */
532         old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
533         /*
534          * We have to collect all information (e.g. unit) needed by 
535          * zfcp_fsf_abort_fcp_command before calling that routine
536          * since that routine is not allowed to access
537          * fsf_req which it is going to abort.
538          * This is because of we need to release fsf_req_list_lock
539          * before calling zfcp_fsf_abort_fcp_command.
540          * Since this lock will not be held, fsf_req may complete
541          * late and may be released meanwhile.
542          */
543         ZFCP_LOG_DEBUG("unit 0x%016Lx (%p)\n", unit->fcp_lun, unit);
544
545         /*
546          * We block (call schedule)
547          * That's why we must release the lock and enable the
548          * interrupts before.
549          * On the other hand we do not need the lock anymore since
550          * all critical accesses to scsi_req are done.
551          */
552         write_unlock_irqrestore(&adapter->abort_lock, flags);
553         /* call FSF routine which does the abort */
554         new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
555                                                  adapter, unit, 0);
556         ZFCP_LOG_DEBUG("new_fsf_req=%p\n", new_fsf_req);
557         if (!new_fsf_req) {
558                 retval = FAILED;
559                 ZFCP_LOG_NORMAL("error: initiation of Abort FCP Cmnd "
560                                 "failed\n");
561                 strncpy(dbf_result, "##nores", ZFCP_ABORT_DBF_LENGTH);
562                 goto out;
563         }
564
565         /* wait for completion of abort */
566         ZFCP_LOG_DEBUG("waiting for cleanup...\n");
567 #if 1
568         /*
569          * FIXME:
570          * copying zfcp_fsf_req_wait_and_cleanup code is not really nice
571          */
572         __wait_event(new_fsf_req->completion_wq,
573                      new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
574         status = new_fsf_req->status;
575         dbf_fsf_status = new_fsf_req->qtcb->header.fsf_status;
576         /*
577          * Ralphs special debug load provides timestamps in the FSF
578          * status qualifier. This might be specified later if being
579          * useful for debugging aborts.
580          */
581         dbf_fsf_qual[0] =
582             *(u64 *) & new_fsf_req->qtcb->header.fsf_status_qual.word[0];
583         dbf_fsf_qual[1] =
584             *(u64 *) & new_fsf_req->qtcb->header.fsf_status_qual.word[2];
585         zfcp_fsf_req_cleanup(new_fsf_req);
586 #else
587         retval = zfcp_fsf_req_wait_and_cleanup(new_fsf_req,
588                                                ZFCP_UNINTERRUPTIBLE, &status);
589 #endif
590         ZFCP_LOG_DEBUG("Waiting for cleanup complete, status=0x%x\n", status);
591         /* status should be valid since signals were not permitted */
592         if (status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
593                 retval = SUCCESS;
594                 strncpy(dbf_result, "##succ", ZFCP_ABORT_DBF_LENGTH);
595         } else if (status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
596                 retval = SUCCESS;
597                 strncpy(dbf_result, "##late2", ZFCP_ABORT_DBF_LENGTH);
598         } else {
599                 retval = FAILED;
600                 strncpy(dbf_result, "##fail", ZFCP_ABORT_DBF_LENGTH);
601         }
602
603  out:
604         debug_event(adapter->abort_dbf, 1, &dbf_scsi_cmnd, sizeof (u64));
605         debug_event(adapter->abort_dbf, 1, &dbf_opcode, ZFCP_ABORT_DBF_LENGTH);
606         debug_event(adapter->abort_dbf, 1, &dbf_wwn, sizeof (wwn_t));
607         debug_event(adapter->abort_dbf, 1, &dbf_fcp_lun, sizeof (fcp_lun_t));
608         debug_event(adapter->abort_dbf, 1, &dbf_retries, sizeof (u64));
609         debug_event(adapter->abort_dbf, 1, &dbf_allowed, sizeof (u64));
610         debug_event(adapter->abort_dbf, 1, &dbf_timeout, sizeof (u64));
611         debug_event(adapter->abort_dbf, 1, &dbf_fsf_req, sizeof (u64));
612         debug_event(adapter->abort_dbf, 1, &dbf_fsf_status, sizeof (u64));
613         debug_event(adapter->abort_dbf, 1, &dbf_fsf_qual[0], sizeof (u64));
614         debug_event(adapter->abort_dbf, 1, &dbf_fsf_qual[1], sizeof (u64));
615         debug_text_event(adapter->abort_dbf, 1, dbf_result);
616
617         spin_lock_irq(scsi_host->host_lock);
618         return retval;
619 }
620
621 /*
622  * function:    zfcp_scsi_eh_device_reset_handler
623  *
624  * purpose:
625  *
626  * returns:
627  */
628 int
629 zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
630 {
631         int retval;
632         struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
633         struct Scsi_Host *scsi_host = scpnt->device->host;
634
635         spin_unlock_irq(scsi_host->host_lock);
636
637         if (!unit) {
638                 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
639                 retval = SUCCESS;
640                 goto out;
641         }
642         ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
643
644         /*
645          * If we do not know whether the unit supports 'logical unit reset'
646          * then try 'logical unit reset' and proceed with 'target reset'
647          * if 'logical unit reset' fails.
648          * If the unit is known not to support 'logical unit reset' then
649          * skip 'logical unit reset' and try 'target reset' immediately.
650          */
651         if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
652                               &unit->status)) {
653                 retval =
654                     zfcp_task_management_function(unit, LOGICAL_UNIT_RESET);
655                 if (retval) {
656                         ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
657                         if (retval == -ENOTSUPP)
658                                 atomic_set_mask
659                                     (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
660                                      &unit->status);
661                         /* fall through and try 'target reset' next */
662                 } else {
663                         ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
664                                        unit);
665                         /* avoid 'target reset' */
666                         retval = SUCCESS;
667                         goto out;
668                 }
669         }
670         retval = zfcp_task_management_function(unit, TARGET_RESET);
671         if (retval) {
672                 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
673                 retval = FAILED;
674         } else {
675                 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
676                 retval = SUCCESS;
677         }
678  out:
679         spin_lock_irq(scsi_host->host_lock);
680         return retval;
681 }
682
683 static int
684 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags)
685 {
686         struct zfcp_adapter *adapter = unit->port->adapter;
687         int retval;
688         int status;
689         struct zfcp_fsf_req *fsf_req;
690
691         /* issue task management function */
692         fsf_req = zfcp_fsf_send_fcp_command_task_management
693                 (adapter, unit, tm_flags, 0);
694         if (!fsf_req) {
695                 ZFCP_LOG_INFO("error: creation of task management request "
696                               "failed for unit 0x%016Lx on port 0x%016Lx on  "
697                               "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
698                               zfcp_get_busid_by_adapter(adapter));
699                 retval = -ENOMEM;
700                 goto out;
701         }
702
703         retval = zfcp_fsf_req_wait_and_cleanup(fsf_req,
704                                                ZFCP_UNINTERRUPTIBLE, &status);
705         /*
706          * check completion status of task management function
707          * (status should always be valid since no signals permitted)
708          */
709         if (status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED)
710                 retval = -EIO;
711         else if (status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP)
712                 retval = -ENOTSUPP;
713         else
714                 retval = 0;
715  out:
716         return retval;
717 }
718
719 /*
720  * function:    zfcp_scsi_eh_bus_reset_handler
721  *
722  * purpose:
723  *
724  * returns:
725  */
726 int
727 zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
728 {
729         int retval = 0;
730         struct zfcp_unit *unit;
731         struct Scsi_Host *scsi_host = scpnt->device->host;
732
733         spin_unlock_irq(scsi_host->host_lock);
734
735         unit = (struct zfcp_unit *) scpnt->device->hostdata;
736         ZFCP_LOG_NORMAL("bus reset because of problems with "
737                         "unit 0x%016Lx\n", unit->fcp_lun);
738         zfcp_erp_adapter_reopen(unit->port->adapter, 0);
739         zfcp_erp_wait(unit->port->adapter);
740         retval = SUCCESS;
741
742         spin_lock_irq(scsi_host->host_lock);
743         return retval;
744 }
745
746 /*
747  * function:    zfcp_scsi_eh_host_reset_handler
748  *
749  * purpose:
750  *
751  * returns:
752  */
753 int
754 zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
755 {
756         int retval = 0;
757         struct zfcp_unit *unit;
758         struct Scsi_Host *scsi_host = scpnt->device->host;
759
760         spin_unlock_irq(scsi_host->host_lock);
761
762         unit = (struct zfcp_unit *) scpnt->device->hostdata;
763         ZFCP_LOG_NORMAL("host reset because of problems with "
764                         "unit 0x%016Lx\n", unit->fcp_lun);
765         zfcp_erp_adapter_reopen(unit->port->adapter, 0);
766         zfcp_erp_wait(unit->port->adapter);
767         retval = SUCCESS;
768
769         spin_lock_irq(scsi_host->host_lock);
770         return retval;
771 }
772
773 /*
774  * function:    
775  *
776  * purpose:     
777  *
778  * returns:
779  */
780 int
781 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
782 {
783         int retval = 0;
784         static unsigned int unique_id = 0;
785
786         /* register adapter as SCSI host with mid layer of SCSI stack */
787         adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
788                                              sizeof (struct zfcp_adapter *));
789         if (!adapter->scsi_host) {
790                 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
791                                 "for adapter %s ",
792                                 zfcp_get_busid_by_adapter(adapter));
793                 retval = -EIO;
794                 goto out;
795         }
796         ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
797
798         /* tell the SCSI stack some characteristics of this adapter */
799         adapter->scsi_host->max_id = 1;
800         adapter->scsi_host->max_lun = 1;
801         adapter->scsi_host->max_channel = 0;
802         adapter->scsi_host->unique_id = unique_id++;    /* FIXME */
803         adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
804         adapter->scsi_host->transportt = zfcp_transport_template;
805         /*
806          * Reverse mapping of the host number to avoid race condition
807          */
808         adapter->scsi_host_no = adapter->scsi_host->host_no;
809
810         /*
811          * save a pointer to our own adapter data structure within
812          * hostdata field of SCSI host data structure
813          */
814         adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
815
816         if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
817                 scsi_host_put(adapter->scsi_host);
818                 retval = -EIO;
819                 goto out;
820         }
821         atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
822  out:
823         return retval;
824 }
825
826 /*
827  * function:    
828  *
829  * purpose:     
830  *
831  * returns:
832  */
833 void
834 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
835 {
836         struct Scsi_Host *shost;
837
838         shost = adapter->scsi_host;
839         if (!shost)
840                 return;
841         scsi_remove_host(shost);
842         scsi_host_put(shost);
843         adapter->scsi_host = NULL;
844         adapter->scsi_host_no = 0;
845         atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
846
847         return;
848 }
849
850
851 void
852 zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
853 {
854         adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
855         adapter->scsi_er_timer.data = (unsigned long) adapter;
856         adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
857         add_timer(&adapter->scsi_er_timer);
858 }
859
860 /*
861  * Support functions for FC transport class
862  */
863 static void
864 zfcp_get_port_id(struct scsi_target *starget)
865 {
866         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
867         struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
868         struct zfcp_port *port;
869         unsigned long flags;
870
871         read_lock_irqsave(&zfcp_data.config_lock, flags);
872         port = zfcp_port_lookup(adapter, starget->channel, starget->id);
873         if (port)
874                 fc_starget_port_id(starget) = port->d_id;
875         else
876                 fc_starget_port_id(starget) = -1;
877         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
878 }
879
880 static void
881 zfcp_get_port_name(struct scsi_target *starget)
882 {
883         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
884         struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
885         struct zfcp_port *port;
886         unsigned long flags;
887
888         read_lock_irqsave(&zfcp_data.config_lock, flags);
889         port = zfcp_port_lookup(adapter, starget->channel, starget->id);
890         if (port)
891                 fc_starget_port_name(starget) = port->wwpn;
892         else
893                 fc_starget_port_name(starget) = -1;
894         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
895 }
896
897 static void
898 zfcp_get_node_name(struct scsi_target *starget)
899 {
900         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
901         struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
902         struct zfcp_port *port;
903         unsigned long flags;
904
905         read_lock_irqsave(&zfcp_data.config_lock, flags);
906         port = zfcp_port_lookup(adapter, starget->channel, starget->id);
907         if (port)
908                 fc_starget_node_name(starget) = port->wwnn;
909         else
910                 fc_starget_node_name(starget) = -1;
911         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
912 }
913
914 struct fc_function_template zfcp_transport_functions = {
915         .get_starget_port_id = zfcp_get_port_id,
916         .get_starget_port_name = zfcp_get_port_name,
917         .get_starget_node_name = zfcp_get_node_name,
918         .show_starget_port_id = 1,
919         .show_starget_port_name = 1,
920         .show_starget_node_name = 1,
921 };
922
923 /**
924  * ZFCP_DEFINE_SCSI_ATTR
925  * @_name:   name of show attribute
926  * @_format: format string
927  * @_value:  value to print
928  *
929  * Generates attribute for a unit.
930  */
931 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)                    \
932 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev,        \
933                                               char *buf)                 \
934 {                                                                        \
935         struct scsi_device *sdev;                                        \
936         struct zfcp_unit *unit;                                          \
937                                                                          \
938         sdev = to_scsi_device(dev);                                      \
939         unit = sdev->hostdata;                                           \
940         return sprintf(buf, _format, _value);                            \
941 }                                                                        \
942                                                                          \
943 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
944
945 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
946 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
947 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
948
949 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
950         &dev_attr_fcp_lun,
951         &dev_attr_wwpn,
952         &dev_attr_hba_id,
953         NULL
954 };
955
956 #undef ZFCP_LOG_AREA