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