[SCSI] zfcp: Move CFDC code to new file.
[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_target_reset_handler(struct scsi_cmnd *);
35 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
36 static int zfcp_task_management_function(struct zfcp_unit *, u8,
37                                          struct scsi_cmnd *);
38
39 static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
40                                           unsigned int, unsigned int);
41
42 static struct device_attribute *zfcp_sysfs_sdev_attrs[];
43 static struct device_attribute *zfcp_a_stats_attrs[];
44
45 struct zfcp_data zfcp_data = {
46         .scsi_host_template = {
47                 .name                   = ZFCP_NAME,
48                 .module                 = THIS_MODULE,
49                 .proc_name              = "zfcp",
50                 .slave_alloc            = zfcp_scsi_slave_alloc,
51                 .slave_configure        = zfcp_scsi_slave_configure,
52                 .slave_destroy          = zfcp_scsi_slave_destroy,
53                 .queuecommand           = zfcp_scsi_queuecommand,
54                 .eh_abort_handler       = zfcp_scsi_eh_abort_handler,
55                 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
56                 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
57                 .eh_host_reset_handler  = zfcp_scsi_eh_host_reset_handler,
58                 .can_queue              = 4096,
59                 .this_id                = -1,
60                 .sg_tablesize           = ZFCP_MAX_SBALES_PER_REQ,
61                 .cmd_per_lun            = 1,
62                 .use_clustering         = 1,
63                 .sdev_attrs             = zfcp_sysfs_sdev_attrs,
64                 .max_sectors            = ZFCP_MAX_SECTORS,
65                 .shost_attrs            = zfcp_a_stats_attrs,
66         },
67         .driver_version = ZFCP_VERSION,
68 };
69
70 /* Find start of Response Information in FCP response unit*/
71 char *
72 zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
73 {
74         char *fcp_rsp_info_ptr;
75
76         fcp_rsp_info_ptr =
77             (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
78
79         return fcp_rsp_info_ptr;
80 }
81
82 /* Find start of Sense Information in FCP response unit*/
83 char *
84 zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
85 {
86         char *fcp_sns_info_ptr;
87
88         fcp_sns_info_ptr =
89             (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
90         if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
91                 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
92                     fcp_rsp_iu->fcp_rsp_len;
93
94         return fcp_sns_info_ptr;
95 }
96
97 static fcp_dl_t *
98 zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
99 {
100         int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
101         fcp_dl_t *fcp_dl_addr;
102
103         fcp_dl_addr = (fcp_dl_t *)
104                 ((unsigned char *) fcp_cmd +
105                  sizeof (struct fcp_cmnd_iu) + additional_length);
106         /*
107          * fcp_dl_addr = start address of fcp_cmnd structure +
108          * size of fixed part + size of dynamically sized add_dcp_cdb field
109          * SEE FCP-2 documentation
110          */
111         return fcp_dl_addr;
112 }
113
114 fcp_dl_t
115 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
116 {
117         return *zfcp_get_fcp_dl_ptr(fcp_cmd);
118 }
119
120 void
121 zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
122 {
123         *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
124 }
125
126 /*
127  * note: it's a bit-or operation not an assignment
128  * regarding the specified byte
129  */
130 static inline void
131 set_byte(int *result, char status, char pos)
132 {
133         *result |= status << (pos * 8);
134 }
135
136 void
137 set_host_byte(int *result, char status)
138 {
139         set_byte(result, status, 2);
140 }
141
142 void
143 set_driver_byte(int *result, char status)
144 {
145         set_byte(result, status, 3);
146 }
147
148 static int
149 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
150 {
151         struct zfcp_adapter *adapter;
152         struct zfcp_unit *unit;
153         unsigned long flags;
154         int retval = -ENXIO;
155
156         adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
157         if (!adapter)
158                 goto out;
159
160         read_lock_irqsave(&zfcp_data.config_lock, flags);
161         unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
162         if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
163                                      &unit->status)) {
164                 sdp->hostdata = unit;
165                 unit->device = sdp;
166                 zfcp_unit_get(unit);
167                 retval = 0;
168         }
169         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
170  out:
171         return retval;
172 }
173
174 /**
175  * zfcp_scsi_slave_destroy - called when scsi device is removed
176  *
177  * Remove reference to associated scsi device for an zfcp_unit.
178  * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
179  * or a scan for this device might have failed.
180  */
181 static void 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_erp_unit_failed(unit, 12, NULL);
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         tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
265                                              ZFCP_REQ_AUTO_CLEANUP);
266         if (unlikely(tmp == -EBUSY)) {
267                 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
268                                "on port 0x%016Lx in recovery\n",
269                                zfcp_get_busid_by_unit(unit),
270                                unit->fcp_lun, unit->port->wwpn);
271                 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
272                 goto out;
273         }
274
275         if (unlikely(tmp < 0)) {
276                 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
277                 retval = SCSI_MLQUEUE_HOST_BUSY;
278         }
279
280 out:
281         return retval;
282 }
283
284 static void
285 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
286 {
287         struct completion *wait = (struct completion *) scpnt->SCp.ptr;
288         complete(wait);
289 }
290
291
292 /**
293  * zfcp_scsi_command_sync - send a SCSI command and wait for completion
294  * @unit: unit where command is sent to
295  * @scpnt: scsi command to be sent
296  * @use_timer: indicates whether timer should be setup or not
297  * Return: 0
298  *
299  * Errors are indicated in scpnt->result
300  */
301 int
302 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
303                        int use_timer)
304 {
305         int ret;
306         DECLARE_COMPLETION_ONSTACK(wait);
307
308         scpnt->SCp.ptr = (void *) &wait;  /* silent re-use */
309         scpnt->scsi_done = zfcp_scsi_command_sync_handler;
310         ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
311                                       use_timer);
312         if (ret == 0)
313                 wait_for_completion(&wait);
314
315         scpnt->SCp.ptr = NULL;
316
317         return 0;
318 }
319
320 /*
321  * function:    zfcp_scsi_queuecommand
322  *
323  * purpose:     enqueues a SCSI command to the specified target device
324  *
325  * returns:     0 - success, SCSI command enqueued
326  *              !0 - failure
327  */
328 static int
329 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
330                        void (*done) (struct scsi_cmnd *))
331 {
332         struct zfcp_unit *unit;
333         struct zfcp_adapter *adapter;
334
335         /* reset the status for this request */
336         scpnt->result = 0;
337         scpnt->host_scribble = NULL;
338         scpnt->scsi_done = done;
339
340         /*
341          * figure out adapter and target device
342          * (stored there by zfcp_scsi_slave_alloc)
343          */
344         adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
345         unit = (struct zfcp_unit *) scpnt->device->hostdata;
346
347         return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
348 }
349
350 static struct zfcp_unit *
351 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
352                  unsigned int lun)
353 {
354         struct zfcp_port *port;
355         struct zfcp_unit *unit, *retval = NULL;
356
357         list_for_each_entry(port, &adapter->port_list_head, list) {
358                 if (!port->rport || (id != port->rport->scsi_target_id))
359                         continue;
360                 list_for_each_entry(unit, &port->unit_list_head, list)
361                         if (lun == unit->scsi_lun) {
362                                 retval = unit;
363                                 goto out;
364                         }
365         }
366  out:
367         return retval;
368 }
369
370 /**
371  * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
372  * @scpnt: pointer to scsi_cmnd to be aborted
373  * Return: SUCCESS - command has been aborted and cleaned up in internal
374  *          bookkeeping, SCSI stack won't be called for aborted command
375  *         FAILED - otherwise
376  *
377  * We do not need to care for a SCSI command which completes normally
378  * but late during this abort routine runs.  We are allowed to return
379  * late commands to the SCSI stack.  It tracks the state of commands and
380  * will handle late commands.  (Usually, the normal completion of late
381  * commands is ignored with respect to the running abort operation.)
382  */
383 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
384 {
385         struct Scsi_Host *scsi_host;
386         struct zfcp_adapter *adapter;
387         struct zfcp_unit *unit;
388         struct zfcp_fsf_req *fsf_req;
389         unsigned long flags;
390         unsigned long old_req_id;
391         int retval = SUCCESS;
392
393         scsi_host = scpnt->device->host;
394         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
395         unit = (struct zfcp_unit *) scpnt->device->hostdata;
396
397         ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
398                       scpnt, zfcp_get_busid_by_adapter(adapter));
399
400         /* avoid race condition between late normal completion and abort */
401         write_lock_irqsave(&adapter->abort_lock, flags);
402
403         /* Check whether corresponding fsf_req is still pending */
404         spin_lock(&adapter->req_list_lock);
405         fsf_req = zfcp_reqlist_find(adapter,
406                                     (unsigned long) scpnt->host_scribble);
407         spin_unlock(&adapter->req_list_lock);
408         if (!fsf_req) {
409                 write_unlock_irqrestore(&adapter->abort_lock, flags);
410                 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
411                 retval = SUCCESS;
412                 goto out;
413         }
414         fsf_req->data = 0;
415         fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
416         old_req_id = fsf_req->req_id;
417
418         /* don't access old fsf_req after releasing the abort_lock */
419         write_unlock_irqrestore(&adapter->abort_lock, flags);
420
421         fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
422         if (!fsf_req) {
423                 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
424                 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
425                                           old_req_id);
426                 retval = FAILED;
427                 goto out;
428         }
429
430         __wait_event(fsf_req->completion_wq,
431                      fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
432
433         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
434                 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
435                 retval = SUCCESS;
436         } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
437                 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
438                 retval = SUCCESS;
439         } else {
440                 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
441                 retval = FAILED;
442         }
443         zfcp_fsf_req_free(fsf_req);
444  out:
445         return retval;
446 }
447
448 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
449 {
450         int retval;
451         struct zfcp_unit *unit = scpnt->device->hostdata;
452
453         if (!unit) {
454                 WARN_ON(1);
455                 return SUCCESS;
456         }
457         retval = zfcp_task_management_function(unit,
458                                                FCP_LOGICAL_UNIT_RESET,
459                                                scpnt);
460         return retval ? FAILED : SUCCESS;
461 }
462
463 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
464 {
465         int retval;
466         struct zfcp_unit *unit = scpnt->device->hostdata;
467
468         if (!unit) {
469                 WARN_ON(1);
470                 return SUCCESS;
471         }
472         retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
473         return retval ? FAILED : SUCCESS;
474 }
475
476 static int
477 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
478                               struct scsi_cmnd *scpnt)
479 {
480         struct zfcp_adapter *adapter = unit->port->adapter;
481         struct zfcp_fsf_req *fsf_req;
482         int retval = 0;
483
484         /* issue task management function */
485         fsf_req = zfcp_fsf_send_fcp_command_task_management
486                 (adapter, unit, tm_flags, 0);
487         if (!fsf_req) {
488                 ZFCP_LOG_INFO("error: creation of task management request "
489                               "failed for unit 0x%016Lx on port 0x%016Lx on  "
490                               "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
491                               zfcp_get_busid_by_adapter(adapter));
492                 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
493                 retval = -ENOMEM;
494                 goto out;
495         }
496
497         __wait_event(fsf_req->completion_wq,
498                      fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
499
500         /*
501          * check completion status of task management function
502          */
503         if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
504                 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
505                 retval = -EIO;
506         } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
507                 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
508                 retval = -ENOTSUPP;
509         } else
510                 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
511
512         zfcp_fsf_req_free(fsf_req);
513  out:
514         return retval;
515 }
516
517 /**
518  * zfcp_scsi_eh_host_reset_handler - handler for host reset
519  */
520 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
521 {
522         struct zfcp_unit *unit;
523         struct zfcp_adapter *adapter;
524
525         unit = (struct zfcp_unit*) scpnt->device->hostdata;
526         adapter = unit->port->adapter;
527
528         ZFCP_LOG_NORMAL("host reset because of problems with "
529                 "unit 0x%016Lx on port 0x%016Lx, adapter %s\n",
530                 unit->fcp_lun, unit->port->wwpn,
531                 zfcp_get_busid_by_adapter(unit->port->adapter));
532
533         zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
534         zfcp_erp_wait(adapter);
535
536         return SUCCESS;
537 }
538
539 int
540 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
541 {
542         int retval = 0;
543         static unsigned int unique_id = 0;
544
545         if (adapter->scsi_host)
546                 goto out;
547
548         /* register adapter as SCSI host with mid layer of SCSI stack */
549         adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
550                                              sizeof (struct zfcp_adapter *));
551         if (!adapter->scsi_host) {
552                 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
553                                 "for adapter %s ",
554                                 zfcp_get_busid_by_adapter(adapter));
555                 retval = -EIO;
556                 goto out;
557         }
558         ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
559
560         /* tell the SCSI stack some characteristics of this adapter */
561         adapter->scsi_host->max_id = 1;
562         adapter->scsi_host->max_lun = 1;
563         adapter->scsi_host->max_channel = 0;
564         adapter->scsi_host->unique_id = unique_id++;    /* FIXME */
565         adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
566         adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
567
568         /*
569          * save a pointer to our own adapter data structure within
570          * hostdata field of SCSI host data structure
571          */
572         adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
573
574         if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
575                 scsi_host_put(adapter->scsi_host);
576                 retval = -EIO;
577                 goto out;
578         }
579         atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
580  out:
581         return retval;
582 }
583
584 void
585 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
586 {
587         struct Scsi_Host *shost;
588         struct zfcp_port *port;
589
590         shost = adapter->scsi_host;
591         if (!shost)
592                 return;
593         read_lock_irq(&zfcp_data.config_lock);
594         list_for_each_entry(port, &adapter->port_list_head, list)
595                 if (port->rport)
596                         port->rport = NULL;
597         read_unlock_irq(&zfcp_data.config_lock);
598         fc_remove_host(shost);
599         scsi_remove_host(shost);
600         scsi_host_put(shost);
601         adapter->scsi_host = NULL;
602         atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
603
604         return;
605 }
606
607 /*
608  * Support functions for FC transport class
609  */
610 static struct fc_host_statistics*
611 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
612 {
613         struct fc_host_statistics *fc_stats;
614
615         if (!adapter->fc_stats) {
616                 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
617                 if (!fc_stats)
618                         return NULL;
619                 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
620         }
621         memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
622         return adapter->fc_stats;
623 }
624
625 static void
626 zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
627                           struct fsf_qtcb_bottom_port *data,
628                           struct fsf_qtcb_bottom_port *old)
629 {
630         fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
631                 old->seconds_since_last_reset;
632         fc_stats->tx_frames = data->tx_frames - old->tx_frames;
633         fc_stats->tx_words = data->tx_words - old->tx_words;
634         fc_stats->rx_frames = data->rx_frames - old->rx_frames;
635         fc_stats->rx_words = data->rx_words - old->rx_words;
636         fc_stats->lip_count = data->lip - old->lip;
637         fc_stats->nos_count = data->nos - old->nos;
638         fc_stats->error_frames = data->error_frames - old->error_frames;
639         fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
640         fc_stats->link_failure_count = data->link_failure - old->link_failure;
641         fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
642         fc_stats->loss_of_signal_count = data->loss_of_signal -
643                 old->loss_of_signal;
644         fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
645                 old->psp_error_counts;
646         fc_stats->invalid_tx_word_count = data->invalid_tx_words -
647                 old->invalid_tx_words;
648         fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
649         fc_stats->fcp_input_requests = data->input_requests -
650                 old->input_requests;
651         fc_stats->fcp_output_requests = data->output_requests -
652                 old->output_requests;
653         fc_stats->fcp_control_requests = data->control_requests -
654                 old->control_requests;
655         fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
656         fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
657 }
658
659 static void
660 zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
661                        struct fsf_qtcb_bottom_port *data)
662 {
663         fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
664         fc_stats->tx_frames = data->tx_frames;
665         fc_stats->tx_words = data->tx_words;
666         fc_stats->rx_frames = data->rx_frames;
667         fc_stats->rx_words = data->rx_words;
668         fc_stats->lip_count = data->lip;
669         fc_stats->nos_count = data->nos;
670         fc_stats->error_frames = data->error_frames;
671         fc_stats->dumped_frames = data->dumped_frames;
672         fc_stats->link_failure_count = data->link_failure;
673         fc_stats->loss_of_sync_count = data->loss_of_sync;
674         fc_stats->loss_of_signal_count = data->loss_of_signal;
675         fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
676         fc_stats->invalid_tx_word_count = data->invalid_tx_words;
677         fc_stats->invalid_crc_count = data->invalid_crcs;
678         fc_stats->fcp_input_requests = data->input_requests;
679         fc_stats->fcp_output_requests = data->output_requests;
680         fc_stats->fcp_control_requests = data->control_requests;
681         fc_stats->fcp_input_megabytes = data->input_mb;
682         fc_stats->fcp_output_megabytes = data->output_mb;
683 }
684
685 /**
686  * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
687  *
688  * assumption: scsi_transport_fc synchronizes calls of
689  *             get_fc_host_stats and reset_fc_host_stats
690  *             (XXX to be checked otherwise introduce locking)
691  */
692 static struct fc_host_statistics *
693 zfcp_get_fc_host_stats(struct Scsi_Host *shost)
694 {
695         struct zfcp_adapter *adapter;
696         struct fc_host_statistics *fc_stats;
697         struct fsf_qtcb_bottom_port *data;
698         int ret;
699
700         adapter = (struct zfcp_adapter *)shost->hostdata[0];
701         fc_stats = zfcp_init_fc_host_stats(adapter);
702         if (!fc_stats)
703                 return NULL;
704
705         data = kzalloc(sizeof(*data), GFP_KERNEL);
706         if (!data)
707                 return NULL;
708
709         ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
710         if (ret) {
711                 kfree(data);
712                 return NULL; /* XXX return zeroed fc_stats? */
713         }
714
715         if (adapter->stats_reset &&
716             ((jiffies/HZ - adapter->stats_reset) <
717              data->seconds_since_last_reset)) {
718                 zfcp_adjust_fc_host_stats(fc_stats, data,
719                                           adapter->stats_reset_data);
720         } else
721                 zfcp_set_fc_host_stats(fc_stats, data);
722
723         kfree(data);
724         return fc_stats;
725 }
726
727 static void
728 zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
729 {
730         struct zfcp_adapter *adapter;
731         struct fsf_qtcb_bottom_port *data, *old_data;
732         int ret;
733
734         adapter = (struct zfcp_adapter *)shost->hostdata[0];
735         data = kzalloc(sizeof(*data), GFP_KERNEL);
736         if (!data)
737                 return;
738
739         ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
740         if (ret) {
741                 kfree(data);
742         } else {
743                 adapter->stats_reset = jiffies/HZ;
744                 old_data = adapter->stats_reset_data;
745                 adapter->stats_reset_data = data; /* finally freed in
746                                                      adater_dequeue */
747                 kfree(old_data);
748         }
749 }
750
751 static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
752 {
753         rport->dev_loss_tmo = timeout;
754 }
755
756 struct fc_function_template zfcp_transport_functions = {
757         .show_starget_port_id = 1,
758         .show_starget_port_name = 1,
759         .show_starget_node_name = 1,
760         .show_rport_supported_classes = 1,
761         .show_rport_maxframe_size = 1,
762         .show_rport_dev_loss_tmo = 1,
763         .show_host_node_name = 1,
764         .show_host_port_name = 1,
765         .show_host_permanent_port_name = 1,
766         .show_host_supported_classes = 1,
767         .show_host_supported_speeds = 1,
768         .show_host_maxframe_size = 1,
769         .show_host_serial_number = 1,
770         .get_fc_host_stats = zfcp_get_fc_host_stats,
771         .reset_fc_host_stats = zfcp_reset_fc_host_stats,
772         .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
773         /* no functions registered for following dynamic attributes but
774            directly set by LLDD */
775         .show_host_port_type = 1,
776         .show_host_speed = 1,
777         .show_host_port_id = 1,
778         .disable_target_scan = 1,
779 };
780
781 #define ZFCP_DEFINE_LATENCY_ATTR(_name)                                 \
782 static ssize_t                                                          \
783 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev,              \
784                                        struct device_attribute *attr,   \
785                                        char *buf) {                     \
786         struct scsi_device *sdev = to_scsi_device(dev);                 \
787         struct zfcp_unit *unit = sdev->hostdata;                        \
788         struct zfcp_latencies *lat = &unit->latencies;                  \
789         struct zfcp_adapter *adapter = unit->port->adapter;             \
790         unsigned long flags;                                            \
791         unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc;      \
792                                                                         \
793         spin_lock_irqsave(&lat->lock, flags);                           \
794         fsum = lat->_name.fabric.sum * adapter->timer_ticks;            \
795         fmin = lat->_name.fabric.min * adapter->timer_ticks;            \
796         fmax = lat->_name.fabric.max * adapter->timer_ticks;            \
797         csum = lat->_name.channel.sum * adapter->timer_ticks;           \
798         cmin = lat->_name.channel.min * adapter->timer_ticks;           \
799         cmax = lat->_name.channel.max * adapter->timer_ticks;           \
800         cc  = lat->_name.counter;                                       \
801         spin_unlock_irqrestore(&lat->lock, flags);                      \
802                                                                         \
803         do_div(fsum, 1000);                                             \
804         do_div(fmin, 1000);                                             \
805         do_div(fmax, 1000);                                             \
806         do_div(csum, 1000);                                             \
807         do_div(cmin, 1000);                                             \
808         do_div(cmax, 1000);                                             \
809                                                                         \
810         return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n",     \
811                        fmin, fmax, fsum, cmin, cmax, csum, cc);         \
812 }                                                                       \
813 static ssize_t                                                          \
814 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev,             \
815                                         struct device_attribute *attr,  \
816                                         const char *buf, size_t count)  \
817 {                                                                       \
818         struct scsi_device *sdev = to_scsi_device(dev);                 \
819         struct zfcp_unit *unit = sdev->hostdata;                        \
820         struct zfcp_latencies *lat = &unit->latencies;                  \
821         unsigned long flags;                                            \
822                                                                         \
823         spin_lock_irqsave(&lat->lock, flags);                           \
824         lat->_name.fabric.sum = 0;                                      \
825         lat->_name.fabric.min = 0xFFFFFFFF;                             \
826         lat->_name.fabric.max = 0;                                      \
827         lat->_name.channel.sum = 0;                                     \
828         lat->_name.channel.min = 0xFFFFFFFF;                            \
829         lat->_name.channel.max = 0;                                     \
830         lat->_name.counter = 0;                                         \
831         spin_unlock_irqrestore(&lat->lock, flags);                      \
832                                                                         \
833         return (ssize_t) count;                                         \
834 }                                                                       \
835 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO,                  \
836                    zfcp_sysfs_unit_##_name##_latency_show,              \
837                    zfcp_sysfs_unit_##_name##_latency_store);
838
839 ZFCP_DEFINE_LATENCY_ATTR(read);
840 ZFCP_DEFINE_LATENCY_ATTR(write);
841 ZFCP_DEFINE_LATENCY_ATTR(cmd);
842
843 /**
844  * ZFCP_DEFINE_SCSI_ATTR
845  * @_name:   name of show attribute
846  * @_format: format string
847  * @_value:  value to print
848  *
849  * Generates attribute for a unit.
850  */
851 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)                    \
852 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr,        \
853                                               char *buf)                 \
854 {                                                                        \
855         struct scsi_device *sdev;                                        \
856         struct zfcp_unit *unit;                                          \
857                                                                          \
858         sdev = to_scsi_device(dev);                                      \
859         unit = sdev->hostdata;                                           \
860         return sprintf(buf, _format, _value);                            \
861 }                                                                        \
862                                                                          \
863 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
864
865 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
866 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
867 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
868
869 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
870         &dev_attr_fcp_lun,
871         &dev_attr_wwpn,
872         &dev_attr_hba_id,
873         &dev_attr_read_latency,
874         &dev_attr_write_latency,
875         &dev_attr_cmd_latency,
876         NULL
877 };
878
879 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
880                                             struct device_attribute *attr,
881                                             char *buf)
882 {
883         struct Scsi_Host *scsi_host = dev_to_shost(dev);
884         struct fsf_qtcb_bottom_port *qtcb_port;
885         int retval;
886         struct zfcp_adapter *adapter;
887
888         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
889         if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
890                 return -EOPNOTSUPP;
891
892         qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
893         if (!qtcb_port)
894                 return -ENOMEM;
895
896         retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
897         if (!retval)
898                 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
899                                  qtcb_port->cb_util, qtcb_port->a_util);
900         kfree(qtcb_port);
901         return retval;
902 }
903
904 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
905                                         struct fsf_statistics_info *stat_inf)
906 {
907         int retval;
908         struct fsf_qtcb_bottom_config *qtcb_config;
909         struct Scsi_Host *scsi_host = dev_to_shost(dev);
910         struct zfcp_adapter *adapter;
911
912         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
913         if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
914                 return -EOPNOTSUPP;
915
916         qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
917                                GFP_KERNEL);
918         if (!qtcb_config)
919                 return -ENOMEM;
920
921         retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
922         if (!retval)
923                 *stat_inf = qtcb_config->stat_info;
924
925         kfree(qtcb_config);
926         return retval;
927 }
928
929 static ssize_t zfcp_sysfs_adapter_request_show(struct device *dev,
930                                                struct device_attribute *attr,
931                                                char *buf)
932 {
933         struct fsf_statistics_info stat_info;
934         int retval;
935
936         retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
937         if (retval)
938                 return retval;
939
940         return sprintf(buf, "%llu %llu %llu\n",
941                        (unsigned long long) stat_info.input_req,
942                        (unsigned long long) stat_info.output_req,
943                        (unsigned long long) stat_info.control_req);
944 }
945
946 static ssize_t zfcp_sysfs_adapter_mb_show(struct device *dev,
947                                           struct device_attribute *attr,
948                                           char *buf)
949 {
950         struct fsf_statistics_info stat_info;
951         int retval;
952
953         retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
954         if (retval)
955                 return retval;
956
957         return sprintf(buf, "%llu %llu\n",
958                        (unsigned long long) stat_info.input_mb,
959                        (unsigned long long) stat_info.output_mb);
960 }
961
962 static ssize_t zfcp_sysfs_adapter_sec_active_show(struct device *dev,
963                                                   struct device_attribute *attr,
964                                                   char *buf)
965 {
966         struct fsf_statistics_info stat_info;
967         int retval;
968
969         retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
970         if (retval)
971                 return retval;
972
973         return sprintf(buf, "%llu\n",
974                        (unsigned long long) stat_info.seconds_act);
975 }
976
977 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
978 static DEVICE_ATTR(requests, S_IRUGO, zfcp_sysfs_adapter_request_show, NULL);
979 static DEVICE_ATTR(megabytes, S_IRUGO, zfcp_sysfs_adapter_mb_show, NULL);
980 static DEVICE_ATTR(seconds_active, S_IRUGO,
981                    zfcp_sysfs_adapter_sec_active_show, NULL);
982
983 static struct device_attribute *zfcp_a_stats_attrs[] = {
984         &dev_attr_utilization,
985         &dev_attr_requests,
986         &dev_attr_megabytes,
987         &dev_attr_seconds_active,
988         NULL
989 };
990
991 #undef ZFCP_LOG_AREA