]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/libsas/sas_scsi_host.c
[SCSI] libsas: check for 'gone' expanders in smp_execute_task()
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / libsas / sas_scsi_host.c
CommitLineData
2908d778
JB
1/*
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
d7a54e30 26#include <linux/kthread.h>
45e6cdf4 27#include <linux/firmware.h>
09703660 28#include <linux/export.h>
45e6cdf4 29#include <linux/ctype.h>
d7a54e30 30
2908d778
JB
31#include "sas_internal.h"
32
33#include <scsi/scsi_host.h>
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_tcq.h>
36#include <scsi/scsi.h>
f456393e 37#include <scsi/scsi_eh.h>
2908d778
JB
38#include <scsi/scsi_transport.h>
39#include <scsi/scsi_transport_sas.h>
338ec570 40#include <scsi/sas_ata.h>
2908d778 41#include "../scsi_sas_internal.h"
79a5eb60 42#include "../scsi_transport_api.h"
ad689233 43#include "../scsi_priv.h"
2908d778
JB
44
45#include <linux/err.h>
46#include <linux/blkdev.h>
83144186 47#include <linux/freezer.h>
5a0e3ad6 48#include <linux/gfp.h>
2908d778 49#include <linux/scatterlist.h>
fa1c1e8f 50#include <linux/libata.h>
2908d778 51
a3a14252
DW
52/* record final status and free the task */
53static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
2908d778
JB
54{
55 struct task_status_struct *ts = &task->task_status;
2908d778
JB
56 int hs = 0, stat = 0;
57
2908d778
JB
58 if (ts->resp == SAS_TASK_UNDELIVERED) {
59 /* transport error */
60 hs = DID_NO_CONNECT;
61 } else { /* ts->resp == SAS_TASK_COMPLETE */
62 /* task delivered, what happened afterwards? */
63 switch (ts->stat) {
64 case SAS_DEV_NO_RESPONSE:
65 case SAS_INTERRUPTED:
66 case SAS_PHY_DOWN:
67 case SAS_NAK_R_ERR:
68 case SAS_OPEN_TO:
69 hs = DID_NO_CONNECT;
70 break;
71 case SAS_DATA_UNDERRUN:
c13e5566
FT
72 scsi_set_resid(sc, ts->residual);
73 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
2908d778
JB
74 hs = DID_ERROR;
75 break;
76 case SAS_DATA_OVERRUN:
77 hs = DID_ERROR;
78 break;
79 case SAS_QUEUE_FULL:
80 hs = DID_SOFT_ERROR; /* retry */
81 break;
82 case SAS_DEVICE_UNKNOWN:
83 hs = DID_BAD_TARGET;
84 break;
85 case SAS_SG_ERR:
86 hs = DID_PARITY;
87 break;
88 case SAS_OPEN_REJECT:
89 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
90 hs = DID_SOFT_ERROR; /* retry */
91 else
92 hs = DID_ERROR;
93 break;
94 case SAS_PROTO_RESPONSE:
95 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
96 "task; please report this\n",
97 task->dev->port->ha->sas_ha_name);
98 break;
99 case SAS_ABORTED_TASK:
100 hs = DID_ABORT;
101 break;
df64d3ca 102 case SAM_STAT_CHECK_CONDITION:
2908d778 103 memcpy(sc->sense_buffer, ts->buf,
cc75e8ab 104 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
df64d3ca 105 stat = SAM_STAT_CHECK_CONDITION;
2908d778
JB
106 break;
107 default:
108 stat = ts->stat;
109 break;
110 }
111 }
a3a14252 112
2908d778 113 sc->result = (hs << 16) | stat;
a3a14252 114 ASSIGN_SAS_TASK(sc, NULL);
2908d778
JB
115 list_del_init(&task->list);
116 sas_free_task(task);
a3a14252
DW
117}
118
119static void sas_scsi_task_done(struct sas_task *task)
120{
121 struct scsi_cmnd *sc = task->uldd_task;
9095a64a
DW
122 struct domain_device *dev = task->dev;
123 struct sas_ha_struct *ha = dev->port->ha;
124 unsigned long flags;
125
126 spin_lock_irqsave(&dev->done_lock, flags);
127 if (test_bit(SAS_HA_FROZEN, &ha->state))
128 task = NULL;
129 else
130 ASSIGN_SAS_TASK(sc, NULL);
131 spin_unlock_irqrestore(&dev->done_lock, flags);
a3a14252 132
9095a64a
DW
133 if (unlikely(!task)) {
134 /* task will be completed by the error handler */
a3a14252
DW
135 SAS_DPRINTK("task done but aborted\n");
136 return;
137 }
138
139 if (unlikely(!sc)) {
140 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
141 list_del_init(&task->list);
142 sas_free_task(task);
143 return;
144 }
145
a3a14252 146 sas_end_task(sc, task);
a8e14fec 147 sc->scsi_done(sc);
2908d778
JB
148}
149
2908d778
JB
150static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
151 struct domain_device *dev,
3cc27547 152 gfp_t gfp_flags)
2908d778
JB
153{
154 struct sas_task *task = sas_alloc_task(gfp_flags);
155 struct scsi_lun lun;
156
157 if (!task)
158 return NULL;
159
2908d778
JB
160 task->uldd_task = cmd;
161 ASSIGN_SAS_TASK(cmd, task);
162
163 task->dev = dev;
164 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
165
166 task->ssp_task.retry_count = 1;
167 int_to_scsilun(cmd->device->lun, &lun);
168 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
9cbbdca4 169 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
2908d778
JB
170 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
171
c13e5566
FT
172 task->scatter = scsi_sglist(cmd);
173 task->num_scatter = scsi_sg_count(cmd);
174 task->total_xfer_len = scsi_bufflen(cmd);
2908d778
JB
175 task->data_dir = cmd->sc_data_direction;
176
177 task->task_done = sas_scsi_task_done;
178
179 return task;
180}
181
338ec570 182int sas_queue_up(struct sas_task *task)
2908d778
JB
183{
184 struct sas_ha_struct *sas_ha = task->dev->port->ha;
185 struct scsi_core *core = &sas_ha->core;
186 unsigned long flags;
187 LIST_HEAD(list);
188
189 spin_lock_irqsave(&core->task_queue_lock, flags);
190 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
191 spin_unlock_irqrestore(&core->task_queue_lock, flags);
192 return -SAS_QUEUE_FULL;
193 }
194 list_add_tail(&task->list, &core->task_queue);
195 core->task_queue_size += 1;
196 spin_unlock_irqrestore(&core->task_queue_lock, flags);
d7a54e30 197 wake_up_process(core->queue_thread);
2908d778
JB
198
199 return 0;
200}
201
92bd401b 202int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2908d778 203{
2908d778 204 struct sas_internal *i = to_sas_internal(host->transportt);
a923f756
CH
205 struct domain_device *dev = cmd_to_domain_dev(cmd);
206 struct sas_ha_struct *sas_ha = dev->port->ha;
207 struct sas_task *task;
208 int res = 0;
2908d778 209
a923f756 210 /* If the device fell off, no sense in issuing commands */
e139942d 211 if (test_bit(SAS_DEV_GONE, &dev->state)) {
a923f756
CH
212 cmd->result = DID_BAD_TARGET << 16;
213 goto out_done;
214 }
2908d778 215
a923f756 216 if (dev_is_sata(dev)) {
312d3e56 217 spin_lock_irq(dev->sata_dev.ap->lock);
a923f756 218 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
312d3e56 219 spin_unlock_irq(dev->sata_dev.ap->lock);
a923f756
CH
220 return res;
221 }
3eb7a51a 222
a923f756
CH
223 task = sas_create_task(cmd, dev, GFP_ATOMIC);
224 if (!task)
ac81c6a8 225 return SCSI_MLQUEUE_HOST_BUSY;
fa1c1e8f 226
a923f756
CH
227 /* Queue up, Direct Mode or Task Collector Mode. */
228 if (sas_ha->lldd_max_execute_num < 2)
229 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
230 else
231 res = sas_queue_up(task);
232
233 if (res)
234 goto out_free_task;
235 return 0;
236
237out_free_task:
238 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
239 ASSIGN_SAS_TASK(cmd, NULL);
240 sas_free_task(task);
ac81c6a8
CH
241 if (res == -SAS_QUEUE_FULL)
242 cmd->result = DID_SOFT_ERROR << 16; /* retry */
243 else
244 cmd->result = DID_ERROR << 16;
a923f756
CH
245out_done:
246 cmd->scsi_done(cmd);
247 return 0;
2908d778
JB
248}
249
a8e14fec
JB
250static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
251{
252 struct sas_task *task = TO_SAS_TASK(cmd);
253 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
254
a3a14252
DW
255 /* At this point, we only get called following an actual abort
256 * of the task, so we should be guaranteed not to be racing with
257 * any completions from the LLD. Task is freed after this.
258 */
259 sas_end_task(cmd, task);
260
a8e14fec
JB
261 /* now finish the command and move it on to the error
262 * handler done list, this also takes it off the
a3a14252
DW
263 * error handler pending list.
264 */
a8e14fec
JB
265 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
266}
267
3944f509
DW
268static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
269{
270 struct sas_task *task = TO_SAS_TASK(cmd);
271 struct domain_device *dev = task->dev;
272 struct sas_ha_struct *ha = dev->port->ha;
273
274 if (!dev_is_sata(dev)) {
275 sas_eh_finish_cmd(cmd);
276 return;
277 }
278
279 /* report the timeout to libata */
280 sas_end_task(cmd, task);
281 list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
282}
283
2908d778
JB
284static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
285{
286 struct scsi_cmnd *cmd, *n;
287
288 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
63e4563b
JB
289 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
290 cmd->device->lun == my_cmd->device->lun)
3a2cdf39 291 sas_eh_defer_cmd(cmd);
2908d778
JB
292 }
293}
294
295static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
296 struct domain_device *dev)
297{
298 struct scsi_cmnd *cmd, *n;
299
300 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
301 struct domain_device *x = cmd_to_domain_dev(cmd);
302
303 if (x == dev)
a8e14fec 304 sas_eh_finish_cmd(cmd);
2908d778
JB
305 }
306}
307
308static void sas_scsi_clear_queue_port(struct list_head *error_q,
309 struct asd_sas_port *port)
310{
311 struct scsi_cmnd *cmd, *n;
312
313 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
314 struct domain_device *dev = cmd_to_domain_dev(cmd);
315 struct asd_sas_port *x = dev->port;
316
317 if (x == port)
a8e14fec 318 sas_eh_finish_cmd(cmd);
2908d778
JB
319 }
320}
321
322enum task_disposition {
323 TASK_IS_DONE,
324 TASK_IS_ABORTED,
325 TASK_IS_AT_LU,
9095a64a 326 TASK_IS_NOT_AT_HA,
2908d778 327 TASK_IS_NOT_AT_LU,
02cd743b 328 TASK_ABORT_FAILED,
2908d778
JB
329};
330
331static enum task_disposition sas_scsi_find_task(struct sas_task *task)
332{
333 struct sas_ha_struct *ha = task->dev->port->ha;
334 unsigned long flags;
335 int i, res;
336 struct sas_internal *si =
337 to_sas_internal(task->dev->port->ha->core.shost->transportt);
338
339 if (ha->lldd_max_execute_num > 1) {
340 struct scsi_core *core = &ha->core;
341 struct sas_task *t, *n;
342
9095a64a 343 mutex_lock(&core->task_queue_flush);
2908d778 344 spin_lock_irqsave(&core->task_queue_lock, flags);
9095a64a 345 list_for_each_entry_safe(t, n, &core->task_queue, list)
2908d778
JB
346 if (task == t) {
347 list_del_init(&t->list);
9095a64a 348 break;
2908d778 349 }
2908d778 350 spin_unlock_irqrestore(&core->task_queue_lock, flags);
9095a64a
DW
351 mutex_unlock(&core->task_queue_flush);
352
353 if (task == t)
354 return TASK_IS_NOT_AT_HA;
2908d778
JB
355 }
356
357 for (i = 0; i < 5; i++) {
cadbd4a5 358 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
2908d778
JB
359 res = si->dft->lldd_abort_task(task);
360
361 spin_lock_irqsave(&task->task_state_lock, flags);
362 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
363 spin_unlock_irqrestore(&task->task_state_lock, flags);
cadbd4a5 364 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
2908d778
JB
365 task);
366 return TASK_IS_DONE;
367 }
368 spin_unlock_irqrestore(&task->task_state_lock, flags);
369
370 if (res == TMF_RESP_FUNC_COMPLETE) {
371 SAS_DPRINTK("%s: task 0x%p is aborted\n",
cadbd4a5 372 __func__, task);
2908d778
JB
373 return TASK_IS_ABORTED;
374 } else if (si->dft->lldd_query_task) {
375 SAS_DPRINTK("%s: querying task 0x%p\n",
cadbd4a5 376 __func__, task);
2908d778 377 res = si->dft->lldd_query_task(task);
02cd743b
DW
378 switch (res) {
379 case TMF_RESP_FUNC_SUCC:
2908d778 380 SAS_DPRINTK("%s: task 0x%p at LU\n",
cadbd4a5 381 __func__, task);
2908d778 382 return TASK_IS_AT_LU;
02cd743b 383 case TMF_RESP_FUNC_COMPLETE:
2908d778 384 SAS_DPRINTK("%s: task 0x%p not at LU\n",
cadbd4a5 385 __func__, task);
2908d778 386 return TASK_IS_NOT_AT_LU;
02cd743b
DW
387 case TMF_RESP_FUNC_FAILED:
388 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
cadbd4a5 389 __func__, task);
02cd743b
DW
390 return TASK_ABORT_FAILED;
391 }
392
2908d778
JB
393 }
394 }
395 return res;
396}
397
398static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
399{
400 int res = TMF_RESP_FUNC_FAILED;
401 struct scsi_lun lun;
402 struct sas_internal *i =
403 to_sas_internal(dev->port->ha->core.shost->transportt);
404
405 int_to_scsilun(cmd->device->lun, &lun);
406
407 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
408 SAS_ADDR(dev->sas_addr),
409 cmd->device->lun);
410
411 if (i->dft->lldd_abort_task_set)
412 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
413
414 if (res == TMF_RESP_FUNC_FAILED) {
415 if (i->dft->lldd_clear_task_set)
416 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
417 }
418
419 if (res == TMF_RESP_FUNC_FAILED) {
420 if (i->dft->lldd_lu_reset)
421 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
422 }
423
424 return res;
425}
426
427static int sas_recover_I_T(struct domain_device *dev)
428{
429 int res = TMF_RESP_FUNC_FAILED;
430 struct sas_internal *i =
431 to_sas_internal(dev->port->ha->core.shost->transportt);
432
433 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
434 SAS_ADDR(dev->sas_addr));
435
436 if (i->dft->lldd_I_T_nexus_reset)
437 res = i->dft->lldd_I_T_nexus_reset(dev);
438
439 return res;
440}
441
ad689233 442/* Find the sas_phy that's attached to this device */
5319578c 443struct sas_phy *sas_find_local_phy(struct domain_device *dev)
3ebf6922 444{
ad689233
DW
445 struct domain_device *pdev = dev->parent;
446 struct ex_phy *exphy = NULL;
447 int i;
448
449 /* Directly attached device */
450 if (!pdev)
451 return dev->port->phy;
3ebf6922 452
ad689233
DW
453 /* Otherwise look in the expander */
454 for (i = 0; i < pdev->ex_dev.num_phys; i++)
455 if (!memcmp(dev->sas_addr,
456 pdev->ex_dev.ex_phy[i].attached_sas_addr,
457 SAS_ADDR_SIZE)) {
458 exphy = &pdev->ex_dev.ex_phy[i];
459 break;
460 }
3ebf6922 461
ad689233
DW
462 BUG_ON(!exphy);
463 return exphy->phy;
3ebf6922 464}
5319578c 465EXPORT_SYMBOL_GPL(sas_find_local_phy);
3ebf6922 466
a9344e68 467/* Attempt to send a LUN reset message to a device */
ad689233 468int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
a9344e68
DW
469{
470 struct domain_device *dev = cmd_to_domain_dev(cmd);
471 struct sas_internal *i =
472 to_sas_internal(dev->port->ha->core.shost->transportt);
473 struct scsi_lun lun;
474 int res;
475
476 int_to_scsilun(cmd->device->lun, &lun);
477
478 if (!i->dft->lldd_lu_reset)
479 return FAILED;
480
481 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
482 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
483 return SUCCESS;
484
485 return FAILED;
486}
487
488/* Attempt to send a phy (bus) reset */
489int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
ad689233
DW
490{
491 struct domain_device *dev = cmd_to_domain_dev(cmd);
5319578c 492 struct sas_phy *phy = sas_find_local_phy(dev);
ad689233
DW
493 int res;
494
495 res = sas_phy_reset(phy, 1);
496 if (res)
a9344e68 497 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
af5ca3f4 498 kobject_name(&phy->dev.kobj),
ad689233
DW
499 res);
500 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
501 return SUCCESS;
502
503 return FAILED;
504}
505
506/* Try to reset a device */
8de3ef25 507static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
ad689233 508{
a9344e68 509 int res;
8de3ef25 510 struct Scsi_Host *shost = cmd->device->host;
a9344e68 511
ad689233 512 if (!shost->hostt->eh_device_reset_handler)
a9344e68
DW
513 goto try_bus_reset;
514
515 res = shost->hostt->eh_device_reset_handler(cmd);
516 if (res == SUCCESS)
517 return res;
ad689233 518
a9344e68
DW
519try_bus_reset:
520 if (shost->hostt->eh_bus_reset_handler)
521 return shost->hostt->eh_bus_reset_handler(cmd);
522
523 return FAILED;
ad689233
DW
524}
525
526static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
9095a64a 527 struct list_head *work_q)
2908d778 528{
2908d778
JB
529 struct scsi_cmnd *cmd, *n;
530 enum task_disposition res = TASK_IS_DONE;
3ebf6922 531 int tmf_resp, need_reset;
2908d778 532 struct sas_internal *i = to_sas_internal(shost->transportt);
ad689233
DW
533 unsigned long flags;
534 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
2908d778 535
2908d778 536Again:
ad689233 537 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
9095a64a
DW
538 struct domain_device *dev = cmd_to_domain_dev(cmd);
539 struct sas_task *task;
540
541 spin_lock_irqsave(&dev->done_lock, flags);
542 /* by this point the lldd has either observed
543 * SAS_HA_FROZEN and is leaving the task alone, or has
544 * won the race with eh and decided to complete it
545 */
546 task = TO_SAS_TASK(cmd);
547 spin_unlock_irqrestore(&dev->done_lock, flags);
2908d778 548
ad689233 549 if (!task)
f456393e 550 continue;
ad689233
DW
551
552 list_del_init(&cmd->eh_entry);
3ebf6922
DW
553
554 spin_lock_irqsave(&task->task_state_lock, flags);
555 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
3ebf6922
DW
556 spin_unlock_irqrestore(&task->task_state_lock, flags);
557
8de3ef25
JB
558 if (need_reset) {
559 SAS_DPRINTK("%s: task 0x%p requests reset\n",
cadbd4a5 560 __func__, task);
8de3ef25
JB
561 goto reset;
562 }
563
2908d778 564 SAS_DPRINTK("trying to find task 0x%p\n", task);
2908d778
JB
565 res = sas_scsi_find_task(task);
566
567 cmd->eh_eflags = 0;
2908d778
JB
568
569 switch (res) {
9095a64a
DW
570 case TASK_IS_NOT_AT_HA:
571 SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
572 __func__, task,
573 cmd->retries ? "retry" : "aborted");
574 if (cmd->retries)
575 cmd->retries--;
576 sas_eh_finish_cmd(cmd);
577 continue;
2908d778 578 case TASK_IS_DONE:
cadbd4a5 579 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
2908d778 580 task);
3944f509 581 sas_eh_defer_cmd(cmd);
2908d778
JB
582 continue;
583 case TASK_IS_ABORTED:
584 SAS_DPRINTK("%s: task 0x%p is aborted\n",
cadbd4a5 585 __func__, task);
3944f509 586 sas_eh_defer_cmd(cmd);
2908d778
JB
587 continue;
588 case TASK_IS_AT_LU:
589 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
8de3ef25 590 reset:
2908d778
JB
591 tmf_resp = sas_recover_lu(task->dev, cmd);
592 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
593 SAS_DPRINTK("dev %016llx LU %x is "
594 "recovered\n",
595 SAS_ADDR(task->dev),
596 cmd->device->lun);
3a2cdf39 597 sas_eh_defer_cmd(cmd);
ad689233 598 sas_scsi_clear_queue_lu(work_q, cmd);
2908d778
JB
599 goto Again;
600 }
601 /* fallthrough */
602 case TASK_IS_NOT_AT_LU:
02cd743b 603 case TASK_ABORT_FAILED:
2908d778
JB
604 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
605 task);
606 tmf_resp = sas_recover_I_T(task->dev);
607 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
8de3ef25 608 struct domain_device *dev = task->dev;
2908d778
JB
609 SAS_DPRINTK("I_T %016llx recovered\n",
610 SAS_ADDR(task->dev->sas_addr));
a8e14fec 611 sas_eh_finish_cmd(cmd);
8de3ef25 612 sas_scsi_clear_queue_I_T(work_q, dev);
2908d778
JB
613 goto Again;
614 }
615 /* Hammer time :-) */
8de3ef25 616 try_to_reset_cmd_device(cmd);
2908d778
JB
617 if (i->dft->lldd_clear_nexus_port) {
618 struct asd_sas_port *port = task->dev->port;
619 SAS_DPRINTK("clearing nexus for port:%d\n",
620 port->id);
621 res = i->dft->lldd_clear_nexus_port(port);
622 if (res == TMF_RESP_FUNC_COMPLETE) {
623 SAS_DPRINTK("clear nexus port:%d "
624 "succeeded\n", port->id);
a8e14fec 625 sas_eh_finish_cmd(cmd);
ad689233 626 sas_scsi_clear_queue_port(work_q,
2908d778
JB
627 port);
628 goto Again;
629 }
630 }
631 if (i->dft->lldd_clear_nexus_ha) {
632 SAS_DPRINTK("clear nexus ha\n");
633 res = i->dft->lldd_clear_nexus_ha(ha);
634 if (res == TMF_RESP_FUNC_COMPLETE) {
635 SAS_DPRINTK("clear nexus ha "
636 "succeeded\n");
a8e14fec 637 sas_eh_finish_cmd(cmd);
a8e14fec 638 goto clear_q;
2908d778
JB
639 }
640 }
641 /* If we are here -- this means that no amount
642 * of effort could recover from errors. Quite
643 * possibly the HA just disappeared.
644 */
645 SAS_DPRINTK("error from device %llx, LUN %x "
646 "couldn't be recovered in any way\n",
647 SAS_ADDR(task->dev->sas_addr),
648 cmd->device->lun);
649
a8e14fec 650 sas_eh_finish_cmd(cmd);
2908d778
JB
651 goto clear_q;
652 }
653 }
3944f509 654 list_splice_tail_init(&ha->eh_ata_q, work_q);
ad689233 655 return list_empty(work_q);
2908d778 656clear_q:
cadbd4a5 657 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
a8e14fec
JB
658 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
659 sas_eh_finish_cmd(cmd);
660
3944f509 661 list_splice_tail_init(&ha->eh_ata_q, work_q);
ad689233
DW
662 return list_empty(work_q);
663}
664
665void sas_scsi_recover_host(struct Scsi_Host *shost)
666{
667 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
668 unsigned long flags;
669 LIST_HEAD(eh_work_q);
670
671 spin_lock_irqsave(shost->host_lock, flags);
672 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
9ee91f7f 673 shost->host_eh_scheduled = 0;
ad689233
DW
674 spin_unlock_irqrestore(shost->host_lock, flags);
675
cadbd4a5 676 SAS_DPRINTK("Enter %s\n", __func__);
ad689233
DW
677 /*
678 * Deal with commands that still have SAS tasks (i.e. they didn't
679 * complete via the normal sas_task completion mechanism)
680 */
9095a64a
DW
681 set_bit(SAS_HA_FROZEN, &ha->state);
682 if (sas_eh_handle_sas_errors(shost, &eh_work_q))
ad689233
DW
683 goto out;
684
685 /*
686 * Now deal with SCSI commands that completed ok but have a an error
687 * code (and hopefully sense data) attached. This is roughly what
688 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
689 * command we see here has no sas_task and is thus unknown to the HA.
690 */
00dd4998
JB
691 if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
692 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
693 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
ad689233
DW
694
695out:
9095a64a
DW
696 clear_bit(SAS_HA_FROZEN, &ha->state);
697 if (ha->lldd_max_execute_num > 1)
698 wake_up_process(ha->core.queue_thread);
699
00dd4998
JB
700 /* now link into libata eh --- if we have any ata devices */
701 sas_ata_strategy_handler(shost);
702
ad689233 703 scsi_eh_flush_done_q(&ha->eh_done_q);
00dd4998 704
cadbd4a5 705 SAS_DPRINTK("--- Exit %s\n", __func__);
ad689233 706 return;
2908d778
JB
707}
708
242f9dcb 709enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
2908d778 710{
9095a64a 711 scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
2908d778 712
242f9dcb 713 return BLK_EH_NOT_HANDLED;
2908d778
JB
714}
715
fa1c1e8f
DW
716int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
717{
718 struct domain_device *dev = sdev_to_domain_dev(sdev);
719
720 if (dev_is_sata(dev))
94be9a58 721 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
fa1c1e8f
DW
722
723 return -EINVAL;
724}
725
2908d778
JB
726struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
727{
728 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
729 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
730 struct domain_device *found_dev = NULL;
731 int i;
980fa2f9 732 unsigned long flags;
2908d778 733
980fa2f9 734 spin_lock_irqsave(&ha->phy_port_lock, flags);
2908d778
JB
735 for (i = 0; i < ha->num_phys; i++) {
736 struct asd_sas_port *port = ha->sas_port[i];
737 struct domain_device *dev;
738
739 spin_lock(&port->dev_list_lock);
740 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
741 if (rphy == dev->rphy) {
742 found_dev = dev;
743 spin_unlock(&port->dev_list_lock);
744 goto found;
745 }
746 }
747 spin_unlock(&port->dev_list_lock);
748 }
749 found:
980fa2f9 750 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
2908d778
JB
751
752 return found_dev;
753}
754
2908d778
JB
755int sas_target_alloc(struct scsi_target *starget)
756{
735f7d2f
DW
757 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
758 struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
338ec570 759 int res;
2908d778
JB
760
761 if (!found_dev)
762 return -ENODEV;
763
fa1c1e8f 764 if (dev_is_sata(found_dev)) {
338ec570
DW
765 res = sas_ata_init_host_and_port(found_dev, starget);
766 if (res)
767 return res;
fa1c1e8f
DW
768 }
769
735f7d2f 770 kref_get(&found_dev->kref);
2908d778
JB
771 starget->hostdata = found_dev;
772 return 0;
773}
774
97a1420d 775#define SAS_DEF_QD 256
2908d778
JB
776
777int sas_slave_configure(struct scsi_device *scsi_dev)
778{
779 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
780 struct sas_ha_struct *sas_ha;
781
782 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
783
fa1c1e8f
DW
784 if (dev_is_sata(dev)) {
785 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
786 return 0;
787 }
788
2908d778
JB
789 sas_ha = dev->port->ha;
790
791 sas_read_port_mode_page(scsi_dev);
792
793 if (scsi_dev->tagged_supported) {
794 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
795 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
796 } else {
797 SAS_DPRINTK("device %llx, LUN %x doesn't support "
798 "TCQ\n", SAS_ADDR(dev->sas_addr),
799 scsi_dev->lun);
800 scsi_dev->tagged_supported = 0;
801 scsi_set_tag_type(scsi_dev, 0);
802 scsi_deactivate_tcq(scsi_dev, 1);
803 }
804
f27708fc
DW
805 scsi_dev->allow_restart = 1;
806
2908d778
JB
807 return 0;
808}
809
97a1420d 810int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
2908d778 811{
97a1420d 812 struct domain_device *dev = sdev_to_domain_dev(sdev);
2908d778 813
f6e67035 814 if (dev_is_sata(dev))
97a1420d
DW
815 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
816 reason);
817
818 switch (reason) {
819 case SCSI_QDEPTH_DEFAULT:
820 case SCSI_QDEPTH_RAMP_UP:
821 if (!sdev->tagged_supported)
822 depth = 1;
823 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
824 break;
825 case SCSI_QDEPTH_QFULL:
826 scsi_track_queue_full(sdev, depth);
827 break;
828 default:
e881a172 829 return -EOPNOTSUPP;
2908d778
JB
830 }
831
97a1420d 832 return depth;
2908d778
JB
833}
834
835int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
836{
f6e67035
DW
837 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
838
839 if (dev_is_sata(dev))
840 return -EINVAL;
841
2908d778
JB
842 if (!scsi_dev->tagged_supported)
843 return 0;
844
845 scsi_deactivate_tcq(scsi_dev, 1);
846
847 scsi_set_tag_type(scsi_dev, qt);
848 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
849
850 return qt;
851}
852
853int sas_bios_param(struct scsi_device *scsi_dev,
854 struct block_device *bdev,
855 sector_t capacity, int *hsc)
856{
857 hsc[0] = 255;
858 hsc[1] = 63;
859 sector_div(capacity, 255*63);
860 hsc[2] = capacity;
861
862 return 0;
863}
864
865/* ---------- Task Collector Thread implementation ---------- */
866
867static void sas_queue(struct sas_ha_struct *sas_ha)
868{
869 struct scsi_core *core = &sas_ha->core;
870 unsigned long flags;
871 LIST_HEAD(q);
872 int can_queue;
873 int res;
874 struct sas_internal *i = to_sas_internal(core->shost->transportt);
875
9095a64a 876 mutex_lock(&core->task_queue_flush);
2908d778 877 spin_lock_irqsave(&core->task_queue_lock, flags);
d7a54e30 878 while (!kthread_should_stop() &&
9095a64a
DW
879 !list_empty(&core->task_queue) &&
880 !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
2908d778
JB
881
882 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
883 if (can_queue >= 0) {
884 can_queue = core->task_queue_size;
885 list_splice_init(&core->task_queue, &q);
886 } else {
887 struct list_head *a, *n;
888
889 can_queue = sas_ha->lldd_queue_size;
890 list_for_each_safe(a, n, &core->task_queue) {
891 list_move_tail(a, &q);
892 if (--can_queue == 0)
893 break;
894 }
895 can_queue = sas_ha->lldd_queue_size;
896 }
897 core->task_queue_size -= can_queue;
898 spin_unlock_irqrestore(&core->task_queue_lock, flags);
899 {
900 struct sas_task *task = list_entry(q.next,
901 struct sas_task,
902 list);
903 list_del_init(&q);
904 res = i->dft->lldd_execute_task(task, can_queue,
905 GFP_KERNEL);
906 if (unlikely(res))
907 __list_add(&q, task->list.prev, &task->list);
908 }
909 spin_lock_irqsave(&core->task_queue_lock, flags);
910 if (res) {
911 list_splice_init(&q, &core->task_queue); /*at head*/
912 core->task_queue_size += can_queue;
913 }
914 }
915 spin_unlock_irqrestore(&core->task_queue_lock, flags);
9095a64a 916 mutex_unlock(&core->task_queue_flush);
2908d778
JB
917}
918
2908d778
JB
919/**
920 * sas_queue_thread -- The Task Collector thread
921 * @_sas_ha: pointer to struct sas_ha
922 */
923static int sas_queue_thread(void *_sas_ha)
924{
925 struct sas_ha_struct *sas_ha = _sas_ha;
2908d778 926
2908d778 927 while (1) {
d7a54e30
CH
928 set_current_state(TASK_INTERRUPTIBLE);
929 schedule();
2908d778 930 sas_queue(sas_ha);
d7a54e30 931 if (kthread_should_stop())
2908d778
JB
932 break;
933 }
934
2908d778
JB
935 return 0;
936}
937
938int sas_init_queue(struct sas_ha_struct *sas_ha)
939{
2908d778
JB
940 struct scsi_core *core = &sas_ha->core;
941
942 spin_lock_init(&core->task_queue_lock);
9095a64a 943 mutex_init(&core->task_queue_flush);
2908d778
JB
944 core->task_queue_size = 0;
945 INIT_LIST_HEAD(&core->task_queue);
2908d778 946
d7a54e30
CH
947 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
948 "sas_queue_%d", core->shost->host_no);
949 if (IS_ERR(core->queue_thread))
950 return PTR_ERR(core->queue_thread);
951 return 0;
2908d778
JB
952}
953
954void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
955{
956 unsigned long flags;
957 struct scsi_core *core = &sas_ha->core;
958 struct sas_task *task, *n;
959
d7a54e30 960 kthread_stop(core->queue_thread);
2908d778
JB
961
962 if (!list_empty(&core->task_queue))
963 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
964 SAS_ADDR(sas_ha->sas_addr));
965
966 spin_lock_irqsave(&core->task_queue_lock, flags);
967 list_for_each_entry_safe(task, n, &core->task_queue, list) {
968 struct scsi_cmnd *cmd = task->uldd_task;
969
970 list_del_init(&task->list);
971
972 ASSIGN_SAS_TASK(cmd, NULL);
973 sas_free_task(task);
974 cmd->result = DID_ABORT << 16;
975 cmd->scsi_done(cmd);
976 }
977 spin_unlock_irqrestore(&core->task_queue_lock, flags);
978}
979
396819fb
DW
980/*
981 * Tell an upper layer that it needs to initiate an abort for a given task.
982 * This should only ever be called by an LLDD.
983 */
984void sas_task_abort(struct sas_task *task)
79a5eb60 985{
396819fb 986 struct scsi_cmnd *sc = task->uldd_task;
79a5eb60 987
396819fb
DW
988 /* Escape for libsas internal commands */
989 if (!sc) {
990 if (!del_timer(&task->timer))
79a5eb60 991 return;
396819fb
DW
992 task->timer.function(task->timer.data);
993 return;
994 }
79a5eb60 995
3a2755af
DW
996 if (dev_is_sata(task->dev)) {
997 sas_ata_task_abort(task);
1b4d0d8e
JB
998 } else {
999 struct request_queue *q = sc->device->request_queue;
1000 unsigned long flags;
3a2755af 1001
1b4d0d8e
JB
1002 spin_lock_irqsave(q->queue_lock, flags);
1003 blk_abort_request(sc->request);
1004 spin_unlock_irqrestore(q->queue_lock, flags);
1005 scsi_schedule_eh(sc->device->host);
1006 }
79a5eb60
DW
1007}
1008
fa1c1e8f
DW
1009int sas_slave_alloc(struct scsi_device *scsi_dev)
1010{
1011 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1012
1013 if (dev_is_sata(dev))
1014 return ata_sas_port_init(dev->sata_dev.ap);
1015
1016 return 0;
1017}
1018
1019void sas_target_destroy(struct scsi_target *starget)
1020{
735f7d2f 1021 struct domain_device *found_dev = starget->hostdata;
fa1c1e8f
DW
1022
1023 if (!found_dev)
1024 return;
1025
1026 if (dev_is_sata(found_dev))
1027 ata_sas_port_destroy(found_dev->sata_dev.ap);
1028
735f7d2f
DW
1029 starget->hostdata = NULL;
1030 sas_put_device(found_dev);
fa1c1e8f
DW
1031}
1032
45e6cdf4
DW
1033static void sas_parse_addr(u8 *sas_addr, const char *p)
1034{
1035 int i;
1036 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1037 u8 h, l;
1038 if (!*p)
1039 break;
1040 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1041 p++;
1042 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1043 p++;
1044 sas_addr[i] = (h<<4) | l;
1045 }
1046}
1047
1048#define SAS_STRING_ADDR_SIZE 16
1049
1050int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1051{
1052 int res;
1053 const struct firmware *fw;
1054
1055 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1056 if (res)
1057 return res;
1058
1059 if (fw->size < SAS_STRING_ADDR_SIZE) {
1060 res = -ENODEV;
1061 goto out;
1062 }
1063
1064 sas_parse_addr(addr, fw->data);
1065
1066out:
1067 release_firmware(fw);
1068 return res;
1069}
1070EXPORT_SYMBOL_GPL(sas_request_addr);
1071
2908d778
JB
1072EXPORT_SYMBOL_GPL(sas_queuecommand);
1073EXPORT_SYMBOL_GPL(sas_target_alloc);
1074EXPORT_SYMBOL_GPL(sas_slave_configure);
2908d778
JB
1075EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1076EXPORT_SYMBOL_GPL(sas_change_queue_type);
1077EXPORT_SYMBOL_GPL(sas_bios_param);
79a5eb60 1078EXPORT_SYMBOL_GPL(sas_task_abort);
dea22214 1079EXPORT_SYMBOL_GPL(sas_phy_reset);
ad689233 1080EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
a9344e68 1081EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
fa1c1e8f
DW
1082EXPORT_SYMBOL_GPL(sas_slave_alloc);
1083EXPORT_SYMBOL_GPL(sas_target_destroy);
1084EXPORT_SYMBOL_GPL(sas_ioctl);