ce.c 35 KB

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