ce.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. 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. struct ath10k *ar = ce_state->ar;
  381. unsigned int sw_index = dest_ring->sw_index;
  382. struct ce_desc *base = dest_ring->base_addr_owner_space;
  383. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
  384. struct ce_desc sdesc;
  385. u16 nbytes;
  386. /* Copy in one go for performance reasons */
  387. sdesc = *desc;
  388. nbytes = __le16_to_cpu(sdesc.nbytes);
  389. if (nbytes == 0) {
  390. /*
  391. * This closes a relatively unusual race where the Host
  392. * sees the updated DRRI before the update to the
  393. * corresponding descriptor has completed. We treat this
  394. * as a descriptor that is not yet done.
  395. */
  396. return -EIO;
  397. }
  398. desc->nbytes = 0;
  399. /* Return data from completed destination descriptor */
  400. *bufferp = __le32_to_cpu(sdesc.addr);
  401. *nbytesp = nbytes;
  402. *transfer_idp = MS(__le16_to_cpu(sdesc.flags), CE_DESC_FLAGS_META_DATA);
  403. if (__le16_to_cpu(sdesc.flags) & CE_DESC_FLAGS_BYTE_SWAP)
  404. *flagsp = CE_RECV_FLAG_SWAPPED;
  405. else
  406. *flagsp = 0;
  407. if (per_transfer_contextp)
  408. *per_transfer_contextp =
  409. dest_ring->per_transfer_context[sw_index];
  410. /* sanity */
  411. dest_ring->per_transfer_context[sw_index] = NULL;
  412. /* Update sw_index */
  413. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  414. dest_ring->sw_index = sw_index;
  415. return 0;
  416. }
  417. int ath10k_ce_completed_recv_next(struct ath10k_ce_pipe *ce_state,
  418. void **per_transfer_contextp,
  419. u32 *bufferp,
  420. unsigned int *nbytesp,
  421. unsigned int *transfer_idp,
  422. unsigned int *flagsp)
  423. {
  424. struct ath10k *ar = ce_state->ar;
  425. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  426. int ret;
  427. spin_lock_bh(&ar_pci->ce_lock);
  428. ret = ath10k_ce_completed_recv_next_nolock(ce_state,
  429. per_transfer_contextp,
  430. bufferp, nbytesp,
  431. transfer_idp, flagsp);
  432. spin_unlock_bh(&ar_pci->ce_lock);
  433. return ret;
  434. }
  435. int ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
  436. void **per_transfer_contextp,
  437. u32 *bufferp)
  438. {
  439. struct ath10k_ce_ring *dest_ring;
  440. unsigned int nentries_mask;
  441. unsigned int sw_index;
  442. unsigned int write_index;
  443. int ret;
  444. struct ath10k *ar;
  445. struct ath10k_pci *ar_pci;
  446. dest_ring = ce_state->dest_ring;
  447. if (!dest_ring)
  448. return -EIO;
  449. ar = ce_state->ar;
  450. ar_pci = ath10k_pci_priv(ar);
  451. spin_lock_bh(&ar_pci->ce_lock);
  452. nentries_mask = dest_ring->nentries_mask;
  453. sw_index = dest_ring->sw_index;
  454. write_index = dest_ring->write_index;
  455. if (write_index != sw_index) {
  456. struct ce_desc *base = dest_ring->base_addr_owner_space;
  457. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
  458. /* Return data from completed destination descriptor */
  459. *bufferp = __le32_to_cpu(desc->addr);
  460. if (per_transfer_contextp)
  461. *per_transfer_contextp =
  462. dest_ring->per_transfer_context[sw_index];
  463. /* sanity */
  464. dest_ring->per_transfer_context[sw_index] = NULL;
  465. desc->nbytes = 0;
  466. /* Update sw_index */
  467. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  468. dest_ring->sw_index = sw_index;
  469. ret = 0;
  470. } else {
  471. ret = -EIO;
  472. }
  473. spin_unlock_bh(&ar_pci->ce_lock);
  474. return ret;
  475. }
  476. /*
  477. * Guts of ath10k_ce_completed_send_next.
  478. * The caller takes responsibility for any necessary locking.
  479. */
  480. int ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
  481. void **per_transfer_contextp,
  482. u32 *bufferp,
  483. unsigned int *nbytesp,
  484. unsigned int *transfer_idp)
  485. {
  486. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  487. u32 ctrl_addr = ce_state->ctrl_addr;
  488. struct ath10k *ar = ce_state->ar;
  489. unsigned int nentries_mask = src_ring->nentries_mask;
  490. unsigned int sw_index = src_ring->sw_index;
  491. struct ce_desc *sdesc, *sbase;
  492. unsigned int read_index;
  493. if (src_ring->hw_index == sw_index) {
  494. /*
  495. * The SW completion index has caught up with the cached
  496. * version of the HW completion index.
  497. * Update the cached HW completion index to see whether
  498. * the SW has really caught up to the HW, or if the cached
  499. * value of the HW index has become stale.
  500. */
  501. read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  502. if (read_index == 0xffffffff)
  503. return -ENODEV;
  504. read_index &= nentries_mask;
  505. src_ring->hw_index = read_index;
  506. }
  507. read_index = src_ring->hw_index;
  508. if (read_index == sw_index)
  509. return -EIO;
  510. sbase = src_ring->shadow_base;
  511. sdesc = CE_SRC_RING_TO_DESC(sbase, sw_index);
  512. /* Return data from completed source descriptor */
  513. *bufferp = __le32_to_cpu(sdesc->addr);
  514. *nbytesp = __le16_to_cpu(sdesc->nbytes);
  515. *transfer_idp = MS(__le16_to_cpu(sdesc->flags),
  516. CE_DESC_FLAGS_META_DATA);
  517. if (per_transfer_contextp)
  518. *per_transfer_contextp =
  519. src_ring->per_transfer_context[sw_index];
  520. /* sanity */
  521. src_ring->per_transfer_context[sw_index] = NULL;
  522. /* Update sw_index */
  523. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  524. src_ring->sw_index = sw_index;
  525. return 0;
  526. }
  527. /* NB: Modeled after ath10k_ce_completed_send_next */
  528. int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
  529. void **per_transfer_contextp,
  530. u32 *bufferp,
  531. unsigned int *nbytesp,
  532. unsigned int *transfer_idp)
  533. {
  534. struct ath10k_ce_ring *src_ring;
  535. unsigned int nentries_mask;
  536. unsigned int sw_index;
  537. unsigned int write_index;
  538. int ret;
  539. struct ath10k *ar;
  540. struct ath10k_pci *ar_pci;
  541. src_ring = ce_state->src_ring;
  542. if (!src_ring)
  543. return -EIO;
  544. ar = ce_state->ar;
  545. ar_pci = ath10k_pci_priv(ar);
  546. spin_lock_bh(&ar_pci->ce_lock);
  547. nentries_mask = src_ring->nentries_mask;
  548. sw_index = src_ring->sw_index;
  549. write_index = src_ring->write_index;
  550. if (write_index != sw_index) {
  551. struct ce_desc *base = src_ring->base_addr_owner_space;
  552. struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);
  553. /* Return data from completed source descriptor */
  554. *bufferp = __le32_to_cpu(desc->addr);
  555. *nbytesp = __le16_to_cpu(desc->nbytes);
  556. *transfer_idp = MS(__le16_to_cpu(desc->flags),
  557. CE_DESC_FLAGS_META_DATA);
  558. if (per_transfer_contextp)
  559. *per_transfer_contextp =
  560. src_ring->per_transfer_context[sw_index];
  561. /* sanity */
  562. src_ring->per_transfer_context[sw_index] = NULL;
  563. /* Update sw_index */
  564. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  565. src_ring->sw_index = sw_index;
  566. ret = 0;
  567. } else {
  568. ret = -EIO;
  569. }
  570. spin_unlock_bh(&ar_pci->ce_lock);
  571. return ret;
  572. }
  573. int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
  574. void **per_transfer_contextp,
  575. u32 *bufferp,
  576. unsigned int *nbytesp,
  577. unsigned int *transfer_idp)
  578. {
  579. struct ath10k *ar = ce_state->ar;
  580. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  581. int ret;
  582. spin_lock_bh(&ar_pci->ce_lock);
  583. ret = ath10k_ce_completed_send_next_nolock(ce_state,
  584. per_transfer_contextp,
  585. bufferp, nbytesp,
  586. transfer_idp);
  587. spin_unlock_bh(&ar_pci->ce_lock);
  588. return ret;
  589. }
  590. /*
  591. * Guts of interrupt handler for per-engine interrupts on a particular CE.
  592. *
  593. * Invokes registered callbacks for recv_complete,
  594. * send_complete, and watermarks.
  595. */
  596. void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
  597. {
  598. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  599. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  600. u32 ctrl_addr = ce_state->ctrl_addr;
  601. spin_lock_bh(&ar_pci->ce_lock);
  602. /* Clear the copy-complete interrupts that will be handled here. */
  603. ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
  604. HOST_IS_COPY_COMPLETE_MASK);
  605. spin_unlock_bh(&ar_pci->ce_lock);
  606. if (ce_state->recv_cb)
  607. ce_state->recv_cb(ce_state);
  608. if (ce_state->send_cb)
  609. ce_state->send_cb(ce_state);
  610. spin_lock_bh(&ar_pci->ce_lock);
  611. /*
  612. * Misc CE interrupts are not being handled, but still need
  613. * to be cleared.
  614. */
  615. ath10k_ce_engine_int_status_clear(ar, ctrl_addr, CE_WATERMARK_MASK);
  616. spin_unlock_bh(&ar_pci->ce_lock);
  617. }
  618. /*
  619. * Handler for per-engine interrupts on ALL active CEs.
  620. * This is used in cases where the system is sharing a
  621. * single interrput for all CEs
  622. */
  623. void ath10k_ce_per_engine_service_any(struct ath10k *ar)
  624. {
  625. int ce_id;
  626. u32 intr_summary;
  627. intr_summary = CE_INTERRUPT_SUMMARY(ar);
  628. for (ce_id = 0; intr_summary && (ce_id < CE_COUNT); ce_id++) {
  629. if (intr_summary & (1 << ce_id))
  630. intr_summary &= ~(1 << ce_id);
  631. else
  632. /* no intr pending on this CE */
  633. continue;
  634. ath10k_ce_per_engine_service(ar, ce_id);
  635. }
  636. }
  637. /*
  638. * Adjust interrupts for the copy complete handler.
  639. * If it's needed for either send or recv, then unmask
  640. * this interrupt; otherwise, mask it.
  641. *
  642. * Called with ce_lock held.
  643. */
  644. static void ath10k_ce_per_engine_handler_adjust(struct ath10k_ce_pipe *ce_state)
  645. {
  646. u32 ctrl_addr = ce_state->ctrl_addr;
  647. struct ath10k *ar = ce_state->ar;
  648. bool disable_copy_compl_intr = ce_state->attr_flags & CE_ATTR_DIS_INTR;
  649. if ((!disable_copy_compl_intr) &&
  650. (ce_state->send_cb || ce_state->recv_cb))
  651. ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
  652. else
  653. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  654. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  655. }
  656. int ath10k_ce_disable_interrupts(struct ath10k *ar)
  657. {
  658. int ce_id;
  659. for (ce_id = 0; ce_id < CE_COUNT; ce_id++) {
  660. u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  661. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  662. ath10k_ce_error_intr_disable(ar, ctrl_addr);
  663. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  664. }
  665. return 0;
  666. }
  667. void ath10k_ce_enable_interrupts(struct ath10k *ar)
  668. {
  669. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  670. int ce_id;
  671. /* Skip the last copy engine, CE7 the diagnostic window, as that
  672. * uses polling and isn't initialized for interrupts.
  673. */
  674. for (ce_id = 0; ce_id < CE_COUNT - 1; ce_id++)
  675. ath10k_ce_per_engine_handler_adjust(&ar_pci->ce_states[ce_id]);
  676. }
  677. static int ath10k_ce_init_src_ring(struct ath10k *ar,
  678. unsigned int ce_id,
  679. const struct ce_attr *attr)
  680. {
  681. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  682. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  683. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  684. u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  685. nentries = roundup_pow_of_two(attr->src_nentries);
  686. memset(src_ring->base_addr_owner_space, 0,
  687. nentries * sizeof(struct ce_desc));
  688. src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  689. src_ring->sw_index &= src_ring->nentries_mask;
  690. src_ring->hw_index = src_ring->sw_index;
  691. src_ring->write_index =
  692. ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
  693. src_ring->write_index &= src_ring->nentries_mask;
  694. ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr,
  695. src_ring->base_addr_ce_space);
  696. ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
  697. ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
  698. ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
  699. ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
  700. ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);
  701. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  702. "boot init ce src ring id %d entries %d base_addr %p\n",
  703. ce_id, nentries, src_ring->base_addr_owner_space);
  704. return 0;
  705. }
  706. static int ath10k_ce_init_dest_ring(struct ath10k *ar,
  707. unsigned int ce_id,
  708. const struct ce_attr *attr)
  709. {
  710. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  711. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  712. struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
  713. u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  714. nentries = roundup_pow_of_two(attr->dest_nentries);
  715. memset(dest_ring->base_addr_owner_space, 0,
  716. nentries * sizeof(struct ce_desc));
  717. dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
  718. dest_ring->sw_index &= dest_ring->nentries_mask;
  719. dest_ring->write_index =
  720. ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
  721. dest_ring->write_index &= dest_ring->nentries_mask;
  722. ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr,
  723. dest_ring->base_addr_ce_space);
  724. ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
  725. ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
  726. ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
  727. ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);
  728. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  729. "boot ce dest ring id %d entries %d base_addr %p\n",
  730. ce_id, nentries, dest_ring->base_addr_owner_space);
  731. return 0;
  732. }
  733. static struct ath10k_ce_ring *
  734. ath10k_ce_alloc_src_ring(struct ath10k *ar, unsigned int ce_id,
  735. const struct ce_attr *attr)
  736. {
  737. struct ath10k_ce_ring *src_ring;
  738. u32 nentries = attr->src_nentries;
  739. dma_addr_t base_addr;
  740. nentries = roundup_pow_of_two(nentries);
  741. src_ring = kzalloc(sizeof(*src_ring) +
  742. (nentries *
  743. sizeof(*src_ring->per_transfer_context)),
  744. GFP_KERNEL);
  745. if (src_ring == NULL)
  746. return ERR_PTR(-ENOMEM);
  747. src_ring->nentries = nentries;
  748. src_ring->nentries_mask = nentries - 1;
  749. /*
  750. * Legacy platforms that do not support cache
  751. * coherent DMA are unsupported
  752. */
  753. src_ring->base_addr_owner_space_unaligned =
  754. dma_alloc_coherent(ar->dev,
  755. (nentries * sizeof(struct ce_desc) +
  756. CE_DESC_RING_ALIGN),
  757. &base_addr, GFP_KERNEL);
  758. if (!src_ring->base_addr_owner_space_unaligned) {
  759. kfree(src_ring);
  760. return ERR_PTR(-ENOMEM);
  761. }
  762. src_ring->base_addr_ce_space_unaligned = base_addr;
  763. src_ring->base_addr_owner_space = PTR_ALIGN(
  764. src_ring->base_addr_owner_space_unaligned,
  765. CE_DESC_RING_ALIGN);
  766. src_ring->base_addr_ce_space = ALIGN(
  767. src_ring->base_addr_ce_space_unaligned,
  768. CE_DESC_RING_ALIGN);
  769. /*
  770. * Also allocate a shadow src ring in regular
  771. * mem to use for faster access.
  772. */
  773. src_ring->shadow_base_unaligned =
  774. kmalloc((nentries * sizeof(struct ce_desc) +
  775. CE_DESC_RING_ALIGN), GFP_KERNEL);
  776. if (!src_ring->shadow_base_unaligned) {
  777. dma_free_coherent(ar->dev,
  778. (nentries * sizeof(struct ce_desc) +
  779. CE_DESC_RING_ALIGN),
  780. src_ring->base_addr_owner_space,
  781. src_ring->base_addr_ce_space);
  782. kfree(src_ring);
  783. return ERR_PTR(-ENOMEM);
  784. }
  785. src_ring->shadow_base = PTR_ALIGN(
  786. src_ring->shadow_base_unaligned,
  787. CE_DESC_RING_ALIGN);
  788. return src_ring;
  789. }
  790. static struct ath10k_ce_ring *
  791. ath10k_ce_alloc_dest_ring(struct ath10k *ar, unsigned int ce_id,
  792. const struct ce_attr *attr)
  793. {
  794. struct ath10k_ce_ring *dest_ring;
  795. u32 nentries;
  796. dma_addr_t base_addr;
  797. nentries = roundup_pow_of_two(attr->dest_nentries);
  798. dest_ring = kzalloc(sizeof(*dest_ring) +
  799. (nentries *
  800. sizeof(*dest_ring->per_transfer_context)),
  801. GFP_KERNEL);
  802. if (dest_ring == NULL)
  803. return ERR_PTR(-ENOMEM);
  804. dest_ring->nentries = nentries;
  805. dest_ring->nentries_mask = nentries - 1;
  806. /*
  807. * Legacy platforms that do not support cache
  808. * coherent DMA are unsupported
  809. */
  810. dest_ring->base_addr_owner_space_unaligned =
  811. dma_alloc_coherent(ar->dev,
  812. (nentries * sizeof(struct ce_desc) +
  813. CE_DESC_RING_ALIGN),
  814. &base_addr, GFP_KERNEL);
  815. if (!dest_ring->base_addr_owner_space_unaligned) {
  816. kfree(dest_ring);
  817. return ERR_PTR(-ENOMEM);
  818. }
  819. dest_ring->base_addr_ce_space_unaligned = base_addr;
  820. /*
  821. * Correctly initialize memory to 0 to prevent garbage
  822. * data crashing system when download firmware
  823. */
  824. memset(dest_ring->base_addr_owner_space_unaligned, 0,
  825. nentries * sizeof(struct ce_desc) + CE_DESC_RING_ALIGN);
  826. dest_ring->base_addr_owner_space = PTR_ALIGN(
  827. dest_ring->base_addr_owner_space_unaligned,
  828. CE_DESC_RING_ALIGN);
  829. dest_ring->base_addr_ce_space = ALIGN(
  830. dest_ring->base_addr_ce_space_unaligned,
  831. CE_DESC_RING_ALIGN);
  832. return dest_ring;
  833. }
  834. /*
  835. * Initialize a Copy Engine based on caller-supplied attributes.
  836. * This may be called once to initialize both source and destination
  837. * rings or it may be called twice for separate source and destination
  838. * initialization. It may be that only one side or the other is
  839. * initialized by software/firmware.
  840. */
  841. int ath10k_ce_init_pipe(struct ath10k *ar, unsigned int ce_id,
  842. const struct ce_attr *attr)
  843. {
  844. int ret;
  845. if (attr->src_nentries) {
  846. ret = ath10k_ce_init_src_ring(ar, ce_id, attr);
  847. if (ret) {
  848. ath10k_err(ar, "Failed to initialize CE src ring for ID: %d (%d)\n",
  849. ce_id, ret);
  850. return ret;
  851. }
  852. }
  853. if (attr->dest_nentries) {
  854. ret = ath10k_ce_init_dest_ring(ar, ce_id, attr);
  855. if (ret) {
  856. ath10k_err(ar, "Failed to initialize CE dest ring for ID: %d (%d)\n",
  857. ce_id, ret);
  858. return ret;
  859. }
  860. }
  861. return 0;
  862. }
  863. static void ath10k_ce_deinit_src_ring(struct ath10k *ar, unsigned int ce_id)
  864. {
  865. u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  866. ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr, 0);
  867. ath10k_ce_src_ring_size_set(ar, ctrl_addr, 0);
  868. ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, 0);
  869. ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, 0);
  870. }
  871. static void ath10k_ce_deinit_dest_ring(struct ath10k *ar, unsigned int ce_id)
  872. {
  873. u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  874. ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr, 0);
  875. ath10k_ce_dest_ring_size_set(ar, ctrl_addr, 0);
  876. ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, 0);
  877. }
  878. void ath10k_ce_deinit_pipe(struct ath10k *ar, unsigned int ce_id)
  879. {
  880. ath10k_ce_deinit_src_ring(ar, ce_id);
  881. ath10k_ce_deinit_dest_ring(ar, ce_id);
  882. }
  883. int ath10k_ce_alloc_pipe(struct ath10k *ar, int ce_id,
  884. const struct ce_attr *attr,
  885. void (*send_cb)(struct ath10k_ce_pipe *),
  886. void (*recv_cb)(struct ath10k_ce_pipe *))
  887. {
  888. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  889. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  890. int ret;
  891. /*
  892. * Make sure there's enough CE ringbuffer entries for HTT TX to avoid
  893. * additional TX locking checks.
  894. *
  895. * For the lack of a better place do the check here.
  896. */
  897. BUILD_BUG_ON(2*TARGET_NUM_MSDU_DESC >
  898. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  899. BUILD_BUG_ON(2*TARGET_10X_NUM_MSDU_DESC >
  900. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  901. BUILD_BUG_ON(2*TARGET_TLV_NUM_MSDU_DESC >
  902. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  903. ce_state->ar = ar;
  904. ce_state->id = ce_id;
  905. ce_state->ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  906. ce_state->attr_flags = attr->flags;
  907. ce_state->src_sz_max = attr->src_sz_max;
  908. if (attr->src_nentries)
  909. ce_state->send_cb = send_cb;
  910. if (attr->dest_nentries)
  911. ce_state->recv_cb = recv_cb;
  912. if (attr->src_nentries) {
  913. ce_state->src_ring = ath10k_ce_alloc_src_ring(ar, ce_id, attr);
  914. if (IS_ERR(ce_state->src_ring)) {
  915. ret = PTR_ERR(ce_state->src_ring);
  916. ath10k_err(ar, "failed to allocate copy engine source ring %d: %d\n",
  917. ce_id, ret);
  918. ce_state->src_ring = NULL;
  919. return ret;
  920. }
  921. }
  922. if (attr->dest_nentries) {
  923. ce_state->dest_ring = ath10k_ce_alloc_dest_ring(ar, ce_id,
  924. attr);
  925. if (IS_ERR(ce_state->dest_ring)) {
  926. ret = PTR_ERR(ce_state->dest_ring);
  927. ath10k_err(ar, "failed to allocate copy engine destination ring %d: %d\n",
  928. ce_id, ret);
  929. ce_state->dest_ring = NULL;
  930. return ret;
  931. }
  932. }
  933. return 0;
  934. }
  935. void ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
  936. {
  937. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  938. struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
  939. if (ce_state->src_ring) {
  940. kfree(ce_state->src_ring->shadow_base_unaligned);
  941. dma_free_coherent(ar->dev,
  942. (ce_state->src_ring->nentries *
  943. sizeof(struct ce_desc) +
  944. CE_DESC_RING_ALIGN),
  945. ce_state->src_ring->base_addr_owner_space,
  946. ce_state->src_ring->base_addr_ce_space);
  947. kfree(ce_state->src_ring);
  948. }
  949. if (ce_state->dest_ring) {
  950. dma_free_coherent(ar->dev,
  951. (ce_state->dest_ring->nentries *
  952. sizeof(struct ce_desc) +
  953. CE_DESC_RING_ALIGN),
  954. ce_state->dest_ring->base_addr_owner_space,
  955. ce_state->dest_ring->base_addr_ce_space);
  956. kfree(ce_state->dest_ring);
  957. }
  958. ce_state->src_ring = NULL;
  959. ce_state->dest_ring = NULL;
  960. }