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