[SCSI] zfcp: Use also port and adapter to identify unit in messages.
[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 #include <asm/atomic.h>
26
27 static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
28 static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
29 static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
30 static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
31                                   void (*done) (struct scsi_cmnd *));
32 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
33 static int zfcp_scsi_eh_device_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 zfcp_data zfcp_data = {
44         .scsi_host_template = {
45                 .name                   = ZFCP_NAME,
46                 .module                 = THIS_MODULE,
47                 .proc_name              = "zfcp",
48                 .slave_alloc            = zfcp_scsi_slave_alloc,
49                 .slave_configure        = zfcp_scsi_slave_configure,
50                 .slave_destroy          = zfcp_scsi_slave_destroy,
51                 .queuecommand           = zfcp_scsi_queuecommand,
52                 .eh_abort_handler       = zfcp_scsi_eh_abort_handler,
53                 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
54                 .eh_host_reset_handler  = zfcp_scsi_eh_host_reset_handler,
55                 .can_queue              = 4096,
56                 .this_id                = -1,
57                 .sg_tablesize           = ZFCP_MAX_SBALES_PER_REQ,
58                 .cmd_per_lun            = 1,
59                 .use_clustering         = 1,
60                 .sdev_attrs             = zfcp_sysfs_sdev_attrs,
61                 .max_sectors            = ZFCP_MAX_SECTORS,
62         },
63         .driver_version = ZFCP_VERSION,
64 };
65
66 /* Find start of Response Information in FCP response unit*/
67 char *
68 zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
69 {
70         char *fcp_rsp_info_ptr;
71
72         fcp_rsp_info_ptr =
73             (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
74
75         return fcp_rsp_info_ptr;
76 }
77
78 /* Find start of Sense Information in FCP response unit*/
79 char *
80 zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
81 {
82         char *fcp_sns_info_ptr;
83
84         fcp_sns_info_ptr =
85             (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
86         if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
87                 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
88                     fcp_rsp_iu->fcp_rsp_len;
89
90         return fcp_sns_info_ptr;
91 }
92
93 static fcp_dl_t *
94 zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
95 {
96         int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
97         fcp_dl_t *fcp_dl_addr;
98
99         fcp_dl_addr = (fcp_dl_t *)
100                 ((unsigned char *) fcp_cmd +
101                  sizeof (struct fcp_cmnd_iu) + additional_length);
102         /*
103          * fcp_dl_addr = start address of fcp_cmnd structure +
104          * size of fixed part + size of dynamically sized add_dcp_cdb field
105          * SEE FCP-2 documentation
106          */
107         return fcp_dl_addr;
108 }
109
110 fcp_dl_t
111 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
112 {
113         return *zfcp_get_fcp_dl_ptr(fcp_cmd);
114 }
115
116 void
117 zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
118 {
119         *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
120 }
121
122 /*
123  * note: it's a bit-or operation not an assignment
124  * regarding the specified byte
125  */
126 static inline void
127 set_byte(int *result, char status, char pos)
128 {
129         *result |= status << (pos * 8);
130 }
131
132 void
133 set_host_byte(int *result, char status)
134 {
135         set_byte(result, status, 2);
136 }
137
138 void
139 set_driver_byte(int *result, char status)
140 {
141         set_byte(result, status, 3);
142 }
143
144 static int
145 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
146 {
147         struct zfcp_adapter *adapter;
148         struct zfcp_unit *unit;
149         unsigned long flags;
150         int retval = -ENXIO;
151
152         adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
153         if (!adapter)
154                 goto out;
155
156         read_lock_irqsave(&zfcp_data.config_lock, flags);
157         unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
158         if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
159                                      &unit->status)) {
160                 sdp->hostdata = unit;
161                 unit->device = sdp;
162                 zfcp_unit_get(unit);
163                 retval = 0;
164         }
165         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
166  out:
167         return retval;
168 }
169
170 /**
171  * zfcp_scsi_slave_destroy - called when scsi device is removed
172  *
173  * Remove reference to associated scsi device for an zfcp_unit.
174  * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
175  * or a scan for this device might have failed.
176  */
177 static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
178 {
179         struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
180
181         if (unit) {
182                 zfcp_erp_wait(unit->port->adapter);
183                 wait_event(unit->scsi_scan_wq,
184                            atomic_test_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING,
185                                             &unit->status) == 0);
186                 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
187                 sdpnt->hostdata = NULL;
188                 unit->device = NULL;
189                 zfcp_erp_unit_failed(unit);
190                 zfcp_unit_put(unit);
191         } else
192                 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
193                                 "address %p\n", sdpnt);
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, int use_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, use_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 static 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  * @use_timer: indicates whether timer should be setup or not
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                        int use_timer)
306 {
307         int ret;
308         DECLARE_COMPLETION_ONSTACK(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,
313                                       use_timer);
314         if (ret == 0)
315                 wait_for_completion(&wait);
316
317         scpnt->SCp.ptr = NULL;
318
319         return 0;
320 }
321
322 /*
323  * function:    zfcp_scsi_queuecommand
324  *
325  * purpose:     enqueues a SCSI command to the specified target device
326  *
327  * returns:     0 - success, SCSI command enqueued
328  *              !0 - failure
329  */
330 static int
331 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
332                        void (*done) (struct scsi_cmnd *))
333 {
334         struct zfcp_unit *unit;
335         struct zfcp_adapter *adapter;
336
337         /* reset the status for this request */
338         scpnt->result = 0;
339         scpnt->host_scribble = NULL;
340         scpnt->scsi_done = done;
341
342         /*
343          * figure out adapter and target device
344          * (stored there by zfcp_scsi_slave_alloc)
345          */
346         adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
347         unit = (struct zfcp_unit *) scpnt->device->hostdata;
348
349         return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
350 }
351
352 static struct zfcp_unit *
353 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
354                  unsigned int lun)
355 {
356         struct zfcp_port *port;
357         struct zfcp_unit *unit, *retval = NULL;
358
359         list_for_each_entry(port, &adapter->port_list_head, list) {
360                 if (!port->rport || (id != port->rport->scsi_target_id))
361                         continue;
362                 list_for_each_entry(unit, &port->unit_list_head, list)
363                         if (lun == unit->scsi_lun) {
364                                 retval = unit;
365                                 goto out;
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 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
386 {
387         struct Scsi_Host *scsi_host;
388         struct zfcp_adapter *adapter;
389         struct zfcp_unit *unit;
390         struct zfcp_fsf_req *fsf_req;
391         unsigned long flags;
392         unsigned long old_req_id;
393         int retval = SUCCESS;
394
395         scsi_host = scpnt->device->host;
396         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
397         unit = (struct zfcp_unit *) scpnt->device->hostdata;
398
399         ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
400                       scpnt, zfcp_get_busid_by_adapter(adapter));
401
402         /* avoid race condition between late normal completion and abort */
403         write_lock_irqsave(&adapter->abort_lock, flags);
404
405         /* Check whether corresponding fsf_req is still pending */
406         spin_lock(&adapter->req_list_lock);
407         fsf_req = zfcp_reqlist_find(adapter,
408                                     (unsigned long) scpnt->host_scribble);
409         spin_unlock(&adapter->req_list_lock);
410         if (!fsf_req) {
411                 write_unlock_irqrestore(&adapter->abort_lock, flags);
412                 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
413                 retval = SUCCESS;
414                 goto out;
415         }
416         fsf_req->data = 0;
417         fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
418         old_req_id = fsf_req->req_id;
419
420         /* don't access old fsf_req after releasing the abort_lock */
421         write_unlock_irqrestore(&adapter->abort_lock, flags);
422
423         fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
424         if (!fsf_req) {
425                 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
426                 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
427                                           old_req_id);
428                 retval = FAILED;
429                 goto out;
430         }
431
432         __wait_event(fsf_req->completion_wq,
433                      fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
434
435         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
436                 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
437                 retval = SUCCESS;
438         } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
439                 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
440                 retval = SUCCESS;
441         } else {
442                 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
443                 retval = FAILED;
444         }
445         zfcp_fsf_req_free(fsf_req);
446  out:
447         return retval;
448 }
449
450 static int
451 zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
452 {
453         int retval;
454         struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
455
456         if (!unit) {
457                 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
458                 retval = SUCCESS;
459                 goto out;
460         }
461         ZFCP_LOG_NORMAL("resetting unit 0x%016Lx on port 0x%016Lx, adapter %s\n",
462                         unit->fcp_lun, unit->port->wwpn,
463                         zfcp_get_busid_by_adapter(unit->port->adapter));
464
465         /*
466          * If we do not know whether the unit supports 'logical unit reset'
467          * then try 'logical unit reset' and proceed with 'target reset'
468          * if 'logical unit reset' fails.
469          * If the unit is known not to support 'logical unit reset' then
470          * skip 'logical unit reset' and try 'target reset' immediately.
471          */
472         if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
473                               &unit->status)) {
474                 retval = zfcp_task_management_function(unit,
475                                                        FCP_LOGICAL_UNIT_RESET,
476                                                        scpnt);
477                 if (retval) {
478                         ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
479                         if (retval == -ENOTSUPP)
480                                 atomic_set_mask
481                                     (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
482                                      &unit->status);
483                         /* fall through and try 'target reset' next */
484                 } else {
485                         ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
486                                        unit);
487                         /* avoid 'target reset' */
488                         retval = SUCCESS;
489                         goto out;
490                 }
491         }
492         retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
493         if (retval) {
494                 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
495                 retval = FAILED;
496         } else {
497                 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
498                 retval = SUCCESS;
499         }
500  out:
501         return retval;
502 }
503
504 static int
505 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
506                               struct scsi_cmnd *scpnt)
507 {
508         struct zfcp_adapter *adapter = unit->port->adapter;
509         struct zfcp_fsf_req *fsf_req;
510         int retval = 0;
511
512         /* issue task management function */
513         fsf_req = zfcp_fsf_send_fcp_command_task_management
514                 (adapter, unit, tm_flags, 0);
515         if (!fsf_req) {
516                 ZFCP_LOG_INFO("error: creation of task management request "
517                               "failed for unit 0x%016Lx on port 0x%016Lx on  "
518                               "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
519                               zfcp_get_busid_by_adapter(adapter));
520                 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
521                 retval = -ENOMEM;
522                 goto out;
523         }
524
525         __wait_event(fsf_req->completion_wq,
526                      fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
527
528         /*
529          * check completion status of task management function
530          */
531         if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
532                 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
533                 retval = -EIO;
534         } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
535                 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
536                 retval = -ENOTSUPP;
537         } else
538                 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
539
540         zfcp_fsf_req_free(fsf_req);
541  out:
542         return retval;
543 }
544
545 /**
546  * zfcp_scsi_eh_host_reset_handler - handler for host reset
547  */
548 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
549 {
550         struct zfcp_unit *unit;
551         struct zfcp_adapter *adapter;
552
553         unit = (struct zfcp_unit*) scpnt->device->hostdata;
554         adapter = unit->port->adapter;
555
556         ZFCP_LOG_NORMAL("host reset because of problems with "
557                 "unit 0x%016Lx on port 0x%016Lx, adapter %s\n",
558                 unit->fcp_lun, unit->port->wwpn,
559                 zfcp_get_busid_by_adapter(unit->port->adapter));
560
561         zfcp_erp_adapter_reopen(adapter, 0);
562         zfcp_erp_wait(adapter);
563
564         return SUCCESS;
565 }
566
567 int
568 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
569 {
570         int retval = 0;
571         static unsigned int unique_id = 0;
572
573         if (adapter->scsi_host)
574                 goto out;
575
576         /* register adapter as SCSI host with mid layer of SCSI stack */
577         adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
578                                              sizeof (struct zfcp_adapter *));
579         if (!adapter->scsi_host) {
580                 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
581                                 "for adapter %s ",
582                                 zfcp_get_busid_by_adapter(adapter));
583                 retval = -EIO;
584                 goto out;
585         }
586         ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
587
588         /* tell the SCSI stack some characteristics of this adapter */
589         adapter->scsi_host->max_id = 1;
590         adapter->scsi_host->max_lun = 1;
591         adapter->scsi_host->max_channel = 0;
592         adapter->scsi_host->unique_id = unique_id++;    /* FIXME */
593         adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
594         adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
595
596         /*
597          * save a pointer to our own adapter data structure within
598          * hostdata field of SCSI host data structure
599          */
600         adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
601
602         if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
603                 scsi_host_put(adapter->scsi_host);
604                 retval = -EIO;
605                 goto out;
606         }
607         atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
608  out:
609         return retval;
610 }
611
612 void
613 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
614 {
615         struct Scsi_Host *shost;
616         struct zfcp_port *port;
617
618         shost = adapter->scsi_host;
619         if (!shost)
620                 return;
621         read_lock_irq(&zfcp_data.config_lock);
622         list_for_each_entry(port, &adapter->port_list_head, list)
623                 if (port->rport)
624                         port->rport = NULL;
625         read_unlock_irq(&zfcp_data.config_lock);
626         fc_remove_host(shost);
627         scsi_remove_host(shost);
628         scsi_host_put(shost);
629         adapter->scsi_host = NULL;
630         atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
631
632         return;
633 }
634
635 /*
636  * Support functions for FC transport class
637  */
638 static struct fc_host_statistics*
639 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
640 {
641         struct fc_host_statistics *fc_stats;
642
643         if (!adapter->fc_stats) {
644                 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
645                 if (!fc_stats)
646                         return NULL;
647                 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
648         }
649         memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
650         return adapter->fc_stats;
651 }
652
653 static void
654 zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
655                           struct fsf_qtcb_bottom_port *data,
656                           struct fsf_qtcb_bottom_port *old)
657 {
658         fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
659                 old->seconds_since_last_reset;
660         fc_stats->tx_frames = data->tx_frames - old->tx_frames;
661         fc_stats->tx_words = data->tx_words - old->tx_words;
662         fc_stats->rx_frames = data->rx_frames - old->rx_frames;
663         fc_stats->rx_words = data->rx_words - old->rx_words;
664         fc_stats->lip_count = data->lip - old->lip;
665         fc_stats->nos_count = data->nos - old->nos;
666         fc_stats->error_frames = data->error_frames - old->error_frames;
667         fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
668         fc_stats->link_failure_count = data->link_failure - old->link_failure;
669         fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
670         fc_stats->loss_of_signal_count = data->loss_of_signal -
671                 old->loss_of_signal;
672         fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
673                 old->psp_error_counts;
674         fc_stats->invalid_tx_word_count = data->invalid_tx_words -
675                 old->invalid_tx_words;
676         fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
677         fc_stats->fcp_input_requests = data->input_requests -
678                 old->input_requests;
679         fc_stats->fcp_output_requests = data->output_requests -
680                 old->output_requests;
681         fc_stats->fcp_control_requests = data->control_requests -
682                 old->control_requests;
683         fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
684         fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
685 }
686
687 static void
688 zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
689                        struct fsf_qtcb_bottom_port *data)
690 {
691         fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
692         fc_stats->tx_frames = data->tx_frames;
693         fc_stats->tx_words = data->tx_words;
694         fc_stats->rx_frames = data->rx_frames;
695         fc_stats->rx_words = data->rx_words;
696         fc_stats->lip_count = data->lip;
697         fc_stats->nos_count = data->nos;
698         fc_stats->error_frames = data->error_frames;
699         fc_stats->dumped_frames = data->dumped_frames;
700         fc_stats->link_failure_count = data->link_failure;
701         fc_stats->loss_of_sync_count = data->loss_of_sync;
702         fc_stats->loss_of_signal_count = data->loss_of_signal;
703         fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
704         fc_stats->invalid_tx_word_count = data->invalid_tx_words;
705         fc_stats->invalid_crc_count = data->invalid_crcs;
706         fc_stats->fcp_input_requests = data->input_requests;
707         fc_stats->fcp_output_requests = data->output_requests;
708         fc_stats->fcp_control_requests = data->control_requests;
709         fc_stats->fcp_input_megabytes = data->input_mb;
710         fc_stats->fcp_output_megabytes = data->output_mb;
711 }
712
713 /**
714  * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
715  *
716  * assumption: scsi_transport_fc synchronizes calls of
717  *             get_fc_host_stats and reset_fc_host_stats
718  *             (XXX to be checked otherwise introduce locking)
719  */
720 static struct fc_host_statistics *
721 zfcp_get_fc_host_stats(struct Scsi_Host *shost)
722 {
723         struct zfcp_adapter *adapter;
724         struct fc_host_statistics *fc_stats;
725         struct fsf_qtcb_bottom_port *data;
726         int ret;
727
728         adapter = (struct zfcp_adapter *)shost->hostdata[0];
729         fc_stats = zfcp_init_fc_host_stats(adapter);
730         if (!fc_stats)
731                 return NULL;
732
733         data = kzalloc(sizeof(*data), GFP_KERNEL);
734         if (!data)
735                 return NULL;
736
737         ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
738         if (ret) {
739                 kfree(data);
740                 return NULL; /* XXX return zeroed fc_stats? */
741         }
742
743         if (adapter->stats_reset &&
744             ((jiffies/HZ - adapter->stats_reset) <
745              data->seconds_since_last_reset)) {
746                 zfcp_adjust_fc_host_stats(fc_stats, data,
747                                           adapter->stats_reset_data);
748         } else
749                 zfcp_set_fc_host_stats(fc_stats, data);
750
751         kfree(data);
752         return fc_stats;
753 }
754
755 static void
756 zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
757 {
758         struct zfcp_adapter *adapter;
759         struct fsf_qtcb_bottom_port *data, *old_data;
760         int ret;
761
762         adapter = (struct zfcp_adapter *)shost->hostdata[0];
763         data = kzalloc(sizeof(*data), GFP_KERNEL);
764         if (!data)
765                 return;
766
767         ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
768         if (ret) {
769                 kfree(data);
770         } else {
771                 adapter->stats_reset = jiffies/HZ;
772                 old_data = adapter->stats_reset_data;
773                 adapter->stats_reset_data = data; /* finally freed in
774                                                      adater_dequeue */
775                 kfree(old_data);
776         }
777 }
778
779 static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
780 {
781         rport->dev_loss_tmo = timeout;
782 }
783
784 struct fc_function_template zfcp_transport_functions = {
785         .show_starget_port_id = 1,
786         .show_starget_port_name = 1,
787         .show_starget_node_name = 1,
788         .show_rport_supported_classes = 1,
789         .show_rport_maxframe_size = 1,
790         .show_rport_dev_loss_tmo = 1,
791         .show_host_node_name = 1,
792         .show_host_port_name = 1,
793         .show_host_permanent_port_name = 1,
794         .show_host_supported_classes = 1,
795         .show_host_supported_speeds = 1,
796         .show_host_maxframe_size = 1,
797         .show_host_serial_number = 1,
798         .get_fc_host_stats = zfcp_get_fc_host_stats,
799         .reset_fc_host_stats = zfcp_reset_fc_host_stats,
800         .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
801         /* no functions registered for following dynamic attributes but
802            directly set by LLDD */
803         .show_host_port_type = 1,
804         .show_host_speed = 1,
805         .show_host_port_id = 1,
806         .disable_target_scan = 1,
807 };
808
809 /**
810  * ZFCP_DEFINE_SCSI_ATTR
811  * @_name:   name of show attribute
812  * @_format: format string
813  * @_value:  value to print
814  *
815  * Generates attribute for a unit.
816  */
817 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)                    \
818 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr,        \
819                                               char *buf)                 \
820 {                                                                        \
821         struct scsi_device *sdev;                                        \
822         struct zfcp_unit *unit;                                          \
823                                                                          \
824         sdev = to_scsi_device(dev);                                      \
825         unit = sdev->hostdata;                                           \
826         return sprintf(buf, _format, _value);                            \
827 }                                                                        \
828                                                                          \
829 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
830
831 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
832 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
833 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
834
835 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
836         &dev_attr_fcp_lun,
837         &dev_attr_wwpn,
838         &dev_attr_hba_id,
839         NULL
840 };
841
842 #undef ZFCP_LOG_AREA