dxe.c 22 KB

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