blob: 897a5e2c55e438a806c6e93072547a7386922832 [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
26#include "sas_internal.h"
27
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi.h>
Darrick J. Wongf4563932006-10-30 15:18:39 -080032#include <scsi/scsi_eh.h>
James Bottomley2908d772006-08-29 09:22:51 -050033#include <scsi/scsi_transport.h>
34#include <scsi/scsi_transport_sas.h>
35#include "../scsi_sas_internal.h"
Darrick J. Wong79a5eb62006-10-30 15:18:50 -080036#include "../scsi_transport_api.h"
Darrick J. Wongad689232007-01-26 14:08:52 -080037#include "../scsi_priv.h"
James Bottomley2908d772006-08-29 09:22:51 -050038
39#include <linux/err.h>
40#include <linux/blkdev.h>
41#include <linux/scatterlist.h>
42
43/* ---------- SCSI Host glue ---------- */
44
45#define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
46#define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
47
48static void sas_scsi_task_done(struct sas_task *task)
49{
50 struct task_status_struct *ts = &task->task_status;
51 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wongf4563932006-10-30 15:18:39 -080052 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
James Bottomley2908d772006-08-29 09:22:51 -050053 unsigned ts_flags = task->task_state_flags;
54 int hs = 0, stat = 0;
55
56 if (unlikely(!sc)) {
57 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
58 list_del_init(&task->list);
59 sas_free_task(task);
60 return;
61 }
62
63 if (ts->resp == SAS_TASK_UNDELIVERED) {
64 /* transport error */
65 hs = DID_NO_CONNECT;
66 } else { /* ts->resp == SAS_TASK_COMPLETE */
67 /* task delivered, what happened afterwards? */
68 switch (ts->stat) {
69 case SAS_DEV_NO_RESPONSE:
70 case SAS_INTERRUPTED:
71 case SAS_PHY_DOWN:
72 case SAS_NAK_R_ERR:
73 case SAS_OPEN_TO:
74 hs = DID_NO_CONNECT;
75 break;
76 case SAS_DATA_UNDERRUN:
77 sc->resid = ts->residual;
78 if (sc->request_bufflen - sc->resid < sc->underflow)
79 hs = DID_ERROR;
80 break;
81 case SAS_DATA_OVERRUN:
82 hs = DID_ERROR;
83 break;
84 case SAS_QUEUE_FULL:
85 hs = DID_SOFT_ERROR; /* retry */
86 break;
87 case SAS_DEVICE_UNKNOWN:
88 hs = DID_BAD_TARGET;
89 break;
90 case SAS_SG_ERR:
91 hs = DID_PARITY;
92 break;
93 case SAS_OPEN_REJECT:
94 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
95 hs = DID_SOFT_ERROR; /* retry */
96 else
97 hs = DID_ERROR;
98 break;
99 case SAS_PROTO_RESPONSE:
100 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
101 "task; please report this\n",
102 task->dev->port->ha->sas_ha_name);
103 break;
104 case SAS_ABORTED_TASK:
105 hs = DID_ABORT;
106 break;
107 case SAM_CHECK_COND:
108 memcpy(sc->sense_buffer, ts->buf,
109 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
110 stat = SAM_CHECK_COND;
111 break;
112 default:
113 stat = ts->stat;
114 break;
115 }
116 }
117 ASSIGN_SAS_TASK(sc, NULL);
118 sc->result = (hs << 16) | stat;
119 list_del_init(&task->list);
120 sas_free_task(task);
121 /* This is very ugly but this is how SCSI Core works. */
122 if (ts_flags & SAS_TASK_STATE_ABORTED)
Darrick J. Wongf4563932006-10-30 15:18:39 -0800123 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
James Bottomley2908d772006-08-29 09:22:51 -0500124 else
125 sc->scsi_done(sc);
126}
127
128static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
129{
130 enum task_attribute ta = TASK_ATTR_SIMPLE;
131 if (cmd->request && blk_rq_tagged(cmd->request)) {
132 if (cmd->device->ordered_tags &&
Jeff Garzik1bdfd552006-09-30 21:28:22 -0400133 (cmd->request->cmd_flags & REQ_HARDBARRIER))
FUJITA Tomonori63bb1bf2007-01-28 01:19:41 +0900134 ta = TASK_ATTR_ORDERED;
James Bottomley2908d772006-08-29 09:22:51 -0500135 }
136 return ta;
137}
138
139static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
140 struct domain_device *dev,
Al Viro3cc27542006-09-25 02:55:40 +0100141 gfp_t gfp_flags)
James Bottomley2908d772006-08-29 09:22:51 -0500142{
143 struct sas_task *task = sas_alloc_task(gfp_flags);
144 struct scsi_lun lun;
145
146 if (!task)
147 return NULL;
148
149 *(u32 *)cmd->sense_buffer = 0;
150 task->uldd_task = cmd;
151 ASSIGN_SAS_TASK(cmd, task);
152
153 task->dev = dev;
154 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
155
156 task->ssp_task.retry_count = 1;
157 int_to_scsilun(cmd->device->lun, &lun);
158 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
159 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
160 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
161
162 task->scatter = cmd->request_buffer;
163 task->num_scatter = cmd->use_sg;
164 task->total_xfer_len = cmd->request_bufflen;
165 task->data_dir = cmd->sc_data_direction;
166
167 task->task_done = sas_scsi_task_done;
168
169 return task;
170}
171
172static int sas_queue_up(struct sas_task *task)
173{
174 struct sas_ha_struct *sas_ha = task->dev->port->ha;
175 struct scsi_core *core = &sas_ha->core;
176 unsigned long flags;
177 LIST_HEAD(list);
178
179 spin_lock_irqsave(&core->task_queue_lock, flags);
180 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
181 spin_unlock_irqrestore(&core->task_queue_lock, flags);
182 return -SAS_QUEUE_FULL;
183 }
184 list_add_tail(&task->list, &core->task_queue);
185 core->task_queue_size += 1;
186 spin_unlock_irqrestore(&core->task_queue_lock, flags);
187 up(&core->queue_thread_sema);
188
189 return 0;
190}
191
192/**
193 * sas_queuecommand -- Enqueue a command for processing
194 * @parameters: See SCSI Core documentation
195 *
196 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
197 * call us without holding an IRQ spinlock...
198 */
199int sas_queuecommand(struct scsi_cmnd *cmd,
200 void (*scsi_done)(struct scsi_cmnd *))
201{
202 int res = 0;
203 struct domain_device *dev = cmd_to_domain_dev(cmd);
204 struct Scsi_Host *host = cmd->device->host;
205 struct sas_internal *i = to_sas_internal(host->transportt);
206
207 spin_unlock_irq(host->host_lock);
208
209 {
210 struct sas_ha_struct *sas_ha = dev->port->ha;
211 struct sas_task *task;
212
213 res = -ENOMEM;
214 task = sas_create_task(cmd, dev, GFP_ATOMIC);
215 if (!task)
216 goto out;
217
218 cmd->scsi_done = scsi_done;
219 /* Queue up, Direct Mode or Task Collector Mode. */
220 if (sas_ha->lldd_max_execute_num < 2)
221 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
222 else
223 res = sas_queue_up(task);
224
225 /* Examine */
226 if (res) {
227 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
228 ASSIGN_SAS_TASK(cmd, NULL);
229 sas_free_task(task);
230 if (res == -SAS_QUEUE_FULL) {
231 cmd->result = DID_SOFT_ERROR << 16; /* retry */
232 res = 0;
233 scsi_done(cmd);
234 }
235 goto out;
236 }
237 }
238out:
239 spin_lock_irq(host->host_lock);
240 return res;
241}
242
243static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
244{
245 struct scsi_cmnd *cmd, *n;
246
247 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
248 if (cmd == my_cmd)
249 list_del_init(&cmd->eh_entry);
250 }
251}
252
253static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
254 struct domain_device *dev)
255{
256 struct scsi_cmnd *cmd, *n;
257
258 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
259 struct domain_device *x = cmd_to_domain_dev(cmd);
260
261 if (x == dev)
262 list_del_init(&cmd->eh_entry);
263 }
264}
265
266static void sas_scsi_clear_queue_port(struct list_head *error_q,
267 struct asd_sas_port *port)
268{
269 struct scsi_cmnd *cmd, *n;
270
271 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
272 struct domain_device *dev = cmd_to_domain_dev(cmd);
273 struct asd_sas_port *x = dev->port;
274
275 if (x == port)
276 list_del_init(&cmd->eh_entry);
277 }
278}
279
280enum task_disposition {
281 TASK_IS_DONE,
282 TASK_IS_ABORTED,
283 TASK_IS_AT_LU,
284 TASK_IS_NOT_AT_LU,
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800285 TASK_ABORT_FAILED,
James Bottomley2908d772006-08-29 09:22:51 -0500286};
287
288static enum task_disposition sas_scsi_find_task(struct sas_task *task)
289{
290 struct sas_ha_struct *ha = task->dev->port->ha;
291 unsigned long flags;
292 int i, res;
293 struct sas_internal *si =
294 to_sas_internal(task->dev->port->ha->core.shost->transportt);
295
296 if (ha->lldd_max_execute_num > 1) {
297 struct scsi_core *core = &ha->core;
298 struct sas_task *t, *n;
299
300 spin_lock_irqsave(&core->task_queue_lock, flags);
301 list_for_each_entry_safe(t, n, &core->task_queue, list) {
302 if (task == t) {
303 list_del_init(&t->list);
304 spin_unlock_irqrestore(&core->task_queue_lock,
305 flags);
306 SAS_DPRINTK("%s: task 0x%p aborted from "
307 "task_queue\n",
308 __FUNCTION__, task);
309 return TASK_IS_ABORTED;
310 }
311 }
312 spin_unlock_irqrestore(&core->task_queue_lock, flags);
313 }
314
315 for (i = 0; i < 5; i++) {
316 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
317 res = si->dft->lldd_abort_task(task);
318
319 spin_lock_irqsave(&task->task_state_lock, flags);
320 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
321 spin_unlock_irqrestore(&task->task_state_lock, flags);
322 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
323 task);
324 return TASK_IS_DONE;
325 }
326 spin_unlock_irqrestore(&task->task_state_lock, flags);
327
328 if (res == TMF_RESP_FUNC_COMPLETE) {
329 SAS_DPRINTK("%s: task 0x%p is aborted\n",
330 __FUNCTION__, task);
331 return TASK_IS_ABORTED;
332 } else if (si->dft->lldd_query_task) {
333 SAS_DPRINTK("%s: querying task 0x%p\n",
334 __FUNCTION__, task);
335 res = si->dft->lldd_query_task(task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800336 switch (res) {
337 case TMF_RESP_FUNC_SUCC:
James Bottomley2908d772006-08-29 09:22:51 -0500338 SAS_DPRINTK("%s: task 0x%p at LU\n",
339 __FUNCTION__, task);
340 return TASK_IS_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800341 case TMF_RESP_FUNC_COMPLETE:
James Bottomley2908d772006-08-29 09:22:51 -0500342 SAS_DPRINTK("%s: task 0x%p not at LU\n",
343 __FUNCTION__, task);
344 return TASK_IS_NOT_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800345 case TMF_RESP_FUNC_FAILED:
346 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
347 __FUNCTION__, task);
348 return TASK_ABORT_FAILED;
349 }
350
James Bottomley2908d772006-08-29 09:22:51 -0500351 }
352 }
353 return res;
354}
355
356static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
357{
358 int res = TMF_RESP_FUNC_FAILED;
359 struct scsi_lun lun;
360 struct sas_internal *i =
361 to_sas_internal(dev->port->ha->core.shost->transportt);
362
363 int_to_scsilun(cmd->device->lun, &lun);
364
365 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
366 SAS_ADDR(dev->sas_addr),
367 cmd->device->lun);
368
369 if (i->dft->lldd_abort_task_set)
370 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
371
372 if (res == TMF_RESP_FUNC_FAILED) {
373 if (i->dft->lldd_clear_task_set)
374 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
375 }
376
377 if (res == TMF_RESP_FUNC_FAILED) {
378 if (i->dft->lldd_lu_reset)
379 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
380 }
381
382 return res;
383}
384
385static int sas_recover_I_T(struct domain_device *dev)
386{
387 int res = TMF_RESP_FUNC_FAILED;
388 struct sas_internal *i =
389 to_sas_internal(dev->port->ha->core.shost->transportt);
390
391 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
392 SAS_ADDR(dev->sas_addr));
393
394 if (i->dft->lldd_I_T_nexus_reset)
395 res = i->dft->lldd_I_T_nexus_reset(dev);
396
397 return res;
398}
399
Darrick J. Wongad689232007-01-26 14:08:52 -0800400/* Find the sas_phy that's attached to this device */
401struct sas_phy *find_local_sas_phy(struct domain_device *dev)
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800402{
Darrick J. Wongad689232007-01-26 14:08:52 -0800403 struct domain_device *pdev = dev->parent;
404 struct ex_phy *exphy = NULL;
405 int i;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800406
Darrick J. Wongad689232007-01-26 14:08:52 -0800407 /* Directly attached device */
408 if (!pdev)
409 return dev->port->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800410
Darrick J. Wongad689232007-01-26 14:08:52 -0800411 /* Otherwise look in the expander */
412 for (i = 0; i < pdev->ex_dev.num_phys; i++)
413 if (!memcmp(dev->sas_addr,
414 pdev->ex_dev.ex_phy[i].attached_sas_addr,
415 SAS_ADDR_SIZE)) {
416 exphy = &pdev->ex_dev.ex_phy[i];
417 break;
418 }
419
420 BUG_ON(!exphy);
421 return exphy->phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800422}
423
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800424/* Attempt to send a LUN reset message to a device */
Darrick J. Wongad689232007-01-26 14:08:52 -0800425int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500426{
Darrick J. Wongad689232007-01-26 14:08:52 -0800427 struct domain_device *dev = cmd_to_domain_dev(cmd);
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800428 struct sas_internal *i =
429 to_sas_internal(dev->port->ha->core.shost->transportt);
430 struct scsi_lun lun;
431 int res;
432
433 int_to_scsilun(cmd->device->lun, &lun);
434
435 if (!i->dft->lldd_lu_reset)
436 return FAILED;
437
438 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
439 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
440 return SUCCESS;
441
442 return FAILED;
443}
444
445/* Attempt to send a phy (bus) reset */
446int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
447{
448 struct domain_device *dev = cmd_to_domain_dev(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800449 struct sas_phy *phy = find_local_sas_phy(dev);
450 int res;
451
452 res = sas_phy_reset(phy, 1);
453 if (res)
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800454 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
Darrick J. Wongad689232007-01-26 14:08:52 -0800455 phy->dev.kobj.k_name,
456 res);
457 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
458 return SUCCESS;
459
460 return FAILED;
461}
462
463/* Try to reset a device */
464static int try_to_reset_cmd_device(struct Scsi_Host *shost,
465 struct scsi_cmnd *cmd)
466{
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800467 int res;
Darrick J. Wongad689232007-01-26 14:08:52 -0800468
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800469 if (!shost->hostt->eh_device_reset_handler)
470 goto try_bus_reset;
471
472 res = shost->hostt->eh_device_reset_handler(cmd);
473 if (res == SUCCESS)
474 return res;
475
476try_bus_reset:
477 if (shost->hostt->eh_bus_reset_handler)
478 return shost->hostt->eh_bus_reset_handler(cmd);
479
480 return FAILED;
Darrick J. Wongad689232007-01-26 14:08:52 -0800481}
482
483static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
484 struct list_head *work_q,
485 struct list_head *done_q)
486{
James Bottomley2908d772006-08-29 09:22:51 -0500487 struct scsi_cmnd *cmd, *n;
488 enum task_disposition res = TASK_IS_DONE;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800489 int tmf_resp, need_reset;
James Bottomley2908d772006-08-29 09:22:51 -0500490 struct sas_internal *i = to_sas_internal(shost->transportt);
Darrick J. Wongad689232007-01-26 14:08:52 -0800491 unsigned long flags;
492 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
James Bottomley2908d772006-08-29 09:22:51 -0500493
James Bottomley2908d772006-08-29 09:22:51 -0500494Again:
Darrick J. Wongad689232007-01-26 14:08:52 -0800495 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
James Bottomley2908d772006-08-29 09:22:51 -0500496 struct sas_task *task = TO_SAS_TASK(cmd);
Darrick J. Wongf4563932006-10-30 15:18:39 -0800497
Darrick J. Wongad689232007-01-26 14:08:52 -0800498 if (!task)
Darrick J. Wongf4563932006-10-30 15:18:39 -0800499 continue;
Darrick J. Wongad689232007-01-26 14:08:52 -0800500
501 list_del_init(&cmd->eh_entry);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800502
503 spin_lock_irqsave(&task->task_state_lock, flags);
504 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800505 spin_unlock_irqrestore(&task->task_state_lock, flags);
506
Darrick J. Wongf4563932006-10-30 15:18:39 -0800507 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500508 res = sas_scsi_find_task(task);
509
510 cmd->eh_eflags = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500511
512 switch (res) {
513 case TASK_IS_DONE:
514 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
515 task);
516 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800517 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800518 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500519 continue;
520 case TASK_IS_ABORTED:
521 SAS_DPRINTK("%s: task 0x%p is aborted\n",
522 __FUNCTION__, task);
523 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800524 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800525 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500526 continue;
527 case TASK_IS_AT_LU:
528 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
529 tmf_resp = sas_recover_lu(task->dev, cmd);
530 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
531 SAS_DPRINTK("dev %016llx LU %x is "
532 "recovered\n",
533 SAS_ADDR(task->dev),
534 cmd->device->lun);
535 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800536 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800537 try_to_reset_cmd_device(shost, cmd);
538 sas_scsi_clear_queue_lu(work_q, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500539 goto Again;
540 }
541 /* fallthrough */
542 case TASK_IS_NOT_AT_LU:
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800543 case TASK_ABORT_FAILED:
James Bottomley2908d772006-08-29 09:22:51 -0500544 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
545 task);
546 tmf_resp = sas_recover_I_T(task->dev);
547 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
548 SAS_DPRINTK("I_T %016llx recovered\n",
549 SAS_ADDR(task->dev->sas_addr));
550 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800551 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800552 try_to_reset_cmd_device(shost, cmd);
553 sas_scsi_clear_queue_I_T(work_q, task->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500554 goto Again;
555 }
556 /* Hammer time :-) */
557 if (i->dft->lldd_clear_nexus_port) {
558 struct asd_sas_port *port = task->dev->port;
559 SAS_DPRINTK("clearing nexus for port:%d\n",
560 port->id);
561 res = i->dft->lldd_clear_nexus_port(port);
562 if (res == TMF_RESP_FUNC_COMPLETE) {
563 SAS_DPRINTK("clear nexus port:%d "
564 "succeeded\n", port->id);
565 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800566 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800567 try_to_reset_cmd_device(shost, cmd);
568 sas_scsi_clear_queue_port(work_q,
James Bottomley2908d772006-08-29 09:22:51 -0500569 port);
570 goto Again;
571 }
572 }
573 if (i->dft->lldd_clear_nexus_ha) {
574 SAS_DPRINTK("clear nexus ha\n");
575 res = i->dft->lldd_clear_nexus_ha(ha);
576 if (res == TMF_RESP_FUNC_COMPLETE) {
577 SAS_DPRINTK("clear nexus ha "
578 "succeeded\n");
579 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800580 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800581 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500582 goto out;
583 }
584 }
585 /* If we are here -- this means that no amount
586 * of effort could recover from errors. Quite
587 * possibly the HA just disappeared.
588 */
589 SAS_DPRINTK("error from device %llx, LUN %x "
590 "couldn't be recovered in any way\n",
591 SAS_ADDR(task->dev->sas_addr),
592 cmd->device->lun);
593
594 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800595 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800596 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500597 goto clear_q;
598 }
599 }
600out:
Darrick J. Wongad689232007-01-26 14:08:52 -0800601 return list_empty(work_q);
James Bottomley2908d772006-08-29 09:22:51 -0500602clear_q:
603 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
Darrick J. Wongad689232007-01-26 14:08:52 -0800604 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
James Bottomley2908d772006-08-29 09:22:51 -0500605 struct sas_task *task = TO_SAS_TASK(cmd);
606 list_del_init(&cmd->eh_entry);
607 task->task_done(task);
608 }
Darrick J. Wongad689232007-01-26 14:08:52 -0800609 return list_empty(work_q);
610}
611
612void sas_scsi_recover_host(struct Scsi_Host *shost)
613{
614 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
615 unsigned long flags;
616 LIST_HEAD(eh_work_q);
617
618 spin_lock_irqsave(shost->host_lock, flags);
619 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
620 spin_unlock_irqrestore(shost->host_lock, flags);
621
622 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
623 /*
624 * Deal with commands that still have SAS tasks (i.e. they didn't
625 * complete via the normal sas_task completion mechanism)
626 */
627 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
628 goto out;
629
630 /*
631 * Now deal with SCSI commands that completed ok but have a an error
632 * code (and hopefully sense data) attached. This is roughly what
633 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
634 * command we see here has no sas_task and is thus unknown to the HA.
635 */
636 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
637 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
638
639out:
640 scsi_eh_flush_done_q(&ha->eh_done_q);
641 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
642 return;
James Bottomley2908d772006-08-29 09:22:51 -0500643}
644
645enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
646{
647 struct sas_task *task = TO_SAS_TASK(cmd);
648 unsigned long flags;
649
650 if (!task) {
Darrick J. Wong6d4dcd42007-01-11 14:15:00 -0800651 cmd->timeout_per_command /= 2;
652 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
653 cmd, task, (cmd->timeout_per_command ?
654 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
655 if (!cmd->timeout_per_command)
656 return EH_NOT_HANDLED;
657 return EH_RESET_TIMER;
James Bottomley2908d772006-08-29 09:22:51 -0500658 }
659
660 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800661 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
James Bottomley2908d772006-08-29 09:22:51 -0500662 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
663 spin_unlock_irqrestore(&task->task_state_lock, flags);
664 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
665 cmd, task);
666 return EH_HANDLED;
667 }
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800668 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
669 spin_unlock_irqrestore(&task->task_state_lock, flags);
670 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
671 "EH_RESET_TIMER\n",
672 cmd, task);
673 return EH_RESET_TIMER;
674 }
James Bottomley2908d772006-08-29 09:22:51 -0500675 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
676 spin_unlock_irqrestore(&task->task_state_lock, flags);
677
678 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
679 cmd, task);
680
681 return EH_NOT_HANDLED;
682}
683
684struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
685{
686 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
687 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
688 struct domain_device *found_dev = NULL;
689 int i;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800690 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500691
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800692 spin_lock_irqsave(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500693 for (i = 0; i < ha->num_phys; i++) {
694 struct asd_sas_port *port = ha->sas_port[i];
695 struct domain_device *dev;
696
697 spin_lock(&port->dev_list_lock);
698 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
699 if (rphy == dev->rphy) {
700 found_dev = dev;
701 spin_unlock(&port->dev_list_lock);
702 goto found;
703 }
704 }
705 spin_unlock(&port->dev_list_lock);
706 }
707 found:
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800708 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500709
710 return found_dev;
711}
712
713static inline struct domain_device *sas_find_target(struct scsi_target *starget)
714{
715 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
716
717 return sas_find_dev_by_rphy(rphy);
718}
719
720int sas_target_alloc(struct scsi_target *starget)
721{
722 struct domain_device *found_dev = sas_find_target(starget);
723
724 if (!found_dev)
725 return -ENODEV;
726
727 starget->hostdata = found_dev;
728 return 0;
729}
730
731#define SAS_DEF_QD 32
732#define SAS_MAX_QD 64
733
734int sas_slave_configure(struct scsi_device *scsi_dev)
735{
736 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
737 struct sas_ha_struct *sas_ha;
738
739 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
740
741 sas_ha = dev->port->ha;
742
743 sas_read_port_mode_page(scsi_dev);
744
745 if (scsi_dev->tagged_supported) {
746 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
747 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
748 } else {
749 SAS_DPRINTK("device %llx, LUN %x doesn't support "
750 "TCQ\n", SAS_ADDR(dev->sas_addr),
751 scsi_dev->lun);
752 scsi_dev->tagged_supported = 0;
753 scsi_set_tag_type(scsi_dev, 0);
754 scsi_deactivate_tcq(scsi_dev, 1);
755 }
756
Darrick J. Wongf27708f2007-01-26 14:08:58 -0800757 scsi_dev->allow_restart = 1;
758
James Bottomley2908d772006-08-29 09:22:51 -0500759 return 0;
760}
761
762void sas_slave_destroy(struct scsi_device *scsi_dev)
763{
764}
765
766int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
767{
768 int res = min(new_depth, SAS_MAX_QD);
769
770 if (scsi_dev->tagged_supported)
771 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
772 res);
773 else {
774 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
775 sas_printk("device %llx LUN %x queue depth changed to 1\n",
776 SAS_ADDR(dev->sas_addr),
777 scsi_dev->lun);
778 scsi_adjust_queue_depth(scsi_dev, 0, 1);
779 res = 1;
780 }
781
782 return res;
783}
784
785int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
786{
787 if (!scsi_dev->tagged_supported)
788 return 0;
789
790 scsi_deactivate_tcq(scsi_dev, 1);
791
792 scsi_set_tag_type(scsi_dev, qt);
793 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
794
795 return qt;
796}
797
798int sas_bios_param(struct scsi_device *scsi_dev,
799 struct block_device *bdev,
800 sector_t capacity, int *hsc)
801{
802 hsc[0] = 255;
803 hsc[1] = 63;
804 sector_div(capacity, 255*63);
805 hsc[2] = capacity;
806
807 return 0;
808}
809
810/* ---------- Task Collector Thread implementation ---------- */
811
812static void sas_queue(struct sas_ha_struct *sas_ha)
813{
814 struct scsi_core *core = &sas_ha->core;
815 unsigned long flags;
816 LIST_HEAD(q);
817 int can_queue;
818 int res;
819 struct sas_internal *i = to_sas_internal(core->shost->transportt);
820
821 spin_lock_irqsave(&core->task_queue_lock, flags);
822 while (!core->queue_thread_kill &&
823 !list_empty(&core->task_queue)) {
824
825 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
826 if (can_queue >= 0) {
827 can_queue = core->task_queue_size;
828 list_splice_init(&core->task_queue, &q);
829 } else {
830 struct list_head *a, *n;
831
832 can_queue = sas_ha->lldd_queue_size;
833 list_for_each_safe(a, n, &core->task_queue) {
834 list_move_tail(a, &q);
835 if (--can_queue == 0)
836 break;
837 }
838 can_queue = sas_ha->lldd_queue_size;
839 }
840 core->task_queue_size -= can_queue;
841 spin_unlock_irqrestore(&core->task_queue_lock, flags);
842 {
843 struct sas_task *task = list_entry(q.next,
844 struct sas_task,
845 list);
846 list_del_init(&q);
847 res = i->dft->lldd_execute_task(task, can_queue,
848 GFP_KERNEL);
849 if (unlikely(res))
850 __list_add(&q, task->list.prev, &task->list);
851 }
852 spin_lock_irqsave(&core->task_queue_lock, flags);
853 if (res) {
854 list_splice_init(&q, &core->task_queue); /*at head*/
855 core->task_queue_size += can_queue;
856 }
857 }
858 spin_unlock_irqrestore(&core->task_queue_lock, flags);
859}
860
861static DECLARE_COMPLETION(queue_th_comp);
862
863/**
864 * sas_queue_thread -- The Task Collector thread
865 * @_sas_ha: pointer to struct sas_ha
866 */
867static int sas_queue_thread(void *_sas_ha)
868{
869 struct sas_ha_struct *sas_ha = _sas_ha;
870 struct scsi_core *core = &sas_ha->core;
871
872 daemonize("sas_queue_%d", core->shost->host_no);
873 current->flags |= PF_NOFREEZE;
874
875 complete(&queue_th_comp);
876
877 while (1) {
878 down_interruptible(&core->queue_thread_sema);
879 sas_queue(sas_ha);
880 if (core->queue_thread_kill)
881 break;
882 }
883
884 complete(&queue_th_comp);
885
886 return 0;
887}
888
889int sas_init_queue(struct sas_ha_struct *sas_ha)
890{
891 int res;
892 struct scsi_core *core = &sas_ha->core;
893
894 spin_lock_init(&core->task_queue_lock);
895 core->task_queue_size = 0;
896 INIT_LIST_HEAD(&core->task_queue);
897 init_MUTEX_LOCKED(&core->queue_thread_sema);
898
899 res = kernel_thread(sas_queue_thread, sas_ha, 0);
900 if (res >= 0)
901 wait_for_completion(&queue_th_comp);
902
903 return res < 0 ? res : 0;
904}
905
906void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
907{
908 unsigned long flags;
909 struct scsi_core *core = &sas_ha->core;
910 struct sas_task *task, *n;
911
912 init_completion(&queue_th_comp);
913 core->queue_thread_kill = 1;
914 up(&core->queue_thread_sema);
915 wait_for_completion(&queue_th_comp);
916
917 if (!list_empty(&core->task_queue))
918 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
919 SAS_ADDR(sas_ha->sas_addr));
920
921 spin_lock_irqsave(&core->task_queue_lock, flags);
922 list_for_each_entry_safe(task, n, &core->task_queue, list) {
923 struct scsi_cmnd *cmd = task->uldd_task;
924
925 list_del_init(&task->list);
926
927 ASSIGN_SAS_TASK(cmd, NULL);
928 sas_free_task(task);
929 cmd->result = DID_ABORT << 16;
930 cmd->scsi_done(cmd);
931 }
932 spin_unlock_irqrestore(&core->task_queue_lock, flags);
933}
934
Darrick J. Wong396819f2007-01-11 14:15:20 -0800935/*
936 * Call the LLDD task abort routine directly. This function is intended for
937 * use by upper layers that need to tell the LLDD to abort a task.
938 */
939int __sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800940{
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800941 struct sas_internal *si =
942 to_sas_internal(task->dev->port->ha->core.shost->transportt);
943 unsigned long flags;
944 int res;
945
946 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800947 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
948 task->task_state_flags & SAS_TASK_STATE_DONE) {
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800949 spin_unlock_irqrestore(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800950 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800951 task);
952 return 0;
953 }
Darrick J. Wong396819f2007-01-11 14:15:20 -0800954 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800955 spin_unlock_irqrestore(&task->task_state_lock, flags);
956
957 if (!si->dft->lldd_abort_task)
958 return -ENODEV;
959
960 res = si->dft->lldd_abort_task(task);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800961
962 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800963 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
964 (res == TMF_RESP_FUNC_COMPLETE))
965 {
Darrick J. Wong396819f2007-01-11 14:15:20 -0800966 spin_unlock_irqrestore(&task->task_state_lock, flags);
967 task->task_done(task);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800968 return 0;
969 }
970
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800971 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
972 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
973 spin_unlock_irqrestore(&task->task_state_lock, flags);
974
975 return -EAGAIN;
976}
977
Darrick J. Wong396819f2007-01-11 14:15:20 -0800978/*
979 * Tell an upper layer that it needs to initiate an abort for a given task.
980 * This should only ever be called by an LLDD.
981 */
982void sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800983{
Darrick J. Wong396819f2007-01-11 14:15:20 -0800984 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800985
Darrick J. Wong396819f2007-01-11 14:15:20 -0800986 /* Escape for libsas internal commands */
987 if (!sc) {
988 if (!del_timer(&task->timer))
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800989 return;
Darrick J. Wong396819f2007-01-11 14:15:20 -0800990 task->timer.function(task->timer.data);
991 return;
992 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800993
Darrick J. Wong396819f2007-01-11 14:15:20 -0800994 scsi_req_abort_cmd(sc);
995 scsi_schedule_eh(sc->device->host);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800996}
997
James Bottomley2908d772006-08-29 09:22:51 -0500998EXPORT_SYMBOL_GPL(sas_queuecommand);
999EXPORT_SYMBOL_GPL(sas_target_alloc);
1000EXPORT_SYMBOL_GPL(sas_slave_configure);
1001EXPORT_SYMBOL_GPL(sas_slave_destroy);
1002EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1003EXPORT_SYMBOL_GPL(sas_change_queue_type);
1004EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong396819f2007-01-11 14:15:20 -08001005EXPORT_SYMBOL_GPL(__sas_task_abort);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -08001006EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -08001007EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001008EXPORT_SYMBOL_GPL(sas_phy_enable);
Darrick J. Wongad689232007-01-26 14:08:52 -08001009EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
Darrick J. Wonga9344e62007-01-29 23:48:19 -08001010EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);