dma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "mt7601u.h"
  14. #include "dma.h"
  15. #include "usb.h"
  16. #include "trace.h"
  17. static int mt7601u_submit_rx_buf(struct mt7601u_dev *dev,
  18. struct mt7601u_dma_buf_rx *e, gfp_t gfp);
  19. static unsigned int ieee80211_get_hdrlen_from_buf(const u8 *data, unsigned len)
  20. {
  21. const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *)data;
  22. unsigned int hdrlen;
  23. if (unlikely(len < 10))
  24. return 0;
  25. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  26. if (unlikely(hdrlen > len))
  27. return 0;
  28. return hdrlen;
  29. }
  30. static struct sk_buff *
  31. mt7601u_rx_skb_from_seg(struct mt7601u_dev *dev, struct mt7601u_rxwi *rxwi,
  32. void *data, u32 seg_len, u32 truesize, struct page *p)
  33. {
  34. struct sk_buff *skb;
  35. u32 true_len, hdr_len = 0, copy, frag;
  36. skb = alloc_skb(p ? 128 : seg_len, GFP_ATOMIC);
  37. if (!skb)
  38. return NULL;
  39. true_len = mt76_mac_process_rx(dev, skb, data, rxwi);
  40. if (!true_len || true_len > seg_len)
  41. goto bad_frame;
  42. hdr_len = ieee80211_get_hdrlen_from_buf(data, true_len);
  43. if (!hdr_len)
  44. goto bad_frame;
  45. if (rxwi->rxinfo & cpu_to_le32(MT_RXINFO_L2PAD)) {
  46. memcpy(skb_put(skb, hdr_len), data, hdr_len);
  47. data += hdr_len + 2;
  48. true_len -= hdr_len;
  49. hdr_len = 0;
  50. }
  51. /* If not doing paged RX allocated skb will always have enough space */
  52. copy = (true_len <= skb_tailroom(skb)) ? true_len : hdr_len + 8;
  53. frag = true_len - copy;
  54. memcpy(skb_put(skb, copy), data, copy);
  55. data += copy;
  56. if (frag) {
  57. skb_add_rx_frag(skb, 0, p, data - page_address(p),
  58. frag, truesize);
  59. get_page(p);
  60. }
  61. return skb;
  62. bad_frame:
  63. dev_err_ratelimited(dev->dev, "Error: incorrect frame len:%u hdr:%u\n",
  64. true_len, hdr_len);
  65. dev_kfree_skb(skb);
  66. return NULL;
  67. }
  68. static void mt7601u_rx_process_seg(struct mt7601u_dev *dev, u8 *data,
  69. u32 seg_len, struct page *p)
  70. {
  71. struct sk_buff *skb;
  72. struct mt7601u_rxwi *rxwi;
  73. u32 fce_info, truesize = seg_len;
  74. /* DMA_INFO field at the beginning of the segment contains only some of
  75. * the information, we need to read the FCE descriptor from the end.
  76. */
  77. fce_info = get_unaligned_le32(data + seg_len - MT_FCE_INFO_LEN);
  78. seg_len -= MT_FCE_INFO_LEN;
  79. data += MT_DMA_HDR_LEN;
  80. seg_len -= MT_DMA_HDR_LEN;
  81. rxwi = (struct mt7601u_rxwi *) data;
  82. data += sizeof(struct mt7601u_rxwi);
  83. seg_len -= sizeof(struct mt7601u_rxwi);
  84. if (unlikely(rxwi->zero[0] || rxwi->zero[1] || rxwi->zero[2]))
  85. dev_err_once(dev->dev, "Error: RXWI zero fields are set\n");
  86. if (unlikely(MT76_GET(MT_RXD_INFO_TYPE, fce_info)))
  87. dev_err_once(dev->dev, "Error: RX path seen a non-pkt urb\n");
  88. trace_mt_rx(dev, rxwi, fce_info);
  89. skb = mt7601u_rx_skb_from_seg(dev, rxwi, data, seg_len, truesize, p);
  90. if (!skb)
  91. return;
  92. spin_lock(&dev->mac_lock);
  93. ieee80211_rx(dev->hw, skb);
  94. spin_unlock(&dev->mac_lock);
  95. }
  96. static u16 mt7601u_rx_next_seg_len(u8 *data, u32 data_len)
  97. {
  98. u32 min_seg_len = MT_DMA_HDR_LEN + MT_RX_INFO_LEN +
  99. sizeof(struct mt7601u_rxwi) + MT_FCE_INFO_LEN;
  100. u16 dma_len = get_unaligned_le16(data);
  101. if (data_len < min_seg_len ||
  102. WARN_ON(!dma_len) ||
  103. WARN_ON(dma_len + MT_DMA_HDRS > data_len) ||
  104. WARN_ON(dma_len & 0x3))
  105. return 0;
  106. return MT_DMA_HDRS + dma_len;
  107. }
  108. static void
  109. mt7601u_rx_process_entry(struct mt7601u_dev *dev, struct mt7601u_dma_buf_rx *e)
  110. {
  111. u32 seg_len, data_len = e->urb->actual_length;
  112. u8 *data = page_address(e->p);
  113. struct page *new_p = NULL;
  114. int cnt = 0;
  115. if (!test_bit(MT7601U_STATE_INITIALIZED, &dev->state))
  116. return;
  117. /* Copy if there is very little data in the buffer. */
  118. if (data_len > 512)
  119. new_p = dev_alloc_pages(MT_RX_ORDER);
  120. while ((seg_len = mt7601u_rx_next_seg_len(data, data_len))) {
  121. mt7601u_rx_process_seg(dev, data, seg_len, new_p ? e->p : NULL);
  122. data_len -= seg_len;
  123. data += seg_len;
  124. cnt++;
  125. }
  126. if (cnt > 1)
  127. trace_mt_rx_dma_aggr(dev, cnt, !!new_p);
  128. if (new_p) {
  129. /* we have one extra ref from the allocator */
  130. __free_pages(e->p, MT_RX_ORDER);
  131. e->p = new_p;
  132. }
  133. }
  134. static struct mt7601u_dma_buf_rx *
  135. mt7601u_rx_get_pending_entry(struct mt7601u_dev *dev)
  136. {
  137. struct mt7601u_rx_queue *q = &dev->rx_q;
  138. struct mt7601u_dma_buf_rx *buf = NULL;
  139. unsigned long flags;
  140. spin_lock_irqsave(&dev->rx_lock, flags);
  141. if (!q->pending)
  142. goto out;
  143. buf = &q->e[q->start];
  144. q->pending--;
  145. q->start = (q->start + 1) % q->entries;
  146. out:
  147. spin_unlock_irqrestore(&dev->rx_lock, flags);
  148. return buf;
  149. }
  150. static void mt7601u_complete_rx(struct urb *urb)
  151. {
  152. struct mt7601u_dev *dev = urb->context;
  153. struct mt7601u_rx_queue *q = &dev->rx_q;
  154. unsigned long flags;
  155. spin_lock_irqsave(&dev->rx_lock, flags);
  156. if (mt7601u_urb_has_error(urb))
  157. dev_err(dev->dev, "Error: RX urb failed:%d\n", urb->status);
  158. if (WARN_ONCE(q->e[q->end].urb != urb, "RX urb mismatch"))
  159. goto out;
  160. q->end = (q->end + 1) % q->entries;
  161. q->pending++;
  162. tasklet_schedule(&dev->rx_tasklet);
  163. out:
  164. spin_unlock_irqrestore(&dev->rx_lock, flags);
  165. }
  166. static void mt7601u_rx_tasklet(unsigned long data)
  167. {
  168. struct mt7601u_dev *dev = (struct mt7601u_dev *) data;
  169. struct mt7601u_dma_buf_rx *e;
  170. while ((e = mt7601u_rx_get_pending_entry(dev))) {
  171. if (e->urb->status)
  172. continue;
  173. mt7601u_rx_process_entry(dev, e);
  174. mt7601u_submit_rx_buf(dev, e, GFP_ATOMIC);
  175. }
  176. }
  177. static void mt7601u_complete_tx(struct urb *urb)
  178. {
  179. struct mt7601u_tx_queue *q = urb->context;
  180. struct mt7601u_dev *dev = q->dev;
  181. struct sk_buff *skb;
  182. unsigned long flags;
  183. spin_lock_irqsave(&dev->tx_lock, flags);
  184. if (mt7601u_urb_has_error(urb))
  185. dev_err(dev->dev, "Error: TX urb failed:%d\n", urb->status);
  186. if (WARN_ONCE(q->e[q->start].urb != urb, "TX urb mismatch"))
  187. goto out;
  188. skb = q->e[q->start].skb;
  189. trace_mt_tx_dma_done(dev, skb);
  190. __skb_queue_tail(&dev->tx_skb_done, skb);
  191. tasklet_schedule(&dev->tx_tasklet);
  192. if (q->used == q->entries - q->entries / 8)
  193. ieee80211_wake_queue(dev->hw, skb_get_queue_mapping(skb));
  194. q->start = (q->start + 1) % q->entries;
  195. q->used--;
  196. out:
  197. spin_unlock_irqrestore(&dev->tx_lock, flags);
  198. }
  199. static void mt7601u_tx_tasklet(unsigned long data)
  200. {
  201. struct mt7601u_dev *dev = (struct mt7601u_dev *) data;
  202. struct sk_buff_head skbs;
  203. unsigned long flags;
  204. __skb_queue_head_init(&skbs);
  205. spin_lock_irqsave(&dev->tx_lock, flags);
  206. set_bit(MT7601U_STATE_MORE_STATS, &dev->state);
  207. if (!test_and_set_bit(MT7601U_STATE_READING_STATS, &dev->state))
  208. queue_delayed_work(dev->stat_wq, &dev->stat_work,
  209. msecs_to_jiffies(10));
  210. skb_queue_splice_init(&dev->tx_skb_done, &skbs);
  211. spin_unlock_irqrestore(&dev->tx_lock, flags);
  212. while (!skb_queue_empty(&skbs)) {
  213. struct sk_buff *skb = __skb_dequeue(&skbs);
  214. mt7601u_tx_status(dev, skb);
  215. }
  216. }
  217. static int mt7601u_dma_submit_tx(struct mt7601u_dev *dev,
  218. struct sk_buff *skb, u8 ep)
  219. {
  220. struct usb_device *usb_dev = mt7601u_to_usb_dev(dev);
  221. unsigned snd_pipe = usb_sndbulkpipe(usb_dev, dev->out_eps[ep]);
  222. struct mt7601u_dma_buf_tx *e;
  223. struct mt7601u_tx_queue *q = &dev->tx_q[ep];
  224. unsigned long flags;
  225. int ret;
  226. spin_lock_irqsave(&dev->tx_lock, flags);
  227. if (WARN_ON(q->entries <= q->used)) {
  228. ret = -ENOSPC;
  229. goto out;
  230. }
  231. e = &q->e[q->end];
  232. e->skb = skb;
  233. usb_fill_bulk_urb(e->urb, usb_dev, snd_pipe, skb->data, skb->len,
  234. mt7601u_complete_tx, q);
  235. ret = usb_submit_urb(e->urb, GFP_ATOMIC);
  236. if (ret) {
  237. /* Special-handle ENODEV from TX urb submission because it will
  238. * often be the first ENODEV we see after device is removed.
  239. */
  240. if (ret == -ENODEV)
  241. set_bit(MT7601U_STATE_REMOVED, &dev->state);
  242. else
  243. dev_err(dev->dev, "Error: TX urb submit failed:%d\n",
  244. ret);
  245. goto out;
  246. }
  247. q->end = (q->end + 1) % q->entries;
  248. q->used++;
  249. if (q->used >= q->entries)
  250. ieee80211_stop_queue(dev->hw, skb_get_queue_mapping(skb));
  251. out:
  252. spin_unlock_irqrestore(&dev->tx_lock, flags);
  253. return ret;
  254. }
  255. /* Map hardware Q to USB endpoint number */
  256. static u8 q2ep(u8 qid)
  257. {
  258. /* TODO: take management packets to queue 5 */
  259. return qid + 1;
  260. }
  261. /* Map USB endpoint number to Q id in the DMA engine */
  262. static enum mt76_qsel ep2dmaq(u8 ep)
  263. {
  264. if (ep == 5)
  265. return MT_QSEL_MGMT;
  266. return MT_QSEL_EDCA;
  267. }
  268. int mt7601u_dma_enqueue_tx(struct mt7601u_dev *dev, struct sk_buff *skb,
  269. struct mt76_wcid *wcid, int hw_q)
  270. {
  271. u8 ep = q2ep(hw_q);
  272. u32 dma_flags;
  273. int ret;
  274. dma_flags = MT_TXD_PKT_INFO_80211;
  275. if (wcid->hw_key_idx == 0xff)
  276. dma_flags |= MT_TXD_PKT_INFO_WIV;
  277. ret = mt7601u_dma_skb_wrap_pkt(skb, ep2dmaq(ep), dma_flags);
  278. if (ret)
  279. return ret;
  280. ret = mt7601u_dma_submit_tx(dev, skb, ep);
  281. if (ret) {
  282. ieee80211_free_txskb(dev->hw, skb);
  283. return ret;
  284. }
  285. return 0;
  286. }
  287. static void mt7601u_kill_rx(struct mt7601u_dev *dev)
  288. {
  289. int i;
  290. unsigned long flags;
  291. spin_lock_irqsave(&dev->rx_lock, flags);
  292. for (i = 0; i < dev->rx_q.entries; i++) {
  293. int next = dev->rx_q.end;
  294. spin_unlock_irqrestore(&dev->rx_lock, flags);
  295. usb_poison_urb(dev->rx_q.e[next].urb);
  296. spin_lock_irqsave(&dev->rx_lock, flags);
  297. }
  298. spin_unlock_irqrestore(&dev->rx_lock, flags);
  299. }
  300. static int mt7601u_submit_rx_buf(struct mt7601u_dev *dev,
  301. struct mt7601u_dma_buf_rx *e, gfp_t gfp)
  302. {
  303. struct usb_device *usb_dev = mt7601u_to_usb_dev(dev);
  304. u8 *buf = page_address(e->p);
  305. unsigned pipe;
  306. int ret;
  307. pipe = usb_rcvbulkpipe(usb_dev, dev->in_eps[MT_EP_IN_PKT_RX]);
  308. usb_fill_bulk_urb(e->urb, usb_dev, pipe, buf, MT_RX_URB_SIZE,
  309. mt7601u_complete_rx, dev);
  310. trace_mt_submit_urb(dev, e->urb);
  311. ret = usb_submit_urb(e->urb, gfp);
  312. if (ret)
  313. dev_err(dev->dev, "Error: submit RX URB failed:%d\n", ret);
  314. return ret;
  315. }
  316. static int mt7601u_submit_rx(struct mt7601u_dev *dev)
  317. {
  318. int i, ret;
  319. for (i = 0; i < dev->rx_q.entries; i++) {
  320. ret = mt7601u_submit_rx_buf(dev, &dev->rx_q.e[i], GFP_KERNEL);
  321. if (ret)
  322. return ret;
  323. }
  324. return 0;
  325. }
  326. static void mt7601u_free_rx(struct mt7601u_dev *dev)
  327. {
  328. int i;
  329. for (i = 0; i < dev->rx_q.entries; i++) {
  330. __free_pages(dev->rx_q.e[i].p, MT_RX_ORDER);
  331. usb_free_urb(dev->rx_q.e[i].urb);
  332. }
  333. }
  334. static int mt7601u_alloc_rx(struct mt7601u_dev *dev)
  335. {
  336. int i;
  337. memset(&dev->rx_q, 0, sizeof(dev->rx_q));
  338. dev->rx_q.dev = dev;
  339. dev->rx_q.entries = N_RX_ENTRIES;
  340. for (i = 0; i < N_RX_ENTRIES; i++) {
  341. dev->rx_q.e[i].urb = usb_alloc_urb(0, GFP_KERNEL);
  342. dev->rx_q.e[i].p = dev_alloc_pages(MT_RX_ORDER);
  343. if (!dev->rx_q.e[i].urb || !dev->rx_q.e[i].p)
  344. return -ENOMEM;
  345. }
  346. return 0;
  347. }
  348. static void mt7601u_free_tx_queue(struct mt7601u_tx_queue *q)
  349. {
  350. int i;
  351. WARN_ON(q->used);
  352. for (i = 0; i < q->entries; i++) {
  353. usb_poison_urb(q->e[i].urb);
  354. usb_free_urb(q->e[i].urb);
  355. }
  356. }
  357. static void mt7601u_free_tx(struct mt7601u_dev *dev)
  358. {
  359. int i;
  360. for (i = 0; i < __MT_EP_OUT_MAX; i++)
  361. mt7601u_free_tx_queue(&dev->tx_q[i]);
  362. }
  363. static int mt7601u_alloc_tx_queue(struct mt7601u_dev *dev,
  364. struct mt7601u_tx_queue *q)
  365. {
  366. int i;
  367. q->dev = dev;
  368. q->entries = N_TX_ENTRIES;
  369. for (i = 0; i < N_TX_ENTRIES; i++) {
  370. q->e[i].urb = usb_alloc_urb(0, GFP_KERNEL);
  371. if (!q->e[i].urb)
  372. return -ENOMEM;
  373. }
  374. return 0;
  375. }
  376. static int mt7601u_alloc_tx(struct mt7601u_dev *dev)
  377. {
  378. int i;
  379. dev->tx_q = devm_kcalloc(dev->dev, __MT_EP_OUT_MAX,
  380. sizeof(*dev->tx_q), GFP_KERNEL);
  381. for (i = 0; i < __MT_EP_OUT_MAX; i++)
  382. if (mt7601u_alloc_tx_queue(dev, &dev->tx_q[i]))
  383. return -ENOMEM;
  384. return 0;
  385. }
  386. int mt7601u_dma_init(struct mt7601u_dev *dev)
  387. {
  388. int ret = -ENOMEM;
  389. tasklet_init(&dev->tx_tasklet, mt7601u_tx_tasklet, (unsigned long) dev);
  390. tasklet_init(&dev->rx_tasklet, mt7601u_rx_tasklet, (unsigned long) dev);
  391. ret = mt7601u_alloc_tx(dev);
  392. if (ret)
  393. goto err;
  394. ret = mt7601u_alloc_rx(dev);
  395. if (ret)
  396. goto err;
  397. ret = mt7601u_submit_rx(dev);
  398. if (ret)
  399. goto err;
  400. return 0;
  401. err:
  402. mt7601u_dma_cleanup(dev);
  403. return ret;
  404. }
  405. void mt7601u_dma_cleanup(struct mt7601u_dev *dev)
  406. {
  407. mt7601u_kill_rx(dev);
  408. tasklet_kill(&dev->rx_tasklet);
  409. mt7601u_free_rx(dev);
  410. mt7601u_free_tx(dev);
  411. tasklet_kill(&dev->tx_tasklet);
  412. }