qib_sdma.c 26 KB

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