aic94xx_tmf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * Aic94xx Task Management Functions
  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. /* ---------- Internal enqueue ---------- */
  31. static int asd_enqueue_internal(struct asd_ascb *ascb,
  32. void (*tasklet_complete)(struct asd_ascb *,
  33. struct done_list_struct *),
  34. void (*timed_out)(unsigned long))
  35. {
  36. int res;
  37. ascb->tasklet_complete = tasklet_complete;
  38. ascb->uldd_timer = 1;
  39. ascb->timer.data = (unsigned long) ascb;
  40. ascb->timer.function = timed_out;
  41. ascb->timer.expires = jiffies + AIC94XX_SCB_TIMEOUT;
  42. add_timer(&ascb->timer);
  43. res = asd_post_ascb_list(ascb->ha, ascb, 1);
  44. if (unlikely(res))
  45. del_timer(&ascb->timer);
  46. return res;
  47. }
  48. /* ---------- CLEAR NEXUS ---------- */
  49. struct tasklet_completion_status {
  50. int dl_opcode;
  51. int tmf_state;
  52. u8 tag_valid:1;
  53. __be16 tag;
  54. };
  55. #define DECLARE_TCS(tcs) \
  56. struct tasklet_completion_status tcs = { \
  57. .dl_opcode = 0, \
  58. .tmf_state = 0, \
  59. .tag_valid = 0, \
  60. .tag = 0, \
  61. }
  62. static void asd_clear_nexus_tasklet_complete(struct asd_ascb *ascb,
  63. struct done_list_struct *dl)
  64. {
  65. struct tasklet_completion_status *tcs = ascb->uldd_task;
  66. ASD_DPRINTK("%s: here\n", __func__);
  67. if (!del_timer(&ascb->timer)) {
  68. ASD_DPRINTK("%s: couldn't delete timer\n", __func__);
  69. return;
  70. }
  71. ASD_DPRINTK("%s: opcode: 0x%x\n", __func__, dl->opcode);
  72. tcs->dl_opcode = dl->opcode;
  73. complete(ascb->completion);
  74. asd_ascb_free(ascb);
  75. }
  76. static void asd_clear_nexus_timedout(unsigned long data)
  77. {
  78. struct asd_ascb *ascb = (void *)data;
  79. struct tasklet_completion_status *tcs = ascb->uldd_task;
  80. ASD_DPRINTK("%s: here\n", __func__);
  81. tcs->dl_opcode = TMF_RESP_FUNC_FAILED;
  82. complete(ascb->completion);
  83. }
  84. #define CLEAR_NEXUS_PRE \
  85. struct asd_ascb *ascb; \
  86. struct scb *scb; \
  87. int res; \
  88. DECLARE_COMPLETION_ONSTACK(completion); \
  89. DECLARE_TCS(tcs); \
  90. \
  91. ASD_DPRINTK("%s: PRE\n", __func__); \
  92. res = 1; \
  93. ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL); \
  94. if (!ascb) \
  95. return -ENOMEM; \
  96. \
  97. ascb->completion = &completion; \
  98. ascb->uldd_task = &tcs; \
  99. scb = ascb->scb; \
  100. scb->header.opcode = CLEAR_NEXUS
  101. #define CLEAR_NEXUS_POST \
  102. ASD_DPRINTK("%s: POST\n", __func__); \
  103. res = asd_enqueue_internal(ascb, asd_clear_nexus_tasklet_complete, \
  104. asd_clear_nexus_timedout); \
  105. if (res) \
  106. goto out_err; \
  107. ASD_DPRINTK("%s: clear nexus posted, waiting...\n", __func__); \
  108. wait_for_completion(&completion); \
  109. res = tcs.dl_opcode; \
  110. if (res == TC_NO_ERROR) \
  111. res = TMF_RESP_FUNC_COMPLETE; \
  112. return res; \
  113. out_err: \
  114. asd_ascb_free(ascb); \
  115. return res
  116. int asd_clear_nexus_ha(struct sas_ha_struct *sas_ha)
  117. {
  118. struct asd_ha_struct *asd_ha = sas_ha->lldd_ha;
  119. CLEAR_NEXUS_PRE;
  120. scb->clear_nexus.nexus = NEXUS_ADAPTER;
  121. CLEAR_NEXUS_POST;
  122. }
  123. int asd_clear_nexus_port(struct asd_sas_port *port)
  124. {
  125. struct asd_ha_struct *asd_ha = port->ha->lldd_ha;
  126. CLEAR_NEXUS_PRE;
  127. scb->clear_nexus.nexus = NEXUS_PORT;
  128. scb->clear_nexus.conn_mask = port->phy_mask;
  129. CLEAR_NEXUS_POST;
  130. }
  131. enum clear_nexus_phase {
  132. NEXUS_PHASE_PRE,
  133. NEXUS_PHASE_POST,
  134. NEXUS_PHASE_RESUME,
  135. };
  136. static int asd_clear_nexus_I_T(struct domain_device *dev,
  137. enum clear_nexus_phase phase)
  138. {
  139. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  140. CLEAR_NEXUS_PRE;
  141. scb->clear_nexus.nexus = NEXUS_I_T;
  142. switch (phase) {
  143. case NEXUS_PHASE_PRE:
  144. scb->clear_nexus.flags = EXEC_Q | SUSPEND_TX;
  145. break;
  146. case NEXUS_PHASE_POST:
  147. scb->clear_nexus.flags = SEND_Q | NOTINQ;
  148. break;
  149. case NEXUS_PHASE_RESUME:
  150. scb->clear_nexus.flags = RESUME_TX;
  151. }
  152. scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long)
  153. dev->lldd_dev);
  154. CLEAR_NEXUS_POST;
  155. }
  156. int asd_I_T_nexus_reset(struct domain_device *dev)
  157. {
  158. int res, tmp_res, i;
  159. struct sas_phy *phy = sas_find_local_phy(dev);
  160. /* Standard mandates link reset for ATA (type 0) and
  161. * hard reset for SSP (type 1) */
  162. int reset_type = (dev->dev_type == SATA_DEV ||
  163. (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
  164. asd_clear_nexus_I_T(dev, NEXUS_PHASE_PRE);
  165. /* send a hard reset */
  166. ASD_DPRINTK("sending %s reset to %s\n",
  167. reset_type ? "hard" : "soft", phy->dev.bus_id);
  168. res = sas_phy_reset(phy, reset_type);
  169. if (res == TMF_RESP_FUNC_COMPLETE) {
  170. /* wait for the maximum settle time */
  171. msleep(500);
  172. /* clear all outstanding commands (keep nexus suspended) */
  173. asd_clear_nexus_I_T(dev, NEXUS_PHASE_POST);
  174. }
  175. for (i = 0 ; i < 3; i++) {
  176. tmp_res = asd_clear_nexus_I_T(dev, NEXUS_PHASE_RESUME);
  177. if (tmp_res == TC_RESUME)
  178. return res;
  179. msleep(500);
  180. }
  181. /* This is a bit of a problem: the sequencer is still suspended
  182. * and is refusing to resume. Hope it will resume on a bigger hammer
  183. * or the disk is lost */
  184. dev_printk(KERN_ERR, &phy->dev,
  185. "Failed to resume nexus after reset 0x%x\n", tmp_res);
  186. return TMF_RESP_FUNC_FAILED;
  187. }
  188. static int asd_clear_nexus_I_T_L(struct domain_device *dev, u8 *lun)
  189. {
  190. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  191. CLEAR_NEXUS_PRE;
  192. scb->clear_nexus.nexus = NEXUS_I_T_L;
  193. scb->clear_nexus.flags = SEND_Q | EXEC_Q | NOTINQ;
  194. memcpy(scb->clear_nexus.ssp_task.lun, lun, 8);
  195. scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long)
  196. dev->lldd_dev);
  197. CLEAR_NEXUS_POST;
  198. }
  199. static int asd_clear_nexus_tag(struct sas_task *task)
  200. {
  201. struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha;
  202. struct asd_ascb *tascb = task->lldd_task;
  203. CLEAR_NEXUS_PRE;
  204. scb->clear_nexus.nexus = NEXUS_TAG;
  205. memcpy(scb->clear_nexus.ssp_task.lun, task->ssp_task.LUN, 8);
  206. scb->clear_nexus.ssp_task.tag = tascb->tag;
  207. if (task->dev->tproto)
  208. scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long)
  209. task->dev->lldd_dev);
  210. CLEAR_NEXUS_POST;
  211. }
  212. static int asd_clear_nexus_index(struct sas_task *task)
  213. {
  214. struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha;
  215. struct asd_ascb *tascb = task->lldd_task;
  216. CLEAR_NEXUS_PRE;
  217. scb->clear_nexus.nexus = NEXUS_TRANS_CX;
  218. if (task->dev->tproto)
  219. scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long)
  220. task->dev->lldd_dev);
  221. scb->clear_nexus.index = cpu_to_le16(tascb->tc_index);
  222. CLEAR_NEXUS_POST;
  223. }
  224. /* ---------- TMFs ---------- */
  225. static void asd_tmf_timedout(unsigned long data)
  226. {
  227. struct asd_ascb *ascb = (void *) data;
  228. struct tasklet_completion_status *tcs = ascb->uldd_task;
  229. ASD_DPRINTK("tmf timed out\n");
  230. tcs->tmf_state = TMF_RESP_FUNC_FAILED;
  231. complete(ascb->completion);
  232. }
  233. static int asd_get_tmf_resp_tasklet(struct asd_ascb *ascb,
  234. struct done_list_struct *dl)
  235. {
  236. struct asd_ha_struct *asd_ha = ascb->ha;
  237. unsigned long flags;
  238. struct tc_resp_sb_struct {
  239. __le16 index_escb;
  240. u8 len_lsb;
  241. u8 flags;
  242. } __attribute__ ((packed)) *resp_sb = (void *) dl->status_block;
  243. int edb_id = ((resp_sb->flags & 0x70) >> 4)-1;
  244. struct asd_ascb *escb;
  245. struct asd_dma_tok *edb;
  246. struct ssp_frame_hdr *fh;
  247. struct ssp_response_iu *ru;
  248. int res = TMF_RESP_FUNC_FAILED;
  249. ASD_DPRINTK("tmf resp tasklet\n");
  250. spin_lock_irqsave(&asd_ha->seq.tc_index_lock, flags);
  251. escb = asd_tc_index_find(&asd_ha->seq,
  252. (int)le16_to_cpu(resp_sb->index_escb));
  253. spin_unlock_irqrestore(&asd_ha->seq.tc_index_lock, flags);
  254. if (!escb) {
  255. ASD_DPRINTK("Uh-oh! No escb for this dl?!\n");
  256. return res;
  257. }
  258. edb = asd_ha->seq.edb_arr[edb_id + escb->edb_index];
  259. ascb->tag = *(__be16 *)(edb->vaddr+4);
  260. fh = edb->vaddr + 16;
  261. ru = edb->vaddr + 16 + sizeof(*fh);
  262. res = ru->status;
  263. if (ru->datapres == 1) /* Response data present */
  264. res = ru->resp_data[3];
  265. #if 0
  266. ascb->tag = fh->tag;
  267. #endif
  268. ascb->tag_valid = 1;
  269. asd_invalidate_edb(escb, edb_id);
  270. return res;
  271. }
  272. static void asd_tmf_tasklet_complete(struct asd_ascb *ascb,
  273. struct done_list_struct *dl)
  274. {
  275. struct tasklet_completion_status *tcs;
  276. if (!del_timer(&ascb->timer))
  277. return;
  278. tcs = ascb->uldd_task;
  279. ASD_DPRINTK("tmf tasklet complete\n");
  280. tcs->dl_opcode = dl->opcode;
  281. if (dl->opcode == TC_SSP_RESP) {
  282. tcs->tmf_state = asd_get_tmf_resp_tasklet(ascb, dl);
  283. tcs->tag_valid = ascb->tag_valid;
  284. tcs->tag = ascb->tag;
  285. }
  286. complete(ascb->completion);
  287. asd_ascb_free(ascb);
  288. }
  289. static int asd_clear_nexus(struct sas_task *task)
  290. {
  291. int res = TMF_RESP_FUNC_FAILED;
  292. int leftover;
  293. struct asd_ascb *tascb = task->lldd_task;
  294. DECLARE_COMPLETION_ONSTACK(completion);
  295. unsigned long flags;
  296. tascb->completion = &completion;
  297. ASD_DPRINTK("task not done, clearing nexus\n");
  298. if (tascb->tag_valid)
  299. res = asd_clear_nexus_tag(task);
  300. else
  301. res = asd_clear_nexus_index(task);
  302. leftover = wait_for_completion_timeout(&completion,
  303. AIC94XX_SCB_TIMEOUT);
  304. tascb->completion = NULL;
  305. ASD_DPRINTK("came back from clear nexus\n");
  306. spin_lock_irqsave(&task->task_state_lock, flags);
  307. if (leftover < 1)
  308. res = TMF_RESP_FUNC_FAILED;
  309. if (task->task_state_flags & SAS_TASK_STATE_DONE)
  310. res = TMF_RESP_FUNC_COMPLETE;
  311. spin_unlock_irqrestore(&task->task_state_lock, flags);
  312. return res;
  313. }
  314. /**
  315. * asd_abort_task -- ABORT TASK TMF
  316. * @task: the task to be aborted
  317. *
  318. * Before calling ABORT TASK the task state flags should be ORed with
  319. * SAS_TASK_STATE_ABORTED (unless SAS_TASK_STATE_DONE is set) under
  320. * the task_state_lock IRQ spinlock, then ABORT TASK *must* be called.
  321. *
  322. * Implements the ABORT TASK TMF, I_T_L_Q nexus.
  323. * Returns: SAS TMF responses (see sas_task.h),
  324. * -ENOMEM,
  325. * -SAS_QUEUE_FULL.
  326. *
  327. * When ABORT TASK returns, the caller of ABORT TASK checks first the
  328. * task->task_state_flags, and then the return value of ABORT TASK.
  329. *
  330. * If the task has task state bit SAS_TASK_STATE_DONE set, then the
  331. * task was completed successfully prior to it being aborted. The
  332. * caller of ABORT TASK has responsibility to call task->task_done()
  333. * xor free the task, depending on their framework. The return code
  334. * is TMF_RESP_FUNC_FAILED in this case.
  335. *
  336. * Else the SAS_TASK_STATE_DONE bit is not set,
  337. * If the return code is TMF_RESP_FUNC_COMPLETE, then
  338. * the task was aborted successfully. The caller of
  339. * ABORT TASK has responsibility to call task->task_done()
  340. * to finish the task, xor free the task depending on their
  341. * framework.
  342. * else
  343. * the ABORT TASK returned some kind of error. The task
  344. * was _not_ cancelled. Nothing can be assumed.
  345. * The caller of ABORT TASK may wish to retry.
  346. */
  347. int asd_abort_task(struct sas_task *task)
  348. {
  349. struct asd_ascb *tascb = task->lldd_task;
  350. struct asd_ha_struct *asd_ha = tascb->ha;
  351. int res = 1;
  352. unsigned long flags;
  353. struct asd_ascb *ascb = NULL;
  354. struct scb *scb;
  355. int leftover;
  356. DECLARE_TCS(tcs);
  357. DECLARE_COMPLETION_ONSTACK(completion);
  358. DECLARE_COMPLETION_ONSTACK(tascb_completion);
  359. tascb->completion = &tascb_completion;
  360. spin_lock_irqsave(&task->task_state_lock, flags);
  361. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  362. spin_unlock_irqrestore(&task->task_state_lock, flags);
  363. res = TMF_RESP_FUNC_COMPLETE;
  364. ASD_DPRINTK("%s: task 0x%p done\n", __func__, task);
  365. goto out_done;
  366. }
  367. spin_unlock_irqrestore(&task->task_state_lock, flags);
  368. ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
  369. if (!ascb)
  370. return -ENOMEM;
  371. ascb->uldd_task = &tcs;
  372. ascb->completion = &completion;
  373. scb = ascb->scb;
  374. scb->header.opcode = SCB_ABORT_TASK;
  375. switch (task->task_proto) {
  376. case SAS_PROTOCOL_SATA:
  377. case SAS_PROTOCOL_STP:
  378. scb->abort_task.proto_conn_rate = (1 << 5); /* STP */
  379. break;
  380. case SAS_PROTOCOL_SSP:
  381. scb->abort_task.proto_conn_rate = (1 << 4); /* SSP */
  382. scb->abort_task.proto_conn_rate |= task->dev->linkrate;
  383. break;
  384. case SAS_PROTOCOL_SMP:
  385. break;
  386. default:
  387. break;
  388. }
  389. if (task->task_proto == SAS_PROTOCOL_SSP) {
  390. scb->abort_task.ssp_frame.frame_type = SSP_TASK;
  391. memcpy(scb->abort_task.ssp_frame.hashed_dest_addr,
  392. task->dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
  393. memcpy(scb->abort_task.ssp_frame.hashed_src_addr,
  394. task->dev->port->ha->hashed_sas_addr,
  395. HASHED_SAS_ADDR_SIZE);
  396. scb->abort_task.ssp_frame.tptt = cpu_to_be16(0xFFFF);
  397. memcpy(scb->abort_task.ssp_task.lun, task->ssp_task.LUN, 8);
  398. scb->abort_task.ssp_task.tmf = TMF_ABORT_TASK;
  399. scb->abort_task.ssp_task.tag = cpu_to_be16(0xFFFF);
  400. }
  401. scb->abort_task.sister_scb = cpu_to_le16(0xFFFF);
  402. scb->abort_task.conn_handle = cpu_to_le16(
  403. (u16)(unsigned long)task->dev->lldd_dev);
  404. scb->abort_task.retry_count = 1;
  405. scb->abort_task.index = cpu_to_le16((u16)tascb->tc_index);
  406. scb->abort_task.itnl_to = cpu_to_le16(ITNL_TIMEOUT_CONST);
  407. res = asd_enqueue_internal(ascb, asd_tmf_tasklet_complete,
  408. asd_tmf_timedout);
  409. if (res)
  410. goto out_free;
  411. wait_for_completion(&completion);
  412. ASD_DPRINTK("tmf came back\n");
  413. tascb->tag = tcs.tag;
  414. tascb->tag_valid = tcs.tag_valid;
  415. spin_lock_irqsave(&task->task_state_lock, flags);
  416. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  417. spin_unlock_irqrestore(&task->task_state_lock, flags);
  418. res = TMF_RESP_FUNC_COMPLETE;
  419. ASD_DPRINTK("%s: task 0x%p done\n", __func__, task);
  420. goto out_done;
  421. }
  422. spin_unlock_irqrestore(&task->task_state_lock, flags);
  423. if (tcs.dl_opcode == TC_SSP_RESP) {
  424. /* The task to be aborted has been sent to the device.
  425. * We got a Response IU for the ABORT TASK TMF. */
  426. if (tcs.tmf_state == TMF_RESP_FUNC_COMPLETE)
  427. res = asd_clear_nexus(task);
  428. else
  429. res = tcs.tmf_state;
  430. } else if (tcs.dl_opcode == TC_NO_ERROR &&
  431. tcs.tmf_state == TMF_RESP_FUNC_FAILED) {
  432. /* timeout */
  433. res = TMF_RESP_FUNC_FAILED;
  434. } else {
  435. /* In the following we assume that the managing layer
  436. * will _never_ make a mistake, when issuing ABORT
  437. * TASK.
  438. */
  439. switch (tcs.dl_opcode) {
  440. default:
  441. res = asd_clear_nexus(task);
  442. /* fallthrough */
  443. case TC_NO_ERROR:
  444. break;
  445. /* The task hasn't been sent to the device xor
  446. * we never got a (sane) Response IU for the
  447. * ABORT TASK TMF.
  448. */
  449. case TF_NAK_RECV:
  450. res = TMF_RESP_INVALID_FRAME;
  451. break;
  452. case TF_TMF_TASK_DONE: /* done but not reported yet */
  453. res = TMF_RESP_FUNC_FAILED;
  454. leftover =
  455. wait_for_completion_timeout(&tascb_completion,
  456. AIC94XX_SCB_TIMEOUT);
  457. spin_lock_irqsave(&task->task_state_lock, flags);
  458. if (leftover < 1)
  459. res = TMF_RESP_FUNC_FAILED;
  460. if (task->task_state_flags & SAS_TASK_STATE_DONE)
  461. res = TMF_RESP_FUNC_COMPLETE;
  462. spin_unlock_irqrestore(&task->task_state_lock, flags);
  463. break;
  464. case TF_TMF_NO_TAG:
  465. case TF_TMF_TAG_FREE: /* the tag is in the free list */
  466. case TF_TMF_NO_CONN_HANDLE: /* no such device */
  467. res = TMF_RESP_FUNC_COMPLETE;
  468. break;
  469. case TF_TMF_NO_CTX: /* not in seq, or proto != SSP */
  470. res = TMF_RESP_FUNC_ESUPP;
  471. break;
  472. }
  473. }
  474. out_done:
  475. tascb->completion = NULL;
  476. if (res == TMF_RESP_FUNC_COMPLETE) {
  477. task->lldd_task = NULL;
  478. mb();
  479. asd_ascb_free(tascb);
  480. }
  481. ASD_DPRINTK("task 0x%p aborted, res: 0x%x\n", task, res);
  482. return res;
  483. out_free:
  484. asd_ascb_free(ascb);
  485. ASD_DPRINTK("task 0x%p aborted, res: 0x%x\n", task, res);
  486. return res;
  487. }
  488. /**
  489. * asd_initiate_ssp_tmf -- send a TMF to an I_T_L or I_T_L_Q nexus
  490. * @dev: pointer to struct domain_device of interest
  491. * @lun: pointer to u8[8] which is the LUN
  492. * @tmf: the TMF to be performed (see sas_task.h or the SAS spec)
  493. * @index: the transaction context of the task to be queried if QT TMF
  494. *
  495. * This function is used to send ABORT TASK SET, CLEAR ACA,
  496. * CLEAR TASK SET, LU RESET and QUERY TASK TMFs.
  497. *
  498. * No SCBs should be queued to the I_T_L nexus when this SCB is
  499. * pending.
  500. *
  501. * Returns: TMF response code (see sas_task.h or the SAS spec)
  502. */
  503. static int asd_initiate_ssp_tmf(struct domain_device *dev, u8 *lun,
  504. int tmf, int index)
  505. {
  506. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  507. struct asd_ascb *ascb;
  508. int res = 1;
  509. struct scb *scb;
  510. DECLARE_COMPLETION_ONSTACK(completion);
  511. DECLARE_TCS(tcs);
  512. if (!(dev->tproto & SAS_PROTOCOL_SSP))
  513. return TMF_RESP_FUNC_ESUPP;
  514. ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
  515. if (!ascb)
  516. return -ENOMEM;
  517. ascb->completion = &completion;
  518. ascb->uldd_task = &tcs;
  519. scb = ascb->scb;
  520. if (tmf == TMF_QUERY_TASK)
  521. scb->header.opcode = QUERY_SSP_TASK;
  522. else
  523. scb->header.opcode = INITIATE_SSP_TMF;
  524. scb->ssp_tmf.proto_conn_rate = (1 << 4); /* SSP */
  525. scb->ssp_tmf.proto_conn_rate |= dev->linkrate;
  526. /* SSP frame header */
  527. scb->ssp_tmf.ssp_frame.frame_type = SSP_TASK;
  528. memcpy(scb->ssp_tmf.ssp_frame.hashed_dest_addr,
  529. dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
  530. memcpy(scb->ssp_tmf.ssp_frame.hashed_src_addr,
  531. dev->port->ha->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
  532. scb->ssp_tmf.ssp_frame.tptt = cpu_to_be16(0xFFFF);
  533. /* SSP Task IU */
  534. memcpy(scb->ssp_tmf.ssp_task.lun, lun, 8);
  535. scb->ssp_tmf.ssp_task.tmf = tmf;
  536. scb->ssp_tmf.sister_scb = cpu_to_le16(0xFFFF);
  537. scb->ssp_tmf.conn_handle= cpu_to_le16((u16)(unsigned long)
  538. dev->lldd_dev);
  539. scb->ssp_tmf.retry_count = 1;
  540. scb->ssp_tmf.itnl_to = cpu_to_le16(ITNL_TIMEOUT_CONST);
  541. if (tmf == TMF_QUERY_TASK)
  542. scb->ssp_tmf.index = cpu_to_le16(index);
  543. res = asd_enqueue_internal(ascb, asd_tmf_tasklet_complete,
  544. asd_tmf_timedout);
  545. if (res)
  546. goto out_err;
  547. wait_for_completion(&completion);
  548. switch (tcs.dl_opcode) {
  549. case TC_NO_ERROR:
  550. res = TMF_RESP_FUNC_COMPLETE;
  551. break;
  552. case TF_NAK_RECV:
  553. res = TMF_RESP_INVALID_FRAME;
  554. break;
  555. case TF_TMF_TASK_DONE:
  556. res = TMF_RESP_FUNC_FAILED;
  557. break;
  558. case TF_TMF_NO_TAG:
  559. case TF_TMF_TAG_FREE: /* the tag is in the free list */
  560. case TF_TMF_NO_CONN_HANDLE: /* no such device */
  561. res = TMF_RESP_FUNC_COMPLETE;
  562. break;
  563. case TF_TMF_NO_CTX: /* not in seq, or proto != SSP */
  564. res = TMF_RESP_FUNC_ESUPP;
  565. break;
  566. default:
  567. /* Allow TMF response codes to propagate upwards */
  568. res = tcs.dl_opcode;
  569. break;
  570. }
  571. return res;
  572. out_err:
  573. asd_ascb_free(ascb);
  574. return res;
  575. }
  576. int asd_abort_task_set(struct domain_device *dev, u8 *lun)
  577. {
  578. int res = asd_initiate_ssp_tmf(dev, lun, TMF_ABORT_TASK_SET, 0);
  579. if (res == TMF_RESP_FUNC_COMPLETE)
  580. asd_clear_nexus_I_T_L(dev, lun);
  581. return res;
  582. }
  583. int asd_clear_aca(struct domain_device *dev, u8 *lun)
  584. {
  585. int res = asd_initiate_ssp_tmf(dev, lun, TMF_CLEAR_ACA, 0);
  586. if (res == TMF_RESP_FUNC_COMPLETE)
  587. asd_clear_nexus_I_T_L(dev, lun);
  588. return res;
  589. }
  590. int asd_clear_task_set(struct domain_device *dev, u8 *lun)
  591. {
  592. int res = asd_initiate_ssp_tmf(dev, lun, TMF_CLEAR_TASK_SET, 0);
  593. if (res == TMF_RESP_FUNC_COMPLETE)
  594. asd_clear_nexus_I_T_L(dev, lun);
  595. return res;
  596. }
  597. int asd_lu_reset(struct domain_device *dev, u8 *lun)
  598. {
  599. int res = asd_initiate_ssp_tmf(dev, lun, TMF_LU_RESET, 0);
  600. if (res == TMF_RESP_FUNC_COMPLETE)
  601. asd_clear_nexus_I_T_L(dev, lun);
  602. return res;
  603. }
  604. /**
  605. * asd_query_task -- send a QUERY TASK TMF to an I_T_L_Q nexus
  606. * task: pointer to sas_task struct of interest
  607. *
  608. * Returns: TMF_RESP_FUNC_COMPLETE if the task is not in the task set,
  609. * or TMF_RESP_FUNC_SUCC if the task is in the task set.
  610. *
  611. * Normally the management layer sets the task to aborted state,
  612. * and then calls query task and then abort task.
  613. */
  614. int asd_query_task(struct sas_task *task)
  615. {
  616. struct asd_ascb *ascb = task->lldd_task;
  617. int index;
  618. if (ascb) {
  619. index = ascb->tc_index;
  620. return asd_initiate_ssp_tmf(task->dev, task->ssp_task.LUN,
  621. TMF_QUERY_TASK, index);
  622. }
  623. return TMF_RESP_FUNC_COMPLETE;
  624. }