Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / scsi / libsas / sas_scsi_host.c
blob97616e0fd9284ace0bd35085796ef8967dd3f738
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 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
55 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
56 unsigned ts_flags = task->task_state_flags;
57 =======
58 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
59 int hs = 0, stat = 0;
61 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
62 =======
63 if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
64 /* Aborted tasks will be completed by the error handler */
65 SAS_DPRINTK("task done but aborted\n");
66 return;
69 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
70 if (unlikely(!sc)) {
71 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
72 list_del_init(&task->list);
73 sas_free_task(task);
74 return;
77 if (ts->resp == SAS_TASK_UNDELIVERED) {
78 /* transport error */
79 hs = DID_NO_CONNECT;
80 } else { /* ts->resp == SAS_TASK_COMPLETE */
81 /* task delivered, what happened afterwards? */
82 switch (ts->stat) {
83 case SAS_DEV_NO_RESPONSE:
84 case SAS_INTERRUPTED:
85 case SAS_PHY_DOWN:
86 case SAS_NAK_R_ERR:
87 case SAS_OPEN_TO:
88 hs = DID_NO_CONNECT;
89 break;
90 case SAS_DATA_UNDERRUN:
91 scsi_set_resid(sc, ts->residual);
92 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
93 hs = DID_ERROR;
94 break;
95 case SAS_DATA_OVERRUN:
96 hs = DID_ERROR;
97 break;
98 case SAS_QUEUE_FULL:
99 hs = DID_SOFT_ERROR; /* retry */
100 break;
101 case SAS_DEVICE_UNKNOWN:
102 hs = DID_BAD_TARGET;
103 break;
104 case SAS_SG_ERR:
105 hs = DID_PARITY;
106 break;
107 case SAS_OPEN_REJECT:
108 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
109 hs = DID_SOFT_ERROR; /* retry */
110 else
111 hs = DID_ERROR;
112 break;
113 case SAS_PROTO_RESPONSE:
114 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
115 "task; please report this\n",
116 task->dev->port->ha->sas_ha_name);
117 break;
118 case SAS_ABORTED_TASK:
119 hs = DID_ABORT;
120 break;
121 case SAM_CHECK_COND:
122 memcpy(sc->sense_buffer, ts->buf,
123 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
124 stat = SAM_CHECK_COND;
125 break;
126 default:
127 stat = ts->stat;
128 break;
131 ASSIGN_SAS_TASK(sc, NULL);
132 sc->result = (hs << 16) | stat;
133 list_del_init(&task->list);
134 sas_free_task(task);
135 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
136 /* This is very ugly but this is how SCSI Core works. */
137 if (ts_flags & SAS_TASK_STATE_ABORTED)
138 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
139 else
140 sc->scsi_done(sc);
141 =======
142 sc->scsi_done(sc);
143 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
146 static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
148 enum task_attribute ta = TASK_ATTR_SIMPLE;
149 if (cmd->request && blk_rq_tagged(cmd->request)) {
150 if (cmd->device->ordered_tags &&
151 (cmd->request->cmd_flags & REQ_HARDBARRIER))
152 ta = TASK_ATTR_ORDERED;
154 return ta;
157 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
158 struct domain_device *dev,
159 gfp_t gfp_flags)
161 struct sas_task *task = sas_alloc_task(gfp_flags);
162 struct scsi_lun lun;
164 if (!task)
165 return NULL;
167 task->uldd_task = cmd;
168 ASSIGN_SAS_TASK(cmd, task);
170 task->dev = dev;
171 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
173 task->ssp_task.retry_count = 1;
174 int_to_scsilun(cmd->device->lun, &lun);
175 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
176 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
177 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
179 task->scatter = scsi_sglist(cmd);
180 task->num_scatter = scsi_sg_count(cmd);
181 task->total_xfer_len = scsi_bufflen(cmd);
182 task->data_dir = cmd->sc_data_direction;
184 task->task_done = sas_scsi_task_done;
186 return task;
189 int sas_queue_up(struct sas_task *task)
191 struct sas_ha_struct *sas_ha = task->dev->port->ha;
192 struct scsi_core *core = &sas_ha->core;
193 unsigned long flags;
194 LIST_HEAD(list);
196 spin_lock_irqsave(&core->task_queue_lock, flags);
197 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
198 spin_unlock_irqrestore(&core->task_queue_lock, flags);
199 return -SAS_QUEUE_FULL;
201 list_add_tail(&task->list, &core->task_queue);
202 core->task_queue_size += 1;
203 spin_unlock_irqrestore(&core->task_queue_lock, flags);
204 wake_up_process(core->queue_thread);
206 return 0;
210 * sas_queuecommand -- Enqueue a command for processing
211 * @parameters: See SCSI Core documentation
213 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
214 * call us without holding an IRQ spinlock...
216 int sas_queuecommand(struct scsi_cmnd *cmd,
217 void (*scsi_done)(struct scsi_cmnd *))
218 __releases(host->host_lock)
219 __acquires(dev->sata_dev.ap->lock)
220 __releases(dev->sata_dev.ap->lock)
221 __acquires(host->host_lock)
223 int res = 0;
224 struct domain_device *dev = cmd_to_domain_dev(cmd);
225 struct Scsi_Host *host = cmd->device->host;
226 struct sas_internal *i = to_sas_internal(host->transportt);
228 spin_unlock_irq(host->host_lock);
231 struct sas_ha_struct *sas_ha = dev->port->ha;
232 struct sas_task *task;
234 if (dev_is_sata(dev)) {
235 unsigned long flags;
237 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
238 res = ata_sas_queuecmd(cmd, scsi_done,
239 dev->sata_dev.ap);
240 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
241 goto out;
244 res = -ENOMEM;
245 task = sas_create_task(cmd, dev, GFP_ATOMIC);
246 if (!task)
247 goto out;
249 cmd->scsi_done = scsi_done;
250 /* Queue up, Direct Mode or Task Collector Mode. */
251 if (sas_ha->lldd_max_execute_num < 2)
252 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
253 else
254 res = sas_queue_up(task);
256 /* Examine */
257 if (res) {
258 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
259 ASSIGN_SAS_TASK(cmd, NULL);
260 sas_free_task(task);
261 if (res == -SAS_QUEUE_FULL) {
262 cmd->result = DID_SOFT_ERROR << 16; /* retry */
263 res = 0;
264 scsi_done(cmd);
266 goto out;
269 out:
270 spin_lock_irq(host->host_lock);
271 return res;
274 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
275 =======
276 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
278 struct sas_task *task = TO_SAS_TASK(cmd);
279 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
281 /* remove the aborted task flag to allow the task to be
282 * completed now. At this point, we only get called following
283 * an actual abort of the task, so we should be guaranteed not
284 * to be racing with any completions from the LLD (hence we
285 * don't need the task state lock to clear the flag) */
286 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
287 /* Now call task_done. However, task will be free'd after
288 * this */
289 task->task_done(task);
290 /* now finish the command and move it on to the error
291 * handler done list, this also takes it off the
292 * error handler pending list */
293 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
296 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
297 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
299 struct scsi_cmnd *cmd, *n;
301 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
302 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
303 if (cmd == my_cmd)
304 list_del_init(&cmd->eh_entry);
305 =======
306 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
307 cmd->device->lun == my_cmd->device->lun)
308 sas_eh_finish_cmd(cmd);
309 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
313 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
314 struct domain_device *dev)
316 struct scsi_cmnd *cmd, *n;
318 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
319 struct domain_device *x = cmd_to_domain_dev(cmd);
321 if (x == dev)
322 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
323 list_del_init(&cmd->eh_entry);
324 =======
325 sas_eh_finish_cmd(cmd);
326 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
330 static void sas_scsi_clear_queue_port(struct list_head *error_q,
331 struct asd_sas_port *port)
333 struct scsi_cmnd *cmd, *n;
335 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
336 struct domain_device *dev = cmd_to_domain_dev(cmd);
337 struct asd_sas_port *x = dev->port;
339 if (x == port)
340 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
341 list_del_init(&cmd->eh_entry);
342 =======
343 sas_eh_finish_cmd(cmd);
344 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
348 enum task_disposition {
349 TASK_IS_DONE,
350 TASK_IS_ABORTED,
351 TASK_IS_AT_LU,
352 TASK_IS_NOT_AT_LU,
353 TASK_ABORT_FAILED,
356 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
358 struct sas_ha_struct *ha = task->dev->port->ha;
359 unsigned long flags;
360 int i, res;
361 struct sas_internal *si =
362 to_sas_internal(task->dev->port->ha->core.shost->transportt);
364 if (ha->lldd_max_execute_num > 1) {
365 struct scsi_core *core = &ha->core;
366 struct sas_task *t, *n;
368 spin_lock_irqsave(&core->task_queue_lock, flags);
369 list_for_each_entry_safe(t, n, &core->task_queue, list) {
370 if (task == t) {
371 list_del_init(&t->list);
372 spin_unlock_irqrestore(&core->task_queue_lock,
373 flags);
374 SAS_DPRINTK("%s: task 0x%p aborted from "
375 "task_queue\n",
376 __FUNCTION__, task);
377 return TASK_IS_ABORTED;
380 spin_unlock_irqrestore(&core->task_queue_lock, flags);
383 for (i = 0; i < 5; i++) {
384 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
385 res = si->dft->lldd_abort_task(task);
387 spin_lock_irqsave(&task->task_state_lock, flags);
388 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
389 spin_unlock_irqrestore(&task->task_state_lock, flags);
390 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
391 task);
392 return TASK_IS_DONE;
394 spin_unlock_irqrestore(&task->task_state_lock, flags);
396 if (res == TMF_RESP_FUNC_COMPLETE) {
397 SAS_DPRINTK("%s: task 0x%p is aborted\n",
398 __FUNCTION__, task);
399 return TASK_IS_ABORTED;
400 } else if (si->dft->lldd_query_task) {
401 SAS_DPRINTK("%s: querying task 0x%p\n",
402 __FUNCTION__, task);
403 res = si->dft->lldd_query_task(task);
404 switch (res) {
405 case TMF_RESP_FUNC_SUCC:
406 SAS_DPRINTK("%s: task 0x%p at LU\n",
407 __FUNCTION__, task);
408 return TASK_IS_AT_LU;
409 case TMF_RESP_FUNC_COMPLETE:
410 SAS_DPRINTK("%s: task 0x%p not at LU\n",
411 __FUNCTION__, task);
412 return TASK_IS_NOT_AT_LU;
413 case TMF_RESP_FUNC_FAILED:
414 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
415 __FUNCTION__, task);
416 return TASK_ABORT_FAILED;
421 return res;
424 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
426 int res = TMF_RESP_FUNC_FAILED;
427 struct scsi_lun lun;
428 struct sas_internal *i =
429 to_sas_internal(dev->port->ha->core.shost->transportt);
431 int_to_scsilun(cmd->device->lun, &lun);
433 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
434 SAS_ADDR(dev->sas_addr),
435 cmd->device->lun);
437 if (i->dft->lldd_abort_task_set)
438 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
440 if (res == TMF_RESP_FUNC_FAILED) {
441 if (i->dft->lldd_clear_task_set)
442 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
445 if (res == TMF_RESP_FUNC_FAILED) {
446 if (i->dft->lldd_lu_reset)
447 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
450 return res;
453 static int sas_recover_I_T(struct domain_device *dev)
455 int res = TMF_RESP_FUNC_FAILED;
456 struct sas_internal *i =
457 to_sas_internal(dev->port->ha->core.shost->transportt);
459 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
460 SAS_ADDR(dev->sas_addr));
462 if (i->dft->lldd_I_T_nexus_reset)
463 res = i->dft->lldd_I_T_nexus_reset(dev);
465 return res;
468 /* Find the sas_phy that's attached to this device */
469 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
470 static struct sas_phy *find_local_sas_phy(struct domain_device *dev)
471 =======
472 struct sas_phy *sas_find_local_phy(struct domain_device *dev)
473 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
475 struct domain_device *pdev = dev->parent;
476 struct ex_phy *exphy = NULL;
477 int i;
479 /* Directly attached device */
480 if (!pdev)
481 return dev->port->phy;
483 /* Otherwise look in the expander */
484 for (i = 0; i < pdev->ex_dev.num_phys; i++)
485 if (!memcmp(dev->sas_addr,
486 pdev->ex_dev.ex_phy[i].attached_sas_addr,
487 SAS_ADDR_SIZE)) {
488 exphy = &pdev->ex_dev.ex_phy[i];
489 break;
492 BUG_ON(!exphy);
493 return exphy->phy;
495 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
496 =======
497 EXPORT_SYMBOL_GPL(sas_find_local_phy);
498 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
500 /* Attempt to send a LUN reset message to a device */
501 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
503 struct domain_device *dev = cmd_to_domain_dev(cmd);
504 struct sas_internal *i =
505 to_sas_internal(dev->port->ha->core.shost->transportt);
506 struct scsi_lun lun;
507 int res;
509 int_to_scsilun(cmd->device->lun, &lun);
511 if (!i->dft->lldd_lu_reset)
512 return FAILED;
514 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
515 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
516 return SUCCESS;
518 return FAILED;
521 /* Attempt to send a phy (bus) reset */
522 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
524 struct domain_device *dev = cmd_to_domain_dev(cmd);
525 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
526 struct sas_phy *phy = find_local_sas_phy(dev);
527 =======
528 struct sas_phy *phy = sas_find_local_phy(dev);
529 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
530 int res;
532 res = sas_phy_reset(phy, 1);
533 if (res)
534 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
535 kobject_name(&phy->dev.kobj),
536 res);
537 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
538 return SUCCESS;
540 return FAILED;
543 /* Try to reset a device */
544 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
545 static int try_to_reset_cmd_device(struct Scsi_Host *shost,
546 struct scsi_cmnd *cmd)
547 =======
548 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
549 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
551 int res;
552 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
553 =======
554 struct Scsi_Host *shost = cmd->device->host;
555 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
557 if (!shost->hostt->eh_device_reset_handler)
558 goto try_bus_reset;
560 res = shost->hostt->eh_device_reset_handler(cmd);
561 if (res == SUCCESS)
562 return res;
564 try_bus_reset:
565 if (shost->hostt->eh_bus_reset_handler)
566 return shost->hostt->eh_bus_reset_handler(cmd);
568 return FAILED;
571 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
572 struct list_head *work_q,
573 struct list_head *done_q)
575 struct scsi_cmnd *cmd, *n;
576 enum task_disposition res = TASK_IS_DONE;
577 int tmf_resp, need_reset;
578 struct sas_internal *i = to_sas_internal(shost->transportt);
579 unsigned long flags;
580 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
582 Again:
583 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
584 struct sas_task *task = TO_SAS_TASK(cmd);
586 if (!task)
587 continue;
589 list_del_init(&cmd->eh_entry);
591 spin_lock_irqsave(&task->task_state_lock, flags);
592 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
593 spin_unlock_irqrestore(&task->task_state_lock, flags);
595 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
596 =======
597 if (need_reset) {
598 SAS_DPRINTK("%s: task 0x%p requests reset\n",
599 __FUNCTION__, task);
600 goto reset;
603 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
604 SAS_DPRINTK("trying to find task 0x%p\n", task);
605 res = sas_scsi_find_task(task);
607 cmd->eh_eflags = 0;
609 switch (res) {
610 case TASK_IS_DONE:
611 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
612 task);
613 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
614 task->task_done(task);
615 if (need_reset)
616 try_to_reset_cmd_device(shost, cmd);
617 =======
618 sas_eh_finish_cmd(cmd);
619 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
620 continue;
621 case TASK_IS_ABORTED:
622 SAS_DPRINTK("%s: task 0x%p is aborted\n",
623 __FUNCTION__, task);
624 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
625 task->task_done(task);
626 if (need_reset)
627 try_to_reset_cmd_device(shost, cmd);
628 =======
629 sas_eh_finish_cmd(cmd);
630 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
631 continue;
632 case TASK_IS_AT_LU:
633 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
634 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
635 =======
636 reset:
637 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
638 tmf_resp = sas_recover_lu(task->dev, cmd);
639 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
640 SAS_DPRINTK("dev %016llx LU %x is "
641 "recovered\n",
642 SAS_ADDR(task->dev),
643 cmd->device->lun);
644 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
645 task->task_done(task);
646 if (need_reset)
647 try_to_reset_cmd_device(shost, cmd);
648 =======
649 sas_eh_finish_cmd(cmd);
650 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
651 sas_scsi_clear_queue_lu(work_q, cmd);
652 goto Again;
654 /* fallthrough */
655 case TASK_IS_NOT_AT_LU:
656 case TASK_ABORT_FAILED:
657 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
658 task);
659 tmf_resp = sas_recover_I_T(task->dev);
660 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
661 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
662 =======
663 struct domain_device *dev = task->dev;
664 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
665 SAS_DPRINTK("I_T %016llx recovered\n",
666 SAS_ADDR(task->dev->sas_addr));
667 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
668 task->task_done(task);
669 if (need_reset)
670 try_to_reset_cmd_device(shost, cmd);
671 sas_scsi_clear_queue_I_T(work_q, task->dev);
672 =======
673 sas_eh_finish_cmd(cmd);
674 sas_scsi_clear_queue_I_T(work_q, dev);
675 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
676 goto Again;
678 /* Hammer time :-) */
679 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
680 =======
681 try_to_reset_cmd_device(cmd);
682 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
683 if (i->dft->lldd_clear_nexus_port) {
684 struct asd_sas_port *port = task->dev->port;
685 SAS_DPRINTK("clearing nexus for port:%d\n",
686 port->id);
687 res = i->dft->lldd_clear_nexus_port(port);
688 if (res == TMF_RESP_FUNC_COMPLETE) {
689 SAS_DPRINTK("clear nexus port:%d "
690 "succeeded\n", port->id);
691 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
692 task->task_done(task);
693 if (need_reset)
694 try_to_reset_cmd_device(shost, cmd);
695 =======
696 sas_eh_finish_cmd(cmd);
697 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
698 sas_scsi_clear_queue_port(work_q,
699 port);
700 goto Again;
703 if (i->dft->lldd_clear_nexus_ha) {
704 SAS_DPRINTK("clear nexus ha\n");
705 res = i->dft->lldd_clear_nexus_ha(ha);
706 if (res == TMF_RESP_FUNC_COMPLETE) {
707 SAS_DPRINTK("clear nexus ha "
708 "succeeded\n");
709 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
710 task->task_done(task);
711 if (need_reset)
712 try_to_reset_cmd_device(shost, cmd);
713 goto out;
714 =======
715 sas_eh_finish_cmd(cmd);
716 goto clear_q;
717 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
720 /* If we are here -- this means that no amount
721 * of effort could recover from errors. Quite
722 * possibly the HA just disappeared.
724 SAS_DPRINTK("error from device %llx, LUN %x "
725 "couldn't be recovered in any way\n",
726 SAS_ADDR(task->dev->sas_addr),
727 cmd->device->lun);
729 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
730 task->task_done(task);
731 if (need_reset)
732 try_to_reset_cmd_device(shost, cmd);
733 =======
734 sas_eh_finish_cmd(cmd);
735 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
736 goto clear_q;
739 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
740 out:
741 =======
742 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
743 return list_empty(work_q);
744 clear_q:
745 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
746 <<<<<<< HEAD:drivers/scsi/libsas/sas_scsi_host.c
747 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
748 struct sas_task *task = TO_SAS_TASK(cmd);
749 list_del_init(&cmd->eh_entry);
750 task->task_done(task);
752 =======
753 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
754 sas_eh_finish_cmd(cmd);
756 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_scsi_host.c
757 return list_empty(work_q);
760 void sas_scsi_recover_host(struct Scsi_Host *shost)
762 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
763 unsigned long flags;
764 LIST_HEAD(eh_work_q);
766 spin_lock_irqsave(shost->host_lock, flags);
767 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
768 spin_unlock_irqrestore(shost->host_lock, flags);
770 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
772 * Deal with commands that still have SAS tasks (i.e. they didn't
773 * complete via the normal sas_task completion mechanism)
775 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
776 goto out;
779 * Now deal with SCSI commands that completed ok but have a an error
780 * code (and hopefully sense data) attached. This is roughly what
781 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
782 * command we see here has no sas_task and is thus unknown to the HA.
784 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
785 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
787 out:
788 scsi_eh_flush_done_q(&ha->eh_done_q);
789 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
790 return;
793 enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
795 struct sas_task *task = TO_SAS_TASK(cmd);
796 unsigned long flags;
798 if (!task) {
799 cmd->timeout_per_command /= 2;
800 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
801 cmd, task, (cmd->timeout_per_command ?
802 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
803 if (!cmd->timeout_per_command)
804 return EH_NOT_HANDLED;
805 return EH_RESET_TIMER;
808 spin_lock_irqsave(&task->task_state_lock, flags);
809 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
810 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
811 spin_unlock_irqrestore(&task->task_state_lock, flags);
812 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
813 cmd, task);
814 return EH_HANDLED;
816 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
817 spin_unlock_irqrestore(&task->task_state_lock, flags);
818 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
819 "EH_RESET_TIMER\n",
820 cmd, task);
821 return EH_RESET_TIMER;
823 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
824 spin_unlock_irqrestore(&task->task_state_lock, flags);
826 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
827 cmd, task);
829 return EH_NOT_HANDLED;
832 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
834 struct domain_device *dev = sdev_to_domain_dev(sdev);
836 if (dev_is_sata(dev))
837 return ata_scsi_ioctl(sdev, cmd, arg);
839 return -EINVAL;
842 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
844 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
845 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
846 struct domain_device *found_dev = NULL;
847 int i;
848 unsigned long flags;
850 spin_lock_irqsave(&ha->phy_port_lock, flags);
851 for (i = 0; i < ha->num_phys; i++) {
852 struct asd_sas_port *port = ha->sas_port[i];
853 struct domain_device *dev;
855 spin_lock(&port->dev_list_lock);
856 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
857 if (rphy == dev->rphy) {
858 found_dev = dev;
859 spin_unlock(&port->dev_list_lock);
860 goto found;
863 spin_unlock(&port->dev_list_lock);
865 found:
866 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
868 return found_dev;
871 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
873 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
875 return sas_find_dev_by_rphy(rphy);
878 int sas_target_alloc(struct scsi_target *starget)
880 struct domain_device *found_dev = sas_find_target(starget);
881 int res;
883 if (!found_dev)
884 return -ENODEV;
886 if (dev_is_sata(found_dev)) {
887 res = sas_ata_init_host_and_port(found_dev, starget);
888 if (res)
889 return res;
892 starget->hostdata = found_dev;
893 return 0;
896 #define SAS_DEF_QD 32
897 #define SAS_MAX_QD 64
899 int sas_slave_configure(struct scsi_device *scsi_dev)
901 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
902 struct sas_ha_struct *sas_ha;
904 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
906 if (dev_is_sata(dev)) {
907 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
908 return 0;
911 sas_ha = dev->port->ha;
913 sas_read_port_mode_page(scsi_dev);
915 if (scsi_dev->tagged_supported) {
916 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
917 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
918 } else {
919 SAS_DPRINTK("device %llx, LUN %x doesn't support "
920 "TCQ\n", SAS_ADDR(dev->sas_addr),
921 scsi_dev->lun);
922 scsi_dev->tagged_supported = 0;
923 scsi_set_tag_type(scsi_dev, 0);
924 scsi_deactivate_tcq(scsi_dev, 1);
927 scsi_dev->allow_restart = 1;
929 return 0;
932 void sas_slave_destroy(struct scsi_device *scsi_dev)
934 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
936 if (dev_is_sata(dev))
937 ata_port_disable(dev->sata_dev.ap);
940 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
942 int res = min(new_depth, SAS_MAX_QD);
944 if (scsi_dev->tagged_supported)
945 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
946 res);
947 else {
948 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
949 sas_printk("device %llx LUN %x queue depth changed to 1\n",
950 SAS_ADDR(dev->sas_addr),
951 scsi_dev->lun);
952 scsi_adjust_queue_depth(scsi_dev, 0, 1);
953 res = 1;
956 return res;
959 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
961 if (!scsi_dev->tagged_supported)
962 return 0;
964 scsi_deactivate_tcq(scsi_dev, 1);
966 scsi_set_tag_type(scsi_dev, qt);
967 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
969 return qt;
972 int sas_bios_param(struct scsi_device *scsi_dev,
973 struct block_device *bdev,
974 sector_t capacity, int *hsc)
976 hsc[0] = 255;
977 hsc[1] = 63;
978 sector_div(capacity, 255*63);
979 hsc[2] = capacity;
981 return 0;
984 /* ---------- Task Collector Thread implementation ---------- */
986 static void sas_queue(struct sas_ha_struct *sas_ha)
988 struct scsi_core *core = &sas_ha->core;
989 unsigned long flags;
990 LIST_HEAD(q);
991 int can_queue;
992 int res;
993 struct sas_internal *i = to_sas_internal(core->shost->transportt);
995 spin_lock_irqsave(&core->task_queue_lock, flags);
996 while (!kthread_should_stop() &&
997 !list_empty(&core->task_queue)) {
999 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
1000 if (can_queue >= 0) {
1001 can_queue = core->task_queue_size;
1002 list_splice_init(&core->task_queue, &q);
1003 } else {
1004 struct list_head *a, *n;
1006 can_queue = sas_ha->lldd_queue_size;
1007 list_for_each_safe(a, n, &core->task_queue) {
1008 list_move_tail(a, &q);
1009 if (--can_queue == 0)
1010 break;
1012 can_queue = sas_ha->lldd_queue_size;
1014 core->task_queue_size -= can_queue;
1015 spin_unlock_irqrestore(&core->task_queue_lock, flags);
1017 struct sas_task *task = list_entry(q.next,
1018 struct sas_task,
1019 list);
1020 list_del_init(&q);
1021 res = i->dft->lldd_execute_task(task, can_queue,
1022 GFP_KERNEL);
1023 if (unlikely(res))
1024 __list_add(&q, task->list.prev, &task->list);
1026 spin_lock_irqsave(&core->task_queue_lock, flags);
1027 if (res) {
1028 list_splice_init(&q, &core->task_queue); /*at head*/
1029 core->task_queue_size += can_queue;
1032 spin_unlock_irqrestore(&core->task_queue_lock, flags);
1036 * sas_queue_thread -- The Task Collector thread
1037 * @_sas_ha: pointer to struct sas_ha
1039 static int sas_queue_thread(void *_sas_ha)
1041 struct sas_ha_struct *sas_ha = _sas_ha;
1043 while (1) {
1044 set_current_state(TASK_INTERRUPTIBLE);
1045 schedule();
1046 sas_queue(sas_ha);
1047 if (kthread_should_stop())
1048 break;
1051 return 0;
1054 int sas_init_queue(struct sas_ha_struct *sas_ha)
1056 struct scsi_core *core = &sas_ha->core;
1058 spin_lock_init(&core->task_queue_lock);
1059 core->task_queue_size = 0;
1060 INIT_LIST_HEAD(&core->task_queue);
1062 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
1063 "sas_queue_%d", core->shost->host_no);
1064 if (IS_ERR(core->queue_thread))
1065 return PTR_ERR(core->queue_thread);
1066 return 0;
1069 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
1071 unsigned long flags;
1072 struct scsi_core *core = &sas_ha->core;
1073 struct sas_task *task, *n;
1075 kthread_stop(core->queue_thread);
1077 if (!list_empty(&core->task_queue))
1078 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
1079 SAS_ADDR(sas_ha->sas_addr));
1081 spin_lock_irqsave(&core->task_queue_lock, flags);
1082 list_for_each_entry_safe(task, n, &core->task_queue, list) {
1083 struct scsi_cmnd *cmd = task->uldd_task;
1085 list_del_init(&task->list);
1087 ASSIGN_SAS_TASK(cmd, NULL);
1088 sas_free_task(task);
1089 cmd->result = DID_ABORT << 16;
1090 cmd->scsi_done(cmd);
1092 spin_unlock_irqrestore(&core->task_queue_lock, flags);
1096 * Call the LLDD task abort routine directly. This function is intended for
1097 * use by upper layers that need to tell the LLDD to abort a task.
1099 int __sas_task_abort(struct sas_task *task)
1101 struct sas_internal *si =
1102 to_sas_internal(task->dev->port->ha->core.shost->transportt);
1103 unsigned long flags;
1104 int res;
1106 spin_lock_irqsave(&task->task_state_lock, flags);
1107 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
1108 task->task_state_flags & SAS_TASK_STATE_DONE) {
1109 spin_unlock_irqrestore(&task->task_state_lock, flags);
1110 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
1111 task);
1112 return 0;
1114 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1115 spin_unlock_irqrestore(&task->task_state_lock, flags);
1117 if (!si->dft->lldd_abort_task)
1118 return -ENODEV;
1120 res = si->dft->lldd_abort_task(task);
1122 spin_lock_irqsave(&task->task_state_lock, flags);
1123 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
1124 (res == TMF_RESP_FUNC_COMPLETE))
1126 spin_unlock_irqrestore(&task->task_state_lock, flags);
1127 task->task_done(task);
1128 return 0;
1131 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1132 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1133 spin_unlock_irqrestore(&task->task_state_lock, flags);
1135 return -EAGAIN;
1139 * Tell an upper layer that it needs to initiate an abort for a given task.
1140 * This should only ever be called by an LLDD.
1142 void sas_task_abort(struct sas_task *task)
1144 struct scsi_cmnd *sc = task->uldd_task;
1146 /* Escape for libsas internal commands */
1147 if (!sc) {
1148 if (!del_timer(&task->timer))
1149 return;
1150 task->timer.function(task->timer.data);
1151 return;
1154 if (dev_is_sata(task->dev)) {
1155 sas_ata_task_abort(task);
1156 return;
1159 scsi_req_abort_cmd(sc);
1160 scsi_schedule_eh(sc->device->host);
1163 int sas_slave_alloc(struct scsi_device *scsi_dev)
1165 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1167 if (dev_is_sata(dev))
1168 return ata_sas_port_init(dev->sata_dev.ap);
1170 return 0;
1173 void sas_target_destroy(struct scsi_target *starget)
1175 struct domain_device *found_dev = sas_find_target(starget);
1177 if (!found_dev)
1178 return;
1180 if (dev_is_sata(found_dev))
1181 ata_sas_port_destroy(found_dev->sata_dev.ap);
1183 return;
1186 EXPORT_SYMBOL_GPL(sas_queuecommand);
1187 EXPORT_SYMBOL_GPL(sas_target_alloc);
1188 EXPORT_SYMBOL_GPL(sas_slave_configure);
1189 EXPORT_SYMBOL_GPL(sas_slave_destroy);
1190 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1191 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1192 EXPORT_SYMBOL_GPL(sas_bios_param);
1193 EXPORT_SYMBOL_GPL(__sas_task_abort);
1194 EXPORT_SYMBOL_GPL(sas_task_abort);
1195 EXPORT_SYMBOL_GPL(sas_phy_reset);
1196 EXPORT_SYMBOL_GPL(sas_phy_enable);
1197 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1198 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1199 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1200 EXPORT_SYMBOL_GPL(sas_target_destroy);
1201 EXPORT_SYMBOL_GPL(sas_ioctl);