[SCSI] zfcp: Improve reliability of SCSI eh handlers in zfcp
[safe/jmp/linux-2.6] / drivers / s390 / scsi / zfcp_scsi.c
1 /*
2  * zfcp device driver
3  *
4  * Interface to Linux SCSI midlayer.
5  *
6  * Copyright IBM Corporation 2002, 2008
7  */
8
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include "zfcp_ext.h"
13 #include <asm/atomic.h>
14
15 /* Find start of Sense Information in FCP response unit*/
16 char *zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
17 {
18         char *fcp_sns_info_ptr;
19
20         fcp_sns_info_ptr = (unsigned char *) &fcp_rsp_iu[1];
21         if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
22                 fcp_sns_info_ptr += fcp_rsp_iu->fcp_rsp_len;
23
24         return fcp_sns_info_ptr;
25 }
26
27 static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
28 {
29         struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
30         unit->device = NULL;
31         zfcp_erp_unit_failed(unit, 12, NULL);
32         zfcp_unit_put(unit);
33 }
34
35 static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
36 {
37         if (sdp->tagged_supported)
38                 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, 32);
39         else
40                 scsi_adjust_queue_depth(sdp, 0, 1);
41         return 0;
42 }
43
44 static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
45 {
46         set_host_byte(scpnt, result);
47         if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
48                 zfcp_scsi_dbf_event_result("fail", 4,
49                         (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
50                         scpnt, NULL);
51         /* return directly */
52         scpnt->scsi_done(scpnt);
53 }
54
55 static int zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
56                                   void (*done) (struct scsi_cmnd *))
57 {
58         struct zfcp_unit *unit;
59         struct zfcp_adapter *adapter;
60         int    status;
61         int    ret;
62
63         /* reset the status for this request */
64         scpnt->result = 0;
65         scpnt->host_scribble = NULL;
66         scpnt->scsi_done = done;
67
68         /*
69          * figure out adapter and target device
70          * (stored there by zfcp_scsi_slave_alloc)
71          */
72         adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
73         unit = scpnt->device->hostdata;
74
75         BUG_ON(!adapter || (adapter != unit->port->adapter));
76         BUG_ON(!scpnt->scsi_done);
77
78         if (unlikely(!unit)) {
79                 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
80                 return 0;
81         }
82
83         status = atomic_read(&unit->status);
84         if (unlikely((status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
85                      !(status & ZFCP_STATUS_COMMON_RUNNING))) {
86                 zfcp_scsi_command_fail(scpnt, DID_ERROR);
87                 return 0;;
88         }
89
90         ret = zfcp_fsf_send_fcp_command_task(unit, scpnt);
91         if (unlikely(ret == -EBUSY))
92                 return SCSI_MLQUEUE_DEVICE_BUSY;
93         else if (unlikely(ret < 0))
94                 return SCSI_MLQUEUE_HOST_BUSY;
95
96         return ret;
97 }
98
99 static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *adapter,
100                                           int channel, unsigned int id,
101                                           unsigned int lun)
102 {
103         struct zfcp_port *port;
104         struct zfcp_unit *unit;
105         int scsi_lun;
106
107         list_for_each_entry(port, &adapter->port_list_head, list) {
108                 if (!port->rport || (id != port->rport->scsi_target_id))
109                         continue;
110                 list_for_each_entry(unit, &port->unit_list_head, list) {
111                         scsi_lun = scsilun_to_int(
112                                 (struct scsi_lun *)&unit->fcp_lun);
113                         if (lun == scsi_lun)
114                                 return unit;
115                 }
116         }
117
118         return NULL;
119 }
120
121 static int zfcp_scsi_slave_alloc(struct scsi_device *sdp)
122 {
123         struct zfcp_adapter *adapter;
124         struct zfcp_unit *unit;
125         unsigned long flags;
126         int retval = -ENXIO;
127
128         adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
129         if (!adapter)
130                 goto out;
131
132         read_lock_irqsave(&zfcp_data.config_lock, flags);
133         unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
134         if (unit) {
135                 sdp->hostdata = unit;
136                 unit->device = sdp;
137                 zfcp_unit_get(unit);
138                 retval = 0;
139         }
140         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
141 out:
142         return retval;
143 }
144
145 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
146 {
147         struct Scsi_Host *scsi_host = scpnt->device->host;
148         struct zfcp_adapter *adapter =
149                 (struct zfcp_adapter *) scsi_host->hostdata[0];
150         struct zfcp_unit *unit = scpnt->device->hostdata;
151         struct zfcp_fsf_req *old_req, *abrt_req;
152         unsigned long flags;
153         unsigned long old_req_id = (unsigned long) scpnt->host_scribble;
154         int retval = SUCCESS;
155         int retry = 3;
156
157         /* avoid race condition between late normal completion and abort */
158         write_lock_irqsave(&adapter->abort_lock, flags);
159
160         spin_lock(&adapter->req_list_lock);
161         old_req = zfcp_reqlist_find(adapter, old_req_id);
162         spin_unlock(&adapter->req_list_lock);
163         if (!old_req) {
164                 write_unlock_irqrestore(&adapter->abort_lock, flags);
165                 zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL,
166                                           old_req_id);
167                 return SUCCESS;
168         }
169         old_req->data = NULL;
170
171         /* don't access old fsf_req after releasing the abort_lock */
172         write_unlock_irqrestore(&adapter->abort_lock, flags);
173
174         while (retry--) {
175                 abrt_req = zfcp_fsf_abort_fcp_command(old_req_id, unit);
176                 if (abrt_req)
177                         break;
178
179                 zfcp_erp_wait(adapter);
180                 if (!(atomic_read(&adapter->status) &
181                       ZFCP_STATUS_COMMON_RUNNING)) {
182                         zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
183                                                   old_req_id);
184                         return SUCCESS;
185                 }
186         }
187         if (!abrt_req)
188                 return FAILED;
189
190         wait_event(abrt_req->completion_wq,
191                    abrt_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
192
193         if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
194                 zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, abrt_req, 0);
195         else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
196                 zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, abrt_req, 0);
197         else {
198                 zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, abrt_req, 0);
199                 retval = FAILED;
200         }
201         zfcp_fsf_req_free(abrt_req);
202         return retval;
203 }
204
205 static int zfcp_task_mgmt_function(struct scsi_cmnd *scpnt, u8 tm_flags)
206 {
207         struct zfcp_unit *unit = scpnt->device->hostdata;
208         struct zfcp_adapter *adapter = unit->port->adapter;
209         struct zfcp_fsf_req *fsf_req;
210         int retval = SUCCESS;
211         int retry = 3;
212
213         while (retry--) {
214                 fsf_req = zfcp_fsf_send_fcp_ctm(unit, tm_flags);
215                 if (fsf_req)
216                         break;
217
218                 zfcp_erp_wait(adapter);
219                 if (!(atomic_read(&adapter->status) &
220                       ZFCP_STATUS_COMMON_RUNNING)) {
221                         zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit,
222                                                      scpnt);
223                         return SUCCESS;
224                 }
225         }
226         if (!fsf_req)
227                 return FAILED;
228
229         wait_event(fsf_req->completion_wq,
230                    fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
231
232         if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
233                 zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
234                 retval = FAILED;
235         } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
236                 zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
237                 retval = FAILED;
238         } else
239                 zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
240
241         zfcp_fsf_req_free(fsf_req);
242         return retval;
243 }
244
245 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
246 {
247         return zfcp_task_mgmt_function(scpnt, FCP_LOGICAL_UNIT_RESET);
248 }
249
250 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
251 {
252         return zfcp_task_mgmt_function(scpnt, FCP_TARGET_RESET);
253 }
254
255 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
256 {
257         struct zfcp_unit *unit = scpnt->device->hostdata;
258         struct zfcp_adapter *adapter = unit->port->adapter;
259
260         zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
261         zfcp_erp_wait(adapter);
262
263         return SUCCESS;
264 }
265
266 int zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
267 {
268         struct ccw_dev_id dev_id;
269
270         if (adapter->scsi_host)
271                 return 0;
272
273         ccw_device_get_id(adapter->ccw_device, &dev_id);
274         /* register adapter as SCSI host with mid layer of SCSI stack */
275         adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
276                                              sizeof (struct zfcp_adapter *));
277         if (!adapter->scsi_host) {
278                 dev_err(&adapter->ccw_device->dev,
279                         "Registering the FCP device with the "
280                         "SCSI stack failed\n");
281                 return -EIO;
282         }
283
284         /* tell the SCSI stack some characteristics of this adapter */
285         adapter->scsi_host->max_id = 1;
286         adapter->scsi_host->max_lun = 1;
287         adapter->scsi_host->max_channel = 0;
288         adapter->scsi_host->unique_id = dev_id.devno;
289         adapter->scsi_host->max_cmd_len = 255;
290         adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
291
292         adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
293
294         if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
295                 scsi_host_put(adapter->scsi_host);
296                 return -EIO;
297         }
298
299         return 0;
300 }
301
302 void zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
303 {
304         struct Scsi_Host *shost;
305         struct zfcp_port *port;
306
307         shost = adapter->scsi_host;
308         if (!shost)
309                 return;
310
311         read_lock_irq(&zfcp_data.config_lock);
312         list_for_each_entry(port, &adapter->port_list_head, list)
313                 if (port->rport)
314                         port->rport = NULL;
315
316         read_unlock_irq(&zfcp_data.config_lock);
317         fc_remove_host(shost);
318         scsi_remove_host(shost);
319         scsi_host_put(shost);
320         adapter->scsi_host = NULL;
321
322         return;
323 }
324
325 static struct fc_host_statistics*
326 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
327 {
328         struct fc_host_statistics *fc_stats;
329
330         if (!adapter->fc_stats) {
331                 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
332                 if (!fc_stats)
333                         return NULL;
334                 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
335         }
336         memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
337         return adapter->fc_stats;
338 }
339
340 static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
341                                       struct fsf_qtcb_bottom_port *data,
342                                       struct fsf_qtcb_bottom_port *old)
343 {
344         fc_stats->seconds_since_last_reset =
345                 data->seconds_since_last_reset - old->seconds_since_last_reset;
346         fc_stats->tx_frames = data->tx_frames - old->tx_frames;
347         fc_stats->tx_words = data->tx_words - old->tx_words;
348         fc_stats->rx_frames = data->rx_frames - old->rx_frames;
349         fc_stats->rx_words = data->rx_words - old->rx_words;
350         fc_stats->lip_count = data->lip - old->lip;
351         fc_stats->nos_count = data->nos - old->nos;
352         fc_stats->error_frames = data->error_frames - old->error_frames;
353         fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
354         fc_stats->link_failure_count = data->link_failure - old->link_failure;
355         fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
356         fc_stats->loss_of_signal_count =
357                 data->loss_of_signal - old->loss_of_signal;
358         fc_stats->prim_seq_protocol_err_count =
359                 data->psp_error_counts - old->psp_error_counts;
360         fc_stats->invalid_tx_word_count =
361                 data->invalid_tx_words - old->invalid_tx_words;
362         fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
363         fc_stats->fcp_input_requests =
364                 data->input_requests - old->input_requests;
365         fc_stats->fcp_output_requests =
366                 data->output_requests - old->output_requests;
367         fc_stats->fcp_control_requests =
368                 data->control_requests - old->control_requests;
369         fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
370         fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
371 }
372
373 static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
374                                    struct fsf_qtcb_bottom_port *data)
375 {
376         fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
377         fc_stats->tx_frames = data->tx_frames;
378         fc_stats->tx_words = data->tx_words;
379         fc_stats->rx_frames = data->rx_frames;
380         fc_stats->rx_words = data->rx_words;
381         fc_stats->lip_count = data->lip;
382         fc_stats->nos_count = data->nos;
383         fc_stats->error_frames = data->error_frames;
384         fc_stats->dumped_frames = data->dumped_frames;
385         fc_stats->link_failure_count = data->link_failure;
386         fc_stats->loss_of_sync_count = data->loss_of_sync;
387         fc_stats->loss_of_signal_count = data->loss_of_signal;
388         fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
389         fc_stats->invalid_tx_word_count = data->invalid_tx_words;
390         fc_stats->invalid_crc_count = data->invalid_crcs;
391         fc_stats->fcp_input_requests = data->input_requests;
392         fc_stats->fcp_output_requests = data->output_requests;
393         fc_stats->fcp_control_requests = data->control_requests;
394         fc_stats->fcp_input_megabytes = data->input_mb;
395         fc_stats->fcp_output_megabytes = data->output_mb;
396 }
397
398 static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
399 {
400         struct zfcp_adapter *adapter;
401         struct fc_host_statistics *fc_stats;
402         struct fsf_qtcb_bottom_port *data;
403         int ret;
404
405         adapter = (struct zfcp_adapter *)host->hostdata[0];
406         fc_stats = zfcp_init_fc_host_stats(adapter);
407         if (!fc_stats)
408                 return NULL;
409
410         data = kzalloc(sizeof(*data), GFP_KERNEL);
411         if (!data)
412                 return NULL;
413
414         ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
415         if (ret) {
416                 kfree(data);
417                 return NULL;
418         }
419
420         if (adapter->stats_reset &&
421             ((jiffies/HZ - adapter->stats_reset) <
422              data->seconds_since_last_reset))
423                 zfcp_adjust_fc_host_stats(fc_stats, data,
424                                           adapter->stats_reset_data);
425         else
426                 zfcp_set_fc_host_stats(fc_stats, data);
427
428         kfree(data);
429         return fc_stats;
430 }
431
432 static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
433 {
434         struct zfcp_adapter *adapter;
435         struct fsf_qtcb_bottom_port *data;
436         int ret;
437
438         adapter = (struct zfcp_adapter *)shost->hostdata[0];
439         data = kzalloc(sizeof(*data), GFP_KERNEL);
440         if (!data)
441                 return;
442
443         ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
444         if (ret)
445                 kfree(data);
446         else {
447                 adapter->stats_reset = jiffies/HZ;
448                 kfree(adapter->stats_reset_data);
449                 adapter->stats_reset_data = data; /* finally freed in
450                                                      adapter_dequeue */
451         }
452 }
453
454 static void zfcp_get_host_port_state(struct Scsi_Host *shost)
455 {
456         struct zfcp_adapter *adapter =
457                 (struct zfcp_adapter *)shost->hostdata[0];
458         int status = atomic_read(&adapter->status);
459
460         if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
461             !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
462                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
463         else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
464                 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
465         else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
466                 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
467         else
468                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
469 }
470
471 static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
472 {
473         rport->dev_loss_tmo = timeout;
474 }
475
476 struct fc_function_template zfcp_transport_functions = {
477         .show_starget_port_id = 1,
478         .show_starget_port_name = 1,
479         .show_starget_node_name = 1,
480         .show_rport_supported_classes = 1,
481         .show_rport_maxframe_size = 1,
482         .show_rport_dev_loss_tmo = 1,
483         .show_host_node_name = 1,
484         .show_host_port_name = 1,
485         .show_host_permanent_port_name = 1,
486         .show_host_supported_classes = 1,
487         .show_host_supported_speeds = 1,
488         .show_host_maxframe_size = 1,
489         .show_host_serial_number = 1,
490         .get_fc_host_stats = zfcp_get_fc_host_stats,
491         .reset_fc_host_stats = zfcp_reset_fc_host_stats,
492         .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
493         .get_host_port_state = zfcp_get_host_port_state,
494         .show_host_port_state = 1,
495         /* no functions registered for following dynamic attributes but
496            directly set by LLDD */
497         .show_host_port_type = 1,
498         .show_host_speed = 1,
499         .show_host_port_id = 1,
500         .disable_target_scan = 1,
501 };
502
503 struct zfcp_data zfcp_data = {
504         .scsi_host_template = {
505                 .name                    = "zfcp",
506                 .module                  = THIS_MODULE,
507                 .proc_name               = "zfcp",
508                 .slave_alloc             = zfcp_scsi_slave_alloc,
509                 .slave_configure         = zfcp_scsi_slave_configure,
510                 .slave_destroy           = zfcp_scsi_slave_destroy,
511                 .queuecommand            = zfcp_scsi_queuecommand,
512                 .eh_abort_handler        = zfcp_scsi_eh_abort_handler,
513                 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
514                 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
515                 .eh_host_reset_handler   = zfcp_scsi_eh_host_reset_handler,
516                 .can_queue               = 4096,
517                 .this_id                 = -1,
518                 .sg_tablesize            = ZFCP_MAX_SBALES_PER_REQ,
519                 .cmd_per_lun             = 1,
520                 .use_clustering          = 1,
521                 .sdev_attrs              = zfcp_sysfs_sdev_attrs,
522                 .max_sectors             = (ZFCP_MAX_SBALES_PER_REQ * 8),
523                 .shost_attrs             = zfcp_sysfs_shost_attrs,
524         },
525 };