ipath_sdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 ((hwstatus & (IPATH_SDMA_STATUS_SCORE_BOARD_DRAIN_IN_PROG |
  220. IPATH_SDMA_STATUS_ABORT_IN_PROG |
  221. IPATH_SDMA_STATUS_INTERNAL_SDMA_ENABLE)) ||
  222. !(hwstatus & IPATH_SDMA_STATUS_SCB_EMPTY)) {
  223. if (dd->ipath_sdma_reset_wait > 0) {
  224. /* not done shutting down sdma */
  225. --dd->ipath_sdma_reset_wait;
  226. goto resched;
  227. }
  228. ipath_cdbg(VERBOSE, "gave up waiting for quiescent "
  229. "status after SDMA reset, continuing\n");
  230. dump_sdma_state(dd);
  231. }
  232. /* dequeue all "sent" requests */
  233. list_for_each_entry_safe(txp, txpnext,
  234. &dd->ipath_sdma_activelist, list) {
  235. txp->callback_status = IPATH_SDMA_TXREQ_S_ABORTED;
  236. if (txp->flags & IPATH_SDMA_TXREQ_F_VL15)
  237. vl15_watchdog_deq(dd);
  238. list_move_tail(&txp->list, &dd->ipath_sdma_notifylist);
  239. notify = 1;
  240. }
  241. if (notify)
  242. tasklet_hi_schedule(&dd->ipath_sdma_notify_task);
  243. /* reset our notion of head and tail */
  244. dd->ipath_sdma_descq_tail = 0;
  245. dd->ipath_sdma_descq_head = 0;
  246. dd->ipath_sdma_head_dma[0] = 0;
  247. dd->ipath_sdma_generation = 0;
  248. dd->ipath_sdma_descq_removed = dd->ipath_sdma_descq_added;
  249. /* Reset SendDmaLenGen */
  250. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmalengen,
  251. (u64) dd->ipath_sdma_descq_cnt | (1ULL << 18));
  252. /* done with sdma state for a bit */
  253. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  254. /*
  255. * Don't restart sdma here (with the exception
  256. * below). Wait until link is up to ACTIVE. VL15 MADs
  257. * used to bring the link up use PIO, and multiple link
  258. * transitions otherwise cause the sdma engine to be
  259. * stopped and started multiple times.
  260. * The disable is done here, including the shadow,
  261. * so the state is kept consistent.
  262. * See ipath_restart_sdma() for the actual starting
  263. * of sdma.
  264. */
  265. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  266. dd->ipath_sendctrl &= ~INFINIPATH_S_SDMAENABLE;
  267. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  268. dd->ipath_sendctrl);
  269. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  270. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  271. /* make sure I see next message */
  272. dd->ipath_sdma_abort_jiffies = 0;
  273. /*
  274. * Not everything that takes SDMA offline is a link
  275. * status change. If the link was up, restart SDMA.
  276. */
  277. if (dd->ipath_flags & IPATH_LINKACTIVE)
  278. ipath_restart_sdma(dd);
  279. goto done;
  280. }
  281. resched:
  282. /*
  283. * for now, keep spinning
  284. * JAG - this is bad to just have default be a loop without
  285. * state change
  286. */
  287. if (jiffies > dd->ipath_sdma_abort_jiffies) {
  288. ipath_dbg("looping with status 0x%08lx\n",
  289. dd->ipath_sdma_status);
  290. dd->ipath_sdma_abort_jiffies = jiffies + 5 * HZ;
  291. }
  292. resched_noprint:
  293. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  294. if (!test_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status))
  295. tasklet_hi_schedule(&dd->ipath_sdma_abort_task);
  296. return;
  297. unlock:
  298. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  299. done:
  300. return;
  301. }
  302. /*
  303. * This is called from interrupt context.
  304. */
  305. void ipath_sdma_intr(struct ipath_devdata *dd)
  306. {
  307. unsigned long flags;
  308. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  309. (void) ipath_sdma_make_progress(dd);
  310. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  311. }
  312. static int alloc_sdma(struct ipath_devdata *dd)
  313. {
  314. int ret = 0;
  315. /* Allocate memory for SendDMA descriptor FIFO */
  316. dd->ipath_sdma_descq = dma_alloc_coherent(&dd->pcidev->dev,
  317. SDMA_DESCQ_SZ, &dd->ipath_sdma_descq_phys, GFP_KERNEL);
  318. if (!dd->ipath_sdma_descq) {
  319. ipath_dev_err(dd, "failed to allocate SendDMA descriptor "
  320. "FIFO memory\n");
  321. ret = -ENOMEM;
  322. goto done;
  323. }
  324. dd->ipath_sdma_descq_cnt =
  325. SDMA_DESCQ_SZ / sizeof(struct ipath_sdma_desc);
  326. /* Allocate memory for DMA of head register to memory */
  327. dd->ipath_sdma_head_dma = dma_alloc_coherent(&dd->pcidev->dev,
  328. PAGE_SIZE, &dd->ipath_sdma_head_phys, GFP_KERNEL);
  329. if (!dd->ipath_sdma_head_dma) {
  330. ipath_dev_err(dd, "failed to allocate SendDMA head memory\n");
  331. ret = -ENOMEM;
  332. goto cleanup_descq;
  333. }
  334. dd->ipath_sdma_head_dma[0] = 0;
  335. init_timer(&dd->ipath_sdma_vl15_timer);
  336. dd->ipath_sdma_vl15_timer.function = vl15_watchdog_timeout;
  337. dd->ipath_sdma_vl15_timer.data = (unsigned long)dd;
  338. atomic_set(&dd->ipath_sdma_vl15_count, 0);
  339. goto done;
  340. cleanup_descq:
  341. dma_free_coherent(&dd->pcidev->dev, SDMA_DESCQ_SZ,
  342. (void *)dd->ipath_sdma_descq, dd->ipath_sdma_descq_phys);
  343. dd->ipath_sdma_descq = NULL;
  344. dd->ipath_sdma_descq_phys = 0;
  345. done:
  346. return ret;
  347. }
  348. int setup_sdma(struct ipath_devdata *dd)
  349. {
  350. int ret = 0;
  351. unsigned i, n;
  352. u64 tmp64;
  353. u64 senddmabufmask[3] = { 0 };
  354. unsigned long flags;
  355. ret = alloc_sdma(dd);
  356. if (ret)
  357. goto done;
  358. if (!dd->ipath_sdma_descq) {
  359. ipath_dev_err(dd, "SendDMA memory not allocated\n");
  360. goto done;
  361. }
  362. /*
  363. * Set initial status as if we had been up, then gone down.
  364. * This lets initial start on transition to ACTIVE be the
  365. * same as restart after link flap.
  366. */
  367. dd->ipath_sdma_status = IPATH_SDMA_ABORT_ABORTED;
  368. dd->ipath_sdma_abort_jiffies = 0;
  369. dd->ipath_sdma_generation = 0;
  370. dd->ipath_sdma_descq_tail = 0;
  371. dd->ipath_sdma_descq_head = 0;
  372. dd->ipath_sdma_descq_removed = 0;
  373. dd->ipath_sdma_descq_added = 0;
  374. /* Set SendDmaBase */
  375. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabase,
  376. dd->ipath_sdma_descq_phys);
  377. /* Set SendDmaLenGen */
  378. tmp64 = dd->ipath_sdma_descq_cnt;
  379. tmp64 |= 1<<18; /* enable generation checking */
  380. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmalengen, tmp64);
  381. /* Set SendDmaTail */
  382. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmatail,
  383. dd->ipath_sdma_descq_tail);
  384. /* Set SendDmaHeadAddr */
  385. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmaheadaddr,
  386. dd->ipath_sdma_head_phys);
  387. /*
  388. * Reserve all the former "kernel" piobufs, using high number range
  389. * so we get as many 4K buffers as possible
  390. */
  391. n = dd->ipath_piobcnt2k + dd->ipath_piobcnt4k;
  392. i = dd->ipath_lastport_piobuf + dd->ipath_pioreserved;
  393. ipath_chg_pioavailkernel(dd, i, n - i , 0);
  394. for (; i < n; ++i) {
  395. unsigned word = i / 64;
  396. unsigned bit = i & 63;
  397. BUG_ON(word >= 3);
  398. senddmabufmask[word] |= 1ULL << bit;
  399. }
  400. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask0,
  401. senddmabufmask[0]);
  402. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask1,
  403. senddmabufmask[1]);
  404. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask2,
  405. senddmabufmask[2]);
  406. INIT_LIST_HEAD(&dd->ipath_sdma_activelist);
  407. INIT_LIST_HEAD(&dd->ipath_sdma_notifylist);
  408. tasklet_init(&dd->ipath_sdma_notify_task, sdma_notify_task,
  409. (unsigned long) dd);
  410. tasklet_init(&dd->ipath_sdma_abort_task, sdma_abort_task,
  411. (unsigned long) dd);
  412. /*
  413. * No use to turn on SDMA here, as link is probably not ACTIVE
  414. * Just mark it RUNNING and enable the interrupt, and let the
  415. * ipath_restart_sdma() on link transition to ACTIVE actually
  416. * enable it.
  417. */
  418. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  419. dd->ipath_sendctrl |= INFINIPATH_S_SDMAINTENABLE;
  420. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  421. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  422. __set_bit(IPATH_SDMA_RUNNING, &dd->ipath_sdma_status);
  423. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  424. done:
  425. return ret;
  426. }
  427. void teardown_sdma(struct ipath_devdata *dd)
  428. {
  429. struct ipath_sdma_txreq *txp, *txpnext;
  430. unsigned long flags;
  431. dma_addr_t sdma_head_phys = 0;
  432. dma_addr_t sdma_descq_phys = 0;
  433. void *sdma_descq = NULL;
  434. void *sdma_head_dma = NULL;
  435. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  436. __clear_bit(IPATH_SDMA_RUNNING, &dd->ipath_sdma_status);
  437. __set_bit(IPATH_SDMA_ABORTING, &dd->ipath_sdma_status);
  438. __set_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status);
  439. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  440. tasklet_kill(&dd->ipath_sdma_abort_task);
  441. tasklet_kill(&dd->ipath_sdma_notify_task);
  442. /* turn off sdma */
  443. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  444. dd->ipath_sendctrl &= ~INFINIPATH_S_SDMAENABLE;
  445. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  446. dd->ipath_sendctrl);
  447. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  448. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  449. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  450. /* dequeue all "sent" requests */
  451. list_for_each_entry_safe(txp, txpnext, &dd->ipath_sdma_activelist,
  452. list) {
  453. txp->callback_status = IPATH_SDMA_TXREQ_S_SHUTDOWN;
  454. if (txp->flags & IPATH_SDMA_TXREQ_F_VL15)
  455. vl15_watchdog_deq(dd);
  456. list_move_tail(&txp->list, &dd->ipath_sdma_notifylist);
  457. }
  458. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  459. sdma_notify_taskbody(dd);
  460. del_timer_sync(&dd->ipath_sdma_vl15_timer);
  461. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  462. dd->ipath_sdma_abort_jiffies = 0;
  463. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabase, 0);
  464. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmalengen, 0);
  465. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmatail, 0);
  466. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmaheadaddr, 0);
  467. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask0, 0);
  468. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask1, 0);
  469. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmabufmask2, 0);
  470. if (dd->ipath_sdma_head_dma) {
  471. sdma_head_dma = (void *) dd->ipath_sdma_head_dma;
  472. sdma_head_phys = dd->ipath_sdma_head_phys;
  473. dd->ipath_sdma_head_dma = NULL;
  474. dd->ipath_sdma_head_phys = 0;
  475. }
  476. if (dd->ipath_sdma_descq) {
  477. sdma_descq = dd->ipath_sdma_descq;
  478. sdma_descq_phys = dd->ipath_sdma_descq_phys;
  479. dd->ipath_sdma_descq = NULL;
  480. dd->ipath_sdma_descq_phys = 0;
  481. }
  482. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  483. if (sdma_head_dma)
  484. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  485. sdma_head_dma, sdma_head_phys);
  486. if (sdma_descq)
  487. dma_free_coherent(&dd->pcidev->dev, SDMA_DESCQ_SZ,
  488. sdma_descq, sdma_descq_phys);
  489. }
  490. /*
  491. * [Re]start SDMA, if we use it, and it's not already OK.
  492. * This is called on transition to link ACTIVE, either the first or
  493. * subsequent times.
  494. */
  495. void ipath_restart_sdma(struct ipath_devdata *dd)
  496. {
  497. unsigned long flags;
  498. int needed = 1;
  499. if (!(dd->ipath_flags & IPATH_HAS_SEND_DMA))
  500. goto bail;
  501. /*
  502. * First, make sure we should, which is to say,
  503. * check that we are "RUNNING" (not in teardown)
  504. * and not "SHUTDOWN"
  505. */
  506. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  507. if (!test_bit(IPATH_SDMA_RUNNING, &dd->ipath_sdma_status)
  508. || test_bit(IPATH_SDMA_SHUTDOWN, &dd->ipath_sdma_status))
  509. needed = 0;
  510. else {
  511. __clear_bit(IPATH_SDMA_DISABLED, &dd->ipath_sdma_status);
  512. __clear_bit(IPATH_SDMA_DISARMED, &dd->ipath_sdma_status);
  513. __clear_bit(IPATH_SDMA_ABORTING, &dd->ipath_sdma_status);
  514. }
  515. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  516. if (!needed) {
  517. ipath_dbg("invalid attempt to restart SDMA, status 0x%08lx\n",
  518. dd->ipath_sdma_status);
  519. goto bail;
  520. }
  521. spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
  522. /*
  523. * First clear, just to be safe. Enable is only done
  524. * in chip on 0->1 transition
  525. */
  526. dd->ipath_sendctrl &= ~INFINIPATH_S_SDMAENABLE;
  527. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  528. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  529. dd->ipath_sendctrl |= INFINIPATH_S_SDMAENABLE;
  530. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  531. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  532. spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
  533. /* notify upper layers */
  534. ipath_ib_piobufavail(dd->verbs_dev);
  535. bail:
  536. return;
  537. }
  538. static inline void make_sdma_desc(struct ipath_devdata *dd,
  539. u64 *sdmadesc, u64 addr, u64 dwlen, u64 dwoffset)
  540. {
  541. WARN_ON(addr & 3);
  542. /* SDmaPhyAddr[47:32] */
  543. sdmadesc[1] = addr >> 32;
  544. /* SDmaPhyAddr[31:0] */
  545. sdmadesc[0] = (addr & 0xfffffffcULL) << 32;
  546. /* SDmaGeneration[1:0] */
  547. sdmadesc[0] |= (dd->ipath_sdma_generation & 3ULL) << 30;
  548. /* SDmaDwordCount[10:0] */
  549. sdmadesc[0] |= (dwlen & 0x7ffULL) << 16;
  550. /* SDmaBufOffset[12:2] */
  551. sdmadesc[0] |= dwoffset & 0x7ffULL;
  552. }
  553. /*
  554. * This function queues one IB packet onto the send DMA queue per call.
  555. * The caller is responsible for checking:
  556. * 1) The number of send DMA descriptor entries is less than the size of
  557. * the descriptor queue.
  558. * 2) The IB SGE addresses and lengths are 32-bit aligned
  559. * (except possibly the last SGE's length)
  560. * 3) The SGE addresses are suitable for passing to dma_map_single().
  561. */
  562. int ipath_sdma_verbs_send(struct ipath_devdata *dd,
  563. struct ipath_sge_state *ss, u32 dwords,
  564. struct ipath_verbs_txreq *tx)
  565. {
  566. unsigned long flags;
  567. struct ipath_sge *sge;
  568. int ret = 0;
  569. u16 tail;
  570. __le64 *descqp;
  571. u64 sdmadesc[2];
  572. u32 dwoffset;
  573. dma_addr_t addr;
  574. if ((tx->map_len + (dwords<<2)) > dd->ipath_ibmaxlen) {
  575. ipath_dbg("packet size %X > ibmax %X, fail\n",
  576. tx->map_len + (dwords<<2), dd->ipath_ibmaxlen);
  577. ret = -EMSGSIZE;
  578. goto fail;
  579. }
  580. spin_lock_irqsave(&dd->ipath_sdma_lock, flags);
  581. retry:
  582. if (unlikely(test_bit(IPATH_SDMA_ABORTING, &dd->ipath_sdma_status))) {
  583. ret = -EBUSY;
  584. goto unlock;
  585. }
  586. if (tx->txreq.sg_count > ipath_sdma_descq_freecnt(dd)) {
  587. if (ipath_sdma_make_progress(dd))
  588. goto retry;
  589. ret = -ENOBUFS;
  590. goto unlock;
  591. }
  592. addr = dma_map_single(&dd->pcidev->dev, tx->txreq.map_addr,
  593. tx->map_len, DMA_TO_DEVICE);
  594. if (dma_mapping_error(&dd->pcidev->dev, addr)) {
  595. ret = -EIO;
  596. goto unlock;
  597. }
  598. dwoffset = tx->map_len >> 2;
  599. make_sdma_desc(dd, sdmadesc, (u64) addr, dwoffset, 0);
  600. /* SDmaFirstDesc */
  601. sdmadesc[0] |= 1ULL << 12;
  602. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_USELARGEBUF)
  603. sdmadesc[0] |= 1ULL << 14; /* SDmaUseLargeBuf */
  604. /* write to the descq */
  605. tail = dd->ipath_sdma_descq_tail;
  606. descqp = &dd->ipath_sdma_descq[tail].qw[0];
  607. *descqp++ = cpu_to_le64(sdmadesc[0]);
  608. *descqp++ = cpu_to_le64(sdmadesc[1]);
  609. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_FREEDESC)
  610. tx->txreq.start_idx = tail;
  611. /* increment the tail */
  612. if (++tail == dd->ipath_sdma_descq_cnt) {
  613. tail = 0;
  614. descqp = &dd->ipath_sdma_descq[0].qw[0];
  615. ++dd->ipath_sdma_generation;
  616. }
  617. sge = &ss->sge;
  618. while (dwords) {
  619. u32 dw;
  620. u32 len;
  621. len = dwords << 2;
  622. if (len > sge->length)
  623. len = sge->length;
  624. if (len > sge->sge_length)
  625. len = sge->sge_length;
  626. BUG_ON(len == 0);
  627. dw = (len + 3) >> 2;
  628. addr = dma_map_single(&dd->pcidev->dev, sge->vaddr, dw << 2,
  629. DMA_TO_DEVICE);
  630. make_sdma_desc(dd, sdmadesc, (u64) addr, dw, dwoffset);
  631. /* SDmaUseLargeBuf has to be set in every descriptor */
  632. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_USELARGEBUF)
  633. sdmadesc[0] |= 1ULL << 14;
  634. /* write to the descq */
  635. *descqp++ = cpu_to_le64(sdmadesc[0]);
  636. *descqp++ = cpu_to_le64(sdmadesc[1]);
  637. /* increment the tail */
  638. if (++tail == dd->ipath_sdma_descq_cnt) {
  639. tail = 0;
  640. descqp = &dd->ipath_sdma_descq[0].qw[0];
  641. ++dd->ipath_sdma_generation;
  642. }
  643. sge->vaddr += len;
  644. sge->length -= len;
  645. sge->sge_length -= len;
  646. if (sge->sge_length == 0) {
  647. if (--ss->num_sge)
  648. *sge = *ss->sg_list++;
  649. } else if (sge->length == 0 && sge->mr != NULL) {
  650. if (++sge->n >= IPATH_SEGSZ) {
  651. if (++sge->m >= sge->mr->mapsz)
  652. break;
  653. sge->n = 0;
  654. }
  655. sge->vaddr =
  656. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  657. sge->length =
  658. sge->mr->map[sge->m]->segs[sge->n].length;
  659. }
  660. dwoffset += dw;
  661. dwords -= dw;
  662. }
  663. if (!tail)
  664. descqp = &dd->ipath_sdma_descq[dd->ipath_sdma_descq_cnt].qw[0];
  665. descqp -= 2;
  666. /* SDmaLastDesc */
  667. descqp[0] |= __constant_cpu_to_le64(1ULL << 11);
  668. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_INTREQ) {
  669. /* SDmaIntReq */
  670. descqp[0] |= __constant_cpu_to_le64(1ULL << 15);
  671. }
  672. /* Commit writes to memory and advance the tail on the chip */
  673. wmb();
  674. ipath_write_kreg(dd, dd->ipath_kregs->kr_senddmatail, tail);
  675. tx->txreq.next_descq_idx = tail;
  676. tx->txreq.callback_status = IPATH_SDMA_TXREQ_S_OK;
  677. dd->ipath_sdma_descq_tail = tail;
  678. dd->ipath_sdma_descq_added += tx->txreq.sg_count;
  679. list_add_tail(&tx->txreq.list, &dd->ipath_sdma_activelist);
  680. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_VL15)
  681. vl15_watchdog_enq(dd);
  682. unlock:
  683. spin_unlock_irqrestore(&dd->ipath_sdma_lock, flags);
  684. fail:
  685. return ret;
  686. }