ce.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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_engine_int_status_clear(struct ath10k *ar,
  211. u32 ce_ctrl_addr,
  212. unsigned int mask)
  213. {
  214. ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IS_ADDRESS, mask);
  215. }
  216. /*
  217. * Guts of ath10k_ce_send, used by both ath10k_ce_send and
  218. * ath10k_ce_sendlist_send.
  219. * The caller takes responsibility for any needed locking.
  220. */
  221. static int ath10k_ce_send_nolock(struct ce_state *ce_state,
  222. void *per_transfer_context,
  223. u32 buffer,
  224. unsigned int nbytes,
  225. unsigned int transfer_id,
  226. unsigned int flags)
  227. {
  228. struct ath10k *ar = ce_state->ar;
  229. struct ce_ring_state *src_ring = ce_state->src_ring;
  230. struct ce_desc *desc, *sdesc;
  231. unsigned int nentries_mask = src_ring->nentries_mask;
  232. unsigned int sw_index = src_ring->sw_index;
  233. unsigned int write_index = src_ring->write_index;
  234. u32 ctrl_addr = ce_state->ctrl_addr;
  235. u32 desc_flags = 0;
  236. int ret = 0;
  237. if (nbytes > ce_state->src_sz_max)
  238. ath10k_warn("%s: send more we can (nbytes: %d, max: %d)\n",
  239. __func__, nbytes, ce_state->src_sz_max);
  240. ath10k_pci_wake(ar);
  241. if (unlikely(CE_RING_DELTA(nentries_mask,
  242. write_index, sw_index - 1) <= 0)) {
  243. ret = -EIO;
  244. goto exit;
  245. }
  246. desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
  247. write_index);
  248. sdesc = CE_SRC_RING_TO_DESC(src_ring->shadow_base, write_index);
  249. desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
  250. if (flags & CE_SEND_FLAG_GATHER)
  251. desc_flags |= CE_DESC_FLAGS_GATHER;
  252. if (flags & CE_SEND_FLAG_BYTE_SWAP)
  253. desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
  254. sdesc->addr = __cpu_to_le32(buffer);
  255. sdesc->nbytes = __cpu_to_le16(nbytes);
  256. sdesc->flags = __cpu_to_le16(desc_flags);
  257. *desc = *sdesc;
  258. src_ring->per_transfer_context[write_index] = per_transfer_context;
  259. /* Update Source Ring Write Index */
  260. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  261. /* WORKAROUND */
  262. if (!(flags & CE_SEND_FLAG_GATHER))
  263. ath10k_ce_src_ring_write_index_set(ar, ctrl_addr, write_index);
  264. src_ring->write_index = write_index;
  265. exit:
  266. ath10k_pci_sleep(ar);
  267. return ret;
  268. }
  269. int ath10k_ce_send(struct ce_state *ce_state,
  270. void *per_transfer_context,
  271. u32 buffer,
  272. unsigned int nbytes,
  273. unsigned int transfer_id,
  274. unsigned int flags)
  275. {
  276. struct ath10k *ar = ce_state->ar;
  277. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  278. int ret;
  279. spin_lock_bh(&ar_pci->ce_lock);
  280. ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
  281. buffer, nbytes, transfer_id, flags);
  282. spin_unlock_bh(&ar_pci->ce_lock);
  283. return ret;
  284. }
  285. void ath10k_ce_sendlist_buf_add(struct ce_sendlist *sendlist, u32 buffer,
  286. unsigned int nbytes, u32 flags)
  287. {
  288. unsigned int num_items = sendlist->num_items;
  289. struct ce_sendlist_item *item;
  290. item = &sendlist->item[num_items];
  291. item->data = buffer;
  292. item->u.nbytes = nbytes;
  293. item->flags = flags;
  294. sendlist->num_items++;
  295. }
  296. int ath10k_ce_sendlist_send(struct ce_state *ce_state,
  297. void *per_transfer_context,
  298. struct ce_sendlist *sendlist,
  299. unsigned int transfer_id)
  300. {
  301. struct ce_ring_state *src_ring = ce_state->src_ring;
  302. struct ce_sendlist_item *item;
  303. struct ath10k *ar = ce_state->ar;
  304. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  305. unsigned int nentries_mask = src_ring->nentries_mask;
  306. unsigned int num_items = sendlist->num_items;
  307. unsigned int sw_index;
  308. unsigned int write_index;
  309. int i, delta, ret = -ENOMEM;
  310. spin_lock_bh(&ar_pci->ce_lock);
  311. sw_index = src_ring->sw_index;
  312. write_index = src_ring->write_index;
  313. delta = CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
  314. if (delta >= num_items) {
  315. /*
  316. * Handle all but the last item uniformly.
  317. */
  318. for (i = 0; i < num_items - 1; i++) {
  319. item = &sendlist->item[i];
  320. ret = ath10k_ce_send_nolock(ce_state,
  321. CE_SENDLIST_ITEM_CTXT,
  322. (u32) item->data,
  323. item->u.nbytes, transfer_id,
  324. item->flags |
  325. CE_SEND_FLAG_GATHER);
  326. if (ret)
  327. ath10k_warn("CE send failed for item: %d\n", i);
  328. }
  329. /*
  330. * Provide valid context pointer for final item.
  331. */
  332. item = &sendlist->item[i];
  333. ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
  334. (u32) item->data, item->u.nbytes,
  335. transfer_id, item->flags);
  336. if (ret)
  337. ath10k_warn("CE send failed for last item: %d\n", i);
  338. }
  339. spin_unlock_bh(&ar_pci->ce_lock);
  340. return ret;
  341. }
  342. int ath10k_ce_recv_buf_enqueue(struct ce_state *ce_state,
  343. void *per_recv_context,
  344. u32 buffer)
  345. {
  346. struct ce_ring_state *dest_ring = ce_state->dest_ring;
  347. u32 ctrl_addr = ce_state->ctrl_addr;
  348. struct ath10k *ar = ce_state->ar;
  349. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  350. unsigned int nentries_mask = dest_ring->nentries_mask;
  351. unsigned int write_index;
  352. unsigned int sw_index;
  353. int ret;
  354. spin_lock_bh(&ar_pci->ce_lock);
  355. write_index = dest_ring->write_index;
  356. sw_index = dest_ring->sw_index;
  357. ath10k_pci_wake(ar);
  358. if (CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) > 0) {
  359. struct ce_desc *base = dest_ring->base_addr_owner_space;
  360. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, write_index);
  361. /* Update destination descriptor */
  362. desc->addr = __cpu_to_le32(buffer);
  363. desc->nbytes = 0;
  364. dest_ring->per_transfer_context[write_index] =
  365. per_recv_context;
  366. /* Update Destination Ring Write Index */
  367. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  368. ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
  369. dest_ring->write_index = write_index;
  370. ret = 0;
  371. } else {
  372. ret = -EIO;
  373. }
  374. ath10k_pci_sleep(ar);
  375. spin_unlock_bh(&ar_pci->ce_lock);
  376. return ret;
  377. }
  378. /*
  379. * Guts of ath10k_ce_completed_recv_next.
  380. * The caller takes responsibility for any necessary locking.
  381. */
  382. static int ath10k_ce_completed_recv_next_nolock(struct ce_state *ce_state,
  383. void **per_transfer_contextp,
  384. u32 *bufferp,
  385. unsigned int *nbytesp,
  386. unsigned int *transfer_idp,
  387. unsigned int *flagsp)
  388. {
  389. struct ce_ring_state *dest_ring = ce_state->dest_ring;
  390. unsigned int nentries_mask = dest_ring->nentries_mask;
  391. unsigned int sw_index = dest_ring->sw_index;
  392. struct ce_desc *base = dest_ring->base_addr_owner_space;
  393. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
  394. struct ce_desc sdesc;
  395. u16 nbytes;
  396. /* Copy in one go for performance reasons */
  397. sdesc = *desc;
  398. nbytes = __le16_to_cpu(sdesc.nbytes);
  399. if (nbytes == 0) {
  400. /*
  401. * This closes a relatively unusual race where the Host
  402. * sees the updated DRRI before the update to the
  403. * corresponding descriptor has completed. We treat this
  404. * as a descriptor that is not yet done.
  405. */
  406. return -EIO;
  407. }
  408. desc->nbytes = 0;
  409. /* Return data from completed destination descriptor */
  410. *bufferp = __le32_to_cpu(sdesc.addr);
  411. *nbytesp = nbytes;
  412. *transfer_idp = MS(__le16_to_cpu(sdesc.flags), CE_DESC_FLAGS_META_DATA);
  413. if (__le16_to_cpu(sdesc.flags) & CE_DESC_FLAGS_BYTE_SWAP)
  414. *flagsp = CE_RECV_FLAG_SWAPPED;
  415. else
  416. *flagsp = 0;
  417. if (per_transfer_contextp)
  418. *per_transfer_contextp =
  419. dest_ring->per_transfer_context[sw_index];
  420. /* sanity */
  421. dest_ring->per_transfer_context[sw_index] = NULL;
  422. /* Update sw_index */
  423. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  424. dest_ring->sw_index = sw_index;
  425. return 0;
  426. }
  427. int ath10k_ce_completed_recv_next(struct ce_state *ce_state,
  428. void **per_transfer_contextp,
  429. u32 *bufferp,
  430. unsigned int *nbytesp,
  431. unsigned int *transfer_idp,
  432. unsigned int *flagsp)
  433. {
  434. struct ath10k *ar = ce_state->ar;
  435. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  436. int ret;
  437. spin_lock_bh(&ar_pci->ce_lock);
  438. ret = ath10k_ce_completed_recv_next_nolock(ce_state,
  439. per_transfer_contextp,
  440. bufferp, nbytesp,
  441. transfer_idp, flagsp);
  442. spin_unlock_bh(&ar_pci->ce_lock);
  443. return ret;
  444. }
  445. int ath10k_ce_revoke_recv_next(struct ce_state *ce_state,
  446. void **per_transfer_contextp,
  447. u32 *bufferp)
  448. {
  449. struct ce_ring_state *dest_ring;
  450. unsigned int nentries_mask;
  451. unsigned int sw_index;
  452. unsigned int write_index;
  453. int ret;
  454. struct ath10k *ar;
  455. struct ath10k_pci *ar_pci;
  456. dest_ring = ce_state->dest_ring;
  457. if (!dest_ring)
  458. return -EIO;
  459. ar = ce_state->ar;
  460. ar_pci = ath10k_pci_priv(ar);
  461. spin_lock_bh(&ar_pci->ce_lock);
  462. nentries_mask = dest_ring->nentries_mask;
  463. sw_index = dest_ring->sw_index;
  464. write_index = dest_ring->write_index;
  465. if (write_index != sw_index) {
  466. struct ce_desc *base = dest_ring->base_addr_owner_space;
  467. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
  468. /* Return data from completed destination descriptor */
  469. *bufferp = __le32_to_cpu(desc->addr);
  470. if (per_transfer_contextp)
  471. *per_transfer_contextp =
  472. dest_ring->per_transfer_context[sw_index];
  473. /* sanity */
  474. dest_ring->per_transfer_context[sw_index] = NULL;
  475. /* Update sw_index */
  476. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  477. dest_ring->sw_index = sw_index;
  478. ret = 0;
  479. } else {
  480. ret = -EIO;
  481. }
  482. spin_unlock_bh(&ar_pci->ce_lock);
  483. return ret;
  484. }
  485. /*
  486. * Guts of ath10k_ce_completed_send_next.
  487. * The caller takes responsibility for any necessary locking.
  488. */
  489. static int ath10k_ce_completed_send_next_nolock(struct ce_state *ce_state,
  490. void **per_transfer_contextp,
  491. u32 *bufferp,
  492. unsigned int *nbytesp,
  493. unsigned int *transfer_idp)
  494. {
  495. struct ce_ring_state *src_ring = ce_state->src_ring;
  496. u32 ctrl_addr = ce_state->ctrl_addr;
  497. struct ath10k *ar = ce_state->ar;
  498. unsigned int nentries_mask = src_ring->nentries_mask;
  499. unsigned int sw_index = src_ring->sw_index;
  500. unsigned int read_index;
  501. int ret = -EIO;
  502. if (src_ring->hw_index == sw_index) {
  503. /*
  504. * The SW completion index has caught up with the cached
  505. * version of the HW completion index.
  506. * Update the cached HW completion index to see whether
  507. * the SW has really caught up to the HW, or if the cached
  508. * value of the HW index has become stale.
  509. */
  510. ath10k_pci_wake(ar);
  511. src_ring->hw_index =
  512. ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  513. src_ring->hw_index &= nentries_mask;
  514. ath10k_pci_sleep(ar);
  515. }
  516. read_index = src_ring->hw_index;
  517. if ((read_index != sw_index) && (read_index != 0xffffffff)) {
  518. struct ce_desc *sbase = src_ring->shadow_base;
  519. struct ce_desc *sdesc = CE_SRC_RING_TO_DESC(sbase, sw_index);
  520. /* Return data from completed source descriptor */
  521. *bufferp = __le32_to_cpu(sdesc->addr);
  522. *nbytesp = __le16_to_cpu(sdesc->nbytes);
  523. *transfer_idp = MS(__le16_to_cpu(sdesc->flags),
  524. CE_DESC_FLAGS_META_DATA);
  525. if (per_transfer_contextp)
  526. *per_transfer_contextp =
  527. src_ring->per_transfer_context[sw_index];
  528. /* sanity */
  529. src_ring->per_transfer_context[sw_index] = NULL;
  530. /* Update sw_index */
  531. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  532. src_ring->sw_index = sw_index;
  533. ret = 0;
  534. }
  535. return ret;
  536. }
  537. /* NB: Modeled after ath10k_ce_completed_send_next */
  538. int ath10k_ce_cancel_send_next(struct ce_state *ce_state,
  539. void **per_transfer_contextp,
  540. u32 *bufferp,
  541. unsigned int *nbytesp,
  542. unsigned int *transfer_idp)
  543. {
  544. struct ce_ring_state *src_ring;
  545. unsigned int nentries_mask;
  546. unsigned int sw_index;
  547. unsigned int write_index;
  548. int ret;
  549. struct ath10k *ar;
  550. struct ath10k_pci *ar_pci;
  551. src_ring = ce_state->src_ring;
  552. if (!src_ring)
  553. return -EIO;
  554. ar = ce_state->ar;
  555. ar_pci = ath10k_pci_priv(ar);
  556. spin_lock_bh(&ar_pci->ce_lock);
  557. nentries_mask = src_ring->nentries_mask;
  558. sw_index = src_ring->sw_index;
  559. write_index = src_ring->write_index;
  560. if (write_index != sw_index) {
  561. struct ce_desc *base = src_ring->base_addr_owner_space;
  562. struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);
  563. /* Return data from completed source descriptor */
  564. *bufferp = __le32_to_cpu(desc->addr);
  565. *nbytesp = __le16_to_cpu(desc->nbytes);
  566. *transfer_idp = MS(__le16_to_cpu(desc->flags),
  567. CE_DESC_FLAGS_META_DATA);
  568. if (per_transfer_contextp)
  569. *per_transfer_contextp =
  570. src_ring->per_transfer_context[sw_index];
  571. /* sanity */
  572. src_ring->per_transfer_context[sw_index] = NULL;
  573. /* Update sw_index */
  574. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  575. src_ring->sw_index = sw_index;
  576. ret = 0;
  577. } else {
  578. ret = -EIO;
  579. }
  580. spin_unlock_bh(&ar_pci->ce_lock);
  581. return ret;
  582. }
  583. int ath10k_ce_completed_send_next(struct ce_state *ce_state,
  584. void **per_transfer_contextp,
  585. u32 *bufferp,
  586. unsigned int *nbytesp,
  587. unsigned int *transfer_idp)
  588. {
  589. struct ath10k *ar = ce_state->ar;
  590. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  591. int ret;
  592. spin_lock_bh(&ar_pci->ce_lock);
  593. ret = ath10k_ce_completed_send_next_nolock(ce_state,
  594. per_transfer_contextp,
  595. bufferp, nbytesp,
  596. transfer_idp);
  597. spin_unlock_bh(&ar_pci->ce_lock);
  598. return ret;
  599. }
  600. /*
  601. * Guts of interrupt handler for per-engine interrupts on a particular CE.
  602. *
  603. * Invokes registered callbacks for recv_complete,
  604. * send_complete, and watermarks.
  605. */
  606. void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
  607. {
  608. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  609. struct ce_state *ce_state = ar_pci->ce_id_to_state[ce_id];
  610. u32 ctrl_addr = ce_state->ctrl_addr;
  611. void *transfer_context;
  612. u32 buf;
  613. unsigned int nbytes;
  614. unsigned int id;
  615. unsigned int flags;
  616. ath10k_pci_wake(ar);
  617. spin_lock_bh(&ar_pci->ce_lock);
  618. /* Clear the copy-complete interrupts that will be handled here. */
  619. ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
  620. HOST_IS_COPY_COMPLETE_MASK);
  621. if (ce_state->recv_cb) {
  622. /*
  623. * Pop completed recv buffers and call the registered
  624. * recv callback for each
  625. */
  626. while (ath10k_ce_completed_recv_next_nolock(ce_state,
  627. &transfer_context,
  628. &buf, &nbytes,
  629. &id, &flags) == 0) {
  630. spin_unlock_bh(&ar_pci->ce_lock);
  631. ce_state->recv_cb(ce_state, transfer_context, buf,
  632. nbytes, id, flags);
  633. spin_lock_bh(&ar_pci->ce_lock);
  634. }
  635. }
  636. if (ce_state->send_cb) {
  637. /*
  638. * Pop completed send buffers and call the registered
  639. * send callback for each
  640. */
  641. while (ath10k_ce_completed_send_next_nolock(ce_state,
  642. &transfer_context,
  643. &buf,
  644. &nbytes,
  645. &id) == 0) {
  646. spin_unlock_bh(&ar_pci->ce_lock);
  647. ce_state->send_cb(ce_state, transfer_context,
  648. buf, nbytes, id);
  649. spin_lock_bh(&ar_pci->ce_lock);
  650. }
  651. }
  652. /*
  653. * Misc CE interrupts are not being handled, but still need
  654. * to be cleared.
  655. */
  656. ath10k_ce_engine_int_status_clear(ar, ctrl_addr, CE_WATERMARK_MASK);
  657. spin_unlock_bh(&ar_pci->ce_lock);
  658. ath10k_pci_sleep(ar);
  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. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  668. int ce_id;
  669. u32 intr_summary;
  670. ath10k_pci_wake(ar);
  671. intr_summary = CE_INTERRUPT_SUMMARY(ar);
  672. for (ce_id = 0; intr_summary && (ce_id < ar_pci->ce_count); ce_id++) {
  673. if (intr_summary & (1 << ce_id))
  674. intr_summary &= ~(1 << ce_id);
  675. else
  676. /* no intr pending on this CE */
  677. continue;
  678. ath10k_ce_per_engine_service(ar, ce_id);
  679. }
  680. ath10k_pci_sleep(ar);
  681. }
  682. /*
  683. * Adjust interrupts for the copy complete handler.
  684. * If it's needed for either send or recv, then unmask
  685. * this interrupt; otherwise, mask it.
  686. *
  687. * Called with ce_lock held.
  688. */
  689. static void ath10k_ce_per_engine_handler_adjust(struct ce_state *ce_state,
  690. int disable_copy_compl_intr)
  691. {
  692. u32 ctrl_addr = ce_state->ctrl_addr;
  693. struct ath10k *ar = ce_state->ar;
  694. ath10k_pci_wake(ar);
  695. if ((!disable_copy_compl_intr) &&
  696. (ce_state->send_cb || ce_state->recv_cb))
  697. ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
  698. else
  699. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  700. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  701. ath10k_pci_sleep(ar);
  702. }
  703. void ath10k_ce_disable_interrupts(struct ath10k *ar)
  704. {
  705. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  706. int ce_id;
  707. ath10k_pci_wake(ar);
  708. for (ce_id = 0; ce_id < ar_pci->ce_count; ce_id++) {
  709. struct ce_state *ce_state = ar_pci->ce_id_to_state[ce_id];
  710. u32 ctrl_addr = ce_state->ctrl_addr;
  711. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  712. }
  713. ath10k_pci_sleep(ar);
  714. }
  715. void ath10k_ce_send_cb_register(struct ce_state *ce_state,
  716. void (*send_cb) (struct ce_state *ce_state,
  717. void *transfer_context,
  718. u32 buffer,
  719. unsigned int nbytes,
  720. unsigned int transfer_id),
  721. int disable_interrupts)
  722. {
  723. struct ath10k *ar = ce_state->ar;
  724. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  725. spin_lock_bh(&ar_pci->ce_lock);
  726. ce_state->send_cb = send_cb;
  727. ath10k_ce_per_engine_handler_adjust(ce_state, disable_interrupts);
  728. spin_unlock_bh(&ar_pci->ce_lock);
  729. }
  730. void ath10k_ce_recv_cb_register(struct ce_state *ce_state,
  731. void (*recv_cb) (struct ce_state *ce_state,
  732. void *transfer_context,
  733. u32 buffer,
  734. unsigned int nbytes,
  735. unsigned int transfer_id,
  736. unsigned int flags))
  737. {
  738. struct ath10k *ar = ce_state->ar;
  739. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  740. spin_lock_bh(&ar_pci->ce_lock);
  741. ce_state->recv_cb = recv_cb;
  742. ath10k_ce_per_engine_handler_adjust(ce_state, 0);
  743. spin_unlock_bh(&ar_pci->ce_lock);
  744. }
  745. static int ath10k_ce_init_src_ring(struct ath10k *ar,
  746. unsigned int ce_id,
  747. struct ce_state *ce_state,
  748. const struct ce_attr *attr)
  749. {
  750. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  751. struct ce_ring_state *src_ring;
  752. unsigned int nentries = attr->src_nentries;
  753. unsigned int ce_nbytes;
  754. u32 ctrl_addr = ath10k_ce_base_address(ce_id);
  755. dma_addr_t base_addr;
  756. char *ptr;
  757. nentries = roundup_pow_of_two(nentries);
  758. if (ce_state->src_ring) {
  759. WARN_ON(ce_state->src_ring->nentries != nentries);
  760. return 0;
  761. }
  762. ce_nbytes = sizeof(struct ce_ring_state) + (nentries * sizeof(void *));
  763. ptr = kzalloc(ce_nbytes, GFP_KERNEL);
  764. if (ptr == NULL)
  765. return -ENOMEM;
  766. ce_state->src_ring = (struct ce_ring_state *)ptr;
  767. src_ring = ce_state->src_ring;
  768. ptr += sizeof(struct ce_ring_state);
  769. src_ring->nentries = nentries;
  770. src_ring->nentries_mask = nentries - 1;
  771. ath10k_pci_wake(ar);
  772. src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  773. src_ring->sw_index &= src_ring->nentries_mask;
  774. src_ring->hw_index = src_ring->sw_index;
  775. src_ring->write_index =
  776. ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
  777. src_ring->write_index &= src_ring->nentries_mask;
  778. ath10k_pci_sleep(ar);
  779. src_ring->per_transfer_context = (void **)ptr;
  780. /*
  781. * Legacy platforms that do not support cache
  782. * coherent DMA are unsupported
  783. */
  784. src_ring->base_addr_owner_space_unaligned =
  785. pci_alloc_consistent(ar_pci->pdev,
  786. (nentries * sizeof(struct ce_desc) +
  787. CE_DESC_RING_ALIGN),
  788. &base_addr);
  789. if (!src_ring->base_addr_owner_space_unaligned) {
  790. kfree(ce_state->src_ring);
  791. ce_state->src_ring = NULL;
  792. return -ENOMEM;
  793. }
  794. src_ring->base_addr_ce_space_unaligned = base_addr;
  795. src_ring->base_addr_owner_space = PTR_ALIGN(
  796. src_ring->base_addr_owner_space_unaligned,
  797. CE_DESC_RING_ALIGN);
  798. src_ring->base_addr_ce_space = ALIGN(
  799. src_ring->base_addr_ce_space_unaligned,
  800. CE_DESC_RING_ALIGN);
  801. /*
  802. * Also allocate a shadow src ring in regular
  803. * mem to use for faster access.
  804. */
  805. src_ring->shadow_base_unaligned =
  806. kmalloc((nentries * sizeof(struct ce_desc) +
  807. CE_DESC_RING_ALIGN), GFP_KERNEL);
  808. if (!src_ring->shadow_base_unaligned) {
  809. pci_free_consistent(ar_pci->pdev,
  810. (nentries * sizeof(struct ce_desc) +
  811. CE_DESC_RING_ALIGN),
  812. src_ring->base_addr_owner_space,
  813. src_ring->base_addr_ce_space);
  814. kfree(ce_state->src_ring);
  815. ce_state->src_ring = NULL;
  816. return -ENOMEM;
  817. }
  818. src_ring->shadow_base = PTR_ALIGN(
  819. src_ring->shadow_base_unaligned,
  820. CE_DESC_RING_ALIGN);
  821. ath10k_pci_wake(ar);
  822. ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr,
  823. src_ring->base_addr_ce_space);
  824. ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
  825. ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
  826. ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
  827. ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
  828. ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);
  829. ath10k_pci_sleep(ar);
  830. return 0;
  831. }
  832. static int ath10k_ce_init_dest_ring(struct ath10k *ar,
  833. unsigned int ce_id,
  834. struct ce_state *ce_state,
  835. const struct ce_attr *attr)
  836. {
  837. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  838. struct ce_ring_state *dest_ring;
  839. unsigned int nentries = attr->dest_nentries;
  840. unsigned int ce_nbytes;
  841. u32 ctrl_addr = ath10k_ce_base_address(ce_id);
  842. dma_addr_t base_addr;
  843. char *ptr;
  844. nentries = roundup_pow_of_two(nentries);
  845. if (ce_state->dest_ring) {
  846. WARN_ON(ce_state->dest_ring->nentries != nentries);
  847. return 0;
  848. }
  849. ce_nbytes = sizeof(struct ce_ring_state) + (nentries * sizeof(void *));
  850. ptr = kzalloc(ce_nbytes, GFP_KERNEL);
  851. if (ptr == NULL)
  852. return -ENOMEM;
  853. ce_state->dest_ring = (struct ce_ring_state *)ptr;
  854. dest_ring = ce_state->dest_ring;
  855. ptr += sizeof(struct ce_ring_state);
  856. dest_ring->nentries = nentries;
  857. dest_ring->nentries_mask = nentries - 1;
  858. ath10k_pci_wake(ar);
  859. dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
  860. dest_ring->sw_index &= dest_ring->nentries_mask;
  861. dest_ring->write_index =
  862. ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
  863. dest_ring->write_index &= dest_ring->nentries_mask;
  864. ath10k_pci_sleep(ar);
  865. dest_ring->per_transfer_context = (void **)ptr;
  866. /*
  867. * Legacy platforms that do not support cache
  868. * coherent DMA are unsupported
  869. */
  870. dest_ring->base_addr_owner_space_unaligned =
  871. pci_alloc_consistent(ar_pci->pdev,
  872. (nentries * sizeof(struct ce_desc) +
  873. CE_DESC_RING_ALIGN),
  874. &base_addr);
  875. if (!dest_ring->base_addr_owner_space_unaligned) {
  876. kfree(ce_state->dest_ring);
  877. ce_state->dest_ring = NULL;
  878. return -ENOMEM;
  879. }
  880. dest_ring->base_addr_ce_space_unaligned = base_addr;
  881. /*
  882. * Correctly initialize memory to 0 to prevent garbage
  883. * data crashing system when download firmware
  884. */
  885. memset(dest_ring->base_addr_owner_space_unaligned, 0,
  886. nentries * sizeof(struct ce_desc) + CE_DESC_RING_ALIGN);
  887. dest_ring->base_addr_owner_space = PTR_ALIGN(
  888. dest_ring->base_addr_owner_space_unaligned,
  889. CE_DESC_RING_ALIGN);
  890. dest_ring->base_addr_ce_space = ALIGN(
  891. dest_ring->base_addr_ce_space_unaligned,
  892. CE_DESC_RING_ALIGN);
  893. ath10k_pci_wake(ar);
  894. ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr,
  895. dest_ring->base_addr_ce_space);
  896. ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
  897. ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
  898. ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
  899. ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);
  900. ath10k_pci_sleep(ar);
  901. return 0;
  902. }
  903. static struct ce_state *ath10k_ce_init_state(struct ath10k *ar,
  904. unsigned int ce_id,
  905. const struct ce_attr *attr)
  906. {
  907. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  908. struct ce_state *ce_state = NULL;
  909. u32 ctrl_addr = ath10k_ce_base_address(ce_id);
  910. spin_lock_bh(&ar_pci->ce_lock);
  911. if (!ar_pci->ce_id_to_state[ce_id]) {
  912. ce_state = kzalloc(sizeof(*ce_state), GFP_ATOMIC);
  913. if (ce_state == NULL) {
  914. spin_unlock_bh(&ar_pci->ce_lock);
  915. return NULL;
  916. }
  917. ar_pci->ce_id_to_state[ce_id] = ce_state;
  918. ce_state->ar = ar;
  919. ce_state->id = ce_id;
  920. ce_state->ctrl_addr = ctrl_addr;
  921. ce_state->state = CE_RUNNING;
  922. /* Save attribute flags */
  923. ce_state->attr_flags = attr->flags;
  924. ce_state->src_sz_max = attr->src_sz_max;
  925. }
  926. spin_unlock_bh(&ar_pci->ce_lock);
  927. return ce_state;
  928. }
  929. /*
  930. * Initialize a Copy Engine based on caller-supplied attributes.
  931. * This may be called once to initialize both source and destination
  932. * rings or it may be called twice for separate source and destination
  933. * initialization. It may be that only one side or the other is
  934. * initialized by software/firmware.
  935. */
  936. struct ce_state *ath10k_ce_init(struct ath10k *ar,
  937. unsigned int ce_id,
  938. const struct ce_attr *attr)
  939. {
  940. struct ce_state *ce_state;
  941. u32 ctrl_addr = ath10k_ce_base_address(ce_id);
  942. int ret;
  943. ce_state = ath10k_ce_init_state(ar, ce_id, attr);
  944. if (!ce_state) {
  945. ath10k_err("Failed to initialize CE state for ID: %d\n", ce_id);
  946. return NULL;
  947. }
  948. if (attr->src_nentries) {
  949. ret = ath10k_ce_init_src_ring(ar, ce_id, ce_state, attr);
  950. if (ret) {
  951. ath10k_err("Failed to initialize CE src ring for ID: %d (%d)\n",
  952. ce_id, ret);
  953. ath10k_ce_deinit(ce_state);
  954. return NULL;
  955. }
  956. }
  957. if (attr->dest_nentries) {
  958. ret = ath10k_ce_init_dest_ring(ar, ce_id, ce_state, attr);
  959. if (ret) {
  960. ath10k_err("Failed to initialize CE dest ring for ID: %d (%d)\n",
  961. ce_id, ret);
  962. ath10k_ce_deinit(ce_state);
  963. return NULL;
  964. }
  965. }
  966. /* Enable CE error interrupts */
  967. ath10k_pci_wake(ar);
  968. ath10k_ce_error_intr_enable(ar, ctrl_addr);
  969. ath10k_pci_sleep(ar);
  970. return ce_state;
  971. }
  972. void ath10k_ce_deinit(struct ce_state *ce_state)
  973. {
  974. unsigned int ce_id = ce_state->id;
  975. struct ath10k *ar = ce_state->ar;
  976. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  977. ce_state->state = CE_UNUSED;
  978. ar_pci->ce_id_to_state[ce_id] = NULL;
  979. if (ce_state->src_ring) {
  980. kfree(ce_state->src_ring->shadow_base_unaligned);
  981. pci_free_consistent(ar_pci->pdev,
  982. (ce_state->src_ring->nentries *
  983. sizeof(struct ce_desc) +
  984. CE_DESC_RING_ALIGN),
  985. ce_state->src_ring->base_addr_owner_space,
  986. ce_state->src_ring->base_addr_ce_space);
  987. kfree(ce_state->src_ring);
  988. }
  989. if (ce_state->dest_ring) {
  990. pci_free_consistent(ar_pci->pdev,
  991. (ce_state->dest_ring->nentries *
  992. sizeof(struct ce_desc) +
  993. CE_DESC_RING_ALIGN),
  994. ce_state->dest_ring->base_addr_owner_space,
  995. ce_state->dest_ring->base_addr_ce_space);
  996. kfree(ce_state->dest_ring);
  997. }
  998. kfree(ce_state);
  999. }