blob: 160307382d2c2c9a7f46acef22cef48311669aae [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int
111zfcp_scsi_slave_alloc(struct scsi_device *sdp)
112{
113 struct zfcp_adapter *adapter;
114 struct zfcp_unit *unit;
115 unsigned long flags;
Andreas Herrmannfb121b02005-12-01 02:49:29 +0100116 int retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
119 if (!adapter)
120 goto out;
121
122 read_lock_irqsave(&zfcp_data.config_lock, flags);
123 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100124 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
125 &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 sdp->hostdata = unit;
127 unit->device = sdp;
128 zfcp_unit_get(unit);
129 retval = 0;
130 }
131 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
132 out:
133 return retval;
134}
135
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200136/**
137 * zfcp_scsi_slave_destroy - called when scsi device is removed
138 *
139 * Remove reference to associated scsi device for an zfcp_unit.
140 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
141 * or a scan for this device might have failed.
142 */
143static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
Christof Schmitt553448f2008-06-10 18:20:58 +0200146 WARN_ON(!unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (unit) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100148 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 sdpnt->hostdata = NULL;
150 unit->device = NULL;
Martin Peschke1f6f7122008-04-18 12:51:55 +0200151 zfcp_erp_unit_failed(unit, 12, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 zfcp_unit_put(unit);
Christof Schmitt553448f2008-06-10 18:20:58 +0200153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * called from scsi midlayer to allow finetuning of a device.
158 */
159static int
160zfcp_scsi_slave_configure(struct scsi_device *sdp)
161{
162 if (sdp->tagged_supported)
163 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
164 else
165 scsi_adjust_queue_depth(sdp, 0, 1);
166 return 0;
167}
168
169/**
170 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
171 * @scpnt: pointer to struct scsi_cmnd where result is set
172 * @result: result to be set in scpnt (e.g. DID_ERROR)
173 */
174static void
175zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
176{
Martin Petermannfeac6a02008-07-02 10:56:35 +0200177 set_host_byte(scpnt, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200178 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
179 zfcp_scsi_dbf_event_result("fail", 4,
180 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100181 scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* return directly */
183 scpnt->scsi_done(scpnt);
184}
185
186/**
187 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
188 * zfcp_scsi_command_sync
189 * @adapter: adapter where scsi command is issued
190 * @unit: unit to which scsi command is sent
191 * @scpnt: scsi command to be sent
192 * @timer: timer to be started if request is successfully initiated
193 *
194 * Note: In scsi_done function must be set in scpnt.
195 */
196int
197zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200198 struct scsi_cmnd *scpnt, int use_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int tmp;
201 int retval;
202
203 retval = 0;
204
205 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
206 BUG_ON(scpnt->scsi_done == NULL);
207
208 if (unlikely(NULL == unit)) {
209 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
210 goto out;
211 }
212
213 if (unlikely(
214 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
215 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 zfcp_scsi_command_fail(scpnt, DID_ERROR);
217 goto out;
218 }
219
Christof Schmittba172422007-12-20 12:30:26 +0100220 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
221 ZFCP_REQ_AUTO_CLEANUP);
222 if (unlikely(tmp == -EBUSY)) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100223 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto out;
225 }
226
Christof Schmitt553448f2008-06-10 18:20:58 +0200227 if (unlikely(tmp < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 retval = SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230out:
231 return retval;
232}
233
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100234static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
236{
237 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
238 complete(wait);
239}
240
241
242/**
243 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
244 * @unit: unit where command is sent to
245 * @scpnt: scsi command to be sent
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200246 * @use_timer: indicates whether timer should be setup or not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * Return: 0
248 *
249 * Errors are indicated in scpnt->result
250 */
251int
252zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200253 int use_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int ret;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700256 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
259 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200260 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
261 use_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (ret == 0)
263 wait_for_completion(&wait);
264
265 scpnt->SCp.ptr = NULL;
266
267 return 0;
268}
269
270/*
271 * function: zfcp_scsi_queuecommand
272 *
273 * purpose: enqueues a SCSI command to the specified target device
274 *
275 * returns: 0 - success, SCSI command enqueued
276 * !0 - failure
277 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100278static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
280 void (*done) (struct scsi_cmnd *))
281{
282 struct zfcp_unit *unit;
283 struct zfcp_adapter *adapter;
284
285 /* reset the status for this request */
286 scpnt->result = 0;
287 scpnt->host_scribble = NULL;
288 scpnt->scsi_done = done;
289
290 /*
291 * figure out adapter and target device
292 * (stored there by zfcp_scsi_slave_alloc)
293 */
294 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
295 unit = (struct zfcp_unit *) scpnt->device->hostdata;
296
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200297 return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static struct zfcp_unit *
Andreas Herrmann06506d02006-05-22 18:18:19 +0200301zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
302 unsigned int lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct zfcp_port *port;
305 struct zfcp_unit *unit, *retval = NULL;
306
307 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700308 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 continue;
Christof Schmitt6b76a722007-08-28 09:30:59 +0200310 list_for_each_entry(unit, &port->unit_list_head, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (lun == unit->scsi_lun) {
312 retval = unit;
313 goto out;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 out:
317 return retval;
318}
319
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200320/**
321 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200322 * @scpnt: pointer to scsi_cmnd to be aborted
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200323 * Return: SUCCESS - command has been aborted and cleaned up in internal
324 * bookkeeping, SCSI stack won't be called for aborted command
325 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200327 * We do not need to care for a SCSI command which completes normally
328 * but late during this abort routine runs. We are allowed to return
329 * late commands to the SCSI stack. It tracks the state of commands and
330 * will handle late commands. (Usually, the normal completion of late
331 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100333static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200335 struct Scsi_Host *scsi_host;
336 struct zfcp_adapter *adapter;
337 struct zfcp_unit *unit;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200338 struct zfcp_fsf_req *fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 unsigned long flags;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200340 unsigned long old_req_id;
341 int retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200343 scsi_host = scpnt->device->host;
344 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
345 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200347 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 write_lock_irqsave(&adapter->abort_lock, flags);
349
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200350 /* Check whether corresponding fsf_req is still pending */
351 spin_lock(&adapter->req_list_lock);
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200352 fsf_req = zfcp_reqlist_find(adapter,
353 (unsigned long) scpnt->host_scribble);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200354 spin_unlock(&adapter->req_list_lock);
355 if (!fsf_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200357 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200358 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto out;
360 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200361 fsf_req->data = 0;
362 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
363 old_req_id = fsf_req->req_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200365 /* don't access old fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200367
368 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
369 if (!fsf_req) {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100370 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200371 old_req_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto out;
374 }
375
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200376 __wait_event(fsf_req->completion_wq,
377 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200378
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200379 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
380 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 retval = SUCCESS;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200382 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
383 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 } else {
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200386 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200389 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return retval;
392}
393
Christof Schmitt74b2e042008-03-03 12:19:28 +0100394static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 int retval;
Christof Schmitt74b2e042008-03-03 12:19:28 +0100397 struct zfcp_unit *unit = scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (!unit) {
Christof Schmitt74b2e042008-03-03 12:19:28 +0100400 WARN_ON(1);
401 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Christof Schmitt74b2e042008-03-03 12:19:28 +0100403 retval = zfcp_task_management_function(unit,
404 FCP_LOGICAL_UNIT_RESET,
405 scpnt);
406 return retval ? FAILED : SUCCESS;
407}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Christof Schmitt74b2e042008-03-03 12:19:28 +0100409static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
410{
411 int retval;
412 struct zfcp_unit *unit = scpnt->device->hostdata;
413
414 if (!unit) {
415 WARN_ON(1);
416 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200418 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Christof Schmitt74b2e042008-03-03 12:19:28 +0100419 return retval ? FAILED : SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200423zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
424 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200428 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* issue task management function */
431 fsf_req = zfcp_fsf_send_fcp_command_task_management
432 (adapter, unit, tm_flags, 0);
433 if (!fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200434 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 retval = -ENOMEM;
436 goto out;
437 }
438
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200439 __wait_event(fsf_req->completion_wq,
440 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
441
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200442 /*
443 * check completion status of task management function
444 */
445 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
446 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200448 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
449 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200451 } else
452 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200453
454 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 out:
456 return retval;
457}
458
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200459/**
Christof Schmitta11b4742007-11-05 12:37:43 +0100460 * zfcp_scsi_eh_host_reset_handler - handler for host reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100462static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200464 struct zfcp_unit *unit;
465 struct zfcp_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200467 unit = (struct zfcp_unit*) scpnt->device->hostdata;
468 adapter = unit->port->adapter;
Martin Peschke1f6f7122008-04-18 12:51:55 +0200469 zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
Andreas Herrmann81654282006-09-18 22:30:36 +0200470 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200472 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475int
476zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
477{
478 int retval = 0;
479 static unsigned int unique_id = 0;
480
Michael Loehr9f287452007-05-09 11:01:24 +0200481 if (adapter->scsi_host)
482 goto out;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /* register adapter as SCSI host with mid layer of SCSI stack */
485 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
486 sizeof (struct zfcp_adapter *));
487 if (!adapter->scsi_host) {
Christof Schmitt553448f2008-06-10 18:20:58 +0200488 dev_err(&adapter->ccw_device->dev,
489 "registration with SCSI stack failed.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 retval = -EIO;
491 goto out;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* tell the SCSI stack some characteristics of this adapter */
495 adapter->scsi_host->max_id = 1;
496 adapter->scsi_host->max_lun = 1;
497 adapter->scsi_host->max_channel = 0;
498 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
499 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200500 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * save a pointer to our own adapter data structure within
504 * hostdata field of SCSI host data structure
505 */
506 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
507
508 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
509 scsi_host_put(adapter->scsi_host);
510 retval = -EIO;
511 goto out;
512 }
513 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
514 out:
515 return retval;
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518void
519zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
520{
521 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200522 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 shost = adapter->scsi_host;
525 if (!shost)
526 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200527 read_lock_irq(&zfcp_data.config_lock);
528 list_for_each_entry(port, &adapter->port_list_head, list)
529 if (port->rport)
530 port->rport = NULL;
531 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700532 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 scsi_remove_host(shost);
534 scsi_host_put(shost);
535 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
537
538 return;
539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*
542 * Support functions for FC transport class
543 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100544static struct fc_host_statistics*
545zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100547 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100549 if (!adapter->fc_stats) {
550 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
551 if (!fc_stats)
552 return NULL;
553 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
554 }
555 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
556 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100560zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
561 struct fsf_qtcb_bottom_port *data,
562 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100564 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
565 old->seconds_since_last_reset;
566 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
567 fc_stats->tx_words = data->tx_words - old->tx_words;
568 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
569 fc_stats->rx_words = data->rx_words - old->rx_words;
570 fc_stats->lip_count = data->lip - old->lip;
571 fc_stats->nos_count = data->nos - old->nos;
572 fc_stats->error_frames = data->error_frames - old->error_frames;
573 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
574 fc_stats->link_failure_count = data->link_failure - old->link_failure;
575 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
576 fc_stats->loss_of_signal_count = data->loss_of_signal -
577 old->loss_of_signal;
578 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
579 old->psp_error_counts;
580 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
581 old->invalid_tx_words;
582 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
583 fc_stats->fcp_input_requests = data->input_requests -
584 old->input_requests;
585 fc_stats->fcp_output_requests = data->output_requests -
586 old->output_requests;
587 fc_stats->fcp_control_requests = data->control_requests -
588 old->control_requests;
589 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
590 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100594zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
595 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100597 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
598 fc_stats->tx_frames = data->tx_frames;
599 fc_stats->tx_words = data->tx_words;
600 fc_stats->rx_frames = data->rx_frames;
601 fc_stats->rx_words = data->rx_words;
602 fc_stats->lip_count = data->lip;
603 fc_stats->nos_count = data->nos;
604 fc_stats->error_frames = data->error_frames;
605 fc_stats->dumped_frames = data->dumped_frames;
606 fc_stats->link_failure_count = data->link_failure;
607 fc_stats->loss_of_sync_count = data->loss_of_sync;
608 fc_stats->loss_of_signal_count = data->loss_of_signal;
609 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
610 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
611 fc_stats->invalid_crc_count = data->invalid_crcs;
612 fc_stats->fcp_input_requests = data->input_requests;
613 fc_stats->fcp_output_requests = data->output_requests;
614 fc_stats->fcp_control_requests = data->control_requests;
615 fc_stats->fcp_input_megabytes = data->input_mb;
616 fc_stats->fcp_output_megabytes = data->output_mb;
617}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100619/**
620 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
621 *
622 * assumption: scsi_transport_fc synchronizes calls of
623 * get_fc_host_stats and reset_fc_host_stats
624 * (XXX to be checked otherwise introduce locking)
625 */
626static struct fc_host_statistics *
627zfcp_get_fc_host_stats(struct Scsi_Host *shost)
628{
629 struct zfcp_adapter *adapter;
630 struct fc_host_statistics *fc_stats;
631 struct fsf_qtcb_bottom_port *data;
632 int ret;
633
634 adapter = (struct zfcp_adapter *)shost->hostdata[0];
635 fc_stats = zfcp_init_fc_host_stats(adapter);
636 if (!fc_stats)
637 return NULL;
638
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200639 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100640 if (!data)
641 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100642
Swen Schillig52ef11a2007-08-28 09:31:09 +0200643 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100644 if (ret) {
645 kfree(data);
646 return NULL; /* XXX return zeroed fc_stats? */
647 }
648
649 if (adapter->stats_reset &&
650 ((jiffies/HZ - adapter->stats_reset) <
651 data->seconds_since_last_reset)) {
652 zfcp_adjust_fc_host_stats(fc_stats, data,
653 adapter->stats_reset_data);
654 } else
655 zfcp_set_fc_host_stats(fc_stats, data);
656
657 kfree(data);
658 return fc_stats;
659}
660
661static void
662zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
663{
664 struct zfcp_adapter *adapter;
665 struct fsf_qtcb_bottom_port *data, *old_data;
666 int ret;
667
668 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200669 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100670 if (!data)
671 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100672
Swen Schillig52ef11a2007-08-28 09:31:09 +0200673 ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
Heiko Carstens83f6d6d2007-08-08 10:47:02 +0200674 if (ret) {
675 kfree(data);
676 } else {
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100677 adapter->stats_reset = jiffies/HZ;
678 old_data = adapter->stats_reset_data;
679 adapter->stats_reset_data = data; /* finally freed in
680 adater_dequeue */
681 kfree(old_data);
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Sven Schuetz85a82392008-06-10 18:20:59 +0200685static void zfcp_get_host_port_state(struct Scsi_Host *shost)
686{
687 struct zfcp_adapter *adapter =
688 (struct zfcp_adapter *)shost->hostdata[0];
689 int status = atomic_read(&adapter->status);
690
691 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
692 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
693 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
694 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
695 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
696 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
697 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
698 else
699 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
700}
701
Andreas Herrmann338151e2006-05-22 18:25:56 +0200702static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
703{
704 rport->dev_loss_tmo = timeout;
705}
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 .show_starget_port_id = 1,
709 .show_starget_port_name = 1,
710 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700711 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200712 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200713 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700714 .show_host_node_name = 1,
715 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100716 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700717 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100718 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200719 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700720 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100721 .get_fc_host_stats = zfcp_get_fc_host_stats,
722 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200723 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
Sven Schuetz85a82392008-06-10 18:20:59 +0200724 .get_host_port_state = zfcp_get_host_port_state,
725 .show_host_port_state = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100726 /* no functions registered for following dynamic attributes but
727 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100728 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200729 .show_host_speed = 1,
730 .show_host_port_id = 1,
Swen Schillig52ef11a2007-08-28 09:31:09 +0200731 .disable_target_scan = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732};
733
Christof Schmitt3a0d9e92008-05-06 11:00:06 +0200734#define ZFCP_DEFINE_LATENCY_ATTR(_name) \
735static ssize_t \
736zfcp_sysfs_unit_##_name##_latency_show(struct device *dev, \
737 struct device_attribute *attr, \
738 char *buf) { \
739 struct scsi_device *sdev = to_scsi_device(dev); \
740 struct zfcp_unit *unit = sdev->hostdata; \
741 struct zfcp_latencies *lat = &unit->latencies; \
742 struct zfcp_adapter *adapter = unit->port->adapter; \
743 unsigned long flags; \
744 unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc; \
745 \
746 spin_lock_irqsave(&lat->lock, flags); \
747 fsum = lat->_name.fabric.sum * adapter->timer_ticks; \
748 fmin = lat->_name.fabric.min * adapter->timer_ticks; \
749 fmax = lat->_name.fabric.max * adapter->timer_ticks; \
750 csum = lat->_name.channel.sum * adapter->timer_ticks; \
751 cmin = lat->_name.channel.min * adapter->timer_ticks; \
752 cmax = lat->_name.channel.max * adapter->timer_ticks; \
753 cc = lat->_name.counter; \
754 spin_unlock_irqrestore(&lat->lock, flags); \
755 \
756 do_div(fsum, 1000); \
757 do_div(fmin, 1000); \
758 do_div(fmax, 1000); \
759 do_div(csum, 1000); \
760 do_div(cmin, 1000); \
761 do_div(cmax, 1000); \
762 \
763 return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n", \
764 fmin, fmax, fsum, cmin, cmax, csum, cc); \
765} \
766static ssize_t \
767zfcp_sysfs_unit_##_name##_latency_store(struct device *dev, \
768 struct device_attribute *attr, \
769 const char *buf, size_t count) \
770{ \
771 struct scsi_device *sdev = to_scsi_device(dev); \
772 struct zfcp_unit *unit = sdev->hostdata; \
773 struct zfcp_latencies *lat = &unit->latencies; \
774 unsigned long flags; \
775 \
776 spin_lock_irqsave(&lat->lock, flags); \
777 lat->_name.fabric.sum = 0; \
778 lat->_name.fabric.min = 0xFFFFFFFF; \
779 lat->_name.fabric.max = 0; \
780 lat->_name.channel.sum = 0; \
781 lat->_name.channel.min = 0xFFFFFFFF; \
782 lat->_name.channel.max = 0; \
783 lat->_name.counter = 0; \
784 spin_unlock_irqrestore(&lat->lock, flags); \
785 \
786 return (ssize_t) count; \
787} \
788static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO, \
789 zfcp_sysfs_unit_##_name##_latency_show, \
790 zfcp_sysfs_unit_##_name##_latency_store);
791
792ZFCP_DEFINE_LATENCY_ATTR(read);
793ZFCP_DEFINE_LATENCY_ATTR(write);
794ZFCP_DEFINE_LATENCY_ATTR(cmd);
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/**
797 * ZFCP_DEFINE_SCSI_ATTR
798 * @_name: name of show attribute
799 * @_format: format string
800 * @_value: value to print
801 *
802 * Generates attribute for a unit.
803 */
804#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400805static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 char *buf) \
807{ \
808 struct scsi_device *sdev; \
809 struct zfcp_unit *unit; \
810 \
811 sdev = to_scsi_device(dev); \
812 unit = sdev->hostdata; \
813 return sprintf(buf, _format, _value); \
814} \
815 \
816static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
817
818ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
819ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
820ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
821
822static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
823 &dev_attr_fcp_lun,
824 &dev_attr_wwpn,
825 &dev_attr_hba_id,
Christof Schmitt3a0d9e92008-05-06 11:00:06 +0200826 &dev_attr_read_latency,
827 &dev_attr_write_latency,
828 &dev_attr_cmd_latency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 NULL
830};
831
Swen Schillig6d9d63b2008-04-24 19:35:54 +0200832static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
833 struct device_attribute *attr,
834 char *buf)
835{
836 struct Scsi_Host *scsi_host = dev_to_shost(dev);
837 struct fsf_qtcb_bottom_port *qtcb_port;
838 int retval;
839 struct zfcp_adapter *adapter;
840
841 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
842 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
843 return -EOPNOTSUPP;
844
845 qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
846 if (!qtcb_port)
847 return -ENOMEM;
848
849 retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
850 if (!retval)
851 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
852 qtcb_port->cb_util, qtcb_port->a_util);
853 kfree(qtcb_port);
854 return retval;
855}
856
857static int zfcp_sysfs_adapter_ex_config(struct device *dev,
858 struct fsf_statistics_info *stat_inf)
859{
860 int retval;
861 struct fsf_qtcb_bottom_config *qtcb_config;
862 struct Scsi_Host *scsi_host = dev_to_shost(dev);
863 struct zfcp_adapter *adapter;
864
865 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
866 if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
867 return -EOPNOTSUPP;
868
869 qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
870 GFP_KERNEL);
871 if (!qtcb_config)
872 return -ENOMEM;
873
874 retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
875 if (!retval)
876 *stat_inf = qtcb_config->stat_info;
877
878 kfree(qtcb_config);
879 return retval;
880}
881
882static ssize_t zfcp_sysfs_adapter_request_show(struct device *dev,
883 struct device_attribute *attr,
884 char *buf)
885{
886 struct fsf_statistics_info stat_info;
887 int retval;
888
889 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
890 if (retval)
891 return retval;
892
893 return sprintf(buf, "%llu %llu %llu\n",
894 (unsigned long long) stat_info.input_req,
895 (unsigned long long) stat_info.output_req,
896 (unsigned long long) stat_info.control_req);
897}
898
899static ssize_t zfcp_sysfs_adapter_mb_show(struct device *dev,
900 struct device_attribute *attr,
901 char *buf)
902{
903 struct fsf_statistics_info stat_info;
904 int retval;
905
906 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
907 if (retval)
908 return retval;
909
910 return sprintf(buf, "%llu %llu\n",
911 (unsigned long long) stat_info.input_mb,
912 (unsigned long long) stat_info.output_mb);
913}
914
915static ssize_t zfcp_sysfs_adapter_sec_active_show(struct device *dev,
916 struct device_attribute *attr,
917 char *buf)
918{
919 struct fsf_statistics_info stat_info;
920 int retval;
921
922 retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);
923 if (retval)
924 return retval;
925
926 return sprintf(buf, "%llu\n",
927 (unsigned long long) stat_info.seconds_act);
928}
929
930static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
931static DEVICE_ATTR(requests, S_IRUGO, zfcp_sysfs_adapter_request_show, NULL);
932static DEVICE_ATTR(megabytes, S_IRUGO, zfcp_sysfs_adapter_mb_show, NULL);
933static DEVICE_ATTR(seconds_active, S_IRUGO,
934 zfcp_sysfs_adapter_sec_active_show, NULL);
935
936static struct device_attribute *zfcp_a_stats_attrs[] = {
937 &dev_attr_utilization,
938 &dev_attr_requests,
939 &dev_attr_megabytes,
940 &dev_attr_seconds_active,
941 NULL
942};