blob: 1bb55086db9f423ca8ab76849e57f24791c16106 [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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020034static int zfcp_task_management_function(struct zfcp_unit *, u8,
35 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Andreas Herrmann06506d02006-05-22 18:18:19 +020037static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
38 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static struct device_attribute *zfcp_sysfs_sdev_attrs[];
41
42struct scsi_transport_template *zfcp_transport_template;
43
44struct zfcp_data zfcp_data = {
45 .scsi_host_template = {
Heiko Carstensd2c993d2006-07-12 16:41:55 +020046 .name = ZFCP_NAME,
47 .proc_name = "zfcp",
Heiko Carstensd2c993d2006-07-12 16:41:55 +020048 .slave_alloc = zfcp_scsi_slave_alloc,
49 .slave_configure = zfcp_scsi_slave_configure,
50 .slave_destroy = zfcp_scsi_slave_destroy,
51 .queuecommand = zfcp_scsi_queuecommand,
52 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
53 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +020054 .eh_bus_reset_handler = zfcp_scsi_eh_host_reset_handler,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020055 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
56 .can_queue = 4096,
57 .this_id = -1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020058 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
59 .cmd_per_lun = 1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020060 .use_clustering = 1,
61 .sdev_attrs = zfcp_sysfs_sdev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 },
63 .driver_version = ZFCP_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66/* Find start of Response Information in FCP response unit*/
67char *
68zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
69{
70 char *fcp_rsp_info_ptr;
71
72 fcp_rsp_info_ptr =
73 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
74
75 return fcp_rsp_info_ptr;
76}
77
78/* Find start of Sense Information in FCP response unit*/
79char *
80zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
81{
82 char *fcp_sns_info_ptr;
83
84 fcp_sns_info_ptr =
85 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
86 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
87 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
88 fcp_rsp_iu->fcp_rsp_len;
89
90 return fcp_sns_info_ptr;
91}
92
93fcp_dl_t *
94zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
95{
96 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
97 fcp_dl_t *fcp_dl_addr;
98
99 fcp_dl_addr = (fcp_dl_t *)
100 ((unsigned char *) fcp_cmd +
101 sizeof (struct fcp_cmnd_iu) + additional_length);
102 /*
103 * fcp_dl_addr = start address of fcp_cmnd structure +
104 * size of fixed part + size of dynamically sized add_dcp_cdb field
105 * SEE FCP-2 documentation
106 */
107 return fcp_dl_addr;
108}
109
110fcp_dl_t
111zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
112{
113 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
114}
115
116void
117zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
118{
119 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
120}
121
122/*
123 * note: it's a bit-or operation not an assignment
124 * regarding the specified byte
125 */
126static inline void
127set_byte(u32 * result, char status, char pos)
128{
129 *result |= status << (pos * 8);
130}
131
132void
133set_host_byte(u32 * result, char status)
134{
135 set_byte(result, status, 2);
136}
137
138void
139set_driver_byte(u32 * result, char status)
140{
141 set_byte(result, status, 3);
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int
145zfcp_scsi_slave_alloc(struct scsi_device *sdp)
146{
147 struct zfcp_adapter *adapter;
148 struct zfcp_unit *unit;
149 unsigned long flags;
Andreas Herrmannfb121b02005-12-01 02:49:29 +0100150 int retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
153 if (!adapter)
154 goto out;
155
156 read_lock_irqsave(&zfcp_data.config_lock, flags);
157 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100158 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
159 &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 sdp->hostdata = unit;
161 unit->device = sdp;
162 zfcp_unit_get(unit);
163 retval = 0;
164 }
165 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
166 out:
167 return retval;
168}
169
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200170/**
171 * zfcp_scsi_slave_destroy - called when scsi device is removed
172 *
173 * Remove reference to associated scsi device for an zfcp_unit.
174 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
175 * or a scan for this device might have failed.
176 */
177static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
180
181 if (unit) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100182 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 sdpnt->hostdata = NULL;
184 unit->device = NULL;
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200185 zfcp_erp_unit_failed(unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 zfcp_unit_put(unit);
187 } else {
188 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
189 "address %p\n", sdpnt);
190 }
191}
192
193/*
194 * called from scsi midlayer to allow finetuning of a device.
195 */
196static int
197zfcp_scsi_slave_configure(struct scsi_device *sdp)
198{
199 if (sdp->tagged_supported)
200 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
201 else
202 scsi_adjust_queue_depth(sdp, 0, 1);
203 return 0;
204}
205
206/**
207 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
208 * @scpnt: pointer to struct scsi_cmnd where result is set
209 * @result: result to be set in scpnt (e.g. DID_ERROR)
210 */
211static void
212zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
213{
214 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200215 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
216 zfcp_scsi_dbf_event_result("fail", 4,
217 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100218 scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* return directly */
220 scpnt->scsi_done(scpnt);
221}
222
223/**
224 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
225 * zfcp_scsi_command_sync
226 * @adapter: adapter where scsi command is issued
227 * @unit: unit to which scsi command is sent
228 * @scpnt: scsi command to be sent
229 * @timer: timer to be started if request is successfully initiated
230 *
231 * Note: In scsi_done function must be set in scpnt.
232 */
233int
234zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
235 struct scsi_cmnd *scpnt, struct timer_list *timer)
236{
237 int tmp;
238 int retval;
239
240 retval = 0;
241
242 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
243 BUG_ON(scpnt->scsi_done == NULL);
244
245 if (unlikely(NULL == unit)) {
246 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
247 goto out;
248 }
249
250 if (unlikely(
251 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
252 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
253 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
254 "0x%016Lx on adapter %s\n",
255 unit->fcp_lun, unit->port->wwpn,
256 zfcp_get_busid_by_adapter(adapter));
257 zfcp_scsi_command_fail(scpnt, DID_ERROR);
258 goto out;
259 }
260
261 if (unlikely(
262 !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
263 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
264 "on port 0x%016Lx in recovery\n",
265 zfcp_get_busid_by_unit(unit),
266 unit->fcp_lun, unit->port->wwpn);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100267 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto out;
269 }
270
271 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
272 ZFCP_REQ_AUTO_CLEANUP);
273
274 if (unlikely(tmp < 0)) {
275 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
276 retval = SCSI_MLQUEUE_HOST_BUSY;
277 }
278
279out:
280 return retval;
281}
282
283void
284zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
285{
286 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
287 complete(wait);
288}
289
290
291/**
292 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
293 * @unit: unit where command is sent to
294 * @scpnt: scsi command to be sent
295 * @timer: timer to be started if request is successfully initiated
296 * Return: 0
297 *
298 * Errors are indicated in scpnt->result
299 */
300int
301zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
302 struct timer_list *timer)
303{
304 int ret;
305 DECLARE_COMPLETION(wait);
306
307 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
308 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
309 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
310 if (ret == 0)
311 wait_for_completion(&wait);
312
313 scpnt->SCp.ptr = NULL;
314
315 return 0;
316}
317
318/*
319 * function: zfcp_scsi_queuecommand
320 *
321 * purpose: enqueues a SCSI command to the specified target device
322 *
323 * returns: 0 - success, SCSI command enqueued
324 * !0 - failure
325 */
326int
327zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
328 void (*done) (struct scsi_cmnd *))
329{
330 struct zfcp_unit *unit;
331 struct zfcp_adapter *adapter;
332
333 /* reset the status for this request */
334 scpnt->result = 0;
335 scpnt->host_scribble = NULL;
336 scpnt->scsi_done = done;
337
338 /*
339 * figure out adapter and target device
340 * (stored there by zfcp_scsi_slave_alloc)
341 */
342 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
343 unit = (struct zfcp_unit *) scpnt->device->hostdata;
344
345 return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static struct zfcp_unit *
Andreas Herrmann06506d02006-05-22 18:18:19 +0200349zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
350 unsigned int lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct zfcp_port *port;
353 struct zfcp_unit *unit, *retval = NULL;
354
355 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700356 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 continue;
358 list_for_each_entry(unit, &port->unit_list_head, list) {
359 if (lun == unit->scsi_lun) {
360 retval = unit;
361 goto out;
362 }
363 }
364 }
365 out:
366 return retval;
367}
368
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200369/**
370 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
371 * @scpnt: pointer to scsi_cmnd to be aborted
372 * Return: SUCCESS - command has been aborted and cleaned up in internal
373 * bookkeeping, SCSI stack won't be called for aborted command
374 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200376 * We do not need to care for a SCSI command which completes normally
377 * but late during this abort routine runs. We are allowed to return
378 * late commands to the SCSI stack. It tracks the state of commands and
379 * will handle late commands. (Usually, the normal completion of late
380 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
382int
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200383zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200385 struct Scsi_Host *scsi_host;
386 struct zfcp_adapter *adapter;
387 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int retval = SUCCESS;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200389 struct zfcp_fsf_req *new_fsf_req = NULL;
390 struct zfcp_fsf_req *old_fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200393 scsi_host = scpnt->device->host;
394 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
395 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
398 scpnt, zfcp_get_busid_by_adapter(adapter));
399
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200400 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 write_lock_irqsave(&adapter->abort_lock, flags);
402
403 /*
404 * Check whether command has just completed and can not be aborted.
405 * Even if the command has just been completed late, we can access
406 * scpnt since the SCSI stack does not release it at least until
407 * this routine returns. (scpnt is parameter passed to this routine
408 * and must not disappear during abort even on late completion.)
409 */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200410 old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!old_fsf_req) {
412 write_unlock_irqrestore(&adapter->abort_lock, flags);
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100413 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, NULL);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200414 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto out;
416 }
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200417 old_fsf_req->data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200420 /* don't access old_fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 write_unlock_irqrestore(&adapter->abort_lock, flags);
422 /* call FSF routine which does the abort */
423 new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
424 adapter, unit, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!new_fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200426 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100427 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
428 old_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto out;
431 }
432
433 /* wait for completion of abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 __wait_event(new_fsf_req->completion_wq,
435 new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* status should be valid since signals were not permitted */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200438 if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100439 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req,
440 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 retval = SUCCESS;
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200442 } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100443 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req,
444 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 } else {
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100447 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req,
448 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200451 zfcp_fsf_req_free(new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return retval;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456int
457zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
458{
459 int retval;
460 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!unit) {
463 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
464 retval = SUCCESS;
465 goto out;
466 }
467 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
468
469 /*
470 * If we do not know whether the unit supports 'logical unit reset'
471 * then try 'logical unit reset' and proceed with 'target reset'
472 * if 'logical unit reset' fails.
473 * If the unit is known not to support 'logical unit reset' then
474 * skip 'logical unit reset' and try 'target reset' immediately.
475 */
476 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
477 &unit->status)) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200478 retval = zfcp_task_management_function(unit,
479 FCP_LOGICAL_UNIT_RESET,
480 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (retval) {
482 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
483 if (retval == -ENOTSUPP)
484 atomic_set_mask
485 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
486 &unit->status);
487 /* fall through and try 'target reset' next */
488 } else {
489 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
490 unit);
491 /* avoid 'target reset' */
492 retval = SUCCESS;
493 goto out;
494 }
495 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200496 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (retval) {
498 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
499 retval = FAILED;
500 } else {
501 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
502 retval = SUCCESS;
503 }
504 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return retval;
506}
507
508static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200509zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
510 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200514 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* issue task management function */
517 fsf_req = zfcp_fsf_send_fcp_command_task_management
518 (adapter, unit, tm_flags, 0);
519 if (!fsf_req) {
520 ZFCP_LOG_INFO("error: creation of task management request "
521 "failed for unit 0x%016Lx on port 0x%016Lx on "
522 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
523 zfcp_get_busid_by_adapter(adapter));
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200524 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 retval = -ENOMEM;
526 goto out;
527 }
528
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200529 __wait_event(fsf_req->completion_wq,
530 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
531
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200532 /*
533 * check completion status of task management function
534 */
535 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
536 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200538 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
539 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200541 } else
542 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200543
544 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 out:
546 return retval;
547}
548
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200549/**
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200550 * zfcp_scsi_eh_host_reset_handler - handler for host and bus reset
551 *
552 * If ERP is already running it will be stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200554int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200556 struct zfcp_unit *unit;
557 struct zfcp_adapter *adapter;
558 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200560 unit = (struct zfcp_unit*) scpnt->device->hostdata;
561 adapter = unit->port->adapter;
562
563 ZFCP_LOG_NORMAL("host/bus reset because of problems with "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 "unit 0x%016Lx\n", unit->fcp_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200566 write_lock_irqsave(&adapter->erp_lock, flags);
567 if (atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
568 &adapter->status)) {
569 zfcp_erp_modify_adapter_status(adapter,
570 ZFCP_STATUS_COMMON_UNBLOCKED|ZFCP_STATUS_COMMON_OPEN,
571 ZFCP_CLEAR);
572 zfcp_erp_action_dismiss_adapter(adapter);
573 write_unlock_irqrestore(&adapter->erp_lock, flags);
574 zfcp_fsf_req_dismiss_all(adapter);
575 adapter->fsf_req_seq_no = 0;
576 zfcp_erp_adapter_reopen(adapter, 0);
577 } else {
578 write_unlock_irqrestore(&adapter->erp_lock, flags);
579 zfcp_erp_adapter_reopen(adapter, 0);
580 zfcp_erp_wait(adapter);
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200583 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586int
587zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
588{
589 int retval = 0;
590 static unsigned int unique_id = 0;
591
592 /* register adapter as SCSI host with mid layer of SCSI stack */
593 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
594 sizeof (struct zfcp_adapter *));
595 if (!adapter->scsi_host) {
596 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
597 "for adapter %s ",
598 zfcp_get_busid_by_adapter(adapter));
599 retval = -EIO;
600 goto out;
601 }
602 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
603
604 /* tell the SCSI stack some characteristics of this adapter */
605 adapter->scsi_host->max_id = 1;
606 adapter->scsi_host->max_lun = 1;
607 adapter->scsi_host->max_channel = 0;
608 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
609 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
610 adapter->scsi_host->transportt = zfcp_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /*
613 * save a pointer to our own adapter data structure within
614 * hostdata field of SCSI host data structure
615 */
616 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
617
618 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
619 scsi_host_put(adapter->scsi_host);
620 retval = -EIO;
621 goto out;
622 }
623 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
624 out:
625 return retval;
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628void
629zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
630{
631 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200632 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 shost = adapter->scsi_host;
635 if (!shost)
636 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200637 read_lock_irq(&zfcp_data.config_lock);
638 list_for_each_entry(port, &adapter->port_list_head, list)
639 if (port->rport)
640 port->rport = NULL;
641 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700642 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 scsi_remove_host(shost);
644 scsi_host_put(shost);
645 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
647
648 return;
649}
650
651
652void
653zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
654{
655 adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
656 adapter->scsi_er_timer.data = (unsigned long) adapter;
657 adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
658 add_timer(&adapter->scsi_er_timer);
659}
660
661/*
662 * Support functions for FC transport class
663 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100664static struct fc_host_statistics*
665zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100667 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100669 if (!adapter->fc_stats) {
670 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
671 if (!fc_stats)
672 return NULL;
673 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
674 }
675 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
676 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100680zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
681 struct fsf_qtcb_bottom_port *data,
682 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100684 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
685 old->seconds_since_last_reset;
686 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
687 fc_stats->tx_words = data->tx_words - old->tx_words;
688 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
689 fc_stats->rx_words = data->rx_words - old->rx_words;
690 fc_stats->lip_count = data->lip - old->lip;
691 fc_stats->nos_count = data->nos - old->nos;
692 fc_stats->error_frames = data->error_frames - old->error_frames;
693 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
694 fc_stats->link_failure_count = data->link_failure - old->link_failure;
695 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
696 fc_stats->loss_of_signal_count = data->loss_of_signal -
697 old->loss_of_signal;
698 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
699 old->psp_error_counts;
700 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
701 old->invalid_tx_words;
702 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
703 fc_stats->fcp_input_requests = data->input_requests -
704 old->input_requests;
705 fc_stats->fcp_output_requests = data->output_requests -
706 old->output_requests;
707 fc_stats->fcp_control_requests = data->control_requests -
708 old->control_requests;
709 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
710 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100714zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
715 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100717 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
718 fc_stats->tx_frames = data->tx_frames;
719 fc_stats->tx_words = data->tx_words;
720 fc_stats->rx_frames = data->rx_frames;
721 fc_stats->rx_words = data->rx_words;
722 fc_stats->lip_count = data->lip;
723 fc_stats->nos_count = data->nos;
724 fc_stats->error_frames = data->error_frames;
725 fc_stats->dumped_frames = data->dumped_frames;
726 fc_stats->link_failure_count = data->link_failure;
727 fc_stats->loss_of_sync_count = data->loss_of_sync;
728 fc_stats->loss_of_signal_count = data->loss_of_signal;
729 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
730 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
731 fc_stats->invalid_crc_count = data->invalid_crcs;
732 fc_stats->fcp_input_requests = data->input_requests;
733 fc_stats->fcp_output_requests = data->output_requests;
734 fc_stats->fcp_control_requests = data->control_requests;
735 fc_stats->fcp_input_megabytes = data->input_mb;
736 fc_stats->fcp_output_megabytes = data->output_mb;
737}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100739/**
740 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
741 *
742 * assumption: scsi_transport_fc synchronizes calls of
743 * get_fc_host_stats and reset_fc_host_stats
744 * (XXX to be checked otherwise introduce locking)
745 */
746static struct fc_host_statistics *
747zfcp_get_fc_host_stats(struct Scsi_Host *shost)
748{
749 struct zfcp_adapter *adapter;
750 struct fc_host_statistics *fc_stats;
751 struct fsf_qtcb_bottom_port *data;
752 int ret;
753
754 adapter = (struct zfcp_adapter *)shost->hostdata[0];
755 fc_stats = zfcp_init_fc_host_stats(adapter);
756 if (!fc_stats)
757 return NULL;
758
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200759 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100760 if (!data)
761 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100762
763 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
764 if (ret) {
765 kfree(data);
766 return NULL; /* XXX return zeroed fc_stats? */
767 }
768
769 if (adapter->stats_reset &&
770 ((jiffies/HZ - adapter->stats_reset) <
771 data->seconds_since_last_reset)) {
772 zfcp_adjust_fc_host_stats(fc_stats, data,
773 adapter->stats_reset_data);
774 } else
775 zfcp_set_fc_host_stats(fc_stats, data);
776
777 kfree(data);
778 return fc_stats;
779}
780
781static void
782zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
783{
784 struct zfcp_adapter *adapter;
785 struct fsf_qtcb_bottom_port *data, *old_data;
786 int ret;
787
788 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200789 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100790 if (!data)
791 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100792
793 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
794 if (ret == 0) {
795 adapter->stats_reset = jiffies/HZ;
796 old_data = adapter->stats_reset_data;
797 adapter->stats_reset_data = data; /* finally freed in
798 adater_dequeue */
799 kfree(old_data);
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Andreas Herrmann338151e2006-05-22 18:25:56 +0200803static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
804{
805 rport->dev_loss_tmo = timeout;
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 .show_starget_port_id = 1,
810 .show_starget_port_name = 1,
811 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700812 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200813 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200814 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700815 .show_host_node_name = 1,
816 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100817 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700818 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100819 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200820 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700821 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100822 .get_fc_host_stats = zfcp_get_fc_host_stats,
823 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200824 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100825 /* no functions registered for following dynamic attributes but
826 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100827 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200828 .show_host_speed = 1,
829 .show_host_port_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830};
831
832/**
833 * ZFCP_DEFINE_SCSI_ATTR
834 * @_name: name of show attribute
835 * @_format: format string
836 * @_value: value to print
837 *
838 * Generates attribute for a unit.
839 */
840#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400841static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 char *buf) \
843{ \
844 struct scsi_device *sdev; \
845 struct zfcp_unit *unit; \
846 \
847 sdev = to_scsi_device(dev); \
848 unit = sdev->hostdata; \
849 return sprintf(buf, _format, _value); \
850} \
851 \
852static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
853
854ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
855ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
856ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
857
858static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
859 &dev_attr_fcp_lun,
860 &dev_attr_wwpn,
861 &dev_attr_hba_id,
862 NULL
863};
864
865#undef ZFCP_LOG_AREA