qib_sdma.c 26 KB

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