GNU Linux-libre 4.9.309-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;
483         struct sas_task *task = TO_SAS_TASK(cmd);
484         struct Scsi_Host *host = cmd->device->host;
485         struct sas_internal *i = to_sas_internal(host->transportt);
486
487         if (current != host->ehandler)
488                 return FAILED;
489
490         if (!i->dft->lldd_abort_task)
491                 return FAILED;
492
493         res = i->dft->lldd_abort_task(task);
494         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
495                 return SUCCESS;
496
497         return FAILED;
498 }
499 EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
500
501 /* Attempt to send a LUN reset message to a device */
502 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
503 {
504         int res;
505         struct scsi_lun lun;
506         struct Scsi_Host *host = cmd->device->host;
507         struct domain_device *dev = cmd_to_domain_dev(cmd);
508         struct sas_internal *i = to_sas_internal(host->transportt);
509
510         if (current != host->ehandler)
511                 return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
512
513         int_to_scsilun(cmd->device->lun, &lun);
514
515         if (!i->dft->lldd_lu_reset)
516                 return FAILED;
517
518         res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
519         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
520                 return SUCCESS;
521
522         return FAILED;
523 }
524
525 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
526 {
527         int res;
528         struct Scsi_Host *host = cmd->device->host;
529         struct domain_device *dev = cmd_to_domain_dev(cmd);
530         struct sas_internal *i = to_sas_internal(host->transportt);
531
532         if (current != host->ehandler)
533                 return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
534
535         if (!i->dft->lldd_I_T_nexus_reset)
536                 return FAILED;
537
538         res = i->dft->lldd_I_T_nexus_reset(dev);
539         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
540             res == -ENODEV)
541                 return SUCCESS;
542
543         return FAILED;
544 }
545
546 /* Try to reset a device */
547 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
548 {
549         int res;
550         struct Scsi_Host *shost = cmd->device->host;
551
552         if (!shost->hostt->eh_device_reset_handler)
553                 goto try_bus_reset;
554
555         res = shost->hostt->eh_device_reset_handler(cmd);
556         if (res == SUCCESS)
557                 return res;
558
559 try_bus_reset:
560         if (shost->hostt->eh_bus_reset_handler)
561                 return shost->hostt->eh_bus_reset_handler(cmd);
562
563         return FAILED;
564 }
565
566 static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
567 {
568         struct scsi_cmnd *cmd, *n;
569         enum task_disposition res = TASK_IS_DONE;
570         int tmf_resp, need_reset;
571         struct sas_internal *i = to_sas_internal(shost->transportt);
572         unsigned long flags;
573         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
574         LIST_HEAD(done);
575
576         /* clean out any commands that won the completion vs eh race */
577         list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
578                 struct domain_device *dev = cmd_to_domain_dev(cmd);
579                 struct sas_task *task;
580
581                 spin_lock_irqsave(&dev->done_lock, flags);
582                 /* by this point the lldd has either observed
583                  * SAS_HA_FROZEN and is leaving the task alone, or has
584                  * won the race with eh and decided to complete it
585                  */
586                 task = TO_SAS_TASK(cmd);
587                 spin_unlock_irqrestore(&dev->done_lock, flags);
588
589                 if (!task)
590                         list_move_tail(&cmd->eh_entry, &done);
591         }
592
593  Again:
594         list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
595                 struct sas_task *task = TO_SAS_TASK(cmd);
596
597                 list_del_init(&cmd->eh_entry);
598
599                 spin_lock_irqsave(&task->task_state_lock, flags);
600                 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
601                 spin_unlock_irqrestore(&task->task_state_lock, flags);
602
603                 if (need_reset) {
604                         SAS_DPRINTK("%s: task 0x%p requests reset\n",
605                                     __func__, task);
606                         goto reset;
607                 }
608
609                 SAS_DPRINTK("trying to find task 0x%p\n", task);
610                 res = sas_scsi_find_task(task);
611
612                 cmd->eh_eflags = 0;
613
614                 switch (res) {
615                 case TASK_IS_DONE:
616                         SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
617                                     task);
618                         sas_eh_finish_cmd(cmd);
619                         continue;
620                 case TASK_IS_ABORTED:
621                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
622                                     __func__, task);
623                         sas_eh_finish_cmd(cmd);
624                         continue;
625                 case TASK_IS_AT_LU:
626                         SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
627  reset:
628                         tmf_resp = sas_recover_lu(task->dev, cmd);
629                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
630                                 SAS_DPRINTK("dev %016llx LU %llx is "
631                                             "recovered\n",
632                                             SAS_ADDR(task->dev),
633                                             cmd->device->lun);
634                                 sas_eh_finish_cmd(cmd);
635                                 sas_scsi_clear_queue_lu(work_q, cmd);
636                                 goto Again;
637                         }
638                         /* fallthrough */
639                 case TASK_IS_NOT_AT_LU:
640                 case TASK_ABORT_FAILED:
641                         SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
642                                     task);
643                         tmf_resp = sas_recover_I_T(task->dev);
644                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
645                             tmf_resp == -ENODEV) {
646                                 struct domain_device *dev = task->dev;
647                                 SAS_DPRINTK("I_T %016llx recovered\n",
648                                             SAS_ADDR(task->dev->sas_addr));
649                                 sas_eh_finish_cmd(cmd);
650                                 sas_scsi_clear_queue_I_T(work_q, dev);
651                                 goto Again;
652                         }
653                         /* Hammer time :-) */
654                         try_to_reset_cmd_device(cmd);
655                         if (i->dft->lldd_clear_nexus_port) {
656                                 struct asd_sas_port *port = task->dev->port;
657                                 SAS_DPRINTK("clearing nexus for port:%d\n",
658                                             port->id);
659                                 res = i->dft->lldd_clear_nexus_port(port);
660                                 if (res == TMF_RESP_FUNC_COMPLETE) {
661                                         SAS_DPRINTK("clear nexus port:%d "
662                                                     "succeeded\n", port->id);
663                                         sas_eh_finish_cmd(cmd);
664                                         sas_scsi_clear_queue_port(work_q,
665                                                                   port);
666                                         goto Again;
667                                 }
668                         }
669                         if (i->dft->lldd_clear_nexus_ha) {
670                                 SAS_DPRINTK("clear nexus ha\n");
671                                 res = i->dft->lldd_clear_nexus_ha(ha);
672                                 if (res == TMF_RESP_FUNC_COMPLETE) {
673                                         SAS_DPRINTK("clear nexus ha "
674                                                     "succeeded\n");
675                                         sas_eh_finish_cmd(cmd);
676                                         goto clear_q;
677                                 }
678                         }
679                         /* If we are here -- this means that no amount
680                          * of effort could recover from errors.  Quite
681                          * possibly the HA just disappeared.
682                          */
683                         SAS_DPRINTK("error from  device %llx, LUN %llx "
684                                     "couldn't be recovered in any way\n",
685                                     SAS_ADDR(task->dev->sas_addr),
686                                     cmd->device->lun);
687
688                         sas_eh_finish_cmd(cmd);
689                         goto clear_q;
690                 }
691         }
692  out:
693         list_splice_tail(&done, work_q);
694         list_splice_tail_init(&ha->eh_ata_q, work_q);
695         return;
696
697  clear_q:
698         SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
699         list_for_each_entry_safe(cmd, n, work_q, eh_entry)
700                 sas_eh_finish_cmd(cmd);
701         goto out;
702 }
703
704 static void sas_eh_handle_resets(struct Scsi_Host *shost)
705 {
706         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
707         struct sas_internal *i = to_sas_internal(shost->transportt);
708
709         /* handle directed resets to sas devices */
710         spin_lock_irq(&ha->lock);
711         while (!list_empty(&ha->eh_dev_q)) {
712                 struct domain_device *dev;
713                 struct ssp_device *ssp;
714
715                 ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
716                 list_del_init(&ssp->eh_list_node);
717                 dev = container_of(ssp, typeof(*dev), ssp_dev);
718                 kref_get(&dev->kref);
719                 WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
720
721                 spin_unlock_irq(&ha->lock);
722
723                 if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
724                         i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
725
726                 if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
727                         i->dft->lldd_I_T_nexus_reset(dev);
728
729                 sas_put_device(dev);
730                 spin_lock_irq(&ha->lock);
731                 clear_bit(SAS_DEV_EH_PENDING, &dev->state);
732                 ha->eh_active--;
733         }
734         spin_unlock_irq(&ha->lock);
735 }
736
737
738 void sas_scsi_recover_host(struct Scsi_Host *shost)
739 {
740         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
741         LIST_HEAD(eh_work_q);
742         int tries = 0;
743         bool retry;
744
745 retry:
746         tries++;
747         retry = true;
748         spin_lock_irq(shost->host_lock);
749         list_splice_init(&shost->eh_cmd_q, &eh_work_q);
750         spin_unlock_irq(shost->host_lock);
751
752         SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
753                     __func__, atomic_read(&shost->host_busy), shost->host_failed);
754         /*
755          * Deal with commands that still have SAS tasks (i.e. they didn't
756          * complete via the normal sas_task completion mechanism),
757          * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
758          */
759         set_bit(SAS_HA_FROZEN, &ha->state);
760         sas_eh_handle_sas_errors(shost, &eh_work_q);
761         clear_bit(SAS_HA_FROZEN, &ha->state);
762         if (list_empty(&eh_work_q))
763                 goto out;
764
765         /*
766          * Now deal with SCSI commands that completed ok but have a an error
767          * code (and hopefully sense data) attached.  This is roughly what
768          * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
769          * command we see here has no sas_task and is thus unknown to the HA.
770          */
771         sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
772         if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
773                 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
774
775 out:
776         sas_eh_handle_resets(shost);
777
778         /* now link into libata eh --- if we have any ata devices */
779         sas_ata_strategy_handler(shost);
780
781         scsi_eh_flush_done_q(&ha->eh_done_q);
782
783         /* check if any new eh work was scheduled during the last run */
784         spin_lock_irq(&ha->lock);
785         if (ha->eh_active == 0) {
786                 shost->host_eh_scheduled = 0;
787                 retry = false;
788         }
789         spin_unlock_irq(&ha->lock);
790
791         if (retry)
792                 goto retry;
793
794         SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
795                     __func__, atomic_read(&shost->host_busy),
796                     shost->host_failed, tries);
797 }
798
799 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
800 {
801         scmd_dbg(cmd, "command %p timed out\n", cmd);
802
803         return BLK_EH_NOT_HANDLED;
804 }
805
806 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
807 {
808         struct domain_device *dev = sdev_to_domain_dev(sdev);
809
810         if (dev_is_sata(dev))
811                 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
812
813         return -EINVAL;
814 }
815
816 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
817 {
818         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
819         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
820         struct domain_device *found_dev = NULL;
821         int i;
822         unsigned long flags;
823
824         spin_lock_irqsave(&ha->phy_port_lock, flags);
825         for (i = 0; i < ha->num_phys; i++) {
826                 struct asd_sas_port *port = ha->sas_port[i];
827                 struct domain_device *dev;
828
829                 spin_lock(&port->dev_list_lock);
830                 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
831                         if (rphy == dev->rphy) {
832                                 found_dev = dev;
833                                 spin_unlock(&port->dev_list_lock);
834                                 goto found;
835                         }
836                 }
837                 spin_unlock(&port->dev_list_lock);
838         }
839  found:
840         spin_unlock_irqrestore(&ha->phy_port_lock, flags);
841
842         return found_dev;
843 }
844
845 int sas_target_alloc(struct scsi_target *starget)
846 {
847         struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
848         struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
849
850         if (!found_dev)
851                 return -ENODEV;
852
853         kref_get(&found_dev->kref);
854         starget->hostdata = found_dev;
855         return 0;
856 }
857
858 #define SAS_DEF_QD 256
859
860 int sas_slave_configure(struct scsi_device *scsi_dev)
861 {
862         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
863         struct sas_ha_struct *sas_ha;
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_ha = dev->port->ha;
873
874         sas_read_port_mode_page(scsi_dev);
875
876         if (scsi_dev->tagged_supported) {
877                 scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
878         } else {
879                 SAS_DPRINTK("device %llx, LUN %llx doesn't support "
880                             "TCQ\n", SAS_ADDR(dev->sas_addr),
881                             scsi_dev->lun);
882                 scsi_change_queue_depth(scsi_dev, 1);
883         }
884
885         scsi_dev->allow_restart = 1;
886
887         return 0;
888 }
889
890 int sas_change_queue_depth(struct scsi_device *sdev, int depth)
891 {
892         struct domain_device *dev = sdev_to_domain_dev(sdev);
893
894         if (dev_is_sata(dev))
895                 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
896
897         if (!sdev->tagged_supported)
898                 depth = 1;
899         return scsi_change_queue_depth(sdev, depth);
900 }
901
902 int sas_bios_param(struct scsi_device *scsi_dev,
903                           struct block_device *bdev,
904                           sector_t capacity, int *hsc)
905 {
906         hsc[0] = 255;
907         hsc[1] = 63;
908         sector_div(capacity, 255*63);
909         hsc[2] = capacity;
910
911         return 0;
912 }
913
914 /*
915  * Tell an upper layer that it needs to initiate an abort for a given task.
916  * This should only ever be called by an LLDD.
917  */
918 void sas_task_abort(struct sas_task *task)
919 {
920         struct scsi_cmnd *sc = task->uldd_task;
921
922         /* Escape for libsas internal commands */
923         if (!sc) {
924                 struct sas_task_slow *slow = task->slow_task;
925
926                 if (!slow)
927                         return;
928                 if (!del_timer(&slow->timer))
929                         return;
930                 slow->timer.function(slow->timer.data);
931                 return;
932         }
933
934         if (dev_is_sata(task->dev)) {
935                 sas_ata_task_abort(task);
936         } else {
937                 struct request_queue *q = sc->device->request_queue;
938                 unsigned long flags;
939
940                 spin_lock_irqsave(q->queue_lock, flags);
941                 blk_abort_request(sc->request);
942                 spin_unlock_irqrestore(q->queue_lock, flags);
943         }
944 }
945
946 void sas_target_destroy(struct scsi_target *starget)
947 {
948         struct domain_device *found_dev = starget->hostdata;
949
950         if (!found_dev)
951                 return;
952
953         starget->hostdata = NULL;
954         sas_put_device(found_dev);
955 }
956
957 static void sas_parse_addr(u8 *sas_addr, const char *p)
958 {
959         int i;
960         for (i = 0; i < SAS_ADDR_SIZE; i++) {
961                 u8 h, l;
962                 if (!*p)
963                         break;
964                 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
965                 p++;
966                 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
967                 p++;
968                 sas_addr[i] = (h<<4) | l;
969         }
970 }
971
972 #define SAS_STRING_ADDR_SIZE    16
973
974 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
975 {
976         int res;
977         const struct firmware *fw;
978
979         res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
980         if (res)
981                 return res;
982
983         if (fw->size < SAS_STRING_ADDR_SIZE) {
984                 res = -ENODEV;
985                 goto out;
986         }
987
988         sas_parse_addr(addr, fw->data);
989
990 out:
991         release_firmware(fw);
992         return res;
993 }
994 EXPORT_SYMBOL_GPL(sas_request_addr);
995
996 EXPORT_SYMBOL_GPL(sas_queuecommand);
997 EXPORT_SYMBOL_GPL(sas_target_alloc);
998 EXPORT_SYMBOL_GPL(sas_slave_configure);
999 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1000 EXPORT_SYMBOL_GPL(sas_bios_param);
1001 EXPORT_SYMBOL_GPL(sas_task_abort);
1002 EXPORT_SYMBOL_GPL(sas_phy_reset);
1003 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1004 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1005 EXPORT_SYMBOL_GPL(sas_target_destroy);
1006 EXPORT_SYMBOL_GPL(sas_ioctl);