blob: 1787bd2eb4a6a7b95645e1bae06b7fc73be18c26 [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
133static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
134{
135 enum task_attribute ta = TASK_ATTR_SIMPLE;
136 if (cmd->request && blk_rq_tagged(cmd->request)) {
137 if (cmd->device->ordered_tags &&
Jeff Garzik1bdfd552006-09-30 21:28:22 -0400138 (cmd->request->cmd_flags & REQ_HARDBARRIER))
FUJITA Tomonori63bb1bf2007-01-28 01:19:41 +0900139 ta = TASK_ATTR_ORDERED;
James Bottomley2908d772006-08-29 09:22:51 -0500140 }
141 return ta;
142}
143
144static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
145 struct domain_device *dev,
Al Viro3cc27542006-09-25 02:55:40 +0100146 gfp_t gfp_flags)
James Bottomley2908d772006-08-29 09:22:51 -0500147{
148 struct sas_task *task = sas_alloc_task(gfp_flags);
149 struct scsi_lun lun;
150
151 if (!task)
152 return NULL;
153
James Bottomley2908d772006-08-29 09:22:51 -0500154 task->uldd_task = cmd;
155 ASSIGN_SAS_TASK(cmd, task);
156
157 task->dev = dev;
158 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
159
160 task->ssp_task.retry_count = 1;
161 int_to_scsilun(cmd->device->lun, &lun);
162 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
163 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
164 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
165
FUJITA Tomonoric13e5562007-05-26 02:46:37 +0900166 task->scatter = scsi_sglist(cmd);
167 task->num_scatter = scsi_sg_count(cmd);
168 task->total_xfer_len = scsi_bufflen(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500169 task->data_dir = cmd->sc_data_direction;
170
171 task->task_done = sas_scsi_task_done;
172
173 return task;
174}
175
Darrick J. Wong338ec572006-10-18 14:43:37 -0700176int sas_queue_up(struct sas_task *task)
James Bottomley2908d772006-08-29 09:22:51 -0500177{
178 struct sas_ha_struct *sas_ha = task->dev->port->ha;
179 struct scsi_core *core = &sas_ha->core;
180 unsigned long flags;
181 LIST_HEAD(list);
182
183 spin_lock_irqsave(&core->task_queue_lock, flags);
184 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
185 spin_unlock_irqrestore(&core->task_queue_lock, flags);
186 return -SAS_QUEUE_FULL;
187 }
188 list_add_tail(&task->list, &core->task_queue);
189 core->task_queue_size += 1;
190 spin_unlock_irqrestore(&core->task_queue_lock, flags);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400191 wake_up_process(core->queue_thread);
James Bottomley2908d772006-08-29 09:22:51 -0500192
193 return 0;
194}
195
196/**
197 * sas_queuecommand -- Enqueue a command for processing
198 * @parameters: See SCSI Core documentation
199 *
200 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
201 * call us without holding an IRQ spinlock...
202 */
203int sas_queuecommand(struct scsi_cmnd *cmd,
204 void (*scsi_done)(struct scsi_cmnd *))
Darrick J. Wong8ee24022007-11-05 11:52:14 -0800205 __releases(host->host_lock)
206 __acquires(dev->sata_dev.ap->lock)
207 __releases(dev->sata_dev.ap->lock)
208 __acquires(host->host_lock)
James Bottomley2908d772006-08-29 09:22:51 -0500209{
210 int res = 0;
211 struct domain_device *dev = cmd_to_domain_dev(cmd);
212 struct Scsi_Host *host = cmd->device->host;
213 struct sas_internal *i = to_sas_internal(host->transportt);
214
215 spin_unlock_irq(host->host_lock);
216
217 {
218 struct sas_ha_struct *sas_ha = dev->port->ha;
219 struct sas_task *task;
220
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700221 if (dev_is_sata(dev)) {
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800222 unsigned long flags;
223
224 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700225 res = ata_sas_queuecmd(cmd, scsi_done,
226 dev->sata_dev.ap);
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800227 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700228 goto out;
229 }
230
Darrick J. Wong56dd2c02010-10-01 13:55:47 -0700231 /* If the device fell off, no sense in issuing commands */
232 if (dev->gone) {
233 cmd->result = DID_BAD_TARGET << 16;
234 scsi_done(cmd);
235 goto out;
236 }
237
James Bottomley2908d772006-08-29 09:22:51 -0500238 res = -ENOMEM;
239 task = sas_create_task(cmd, dev, GFP_ATOMIC);
240 if (!task)
241 goto out;
242
243 cmd->scsi_done = scsi_done;
244 /* Queue up, Direct Mode or Task Collector Mode. */
245 if (sas_ha->lldd_max_execute_num < 2)
246 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
247 else
248 res = sas_queue_up(task);
249
250 /* Examine */
251 if (res) {
252 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
253 ASSIGN_SAS_TASK(cmd, NULL);
254 sas_free_task(task);
255 if (res == -SAS_QUEUE_FULL) {
256 cmd->result = DID_SOFT_ERROR << 16; /* retry */
257 res = 0;
258 scsi_done(cmd);
259 }
260 goto out;
261 }
262 }
263out:
264 spin_lock_irq(host->host_lock);
265 return res;
266}
267
James Bottomleya8e14fe2008-02-19 21:48:42 -0600268static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
269{
270 struct sas_task *task = TO_SAS_TASK(cmd);
271 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
272
273 /* remove the aborted task flag to allow the task to be
274 * completed now. At this point, we only get called following
275 * an actual abort of the task, so we should be guaranteed not
276 * to be racing with any completions from the LLD (hence we
277 * don't need the task state lock to clear the flag) */
278 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
279 /* Now call task_done. However, task will be free'd after
280 * this */
281 task->task_done(task);
282 /* now finish the command and move it on to the error
283 * handler done list, this also takes it off the
284 * error handler pending list */
285 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
286}
287
James Bottomley2908d772006-08-29 09:22:51 -0500288static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
289{
290 struct scsi_cmnd *cmd, *n;
291
292 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
James Bottomley63e45632008-02-22 17:07:52 -0600293 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
294 cmd->device->lun == my_cmd->device->lun)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600295 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500296 }
297}
298
299static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
300 struct domain_device *dev)
301{
302 struct scsi_cmnd *cmd, *n;
303
304 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
305 struct domain_device *x = cmd_to_domain_dev(cmd);
306
307 if (x == dev)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600308 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500309 }
310}
311
312static void sas_scsi_clear_queue_port(struct list_head *error_q,
313 struct asd_sas_port *port)
314{
315 struct scsi_cmnd *cmd, *n;
316
317 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
318 struct domain_device *dev = cmd_to_domain_dev(cmd);
319 struct asd_sas_port *x = dev->port;
320
321 if (x == port)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600322 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500323 }
324}
325
326enum task_disposition {
327 TASK_IS_DONE,
328 TASK_IS_ABORTED,
329 TASK_IS_AT_LU,
330 TASK_IS_NOT_AT_LU,
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800331 TASK_ABORT_FAILED,
James Bottomley2908d772006-08-29 09:22:51 -0500332};
333
334static enum task_disposition sas_scsi_find_task(struct sas_task *task)
335{
336 struct sas_ha_struct *ha = task->dev->port->ha;
337 unsigned long flags;
338 int i, res;
339 struct sas_internal *si =
340 to_sas_internal(task->dev->port->ha->core.shost->transportt);
341
342 if (ha->lldd_max_execute_num > 1) {
343 struct scsi_core *core = &ha->core;
344 struct sas_task *t, *n;
345
346 spin_lock_irqsave(&core->task_queue_lock, flags);
347 list_for_each_entry_safe(t, n, &core->task_queue, list) {
348 if (task == t) {
349 list_del_init(&t->list);
350 spin_unlock_irqrestore(&core->task_queue_lock,
351 flags);
352 SAS_DPRINTK("%s: task 0x%p aborted from "
353 "task_queue\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700354 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500355 return TASK_IS_ABORTED;
356 }
357 }
358 spin_unlock_irqrestore(&core->task_queue_lock, flags);
359 }
360
361 for (i = 0; i < 5; i++) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700362 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500363 res = si->dft->lldd_abort_task(task);
364
365 spin_lock_irqsave(&task->task_state_lock, flags);
366 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
367 spin_unlock_irqrestore(&task->task_state_lock, flags);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700368 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -0500369 task);
370 return TASK_IS_DONE;
371 }
372 spin_unlock_irqrestore(&task->task_state_lock, flags);
373
374 if (res == TMF_RESP_FUNC_COMPLETE) {
375 SAS_DPRINTK("%s: task 0x%p is aborted\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700376 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500377 return TASK_IS_ABORTED;
378 } else if (si->dft->lldd_query_task) {
379 SAS_DPRINTK("%s: querying task 0x%p\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700380 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500381 res = si->dft->lldd_query_task(task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800382 switch (res) {
383 case TMF_RESP_FUNC_SUCC:
James Bottomley2908d772006-08-29 09:22:51 -0500384 SAS_DPRINTK("%s: task 0x%p at LU\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700385 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500386 return TASK_IS_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800387 case TMF_RESP_FUNC_COMPLETE:
James Bottomley2908d772006-08-29 09:22:51 -0500388 SAS_DPRINTK("%s: task 0x%p not at LU\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700389 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500390 return TASK_IS_NOT_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800391 case TMF_RESP_FUNC_FAILED:
392 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700393 __func__, task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800394 return TASK_ABORT_FAILED;
395 }
396
James Bottomley2908d772006-08-29 09:22:51 -0500397 }
398 }
399 return res;
400}
401
402static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
403{
404 int res = TMF_RESP_FUNC_FAILED;
405 struct scsi_lun lun;
406 struct sas_internal *i =
407 to_sas_internal(dev->port->ha->core.shost->transportt);
408
409 int_to_scsilun(cmd->device->lun, &lun);
410
411 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
412 SAS_ADDR(dev->sas_addr),
413 cmd->device->lun);
414
415 if (i->dft->lldd_abort_task_set)
416 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
417
418 if (res == TMF_RESP_FUNC_FAILED) {
419 if (i->dft->lldd_clear_task_set)
420 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
421 }
422
423 if (res == TMF_RESP_FUNC_FAILED) {
424 if (i->dft->lldd_lu_reset)
425 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
426 }
427
428 return res;
429}
430
431static int sas_recover_I_T(struct domain_device *dev)
432{
433 int res = TMF_RESP_FUNC_FAILED;
434 struct sas_internal *i =
435 to_sas_internal(dev->port->ha->core.shost->transportt);
436
437 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
438 SAS_ADDR(dev->sas_addr));
439
440 if (i->dft->lldd_I_T_nexus_reset)
441 res = i->dft->lldd_I_T_nexus_reset(dev);
442
443 return res;
444}
445
Darrick J. Wongad689232007-01-26 14:08:52 -0800446/* Find the sas_phy that's attached to this device */
James Bottomley53195782008-02-23 23:35:44 -0600447struct sas_phy *sas_find_local_phy(struct domain_device *dev)
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800448{
Darrick J. Wongad689232007-01-26 14:08:52 -0800449 struct domain_device *pdev = dev->parent;
450 struct ex_phy *exphy = NULL;
451 int i;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800452
Darrick J. Wongad689232007-01-26 14:08:52 -0800453 /* Directly attached device */
454 if (!pdev)
455 return dev->port->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800456
Darrick J. Wongad689232007-01-26 14:08:52 -0800457 /* Otherwise look in the expander */
458 for (i = 0; i < pdev->ex_dev.num_phys; i++)
459 if (!memcmp(dev->sas_addr,
460 pdev->ex_dev.ex_phy[i].attached_sas_addr,
461 SAS_ADDR_SIZE)) {
462 exphy = &pdev->ex_dev.ex_phy[i];
463 break;
464 }
465
466 BUG_ON(!exphy);
467 return exphy->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800468}
James Bottomley53195782008-02-23 23:35:44 -0600469EXPORT_SYMBOL_GPL(sas_find_local_phy);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800470
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800471/* Attempt to send a LUN reset message to a device */
Darrick J. Wongad689232007-01-26 14:08:52 -0800472int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500473{
Darrick J. Wongad689232007-01-26 14:08:52 -0800474 struct domain_device *dev = cmd_to_domain_dev(cmd);
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800475 struct sas_internal *i =
476 to_sas_internal(dev->port->ha->core.shost->transportt);
477 struct scsi_lun lun;
478 int res;
479
480 int_to_scsilun(cmd->device->lun, &lun);
481
482 if (!i->dft->lldd_lu_reset)
483 return FAILED;
484
485 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
486 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
487 return SUCCESS;
488
489 return FAILED;
490}
491
492/* Attempt to send a phy (bus) reset */
493int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
494{
495 struct domain_device *dev = cmd_to_domain_dev(cmd);
James Bottomley53195782008-02-23 23:35:44 -0600496 struct sas_phy *phy = sas_find_local_phy(dev);
Darrick J. Wongad689232007-01-26 14:08:52 -0800497 int res;
498
499 res = sas_phy_reset(phy, 1);
500 if (res)
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800501 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100502 kobject_name(&phy->dev.kobj),
Darrick J. Wongad689232007-01-26 14:08:52 -0800503 res);
504 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
505 return SUCCESS;
506
507 return FAILED;
508}
509
510/* Try to reset a device */
James Bottomley8de3ef22008-02-23 23:39:59 -0600511static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
Darrick J. Wongad689232007-01-26 14:08:52 -0800512{
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800513 int res;
James Bottomley8de3ef22008-02-23 23:39:59 -0600514 struct Scsi_Host *shost = cmd->device->host;
Darrick J. Wongad689232007-01-26 14:08:52 -0800515
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800516 if (!shost->hostt->eh_device_reset_handler)
517 goto try_bus_reset;
518
519 res = shost->hostt->eh_device_reset_handler(cmd);
520 if (res == SUCCESS)
521 return res;
522
523try_bus_reset:
524 if (shost->hostt->eh_bus_reset_handler)
525 return shost->hostt->eh_bus_reset_handler(cmd);
526
527 return FAILED;
Darrick J. Wongad689232007-01-26 14:08:52 -0800528}
529
530static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
531 struct list_head *work_q,
532 struct list_head *done_q)
533{
James Bottomley2908d772006-08-29 09:22:51 -0500534 struct scsi_cmnd *cmd, *n;
535 enum task_disposition res = TASK_IS_DONE;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800536 int tmf_resp, need_reset;
James Bottomley2908d772006-08-29 09:22:51 -0500537 struct sas_internal *i = to_sas_internal(shost->transportt);
Darrick J. Wongad689232007-01-26 14:08:52 -0800538 unsigned long flags;
539 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
James Bottomley2908d772006-08-29 09:22:51 -0500540
James Bottomley2908d772006-08-29 09:22:51 -0500541Again:
Darrick J. Wongad689232007-01-26 14:08:52 -0800542 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
James Bottomley2908d772006-08-29 09:22:51 -0500543 struct sas_task *task = TO_SAS_TASK(cmd);
Darrick J. Wongf4563932006-10-30 15:18:39 -0800544
Darrick J. Wongad689232007-01-26 14:08:52 -0800545 if (!task)
Darrick J. Wongf4563932006-10-30 15:18:39 -0800546 continue;
Darrick J. Wongad689232007-01-26 14:08:52 -0800547
548 list_del_init(&cmd->eh_entry);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800549
550 spin_lock_irqsave(&task->task_state_lock, flags);
551 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800552 spin_unlock_irqrestore(&task->task_state_lock, flags);
553
James Bottomley8de3ef22008-02-23 23:39:59 -0600554 if (need_reset) {
555 SAS_DPRINTK("%s: task 0x%p requests reset\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700556 __func__, task);
James Bottomley8de3ef22008-02-23 23:39:59 -0600557 goto reset;
558 }
559
Darrick J. Wongf4563932006-10-30 15:18:39 -0800560 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500561 res = sas_scsi_find_task(task);
562
563 cmd->eh_eflags = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500564
565 switch (res) {
566 case TASK_IS_DONE:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700567 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -0500568 task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600569 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500570 continue;
571 case TASK_IS_ABORTED:
572 SAS_DPRINTK("%s: task 0x%p is aborted\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700573 __func__, task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600574 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500575 continue;
576 case TASK_IS_AT_LU:
577 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
James Bottomley8de3ef22008-02-23 23:39:59 -0600578 reset:
James Bottomley2908d772006-08-29 09:22:51 -0500579 tmf_resp = sas_recover_lu(task->dev, cmd);
580 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
581 SAS_DPRINTK("dev %016llx LU %x is "
582 "recovered\n",
583 SAS_ADDR(task->dev),
584 cmd->device->lun);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600585 sas_eh_finish_cmd(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800586 sas_scsi_clear_queue_lu(work_q, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500587 goto Again;
588 }
589 /* fallthrough */
590 case TASK_IS_NOT_AT_LU:
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800591 case TASK_ABORT_FAILED:
James Bottomley2908d772006-08-29 09:22:51 -0500592 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
593 task);
594 tmf_resp = sas_recover_I_T(task->dev);
595 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
James Bottomley8de3ef22008-02-23 23:39:59 -0600596 struct domain_device *dev = task->dev;
James Bottomley2908d772006-08-29 09:22:51 -0500597 SAS_DPRINTK("I_T %016llx recovered\n",
598 SAS_ADDR(task->dev->sas_addr));
James Bottomleya8e14fe2008-02-19 21:48:42 -0600599 sas_eh_finish_cmd(cmd);
James Bottomley8de3ef22008-02-23 23:39:59 -0600600 sas_scsi_clear_queue_I_T(work_q, dev);
James Bottomley2908d772006-08-29 09:22:51 -0500601 goto Again;
602 }
603 /* Hammer time :-) */
James Bottomley8de3ef22008-02-23 23:39:59 -0600604 try_to_reset_cmd_device(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500605 if (i->dft->lldd_clear_nexus_port) {
606 struct asd_sas_port *port = task->dev->port;
607 SAS_DPRINTK("clearing nexus for port:%d\n",
608 port->id);
609 res = i->dft->lldd_clear_nexus_port(port);
610 if (res == TMF_RESP_FUNC_COMPLETE) {
611 SAS_DPRINTK("clear nexus port:%d "
612 "succeeded\n", port->id);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600613 sas_eh_finish_cmd(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800614 sas_scsi_clear_queue_port(work_q,
James Bottomley2908d772006-08-29 09:22:51 -0500615 port);
616 goto Again;
617 }
618 }
619 if (i->dft->lldd_clear_nexus_ha) {
620 SAS_DPRINTK("clear nexus ha\n");
621 res = i->dft->lldd_clear_nexus_ha(ha);
622 if (res == TMF_RESP_FUNC_COMPLETE) {
623 SAS_DPRINTK("clear nexus ha "
624 "succeeded\n");
James Bottomleya8e14fe2008-02-19 21:48:42 -0600625 sas_eh_finish_cmd(cmd);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600626 goto clear_q;
James Bottomley2908d772006-08-29 09:22:51 -0500627 }
628 }
629 /* If we are here -- this means that no amount
630 * of effort could recover from errors. Quite
631 * possibly the HA just disappeared.
632 */
633 SAS_DPRINTK("error from device %llx, LUN %x "
634 "couldn't be recovered in any way\n",
635 SAS_ADDR(task->dev->sas_addr),
636 cmd->device->lun);
637
James Bottomleya8e14fe2008-02-19 21:48:42 -0600638 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500639 goto clear_q;
640 }
641 }
Darrick J. Wongad689232007-01-26 14:08:52 -0800642 return list_empty(work_q);
James Bottomley2908d772006-08-29 09:22:51 -0500643clear_q:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700644 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600645 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
646 sas_eh_finish_cmd(cmd);
647
Darrick J. Wongad689232007-01-26 14:08:52 -0800648 return list_empty(work_q);
649}
650
651void sas_scsi_recover_host(struct Scsi_Host *shost)
652{
653 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
654 unsigned long flags;
655 LIST_HEAD(eh_work_q);
656
657 spin_lock_irqsave(shost->host_lock, flags);
658 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
659 spin_unlock_irqrestore(shost->host_lock, flags);
660
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700661 SAS_DPRINTK("Enter %s\n", __func__);
Darrick J. Wongad689232007-01-26 14:08:52 -0800662 /*
663 * Deal with commands that still have SAS tasks (i.e. they didn't
664 * complete via the normal sas_task completion mechanism)
665 */
666 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
667 goto out;
668
669 /*
670 * Now deal with SCSI commands that completed ok but have a an error
671 * code (and hopefully sense data) attached. This is roughly what
672 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
673 * command we see here has no sas_task and is thus unknown to the HA.
674 */
675 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
676 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
677
678out:
679 scsi_eh_flush_done_q(&ha->eh_done_q);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700680 SAS_DPRINTK("--- Exit %s\n", __func__);
Darrick J. Wongad689232007-01-26 14:08:52 -0800681 return;
James Bottomley2908d772006-08-29 09:22:51 -0500682}
683
Jens Axboe242f9dc2008-09-14 05:55:09 -0700684enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500685{
686 struct sas_task *task = TO_SAS_TASK(cmd);
687 unsigned long flags;
688
689 if (!task) {
Jens Axboe242f9dc2008-09-14 05:55:09 -0700690 cmd->request->timeout /= 2;
Darrick J. Wong6d4dcd42007-01-11 14:15:00 -0800691 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -0700692 cmd, task, (cmd->request->timeout ?
693 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
694 if (!cmd->request->timeout)
695 return BLK_EH_NOT_HANDLED;
696 return BLK_EH_RESET_TIMER;
James Bottomley2908d772006-08-29 09:22:51 -0500697 }
698
699 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800700 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
James Bottomley2908d772006-08-29 09:22:51 -0500701 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
702 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700703 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
704 "BLK_EH_HANDLED\n", cmd, task);
705 return BLK_EH_HANDLED;
James Bottomley2908d772006-08-29 09:22:51 -0500706 }
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800707 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
708 spin_unlock_irqrestore(&task->task_state_lock, flags);
709 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
Jens Axboe242f9dc2008-09-14 05:55:09 -0700710 "BLK_EH_RESET_TIMER\n",
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800711 cmd, task);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700712 return BLK_EH_RESET_TIMER;
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800713 }
James Bottomley2908d772006-08-29 09:22:51 -0500714 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
715 spin_unlock_irqrestore(&task->task_state_lock, flags);
716
Jens Axboe242f9dc2008-09-14 05:55:09 -0700717 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
James Bottomley2908d772006-08-29 09:22:51 -0500718 cmd, task);
719
Jens Axboe242f9dc2008-09-14 05:55:09 -0700720 return BLK_EH_NOT_HANDLED;
James Bottomley2908d772006-08-29 09:22:51 -0500721}
722
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700723int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
724{
725 struct domain_device *dev = sdev_to_domain_dev(sdev);
726
727 if (dev_is_sata(dev))
Jeff Garzik94be9a52009-01-16 10:17:09 -0500728 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700729
730 return -EINVAL;
731}
732
James Bottomley2908d772006-08-29 09:22:51 -0500733struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
734{
735 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
736 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
737 struct domain_device *found_dev = NULL;
738 int i;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800739 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500740
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800741 spin_lock_irqsave(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500742 for (i = 0; i < ha->num_phys; i++) {
743 struct asd_sas_port *port = ha->sas_port[i];
744 struct domain_device *dev;
745
746 spin_lock(&port->dev_list_lock);
747 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
748 if (rphy == dev->rphy) {
749 found_dev = dev;
750 spin_unlock(&port->dev_list_lock);
751 goto found;
752 }
753 }
754 spin_unlock(&port->dev_list_lock);
755 }
756 found:
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800757 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500758
759 return found_dev;
760}
761
762static inline struct domain_device *sas_find_target(struct scsi_target *starget)
763{
764 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
765
766 return sas_find_dev_by_rphy(rphy);
767}
768
769int sas_target_alloc(struct scsi_target *starget)
770{
771 struct domain_device *found_dev = sas_find_target(starget);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700772 int res;
James Bottomley2908d772006-08-29 09:22:51 -0500773
774 if (!found_dev)
775 return -ENODEV;
776
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700777 if (dev_is_sata(found_dev)) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700778 res = sas_ata_init_host_and_port(found_dev, starget);
779 if (res)
780 return res;
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700781 }
782
James Bottomley2908d772006-08-29 09:22:51 -0500783 starget->hostdata = found_dev;
784 return 0;
785}
786
787#define SAS_DEF_QD 32
788#define SAS_MAX_QD 64
789
790int sas_slave_configure(struct scsi_device *scsi_dev)
791{
792 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
793 struct sas_ha_struct *sas_ha;
794
795 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
796
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700797 if (dev_is_sata(dev)) {
798 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
799 return 0;
800 }
801
James Bottomley2908d772006-08-29 09:22:51 -0500802 sas_ha = dev->port->ha;
803
804 sas_read_port_mode_page(scsi_dev);
805
806 if (scsi_dev->tagged_supported) {
807 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
808 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
809 } else {
810 SAS_DPRINTK("device %llx, LUN %x doesn't support "
811 "TCQ\n", SAS_ADDR(dev->sas_addr),
812 scsi_dev->lun);
813 scsi_dev->tagged_supported = 0;
814 scsi_set_tag_type(scsi_dev, 0);
815 scsi_deactivate_tcq(scsi_dev, 1);
816 }
817
Darrick J. Wongf27708f2007-01-26 14:08:58 -0800818 scsi_dev->allow_restart = 1;
819
James Bottomley2908d772006-08-29 09:22:51 -0500820 return 0;
821}
822
823void sas_slave_destroy(struct scsi_device *scsi_dev)
824{
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700825 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
826
827 if (dev_is_sata(dev))
Tejun Heo3e4ec342010-05-10 21:41:30 +0200828 dev->sata_dev.ap->link.device[0].class = ATA_DEV_NONE;
James Bottomley2908d772006-08-29 09:22:51 -0500829}
830
Mike Christiee881a172009-10-15 17:46:39 -0700831int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth,
832 int reason)
James Bottomley2908d772006-08-29 09:22:51 -0500833{
834 int res = min(new_depth, SAS_MAX_QD);
835
Mike Christiee881a172009-10-15 17:46:39 -0700836 if (reason != SCSI_QDEPTH_DEFAULT)
837 return -EOPNOTSUPP;
838
James Bottomley2908d772006-08-29 09:22:51 -0500839 if (scsi_dev->tagged_supported)
840 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
841 res);
842 else {
843 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
844 sas_printk("device %llx LUN %x queue depth changed to 1\n",
845 SAS_ADDR(dev->sas_addr),
846 scsi_dev->lun);
847 scsi_adjust_queue_depth(scsi_dev, 0, 1);
848 res = 1;
849 }
850
851 return res;
852}
853
854int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
855{
856 if (!scsi_dev->tagged_supported)
857 return 0;
858
859 scsi_deactivate_tcq(scsi_dev, 1);
860
861 scsi_set_tag_type(scsi_dev, qt);
862 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
863
864 return qt;
865}
866
867int sas_bios_param(struct scsi_device *scsi_dev,
868 struct block_device *bdev,
869 sector_t capacity, int *hsc)
870{
871 hsc[0] = 255;
872 hsc[1] = 63;
873 sector_div(capacity, 255*63);
874 hsc[2] = capacity;
875
876 return 0;
877}
878
879/* ---------- Task Collector Thread implementation ---------- */
880
881static void sas_queue(struct sas_ha_struct *sas_ha)
882{
883 struct scsi_core *core = &sas_ha->core;
884 unsigned long flags;
885 LIST_HEAD(q);
886 int can_queue;
887 int res;
888 struct sas_internal *i = to_sas_internal(core->shost->transportt);
889
890 spin_lock_irqsave(&core->task_queue_lock, flags);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400891 while (!kthread_should_stop() &&
James Bottomley2908d772006-08-29 09:22:51 -0500892 !list_empty(&core->task_queue)) {
893
894 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
895 if (can_queue >= 0) {
896 can_queue = core->task_queue_size;
897 list_splice_init(&core->task_queue, &q);
898 } else {
899 struct list_head *a, *n;
900
901 can_queue = sas_ha->lldd_queue_size;
902 list_for_each_safe(a, n, &core->task_queue) {
903 list_move_tail(a, &q);
904 if (--can_queue == 0)
905 break;
906 }
907 can_queue = sas_ha->lldd_queue_size;
908 }
909 core->task_queue_size -= can_queue;
910 spin_unlock_irqrestore(&core->task_queue_lock, flags);
911 {
912 struct sas_task *task = list_entry(q.next,
913 struct sas_task,
914 list);
915 list_del_init(&q);
916 res = i->dft->lldd_execute_task(task, can_queue,
917 GFP_KERNEL);
918 if (unlikely(res))
919 __list_add(&q, task->list.prev, &task->list);
920 }
921 spin_lock_irqsave(&core->task_queue_lock, flags);
922 if (res) {
923 list_splice_init(&q, &core->task_queue); /*at head*/
924 core->task_queue_size += can_queue;
925 }
926 }
927 spin_unlock_irqrestore(&core->task_queue_lock, flags);
928}
929
James Bottomley2908d772006-08-29 09:22:51 -0500930/**
931 * sas_queue_thread -- The Task Collector thread
932 * @_sas_ha: pointer to struct sas_ha
933 */
934static int sas_queue_thread(void *_sas_ha)
935{
936 struct sas_ha_struct *sas_ha = _sas_ha;
James Bottomley2908d772006-08-29 09:22:51 -0500937
James Bottomley2908d772006-08-29 09:22:51 -0500938 while (1) {
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400939 set_current_state(TASK_INTERRUPTIBLE);
940 schedule();
James Bottomley2908d772006-08-29 09:22:51 -0500941 sas_queue(sas_ha);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400942 if (kthread_should_stop())
James Bottomley2908d772006-08-29 09:22:51 -0500943 break;
944 }
945
James Bottomley2908d772006-08-29 09:22:51 -0500946 return 0;
947}
948
949int sas_init_queue(struct sas_ha_struct *sas_ha)
950{
James Bottomley2908d772006-08-29 09:22:51 -0500951 struct scsi_core *core = &sas_ha->core;
952
953 spin_lock_init(&core->task_queue_lock);
954 core->task_queue_size = 0;
955 INIT_LIST_HEAD(&core->task_queue);
James Bottomley2908d772006-08-29 09:22:51 -0500956
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400957 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
958 "sas_queue_%d", core->shost->host_no);
959 if (IS_ERR(core->queue_thread))
960 return PTR_ERR(core->queue_thread);
961 return 0;
James Bottomley2908d772006-08-29 09:22:51 -0500962}
963
964void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
965{
966 unsigned long flags;
967 struct scsi_core *core = &sas_ha->core;
968 struct sas_task *task, *n;
969
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400970 kthread_stop(core->queue_thread);
James Bottomley2908d772006-08-29 09:22:51 -0500971
972 if (!list_empty(&core->task_queue))
973 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
974 SAS_ADDR(sas_ha->sas_addr));
975
976 spin_lock_irqsave(&core->task_queue_lock, flags);
977 list_for_each_entry_safe(task, n, &core->task_queue, list) {
978 struct scsi_cmnd *cmd = task->uldd_task;
979
980 list_del_init(&task->list);
981
982 ASSIGN_SAS_TASK(cmd, NULL);
983 sas_free_task(task);
984 cmd->result = DID_ABORT << 16;
985 cmd->scsi_done(cmd);
986 }
987 spin_unlock_irqrestore(&core->task_queue_lock, flags);
988}
989
Darrick J. Wong396819f2007-01-11 14:15:20 -0800990/*
991 * Call the LLDD task abort routine directly. This function is intended for
992 * use by upper layers that need to tell the LLDD to abort a task.
993 */
994int __sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800995{
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800996 struct sas_internal *si =
997 to_sas_internal(task->dev->port->ha->core.shost->transportt);
998 unsigned long flags;
999 int res;
1000
1001 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001002 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
1003 task->task_state_flags & SAS_TASK_STATE_DONE) {
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001004 spin_unlock_irqrestore(&task->task_state_lock, flags);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001005 SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001006 task);
1007 return 0;
1008 }
Darrick J. Wong396819f2007-01-11 14:15:20 -08001009 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001010 spin_unlock_irqrestore(&task->task_state_lock, flags);
1011
1012 if (!si->dft->lldd_abort_task)
1013 return -ENODEV;
1014
1015 res = si->dft->lldd_abort_task(task);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001016
1017 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001018 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
1019 (res == TMF_RESP_FUNC_COMPLETE))
1020 {
Darrick J. Wong396819f2007-01-11 14:15:20 -08001021 spin_unlock_irqrestore(&task->task_state_lock, flags);
1022 task->task_done(task);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001023 return 0;
1024 }
1025
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001026 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1027 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1028 spin_unlock_irqrestore(&task->task_state_lock, flags);
1029
1030 return -EAGAIN;
1031}
1032
Darrick J. Wong396819f2007-01-11 14:15:20 -08001033/*
1034 * Tell an upper layer that it needs to initiate an abort for a given task.
1035 * This should only ever be called by an LLDD.
1036 */
1037void sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001038{
Darrick J. Wong396819f2007-01-11 14:15:20 -08001039 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001040
Darrick J. Wong396819f2007-01-11 14:15:20 -08001041 /* Escape for libsas internal commands */
1042 if (!sc) {
1043 if (!del_timer(&task->timer))
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001044 return;
Darrick J. Wong396819f2007-01-11 14:15:20 -08001045 task->timer.function(task->timer.data);
1046 return;
1047 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001048
Darrick J. Wong3a2755a2007-01-30 01:18:58 -08001049 if (dev_is_sata(task->dev)) {
1050 sas_ata_task_abort(task);
James Bottomley1b4d0d82010-05-13 09:31:54 -05001051 } else {
1052 struct request_queue *q = sc->device->request_queue;
1053 unsigned long flags;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -08001054
James Bottomley1b4d0d82010-05-13 09:31:54 -05001055 spin_lock_irqsave(q->queue_lock, flags);
1056 blk_abort_request(sc->request);
1057 spin_unlock_irqrestore(q->queue_lock, flags);
1058 scsi_schedule_eh(sc->device->host);
1059 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001060}
1061
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001062int sas_slave_alloc(struct scsi_device *scsi_dev)
1063{
1064 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1065
1066 if (dev_is_sata(dev))
1067 return ata_sas_port_init(dev->sata_dev.ap);
1068
1069 return 0;
1070}
1071
1072void sas_target_destroy(struct scsi_target *starget)
1073{
1074 struct domain_device *found_dev = sas_find_target(starget);
1075
1076 if (!found_dev)
1077 return;
1078
1079 if (dev_is_sata(found_dev))
1080 ata_sas_port_destroy(found_dev->sata_dev.ap);
1081
1082 return;
1083}
1084
Darrick J. Wong45e6cdf2008-02-19 10:49:40 -08001085static void sas_parse_addr(u8 *sas_addr, const char *p)
1086{
1087 int i;
1088 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1089 u8 h, l;
1090 if (!*p)
1091 break;
1092 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1093 p++;
1094 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1095 p++;
1096 sas_addr[i] = (h<<4) | l;
1097 }
1098}
1099
1100#define SAS_STRING_ADDR_SIZE 16
1101
1102int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1103{
1104 int res;
1105 const struct firmware *fw;
1106
1107 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1108 if (res)
1109 return res;
1110
1111 if (fw->size < SAS_STRING_ADDR_SIZE) {
1112 res = -ENODEV;
1113 goto out;
1114 }
1115
1116 sas_parse_addr(addr, fw->data);
1117
1118out:
1119 release_firmware(fw);
1120 return res;
1121}
1122EXPORT_SYMBOL_GPL(sas_request_addr);
1123
James Bottomley2908d772006-08-29 09:22:51 -05001124EXPORT_SYMBOL_GPL(sas_queuecommand);
1125EXPORT_SYMBOL_GPL(sas_target_alloc);
1126EXPORT_SYMBOL_GPL(sas_slave_configure);
1127EXPORT_SYMBOL_GPL(sas_slave_destroy);
1128EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1129EXPORT_SYMBOL_GPL(sas_change_queue_type);
1130EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001131EXPORT_SYMBOL_GPL(__sas_task_abort);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001132EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -08001133EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001134EXPORT_SYMBOL_GPL(sas_phy_enable);
Darrick J. Wongad689232007-01-26 14:08:52 -08001135EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
Darrick J. Wonga9344e62007-01-29 23:48:19 -08001136EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001137EXPORT_SYMBOL_GPL(sas_slave_alloc);
1138EXPORT_SYMBOL_GPL(sas_target_destroy);
1139EXPORT_SYMBOL_GPL(sas_ioctl);