dxe.c 24 KB

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