blob: eac1d2dde743daf23016d2a1ce372485a6725131 [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))
James Bottomley2908d772006-08-29 09:22:51 -0500134 ta = TASK_ATTR_HOQ;
135 }
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. Wongad689232007-01-26 14:08:52 -0800424/* Attempt to send a target reset message to a device */
425int 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);
428 struct sas_phy *phy = find_local_sas_phy(dev);
429 int res;
430
431 res = sas_phy_reset(phy, 1);
432 if (res)
433 SAS_DPRINTK("Device reset of %s failed 0x%x\n",
434 phy->dev.kobj.k_name,
435 res);
436 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
437 return SUCCESS;
438
439 return FAILED;
440}
441
442/* Try to reset a device */
443static int try_to_reset_cmd_device(struct Scsi_Host *shost,
444 struct scsi_cmnd *cmd)
445{
446 if (!shost->hostt->eh_device_reset_handler)
447 return FAILED;
448
449 return shost->hostt->eh_device_reset_handler(cmd);
450}
451
452static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
453 struct list_head *work_q,
454 struct list_head *done_q)
455{
James Bottomley2908d772006-08-29 09:22:51 -0500456 struct scsi_cmnd *cmd, *n;
457 enum task_disposition res = TASK_IS_DONE;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800458 int tmf_resp, need_reset;
James Bottomley2908d772006-08-29 09:22:51 -0500459 struct sas_internal *i = to_sas_internal(shost->transportt);
Darrick J. Wongad689232007-01-26 14:08:52 -0800460 unsigned long flags;
461 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
James Bottomley2908d772006-08-29 09:22:51 -0500462
James Bottomley2908d772006-08-29 09:22:51 -0500463Again:
Darrick J. Wongad689232007-01-26 14:08:52 -0800464 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
James Bottomley2908d772006-08-29 09:22:51 -0500465 struct sas_task *task = TO_SAS_TASK(cmd);
Darrick J. Wongf4563932006-10-30 15:18:39 -0800466
Darrick J. Wongad689232007-01-26 14:08:52 -0800467 if (!task)
Darrick J. Wongf4563932006-10-30 15:18:39 -0800468 continue;
Darrick J. Wongad689232007-01-26 14:08:52 -0800469
470 list_del_init(&cmd->eh_entry);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800471
472 spin_lock_irqsave(&task->task_state_lock, flags);
473 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800474 spin_unlock_irqrestore(&task->task_state_lock, flags);
475
Darrick J. Wongf4563932006-10-30 15:18:39 -0800476 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500477 res = sas_scsi_find_task(task);
478
479 cmd->eh_eflags = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500480
481 switch (res) {
482 case TASK_IS_DONE:
483 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
484 task);
485 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800486 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800487 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500488 continue;
489 case TASK_IS_ABORTED:
490 SAS_DPRINTK("%s: task 0x%p is aborted\n",
491 __FUNCTION__, task);
492 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800493 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800494 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500495 continue;
496 case TASK_IS_AT_LU:
497 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
498 tmf_resp = sas_recover_lu(task->dev, cmd);
499 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
500 SAS_DPRINTK("dev %016llx LU %x is "
501 "recovered\n",
502 SAS_ADDR(task->dev),
503 cmd->device->lun);
504 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800505 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800506 try_to_reset_cmd_device(shost, cmd);
507 sas_scsi_clear_queue_lu(work_q, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500508 goto Again;
509 }
510 /* fallthrough */
511 case TASK_IS_NOT_AT_LU:
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800512 case TASK_ABORT_FAILED:
James Bottomley2908d772006-08-29 09:22:51 -0500513 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
514 task);
515 tmf_resp = sas_recover_I_T(task->dev);
516 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
517 SAS_DPRINTK("I_T %016llx recovered\n",
518 SAS_ADDR(task->dev->sas_addr));
519 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800520 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800521 try_to_reset_cmd_device(shost, cmd);
522 sas_scsi_clear_queue_I_T(work_q, task->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500523 goto Again;
524 }
525 /* Hammer time :-) */
526 if (i->dft->lldd_clear_nexus_port) {
527 struct asd_sas_port *port = task->dev->port;
528 SAS_DPRINTK("clearing nexus for port:%d\n",
529 port->id);
530 res = i->dft->lldd_clear_nexus_port(port);
531 if (res == TMF_RESP_FUNC_COMPLETE) {
532 SAS_DPRINTK("clear nexus port:%d "
533 "succeeded\n", port->id);
534 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800535 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800536 try_to_reset_cmd_device(shost, cmd);
537 sas_scsi_clear_queue_port(work_q,
James Bottomley2908d772006-08-29 09:22:51 -0500538 port);
539 goto Again;
540 }
541 }
542 if (i->dft->lldd_clear_nexus_ha) {
543 SAS_DPRINTK("clear nexus ha\n");
544 res = i->dft->lldd_clear_nexus_ha(ha);
545 if (res == TMF_RESP_FUNC_COMPLETE) {
546 SAS_DPRINTK("clear nexus ha "
547 "succeeded\n");
548 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800549 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800550 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500551 goto out;
552 }
553 }
554 /* If we are here -- this means that no amount
555 * of effort could recover from errors. Quite
556 * possibly the HA just disappeared.
557 */
558 SAS_DPRINTK("error from device %llx, LUN %x "
559 "couldn't be recovered in any way\n",
560 SAS_ADDR(task->dev->sas_addr),
561 cmd->device->lun);
562
563 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800564 if (need_reset)
Darrick J. Wongad689232007-01-26 14:08:52 -0800565 try_to_reset_cmd_device(shost, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500566 goto clear_q;
567 }
568 }
569out:
Darrick J. Wongad689232007-01-26 14:08:52 -0800570 return list_empty(work_q);
James Bottomley2908d772006-08-29 09:22:51 -0500571clear_q:
572 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
Darrick J. Wongad689232007-01-26 14:08:52 -0800573 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
James Bottomley2908d772006-08-29 09:22:51 -0500574 struct sas_task *task = TO_SAS_TASK(cmd);
575 list_del_init(&cmd->eh_entry);
576 task->task_done(task);
577 }
Darrick J. Wongad689232007-01-26 14:08:52 -0800578 return list_empty(work_q);
579}
580
581void sas_scsi_recover_host(struct Scsi_Host *shost)
582{
583 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
584 unsigned long flags;
585 LIST_HEAD(eh_work_q);
586
587 spin_lock_irqsave(shost->host_lock, flags);
588 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
589 spin_unlock_irqrestore(shost->host_lock, flags);
590
591 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
592 /*
593 * Deal with commands that still have SAS tasks (i.e. they didn't
594 * complete via the normal sas_task completion mechanism)
595 */
596 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
597 goto out;
598
599 /*
600 * Now deal with SCSI commands that completed ok but have a an error
601 * code (and hopefully sense data) attached. This is roughly what
602 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
603 * command we see here has no sas_task and is thus unknown to the HA.
604 */
605 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
606 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
607
608out:
609 scsi_eh_flush_done_q(&ha->eh_done_q);
610 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
611 return;
James Bottomley2908d772006-08-29 09:22:51 -0500612}
613
614enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
615{
616 struct sas_task *task = TO_SAS_TASK(cmd);
617 unsigned long flags;
618
619 if (!task) {
Darrick J. Wong6d4dcd42007-01-11 14:15:00 -0800620 cmd->timeout_per_command /= 2;
621 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
622 cmd, task, (cmd->timeout_per_command ?
623 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
624 if (!cmd->timeout_per_command)
625 return EH_NOT_HANDLED;
626 return EH_RESET_TIMER;
James Bottomley2908d772006-08-29 09:22:51 -0500627 }
628
629 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800630 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
James Bottomley2908d772006-08-29 09:22:51 -0500631 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
632 spin_unlock_irqrestore(&task->task_state_lock, flags);
633 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
634 cmd, task);
635 return EH_HANDLED;
636 }
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800637 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
638 spin_unlock_irqrestore(&task->task_state_lock, flags);
639 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
640 "EH_RESET_TIMER\n",
641 cmd, task);
642 return EH_RESET_TIMER;
643 }
James Bottomley2908d772006-08-29 09:22:51 -0500644 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
645 spin_unlock_irqrestore(&task->task_state_lock, flags);
646
647 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
648 cmd, task);
649
650 return EH_NOT_HANDLED;
651}
652
653struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
654{
655 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
656 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
657 struct domain_device *found_dev = NULL;
658 int i;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800659 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500660
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800661 spin_lock_irqsave(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500662 for (i = 0; i < ha->num_phys; i++) {
663 struct asd_sas_port *port = ha->sas_port[i];
664 struct domain_device *dev;
665
666 spin_lock(&port->dev_list_lock);
667 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
668 if (rphy == dev->rphy) {
669 found_dev = dev;
670 spin_unlock(&port->dev_list_lock);
671 goto found;
672 }
673 }
674 spin_unlock(&port->dev_list_lock);
675 }
676 found:
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800677 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500678
679 return found_dev;
680}
681
682static inline struct domain_device *sas_find_target(struct scsi_target *starget)
683{
684 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
685
686 return sas_find_dev_by_rphy(rphy);
687}
688
689int sas_target_alloc(struct scsi_target *starget)
690{
691 struct domain_device *found_dev = sas_find_target(starget);
692
693 if (!found_dev)
694 return -ENODEV;
695
696 starget->hostdata = found_dev;
697 return 0;
698}
699
700#define SAS_DEF_QD 32
701#define SAS_MAX_QD 64
702
703int sas_slave_configure(struct scsi_device *scsi_dev)
704{
705 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
706 struct sas_ha_struct *sas_ha;
707
708 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
709
710 sas_ha = dev->port->ha;
711
712 sas_read_port_mode_page(scsi_dev);
713
714 if (scsi_dev->tagged_supported) {
715 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
716 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
717 } else {
718 SAS_DPRINTK("device %llx, LUN %x doesn't support "
719 "TCQ\n", SAS_ADDR(dev->sas_addr),
720 scsi_dev->lun);
721 scsi_dev->tagged_supported = 0;
722 scsi_set_tag_type(scsi_dev, 0);
723 scsi_deactivate_tcq(scsi_dev, 1);
724 }
725
726 return 0;
727}
728
729void sas_slave_destroy(struct scsi_device *scsi_dev)
730{
731}
732
733int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
734{
735 int res = min(new_depth, SAS_MAX_QD);
736
737 if (scsi_dev->tagged_supported)
738 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
739 res);
740 else {
741 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
742 sas_printk("device %llx LUN %x queue depth changed to 1\n",
743 SAS_ADDR(dev->sas_addr),
744 scsi_dev->lun);
745 scsi_adjust_queue_depth(scsi_dev, 0, 1);
746 res = 1;
747 }
748
749 return res;
750}
751
752int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
753{
754 if (!scsi_dev->tagged_supported)
755 return 0;
756
757 scsi_deactivate_tcq(scsi_dev, 1);
758
759 scsi_set_tag_type(scsi_dev, qt);
760 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
761
762 return qt;
763}
764
765int sas_bios_param(struct scsi_device *scsi_dev,
766 struct block_device *bdev,
767 sector_t capacity, int *hsc)
768{
769 hsc[0] = 255;
770 hsc[1] = 63;
771 sector_div(capacity, 255*63);
772 hsc[2] = capacity;
773
774 return 0;
775}
776
777/* ---------- Task Collector Thread implementation ---------- */
778
779static void sas_queue(struct sas_ha_struct *sas_ha)
780{
781 struct scsi_core *core = &sas_ha->core;
782 unsigned long flags;
783 LIST_HEAD(q);
784 int can_queue;
785 int res;
786 struct sas_internal *i = to_sas_internal(core->shost->transportt);
787
788 spin_lock_irqsave(&core->task_queue_lock, flags);
789 while (!core->queue_thread_kill &&
790 !list_empty(&core->task_queue)) {
791
792 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
793 if (can_queue >= 0) {
794 can_queue = core->task_queue_size;
795 list_splice_init(&core->task_queue, &q);
796 } else {
797 struct list_head *a, *n;
798
799 can_queue = sas_ha->lldd_queue_size;
800 list_for_each_safe(a, n, &core->task_queue) {
801 list_move_tail(a, &q);
802 if (--can_queue == 0)
803 break;
804 }
805 can_queue = sas_ha->lldd_queue_size;
806 }
807 core->task_queue_size -= can_queue;
808 spin_unlock_irqrestore(&core->task_queue_lock, flags);
809 {
810 struct sas_task *task = list_entry(q.next,
811 struct sas_task,
812 list);
813 list_del_init(&q);
814 res = i->dft->lldd_execute_task(task, can_queue,
815 GFP_KERNEL);
816 if (unlikely(res))
817 __list_add(&q, task->list.prev, &task->list);
818 }
819 spin_lock_irqsave(&core->task_queue_lock, flags);
820 if (res) {
821 list_splice_init(&q, &core->task_queue); /*at head*/
822 core->task_queue_size += can_queue;
823 }
824 }
825 spin_unlock_irqrestore(&core->task_queue_lock, flags);
826}
827
828static DECLARE_COMPLETION(queue_th_comp);
829
830/**
831 * sas_queue_thread -- The Task Collector thread
832 * @_sas_ha: pointer to struct sas_ha
833 */
834static int sas_queue_thread(void *_sas_ha)
835{
836 struct sas_ha_struct *sas_ha = _sas_ha;
837 struct scsi_core *core = &sas_ha->core;
838
839 daemonize("sas_queue_%d", core->shost->host_no);
840 current->flags |= PF_NOFREEZE;
841
842 complete(&queue_th_comp);
843
844 while (1) {
845 down_interruptible(&core->queue_thread_sema);
846 sas_queue(sas_ha);
847 if (core->queue_thread_kill)
848 break;
849 }
850
851 complete(&queue_th_comp);
852
853 return 0;
854}
855
856int sas_init_queue(struct sas_ha_struct *sas_ha)
857{
858 int res;
859 struct scsi_core *core = &sas_ha->core;
860
861 spin_lock_init(&core->task_queue_lock);
862 core->task_queue_size = 0;
863 INIT_LIST_HEAD(&core->task_queue);
864 init_MUTEX_LOCKED(&core->queue_thread_sema);
865
866 res = kernel_thread(sas_queue_thread, sas_ha, 0);
867 if (res >= 0)
868 wait_for_completion(&queue_th_comp);
869
870 return res < 0 ? res : 0;
871}
872
873void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
874{
875 unsigned long flags;
876 struct scsi_core *core = &sas_ha->core;
877 struct sas_task *task, *n;
878
879 init_completion(&queue_th_comp);
880 core->queue_thread_kill = 1;
881 up(&core->queue_thread_sema);
882 wait_for_completion(&queue_th_comp);
883
884 if (!list_empty(&core->task_queue))
885 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
886 SAS_ADDR(sas_ha->sas_addr));
887
888 spin_lock_irqsave(&core->task_queue_lock, flags);
889 list_for_each_entry_safe(task, n, &core->task_queue, list) {
890 struct scsi_cmnd *cmd = task->uldd_task;
891
892 list_del_init(&task->list);
893
894 ASSIGN_SAS_TASK(cmd, NULL);
895 sas_free_task(task);
896 cmd->result = DID_ABORT << 16;
897 cmd->scsi_done(cmd);
898 }
899 spin_unlock_irqrestore(&core->task_queue_lock, flags);
900}
901
Darrick J. Wong396819f2007-01-11 14:15:20 -0800902/*
903 * Call the LLDD task abort routine directly. This function is intended for
904 * use by upper layers that need to tell the LLDD to abort a task.
905 */
906int __sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800907{
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800908 struct sas_internal *si =
909 to_sas_internal(task->dev->port->ha->core.shost->transportt);
910 unsigned long flags;
911 int res;
912
913 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800914 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
915 task->task_state_flags & SAS_TASK_STATE_DONE) {
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800916 spin_unlock_irqrestore(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800917 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800918 task);
919 return 0;
920 }
Darrick J. Wong396819f2007-01-11 14:15:20 -0800921 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800922 spin_unlock_irqrestore(&task->task_state_lock, flags);
923
924 if (!si->dft->lldd_abort_task)
925 return -ENODEV;
926
927 res = si->dft->lldd_abort_task(task);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800928
929 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800930 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
931 (res == TMF_RESP_FUNC_COMPLETE))
932 {
Darrick J. Wong396819f2007-01-11 14:15:20 -0800933 spin_unlock_irqrestore(&task->task_state_lock, flags);
934 task->task_done(task);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800935 return 0;
936 }
937
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800938 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
939 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
940 spin_unlock_irqrestore(&task->task_state_lock, flags);
941
942 return -EAGAIN;
943}
944
Darrick J. Wong396819f2007-01-11 14:15:20 -0800945/*
946 * Tell an upper layer that it needs to initiate an abort for a given task.
947 * This should only ever be called by an LLDD.
948 */
949void sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800950{
Darrick J. Wong396819f2007-01-11 14:15:20 -0800951 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800952
Darrick J. Wong396819f2007-01-11 14:15:20 -0800953 /* Escape for libsas internal commands */
954 if (!sc) {
955 if (!del_timer(&task->timer))
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800956 return;
Darrick J. Wong396819f2007-01-11 14:15:20 -0800957 task->timer.function(task->timer.data);
958 return;
959 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800960
Darrick J. Wong396819f2007-01-11 14:15:20 -0800961 scsi_req_abort_cmd(sc);
962 scsi_schedule_eh(sc->device->host);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800963}
964
James Bottomley2908d772006-08-29 09:22:51 -0500965EXPORT_SYMBOL_GPL(sas_queuecommand);
966EXPORT_SYMBOL_GPL(sas_target_alloc);
967EXPORT_SYMBOL_GPL(sas_slave_configure);
968EXPORT_SYMBOL_GPL(sas_slave_destroy);
969EXPORT_SYMBOL_GPL(sas_change_queue_depth);
970EXPORT_SYMBOL_GPL(sas_change_queue_type);
971EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800972EXPORT_SYMBOL_GPL(__sas_task_abort);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800973EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -0800974EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800975EXPORT_SYMBOL_GPL(sas_phy_enable);
Darrick J. Wongad689232007-01-26 14:08:52 -0800976EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);