[SCSI] zfcp: introduce struct timer_list in struct zfcp_fsf_req
[linux-2.6/kvm.git] / drivers / s390 / scsi / zfcp_scsi.c
blob753bb9b2fe74a8ad1e51825ece0f7f9fca3db0d6
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
6 *
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"
26 static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
27 static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
28 static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
29 static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
30 void (*done) (struct scsi_cmnd *));
31 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
32 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
33 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
34 static int zfcp_task_management_function(struct zfcp_unit *, u8,
35 struct scsi_cmnd *);
37 static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
38 unsigned int, unsigned int);
40 static struct device_attribute *zfcp_sysfs_sdev_attrs[];
42 struct zfcp_data zfcp_data = {
43 .scsi_host_template = {
44 .name = ZFCP_NAME,
45 .module = THIS_MODULE,
46 .proc_name = "zfcp",
47 .slave_alloc = zfcp_scsi_slave_alloc,
48 .slave_configure = zfcp_scsi_slave_configure,
49 .slave_destroy = zfcp_scsi_slave_destroy,
50 .queuecommand = zfcp_scsi_queuecommand,
51 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
52 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
53 .eh_bus_reset_handler = zfcp_scsi_eh_host_reset_handler,
54 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
55 .can_queue = 4096,
56 .this_id = -1,
57 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
58 .cmd_per_lun = 1,
59 .use_clustering = 1,
60 .sdev_attrs = zfcp_sysfs_sdev_attrs,
62 .driver_version = ZFCP_VERSION,
65 /* Find start of Response Information in FCP response unit*/
66 char *
67 zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
69 char *fcp_rsp_info_ptr;
71 fcp_rsp_info_ptr =
72 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
74 return fcp_rsp_info_ptr;
77 /* Find start of Sense Information in FCP response unit*/
78 char *
79 zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
81 char *fcp_sns_info_ptr;
83 fcp_sns_info_ptr =
84 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
85 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
86 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
87 fcp_rsp_iu->fcp_rsp_len;
89 return fcp_sns_info_ptr;
92 fcp_dl_t *
93 zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
95 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
96 fcp_dl_t *fcp_dl_addr;
98 fcp_dl_addr = (fcp_dl_t *)
99 ((unsigned char *) fcp_cmd +
100 sizeof (struct fcp_cmnd_iu) + additional_length);
102 * fcp_dl_addr = start address of fcp_cmnd structure +
103 * size of fixed part + size of dynamically sized add_dcp_cdb field
104 * SEE FCP-2 documentation
106 return fcp_dl_addr;
109 fcp_dl_t
110 zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
112 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
115 void
116 zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
118 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
122 * note: it's a bit-or operation not an assignment
123 * regarding the specified byte
125 static inline void
126 set_byte(u32 * result, char status, char pos)
128 *result |= status << (pos * 8);
131 void
132 set_host_byte(u32 * result, char status)
134 set_byte(result, status, 2);
137 void
138 set_driver_byte(u32 * result, char status)
140 set_byte(result, status, 3);
143 static int
144 zfcp_scsi_slave_alloc(struct scsi_device *sdp)
146 struct zfcp_adapter *adapter;
147 struct zfcp_unit *unit;
148 unsigned long flags;
149 int retval = -ENXIO;
151 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
152 if (!adapter)
153 goto out;
155 read_lock_irqsave(&zfcp_data.config_lock, flags);
156 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
157 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
158 &unit->status)) {
159 sdp->hostdata = unit;
160 unit->device = sdp;
161 zfcp_unit_get(unit);
162 retval = 0;
164 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
165 out:
166 return retval;
170 * zfcp_scsi_slave_destroy - called when scsi device is removed
172 * Remove reference to associated scsi device for an zfcp_unit.
173 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
174 * or a scan for this device might have failed.
176 static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
178 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
180 if (unit) {
181 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
182 sdpnt->hostdata = NULL;
183 unit->device = NULL;
184 zfcp_erp_unit_failed(unit);
185 zfcp_unit_put(unit);
186 } else {
187 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
188 "address %p\n", sdpnt);
193 * called from scsi midlayer to allow finetuning of a device.
195 static int
196 zfcp_scsi_slave_configure(struct scsi_device *sdp)
198 if (sdp->tagged_supported)
199 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
200 else
201 scsi_adjust_queue_depth(sdp, 0, 1);
202 return 0;
206 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
207 * @scpnt: pointer to struct scsi_cmnd where result is set
208 * @result: result to be set in scpnt (e.g. DID_ERROR)
210 static void
211 zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
213 set_host_byte(&scpnt->result, result);
214 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
215 zfcp_scsi_dbf_event_result("fail", 4,
216 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
217 scpnt, NULL);
218 /* return directly */
219 scpnt->scsi_done(scpnt);
223 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
224 * zfcp_scsi_command_sync
225 * @adapter: adapter where scsi command is issued
226 * @unit: unit to which scsi command is sent
227 * @scpnt: scsi command to be sent
228 * @timer: timer to be started if request is successfully initiated
230 * Note: In scsi_done function must be set in scpnt.
233 zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
234 struct scsi_cmnd *scpnt, int use_timer)
236 int tmp;
237 int retval;
239 retval = 0;
241 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
242 BUG_ON(scpnt->scsi_done == NULL);
244 if (unlikely(NULL == unit)) {
245 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
246 goto out;
249 if (unlikely(
250 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
251 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
252 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
253 "0x%016Lx on adapter %s\n",
254 unit->fcp_lun, unit->port->wwpn,
255 zfcp_get_busid_by_adapter(adapter));
256 zfcp_scsi_command_fail(scpnt, DID_ERROR);
257 goto out;
260 if (unlikely(
261 !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
262 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
263 "on port 0x%016Lx in recovery\n",
264 zfcp_get_busid_by_unit(unit),
265 unit->fcp_lun, unit->port->wwpn);
266 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
267 goto out;
270 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
271 ZFCP_REQ_AUTO_CLEANUP);
273 if (unlikely(tmp < 0)) {
274 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
275 retval = SCSI_MLQUEUE_HOST_BUSY;
278 out:
279 return retval;
282 void
283 zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
285 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
286 complete(wait);
291 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
292 * @unit: unit where command is sent to
293 * @scpnt: scsi command to be sent
294 * @use_timer: indicates whether timer should be setup or not
295 * Return: 0
297 * Errors are indicated in scpnt->result
300 zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
301 int use_timer)
303 int ret;
304 DECLARE_COMPLETION(wait);
306 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
307 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
308 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
309 use_timer);
310 if (ret == 0)
311 wait_for_completion(&wait);
313 scpnt->SCp.ptr = NULL;
315 return 0;
319 * function: zfcp_scsi_queuecommand
321 * purpose: enqueues a SCSI command to the specified target device
323 * returns: 0 - success, SCSI command enqueued
324 * !0 - failure
327 zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
328 void (*done) (struct scsi_cmnd *))
330 struct zfcp_unit *unit;
331 struct zfcp_adapter *adapter;
333 /* reset the status for this request */
334 scpnt->result = 0;
335 scpnt->host_scribble = NULL;
336 scpnt->scsi_done = done;
339 * figure out adapter and target device
340 * (stored there by zfcp_scsi_slave_alloc)
342 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
343 unit = (struct zfcp_unit *) scpnt->device->hostdata;
345 return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
348 static struct zfcp_unit *
349 zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
350 unsigned int lun)
352 struct zfcp_port *port;
353 struct zfcp_unit *unit, *retval = NULL;
355 list_for_each_entry(port, &adapter->port_list_head, list) {
356 if (!port->rport || (id != port->rport->scsi_target_id))
357 continue;
358 list_for_each_entry(unit, &port->unit_list_head, list) {
359 if (lun == unit->scsi_lun) {
360 retval = unit;
361 goto out;
365 out:
366 return retval;
370 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
371 * @scpnt: pointer to scsi_cmnd to be aborted
372 * Return: SUCCESS - command has been aborted and cleaned up in internal
373 * bookkeeping, SCSI stack won't be called for aborted command
374 * FAILED - otherwise
376 * We do not need to care for a SCSI command which completes normally
377 * but late during this abort routine runs. We are allowed to return
378 * late commands to the SCSI stack. It tracks the state of commands and
379 * will handle late commands. (Usually, the normal completion of late
380 * commands is ignored with respect to the running abort operation.)
382 int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
384 struct Scsi_Host *scsi_host;
385 struct zfcp_adapter *adapter;
386 struct zfcp_unit *unit;
387 struct zfcp_fsf_req *fsf_req;
388 unsigned long flags;
389 unsigned long old_req_id;
390 int retval = SUCCESS;
392 scsi_host = scpnt->device->host;
393 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
394 unit = (struct zfcp_unit *) scpnt->device->hostdata;
396 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
397 scpnt, zfcp_get_busid_by_adapter(adapter));
399 /* avoid race condition between late normal completion and abort */
400 write_lock_irqsave(&adapter->abort_lock, flags);
402 /* Check whether corresponding fsf_req is still pending */
403 spin_lock(&adapter->req_list_lock);
404 fsf_req = zfcp_reqlist_ismember(adapter, (unsigned long)
405 scpnt->host_scribble);
406 spin_unlock(&adapter->req_list_lock);
407 if (!fsf_req) {
408 write_unlock_irqrestore(&adapter->abort_lock, flags);
409 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
410 retval = SUCCESS;
411 goto out;
413 fsf_req->data = 0;
414 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
415 old_req_id = fsf_req->req_id;
417 /* don't access old fsf_req after releasing the abort_lock */
418 write_unlock_irqrestore(&adapter->abort_lock, flags);
420 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
421 if (!fsf_req) {
422 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
423 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
424 old_req_id);
425 retval = FAILED;
426 goto out;
429 __wait_event(fsf_req->completion_wq,
430 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
432 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
433 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
434 retval = SUCCESS;
435 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
436 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
437 retval = SUCCESS;
438 } else {
439 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
440 retval = FAILED;
442 zfcp_fsf_req_free(fsf_req);
443 out:
444 return retval;
448 zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
450 int retval;
451 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
453 if (!unit) {
454 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
455 retval = SUCCESS;
456 goto out;
458 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
461 * If we do not know whether the unit supports 'logical unit reset'
462 * then try 'logical unit reset' and proceed with 'target reset'
463 * if 'logical unit reset' fails.
464 * If the unit is known not to support 'logical unit reset' then
465 * skip 'logical unit reset' and try 'target reset' immediately.
467 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
468 &unit->status)) {
469 retval = zfcp_task_management_function(unit,
470 FCP_LOGICAL_UNIT_RESET,
471 scpnt);
472 if (retval) {
473 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
474 if (retval == -ENOTSUPP)
475 atomic_set_mask
476 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
477 &unit->status);
478 /* fall through and try 'target reset' next */
479 } else {
480 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
481 unit);
482 /* avoid 'target reset' */
483 retval = SUCCESS;
484 goto out;
487 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
488 if (retval) {
489 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
490 retval = FAILED;
491 } else {
492 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
493 retval = SUCCESS;
495 out:
496 return retval;
499 static int
500 zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
501 struct scsi_cmnd *scpnt)
503 struct zfcp_adapter *adapter = unit->port->adapter;
504 struct zfcp_fsf_req *fsf_req;
505 int retval = 0;
507 /* issue task management function */
508 fsf_req = zfcp_fsf_send_fcp_command_task_management
509 (adapter, unit, tm_flags, 0);
510 if (!fsf_req) {
511 ZFCP_LOG_INFO("error: creation of task management request "
512 "failed for unit 0x%016Lx on port 0x%016Lx on "
513 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
514 zfcp_get_busid_by_adapter(adapter));
515 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
516 retval = -ENOMEM;
517 goto out;
520 __wait_event(fsf_req->completion_wq,
521 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
524 * check completion status of task management function
526 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
527 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
528 retval = -EIO;
529 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
530 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
531 retval = -ENOTSUPP;
532 } else
533 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
535 zfcp_fsf_req_free(fsf_req);
536 out:
537 return retval;
541 * zfcp_scsi_eh_host_reset_handler - handler for host and bus reset
543 int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
545 struct zfcp_unit *unit;
546 struct zfcp_adapter *adapter;
547 unsigned long flags;
549 unit = (struct zfcp_unit*) scpnt->device->hostdata;
550 adapter = unit->port->adapter;
552 ZFCP_LOG_NORMAL("host/bus reset because of problems with "
553 "unit 0x%016Lx\n", unit->fcp_lun);
555 write_lock_irqsave(&adapter->erp_lock, flags);
556 if (atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
557 &adapter->status)) {
558 zfcp_erp_modify_adapter_status(adapter,
559 ZFCP_STATUS_COMMON_UNBLOCKED|ZFCP_STATUS_COMMON_OPEN,
560 ZFCP_CLEAR);
561 zfcp_erp_action_dismiss_adapter(adapter);
562 write_unlock_irqrestore(&adapter->erp_lock, flags);
563 zfcp_fsf_req_dismiss_all(adapter);
564 adapter->fsf_req_seq_no = 0;
565 zfcp_erp_adapter_reopen(adapter, 0);
566 } else {
567 write_unlock_irqrestore(&adapter->erp_lock, flags);
568 zfcp_erp_adapter_reopen(adapter, 0);
569 zfcp_erp_wait(adapter);
572 return SUCCESS;
576 zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
578 int retval = 0;
579 static unsigned int unique_id = 0;
581 /* register adapter as SCSI host with mid layer of SCSI stack */
582 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
583 sizeof (struct zfcp_adapter *));
584 if (!adapter->scsi_host) {
585 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
586 "for adapter %s ",
587 zfcp_get_busid_by_adapter(adapter));
588 retval = -EIO;
589 goto out;
591 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
593 /* tell the SCSI stack some characteristics of this adapter */
594 adapter->scsi_host->max_id = 1;
595 adapter->scsi_host->max_lun = 1;
596 adapter->scsi_host->max_channel = 0;
597 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
598 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
599 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
602 * save a pointer to our own adapter data structure within
603 * hostdata field of SCSI host data structure
605 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
607 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
608 scsi_host_put(adapter->scsi_host);
609 retval = -EIO;
610 goto out;
612 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
613 out:
614 return retval;
617 void
618 zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
620 struct Scsi_Host *shost;
621 struct zfcp_port *port;
623 shost = adapter->scsi_host;
624 if (!shost)
625 return;
626 read_lock_irq(&zfcp_data.config_lock);
627 list_for_each_entry(port, &adapter->port_list_head, list)
628 if (port->rport)
629 port->rport = NULL;
630 read_unlock_irq(&zfcp_data.config_lock);
631 fc_remove_host(shost);
632 scsi_remove_host(shost);
633 scsi_host_put(shost);
634 adapter->scsi_host = NULL;
635 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
637 return;
641 * Support functions for FC transport class
643 static struct fc_host_statistics*
644 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
646 struct fc_host_statistics *fc_stats;
648 if (!adapter->fc_stats) {
649 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
650 if (!fc_stats)
651 return NULL;
652 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
654 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
655 return adapter->fc_stats;
658 static void
659 zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
660 struct fsf_qtcb_bottom_port *data,
661 struct fsf_qtcb_bottom_port *old)
663 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
664 old->seconds_since_last_reset;
665 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
666 fc_stats->tx_words = data->tx_words - old->tx_words;
667 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
668 fc_stats->rx_words = data->rx_words - old->rx_words;
669 fc_stats->lip_count = data->lip - old->lip;
670 fc_stats->nos_count = data->nos - old->nos;
671 fc_stats->error_frames = data->error_frames - old->error_frames;
672 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
673 fc_stats->link_failure_count = data->link_failure - old->link_failure;
674 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
675 fc_stats->loss_of_signal_count = data->loss_of_signal -
676 old->loss_of_signal;
677 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
678 old->psp_error_counts;
679 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
680 old->invalid_tx_words;
681 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
682 fc_stats->fcp_input_requests = data->input_requests -
683 old->input_requests;
684 fc_stats->fcp_output_requests = data->output_requests -
685 old->output_requests;
686 fc_stats->fcp_control_requests = data->control_requests -
687 old->control_requests;
688 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
689 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
692 static void
693 zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
694 struct fsf_qtcb_bottom_port *data)
696 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
697 fc_stats->tx_frames = data->tx_frames;
698 fc_stats->tx_words = data->tx_words;
699 fc_stats->rx_frames = data->rx_frames;
700 fc_stats->rx_words = data->rx_words;
701 fc_stats->lip_count = data->lip;
702 fc_stats->nos_count = data->nos;
703 fc_stats->error_frames = data->error_frames;
704 fc_stats->dumped_frames = data->dumped_frames;
705 fc_stats->link_failure_count = data->link_failure;
706 fc_stats->loss_of_sync_count = data->loss_of_sync;
707 fc_stats->loss_of_signal_count = data->loss_of_signal;
708 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
709 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
710 fc_stats->invalid_crc_count = data->invalid_crcs;
711 fc_stats->fcp_input_requests = data->input_requests;
712 fc_stats->fcp_output_requests = data->output_requests;
713 fc_stats->fcp_control_requests = data->control_requests;
714 fc_stats->fcp_input_megabytes = data->input_mb;
715 fc_stats->fcp_output_megabytes = data->output_mb;
719 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
721 * assumption: scsi_transport_fc synchronizes calls of
722 * get_fc_host_stats and reset_fc_host_stats
723 * (XXX to be checked otherwise introduce locking)
725 static struct fc_host_statistics *
726 zfcp_get_fc_host_stats(struct Scsi_Host *shost)
728 struct zfcp_adapter *adapter;
729 struct fc_host_statistics *fc_stats;
730 struct fsf_qtcb_bottom_port *data;
731 int ret;
733 adapter = (struct zfcp_adapter *)shost->hostdata[0];
734 fc_stats = zfcp_init_fc_host_stats(adapter);
735 if (!fc_stats)
736 return NULL;
738 data = kzalloc(sizeof(*data), GFP_KERNEL);
739 if (!data)
740 return NULL;
742 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
743 if (ret) {
744 kfree(data);
745 return NULL; /* XXX return zeroed fc_stats? */
748 if (adapter->stats_reset &&
749 ((jiffies/HZ - adapter->stats_reset) <
750 data->seconds_since_last_reset)) {
751 zfcp_adjust_fc_host_stats(fc_stats, data,
752 adapter->stats_reset_data);
753 } else
754 zfcp_set_fc_host_stats(fc_stats, data);
756 kfree(data);
757 return fc_stats;
760 static void
761 zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
763 struct zfcp_adapter *adapter;
764 struct fsf_qtcb_bottom_port *data, *old_data;
765 int ret;
767 adapter = (struct zfcp_adapter *)shost->hostdata[0];
768 data = kzalloc(sizeof(*data), GFP_KERNEL);
769 if (!data)
770 return;
772 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
773 if (ret == 0) {
774 adapter->stats_reset = jiffies/HZ;
775 old_data = adapter->stats_reset_data;
776 adapter->stats_reset_data = data; /* finally freed in
777 adater_dequeue */
778 kfree(old_data);
782 static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
784 rport->dev_loss_tmo = timeout;
787 struct fc_function_template zfcp_transport_functions = {
788 .show_starget_port_id = 1,
789 .show_starget_port_name = 1,
790 .show_starget_node_name = 1,
791 .show_rport_supported_classes = 1,
792 .show_rport_maxframe_size = 1,
793 .show_rport_dev_loss_tmo = 1,
794 .show_host_node_name = 1,
795 .show_host_port_name = 1,
796 .show_host_permanent_port_name = 1,
797 .show_host_supported_classes = 1,
798 .show_host_supported_speeds = 1,
799 .show_host_maxframe_size = 1,
800 .show_host_serial_number = 1,
801 .get_fc_host_stats = zfcp_get_fc_host_stats,
802 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
803 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
804 /* no functions registered for following dynamic attributes but
805 directly set by LLDD */
806 .show_host_port_type = 1,
807 .show_host_speed = 1,
808 .show_host_port_id = 1,
812 * ZFCP_DEFINE_SCSI_ATTR
813 * @_name: name of show attribute
814 * @_format: format string
815 * @_value: value to print
817 * Generates attribute for a unit.
819 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
820 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
821 char *buf) \
823 struct scsi_device *sdev; \
824 struct zfcp_unit *unit; \
826 sdev = to_scsi_device(dev); \
827 unit = sdev->hostdata; \
828 return sprintf(buf, _format, _value); \
831 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
833 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
834 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
835 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
837 static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
838 &dev_attr_fcp_lun,
839 &dev_attr_wwpn,
840 &dev_attr_hba_id,
841 NULL
844 #undef ZFCP_LOG_AREA