ipath_sdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Copyright (c) 2007, 2008 QLogic Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/spinlock.h>
  33. #include "ipath_kernel.h"
  34. #include "ipath_verbs.h"
  35. #include "ipath_common.h"
  36. #define SDMA_DESCQ_SZ PAGE_SIZE /* 256 entries per 4KB page */
  37. static void vl15_watchdog_enq(struct ipath_devdata *dd)
  38. {
  39. /* ipath_sdma_lock must already be held */
  40. if (atomic_inc_return(&dd->ipath_sdma_vl15_count) == 1) {
  41. unsigned long interval = (HZ + 19) / 20;
  42. dd->ipath_sdma_vl15_timer.expires = jiffies + interval;
  43. add_timer(&dd->ipath_sdma_vl15_timer);
  44. }
  45. }
  46. static void vl15_watchdog_deq(struct ipath_devdata *dd)
  47. {
  48. /* ipath_sdma_lock must already be held */
  49. if (atomic_dec_return(&dd->ipath_sdma_vl15_count) != 0) {
  50. unsigned long interval = (HZ + 19) / 20;
  51. mod_timer(&dd->ipath_sdma_vl15_timer, jiffies + interval);
  52. } else {
  53. del_timer(&dd->ipath_sdma_vl15_timer);
  54. }
  55. }
  56. static void vl15_watchdog_timeout(unsigned long opaque)
  57. {
  58. struct ipath_devdata *dd = (struct ipath_devdata *)opaque;
  59. if (atomic_read(&dd->ipath_sdma_vl15_count) != 0) {
  60. ipath_dbg("vl15 watchdog timeout - clearing\n");
  61. ipath_cancel_sends(dd, 1);
  62. ipath_hol_down(dd);
  63. } else {
  64. ipath_dbg("vl15 watchdog timeout - "
  65. "condition already cleared\n");
  66. }
  67. }
  68. static void unmap_desc(struct ipath_devdata *dd, unsigned head)
  69. {
  70. __le64 *descqp = &dd->ipath_sdma_descq[head].qw[0];
  71. u64 desc[2];
  72. dma_addr_t addr;
  73. size_t len;
  74. desc[0] = le64_to_cpu(descqp[0]);
  75. desc[1] = le64_to_cpu(descqp[1]);
  76. addr = (desc[1] << 32) | (desc[0] >> 32);
  77. len = (desc[0] >> 14) & (0x7ffULL << 2);
  78. dma_unmap_single(&dd->pcidev->dev, addr, len, DMA_TO_DEVICE);
  79. }
  80. /*
  81. * ipath_sdma_lock should be locked before calling this.
  82. */
  83. int ipath_sdma_make_progress(struct ipath_devdata *dd)
  84. {
  85. struct list_head *lp = NULL;
  86. struct ipath_sdma_txreq *txp = NULL;
  87. u16 dmahead;
  88. u16 start_idx = 0;
  89. int progress = 0;
  90. if (!list_empty(&dd->ipath_sdma_activelist)) {
  91. lp = dd->ipath_sdma_activelist.next;
  92. txp = list_entry(lp, struct ipath_sdma_txreq, list);
  93. start_idx = txp->start_idx;
  94. }
  95. /*
  96. * Read the SDMA head register in order to know that the
  97. * interrupt clear has been written to the chip.
  98. * Otherwise, we may not get an interrupt for the last
  99. * descriptor in the queue.
  100. */
  101. dmahead = (u16)ipath_read_kreg32(dd, dd->ipath_kregs->kr_senddmahead);
  102. /* sanity check return value for error handling (chip reset, etc.) */
  103. if (dmahead >= dd->ipath_sdma_descq_cnt)
  104. goto done;
  105. while (dd->ipath_sdma_descq_head != dmahead) {
  106. if (txp && txp->flags & IPATH_SDMA_TXREQ_F_FREEDESC &&
  107. dd->ipath_sdma_descq_head == start_idx) {
  108. unmap_desc(dd, dd->ipath_sdma_descq_head);
  109. start_idx++;
  110. if (start_idx == dd->ipath_sdma_descq_cnt)
  111. start_idx = 0;
  112. }
  113. /* increment free count and head */
  114. dd->ipath_sdma_descq_removed++;
  115. if (++dd->ipath_sdma_descq_head == dd->ipath_sdma_descq_cnt)
  116. dd->ipath_sdma_descq_head = 0;
  117. if (txp && txp->next_descq_idx == dd->ipath_sdma_descq_head) {
  118. /* move to notify list */
  119. if (txp->flags & IPATH_SDMA_TXREQ_F_VL15)
  120. vl15_watchdog_deq(dd);
  121. list_move_tail(lp, &dd->ipath_sdma_notifylist);
  122. if (!list_empty(&dd->ipath_sdma_activelist)) {
  123. lp = dd->ipath_sdma_activelist.next;
  124. txp = list_entry(lp, struct ipath_sdma_txreq,
  125. list);
  126. start_idx = txp->start_idx;
  127. } else {
  128. lp = NULL;
  129. txp = NULL;
  130. }
  131. }
  132. progress = 1;
  133. }
  134. if (progress)
  135. tasklet_hi_schedule(&dd->ipath_sdma_notify_task);
  136. done:
  137. return progress;
  138. }
  139. static void ipath_sdma_notify(struct ipath_devdata *dd, struct list_head *list)
  140. {
  141. struct ipath_sdma_txreq *txp, *txp_next;
  142. list_for_each_entry_safe(txp, txp_next, list, list) {
  143. list_del_init(&txp->list);
  144. if (txp->callback)
  145. (*txp->callback)(txp->callback_cookie,
  146. txp->callback_status);
  147. }
  148. }
  149. static void sdma_notify_taskbody(struct ipath_devdata *dd)
  150. {
  151. unsigned long flags;
  152. struct list_head list;
  153. INIT_LIST_HEAD(&list);
  154. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  155. list_splice_init(&dd->ipath_sdma_notifylist, &list);
  156. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  157. ipath_sdma_notify(dd, &list);
  158. /*
  159. * The IB verbs layer needs to see the callback before getting
  160. * the call to ipath_ib_piobufavail() because the callback
  161. * handles releasing resources the next send will need.
  162. * Otherwise, we could do these calls in
  163. * ipath_sdma_make_progress().
  164. */
  165. ipath_ib_piobufavail(dd->verbs_dev);
  166. }
  167. static void sdma_notify_task(unsigned long opaque)
  168. {
  169. struct ipath_devdata *dd = (struct ipath_devdata *)opaque;
  170. if (!test_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status))
  171. sdma_notify_taskbody(dd);
  172. }
  173. static void dump_sdma_state(struct ipath_devdata *dd)
  174. {
  175. unsigned long reg;
  176. reg = ipath_read_kreg64(dd, dd->ipath_kregs->kr_senddmastatus);
  177. ipath_cdbg(VERBOSE, "kr_senddmastatus: 0x%016lx\n", reg);
  178. reg = ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendctrl);
  179. ipath_cdbg(VERBOSE, "kr_sendctrl: 0x%016lx\n", reg);
  180. reg = ipath_read_kreg64(dd, dd->ipath_kregs->kr_senddmabufmask0);
  181. ipath_cdbg(VERBOSE, "kr_senddmabufmask0: 0x%016lx\n", reg);
  182. reg = ipath_read_kreg64(dd, dd->ipath_kregs->kr_senddmabufmask1);
  183. ipath_cdbg(VERBOSE, "kr_senddmabufmask1: 0x%016lx\n", reg);
  184. reg = ipath_read_kreg64(dd, dd->ipath_kregs->kr_senddmabufmask2);
  185. ipath_cdbg(VERBOSE, "kr_senddmabufmask2: 0x%016lx\n", reg);
  186. reg = ipath_read_kreg64(dd, dd->ipath_kregs->kr_senddmatail);
  187. ipath_cdbg(VERBOSE, "kr_senddmatail: 0x%016lx\n", reg);
  188. reg = ipath_read_kreg64(dd, dd->ipath_kregs->kr_senddmahead);
  189. ipath_cdbg(VERBOSE, "kr_senddmahead: 0x%016lx\n", reg);
  190. }
  191. static void sdma_abort_task(unsigned long opaque)
  192. {
  193. struct ipath_devdata *dd = (struct ipath_devdata *) opaque;
  194. u64 status;
  195. unsigned long flags;
  196. if (test_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status))
  197. return;
  198. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  199. status = dd->ipath_sdma_status & IPATH_SDMA_ABORT_MASK;
  200. /* nothing to do */
  201. if (status == IPATH_SDMA_ABORT_NONE)
  202. goto unlock;
  203. /* ipath_sdma_abort() is done, waiting for interrupt */
  204. if (status == IPATH_SDMA_ABORT_DISARMED) {
  205. if (jiffies < dd->ipath_sdma_abort_intr_timeout)
  206. goto resched_noprint;
  207. /* give up, intr got lost somewhere */
  208. ipath_dbg("give up waiting for SDMADISABLED intr\n");
  209. __set_bit(IPATH_SDMA_DISABLED, &dd->ipath_sdma_status);
  210. status = IPATH_SDMA_ABORT_ABORTED;
  211. }
  212. /* everything is stopped, time to clean up and restart */
  213. if (status == IPATH_SDMA_ABORT_ABORTED) {
  214. struct ipath_sdma_txreq *txp, *txpnext;
  215. u64 hwstatus;
  216. int notify = 0;
  217. hwstatus = ipath_read_kreg64(dd,
  218. dd->ipath_kregs->kr_senddmastatus);
  219. if (/* ScoreBoardDrainInProg */
  220. test_bit(63, &hwstatus) ||
  221. /* AbortInProg */
  222. test_bit(62, &hwstatus) ||
  223. /* InternalSDmaEnable */
  224. test_bit(61, &hwstatus) ||
  225. /* ScbEmpty */
  226. !test_bit(30, &hwstatus)) {
  227. if (dd->ipath_sdma_reset_wait > 0) {
  228. /* not done shutting down sdma */
  229. --dd->ipath_sdma_reset_wait;
  230. goto resched;
  231. }
  232. ipath_cdbg(VERBOSE, "gave up waiting for quiescent "
  233. "status after SDMA reset, continuing\n");
  234. dump_sdma_state(dd);
  235. }
  236. /* dequeue all "sent" requests */
  237. list_for_each_entry_safe(txp, txpnext,
  238. &dd->ipath_sdma_activelist, list) {
  239. txp->callback_status = IPATH_SDMA_TXREQ_S_ABORTED;
  240. if (txp->flags & IPATH_SDMA_TXREQ_F_VL15)
  241. vl15_watchdog_deq(dd);
  242. list_move_tail(&txp->list, &dd->ipath_sdma_notifylist);
  243. notify = 1;
  244. }
  245. if (notify)
  246. tasklet_hi_schedule(&dd->ipath_sdma_notify_task);
  247. /* reset our notion of head and tail */
  248. dd->ipath_sdma_descq_tail = 0;
  249. dd->ipath_sdma_descq_head = 0;
  250. dd->ipath_sdma_head_dma[0] = 0;
  251. dd->ipath_sdma_generation = 0;
  252. dd->ipath_sdma_descq_removed = dd->ipath_sdma_descq_added;
  253. /* Reset SendDmaLenGen */
  254. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmalengen,
  255. (u64) dd->ipath_sdma_descq_cnt | (1ULL << 18));
  256. /* done with sdma state for a bit */
  257. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  258. /*
  259. * Don't restart sdma here. Wait until link is up to ACTIVE.
  260. * VL15 MADs used to bring the link up use PIO, and multiple
  261. * link transitions otherwise cause the sdma engine to be
  262. * stopped and started multiple times.
  263. * The disable is done here, including the shadow, so the
  264. * state is kept consistent.
  265. * See ipath_restart_sdma() for the actual starting of sdma.
  266. */
  267. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  268. dd->ipath_sendctrl &= ~INFINIPATH_S_SDMAENABLE;
  269. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  270. dd->ipath_sendctrl);
  271. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  272. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  273. /* make sure I see next message */
  274. dd->ipath_sdma_abort_jiffies = 0;
  275. goto done;
  276. }
  277. resched:
  278. /*
  279. * for now, keep spinning
  280. * JAG - this is bad to just have default be a loop without
  281. * state change
  282. */
  283. if (jiffies > dd->ipath_sdma_abort_jiffies) {
  284. ipath_dbg("looping with status 0x%016llx\n",
  285. dd->ipath_sdma_status);
  286. dd->ipath_sdma_abort_jiffies = jiffies + 5 * HZ;
  287. }
  288. resched_noprint:
  289. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  290. if (!test_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status))
  291. tasklet_hi_schedule(&dd->ipath_sdma_abort_task);
  292. return;
  293. unlock:
  294. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  295. done:
  296. return;
  297. }
  298. /*
  299. * This is called from interrupt context.
  300. */
  301. void ipath_sdma_intr(struct ipath_devdata *dd)
  302. {
  303. unsigned long flags;
  304. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  305. (void) ipath_sdma_make_progress(dd);
  306. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  307. }
  308. static int alloc_sdma(struct ipath_devdata *dd)
  309. {
  310. int ret = 0;
  311. /* Allocate memory for SendDMA descriptor FIFO */
  312. dd->ipath_sdma_descq = dma_alloc_coherent(&dd->pcidev->dev,
  313. SDMA_DESCQ_SZ, &dd->ipath_sdma_descq_phys, GFP_KERNEL);
  314. if (!dd->ipath_sdma_descq) {
  315. ipath_dev_err(dd, "failed to allocate SendDMA descriptor "
  316. "FIFO memory\n");
  317. ret = -ENOMEM;
  318. goto done;
  319. }
  320. dd->ipath_sdma_descq_cnt =
  321. SDMA_DESCQ_SZ / sizeof(struct ipath_sdma_desc);
  322. /* Allocate memory for DMA of head register to memory */
  323. dd->ipath_sdma_head_dma = dma_alloc_coherent(&dd->pcidev->dev,
  324. PAGE_SIZE, &dd->ipath_sdma_head_phys, GFP_KERNEL);
  325. if (!dd->ipath_sdma_head_dma) {
  326. ipath_dev_err(dd, "failed to allocate SendDMA head memory\n");
  327. ret = -ENOMEM;
  328. goto cleanup_descq;
  329. }
  330. dd->ipath_sdma_head_dma[0] = 0;
  331. init_timer(&dd->ipath_sdma_vl15_timer);
  332. dd->ipath_sdma_vl15_timer.function = vl15_watchdog_timeout;
  333. dd->ipath_sdma_vl15_timer.data = (unsigned long)dd;
  334. atomic_set(&dd->ipath_sdma_vl15_count, 0);
  335. goto done;
  336. cleanup_descq:
  337. dma_free_coherent(&dd->pcidev->dev, SDMA_DESCQ_SZ,
  338. (void *)dd->ipath_sdma_descq, dd->ipath_sdma_descq_phys);
  339. dd->ipath_sdma_descq = NULL;
  340. dd->ipath_sdma_descq_phys = 0;
  341. done:
  342. return ret;
  343. }
  344. int setup_sdma(struct ipath_devdata *dd)
  345. {
  346. int ret = 0;
  347. unsigned i, n;
  348. u64 tmp64;
  349. u64 senddmabufmask[3] = { 0 };
  350. unsigned long flags;
  351. ret = alloc_sdma(dd);
  352. if (ret)
  353. goto done;
  354. if (!dd->ipath_sdma_descq) {
  355. ipath_dev_err(dd, "SendDMA memory not allocated\n");
  356. goto done;
  357. }
  358. dd->ipath_sdma_status = 0;
  359. dd->ipath_sdma_abort_jiffies = 0;
  360. dd->ipath_sdma_generation = 0;
  361. dd->ipath_sdma_descq_tail = 0;
  362. dd->ipath_sdma_descq_head = 0;
  363. dd->ipath_sdma_descq_removed = 0;
  364. dd->ipath_sdma_descq_added = 0;
  365. /* Set SendDmaBase */
  366. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabase,
  367. dd->ipath_sdma_descq_phys);
  368. /* Set SendDmaLenGen */
  369. tmp64 = dd->ipath_sdma_descq_cnt;
  370. tmp64 |= 1<<18; /* enable generation checking */
  371. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmalengen, tmp64);
  372. /* Set SendDmaTail */
  373. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmatail,
  374. dd->ipath_sdma_descq_tail);
  375. /* Set SendDmaHeadAddr */
  376. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmaheadaddr,
  377. dd->ipath_sdma_head_phys);
  378. /* Reserve all the former "kernel" piobufs */
  379. n = dd->ipath_piobcnt2k + dd->ipath_piobcnt4k - dd->ipath_pioreserved;
  380. for (i = dd->ipath_lastport_piobuf; i < n; ++i) {
  381. unsigned word = i / 64;
  382. unsigned bit = i & 63;
  383. BUG_ON(word >= 3);
  384. senddmabufmask[word] |= 1ULL << bit;
  385. }
  386. ipath_chg_pioavailkernel(dd, dd->ipath_lastport_piobuf,
  387. n - dd->ipath_lastport_piobuf, 0);
  388. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask0,
  389. senddmabufmask[0]);
  390. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask1,
  391. senddmabufmask[1]);
  392. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask2,
  393. senddmabufmask[2]);
  394. INIT_LIST_HEAD(&dd->ipath_sdma_activelist);
  395. INIT_LIST_HEAD(&dd->ipath_sdma_notifylist);
  396. tasklet_init(&dd->ipath_sdma_notify_task, sdma_notify_task,
  397. (unsigned long) dd);
  398. tasklet_init(&dd->ipath_sdma_abort_task, sdma_abort_task,
  399. (unsigned long) dd);
  400. /*
  401. * No use to turn on SDMA here, as link is probably not ACTIVE
  402. * Just mark it RUNNING and enable the interrupt, and let the
  403. * ipath_restart_sdma() on link transition to ACTIVE actually
  404. * enable it.
  405. */
  406. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  407. dd->ipath_sendctrl |= INFINIPATH_S_SDMAINTENABLE;
  408. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  409. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  410. __set_bit(IPATH_SDMA_RUNNING, &dd->ipath_sdma_status);
  411. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  412. done:
  413. return ret;
  414. }
  415. void teardown_sdma(struct ipath_devdata *dd)
  416. {
  417. struct ipath_sdma_txreq *txp, *txpnext;
  418. unsigned long flags;
  419. dma_addr_t sdma_head_phys = 0;
  420. dma_addr_t sdma_descq_phys = 0;
  421. void *sdma_descq = NULL;
  422. void *sdma_head_dma = NULL;
  423. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  424. __clear_bit(IPATH_SDMA_RUNNING, &dd->ipath_sdma_status);
  425. __set_bit(IPATH_SDMA_ABORTING, &dd->ipath_sdma_status);
  426. __set_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status);
  427. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  428. tasklet_kill(&dd->ipath_sdma_abort_task);
  429. tasklet_kill(&dd->ipath_sdma_notify_task);
  430. /* turn off sdma */
  431. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  432. dd->ipath_sendctrl &= ~INFINIPATH_S_SDMAENABLE;
  433. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  434. dd->ipath_sendctrl);
  435. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  436. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  437. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  438. /* dequeue all "sent" requests */
  439. list_for_each_entry_safe(txp, txpnext, &dd->ipath_sdma_activelist,
  440. list) {
  441. txp->callback_status = IPATH_SDMA_TXREQ_S_SHUTDOWN;
  442. if (txp->flags & IPATH_SDMA_TXREQ_F_VL15)
  443. vl15_watchdog_deq(dd);
  444. list_move_tail(&txp->list, &dd->ipath_sdma_notifylist);
  445. }
  446. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  447. sdma_notify_taskbody(dd);
  448. del_timer_sync(&dd->ipath_sdma_vl15_timer);
  449. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  450. dd->ipath_sdma_abort_jiffies = 0;
  451. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabase, 0);
  452. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmalengen, 0);
  453. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmatail, 0);
  454. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmaheadaddr, 0);
  455. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask0, 0);
  456. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask1, 0);
  457. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask2, 0);
  458. if (dd->ipath_sdma_head_dma) {
  459. sdma_head_dma = (void *) dd->ipath_sdma_head_dma;
  460. sdma_head_phys = dd->ipath_sdma_head_phys;
  461. dd->ipath_sdma_head_dma = NULL;
  462. dd->ipath_sdma_head_phys = 0;
  463. }
  464. if (dd->ipath_sdma_descq) {
  465. sdma_descq = dd->ipath_sdma_descq;
  466. sdma_descq_phys = dd->ipath_sdma_descq_phys;
  467. dd->ipath_sdma_descq = NULL;
  468. dd->ipath_sdma_descq_phys = 0;
  469. }
  470. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  471. if (sdma_head_dma)
  472. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  473. sdma_head_dma, sdma_head_phys);
  474. if (sdma_descq)
  475. dma_free_coherent(&dd->pcidev->dev, SDMA_DESCQ_SZ,
  476. sdma_descq, sdma_descq_phys);
  477. }
  478. /*
  479. * [Re]start SDMA, if we use it, and it's not already OK.
  480. * This is called on transition to link ACTIVE, either the first or
  481. * subsequent times.
  482. */
  483. void ipath_restart_sdma(struct ipath_devdata *dd)
  484. {
  485. unsigned long flags;
  486. int needed = 1;
  487. if (!(dd->ipath_flags & IPATH_HAS_SEND_DMA))
  488. goto bail;
  489. /*
  490. * First, make sure we should, which is to say,
  491. * check that we are "RUNNING" (not in teardown)
  492. * and not "SHUTDOWN"
  493. */
  494. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  495. if (!test_bit(IPATH_SDMA_RUNNING, &dd->ipath_sdma_status)
  496. || test_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status))
  497. needed = 0;
  498. else {
  499. __clear_bit(IPATH_SDMA_DISABLED, &dd->ipath_sdma_status);
  500. __clear_bit(IPATH_SDMA_DISARMED, &dd->ipath_sdma_status);
  501. __clear_bit(IPATH_SDMA_ABORTING, &dd->ipath_sdma_status);
  502. }
  503. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  504. if (!needed) {
  505. ipath_dbg("invalid attempt to restart SDMA, status 0x%016llx\n",
  506. dd->ipath_sdma_status);
  507. goto bail;
  508. }
  509. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  510. /*
  511. * First clear, just to be safe. Enable is only done
  512. * in chip on 0->1 transition
  513. */
  514. dd->ipath_sendctrl &= ~INFINIPATH_S_SDMAENABLE;
  515. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  516. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  517. dd->ipath_sendctrl |= INFINIPATH_S_SDMAENABLE;
  518. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  519. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  520. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  521. bail:
  522. return;
  523. }
  524. static inline void make_sdma_desc(struct ipath_devdata *dd,
  525. u64 *sdmadesc, u64 addr, u64 dwlen, u64 dwoffset)
  526. {
  527. WARN_ON(addr & 3);
  528. /* SDmaPhyAddr[47:32] */
  529. sdmadesc[1] = addr >> 32;
  530. /* SDmaPhyAddr[31:0] */
  531. sdmadesc[0] = (addr & 0xfffffffcULL) << 32;
  532. /* SDmaGeneration[1:0] */
  533. sdmadesc[0] |= (dd->ipath_sdma_generation & 3ULL) << 30;
  534. /* SDmaDwordCount[10:0] */
  535. sdmadesc[0] |= (dwlen & 0x7ffULL) << 16;
  536. /* SDmaBufOffset[12:2] */
  537. sdmadesc[0] |= dwoffset & 0x7ffULL;
  538. }
  539. /*
  540. * This function queues one IB packet onto the send DMA queue per call.
  541. * The caller is responsible for checking:
  542. * 1) The number of send DMA descriptor entries is less than the size of
  543. * the descriptor queue.
  544. * 2) The IB SGE addresses and lengths are 32-bit aligned
  545. * (except possibly the last SGE's length)
  546. * 3) The SGE addresses are suitable for passing to dma_map_single().
  547. */
  548. int ipath_sdma_verbs_send(struct ipath_devdata *dd,
  549. struct ipath_sge_state *ss, u32 dwords,
  550. struct ipath_verbs_txreq *tx)
  551. {
  552. unsigned long flags;
  553. struct ipath_sge *sge;
  554. int ret = 0;
  555. u16 tail;
  556. __le64 *descqp;
  557. u64 sdmadesc[2];
  558. u32 dwoffset;
  559. dma_addr_t addr;
  560. if ((tx->map_len + (dwords<<2)) > dd->ipath_ibmaxlen) {
  561. ipath_dbg("packet size %X > ibmax %X, fail\n",
  562. tx->map_len + (dwords<<2), dd->ipath_ibmaxlen);
  563. ret = -EMSGSIZE;
  564. goto fail;
  565. }
  566. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  567. retry:
  568. if (unlikely(test_bit(IPATH_SDMA_ABORTING, &dd->ipath_sdma_status))) {
  569. ret = -EBUSY;
  570. goto unlock;
  571. }
  572. if (tx->txreq.sg_count > ipath_sdma_descq_freecnt(dd)) {
  573. if (ipath_sdma_make_progress(dd))
  574. goto retry;
  575. ret = -ENOBUFS;
  576. goto unlock;
  577. }
  578. addr = dma_map_single(&dd->pcidev->dev, tx->txreq.map_addr,
  579. tx->map_len, DMA_TO_DEVICE);
  580. if (dma_mapping_error(addr)) {
  581. ret = -EIO;
  582. goto unlock;
  583. }
  584. dwoffset = tx->map_len >> 2;
  585. make_sdma_desc(dd, sdmadesc, (u64) addr, dwoffset, 0);
  586. /* SDmaFirstDesc */
  587. sdmadesc[0] |= 1ULL << 12;
  588. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_USELARGEBUF)
  589. sdmadesc[0] |= 1ULL << 14; /* SDmaUseLargeBuf */
  590. /* write to the descq */
  591. tail = dd->ipath_sdma_descq_tail;
  592. descqp = &dd->ipath_sdma_descq[tail].qw[0];
  593. *descqp++ = cpu_to_le64(sdmadesc[0]);
  594. *descqp++ = cpu_to_le64(sdmadesc[1]);
  595. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_FREEDESC)
  596. tx->txreq.start_idx = tail;
  597. /* increment the tail */
  598. if (++tail == dd->ipath_sdma_descq_cnt) {
  599. tail = 0;
  600. descqp = &dd->ipath_sdma_descq[0].qw[0];
  601. ++dd->ipath_sdma_generation;
  602. }
  603. sge = &ss->sge;
  604. while (dwords) {
  605. u32 dw;
  606. u32 len;
  607. len = dwords << 2;
  608. if (len > sge->length)
  609. len = sge->length;
  610. if (len > sge->sge_length)
  611. len = sge->sge_length;
  612. BUG_ON(len == 0);
  613. dw = (len + 3) >> 2;
  614. addr = dma_map_single(&dd->pcidev->dev, sge->vaddr, dw << 2,
  615. DMA_TO_DEVICE);
  616. make_sdma_desc(dd, sdmadesc, (u64) addr, dw, dwoffset);
  617. /* SDmaUseLargeBuf has to be set in every descriptor */
  618. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_USELARGEBUF)
  619. sdmadesc[0] |= 1ULL << 14;
  620. /* write to the descq */
  621. *descqp++ = cpu_to_le64(sdmadesc[0]);
  622. *descqp++ = cpu_to_le64(sdmadesc[1]);
  623. /* increment the tail */
  624. if (++tail == dd->ipath_sdma_descq_cnt) {
  625. tail = 0;
  626. descqp = &dd->ipath_sdma_descq[0].qw[0];
  627. ++dd->ipath_sdma_generation;
  628. }
  629. sge->vaddr += len;
  630. sge->length -= len;
  631. sge->sge_length -= len;
  632. if (sge->sge_length == 0) {
  633. if (--ss->num_sge)
  634. *sge = *ss->sg_list++;
  635. } else if (sge->length == 0 && sge->mr != NULL) {
  636. if (++sge->n >= IPATH_SEGSZ) {
  637. if (++sge->m >= sge->mr->mapsz)
  638. break;
  639. sge->n = 0;
  640. }
  641. sge->vaddr =
  642. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  643. sge->length =
  644. sge->mr->map[sge->m]->segs[sge->n].length;
  645. }
  646. dwoffset += dw;
  647. dwords -= dw;
  648. }
  649. if (!tail)
  650. descqp = &dd->ipath_sdma_descq[dd->ipath_sdma_descq_cnt].qw[0];
  651. descqp -= 2;
  652. /* SDmaLastDesc */
  653. descqp[0] |= __constant_cpu_to_le64(1ULL << 11);
  654. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_INTREQ) {
  655. /* SDmaIntReq */
  656. descqp[0] |= __constant_cpu_to_le64(1ULL << 15);
  657. }
  658. /* Commit writes to memory and advance the tail on the chip */
  659. wmb();
  660. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmatail, tail);
  661. tx->txreq.next_descq_idx = tail;
  662. tx->txreq.callback_status = IPATH_SDMA_TXREQ_S_OK;
  663. dd->ipath_sdma_descq_tail = tail;
  664. dd->ipath_sdma_descq_added += tx->txreq.sg_count;
  665. list_add_tail(&tx->txreq.list, &dd->ipath_sdma_activelist);
  666. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_VL15)
  667. vl15_watchdog_enq(dd);
  668. unlock:
  669. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  670. fail:
  671. return ret;
  672. }