blob: a96e5c3b9460cdbe9d3f0e9158e79509bd6a07a6 [file] [log] [blame]
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001/*
Christof Schmitt553448f2008-06-10 18:20:58 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Christof Schmitt553448f2008-06-10 18:20:58 +02004 * Interface to Linux SCSI midlayer.
Swen Schillig41fa2ad2007-09-07 09:15:31 +02005 *
Christof Schmitt553448f2008-06-10 18:20:58 +02006 * Copyright IBM Corporation 2002, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "zfcp_ext.h"
Christof Schmitt5f852be2007-05-08 11:16:52 +020010#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
13static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
14static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
15static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
16 void (*done) (struct scsi_cmnd *));
17static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
18static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
Christof Schmitt74b2e042008-03-03 12:19:28 +010019static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020021static int zfcp_task_management_function(struct zfcp_unit *, u8,
22 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Andreas Herrmann06506d02006-05-22 18:18:19 +020024static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
25 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static struct device_attribute *zfcp_sysfs_sdev_attrs[];
Swen Schillig6d9d63b2008-04-24 19:35:54 +020028static struct device_attribute *zfcp_a_stats_attrs[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030struct zfcp_data zfcp_data = {
31 .scsi_host_template = {
Christof Schmitt553448f2008-06-10 18:20:58 +020032 .name = "zfcp",
Heiko Carstensdd52e0e2006-09-18 22:28:49 +020033 .module = THIS_MODULE,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020034 .proc_name = "zfcp",
Heiko Carstensd2c993d2006-07-12 16:41:55 +020035 .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,
Christof Schmitt74b2e042008-03-03 12:19:28 +010041 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020042 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
43 .can_queue = 4096,
44 .this_id = -1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020045 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
46 .cmd_per_lun = 1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020047 .use_clustering = 1,
48 .sdev_attrs = zfcp_sysfs_sdev_attrs,
Swen Schillig8d1a0062006-10-12 11:43:44 +020049 .max_sectors = ZFCP_MAX_SECTORS,
Swen Schillig6d9d63b2008-04-24 19:35:54 +020050 .shost_attrs = zfcp_a_stats_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54/* Find start of Response Information in FCP response unit*/
55char *
56zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
57{
58 char *fcp_rsp_info_ptr;
59
60 fcp_rsp_info_ptr =
61 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
62
63 return fcp_rsp_info_ptr;
64}
65
66/* Find start of Sense Information in FCP response unit*/
67char *
68zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
69{
70 char *fcp_sns_info_ptr;
71
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;
77
78 return fcp_sns_info_ptr;
79}
80
Heiko Carstens2b67fc42007-02-05 21:16:47 +010081static fcp_dl_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070082zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
83{
84 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
85 fcp_dl_t *fcp_dl_addr;
86
87 fcp_dl_addr = (fcp_dl_t *)
88 ((unsigned char *) fcp_cmd +
89 sizeof (struct fcp_cmnd_iu) + additional_length);
90 /*
Swen Schillig41fa2ad2007-09-07 09:15:31 +020091 * fcp_dl_addr = start address of fcp_cmnd structure +
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * size of fixed part + size of dynamically sized add_dcp_cdb field
93 * SEE FCP-2 documentation
94 */
95 return fcp_dl_addr;
96}
97
98fcp_dl_t
99zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
100{
101 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
102}
103
104void
105zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
106{
107 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
108}
109
110/*
111 * note: it's a bit-or operation not an assignment
112 * regarding the specified byte
113 */
114static inline void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100115set_byte(int *result, char status, char pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 *result |= status << (pos * 8);
118}
119
120void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100121set_host_byte(int *result, char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 set_byte(result, status, 2);
124}
125
126void
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100127set_driver_byte(int *result, char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 set_byte(result, status, 3);
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int
133zfcp_scsi_slave_alloc(struct scsi_device *sdp)
134{
135 struct zfcp_adapter *adapter;
136 struct zfcp_unit *unit;
137 unsigned long flags;
Andreas Herrmannfb121b02005-12-01 02:49:29 +0100138 int retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
141 if (!adapter)
142 goto out;
143
144 read_lock_irqsave(&zfcp_data.config_lock, flags);
145 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100146 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
147 &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 sdp->hostdata = unit;
149 unit->device = sdp;
150 zfcp_unit_get(unit);
151 retval = 0;
152 }
153 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
154 out:
155 return retval;
156}
157
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200158/**
159 * zfcp_scsi_slave_destroy - called when scsi device is removed
160 *
161 * Remove reference to associated scsi device for an zfcp_unit.
162 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
163 * or a scan for this device might have failed.
164 */
165static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
Christof Schmitt553448f2008-06-10 18:20:58 +0200168 WARN_ON(!unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (unit) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100170 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 sdpnt->hostdata = NULL;
172 unit->device = NULL;
Martin Peschke1f6f7122008-04-18 12:51:55 +0200173 zfcp_erp_unit_failed(unit, 12, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 zfcp_unit_put(unit);
Christof Schmitt553448f2008-06-10 18:20:58 +0200175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * called from scsi midlayer to allow finetuning of a device.
180 */
181static int
182zfcp_scsi_slave_configure(struct scsi_device *sdp)
183{
184 if (sdp->tagged_supported)
185 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
186 else
187 scsi_adjust_queue_depth(sdp, 0, 1);
188 return 0;
189}
190
191/**
192 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
193 * @scpnt: pointer to struct scsi_cmnd where result is set
194 * @result: result to be set in scpnt (e.g. DID_ERROR)
195 */
196static void
197zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
198{
199 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200200 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
201 zfcp_scsi_dbf_event_result("fail", 4,
202 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100203 scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* return directly */
205 scpnt->scsi_done(scpnt);
206}
207
208/**
209 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
210 * zfcp_scsi_command_sync
211 * @adapter: adapter where scsi command is issued
212 * @unit: unit to which scsi command is sent
213 * @scpnt: scsi command to be sent
214 * @timer: timer to be started if request is successfully initiated
215 *
216 * Note: In scsi_done function must be set in scpnt.
217 */
218int
219zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200220 struct scsi_cmnd *scpnt, int use_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int tmp;
223 int retval;
224
225 retval = 0;
226
227 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
228 BUG_ON(scpnt->scsi_done == NULL);
229
230 if (unlikely(NULL == unit)) {
231 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
232 goto out;
233 }
234
235 if (unlikely(
236 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
237 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 zfcp_scsi_command_fail(scpnt, DID_ERROR);
239 goto out;
240 }
241
Christof Schmittba172422007-12-20 12:30:26 +0100242 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
243 ZFCP_REQ_AUTO_CLEANUP);
244 if (unlikely(tmp == -EBUSY)) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100245 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 goto out;
247 }
248
Christof Schmitt553448f2008-06-10 18:20:58 +0200249 if (unlikely(tmp < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 retval = SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252out:
253 return retval;
254}
255
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100256static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
258{
259 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
260 complete(wait);
261}
262
263
264/**
265 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
266 * @unit: unit where command is sent to
267 * @scpnt: scsi command to be sent
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200268 * @use_timer: indicates whether timer should be setup or not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * Return: 0
270 *
271 * Errors are indicated in scpnt->result
272 */
273int
274zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200275 int use_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int ret;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700278 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
281 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200282 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
283 use_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (ret == 0)
285 wait_for_completion(&wait);
286
287 scpnt->SCp.ptr = NULL;
288
289 return 0;
290}
291
292/*
293 * function: zfcp_scsi_queuecommand
294 *
295 * purpose: enqueues a SCSI command to the specified target device
296 *
297 * returns: 0 - success, SCSI command enqueued
298 * !0 - failure
299 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100300static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
302 void (*done) (struct scsi_cmnd *))
303{
304 struct zfcp_unit *unit;
305 struct zfcp_adapter *adapter;
306
307 /* reset the status for this request */
308 scpnt->result = 0;
309 scpnt->host_scribble = NULL;
310 scpnt->scsi_done = done;
311
312 /*
313 * figure out adapter and target device
314 * (stored there by zfcp_scsi_slave_alloc)
315 */
316 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
317 unit = (struct zfcp_unit *) scpnt->device->hostdata;
318
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200319 return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static struct zfcp_unit *
Andreas Herrmann06506d02006-05-22 18:18:19 +0200323zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
324 unsigned int lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct zfcp_port *port;
327 struct zfcp_unit *unit, *retval = NULL;
328
329 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700330 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 continue;
Christof Schmitt6b76a722007-08-28 09:30:59 +0200332 list_for_each_entry(unit, &port->unit_list_head, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (lun == unit->scsi_lun) {
334 retval = unit;
335 goto out;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 out:
339 return retval;
340}
341
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200342/**
343 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200344 * @scpnt: pointer to scsi_cmnd to be aborted
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200345 * Return: SUCCESS - command has been aborted and cleaned up in internal
346 * bookkeeping, SCSI stack won't be called for aborted command
347 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200349 * We do not need to care for a SCSI command which completes normally
350 * but late during this abort routine runs. We are allowed to return
351 * late commands to the SCSI stack. It tracks the state of commands and
352 * will handle late commands. (Usually, the normal completion of late
353 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100355static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200357 struct Scsi_Host *scsi_host;
358 struct zfcp_adapter *adapter;
359 struct zfcp_unit *unit;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200360 struct zfcp_fsf_req *fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 unsigned long flags;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200362 unsigned long old_req_id;
363 int retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200365 scsi_host = scpnt->device->host;
366 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
367 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200369 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 write_lock_irqsave(&adapter->abort_lock, flags);
371
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200372 /* Check whether corresponding fsf_req is still pending */
373 spin_lock(&adapter->req_list_lock);
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200374 fsf_req = zfcp_reqlist_find(adapter,
375 (unsigned long) scpnt->host_scribble);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200376 spin_unlock(&adapter->req_list_lock);
377 if (!fsf_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200379 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200380 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto out;
382 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200383 fsf_req->data = 0;
384 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
385 old_req_id = fsf_req->req_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200387 /* don't access old fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200389
390 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
391 if (!fsf_req) {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100392 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200393 old_req_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto out;
396 }
397
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200398 __wait_event(fsf_req->completion_wq,
399 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200400
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200401 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
402 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 retval = SUCCESS;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200404 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
405 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 } else {
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200408 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200411 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return retval;
414}
415
Christof Schmitt74b2e042008-03-03 12:19:28 +0100416static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 int retval;
Christof Schmitt74b2e042008-03-03 12:19:28 +0100419 struct zfcp_unit *unit = scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!unit) {
Christof Schmitt74b2e042008-03-03 12:19:28 +0100422 WARN_ON(1);
423 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Christof Schmitt74b2e042008-03-03 12:19:28 +0100425 retval = zfcp_task_management_function(unit,
426 FCP_LOGICAL_UNIT_RESET,
427 scpnt);
428 return retval ? FAILED : SUCCESS;
429}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Christof Schmitt74b2e042008-03-03 12:19:28 +0100431static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
432{
433 int retval;
434 struct zfcp_unit *unit = scpnt->device->hostdata;
435
436 if (!unit) {
437 WARN_ON(1);
438 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200440 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Christof Schmitt74b2e042008-03-03 12:19:28 +0100441 return retval ? FAILED : SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200445zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
446 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200450 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* issue task management function */
453 fsf_req = zfcp_fsf_send_fcp_command_task_management
454 (adapter, unit, tm_flags, 0);
455 if (!fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200456 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 retval = -ENOMEM;
458 goto out;
459 }
460
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200461 __wait_event(fsf_req->completion_wq,
462 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
463
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200464 /*
465 * check completion status of task management function
466 */
467 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
468 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200470 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
471 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200473 } else
474 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200475
476 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 out:
478 return retval;
479}
480
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200481/**
Christof Schmitta11b4742007-11-05 12:37:43 +0100482 * zfcp_scsi_eh_host_reset_handler - handler for host reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100484static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200486 struct zfcp_unit *unit;
487 struct zfcp_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200489 unit = (struct zfcp_unit*) scpnt->device->hostdata;
490 adapter = unit->port->adapter;
Martin Peschke1f6f7122008-04-18 12:51:55 +0200491 zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
Andreas Herrmann81654282006-09-18 22:30:36 +0200492 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200494 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497int
498zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
499{
500 int retval = 0;
501 static unsigned int unique_id = 0;
502
Michael Loehr9f287452007-05-09 11:01:24 +0200503 if (adapter->scsi_host)
504 goto out;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* register adapter as SCSI host with mid layer of SCSI stack */
507 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
508 sizeof (struct zfcp_adapter *));
509 if (!adapter->scsi_host) {
Christof Schmitt553448f2008-06-10 18:20:58 +0200510 dev_err(&adapter->ccw_device->dev,
511 "registration with SCSI stack failed.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 retval = -EIO;
513 goto out;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* tell the SCSI stack some characteristics of this adapter */
517 adapter->scsi_host->max_id = 1;
518 adapter->scsi_host->max_lun = 1;
519 adapter->scsi_host->max_channel = 0;
520 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
521 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200522 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /*
525 * save a pointer to our own adapter data structure within
526 * hostdata field of SCSI host data structure
527 */
528 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
529
530 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
531 scsi_host_put(adapter->scsi_host);
532 retval = -EIO;
533 goto out;
534 }
535 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
536 out:
537 return retval;
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540void
541zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
542{
543 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200544 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 shost = adapter->scsi_host;
547 if (!shost)
548 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200549 read_lock_irq(&zfcp_data.config_lock);
550 list_for_each_entry(port, &adapter->port_list_head, list)
551 if (port->rport)
552 port->rport = NULL;
553 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700554 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 scsi_remove_host(shost);
556 scsi_host_put(shost);
557 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
559
560 return;
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
564 * Support functions for FC transport class
565 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100566static struct fc_host_statistics*
567zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100569 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100571 if (!adapter->fc_stats) {
572 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
573 if (!fc_stats)
574 return NULL;
575 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
576 }
577 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
578 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100582zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
583 struct fsf_qtcb_bottom_port *data,
584 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100586 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
587 old->seconds_since_last_reset;
588 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
589 fc_stats->tx_words = data->tx_words - old->tx_words;
590 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
591 fc_stats->rx_words = data->rx_words - old->rx_words;
592 fc_stats->lip_count = data->lip - old->lip;
593 fc_stats->nos_count = data->nos - old->nos;
594 fc_stats->error_frames = data->error_frames - old->error_frames;
595 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
596 fc_stats->link_failure_count = data->link_failure - old->link_failure;
597 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
598 fc_stats->loss_of_signal_count = data->loss_of_signal -
599 old->loss_of_signal;
600 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
601 old->psp_error_counts;
602 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
603 old->invalid_tx_words;
604 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
605 fc_stats->fcp_input_requests = data->input_requests -
606 old->input_requests;
607 fc_stats->fcp_output_requests = data->output_requests -
608 old->output_requests;
609 fc_stats->fcp_control_requests = data->control_requests -
610 old->control_requests;
611 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
612 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100616zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
617 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100619 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
620 fc_stats->tx_frames = data->tx_frames;
621 fc_stats->tx_words = data->tx_words;
622 fc_stats->rx_frames = data->rx_frames;
623 fc_stats->rx_words = data->rx_words;
624 fc_stats->lip_count = data->lip;
625 fc_stats->nos_count = data->nos;
626 fc_stats->error_frames = data->error_frames;
627 fc_stats->dumped_frames = data->dumped_frames;
628 fc_stats->link_failure_count = data->link_failure;
629 fc_stats->loss_of_sync_count = data->loss_of_sync;
630 fc_stats->loss_of_signal_count = data->loss_of_signal;
631 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
632 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
633 fc_stats->invalid_crc_count = data->invalid_crcs;
634 fc_stats->fcp_input_requests = data->input_requests;
635 fc_stats->fcp_output_requests = data->output_requests;
636 fc_stats->fcp_control_requests = data->control_requests;
637 fc_stats->fcp_input_megabytes = data->input_mb;
638 fc_stats->fcp_output_megabytes = data->output_mb;
639}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100641/**
642 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
643 *
644 * assumption: scsi_transport_fc synchronizes calls of
645 * get_fc_host_stats and reset_fc_host_stats
646 * (XXX to be checked otherwise introduce locking)
647 */
648static struct fc_host_statistics *
649zfcp_get_fc_host_stats(struct Scsi_Host *shost)
650{
651 struct zfcp_adapter *adapter;
652 struct fc_host_statistics *fc_stats;
653 struct fsf_qtcb_bottom_port *data;
654 int ret;
655
656 adapter = (struct zfcp_adapter *)shost->hostdata[0];
657 fc_stats = zfcp_init_fc_host_stats(adapter);
658 if (!fc_stats)
659 return NULL;
660
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200661 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100662 if (!data)
663 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100664
Swen Schillig52ef11a2007-08-28 09:31:09 +0200665 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100666 if (ret) {
667 kfree(data);
668 return NULL; /* XXX return zeroed fc_stats? */
669 }
670
671 if (adapter->stats_reset &&
672 ((jiffies/HZ - adapter->stats_reset) <
673 data->seconds_since_last_reset)) {
674 zfcp_adjust_fc_host_stats(fc_stats, data,
675 adapter->stats_reset_data);
676 } else
677 zfcp_set_fc_host_stats(fc_stats, data);
678
679 kfree(data);
680 return fc_stats;
681}
682
683static void
684zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
685{
686 struct zfcp_adapter *adapter;
687 struct fsf_qtcb_bottom_port *data, *old_data;
688 int ret;
689
690 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200691 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100692 if (!data)
693 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100694
Swen Schillig52ef11a2007-08-28 09:31:09 +0200695 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
Heiko Carstens83f6d6d2007-08-08 10:47:02 +0200696 if (ret) {
697 kfree(data);
698 } else {
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100699 adapter->stats_reset = jiffies/HZ;
700 old_data = adapter->stats_reset_data;
701 adapter->stats_reset_data = data; /* finally freed in
702 adater_dequeue */
703 kfree(old_data);
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Andreas Herrmann338151e2006-05-22 18:25:56 +0200707static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
708{
709 rport->dev_loss_tmo = timeout;
710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 .show_starget_port_id = 1,
714 .show_starget_port_name = 1,
715 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700716 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200717 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200718 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700719 .show_host_node_name = 1,
720 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100721 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700722 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100723 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200724 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700725 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100726 .get_fc_host_stats = zfcp_get_fc_host_stats,
727 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200728 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100729 /* no functions registered for following dynamic attributes but
730 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100731 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200732 .show_host_speed = 1,
733 .show_host_port_id = 1,
Swen Schillig52ef11a2007-08-28 09:31:09 +0200734 .disable_target_scan = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
Christof Schmitt3a0d9e92008-05-06 11:00:06 +0200737#define ZFCP_DEFINE_LATENCY_ATTR(_name) \
738static ssize_t \
739zfcp_sysfs_unit_##_name##_latency_show(struct device *dev, \
740 struct device_attribute *attr, \
741 char *buf) { \
742 struct scsi_device *sdev = to_scsi_device(dev); \
743 struct zfcp_unit *unit = sdev->hostdata; \
744 struct zfcp_latencies *lat = &unit->latencies; \
745 struct zfcp_adapter *adapter = unit->port->adapter; \
746 unsigned long flags; \
747 unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc; \
748 \
749 spin_lock_irqsave(&lat->lock, flags); \
750 fsum = lat->_name.fabric.sum * adapter->timer_ticks; \
751 fmin = lat->_name.fabric.min * adapter->timer_ticks; \
752 fmax = lat->_name.fabric.max * adapter->timer_ticks; \
753 csum = lat->_name.channel.sum * adapter->timer_ticks; \
754 cmin = lat->_name.channel.min * adapter->timer_ticks; \
755 cmax = lat->_name.channel.max * adapter->timer_ticks; \
756 cc = lat->_name.counter; \
757 spin_unlock_irqrestore(&lat->lock, flags); \
758 \
759 do_div(fsum, 1000); \
760 do_div(fmin, 1000); \
761 do_div(fmax, 1000); \
762 do_div(csum, 1000); \
763 do_div(cmin, 1000); \
764 do_div(cmax, 1000); \
765 \
766 return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n", \
767 fmin, fmax, fsum, cmin, cmax, csum, cc); \
768} \
769static ssize_t \
770zfcp_sysfs_unit_##_name##_latency_store(struct device *dev, \
771 struct device_attribute *attr, \
772 const char *buf, size_t count) \
773{ \
774 struct scsi_device *sdev = to_scsi_device(dev); \
775 struct zfcp_unit *unit = sdev->hostdata; \
776 struct zfcp_latencies *lat = &unit->latencies; \
777 unsigned long flags; \
778 \
779 spin_lock_irqsave(&lat->lock, flags); \
780 lat->_name.fabric.sum = 0; \
781 lat->_name.fabric.min = 0xFFFFFFFF; \
782 lat->_name.fabric.max = 0; \
783 lat->_name.channel.sum = 0; \
784 lat->_name.channel.min = 0xFFFFFFFF; \
785 lat->_name.channel.max = 0; \
786 lat->_name.counter = 0; \
787 spin_unlock_irqrestore(&lat->lock, flags); \
788 \
789 return (ssize_t) count; \
790} \
791static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO, \
792 zfcp_sysfs_unit_##_name##_latency_show, \
793 zfcp_sysfs_unit_##_name##_latency_store);
794
795ZFCP_DEFINE_LATENCY_ATTR(read);
796ZFCP_DEFINE_LATENCY_ATTR(write);
797ZFCP_DEFINE_LATENCY_ATTR(cmd);
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/**
800 * ZFCP_DEFINE_SCSI_ATTR
801 * @_name: name of show attribute
802 * @_format: format string
803 * @_value: value to print
804 *
805 * Generates attribute for a unit.
806 */
807#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400808static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 char *buf) \
810{ \
811 struct scsi_device *sdev; \
812 struct zfcp_unit *unit; \
813 \
814 sdev = to_scsi_device(dev); \
815 unit = sdev->hostdata; \
816 return sprintf(buf, _format, _value); \
817} \
818 \
819static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
820
821ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
822ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
823ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
824
825static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
826 &dev_attr_fcp_lun,
827 &dev_attr_wwpn,
828 &dev_attr_hba_id,
Christof Schmitt3a0d9e92008-05-06 11:00:06 +0200829 &dev_attr_read_latency,
830 &dev_attr_write_latency,
831 &dev_attr_cmd_latency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 NULL
833};
834
Swen Schillig6d9d63b2008-04-24 19:35:54 +0200835static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
836 struct device_attribute *attr,
837 char *buf)
838{
839 struct Scsi_Host *scsi_host = dev_to_shost(dev);
840 struct fsf_qtcb_bottom_port *qtcb_port;
841 int retval;
842 struct zfcp_adapter *adapter;
843
844 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
845 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
846 return -EOPNOTSUPP;
847
848 qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
849 if (!qtcb_port)
850 return -ENOMEM;
851
852 retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
853 if (!retval)
854 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
855 qtcb_port->cb_util, qtcb_port->a_util);
856 kfree(qtcb_port);
857 return retval;
858}
859
860static int zfcp_sysfs_adapter_ex_config(struct device *dev,
861 struct fsf_statistics_info *stat_inf)
862{
863 int retval;
864 struct fsf_qtcb_bottom_config *qtcb_config;
865 struct Scsi_Host *scsi_host = dev_to_shost(dev);
866 struct zfcp_adapter *adapter;
867
868 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
869 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
870 return -EOPNOTSUPP;
871
872 qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
873 GFP_KERNEL);
874 if (!qtcb_config)
875 return -ENOMEM;
876
877 retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
878 if (!retval)
879 *stat_inf = qtcb_config->stat_info;
880
881 kfree(qtcb_config);
882 return retval;
883}
884
885static ssize_t zfcp_sysfs_adapter_request_show(struct device *dev,
886 struct device_attribute *attr,
887 char *buf)
888{
889 struct fsf_statistics_info stat_info;
890 int retval;
891
892 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
893 if (retval)
894 return retval;
895
896 return sprintf(buf, "%llu %llu %llu\n",
897 (unsigned long long) stat_info.input_req,
898 (unsigned long long) stat_info.output_req,
899 (unsigned long long) stat_info.control_req);
900}
901
902static ssize_t zfcp_sysfs_adapter_mb_show(struct device *dev,
903 struct device_attribute *attr,
904 char *buf)
905{
906 struct fsf_statistics_info stat_info;
907 int retval;
908
909 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
910 if (retval)
911 return retval;
912
913 return sprintf(buf, "%llu %llu\n",
914 (unsigned long long) stat_info.input_mb,
915 (unsigned long long) stat_info.output_mb);
916}
917
918static ssize_t zfcp_sysfs_adapter_sec_active_show(struct device *dev,
919 struct device_attribute *attr,
920 char *buf)
921{
922 struct fsf_statistics_info stat_info;
923 int retval;
924
925 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
926 if (retval)
927 return retval;
928
929 return sprintf(buf, "%llu\n",
930 (unsigned long long) stat_info.seconds_act);
931}
932
933static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
934static DEVICE_ATTR(requests, S_IRUGO, zfcp_sysfs_adapter_request_show, NULL);
935static DEVICE_ATTR(megabytes, S_IRUGO, zfcp_sysfs_adapter_mb_show, NULL);
936static DEVICE_ATTR(seconds_active, S_IRUGO,
937 zfcp_sysfs_adapter_sec_active_show, NULL);
938
939static struct device_attribute *zfcp_a_stats_attrs[] = {
940 &dev_attr_utilization,
941 &dev_attr_requests,
942 &dev_attr_megabytes,
943 &dev_attr_seconds_active,
944 NULL
945};