dxe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Copyright (c) 2013 Eugene Krasnikov <k.eugene.e@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* DXE - DMA transfer engine
  17. * we have 2 channels(High prio and Low prio) for TX and 2 channels for RX.
  18. * through low channels data packets are transfered
  19. * through high channels managment packets are transfered
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/interrupt.h>
  23. #include <linux/soc/qcom/smem_state.h>
  24. #include "wcn36xx.h"
  25. #include "txrx.h"
  26. void *wcn36xx_dxe_get_next_bd(struct wcn36xx *wcn, bool is_low)
  27. {
  28. struct wcn36xx_dxe_ch *ch = is_low ?
  29. &wcn->dxe_tx_l_ch :
  30. &wcn->dxe_tx_h_ch;
  31. return ch->head_blk_ctl->bd_cpu_addr;
  32. }
  33. static void wcn36xx_ccu_write_register(struct wcn36xx *wcn, int addr, int data)
  34. {
  35. wcn36xx_dbg(WCN36XX_DBG_DXE,
  36. "wcn36xx_ccu_write_register: addr=%x, data=%x\n",
  37. addr, data);
  38. writel(data, wcn->ccu_base + addr);
  39. }
  40. static void wcn36xx_dxe_write_register(struct wcn36xx *wcn, int addr, int data)
  41. {
  42. wcn36xx_dbg(WCN36XX_DBG_DXE,
  43. "wcn36xx_dxe_write_register: addr=%x, data=%x\n",
  44. addr, data);
  45. writel(data, wcn->dxe_base + addr);
  46. }
  47. static void wcn36xx_dxe_read_register(struct wcn36xx *wcn, int addr, int *data)
  48. {
  49. *data = readl(wcn->dxe_base + addr);
  50. wcn36xx_dbg(WCN36XX_DBG_DXE,
  51. "wcn36xx_dxe_read_register: addr=%x, data=%x\n",
  52. addr, *data);
  53. }
  54. static void wcn36xx_dxe_free_ctl_block(struct wcn36xx_dxe_ch *ch)
  55. {
  56. struct wcn36xx_dxe_ctl *ctl = ch->head_blk_ctl, *next;
  57. int i;
  58. for (i = 0; i < ch->desc_num && ctl; i++) {
  59. next = ctl->next;
  60. kfree(ctl);
  61. ctl = next;
  62. }
  63. }
  64. static int wcn36xx_dxe_allocate_ctl_block(struct wcn36xx_dxe_ch *ch)
  65. {
  66. struct wcn36xx_dxe_ctl *prev_ctl = NULL;
  67. struct wcn36xx_dxe_ctl *cur_ctl = NULL;
  68. int i;
  69. spin_lock_init(&ch->lock);
  70. for (i = 0; i < ch->desc_num; i++) {
  71. cur_ctl = kzalloc(sizeof(*cur_ctl), GFP_KERNEL);
  72. if (!cur_ctl)
  73. goto out_fail;
  74. spin_lock_init(&cur_ctl->skb_lock);
  75. cur_ctl->ctl_blk_order = i;
  76. if (i == 0) {
  77. ch->head_blk_ctl = cur_ctl;
  78. ch->tail_blk_ctl = cur_ctl;
  79. } else if (ch->desc_num - 1 == i) {
  80. prev_ctl->next = cur_ctl;
  81. cur_ctl->next = ch->head_blk_ctl;
  82. } else {
  83. prev_ctl->next = cur_ctl;
  84. }
  85. prev_ctl = cur_ctl;
  86. }
  87. return 0;
  88. out_fail:
  89. wcn36xx_dxe_free_ctl_block(ch);
  90. return -ENOMEM;
  91. }
  92. int wcn36xx_dxe_alloc_ctl_blks(struct wcn36xx *wcn)
  93. {
  94. int ret;
  95. wcn->dxe_tx_l_ch.ch_type = WCN36XX_DXE_CH_TX_L;
  96. wcn->dxe_tx_h_ch.ch_type = WCN36XX_DXE_CH_TX_H;
  97. wcn->dxe_rx_l_ch.ch_type = WCN36XX_DXE_CH_RX_L;
  98. wcn->dxe_rx_h_ch.ch_type = WCN36XX_DXE_CH_RX_H;
  99. wcn->dxe_tx_l_ch.desc_num = WCN36XX_DXE_CH_DESC_NUMB_TX_L;
  100. wcn->dxe_tx_h_ch.desc_num = WCN36XX_DXE_CH_DESC_NUMB_TX_H;
  101. wcn->dxe_rx_l_ch.desc_num = WCN36XX_DXE_CH_DESC_NUMB_RX_L;
  102. wcn->dxe_rx_h_ch.desc_num = WCN36XX_DXE_CH_DESC_NUMB_RX_H;
  103. wcn->dxe_tx_l_ch.dxe_wq = WCN36XX_DXE_WQ_TX_L;
  104. wcn->dxe_tx_h_ch.dxe_wq = WCN36XX_DXE_WQ_TX_H;
  105. wcn->dxe_tx_l_ch.ctrl_bd = WCN36XX_DXE_CTRL_TX_L_BD;
  106. wcn->dxe_tx_h_ch.ctrl_bd = WCN36XX_DXE_CTRL_TX_H_BD;
  107. wcn->dxe_tx_l_ch.ctrl_skb = WCN36XX_DXE_CTRL_TX_L_SKB;
  108. wcn->dxe_tx_h_ch.ctrl_skb = WCN36XX_DXE_CTRL_TX_H_SKB;
  109. wcn->dxe_tx_l_ch.reg_ctrl = WCN36XX_DXE_REG_CTL_TX_L;
  110. wcn->dxe_tx_h_ch.reg_ctrl = WCN36XX_DXE_REG_CTL_TX_H;
  111. wcn->dxe_tx_l_ch.def_ctrl = WCN36XX_DXE_CH_DEFAULT_CTL_TX_L;
  112. wcn->dxe_tx_h_ch.def_ctrl = WCN36XX_DXE_CH_DEFAULT_CTL_TX_H;
  113. /* DXE control block allocation */
  114. ret = wcn36xx_dxe_allocate_ctl_block(&wcn->dxe_tx_l_ch);
  115. if (ret)
  116. goto out_err;
  117. ret = wcn36xx_dxe_allocate_ctl_block(&wcn->dxe_tx_h_ch);
  118. if (ret)
  119. goto out_err;
  120. ret = wcn36xx_dxe_allocate_ctl_block(&wcn->dxe_rx_l_ch);
  121. if (ret)
  122. goto out_err;
  123. ret = wcn36xx_dxe_allocate_ctl_block(&wcn->dxe_rx_h_ch);
  124. if (ret)
  125. goto out_err;
  126. /* Initialize SMSM state Clear TX Enable RING EMPTY STATE */
  127. ret = qcom_smem_state_update_bits(wcn->tx_enable_state,
  128. WCN36XX_SMSM_WLAN_TX_ENABLE |
  129. WCN36XX_SMSM_WLAN_TX_RINGS_EMPTY,
  130. WCN36XX_SMSM_WLAN_TX_RINGS_EMPTY);
  131. if (ret)
  132. goto out_err;
  133. return 0;
  134. out_err:
  135. wcn36xx_err("Failed to allocate DXE control blocks\n");
  136. wcn36xx_dxe_free_ctl_blks(wcn);
  137. return -ENOMEM;
  138. }
  139. void wcn36xx_dxe_free_ctl_blks(struct wcn36xx *wcn)
  140. {
  141. wcn36xx_dxe_free_ctl_block(&wcn->dxe_tx_l_ch);
  142. wcn36xx_dxe_free_ctl_block(&wcn->dxe_tx_h_ch);
  143. wcn36xx_dxe_free_ctl_block(&wcn->dxe_rx_l_ch);
  144. wcn36xx_dxe_free_ctl_block(&wcn->dxe_rx_h_ch);
  145. }
  146. static int wcn36xx_dxe_init_descs(struct device *dev, struct wcn36xx_dxe_ch *wcn_ch)
  147. {
  148. struct wcn36xx_dxe_desc *cur_dxe = NULL;
  149. struct wcn36xx_dxe_desc *prev_dxe = NULL;
  150. struct wcn36xx_dxe_ctl *cur_ctl = NULL;
  151. size_t size;
  152. int i;
  153. size = wcn_ch->desc_num * sizeof(struct wcn36xx_dxe_desc);
  154. wcn_ch->cpu_addr = dma_alloc_coherent(dev, size, &wcn_ch->dma_addr,
  155. GFP_KERNEL);
  156. if (!wcn_ch->cpu_addr)
  157. return -ENOMEM;
  158. memset(wcn_ch->cpu_addr, 0, size);
  159. cur_dxe = (struct wcn36xx_dxe_desc *)wcn_ch->cpu_addr;
  160. cur_ctl = wcn_ch->head_blk_ctl;
  161. for (i = 0; i < wcn_ch->desc_num; i++) {
  162. cur_ctl->desc = cur_dxe;
  163. cur_ctl->desc_phy_addr = wcn_ch->dma_addr +
  164. i * sizeof(struct wcn36xx_dxe_desc);
  165. switch (wcn_ch->ch_type) {
  166. case WCN36XX_DXE_CH_TX_L:
  167. cur_dxe->ctrl = WCN36XX_DXE_CTRL_TX_L;
  168. cur_dxe->dst_addr_l = WCN36XX_DXE_WQ_TX_L;
  169. break;
  170. case WCN36XX_DXE_CH_TX_H:
  171. cur_dxe->ctrl = WCN36XX_DXE_CTRL_TX_H;
  172. cur_dxe->dst_addr_l = WCN36XX_DXE_WQ_TX_H;
  173. break;
  174. case WCN36XX_DXE_CH_RX_L:
  175. cur_dxe->ctrl = WCN36XX_DXE_CTRL_RX_L;
  176. cur_dxe->src_addr_l = WCN36XX_DXE_WQ_RX_L;
  177. break;
  178. case WCN36XX_DXE_CH_RX_H:
  179. cur_dxe->ctrl = WCN36XX_DXE_CTRL_RX_H;
  180. cur_dxe->src_addr_l = WCN36XX_DXE_WQ_RX_H;
  181. break;
  182. }
  183. if (0 == i) {
  184. cur_dxe->phy_next_l = 0;
  185. } else if ((0 < i) && (i < wcn_ch->desc_num - 1)) {
  186. prev_dxe->phy_next_l =
  187. cur_ctl->desc_phy_addr;
  188. } else if (i == (wcn_ch->desc_num - 1)) {
  189. prev_dxe->phy_next_l =
  190. cur_ctl->desc_phy_addr;
  191. cur_dxe->phy_next_l =
  192. wcn_ch->head_blk_ctl->desc_phy_addr;
  193. }
  194. cur_ctl = cur_ctl->next;
  195. prev_dxe = cur_dxe;
  196. cur_dxe++;
  197. }
  198. return 0;
  199. }
  200. static void wcn36xx_dxe_init_tx_bd(struct wcn36xx_dxe_ch *ch,
  201. struct wcn36xx_dxe_mem_pool *pool)
  202. {
  203. int i, chunk_size = pool->chunk_size;
  204. dma_addr_t bd_phy_addr = pool->phy_addr;
  205. void *bd_cpu_addr = pool->virt_addr;
  206. struct wcn36xx_dxe_ctl *cur = ch->head_blk_ctl;
  207. for (i = 0; i < ch->desc_num; i++) {
  208. /* Only every second dxe needs a bd pointer,
  209. the other will point to the skb data */
  210. if (!(i & 1)) {
  211. cur->bd_phy_addr = bd_phy_addr;
  212. cur->bd_cpu_addr = bd_cpu_addr;
  213. bd_phy_addr += chunk_size;
  214. bd_cpu_addr += chunk_size;
  215. } else {
  216. cur->bd_phy_addr = 0;
  217. cur->bd_cpu_addr = NULL;
  218. }
  219. cur = cur->next;
  220. }
  221. }
  222. static int wcn36xx_dxe_enable_ch_int(struct wcn36xx *wcn, u16 wcn_ch)
  223. {
  224. int reg_data = 0;
  225. wcn36xx_dxe_read_register(wcn,
  226. WCN36XX_DXE_INT_MASK_REG,
  227. &reg_data);
  228. reg_data |= wcn_ch;
  229. wcn36xx_dxe_write_register(wcn,
  230. WCN36XX_DXE_INT_MASK_REG,
  231. (int)reg_data);
  232. return 0;
  233. }
  234. static int wcn36xx_dxe_fill_skb(struct device *dev, struct wcn36xx_dxe_ctl *ctl)
  235. {
  236. struct wcn36xx_dxe_desc *dxe = ctl->desc;
  237. struct sk_buff *skb;
  238. skb = alloc_skb(WCN36XX_PKT_SIZE, GFP_ATOMIC);
  239. if (skb == NULL)
  240. return -ENOMEM;
  241. dxe->dst_addr_l = dma_map_single(dev,
  242. skb_tail_pointer(skb),
  243. WCN36XX_PKT_SIZE,
  244. DMA_FROM_DEVICE);
  245. ctl->skb = skb;
  246. return 0;
  247. }
  248. static int wcn36xx_dxe_ch_alloc_skb(struct wcn36xx *wcn,
  249. struct wcn36xx_dxe_ch *wcn_ch)
  250. {
  251. int i;
  252. struct wcn36xx_dxe_ctl *cur_ctl = NULL;
  253. cur_ctl = wcn_ch->head_blk_ctl;
  254. for (i = 0; i < wcn_ch->desc_num; i++) {
  255. wcn36xx_dxe_fill_skb(wcn->dev, cur_ctl);
  256. cur_ctl = cur_ctl->next;
  257. }
  258. return 0;
  259. }
  260. static void wcn36xx_dxe_ch_free_skbs(struct wcn36xx *wcn,
  261. struct wcn36xx_dxe_ch *wcn_ch)
  262. {
  263. struct wcn36xx_dxe_ctl *cur = wcn_ch->head_blk_ctl;
  264. int i;
  265. for (i = 0; i < wcn_ch->desc_num; i++) {
  266. kfree_skb(cur->skb);
  267. cur = cur->next;
  268. }
  269. }
  270. void wcn36xx_dxe_tx_ack_ind(struct wcn36xx *wcn, u32 status)
  271. {
  272. struct ieee80211_tx_info *info;
  273. struct sk_buff *skb;
  274. unsigned long flags;
  275. spin_lock_irqsave(&wcn->dxe_lock, flags);
  276. skb = wcn->tx_ack_skb;
  277. wcn->tx_ack_skb = NULL;
  278. spin_unlock_irqrestore(&wcn->dxe_lock, flags);
  279. if (!skb) {
  280. wcn36xx_warn("Spurious TX complete indication\n");
  281. return;
  282. }
  283. info = IEEE80211_SKB_CB(skb);
  284. if (status == 1)
  285. info->flags |= IEEE80211_TX_STAT_ACK;
  286. wcn36xx_dbg(WCN36XX_DBG_DXE, "dxe tx ack status: %d\n", status);
  287. ieee80211_tx_status_irqsafe(wcn->hw, skb);
  288. ieee80211_wake_queues(wcn->hw);
  289. }
  290. static void reap_tx_dxes(struct wcn36xx *wcn, struct wcn36xx_dxe_ch *ch)
  291. {
  292. struct wcn36xx_dxe_ctl *ctl;
  293. struct ieee80211_tx_info *info;
  294. unsigned long flags;
  295. /*
  296. * Make at least one loop of do-while because in case ring is
  297. * completely full head and tail are pointing to the same element
  298. * and while-do will not make any cycles.
  299. */
  300. spin_lock_irqsave(&ch->lock, flags);
  301. ctl = ch->tail_blk_ctl;
  302. do {
  303. if (ctl->desc->ctrl & WCN36XX_DXE_CTRL_VALID_MASK)
  304. break;
  305. if (ctl->skb) {
  306. dma_unmap_single(wcn->dev, ctl->desc->src_addr_l,
  307. ctl->skb->len, DMA_TO_DEVICE);
  308. info = IEEE80211_SKB_CB(ctl->skb);
  309. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)) {
  310. /* Keep frame until TX status comes */
  311. ieee80211_free_txskb(wcn->hw, ctl->skb);
  312. }
  313. spin_lock(&ctl->skb_lock);
  314. if (wcn->queues_stopped) {
  315. wcn->queues_stopped = false;
  316. ieee80211_wake_queues(wcn->hw);
  317. }
  318. spin_unlock(&ctl->skb_lock);
  319. ctl->skb = NULL;
  320. }
  321. ctl = ctl->next;
  322. } while (ctl != ch->head_blk_ctl &&
  323. !(ctl->desc->ctrl & WCN36XX_DXE_CTRL_VALID_MASK));
  324. ch->tail_blk_ctl = ctl;
  325. spin_unlock_irqrestore(&ch->lock, flags);
  326. }
  327. static irqreturn_t wcn36xx_irq_tx_complete(int irq, void *dev)
  328. {
  329. struct wcn36xx *wcn = (struct wcn36xx *)dev;
  330. int int_src, int_reason;
  331. wcn36xx_dxe_read_register(wcn, WCN36XX_DXE_INT_SRC_RAW_REG, &int_src);
  332. if (int_src & WCN36XX_INT_MASK_CHAN_TX_H) {
  333. wcn36xx_dxe_read_register(wcn,
  334. WCN36XX_DXE_CH_STATUS_REG_ADDR_TX_H,
  335. &int_reason);
  336. /* TODO: Check int_reason */
  337. wcn36xx_dxe_write_register(wcn,
  338. WCN36XX_DXE_0_INT_CLR,
  339. WCN36XX_INT_MASK_CHAN_TX_H);
  340. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_0_INT_ED_CLR,
  341. WCN36XX_INT_MASK_CHAN_TX_H);
  342. wcn36xx_dbg(WCN36XX_DBG_DXE, "dxe tx ready high\n");
  343. reap_tx_dxes(wcn, &wcn->dxe_tx_h_ch);
  344. }
  345. if (int_src & WCN36XX_INT_MASK_CHAN_TX_L) {
  346. wcn36xx_dxe_read_register(wcn,
  347. WCN36XX_DXE_CH_STATUS_REG_ADDR_TX_L,
  348. &int_reason);
  349. /* TODO: Check int_reason */
  350. wcn36xx_dxe_write_register(wcn,
  351. WCN36XX_DXE_0_INT_CLR,
  352. WCN36XX_INT_MASK_CHAN_TX_L);
  353. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_0_INT_ED_CLR,
  354. WCN36XX_INT_MASK_CHAN_TX_L);
  355. wcn36xx_dbg(WCN36XX_DBG_DXE, "dxe tx ready low\n");
  356. reap_tx_dxes(wcn, &wcn->dxe_tx_l_ch);
  357. }
  358. return IRQ_HANDLED;
  359. }
  360. static irqreturn_t wcn36xx_irq_rx_ready(int irq, void *dev)
  361. {
  362. struct wcn36xx *wcn = (struct wcn36xx *)dev;
  363. disable_irq_nosync(wcn->rx_irq);
  364. wcn36xx_dxe_rx_frame(wcn);
  365. enable_irq(wcn->rx_irq);
  366. return IRQ_HANDLED;
  367. }
  368. static int wcn36xx_dxe_request_irqs(struct wcn36xx *wcn)
  369. {
  370. int ret;
  371. ret = request_irq(wcn->tx_irq, wcn36xx_irq_tx_complete,
  372. IRQF_TRIGGER_HIGH, "wcn36xx_tx", wcn);
  373. if (ret) {
  374. wcn36xx_err("failed to alloc tx irq\n");
  375. goto out_err;
  376. }
  377. ret = request_irq(wcn->rx_irq, wcn36xx_irq_rx_ready, IRQF_TRIGGER_HIGH,
  378. "wcn36xx_rx", wcn);
  379. if (ret) {
  380. wcn36xx_err("failed to alloc rx irq\n");
  381. goto out_txirq;
  382. }
  383. enable_irq_wake(wcn->rx_irq);
  384. return 0;
  385. out_txirq:
  386. free_irq(wcn->tx_irq, wcn);
  387. out_err:
  388. return ret;
  389. }
  390. static int wcn36xx_rx_handle_packets(struct wcn36xx *wcn,
  391. struct wcn36xx_dxe_ch *ch)
  392. {
  393. struct wcn36xx_dxe_ctl *ctl = ch->head_blk_ctl;
  394. struct wcn36xx_dxe_desc *dxe = ctl->desc;
  395. dma_addr_t dma_addr;
  396. struct sk_buff *skb;
  397. int ret = 0, int_mask;
  398. u32 value;
  399. if (ch->ch_type == WCN36XX_DXE_CH_RX_L) {
  400. value = WCN36XX_DXE_CTRL_RX_L;
  401. int_mask = WCN36XX_DXE_INT_CH1_MASK;
  402. } else {
  403. value = WCN36XX_DXE_CTRL_RX_H;
  404. int_mask = WCN36XX_DXE_INT_CH3_MASK;
  405. }
  406. while (!(dxe->ctrl & WCN36XX_DXE_CTRL_VALID_MASK)) {
  407. skb = ctl->skb;
  408. dma_addr = dxe->dst_addr_l;
  409. ret = wcn36xx_dxe_fill_skb(wcn->dev, ctl);
  410. if (0 == ret) {
  411. /* new skb allocation ok. Use the new one and queue
  412. * the old one to network system.
  413. */
  414. dma_unmap_single(wcn->dev, dma_addr, WCN36XX_PKT_SIZE,
  415. DMA_FROM_DEVICE);
  416. wcn36xx_rx_skb(wcn, skb);
  417. } /* else keep old skb not submitted and use it for rx DMA */
  418. dxe->ctrl = value;
  419. ctl = ctl->next;
  420. dxe = ctl->desc;
  421. }
  422. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_ENCH_ADDR, int_mask);
  423. ch->head_blk_ctl = ctl;
  424. return 0;
  425. }
  426. void wcn36xx_dxe_rx_frame(struct wcn36xx *wcn)
  427. {
  428. int int_src;
  429. wcn36xx_dxe_read_register(wcn, WCN36XX_DXE_INT_SRC_RAW_REG, &int_src);
  430. /* RX_LOW_PRI */
  431. if (int_src & WCN36XX_DXE_INT_CH1_MASK) {
  432. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_0_INT_CLR,
  433. WCN36XX_DXE_INT_CH1_MASK);
  434. wcn36xx_rx_handle_packets(wcn, &(wcn->dxe_rx_l_ch));
  435. }
  436. /* RX_HIGH_PRI */
  437. if (int_src & WCN36XX_DXE_INT_CH3_MASK) {
  438. /* Clean up all the INT within this channel */
  439. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_0_INT_CLR,
  440. WCN36XX_DXE_INT_CH3_MASK);
  441. wcn36xx_rx_handle_packets(wcn, &(wcn->dxe_rx_h_ch));
  442. }
  443. if (!int_src)
  444. wcn36xx_warn("No DXE interrupt pending\n");
  445. }
  446. int wcn36xx_dxe_allocate_mem_pools(struct wcn36xx *wcn)
  447. {
  448. size_t s;
  449. void *cpu_addr;
  450. /* Allocate BD headers for MGMT frames */
  451. /* Where this come from ask QC */
  452. wcn->mgmt_mem_pool.chunk_size = WCN36XX_BD_CHUNK_SIZE +
  453. 16 - (WCN36XX_BD_CHUNK_SIZE % 8);
  454. s = wcn->mgmt_mem_pool.chunk_size * WCN36XX_DXE_CH_DESC_NUMB_TX_H;
  455. cpu_addr = dma_alloc_coherent(wcn->dev, s, &wcn->mgmt_mem_pool.phy_addr,
  456. GFP_KERNEL);
  457. if (!cpu_addr)
  458. goto out_err;
  459. wcn->mgmt_mem_pool.virt_addr = cpu_addr;
  460. memset(cpu_addr, 0, s);
  461. /* Allocate BD headers for DATA frames */
  462. /* Where this come from ask QC */
  463. wcn->data_mem_pool.chunk_size = WCN36XX_BD_CHUNK_SIZE +
  464. 16 - (WCN36XX_BD_CHUNK_SIZE % 8);
  465. s = wcn->data_mem_pool.chunk_size * WCN36XX_DXE_CH_DESC_NUMB_TX_L;
  466. cpu_addr = dma_alloc_coherent(wcn->dev, s, &wcn->data_mem_pool.phy_addr,
  467. GFP_KERNEL);
  468. if (!cpu_addr)
  469. goto out_err;
  470. wcn->data_mem_pool.virt_addr = cpu_addr;
  471. memset(cpu_addr, 0, s);
  472. return 0;
  473. out_err:
  474. wcn36xx_dxe_free_mem_pools(wcn);
  475. wcn36xx_err("Failed to allocate BD mempool\n");
  476. return -ENOMEM;
  477. }
  478. void wcn36xx_dxe_free_mem_pools(struct wcn36xx *wcn)
  479. {
  480. if (wcn->mgmt_mem_pool.virt_addr)
  481. dma_free_coherent(wcn->dev, wcn->mgmt_mem_pool.chunk_size *
  482. WCN36XX_DXE_CH_DESC_NUMB_TX_H,
  483. wcn->mgmt_mem_pool.virt_addr,
  484. wcn->mgmt_mem_pool.phy_addr);
  485. if (wcn->data_mem_pool.virt_addr) {
  486. dma_free_coherent(wcn->dev, wcn->data_mem_pool.chunk_size *
  487. WCN36XX_DXE_CH_DESC_NUMB_TX_L,
  488. wcn->data_mem_pool.virt_addr,
  489. wcn->data_mem_pool.phy_addr);
  490. }
  491. }
  492. int wcn36xx_dxe_tx_frame(struct wcn36xx *wcn,
  493. struct wcn36xx_vif *vif_priv,
  494. struct sk_buff *skb,
  495. bool is_low)
  496. {
  497. struct wcn36xx_dxe_ctl *ctl = NULL;
  498. struct wcn36xx_dxe_desc *desc = NULL;
  499. struct wcn36xx_dxe_ch *ch = NULL;
  500. unsigned long flags;
  501. int ret;
  502. ch = is_low ? &wcn->dxe_tx_l_ch : &wcn->dxe_tx_h_ch;
  503. spin_lock_irqsave(&ch->lock, flags);
  504. ctl = ch->head_blk_ctl;
  505. spin_lock(&ctl->next->skb_lock);
  506. /*
  507. * If skb is not null that means that we reached the tail of the ring
  508. * hence ring is full. Stop queues to let mac80211 back off until ring
  509. * has an empty slot again.
  510. */
  511. if (NULL != ctl->next->skb) {
  512. ieee80211_stop_queues(wcn->hw);
  513. wcn->queues_stopped = true;
  514. spin_unlock(&ctl->next->skb_lock);
  515. spin_unlock_irqrestore(&ch->lock, flags);
  516. return -EBUSY;
  517. }
  518. spin_unlock(&ctl->next->skb_lock);
  519. ctl->skb = NULL;
  520. desc = ctl->desc;
  521. /* Set source address of the BD we send */
  522. desc->src_addr_l = ctl->bd_phy_addr;
  523. desc->dst_addr_l = ch->dxe_wq;
  524. desc->fr_len = sizeof(struct wcn36xx_tx_bd);
  525. desc->ctrl = ch->ctrl_bd;
  526. wcn36xx_dbg(WCN36XX_DBG_DXE, "DXE TX\n");
  527. wcn36xx_dbg_dump(WCN36XX_DBG_DXE_DUMP, "DESC1 >>> ",
  528. (char *)desc, sizeof(*desc));
  529. wcn36xx_dbg_dump(WCN36XX_DBG_DXE_DUMP,
  530. "BD >>> ", (char *)ctl->bd_cpu_addr,
  531. sizeof(struct wcn36xx_tx_bd));
  532. /* Set source address of the SKB we send */
  533. ctl = ctl->next;
  534. ctl->skb = skb;
  535. desc = ctl->desc;
  536. if (ctl->bd_cpu_addr) {
  537. wcn36xx_err("bd_cpu_addr cannot be NULL for skb DXE\n");
  538. ret = -EINVAL;
  539. goto unlock;
  540. }
  541. desc->src_addr_l = dma_map_single(wcn->dev,
  542. ctl->skb->data,
  543. ctl->skb->len,
  544. DMA_TO_DEVICE);
  545. desc->dst_addr_l = ch->dxe_wq;
  546. desc->fr_len = ctl->skb->len;
  547. /* set dxe descriptor to VALID */
  548. desc->ctrl = ch->ctrl_skb;
  549. wcn36xx_dbg_dump(WCN36XX_DBG_DXE_DUMP, "DESC2 >>> ",
  550. (char *)desc, sizeof(*desc));
  551. wcn36xx_dbg_dump(WCN36XX_DBG_DXE_DUMP, "SKB >>> ",
  552. (char *)ctl->skb->data, ctl->skb->len);
  553. /* Move the head of the ring to the next empty descriptor */
  554. ch->head_blk_ctl = ctl->next;
  555. /*
  556. * When connected and trying to send data frame chip can be in sleep
  557. * mode and writing to the register will not wake up the chip. Instead
  558. * notify chip about new frame through SMSM bus.
  559. */
  560. if (is_low && vif_priv->pw_state == WCN36XX_BMPS) {
  561. qcom_smem_state_update_bits(wcn->tx_rings_empty_state,
  562. WCN36XX_SMSM_WLAN_TX_ENABLE,
  563. WCN36XX_SMSM_WLAN_TX_ENABLE);
  564. } else {
  565. /* indicate End Of Packet and generate interrupt on descriptor
  566. * done.
  567. */
  568. wcn36xx_dxe_write_register(wcn,
  569. ch->reg_ctrl, ch->def_ctrl);
  570. }
  571. ret = 0;
  572. unlock:
  573. spin_unlock_irqrestore(&ch->lock, flags);
  574. return ret;
  575. }
  576. int wcn36xx_dxe_init(struct wcn36xx *wcn)
  577. {
  578. int reg_data = 0, ret;
  579. reg_data = WCN36XX_DXE_REG_RESET;
  580. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_REG_CSR_RESET, reg_data);
  581. /* Select channels for rx avail and xfer done interrupts... */
  582. reg_data = (WCN36XX_DXE_INT_CH3_MASK | WCN36XX_DXE_INT_CH1_MASK) << 16 |
  583. WCN36XX_DXE_INT_CH0_MASK | WCN36XX_DXE_INT_CH4_MASK;
  584. if (wcn->is_pronto)
  585. wcn36xx_ccu_write_register(wcn, WCN36XX_CCU_DXE_INT_SELECT_PRONTO, reg_data);
  586. else
  587. wcn36xx_ccu_write_register(wcn, WCN36XX_CCU_DXE_INT_SELECT_RIVA, reg_data);
  588. /***************************************/
  589. /* Init descriptors for TX LOW channel */
  590. /***************************************/
  591. wcn36xx_dxe_init_descs(wcn->dev, &wcn->dxe_tx_l_ch);
  592. wcn36xx_dxe_init_tx_bd(&wcn->dxe_tx_l_ch, &wcn->data_mem_pool);
  593. /* Write channel head to a NEXT register */
  594. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_CH_NEXT_DESC_ADDR_TX_L,
  595. wcn->dxe_tx_l_ch.head_blk_ctl->desc_phy_addr);
  596. /* Program DMA destination addr for TX LOW */
  597. wcn36xx_dxe_write_register(wcn,
  598. WCN36XX_DXE_CH_DEST_ADDR_TX_L,
  599. WCN36XX_DXE_WQ_TX_L);
  600. wcn36xx_dxe_read_register(wcn, WCN36XX_DXE_REG_CH_EN, &reg_data);
  601. wcn36xx_dxe_enable_ch_int(wcn, WCN36XX_INT_MASK_CHAN_TX_L);
  602. /***************************************/
  603. /* Init descriptors for TX HIGH channel */
  604. /***************************************/
  605. wcn36xx_dxe_init_descs(wcn->dev, &wcn->dxe_tx_h_ch);
  606. wcn36xx_dxe_init_tx_bd(&wcn->dxe_tx_h_ch, &wcn->mgmt_mem_pool);
  607. /* Write channel head to a NEXT register */
  608. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_CH_NEXT_DESC_ADDR_TX_H,
  609. wcn->dxe_tx_h_ch.head_blk_ctl->desc_phy_addr);
  610. /* Program DMA destination addr for TX HIGH */
  611. wcn36xx_dxe_write_register(wcn,
  612. WCN36XX_DXE_CH_DEST_ADDR_TX_H,
  613. WCN36XX_DXE_WQ_TX_H);
  614. wcn36xx_dxe_read_register(wcn, WCN36XX_DXE_REG_CH_EN, &reg_data);
  615. /* Enable channel interrupts */
  616. wcn36xx_dxe_enable_ch_int(wcn, WCN36XX_INT_MASK_CHAN_TX_H);
  617. /***************************************/
  618. /* Init descriptors for RX LOW channel */
  619. /***************************************/
  620. wcn36xx_dxe_init_descs(wcn->dev, &wcn->dxe_rx_l_ch);
  621. /* For RX we need to preallocated buffers */
  622. wcn36xx_dxe_ch_alloc_skb(wcn, &wcn->dxe_rx_l_ch);
  623. /* Write channel head to a NEXT register */
  624. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_CH_NEXT_DESC_ADDR_RX_L,
  625. wcn->dxe_rx_l_ch.head_blk_ctl->desc_phy_addr);
  626. /* Write DMA source address */
  627. wcn36xx_dxe_write_register(wcn,
  628. WCN36XX_DXE_CH_SRC_ADDR_RX_L,
  629. WCN36XX_DXE_WQ_RX_L);
  630. /* Program preallocated destination address */
  631. wcn36xx_dxe_write_register(wcn,
  632. WCN36XX_DXE_CH_DEST_ADDR_RX_L,
  633. wcn->dxe_rx_l_ch.head_blk_ctl->desc->phy_next_l);
  634. /* Enable default control registers */
  635. wcn36xx_dxe_write_register(wcn,
  636. WCN36XX_DXE_REG_CTL_RX_L,
  637. WCN36XX_DXE_CH_DEFAULT_CTL_RX_L);
  638. /* Enable channel interrupts */
  639. wcn36xx_dxe_enable_ch_int(wcn, WCN36XX_INT_MASK_CHAN_RX_L);
  640. /***************************************/
  641. /* Init descriptors for RX HIGH channel */
  642. /***************************************/
  643. wcn36xx_dxe_init_descs(wcn->dev, &wcn->dxe_rx_h_ch);
  644. /* For RX we need to prealocat buffers */
  645. wcn36xx_dxe_ch_alloc_skb(wcn, &wcn->dxe_rx_h_ch);
  646. /* Write chanel head to a NEXT register */
  647. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_CH_NEXT_DESC_ADDR_RX_H,
  648. wcn->dxe_rx_h_ch.head_blk_ctl->desc_phy_addr);
  649. /* Write DMA source address */
  650. wcn36xx_dxe_write_register(wcn,
  651. WCN36XX_DXE_CH_SRC_ADDR_RX_H,
  652. WCN36XX_DXE_WQ_RX_H);
  653. /* Program preallocated destination address */
  654. wcn36xx_dxe_write_register(wcn,
  655. WCN36XX_DXE_CH_DEST_ADDR_RX_H,
  656. wcn->dxe_rx_h_ch.head_blk_ctl->desc->phy_next_l);
  657. /* Enable default control registers */
  658. wcn36xx_dxe_write_register(wcn,
  659. WCN36XX_DXE_REG_CTL_RX_H,
  660. WCN36XX_DXE_CH_DEFAULT_CTL_RX_H);
  661. /* Enable channel interrupts */
  662. wcn36xx_dxe_enable_ch_int(wcn, WCN36XX_INT_MASK_CHAN_RX_H);
  663. ret = wcn36xx_dxe_request_irqs(wcn);
  664. if (ret < 0)
  665. goto out_err;
  666. return 0;
  667. out_err:
  668. return ret;
  669. }
  670. void wcn36xx_dxe_deinit(struct wcn36xx *wcn)
  671. {
  672. free_irq(wcn->tx_irq, wcn);
  673. free_irq(wcn->rx_irq, wcn);
  674. if (wcn->tx_ack_skb) {
  675. ieee80211_tx_status_irqsafe(wcn->hw, wcn->tx_ack_skb);
  676. wcn->tx_ack_skb = NULL;
  677. }
  678. wcn36xx_dxe_ch_free_skbs(wcn, &wcn->dxe_rx_l_ch);
  679. wcn36xx_dxe_ch_free_skbs(wcn, &wcn->dxe_rx_h_ch);
  680. }