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