blob: c1403a23174fb3a0e9cdf3d7163f2d4e535f43ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * linux/drivers/s390/scsi/zfcp_scsi.c
4 *
5 * FCP adapter driver for IBM eServer zSeries
6 *
7 * (C) Copyright IBM Corp. 2002, 2004
8 *
9 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
10 * Raimund Schroeder <raimund.schroeder@de.ibm.com>
11 * Aron Zeh
12 * Wolfgang Taphorn
13 * Stefan Bader <stefan.bader@de.ibm.com>
14 * Heiko Carstens <heiko.carstens@de.ibm.com>
15 * Andreas Herrmann <aherrman@de.ibm.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2, or (at your option)
20 * any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 */
31
32#define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
33
34#define ZFCP_SCSI_REVISION "$Revision: 1.74 $"
35
36#include "zfcp_ext.h"
37
38static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
39static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
40static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
41static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
42 void (*done) (struct scsi_cmnd *));
43static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
44static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
45static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
46static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020047static int zfcp_task_management_function(struct zfcp_unit *, u8,
48 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int, scsi_id_t,
51 scsi_lun_t);
52static struct zfcp_port *zfcp_port_lookup(struct zfcp_adapter *, int,
53 scsi_id_t);
54
55static struct device_attribute *zfcp_sysfs_sdev_attrs[];
56
57struct scsi_transport_template *zfcp_transport_template;
58
59struct zfcp_data zfcp_data = {
60 .scsi_host_template = {
61 name: ZFCP_NAME,
62 proc_name: "zfcp",
63 proc_info: NULL,
64 detect: NULL,
65 slave_alloc: zfcp_scsi_slave_alloc,
66 slave_configure: zfcp_scsi_slave_configure,
67 slave_destroy: zfcp_scsi_slave_destroy,
68 queuecommand: zfcp_scsi_queuecommand,
69 eh_abort_handler: zfcp_scsi_eh_abort_handler,
70 eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
71 eh_bus_reset_handler: zfcp_scsi_eh_bus_reset_handler,
72 eh_host_reset_handler: zfcp_scsi_eh_host_reset_handler,
73 /* FIXME(openfcp): Tune */
74 can_queue: 4096,
75 this_id: 0,
76 /*
77 * FIXME:
78 * one less? can zfcp_create_sbale cope with it?
79 */
80 sg_tablesize: ZFCP_MAX_SBALES_PER_REQ,
81 cmd_per_lun: 1,
82 unchecked_isa_dma: 0,
83 use_clustering: 1,
84 sdev_attrs: zfcp_sysfs_sdev_attrs,
85 },
86 .driver_version = ZFCP_VERSION,
87 /* rest initialised with zeros */
88};
89
90/* Find start of Response Information in FCP response unit*/
91char *
92zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
93{
94 char *fcp_rsp_info_ptr;
95
96 fcp_rsp_info_ptr =
97 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
98
99 return fcp_rsp_info_ptr;
100}
101
102/* Find start of Sense Information in FCP response unit*/
103char *
104zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
105{
106 char *fcp_sns_info_ptr;
107
108 fcp_sns_info_ptr =
109 (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
110 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
111 fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
112 fcp_rsp_iu->fcp_rsp_len;
113
114 return fcp_sns_info_ptr;
115}
116
117fcp_dl_t *
118zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
119{
120 int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
121 fcp_dl_t *fcp_dl_addr;
122
123 fcp_dl_addr = (fcp_dl_t *)
124 ((unsigned char *) fcp_cmd +
125 sizeof (struct fcp_cmnd_iu) + additional_length);
126 /*
127 * fcp_dl_addr = start address of fcp_cmnd structure +
128 * size of fixed part + size of dynamically sized add_dcp_cdb field
129 * SEE FCP-2 documentation
130 */
131 return fcp_dl_addr;
132}
133
134fcp_dl_t
135zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
136{
137 return *zfcp_get_fcp_dl_ptr(fcp_cmd);
138}
139
140void
141zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
142{
143 *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
144}
145
146/*
147 * note: it's a bit-or operation not an assignment
148 * regarding the specified byte
149 */
150static inline void
151set_byte(u32 * result, char status, char pos)
152{
153 *result |= status << (pos * 8);
154}
155
156void
157set_host_byte(u32 * result, char status)
158{
159 set_byte(result, status, 2);
160}
161
162void
163set_driver_byte(u32 * result, char status)
164{
165 set_byte(result, status, 3);
166}
167
168/*
169 * function: zfcp_scsi_slave_alloc
170 *
171 * purpose:
172 *
173 * returns:
174 */
175
176static int
177zfcp_scsi_slave_alloc(struct scsi_device *sdp)
178{
179 struct zfcp_adapter *adapter;
180 struct zfcp_unit *unit;
181 unsigned long flags;
182 int retval = -ENODEV;
183
184 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
185 if (!adapter)
186 goto out;
187
188 read_lock_irqsave(&zfcp_data.config_lock, flags);
189 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
190 if (unit) {
191 sdp->hostdata = unit;
192 unit->device = sdp;
193 zfcp_unit_get(unit);
194 retval = 0;
195 }
196 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
197 out:
198 return retval;
199}
200
201/*
202 * function: zfcp_scsi_slave_destroy
203 *
204 * purpose:
205 *
206 * returns:
207 */
208
209static void
210zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
211{
212 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
213
214 if (unit) {
215 sdpnt->hostdata = NULL;
216 unit->device = NULL;
217 zfcp_unit_put(unit);
218 } else {
219 ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
220 "address %p\n", sdpnt);
221 }
222}
223
224/*
225 * called from scsi midlayer to allow finetuning of a device.
226 */
227static int
228zfcp_scsi_slave_configure(struct scsi_device *sdp)
229{
230 if (sdp->tagged_supported)
231 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
232 else
233 scsi_adjust_queue_depth(sdp, 0, 1);
234 return 0;
235}
236
237/**
238 * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
239 * @scpnt: pointer to struct scsi_cmnd where result is set
240 * @result: result to be set in scpnt (e.g. DID_ERROR)
241 */
242static void
243zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
244{
245 set_host_byte(&scpnt->result, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200246 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
247 zfcp_scsi_dbf_event_result("fail", 4,
248 (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
249 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* return directly */
251 scpnt->scsi_done(scpnt);
252}
253
254/**
255 * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
256 * zfcp_scsi_command_sync
257 * @adapter: adapter where scsi command is issued
258 * @unit: unit to which scsi command is sent
259 * @scpnt: scsi command to be sent
260 * @timer: timer to be started if request is successfully initiated
261 *
262 * Note: In scsi_done function must be set in scpnt.
263 */
264int
265zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
266 struct scsi_cmnd *scpnt, struct timer_list *timer)
267{
268 int tmp;
269 int retval;
270
271 retval = 0;
272
273 BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
274 BUG_ON(scpnt->scsi_done == NULL);
275
276 if (unlikely(NULL == unit)) {
277 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
278 goto out;
279 }
280
281 if (unlikely(
282 atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
283 !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
284 ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
285 "0x%016Lx on adapter %s\n",
286 unit->fcp_lun, unit->port->wwpn,
287 zfcp_get_busid_by_adapter(adapter));
288 zfcp_scsi_command_fail(scpnt, DID_ERROR);
289 goto out;
290 }
291
292 if (unlikely(
293 !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
294 ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
295 "on port 0x%016Lx in recovery\n",
296 zfcp_get_busid_by_unit(unit),
297 unit->fcp_lun, unit->port->wwpn);
298 retval = SCSI_MLQUEUE_DEVICE_BUSY;
299 goto out;
300 }
301
302 tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
303 ZFCP_REQ_AUTO_CLEANUP);
304
305 if (unlikely(tmp < 0)) {
306 ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
307 retval = SCSI_MLQUEUE_HOST_BUSY;
308 }
309
310out:
311 return retval;
312}
313
314void
315zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
316{
317 struct completion *wait = (struct completion *) scpnt->SCp.ptr;
318 complete(wait);
319}
320
321
322/**
323 * zfcp_scsi_command_sync - send a SCSI command and wait for completion
324 * @unit: unit where command is sent to
325 * @scpnt: scsi command to be sent
326 * @timer: timer to be started if request is successfully initiated
327 * Return: 0
328 *
329 * Errors are indicated in scpnt->result
330 */
331int
332zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
333 struct timer_list *timer)
334{
335 int ret;
336 DECLARE_COMPLETION(wait);
337
338 scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
339 scpnt->scsi_done = zfcp_scsi_command_sync_handler;
340 ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
341 if (ret == 0)
342 wait_for_completion(&wait);
343
344 scpnt->SCp.ptr = NULL;
345
346 return 0;
347}
348
349/*
350 * function: zfcp_scsi_queuecommand
351 *
352 * purpose: enqueues a SCSI command to the specified target device
353 *
354 * returns: 0 - success, SCSI command enqueued
355 * !0 - failure
356 */
357int
358zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
359 void (*done) (struct scsi_cmnd *))
360{
361 struct zfcp_unit *unit;
362 struct zfcp_adapter *adapter;
363
364 /* reset the status for this request */
365 scpnt->result = 0;
366 scpnt->host_scribble = NULL;
367 scpnt->scsi_done = done;
368
369 /*
370 * figure out adapter and target device
371 * (stored there by zfcp_scsi_slave_alloc)
372 */
373 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
374 unit = (struct zfcp_unit *) scpnt->device->hostdata;
375
376 return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
377}
378
379/*
380 * function: zfcp_unit_lookup
381 *
382 * purpose:
383 *
384 * returns:
385 *
386 * context:
387 */
388static struct zfcp_unit *
389zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
390 scsi_lun_t lun)
391{
392 struct zfcp_port *port;
393 struct zfcp_unit *unit, *retval = NULL;
394
395 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700396 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 continue;
398 list_for_each_entry(unit, &port->unit_list_head, list) {
399 if (lun == unit->scsi_lun) {
400 retval = unit;
401 goto out;
402 }
403 }
404 }
405 out:
406 return retval;
407}
408
409static struct zfcp_port *
410zfcp_port_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id)
411{
412 struct zfcp_port *port;
413
414 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700415 if (port->rport && (id == port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return port;
417 }
418 return (struct zfcp_port *) NULL;
419}
420
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200421/**
422 * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
423 * @scpnt: pointer to scsi_cmnd to be aborted
424 * Return: SUCCESS - command has been aborted and cleaned up in internal
425 * bookkeeping, SCSI stack won't be called for aborted command
426 * FAILED - otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200428 * We do not need to care for a SCSI command which completes normally
429 * but late during this abort routine runs. We are allowed to return
430 * late commands to the SCSI stack. It tracks the state of commands and
431 * will handle late commands. (Usually, the normal completion of late
432 * commands is ignored with respect to the running abort operation.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434int
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200435zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200437 struct Scsi_Host *scsi_host;
438 struct zfcp_adapter *adapter;
439 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int retval = SUCCESS;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200441 struct zfcp_fsf_req *new_fsf_req = NULL;
442 struct zfcp_fsf_req *old_fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200445 scsi_host = scpnt->device->host;
446 adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
447 unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
450 scpnt, zfcp_get_busid_by_adapter(adapter));
451
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200452 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 write_lock_irqsave(&adapter->abort_lock, flags);
454
455 /*
456 * Check whether command has just completed and can not be aborted.
457 * Even if the command has just been completed late, we can access
458 * scpnt since the SCSI stack does not release it at least until
459 * this routine returns. (scpnt is parameter passed to this routine
460 * and must not disappear during abort even on late completion.)
461 */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200462 old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!old_fsf_req) {
464 write_unlock_irqrestore(&adapter->abort_lock, flags);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200465 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, new_fsf_req);
466 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 goto out;
468 }
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200469 old_fsf_req->data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200472 /* don't access old_fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 write_unlock_irqrestore(&adapter->abort_lock, flags);
474 /* call FSF routine which does the abort */
475 new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
476 adapter, unit, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (!new_fsf_req) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200478 ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto out;
481 }
482
483 /* wait for completion of abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 __wait_event(new_fsf_req->completion_wq,
485 new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* status should be valid since signals were not permitted */
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200488 if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200489 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 retval = SUCCESS;
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200491 } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200492 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 retval = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200495 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200498 zfcp_fsf_req_free(new_fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return retval;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*
504 * function: zfcp_scsi_eh_device_reset_handler
505 *
506 * purpose:
507 *
508 * returns:
509 */
510int
511zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
512{
513 int retval;
514 struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!unit) {
517 ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
518 retval = SUCCESS;
519 goto out;
520 }
521 ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
522
523 /*
524 * If we do not know whether the unit supports 'logical unit reset'
525 * then try 'logical unit reset' and proceed with 'target reset'
526 * if 'logical unit reset' fails.
527 * If the unit is known not to support 'logical unit reset' then
528 * skip 'logical unit reset' and try 'target reset' immediately.
529 */
530 if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
531 &unit->status)) {
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200532 retval = zfcp_task_management_function(unit,
533 FCP_LOGICAL_UNIT_RESET,
534 scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (retval) {
536 ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
537 if (retval == -ENOTSUPP)
538 atomic_set_mask
539 (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
540 &unit->status);
541 /* fall through and try 'target reset' next */
542 } else {
543 ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
544 unit);
545 /* avoid 'target reset' */
546 retval = SUCCESS;
547 goto out;
548 }
549 }
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200550 retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (retval) {
552 ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
553 retval = FAILED;
554 } else {
555 ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
556 retval = SUCCESS;
557 }
558 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return retval;
560}
561
562static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200563zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
564 struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct zfcp_fsf_req *fsf_req;
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200568 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* issue task management function */
571 fsf_req = zfcp_fsf_send_fcp_command_task_management
572 (adapter, unit, tm_flags, 0);
573 if (!fsf_req) {
574 ZFCP_LOG_INFO("error: creation of task management request "
575 "failed for unit 0x%016Lx on port 0x%016Lx on "
576 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
577 zfcp_get_busid_by_adapter(adapter));
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200578 zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 retval = -ENOMEM;
580 goto out;
581 }
582
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200583 __wait_event(fsf_req->completion_wq,
584 fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
585
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200586 /*
587 * check completion status of task management function
588 */
589 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
590 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 retval = -EIO;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200592 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
593 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 retval = -ENOTSUPP;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200595 } else
596 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200597
598 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 out:
600 return retval;
601}
602
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200603/**
604 * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
606int
607zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
608{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200609 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
610 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 ZFCP_LOG_NORMAL("bus reset because of problems with "
613 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200614 zfcp_erp_adapter_reopen(adapter, 0);
615 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200617 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200620/**
621 * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623int
624zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
625{
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200626 struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
627 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ZFCP_LOG_NORMAL("host reset because of problems with "
630 "unit 0x%016Lx\n", unit->fcp_lun);
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200631 zfcp_erp_adapter_reopen(adapter, 0);
632 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200634 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/*
638 * function:
639 *
640 * purpose:
641 *
642 * returns:
643 */
644int
645zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
646{
647 int retval = 0;
648 static unsigned int unique_id = 0;
649
650 /* register adapter as SCSI host with mid layer of SCSI stack */
651 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
652 sizeof (struct zfcp_adapter *));
653 if (!adapter->scsi_host) {
654 ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
655 "for adapter %s ",
656 zfcp_get_busid_by_adapter(adapter));
657 retval = -EIO;
658 goto out;
659 }
660 ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
661
662 /* tell the SCSI stack some characteristics of this adapter */
663 adapter->scsi_host->max_id = 1;
664 adapter->scsi_host->max_lun = 1;
665 adapter->scsi_host->max_channel = 0;
666 adapter->scsi_host->unique_id = unique_id++; /* FIXME */
667 adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
668 adapter->scsi_host->transportt = zfcp_transport_template;
669 /*
670 * Reverse mapping of the host number to avoid race condition
671 */
672 adapter->scsi_host_no = adapter->scsi_host->host_no;
673
674 /*
675 * save a pointer to our own adapter data structure within
676 * hostdata field of SCSI host data structure
677 */
678 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
679
680 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
681 scsi_host_put(adapter->scsi_host);
682 retval = -EIO;
683 goto out;
684 }
685 atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
686 out:
687 return retval;
688}
689
690/*
691 * function:
692 *
693 * purpose:
694 *
695 * returns:
696 */
697void
698zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
699{
700 struct Scsi_Host *shost;
701
702 shost = adapter->scsi_host;
703 if (!shost)
704 return;
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700705 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 scsi_remove_host(shost);
707 scsi_host_put(shost);
708 adapter->scsi_host = NULL;
709 adapter->scsi_host_no = 0;
710 atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
711
712 return;
713}
714
715
716void
717zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
718{
719 adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
720 adapter->scsi_er_timer.data = (unsigned long) adapter;
721 adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
722 add_timer(&adapter->scsi_er_timer);
723}
724
725/*
726 * Support functions for FC transport class
727 */
728static void
729zfcp_get_port_id(struct scsi_target *starget)
730{
731 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
732 struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
733 struct zfcp_port *port;
734 unsigned long flags;
735
736 read_lock_irqsave(&zfcp_data.config_lock, flags);
737 port = zfcp_port_lookup(adapter, starget->channel, starget->id);
738 if (port)
739 fc_starget_port_id(starget) = port->d_id;
740 else
741 fc_starget_port_id(starget) = -1;
742 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
743}
744
745static void
746zfcp_get_port_name(struct scsi_target *starget)
747{
748 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
749 struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
750 struct zfcp_port *port;
751 unsigned long flags;
752
753 read_lock_irqsave(&zfcp_data.config_lock, flags);
754 port = zfcp_port_lookup(adapter, starget->channel, starget->id);
755 if (port)
756 fc_starget_port_name(starget) = port->wwpn;
757 else
758 fc_starget_port_name(starget) = -1;
759 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
760}
761
762static void
763zfcp_get_node_name(struct scsi_target *starget)
764{
765 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
766 struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
767 struct zfcp_port *port;
768 unsigned long flags;
769
770 read_lock_irqsave(&zfcp_data.config_lock, flags);
771 port = zfcp_port_lookup(adapter, starget->channel, starget->id);
772 if (port)
773 fc_starget_node_name(starget) = port->wwnn;
774 else
775 fc_starget_node_name(starget) = -1;
776 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
777}
778
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700779void
780zfcp_set_fc_host_attrs(struct zfcp_adapter *adapter)
781{
782 struct Scsi_Host *shost = adapter->scsi_host;
783
784 fc_host_node_name(shost) = adapter->wwnn;
785 fc_host_port_name(shost) = adapter->wwpn;
786 strncpy(fc_host_serial_number(shost), adapter->serial_number,
787 min(FC_SERIAL_NUMBER_SIZE, 32));
788 fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791struct fc_function_template zfcp_transport_functions = {
792 .get_starget_port_id = zfcp_get_port_id,
793 .get_starget_port_name = zfcp_get_port_name,
794 .get_starget_node_name = zfcp_get_node_name,
795 .show_starget_port_id = 1,
796 .show_starget_port_name = 1,
797 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700798 .show_rport_supported_classes = 1,
799 .show_host_node_name = 1,
800 .show_host_port_name = 1,
801 .show_host_supported_classes = 1,
802 .show_host_serial_number = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
804
805/**
806 * ZFCP_DEFINE_SCSI_ATTR
807 * @_name: name of show attribute
808 * @_format: format string
809 * @_value: value to print
810 *
811 * Generates attribute for a unit.
812 */
813#define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
Yani Ioannou10523b32005-05-17 06:43:37 -0400814static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 char *buf) \
816{ \
817 struct scsi_device *sdev; \
818 struct zfcp_unit *unit; \
819 \
820 sdev = to_scsi_device(dev); \
821 unit = sdev->hostdata; \
822 return sprintf(buf, _format, _value); \
823} \
824 \
825static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
826
827ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
828ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
829ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
830
831static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
832 &dev_attr_fcp_lun,
833 &dev_attr_wwpn,
834 &dev_attr_hba_id,
835 NULL
836};
837
838#undef ZFCP_LOG_AREA