qib_sdma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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(unsigned long);
  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(unsigned long opaque)
  109. {
  110. struct qib_pportdata *ppd = (struct qib_pportdata *) opaque;
  111. unsigned long flags;
  112. spin_lock_irqsave(&ppd->sdma_lock, flags);
  113. /*
  114. * At this point, the following should always be true:
  115. * - We are halted, so no more descriptors are getting retired.
  116. * - We are not running, so no one is submitting new work.
  117. * - Only we can send the e40_sw_cleaned, so we can't start
  118. * running again until we say so. So, the active list and
  119. * descq are ours to play with.
  120. */
  121. /* Process all retired requests. */
  122. qib_sdma_make_progress(ppd);
  123. clear_sdma_activelist(ppd);
  124. /*
  125. * Resync count of added and removed. It is VERY important that
  126. * sdma_descq_removed NEVER decrement - user_sdma depends on it.
  127. */
  128. ppd->sdma_descq_removed = ppd->sdma_descq_added;
  129. /*
  130. * Reset our notion of head and tail.
  131. * Note that the HW registers will be reset when switching states
  132. * due to calling __qib_sdma_process_event() below.
  133. */
  134. ppd->sdma_descq_tail = 0;
  135. ppd->sdma_descq_head = 0;
  136. ppd->sdma_head_dma[0] = 0;
  137. ppd->sdma_generation = 0;
  138. __qib_sdma_process_event(ppd, qib_sdma_event_e40_sw_cleaned);
  139. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  140. }
  141. /*
  142. * This is called when changing to state qib_sdma_state_s10_hw_start_up_wait
  143. * as a result of send buffer errors or send DMA descriptor errors.
  144. * We want to disarm the buffers in these cases.
  145. */
  146. static void sdma_hw_start_up(struct qib_pportdata *ppd)
  147. {
  148. struct qib_sdma_state *ss = &ppd->sdma_state;
  149. unsigned bufno;
  150. for (bufno = ss->first_sendbuf; bufno < ss->last_sendbuf; ++bufno)
  151. ppd->dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_BUF(bufno));
  152. ppd->dd->f_sdma_hw_start_up(ppd);
  153. }
  154. static void sdma_sw_tear_down(struct qib_pportdata *ppd)
  155. {
  156. struct qib_sdma_state *ss = &ppd->sdma_state;
  157. /* Releasing this reference means the state machine has stopped. */
  158. sdma_put(ss);
  159. }
  160. static void sdma_start_sw_clean_up(struct qib_pportdata *ppd)
  161. {
  162. tasklet_hi_schedule(&ppd->sdma_sw_clean_up_task);
  163. }
  164. static void sdma_set_state(struct qib_pportdata *ppd,
  165. enum qib_sdma_states next_state)
  166. {
  167. struct qib_sdma_state *ss = &ppd->sdma_state;
  168. struct sdma_set_state_action *action = ss->set_state_action;
  169. unsigned op = 0;
  170. /* debugging bookkeeping */
  171. ss->previous_state = ss->current_state;
  172. ss->previous_op = ss->current_op;
  173. ss->current_state = next_state;
  174. if (action[next_state].op_enable)
  175. op |= QIB_SDMA_SENDCTRL_OP_ENABLE;
  176. if (action[next_state].op_intenable)
  177. op |= QIB_SDMA_SENDCTRL_OP_INTENABLE;
  178. if (action[next_state].op_halt)
  179. op |= QIB_SDMA_SENDCTRL_OP_HALT;
  180. if (action[next_state].op_drain)
  181. op |= QIB_SDMA_SENDCTRL_OP_DRAIN;
  182. if (action[next_state].go_s99_running_tofalse)
  183. ss->go_s99_running = 0;
  184. if (action[next_state].go_s99_running_totrue)
  185. ss->go_s99_running = 1;
  186. ss->current_op = op;
  187. ppd->dd->f_sdma_sendctrl(ppd, ss->current_op);
  188. }
  189. static void unmap_desc(struct qib_pportdata *ppd, unsigned head)
  190. {
  191. __le64 *descqp = &ppd->sdma_descq[head].qw[0];
  192. u64 desc[2];
  193. dma_addr_t addr;
  194. size_t len;
  195. desc[0] = le64_to_cpu(descqp[0]);
  196. desc[1] = le64_to_cpu(descqp[1]);
  197. addr = (desc[1] << 32) | (desc[0] >> 32);
  198. len = (desc[0] >> 14) & (0x7ffULL << 2);
  199. dma_unmap_single(&ppd->dd->pcidev->dev, addr, len, DMA_TO_DEVICE);
  200. }
  201. static int alloc_sdma(struct qib_pportdata *ppd)
  202. {
  203. ppd->sdma_descq_cnt = sdma_descq_cnt;
  204. if (!ppd->sdma_descq_cnt)
  205. ppd->sdma_descq_cnt = 256;
  206. /* Allocate memory for SendDMA descriptor FIFO */
  207. ppd->sdma_descq = dma_alloc_coherent(&ppd->dd->pcidev->dev,
  208. ppd->sdma_descq_cnt * sizeof(u64[2]), &ppd->sdma_descq_phys,
  209. GFP_KERNEL);
  210. if (!ppd->sdma_descq) {
  211. qib_dev_err(ppd->dd,
  212. "failed to allocate SendDMA descriptor FIFO memory\n");
  213. goto bail;
  214. }
  215. /* Allocate memory for DMA of head register to memory */
  216. ppd->sdma_head_dma = dma_alloc_coherent(&ppd->dd->pcidev->dev,
  217. PAGE_SIZE, &ppd->sdma_head_phys, GFP_KERNEL);
  218. if (!ppd->sdma_head_dma) {
  219. qib_dev_err(ppd->dd,
  220. "failed to allocate SendDMA head memory\n");
  221. goto cleanup_descq;
  222. }
  223. ppd->sdma_head_dma[0] = 0;
  224. return 0;
  225. cleanup_descq:
  226. dma_free_coherent(&ppd->dd->pcidev->dev,
  227. ppd->sdma_descq_cnt * sizeof(u64[2]), (void *)ppd->sdma_descq,
  228. ppd->sdma_descq_phys);
  229. ppd->sdma_descq = NULL;
  230. ppd->sdma_descq_phys = 0;
  231. bail:
  232. ppd->sdma_descq_cnt = 0;
  233. return -ENOMEM;
  234. }
  235. static void free_sdma(struct qib_pportdata *ppd)
  236. {
  237. struct qib_devdata *dd = ppd->dd;
  238. if (ppd->sdma_head_dma) {
  239. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  240. (void *)ppd->sdma_head_dma,
  241. ppd->sdma_head_phys);
  242. ppd->sdma_head_dma = NULL;
  243. ppd->sdma_head_phys = 0;
  244. }
  245. if (ppd->sdma_descq) {
  246. dma_free_coherent(&dd->pcidev->dev,
  247. ppd->sdma_descq_cnt * sizeof(u64[2]),
  248. ppd->sdma_descq, ppd->sdma_descq_phys);
  249. ppd->sdma_descq = NULL;
  250. ppd->sdma_descq_phys = 0;
  251. }
  252. }
  253. static inline void make_sdma_desc(struct qib_pportdata *ppd,
  254. u64 *sdmadesc, u64 addr, u64 dwlen,
  255. u64 dwoffset)
  256. {
  257. WARN_ON(addr & 3);
  258. /* SDmaPhyAddr[47:32] */
  259. sdmadesc[1] = addr >> 32;
  260. /* SDmaPhyAddr[31:0] */
  261. sdmadesc[0] = (addr & 0xfffffffcULL) << 32;
  262. /* SDmaGeneration[1:0] */
  263. sdmadesc[0] |= (ppd->sdma_generation & 3ULL) <<
  264. SDMA_DESC_GEN_LSB;
  265. /* SDmaDwordCount[10:0] */
  266. sdmadesc[0] |= (dwlen & 0x7ffULL) << SDMA_DESC_COUNT_LSB;
  267. /* SDmaBufOffset[12:2] */
  268. sdmadesc[0] |= dwoffset & 0x7ffULL;
  269. }
  270. /* sdma_lock must be held */
  271. int qib_sdma_make_progress(struct qib_pportdata *ppd)
  272. {
  273. struct list_head *lp = NULL;
  274. struct qib_sdma_txreq *txp = NULL;
  275. struct qib_devdata *dd = ppd->dd;
  276. int progress = 0;
  277. u16 hwhead;
  278. u16 idx = 0;
  279. hwhead = dd->f_sdma_gethead(ppd);
  280. /* The reason for some of the complexity of this code is that
  281. * not all descriptors have corresponding txps. So, we have to
  282. * be able to skip over descs until we wander into the range of
  283. * the next txp on the list.
  284. */
  285. if (!list_empty(&ppd->sdma_activelist)) {
  286. lp = ppd->sdma_activelist.next;
  287. txp = list_entry(lp, struct qib_sdma_txreq, list);
  288. idx = txp->start_idx;
  289. }
  290. while (ppd->sdma_descq_head != hwhead) {
  291. /* if desc is part of this txp, unmap if needed */
  292. if (txp && (txp->flags & QIB_SDMA_TXREQ_F_FREEDESC) &&
  293. (idx == ppd->sdma_descq_head)) {
  294. unmap_desc(ppd, ppd->sdma_descq_head);
  295. if (++idx == ppd->sdma_descq_cnt)
  296. idx = 0;
  297. }
  298. /* increment dequed desc count */
  299. ppd->sdma_descq_removed++;
  300. /* advance head, wrap if needed */
  301. if (++ppd->sdma_descq_head == ppd->sdma_descq_cnt)
  302. ppd->sdma_descq_head = 0;
  303. /* if now past this txp's descs, do the callback */
  304. if (txp && txp->next_descq_idx == ppd->sdma_descq_head) {
  305. /* remove from active list */
  306. list_del_init(&txp->list);
  307. if (txp->callback)
  308. (*txp->callback)(txp, QIB_SDMA_TXREQ_S_OK);
  309. /* see if there is another txp */
  310. if (list_empty(&ppd->sdma_activelist))
  311. txp = NULL;
  312. else {
  313. lp = ppd->sdma_activelist.next;
  314. txp = list_entry(lp, struct qib_sdma_txreq,
  315. list);
  316. idx = txp->start_idx;
  317. }
  318. }
  319. progress = 1;
  320. }
  321. if (progress)
  322. qib_verbs_sdma_desc_avail(ppd, qib_sdma_descq_freecnt(ppd));
  323. return progress;
  324. }
  325. /*
  326. * This is called from interrupt context.
  327. */
  328. void qib_sdma_intr(struct qib_pportdata *ppd)
  329. {
  330. unsigned long flags;
  331. spin_lock_irqsave(&ppd->sdma_lock, flags);
  332. __qib_sdma_intr(ppd);
  333. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  334. }
  335. void __qib_sdma_intr(struct qib_pportdata *ppd)
  336. {
  337. if (__qib_sdma_running(ppd)) {
  338. qib_sdma_make_progress(ppd);
  339. if (!list_empty(&ppd->sdma_userpending))
  340. qib_user_sdma_send_desc(ppd, &ppd->sdma_userpending);
  341. }
  342. }
  343. int qib_setup_sdma(struct qib_pportdata *ppd)
  344. {
  345. struct qib_devdata *dd = ppd->dd;
  346. unsigned long flags;
  347. int ret = 0;
  348. ret = alloc_sdma(ppd);
  349. if (ret)
  350. goto bail;
  351. /* set consistent sdma state */
  352. ppd->dd->f_sdma_init_early(ppd);
  353. spin_lock_irqsave(&ppd->sdma_lock, flags);
  354. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  355. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  356. /* set up reference counting */
  357. kref_init(&ppd->sdma_state.kref);
  358. init_completion(&ppd->sdma_state.comp);
  359. ppd->sdma_generation = 0;
  360. ppd->sdma_descq_head = 0;
  361. ppd->sdma_descq_removed = 0;
  362. ppd->sdma_descq_added = 0;
  363. ppd->sdma_intrequest = 0;
  364. INIT_LIST_HEAD(&ppd->sdma_userpending);
  365. INIT_LIST_HEAD(&ppd->sdma_activelist);
  366. tasklet_init(&ppd->sdma_sw_clean_up_task, sdma_sw_clean_up_task,
  367. (unsigned long)ppd);
  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;
  474. len = dwords << 2;
  475. if (len > sge->length)
  476. len = sge->length;
  477. if (len > sge->sge_length)
  478. len = sge->sge_length;
  479. BUG_ON(len == 0);
  480. dw = (len + 3) >> 2;
  481. addr = dma_map_single(&ppd->dd->pcidev->dev, sge->vaddr,
  482. dw << 2, DMA_TO_DEVICE);
  483. if (dma_mapping_error(&ppd->dd->pcidev->dev, addr)) {
  484. ret = -ENOMEM;
  485. goto unmap;
  486. }
  487. sdmadesc[0] = 0;
  488. make_sdma_desc(ppd, sdmadesc, (u64) addr, dw, dwoffset);
  489. /* SDmaUseLargeBuf has to be set in every descriptor */
  490. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_USELARGEBUF)
  491. sdmadesc[0] |= SDMA_DESC_USE_LARGE_BUF;
  492. /* write to the descq */
  493. *descqp++ = cpu_to_le64(sdmadesc[0]);
  494. *descqp++ = cpu_to_le64(sdmadesc[1]);
  495. /* increment the tail */
  496. if (++tail == ppd->sdma_descq_cnt) {
  497. tail = 0;
  498. descqp = &ppd->sdma_descq[0].qw[0];
  499. ++ppd->sdma_generation;
  500. }
  501. sge->vaddr += len;
  502. sge->length -= len;
  503. sge->sge_length -= len;
  504. if (sge->sge_length == 0) {
  505. if (--ss->num_sge)
  506. *sge = *ss->sg_list++;
  507. } else if (sge->length == 0 && sge->mr->lkey) {
  508. if (++sge->n >= RVT_SEGSZ) {
  509. if (++sge->m >= sge->mr->mapsz)
  510. break;
  511. sge->n = 0;
  512. }
  513. sge->vaddr =
  514. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  515. sge->length =
  516. sge->mr->map[sge->m]->segs[sge->n].length;
  517. }
  518. dwoffset += dw;
  519. dwords -= dw;
  520. }
  521. if (!tail)
  522. descqp = &ppd->sdma_descq[ppd->sdma_descq_cnt].qw[0];
  523. descqp -= 2;
  524. descqp[0] |= cpu_to_le64(SDMA_DESC_LAST);
  525. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_HEADTOHOST)
  526. descqp[0] |= cpu_to_le64(SDMA_DESC_DMA_HEAD);
  527. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_INTREQ)
  528. descqp[0] |= cpu_to_le64(SDMA_DESC_INTR);
  529. priv = tx->qp->priv;
  530. atomic_inc(&priv->s_dma_busy);
  531. tx->txreq.next_descq_idx = tail;
  532. ppd->dd->f_sdma_update_tail(ppd, tail);
  533. ppd->sdma_descq_added += tx->txreq.sg_count;
  534. list_add_tail(&tx->txreq.list, &ppd->sdma_activelist);
  535. goto unlock;
  536. unmap:
  537. for (;;) {
  538. if (!tail)
  539. tail = ppd->sdma_descq_cnt - 1;
  540. else
  541. tail--;
  542. if (tail == ppd->sdma_descq_tail)
  543. break;
  544. unmap_desc(ppd, tail);
  545. }
  546. qp = tx->qp;
  547. priv = qp->priv;
  548. qib_put_txreq(tx);
  549. spin_lock(&qp->r_lock);
  550. spin_lock(&qp->s_lock);
  551. if (qp->ibqp.qp_type == IB_QPT_RC) {
  552. /* XXX what about error sending RDMA read responses? */
  553. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)
  554. rvt_error_qp(qp, IB_WC_GENERAL_ERR);
  555. } else if (qp->s_wqe)
  556. qib_send_complete(qp, qp->s_wqe, IB_WC_GENERAL_ERR);
  557. spin_unlock(&qp->s_lock);
  558. spin_unlock(&qp->r_lock);
  559. /* return zero to process the next send work request */
  560. goto unlock;
  561. busy:
  562. qp = tx->qp;
  563. priv = qp->priv;
  564. spin_lock(&qp->s_lock);
  565. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  566. struct qib_ibdev *dev;
  567. /*
  568. * If we couldn't queue the DMA request, save the info
  569. * and try again later rather than destroying the
  570. * buffer and undoing the side effects of the copy.
  571. */
  572. tx->ss = ss;
  573. tx->dwords = dwords;
  574. priv->s_tx = tx;
  575. dev = &ppd->dd->verbs_dev;
  576. spin_lock(&dev->rdi.pending_lock);
  577. if (list_empty(&priv->iowait)) {
  578. struct qib_ibport *ibp;
  579. ibp = &ppd->ibport_data;
  580. ibp->rvp.n_dmawait++;
  581. qp->s_flags |= RVT_S_WAIT_DMA_DESC;
  582. list_add_tail(&priv->iowait, &dev->dmawait);
  583. }
  584. spin_unlock(&dev->rdi.pending_lock);
  585. qp->s_flags &= ~RVT_S_BUSY;
  586. spin_unlock(&qp->s_lock);
  587. ret = -EBUSY;
  588. } else {
  589. spin_unlock(&qp->s_lock);
  590. qib_put_txreq(tx);
  591. }
  592. unlock:
  593. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  594. return ret;
  595. }
  596. /*
  597. * sdma_lock should be acquired before calling this routine
  598. */
  599. void dump_sdma_state(struct qib_pportdata *ppd)
  600. {
  601. struct qib_sdma_desc *descq;
  602. struct qib_sdma_txreq *txp, *txpnext;
  603. __le64 *descqp;
  604. u64 desc[2];
  605. u64 addr;
  606. u16 gen, dwlen, dwoffset;
  607. u16 head, tail, cnt;
  608. head = ppd->sdma_descq_head;
  609. tail = ppd->sdma_descq_tail;
  610. cnt = qib_sdma_descq_freecnt(ppd);
  611. descq = ppd->sdma_descq;
  612. qib_dev_porterr(ppd->dd, ppd->port,
  613. "SDMA ppd->sdma_descq_head: %u\n", head);
  614. qib_dev_porterr(ppd->dd, ppd->port,
  615. "SDMA ppd->sdma_descq_tail: %u\n", tail);
  616. qib_dev_porterr(ppd->dd, ppd->port,
  617. "SDMA sdma_descq_freecnt: %u\n", cnt);
  618. /* print info for each entry in the descriptor queue */
  619. while (head != tail) {
  620. char flags[6] = { 'x', 'x', 'x', 'x', 'x', 0 };
  621. descqp = &descq[head].qw[0];
  622. desc[0] = le64_to_cpu(descqp[0]);
  623. desc[1] = le64_to_cpu(descqp[1]);
  624. flags[0] = (desc[0] & 1<<15) ? 'I' : '-';
  625. flags[1] = (desc[0] & 1<<14) ? 'L' : 'S';
  626. flags[2] = (desc[0] & 1<<13) ? 'H' : '-';
  627. flags[3] = (desc[0] & 1<<12) ? 'F' : '-';
  628. flags[4] = (desc[0] & 1<<11) ? 'L' : '-';
  629. addr = (desc[1] << 32) | ((desc[0] >> 32) & 0xfffffffcULL);
  630. gen = (desc[0] >> 30) & 3ULL;
  631. dwlen = (desc[0] >> 14) & (0x7ffULL << 2);
  632. dwoffset = (desc[0] & 0x7ffULL) << 2;
  633. qib_dev_porterr(ppd->dd, ppd->port,
  634. "SDMA sdmadesc[%u]: flags:%s addr:0x%016llx gen:%u len:%u bytes offset:%u bytes\n",
  635. head, flags, addr, gen, dwlen, dwoffset);
  636. if (++head == ppd->sdma_descq_cnt)
  637. head = 0;
  638. }
  639. /* print dma descriptor indices from the TX requests */
  640. list_for_each_entry_safe(txp, txpnext, &ppd->sdma_activelist,
  641. list)
  642. qib_dev_porterr(ppd->dd, ppd->port,
  643. "SDMA txp->start_idx: %u txp->next_descq_idx: %u\n",
  644. txp->start_idx, txp->next_descq_idx);
  645. }
  646. void qib_sdma_process_event(struct qib_pportdata *ppd,
  647. enum qib_sdma_events event)
  648. {
  649. unsigned long flags;
  650. spin_lock_irqsave(&ppd->sdma_lock, flags);
  651. __qib_sdma_process_event(ppd, event);
  652. if (ppd->sdma_state.current_state == qib_sdma_state_s99_running)
  653. qib_verbs_sdma_desc_avail(ppd, qib_sdma_descq_freecnt(ppd));
  654. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  655. }
  656. void __qib_sdma_process_event(struct qib_pportdata *ppd,
  657. enum qib_sdma_events event)
  658. {
  659. struct qib_sdma_state *ss = &ppd->sdma_state;
  660. switch (ss->current_state) {
  661. case qib_sdma_state_s00_hw_down:
  662. switch (event) {
  663. case qib_sdma_event_e00_go_hw_down:
  664. break;
  665. case qib_sdma_event_e30_go_running:
  666. /*
  667. * If down, but running requested (usually result
  668. * of link up, then we need to start up.
  669. * This can happen when hw down is requested while
  670. * bringing the link up with traffic active on
  671. * 7220, e.g. */
  672. ss->go_s99_running = 1;
  673. /* fall through -- and start dma engine */
  674. case qib_sdma_event_e10_go_hw_start:
  675. /* This reference means the state machine is started */
  676. sdma_get(&ppd->sdma_state);
  677. sdma_set_state(ppd,
  678. qib_sdma_state_s10_hw_start_up_wait);
  679. break;
  680. case qib_sdma_event_e20_hw_started:
  681. break;
  682. case qib_sdma_event_e40_sw_cleaned:
  683. sdma_sw_tear_down(ppd);
  684. break;
  685. case qib_sdma_event_e50_hw_cleaned:
  686. break;
  687. case qib_sdma_event_e60_hw_halted:
  688. break;
  689. case qib_sdma_event_e70_go_idle:
  690. break;
  691. case qib_sdma_event_e7220_err_halted:
  692. break;
  693. case qib_sdma_event_e7322_err_halted:
  694. break;
  695. case qib_sdma_event_e90_timer_tick:
  696. break;
  697. }
  698. break;
  699. case qib_sdma_state_s10_hw_start_up_wait:
  700. switch (event) {
  701. case qib_sdma_event_e00_go_hw_down:
  702. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  703. sdma_sw_tear_down(ppd);
  704. break;
  705. case qib_sdma_event_e10_go_hw_start:
  706. break;
  707. case qib_sdma_event_e20_hw_started:
  708. sdma_set_state(ppd, ss->go_s99_running ?
  709. qib_sdma_state_s99_running :
  710. qib_sdma_state_s20_idle);
  711. break;
  712. case qib_sdma_event_e30_go_running:
  713. ss->go_s99_running = 1;
  714. break;
  715. case qib_sdma_event_e40_sw_cleaned:
  716. break;
  717. case qib_sdma_event_e50_hw_cleaned:
  718. break;
  719. case qib_sdma_event_e60_hw_halted:
  720. break;
  721. case qib_sdma_event_e70_go_idle:
  722. ss->go_s99_running = 0;
  723. break;
  724. case qib_sdma_event_e7220_err_halted:
  725. break;
  726. case qib_sdma_event_e7322_err_halted:
  727. break;
  728. case qib_sdma_event_e90_timer_tick:
  729. break;
  730. }
  731. break;
  732. case qib_sdma_state_s20_idle:
  733. switch (event) {
  734. case qib_sdma_event_e00_go_hw_down:
  735. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  736. sdma_sw_tear_down(ppd);
  737. break;
  738. case qib_sdma_event_e10_go_hw_start:
  739. break;
  740. case qib_sdma_event_e20_hw_started:
  741. break;
  742. case qib_sdma_event_e30_go_running:
  743. sdma_set_state(ppd, qib_sdma_state_s99_running);
  744. ss->go_s99_running = 1;
  745. break;
  746. case qib_sdma_event_e40_sw_cleaned:
  747. break;
  748. case qib_sdma_event_e50_hw_cleaned:
  749. break;
  750. case qib_sdma_event_e60_hw_halted:
  751. break;
  752. case qib_sdma_event_e70_go_idle:
  753. break;
  754. case qib_sdma_event_e7220_err_halted:
  755. break;
  756. case qib_sdma_event_e7322_err_halted:
  757. break;
  758. case qib_sdma_event_e90_timer_tick:
  759. break;
  760. }
  761. break;
  762. case qib_sdma_state_s30_sw_clean_up_wait:
  763. switch (event) {
  764. case qib_sdma_event_e00_go_hw_down:
  765. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  766. break;
  767. case qib_sdma_event_e10_go_hw_start:
  768. break;
  769. case qib_sdma_event_e20_hw_started:
  770. break;
  771. case qib_sdma_event_e30_go_running:
  772. ss->go_s99_running = 1;
  773. break;
  774. case qib_sdma_event_e40_sw_cleaned:
  775. sdma_set_state(ppd,
  776. qib_sdma_state_s10_hw_start_up_wait);
  777. sdma_hw_start_up(ppd);
  778. break;
  779. case qib_sdma_event_e50_hw_cleaned:
  780. break;
  781. case qib_sdma_event_e60_hw_halted:
  782. break;
  783. case qib_sdma_event_e70_go_idle:
  784. ss->go_s99_running = 0;
  785. break;
  786. case qib_sdma_event_e7220_err_halted:
  787. break;
  788. case qib_sdma_event_e7322_err_halted:
  789. break;
  790. case qib_sdma_event_e90_timer_tick:
  791. break;
  792. }
  793. break;
  794. case qib_sdma_state_s40_hw_clean_up_wait:
  795. switch (event) {
  796. case qib_sdma_event_e00_go_hw_down:
  797. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  798. sdma_start_sw_clean_up(ppd);
  799. break;
  800. case qib_sdma_event_e10_go_hw_start:
  801. break;
  802. case qib_sdma_event_e20_hw_started:
  803. break;
  804. case qib_sdma_event_e30_go_running:
  805. ss->go_s99_running = 1;
  806. break;
  807. case qib_sdma_event_e40_sw_cleaned:
  808. break;
  809. case qib_sdma_event_e50_hw_cleaned:
  810. sdma_set_state(ppd,
  811. qib_sdma_state_s30_sw_clean_up_wait);
  812. sdma_start_sw_clean_up(ppd);
  813. break;
  814. case qib_sdma_event_e60_hw_halted:
  815. break;
  816. case qib_sdma_event_e70_go_idle:
  817. ss->go_s99_running = 0;
  818. break;
  819. case qib_sdma_event_e7220_err_halted:
  820. break;
  821. case qib_sdma_event_e7322_err_halted:
  822. break;
  823. case qib_sdma_event_e90_timer_tick:
  824. break;
  825. }
  826. break;
  827. case qib_sdma_state_s50_hw_halt_wait:
  828. switch (event) {
  829. case qib_sdma_event_e00_go_hw_down:
  830. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  831. sdma_start_sw_clean_up(ppd);
  832. break;
  833. case qib_sdma_event_e10_go_hw_start:
  834. break;
  835. case qib_sdma_event_e20_hw_started:
  836. break;
  837. case qib_sdma_event_e30_go_running:
  838. ss->go_s99_running = 1;
  839. break;
  840. case qib_sdma_event_e40_sw_cleaned:
  841. break;
  842. case qib_sdma_event_e50_hw_cleaned:
  843. break;
  844. case qib_sdma_event_e60_hw_halted:
  845. sdma_set_state(ppd,
  846. qib_sdma_state_s40_hw_clean_up_wait);
  847. ppd->dd->f_sdma_hw_clean_up(ppd);
  848. break;
  849. case qib_sdma_event_e70_go_idle:
  850. ss->go_s99_running = 0;
  851. break;
  852. case qib_sdma_event_e7220_err_halted:
  853. break;
  854. case qib_sdma_event_e7322_err_halted:
  855. break;
  856. case qib_sdma_event_e90_timer_tick:
  857. break;
  858. }
  859. break;
  860. case qib_sdma_state_s99_running:
  861. switch (event) {
  862. case qib_sdma_event_e00_go_hw_down:
  863. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  864. sdma_start_sw_clean_up(ppd);
  865. break;
  866. case qib_sdma_event_e10_go_hw_start:
  867. break;
  868. case qib_sdma_event_e20_hw_started:
  869. break;
  870. case qib_sdma_event_e30_go_running:
  871. break;
  872. case qib_sdma_event_e40_sw_cleaned:
  873. break;
  874. case qib_sdma_event_e50_hw_cleaned:
  875. break;
  876. case qib_sdma_event_e60_hw_halted:
  877. sdma_set_state(ppd,
  878. qib_sdma_state_s30_sw_clean_up_wait);
  879. sdma_start_sw_clean_up(ppd);
  880. break;
  881. case qib_sdma_event_e70_go_idle:
  882. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  883. ss->go_s99_running = 0;
  884. break;
  885. case qib_sdma_event_e7220_err_halted:
  886. sdma_set_state(ppd,
  887. qib_sdma_state_s30_sw_clean_up_wait);
  888. sdma_start_sw_clean_up(ppd);
  889. break;
  890. case qib_sdma_event_e7322_err_halted:
  891. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  892. break;
  893. case qib_sdma_event_e90_timer_tick:
  894. break;
  895. }
  896. break;
  897. }
  898. ss->last_event = event;
  899. }