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