blob: 55f09e92ab5918fd5019cacaded186d2cd1b1c55 [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>
Darrick J. Wong45e6cdf2008-02-19 10:49:40 -080027#include <linux/firmware.h>
28#include <linux/ctype.h>
Christoph Hellwigd7a54e32007-04-26 09:38:01 -040029
James Bottomley2908d772006-08-29 09:22:51 -050030#include "sas_internal.h"
31
32#include <scsi/scsi_host.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi.h>
Darrick J. Wongf4563932006-10-30 15:18:39 -080036#include <scsi/scsi_eh.h>
James Bottomley2908d772006-08-29 09:22:51 -050037#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_sas.h>
Darrick J. Wong338ec572006-10-18 14:43:37 -070039#include <scsi/sas_ata.h>
James Bottomley2908d772006-08-29 09:22:51 -050040#include "../scsi_sas_internal.h"
Darrick J. Wong79a5eb62006-10-30 15:18:50 -080041#include "../scsi_transport_api.h"
Darrick J. Wongad689232007-01-26 14:08:52 -080042#include "../scsi_priv.h"
James Bottomley2908d772006-08-29 09:22:51 -050043
44#include <linux/err.h>
45#include <linux/blkdev.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070046#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/gfp.h>
James Bottomley2908d772006-08-29 09:22:51 -050048#include <linux/scatterlist.h>
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -070049#include <linux/libata.h>
James Bottomley2908d772006-08-29 09:22:51 -050050
51/* ---------- SCSI Host glue ---------- */
52
James Bottomley2908d772006-08-29 09:22:51 -050053static void sas_scsi_task_done(struct sas_task *task)
54{
55 struct task_status_struct *ts = &task->task_status;
56 struct scsi_cmnd *sc = task->uldd_task;
James Bottomley2908d772006-08-29 09:22:51 -050057 int hs = 0, stat = 0;
58
James Bottomleya8e14fe2008-02-19 21:48:42 -060059 if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
60 /* Aborted tasks will be completed by the error handler */
61 SAS_DPRINTK("task done but aborted\n");
62 return;
63 }
64
James Bottomley2908d772006-08-29 09:22:51 -050065 if (unlikely(!sc)) {
66 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
67 list_del_init(&task->list);
68 sas_free_task(task);
69 return;
70 }
71
72 if (ts->resp == SAS_TASK_UNDELIVERED) {
73 /* transport error */
74 hs = DID_NO_CONNECT;
75 } else { /* ts->resp == SAS_TASK_COMPLETE */
76 /* task delivered, what happened afterwards? */
77 switch (ts->stat) {
78 case SAS_DEV_NO_RESPONSE:
79 case SAS_INTERRUPTED:
80 case SAS_PHY_DOWN:
81 case SAS_NAK_R_ERR:
82 case SAS_OPEN_TO:
83 hs = DID_NO_CONNECT;
84 break;
85 case SAS_DATA_UNDERRUN:
FUJITA Tomonoric13e5562007-05-26 02:46:37 +090086 scsi_set_resid(sc, ts->residual);
87 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
James Bottomley2908d772006-08-29 09:22:51 -050088 hs = DID_ERROR;
89 break;
90 case SAS_DATA_OVERRUN:
91 hs = DID_ERROR;
92 break;
93 case SAS_QUEUE_FULL:
94 hs = DID_SOFT_ERROR; /* retry */
95 break;
96 case SAS_DEVICE_UNKNOWN:
97 hs = DID_BAD_TARGET;
98 break;
99 case SAS_SG_ERR:
100 hs = DID_PARITY;
101 break;
102 case SAS_OPEN_REJECT:
103 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
104 hs = DID_SOFT_ERROR; /* retry */
105 else
106 hs = DID_ERROR;
107 break;
108 case SAS_PROTO_RESPONSE:
109 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
110 "task; please report this\n",
111 task->dev->port->ha->sas_ha_name);
112 break;
113 case SAS_ABORTED_TASK:
114 hs = DID_ABORT;
115 break;
James Bottomleydf64d3c2010-07-27 15:51:13 -0500116 case SAM_STAT_CHECK_CONDITION:
James Bottomley2908d772006-08-29 09:22:51 -0500117 memcpy(sc->sense_buffer, ts->buf,
FUJITA Tomonoricc75e8a2008-01-13 02:20:18 +0900118 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
James Bottomleydf64d3c2010-07-27 15:51:13 -0500119 stat = SAM_STAT_CHECK_CONDITION;
James Bottomley2908d772006-08-29 09:22:51 -0500120 break;
121 default:
122 stat = ts->stat;
123 break;
124 }
125 }
126 ASSIGN_SAS_TASK(sc, NULL);
127 sc->result = (hs << 16) | stat;
128 list_del_init(&task->list);
129 sas_free_task(task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600130 sc->scsi_done(sc);
James Bottomley2908d772006-08-29 09:22:51 -0500131}
132
James Bottomley2908d772006-08-29 09:22:51 -0500133static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
134 struct domain_device *dev,
Al Viro3cc27542006-09-25 02:55:40 +0100135 gfp_t gfp_flags)
James Bottomley2908d772006-08-29 09:22:51 -0500136{
137 struct sas_task *task = sas_alloc_task(gfp_flags);
138 struct scsi_lun lun;
139
140 if (!task)
141 return NULL;
142
James Bottomley2908d772006-08-29 09:22:51 -0500143 task->uldd_task = cmd;
144 ASSIGN_SAS_TASK(cmd, task);
145
146 task->dev = dev;
147 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
148
149 task->ssp_task.retry_count = 1;
150 int_to_scsilun(cmd->device->lun, &lun);
151 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
Tejun Heo9cbbdca2010-09-03 11:56:16 +0200152 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
James Bottomley2908d772006-08-29 09:22:51 -0500153 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
154
FUJITA Tomonoric13e5562007-05-26 02:46:37 +0900155 task->scatter = scsi_sglist(cmd);
156 task->num_scatter = scsi_sg_count(cmd);
157 task->total_xfer_len = scsi_bufflen(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500158 task->data_dir = cmd->sc_data_direction;
159
160 task->task_done = sas_scsi_task_done;
161
162 return task;
163}
164
Darrick J. Wong338ec572006-10-18 14:43:37 -0700165int sas_queue_up(struct sas_task *task)
James Bottomley2908d772006-08-29 09:22:51 -0500166{
167 struct sas_ha_struct *sas_ha = task->dev->port->ha;
168 struct scsi_core *core = &sas_ha->core;
169 unsigned long flags;
170 LIST_HEAD(list);
171
172 spin_lock_irqsave(&core->task_queue_lock, flags);
173 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
174 spin_unlock_irqrestore(&core->task_queue_lock, flags);
175 return -SAS_QUEUE_FULL;
176 }
177 list_add_tail(&task->list, &core->task_queue);
178 core->task_queue_size += 1;
179 spin_unlock_irqrestore(&core->task_queue_lock, flags);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400180 wake_up_process(core->queue_thread);
James Bottomley2908d772006-08-29 09:22:51 -0500181
182 return 0;
183}
184
185/**
186 * sas_queuecommand -- Enqueue a command for processing
187 * @parameters: See SCSI Core documentation
188 *
189 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
190 * call us without holding an IRQ spinlock...
191 */
192int sas_queuecommand(struct scsi_cmnd *cmd,
193 void (*scsi_done)(struct scsi_cmnd *))
Darrick J. Wong8ee24022007-11-05 11:52:14 -0800194 __releases(host->host_lock)
195 __acquires(dev->sata_dev.ap->lock)
196 __releases(dev->sata_dev.ap->lock)
197 __acquires(host->host_lock)
James Bottomley2908d772006-08-29 09:22:51 -0500198{
199 int res = 0;
200 struct domain_device *dev = cmd_to_domain_dev(cmd);
201 struct Scsi_Host *host = cmd->device->host;
202 struct sas_internal *i = to_sas_internal(host->transportt);
203
204 spin_unlock_irq(host->host_lock);
205
206 {
207 struct sas_ha_struct *sas_ha = dev->port->ha;
208 struct sas_task *task;
209
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700210 if (dev_is_sata(dev)) {
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800211 unsigned long flags;
212
213 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700214 res = ata_sas_queuecmd(cmd, scsi_done,
215 dev->sata_dev.ap);
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800216 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700217 goto out;
218 }
219
Darrick J. Wong56dd2c02010-10-01 13:55:47 -0700220 /* If the device fell off, no sense in issuing commands */
221 if (dev->gone) {
222 cmd->result = DID_BAD_TARGET << 16;
223 scsi_done(cmd);
224 goto out;
225 }
226
James Bottomley2908d772006-08-29 09:22:51 -0500227 res = -ENOMEM;
228 task = sas_create_task(cmd, dev, GFP_ATOMIC);
229 if (!task)
230 goto out;
231
232 cmd->scsi_done = scsi_done;
233 /* Queue up, Direct Mode or Task Collector Mode. */
234 if (sas_ha->lldd_max_execute_num < 2)
235 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
236 else
237 res = sas_queue_up(task);
238
239 /* Examine */
240 if (res) {
241 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
242 ASSIGN_SAS_TASK(cmd, NULL);
243 sas_free_task(task);
244 if (res == -SAS_QUEUE_FULL) {
245 cmd->result = DID_SOFT_ERROR << 16; /* retry */
246 res = 0;
247 scsi_done(cmd);
248 }
249 goto out;
250 }
251 }
252out:
253 spin_lock_irq(host->host_lock);
254 return res;
255}
256
James Bottomleya8e14fe2008-02-19 21:48:42 -0600257static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
258{
259 struct sas_task *task = TO_SAS_TASK(cmd);
260 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
261
262 /* remove the aborted task flag to allow the task to be
263 * completed now. At this point, we only get called following
264 * an actual abort of the task, so we should be guaranteed not
265 * to be racing with any completions from the LLD (hence we
266 * don't need the task state lock to clear the flag) */
267 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
268 /* Now call task_done. However, task will be free'd after
269 * this */
270 task->task_done(task);
271 /* now finish the command and move it on to the error
272 * handler done list, this also takes it off the
273 * error handler pending list */
274 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
275}
276
James Bottomley2908d772006-08-29 09:22:51 -0500277static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
278{
279 struct scsi_cmnd *cmd, *n;
280
281 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
James Bottomley63e45632008-02-22 17:07:52 -0600282 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
283 cmd->device->lun == my_cmd->device->lun)
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",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700343 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500344 return TASK_IS_ABORTED;
345 }
346 }
347 spin_unlock_irqrestore(&core->task_queue_lock, flags);
348 }
349
350 for (i = 0; i < 5; i++) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700351 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500352 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);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700357 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -0500358 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",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700365 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500366 return TASK_IS_ABORTED;
367 } else if (si->dft->lldd_query_task) {
368 SAS_DPRINTK("%s: querying task 0x%p\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700369 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500370 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",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700374 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500375 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",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700378 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500379 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",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700382 __func__, task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800383 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 */
James Bottomley53195782008-02-23 23:35:44 -0600436struct sas_phy *sas_find_local_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}
James Bottomley53195782008-02-23 23:35:44 -0600458EXPORT_SYMBOL_GPL(sas_find_local_phy);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800459
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);
James Bottomley53195782008-02-23 23:35:44 -0600485 struct sas_phy *phy = sas_find_local_phy(dev);
Darrick J. Wongad689232007-01-26 14:08:52 -0800486 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 */
James Bottomley8de3ef22008-02-23 23:39:59 -0600500static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
Darrick J. Wongad689232007-01-26 14:08:52 -0800501{
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800502 int res;
James Bottomley8de3ef22008-02-23 23:39:59 -0600503 struct Scsi_Host *shost = cmd->device->host;
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
James Bottomley8de3ef22008-02-23 23:39:59 -0600543 if (need_reset) {
544 SAS_DPRINTK("%s: task 0x%p requests reset\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700545 __func__, task);
James Bottomley8de3ef22008-02-23 23:39:59 -0600546 goto reset;
547 }
548
Darrick J. Wongf4563932006-10-30 15:18:39 -0800549 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500550 res = sas_scsi_find_task(task);
551
552 cmd->eh_eflags = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500553
554 switch (res) {
555 case TASK_IS_DONE:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700556 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -0500557 task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600558 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500559 continue;
560 case TASK_IS_ABORTED:
561 SAS_DPRINTK("%s: task 0x%p is aborted\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700562 __func__, task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600563 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500564 continue;
565 case TASK_IS_AT_LU:
566 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
James Bottomley8de3ef22008-02-23 23:39:59 -0600567 reset:
James Bottomley2908d772006-08-29 09:22:51 -0500568 tmf_resp = sas_recover_lu(task->dev, cmd);
569 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
570 SAS_DPRINTK("dev %016llx LU %x is "
571 "recovered\n",
572 SAS_ADDR(task->dev),
573 cmd->device->lun);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600574 sas_eh_finish_cmd(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800575 sas_scsi_clear_queue_lu(work_q, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500576 goto Again;
577 }
578 /* fallthrough */
579 case TASK_IS_NOT_AT_LU:
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800580 case TASK_ABORT_FAILED:
James Bottomley2908d772006-08-29 09:22:51 -0500581 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
582 task);
583 tmf_resp = sas_recover_I_T(task->dev);
584 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
James Bottomley8de3ef22008-02-23 23:39:59 -0600585 struct domain_device *dev = task->dev;
James Bottomley2908d772006-08-29 09:22:51 -0500586 SAS_DPRINTK("I_T %016llx recovered\n",
587 SAS_ADDR(task->dev->sas_addr));
James Bottomleya8e14fe2008-02-19 21:48:42 -0600588 sas_eh_finish_cmd(cmd);
James Bottomley8de3ef22008-02-23 23:39:59 -0600589 sas_scsi_clear_queue_I_T(work_q, dev);
James Bottomley2908d772006-08-29 09:22:51 -0500590 goto Again;
591 }
592 /* Hammer time :-) */
James Bottomley8de3ef22008-02-23 23:39:59 -0600593 try_to_reset_cmd_device(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500594 if (i->dft->lldd_clear_nexus_port) {
595 struct asd_sas_port *port = task->dev->port;
596 SAS_DPRINTK("clearing nexus for port:%d\n",
597 port->id);
598 res = i->dft->lldd_clear_nexus_port(port);
599 if (res == TMF_RESP_FUNC_COMPLETE) {
600 SAS_DPRINTK("clear nexus port:%d "
601 "succeeded\n", port->id);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600602 sas_eh_finish_cmd(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800603 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);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600615 goto clear_q;
James Bottomley2908d772006-08-29 09:22:51 -0500616 }
617 }
618 /* If we are here -- this means that no amount
619 * of effort could recover from errors. Quite
620 * possibly the HA just disappeared.
621 */
622 SAS_DPRINTK("error from device %llx, LUN %x "
623 "couldn't be recovered in any way\n",
624 SAS_ADDR(task->dev->sas_addr),
625 cmd->device->lun);
626
James Bottomleya8e14fe2008-02-19 21:48:42 -0600627 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500628 goto clear_q;
629 }
630 }
Darrick J. Wongad689232007-01-26 14:08:52 -0800631 return list_empty(work_q);
James Bottomley2908d772006-08-29 09:22:51 -0500632clear_q:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700633 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600634 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
635 sas_eh_finish_cmd(cmd);
636
Darrick J. Wongad689232007-01-26 14:08:52 -0800637 return list_empty(work_q);
638}
639
640void sas_scsi_recover_host(struct Scsi_Host *shost)
641{
642 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
643 unsigned long flags;
644 LIST_HEAD(eh_work_q);
645
646 spin_lock_irqsave(shost->host_lock, flags);
647 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
648 spin_unlock_irqrestore(shost->host_lock, flags);
649
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700650 SAS_DPRINTK("Enter %s\n", __func__);
Darrick J. Wongad689232007-01-26 14:08:52 -0800651 /*
652 * Deal with commands that still have SAS tasks (i.e. they didn't
653 * complete via the normal sas_task completion mechanism)
654 */
655 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
656 goto out;
657
658 /*
659 * Now deal with SCSI commands that completed ok but have a an error
660 * code (and hopefully sense data) attached. This is roughly what
661 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
662 * command we see here has no sas_task and is thus unknown to the HA.
663 */
664 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
665 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
666
667out:
668 scsi_eh_flush_done_q(&ha->eh_done_q);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700669 SAS_DPRINTK("--- Exit %s\n", __func__);
Darrick J. Wongad689232007-01-26 14:08:52 -0800670 return;
James Bottomley2908d772006-08-29 09:22:51 -0500671}
672
Jens Axboe242f9dc2008-09-14 05:55:09 -0700673enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500674{
675 struct sas_task *task = TO_SAS_TASK(cmd);
676 unsigned long flags;
677
678 if (!task) {
Jens Axboe242f9dc2008-09-14 05:55:09 -0700679 cmd->request->timeout /= 2;
Darrick J. Wong6d4dcd42007-01-11 14:15:00 -0800680 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -0700681 cmd, task, (cmd->request->timeout ?
682 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
683 if (!cmd->request->timeout)
684 return BLK_EH_NOT_HANDLED;
685 return BLK_EH_RESET_TIMER;
James Bottomley2908d772006-08-29 09:22:51 -0500686 }
687
688 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800689 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
James Bottomley2908d772006-08-29 09:22:51 -0500690 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
691 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700692 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
693 "BLK_EH_HANDLED\n", cmd, task);
694 return BLK_EH_HANDLED;
James Bottomley2908d772006-08-29 09:22:51 -0500695 }
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800696 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
697 spin_unlock_irqrestore(&task->task_state_lock, flags);
698 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
Jens Axboe242f9dc2008-09-14 05:55:09 -0700699 "BLK_EH_RESET_TIMER\n",
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800700 cmd, task);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700701 return BLK_EH_RESET_TIMER;
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800702 }
James Bottomley2908d772006-08-29 09:22:51 -0500703 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
704 spin_unlock_irqrestore(&task->task_state_lock, flags);
705
Jens Axboe242f9dc2008-09-14 05:55:09 -0700706 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
James Bottomley2908d772006-08-29 09:22:51 -0500707 cmd, task);
708
Jens Axboe242f9dc2008-09-14 05:55:09 -0700709 return BLK_EH_NOT_HANDLED;
James Bottomley2908d772006-08-29 09:22:51 -0500710}
711
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700712int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
713{
714 struct domain_device *dev = sdev_to_domain_dev(sdev);
715
716 if (dev_is_sata(dev))
Jeff Garzik94be9a52009-01-16 10:17:09 -0500717 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700718
719 return -EINVAL;
720}
721
James Bottomley2908d772006-08-29 09:22:51 -0500722struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
723{
724 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
725 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
726 struct domain_device *found_dev = NULL;
727 int i;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800728 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500729
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800730 spin_lock_irqsave(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500731 for (i = 0; i < ha->num_phys; i++) {
732 struct asd_sas_port *port = ha->sas_port[i];
733 struct domain_device *dev;
734
735 spin_lock(&port->dev_list_lock);
736 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
737 if (rphy == dev->rphy) {
738 found_dev = dev;
739 spin_unlock(&port->dev_list_lock);
740 goto found;
741 }
742 }
743 spin_unlock(&port->dev_list_lock);
744 }
745 found:
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800746 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500747
748 return found_dev;
749}
750
751static inline struct domain_device *sas_find_target(struct scsi_target *starget)
752{
753 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
754
755 return sas_find_dev_by_rphy(rphy);
756}
757
758int sas_target_alloc(struct scsi_target *starget)
759{
760 struct domain_device *found_dev = sas_find_target(starget);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700761 int res;
James Bottomley2908d772006-08-29 09:22:51 -0500762
763 if (!found_dev)
764 return -ENODEV;
765
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700766 if (dev_is_sata(found_dev)) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700767 res = sas_ata_init_host_and_port(found_dev, starget);
768 if (res)
769 return res;
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700770 }
771
James Bottomley2908d772006-08-29 09:22:51 -0500772 starget->hostdata = found_dev;
773 return 0;
774}
775
776#define SAS_DEF_QD 32
777#define SAS_MAX_QD 64
778
779int sas_slave_configure(struct scsi_device *scsi_dev)
780{
781 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
782 struct sas_ha_struct *sas_ha;
783
784 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
785
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700786 if (dev_is_sata(dev)) {
787 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
788 return 0;
789 }
790
James Bottomley2908d772006-08-29 09:22:51 -0500791 sas_ha = dev->port->ha;
792
793 sas_read_port_mode_page(scsi_dev);
794
795 if (scsi_dev->tagged_supported) {
796 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
797 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
798 } else {
799 SAS_DPRINTK("device %llx, LUN %x doesn't support "
800 "TCQ\n", SAS_ADDR(dev->sas_addr),
801 scsi_dev->lun);
802 scsi_dev->tagged_supported = 0;
803 scsi_set_tag_type(scsi_dev, 0);
804 scsi_deactivate_tcq(scsi_dev, 1);
805 }
806
Darrick J. Wongf27708f2007-01-26 14:08:58 -0800807 scsi_dev->allow_restart = 1;
808
James Bottomley2908d772006-08-29 09:22:51 -0500809 return 0;
810}
811
812void sas_slave_destroy(struct scsi_device *scsi_dev)
813{
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700814 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
815
816 if (dev_is_sata(dev))
Tejun Heo3e4ec342010-05-10 21:41:30 +0200817 dev->sata_dev.ap->link.device[0].class = ATA_DEV_NONE;
James Bottomley2908d772006-08-29 09:22:51 -0500818}
819
Mike Christiee881a172009-10-15 17:46:39 -0700820int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth,
821 int reason)
James Bottomley2908d772006-08-29 09:22:51 -0500822{
823 int res = min(new_depth, SAS_MAX_QD);
824
Mike Christiee881a172009-10-15 17:46:39 -0700825 if (reason != SCSI_QDEPTH_DEFAULT)
826 return -EOPNOTSUPP;
827
James Bottomley2908d772006-08-29 09:22:51 -0500828 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);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700994 SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
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);
James Bottomley1b4d0d82010-05-13 09:31:54 -05001040 } else {
1041 struct request_queue *q = sc->device->request_queue;
1042 unsigned long flags;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -08001043
James Bottomley1b4d0d82010-05-13 09:31:54 -05001044 spin_lock_irqsave(q->queue_lock, flags);
1045 blk_abort_request(sc->request);
1046 spin_unlock_irqrestore(q->queue_lock, flags);
1047 scsi_schedule_eh(sc->device->host);
1048 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001049}
1050
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001051int sas_slave_alloc(struct scsi_device *scsi_dev)
1052{
1053 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1054
1055 if (dev_is_sata(dev))
1056 return ata_sas_port_init(dev->sata_dev.ap);
1057
1058 return 0;
1059}
1060
1061void sas_target_destroy(struct scsi_target *starget)
1062{
1063 struct domain_device *found_dev = sas_find_target(starget);
1064
1065 if (!found_dev)
1066 return;
1067
1068 if (dev_is_sata(found_dev))
1069 ata_sas_port_destroy(found_dev->sata_dev.ap);
1070
1071 return;
1072}
1073
Darrick J. Wong45e6cdf2008-02-19 10:49:40 -08001074static void sas_parse_addr(u8 *sas_addr, const char *p)
1075{
1076 int i;
1077 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1078 u8 h, l;
1079 if (!*p)
1080 break;
1081 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1082 p++;
1083 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1084 p++;
1085 sas_addr[i] = (h<<4) | l;
1086 }
1087}
1088
1089#define SAS_STRING_ADDR_SIZE 16
1090
1091int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1092{
1093 int res;
1094 const struct firmware *fw;
1095
1096 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1097 if (res)
1098 return res;
1099
1100 if (fw->size < SAS_STRING_ADDR_SIZE) {
1101 res = -ENODEV;
1102 goto out;
1103 }
1104
1105 sas_parse_addr(addr, fw->data);
1106
1107out:
1108 release_firmware(fw);
1109 return res;
1110}
1111EXPORT_SYMBOL_GPL(sas_request_addr);
1112
James Bottomley2908d772006-08-29 09:22:51 -05001113EXPORT_SYMBOL_GPL(sas_queuecommand);
1114EXPORT_SYMBOL_GPL(sas_target_alloc);
1115EXPORT_SYMBOL_GPL(sas_slave_configure);
1116EXPORT_SYMBOL_GPL(sas_slave_destroy);
1117EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1118EXPORT_SYMBOL_GPL(sas_change_queue_type);
1119EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001120EXPORT_SYMBOL_GPL(__sas_task_abort);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001121EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -08001122EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001123EXPORT_SYMBOL_GPL(sas_phy_enable);
Darrick J. Wongad689232007-01-26 14:08:52 -08001124EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
Darrick J. Wonga9344e62007-01-29 23:48:19 -08001125EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001126EXPORT_SYMBOL_GPL(sas_slave_alloc);
1127EXPORT_SYMBOL_GPL(sas_target_destroy);
1128EXPORT_SYMBOL_GPL(sas_ioctl);