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