]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/scsi/libsas/sas_scsi_host.c
a4e5eb30039024408a03762194813222fd42c7e5
[mirror_ubuntu-jammy-kernel.git] / drivers / scsi / libsas / sas_scsi_host.c
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 */
37 static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
38 {
39 struct task_status_struct *ts = &task->task_status;
40 int hs = 0, stat = 0;
41
42 if (ts->resp == SAS_TASK_UNDELIVERED) {
43 /* transport error */
44 hs = DID_NO_CONNECT;
45 } else { /* ts->resp == SAS_TASK_COMPLETE */
46 /* task delivered, what happened afterwards? */
47 switch (ts->stat) {
48 case SAS_DEV_NO_RESPONSE:
49 case SAS_INTERRUPTED:
50 case SAS_PHY_DOWN:
51 case SAS_NAK_R_ERR:
52 case SAS_OPEN_TO:
53 hs = DID_NO_CONNECT;
54 break;
55 case SAS_DATA_UNDERRUN:
56 scsi_set_resid(sc, ts->residual);
57 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
58 hs = DID_ERROR;
59 break;
60 case SAS_DATA_OVERRUN:
61 hs = DID_ERROR;
62 break;
63 case SAS_QUEUE_FULL:
64 hs = DID_SOFT_ERROR; /* retry */
65 break;
66 case SAS_DEVICE_UNKNOWN:
67 hs = DID_BAD_TARGET;
68 break;
69 case SAS_SG_ERR:
70 hs = DID_PARITY;
71 break;
72 case SAS_OPEN_REJECT:
73 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
74 hs = DID_SOFT_ERROR; /* retry */
75 else
76 hs = DID_ERROR;
77 break;
78 case SAS_PROTO_RESPONSE:
79 pr_notice("LLDD:%s sent SAS_PROTO_RESP for an SSP task; please report this\n",
80 task->dev->port->ha->sas_ha_name);
81 break;
82 case SAS_ABORTED_TASK:
83 hs = DID_ABORT;
84 break;
85 case SAM_STAT_CHECK_CONDITION:
86 memcpy(sc->sense_buffer, ts->buf,
87 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
88 stat = SAM_STAT_CHECK_CONDITION;
89 break;
90 default:
91 stat = ts->stat;
92 break;
93 }
94 }
95
96 sc->result = (hs << 16) | stat;
97 ASSIGN_SAS_TASK(sc, NULL);
98 sas_free_task(task);
99 }
100
101 static void sas_scsi_task_done(struct sas_task *task)
102 {
103 struct scsi_cmnd *sc = task->uldd_task;
104 struct domain_device *dev = task->dev;
105 struct sas_ha_struct *ha = dev->port->ha;
106 unsigned long flags;
107
108 spin_lock_irqsave(&dev->done_lock, flags);
109 if (test_bit(SAS_HA_FROZEN, &ha->state))
110 task = NULL;
111 else
112 ASSIGN_SAS_TASK(sc, NULL);
113 spin_unlock_irqrestore(&dev->done_lock, flags);
114
115 if (unlikely(!task)) {
116 /* task will be completed by the error handler */
117 pr_debug("task done but aborted\n");
118 return;
119 }
120
121 if (unlikely(!sc)) {
122 pr_debug("task_done called with non existing SCSI cmnd!\n");
123 sas_free_task(task);
124 return;
125 }
126
127 sas_end_task(sc, task);
128 sc->scsi_done(sc);
129 }
130
131 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
132 struct domain_device *dev,
133 gfp_t gfp_flags)
134 {
135 struct sas_task *task = sas_alloc_task(gfp_flags);
136 struct scsi_lun lun;
137
138 if (!task)
139 return NULL;
140
141 task->uldd_task = cmd;
142 ASSIGN_SAS_TASK(cmd, task);
143
144 task->dev = dev;
145 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
146
147 task->ssp_task.retry_count = 1;
148 int_to_scsilun(cmd->device->lun, &lun);
149 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
150 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
151 task->ssp_task.cmd = cmd;
152
153 task->scatter = scsi_sglist(cmd);
154 task->num_scatter = scsi_sg_count(cmd);
155 task->total_xfer_len = scsi_bufflen(cmd);
156 task->data_dir = cmd->sc_data_direction;
157
158 task->task_done = sas_scsi_task_done;
159
160 return task;
161 }
162
163 int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
164 {
165 struct sas_internal *i = to_sas_internal(host->transportt);
166 struct domain_device *dev = cmd_to_domain_dev(cmd);
167 struct sas_task *task;
168 int res = 0;
169
170 /* If the device fell off, no sense in issuing commands */
171 if (test_bit(SAS_DEV_GONE, &dev->state)) {
172 cmd->result = DID_BAD_TARGET << 16;
173 goto out_done;
174 }
175
176 if (dev_is_sata(dev)) {
177 spin_lock_irq(dev->sata_dev.ap->lock);
178 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
179 spin_unlock_irq(dev->sata_dev.ap->lock);
180 return res;
181 }
182
183 task = sas_create_task(cmd, dev, GFP_ATOMIC);
184 if (!task)
185 return SCSI_MLQUEUE_HOST_BUSY;
186
187 res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
188 if (res)
189 goto out_free_task;
190 return 0;
191
192 out_free_task:
193 pr_debug("lldd_execute_task returned: %d\n", res);
194 ASSIGN_SAS_TASK(cmd, NULL);
195 sas_free_task(task);
196 if (res == -SAS_QUEUE_FULL)
197 cmd->result = DID_SOFT_ERROR << 16; /* retry */
198 else
199 cmd->result = DID_ERROR << 16;
200 out_done:
201 cmd->scsi_done(cmd);
202 return 0;
203 }
204
205 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
206 {
207 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
208 struct domain_device *dev = cmd_to_domain_dev(cmd);
209 struct sas_task *task = TO_SAS_TASK(cmd);
210
211 /* At this point, we only get called following an actual abort
212 * of the task, so we should be guaranteed not to be racing with
213 * any completions from the LLD. Task is freed after this.
214 */
215 sas_end_task(cmd, task);
216
217 if (dev_is_sata(dev)) {
218 /* defer commands to libata so that libata EH can
219 * handle ata qcs correctly
220 */
221 list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q);
222 return;
223 }
224
225 /* now finish the command and move it on to the error
226 * handler done list, this also takes it off the
227 * error handler pending list.
228 */
229 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
230 }
231
232 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
233 {
234 struct scsi_cmnd *cmd, *n;
235
236 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
237 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
238 cmd->device->lun == my_cmd->device->lun)
239 sas_eh_finish_cmd(cmd);
240 }
241 }
242
243 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
244 struct domain_device *dev)
245 {
246 struct scsi_cmnd *cmd, *n;
247
248 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
249 struct domain_device *x = cmd_to_domain_dev(cmd);
250
251 if (x == dev)
252 sas_eh_finish_cmd(cmd);
253 }
254 }
255
256 static void sas_scsi_clear_queue_port(struct list_head *error_q,
257 struct asd_sas_port *port)
258 {
259 struct scsi_cmnd *cmd, *n;
260
261 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
262 struct domain_device *dev = cmd_to_domain_dev(cmd);
263 struct asd_sas_port *x = dev->port;
264
265 if (x == port)
266 sas_eh_finish_cmd(cmd);
267 }
268 }
269
270 enum task_disposition {
271 TASK_IS_DONE,
272 TASK_IS_ABORTED,
273 TASK_IS_AT_LU,
274 TASK_IS_NOT_AT_LU,
275 TASK_ABORT_FAILED,
276 };
277
278 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
279 {
280 unsigned long flags;
281 int i, res;
282 struct sas_internal *si =
283 to_sas_internal(task->dev->port->ha->core.shost->transportt);
284
285 for (i = 0; i < 5; i++) {
286 pr_notice("%s: aborting task 0x%p\n", __func__, task);
287 res = si->dft->lldd_abort_task(task);
288
289 spin_lock_irqsave(&task->task_state_lock, flags);
290 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
291 spin_unlock_irqrestore(&task->task_state_lock, flags);
292 pr_debug("%s: task 0x%p is done\n", __func__, task);
293 return TASK_IS_DONE;
294 }
295 spin_unlock_irqrestore(&task->task_state_lock, flags);
296
297 if (res == TMF_RESP_FUNC_COMPLETE) {
298 pr_notice("%s: task 0x%p is aborted\n",
299 __func__, task);
300 return TASK_IS_ABORTED;
301 } else if (si->dft->lldd_query_task) {
302 pr_notice("%s: querying task 0x%p\n", __func__, task);
303 res = si->dft->lldd_query_task(task);
304 switch (res) {
305 case TMF_RESP_FUNC_SUCC:
306 pr_notice("%s: task 0x%p at LU\n", __func__,
307 task);
308 return TASK_IS_AT_LU;
309 case TMF_RESP_FUNC_COMPLETE:
310 pr_notice("%s: task 0x%p not at LU\n",
311 __func__, task);
312 return TASK_IS_NOT_AT_LU;
313 case TMF_RESP_FUNC_FAILED:
314 pr_notice("%s: task 0x%p failed to abort\n",
315 __func__, task);
316 return TASK_ABORT_FAILED;
317 }
318
319 }
320 }
321 return res;
322 }
323
324 static 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->core.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
353 static 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->core.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 */
369 struct 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 }
387 EXPORT_SYMBOL_GPL(sas_get_local_phy);
388
389 static void sas_wait_eh(struct domain_device *dev)
390 {
391 struct sas_ha_struct *ha = dev->port->ha;
392 DEFINE_WAIT(wait);
393
394 if (dev_is_sata(dev)) {
395 ata_port_wait_eh(dev->sata_dev.ap);
396 return;
397 }
398 retry:
399 spin_lock_irq(&ha->lock);
400
401 while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
402 prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
403 spin_unlock_irq(&ha->lock);
404 schedule();
405 spin_lock_irq(&ha->lock);
406 }
407 finish_wait(&ha->eh_wait_q, &wait);
408
409 spin_unlock_irq(&ha->lock);
410
411 /* make sure SCSI EH is complete */
412 if (scsi_host_in_recovery(ha->core.shost)) {
413 msleep(10);
414 goto retry;
415 }
416 }
417
418 static int sas_queue_reset(struct domain_device *dev, int reset_type,
419 u64 lun, int wait)
420 {
421 struct sas_ha_struct *ha = dev->port->ha;
422 int scheduled = 0, tries = 100;
423
424 /* ata: promote lun reset to bus reset */
425 if (dev_is_sata(dev)) {
426 sas_ata_schedule_reset(dev);
427 if (wait)
428 sas_ata_wait_eh(dev);
429 return SUCCESS;
430 }
431
432 while (!scheduled && tries--) {
433 spin_lock_irq(&ha->lock);
434 if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
435 !test_bit(reset_type, &dev->state)) {
436 scheduled = 1;
437 ha->eh_active++;
438 list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
439 set_bit(SAS_DEV_EH_PENDING, &dev->state);
440 set_bit(reset_type, &dev->state);
441 int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
442 scsi_schedule_eh(ha->core.shost);
443 }
444 spin_unlock_irq(&ha->lock);
445
446 if (wait)
447 sas_wait_eh(dev);
448
449 if (scheduled)
450 return SUCCESS;
451 }
452
453 pr_warn("%s reset of %s failed\n",
454 reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
455 dev_name(&dev->rphy->dev));
456
457 return FAILED;
458 }
459
460 int sas_eh_abort_handler(struct scsi_cmnd *cmd)
461 {
462 int res = TMF_RESP_FUNC_FAILED;
463 struct sas_task *task = TO_SAS_TASK(cmd);
464 struct Scsi_Host *host = cmd->device->host;
465 struct domain_device *dev = cmd_to_domain_dev(cmd);
466 struct sas_internal *i = to_sas_internal(host->transportt);
467 unsigned long flags;
468
469 if (current != host->ehandler)
470 return FAILED;
471
472 if (!i->dft->lldd_abort_task)
473 return FAILED;
474
475 spin_lock_irqsave(host->host_lock, flags);
476 /* We cannot do async aborts for SATA devices */
477 if (dev_is_sata(dev) && !host->host_eh_scheduled) {
478 spin_unlock_irqrestore(host->host_lock, flags);
479 return FAILED;
480 }
481 spin_unlock_irqrestore(host->host_lock, flags);
482
483 if (task)
484 res = i->dft->lldd_abort_task(task);
485 else
486 pr_notice("no task to abort\n");
487 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
488 return SUCCESS;
489
490 return FAILED;
491 }
492 EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
493
494 /* Attempt to send a LUN reset message to a device */
495 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
496 {
497 int res;
498 struct scsi_lun lun;
499 struct Scsi_Host *host = cmd->device->host;
500 struct domain_device *dev = cmd_to_domain_dev(cmd);
501 struct sas_internal *i = to_sas_internal(host->transportt);
502
503 if (current != host->ehandler)
504 return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
505
506 int_to_scsilun(cmd->device->lun, &lun);
507
508 if (!i->dft->lldd_lu_reset)
509 return FAILED;
510
511 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
512 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
513 return SUCCESS;
514
515 return FAILED;
516 }
517
518 int sas_eh_target_reset_handler(struct scsi_cmnd *cmd)
519 {
520 int res;
521 struct Scsi_Host *host = cmd->device->host;
522 struct domain_device *dev = cmd_to_domain_dev(cmd);
523 struct sas_internal *i = to_sas_internal(host->transportt);
524
525 if (current != host->ehandler)
526 return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
527
528 if (!i->dft->lldd_I_T_nexus_reset)
529 return FAILED;
530
531 res = i->dft->lldd_I_T_nexus_reset(dev);
532 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
533 res == -ENODEV)
534 return SUCCESS;
535
536 return FAILED;
537 }
538
539 /* Try to reset a device */
540 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
541 {
542 int res;
543 struct Scsi_Host *shost = cmd->device->host;
544
545 if (!shost->hostt->eh_device_reset_handler)
546 goto try_target_reset;
547
548 res = shost->hostt->eh_device_reset_handler(cmd);
549 if (res == SUCCESS)
550 return res;
551
552 try_target_reset:
553 if (shost->hostt->eh_target_reset_handler)
554 return shost->hostt->eh_target_reset_handler(cmd);
555
556 return FAILED;
557 }
558
559 static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
560 {
561 struct scsi_cmnd *cmd, *n;
562 enum task_disposition res = TASK_IS_DONE;
563 int tmf_resp, need_reset;
564 struct sas_internal *i = to_sas_internal(shost->transportt);
565 unsigned long flags;
566 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
567 LIST_HEAD(done);
568
569 /* clean out any commands that won the completion vs eh race */
570 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
571 struct domain_device *dev = cmd_to_domain_dev(cmd);
572 struct sas_task *task;
573
574 spin_lock_irqsave(&dev->done_lock, flags);
575 /* by this point the lldd has either observed
576 * SAS_HA_FROZEN and is leaving the task alone, or has
577 * won the race with eh and decided to complete it
578 */
579 task = TO_SAS_TASK(cmd);
580 spin_unlock_irqrestore(&dev->done_lock, flags);
581
582 if (!task)
583 list_move_tail(&cmd->eh_entry, &done);
584 }
585
586 Again:
587 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
588 struct sas_task *task = TO_SAS_TASK(cmd);
589
590 list_del_init(&cmd->eh_entry);
591
592 spin_lock_irqsave(&task->task_state_lock, flags);
593 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
594 spin_unlock_irqrestore(&task->task_state_lock, flags);
595
596 if (need_reset) {
597 pr_notice("%s: task 0x%p requests reset\n",
598 __func__, task);
599 goto reset;
600 }
601
602 pr_debug("trying to find task 0x%p\n", task);
603 res = sas_scsi_find_task(task);
604
605 switch (res) {
606 case TASK_IS_DONE:
607 pr_notice("%s: task 0x%p is done\n", __func__,
608 task);
609 sas_eh_finish_cmd(cmd);
610 continue;
611 case TASK_IS_ABORTED:
612 pr_notice("%s: task 0x%p is aborted\n",
613 __func__, task);
614 sas_eh_finish_cmd(cmd);
615 continue;
616 case TASK_IS_AT_LU:
617 pr_info("task 0x%p is at LU: lu recover\n", task);
618 reset:
619 tmf_resp = sas_recover_lu(task->dev, cmd);
620 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
621 pr_notice("dev %016llx LU 0x%llx is recovered\n",
622 SAS_ADDR(task->dev),
623 cmd->device->lun);
624 sas_eh_finish_cmd(cmd);
625 sas_scsi_clear_queue_lu(work_q, cmd);
626 goto Again;
627 }
628 fallthrough;
629 case TASK_IS_NOT_AT_LU:
630 case TASK_ABORT_FAILED:
631 pr_notice("task 0x%p is not at LU: I_T recover\n",
632 task);
633 tmf_resp = sas_recover_I_T(task->dev);
634 if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
635 tmf_resp == -ENODEV) {
636 struct domain_device *dev = task->dev;
637 pr_notice("I_T %016llx recovered\n",
638 SAS_ADDR(task->dev->sas_addr));
639 sas_eh_finish_cmd(cmd);
640 sas_scsi_clear_queue_I_T(work_q, dev);
641 goto Again;
642 }
643 /* Hammer time :-) */
644 try_to_reset_cmd_device(cmd);
645 if (i->dft->lldd_clear_nexus_port) {
646 struct asd_sas_port *port = task->dev->port;
647 pr_debug("clearing nexus for port:%d\n",
648 port->id);
649 res = i->dft->lldd_clear_nexus_port(port);
650 if (res == TMF_RESP_FUNC_COMPLETE) {
651 pr_notice("clear nexus port:%d succeeded\n",
652 port->id);
653 sas_eh_finish_cmd(cmd);
654 sas_scsi_clear_queue_port(work_q,
655 port);
656 goto Again;
657 }
658 }
659 if (i->dft->lldd_clear_nexus_ha) {
660 pr_debug("clear nexus ha\n");
661 res = i->dft->lldd_clear_nexus_ha(ha);
662 if (res == TMF_RESP_FUNC_COMPLETE) {
663 pr_notice("clear nexus ha succeeded\n");
664 sas_eh_finish_cmd(cmd);
665 goto clear_q;
666 }
667 }
668 /* If we are here -- this means that no amount
669 * of effort could recover from errors. Quite
670 * possibly the HA just disappeared.
671 */
672 pr_err("error from device %016llx, LUN 0x%llx couldn't be recovered in any way\n",
673 SAS_ADDR(task->dev->sas_addr),
674 cmd->device->lun);
675
676 sas_eh_finish_cmd(cmd);
677 goto clear_q;
678 }
679 }
680 out:
681 list_splice_tail(&done, work_q);
682 list_splice_tail_init(&ha->eh_ata_q, work_q);
683 return;
684
685 clear_q:
686 pr_debug("--- Exit %s -- clear_q\n", __func__);
687 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
688 sas_eh_finish_cmd(cmd);
689 goto out;
690 }
691
692 static void sas_eh_handle_resets(struct Scsi_Host *shost)
693 {
694 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
695 struct sas_internal *i = to_sas_internal(shost->transportt);
696
697 /* handle directed resets to sas devices */
698 spin_lock_irq(&ha->lock);
699 while (!list_empty(&ha->eh_dev_q)) {
700 struct domain_device *dev;
701 struct ssp_device *ssp;
702
703 ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
704 list_del_init(&ssp->eh_list_node);
705 dev = container_of(ssp, typeof(*dev), ssp_dev);
706 kref_get(&dev->kref);
707 WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
708
709 spin_unlock_irq(&ha->lock);
710
711 if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
712 i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
713
714 if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
715 i->dft->lldd_I_T_nexus_reset(dev);
716
717 sas_put_device(dev);
718 spin_lock_irq(&ha->lock);
719 clear_bit(SAS_DEV_EH_PENDING, &dev->state);
720 ha->eh_active--;
721 }
722 spin_unlock_irq(&ha->lock);
723 }
724
725
726 void sas_scsi_recover_host(struct Scsi_Host *shost)
727 {
728 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
729 LIST_HEAD(eh_work_q);
730 int tries = 0;
731 bool retry;
732
733 retry:
734 tries++;
735 retry = true;
736 spin_lock_irq(shost->host_lock);
737 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
738 spin_unlock_irq(shost->host_lock);
739
740 pr_notice("Enter %s busy: %d failed: %d\n",
741 __func__, scsi_host_busy(shost), shost->host_failed);
742 /*
743 * Deal with commands that still have SAS tasks (i.e. they didn't
744 * complete via the normal sas_task completion mechanism),
745 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
746 */
747 set_bit(SAS_HA_FROZEN, &ha->state);
748 sas_eh_handle_sas_errors(shost, &eh_work_q);
749 clear_bit(SAS_HA_FROZEN, &ha->state);
750 if (list_empty(&eh_work_q))
751 goto out;
752
753 /*
754 * Now deal with SCSI commands that completed ok but have a an error
755 * code (and hopefully sense data) attached. This is roughly what
756 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
757 * command we see here has no sas_task and is thus unknown to the HA.
758 */
759 sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
760 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
761 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
762
763 out:
764 sas_eh_handle_resets(shost);
765
766 /* now link into libata eh --- if we have any ata devices */
767 sas_ata_strategy_handler(shost);
768
769 scsi_eh_flush_done_q(&ha->eh_done_q);
770
771 /* check if any new eh work was scheduled during the last run */
772 spin_lock_irq(&ha->lock);
773 if (ha->eh_active == 0) {
774 shost->host_eh_scheduled = 0;
775 retry = false;
776 }
777 spin_unlock_irq(&ha->lock);
778
779 if (retry)
780 goto retry;
781
782 pr_notice("--- Exit %s: busy: %d failed: %d tries: %d\n",
783 __func__, scsi_host_busy(shost),
784 shost->host_failed, tries);
785 }
786
787 int sas_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg)
788 {
789 struct domain_device *dev = sdev_to_domain_dev(sdev);
790
791 if (dev_is_sata(dev))
792 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
793
794 return -EINVAL;
795 }
796
797 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
798 {
799 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
800 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
801 struct domain_device *found_dev = NULL;
802 int i;
803 unsigned long flags;
804
805 spin_lock_irqsave(&ha->phy_port_lock, flags);
806 for (i = 0; i < ha->num_phys; i++) {
807 struct asd_sas_port *port = ha->sas_port[i];
808 struct domain_device *dev;
809
810 spin_lock(&port->dev_list_lock);
811 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
812 if (rphy == dev->rphy) {
813 found_dev = dev;
814 spin_unlock(&port->dev_list_lock);
815 goto found;
816 }
817 }
818 spin_unlock(&port->dev_list_lock);
819 }
820 found:
821 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
822
823 return found_dev;
824 }
825
826 int sas_target_alloc(struct scsi_target *starget)
827 {
828 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
829 struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
830
831 if (!found_dev)
832 return -ENODEV;
833
834 kref_get(&found_dev->kref);
835 starget->hostdata = found_dev;
836 return 0;
837 }
838
839 #define SAS_DEF_QD 256
840
841 int sas_slave_configure(struct scsi_device *scsi_dev)
842 {
843 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
844
845 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
846
847 if (dev_is_sata(dev)) {
848 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
849 return 0;
850 }
851
852 sas_read_port_mode_page(scsi_dev);
853
854 if (scsi_dev->tagged_supported) {
855 scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
856 } else {
857 pr_notice("device %016llx, LUN 0x%llx doesn't support TCQ\n",
858 SAS_ADDR(dev->sas_addr), scsi_dev->lun);
859 scsi_change_queue_depth(scsi_dev, 1);
860 }
861
862 scsi_dev->allow_restart = 1;
863
864 return 0;
865 }
866
867 int sas_change_queue_depth(struct scsi_device *sdev, int depth)
868 {
869 struct domain_device *dev = sdev_to_domain_dev(sdev);
870
871 if (dev_is_sata(dev))
872 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
873
874 if (!sdev->tagged_supported)
875 depth = 1;
876 return scsi_change_queue_depth(sdev, depth);
877 }
878
879 int sas_bios_param(struct scsi_device *scsi_dev,
880 struct block_device *bdev,
881 sector_t capacity, int *hsc)
882 {
883 hsc[0] = 255;
884 hsc[1] = 63;
885 sector_div(capacity, 255*63);
886 hsc[2] = capacity;
887
888 return 0;
889 }
890
891 /*
892 * Tell an upper layer that it needs to initiate an abort for a given task.
893 * This should only ever be called by an LLDD.
894 */
895 void sas_task_abort(struct sas_task *task)
896 {
897 struct scsi_cmnd *sc = task->uldd_task;
898
899 /* Escape for libsas internal commands */
900 if (!sc) {
901 struct sas_task_slow *slow = task->slow_task;
902
903 if (!slow)
904 return;
905 if (!del_timer(&slow->timer))
906 return;
907 slow->timer.function(&slow->timer);
908 return;
909 }
910
911 if (dev_is_sata(task->dev))
912 sas_ata_task_abort(task);
913 else
914 blk_abort_request(scsi_cmd_to_rq(sc));
915 }
916
917 int sas_slave_alloc(struct scsi_device *sdev)
918 {
919 if (dev_is_sata(sdev_to_domain_dev(sdev)) && sdev->lun)
920 return -ENXIO;
921
922 return 0;
923 }
924
925 void sas_target_destroy(struct scsi_target *starget)
926 {
927 struct domain_device *found_dev = starget->hostdata;
928
929 if (!found_dev)
930 return;
931
932 starget->hostdata = NULL;
933 sas_put_device(found_dev);
934 }
935
936 #define SAS_STRING_ADDR_SIZE 16
937
938 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
939 {
940 int res;
941 const struct firmware *fw;
942
943 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
944 if (res)
945 return res;
946
947 if (fw->size < SAS_STRING_ADDR_SIZE) {
948 res = -ENODEV;
949 goto out;
950 }
951
952 res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2);
953 if (res)
954 goto out;
955
956 out:
957 release_firmware(fw);
958 return res;
959 }
960 EXPORT_SYMBOL_GPL(sas_request_addr);
961
962 EXPORT_SYMBOL_GPL(sas_queuecommand);
963 EXPORT_SYMBOL_GPL(sas_target_alloc);
964 EXPORT_SYMBOL_GPL(sas_slave_configure);
965 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
966 EXPORT_SYMBOL_GPL(sas_bios_param);
967 EXPORT_SYMBOL_GPL(sas_task_abort);
968 EXPORT_SYMBOL_GPL(sas_phy_reset);
969 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
970 EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler);
971 EXPORT_SYMBOL_GPL(sas_slave_alloc);
972 EXPORT_SYMBOL_GPL(sas_target_destroy);
973 EXPORT_SYMBOL_GPL(sas_ioctl);