blob: 4b4041a9da58173a0f3390687d7cfd2440fb2607 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02002 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02005 * (C) Copyright IBM Corp. 2002, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
11 *
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.
16 *
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.
20 */
21
22#define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "zfcp_ext.h"
25
26static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
27static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
28static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
29static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
30 void (*done) (struct scsi_cmnd *));
31static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
32static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
33static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
34static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020035static int zfcp_task_management_function(struct zfcp_unit *, u8,
36 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int, scsi_id_t,
39 scsi_lun_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static struct device_attribute *zfcp_sysfs_sdev_attrs[];
42
43struct scsi_transport_template *zfcp_transport_template;
44
45struct zfcp_data zfcp_data = {
46 .scsi_host_template = {
47 name: ZFCP_NAME,
48 proc_name: "zfcp",
49 proc_info: NULL,
50 detect: NULL,
51 slave_alloc: zfcp_scsi_slave_alloc,
52 slave_configure: zfcp_scsi_slave_configure,
53 slave_destroy: zfcp_scsi_slave_destroy,
54 queuecommand: zfcp_scsi_queuecommand,
55 eh_abort_handler: zfcp_scsi_eh_abort_handler,
56 eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
57 eh_bus_reset_handler: zfcp_scsi_eh_bus_reset_handler,
58 eh_host_reset_handler: zfcp_scsi_eh_host_reset_handler,
59 /* FIXME(openfcp): Tune */
60 can_queue: 4096,
Andreas Herrmann35dc25852006-03-02 21:28:54 +010061 this_id: -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /*
63 * FIXME:
64 * one less? can zfcp_create_sbale cope with it?
65 */
66 sg_tablesize: ZFCP_MAX_SBALES_PER_REQ,
67 cmd_per_lun: 1,
68 unchecked_isa_dma: 0,
69 use_clustering: 1,
70 sdev_attrs: zfcp_sysfs_sdev_attrs,
71 },
72 .driver_version = ZFCP_VERSION,
73 /* rest initialised with zeros */
74};
75
76/* Find start of Response Information in FCP response unit*/
77char *
78zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
79{
80 char *fcp_rsp_info_ptr;
81
82 fcp_rsp_info_ptr =
83 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
84
85 return fcp_rsp_info_ptr;
86}
87
88/* Find start of Sense Information in FCP response unit*/
89char *
90zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
91{
92 char *fcp_sns_info_ptr;
93
94 fcp_sns_info_ptr =
95 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
96 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
97 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
98 fcp_rsp_iu->fcp_rsp_len;
99
100 return fcp_sns_info_ptr;
101}
102
103fcp_dl_t *
104zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
105{
106 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
107 fcp_dl_t *fcp_dl_addr;
108
109 fcp_dl_addr = (fcp_dl_t *)
110 ((unsigned char *) fcp_cmd +
111 sizeof (struct fcp_cmnd_iu) + additional_length);
112 /*
113 * fcp_dl_addr = start address of fcp_cmnd structure +
114 * size of fixed part + size of dynamically sized add_dcp_cdb field
115 * SEE FCP-2 documentation
116 */
117 return fcp_dl_addr;
118}
119
120fcp_dl_t
121zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
122{
123 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
124}
125
126void
127zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
128{
129 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
130}
131
132/*
133 * note: it's a bit-or operation not an assignment
134 * regarding the specified byte
135 */
136static inline void
137set_byte(u32 * result, char status, char pos)
138{
139 *result |= status << (pos * 8);
140}
141
142void
143set_host_byte(u32 * result, char status)
144{
145 set_byte(result, status, 2);
146}
147
148void
149set_driver_byte(u32 * result, char status)
150{
151 set_byte(result, status, 3);
152}
153
154/*
155 * function: zfcp_scsi_slave_alloc
156 *
157 * purpose:
158 *
159 * returns:
160 */
161
162static int
163zfcp_scsi_slave_alloc(struct scsi_device *sdp)
164{
165 struct zfcp_adapter *adapter;
166 struct zfcp_unit *unit;
167 unsigned long flags;
Andreas Herrmannfb121b02005-12-01 02:49:29 +0100168 int retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
171 if (!adapter)
172 goto out;
173
174 read_lock_irqsave(&zfcp_data.config_lock, flags);
175 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100176 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
177 &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 sdp->hostdata = unit;
179 unit->device = sdp;
180 zfcp_unit_get(unit);
181 retval = 0;
182 }
183 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
184 out:
185 return retval;
186}
187
188/*
189 * function: zfcp_scsi_slave_destroy
190 *
191 * purpose:
192 *
193 * returns:
194 */
195
196static void
197zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
198{
199 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
200
201 if (unit) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100202 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 sdpnt->hostdata = NULL;
204 unit->device = NULL;
205 zfcp_unit_put(unit);
206 } else {
207 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
208 "address %p\n", sdpnt);
209 }
210}
211
212/*
213 * called from scsi midlayer to allow finetuning of a device.
214 */
215static int
216zfcp_scsi_slave_configure(struct scsi_device *sdp)
217{
218 if (sdp->tagged_supported)
219 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
220 else
221 scsi_adjust_queue_depth(sdp, 0, 1);
222 return 0;
223}
224
225/**
226 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
227 * @scpnt: pointer to struct scsi_cmnd where result is set
228 * @result: result to be set in scpnt (e.g. DID_ERROR)
229 */
230static void
231zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
232{
233 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200234 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
235 zfcp_scsi_dbf_event_result("fail", 4,
236 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100237 scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* return directly */
239 scpnt->scsi_done(scpnt);
240}
241
242/**
243 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
244 * zfcp_scsi_command_sync
245 * @adapter: adapter where scsi command is issued
246 * @unit: unit to which scsi command is sent
247 * @scpnt: scsi command to be sent
248 * @timer: timer to be started if request is successfully initiated
249 *
250 * Note: In scsi_done function must be set in scpnt.
251 */
252int
253zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
254 struct scsi_cmnd *scpnt, struct timer_list *timer)
255{
256 int tmp;
257 int retval;
258
259 retval = 0;
260
261 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
262 BUG_ON(scpnt->scsi_done == NULL);
263
264 if (unlikely(NULL == unit)) {
265 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
266 goto out;
267 }
268
269 if (unlikely(
270 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
271 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
272 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
273 "0x%016Lx on adapter %s\n",
274 unit->fcp_lun, unit->port->wwpn,
275 zfcp_get_busid_by_adapter(adapter));
276 zfcp_scsi_command_fail(scpnt, DID_ERROR);
277 goto out;
278 }
279
280 if (unlikely(
281 !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
282 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
283 "on port 0x%016Lx in recovery\n",
284 zfcp_get_busid_by_unit(unit),
285 unit->fcp_lun, unit->port->wwpn);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100286 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 goto out;
288 }
289
290 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
291 ZFCP_REQ_AUTO_CLEANUP);
292
293 if (unlikely(tmp < 0)) {
294 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
295 retval = SCSI_MLQUEUE_HOST_BUSY;
296 }
297
298out:
299 return retval;
300}
301
302void
303zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
304{
305 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
306 complete(wait);
307}
308
309
310/**
311 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
312 * @unit: unit where command is sent to
313 * @scpnt: scsi command to be sent
314 * @timer: timer to be started if request is successfully initiated
315 * Return: 0
316 *
317 * Errors are indicated in scpnt->result
318 */
319int
320zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
321 struct timer_list *timer)
322{
323 int ret;
324 DECLARE_COMPLETION(wait);
325
326 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
327 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
328 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
329 if (ret == 0)
330 wait_for_completion(&wait);
331
332 scpnt->SCp.ptr = NULL;
333
334 return 0;
335}
336
337/*
338 * function: zfcp_scsi_queuecommand
339 *
340 * purpose: enqueues a SCSI command to the specified target device
341 *
342 * returns: 0 - success, SCSI command enqueued
343 * !0 - failure
344 */
345int
346zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
347 void (*done) (struct scsi_cmnd *))
348{
349 struct zfcp_unit *unit;
350 struct zfcp_adapter *adapter;
351
352 /* reset the status for this request */
353 scpnt->result = 0;
354 scpnt->host_scribble = NULL;
355 scpnt->scsi_done = done;
356
357 /*
358 * figure out adapter and target device
359 * (stored there by zfcp_scsi_slave_alloc)
360 */
361 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
362 unit = (struct zfcp_unit *) scpnt->device->hostdata;
363
364 return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
365}
366
367/*
368 * function: zfcp_unit_lookup
369 *
370 * purpose:
371 *
372 * returns:
373 *
374 * context:
375 */
376static struct zfcp_unit *
377zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
378 scsi_lun_t lun)
379{
380 struct zfcp_port *port;
381 struct zfcp_unit *unit, *retval = NULL;
382
383 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700384 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 continue;
386 list_for_each_entry(unit, &port->unit_list_head, list) {
387 if (lun == unit->scsi_lun) {
388 retval = unit;
389 goto out;
390 }
391 }
392 }
393 out:
394 return retval;
395}
396
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200397/**
398 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
399 * @scpnt: pointer to scsi_cmnd to be aborted
400 * Return: SUCCESS - command has been aborted and cleaned up in internal
401 * bookkeeping, SCSI stack won't be called for aborted command
402 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200404 * We do not need to care for a SCSI command which completes normally
405 * but late during this abort routine runs. We are allowed to return
406 * late commands to the SCSI stack. It tracks the state of commands and
407 * will handle late commands. (Usually, the normal completion of late
408 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
410int
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200411zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200413 struct Scsi_Host *scsi_host;
414 struct zfcp_adapter *adapter;
415 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int retval = SUCCESS;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200417 struct zfcp_fsf_req *new_fsf_req = NULL;
418 struct zfcp_fsf_req *old_fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200421 scsi_host = scpnt->device->host;
422 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
423 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
426 scpnt, zfcp_get_busid_by_adapter(adapter));
427
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200428 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 write_lock_irqsave(&adapter->abort_lock, flags);
430
431 /*
432 * Check whether command has just completed and can not be aborted.
433 * Even if the command has just been completed late, we can access
434 * scpnt since the SCSI stack does not release it at least until
435 * this routine returns. (scpnt is parameter passed to this routine
436 * and must not disappear during abort even on late completion.)
437 */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200438 old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (!old_fsf_req) {
440 write_unlock_irqrestore(&adapter->abort_lock, flags);
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100441 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, NULL);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200442 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto out;
444 }
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200445 old_fsf_req->data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200448 /* don't access old_fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 write_unlock_irqrestore(&adapter->abort_lock, flags);
450 /* call FSF routine which does the abort */
451 new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
452 adapter, unit, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!new_fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200454 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100455 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
456 old_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto out;
459 }
460
461 /* wait for completion of abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 __wait_event(new_fsf_req->completion_wq,
463 new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* status should be valid since signals were not permitted */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200466 if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100467 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req,
468 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 retval = SUCCESS;
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200470 } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100471 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req,
472 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100475 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req,
476 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200479 zfcp_fsf_req_free(new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return retval;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * function: zfcp_scsi_eh_device_reset_handler
486 *
487 * purpose:
488 *
489 * returns:
490 */
491int
492zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
493{
494 int retval;
495 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!unit) {
498 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
499 retval = SUCCESS;
500 goto out;
501 }
502 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
503
504 /*
505 * If we do not know whether the unit supports 'logical unit reset'
506 * then try 'logical unit reset' and proceed with 'target reset'
507 * if 'logical unit reset' fails.
508 * If the unit is known not to support 'logical unit reset' then
509 * skip 'logical unit reset' and try 'target reset' immediately.
510 */
511 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
512 &unit->status)) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200513 retval = zfcp_task_management_function(unit,
514 FCP_LOGICAL_UNIT_RESET,
515 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (retval) {
517 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
518 if (retval == -ENOTSUPP)
519 atomic_set_mask
520 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
521 &unit->status);
522 /* fall through and try 'target reset' next */
523 } else {
524 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
525 unit);
526 /* avoid 'target reset' */
527 retval = SUCCESS;
528 goto out;
529 }
530 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200531 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (retval) {
533 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
534 retval = FAILED;
535 } else {
536 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
537 retval = SUCCESS;
538 }
539 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return retval;
541}
542
543static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200544zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
545 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200549 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* issue task management function */
552 fsf_req = zfcp_fsf_send_fcp_command_task_management
553 (adapter, unit, tm_flags, 0);
554 if (!fsf_req) {
555 ZFCP_LOG_INFO("error: creation of task management request "
556 "failed for unit 0x%016Lx on port 0x%016Lx on "
557 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
558 zfcp_get_busid_by_adapter(adapter));
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200559 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 retval = -ENOMEM;
561 goto out;
562 }
563
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200564 __wait_event(fsf_req->completion_wq,
565 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
566
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200567 /*
568 * check completion status of task management function
569 */
570 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
571 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200573 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
574 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200576 } else
577 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200578
579 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 out:
581 return retval;
582}
583
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200584/**
585 * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
587int
588zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
589{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200590 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
591 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 ZFCP_LOG_NORMAL("bus reset because of problems with "
594 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200595 zfcp_erp_adapter_reopen(adapter, 0);
596 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200598 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200601/**
602 * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
604int
605zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
606{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200607 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
608 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 ZFCP_LOG_NORMAL("host reset because of problems with "
611 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200612 zfcp_erp_adapter_reopen(adapter, 0);
613 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200615 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618/*
619 * function:
620 *
621 * purpose:
622 *
623 * returns:
624 */
625int
626zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
627{
628 int retval = 0;
629 static unsigned int unique_id = 0;
630
631 /* register adapter as SCSI host with mid layer of SCSI stack */
632 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
633 sizeof (struct zfcp_adapter *));
634 if (!adapter->scsi_host) {
635 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
636 "for adapter %s ",
637 zfcp_get_busid_by_adapter(adapter));
638 retval = -EIO;
639 goto out;
640 }
641 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
642
643 /* tell the SCSI stack some characteristics of this adapter */
644 adapter->scsi_host->max_id = 1;
645 adapter->scsi_host->max_lun = 1;
646 adapter->scsi_host->max_channel = 0;
647 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
648 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
649 adapter->scsi_host->transportt = zfcp_transport_template;
650 /*
651 * Reverse mapping of the host number to avoid race condition
652 */
653 adapter->scsi_host_no = adapter->scsi_host->host_no;
654
655 /*
656 * save a pointer to our own adapter data structure within
657 * hostdata field of SCSI host data structure
658 */
659 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
660
661 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
662 scsi_host_put(adapter->scsi_host);
663 retval = -EIO;
664 goto out;
665 }
666 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
667 out:
668 return retval;
669}
670
671/*
672 * function:
673 *
674 * purpose:
675 *
676 * returns:
677 */
678void
679zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
680{
681 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200682 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 shost = adapter->scsi_host;
685 if (!shost)
686 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200687 read_lock_irq(&zfcp_data.config_lock);
688 list_for_each_entry(port, &adapter->port_list_head, list)
689 if (port->rport)
690 port->rport = NULL;
691 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700692 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 scsi_remove_host(shost);
694 scsi_host_put(shost);
695 adapter->scsi_host = NULL;
696 adapter->scsi_host_no = 0;
697 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
698
699 return;
700}
701
702
703void
704zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
705{
706 adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
707 adapter->scsi_er_timer.data = (unsigned long) adapter;
708 adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
709 add_timer(&adapter->scsi_er_timer);
710}
711
712/*
713 * Support functions for FC transport class
714 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100715static struct fc_host_statistics*
716zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100718 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100720 if (!adapter->fc_stats) {
721 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
722 if (!fc_stats)
723 return NULL;
724 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
725 }
726 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
727 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100731zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
732 struct fsf_qtcb_bottom_port *data,
733 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100735 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
736 old->seconds_since_last_reset;
737 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
738 fc_stats->tx_words = data->tx_words - old->tx_words;
739 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
740 fc_stats->rx_words = data->rx_words - old->rx_words;
741 fc_stats->lip_count = data->lip - old->lip;
742 fc_stats->nos_count = data->nos - old->nos;
743 fc_stats->error_frames = data->error_frames - old->error_frames;
744 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
745 fc_stats->link_failure_count = data->link_failure - old->link_failure;
746 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
747 fc_stats->loss_of_signal_count = data->loss_of_signal -
748 old->loss_of_signal;
749 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
750 old->psp_error_counts;
751 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
752 old->invalid_tx_words;
753 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
754 fc_stats->fcp_input_requests = data->input_requests -
755 old->input_requests;
756 fc_stats->fcp_output_requests = data->output_requests -
757 old->output_requests;
758 fc_stats->fcp_control_requests = data->control_requests -
759 old->control_requests;
760 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
761 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100765zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
766 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100768 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
769 fc_stats->tx_frames = data->tx_frames;
770 fc_stats->tx_words = data->tx_words;
771 fc_stats->rx_frames = data->rx_frames;
772 fc_stats->rx_words = data->rx_words;
773 fc_stats->lip_count = data->lip;
774 fc_stats->nos_count = data->nos;
775 fc_stats->error_frames = data->error_frames;
776 fc_stats->dumped_frames = data->dumped_frames;
777 fc_stats->link_failure_count = data->link_failure;
778 fc_stats->loss_of_sync_count = data->loss_of_sync;
779 fc_stats->loss_of_signal_count = data->loss_of_signal;
780 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
781 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
782 fc_stats->invalid_crc_count = data->invalid_crcs;
783 fc_stats->fcp_input_requests = data->input_requests;
784 fc_stats->fcp_output_requests = data->output_requests;
785 fc_stats->fcp_control_requests = data->control_requests;
786 fc_stats->fcp_input_megabytes = data->input_mb;
787 fc_stats->fcp_output_megabytes = data->output_mb;
788}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100790/**
791 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
792 *
793 * assumption: scsi_transport_fc synchronizes calls of
794 * get_fc_host_stats and reset_fc_host_stats
795 * (XXX to be checked otherwise introduce locking)
796 */
797static struct fc_host_statistics *
798zfcp_get_fc_host_stats(struct Scsi_Host *shost)
799{
800 struct zfcp_adapter *adapter;
801 struct fc_host_statistics *fc_stats;
802 struct fsf_qtcb_bottom_port *data;
803 int ret;
804
805 adapter = (struct zfcp_adapter *)shost->hostdata[0];
806 fc_stats = zfcp_init_fc_host_stats(adapter);
807 if (!fc_stats)
808 return NULL;
809
810 data = kmalloc(sizeof(*data), GFP_KERNEL);
811 if (!data)
812 return NULL;
813 memset(data, 0, sizeof(*data));
814
815 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
816 if (ret) {
817 kfree(data);
818 return NULL; /* XXX return zeroed fc_stats? */
819 }
820
821 if (adapter->stats_reset &&
822 ((jiffies/HZ - adapter->stats_reset) <
823 data->seconds_since_last_reset)) {
824 zfcp_adjust_fc_host_stats(fc_stats, data,
825 adapter->stats_reset_data);
826 } else
827 zfcp_set_fc_host_stats(fc_stats, data);
828
829 kfree(data);
830 return fc_stats;
831}
832
833static void
834zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
835{
836 struct zfcp_adapter *adapter;
837 struct fsf_qtcb_bottom_port *data, *old_data;
838 int ret;
839
840 adapter = (struct zfcp_adapter *)shost->hostdata[0];
841 data = kmalloc(sizeof(*data), GFP_KERNEL);
842 if (!data)
843 return;
844 memset(data, 0, sizeof(*data));
845
846 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
847 if (ret == 0) {
848 adapter->stats_reset = jiffies/HZ;
849 old_data = adapter->stats_reset_data;
850 adapter->stats_reset_data = data; /* finally freed in
851 adater_dequeue */
852 kfree(old_data);
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 .show_starget_port_id = 1,
858 .show_starget_port_name = 1,
859 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700860 .show_rport_supported_classes = 1,
861 .show_host_node_name = 1,
862 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100863 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700864 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100865 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200866 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700867 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100868 .get_fc_host_stats = zfcp_get_fc_host_stats,
869 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
870 /* no functions registered for following dynamic attributes but
871 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100872 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200873 .show_host_speed = 1,
874 .show_host_port_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875};
876
877/**
878 * ZFCP_DEFINE_SCSI_ATTR
879 * @_name: name of show attribute
880 * @_format: format string
881 * @_value: value to print
882 *
883 * Generates attribute for a unit.
884 */
885#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400886static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 char *buf) \
888{ \
889 struct scsi_device *sdev; \
890 struct zfcp_unit *unit; \
891 \
892 sdev = to_scsi_device(dev); \
893 unit = sdev->hostdata; \
894 return sprintf(buf, _format, _value); \
895} \
896 \
897static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
898
899ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
900ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
901ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
902
903static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
904 &dev_attr_fcp_lun,
905 &dev_attr_wwpn,
906 &dev_attr_hba_id,
907 NULL
908};
909
910#undef ZFCP_LOG_AREA