blob: 043ed7c0a7ed59e6fec3ade88dbff03c7501dc27 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070042struct zfcp_data zfcp_data = {
43 .scsi_host_template = {
Heiko Carstensd2c993d2006-07-12 16:41:55 +020044 .name = ZFCP_NAME,
Heiko Carstensdd52e0e2006-09-18 22:28:49 +020045 .module = THIS_MODULE,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020046 .proc_name = "zfcp",
Heiko Carstensd2c993d2006-07-12 16:41:55 +020047 .slave_alloc = zfcp_scsi_slave_alloc,
48 .slave_configure = zfcp_scsi_slave_configure,
49 .slave_destroy = zfcp_scsi_slave_destroy,
50 .queuecommand = zfcp_scsi_queuecommand,
51 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
52 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +020053 .eh_bus_reset_handler = zfcp_scsi_eh_host_reset_handler,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020054 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
55 .can_queue = 4096,
56 .this_id = -1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020057 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
58 .cmd_per_lun = 1,
Heiko Carstensd2c993d2006-07-12 16:41:55 +020059 .use_clustering = 1,
60 .sdev_attrs = zfcp_sysfs_sdev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 },
62 .driver_version = ZFCP_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65/* Find start of Response Information in FCP response unit*/
66char *
67zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
68{
69 char *fcp_rsp_info_ptr;
70
71 fcp_rsp_info_ptr =
72 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
73
74 return fcp_rsp_info_ptr;
75}
76
77/* Find start of Sense Information in FCP response unit*/
78char *
79zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
80{
81 char *fcp_sns_info_ptr;
82
83 fcp_sns_info_ptr =
84 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
85 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
86 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
87 fcp_rsp_iu->fcp_rsp_len;
88
89 return fcp_sns_info_ptr;
90}
91
92fcp_dl_t *
93zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
94{
95 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
96 fcp_dl_t *fcp_dl_addr;
97
98 fcp_dl_addr = (fcp_dl_t *)
99 ((unsigned char *) fcp_cmd +
100 sizeof (struct fcp_cmnd_iu) + additional_length);
101 /*
102 * fcp_dl_addr = start address of fcp_cmnd structure +
103 * size of fixed part + size of dynamically sized add_dcp_cdb field
104 * SEE FCP-2 documentation
105 */
106 return fcp_dl_addr;
107}
108
109fcp_dl_t
110zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
111{
112 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
113}
114
115void
116zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
117{
118 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
119}
120
121/*
122 * note: it's a bit-or operation not an assignment
123 * regarding the specified byte
124 */
125static inline void
126set_byte(u32 * result, char status, char pos)
127{
128 *result |= status << (pos * 8);
129}
130
131void
132set_host_byte(u32 * result, char status)
133{
134 set_byte(result, status, 2);
135}
136
137void
138set_driver_byte(u32 * result, char status)
139{
140 set_byte(result, status, 3);
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int
144zfcp_scsi_slave_alloc(struct scsi_device *sdp)
145{
146 struct zfcp_adapter *adapter;
147 struct zfcp_unit *unit;
148 unsigned long flags;
Andreas Herrmannfb121b02005-12-01 02:49:29 +0100149 int retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
152 if (!adapter)
153 goto out;
154
155 read_lock_irqsave(&zfcp_data.config_lock, flags);
156 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100157 if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
158 &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 sdp->hostdata = unit;
160 unit->device = sdp;
161 zfcp_unit_get(unit);
162 retval = 0;
163 }
164 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
165 out:
166 return retval;
167}
168
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200169/**
170 * zfcp_scsi_slave_destroy - called when scsi device is removed
171 *
172 * Remove reference to associated scsi device for an zfcp_unit.
173 * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
174 * or a scan for this device might have failed.
175 */
176static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
179
180 if (unit) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100181 atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 sdpnt->hostdata = NULL;
183 unit->device = NULL;
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200184 zfcp_erp_unit_failed(unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 zfcp_unit_put(unit);
186 } else {
187 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
188 "address %p\n", sdpnt);
189 }
190}
191
192/*
193 * called from scsi midlayer to allow finetuning of a device.
194 */
195static int
196zfcp_scsi_slave_configure(struct scsi_device *sdp)
197{
198 if (sdp->tagged_supported)
199 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
200 else
201 scsi_adjust_queue_depth(sdp, 0, 1);
202 return 0;
203}
204
205/**
206 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
207 * @scpnt: pointer to struct scsi_cmnd where result is set
208 * @result: result to be set in scpnt (e.g. DID_ERROR)
209 */
210static void
211zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
212{
213 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200214 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
215 zfcp_scsi_dbf_event_result("fail", 4,
216 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100217 scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /* return directly */
219 scpnt->scsi_done(scpnt);
220}
221
222/**
223 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
224 * zfcp_scsi_command_sync
225 * @adapter: adapter where scsi command is issued
226 * @unit: unit to which scsi command is sent
227 * @scpnt: scsi command to be sent
228 * @timer: timer to be started if request is successfully initiated
229 *
230 * Note: In scsi_done function must be set in scpnt.
231 */
232int
233zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
234 struct scsi_cmnd *scpnt, struct timer_list *timer)
235{
236 int tmp;
237 int retval;
238
239 retval = 0;
240
241 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
242 BUG_ON(scpnt->scsi_done == NULL);
243
244 if (unlikely(NULL == unit)) {
245 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
246 goto out;
247 }
248
249 if (unlikely(
250 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
251 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
252 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
253 "0x%016Lx on adapter %s\n",
254 unit->fcp_lun, unit->port->wwpn,
255 zfcp_get_busid_by_adapter(adapter));
256 zfcp_scsi_command_fail(scpnt, DID_ERROR);
257 goto out;
258 }
259
260 if (unlikely(
261 !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
262 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
263 "on port 0x%016Lx in recovery\n",
264 zfcp_get_busid_by_unit(unit),
265 unit->fcp_lun, unit->port->wwpn);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +0100266 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto out;
268 }
269
270 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
271 ZFCP_REQ_AUTO_CLEANUP);
272
273 if (unlikely(tmp < 0)) {
274 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
275 retval = SCSI_MLQUEUE_HOST_BUSY;
276 }
277
278out:
279 return retval;
280}
281
282void
283zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
284{
285 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
286 complete(wait);
287}
288
289
290/**
291 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
292 * @unit: unit where command is sent to
293 * @scpnt: scsi command to be sent
294 * @timer: timer to be started if request is successfully initiated
295 * Return: 0
296 *
297 * Errors are indicated in scpnt->result
298 */
299int
300zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
301 struct timer_list *timer)
302{
303 int ret;
304 DECLARE_COMPLETION(wait);
305
306 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
307 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
308 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
309 if (ret == 0)
310 wait_for_completion(&wait);
311
312 scpnt->SCp.ptr = NULL;
313
314 return 0;
315}
316
317/*
318 * function: zfcp_scsi_queuecommand
319 *
320 * purpose: enqueues a SCSI command to the specified target device
321 *
322 * returns: 0 - success, SCSI command enqueued
323 * !0 - failure
324 */
325int
326zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
327 void (*done) (struct scsi_cmnd *))
328{
329 struct zfcp_unit *unit;
330 struct zfcp_adapter *adapter;
331
332 /* reset the status for this request */
333 scpnt->result = 0;
334 scpnt->host_scribble = NULL;
335 scpnt->scsi_done = done;
336
337 /*
338 * figure out adapter and target device
339 * (stored there by zfcp_scsi_slave_alloc)
340 */
341 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
342 unit = (struct zfcp_unit *) scpnt->device->hostdata;
343
344 return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static struct zfcp_unit *
Andreas Herrmann06506d02006-05-22 18:18:19 +0200348zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
349 unsigned int lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct zfcp_port *port;
352 struct zfcp_unit *unit, *retval = NULL;
353
354 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700355 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 continue;
357 list_for_each_entry(unit, &port->unit_list_head, list) {
358 if (lun == unit->scsi_lun) {
359 retval = unit;
360 goto out;
361 }
362 }
363 }
364 out:
365 return retval;
366}
367
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200368/**
369 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
370 * @scpnt: pointer to scsi_cmnd to be aborted
371 * Return: SUCCESS - command has been aborted and cleaned up in internal
372 * bookkeeping, SCSI stack won't be called for aborted command
373 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200375 * We do not need to care for a SCSI command which completes normally
376 * but late during this abort routine runs. We are allowed to return
377 * late commands to the SCSI stack. It tracks the state of commands and
378 * will handle late commands. (Usually, the normal completion of late
379 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200381int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200383 struct Scsi_Host *scsi_host;
384 struct zfcp_adapter *adapter;
385 struct zfcp_unit *unit;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200386 struct zfcp_fsf_req *fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned long flags;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200388 unsigned long old_req_id;
389 int retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200391 scsi_host = scpnt->device->host;
392 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
393 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
396 scpnt, zfcp_get_busid_by_adapter(adapter));
397
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200398 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 write_lock_irqsave(&adapter->abort_lock, flags);
400
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200401 /* Check whether corresponding fsf_req is still pending */
402 spin_lock(&adapter->req_list_lock);
403 fsf_req = zfcp_reqlist_ismember(adapter, (unsigned long)
404 scpnt->host_scribble);
405 spin_unlock(&adapter->req_list_lock);
406 if (!fsf_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200408 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200409 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto out;
411 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200412 fsf_req->data = 0;
413 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
414 old_req_id = fsf_req->req_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200416 /* don't access old fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200418
419 fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
420 if (!fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200421 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
Maxim Shchetynined829ad2006-02-11 01:42:58 +0100422 zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200423 old_req_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto out;
426 }
427
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200428 __wait_event(fsf_req->completion_wq,
429 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200430
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200431 if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
432 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 retval = SUCCESS;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200434 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
435 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 } else {
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200438 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200441 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return retval;
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446int
447zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
448{
449 int retval;
450 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!unit) {
453 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
454 retval = SUCCESS;
455 goto out;
456 }
457 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
458
459 /*
460 * If we do not know whether the unit supports 'logical unit reset'
461 * then try 'logical unit reset' and proceed with 'target reset'
462 * if 'logical unit reset' fails.
463 * If the unit is known not to support 'logical unit reset' then
464 * skip 'logical unit reset' and try 'target reset' immediately.
465 */
466 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
467 &unit->status)) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200468 retval = zfcp_task_management_function(unit,
469 FCP_LOGICAL_UNIT_RESET,
470 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (retval) {
472 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
473 if (retval == -ENOTSUPP)
474 atomic_set_mask
475 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
476 &unit->status);
477 /* fall through and try 'target reset' next */
478 } else {
479 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
480 unit);
481 /* avoid 'target reset' */
482 retval = SUCCESS;
483 goto out;
484 }
485 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200486 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (retval) {
488 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
489 retval = FAILED;
490 } else {
491 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
492 retval = SUCCESS;
493 }
494 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return retval;
496}
497
498static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200499zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
500 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200504 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* issue task management function */
507 fsf_req = zfcp_fsf_send_fcp_command_task_management
508 (adapter, unit, tm_flags, 0);
509 if (!fsf_req) {
510 ZFCP_LOG_INFO("error: creation of task management request "
511 "failed for unit 0x%016Lx on port 0x%016Lx on "
512 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
513 zfcp_get_busid_by_adapter(adapter));
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200514 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 retval = -ENOMEM;
516 goto out;
517 }
518
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200519 __wait_event(fsf_req->completion_wq,
520 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
521
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200522 /*
523 * check completion status of task management function
524 */
525 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
526 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200528 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
529 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200531 } else
532 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200533
534 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 out:
536 return retval;
537}
538
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200539/**
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200540 * zfcp_scsi_eh_host_reset_handler - handler for host and bus reset
541 *
542 * If ERP is already running it will be stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200544int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200546 struct zfcp_unit *unit;
547 struct zfcp_adapter *adapter;
548 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200550 unit = (struct zfcp_unit*) scpnt->device->hostdata;
551 adapter = unit->port->adapter;
552
553 ZFCP_LOG_NORMAL("host/bus reset because of problems with "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 "unit 0x%016Lx\n", unit->fcp_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200556 write_lock_irqsave(&adapter->erp_lock, flags);
557 if (atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
558 &adapter->status)) {
559 zfcp_erp_modify_adapter_status(adapter,
560 ZFCP_STATUS_COMMON_UNBLOCKED|ZFCP_STATUS_COMMON_OPEN,
561 ZFCP_CLEAR);
562 zfcp_erp_action_dismiss_adapter(adapter);
563 write_unlock_irqrestore(&adapter->erp_lock, flags);
564 zfcp_fsf_req_dismiss_all(adapter);
565 adapter->fsf_req_seq_no = 0;
566 zfcp_erp_adapter_reopen(adapter, 0);
567 } else {
568 write_unlock_irqrestore(&adapter->erp_lock, flags);
569 zfcp_erp_adapter_reopen(adapter, 0);
570 zfcp_erp_wait(adapter);
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200573 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576int
577zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
578{
579 int retval = 0;
580 static unsigned int unique_id = 0;
581
582 /* register adapter as SCSI host with mid layer of SCSI stack */
583 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
584 sizeof (struct zfcp_adapter *));
585 if (!adapter->scsi_host) {
586 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
587 "for adapter %s ",
588 zfcp_get_busid_by_adapter(adapter));
589 retval = -EIO;
590 goto out;
591 }
592 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
593
594 /* tell the SCSI stack some characteristics of this adapter */
595 adapter->scsi_host->max_id = 1;
596 adapter->scsi_host->max_lun = 1;
597 adapter->scsi_host->max_channel = 0;
598 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
599 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200600 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /*
603 * save a pointer to our own adapter data structure within
604 * hostdata field of SCSI host data structure
605 */
606 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
607
608 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
609 scsi_host_put(adapter->scsi_host);
610 retval = -EIO;
611 goto out;
612 }
613 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
614 out:
615 return retval;
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618void
619zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
620{
621 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200622 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 shost = adapter->scsi_host;
625 if (!shost)
626 return;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200627 read_lock_irq(&zfcp_data.config_lock);
628 list_for_each_entry(port, &adapter->port_list_head, list)
629 if (port->rport)
630 port->rport = NULL;
631 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700632 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 scsi_remove_host(shost);
634 scsi_host_put(shost);
635 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
637
638 return;
639}
640
641
642void
643zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
644{
645 adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
646 adapter->scsi_er_timer.data = (unsigned long) adapter;
647 adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
648 add_timer(&adapter->scsi_er_timer);
649}
650
651/*
652 * Support functions for FC transport class
653 */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100654static struct fc_host_statistics*
655zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100657 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100659 if (!adapter->fc_stats) {
660 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
661 if (!fc_stats)
662 return NULL;
663 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
664 }
665 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
666 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100670zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
671 struct fsf_qtcb_bottom_port *data,
672 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100674 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
675 old->seconds_since_last_reset;
676 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
677 fc_stats->tx_words = data->tx_words - old->tx_words;
678 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
679 fc_stats->rx_words = data->rx_words - old->rx_words;
680 fc_stats->lip_count = data->lip - old->lip;
681 fc_stats->nos_count = data->nos - old->nos;
682 fc_stats->error_frames = data->error_frames - old->error_frames;
683 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
684 fc_stats->link_failure_count = data->link_failure - old->link_failure;
685 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
686 fc_stats->loss_of_signal_count = data->loss_of_signal -
687 old->loss_of_signal;
688 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
689 old->psp_error_counts;
690 fc_stats->invalid_tx_word_count = data->invalid_tx_words -
691 old->invalid_tx_words;
692 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
693 fc_stats->fcp_input_requests = data->input_requests -
694 old->input_requests;
695 fc_stats->fcp_output_requests = data->output_requests -
696 old->output_requests;
697 fc_stats->fcp_control_requests = data->control_requests -
698 old->control_requests;
699 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
700 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703static void
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100704zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
705 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100707 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
708 fc_stats->tx_frames = data->tx_frames;
709 fc_stats->tx_words = data->tx_words;
710 fc_stats->rx_frames = data->rx_frames;
711 fc_stats->rx_words = data->rx_words;
712 fc_stats->lip_count = data->lip;
713 fc_stats->nos_count = data->nos;
714 fc_stats->error_frames = data->error_frames;
715 fc_stats->dumped_frames = data->dumped_frames;
716 fc_stats->link_failure_count = data->link_failure;
717 fc_stats->loss_of_sync_count = data->loss_of_sync;
718 fc_stats->loss_of_signal_count = data->loss_of_signal;
719 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
720 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
721 fc_stats->invalid_crc_count = data->invalid_crcs;
722 fc_stats->fcp_input_requests = data->input_requests;
723 fc_stats->fcp_output_requests = data->output_requests;
724 fc_stats->fcp_control_requests = data->control_requests;
725 fc_stats->fcp_input_megabytes = data->input_mb;
726 fc_stats->fcp_output_megabytes = data->output_mb;
727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100729/**
730 * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
731 *
732 * assumption: scsi_transport_fc synchronizes calls of
733 * get_fc_host_stats and reset_fc_host_stats
734 * (XXX to be checked otherwise introduce locking)
735 */
736static struct fc_host_statistics *
737zfcp_get_fc_host_stats(struct Scsi_Host *shost)
738{
739 struct zfcp_adapter *adapter;
740 struct fc_host_statistics *fc_stats;
741 struct fsf_qtcb_bottom_port *data;
742 int ret;
743
744 adapter = (struct zfcp_adapter *)shost->hostdata[0];
745 fc_stats = zfcp_init_fc_host_stats(adapter);
746 if (!fc_stats)
747 return NULL;
748
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200749 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100750 if (!data)
751 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100752
753 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
754 if (ret) {
755 kfree(data);
756 return NULL; /* XXX return zeroed fc_stats? */
757 }
758
759 if (adapter->stats_reset &&
760 ((jiffies/HZ - adapter->stats_reset) <
761 data->seconds_since_last_reset)) {
762 zfcp_adjust_fc_host_stats(fc_stats, data,
763 adapter->stats_reset_data);
764 } else
765 zfcp_set_fc_host_stats(fc_stats, data);
766
767 kfree(data);
768 return fc_stats;
769}
770
771static void
772zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
773{
774 struct zfcp_adapter *adapter;
775 struct fsf_qtcb_bottom_port *data, *old_data;
776 int ret;
777
778 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200779 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100780 if (!data)
781 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100782
783 ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
784 if (ret == 0) {
785 adapter->stats_reset = jiffies/HZ;
786 old_data = adapter->stats_reset_data;
787 adapter->stats_reset_data = data; /* finally freed in
788 adater_dequeue */
789 kfree(old_data);
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Andreas Herrmann338151e2006-05-22 18:25:56 +0200793static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
794{
795 rport->dev_loss_tmo = timeout;
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .show_starget_port_id = 1,
800 .show_starget_port_name = 1,
801 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700802 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200803 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200804 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700805 .show_host_node_name = 1,
806 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100807 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700808 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100809 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200810 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700811 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100812 .get_fc_host_stats = zfcp_get_fc_host_stats,
813 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200814 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100815 /* no functions registered for following dynamic attributes but
816 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100817 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200818 .show_host_speed = 1,
819 .show_host_port_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820};
821
822/**
823 * ZFCP_DEFINE_SCSI_ATTR
824 * @_name: name of show attribute
825 * @_format: format string
826 * @_value: value to print
827 *
828 * Generates attribute for a unit.
829 */
830#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400831static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 char *buf) \
833{ \
834 struct scsi_device *sdev; \
835 struct zfcp_unit *unit; \
836 \
837 sdev = to_scsi_device(dev); \
838 unit = sdev->hostdata; \
839 return sprintf(buf, _format, _value); \
840} \
841 \
842static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
843
844ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
845ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
846ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
847
848static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
849 &dev_attr_fcp_lun,
850 &dev_attr_wwpn,
851 &dev_attr_hba_id,
852 NULL
853};
854
855#undef ZFCP_LOG_AREA