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