[SCSI] zfcp: Move status accessors from zfcp to SCSI include file.
[linux-2.6/kvm.git] / drivers / s390 / scsi / zfcp_scsi.c
blob160307382d2c2c9a7f46acef22cef48311669aae
1 /*
2 * zfcp device driver
4 * Interface to Linux SCSI midlayer.
6 * Copyright IBM Corporation 2002, 2008
7 */
9 #include "zfcp_ext.h"
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,
22 struct scsi_cmnd *);
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 = {
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,
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)
58 char *fcp_rsp_info_ptr;
60 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*/
67 char *
68 zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
70 char *fcp_sns_info_ptr;
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;
78 return fcp_sns_info_ptr;
81 static fcp_dl_t *
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
95 return fcp_dl_addr;
98 fcp_dl_t
99 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
101 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
104 void
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;
110 static int
111 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
113 struct zfcp_adapter *adapter;
114 struct zfcp_unit *unit;
115 unsigned long flags;
116 int retval = -ENXIO;
118 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
119 if (!adapter)
120 goto out;
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,
125 &unit->status)) {
126 sdp->hostdata = unit;
127 unit->device = sdp;
128 zfcp_unit_get(unit);
129 retval = 0;
131 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
132 out:
133 return retval;
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;
146 WARN_ON(!unit);
147 if (unit) {
148 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
149 sdpnt->hostdata = NULL;
150 unit->device = NULL;
151 zfcp_erp_unit_failed(unit, 12, NULL);
152 zfcp_unit_put(unit);
157 * called from scsi midlayer to allow finetuning of a device.
159 static int
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);
164 else
165 scsi_adjust_queue_depth(sdp, 0, 1);
166 return 0;
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)
174 static void
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],
181 scpnt, NULL);
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)
200 int tmp;
201 int retval;
203 retval = 0;
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);
210 goto out;
213 if (unlikely(
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);
217 goto out;
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);
224 goto out;
227 if (unlikely(tmp < 0))
228 retval = SCSI_MLQUEUE_HOST_BUSY;
230 out:
231 return retval;
234 static void
235 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
237 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
238 complete(wait);
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
247 * Return: 0
249 * Errors are indicated in scpnt->result
252 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
253 int use_timer)
255 int ret;
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,
261 use_timer);
262 if (ret == 0)
263 wait_for_completion(&wait);
265 scpnt->SCp.ptr = NULL;
267 return 0;
271 * function: zfcp_scsi_queuecommand
273 * purpose: enqueues a SCSI command to the specified target device
275 * returns: 0 - success, SCSI command enqueued
276 * !0 - failure
278 static int
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 */
286 scpnt->result = 0;
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,
302 unsigned int lun)
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))
309 continue;
310 list_for_each_entry(unit, &port->unit_list_head, list)
311 if (lun == unit->scsi_lun) {
312 retval = unit;
313 goto out;
316 out:
317 return retval;
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
325 * FAILED - otherwise
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;
339 unsigned long flags;
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);
355 if (!fsf_req) {
356 write_unlock_irqrestore(&adapter->abort_lock, flags);
357 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
358 retval = SUCCESS;
359 goto out;
361 fsf_req->data = 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);
369 if (!fsf_req) {
370 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
371 old_req_id);
372 retval = FAILED;
373 goto out;
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);
381 retval = SUCCESS;
382 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
383 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
384 retval = SUCCESS;
385 } else {
386 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
387 retval = FAILED;
389 zfcp_fsf_req_free(fsf_req);
390 out:
391 return retval;
394 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
396 int retval;
397 struct zfcp_unit *unit = scpnt->device->hostdata;
399 if (!unit) {
400 WARN_ON(1);
401 return SUCCESS;
403 retval = zfcp_task_management_function(unit,
404 FCP_LOGICAL_UNIT_RESET,
405 scpnt);
406 return retval ? FAILED : SUCCESS;
409 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
411 int retval;
412 struct zfcp_unit *unit = scpnt->device->hostdata;
414 if (!unit) {
415 WARN_ON(1);
416 return SUCCESS;
418 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
419 return retval ? FAILED : SUCCESS;
422 static int
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;
428 int retval = 0;
430 /* issue task management function */
431 fsf_req = zfcp_fsf_send_fcp_command_task_management
432 (adapter, unit, tm_flags, 0);
433 if (!fsf_req) {
434 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
435 retval = -ENOMEM;
436 goto out;
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);
447 retval = -EIO;
448 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
449 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
450 retval = -ENOTSUPP;
451 } else
452 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
454 zfcp_fsf_req_free(fsf_req);
455 out:
456 return retval;
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);
472 return SUCCESS;
476 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
478 int retval = 0;
479 static unsigned int unique_id = 0;
481 if (adapter->scsi_host)
482 goto out;
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.");
490 retval = -EIO;
491 goto out;
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);
510 retval = -EIO;
511 goto out;
513 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
514 out:
515 return retval;
518 void
519 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
521 struct Scsi_Host *shost;
522 struct zfcp_port *port;
524 shost = adapter->scsi_host;
525 if (!shost)
526 return;
527 read_lock_irq(&zfcp_data.config_lock);
528 list_for_each_entry(port, &adapter->port_list_head, list)
529 if (port->rport)
530 port->rport = NULL;
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);
538 return;
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);
551 if (!fc_stats)
552 return NULL;
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;
559 static void
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 -
577 old->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 -
584 old->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;
593 static void
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;
632 int ret;
634 adapter = (struct zfcp_adapter *)shost->hostdata[0];
635 fc_stats = zfcp_init_fc_host_stats(adapter);
636 if (!fc_stats)
637 return NULL;
639 data = kzalloc(sizeof(*data), GFP_KERNEL);
640 if (!data)
641 return NULL;
643 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
644 if (ret) {
645 kfree(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);
654 } else
655 zfcp_set_fc_host_stats(fc_stats, data);
657 kfree(data);
658 return fc_stats;
661 static void
662 zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
664 struct zfcp_adapter *adapter;
665 struct fsf_qtcb_bottom_port *data, *old_data;
666 int ret;
668 adapter = (struct zfcp_adapter *)shost->hostdata[0];
669 data = kzalloc(sizeof(*data), GFP_KERNEL);
670 if (!data)
671 return;
673 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
674 if (ret) {
675 kfree(data);
676 } else {
677 adapter->stats_reset = jiffies/HZ;
678 old_data = adapter->stats_reset_data;
679 adapter->stats_reset_data = data; /* finally freed in
680 adater_dequeue */
681 kfree(old_data);
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;
698 else
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) \
735 static ssize_t \
736 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev, \
737 struct device_attribute *attr, \
738 char *buf) { \
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); \
766 static ssize_t \
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, \
806 char *buf) \
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[] = {
823 &dev_attr_fcp_lun,
824 &dev_attr_wwpn,
825 &dev_attr_hba_id,
826 &dev_attr_read_latency,
827 &dev_attr_write_latency,
828 &dev_attr_cmd_latency,
829 NULL
832 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
833 struct device_attribute *attr,
834 char *buf)
836 struct Scsi_Host *scsi_host = dev_to_shost(dev);
837 struct fsf_qtcb_bottom_port *qtcb_port;
838 int retval;
839 struct zfcp_adapter *adapter;
841 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
842 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
843 return -EOPNOTSUPP;
845 qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
846 if (!qtcb_port)
847 return -ENOMEM;
849 retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
850 if (!retval)
851 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
852 qtcb_port->cb_util, qtcb_port->a_util);
853 kfree(qtcb_port);
854 return retval;
857 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
858 struct fsf_statistics_info *stat_inf)
860 int retval;
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))
867 return -EOPNOTSUPP;
869 qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
870 GFP_KERNEL);
871 if (!qtcb_config)
872 return -ENOMEM;
874 retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
875 if (!retval)
876 *stat_inf = qtcb_config->stat_info;
878 kfree(qtcb_config);
879 return retval;
882 static ssize_t zfcp_sysfs_adapter_request_show(struct device *dev,
883 struct device_attribute *attr,
884 char *buf)
886 struct fsf_statistics_info stat_info;
887 int retval;
889 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
890 if (retval)
891 return retval;
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,
901 char *buf)
903 struct fsf_statistics_info stat_info;
904 int retval;
906 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
907 if (retval)
908 return retval;
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,
917 char *buf)
919 struct fsf_statistics_info stat_info;
920 int retval;
922 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
923 if (retval)
924 return retval;
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,
938 &dev_attr_requests,
939 &dev_attr_megabytes,
940 &dev_attr_seconds_active,
941 NULL