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