ipath_sdma.c 24 KB

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