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