ce.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "hif.h"
  18. #include "pci.h"
  19. #include "ce.h"
  20. #include "debug.h"
  21. /*
  22. * Support for Copy Engine hardware, which is mainly used for
  23. * communication between Host and Target over a PCIe interconnect.
  24. */
  25. /*
  26. * A single CopyEngine (CE) comprises two "rings":
  27. * a source ring
  28. * a destination ring
  29. *
  30. * Each ring consists of a number of descriptors which specify
  31. * an address, length, and meta-data.
  32. *
  33. * Typically, one side of the PCIe interconnect (Host or Target)
  34. * controls one ring and the other side controls the other ring.
  35. * The source side chooses when to initiate a transfer and it
  36. * chooses what to send (buffer address, length). The destination
  37. * side keeps a supply of "anonymous receive buffers" available and
  38. * it handles incoming data as it arrives (when the destination
  39. * recieves an interrupt).
  40. *
  41. * The sender may send a simple buffer (address/length) or it may
  42. * send a small list of buffers. When a small list is sent, hardware
  43. * "gathers" these and they end up in a single destination buffer
  44. * with a single interrupt.
  45. *
  46. * There are several "contexts" managed by this layer -- more, it
  47. * may seem -- than should be needed. These are provided mainly for
  48. * maximum flexibility and especially to facilitate a simpler HIF
  49. * implementation. There are per-CopyEngine recv, send, and watermark
  50. * contexts. These are supplied by the caller when a recv, send,
  51. * or watermark handler is established and they are echoed back to
  52. * the caller when the respective callbacks are invoked. There is
  53. * also a per-transfer context supplied by the caller when a buffer
  54. * (or sendlist) is sent and when a buffer is enqueued for recv.
  55. * These per-transfer contexts are echoed back to the caller when
  56. * the buffer is sent/received.
  57. */
  58. static inline void ath10k_ce_dest_ring_write_index_set(struct ath10k *ar,
  59. u32 ce_ctrl_addr,
  60. unsigned int n)
  61. {
  62. ath10k_pci_write32(ar, ce_ctrl_addr + DST_WR_INDEX_ADDRESS, n);
  63. }
  64. static inline u32 ath10k_ce_dest_ring_write_index_get(struct ath10k *ar,
  65. u32 ce_ctrl_addr)
  66. {
  67. return ath10k_pci_read32(ar, ce_ctrl_addr + DST_WR_INDEX_ADDRESS);
  68. }
  69. static inline void ath10k_ce_src_ring_write_index_set(struct ath10k *ar,
  70. u32 ce_ctrl_addr,
  71. unsigned int n)
  72. {
  73. ath10k_pci_write32(ar, ce_ctrl_addr + SR_WR_INDEX_ADDRESS, n);
  74. }
  75. static inline u32 ath10k_ce_src_ring_write_index_get(struct ath10k *ar,
  76. u32 ce_ctrl_addr)
  77. {
  78. return ath10k_pci_read32(ar, ce_ctrl_addr + SR_WR_INDEX_ADDRESS);
  79. }
  80. static inline u32 ath10k_ce_src_ring_read_index_get(struct ath10k *ar,
  81. u32 ce_ctrl_addr)
  82. {
  83. return ath10k_pci_read32(ar, ce_ctrl_addr + CURRENT_SRRI_ADDRESS);
  84. }
  85. static inline void ath10k_ce_src_ring_base_addr_set(struct ath10k *ar,
  86. u32 ce_ctrl_addr,
  87. unsigned int addr)
  88. {
  89. ath10k_pci_write32(ar, ce_ctrl_addr + SR_BA_ADDRESS, addr);
  90. }
  91. static inline void ath10k_ce_src_ring_size_set(struct ath10k *ar,
  92. u32 ce_ctrl_addr,
  93. unsigned int n)
  94. {
  95. ath10k_pci_write32(ar, ce_ctrl_addr + SR_SIZE_ADDRESS, n);
  96. }
  97. static inline void ath10k_ce_src_ring_dmax_set(struct ath10k *ar,
  98. u32 ce_ctrl_addr,
  99. unsigned int n)
  100. {
  101. u32 ctrl1_addr = ath10k_pci_read32((ar),
  102. (ce_ctrl_addr) + CE_CTRL1_ADDRESS);
  103. ath10k_pci_write32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS,
  104. (ctrl1_addr & ~CE_CTRL1_DMAX_LENGTH_MASK) |
  105. CE_CTRL1_DMAX_LENGTH_SET(n));
  106. }
  107. static inline void ath10k_ce_src_ring_byte_swap_set(struct ath10k *ar,
  108. u32 ce_ctrl_addr,
  109. unsigned int n)
  110. {
  111. u32 ctrl1_addr = ath10k_pci_read32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS);
  112. ath10k_pci_write32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS,
  113. (ctrl1_addr & ~CE_CTRL1_SRC_RING_BYTE_SWAP_EN_MASK) |
  114. CE_CTRL1_SRC_RING_BYTE_SWAP_EN_SET(n));
  115. }
  116. static inline void ath10k_ce_dest_ring_byte_swap_set(struct ath10k *ar,
  117. u32 ce_ctrl_addr,
  118. unsigned int n)
  119. {
  120. u32 ctrl1_addr = ath10k_pci_read32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS);
  121. ath10k_pci_write32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS,
  122. (ctrl1_addr & ~CE_CTRL1_DST_RING_BYTE_SWAP_EN_MASK) |
  123. CE_CTRL1_DST_RING_BYTE_SWAP_EN_SET(n));
  124. }
  125. static inline u32 ath10k_ce_dest_ring_read_index_get(struct ath10k *ar,
  126. u32 ce_ctrl_addr)
  127. {
  128. return ath10k_pci_read32(ar, ce_ctrl_addr + CURRENT_DRRI_ADDRESS);
  129. }
  130. static inline void ath10k_ce_dest_ring_base_addr_set(struct ath10k *ar,
  131. u32 ce_ctrl_addr,
  132. u32 addr)
  133. {
  134. ath10k_pci_write32(ar, ce_ctrl_addr + DR_BA_ADDRESS, addr);
  135. }
  136. static inline void ath10k_ce_dest_ring_size_set(struct ath10k *ar,
  137. u32 ce_ctrl_addr,
  138. unsigned int n)
  139. {
  140. ath10k_pci_write32(ar, ce_ctrl_addr + DR_SIZE_ADDRESS, n);
  141. }
  142. static inline void ath10k_ce_src_ring_highmark_set(struct ath10k *ar,
  143. u32 ce_ctrl_addr,
  144. unsigned int n)
  145. {
  146. u32 addr = ath10k_pci_read32(ar, ce_ctrl_addr + SRC_WATERMARK_ADDRESS);
  147. ath10k_pci_write32(ar, ce_ctrl_addr + SRC_WATERMARK_ADDRESS,
  148. (addr & ~SRC_WATERMARK_HIGH_MASK) |
  149. SRC_WATERMARK_HIGH_SET(n));
  150. }
  151. static inline void ath10k_ce_src_ring_lowmark_set(struct ath10k *ar,
  152. u32 ce_ctrl_addr,
  153. unsigned int n)
  154. {
  155. u32 addr = ath10k_pci_read32(ar, ce_ctrl_addr + SRC_WATERMARK_ADDRESS);
  156. ath10k_pci_write32(ar, ce_ctrl_addr + SRC_WATERMARK_ADDRESS,
  157. (addr & ~SRC_WATERMARK_LOW_MASK) |
  158. SRC_WATERMARK_LOW_SET(n));
  159. }
  160. static inline void ath10k_ce_dest_ring_highmark_set(struct ath10k *ar,
  161. u32 ce_ctrl_addr,
  162. unsigned int n)
  163. {
  164. u32 addr = ath10k_pci_read32(ar, ce_ctrl_addr + DST_WATERMARK_ADDRESS);
  165. ath10k_pci_write32(ar, ce_ctrl_addr + DST_WATERMARK_ADDRESS,
  166. (addr & ~DST_WATERMARK_HIGH_MASK) |
  167. DST_WATERMARK_HIGH_SET(n));
  168. }
  169. static inline void ath10k_ce_dest_ring_lowmark_set(struct ath10k *ar,
  170. u32 ce_ctrl_addr,
  171. unsigned int n)
  172. {
  173. u32 addr = ath10k_pci_read32(ar, ce_ctrl_addr + DST_WATERMARK_ADDRESS);
  174. ath10k_pci_write32(ar, ce_ctrl_addr + DST_WATERMARK_ADDRESS,
  175. (addr & ~DST_WATERMARK_LOW_MASK) |
  176. DST_WATERMARK_LOW_SET(n));
  177. }
  178. static inline void ath10k_ce_copy_complete_inter_enable(struct ath10k *ar,
  179. u32 ce_ctrl_addr)
  180. {
  181. u32 host_ie_addr = ath10k_pci_read32(ar,
  182. ce_ctrl_addr + HOST_IE_ADDRESS);
  183. ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IE_ADDRESS,
  184. host_ie_addr | HOST_IE_COPY_COMPLETE_MASK);
  185. }
  186. static inline void ath10k_ce_copy_complete_intr_disable(struct ath10k *ar,
  187. u32 ce_ctrl_addr)
  188. {
  189. u32 host_ie_addr = ath10k_pci_read32(ar,
  190. ce_ctrl_addr + HOST_IE_ADDRESS);
  191. ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IE_ADDRESS,
  192. host_ie_addr & ~HOST_IE_COPY_COMPLETE_MASK);
  193. }
  194. static inline void ath10k_ce_watermark_intr_disable(struct ath10k *ar,
  195. u32 ce_ctrl_addr)
  196. {
  197. u32 host_ie_addr = ath10k_pci_read32(ar,
  198. ce_ctrl_addr + HOST_IE_ADDRESS);
  199. ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IE_ADDRESS,
  200. host_ie_addr & ~CE_WATERMARK_MASK);
  201. }
  202. static inline void ath10k_ce_error_intr_enable(struct ath10k *ar,
  203. u32 ce_ctrl_addr)
  204. {
  205. u32 misc_ie_addr = ath10k_pci_read32(ar,
  206. ce_ctrl_addr + MISC_IE_ADDRESS);
  207. ath10k_pci_write32(ar, ce_ctrl_addr + MISC_IE_ADDRESS,
  208. misc_ie_addr | CE_ERROR_MASK);
  209. }
  210. static inline void ath10k_ce_error_intr_disable(struct ath10k *ar,
  211. u32 ce_ctrl_addr)
  212. {
  213. u32 misc_ie_addr = ath10k_pci_read32(ar,
  214. ce_ctrl_addr + MISC_IE_ADDRESS);
  215. ath10k_pci_write32(ar, ce_ctrl_addr + MISC_IE_ADDRESS,
  216. misc_ie_addr & ~CE_ERROR_MASK);
  217. }
  218. static inline void ath10k_ce_engine_int_status_clear(struct ath10k *ar,
  219. u32 ce_ctrl_addr,
  220. unsigned int mask)
  221. {
  222. ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IS_ADDRESS, mask);
  223. }
  224. /*
  225. * Guts of ath10k_ce_send, used by both ath10k_ce_send and
  226. * ath10k_ce_sendlist_send.
  227. * The caller takes responsibility for any needed locking.
  228. */
  229. int ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
  230. void *per_transfer_context,
  231. u32 buffer,
  232. unsigned int nbytes,
  233. unsigned int transfer_id,
  234. unsigned int flags)
  235. {
  236. struct ath10k *ar = ce_state->ar;
  237. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  238. struct ce_desc *desc, *sdesc;
  239. unsigned int nentries_mask = src_ring->nentries_mask;
  240. unsigned int sw_index = src_ring->sw_index;
  241. unsigned int write_index = src_ring->write_index;
  242. u32 ctrl_addr = ce_state->ctrl_addr;
  243. u32 desc_flags = 0;
  244. int ret = 0;
  245. if (nbytes > ce_state->src_sz_max)
  246. ath10k_warn("%s: send more we can (nbytes: %d, max: %d)\n",
  247. __func__, nbytes, ce_state->src_sz_max);
  248. ret = ath10k_pci_wake(ar);
  249. if (ret)
  250. return ret;
  251. if (unlikely(CE_RING_DELTA(nentries_mask,
  252. write_index, sw_index - 1) <= 0)) {
  253. ret = -ENOSR;
  254. goto exit;
  255. }
  256. desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
  257. write_index);
  258. sdesc = CE_SRC_RING_TO_DESC(src_ring->shadow_base, write_index);
  259. desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
  260. if (flags & CE_SEND_FLAG_GATHER)
  261. desc_flags |= CE_DESC_FLAGS_GATHER;
  262. if (flags & CE_SEND_FLAG_BYTE_SWAP)
  263. desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
  264. sdesc->addr = __cpu_to_le32(buffer);
  265. sdesc->nbytes = __cpu_to_le16(nbytes);
  266. sdesc->flags = __cpu_to_le16(desc_flags);
  267. *desc = *sdesc;
  268. src_ring->per_transfer_context[write_index] = per_transfer_context;
  269. /* Update Source Ring Write Index */
  270. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  271. /* WORKAROUND */
  272. if (!(flags & CE_SEND_FLAG_GATHER))
  273. ath10k_ce_src_ring_write_index_set(ar, ctrl_addr, write_index);
  274. src_ring->write_index = write_index;
  275. exit:
  276. ath10k_pci_sleep(ar);
  277. return ret;
  278. }
  279. void __ath10k_ce_send_revert(struct ath10k_ce_pipe *pipe)
  280. {
  281. struct ath10k *ar = pipe->ar;
  282. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  283. struct ath10k_ce_ring *src_ring = pipe->src_ring;
  284. u32 ctrl_addr = pipe->ctrl_addr;
  285. lockdep_assert_held(&ar_pci->ce_lock);
  286. /*
  287. * This function must be called only if there is an incomplete
  288. * scatter-gather transfer (before index register is updated)
  289. * that needs to be cleaned up.
  290. */
  291. if (WARN_ON_ONCE(src_ring->write_index == src_ring->sw_index))
  292. return;
  293. if (WARN_ON_ONCE(src_ring->write_index ==
  294. ath10k_ce_src_ring_write_index_get(ar, ctrl_addr)))
  295. return;
  296. src_ring->write_index--;
  297. src_ring->write_index &= src_ring->nentries_mask;
  298. src_ring->per_transfer_context[src_ring->write_index] = NULL;
  299. }
  300. int ath10k_ce_send(struct ath10k_ce_pipe *ce_state,
  301. void *per_transfer_context,
  302. u32 buffer,
  303. unsigned int nbytes,
  304. unsigned int transfer_id,
  305. unsigned int flags)
  306. {
  307. struct ath10k *ar = ce_state->ar;
  308. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  309. int ret;
  310. spin_lock_bh(&ar_pci->ce_lock);
  311. ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
  312. buffer, nbytes, transfer_id, flags);
  313. spin_unlock_bh(&ar_pci->ce_lock);
  314. return ret;
  315. }
  316. int ath10k_ce_num_free_src_entries(struct ath10k_ce_pipe *pipe)
  317. {
  318. struct ath10k *ar = pipe->ar;
  319. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  320. int delta;
  321. spin_lock_bh(&ar_pci->ce_lock);
  322. delta = CE_RING_DELTA(pipe->src_ring->nentries_mask,
  323. pipe->src_ring->write_index,
  324. pipe->src_ring->sw_index - 1);
  325. spin_unlock_bh(&ar_pci->ce_lock);
  326. return delta;
  327. }
  328. int ath10k_ce_recv_buf_enqueue(struct ath10k_ce_pipe *ce_state,
  329. void *per_recv_context,
  330. u32 buffer)
  331. {
  332. struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
  333. u32 ctrl_addr = ce_state->ctrl_addr;
  334. struct ath10k *ar = ce_state->ar;
  335. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  336. unsigned int nentries_mask = dest_ring->nentries_mask;
  337. unsigned int write_index;
  338. unsigned int sw_index;
  339. int ret;
  340. spin_lock_bh(&ar_pci->ce_lock);
  341. write_index = dest_ring->write_index;
  342. sw_index = dest_ring->sw_index;
  343. ret = ath10k_pci_wake(ar);
  344. if (ret)
  345. goto out;
  346. if (CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) > 0) {
  347. struct ce_desc *base = dest_ring->base_addr_owner_space;
  348. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, write_index);
  349. /* Update destination descriptor */
  350. desc->addr = __cpu_to_le32(buffer);
  351. desc->nbytes = 0;
  352. dest_ring->per_transfer_context[write_index] =
  353. per_recv_context;
  354. /* Update Destination Ring Write Index */
  355. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  356. ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
  357. dest_ring->write_index = write_index;
  358. ret = 0;
  359. } else {
  360. ret = -EIO;
  361. }
  362. ath10k_pci_sleep(ar);
  363. out:
  364. spin_unlock_bh(&ar_pci->ce_lock);
  365. return ret;
  366. }
  367. /*
  368. * Guts of ath10k_ce_completed_recv_next.
  369. * The caller takes responsibility for any necessary locking.
  370. */
  371. static int ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
  372. void **per_transfer_contextp,
  373. u32 *bufferp,
  374. unsigned int *nbytesp,
  375. unsigned int *transfer_idp,
  376. unsigned int *flagsp)
  377. {
  378. struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
  379. unsigned int nentries_mask = dest_ring->nentries_mask;
  380. unsigned int sw_index = dest_ring->sw_index;
  381. struct ce_desc *base = dest_ring->base_addr_owner_space;
  382. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
  383. struct ce_desc sdesc;
  384. u16 nbytes;
  385. /* Copy in one go for performance reasons */
  386. sdesc = *desc;
  387. nbytes = __le16_to_cpu(sdesc.nbytes);
  388. if (nbytes == 0) {
  389. /*
  390. * This closes a relatively unusual race where the Host
  391. * sees the updated DRRI before the update to the
  392. * corresponding descriptor has completed. We treat this
  393. * as a descriptor that is not yet done.
  394. */
  395. return -EIO;
  396. }
  397. desc->nbytes = 0;
  398. /* Return data from completed destination descriptor */
  399. *bufferp = __le32_to_cpu(sdesc.addr);
  400. *nbytesp = nbytes;
  401. *transfer_idp = MS(__le16_to_cpu(sdesc.flags), CE_DESC_FLAGS_META_DATA);
  402. if (__le16_to_cpu(sdesc.flags) & CE_DESC_FLAGS_BYTE_SWAP)
  403. *flagsp = CE_RECV_FLAG_SWAPPED;
  404. else
  405. *flagsp = 0;
  406. if (per_transfer_contextp)
  407. *per_transfer_contextp =
  408. dest_ring->per_transfer_context[sw_index];
  409. /* sanity */
  410. dest_ring->per_transfer_context[sw_index] = NULL;
  411. /* Update sw_index */
  412. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  413. dest_ring->sw_index = sw_index;
  414. return 0;
  415. }
  416. int ath10k_ce_completed_recv_next(struct ath10k_ce_pipe *ce_state,
  417. void **per_transfer_contextp,
  418. u32 *bufferp,
  419. unsigned int *nbytesp,
  420. unsigned int *transfer_idp,
  421. unsigned int *flagsp)
  422. {
  423. struct ath10k *ar = ce_state->ar;
  424. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  425. int ret;
  426. spin_lock_bh(&ar_pci->ce_lock);
  427. ret = ath10k_ce_completed_recv_next_nolock(ce_state,
  428. per_transfer_contextp,
  429. bufferp, nbytesp,
  430. transfer_idp, flagsp);
  431. spin_unlock_bh(&ar_pci->ce_lock);
  432. return ret;
  433. }
  434. int ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
  435. void **per_transfer_contextp,
  436. u32 *bufferp)
  437. {
  438. struct ath10k_ce_ring *dest_ring;
  439. unsigned int nentries_mask;
  440. unsigned int sw_index;
  441. unsigned int write_index;
  442. int ret;
  443. struct ath10k *ar;
  444. struct ath10k_pci *ar_pci;
  445. dest_ring = ce_state->dest_ring;
  446. if (!dest_ring)
  447. return -EIO;
  448. ar = ce_state->ar;
  449. ar_pci = ath10k_pci_priv(ar);
  450. spin_lock_bh(&ar_pci->ce_lock);
  451. nentries_mask = dest_ring->nentries_mask;
  452. sw_index = dest_ring->sw_index;
  453. write_index = dest_ring->write_index;
  454. if (write_index != sw_index) {
  455. struct ce_desc *base = dest_ring->base_addr_owner_space;
  456. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
  457. /* Return data from completed destination descriptor */
  458. *bufferp = __le32_to_cpu(desc->addr);
  459. if (per_transfer_contextp)
  460. *per_transfer_contextp =
  461. dest_ring->per_transfer_context[sw_index];
  462. /* sanity */
  463. dest_ring->per_transfer_context[sw_index] = NULL;
  464. /* Update sw_index */
  465. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  466. dest_ring->sw_index = sw_index;
  467. ret = 0;
  468. } else {
  469. ret = -EIO;
  470. }
  471. spin_unlock_bh(&ar_pci->ce_lock);
  472. return ret;
  473. }
  474. /*
  475. * Guts of ath10k_ce_completed_send_next.
  476. * The caller takes responsibility for any necessary locking.
  477. */
  478. static int ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
  479. void **per_transfer_contextp,
  480. u32 *bufferp,
  481. unsigned int *nbytesp,
  482. unsigned int *transfer_idp)
  483. {
  484. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  485. u32 ctrl_addr = ce_state->ctrl_addr;
  486. struct ath10k *ar = ce_state->ar;
  487. unsigned int nentries_mask = src_ring->nentries_mask;
  488. unsigned int sw_index = src_ring->sw_index;
  489. struct ce_desc *sdesc, *sbase;
  490. unsigned int read_index;
  491. int ret;
  492. if (src_ring->hw_index == sw_index) {
  493. /*
  494. * The SW completion index has caught up with the cached
  495. * version of the HW completion index.
  496. * Update the cached HW completion index to see whether
  497. * the SW has really caught up to the HW, or if the cached
  498. * value of the HW index has become stale.
  499. */
  500. ret = ath10k_pci_wake(ar);
  501. if (ret)
  502. return ret;
  503. src_ring->hw_index =
  504. ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  505. src_ring->hw_index &= nentries_mask;
  506. ath10k_pci_sleep(ar);
  507. }
  508. read_index = src_ring->hw_index;
  509. if ((read_index == sw_index) || (read_index == 0xffffffff))
  510. return -EIO;
  511. sbase = src_ring->shadow_base;
  512. sdesc = CE_SRC_RING_TO_DESC(sbase, sw_index);
  513. /* Return data from completed source descriptor */
  514. *bufferp = __le32_to_cpu(sdesc->addr);
  515. *nbytesp = __le16_to_cpu(sdesc->nbytes);
  516. *transfer_idp = MS(__le16_to_cpu(sdesc->flags),
  517. CE_DESC_FLAGS_META_DATA);
  518. if (per_transfer_contextp)
  519. *per_transfer_contextp =
  520. src_ring->per_transfer_context[sw_index];
  521. /* sanity */
  522. src_ring->per_transfer_context[sw_index] = NULL;
  523. /* Update sw_index */
  524. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  525. src_ring->sw_index = sw_index;
  526. return 0;
  527. }
  528. /* NB: Modeled after ath10k_ce_completed_send_next */
  529. int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
  530. void **per_transfer_contextp,
  531. u32 *bufferp,
  532. unsigned int *nbytesp,
  533. unsigned int *transfer_idp)
  534. {
  535. struct ath10k_ce_ring *src_ring;
  536. unsigned int nentries_mask;
  537. unsigned int sw_index;
  538. unsigned int write_index;
  539. int ret;
  540. struct ath10k *ar;
  541. struct ath10k_pci *ar_pci;
  542. src_ring = ce_state->src_ring;
  543. if (!src_ring)
  544. return -EIO;
  545. ar = ce_state->ar;
  546. ar_pci = ath10k_pci_priv(ar);
  547. spin_lock_bh(&ar_pci->ce_lock);
  548. nentries_mask = src_ring->nentries_mask;
  549. sw_index = src_ring->sw_index;
  550. write_index = src_ring->write_index;
  551. if (write_index != sw_index) {
  552. struct ce_desc *base = src_ring->base_addr_owner_space;
  553. struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);
  554. /* Return data from completed source descriptor */
  555. *bufferp = __le32_to_cpu(desc->addr);
  556. *nbytesp = __le16_to_cpu(desc->nbytes);
  557. *transfer_idp = MS(__le16_to_cpu(desc->flags),
  558. CE_DESC_FLAGS_META_DATA);
  559. if (per_transfer_contextp)
  560. *per_transfer_contextp =
  561. src_ring->per_transfer_context[sw_index];
  562. /* sanity */
  563. src_ring->per_transfer_context[sw_index] = NULL;
  564. /* Update sw_index */
  565. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  566. src_ring->sw_index = sw_index;
  567. ret = 0;
  568. } else {
  569. ret = -EIO;
  570. }
  571. spin_unlock_bh(&ar_pci->ce_lock);
  572. return ret;
  573. }
  574. int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
  575. void **per_transfer_contextp,
  576. u32 *bufferp,
  577. unsigned int *nbytesp,
  578. unsigned int *transfer_idp)
  579. {
  580. struct ath10k *ar = ce_state->ar;
  581. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  582. int ret;
  583. spin_lock_bh(&ar_pci->ce_lock);
  584. ret = ath10k_ce_completed_send_next_nolock(ce_state,
  585. per_transfer_contextp,
  586. bufferp, nbytesp,
  587. transfer_idp);
  588. spin_unlock_bh(&ar_pci->ce_lock);
  589. return ret;
  590. }
  591. /*
  592. * Guts of interrupt handler for per-engine interrupts on a particular CE.
  593. *
  594. * Invokes registered callbacks for recv_complete,
  595. * send_complete, and watermarks.
  596. */
  597. void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
  598. {
  599. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  600. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  601. u32 ctrl_addr = ce_state->ctrl_addr;
  602. int ret;
  603. ret = ath10k_pci_wake(ar);
  604. if (ret)
  605. return;
  606. spin_lock_bh(&ar_pci->ce_lock);
  607. /* Clear the copy-complete interrupts that will be handled here. */
  608. ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
  609. HOST_IS_COPY_COMPLETE_MASK);
  610. spin_unlock_bh(&ar_pci->ce_lock);
  611. if (ce_state->recv_cb)
  612. ce_state->recv_cb(ce_state);
  613. if (ce_state->send_cb)
  614. ce_state->send_cb(ce_state);
  615. spin_lock_bh(&ar_pci->ce_lock);
  616. /*
  617. * Misc CE interrupts are not being handled, but still need
  618. * to be cleared.
  619. */
  620. ath10k_ce_engine_int_status_clear(ar, ctrl_addr, CE_WATERMARK_MASK);
  621. spin_unlock_bh(&ar_pci->ce_lock);
  622. ath10k_pci_sleep(ar);
  623. }
  624. /*
  625. * Handler for per-engine interrupts on ALL active CEs.
  626. * This is used in cases where the system is sharing a
  627. * single interrput for all CEs
  628. */
  629. void ath10k_ce_per_engine_service_any(struct ath10k *ar)
  630. {
  631. int ce_id, ret;
  632. u32 intr_summary;
  633. ret = ath10k_pci_wake(ar);
  634. if (ret)
  635. return;
  636. intr_summary = CE_INTERRUPT_SUMMARY(ar);
  637. for (ce_id = 0; intr_summary && (ce_id < CE_COUNT); ce_id++) {
  638. if (intr_summary & (1 << ce_id))
  639. intr_summary &= ~(1 << ce_id);
  640. else
  641. /* no intr pending on this CE */
  642. continue;
  643. ath10k_ce_per_engine_service(ar, ce_id);
  644. }
  645. ath10k_pci_sleep(ar);
  646. }
  647. /*
  648. * Adjust interrupts for the copy complete handler.
  649. * If it's needed for either send or recv, then unmask
  650. * this interrupt; otherwise, mask it.
  651. *
  652. * Called with ce_lock held.
  653. */
  654. static void ath10k_ce_per_engine_handler_adjust(struct ath10k_ce_pipe *ce_state,
  655. int disable_copy_compl_intr)
  656. {
  657. u32 ctrl_addr = ce_state->ctrl_addr;
  658. struct ath10k *ar = ce_state->ar;
  659. int ret;
  660. ret = ath10k_pci_wake(ar);
  661. if (ret)
  662. return;
  663. if ((!disable_copy_compl_intr) &&
  664. (ce_state->send_cb || ce_state->recv_cb))
  665. ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
  666. else
  667. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  668. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  669. ath10k_pci_sleep(ar);
  670. }
  671. int ath10k_ce_disable_interrupts(struct ath10k *ar)
  672. {
  673. int ce_id, ret;
  674. ret = ath10k_pci_wake(ar);
  675. if (ret)
  676. return ret;
  677. for (ce_id = 0; ce_id < CE_COUNT; ce_id++) {
  678. u32 ctrl_addr = ath10k_ce_base_address(ce_id);
  679. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  680. ath10k_ce_error_intr_disable(ar, ctrl_addr);
  681. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  682. }
  683. ath10k_pci_sleep(ar);
  684. return 0;
  685. }
  686. void ath10k_ce_send_cb_register(struct ath10k_ce_pipe *ce_state,
  687. void (*send_cb)(struct ath10k_ce_pipe *),
  688. int disable_interrupts)
  689. {
  690. struct ath10k *ar = ce_state->ar;
  691. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  692. spin_lock_bh(&ar_pci->ce_lock);
  693. ce_state->send_cb = send_cb;
  694. ath10k_ce_per_engine_handler_adjust(ce_state, disable_interrupts);
  695. spin_unlock_bh(&ar_pci->ce_lock);
  696. }
  697. void ath10k_ce_recv_cb_register(struct ath10k_ce_pipe *ce_state,
  698. void (*recv_cb)(struct ath10k_ce_pipe *))
  699. {
  700. struct ath10k *ar = ce_state->ar;
  701. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  702. spin_lock_bh(&ar_pci->ce_lock);
  703. ce_state->recv_cb = recv_cb;
  704. ath10k_ce_per_engine_handler_adjust(ce_state, 0);
  705. spin_unlock_bh(&ar_pci->ce_lock);
  706. }
  707. static int ath10k_ce_init_src_ring(struct ath10k *ar,
  708. unsigned int ce_id,
  709. const struct ce_attr *attr)
  710. {
  711. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  712. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  713. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  714. u32 nentries, ctrl_addr = ath10k_ce_base_address(ce_id);
  715. nentries = roundup_pow_of_two(attr->src_nentries);
  716. memset(src_ring->per_transfer_context, 0,
  717. nentries * sizeof(*src_ring->per_transfer_context));
  718. src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  719. src_ring->sw_index &= src_ring->nentries_mask;
  720. src_ring->hw_index = src_ring->sw_index;
  721. src_ring->write_index =
  722. ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
  723. src_ring->write_index &= src_ring->nentries_mask;
  724. ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr,
  725. src_ring->base_addr_ce_space);
  726. ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
  727. ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
  728. ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
  729. ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
  730. ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);
  731. ath10k_dbg(ATH10K_DBG_BOOT,
  732. "boot init ce src ring id %d entries %d base_addr %p\n",
  733. ce_id, nentries, src_ring->base_addr_owner_space);
  734. return 0;
  735. }
  736. static int ath10k_ce_init_dest_ring(struct ath10k *ar,
  737. unsigned int ce_id,
  738. const struct ce_attr *attr)
  739. {
  740. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  741. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  742. struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
  743. u32 nentries, ctrl_addr = ath10k_ce_base_address(ce_id);
  744. nentries = roundup_pow_of_two(attr->dest_nentries);
  745. memset(dest_ring->per_transfer_context, 0,
  746. nentries * sizeof(*dest_ring->per_transfer_context));
  747. dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
  748. dest_ring->sw_index &= dest_ring->nentries_mask;
  749. dest_ring->write_index =
  750. ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
  751. dest_ring->write_index &= dest_ring->nentries_mask;
  752. ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr,
  753. dest_ring->base_addr_ce_space);
  754. ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
  755. ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
  756. ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
  757. ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);
  758. ath10k_dbg(ATH10K_DBG_BOOT,
  759. "boot ce dest ring id %d entries %d base_addr %p\n",
  760. ce_id, nentries, dest_ring->base_addr_owner_space);
  761. return 0;
  762. }
  763. static struct ath10k_ce_ring *
  764. ath10k_ce_alloc_src_ring(struct ath10k *ar, unsigned int ce_id,
  765. const struct ce_attr *attr)
  766. {
  767. struct ath10k_ce_ring *src_ring;
  768. u32 nentries = attr->src_nentries;
  769. dma_addr_t base_addr;
  770. nentries = roundup_pow_of_two(nentries);
  771. src_ring = kzalloc(sizeof(*src_ring) +
  772. (nentries *
  773. sizeof(*src_ring->per_transfer_context)),
  774. GFP_KERNEL);
  775. if (src_ring == NULL)
  776. return ERR_PTR(-ENOMEM);
  777. src_ring->nentries = nentries;
  778. src_ring->nentries_mask = nentries - 1;
  779. /*
  780. * Legacy platforms that do not support cache
  781. * coherent DMA are unsupported
  782. */
  783. src_ring->base_addr_owner_space_unaligned =
  784. dma_alloc_coherent(ar->dev,
  785. (nentries * sizeof(struct ce_desc) +
  786. CE_DESC_RING_ALIGN),
  787. &base_addr, GFP_KERNEL);
  788. if (!src_ring->base_addr_owner_space_unaligned) {
  789. kfree(src_ring);
  790. return ERR_PTR(-ENOMEM);
  791. }
  792. src_ring->base_addr_ce_space_unaligned = base_addr;
  793. src_ring->base_addr_owner_space = PTR_ALIGN(
  794. src_ring->base_addr_owner_space_unaligned,
  795. CE_DESC_RING_ALIGN);
  796. src_ring->base_addr_ce_space = ALIGN(
  797. src_ring->base_addr_ce_space_unaligned,
  798. CE_DESC_RING_ALIGN);
  799. /*
  800. * Also allocate a shadow src ring in regular
  801. * mem to use for faster access.
  802. */
  803. src_ring->shadow_base_unaligned =
  804. kmalloc((nentries * sizeof(struct ce_desc) +
  805. CE_DESC_RING_ALIGN), GFP_KERNEL);
  806. if (!src_ring->shadow_base_unaligned) {
  807. dma_free_coherent(ar->dev,
  808. (nentries * sizeof(struct ce_desc) +
  809. CE_DESC_RING_ALIGN),
  810. src_ring->base_addr_owner_space,
  811. src_ring->base_addr_ce_space);
  812. kfree(src_ring);
  813. return ERR_PTR(-ENOMEM);
  814. }
  815. src_ring->shadow_base = PTR_ALIGN(
  816. src_ring->shadow_base_unaligned,
  817. CE_DESC_RING_ALIGN);
  818. return src_ring;
  819. }
  820. static struct ath10k_ce_ring *
  821. ath10k_ce_alloc_dest_ring(struct ath10k *ar, unsigned int ce_id,
  822. const struct ce_attr *attr)
  823. {
  824. struct ath10k_ce_ring *dest_ring;
  825. u32 nentries;
  826. dma_addr_t base_addr;
  827. nentries = roundup_pow_of_two(attr->dest_nentries);
  828. dest_ring = kzalloc(sizeof(*dest_ring) +
  829. (nentries *
  830. sizeof(*dest_ring->per_transfer_context)),
  831. GFP_KERNEL);
  832. if (dest_ring == NULL)
  833. return ERR_PTR(-ENOMEM);
  834. dest_ring->nentries = nentries;
  835. dest_ring->nentries_mask = nentries - 1;
  836. /*
  837. * Legacy platforms that do not support cache
  838. * coherent DMA are unsupported
  839. */
  840. dest_ring->base_addr_owner_space_unaligned =
  841. dma_alloc_coherent(ar->dev,
  842. (nentries * sizeof(struct ce_desc) +
  843. CE_DESC_RING_ALIGN),
  844. &base_addr, GFP_KERNEL);
  845. if (!dest_ring->base_addr_owner_space_unaligned) {
  846. kfree(dest_ring);
  847. return ERR_PTR(-ENOMEM);
  848. }
  849. dest_ring->base_addr_ce_space_unaligned = base_addr;
  850. /*
  851. * Correctly initialize memory to 0 to prevent garbage
  852. * data crashing system when download firmware
  853. */
  854. memset(dest_ring->base_addr_owner_space_unaligned, 0,
  855. nentries * sizeof(struct ce_desc) + CE_DESC_RING_ALIGN);
  856. dest_ring->base_addr_owner_space = PTR_ALIGN(
  857. dest_ring->base_addr_owner_space_unaligned,
  858. CE_DESC_RING_ALIGN);
  859. dest_ring->base_addr_ce_space = ALIGN(
  860. dest_ring->base_addr_ce_space_unaligned,
  861. CE_DESC_RING_ALIGN);
  862. return dest_ring;
  863. }
  864. /*
  865. * Initialize a Copy Engine based on caller-supplied attributes.
  866. * This may be called once to initialize both source and destination
  867. * rings or it may be called twice for separate source and destination
  868. * initialization. It may be that only one side or the other is
  869. * initialized by software/firmware.
  870. */
  871. int ath10k_ce_init_pipe(struct ath10k *ar, unsigned int ce_id,
  872. const struct ce_attr *attr)
  873. {
  874. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  875. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  876. int ret;
  877. /*
  878. * Make sure there's enough CE ringbuffer entries for HTT TX to avoid
  879. * additional TX locking checks.
  880. *
  881. * For the lack of a better place do the check here.
  882. */
  883. BUILD_BUG_ON(2*TARGET_NUM_MSDU_DESC >
  884. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  885. BUILD_BUG_ON(2*TARGET_10X_NUM_MSDU_DESC >
  886. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  887. ret = ath10k_pci_wake(ar);
  888. if (ret)
  889. return ret;
  890. spin_lock_bh(&ar_pci->ce_lock);
  891. ce_state->ar = ar;
  892. ce_state->id = ce_id;
  893. ce_state->ctrl_addr = ath10k_ce_base_address(ce_id);
  894. ce_state->attr_flags = attr->flags;
  895. ce_state->src_sz_max = attr->src_sz_max;
  896. spin_unlock_bh(&ar_pci->ce_lock);
  897. if (attr->src_nentries) {
  898. ret = ath10k_ce_init_src_ring(ar, ce_id, attr);
  899. if (ret) {
  900. ath10k_err("Failed to initialize CE src ring for ID: %d (%d)\n",
  901. ce_id, ret);
  902. goto out;
  903. }
  904. }
  905. if (attr->dest_nentries) {
  906. ret = ath10k_ce_init_dest_ring(ar, ce_id, attr);
  907. if (ret) {
  908. ath10k_err("Failed to initialize CE dest ring for ID: %d (%d)\n",
  909. ce_id, ret);
  910. goto out;
  911. }
  912. }
  913. out:
  914. ath10k_pci_sleep(ar);
  915. return ret;
  916. }
  917. static void ath10k_ce_deinit_src_ring(struct ath10k *ar, unsigned int ce_id)
  918. {
  919. u32 ctrl_addr = ath10k_ce_base_address(ce_id);
  920. ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr, 0);
  921. ath10k_ce_src_ring_size_set(ar, ctrl_addr, 0);
  922. ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, 0);
  923. ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, 0);
  924. }
  925. static void ath10k_ce_deinit_dest_ring(struct ath10k *ar, unsigned int ce_id)
  926. {
  927. u32 ctrl_addr = ath10k_ce_base_address(ce_id);
  928. ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr, 0);
  929. ath10k_ce_dest_ring_size_set(ar, ctrl_addr, 0);
  930. ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, 0);
  931. }
  932. void ath10k_ce_deinit_pipe(struct ath10k *ar, unsigned int ce_id)
  933. {
  934. int ret;
  935. ret = ath10k_pci_wake(ar);
  936. if (ret)
  937. return;
  938. ath10k_ce_deinit_src_ring(ar, ce_id);
  939. ath10k_ce_deinit_dest_ring(ar, ce_id);
  940. ath10k_pci_sleep(ar);
  941. }
  942. int ath10k_ce_alloc_pipe(struct ath10k *ar, int ce_id,
  943. const struct ce_attr *attr)
  944. {
  945. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  946. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  947. int ret;
  948. if (attr->src_nentries) {
  949. ce_state->src_ring = ath10k_ce_alloc_src_ring(ar, ce_id, attr);
  950. if (IS_ERR(ce_state->src_ring)) {
  951. ret = PTR_ERR(ce_state->src_ring);
  952. ath10k_err("failed to allocate copy engine source ring %d: %d\n",
  953. ce_id, ret);
  954. ce_state->src_ring = NULL;
  955. return ret;
  956. }
  957. }
  958. if (attr->dest_nentries) {
  959. ce_state->dest_ring = ath10k_ce_alloc_dest_ring(ar, ce_id,
  960. attr);
  961. if (IS_ERR(ce_state->dest_ring)) {
  962. ret = PTR_ERR(ce_state->dest_ring);
  963. ath10k_err("failed to allocate copy engine destination ring %d: %d\n",
  964. ce_id, ret);
  965. ce_state->dest_ring = NULL;
  966. return ret;
  967. }
  968. }
  969. return 0;
  970. }
  971. void ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
  972. {
  973. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  974. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  975. if (ce_state->src_ring) {
  976. kfree(ce_state->src_ring->shadow_base_unaligned);
  977. dma_free_coherent(ar->dev,
  978. (ce_state->src_ring->nentries *
  979. sizeof(struct ce_desc) +
  980. CE_DESC_RING_ALIGN),
  981. ce_state->src_ring->base_addr_owner_space,
  982. ce_state->src_ring->base_addr_ce_space);
  983. kfree(ce_state->src_ring);
  984. }
  985. if (ce_state->dest_ring) {
  986. dma_free_coherent(ar->dev,
  987. (ce_state->dest_ring->nentries *
  988. sizeof(struct ce_desc) +
  989. CE_DESC_RING_ALIGN),
  990. ce_state->dest_ring->base_addr_owner_space,
  991. ce_state->dest_ring->base_addr_ce_space);
  992. kfree(ce_state->dest_ring);
  993. }
  994. ce_state->src_ring = NULL;
  995. ce_state->dest_ring = NULL;
  996. }