4 * Interface to Linux SCSI midlayer.
6 * Copyright IBM Corporation 2002, 2008
10 #include <asm/atomic.h>
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,
24 static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
25 unsigned int, unsigned int);
27 static struct device_attribute *zfcp_sysfs_sdev_attrs[];
28 static struct device_attribute *zfcp_a_stats_attrs[];
30 struct zfcp_data zfcp_data = {
31 .scsi_host_template = {
33 .module = THIS_MODULE,
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,
45 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
48 .sdev_attrs = zfcp_sysfs_sdev_attrs,
49 .max_sectors = ZFCP_MAX_SECTORS,
50 .shost_attrs = zfcp_a_stats_attrs,
54 /* Find start of Response Information in FCP response unit*/
56 zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
58 char *fcp_rsp_info_ptr;
61 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
63 return fcp_rsp_info_ptr;
66 /* Find start of Sense Information in FCP response unit*/
68 zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
70 char *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;
78 return fcp_sns_info_ptr;
82 zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
84 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
85 fcp_dl_t *fcp_dl_addr;
87 fcp_dl_addr = (fcp_dl_t *)
88 ((unsigned char *) fcp_cmd +
89 sizeof (struct fcp_cmnd_iu) + additional_length);
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
99 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
101 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
105 zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
107 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
111 * note: it's a bit-or operation not an assignment
112 * regarding the specified byte
115 set_byte(int *result, char status, char pos)
117 *result |= status << (pos * 8);
121 set_host_byte(int *result, char status)
123 set_byte(result, status, 2);
127 set_driver_byte(int *result, char status)
129 set_byte(result, status, 3);
133 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
135 struct zfcp_adapter *adapter;
136 struct zfcp_unit *unit;
140 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
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,
148 sdp->hostdata = unit;
153 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
159 * zfcp_scsi_slave_destroy - called when scsi device is removed
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.
165 static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
167 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
170 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
171 sdpnt->hostdata = NULL;
173 zfcp_erp_unit_failed(unit, 12, NULL);
179 * called from scsi midlayer to allow finetuning of a device.
182 zfcp_scsi_slave_configure(struct scsi_device *sdp)
184 if (sdp->tagged_supported)
185 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
187 scsi_adjust_queue_depth(sdp, 0, 1);
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)
197 zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
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],
204 /* return directly */
205 scpnt->scsi_done(scpnt);
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
216 * Note: In scsi_done function must be set in scpnt.
219 zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
220 struct scsi_cmnd *scpnt, int use_timer)
227 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
228 BUG_ON(scpnt->scsi_done == NULL);
230 if (unlikely(NULL == unit)) {
231 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
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);
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);
249 if (unlikely(tmp < 0))
250 retval = SCSI_MLQUEUE_HOST_BUSY;
257 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
259 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
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
271 * Errors are indicated in scpnt->result
274 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
278 DECLARE_COMPLETION_ONSTACK(wait);
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,
285 wait_for_completion(&wait);
287 scpnt->SCp.ptr = NULL;
293 * function: zfcp_scsi_queuecommand
295 * purpose: enqueues a SCSI command to the specified target device
297 * returns: 0 - success, SCSI command enqueued
301 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
302 void (*done) (struct scsi_cmnd *))
304 struct zfcp_unit *unit;
305 struct zfcp_adapter *adapter;
307 /* reset the status for this request */
309 scpnt->host_scribble = NULL;
310 scpnt->scsi_done = done;
313 * figure out adapter and target device
314 * (stored there by zfcp_scsi_slave_alloc)
316 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
317 unit = (struct zfcp_unit *) scpnt->device->hostdata;
319 return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
322 static struct zfcp_unit *
323 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
326 struct zfcp_port *port;
327 struct zfcp_unit *unit, *retval = NULL;
329 list_for_each_entry(port, &adapter->port_list_head, list) {
330 if (!port->rport || (id != port->rport->scsi_target_id))
332 list_for_each_entry(unit, &port->unit_list_head, list)
333 if (lun == unit->scsi_lun) {
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
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.)
355 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
357 struct Scsi_Host *scsi_host;
358 struct zfcp_adapter *adapter;
359 struct zfcp_unit *unit;
360 struct zfcp_fsf_req *fsf_req;
362 unsigned long old_req_id;
363 int retval = SUCCESS;
365 scsi_host = scpnt->device->host;
366 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
367 unit = (struct zfcp_unit *) scpnt->device->hostdata;
369 /* avoid race condition between late normal completion and abort */
370 write_lock_irqsave(&adapter->abort_lock, flags);
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);
378 write_unlock_irqrestore(&adapter->abort_lock, flags);
379 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
384 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
385 old_req_id = fsf_req->req_id;
387 /* don't access old fsf_req after releasing the abort_lock */
388 write_unlock_irqrestore(&adapter->abort_lock, flags);
390 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
392 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
398 __wait_event(fsf_req->completion_wq,
399 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
401 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
402 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
404 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
405 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
408 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
411 zfcp_fsf_req_free(fsf_req);
416 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
419 struct zfcp_unit *unit = scpnt->device->hostdata;
425 retval = zfcp_task_management_function(unit,
426 FCP_LOGICAL_UNIT_RESET,
428 return retval ? FAILED : SUCCESS;
431 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
434 struct zfcp_unit *unit = scpnt->device->hostdata;
440 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
441 return retval ? FAILED : SUCCESS;
445 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
446 struct scsi_cmnd *scpnt)
448 struct zfcp_adapter *adapter = unit->port->adapter;
449 struct zfcp_fsf_req *fsf_req;
452 /* issue task management function */
453 fsf_req = zfcp_fsf_send_fcp_command_task_management
454 (adapter, unit, tm_flags, 0);
456 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
461 __wait_event(fsf_req->completion_wq,
462 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
465 * check completion status of task management function
467 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
468 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
470 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
471 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
474 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
476 zfcp_fsf_req_free(fsf_req);
482 * zfcp_scsi_eh_host_reset_handler - handler for host reset
484 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
486 struct zfcp_unit *unit;
487 struct zfcp_adapter *adapter;
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);
498 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
501 static unsigned int unique_id = 0;
503 if (adapter->scsi_host)
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.");
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;
525 * save a pointer to our own adapter data structure within
526 * hostdata field of SCSI host data structure
528 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
530 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
531 scsi_host_put(adapter->scsi_host);
535 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
541 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
543 struct Scsi_Host *shost;
544 struct zfcp_port *port;
546 shost = adapter->scsi_host;
549 read_lock_irq(&zfcp_data.config_lock);
550 list_for_each_entry(port, &adapter->port_list_head, list)
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);
564 * Support functions for FC transport class
566 static struct fc_host_statistics*
567 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
569 struct fc_host_statistics *fc_stats;
571 if (!adapter->fc_stats) {
572 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
575 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
577 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
578 return adapter->fc_stats;
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)
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 -
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 -
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;
616 zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
617 struct fsf_qtcb_bottom_port *data)
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;
642 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
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)
648 static struct fc_host_statistics *
649 zfcp_get_fc_host_stats(struct Scsi_Host *shost)
651 struct zfcp_adapter *adapter;
652 struct fc_host_statistics *fc_stats;
653 struct fsf_qtcb_bottom_port *data;
656 adapter = (struct zfcp_adapter *)shost->hostdata[0];
657 fc_stats = zfcp_init_fc_host_stats(adapter);
661 data = kzalloc(sizeof(*data), GFP_KERNEL);
665 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
668 return NULL; /* XXX return zeroed fc_stats? */
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);
677 zfcp_set_fc_host_stats(fc_stats, data);
684 zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
686 struct zfcp_adapter *adapter;
687 struct fsf_qtcb_bottom_port *data, *old_data;
690 adapter = (struct zfcp_adapter *)shost->hostdata[0];
691 data = kzalloc(sizeof(*data), GFP_KERNEL);
695 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
699 adapter->stats_reset = jiffies/HZ;
700 old_data = adapter->stats_reset_data;
701 adapter->stats_reset_data = data; /* finally freed in
707 static void zfcp_get_host_port_state(struct Scsi_Host *shost)
709 struct zfcp_adapter *adapter =
710 (struct zfcp_adapter *)shost->hostdata[0];
711 int status = atomic_read(&adapter->status);
713 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
714 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
715 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
716 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
717 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
718 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
719 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
721 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
724 static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
726 rport->dev_loss_tmo = timeout;
729 struct fc_function_template zfcp_transport_functions = {
730 .show_starget_port_id = 1,
731 .show_starget_port_name = 1,
732 .show_starget_node_name = 1,
733 .show_rport_supported_classes = 1,
734 .show_rport_maxframe_size = 1,
735 .show_rport_dev_loss_tmo = 1,
736 .show_host_node_name = 1,
737 .show_host_port_name = 1,
738 .show_host_permanent_port_name = 1,
739 .show_host_supported_classes = 1,
740 .show_host_supported_speeds = 1,
741 .show_host_maxframe_size = 1,
742 .show_host_serial_number = 1,
743 .get_fc_host_stats = zfcp_get_fc_host_stats,
744 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
745 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
746 .get_host_port_state = zfcp_get_host_port_state,
747 .show_host_port_state = 1,
748 /* no functions registered for following dynamic attributes but
749 directly set by LLDD */
750 .show_host_port_type = 1,
751 .show_host_speed = 1,
752 .show_host_port_id = 1,
753 .disable_target_scan = 1,
756 #define ZFCP_DEFINE_LATENCY_ATTR(_name) \
758 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev, \
759 struct device_attribute *attr, \
761 struct scsi_device *sdev = to_scsi_device(dev); \
762 struct zfcp_unit *unit = sdev->hostdata; \
763 struct zfcp_latencies *lat = &unit->latencies; \
764 struct zfcp_adapter *adapter = unit->port->adapter; \
765 unsigned long flags; \
766 unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc; \
768 spin_lock_irqsave(&lat->lock, flags); \
769 fsum = lat->_name.fabric.sum * adapter->timer_ticks; \
770 fmin = lat->_name.fabric.min * adapter->timer_ticks; \
771 fmax = lat->_name.fabric.max * adapter->timer_ticks; \
772 csum = lat->_name.channel.sum * adapter->timer_ticks; \
773 cmin = lat->_name.channel.min * adapter->timer_ticks; \
774 cmax = lat->_name.channel.max * adapter->timer_ticks; \
775 cc = lat->_name.counter; \
776 spin_unlock_irqrestore(&lat->lock, flags); \
778 do_div(fsum, 1000); \
779 do_div(fmin, 1000); \
780 do_div(fmax, 1000); \
781 do_div(csum, 1000); \
782 do_div(cmin, 1000); \
783 do_div(cmax, 1000); \
785 return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n", \
786 fmin, fmax, fsum, cmin, cmax, csum, cc); \
789 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev, \
790 struct device_attribute *attr, \
791 const char *buf, size_t count) \
793 struct scsi_device *sdev = to_scsi_device(dev); \
794 struct zfcp_unit *unit = sdev->hostdata; \
795 struct zfcp_latencies *lat = &unit->latencies; \
796 unsigned long flags; \
798 spin_lock_irqsave(&lat->lock, flags); \
799 lat->_name.fabric.sum = 0; \
800 lat->_name.fabric.min = 0xFFFFFFFF; \
801 lat->_name.fabric.max = 0; \
802 lat->_name.channel.sum = 0; \
803 lat->_name.channel.min = 0xFFFFFFFF; \
804 lat->_name.channel.max = 0; \
805 lat->_name.counter = 0; \
806 spin_unlock_irqrestore(&lat->lock, flags); \
808 return (ssize_t) count; \
810 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO, \
811 zfcp_sysfs_unit_##_name##_latency_show, \
812 zfcp_sysfs_unit_##_name##_latency_store);
814 ZFCP_DEFINE_LATENCY_ATTR(read);
815 ZFCP_DEFINE_LATENCY_ATTR(write);
816 ZFCP_DEFINE_LATENCY_ATTR(cmd);
819 * ZFCP_DEFINE_SCSI_ATTR
820 * @_name: name of show attribute
821 * @_format: format string
822 * @_value: value to print
824 * Generates attribute for a unit.
826 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
827 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
830 struct scsi_device *sdev; \
831 struct zfcp_unit *unit; \
833 sdev = to_scsi_device(dev); \
834 unit = sdev->hostdata; \
835 return sprintf(buf, _format, _value); \
838 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
840 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
841 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
842 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
844 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
848 &dev_attr_read_latency,
849 &dev_attr_write_latency,
850 &dev_attr_cmd_latency,
854 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
855 struct device_attribute *attr,
858 struct Scsi_Host *scsi_host = dev_to_shost(dev);
859 struct fsf_qtcb_bottom_port *qtcb_port;
861 struct zfcp_adapter *adapter;
863 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
864 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
867 qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
871 retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
873 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
874 qtcb_port->cb_util, qtcb_port->a_util);
879 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
880 struct fsf_statistics_info *stat_inf)
883 struct fsf_qtcb_bottom_config *qtcb_config;
884 struct Scsi_Host *scsi_host = dev_to_shost(dev);
885 struct zfcp_adapter *adapter;
887 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
888 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
891 qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
896 retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
898 *stat_inf = qtcb_config->stat_info;
904 static ssize_t zfcp_sysfs_adapter_request_show(struct device *dev,
905 struct device_attribute *attr,
908 struct fsf_statistics_info stat_info;
911 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
915 return sprintf(buf, "%llu %llu %llu\n",
916 (unsigned long long) stat_info.input_req,
917 (unsigned long long) stat_info.output_req,
918 (unsigned long long) stat_info.control_req);
921 static ssize_t zfcp_sysfs_adapter_mb_show(struct device *dev,
922 struct device_attribute *attr,
925 struct fsf_statistics_info stat_info;
928 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
932 return sprintf(buf, "%llu %llu\n",
933 (unsigned long long) stat_info.input_mb,
934 (unsigned long long) stat_info.output_mb);
937 static ssize_t zfcp_sysfs_adapter_sec_active_show(struct device *dev,
938 struct device_attribute *attr,
941 struct fsf_statistics_info stat_info;
944 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
948 return sprintf(buf, "%llu\n",
949 (unsigned long long) stat_info.seconds_act);
952 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
953 static DEVICE_ATTR(requests, S_IRUGO, zfcp_sysfs_adapter_request_show, NULL);
954 static DEVICE_ATTR(megabytes, S_IRUGO, zfcp_sysfs_adapter_mb_show, NULL);
955 static DEVICE_ATTR(seconds_active, S_IRUGO,
956 zfcp_sysfs_adapter_sec_active_show, NULL);
958 static struct device_attribute *zfcp_a_stats_attrs[] = {
959 &dev_attr_utilization,
962 &dev_attr_seconds_active,