qib_sdma.c 26 KB

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