fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / drivers / scsi / libsas / sas_scsi_host.c
blob7663841eb4cfe7af81a3ae186f332ffcf34531e4
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>
28 #include "sas_internal.h"
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_sas.h>
37 #include <scsi/sas_ata.h>
38 #include "../scsi_sas_internal.h"
39 #include "../scsi_transport_api.h"
40 #include "../scsi_priv.h"
42 #include <linux/err.h>
43 #include <linux/blkdev.h>
44 #include <linux/freezer.h>
45 #include <linux/scatterlist.h>
46 #include <linux/libata.h>
48 /* ---------- SCSI Host glue ---------- */
50 static void sas_scsi_task_done(struct sas_task *task)
52 struct task_status_struct *ts = &task->task_status;
53 struct scsi_cmnd *sc = task->uldd_task;
54 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
55 unsigned ts_flags = task->task_state_flags;
56 int hs = 0, stat = 0;
58 if (unlikely(!sc)) {
59 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
60 list_del_init(&task->list);
61 sas_free_task(task);
62 return;
65 if (ts->resp == SAS_TASK_UNDELIVERED) {
66 /* transport error */
67 hs = DID_NO_CONNECT;
68 } else { /* ts->resp == SAS_TASK_COMPLETE */
69 /* task delivered, what happened afterwards? */
70 switch (ts->stat) {
71 case SAS_DEV_NO_RESPONSE:
72 case SAS_INTERRUPTED:
73 case SAS_PHY_DOWN:
74 case SAS_NAK_R_ERR:
75 case SAS_OPEN_TO:
76 hs = DID_NO_CONNECT;
77 break;
78 case SAS_DATA_UNDERRUN:
79 scsi_set_resid(sc, ts->residual);
80 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
81 hs = DID_ERROR;
82 break;
83 case SAS_DATA_OVERRUN:
84 hs = DID_ERROR;
85 break;
86 case SAS_QUEUE_FULL:
87 hs = DID_SOFT_ERROR; /* retry */
88 break;
89 case SAS_DEVICE_UNKNOWN:
90 hs = DID_BAD_TARGET;
91 break;
92 case SAS_SG_ERR:
93 hs = DID_PARITY;
94 break;
95 case SAS_OPEN_REJECT:
96 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
97 hs = DID_SOFT_ERROR; /* retry */
98 else
99 hs = DID_ERROR;
100 break;
101 case SAS_PROTO_RESPONSE:
102 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
103 "task; please report this\n",
104 task->dev->port->ha->sas_ha_name);
105 break;
106 case SAS_ABORTED_TASK:
107 hs = DID_ABORT;
108 break;
109 case SAM_CHECK_COND:
110 memcpy(sc->sense_buffer, ts->buf,
111 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
112 stat = SAM_CHECK_COND;
113 break;
114 default:
115 stat = ts->stat;
116 break;
119 ASSIGN_SAS_TASK(sc, NULL);
120 sc->result = (hs << 16) | stat;
121 list_del_init(&task->list);
122 sas_free_task(task);
123 /* This is very ugly but this is how SCSI Core works. */
124 if (ts_flags & SAS_TASK_STATE_ABORTED)
125 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
126 else
127 sc->scsi_done(sc);
130 static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
132 enum task_attribute ta = TASK_ATTR_SIMPLE;
133 if (cmd->request && blk_rq_tagged(cmd->request)) {
134 if (cmd->device->ordered_tags &&
135 (cmd->request->cmd_flags & REQ_HARDBARRIER))
136 ta = TASK_ATTR_ORDERED;
138 return ta;
141 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
142 struct domain_device *dev,
143 gfp_t gfp_flags)
145 struct sas_task *task = sas_alloc_task(gfp_flags);
146 struct scsi_lun lun;
148 if (!task)
149 return NULL;
151 *(u32 *)cmd->sense_buffer = 0;
152 task->uldd_task = cmd;
153 ASSIGN_SAS_TASK(cmd, task);
155 task->dev = dev;
156 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
158 task->ssp_task.retry_count = 1;
159 int_to_scsilun(cmd->device->lun, &lun);
160 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
161 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
162 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
164 task->scatter = scsi_sglist(cmd);
165 task->num_scatter = scsi_sg_count(cmd);
166 task->total_xfer_len = scsi_bufflen(cmd);
167 task->data_dir = cmd->sc_data_direction;
169 task->task_done = sas_scsi_task_done;
171 return task;
174 int sas_queue_up(struct sas_task *task)
176 struct sas_ha_struct *sas_ha = task->dev->port->ha;
177 struct scsi_core *core = &sas_ha->core;
178 unsigned long flags;
179 LIST_HEAD(list);
181 spin_lock_irqsave(&core->task_queue_lock, flags);
182 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
183 spin_unlock_irqrestore(&core->task_queue_lock, flags);
184 return -SAS_QUEUE_FULL;
186 list_add_tail(&task->list, &core->task_queue);
187 core->task_queue_size += 1;
188 spin_unlock_irqrestore(&core->task_queue_lock, flags);
189 wake_up_process(core->queue_thread);
191 return 0;
195 * sas_queuecommand -- Enqueue a command for processing
196 * @parameters: See SCSI Core documentation
198 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
199 * call us without holding an IRQ spinlock...
201 int sas_queuecommand(struct scsi_cmnd *cmd,
202 void (*scsi_done)(struct scsi_cmnd *))
204 int res = 0;
205 struct domain_device *dev = cmd_to_domain_dev(cmd);
206 struct Scsi_Host *host = cmd->device->host;
207 struct sas_internal *i = to_sas_internal(host->transportt);
209 spin_unlock_irq(host->host_lock);
212 struct sas_ha_struct *sas_ha = dev->port->ha;
213 struct sas_task *task;
215 if (dev_is_sata(dev)) {
216 unsigned long flags;
218 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
219 res = ata_sas_queuecmd(cmd, scsi_done,
220 dev->sata_dev.ap);
221 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
222 goto out;
225 res = -ENOMEM;
226 task = sas_create_task(cmd, dev, GFP_ATOMIC);
227 if (!task)
228 goto out;
230 cmd->scsi_done = scsi_done;
231 /* Queue up, Direct Mode or Task Collector Mode. */
232 if (sas_ha->lldd_max_execute_num < 2)
233 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
234 else
235 res = sas_queue_up(task);
237 /* Examine */
238 if (res) {
239 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
240 ASSIGN_SAS_TASK(cmd, NULL);
241 sas_free_task(task);
242 if (res == -SAS_QUEUE_FULL) {
243 cmd->result = DID_SOFT_ERROR << 16; /* retry */
244 res = 0;
245 scsi_done(cmd);
247 goto out;
250 out:
251 spin_lock_irq(host->host_lock);
252 return res;
255 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
257 struct scsi_cmnd *cmd, *n;
259 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
260 if (cmd == my_cmd)
261 list_del_init(&cmd->eh_entry);
265 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
266 struct domain_device *dev)
268 struct scsi_cmnd *cmd, *n;
270 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
271 struct domain_device *x = cmd_to_domain_dev(cmd);
273 if (x == dev)
274 list_del_init(&cmd->eh_entry);
278 static void sas_scsi_clear_queue_port(struct list_head *error_q,
279 struct asd_sas_port *port)
281 struct scsi_cmnd *cmd, *n;
283 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
284 struct domain_device *dev = cmd_to_domain_dev(cmd);
285 struct asd_sas_port *x = dev->port;
287 if (x == port)
288 list_del_init(&cmd->eh_entry);
292 enum task_disposition {
293 TASK_IS_DONE,
294 TASK_IS_ABORTED,
295 TASK_IS_AT_LU,
296 TASK_IS_NOT_AT_LU,
297 TASK_ABORT_FAILED,
300 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
302 struct sas_ha_struct *ha = task->dev->port->ha;
303 unsigned long flags;
304 int i, res;
305 struct sas_internal *si =
306 to_sas_internal(task->dev->port->ha->core.shost->transportt);
308 if (ha->lldd_max_execute_num > 1) {
309 struct scsi_core *core = &ha->core;
310 struct sas_task *t, *n;
312 spin_lock_irqsave(&core->task_queue_lock, flags);
313 list_for_each_entry_safe(t, n, &core->task_queue, list) {
314 if (task == t) {
315 list_del_init(&t->list);
316 spin_unlock_irqrestore(&core->task_queue_lock,
317 flags);
318 SAS_DPRINTK("%s: task 0x%p aborted from "
319 "task_queue\n",
320 __FUNCTION__, task);
321 return TASK_IS_ABORTED;
324 spin_unlock_irqrestore(&core->task_queue_lock, flags);
327 for (i = 0; i < 5; i++) {
328 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
329 res = si->dft->lldd_abort_task(task);
331 spin_lock_irqsave(&task->task_state_lock, flags);
332 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
333 spin_unlock_irqrestore(&task->task_state_lock, flags);
334 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
335 task);
336 return TASK_IS_DONE;
338 spin_unlock_irqrestore(&task->task_state_lock, flags);
340 if (res == TMF_RESP_FUNC_COMPLETE) {
341 SAS_DPRINTK("%s: task 0x%p is aborted\n",
342 __FUNCTION__, task);
343 return TASK_IS_ABORTED;
344 } else if (si->dft->lldd_query_task) {
345 SAS_DPRINTK("%s: querying task 0x%p\n",
346 __FUNCTION__, task);
347 res = si->dft->lldd_query_task(task);
348 switch (res) {
349 case TMF_RESP_FUNC_SUCC:
350 SAS_DPRINTK("%s: task 0x%p at LU\n",
351 __FUNCTION__, task);
352 return TASK_IS_AT_LU;
353 case TMF_RESP_FUNC_COMPLETE:
354 SAS_DPRINTK("%s: task 0x%p not at LU\n",
355 __FUNCTION__, task);
356 return TASK_IS_NOT_AT_LU;
357 case TMF_RESP_FUNC_FAILED:
358 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
359 __FUNCTION__, task);
360 return TASK_ABORT_FAILED;
365 return res;
368 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
370 int res = TMF_RESP_FUNC_FAILED;
371 struct scsi_lun lun;
372 struct sas_internal *i =
373 to_sas_internal(dev->port->ha->core.shost->transportt);
375 int_to_scsilun(cmd->device->lun, &lun);
377 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
378 SAS_ADDR(dev->sas_addr),
379 cmd->device->lun);
381 if (i->dft->lldd_abort_task_set)
382 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
384 if (res == TMF_RESP_FUNC_FAILED) {
385 if (i->dft->lldd_clear_task_set)
386 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
389 if (res == TMF_RESP_FUNC_FAILED) {
390 if (i->dft->lldd_lu_reset)
391 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
394 return res;
397 static int sas_recover_I_T(struct domain_device *dev)
399 int res = TMF_RESP_FUNC_FAILED;
400 struct sas_internal *i =
401 to_sas_internal(dev->port->ha->core.shost->transportt);
403 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
404 SAS_ADDR(dev->sas_addr));
406 if (i->dft->lldd_I_T_nexus_reset)
407 res = i->dft->lldd_I_T_nexus_reset(dev);
409 return res;
412 /* Find the sas_phy that's attached to this device */
413 struct sas_phy *find_local_sas_phy(struct domain_device *dev)
415 struct domain_device *pdev = dev->parent;
416 struct ex_phy *exphy = NULL;
417 int i;
419 /* Directly attached device */
420 if (!pdev)
421 return dev->port->phy;
423 /* Otherwise look in the expander */
424 for (i = 0; i < pdev->ex_dev.num_phys; i++)
425 if (!memcmp(dev->sas_addr,
426 pdev->ex_dev.ex_phy[i].attached_sas_addr,
427 SAS_ADDR_SIZE)) {
428 exphy = &pdev->ex_dev.ex_phy[i];
429 break;
432 BUG_ON(!exphy);
433 return exphy->phy;
436 /* Attempt to send a LUN reset message to a device */
437 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
439 struct domain_device *dev = cmd_to_domain_dev(cmd);
440 struct sas_internal *i =
441 to_sas_internal(dev->port->ha->core.shost->transportt);
442 struct scsi_lun lun;
443 int res;
445 int_to_scsilun(cmd->device->lun, &lun);
447 if (!i->dft->lldd_lu_reset)
448 return FAILED;
450 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
451 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
452 return SUCCESS;
454 return FAILED;
457 /* Attempt to send a phy (bus) reset */
458 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
460 struct domain_device *dev = cmd_to_domain_dev(cmd);
461 struct sas_phy *phy = find_local_sas_phy(dev);
462 int res;
464 res = sas_phy_reset(phy, 1);
465 if (res)
466 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
467 phy->dev.kobj.k_name,
468 res);
469 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
470 return SUCCESS;
472 return FAILED;
475 /* Try to reset a device */
476 static int try_to_reset_cmd_device(struct Scsi_Host *shost,
477 struct scsi_cmnd *cmd)
479 int res;
481 if (!shost->hostt->eh_device_reset_handler)
482 goto try_bus_reset;
484 res = shost->hostt->eh_device_reset_handler(cmd);
485 if (res == SUCCESS)
486 return res;
488 try_bus_reset:
489 if (shost->hostt->eh_bus_reset_handler)
490 return shost->hostt->eh_bus_reset_handler(cmd);
492 return FAILED;
495 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
496 struct list_head *work_q,
497 struct list_head *done_q)
499 struct scsi_cmnd *cmd, *n;
500 enum task_disposition res = TASK_IS_DONE;
501 int tmf_resp, need_reset;
502 struct sas_internal *i = to_sas_internal(shost->transportt);
503 unsigned long flags;
504 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
506 Again:
507 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
508 struct sas_task *task = TO_SAS_TASK(cmd);
510 if (!task)
511 continue;
513 list_del_init(&cmd->eh_entry);
515 spin_lock_irqsave(&task->task_state_lock, flags);
516 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
517 spin_unlock_irqrestore(&task->task_state_lock, flags);
519 SAS_DPRINTK("trying to find task 0x%p\n", task);
520 res = sas_scsi_find_task(task);
522 cmd->eh_eflags = 0;
524 switch (res) {
525 case TASK_IS_DONE:
526 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
527 task);
528 task->task_done(task);
529 if (need_reset)
530 try_to_reset_cmd_device(shost, cmd);
531 continue;
532 case TASK_IS_ABORTED:
533 SAS_DPRINTK("%s: task 0x%p is aborted\n",
534 __FUNCTION__, task);
535 task->task_done(task);
536 if (need_reset)
537 try_to_reset_cmd_device(shost, cmd);
538 continue;
539 case TASK_IS_AT_LU:
540 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
541 tmf_resp = sas_recover_lu(task->dev, cmd);
542 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
543 SAS_DPRINTK("dev %016llx LU %x is "
544 "recovered\n",
545 SAS_ADDR(task->dev),
546 cmd->device->lun);
547 task->task_done(task);
548 if (need_reset)
549 try_to_reset_cmd_device(shost, cmd);
550 sas_scsi_clear_queue_lu(work_q, cmd);
551 goto Again;
553 /* fallthrough */
554 case TASK_IS_NOT_AT_LU:
555 case TASK_ABORT_FAILED:
556 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
557 task);
558 tmf_resp = sas_recover_I_T(task->dev);
559 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
560 SAS_DPRINTK("I_T %016llx recovered\n",
561 SAS_ADDR(task->dev->sas_addr));
562 task->task_done(task);
563 if (need_reset)
564 try_to_reset_cmd_device(shost, cmd);
565 sas_scsi_clear_queue_I_T(work_q, task->dev);
566 goto Again;
568 /* Hammer time :-) */
569 if (i->dft->lldd_clear_nexus_port) {
570 struct asd_sas_port *port = task->dev->port;
571 SAS_DPRINTK("clearing nexus for port:%d\n",
572 port->id);
573 res = i->dft->lldd_clear_nexus_port(port);
574 if (res == TMF_RESP_FUNC_COMPLETE) {
575 SAS_DPRINTK("clear nexus port:%d "
576 "succeeded\n", port->id);
577 task->task_done(task);
578 if (need_reset)
579 try_to_reset_cmd_device(shost, cmd);
580 sas_scsi_clear_queue_port(work_q,
581 port);
582 goto Again;
585 if (i->dft->lldd_clear_nexus_ha) {
586 SAS_DPRINTK("clear nexus ha\n");
587 res = i->dft->lldd_clear_nexus_ha(ha);
588 if (res == TMF_RESP_FUNC_COMPLETE) {
589 SAS_DPRINTK("clear nexus ha "
590 "succeeded\n");
591 task->task_done(task);
592 if (need_reset)
593 try_to_reset_cmd_device(shost, cmd);
594 goto out;
597 /* If we are here -- this means that no amount
598 * of effort could recover from errors. Quite
599 * possibly the HA just disappeared.
601 SAS_DPRINTK("error from device %llx, LUN %x "
602 "couldn't be recovered in any way\n",
603 SAS_ADDR(task->dev->sas_addr),
604 cmd->device->lun);
606 task->task_done(task);
607 if (need_reset)
608 try_to_reset_cmd_device(shost, cmd);
609 goto clear_q;
612 out:
613 return list_empty(work_q);
614 clear_q:
615 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
616 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
617 struct sas_task *task = TO_SAS_TASK(cmd);
618 list_del_init(&cmd->eh_entry);
619 task->task_done(task);
621 return list_empty(work_q);
624 void sas_scsi_recover_host(struct Scsi_Host *shost)
626 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
627 unsigned long flags;
628 LIST_HEAD(eh_work_q);
630 spin_lock_irqsave(shost->host_lock, flags);
631 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
632 spin_unlock_irqrestore(shost->host_lock, flags);
634 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
636 * Deal with commands that still have SAS tasks (i.e. they didn't
637 * complete via the normal sas_task completion mechanism)
639 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
640 goto out;
643 * Now deal with SCSI commands that completed ok but have a an error
644 * code (and hopefully sense data) attached. This is roughly what
645 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
646 * command we see here has no sas_task and is thus unknown to the HA.
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);
651 out:
652 scsi_eh_flush_done_q(&ha->eh_done_q);
653 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
654 return;
657 enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
659 struct sas_task *task = TO_SAS_TASK(cmd);
660 unsigned long flags;
662 if (!task) {
663 cmd->timeout_per_command /= 2;
664 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
665 cmd, task, (cmd->timeout_per_command ?
666 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
667 if (!cmd->timeout_per_command)
668 return EH_NOT_HANDLED;
669 return EH_RESET_TIMER;
672 spin_lock_irqsave(&task->task_state_lock, flags);
673 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
674 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
675 spin_unlock_irqrestore(&task->task_state_lock, flags);
676 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
677 cmd, task);
678 return EH_HANDLED;
680 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
681 spin_unlock_irqrestore(&task->task_state_lock, flags);
682 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
683 "EH_RESET_TIMER\n",
684 cmd, task);
685 return EH_RESET_TIMER;
687 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
688 spin_unlock_irqrestore(&task->task_state_lock, flags);
690 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
691 cmd, task);
693 return EH_NOT_HANDLED;
696 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
698 struct domain_device *dev = sdev_to_domain_dev(sdev);
700 if (dev_is_sata(dev))
701 return ata_scsi_ioctl(sdev, cmd, arg);
703 return -EINVAL;
706 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
708 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
709 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
710 struct domain_device *found_dev = NULL;
711 int i;
712 unsigned long flags;
714 spin_lock_irqsave(&ha->phy_port_lock, flags);
715 for (i = 0; i < ha->num_phys; i++) {
716 struct asd_sas_port *port = ha->sas_port[i];
717 struct domain_device *dev;
719 spin_lock(&port->dev_list_lock);
720 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
721 if (rphy == dev->rphy) {
722 found_dev = dev;
723 spin_unlock(&port->dev_list_lock);
724 goto found;
727 spin_unlock(&port->dev_list_lock);
729 found:
730 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
732 return found_dev;
735 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
737 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
739 return sas_find_dev_by_rphy(rphy);
742 int sas_target_alloc(struct scsi_target *starget)
744 struct domain_device *found_dev = sas_find_target(starget);
745 int res;
747 if (!found_dev)
748 return -ENODEV;
750 if (dev_is_sata(found_dev)) {
751 res = sas_ata_init_host_and_port(found_dev, starget);
752 if (res)
753 return res;
756 starget->hostdata = found_dev;
757 return 0;
760 #define SAS_DEF_QD 32
761 #define SAS_MAX_QD 64
763 int sas_slave_configure(struct scsi_device *scsi_dev)
765 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
766 struct sas_ha_struct *sas_ha;
768 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
770 if (dev_is_sata(dev)) {
771 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
772 return 0;
775 sas_ha = dev->port->ha;
777 sas_read_port_mode_page(scsi_dev);
779 if (scsi_dev->tagged_supported) {
780 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
781 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
782 } else {
783 SAS_DPRINTK("device %llx, LUN %x doesn't support "
784 "TCQ\n", SAS_ADDR(dev->sas_addr),
785 scsi_dev->lun);
786 scsi_dev->tagged_supported = 0;
787 scsi_set_tag_type(scsi_dev, 0);
788 scsi_deactivate_tcq(scsi_dev, 1);
791 scsi_dev->allow_restart = 1;
793 return 0;
796 void sas_slave_destroy(struct scsi_device *scsi_dev)
798 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
800 if (dev_is_sata(dev))
801 ata_port_disable(dev->sata_dev.ap);
804 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
806 int res = min(new_depth, SAS_MAX_QD);
808 if (scsi_dev->tagged_supported)
809 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
810 res);
811 else {
812 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
813 sas_printk("device %llx LUN %x queue depth changed to 1\n",
814 SAS_ADDR(dev->sas_addr),
815 scsi_dev->lun);
816 scsi_adjust_queue_depth(scsi_dev, 0, 1);
817 res = 1;
820 return res;
823 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
825 if (!scsi_dev->tagged_supported)
826 return 0;
828 scsi_deactivate_tcq(scsi_dev, 1);
830 scsi_set_tag_type(scsi_dev, qt);
831 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
833 return qt;
836 int sas_bios_param(struct scsi_device *scsi_dev,
837 struct block_device *bdev,
838 sector_t capacity, int *hsc)
840 hsc[0] = 255;
841 hsc[1] = 63;
842 sector_div(capacity, 255*63);
843 hsc[2] = capacity;
845 return 0;
848 /* ---------- Task Collector Thread implementation ---------- */
850 static void sas_queue(struct sas_ha_struct *sas_ha)
852 struct scsi_core *core = &sas_ha->core;
853 unsigned long flags;
854 LIST_HEAD(q);
855 int can_queue;
856 int res;
857 struct sas_internal *i = to_sas_internal(core->shost->transportt);
859 spin_lock_irqsave(&core->task_queue_lock, flags);
860 while (!kthread_should_stop() &&
861 !list_empty(&core->task_queue)) {
863 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
864 if (can_queue >= 0) {
865 can_queue = core->task_queue_size;
866 list_splice_init(&core->task_queue, &q);
867 } else {
868 struct list_head *a, *n;
870 can_queue = sas_ha->lldd_queue_size;
871 list_for_each_safe(a, n, &core->task_queue) {
872 list_move_tail(a, &q);
873 if (--can_queue == 0)
874 break;
876 can_queue = sas_ha->lldd_queue_size;
878 core->task_queue_size -= can_queue;
879 spin_unlock_irqrestore(&core->task_queue_lock, flags);
881 struct sas_task *task = list_entry(q.next,
882 struct sas_task,
883 list);
884 list_del_init(&q);
885 res = i->dft->lldd_execute_task(task, can_queue,
886 GFP_KERNEL);
887 if (unlikely(res))
888 __list_add(&q, task->list.prev, &task->list);
890 spin_lock_irqsave(&core->task_queue_lock, flags);
891 if (res) {
892 list_splice_init(&q, &core->task_queue); /*at head*/
893 core->task_queue_size += can_queue;
896 spin_unlock_irqrestore(&core->task_queue_lock, flags);
900 * sas_queue_thread -- The Task Collector thread
901 * @_sas_ha: pointer to struct sas_ha
903 static int sas_queue_thread(void *_sas_ha)
905 struct sas_ha_struct *sas_ha = _sas_ha;
907 while (1) {
908 set_current_state(TASK_INTERRUPTIBLE);
909 schedule();
910 sas_queue(sas_ha);
911 if (kthread_should_stop())
912 break;
915 return 0;
918 int sas_init_queue(struct sas_ha_struct *sas_ha)
920 struct scsi_core *core = &sas_ha->core;
922 spin_lock_init(&core->task_queue_lock);
923 core->task_queue_size = 0;
924 INIT_LIST_HEAD(&core->task_queue);
926 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
927 "sas_queue_%d", core->shost->host_no);
928 if (IS_ERR(core->queue_thread))
929 return PTR_ERR(core->queue_thread);
930 return 0;
933 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
935 unsigned long flags;
936 struct scsi_core *core = &sas_ha->core;
937 struct sas_task *task, *n;
939 kthread_stop(core->queue_thread);
941 if (!list_empty(&core->task_queue))
942 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
943 SAS_ADDR(sas_ha->sas_addr));
945 spin_lock_irqsave(&core->task_queue_lock, flags);
946 list_for_each_entry_safe(task, n, &core->task_queue, list) {
947 struct scsi_cmnd *cmd = task->uldd_task;
949 list_del_init(&task->list);
951 ASSIGN_SAS_TASK(cmd, NULL);
952 sas_free_task(task);
953 cmd->result = DID_ABORT << 16;
954 cmd->scsi_done(cmd);
956 spin_unlock_irqrestore(&core->task_queue_lock, flags);
960 * Call the LLDD task abort routine directly. This function is intended for
961 * use by upper layers that need to tell the LLDD to abort a task.
963 int __sas_task_abort(struct sas_task *task)
965 struct sas_internal *si =
966 to_sas_internal(task->dev->port->ha->core.shost->transportt);
967 unsigned long flags;
968 int res;
970 spin_lock_irqsave(&task->task_state_lock, flags);
971 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
972 task->task_state_flags & SAS_TASK_STATE_DONE) {
973 spin_unlock_irqrestore(&task->task_state_lock, flags);
974 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
975 task);
976 return 0;
978 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
979 spin_unlock_irqrestore(&task->task_state_lock, flags);
981 if (!si->dft->lldd_abort_task)
982 return -ENODEV;
984 res = si->dft->lldd_abort_task(task);
986 spin_lock_irqsave(&task->task_state_lock, flags);
987 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
988 (res == TMF_RESP_FUNC_COMPLETE))
990 spin_unlock_irqrestore(&task->task_state_lock, flags);
991 task->task_done(task);
992 return 0;
995 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
996 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
997 spin_unlock_irqrestore(&task->task_state_lock, flags);
999 return -EAGAIN;
1003 * Tell an upper layer that it needs to initiate an abort for a given task.
1004 * This should only ever be called by an LLDD.
1006 void sas_task_abort(struct sas_task *task)
1008 struct scsi_cmnd *sc = task->uldd_task;
1010 /* Escape for libsas internal commands */
1011 if (!sc) {
1012 if (!del_timer(&task->timer))
1013 return;
1014 task->timer.function(task->timer.data);
1015 return;
1018 if (dev_is_sata(task->dev)) {
1019 sas_ata_task_abort(task);
1020 return;
1023 scsi_req_abort_cmd(sc);
1024 scsi_schedule_eh(sc->device->host);
1027 int sas_slave_alloc(struct scsi_device *scsi_dev)
1029 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1031 if (dev_is_sata(dev))
1032 return ata_sas_port_init(dev->sata_dev.ap);
1034 return 0;
1037 void sas_target_destroy(struct scsi_target *starget)
1039 struct domain_device *found_dev = sas_find_target(starget);
1041 if (!found_dev)
1042 return;
1044 if (dev_is_sata(found_dev))
1045 ata_sas_port_destroy(found_dev->sata_dev.ap);
1047 return;
1050 EXPORT_SYMBOL_GPL(sas_queuecommand);
1051 EXPORT_SYMBOL_GPL(sas_target_alloc);
1052 EXPORT_SYMBOL_GPL(sas_slave_configure);
1053 EXPORT_SYMBOL_GPL(sas_slave_destroy);
1054 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1055 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1056 EXPORT_SYMBOL_GPL(sas_bios_param);
1057 EXPORT_SYMBOL_GPL(__sas_task_abort);
1058 EXPORT_SYMBOL_GPL(sas_task_abort);
1059 EXPORT_SYMBOL_GPL(sas_phy_reset);
1060 EXPORT_SYMBOL_GPL(sas_phy_enable);
1061 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1062 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1063 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1064 EXPORT_SYMBOL_GPL(sas_target_destroy);
1065 EXPORT_SYMBOL_GPL(sas_ioctl);