dxe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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_dxe_write_register(struct wcn36xx *wcn, int addr, int data)
  33. {
  34. wcn36xx_dbg(WCN36XX_DBG_DXE,
  35. "wcn36xx_dxe_write_register: addr=%x, data=%x\n",
  36. addr, data);
  37. writel(data, wcn->mmio + addr);
  38. }
  39. #define wcn36xx_dxe_write_register_x(wcn, reg, reg_data) \
  40. do { \
  41. if (wcn->chip_version == WCN36XX_CHIP_3680) \
  42. wcn36xx_dxe_write_register(wcn, reg ## _3680, reg_data); \
  43. else \
  44. wcn36xx_dxe_write_register(wcn, reg ## _3660, reg_data); \
  45. } while (0) \
  46. static void wcn36xx_dxe_read_register(struct wcn36xx *wcn, int addr, int *data)
  47. {
  48. *data = readl(wcn->mmio + 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. while (!(dxe->ctrl & WCN36XX_DXE_CTRL_VALID_MASK)) {
  394. skb = ctl->skb;
  395. dma_addr = dxe->dst_addr_l;
  396. wcn36xx_dxe_fill_skb(wcn->dev, ctl);
  397. switch (ch->ch_type) {
  398. case WCN36XX_DXE_CH_RX_L:
  399. dxe->ctrl = WCN36XX_DXE_CTRL_RX_L;
  400. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_ENCH_ADDR,
  401. WCN36XX_DXE_INT_CH1_MASK);
  402. break;
  403. case WCN36XX_DXE_CH_RX_H:
  404. dxe->ctrl = WCN36XX_DXE_CTRL_RX_H;
  405. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_ENCH_ADDR,
  406. WCN36XX_DXE_INT_CH3_MASK);
  407. break;
  408. default:
  409. wcn36xx_warn("Unknown channel\n");
  410. }
  411. dma_unmap_single(wcn->dev, dma_addr, WCN36XX_PKT_SIZE,
  412. DMA_FROM_DEVICE);
  413. wcn36xx_rx_skb(wcn, skb);
  414. ctl = ctl->next;
  415. dxe = ctl->desc;
  416. }
  417. ch->head_blk_ctl = ctl;
  418. return 0;
  419. }
  420. void wcn36xx_dxe_rx_frame(struct wcn36xx *wcn)
  421. {
  422. int int_src;
  423. wcn36xx_dxe_read_register(wcn, WCN36XX_DXE_INT_SRC_RAW_REG, &int_src);
  424. /* RX_LOW_PRI */
  425. if (int_src & WCN36XX_DXE_INT_CH1_MASK) {
  426. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_0_INT_CLR,
  427. WCN36XX_DXE_INT_CH1_MASK);
  428. wcn36xx_rx_handle_packets(wcn, &(wcn->dxe_rx_l_ch));
  429. }
  430. /* RX_HIGH_PRI */
  431. if (int_src & WCN36XX_DXE_INT_CH3_MASK) {
  432. /* Clean up all the INT within this channel */
  433. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_0_INT_CLR,
  434. WCN36XX_DXE_INT_CH3_MASK);
  435. wcn36xx_rx_handle_packets(wcn, &(wcn->dxe_rx_h_ch));
  436. }
  437. if (!int_src)
  438. wcn36xx_warn("No DXE interrupt pending\n");
  439. }
  440. int wcn36xx_dxe_allocate_mem_pools(struct wcn36xx *wcn)
  441. {
  442. size_t s;
  443. void *cpu_addr;
  444. /* Allocate BD headers for MGMT frames */
  445. /* Where this come from ask QC */
  446. wcn->mgmt_mem_pool.chunk_size = WCN36XX_BD_CHUNK_SIZE +
  447. 16 - (WCN36XX_BD_CHUNK_SIZE % 8);
  448. s = wcn->mgmt_mem_pool.chunk_size * WCN36XX_DXE_CH_DESC_NUMB_TX_H;
  449. cpu_addr = dma_alloc_coherent(wcn->dev, s, &wcn->mgmt_mem_pool.phy_addr,
  450. GFP_KERNEL);
  451. if (!cpu_addr)
  452. goto out_err;
  453. wcn->mgmt_mem_pool.virt_addr = cpu_addr;
  454. memset(cpu_addr, 0, s);
  455. /* Allocate BD headers for DATA frames */
  456. /* Where this come from ask QC */
  457. wcn->data_mem_pool.chunk_size = WCN36XX_BD_CHUNK_SIZE +
  458. 16 - (WCN36XX_BD_CHUNK_SIZE % 8);
  459. s = wcn->data_mem_pool.chunk_size * WCN36XX_DXE_CH_DESC_NUMB_TX_L;
  460. cpu_addr = dma_alloc_coherent(wcn->dev, s, &wcn->data_mem_pool.phy_addr,
  461. GFP_KERNEL);
  462. if (!cpu_addr)
  463. goto out_err;
  464. wcn->data_mem_pool.virt_addr = cpu_addr;
  465. memset(cpu_addr, 0, s);
  466. return 0;
  467. out_err:
  468. wcn36xx_dxe_free_mem_pools(wcn);
  469. wcn36xx_err("Failed to allocate BD mempool\n");
  470. return -ENOMEM;
  471. }
  472. void wcn36xx_dxe_free_mem_pools(struct wcn36xx *wcn)
  473. {
  474. if (wcn->mgmt_mem_pool.virt_addr)
  475. dma_free_coherent(wcn->dev, wcn->mgmt_mem_pool.chunk_size *
  476. WCN36XX_DXE_CH_DESC_NUMB_TX_H,
  477. wcn->mgmt_mem_pool.virt_addr,
  478. wcn->mgmt_mem_pool.phy_addr);
  479. if (wcn->data_mem_pool.virt_addr) {
  480. dma_free_coherent(wcn->dev, wcn->data_mem_pool.chunk_size *
  481. WCN36XX_DXE_CH_DESC_NUMB_TX_L,
  482. wcn->data_mem_pool.virt_addr,
  483. wcn->data_mem_pool.phy_addr);
  484. }
  485. }
  486. int wcn36xx_dxe_tx_frame(struct wcn36xx *wcn,
  487. struct wcn36xx_vif *vif_priv,
  488. struct sk_buff *skb,
  489. bool is_low)
  490. {
  491. struct wcn36xx_dxe_ctl *ctl = NULL;
  492. struct wcn36xx_dxe_desc *desc = NULL;
  493. struct wcn36xx_dxe_ch *ch = NULL;
  494. unsigned long flags;
  495. int ret;
  496. ch = is_low ? &wcn->dxe_tx_l_ch : &wcn->dxe_tx_h_ch;
  497. spin_lock_irqsave(&ch->lock, flags);
  498. ctl = ch->head_blk_ctl;
  499. spin_lock(&ctl->next->skb_lock);
  500. /*
  501. * If skb is not null that means that we reached the tail of the ring
  502. * hence ring is full. Stop queues to let mac80211 back off until ring
  503. * has an empty slot again.
  504. */
  505. if (NULL != ctl->next->skb) {
  506. ieee80211_stop_queues(wcn->hw);
  507. wcn->queues_stopped = true;
  508. spin_unlock(&ctl->next->skb_lock);
  509. spin_unlock_irqrestore(&ch->lock, flags);
  510. return -EBUSY;
  511. }
  512. spin_unlock(&ctl->next->skb_lock);
  513. ctl->skb = NULL;
  514. desc = ctl->desc;
  515. /* Set source address of the BD we send */
  516. desc->src_addr_l = ctl->bd_phy_addr;
  517. desc->dst_addr_l = ch->dxe_wq;
  518. desc->fr_len = sizeof(struct wcn36xx_tx_bd);
  519. desc->ctrl = ch->ctrl_bd;
  520. wcn36xx_dbg(WCN36XX_DBG_DXE, "DXE TX\n");
  521. wcn36xx_dbg_dump(WCN36XX_DBG_DXE_DUMP, "DESC1 >>> ",
  522. (char *)desc, sizeof(*desc));
  523. wcn36xx_dbg_dump(WCN36XX_DBG_DXE_DUMP,
  524. "BD >>> ", (char *)ctl->bd_cpu_addr,
  525. sizeof(struct wcn36xx_tx_bd));
  526. /* Set source address of the SKB we send */
  527. ctl = ctl->next;
  528. ctl->skb = skb;
  529. desc = ctl->desc;
  530. if (ctl->bd_cpu_addr) {
  531. wcn36xx_err("bd_cpu_addr cannot be NULL for skb DXE\n");
  532. ret = -EINVAL;
  533. goto unlock;
  534. }
  535. desc->src_addr_l = dma_map_single(wcn->dev,
  536. ctl->skb->data,
  537. ctl->skb->len,
  538. DMA_TO_DEVICE);
  539. desc->dst_addr_l = ch->dxe_wq;
  540. desc->fr_len = ctl->skb->len;
  541. /* set dxe descriptor to VALID */
  542. desc->ctrl = ch->ctrl_skb;
  543. wcn36xx_dbg_dump(WCN36XX_DBG_DXE_DUMP, "DESC2 >>> ",
  544. (char *)desc, sizeof(*desc));
  545. wcn36xx_dbg_dump(WCN36XX_DBG_DXE_DUMP, "SKB >>> ",
  546. (char *)ctl->skb->data, ctl->skb->len);
  547. /* Move the head of the ring to the next empty descriptor */
  548. ch->head_blk_ctl = ctl->next;
  549. /*
  550. * When connected and trying to send data frame chip can be in sleep
  551. * mode and writing to the register will not wake up the chip. Instead
  552. * notify chip about new frame through SMSM bus.
  553. */
  554. if (is_low && vif_priv->pw_state == WCN36XX_BMPS) {
  555. wcn->ctrl_ops->smsm_change_state(
  556. 0,
  557. WCN36XX_SMSM_WLAN_TX_ENABLE);
  558. } else {
  559. /* indicate End Of Packet and generate interrupt on descriptor
  560. * done.
  561. */
  562. wcn36xx_dxe_write_register(wcn,
  563. ch->reg_ctrl, ch->def_ctrl);
  564. }
  565. ret = 0;
  566. unlock:
  567. spin_unlock_irqrestore(&ch->lock, flags);
  568. return ret;
  569. }
  570. int wcn36xx_dxe_init(struct wcn36xx *wcn)
  571. {
  572. int reg_data = 0, ret;
  573. reg_data = WCN36XX_DXE_REG_RESET;
  574. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_REG_CSR_RESET, reg_data);
  575. /* Setting interrupt path */
  576. reg_data = WCN36XX_DXE_CCU_INT;
  577. wcn36xx_dxe_write_register_x(wcn, WCN36XX_DXE_REG_CCU_INT, reg_data);
  578. /***************************************/
  579. /* Init descriptors for TX LOW channel */
  580. /***************************************/
  581. wcn36xx_dxe_init_descs(wcn->dev, &wcn->dxe_tx_l_ch);
  582. wcn36xx_dxe_init_tx_bd(&wcn->dxe_tx_l_ch, &wcn->data_mem_pool);
  583. /* Write channel head to a NEXT register */
  584. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_CH_NEXT_DESC_ADDR_TX_L,
  585. wcn->dxe_tx_l_ch.head_blk_ctl->desc_phy_addr);
  586. /* Program DMA destination addr for TX LOW */
  587. wcn36xx_dxe_write_register(wcn,
  588. WCN36XX_DXE_CH_DEST_ADDR_TX_L,
  589. WCN36XX_DXE_WQ_TX_L);
  590. wcn36xx_dxe_read_register(wcn, WCN36XX_DXE_REG_CH_EN, &reg_data);
  591. wcn36xx_dxe_enable_ch_int(wcn, WCN36XX_INT_MASK_CHAN_TX_L);
  592. /***************************************/
  593. /* Init descriptors for TX HIGH channel */
  594. /***************************************/
  595. wcn36xx_dxe_init_descs(wcn->dev, &wcn->dxe_tx_h_ch);
  596. wcn36xx_dxe_init_tx_bd(&wcn->dxe_tx_h_ch, &wcn->mgmt_mem_pool);
  597. /* Write channel head to a NEXT register */
  598. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_CH_NEXT_DESC_ADDR_TX_H,
  599. wcn->dxe_tx_h_ch.head_blk_ctl->desc_phy_addr);
  600. /* Program DMA destination addr for TX HIGH */
  601. wcn36xx_dxe_write_register(wcn,
  602. WCN36XX_DXE_CH_DEST_ADDR_TX_H,
  603. WCN36XX_DXE_WQ_TX_H);
  604. wcn36xx_dxe_read_register(wcn, WCN36XX_DXE_REG_CH_EN, &reg_data);
  605. /* Enable channel interrupts */
  606. wcn36xx_dxe_enable_ch_int(wcn, WCN36XX_INT_MASK_CHAN_TX_H);
  607. /***************************************/
  608. /* Init descriptors for RX LOW channel */
  609. /***************************************/
  610. wcn36xx_dxe_init_descs(wcn->dev, &wcn->dxe_rx_l_ch);
  611. /* For RX we need to preallocated buffers */
  612. wcn36xx_dxe_ch_alloc_skb(wcn, &wcn->dxe_rx_l_ch);
  613. /* Write channel head to a NEXT register */
  614. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_CH_NEXT_DESC_ADDR_RX_L,
  615. wcn->dxe_rx_l_ch.head_blk_ctl->desc_phy_addr);
  616. /* Write DMA source address */
  617. wcn36xx_dxe_write_register(wcn,
  618. WCN36XX_DXE_CH_SRC_ADDR_RX_L,
  619. WCN36XX_DXE_WQ_RX_L);
  620. /* Program preallocated destination address */
  621. wcn36xx_dxe_write_register(wcn,
  622. WCN36XX_DXE_CH_DEST_ADDR_RX_L,
  623. wcn->dxe_rx_l_ch.head_blk_ctl->desc->phy_next_l);
  624. /* Enable default control registers */
  625. wcn36xx_dxe_write_register(wcn,
  626. WCN36XX_DXE_REG_CTL_RX_L,
  627. WCN36XX_DXE_CH_DEFAULT_CTL_RX_L);
  628. /* Enable channel interrupts */
  629. wcn36xx_dxe_enable_ch_int(wcn, WCN36XX_INT_MASK_CHAN_RX_L);
  630. /***************************************/
  631. /* Init descriptors for RX HIGH channel */
  632. /***************************************/
  633. wcn36xx_dxe_init_descs(wcn->dev, &wcn->dxe_rx_h_ch);
  634. /* For RX we need to prealocat buffers */
  635. wcn36xx_dxe_ch_alloc_skb(wcn, &wcn->dxe_rx_h_ch);
  636. /* Write chanel head to a NEXT register */
  637. wcn36xx_dxe_write_register(wcn, WCN36XX_DXE_CH_NEXT_DESC_ADDR_RX_H,
  638. wcn->dxe_rx_h_ch.head_blk_ctl->desc_phy_addr);
  639. /* Write DMA source address */
  640. wcn36xx_dxe_write_register(wcn,
  641. WCN36XX_DXE_CH_SRC_ADDR_RX_H,
  642. WCN36XX_DXE_WQ_RX_H);
  643. /* Program preallocated destination address */
  644. wcn36xx_dxe_write_register(wcn,
  645. WCN36XX_DXE_CH_DEST_ADDR_RX_H,
  646. wcn->dxe_rx_h_ch.head_blk_ctl->desc->phy_next_l);
  647. /* Enable default control registers */
  648. wcn36xx_dxe_write_register(wcn,
  649. WCN36XX_DXE_REG_CTL_RX_H,
  650. WCN36XX_DXE_CH_DEFAULT_CTL_RX_H);
  651. /* Enable channel interrupts */
  652. wcn36xx_dxe_enable_ch_int(wcn, WCN36XX_INT_MASK_CHAN_RX_H);
  653. ret = wcn36xx_dxe_request_irqs(wcn);
  654. if (ret < 0)
  655. goto out_err;
  656. return 0;
  657. out_err:
  658. return ret;
  659. }
  660. void wcn36xx_dxe_deinit(struct wcn36xx *wcn)
  661. {
  662. free_irq(wcn->tx_irq, wcn);
  663. free_irq(wcn->rx_irq, wcn);
  664. if (wcn->tx_ack_skb) {
  665. ieee80211_tx_status_irqsafe(wcn->hw, wcn->tx_ack_skb);
  666. wcn->tx_ack_skb = NULL;
  667. }
  668. wcn36xx_dxe_ch_free_skbs(wcn, &wcn->dxe_rx_l_ch);
  669. wcn36xx_dxe_ch_free_skbs(wcn, &wcn->dxe_rx_h_ch);
  670. }