ce.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. // SPDX-License-Identifier: BSD-3-Clause-Clear
  2. /*
  3. * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
  4. * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
  5. */
  6. #include "dp_rx.h"
  7. #include "debug.h"
  8. #include "hif.h"
  9. static int ath12k_ce_rx_buf_enqueue_pipe(struct ath12k_ce_pipe *pipe,
  10. struct sk_buff *skb, dma_addr_t paddr)
  11. {
  12. struct ath12k_base *ab = pipe->ab;
  13. struct ath12k_ce_ring *ring = pipe->dest_ring;
  14. struct hal_srng *srng;
  15. unsigned int write_index;
  16. unsigned int nentries_mask = ring->nentries_mask;
  17. struct hal_ce_srng_dest_desc *desc;
  18. int ret;
  19. lockdep_assert_held(&ab->ce.ce_lock);
  20. write_index = ring->write_index;
  21. srng = &ab->hal.srng_list[ring->hal_ring_id];
  22. spin_lock_bh(&srng->lock);
  23. ath12k_hal_srng_access_begin(ab, srng);
  24. if (unlikely(ath12k_hal_srng_src_num_free(ab, srng, false) < 1)) {
  25. ret = -ENOSPC;
  26. goto exit;
  27. }
  28. desc = ath12k_hal_srng_src_get_next_entry(ab, srng);
  29. if (!desc) {
  30. ret = -ENOSPC;
  31. goto exit;
  32. }
  33. ath12k_hal_ce_dst_set_desc(&ab->hal, desc, paddr);
  34. ring->skb[write_index] = skb;
  35. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  36. ring->write_index = write_index;
  37. pipe->rx_buf_needed--;
  38. ret = 0;
  39. exit:
  40. ath12k_hal_srng_access_end(ab, srng);
  41. spin_unlock_bh(&srng->lock);
  42. return ret;
  43. }
  44. static int ath12k_ce_rx_post_pipe(struct ath12k_ce_pipe *pipe)
  45. {
  46. struct ath12k_base *ab = pipe->ab;
  47. struct sk_buff *skb;
  48. dma_addr_t paddr;
  49. int ret = 0;
  50. if (!(pipe->dest_ring || pipe->status_ring))
  51. return 0;
  52. spin_lock_bh(&ab->ce.ce_lock);
  53. while (pipe->rx_buf_needed) {
  54. skb = dev_alloc_skb(pipe->buf_sz);
  55. if (!skb) {
  56. ret = -ENOMEM;
  57. goto exit;
  58. }
  59. WARN_ON_ONCE(!IS_ALIGNED((unsigned long)skb->data, 4));
  60. paddr = dma_map_single(ab->dev, skb->data,
  61. skb->len + skb_tailroom(skb),
  62. DMA_FROM_DEVICE);
  63. if (unlikely(dma_mapping_error(ab->dev, paddr))) {
  64. ath12k_warn(ab, "failed to dma map ce rx buf\n");
  65. dev_kfree_skb_any(skb);
  66. ret = -EIO;
  67. goto exit;
  68. }
  69. ATH12K_SKB_RXCB(skb)->paddr = paddr;
  70. ret = ath12k_ce_rx_buf_enqueue_pipe(pipe, skb, paddr);
  71. if (ret) {
  72. ath12k_dbg(ab, ATH12K_DBG_CE, "failed to enqueue rx buf: %d\n",
  73. ret);
  74. dma_unmap_single(ab->dev, paddr,
  75. skb->len + skb_tailroom(skb),
  76. DMA_FROM_DEVICE);
  77. dev_kfree_skb_any(skb);
  78. goto exit;
  79. }
  80. }
  81. exit:
  82. spin_unlock_bh(&ab->ce.ce_lock);
  83. return ret;
  84. }
  85. static int ath12k_ce_completed_recv_next(struct ath12k_ce_pipe *pipe,
  86. struct sk_buff **skb, int *nbytes)
  87. {
  88. struct ath12k_base *ab = pipe->ab;
  89. struct hal_ce_srng_dst_status_desc *desc;
  90. struct hal_srng *srng;
  91. unsigned int sw_index;
  92. unsigned int nentries_mask;
  93. int ret = 0;
  94. spin_lock_bh(&ab->ce.ce_lock);
  95. sw_index = pipe->dest_ring->sw_index;
  96. nentries_mask = pipe->dest_ring->nentries_mask;
  97. srng = &ab->hal.srng_list[pipe->status_ring->hal_ring_id];
  98. spin_lock_bh(&srng->lock);
  99. ath12k_hal_srng_access_begin(ab, srng);
  100. desc = ath12k_hal_srng_dst_get_next_entry(ab, srng);
  101. if (!desc) {
  102. ret = -EIO;
  103. goto err;
  104. }
  105. *nbytes = ath12k_hal_ce_dst_status_get_length(&ab->hal, desc);
  106. *skb = pipe->dest_ring->skb[sw_index];
  107. pipe->dest_ring->skb[sw_index] = NULL;
  108. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  109. pipe->dest_ring->sw_index = sw_index;
  110. pipe->rx_buf_needed++;
  111. err:
  112. ath12k_hal_srng_access_end(ab, srng);
  113. spin_unlock_bh(&srng->lock);
  114. spin_unlock_bh(&ab->ce.ce_lock);
  115. return ret;
  116. }
  117. static void ath12k_ce_recv_process_cb(struct ath12k_ce_pipe *pipe)
  118. {
  119. struct ath12k_base *ab = pipe->ab;
  120. struct sk_buff *skb;
  121. struct sk_buff_head list;
  122. unsigned int nbytes, max_nbytes;
  123. int ret;
  124. __skb_queue_head_init(&list);
  125. while (ath12k_ce_completed_recv_next(pipe, &skb, &nbytes) == 0) {
  126. max_nbytes = skb->len + skb_tailroom(skb);
  127. dma_unmap_single(ab->dev, ATH12K_SKB_RXCB(skb)->paddr,
  128. max_nbytes, DMA_FROM_DEVICE);
  129. if (unlikely(max_nbytes < nbytes || nbytes == 0)) {
  130. ath12k_warn(ab, "unexpected rx length (nbytes %d, max %d)",
  131. nbytes, max_nbytes);
  132. dev_kfree_skb_any(skb);
  133. continue;
  134. }
  135. skb_put(skb, nbytes);
  136. __skb_queue_tail(&list, skb);
  137. }
  138. while ((skb = __skb_dequeue(&list))) {
  139. ath12k_dbg(ab, ATH12K_DBG_CE, "rx ce pipe %d len %d\n",
  140. pipe->pipe_num, skb->len);
  141. pipe->recv_cb(ab, skb);
  142. }
  143. ret = ath12k_ce_rx_post_pipe(pipe);
  144. if (ret && ret != -ENOSPC) {
  145. ath12k_warn(ab, "failed to post rx buf to pipe: %d err: %d\n",
  146. pipe->pipe_num, ret);
  147. mod_timer(&ab->rx_replenish_retry,
  148. jiffies + ATH12K_CE_RX_POST_RETRY_JIFFIES);
  149. }
  150. }
  151. static struct sk_buff *ath12k_ce_completed_send_next(struct ath12k_ce_pipe *pipe)
  152. {
  153. struct ath12k_base *ab = pipe->ab;
  154. struct hal_ce_srng_src_desc *desc;
  155. struct hal_srng *srng;
  156. unsigned int sw_index;
  157. unsigned int nentries_mask;
  158. struct sk_buff *skb;
  159. spin_lock_bh(&ab->ce.ce_lock);
  160. sw_index = pipe->src_ring->sw_index;
  161. nentries_mask = pipe->src_ring->nentries_mask;
  162. srng = &ab->hal.srng_list[pipe->src_ring->hal_ring_id];
  163. spin_lock_bh(&srng->lock);
  164. ath12k_hal_srng_access_begin(ab, srng);
  165. desc = ath12k_hal_srng_src_reap_next(ab, srng);
  166. if (!desc) {
  167. skb = ERR_PTR(-EIO);
  168. goto err_unlock;
  169. }
  170. skb = pipe->src_ring->skb[sw_index];
  171. pipe->src_ring->skb[sw_index] = NULL;
  172. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  173. pipe->src_ring->sw_index = sw_index;
  174. err_unlock:
  175. spin_unlock_bh(&srng->lock);
  176. spin_unlock_bh(&ab->ce.ce_lock);
  177. return skb;
  178. }
  179. static void ath12k_ce_send_done_cb(struct ath12k_ce_pipe *pipe)
  180. {
  181. struct ath12k_base *ab = pipe->ab;
  182. struct sk_buff *skb;
  183. while (!IS_ERR(skb = ath12k_ce_completed_send_next(pipe))) {
  184. if (!skb)
  185. continue;
  186. dma_unmap_single(ab->dev, ATH12K_SKB_CB(skb)->paddr, skb->len,
  187. DMA_TO_DEVICE);
  188. dev_kfree_skb_any(skb);
  189. }
  190. }
  191. static void ath12k_ce_srng_msi_ring_params_setup(struct ath12k_base *ab, u32 ce_id,
  192. struct hal_srng_params *ring_params)
  193. {
  194. u32 msi_data_start;
  195. u32 msi_data_count, msi_data_idx;
  196. u32 msi_irq_start;
  197. u32 addr_lo;
  198. u32 addr_hi;
  199. int ret;
  200. ret = ath12k_hif_get_user_msi_vector(ab, "CE",
  201. &msi_data_count, &msi_data_start,
  202. &msi_irq_start);
  203. if (ret)
  204. return;
  205. ath12k_hif_get_msi_address(ab, &addr_lo, &addr_hi);
  206. ath12k_hif_get_ce_msi_idx(ab, ce_id, &msi_data_idx);
  207. ring_params->msi_addr = addr_lo;
  208. ring_params->msi_addr |= (dma_addr_t)(((uint64_t)addr_hi) << 32);
  209. ring_params->msi_data = (msi_data_idx % msi_data_count) + msi_data_start;
  210. ring_params->flags |= HAL_SRNG_FLAGS_MSI_INTR;
  211. }
  212. static int ath12k_ce_init_ring(struct ath12k_base *ab,
  213. struct ath12k_ce_ring *ce_ring,
  214. int ce_id, enum hal_ring_type type)
  215. {
  216. struct hal_srng_params params = {};
  217. int ret;
  218. params.ring_base_paddr = ce_ring->base_addr_ce_space;
  219. params.ring_base_vaddr = ce_ring->base_addr_owner_space;
  220. params.num_entries = ce_ring->nentries;
  221. if (!(CE_ATTR_DIS_INTR & ab->hw_params->host_ce_config[ce_id].flags))
  222. ath12k_ce_srng_msi_ring_params_setup(ab, ce_id, &params);
  223. switch (type) {
  224. case HAL_CE_SRC:
  225. if (!(CE_ATTR_DIS_INTR & ab->hw_params->host_ce_config[ce_id].flags))
  226. params.intr_batch_cntr_thres_entries = 1;
  227. break;
  228. case HAL_CE_DST:
  229. params.max_buffer_len = ab->hw_params->host_ce_config[ce_id].src_sz_max;
  230. if (!(ab->hw_params->host_ce_config[ce_id].flags & CE_ATTR_DIS_INTR)) {
  231. params.intr_timer_thres_us = 1024;
  232. params.flags |= HAL_SRNG_FLAGS_LOW_THRESH_INTR_EN;
  233. params.low_threshold = ce_ring->nentries - 3;
  234. }
  235. break;
  236. case HAL_CE_DST_STATUS:
  237. if (!(ab->hw_params->host_ce_config[ce_id].flags & CE_ATTR_DIS_INTR)) {
  238. params.intr_batch_cntr_thres_entries = 1;
  239. params.intr_timer_thres_us = 0x1000;
  240. }
  241. break;
  242. default:
  243. ath12k_warn(ab, "Invalid CE ring type %d\n", type);
  244. return -EINVAL;
  245. }
  246. /* TODO: Init other params needed by HAL to init the ring */
  247. ret = ath12k_hal_srng_setup(ab, type, ce_id, 0, &params);
  248. if (ret < 0) {
  249. ath12k_warn(ab, "failed to setup srng: %d ring_id %d\n",
  250. ret, ce_id);
  251. return ret;
  252. }
  253. ce_ring->hal_ring_id = ret;
  254. return 0;
  255. }
  256. static struct ath12k_ce_ring *
  257. ath12k_ce_alloc_ring(struct ath12k_base *ab, int nentries, int desc_sz)
  258. {
  259. struct ath12k_ce_ring *ce_ring;
  260. dma_addr_t base_addr;
  261. ce_ring = kzalloc_flex(*ce_ring, skb, nentries);
  262. if (!ce_ring)
  263. return ERR_PTR(-ENOMEM);
  264. ce_ring->nentries = nentries;
  265. ce_ring->nentries_mask = nentries - 1;
  266. /* Legacy platforms that do not support cache
  267. * coherent DMA are unsupported
  268. */
  269. ce_ring->base_addr_owner_space_unaligned =
  270. dma_alloc_coherent(ab->dev,
  271. nentries * desc_sz + CE_DESC_RING_ALIGN,
  272. &base_addr, GFP_KERNEL);
  273. if (!ce_ring->base_addr_owner_space_unaligned) {
  274. kfree(ce_ring);
  275. return ERR_PTR(-ENOMEM);
  276. }
  277. ce_ring->base_addr_ce_space_unaligned = base_addr;
  278. ce_ring->base_addr_owner_space =
  279. PTR_ALIGN(ce_ring->base_addr_owner_space_unaligned,
  280. CE_DESC_RING_ALIGN);
  281. ce_ring->base_addr_ce_space = ALIGN(ce_ring->base_addr_ce_space_unaligned,
  282. CE_DESC_RING_ALIGN);
  283. return ce_ring;
  284. }
  285. static int ath12k_ce_alloc_pipe(struct ath12k_base *ab, int ce_id)
  286. {
  287. struct ath12k_hal *hal = &ab->hal;
  288. struct ath12k_ce_pipe *pipe = &ab->ce.ce_pipe[ce_id];
  289. const struct ce_attr *attr = &ab->hw_params->host_ce_config[ce_id];
  290. struct ath12k_ce_ring *ring;
  291. int nentries;
  292. int desc_sz;
  293. pipe->attr_flags = attr->flags;
  294. if (attr->src_nentries) {
  295. pipe->send_cb = ath12k_ce_send_done_cb;
  296. nentries = roundup_pow_of_two(attr->src_nentries);
  297. desc_sz = ath12k_hal_ce_get_desc_size(hal, HAL_CE_DESC_SRC);
  298. ring = ath12k_ce_alloc_ring(ab, nentries, desc_sz);
  299. if (IS_ERR(ring))
  300. return PTR_ERR(ring);
  301. pipe->src_ring = ring;
  302. }
  303. if (attr->dest_nentries) {
  304. pipe->recv_cb = attr->recv_cb;
  305. nentries = roundup_pow_of_two(attr->dest_nentries);
  306. desc_sz = ath12k_hal_ce_get_desc_size(hal, HAL_CE_DESC_DST);
  307. ring = ath12k_ce_alloc_ring(ab, nentries, desc_sz);
  308. if (IS_ERR(ring))
  309. return PTR_ERR(ring);
  310. pipe->dest_ring = ring;
  311. desc_sz = ath12k_hal_ce_get_desc_size(hal, HAL_CE_DESC_DST_STATUS);
  312. ring = ath12k_ce_alloc_ring(ab, nentries, desc_sz);
  313. if (IS_ERR(ring))
  314. return PTR_ERR(ring);
  315. pipe->status_ring = ring;
  316. }
  317. return 0;
  318. }
  319. void ath12k_ce_per_engine_service(struct ath12k_base *ab, u16 ce_id)
  320. {
  321. struct ath12k_ce_pipe *pipe = &ab->ce.ce_pipe[ce_id];
  322. if (pipe->send_cb)
  323. pipe->send_cb(pipe);
  324. if (pipe->recv_cb)
  325. ath12k_ce_recv_process_cb(pipe);
  326. }
  327. void ath12k_ce_poll_send_completed(struct ath12k_base *ab, u8 pipe_id)
  328. {
  329. struct ath12k_ce_pipe *pipe = &ab->ce.ce_pipe[pipe_id];
  330. if ((pipe->attr_flags & CE_ATTR_DIS_INTR) && pipe->send_cb)
  331. pipe->send_cb(pipe);
  332. }
  333. int ath12k_ce_send(struct ath12k_base *ab, struct sk_buff *skb, u8 pipe_id,
  334. u16 transfer_id)
  335. {
  336. struct ath12k_ce_pipe *pipe = &ab->ce.ce_pipe[pipe_id];
  337. struct hal_ce_srng_src_desc *desc;
  338. struct hal_srng *srng;
  339. unsigned int write_index, sw_index;
  340. unsigned int nentries_mask;
  341. int ret = 0;
  342. u8 byte_swap_data = 0;
  343. int num_used;
  344. /* Check if some entries could be regained by handling tx completion if
  345. * the CE has interrupts disabled and the used entries is more than the
  346. * defined usage threshold.
  347. */
  348. if (pipe->attr_flags & CE_ATTR_DIS_INTR) {
  349. spin_lock_bh(&ab->ce.ce_lock);
  350. write_index = pipe->src_ring->write_index;
  351. sw_index = pipe->src_ring->sw_index;
  352. if (write_index >= sw_index)
  353. num_used = write_index - sw_index;
  354. else
  355. num_used = pipe->src_ring->nentries - sw_index +
  356. write_index;
  357. spin_unlock_bh(&ab->ce.ce_lock);
  358. if (num_used > ATH12K_CE_USAGE_THRESHOLD)
  359. ath12k_ce_poll_send_completed(ab, pipe->pipe_num);
  360. }
  361. if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags))
  362. return -ESHUTDOWN;
  363. spin_lock_bh(&ab->ce.ce_lock);
  364. write_index = pipe->src_ring->write_index;
  365. nentries_mask = pipe->src_ring->nentries_mask;
  366. srng = &ab->hal.srng_list[pipe->src_ring->hal_ring_id];
  367. spin_lock_bh(&srng->lock);
  368. ath12k_hal_srng_access_begin(ab, srng);
  369. if (unlikely(ath12k_hal_srng_src_num_free(ab, srng, false) < 1)) {
  370. ath12k_hal_srng_access_end(ab, srng);
  371. ret = -ENOBUFS;
  372. goto unlock;
  373. }
  374. desc = ath12k_hal_srng_src_get_next_reaped(ab, srng);
  375. if (!desc) {
  376. ath12k_hal_srng_access_end(ab, srng);
  377. ret = -ENOBUFS;
  378. goto unlock;
  379. }
  380. if (pipe->attr_flags & CE_ATTR_BYTE_SWAP_DATA)
  381. byte_swap_data = 1;
  382. ath12k_hal_ce_src_set_desc(&ab->hal, desc, ATH12K_SKB_CB(skb)->paddr,
  383. skb->len, transfer_id, byte_swap_data);
  384. pipe->src_ring->skb[write_index] = skb;
  385. pipe->src_ring->write_index = CE_RING_IDX_INCR(nentries_mask,
  386. write_index);
  387. ath12k_hal_srng_access_end(ab, srng);
  388. unlock:
  389. spin_unlock_bh(&srng->lock);
  390. spin_unlock_bh(&ab->ce.ce_lock);
  391. return ret;
  392. }
  393. static void ath12k_ce_rx_pipe_cleanup(struct ath12k_ce_pipe *pipe)
  394. {
  395. struct ath12k_base *ab = pipe->ab;
  396. struct ath12k_ce_ring *ring = pipe->dest_ring;
  397. struct sk_buff *skb;
  398. int i;
  399. if (!(ring && pipe->buf_sz))
  400. return;
  401. for (i = 0; i < ring->nentries; i++) {
  402. skb = ring->skb[i];
  403. if (!skb)
  404. continue;
  405. ring->skb[i] = NULL;
  406. dma_unmap_single(ab->dev, ATH12K_SKB_RXCB(skb)->paddr,
  407. skb->len + skb_tailroom(skb), DMA_FROM_DEVICE);
  408. dev_kfree_skb_any(skb);
  409. }
  410. }
  411. void ath12k_ce_cleanup_pipes(struct ath12k_base *ab)
  412. {
  413. struct ath12k_ce_pipe *pipe;
  414. int pipe_num;
  415. for (pipe_num = 0; pipe_num < ab->hw_params->ce_count; pipe_num++) {
  416. pipe = &ab->ce.ce_pipe[pipe_num];
  417. ath12k_ce_rx_pipe_cleanup(pipe);
  418. /* Cleanup any src CE's which have interrupts disabled */
  419. ath12k_ce_poll_send_completed(ab, pipe_num);
  420. /* NOTE: Should we also clean up tx buffer in all pipes? */
  421. }
  422. }
  423. void ath12k_ce_rx_post_buf(struct ath12k_base *ab)
  424. {
  425. struct ath12k_ce_pipe *pipe;
  426. int i;
  427. int ret;
  428. for (i = 0; i < ab->hw_params->ce_count; i++) {
  429. pipe = &ab->ce.ce_pipe[i];
  430. ret = ath12k_ce_rx_post_pipe(pipe);
  431. if (ret) {
  432. if (ret == -ENOSPC)
  433. continue;
  434. ath12k_warn(ab, "failed to post rx buf to pipe: %d err: %d\n",
  435. i, ret);
  436. mod_timer(&ab->rx_replenish_retry,
  437. jiffies + ATH12K_CE_RX_POST_RETRY_JIFFIES);
  438. return;
  439. }
  440. }
  441. }
  442. void ath12k_ce_rx_replenish_retry(struct timer_list *t)
  443. {
  444. struct ath12k_base *ab = timer_container_of(ab, t, rx_replenish_retry);
  445. ath12k_ce_rx_post_buf(ab);
  446. }
  447. static void ath12k_ce_shadow_config(struct ath12k_base *ab)
  448. {
  449. int i;
  450. for (i = 0; i < ab->hw_params->ce_count; i++) {
  451. if (ab->hw_params->host_ce_config[i].src_nentries)
  452. ath12k_hal_srng_update_shadow_config(ab, HAL_CE_SRC, i);
  453. if (ab->hw_params->host_ce_config[i].dest_nentries) {
  454. ath12k_hal_srng_update_shadow_config(ab, HAL_CE_DST, i);
  455. ath12k_hal_srng_update_shadow_config(ab, HAL_CE_DST_STATUS, i);
  456. }
  457. }
  458. }
  459. void ath12k_ce_get_shadow_config(struct ath12k_base *ab,
  460. u32 **shadow_cfg, u32 *shadow_cfg_len)
  461. {
  462. if (!ab->hw_params->supports_shadow_regs)
  463. return;
  464. ath12k_hal_srng_get_shadow_config(ab, shadow_cfg, shadow_cfg_len);
  465. /* shadow is already configured */
  466. if (*shadow_cfg_len)
  467. return;
  468. /* shadow isn't configured yet, configure now.
  469. * non-CE srngs are configured firstly, then
  470. * all CE srngs.
  471. */
  472. ath12k_hal_srng_shadow_config(ab);
  473. ath12k_ce_shadow_config(ab);
  474. /* get the shadow configuration */
  475. ath12k_hal_srng_get_shadow_config(ab, shadow_cfg, shadow_cfg_len);
  476. }
  477. int ath12k_ce_init_pipes(struct ath12k_base *ab)
  478. {
  479. struct ath12k_ce_pipe *pipe;
  480. int i;
  481. int ret;
  482. ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3,
  483. &ab->qmi.ce_cfg.shadow_reg_v3_len);
  484. for (i = 0; i < ab->hw_params->ce_count; i++) {
  485. pipe = &ab->ce.ce_pipe[i];
  486. if (pipe->src_ring) {
  487. ret = ath12k_ce_init_ring(ab, pipe->src_ring, i,
  488. HAL_CE_SRC);
  489. if (ret) {
  490. ath12k_warn(ab, "failed to init src ring: %d\n",
  491. ret);
  492. /* Should we clear any partial init */
  493. return ret;
  494. }
  495. pipe->src_ring->write_index = 0;
  496. pipe->src_ring->sw_index = 0;
  497. }
  498. if (pipe->dest_ring) {
  499. ret = ath12k_ce_init_ring(ab, pipe->dest_ring, i,
  500. HAL_CE_DST);
  501. if (ret) {
  502. ath12k_warn(ab, "failed to init dest ring: %d\n",
  503. ret);
  504. /* Should we clear any partial init */
  505. return ret;
  506. }
  507. pipe->rx_buf_needed = pipe->dest_ring->nentries ?
  508. pipe->dest_ring->nentries - 2 : 0;
  509. pipe->dest_ring->write_index = 0;
  510. pipe->dest_ring->sw_index = 0;
  511. }
  512. if (pipe->status_ring) {
  513. ret = ath12k_ce_init_ring(ab, pipe->status_ring, i,
  514. HAL_CE_DST_STATUS);
  515. if (ret) {
  516. ath12k_warn(ab, "failed to init dest status ing: %d\n",
  517. ret);
  518. /* Should we clear any partial init */
  519. return ret;
  520. }
  521. pipe->status_ring->write_index = 0;
  522. pipe->status_ring->sw_index = 0;
  523. }
  524. }
  525. return 0;
  526. }
  527. void ath12k_ce_free_pipes(struct ath12k_base *ab)
  528. {
  529. struct ath12k_hal *hal = &ab->hal;
  530. struct ath12k_ce_pipe *pipe;
  531. int desc_sz;
  532. int i;
  533. for (i = 0; i < ab->hw_params->ce_count; i++) {
  534. pipe = &ab->ce.ce_pipe[i];
  535. if (pipe->src_ring) {
  536. desc_sz = ath12k_hal_ce_get_desc_size(hal,
  537. HAL_CE_DESC_SRC);
  538. dma_free_coherent(ab->dev,
  539. pipe->src_ring->nentries * desc_sz +
  540. CE_DESC_RING_ALIGN,
  541. pipe->src_ring->base_addr_owner_space_unaligned,
  542. pipe->src_ring->base_addr_ce_space_unaligned);
  543. kfree(pipe->src_ring);
  544. pipe->src_ring = NULL;
  545. }
  546. if (pipe->dest_ring) {
  547. desc_sz = ath12k_hal_ce_get_desc_size(hal,
  548. HAL_CE_DESC_DST);
  549. dma_free_coherent(ab->dev,
  550. pipe->dest_ring->nentries * desc_sz +
  551. CE_DESC_RING_ALIGN,
  552. pipe->dest_ring->base_addr_owner_space_unaligned,
  553. pipe->dest_ring->base_addr_ce_space_unaligned);
  554. kfree(pipe->dest_ring);
  555. pipe->dest_ring = NULL;
  556. }
  557. if (pipe->status_ring) {
  558. desc_sz =
  559. ath12k_hal_ce_get_desc_size(hal,
  560. HAL_CE_DESC_DST_STATUS);
  561. dma_free_coherent(ab->dev,
  562. pipe->status_ring->nentries * desc_sz +
  563. CE_DESC_RING_ALIGN,
  564. pipe->status_ring->base_addr_owner_space_unaligned,
  565. pipe->status_ring->base_addr_ce_space_unaligned);
  566. kfree(pipe->status_ring);
  567. pipe->status_ring = NULL;
  568. }
  569. }
  570. }
  571. int ath12k_ce_alloc_pipes(struct ath12k_base *ab)
  572. {
  573. struct ath12k_ce_pipe *pipe;
  574. int i;
  575. int ret;
  576. const struct ce_attr *attr;
  577. spin_lock_init(&ab->ce.ce_lock);
  578. for (i = 0; i < ab->hw_params->ce_count; i++) {
  579. attr = &ab->hw_params->host_ce_config[i];
  580. pipe = &ab->ce.ce_pipe[i];
  581. pipe->pipe_num = i;
  582. pipe->ab = ab;
  583. pipe->buf_sz = attr->src_sz_max;
  584. ret = ath12k_ce_alloc_pipe(ab, i);
  585. if (ret) {
  586. /* Free any partial successful allocation */
  587. ath12k_ce_free_pipes(ab);
  588. return ret;
  589. }
  590. }
  591. return 0;
  592. }
  593. int ath12k_ce_get_attr_flags(struct ath12k_base *ab, int ce_id)
  594. {
  595. if (ce_id >= ab->hw_params->ce_count)
  596. return -EINVAL;
  597. return ab->hw_params->host_ce_config[ce_id].flags;
  598. }