Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / s390 / scsi / zfcp_scsi.c
blob01687559dc06094fc4f81d89ab6256e2717a56a7
1 /*
2 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
5 * (C) Copyright IBM Corp. 2002, 2006
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
24 #include "zfcp_ext.h"
25 #include <asm/atomic.h>
27 static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
28 static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
29 static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
30 static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
31 void (*done) (struct scsi_cmnd *));
32 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
33 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
34 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *);
35 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
36 static int zfcp_task_management_function(struct zfcp_unit *, u8,
37 struct scsi_cmnd *);
39 static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
40 unsigned int, unsigned int);
42 static struct device_attribute *zfcp_sysfs_sdev_attrs[];
43 static struct device_attribute *zfcp_a_stats_attrs[];
45 struct zfcp_data zfcp_data = {
46 .scsi_host_template = {
47 .name = ZFCP_NAME,
48 .module = THIS_MODULE,
49 .proc_name = "zfcp",
50 .slave_alloc = zfcp_scsi_slave_alloc,
51 .slave_configure = zfcp_scsi_slave_configure,
52 .slave_destroy = zfcp_scsi_slave_destroy,
53 .queuecommand = zfcp_scsi_queuecommand,
54 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
55 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
56 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
57 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
58 .can_queue = 4096,
59 .this_id = -1,
60 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
61 .cmd_per_lun = 1,
62 .use_clustering = 1,
63 .sdev_attrs = zfcp_sysfs_sdev_attrs,
64 .max_sectors = ZFCP_MAX_SECTORS,
65 .shost_attrs = zfcp_a_stats_attrs,
67 .driver_version = ZFCP_VERSION,
70 /* Find start of Response Information in FCP response unit*/
71 char *
72 zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
74 char *fcp_rsp_info_ptr;
76 fcp_rsp_info_ptr =
77 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
79 return fcp_rsp_info_ptr;
82 /* Find start of Sense Information in FCP response unit*/
83 char *
84 zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
86 char *fcp_sns_info_ptr;
88 fcp_sns_info_ptr =
89 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
90 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
91 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
92 fcp_rsp_iu->fcp_rsp_len;
94 return fcp_sns_info_ptr;
97 static fcp_dl_t *
98 zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
100 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
101 fcp_dl_t *fcp_dl_addr;
103 fcp_dl_addr = (fcp_dl_t *)
104 ((unsigned char *) fcp_cmd +
105 sizeof (struct fcp_cmnd_iu) + additional_length);
107 * fcp_dl_addr = start address of fcp_cmnd structure +
108 * size of fixed part + size of dynamically sized add_dcp_cdb field
109 * SEE FCP-2 documentation
111 return fcp_dl_addr;
114 fcp_dl_t
115 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
117 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
120 void
121 zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
123 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
127 * note: it's a bit-or operation not an assignment
128 * regarding the specified byte
130 static inline void
131 set_byte(int *result, char status, char pos)
133 *result |= status << (pos * 8);
136 void
137 set_host_byte(int *result, char status)
139 set_byte(result, status, 2);
142 void
143 set_driver_byte(int *result, char status)
145 set_byte(result, status, 3);
148 static int
149 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
151 struct zfcp_adapter *adapter;
152 struct zfcp_unit *unit;
153 unsigned long flags;
154 int retval = -ENXIO;
156 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
157 if (!adapter)
158 goto out;
160 read_lock_irqsave(&zfcp_data.config_lock, flags);
161 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
162 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
163 &unit->status)) {
164 sdp->hostdata = unit;
165 unit->device = sdp;
166 zfcp_unit_get(unit);
167 retval = 0;
169 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
170 out:
171 return retval;
175 * zfcp_scsi_slave_destroy - called when scsi device is removed
177 * Remove reference to associated scsi device for an zfcp_unit.
178 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
179 * or a scan for this device might have failed.
181 static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
183 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
185 if (unit) {
186 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
187 sdpnt->hostdata = NULL;
188 unit->device = NULL;
189 zfcp_erp_unit_failed(unit, 12, NULL);
190 zfcp_unit_put(unit);
191 } else
192 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
193 "address %p\n", sdpnt);
197 * called from scsi midlayer to allow finetuning of a device.
199 static int
200 zfcp_scsi_slave_configure(struct scsi_device *sdp)
202 if (sdp->tagged_supported)
203 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
204 else
205 scsi_adjust_queue_depth(sdp, 0, 1);
206 return 0;
210 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
211 * @scpnt: pointer to struct scsi_cmnd where result is set
212 * @result: result to be set in scpnt (e.g. DID_ERROR)
214 static void
215 zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
217 set_host_byte(&scpnt->result, result);
218 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
219 zfcp_scsi_dbf_event_result("fail", 4,
220 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
221 scpnt, NULL);
222 /* return directly */
223 scpnt->scsi_done(scpnt);
227 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
228 * zfcp_scsi_command_sync
229 * @adapter: adapter where scsi command is issued
230 * @unit: unit to which scsi command is sent
231 * @scpnt: scsi command to be sent
232 * @timer: timer to be started if request is successfully initiated
234 * Note: In scsi_done function must be set in scpnt.
237 zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
238 struct scsi_cmnd *scpnt, int use_timer)
240 int tmp;
241 int retval;
243 retval = 0;
245 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
246 BUG_ON(scpnt->scsi_done == NULL);
248 if (unlikely(NULL == unit)) {
249 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
250 goto out;
253 if (unlikely(
254 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
255 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
256 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
257 "0x%016Lx on adapter %s\n",
258 unit->fcp_lun, unit->port->wwpn,
259 zfcp_get_busid_by_adapter(adapter));
260 zfcp_scsi_command_fail(scpnt, DID_ERROR);
261 goto out;
264 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
265 ZFCP_REQ_AUTO_CLEANUP);
266 if (unlikely(tmp == -EBUSY)) {
267 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
268 "on port 0x%016Lx in recovery\n",
269 zfcp_get_busid_by_unit(unit),
270 unit->fcp_lun, unit->port->wwpn);
271 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
272 goto out;
275 if (unlikely(tmp < 0)) {
276 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
277 retval = SCSI_MLQUEUE_HOST_BUSY;
280 out:
281 return retval;
284 static void
285 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
287 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
288 complete(wait);
293 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
294 * @unit: unit where command is sent to
295 * @scpnt: scsi command to be sent
296 * @use_timer: indicates whether timer should be setup or not
297 * Return: 0
299 * Errors are indicated in scpnt->result
302 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
303 int use_timer)
305 int ret;
306 DECLARE_COMPLETION_ONSTACK(wait);
308 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
309 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
310 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
311 use_timer);
312 if (ret == 0)
313 wait_for_completion(&wait);
315 scpnt->SCp.ptr = NULL;
317 return 0;
321 * function: zfcp_scsi_queuecommand
323 * purpose: enqueues a SCSI command to the specified target device
325 * returns: 0 - success, SCSI command enqueued
326 * !0 - failure
328 static int
329 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
330 void (*done) (struct scsi_cmnd *))
332 struct zfcp_unit *unit;
333 struct zfcp_adapter *adapter;
335 /* reset the status for this request */
336 scpnt->result = 0;
337 scpnt->host_scribble = NULL;
338 scpnt->scsi_done = done;
341 * figure out adapter and target device
342 * (stored there by zfcp_scsi_slave_alloc)
344 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
345 unit = (struct zfcp_unit *) scpnt->device->hostdata;
347 return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
350 static struct zfcp_unit *
351 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
352 unsigned int lun)
354 struct zfcp_port *port;
355 struct zfcp_unit *unit, *retval = NULL;
357 list_for_each_entry(port, &adapter->port_list_head, list) {
358 if (!port->rport || (id != port->rport->scsi_target_id))
359 continue;
360 list_for_each_entry(unit, &port->unit_list_head, list)
361 if (lun == unit->scsi_lun) {
362 retval = unit;
363 goto out;
366 out:
367 return retval;
371 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
372 * @scpnt: pointer to scsi_cmnd to be aborted
373 * Return: SUCCESS - command has been aborted and cleaned up in internal
374 * bookkeeping, SCSI stack won't be called for aborted command
375 * FAILED - otherwise
377 * We do not need to care for a SCSI command which completes normally
378 * but late during this abort routine runs. We are allowed to return
379 * late commands to the SCSI stack. It tracks the state of commands and
380 * will handle late commands. (Usually, the normal completion of late
381 * commands is ignored with respect to the running abort operation.)
383 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
385 struct Scsi_Host *scsi_host;
386 struct zfcp_adapter *adapter;
387 struct zfcp_unit *unit;
388 struct zfcp_fsf_req *fsf_req;
389 unsigned long flags;
390 unsigned long old_req_id;
391 int retval = SUCCESS;
393 scsi_host = scpnt->device->host;
394 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
395 unit = (struct zfcp_unit *) scpnt->device->hostdata;
397 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
398 scpnt, zfcp_get_busid_by_adapter(adapter));
400 /* avoid race condition between late normal completion and abort */
401 write_lock_irqsave(&adapter->abort_lock, flags);
403 /* Check whether corresponding fsf_req is still pending */
404 spin_lock(&adapter->req_list_lock);
405 fsf_req = zfcp_reqlist_find(adapter,
406 (unsigned long) scpnt->host_scribble);
407 spin_unlock(&adapter->req_list_lock);
408 if (!fsf_req) {
409 write_unlock_irqrestore(&adapter->abort_lock, flags);
410 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
411 retval = SUCCESS;
412 goto out;
414 fsf_req->data = 0;
415 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
416 old_req_id = fsf_req->req_id;
418 /* don't access old fsf_req after releasing the abort_lock */
419 write_unlock_irqrestore(&adapter->abort_lock, flags);
421 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
422 if (!fsf_req) {
423 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
424 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
425 old_req_id);
426 retval = FAILED;
427 goto out;
430 __wait_event(fsf_req->completion_wq,
431 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
433 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
434 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
435 retval = SUCCESS;
436 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
437 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
438 retval = SUCCESS;
439 } else {
440 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
441 retval = FAILED;
443 zfcp_fsf_req_free(fsf_req);
444 out:
445 return retval;
448 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
450 int retval;
451 struct zfcp_unit *unit = scpnt->device->hostdata;
453 if (!unit) {
454 WARN_ON(1);
455 return SUCCESS;
457 retval = zfcp_task_management_function(unit,
458 FCP_LOGICAL_UNIT_RESET,
459 scpnt);
460 return retval ? FAILED : SUCCESS;
463 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
465 int retval;
466 struct zfcp_unit *unit = scpnt->device->hostdata;
468 if (!unit) {
469 WARN_ON(1);
470 return SUCCESS;
472 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
473 return retval ? FAILED : SUCCESS;
476 static int
477 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
478 struct scsi_cmnd *scpnt)
480 struct zfcp_adapter *adapter = unit->port->adapter;
481 struct zfcp_fsf_req *fsf_req;
482 int retval = 0;
484 /* issue task management function */
485 fsf_req = zfcp_fsf_send_fcp_command_task_management
486 (adapter, unit, tm_flags, 0);
487 if (!fsf_req) {
488 ZFCP_LOG_INFO("error: creation of task management request "
489 "failed for unit 0x%016Lx on port 0x%016Lx on "
490 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
491 zfcp_get_busid_by_adapter(adapter));
492 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
493 retval = -ENOMEM;
494 goto out;
497 __wait_event(fsf_req->completion_wq,
498 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
501 * check completion status of task management function
503 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
504 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
505 retval = -EIO;
506 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
507 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
508 retval = -ENOTSUPP;
509 } else
510 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
512 zfcp_fsf_req_free(fsf_req);
513 out:
514 return retval;
518 * zfcp_scsi_eh_host_reset_handler - handler for host reset
520 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
522 struct zfcp_unit *unit;
523 struct zfcp_adapter *adapter;
525 unit = (struct zfcp_unit*) scpnt->device->hostdata;
526 adapter = unit->port->adapter;
528 ZFCP_LOG_NORMAL("host reset because of problems with "
529 "unit 0x%016Lx on port 0x%016Lx, adapter %s\n",
530 unit->fcp_lun, unit->port->wwpn,
531 zfcp_get_busid_by_adapter(unit->port->adapter));
533 zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
534 zfcp_erp_wait(adapter);
536 return SUCCESS;
540 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
542 int retval = 0;
543 static unsigned int unique_id = 0;
545 if (adapter->scsi_host)
546 goto out;
548 /* register adapter as SCSI host with mid layer of SCSI stack */
549 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
550 sizeof (struct zfcp_adapter *));
551 if (!adapter->scsi_host) {
552 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
553 "for adapter %s ",
554 zfcp_get_busid_by_adapter(adapter));
555 retval = -EIO;
556 goto out;
558 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
560 /* tell the SCSI stack some characteristics of this adapter */
561 adapter->scsi_host->max_id = 1;
562 adapter->scsi_host->max_lun = 1;
563 adapter->scsi_host->max_channel = 0;
564 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
565 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
566 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
569 * save a pointer to our own adapter data structure within
570 * hostdata field of SCSI host data structure
572 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
574 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
575 scsi_host_put(adapter->scsi_host);
576 retval = -EIO;
577 goto out;
579 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
580 out:
581 return retval;
584 void
585 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
587 struct Scsi_Host *shost;
588 struct zfcp_port *port;
590 shost = adapter->scsi_host;
591 if (!shost)
592 return;
593 read_lock_irq(&zfcp_data.config_lock);
594 list_for_each_entry(port, &adapter->port_list_head, list)
595 if (port->rport)
596 port->rport = NULL;
597 read_unlock_irq(&zfcp_data.config_lock);
598 fc_remove_host(shost);
599 scsi_remove_host(shost);
600 scsi_host_put(shost);
601 adapter->scsi_host = NULL;
602 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
604 return;
608 * Support functions for FC transport class
610 static struct fc_host_statistics*
611 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
613 struct fc_host_statistics *fc_stats;
615 if (!adapter->fc_stats) {
616 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
617 if (!fc_stats)
618 return NULL;
619 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
621 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
622 return adapter->fc_stats;
625 static void
626 zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
627 struct fsf_qtcb_bottom_port *data,
628 struct fsf_qtcb_bottom_port *old)
630 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
631 old->seconds_since_last_reset;
632 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
633 fc_stats->tx_words = data->tx_words - old->tx_words;
634 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
635 fc_stats->rx_words = data->rx_words - old->rx_words;
636 fc_stats->lip_count = data->lip - old->lip;
637 fc_stats->nos_count = data->nos - old->nos;
638 fc_stats->error_frames = data->error_frames - old->error_frames;
639 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
640 fc_stats->link_failure_count = data->link_failure - old->link_failure;
641 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
642 fc_stats->loss_of_signal_count = data->loss_of_signal -
643 old->loss_of_signal;
644 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
645 old->psp_error_counts;
646 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
647 old->invalid_tx_words;
648 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
649 fc_stats->fcp_input_requests = data->input_requests -
650 old->input_requests;
651 fc_stats->fcp_output_requests = data->output_requests -
652 old->output_requests;
653 fc_stats->fcp_control_requests = data->control_requests -
654 old->control_requests;
655 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
656 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
659 static void
660 zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
661 struct fsf_qtcb_bottom_port *data)
663 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
664 fc_stats->tx_frames = data->tx_frames;
665 fc_stats->tx_words = data->tx_words;
666 fc_stats->rx_frames = data->rx_frames;
667 fc_stats->rx_words = data->rx_words;
668 fc_stats->lip_count = data->lip;
669 fc_stats->nos_count = data->nos;
670 fc_stats->error_frames = data->error_frames;
671 fc_stats->dumped_frames = data->dumped_frames;
672 fc_stats->link_failure_count = data->link_failure;
673 fc_stats->loss_of_sync_count = data->loss_of_sync;
674 fc_stats->loss_of_signal_count = data->loss_of_signal;
675 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
676 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
677 fc_stats->invalid_crc_count = data->invalid_crcs;
678 fc_stats->fcp_input_requests = data->input_requests;
679 fc_stats->fcp_output_requests = data->output_requests;
680 fc_stats->fcp_control_requests = data->control_requests;
681 fc_stats->fcp_input_megabytes = data->input_mb;
682 fc_stats->fcp_output_megabytes = data->output_mb;
686 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
688 * assumption: scsi_transport_fc synchronizes calls of
689 * get_fc_host_stats and reset_fc_host_stats
690 * (XXX to be checked otherwise introduce locking)
692 static struct fc_host_statistics *
693 zfcp_get_fc_host_stats(struct Scsi_Host *shost)
695 struct zfcp_adapter *adapter;
696 struct fc_host_statistics *fc_stats;
697 struct fsf_qtcb_bottom_port *data;
698 int ret;
700 adapter = (struct zfcp_adapter *)shost->hostdata[0];
701 fc_stats = zfcp_init_fc_host_stats(adapter);
702 if (!fc_stats)
703 return NULL;
705 data = kzalloc(sizeof(*data), GFP_KERNEL);
706 if (!data)
707 return NULL;
709 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
710 if (ret) {
711 kfree(data);
712 return NULL; /* XXX return zeroed fc_stats? */
715 if (adapter->stats_reset &&
716 ((jiffies/HZ - adapter->stats_reset) <
717 data->seconds_since_last_reset)) {
718 zfcp_adjust_fc_host_stats(fc_stats, data,
719 adapter->stats_reset_data);
720 } else
721 zfcp_set_fc_host_stats(fc_stats, data);
723 kfree(data);
724 return fc_stats;
727 static void
728 zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
730 struct zfcp_adapter *adapter;
731 struct fsf_qtcb_bottom_port *data, *old_data;
732 int ret;
734 adapter = (struct zfcp_adapter *)shost->hostdata[0];
735 data = kzalloc(sizeof(*data), GFP_KERNEL);
736 if (!data)
737 return;
739 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
740 if (ret) {
741 kfree(data);
742 } else {
743 adapter->stats_reset = jiffies/HZ;
744 old_data = adapter->stats_reset_data;
745 adapter->stats_reset_data = data; /* finally freed in
746 adater_dequeue */
747 kfree(old_data);
751 static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
753 rport->dev_loss_tmo = timeout;
756 struct fc_function_template zfcp_transport_functions = {
757 .show_starget_port_id = 1,
758 .show_starget_port_name = 1,
759 .show_starget_node_name = 1,
760 .show_rport_supported_classes = 1,
761 .show_rport_maxframe_size = 1,
762 .show_rport_dev_loss_tmo = 1,
763 .show_host_node_name = 1,
764 .show_host_port_name = 1,
765 .show_host_permanent_port_name = 1,
766 .show_host_supported_classes = 1,
767 .show_host_supported_speeds = 1,
768 .show_host_maxframe_size = 1,
769 .show_host_serial_number = 1,
770 .get_fc_host_stats = zfcp_get_fc_host_stats,
771 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
772 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
773 /* no functions registered for following dynamic attributes but
774 directly set by LLDD */
775 .show_host_port_type = 1,
776 .show_host_speed = 1,
777 .show_host_port_id = 1,
778 .disable_target_scan = 1,
782 * ZFCP_DEFINE_SCSI_ATTR
783 * @_name: name of show attribute
784 * @_format: format string
785 * @_value: value to print
787 * Generates attribute for a unit.
789 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
790 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
791 char *buf) \
793 struct scsi_device *sdev; \
794 struct zfcp_unit *unit; \
796 sdev = to_scsi_device(dev); \
797 unit = sdev->hostdata; \
798 return sprintf(buf, _format, _value); \
801 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
803 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
804 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
805 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
807 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
808 &dev_attr_fcp_lun,
809 &dev_attr_wwpn,
810 &dev_attr_hba_id,
811 NULL
814 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
815 struct device_attribute *attr,
816 char *buf)
818 struct Scsi_Host *scsi_host = dev_to_shost(dev);
819 struct fsf_qtcb_bottom_port *qtcb_port;
820 int retval;
821 struct zfcp_adapter *adapter;
823 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
824 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
825 return -EOPNOTSUPP;
827 qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
828 if (!qtcb_port)
829 return -ENOMEM;
831 retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
832 if (!retval)
833 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
834 qtcb_port->cb_util, qtcb_port->a_util);
835 kfree(qtcb_port);
836 return retval;
839 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
840 struct fsf_statistics_info *stat_inf)
842 int retval;
843 struct fsf_qtcb_bottom_config *qtcb_config;
844 struct Scsi_Host *scsi_host = dev_to_shost(dev);
845 struct zfcp_adapter *adapter;
847 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
848 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
849 return -EOPNOTSUPP;
851 qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
852 GFP_KERNEL);
853 if (!qtcb_config)
854 return -ENOMEM;
856 retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
857 if (!retval)
858 *stat_inf = qtcb_config->stat_info;
860 kfree(qtcb_config);
861 return retval;
864 static ssize_t zfcp_sysfs_adapter_request_show(struct device *dev,
865 struct device_attribute *attr,
866 char *buf)
868 struct fsf_statistics_info stat_info;
869 int retval;
871 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
872 if (retval)
873 return retval;
875 return sprintf(buf, "%llu %llu %llu\n",
876 (unsigned long long) stat_info.input_req,
877 (unsigned long long) stat_info.output_req,
878 (unsigned long long) stat_info.control_req);
881 static ssize_t zfcp_sysfs_adapter_mb_show(struct device *dev,
882 struct device_attribute *attr,
883 char *buf)
885 struct fsf_statistics_info stat_info;
886 int retval;
888 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
889 if (retval)
890 return retval;
892 return sprintf(buf, "%llu %llu\n",
893 (unsigned long long) stat_info.input_mb,
894 (unsigned long long) stat_info.output_mb);
897 static ssize_t zfcp_sysfs_adapter_sec_active_show(struct device *dev,
898 struct device_attribute *attr,
899 char *buf)
901 struct fsf_statistics_info stat_info;
902 int retval;
904 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
905 if (retval)
906 return retval;
908 return sprintf(buf, "%llu\n",
909 (unsigned long long) stat_info.seconds_act);
912 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
913 static DEVICE_ATTR(requests, S_IRUGO, zfcp_sysfs_adapter_request_show, NULL);
914 static DEVICE_ATTR(megabytes, S_IRUGO, zfcp_sysfs_adapter_mb_show, NULL);
915 static DEVICE_ATTR(seconds_active, S_IRUGO,
916 zfcp_sysfs_adapter_sec_active_show, NULL);
918 static struct device_attribute *zfcp_a_stats_attrs[] = {
919 &dev_attr_utilization,
920 &dev_attr_requests,
921 &dev_attr_megabytes,
922 &dev_attr_seconds_active,
923 NULL
926 #undef ZFCP_LOG_AREA