Actually update the fixed up compile failures.
[linux-2.6] / drivers / scsi / libsas / sas_scsi_host.c
1 /*
2  * Serial Attached SCSI (SAS) class SCSI Host glue.
3  *
4  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
5  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6  *
7  * This file is licensed under GPLv2.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  */
25
26 #include "sas_internal.h"
27
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_sas.h>
35 #include "../scsi_sas_internal.h"
36 #include "../scsi_transport_api.h"
37
38 #include <linux/err.h>
39 #include <linux/blkdev.h>
40 #include <linux/scatterlist.h>
41
42 /* ---------- SCSI Host glue ---------- */
43
44 #define TO_SAS_TASK(_scsi_cmd)  ((void *)(_scsi_cmd)->host_scribble)
45 #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
46
47 static void sas_scsi_task_done(struct sas_task *task)
48 {
49         struct task_status_struct *ts = &task->task_status;
50         struct scsi_cmnd *sc = task->uldd_task;
51         struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
52         unsigned ts_flags = task->task_state_flags;
53         int hs = 0, stat = 0;
54
55         if (unlikely(!sc)) {
56                 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
57                 list_del_init(&task->list);
58                 sas_free_task(task);
59                 return;
60         }
61
62         if (ts->resp == SAS_TASK_UNDELIVERED) {
63                 /* transport error */
64                 hs = DID_NO_CONNECT;
65         } else { /* ts->resp == SAS_TASK_COMPLETE */
66                 /* task delivered, what happened afterwards? */
67                 switch (ts->stat) {
68                 case SAS_DEV_NO_RESPONSE:
69                 case SAS_INTERRUPTED:
70                 case SAS_PHY_DOWN:
71                 case SAS_NAK_R_ERR:
72                 case SAS_OPEN_TO:
73                         hs = DID_NO_CONNECT;
74                         break;
75                 case SAS_DATA_UNDERRUN:
76                         sc->resid = ts->residual;
77                         if (sc->request_bufflen - sc->resid < sc->underflow)
78                                 hs = DID_ERROR;
79                         break;
80                 case SAS_DATA_OVERRUN:
81                         hs = DID_ERROR;
82                         break;
83                 case SAS_QUEUE_FULL:
84                         hs = DID_SOFT_ERROR; /* retry */
85                         break;
86                 case SAS_DEVICE_UNKNOWN:
87                         hs = DID_BAD_TARGET;
88                         break;
89                 case SAS_SG_ERR:
90                         hs = DID_PARITY;
91                         break;
92                 case SAS_OPEN_REJECT:
93                         if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
94                                 hs = DID_SOFT_ERROR; /* retry */
95                         else
96                                 hs = DID_ERROR;
97                         break;
98                 case SAS_PROTO_RESPONSE:
99                         SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
100                                     "task; please report this\n",
101                                     task->dev->port->ha->sas_ha_name);
102                         break;
103                 case SAS_ABORTED_TASK:
104                         hs = DID_ABORT;
105                         break;
106                 case SAM_CHECK_COND:
107                         memcpy(sc->sense_buffer, ts->buf,
108                                max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
109                         stat = SAM_CHECK_COND;
110                         break;
111                 default:
112                         stat = ts->stat;
113                         break;
114                 }
115         }
116         ASSIGN_SAS_TASK(sc, NULL);
117         sc->result = (hs << 16) | stat;
118         list_del_init(&task->list);
119         sas_free_task(task);
120         /* This is very ugly but this is how SCSI Core works. */
121         if (ts_flags & SAS_TASK_STATE_ABORTED)
122                 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
123         else
124                 sc->scsi_done(sc);
125 }
126
127 static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
128 {
129         enum task_attribute ta = TASK_ATTR_SIMPLE;
130         if (cmd->request && blk_rq_tagged(cmd->request)) {
131                 if (cmd->device->ordered_tags &&
132                     (cmd->request->cmd_flags & REQ_HARDBARRIER))
133                         ta = TASK_ATTR_HOQ;
134         }
135         return ta;
136 }
137
138 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
139                                                struct domain_device *dev,
140                                                gfp_t gfp_flags)
141 {
142         struct sas_task *task = sas_alloc_task(gfp_flags);
143         struct scsi_lun lun;
144
145         if (!task)
146                 return NULL;
147
148         *(u32 *)cmd->sense_buffer = 0;
149         task->uldd_task = cmd;
150         ASSIGN_SAS_TASK(cmd, task);
151
152         task->dev = dev;
153         task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
154
155         task->ssp_task.retry_count = 1;
156         int_to_scsilun(cmd->device->lun, &lun);
157         memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
158         task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
159         memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
160
161         task->scatter = cmd->request_buffer;
162         task->num_scatter = cmd->use_sg;
163         task->total_xfer_len = cmd->request_bufflen;
164         task->data_dir = cmd->sc_data_direction;
165
166         task->task_done = sas_scsi_task_done;
167
168         return task;
169 }
170
171 static int sas_queue_up(struct sas_task *task)
172 {
173         struct sas_ha_struct *sas_ha = task->dev->port->ha;
174         struct scsi_core *core = &sas_ha->core;
175         unsigned long flags;
176         LIST_HEAD(list);
177
178         spin_lock_irqsave(&core->task_queue_lock, flags);
179         if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
180                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
181                 return -SAS_QUEUE_FULL;
182         }
183         list_add_tail(&task->list, &core->task_queue);
184         core->task_queue_size += 1;
185         spin_unlock_irqrestore(&core->task_queue_lock, flags);
186         up(&core->queue_thread_sema);
187
188         return 0;
189 }
190
191 /**
192  * sas_queuecommand -- Enqueue a command for processing
193  * @parameters: See SCSI Core documentation
194  *
195  * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
196  * call us without holding an IRQ spinlock...
197  */
198 int sas_queuecommand(struct scsi_cmnd *cmd,
199                      void (*scsi_done)(struct scsi_cmnd *))
200 {
201         int res = 0;
202         struct domain_device *dev = cmd_to_domain_dev(cmd);
203         struct Scsi_Host *host = cmd->device->host;
204         struct sas_internal *i = to_sas_internal(host->transportt);
205
206         spin_unlock_irq(host->host_lock);
207
208         {
209                 struct sas_ha_struct *sas_ha = dev->port->ha;
210                 struct sas_task *task;
211
212                 res = -ENOMEM;
213                 task = sas_create_task(cmd, dev, GFP_ATOMIC);
214                 if (!task)
215                         goto out;
216
217                 cmd->scsi_done = scsi_done;
218                 /* Queue up, Direct Mode or Task Collector Mode. */
219                 if (sas_ha->lldd_max_execute_num < 2)
220                         res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
221                 else
222                         res = sas_queue_up(task);
223
224                 /* Examine */
225                 if (res) {
226                         SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
227                         ASSIGN_SAS_TASK(cmd, NULL);
228                         sas_free_task(task);
229                         if (res == -SAS_QUEUE_FULL) {
230                                 cmd->result = DID_SOFT_ERROR << 16; /* retry */
231                                 res = 0;
232                                 scsi_done(cmd);
233                         }
234                         goto out;
235                 }
236         }
237 out:
238         spin_lock_irq(host->host_lock);
239         return res;
240 }
241
242 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
243 {
244         struct scsi_cmnd *cmd, *n;
245
246         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
247                 if (cmd == my_cmd)
248                         list_del_init(&cmd->eh_entry);
249         }
250 }
251
252 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
253                                      struct domain_device *dev)
254 {
255         struct scsi_cmnd *cmd, *n;
256
257         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
258                 struct domain_device *x = cmd_to_domain_dev(cmd);
259
260                 if (x == dev)
261                         list_del_init(&cmd->eh_entry);
262         }
263 }
264
265 static void sas_scsi_clear_queue_port(struct list_head *error_q,
266                                       struct asd_sas_port *port)
267 {
268         struct scsi_cmnd *cmd, *n;
269
270         list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
271                 struct domain_device *dev = cmd_to_domain_dev(cmd);
272                 struct asd_sas_port *x = dev->port;
273
274                 if (x == port)
275                         list_del_init(&cmd->eh_entry);
276         }
277 }
278
279 enum task_disposition {
280         TASK_IS_DONE,
281         TASK_IS_ABORTED,
282         TASK_IS_AT_LU,
283         TASK_IS_NOT_AT_LU,
284 };
285
286 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
287 {
288         struct sas_ha_struct *ha = task->dev->port->ha;
289         unsigned long flags;
290         int i, res;
291         struct sas_internal *si =
292                 to_sas_internal(task->dev->port->ha->core.shost->transportt);
293
294         if (ha->lldd_max_execute_num > 1) {
295                 struct scsi_core *core = &ha->core;
296                 struct sas_task *t, *n;
297
298                 spin_lock_irqsave(&core->task_queue_lock, flags);
299                 list_for_each_entry_safe(t, n, &core->task_queue, list) {
300                         if (task == t) {
301                                 list_del_init(&t->list);
302                                 spin_unlock_irqrestore(&core->task_queue_lock,
303                                                        flags);
304                                 SAS_DPRINTK("%s: task 0x%p aborted from "
305                                             "task_queue\n",
306                                             __FUNCTION__, task);
307                                 return TASK_IS_ABORTED;
308                         }
309                 }
310                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
311         }
312
313         spin_lock_irqsave(&task->task_state_lock, flags);
314         if (task->task_state_flags & SAS_TASK_INITIATOR_ABORTED) {
315                 spin_unlock_irqrestore(&task->task_state_lock, flags);
316                 SAS_DPRINTK("%s: task 0x%p already aborted\n",
317                             __FUNCTION__, task);
318                 return TASK_IS_ABORTED;
319         }
320         spin_unlock_irqrestore(&task->task_state_lock, flags);
321
322         for (i = 0; i < 5; i++) {
323                 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
324                 res = si->dft->lldd_abort_task(task);
325
326                 spin_lock_irqsave(&task->task_state_lock, flags);
327                 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
328                         spin_unlock_irqrestore(&task->task_state_lock, flags);
329                         SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
330                                     task);
331                         return TASK_IS_DONE;
332                 }
333                 spin_unlock_irqrestore(&task->task_state_lock, flags);
334
335                 if (res == TMF_RESP_FUNC_COMPLETE) {
336                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
337                                     __FUNCTION__, task);
338                         return TASK_IS_ABORTED;
339                 } else if (si->dft->lldd_query_task) {
340                         SAS_DPRINTK("%s: querying task 0x%p\n",
341                                     __FUNCTION__, task);
342                         res = si->dft->lldd_query_task(task);
343                         if (res == TMF_RESP_FUNC_SUCC) {
344                                 SAS_DPRINTK("%s: task 0x%p at LU\n",
345                                             __FUNCTION__, task);
346                                 return TASK_IS_AT_LU;
347                         } else if (res == TMF_RESP_FUNC_COMPLETE) {
348                                 SAS_DPRINTK("%s: task 0x%p not at LU\n",
349                                             __FUNCTION__, task);
350                                 return TASK_IS_NOT_AT_LU;
351                         }
352                 }
353         }
354         return res;
355 }
356
357 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
358 {
359         int res = TMF_RESP_FUNC_FAILED;
360         struct scsi_lun lun;
361         struct sas_internal *i =
362                 to_sas_internal(dev->port->ha->core.shost->transportt);
363
364         int_to_scsilun(cmd->device->lun, &lun);
365
366         SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
367                     SAS_ADDR(dev->sas_addr),
368                     cmd->device->lun);
369
370         if (i->dft->lldd_abort_task_set)
371                 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
372
373         if (res == TMF_RESP_FUNC_FAILED) {
374                 if (i->dft->lldd_clear_task_set)
375                         res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
376         }
377
378         if (res == TMF_RESP_FUNC_FAILED) {
379                 if (i->dft->lldd_lu_reset)
380                         res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
381         }
382
383         return res;
384 }
385
386 static int sas_recover_I_T(struct domain_device *dev)
387 {
388         int res = TMF_RESP_FUNC_FAILED;
389         struct sas_internal *i =
390                 to_sas_internal(dev->port->ha->core.shost->transportt);
391
392         SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
393                     SAS_ADDR(dev->sas_addr));
394
395         if (i->dft->lldd_I_T_nexus_reset)
396                 res = i->dft->lldd_I_T_nexus_reset(dev);
397
398         return res;
399 }
400
401 void sas_scsi_recover_host(struct Scsi_Host *shost)
402 {
403         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
404         unsigned long flags;
405         LIST_HEAD(error_q);
406         struct scsi_cmnd *cmd, *n;
407         enum task_disposition res = TASK_IS_DONE;
408         int tmf_resp;
409         struct sas_internal *i = to_sas_internal(shost->transportt);
410
411         spin_lock_irqsave(shost->host_lock, flags);
412         list_splice_init(&shost->eh_cmd_q, &error_q);
413         spin_unlock_irqrestore(shost->host_lock, flags);
414
415         SAS_DPRINTK("Enter %s\n", __FUNCTION__);
416
417         /* All tasks on this list were marked SAS_TASK_STATE_ABORTED
418          * by sas_scsi_timed_out() callback.
419          */
420 Again:
421         SAS_DPRINTK("going over list...\n");
422         list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
423                 struct sas_task *task = TO_SAS_TASK(cmd);
424                 list_del_init(&cmd->eh_entry);
425
426                 if (!task) {
427                         SAS_DPRINTK("%s: taskless cmd?!\n", __FUNCTION__);
428                         continue;
429                 }
430                 SAS_DPRINTK("trying to find task 0x%p\n", task);
431                 res = sas_scsi_find_task(task);
432
433                 cmd->eh_eflags = 0;
434
435                 switch (res) {
436                 case TASK_IS_DONE:
437                         SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
438                                     task);
439                         task->task_done(task);
440                         continue;
441                 case TASK_IS_ABORTED:
442                         SAS_DPRINTK("%s: task 0x%p is aborted\n",
443                                     __FUNCTION__, task);
444                         task->task_done(task);
445                         continue;
446                 case TASK_IS_AT_LU:
447                         SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
448                         tmf_resp = sas_recover_lu(task->dev, cmd);
449                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
450                                 SAS_DPRINTK("dev %016llx LU %x is "
451                                             "recovered\n",
452                                             SAS_ADDR(task->dev),
453                                             cmd->device->lun);
454                                 task->task_done(task);
455                                 sas_scsi_clear_queue_lu(&error_q, cmd);
456                                 goto Again;
457                         }
458                         /* fallthrough */
459                 case TASK_IS_NOT_AT_LU:
460                         SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
461                                     task);
462                         tmf_resp = sas_recover_I_T(task->dev);
463                         if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
464                                 SAS_DPRINTK("I_T %016llx recovered\n",
465                                             SAS_ADDR(task->dev->sas_addr));
466                                 task->task_done(task);
467                                 sas_scsi_clear_queue_I_T(&error_q, task->dev);
468                                 goto Again;
469                         }
470                         /* Hammer time :-) */
471                         if (i->dft->lldd_clear_nexus_port) {
472                                 struct asd_sas_port *port = task->dev->port;
473                                 SAS_DPRINTK("clearing nexus for port:%d\n",
474                                             port->id);
475                                 res = i->dft->lldd_clear_nexus_port(port);
476                                 if (res == TMF_RESP_FUNC_COMPLETE) {
477                                         SAS_DPRINTK("clear nexus port:%d "
478                                                     "succeeded\n", port->id);
479                                         task->task_done(task);
480                                         sas_scsi_clear_queue_port(&error_q,
481                                                                   port);
482                                         goto Again;
483                                 }
484                         }
485                         if (i->dft->lldd_clear_nexus_ha) {
486                                 SAS_DPRINTK("clear nexus ha\n");
487                                 res = i->dft->lldd_clear_nexus_ha(ha);
488                                 if (res == TMF_RESP_FUNC_COMPLETE) {
489                                         SAS_DPRINTK("clear nexus ha "
490                                                     "succeeded\n");
491                                         task->task_done(task);
492                                         goto out;
493                                 }
494                         }
495                         /* If we are here -- this means that no amount
496                          * of effort could recover from errors.  Quite
497                          * possibly the HA just disappeared.
498                          */
499                         SAS_DPRINTK("error from  device %llx, LUN %x "
500                                     "couldn't be recovered in any way\n",
501                                     SAS_ADDR(task->dev->sas_addr),
502                                     cmd->device->lun);
503
504                         task->task_done(task);
505                         goto clear_q;
506                 }
507         }
508 out:
509         scsi_eh_flush_done_q(&ha->eh_done_q);
510         SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
511         return;
512 clear_q:
513         SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
514         list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
515                 struct sas_task *task = TO_SAS_TASK(cmd);
516                 list_del_init(&cmd->eh_entry);
517                 task->task_done(task);
518         }
519 }
520
521 enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
522 {
523         struct sas_task *task = TO_SAS_TASK(cmd);
524         unsigned long flags;
525
526         if (!task) {
527                 SAS_DPRINTK("command 0x%p, task 0x%p, gone: EH_HANDLED\n",
528                             cmd, task);
529                 return EH_HANDLED;
530         }
531
532         spin_lock_irqsave(&task->task_state_lock, flags);
533         if (task->task_state_flags & SAS_TASK_INITIATOR_ABORTED) {
534                 spin_unlock_irqrestore(&task->task_state_lock, flags);
535                 SAS_DPRINTK("command 0x%p, task 0x%p, aborted by initiator: "
536                             "EH_NOT_HANDLED\n", cmd, task);
537                 return EH_NOT_HANDLED;
538         }
539         if (task->task_state_flags & SAS_TASK_STATE_DONE) {
540                 spin_unlock_irqrestore(&task->task_state_lock, flags);
541                 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
542                             cmd, task);
543                 return EH_HANDLED;
544         }
545         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
546         spin_unlock_irqrestore(&task->task_state_lock, flags);
547
548         SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
549                     cmd, task);
550
551         return EH_NOT_HANDLED;
552 }
553
554 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
555 {
556         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
557         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
558         struct domain_device *found_dev = NULL;
559         int i;
560
561         spin_lock(&ha->phy_port_lock);
562         for (i = 0; i < ha->num_phys; i++) {
563                 struct asd_sas_port *port = ha->sas_port[i];
564                 struct domain_device *dev;
565
566                 spin_lock(&port->dev_list_lock);
567                 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
568                         if (rphy == dev->rphy) {
569                                 found_dev = dev;
570                                 spin_unlock(&port->dev_list_lock);
571                                 goto found;
572                         }
573                 }
574                 spin_unlock(&port->dev_list_lock);
575         }
576  found:
577         spin_unlock(&ha->phy_port_lock);
578
579         return found_dev;
580 }
581
582 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
583 {
584         struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
585
586         return sas_find_dev_by_rphy(rphy);
587 }
588
589 int sas_target_alloc(struct scsi_target *starget)
590 {
591         struct domain_device *found_dev = sas_find_target(starget);
592
593         if (!found_dev)
594                 return -ENODEV;
595
596         starget->hostdata = found_dev;
597         return 0;
598 }
599
600 #define SAS_DEF_QD 32
601 #define SAS_MAX_QD 64
602
603 int sas_slave_configure(struct scsi_device *scsi_dev)
604 {
605         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
606         struct sas_ha_struct *sas_ha;
607
608         BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
609
610         sas_ha = dev->port->ha;
611
612         sas_read_port_mode_page(scsi_dev);
613
614         if (scsi_dev->tagged_supported) {
615                 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
616                 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
617         } else {
618                 SAS_DPRINTK("device %llx, LUN %x doesn't support "
619                             "TCQ\n", SAS_ADDR(dev->sas_addr),
620                             scsi_dev->lun);
621                 scsi_dev->tagged_supported = 0;
622                 scsi_set_tag_type(scsi_dev, 0);
623                 scsi_deactivate_tcq(scsi_dev, 1);
624         }
625
626         return 0;
627 }
628
629 void sas_slave_destroy(struct scsi_device *scsi_dev)
630 {
631 }
632
633 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
634 {
635         int res = min(new_depth, SAS_MAX_QD);
636
637         if (scsi_dev->tagged_supported)
638                 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
639                                         res);
640         else {
641                 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
642                 sas_printk("device %llx LUN %x queue depth changed to 1\n",
643                            SAS_ADDR(dev->sas_addr),
644                            scsi_dev->lun);
645                 scsi_adjust_queue_depth(scsi_dev, 0, 1);
646                 res = 1;
647         }
648
649         return res;
650 }
651
652 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
653 {
654         if (!scsi_dev->tagged_supported)
655                 return 0;
656
657         scsi_deactivate_tcq(scsi_dev, 1);
658
659         scsi_set_tag_type(scsi_dev, qt);
660         scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
661
662         return qt;
663 }
664
665 int sas_bios_param(struct scsi_device *scsi_dev,
666                           struct block_device *bdev,
667                           sector_t capacity, int *hsc)
668 {
669         hsc[0] = 255;
670         hsc[1] = 63;
671         sector_div(capacity, 255*63);
672         hsc[2] = capacity;
673
674         return 0;
675 }
676
677 /* ---------- Task Collector Thread implementation ---------- */
678
679 static void sas_queue(struct sas_ha_struct *sas_ha)
680 {
681         struct scsi_core *core = &sas_ha->core;
682         unsigned long flags;
683         LIST_HEAD(q);
684         int can_queue;
685         int res;
686         struct sas_internal *i = to_sas_internal(core->shost->transportt);
687
688         spin_lock_irqsave(&core->task_queue_lock, flags);
689         while (!core->queue_thread_kill &&
690                !list_empty(&core->task_queue)) {
691
692                 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
693                 if (can_queue >= 0) {
694                         can_queue = core->task_queue_size;
695                         list_splice_init(&core->task_queue, &q);
696                 } else {
697                         struct list_head *a, *n;
698
699                         can_queue = sas_ha->lldd_queue_size;
700                         list_for_each_safe(a, n, &core->task_queue) {
701                                 list_move_tail(a, &q);
702                                 if (--can_queue == 0)
703                                         break;
704                         }
705                         can_queue = sas_ha->lldd_queue_size;
706                 }
707                 core->task_queue_size -= can_queue;
708                 spin_unlock_irqrestore(&core->task_queue_lock, flags);
709                 {
710                         struct sas_task *task = list_entry(q.next,
711                                                            struct sas_task,
712                                                            list);
713                         list_del_init(&q);
714                         res = i->dft->lldd_execute_task(task, can_queue,
715                                                         GFP_KERNEL);
716                         if (unlikely(res))
717                                 __list_add(&q, task->list.prev, &task->list);
718                 }
719                 spin_lock_irqsave(&core->task_queue_lock, flags);
720                 if (res) {
721                         list_splice_init(&q, &core->task_queue); /*at head*/
722                         core->task_queue_size += can_queue;
723                 }
724         }
725         spin_unlock_irqrestore(&core->task_queue_lock, flags);
726 }
727
728 static DECLARE_COMPLETION(queue_th_comp);
729
730 /**
731  * sas_queue_thread -- The Task Collector thread
732  * @_sas_ha: pointer to struct sas_ha
733  */
734 static int sas_queue_thread(void *_sas_ha)
735 {
736         struct sas_ha_struct *sas_ha = _sas_ha;
737         struct scsi_core *core = &sas_ha->core;
738
739         daemonize("sas_queue_%d", core->shost->host_no);
740         current->flags |= PF_NOFREEZE;
741
742         complete(&queue_th_comp);
743
744         while (1) {
745                 down_interruptible(&core->queue_thread_sema);
746                 sas_queue(sas_ha);
747                 if (core->queue_thread_kill)
748                         break;
749         }
750
751         complete(&queue_th_comp);
752
753         return 0;
754 }
755
756 int sas_init_queue(struct sas_ha_struct *sas_ha)
757 {
758         int res;
759         struct scsi_core *core = &sas_ha->core;
760
761         spin_lock_init(&core->task_queue_lock);
762         core->task_queue_size = 0;
763         INIT_LIST_HEAD(&core->task_queue);
764         init_MUTEX_LOCKED(&core->queue_thread_sema);
765
766         res = kernel_thread(sas_queue_thread, sas_ha, 0);
767         if (res >= 0)
768                 wait_for_completion(&queue_th_comp);
769
770         return res < 0 ? res : 0;
771 }
772
773 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
774 {
775         unsigned long flags;
776         struct scsi_core *core = &sas_ha->core;
777         struct sas_task *task, *n;
778
779         init_completion(&queue_th_comp);
780         core->queue_thread_kill = 1;
781         up(&core->queue_thread_sema);
782         wait_for_completion(&queue_th_comp);
783
784         if (!list_empty(&core->task_queue))
785                 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
786                             SAS_ADDR(sas_ha->sas_addr));
787
788         spin_lock_irqsave(&core->task_queue_lock, flags);
789         list_for_each_entry_safe(task, n, &core->task_queue, list) {
790                 struct scsi_cmnd *cmd = task->uldd_task;
791
792                 list_del_init(&task->list);
793
794                 ASSIGN_SAS_TASK(cmd, NULL);
795                 sas_free_task(task);
796                 cmd->result = DID_ABORT << 16;
797                 cmd->scsi_done(cmd);
798         }
799         spin_unlock_irqrestore(&core->task_queue_lock, flags);
800 }
801
802 static int do_sas_task_abort(struct sas_task *task)
803 {
804         struct scsi_cmnd *sc = task->uldd_task;
805         struct sas_internal *si =
806                 to_sas_internal(task->dev->port->ha->core.shost->transportt);
807         unsigned long flags;
808         int res;
809
810         spin_lock_irqsave(&task->task_state_lock, flags);
811         if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
812                 spin_unlock_irqrestore(&task->task_state_lock, flags);
813                 SAS_DPRINTK("%s: Task %p already aborted.\n", __FUNCTION__,
814                             task);
815                 return 0;
816         }
817
818         task->task_state_flags |= SAS_TASK_INITIATOR_ABORTED;
819         if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
820                 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
821         spin_unlock_irqrestore(&task->task_state_lock, flags);
822
823         if (!si->dft->lldd_abort_task)
824                 return -ENODEV;
825
826         res = si->dft->lldd_abort_task(task);
827         if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
828             (res == TMF_RESP_FUNC_COMPLETE))
829         {
830                 /* SMP commands don't have scsi_cmds(?) */
831                 if (!sc) {
832                         task->task_done(task);
833                         return 0;
834                 }
835                 scsi_req_abort_cmd(sc);
836                 scsi_schedule_eh(sc->device->host);
837                 return 0;
838         }
839
840         spin_lock_irqsave(&task->task_state_lock, flags);
841         task->task_state_flags &= ~SAS_TASK_INITIATOR_ABORTED;
842         if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
843                 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
844         spin_unlock_irqrestore(&task->task_state_lock, flags);
845
846         return -EAGAIN;
847 }
848
849 void sas_task_abort(struct work_struct *work)
850 {
851         struct sas_task *task =
852                 container_of(work, struct sas_task, abort_work);
853         int i;
854
855         for (i = 0; i < 5; i++)
856                 if (!do_sas_task_abort(task))
857                         return;
858
859         SAS_DPRINTK("%s: Could not kill task!\n", __FUNCTION__);
860 }
861
862 EXPORT_SYMBOL_GPL(sas_queuecommand);
863 EXPORT_SYMBOL_GPL(sas_target_alloc);
864 EXPORT_SYMBOL_GPL(sas_slave_configure);
865 EXPORT_SYMBOL_GPL(sas_slave_destroy);
866 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
867 EXPORT_SYMBOL_GPL(sas_change_queue_type);
868 EXPORT_SYMBOL_GPL(sas_bios_param);
869 EXPORT_SYMBOL_GPL(sas_task_abort);
870 EXPORT_SYMBOL_GPL(sas_phy_reset);