qib_sdma.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. void qib_sdma_process_event(struct qib_pportdata *ppd,
  607. enum qib_sdma_events event)
  608. {
  609. unsigned long flags;
  610. spin_lock_irqsave(&ppd->sdma_lock, flags);
  611. __qib_sdma_process_event(ppd, event);
  612. if (ppd->sdma_state.current_state == qib_sdma_state_s99_running)
  613. qib_verbs_sdma_desc_avail(ppd, qib_sdma_descq_freecnt(ppd));
  614. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  615. }
  616. void __qib_sdma_process_event(struct qib_pportdata *ppd,
  617. enum qib_sdma_events event)
  618. {
  619. struct qib_sdma_state *ss = &ppd->sdma_state;
  620. switch (ss->current_state) {
  621. case qib_sdma_state_s00_hw_down:
  622. switch (event) {
  623. case qib_sdma_event_e00_go_hw_down:
  624. break;
  625. case qib_sdma_event_e30_go_running:
  626. /*
  627. * If down, but running requested (usually result
  628. * of link up, then we need to start up.
  629. * This can happen when hw down is requested while
  630. * bringing the link up with traffic active on
  631. * 7220, e.g. */
  632. ss->go_s99_running = 1;
  633. /* fall through and start dma engine */
  634. case qib_sdma_event_e10_go_hw_start:
  635. /* This reference means the state machine is started */
  636. sdma_get(&ppd->sdma_state);
  637. sdma_set_state(ppd,
  638. qib_sdma_state_s10_hw_start_up_wait);
  639. break;
  640. case qib_sdma_event_e20_hw_started:
  641. break;
  642. case qib_sdma_event_e40_sw_cleaned:
  643. sdma_sw_tear_down(ppd);
  644. break;
  645. case qib_sdma_event_e50_hw_cleaned:
  646. break;
  647. case qib_sdma_event_e60_hw_halted:
  648. break;
  649. case qib_sdma_event_e70_go_idle:
  650. break;
  651. case qib_sdma_event_e7220_err_halted:
  652. break;
  653. case qib_sdma_event_e7322_err_halted:
  654. break;
  655. case qib_sdma_event_e90_timer_tick:
  656. break;
  657. }
  658. break;
  659. case qib_sdma_state_s10_hw_start_up_wait:
  660. switch (event) {
  661. case qib_sdma_event_e00_go_hw_down:
  662. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  663. sdma_sw_tear_down(ppd);
  664. break;
  665. case qib_sdma_event_e10_go_hw_start:
  666. break;
  667. case qib_sdma_event_e20_hw_started:
  668. sdma_set_state(ppd, ss->go_s99_running ?
  669. qib_sdma_state_s99_running :
  670. qib_sdma_state_s20_idle);
  671. break;
  672. case qib_sdma_event_e30_go_running:
  673. ss->go_s99_running = 1;
  674. break;
  675. case qib_sdma_event_e40_sw_cleaned:
  676. break;
  677. case qib_sdma_event_e50_hw_cleaned:
  678. break;
  679. case qib_sdma_event_e60_hw_halted:
  680. break;
  681. case qib_sdma_event_e70_go_idle:
  682. ss->go_s99_running = 0;
  683. break;
  684. case qib_sdma_event_e7220_err_halted:
  685. break;
  686. case qib_sdma_event_e7322_err_halted:
  687. break;
  688. case qib_sdma_event_e90_timer_tick:
  689. break;
  690. }
  691. break;
  692. case qib_sdma_state_s20_idle:
  693. switch (event) {
  694. case qib_sdma_event_e00_go_hw_down:
  695. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  696. sdma_sw_tear_down(ppd);
  697. break;
  698. case qib_sdma_event_e10_go_hw_start:
  699. break;
  700. case qib_sdma_event_e20_hw_started:
  701. break;
  702. case qib_sdma_event_e30_go_running:
  703. sdma_set_state(ppd, qib_sdma_state_s99_running);
  704. ss->go_s99_running = 1;
  705. break;
  706. case qib_sdma_event_e40_sw_cleaned:
  707. break;
  708. case qib_sdma_event_e50_hw_cleaned:
  709. break;
  710. case qib_sdma_event_e60_hw_halted:
  711. break;
  712. case qib_sdma_event_e70_go_idle:
  713. break;
  714. case qib_sdma_event_e7220_err_halted:
  715. break;
  716. case qib_sdma_event_e7322_err_halted:
  717. break;
  718. case qib_sdma_event_e90_timer_tick:
  719. break;
  720. }
  721. break;
  722. case qib_sdma_state_s30_sw_clean_up_wait:
  723. switch (event) {
  724. case qib_sdma_event_e00_go_hw_down:
  725. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  726. break;
  727. case qib_sdma_event_e10_go_hw_start:
  728. break;
  729. case qib_sdma_event_e20_hw_started:
  730. break;
  731. case qib_sdma_event_e30_go_running:
  732. ss->go_s99_running = 1;
  733. break;
  734. case qib_sdma_event_e40_sw_cleaned:
  735. sdma_set_state(ppd,
  736. qib_sdma_state_s10_hw_start_up_wait);
  737. sdma_hw_start_up(ppd);
  738. break;
  739. case qib_sdma_event_e50_hw_cleaned:
  740. break;
  741. case qib_sdma_event_e60_hw_halted:
  742. break;
  743. case qib_sdma_event_e70_go_idle:
  744. ss->go_s99_running = 0;
  745. break;
  746. case qib_sdma_event_e7220_err_halted:
  747. break;
  748. case qib_sdma_event_e7322_err_halted:
  749. break;
  750. case qib_sdma_event_e90_timer_tick:
  751. break;
  752. }
  753. break;
  754. case qib_sdma_state_s40_hw_clean_up_wait:
  755. switch (event) {
  756. case qib_sdma_event_e00_go_hw_down:
  757. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  758. sdma_start_sw_clean_up(ppd);
  759. break;
  760. case qib_sdma_event_e10_go_hw_start:
  761. break;
  762. case qib_sdma_event_e20_hw_started:
  763. break;
  764. case qib_sdma_event_e30_go_running:
  765. ss->go_s99_running = 1;
  766. break;
  767. case qib_sdma_event_e40_sw_cleaned:
  768. break;
  769. case qib_sdma_event_e50_hw_cleaned:
  770. sdma_set_state(ppd,
  771. qib_sdma_state_s30_sw_clean_up_wait);
  772. sdma_start_sw_clean_up(ppd);
  773. break;
  774. case qib_sdma_event_e60_hw_halted:
  775. break;
  776. case qib_sdma_event_e70_go_idle:
  777. ss->go_s99_running = 0;
  778. break;
  779. case qib_sdma_event_e7220_err_halted:
  780. break;
  781. case qib_sdma_event_e7322_err_halted:
  782. break;
  783. case qib_sdma_event_e90_timer_tick:
  784. break;
  785. }
  786. break;
  787. case qib_sdma_state_s50_hw_halt_wait:
  788. switch (event) {
  789. case qib_sdma_event_e00_go_hw_down:
  790. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  791. sdma_start_sw_clean_up(ppd);
  792. break;
  793. case qib_sdma_event_e10_go_hw_start:
  794. break;
  795. case qib_sdma_event_e20_hw_started:
  796. break;
  797. case qib_sdma_event_e30_go_running:
  798. ss->go_s99_running = 1;
  799. break;
  800. case qib_sdma_event_e40_sw_cleaned:
  801. break;
  802. case qib_sdma_event_e50_hw_cleaned:
  803. break;
  804. case qib_sdma_event_e60_hw_halted:
  805. sdma_set_state(ppd,
  806. qib_sdma_state_s40_hw_clean_up_wait);
  807. ppd->dd->f_sdma_hw_clean_up(ppd);
  808. break;
  809. case qib_sdma_event_e70_go_idle:
  810. ss->go_s99_running = 0;
  811. break;
  812. case qib_sdma_event_e7220_err_halted:
  813. break;
  814. case qib_sdma_event_e7322_err_halted:
  815. break;
  816. case qib_sdma_event_e90_timer_tick:
  817. break;
  818. }
  819. break;
  820. case qib_sdma_state_s99_running:
  821. switch (event) {
  822. case qib_sdma_event_e00_go_hw_down:
  823. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  824. sdma_start_sw_clean_up(ppd);
  825. break;
  826. case qib_sdma_event_e10_go_hw_start:
  827. break;
  828. case qib_sdma_event_e20_hw_started:
  829. break;
  830. case qib_sdma_event_e30_go_running:
  831. break;
  832. case qib_sdma_event_e40_sw_cleaned:
  833. break;
  834. case qib_sdma_event_e50_hw_cleaned:
  835. break;
  836. case qib_sdma_event_e60_hw_halted:
  837. sdma_set_state(ppd,
  838. qib_sdma_state_s30_sw_clean_up_wait);
  839. sdma_start_sw_clean_up(ppd);
  840. break;
  841. case qib_sdma_event_e70_go_idle:
  842. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  843. ss->go_s99_running = 0;
  844. break;
  845. case qib_sdma_event_e7220_err_halted:
  846. sdma_set_state(ppd,
  847. qib_sdma_state_s30_sw_clean_up_wait);
  848. sdma_start_sw_clean_up(ppd);
  849. break;
  850. case qib_sdma_event_e7322_err_halted:
  851. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  852. break;
  853. case qib_sdma_event_e90_timer_tick:
  854. break;
  855. }
  856. break;
  857. }
  858. ss->last_event = event;
  859. }