blob: 704ea06a6e500a8d3bfd25179dc9efa73791c655 [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
2 * Serial Attached SCSI (SAS) class SCSI Host glue.
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
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.
13 *
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.
18 *
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
23 *
24 */
25
Christoph Hellwigd7a54e32007-04-26 09:38:01 -040026#include <linux/kthread.h>
27
James Bottomley2908d772006-08-29 09:22:51 -050028#include "sas_internal.h"
29
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi.h>
Darrick J. Wongf4563932006-10-30 15:18:39 -080034#include <scsi/scsi_eh.h>
James Bottomley2908d772006-08-29 09:22:51 -050035#include <scsi/scsi_transport.h>
36#include <scsi/scsi_transport_sas.h>
Darrick J. Wong338ec572006-10-18 14:43:37 -070037#include <scsi/sas_ata.h>
James Bottomley2908d772006-08-29 09:22:51 -050038#include "../scsi_sas_internal.h"
Darrick J. Wong79a5eb62006-10-30 15:18:50 -080039#include "../scsi_transport_api.h"
Darrick J. Wongad689232007-01-26 14:08:52 -080040#include "../scsi_priv.h"
James Bottomley2908d772006-08-29 09:22:51 -050041
42#include <linux/err.h>
43#include <linux/blkdev.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070044#include <linux/freezer.h>
James Bottomley2908d772006-08-29 09:22:51 -050045#include <linux/scatterlist.h>
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -070046#include <linux/libata.h>
James Bottomley2908d772006-08-29 09:22:51 -050047
48/* ---------- SCSI Host glue ---------- */
49
James Bottomley2908d772006-08-29 09:22:51 -050050static void sas_scsi_task_done(struct sas_task *task)
51{
52 struct task_status_struct *ts = &task->task_status;
53 struct scsi_cmnd *sc = task->uldd_task;
James Bottomley2908d772006-08-29 09:22:51 -050054 int hs = 0, stat = 0;
55
James Bottomleya8e14fe2008-02-19 21:48:42 -060056 if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
57 /* Aborted tasks will be completed by the error handler */
58 SAS_DPRINTK("task done but aborted\n");
59 return;
60 }
61
James Bottomley2908d772006-08-29 09:22:51 -050062 if (unlikely(!sc)) {
63 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
64 list_del_init(&task->list);
65 sas_free_task(task);
66 return;
67 }
68
69 if (ts->resp == SAS_TASK_UNDELIVERED) {
70 /* transport error */
71 hs = DID_NO_CONNECT;
72 } else { /* ts->resp == SAS_TASK_COMPLETE */
73 /* task delivered, what happened afterwards? */
74 switch (ts->stat) {
75 case SAS_DEV_NO_RESPONSE:
76 case SAS_INTERRUPTED:
77 case SAS_PHY_DOWN:
78 case SAS_NAK_R_ERR:
79 case SAS_OPEN_TO:
80 hs = DID_NO_CONNECT;
81 break;
82 case SAS_DATA_UNDERRUN:
FUJITA Tomonoric13e5562007-05-26 02:46:37 +090083 scsi_set_resid(sc, ts->residual);
84 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
James Bottomley2908d772006-08-29 09:22:51 -050085 hs = DID_ERROR;
86 break;
87 case SAS_DATA_OVERRUN:
88 hs = DID_ERROR;
89 break;
90 case SAS_QUEUE_FULL:
91 hs = DID_SOFT_ERROR; /* retry */
92 break;
93 case SAS_DEVICE_UNKNOWN:
94 hs = DID_BAD_TARGET;
95 break;
96 case SAS_SG_ERR:
97 hs = DID_PARITY;
98 break;
99 case SAS_OPEN_REJECT:
100 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
101 hs = DID_SOFT_ERROR; /* retry */
102 else
103 hs = DID_ERROR;
104 break;
105 case SAS_PROTO_RESPONSE:
106 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
107 "task; please report this\n",
108 task->dev->port->ha->sas_ha_name);
109 break;
110 case SAS_ABORTED_TASK:
111 hs = DID_ABORT;
112 break;
113 case SAM_CHECK_COND:
114 memcpy(sc->sense_buffer, ts->buf,
FUJITA Tomonoricc75e8a2008-01-13 02:20:18 +0900115 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
James Bottomley2908d772006-08-29 09:22:51 -0500116 stat = SAM_CHECK_COND;
117 break;
118 default:
119 stat = ts->stat;
120 break;
121 }
122 }
123 ASSIGN_SAS_TASK(sc, NULL);
124 sc->result = (hs << 16) | stat;
125 list_del_init(&task->list);
126 sas_free_task(task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600127 sc->scsi_done(sc);
James Bottomley2908d772006-08-29 09:22:51 -0500128}
129
130static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
131{
132 enum task_attribute ta = TASK_ATTR_SIMPLE;
133 if (cmd->request && blk_rq_tagged(cmd->request)) {
134 if (cmd->device->ordered_tags &&
Jeff Garzik1bdfd552006-09-30 21:28:22 -0400135 (cmd->request->cmd_flags & REQ_HARDBARRIER))
FUJITA Tomonori63bb1bf2007-01-28 01:19:41 +0900136 ta = TASK_ATTR_ORDERED;
James Bottomley2908d772006-08-29 09:22:51 -0500137 }
138 return ta;
139}
140
141static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
142 struct domain_device *dev,
Al Viro3cc27542006-09-25 02:55:40 +0100143 gfp_t gfp_flags)
James Bottomley2908d772006-08-29 09:22:51 -0500144{
145 struct sas_task *task = sas_alloc_task(gfp_flags);
146 struct scsi_lun lun;
147
148 if (!task)
149 return NULL;
150
James Bottomley2908d772006-08-29 09:22:51 -0500151 task->uldd_task = cmd;
152 ASSIGN_SAS_TASK(cmd, task);
153
154 task->dev = dev;
155 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
156
157 task->ssp_task.retry_count = 1;
158 int_to_scsilun(cmd->device->lun, &lun);
159 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
160 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
161 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
162
FUJITA Tomonoric13e5562007-05-26 02:46:37 +0900163 task->scatter = scsi_sglist(cmd);
164 task->num_scatter = scsi_sg_count(cmd);
165 task->total_xfer_len = scsi_bufflen(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500166 task->data_dir = cmd->sc_data_direction;
167
168 task->task_done = sas_scsi_task_done;
169
170 return task;
171}
172
Darrick J. Wong338ec572006-10-18 14:43:37 -0700173int sas_queue_up(struct sas_task *task)
James Bottomley2908d772006-08-29 09:22:51 -0500174{
175 struct sas_ha_struct *sas_ha = task->dev->port->ha;
176 struct scsi_core *core = &sas_ha->core;
177 unsigned long flags;
178 LIST_HEAD(list);
179
180 spin_lock_irqsave(&core->task_queue_lock, flags);
181 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
182 spin_unlock_irqrestore(&core->task_queue_lock, flags);
183 return -SAS_QUEUE_FULL;
184 }
185 list_add_tail(&task->list, &core->task_queue);
186 core->task_queue_size += 1;
187 spin_unlock_irqrestore(&core->task_queue_lock, flags);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400188 wake_up_process(core->queue_thread);
James Bottomley2908d772006-08-29 09:22:51 -0500189
190 return 0;
191}
192
193/**
194 * sas_queuecommand -- Enqueue a command for processing
195 * @parameters: See SCSI Core documentation
196 *
197 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
198 * call us without holding an IRQ spinlock...
199 */
200int sas_queuecommand(struct scsi_cmnd *cmd,
201 void (*scsi_done)(struct scsi_cmnd *))
Darrick J. Wong8ee24022007-11-05 11:52:14 -0800202 __releases(host->host_lock)
203 __acquires(dev->sata_dev.ap->lock)
204 __releases(dev->sata_dev.ap->lock)
205 __acquires(host->host_lock)
James Bottomley2908d772006-08-29 09:22:51 -0500206{
207 int res = 0;
208 struct domain_device *dev = cmd_to_domain_dev(cmd);
209 struct Scsi_Host *host = cmd->device->host;
210 struct sas_internal *i = to_sas_internal(host->transportt);
211
212 spin_unlock_irq(host->host_lock);
213
214 {
215 struct sas_ha_struct *sas_ha = dev->port->ha;
216 struct sas_task *task;
217
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700218 if (dev_is_sata(dev)) {
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800219 unsigned long flags;
220
221 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700222 res = ata_sas_queuecmd(cmd, scsi_done,
223 dev->sata_dev.ap);
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800224 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700225 goto out;
226 }
227
James Bottomley2908d772006-08-29 09:22:51 -0500228 res = -ENOMEM;
229 task = sas_create_task(cmd, dev, GFP_ATOMIC);
230 if (!task)
231 goto out;
232
233 cmd->scsi_done = scsi_done;
234 /* Queue up, Direct Mode or Task Collector Mode. */
235 if (sas_ha->lldd_max_execute_num < 2)
236 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
237 else
238 res = sas_queue_up(task);
239
240 /* Examine */
241 if (res) {
242 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
243 ASSIGN_SAS_TASK(cmd, NULL);
244 sas_free_task(task);
245 if (res == -SAS_QUEUE_FULL) {
246 cmd->result = DID_SOFT_ERROR << 16; /* retry */
247 res = 0;
248 scsi_done(cmd);
249 }
250 goto out;
251 }
252 }
253out:
254 spin_lock_irq(host->host_lock);
255 return res;
256}
257
James Bottomleya8e14fe2008-02-19 21:48:42 -0600258static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
259{
260 struct sas_task *task = TO_SAS_TASK(cmd);
261 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
262
263 /* remove the aborted task flag to allow the task to be
264 * completed now. At this point, we only get called following
265 * an actual abort of the task, so we should be guaranteed not
266 * to be racing with any completions from the LLD (hence we
267 * don't need the task state lock to clear the flag) */
268 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
269 /* Now call task_done. However, task will be free'd after
270 * this */
271 task->task_done(task);
272 /* now finish the command and move it on to the error
273 * handler done list, this also takes it off the
274 * error handler pending list */
275 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
276}
277
James Bottomley2908d772006-08-29 09:22:51 -0500278static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
279{
280 struct scsi_cmnd *cmd, *n;
281
282 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
James Bottomley63e45632008-02-22 17:07:52 -0600283 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
284 cmd->device->lun == my_cmd->device->lun)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600285 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500286 }
287}
288
289static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
290 struct domain_device *dev)
291{
292 struct scsi_cmnd *cmd, *n;
293
294 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
295 struct domain_device *x = cmd_to_domain_dev(cmd);
296
297 if (x == dev)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600298 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500299 }
300}
301
302static void sas_scsi_clear_queue_port(struct list_head *error_q,
303 struct asd_sas_port *port)
304{
305 struct scsi_cmnd *cmd, *n;
306
307 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
308 struct domain_device *dev = cmd_to_domain_dev(cmd);
309 struct asd_sas_port *x = dev->port;
310
311 if (x == port)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600312 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500313 }
314}
315
316enum task_disposition {
317 TASK_IS_DONE,
318 TASK_IS_ABORTED,
319 TASK_IS_AT_LU,
320 TASK_IS_NOT_AT_LU,
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800321 TASK_ABORT_FAILED,
James Bottomley2908d772006-08-29 09:22:51 -0500322};
323
324static enum task_disposition sas_scsi_find_task(struct sas_task *task)
325{
326 struct sas_ha_struct *ha = task->dev->port->ha;
327 unsigned long flags;
328 int i, res;
329 struct sas_internal *si =
330 to_sas_internal(task->dev->port->ha->core.shost->transportt);
331
332 if (ha->lldd_max_execute_num > 1) {
333 struct scsi_core *core = &ha->core;
334 struct sas_task *t, *n;
335
336 spin_lock_irqsave(&core->task_queue_lock, flags);
337 list_for_each_entry_safe(t, n, &core->task_queue, list) {
338 if (task == t) {
339 list_del_init(&t->list);
340 spin_unlock_irqrestore(&core->task_queue_lock,
341 flags);
342 SAS_DPRINTK("%s: task 0x%p aborted from "
343 "task_queue\n",
344 __FUNCTION__, task);
345 return TASK_IS_ABORTED;
346 }
347 }
348 spin_unlock_irqrestore(&core->task_queue_lock, flags);
349 }
350
351 for (i = 0; i < 5; i++) {
352 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
353 res = si->dft->lldd_abort_task(task);
354
355 spin_lock_irqsave(&task->task_state_lock, flags);
356 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
357 spin_unlock_irqrestore(&task->task_state_lock, flags);
358 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
359 task);
360 return TASK_IS_DONE;
361 }
362 spin_unlock_irqrestore(&task->task_state_lock, flags);
363
364 if (res == TMF_RESP_FUNC_COMPLETE) {
365 SAS_DPRINTK("%s: task 0x%p is aborted\n",
366 __FUNCTION__, task);
367 return TASK_IS_ABORTED;
368 } else if (si->dft->lldd_query_task) {
369 SAS_DPRINTK("%s: querying task 0x%p\n",
370 __FUNCTION__, task);
371 res = si->dft->lldd_query_task(task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800372 switch (res) {
373 case TMF_RESP_FUNC_SUCC:
James Bottomley2908d772006-08-29 09:22:51 -0500374 SAS_DPRINTK("%s: task 0x%p at LU\n",
375 __FUNCTION__, task);
376 return TASK_IS_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800377 case TMF_RESP_FUNC_COMPLETE:
James Bottomley2908d772006-08-29 09:22:51 -0500378 SAS_DPRINTK("%s: task 0x%p not at LU\n",
379 __FUNCTION__, task);
380 return TASK_IS_NOT_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800381 case TMF_RESP_FUNC_FAILED:
382 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
383 __FUNCTION__, task);
384 return TASK_ABORT_FAILED;
385 }
386
James Bottomley2908d772006-08-29 09:22:51 -0500387 }
388 }
389 return res;
390}
391
392static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
393{
394 int res = TMF_RESP_FUNC_FAILED;
395 struct scsi_lun lun;
396 struct sas_internal *i =
397 to_sas_internal(dev->port->ha->core.shost->transportt);
398
399 int_to_scsilun(cmd->device->lun, &lun);
400
401 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
402 SAS_ADDR(dev->sas_addr),
403 cmd->device->lun);
404
405 if (i->dft->lldd_abort_task_set)
406 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
407
408 if (res == TMF_RESP_FUNC_FAILED) {
409 if (i->dft->lldd_clear_task_set)
410 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
411 }
412
413 if (res == TMF_RESP_FUNC_FAILED) {
414 if (i->dft->lldd_lu_reset)
415 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
416 }
417
418 return res;
419}
420
421static int sas_recover_I_T(struct domain_device *dev)
422{
423 int res = TMF_RESP_FUNC_FAILED;
424 struct sas_internal *i =
425 to_sas_internal(dev->port->ha->core.shost->transportt);
426
427 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
428 SAS_ADDR(dev->sas_addr));
429
430 if (i->dft->lldd_I_T_nexus_reset)
431 res = i->dft->lldd_I_T_nexus_reset(dev);
432
433 return res;
434}
435
Darrick J. Wongad689232007-01-26 14:08:52 -0800436/* Find the sas_phy that's attached to this device */
Darrick J. Wong8ee24022007-11-05 11:52:14 -0800437static struct sas_phy *find_local_sas_phy(struct domain_device *dev)
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800438{
Darrick J. Wongad689232007-01-26 14:08:52 -0800439 struct domain_device *pdev = dev->parent;
440 struct ex_phy *exphy = NULL;
441 int i;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800442
Darrick J. Wongad689232007-01-26 14:08:52 -0800443 /* Directly attached device */
444 if (!pdev)
445 return dev->port->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800446
Darrick J. Wongad689232007-01-26 14:08:52 -0800447 /* Otherwise look in the expander */
448 for (i = 0; i < pdev->ex_dev.num_phys; i++)
449 if (!memcmp(dev->sas_addr,
450 pdev->ex_dev.ex_phy[i].attached_sas_addr,
451 SAS_ADDR_SIZE)) {
452 exphy = &pdev->ex_dev.ex_phy[i];
453 break;
454 }
455
456 BUG_ON(!exphy);
457 return exphy->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800458}
459
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800460/* Attempt to send a LUN reset message to a device */
Darrick J. Wongad689232007-01-26 14:08:52 -0800461int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500462{
Darrick J. Wongad689232007-01-26 14:08:52 -0800463 struct domain_device *dev = cmd_to_domain_dev(cmd);
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800464 struct sas_internal *i =
465 to_sas_internal(dev->port->ha->core.shost->transportt);
466 struct scsi_lun lun;
467 int res;
468
469 int_to_scsilun(cmd->device->lun, &lun);
470
471 if (!i->dft->lldd_lu_reset)
472 return FAILED;
473
474 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
475 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
476 return SUCCESS;
477
478 return FAILED;
479}
480
481/* Attempt to send a phy (bus) reset */
482int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
483{
484 struct domain_device *dev = cmd_to_domain_dev(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800485 struct sas_phy *phy = find_local_sas_phy(dev);
486 int res;
487
488 res = sas_phy_reset(phy, 1);
489 if (res)
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800490 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100491 kobject_name(&phy->dev.kobj),
Darrick J. Wongad689232007-01-26 14:08:52 -0800492 res);
493 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
494 return SUCCESS;
495
496 return FAILED;
497}
498
499/* Try to reset a device */
500static int try_to_reset_cmd_device(struct Scsi_Host *shost,
501 struct scsi_cmnd *cmd)
502{
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800503 int res;
Darrick J. Wongad689232007-01-26 14:08:52 -0800504
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800505 if (!shost->hostt->eh_device_reset_handler)
506 goto try_bus_reset;
507
508 res = shost->hostt->eh_device_reset_handler(cmd);
509 if (res == SUCCESS)
510 return res;
511
512try_bus_reset:
513 if (shost->hostt->eh_bus_reset_handler)
514 return shost->hostt->eh_bus_reset_handler(cmd);
515
516 return FAILED;
Darrick J. Wongad689232007-01-26 14:08:52 -0800517}
518
519static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
520 struct list_head *work_q,
521 struct list_head *done_q)
522{
James Bottomley2908d772006-08-29 09:22:51 -0500523 struct scsi_cmnd *cmd, *n;
524 enum task_disposition res = TASK_IS_DONE;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800525 int tmf_resp, need_reset;
James Bottomley2908d772006-08-29 09:22:51 -0500526 struct sas_internal *i = to_sas_internal(shost->transportt);
Darrick J. Wongad689232007-01-26 14:08:52 -0800527 unsigned long flags;
528 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
James Bottomley2908d772006-08-29 09:22:51 -0500529
James Bottomley2908d772006-08-29 09:22:51 -0500530Again:
Darrick J. Wongad689232007-01-26 14:08:52 -0800531 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
James Bottomley2908d772006-08-29 09:22:51 -0500532 struct sas_task *task = TO_SAS_TASK(cmd);
Darrick J. Wongf4563932006-10-30 15:18:39 -0800533
Darrick J. Wongad689232007-01-26 14:08:52 -0800534 if (!task)
Darrick J. Wongf4563932006-10-30 15:18:39 -0800535 continue;
Darrick J. Wongad689232007-01-26 14:08:52 -0800536
537 list_del_init(&cmd->eh_entry);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800538
539 spin_lock_irqsave(&task->task_state_lock, flags);
540 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800541 spin_unlock_irqrestore(&task->task_state_lock, flags);
542
Darrick J. Wongf4563932006-10-30 15:18:39 -0800543 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500544 res = sas_scsi_find_task(task);
545
546 cmd->eh_eflags = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500547
548 switch (res) {
549 case TASK_IS_DONE:
550 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
551 task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600552 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800553 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800554 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500555 continue;
556 case TASK_IS_ABORTED:
557 SAS_DPRINTK("%s: task 0x%p is aborted\n",
558 __FUNCTION__, task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600559 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800560 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800561 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500562 continue;
563 case TASK_IS_AT_LU:
564 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
565 tmf_resp = sas_recover_lu(task->dev, cmd);
566 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
567 SAS_DPRINTK("dev %016llx LU %x is "
568 "recovered\n",
569 SAS_ADDR(task->dev),
570 cmd->device->lun);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600571 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800572 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800573 try_to_reset_cmd_device(shost, cmd);
574 sas_scsi_clear_queue_lu(work_q, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500575 goto Again;
576 }
577 /* fallthrough */
578 case TASK_IS_NOT_AT_LU:
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800579 case TASK_ABORT_FAILED:
James Bottomley2908d772006-08-29 09:22:51 -0500580 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
581 task);
582 tmf_resp = sas_recover_I_T(task->dev);
583 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
584 SAS_DPRINTK("I_T %016llx recovered\n",
585 SAS_ADDR(task->dev->sas_addr));
James Bottomleya8e14fe2008-02-19 21:48:42 -0600586 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800587 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800588 try_to_reset_cmd_device(shost, cmd);
589 sas_scsi_clear_queue_I_T(work_q, task->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500590 goto Again;
591 }
592 /* Hammer time :-) */
593 if (i->dft->lldd_clear_nexus_port) {
594 struct asd_sas_port *port = task->dev->port;
595 SAS_DPRINTK("clearing nexus for port:%d\n",
596 port->id);
597 res = i->dft->lldd_clear_nexus_port(port);
598 if (res == TMF_RESP_FUNC_COMPLETE) {
599 SAS_DPRINTK("clear nexus port:%d "
600 "succeeded\n", port->id);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600601 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800602 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800603 try_to_reset_cmd_device(shost, cmd);
604 sas_scsi_clear_queue_port(work_q,
James Bottomley2908d772006-08-29 09:22:51 -0500605 port);
606 goto Again;
607 }
608 }
609 if (i->dft->lldd_clear_nexus_ha) {
610 SAS_DPRINTK("clear nexus ha\n");
611 res = i->dft->lldd_clear_nexus_ha(ha);
612 if (res == TMF_RESP_FUNC_COMPLETE) {
613 SAS_DPRINTK("clear nexus ha "
614 "succeeded\n");
James Bottomleya8e14fe2008-02-19 21:48:42 -0600615 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800616 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800617 try_to_reset_cmd_device(shost, cmd);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600618 goto clear_q;
James Bottomley2908d772006-08-29 09:22:51 -0500619 }
620 }
621 /* If we are here -- this means that no amount
622 * of effort could recover from errors. Quite
623 * possibly the HA just disappeared.
624 */
625 SAS_DPRINTK("error from device %llx, LUN %x "
626 "couldn't be recovered in any way\n",
627 SAS_ADDR(task->dev->sas_addr),
628 cmd->device->lun);
629
James Bottomleya8e14fe2008-02-19 21:48:42 -0600630 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800631 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800632 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500633 goto clear_q;
634 }
635 }
Darrick J. Wongad689232007-01-26 14:08:52 -0800636 return list_empty(work_q);
James Bottomley2908d772006-08-29 09:22:51 -0500637clear_q:
638 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600639 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
640 sas_eh_finish_cmd(cmd);
641
Darrick J. Wongad689232007-01-26 14:08:52 -0800642 return list_empty(work_q);
643}
644
645void sas_scsi_recover_host(struct Scsi_Host *shost)
646{
647 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
648 unsigned long flags;
649 LIST_HEAD(eh_work_q);
650
651 spin_lock_irqsave(shost->host_lock, flags);
652 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
653 spin_unlock_irqrestore(shost->host_lock, flags);
654
655 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
656 /*
657 * Deal with commands that still have SAS tasks (i.e. they didn't
658 * complete via the normal sas_task completion mechanism)
659 */
660 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
661 goto out;
662
663 /*
664 * Now deal with SCSI commands that completed ok but have a an error
665 * code (and hopefully sense data) attached. This is roughly what
666 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
667 * command we see here has no sas_task and is thus unknown to the HA.
668 */
669 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
670 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
671
672out:
673 scsi_eh_flush_done_q(&ha->eh_done_q);
674 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
675 return;
James Bottomley2908d772006-08-29 09:22:51 -0500676}
677
678enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
679{
680 struct sas_task *task = TO_SAS_TASK(cmd);
681 unsigned long flags;
682
683 if (!task) {
Darrick J. Wong6d4dcd42007-01-11 14:15:00 -0800684 cmd->timeout_per_command /= 2;
685 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
686 cmd, task, (cmd->timeout_per_command ?
687 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
688 if (!cmd->timeout_per_command)
689 return EH_NOT_HANDLED;
690 return EH_RESET_TIMER;
James Bottomley2908d772006-08-29 09:22:51 -0500691 }
692
693 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800694 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
James Bottomley2908d772006-08-29 09:22:51 -0500695 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
696 spin_unlock_irqrestore(&task->task_state_lock, flags);
697 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
698 cmd, task);
699 return EH_HANDLED;
700 }
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800701 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
702 spin_unlock_irqrestore(&task->task_state_lock, flags);
703 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
704 "EH_RESET_TIMER\n",
705 cmd, task);
706 return EH_RESET_TIMER;
707 }
James Bottomley2908d772006-08-29 09:22:51 -0500708 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
709 spin_unlock_irqrestore(&task->task_state_lock, flags);
710
711 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
712 cmd, task);
713
714 return EH_NOT_HANDLED;
715}
716
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700717int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
718{
719 struct domain_device *dev = sdev_to_domain_dev(sdev);
720
721 if (dev_is_sata(dev))
722 return ata_scsi_ioctl(sdev, cmd, arg);
723
724 return -EINVAL;
725}
726
James Bottomley2908d772006-08-29 09:22:51 -0500727struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
728{
729 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
730 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
731 struct domain_device *found_dev = NULL;
732 int i;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800733 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500734
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800735 spin_lock_irqsave(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500736 for (i = 0; i < ha->num_phys; i++) {
737 struct asd_sas_port *port = ha->sas_port[i];
738 struct domain_device *dev;
739
740 spin_lock(&port->dev_list_lock);
741 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
742 if (rphy == dev->rphy) {
743 found_dev = dev;
744 spin_unlock(&port->dev_list_lock);
745 goto found;
746 }
747 }
748 spin_unlock(&port->dev_list_lock);
749 }
750 found:
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800751 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500752
753 return found_dev;
754}
755
756static inline struct domain_device *sas_find_target(struct scsi_target *starget)
757{
758 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
759
760 return sas_find_dev_by_rphy(rphy);
761}
762
763int sas_target_alloc(struct scsi_target *starget)
764{
765 struct domain_device *found_dev = sas_find_target(starget);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700766 int res;
James Bottomley2908d772006-08-29 09:22:51 -0500767
768 if (!found_dev)
769 return -ENODEV;
770
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700771 if (dev_is_sata(found_dev)) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700772 res = sas_ata_init_host_and_port(found_dev, starget);
773 if (res)
774 return res;
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700775 }
776
James Bottomley2908d772006-08-29 09:22:51 -0500777 starget->hostdata = found_dev;
778 return 0;
779}
780
781#define SAS_DEF_QD 32
782#define SAS_MAX_QD 64
783
784int sas_slave_configure(struct scsi_device *scsi_dev)
785{
786 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
787 struct sas_ha_struct *sas_ha;
788
789 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
790
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700791 if (dev_is_sata(dev)) {
792 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
793 return 0;
794 }
795
James Bottomley2908d772006-08-29 09:22:51 -0500796 sas_ha = dev->port->ha;
797
798 sas_read_port_mode_page(scsi_dev);
799
800 if (scsi_dev->tagged_supported) {
801 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
802 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
803 } else {
804 SAS_DPRINTK("device %llx, LUN %x doesn't support "
805 "TCQ\n", SAS_ADDR(dev->sas_addr),
806 scsi_dev->lun);
807 scsi_dev->tagged_supported = 0;
808 scsi_set_tag_type(scsi_dev, 0);
809 scsi_deactivate_tcq(scsi_dev, 1);
810 }
811
Darrick J. Wongf27708f2007-01-26 14:08:58 -0800812 scsi_dev->allow_restart = 1;
813
James Bottomley2908d772006-08-29 09:22:51 -0500814 return 0;
815}
816
817void sas_slave_destroy(struct scsi_device *scsi_dev)
818{
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700819 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
820
821 if (dev_is_sata(dev))
822 ata_port_disable(dev->sata_dev.ap);
James Bottomley2908d772006-08-29 09:22:51 -0500823}
824
825int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
826{
827 int res = min(new_depth, SAS_MAX_QD);
828
829 if (scsi_dev->tagged_supported)
830 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
831 res);
832 else {
833 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
834 sas_printk("device %llx LUN %x queue depth changed to 1\n",
835 SAS_ADDR(dev->sas_addr),
836 scsi_dev->lun);
837 scsi_adjust_queue_depth(scsi_dev, 0, 1);
838 res = 1;
839 }
840
841 return res;
842}
843
844int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
845{
846 if (!scsi_dev->tagged_supported)
847 return 0;
848
849 scsi_deactivate_tcq(scsi_dev, 1);
850
851 scsi_set_tag_type(scsi_dev, qt);
852 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
853
854 return qt;
855}
856
857int sas_bios_param(struct scsi_device *scsi_dev,
858 struct block_device *bdev,
859 sector_t capacity, int *hsc)
860{
861 hsc[0] = 255;
862 hsc[1] = 63;
863 sector_div(capacity, 255*63);
864 hsc[2] = capacity;
865
866 return 0;
867}
868
869/* ---------- Task Collector Thread implementation ---------- */
870
871static void sas_queue(struct sas_ha_struct *sas_ha)
872{
873 struct scsi_core *core = &sas_ha->core;
874 unsigned long flags;
875 LIST_HEAD(q);
876 int can_queue;
877 int res;
878 struct sas_internal *i = to_sas_internal(core->shost->transportt);
879
880 spin_lock_irqsave(&core->task_queue_lock, flags);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400881 while (!kthread_should_stop() &&
James Bottomley2908d772006-08-29 09:22:51 -0500882 !list_empty(&core->task_queue)) {
883
884 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
885 if (can_queue >= 0) {
886 can_queue = core->task_queue_size;
887 list_splice_init(&core->task_queue, &q);
888 } else {
889 struct list_head *a, *n;
890
891 can_queue = sas_ha->lldd_queue_size;
892 list_for_each_safe(a, n, &core->task_queue) {
893 list_move_tail(a, &q);
894 if (--can_queue == 0)
895 break;
896 }
897 can_queue = sas_ha->lldd_queue_size;
898 }
899 core->task_queue_size -= can_queue;
900 spin_unlock_irqrestore(&core->task_queue_lock, flags);
901 {
902 struct sas_task *task = list_entry(q.next,
903 struct sas_task,
904 list);
905 list_del_init(&q);
906 res = i->dft->lldd_execute_task(task, can_queue,
907 GFP_KERNEL);
908 if (unlikely(res))
909 __list_add(&q, task->list.prev, &task->list);
910 }
911 spin_lock_irqsave(&core->task_queue_lock, flags);
912 if (res) {
913 list_splice_init(&q, &core->task_queue); /*at head*/
914 core->task_queue_size += can_queue;
915 }
916 }
917 spin_unlock_irqrestore(&core->task_queue_lock, flags);
918}
919
James Bottomley2908d772006-08-29 09:22:51 -0500920/**
921 * sas_queue_thread -- The Task Collector thread
922 * @_sas_ha: pointer to struct sas_ha
923 */
924static int sas_queue_thread(void *_sas_ha)
925{
926 struct sas_ha_struct *sas_ha = _sas_ha;
James Bottomley2908d772006-08-29 09:22:51 -0500927
James Bottomley2908d772006-08-29 09:22:51 -0500928 while (1) {
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400929 set_current_state(TASK_INTERRUPTIBLE);
930 schedule();
James Bottomley2908d772006-08-29 09:22:51 -0500931 sas_queue(sas_ha);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400932 if (kthread_should_stop())
James Bottomley2908d772006-08-29 09:22:51 -0500933 break;
934 }
935
James Bottomley2908d772006-08-29 09:22:51 -0500936 return 0;
937}
938
939int sas_init_queue(struct sas_ha_struct *sas_ha)
940{
James Bottomley2908d772006-08-29 09:22:51 -0500941 struct scsi_core *core = &sas_ha->core;
942
943 spin_lock_init(&core->task_queue_lock);
944 core->task_queue_size = 0;
945 INIT_LIST_HEAD(&core->task_queue);
James Bottomley2908d772006-08-29 09:22:51 -0500946
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400947 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
948 "sas_queue_%d", core->shost->host_no);
949 if (IS_ERR(core->queue_thread))
950 return PTR_ERR(core->queue_thread);
951 return 0;
James Bottomley2908d772006-08-29 09:22:51 -0500952}
953
954void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
955{
956 unsigned long flags;
957 struct scsi_core *core = &sas_ha->core;
958 struct sas_task *task, *n;
959
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400960 kthread_stop(core->queue_thread);
James Bottomley2908d772006-08-29 09:22:51 -0500961
962 if (!list_empty(&core->task_queue))
963 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
964 SAS_ADDR(sas_ha->sas_addr));
965
966 spin_lock_irqsave(&core->task_queue_lock, flags);
967 list_for_each_entry_safe(task, n, &core->task_queue, list) {
968 struct scsi_cmnd *cmd = task->uldd_task;
969
970 list_del_init(&task->list);
971
972 ASSIGN_SAS_TASK(cmd, NULL);
973 sas_free_task(task);
974 cmd->result = DID_ABORT << 16;
975 cmd->scsi_done(cmd);
976 }
977 spin_unlock_irqrestore(&core->task_queue_lock, flags);
978}
979
Darrick J. Wong396819f2007-01-11 14:15:20 -0800980/*
981 * Call the LLDD task abort routine directly. This function is intended for
982 * use by upper layers that need to tell the LLDD to abort a task.
983 */
984int __sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800985{
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800986 struct sas_internal *si =
987 to_sas_internal(task->dev->port->ha->core.shost->transportt);
988 unsigned long flags;
989 int res;
990
991 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800992 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
993 task->task_state_flags & SAS_TASK_STATE_DONE) {
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800994 spin_unlock_irqrestore(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800995 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800996 task);
997 return 0;
998 }
Darrick J. Wong396819f2007-01-11 14:15:20 -0800999 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001000 spin_unlock_irqrestore(&task->task_state_lock, flags);
1001
1002 if (!si->dft->lldd_abort_task)
1003 return -ENODEV;
1004
1005 res = si->dft->lldd_abort_task(task);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001006
1007 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001008 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
1009 (res == TMF_RESP_FUNC_COMPLETE))
1010 {
Darrick J. Wong396819f2007-01-11 14:15:20 -08001011 spin_unlock_irqrestore(&task->task_state_lock, flags);
1012 task->task_done(task);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001013 return 0;
1014 }
1015
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001016 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1017 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1018 spin_unlock_irqrestore(&task->task_state_lock, flags);
1019
1020 return -EAGAIN;
1021}
1022
Darrick J. Wong396819f2007-01-11 14:15:20 -08001023/*
1024 * Tell an upper layer that it needs to initiate an abort for a given task.
1025 * This should only ever be called by an LLDD.
1026 */
1027void sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001028{
Darrick J. Wong396819f2007-01-11 14:15:20 -08001029 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001030
Darrick J. Wong396819f2007-01-11 14:15:20 -08001031 /* Escape for libsas internal commands */
1032 if (!sc) {
1033 if (!del_timer(&task->timer))
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001034 return;
Darrick J. Wong396819f2007-01-11 14:15:20 -08001035 task->timer.function(task->timer.data);
1036 return;
1037 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001038
Darrick J. Wong3a2755a2007-01-30 01:18:58 -08001039 if (dev_is_sata(task->dev)) {
1040 sas_ata_task_abort(task);
1041 return;
1042 }
1043
Darrick J. Wong396819f2007-01-11 14:15:20 -08001044 scsi_req_abort_cmd(sc);
1045 scsi_schedule_eh(sc->device->host);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001046}
1047
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001048int sas_slave_alloc(struct scsi_device *scsi_dev)
1049{
1050 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1051
1052 if (dev_is_sata(dev))
1053 return ata_sas_port_init(dev->sata_dev.ap);
1054
1055 return 0;
1056}
1057
1058void sas_target_destroy(struct scsi_target *starget)
1059{
1060 struct domain_device *found_dev = sas_find_target(starget);
1061
1062 if (!found_dev)
1063 return;
1064
1065 if (dev_is_sata(found_dev))
1066 ata_sas_port_destroy(found_dev->sata_dev.ap);
1067
1068 return;
1069}
1070
James Bottomley2908d772006-08-29 09:22:51 -05001071EXPORT_SYMBOL_GPL(sas_queuecommand);
1072EXPORT_SYMBOL_GPL(sas_target_alloc);
1073EXPORT_SYMBOL_GPL(sas_slave_configure);
1074EXPORT_SYMBOL_GPL(sas_slave_destroy);
1075EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1076EXPORT_SYMBOL_GPL(sas_change_queue_type);
1077EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001078EXPORT_SYMBOL_GPL(__sas_task_abort);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001079EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -08001080EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001081EXPORT_SYMBOL_GPL(sas_phy_enable);
Darrick J. Wongad689232007-01-26 14:08:52 -08001082EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
Darrick J. Wonga9344e62007-01-29 23:48:19 -08001083EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001084EXPORT_SYMBOL_GPL(sas_slave_alloc);
1085EXPORT_SYMBOL_GPL(sas_target_destroy);
1086EXPORT_SYMBOL_GPL(sas_ioctl);