ce.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. // SPDX-License-Identifier: BSD-3-Clause-Clear
  2. /*
  3. * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
  6. */
  7. #include <linux/export.h>
  8. #include "dp_rx.h"
  9. #include "debug.h"
  10. #include "hif.h"
  11. const struct ce_attr ath11k_host_ce_config_ipq8074[] = {
  12. /* CE0: host->target HTC control and raw streams */
  13. {
  14. .flags = CE_ATTR_FLAGS,
  15. .src_nentries = 16,
  16. .src_sz_max = 2048,
  17. .dest_nentries = 0,
  18. .send_cb = ath11k_htc_tx_completion_handler,
  19. },
  20. /* CE1: target->host HTT + HTC control */
  21. {
  22. .flags = CE_ATTR_FLAGS,
  23. .src_nentries = 0,
  24. .src_sz_max = 2048,
  25. .dest_nentries = 512,
  26. .recv_cb = ath11k_htc_rx_completion_handler,
  27. },
  28. /* CE2: target->host WMI */
  29. {
  30. .flags = CE_ATTR_FLAGS,
  31. .src_nentries = 0,
  32. .src_sz_max = 2048,
  33. .dest_nentries = 512,
  34. .recv_cb = ath11k_htc_rx_completion_handler,
  35. },
  36. /* CE3: host->target WMI (mac0) */
  37. {
  38. .flags = CE_ATTR_FLAGS,
  39. .src_nentries = 32,
  40. .src_sz_max = 2048,
  41. .dest_nentries = 0,
  42. .send_cb = ath11k_htc_tx_completion_handler,
  43. },
  44. /* CE4: host->target HTT */
  45. {
  46. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  47. .src_nentries = 2048,
  48. .src_sz_max = 256,
  49. .dest_nentries = 0,
  50. },
  51. /* CE5: target->host pktlog */
  52. {
  53. .flags = CE_ATTR_FLAGS,
  54. .src_nentries = 0,
  55. .src_sz_max = 2048,
  56. .dest_nentries = 512,
  57. .recv_cb = ath11k_dp_htt_htc_t2h_msg_handler,
  58. },
  59. /* CE6: target autonomous hif_memcpy */
  60. {
  61. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  62. .src_nentries = 0,
  63. .src_sz_max = 0,
  64. .dest_nentries = 0,
  65. },
  66. /* CE7: host->target WMI (mac1) */
  67. {
  68. .flags = CE_ATTR_FLAGS,
  69. .src_nentries = 32,
  70. .src_sz_max = 2048,
  71. .dest_nentries = 0,
  72. .send_cb = ath11k_htc_tx_completion_handler,
  73. },
  74. /* CE8: target autonomous hif_memcpy */
  75. {
  76. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  77. .src_nentries = 0,
  78. .src_sz_max = 0,
  79. .dest_nentries = 0,
  80. },
  81. /* CE9: host->target WMI (mac2) */
  82. {
  83. .flags = CE_ATTR_FLAGS,
  84. .src_nentries = 32,
  85. .src_sz_max = 2048,
  86. .dest_nentries = 0,
  87. .send_cb = ath11k_htc_tx_completion_handler,
  88. },
  89. /* CE10: target->host HTT */
  90. {
  91. .flags = CE_ATTR_FLAGS,
  92. .src_nentries = 0,
  93. .src_sz_max = 2048,
  94. .dest_nentries = 512,
  95. .recv_cb = ath11k_htc_rx_completion_handler,
  96. },
  97. /* CE11: Not used */
  98. {
  99. .flags = CE_ATTR_FLAGS,
  100. .src_nentries = 0,
  101. .src_sz_max = 0,
  102. .dest_nentries = 0,
  103. },
  104. };
  105. const struct ce_attr ath11k_host_ce_config_qca6390[] = {
  106. /* CE0: host->target HTC control and raw streams */
  107. {
  108. .flags = CE_ATTR_FLAGS,
  109. .src_nentries = 16,
  110. .src_sz_max = 2048,
  111. .dest_nentries = 0,
  112. },
  113. /* CE1: target->host HTT + HTC control */
  114. {
  115. .flags = CE_ATTR_FLAGS,
  116. .src_nentries = 0,
  117. .src_sz_max = 2048,
  118. .dest_nentries = 512,
  119. .recv_cb = ath11k_htc_rx_completion_handler,
  120. },
  121. /* CE2: target->host WMI */
  122. {
  123. .flags = CE_ATTR_FLAGS,
  124. .src_nentries = 0,
  125. .src_sz_max = 2048,
  126. .dest_nentries = 512,
  127. .recv_cb = ath11k_htc_rx_completion_handler,
  128. },
  129. /* CE3: host->target WMI (mac0) */
  130. {
  131. .flags = CE_ATTR_FLAGS,
  132. .src_nentries = 32,
  133. .src_sz_max = 2048,
  134. .dest_nentries = 0,
  135. .send_cb = ath11k_htc_tx_completion_handler,
  136. },
  137. /* CE4: host->target HTT */
  138. {
  139. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  140. .src_nentries = 2048,
  141. .src_sz_max = 256,
  142. .dest_nentries = 0,
  143. },
  144. /* CE5: target->host pktlog */
  145. {
  146. .flags = CE_ATTR_FLAGS,
  147. .src_nentries = 0,
  148. .src_sz_max = 2048,
  149. .dest_nentries = 512,
  150. .recv_cb = ath11k_dp_htt_htc_t2h_msg_handler,
  151. },
  152. /* CE6: target autonomous hif_memcpy */
  153. {
  154. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  155. .src_nentries = 0,
  156. .src_sz_max = 0,
  157. .dest_nentries = 0,
  158. },
  159. /* CE7: host->target WMI (mac1) */
  160. {
  161. .flags = CE_ATTR_FLAGS,
  162. .src_nentries = 32,
  163. .src_sz_max = 2048,
  164. .dest_nentries = 0,
  165. .send_cb = ath11k_htc_tx_completion_handler,
  166. },
  167. /* CE8: target autonomous hif_memcpy */
  168. {
  169. .flags = CE_ATTR_FLAGS,
  170. .src_nentries = 0,
  171. .src_sz_max = 0,
  172. .dest_nentries = 0,
  173. },
  174. };
  175. const struct ce_attr ath11k_host_ce_config_qcn9074[] = {
  176. /* CE0: host->target HTC control and raw streams */
  177. {
  178. .flags = CE_ATTR_FLAGS,
  179. .src_nentries = 16,
  180. .src_sz_max = 2048,
  181. .dest_nentries = 0,
  182. },
  183. /* CE1: target->host HTT + HTC control */
  184. {
  185. .flags = CE_ATTR_FLAGS,
  186. .src_nentries = 0,
  187. .src_sz_max = 2048,
  188. .dest_nentries = 512,
  189. .recv_cb = ath11k_htc_rx_completion_handler,
  190. },
  191. /* CE2: target->host WMI */
  192. {
  193. .flags = CE_ATTR_FLAGS,
  194. .src_nentries = 0,
  195. .src_sz_max = 2048,
  196. .dest_nentries = 32,
  197. .recv_cb = ath11k_htc_rx_completion_handler,
  198. },
  199. /* CE3: host->target WMI (mac0) */
  200. {
  201. .flags = CE_ATTR_FLAGS,
  202. .src_nentries = 32,
  203. .src_sz_max = 2048,
  204. .dest_nentries = 0,
  205. .send_cb = ath11k_htc_tx_completion_handler,
  206. },
  207. /* CE4: host->target HTT */
  208. {
  209. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  210. .src_nentries = 2048,
  211. .src_sz_max = 256,
  212. .dest_nentries = 0,
  213. },
  214. /* CE5: target->host pktlog */
  215. {
  216. .flags = CE_ATTR_FLAGS,
  217. .src_nentries = 0,
  218. .src_sz_max = 2048,
  219. .dest_nentries = 512,
  220. .recv_cb = ath11k_dp_htt_htc_t2h_msg_handler,
  221. },
  222. };
  223. static bool ath11k_ce_need_shadow_fix(int ce_id)
  224. {
  225. /* only ce4 needs shadow workaround */
  226. if (ce_id == 4)
  227. return true;
  228. return false;
  229. }
  230. void ath11k_ce_stop_shadow_timers(struct ath11k_base *ab)
  231. {
  232. int i;
  233. if (!ab->hw_params.supports_shadow_regs)
  234. return;
  235. for (i = 0; i < ab->hw_params.ce_count; i++)
  236. if (ath11k_ce_need_shadow_fix(i))
  237. ath11k_dp_shadow_stop_timer(ab, &ab->ce.hp_timer[i]);
  238. }
  239. static int ath11k_ce_rx_buf_enqueue_pipe(struct ath11k_ce_pipe *pipe,
  240. struct sk_buff *skb, dma_addr_t paddr)
  241. {
  242. struct ath11k_base *ab = pipe->ab;
  243. struct ath11k_ce_ring *ring = pipe->dest_ring;
  244. struct hal_srng *srng;
  245. unsigned int write_index;
  246. unsigned int nentries_mask = ring->nentries_mask;
  247. u32 *desc;
  248. int ret;
  249. lockdep_assert_held(&ab->ce.ce_lock);
  250. write_index = ring->write_index;
  251. srng = &ab->hal.srng_list[ring->hal_ring_id];
  252. spin_lock_bh(&srng->lock);
  253. ath11k_hal_srng_access_begin(ab, srng);
  254. if (unlikely(ath11k_hal_srng_src_num_free(ab, srng, false) < 1)) {
  255. ret = -ENOSPC;
  256. goto exit;
  257. }
  258. desc = ath11k_hal_srng_src_get_next_entry(ab, srng);
  259. if (!desc) {
  260. ret = -ENOSPC;
  261. goto exit;
  262. }
  263. ath11k_hal_ce_dst_set_desc(desc, paddr);
  264. ring->skb[write_index] = skb;
  265. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  266. ring->write_index = write_index;
  267. pipe->rx_buf_needed--;
  268. ret = 0;
  269. exit:
  270. ath11k_hal_srng_access_end(ab, srng);
  271. spin_unlock_bh(&srng->lock);
  272. return ret;
  273. }
  274. static int ath11k_ce_rx_post_pipe(struct ath11k_ce_pipe *pipe)
  275. {
  276. struct ath11k_base *ab = pipe->ab;
  277. struct sk_buff *skb;
  278. dma_addr_t paddr;
  279. int ret = 0;
  280. if (!(pipe->dest_ring || pipe->status_ring))
  281. return 0;
  282. spin_lock_bh(&ab->ce.ce_lock);
  283. while (pipe->rx_buf_needed) {
  284. skb = dev_alloc_skb(pipe->buf_sz);
  285. if (!skb) {
  286. ret = -ENOMEM;
  287. goto exit;
  288. }
  289. WARN_ON_ONCE(!IS_ALIGNED((unsigned long)skb->data, 4));
  290. paddr = dma_map_single(ab->dev, skb->data,
  291. skb->len + skb_tailroom(skb),
  292. DMA_FROM_DEVICE);
  293. if (unlikely(dma_mapping_error(ab->dev, paddr))) {
  294. ath11k_warn(ab, "failed to dma map ce rx buf\n");
  295. dev_kfree_skb_any(skb);
  296. ret = -EIO;
  297. goto exit;
  298. }
  299. ATH11K_SKB_RXCB(skb)->paddr = paddr;
  300. ret = ath11k_ce_rx_buf_enqueue_pipe(pipe, skb, paddr);
  301. if (ret) {
  302. ath11k_dbg(ab, ATH11K_DBG_CE, "failed to enqueue rx buf: %d\n",
  303. ret);
  304. dma_unmap_single(ab->dev, paddr,
  305. skb->len + skb_tailroom(skb),
  306. DMA_FROM_DEVICE);
  307. dev_kfree_skb_any(skb);
  308. goto exit;
  309. }
  310. }
  311. exit:
  312. spin_unlock_bh(&ab->ce.ce_lock);
  313. return ret;
  314. }
  315. static int ath11k_ce_completed_recv_next(struct ath11k_ce_pipe *pipe,
  316. struct sk_buff **skb, int *nbytes)
  317. {
  318. struct ath11k_base *ab = pipe->ab;
  319. struct hal_srng *srng;
  320. unsigned int sw_index;
  321. unsigned int nentries_mask;
  322. u32 *desc;
  323. int ret = 0;
  324. spin_lock_bh(&ab->ce.ce_lock);
  325. sw_index = pipe->dest_ring->sw_index;
  326. nentries_mask = pipe->dest_ring->nentries_mask;
  327. srng = &ab->hal.srng_list[pipe->status_ring->hal_ring_id];
  328. spin_lock_bh(&srng->lock);
  329. ath11k_hal_srng_access_begin(ab, srng);
  330. desc = ath11k_hal_srng_dst_get_next_entry(ab, srng);
  331. if (!desc) {
  332. ret = -EIO;
  333. goto err;
  334. }
  335. *nbytes = ath11k_hal_ce_dst_status_get_length(desc);
  336. *skb = pipe->dest_ring->skb[sw_index];
  337. pipe->dest_ring->skb[sw_index] = NULL;
  338. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  339. pipe->dest_ring->sw_index = sw_index;
  340. pipe->rx_buf_needed++;
  341. err:
  342. ath11k_hal_srng_access_end(ab, srng);
  343. spin_unlock_bh(&srng->lock);
  344. spin_unlock_bh(&ab->ce.ce_lock);
  345. return ret;
  346. }
  347. static void ath11k_ce_recv_process_cb(struct ath11k_ce_pipe *pipe)
  348. {
  349. struct ath11k_base *ab = pipe->ab;
  350. struct sk_buff *skb;
  351. struct sk_buff_head list;
  352. unsigned int nbytes, max_nbytes;
  353. int ret;
  354. __skb_queue_head_init(&list);
  355. while (ath11k_ce_completed_recv_next(pipe, &skb, &nbytes) == 0) {
  356. max_nbytes = skb->len + skb_tailroom(skb);
  357. dma_unmap_single(ab->dev, ATH11K_SKB_RXCB(skb)->paddr,
  358. max_nbytes, DMA_FROM_DEVICE);
  359. if (unlikely(max_nbytes < nbytes || nbytes == 0)) {
  360. ath11k_warn(ab, "unexpected rx length (nbytes %d, max %d)",
  361. nbytes, max_nbytes);
  362. dev_kfree_skb_any(skb);
  363. continue;
  364. }
  365. skb_put(skb, nbytes);
  366. __skb_queue_tail(&list, skb);
  367. }
  368. while ((skb = __skb_dequeue(&list))) {
  369. ath11k_dbg(ab, ATH11K_DBG_CE, "rx ce pipe %d len %d\n",
  370. pipe->pipe_num, skb->len);
  371. pipe->recv_cb(ab, skb);
  372. }
  373. ret = ath11k_ce_rx_post_pipe(pipe);
  374. if (ret && ret != -ENOSPC) {
  375. ath11k_warn(ab, "failed to post rx buf to pipe: %d err: %d\n",
  376. pipe->pipe_num, ret);
  377. mod_timer(&ab->rx_replenish_retry,
  378. jiffies + ATH11K_CE_RX_POST_RETRY_JIFFIES);
  379. }
  380. }
  381. static struct sk_buff *ath11k_ce_completed_send_next(struct ath11k_ce_pipe *pipe)
  382. {
  383. struct ath11k_base *ab = pipe->ab;
  384. struct hal_srng *srng;
  385. unsigned int sw_index;
  386. unsigned int nentries_mask;
  387. struct sk_buff *skb;
  388. u32 *desc;
  389. spin_lock_bh(&ab->ce.ce_lock);
  390. sw_index = pipe->src_ring->sw_index;
  391. nentries_mask = pipe->src_ring->nentries_mask;
  392. srng = &ab->hal.srng_list[pipe->src_ring->hal_ring_id];
  393. spin_lock_bh(&srng->lock);
  394. ath11k_hal_srng_access_begin(ab, srng);
  395. desc = ath11k_hal_srng_src_reap_next(ab, srng);
  396. if (!desc) {
  397. skb = ERR_PTR(-EIO);
  398. goto err_unlock;
  399. }
  400. skb = pipe->src_ring->skb[sw_index];
  401. pipe->src_ring->skb[sw_index] = NULL;
  402. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  403. pipe->src_ring->sw_index = sw_index;
  404. err_unlock:
  405. spin_unlock_bh(&srng->lock);
  406. spin_unlock_bh(&ab->ce.ce_lock);
  407. return skb;
  408. }
  409. static void ath11k_ce_tx_process_cb(struct ath11k_ce_pipe *pipe)
  410. {
  411. struct ath11k_base *ab = pipe->ab;
  412. struct sk_buff *skb;
  413. struct sk_buff_head list;
  414. __skb_queue_head_init(&list);
  415. while (!IS_ERR(skb = ath11k_ce_completed_send_next(pipe))) {
  416. if (!skb)
  417. continue;
  418. dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr, skb->len,
  419. DMA_TO_DEVICE);
  420. if ((!pipe->send_cb) || ab->hw_params.credit_flow) {
  421. dev_kfree_skb_any(skb);
  422. continue;
  423. }
  424. __skb_queue_tail(&list, skb);
  425. }
  426. while ((skb = __skb_dequeue(&list))) {
  427. ath11k_dbg(ab, ATH11K_DBG_CE, "tx ce pipe %d len %d\n",
  428. pipe->pipe_num, skb->len);
  429. pipe->send_cb(ab, skb);
  430. }
  431. }
  432. static void ath11k_ce_srng_msi_ring_params_setup(struct ath11k_base *ab, u32 ce_id,
  433. struct hal_srng_params *ring_params)
  434. {
  435. u32 msi_data_start;
  436. u32 msi_data_count, msi_data_idx;
  437. u32 msi_irq_start;
  438. u32 addr_lo;
  439. u32 addr_hi;
  440. int ret;
  441. ret = ath11k_get_user_msi_vector(ab, "CE",
  442. &msi_data_count, &msi_data_start,
  443. &msi_irq_start);
  444. if (ret)
  445. return;
  446. ath11k_get_msi_address(ab, &addr_lo, &addr_hi);
  447. ath11k_get_ce_msi_idx(ab, ce_id, &msi_data_idx);
  448. ring_params->msi_addr = addr_lo;
  449. ring_params->msi_addr |= (dma_addr_t)(((uint64_t)addr_hi) << 32);
  450. ring_params->msi_data = (msi_data_idx % msi_data_count) + msi_data_start;
  451. ring_params->flags |= HAL_SRNG_FLAGS_MSI_INTR;
  452. }
  453. static int ath11k_ce_init_ring(struct ath11k_base *ab,
  454. struct ath11k_ce_ring *ce_ring,
  455. int ce_id, enum hal_ring_type type)
  456. {
  457. struct hal_srng_params params = {};
  458. int ret;
  459. params.ring_base_paddr = ce_ring->base_addr_ce_space;
  460. params.ring_base_vaddr = ce_ring->base_addr_owner_space;
  461. params.num_entries = ce_ring->nentries;
  462. if (!(CE_ATTR_DIS_INTR & ab->hw_params.host_ce_config[ce_id].flags))
  463. ath11k_ce_srng_msi_ring_params_setup(ab, ce_id, &params);
  464. switch (type) {
  465. case HAL_CE_SRC:
  466. if (!(CE_ATTR_DIS_INTR & ab->hw_params.host_ce_config[ce_id].flags))
  467. params.intr_batch_cntr_thres_entries = 1;
  468. break;
  469. case HAL_CE_DST:
  470. params.max_buffer_len = ab->hw_params.host_ce_config[ce_id].src_sz_max;
  471. if (!(ab->hw_params.host_ce_config[ce_id].flags & CE_ATTR_DIS_INTR)) {
  472. params.intr_timer_thres_us = 1024;
  473. params.flags |= HAL_SRNG_FLAGS_LOW_THRESH_INTR_EN;
  474. params.low_threshold = ce_ring->nentries - 3;
  475. }
  476. break;
  477. case HAL_CE_DST_STATUS:
  478. if (!(ab->hw_params.host_ce_config[ce_id].flags & CE_ATTR_DIS_INTR)) {
  479. params.intr_batch_cntr_thres_entries = 1;
  480. params.intr_timer_thres_us = 0x1000;
  481. }
  482. break;
  483. default:
  484. ath11k_warn(ab, "Invalid CE ring type %d\n", type);
  485. return -EINVAL;
  486. }
  487. /* TODO: Init other params needed by HAL to init the ring */
  488. ret = ath11k_hal_srng_setup(ab, type, ce_id, 0, &params);
  489. if (ret < 0) {
  490. ath11k_warn(ab, "failed to setup srng: %d ring_id %d\n",
  491. ret, ce_id);
  492. return ret;
  493. }
  494. ce_ring->hal_ring_id = ret;
  495. if (ab->hw_params.supports_shadow_regs &&
  496. ath11k_ce_need_shadow_fix(ce_id))
  497. ath11k_dp_shadow_init_timer(ab, &ab->ce.hp_timer[ce_id],
  498. ATH11K_SHADOW_CTRL_TIMER_INTERVAL,
  499. ce_ring->hal_ring_id);
  500. return 0;
  501. }
  502. static struct ath11k_ce_ring *
  503. ath11k_ce_alloc_ring(struct ath11k_base *ab, int nentries, int desc_sz)
  504. {
  505. struct ath11k_ce_ring *ce_ring;
  506. dma_addr_t base_addr;
  507. ce_ring = kzalloc_flex(*ce_ring, skb, nentries);
  508. if (ce_ring == NULL)
  509. return ERR_PTR(-ENOMEM);
  510. ce_ring->nentries = nentries;
  511. ce_ring->nentries_mask = nentries - 1;
  512. /* Legacy platforms that do not support cache
  513. * coherent DMA are unsupported
  514. */
  515. ce_ring->base_addr_owner_space_unaligned =
  516. dma_alloc_coherent(ab->dev,
  517. nentries * desc_sz + CE_DESC_RING_ALIGN,
  518. &base_addr, GFP_KERNEL);
  519. if (!ce_ring->base_addr_owner_space_unaligned) {
  520. kfree(ce_ring);
  521. return ERR_PTR(-ENOMEM);
  522. }
  523. ce_ring->base_addr_ce_space_unaligned = base_addr;
  524. ce_ring->base_addr_owner_space = PTR_ALIGN(
  525. ce_ring->base_addr_owner_space_unaligned,
  526. CE_DESC_RING_ALIGN);
  527. ce_ring->base_addr_ce_space = ALIGN(
  528. ce_ring->base_addr_ce_space_unaligned,
  529. CE_DESC_RING_ALIGN);
  530. return ce_ring;
  531. }
  532. static int ath11k_ce_alloc_pipe(struct ath11k_base *ab, int ce_id)
  533. {
  534. struct ath11k_ce_pipe *pipe = &ab->ce.ce_pipe[ce_id];
  535. const struct ce_attr *attr = &ab->hw_params.host_ce_config[ce_id];
  536. struct ath11k_ce_ring *ring;
  537. int nentries;
  538. int desc_sz;
  539. pipe->attr_flags = attr->flags;
  540. if (attr->src_nentries) {
  541. pipe->send_cb = attr->send_cb;
  542. nentries = roundup_pow_of_two(attr->src_nentries);
  543. desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_SRC);
  544. ring = ath11k_ce_alloc_ring(ab, nentries, desc_sz);
  545. if (IS_ERR(ring))
  546. return PTR_ERR(ring);
  547. pipe->src_ring = ring;
  548. }
  549. if (attr->dest_nentries) {
  550. pipe->recv_cb = attr->recv_cb;
  551. nentries = roundup_pow_of_two(attr->dest_nentries);
  552. desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_DST);
  553. ring = ath11k_ce_alloc_ring(ab, nentries, desc_sz);
  554. if (IS_ERR(ring))
  555. return PTR_ERR(ring);
  556. pipe->dest_ring = ring;
  557. desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_DST_STATUS);
  558. ring = ath11k_ce_alloc_ring(ab, nentries, desc_sz);
  559. if (IS_ERR(ring))
  560. return PTR_ERR(ring);
  561. pipe->status_ring = ring;
  562. }
  563. return 0;
  564. }
  565. void ath11k_ce_per_engine_service(struct ath11k_base *ab, u16 ce_id)
  566. {
  567. struct ath11k_ce_pipe *pipe = &ab->ce.ce_pipe[ce_id];
  568. const struct ce_attr *attr = &ab->hw_params.host_ce_config[ce_id];
  569. if (attr->src_nentries)
  570. ath11k_ce_tx_process_cb(pipe);
  571. if (pipe->recv_cb)
  572. ath11k_ce_recv_process_cb(pipe);
  573. }
  574. void ath11k_ce_poll_send_completed(struct ath11k_base *ab, u8 pipe_id)
  575. {
  576. struct ath11k_ce_pipe *pipe = &ab->ce.ce_pipe[pipe_id];
  577. const struct ce_attr *attr = &ab->hw_params.host_ce_config[pipe_id];
  578. if ((pipe->attr_flags & CE_ATTR_DIS_INTR) && attr->src_nentries)
  579. ath11k_ce_tx_process_cb(pipe);
  580. }
  581. EXPORT_SYMBOL(ath11k_ce_per_engine_service);
  582. int ath11k_ce_send(struct ath11k_base *ab, struct sk_buff *skb, u8 pipe_id,
  583. u16 transfer_id)
  584. {
  585. struct ath11k_ce_pipe *pipe = &ab->ce.ce_pipe[pipe_id];
  586. struct hal_srng *srng;
  587. u32 *desc;
  588. unsigned int write_index, sw_index;
  589. unsigned int nentries_mask;
  590. int ret = 0;
  591. u8 byte_swap_data = 0;
  592. int num_used;
  593. /* Check if some entries could be regained by handling tx completion if
  594. * the CE has interrupts disabled and the used entries is more than the
  595. * defined usage threshold.
  596. */
  597. if (pipe->attr_flags & CE_ATTR_DIS_INTR) {
  598. spin_lock_bh(&ab->ce.ce_lock);
  599. write_index = pipe->src_ring->write_index;
  600. sw_index = pipe->src_ring->sw_index;
  601. if (write_index >= sw_index)
  602. num_used = write_index - sw_index;
  603. else
  604. num_used = pipe->src_ring->nentries - sw_index +
  605. write_index;
  606. spin_unlock_bh(&ab->ce.ce_lock);
  607. if (num_used > ATH11K_CE_USAGE_THRESHOLD)
  608. ath11k_ce_poll_send_completed(ab, pipe->pipe_num);
  609. }
  610. if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags))
  611. return -ESHUTDOWN;
  612. spin_lock_bh(&ab->ce.ce_lock);
  613. write_index = pipe->src_ring->write_index;
  614. nentries_mask = pipe->src_ring->nentries_mask;
  615. srng = &ab->hal.srng_list[pipe->src_ring->hal_ring_id];
  616. spin_lock_bh(&srng->lock);
  617. ath11k_hal_srng_access_begin(ab, srng);
  618. if (unlikely(ath11k_hal_srng_src_num_free(ab, srng, false) < 1)) {
  619. ath11k_hal_srng_access_end(ab, srng);
  620. ret = -ENOBUFS;
  621. goto err_unlock;
  622. }
  623. desc = ath11k_hal_srng_src_get_next_reaped(ab, srng);
  624. if (!desc) {
  625. ath11k_hal_srng_access_end(ab, srng);
  626. ret = -ENOBUFS;
  627. goto err_unlock;
  628. }
  629. if (pipe->attr_flags & CE_ATTR_BYTE_SWAP_DATA)
  630. byte_swap_data = 1;
  631. ath11k_hal_ce_src_set_desc(desc, ATH11K_SKB_CB(skb)->paddr,
  632. skb->len, transfer_id, byte_swap_data);
  633. pipe->src_ring->skb[write_index] = skb;
  634. pipe->src_ring->write_index = CE_RING_IDX_INCR(nentries_mask,
  635. write_index);
  636. ath11k_hal_srng_access_end(ab, srng);
  637. if (ath11k_ce_need_shadow_fix(pipe_id))
  638. ath11k_dp_shadow_start_timer(ab, srng, &ab->ce.hp_timer[pipe_id]);
  639. spin_unlock_bh(&srng->lock);
  640. spin_unlock_bh(&ab->ce.ce_lock);
  641. return 0;
  642. err_unlock:
  643. spin_unlock_bh(&srng->lock);
  644. spin_unlock_bh(&ab->ce.ce_lock);
  645. return ret;
  646. }
  647. static void ath11k_ce_rx_pipe_cleanup(struct ath11k_ce_pipe *pipe)
  648. {
  649. struct ath11k_base *ab = pipe->ab;
  650. struct ath11k_ce_ring *ring = pipe->dest_ring;
  651. struct sk_buff *skb;
  652. int i;
  653. if (!(ring && pipe->buf_sz))
  654. return;
  655. for (i = 0; i < ring->nentries; i++) {
  656. skb = ring->skb[i];
  657. if (!skb)
  658. continue;
  659. ring->skb[i] = NULL;
  660. dma_unmap_single(ab->dev, ATH11K_SKB_RXCB(skb)->paddr,
  661. skb->len + skb_tailroom(skb), DMA_FROM_DEVICE);
  662. dev_kfree_skb_any(skb);
  663. }
  664. }
  665. static void ath11k_ce_shadow_config(struct ath11k_base *ab)
  666. {
  667. int i;
  668. for (i = 0; i < ab->hw_params.ce_count; i++) {
  669. if (ab->hw_params.host_ce_config[i].src_nentries)
  670. ath11k_hal_srng_update_shadow_config(ab,
  671. HAL_CE_SRC, i);
  672. if (ab->hw_params.host_ce_config[i].dest_nentries) {
  673. ath11k_hal_srng_update_shadow_config(ab,
  674. HAL_CE_DST, i);
  675. ath11k_hal_srng_update_shadow_config(ab,
  676. HAL_CE_DST_STATUS, i);
  677. }
  678. }
  679. }
  680. void ath11k_ce_get_shadow_config(struct ath11k_base *ab,
  681. u32 **shadow_cfg, u32 *shadow_cfg_len)
  682. {
  683. if (!ab->hw_params.supports_shadow_regs)
  684. return;
  685. ath11k_hal_srng_get_shadow_config(ab, shadow_cfg, shadow_cfg_len);
  686. /* shadow is already configured */
  687. if (*shadow_cfg_len)
  688. return;
  689. /* shadow isn't configured yet, configure now.
  690. * non-CE srngs are configured firstly, then
  691. * all CE srngs.
  692. */
  693. ath11k_hal_srng_shadow_config(ab);
  694. ath11k_ce_shadow_config(ab);
  695. /* get the shadow configuration */
  696. ath11k_hal_srng_get_shadow_config(ab, shadow_cfg, shadow_cfg_len);
  697. }
  698. EXPORT_SYMBOL(ath11k_ce_get_shadow_config);
  699. void ath11k_ce_cleanup_pipes(struct ath11k_base *ab)
  700. {
  701. struct ath11k_ce_pipe *pipe;
  702. int pipe_num;
  703. ath11k_ce_stop_shadow_timers(ab);
  704. for (pipe_num = 0; pipe_num < ab->hw_params.ce_count; pipe_num++) {
  705. pipe = &ab->ce.ce_pipe[pipe_num];
  706. ath11k_ce_rx_pipe_cleanup(pipe);
  707. /* Cleanup any src CE's which have interrupts disabled */
  708. ath11k_ce_poll_send_completed(ab, pipe_num);
  709. /* NOTE: Should we also clean up tx buffer in all pipes? */
  710. }
  711. }
  712. EXPORT_SYMBOL(ath11k_ce_cleanup_pipes);
  713. void ath11k_ce_rx_post_buf(struct ath11k_base *ab)
  714. {
  715. struct ath11k_ce_pipe *pipe;
  716. int i;
  717. int ret;
  718. for (i = 0; i < ab->hw_params.ce_count; i++) {
  719. pipe = &ab->ce.ce_pipe[i];
  720. ret = ath11k_ce_rx_post_pipe(pipe);
  721. if (ret) {
  722. if (ret == -ENOSPC)
  723. continue;
  724. ath11k_warn(ab, "failed to post rx buf to pipe: %d err: %d\n",
  725. i, ret);
  726. mod_timer(&ab->rx_replenish_retry,
  727. jiffies + ATH11K_CE_RX_POST_RETRY_JIFFIES);
  728. return;
  729. }
  730. }
  731. }
  732. EXPORT_SYMBOL(ath11k_ce_rx_post_buf);
  733. void ath11k_ce_rx_replenish_retry(struct timer_list *t)
  734. {
  735. struct ath11k_base *ab = timer_container_of(ab, t, rx_replenish_retry);
  736. ath11k_ce_rx_post_buf(ab);
  737. }
  738. int ath11k_ce_init_pipes(struct ath11k_base *ab)
  739. {
  740. struct ath11k_ce_pipe *pipe;
  741. int i;
  742. int ret;
  743. for (i = 0; i < ab->hw_params.ce_count; i++) {
  744. pipe = &ab->ce.ce_pipe[i];
  745. if (pipe->src_ring) {
  746. ret = ath11k_ce_init_ring(ab, pipe->src_ring, i,
  747. HAL_CE_SRC);
  748. if (ret) {
  749. ath11k_warn(ab, "failed to init src ring: %d\n",
  750. ret);
  751. /* Should we clear any partial init */
  752. return ret;
  753. }
  754. pipe->src_ring->write_index = 0;
  755. pipe->src_ring->sw_index = 0;
  756. }
  757. if (pipe->dest_ring) {
  758. ret = ath11k_ce_init_ring(ab, pipe->dest_ring, i,
  759. HAL_CE_DST);
  760. if (ret) {
  761. ath11k_warn(ab, "failed to init dest ring: %d\n",
  762. ret);
  763. /* Should we clear any partial init */
  764. return ret;
  765. }
  766. pipe->rx_buf_needed = pipe->dest_ring->nentries ?
  767. pipe->dest_ring->nentries - 2 : 0;
  768. pipe->dest_ring->write_index = 0;
  769. pipe->dest_ring->sw_index = 0;
  770. }
  771. if (pipe->status_ring) {
  772. ret = ath11k_ce_init_ring(ab, pipe->status_ring, i,
  773. HAL_CE_DST_STATUS);
  774. if (ret) {
  775. ath11k_warn(ab, "failed to init dest status ing: %d\n",
  776. ret);
  777. /* Should we clear any partial init */
  778. return ret;
  779. }
  780. pipe->status_ring->write_index = 0;
  781. pipe->status_ring->sw_index = 0;
  782. }
  783. }
  784. return 0;
  785. }
  786. void ath11k_ce_free_pipes(struct ath11k_base *ab)
  787. {
  788. struct ath11k_ce_pipe *pipe;
  789. struct ath11k_ce_ring *ce_ring;
  790. int desc_sz;
  791. int i;
  792. for (i = 0; i < ab->hw_params.ce_count; i++) {
  793. pipe = &ab->ce.ce_pipe[i];
  794. if (ath11k_ce_need_shadow_fix(i))
  795. ath11k_dp_shadow_stop_timer(ab, &ab->ce.hp_timer[i]);
  796. if (pipe->src_ring) {
  797. desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_SRC);
  798. ce_ring = pipe->src_ring;
  799. dma_free_coherent(ab->dev,
  800. pipe->src_ring->nentries * desc_sz +
  801. CE_DESC_RING_ALIGN,
  802. ce_ring->base_addr_owner_space_unaligned,
  803. ce_ring->base_addr_ce_space_unaligned);
  804. kfree(pipe->src_ring);
  805. pipe->src_ring = NULL;
  806. }
  807. if (pipe->dest_ring) {
  808. desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_DST);
  809. ce_ring = pipe->dest_ring;
  810. dma_free_coherent(ab->dev,
  811. pipe->dest_ring->nentries * desc_sz +
  812. CE_DESC_RING_ALIGN,
  813. ce_ring->base_addr_owner_space_unaligned,
  814. ce_ring->base_addr_ce_space_unaligned);
  815. kfree(pipe->dest_ring);
  816. pipe->dest_ring = NULL;
  817. }
  818. if (pipe->status_ring) {
  819. desc_sz =
  820. ath11k_hal_ce_get_desc_size(HAL_CE_DESC_DST_STATUS);
  821. ce_ring = pipe->status_ring;
  822. dma_free_coherent(ab->dev,
  823. pipe->status_ring->nentries * desc_sz +
  824. CE_DESC_RING_ALIGN,
  825. ce_ring->base_addr_owner_space_unaligned,
  826. ce_ring->base_addr_ce_space_unaligned);
  827. kfree(pipe->status_ring);
  828. pipe->status_ring = NULL;
  829. }
  830. }
  831. }
  832. EXPORT_SYMBOL(ath11k_ce_free_pipes);
  833. int ath11k_ce_alloc_pipes(struct ath11k_base *ab)
  834. {
  835. struct ath11k_ce_pipe *pipe;
  836. int i;
  837. int ret;
  838. const struct ce_attr *attr;
  839. spin_lock_init(&ab->ce.ce_lock);
  840. for (i = 0; i < ab->hw_params.ce_count; i++) {
  841. attr = &ab->hw_params.host_ce_config[i];
  842. pipe = &ab->ce.ce_pipe[i];
  843. pipe->pipe_num = i;
  844. pipe->ab = ab;
  845. pipe->buf_sz = attr->src_sz_max;
  846. ret = ath11k_ce_alloc_pipe(ab, i);
  847. if (ret) {
  848. /* Free any partial successful allocation */
  849. ath11k_ce_free_pipes(ab);
  850. return ret;
  851. }
  852. }
  853. return 0;
  854. }
  855. EXPORT_SYMBOL(ath11k_ce_alloc_pipes);
  856. /* For Big Endian Host, Copy Engine byte_swap is enabled
  857. * When Copy Engine does byte_swap, need to byte swap again for the
  858. * Host to get/put buffer content in the correct byte order
  859. */
  860. void ath11k_ce_byte_swap(void *mem, u32 len)
  861. {
  862. int i;
  863. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) {
  864. if (!mem)
  865. return;
  866. for (i = 0; i < (len / 4); i++) {
  867. *(u32 *)mem = swab32(*(u32 *)mem);
  868. mem += 4;
  869. }
  870. }
  871. }
  872. int ath11k_ce_get_attr_flags(struct ath11k_base *ab, int ce_id)
  873. {
  874. if (ce_id >= ab->hw_params.ce_count)
  875. return -EINVAL;
  876. return ab->hw_params.host_ce_config[ce_id].flags;
  877. }
  878. EXPORT_SYMBOL(ath11k_ce_get_attr_flags);