Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / scsi / libsas / sas_scsi_host.c
blob711d177dcb4243c7b803bfbccf16238f4a3926c7
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/export.h>
29 #include <linux/ctype.h>
31 #include "sas_internal.h"
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"
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>
52 /* ---------- SCSI Host glue ---------- */
54 static void sas_scsi_task_done(struct sas_task *task)
56 struct task_status_struct *ts = &task->task_status;
57 struct scsi_cmnd *sc = task->uldd_task;
58 int hs = 0, stat = 0;
60 if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
61 /* Aborted tasks will be completed by the error handler */
62 SAS_DPRINTK("task done but aborted\n");
63 return;
66 if (unlikely(!sc)) {
67 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
68 list_del_init(&task->list);
69 sas_free_task(task);
70 return;
73 if (ts->resp == SAS_TASK_UNDELIVERED) {
74 /* transport error */
75 hs = DID_NO_CONNECT;
76 } else { /* ts->resp == SAS_TASK_COMPLETE */
77 /* task delivered, what happened afterwards? */
78 switch (ts->stat) {
79 case SAS_DEV_NO_RESPONSE:
80 case SAS_INTERRUPTED:
81 case SAS_PHY_DOWN:
82 case SAS_NAK_R_ERR:
83 case SAS_OPEN_TO:
84 hs = DID_NO_CONNECT;
85 break;
86 case SAS_DATA_UNDERRUN:
87 scsi_set_resid(sc, ts->residual);
88 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
89 hs = DID_ERROR;
90 break;
91 case SAS_DATA_OVERRUN:
92 hs = DID_ERROR;
93 break;
94 case SAS_QUEUE_FULL:
95 hs = DID_SOFT_ERROR; /* retry */
96 break;
97 case SAS_DEVICE_UNKNOWN:
98 hs = DID_BAD_TARGET;
99 break;
100 case SAS_SG_ERR:
101 hs = DID_PARITY;
102 break;
103 case SAS_OPEN_REJECT:
104 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
105 hs = DID_SOFT_ERROR; /* retry */
106 else
107 hs = DID_ERROR;
108 break;
109 case SAS_PROTO_RESPONSE:
110 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
111 "task; please report this\n",
112 task->dev->port->ha->sas_ha_name);
113 break;
114 case SAS_ABORTED_TASK:
115 hs = DID_ABORT;
116 break;
117 case SAM_STAT_CHECK_CONDITION:
118 memcpy(sc->sense_buffer, ts->buf,
119 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
120 stat = SAM_STAT_CHECK_CONDITION;
121 break;
122 default:
123 stat = ts->stat;
124 break;
127 ASSIGN_SAS_TASK(sc, NULL);
128 sc->result = (hs << 16) | stat;
129 list_del_init(&task->list);
130 sas_free_task(task);
131 sc->scsi_done(sc);
134 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
135 struct domain_device *dev,
136 gfp_t gfp_flags)
138 struct sas_task *task = sas_alloc_task(gfp_flags);
139 struct scsi_lun lun;
141 if (!task)
142 return NULL;
144 task->uldd_task = cmd;
145 ASSIGN_SAS_TASK(cmd, task);
147 task->dev = dev;
148 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
150 task->ssp_task.retry_count = 1;
151 int_to_scsilun(cmd->device->lun, &lun);
152 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
153 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
154 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
156 task->scatter = scsi_sglist(cmd);
157 task->num_scatter = scsi_sg_count(cmd);
158 task->total_xfer_len = scsi_bufflen(cmd);
159 task->data_dir = cmd->sc_data_direction;
161 task->task_done = sas_scsi_task_done;
163 return task;
166 int sas_queue_up(struct sas_task *task)
168 struct sas_ha_struct *sas_ha = task->dev->port->ha;
169 struct scsi_core *core = &sas_ha->core;
170 unsigned long flags;
171 LIST_HEAD(list);
173 spin_lock_irqsave(&core->task_queue_lock, flags);
174 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
175 spin_unlock_irqrestore(&core->task_queue_lock, flags);
176 return -SAS_QUEUE_FULL;
178 list_add_tail(&task->list, &core->task_queue);
179 core->task_queue_size += 1;
180 spin_unlock_irqrestore(&core->task_queue_lock, flags);
181 wake_up_process(core->queue_thread);
183 return 0;
186 int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
188 struct sas_internal *i = to_sas_internal(host->transportt);
189 struct domain_device *dev = cmd_to_domain_dev(cmd);
190 struct sas_ha_struct *sas_ha = dev->port->ha;
191 struct sas_task *task;
192 int res = 0;
194 /* If the device fell off, no sense in issuing commands */
195 if (dev->gone) {
196 cmd->result = DID_BAD_TARGET << 16;
197 goto out_done;
200 if (dev_is_sata(dev)) {
201 unsigned long flags;
203 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
204 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
205 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
206 return res;
209 task = sas_create_task(cmd, dev, GFP_ATOMIC);
210 if (!task)
211 return SCSI_MLQUEUE_HOST_BUSY;
213 /* Queue up, Direct Mode or Task Collector Mode. */
214 if (sas_ha->lldd_max_execute_num < 2)
215 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
216 else
217 res = sas_queue_up(task);
219 if (res)
220 goto out_free_task;
221 return 0;
223 out_free_task:
224 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
225 ASSIGN_SAS_TASK(cmd, NULL);
226 sas_free_task(task);
227 if (res == -SAS_QUEUE_FULL)
228 cmd->result = DID_SOFT_ERROR << 16; /* retry */
229 else
230 cmd->result = DID_ERROR << 16;
231 out_done:
232 cmd->scsi_done(cmd);
233 return 0;
236 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
238 struct sas_task *task = TO_SAS_TASK(cmd);
239 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
241 /* remove the aborted task flag to allow the task to be
242 * completed now. At this point, we only get called following
243 * an actual abort of the task, so we should be guaranteed not
244 * to be racing with any completions from the LLD (hence we
245 * don't need the task state lock to clear the flag) */
246 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
247 /* Now call task_done. However, task will be free'd after
248 * this */
249 task->task_done(task);
250 /* now finish the command and move it on to the error
251 * handler done list, this also takes it off the
252 * error handler pending list */
253 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
256 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
258 struct scsi_cmnd *cmd, *n;
260 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
261 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
262 cmd->device->lun == my_cmd->device->lun)
263 sas_eh_finish_cmd(cmd);
267 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
268 struct domain_device *dev)
270 struct scsi_cmnd *cmd, *n;
272 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
273 struct domain_device *x = cmd_to_domain_dev(cmd);
275 if (x == dev)
276 sas_eh_finish_cmd(cmd);
280 static void sas_scsi_clear_queue_port(struct list_head *error_q,
281 struct asd_sas_port *port)
283 struct scsi_cmnd *cmd, *n;
285 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
286 struct domain_device *dev = cmd_to_domain_dev(cmd);
287 struct asd_sas_port *x = dev->port;
289 if (x == port)
290 sas_eh_finish_cmd(cmd);
294 enum task_disposition {
295 TASK_IS_DONE,
296 TASK_IS_ABORTED,
297 TASK_IS_AT_LU,
298 TASK_IS_NOT_AT_LU,
299 TASK_ABORT_FAILED,
302 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
304 struct sas_ha_struct *ha = task->dev->port->ha;
305 unsigned long flags;
306 int i, res;
307 struct sas_internal *si =
308 to_sas_internal(task->dev->port->ha->core.shost->transportt);
310 if (ha->lldd_max_execute_num > 1) {
311 struct scsi_core *core = &ha->core;
312 struct sas_task *t, *n;
314 spin_lock_irqsave(&core->task_queue_lock, flags);
315 list_for_each_entry_safe(t, n, &core->task_queue, list) {
316 if (task == t) {
317 list_del_init(&t->list);
318 spin_unlock_irqrestore(&core->task_queue_lock,
319 flags);
320 SAS_DPRINTK("%s: task 0x%p aborted from "
321 "task_queue\n",
322 __func__, task);
323 return TASK_IS_ABORTED;
326 spin_unlock_irqrestore(&core->task_queue_lock, flags);
329 for (i = 0; i < 5; i++) {
330 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
331 res = si->dft->lldd_abort_task(task);
333 spin_lock_irqsave(&task->task_state_lock, flags);
334 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
335 spin_unlock_irqrestore(&task->task_state_lock, flags);
336 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
337 task);
338 return TASK_IS_DONE;
340 spin_unlock_irqrestore(&task->task_state_lock, flags);
342 if (res == TMF_RESP_FUNC_COMPLETE) {
343 SAS_DPRINTK("%s: task 0x%p is aborted\n",
344 __func__, task);
345 return TASK_IS_ABORTED;
346 } else if (si->dft->lldd_query_task) {
347 SAS_DPRINTK("%s: querying task 0x%p\n",
348 __func__, task);
349 res = si->dft->lldd_query_task(task);
350 switch (res) {
351 case TMF_RESP_FUNC_SUCC:
352 SAS_DPRINTK("%s: task 0x%p at LU\n",
353 __func__, task);
354 return TASK_IS_AT_LU;
355 case TMF_RESP_FUNC_COMPLETE:
356 SAS_DPRINTK("%s: task 0x%p not at LU\n",
357 __func__, task);
358 return TASK_IS_NOT_AT_LU;
359 case TMF_RESP_FUNC_FAILED:
360 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
361 __func__, task);
362 return TASK_ABORT_FAILED;
367 return res;
370 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
372 int res = TMF_RESP_FUNC_FAILED;
373 struct scsi_lun lun;
374 struct sas_internal *i =
375 to_sas_internal(dev->port->ha->core.shost->transportt);
377 int_to_scsilun(cmd->device->lun, &lun);
379 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
380 SAS_ADDR(dev->sas_addr),
381 cmd->device->lun);
383 if (i->dft->lldd_abort_task_set)
384 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
386 if (res == TMF_RESP_FUNC_FAILED) {
387 if (i->dft->lldd_clear_task_set)
388 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
391 if (res == TMF_RESP_FUNC_FAILED) {
392 if (i->dft->lldd_lu_reset)
393 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
396 return res;
399 static int sas_recover_I_T(struct domain_device *dev)
401 int res = TMF_RESP_FUNC_FAILED;
402 struct sas_internal *i =
403 to_sas_internal(dev->port->ha->core.shost->transportt);
405 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
406 SAS_ADDR(dev->sas_addr));
408 if (i->dft->lldd_I_T_nexus_reset)
409 res = i->dft->lldd_I_T_nexus_reset(dev);
411 return res;
414 /* Find the sas_phy that's attached to this device */
415 struct sas_phy *sas_find_local_phy(struct domain_device *dev)
417 struct domain_device *pdev = dev->parent;
418 struct ex_phy *exphy = NULL;
419 int i;
421 /* Directly attached device */
422 if (!pdev)
423 return dev->port->phy;
425 /* Otherwise look in the expander */
426 for (i = 0; i < pdev->ex_dev.num_phys; i++)
427 if (!memcmp(dev->sas_addr,
428 pdev->ex_dev.ex_phy[i].attached_sas_addr,
429 SAS_ADDR_SIZE)) {
430 exphy = &pdev->ex_dev.ex_phy[i];
431 break;
434 BUG_ON(!exphy);
435 return exphy->phy;
437 EXPORT_SYMBOL_GPL(sas_find_local_phy);
439 /* Attempt to send a LUN reset message to a device */
440 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
442 struct domain_device *dev = cmd_to_domain_dev(cmd);
443 struct sas_internal *i =
444 to_sas_internal(dev->port->ha->core.shost->transportt);
445 struct scsi_lun lun;
446 int res;
448 int_to_scsilun(cmd->device->lun, &lun);
450 if (!i->dft->lldd_lu_reset)
451 return FAILED;
453 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
454 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
455 return SUCCESS;
457 return FAILED;
460 /* Attempt to send a phy (bus) reset */
461 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
463 struct domain_device *dev = cmd_to_domain_dev(cmd);
464 struct sas_phy *phy = sas_find_local_phy(dev);
465 int res;
467 res = sas_phy_reset(phy, 1);
468 if (res)
469 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
470 kobject_name(&phy->dev.kobj),
471 res);
472 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
473 return SUCCESS;
475 return FAILED;
478 /* Try to reset a device */
479 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
481 int res;
482 struct Scsi_Host *shost = cmd->device->host;
484 if (!shost->hostt->eh_device_reset_handler)
485 goto try_bus_reset;
487 res = shost->hostt->eh_device_reset_handler(cmd);
488 if (res == SUCCESS)
489 return res;
491 try_bus_reset:
492 if (shost->hostt->eh_bus_reset_handler)
493 return shost->hostt->eh_bus_reset_handler(cmd);
495 return FAILED;
498 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
499 struct list_head *work_q,
500 struct list_head *done_q)
502 struct scsi_cmnd *cmd, *n;
503 enum task_disposition res = TASK_IS_DONE;
504 int tmf_resp, need_reset;
505 struct sas_internal *i = to_sas_internal(shost->transportt);
506 unsigned long flags;
507 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
509 Again:
510 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
511 struct sas_task *task = TO_SAS_TASK(cmd);
513 if (!task)
514 continue;
516 list_del_init(&cmd->eh_entry);
518 spin_lock_irqsave(&task->task_state_lock, flags);
519 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
520 spin_unlock_irqrestore(&task->task_state_lock, flags);
522 if (need_reset) {
523 SAS_DPRINTK("%s: task 0x%p requests reset\n",
524 __func__, task);
525 goto reset;
528 SAS_DPRINTK("trying to find task 0x%p\n", task);
529 res = sas_scsi_find_task(task);
531 cmd->eh_eflags = 0;
533 switch (res) {
534 case TASK_IS_DONE:
535 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
536 task);
537 sas_eh_finish_cmd(cmd);
538 continue;
539 case TASK_IS_ABORTED:
540 SAS_DPRINTK("%s: task 0x%p is aborted\n",
541 __func__, task);
542 sas_eh_finish_cmd(cmd);
543 continue;
544 case TASK_IS_AT_LU:
545 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
546 reset:
547 tmf_resp = sas_recover_lu(task->dev, cmd);
548 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
549 SAS_DPRINTK("dev %016llx LU %x is "
550 "recovered\n",
551 SAS_ADDR(task->dev),
552 cmd->device->lun);
553 sas_eh_finish_cmd(cmd);
554 sas_scsi_clear_queue_lu(work_q, cmd);
555 goto Again;
557 /* fallthrough */
558 case TASK_IS_NOT_AT_LU:
559 case TASK_ABORT_FAILED:
560 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
561 task);
562 tmf_resp = sas_recover_I_T(task->dev);
563 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
564 struct domain_device *dev = task->dev;
565 SAS_DPRINTK("I_T %016llx recovered\n",
566 SAS_ADDR(task->dev->sas_addr));
567 sas_eh_finish_cmd(cmd);
568 sas_scsi_clear_queue_I_T(work_q, dev);
569 goto Again;
571 /* Hammer time :-) */
572 try_to_reset_cmd_device(cmd);
573 if (i->dft->lldd_clear_nexus_port) {
574 struct asd_sas_port *port = task->dev->port;
575 SAS_DPRINTK("clearing nexus for port:%d\n",
576 port->id);
577 res = i->dft->lldd_clear_nexus_port(port);
578 if (res == TMF_RESP_FUNC_COMPLETE) {
579 SAS_DPRINTK("clear nexus port:%d "
580 "succeeded\n", port->id);
581 sas_eh_finish_cmd(cmd);
582 sas_scsi_clear_queue_port(work_q,
583 port);
584 goto Again;
587 if (i->dft->lldd_clear_nexus_ha) {
588 SAS_DPRINTK("clear nexus ha\n");
589 res = i->dft->lldd_clear_nexus_ha(ha);
590 if (res == TMF_RESP_FUNC_COMPLETE) {
591 SAS_DPRINTK("clear nexus ha "
592 "succeeded\n");
593 sas_eh_finish_cmd(cmd);
594 goto clear_q;
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 sas_eh_finish_cmd(cmd);
607 goto clear_q;
610 return list_empty(work_q);
611 clear_q:
612 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
613 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
614 sas_eh_finish_cmd(cmd);
616 return list_empty(work_q);
619 void sas_scsi_recover_host(struct Scsi_Host *shost)
621 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
622 unsigned long flags;
623 LIST_HEAD(eh_work_q);
625 spin_lock_irqsave(shost->host_lock, flags);
626 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
627 shost->host_eh_scheduled = 0;
628 spin_unlock_irqrestore(shost->host_lock, flags);
630 SAS_DPRINTK("Enter %s\n", __func__);
632 * Deal with commands that still have SAS tasks (i.e. they didn't
633 * complete via the normal sas_task completion mechanism)
635 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
636 goto out;
639 * Now deal with SCSI commands that completed ok but have a an error
640 * code (and hopefully sense data) attached. This is roughly what
641 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
642 * command we see here has no sas_task and is thus unknown to the HA.
644 if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
645 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
646 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
648 out:
649 /* now link into libata eh --- if we have any ata devices */
650 sas_ata_strategy_handler(shost);
652 scsi_eh_flush_done_q(&ha->eh_done_q);
654 SAS_DPRINTK("--- Exit %s\n", __func__);
655 return;
658 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
660 struct sas_task *task = TO_SAS_TASK(cmd);
661 unsigned long flags;
662 enum blk_eh_timer_return rtn;
664 if (sas_ata_timed_out(cmd, task, &rtn))
665 return rtn;
667 if (!task) {
668 cmd->request->timeout /= 2;
669 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
670 cmd, task, (cmd->request->timeout ?
671 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
672 if (!cmd->request->timeout)
673 return BLK_EH_NOT_HANDLED;
674 return BLK_EH_RESET_TIMER;
677 spin_lock_irqsave(&task->task_state_lock, flags);
678 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
679 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
680 spin_unlock_irqrestore(&task->task_state_lock, flags);
681 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
682 "BLK_EH_HANDLED\n", cmd, task);
683 return BLK_EH_HANDLED;
685 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
686 spin_unlock_irqrestore(&task->task_state_lock, flags);
687 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
688 "BLK_EH_RESET_TIMER\n",
689 cmd, task);
690 return BLK_EH_RESET_TIMER;
692 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
693 spin_unlock_irqrestore(&task->task_state_lock, flags);
695 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
696 cmd, task);
698 return BLK_EH_NOT_HANDLED;
701 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
703 struct domain_device *dev = sdev_to_domain_dev(sdev);
705 if (dev_is_sata(dev))
706 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
708 return -EINVAL;
711 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
713 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
714 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
715 struct domain_device *found_dev = NULL;
716 int i;
717 unsigned long flags;
719 spin_lock_irqsave(&ha->phy_port_lock, flags);
720 for (i = 0; i < ha->num_phys; i++) {
721 struct asd_sas_port *port = ha->sas_port[i];
722 struct domain_device *dev;
724 spin_lock(&port->dev_list_lock);
725 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
726 if (rphy == dev->rphy) {
727 found_dev = dev;
728 spin_unlock(&port->dev_list_lock);
729 goto found;
732 spin_unlock(&port->dev_list_lock);
734 found:
735 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
737 return found_dev;
740 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
742 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
744 return sas_find_dev_by_rphy(rphy);
747 int sas_target_alloc(struct scsi_target *starget)
749 struct domain_device *found_dev = sas_find_target(starget);
750 int res;
752 if (!found_dev)
753 return -ENODEV;
755 if (dev_is_sata(found_dev)) {
756 res = sas_ata_init_host_and_port(found_dev, starget);
757 if (res)
758 return res;
761 starget->hostdata = found_dev;
762 return 0;
765 #define SAS_DEF_QD 32
766 #define SAS_MAX_QD 64
768 int sas_slave_configure(struct scsi_device *scsi_dev)
770 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
771 struct sas_ha_struct *sas_ha;
773 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
775 if (dev_is_sata(dev)) {
776 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
777 return 0;
780 sas_ha = dev->port->ha;
782 sas_read_port_mode_page(scsi_dev);
784 if (scsi_dev->tagged_supported) {
785 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
786 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
787 } else {
788 SAS_DPRINTK("device %llx, LUN %x doesn't support "
789 "TCQ\n", SAS_ADDR(dev->sas_addr),
790 scsi_dev->lun);
791 scsi_dev->tagged_supported = 0;
792 scsi_set_tag_type(scsi_dev, 0);
793 scsi_deactivate_tcq(scsi_dev, 1);
796 scsi_dev->allow_restart = 1;
798 return 0;
801 void sas_slave_destroy(struct scsi_device *scsi_dev)
803 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
805 if (dev_is_sata(dev))
806 dev->sata_dev.ap->link.device[0].class = ATA_DEV_NONE;
809 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth,
810 int reason)
812 int res = min(new_depth, SAS_MAX_QD);
814 if (reason != SCSI_QDEPTH_DEFAULT)
815 return -EOPNOTSUPP;
817 if (scsi_dev->tagged_supported)
818 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
819 res);
820 else {
821 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
822 sas_printk("device %llx LUN %x queue depth changed to 1\n",
823 SAS_ADDR(dev->sas_addr),
824 scsi_dev->lun);
825 scsi_adjust_queue_depth(scsi_dev, 0, 1);
826 res = 1;
829 return res;
832 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
834 if (!scsi_dev->tagged_supported)
835 return 0;
837 scsi_deactivate_tcq(scsi_dev, 1);
839 scsi_set_tag_type(scsi_dev, qt);
840 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
842 return qt;
845 int sas_bios_param(struct scsi_device *scsi_dev,
846 struct block_device *bdev,
847 sector_t capacity, int *hsc)
849 hsc[0] = 255;
850 hsc[1] = 63;
851 sector_div(capacity, 255*63);
852 hsc[2] = capacity;
854 return 0;
857 /* ---------- Task Collector Thread implementation ---------- */
859 static void sas_queue(struct sas_ha_struct *sas_ha)
861 struct scsi_core *core = &sas_ha->core;
862 unsigned long flags;
863 LIST_HEAD(q);
864 int can_queue;
865 int res;
866 struct sas_internal *i = to_sas_internal(core->shost->transportt);
868 spin_lock_irqsave(&core->task_queue_lock, flags);
869 while (!kthread_should_stop() &&
870 !list_empty(&core->task_queue)) {
872 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
873 if (can_queue >= 0) {
874 can_queue = core->task_queue_size;
875 list_splice_init(&core->task_queue, &q);
876 } else {
877 struct list_head *a, *n;
879 can_queue = sas_ha->lldd_queue_size;
880 list_for_each_safe(a, n, &core->task_queue) {
881 list_move_tail(a, &q);
882 if (--can_queue == 0)
883 break;
885 can_queue = sas_ha->lldd_queue_size;
887 core->task_queue_size -= can_queue;
888 spin_unlock_irqrestore(&core->task_queue_lock, flags);
890 struct sas_task *task = list_entry(q.next,
891 struct sas_task,
892 list);
893 list_del_init(&q);
894 res = i->dft->lldd_execute_task(task, can_queue,
895 GFP_KERNEL);
896 if (unlikely(res))
897 __list_add(&q, task->list.prev, &task->list);
899 spin_lock_irqsave(&core->task_queue_lock, flags);
900 if (res) {
901 list_splice_init(&q, &core->task_queue); /*at head*/
902 core->task_queue_size += can_queue;
905 spin_unlock_irqrestore(&core->task_queue_lock, flags);
909 * sas_queue_thread -- The Task Collector thread
910 * @_sas_ha: pointer to struct sas_ha
912 static int sas_queue_thread(void *_sas_ha)
914 struct sas_ha_struct *sas_ha = _sas_ha;
916 while (1) {
917 set_current_state(TASK_INTERRUPTIBLE);
918 schedule();
919 sas_queue(sas_ha);
920 if (kthread_should_stop())
921 break;
924 return 0;
927 int sas_init_queue(struct sas_ha_struct *sas_ha)
929 struct scsi_core *core = &sas_ha->core;
931 spin_lock_init(&core->task_queue_lock);
932 core->task_queue_size = 0;
933 INIT_LIST_HEAD(&core->task_queue);
935 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
936 "sas_queue_%d", core->shost->host_no);
937 if (IS_ERR(core->queue_thread))
938 return PTR_ERR(core->queue_thread);
939 return 0;
942 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
944 unsigned long flags;
945 struct scsi_core *core = &sas_ha->core;
946 struct sas_task *task, *n;
948 kthread_stop(core->queue_thread);
950 if (!list_empty(&core->task_queue))
951 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
952 SAS_ADDR(sas_ha->sas_addr));
954 spin_lock_irqsave(&core->task_queue_lock, flags);
955 list_for_each_entry_safe(task, n, &core->task_queue, list) {
956 struct scsi_cmnd *cmd = task->uldd_task;
958 list_del_init(&task->list);
960 ASSIGN_SAS_TASK(cmd, NULL);
961 sas_free_task(task);
962 cmd->result = DID_ABORT << 16;
963 cmd->scsi_done(cmd);
965 spin_unlock_irqrestore(&core->task_queue_lock, flags);
969 * Call the LLDD task abort routine directly. This function is intended for
970 * use by upper layers that need to tell the LLDD to abort a task.
972 int __sas_task_abort(struct sas_task *task)
974 struct sas_internal *si =
975 to_sas_internal(task->dev->port->ha->core.shost->transportt);
976 unsigned long flags;
977 int res;
979 spin_lock_irqsave(&task->task_state_lock, flags);
980 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
981 task->task_state_flags & SAS_TASK_STATE_DONE) {
982 spin_unlock_irqrestore(&task->task_state_lock, flags);
983 SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
984 task);
985 return 0;
987 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
988 spin_unlock_irqrestore(&task->task_state_lock, flags);
990 if (!si->dft->lldd_abort_task)
991 return -ENODEV;
993 res = si->dft->lldd_abort_task(task);
995 spin_lock_irqsave(&task->task_state_lock, flags);
996 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
997 (res == TMF_RESP_FUNC_COMPLETE))
999 spin_unlock_irqrestore(&task->task_state_lock, flags);
1000 task->task_done(task);
1001 return 0;
1004 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1005 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1006 spin_unlock_irqrestore(&task->task_state_lock, flags);
1008 return -EAGAIN;
1012 * Tell an upper layer that it needs to initiate an abort for a given task.
1013 * This should only ever be called by an LLDD.
1015 void sas_task_abort(struct sas_task *task)
1017 struct scsi_cmnd *sc = task->uldd_task;
1019 /* Escape for libsas internal commands */
1020 if (!sc) {
1021 if (!del_timer(&task->timer))
1022 return;
1023 task->timer.function(task->timer.data);
1024 return;
1027 if (dev_is_sata(task->dev)) {
1028 sas_ata_task_abort(task);
1029 } else {
1030 struct request_queue *q = sc->device->request_queue;
1031 unsigned long flags;
1033 spin_lock_irqsave(q->queue_lock, flags);
1034 blk_abort_request(sc->request);
1035 spin_unlock_irqrestore(q->queue_lock, flags);
1036 scsi_schedule_eh(sc->device->host);
1040 int sas_slave_alloc(struct scsi_device *scsi_dev)
1042 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1044 if (dev_is_sata(dev))
1045 return ata_sas_port_init(dev->sata_dev.ap);
1047 return 0;
1050 void sas_target_destroy(struct scsi_target *starget)
1052 struct domain_device *found_dev = sas_find_target(starget);
1054 if (!found_dev)
1055 return;
1057 if (dev_is_sata(found_dev))
1058 ata_sas_port_destroy(found_dev->sata_dev.ap);
1060 return;
1063 static void sas_parse_addr(u8 *sas_addr, const char *p)
1065 int i;
1066 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1067 u8 h, l;
1068 if (!*p)
1069 break;
1070 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1071 p++;
1072 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1073 p++;
1074 sas_addr[i] = (h<<4) | l;
1078 #define SAS_STRING_ADDR_SIZE 16
1080 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1082 int res;
1083 const struct firmware *fw;
1085 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1086 if (res)
1087 return res;
1089 if (fw->size < SAS_STRING_ADDR_SIZE) {
1090 res = -ENODEV;
1091 goto out;
1094 sas_parse_addr(addr, fw->data);
1096 out:
1097 release_firmware(fw);
1098 return res;
1100 EXPORT_SYMBOL_GPL(sas_request_addr);
1102 EXPORT_SYMBOL_GPL(sas_queuecommand);
1103 EXPORT_SYMBOL_GPL(sas_target_alloc);
1104 EXPORT_SYMBOL_GPL(sas_slave_configure);
1105 EXPORT_SYMBOL_GPL(sas_slave_destroy);
1106 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1107 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1108 EXPORT_SYMBOL_GPL(sas_bios_param);
1109 EXPORT_SYMBOL_GPL(__sas_task_abort);
1110 EXPORT_SYMBOL_GPL(sas_task_abort);
1111 EXPORT_SYMBOL_GPL(sas_phy_reset);
1112 EXPORT_SYMBOL_GPL(sas_phy_enable);
1113 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1114 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1115 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1116 EXPORT_SYMBOL_GPL(sas_target_destroy);
1117 EXPORT_SYMBOL_GPL(sas_ioctl);