[SCSI] libsas: Enable automatic spin-up of SAS disks
[linux-2.6/mini2440.git] / drivers / scsi / libsas / sas_scsi_host.c
blob04eace9020ba14fffd0bccf54acecb9bb4f3eada
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 "sas_internal.h"
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_sas.h>
35 #include "../scsi_sas_internal.h"
36 #include "../scsi_transport_api.h"
37 #include "../scsi_priv.h"
39 #include <linux/err.h>
40 #include <linux/blkdev.h>
41 #include <linux/scatterlist.h>
43 /* ---------- SCSI Host glue ---------- */
45 #define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
46 #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
48 static void sas_scsi_task_done(struct sas_task *task)
50 struct task_status_struct *ts = &task->task_status;
51 struct scsi_cmnd *sc = task->uldd_task;
52 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
53 unsigned ts_flags = task->task_state_flags;
54 int hs = 0, stat = 0;
56 if (unlikely(!sc)) {
57 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
58 list_del_init(&task->list);
59 sas_free_task(task);
60 return;
63 if (ts->resp == SAS_TASK_UNDELIVERED) {
64 /* transport error */
65 hs = DID_NO_CONNECT;
66 } else { /* ts->resp == SAS_TASK_COMPLETE */
67 /* task delivered, what happened afterwards? */
68 switch (ts->stat) {
69 case SAS_DEV_NO_RESPONSE:
70 case SAS_INTERRUPTED:
71 case SAS_PHY_DOWN:
72 case SAS_NAK_R_ERR:
73 case SAS_OPEN_TO:
74 hs = DID_NO_CONNECT;
75 break;
76 case SAS_DATA_UNDERRUN:
77 sc->resid = ts->residual;
78 if (sc->request_bufflen - sc->resid < sc->underflow)
79 hs = DID_ERROR;
80 break;
81 case SAS_DATA_OVERRUN:
82 hs = DID_ERROR;
83 break;
84 case SAS_QUEUE_FULL:
85 hs = DID_SOFT_ERROR; /* retry */
86 break;
87 case SAS_DEVICE_UNKNOWN:
88 hs = DID_BAD_TARGET;
89 break;
90 case SAS_SG_ERR:
91 hs = DID_PARITY;
92 break;
93 case SAS_OPEN_REJECT:
94 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
95 hs = DID_SOFT_ERROR; /* retry */
96 else
97 hs = DID_ERROR;
98 break;
99 case SAS_PROTO_RESPONSE:
100 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
101 "task; please report this\n",
102 task->dev->port->ha->sas_ha_name);
103 break;
104 case SAS_ABORTED_TASK:
105 hs = DID_ABORT;
106 break;
107 case SAM_CHECK_COND:
108 memcpy(sc->sense_buffer, ts->buf,
109 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
110 stat = SAM_CHECK_COND;
111 break;
112 default:
113 stat = ts->stat;
114 break;
117 ASSIGN_SAS_TASK(sc, NULL);
118 sc->result = (hs << 16) | stat;
119 list_del_init(&task->list);
120 sas_free_task(task);
121 /* This is very ugly but this is how SCSI Core works. */
122 if (ts_flags & SAS_TASK_STATE_ABORTED)
123 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
124 else
125 sc->scsi_done(sc);
128 static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
130 enum task_attribute ta = TASK_ATTR_SIMPLE;
131 if (cmd->request && blk_rq_tagged(cmd->request)) {
132 if (cmd->device->ordered_tags &&
133 (cmd->request->cmd_flags & REQ_HARDBARRIER))
134 ta = TASK_ATTR_HOQ;
136 return ta;
139 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
140 struct domain_device *dev,
141 gfp_t gfp_flags)
143 struct sas_task *task = sas_alloc_task(gfp_flags);
144 struct scsi_lun lun;
146 if (!task)
147 return NULL;
149 *(u32 *)cmd->sense_buffer = 0;
150 task->uldd_task = cmd;
151 ASSIGN_SAS_TASK(cmd, task);
153 task->dev = dev;
154 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
156 task->ssp_task.retry_count = 1;
157 int_to_scsilun(cmd->device->lun, &lun);
158 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
159 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
160 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
162 task->scatter = cmd->request_buffer;
163 task->num_scatter = cmd->use_sg;
164 task->total_xfer_len = cmd->request_bufflen;
165 task->data_dir = cmd->sc_data_direction;
167 task->task_done = sas_scsi_task_done;
169 return task;
172 static int sas_queue_up(struct sas_task *task)
174 struct sas_ha_struct *sas_ha = task->dev->port->ha;
175 struct scsi_core *core = &sas_ha->core;
176 unsigned long flags;
177 LIST_HEAD(list);
179 spin_lock_irqsave(&core->task_queue_lock, flags);
180 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
181 spin_unlock_irqrestore(&core->task_queue_lock, flags);
182 return -SAS_QUEUE_FULL;
184 list_add_tail(&task->list, &core->task_queue);
185 core->task_queue_size += 1;
186 spin_unlock_irqrestore(&core->task_queue_lock, flags);
187 up(&core->queue_thread_sema);
189 return 0;
193 * sas_queuecommand -- Enqueue a command for processing
194 * @parameters: See SCSI Core documentation
196 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
197 * call us without holding an IRQ spinlock...
199 int sas_queuecommand(struct scsi_cmnd *cmd,
200 void (*scsi_done)(struct scsi_cmnd *))
202 int res = 0;
203 struct domain_device *dev = cmd_to_domain_dev(cmd);
204 struct Scsi_Host *host = cmd->device->host;
205 struct sas_internal *i = to_sas_internal(host->transportt);
207 spin_unlock_irq(host->host_lock);
210 struct sas_ha_struct *sas_ha = dev->port->ha;
211 struct sas_task *task;
213 res = -ENOMEM;
214 task = sas_create_task(cmd, dev, GFP_ATOMIC);
215 if (!task)
216 goto out;
218 cmd->scsi_done = scsi_done;
219 /* Queue up, Direct Mode or Task Collector Mode. */
220 if (sas_ha->lldd_max_execute_num < 2)
221 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
222 else
223 res = sas_queue_up(task);
225 /* Examine */
226 if (res) {
227 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
228 ASSIGN_SAS_TASK(cmd, NULL);
229 sas_free_task(task);
230 if (res == -SAS_QUEUE_FULL) {
231 cmd->result = DID_SOFT_ERROR << 16; /* retry */
232 res = 0;
233 scsi_done(cmd);
235 goto out;
238 out:
239 spin_lock_irq(host->host_lock);
240 return res;
243 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
245 struct scsi_cmnd *cmd, *n;
247 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
248 if (cmd == my_cmd)
249 list_del_init(&cmd->eh_entry);
253 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
254 struct domain_device *dev)
256 struct scsi_cmnd *cmd, *n;
258 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
259 struct domain_device *x = cmd_to_domain_dev(cmd);
261 if (x == dev)
262 list_del_init(&cmd->eh_entry);
266 static void sas_scsi_clear_queue_port(struct list_head *error_q,
267 struct asd_sas_port *port)
269 struct scsi_cmnd *cmd, *n;
271 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
272 struct domain_device *dev = cmd_to_domain_dev(cmd);
273 struct asd_sas_port *x = dev->port;
275 if (x == port)
276 list_del_init(&cmd->eh_entry);
280 enum task_disposition {
281 TASK_IS_DONE,
282 TASK_IS_ABORTED,
283 TASK_IS_AT_LU,
284 TASK_IS_NOT_AT_LU,
285 TASK_ABORT_FAILED,
288 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
290 struct sas_ha_struct *ha = task->dev->port->ha;
291 unsigned long flags;
292 int i, res;
293 struct sas_internal *si =
294 to_sas_internal(task->dev->port->ha->core.shost->transportt);
296 if (ha->lldd_max_execute_num > 1) {
297 struct scsi_core *core = &ha->core;
298 struct sas_task *t, *n;
300 spin_lock_irqsave(&core->task_queue_lock, flags);
301 list_for_each_entry_safe(t, n, &core->task_queue, list) {
302 if (task == t) {
303 list_del_init(&t->list);
304 spin_unlock_irqrestore(&core->task_queue_lock,
305 flags);
306 SAS_DPRINTK("%s: task 0x%p aborted from "
307 "task_queue\n",
308 __FUNCTION__, task);
309 return TASK_IS_ABORTED;
312 spin_unlock_irqrestore(&core->task_queue_lock, flags);
315 for (i = 0; i < 5; i++) {
316 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
317 res = si->dft->lldd_abort_task(task);
319 spin_lock_irqsave(&task->task_state_lock, flags);
320 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
321 spin_unlock_irqrestore(&task->task_state_lock, flags);
322 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
323 task);
324 return TASK_IS_DONE;
326 spin_unlock_irqrestore(&task->task_state_lock, flags);
328 if (res == TMF_RESP_FUNC_COMPLETE) {
329 SAS_DPRINTK("%s: task 0x%p is aborted\n",
330 __FUNCTION__, task);
331 return TASK_IS_ABORTED;
332 } else if (si->dft->lldd_query_task) {
333 SAS_DPRINTK("%s: querying task 0x%p\n",
334 __FUNCTION__, task);
335 res = si->dft->lldd_query_task(task);
336 switch (res) {
337 case TMF_RESP_FUNC_SUCC:
338 SAS_DPRINTK("%s: task 0x%p at LU\n",
339 __FUNCTION__, task);
340 return TASK_IS_AT_LU;
341 case TMF_RESP_FUNC_COMPLETE:
342 SAS_DPRINTK("%s: task 0x%p not at LU\n",
343 __FUNCTION__, task);
344 return TASK_IS_NOT_AT_LU;
345 case TMF_RESP_FUNC_FAILED:
346 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
347 __FUNCTION__, task);
348 return TASK_ABORT_FAILED;
353 return res;
356 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
358 int res = TMF_RESP_FUNC_FAILED;
359 struct scsi_lun lun;
360 struct sas_internal *i =
361 to_sas_internal(dev->port->ha->core.shost->transportt);
363 int_to_scsilun(cmd->device->lun, &lun);
365 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
366 SAS_ADDR(dev->sas_addr),
367 cmd->device->lun);
369 if (i->dft->lldd_abort_task_set)
370 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
372 if (res == TMF_RESP_FUNC_FAILED) {
373 if (i->dft->lldd_clear_task_set)
374 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
377 if (res == TMF_RESP_FUNC_FAILED) {
378 if (i->dft->lldd_lu_reset)
379 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
382 return res;
385 static int sas_recover_I_T(struct domain_device *dev)
387 int res = TMF_RESP_FUNC_FAILED;
388 struct sas_internal *i =
389 to_sas_internal(dev->port->ha->core.shost->transportt);
391 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
392 SAS_ADDR(dev->sas_addr));
394 if (i->dft->lldd_I_T_nexus_reset)
395 res = i->dft->lldd_I_T_nexus_reset(dev);
397 return res;
400 /* Find the sas_phy that's attached to this device */
401 struct sas_phy *find_local_sas_phy(struct domain_device *dev)
403 struct domain_device *pdev = dev->parent;
404 struct ex_phy *exphy = NULL;
405 int i;
407 /* Directly attached device */
408 if (!pdev)
409 return dev->port->phy;
411 /* Otherwise look in the expander */
412 for (i = 0; i < pdev->ex_dev.num_phys; i++)
413 if (!memcmp(dev->sas_addr,
414 pdev->ex_dev.ex_phy[i].attached_sas_addr,
415 SAS_ADDR_SIZE)) {
416 exphy = &pdev->ex_dev.ex_phy[i];
417 break;
420 BUG_ON(!exphy);
421 return exphy->phy;
424 /* Attempt to send a target reset message to a device */
425 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
427 struct domain_device *dev = cmd_to_domain_dev(cmd);
428 struct sas_phy *phy = find_local_sas_phy(dev);
429 int res;
431 res = sas_phy_reset(phy, 1);
432 if (res)
433 SAS_DPRINTK("Device reset of %s failed 0x%x\n",
434 phy->dev.kobj.k_name,
435 res);
436 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
437 return SUCCESS;
439 return FAILED;
442 /* Try to reset a device */
443 static int try_to_reset_cmd_device(struct Scsi_Host *shost,
444 struct scsi_cmnd *cmd)
446 if (!shost->hostt->eh_device_reset_handler)
447 return FAILED;
449 return shost->hostt->eh_device_reset_handler(cmd);
452 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
453 struct list_head *work_q,
454 struct list_head *done_q)
456 struct scsi_cmnd *cmd, *n;
457 enum task_disposition res = TASK_IS_DONE;
458 int tmf_resp, need_reset;
459 struct sas_internal *i = to_sas_internal(shost->transportt);
460 unsigned long flags;
461 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
463 Again:
464 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
465 struct sas_task *task = TO_SAS_TASK(cmd);
467 if (!task)
468 continue;
470 list_del_init(&cmd->eh_entry);
472 spin_lock_irqsave(&task->task_state_lock, flags);
473 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
474 spin_unlock_irqrestore(&task->task_state_lock, flags);
476 SAS_DPRINTK("trying to find task 0x%p\n", task);
477 res = sas_scsi_find_task(task);
479 cmd->eh_eflags = 0;
481 switch (res) {
482 case TASK_IS_DONE:
483 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
484 task);
485 task->task_done(task);
486 if (need_reset)
487 try_to_reset_cmd_device(shost, cmd);
488 continue;
489 case TASK_IS_ABORTED:
490 SAS_DPRINTK("%s: task 0x%p is aborted\n",
491 __FUNCTION__, task);
492 task->task_done(task);
493 if (need_reset)
494 try_to_reset_cmd_device(shost, cmd);
495 continue;
496 case TASK_IS_AT_LU:
497 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
498 tmf_resp = sas_recover_lu(task->dev, cmd);
499 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
500 SAS_DPRINTK("dev %016llx LU %x is "
501 "recovered\n",
502 SAS_ADDR(task->dev),
503 cmd->device->lun);
504 task->task_done(task);
505 if (need_reset)
506 try_to_reset_cmd_device(shost, cmd);
507 sas_scsi_clear_queue_lu(work_q, cmd);
508 goto Again;
510 /* fallthrough */
511 case TASK_IS_NOT_AT_LU:
512 case TASK_ABORT_FAILED:
513 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
514 task);
515 tmf_resp = sas_recover_I_T(task->dev);
516 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
517 SAS_DPRINTK("I_T %016llx recovered\n",
518 SAS_ADDR(task->dev->sas_addr));
519 task->task_done(task);
520 if (need_reset)
521 try_to_reset_cmd_device(shost, cmd);
522 sas_scsi_clear_queue_I_T(work_q, task->dev);
523 goto Again;
525 /* Hammer time :-) */
526 if (i->dft->lldd_clear_nexus_port) {
527 struct asd_sas_port *port = task->dev->port;
528 SAS_DPRINTK("clearing nexus for port:%d\n",
529 port->id);
530 res = i->dft->lldd_clear_nexus_port(port);
531 if (res == TMF_RESP_FUNC_COMPLETE) {
532 SAS_DPRINTK("clear nexus port:%d "
533 "succeeded\n", port->id);
534 task->task_done(task);
535 if (need_reset)
536 try_to_reset_cmd_device(shost, cmd);
537 sas_scsi_clear_queue_port(work_q,
538 port);
539 goto Again;
542 if (i->dft->lldd_clear_nexus_ha) {
543 SAS_DPRINTK("clear nexus ha\n");
544 res = i->dft->lldd_clear_nexus_ha(ha);
545 if (res == TMF_RESP_FUNC_COMPLETE) {
546 SAS_DPRINTK("clear nexus ha "
547 "succeeded\n");
548 task->task_done(task);
549 if (need_reset)
550 try_to_reset_cmd_device(shost, cmd);
551 goto out;
554 /* If we are here -- this means that no amount
555 * of effort could recover from errors. Quite
556 * possibly the HA just disappeared.
558 SAS_DPRINTK("error from device %llx, LUN %x "
559 "couldn't be recovered in any way\n",
560 SAS_ADDR(task->dev->sas_addr),
561 cmd->device->lun);
563 task->task_done(task);
564 if (need_reset)
565 try_to_reset_cmd_device(shost, cmd);
566 goto clear_q;
569 out:
570 return list_empty(work_q);
571 clear_q:
572 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
573 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
574 struct sas_task *task = TO_SAS_TASK(cmd);
575 list_del_init(&cmd->eh_entry);
576 task->task_done(task);
578 return list_empty(work_q);
581 void sas_scsi_recover_host(struct Scsi_Host *shost)
583 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
584 unsigned long flags;
585 LIST_HEAD(eh_work_q);
587 spin_lock_irqsave(shost->host_lock, flags);
588 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
589 spin_unlock_irqrestore(shost->host_lock, flags);
591 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
593 * Deal with commands that still have SAS tasks (i.e. they didn't
594 * complete via the normal sas_task completion mechanism)
596 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
597 goto out;
600 * Now deal with SCSI commands that completed ok but have a an error
601 * code (and hopefully sense data) attached. This is roughly what
602 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
603 * command we see here has no sas_task and is thus unknown to the HA.
605 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
606 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
608 out:
609 scsi_eh_flush_done_q(&ha->eh_done_q);
610 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
611 return;
614 enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
616 struct sas_task *task = TO_SAS_TASK(cmd);
617 unsigned long flags;
619 if (!task) {
620 cmd->timeout_per_command /= 2;
621 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
622 cmd, task, (cmd->timeout_per_command ?
623 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
624 if (!cmd->timeout_per_command)
625 return EH_NOT_HANDLED;
626 return EH_RESET_TIMER;
629 spin_lock_irqsave(&task->task_state_lock, flags);
630 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
631 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
632 spin_unlock_irqrestore(&task->task_state_lock, flags);
633 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
634 cmd, task);
635 return EH_HANDLED;
637 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
638 spin_unlock_irqrestore(&task->task_state_lock, flags);
639 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
640 "EH_RESET_TIMER\n",
641 cmd, task);
642 return EH_RESET_TIMER;
644 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
645 spin_unlock_irqrestore(&task->task_state_lock, flags);
647 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
648 cmd, task);
650 return EH_NOT_HANDLED;
653 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
655 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
656 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
657 struct domain_device *found_dev = NULL;
658 int i;
659 unsigned long flags;
661 spin_lock_irqsave(&ha->phy_port_lock, flags);
662 for (i = 0; i < ha->num_phys; i++) {
663 struct asd_sas_port *port = ha->sas_port[i];
664 struct domain_device *dev;
666 spin_lock(&port->dev_list_lock);
667 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
668 if (rphy == dev->rphy) {
669 found_dev = dev;
670 spin_unlock(&port->dev_list_lock);
671 goto found;
674 spin_unlock(&port->dev_list_lock);
676 found:
677 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
679 return found_dev;
682 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
684 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
686 return sas_find_dev_by_rphy(rphy);
689 int sas_target_alloc(struct scsi_target *starget)
691 struct domain_device *found_dev = sas_find_target(starget);
693 if (!found_dev)
694 return -ENODEV;
696 starget->hostdata = found_dev;
697 return 0;
700 #define SAS_DEF_QD 32
701 #define SAS_MAX_QD 64
703 int sas_slave_configure(struct scsi_device *scsi_dev)
705 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
706 struct sas_ha_struct *sas_ha;
708 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
710 sas_ha = dev->port->ha;
712 sas_read_port_mode_page(scsi_dev);
714 if (scsi_dev->tagged_supported) {
715 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
716 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
717 } else {
718 SAS_DPRINTK("device %llx, LUN %x doesn't support "
719 "TCQ\n", SAS_ADDR(dev->sas_addr),
720 scsi_dev->lun);
721 scsi_dev->tagged_supported = 0;
722 scsi_set_tag_type(scsi_dev, 0);
723 scsi_deactivate_tcq(scsi_dev, 1);
726 scsi_dev->allow_restart = 1;
728 return 0;
731 void sas_slave_destroy(struct scsi_device *scsi_dev)
735 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
737 int res = min(new_depth, SAS_MAX_QD);
739 if (scsi_dev->tagged_supported)
740 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
741 res);
742 else {
743 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
744 sas_printk("device %llx LUN %x queue depth changed to 1\n",
745 SAS_ADDR(dev->sas_addr),
746 scsi_dev->lun);
747 scsi_adjust_queue_depth(scsi_dev, 0, 1);
748 res = 1;
751 return res;
754 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
756 if (!scsi_dev->tagged_supported)
757 return 0;
759 scsi_deactivate_tcq(scsi_dev, 1);
761 scsi_set_tag_type(scsi_dev, qt);
762 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
764 return qt;
767 int sas_bios_param(struct scsi_device *scsi_dev,
768 struct block_device *bdev,
769 sector_t capacity, int *hsc)
771 hsc[0] = 255;
772 hsc[1] = 63;
773 sector_div(capacity, 255*63);
774 hsc[2] = capacity;
776 return 0;
779 /* ---------- Task Collector Thread implementation ---------- */
781 static void sas_queue(struct sas_ha_struct *sas_ha)
783 struct scsi_core *core = &sas_ha->core;
784 unsigned long flags;
785 LIST_HEAD(q);
786 int can_queue;
787 int res;
788 struct sas_internal *i = to_sas_internal(core->shost->transportt);
790 spin_lock_irqsave(&core->task_queue_lock, flags);
791 while (!core->queue_thread_kill &&
792 !list_empty(&core->task_queue)) {
794 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
795 if (can_queue >= 0) {
796 can_queue = core->task_queue_size;
797 list_splice_init(&core->task_queue, &q);
798 } else {
799 struct list_head *a, *n;
801 can_queue = sas_ha->lldd_queue_size;
802 list_for_each_safe(a, n, &core->task_queue) {
803 list_move_tail(a, &q);
804 if (--can_queue == 0)
805 break;
807 can_queue = sas_ha->lldd_queue_size;
809 core->task_queue_size -= can_queue;
810 spin_unlock_irqrestore(&core->task_queue_lock, flags);
812 struct sas_task *task = list_entry(q.next,
813 struct sas_task,
814 list);
815 list_del_init(&q);
816 res = i->dft->lldd_execute_task(task, can_queue,
817 GFP_KERNEL);
818 if (unlikely(res))
819 __list_add(&q, task->list.prev, &task->list);
821 spin_lock_irqsave(&core->task_queue_lock, flags);
822 if (res) {
823 list_splice_init(&q, &core->task_queue); /*at head*/
824 core->task_queue_size += can_queue;
827 spin_unlock_irqrestore(&core->task_queue_lock, flags);
830 static DECLARE_COMPLETION(queue_th_comp);
833 * sas_queue_thread -- The Task Collector thread
834 * @_sas_ha: pointer to struct sas_ha
836 static int sas_queue_thread(void *_sas_ha)
838 struct sas_ha_struct *sas_ha = _sas_ha;
839 struct scsi_core *core = &sas_ha->core;
841 daemonize("sas_queue_%d", core->shost->host_no);
842 current->flags |= PF_NOFREEZE;
844 complete(&queue_th_comp);
846 while (1) {
847 down_interruptible(&core->queue_thread_sema);
848 sas_queue(sas_ha);
849 if (core->queue_thread_kill)
850 break;
853 complete(&queue_th_comp);
855 return 0;
858 int sas_init_queue(struct sas_ha_struct *sas_ha)
860 int res;
861 struct scsi_core *core = &sas_ha->core;
863 spin_lock_init(&core->task_queue_lock);
864 core->task_queue_size = 0;
865 INIT_LIST_HEAD(&core->task_queue);
866 init_MUTEX_LOCKED(&core->queue_thread_sema);
868 res = kernel_thread(sas_queue_thread, sas_ha, 0);
869 if (res >= 0)
870 wait_for_completion(&queue_th_comp);
872 return res < 0 ? res : 0;
875 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
877 unsigned long flags;
878 struct scsi_core *core = &sas_ha->core;
879 struct sas_task *task, *n;
881 init_completion(&queue_th_comp);
882 core->queue_thread_kill = 1;
883 up(&core->queue_thread_sema);
884 wait_for_completion(&queue_th_comp);
886 if (!list_empty(&core->task_queue))
887 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
888 SAS_ADDR(sas_ha->sas_addr));
890 spin_lock_irqsave(&core->task_queue_lock, flags);
891 list_for_each_entry_safe(task, n, &core->task_queue, list) {
892 struct scsi_cmnd *cmd = task->uldd_task;
894 list_del_init(&task->list);
896 ASSIGN_SAS_TASK(cmd, NULL);
897 sas_free_task(task);
898 cmd->result = DID_ABORT << 16;
899 cmd->scsi_done(cmd);
901 spin_unlock_irqrestore(&core->task_queue_lock, flags);
905 * Call the LLDD task abort routine directly. This function is intended for
906 * use by upper layers that need to tell the LLDD to abort a task.
908 int __sas_task_abort(struct sas_task *task)
910 struct sas_internal *si =
911 to_sas_internal(task->dev->port->ha->core.shost->transportt);
912 unsigned long flags;
913 int res;
915 spin_lock_irqsave(&task->task_state_lock, flags);
916 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
917 task->task_state_flags & SAS_TASK_STATE_DONE) {
918 spin_unlock_irqrestore(&task->task_state_lock, flags);
919 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
920 task);
921 return 0;
923 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
924 spin_unlock_irqrestore(&task->task_state_lock, flags);
926 if (!si->dft->lldd_abort_task)
927 return -ENODEV;
929 res = si->dft->lldd_abort_task(task);
931 spin_lock_irqsave(&task->task_state_lock, flags);
932 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
933 (res == TMF_RESP_FUNC_COMPLETE))
935 spin_unlock_irqrestore(&task->task_state_lock, flags);
936 task->task_done(task);
937 return 0;
940 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
941 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
942 spin_unlock_irqrestore(&task->task_state_lock, flags);
944 return -EAGAIN;
948 * Tell an upper layer that it needs to initiate an abort for a given task.
949 * This should only ever be called by an LLDD.
951 void sas_task_abort(struct sas_task *task)
953 struct scsi_cmnd *sc = task->uldd_task;
955 /* Escape for libsas internal commands */
956 if (!sc) {
957 if (!del_timer(&task->timer))
958 return;
959 task->timer.function(task->timer.data);
960 return;
963 scsi_req_abort_cmd(sc);
964 scsi_schedule_eh(sc->device->host);
967 EXPORT_SYMBOL_GPL(sas_queuecommand);
968 EXPORT_SYMBOL_GPL(sas_target_alloc);
969 EXPORT_SYMBOL_GPL(sas_slave_configure);
970 EXPORT_SYMBOL_GPL(sas_slave_destroy);
971 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
972 EXPORT_SYMBOL_GPL(sas_change_queue_type);
973 EXPORT_SYMBOL_GPL(sas_bios_param);
974 EXPORT_SYMBOL_GPL(__sas_task_abort);
975 EXPORT_SYMBOL_GPL(sas_task_abort);
976 EXPORT_SYMBOL_GPL(sas_phy_reset);
977 EXPORT_SYMBOL_GPL(sas_phy_enable);
978 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);