include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / scsi / libsas / sas_scsi_host.c
blobc0155e9740aeb7cb79469870a15f5bcf26cc5b28
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;
187 * sas_queuecommand -- Enqueue a command for processing
188 * @parameters: See SCSI Core documentation
190 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
191 * call us without holding an IRQ spinlock...
193 static int sas_queuecommand_lck(struct scsi_cmnd *cmd,
194 void (*scsi_done)(struct scsi_cmnd *))
195 __releases(host->host_lock)
196 __acquires(dev->sata_dev.ap->lock)
197 __releases(dev->sata_dev.ap->lock)
198 __acquires(host->host_lock)
200 int res = 0;
201 struct domain_device *dev = cmd_to_domain_dev(cmd);
202 struct Scsi_Host *host = cmd->device->host;
203 struct sas_internal *i = to_sas_internal(host->transportt);
205 spin_unlock_irq(host->host_lock);
208 struct sas_ha_struct *sas_ha = dev->port->ha;
209 struct sas_task *task;
211 /* If the device fell off, no sense in issuing commands */
212 if (dev->gone) {
213 cmd->result = DID_BAD_TARGET << 16;
214 scsi_done(cmd);
215 goto out;
218 if (dev_is_sata(dev)) {
219 unsigned long flags;
221 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
222 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
223 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
224 goto out;
227 res = -ENOMEM;
228 task = sas_create_task(cmd, dev, GFP_ATOMIC);
229 if (!task)
230 goto out;
232 cmd->scsi_done = scsi_done;
233 /* Queue up, Direct Mode or Task Collector Mode. */
234 if (sas_ha->lldd_max_execute_num < 2)
235 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
236 else
237 res = sas_queue_up(task);
239 /* Examine */
240 if (res) {
241 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
242 ASSIGN_SAS_TASK(cmd, NULL);
243 sas_free_task(task);
244 if (res == -SAS_QUEUE_FULL) {
245 cmd->result = DID_SOFT_ERROR << 16; /* retry */
246 res = 0;
247 scsi_done(cmd);
249 goto out;
252 out:
253 spin_lock_irq(host->host_lock);
254 return res;
257 DEF_SCSI_QCMD(sas_queuecommand)
259 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
261 struct sas_task *task = TO_SAS_TASK(cmd);
262 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
264 /* remove the aborted task flag to allow the task to be
265 * completed now. At this point, we only get called following
266 * an actual abort of the task, so we should be guaranteed not
267 * to be racing with any completions from the LLD (hence we
268 * don't need the task state lock to clear the flag) */
269 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
270 /* Now call task_done. However, task will be free'd after
271 * this */
272 task->task_done(task);
273 /* now finish the command and move it on to the error
274 * handler done list, this also takes it off the
275 * error handler pending list */
276 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
279 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
281 struct scsi_cmnd *cmd, *n;
283 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
284 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
285 cmd->device->lun == my_cmd->device->lun)
286 sas_eh_finish_cmd(cmd);
290 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
291 struct domain_device *dev)
293 struct scsi_cmnd *cmd, *n;
295 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
296 struct domain_device *x = cmd_to_domain_dev(cmd);
298 if (x == dev)
299 sas_eh_finish_cmd(cmd);
303 static void sas_scsi_clear_queue_port(struct list_head *error_q,
304 struct asd_sas_port *port)
306 struct scsi_cmnd *cmd, *n;
308 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
309 struct domain_device *dev = cmd_to_domain_dev(cmd);
310 struct asd_sas_port *x = dev->port;
312 if (x == port)
313 sas_eh_finish_cmd(cmd);
317 enum task_disposition {
318 TASK_IS_DONE,
319 TASK_IS_ABORTED,
320 TASK_IS_AT_LU,
321 TASK_IS_NOT_AT_LU,
322 TASK_ABORT_FAILED,
325 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
327 struct sas_ha_struct *ha = task->dev->port->ha;
328 unsigned long flags;
329 int i, res;
330 struct sas_internal *si =
331 to_sas_internal(task->dev->port->ha->core.shost->transportt);
333 if (ha->lldd_max_execute_num > 1) {
334 struct scsi_core *core = &ha->core;
335 struct sas_task *t, *n;
337 spin_lock_irqsave(&core->task_queue_lock, flags);
338 list_for_each_entry_safe(t, n, &core->task_queue, list) {
339 if (task == t) {
340 list_del_init(&t->list);
341 spin_unlock_irqrestore(&core->task_queue_lock,
342 flags);
343 SAS_DPRINTK("%s: task 0x%p aborted from "
344 "task_queue\n",
345 __func__, task);
346 return TASK_IS_ABORTED;
349 spin_unlock_irqrestore(&core->task_queue_lock, flags);
352 for (i = 0; i < 5; i++) {
353 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
354 res = si->dft->lldd_abort_task(task);
356 spin_lock_irqsave(&task->task_state_lock, flags);
357 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
358 spin_unlock_irqrestore(&task->task_state_lock, flags);
359 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
360 task);
361 return TASK_IS_DONE;
363 spin_unlock_irqrestore(&task->task_state_lock, flags);
365 if (res == TMF_RESP_FUNC_COMPLETE) {
366 SAS_DPRINTK("%s: task 0x%p is aborted\n",
367 __func__, task);
368 return TASK_IS_ABORTED;
369 } else if (si->dft->lldd_query_task) {
370 SAS_DPRINTK("%s: querying task 0x%p\n",
371 __func__, task);
372 res = si->dft->lldd_query_task(task);
373 switch (res) {
374 case TMF_RESP_FUNC_SUCC:
375 SAS_DPRINTK("%s: task 0x%p at LU\n",
376 __func__, task);
377 return TASK_IS_AT_LU;
378 case TMF_RESP_FUNC_COMPLETE:
379 SAS_DPRINTK("%s: task 0x%p not at LU\n",
380 __func__, task);
381 return TASK_IS_NOT_AT_LU;
382 case TMF_RESP_FUNC_FAILED:
383 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
384 __func__, task);
385 return TASK_ABORT_FAILED;
390 return res;
393 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
395 int res = TMF_RESP_FUNC_FAILED;
396 struct scsi_lun lun;
397 struct sas_internal *i =
398 to_sas_internal(dev->port->ha->core.shost->transportt);
400 int_to_scsilun(cmd->device->lun, &lun);
402 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
403 SAS_ADDR(dev->sas_addr),
404 cmd->device->lun);
406 if (i->dft->lldd_abort_task_set)
407 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
409 if (res == TMF_RESP_FUNC_FAILED) {
410 if (i->dft->lldd_clear_task_set)
411 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
414 if (res == TMF_RESP_FUNC_FAILED) {
415 if (i->dft->lldd_lu_reset)
416 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
419 return res;
422 static int sas_recover_I_T(struct domain_device *dev)
424 int res = TMF_RESP_FUNC_FAILED;
425 struct sas_internal *i =
426 to_sas_internal(dev->port->ha->core.shost->transportt);
428 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
429 SAS_ADDR(dev->sas_addr));
431 if (i->dft->lldd_I_T_nexus_reset)
432 res = i->dft->lldd_I_T_nexus_reset(dev);
434 return res;
437 /* Find the sas_phy that's attached to this device */
438 struct sas_phy *sas_find_local_phy(struct domain_device *dev)
440 struct domain_device *pdev = dev->parent;
441 struct ex_phy *exphy = NULL;
442 int i;
444 /* Directly attached device */
445 if (!pdev)
446 return dev->port->phy;
448 /* Otherwise look in the expander */
449 for (i = 0; i < pdev->ex_dev.num_phys; i++)
450 if (!memcmp(dev->sas_addr,
451 pdev->ex_dev.ex_phy[i].attached_sas_addr,
452 SAS_ADDR_SIZE)) {
453 exphy = &pdev->ex_dev.ex_phy[i];
454 break;
457 BUG_ON(!exphy);
458 return exphy->phy;
460 EXPORT_SYMBOL_GPL(sas_find_local_phy);
462 /* Attempt to send a LUN reset message to a device */
463 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
465 struct domain_device *dev = cmd_to_domain_dev(cmd);
466 struct sas_internal *i =
467 to_sas_internal(dev->port->ha->core.shost->transportt);
468 struct scsi_lun lun;
469 int res;
471 int_to_scsilun(cmd->device->lun, &lun);
473 if (!i->dft->lldd_lu_reset)
474 return FAILED;
476 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
477 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
478 return SUCCESS;
480 return FAILED;
483 /* Attempt to send a phy (bus) reset */
484 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
486 struct domain_device *dev = cmd_to_domain_dev(cmd);
487 struct sas_phy *phy = sas_find_local_phy(dev);
488 int res;
490 res = sas_phy_reset(phy, 1);
491 if (res)
492 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
493 kobject_name(&phy->dev.kobj),
494 res);
495 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
496 return SUCCESS;
498 return FAILED;
501 /* Try to reset a device */
502 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
504 int res;
505 struct Scsi_Host *shost = cmd->device->host;
507 if (!shost->hostt->eh_device_reset_handler)
508 goto try_bus_reset;
510 res = shost->hostt->eh_device_reset_handler(cmd);
511 if (res == SUCCESS)
512 return res;
514 try_bus_reset:
515 if (shost->hostt->eh_bus_reset_handler)
516 return shost->hostt->eh_bus_reset_handler(cmd);
518 return FAILED;
521 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
522 struct list_head *work_q,
523 struct list_head *done_q)
525 struct scsi_cmnd *cmd, *n;
526 enum task_disposition res = TASK_IS_DONE;
527 int tmf_resp, need_reset;
528 struct sas_internal *i = to_sas_internal(shost->transportt);
529 unsigned long flags;
530 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
532 Again:
533 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
534 struct sas_task *task = TO_SAS_TASK(cmd);
536 if (!task)
537 continue;
539 list_del_init(&cmd->eh_entry);
541 spin_lock_irqsave(&task->task_state_lock, flags);
542 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
543 spin_unlock_irqrestore(&task->task_state_lock, flags);
545 if (need_reset) {
546 SAS_DPRINTK("%s: task 0x%p requests reset\n",
547 __func__, task);
548 goto reset;
551 SAS_DPRINTK("trying to find task 0x%p\n", task);
552 res = sas_scsi_find_task(task);
554 cmd->eh_eflags = 0;
556 switch (res) {
557 case TASK_IS_DONE:
558 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
559 task);
560 sas_eh_finish_cmd(cmd);
561 continue;
562 case TASK_IS_ABORTED:
563 SAS_DPRINTK("%s: task 0x%p is aborted\n",
564 __func__, task);
565 sas_eh_finish_cmd(cmd);
566 continue;
567 case TASK_IS_AT_LU:
568 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
569 reset:
570 tmf_resp = sas_recover_lu(task->dev, cmd);
571 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
572 SAS_DPRINTK("dev %016llx LU %x is "
573 "recovered\n",
574 SAS_ADDR(task->dev),
575 cmd->device->lun);
576 sas_eh_finish_cmd(cmd);
577 sas_scsi_clear_queue_lu(work_q, cmd);
578 goto Again;
580 /* fallthrough */
581 case TASK_IS_NOT_AT_LU:
582 case TASK_ABORT_FAILED:
583 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
584 task);
585 tmf_resp = sas_recover_I_T(task->dev);
586 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
587 struct domain_device *dev = task->dev;
588 SAS_DPRINTK("I_T %016llx recovered\n",
589 SAS_ADDR(task->dev->sas_addr));
590 sas_eh_finish_cmd(cmd);
591 sas_scsi_clear_queue_I_T(work_q, dev);
592 goto Again;
594 /* Hammer time :-) */
595 try_to_reset_cmd_device(cmd);
596 if (i->dft->lldd_clear_nexus_port) {
597 struct asd_sas_port *port = task->dev->port;
598 SAS_DPRINTK("clearing nexus for port:%d\n",
599 port->id);
600 res = i->dft->lldd_clear_nexus_port(port);
601 if (res == TMF_RESP_FUNC_COMPLETE) {
602 SAS_DPRINTK("clear nexus port:%d "
603 "succeeded\n", port->id);
604 sas_eh_finish_cmd(cmd);
605 sas_scsi_clear_queue_port(work_q,
606 port);
607 goto Again;
610 if (i->dft->lldd_clear_nexus_ha) {
611 SAS_DPRINTK("clear nexus ha\n");
612 res = i->dft->lldd_clear_nexus_ha(ha);
613 if (res == TMF_RESP_FUNC_COMPLETE) {
614 SAS_DPRINTK("clear nexus ha "
615 "succeeded\n");
616 sas_eh_finish_cmd(cmd);
617 goto clear_q;
620 /* If we are here -- this means that no amount
621 * of effort could recover from errors. Quite
622 * possibly the HA just disappeared.
624 SAS_DPRINTK("error from device %llx, LUN %x "
625 "couldn't be recovered in any way\n",
626 SAS_ADDR(task->dev->sas_addr),
627 cmd->device->lun);
629 sas_eh_finish_cmd(cmd);
630 goto clear_q;
633 return list_empty(work_q);
634 clear_q:
635 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
636 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
637 sas_eh_finish_cmd(cmd);
639 return list_empty(work_q);
642 void sas_scsi_recover_host(struct Scsi_Host *shost)
644 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
645 unsigned long flags;
646 LIST_HEAD(eh_work_q);
648 spin_lock_irqsave(shost->host_lock, flags);
649 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
650 shost->host_eh_scheduled = 0;
651 spin_unlock_irqrestore(shost->host_lock, flags);
653 SAS_DPRINTK("Enter %s\n", __func__);
655 * Deal with commands that still have SAS tasks (i.e. they didn't
656 * complete via the normal sas_task completion mechanism)
658 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
659 goto out;
662 * Now deal with SCSI commands that completed ok but have a an error
663 * code (and hopefully sense data) attached. This is roughly what
664 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
665 * command we see here has no sas_task and is thus unknown to the HA.
667 if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
668 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
669 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
671 out:
672 /* now link into libata eh --- if we have any ata devices */
673 sas_ata_strategy_handler(shost);
675 scsi_eh_flush_done_q(&ha->eh_done_q);
677 SAS_DPRINTK("--- Exit %s\n", __func__);
678 return;
681 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
683 struct sas_task *task = TO_SAS_TASK(cmd);
684 unsigned long flags;
685 enum blk_eh_timer_return rtn;
687 if (sas_ata_timed_out(cmd, task, &rtn))
688 return rtn;
690 if (!task) {
691 cmd->request->timeout /= 2;
692 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
693 cmd, task, (cmd->request->timeout ?
694 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
695 if (!cmd->request->timeout)
696 return BLK_EH_NOT_HANDLED;
697 return BLK_EH_RESET_TIMER;
700 spin_lock_irqsave(&task->task_state_lock, flags);
701 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
702 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
703 spin_unlock_irqrestore(&task->task_state_lock, flags);
704 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
705 "BLK_EH_HANDLED\n", cmd, task);
706 return BLK_EH_HANDLED;
708 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
709 spin_unlock_irqrestore(&task->task_state_lock, flags);
710 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
711 "BLK_EH_RESET_TIMER\n",
712 cmd, task);
713 return BLK_EH_RESET_TIMER;
715 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
716 spin_unlock_irqrestore(&task->task_state_lock, flags);
718 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
719 cmd, task);
721 return BLK_EH_NOT_HANDLED;
724 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
726 struct domain_device *dev = sdev_to_domain_dev(sdev);
728 if (dev_is_sata(dev))
729 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
731 return -EINVAL;
734 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
736 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
737 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
738 struct domain_device *found_dev = NULL;
739 int i;
740 unsigned long flags;
742 spin_lock_irqsave(&ha->phy_port_lock, flags);
743 for (i = 0; i < ha->num_phys; i++) {
744 struct asd_sas_port *port = ha->sas_port[i];
745 struct domain_device *dev;
747 spin_lock(&port->dev_list_lock);
748 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
749 if (rphy == dev->rphy) {
750 found_dev = dev;
751 spin_unlock(&port->dev_list_lock);
752 goto found;
755 spin_unlock(&port->dev_list_lock);
757 found:
758 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
760 return found_dev;
763 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
765 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
767 return sas_find_dev_by_rphy(rphy);
770 int sas_target_alloc(struct scsi_target *starget)
772 struct domain_device *found_dev = sas_find_target(starget);
773 int res;
775 if (!found_dev)
776 return -ENODEV;
778 if (dev_is_sata(found_dev)) {
779 res = sas_ata_init_host_and_port(found_dev, starget);
780 if (res)
781 return res;
784 starget->hostdata = found_dev;
785 return 0;
788 #define SAS_DEF_QD 32
789 #define SAS_MAX_QD 64
791 int sas_slave_configure(struct scsi_device *scsi_dev)
793 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
794 struct sas_ha_struct *sas_ha;
796 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
798 if (dev_is_sata(dev)) {
799 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
800 return 0;
803 sas_ha = dev->port->ha;
805 sas_read_port_mode_page(scsi_dev);
807 if (scsi_dev->tagged_supported) {
808 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
809 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
810 } else {
811 SAS_DPRINTK("device %llx, LUN %x doesn't support "
812 "TCQ\n", SAS_ADDR(dev->sas_addr),
813 scsi_dev->lun);
814 scsi_dev->tagged_supported = 0;
815 scsi_set_tag_type(scsi_dev, 0);
816 scsi_deactivate_tcq(scsi_dev, 1);
819 scsi_dev->allow_restart = 1;
821 return 0;
824 void sas_slave_destroy(struct scsi_device *scsi_dev)
826 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
828 if (dev_is_sata(dev))
829 dev->sata_dev.ap->link.device[0].class = ATA_DEV_NONE;
832 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth,
833 int reason)
835 int res = min(new_depth, SAS_MAX_QD);
837 if (reason != SCSI_QDEPTH_DEFAULT)
838 return -EOPNOTSUPP;
840 if (scsi_dev->tagged_supported)
841 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
842 res);
843 else {
844 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
845 sas_printk("device %llx LUN %x queue depth changed to 1\n",
846 SAS_ADDR(dev->sas_addr),
847 scsi_dev->lun);
848 scsi_adjust_queue_depth(scsi_dev, 0, 1);
849 res = 1;
852 return res;
855 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
857 if (!scsi_dev->tagged_supported)
858 return 0;
860 scsi_deactivate_tcq(scsi_dev, 1);
862 scsi_set_tag_type(scsi_dev, qt);
863 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
865 return qt;
868 int sas_bios_param(struct scsi_device *scsi_dev,
869 struct block_device *bdev,
870 sector_t capacity, int *hsc)
872 hsc[0] = 255;
873 hsc[1] = 63;
874 sector_div(capacity, 255*63);
875 hsc[2] = capacity;
877 return 0;
880 /* ---------- Task Collector Thread implementation ---------- */
882 static void sas_queue(struct sas_ha_struct *sas_ha)
884 struct scsi_core *core = &sas_ha->core;
885 unsigned long flags;
886 LIST_HEAD(q);
887 int can_queue;
888 int res;
889 struct sas_internal *i = to_sas_internal(core->shost->transportt);
891 spin_lock_irqsave(&core->task_queue_lock, flags);
892 while (!kthread_should_stop() &&
893 !list_empty(&core->task_queue)) {
895 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
896 if (can_queue >= 0) {
897 can_queue = core->task_queue_size;
898 list_splice_init(&core->task_queue, &q);
899 } else {
900 struct list_head *a, *n;
902 can_queue = sas_ha->lldd_queue_size;
903 list_for_each_safe(a, n, &core->task_queue) {
904 list_move_tail(a, &q);
905 if (--can_queue == 0)
906 break;
908 can_queue = sas_ha->lldd_queue_size;
910 core->task_queue_size -= can_queue;
911 spin_unlock_irqrestore(&core->task_queue_lock, flags);
913 struct sas_task *task = list_entry(q.next,
914 struct sas_task,
915 list);
916 list_del_init(&q);
917 res = i->dft->lldd_execute_task(task, can_queue,
918 GFP_KERNEL);
919 if (unlikely(res))
920 __list_add(&q, task->list.prev, &task->list);
922 spin_lock_irqsave(&core->task_queue_lock, flags);
923 if (res) {
924 list_splice_init(&q, &core->task_queue); /*at head*/
925 core->task_queue_size += can_queue;
928 spin_unlock_irqrestore(&core->task_queue_lock, flags);
932 * sas_queue_thread -- The Task Collector thread
933 * @_sas_ha: pointer to struct sas_ha
935 static int sas_queue_thread(void *_sas_ha)
937 struct sas_ha_struct *sas_ha = _sas_ha;
939 while (1) {
940 set_current_state(TASK_INTERRUPTIBLE);
941 schedule();
942 sas_queue(sas_ha);
943 if (kthread_should_stop())
944 break;
947 return 0;
950 int sas_init_queue(struct sas_ha_struct *sas_ha)
952 struct scsi_core *core = &sas_ha->core;
954 spin_lock_init(&core->task_queue_lock);
955 core->task_queue_size = 0;
956 INIT_LIST_HEAD(&core->task_queue);
958 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
959 "sas_queue_%d", core->shost->host_no);
960 if (IS_ERR(core->queue_thread))
961 return PTR_ERR(core->queue_thread);
962 return 0;
965 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
967 unsigned long flags;
968 struct scsi_core *core = &sas_ha->core;
969 struct sas_task *task, *n;
971 kthread_stop(core->queue_thread);
973 if (!list_empty(&core->task_queue))
974 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
975 SAS_ADDR(sas_ha->sas_addr));
977 spin_lock_irqsave(&core->task_queue_lock, flags);
978 list_for_each_entry_safe(task, n, &core->task_queue, list) {
979 struct scsi_cmnd *cmd = task->uldd_task;
981 list_del_init(&task->list);
983 ASSIGN_SAS_TASK(cmd, NULL);
984 sas_free_task(task);
985 cmd->result = DID_ABORT << 16;
986 cmd->scsi_done(cmd);
988 spin_unlock_irqrestore(&core->task_queue_lock, flags);
992 * Call the LLDD task abort routine directly. This function is intended for
993 * use by upper layers that need to tell the LLDD to abort a task.
995 int __sas_task_abort(struct sas_task *task)
997 struct sas_internal *si =
998 to_sas_internal(task->dev->port->ha->core.shost->transportt);
999 unsigned long flags;
1000 int res;
1002 spin_lock_irqsave(&task->task_state_lock, flags);
1003 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
1004 task->task_state_flags & SAS_TASK_STATE_DONE) {
1005 spin_unlock_irqrestore(&task->task_state_lock, flags);
1006 SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
1007 task);
1008 return 0;
1010 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1011 spin_unlock_irqrestore(&task->task_state_lock, flags);
1013 if (!si->dft->lldd_abort_task)
1014 return -ENODEV;
1016 res = si->dft->lldd_abort_task(task);
1018 spin_lock_irqsave(&task->task_state_lock, flags);
1019 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
1020 (res == TMF_RESP_FUNC_COMPLETE))
1022 spin_unlock_irqrestore(&task->task_state_lock, flags);
1023 task->task_done(task);
1024 return 0;
1027 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1028 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1029 spin_unlock_irqrestore(&task->task_state_lock, flags);
1031 return -EAGAIN;
1035 * Tell an upper layer that it needs to initiate an abort for a given task.
1036 * This should only ever be called by an LLDD.
1038 void sas_task_abort(struct sas_task *task)
1040 struct scsi_cmnd *sc = task->uldd_task;
1042 /* Escape for libsas internal commands */
1043 if (!sc) {
1044 if (!del_timer(&task->timer))
1045 return;
1046 task->timer.function(task->timer.data);
1047 return;
1050 if (dev_is_sata(task->dev)) {
1051 sas_ata_task_abort(task);
1052 } else {
1053 struct request_queue *q = sc->device->request_queue;
1054 unsigned long flags;
1056 spin_lock_irqsave(q->queue_lock, flags);
1057 blk_abort_request(sc->request);
1058 spin_unlock_irqrestore(q->queue_lock, flags);
1059 scsi_schedule_eh(sc->device->host);
1063 int sas_slave_alloc(struct scsi_device *scsi_dev)
1065 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1067 if (dev_is_sata(dev))
1068 return ata_sas_port_init(dev->sata_dev.ap);
1070 return 0;
1073 void sas_target_destroy(struct scsi_target *starget)
1075 struct domain_device *found_dev = sas_find_target(starget);
1077 if (!found_dev)
1078 return;
1080 if (dev_is_sata(found_dev))
1081 ata_sas_port_destroy(found_dev->sata_dev.ap);
1083 return;
1086 static void sas_parse_addr(u8 *sas_addr, const char *p)
1088 int i;
1089 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1090 u8 h, l;
1091 if (!*p)
1092 break;
1093 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1094 p++;
1095 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1096 p++;
1097 sas_addr[i] = (h<<4) | l;
1101 #define SAS_STRING_ADDR_SIZE 16
1103 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1105 int res;
1106 const struct firmware *fw;
1108 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1109 if (res)
1110 return res;
1112 if (fw->size < SAS_STRING_ADDR_SIZE) {
1113 res = -ENODEV;
1114 goto out;
1117 sas_parse_addr(addr, fw->data);
1119 out:
1120 release_firmware(fw);
1121 return res;
1123 EXPORT_SYMBOL_GPL(sas_request_addr);
1125 EXPORT_SYMBOL_GPL(sas_queuecommand);
1126 EXPORT_SYMBOL_GPL(sas_target_alloc);
1127 EXPORT_SYMBOL_GPL(sas_slave_configure);
1128 EXPORT_SYMBOL_GPL(sas_slave_destroy);
1129 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1130 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1131 EXPORT_SYMBOL_GPL(sas_bios_param);
1132 EXPORT_SYMBOL_GPL(__sas_task_abort);
1133 EXPORT_SYMBOL_GPL(sas_task_abort);
1134 EXPORT_SYMBOL_GPL(sas_phy_reset);
1135 EXPORT_SYMBOL_GPL(sas_phy_enable);
1136 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1137 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1138 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1139 EXPORT_SYMBOL_GPL(sas_target_destroy);
1140 EXPORT_SYMBOL_GPL(sas_ioctl);