ce.c 31 KB

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