blob: 9dd1e9eda19b0964ceb99a1d8bd962064a63ad59 [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
Christoph Hellwig92bd4012011-07-11 14:49:23 -0400185int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500186{
James Bottomley2908d772006-08-29 09:22:51 -0500187 struct sas_internal *i = to_sas_internal(host->transportt);
Christoph Hellwiga923f752011-07-11 14:49:24 -0400188 struct domain_device *dev = cmd_to_domain_dev(cmd);
189 struct sas_ha_struct *sas_ha = dev->port->ha;
190 struct sas_task *task;
191 int res = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500192
Christoph Hellwiga923f752011-07-11 14:49:24 -0400193 /* If the device fell off, no sense in issuing commands */
194 if (dev->gone) {
195 cmd->result = DID_BAD_TARGET << 16;
196 goto out_done;
James Bottomley2908d772006-08-29 09:22:51 -0500197 }
Christoph Hellwiga923f752011-07-11 14:49:24 -0400198
199 if (dev_is_sata(dev)) {
200 unsigned long flags;
201
202 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
203 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
204 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
205 return res;
206 }
207
208 task = sas_create_task(cmd, dev, GFP_ATOMIC);
209 if (!task)
210 return -ENOMEM;
211
212 /* Queue up, Direct Mode or Task Collector Mode. */
213 if (sas_ha->lldd_max_execute_num < 2)
214 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
215 else
216 res = sas_queue_up(task);
217
218 if (res)
219 goto out_free_task;
220 return 0;
221
222out_free_task:
223 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
224 ASSIGN_SAS_TASK(cmd, NULL);
225 sas_free_task(task);
226 if (res != -SAS_QUEUE_FULL)
227 return res;
228 cmd->result = DID_SOFT_ERROR << 16; /* retry */
229out_done:
230 cmd->scsi_done(cmd);
231 return 0;
James Bottomley2908d772006-08-29 09:22:51 -0500232}
233
James Bottomleya8e14fe2008-02-19 21:48:42 -0600234static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
235{
236 struct sas_task *task = TO_SAS_TASK(cmd);
237 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
238
239 /* remove the aborted task flag to allow the task to be
240 * completed now. At this point, we only get called following
241 * an actual abort of the task, so we should be guaranteed not
242 * to be racing with any completions from the LLD (hence we
243 * don't need the task state lock to clear the flag) */
244 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
245 /* Now call task_done. However, task will be free'd after
246 * this */
247 task->task_done(task);
248 /* now finish the command and move it on to the error
249 * handler done list, this also takes it off the
250 * error handler pending list */
251 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
252}
253
James Bottomley2908d772006-08-29 09:22:51 -0500254static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
255{
256 struct scsi_cmnd *cmd, *n;
257
258 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
James Bottomley63e45632008-02-22 17:07:52 -0600259 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
260 cmd->device->lun == my_cmd->device->lun)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600261 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500262 }
263}
264
265static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
266 struct domain_device *dev)
267{
268 struct scsi_cmnd *cmd, *n;
269
270 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
271 struct domain_device *x = cmd_to_domain_dev(cmd);
272
273 if (x == dev)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600274 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500275 }
276}
277
278static void sas_scsi_clear_queue_port(struct list_head *error_q,
279 struct asd_sas_port *port)
280{
281 struct scsi_cmnd *cmd, *n;
282
283 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
284 struct domain_device *dev = cmd_to_domain_dev(cmd);
285 struct asd_sas_port *x = dev->port;
286
287 if (x == port)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600288 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500289 }
290}
291
292enum task_disposition {
293 TASK_IS_DONE,
294 TASK_IS_ABORTED,
295 TASK_IS_AT_LU,
296 TASK_IS_NOT_AT_LU,
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800297 TASK_ABORT_FAILED,
James Bottomley2908d772006-08-29 09:22:51 -0500298};
299
300static enum task_disposition sas_scsi_find_task(struct sas_task *task)
301{
302 struct sas_ha_struct *ha = task->dev->port->ha;
303 unsigned long flags;
304 int i, res;
305 struct sas_internal *si =
306 to_sas_internal(task->dev->port->ha->core.shost->transportt);
307
308 if (ha->lldd_max_execute_num > 1) {
309 struct scsi_core *core = &ha->core;
310 struct sas_task *t, *n;
311
312 spin_lock_irqsave(&core->task_queue_lock, flags);
313 list_for_each_entry_safe(t, n, &core->task_queue, list) {
314 if (task == t) {
315 list_del_init(&t->list);
316 spin_unlock_irqrestore(&core->task_queue_lock,
317 flags);
318 SAS_DPRINTK("%s: task 0x%p aborted from "
319 "task_queue\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700320 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500321 return TASK_IS_ABORTED;
322 }
323 }
324 spin_unlock_irqrestore(&core->task_queue_lock, flags);
325 }
326
327 for (i = 0; i < 5; i++) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700328 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500329 res = si->dft->lldd_abort_task(task);
330
331 spin_lock_irqsave(&task->task_state_lock, flags);
332 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
333 spin_unlock_irqrestore(&task->task_state_lock, flags);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700334 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -0500335 task);
336 return TASK_IS_DONE;
337 }
338 spin_unlock_irqrestore(&task->task_state_lock, flags);
339
340 if (res == TMF_RESP_FUNC_COMPLETE) {
341 SAS_DPRINTK("%s: task 0x%p is aborted\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700342 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500343 return TASK_IS_ABORTED;
344 } else if (si->dft->lldd_query_task) {
345 SAS_DPRINTK("%s: querying task 0x%p\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700346 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500347 res = si->dft->lldd_query_task(task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800348 switch (res) {
349 case TMF_RESP_FUNC_SUCC:
James Bottomley2908d772006-08-29 09:22:51 -0500350 SAS_DPRINTK("%s: task 0x%p at LU\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700351 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500352 return TASK_IS_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800353 case TMF_RESP_FUNC_COMPLETE:
James Bottomley2908d772006-08-29 09:22:51 -0500354 SAS_DPRINTK("%s: task 0x%p not at LU\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700355 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500356 return TASK_IS_NOT_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800357 case TMF_RESP_FUNC_FAILED:
358 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700359 __func__, task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800360 return TASK_ABORT_FAILED;
361 }
362
James Bottomley2908d772006-08-29 09:22:51 -0500363 }
364 }
365 return res;
366}
367
368static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
369{
370 int res = TMF_RESP_FUNC_FAILED;
371 struct scsi_lun lun;
372 struct sas_internal *i =
373 to_sas_internal(dev->port->ha->core.shost->transportt);
374
375 int_to_scsilun(cmd->device->lun, &lun);
376
377 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
378 SAS_ADDR(dev->sas_addr),
379 cmd->device->lun);
380
381 if (i->dft->lldd_abort_task_set)
382 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
383
384 if (res == TMF_RESP_FUNC_FAILED) {
385 if (i->dft->lldd_clear_task_set)
386 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
387 }
388
389 if (res == TMF_RESP_FUNC_FAILED) {
390 if (i->dft->lldd_lu_reset)
391 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
392 }
393
394 return res;
395}
396
397static int sas_recover_I_T(struct domain_device *dev)
398{
399 int res = TMF_RESP_FUNC_FAILED;
400 struct sas_internal *i =
401 to_sas_internal(dev->port->ha->core.shost->transportt);
402
403 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
404 SAS_ADDR(dev->sas_addr));
405
406 if (i->dft->lldd_I_T_nexus_reset)
407 res = i->dft->lldd_I_T_nexus_reset(dev);
408
409 return res;
410}
411
Darrick J. Wongad689232007-01-26 14:08:52 -0800412/* Find the sas_phy that's attached to this device */
James Bottomley53195782008-02-23 23:35:44 -0600413struct sas_phy *sas_find_local_phy(struct domain_device *dev)
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800414{
Darrick J. Wongad689232007-01-26 14:08:52 -0800415 struct domain_device *pdev = dev->parent;
416 struct ex_phy *exphy = NULL;
417 int i;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800418
Darrick J. Wongad689232007-01-26 14:08:52 -0800419 /* Directly attached device */
420 if (!pdev)
421 return dev->port->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800422
Darrick J. Wongad689232007-01-26 14:08:52 -0800423 /* Otherwise look in the expander */
424 for (i = 0; i < pdev->ex_dev.num_phys; i++)
425 if (!memcmp(dev->sas_addr,
426 pdev->ex_dev.ex_phy[i].attached_sas_addr,
427 SAS_ADDR_SIZE)) {
428 exphy = &pdev->ex_dev.ex_phy[i];
429 break;
430 }
431
432 BUG_ON(!exphy);
433 return exphy->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800434}
James Bottomley53195782008-02-23 23:35:44 -0600435EXPORT_SYMBOL_GPL(sas_find_local_phy);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800436
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800437/* Attempt to send a LUN reset message to a device */
Darrick J. Wongad689232007-01-26 14:08:52 -0800438int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500439{
Darrick J. Wongad689232007-01-26 14:08:52 -0800440 struct domain_device *dev = cmd_to_domain_dev(cmd);
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800441 struct sas_internal *i =
442 to_sas_internal(dev->port->ha->core.shost->transportt);
443 struct scsi_lun lun;
444 int res;
445
446 int_to_scsilun(cmd->device->lun, &lun);
447
448 if (!i->dft->lldd_lu_reset)
449 return FAILED;
450
451 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
452 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
453 return SUCCESS;
454
455 return FAILED;
456}
457
458/* Attempt to send a phy (bus) reset */
459int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
460{
461 struct domain_device *dev = cmd_to_domain_dev(cmd);
James Bottomley53195782008-02-23 23:35:44 -0600462 struct sas_phy *phy = sas_find_local_phy(dev);
Darrick J. Wongad689232007-01-26 14:08:52 -0800463 int res;
464
465 res = sas_phy_reset(phy, 1);
466 if (res)
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800467 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100468 kobject_name(&phy->dev.kobj),
Darrick J. Wongad689232007-01-26 14:08:52 -0800469 res);
470 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
471 return SUCCESS;
472
473 return FAILED;
474}
475
476/* Try to reset a device */
James Bottomley8de3ef22008-02-23 23:39:59 -0600477static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
Darrick J. Wongad689232007-01-26 14:08:52 -0800478{
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800479 int res;
James Bottomley8de3ef22008-02-23 23:39:59 -0600480 struct Scsi_Host *shost = cmd->device->host;
Darrick J. Wongad689232007-01-26 14:08:52 -0800481
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800482 if (!shost->hostt->eh_device_reset_handler)
483 goto try_bus_reset;
484
485 res = shost->hostt->eh_device_reset_handler(cmd);
486 if (res == SUCCESS)
487 return res;
488
489try_bus_reset:
490 if (shost->hostt->eh_bus_reset_handler)
491 return shost->hostt->eh_bus_reset_handler(cmd);
492
493 return FAILED;
Darrick J. Wongad689232007-01-26 14:08:52 -0800494}
495
496static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
497 struct list_head *work_q,
498 struct list_head *done_q)
499{
James Bottomley2908d772006-08-29 09:22:51 -0500500 struct scsi_cmnd *cmd, *n;
501 enum task_disposition res = TASK_IS_DONE;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800502 int tmf_resp, need_reset;
James Bottomley2908d772006-08-29 09:22:51 -0500503 struct sas_internal *i = to_sas_internal(shost->transportt);
Darrick J. Wongad689232007-01-26 14:08:52 -0800504 unsigned long flags;
505 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
James Bottomley2908d772006-08-29 09:22:51 -0500506
James Bottomley2908d772006-08-29 09:22:51 -0500507Again:
Darrick J. Wongad689232007-01-26 14:08:52 -0800508 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
James Bottomley2908d772006-08-29 09:22:51 -0500509 struct sas_task *task = TO_SAS_TASK(cmd);
Darrick J. Wongf4563932006-10-30 15:18:39 -0800510
Darrick J. Wongad689232007-01-26 14:08:52 -0800511 if (!task)
Darrick J. Wongf4563932006-10-30 15:18:39 -0800512 continue;
Darrick J. Wongad689232007-01-26 14:08:52 -0800513
514 list_del_init(&cmd->eh_entry);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800515
516 spin_lock_irqsave(&task->task_state_lock, flags);
517 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800518 spin_unlock_irqrestore(&task->task_state_lock, flags);
519
James Bottomley8de3ef22008-02-23 23:39:59 -0600520 if (need_reset) {
521 SAS_DPRINTK("%s: task 0x%p requests reset\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700522 __func__, task);
James Bottomley8de3ef22008-02-23 23:39:59 -0600523 goto reset;
524 }
525
Darrick J. Wongf4563932006-10-30 15:18:39 -0800526 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500527 res = sas_scsi_find_task(task);
528
529 cmd->eh_eflags = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500530
531 switch (res) {
532 case TASK_IS_DONE:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700533 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -0500534 task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600535 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500536 continue;
537 case TASK_IS_ABORTED:
538 SAS_DPRINTK("%s: task 0x%p is aborted\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700539 __func__, task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600540 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500541 continue;
542 case TASK_IS_AT_LU:
543 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
James Bottomley8de3ef22008-02-23 23:39:59 -0600544 reset:
James Bottomley2908d772006-08-29 09:22:51 -0500545 tmf_resp = sas_recover_lu(task->dev, cmd);
546 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
547 SAS_DPRINTK("dev %016llx LU %x is "
548 "recovered\n",
549 SAS_ADDR(task->dev),
550 cmd->device->lun);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600551 sas_eh_finish_cmd(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800552 sas_scsi_clear_queue_lu(work_q, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500553 goto Again;
554 }
555 /* fallthrough */
556 case TASK_IS_NOT_AT_LU:
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800557 case TASK_ABORT_FAILED:
James Bottomley2908d772006-08-29 09:22:51 -0500558 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
559 task);
560 tmf_resp = sas_recover_I_T(task->dev);
561 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
James Bottomley8de3ef22008-02-23 23:39:59 -0600562 struct domain_device *dev = task->dev;
James Bottomley2908d772006-08-29 09:22:51 -0500563 SAS_DPRINTK("I_T %016llx recovered\n",
564 SAS_ADDR(task->dev->sas_addr));
James Bottomleya8e14fe2008-02-19 21:48:42 -0600565 sas_eh_finish_cmd(cmd);
James Bottomley8de3ef22008-02-23 23:39:59 -0600566 sas_scsi_clear_queue_I_T(work_q, dev);
James Bottomley2908d772006-08-29 09:22:51 -0500567 goto Again;
568 }
569 /* Hammer time :-) */
James Bottomley8de3ef22008-02-23 23:39:59 -0600570 try_to_reset_cmd_device(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500571 if (i->dft->lldd_clear_nexus_port) {
572 struct asd_sas_port *port = task->dev->port;
573 SAS_DPRINTK("clearing nexus for port:%d\n",
574 port->id);
575 res = i->dft->lldd_clear_nexus_port(port);
576 if (res == TMF_RESP_FUNC_COMPLETE) {
577 SAS_DPRINTK("clear nexus port:%d "
578 "succeeded\n", port->id);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600579 sas_eh_finish_cmd(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800580 sas_scsi_clear_queue_port(work_q,
James Bottomley2908d772006-08-29 09:22:51 -0500581 port);
582 goto Again;
583 }
584 }
585 if (i->dft->lldd_clear_nexus_ha) {
586 SAS_DPRINTK("clear nexus ha\n");
587 res = i->dft->lldd_clear_nexus_ha(ha);
588 if (res == TMF_RESP_FUNC_COMPLETE) {
589 SAS_DPRINTK("clear nexus ha "
590 "succeeded\n");
James Bottomleya8e14fe2008-02-19 21:48:42 -0600591 sas_eh_finish_cmd(cmd);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600592 goto clear_q;
James Bottomley2908d772006-08-29 09:22:51 -0500593 }
594 }
595 /* If we are here -- this means that no amount
596 * of effort could recover from errors. Quite
597 * possibly the HA just disappeared.
598 */
599 SAS_DPRINTK("error from device %llx, LUN %x "
600 "couldn't be recovered in any way\n",
601 SAS_ADDR(task->dev->sas_addr),
602 cmd->device->lun);
603
James Bottomleya8e14fe2008-02-19 21:48:42 -0600604 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500605 goto clear_q;
606 }
607 }
Darrick J. Wongad689232007-01-26 14:08:52 -0800608 return list_empty(work_q);
James Bottomley2908d772006-08-29 09:22:51 -0500609clear_q:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700610 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600611 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
612 sas_eh_finish_cmd(cmd);
613
Darrick J. Wongad689232007-01-26 14:08:52 -0800614 return list_empty(work_q);
615}
616
617void sas_scsi_recover_host(struct Scsi_Host *shost)
618{
619 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
620 unsigned long flags;
621 LIST_HEAD(eh_work_q);
622
623 spin_lock_irqsave(shost->host_lock, flags);
624 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
James Bottomley9ee91f72011-01-20 17:26:44 -0600625 shost->host_eh_scheduled = 0;
Darrick J. Wongad689232007-01-26 14:08:52 -0800626 spin_unlock_irqrestore(shost->host_lock, flags);
627
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700628 SAS_DPRINTK("Enter %s\n", __func__);
Darrick J. Wongad689232007-01-26 14:08:52 -0800629 /*
630 * Deal with commands that still have SAS tasks (i.e. they didn't
631 * complete via the normal sas_task completion mechanism)
632 */
633 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
634 goto out;
635
636 /*
637 * Now deal with SCSI commands that completed ok but have a an error
638 * code (and hopefully sense data) attached. This is roughly what
639 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
640 * command we see here has no sas_task and is thus unknown to the HA.
641 */
James Bottomley00dd4992011-01-23 09:44:12 -0600642 if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
643 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
644 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
Darrick J. Wongad689232007-01-26 14:08:52 -0800645
646out:
James Bottomley00dd4992011-01-23 09:44:12 -0600647 /* now link into libata eh --- if we have any ata devices */
648 sas_ata_strategy_handler(shost);
649
Darrick J. Wongad689232007-01-26 14:08:52 -0800650 scsi_eh_flush_done_q(&ha->eh_done_q);
James Bottomley00dd4992011-01-23 09:44:12 -0600651
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700652 SAS_DPRINTK("--- Exit %s\n", __func__);
Darrick J. Wongad689232007-01-26 14:08:52 -0800653 return;
James Bottomley2908d772006-08-29 09:22:51 -0500654}
655
Jens Axboe242f9dc2008-09-14 05:55:09 -0700656enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500657{
658 struct sas_task *task = TO_SAS_TASK(cmd);
659 unsigned long flags;
James Bottomleyc2991902011-01-23 09:44:12 -0600660 enum blk_eh_timer_return rtn;
James Bottomley00dd4992011-01-23 09:44:12 -0600661
662 if (sas_ata_timed_out(cmd, task, &rtn))
663 return rtn;
James Bottomley2908d772006-08-29 09:22:51 -0500664
665 if (!task) {
Jens Axboe242f9dc2008-09-14 05:55:09 -0700666 cmd->request->timeout /= 2;
Darrick J. Wong6d4dcd42007-01-11 14:15:00 -0800667 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -0700668 cmd, task, (cmd->request->timeout ?
669 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
670 if (!cmd->request->timeout)
671 return BLK_EH_NOT_HANDLED;
672 return BLK_EH_RESET_TIMER;
James Bottomley2908d772006-08-29 09:22:51 -0500673 }
674
675 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800676 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
James Bottomley2908d772006-08-29 09:22:51 -0500677 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
678 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700679 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
680 "BLK_EH_HANDLED\n", cmd, task);
681 return BLK_EH_HANDLED;
James Bottomley2908d772006-08-29 09:22:51 -0500682 }
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800683 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
684 spin_unlock_irqrestore(&task->task_state_lock, flags);
685 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
Jens Axboe242f9dc2008-09-14 05:55:09 -0700686 "BLK_EH_RESET_TIMER\n",
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800687 cmd, task);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700688 return BLK_EH_RESET_TIMER;
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800689 }
James Bottomley2908d772006-08-29 09:22:51 -0500690 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
691 spin_unlock_irqrestore(&task->task_state_lock, flags);
692
Jens Axboe242f9dc2008-09-14 05:55:09 -0700693 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
James Bottomley2908d772006-08-29 09:22:51 -0500694 cmd, task);
695
Jens Axboe242f9dc2008-09-14 05:55:09 -0700696 return BLK_EH_NOT_HANDLED;
James Bottomley2908d772006-08-29 09:22:51 -0500697}
698
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700699int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
700{
701 struct domain_device *dev = sdev_to_domain_dev(sdev);
702
703 if (dev_is_sata(dev))
Jeff Garzik94be9a52009-01-16 10:17:09 -0500704 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700705
706 return -EINVAL;
707}
708
James Bottomley2908d772006-08-29 09:22:51 -0500709struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
710{
711 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
712 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
713 struct domain_device *found_dev = NULL;
714 int i;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800715 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500716
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800717 spin_lock_irqsave(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500718 for (i = 0; i < ha->num_phys; i++) {
719 struct asd_sas_port *port = ha->sas_port[i];
720 struct domain_device *dev;
721
722 spin_lock(&port->dev_list_lock);
723 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
724 if (rphy == dev->rphy) {
725 found_dev = dev;
726 spin_unlock(&port->dev_list_lock);
727 goto found;
728 }
729 }
730 spin_unlock(&port->dev_list_lock);
731 }
732 found:
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800733 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500734
735 return found_dev;
736}
737
738static inline struct domain_device *sas_find_target(struct scsi_target *starget)
739{
740 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
741
742 return sas_find_dev_by_rphy(rphy);
743}
744
745int sas_target_alloc(struct scsi_target *starget)
746{
747 struct domain_device *found_dev = sas_find_target(starget);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700748 int res;
James Bottomley2908d772006-08-29 09:22:51 -0500749
750 if (!found_dev)
751 return -ENODEV;
752
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700753 if (dev_is_sata(found_dev)) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700754 res = sas_ata_init_host_and_port(found_dev, starget);
755 if (res)
756 return res;
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700757 }
758
James Bottomley2908d772006-08-29 09:22:51 -0500759 starget->hostdata = found_dev;
760 return 0;
761}
762
763#define SAS_DEF_QD 32
764#define SAS_MAX_QD 64
765
766int sas_slave_configure(struct scsi_device *scsi_dev)
767{
768 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
769 struct sas_ha_struct *sas_ha;
770
771 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
772
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700773 if (dev_is_sata(dev)) {
774 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
775 return 0;
776 }
777
James Bottomley2908d772006-08-29 09:22:51 -0500778 sas_ha = dev->port->ha;
779
780 sas_read_port_mode_page(scsi_dev);
781
782 if (scsi_dev->tagged_supported) {
783 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
784 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
785 } else {
786 SAS_DPRINTK("device %llx, LUN %x doesn't support "
787 "TCQ\n", SAS_ADDR(dev->sas_addr),
788 scsi_dev->lun);
789 scsi_dev->tagged_supported = 0;
790 scsi_set_tag_type(scsi_dev, 0);
791 scsi_deactivate_tcq(scsi_dev, 1);
792 }
793
Darrick J. Wongf27708f2007-01-26 14:08:58 -0800794 scsi_dev->allow_restart = 1;
795
James Bottomley2908d772006-08-29 09:22:51 -0500796 return 0;
797}
798
799void sas_slave_destroy(struct scsi_device *scsi_dev)
800{
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700801 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
802
803 if (dev_is_sata(dev))
Tejun Heo3e4ec342010-05-10 21:41:30 +0200804 dev->sata_dev.ap->link.device[0].class = ATA_DEV_NONE;
James Bottomley2908d772006-08-29 09:22:51 -0500805}
806
Mike Christiee881a172009-10-15 17:46:39 -0700807int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth,
808 int reason)
James Bottomley2908d772006-08-29 09:22:51 -0500809{
810 int res = min(new_depth, SAS_MAX_QD);
811
Mike Christiee881a172009-10-15 17:46:39 -0700812 if (reason != SCSI_QDEPTH_DEFAULT)
813 return -EOPNOTSUPP;
814
James Bottomley2908d772006-08-29 09:22:51 -0500815 if (scsi_dev->tagged_supported)
816 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
817 res);
818 else {
819 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
820 sas_printk("device %llx LUN %x queue depth changed to 1\n",
821 SAS_ADDR(dev->sas_addr),
822 scsi_dev->lun);
823 scsi_adjust_queue_depth(scsi_dev, 0, 1);
824 res = 1;
825 }
826
827 return res;
828}
829
830int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
831{
832 if (!scsi_dev->tagged_supported)
833 return 0;
834
835 scsi_deactivate_tcq(scsi_dev, 1);
836
837 scsi_set_tag_type(scsi_dev, qt);
838 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
839
840 return qt;
841}
842
843int sas_bios_param(struct scsi_device *scsi_dev,
844 struct block_device *bdev,
845 sector_t capacity, int *hsc)
846{
847 hsc[0] = 255;
848 hsc[1] = 63;
849 sector_div(capacity, 255*63);
850 hsc[2] = capacity;
851
852 return 0;
853}
854
855/* ---------- Task Collector Thread implementation ---------- */
856
857static void sas_queue(struct sas_ha_struct *sas_ha)
858{
859 struct scsi_core *core = &sas_ha->core;
860 unsigned long flags;
861 LIST_HEAD(q);
862 int can_queue;
863 int res;
864 struct sas_internal *i = to_sas_internal(core->shost->transportt);
865
866 spin_lock_irqsave(&core->task_queue_lock, flags);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400867 while (!kthread_should_stop() &&
James Bottomley2908d772006-08-29 09:22:51 -0500868 !list_empty(&core->task_queue)) {
869
870 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
871 if (can_queue >= 0) {
872 can_queue = core->task_queue_size;
873 list_splice_init(&core->task_queue, &q);
874 } else {
875 struct list_head *a, *n;
876
877 can_queue = sas_ha->lldd_queue_size;
878 list_for_each_safe(a, n, &core->task_queue) {
879 list_move_tail(a, &q);
880 if (--can_queue == 0)
881 break;
882 }
883 can_queue = sas_ha->lldd_queue_size;
884 }
885 core->task_queue_size -= can_queue;
886 spin_unlock_irqrestore(&core->task_queue_lock, flags);
887 {
888 struct sas_task *task = list_entry(q.next,
889 struct sas_task,
890 list);
891 list_del_init(&q);
892 res = i->dft->lldd_execute_task(task, can_queue,
893 GFP_KERNEL);
894 if (unlikely(res))
895 __list_add(&q, task->list.prev, &task->list);
896 }
897 spin_lock_irqsave(&core->task_queue_lock, flags);
898 if (res) {
899 list_splice_init(&q, &core->task_queue); /*at head*/
900 core->task_queue_size += can_queue;
901 }
902 }
903 spin_unlock_irqrestore(&core->task_queue_lock, flags);
904}
905
James Bottomley2908d772006-08-29 09:22:51 -0500906/**
907 * sas_queue_thread -- The Task Collector thread
908 * @_sas_ha: pointer to struct sas_ha
909 */
910static int sas_queue_thread(void *_sas_ha)
911{
912 struct sas_ha_struct *sas_ha = _sas_ha;
James Bottomley2908d772006-08-29 09:22:51 -0500913
James Bottomley2908d772006-08-29 09:22:51 -0500914 while (1) {
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400915 set_current_state(TASK_INTERRUPTIBLE);
916 schedule();
James Bottomley2908d772006-08-29 09:22:51 -0500917 sas_queue(sas_ha);
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400918 if (kthread_should_stop())
James Bottomley2908d772006-08-29 09:22:51 -0500919 break;
920 }
921
James Bottomley2908d772006-08-29 09:22:51 -0500922 return 0;
923}
924
925int sas_init_queue(struct sas_ha_struct *sas_ha)
926{
James Bottomley2908d772006-08-29 09:22:51 -0500927 struct scsi_core *core = &sas_ha->core;
928
929 spin_lock_init(&core->task_queue_lock);
930 core->task_queue_size = 0;
931 INIT_LIST_HEAD(&core->task_queue);
James Bottomley2908d772006-08-29 09:22:51 -0500932
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400933 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
934 "sas_queue_%d", core->shost->host_no);
935 if (IS_ERR(core->queue_thread))
936 return PTR_ERR(core->queue_thread);
937 return 0;
James Bottomley2908d772006-08-29 09:22:51 -0500938}
939
940void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
941{
942 unsigned long flags;
943 struct scsi_core *core = &sas_ha->core;
944 struct sas_task *task, *n;
945
Christoph Hellwigd7a54e32007-04-26 09:38:01 -0400946 kthread_stop(core->queue_thread);
James Bottomley2908d772006-08-29 09:22:51 -0500947
948 if (!list_empty(&core->task_queue))
949 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
950 SAS_ADDR(sas_ha->sas_addr));
951
952 spin_lock_irqsave(&core->task_queue_lock, flags);
953 list_for_each_entry_safe(task, n, &core->task_queue, list) {
954 struct scsi_cmnd *cmd = task->uldd_task;
955
956 list_del_init(&task->list);
957
958 ASSIGN_SAS_TASK(cmd, NULL);
959 sas_free_task(task);
960 cmd->result = DID_ABORT << 16;
961 cmd->scsi_done(cmd);
962 }
963 spin_unlock_irqrestore(&core->task_queue_lock, flags);
964}
965
Darrick J. Wong396819f2007-01-11 14:15:20 -0800966/*
967 * Call the LLDD task abort routine directly. This function is intended for
968 * use by upper layers that need to tell the LLDD to abort a task.
969 */
970int __sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800971{
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800972 struct sas_internal *si =
973 to_sas_internal(task->dev->port->ha->core.shost->transportt);
974 unsigned long flags;
975 int res;
976
977 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800978 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
979 task->task_state_flags & SAS_TASK_STATE_DONE) {
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800980 spin_unlock_irqrestore(&task->task_state_lock, flags);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700981 SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800982 task);
983 return 0;
984 }
Darrick J. Wong396819f2007-01-11 14:15:20 -0800985 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800986 spin_unlock_irqrestore(&task->task_state_lock, flags);
987
988 if (!si->dft->lldd_abort_task)
989 return -ENODEV;
990
991 res = si->dft->lldd_abort_task(task);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800992
993 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800994 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
995 (res == TMF_RESP_FUNC_COMPLETE))
996 {
Darrick J. Wong396819f2007-01-11 14:15:20 -0800997 spin_unlock_irqrestore(&task->task_state_lock, flags);
998 task->task_done(task);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800999 return 0;
1000 }
1001
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001002 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1003 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1004 spin_unlock_irqrestore(&task->task_state_lock, flags);
1005
1006 return -EAGAIN;
1007}
1008
Darrick J. Wong396819f2007-01-11 14:15:20 -08001009/*
1010 * Tell an upper layer that it needs to initiate an abort for a given task.
1011 * This should only ever be called by an LLDD.
1012 */
1013void sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001014{
Darrick J. Wong396819f2007-01-11 14:15:20 -08001015 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001016
Darrick J. Wong396819f2007-01-11 14:15:20 -08001017 /* Escape for libsas internal commands */
1018 if (!sc) {
1019 if (!del_timer(&task->timer))
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001020 return;
Darrick J. Wong396819f2007-01-11 14:15:20 -08001021 task->timer.function(task->timer.data);
1022 return;
1023 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001024
Darrick J. Wong3a2755a2007-01-30 01:18:58 -08001025 if (dev_is_sata(task->dev)) {
1026 sas_ata_task_abort(task);
James Bottomley1b4d0d82010-05-13 09:31:54 -05001027 } else {
1028 struct request_queue *q = sc->device->request_queue;
1029 unsigned long flags;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -08001030
James Bottomley1b4d0d82010-05-13 09:31:54 -05001031 spin_lock_irqsave(q->queue_lock, flags);
1032 blk_abort_request(sc->request);
1033 spin_unlock_irqrestore(q->queue_lock, flags);
1034 scsi_schedule_eh(sc->device->host);
1035 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001036}
1037
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001038int sas_slave_alloc(struct scsi_device *scsi_dev)
1039{
1040 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1041
1042 if (dev_is_sata(dev))
1043 return ata_sas_port_init(dev->sata_dev.ap);
1044
1045 return 0;
1046}
1047
1048void sas_target_destroy(struct scsi_target *starget)
1049{
1050 struct domain_device *found_dev = sas_find_target(starget);
1051
1052 if (!found_dev)
1053 return;
1054
1055 if (dev_is_sata(found_dev))
1056 ata_sas_port_destroy(found_dev->sata_dev.ap);
1057
1058 return;
1059}
1060
Darrick J. Wong45e6cdf2008-02-19 10:49:40 -08001061static void sas_parse_addr(u8 *sas_addr, const char *p)
1062{
1063 int i;
1064 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1065 u8 h, l;
1066 if (!*p)
1067 break;
1068 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1069 p++;
1070 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1071 p++;
1072 sas_addr[i] = (h<<4) | l;
1073 }
1074}
1075
1076#define SAS_STRING_ADDR_SIZE 16
1077
1078int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1079{
1080 int res;
1081 const struct firmware *fw;
1082
1083 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1084 if (res)
1085 return res;
1086
1087 if (fw->size < SAS_STRING_ADDR_SIZE) {
1088 res = -ENODEV;
1089 goto out;
1090 }
1091
1092 sas_parse_addr(addr, fw->data);
1093
1094out:
1095 release_firmware(fw);
1096 return res;
1097}
1098EXPORT_SYMBOL_GPL(sas_request_addr);
1099
James Bottomley2908d772006-08-29 09:22:51 -05001100EXPORT_SYMBOL_GPL(sas_queuecommand);
1101EXPORT_SYMBOL_GPL(sas_target_alloc);
1102EXPORT_SYMBOL_GPL(sas_slave_configure);
1103EXPORT_SYMBOL_GPL(sas_slave_destroy);
1104EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1105EXPORT_SYMBOL_GPL(sas_change_queue_type);
1106EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001107EXPORT_SYMBOL_GPL(__sas_task_abort);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001108EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -08001109EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001110EXPORT_SYMBOL_GPL(sas_phy_enable);
Darrick J. Wongad689232007-01-26 14:08:52 -08001111EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
Darrick J. Wonga9344e62007-01-29 23:48:19 -08001112EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -07001113EXPORT_SYMBOL_GPL(sas_slave_alloc);
1114EXPORT_SYMBOL_GPL(sas_target_destroy);
1115EXPORT_SYMBOL_GPL(sas_ioctl);