blob: f867455dd339a98d521823dc56fb4a5f1da8426a [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"
James Bottomley2908d772006-08-29 09:22:51 -050037
38#include <linux/err.h>
39#include <linux/blkdev.h>
40#include <linux/scatterlist.h>
41
42/* ---------- SCSI Host glue ---------- */
43
44#define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
45#define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
46
47static void sas_scsi_task_done(struct sas_task *task)
48{
49 struct task_status_struct *ts = &task->task_status;
50 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wongf4563932006-10-30 15:18:39 -080051 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
James Bottomley2908d772006-08-29 09:22:51 -050052 unsigned ts_flags = task->task_state_flags;
53 int hs = 0, stat = 0;
54
55 if (unlikely(!sc)) {
56 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
57 list_del_init(&task->list);
58 sas_free_task(task);
59 return;
60 }
61
62 if (ts->resp == SAS_TASK_UNDELIVERED) {
63 /* transport error */
64 hs = DID_NO_CONNECT;
65 } else { /* ts->resp == SAS_TASK_COMPLETE */
66 /* task delivered, what happened afterwards? */
67 switch (ts->stat) {
68 case SAS_DEV_NO_RESPONSE:
69 case SAS_INTERRUPTED:
70 case SAS_PHY_DOWN:
71 case SAS_NAK_R_ERR:
72 case SAS_OPEN_TO:
73 hs = DID_NO_CONNECT;
74 break;
75 case SAS_DATA_UNDERRUN:
76 sc->resid = ts->residual;
77 if (sc->request_bufflen - sc->resid < sc->underflow)
78 hs = DID_ERROR;
79 break;
80 case SAS_DATA_OVERRUN:
81 hs = DID_ERROR;
82 break;
83 case SAS_QUEUE_FULL:
84 hs = DID_SOFT_ERROR; /* retry */
85 break;
86 case SAS_DEVICE_UNKNOWN:
87 hs = DID_BAD_TARGET;
88 break;
89 case SAS_SG_ERR:
90 hs = DID_PARITY;
91 break;
92 case SAS_OPEN_REJECT:
93 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
94 hs = DID_SOFT_ERROR; /* retry */
95 else
96 hs = DID_ERROR;
97 break;
98 case SAS_PROTO_RESPONSE:
99 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
100 "task; please report this\n",
101 task->dev->port->ha->sas_ha_name);
102 break;
103 case SAS_ABORTED_TASK:
104 hs = DID_ABORT;
105 break;
106 case SAM_CHECK_COND:
107 memcpy(sc->sense_buffer, ts->buf,
108 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
109 stat = SAM_CHECK_COND;
110 break;
111 default:
112 stat = ts->stat;
113 break;
114 }
115 }
116 ASSIGN_SAS_TASK(sc, NULL);
117 sc->result = (hs << 16) | stat;
118 list_del_init(&task->list);
119 sas_free_task(task);
120 /* This is very ugly but this is how SCSI Core works. */
121 if (ts_flags & SAS_TASK_STATE_ABORTED)
Darrick J. Wongf4563932006-10-30 15:18:39 -0800122 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
James Bottomley2908d772006-08-29 09:22:51 -0500123 else
124 sc->scsi_done(sc);
125}
126
127static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
128{
129 enum task_attribute ta = TASK_ATTR_SIMPLE;
130 if (cmd->request && blk_rq_tagged(cmd->request)) {
131 if (cmd->device->ordered_tags &&
Jeff Garzik1bdfd552006-09-30 21:28:22 -0400132 (cmd->request->cmd_flags & REQ_HARDBARRIER))
James Bottomley2908d772006-08-29 09:22:51 -0500133 ta = TASK_ATTR_HOQ;
134 }
135 return ta;
136}
137
138static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
139 struct domain_device *dev,
Al Viro3cc27542006-09-25 02:55:40 +0100140 gfp_t gfp_flags)
James Bottomley2908d772006-08-29 09:22:51 -0500141{
142 struct sas_task *task = sas_alloc_task(gfp_flags);
143 struct scsi_lun lun;
144
145 if (!task)
146 return NULL;
147
148 *(u32 *)cmd->sense_buffer = 0;
149 task->uldd_task = cmd;
150 ASSIGN_SAS_TASK(cmd, task);
151
152 task->dev = dev;
153 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
154
155 task->ssp_task.retry_count = 1;
156 int_to_scsilun(cmd->device->lun, &lun);
157 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
158 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
159 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
160
161 task->scatter = cmd->request_buffer;
162 task->num_scatter = cmd->use_sg;
163 task->total_xfer_len = cmd->request_bufflen;
164 task->data_dir = cmd->sc_data_direction;
165
166 task->task_done = sas_scsi_task_done;
167
168 return task;
169}
170
171static int sas_queue_up(struct sas_task *task)
172{
173 struct sas_ha_struct *sas_ha = task->dev->port->ha;
174 struct scsi_core *core = &sas_ha->core;
175 unsigned long flags;
176 LIST_HEAD(list);
177
178 spin_lock_irqsave(&core->task_queue_lock, flags);
179 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
180 spin_unlock_irqrestore(&core->task_queue_lock, flags);
181 return -SAS_QUEUE_FULL;
182 }
183 list_add_tail(&task->list, &core->task_queue);
184 core->task_queue_size += 1;
185 spin_unlock_irqrestore(&core->task_queue_lock, flags);
186 up(&core->queue_thread_sema);
187
188 return 0;
189}
190
191/**
192 * sas_queuecommand -- Enqueue a command for processing
193 * @parameters: See SCSI Core documentation
194 *
195 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
196 * call us without holding an IRQ spinlock...
197 */
198int sas_queuecommand(struct scsi_cmnd *cmd,
199 void (*scsi_done)(struct scsi_cmnd *))
200{
201 int res = 0;
202 struct domain_device *dev = cmd_to_domain_dev(cmd);
203 struct Scsi_Host *host = cmd->device->host;
204 struct sas_internal *i = to_sas_internal(host->transportt);
205
206 spin_unlock_irq(host->host_lock);
207
208 {
209 struct sas_ha_struct *sas_ha = dev->port->ha;
210 struct sas_task *task;
211
212 res = -ENOMEM;
213 task = sas_create_task(cmd, dev, GFP_ATOMIC);
214 if (!task)
215 goto out;
216
217 cmd->scsi_done = scsi_done;
218 /* Queue up, Direct Mode or Task Collector Mode. */
219 if (sas_ha->lldd_max_execute_num < 2)
220 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
221 else
222 res = sas_queue_up(task);
223
224 /* Examine */
225 if (res) {
226 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
227 ASSIGN_SAS_TASK(cmd, NULL);
228 sas_free_task(task);
229 if (res == -SAS_QUEUE_FULL) {
230 cmd->result = DID_SOFT_ERROR << 16; /* retry */
231 res = 0;
232 scsi_done(cmd);
233 }
234 goto out;
235 }
236 }
237out:
238 spin_lock_irq(host->host_lock);
239 return res;
240}
241
242static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
243{
244 struct scsi_cmnd *cmd, *n;
245
246 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
247 if (cmd == my_cmd)
248 list_del_init(&cmd->eh_entry);
249 }
250}
251
252static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
253 struct domain_device *dev)
254{
255 struct scsi_cmnd *cmd, *n;
256
257 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
258 struct domain_device *x = cmd_to_domain_dev(cmd);
259
260 if (x == dev)
261 list_del_init(&cmd->eh_entry);
262 }
263}
264
265static void sas_scsi_clear_queue_port(struct list_head *error_q,
266 struct asd_sas_port *port)
267{
268 struct scsi_cmnd *cmd, *n;
269
270 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
271 struct domain_device *dev = cmd_to_domain_dev(cmd);
272 struct asd_sas_port *x = dev->port;
273
274 if (x == port)
275 list_del_init(&cmd->eh_entry);
276 }
277}
278
279enum task_disposition {
280 TASK_IS_DONE,
281 TASK_IS_ABORTED,
282 TASK_IS_AT_LU,
283 TASK_IS_NOT_AT_LU,
284};
285
286static enum task_disposition sas_scsi_find_task(struct sas_task *task)
287{
288 struct sas_ha_struct *ha = task->dev->port->ha;
289 unsigned long flags;
290 int i, res;
291 struct sas_internal *si =
292 to_sas_internal(task->dev->port->ha->core.shost->transportt);
293
294 if (ha->lldd_max_execute_num > 1) {
295 struct scsi_core *core = &ha->core;
296 struct sas_task *t, *n;
297
298 spin_lock_irqsave(&core->task_queue_lock, flags);
299 list_for_each_entry_safe(t, n, &core->task_queue, list) {
300 if (task == t) {
301 list_del_init(&t->list);
302 spin_unlock_irqrestore(&core->task_queue_lock,
303 flags);
304 SAS_DPRINTK("%s: task 0x%p aborted from "
305 "task_queue\n",
306 __FUNCTION__, task);
307 return TASK_IS_ABORTED;
308 }
309 }
310 spin_unlock_irqrestore(&core->task_queue_lock, flags);
311 }
312
313 for (i = 0; i < 5; i++) {
314 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
315 res = si->dft->lldd_abort_task(task);
316
317 spin_lock_irqsave(&task->task_state_lock, flags);
318 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
319 spin_unlock_irqrestore(&task->task_state_lock, flags);
320 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
321 task);
322 return TASK_IS_DONE;
323 }
324 spin_unlock_irqrestore(&task->task_state_lock, flags);
325
326 if (res == TMF_RESP_FUNC_COMPLETE) {
327 SAS_DPRINTK("%s: task 0x%p is aborted\n",
328 __FUNCTION__, task);
329 return TASK_IS_ABORTED;
330 } else if (si->dft->lldd_query_task) {
331 SAS_DPRINTK("%s: querying task 0x%p\n",
332 __FUNCTION__, task);
333 res = si->dft->lldd_query_task(task);
334 if (res == TMF_RESP_FUNC_SUCC) {
335 SAS_DPRINTK("%s: task 0x%p at LU\n",
336 __FUNCTION__, task);
337 return TASK_IS_AT_LU;
338 } else if (res == TMF_RESP_FUNC_COMPLETE) {
339 SAS_DPRINTK("%s: task 0x%p not at LU\n",
340 __FUNCTION__, task);
341 return TASK_IS_NOT_AT_LU;
342 }
343 }
344 }
345 return res;
346}
347
348static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
349{
350 int res = TMF_RESP_FUNC_FAILED;
351 struct scsi_lun lun;
352 struct sas_internal *i =
353 to_sas_internal(dev->port->ha->core.shost->transportt);
354
355 int_to_scsilun(cmd->device->lun, &lun);
356
357 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
358 SAS_ADDR(dev->sas_addr),
359 cmd->device->lun);
360
361 if (i->dft->lldd_abort_task_set)
362 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
363
364 if (res == TMF_RESP_FUNC_FAILED) {
365 if (i->dft->lldd_clear_task_set)
366 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
367 }
368
369 if (res == TMF_RESP_FUNC_FAILED) {
370 if (i->dft->lldd_lu_reset)
371 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
372 }
373
374 return res;
375}
376
377static int sas_recover_I_T(struct domain_device *dev)
378{
379 int res = TMF_RESP_FUNC_FAILED;
380 struct sas_internal *i =
381 to_sas_internal(dev->port->ha->core.shost->transportt);
382
383 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
384 SAS_ADDR(dev->sas_addr));
385
386 if (i->dft->lldd_I_T_nexus_reset)
387 res = i->dft->lldd_I_T_nexus_reset(dev);
388
389 return res;
390}
391
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800392static int eh_reset_phy_helper(struct sas_phy *phy)
393{
394 int tmf_resp;
395
396 tmf_resp = sas_phy_reset(phy, 1);
397 if (tmf_resp)
398 SAS_DPRINTK("Hard reset of phy %d failed 0x%x\n",
399 phy->identify.phy_identifier,
400 tmf_resp);
401
402 return tmf_resp;
403}
404
James Bottomley2908d772006-08-29 09:22:51 -0500405void sas_scsi_recover_host(struct Scsi_Host *shost)
406{
407 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
408 unsigned long flags;
409 LIST_HEAD(error_q);
410 struct scsi_cmnd *cmd, *n;
411 enum task_disposition res = TASK_IS_DONE;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800412 int tmf_resp, need_reset;
James Bottomley2908d772006-08-29 09:22:51 -0500413 struct sas_internal *i = to_sas_internal(shost->transportt);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800414 struct sas_phy *task_sas_phy = NULL;
James Bottomley2908d772006-08-29 09:22:51 -0500415
416 spin_lock_irqsave(shost->host_lock, flags);
417 list_splice_init(&shost->eh_cmd_q, &error_q);
418 spin_unlock_irqrestore(shost->host_lock, flags);
419
420 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
421
422 /* All tasks on this list were marked SAS_TASK_STATE_ABORTED
423 * by sas_scsi_timed_out() callback.
424 */
425Again:
426 SAS_DPRINTK("going over list...\n");
427 list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
428 struct sas_task *task = TO_SAS_TASK(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500429 list_del_init(&cmd->eh_entry);
Darrick J. Wongf4563932006-10-30 15:18:39 -0800430
431 if (!task) {
432 SAS_DPRINTK("%s: taskless cmd?!\n", __FUNCTION__);
433 continue;
434 }
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800435
436 spin_lock_irqsave(&task->task_state_lock, flags);
437 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
438 if (need_reset)
439 task_sas_phy = task->dev->port->phy;
440 spin_unlock_irqrestore(&task->task_state_lock, flags);
441
Darrick J. Wongf4563932006-10-30 15:18:39 -0800442 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500443 res = sas_scsi_find_task(task);
444
445 cmd->eh_eflags = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500446
447 switch (res) {
448 case TASK_IS_DONE:
449 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
450 task);
451 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800452 if (need_reset)
453 eh_reset_phy_helper(task_sas_phy);
James Bottomley2908d772006-08-29 09:22:51 -0500454 continue;
455 case TASK_IS_ABORTED:
456 SAS_DPRINTK("%s: task 0x%p is aborted\n",
457 __FUNCTION__, task);
458 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800459 if (need_reset)
460 eh_reset_phy_helper(task_sas_phy);
James Bottomley2908d772006-08-29 09:22:51 -0500461 continue;
462 case TASK_IS_AT_LU:
463 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
464 tmf_resp = sas_recover_lu(task->dev, cmd);
465 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
466 SAS_DPRINTK("dev %016llx LU %x is "
467 "recovered\n",
468 SAS_ADDR(task->dev),
469 cmd->device->lun);
470 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800471 if (need_reset)
472 eh_reset_phy_helper(task_sas_phy);
James Bottomley2908d772006-08-29 09:22:51 -0500473 sas_scsi_clear_queue_lu(&error_q, cmd);
474 goto Again;
475 }
476 /* fallthrough */
477 case TASK_IS_NOT_AT_LU:
478 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
479 task);
480 tmf_resp = sas_recover_I_T(task->dev);
481 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
482 SAS_DPRINTK("I_T %016llx recovered\n",
483 SAS_ADDR(task->dev->sas_addr));
484 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800485 if (need_reset)
486 eh_reset_phy_helper(task_sas_phy);
James Bottomley2908d772006-08-29 09:22:51 -0500487 sas_scsi_clear_queue_I_T(&error_q, task->dev);
488 goto Again;
489 }
490 /* Hammer time :-) */
491 if (i->dft->lldd_clear_nexus_port) {
492 struct asd_sas_port *port = task->dev->port;
493 SAS_DPRINTK("clearing nexus for port:%d\n",
494 port->id);
495 res = i->dft->lldd_clear_nexus_port(port);
496 if (res == TMF_RESP_FUNC_COMPLETE) {
497 SAS_DPRINTK("clear nexus port:%d "
498 "succeeded\n", port->id);
499 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800500 if (need_reset)
501 eh_reset_phy_helper(task_sas_phy);
James Bottomley2908d772006-08-29 09:22:51 -0500502 sas_scsi_clear_queue_port(&error_q,
503 port);
504 goto Again;
505 }
506 }
507 if (i->dft->lldd_clear_nexus_ha) {
508 SAS_DPRINTK("clear nexus ha\n");
509 res = i->dft->lldd_clear_nexus_ha(ha);
510 if (res == TMF_RESP_FUNC_COMPLETE) {
511 SAS_DPRINTK("clear nexus ha "
512 "succeeded\n");
513 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800514 if (need_reset)
515 eh_reset_phy_helper(task_sas_phy);
James Bottomley2908d772006-08-29 09:22:51 -0500516 goto out;
517 }
518 }
519 /* If we are here -- this means that no amount
520 * of effort could recover from errors. Quite
521 * possibly the HA just disappeared.
522 */
523 SAS_DPRINTK("error from device %llx, LUN %x "
524 "couldn't be recovered in any way\n",
525 SAS_ADDR(task->dev->sas_addr),
526 cmd->device->lun);
527
528 task->task_done(task);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800529 if (need_reset)
530 eh_reset_phy_helper(task_sas_phy);
James Bottomley2908d772006-08-29 09:22:51 -0500531 goto clear_q;
532 }
533 }
534out:
Darrick J. Wongf4563932006-10-30 15:18:39 -0800535 scsi_eh_flush_done_q(&ha->eh_done_q);
James Bottomley2908d772006-08-29 09:22:51 -0500536 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
537 return;
538clear_q:
539 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
540 list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
541 struct sas_task *task = TO_SAS_TASK(cmd);
542 list_del_init(&cmd->eh_entry);
543 task->task_done(task);
544 }
545}
546
547enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
548{
549 struct sas_task *task = TO_SAS_TASK(cmd);
550 unsigned long flags;
551
552 if (!task) {
Darrick J. Wong6d4dcd42007-01-11 14:15:00 -0800553 cmd->timeout_per_command /= 2;
554 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
555 cmd, task, (cmd->timeout_per_command ?
556 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
557 if (!cmd->timeout_per_command)
558 return EH_NOT_HANDLED;
559 return EH_RESET_TIMER;
James Bottomley2908d772006-08-29 09:22:51 -0500560 }
561
562 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800563 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
James Bottomley2908d772006-08-29 09:22:51 -0500564 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
565 spin_unlock_irqrestore(&task->task_state_lock, flags);
566 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
567 cmd, task);
568 return EH_HANDLED;
569 }
Darrick J. Wongb218a0d2007-01-11 14:14:55 -0800570 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
571 spin_unlock_irqrestore(&task->task_state_lock, flags);
572 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
573 "EH_RESET_TIMER\n",
574 cmd, task);
575 return EH_RESET_TIMER;
576 }
James Bottomley2908d772006-08-29 09:22:51 -0500577 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
578 spin_unlock_irqrestore(&task->task_state_lock, flags);
579
580 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
581 cmd, task);
582
583 return EH_NOT_HANDLED;
584}
585
586struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
587{
588 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
589 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
590 struct domain_device *found_dev = NULL;
591 int i;
592
593 spin_lock(&ha->phy_port_lock);
594 for (i = 0; i < ha->num_phys; i++) {
595 struct asd_sas_port *port = ha->sas_port[i];
596 struct domain_device *dev;
597
598 spin_lock(&port->dev_list_lock);
599 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
600 if (rphy == dev->rphy) {
601 found_dev = dev;
602 spin_unlock(&port->dev_list_lock);
603 goto found;
604 }
605 }
606 spin_unlock(&port->dev_list_lock);
607 }
608 found:
609 spin_unlock(&ha->phy_port_lock);
610
611 return found_dev;
612}
613
614static inline struct domain_device *sas_find_target(struct scsi_target *starget)
615{
616 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
617
618 return sas_find_dev_by_rphy(rphy);
619}
620
621int sas_target_alloc(struct scsi_target *starget)
622{
623 struct domain_device *found_dev = sas_find_target(starget);
624
625 if (!found_dev)
626 return -ENODEV;
627
628 starget->hostdata = found_dev;
629 return 0;
630}
631
632#define SAS_DEF_QD 32
633#define SAS_MAX_QD 64
634
635int sas_slave_configure(struct scsi_device *scsi_dev)
636{
637 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
638 struct sas_ha_struct *sas_ha;
639
640 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
641
642 sas_ha = dev->port->ha;
643
644 sas_read_port_mode_page(scsi_dev);
645
646 if (scsi_dev->tagged_supported) {
647 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
648 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
649 } else {
650 SAS_DPRINTK("device %llx, LUN %x doesn't support "
651 "TCQ\n", SAS_ADDR(dev->sas_addr),
652 scsi_dev->lun);
653 scsi_dev->tagged_supported = 0;
654 scsi_set_tag_type(scsi_dev, 0);
655 scsi_deactivate_tcq(scsi_dev, 1);
656 }
657
658 return 0;
659}
660
661void sas_slave_destroy(struct scsi_device *scsi_dev)
662{
663}
664
665int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
666{
667 int res = min(new_depth, SAS_MAX_QD);
668
669 if (scsi_dev->tagged_supported)
670 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
671 res);
672 else {
673 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
674 sas_printk("device %llx LUN %x queue depth changed to 1\n",
675 SAS_ADDR(dev->sas_addr),
676 scsi_dev->lun);
677 scsi_adjust_queue_depth(scsi_dev, 0, 1);
678 res = 1;
679 }
680
681 return res;
682}
683
684int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
685{
686 if (!scsi_dev->tagged_supported)
687 return 0;
688
689 scsi_deactivate_tcq(scsi_dev, 1);
690
691 scsi_set_tag_type(scsi_dev, qt);
692 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
693
694 return qt;
695}
696
697int sas_bios_param(struct scsi_device *scsi_dev,
698 struct block_device *bdev,
699 sector_t capacity, int *hsc)
700{
701 hsc[0] = 255;
702 hsc[1] = 63;
703 sector_div(capacity, 255*63);
704 hsc[2] = capacity;
705
706 return 0;
707}
708
709/* ---------- Task Collector Thread implementation ---------- */
710
711static void sas_queue(struct sas_ha_struct *sas_ha)
712{
713 struct scsi_core *core = &sas_ha->core;
714 unsigned long flags;
715 LIST_HEAD(q);
716 int can_queue;
717 int res;
718 struct sas_internal *i = to_sas_internal(core->shost->transportt);
719
720 spin_lock_irqsave(&core->task_queue_lock, flags);
721 while (!core->queue_thread_kill &&
722 !list_empty(&core->task_queue)) {
723
724 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
725 if (can_queue >= 0) {
726 can_queue = core->task_queue_size;
727 list_splice_init(&core->task_queue, &q);
728 } else {
729 struct list_head *a, *n;
730
731 can_queue = sas_ha->lldd_queue_size;
732 list_for_each_safe(a, n, &core->task_queue) {
733 list_move_tail(a, &q);
734 if (--can_queue == 0)
735 break;
736 }
737 can_queue = sas_ha->lldd_queue_size;
738 }
739 core->task_queue_size -= can_queue;
740 spin_unlock_irqrestore(&core->task_queue_lock, flags);
741 {
742 struct sas_task *task = list_entry(q.next,
743 struct sas_task,
744 list);
745 list_del_init(&q);
746 res = i->dft->lldd_execute_task(task, can_queue,
747 GFP_KERNEL);
748 if (unlikely(res))
749 __list_add(&q, task->list.prev, &task->list);
750 }
751 spin_lock_irqsave(&core->task_queue_lock, flags);
752 if (res) {
753 list_splice_init(&q, &core->task_queue); /*at head*/
754 core->task_queue_size += can_queue;
755 }
756 }
757 spin_unlock_irqrestore(&core->task_queue_lock, flags);
758}
759
760static DECLARE_COMPLETION(queue_th_comp);
761
762/**
763 * sas_queue_thread -- The Task Collector thread
764 * @_sas_ha: pointer to struct sas_ha
765 */
766static int sas_queue_thread(void *_sas_ha)
767{
768 struct sas_ha_struct *sas_ha = _sas_ha;
769 struct scsi_core *core = &sas_ha->core;
770
771 daemonize("sas_queue_%d", core->shost->host_no);
772 current->flags |= PF_NOFREEZE;
773
774 complete(&queue_th_comp);
775
776 while (1) {
777 down_interruptible(&core->queue_thread_sema);
778 sas_queue(sas_ha);
779 if (core->queue_thread_kill)
780 break;
781 }
782
783 complete(&queue_th_comp);
784
785 return 0;
786}
787
788int sas_init_queue(struct sas_ha_struct *sas_ha)
789{
790 int res;
791 struct scsi_core *core = &sas_ha->core;
792
793 spin_lock_init(&core->task_queue_lock);
794 core->task_queue_size = 0;
795 INIT_LIST_HEAD(&core->task_queue);
796 init_MUTEX_LOCKED(&core->queue_thread_sema);
797
798 res = kernel_thread(sas_queue_thread, sas_ha, 0);
799 if (res >= 0)
800 wait_for_completion(&queue_th_comp);
801
802 return res < 0 ? res : 0;
803}
804
805void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
806{
807 unsigned long flags;
808 struct scsi_core *core = &sas_ha->core;
809 struct sas_task *task, *n;
810
811 init_completion(&queue_th_comp);
812 core->queue_thread_kill = 1;
813 up(&core->queue_thread_sema);
814 wait_for_completion(&queue_th_comp);
815
816 if (!list_empty(&core->task_queue))
817 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
818 SAS_ADDR(sas_ha->sas_addr));
819
820 spin_lock_irqsave(&core->task_queue_lock, flags);
821 list_for_each_entry_safe(task, n, &core->task_queue, list) {
822 struct scsi_cmnd *cmd = task->uldd_task;
823
824 list_del_init(&task->list);
825
826 ASSIGN_SAS_TASK(cmd, NULL);
827 sas_free_task(task);
828 cmd->result = DID_ABORT << 16;
829 cmd->scsi_done(cmd);
830 }
831 spin_unlock_irqrestore(&core->task_queue_lock, flags);
832}
833
Darrick J. Wong396819f2007-01-11 14:15:20 -0800834/*
835 * Call the LLDD task abort routine directly. This function is intended for
836 * use by upper layers that need to tell the LLDD to abort a task.
837 */
838int __sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800839{
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800840 struct sas_internal *si =
841 to_sas_internal(task->dev->port->ha->core.shost->transportt);
842 unsigned long flags;
843 int res;
844
845 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800846 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
847 task->task_state_flags & SAS_TASK_STATE_DONE) {
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800848 spin_unlock_irqrestore(&task->task_state_lock, flags);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800849 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800850 task);
851 return 0;
852 }
Darrick J. Wong396819f2007-01-11 14:15:20 -0800853 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800854 spin_unlock_irqrestore(&task->task_state_lock, flags);
855
856 if (!si->dft->lldd_abort_task)
857 return -ENODEV;
858
859 res = si->dft->lldd_abort_task(task);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800860
861 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800862 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
863 (res == TMF_RESP_FUNC_COMPLETE))
864 {
Darrick J. Wong396819f2007-01-11 14:15:20 -0800865 spin_unlock_irqrestore(&task->task_state_lock, flags);
866 task->task_done(task);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800867 return 0;
868 }
869
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800870 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
871 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
872 spin_unlock_irqrestore(&task->task_state_lock, flags);
873
874 return -EAGAIN;
875}
876
Darrick J. Wong396819f2007-01-11 14:15:20 -0800877/*
878 * Tell an upper layer that it needs to initiate an abort for a given task.
879 * This should only ever be called by an LLDD.
880 */
881void sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800882{
Darrick J. Wong396819f2007-01-11 14:15:20 -0800883 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800884
Darrick J. Wong396819f2007-01-11 14:15:20 -0800885 /* Escape for libsas internal commands */
886 if (!sc) {
887 if (!del_timer(&task->timer))
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800888 return;
Darrick J. Wong396819f2007-01-11 14:15:20 -0800889 task->timer.function(task->timer.data);
890 return;
891 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800892
Darrick J. Wong396819f2007-01-11 14:15:20 -0800893 scsi_req_abort_cmd(sc);
894 scsi_schedule_eh(sc->device->host);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800895}
896
James Bottomley2908d772006-08-29 09:22:51 -0500897EXPORT_SYMBOL_GPL(sas_queuecommand);
898EXPORT_SYMBOL_GPL(sas_target_alloc);
899EXPORT_SYMBOL_GPL(sas_slave_configure);
900EXPORT_SYMBOL_GPL(sas_slave_destroy);
901EXPORT_SYMBOL_GPL(sas_change_queue_depth);
902EXPORT_SYMBOL_GPL(sas_change_queue_type);
903EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800904EXPORT_SYMBOL_GPL(__sas_task_abort);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800905EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -0800906EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800907EXPORT_SYMBOL_GPL(sas_phy_enable);