qib_sdma.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Copyright (c) 2012 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2007 - 2012 QLogic Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/spinlock.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/moduleparam.h>
  36. #include "qib.h"
  37. #include "qib_common.h"
  38. /* default pio off, sdma on */
  39. static ushort sdma_descq_cnt = 256;
  40. module_param_named(sdma_descq_cnt, sdma_descq_cnt, ushort, S_IRUGO);
  41. MODULE_PARM_DESC(sdma_descq_cnt, "Number of SDMA descq entries");
  42. /*
  43. * Bits defined in the send DMA descriptor.
  44. */
  45. #define SDMA_DESC_LAST (1ULL << 11)
  46. #define SDMA_DESC_FIRST (1ULL << 12)
  47. #define SDMA_DESC_DMA_HEAD (1ULL << 13)
  48. #define SDMA_DESC_USE_LARGE_BUF (1ULL << 14)
  49. #define SDMA_DESC_INTR (1ULL << 15)
  50. #define SDMA_DESC_COUNT_LSB 16
  51. #define SDMA_DESC_GEN_LSB 30
  52. char *qib_sdma_state_names[] = {
  53. [qib_sdma_state_s00_hw_down] = "s00_HwDown",
  54. [qib_sdma_state_s10_hw_start_up_wait] = "s10_HwStartUpWait",
  55. [qib_sdma_state_s20_idle] = "s20_Idle",
  56. [qib_sdma_state_s30_sw_clean_up_wait] = "s30_SwCleanUpWait",
  57. [qib_sdma_state_s40_hw_clean_up_wait] = "s40_HwCleanUpWait",
  58. [qib_sdma_state_s50_hw_halt_wait] = "s50_HwHaltWait",
  59. [qib_sdma_state_s99_running] = "s99_Running",
  60. };
  61. char *qib_sdma_event_names[] = {
  62. [qib_sdma_event_e00_go_hw_down] = "e00_GoHwDown",
  63. [qib_sdma_event_e10_go_hw_start] = "e10_GoHwStart",
  64. [qib_sdma_event_e20_hw_started] = "e20_HwStarted",
  65. [qib_sdma_event_e30_go_running] = "e30_GoRunning",
  66. [qib_sdma_event_e40_sw_cleaned] = "e40_SwCleaned",
  67. [qib_sdma_event_e50_hw_cleaned] = "e50_HwCleaned",
  68. [qib_sdma_event_e60_hw_halted] = "e60_HwHalted",
  69. [qib_sdma_event_e70_go_idle] = "e70_GoIdle",
  70. [qib_sdma_event_e7220_err_halted] = "e7220_ErrHalted",
  71. [qib_sdma_event_e7322_err_halted] = "e7322_ErrHalted",
  72. [qib_sdma_event_e90_timer_tick] = "e90_TimerTick",
  73. };
  74. /* declare all statics here rather than keep sorting */
  75. static int alloc_sdma(struct qib_pportdata *);
  76. static void sdma_complete(struct kref *);
  77. static void sdma_finalput(struct qib_sdma_state *);
  78. static void sdma_get(struct qib_sdma_state *);
  79. static void sdma_put(struct qib_sdma_state *);
  80. static void sdma_set_state(struct qib_pportdata *, enum qib_sdma_states);
  81. static void sdma_start_sw_clean_up(struct qib_pportdata *);
  82. static void sdma_sw_clean_up_task(unsigned long);
  83. static void unmap_desc(struct qib_pportdata *, unsigned);
  84. static void sdma_get(struct qib_sdma_state *ss)
  85. {
  86. kref_get(&ss->kref);
  87. }
  88. static void sdma_complete(struct kref *kref)
  89. {
  90. struct qib_sdma_state *ss =
  91. container_of(kref, struct qib_sdma_state, kref);
  92. complete(&ss->comp);
  93. }
  94. static void sdma_put(struct qib_sdma_state *ss)
  95. {
  96. kref_put(&ss->kref, sdma_complete);
  97. }
  98. static void sdma_finalput(struct qib_sdma_state *ss)
  99. {
  100. sdma_put(ss);
  101. wait_for_completion(&ss->comp);
  102. }
  103. /*
  104. * Complete all the sdma requests on the active list, in the correct
  105. * order, and with appropriate processing. Called when cleaning up
  106. * after sdma shutdown, and when new sdma requests are submitted for
  107. * a link that is down. This matches what is done for requests
  108. * that complete normally, it's just the full list.
  109. *
  110. * Must be called with sdma_lock held
  111. */
  112. static void clear_sdma_activelist(struct qib_pportdata *ppd)
  113. {
  114. struct qib_sdma_txreq *txp, *txp_next;
  115. list_for_each_entry_safe(txp, txp_next, &ppd->sdma_activelist, list) {
  116. list_del_init(&txp->list);
  117. if (txp->flags & QIB_SDMA_TXREQ_F_FREEDESC) {
  118. unsigned idx;
  119. idx = txp->start_idx;
  120. while (idx != txp->next_descq_idx) {
  121. unmap_desc(ppd, idx);
  122. if (++idx == ppd->sdma_descq_cnt)
  123. idx = 0;
  124. }
  125. }
  126. if (txp->callback)
  127. (*txp->callback)(txp, QIB_SDMA_TXREQ_S_ABORTED);
  128. }
  129. }
  130. static void sdma_sw_clean_up_task(unsigned long opaque)
  131. {
  132. struct qib_pportdata *ppd = (struct qib_pportdata *) opaque;
  133. unsigned long flags;
  134. spin_lock_irqsave(&ppd->sdma_lock, flags);
  135. /*
  136. * At this point, the following should always be true:
  137. * - We are halted, so no more descriptors are getting retired.
  138. * - We are not running, so no one is submitting new work.
  139. * - Only we can send the e40_sw_cleaned, so we can't start
  140. * running again until we say so. So, the active list and
  141. * descq are ours to play with.
  142. */
  143. /* Process all retired requests. */
  144. qib_sdma_make_progress(ppd);
  145. clear_sdma_activelist(ppd);
  146. /*
  147. * Resync count of added and removed. It is VERY important that
  148. * sdma_descq_removed NEVER decrement - user_sdma depends on it.
  149. */
  150. ppd->sdma_descq_removed = ppd->sdma_descq_added;
  151. /*
  152. * Reset our notion of head and tail.
  153. * Note that the HW registers will be reset when switching states
  154. * due to calling __qib_sdma_process_event() below.
  155. */
  156. ppd->sdma_descq_tail = 0;
  157. ppd->sdma_descq_head = 0;
  158. ppd->sdma_head_dma[0] = 0;
  159. ppd->sdma_generation = 0;
  160. __qib_sdma_process_event(ppd, qib_sdma_event_e40_sw_cleaned);
  161. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  162. }
  163. /*
  164. * This is called when changing to state qib_sdma_state_s10_hw_start_up_wait
  165. * as a result of send buffer errors or send DMA descriptor errors.
  166. * We want to disarm the buffers in these cases.
  167. */
  168. static void sdma_hw_start_up(struct qib_pportdata *ppd)
  169. {
  170. struct qib_sdma_state *ss = &ppd->sdma_state;
  171. unsigned bufno;
  172. for (bufno = ss->first_sendbuf; bufno < ss->last_sendbuf; ++bufno)
  173. ppd->dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_BUF(bufno));
  174. ppd->dd->f_sdma_hw_start_up(ppd);
  175. }
  176. static void sdma_sw_tear_down(struct qib_pportdata *ppd)
  177. {
  178. struct qib_sdma_state *ss = &ppd->sdma_state;
  179. /* Releasing this reference means the state machine has stopped. */
  180. sdma_put(ss);
  181. }
  182. static void sdma_start_sw_clean_up(struct qib_pportdata *ppd)
  183. {
  184. tasklet_hi_schedule(&ppd->sdma_sw_clean_up_task);
  185. }
  186. static void sdma_set_state(struct qib_pportdata *ppd,
  187. enum qib_sdma_states next_state)
  188. {
  189. struct qib_sdma_state *ss = &ppd->sdma_state;
  190. struct sdma_set_state_action *action = ss->set_state_action;
  191. unsigned op = 0;
  192. /* debugging bookkeeping */
  193. ss->previous_state = ss->current_state;
  194. ss->previous_op = ss->current_op;
  195. ss->current_state = next_state;
  196. if (action[next_state].op_enable)
  197. op |= QIB_SDMA_SENDCTRL_OP_ENABLE;
  198. if (action[next_state].op_intenable)
  199. op |= QIB_SDMA_SENDCTRL_OP_INTENABLE;
  200. if (action[next_state].op_halt)
  201. op |= QIB_SDMA_SENDCTRL_OP_HALT;
  202. if (action[next_state].op_drain)
  203. op |= QIB_SDMA_SENDCTRL_OP_DRAIN;
  204. if (action[next_state].go_s99_running_tofalse)
  205. ss->go_s99_running = 0;
  206. if (action[next_state].go_s99_running_totrue)
  207. ss->go_s99_running = 1;
  208. ss->current_op = op;
  209. ppd->dd->f_sdma_sendctrl(ppd, ss->current_op);
  210. }
  211. static void unmap_desc(struct qib_pportdata *ppd, unsigned head)
  212. {
  213. __le64 *descqp = &ppd->sdma_descq[head].qw[0];
  214. u64 desc[2];
  215. dma_addr_t addr;
  216. size_t len;
  217. desc[0] = le64_to_cpu(descqp[0]);
  218. desc[1] = le64_to_cpu(descqp[1]);
  219. addr = (desc[1] << 32) | (desc[0] >> 32);
  220. len = (desc[0] >> 14) & (0x7ffULL << 2);
  221. dma_unmap_single(&ppd->dd->pcidev->dev, addr, len, DMA_TO_DEVICE);
  222. }
  223. static int alloc_sdma(struct qib_pportdata *ppd)
  224. {
  225. ppd->sdma_descq_cnt = sdma_descq_cnt;
  226. if (!ppd->sdma_descq_cnt)
  227. ppd->sdma_descq_cnt = 256;
  228. /* Allocate memory for SendDMA descriptor FIFO */
  229. ppd->sdma_descq = dma_alloc_coherent(&ppd->dd->pcidev->dev,
  230. ppd->sdma_descq_cnt * sizeof(u64[2]), &ppd->sdma_descq_phys,
  231. GFP_KERNEL);
  232. if (!ppd->sdma_descq) {
  233. qib_dev_err(ppd->dd,
  234. "failed to allocate SendDMA descriptor FIFO memory\n");
  235. goto bail;
  236. }
  237. /* Allocate memory for DMA of head register to memory */
  238. ppd->sdma_head_dma = dma_alloc_coherent(&ppd->dd->pcidev->dev,
  239. PAGE_SIZE, &ppd->sdma_head_phys, GFP_KERNEL);
  240. if (!ppd->sdma_head_dma) {
  241. qib_dev_err(ppd->dd,
  242. "failed to allocate SendDMA head memory\n");
  243. goto cleanup_descq;
  244. }
  245. ppd->sdma_head_dma[0] = 0;
  246. return 0;
  247. cleanup_descq:
  248. dma_free_coherent(&ppd->dd->pcidev->dev,
  249. ppd->sdma_descq_cnt * sizeof(u64[2]), (void *)ppd->sdma_descq,
  250. ppd->sdma_descq_phys);
  251. ppd->sdma_descq = NULL;
  252. ppd->sdma_descq_phys = 0;
  253. bail:
  254. ppd->sdma_descq_cnt = 0;
  255. return -ENOMEM;
  256. }
  257. static void free_sdma(struct qib_pportdata *ppd)
  258. {
  259. struct qib_devdata *dd = ppd->dd;
  260. if (ppd->sdma_head_dma) {
  261. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  262. (void *)ppd->sdma_head_dma,
  263. ppd->sdma_head_phys);
  264. ppd->sdma_head_dma = NULL;
  265. ppd->sdma_head_phys = 0;
  266. }
  267. if (ppd->sdma_descq) {
  268. dma_free_coherent(&dd->pcidev->dev,
  269. ppd->sdma_descq_cnt * sizeof(u64[2]),
  270. ppd->sdma_descq, ppd->sdma_descq_phys);
  271. ppd->sdma_descq = NULL;
  272. ppd->sdma_descq_phys = 0;
  273. }
  274. }
  275. static inline void make_sdma_desc(struct qib_pportdata *ppd,
  276. u64 *sdmadesc, u64 addr, u64 dwlen,
  277. u64 dwoffset)
  278. {
  279. WARN_ON(addr & 3);
  280. /* SDmaPhyAddr[47:32] */
  281. sdmadesc[1] = addr >> 32;
  282. /* SDmaPhyAddr[31:0] */
  283. sdmadesc[0] = (addr & 0xfffffffcULL) << 32;
  284. /* SDmaGeneration[1:0] */
  285. sdmadesc[0] |= (ppd->sdma_generation & 3ULL) <<
  286. SDMA_DESC_GEN_LSB;
  287. /* SDmaDwordCount[10:0] */
  288. sdmadesc[0] |= (dwlen & 0x7ffULL) << SDMA_DESC_COUNT_LSB;
  289. /* SDmaBufOffset[12:2] */
  290. sdmadesc[0] |= dwoffset & 0x7ffULL;
  291. }
  292. /* sdma_lock must be held */
  293. int qib_sdma_make_progress(struct qib_pportdata *ppd)
  294. {
  295. struct list_head *lp = NULL;
  296. struct qib_sdma_txreq *txp = NULL;
  297. struct qib_devdata *dd = ppd->dd;
  298. int progress = 0;
  299. u16 hwhead;
  300. u16 idx = 0;
  301. hwhead = dd->f_sdma_gethead(ppd);
  302. /* The reason for some of the complexity of this code is that
  303. * not all descriptors have corresponding txps. So, we have to
  304. * be able to skip over descs until we wander into the range of
  305. * the next txp on the list.
  306. */
  307. if (!list_empty(&ppd->sdma_activelist)) {
  308. lp = ppd->sdma_activelist.next;
  309. txp = list_entry(lp, struct qib_sdma_txreq, list);
  310. idx = txp->start_idx;
  311. }
  312. while (ppd->sdma_descq_head != hwhead) {
  313. /* if desc is part of this txp, unmap if needed */
  314. if (txp && (txp->flags & QIB_SDMA_TXREQ_F_FREEDESC) &&
  315. (idx == ppd->sdma_descq_head)) {
  316. unmap_desc(ppd, ppd->sdma_descq_head);
  317. if (++idx == ppd->sdma_descq_cnt)
  318. idx = 0;
  319. }
  320. /* increment dequed desc count */
  321. ppd->sdma_descq_removed++;
  322. /* advance head, wrap if needed */
  323. if (++ppd->sdma_descq_head == ppd->sdma_descq_cnt)
  324. ppd->sdma_descq_head = 0;
  325. /* if now past this txp's descs, do the callback */
  326. if (txp && txp->next_descq_idx == ppd->sdma_descq_head) {
  327. /* remove from active list */
  328. list_del_init(&txp->list);
  329. if (txp->callback)
  330. (*txp->callback)(txp, QIB_SDMA_TXREQ_S_OK);
  331. /* see if there is another txp */
  332. if (list_empty(&ppd->sdma_activelist))
  333. txp = NULL;
  334. else {
  335. lp = ppd->sdma_activelist.next;
  336. txp = list_entry(lp, struct qib_sdma_txreq,
  337. list);
  338. idx = txp->start_idx;
  339. }
  340. }
  341. progress = 1;
  342. }
  343. if (progress)
  344. qib_verbs_sdma_desc_avail(ppd, qib_sdma_descq_freecnt(ppd));
  345. return progress;
  346. }
  347. /*
  348. * This is called from interrupt context.
  349. */
  350. void qib_sdma_intr(struct qib_pportdata *ppd)
  351. {
  352. unsigned long flags;
  353. spin_lock_irqsave(&ppd->sdma_lock, flags);
  354. __qib_sdma_intr(ppd);
  355. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  356. }
  357. void __qib_sdma_intr(struct qib_pportdata *ppd)
  358. {
  359. if (__qib_sdma_running(ppd)) {
  360. qib_sdma_make_progress(ppd);
  361. if (!list_empty(&ppd->sdma_userpending))
  362. qib_user_sdma_send_desc(ppd, &ppd->sdma_userpending);
  363. }
  364. }
  365. int qib_setup_sdma(struct qib_pportdata *ppd)
  366. {
  367. struct qib_devdata *dd = ppd->dd;
  368. unsigned long flags;
  369. int ret = 0;
  370. ret = alloc_sdma(ppd);
  371. if (ret)
  372. goto bail;
  373. /* set consistent sdma state */
  374. ppd->dd->f_sdma_init_early(ppd);
  375. spin_lock_irqsave(&ppd->sdma_lock, flags);
  376. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  377. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  378. /* set up reference counting */
  379. kref_init(&ppd->sdma_state.kref);
  380. init_completion(&ppd->sdma_state.comp);
  381. ppd->sdma_generation = 0;
  382. ppd->sdma_descq_head = 0;
  383. ppd->sdma_descq_removed = 0;
  384. ppd->sdma_descq_added = 0;
  385. ppd->sdma_intrequest = 0;
  386. INIT_LIST_HEAD(&ppd->sdma_userpending);
  387. INIT_LIST_HEAD(&ppd->sdma_activelist);
  388. tasklet_init(&ppd->sdma_sw_clean_up_task, sdma_sw_clean_up_task,
  389. (unsigned long)ppd);
  390. ret = dd->f_init_sdma_regs(ppd);
  391. if (ret)
  392. goto bail_alloc;
  393. qib_sdma_process_event(ppd, qib_sdma_event_e10_go_hw_start);
  394. return 0;
  395. bail_alloc:
  396. qib_teardown_sdma(ppd);
  397. bail:
  398. return ret;
  399. }
  400. void qib_teardown_sdma(struct qib_pportdata *ppd)
  401. {
  402. qib_sdma_process_event(ppd, qib_sdma_event_e00_go_hw_down);
  403. /*
  404. * This waits for the state machine to exit so it is not
  405. * necessary to kill the sdma_sw_clean_up_task to make sure
  406. * it is not running.
  407. */
  408. sdma_finalput(&ppd->sdma_state);
  409. free_sdma(ppd);
  410. }
  411. int qib_sdma_running(struct qib_pportdata *ppd)
  412. {
  413. unsigned long flags;
  414. int ret;
  415. spin_lock_irqsave(&ppd->sdma_lock, flags);
  416. ret = __qib_sdma_running(ppd);
  417. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  418. return ret;
  419. }
  420. /*
  421. * Complete a request when sdma not running; likely only request
  422. * but to simplify the code, always queue it, then process the full
  423. * activelist. We process the entire list to ensure that this particular
  424. * request does get it's callback, but in the correct order.
  425. * Must be called with sdma_lock held
  426. */
  427. static void complete_sdma_err_req(struct qib_pportdata *ppd,
  428. struct qib_verbs_txreq *tx)
  429. {
  430. struct qib_qp_priv *priv = tx->qp->priv;
  431. atomic_inc(&priv->s_dma_busy);
  432. /* no sdma descriptors, so no unmap_desc */
  433. tx->txreq.start_idx = 0;
  434. tx->txreq.next_descq_idx = 0;
  435. list_add_tail(&tx->txreq.list, &ppd->sdma_activelist);
  436. clear_sdma_activelist(ppd);
  437. }
  438. /*
  439. * This function queues one IB packet onto the send DMA queue per call.
  440. * The caller is responsible for checking:
  441. * 1) The number of send DMA descriptor entries is less than the size of
  442. * the descriptor queue.
  443. * 2) The IB SGE addresses and lengths are 32-bit aligned
  444. * (except possibly the last SGE's length)
  445. * 3) The SGE addresses are suitable for passing to dma_map_single().
  446. */
  447. int qib_sdma_verbs_send(struct qib_pportdata *ppd,
  448. struct rvt_sge_state *ss, u32 dwords,
  449. struct qib_verbs_txreq *tx)
  450. {
  451. unsigned long flags;
  452. struct rvt_sge *sge;
  453. struct rvt_qp *qp;
  454. int ret = 0;
  455. u16 tail;
  456. __le64 *descqp;
  457. u64 sdmadesc[2];
  458. u32 dwoffset;
  459. dma_addr_t addr;
  460. struct qib_qp_priv *priv;
  461. spin_lock_irqsave(&ppd->sdma_lock, flags);
  462. retry:
  463. if (unlikely(!__qib_sdma_running(ppd))) {
  464. complete_sdma_err_req(ppd, tx);
  465. goto unlock;
  466. }
  467. if (tx->txreq.sg_count > qib_sdma_descq_freecnt(ppd)) {
  468. if (qib_sdma_make_progress(ppd))
  469. goto retry;
  470. if (ppd->dd->flags & QIB_HAS_SDMA_TIMEOUT)
  471. ppd->dd->f_sdma_set_desc_cnt(ppd,
  472. ppd->sdma_descq_cnt / 2);
  473. goto busy;
  474. }
  475. dwoffset = tx->hdr_dwords;
  476. make_sdma_desc(ppd, sdmadesc, (u64) tx->txreq.addr, dwoffset, 0);
  477. sdmadesc[0] |= SDMA_DESC_FIRST;
  478. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_USELARGEBUF)
  479. sdmadesc[0] |= SDMA_DESC_USE_LARGE_BUF;
  480. /* write to the descq */
  481. tail = ppd->sdma_descq_tail;
  482. descqp = &ppd->sdma_descq[tail].qw[0];
  483. *descqp++ = cpu_to_le64(sdmadesc[0]);
  484. *descqp++ = cpu_to_le64(sdmadesc[1]);
  485. /* increment the tail */
  486. if (++tail == ppd->sdma_descq_cnt) {
  487. tail = 0;
  488. descqp = &ppd->sdma_descq[0].qw[0];
  489. ++ppd->sdma_generation;
  490. }
  491. tx->txreq.start_idx = tail;
  492. sge = &ss->sge;
  493. while (dwords) {
  494. u32 dw;
  495. u32 len;
  496. len = dwords << 2;
  497. if (len > sge->length)
  498. len = sge->length;
  499. if (len > sge->sge_length)
  500. len = sge->sge_length;
  501. BUG_ON(len == 0);
  502. dw = (len + 3) >> 2;
  503. addr = dma_map_single(&ppd->dd->pcidev->dev, sge->vaddr,
  504. dw << 2, DMA_TO_DEVICE);
  505. if (dma_mapping_error(&ppd->dd->pcidev->dev, addr))
  506. goto unmap;
  507. sdmadesc[0] = 0;
  508. make_sdma_desc(ppd, sdmadesc, (u64) addr, dw, dwoffset);
  509. /* SDmaUseLargeBuf has to be set in every descriptor */
  510. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_USELARGEBUF)
  511. sdmadesc[0] |= SDMA_DESC_USE_LARGE_BUF;
  512. /* write to the descq */
  513. *descqp++ = cpu_to_le64(sdmadesc[0]);
  514. *descqp++ = cpu_to_le64(sdmadesc[1]);
  515. /* increment the tail */
  516. if (++tail == ppd->sdma_descq_cnt) {
  517. tail = 0;
  518. descqp = &ppd->sdma_descq[0].qw[0];
  519. ++ppd->sdma_generation;
  520. }
  521. sge->vaddr += len;
  522. sge->length -= len;
  523. sge->sge_length -= len;
  524. if (sge->sge_length == 0) {
  525. if (--ss->num_sge)
  526. *sge = *ss->sg_list++;
  527. } else if (sge->length == 0 && sge->mr->lkey) {
  528. if (++sge->n >= RVT_SEGSZ) {
  529. if (++sge->m >= sge->mr->mapsz)
  530. break;
  531. sge->n = 0;
  532. }
  533. sge->vaddr =
  534. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  535. sge->length =
  536. sge->mr->map[sge->m]->segs[sge->n].length;
  537. }
  538. dwoffset += dw;
  539. dwords -= dw;
  540. }
  541. if (!tail)
  542. descqp = &ppd->sdma_descq[ppd->sdma_descq_cnt].qw[0];
  543. descqp -= 2;
  544. descqp[0] |= cpu_to_le64(SDMA_DESC_LAST);
  545. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_HEADTOHOST)
  546. descqp[0] |= cpu_to_le64(SDMA_DESC_DMA_HEAD);
  547. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_INTREQ)
  548. descqp[0] |= cpu_to_le64(SDMA_DESC_INTR);
  549. priv = tx->qp->priv;
  550. atomic_inc(&priv->s_dma_busy);
  551. tx->txreq.next_descq_idx = tail;
  552. ppd->dd->f_sdma_update_tail(ppd, tail);
  553. ppd->sdma_descq_added += tx->txreq.sg_count;
  554. list_add_tail(&tx->txreq.list, &ppd->sdma_activelist);
  555. goto unlock;
  556. unmap:
  557. for (;;) {
  558. if (!tail)
  559. tail = ppd->sdma_descq_cnt - 1;
  560. else
  561. tail--;
  562. if (tail == ppd->sdma_descq_tail)
  563. break;
  564. unmap_desc(ppd, tail);
  565. }
  566. qp = tx->qp;
  567. priv = qp->priv;
  568. qib_put_txreq(tx);
  569. spin_lock(&qp->r_lock);
  570. spin_lock(&qp->s_lock);
  571. if (qp->ibqp.qp_type == IB_QPT_RC) {
  572. /* XXX what about error sending RDMA read responses? */
  573. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)
  574. rvt_error_qp(qp, IB_WC_GENERAL_ERR);
  575. } else if (qp->s_wqe)
  576. qib_send_complete(qp, qp->s_wqe, IB_WC_GENERAL_ERR);
  577. spin_unlock(&qp->s_lock);
  578. spin_unlock(&qp->r_lock);
  579. /* return zero to process the next send work request */
  580. goto unlock;
  581. busy:
  582. qp = tx->qp;
  583. priv = qp->priv;
  584. spin_lock(&qp->s_lock);
  585. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  586. struct qib_ibdev *dev;
  587. /*
  588. * If we couldn't queue the DMA request, save the info
  589. * and try again later rather than destroying the
  590. * buffer and undoing the side effects of the copy.
  591. */
  592. tx->ss = ss;
  593. tx->dwords = dwords;
  594. priv->s_tx = tx;
  595. dev = &ppd->dd->verbs_dev;
  596. spin_lock(&dev->rdi.pending_lock);
  597. if (list_empty(&priv->iowait)) {
  598. struct qib_ibport *ibp;
  599. ibp = &ppd->ibport_data;
  600. ibp->rvp.n_dmawait++;
  601. qp->s_flags |= RVT_S_WAIT_DMA_DESC;
  602. list_add_tail(&priv->iowait, &dev->dmawait);
  603. }
  604. spin_unlock(&dev->rdi.pending_lock);
  605. qp->s_flags &= ~RVT_S_BUSY;
  606. spin_unlock(&qp->s_lock);
  607. ret = -EBUSY;
  608. } else {
  609. spin_unlock(&qp->s_lock);
  610. qib_put_txreq(tx);
  611. }
  612. unlock:
  613. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  614. return ret;
  615. }
  616. /*
  617. * sdma_lock should be acquired before calling this routine
  618. */
  619. void dump_sdma_state(struct qib_pportdata *ppd)
  620. {
  621. struct qib_sdma_desc *descq;
  622. struct qib_sdma_txreq *txp, *txpnext;
  623. __le64 *descqp;
  624. u64 desc[2];
  625. u64 addr;
  626. u16 gen, dwlen, dwoffset;
  627. u16 head, tail, cnt;
  628. head = ppd->sdma_descq_head;
  629. tail = ppd->sdma_descq_tail;
  630. cnt = qib_sdma_descq_freecnt(ppd);
  631. descq = ppd->sdma_descq;
  632. qib_dev_porterr(ppd->dd, ppd->port,
  633. "SDMA ppd->sdma_descq_head: %u\n", head);
  634. qib_dev_porterr(ppd->dd, ppd->port,
  635. "SDMA ppd->sdma_descq_tail: %u\n", tail);
  636. qib_dev_porterr(ppd->dd, ppd->port,
  637. "SDMA sdma_descq_freecnt: %u\n", cnt);
  638. /* print info for each entry in the descriptor queue */
  639. while (head != tail) {
  640. char flags[6] = { 'x', 'x', 'x', 'x', 'x', 0 };
  641. descqp = &descq[head].qw[0];
  642. desc[0] = le64_to_cpu(descqp[0]);
  643. desc[1] = le64_to_cpu(descqp[1]);
  644. flags[0] = (desc[0] & 1<<15) ? 'I' : '-';
  645. flags[1] = (desc[0] & 1<<14) ? 'L' : 'S';
  646. flags[2] = (desc[0] & 1<<13) ? 'H' : '-';
  647. flags[3] = (desc[0] & 1<<12) ? 'F' : '-';
  648. flags[4] = (desc[0] & 1<<11) ? 'L' : '-';
  649. addr = (desc[1] << 32) | ((desc[0] >> 32) & 0xfffffffcULL);
  650. gen = (desc[0] >> 30) & 3ULL;
  651. dwlen = (desc[0] >> 14) & (0x7ffULL << 2);
  652. dwoffset = (desc[0] & 0x7ffULL) << 2;
  653. qib_dev_porterr(ppd->dd, ppd->port,
  654. "SDMA sdmadesc[%u]: flags:%s addr:0x%016llx gen:%u len:%u bytes offset:%u bytes\n",
  655. head, flags, addr, gen, dwlen, dwoffset);
  656. if (++head == ppd->sdma_descq_cnt)
  657. head = 0;
  658. }
  659. /* print dma descriptor indices from the TX requests */
  660. list_for_each_entry_safe(txp, txpnext, &ppd->sdma_activelist,
  661. list)
  662. qib_dev_porterr(ppd->dd, ppd->port,
  663. "SDMA txp->start_idx: %u txp->next_descq_idx: %u\n",
  664. txp->start_idx, txp->next_descq_idx);
  665. }
  666. void qib_sdma_process_event(struct qib_pportdata *ppd,
  667. enum qib_sdma_events event)
  668. {
  669. unsigned long flags;
  670. spin_lock_irqsave(&ppd->sdma_lock, flags);
  671. __qib_sdma_process_event(ppd, event);
  672. if (ppd->sdma_state.current_state == qib_sdma_state_s99_running)
  673. qib_verbs_sdma_desc_avail(ppd, qib_sdma_descq_freecnt(ppd));
  674. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  675. }
  676. void __qib_sdma_process_event(struct qib_pportdata *ppd,
  677. enum qib_sdma_events event)
  678. {
  679. struct qib_sdma_state *ss = &ppd->sdma_state;
  680. switch (ss->current_state) {
  681. case qib_sdma_state_s00_hw_down:
  682. switch (event) {
  683. case qib_sdma_event_e00_go_hw_down:
  684. break;
  685. case qib_sdma_event_e30_go_running:
  686. /*
  687. * If down, but running requested (usually result
  688. * of link up, then we need to start up.
  689. * This can happen when hw down is requested while
  690. * bringing the link up with traffic active on
  691. * 7220, e.g. */
  692. ss->go_s99_running = 1;
  693. /* fall through and start dma engine */
  694. case qib_sdma_event_e10_go_hw_start:
  695. /* This reference means the state machine is started */
  696. sdma_get(&ppd->sdma_state);
  697. sdma_set_state(ppd,
  698. qib_sdma_state_s10_hw_start_up_wait);
  699. break;
  700. case qib_sdma_event_e20_hw_started:
  701. break;
  702. case qib_sdma_event_e40_sw_cleaned:
  703. sdma_sw_tear_down(ppd);
  704. break;
  705. case qib_sdma_event_e50_hw_cleaned:
  706. break;
  707. case qib_sdma_event_e60_hw_halted:
  708. break;
  709. case qib_sdma_event_e70_go_idle:
  710. break;
  711. case qib_sdma_event_e7220_err_halted:
  712. break;
  713. case qib_sdma_event_e7322_err_halted:
  714. break;
  715. case qib_sdma_event_e90_timer_tick:
  716. break;
  717. }
  718. break;
  719. case qib_sdma_state_s10_hw_start_up_wait:
  720. switch (event) {
  721. case qib_sdma_event_e00_go_hw_down:
  722. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  723. sdma_sw_tear_down(ppd);
  724. break;
  725. case qib_sdma_event_e10_go_hw_start:
  726. break;
  727. case qib_sdma_event_e20_hw_started:
  728. sdma_set_state(ppd, ss->go_s99_running ?
  729. qib_sdma_state_s99_running :
  730. qib_sdma_state_s20_idle);
  731. break;
  732. case qib_sdma_event_e30_go_running:
  733. ss->go_s99_running = 1;
  734. break;
  735. case qib_sdma_event_e40_sw_cleaned:
  736. break;
  737. case qib_sdma_event_e50_hw_cleaned:
  738. break;
  739. case qib_sdma_event_e60_hw_halted:
  740. break;
  741. case qib_sdma_event_e70_go_idle:
  742. ss->go_s99_running = 0;
  743. break;
  744. case qib_sdma_event_e7220_err_halted:
  745. break;
  746. case qib_sdma_event_e7322_err_halted:
  747. break;
  748. case qib_sdma_event_e90_timer_tick:
  749. break;
  750. }
  751. break;
  752. case qib_sdma_state_s20_idle:
  753. switch (event) {
  754. case qib_sdma_event_e00_go_hw_down:
  755. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  756. sdma_sw_tear_down(ppd);
  757. break;
  758. case qib_sdma_event_e10_go_hw_start:
  759. break;
  760. case qib_sdma_event_e20_hw_started:
  761. break;
  762. case qib_sdma_event_e30_go_running:
  763. sdma_set_state(ppd, qib_sdma_state_s99_running);
  764. ss->go_s99_running = 1;
  765. break;
  766. case qib_sdma_event_e40_sw_cleaned:
  767. break;
  768. case qib_sdma_event_e50_hw_cleaned:
  769. break;
  770. case qib_sdma_event_e60_hw_halted:
  771. break;
  772. case qib_sdma_event_e70_go_idle:
  773. break;
  774. case qib_sdma_event_e7220_err_halted:
  775. break;
  776. case qib_sdma_event_e7322_err_halted:
  777. break;
  778. case qib_sdma_event_e90_timer_tick:
  779. break;
  780. }
  781. break;
  782. case qib_sdma_state_s30_sw_clean_up_wait:
  783. switch (event) {
  784. case qib_sdma_event_e00_go_hw_down:
  785. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  786. break;
  787. case qib_sdma_event_e10_go_hw_start:
  788. break;
  789. case qib_sdma_event_e20_hw_started:
  790. break;
  791. case qib_sdma_event_e30_go_running:
  792. ss->go_s99_running = 1;
  793. break;
  794. case qib_sdma_event_e40_sw_cleaned:
  795. sdma_set_state(ppd,
  796. qib_sdma_state_s10_hw_start_up_wait);
  797. sdma_hw_start_up(ppd);
  798. break;
  799. case qib_sdma_event_e50_hw_cleaned:
  800. break;
  801. case qib_sdma_event_e60_hw_halted:
  802. break;
  803. case qib_sdma_event_e70_go_idle:
  804. ss->go_s99_running = 0;
  805. break;
  806. case qib_sdma_event_e7220_err_halted:
  807. break;
  808. case qib_sdma_event_e7322_err_halted:
  809. break;
  810. case qib_sdma_event_e90_timer_tick:
  811. break;
  812. }
  813. break;
  814. case qib_sdma_state_s40_hw_clean_up_wait:
  815. switch (event) {
  816. case qib_sdma_event_e00_go_hw_down:
  817. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  818. sdma_start_sw_clean_up(ppd);
  819. break;
  820. case qib_sdma_event_e10_go_hw_start:
  821. break;
  822. case qib_sdma_event_e20_hw_started:
  823. break;
  824. case qib_sdma_event_e30_go_running:
  825. ss->go_s99_running = 1;
  826. break;
  827. case qib_sdma_event_e40_sw_cleaned:
  828. break;
  829. case qib_sdma_event_e50_hw_cleaned:
  830. sdma_set_state(ppd,
  831. qib_sdma_state_s30_sw_clean_up_wait);
  832. sdma_start_sw_clean_up(ppd);
  833. break;
  834. case qib_sdma_event_e60_hw_halted:
  835. break;
  836. case qib_sdma_event_e70_go_idle:
  837. ss->go_s99_running = 0;
  838. break;
  839. case qib_sdma_event_e7220_err_halted:
  840. break;
  841. case qib_sdma_event_e7322_err_halted:
  842. break;
  843. case qib_sdma_event_e90_timer_tick:
  844. break;
  845. }
  846. break;
  847. case qib_sdma_state_s50_hw_halt_wait:
  848. switch (event) {
  849. case qib_sdma_event_e00_go_hw_down:
  850. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  851. sdma_start_sw_clean_up(ppd);
  852. break;
  853. case qib_sdma_event_e10_go_hw_start:
  854. break;
  855. case qib_sdma_event_e20_hw_started:
  856. break;
  857. case qib_sdma_event_e30_go_running:
  858. ss->go_s99_running = 1;
  859. break;
  860. case qib_sdma_event_e40_sw_cleaned:
  861. break;
  862. case qib_sdma_event_e50_hw_cleaned:
  863. break;
  864. case qib_sdma_event_e60_hw_halted:
  865. sdma_set_state(ppd,
  866. qib_sdma_state_s40_hw_clean_up_wait);
  867. ppd->dd->f_sdma_hw_clean_up(ppd);
  868. break;
  869. case qib_sdma_event_e70_go_idle:
  870. ss->go_s99_running = 0;
  871. break;
  872. case qib_sdma_event_e7220_err_halted:
  873. break;
  874. case qib_sdma_event_e7322_err_halted:
  875. break;
  876. case qib_sdma_event_e90_timer_tick:
  877. break;
  878. }
  879. break;
  880. case qib_sdma_state_s99_running:
  881. switch (event) {
  882. case qib_sdma_event_e00_go_hw_down:
  883. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  884. sdma_start_sw_clean_up(ppd);
  885. break;
  886. case qib_sdma_event_e10_go_hw_start:
  887. break;
  888. case qib_sdma_event_e20_hw_started:
  889. break;
  890. case qib_sdma_event_e30_go_running:
  891. break;
  892. case qib_sdma_event_e40_sw_cleaned:
  893. break;
  894. case qib_sdma_event_e50_hw_cleaned:
  895. break;
  896. case qib_sdma_event_e60_hw_halted:
  897. sdma_set_state(ppd,
  898. qib_sdma_state_s30_sw_clean_up_wait);
  899. sdma_start_sw_clean_up(ppd);
  900. break;
  901. case qib_sdma_event_e70_go_idle:
  902. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  903. ss->go_s99_running = 0;
  904. break;
  905. case qib_sdma_event_e7220_err_halted:
  906. sdma_set_state(ppd,
  907. qib_sdma_state_s30_sw_clean_up_wait);
  908. sdma_start_sw_clean_up(ppd);
  909. break;
  910. case qib_sdma_event_e7322_err_halted:
  911. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  912. break;
  913. case qib_sdma_event_e90_timer_tick:
  914. break;
  915. }
  916. break;
  917. }
  918. ss->last_event = event;
  919. }