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