[SCSI] zfcp: evaluate plogi payload to set maxframe_size, supported_classes of rports
[safe/jmp/linux-2.6] / drivers / s390 / scsi / zfcp_scsi.c
1 /* 
2  * This file is part of the zfcp device driver for
3  * FCP adapters for IBM System z9 and zSeries.
4  *
5  * (C) Copyright IBM Corp. 2002, 2006
6  * 
7  * This program is free software; you can redistribute it and/or modify 
8  * it under the terms of the GNU General Public License as published by 
9  * the Free Software Foundation; either version 2, or (at your option) 
10  * any later version. 
11  * 
12  * This program is distributed in the hope that it will be useful, 
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
15  * GNU General Public License for more details. 
16  * 
17  * You should have received a copy of the GNU General Public License 
18  * along with this program; if not, write to the Free Software 
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
20  */
21
22 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_SCSI
23
24 #include "zfcp_ext.h"
25
26 static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
27 static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
28 static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
29 static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
30                                   void (*done) (struct scsi_cmnd *));
31 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
32 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
33 static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
34 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
35 static int zfcp_task_management_function(struct zfcp_unit *, u8,
36                                          struct scsi_cmnd *);
37
38 static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
39                                           unsigned int, unsigned int);
40
41 static struct device_attribute *zfcp_sysfs_sdev_attrs[];
42
43 struct scsi_transport_template *zfcp_transport_template;
44
45 struct zfcp_data zfcp_data = {
46         .scsi_host_template = {
47               name:                    ZFCP_NAME,
48               proc_name:               "zfcp",
49               proc_info:               NULL,
50               detect:                  NULL,
51               slave_alloc:             zfcp_scsi_slave_alloc,
52               slave_configure:         zfcp_scsi_slave_configure,
53               slave_destroy:           zfcp_scsi_slave_destroy,
54               queuecommand:            zfcp_scsi_queuecommand,
55               eh_abort_handler:        zfcp_scsi_eh_abort_handler,
56               eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
57               eh_bus_reset_handler:    zfcp_scsi_eh_bus_reset_handler,
58               eh_host_reset_handler:   zfcp_scsi_eh_host_reset_handler,
59                                        /* FIXME(openfcp): Tune */
60               can_queue:               4096,
61               this_id:                 -1,
62               /*
63                * FIXME:
64                * one less? can zfcp_create_sbale cope with it?
65                */
66               sg_tablesize:            ZFCP_MAX_SBALES_PER_REQ,
67               cmd_per_lun:             1,
68               unchecked_isa_dma:       0,
69               use_clustering:          1,
70               sdev_attrs:              zfcp_sysfs_sdev_attrs,
71         },
72         .driver_version = ZFCP_VERSION,
73         /* rest initialised with zeros */
74 };
75
76 /* Find start of Response Information in FCP response unit*/
77 char *
78 zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
79 {
80         char *fcp_rsp_info_ptr;
81
82         fcp_rsp_info_ptr =
83             (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
84
85         return fcp_rsp_info_ptr;
86 }
87
88 /* Find start of Sense Information in FCP response unit*/
89 char *
90 zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
91 {
92         char *fcp_sns_info_ptr;
93
94         fcp_sns_info_ptr =
95             (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
96         if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
97                 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
98                     fcp_rsp_iu->fcp_rsp_len;
99
100         return fcp_sns_info_ptr;
101 }
102
103 fcp_dl_t *
104 zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
105 {
106         int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
107         fcp_dl_t *fcp_dl_addr;
108
109         fcp_dl_addr = (fcp_dl_t *)
110                 ((unsigned char *) fcp_cmd +
111                  sizeof (struct fcp_cmnd_iu) + additional_length);
112         /*
113          * fcp_dl_addr = start address of fcp_cmnd structure + 
114          * size of fixed part + size of dynamically sized add_dcp_cdb field
115          * SEE FCP-2 documentation
116          */
117         return fcp_dl_addr;
118 }
119
120 fcp_dl_t
121 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
122 {
123         return *zfcp_get_fcp_dl_ptr(fcp_cmd);
124 }
125
126 void
127 zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
128 {
129         *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
130 }
131
132 /*
133  * note: it's a bit-or operation not an assignment
134  * regarding the specified byte
135  */
136 static inline void
137 set_byte(u32 * result, char status, char pos)
138 {
139         *result |= status << (pos * 8);
140 }
141
142 void
143 set_host_byte(u32 * result, char status)
144 {
145         set_byte(result, status, 2);
146 }
147
148 void
149 set_driver_byte(u32 * result, char status)
150 {
151         set_byte(result, status, 3);
152 }
153
154 static int
155 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
156 {
157         struct zfcp_adapter *adapter;
158         struct zfcp_unit *unit;
159         unsigned long flags;
160         int retval = -ENXIO;
161
162         adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
163         if (!adapter)
164                 goto out;
165
166         read_lock_irqsave(&zfcp_data.config_lock, flags);
167         unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
168         if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
169                                      &unit->status)) {
170                 sdp->hostdata = unit;
171                 unit->device = sdp;
172                 zfcp_unit_get(unit);
173                 retval = 0;
174         }
175         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
176  out:
177         return retval;
178 }
179
180 static void
181 zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
182 {
183         struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
184
185         if (unit) {
186                 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
187                 sdpnt->hostdata = NULL;
188                 unit->device = NULL;
189                 zfcp_unit_put(unit);
190         } else {
191                 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
192                                 "address %p\n", sdpnt);
193         }
194 }
195
196 /* 
197  * called from scsi midlayer to allow finetuning of a device.
198  */
199 static int
200 zfcp_scsi_slave_configure(struct scsi_device *sdp)
201 {
202         if (sdp->tagged_supported)
203                 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
204         else
205                 scsi_adjust_queue_depth(sdp, 0, 1);
206         return 0;
207 }
208
209 /**
210  * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
211  * @scpnt: pointer to struct scsi_cmnd where result is set
212  * @result: result to be set in scpnt (e.g. DID_ERROR)
213  */
214 static void
215 zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
216 {
217         set_host_byte(&scpnt->result, result);
218         if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
219                 zfcp_scsi_dbf_event_result("fail", 4,
220                         (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
221                         scpnt, NULL);
222         /* return directly */
223         scpnt->scsi_done(scpnt);
224 }
225
226 /**
227  * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
228  *      zfcp_scsi_command_sync
229  * @adapter: adapter where scsi command is issued
230  * @unit: unit to which scsi command is sent
231  * @scpnt: scsi command to be sent
232  * @timer: timer to be started if request is successfully initiated
233  *
234  * Note: In scsi_done function must be set in scpnt.
235  */
236 int
237 zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
238                         struct scsi_cmnd *scpnt, struct timer_list *timer)
239 {
240         int tmp;
241         int retval;
242
243         retval = 0;
244
245         BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
246         BUG_ON(scpnt->scsi_done == NULL);
247
248         if (unlikely(NULL == unit)) {
249                 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
250                 goto out;
251         }
252
253         if (unlikely(
254               atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
255              !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
256                 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
257                                "0x%016Lx on adapter %s\n",
258                                unit->fcp_lun, unit->port->wwpn,
259                                zfcp_get_busid_by_adapter(adapter));
260                 zfcp_scsi_command_fail(scpnt, DID_ERROR);
261                 goto out;
262         }
263
264         if (unlikely(
265              !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
266                 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
267                                "on port 0x%016Lx in recovery\n",
268                                zfcp_get_busid_by_unit(unit),
269                                unit->fcp_lun, unit->port->wwpn);
270                 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
271                 goto out;
272         }
273
274         tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
275                                              ZFCP_REQ_AUTO_CLEANUP);
276
277         if (unlikely(tmp < 0)) {
278                 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
279                 retval = SCSI_MLQUEUE_HOST_BUSY;
280         }
281
282 out:
283         return retval;
284 }
285
286 void
287 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
288 {
289         struct completion *wait = (struct completion *) scpnt->SCp.ptr;
290         complete(wait);
291 }
292
293
294 /**
295  * zfcp_scsi_command_sync - send a SCSI command and wait for completion
296  * @unit: unit where command is sent to
297  * @scpnt: scsi command to be sent
298  * @timer: timer to be started if request is successfully initiated
299  * Return: 0
300  *
301  * Errors are indicated in scpnt->result
302  */
303 int
304 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
305                        struct timer_list *timer)
306 {
307         int ret;
308         DECLARE_COMPLETION(wait);
309
310         scpnt->SCp.ptr = (void *) &wait;  /* silent re-use */
311         scpnt->scsi_done = zfcp_scsi_command_sync_handler;
312         ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
313         if (ret == 0)
314                 wait_for_completion(&wait);
315
316         scpnt->SCp.ptr = NULL;
317
318         return 0;
319 }
320
321 /*
322  * function:    zfcp_scsi_queuecommand
323  *
324  * purpose:     enqueues a SCSI command to the specified target device
325  *
326  * returns:     0 - success, SCSI command enqueued
327  *              !0 - failure
328  */
329 int
330 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
331                        void (*done) (struct scsi_cmnd *))
332 {
333         struct zfcp_unit *unit;
334         struct zfcp_adapter *adapter;
335
336         /* reset the status for this request */
337         scpnt->result = 0;
338         scpnt->host_scribble = NULL;
339         scpnt->scsi_done = done;
340
341         /*
342          * figure out adapter and target device
343          * (stored there by zfcp_scsi_slave_alloc)
344          */
345         adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
346         unit = (struct zfcp_unit *) scpnt->device->hostdata;
347
348         return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
349 }
350
351 static struct zfcp_unit *
352 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
353                  unsigned int lun)
354 {
355         struct zfcp_port *port;
356         struct zfcp_unit *unit, *retval = NULL;
357
358         list_for_each_entry(port, &adapter->port_list_head, list) {
359                 if (!port->rport || (id != port->rport->scsi_target_id))
360                         continue;
361                 list_for_each_entry(unit, &port->unit_list_head, list) {
362                         if (lun == unit->scsi_lun) {
363                                 retval = unit;
364                                 goto out;
365                         }
366                 }
367         }
368  out:
369         return retval;
370 }
371
372 /**
373  * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
374  * @scpnt: pointer to scsi_cmnd to be aborted 
375  * Return: SUCCESS - command has been aborted and cleaned up in internal
376  *          bookkeeping, SCSI stack won't be called for aborted command
377  *         FAILED - otherwise
378  *
379  * We do not need to care for a SCSI command which completes normally
380  * but late during this abort routine runs.  We are allowed to return
381  * late commands to the SCSI stack.  It tracks the state of commands and
382  * will handle late commands.  (Usually, the normal completion of late
383  * commands is ignored with respect to the running abort operation.)
384  */
385 int
386 zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
387 {
388         struct Scsi_Host *scsi_host;
389         struct zfcp_adapter *adapter;
390         struct zfcp_unit *unit;
391         int retval = SUCCESS;
392         struct zfcp_fsf_req *new_fsf_req = NULL;
393         struct zfcp_fsf_req *old_fsf_req;
394         unsigned long flags;
395
396         scsi_host = scpnt->device->host;
397         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
398         unit = (struct zfcp_unit *) scpnt->device->hostdata;
399
400         ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
401                       scpnt, zfcp_get_busid_by_adapter(adapter));
402
403         /* avoid race condition between late normal completion and abort */
404         write_lock_irqsave(&adapter->abort_lock, flags);
405
406         /*
407          * Check whether command has just completed and can not be aborted.
408          * Even if the command has just been completed late, we can access
409          * scpnt since the SCSI stack does not release it at least until
410          * this routine returns. (scpnt is parameter passed to this routine
411          * and must not disappear during abort even on late completion.)
412          */
413         old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
414         if (!old_fsf_req) {
415                 write_unlock_irqrestore(&adapter->abort_lock, flags);
416                 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, NULL);
417                 retval = SUCCESS;
418                 goto out;
419         }
420         old_fsf_req->data = 0;
421         old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
422
423         /* don't access old_fsf_req after releasing the abort_lock */
424         write_unlock_irqrestore(&adapter->abort_lock, flags);
425         /* call FSF routine which does the abort */
426         new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
427                                                  adapter, unit, 0);
428         if (!new_fsf_req) {
429                 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
430                 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
431                                           old_fsf_req);
432                 retval = FAILED;
433                 goto out;
434         }
435
436         /* wait for completion of abort */
437         __wait_event(new_fsf_req->completion_wq,
438                      new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
439
440         /* status should be valid since signals were not permitted */
441         if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
442                 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req,
443                                           NULL);
444                 retval = SUCCESS;
445         } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
446                 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req,
447                                           NULL);
448                 retval = SUCCESS;
449         } else {
450                 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req,
451                                           NULL);
452                 retval = FAILED;
453         }
454         zfcp_fsf_req_free(new_fsf_req);
455  out:
456         return retval;
457 }
458
459 int
460 zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
461 {
462         int retval;
463         struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
464
465         if (!unit) {
466                 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
467                 retval = SUCCESS;
468                 goto out;
469         }
470         ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
471
472         /*
473          * If we do not know whether the unit supports 'logical unit reset'
474          * then try 'logical unit reset' and proceed with 'target reset'
475          * if 'logical unit reset' fails.
476          * If the unit is known not to support 'logical unit reset' then
477          * skip 'logical unit reset' and try 'target reset' immediately.
478          */
479         if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
480                               &unit->status)) {
481                 retval = zfcp_task_management_function(unit,
482                                                        FCP_LOGICAL_UNIT_RESET,
483                                                        scpnt);
484                 if (retval) {
485                         ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
486                         if (retval == -ENOTSUPP)
487                                 atomic_set_mask
488                                     (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
489                                      &unit->status);
490                         /* fall through and try 'target reset' next */
491                 } else {
492                         ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
493                                        unit);
494                         /* avoid 'target reset' */
495                         retval = SUCCESS;
496                         goto out;
497                 }
498         }
499         retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
500         if (retval) {
501                 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
502                 retval = FAILED;
503         } else {
504                 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
505                 retval = SUCCESS;
506         }
507  out:
508         return retval;
509 }
510
511 static int
512 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
513                               struct scsi_cmnd *scpnt)
514 {
515         struct zfcp_adapter *adapter = unit->port->adapter;
516         struct zfcp_fsf_req *fsf_req;
517         int retval = 0;
518
519         /* issue task management function */
520         fsf_req = zfcp_fsf_send_fcp_command_task_management
521                 (adapter, unit, tm_flags, 0);
522         if (!fsf_req) {
523                 ZFCP_LOG_INFO("error: creation of task management request "
524                               "failed for unit 0x%016Lx on port 0x%016Lx on  "
525                               "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
526                               zfcp_get_busid_by_adapter(adapter));
527                 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
528                 retval = -ENOMEM;
529                 goto out;
530         }
531
532         __wait_event(fsf_req->completion_wq,
533                      fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
534
535         /*
536          * check completion status of task management function
537          */
538         if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
539                 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
540                 retval = -EIO;
541         } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
542                 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
543                 retval = -ENOTSUPP;
544         } else
545                 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
546
547         zfcp_fsf_req_free(fsf_req);
548  out:
549         return retval;
550 }
551
552 /**
553  * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
554  */
555 int
556 zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
557 {
558         struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
559         struct zfcp_adapter *adapter = unit->port->adapter;
560
561         ZFCP_LOG_NORMAL("bus reset because of problems with "
562                         "unit 0x%016Lx\n", unit->fcp_lun);
563         zfcp_erp_adapter_reopen(adapter, 0);
564         zfcp_erp_wait(adapter);
565
566         return SUCCESS;
567 }
568
569 /**
570  * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
571  */
572 int
573 zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
574 {
575         struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
576         struct zfcp_adapter *adapter = unit->port->adapter;
577
578         ZFCP_LOG_NORMAL("host reset because of problems with "
579                         "unit 0x%016Lx\n", unit->fcp_lun);
580         zfcp_erp_adapter_reopen(adapter, 0);
581         zfcp_erp_wait(adapter);
582
583         return SUCCESS;
584 }
585
586 int
587 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
588 {
589         int retval = 0;
590         static unsigned int unique_id = 0;
591
592         /* register adapter as SCSI host with mid layer of SCSI stack */
593         adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
594                                              sizeof (struct zfcp_adapter *));
595         if (!adapter->scsi_host) {
596                 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
597                                 "for adapter %s ",
598                                 zfcp_get_busid_by_adapter(adapter));
599                 retval = -EIO;
600                 goto out;
601         }
602         ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
603
604         /* tell the SCSI stack some characteristics of this adapter */
605         adapter->scsi_host->max_id = 1;
606         adapter->scsi_host->max_lun = 1;
607         adapter->scsi_host->max_channel = 0;
608         adapter->scsi_host->unique_id = unique_id++;    /* FIXME */
609         adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
610         adapter->scsi_host->transportt = zfcp_transport_template;
611
612         /*
613          * save a pointer to our own adapter data structure within
614          * hostdata field of SCSI host data structure
615          */
616         adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
617
618         if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
619                 scsi_host_put(adapter->scsi_host);
620                 retval = -EIO;
621                 goto out;
622         }
623         atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
624  out:
625         return retval;
626 }
627
628 void
629 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
630 {
631         struct Scsi_Host *shost;
632         struct zfcp_port *port;
633
634         shost = adapter->scsi_host;
635         if (!shost)
636                 return;
637         read_lock_irq(&zfcp_data.config_lock);
638         list_for_each_entry(port, &adapter->port_list_head, list)
639                 if (port->rport)
640                         port->rport = NULL;
641         read_unlock_irq(&zfcp_data.config_lock);
642         fc_remove_host(shost);
643         scsi_remove_host(shost);
644         scsi_host_put(shost);
645         adapter->scsi_host = NULL;
646         atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
647
648         return;
649 }
650
651
652 void
653 zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
654 {
655         adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
656         adapter->scsi_er_timer.data = (unsigned long) adapter;
657         adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
658         add_timer(&adapter->scsi_er_timer);
659 }
660
661 /*
662  * Support functions for FC transport class
663  */
664 static struct fc_host_statistics*
665 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
666 {
667         struct fc_host_statistics *fc_stats;
668
669         if (!adapter->fc_stats) {
670                 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
671                 if (!fc_stats)
672                         return NULL;
673                 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
674         }
675         memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
676         return adapter->fc_stats;
677 }
678
679 static void
680 zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
681                           struct fsf_qtcb_bottom_port *data,
682                           struct fsf_qtcb_bottom_port *old)
683 {
684         fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
685                 old->seconds_since_last_reset;
686         fc_stats->tx_frames = data->tx_frames - old->tx_frames;
687         fc_stats->tx_words = data->tx_words - old->tx_words;
688         fc_stats->rx_frames = data->rx_frames - old->rx_frames;
689         fc_stats->rx_words = data->rx_words - old->rx_words;
690         fc_stats->lip_count = data->lip - old->lip;
691         fc_stats->nos_count = data->nos - old->nos;
692         fc_stats->error_frames = data->error_frames - old->error_frames;
693         fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
694         fc_stats->link_failure_count = data->link_failure - old->link_failure;
695         fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
696         fc_stats->loss_of_signal_count = data->loss_of_signal -
697                 old->loss_of_signal;
698         fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
699                 old->psp_error_counts;
700         fc_stats->invalid_tx_word_count = data->invalid_tx_words -
701                 old->invalid_tx_words;
702         fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
703         fc_stats->fcp_input_requests = data->input_requests -
704                 old->input_requests;
705         fc_stats->fcp_output_requests = data->output_requests -
706                 old->output_requests;
707         fc_stats->fcp_control_requests = data->control_requests -
708                 old->control_requests;
709         fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
710         fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
711 }
712
713 static void
714 zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
715                        struct fsf_qtcb_bottom_port *data)
716 {
717         fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
718         fc_stats->tx_frames = data->tx_frames;
719         fc_stats->tx_words = data->tx_words;
720         fc_stats->rx_frames = data->rx_frames;
721         fc_stats->rx_words = data->rx_words;
722         fc_stats->lip_count = data->lip;
723         fc_stats->nos_count = data->nos;
724         fc_stats->error_frames = data->error_frames;
725         fc_stats->dumped_frames = data->dumped_frames;
726         fc_stats->link_failure_count = data->link_failure;
727         fc_stats->loss_of_sync_count = data->loss_of_sync;
728         fc_stats->loss_of_signal_count = data->loss_of_signal;
729         fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
730         fc_stats->invalid_tx_word_count = data->invalid_tx_words;
731         fc_stats->invalid_crc_count = data->invalid_crcs;
732         fc_stats->fcp_input_requests = data->input_requests;
733         fc_stats->fcp_output_requests = data->output_requests;
734         fc_stats->fcp_control_requests = data->control_requests;
735         fc_stats->fcp_input_megabytes = data->input_mb;
736         fc_stats->fcp_output_megabytes = data->output_mb;
737 }
738
739 /**
740  * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
741  *
742  * assumption: scsi_transport_fc synchronizes calls of
743  *             get_fc_host_stats and reset_fc_host_stats
744  *             (XXX to be checked otherwise introduce locking)
745  */
746 static struct fc_host_statistics *
747 zfcp_get_fc_host_stats(struct Scsi_Host *shost)
748 {
749         struct zfcp_adapter *adapter;
750         struct fc_host_statistics *fc_stats;
751         struct fsf_qtcb_bottom_port *data;
752         int ret;
753
754         adapter = (struct zfcp_adapter *)shost->hostdata[0];
755         fc_stats = zfcp_init_fc_host_stats(adapter);
756         if (!fc_stats)
757                 return NULL;
758
759         data = kzalloc(sizeof(*data), GFP_KERNEL);
760         if (!data)
761                 return NULL;
762
763         ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
764         if (ret) {
765                 kfree(data);
766                 return NULL; /* XXX return zeroed fc_stats? */
767         }
768
769         if (adapter->stats_reset &&
770             ((jiffies/HZ - adapter->stats_reset) <
771              data->seconds_since_last_reset)) {
772                 zfcp_adjust_fc_host_stats(fc_stats, data,
773                                           adapter->stats_reset_data);
774         } else
775                 zfcp_set_fc_host_stats(fc_stats, data);
776
777         kfree(data);
778         return fc_stats;
779 }
780
781 static void
782 zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
783 {
784         struct zfcp_adapter *adapter;
785         struct fsf_qtcb_bottom_port *data, *old_data;
786         int ret;
787
788         adapter = (struct zfcp_adapter *)shost->hostdata[0];
789         data = kzalloc(sizeof(*data), GFP_KERNEL);
790         if (!data)
791                 return;
792
793         ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
794         if (ret == 0) {
795                 adapter->stats_reset = jiffies/HZ;
796                 old_data = adapter->stats_reset_data;
797                 adapter->stats_reset_data = data; /* finally freed in
798                                                      adater_dequeue */
799                 kfree(old_data);
800         }
801 }
802
803 struct fc_function_template zfcp_transport_functions = {
804         .show_starget_port_id = 1,
805         .show_starget_port_name = 1,
806         .show_starget_node_name = 1,
807         .show_rport_supported_classes = 1,
808         .show_rport_maxframe_size = 1,
809         .show_host_node_name = 1,
810         .show_host_port_name = 1,
811         .show_host_permanent_port_name = 1,
812         .show_host_supported_classes = 1,
813         .show_host_supported_speeds = 1,
814         .show_host_maxframe_size = 1,
815         .show_host_serial_number = 1,
816         .get_fc_host_stats = zfcp_get_fc_host_stats,
817         .reset_fc_host_stats = zfcp_reset_fc_host_stats,
818         /* no functions registered for following dynamic attributes but
819            directly set by LLDD */
820         .show_host_port_type = 1,
821         .show_host_speed = 1,
822         .show_host_port_id = 1,
823 };
824
825 /**
826  * ZFCP_DEFINE_SCSI_ATTR
827  * @_name:   name of show attribute
828  * @_format: format string
829  * @_value:  value to print
830  *
831  * Generates attribute for a unit.
832  */
833 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)                    \
834 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr,        \
835                                               char *buf)                 \
836 {                                                                        \
837         struct scsi_device *sdev;                                        \
838         struct zfcp_unit *unit;                                          \
839                                                                          \
840         sdev = to_scsi_device(dev);                                      \
841         unit = sdev->hostdata;                                           \
842         return sprintf(buf, _format, _value);                            \
843 }                                                                        \
844                                                                          \
845 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
846
847 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
848 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
849 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
850
851 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
852         &dev_attr_fcp_lun,
853         &dev_attr_wwpn,
854         &dev_attr_hba_id,
855         NULL
856 };
857
858 #undef ZFCP_LOG_AREA