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