[SCSI] libsas: prevent double completion of scmds from eh
[deliverable/linux.git] / drivers / scsi / libsas / sas_scsi_host.c
1 /*
2 * Serial Attached SCSI (SAS) class SCSI Host glue.
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 */
25
26 #include <linux/kthread.h>
27 #include <linux/firmware.h>
28 #include <linux/export.h>
29 #include <linux/ctype.h>
30
31 #include "sas_internal.h"
32
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_eh.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_sas.h>
40 #include <scsi/sas_ata.h>
41 #include "../scsi_sas_internal.h"
42 #include "../scsi_transport_api.h"
43 #include "../scsi_priv.h"
44
45 #include <linux/err.h>
46 #include <linux/blkdev.h>
47 #include <linux/freezer.h>
48 #include <linux/gfp.h>
49 #include <linux/scatterlist.h>
50 #include <linux/libata.h>
51
52 /* record final status and free the task */
53 static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
54 {
55 struct task_status_struct *ts = &task->task_status;
56 int hs = 0, stat = 0;
57
58 if (ts->resp == SAS_TASK_UNDELIVERED) {
59 /* transport error */
60 hs = DID_NO_CONNECT;
61 } else { /* ts->resp == SAS_TASK_COMPLETE */
62 /* task delivered, what happened afterwards? */
63 switch (ts->stat) {
64 case SAS_DEV_NO_RESPONSE:
65 case SAS_INTERRUPTED:
66 case SAS_PHY_DOWN:
67 case SAS_NAK_R_ERR:
68 case SAS_OPEN_TO:
69 hs = DID_NO_CONNECT;
70 break;
71 case SAS_DATA_UNDERRUN:
72 scsi_set_resid(sc, ts->residual);
73 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
74 hs = DID_ERROR;
75 break;
76 case SAS_DATA_OVERRUN:
77 hs = DID_ERROR;
78 break;
79 case SAS_QUEUE_FULL:
80 hs = DID_SOFT_ERROR; /* retry */
81 break;
82 case SAS_DEVICE_UNKNOWN:
83 hs = DID_BAD_TARGET;
84 break;
85 case SAS_SG_ERR:
86 hs = DID_PARITY;
87 break;
88 case SAS_OPEN_REJECT:
89 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
90 hs = DID_SOFT_ERROR; /* retry */
91 else
92 hs = DID_ERROR;
93 break;
94 case SAS_PROTO_RESPONSE:
95 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
96 "task; please report this\n",
97 task->dev->port->ha->sas_ha_name);
98 break;
99 case SAS_ABORTED_TASK:
100 hs = DID_ABORT;
101 break;
102 case SAM_STAT_CHECK_CONDITION:
103 memcpy(sc->sense_buffer, ts->buf,
104 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
105 stat = SAM_STAT_CHECK_CONDITION;
106 break;
107 default:
108 stat = ts->stat;
109 break;
110 }
111 }
112
113 sc->result = (hs << 16) | stat;
114 ASSIGN_SAS_TASK(sc, NULL);
115 list_del_init(&task->list);
116 sas_free_task(task);
117 }
118
119 static void sas_scsi_task_done(struct sas_task *task)
120 {
121 struct scsi_cmnd *sc = task->uldd_task;
122
123 if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
124 /* Aborted tasks will be completed by the error handler */
125 SAS_DPRINTK("task done but aborted\n");
126 return;
127 }
128
129 if (unlikely(!sc)) {
130 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
131 list_del_init(&task->list);
132 sas_free_task(task);
133 return;
134 }
135
136 ASSIGN_SAS_TASK(sc, NULL);
137 sas_end_task(sc, task);
138 sc->scsi_done(sc);
139 }
140
141 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
142 struct domain_device *dev,
143 gfp_t gfp_flags)
144 {
145 struct sas_task *task = sas_alloc_task(gfp_flags);
146 struct scsi_lun lun;
147
148 if (!task)
149 return NULL;
150
151 task->uldd_task = cmd;
152 ASSIGN_SAS_TASK(cmd, task);
153
154 task->dev = dev;
155 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
156
157 task->ssp_task.retry_count = 1;
158 int_to_scsilun(cmd->device->lun, &lun);
159 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
160 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
161 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
162
163 task->scatter = scsi_sglist(cmd);
164 task->num_scatter = scsi_sg_count(cmd);
165 task->total_xfer_len = scsi_bufflen(cmd);
166 task->data_dir = cmd->sc_data_direction;
167
168 task->task_done = sas_scsi_task_done;
169
170 return task;
171 }
172
173 int sas_queue_up(struct sas_task *task)
174 {
175 struct sas_ha_struct *sas_ha = task->dev->port->ha;
176 struct scsi_core *core = &sas_ha->core;
177 unsigned long flags;
178 LIST_HEAD(list);
179
180 spin_lock_irqsave(&core->task_queue_lock, flags);
181 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
182 spin_unlock_irqrestore(&core->task_queue_lock, flags);
183 return -SAS_QUEUE_FULL;
184 }
185 list_add_tail(&task->list, &core->task_queue);
186 core->task_queue_size += 1;
187 spin_unlock_irqrestore(&core->task_queue_lock, flags);
188 wake_up_process(core->queue_thread);
189
190 return 0;
191 }
192
193 int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
194 {
195 struct sas_internal *i = to_sas_internal(host->transportt);
196 struct domain_device *dev = cmd_to_domain_dev(cmd);
197 struct sas_ha_struct *sas_ha = dev->port->ha;
198 struct sas_task *task;
199 int res = 0;
200
201 /* If the device fell off, no sense in issuing commands */
202 if (test_bit(SAS_DEV_GONE, &dev->state)) {
203 cmd->result = DID_BAD_TARGET << 16;
204 goto out_done;
205 }
206
207 if (dev_is_sata(dev)) {
208 spin_lock_irq(dev->sata_dev.ap->lock);
209 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
210 spin_unlock_irq(dev->sata_dev.ap->lock);
211 return res;
212 }
213
214 task = sas_create_task(cmd, dev, GFP_ATOMIC);
215 if (!task)
216 return SCSI_MLQUEUE_HOST_BUSY;
217
218 /* Queue up, Direct Mode or Task Collector Mode. */
219 if (sas_ha->lldd_max_execute_num < 2)
220 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
221 else
222 res = sas_queue_up(task);
223
224 if (res)
225 goto out_free_task;
226 return 0;
227
228 out_free_task:
229 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
230 ASSIGN_SAS_TASK(cmd, NULL);
231 sas_free_task(task);
232 if (res == -SAS_QUEUE_FULL)
233 cmd->result = DID_SOFT_ERROR << 16; /* retry */
234 else
235 cmd->result = DID_ERROR << 16;
236 out_done:
237 cmd->scsi_done(cmd);
238 return 0;
239 }
240
241 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
242 {
243 struct sas_task *task = TO_SAS_TASK(cmd);
244 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
245
246 /* At this point, we only get called following an actual abort
247 * of the task, so we should be guaranteed not to be racing with
248 * any completions from the LLD. Task is freed after this.
249 */
250 sas_end_task(cmd, task);
251
252 /* now finish the command and move it on to the error
253 * handler done list, this also takes it off the
254 * error handler pending list.
255 */
256 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
257 }
258
259 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
260 {
261 struct scsi_cmnd *cmd, *n;
262
263 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
264 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
265 cmd->device->lun == my_cmd->device->lun)
266 sas_eh_finish_cmd(cmd);
267 }
268 }
269
270 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
271 struct domain_device *dev)
272 {
273 struct scsi_cmnd *cmd, *n;
274
275 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
276 struct domain_device *x = cmd_to_domain_dev(cmd);
277
278 if (x == dev)
279 sas_eh_finish_cmd(cmd);
280 }
281 }
282
283 static void sas_scsi_clear_queue_port(struct list_head *error_q,
284 struct asd_sas_port *port)
285 {
286 struct scsi_cmnd *cmd, *n;
287
288 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
289 struct domain_device *dev = cmd_to_domain_dev(cmd);
290 struct asd_sas_port *x = dev->port;
291
292 if (x == port)
293 sas_eh_finish_cmd(cmd);
294 }
295 }
296
297 enum task_disposition {
298 TASK_IS_DONE,
299 TASK_IS_ABORTED,
300 TASK_IS_AT_LU,
301 TASK_IS_NOT_AT_LU,
302 TASK_ABORT_FAILED,
303 };
304
305 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
306 {
307 struct sas_ha_struct *ha = task->dev->port->ha;
308 unsigned long flags;
309 int i, res;
310 struct sas_internal *si =
311 to_sas_internal(task->dev->port->ha->core.shost->transportt);
312
313 if (ha->lldd_max_execute_num > 1) {
314 struct scsi_core *core = &ha->core;
315 struct sas_task *t, *n;
316
317 spin_lock_irqsave(&core->task_queue_lock, flags);
318 list_for_each_entry_safe(t, n, &core->task_queue, list) {
319 if (task == t) {
320 list_del_init(&t->list);
321 spin_unlock_irqrestore(&core->task_queue_lock,
322 flags);
323 SAS_DPRINTK("%s: task 0x%p aborted from "
324 "task_queue\n",
325 __func__, task);
326 return TASK_IS_ABORTED;
327 }
328 }
329 spin_unlock_irqrestore(&core->task_queue_lock, flags);
330 }
331
332 for (i = 0; i < 5; i++) {
333 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
334 res = si->dft->lldd_abort_task(task);
335
336 spin_lock_irqsave(&task->task_state_lock, flags);
337 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
338 spin_unlock_irqrestore(&task->task_state_lock, flags);
339 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
340 task);
341 return TASK_IS_DONE;
342 }
343 spin_unlock_irqrestore(&task->task_state_lock, flags);
344
345 if (res == TMF_RESP_FUNC_COMPLETE) {
346 SAS_DPRINTK("%s: task 0x%p is aborted\n",
347 __func__, task);
348 return TASK_IS_ABORTED;
349 } else if (si->dft->lldd_query_task) {
350 SAS_DPRINTK("%s: querying task 0x%p\n",
351 __func__, task);
352 res = si->dft->lldd_query_task(task);
353 switch (res) {
354 case TMF_RESP_FUNC_SUCC:
355 SAS_DPRINTK("%s: task 0x%p at LU\n",
356 __func__, task);
357 return TASK_IS_AT_LU;
358 case TMF_RESP_FUNC_COMPLETE:
359 SAS_DPRINTK("%s: task 0x%p not at LU\n",
360 __func__, task);
361 return TASK_IS_NOT_AT_LU;
362 case TMF_RESP_FUNC_FAILED:
363 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
364 __func__, task);
365 return TASK_ABORT_FAILED;
366 }
367
368 }
369 }
370 return res;
371 }
372
373 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
374 {
375 int res = TMF_RESP_FUNC_FAILED;
376 struct scsi_lun lun;
377 struct sas_internal *i =
378 to_sas_internal(dev->port->ha->core.shost->transportt);
379
380 int_to_scsilun(cmd->device->lun, &lun);
381
382 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
383 SAS_ADDR(dev->sas_addr),
384 cmd->device->lun);
385
386 if (i->dft->lldd_abort_task_set)
387 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
388
389 if (res == TMF_RESP_FUNC_FAILED) {
390 if (i->dft->lldd_clear_task_set)
391 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
392 }
393
394 if (res == TMF_RESP_FUNC_FAILED) {
395 if (i->dft->lldd_lu_reset)
396 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
397 }
398
399 return res;
400 }
401
402 static int sas_recover_I_T(struct domain_device *dev)
403 {
404 int res = TMF_RESP_FUNC_FAILED;
405 struct sas_internal *i =
406 to_sas_internal(dev->port->ha->core.shost->transportt);
407
408 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
409 SAS_ADDR(dev->sas_addr));
410
411 if (i->dft->lldd_I_T_nexus_reset)
412 res = i->dft->lldd_I_T_nexus_reset(dev);
413
414 return res;
415 }
416
417 /* Find the sas_phy that's attached to this device */
418 struct sas_phy *sas_find_local_phy(struct domain_device *dev)
419 {
420 struct domain_device *pdev = dev->parent;
421 struct ex_phy *exphy = NULL;
422 int i;
423
424 /* Directly attached device */
425 if (!pdev)
426 return dev->port->phy;
427
428 /* Otherwise look in the expander */
429 for (i = 0; i < pdev->ex_dev.num_phys; i++)
430 if (!memcmp(dev->sas_addr,
431 pdev->ex_dev.ex_phy[i].attached_sas_addr,
432 SAS_ADDR_SIZE)) {
433 exphy = &pdev->ex_dev.ex_phy[i];
434 break;
435 }
436
437 BUG_ON(!exphy);
438 return exphy->phy;
439 }
440 EXPORT_SYMBOL_GPL(sas_find_local_phy);
441
442 /* Attempt to send a LUN reset message to a device */
443 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
444 {
445 struct domain_device *dev = cmd_to_domain_dev(cmd);
446 struct sas_internal *i =
447 to_sas_internal(dev->port->ha->core.shost->transportt);
448 struct scsi_lun lun;
449 int res;
450
451 int_to_scsilun(cmd->device->lun, &lun);
452
453 if (!i->dft->lldd_lu_reset)
454 return FAILED;
455
456 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
457 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
458 return SUCCESS;
459
460 return FAILED;
461 }
462
463 /* Attempt to send a phy (bus) reset */
464 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
465 {
466 struct domain_device *dev = cmd_to_domain_dev(cmd);
467 struct sas_phy *phy = sas_find_local_phy(dev);
468 int res;
469
470 res = sas_phy_reset(phy, 1);
471 if (res)
472 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
473 kobject_name(&phy->dev.kobj),
474 res);
475 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
476 return SUCCESS;
477
478 return FAILED;
479 }
480
481 /* Try to reset a device */
482 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
483 {
484 int res;
485 struct Scsi_Host *shost = cmd->device->host;
486
487 if (!shost->hostt->eh_device_reset_handler)
488 goto try_bus_reset;
489
490 res = shost->hostt->eh_device_reset_handler(cmd);
491 if (res == SUCCESS)
492 return res;
493
494 try_bus_reset:
495 if (shost->hostt->eh_bus_reset_handler)
496 return shost->hostt->eh_bus_reset_handler(cmd);
497
498 return FAILED;
499 }
500
501 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
502 struct list_head *work_q,
503 struct list_head *done_q)
504 {
505 struct scsi_cmnd *cmd, *n;
506 enum task_disposition res = TASK_IS_DONE;
507 int tmf_resp, need_reset;
508 struct sas_internal *i = to_sas_internal(shost->transportt);
509 unsigned long flags;
510 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
511
512 Again:
513 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
514 struct sas_task *task = TO_SAS_TASK(cmd);
515
516 if (!task)
517 continue;
518
519 list_del_init(&cmd->eh_entry);
520
521 spin_lock_irqsave(&task->task_state_lock, flags);
522 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
523 spin_unlock_irqrestore(&task->task_state_lock, flags);
524
525 if (need_reset) {
526 SAS_DPRINTK("%s: task 0x%p requests reset\n",
527 __func__, task);
528 goto reset;
529 }
530
531 SAS_DPRINTK("trying to find task 0x%p\n", task);
532 res = sas_scsi_find_task(task);
533
534 cmd->eh_eflags = 0;
535
536 switch (res) {
537 case TASK_IS_DONE:
538 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
539 task);
540 sas_eh_finish_cmd(cmd);
541 continue;
542 case TASK_IS_ABORTED:
543 SAS_DPRINTK("%s: task 0x%p is aborted\n",
544 __func__, task);
545 sas_eh_finish_cmd(cmd);
546 continue;
547 case TASK_IS_AT_LU:
548 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
549 reset:
550 tmf_resp = sas_recover_lu(task->dev, cmd);
551 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
552 SAS_DPRINTK("dev %016llx LU %x is "
553 "recovered\n",
554 SAS_ADDR(task->dev),
555 cmd->device->lun);
556 sas_eh_finish_cmd(cmd);
557 sas_scsi_clear_queue_lu(work_q, cmd);
558 goto Again;
559 }
560 /* fallthrough */
561 case TASK_IS_NOT_AT_LU:
562 case TASK_ABORT_FAILED:
563 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
564 task);
565 tmf_resp = sas_recover_I_T(task->dev);
566 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
567 struct domain_device *dev = task->dev;
568 SAS_DPRINTK("I_T %016llx recovered\n",
569 SAS_ADDR(task->dev->sas_addr));
570 sas_eh_finish_cmd(cmd);
571 sas_scsi_clear_queue_I_T(work_q, dev);
572 goto Again;
573 }
574 /* Hammer time :-) */
575 try_to_reset_cmd_device(cmd);
576 if (i->dft->lldd_clear_nexus_port) {
577 struct asd_sas_port *port = task->dev->port;
578 SAS_DPRINTK("clearing nexus for port:%d\n",
579 port->id);
580 res = i->dft->lldd_clear_nexus_port(port);
581 if (res == TMF_RESP_FUNC_COMPLETE) {
582 SAS_DPRINTK("clear nexus port:%d "
583 "succeeded\n", port->id);
584 sas_eh_finish_cmd(cmd);
585 sas_scsi_clear_queue_port(work_q,
586 port);
587 goto Again;
588 }
589 }
590 if (i->dft->lldd_clear_nexus_ha) {
591 SAS_DPRINTK("clear nexus ha\n");
592 res = i->dft->lldd_clear_nexus_ha(ha);
593 if (res == TMF_RESP_FUNC_COMPLETE) {
594 SAS_DPRINTK("clear nexus ha "
595 "succeeded\n");
596 sas_eh_finish_cmd(cmd);
597 goto clear_q;
598 }
599 }
600 /* If we are here -- this means that no amount
601 * of effort could recover from errors. Quite
602 * possibly the HA just disappeared.
603 */
604 SAS_DPRINTK("error from device %llx, LUN %x "
605 "couldn't be recovered in any way\n",
606 SAS_ADDR(task->dev->sas_addr),
607 cmd->device->lun);
608
609 sas_eh_finish_cmd(cmd);
610 goto clear_q;
611 }
612 }
613 return list_empty(work_q);
614 clear_q:
615 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
616 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
617 sas_eh_finish_cmd(cmd);
618
619 return list_empty(work_q);
620 }
621
622 void sas_scsi_recover_host(struct Scsi_Host *shost)
623 {
624 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
625 unsigned long flags;
626 LIST_HEAD(eh_work_q);
627
628 spin_lock_irqsave(shost->host_lock, flags);
629 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
630 shost->host_eh_scheduled = 0;
631 spin_unlock_irqrestore(shost->host_lock, flags);
632
633 SAS_DPRINTK("Enter %s\n", __func__);
634 /*
635 * Deal with commands that still have SAS tasks (i.e. they didn't
636 * complete via the normal sas_task completion mechanism)
637 */
638 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
639 goto out;
640
641 /*
642 * Now deal with SCSI commands that completed ok but have a an error
643 * code (and hopefully sense data) attached. This is roughly what
644 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
645 * command we see here has no sas_task and is thus unknown to the HA.
646 */
647 if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
648 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
649 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
650
651 out:
652 /* now link into libata eh --- if we have any ata devices */
653 sas_ata_strategy_handler(shost);
654
655 scsi_eh_flush_done_q(&ha->eh_done_q);
656
657 SAS_DPRINTK("--- Exit %s\n", __func__);
658 return;
659 }
660
661 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
662 {
663 struct sas_task *task = TO_SAS_TASK(cmd);
664 unsigned long flags;
665 enum blk_eh_timer_return rtn;
666
667 if (sas_ata_timed_out(cmd, task, &rtn))
668 return rtn;
669
670 if (!task) {
671 cmd->request->timeout /= 2;
672 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
673 cmd, task, (cmd->request->timeout ?
674 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
675 if (!cmd->request->timeout)
676 return BLK_EH_NOT_HANDLED;
677 return BLK_EH_RESET_TIMER;
678 }
679
680 spin_lock_irqsave(&task->task_state_lock, flags);
681 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
682 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
683 spin_unlock_irqrestore(&task->task_state_lock, flags);
684 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
685 "BLK_EH_HANDLED\n", cmd, task);
686 return BLK_EH_HANDLED;
687 }
688 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
689 spin_unlock_irqrestore(&task->task_state_lock, flags);
690 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
691 "BLK_EH_RESET_TIMER\n",
692 cmd, task);
693 return BLK_EH_RESET_TIMER;
694 }
695 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
696 spin_unlock_irqrestore(&task->task_state_lock, flags);
697
698 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
699 cmd, task);
700
701 return BLK_EH_NOT_HANDLED;
702 }
703
704 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
705 {
706 struct domain_device *dev = sdev_to_domain_dev(sdev);
707
708 if (dev_is_sata(dev))
709 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
710
711 return -EINVAL;
712 }
713
714 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
715 {
716 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
717 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
718 struct domain_device *found_dev = NULL;
719 int i;
720 unsigned long flags;
721
722 spin_lock_irqsave(&ha->phy_port_lock, flags);
723 for (i = 0; i < ha->num_phys; i++) {
724 struct asd_sas_port *port = ha->sas_port[i];
725 struct domain_device *dev;
726
727 spin_lock(&port->dev_list_lock);
728 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
729 if (rphy == dev->rphy) {
730 found_dev = dev;
731 spin_unlock(&port->dev_list_lock);
732 goto found;
733 }
734 }
735 spin_unlock(&port->dev_list_lock);
736 }
737 found:
738 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
739
740 return found_dev;
741 }
742
743 int sas_target_alloc(struct scsi_target *starget)
744 {
745 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
746 struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
747 int res;
748
749 if (!found_dev)
750 return -ENODEV;
751
752 if (dev_is_sata(found_dev)) {
753 res = sas_ata_init_host_and_port(found_dev, starget);
754 if (res)
755 return res;
756 }
757
758 kref_get(&found_dev->kref);
759 starget->hostdata = found_dev;
760 return 0;
761 }
762
763 #define SAS_DEF_QD 256
764
765 int sas_slave_configure(struct scsi_device *scsi_dev)
766 {
767 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
768 struct sas_ha_struct *sas_ha;
769
770 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
771
772 if (dev_is_sata(dev)) {
773 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
774 return 0;
775 }
776
777 sas_ha = dev->port->ha;
778
779 sas_read_port_mode_page(scsi_dev);
780
781 if (scsi_dev->tagged_supported) {
782 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
783 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
784 } else {
785 SAS_DPRINTK("device %llx, LUN %x doesn't support "
786 "TCQ\n", SAS_ADDR(dev->sas_addr),
787 scsi_dev->lun);
788 scsi_dev->tagged_supported = 0;
789 scsi_set_tag_type(scsi_dev, 0);
790 scsi_deactivate_tcq(scsi_dev, 1);
791 }
792
793 scsi_dev->allow_restart = 1;
794
795 return 0;
796 }
797
798 int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
799 {
800 struct domain_device *dev = sdev_to_domain_dev(sdev);
801
802 if (dev_is_sata(dev))
803 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
804 reason);
805
806 switch (reason) {
807 case SCSI_QDEPTH_DEFAULT:
808 case SCSI_QDEPTH_RAMP_UP:
809 if (!sdev->tagged_supported)
810 depth = 1;
811 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
812 break;
813 case SCSI_QDEPTH_QFULL:
814 scsi_track_queue_full(sdev, depth);
815 break;
816 default:
817 return -EOPNOTSUPP;
818 }
819
820 return depth;
821 }
822
823 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
824 {
825 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
826
827 if (dev_is_sata(dev))
828 return -EINVAL;
829
830 if (!scsi_dev->tagged_supported)
831 return 0;
832
833 scsi_deactivate_tcq(scsi_dev, 1);
834
835 scsi_set_tag_type(scsi_dev, qt);
836 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
837
838 return qt;
839 }
840
841 int sas_bios_param(struct scsi_device *scsi_dev,
842 struct block_device *bdev,
843 sector_t capacity, int *hsc)
844 {
845 hsc[0] = 255;
846 hsc[1] = 63;
847 sector_div(capacity, 255*63);
848 hsc[2] = capacity;
849
850 return 0;
851 }
852
853 /* ---------- Task Collector Thread implementation ---------- */
854
855 static void sas_queue(struct sas_ha_struct *sas_ha)
856 {
857 struct scsi_core *core = &sas_ha->core;
858 unsigned long flags;
859 LIST_HEAD(q);
860 int can_queue;
861 int res;
862 struct sas_internal *i = to_sas_internal(core->shost->transportt);
863
864 spin_lock_irqsave(&core->task_queue_lock, flags);
865 while (!kthread_should_stop() &&
866 !list_empty(&core->task_queue)) {
867
868 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
869 if (can_queue >= 0) {
870 can_queue = core->task_queue_size;
871 list_splice_init(&core->task_queue, &q);
872 } else {
873 struct list_head *a, *n;
874
875 can_queue = sas_ha->lldd_queue_size;
876 list_for_each_safe(a, n, &core->task_queue) {
877 list_move_tail(a, &q);
878 if (--can_queue == 0)
879 break;
880 }
881 can_queue = sas_ha->lldd_queue_size;
882 }
883 core->task_queue_size -= can_queue;
884 spin_unlock_irqrestore(&core->task_queue_lock, flags);
885 {
886 struct sas_task *task = list_entry(q.next,
887 struct sas_task,
888 list);
889 list_del_init(&q);
890 res = i->dft->lldd_execute_task(task, can_queue,
891 GFP_KERNEL);
892 if (unlikely(res))
893 __list_add(&q, task->list.prev, &task->list);
894 }
895 spin_lock_irqsave(&core->task_queue_lock, flags);
896 if (res) {
897 list_splice_init(&q, &core->task_queue); /*at head*/
898 core->task_queue_size += can_queue;
899 }
900 }
901 spin_unlock_irqrestore(&core->task_queue_lock, flags);
902 }
903
904 /**
905 * sas_queue_thread -- The Task Collector thread
906 * @_sas_ha: pointer to struct sas_ha
907 */
908 static int sas_queue_thread(void *_sas_ha)
909 {
910 struct sas_ha_struct *sas_ha = _sas_ha;
911
912 while (1) {
913 set_current_state(TASK_INTERRUPTIBLE);
914 schedule();
915 sas_queue(sas_ha);
916 if (kthread_should_stop())
917 break;
918 }
919
920 return 0;
921 }
922
923 int sas_init_queue(struct sas_ha_struct *sas_ha)
924 {
925 struct scsi_core *core = &sas_ha->core;
926
927 spin_lock_init(&core->task_queue_lock);
928 core->task_queue_size = 0;
929 INIT_LIST_HEAD(&core->task_queue);
930
931 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
932 "sas_queue_%d", core->shost->host_no);
933 if (IS_ERR(core->queue_thread))
934 return PTR_ERR(core->queue_thread);
935 return 0;
936 }
937
938 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
939 {
940 unsigned long flags;
941 struct scsi_core *core = &sas_ha->core;
942 struct sas_task *task, *n;
943
944 kthread_stop(core->queue_thread);
945
946 if (!list_empty(&core->task_queue))
947 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
948 SAS_ADDR(sas_ha->sas_addr));
949
950 spin_lock_irqsave(&core->task_queue_lock, flags);
951 list_for_each_entry_safe(task, n, &core->task_queue, list) {
952 struct scsi_cmnd *cmd = task->uldd_task;
953
954 list_del_init(&task->list);
955
956 ASSIGN_SAS_TASK(cmd, NULL);
957 sas_free_task(task);
958 cmd->result = DID_ABORT << 16;
959 cmd->scsi_done(cmd);
960 }
961 spin_unlock_irqrestore(&core->task_queue_lock, flags);
962 }
963
964 /*
965 * Tell an upper layer that it needs to initiate an abort for a given task.
966 * This should only ever be called by an LLDD.
967 */
968 void sas_task_abort(struct sas_task *task)
969 {
970 struct scsi_cmnd *sc = task->uldd_task;
971
972 /* Escape for libsas internal commands */
973 if (!sc) {
974 if (!del_timer(&task->timer))
975 return;
976 task->timer.function(task->timer.data);
977 return;
978 }
979
980 if (dev_is_sata(task->dev)) {
981 sas_ata_task_abort(task);
982 } else {
983 struct request_queue *q = sc->device->request_queue;
984 unsigned long flags;
985
986 spin_lock_irqsave(q->queue_lock, flags);
987 blk_abort_request(sc->request);
988 spin_unlock_irqrestore(q->queue_lock, flags);
989 scsi_schedule_eh(sc->device->host);
990 }
991 }
992
993 int sas_slave_alloc(struct scsi_device *scsi_dev)
994 {
995 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
996
997 if (dev_is_sata(dev))
998 return ata_sas_port_init(dev->sata_dev.ap);
999
1000 return 0;
1001 }
1002
1003 void sas_target_destroy(struct scsi_target *starget)
1004 {
1005 struct domain_device *found_dev = starget->hostdata;
1006
1007 if (!found_dev)
1008 return;
1009
1010 if (dev_is_sata(found_dev))
1011 ata_sas_port_destroy(found_dev->sata_dev.ap);
1012
1013 starget->hostdata = NULL;
1014 sas_put_device(found_dev);
1015 }
1016
1017 static void sas_parse_addr(u8 *sas_addr, const char *p)
1018 {
1019 int i;
1020 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1021 u8 h, l;
1022 if (!*p)
1023 break;
1024 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1025 p++;
1026 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1027 p++;
1028 sas_addr[i] = (h<<4) | l;
1029 }
1030 }
1031
1032 #define SAS_STRING_ADDR_SIZE 16
1033
1034 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1035 {
1036 int res;
1037 const struct firmware *fw;
1038
1039 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1040 if (res)
1041 return res;
1042
1043 if (fw->size < SAS_STRING_ADDR_SIZE) {
1044 res = -ENODEV;
1045 goto out;
1046 }
1047
1048 sas_parse_addr(addr, fw->data);
1049
1050 out:
1051 release_firmware(fw);
1052 return res;
1053 }
1054 EXPORT_SYMBOL_GPL(sas_request_addr);
1055
1056 EXPORT_SYMBOL_GPL(sas_queuecommand);
1057 EXPORT_SYMBOL_GPL(sas_target_alloc);
1058 EXPORT_SYMBOL_GPL(sas_slave_configure);
1059 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1060 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1061 EXPORT_SYMBOL_GPL(sas_bios_param);
1062 EXPORT_SYMBOL_GPL(sas_task_abort);
1063 EXPORT_SYMBOL_GPL(sas_phy_reset);
1064 EXPORT_SYMBOL_GPL(sas_phy_enable);
1065 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1066 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1067 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1068 EXPORT_SYMBOL_GPL(sas_target_destroy);
1069 EXPORT_SYMBOL_GPL(sas_ioctl);
This page took 0.055599 seconds and 5 git commands to generate.