• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
asd_can_dequeue(struct asd_ha_struct * asd_ha,int num)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 
asd_map_scatterlist(struct sas_task * task,struct sg_el * sg_arr,gfp_t gfp_flags)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 
asd_unmap_scatterlist(struct asd_ascb * ascb)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 
asd_get_response_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl)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 
asd_task_tasklet_complete(struct asd_ascb * ascb,struct done_list_struct * dl)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_AT_INITIATOR;
329 	task->task_state_flags |= SAS_TASK_STATE_DONE;
330 	if (unlikely((task->task_state_flags & SAS_TASK_STATE_ABORTED))) {
331 		struct completion *completion = ascb->completion;
332 		spin_unlock_irqrestore(&task->task_state_lock, flags);
333 		ASD_DPRINTK("task 0x%p done with opcode 0x%x resp 0x%x "
334 			    "stat 0x%x but aborted by upper layer!\n",
335 			    task, opcode, ts->resp, ts->stat);
336 		if (completion)
337 			complete(completion);
338 	} else {
339 		spin_unlock_irqrestore(&task->task_state_lock, flags);
340 		task->lldd_task = NULL;
341 		asd_ascb_free(ascb);
342 		mb();
343 		task->task_done(task);
344 	}
345 }
346 
347 /* ---------- ATA ---------- */
348 
asd_build_ata_ascb(struct asd_ascb * ascb,struct sas_task * task,gfp_t gfp_flags)349 static int asd_build_ata_ascb(struct asd_ascb *ascb, struct sas_task *task,
350 			      gfp_t gfp_flags)
351 {
352 	struct domain_device *dev = task->dev;
353 	struct scb *scb;
354 	u8     flags;
355 	int    res = 0;
356 
357 	scb = ascb->scb;
358 
359 	if (unlikely(task->ata_task.device_control_reg_update))
360 		scb->header.opcode = CONTROL_ATA_DEV;
361 	else if (dev->sata_dev.class == ATA_DEV_ATAPI)
362 		scb->header.opcode = INITIATE_ATAPI_TASK;
363 	else
364 		scb->header.opcode = INITIATE_ATA_TASK;
365 
366 	scb->ata_task.proto_conn_rate = (1 << 5); /* STP */
367 	if (dev->port->oob_mode == SAS_OOB_MODE)
368 		scb->ata_task.proto_conn_rate |= dev->linkrate;
369 
370 	scb->ata_task.total_xfer_len = cpu_to_le32(task->total_xfer_len);
371 	scb->ata_task.fis = task->ata_task.fis;
372 	if (likely(!task->ata_task.device_control_reg_update))
373 		scb->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
374 	scb->ata_task.fis.flags &= 0xF0; /* PM_PORT field shall be 0 */
375 	if (dev->sata_dev.class == ATA_DEV_ATAPI)
376 		memcpy(scb->ata_task.atapi_packet, task->ata_task.atapi_packet,
377 		       16);
378 	scb->ata_task.sister_scb = cpu_to_le16(0xFFFF);
379 	scb->ata_task.conn_handle = cpu_to_le16(
380 		(u16)(unsigned long)dev->lldd_dev);
381 
382 	if (likely(!task->ata_task.device_control_reg_update)) {
383 		flags = 0;
384 		if (task->ata_task.dma_xfer)
385 			flags |= DATA_XFER_MODE_DMA;
386 		if (task->ata_task.use_ncq &&
387 		    dev->sata_dev.class != ATA_DEV_ATAPI)
388 			flags |= ATA_Q_TYPE_NCQ;
389 		flags |= data_dir_flags[task->data_dir];
390 		scb->ata_task.ata_flags = flags;
391 
392 		scb->ata_task.retry_count = task->ata_task.retry_count;
393 
394 		flags = 0;
395 		if (task->ata_task.set_affil_pol)
396 			flags |= SET_AFFIL_POLICY;
397 		if (task->ata_task.stp_affil_pol)
398 			flags |= STP_AFFIL_POLICY;
399 		scb->ata_task.flags = flags;
400 	}
401 	ascb->tasklet_complete = asd_task_tasklet_complete;
402 
403 	if (likely(!task->ata_task.device_control_reg_update))
404 		res = asd_map_scatterlist(task, scb->ata_task.sg_element,
405 					  gfp_flags);
406 
407 	return res;
408 }
409 
asd_unbuild_ata_ascb(struct asd_ascb * a)410 static void asd_unbuild_ata_ascb(struct asd_ascb *a)
411 {
412 	asd_unmap_scatterlist(a);
413 }
414 
415 /* ---------- SMP ---------- */
416 
asd_build_smp_ascb(struct asd_ascb * ascb,struct sas_task * task,gfp_t gfp_flags)417 static int asd_build_smp_ascb(struct asd_ascb *ascb, struct sas_task *task,
418 			      gfp_t gfp_flags)
419 {
420 	struct asd_ha_struct *asd_ha = ascb->ha;
421 	struct domain_device *dev = task->dev;
422 	struct scb *scb;
423 
424 	dma_map_sg(&asd_ha->pcidev->dev, &task->smp_task.smp_req, 1,
425 		   DMA_TO_DEVICE);
426 	dma_map_sg(&asd_ha->pcidev->dev, &task->smp_task.smp_resp, 1,
427 		   DMA_FROM_DEVICE);
428 
429 	scb = ascb->scb;
430 
431 	scb->header.opcode = INITIATE_SMP_TASK;
432 
433 	scb->smp_task.proto_conn_rate = dev->linkrate;
434 
435 	scb->smp_task.smp_req.bus_addr =
436 		cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_req));
437 	scb->smp_task.smp_req.size =
438 		cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_req)-4);
439 
440 	scb->smp_task.smp_resp.bus_addr =
441 		cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_resp));
442 	scb->smp_task.smp_resp.size =
443 		cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_resp)-4);
444 
445 	scb->smp_task.sister_scb = cpu_to_le16(0xFFFF);
446 	scb->smp_task.conn_handle = cpu_to_le16((u16)
447 						(unsigned long)dev->lldd_dev);
448 
449 	ascb->tasklet_complete = asd_task_tasklet_complete;
450 
451 	return 0;
452 }
453 
asd_unbuild_smp_ascb(struct asd_ascb * a)454 static void asd_unbuild_smp_ascb(struct asd_ascb *a)
455 {
456 	struct sas_task *task = a->uldd_task;
457 
458 	BUG_ON(!task);
459 	dma_unmap_sg(&a->ha->pcidev->dev, &task->smp_task.smp_req, 1,
460 		     DMA_TO_DEVICE);
461 	dma_unmap_sg(&a->ha->pcidev->dev, &task->smp_task.smp_resp, 1,
462 		     DMA_FROM_DEVICE);
463 }
464 
465 /* ---------- SSP ---------- */
466 
asd_build_ssp_ascb(struct asd_ascb * ascb,struct sas_task * task,gfp_t gfp_flags)467 static int asd_build_ssp_ascb(struct asd_ascb *ascb, struct sas_task *task,
468 			      gfp_t gfp_flags)
469 {
470 	struct domain_device *dev = task->dev;
471 	struct scb *scb;
472 	int    res = 0;
473 
474 	scb = ascb->scb;
475 
476 	scb->header.opcode = INITIATE_SSP_TASK;
477 
478 	scb->ssp_task.proto_conn_rate  = (1 << 4); /* SSP */
479 	scb->ssp_task.proto_conn_rate |= dev->linkrate;
480 	scb->ssp_task.total_xfer_len = cpu_to_le32(task->total_xfer_len);
481 	scb->ssp_task.ssp_frame.frame_type = SSP_DATA;
482 	memcpy(scb->ssp_task.ssp_frame.hashed_dest_addr, dev->hashed_sas_addr,
483 	       HASHED_SAS_ADDR_SIZE);
484 	memcpy(scb->ssp_task.ssp_frame.hashed_src_addr,
485 	       dev->port->ha->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
486 	scb->ssp_task.ssp_frame.tptt = cpu_to_be16(0xFFFF);
487 
488 	memcpy(scb->ssp_task.ssp_cmd.lun, task->ssp_task.LUN, 8);
489 	if (task->ssp_task.enable_first_burst)
490 		scb->ssp_task.ssp_cmd.efb_prio_attr |= EFB_MASK;
491 	scb->ssp_task.ssp_cmd.efb_prio_attr |= (task->ssp_task.task_prio << 3);
492 	scb->ssp_task.ssp_cmd.efb_prio_attr |= (task->ssp_task.task_attr & 7);
493 	memcpy(scb->ssp_task.ssp_cmd.cdb, task->ssp_task.cmd->cmnd,
494 	       task->ssp_task.cmd->cmd_len);
495 
496 	scb->ssp_task.sister_scb = cpu_to_le16(0xFFFF);
497 	scb->ssp_task.conn_handle = cpu_to_le16(
498 		(u16)(unsigned long)dev->lldd_dev);
499 	scb->ssp_task.data_dir = data_dir_flags[task->data_dir];
500 	scb->ssp_task.retry_count = scb->ssp_task.retry_count;
501 
502 	ascb->tasklet_complete = asd_task_tasklet_complete;
503 
504 	res = asd_map_scatterlist(task, scb->ssp_task.sg_element, gfp_flags);
505 
506 	return res;
507 }
508 
asd_unbuild_ssp_ascb(struct asd_ascb * a)509 static void asd_unbuild_ssp_ascb(struct asd_ascb *a)
510 {
511 	asd_unmap_scatterlist(a);
512 }
513 
514 /* ---------- Execute Task ---------- */
515 
asd_can_queue(struct asd_ha_struct * asd_ha,int num)516 static int asd_can_queue(struct asd_ha_struct *asd_ha, int num)
517 {
518 	int res = 0;
519 	unsigned long flags;
520 
521 	spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
522 	if ((asd_ha->seq.can_queue - num) < 0)
523 		res = -SAS_QUEUE_FULL;
524 	else
525 		asd_ha->seq.can_queue -= num;
526 	spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
527 
528 	return res;
529 }
530 
asd_execute_task(struct sas_task * task,gfp_t gfp_flags)531 int asd_execute_task(struct sas_task *task, gfp_t gfp_flags)
532 {
533 	int res = 0;
534 	LIST_HEAD(alist);
535 	struct sas_task *t = task;
536 	struct asd_ascb *ascb = NULL, *a;
537 	struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha;
538 	unsigned long flags;
539 
540 	res = asd_can_queue(asd_ha, 1);
541 	if (res)
542 		return res;
543 
544 	res = 1;
545 	ascb = asd_ascb_alloc_list(asd_ha, &res, gfp_flags);
546 	if (res) {
547 		res = -ENOMEM;
548 		goto out_err;
549 	}
550 
551 	__list_add(&alist, ascb->list.prev, &ascb->list);
552 	list_for_each_entry(a, &alist, list) {
553 		a->uldd_task = t;
554 		t->lldd_task = a;
555 		break;
556 	}
557 	list_for_each_entry(a, &alist, list) {
558 		t = a->uldd_task;
559 		a->uldd_timer = 1;
560 		if (t->task_proto & SAS_PROTOCOL_STP)
561 			t->task_proto = SAS_PROTOCOL_STP;
562 		switch (t->task_proto) {
563 		case SAS_PROTOCOL_SATA:
564 		case SAS_PROTOCOL_STP:
565 			res = asd_build_ata_ascb(a, t, gfp_flags);
566 			break;
567 		case SAS_PROTOCOL_SMP:
568 			res = asd_build_smp_ascb(a, t, gfp_flags);
569 			break;
570 		case SAS_PROTOCOL_SSP:
571 			res = asd_build_ssp_ascb(a, t, gfp_flags);
572 			break;
573 		default:
574 			asd_printk("unknown sas_task proto: 0x%x\n",
575 				   t->task_proto);
576 			res = -ENOMEM;
577 			break;
578 		}
579 		if (res)
580 			goto out_err_unmap;
581 
582 		spin_lock_irqsave(&t->task_state_lock, flags);
583 		t->task_state_flags |= SAS_TASK_AT_INITIATOR;
584 		spin_unlock_irqrestore(&t->task_state_lock, flags);
585 	}
586 	list_del_init(&alist);
587 
588 	res = asd_post_ascb_list(asd_ha, ascb, 1);
589 	if (unlikely(res)) {
590 		a = NULL;
591 		__list_add(&alist, ascb->list.prev, &ascb->list);
592 		goto out_err_unmap;
593 	}
594 
595 	return 0;
596 out_err_unmap:
597 	{
598 		struct asd_ascb *b = a;
599 		list_for_each_entry(a, &alist, list) {
600 			if (a == b)
601 				break;
602 			t = a->uldd_task;
603 			spin_lock_irqsave(&t->task_state_lock, flags);
604 			t->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
605 			spin_unlock_irqrestore(&t->task_state_lock, flags);
606 			switch (t->task_proto) {
607 			case SAS_PROTOCOL_SATA:
608 			case SAS_PROTOCOL_STP:
609 				asd_unbuild_ata_ascb(a);
610 				break;
611 			case SAS_PROTOCOL_SMP:
612 				asd_unbuild_smp_ascb(a);
613 				break;
614 			case SAS_PROTOCOL_SSP:
615 				asd_unbuild_ssp_ascb(a);
616 				break;
617 			default:
618 				break;
619 			}
620 			t->lldd_task = NULL;
621 		}
622 	}
623 	list_del_init(&alist);
624 out_err:
625 	if (ascb)
626 		asd_ascb_free_list(ascb);
627 	asd_can_dequeue(asd_ha, 1);
628 	return res;
629 }
630