ce.c 30 KB

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