Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[linux-btrfs-devel.git] / drivers / scsi / libsas / sas_scsi_host.c
blobeeba76cdf7746c518c49f680d3d2ab9f3a9b863a
1 /*
2 * Serial Attached SCSI (SAS) class SCSI Host glue.
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
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.
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.
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
26 #include <linux/kthread.h>
27 #include <linux/firmware.h>
28 #include <linux/ctype.h>
30 #include "sas_internal.h"
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_eh.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_sas.h>
39 #include <scsi/sas_ata.h>
40 #include "../scsi_sas_internal.h"
41 #include "../scsi_transport_api.h"
42 #include "../scsi_priv.h"
44 #include <linux/err.h>
45 #include <linux/blkdev.h>
46 #include <linux/freezer.h>
47 #include <linux/gfp.h>
48 #include <linux/scatterlist.h>
49 #include <linux/libata.h>
51 /* ---------- SCSI Host glue ---------- */
53 static void sas_scsi_task_done(struct sas_task *task)
55 struct task_status_struct *ts = &task->task_status;
56 struct scsi_cmnd *sc = task->uldd_task;
57 int hs = 0, stat = 0;
59 if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
60 /* Aborted tasks will be completed by the error handler */
61 SAS_DPRINTK("task done but aborted\n");
62 return;
65 if (unlikely(!sc)) {
66 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
67 list_del_init(&task->list);
68 sas_free_task(task);
69 return;
72 if (ts->resp == SAS_TASK_UNDELIVERED) {
73 /* transport error */
74 hs = DID_NO_CONNECT;
75 } else { /* ts->resp == SAS_TASK_COMPLETE */
76 /* task delivered, what happened afterwards? */
77 switch (ts->stat) {
78 case SAS_DEV_NO_RESPONSE:
79 case SAS_INTERRUPTED:
80 case SAS_PHY_DOWN:
81 case SAS_NAK_R_ERR:
82 case SAS_OPEN_TO:
83 hs = DID_NO_CONNECT;
84 break;
85 case SAS_DATA_UNDERRUN:
86 scsi_set_resid(sc, ts->residual);
87 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
88 hs = DID_ERROR;
89 break;
90 case SAS_DATA_OVERRUN:
91 hs = DID_ERROR;
92 break;
93 case SAS_QUEUE_FULL:
94 hs = DID_SOFT_ERROR; /* retry */
95 break;
96 case SAS_DEVICE_UNKNOWN:
97 hs = DID_BAD_TARGET;
98 break;
99 case SAS_SG_ERR:
100 hs = DID_PARITY;
101 break;
102 case SAS_OPEN_REJECT:
103 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
104 hs = DID_SOFT_ERROR; /* retry */
105 else
106 hs = DID_ERROR;
107 break;
108 case SAS_PROTO_RESPONSE:
109 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
110 "task; please report this\n",
111 task->dev->port->ha->sas_ha_name);
112 break;
113 case SAS_ABORTED_TASK:
114 hs = DID_ABORT;
115 break;
116 case SAM_STAT_CHECK_CONDITION:
117 memcpy(sc->sense_buffer, ts->buf,
118 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
119 stat = SAM_STAT_CHECK_CONDITION;
120 break;
121 default:
122 stat = ts->stat;
123 break;
126 ASSIGN_SAS_TASK(sc, NULL);
127 sc->result = (hs << 16) | stat;
128 list_del_init(&task->list);
129 sas_free_task(task);
130 sc->scsi_done(sc);
133 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
134 struct domain_device *dev,
135 gfp_t gfp_flags)
137 struct sas_task *task = sas_alloc_task(gfp_flags);
138 struct scsi_lun lun;
140 if (!task)
141 return NULL;
143 task->uldd_task = cmd;
144 ASSIGN_SAS_TASK(cmd, task);
146 task->dev = dev;
147 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
149 task->ssp_task.retry_count = 1;
150 int_to_scsilun(cmd->device->lun, &lun);
151 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
152 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
153 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
155 task->scatter = scsi_sglist(cmd);
156 task->num_scatter = scsi_sg_count(cmd);
157 task->total_xfer_len = scsi_bufflen(cmd);
158 task->data_dir = cmd->sc_data_direction;
160 task->task_done = sas_scsi_task_done;
162 return task;
165 int sas_queue_up(struct sas_task *task)
167 struct sas_ha_struct *sas_ha = task->dev->port->ha;
168 struct scsi_core *core = &sas_ha->core;
169 unsigned long flags;
170 LIST_HEAD(list);
172 spin_lock_irqsave(&core->task_queue_lock, flags);
173 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
174 spin_unlock_irqrestore(&core->task_queue_lock, flags);
175 return -SAS_QUEUE_FULL;
177 list_add_tail(&task->list, &core->task_queue);
178 core->task_queue_size += 1;
179 spin_unlock_irqrestore(&core->task_queue_lock, flags);
180 wake_up_process(core->queue_thread);
182 return 0;
186 * sas_queuecommand -- Enqueue a command for processing
187 * @parameters: See SCSI Core documentation
189 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
190 * call us without holding an IRQ spinlock...
192 static int sas_queuecommand_lck(struct scsi_cmnd *cmd,
193 void (*scsi_done)(struct scsi_cmnd *))
194 __releases(host->host_lock)
195 __acquires(dev->sata_dev.ap->lock)
196 __releases(dev->sata_dev.ap->lock)
197 __acquires(host->host_lock)
199 int res = 0;
200 struct domain_device *dev = cmd_to_domain_dev(cmd);
201 struct Scsi_Host *host = cmd->device->host;
202 struct sas_internal *i = to_sas_internal(host->transportt);
204 spin_unlock_irq(host->host_lock);
207 struct sas_ha_struct *sas_ha = dev->port->ha;
208 struct sas_task *task;
210 /* If the device fell off, no sense in issuing commands */
211 if (dev->gone) {
212 cmd->result = DID_BAD_TARGET << 16;
213 scsi_done(cmd);
214 goto out;
217 if (dev_is_sata(dev)) {
218 unsigned long flags;
220 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
221 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
222 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
223 goto out;
226 res = -ENOMEM;
227 task = sas_create_task(cmd, dev, GFP_ATOMIC);
228 if (!task)
229 goto out;
231 cmd->scsi_done = scsi_done;
232 /* Queue up, Direct Mode or Task Collector Mode. */
233 if (sas_ha->lldd_max_execute_num < 2)
234 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
235 else
236 res = sas_queue_up(task);
238 /* Examine */
239 if (res) {
240 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
241 ASSIGN_SAS_TASK(cmd, NULL);
242 sas_free_task(task);
243 if (res == -SAS_QUEUE_FULL) {
244 cmd->result = DID_SOFT_ERROR << 16; /* retry */
245 res = 0;
246 scsi_done(cmd);
248 goto out;
251 out:
252 spin_lock_irq(host->host_lock);
253 return res;
256 DEF_SCSI_QCMD(sas_queuecommand)
258 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
260 struct sas_task *task = TO_SAS_TASK(cmd);
261 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
263 /* remove the aborted task flag to allow the task to be
264 * completed now. At this point, we only get called following
265 * an actual abort of the task, so we should be guaranteed not
266 * to be racing with any completions from the LLD (hence we
267 * don't need the task state lock to clear the flag) */
268 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
269 /* Now call task_done. However, task will be free'd after
270 * this */
271 task->task_done(task);
272 /* now finish the command and move it on to the error
273 * handler done list, this also takes it off the
274 * error handler pending list */
275 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
278 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
280 struct scsi_cmnd *cmd, *n;
282 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
283 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
284 cmd->device->lun == my_cmd->device->lun)
285 sas_eh_finish_cmd(cmd);
289 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
290 struct domain_device *dev)
292 struct scsi_cmnd *cmd, *n;
294 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
295 struct domain_device *x = cmd_to_domain_dev(cmd);
297 if (x == dev)
298 sas_eh_finish_cmd(cmd);
302 static void sas_scsi_clear_queue_port(struct list_head *error_q,
303 struct asd_sas_port *port)
305 struct scsi_cmnd *cmd, *n;
307 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
308 struct domain_device *dev = cmd_to_domain_dev(cmd);
309 struct asd_sas_port *x = dev->port;
311 if (x == port)
312 sas_eh_finish_cmd(cmd);
316 enum task_disposition {
317 TASK_IS_DONE,
318 TASK_IS_ABORTED,
319 TASK_IS_AT_LU,
320 TASK_IS_NOT_AT_LU,
321 TASK_ABORT_FAILED,
324 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
326 struct sas_ha_struct *ha = task->dev->port->ha;
327 unsigned long flags;
328 int i, res;
329 struct sas_internal *si =
330 to_sas_internal(task->dev->port->ha->core.shost->transportt);
332 if (ha->lldd_max_execute_num > 1) {
333 struct scsi_core *core = &ha->core;
334 struct sas_task *t, *n;
336 spin_lock_irqsave(&core->task_queue_lock, flags);
337 list_for_each_entry_safe(t, n, &core->task_queue, list) {
338 if (task == t) {
339 list_del_init(&t->list);
340 spin_unlock_irqrestore(&core->task_queue_lock,
341 flags);
342 SAS_DPRINTK("%s: task 0x%p aborted from "
343 "task_queue\n",
344 __func__, task);
345 return TASK_IS_ABORTED;
348 spin_unlock_irqrestore(&core->task_queue_lock, flags);
351 for (i = 0; i < 5; i++) {
352 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
353 res = si->dft->lldd_abort_task(task);
355 spin_lock_irqsave(&task->task_state_lock, flags);
356 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
357 spin_unlock_irqrestore(&task->task_state_lock, flags);
358 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
359 task);
360 return TASK_IS_DONE;
362 spin_unlock_irqrestore(&task->task_state_lock, flags);
364 if (res == TMF_RESP_FUNC_COMPLETE) {
365 SAS_DPRINTK("%s: task 0x%p is aborted\n",
366 __func__, task);
367 return TASK_IS_ABORTED;
368 } else if (si->dft->lldd_query_task) {
369 SAS_DPRINTK("%s: querying task 0x%p\n",
370 __func__, task);
371 res = si->dft->lldd_query_task(task);
372 switch (res) {
373 case TMF_RESP_FUNC_SUCC:
374 SAS_DPRINTK("%s: task 0x%p at LU\n",
375 __func__, task);
376 return TASK_IS_AT_LU;
377 case TMF_RESP_FUNC_COMPLETE:
378 SAS_DPRINTK("%s: task 0x%p not at LU\n",
379 __func__, task);
380 return TASK_IS_NOT_AT_LU;
381 case TMF_RESP_FUNC_FAILED:
382 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
383 __func__, task);
384 return TASK_ABORT_FAILED;
389 return res;
392 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
394 int res = TMF_RESP_FUNC_FAILED;
395 struct scsi_lun lun;
396 struct sas_internal *i =
397 to_sas_internal(dev->port->ha->core.shost->transportt);
399 int_to_scsilun(cmd->device->lun, &lun);
401 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
402 SAS_ADDR(dev->sas_addr),
403 cmd->device->lun);
405 if (i->dft->lldd_abort_task_set)
406 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
408 if (res == TMF_RESP_FUNC_FAILED) {
409 if (i->dft->lldd_clear_task_set)
410 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
413 if (res == TMF_RESP_FUNC_FAILED) {
414 if (i->dft->lldd_lu_reset)
415 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
418 return res;
421 static int sas_recover_I_T(struct domain_device *dev)
423 int res = TMF_RESP_FUNC_FAILED;
424 struct sas_internal *i =
425 to_sas_internal(dev->port->ha->core.shost->transportt);
427 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
428 SAS_ADDR(dev->sas_addr));
430 if (i->dft->lldd_I_T_nexus_reset)
431 res = i->dft->lldd_I_T_nexus_reset(dev);
433 return res;
436 /* Find the sas_phy that's attached to this device */
437 struct sas_phy *sas_find_local_phy(struct domain_device *dev)
439 struct domain_device *pdev = dev->parent;
440 struct ex_phy *exphy = NULL;
441 int i;
443 /* Directly attached device */
444 if (!pdev)
445 return dev->port->phy;
447 /* Otherwise look in the expander */
448 for (i = 0; i < pdev->ex_dev.num_phys; i++)
449 if (!memcmp(dev->sas_addr,
450 pdev->ex_dev.ex_phy[i].attached_sas_addr,
451 SAS_ADDR_SIZE)) {
452 exphy = &pdev->ex_dev.ex_phy[i];
453 break;
456 BUG_ON(!exphy);
457 return exphy->phy;
459 EXPORT_SYMBOL_GPL(sas_find_local_phy);
461 /* Attempt to send a LUN reset message to a device */
462 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
464 struct domain_device *dev = cmd_to_domain_dev(cmd);
465 struct sas_internal *i =
466 to_sas_internal(dev->port->ha->core.shost->transportt);
467 struct scsi_lun lun;
468 int res;
470 int_to_scsilun(cmd->device->lun, &lun);
472 if (!i->dft->lldd_lu_reset)
473 return FAILED;
475 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
476 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
477 return SUCCESS;
479 return FAILED;
482 /* Attempt to send a phy (bus) reset */
483 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
485 struct domain_device *dev = cmd_to_domain_dev(cmd);
486 struct sas_phy *phy = sas_find_local_phy(dev);
487 int res;
489 res = sas_phy_reset(phy, 1);
490 if (res)
491 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
492 kobject_name(&phy->dev.kobj),
493 res);
494 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
495 return SUCCESS;
497 return FAILED;
500 /* Try to reset a device */
501 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
503 int res;
504 struct Scsi_Host *shost = cmd->device->host;
506 if (!shost->hostt->eh_device_reset_handler)
507 goto try_bus_reset;
509 res = shost->hostt->eh_device_reset_handler(cmd);
510 if (res == SUCCESS)
511 return res;
513 try_bus_reset:
514 if (shost->hostt->eh_bus_reset_handler)
515 return shost->hostt->eh_bus_reset_handler(cmd);
517 return FAILED;
520 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
521 struct list_head *work_q,
522 struct list_head *done_q)
524 struct scsi_cmnd *cmd, *n;
525 enum task_disposition res = TASK_IS_DONE;
526 int tmf_resp, need_reset;
527 struct sas_internal *i = to_sas_internal(shost->transportt);
528 unsigned long flags;
529 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
531 Again:
532 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
533 struct sas_task *task = TO_SAS_TASK(cmd);
535 if (!task)
536 continue;
538 list_del_init(&cmd->eh_entry);
540 spin_lock_irqsave(&task->task_state_lock, flags);
541 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
542 spin_unlock_irqrestore(&task->task_state_lock, flags);
544 if (need_reset) {
545 SAS_DPRINTK("%s: task 0x%p requests reset\n",
546 __func__, task);
547 goto reset;
550 SAS_DPRINTK("trying to find task 0x%p\n", task);
551 res = sas_scsi_find_task(task);
553 cmd->eh_eflags = 0;
555 switch (res) {
556 case TASK_IS_DONE:
557 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
558 task);
559 sas_eh_finish_cmd(cmd);
560 continue;
561 case TASK_IS_ABORTED:
562 SAS_DPRINTK("%s: task 0x%p is aborted\n",
563 __func__, task);
564 sas_eh_finish_cmd(cmd);
565 continue;
566 case TASK_IS_AT_LU:
567 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
568 reset:
569 tmf_resp = sas_recover_lu(task->dev, cmd);
570 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
571 SAS_DPRINTK("dev %016llx LU %x is "
572 "recovered\n",
573 SAS_ADDR(task->dev),
574 cmd->device->lun);
575 sas_eh_finish_cmd(cmd);
576 sas_scsi_clear_queue_lu(work_q, cmd);
577 goto Again;
579 /* fallthrough */
580 case TASK_IS_NOT_AT_LU:
581 case TASK_ABORT_FAILED:
582 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
583 task);
584 tmf_resp = sas_recover_I_T(task->dev);
585 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
586 struct domain_device *dev = task->dev;
587 SAS_DPRINTK("I_T %016llx recovered\n",
588 SAS_ADDR(task->dev->sas_addr));
589 sas_eh_finish_cmd(cmd);
590 sas_scsi_clear_queue_I_T(work_q, dev);
591 goto Again;
593 /* Hammer time :-) */
594 try_to_reset_cmd_device(cmd);
595 if (i->dft->lldd_clear_nexus_port) {
596 struct asd_sas_port *port = task->dev->port;
597 SAS_DPRINTK("clearing nexus for port:%d\n",
598 port->id);
599 res = i->dft->lldd_clear_nexus_port(port);
600 if (res == TMF_RESP_FUNC_COMPLETE) {
601 SAS_DPRINTK("clear nexus port:%d "
602 "succeeded\n", port->id);
603 sas_eh_finish_cmd(cmd);
604 sas_scsi_clear_queue_port(work_q,
605 port);
606 goto Again;
609 if (i->dft->lldd_clear_nexus_ha) {
610 SAS_DPRINTK("clear nexus ha\n");
611 res = i->dft->lldd_clear_nexus_ha(ha);
612 if (res == TMF_RESP_FUNC_COMPLETE) {
613 SAS_DPRINTK("clear nexus ha "
614 "succeeded\n");
615 sas_eh_finish_cmd(cmd);
616 goto clear_q;
619 /* If we are here -- this means that no amount
620 * of effort could recover from errors. Quite
621 * possibly the HA just disappeared.
623 SAS_DPRINTK("error from device %llx, LUN %x "
624 "couldn't be recovered in any way\n",
625 SAS_ADDR(task->dev->sas_addr),
626 cmd->device->lun);
628 sas_eh_finish_cmd(cmd);
629 goto clear_q;
632 return list_empty(work_q);
633 clear_q:
634 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
635 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
636 sas_eh_finish_cmd(cmd);
638 return list_empty(work_q);
641 void sas_scsi_recover_host(struct Scsi_Host *shost)
643 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
644 unsigned long flags;
645 LIST_HEAD(eh_work_q);
647 spin_lock_irqsave(shost->host_lock, flags);
648 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
649 shost->host_eh_scheduled = 0;
650 spin_unlock_irqrestore(shost->host_lock, flags);
652 SAS_DPRINTK("Enter %s\n", __func__);
654 * Deal with commands that still have SAS tasks (i.e. they didn't
655 * complete via the normal sas_task completion mechanism)
657 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
658 goto out;
661 * Now deal with SCSI commands that completed ok but have a an error
662 * code (and hopefully sense data) attached. This is roughly what
663 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
664 * command we see here has no sas_task and is thus unknown to the HA.
666 if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
667 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
668 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
670 out:
671 /* now link into libata eh --- if we have any ata devices */
672 sas_ata_strategy_handler(shost);
674 scsi_eh_flush_done_q(&ha->eh_done_q);
676 SAS_DPRINTK("--- Exit %s\n", __func__);
677 return;
680 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
682 struct sas_task *task = TO_SAS_TASK(cmd);
683 unsigned long flags;
684 enum blk_eh_timer_return rtn;
686 if (sas_ata_timed_out(cmd, task, &rtn))
687 return rtn;
689 if (!task) {
690 cmd->request->timeout /= 2;
691 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
692 cmd, task, (cmd->request->timeout ?
693 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
694 if (!cmd->request->timeout)
695 return BLK_EH_NOT_HANDLED;
696 return BLK_EH_RESET_TIMER;
699 spin_lock_irqsave(&task->task_state_lock, flags);
700 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
701 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
702 spin_unlock_irqrestore(&task->task_state_lock, flags);
703 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
704 "BLK_EH_HANDLED\n", cmd, task);
705 return BLK_EH_HANDLED;
707 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
708 spin_unlock_irqrestore(&task->task_state_lock, flags);
709 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
710 "BLK_EH_RESET_TIMER\n",
711 cmd, task);
712 return BLK_EH_RESET_TIMER;
714 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
715 spin_unlock_irqrestore(&task->task_state_lock, flags);
717 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
718 cmd, task);
720 return BLK_EH_NOT_HANDLED;
723 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
725 struct domain_device *dev = sdev_to_domain_dev(sdev);
727 if (dev_is_sata(dev))
728 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
730 return -EINVAL;
733 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
735 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
736 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
737 struct domain_device *found_dev = NULL;
738 int i;
739 unsigned long flags;
741 spin_lock_irqsave(&ha->phy_port_lock, flags);
742 for (i = 0; i < ha->num_phys; i++) {
743 struct asd_sas_port *port = ha->sas_port[i];
744 struct domain_device *dev;
746 spin_lock(&port->dev_list_lock);
747 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
748 if (rphy == dev->rphy) {
749 found_dev = dev;
750 spin_unlock(&port->dev_list_lock);
751 goto found;
754 spin_unlock(&port->dev_list_lock);
756 found:
757 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
759 return found_dev;
762 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
764 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
766 return sas_find_dev_by_rphy(rphy);
769 int sas_target_alloc(struct scsi_target *starget)
771 struct domain_device *found_dev = sas_find_target(starget);
772 int res;
774 if (!found_dev)
775 return -ENODEV;
777 if (dev_is_sata(found_dev)) {
778 res = sas_ata_init_host_and_port(found_dev, starget);
779 if (res)
780 return res;
783 starget->hostdata = found_dev;
784 return 0;
787 #define SAS_DEF_QD 32
788 #define SAS_MAX_QD 64
790 int sas_slave_configure(struct scsi_device *scsi_dev)
792 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
793 struct sas_ha_struct *sas_ha;
795 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
797 if (dev_is_sata(dev)) {
798 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
799 return 0;
802 sas_ha = dev->port->ha;
804 sas_read_port_mode_page(scsi_dev);
806 if (scsi_dev->tagged_supported) {
807 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
808 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
809 } else {
810 SAS_DPRINTK("device %llx, LUN %x doesn't support "
811 "TCQ\n", SAS_ADDR(dev->sas_addr),
812 scsi_dev->lun);
813 scsi_dev->tagged_supported = 0;
814 scsi_set_tag_type(scsi_dev, 0);
815 scsi_deactivate_tcq(scsi_dev, 1);
818 scsi_dev->allow_restart = 1;
820 return 0;
823 void sas_slave_destroy(struct scsi_device *scsi_dev)
825 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
827 if (dev_is_sata(dev))
828 dev->sata_dev.ap->link.device[0].class = ATA_DEV_NONE;
831 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth,
832 int reason)
834 int res = min(new_depth, SAS_MAX_QD);
836 if (reason != SCSI_QDEPTH_DEFAULT)
837 return -EOPNOTSUPP;
839 if (scsi_dev->tagged_supported)
840 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
841 res);
842 else {
843 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
844 sas_printk("device %llx LUN %x queue depth changed to 1\n",
845 SAS_ADDR(dev->sas_addr),
846 scsi_dev->lun);
847 scsi_adjust_queue_depth(scsi_dev, 0, 1);
848 res = 1;
851 return res;
854 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
856 if (!scsi_dev->tagged_supported)
857 return 0;
859 scsi_deactivate_tcq(scsi_dev, 1);
861 scsi_set_tag_type(scsi_dev, qt);
862 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
864 return qt;
867 int sas_bios_param(struct scsi_device *scsi_dev,
868 struct block_device *bdev,
869 sector_t capacity, int *hsc)
871 hsc[0] = 255;
872 hsc[1] = 63;
873 sector_div(capacity, 255*63);
874 hsc[2] = capacity;
876 return 0;
879 /* ---------- Task Collector Thread implementation ---------- */
881 static void sas_queue(struct sas_ha_struct *sas_ha)
883 struct scsi_core *core = &sas_ha->core;
884 unsigned long flags;
885 LIST_HEAD(q);
886 int can_queue;
887 int res;
888 struct sas_internal *i = to_sas_internal(core->shost->transportt);
890 spin_lock_irqsave(&core->task_queue_lock, flags);
891 while (!kthread_should_stop() &&
892 !list_empty(&core->task_queue)) {
894 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
895 if (can_queue >= 0) {
896 can_queue = core->task_queue_size;
897 list_splice_init(&core->task_queue, &q);
898 } else {
899 struct list_head *a, *n;
901 can_queue = sas_ha->lldd_queue_size;
902 list_for_each_safe(a, n, &core->task_queue) {
903 list_move_tail(a, &q);
904 if (--can_queue == 0)
905 break;
907 can_queue = sas_ha->lldd_queue_size;
909 core->task_queue_size -= can_queue;
910 spin_unlock_irqrestore(&core->task_queue_lock, flags);
912 struct sas_task *task = list_entry(q.next,
913 struct sas_task,
914 list);
915 list_del_init(&q);
916 res = i->dft->lldd_execute_task(task, can_queue,
917 GFP_KERNEL);
918 if (unlikely(res))
919 __list_add(&q, task->list.prev, &task->list);
921 spin_lock_irqsave(&core->task_queue_lock, flags);
922 if (res) {
923 list_splice_init(&q, &core->task_queue); /*at head*/
924 core->task_queue_size += can_queue;
927 spin_unlock_irqrestore(&core->task_queue_lock, flags);
931 * sas_queue_thread -- The Task Collector thread
932 * @_sas_ha: pointer to struct sas_ha
934 static int sas_queue_thread(void *_sas_ha)
936 struct sas_ha_struct *sas_ha = _sas_ha;
938 while (1) {
939 set_current_state(TASK_INTERRUPTIBLE);
940 schedule();
941 sas_queue(sas_ha);
942 if (kthread_should_stop())
943 break;
946 return 0;
949 int sas_init_queue(struct sas_ha_struct *sas_ha)
951 struct scsi_core *core = &sas_ha->core;
953 spin_lock_init(&core->task_queue_lock);
954 core->task_queue_size = 0;
955 INIT_LIST_HEAD(&core->task_queue);
957 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
958 "sas_queue_%d", core->shost->host_no);
959 if (IS_ERR(core->queue_thread))
960 return PTR_ERR(core->queue_thread);
961 return 0;
964 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
966 unsigned long flags;
967 struct scsi_core *core = &sas_ha->core;
968 struct sas_task *task, *n;
970 kthread_stop(core->queue_thread);
972 if (!list_empty(&core->task_queue))
973 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
974 SAS_ADDR(sas_ha->sas_addr));
976 spin_lock_irqsave(&core->task_queue_lock, flags);
977 list_for_each_entry_safe(task, n, &core->task_queue, list) {
978 struct scsi_cmnd *cmd = task->uldd_task;
980 list_del_init(&task->list);
982 ASSIGN_SAS_TASK(cmd, NULL);
983 sas_free_task(task);
984 cmd->result = DID_ABORT << 16;
985 cmd->scsi_done(cmd);
987 spin_unlock_irqrestore(&core->task_queue_lock, flags);
991 * Call the LLDD task abort routine directly. This function is intended for
992 * use by upper layers that need to tell the LLDD to abort a task.
994 int __sas_task_abort(struct sas_task *task)
996 struct sas_internal *si =
997 to_sas_internal(task->dev->port->ha->core.shost->transportt);
998 unsigned long flags;
999 int res;
1001 spin_lock_irqsave(&task->task_state_lock, flags);
1002 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
1003 task->task_state_flags & SAS_TASK_STATE_DONE) {
1004 spin_unlock_irqrestore(&task->task_state_lock, flags);
1005 SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
1006 task);
1007 return 0;
1009 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1010 spin_unlock_irqrestore(&task->task_state_lock, flags);
1012 if (!si->dft->lldd_abort_task)
1013 return -ENODEV;
1015 res = si->dft->lldd_abort_task(task);
1017 spin_lock_irqsave(&task->task_state_lock, flags);
1018 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
1019 (res == TMF_RESP_FUNC_COMPLETE))
1021 spin_unlock_irqrestore(&task->task_state_lock, flags);
1022 task->task_done(task);
1023 return 0;
1026 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1027 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1028 spin_unlock_irqrestore(&task->task_state_lock, flags);
1030 return -EAGAIN;
1034 * Tell an upper layer that it needs to initiate an abort for a given task.
1035 * This should only ever be called by an LLDD.
1037 void sas_task_abort(struct sas_task *task)
1039 struct scsi_cmnd *sc = task->uldd_task;
1041 /* Escape for libsas internal commands */
1042 if (!sc) {
1043 if (!del_timer(&task->timer))
1044 return;
1045 task->timer.function(task->timer.data);
1046 return;
1049 if (dev_is_sata(task->dev)) {
1050 sas_ata_task_abort(task);
1051 } else {
1052 struct request_queue *q = sc->device->request_queue;
1053 unsigned long flags;
1055 spin_lock_irqsave(q->queue_lock, flags);
1056 blk_abort_request(sc->request);
1057 spin_unlock_irqrestore(q->queue_lock, flags);
1058 scsi_schedule_eh(sc->device->host);
1062 int sas_slave_alloc(struct scsi_device *scsi_dev)
1064 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1066 if (dev_is_sata(dev))
1067 return ata_sas_port_init(dev->sata_dev.ap);
1069 return 0;
1072 void sas_target_destroy(struct scsi_target *starget)
1074 struct domain_device *found_dev = sas_find_target(starget);
1076 if (!found_dev)
1077 return;
1079 if (dev_is_sata(found_dev))
1080 ata_sas_port_destroy(found_dev->sata_dev.ap);
1082 return;
1085 static void sas_parse_addr(u8 *sas_addr, const char *p)
1087 int i;
1088 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1089 u8 h, l;
1090 if (!*p)
1091 break;
1092 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1093 p++;
1094 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1095 p++;
1096 sas_addr[i] = (h<<4) | l;
1100 #define SAS_STRING_ADDR_SIZE 16
1102 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1104 int res;
1105 const struct firmware *fw;
1107 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1108 if (res)
1109 return res;
1111 if (fw->size < SAS_STRING_ADDR_SIZE) {
1112 res = -ENODEV;
1113 goto out;
1116 sas_parse_addr(addr, fw->data);
1118 out:
1119 release_firmware(fw);
1120 return res;
1122 EXPORT_SYMBOL_GPL(sas_request_addr);
1124 EXPORT_SYMBOL_GPL(sas_queuecommand);
1125 EXPORT_SYMBOL_GPL(sas_target_alloc);
1126 EXPORT_SYMBOL_GPL(sas_slave_configure);
1127 EXPORT_SYMBOL_GPL(sas_slave_destroy);
1128 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1129 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1130 EXPORT_SYMBOL_GPL(sas_bios_param);
1131 EXPORT_SYMBOL_GPL(__sas_task_abort);
1132 EXPORT_SYMBOL_GPL(sas_task_abort);
1133 EXPORT_SYMBOL_GPL(sas_phy_reset);
1134 EXPORT_SYMBOL_GPL(sas_phy_enable);
1135 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1136 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1137 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1138 EXPORT_SYMBOL_GPL(sas_target_destroy);
1139 EXPORT_SYMBOL_GPL(sas_ioctl);