qib_sdma.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. if (!list_empty(&ppd->sdma_userpending))
  362. qib_user_sdma_send_desc(ppd, &ppd->sdma_userpending);
  363. }
  364. }
  365. int qib_setup_sdma(struct qib_pportdata *ppd)
  366. {
  367. struct qib_devdata *dd = ppd->dd;
  368. unsigned long flags;
  369. int ret = 0;
  370. ret = alloc_sdma(ppd);
  371. if (ret)
  372. goto bail;
  373. /* set consistent sdma state */
  374. ppd->dd->f_sdma_init_early(ppd);
  375. spin_lock_irqsave(&ppd->sdma_lock, flags);
  376. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  377. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  378. /* set up reference counting */
  379. kref_init(&ppd->sdma_state.kref);
  380. init_completion(&ppd->sdma_state.comp);
  381. ppd->sdma_generation = 0;
  382. ppd->sdma_descq_head = 0;
  383. ppd->sdma_descq_removed = 0;
  384. ppd->sdma_descq_added = 0;
  385. ppd->sdma_intrequest = 0;
  386. INIT_LIST_HEAD(&ppd->sdma_userpending);
  387. INIT_LIST_HEAD(&ppd->sdma_activelist);
  388. tasklet_init(&ppd->sdma_sw_clean_up_task, sdma_sw_clean_up_task,
  389. (unsigned long)ppd);
  390. ret = dd->f_init_sdma_regs(ppd);
  391. if (ret)
  392. goto bail_alloc;
  393. qib_sdma_process_event(ppd, qib_sdma_event_e10_go_hw_start);
  394. return 0;
  395. bail_alloc:
  396. qib_teardown_sdma(ppd);
  397. bail:
  398. return ret;
  399. }
  400. void qib_teardown_sdma(struct qib_pportdata *ppd)
  401. {
  402. qib_sdma_process_event(ppd, qib_sdma_event_e00_go_hw_down);
  403. /*
  404. * This waits for the state machine to exit so it is not
  405. * necessary to kill the sdma_sw_clean_up_task to make sure
  406. * it is not running.
  407. */
  408. sdma_finalput(&ppd->sdma_state);
  409. free_sdma(ppd);
  410. }
  411. int qib_sdma_running(struct qib_pportdata *ppd)
  412. {
  413. unsigned long flags;
  414. int ret;
  415. spin_lock_irqsave(&ppd->sdma_lock, flags);
  416. ret = __qib_sdma_running(ppd);
  417. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  418. return ret;
  419. }
  420. /*
  421. * Complete a request when sdma not running; likely only request
  422. * but to simplify the code, always queue it, then process the full
  423. * activelist. We process the entire list to ensure that this particular
  424. * request does get it's callback, but in the correct order.
  425. * Must be called with sdma_lock held
  426. */
  427. static void complete_sdma_err_req(struct qib_pportdata *ppd,
  428. struct qib_verbs_txreq *tx)
  429. {
  430. atomic_inc(&tx->qp->s_dma_busy);
  431. /* no sdma descriptors, so no unmap_desc */
  432. tx->txreq.start_idx = 0;
  433. tx->txreq.next_descq_idx = 0;
  434. list_add_tail(&tx->txreq.list, &ppd->sdma_activelist);
  435. clear_sdma_activelist(ppd);
  436. }
  437. /*
  438. * This function queues one IB packet onto the send DMA queue per call.
  439. * The caller is responsible for checking:
  440. * 1) The number of send DMA descriptor entries is less than the size of
  441. * the descriptor queue.
  442. * 2) The IB SGE addresses and lengths are 32-bit aligned
  443. * (except possibly the last SGE's length)
  444. * 3) The SGE addresses are suitable for passing to dma_map_single().
  445. */
  446. int qib_sdma_verbs_send(struct qib_pportdata *ppd,
  447. struct qib_sge_state *ss, u32 dwords,
  448. struct qib_verbs_txreq *tx)
  449. {
  450. unsigned long flags;
  451. struct qib_sge *sge;
  452. struct qib_qp *qp;
  453. int ret = 0;
  454. u16 tail;
  455. __le64 *descqp;
  456. u64 sdmadesc[2];
  457. u32 dwoffset;
  458. dma_addr_t addr;
  459. spin_lock_irqsave(&ppd->sdma_lock, flags);
  460. retry:
  461. if (unlikely(!__qib_sdma_running(ppd))) {
  462. complete_sdma_err_req(ppd, tx);
  463. goto unlock;
  464. }
  465. if (tx->txreq.sg_count > qib_sdma_descq_freecnt(ppd)) {
  466. if (qib_sdma_make_progress(ppd))
  467. goto retry;
  468. if (ppd->dd->flags & QIB_HAS_SDMA_TIMEOUT)
  469. ppd->dd->f_sdma_set_desc_cnt(ppd,
  470. ppd->sdma_descq_cnt / 2);
  471. goto busy;
  472. }
  473. dwoffset = tx->hdr_dwords;
  474. make_sdma_desc(ppd, sdmadesc, (u64) tx->txreq.addr, dwoffset, 0);
  475. sdmadesc[0] |= SDMA_DESC_FIRST;
  476. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_USELARGEBUF)
  477. sdmadesc[0] |= SDMA_DESC_USE_LARGE_BUF;
  478. /* write to the descq */
  479. tail = ppd->sdma_descq_tail;
  480. descqp = &ppd->sdma_descq[tail].qw[0];
  481. *descqp++ = cpu_to_le64(sdmadesc[0]);
  482. *descqp++ = cpu_to_le64(sdmadesc[1]);
  483. /* increment the tail */
  484. if (++tail == ppd->sdma_descq_cnt) {
  485. tail = 0;
  486. descqp = &ppd->sdma_descq[0].qw[0];
  487. ++ppd->sdma_generation;
  488. }
  489. tx->txreq.start_idx = tail;
  490. sge = &ss->sge;
  491. while (dwords) {
  492. u32 dw;
  493. u32 len;
  494. len = dwords << 2;
  495. if (len > sge->length)
  496. len = sge->length;
  497. if (len > sge->sge_length)
  498. len = sge->sge_length;
  499. BUG_ON(len == 0);
  500. dw = (len + 3) >> 2;
  501. addr = dma_map_single(&ppd->dd->pcidev->dev, sge->vaddr,
  502. dw << 2, DMA_TO_DEVICE);
  503. if (dma_mapping_error(&ppd->dd->pcidev->dev, addr))
  504. goto unmap;
  505. sdmadesc[0] = 0;
  506. make_sdma_desc(ppd, sdmadesc, (u64) addr, dw, dwoffset);
  507. /* SDmaUseLargeBuf has to be set in every descriptor */
  508. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_USELARGEBUF)
  509. sdmadesc[0] |= SDMA_DESC_USE_LARGE_BUF;
  510. /* write to the descq */
  511. *descqp++ = cpu_to_le64(sdmadesc[0]);
  512. *descqp++ = cpu_to_le64(sdmadesc[1]);
  513. /* increment the tail */
  514. if (++tail == ppd->sdma_descq_cnt) {
  515. tail = 0;
  516. descqp = &ppd->sdma_descq[0].qw[0];
  517. ++ppd->sdma_generation;
  518. }
  519. sge->vaddr += len;
  520. sge->length -= len;
  521. sge->sge_length -= len;
  522. if (sge->sge_length == 0) {
  523. if (--ss->num_sge)
  524. *sge = *ss->sg_list++;
  525. } else if (sge->length == 0 && sge->mr->lkey) {
  526. if (++sge->n >= QIB_SEGSZ) {
  527. if (++sge->m >= sge->mr->mapsz)
  528. break;
  529. sge->n = 0;
  530. }
  531. sge->vaddr =
  532. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  533. sge->length =
  534. sge->mr->map[sge->m]->segs[sge->n].length;
  535. }
  536. dwoffset += dw;
  537. dwords -= dw;
  538. }
  539. if (!tail)
  540. descqp = &ppd->sdma_descq[ppd->sdma_descq_cnt].qw[0];
  541. descqp -= 2;
  542. descqp[0] |= cpu_to_le64(SDMA_DESC_LAST);
  543. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_HEADTOHOST)
  544. descqp[0] |= cpu_to_le64(SDMA_DESC_DMA_HEAD);
  545. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_INTREQ)
  546. descqp[0] |= cpu_to_le64(SDMA_DESC_INTR);
  547. atomic_inc(&tx->qp->s_dma_busy);
  548. tx->txreq.next_descq_idx = tail;
  549. ppd->dd->f_sdma_update_tail(ppd, tail);
  550. ppd->sdma_descq_added += tx->txreq.sg_count;
  551. list_add_tail(&tx->txreq.list, &ppd->sdma_activelist);
  552. goto unlock;
  553. unmap:
  554. for (;;) {
  555. if (!tail)
  556. tail = ppd->sdma_descq_cnt - 1;
  557. else
  558. tail--;
  559. if (tail == ppd->sdma_descq_tail)
  560. break;
  561. unmap_desc(ppd, tail);
  562. }
  563. qp = tx->qp;
  564. qib_put_txreq(tx);
  565. spin_lock(&qp->r_lock);
  566. spin_lock(&qp->s_lock);
  567. if (qp->ibqp.qp_type == IB_QPT_RC) {
  568. /* XXX what about error sending RDMA read responses? */
  569. if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)
  570. qib_error_qp(qp, IB_WC_GENERAL_ERR);
  571. } else if (qp->s_wqe)
  572. qib_send_complete(qp, qp->s_wqe, IB_WC_GENERAL_ERR);
  573. spin_unlock(&qp->s_lock);
  574. spin_unlock(&qp->r_lock);
  575. /* return zero to process the next send work request */
  576. goto unlock;
  577. busy:
  578. qp = tx->qp;
  579. spin_lock(&qp->s_lock);
  580. if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
  581. struct qib_ibdev *dev;
  582. /*
  583. * If we couldn't queue the DMA request, save the info
  584. * and try again later rather than destroying the
  585. * buffer and undoing the side effects of the copy.
  586. */
  587. tx->ss = ss;
  588. tx->dwords = dwords;
  589. qp->s_tx = tx;
  590. dev = &ppd->dd->verbs_dev;
  591. spin_lock(&dev->pending_lock);
  592. if (list_empty(&qp->iowait)) {
  593. struct qib_ibport *ibp;
  594. ibp = &ppd->ibport_data;
  595. ibp->n_dmawait++;
  596. qp->s_flags |= QIB_S_WAIT_DMA_DESC;
  597. list_add_tail(&qp->iowait, &dev->dmawait);
  598. }
  599. spin_unlock(&dev->pending_lock);
  600. qp->s_flags &= ~QIB_S_BUSY;
  601. spin_unlock(&qp->s_lock);
  602. ret = -EBUSY;
  603. } else {
  604. spin_unlock(&qp->s_lock);
  605. qib_put_txreq(tx);
  606. }
  607. unlock:
  608. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  609. return ret;
  610. }
  611. /*
  612. * sdma_lock should be acquired before calling this routine
  613. */
  614. void dump_sdma_state(struct qib_pportdata *ppd)
  615. {
  616. struct qib_sdma_desc *descq;
  617. struct qib_sdma_txreq *txp, *txpnext;
  618. __le64 *descqp;
  619. u64 desc[2];
  620. u64 addr;
  621. u16 gen, dwlen, dwoffset;
  622. u16 head, tail, cnt;
  623. head = ppd->sdma_descq_head;
  624. tail = ppd->sdma_descq_tail;
  625. cnt = qib_sdma_descq_freecnt(ppd);
  626. descq = ppd->sdma_descq;
  627. qib_dev_porterr(ppd->dd, ppd->port,
  628. "SDMA ppd->sdma_descq_head: %u\n", head);
  629. qib_dev_porterr(ppd->dd, ppd->port,
  630. "SDMA ppd->sdma_descq_tail: %u\n", tail);
  631. qib_dev_porterr(ppd->dd, ppd->port,
  632. "SDMA sdma_descq_freecnt: %u\n", cnt);
  633. /* print info for each entry in the descriptor queue */
  634. while (head != tail) {
  635. char flags[6] = { 'x', 'x', 'x', 'x', 'x', 0 };
  636. descqp = &descq[head].qw[0];
  637. desc[0] = le64_to_cpu(descqp[0]);
  638. desc[1] = le64_to_cpu(descqp[1]);
  639. flags[0] = (desc[0] & 1<<15) ? 'I' : '-';
  640. flags[1] = (desc[0] & 1<<14) ? 'L' : 'S';
  641. flags[2] = (desc[0] & 1<<13) ? 'H' : '-';
  642. flags[3] = (desc[0] & 1<<12) ? 'F' : '-';
  643. flags[4] = (desc[0] & 1<<11) ? 'L' : '-';
  644. addr = (desc[1] << 32) | ((desc[0] >> 32) & 0xfffffffcULL);
  645. gen = (desc[0] >> 30) & 3ULL;
  646. dwlen = (desc[0] >> 14) & (0x7ffULL << 2);
  647. dwoffset = (desc[0] & 0x7ffULL) << 2;
  648. qib_dev_porterr(ppd->dd, ppd->port,
  649. "SDMA sdmadesc[%u]: flags:%s addr:0x%016llx gen:%u len:%u bytes offset:%u bytes\n",
  650. head, flags, addr, gen, dwlen, dwoffset);
  651. if (++head == ppd->sdma_descq_cnt)
  652. head = 0;
  653. }
  654. /* print dma descriptor indices from the TX requests */
  655. list_for_each_entry_safe(txp, txpnext, &ppd->sdma_activelist,
  656. list)
  657. qib_dev_porterr(ppd->dd, ppd->port,
  658. "SDMA txp->start_idx: %u txp->next_descq_idx: %u\n",
  659. txp->start_idx, txp->next_descq_idx);
  660. }
  661. void qib_sdma_process_event(struct qib_pportdata *ppd,
  662. enum qib_sdma_events event)
  663. {
  664. unsigned long flags;
  665. spin_lock_irqsave(&ppd->sdma_lock, flags);
  666. __qib_sdma_process_event(ppd, event);
  667. if (ppd->sdma_state.current_state == qib_sdma_state_s99_running)
  668. qib_verbs_sdma_desc_avail(ppd, qib_sdma_descq_freecnt(ppd));
  669. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  670. }
  671. void __qib_sdma_process_event(struct qib_pportdata *ppd,
  672. enum qib_sdma_events event)
  673. {
  674. struct qib_sdma_state *ss = &ppd->sdma_state;
  675. switch (ss->current_state) {
  676. case qib_sdma_state_s00_hw_down:
  677. switch (event) {
  678. case qib_sdma_event_e00_go_hw_down:
  679. break;
  680. case qib_sdma_event_e30_go_running:
  681. /*
  682. * If down, but running requested (usually result
  683. * of link up, then we need to start up.
  684. * This can happen when hw down is requested while
  685. * bringing the link up with traffic active on
  686. * 7220, e.g. */
  687. ss->go_s99_running = 1;
  688. /* fall through and start dma engine */
  689. case qib_sdma_event_e10_go_hw_start:
  690. /* This reference means the state machine is started */
  691. sdma_get(&ppd->sdma_state);
  692. sdma_set_state(ppd,
  693. qib_sdma_state_s10_hw_start_up_wait);
  694. break;
  695. case qib_sdma_event_e20_hw_started:
  696. break;
  697. case qib_sdma_event_e40_sw_cleaned:
  698. sdma_sw_tear_down(ppd);
  699. break;
  700. case qib_sdma_event_e50_hw_cleaned:
  701. break;
  702. case qib_sdma_event_e60_hw_halted:
  703. break;
  704. case qib_sdma_event_e70_go_idle:
  705. break;
  706. case qib_sdma_event_e7220_err_halted:
  707. break;
  708. case qib_sdma_event_e7322_err_halted:
  709. break;
  710. case qib_sdma_event_e90_timer_tick:
  711. break;
  712. }
  713. break;
  714. case qib_sdma_state_s10_hw_start_up_wait:
  715. switch (event) {
  716. case qib_sdma_event_e00_go_hw_down:
  717. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  718. sdma_sw_tear_down(ppd);
  719. break;
  720. case qib_sdma_event_e10_go_hw_start:
  721. break;
  722. case qib_sdma_event_e20_hw_started:
  723. sdma_set_state(ppd, ss->go_s99_running ?
  724. qib_sdma_state_s99_running :
  725. qib_sdma_state_s20_idle);
  726. break;
  727. case qib_sdma_event_e30_go_running:
  728. ss->go_s99_running = 1;
  729. break;
  730. case qib_sdma_event_e40_sw_cleaned:
  731. break;
  732. case qib_sdma_event_e50_hw_cleaned:
  733. break;
  734. case qib_sdma_event_e60_hw_halted:
  735. break;
  736. case qib_sdma_event_e70_go_idle:
  737. ss->go_s99_running = 0;
  738. break;
  739. case qib_sdma_event_e7220_err_halted:
  740. break;
  741. case qib_sdma_event_e7322_err_halted:
  742. break;
  743. case qib_sdma_event_e90_timer_tick:
  744. break;
  745. }
  746. break;
  747. case qib_sdma_state_s20_idle:
  748. switch (event) {
  749. case qib_sdma_event_e00_go_hw_down:
  750. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  751. sdma_sw_tear_down(ppd);
  752. break;
  753. case qib_sdma_event_e10_go_hw_start:
  754. break;
  755. case qib_sdma_event_e20_hw_started:
  756. break;
  757. case qib_sdma_event_e30_go_running:
  758. sdma_set_state(ppd, qib_sdma_state_s99_running);
  759. ss->go_s99_running = 1;
  760. break;
  761. case qib_sdma_event_e40_sw_cleaned:
  762. break;
  763. case qib_sdma_event_e50_hw_cleaned:
  764. break;
  765. case qib_sdma_event_e60_hw_halted:
  766. break;
  767. case qib_sdma_event_e70_go_idle:
  768. break;
  769. case qib_sdma_event_e7220_err_halted:
  770. break;
  771. case qib_sdma_event_e7322_err_halted:
  772. break;
  773. case qib_sdma_event_e90_timer_tick:
  774. break;
  775. }
  776. break;
  777. case qib_sdma_state_s30_sw_clean_up_wait:
  778. switch (event) {
  779. case qib_sdma_event_e00_go_hw_down:
  780. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  781. break;
  782. case qib_sdma_event_e10_go_hw_start:
  783. break;
  784. case qib_sdma_event_e20_hw_started:
  785. break;
  786. case qib_sdma_event_e30_go_running:
  787. ss->go_s99_running = 1;
  788. break;
  789. case qib_sdma_event_e40_sw_cleaned:
  790. sdma_set_state(ppd,
  791. qib_sdma_state_s10_hw_start_up_wait);
  792. sdma_hw_start_up(ppd);
  793. break;
  794. case qib_sdma_event_e50_hw_cleaned:
  795. break;
  796. case qib_sdma_event_e60_hw_halted:
  797. break;
  798. case qib_sdma_event_e70_go_idle:
  799. ss->go_s99_running = 0;
  800. break;
  801. case qib_sdma_event_e7220_err_halted:
  802. break;
  803. case qib_sdma_event_e7322_err_halted:
  804. break;
  805. case qib_sdma_event_e90_timer_tick:
  806. break;
  807. }
  808. break;
  809. case qib_sdma_state_s40_hw_clean_up_wait:
  810. switch (event) {
  811. case qib_sdma_event_e00_go_hw_down:
  812. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  813. sdma_start_sw_clean_up(ppd);
  814. break;
  815. case qib_sdma_event_e10_go_hw_start:
  816. break;
  817. case qib_sdma_event_e20_hw_started:
  818. break;
  819. case qib_sdma_event_e30_go_running:
  820. ss->go_s99_running = 1;
  821. break;
  822. case qib_sdma_event_e40_sw_cleaned:
  823. break;
  824. case qib_sdma_event_e50_hw_cleaned:
  825. sdma_set_state(ppd,
  826. qib_sdma_state_s30_sw_clean_up_wait);
  827. sdma_start_sw_clean_up(ppd);
  828. break;
  829. case qib_sdma_event_e60_hw_halted:
  830. break;
  831. case qib_sdma_event_e70_go_idle:
  832. ss->go_s99_running = 0;
  833. break;
  834. case qib_sdma_event_e7220_err_halted:
  835. break;
  836. case qib_sdma_event_e7322_err_halted:
  837. break;
  838. case qib_sdma_event_e90_timer_tick:
  839. break;
  840. }
  841. break;
  842. case qib_sdma_state_s50_hw_halt_wait:
  843. switch (event) {
  844. case qib_sdma_event_e00_go_hw_down:
  845. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  846. sdma_start_sw_clean_up(ppd);
  847. break;
  848. case qib_sdma_event_e10_go_hw_start:
  849. break;
  850. case qib_sdma_event_e20_hw_started:
  851. break;
  852. case qib_sdma_event_e30_go_running:
  853. ss->go_s99_running = 1;
  854. break;
  855. case qib_sdma_event_e40_sw_cleaned:
  856. break;
  857. case qib_sdma_event_e50_hw_cleaned:
  858. break;
  859. case qib_sdma_event_e60_hw_halted:
  860. sdma_set_state(ppd,
  861. qib_sdma_state_s40_hw_clean_up_wait);
  862. ppd->dd->f_sdma_hw_clean_up(ppd);
  863. break;
  864. case qib_sdma_event_e70_go_idle:
  865. ss->go_s99_running = 0;
  866. break;
  867. case qib_sdma_event_e7220_err_halted:
  868. break;
  869. case qib_sdma_event_e7322_err_halted:
  870. break;
  871. case qib_sdma_event_e90_timer_tick:
  872. break;
  873. }
  874. break;
  875. case qib_sdma_state_s99_running:
  876. switch (event) {
  877. case qib_sdma_event_e00_go_hw_down:
  878. sdma_set_state(ppd, qib_sdma_state_s00_hw_down);
  879. sdma_start_sw_clean_up(ppd);
  880. break;
  881. case qib_sdma_event_e10_go_hw_start:
  882. break;
  883. case qib_sdma_event_e20_hw_started:
  884. break;
  885. case qib_sdma_event_e30_go_running:
  886. break;
  887. case qib_sdma_event_e40_sw_cleaned:
  888. break;
  889. case qib_sdma_event_e50_hw_cleaned:
  890. break;
  891. case qib_sdma_event_e60_hw_halted:
  892. sdma_set_state(ppd,
  893. qib_sdma_state_s30_sw_clean_up_wait);
  894. sdma_start_sw_clean_up(ppd);
  895. break;
  896. case qib_sdma_event_e70_go_idle:
  897. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  898. ss->go_s99_running = 0;
  899. break;
  900. case qib_sdma_event_e7220_err_halted:
  901. sdma_set_state(ppd,
  902. qib_sdma_state_s30_sw_clean_up_wait);
  903. sdma_start_sw_clean_up(ppd);
  904. break;
  905. case qib_sdma_event_e7322_err_halted:
  906. sdma_set_state(ppd, qib_sdma_state_s50_hw_halt_wait);
  907. break;
  908. case qib_sdma_event_e90_timer_tick:
  909. break;
  910. }
  911. break;
  912. }
  913. ss->last_event = event;
  914. }