qib_sdma.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. }
  362. int qib_setup_sdma(struct qib_pportdata *ppd)
  363. {
  364. struct qib_devdata *dd = ppd->dd;
  365. unsigned long flags;
  366. int ret = 0;
  367. ret = alloc_sdma(ppd);
  368. if (ret)
  369. goto bail;
  370. /* set consistent sdma state */
  371. ppd->dd->f_sdma_init_early(ppd);
  372. spin_lock_irqsave(&ppd->sdma_lock, flags);
  373. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  374. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  375. /* set up reference counting */
  376. kref_init(&ppd->sdma_state.kref);
  377. init_completion(&ppd->sdma_state.comp);
  378. ppd->sdma_generation = 0;
  379. ppd->sdma_descq_head = 0;
  380. ppd->sdma_descq_removed = 0;
  381. ppd->sdma_descq_added = 0;
  382. INIT_LIST_HEAD(&ppd->sdma_activelist);
  383. tasklet_init(&ppd->sdma_sw_clean_up_task, sdma_sw_clean_up_task,
  384. (unsigned long)ppd);
  385. ret = dd->f_init_sdma_regs(ppd);
  386. if (ret)
  387. goto bail_alloc;
  388. qib_sdma_process_event(ppd, qib_sdma_event_e10_go_hw_start);
  389. return 0;
  390. bail_alloc:
  391. qib_teardown_sdma(ppd);
  392. bail:
  393. return ret;
  394. }
  395. void qib_teardown_sdma(struct qib_pportdata *ppd)
  396. {
  397. qib_sdma_process_event(ppd, qib_sdma_event_e00_go_hw_down);
  398. /*
  399. * This waits for the state machine to exit so it is not
  400. * necessary to kill the sdma_sw_clean_up_task to make sure
  401. * it is not running.
  402. */
  403. sdma_finalput(&ppd->sdma_state);
  404. free_sdma(ppd);
  405. }
  406. int qib_sdma_running(struct qib_pportdata *ppd)
  407. {
  408. unsigned long flags;
  409. int ret;
  410. spin_lock_irqsave(&ppd->sdma_lock, flags);
  411. ret = __qib_sdma_running(ppd);
  412. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  413. return ret;
  414. }
  415. /*
  416. * Complete a request when sdma not running; likely only request
  417. * but to simplify the code, always queue it, then process the full
  418. * activelist. We process the entire list to ensure that this particular
  419. * request does get it's callback, but in the correct order.
  420. * Must be called with sdma_lock held
  421. */
  422. static void complete_sdma_err_req(struct qib_pportdata *ppd,
  423. struct qib_verbs_txreq *tx)
  424. {
  425. atomic_inc(&tx->qp->s_dma_busy);
  426. /* no sdma descriptors, so no unmap_desc */
  427. tx->txreq.start_idx = 0;
  428. tx->txreq.next_descq_idx = 0;
  429. list_add_tail(&tx->txreq.list, &ppd->sdma_activelist);
  430. clear_sdma_activelist(ppd);
  431. }
  432. /*
  433. * This function queues one IB packet onto the send DMA queue per call.
  434. * The caller is responsible for checking:
  435. * 1) The number of send DMA descriptor entries is less than the size of
  436. * the descriptor queue.
  437. * 2) The IB SGE addresses and lengths are 32-bit aligned
  438. * (except possibly the last SGE's length)
  439. * 3) The SGE addresses are suitable for passing to dma_map_single().
  440. */
  441. int qib_sdma_verbs_send(struct qib_pportdata *ppd,
  442. struct qib_sge_state *ss, u32 dwords,
  443. struct qib_verbs_txreq *tx)
  444. {
  445. unsigned long flags;
  446. struct qib_sge *sge;
  447. struct qib_qp *qp;
  448. int ret = 0;
  449. u16 tail;
  450. __le64 *descqp;
  451. u64 sdmadesc[2];
  452. u32 dwoffset;
  453. dma_addr_t addr;
  454. spin_lock_irqsave(&ppd->sdma_lock, flags);
  455. retry:
  456. if (unlikely(!__qib_sdma_running(ppd))) {
  457. complete_sdma_err_req(ppd, tx);
  458. goto unlock;
  459. }
  460. if (tx->txreq.sg_count > qib_sdma_descq_freecnt(ppd)) {
  461. if (qib_sdma_make_progress(ppd))
  462. goto retry;
  463. if (ppd->dd->flags & QIB_HAS_SDMA_TIMEOUT)
  464. ppd->dd->f_sdma_set_desc_cnt(ppd,
  465. ppd->sdma_descq_cnt / 2);
  466. goto busy;
  467. }
  468. dwoffset = tx->hdr_dwords;
  469. make_sdma_desc(ppd, sdmadesc, (u64) tx->txreq.addr, dwoffset, 0);
  470. sdmadesc[0] |= SDMA_DESC_FIRST;
  471. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_USELARGEBUF)
  472. sdmadesc[0] |= SDMA_DESC_USE_LARGE_BUF;
  473. /* write to the descq */
  474. tail = ppd->sdma_descq_tail;
  475. descqp = &ppd->sdma_descq[tail].qw[0];
  476. *descqp++ = cpu_to_le64(sdmadesc[0]);
  477. *descqp++ = cpu_to_le64(sdmadesc[1]);
  478. /* increment the tail */
  479. if (++tail == ppd->sdma_descq_cnt) {
  480. tail = 0;
  481. descqp = &ppd->sdma_descq[0].qw[0];
  482. ++ppd->sdma_generation;
  483. }
  484. tx->txreq.start_idx = tail;
  485. sge = &ss->sge;
  486. while (dwords) {
  487. u32 dw;
  488. u32 len;
  489. len = dwords << 2;
  490. if (len > sge->length)
  491. len = sge->length;
  492. if (len > sge->sge_length)
  493. len = sge->sge_length;
  494. BUG_ON(len == 0);
  495. dw = (len + 3) >> 2;
  496. addr = dma_map_single(&ppd->dd->pcidev->dev, sge->vaddr,
  497. dw << 2, DMA_TO_DEVICE);
  498. if (dma_mapping_error(&ppd->dd->pcidev->dev, addr))
  499. goto unmap;
  500. sdmadesc[0] = 0;
  501. make_sdma_desc(ppd, sdmadesc, (u64) addr, dw, dwoffset);
  502. /* SDmaUseLargeBuf has to be set in every descriptor */
  503. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_USELARGEBUF)
  504. sdmadesc[0] |= SDMA_DESC_USE_LARGE_BUF;
  505. /* write to the descq */
  506. *descqp++ = cpu_to_le64(sdmadesc[0]);
  507. *descqp++ = cpu_to_le64(sdmadesc[1]);
  508. /* increment the tail */
  509. if (++tail == ppd->sdma_descq_cnt) {
  510. tail = 0;
  511. descqp = &ppd->sdma_descq[0].qw[0];
  512. ++ppd->sdma_generation;
  513. }
  514. sge->vaddr += len;
  515. sge->length -= len;
  516. sge->sge_length -= len;
  517. if (sge->sge_length == 0) {
  518. if (--ss->num_sge)
  519. *sge = *ss->sg_list++;
  520. } else if (sge->length == 0 && sge->mr->lkey) {
  521. if (++sge->n >= QIB_SEGSZ) {
  522. if (++sge->m >= sge->mr->mapsz)
  523. break;
  524. sge->n = 0;
  525. }
  526. sge->vaddr =
  527. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  528. sge->length =
  529. sge->mr->map[sge->m]->segs[sge->n].length;
  530. }
  531. dwoffset += dw;
  532. dwords -= dw;
  533. }
  534. if (!tail)
  535. descqp = &ppd->sdma_descq[ppd->sdma_descq_cnt].qw[0];
  536. descqp -= 2;
  537. descqp[0] |= cpu_to_le64(SDMA_DESC_LAST);
  538. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_HEADTOHOST)
  539. descqp[0] |= cpu_to_le64(SDMA_DESC_DMA_HEAD);
  540. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_INTREQ)
  541. descqp[0] |= cpu_to_le64(SDMA_DESC_INTR);
  542. atomic_inc(&tx->qp->s_dma_busy);
  543. tx->txreq.next_descq_idx = tail;
  544. ppd->dd->f_sdma_update_tail(ppd, tail);
  545. ppd->sdma_descq_added += tx->txreq.sg_count;
  546. list_add_tail(&tx->txreq.list, &ppd->sdma_activelist);
  547. goto unlock;
  548. unmap:
  549. for (;;) {
  550. if (!tail)
  551. tail = ppd->sdma_descq_cnt - 1;
  552. else
  553. tail--;
  554. if (tail == ppd->sdma_descq_tail)
  555. break;
  556. unmap_desc(ppd, tail);
  557. }
  558. qp = tx->qp;
  559. qib_put_txreq(tx);
  560. spin_lock(&qp->r_lock);
  561. spin_lock(&qp->s_lock);
  562. if (qp->ibqp.qp_type == IB_QPT_RC) {
  563. /* XXX what about error sending RDMA read responses? */
  564. if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)
  565. qib_error_qp(qp, IB_WC_GENERAL_ERR);
  566. } else if (qp->s_wqe)
  567. qib_send_complete(qp, qp->s_wqe, IB_WC_GENERAL_ERR);
  568. spin_unlock(&qp->s_lock);
  569. spin_unlock(&qp->r_lock);
  570. /* return zero to process the next send work request */
  571. goto unlock;
  572. busy:
  573. qp = tx->qp;
  574. spin_lock(&qp->s_lock);
  575. if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
  576. struct qib_ibdev *dev;
  577. /*
  578. * If we couldn't queue the DMA request, save the info
  579. * and try again later rather than destroying the
  580. * buffer and undoing the side effects of the copy.
  581. */
  582. tx->ss = ss;
  583. tx->dwords = dwords;
  584. qp->s_tx = tx;
  585. dev = &ppd->dd->verbs_dev;
  586. spin_lock(&dev->pending_lock);
  587. if (list_empty(&qp->iowait)) {
  588. struct qib_ibport *ibp;
  589. ibp = &ppd->ibport_data;
  590. ibp->n_dmawait++;
  591. qp->s_flags |= QIB_S_WAIT_DMA_DESC;
  592. list_add_tail(&qp->iowait, &dev->dmawait);
  593. }
  594. spin_unlock(&dev->pending_lock);
  595. qp->s_flags &= ~QIB_S_BUSY;
  596. spin_unlock(&qp->s_lock);
  597. ret = -EBUSY;
  598. } else {
  599. spin_unlock(&qp->s_lock);
  600. qib_put_txreq(tx);
  601. }
  602. unlock:
  603. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  604. return ret;
  605. }
  606. /*
  607. * sdma_lock should be acquired before calling this routine
  608. */
  609. void dump_sdma_state(struct qib_pportdata *ppd)
  610. {
  611. struct qib_sdma_desc *descq;
  612. struct qib_sdma_txreq *txp, *txpnext;
  613. __le64 *descqp;
  614. u64 desc[2];
  615. u64 addr;
  616. u16 gen, dwlen, dwoffset;
  617. u16 head, tail, cnt;
  618. head = ppd->sdma_descq_head;
  619. tail = ppd->sdma_descq_tail;
  620. cnt = qib_sdma_descq_freecnt(ppd);
  621. descq = ppd->sdma_descq;
  622. qib_dev_porterr(ppd->dd, ppd->port,
  623. "SDMA ppd->sdma_descq_head: %u\n", head);
  624. qib_dev_porterr(ppd->dd, ppd->port,
  625. "SDMA ppd->sdma_descq_tail: %u\n", tail);
  626. qib_dev_porterr(ppd->dd, ppd->port,
  627. "SDMA sdma_descq_freecnt: %u\n", cnt);
  628. /* print info for each entry in the descriptor queue */
  629. while (head != tail) {
  630. char flags[6] = { 'x', 'x', 'x', 'x', 'x', 0 };
  631. descqp = &descq[head].qw[0];
  632. desc[0] = le64_to_cpu(descqp[0]);
  633. desc[1] = le64_to_cpu(descqp[1]);
  634. flags[0] = (desc[0] & 1<<15) ? 'I' : '-';
  635. flags[1] = (desc[0] & 1<<14) ? 'L' : 'S';
  636. flags[2] = (desc[0] & 1<<13) ? 'H' : '-';
  637. flags[3] = (desc[0] & 1<<12) ? 'F' : '-';
  638. flags[4] = (desc[0] & 1<<11) ? 'L' : '-';
  639. addr = (desc[1] << 32) | ((desc[0] >> 32) & 0xfffffffcULL);
  640. gen = (desc[0] >> 30) & 3ULL;
  641. dwlen = (desc[0] >> 14) & (0x7ffULL << 2);
  642. dwoffset = (desc[0] & 0x7ffULL) << 2;
  643. qib_dev_porterr(ppd->dd, ppd->port,
  644. "SDMA sdmadesc[%u]: flags:%s addr:0x%016llx gen:%u len:%u bytes offset:%u bytes\n",
  645. head, flags, addr, gen, dwlen, dwoffset);
  646. if (++head == ppd->sdma_descq_cnt)
  647. head = 0;
  648. }
  649. /* print dma descriptor indices from the TX requests */
  650. list_for_each_entry_safe(txp, txpnext, &ppd->sdma_activelist,
  651. list)
  652. qib_dev_porterr(ppd->dd, ppd->port,
  653. "SDMA txp->start_idx: %u txp->next_descq_idx: %u\n",
  654. txp->start_idx, txp->next_descq_idx);
  655. }
  656. void qib_sdma_process_event(struct qib_pportdata *ppd,
  657. enum qib_sdma_events event)
  658. {
  659. unsigned long flags;
  660. spin_lock_irqsave(&ppd->sdma_lock, flags);
  661. __qib_sdma_process_event(ppd, event);
  662. if (ppd->sdma_state.current_state == qib_sdma_state_s99_running)
  663. qib_verbs_sdma_desc_avail(ppd, qib_sdma_descq_freecnt(ppd));
  664. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  665. }
  666. void __qib_sdma_process_event(struct qib_pportdata *ppd,
  667. enum qib_sdma_events event)
  668. {
  669. struct qib_sdma_state *ss = &ppd->sdma_state;
  670. switch (ss->current_state) {
  671. case qib_sdma_state_s00_hw_down:
  672. switch (event) {
  673. case qib_sdma_event_e00_go_hw_down:
  674. break;
  675. case qib_sdma_event_e30_go_running:
  676. /*
  677. * If down, but running requested (usually result
  678. * of link up, then we need to start up.
  679. * This can happen when hw down is requested while
  680. * bringing the link up with traffic active on
  681. * 7220, e.g. */
  682. ss->go_s99_running = 1;
  683. /* fall through and start dma engine */
  684. case qib_sdma_event_e10_go_hw_start:
  685. /* This reference means the state machine is started */
  686. sdma_get(&ppd->sdma_state);
  687. sdma_set_state(ppd,
  688. qib_sdma_state_s10_hw_start_up_wait);
  689. break;
  690. case qib_sdma_event_e20_hw_started:
  691. break;
  692. case qib_sdma_event_e40_sw_cleaned:
  693. sdma_sw_tear_down(ppd);
  694. break;
  695. case qib_sdma_event_e50_hw_cleaned:
  696. break;
  697. case qib_sdma_event_e60_hw_halted:
  698. break;
  699. case qib_sdma_event_e70_go_idle:
  700. break;
  701. case qib_sdma_event_e7220_err_halted:
  702. break;
  703. case qib_sdma_event_e7322_err_halted:
  704. break;
  705. case qib_sdma_event_e90_timer_tick:
  706. break;
  707. }
  708. break;
  709. case qib_sdma_state_s10_hw_start_up_wait:
  710. switch (event) {
  711. case qib_sdma_event_e00_go_hw_down:
  712. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  713. sdma_sw_tear_down(ppd);
  714. break;
  715. case qib_sdma_event_e10_go_hw_start:
  716. break;
  717. case qib_sdma_event_e20_hw_started:
  718. sdma_set_state(ppd, ss->go_s99_running ?
  719. qib_sdma_state_s99_running :
  720. qib_sdma_state_s20_idle);
  721. break;
  722. case qib_sdma_event_e30_go_running:
  723. ss->go_s99_running = 1;
  724. break;
  725. case qib_sdma_event_e40_sw_cleaned:
  726. break;
  727. case qib_sdma_event_e50_hw_cleaned:
  728. break;
  729. case qib_sdma_event_e60_hw_halted:
  730. break;
  731. case qib_sdma_event_e70_go_idle:
  732. ss->go_s99_running = 0;
  733. break;
  734. case qib_sdma_event_e7220_err_halted:
  735. break;
  736. case qib_sdma_event_e7322_err_halted:
  737. break;
  738. case qib_sdma_event_e90_timer_tick:
  739. break;
  740. }
  741. break;
  742. case qib_sdma_state_s20_idle:
  743. switch (event) {
  744. case qib_sdma_event_e00_go_hw_down:
  745. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  746. sdma_sw_tear_down(ppd);
  747. break;
  748. case qib_sdma_event_e10_go_hw_start:
  749. break;
  750. case qib_sdma_event_e20_hw_started:
  751. break;
  752. case qib_sdma_event_e30_go_running:
  753. sdma_set_state(ppd, qib_sdma_state_s99_running);
  754. ss->go_s99_running = 1;
  755. break;
  756. case qib_sdma_event_e40_sw_cleaned:
  757. break;
  758. case qib_sdma_event_e50_hw_cleaned:
  759. break;
  760. case qib_sdma_event_e60_hw_halted:
  761. break;
  762. case qib_sdma_event_e70_go_idle:
  763. break;
  764. case qib_sdma_event_e7220_err_halted:
  765. break;
  766. case qib_sdma_event_e7322_err_halted:
  767. break;
  768. case qib_sdma_event_e90_timer_tick:
  769. break;
  770. }
  771. break;
  772. case qib_sdma_state_s30_sw_clean_up_wait:
  773. switch (event) {
  774. case qib_sdma_event_e00_go_hw_down:
  775. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  776. break;
  777. case qib_sdma_event_e10_go_hw_start:
  778. break;
  779. case qib_sdma_event_e20_hw_started:
  780. break;
  781. case qib_sdma_event_e30_go_running:
  782. ss->go_s99_running = 1;
  783. break;
  784. case qib_sdma_event_e40_sw_cleaned:
  785. sdma_set_state(ppd,
  786. qib_sdma_state_s10_hw_start_up_wait);
  787. sdma_hw_start_up(ppd);
  788. break;
  789. case qib_sdma_event_e50_hw_cleaned:
  790. break;
  791. case qib_sdma_event_e60_hw_halted:
  792. break;
  793. case qib_sdma_event_e70_go_idle:
  794. ss->go_s99_running = 0;
  795. break;
  796. case qib_sdma_event_e7220_err_halted:
  797. break;
  798. case qib_sdma_event_e7322_err_halted:
  799. break;
  800. case qib_sdma_event_e90_timer_tick:
  801. break;
  802. }
  803. break;
  804. case qib_sdma_state_s40_hw_clean_up_wait:
  805. switch (event) {
  806. case qib_sdma_event_e00_go_hw_down:
  807. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  808. sdma_start_sw_clean_up(ppd);
  809. break;
  810. case qib_sdma_event_e10_go_hw_start:
  811. break;
  812. case qib_sdma_event_e20_hw_started:
  813. break;
  814. case qib_sdma_event_e30_go_running:
  815. ss->go_s99_running = 1;
  816. break;
  817. case qib_sdma_event_e40_sw_cleaned:
  818. break;
  819. case qib_sdma_event_e50_hw_cleaned:
  820. sdma_set_state(ppd,
  821. qib_sdma_state_s30_sw_clean_up_wait);
  822. sdma_start_sw_clean_up(ppd);
  823. break;
  824. case qib_sdma_event_e60_hw_halted:
  825. break;
  826. case qib_sdma_event_e70_go_idle:
  827. ss->go_s99_running = 0;
  828. break;
  829. case qib_sdma_event_e7220_err_halted:
  830. break;
  831. case qib_sdma_event_e7322_err_halted:
  832. break;
  833. case qib_sdma_event_e90_timer_tick:
  834. break;
  835. }
  836. break;
  837. case qib_sdma_state_s50_hw_halt_wait:
  838. switch (event) {
  839. case qib_sdma_event_e00_go_hw_down:
  840. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  841. sdma_start_sw_clean_up(ppd);
  842. break;
  843. case qib_sdma_event_e10_go_hw_start:
  844. break;
  845. case qib_sdma_event_e20_hw_started:
  846. break;
  847. case qib_sdma_event_e30_go_running:
  848. ss->go_s99_running = 1;
  849. break;
  850. case qib_sdma_event_e40_sw_cleaned:
  851. break;
  852. case qib_sdma_event_e50_hw_cleaned:
  853. break;
  854. case qib_sdma_event_e60_hw_halted:
  855. sdma_set_state(ppd,
  856. qib_sdma_state_s40_hw_clean_up_wait);
  857. ppd->dd->f_sdma_hw_clean_up(ppd);
  858. break;
  859. case qib_sdma_event_e70_go_idle:
  860. ss->go_s99_running = 0;
  861. break;
  862. case qib_sdma_event_e7220_err_halted:
  863. break;
  864. case qib_sdma_event_e7322_err_halted:
  865. break;
  866. case qib_sdma_event_e90_timer_tick:
  867. break;
  868. }
  869. break;
  870. case qib_sdma_state_s99_running:
  871. switch (event) {
  872. case qib_sdma_event_e00_go_hw_down:
  873. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  874. sdma_start_sw_clean_up(ppd);
  875. break;
  876. case qib_sdma_event_e10_go_hw_start:
  877. break;
  878. case qib_sdma_event_e20_hw_started:
  879. break;
  880. case qib_sdma_event_e30_go_running:
  881. break;
  882. case qib_sdma_event_e40_sw_cleaned:
  883. break;
  884. case qib_sdma_event_e50_hw_cleaned:
  885. break;
  886. case qib_sdma_event_e60_hw_halted:
  887. sdma_set_state(ppd,
  888. qib_sdma_state_s30_sw_clean_up_wait);
  889. sdma_start_sw_clean_up(ppd);
  890. break;
  891. case qib_sdma_event_e70_go_idle:
  892. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  893. ss->go_s99_running = 0;
  894. break;
  895. case qib_sdma_event_e7220_err_halted:
  896. sdma_set_state(ppd,
  897. qib_sdma_state_s30_sw_clean_up_wait);
  898. sdma_start_sw_clean_up(ppd);
  899. break;
  900. case qib_sdma_event_e7322_err_halted:
  901. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  902. break;
  903. case qib_sdma_event_e90_timer_tick:
  904. break;
  905. }
  906. break;
  907. }
  908. ss->last_event = event;
  909. }