Loading...
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
26#include <linux/kthread.h>
27#include <linux/firmware.h>
28#include <linux/ctype.h>
29
30#include "sas_internal.h"
31
32#include <scsi/scsi_host.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi.h>
36#include <scsi/scsi_eh.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_sas.h>
39#include <scsi/sas_ata.h>
40#include "../scsi_sas_internal.h"
41#include "../scsi_transport_api.h"
42#include "../scsi_priv.h"
43
44#include <linux/err.h>
45#include <linux/blkdev.h>
46#include <linux/freezer.h>
47#include <linux/gfp.h>
48#include <linux/scatterlist.h>
49#include <linux/libata.h>
50
51/* ---------- SCSI Host glue ---------- */
52
53static void sas_scsi_task_done(struct sas_task *task)
54{
55 struct task_status_struct *ts = &task->task_status;
56 struct scsi_cmnd *sc = task->uldd_task;
57 int hs = 0, stat = 0;
58
59 if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
60 /* Aborted tasks will be completed by the error handler */
61 SAS_DPRINTK("task done but aborted\n");
62 return;
63 }
64
65 if (unlikely(!sc)) {
66 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
67 list_del_init(&task->list);
68 sas_free_task(task);
69 return;
70 }
71
72 if (ts->resp == SAS_TASK_UNDELIVERED) {
73 /* transport error */
74 hs = DID_NO_CONNECT;
75 } else { /* ts->resp == SAS_TASK_COMPLETE */
76 /* task delivered, what happened afterwards? */
77 switch (ts->stat) {
78 case SAS_DEV_NO_RESPONSE:
79 case SAS_INTERRUPTED:
80 case SAS_PHY_DOWN:
81 case SAS_NAK_R_ERR:
82 case SAS_OPEN_TO:
83 hs = DID_NO_CONNECT;
84 break;
85 case SAS_DATA_UNDERRUN:
86 scsi_set_resid(sc, ts->residual);
87 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
88 hs = DID_ERROR;
89 break;
90 case SAS_DATA_OVERRUN:
91 hs = DID_ERROR;
92 break;
93 case SAS_QUEUE_FULL:
94 hs = DID_SOFT_ERROR; /* retry */
95 break;
96 case SAS_DEVICE_UNKNOWN:
97 hs = DID_BAD_TARGET;
98 break;
99 case SAS_SG_ERR:
100 hs = DID_PARITY;
101 break;
102 case SAS_OPEN_REJECT:
103 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
104 hs = DID_SOFT_ERROR; /* retry */
105 else
106 hs = DID_ERROR;
107 break;
108 case SAS_PROTO_RESPONSE:
109 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
110 "task; please report this\n",
111 task->dev->port->ha->sas_ha_name);
112 break;
113 case SAS_ABORTED_TASK:
114 hs = DID_ABORT;
115 break;
116 case SAM_STAT_CHECK_CONDITION:
117 memcpy(sc->sense_buffer, ts->buf,
118 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
119 stat = SAM_STAT_CHECK_CONDITION;
120 break;
121 default:
122 stat = ts->stat;
123 break;
124 }
125 }
126 ASSIGN_SAS_TASK(sc, NULL);
127 sc->result = (hs << 16) | stat;
128 list_del_init(&task->list);
129 sas_free_task(task);
130 sc->scsi_done(sc);
131}
132
133static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
134 struct domain_device *dev,
135 gfp_t gfp_flags)
136{
137 struct sas_task *task = sas_alloc_task(gfp_flags);
138 struct scsi_lun lun;
139
140 if (!task)
141 return NULL;
142
143 task->uldd_task = cmd;
144 ASSIGN_SAS_TASK(cmd, task);
145
146 task->dev = dev;
147 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
148
149 task->ssp_task.retry_count = 1;
150 int_to_scsilun(cmd->device->lun, &lun);
151 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
152 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
153 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
154
155 task->scatter = scsi_sglist(cmd);
156 task->num_scatter = scsi_sg_count(cmd);
157 task->total_xfer_len = scsi_bufflen(cmd);
158 task->data_dir = cmd->sc_data_direction;
159
160 task->task_done = sas_scsi_task_done;
161
162 return task;
163}
164
165int sas_queue_up(struct sas_task *task)
166{
167 struct sas_ha_struct *sas_ha = task->dev->port->ha;
168 struct scsi_core *core = &sas_ha->core;
169 unsigned long flags;
170 LIST_HEAD(list);
171
172 spin_lock_irqsave(&core->task_queue_lock, flags);
173 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
174 spin_unlock_irqrestore(&core->task_queue_lock, flags);
175 return -SAS_QUEUE_FULL;
176 }
177 list_add_tail(&task->list, &core->task_queue);
178 core->task_queue_size += 1;
179 spin_unlock_irqrestore(&core->task_queue_lock, flags);
180 wake_up_process(core->queue_thread);
181
182 return 0;
183}
184
185/**
186 * sas_queuecommand -- Enqueue a command for processing
187 * @parameters: See SCSI Core documentation
188 *
189 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
190 * call us without holding an IRQ spinlock...
191 */
192static int sas_queuecommand_lck(struct scsi_cmnd *cmd,
193 void (*scsi_done)(struct scsi_cmnd *))
194 __releases(host->host_lock)
195 __acquires(dev->sata_dev.ap->lock)
196 __releases(dev->sata_dev.ap->lock)
197 __acquires(host->host_lock)
198{
199 int res = 0;
200 struct domain_device *dev = cmd_to_domain_dev(cmd);
201 struct Scsi_Host *host = cmd->device->host;
202 struct sas_internal *i = to_sas_internal(host->transportt);
203
204 spin_unlock_irq(host->host_lock);
205
206 {
207 struct sas_ha_struct *sas_ha = dev->port->ha;
208 struct sas_task *task;
209
210 /* If the device fell off, no sense in issuing commands */
211 if (dev->gone) {
212 cmd->result = DID_BAD_TARGET << 16;
213 scsi_done(cmd);
214 goto out;
215 }
216
217 if (dev_is_sata(dev)) {
218 unsigned long flags;
219
220 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
221 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
222 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
223 goto out;
224 }
225
226 res = -ENOMEM;
227 task = sas_create_task(cmd, dev, GFP_ATOMIC);
228 if (!task)
229 goto out;
230
231 cmd->scsi_done = scsi_done;
232 /* Queue up, Direct Mode or Task Collector Mode. */
233 if (sas_ha->lldd_max_execute_num < 2)
234 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
235 else
236 res = sas_queue_up(task);
237
238 /* Examine */
239 if (res) {
240 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
241 ASSIGN_SAS_TASK(cmd, NULL);
242 sas_free_task(task);
243 if (res == -SAS_QUEUE_FULL) {
244 cmd->result = DID_SOFT_ERROR << 16; /* retry */
245 res = 0;
246 scsi_done(cmd);
247 }
248 goto out;
249 }
250 }
251out:
252 spin_lock_irq(host->host_lock);
253 return res;
254}
255
256DEF_SCSI_QCMD(sas_queuecommand)
257
258static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
259{
260 struct sas_task *task = TO_SAS_TASK(cmd);
261 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
262
263 /* remove the aborted task flag to allow the task to be
264 * completed now. At this point, we only get called following
265 * an actual abort of the task, so we should be guaranteed not
266 * to be racing with any completions from the LLD (hence we
267 * don't need the task state lock to clear the flag) */
268 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
269 /* Now call task_done. However, task will be free'd after
270 * this */
271 task->task_done(task);
272 /* now finish the command and move it on to the error
273 * handler done list, this also takes it off the
274 * error handler pending list */
275 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
276}
277
278static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
279{
280 struct scsi_cmnd *cmd, *n;
281
282 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
283 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
284 cmd->device->lun == my_cmd->device->lun)
285 sas_eh_finish_cmd(cmd);
286 }
287}
288
289static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
290 struct domain_device *dev)
291{
292 struct scsi_cmnd *cmd, *n;
293
294 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
295 struct domain_device *x = cmd_to_domain_dev(cmd);
296
297 if (x == dev)
298 sas_eh_finish_cmd(cmd);
299 }
300}
301
302static void sas_scsi_clear_queue_port(struct list_head *error_q,
303 struct asd_sas_port *port)
304{
305 struct scsi_cmnd *cmd, *n;
306
307 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
308 struct domain_device *dev = cmd_to_domain_dev(cmd);
309 struct asd_sas_port *x = dev->port;
310
311 if (x == port)
312 sas_eh_finish_cmd(cmd);
313 }
314}
315
316enum task_disposition {
317 TASK_IS_DONE,
318 TASK_IS_ABORTED,
319 TASK_IS_AT_LU,
320 TASK_IS_NOT_AT_LU,
321 TASK_ABORT_FAILED,
322};
323
324static enum task_disposition sas_scsi_find_task(struct sas_task *task)
325{
326 struct sas_ha_struct *ha = task->dev->port->ha;
327 unsigned long flags;
328 int i, res;
329 struct sas_internal *si =
330 to_sas_internal(task->dev->port->ha->core.shost->transportt);
331
332 if (ha->lldd_max_execute_num > 1) {
333 struct scsi_core *core = &ha->core;
334 struct sas_task *t, *n;
335
336 spin_lock_irqsave(&core->task_queue_lock, flags);
337 list_for_each_entry_safe(t, n, &core->task_queue, list) {
338 if (task == t) {
339 list_del_init(&t->list);
340 spin_unlock_irqrestore(&core->task_queue_lock,
341 flags);
342 SAS_DPRINTK("%s: task 0x%p aborted from "
343 "task_queue\n",
344 __func__, task);
345 return TASK_IS_ABORTED;
346 }
347 }
348 spin_unlock_irqrestore(&core->task_queue_lock, flags);
349 }
350
351 for (i = 0; i < 5; i++) {
352 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
353 res = si->dft->lldd_abort_task(task);
354
355 spin_lock_irqsave(&task->task_state_lock, flags);
356 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
357 spin_unlock_irqrestore(&task->task_state_lock, flags);
358 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
359 task);
360 return TASK_IS_DONE;
361 }
362 spin_unlock_irqrestore(&task->task_state_lock, flags);
363
364 if (res == TMF_RESP_FUNC_COMPLETE) {
365 SAS_DPRINTK("%s: task 0x%p is aborted\n",
366 __func__, task);
367 return TASK_IS_ABORTED;
368 } else if (si->dft->lldd_query_task) {
369 SAS_DPRINTK("%s: querying task 0x%p\n",
370 __func__, task);
371 res = si->dft->lldd_query_task(task);
372 switch (res) {
373 case TMF_RESP_FUNC_SUCC:
374 SAS_DPRINTK("%s: task 0x%p at LU\n",
375 __func__, task);
376 return TASK_IS_AT_LU;
377 case TMF_RESP_FUNC_COMPLETE:
378 SAS_DPRINTK("%s: task 0x%p not at LU\n",
379 __func__, task);
380 return TASK_IS_NOT_AT_LU;
381 case TMF_RESP_FUNC_FAILED:
382 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
383 __func__, task);
384 return TASK_ABORT_FAILED;
385 }
386
387 }
388 }
389 return res;
390}
391
392static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
393{
394 int res = TMF_RESP_FUNC_FAILED;
395 struct scsi_lun lun;
396 struct sas_internal *i =
397 to_sas_internal(dev->port->ha->core.shost->transportt);
398
399 int_to_scsilun(cmd->device->lun, &lun);
400
401 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
402 SAS_ADDR(dev->sas_addr),
403 cmd->device->lun);
404
405 if (i->dft->lldd_abort_task_set)
406 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
407
408 if (res == TMF_RESP_FUNC_FAILED) {
409 if (i->dft->lldd_clear_task_set)
410 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
411 }
412
413 if (res == TMF_RESP_FUNC_FAILED) {
414 if (i->dft->lldd_lu_reset)
415 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
416 }
417
418 return res;
419}
420
421static int sas_recover_I_T(struct domain_device *dev)
422{
423 int res = TMF_RESP_FUNC_FAILED;
424 struct sas_internal *i =
425 to_sas_internal(dev->port->ha->core.shost->transportt);
426
427 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
428 SAS_ADDR(dev->sas_addr));
429
430 if (i->dft->lldd_I_T_nexus_reset)
431 res = i->dft->lldd_I_T_nexus_reset(dev);
432
433 return res;
434}
435
436/* Find the sas_phy that's attached to this device */
437struct sas_phy *sas_find_local_phy(struct domain_device *dev)
438{
439 struct domain_device *pdev = dev->parent;
440 struct ex_phy *exphy = NULL;
441 int i;
442
443 /* Directly attached device */
444 if (!pdev)
445 return dev->port->phy;
446
447 /* Otherwise look in the expander */
448 for (i = 0; i < pdev->ex_dev.num_phys; i++)
449 if (!memcmp(dev->sas_addr,
450 pdev->ex_dev.ex_phy[i].attached_sas_addr,
451 SAS_ADDR_SIZE)) {
452 exphy = &pdev->ex_dev.ex_phy[i];
453 break;
454 }
455
456 BUG_ON(!exphy);
457 return exphy->phy;
458}
459EXPORT_SYMBOL_GPL(sas_find_local_phy);
460
461/* Attempt to send a LUN reset message to a device */
462int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
463{
464 struct domain_device *dev = cmd_to_domain_dev(cmd);
465 struct sas_internal *i =
466 to_sas_internal(dev->port->ha->core.shost->transportt);
467 struct scsi_lun lun;
468 int res;
469
470 int_to_scsilun(cmd->device->lun, &lun);
471
472 if (!i->dft->lldd_lu_reset)
473 return FAILED;
474
475 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
476 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
477 return SUCCESS;
478
479 return FAILED;
480}
481
482/* Attempt to send a phy (bus) reset */
483int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
484{
485 struct domain_device *dev = cmd_to_domain_dev(cmd);
486 struct sas_phy *phy = sas_find_local_phy(dev);
487 int res;
488
489 res = sas_phy_reset(phy, 1);
490 if (res)
491 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
492 kobject_name(&phy->dev.kobj),
493 res);
494 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
495 return SUCCESS;
496
497 return FAILED;
498}
499
500/* Try to reset a device */
501static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
502{
503 int res;
504 struct Scsi_Host *shost = cmd->device->host;
505
506 if (!shost->hostt->eh_device_reset_handler)
507 goto try_bus_reset;
508
509 res = shost->hostt->eh_device_reset_handler(cmd);
510 if (res == SUCCESS)
511 return res;
512
513try_bus_reset:
514 if (shost->hostt->eh_bus_reset_handler)
515 return shost->hostt->eh_bus_reset_handler(cmd);
516
517 return FAILED;
518}
519
520static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
521 struct list_head *work_q,
522 struct list_head *done_q)
523{
524 struct scsi_cmnd *cmd, *n;
525 enum task_disposition res = TASK_IS_DONE;
526 int tmf_resp, need_reset;
527 struct sas_internal *i = to_sas_internal(shost->transportt);
528 unsigned long flags;
529 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
530
531Again:
532 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
533 struct sas_task *task = TO_SAS_TASK(cmd);
534
535 if (!task)
536 continue;
537
538 list_del_init(&cmd->eh_entry);
539
540 spin_lock_irqsave(&task->task_state_lock, flags);
541 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
542 spin_unlock_irqrestore(&task->task_state_lock, flags);
543
544 if (need_reset) {
545 SAS_DPRINTK("%s: task 0x%p requests reset\n",
546 __func__, task);
547 goto reset;
548 }
549
550 SAS_DPRINTK("trying to find task 0x%p\n", task);
551 res = sas_scsi_find_task(task);
552
553 cmd->eh_eflags = 0;
554
555 switch (res) {
556 case TASK_IS_DONE:
557 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
558 task);
559 sas_eh_finish_cmd(cmd);
560 continue;
561 case TASK_IS_ABORTED:
562 SAS_DPRINTK("%s: task 0x%p is aborted\n",
563 __func__, task);
564 sas_eh_finish_cmd(cmd);
565 continue;
566 case TASK_IS_AT_LU:
567 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
568 reset:
569 tmf_resp = sas_recover_lu(task->dev, cmd);
570 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
571 SAS_DPRINTK("dev %016llx LU %x is "
572 "recovered\n",
573 SAS_ADDR(task->dev),
574 cmd->device->lun);
575 sas_eh_finish_cmd(cmd);
576 sas_scsi_clear_queue_lu(work_q, cmd);
577 goto Again;
578 }
579 /* fallthrough */
580 case TASK_IS_NOT_AT_LU:
581 case TASK_ABORT_FAILED:
582 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
583 task);
584 tmf_resp = sas_recover_I_T(task->dev);
585 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
586 struct domain_device *dev = task->dev;
587 SAS_DPRINTK("I_T %016llx recovered\n",
588 SAS_ADDR(task->dev->sas_addr));
589 sas_eh_finish_cmd(cmd);
590 sas_scsi_clear_queue_I_T(work_q, dev);
591 goto Again;
592 }
593 /* Hammer time :-) */
594 try_to_reset_cmd_device(cmd);
595 if (i->dft->lldd_clear_nexus_port) {
596 struct asd_sas_port *port = task->dev->port;
597 SAS_DPRINTK("clearing nexus for port:%d\n",
598 port->id);
599 res = i->dft->lldd_clear_nexus_port(port);
600 if (res == TMF_RESP_FUNC_COMPLETE) {
601 SAS_DPRINTK("clear nexus port:%d "
602 "succeeded\n", port->id);
603 sas_eh_finish_cmd(cmd);
604 sas_scsi_clear_queue_port(work_q,
605 port);
606 goto Again;
607 }
608 }
609 if (i->dft->lldd_clear_nexus_ha) {
610 SAS_DPRINTK("clear nexus ha\n");
611 res = i->dft->lldd_clear_nexus_ha(ha);
612 if (res == TMF_RESP_FUNC_COMPLETE) {
613 SAS_DPRINTK("clear nexus ha "
614 "succeeded\n");
615 sas_eh_finish_cmd(cmd);
616 goto clear_q;
617 }
618 }
619 /* If we are here -- this means that no amount
620 * of effort could recover from errors. Quite
621 * possibly the HA just disappeared.
622 */
623 SAS_DPRINTK("error from device %llx, LUN %x "
624 "couldn't be recovered in any way\n",
625 SAS_ADDR(task->dev->sas_addr),
626 cmd->device->lun);
627
628 sas_eh_finish_cmd(cmd);
629 goto clear_q;
630 }
631 }
632 return list_empty(work_q);
633clear_q:
634 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
635 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
636 sas_eh_finish_cmd(cmd);
637
638 return list_empty(work_q);
639}
640
641void sas_scsi_recover_host(struct Scsi_Host *shost)
642{
643 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
644 unsigned long flags;
645 LIST_HEAD(eh_work_q);
646
647 spin_lock_irqsave(shost->host_lock, flags);
648 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
649 shost->host_eh_scheduled = 0;
650 spin_unlock_irqrestore(shost->host_lock, flags);
651
652 SAS_DPRINTK("Enter %s\n", __func__);
653 /*
654 * Deal with commands that still have SAS tasks (i.e. they didn't
655 * complete via the normal sas_task completion mechanism)
656 */
657 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
658 goto out;
659
660 /*
661 * Now deal with SCSI commands that completed ok but have a an error
662 * code (and hopefully sense data) attached. This is roughly what
663 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
664 * command we see here has no sas_task and is thus unknown to the HA.
665 */
666 if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
667 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
668 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
669
670out:
671 /* now link into libata eh --- if we have any ata devices */
672 sas_ata_strategy_handler(shost);
673
674 scsi_eh_flush_done_q(&ha->eh_done_q);
675
676 SAS_DPRINTK("--- Exit %s\n", __func__);
677 return;
678}
679
680enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
681{
682 struct sas_task *task = TO_SAS_TASK(cmd);
683 unsigned long flags;
684 enum blk_eh_timer_return rtn;
685
686 if (sas_ata_timed_out(cmd, task, &rtn))
687 return rtn;
688
689 if (!task) {
690 cmd->request->timeout /= 2;
691 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
692 cmd, task, (cmd->request->timeout ?
693 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
694 if (!cmd->request->timeout)
695 return BLK_EH_NOT_HANDLED;
696 return BLK_EH_RESET_TIMER;
697 }
698
699 spin_lock_irqsave(&task->task_state_lock, flags);
700 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
701 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
702 spin_unlock_irqrestore(&task->task_state_lock, flags);
703 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
704 "BLK_EH_HANDLED\n", cmd, task);
705 return BLK_EH_HANDLED;
706 }
707 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
708 spin_unlock_irqrestore(&task->task_state_lock, flags);
709 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
710 "BLK_EH_RESET_TIMER\n",
711 cmd, task);
712 return BLK_EH_RESET_TIMER;
713 }
714 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
715 spin_unlock_irqrestore(&task->task_state_lock, flags);
716
717 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
718 cmd, task);
719
720 return BLK_EH_NOT_HANDLED;
721}
722
723int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
724{
725 struct domain_device *dev = sdev_to_domain_dev(sdev);
726
727 if (dev_is_sata(dev))
728 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
729
730 return -EINVAL;
731}
732
733struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
734{
735 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
736 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
737 struct domain_device *found_dev = NULL;
738 int i;
739 unsigned long flags;
740
741 spin_lock_irqsave(&ha->phy_port_lock, flags);
742 for (i = 0; i < ha->num_phys; i++) {
743 struct asd_sas_port *port = ha->sas_port[i];
744 struct domain_device *dev;
745
746 spin_lock(&port->dev_list_lock);
747 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
748 if (rphy == dev->rphy) {
749 found_dev = dev;
750 spin_unlock(&port->dev_list_lock);
751 goto found;
752 }
753 }
754 spin_unlock(&port->dev_list_lock);
755 }
756 found:
757 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
758
759 return found_dev;
760}
761
762static inline struct domain_device *sas_find_target(struct scsi_target *starget)
763{
764 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
765
766 return sas_find_dev_by_rphy(rphy);
767}
768
769int sas_target_alloc(struct scsi_target *starget)
770{
771 struct domain_device *found_dev = sas_find_target(starget);
772 int res;
773
774 if (!found_dev)
775 return -ENODEV;
776
777 if (dev_is_sata(found_dev)) {
778 res = sas_ata_init_host_and_port(found_dev, starget);
779 if (res)
780 return res;
781 }
782
783 starget->hostdata = found_dev;
784 return 0;
785}
786
787#define SAS_DEF_QD 32
788#define SAS_MAX_QD 64
789
790int sas_slave_configure(struct scsi_device *scsi_dev)
791{
792 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
793 struct sas_ha_struct *sas_ha;
794
795 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
796
797 if (dev_is_sata(dev)) {
798 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
799 return 0;
800 }
801
802 sas_ha = dev->port->ha;
803
804 sas_read_port_mode_page(scsi_dev);
805
806 if (scsi_dev->tagged_supported) {
807 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
808 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
809 } else {
810 SAS_DPRINTK("device %llx, LUN %x doesn't support "
811 "TCQ\n", SAS_ADDR(dev->sas_addr),
812 scsi_dev->lun);
813 scsi_dev->tagged_supported = 0;
814 scsi_set_tag_type(scsi_dev, 0);
815 scsi_deactivate_tcq(scsi_dev, 1);
816 }
817
818 scsi_dev->allow_restart = 1;
819
820 return 0;
821}
822
823void sas_slave_destroy(struct scsi_device *scsi_dev)
824{
825 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
826
827 if (dev_is_sata(dev))
828 dev->sata_dev.ap->link.device[0].class = ATA_DEV_NONE;
829}
830
831int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth,
832 int reason)
833{
834 int res = min(new_depth, SAS_MAX_QD);
835
836 if (reason != SCSI_QDEPTH_DEFAULT)
837 return -EOPNOTSUPP;
838
839 if (scsi_dev->tagged_supported)
840 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
841 res);
842 else {
843 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
844 sas_printk("device %llx LUN %x queue depth changed to 1\n",
845 SAS_ADDR(dev->sas_addr),
846 scsi_dev->lun);
847 scsi_adjust_queue_depth(scsi_dev, 0, 1);
848 res = 1;
849 }
850
851 return res;
852}
853
854int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
855{
856 if (!scsi_dev->tagged_supported)
857 return 0;
858
859 scsi_deactivate_tcq(scsi_dev, 1);
860
861 scsi_set_tag_type(scsi_dev, qt);
862 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
863
864 return qt;
865}
866
867int sas_bios_param(struct scsi_device *scsi_dev,
868 struct block_device *bdev,
869 sector_t capacity, int *hsc)
870{
871 hsc[0] = 255;
872 hsc[1] = 63;
873 sector_div(capacity, 255*63);
874 hsc[2] = capacity;
875
876 return 0;
877}
878
879/* ---------- Task Collector Thread implementation ---------- */
880
881static void sas_queue(struct sas_ha_struct *sas_ha)
882{
883 struct scsi_core *core = &sas_ha->core;
884 unsigned long flags;
885 LIST_HEAD(q);
886 int can_queue;
887 int res;
888 struct sas_internal *i = to_sas_internal(core->shost->transportt);
889
890 spin_lock_irqsave(&core->task_queue_lock, flags);
891 while (!kthread_should_stop() &&
892 !list_empty(&core->task_queue)) {
893
894 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
895 if (can_queue >= 0) {
896 can_queue = core->task_queue_size;
897 list_splice_init(&core->task_queue, &q);
898 } else {
899 struct list_head *a, *n;
900
901 can_queue = sas_ha->lldd_queue_size;
902 list_for_each_safe(a, n, &core->task_queue) {
903 list_move_tail(a, &q);
904 if (--can_queue == 0)
905 break;
906 }
907 can_queue = sas_ha->lldd_queue_size;
908 }
909 core->task_queue_size -= can_queue;
910 spin_unlock_irqrestore(&core->task_queue_lock, flags);
911 {
912 struct sas_task *task = list_entry(q.next,
913 struct sas_task,
914 list);
915 list_del_init(&q);
916 res = i->dft->lldd_execute_task(task, can_queue,
917 GFP_KERNEL);
918 if (unlikely(res))
919 __list_add(&q, task->list.prev, &task->list);
920 }
921 spin_lock_irqsave(&core->task_queue_lock, flags);
922 if (res) {
923 list_splice_init(&q, &core->task_queue); /*at head*/
924 core->task_queue_size += can_queue;
925 }
926 }
927 spin_unlock_irqrestore(&core->task_queue_lock, flags);
928}
929
930/**
931 * sas_queue_thread -- The Task Collector thread
932 * @_sas_ha: pointer to struct sas_ha
933 */
934static int sas_queue_thread(void *_sas_ha)
935{
936 struct sas_ha_struct *sas_ha = _sas_ha;
937
938 while (1) {
939 set_current_state(TASK_INTERRUPTIBLE);
940 schedule();
941 sas_queue(sas_ha);
942 if (kthread_should_stop())
943 break;
944 }
945
946 return 0;
947}
948
949int sas_init_queue(struct sas_ha_struct *sas_ha)
950{
951 struct scsi_core *core = &sas_ha->core;
952
953 spin_lock_init(&core->task_queue_lock);
954 core->task_queue_size = 0;
955 INIT_LIST_HEAD(&core->task_queue);
956
957 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
958 "sas_queue_%d", core->shost->host_no);
959 if (IS_ERR(core->queue_thread))
960 return PTR_ERR(core->queue_thread);
961 return 0;
962}
963
964void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
965{
966 unsigned long flags;
967 struct scsi_core *core = &sas_ha->core;
968 struct sas_task *task, *n;
969
970 kthread_stop(core->queue_thread);
971
972 if (!list_empty(&core->task_queue))
973 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
974 SAS_ADDR(sas_ha->sas_addr));
975
976 spin_lock_irqsave(&core->task_queue_lock, flags);
977 list_for_each_entry_safe(task, n, &core->task_queue, list) {
978 struct scsi_cmnd *cmd = task->uldd_task;
979
980 list_del_init(&task->list);
981
982 ASSIGN_SAS_TASK(cmd, NULL);
983 sas_free_task(task);
984 cmd->result = DID_ABORT << 16;
985 cmd->scsi_done(cmd);
986 }
987 spin_unlock_irqrestore(&core->task_queue_lock, flags);
988}
989
990/*
991 * Call the LLDD task abort routine directly. This function is intended for
992 * use by upper layers that need to tell the LLDD to abort a task.
993 */
994int __sas_task_abort(struct sas_task *task)
995{
996 struct sas_internal *si =
997 to_sas_internal(task->dev->port->ha->core.shost->transportt);
998 unsigned long flags;
999 int res;
1000
1001 spin_lock_irqsave(&task->task_state_lock, flags);
1002 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
1003 task->task_state_flags & SAS_TASK_STATE_DONE) {
1004 spin_unlock_irqrestore(&task->task_state_lock, flags);
1005 SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
1006 task);
1007 return 0;
1008 }
1009 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1010 spin_unlock_irqrestore(&task->task_state_lock, flags);
1011
1012 if (!si->dft->lldd_abort_task)
1013 return -ENODEV;
1014
1015 res = si->dft->lldd_abort_task(task);
1016
1017 spin_lock_irqsave(&task->task_state_lock, flags);
1018 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
1019 (res == TMF_RESP_FUNC_COMPLETE))
1020 {
1021 spin_unlock_irqrestore(&task->task_state_lock, flags);
1022 task->task_done(task);
1023 return 0;
1024 }
1025
1026 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1027 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1028 spin_unlock_irqrestore(&task->task_state_lock, flags);
1029
1030 return -EAGAIN;
1031}
1032
1033/*
1034 * Tell an upper layer that it needs to initiate an abort for a given task.
1035 * This should only ever be called by an LLDD.
1036 */
1037void sas_task_abort(struct sas_task *task)
1038{
1039 struct scsi_cmnd *sc = task->uldd_task;
1040
1041 /* Escape for libsas internal commands */
1042 if (!sc) {
1043 if (!del_timer(&task->timer))
1044 return;
1045 task->timer.function(task->timer.data);
1046 return;
1047 }
1048
1049 if (dev_is_sata(task->dev)) {
1050 sas_ata_task_abort(task);
1051 } else {
1052 struct request_queue *q = sc->device->request_queue;
1053 unsigned long flags;
1054
1055 spin_lock_irqsave(q->queue_lock, flags);
1056 blk_abort_request(sc->request);
1057 spin_unlock_irqrestore(q->queue_lock, flags);
1058 scsi_schedule_eh(sc->device->host);
1059 }
1060}
1061
1062int sas_slave_alloc(struct scsi_device *scsi_dev)
1063{
1064 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1065
1066 if (dev_is_sata(dev))
1067 return ata_sas_port_init(dev->sata_dev.ap);
1068
1069 return 0;
1070}
1071
1072void sas_target_destroy(struct scsi_target *starget)
1073{
1074 struct domain_device *found_dev = sas_find_target(starget);
1075
1076 if (!found_dev)
1077 return;
1078
1079 if (dev_is_sata(found_dev))
1080 ata_sas_port_destroy(found_dev->sata_dev.ap);
1081
1082 return;
1083}
1084
1085static void sas_parse_addr(u8 *sas_addr, const char *p)
1086{
1087 int i;
1088 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1089 u8 h, l;
1090 if (!*p)
1091 break;
1092 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1093 p++;
1094 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1095 p++;
1096 sas_addr[i] = (h<<4) | l;
1097 }
1098}
1099
1100#define SAS_STRING_ADDR_SIZE 16
1101
1102int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1103{
1104 int res;
1105 const struct firmware *fw;
1106
1107 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1108 if (res)
1109 return res;
1110
1111 if (fw->size < SAS_STRING_ADDR_SIZE) {
1112 res = -ENODEV;
1113 goto out;
1114 }
1115
1116 sas_parse_addr(addr, fw->data);
1117
1118out:
1119 release_firmware(fw);
1120 return res;
1121}
1122EXPORT_SYMBOL_GPL(sas_request_addr);
1123
1124EXPORT_SYMBOL_GPL(sas_queuecommand);
1125EXPORT_SYMBOL_GPL(sas_target_alloc);
1126EXPORT_SYMBOL_GPL(sas_slave_configure);
1127EXPORT_SYMBOL_GPL(sas_slave_destroy);
1128EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1129EXPORT_SYMBOL_GPL(sas_change_queue_type);
1130EXPORT_SYMBOL_GPL(sas_bios_param);
1131EXPORT_SYMBOL_GPL(__sas_task_abort);
1132EXPORT_SYMBOL_GPL(sas_task_abort);
1133EXPORT_SYMBOL_GPL(sas_phy_reset);
1134EXPORT_SYMBOL_GPL(sas_phy_enable);
1135EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1136EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1137EXPORT_SYMBOL_GPL(sas_slave_alloc);
1138EXPORT_SYMBOL_GPL(sas_target_destroy);
1139EXPORT_SYMBOL_GPL(sas_ioctl);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Serial Attached SCSI (SAS) class SCSI Host glue.
4 *
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
6 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 */
8
9#include <linux/kthread.h>
10#include <linux/firmware.h>
11#include <linux/export.h>
12#include <linux/ctype.h>
13#include <linux/kernel.h>
14
15#include "sas_internal.h"
16
17#include <scsi/scsi_host.h>
18#include <scsi/scsi_device.h>
19#include <scsi/scsi_tcq.h>
20#include <scsi/scsi.h>
21#include <scsi/scsi_eh.h>
22#include <scsi/scsi_transport.h>
23#include <scsi/scsi_transport_sas.h>
24#include <scsi/sas_ata.h>
25#include "scsi_sas_internal.h"
26#include "scsi_transport_api.h"
27#include "scsi_priv.h"
28
29#include <linux/err.h>
30#include <linux/blkdev.h>
31#include <linux/freezer.h>
32#include <linux/gfp.h>
33#include <linux/scatterlist.h>
34#include <linux/libata.h>
35
36/* record final status and free the task */
37static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
38{
39 struct task_status_struct *ts = &task->task_status;
40 enum scsi_host_status hs = DID_OK;
41 enum exec_status stat = SAS_SAM_STAT_GOOD;
42
43 if (ts->resp == SAS_TASK_UNDELIVERED) {
44 /* transport error */
45 hs = DID_NO_CONNECT;
46 } else { /* ts->resp == SAS_TASK_COMPLETE */
47 /* task delivered, what happened afterwards? */
48 switch (ts->stat) {
49 case SAS_DEV_NO_RESPONSE:
50 case SAS_INTERRUPTED:
51 case SAS_PHY_DOWN:
52 case SAS_NAK_R_ERR:
53 case SAS_OPEN_TO:
54 hs = DID_NO_CONNECT;
55 break;
56 case SAS_DATA_UNDERRUN:
57 scsi_set_resid(sc, ts->residual);
58 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
59 hs = DID_ERROR;
60 break;
61 case SAS_DATA_OVERRUN:
62 hs = DID_ERROR;
63 break;
64 case SAS_QUEUE_FULL:
65 hs = DID_SOFT_ERROR; /* retry */
66 break;
67 case SAS_DEVICE_UNKNOWN:
68 hs = DID_BAD_TARGET;
69 break;
70 case SAS_OPEN_REJECT:
71 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
72 hs = DID_SOFT_ERROR; /* retry */
73 else
74 hs = DID_ERROR;
75 break;
76 case SAS_PROTO_RESPONSE:
77 pr_notice("LLDD:%s sent SAS_PROTO_RESP for an SSP task; please report this\n",
78 task->dev->port->ha->sas_ha_name);
79 break;
80 case SAS_ABORTED_TASK:
81 hs = DID_ABORT;
82 break;
83 case SAS_SAM_STAT_CHECK_CONDITION:
84 memcpy(sc->sense_buffer, ts->buf,
85 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
86 stat = SAS_SAM_STAT_CHECK_CONDITION;
87 break;
88 default:
89 stat = ts->stat;
90 break;
91 }
92 }
93
94 sc->result = (hs << 16) | stat;
95 ASSIGN_SAS_TASK(sc, NULL);
96 sas_free_task(task);
97}
98
99static void sas_scsi_task_done(struct sas_task *task)
100{
101 struct scsi_cmnd *sc = task->uldd_task;
102 struct domain_device *dev = task->dev;
103 struct sas_ha_struct *ha = dev->port->ha;
104 unsigned long flags;
105
106 spin_lock_irqsave(&dev->done_lock, flags);
107 if (test_bit(SAS_HA_FROZEN, &ha->state))
108 task = NULL;
109 else
110 ASSIGN_SAS_TASK(sc, NULL);
111 spin_unlock_irqrestore(&dev->done_lock, flags);
112
113 if (unlikely(!task)) {
114 /* task will be completed by the error handler */
115 pr_debug("task done but aborted\n");
116 return;
117 }
118
119 if (unlikely(!sc)) {
120 pr_debug("task_done called with non existing SCSI cmnd!\n");
121 sas_free_task(task);
122 return;
123 }
124
125 sas_end_task(sc, task);
126 scsi_done(sc);
127}
128
129static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
130 struct domain_device *dev,
131 gfp_t gfp_flags)
132{
133 struct sas_task *task = sas_alloc_task(gfp_flags);
134 struct scsi_lun lun;
135
136 if (!task)
137 return NULL;
138
139 task->uldd_task = cmd;
140 ASSIGN_SAS_TASK(cmd, task);
141
142 task->dev = dev;
143 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
144
145 int_to_scsilun(cmd->device->lun, &lun);
146 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
147 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
148 task->ssp_task.cmd = cmd;
149
150 task->scatter = scsi_sglist(cmd);
151 task->num_scatter = scsi_sg_count(cmd);
152 task->total_xfer_len = scsi_bufflen(cmd);
153 task->data_dir = cmd->sc_data_direction;
154
155 task->task_done = sas_scsi_task_done;
156
157 return task;
158}
159
160int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
161{
162 struct sas_internal *i = to_sas_internal(host->transportt);
163 struct domain_device *dev = cmd_to_domain_dev(cmd);
164 struct sas_task *task;
165 int res = 0;
166
167 /* If the device fell off, no sense in issuing commands */
168 if (test_bit(SAS_DEV_GONE, &dev->state)) {
169 cmd->result = DID_BAD_TARGET << 16;
170 goto out_done;
171 }
172
173 if (dev_is_sata(dev)) {
174 spin_lock_irq(dev->sata_dev.ap->lock);
175 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
176 spin_unlock_irq(dev->sata_dev.ap->lock);
177 return res;
178 }
179
180 task = sas_create_task(cmd, dev, GFP_ATOMIC);
181 if (!task)
182 return SCSI_MLQUEUE_HOST_BUSY;
183
184 res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
185 if (res)
186 goto out_free_task;
187 return 0;
188
189out_free_task:
190 pr_debug("lldd_execute_task returned: %d\n", res);
191 ASSIGN_SAS_TASK(cmd, NULL);
192 sas_free_task(task);
193 if (res == -SAS_QUEUE_FULL)
194 cmd->result = DID_SOFT_ERROR << 16; /* retry */
195 else
196 cmd->result = DID_ERROR << 16;
197out_done:
198 scsi_done(cmd);
199 return 0;
200}
201EXPORT_SYMBOL_GPL(sas_queuecommand);
202
203static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
204{
205 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
206 struct domain_device *dev = cmd_to_domain_dev(cmd);
207 struct sas_task *task = TO_SAS_TASK(cmd);
208
209 /* At this point, we only get called following an actual abort
210 * of the task, so we should be guaranteed not to be racing with
211 * any completions from the LLD. Task is freed after this.
212 */
213 sas_end_task(cmd, task);
214
215 if (dev_is_sata(dev)) {
216 /* defer commands to libata so that libata EH can
217 * handle ata qcs correctly
218 */
219 list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q);
220 return;
221 }
222
223 /* now finish the command and move it on to the error
224 * handler done list, this also takes it off the
225 * error handler pending list.
226 */
227 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
228}
229
230static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
231{
232 struct scsi_cmnd *cmd, *n;
233
234 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
235 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
236 cmd->device->lun == my_cmd->device->lun)
237 sas_eh_finish_cmd(cmd);
238 }
239}
240
241static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
242 struct domain_device *dev)
243{
244 struct scsi_cmnd *cmd, *n;
245
246 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
247 struct domain_device *x = cmd_to_domain_dev(cmd);
248
249 if (x == dev)
250 sas_eh_finish_cmd(cmd);
251 }
252}
253
254static void sas_scsi_clear_queue_port(struct list_head *error_q,
255 struct asd_sas_port *port)
256{
257 struct scsi_cmnd *cmd, *n;
258
259 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
260 struct domain_device *dev = cmd_to_domain_dev(cmd);
261 struct asd_sas_port *x = dev->port;
262
263 if (x == port)
264 sas_eh_finish_cmd(cmd);
265 }
266}
267
268enum task_disposition {
269 TASK_IS_DONE,
270 TASK_IS_ABORTED,
271 TASK_IS_AT_LU,
272 TASK_IS_NOT_AT_LU,
273 TASK_ABORT_FAILED,
274};
275
276static enum task_disposition sas_scsi_find_task(struct sas_task *task)
277{
278 unsigned long flags;
279 int i, res;
280 struct sas_internal *si =
281 to_sas_internal(task->dev->port->ha->shost->transportt);
282
283 for (i = 0; i < 5; i++) {
284 pr_notice("%s: aborting task 0x%p\n", __func__, task);
285 res = si->dft->lldd_abort_task(task);
286
287 spin_lock_irqsave(&task->task_state_lock, flags);
288 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
289 spin_unlock_irqrestore(&task->task_state_lock, flags);
290 pr_debug("%s: task 0x%p is done\n", __func__, task);
291 return TASK_IS_DONE;
292 }
293 spin_unlock_irqrestore(&task->task_state_lock, flags);
294
295 if (res == TMF_RESP_FUNC_COMPLETE) {
296 pr_notice("%s: task 0x%p is aborted\n",
297 __func__, task);
298 return TASK_IS_ABORTED;
299 } else if (si->dft->lldd_query_task) {
300 pr_notice("%s: querying task 0x%p\n", __func__, task);
301 res = si->dft->lldd_query_task(task);
302 switch (res) {
303 case TMF_RESP_FUNC_SUCC:
304 pr_notice("%s: task 0x%p at LU\n", __func__,
305 task);
306 return TASK_IS_AT_LU;
307 case TMF_RESP_FUNC_COMPLETE:
308 pr_notice("%s: task 0x%p not at LU\n",
309 __func__, task);
310 return TASK_IS_NOT_AT_LU;
311 case TMF_RESP_FUNC_FAILED:
312 pr_notice("%s: task 0x%p failed to abort\n",
313 __func__, task);
314 return TASK_ABORT_FAILED;
315 default:
316 pr_notice("%s: task 0x%p result code %d not handled\n",
317 __func__, task, res);
318 }
319 }
320 }
321 return TASK_ABORT_FAILED;
322}
323
324static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
325{
326 int res = TMF_RESP_FUNC_FAILED;
327 struct scsi_lun lun;
328 struct sas_internal *i =
329 to_sas_internal(dev->port->ha->shost->transportt);
330
331 int_to_scsilun(cmd->device->lun, &lun);
332
333 pr_notice("eh: device %016llx LUN 0x%llx has the task\n",
334 SAS_ADDR(dev->sas_addr),
335 cmd->device->lun);
336
337 if (i->dft->lldd_abort_task_set)
338 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
339
340 if (res == TMF_RESP_FUNC_FAILED) {
341 if (i->dft->lldd_clear_task_set)
342 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
343 }
344
345 if (res == TMF_RESP_FUNC_FAILED) {
346 if (i->dft->lldd_lu_reset)
347 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
348 }
349
350 return res;
351}
352
353static int sas_recover_I_T(struct domain_device *dev)
354{
355 int res = TMF_RESP_FUNC_FAILED;
356 struct sas_internal *i =
357 to_sas_internal(dev->port->ha->shost->transportt);
358
359 pr_notice("I_T nexus reset for dev %016llx\n",
360 SAS_ADDR(dev->sas_addr));
361
362 if (i->dft->lldd_I_T_nexus_reset)
363 res = i->dft->lldd_I_T_nexus_reset(dev);
364
365 return res;
366}
367
368/* take a reference on the last known good phy for this device */
369struct sas_phy *sas_get_local_phy(struct domain_device *dev)
370{
371 struct sas_ha_struct *ha = dev->port->ha;
372 struct sas_phy *phy;
373 unsigned long flags;
374
375 /* a published domain device always has a valid phy, it may be
376 * stale, but it is never NULL
377 */
378 BUG_ON(!dev->phy);
379
380 spin_lock_irqsave(&ha->phy_port_lock, flags);
381 phy = dev->phy;
382 get_device(&phy->dev);
383 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
384
385 return phy;
386}
387EXPORT_SYMBOL_GPL(sas_get_local_phy);
388
389static int sas_queue_reset(struct domain_device *dev, int reset_type, u64 lun)
390{
391 struct sas_ha_struct *ha = dev->port->ha;
392 int scheduled = 0, tries = 100;
393
394 /* ata: promote lun reset to bus reset */
395 if (dev_is_sata(dev)) {
396 sas_ata_schedule_reset(dev);
397 return SUCCESS;
398 }
399
400 while (!scheduled && tries--) {
401 spin_lock_irq(&ha->lock);
402 if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
403 !test_bit(reset_type, &dev->state)) {
404 scheduled = 1;
405 ha->eh_active++;
406 list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
407 set_bit(SAS_DEV_EH_PENDING, &dev->state);
408 set_bit(reset_type, &dev->state);
409 int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
410 scsi_schedule_eh(ha->shost);
411 }
412 spin_unlock_irq(&ha->lock);
413
414 if (scheduled)
415 return SUCCESS;
416 }
417
418 pr_warn("%s reset of %s failed\n",
419 reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
420 dev_name(&dev->rphy->dev));
421
422 return FAILED;
423}
424
425int sas_eh_abort_handler(struct scsi_cmnd *cmd)
426{
427 int res = TMF_RESP_FUNC_FAILED;
428 struct sas_task *task = TO_SAS_TASK(cmd);
429 struct Scsi_Host *host = cmd->device->host;
430 struct domain_device *dev = cmd_to_domain_dev(cmd);
431 struct sas_internal *i = to_sas_internal(host->transportt);
432 unsigned long flags;
433
434 if (!i->dft->lldd_abort_task)
435 return FAILED;
436
437 spin_lock_irqsave(host->host_lock, flags);
438 /* We cannot do async aborts for SATA devices */
439 if (dev_is_sata(dev) && !host->host_eh_scheduled) {
440 spin_unlock_irqrestore(host->host_lock, flags);
441 return FAILED;
442 }
443 spin_unlock_irqrestore(host->host_lock, flags);
444
445 if (task)
446 res = i->dft->lldd_abort_task(task);
447 else
448 pr_notice("no task to abort\n");
449 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
450 return SUCCESS;
451
452 return FAILED;
453}
454EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
455
456/* Attempt to send a LUN reset message to a device */
457int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
458{
459 int res;
460 struct scsi_lun lun;
461 struct Scsi_Host *host = cmd->device->host;
462 struct domain_device *dev = cmd_to_domain_dev(cmd);
463 struct sas_internal *i = to_sas_internal(host->transportt);
464
465 if (current != host->ehandler)
466 return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun);
467
468 int_to_scsilun(cmd->device->lun, &lun);
469
470 if (!i->dft->lldd_lu_reset)
471 return FAILED;
472
473 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
474 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
475 return SUCCESS;
476
477 return FAILED;
478}
479EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
480
481int sas_eh_target_reset_handler(struct scsi_cmnd *cmd)
482{
483 int res;
484 struct Scsi_Host *host = cmd->device->host;
485 struct domain_device *dev = cmd_to_domain_dev(cmd);
486 struct sas_internal *i = to_sas_internal(host->transportt);
487
488 if (current != host->ehandler)
489 return sas_queue_reset(dev, SAS_DEV_RESET, 0);
490
491 if (!i->dft->lldd_I_T_nexus_reset)
492 return FAILED;
493
494 res = i->dft->lldd_I_T_nexus_reset(dev);
495 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
496 res == -ENODEV)
497 return SUCCESS;
498
499 return FAILED;
500}
501EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler);
502
503/* Try to reset a device */
504static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
505{
506 int res;
507 struct Scsi_Host *shost = cmd->device->host;
508
509 if (!shost->hostt->eh_device_reset_handler)
510 goto try_target_reset;
511
512 res = shost->hostt->eh_device_reset_handler(cmd);
513 if (res == SUCCESS)
514 return res;
515
516try_target_reset:
517 if (shost->hostt->eh_target_reset_handler)
518 return shost->hostt->eh_target_reset_handler(cmd);
519
520 return FAILED;
521}
522
523static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
524{
525 struct scsi_cmnd *cmd, *n;
526 enum task_disposition res = TASK_IS_DONE;
527 int tmf_resp, need_reset;
528 struct sas_internal *i = to_sas_internal(shost->transportt);
529 unsigned long flags;
530 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
531 LIST_HEAD(done);
532
533 /* clean out any commands that won the completion vs eh race */
534 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
535 struct domain_device *dev = cmd_to_domain_dev(cmd);
536 struct sas_task *task;
537
538 spin_lock_irqsave(&dev->done_lock, flags);
539 /* by this point the lldd has either observed
540 * SAS_HA_FROZEN and is leaving the task alone, or has
541 * won the race with eh and decided to complete it
542 */
543 task = TO_SAS_TASK(cmd);
544 spin_unlock_irqrestore(&dev->done_lock, flags);
545
546 if (!task)
547 list_move_tail(&cmd->eh_entry, &done);
548 }
549
550 Again:
551 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
552 struct sas_task *task = TO_SAS_TASK(cmd);
553
554 list_del_init(&cmd->eh_entry);
555
556 spin_lock_irqsave(&task->task_state_lock, flags);
557 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
558 spin_unlock_irqrestore(&task->task_state_lock, flags);
559
560 if (need_reset) {
561 pr_notice("%s: task 0x%p requests reset\n",
562 __func__, task);
563 goto reset;
564 }
565
566 pr_debug("trying to find task 0x%p\n", task);
567 res = sas_scsi_find_task(task);
568
569 switch (res) {
570 case TASK_IS_DONE:
571 pr_notice("%s: task 0x%p is done\n", __func__,
572 task);
573 sas_eh_finish_cmd(cmd);
574 continue;
575 case TASK_IS_ABORTED:
576 pr_notice("%s: task 0x%p is aborted\n",
577 __func__, task);
578 sas_eh_finish_cmd(cmd);
579 continue;
580 case TASK_IS_AT_LU:
581 pr_info("task 0x%p is at LU: lu recover\n", task);
582 reset:
583 tmf_resp = sas_recover_lu(task->dev, cmd);
584 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
585 pr_notice("dev %016llx LU 0x%llx is recovered\n",
586 SAS_ADDR(task->dev),
587 cmd->device->lun);
588 sas_eh_finish_cmd(cmd);
589 sas_scsi_clear_queue_lu(work_q, cmd);
590 goto Again;
591 }
592 fallthrough;
593 case TASK_IS_NOT_AT_LU:
594 case TASK_ABORT_FAILED:
595 pr_notice("task 0x%p is not at LU: I_T recover\n",
596 task);
597 tmf_resp = sas_recover_I_T(task->dev);
598 if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
599 tmf_resp == -ENODEV) {
600 struct domain_device *dev = task->dev;
601 pr_notice("I_T %016llx recovered\n",
602 SAS_ADDR(task->dev->sas_addr));
603 sas_eh_finish_cmd(cmd);
604 sas_scsi_clear_queue_I_T(work_q, dev);
605 goto Again;
606 }
607 /* Hammer time :-) */
608 try_to_reset_cmd_device(cmd);
609 if (i->dft->lldd_clear_nexus_port) {
610 struct asd_sas_port *port = task->dev->port;
611 pr_debug("clearing nexus for port:%d\n",
612 port->id);
613 res = i->dft->lldd_clear_nexus_port(port);
614 if (res == TMF_RESP_FUNC_COMPLETE) {
615 pr_notice("clear nexus port:%d succeeded\n",
616 port->id);
617 sas_eh_finish_cmd(cmd);
618 sas_scsi_clear_queue_port(work_q,
619 port);
620 goto Again;
621 }
622 }
623 if (i->dft->lldd_clear_nexus_ha) {
624 pr_debug("clear nexus ha\n");
625 res = i->dft->lldd_clear_nexus_ha(ha);
626 if (res == TMF_RESP_FUNC_COMPLETE) {
627 pr_notice("clear nexus ha succeeded\n");
628 sas_eh_finish_cmd(cmd);
629 goto clear_q;
630 }
631 }
632 /* If we are here -- this means that no amount
633 * of effort could recover from errors. Quite
634 * possibly the HA just disappeared.
635 */
636 pr_err("error from device %016llx, LUN 0x%llx couldn't be recovered in any way\n",
637 SAS_ADDR(task->dev->sas_addr),
638 cmd->device->lun);
639
640 sas_eh_finish_cmd(cmd);
641 goto clear_q;
642 }
643 }
644 out:
645 list_splice_tail(&done, work_q);
646 list_splice_tail_init(&ha->eh_ata_q, work_q);
647 return;
648
649 clear_q:
650 pr_debug("--- Exit %s -- clear_q\n", __func__);
651 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
652 sas_eh_finish_cmd(cmd);
653 goto out;
654}
655
656static void sas_eh_handle_resets(struct Scsi_Host *shost)
657{
658 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
659 struct sas_internal *i = to_sas_internal(shost->transportt);
660
661 /* handle directed resets to sas devices */
662 spin_lock_irq(&ha->lock);
663 while (!list_empty(&ha->eh_dev_q)) {
664 struct domain_device *dev;
665 struct ssp_device *ssp;
666
667 ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
668 list_del_init(&ssp->eh_list_node);
669 dev = container_of(ssp, typeof(*dev), ssp_dev);
670 kref_get(&dev->kref);
671 WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
672
673 spin_unlock_irq(&ha->lock);
674
675 if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
676 i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
677
678 if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
679 i->dft->lldd_I_T_nexus_reset(dev);
680
681 sas_put_device(dev);
682 spin_lock_irq(&ha->lock);
683 clear_bit(SAS_DEV_EH_PENDING, &dev->state);
684 ha->eh_active--;
685 }
686 spin_unlock_irq(&ha->lock);
687}
688
689
690void sas_scsi_recover_host(struct Scsi_Host *shost)
691{
692 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
693 LIST_HEAD(eh_work_q);
694 int tries = 0;
695 bool retry;
696
697retry:
698 tries++;
699 retry = true;
700 spin_lock_irq(shost->host_lock);
701 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
702 spin_unlock_irq(shost->host_lock);
703
704 pr_notice("Enter %s busy: %d failed: %d\n",
705 __func__, scsi_host_busy(shost), shost->host_failed);
706 /*
707 * Deal with commands that still have SAS tasks (i.e. they didn't
708 * complete via the normal sas_task completion mechanism),
709 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
710 */
711 set_bit(SAS_HA_FROZEN, &ha->state);
712 sas_eh_handle_sas_errors(shost, &eh_work_q);
713 clear_bit(SAS_HA_FROZEN, &ha->state);
714 if (list_empty(&eh_work_q))
715 goto out;
716
717 /*
718 * Now deal with SCSI commands that completed ok but have a an error
719 * code (and hopefully sense data) attached. This is roughly what
720 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
721 * command we see here has no sas_task and is thus unknown to the HA.
722 */
723 sas_ata_eh(shost, &eh_work_q);
724 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
725 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
726
727out:
728 sas_eh_handle_resets(shost);
729
730 /* now link into libata eh --- if we have any ata devices */
731 sas_ata_strategy_handler(shost);
732
733 scsi_eh_flush_done_q(&ha->eh_done_q);
734
735 /* check if any new eh work was scheduled during the last run */
736 spin_lock_irq(&ha->lock);
737 if (ha->eh_active == 0) {
738 shost->host_eh_scheduled = 0;
739 retry = false;
740 }
741 spin_unlock_irq(&ha->lock);
742
743 if (retry)
744 goto retry;
745
746 pr_notice("--- Exit %s: busy: %d failed: %d tries: %d\n",
747 __func__, scsi_host_busy(shost),
748 shost->host_failed, tries);
749}
750
751int sas_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg)
752{
753 struct domain_device *dev = sdev_to_domain_dev(sdev);
754
755 if (dev_is_sata(dev))
756 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
757
758 return -EINVAL;
759}
760EXPORT_SYMBOL_GPL(sas_ioctl);
761
762struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
763{
764 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
765 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
766 struct domain_device *found_dev = NULL;
767 int i;
768 unsigned long flags;
769
770 spin_lock_irqsave(&ha->phy_port_lock, flags);
771 for (i = 0; i < ha->num_phys; i++) {
772 struct asd_sas_port *port = ha->sas_port[i];
773 struct domain_device *dev;
774
775 spin_lock(&port->dev_list_lock);
776 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
777 if (rphy == dev->rphy) {
778 found_dev = dev;
779 spin_unlock(&port->dev_list_lock);
780 goto found;
781 }
782 }
783 spin_unlock(&port->dev_list_lock);
784 }
785 found:
786 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
787
788 return found_dev;
789}
790
791int sas_target_alloc(struct scsi_target *starget)
792{
793 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
794 struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
795
796 if (!found_dev)
797 return -ENODEV;
798
799 kref_get(&found_dev->kref);
800 starget->hostdata = found_dev;
801 return 0;
802}
803EXPORT_SYMBOL_GPL(sas_target_alloc);
804
805#define SAS_DEF_QD 256
806
807int sas_slave_configure(struct scsi_device *scsi_dev)
808{
809 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
810
811 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
812
813 if (dev_is_sata(dev)) {
814 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
815 return 0;
816 }
817
818 sas_read_port_mode_page(scsi_dev);
819
820 if (scsi_dev->tagged_supported) {
821 scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
822 } else {
823 pr_notice("device %016llx, LUN 0x%llx doesn't support TCQ\n",
824 SAS_ADDR(dev->sas_addr), scsi_dev->lun);
825 scsi_change_queue_depth(scsi_dev, 1);
826 }
827
828 scsi_dev->allow_restart = 1;
829
830 return 0;
831}
832EXPORT_SYMBOL_GPL(sas_slave_configure);
833
834int sas_change_queue_depth(struct scsi_device *sdev, int depth)
835{
836 struct domain_device *dev = sdev_to_domain_dev(sdev);
837
838 if (dev_is_sata(dev))
839 return ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
840
841 if (!sdev->tagged_supported)
842 depth = 1;
843 return scsi_change_queue_depth(sdev, depth);
844}
845EXPORT_SYMBOL_GPL(sas_change_queue_depth);
846
847int sas_bios_param(struct scsi_device *scsi_dev,
848 struct block_device *bdev,
849 sector_t capacity, int *hsc)
850{
851 hsc[0] = 255;
852 hsc[1] = 63;
853 sector_div(capacity, 255*63);
854 hsc[2] = capacity;
855
856 return 0;
857}
858EXPORT_SYMBOL_GPL(sas_bios_param);
859
860void sas_task_internal_done(struct sas_task *task)
861{
862 del_timer(&task->slow_task->timer);
863 complete(&task->slow_task->completion);
864}
865
866void sas_task_internal_timedout(struct timer_list *t)
867{
868 struct sas_task_slow *slow = from_timer(slow, t, timer);
869 struct sas_task *task = slow->task;
870 bool is_completed = true;
871 unsigned long flags;
872
873 spin_lock_irqsave(&task->task_state_lock, flags);
874 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
875 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
876 is_completed = false;
877 }
878 spin_unlock_irqrestore(&task->task_state_lock, flags);
879
880 if (!is_completed)
881 complete(&task->slow_task->completion);
882}
883
884#define TASK_TIMEOUT (20 * HZ)
885#define TASK_RETRY 3
886
887static int sas_execute_internal_abort(struct domain_device *device,
888 enum sas_internal_abort type, u16 tag,
889 unsigned int qid, void *data)
890{
891 struct sas_ha_struct *ha = device->port->ha;
892 struct sas_internal *i = to_sas_internal(ha->shost->transportt);
893 struct sas_task *task = NULL;
894 int res, retry;
895
896 for (retry = 0; retry < TASK_RETRY; retry++) {
897 task = sas_alloc_slow_task(GFP_KERNEL);
898 if (!task)
899 return -ENOMEM;
900
901 task->dev = device;
902 task->task_proto = SAS_PROTOCOL_INTERNAL_ABORT;
903 task->task_done = sas_task_internal_done;
904 task->slow_task->timer.function = sas_task_internal_timedout;
905 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT;
906 add_timer(&task->slow_task->timer);
907
908 task->abort_task.tag = tag;
909 task->abort_task.type = type;
910 task->abort_task.qid = qid;
911
912 res = i->dft->lldd_execute_task(task, GFP_KERNEL);
913 if (res) {
914 del_timer_sync(&task->slow_task->timer);
915 pr_err("Executing internal abort failed %016llx (%d)\n",
916 SAS_ADDR(device->sas_addr), res);
917 break;
918 }
919
920 wait_for_completion(&task->slow_task->completion);
921 res = TMF_RESP_FUNC_FAILED;
922
923 /* Even if the internal abort timed out, return direct. */
924 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
925 bool quit = true;
926
927 if (i->dft->lldd_abort_timeout)
928 quit = i->dft->lldd_abort_timeout(task, data);
929 else
930 pr_err("Internal abort: timeout %016llx\n",
931 SAS_ADDR(device->sas_addr));
932 res = -EIO;
933 if (quit)
934 break;
935 }
936
937 if (task->task_status.resp == SAS_TASK_COMPLETE &&
938 task->task_status.stat == SAS_SAM_STAT_GOOD) {
939 res = TMF_RESP_FUNC_COMPLETE;
940 break;
941 }
942
943 if (task->task_status.resp == SAS_TASK_COMPLETE &&
944 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
945 res = TMF_RESP_FUNC_SUCC;
946 break;
947 }
948
949 pr_err("Internal abort: task to dev %016llx response: 0x%x status 0x%x\n",
950 SAS_ADDR(device->sas_addr), task->task_status.resp,
951 task->task_status.stat);
952 sas_free_task(task);
953 task = NULL;
954 }
955 BUG_ON(retry == TASK_RETRY && task != NULL);
956 sas_free_task(task);
957 return res;
958}
959
960int sas_execute_internal_abort_single(struct domain_device *device, u16 tag,
961 unsigned int qid, void *data)
962{
963 return sas_execute_internal_abort(device, SAS_INTERNAL_ABORT_SINGLE,
964 tag, qid, data);
965}
966EXPORT_SYMBOL_GPL(sas_execute_internal_abort_single);
967
968int sas_execute_internal_abort_dev(struct domain_device *device,
969 unsigned int qid, void *data)
970{
971 return sas_execute_internal_abort(device, SAS_INTERNAL_ABORT_DEV,
972 SCSI_NO_TAG, qid, data);
973}
974EXPORT_SYMBOL_GPL(sas_execute_internal_abort_dev);
975
976int sas_execute_tmf(struct domain_device *device, void *parameter,
977 int para_len, int force_phy_id,
978 struct sas_tmf_task *tmf)
979{
980 struct sas_task *task;
981 struct sas_internal *i =
982 to_sas_internal(device->port->ha->shost->transportt);
983 int res, retry;
984
985 for (retry = 0; retry < TASK_RETRY; retry++) {
986 task = sas_alloc_slow_task(GFP_KERNEL);
987 if (!task)
988 return -ENOMEM;
989
990 task->dev = device;
991 task->task_proto = device->tproto;
992
993 if (dev_is_sata(device)) {
994 task->ata_task.device_control_reg_update = 1;
995 if (force_phy_id >= 0) {
996 task->ata_task.force_phy = true;
997 task->ata_task.force_phy_id = force_phy_id;
998 }
999 memcpy(&task->ata_task.fis, parameter, para_len);
1000 } else {
1001 memcpy(&task->ssp_task, parameter, para_len);
1002 }
1003
1004 task->task_done = sas_task_internal_done;
1005 task->tmf = tmf;
1006
1007 task->slow_task->timer.function = sas_task_internal_timedout;
1008 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT;
1009 add_timer(&task->slow_task->timer);
1010
1011 res = i->dft->lldd_execute_task(task, GFP_KERNEL);
1012 if (res) {
1013 del_timer_sync(&task->slow_task->timer);
1014 pr_err("executing TMF task failed %016llx (%d)\n",
1015 SAS_ADDR(device->sas_addr), res);
1016 break;
1017 }
1018
1019 wait_for_completion(&task->slow_task->completion);
1020
1021 if (i->dft->lldd_tmf_exec_complete)
1022 i->dft->lldd_tmf_exec_complete(device);
1023
1024 res = TMF_RESP_FUNC_FAILED;
1025
1026 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1027 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1028 pr_err("TMF task timeout for %016llx and not done\n",
1029 SAS_ADDR(device->sas_addr));
1030 if (i->dft->lldd_tmf_aborted)
1031 i->dft->lldd_tmf_aborted(task);
1032 break;
1033 }
1034 pr_warn("TMF task timeout for %016llx and done\n",
1035 SAS_ADDR(device->sas_addr));
1036 }
1037
1038 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1039 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1040 res = TMF_RESP_FUNC_COMPLETE;
1041 break;
1042 }
1043
1044 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1045 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1046 res = TMF_RESP_FUNC_SUCC;
1047 break;
1048 }
1049
1050 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1051 task->task_status.stat == SAS_DATA_UNDERRUN) {
1052 /* no error, but return the number of bytes of
1053 * underrun
1054 */
1055 pr_warn("TMF task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1056 SAS_ADDR(device->sas_addr),
1057 task->task_status.resp,
1058 task->task_status.stat);
1059 res = task->task_status.residual;
1060 break;
1061 }
1062
1063 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1064 task->task_status.stat == SAS_DATA_OVERRUN) {
1065 pr_warn("TMF task blocked task error %016llx\n",
1066 SAS_ADDR(device->sas_addr));
1067 res = -EMSGSIZE;
1068 break;
1069 }
1070
1071 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1072 task->task_status.stat == SAS_OPEN_REJECT) {
1073 pr_warn("TMF task open reject failed %016llx\n",
1074 SAS_ADDR(device->sas_addr));
1075 res = -EIO;
1076 } else {
1077 pr_warn("TMF task to dev %016llx resp: 0x%x status 0x%x\n",
1078 SAS_ADDR(device->sas_addr),
1079 task->task_status.resp,
1080 task->task_status.stat);
1081 }
1082 sas_free_task(task);
1083 task = NULL;
1084 }
1085
1086 if (retry == TASK_RETRY)
1087 pr_warn("executing TMF for %016llx failed after %d attempts!\n",
1088 SAS_ADDR(device->sas_addr), TASK_RETRY);
1089 sas_free_task(task);
1090
1091 return res;
1092}
1093
1094static int sas_execute_ssp_tmf(struct domain_device *device, u8 *lun,
1095 struct sas_tmf_task *tmf)
1096{
1097 struct sas_ssp_task ssp_task;
1098
1099 if (!(device->tproto & SAS_PROTOCOL_SSP))
1100 return TMF_RESP_FUNC_ESUPP;
1101
1102 memcpy(ssp_task.LUN, lun, 8);
1103
1104 return sas_execute_tmf(device, &ssp_task, sizeof(ssp_task), -1, tmf);
1105}
1106
1107int sas_abort_task_set(struct domain_device *dev, u8 *lun)
1108{
1109 struct sas_tmf_task tmf_task = {
1110 .tmf = TMF_ABORT_TASK_SET,
1111 };
1112
1113 return sas_execute_ssp_tmf(dev, lun, &tmf_task);
1114}
1115EXPORT_SYMBOL_GPL(sas_abort_task_set);
1116
1117int sas_clear_task_set(struct domain_device *dev, u8 *lun)
1118{
1119 struct sas_tmf_task tmf_task = {
1120 .tmf = TMF_CLEAR_TASK_SET,
1121 };
1122
1123 return sas_execute_ssp_tmf(dev, lun, &tmf_task);
1124}
1125EXPORT_SYMBOL_GPL(sas_clear_task_set);
1126
1127int sas_lu_reset(struct domain_device *dev, u8 *lun)
1128{
1129 struct sas_tmf_task tmf_task = {
1130 .tmf = TMF_LU_RESET,
1131 };
1132
1133 return sas_execute_ssp_tmf(dev, lun, &tmf_task);
1134}
1135EXPORT_SYMBOL_GPL(sas_lu_reset);
1136
1137int sas_query_task(struct sas_task *task, u16 tag)
1138{
1139 struct sas_tmf_task tmf_task = {
1140 .tmf = TMF_QUERY_TASK,
1141 .tag_of_task_to_be_managed = tag,
1142 };
1143 struct scsi_cmnd *cmnd = task->uldd_task;
1144 struct domain_device *dev = task->dev;
1145 struct scsi_lun lun;
1146
1147 int_to_scsilun(cmnd->device->lun, &lun);
1148
1149 return sas_execute_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1150}
1151EXPORT_SYMBOL_GPL(sas_query_task);
1152
1153int sas_abort_task(struct sas_task *task, u16 tag)
1154{
1155 struct sas_tmf_task tmf_task = {
1156 .tmf = TMF_ABORT_TASK,
1157 .tag_of_task_to_be_managed = tag,
1158 };
1159 struct scsi_cmnd *cmnd = task->uldd_task;
1160 struct domain_device *dev = task->dev;
1161 struct scsi_lun lun;
1162
1163 int_to_scsilun(cmnd->device->lun, &lun);
1164
1165 return sas_execute_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1166}
1167EXPORT_SYMBOL_GPL(sas_abort_task);
1168
1169/*
1170 * Tell an upper layer that it needs to initiate an abort for a given task.
1171 * This should only ever be called by an LLDD.
1172 */
1173void sas_task_abort(struct sas_task *task)
1174{
1175 struct scsi_cmnd *sc = task->uldd_task;
1176
1177 /* Escape for libsas internal commands */
1178 if (!sc) {
1179 struct sas_task_slow *slow = task->slow_task;
1180
1181 if (!slow)
1182 return;
1183 if (!del_timer(&slow->timer))
1184 return;
1185 slow->timer.function(&slow->timer);
1186 return;
1187 }
1188
1189 if (dev_is_sata(task->dev))
1190 sas_ata_task_abort(task);
1191 else
1192 blk_abort_request(scsi_cmd_to_rq(sc));
1193}
1194EXPORT_SYMBOL_GPL(sas_task_abort);
1195
1196int sas_slave_alloc(struct scsi_device *sdev)
1197{
1198 if (dev_is_sata(sdev_to_domain_dev(sdev)) && sdev->lun)
1199 return -ENXIO;
1200
1201 return 0;
1202}
1203EXPORT_SYMBOL_GPL(sas_slave_alloc);
1204
1205void sas_target_destroy(struct scsi_target *starget)
1206{
1207 struct domain_device *found_dev = starget->hostdata;
1208
1209 if (!found_dev)
1210 return;
1211
1212 starget->hostdata = NULL;
1213 sas_put_device(found_dev);
1214}
1215EXPORT_SYMBOL_GPL(sas_target_destroy);
1216
1217#define SAS_STRING_ADDR_SIZE 16
1218
1219int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1220{
1221 int res;
1222 const struct firmware *fw;
1223
1224 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1225 if (res)
1226 return res;
1227
1228 if (fw->size < SAS_STRING_ADDR_SIZE) {
1229 res = -ENODEV;
1230 goto out;
1231 }
1232
1233 res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2);
1234 if (res)
1235 goto out;
1236
1237out:
1238 release_firmware(fw);
1239 return res;
1240}
1241EXPORT_SYMBOL_GPL(sas_request_addr);
1242