aic94xx_task.c 17 KB

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