ce.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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(ar, "%s: send more we can (nbytes: %d, max: %d)\n",
  247. __func__, nbytes, ce_state->src_sz_max);
  248. if (unlikely(CE_RING_DELTA(nentries_mask,
  249. write_index, sw_index - 1) <= 0)) {
  250. ret = -ENOSR;
  251. goto exit;
  252. }
  253. desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
  254. write_index);
  255. sdesc = CE_SRC_RING_TO_DESC(src_ring->shadow_base, write_index);
  256. desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
  257. if (flags & CE_SEND_FLAG_GATHER)
  258. desc_flags |= CE_DESC_FLAGS_GATHER;
  259. if (flags & CE_SEND_FLAG_BYTE_SWAP)
  260. desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
  261. sdesc->addr = __cpu_to_le32(buffer);
  262. sdesc->nbytes = __cpu_to_le16(nbytes);
  263. sdesc->flags = __cpu_to_le16(desc_flags);
  264. *desc = *sdesc;
  265. src_ring->per_transfer_context[write_index] = per_transfer_context;
  266. /* Update Source Ring Write Index */
  267. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  268. /* WORKAROUND */
  269. if (!(flags & CE_SEND_FLAG_GATHER))
  270. ath10k_ce_src_ring_write_index_set(ar, ctrl_addr, write_index);
  271. src_ring->write_index = write_index;
  272. exit:
  273. return ret;
  274. }
  275. void __ath10k_ce_send_revert(struct ath10k_ce_pipe *pipe)
  276. {
  277. struct ath10k *ar = pipe->ar;
  278. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  279. struct ath10k_ce_ring *src_ring = pipe->src_ring;
  280. u32 ctrl_addr = pipe->ctrl_addr;
  281. lockdep_assert_held(&ar_pci->ce_lock);
  282. /*
  283. * This function must be called only if there is an incomplete
  284. * scatter-gather transfer (before index register is updated)
  285. * that needs to be cleaned up.
  286. */
  287. if (WARN_ON_ONCE(src_ring->write_index == src_ring->sw_index))
  288. return;
  289. if (WARN_ON_ONCE(src_ring->write_index ==
  290. ath10k_ce_src_ring_write_index_get(ar, ctrl_addr)))
  291. return;
  292. src_ring->write_index--;
  293. src_ring->write_index &= src_ring->nentries_mask;
  294. src_ring->per_transfer_context[src_ring->write_index] = NULL;
  295. }
  296. int ath10k_ce_send(struct ath10k_ce_pipe *ce_state,
  297. void *per_transfer_context,
  298. u32 buffer,
  299. unsigned int nbytes,
  300. unsigned int transfer_id,
  301. unsigned int flags)
  302. {
  303. struct ath10k *ar = ce_state->ar;
  304. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  305. int ret;
  306. spin_lock_bh(&ar_pci->ce_lock);
  307. ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
  308. buffer, nbytes, transfer_id, flags);
  309. spin_unlock_bh(&ar_pci->ce_lock);
  310. return ret;
  311. }
  312. int ath10k_ce_num_free_src_entries(struct ath10k_ce_pipe *pipe)
  313. {
  314. struct ath10k *ar = pipe->ar;
  315. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  316. int delta;
  317. spin_lock_bh(&ar_pci->ce_lock);
  318. delta = CE_RING_DELTA(pipe->src_ring->nentries_mask,
  319. pipe->src_ring->write_index,
  320. pipe->src_ring->sw_index - 1);
  321. spin_unlock_bh(&ar_pci->ce_lock);
  322. return delta;
  323. }
  324. int __ath10k_ce_rx_num_free_bufs(struct ath10k_ce_pipe *pipe)
  325. {
  326. struct ath10k *ar = pipe->ar;
  327. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  328. struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
  329. unsigned int nentries_mask = dest_ring->nentries_mask;
  330. unsigned int write_index = dest_ring->write_index;
  331. unsigned int sw_index = dest_ring->sw_index;
  332. lockdep_assert_held(&ar_pci->ce_lock);
  333. return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
  334. }
  335. int __ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx, u32 paddr)
  336. {
  337. struct ath10k *ar = pipe->ar;
  338. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  339. struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
  340. unsigned int nentries_mask = dest_ring->nentries_mask;
  341. unsigned int write_index = dest_ring->write_index;
  342. unsigned int sw_index = dest_ring->sw_index;
  343. struct ce_desc *base = dest_ring->base_addr_owner_space;
  344. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, write_index);
  345. u32 ctrl_addr = pipe->ctrl_addr;
  346. lockdep_assert_held(&ar_pci->ce_lock);
  347. if (CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) == 0)
  348. return -EIO;
  349. desc->addr = __cpu_to_le32(paddr);
  350. desc->nbytes = 0;
  351. dest_ring->per_transfer_context[write_index] = ctx;
  352. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  353. ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
  354. dest_ring->write_index = write_index;
  355. return 0;
  356. }
  357. int ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx, u32 paddr)
  358. {
  359. struct ath10k *ar = pipe->ar;
  360. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  361. int ret;
  362. spin_lock_bh(&ar_pci->ce_lock);
  363. ret = __ath10k_ce_rx_post_buf(pipe, ctx, paddr);
  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. if (src_ring->hw_index == sw_index) {
  492. /*
  493. * The SW completion index has caught up with the cached
  494. * version of the HW completion index.
  495. * Update the cached HW completion index to see whether
  496. * the SW has really caught up to the HW, or if the cached
  497. * value of the HW index has become stale.
  498. */
  499. read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  500. if (read_index == 0xffffffff)
  501. return -ENODEV;
  502. read_index &= nentries_mask;
  503. src_ring->hw_index = read_index;
  504. }
  505. read_index = src_ring->hw_index;
  506. if (read_index == sw_index)
  507. return -EIO;
  508. sbase = src_ring->shadow_base;
  509. sdesc = CE_SRC_RING_TO_DESC(sbase, sw_index);
  510. /* Return data from completed source descriptor */
  511. *bufferp = __le32_to_cpu(sdesc->addr);
  512. *nbytesp = __le16_to_cpu(sdesc->nbytes);
  513. *transfer_idp = MS(__le16_to_cpu(sdesc->flags),
  514. CE_DESC_FLAGS_META_DATA);
  515. if (per_transfer_contextp)
  516. *per_transfer_contextp =
  517. src_ring->per_transfer_context[sw_index];
  518. /* sanity */
  519. src_ring->per_transfer_context[sw_index] = NULL;
  520. /* Update sw_index */
  521. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  522. src_ring->sw_index = sw_index;
  523. return 0;
  524. }
  525. /* NB: Modeled after ath10k_ce_completed_send_next */
  526. int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
  527. void **per_transfer_contextp,
  528. u32 *bufferp,
  529. unsigned int *nbytesp,
  530. unsigned int *transfer_idp)
  531. {
  532. struct ath10k_ce_ring *src_ring;
  533. unsigned int nentries_mask;
  534. unsigned int sw_index;
  535. unsigned int write_index;
  536. int ret;
  537. struct ath10k *ar;
  538. struct ath10k_pci *ar_pci;
  539. src_ring = ce_state->src_ring;
  540. if (!src_ring)
  541. return -EIO;
  542. ar = ce_state->ar;
  543. ar_pci = ath10k_pci_priv(ar);
  544. spin_lock_bh(&ar_pci->ce_lock);
  545. nentries_mask = src_ring->nentries_mask;
  546. sw_index = src_ring->sw_index;
  547. write_index = src_ring->write_index;
  548. if (write_index != sw_index) {
  549. struct ce_desc *base = src_ring->base_addr_owner_space;
  550. struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);
  551. /* Return data from completed source descriptor */
  552. *bufferp = __le32_to_cpu(desc->addr);
  553. *nbytesp = __le16_to_cpu(desc->nbytes);
  554. *transfer_idp = MS(__le16_to_cpu(desc->flags),
  555. CE_DESC_FLAGS_META_DATA);
  556. if (per_transfer_contextp)
  557. *per_transfer_contextp =
  558. src_ring->per_transfer_context[sw_index];
  559. /* sanity */
  560. src_ring->per_transfer_context[sw_index] = NULL;
  561. /* Update sw_index */
  562. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  563. src_ring->sw_index = sw_index;
  564. ret = 0;
  565. } else {
  566. ret = -EIO;
  567. }
  568. spin_unlock_bh(&ar_pci->ce_lock);
  569. return ret;
  570. }
  571. int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
  572. void **per_transfer_contextp,
  573. u32 *bufferp,
  574. unsigned int *nbytesp,
  575. unsigned int *transfer_idp)
  576. {
  577. struct ath10k *ar = ce_state->ar;
  578. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  579. int ret;
  580. spin_lock_bh(&ar_pci->ce_lock);
  581. ret = ath10k_ce_completed_send_next_nolock(ce_state,
  582. per_transfer_contextp,
  583. bufferp, nbytesp,
  584. transfer_idp);
  585. spin_unlock_bh(&ar_pci->ce_lock);
  586. return ret;
  587. }
  588. /*
  589. * Guts of interrupt handler for per-engine interrupts on a particular CE.
  590. *
  591. * Invokes registered callbacks for recv_complete,
  592. * send_complete, and watermarks.
  593. */
  594. void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
  595. {
  596. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  597. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  598. u32 ctrl_addr = ce_state->ctrl_addr;
  599. spin_lock_bh(&ar_pci->ce_lock);
  600. /* Clear the copy-complete interrupts that will be handled here. */
  601. ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
  602. HOST_IS_COPY_COMPLETE_MASK);
  603. spin_unlock_bh(&ar_pci->ce_lock);
  604. if (ce_state->recv_cb)
  605. ce_state->recv_cb(ce_state);
  606. if (ce_state->send_cb)
  607. ce_state->send_cb(ce_state);
  608. spin_lock_bh(&ar_pci->ce_lock);
  609. /*
  610. * Misc CE interrupts are not being handled, but still need
  611. * to be cleared.
  612. */
  613. ath10k_ce_engine_int_status_clear(ar, ctrl_addr, CE_WATERMARK_MASK);
  614. spin_unlock_bh(&ar_pci->ce_lock);
  615. }
  616. /*
  617. * Handler for per-engine interrupts on ALL active CEs.
  618. * This is used in cases where the system is sharing a
  619. * single interrput for all CEs
  620. */
  621. void ath10k_ce_per_engine_service_any(struct ath10k *ar)
  622. {
  623. int ce_id;
  624. u32 intr_summary;
  625. intr_summary = CE_INTERRUPT_SUMMARY(ar);
  626. for (ce_id = 0; intr_summary && (ce_id < CE_COUNT); ce_id++) {
  627. if (intr_summary & (1 << ce_id))
  628. intr_summary &= ~(1 << ce_id);
  629. else
  630. /* no intr pending on this CE */
  631. continue;
  632. ath10k_ce_per_engine_service(ar, ce_id);
  633. }
  634. }
  635. /*
  636. * Adjust interrupts for the copy complete handler.
  637. * If it's needed for either send or recv, then unmask
  638. * this interrupt; otherwise, mask it.
  639. *
  640. * Called with ce_lock held.
  641. */
  642. static void ath10k_ce_per_engine_handler_adjust(struct ath10k_ce_pipe *ce_state)
  643. {
  644. u32 ctrl_addr = ce_state->ctrl_addr;
  645. struct ath10k *ar = ce_state->ar;
  646. bool disable_copy_compl_intr = ce_state->attr_flags & CE_ATTR_DIS_INTR;
  647. if ((!disable_copy_compl_intr) &&
  648. (ce_state->send_cb || ce_state->recv_cb))
  649. ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
  650. else
  651. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  652. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  653. }
  654. int ath10k_ce_disable_interrupts(struct ath10k *ar)
  655. {
  656. int ce_id;
  657. for (ce_id = 0; ce_id < CE_COUNT; ce_id++) {
  658. u32 ctrl_addr = ath10k_ce_base_address(ce_id);
  659. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  660. ath10k_ce_error_intr_disable(ar, ctrl_addr);
  661. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  662. }
  663. return 0;
  664. }
  665. void ath10k_ce_enable_interrupts(struct ath10k *ar)
  666. {
  667. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  668. int ce_id;
  669. for (ce_id = 0; ce_id < CE_COUNT; ce_id++)
  670. ath10k_ce_per_engine_handler_adjust(&ar_pci->ce_states[ce_id]);
  671. }
  672. static int ath10k_ce_init_src_ring(struct ath10k *ar,
  673. unsigned int ce_id,
  674. const struct ce_attr *attr)
  675. {
  676. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  677. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  678. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  679. u32 nentries, ctrl_addr = ath10k_ce_base_address(ce_id);
  680. nentries = roundup_pow_of_two(attr->src_nentries);
  681. memset(src_ring->per_transfer_context, 0,
  682. nentries * sizeof(*src_ring->per_transfer_context));
  683. src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  684. src_ring->sw_index &= src_ring->nentries_mask;
  685. src_ring->hw_index = src_ring->sw_index;
  686. src_ring->write_index =
  687. ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
  688. src_ring->write_index &= src_ring->nentries_mask;
  689. ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr,
  690. src_ring->base_addr_ce_space);
  691. ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
  692. ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
  693. ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
  694. ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
  695. ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);
  696. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  697. "boot init ce src ring id %d entries %d base_addr %p\n",
  698. ce_id, nentries, src_ring->base_addr_owner_space);
  699. return 0;
  700. }
  701. static int ath10k_ce_init_dest_ring(struct ath10k *ar,
  702. unsigned int ce_id,
  703. const struct ce_attr *attr)
  704. {
  705. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  706. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  707. struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
  708. u32 nentries, ctrl_addr = ath10k_ce_base_address(ce_id);
  709. nentries = roundup_pow_of_two(attr->dest_nentries);
  710. memset(dest_ring->per_transfer_context, 0,
  711. nentries * sizeof(*dest_ring->per_transfer_context));
  712. dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
  713. dest_ring->sw_index &= dest_ring->nentries_mask;
  714. dest_ring->write_index =
  715. ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
  716. dest_ring->write_index &= dest_ring->nentries_mask;
  717. ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr,
  718. dest_ring->base_addr_ce_space);
  719. ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
  720. ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
  721. ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
  722. ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);
  723. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  724. "boot ce dest ring id %d entries %d base_addr %p\n",
  725. ce_id, nentries, dest_ring->base_addr_owner_space);
  726. return 0;
  727. }
  728. static struct ath10k_ce_ring *
  729. ath10k_ce_alloc_src_ring(struct ath10k *ar, unsigned int ce_id,
  730. const struct ce_attr *attr)
  731. {
  732. struct ath10k_ce_ring *src_ring;
  733. u32 nentries = attr->src_nentries;
  734. dma_addr_t base_addr;
  735. nentries = roundup_pow_of_two(nentries);
  736. src_ring = kzalloc(sizeof(*src_ring) +
  737. (nentries *
  738. sizeof(*src_ring->per_transfer_context)),
  739. GFP_KERNEL);
  740. if (src_ring == NULL)
  741. return ERR_PTR(-ENOMEM);
  742. src_ring->nentries = nentries;
  743. src_ring->nentries_mask = nentries - 1;
  744. /*
  745. * Legacy platforms that do not support cache
  746. * coherent DMA are unsupported
  747. */
  748. src_ring->base_addr_owner_space_unaligned =
  749. dma_alloc_coherent(ar->dev,
  750. (nentries * sizeof(struct ce_desc) +
  751. CE_DESC_RING_ALIGN),
  752. &base_addr, GFP_KERNEL);
  753. if (!src_ring->base_addr_owner_space_unaligned) {
  754. kfree(src_ring);
  755. return ERR_PTR(-ENOMEM);
  756. }
  757. src_ring->base_addr_ce_space_unaligned = base_addr;
  758. src_ring->base_addr_owner_space = PTR_ALIGN(
  759. src_ring->base_addr_owner_space_unaligned,
  760. CE_DESC_RING_ALIGN);
  761. src_ring->base_addr_ce_space = ALIGN(
  762. src_ring->base_addr_ce_space_unaligned,
  763. CE_DESC_RING_ALIGN);
  764. /*
  765. * Also allocate a shadow src ring in regular
  766. * mem to use for faster access.
  767. */
  768. src_ring->shadow_base_unaligned =
  769. kmalloc((nentries * sizeof(struct ce_desc) +
  770. CE_DESC_RING_ALIGN), GFP_KERNEL);
  771. if (!src_ring->shadow_base_unaligned) {
  772. dma_free_coherent(ar->dev,
  773. (nentries * sizeof(struct ce_desc) +
  774. CE_DESC_RING_ALIGN),
  775. src_ring->base_addr_owner_space,
  776. src_ring->base_addr_ce_space);
  777. kfree(src_ring);
  778. return ERR_PTR(-ENOMEM);
  779. }
  780. src_ring->shadow_base = PTR_ALIGN(
  781. src_ring->shadow_base_unaligned,
  782. CE_DESC_RING_ALIGN);
  783. return src_ring;
  784. }
  785. static struct ath10k_ce_ring *
  786. ath10k_ce_alloc_dest_ring(struct ath10k *ar, unsigned int ce_id,
  787. const struct ce_attr *attr)
  788. {
  789. struct ath10k_ce_ring *dest_ring;
  790. u32 nentries;
  791. dma_addr_t base_addr;
  792. nentries = roundup_pow_of_two(attr->dest_nentries);
  793. dest_ring = kzalloc(sizeof(*dest_ring) +
  794. (nentries *
  795. sizeof(*dest_ring->per_transfer_context)),
  796. GFP_KERNEL);
  797. if (dest_ring == NULL)
  798. return ERR_PTR(-ENOMEM);
  799. dest_ring->nentries = nentries;
  800. dest_ring->nentries_mask = nentries - 1;
  801. /*
  802. * Legacy platforms that do not support cache
  803. * coherent DMA are unsupported
  804. */
  805. dest_ring->base_addr_owner_space_unaligned =
  806. dma_alloc_coherent(ar->dev,
  807. (nentries * sizeof(struct ce_desc) +
  808. CE_DESC_RING_ALIGN),
  809. &base_addr, GFP_KERNEL);
  810. if (!dest_ring->base_addr_owner_space_unaligned) {
  811. kfree(dest_ring);
  812. return ERR_PTR(-ENOMEM);
  813. }
  814. dest_ring->base_addr_ce_space_unaligned = base_addr;
  815. /*
  816. * Correctly initialize memory to 0 to prevent garbage
  817. * data crashing system when download firmware
  818. */
  819. memset(dest_ring->base_addr_owner_space_unaligned, 0,
  820. nentries * sizeof(struct ce_desc) + CE_DESC_RING_ALIGN);
  821. dest_ring->base_addr_owner_space = PTR_ALIGN(
  822. dest_ring->base_addr_owner_space_unaligned,
  823. CE_DESC_RING_ALIGN);
  824. dest_ring->base_addr_ce_space = ALIGN(
  825. dest_ring->base_addr_ce_space_unaligned,
  826. CE_DESC_RING_ALIGN);
  827. return dest_ring;
  828. }
  829. /*
  830. * Initialize a Copy Engine based on caller-supplied attributes.
  831. * This may be called once to initialize both source and destination
  832. * rings or it may be called twice for separate source and destination
  833. * initialization. It may be that only one side or the other is
  834. * initialized by software/firmware.
  835. */
  836. int ath10k_ce_init_pipe(struct ath10k *ar, unsigned int ce_id,
  837. const struct ce_attr *attr,
  838. void (*send_cb)(struct ath10k_ce_pipe *),
  839. void (*recv_cb)(struct ath10k_ce_pipe *))
  840. {
  841. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  842. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  843. int ret;
  844. /*
  845. * Make sure there's enough CE ringbuffer entries for HTT TX to avoid
  846. * additional TX locking checks.
  847. *
  848. * For the lack of a better place do the check here.
  849. */
  850. BUILD_BUG_ON(2*TARGET_NUM_MSDU_DESC >
  851. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  852. BUILD_BUG_ON(2*TARGET_10X_NUM_MSDU_DESC >
  853. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  854. spin_lock_bh(&ar_pci->ce_lock);
  855. ce_state->ar = ar;
  856. ce_state->id = ce_id;
  857. ce_state->ctrl_addr = ath10k_ce_base_address(ce_id);
  858. ce_state->attr_flags = attr->flags;
  859. ce_state->src_sz_max = attr->src_sz_max;
  860. if (attr->src_nentries)
  861. ce_state->send_cb = send_cb;
  862. if (attr->dest_nentries)
  863. ce_state->recv_cb = recv_cb;
  864. spin_unlock_bh(&ar_pci->ce_lock);
  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(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(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_pci *ar_pci = ath10k_pci_priv(ar);
  907. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  908. int ret;
  909. if (attr->src_nentries) {
  910. ce_state->src_ring = ath10k_ce_alloc_src_ring(ar, ce_id, attr);
  911. if (IS_ERR(ce_state->src_ring)) {
  912. ret = PTR_ERR(ce_state->src_ring);
  913. ath10k_err(ar, "failed to allocate copy engine source ring %d: %d\n",
  914. ce_id, ret);
  915. ce_state->src_ring = NULL;
  916. return ret;
  917. }
  918. }
  919. if (attr->dest_nentries) {
  920. ce_state->dest_ring = ath10k_ce_alloc_dest_ring(ar, ce_id,
  921. attr);
  922. if (IS_ERR(ce_state->dest_ring)) {
  923. ret = PTR_ERR(ce_state->dest_ring);
  924. ath10k_err(ar, "failed to allocate copy engine destination ring %d: %d\n",
  925. ce_id, ret);
  926. ce_state->dest_ring = NULL;
  927. return ret;
  928. }
  929. }
  930. return 0;
  931. }
  932. void ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
  933. {
  934. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  935. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  936. if (ce_state->src_ring) {
  937. kfree(ce_state->src_ring->shadow_base_unaligned);
  938. dma_free_coherent(ar->dev,
  939. (ce_state->src_ring->nentries *
  940. sizeof(struct ce_desc) +
  941. CE_DESC_RING_ALIGN),
  942. ce_state->src_ring->base_addr_owner_space,
  943. ce_state->src_ring->base_addr_ce_space);
  944. kfree(ce_state->src_ring);
  945. }
  946. if (ce_state->dest_ring) {
  947. dma_free_coherent(ar->dev,
  948. (ce_state->dest_ring->nentries *
  949. sizeof(struct ce_desc) +
  950. CE_DESC_RING_ALIGN),
  951. ce_state->dest_ring->base_addr_owner_space,
  952. ce_state->dest_ring->base_addr_ce_space);
  953. kfree(ce_state->dest_ring);
  954. }
  955. ce_state->src_ring = NULL;
  956. ce_state->dest_ring = NULL;
  957. }