qib_tx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (c) 2008, 2009, 2010 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/pci.h>
  34. #include <linux/io.h>
  35. #include <linux/delay.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/vmalloc.h>
  38. #include "qib.h"
  39. static unsigned qib_hol_timeout_ms = 3000;
  40. module_param_named(hol_timeout_ms, qib_hol_timeout_ms, uint, S_IRUGO);
  41. MODULE_PARM_DESC(hol_timeout_ms,
  42. "duration of user app suspension after link failure");
  43. unsigned qib_sdma_fetch_arb = 1;
  44. module_param_named(fetch_arb, qib_sdma_fetch_arb, uint, S_IRUGO);
  45. MODULE_PARM_DESC(fetch_arb, "IBA7220: change SDMA descriptor arbitration");
  46. /**
  47. * qib_disarm_piobufs - cancel a range of PIO buffers
  48. * @dd: the qlogic_ib device
  49. * @first: the first PIO buffer to cancel
  50. * @cnt: the number of PIO buffers to cancel
  51. *
  52. * Cancel a range of PIO buffers. Used at user process close,
  53. * in case it died while writing to a PIO buffer.
  54. */
  55. void qib_disarm_piobufs(struct qib_devdata *dd, unsigned first, unsigned cnt)
  56. {
  57. unsigned long flags;
  58. unsigned i;
  59. unsigned last;
  60. last = first + cnt;
  61. spin_lock_irqsave(&dd->pioavail_lock, flags);
  62. for (i = first; i < last; i++) {
  63. __clear_bit(i, dd->pio_need_disarm);
  64. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
  65. }
  66. spin_unlock_irqrestore(&dd->pioavail_lock, flags);
  67. }
  68. /*
  69. * This is called by a user process when it sees the DISARM_BUFS event
  70. * bit is set.
  71. */
  72. int qib_disarm_piobufs_ifneeded(struct qib_ctxtdata *rcd)
  73. {
  74. struct qib_devdata *dd = rcd->dd;
  75. unsigned i;
  76. unsigned last;
  77. unsigned n = 0;
  78. last = rcd->pio_base + rcd->piocnt;
  79. /*
  80. * Don't need uctxt_lock here, since user has called in to us.
  81. * Clear at start in case more interrupts set bits while we
  82. * are disarming
  83. */
  84. if (rcd->user_event_mask) {
  85. /*
  86. * subctxt_cnt is 0 if not shared, so do base
  87. * separately, first, then remaining subctxt, if any
  88. */
  89. clear_bit(_QIB_EVENT_DISARM_BUFS_BIT, &rcd->user_event_mask[0]);
  90. for (i = 1; i < rcd->subctxt_cnt; i++)
  91. clear_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  92. &rcd->user_event_mask[i]);
  93. }
  94. spin_lock_irq(&dd->pioavail_lock);
  95. for (i = rcd->pio_base; i < last; i++) {
  96. if (__test_and_clear_bit(i, dd->pio_need_disarm)) {
  97. n++;
  98. dd->f_sendctrl(rcd->ppd, QIB_SENDCTRL_DISARM_BUF(i));
  99. }
  100. }
  101. spin_unlock_irq(&dd->pioavail_lock);
  102. return 0;
  103. }
  104. static struct qib_pportdata *is_sdma_buf(struct qib_devdata *dd, unsigned i)
  105. {
  106. struct qib_pportdata *ppd;
  107. unsigned pidx;
  108. for (pidx = 0; pidx < dd->num_pports; pidx++) {
  109. ppd = dd->pport + pidx;
  110. if (i >= ppd->sdma_state.first_sendbuf &&
  111. i < ppd->sdma_state.last_sendbuf)
  112. return ppd;
  113. }
  114. return NULL;
  115. }
  116. /*
  117. * Return true if send buffer is being used by a user context.
  118. * Sets _QIB_EVENT_DISARM_BUFS_BIT in user_event_mask as a side effect
  119. */
  120. static int find_ctxt(struct qib_devdata *dd, unsigned bufn)
  121. {
  122. struct qib_ctxtdata *rcd;
  123. unsigned ctxt;
  124. int ret = 0;
  125. spin_lock(&dd->uctxt_lock);
  126. for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
  127. rcd = dd->rcd[ctxt];
  128. if (!rcd || bufn < rcd->pio_base ||
  129. bufn >= rcd->pio_base + rcd->piocnt)
  130. continue;
  131. if (rcd->user_event_mask) {
  132. int i;
  133. /*
  134. * subctxt_cnt is 0 if not shared, so do base
  135. * separately, first, then remaining subctxt, if any
  136. */
  137. set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  138. &rcd->user_event_mask[0]);
  139. for (i = 1; i < rcd->subctxt_cnt; i++)
  140. set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  141. &rcd->user_event_mask[i]);
  142. }
  143. ret = 1;
  144. break;
  145. }
  146. spin_unlock(&dd->uctxt_lock);
  147. return ret;
  148. }
  149. /*
  150. * Disarm a set of send buffers. If the buffer might be actively being
  151. * written to, mark the buffer to be disarmed later when it is not being
  152. * written to.
  153. *
  154. * This should only be called from the IRQ error handler.
  155. */
  156. void qib_disarm_piobufs_set(struct qib_devdata *dd, unsigned long *mask,
  157. unsigned cnt)
  158. {
  159. struct qib_pportdata *ppd, *pppd[QIB_MAX_IB_PORTS];
  160. unsigned i;
  161. unsigned long flags;
  162. for (i = 0; i < dd->num_pports; i++)
  163. pppd[i] = NULL;
  164. for (i = 0; i < cnt; i++) {
  165. int which;
  166. if (!test_bit(i, mask))
  167. continue;
  168. /*
  169. * If the buffer is owned by the DMA hardware,
  170. * reset the DMA engine.
  171. */
  172. ppd = is_sdma_buf(dd, i);
  173. if (ppd) {
  174. pppd[ppd->port] = ppd;
  175. continue;
  176. }
  177. /*
  178. * If the kernel is writing the buffer or the buffer is
  179. * owned by a user process, we can't clear it yet.
  180. */
  181. spin_lock_irqsave(&dd->pioavail_lock, flags);
  182. if (test_bit(i, dd->pio_writing) ||
  183. (!test_bit(i << 1, dd->pioavailkernel) &&
  184. find_ctxt(dd, i))) {
  185. __set_bit(i, dd->pio_need_disarm);
  186. which = 0;
  187. } else {
  188. which = 1;
  189. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
  190. }
  191. spin_unlock_irqrestore(&dd->pioavail_lock, flags);
  192. }
  193. /* do cancel_sends once per port that had sdma piobufs in error */
  194. for (i = 0; i < dd->num_pports; i++)
  195. if (pppd[i])
  196. qib_cancel_sends(pppd[i]);
  197. }
  198. /**
  199. * update_send_bufs - update shadow copy of the PIO availability map
  200. * @dd: the qlogic_ib device
  201. *
  202. * called whenever our local copy indicates we have run out of send buffers
  203. */
  204. static void update_send_bufs(struct qib_devdata *dd)
  205. {
  206. unsigned long flags;
  207. unsigned i;
  208. const unsigned piobregs = dd->pioavregs;
  209. /*
  210. * If the generation (check) bits have changed, then we update the
  211. * busy bit for the corresponding PIO buffer. This algorithm will
  212. * modify positions to the value they already have in some cases
  213. * (i.e., no change), but it's faster than changing only the bits
  214. * that have changed.
  215. *
  216. * We would like to do this atomicly, to avoid spinlocks in the
  217. * critical send path, but that's not really possible, given the
  218. * type of changes, and that this routine could be called on
  219. * multiple cpu's simultaneously, so we lock in this routine only,
  220. * to avoid conflicting updates; all we change is the shadow, and
  221. * it's a single 64 bit memory location, so by definition the update
  222. * is atomic in terms of what other cpu's can see in testing the
  223. * bits. The spin_lock overhead isn't too bad, since it only
  224. * happens when all buffers are in use, so only cpu overhead, not
  225. * latency or bandwidth is affected.
  226. */
  227. if (!dd->pioavailregs_dma)
  228. return;
  229. spin_lock_irqsave(&dd->pioavail_lock, flags);
  230. for (i = 0; i < piobregs; i++) {
  231. u64 pchbusy, pchg, piov, pnew;
  232. piov = le64_to_cpu(dd->pioavailregs_dma[i]);
  233. pchg = dd->pioavailkernel[i] &
  234. ~(dd->pioavailshadow[i] ^ piov);
  235. pchbusy = pchg << QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT;
  236. if (pchg && (pchbusy & dd->pioavailshadow[i])) {
  237. pnew = dd->pioavailshadow[i] & ~pchbusy;
  238. pnew |= piov & pchbusy;
  239. dd->pioavailshadow[i] = pnew;
  240. }
  241. }
  242. spin_unlock_irqrestore(&dd->pioavail_lock, flags);
  243. }
  244. /*
  245. * Debugging code and stats updates if no pio buffers available.
  246. */
  247. static noinline void no_send_bufs(struct qib_devdata *dd)
  248. {
  249. dd->upd_pio_shadow = 1;
  250. /* not atomic, but if we lose a stat count in a while, that's OK */
  251. qib_stats.sps_nopiobufs++;
  252. }
  253. /*
  254. * Common code for normal driver send buffer allocation, and reserved
  255. * allocation.
  256. *
  257. * Do appropriate marking as busy, etc.
  258. * Returns buffer pointer if one is found, otherwise NULL.
  259. */
  260. u32 __iomem *qib_getsendbuf_range(struct qib_devdata *dd, u32 *pbufnum,
  261. u32 first, u32 last)
  262. {
  263. unsigned i, j, updated = 0;
  264. unsigned nbufs;
  265. unsigned long flags;
  266. unsigned long *shadow = dd->pioavailshadow;
  267. u32 __iomem *buf;
  268. if (!(dd->flags & QIB_PRESENT))
  269. return NULL;
  270. nbufs = last - first + 1; /* number in range to check */
  271. if (dd->upd_pio_shadow) {
  272. /*
  273. * Minor optimization. If we had no buffers on last call,
  274. * start out by doing the update; continue and do scan even
  275. * if no buffers were updated, to be paranoid.
  276. */
  277. update_send_bufs(dd);
  278. updated++;
  279. }
  280. i = first;
  281. rescan:
  282. /*
  283. * While test_and_set_bit() is atomic, we do that and then the
  284. * change_bit(), and the pair is not. See if this is the cause
  285. * of the remaining armlaunch errors.
  286. */
  287. spin_lock_irqsave(&dd->pioavail_lock, flags);
  288. for (j = 0; j < nbufs; j++, i++) {
  289. if (i > last)
  290. i = first;
  291. if (__test_and_set_bit((2 * i) + 1, shadow))
  292. continue;
  293. /* flip generation bit */
  294. __change_bit(2 * i, shadow);
  295. /* remember that the buffer can be written to now */
  296. __set_bit(i, dd->pio_writing);
  297. break;
  298. }
  299. spin_unlock_irqrestore(&dd->pioavail_lock, flags);
  300. if (j == nbufs) {
  301. if (!updated) {
  302. /*
  303. * First time through; shadow exhausted, but may be
  304. * buffers available, try an update and then rescan.
  305. */
  306. update_send_bufs(dd);
  307. updated++;
  308. i = first;
  309. goto rescan;
  310. }
  311. no_send_bufs(dd);
  312. buf = NULL;
  313. } else {
  314. if (i < dd->piobcnt2k)
  315. buf = (u32 __iomem *)(dd->pio2kbase +
  316. i * dd->palign);
  317. else if (i < dd->piobcnt2k + dd->piobcnt4k || !dd->piovl15base)
  318. buf = (u32 __iomem *)(dd->pio4kbase +
  319. (i - dd->piobcnt2k) * dd->align4k);
  320. else
  321. buf = (u32 __iomem *)(dd->piovl15base +
  322. (i - (dd->piobcnt2k + dd->piobcnt4k)) *
  323. dd->align4k);
  324. if (pbufnum)
  325. *pbufnum = i;
  326. dd->upd_pio_shadow = 0;
  327. }
  328. return buf;
  329. }
  330. /*
  331. * Record that the caller is finished writing to the buffer so we don't
  332. * disarm it while it is being written and disarm it now if needed.
  333. */
  334. void qib_sendbuf_done(struct qib_devdata *dd, unsigned n)
  335. {
  336. unsigned long flags;
  337. spin_lock_irqsave(&dd->pioavail_lock, flags);
  338. __clear_bit(n, dd->pio_writing);
  339. if (__test_and_clear_bit(n, dd->pio_need_disarm))
  340. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(n));
  341. spin_unlock_irqrestore(&dd->pioavail_lock, flags);
  342. }
  343. /**
  344. * qib_chg_pioavailkernel - change which send buffers are available for kernel
  345. * @dd: the qlogic_ib device
  346. * @start: the starting send buffer number
  347. * @len: the number of send buffers
  348. * @avail: true if the buffers are available for kernel use, false otherwise
  349. */
  350. void qib_chg_pioavailkernel(struct qib_devdata *dd, unsigned start,
  351. unsigned len, u32 avail, struct qib_ctxtdata *rcd)
  352. {
  353. unsigned long flags;
  354. unsigned end;
  355. unsigned ostart = start;
  356. /* There are two bits per send buffer (busy and generation) */
  357. start *= 2;
  358. end = start + len * 2;
  359. spin_lock_irqsave(&dd->pioavail_lock, flags);
  360. /* Set or clear the busy bit in the shadow. */
  361. while (start < end) {
  362. if (avail) {
  363. unsigned long dma;
  364. int i;
  365. /*
  366. * The BUSY bit will never be set, because we disarm
  367. * the user buffers before we hand them back to the
  368. * kernel. We do have to make sure the generation
  369. * bit is set correctly in shadow, since it could
  370. * have changed many times while allocated to user.
  371. * We can't use the bitmap functions on the full
  372. * dma array because it is always little-endian, so
  373. * we have to flip to host-order first.
  374. * BITS_PER_LONG is slightly wrong, since it's
  375. * always 64 bits per register in chip...
  376. * We only work on 64 bit kernels, so that's OK.
  377. */
  378. i = start / BITS_PER_LONG;
  379. __clear_bit(QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT + start,
  380. dd->pioavailshadow);
  381. dma = (unsigned long)
  382. le64_to_cpu(dd->pioavailregs_dma[i]);
  383. if (test_bit((QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
  384. start) % BITS_PER_LONG, &dma))
  385. __set_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
  386. start, dd->pioavailshadow);
  387. else
  388. __clear_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT
  389. + start, dd->pioavailshadow);
  390. __set_bit(start, dd->pioavailkernel);
  391. } else {
  392. __set_bit(start + QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT,
  393. dd->pioavailshadow);
  394. __clear_bit(start, dd->pioavailkernel);
  395. }
  396. start += 2;
  397. }
  398. spin_unlock_irqrestore(&dd->pioavail_lock, flags);
  399. dd->f_txchk_change(dd, ostart, len, avail, rcd);
  400. }
  401. /*
  402. * Flush all sends that might be in the ready to send state, as well as any
  403. * that are in the process of being sent. Used whenever we need to be
  404. * sure the send side is idle. Cleans up all buffer state by canceling
  405. * all pio buffers, and issuing an abort, which cleans up anything in the
  406. * launch fifo. The cancel is superfluous on some chip versions, but
  407. * it's safer to always do it.
  408. * PIOAvail bits are updated by the chip as if a normal send had happened.
  409. */
  410. void qib_cancel_sends(struct qib_pportdata *ppd)
  411. {
  412. struct qib_devdata *dd = ppd->dd;
  413. struct qib_ctxtdata *rcd;
  414. unsigned long flags;
  415. unsigned ctxt;
  416. unsigned i;
  417. unsigned last;
  418. /*
  419. * Tell PSM to disarm buffers again before trying to reuse them.
  420. * We need to be sure the rcd doesn't change out from under us
  421. * while we do so. We hold the two locks sequentially. We might
  422. * needlessly set some need_disarm bits as a result, if the
  423. * context is closed after we release the uctxt_lock, but that's
  424. * fairly benign, and safer than nesting the locks.
  425. */
  426. for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
  427. spin_lock_irqsave(&dd->uctxt_lock, flags);
  428. rcd = dd->rcd[ctxt];
  429. if (rcd && rcd->ppd == ppd) {
  430. last = rcd->pio_base + rcd->piocnt;
  431. if (rcd->user_event_mask) {
  432. /*
  433. * subctxt_cnt is 0 if not shared, so do base
  434. * separately, first, then remaining subctxt,
  435. * if any
  436. */
  437. set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  438. &rcd->user_event_mask[0]);
  439. for (i = 1; i < rcd->subctxt_cnt; i++)
  440. set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  441. &rcd->user_event_mask[i]);
  442. }
  443. i = rcd->pio_base;
  444. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  445. spin_lock_irqsave(&dd->pioavail_lock, flags);
  446. for (; i < last; i++)
  447. __set_bit(i, dd->pio_need_disarm);
  448. spin_unlock_irqrestore(&dd->pioavail_lock, flags);
  449. } else
  450. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  451. }
  452. if (!(dd->flags & QIB_HAS_SEND_DMA))
  453. dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_ALL |
  454. QIB_SENDCTRL_FLUSH);
  455. }
  456. /*
  457. * Force an update of in-memory copy of the pioavail registers, when
  458. * needed for any of a variety of reasons.
  459. * If already off, this routine is a nop, on the assumption that the
  460. * caller (or set of callers) will "do the right thing".
  461. * This is a per-device operation, so just the first port.
  462. */
  463. void qib_force_pio_avail_update(struct qib_devdata *dd)
  464. {
  465. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
  466. }
  467. void qib_hol_down(struct qib_pportdata *ppd)
  468. {
  469. /*
  470. * Cancel sends when the link goes DOWN so that we aren't doing it
  471. * at INIT when we might be trying to send SMI packets.
  472. */
  473. if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG))
  474. qib_cancel_sends(ppd);
  475. }
  476. /*
  477. * Link is at INIT.
  478. * We start the HoL timer so we can detect stuck packets blocking SMP replies.
  479. * Timer may already be running, so use mod_timer, not add_timer.
  480. */
  481. void qib_hol_init(struct qib_pportdata *ppd)
  482. {
  483. if (ppd->hol_state != QIB_HOL_INIT) {
  484. ppd->hol_state = QIB_HOL_INIT;
  485. mod_timer(&ppd->hol_timer,
  486. jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
  487. }
  488. }
  489. /*
  490. * Link is up, continue any user processes, and ensure timer
  491. * is a nop, if running. Let timer keep running, if set; it
  492. * will nop when it sees the link is up.
  493. */
  494. void qib_hol_up(struct qib_pportdata *ppd)
  495. {
  496. ppd->hol_state = QIB_HOL_UP;
  497. }
  498. /*
  499. * This is only called via the timer.
  500. */
  501. void qib_hol_event(unsigned long opaque)
  502. {
  503. struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;
  504. /* If hardware error, etc, skip. */
  505. if (!(ppd->dd->flags & QIB_INITTED))
  506. return;
  507. if (ppd->hol_state != QIB_HOL_UP) {
  508. /*
  509. * Try to flush sends in case a stuck packet is blocking
  510. * SMP replies.
  511. */
  512. qib_hol_down(ppd);
  513. mod_timer(&ppd->hol_timer,
  514. jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
  515. }
  516. }