blob: 9c96d1bd36e240221fae332e81116bf1fc8b813b [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) {
283 if (cmd == my_cmd)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600284 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500285 }
286}
287
288static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
289 struct domain_device *dev)
290{
291 struct scsi_cmnd *cmd, *n;
292
293 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
294 struct domain_device *x = cmd_to_domain_dev(cmd);
295
296 if (x == dev)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600297 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500298 }
299}
300
301static void sas_scsi_clear_queue_port(struct list_head *error_q,
302 struct asd_sas_port *port)
303{
304 struct scsi_cmnd *cmd, *n;
305
306 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
307 struct domain_device *dev = cmd_to_domain_dev(cmd);
308 struct asd_sas_port *x = dev->port;
309
310 if (x == port)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600311 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500312 }
313}
314
315enum task_disposition {
316 TASK_IS_DONE,
317 TASK_IS_ABORTED,
318 TASK_IS_AT_LU,
319 TASK_IS_NOT_AT_LU,
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800320 TASK_ABORT_FAILED,
James Bottomley2908d772006-08-29 09:22:51 -0500321};
322
323static enum task_disposition sas_scsi_find_task(struct sas_task *task)
324{
325 struct sas_ha_struct *ha = task->dev->port->ha;
326 unsigned long flags;
327 int i, res;
328 struct sas_internal *si =
329 to_sas_internal(task->dev->port->ha->core.shost->transportt);
330
331 if (ha->lldd_max_execute_num > 1) {
332 struct scsi_core *core = &ha->core;
333 struct sas_task *t, *n;
334
335 spin_lock_irqsave(&core->task_queue_lock, flags);
336 list_for_each_entry_safe(t, n, &core->task_queue, list) {
337 if (task == t) {
338 list_del_init(&t->list);
339 spin_unlock_irqrestore(&core->task_queue_lock,
340 flags);
341 SAS_DPRINTK("%s: task 0x%p aborted from "
342 "task_queue\n",
343 __FUNCTION__, task);
344 return TASK_IS_ABORTED;
345 }
346 }
347 spin_unlock_irqrestore(&core->task_queue_lock, flags);
348 }
349
350 for (i = 0; i < 5; i++) {
351 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
352 res = si->dft->lldd_abort_task(task);
353
354 spin_lock_irqsave(&task->task_state_lock, flags);
355 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
356 spin_unlock_irqrestore(&task->task_state_lock, flags);
357 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
358 task);
359 return TASK_IS_DONE;
360 }
361 spin_unlock_irqrestore(&task->task_state_lock, flags);
362
363 if (res == TMF_RESP_FUNC_COMPLETE) {
364 SAS_DPRINTK("%s: task 0x%p is aborted\n",
365 __FUNCTION__, task);
366 return TASK_IS_ABORTED;
367 } else if (si->dft->lldd_query_task) {
368 SAS_DPRINTK("%s: querying task 0x%p\n",
369 __FUNCTION__, task);
370 res = si->dft->lldd_query_task(task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800371 switch (res) {
372 case TMF_RESP_FUNC_SUCC:
James Bottomley2908d772006-08-29 09:22:51 -0500373 SAS_DPRINTK("%s: task 0x%p at LU\n",
374 __FUNCTION__, task);
375 return TASK_IS_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800376 case TMF_RESP_FUNC_COMPLETE:
James Bottomley2908d772006-08-29 09:22:51 -0500377 SAS_DPRINTK("%s: task 0x%p not at LU\n",
378 __FUNCTION__, task);
379 return TASK_IS_NOT_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800380 case TMF_RESP_FUNC_FAILED:
381 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
382 __FUNCTION__, task);
383 return TASK_ABORT_FAILED;
384 }
385
James Bottomley2908d772006-08-29 09:22:51 -0500386 }
387 }
388 return res;
389}
390
391static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
392{
393 int res = TMF_RESP_FUNC_FAILED;
394 struct scsi_lun lun;
395 struct sas_internal *i =
396 to_sas_internal(dev->port->ha->core.shost->transportt);
397
398 int_to_scsilun(cmd->device->lun, &lun);
399
400 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
401 SAS_ADDR(dev->sas_addr),
402 cmd->device->lun);
403
404 if (i->dft->lldd_abort_task_set)
405 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
406
407 if (res == TMF_RESP_FUNC_FAILED) {
408 if (i->dft->lldd_clear_task_set)
409 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
410 }
411
412 if (res == TMF_RESP_FUNC_FAILED) {
413 if (i->dft->lldd_lu_reset)
414 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
415 }
416
417 return res;
418}
419
420static int sas_recover_I_T(struct domain_device *dev)
421{
422 int res = TMF_RESP_FUNC_FAILED;
423 struct sas_internal *i =
424 to_sas_internal(dev->port->ha->core.shost->transportt);
425
426 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
427 SAS_ADDR(dev->sas_addr));
428
429 if (i->dft->lldd_I_T_nexus_reset)
430 res = i->dft->lldd_I_T_nexus_reset(dev);
431
432 return res;
433}
434
Darrick J. Wongad689232007-01-26 14:08:52 -0800435/* Find the sas_phy that's attached to this device */
Darrick J. Wong8ee24022007-11-05 11:52:14 -0800436static struct sas_phy *find_local_sas_phy(struct domain_device *dev)
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800437{
Darrick J. Wongad689232007-01-26 14:08:52 -0800438 struct domain_device *pdev = dev->parent;
439 struct ex_phy *exphy = NULL;
440 int i;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800441
Darrick J. Wongad689232007-01-26 14:08:52 -0800442 /* Directly attached device */
443 if (!pdev)
444 return dev->port->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800445
Darrick J. Wongad689232007-01-26 14:08:52 -0800446 /* Otherwise look in the expander */
447 for (i = 0; i < pdev->ex_dev.num_phys; i++)
448 if (!memcmp(dev->sas_addr,
449 pdev->ex_dev.ex_phy[i].attached_sas_addr,
450 SAS_ADDR_SIZE)) {
451 exphy = &pdev->ex_dev.ex_phy[i];
452 break;
453 }
454
455 BUG_ON(!exphy);
456 return exphy->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800457}
458
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800459/* Attempt to send a LUN reset message to a device */
Darrick J. Wongad689232007-01-26 14:08:52 -0800460int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500461{
Darrick J. Wongad689232007-01-26 14:08:52 -0800462 struct domain_device *dev = cmd_to_domain_dev(cmd);
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800463 struct sas_internal *i =
464 to_sas_internal(dev->port->ha->core.shost->transportt);
465 struct scsi_lun lun;
466 int res;
467
468 int_to_scsilun(cmd->device->lun, &lun);
469
470 if (!i->dft->lldd_lu_reset)
471 return FAILED;
472
473 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
474 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
475 return SUCCESS;
476
477 return FAILED;
478}
479
480/* Attempt to send a phy (bus) reset */
481int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
482{
483 struct domain_device *dev = cmd_to_domain_dev(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800484 struct sas_phy *phy = find_local_sas_phy(dev);
485 int res;
486
487 res = sas_phy_reset(phy, 1);
488 if (res)
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800489 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100490 kobject_name(&phy->dev.kobj),
Darrick J. Wongad689232007-01-26 14:08:52 -0800491 res);
492 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
493 return SUCCESS;
494
495 return FAILED;
496}
497
498/* Try to reset a device */
499static int try_to_reset_cmd_device(struct Scsi_Host *shost,
500 struct scsi_cmnd *cmd)
501{
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800502 int res;
Darrick J. Wongad689232007-01-26 14:08:52 -0800503
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800504 if (!shost->hostt->eh_device_reset_handler)
505 goto try_bus_reset;
506
507 res = shost->hostt->eh_device_reset_handler(cmd);
508 if (res == SUCCESS)
509 return res;
510
511try_bus_reset:
512 if (shost->hostt->eh_bus_reset_handler)
513 return shost->hostt->eh_bus_reset_handler(cmd);
514
515 return FAILED;
Darrick J. Wongad689232007-01-26 14:08:52 -0800516}
517
518static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
519 struct list_head *work_q,
520 struct list_head *done_q)
521{
James Bottomley2908d772006-08-29 09:22:51 -0500522 struct scsi_cmnd *cmd, *n;
523 enum task_disposition res = TASK_IS_DONE;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800524 int tmf_resp, need_reset;
James Bottomley2908d772006-08-29 09:22:51 -0500525 struct sas_internal *i = to_sas_internal(shost->transportt);
Darrick J. Wongad689232007-01-26 14:08:52 -0800526 unsigned long flags;
527 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
James Bottomley2908d772006-08-29 09:22:51 -0500528
James Bottomley2908d772006-08-29 09:22:51 -0500529Again:
Darrick J. Wongad689232007-01-26 14:08:52 -0800530 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
James Bottomley2908d772006-08-29 09:22:51 -0500531 struct sas_task *task = TO_SAS_TASK(cmd);
Darrick J. Wongf4563932006-10-30 15:18:39 -0800532
Darrick J. Wongad689232007-01-26 14:08:52 -0800533 if (!task)
Darrick J. Wongf4563932006-10-30 15:18:39 -0800534 continue;
Darrick J. Wongad689232007-01-26 14:08:52 -0800535
536 list_del_init(&cmd->eh_entry);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800537
538 spin_lock_irqsave(&task->task_state_lock, flags);
539 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800540 spin_unlock_irqrestore(&task->task_state_lock, flags);
541
Darrick J. Wongf4563932006-10-30 15:18:39 -0800542 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500543 res = sas_scsi_find_task(task);
544
545 cmd->eh_eflags = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500546
547 switch (res) {
548 case TASK_IS_DONE:
549 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
550 task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600551 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800552 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800553 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500554 continue;
555 case TASK_IS_ABORTED:
556 SAS_DPRINTK("%s: task 0x%p is aborted\n",
557 __FUNCTION__, task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600558 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800559 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800560 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500561 continue;
562 case TASK_IS_AT_LU:
563 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
564 tmf_resp = sas_recover_lu(task->dev, cmd);
565 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
566 SAS_DPRINTK("dev %016llx LU %x is "
567 "recovered\n",
568 SAS_ADDR(task->dev),
569 cmd->device->lun);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600570 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800571 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800572 try_to_reset_cmd_device(shost, cmd);
573 sas_scsi_clear_queue_lu(work_q, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500574 goto Again;
575 }
576 /* fallthrough */
577 case TASK_IS_NOT_AT_LU:
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800578 case TASK_ABORT_FAILED:
James Bottomley2908d772006-08-29 09:22:51 -0500579 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
580 task);
581 tmf_resp = sas_recover_I_T(task->dev);
582 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
583 SAS_DPRINTK("I_T %016llx recovered\n",
584 SAS_ADDR(task->dev->sas_addr));
James Bottomleya8e14fe2008-02-19 21:48:42 -0600585 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800586 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800587 try_to_reset_cmd_device(shost, cmd);
588 sas_scsi_clear_queue_I_T(work_q, task->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500589 goto Again;
590 }
591 /* Hammer time :-) */
592 if (i->dft->lldd_clear_nexus_port) {
593 struct asd_sas_port *port = task->dev->port;
594 SAS_DPRINTK("clearing nexus for port:%d\n",
595 port->id);
596 res = i->dft->lldd_clear_nexus_port(port);
597 if (res == TMF_RESP_FUNC_COMPLETE) {
598 SAS_DPRINTK("clear nexus port:%d "
599 "succeeded\n", port->id);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600600 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800601 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800602 try_to_reset_cmd_device(shost, cmd);
603 sas_scsi_clear_queue_port(work_q,
James Bottomley2908d772006-08-29 09:22:51 -0500604 port);
605 goto Again;
606 }
607 }
608 if (i->dft->lldd_clear_nexus_ha) {
609 SAS_DPRINTK("clear nexus ha\n");
610 res = i->dft->lldd_clear_nexus_ha(ha);
611 if (res == TMF_RESP_FUNC_COMPLETE) {
612 SAS_DPRINTK("clear nexus ha "
613 "succeeded\n");
James Bottomleya8e14fe2008-02-19 21:48:42 -0600614 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800615 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800616 try_to_reset_cmd_device(shost, cmd);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600617 goto clear_q;
James Bottomley2908d772006-08-29 09:22:51 -0500618 }
619 }
620 /* If we are here -- this means that no amount
621 * of effort could recover from errors. Quite
622 * possibly the HA just disappeared.
623 */
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);
628
James Bottomleya8e14fe2008-02-19 21:48:42 -0600629 sas_eh_finish_cmd(cmd);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800630 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800631 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500632 goto clear_q;
633 }
634 }
Darrick J. Wongad689232007-01-26 14:08:52 -0800635 return list_empty(work_q);
James Bottomley2908d772006-08-29 09:22:51 -0500636clear_q:
637 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600638 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
639 sas_eh_finish_cmd(cmd);
640
Darrick J. Wongad689232007-01-26 14:08:52 -0800641 return list_empty(work_q);
642}
643
644void sas_scsi_recover_host(struct Scsi_Host *shost)
645{
646 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
647 unsigned long flags;
648 LIST_HEAD(eh_work_q);
649
650 spin_lock_irqsave(shost->host_lock, flags);
651 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
652 spin_unlock_irqrestore(shost->host_lock, flags);
653
654 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
655 /*
656 * Deal with commands that still have SAS tasks (i.e. they didn't
657 * complete via the normal sas_task completion mechanism)
658 */
659 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
660 goto out;
661
662 /*
663 * Now deal with SCSI commands that completed ok but have a an error
664 * code (and hopefully sense data) attached. This is roughly what
665 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
666 * command we see here has no sas_task and is thus unknown to the HA.
667 */
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);
670
671out:
672 scsi_eh_flush_done_q(&ha->eh_done_q);
673 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
674 return;
James Bottomley2908d772006-08-29 09:22:51 -0500675}
676
677enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
678{
679 struct sas_task *task = TO_SAS_TASK(cmd);
680 unsigned long flags;
681
682 if (!task) {
Darrick J. Wong6d4dcd42007-01-11 14:15:00 -0800683 cmd->timeout_per_command /= 2;
684 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
685 cmd, task, (cmd->timeout_per_command ?
686 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
687 if (!cmd->timeout_per_command)
688 return EH_NOT_HANDLED;
689 return EH_RESET_TIMER;
James Bottomley2908d772006-08-29 09:22:51 -0500690 }
691
692 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800693 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
James Bottomley2908d772006-08-29 09:22:51 -0500694 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
695 spin_unlock_irqrestore(&task->task_state_lock, flags);
696 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
697 cmd, task);
698 return EH_HANDLED;
699 }
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800700 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
701 spin_unlock_irqrestore(&task->task_state_lock, flags);
702 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
703 "EH_RESET_TIMER\n",
704 cmd, task);
705 return EH_RESET_TIMER;
706 }
James Bottomley2908d772006-08-29 09:22:51 -0500707 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
708 spin_unlock_irqrestore(&task->task_state_lock, flags);
709
710 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
711 cmd, task);
712
713 return EH_NOT_HANDLED;
714}
715
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700716int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
717{
718 struct domain_device *dev = sdev_to_domain_dev(sdev);
719
720 if (dev_is_sata(dev))
721 return ata_scsi_ioctl(sdev, cmd, arg);
722
723 return -EINVAL;
724}
725
James Bottomley2908d772006-08-29 09:22:51 -0500726struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
727{
728 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
729 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
730 struct domain_device *found_dev = NULL;
731 int i;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800732 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500733
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800734 spin_lock_irqsave(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500735 for (i = 0; i < ha->num_phys; i++) {
736 struct asd_sas_port *port = ha->sas_port[i];
737 struct domain_device *dev;
738
739 spin_lock(&port->dev_list_lock);
740 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
741 if (rphy == dev->rphy) {
742 found_dev = dev;
743 spin_unlock(&port->dev_list_lock);
744 goto found;
745 }
746 }
747 spin_unlock(&port->dev_list_lock);
748 }
749 found:
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800750 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500751
752 return found_dev;
753}
754
755static inline struct domain_device *sas_find_target(struct scsi_target *starget)
756{
757 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
758
759 return sas_find_dev_by_rphy(rphy);
760}
761
762int sas_target_alloc(struct scsi_target *starget)
763{
764 struct domain_device *found_dev = sas_find_target(starget);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700765 int res;
James Bottomley2908d772006-08-29 09:22:51 -0500766
767 if (!found_dev)
768 return -ENODEV;
769
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700770 if (dev_is_sata(found_dev)) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700771 res = sas_ata_init_host_and_port(found_dev, starget);
772 if (res)
773 return res;
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700774 }
775
James Bottomley2908d772006-08-29 09:22:51 -0500776 starget->hostdata = found_dev;
777 return 0;
778}
779
780#define SAS_DEF_QD 32
781#define SAS_MAX_QD 64
782
783int sas_slave_configure(struct scsi_device *scsi_dev)
784{
785 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
786 struct sas_ha_struct *sas_ha;
787
788 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
789
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700790 if (dev_is_sata(dev)) {
791 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
792 return 0;
793 }
794
James Bottomley2908d772006-08-29 09:22:51 -0500795 sas_ha = dev->port->ha;
796
797 sas_read_port_mode_page(scsi_dev);
798
799 if (scsi_dev->tagged_supported) {
800 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
801 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
802 } else {
803 SAS_DPRINTK("device %llx, LUN %x doesn't support "
804 "TCQ\n", SAS_ADDR(dev->sas_addr),
805 scsi_dev->lun);
806 scsi_dev->tagged_supported = 0;
807 scsi_set_tag_type(scsi_dev, 0);
808 scsi_deactivate_tcq(scsi_dev, 1);
809 }
810
Darrick J. Wongf27708f2007-01-26 14:08:58 -0800811 scsi_dev->allow_restart = 1;
812
James Bottomley2908d772006-08-29 09:22:51 -0500813 return 0;
814}
815
816void sas_slave_destroy(struct scsi_device *scsi_dev)
817{
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700818 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
819
820 if (dev_is_sata(dev))
821 ata_port_disable(dev->sata_dev.ap);
James Bottomley2908d772006-08-29 09:22:51 -0500822}
823
824int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
825{
826 int res = min(new_depth, SAS_MAX_QD);
827
828 if (scsi_dev->tagged_supported)
829 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
830 res);
831 else {
832 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
833 sas_printk("device %llx LUN %x queue depth changed to 1\n",
834 SAS_ADDR(dev->sas_addr),
835 scsi_dev->lun);
836 scsi_adjust_queue_depth(scsi_dev, 0, 1);
837 res = 1;
838 }
839
840 return res;
841}
842
843int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
844{
845 if (!scsi_dev->tagged_supported)
846 return 0;
847
848 scsi_deactivate_tcq(scsi_dev, 1);
849
850 scsi_set_tag_type(scsi_dev, qt);
851 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
852
853 return qt;
854}
855
856int sas_bios_param(struct scsi_device *scsi_dev,
857 struct block_device *bdev,
858 sector_t capacity, int *hsc)
859{
860 hsc[0] = 255;
861 hsc[1] = 63;
862 sector_div(capacity, 255*63);
863 hsc[2] = capacity;
864
865 return 0;
866}
867
868/* ---------- Task Collector Thread implementation ---------- */
869
870static void sas_queue(struct sas_ha_struct *sas_ha)
871{
872 struct scsi_core *core = &sas_ha->core;
873 unsigned long flags;
874 LIST_HEAD(q);
875 int can_queue;
876 int res;
877 struct sas_internal *i = to_sas_internal(core->shost->transportt);
878
879 spin_lock_irqsave(&core->task_queue_lock, flags);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400880 while (!kthread_should_stop() &&
James Bottomley2908d772006-08-29 09:22:51 -0500881 !list_empty(&core->task_queue)) {
882
883 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
884 if (can_queue >= 0) {
885 can_queue = core->task_queue_size;
886 list_splice_init(&core->task_queue, &q);
887 } else {
888 struct list_head *a, *n;
889
890 can_queue = sas_ha->lldd_queue_size;
891 list_for_each_safe(a, n, &core->task_queue) {
892 list_move_tail(a, &q);
893 if (--can_queue == 0)
894 break;
895 }
896 can_queue = sas_ha->lldd_queue_size;
897 }
898 core->task_queue_size -= can_queue;
899 spin_unlock_irqrestore(&core->task_queue_lock, flags);
900 {
901 struct sas_task *task = list_entry(q.next,
902 struct sas_task,
903 list);
904 list_del_init(&q);
905 res = i->dft->lldd_execute_task(task, can_queue,
906 GFP_KERNEL);
907 if (unlikely(res))
908 __list_add(&q, task->list.prev, &task->list);
909 }
910 spin_lock_irqsave(&core->task_queue_lock, flags);
911 if (res) {
912 list_splice_init(&q, &core->task_queue); /*at head*/
913 core->task_queue_size += can_queue;
914 }
915 }
916 spin_unlock_irqrestore(&core->task_queue_lock, flags);
917}
918
James Bottomley2908d772006-08-29 09:22:51 -0500919/**
920 * sas_queue_thread -- The Task Collector thread
921 * @_sas_ha: pointer to struct sas_ha
922 */
923static int sas_queue_thread(void *_sas_ha)
924{
925 struct sas_ha_struct *sas_ha = _sas_ha;
James Bottomley2908d772006-08-29 09:22:51 -0500926
James Bottomley2908d772006-08-29 09:22:51 -0500927 while (1) {
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400928 set_current_state(TASK_INTERRUPTIBLE);
929 schedule();
James Bottomley2908d772006-08-29 09:22:51 -0500930 sas_queue(sas_ha);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400931 if (kthread_should_stop())
James Bottomley2908d772006-08-29 09:22:51 -0500932 break;
933 }
934
James Bottomley2908d772006-08-29 09:22:51 -0500935 return 0;
936}
937
938int sas_init_queue(struct sas_ha_struct *sas_ha)
939{
James Bottomley2908d772006-08-29 09:22:51 -0500940 struct scsi_core *core = &sas_ha->core;
941
942 spin_lock_init(&core->task_queue_lock);
943 core->task_queue_size = 0;
944 INIT_LIST_HEAD(&core->task_queue);
James Bottomley2908d772006-08-29 09:22:51 -0500945
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400946 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
947 "sas_queue_%d", core->shost->host_no);
948 if (IS_ERR(core->queue_thread))
949 return PTR_ERR(core->queue_thread);
950 return 0;
James Bottomley2908d772006-08-29 09:22:51 -0500951}
952
953void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
954{
955 unsigned long flags;
956 struct scsi_core *core = &sas_ha->core;
957 struct sas_task *task, *n;
958
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400959 kthread_stop(core->queue_thread);
James Bottomley2908d772006-08-29 09:22:51 -0500960
961 if (!list_empty(&core->task_queue))
962 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
963 SAS_ADDR(sas_ha->sas_addr));
964
965 spin_lock_irqsave(&core->task_queue_lock, flags);
966 list_for_each_entry_safe(task, n, &core->task_queue, list) {
967 struct scsi_cmnd *cmd = task->uldd_task;
968
969 list_del_init(&task->list);
970
971 ASSIGN_SAS_TASK(cmd, NULL);
972 sas_free_task(task);
973 cmd->result = DID_ABORT << 16;
974 cmd->scsi_done(cmd);
975 }
976 spin_unlock_irqrestore(&core->task_queue_lock, flags);
977}
978
Darrick J. Wong396819f2007-01-11 14:15:20 -0800979/*
980 * Call the LLDD task abort routine directly. This function is intended for
981 * use by upper layers that need to tell the LLDD to abort a task.
982 */
983int __sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800984{
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800985 struct sas_internal *si =
986 to_sas_internal(task->dev->port->ha->core.shost->transportt);
987 unsigned long flags;
988 int res;
989
990 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800991 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
992 task->task_state_flags & SAS_TASK_STATE_DONE) {
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800993 spin_unlock_irqrestore(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800994 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800995 task);
996 return 0;
997 }
Darrick J. Wong396819f2007-01-11 14:15:20 -0800998 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800999 spin_unlock_irqrestore(&task->task_state_lock, flags);
1000
1001 if (!si->dft->lldd_abort_task)
1002 return -ENODEV;
1003
1004 res = si->dft->lldd_abort_task(task);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001005
1006 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001007 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
1008 (res == TMF_RESP_FUNC_COMPLETE))
1009 {
Darrick J. Wong396819f2007-01-11 14:15:20 -08001010 spin_unlock_irqrestore(&task->task_state_lock, flags);
1011 task->task_done(task);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001012 return 0;
1013 }
1014
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001015 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1016 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1017 spin_unlock_irqrestore(&task->task_state_lock, flags);
1018
1019 return -EAGAIN;
1020}
1021
Darrick J. Wong396819f2007-01-11 14:15:20 -08001022/*
1023 * Tell an upper layer that it needs to initiate an abort for a given task.
1024 * This should only ever be called by an LLDD.
1025 */
1026void sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001027{
Darrick J. Wong396819f2007-01-11 14:15:20 -08001028 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001029
Darrick J. Wong396819f2007-01-11 14:15:20 -08001030 /* Escape for libsas internal commands */
1031 if (!sc) {
1032 if (!del_timer(&task->timer))
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001033 return;
Darrick J. Wong396819f2007-01-11 14:15:20 -08001034 task->timer.function(task->timer.data);
1035 return;
1036 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001037
Darrick J. Wong3a2755a2007-01-30 01:18:58 -08001038 if (dev_is_sata(task->dev)) {
1039 sas_ata_task_abort(task);
1040 return;
1041 }
1042
Darrick J. Wong396819f2007-01-11 14:15:20 -08001043 scsi_req_abort_cmd(sc);
1044 scsi_schedule_eh(sc->device->host);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001045}
1046
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001047int sas_slave_alloc(struct scsi_device *scsi_dev)
1048{
1049 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1050
1051 if (dev_is_sata(dev))
1052 return ata_sas_port_init(dev->sata_dev.ap);
1053
1054 return 0;
1055}
1056
1057void sas_target_destroy(struct scsi_target *starget)
1058{
1059 struct domain_device *found_dev = sas_find_target(starget);
1060
1061 if (!found_dev)
1062 return;
1063
1064 if (dev_is_sata(found_dev))
1065 ata_sas_port_destroy(found_dev->sata_dev.ap);
1066
1067 return;
1068}
1069
James Bottomley2908d772006-08-29 09:22:51 -05001070EXPORT_SYMBOL_GPL(sas_queuecommand);
1071EXPORT_SYMBOL_GPL(sas_target_alloc);
1072EXPORT_SYMBOL_GPL(sas_slave_configure);
1073EXPORT_SYMBOL_GPL(sas_slave_destroy);
1074EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1075EXPORT_SYMBOL_GPL(sas_change_queue_type);
1076EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001077EXPORT_SYMBOL_GPL(__sas_task_abort);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001078EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -08001079EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001080EXPORT_SYMBOL_GPL(sas_phy_enable);
Darrick J. Wongad689232007-01-26 14:08:52 -08001081EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
Darrick J. Wonga9344e62007-01-29 23:48:19 -08001082EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001083EXPORT_SYMBOL_GPL(sas_slave_alloc);
1084EXPORT_SYMBOL_GPL(sas_target_destroy);
1085EXPORT_SYMBOL_GPL(sas_ioctl);