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