Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6] / 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
28 #include "sas_internal.h"
29
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_sas.h>
37 #include <scsi/sas_ata.h>
38 #include "../scsi_sas_internal.h"
39 #include "../scsi_transport_api.h"
40 #include "../scsi_priv.h"
41
42 #include <linux/err.h>
43 #include <linux/blkdev.h>
44 #include <linux/freezer.h>
45 #include <linux/scatterlist.h>
46 #include <linux/libata.h>
47
48 /* ---------- SCSI Host glue ---------- */
49
50 static void sas_scsi_task_done(struct sas_task *task)
51 {
52         struct task_status_struct *ts = &task->task_status;
53         struct scsi_cmnd *sc = task->uldd_task;
54         struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
55         unsigned ts_flags = task->task_state_flags;
56         int hs = 0, stat = 0;
57
58         if (unlikely(!sc)) {
59                 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
60                 list_del_init(&task->list);
61                 sas_free_task(task);
62                 return;
63         }
64
65         if (ts->resp == SAS_TASK_UNDELIVERED) {
66                 /* transport error */
67                 hs = DID_NO_CONNECT;
68         } else { /* ts->resp == SAS_TASK_COMPLETE */
69                 /* task delivered, what happened afterwards? */
70                 switch (ts->stat) {
71                 case SAS_DEV_NO_RESPONSE:
72                 case SAS_INTERRUPTED:
73                 case SAS_PHY_DOWN:
74                 case SAS_NAK_R_ERR:
75                 case SAS_OPEN_TO:
76                         hs = DID_NO_CONNECT;
77                         break;
78                 case SAS_DATA_UNDERRUN:
79                         scsi_set_resid(sc, ts->residual);
80                         if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
81                                 hs = DID_ERROR;
82                         break;
83                 case SAS_DATA_OVERRUN:
84                         hs = DID_ERROR;
85                         break;
86                 case SAS_QUEUE_FULL:
87                         hs = DID_SOFT_ERROR; /* retry */
88                         break;
89                 case SAS_DEVICE_UNKNOWN:
90                         hs = DID_BAD_TARGET;
91                         break;
92                 case SAS_SG_ERR:
93                         hs = DID_PARITY;
94                         break;
95                 case SAS_OPEN_REJECT:
96                         if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
97                                 hs = DID_SOFT_ERROR; /* retry */
98                         else
99                                 hs = DID_ERROR;
100                         break;
101                 case SAS_PROTO_RESPONSE:
102                         SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
103                                     "task; please report this\n",
104                                     task->dev->port->ha->sas_ha_name);
105                         break;
106                 case SAS_ABORTED_TASK:
107                         hs = DID_ABORT;
108                         break;
109                 case SAM_CHECK_COND:
110                         memcpy(sc->sense_buffer, ts->buf,
111                                min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
112                         stat = SAM_CHECK_COND;
113                         break;
114                 default:
115                         stat = ts->stat;
116                         break;
117                 }
118         }
119         ASSIGN_SAS_TASK(sc, NULL);
120         sc->result = (hs << 16) | stat;
121         list_del_init(&task->list);
122         sas_free_task(task);
123         /* This is very ugly but this is how SCSI Core works. */
124         if (ts_flags & SAS_TASK_STATE_ABORTED)
125                 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
126         else
127                 sc->scsi_done(sc);
128 }
129
130 static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
131 {
132         enum task_attribute ta = TASK_ATTR_SIMPLE;
133         if (cmd->request && blk_rq_tagged(cmd->request)) {
134                 if (cmd->device->ordered_tags &&
135                     (cmd->request->cmd_flags & REQ_HARDBARRIER))
136                         ta = TASK_ATTR_ORDERED;
137         }
138         return ta;
139 }
140
141 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
142                                                struct domain_device *dev,
143                                                gfp_t gfp_flags)
144 {
145         struct sas_task *task = sas_alloc_task(gfp_flags);
146         struct scsi_lun lun;
147
148         if (!task)
149                 return NULL;
150
151         task->uldd_task = cmd;
152         ASSIGN_SAS_TASK(cmd, task);
153
154         task->dev = dev;
155         task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
156
157         task->ssp_task.retry_count = 1;
158         int_to_scsilun(cmd->device->lun, &lun);
159         memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
160         task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
161         memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
162
163         task->scatter = scsi_sglist(cmd);
164         task->num_scatter = scsi_sg_count(cmd);
165         task->total_xfer_len = scsi_bufflen(cmd);
166         task->data_dir = cmd->sc_data_direction;
167
168         task->task_done = sas_scsi_task_done;
169
170         return task;
171 }
172
173 int sas_queue_up(struct sas_task *task)
174 {
175         struct sas_ha_struct *sas_ha = task->dev->port->ha;
176         struct scsi_core *core = &sas_ha->core;
177         unsigned long flags;
178         LIST_HEAD(list);
179
180         spin_lock_irqsave(&core->task_queue_lock, flags);
181         if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
182                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
183                 return -SAS_QUEUE_FULL;
184         }
185         list_add_tail(&task->list, &core->task_queue);
186         core->task_queue_size += 1;
187         spin_unlock_irqrestore(&core->task_queue_lock, flags);
188         wake_up_process(core->queue_thread);
189
190         return 0;
191 }
192
193 /**
194  * sas_queuecommand -- Enqueue a command for processing
195  * @parameters: See SCSI Core documentation
196  *
197  * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
198  * call us without holding an IRQ spinlock...
199  */
200 int sas_queuecommand(struct scsi_cmnd *cmd,
201                      void (*scsi_done)(struct scsi_cmnd *))
202         __releases(host->host_lock)
203         __acquires(dev->sata_dev.ap->lock)
204         __releases(dev->sata_dev.ap->lock)
205         __acquires(host->host_lock)
206 {
207         int res = 0;
208         struct domain_device *dev = cmd_to_domain_dev(cmd);
209         struct Scsi_Host *host = cmd->device->host;
210         struct sas_internal *i = to_sas_internal(host->transportt);
211
212         spin_unlock_irq(host->host_lock);
213
214         {
215                 struct sas_ha_struct *sas_ha = dev->port->ha;
216                 struct sas_task *task;
217
218                 if (dev_is_sata(dev)) {
219                         unsigned long flags;
220
221                         spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
222                         res = ata_sas_queuecmd(cmd, scsi_done,
223                                                dev->sata_dev.ap);
224                         spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
225                         goto out;
226                 }
227
228                 res = -ENOMEM;
229                 task = sas_create_task(cmd, dev, GFP_ATOMIC);
230                 if (!task)
231                         goto out;
232
233                 cmd->scsi_done = scsi_done;
234                 /* Queue up, Direct Mode or Task Collector Mode. */
235                 if (sas_ha->lldd_max_execute_num < 2)
236                         res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
237                 else
238                         res = sas_queue_up(task);
239
240                 /* Examine */
241                 if (res) {
242                         SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
243                         ASSIGN_SAS_TASK(cmd, NULL);
244                         sas_free_task(task);
245                         if (res == -SAS_QUEUE_FULL) {
246                                 cmd->result = DID_SOFT_ERROR << 16; /* retry */
247                                 res = 0;
248                                 scsi_done(cmd);
249                         }
250                         goto out;
251                 }
252         }
253 out:
254         spin_lock_irq(host->host_lock);
255         return res;
256 }
257
258 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
259 {
260         struct scsi_cmnd *cmd, *n;
261
262         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
263                 if (cmd == my_cmd)
264                         list_del_init(&cmd->eh_entry);
265         }
266 }
267
268 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
269                                      struct domain_device *dev)
270 {
271         struct scsi_cmnd *cmd, *n;
272
273         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
274                 struct domain_device *x = cmd_to_domain_dev(cmd);
275
276                 if (x == dev)
277                         list_del_init(&cmd->eh_entry);
278         }
279 }
280
281 static void sas_scsi_clear_queue_port(struct list_head *error_q,
282                                       struct asd_sas_port *port)
283 {
284         struct scsi_cmnd *cmd, *n;
285
286         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
287                 struct domain_device *dev = cmd_to_domain_dev(cmd);
288                 struct asd_sas_port *x = dev->port;
289
290                 if (x == port)
291                         list_del_init(&cmd->eh_entry);
292         }
293 }
294
295 enum task_disposition {
296         TASK_IS_DONE,
297         TASK_IS_ABORTED,
298         TASK_IS_AT_LU,
299         TASK_IS_NOT_AT_LU,
300         TASK_ABORT_FAILED,
301 };
302
303 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
304 {
305         struct sas_ha_struct *ha = task->dev->port->ha;
306         unsigned long flags;
307         int i, res;
308         struct sas_internal *si =
309                 to_sas_internal(task->dev->port->ha->core.shost->transportt);
310
311         if (ha->lldd_max_execute_num > 1) {
312                 struct scsi_core *core = &ha->core;
313                 struct sas_task *t, *n;
314
315                 spin_lock_irqsave(&core->task_queue_lock, flags);
316                 list_for_each_entry_safe(t, n, &core->task_queue, list) {
317                         if (task == t) {
318                                 list_del_init(&t->list);
319                                 spin_unlock_irqrestore(&core->task_queue_lock,
320                                                        flags);
321                                 SAS_DPRINTK("%s: task 0x%p aborted from "
322                                             "task_queue\n",
323                                             __FUNCTION__, task);
324                                 return TASK_IS_ABORTED;
325                         }
326                 }
327                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
328         }
329
330         for (i = 0; i < 5; i++) {
331                 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
332                 res = si->dft->lldd_abort_task(task);
333
334                 spin_lock_irqsave(&task->task_state_lock, flags);
335                 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
336                         spin_unlock_irqrestore(&task->task_state_lock, flags);
337                         SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
338                                     task);
339                         return TASK_IS_DONE;
340                 }
341                 spin_unlock_irqrestore(&task->task_state_lock, flags);
342
343                 if (res == TMF_RESP_FUNC_COMPLETE) {
344                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
345                                     __FUNCTION__, task);
346                         return TASK_IS_ABORTED;
347                 } else if (si->dft->lldd_query_task) {
348                         SAS_DPRINTK("%s: querying task 0x%p\n",
349                                     __FUNCTION__, task);
350                         res = si->dft->lldd_query_task(task);
351                         switch (res) {
352                         case TMF_RESP_FUNC_SUCC:
353                                 SAS_DPRINTK("%s: task 0x%p at LU\n",
354                                             __FUNCTION__, task);
355                                 return TASK_IS_AT_LU;
356                         case TMF_RESP_FUNC_COMPLETE:
357                                 SAS_DPRINTK("%s: task 0x%p not at LU\n",
358                                             __FUNCTION__, task);
359                                 return TASK_IS_NOT_AT_LU;
360                         case TMF_RESP_FUNC_FAILED:
361                                 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
362                                                 __FUNCTION__, task);
363                                 return TASK_ABORT_FAILED;
364                         }
365
366                 }
367         }
368         return res;
369 }
370
371 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
372 {
373         int res = TMF_RESP_FUNC_FAILED;
374         struct scsi_lun lun;
375         struct sas_internal *i =
376                 to_sas_internal(dev->port->ha->core.shost->transportt);
377
378         int_to_scsilun(cmd->device->lun, &lun);
379
380         SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
381                     SAS_ADDR(dev->sas_addr),
382                     cmd->device->lun);
383
384         if (i->dft->lldd_abort_task_set)
385                 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
386
387         if (res == TMF_RESP_FUNC_FAILED) {
388                 if (i->dft->lldd_clear_task_set)
389                         res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
390         }
391
392         if (res == TMF_RESP_FUNC_FAILED) {
393                 if (i->dft->lldd_lu_reset)
394                         res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
395         }
396
397         return res;
398 }
399
400 static int sas_recover_I_T(struct domain_device *dev)
401 {
402         int res = TMF_RESP_FUNC_FAILED;
403         struct sas_internal *i =
404                 to_sas_internal(dev->port->ha->core.shost->transportt);
405
406         SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
407                     SAS_ADDR(dev->sas_addr));
408
409         if (i->dft->lldd_I_T_nexus_reset)
410                 res = i->dft->lldd_I_T_nexus_reset(dev);
411
412         return res;
413 }
414
415 /* Find the sas_phy that's attached to this device */
416 static struct sas_phy *find_local_sas_phy(struct domain_device *dev)
417 {
418         struct domain_device *pdev = dev->parent;
419         struct ex_phy *exphy = NULL;
420         int i;
421
422         /* Directly attached device */
423         if (!pdev)
424                 return dev->port->phy;
425
426         /* Otherwise look in the expander */
427         for (i = 0; i < pdev->ex_dev.num_phys; i++)
428                 if (!memcmp(dev->sas_addr,
429                             pdev->ex_dev.ex_phy[i].attached_sas_addr,
430                             SAS_ADDR_SIZE)) {
431                         exphy = &pdev->ex_dev.ex_phy[i];
432                         break;
433                 }
434
435         BUG_ON(!exphy);
436         return exphy->phy;
437 }
438
439 /* Attempt to send a LUN reset message to a device */
440 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
441 {
442         struct domain_device *dev = cmd_to_domain_dev(cmd);
443         struct sas_internal *i =
444                 to_sas_internal(dev->port->ha->core.shost->transportt);
445         struct scsi_lun lun;
446         int res;
447
448         int_to_scsilun(cmd->device->lun, &lun);
449
450         if (!i->dft->lldd_lu_reset)
451                 return FAILED;
452
453         res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
454         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
455                 return SUCCESS;
456
457         return FAILED;
458 }
459
460 /* Attempt to send a phy (bus) reset */
461 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
462 {
463         struct domain_device *dev = cmd_to_domain_dev(cmd);
464         struct sas_phy *phy = find_local_sas_phy(dev);
465         int res;
466
467         res = sas_phy_reset(phy, 1);
468         if (res)
469                 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
470                             kobject_name(&phy->dev.kobj),
471                             res);
472         if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
473                 return SUCCESS;
474
475         return FAILED;
476 }
477
478 /* Try to reset a device */
479 static int try_to_reset_cmd_device(struct Scsi_Host *shost,
480                                    struct scsi_cmnd *cmd)
481 {
482         int res;
483
484         if (!shost->hostt->eh_device_reset_handler)
485                 goto try_bus_reset;
486
487         res = shost->hostt->eh_device_reset_handler(cmd);
488         if (res == SUCCESS)
489                 return res;
490
491 try_bus_reset:
492         if (shost->hostt->eh_bus_reset_handler)
493                 return shost->hostt->eh_bus_reset_handler(cmd);
494
495         return FAILED;
496 }
497
498 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
499                                     struct list_head *work_q,
500                                     struct list_head *done_q)
501 {
502         struct scsi_cmnd *cmd, *n;
503         enum task_disposition res = TASK_IS_DONE;
504         int tmf_resp, need_reset;
505         struct sas_internal *i = to_sas_internal(shost->transportt);
506         unsigned long flags;
507         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
508
509 Again:
510         list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
511                 struct sas_task *task = TO_SAS_TASK(cmd);
512
513                 if (!task)
514                         continue;
515
516                 list_del_init(&cmd->eh_entry);
517
518                 spin_lock_irqsave(&task->task_state_lock, flags);
519                 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
520                 spin_unlock_irqrestore(&task->task_state_lock, flags);
521
522                 SAS_DPRINTK("trying to find task 0x%p\n", task);
523                 res = sas_scsi_find_task(task);
524
525                 cmd->eh_eflags = 0;
526
527                 switch (res) {
528                 case TASK_IS_DONE:
529                         SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
530                                     task);
531                         task->task_done(task);
532                         if (need_reset)
533                                 try_to_reset_cmd_device(shost, cmd);
534                         continue;
535                 case TASK_IS_ABORTED:
536                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
537                                     __FUNCTION__, task);
538                         task->task_done(task);
539                         if (need_reset)
540                                 try_to_reset_cmd_device(shost, cmd);
541                         continue;
542                 case TASK_IS_AT_LU:
543                         SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
544                         tmf_resp = sas_recover_lu(task->dev, cmd);
545                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
546                                 SAS_DPRINTK("dev %016llx LU %x is "
547                                             "recovered\n",
548                                             SAS_ADDR(task->dev),
549                                             cmd->device->lun);
550                                 task->task_done(task);
551                                 if (need_reset)
552                                         try_to_reset_cmd_device(shost, cmd);
553                                 sas_scsi_clear_queue_lu(work_q, cmd);
554                                 goto Again;
555                         }
556                         /* fallthrough */
557                 case TASK_IS_NOT_AT_LU:
558                 case TASK_ABORT_FAILED:
559                         SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
560                                     task);
561                         tmf_resp = sas_recover_I_T(task->dev);
562                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
563                                 SAS_DPRINTK("I_T %016llx recovered\n",
564                                             SAS_ADDR(task->dev->sas_addr));
565                                 task->task_done(task);
566                                 if (need_reset)
567                                         try_to_reset_cmd_device(shost, cmd);
568                                 sas_scsi_clear_queue_I_T(work_q, task->dev);
569                                 goto Again;
570                         }
571                         /* Hammer time :-) */
572                         if (i->dft->lldd_clear_nexus_port) {
573                                 struct asd_sas_port *port = task->dev->port;
574                                 SAS_DPRINTK("clearing nexus for port:%d\n",
575                                             port->id);
576                                 res = i->dft->lldd_clear_nexus_port(port);
577                                 if (res == TMF_RESP_FUNC_COMPLETE) {
578                                         SAS_DPRINTK("clear nexus port:%d "
579                                                     "succeeded\n", port->id);
580                                         task->task_done(task);
581                                         if (need_reset)
582                                                 try_to_reset_cmd_device(shost, cmd);
583                                         sas_scsi_clear_queue_port(work_q,
584                                                                   port);
585                                         goto Again;
586                                 }
587                         }
588                         if (i->dft->lldd_clear_nexus_ha) {
589                                 SAS_DPRINTK("clear nexus ha\n");
590                                 res = i->dft->lldd_clear_nexus_ha(ha);
591                                 if (res == TMF_RESP_FUNC_COMPLETE) {
592                                         SAS_DPRINTK("clear nexus ha "
593                                                     "succeeded\n");
594                                         task->task_done(task);
595                                         if (need_reset)
596                                                 try_to_reset_cmd_device(shost, cmd);
597                                         goto out;
598                                 }
599                         }
600                         /* If we are here -- this means that no amount
601                          * of effort could recover from errors.  Quite
602                          * possibly the HA just disappeared.
603                          */
604                         SAS_DPRINTK("error from  device %llx, LUN %x "
605                                     "couldn't be recovered in any way\n",
606                                     SAS_ADDR(task->dev->sas_addr),
607                                     cmd->device->lun);
608
609                         task->task_done(task);
610                         if (need_reset)
611                                 try_to_reset_cmd_device(shost, cmd);
612                         goto clear_q;
613                 }
614         }
615 out:
616         return list_empty(work_q);
617 clear_q:
618         SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
619         list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
620                 struct sas_task *task = TO_SAS_TASK(cmd);
621                 list_del_init(&cmd->eh_entry);
622                 task->task_done(task);
623         }
624         return list_empty(work_q);
625 }
626
627 void sas_scsi_recover_host(struct Scsi_Host *shost)
628 {
629         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
630         unsigned long flags;
631         LIST_HEAD(eh_work_q);
632
633         spin_lock_irqsave(shost->host_lock, flags);
634         list_splice_init(&shost->eh_cmd_q, &eh_work_q);
635         spin_unlock_irqrestore(shost->host_lock, flags);
636
637         SAS_DPRINTK("Enter %s\n", __FUNCTION__);
638         /*
639          * Deal with commands that still have SAS tasks (i.e. they didn't
640          * complete via the normal sas_task completion mechanism)
641          */
642         if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
643                 goto out;
644
645         /*
646          * Now deal with SCSI commands that completed ok but have a an error
647          * code (and hopefully sense data) attached.  This is roughly what
648          * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
649          * command we see here has no sas_task and is thus unknown to the HA.
650          */
651         if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
652                 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
653
654 out:
655         scsi_eh_flush_done_q(&ha->eh_done_q);
656         SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
657         return;
658 }
659
660 enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
661 {
662         struct sas_task *task = TO_SAS_TASK(cmd);
663         unsigned long flags;
664
665         if (!task) {
666                 cmd->timeout_per_command /= 2;
667                 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
668                             cmd, task, (cmd->timeout_per_command ?
669                             "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
670                 if (!cmd->timeout_per_command)
671                         return EH_NOT_HANDLED;
672                 return EH_RESET_TIMER;
673         }
674
675         spin_lock_irqsave(&task->task_state_lock, flags);
676         BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
677         if (task->task_state_flags & SAS_TASK_STATE_DONE) {
678                 spin_unlock_irqrestore(&task->task_state_lock, flags);
679                 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
680                             cmd, task);
681                 return EH_HANDLED;
682         }
683         if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
684                 spin_unlock_irqrestore(&task->task_state_lock, flags);
685                 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
686                             "EH_RESET_TIMER\n",
687                             cmd, task);
688                 return EH_RESET_TIMER;
689         }
690         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
691         spin_unlock_irqrestore(&task->task_state_lock, flags);
692
693         SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
694                     cmd, task);
695
696         return EH_NOT_HANDLED;
697 }
698
699 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
700 {
701         struct domain_device *dev = sdev_to_domain_dev(sdev);
702
703         if (dev_is_sata(dev))
704                 return ata_scsi_ioctl(sdev, cmd, arg);
705
706         return -EINVAL;
707 }
708
709 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
710 {
711         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
712         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
713         struct domain_device *found_dev = NULL;
714         int i;
715         unsigned long flags;
716
717         spin_lock_irqsave(&ha->phy_port_lock, flags);
718         for (i = 0; i < ha->num_phys; i++) {
719                 struct asd_sas_port *port = ha->sas_port[i];
720                 struct domain_device *dev;
721
722                 spin_lock(&port->dev_list_lock);
723                 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
724                         if (rphy == dev->rphy) {
725                                 found_dev = dev;
726                                 spin_unlock(&port->dev_list_lock);
727                                 goto found;
728                         }
729                 }
730                 spin_unlock(&port->dev_list_lock);
731         }
732  found:
733         spin_unlock_irqrestore(&ha->phy_port_lock, flags);
734
735         return found_dev;
736 }
737
738 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
739 {
740         struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
741
742         return sas_find_dev_by_rphy(rphy);
743 }
744
745 int sas_target_alloc(struct scsi_target *starget)
746 {
747         struct domain_device *found_dev = sas_find_target(starget);
748         int res;
749
750         if (!found_dev)
751                 return -ENODEV;
752
753         if (dev_is_sata(found_dev)) {
754                 res = sas_ata_init_host_and_port(found_dev, starget);
755                 if (res)
756                         return res;
757         }
758
759         starget->hostdata = found_dev;
760         return 0;
761 }
762
763 #define SAS_DEF_QD 32
764 #define SAS_MAX_QD 64
765
766 int sas_slave_configure(struct scsi_device *scsi_dev)
767 {
768         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
769         struct sas_ha_struct *sas_ha;
770
771         BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
772
773         if (dev_is_sata(dev)) {
774                 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
775                 return 0;
776         }
777
778         sas_ha = dev->port->ha;
779
780         sas_read_port_mode_page(scsi_dev);
781
782         if (scsi_dev->tagged_supported) {
783                 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
784                 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
785         } else {
786                 SAS_DPRINTK("device %llx, LUN %x doesn't support "
787                             "TCQ\n", SAS_ADDR(dev->sas_addr),
788                             scsi_dev->lun);
789                 scsi_dev->tagged_supported = 0;
790                 scsi_set_tag_type(scsi_dev, 0);
791                 scsi_deactivate_tcq(scsi_dev, 1);
792         }
793
794         scsi_dev->allow_restart = 1;
795
796         return 0;
797 }
798
799 void sas_slave_destroy(struct scsi_device *scsi_dev)
800 {
801         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
802
803         if (dev_is_sata(dev))
804                 ata_port_disable(dev->sata_dev.ap);
805 }
806
807 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
808 {
809         int res = min(new_depth, SAS_MAX_QD);
810
811         if (scsi_dev->tagged_supported)
812                 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
813                                         res);
814         else {
815                 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
816                 sas_printk("device %llx LUN %x queue depth changed to 1\n",
817                            SAS_ADDR(dev->sas_addr),
818                            scsi_dev->lun);
819                 scsi_adjust_queue_depth(scsi_dev, 0, 1);
820                 res = 1;
821         }
822
823         return res;
824 }
825
826 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
827 {
828         if (!scsi_dev->tagged_supported)
829                 return 0;
830
831         scsi_deactivate_tcq(scsi_dev, 1);
832
833         scsi_set_tag_type(scsi_dev, qt);
834         scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
835
836         return qt;
837 }
838
839 int sas_bios_param(struct scsi_device *scsi_dev,
840                           struct block_device *bdev,
841                           sector_t capacity, int *hsc)
842 {
843         hsc[0] = 255;
844         hsc[1] = 63;
845         sector_div(capacity, 255*63);
846         hsc[2] = capacity;
847
848         return 0;
849 }
850
851 /* ---------- Task Collector Thread implementation ---------- */
852
853 static void sas_queue(struct sas_ha_struct *sas_ha)
854 {
855         struct scsi_core *core = &sas_ha->core;
856         unsigned long flags;
857         LIST_HEAD(q);
858         int can_queue;
859         int res;
860         struct sas_internal *i = to_sas_internal(core->shost->transportt);
861
862         spin_lock_irqsave(&core->task_queue_lock, flags);
863         while (!kthread_should_stop() &&
864                !list_empty(&core->task_queue)) {
865
866                 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
867                 if (can_queue >= 0) {
868                         can_queue = core->task_queue_size;
869                         list_splice_init(&core->task_queue, &q);
870                 } else {
871                         struct list_head *a, *n;
872
873                         can_queue = sas_ha->lldd_queue_size;
874                         list_for_each_safe(a, n, &core->task_queue) {
875                                 list_move_tail(a, &q);
876                                 if (--can_queue == 0)
877                                         break;
878                         }
879                         can_queue = sas_ha->lldd_queue_size;
880                 }
881                 core->task_queue_size -= can_queue;
882                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
883                 {
884                         struct sas_task *task = list_entry(q.next,
885                                                            struct sas_task,
886                                                            list);
887                         list_del_init(&q);
888                         res = i->dft->lldd_execute_task(task, can_queue,
889                                                         GFP_KERNEL);
890                         if (unlikely(res))
891                                 __list_add(&q, task->list.prev, &task->list);
892                 }
893                 spin_lock_irqsave(&core->task_queue_lock, flags);
894                 if (res) {
895                         list_splice_init(&q, &core->task_queue); /*at head*/
896                         core->task_queue_size += can_queue;
897                 }
898         }
899         spin_unlock_irqrestore(&core->task_queue_lock, flags);
900 }
901
902 /**
903  * sas_queue_thread -- The Task Collector thread
904  * @_sas_ha: pointer to struct sas_ha
905  */
906 static int sas_queue_thread(void *_sas_ha)
907 {
908         struct sas_ha_struct *sas_ha = _sas_ha;
909
910         while (1) {
911                 set_current_state(TASK_INTERRUPTIBLE);
912                 schedule();
913                 sas_queue(sas_ha);
914                 if (kthread_should_stop())
915                         break;
916         }
917
918         return 0;
919 }
920
921 int sas_init_queue(struct sas_ha_struct *sas_ha)
922 {
923         struct scsi_core *core = &sas_ha->core;
924
925         spin_lock_init(&core->task_queue_lock);
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  * Call the LLDD task abort routine directly.  This function is intended for
964  * use by upper layers that need to tell the LLDD to abort a task.
965  */
966 int __sas_task_abort(struct sas_task *task)
967 {
968         struct sas_internal *si =
969                 to_sas_internal(task->dev->port->ha->core.shost->transportt);
970         unsigned long flags;
971         int res;
972
973         spin_lock_irqsave(&task->task_state_lock, flags);
974         if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
975             task->task_state_flags & SAS_TASK_STATE_DONE) {
976                 spin_unlock_irqrestore(&task->task_state_lock, flags);
977                 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
978                             task);
979                 return 0;
980         }
981         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
982         spin_unlock_irqrestore(&task->task_state_lock, flags);
983
984         if (!si->dft->lldd_abort_task)
985                 return -ENODEV;
986
987         res = si->dft->lldd_abort_task(task);
988
989         spin_lock_irqsave(&task->task_state_lock, flags);
990         if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
991             (res == TMF_RESP_FUNC_COMPLETE))
992         {
993                 spin_unlock_irqrestore(&task->task_state_lock, flags);
994                 task->task_done(task);
995                 return 0;
996         }
997
998         if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
999                 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
1000         spin_unlock_irqrestore(&task->task_state_lock, flags);
1001
1002         return -EAGAIN;
1003 }
1004
1005 /*
1006  * Tell an upper layer that it needs to initiate an abort for a given task.
1007  * This should only ever be called by an LLDD.
1008  */
1009 void sas_task_abort(struct sas_task *task)
1010 {
1011         struct scsi_cmnd *sc = task->uldd_task;
1012
1013         /* Escape for libsas internal commands */
1014         if (!sc) {
1015                 if (!del_timer(&task->timer))
1016                         return;
1017                 task->timer.function(task->timer.data);
1018                 return;
1019         }
1020
1021         if (dev_is_sata(task->dev)) {
1022                 sas_ata_task_abort(task);
1023                 return;
1024         }
1025
1026         scsi_req_abort_cmd(sc);
1027         scsi_schedule_eh(sc->device->host);
1028 }
1029
1030 int sas_slave_alloc(struct scsi_device *scsi_dev)
1031 {
1032         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1033
1034         if (dev_is_sata(dev))
1035                 return ata_sas_port_init(dev->sata_dev.ap);
1036
1037         return 0;
1038 }
1039
1040 void sas_target_destroy(struct scsi_target *starget)
1041 {
1042         struct domain_device *found_dev = sas_find_target(starget);
1043
1044         if (!found_dev)
1045                 return;
1046
1047         if (dev_is_sata(found_dev))
1048                 ata_sas_port_destroy(found_dev->sata_dev.ap);
1049
1050         return;
1051 }
1052
1053 EXPORT_SYMBOL_GPL(sas_queuecommand);
1054 EXPORT_SYMBOL_GPL(sas_target_alloc);
1055 EXPORT_SYMBOL_GPL(sas_slave_configure);
1056 EXPORT_SYMBOL_GPL(sas_slave_destroy);
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_task_abort);
1062 EXPORT_SYMBOL_GPL(sas_phy_reset);
1063 EXPORT_SYMBOL_GPL(sas_phy_enable);
1064 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1065 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1066 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1067 EXPORT_SYMBOL_GPL(sas_target_destroy);
1068 EXPORT_SYMBOL_GPL(sas_ioctl);