2e0e779fb3b2b7bdc060ebbb882a10947b56859e
[platform/adaptation/renesas_rcar/renesas_kernel.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         list_del_init(&task->list);
116         sas_free_task(task);
117 }
118
119 static void sas_scsi_task_done(struct sas_task *task)
120 {
121         struct scsi_cmnd *sc = task->uldd_task;
122         struct domain_device *dev = task->dev;
123         struct sas_ha_struct *ha = dev->port->ha;
124         unsigned long flags;
125
126         spin_lock_irqsave(&dev->done_lock, flags);
127         if (test_bit(SAS_HA_FROZEN, &ha->state))
128                 task = NULL;
129         else
130                 ASSIGN_SAS_TASK(sc, NULL);
131         spin_unlock_irqrestore(&dev->done_lock, flags);
132
133         if (unlikely(!task)) {
134                 /* task will be completed by the error handler */
135                 SAS_DPRINTK("task done but aborted\n");
136                 return;
137         }
138
139         if (unlikely(!sc)) {
140                 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
141                 list_del_init(&task->list);
142                 sas_free_task(task);
143                 return;
144         }
145
146         sas_end_task(sc, task);
147         sc->scsi_done(sc);
148 }
149
150 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
151                                                struct domain_device *dev,
152                                                gfp_t gfp_flags)
153 {
154         struct sas_task *task = sas_alloc_task(gfp_flags);
155         struct scsi_lun lun;
156
157         if (!task)
158                 return NULL;
159
160         task->uldd_task = cmd;
161         ASSIGN_SAS_TASK(cmd, task);
162
163         task->dev = dev;
164         task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
165
166         task->ssp_task.retry_count = 1;
167         int_to_scsilun(cmd->device->lun, &lun);
168         memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
169         task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
170         memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
171
172         task->scatter = scsi_sglist(cmd);
173         task->num_scatter = scsi_sg_count(cmd);
174         task->total_xfer_len = scsi_bufflen(cmd);
175         task->data_dir = cmd->sc_data_direction;
176
177         task->task_done = sas_scsi_task_done;
178
179         return task;
180 }
181
182 int sas_queue_up(struct sas_task *task)
183 {
184         struct sas_ha_struct *sas_ha = task->dev->port->ha;
185         struct scsi_core *core = &sas_ha->core;
186         unsigned long flags;
187         LIST_HEAD(list);
188
189         spin_lock_irqsave(&core->task_queue_lock, flags);
190         if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
191                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
192                 return -SAS_QUEUE_FULL;
193         }
194         list_add_tail(&task->list, &core->task_queue);
195         core->task_queue_size += 1;
196         spin_unlock_irqrestore(&core->task_queue_lock, flags);
197         wake_up_process(core->queue_thread);
198
199         return 0;
200 }
201
202 int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
203 {
204         struct sas_internal *i = to_sas_internal(host->transportt);
205         struct domain_device *dev = cmd_to_domain_dev(cmd);
206         struct sas_ha_struct *sas_ha = dev->port->ha;
207         struct sas_task *task;
208         int res = 0;
209
210         /* If the device fell off, no sense in issuing commands */
211         if (test_bit(SAS_DEV_GONE, &dev->state)) {
212                 cmd->result = DID_BAD_TARGET << 16;
213                 goto out_done;
214         }
215
216         if (dev_is_sata(dev)) {
217                 spin_lock_irq(dev->sata_dev.ap->lock);
218                 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
219                 spin_unlock_irq(dev->sata_dev.ap->lock);
220                 return res;
221         }
222
223         task = sas_create_task(cmd, dev, GFP_ATOMIC);
224         if (!task)
225                 return SCSI_MLQUEUE_HOST_BUSY;
226
227         /* Queue up, Direct Mode or Task Collector Mode. */
228         if (sas_ha->lldd_max_execute_num < 2)
229                 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
230         else
231                 res = sas_queue_up(task);
232
233         if (res)
234                 goto out_free_task;
235         return 0;
236
237 out_free_task:
238         SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
239         ASSIGN_SAS_TASK(cmd, NULL);
240         sas_free_task(task);
241         if (res == -SAS_QUEUE_FULL)
242                 cmd->result = DID_SOFT_ERROR << 16; /* retry */
243         else
244                 cmd->result = DID_ERROR << 16;
245 out_done:
246         cmd->scsi_done(cmd);
247         return 0;
248 }
249
250 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
251 {
252         struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
253         struct sas_task *task = TO_SAS_TASK(cmd);
254
255         /* At this point, we only get called following an actual abort
256          * of the task, so we should be guaranteed not to be racing with
257          * any completions from the LLD.  Task is freed after this.
258          */
259         sas_end_task(cmd, task);
260
261         /* now finish the command and move it on to the error
262          * handler done list, this also takes it off the
263          * error handler pending list.
264          */
265         scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
266 }
267
268 static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
269 {
270         struct domain_device *dev = cmd_to_domain_dev(cmd);
271         struct sas_ha_struct *ha = dev->port->ha;
272         struct sas_task *task = TO_SAS_TASK(cmd);
273
274         if (!dev_is_sata(dev)) {
275                 sas_eh_finish_cmd(cmd);
276                 return;
277         }
278
279         /* report the timeout to libata */
280         sas_end_task(cmd, task);
281         list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
282 }
283
284 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
285 {
286         struct scsi_cmnd *cmd, *n;
287
288         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
289                 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
290                     cmd->device->lun == my_cmd->device->lun)
291                         sas_eh_defer_cmd(cmd);
292         }
293 }
294
295 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
296                                      struct domain_device *dev)
297 {
298         struct scsi_cmnd *cmd, *n;
299
300         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
301                 struct domain_device *x = cmd_to_domain_dev(cmd);
302
303                 if (x == dev)
304                         sas_eh_finish_cmd(cmd);
305         }
306 }
307
308 static void sas_scsi_clear_queue_port(struct list_head *error_q,
309                                       struct asd_sas_port *port)
310 {
311         struct scsi_cmnd *cmd, *n;
312
313         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
314                 struct domain_device *dev = cmd_to_domain_dev(cmd);
315                 struct asd_sas_port *x = dev->port;
316
317                 if (x == port)
318                         sas_eh_finish_cmd(cmd);
319         }
320 }
321
322 enum task_disposition {
323         TASK_IS_DONE,
324         TASK_IS_ABORTED,
325         TASK_IS_AT_LU,
326         TASK_IS_NOT_AT_HA,
327         TASK_IS_NOT_AT_LU,
328         TASK_ABORT_FAILED,
329 };
330
331 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
332 {
333         struct sas_ha_struct *ha = task->dev->port->ha;
334         unsigned long flags;
335         int i, res;
336         struct sas_internal *si =
337                 to_sas_internal(task->dev->port->ha->core.shost->transportt);
338
339         if (ha->lldd_max_execute_num > 1) {
340                 struct scsi_core *core = &ha->core;
341                 struct sas_task *t, *n;
342
343                 mutex_lock(&core->task_queue_flush);
344                 spin_lock_irqsave(&core->task_queue_lock, flags);
345                 list_for_each_entry_safe(t, n, &core->task_queue, list)
346                         if (task == t) {
347                                 list_del_init(&t->list);
348                                 break;
349                         }
350                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
351                 mutex_unlock(&core->task_queue_flush);
352
353                 if (task == t)
354                         return TASK_IS_NOT_AT_HA;
355         }
356
357         for (i = 0; i < 5; i++) {
358                 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
359                 res = si->dft->lldd_abort_task(task);
360
361                 spin_lock_irqsave(&task->task_state_lock, flags);
362                 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
363                         spin_unlock_irqrestore(&task->task_state_lock, flags);
364                         SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
365                                     task);
366                         return TASK_IS_DONE;
367                 }
368                 spin_unlock_irqrestore(&task->task_state_lock, flags);
369
370                 if (res == TMF_RESP_FUNC_COMPLETE) {
371                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
372                                     __func__, task);
373                         return TASK_IS_ABORTED;
374                 } else if (si->dft->lldd_query_task) {
375                         SAS_DPRINTK("%s: querying task 0x%p\n",
376                                     __func__, task);
377                         res = si->dft->lldd_query_task(task);
378                         switch (res) {
379                         case TMF_RESP_FUNC_SUCC:
380                                 SAS_DPRINTK("%s: task 0x%p at LU\n",
381                                             __func__, task);
382                                 return TASK_IS_AT_LU;
383                         case TMF_RESP_FUNC_COMPLETE:
384                                 SAS_DPRINTK("%s: task 0x%p not at LU\n",
385                                             __func__, task);
386                                 return TASK_IS_NOT_AT_LU;
387                         case TMF_RESP_FUNC_FAILED:
388                                 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
389                                                 __func__, task);
390                                 return TASK_ABORT_FAILED;
391                         }
392
393                 }
394         }
395         return res;
396 }
397
398 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
399 {
400         int res = TMF_RESP_FUNC_FAILED;
401         struct scsi_lun lun;
402         struct sas_internal *i =
403                 to_sas_internal(dev->port->ha->core.shost->transportt);
404
405         int_to_scsilun(cmd->device->lun, &lun);
406
407         SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
408                     SAS_ADDR(dev->sas_addr),
409                     cmd->device->lun);
410
411         if (i->dft->lldd_abort_task_set)
412                 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
413
414         if (res == TMF_RESP_FUNC_FAILED) {
415                 if (i->dft->lldd_clear_task_set)
416                         res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
417         }
418
419         if (res == TMF_RESP_FUNC_FAILED) {
420                 if (i->dft->lldd_lu_reset)
421                         res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
422         }
423
424         return res;
425 }
426
427 static int sas_recover_I_T(struct domain_device *dev)
428 {
429         int res = TMF_RESP_FUNC_FAILED;
430         struct sas_internal *i =
431                 to_sas_internal(dev->port->ha->core.shost->transportt);
432
433         SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
434                     SAS_ADDR(dev->sas_addr));
435
436         if (i->dft->lldd_I_T_nexus_reset)
437                 res = i->dft->lldd_I_T_nexus_reset(dev);
438
439         return res;
440 }
441
442 /* take a reference on the last known good phy for this device */
443 struct sas_phy *sas_get_local_phy(struct domain_device *dev)
444 {
445         struct sas_ha_struct *ha = dev->port->ha;
446         struct sas_phy *phy;
447         unsigned long flags;
448
449         /* a published domain device always has a valid phy, it may be
450          * stale, but it is never NULL
451          */
452         BUG_ON(!dev->phy);
453
454         spin_lock_irqsave(&ha->phy_port_lock, flags);
455         phy = dev->phy;
456         get_device(&phy->dev);
457         spin_unlock_irqrestore(&ha->phy_port_lock, flags);
458
459         return phy;
460 }
461 EXPORT_SYMBOL_GPL(sas_get_local_phy);
462
463 static void sas_wait_eh(struct domain_device *dev)
464 {
465         struct sas_ha_struct *ha = dev->port->ha;
466         DEFINE_WAIT(wait);
467
468         if (dev_is_sata(dev)) {
469                 ata_port_wait_eh(dev->sata_dev.ap);
470                 return;
471         }
472  retry:
473         spin_lock_irq(&ha->lock);
474
475         while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
476                 prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
477                 spin_unlock_irq(&ha->lock);
478                 schedule();
479                 spin_lock_irq(&ha->lock);
480         }
481         finish_wait(&ha->eh_wait_q, &wait);
482
483         spin_unlock_irq(&ha->lock);
484
485         /* make sure SCSI EH is complete */
486         if (scsi_host_in_recovery(ha->core.shost)) {
487                 msleep(10);
488                 goto retry;
489         }
490 }
491 EXPORT_SYMBOL(sas_wait_eh);
492
493 static int sas_queue_reset(struct domain_device *dev, int reset_type, int lun, int wait)
494 {
495         struct sas_ha_struct *ha = dev->port->ha;
496         int scheduled = 0, tries = 100;
497
498         /* ata: promote lun reset to bus reset */
499         if (dev_is_sata(dev)) {
500                 sas_ata_schedule_reset(dev);
501                 if (wait)
502                         sas_ata_wait_eh(dev);
503                 return SUCCESS;
504         }
505
506         while (!scheduled && tries--) {
507                 spin_lock_irq(&ha->lock);
508                 if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
509                     !test_bit(reset_type, &dev->state)) {
510                         scheduled = 1;
511                         ha->eh_active++;
512                         list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
513                         set_bit(SAS_DEV_EH_PENDING, &dev->state);
514                         set_bit(reset_type, &dev->state);
515                         int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
516                         scsi_schedule_eh(ha->core.shost);
517                 }
518                 spin_unlock_irq(&ha->lock);
519
520                 if (wait)
521                         sas_wait_eh(dev);
522
523                 if (scheduled)
524                         return SUCCESS;
525         }
526
527         SAS_DPRINTK("%s reset of %s failed\n",
528                     reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
529                     dev_name(&dev->rphy->dev));
530
531         return FAILED;
532 }
533
534 /* Attempt to send a LUN reset message to a device */
535 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
536 {
537         int res;
538         struct scsi_lun lun;
539         struct Scsi_Host *host = cmd->device->host;
540         struct domain_device *dev = cmd_to_domain_dev(cmd);
541         struct sas_internal *i = to_sas_internal(host->transportt);
542
543         if (current != host->ehandler)
544                 return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
545
546         int_to_scsilun(cmd->device->lun, &lun);
547
548         if (!i->dft->lldd_lu_reset)
549                 return FAILED;
550
551         res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
552         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
553                 return SUCCESS;
554
555         return FAILED;
556 }
557
558 /* Attempt to send a phy (bus) reset */
559 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
560 {
561         struct domain_device *dev = cmd_to_domain_dev(cmd);
562         struct sas_phy *phy = sas_get_local_phy(dev);
563         struct Scsi_Host *host = cmd->device->host;
564         int res;
565
566         if (current != host->ehandler)
567                 return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
568
569         res = sas_phy_reset(phy, 1);
570         if (res)
571                 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
572                             kobject_name(&phy->dev.kobj),
573                             res);
574         sas_put_local_phy(phy);
575
576         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
577                 return SUCCESS;
578
579         return FAILED;
580 }
581
582 /* Try to reset a device */
583 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
584 {
585         int res;
586         struct Scsi_Host *shost = cmd->device->host;
587
588         if (!shost->hostt->eh_device_reset_handler)
589                 goto try_bus_reset;
590
591         res = shost->hostt->eh_device_reset_handler(cmd);
592         if (res == SUCCESS)
593                 return res;
594
595 try_bus_reset:
596         if (shost->hostt->eh_bus_reset_handler)
597                 return shost->hostt->eh_bus_reset_handler(cmd);
598
599         return FAILED;
600 }
601
602 static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
603 {
604         struct scsi_cmnd *cmd, *n;
605         enum task_disposition res = TASK_IS_DONE;
606         int tmf_resp, need_reset;
607         struct sas_internal *i = to_sas_internal(shost->transportt);
608         unsigned long flags;
609         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
610         LIST_HEAD(done);
611
612         /* clean out any commands that won the completion vs eh race */
613         list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
614                 struct domain_device *dev = cmd_to_domain_dev(cmd);
615                 struct sas_task *task;
616
617                 spin_lock_irqsave(&dev->done_lock, flags);
618                 /* by this point the lldd has either observed
619                  * SAS_HA_FROZEN and is leaving the task alone, or has
620                  * won the race with eh and decided to complete it
621                  */
622                 task = TO_SAS_TASK(cmd);
623                 spin_unlock_irqrestore(&dev->done_lock, flags);
624
625                 if (!task)
626                         list_move_tail(&cmd->eh_entry, &done);
627         }
628
629  Again:
630         list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
631                 struct sas_task *task = TO_SAS_TASK(cmd);
632
633                 list_del_init(&cmd->eh_entry);
634
635                 spin_lock_irqsave(&task->task_state_lock, flags);
636                 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
637                 spin_unlock_irqrestore(&task->task_state_lock, flags);
638
639                 if (need_reset) {
640                         SAS_DPRINTK("%s: task 0x%p requests reset\n",
641                                     __func__, task);
642                         goto reset;
643                 }
644
645                 SAS_DPRINTK("trying to find task 0x%p\n", task);
646                 res = sas_scsi_find_task(task);
647
648                 cmd->eh_eflags = 0;
649
650                 switch (res) {
651                 case TASK_IS_NOT_AT_HA:
652                         SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
653                                     __func__, task,
654                                     cmd->retries ? "retry" : "aborted");
655                         if (cmd->retries)
656                                 cmd->retries--;
657                         sas_eh_finish_cmd(cmd);
658                         continue;
659                 case TASK_IS_DONE:
660                         SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
661                                     task);
662                         sas_eh_defer_cmd(cmd);
663                         continue;
664                 case TASK_IS_ABORTED:
665                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
666                                     __func__, task);
667                         sas_eh_defer_cmd(cmd);
668                         continue;
669                 case TASK_IS_AT_LU:
670                         SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
671  reset:
672                         tmf_resp = sas_recover_lu(task->dev, cmd);
673                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
674                                 SAS_DPRINTK("dev %016llx LU %x is "
675                                             "recovered\n",
676                                             SAS_ADDR(task->dev),
677                                             cmd->device->lun);
678                                 sas_eh_defer_cmd(cmd);
679                                 sas_scsi_clear_queue_lu(work_q, cmd);
680                                 goto Again;
681                         }
682                         /* fallthrough */
683                 case TASK_IS_NOT_AT_LU:
684                 case TASK_ABORT_FAILED:
685                         SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
686                                     task);
687                         tmf_resp = sas_recover_I_T(task->dev);
688                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
689                             tmf_resp == -ENODEV) {
690                                 struct domain_device *dev = task->dev;
691                                 SAS_DPRINTK("I_T %016llx recovered\n",
692                                             SAS_ADDR(task->dev->sas_addr));
693                                 sas_eh_finish_cmd(cmd);
694                                 sas_scsi_clear_queue_I_T(work_q, dev);
695                                 goto Again;
696                         }
697                         /* Hammer time :-) */
698                         try_to_reset_cmd_device(cmd);
699                         if (i->dft->lldd_clear_nexus_port) {
700                                 struct asd_sas_port *port = task->dev->port;
701                                 SAS_DPRINTK("clearing nexus for port:%d\n",
702                                             port->id);
703                                 res = i->dft->lldd_clear_nexus_port(port);
704                                 if (res == TMF_RESP_FUNC_COMPLETE) {
705                                         SAS_DPRINTK("clear nexus port:%d "
706                                                     "succeeded\n", port->id);
707                                         sas_eh_finish_cmd(cmd);
708                                         sas_scsi_clear_queue_port(work_q,
709                                                                   port);
710                                         goto Again;
711                                 }
712                         }
713                         if (i->dft->lldd_clear_nexus_ha) {
714                                 SAS_DPRINTK("clear nexus ha\n");
715                                 res = i->dft->lldd_clear_nexus_ha(ha);
716                                 if (res == TMF_RESP_FUNC_COMPLETE) {
717                                         SAS_DPRINTK("clear nexus ha "
718                                                     "succeeded\n");
719                                         sas_eh_finish_cmd(cmd);
720                                         goto clear_q;
721                                 }
722                         }
723                         /* If we are here -- this means that no amount
724                          * of effort could recover from errors.  Quite
725                          * possibly the HA just disappeared.
726                          */
727                         SAS_DPRINTK("error from  device %llx, LUN %x "
728                                     "couldn't be recovered in any way\n",
729                                     SAS_ADDR(task->dev->sas_addr),
730                                     cmd->device->lun);
731
732                         sas_eh_finish_cmd(cmd);
733                         goto clear_q;
734                 }
735         }
736  out:
737         list_splice_tail(&done, work_q);
738         list_splice_tail_init(&ha->eh_ata_q, work_q);
739         return;
740
741  clear_q:
742         SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
743         list_for_each_entry_safe(cmd, n, work_q, eh_entry)
744                 sas_eh_finish_cmd(cmd);
745         goto out;
746 }
747
748 static void sas_eh_handle_resets(struct Scsi_Host *shost)
749 {
750         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
751         struct sas_internal *i = to_sas_internal(shost->transportt);
752
753         /* handle directed resets to sas devices */
754         spin_lock_irq(&ha->lock);
755         while (!list_empty(&ha->eh_dev_q)) {
756                 struct domain_device *dev;
757                 struct ssp_device *ssp;
758
759                 ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
760                 list_del_init(&ssp->eh_list_node);
761                 dev = container_of(ssp, typeof(*dev), ssp_dev);
762                 kref_get(&dev->kref);
763                 WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
764
765                 spin_unlock_irq(&ha->lock);
766
767                 if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
768                         i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
769
770                 if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
771                         i->dft->lldd_I_T_nexus_reset(dev);
772
773                 sas_put_device(dev);
774                 spin_lock_irq(&ha->lock);
775                 clear_bit(SAS_DEV_EH_PENDING, &dev->state);
776                 ha->eh_active--;
777         }
778         spin_unlock_irq(&ha->lock);
779 }
780
781
782 void sas_scsi_recover_host(struct Scsi_Host *shost)
783 {
784         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
785         LIST_HEAD(eh_work_q);
786         int tries = 0;
787         bool retry;
788
789 retry:
790         tries++;
791         retry = true;
792         spin_lock_irq(shost->host_lock);
793         list_splice_init(&shost->eh_cmd_q, &eh_work_q);
794         spin_unlock_irq(shost->host_lock);
795
796         SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
797                     __func__, shost->host_busy, shost->host_failed);
798         /*
799          * Deal with commands that still have SAS tasks (i.e. they didn't
800          * complete via the normal sas_task completion mechanism),
801          * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
802          */
803         set_bit(SAS_HA_FROZEN, &ha->state);
804         sas_eh_handle_sas_errors(shost, &eh_work_q);
805         clear_bit(SAS_HA_FROZEN, &ha->state);
806         if (list_empty(&eh_work_q))
807                 goto out;
808
809         /*
810          * Now deal with SCSI commands that completed ok but have a an error
811          * code (and hopefully sense data) attached.  This is roughly what
812          * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
813          * command we see here has no sas_task and is thus unknown to the HA.
814          */
815         sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
816         if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
817                 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
818
819 out:
820         if (ha->lldd_max_execute_num > 1)
821                 wake_up_process(ha->core.queue_thread);
822
823         sas_eh_handle_resets(shost);
824
825         /* now link into libata eh --- if we have any ata devices */
826         sas_ata_strategy_handler(shost);
827
828         scsi_eh_flush_done_q(&ha->eh_done_q);
829
830         /* check if any new eh work was scheduled during the last run */
831         spin_lock_irq(&ha->lock);
832         if (ha->eh_active == 0) {
833                 shost->host_eh_scheduled = 0;
834                 retry = false;
835         }
836         spin_unlock_irq(&ha->lock);
837
838         if (retry)
839                 goto retry;
840
841         SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
842                     __func__, shost->host_busy, shost->host_failed, tries);
843 }
844
845 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
846 {
847         scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
848
849         return BLK_EH_NOT_HANDLED;
850 }
851
852 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
853 {
854         struct domain_device *dev = sdev_to_domain_dev(sdev);
855
856         if (dev_is_sata(dev))
857                 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
858
859         return -EINVAL;
860 }
861
862 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
863 {
864         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
865         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
866         struct domain_device *found_dev = NULL;
867         int i;
868         unsigned long flags;
869
870         spin_lock_irqsave(&ha->phy_port_lock, flags);
871         for (i = 0; i < ha->num_phys; i++) {
872                 struct asd_sas_port *port = ha->sas_port[i];
873                 struct domain_device *dev;
874
875                 spin_lock(&port->dev_list_lock);
876                 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
877                         if (rphy == dev->rphy) {
878                                 found_dev = dev;
879                                 spin_unlock(&port->dev_list_lock);
880                                 goto found;
881                         }
882                 }
883                 spin_unlock(&port->dev_list_lock);
884         }
885  found:
886         spin_unlock_irqrestore(&ha->phy_port_lock, flags);
887
888         return found_dev;
889 }
890
891 int sas_target_alloc(struct scsi_target *starget)
892 {
893         struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
894         struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
895
896         if (!found_dev)
897                 return -ENODEV;
898
899         kref_get(&found_dev->kref);
900         starget->hostdata = found_dev;
901         return 0;
902 }
903
904 #define SAS_DEF_QD 256
905
906 int sas_slave_configure(struct scsi_device *scsi_dev)
907 {
908         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
909         struct sas_ha_struct *sas_ha;
910
911         BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
912
913         if (dev_is_sata(dev)) {
914                 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
915                 return 0;
916         }
917
918         sas_ha = dev->port->ha;
919
920         sas_read_port_mode_page(scsi_dev);
921
922         if (scsi_dev->tagged_supported) {
923                 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
924                 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
925         } else {
926                 SAS_DPRINTK("device %llx, LUN %x doesn't support "
927                             "TCQ\n", SAS_ADDR(dev->sas_addr),
928                             scsi_dev->lun);
929                 scsi_dev->tagged_supported = 0;
930                 scsi_set_tag_type(scsi_dev, 0);
931                 scsi_deactivate_tcq(scsi_dev, 1);
932         }
933
934         scsi_dev->allow_restart = 1;
935
936         return 0;
937 }
938
939 int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
940 {
941         struct domain_device *dev = sdev_to_domain_dev(sdev);
942
943         if (dev_is_sata(dev))
944                 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
945                                                 reason);
946
947         switch (reason) {
948         case SCSI_QDEPTH_DEFAULT:
949         case SCSI_QDEPTH_RAMP_UP:
950                 if (!sdev->tagged_supported)
951                         depth = 1;
952                 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
953                 break;
954         case SCSI_QDEPTH_QFULL:
955                 scsi_track_queue_full(sdev, depth);
956                 break;
957         default:
958                 return -EOPNOTSUPP;
959         }
960
961         return depth;
962 }
963
964 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
965 {
966         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
967
968         if (dev_is_sata(dev))
969                 return -EINVAL;
970
971         if (!scsi_dev->tagged_supported)
972                 return 0;
973
974         scsi_deactivate_tcq(scsi_dev, 1);
975
976         scsi_set_tag_type(scsi_dev, qt);
977         scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
978
979         return qt;
980 }
981
982 int sas_bios_param(struct scsi_device *scsi_dev,
983                           struct block_device *bdev,
984                           sector_t capacity, int *hsc)
985 {
986         hsc[0] = 255;
987         hsc[1] = 63;
988         sector_div(capacity, 255*63);
989         hsc[2] = capacity;
990
991         return 0;
992 }
993
994 /* ---------- Task Collector Thread implementation ---------- */
995
996 static void sas_queue(struct sas_ha_struct *sas_ha)
997 {
998         struct scsi_core *core = &sas_ha->core;
999         unsigned long flags;
1000         LIST_HEAD(q);
1001         int can_queue;
1002         int res;
1003         struct sas_internal *i = to_sas_internal(core->shost->transportt);
1004
1005         mutex_lock(&core->task_queue_flush);
1006         spin_lock_irqsave(&core->task_queue_lock, flags);
1007         while (!kthread_should_stop() &&
1008                !list_empty(&core->task_queue) &&
1009                !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
1010
1011                 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
1012                 if (can_queue >= 0) {
1013                         can_queue = core->task_queue_size;
1014                         list_splice_init(&core->task_queue, &q);
1015                 } else {
1016                         struct list_head *a, *n;
1017
1018                         can_queue = sas_ha->lldd_queue_size;
1019                         list_for_each_safe(a, n, &core->task_queue) {
1020                                 list_move_tail(a, &q);
1021                                 if (--can_queue == 0)
1022                                         break;
1023                         }
1024                         can_queue = sas_ha->lldd_queue_size;
1025                 }
1026                 core->task_queue_size -= can_queue;
1027                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
1028                 {
1029                         struct sas_task *task = list_entry(q.next,
1030                                                            struct sas_task,
1031                                                            list);
1032                         list_del_init(&q);
1033                         res = i->dft->lldd_execute_task(task, can_queue,
1034                                                         GFP_KERNEL);
1035                         if (unlikely(res))
1036                                 __list_add(&q, task->list.prev, &task->list);
1037                 }
1038                 spin_lock_irqsave(&core->task_queue_lock, flags);
1039                 if (res) {
1040                         list_splice_init(&q, &core->task_queue); /*at head*/
1041                         core->task_queue_size += can_queue;
1042                 }
1043         }
1044         spin_unlock_irqrestore(&core->task_queue_lock, flags);
1045         mutex_unlock(&core->task_queue_flush);
1046 }
1047
1048 /**
1049  * sas_queue_thread -- The Task Collector thread
1050  * @_sas_ha: pointer to struct sas_ha
1051  */
1052 static int sas_queue_thread(void *_sas_ha)
1053 {
1054         struct sas_ha_struct *sas_ha = _sas_ha;
1055
1056         while (1) {
1057                 set_current_state(TASK_INTERRUPTIBLE);
1058                 schedule();
1059                 sas_queue(sas_ha);
1060                 if (kthread_should_stop())
1061                         break;
1062         }
1063
1064         return 0;
1065 }
1066
1067 int sas_init_queue(struct sas_ha_struct *sas_ha)
1068 {
1069         struct scsi_core *core = &sas_ha->core;
1070
1071         spin_lock_init(&core->task_queue_lock);
1072         mutex_init(&core->task_queue_flush);
1073         core->task_queue_size = 0;
1074         INIT_LIST_HEAD(&core->task_queue);
1075
1076         core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
1077                                          "sas_queue_%d", core->shost->host_no);
1078         if (IS_ERR(core->queue_thread))
1079                 return PTR_ERR(core->queue_thread);
1080         return 0;
1081 }
1082
1083 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
1084 {
1085         unsigned long flags;
1086         struct scsi_core *core = &sas_ha->core;
1087         struct sas_task *task, *n;
1088
1089         kthread_stop(core->queue_thread);
1090
1091         if (!list_empty(&core->task_queue))
1092                 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
1093                             SAS_ADDR(sas_ha->sas_addr));
1094
1095         spin_lock_irqsave(&core->task_queue_lock, flags);
1096         list_for_each_entry_safe(task, n, &core->task_queue, list) {
1097                 struct scsi_cmnd *cmd = task->uldd_task;
1098
1099                 list_del_init(&task->list);
1100
1101                 ASSIGN_SAS_TASK(cmd, NULL);
1102                 sas_free_task(task);
1103                 cmd->result = DID_ABORT << 16;
1104                 cmd->scsi_done(cmd);
1105         }
1106         spin_unlock_irqrestore(&core->task_queue_lock, flags);
1107 }
1108
1109 /*
1110  * Tell an upper layer that it needs to initiate an abort for a given task.
1111  * This should only ever be called by an LLDD.
1112  */
1113 void sas_task_abort(struct sas_task *task)
1114 {
1115         struct scsi_cmnd *sc = task->uldd_task;
1116
1117         /* Escape for libsas internal commands */
1118         if (!sc) {
1119                 if (!del_timer(&task->timer))
1120                         return;
1121                 task->timer.function(task->timer.data);
1122                 return;
1123         }
1124
1125         if (dev_is_sata(task->dev)) {
1126                 sas_ata_task_abort(task);
1127         } else {
1128                 struct request_queue *q = sc->device->request_queue;
1129                 unsigned long flags;
1130
1131                 spin_lock_irqsave(q->queue_lock, flags);
1132                 blk_abort_request(sc->request);
1133                 spin_unlock_irqrestore(q->queue_lock, flags);
1134         }
1135 }
1136
1137 void sas_target_destroy(struct scsi_target *starget)
1138 {
1139         struct domain_device *found_dev = starget->hostdata;
1140
1141         if (!found_dev)
1142                 return;
1143
1144         starget->hostdata = NULL;
1145         sas_put_device(found_dev);
1146 }
1147
1148 static void sas_parse_addr(u8 *sas_addr, const char *p)
1149 {
1150         int i;
1151         for (i = 0; i < SAS_ADDR_SIZE; i++) {
1152                 u8 h, l;
1153                 if (!*p)
1154                         break;
1155                 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1156                 p++;
1157                 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1158                 p++;
1159                 sas_addr[i] = (h<<4) | l;
1160         }
1161 }
1162
1163 #define SAS_STRING_ADDR_SIZE    16
1164
1165 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1166 {
1167         int res;
1168         const struct firmware *fw;
1169
1170         res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1171         if (res)
1172                 return res;
1173
1174         if (fw->size < SAS_STRING_ADDR_SIZE) {
1175                 res = -ENODEV;
1176                 goto out;
1177         }
1178
1179         sas_parse_addr(addr, fw->data);
1180
1181 out:
1182         release_firmware(fw);
1183         return res;
1184 }
1185 EXPORT_SYMBOL_GPL(sas_request_addr);
1186
1187 EXPORT_SYMBOL_GPL(sas_queuecommand);
1188 EXPORT_SYMBOL_GPL(sas_target_alloc);
1189 EXPORT_SYMBOL_GPL(sas_slave_configure);
1190 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1191 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1192 EXPORT_SYMBOL_GPL(sas_bios_param);
1193 EXPORT_SYMBOL_GPL(sas_task_abort);
1194 EXPORT_SYMBOL_GPL(sas_phy_reset);
1195 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1196 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1197 EXPORT_SYMBOL_GPL(sas_target_destroy);
1198 EXPORT_SYMBOL_GPL(sas_ioctl);