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 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
113 struct zfcp_adapter *adapter;
114 struct zfcp_unit *unit;
118 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
122 read_lock_irqsave(&zfcp_data.config_lock, flags);
123 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
124 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
126 sdp->hostdata = unit;
131 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
137 * zfcp_scsi_slave_destroy - called when scsi device is removed
139 * Remove reference to associated scsi device for an zfcp_unit.
140 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
141 * or a scan for this device might have failed.
143 static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
145 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
148 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
149 sdpnt->hostdata = NULL;
151 zfcp_erp_unit_failed(unit, 12, NULL);
157 * called from scsi midlayer to allow finetuning of a device.
160 zfcp_scsi_slave_configure(struct scsi_device *sdp)
162 if (sdp->tagged_supported)
163 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
165 scsi_adjust_queue_depth(sdp, 0, 1);
170 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
171 * @scpnt: pointer to struct scsi_cmnd where result is set
172 * @result: result to be set in scpnt (e.g. DID_ERROR)
175 zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
177 set_host_byte(scpnt, result);
178 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
179 zfcp_scsi_dbf_event_result("fail", 4,
180 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
182 /* return directly */
183 scpnt->scsi_done(scpnt);
187 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
188 * zfcp_scsi_command_sync
189 * @adapter: adapter where scsi command is issued
190 * @unit: unit to which scsi command is sent
191 * @scpnt: scsi command to be sent
192 * @timer: timer to be started if request is successfully initiated
194 * Note: In scsi_done function must be set in scpnt.
197 zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
198 struct scsi_cmnd *scpnt, int use_timer)
205 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
206 BUG_ON(scpnt->scsi_done == NULL);
208 if (unlikely(NULL == unit)) {
209 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
214 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
215 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
216 zfcp_scsi_command_fail(scpnt, DID_ERROR);
220 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
221 ZFCP_REQ_AUTO_CLEANUP);
222 if (unlikely(tmp == -EBUSY)) {
223 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
227 if (unlikely(tmp < 0))
228 retval = SCSI_MLQUEUE_HOST_BUSY;
235 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
237 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
243 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
244 * @unit: unit where command is sent to
245 * @scpnt: scsi command to be sent
246 * @use_timer: indicates whether timer should be setup or not
249 * Errors are indicated in scpnt->result
252 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
256 DECLARE_COMPLETION_ONSTACK(wait);
258 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
259 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
260 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
263 wait_for_completion(&wait);
265 scpnt->SCp.ptr = NULL;
271 * function: zfcp_scsi_queuecommand
273 * purpose: enqueues a SCSI command to the specified target device
275 * returns: 0 - success, SCSI command enqueued
279 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
280 void (*done) (struct scsi_cmnd *))
282 struct zfcp_unit *unit;
283 struct zfcp_adapter *adapter;
285 /* reset the status for this request */
287 scpnt->host_scribble = NULL;
288 scpnt->scsi_done = done;
291 * figure out adapter and target device
292 * (stored there by zfcp_scsi_slave_alloc)
294 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
295 unit = (struct zfcp_unit *) scpnt->device->hostdata;
297 return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
300 static struct zfcp_unit *
301 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
304 struct zfcp_port *port;
305 struct zfcp_unit *unit, *retval = NULL;
307 list_for_each_entry(port, &adapter->port_list_head, list) {
308 if (!port->rport || (id != port->rport->scsi_target_id))
310 list_for_each_entry(unit, &port->unit_list_head, list)
311 if (lun == unit->scsi_lun) {
321 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
322 * @scpnt: pointer to scsi_cmnd to be aborted
323 * Return: SUCCESS - command has been aborted and cleaned up in internal
324 * bookkeeping, SCSI stack won't be called for aborted command
327 * We do not need to care for a SCSI command which completes normally
328 * but late during this abort routine runs. We are allowed to return
329 * late commands to the SCSI stack. It tracks the state of commands and
330 * will handle late commands. (Usually, the normal completion of late
331 * commands is ignored with respect to the running abort operation.)
333 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
335 struct Scsi_Host *scsi_host;
336 struct zfcp_adapter *adapter;
337 struct zfcp_unit *unit;
338 struct zfcp_fsf_req *fsf_req;
340 unsigned long old_req_id;
341 int retval = SUCCESS;
343 scsi_host = scpnt->device->host;
344 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
345 unit = (struct zfcp_unit *) scpnt->device->hostdata;
347 /* avoid race condition between late normal completion and abort */
348 write_lock_irqsave(&adapter->abort_lock, flags);
350 /* Check whether corresponding fsf_req is still pending */
351 spin_lock(&adapter->req_list_lock);
352 fsf_req = zfcp_reqlist_find(adapter,
353 (unsigned long) scpnt->host_scribble);
354 spin_unlock(&adapter->req_list_lock);
356 write_unlock_irqrestore(&adapter->abort_lock, flags);
357 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
362 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
363 old_req_id = fsf_req->req_id;
365 /* don't access old fsf_req after releasing the abort_lock */
366 write_unlock_irqrestore(&adapter->abort_lock, flags);
368 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
370 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
376 __wait_event(fsf_req->completion_wq,
377 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
379 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
380 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
382 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
383 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
386 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
389 zfcp_fsf_req_free(fsf_req);
394 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
397 struct zfcp_unit *unit = scpnt->device->hostdata;
403 retval = zfcp_task_management_function(unit,
404 FCP_LOGICAL_UNIT_RESET,
406 return retval ? FAILED : SUCCESS;
409 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
412 struct zfcp_unit *unit = scpnt->device->hostdata;
418 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
419 return retval ? FAILED : SUCCESS;
423 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
424 struct scsi_cmnd *scpnt)
426 struct zfcp_adapter *adapter = unit->port->adapter;
427 struct zfcp_fsf_req *fsf_req;
430 /* issue task management function */
431 fsf_req = zfcp_fsf_send_fcp_command_task_management
432 (adapter, unit, tm_flags, 0);
434 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
439 __wait_event(fsf_req->completion_wq,
440 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
443 * check completion status of task management function
445 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
446 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
448 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
449 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
452 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
454 zfcp_fsf_req_free(fsf_req);
460 * zfcp_scsi_eh_host_reset_handler - handler for host reset
462 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
464 struct zfcp_unit *unit;
465 struct zfcp_adapter *adapter;
467 unit = (struct zfcp_unit*) scpnt->device->hostdata;
468 adapter = unit->port->adapter;
469 zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
470 zfcp_erp_wait(adapter);
476 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
479 static unsigned int unique_id = 0;
481 if (adapter->scsi_host)
484 /* register adapter as SCSI host with mid layer of SCSI stack */
485 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
486 sizeof (struct zfcp_adapter *));
487 if (!adapter->scsi_host) {
488 dev_err(&adapter->ccw_device->dev,
489 "registration with SCSI stack failed.");
494 /* tell the SCSI stack some characteristics of this adapter */
495 adapter->scsi_host->max_id = 1;
496 adapter->scsi_host->max_lun = 1;
497 adapter->scsi_host->max_channel = 0;
498 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
499 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
500 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
503 * save a pointer to our own adapter data structure within
504 * hostdata field of SCSI host data structure
506 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
508 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
509 scsi_host_put(adapter->scsi_host);
513 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
519 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
521 struct Scsi_Host *shost;
522 struct zfcp_port *port;
524 shost = adapter->scsi_host;
527 read_lock_irq(&zfcp_data.config_lock);
528 list_for_each_entry(port, &adapter->port_list_head, list)
531 read_unlock_irq(&zfcp_data.config_lock);
532 fc_remove_host(shost);
533 scsi_remove_host(shost);
534 scsi_host_put(shost);
535 adapter->scsi_host = NULL;
536 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
542 * Support functions for FC transport class
544 static struct fc_host_statistics*
545 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
547 struct fc_host_statistics *fc_stats;
549 if (!adapter->fc_stats) {
550 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
553 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
555 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
556 return adapter->fc_stats;
560 zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
561 struct fsf_qtcb_bottom_port *data,
562 struct fsf_qtcb_bottom_port *old)
564 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
565 old->seconds_since_last_reset;
566 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
567 fc_stats->tx_words = data->tx_words - old->tx_words;
568 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
569 fc_stats->rx_words = data->rx_words - old->rx_words;
570 fc_stats->lip_count = data->lip - old->lip;
571 fc_stats->nos_count = data->nos - old->nos;
572 fc_stats->error_frames = data->error_frames - old->error_frames;
573 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
574 fc_stats->link_failure_count = data->link_failure - old->link_failure;
575 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
576 fc_stats->loss_of_signal_count = data->loss_of_signal -
578 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
579 old->psp_error_counts;
580 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
581 old->invalid_tx_words;
582 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
583 fc_stats->fcp_input_requests = data->input_requests -
585 fc_stats->fcp_output_requests = data->output_requests -
586 old->output_requests;
587 fc_stats->fcp_control_requests = data->control_requests -
588 old->control_requests;
589 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
590 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
594 zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
595 struct fsf_qtcb_bottom_port *data)
597 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
598 fc_stats->tx_frames = data->tx_frames;
599 fc_stats->tx_words = data->tx_words;
600 fc_stats->rx_frames = data->rx_frames;
601 fc_stats->rx_words = data->rx_words;
602 fc_stats->lip_count = data->lip;
603 fc_stats->nos_count = data->nos;
604 fc_stats->error_frames = data->error_frames;
605 fc_stats->dumped_frames = data->dumped_frames;
606 fc_stats->link_failure_count = data->link_failure;
607 fc_stats->loss_of_sync_count = data->loss_of_sync;
608 fc_stats->loss_of_signal_count = data->loss_of_signal;
609 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
610 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
611 fc_stats->invalid_crc_count = data->invalid_crcs;
612 fc_stats->fcp_input_requests = data->input_requests;
613 fc_stats->fcp_output_requests = data->output_requests;
614 fc_stats->fcp_control_requests = data->control_requests;
615 fc_stats->fcp_input_megabytes = data->input_mb;
616 fc_stats->fcp_output_megabytes = data->output_mb;
620 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
622 * assumption: scsi_transport_fc synchronizes calls of
623 * get_fc_host_stats and reset_fc_host_stats
624 * (XXX to be checked otherwise introduce locking)
626 static struct fc_host_statistics *
627 zfcp_get_fc_host_stats(struct Scsi_Host *shost)
629 struct zfcp_adapter *adapter;
630 struct fc_host_statistics *fc_stats;
631 struct fsf_qtcb_bottom_port *data;
634 adapter = (struct zfcp_adapter *)shost->hostdata[0];
635 fc_stats = zfcp_init_fc_host_stats(adapter);
639 data = kzalloc(sizeof(*data), GFP_KERNEL);
643 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
646 return NULL; /* XXX return zeroed fc_stats? */
649 if (adapter->stats_reset &&
650 ((jiffies/HZ - adapter->stats_reset) <
651 data->seconds_since_last_reset)) {
652 zfcp_adjust_fc_host_stats(fc_stats, data,
653 adapter->stats_reset_data);
655 zfcp_set_fc_host_stats(fc_stats, data);
662 zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
664 struct zfcp_adapter *adapter;
665 struct fsf_qtcb_bottom_port *data, *old_data;
668 adapter = (struct zfcp_adapter *)shost->hostdata[0];
669 data = kzalloc(sizeof(*data), GFP_KERNEL);
673 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
677 adapter->stats_reset = jiffies/HZ;
678 old_data = adapter->stats_reset_data;
679 adapter->stats_reset_data = data; /* finally freed in
685 static void zfcp_get_host_port_state(struct Scsi_Host *shost)
687 struct zfcp_adapter *adapter =
688 (struct zfcp_adapter *)shost->hostdata[0];
689 int status = atomic_read(&adapter->status);
691 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
692 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
693 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
694 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
695 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
696 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
697 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
699 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
702 static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
704 rport->dev_loss_tmo = timeout;
707 struct fc_function_template zfcp_transport_functions = {
708 .show_starget_port_id = 1,
709 .show_starget_port_name = 1,
710 .show_starget_node_name = 1,
711 .show_rport_supported_classes = 1,
712 .show_rport_maxframe_size = 1,
713 .show_rport_dev_loss_tmo = 1,
714 .show_host_node_name = 1,
715 .show_host_port_name = 1,
716 .show_host_permanent_port_name = 1,
717 .show_host_supported_classes = 1,
718 .show_host_supported_speeds = 1,
719 .show_host_maxframe_size = 1,
720 .show_host_serial_number = 1,
721 .get_fc_host_stats = zfcp_get_fc_host_stats,
722 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
723 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
724 .get_host_port_state = zfcp_get_host_port_state,
725 .show_host_port_state = 1,
726 /* no functions registered for following dynamic attributes but
727 directly set by LLDD */
728 .show_host_port_type = 1,
729 .show_host_speed = 1,
730 .show_host_port_id = 1,
731 .disable_target_scan = 1,
734 #define ZFCP_DEFINE_LATENCY_ATTR(_name) \
736 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev, \
737 struct device_attribute *attr, \
739 struct scsi_device *sdev = to_scsi_device(dev); \
740 struct zfcp_unit *unit = sdev->hostdata; \
741 struct zfcp_latencies *lat = &unit->latencies; \
742 struct zfcp_adapter *adapter = unit->port->adapter; \
743 unsigned long flags; \
744 unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc; \
746 spin_lock_irqsave(&lat->lock, flags); \
747 fsum = lat->_name.fabric.sum * adapter->timer_ticks; \
748 fmin = lat->_name.fabric.min * adapter->timer_ticks; \
749 fmax = lat->_name.fabric.max * adapter->timer_ticks; \
750 csum = lat->_name.channel.sum * adapter->timer_ticks; \
751 cmin = lat->_name.channel.min * adapter->timer_ticks; \
752 cmax = lat->_name.channel.max * adapter->timer_ticks; \
753 cc = lat->_name.counter; \
754 spin_unlock_irqrestore(&lat->lock, flags); \
756 do_div(fsum, 1000); \
757 do_div(fmin, 1000); \
758 do_div(fmax, 1000); \
759 do_div(csum, 1000); \
760 do_div(cmin, 1000); \
761 do_div(cmax, 1000); \
763 return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n", \
764 fmin, fmax, fsum, cmin, cmax, csum, cc); \
767 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev, \
768 struct device_attribute *attr, \
769 const char *buf, size_t count) \
771 struct scsi_device *sdev = to_scsi_device(dev); \
772 struct zfcp_unit *unit = sdev->hostdata; \
773 struct zfcp_latencies *lat = &unit->latencies; \
774 unsigned long flags; \
776 spin_lock_irqsave(&lat->lock, flags); \
777 lat->_name.fabric.sum = 0; \
778 lat->_name.fabric.min = 0xFFFFFFFF; \
779 lat->_name.fabric.max = 0; \
780 lat->_name.channel.sum = 0; \
781 lat->_name.channel.min = 0xFFFFFFFF; \
782 lat->_name.channel.max = 0; \
783 lat->_name.counter = 0; \
784 spin_unlock_irqrestore(&lat->lock, flags); \
786 return (ssize_t) count; \
788 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO, \
789 zfcp_sysfs_unit_##_name##_latency_show, \
790 zfcp_sysfs_unit_##_name##_latency_store);
792 ZFCP_DEFINE_LATENCY_ATTR(read);
793 ZFCP_DEFINE_LATENCY_ATTR(write);
794 ZFCP_DEFINE_LATENCY_ATTR(cmd);
797 * ZFCP_DEFINE_SCSI_ATTR
798 * @_name: name of show attribute
799 * @_format: format string
800 * @_value: value to print
802 * Generates attribute for a unit.
804 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
805 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
808 struct scsi_device *sdev; \
809 struct zfcp_unit *unit; \
811 sdev = to_scsi_device(dev); \
812 unit = sdev->hostdata; \
813 return sprintf(buf, _format, _value); \
816 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
818 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
819 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
820 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
822 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
826 &dev_attr_read_latency,
827 &dev_attr_write_latency,
828 &dev_attr_cmd_latency,
832 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
833 struct device_attribute *attr,
836 struct Scsi_Host *scsi_host = dev_to_shost(dev);
837 struct fsf_qtcb_bottom_port *qtcb_port;
839 struct zfcp_adapter *adapter;
841 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
842 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
845 qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
849 retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
851 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
852 qtcb_port->cb_util, qtcb_port->a_util);
857 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
858 struct fsf_statistics_info *stat_inf)
861 struct fsf_qtcb_bottom_config *qtcb_config;
862 struct Scsi_Host *scsi_host = dev_to_shost(dev);
863 struct zfcp_adapter *adapter;
865 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
866 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
869 qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
874 retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
876 *stat_inf = qtcb_config->stat_info;
882 static ssize_t zfcp_sysfs_adapter_request_show(struct device *dev,
883 struct device_attribute *attr,
886 struct fsf_statistics_info stat_info;
889 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
893 return sprintf(buf, "%llu %llu %llu\n",
894 (unsigned long long) stat_info.input_req,
895 (unsigned long long) stat_info.output_req,
896 (unsigned long long) stat_info.control_req);
899 static ssize_t zfcp_sysfs_adapter_mb_show(struct device *dev,
900 struct device_attribute *attr,
903 struct fsf_statistics_info stat_info;
906 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
910 return sprintf(buf, "%llu %llu\n",
911 (unsigned long long) stat_info.input_mb,
912 (unsigned long long) stat_info.output_mb);
915 static ssize_t zfcp_sysfs_adapter_sec_active_show(struct device *dev,
916 struct device_attribute *attr,
919 struct fsf_statistics_info stat_info;
922 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
926 return sprintf(buf, "%llu\n",
927 (unsigned long long) stat_info.seconds_act);
930 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
931 static DEVICE_ATTR(requests, S_IRUGO, zfcp_sysfs_adapter_request_show, NULL);
932 static DEVICE_ATTR(megabytes, S_IRUGO, zfcp_sysfs_adapter_mb_show, NULL);
933 static DEVICE_ATTR(seconds_active, S_IRUGO,
934 zfcp_sysfs_adapter_sec_active_show, NULL);
936 static struct device_attribute *zfcp_a_stats_attrs[] = {
937 &dev_attr_utilization,
940 &dev_attr_seconds_active,