Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6.git] / drivers / scsi / aic94xx / aic94xx_task.c
1 /*
2  * Aic94xx SAS/SATA Tasks
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 file is part of the aic94xx driver.
10  *
11  * The aic94xx driver is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; version 2 of the
14  * License.
15  *
16  * The aic94xx driver is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with the aic94xx driver; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  */
26
27 #include <linux/spinlock.h>
28 #include "aic94xx.h"
29 #include "aic94xx_sas.h"
30 #include "aic94xx_hwi.h"
31
32 static void asd_unbuild_ata_ascb(struct asd_ascb *a);
33 static void asd_unbuild_smp_ascb(struct asd_ascb *a);
34 static void asd_unbuild_ssp_ascb(struct asd_ascb *a);
35
36 static inline void asd_can_dequeue(struct asd_ha_struct *asd_ha, int num)
37 {
38         unsigned long flags;
39
40         spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
41         asd_ha->seq.can_queue += num;
42         spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
43 }
44
45 /* PCI_DMA_... to our direction translation.
46  */
47 static const u8 data_dir_flags[] = {
48         [PCI_DMA_BIDIRECTIONAL] = DATA_DIR_BYRECIPIENT, /* UNSPECIFIED */
49         [PCI_DMA_TODEVICE]      = DATA_DIR_OUT, /* OUTBOUND */
50         [PCI_DMA_FROMDEVICE]    = DATA_DIR_IN, /* INBOUND */
51         [PCI_DMA_NONE]          = DATA_DIR_NONE, /* NO TRANSFER */
52 };
53
54 static inline int asd_map_scatterlist(struct sas_task *task,
55                                       struct sg_el *sg_arr,
56                                       gfp_t gfp_flags)
57 {
58         struct asd_ascb *ascb = task->lldd_task;
59         struct asd_ha_struct *asd_ha = ascb->ha;
60         struct scatterlist *sc;
61         int num_sg, res;
62
63         if (task->data_dir == PCI_DMA_NONE)
64                 return 0;
65
66         if (task->num_scatter == 0) {
67                 void *p = task->scatter;
68                 dma_addr_t dma = pci_map_single(asd_ha->pcidev, p,
69                                                 task->total_xfer_len,
70                                                 task->data_dir);
71                 sg_arr[0].bus_addr = cpu_to_le64((u64)dma);
72                 sg_arr[0].size = cpu_to_le32(task->total_xfer_len);
73                 sg_arr[0].flags |= ASD_SG_EL_LIST_EOL;
74                 return 0;
75         }
76
77         /* STP tasks come from libata which has already mapped
78          * the SG list */
79         if (sas_protocol_ata(task->task_proto))
80                 num_sg = task->num_scatter;
81         else
82                 num_sg = pci_map_sg(asd_ha->pcidev, task->scatter,
83                                     task->num_scatter, task->data_dir);
84         if (num_sg == 0)
85                 return -ENOMEM;
86
87         if (num_sg > 3) {
88                 int i;
89
90                 ascb->sg_arr = asd_alloc_coherent(asd_ha,
91                                                   num_sg*sizeof(struct sg_el),
92                                                   gfp_flags);
93                 if (!ascb->sg_arr) {
94                         res = -ENOMEM;
95                         goto err_unmap;
96                 }
97                 for (sc = task->scatter, i = 0; i < num_sg; i++, sc++) {
98                         struct sg_el *sg =
99                                 &((struct sg_el *)ascb->sg_arr->vaddr)[i];
100                         sg->bus_addr = cpu_to_le64((u64)sg_dma_address(sc));
101                         sg->size = cpu_to_le32((u32)sg_dma_len(sc));
102                         if (i == num_sg-1)
103                                 sg->flags |= ASD_SG_EL_LIST_EOL;
104                 }
105
106                 for (sc = task->scatter, i = 0; i < 2; i++, sc++) {
107                         sg_arr[i].bus_addr =
108                                 cpu_to_le64((u64)sg_dma_address(sc));
109                         sg_arr[i].size = cpu_to_le32((u32)sg_dma_len(sc));
110                 }
111                 sg_arr[1].next_sg_offs = 2 * sizeof(*sg_arr);
112                 sg_arr[1].flags |= ASD_SG_EL_LIST_EOS;
113
114                 memset(&sg_arr[2], 0, sizeof(*sg_arr));
115                 sg_arr[2].bus_addr=cpu_to_le64((u64)ascb->sg_arr->dma_handle);
116         } else {
117                 int i;
118                 for (sc = task->scatter, i = 0; i < num_sg; i++, sc++) {
119                         sg_arr[i].bus_addr =
120                                 cpu_to_le64((u64)sg_dma_address(sc));
121                         sg_arr[i].size = cpu_to_le32((u32)sg_dma_len(sc));
122                 }
123                 sg_arr[i-1].flags |= ASD_SG_EL_LIST_EOL;
124         }
125
126         return 0;
127 err_unmap:
128         if (sas_protocol_ata(task->task_proto))
129                 pci_unmap_sg(asd_ha->pcidev, task->scatter, task->num_scatter,
130                              task->data_dir);
131         return res;
132 }
133
134 static inline void asd_unmap_scatterlist(struct asd_ascb *ascb)
135 {
136         struct asd_ha_struct *asd_ha = ascb->ha;
137         struct sas_task *task = ascb->uldd_task;
138
139         if (task->data_dir == PCI_DMA_NONE)
140                 return;
141
142         if (task->num_scatter == 0) {
143                 dma_addr_t dma = (dma_addr_t)
144                        le64_to_cpu(ascb->scb->ssp_task.sg_element[0].bus_addr);
145                 pci_unmap_single(ascb->ha->pcidev, dma, task->total_xfer_len,
146                                  task->data_dir);
147                 return;
148         }
149
150         asd_free_coherent(asd_ha, ascb->sg_arr);
151         if (task->task_proto != SAS_PROTOCOL_STP)
152                 pci_unmap_sg(asd_ha->pcidev, task->scatter, task->num_scatter,
153                              task->data_dir);
154 }
155
156 /* ---------- Task complete tasklet ---------- */
157
158 static void asd_get_response_tasklet(struct asd_ascb *ascb,
159                                      struct done_list_struct *dl)
160 {
161         struct asd_ha_struct *asd_ha = ascb->ha;
162         struct sas_task *task = ascb->uldd_task;
163         struct task_status_struct *ts = &task->task_status;
164         unsigned long flags;
165         struct tc_resp_sb_struct {
166                 __le16 index_escb;
167                 u8     len_lsb;
168                 u8     flags;
169         } __attribute__ ((packed)) *resp_sb = (void *) dl->status_block;
170
171 /*      int  size   = ((resp_sb->flags & 7) << 8) | resp_sb->len_lsb; */
172         int  edb_id = ((resp_sb->flags & 0x70) >> 4)-1;
173         struct asd_ascb *escb;
174         struct asd_dma_tok *edb;
175         void *r;
176
177         spin_lock_irqsave(&asd_ha->seq.tc_index_lock, flags);
178         escb = asd_tc_index_find(&asd_ha->seq,
179                                  (int)le16_to_cpu(resp_sb->index_escb));
180         spin_unlock_irqrestore(&asd_ha->seq.tc_index_lock, flags);
181
182         if (!escb) {
183                 ASD_DPRINTK("Uh-oh! No escb for this dl?!\n");
184                 return;
185         }
186
187         ts->buf_valid_size = 0;
188         edb = asd_ha->seq.edb_arr[edb_id + escb->edb_index];
189         r = edb->vaddr;
190         if (task->task_proto == SAS_PROTO_SSP) {
191                 struct ssp_response_iu *iu =
192                         r + 16 + sizeof(struct ssp_frame_hdr);
193
194                 ts->residual = le32_to_cpu(*(__le32 *)r);
195                 ts->resp = SAS_TASK_COMPLETE;
196                 if (iu->datapres == 0)
197                         ts->stat = iu->status;
198                 else if (iu->datapres == 1)
199                         ts->stat = iu->resp_data[3];
200                 else if (iu->datapres == 2) {
201                         ts->stat = SAM_CHECK_COND;
202                         ts->buf_valid_size = min((u32) SAS_STATUS_BUF_SIZE,
203                                          be32_to_cpu(iu->sense_data_len));
204                         memcpy(ts->buf, iu->sense_data, ts->buf_valid_size);
205                         if (iu->status != SAM_CHECK_COND) {
206                                 ASD_DPRINTK("device %llx sent sense data, but "
207                                             "stat(0x%x) is not CHECK_CONDITION"
208                                             "\n",
209                                             SAS_ADDR(task->dev->sas_addr),
210                                             iu->status);
211                         }
212                 }
213         }  else {
214                 struct ata_task_resp *resp = (void *) &ts->buf[0];
215
216                 ts->residual = le32_to_cpu(*(__le32 *)r);
217
218                 if (SAS_STATUS_BUF_SIZE >= sizeof(*resp)) {
219                         resp->frame_len = le16_to_cpu(*(__le16 *)(r+6));
220                         memcpy(&resp->ending_fis[0], r+16, 24);
221                         ts->buf_valid_size = sizeof(*resp);
222                 }
223         }
224
225         asd_invalidate_edb(escb, edb_id);
226 }
227
228 static void asd_task_tasklet_complete(struct asd_ascb *ascb,
229                                       struct done_list_struct *dl)
230 {
231         struct sas_task *task = ascb->uldd_task;
232         struct task_status_struct *ts = &task->task_status;
233         unsigned long flags;
234         u8 opcode = dl->opcode;
235
236         asd_can_dequeue(ascb->ha, 1);
237
238 Again:
239         switch (opcode) {
240         case TC_NO_ERROR:
241                 ts->resp = SAS_TASK_COMPLETE;
242                 ts->stat = SAM_GOOD;
243                 break;
244         case TC_UNDERRUN:
245                 ts->resp = SAS_TASK_COMPLETE;
246                 ts->stat = SAS_DATA_UNDERRUN;
247                 ts->residual = le32_to_cpu(*(__le32 *)dl->status_block);
248                 break;
249         case TC_OVERRUN:
250                 ts->resp = SAS_TASK_COMPLETE;
251                 ts->stat = SAS_DATA_OVERRUN;
252                 ts->residual = 0;
253                 break;
254         case TC_SSP_RESP:
255         case TC_ATA_RESP:
256                 ts->resp = SAS_TASK_COMPLETE;
257                 ts->stat = SAS_PROTO_RESPONSE;
258                 asd_get_response_tasklet(ascb, dl);
259                 break;
260         case TF_OPEN_REJECT:
261                 ts->resp = SAS_TASK_UNDELIVERED;
262                 ts->stat = SAS_OPEN_REJECT;
263                 if (dl->status_block[1] & 2)
264                         ts->open_rej_reason = 1 + dl->status_block[2];
265                 else if (dl->status_block[1] & 1)
266                         ts->open_rej_reason = (dl->status_block[2] >> 4)+10;
267                 else
268                         ts->open_rej_reason = SAS_OREJ_UNKNOWN;
269                 break;
270         case TF_OPEN_TO:
271                 ts->resp = SAS_TASK_UNDELIVERED;
272                 ts->stat = SAS_OPEN_TO;
273                 break;
274         case TF_PHY_DOWN:
275         case TU_PHY_DOWN:
276                 ts->resp = SAS_TASK_UNDELIVERED;
277                 ts->stat = SAS_PHY_DOWN;
278                 break;
279         case TI_PHY_DOWN:
280                 ts->resp = SAS_TASK_COMPLETE;
281                 ts->stat = SAS_PHY_DOWN;
282                 break;
283         case TI_BREAK:
284         case TI_PROTO_ERR:
285         case TI_NAK:
286         case TI_ACK_NAK_TO:
287         case TF_SMP_XMIT_RCV_ERR:
288         case TC_ATA_R_ERR_RECV:
289                 ts->resp = SAS_TASK_COMPLETE;
290                 ts->stat = SAS_INTERRUPTED;
291                 break;
292         case TF_BREAK:
293         case TU_BREAK:
294         case TU_ACK_NAK_TO:
295         case TF_SMPRSP_TO:
296                 ts->resp = SAS_TASK_UNDELIVERED;
297                 ts->stat = SAS_DEV_NO_RESPONSE;
298                 break;
299         case TF_NAK_RECV:
300                 ts->resp = SAS_TASK_COMPLETE;
301                 ts->stat = SAS_NAK_R_ERR;
302                 break;
303         case TA_I_T_NEXUS_LOSS:
304                 opcode = dl->status_block[0];
305                 goto Again;
306                 break;
307         case TF_INV_CONN_HANDLE:
308                 ts->resp = SAS_TASK_UNDELIVERED;
309                 ts->stat = SAS_DEVICE_UNKNOWN;
310                 break;
311         case TF_REQUESTED_N_PENDING:
312                 ts->resp = SAS_TASK_UNDELIVERED;
313                 ts->stat = SAS_PENDING;
314                 break;
315         case TC_TASK_CLEARED:
316         case TA_ON_REQ:
317                 ts->resp = SAS_TASK_COMPLETE;
318                 ts->stat = SAS_ABORTED_TASK;
319                 break;
320
321         case TF_NO_SMP_CONN:
322         case TF_TMF_NO_CTX:
323         case TF_TMF_NO_TAG:
324         case TF_TMF_TAG_FREE:
325         case TF_TMF_TASK_DONE:
326         case TF_TMF_NO_CONN_HANDLE:
327         case TF_IRTT_TO:
328         case TF_IU_SHORT:
329         case TF_DATA_OFFS_ERR:
330                 ts->resp = SAS_TASK_UNDELIVERED;
331                 ts->stat = SAS_DEV_NO_RESPONSE;
332                 break;
333
334         case TC_LINK_ADM_RESP:
335         case TC_CONTROL_PHY:
336         case TC_RESUME:
337         case TC_PARTIAL_SG_LIST:
338         default:
339                 ASD_DPRINTK("%s: dl opcode: 0x%x?\n", __FUNCTION__, opcode);
340                 break;
341         }
342
343         switch (task->task_proto) {
344         case SATA_PROTO:
345         case SAS_PROTO_STP:
346                 asd_unbuild_ata_ascb(ascb);
347                 break;
348         case SAS_PROTO_SMP:
349                 asd_unbuild_smp_ascb(ascb);
350                 break;
351         case SAS_PROTO_SSP:
352                 asd_unbuild_ssp_ascb(ascb);
353         default:
354                 break;
355         }
356
357         spin_lock_irqsave(&task->task_state_lock, flags);
358         task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
359         task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
360         task->task_state_flags |= SAS_TASK_STATE_DONE;
361         if (unlikely((task->task_state_flags & SAS_TASK_STATE_ABORTED))) {
362                 spin_unlock_irqrestore(&task->task_state_lock, flags);
363                 ASD_DPRINTK("task 0x%p done with opcode 0x%x resp 0x%x "
364                             "stat 0x%x but aborted by upper layer!\n",
365                             task, opcode, ts->resp, ts->stat);
366                 complete(&ascb->completion);
367         } else {
368                 spin_unlock_irqrestore(&task->task_state_lock, flags);
369                 task->lldd_task = NULL;
370                 asd_ascb_free(ascb);
371                 mb();
372                 task->task_done(task);
373         }
374 }
375
376 /* ---------- ATA ---------- */
377
378 static int asd_build_ata_ascb(struct asd_ascb *ascb, struct sas_task *task,
379                               gfp_t gfp_flags)
380 {
381         struct domain_device *dev = task->dev;
382         struct scb *scb;
383         u8     flags;
384         int    res = 0;
385
386         scb = ascb->scb;
387
388         if (unlikely(task->ata_task.device_control_reg_update))
389                 scb->header.opcode = CONTROL_ATA_DEV;
390         else if (dev->sata_dev.command_set == ATA_COMMAND_SET)
391                 scb->header.opcode = INITIATE_ATA_TASK;
392         else
393                 scb->header.opcode = INITIATE_ATAPI_TASK;
394
395         scb->ata_task.proto_conn_rate = (1 << 5); /* STP */
396         if (dev->port->oob_mode == SAS_OOB_MODE)
397                 scb->ata_task.proto_conn_rate |= dev->linkrate;
398
399         scb->ata_task.total_xfer_len = cpu_to_le32(task->total_xfer_len);
400         scb->ata_task.fis = task->ata_task.fis;
401         if (likely(!task->ata_task.device_control_reg_update))
402                 scb->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
403         scb->ata_task.fis.flags &= 0xF0; /* PM_PORT field shall be 0 */
404         if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
405                 memcpy(scb->ata_task.atapi_packet, task->ata_task.atapi_packet,
406                        16);
407         scb->ata_task.sister_scb = cpu_to_le16(0xFFFF);
408         scb->ata_task.conn_handle = cpu_to_le16(
409                 (u16)(unsigned long)dev->lldd_dev);
410
411         if (likely(!task->ata_task.device_control_reg_update)) {
412                 flags = 0;
413                 if (task->ata_task.dma_xfer)
414                         flags |= DATA_XFER_MODE_DMA;
415                 if (task->ata_task.use_ncq &&
416                     dev->sata_dev.command_set != ATAPI_COMMAND_SET)
417                         flags |= ATA_Q_TYPE_NCQ;
418                 flags |= data_dir_flags[task->data_dir];
419                 scb->ata_task.ata_flags = flags;
420
421                 scb->ata_task.retry_count = task->ata_task.retry_count;
422
423                 flags = 0;
424                 if (task->ata_task.set_affil_pol)
425                         flags |= SET_AFFIL_POLICY;
426                 if (task->ata_task.stp_affil_pol)
427                         flags |= STP_AFFIL_POLICY;
428                 scb->ata_task.flags = flags;
429         }
430         ascb->tasklet_complete = asd_task_tasklet_complete;
431
432         if (likely(!task->ata_task.device_control_reg_update))
433                 res = asd_map_scatterlist(task, scb->ata_task.sg_element,
434                                           gfp_flags);
435
436         return res;
437 }
438
439 static void asd_unbuild_ata_ascb(struct asd_ascb *a)
440 {
441         asd_unmap_scatterlist(a);
442 }
443
444 /* ---------- SMP ---------- */
445
446 static int asd_build_smp_ascb(struct asd_ascb *ascb, struct sas_task *task,
447                               gfp_t gfp_flags)
448 {
449         struct asd_ha_struct *asd_ha = ascb->ha;
450         struct domain_device *dev = task->dev;
451         struct scb *scb;
452
453         pci_map_sg(asd_ha->pcidev, &task->smp_task.smp_req, 1,
454                    PCI_DMA_TODEVICE);
455         pci_map_sg(asd_ha->pcidev, &task->smp_task.smp_resp, 1,
456                    PCI_DMA_FROMDEVICE);
457
458         scb = ascb->scb;
459
460         scb->header.opcode = INITIATE_SMP_TASK;
461
462         scb->smp_task.proto_conn_rate = dev->linkrate;
463
464         scb->smp_task.smp_req.bus_addr =
465                 cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_req));
466         scb->smp_task.smp_req.size =
467                 cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_req)-4);
468
469         scb->smp_task.smp_resp.bus_addr =
470                 cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_resp));
471         scb->smp_task.smp_resp.size =
472                 cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_resp)-4);
473
474         scb->smp_task.sister_scb = cpu_to_le16(0xFFFF);
475         scb->smp_task.conn_handle = cpu_to_le16((u16)
476                                                 (unsigned long)dev->lldd_dev);
477
478         ascb->tasklet_complete = asd_task_tasklet_complete;
479
480         return 0;
481 }
482
483 static void asd_unbuild_smp_ascb(struct asd_ascb *a)
484 {
485         struct sas_task *task = a->uldd_task;
486
487         BUG_ON(!task);
488         pci_unmap_sg(a->ha->pcidev, &task->smp_task.smp_req, 1,
489                      PCI_DMA_TODEVICE);
490         pci_unmap_sg(a->ha->pcidev, &task->smp_task.smp_resp, 1,
491                      PCI_DMA_FROMDEVICE);
492 }
493
494 /* ---------- SSP ---------- */
495
496 static int asd_build_ssp_ascb(struct asd_ascb *ascb, struct sas_task *task,
497                               gfp_t gfp_flags)
498 {
499         struct domain_device *dev = task->dev;
500         struct scb *scb;
501         int    res = 0;
502
503         scb = ascb->scb;
504
505         scb->header.opcode = INITIATE_SSP_TASK;
506
507         scb->ssp_task.proto_conn_rate  = (1 << 4); /* SSP */
508         scb->ssp_task.proto_conn_rate |= dev->linkrate;
509         scb->ssp_task.total_xfer_len = cpu_to_le32(task->total_xfer_len);
510         scb->ssp_task.ssp_frame.frame_type = SSP_DATA;
511         memcpy(scb->ssp_task.ssp_frame.hashed_dest_addr, dev->hashed_sas_addr,
512                HASHED_SAS_ADDR_SIZE);
513         memcpy(scb->ssp_task.ssp_frame.hashed_src_addr,
514                dev->port->ha->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
515         scb->ssp_task.ssp_frame.tptt = cpu_to_be16(0xFFFF);
516
517         memcpy(scb->ssp_task.ssp_cmd.lun, task->ssp_task.LUN, 8);
518         if (task->ssp_task.enable_first_burst)
519                 scb->ssp_task.ssp_cmd.efb_prio_attr |= EFB_MASK;
520         scb->ssp_task.ssp_cmd.efb_prio_attr |= (task->ssp_task.task_prio << 3);
521         scb->ssp_task.ssp_cmd.efb_prio_attr |= (task->ssp_task.task_attr & 7);
522         memcpy(scb->ssp_task.ssp_cmd.cdb, task->ssp_task.cdb, 16);
523
524         scb->ssp_task.sister_scb = cpu_to_le16(0xFFFF);
525         scb->ssp_task.conn_handle = cpu_to_le16(
526                 (u16)(unsigned long)dev->lldd_dev);
527         scb->ssp_task.data_dir = data_dir_flags[task->data_dir];
528         scb->ssp_task.retry_count = scb->ssp_task.retry_count;
529
530         ascb->tasklet_complete = asd_task_tasklet_complete;
531
532         res = asd_map_scatterlist(task, scb->ssp_task.sg_element, gfp_flags);
533
534         return res;
535 }
536
537 static void asd_unbuild_ssp_ascb(struct asd_ascb *a)
538 {
539         asd_unmap_scatterlist(a);
540 }
541
542 /* ---------- Execute Task ---------- */
543
544 static inline int asd_can_queue(struct asd_ha_struct *asd_ha, int num)
545 {
546         int res = 0;
547         unsigned long flags;
548
549         spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
550         if ((asd_ha->seq.can_queue - num) < 0)
551                 res = -SAS_QUEUE_FULL;
552         else
553                 asd_ha->seq.can_queue -= num;
554         spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
555
556         return res;
557 }
558
559 int asd_execute_task(struct sas_task *task, const int num,
560                      gfp_t gfp_flags)
561 {
562         int res = 0;
563         LIST_HEAD(alist);
564         struct sas_task *t = task;
565         struct asd_ascb *ascb = NULL, *a;
566         struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha;
567         unsigned long flags;
568
569         res = asd_can_queue(asd_ha, num);
570         if (res)
571                 return res;
572
573         res = num;
574         ascb = asd_ascb_alloc_list(asd_ha, &res, gfp_flags);
575         if (res) {
576                 res = -ENOMEM;
577                 goto out_err;
578         }
579
580         __list_add(&alist, ascb->list.prev, &ascb->list);
581         list_for_each_entry(a, &alist, list) {
582                 a->uldd_task = t;
583                 t->lldd_task = a;
584                 t = list_entry(t->list.next, struct sas_task, list);
585         }
586         list_for_each_entry(a, &alist, list) {
587                 t = a->uldd_task;
588                 a->uldd_timer = 1;
589                 if (t->task_proto & SAS_PROTO_STP)
590                         t->task_proto = SAS_PROTO_STP;
591                 switch (t->task_proto) {
592                 case SATA_PROTO:
593                 case SAS_PROTO_STP:
594                         res = asd_build_ata_ascb(a, t, gfp_flags);
595                         break;
596                 case SAS_PROTO_SMP:
597                         res = asd_build_smp_ascb(a, t, gfp_flags);
598                         break;
599                 case SAS_PROTO_SSP:
600                         res = asd_build_ssp_ascb(a, t, gfp_flags);
601                         break;
602                 default:
603                         asd_printk("unknown sas_task proto: 0x%x\n",
604                                    t->task_proto);
605                         res = -ENOMEM;
606                         break;
607                 }
608                 if (res)
609                         goto out_err_unmap;
610
611                 spin_lock_irqsave(&t->task_state_lock, flags);
612                 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
613                 spin_unlock_irqrestore(&t->task_state_lock, flags);
614         }
615         list_del_init(&alist);
616
617         res = asd_post_ascb_list(asd_ha, ascb, num);
618         if (unlikely(res)) {
619                 a = NULL;
620                 __list_add(&alist, ascb->list.prev, &ascb->list);
621                 goto out_err_unmap;
622         }
623
624         return 0;
625 out_err_unmap:
626         {
627                 struct asd_ascb *b = a;
628                 list_for_each_entry(a, &alist, list) {
629                         if (a == b)
630                                 break;
631                         t = a->uldd_task;
632                         spin_lock_irqsave(&t->task_state_lock, flags);
633                         t->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
634                         spin_unlock_irqrestore(&t->task_state_lock, flags);
635                         switch (t->task_proto) {
636                         case SATA_PROTO:
637                         case SAS_PROTO_STP:
638                                 asd_unbuild_ata_ascb(a);
639                                 break;
640                         case SAS_PROTO_SMP:
641                                 asd_unbuild_smp_ascb(a);
642                                 break;
643                         case SAS_PROTO_SSP:
644                                 asd_unbuild_ssp_ascb(a);
645                         default:
646                                 break;
647                         }
648                         t->lldd_task = NULL;
649                 }
650         }
651         list_del_init(&alist);
652 out_err:
653         if (ascb)
654                 asd_ascb_free_list(ascb);
655         asd_can_dequeue(asd_ha, num);
656         return res;
657 }