]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/libsas/sas_scsi_host.c
[SCSI] libsas: fix timeout vs completion race
[karo-tx-linux.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_task *task = TO_SAS_TASK(cmd);
253         struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
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_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
269 {
270         struct scsi_cmnd *cmd, *n;
271
272         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
273                 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
274                     cmd->device->lun == my_cmd->device->lun)
275                         sas_eh_finish_cmd(cmd);
276         }
277 }
278
279 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
280                                      struct domain_device *dev)
281 {
282         struct scsi_cmnd *cmd, *n;
283
284         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
285                 struct domain_device *x = cmd_to_domain_dev(cmd);
286
287                 if (x == dev)
288                         sas_eh_finish_cmd(cmd);
289         }
290 }
291
292 static void sas_scsi_clear_queue_port(struct list_head *error_q,
293                                       struct asd_sas_port *port)
294 {
295         struct scsi_cmnd *cmd, *n;
296
297         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
298                 struct domain_device *dev = cmd_to_domain_dev(cmd);
299                 struct asd_sas_port *x = dev->port;
300
301                 if (x == port)
302                         sas_eh_finish_cmd(cmd);
303         }
304 }
305
306 enum task_disposition {
307         TASK_IS_DONE,
308         TASK_IS_ABORTED,
309         TASK_IS_AT_LU,
310         TASK_IS_NOT_AT_HA,
311         TASK_IS_NOT_AT_LU,
312         TASK_ABORT_FAILED,
313 };
314
315 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
316 {
317         struct sas_ha_struct *ha = task->dev->port->ha;
318         unsigned long flags;
319         int i, res;
320         struct sas_internal *si =
321                 to_sas_internal(task->dev->port->ha->core.shost->transportt);
322
323         if (ha->lldd_max_execute_num > 1) {
324                 struct scsi_core *core = &ha->core;
325                 struct sas_task *t, *n;
326
327                 mutex_lock(&core->task_queue_flush);
328                 spin_lock_irqsave(&core->task_queue_lock, flags);
329                 list_for_each_entry_safe(t, n, &core->task_queue, list)
330                         if (task == t) {
331                                 list_del_init(&t->list);
332                                 break;
333                         }
334                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
335                 mutex_unlock(&core->task_queue_flush);
336
337                 if (task == t)
338                         return TASK_IS_NOT_AT_HA;
339         }
340
341         for (i = 0; i < 5; i++) {
342                 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
343                 res = si->dft->lldd_abort_task(task);
344
345                 spin_lock_irqsave(&task->task_state_lock, flags);
346                 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
347                         spin_unlock_irqrestore(&task->task_state_lock, flags);
348                         SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
349                                     task);
350                         return TASK_IS_DONE;
351                 }
352                 spin_unlock_irqrestore(&task->task_state_lock, flags);
353
354                 if (res == TMF_RESP_FUNC_COMPLETE) {
355                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
356                                     __func__, task);
357                         return TASK_IS_ABORTED;
358                 } else if (si->dft->lldd_query_task) {
359                         SAS_DPRINTK("%s: querying task 0x%p\n",
360                                     __func__, task);
361                         res = si->dft->lldd_query_task(task);
362                         switch (res) {
363                         case TMF_RESP_FUNC_SUCC:
364                                 SAS_DPRINTK("%s: task 0x%p at LU\n",
365                                             __func__, task);
366                                 return TASK_IS_AT_LU;
367                         case TMF_RESP_FUNC_COMPLETE:
368                                 SAS_DPRINTK("%s: task 0x%p not at LU\n",
369                                             __func__, task);
370                                 return TASK_IS_NOT_AT_LU;
371                         case TMF_RESP_FUNC_FAILED:
372                                 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
373                                                 __func__, task);
374                                 return TASK_ABORT_FAILED;
375                         }
376
377                 }
378         }
379         return res;
380 }
381
382 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
383 {
384         int res = TMF_RESP_FUNC_FAILED;
385         struct scsi_lun lun;
386         struct sas_internal *i =
387                 to_sas_internal(dev->port->ha->core.shost->transportt);
388
389         int_to_scsilun(cmd->device->lun, &lun);
390
391         SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
392                     SAS_ADDR(dev->sas_addr),
393                     cmd->device->lun);
394
395         if (i->dft->lldd_abort_task_set)
396                 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
397
398         if (res == TMF_RESP_FUNC_FAILED) {
399                 if (i->dft->lldd_clear_task_set)
400                         res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
401         }
402
403         if (res == TMF_RESP_FUNC_FAILED) {
404                 if (i->dft->lldd_lu_reset)
405                         res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
406         }
407
408         return res;
409 }
410
411 static int sas_recover_I_T(struct domain_device *dev)
412 {
413         int res = TMF_RESP_FUNC_FAILED;
414         struct sas_internal *i =
415                 to_sas_internal(dev->port->ha->core.shost->transportt);
416
417         SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
418                     SAS_ADDR(dev->sas_addr));
419
420         if (i->dft->lldd_I_T_nexus_reset)
421                 res = i->dft->lldd_I_T_nexus_reset(dev);
422
423         return res;
424 }
425
426 /* Find the sas_phy that's attached to this device */
427 struct sas_phy *sas_find_local_phy(struct domain_device *dev)
428 {
429         struct domain_device *pdev = dev->parent;
430         struct ex_phy *exphy = NULL;
431         int i;
432
433         /* Directly attached device */
434         if (!pdev)
435                 return dev->port->phy;
436
437         /* Otherwise look in the expander */
438         for (i = 0; i < pdev->ex_dev.num_phys; i++)
439                 if (!memcmp(dev->sas_addr,
440                             pdev->ex_dev.ex_phy[i].attached_sas_addr,
441                             SAS_ADDR_SIZE)) {
442                         exphy = &pdev->ex_dev.ex_phy[i];
443                         break;
444                 }
445
446         BUG_ON(!exphy);
447         return exphy->phy;
448 }
449 EXPORT_SYMBOL_GPL(sas_find_local_phy);
450
451 /* Attempt to send a LUN reset message to a device */
452 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
453 {
454         struct domain_device *dev = cmd_to_domain_dev(cmd);
455         struct sas_internal *i =
456                 to_sas_internal(dev->port->ha->core.shost->transportt);
457         struct scsi_lun lun;
458         int res;
459
460         int_to_scsilun(cmd->device->lun, &lun);
461
462         if (!i->dft->lldd_lu_reset)
463                 return FAILED;
464
465         res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
466         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
467                 return SUCCESS;
468
469         return FAILED;
470 }
471
472 /* Attempt to send a phy (bus) reset */
473 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
474 {
475         struct domain_device *dev = cmd_to_domain_dev(cmd);
476         struct sas_phy *phy = sas_find_local_phy(dev);
477         int res;
478
479         res = sas_phy_reset(phy, 1);
480         if (res)
481                 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
482                             kobject_name(&phy->dev.kobj),
483                             res);
484         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
485                 return SUCCESS;
486
487         return FAILED;
488 }
489
490 /* Try to reset a device */
491 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
492 {
493         int res;
494         struct Scsi_Host *shost = cmd->device->host;
495
496         if (!shost->hostt->eh_device_reset_handler)
497                 goto try_bus_reset;
498
499         res = shost->hostt->eh_device_reset_handler(cmd);
500         if (res == SUCCESS)
501                 return res;
502
503 try_bus_reset:
504         if (shost->hostt->eh_bus_reset_handler)
505                 return shost->hostt->eh_bus_reset_handler(cmd);
506
507         return FAILED;
508 }
509
510 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
511                                     struct list_head *work_q)
512 {
513         struct scsi_cmnd *cmd, *n;
514         enum task_disposition res = TASK_IS_DONE;
515         int tmf_resp, need_reset;
516         struct sas_internal *i = to_sas_internal(shost->transportt);
517         unsigned long flags;
518         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
519
520 Again:
521         list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
522                 struct domain_device *dev = cmd_to_domain_dev(cmd);
523                 struct sas_task *task;
524
525                 spin_lock_irqsave(&dev->done_lock, flags);
526                 /* by this point the lldd has either observed
527                  * SAS_HA_FROZEN and is leaving the task alone, or has
528                  * won the race with eh and decided to complete it
529                  */
530                 task = TO_SAS_TASK(cmd);
531                 spin_unlock_irqrestore(&dev->done_lock, flags);
532
533                 if (!task)
534                         continue;
535
536                 list_del_init(&cmd->eh_entry);
537
538                 spin_lock_irqsave(&task->task_state_lock, flags);
539                 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
540                 spin_unlock_irqrestore(&task->task_state_lock, flags);
541
542                 if (need_reset) {
543                         SAS_DPRINTK("%s: task 0x%p requests reset\n",
544                                     __func__, task);
545                         goto reset;
546                 }
547
548                 SAS_DPRINTK("trying to find task 0x%p\n", task);
549                 res = sas_scsi_find_task(task);
550
551                 cmd->eh_eflags = 0;
552
553                 switch (res) {
554                 case TASK_IS_NOT_AT_HA:
555                         SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
556                                     __func__, task,
557                                     cmd->retries ? "retry" : "aborted");
558                         if (cmd->retries)
559                                 cmd->retries--;
560                         sas_eh_finish_cmd(cmd);
561                         continue;
562                 case TASK_IS_DONE:
563                         SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
564                                     task);
565                         sas_eh_finish_cmd(cmd);
566                         continue;
567                 case TASK_IS_ABORTED:
568                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
569                                     __func__, task);
570                         sas_eh_finish_cmd(cmd);
571                         continue;
572                 case TASK_IS_AT_LU:
573                         SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
574  reset:
575                         tmf_resp = sas_recover_lu(task->dev, cmd);
576                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
577                                 SAS_DPRINTK("dev %016llx LU %x is "
578                                             "recovered\n",
579                                             SAS_ADDR(task->dev),
580                                             cmd->device->lun);
581                                 sas_eh_finish_cmd(cmd);
582                                 sas_scsi_clear_queue_lu(work_q, cmd);
583                                 goto Again;
584                         }
585                         /* fallthrough */
586                 case TASK_IS_NOT_AT_LU:
587                 case TASK_ABORT_FAILED:
588                         SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
589                                     task);
590                         tmf_resp = sas_recover_I_T(task->dev);
591                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
592                                 struct domain_device *dev = task->dev;
593                                 SAS_DPRINTK("I_T %016llx recovered\n",
594                                             SAS_ADDR(task->dev->sas_addr));
595                                 sas_eh_finish_cmd(cmd);
596                                 sas_scsi_clear_queue_I_T(work_q, dev);
597                                 goto Again;
598                         }
599                         /* Hammer time :-) */
600                         try_to_reset_cmd_device(cmd);
601                         if (i->dft->lldd_clear_nexus_port) {
602                                 struct asd_sas_port *port = task->dev->port;
603                                 SAS_DPRINTK("clearing nexus for port:%d\n",
604                                             port->id);
605                                 res = i->dft->lldd_clear_nexus_port(port);
606                                 if (res == TMF_RESP_FUNC_COMPLETE) {
607                                         SAS_DPRINTK("clear nexus port:%d "
608                                                     "succeeded\n", port->id);
609                                         sas_eh_finish_cmd(cmd);
610                                         sas_scsi_clear_queue_port(work_q,
611                                                                   port);
612                                         goto Again;
613                                 }
614                         }
615                         if (i->dft->lldd_clear_nexus_ha) {
616                                 SAS_DPRINTK("clear nexus ha\n");
617                                 res = i->dft->lldd_clear_nexus_ha(ha);
618                                 if (res == TMF_RESP_FUNC_COMPLETE) {
619                                         SAS_DPRINTK("clear nexus ha "
620                                                     "succeeded\n");
621                                         sas_eh_finish_cmd(cmd);
622                                         goto clear_q;
623                                 }
624                         }
625                         /* If we are here -- this means that no amount
626                          * of effort could recover from errors.  Quite
627                          * possibly the HA just disappeared.
628                          */
629                         SAS_DPRINTK("error from  device %llx, LUN %x "
630                                     "couldn't be recovered in any way\n",
631                                     SAS_ADDR(task->dev->sas_addr),
632                                     cmd->device->lun);
633
634                         sas_eh_finish_cmd(cmd);
635                         goto clear_q;
636                 }
637         }
638         return list_empty(work_q);
639 clear_q:
640         SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
641         list_for_each_entry_safe(cmd, n, work_q, eh_entry)
642                 sas_eh_finish_cmd(cmd);
643
644         return list_empty(work_q);
645 }
646
647 void sas_scsi_recover_host(struct Scsi_Host *shost)
648 {
649         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
650         unsigned long flags;
651         LIST_HEAD(eh_work_q);
652
653         spin_lock_irqsave(shost->host_lock, flags);
654         list_splice_init(&shost->eh_cmd_q, &eh_work_q);
655         shost->host_eh_scheduled = 0;
656         spin_unlock_irqrestore(shost->host_lock, flags);
657
658         SAS_DPRINTK("Enter %s\n", __func__);
659         /*
660          * Deal with commands that still have SAS tasks (i.e. they didn't
661          * complete via the normal sas_task completion mechanism)
662          */
663         set_bit(SAS_HA_FROZEN, &ha->state);
664         if (sas_eh_handle_sas_errors(shost, &eh_work_q))
665                 goto out;
666
667         /*
668          * Now deal with SCSI commands that completed ok but have a an error
669          * code (and hopefully sense data) attached.  This is roughly what
670          * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
671          * command we see here has no sas_task and is thus unknown to the HA.
672          */
673         if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
674                 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
675                         scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
676
677 out:
678         clear_bit(SAS_HA_FROZEN, &ha->state);
679         if (ha->lldd_max_execute_num > 1)
680                 wake_up_process(ha->core.queue_thread);
681
682         /* now link into libata eh --- if we have any ata devices */
683         sas_ata_strategy_handler(shost);
684
685         scsi_eh_flush_done_q(&ha->eh_done_q);
686
687         SAS_DPRINTK("--- Exit %s\n", __func__);
688         return;
689 }
690
691 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
692 {
693         scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
694
695         return BLK_EH_NOT_HANDLED;
696 }
697
698 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
699 {
700         struct domain_device *dev = sdev_to_domain_dev(sdev);
701
702         if (dev_is_sata(dev))
703                 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
704
705         return -EINVAL;
706 }
707
708 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
709 {
710         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
711         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
712         struct domain_device *found_dev = NULL;
713         int i;
714         unsigned long flags;
715
716         spin_lock_irqsave(&ha->phy_port_lock, flags);
717         for (i = 0; i < ha->num_phys; i++) {
718                 struct asd_sas_port *port = ha->sas_port[i];
719                 struct domain_device *dev;
720
721                 spin_lock(&port->dev_list_lock);
722                 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
723                         if (rphy == dev->rphy) {
724                                 found_dev = dev;
725                                 spin_unlock(&port->dev_list_lock);
726                                 goto found;
727                         }
728                 }
729                 spin_unlock(&port->dev_list_lock);
730         }
731  found:
732         spin_unlock_irqrestore(&ha->phy_port_lock, flags);
733
734         return found_dev;
735 }
736
737 int sas_target_alloc(struct scsi_target *starget)
738 {
739         struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
740         struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
741         int res;
742
743         if (!found_dev)
744                 return -ENODEV;
745
746         if (dev_is_sata(found_dev)) {
747                 res = sas_ata_init_host_and_port(found_dev, starget);
748                 if (res)
749                         return res;
750         }
751
752         kref_get(&found_dev->kref);
753         starget->hostdata = found_dev;
754         return 0;
755 }
756
757 #define SAS_DEF_QD 256
758
759 int sas_slave_configure(struct scsi_device *scsi_dev)
760 {
761         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
762         struct sas_ha_struct *sas_ha;
763
764         BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
765
766         if (dev_is_sata(dev)) {
767                 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
768                 return 0;
769         }
770
771         sas_ha = dev->port->ha;
772
773         sas_read_port_mode_page(scsi_dev);
774
775         if (scsi_dev->tagged_supported) {
776                 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
777                 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
778         } else {
779                 SAS_DPRINTK("device %llx, LUN %x doesn't support "
780                             "TCQ\n", SAS_ADDR(dev->sas_addr),
781                             scsi_dev->lun);
782                 scsi_dev->tagged_supported = 0;
783                 scsi_set_tag_type(scsi_dev, 0);
784                 scsi_deactivate_tcq(scsi_dev, 1);
785         }
786
787         scsi_dev->allow_restart = 1;
788
789         return 0;
790 }
791
792 int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
793 {
794         struct domain_device *dev = sdev_to_domain_dev(sdev);
795
796         if (dev_is_sata(dev))
797                 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
798                                                 reason);
799
800         switch (reason) {
801         case SCSI_QDEPTH_DEFAULT:
802         case SCSI_QDEPTH_RAMP_UP:
803                 if (!sdev->tagged_supported)
804                         depth = 1;
805                 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
806                 break;
807         case SCSI_QDEPTH_QFULL:
808                 scsi_track_queue_full(sdev, depth);
809                 break;
810         default:
811                 return -EOPNOTSUPP;
812         }
813
814         return depth;
815 }
816
817 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
818 {
819         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
820
821         if (dev_is_sata(dev))
822                 return -EINVAL;
823
824         if (!scsi_dev->tagged_supported)
825                 return 0;
826
827         scsi_deactivate_tcq(scsi_dev, 1);
828
829         scsi_set_tag_type(scsi_dev, qt);
830         scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
831
832         return qt;
833 }
834
835 int sas_bios_param(struct scsi_device *scsi_dev,
836                           struct block_device *bdev,
837                           sector_t capacity, int *hsc)
838 {
839         hsc[0] = 255;
840         hsc[1] = 63;
841         sector_div(capacity, 255*63);
842         hsc[2] = capacity;
843
844         return 0;
845 }
846
847 /* ---------- Task Collector Thread implementation ---------- */
848
849 static void sas_queue(struct sas_ha_struct *sas_ha)
850 {
851         struct scsi_core *core = &sas_ha->core;
852         unsigned long flags;
853         LIST_HEAD(q);
854         int can_queue;
855         int res;
856         struct sas_internal *i = to_sas_internal(core->shost->transportt);
857
858         mutex_lock(&core->task_queue_flush);
859         spin_lock_irqsave(&core->task_queue_lock, flags);
860         while (!kthread_should_stop() &&
861                !list_empty(&core->task_queue) &&
862                !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
863
864                 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
865                 if (can_queue >= 0) {
866                         can_queue = core->task_queue_size;
867                         list_splice_init(&core->task_queue, &q);
868                 } else {
869                         struct list_head *a, *n;
870
871                         can_queue = sas_ha->lldd_queue_size;
872                         list_for_each_safe(a, n, &core->task_queue) {
873                                 list_move_tail(a, &q);
874                                 if (--can_queue == 0)
875                                         break;
876                         }
877                         can_queue = sas_ha->lldd_queue_size;
878                 }
879                 core->task_queue_size -= can_queue;
880                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
881                 {
882                         struct sas_task *task = list_entry(q.next,
883                                                            struct sas_task,
884                                                            list);
885                         list_del_init(&q);
886                         res = i->dft->lldd_execute_task(task, can_queue,
887                                                         GFP_KERNEL);
888                         if (unlikely(res))
889                                 __list_add(&q, task->list.prev, &task->list);
890                 }
891                 spin_lock_irqsave(&core->task_queue_lock, flags);
892                 if (res) {
893                         list_splice_init(&q, &core->task_queue); /*at head*/
894                         core->task_queue_size += can_queue;
895                 }
896         }
897         spin_unlock_irqrestore(&core->task_queue_lock, flags);
898         mutex_unlock(&core->task_queue_flush);
899 }
900
901 /**
902  * sas_queue_thread -- The Task Collector thread
903  * @_sas_ha: pointer to struct sas_ha
904  */
905 static int sas_queue_thread(void *_sas_ha)
906 {
907         struct sas_ha_struct *sas_ha = _sas_ha;
908
909         while (1) {
910                 set_current_state(TASK_INTERRUPTIBLE);
911                 schedule();
912                 sas_queue(sas_ha);
913                 if (kthread_should_stop())
914                         break;
915         }
916
917         return 0;
918 }
919
920 int sas_init_queue(struct sas_ha_struct *sas_ha)
921 {
922         struct scsi_core *core = &sas_ha->core;
923
924         spin_lock_init(&core->task_queue_lock);
925         mutex_init(&core->task_queue_flush);
926         core->task_queue_size = 0;
927         INIT_LIST_HEAD(&core->task_queue);
928
929         core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
930                                          "sas_queue_%d", core->shost->host_no);
931         if (IS_ERR(core->queue_thread))
932                 return PTR_ERR(core->queue_thread);
933         return 0;
934 }
935
936 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
937 {
938         unsigned long flags;
939         struct scsi_core *core = &sas_ha->core;
940         struct sas_task *task, *n;
941
942         kthread_stop(core->queue_thread);
943
944         if (!list_empty(&core->task_queue))
945                 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
946                             SAS_ADDR(sas_ha->sas_addr));
947
948         spin_lock_irqsave(&core->task_queue_lock, flags);
949         list_for_each_entry_safe(task, n, &core->task_queue, list) {
950                 struct scsi_cmnd *cmd = task->uldd_task;
951
952                 list_del_init(&task->list);
953
954                 ASSIGN_SAS_TASK(cmd, NULL);
955                 sas_free_task(task);
956                 cmd->result = DID_ABORT << 16;
957                 cmd->scsi_done(cmd);
958         }
959         spin_unlock_irqrestore(&core->task_queue_lock, flags);
960 }
961
962 /*
963  * Tell an upper layer that it needs to initiate an abort for a given task.
964  * This should only ever be called by an LLDD.
965  */
966 void sas_task_abort(struct sas_task *task)
967 {
968         struct scsi_cmnd *sc = task->uldd_task;
969
970         /* Escape for libsas internal commands */
971         if (!sc) {
972                 if (!del_timer(&task->timer))
973                         return;
974                 task->timer.function(task->timer.data);
975                 return;
976         }
977
978         if (dev_is_sata(task->dev)) {
979                 sas_ata_task_abort(task);
980         } else {
981                 struct request_queue *q = sc->device->request_queue;
982                 unsigned long flags;
983
984                 spin_lock_irqsave(q->queue_lock, flags);
985                 blk_abort_request(sc->request);
986                 spin_unlock_irqrestore(q->queue_lock, flags);
987                 scsi_schedule_eh(sc->device->host);
988         }
989 }
990
991 int sas_slave_alloc(struct scsi_device *scsi_dev)
992 {
993         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
994
995         if (dev_is_sata(dev))
996                 return ata_sas_port_init(dev->sata_dev.ap);
997
998         return 0;
999 }
1000
1001 void sas_target_destroy(struct scsi_target *starget)
1002 {
1003         struct domain_device *found_dev = starget->hostdata;
1004
1005         if (!found_dev)
1006                 return;
1007
1008         if (dev_is_sata(found_dev))
1009                 ata_sas_port_destroy(found_dev->sata_dev.ap);
1010
1011         starget->hostdata = NULL;
1012         sas_put_device(found_dev);
1013 }
1014
1015 static void sas_parse_addr(u8 *sas_addr, const char *p)
1016 {
1017         int i;
1018         for (i = 0; i < SAS_ADDR_SIZE; i++) {
1019                 u8 h, l;
1020                 if (!*p)
1021                         break;
1022                 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1023                 p++;
1024                 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1025                 p++;
1026                 sas_addr[i] = (h<<4) | l;
1027         }
1028 }
1029
1030 #define SAS_STRING_ADDR_SIZE    16
1031
1032 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1033 {
1034         int res;
1035         const struct firmware *fw;
1036
1037         res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1038         if (res)
1039                 return res;
1040
1041         if (fw->size < SAS_STRING_ADDR_SIZE) {
1042                 res = -ENODEV;
1043                 goto out;
1044         }
1045
1046         sas_parse_addr(addr, fw->data);
1047
1048 out:
1049         release_firmware(fw);
1050         return res;
1051 }
1052 EXPORT_SYMBOL_GPL(sas_request_addr);
1053
1054 EXPORT_SYMBOL_GPL(sas_queuecommand);
1055 EXPORT_SYMBOL_GPL(sas_target_alloc);
1056 EXPORT_SYMBOL_GPL(sas_slave_configure);
1057 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1058 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1059 EXPORT_SYMBOL_GPL(sas_bios_param);
1060 EXPORT_SYMBOL_GPL(sas_task_abort);
1061 EXPORT_SYMBOL_GPL(sas_phy_reset);
1062 EXPORT_SYMBOL_GPL(sas_phy_enable);
1063 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1064 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1065 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1066 EXPORT_SYMBOL_GPL(sas_target_destroy);
1067 EXPORT_SYMBOL_GPL(sas_ioctl);