usb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
  4. */
  5. #include <linux/module.h>
  6. #include "mt76.h"
  7. #include "usb_trace.h"
  8. #include "dma.h"
  9. #define MT_VEND_REQ_MAX_RETRY 10
  10. #define MT_VEND_REQ_TOUT_MS 300
  11. static bool disable_usb_sg;
  12. module_param_named(disable_usb_sg, disable_usb_sg, bool, 0644);
  13. MODULE_PARM_DESC(disable_usb_sg, "Disable usb scatter-gather support");
  14. static int __mt76u_vendor_request(struct mt76_dev *dev, u8 req,
  15. u8 req_type, u16 val, u16 offset,
  16. void *buf, size_t len)
  17. {
  18. struct usb_interface *uintf = to_usb_interface(dev->dev);
  19. struct usb_device *udev = interface_to_usbdev(uintf);
  20. unsigned int pipe;
  21. int i, ret;
  22. lockdep_assert_held(&dev->usb.usb_ctrl_mtx);
  23. pipe = (req_type & USB_DIR_IN) ? usb_rcvctrlpipe(udev, 0)
  24. : usb_sndctrlpipe(udev, 0);
  25. for (i = 0; i < MT_VEND_REQ_MAX_RETRY; i++) {
  26. if (test_bit(MT76_REMOVED, &dev->phy.state))
  27. return -EIO;
  28. ret = usb_control_msg(udev, pipe, req, req_type, val,
  29. offset, buf, len, MT_VEND_REQ_TOUT_MS);
  30. if (ret == -ENODEV)
  31. set_bit(MT76_REMOVED, &dev->phy.state);
  32. if (ret >= 0 || ret == -ENODEV)
  33. return ret;
  34. usleep_range(5000, 10000);
  35. }
  36. dev_err(dev->dev, "vendor request req:%02x off:%04x failed:%d\n",
  37. req, offset, ret);
  38. return ret;
  39. }
  40. int mt76u_vendor_request(struct mt76_dev *dev, u8 req,
  41. u8 req_type, u16 val, u16 offset,
  42. void *buf, size_t len)
  43. {
  44. int ret;
  45. mutex_lock(&dev->usb.usb_ctrl_mtx);
  46. ret = __mt76u_vendor_request(dev, req, req_type,
  47. val, offset, buf, len);
  48. trace_usb_reg_wr(dev, offset, val);
  49. mutex_unlock(&dev->usb.usb_ctrl_mtx);
  50. return ret;
  51. }
  52. EXPORT_SYMBOL_GPL(mt76u_vendor_request);
  53. static u32 ___mt76u_rr(struct mt76_dev *dev, u8 req, u32 addr)
  54. {
  55. struct mt76_usb *usb = &dev->usb;
  56. u32 data = ~0;
  57. int ret;
  58. ret = __mt76u_vendor_request(dev, req,
  59. USB_DIR_IN | USB_TYPE_VENDOR,
  60. addr >> 16, addr, usb->data,
  61. sizeof(__le32));
  62. if (ret == sizeof(__le32))
  63. data = get_unaligned_le32(usb->data);
  64. trace_usb_reg_rr(dev, addr, data);
  65. return data;
  66. }
  67. static u32 __mt76u_rr(struct mt76_dev *dev, u32 addr)
  68. {
  69. u8 req;
  70. switch (addr & MT_VEND_TYPE_MASK) {
  71. case MT_VEND_TYPE_EEPROM:
  72. req = MT_VEND_READ_EEPROM;
  73. break;
  74. case MT_VEND_TYPE_CFG:
  75. req = MT_VEND_READ_CFG;
  76. break;
  77. default:
  78. req = MT_VEND_MULTI_READ;
  79. break;
  80. }
  81. return ___mt76u_rr(dev, req, addr & ~MT_VEND_TYPE_MASK);
  82. }
  83. static u32 mt76u_rr(struct mt76_dev *dev, u32 addr)
  84. {
  85. u32 ret;
  86. mutex_lock(&dev->usb.usb_ctrl_mtx);
  87. ret = __mt76u_rr(dev, addr);
  88. mutex_unlock(&dev->usb.usb_ctrl_mtx);
  89. return ret;
  90. }
  91. static u32 mt76u_rr_ext(struct mt76_dev *dev, u32 addr)
  92. {
  93. u32 ret;
  94. mutex_lock(&dev->usb.usb_ctrl_mtx);
  95. ret = ___mt76u_rr(dev, MT_VEND_READ_EXT, addr);
  96. mutex_unlock(&dev->usb.usb_ctrl_mtx);
  97. return ret;
  98. }
  99. static void ___mt76u_wr(struct mt76_dev *dev, u8 req,
  100. u32 addr, u32 val)
  101. {
  102. struct mt76_usb *usb = &dev->usb;
  103. put_unaligned_le32(val, usb->data);
  104. __mt76u_vendor_request(dev, req,
  105. USB_DIR_OUT | USB_TYPE_VENDOR,
  106. addr >> 16, addr, usb->data,
  107. sizeof(__le32));
  108. trace_usb_reg_wr(dev, addr, val);
  109. }
  110. static void __mt76u_wr(struct mt76_dev *dev, u32 addr, u32 val)
  111. {
  112. u8 req;
  113. switch (addr & MT_VEND_TYPE_MASK) {
  114. case MT_VEND_TYPE_CFG:
  115. req = MT_VEND_WRITE_CFG;
  116. break;
  117. default:
  118. req = MT_VEND_MULTI_WRITE;
  119. break;
  120. }
  121. ___mt76u_wr(dev, req, addr & ~MT_VEND_TYPE_MASK, val);
  122. }
  123. static void mt76u_wr(struct mt76_dev *dev, u32 addr, u32 val)
  124. {
  125. mutex_lock(&dev->usb.usb_ctrl_mtx);
  126. __mt76u_wr(dev, addr, val);
  127. mutex_unlock(&dev->usb.usb_ctrl_mtx);
  128. }
  129. static void mt76u_wr_ext(struct mt76_dev *dev, u32 addr, u32 val)
  130. {
  131. mutex_lock(&dev->usb.usb_ctrl_mtx);
  132. ___mt76u_wr(dev, MT_VEND_WRITE_EXT, addr, val);
  133. mutex_unlock(&dev->usb.usb_ctrl_mtx);
  134. }
  135. static u32 mt76u_rmw(struct mt76_dev *dev, u32 addr,
  136. u32 mask, u32 val)
  137. {
  138. mutex_lock(&dev->usb.usb_ctrl_mtx);
  139. val |= __mt76u_rr(dev, addr) & ~mask;
  140. __mt76u_wr(dev, addr, val);
  141. mutex_unlock(&dev->usb.usb_ctrl_mtx);
  142. return val;
  143. }
  144. static u32 mt76u_rmw_ext(struct mt76_dev *dev, u32 addr,
  145. u32 mask, u32 val)
  146. {
  147. mutex_lock(&dev->usb.usb_ctrl_mtx);
  148. val |= ___mt76u_rr(dev, MT_VEND_READ_EXT, addr) & ~mask;
  149. ___mt76u_wr(dev, MT_VEND_WRITE_EXT, addr, val);
  150. mutex_unlock(&dev->usb.usb_ctrl_mtx);
  151. return val;
  152. }
  153. static void mt76u_copy(struct mt76_dev *dev, u32 offset,
  154. const void *data, int len)
  155. {
  156. struct mt76_usb *usb = &dev->usb;
  157. const u8 *val = data;
  158. int ret;
  159. int current_batch_size;
  160. int i = 0;
  161. /* Assure that always a multiple of 4 bytes are copied,
  162. * otherwise beacons can be corrupted.
  163. * See: "mt76: round up length on mt76_wr_copy"
  164. * Commit 850e8f6fbd5d0003b0
  165. */
  166. len = round_up(len, 4);
  167. mutex_lock(&usb->usb_ctrl_mtx);
  168. while (i < len) {
  169. current_batch_size = min_t(int, usb->data_len, len - i);
  170. memcpy(usb->data, val + i, current_batch_size);
  171. ret = __mt76u_vendor_request(dev, MT_VEND_MULTI_WRITE,
  172. USB_DIR_OUT | USB_TYPE_VENDOR,
  173. 0, offset + i, usb->data,
  174. current_batch_size);
  175. if (ret < 0)
  176. break;
  177. i += current_batch_size;
  178. }
  179. mutex_unlock(&usb->usb_ctrl_mtx);
  180. }
  181. static void mt76u_copy_ext(struct mt76_dev *dev, u32 offset,
  182. const void *data, int len)
  183. {
  184. struct mt76_usb *usb = &dev->usb;
  185. int ret, i = 0, batch_len;
  186. const u8 *val = data;
  187. len = round_up(len, 4);
  188. mutex_lock(&usb->usb_ctrl_mtx);
  189. while (i < len) {
  190. batch_len = min_t(int, usb->data_len, len - i);
  191. memcpy(usb->data, val + i, batch_len);
  192. ret = __mt76u_vendor_request(dev, MT_VEND_WRITE_EXT,
  193. USB_DIR_OUT | USB_TYPE_VENDOR,
  194. (offset + i) >> 16, offset + i,
  195. usb->data, batch_len);
  196. if (ret < 0)
  197. break;
  198. i += batch_len;
  199. }
  200. mutex_unlock(&usb->usb_ctrl_mtx);
  201. }
  202. static void
  203. mt76u_read_copy_ext(struct mt76_dev *dev, u32 offset,
  204. void *data, int len)
  205. {
  206. struct mt76_usb *usb = &dev->usb;
  207. int i = 0, batch_len, ret;
  208. u8 *val = data;
  209. len = round_up(len, 4);
  210. mutex_lock(&usb->usb_ctrl_mtx);
  211. while (i < len) {
  212. batch_len = min_t(int, usb->data_len, len - i);
  213. ret = __mt76u_vendor_request(dev, MT_VEND_READ_EXT,
  214. USB_DIR_IN | USB_TYPE_VENDOR,
  215. (offset + i) >> 16, offset + i,
  216. usb->data, batch_len);
  217. if (ret < 0)
  218. break;
  219. memcpy(val + i, usb->data, batch_len);
  220. i += batch_len;
  221. }
  222. mutex_unlock(&usb->usb_ctrl_mtx);
  223. }
  224. void mt76u_single_wr(struct mt76_dev *dev, const u8 req,
  225. const u16 offset, const u32 val)
  226. {
  227. mutex_lock(&dev->usb.usb_ctrl_mtx);
  228. __mt76u_vendor_request(dev, req,
  229. USB_DIR_OUT | USB_TYPE_VENDOR,
  230. val & 0xffff, offset, NULL, 0);
  231. __mt76u_vendor_request(dev, req,
  232. USB_DIR_OUT | USB_TYPE_VENDOR,
  233. val >> 16, offset + 2, NULL, 0);
  234. mutex_unlock(&dev->usb.usb_ctrl_mtx);
  235. }
  236. EXPORT_SYMBOL_GPL(mt76u_single_wr);
  237. static int
  238. mt76u_req_wr_rp(struct mt76_dev *dev, u32 base,
  239. const struct mt76_reg_pair *data, int len)
  240. {
  241. struct mt76_usb *usb = &dev->usb;
  242. mutex_lock(&usb->usb_ctrl_mtx);
  243. while (len > 0) {
  244. __mt76u_wr(dev, base + data->reg, data->value);
  245. len--;
  246. data++;
  247. }
  248. mutex_unlock(&usb->usb_ctrl_mtx);
  249. return 0;
  250. }
  251. static int
  252. mt76u_wr_rp(struct mt76_dev *dev, u32 base,
  253. const struct mt76_reg_pair *data, int n)
  254. {
  255. if (test_bit(MT76_STATE_MCU_RUNNING, &dev->phy.state))
  256. return dev->mcu_ops->mcu_wr_rp(dev, base, data, n);
  257. else
  258. return mt76u_req_wr_rp(dev, base, data, n);
  259. }
  260. static int
  261. mt76u_req_rd_rp(struct mt76_dev *dev, u32 base, struct mt76_reg_pair *data,
  262. int len)
  263. {
  264. struct mt76_usb *usb = &dev->usb;
  265. mutex_lock(&usb->usb_ctrl_mtx);
  266. while (len > 0) {
  267. data->value = __mt76u_rr(dev, base + data->reg);
  268. len--;
  269. data++;
  270. }
  271. mutex_unlock(&usb->usb_ctrl_mtx);
  272. return 0;
  273. }
  274. static int
  275. mt76u_rd_rp(struct mt76_dev *dev, u32 base,
  276. struct mt76_reg_pair *data, int n)
  277. {
  278. if (test_bit(MT76_STATE_MCU_RUNNING, &dev->phy.state))
  279. return dev->mcu_ops->mcu_rd_rp(dev, base, data, n);
  280. else
  281. return mt76u_req_rd_rp(dev, base, data, n);
  282. }
  283. static bool mt76u_check_sg(struct mt76_dev *dev)
  284. {
  285. struct usb_interface *uintf = to_usb_interface(dev->dev);
  286. struct usb_device *udev = interface_to_usbdev(uintf);
  287. return (!disable_usb_sg && udev->bus->sg_tablesize > 0 &&
  288. (udev->bus->no_sg_constraint ||
  289. udev->speed == USB_SPEED_WIRELESS));
  290. }
  291. static int
  292. mt76u_set_endpoints(struct usb_interface *intf,
  293. struct mt76_usb *usb)
  294. {
  295. struct usb_host_interface *intf_desc = intf->cur_altsetting;
  296. struct usb_endpoint_descriptor *ep_desc;
  297. int i, in_ep = 0, out_ep = 0;
  298. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  299. ep_desc = &intf_desc->endpoint[i].desc;
  300. if (usb_endpoint_is_bulk_in(ep_desc) &&
  301. in_ep < __MT_EP_IN_MAX) {
  302. usb->in_ep[in_ep] = usb_endpoint_num(ep_desc);
  303. in_ep++;
  304. } else if (usb_endpoint_is_bulk_out(ep_desc) &&
  305. out_ep < __MT_EP_OUT_MAX) {
  306. usb->out_ep[out_ep] = usb_endpoint_num(ep_desc);
  307. out_ep++;
  308. }
  309. }
  310. if (in_ep != __MT_EP_IN_MAX || out_ep != __MT_EP_OUT_MAX)
  311. return -EINVAL;
  312. return 0;
  313. }
  314. static int
  315. mt76u_fill_rx_sg(struct mt76_dev *dev, struct mt76_queue *q, struct urb *urb,
  316. int nsgs, gfp_t gfp)
  317. {
  318. int i;
  319. for (i = 0; i < nsgs; i++) {
  320. struct page *page;
  321. void *data;
  322. int offset;
  323. data = page_frag_alloc(&q->rx_page, q->buf_size, gfp);
  324. if (!data)
  325. break;
  326. page = virt_to_head_page(data);
  327. offset = data - page_address(page);
  328. sg_set_page(&urb->sg[i], page, q->buf_size, offset);
  329. }
  330. if (i < nsgs) {
  331. int j;
  332. for (j = nsgs; j < urb->num_sgs; j++)
  333. skb_free_frag(sg_virt(&urb->sg[j]));
  334. urb->num_sgs = i;
  335. }
  336. urb->num_sgs = max_t(int, i, urb->num_sgs);
  337. urb->transfer_buffer_length = urb->num_sgs * q->buf_size;
  338. sg_init_marker(urb->sg, urb->num_sgs);
  339. return i ? : -ENOMEM;
  340. }
  341. static int
  342. mt76u_refill_rx(struct mt76_dev *dev, struct mt76_queue *q,
  343. struct urb *urb, int nsgs, gfp_t gfp)
  344. {
  345. enum mt76_rxq_id qid = q - &dev->q_rx[MT_RXQ_MAIN];
  346. if (qid == MT_RXQ_MAIN && dev->usb.sg_en)
  347. return mt76u_fill_rx_sg(dev, q, urb, nsgs, gfp);
  348. urb->transfer_buffer_length = q->buf_size;
  349. urb->transfer_buffer = page_frag_alloc(&q->rx_page, q->buf_size, gfp);
  350. return urb->transfer_buffer ? 0 : -ENOMEM;
  351. }
  352. static int
  353. mt76u_urb_alloc(struct mt76_dev *dev, struct mt76_queue_entry *e,
  354. int sg_max_size)
  355. {
  356. unsigned int size = sizeof(struct urb);
  357. if (dev->usb.sg_en)
  358. size += sg_max_size * sizeof(struct scatterlist);
  359. e->urb = kzalloc(size, GFP_KERNEL);
  360. if (!e->urb)
  361. return -ENOMEM;
  362. usb_init_urb(e->urb);
  363. if (dev->usb.sg_en && sg_max_size > 0)
  364. e->urb->sg = (struct scatterlist *)(e->urb + 1);
  365. return 0;
  366. }
  367. static int
  368. mt76u_rx_urb_alloc(struct mt76_dev *dev, struct mt76_queue *q,
  369. struct mt76_queue_entry *e)
  370. {
  371. enum mt76_rxq_id qid = q - &dev->q_rx[MT_RXQ_MAIN];
  372. int err, sg_size;
  373. sg_size = qid == MT_RXQ_MAIN ? MT_RX_SG_MAX_SIZE : 0;
  374. err = mt76u_urb_alloc(dev, e, sg_size);
  375. if (err)
  376. return err;
  377. return mt76u_refill_rx(dev, q, e->urb, sg_size, GFP_KERNEL);
  378. }
  379. static void mt76u_urb_free(struct urb *urb)
  380. {
  381. int i;
  382. for (i = 0; i < urb->num_sgs; i++)
  383. skb_free_frag(sg_virt(&urb->sg[i]));
  384. if (urb->transfer_buffer)
  385. skb_free_frag(urb->transfer_buffer);
  386. usb_free_urb(urb);
  387. }
  388. static void
  389. mt76u_fill_bulk_urb(struct mt76_dev *dev, int dir, int index,
  390. struct urb *urb, usb_complete_t complete_fn,
  391. void *context)
  392. {
  393. struct usb_interface *uintf = to_usb_interface(dev->dev);
  394. struct usb_device *udev = interface_to_usbdev(uintf);
  395. unsigned int pipe;
  396. if (dir == USB_DIR_IN)
  397. pipe = usb_rcvbulkpipe(udev, dev->usb.in_ep[index]);
  398. else
  399. pipe = usb_sndbulkpipe(udev, dev->usb.out_ep[index]);
  400. urb->dev = udev;
  401. urb->pipe = pipe;
  402. urb->complete = complete_fn;
  403. urb->context = context;
  404. }
  405. static struct urb *
  406. mt76u_get_next_rx_entry(struct mt76_queue *q)
  407. {
  408. struct urb *urb = NULL;
  409. unsigned long flags;
  410. spin_lock_irqsave(&q->lock, flags);
  411. if (q->queued > 0) {
  412. urb = q->entry[q->tail].urb;
  413. q->tail = (q->tail + 1) % q->ndesc;
  414. q->queued--;
  415. }
  416. spin_unlock_irqrestore(&q->lock, flags);
  417. return urb;
  418. }
  419. static int
  420. mt76u_get_rx_entry_len(struct mt76_dev *dev, u8 *data,
  421. u32 data_len)
  422. {
  423. u16 dma_len, min_len;
  424. dma_len = get_unaligned_le16(data);
  425. if (dev->drv->drv_flags & MT_DRV_RX_DMA_HDR)
  426. return dma_len;
  427. min_len = MT_DMA_HDR_LEN + MT_RX_RXWI_LEN + MT_FCE_INFO_LEN;
  428. if (data_len < min_len || !dma_len ||
  429. dma_len + MT_DMA_HDR_LEN > data_len ||
  430. (dma_len & 0x3))
  431. return -EINVAL;
  432. return dma_len;
  433. }
  434. static struct sk_buff *
  435. mt76u_build_rx_skb(struct mt76_dev *dev, void *data,
  436. int len, int buf_size)
  437. {
  438. int head_room, drv_flags = dev->drv->drv_flags;
  439. struct sk_buff *skb;
  440. head_room = drv_flags & MT_DRV_RX_DMA_HDR ? 0 : MT_DMA_HDR_LEN;
  441. if (SKB_WITH_OVERHEAD(buf_size) < head_room + len) {
  442. struct page *page;
  443. /* slow path, not enough space for data and
  444. * skb_shared_info
  445. */
  446. skb = alloc_skb(MT_SKB_HEAD_LEN, GFP_ATOMIC);
  447. if (!skb)
  448. return NULL;
  449. skb_put_data(skb, data + head_room, MT_SKB_HEAD_LEN);
  450. data += head_room + MT_SKB_HEAD_LEN;
  451. page = virt_to_head_page(data);
  452. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  453. page, data - page_address(page),
  454. len - MT_SKB_HEAD_LEN, buf_size);
  455. return skb;
  456. }
  457. /* fast path */
  458. skb = build_skb(data, buf_size);
  459. if (!skb)
  460. return NULL;
  461. skb_reserve(skb, head_room);
  462. __skb_put(skb, len);
  463. return skb;
  464. }
  465. static int
  466. mt76u_process_rx_entry(struct mt76_dev *dev, struct urb *urb,
  467. int buf_size)
  468. {
  469. u8 *data = urb->num_sgs ? sg_virt(&urb->sg[0]) : urb->transfer_buffer;
  470. int data_len = urb->num_sgs ? urb->sg[0].length : urb->actual_length;
  471. int len, nsgs = 1, head_room, drv_flags = dev->drv->drv_flags;
  472. struct sk_buff *skb;
  473. if (!test_bit(MT76_STATE_INITIALIZED, &dev->phy.state))
  474. return 0;
  475. len = mt76u_get_rx_entry_len(dev, data, urb->actual_length);
  476. if (len < 0)
  477. return 0;
  478. head_room = drv_flags & MT_DRV_RX_DMA_HDR ? 0 : MT_DMA_HDR_LEN;
  479. data_len = min_t(int, len, data_len - head_room);
  480. skb = mt76u_build_rx_skb(dev, data, data_len, buf_size);
  481. if (!skb)
  482. return 0;
  483. len -= data_len;
  484. while (len > 0 && nsgs < urb->num_sgs) {
  485. data_len = min_t(int, len, urb->sg[nsgs].length);
  486. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  487. sg_page(&urb->sg[nsgs]),
  488. urb->sg[nsgs].offset, data_len,
  489. buf_size);
  490. len -= data_len;
  491. nsgs++;
  492. }
  493. dev->drv->rx_skb(dev, MT_RXQ_MAIN, skb);
  494. return nsgs;
  495. }
  496. static void mt76u_complete_rx(struct urb *urb)
  497. {
  498. struct mt76_dev *dev = dev_get_drvdata(&urb->dev->dev);
  499. struct mt76_queue *q = urb->context;
  500. unsigned long flags;
  501. trace_rx_urb(dev, urb);
  502. switch (urb->status) {
  503. case -ECONNRESET:
  504. case -ESHUTDOWN:
  505. case -ENOENT:
  506. case -EPROTO:
  507. return;
  508. default:
  509. dev_err_ratelimited(dev->dev, "rx urb failed: %d\n",
  510. urb->status);
  511. fallthrough;
  512. case 0:
  513. break;
  514. }
  515. spin_lock_irqsave(&q->lock, flags);
  516. if (WARN_ONCE(q->entry[q->head].urb != urb, "rx urb mismatch"))
  517. goto out;
  518. q->head = (q->head + 1) % q->ndesc;
  519. q->queued++;
  520. mt76_worker_schedule(&dev->usb.rx_worker);
  521. out:
  522. spin_unlock_irqrestore(&q->lock, flags);
  523. }
  524. static int
  525. mt76u_submit_rx_buf(struct mt76_dev *dev, enum mt76_rxq_id qid,
  526. struct urb *urb)
  527. {
  528. int ep = qid == MT_RXQ_MAIN ? MT_EP_IN_PKT_RX : MT_EP_IN_CMD_RESP;
  529. mt76u_fill_bulk_urb(dev, USB_DIR_IN, ep, urb,
  530. mt76u_complete_rx, &dev->q_rx[qid]);
  531. trace_submit_urb(dev, urb);
  532. return usb_submit_urb(urb, GFP_ATOMIC);
  533. }
  534. static void
  535. mt76u_process_rx_queue(struct mt76_dev *dev, struct mt76_queue *q)
  536. {
  537. int qid = q - &dev->q_rx[MT_RXQ_MAIN];
  538. struct urb *urb;
  539. int err, count;
  540. while (true) {
  541. urb = mt76u_get_next_rx_entry(q);
  542. if (!urb)
  543. break;
  544. count = mt76u_process_rx_entry(dev, urb, q->buf_size);
  545. if (count > 0) {
  546. err = mt76u_refill_rx(dev, q, urb, count, GFP_ATOMIC);
  547. if (err < 0)
  548. break;
  549. }
  550. mt76u_submit_rx_buf(dev, qid, urb);
  551. }
  552. if (qid == MT_RXQ_MAIN) {
  553. local_bh_disable();
  554. mt76_rx_poll_complete(dev, MT_RXQ_MAIN, NULL);
  555. local_bh_enable();
  556. }
  557. }
  558. static void mt76u_rx_worker(struct mt76_worker *w)
  559. {
  560. struct mt76_usb *usb = container_of(w, struct mt76_usb, rx_worker);
  561. struct mt76_dev *dev = container_of(usb, struct mt76_dev, usb);
  562. int i;
  563. rcu_read_lock();
  564. mt76_for_each_q_rx(dev, i)
  565. mt76u_process_rx_queue(dev, &dev->q_rx[i]);
  566. rcu_read_unlock();
  567. }
  568. static int
  569. mt76u_submit_rx_buffers(struct mt76_dev *dev, enum mt76_rxq_id qid)
  570. {
  571. struct mt76_queue *q = &dev->q_rx[qid];
  572. unsigned long flags;
  573. int i, err = 0;
  574. spin_lock_irqsave(&q->lock, flags);
  575. for (i = 0; i < q->ndesc; i++) {
  576. err = mt76u_submit_rx_buf(dev, qid, q->entry[i].urb);
  577. if (err < 0)
  578. break;
  579. }
  580. q->head = q->tail = 0;
  581. q->queued = 0;
  582. spin_unlock_irqrestore(&q->lock, flags);
  583. return err;
  584. }
  585. static int
  586. mt76u_alloc_rx_queue(struct mt76_dev *dev, enum mt76_rxq_id qid)
  587. {
  588. struct mt76_queue *q = &dev->q_rx[qid];
  589. int i, err;
  590. spin_lock_init(&q->lock);
  591. q->entry = devm_kcalloc(dev->dev,
  592. MT_NUM_RX_ENTRIES, sizeof(*q->entry),
  593. GFP_KERNEL);
  594. if (!q->entry)
  595. return -ENOMEM;
  596. q->ndesc = MT_NUM_RX_ENTRIES;
  597. q->buf_size = PAGE_SIZE;
  598. for (i = 0; i < q->ndesc; i++) {
  599. err = mt76u_rx_urb_alloc(dev, q, &q->entry[i]);
  600. if (err < 0)
  601. return err;
  602. }
  603. return mt76u_submit_rx_buffers(dev, qid);
  604. }
  605. int mt76u_alloc_mcu_queue(struct mt76_dev *dev)
  606. {
  607. return mt76u_alloc_rx_queue(dev, MT_RXQ_MCU);
  608. }
  609. EXPORT_SYMBOL_GPL(mt76u_alloc_mcu_queue);
  610. static void
  611. mt76u_free_rx_queue(struct mt76_dev *dev, struct mt76_queue *q)
  612. {
  613. struct page *page;
  614. int i;
  615. for (i = 0; i < q->ndesc; i++) {
  616. if (!q->entry[i].urb)
  617. continue;
  618. mt76u_urb_free(q->entry[i].urb);
  619. q->entry[i].urb = NULL;
  620. }
  621. if (!q->rx_page.va)
  622. return;
  623. page = virt_to_page(q->rx_page.va);
  624. __page_frag_cache_drain(page, q->rx_page.pagecnt_bias);
  625. memset(&q->rx_page, 0, sizeof(q->rx_page));
  626. }
  627. static void mt76u_free_rx(struct mt76_dev *dev)
  628. {
  629. int i;
  630. mt76_worker_teardown(&dev->usb.rx_worker);
  631. mt76_for_each_q_rx(dev, i)
  632. mt76u_free_rx_queue(dev, &dev->q_rx[i]);
  633. }
  634. void mt76u_stop_rx(struct mt76_dev *dev)
  635. {
  636. int i;
  637. mt76_worker_disable(&dev->usb.rx_worker);
  638. mt76_for_each_q_rx(dev, i) {
  639. struct mt76_queue *q = &dev->q_rx[i];
  640. int j;
  641. for (j = 0; j < q->ndesc; j++)
  642. usb_poison_urb(q->entry[j].urb);
  643. }
  644. }
  645. EXPORT_SYMBOL_GPL(mt76u_stop_rx);
  646. int mt76u_resume_rx(struct mt76_dev *dev)
  647. {
  648. int i;
  649. mt76_for_each_q_rx(dev, i) {
  650. struct mt76_queue *q = &dev->q_rx[i];
  651. int err, j;
  652. for (j = 0; j < q->ndesc; j++)
  653. usb_unpoison_urb(q->entry[j].urb);
  654. err = mt76u_submit_rx_buffers(dev, i);
  655. if (err < 0)
  656. return err;
  657. }
  658. mt76_worker_enable(&dev->usb.rx_worker);
  659. return 0;
  660. }
  661. EXPORT_SYMBOL_GPL(mt76u_resume_rx);
  662. static void mt76u_status_worker(struct mt76_worker *w)
  663. {
  664. struct mt76_usb *usb = container_of(w, struct mt76_usb, status_worker);
  665. struct mt76_dev *dev = container_of(usb, struct mt76_dev, usb);
  666. struct mt76_queue_entry entry;
  667. struct mt76_queue *q;
  668. int i;
  669. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  670. q = dev->phy.q_tx[i];
  671. if (!q)
  672. continue;
  673. while (q->queued > 0) {
  674. if (!q->entry[q->tail].done)
  675. break;
  676. entry = q->entry[q->tail];
  677. q->entry[q->tail].done = false;
  678. mt76_queue_tx_complete(dev, q, &entry);
  679. }
  680. if (!q->queued)
  681. wake_up(&dev->tx_wait);
  682. mt76_worker_schedule(&dev->tx_worker);
  683. if (dev->drv->tx_status_data &&
  684. !test_and_set_bit(MT76_READING_STATS, &dev->phy.state))
  685. queue_work(dev->wq, &dev->usb.stat_work);
  686. }
  687. }
  688. static void mt76u_tx_status_data(struct work_struct *work)
  689. {
  690. struct mt76_usb *usb;
  691. struct mt76_dev *dev;
  692. u8 update = 1;
  693. u16 count = 0;
  694. usb = container_of(work, struct mt76_usb, stat_work);
  695. dev = container_of(usb, struct mt76_dev, usb);
  696. while (true) {
  697. if (test_bit(MT76_REMOVED, &dev->phy.state))
  698. break;
  699. if (!dev->drv->tx_status_data(dev, &update))
  700. break;
  701. count++;
  702. }
  703. if (count && test_bit(MT76_STATE_RUNNING, &dev->phy.state))
  704. queue_work(dev->wq, &usb->stat_work);
  705. else
  706. clear_bit(MT76_READING_STATS, &dev->phy.state);
  707. }
  708. static void mt76u_complete_tx(struct urb *urb)
  709. {
  710. struct mt76_dev *dev = dev_get_drvdata(&urb->dev->dev);
  711. struct mt76_queue_entry *e = urb->context;
  712. if (mt76u_urb_error(urb))
  713. dev_err(dev->dev, "tx urb failed: %d\n", urb->status);
  714. e->done = true;
  715. mt76_worker_schedule(&dev->usb.status_worker);
  716. }
  717. static int
  718. mt76u_tx_setup_buffers(struct mt76_dev *dev, struct sk_buff *skb,
  719. struct urb *urb)
  720. {
  721. urb->transfer_buffer_length = skb->len;
  722. if (!dev->usb.sg_en) {
  723. urb->transfer_buffer = skb->data;
  724. return 0;
  725. }
  726. sg_init_table(urb->sg, MT_TX_SG_MAX_SIZE);
  727. urb->num_sgs = skb_to_sgvec(skb, urb->sg, 0, skb->len);
  728. if (!urb->num_sgs)
  729. return -ENOMEM;
  730. return urb->num_sgs;
  731. }
  732. static int
  733. mt76u_tx_queue_skb(struct mt76_dev *dev, struct mt76_queue *q,
  734. struct sk_buff *skb, struct mt76_wcid *wcid,
  735. struct ieee80211_sta *sta)
  736. {
  737. struct mt76_tx_info tx_info = {
  738. .skb = skb,
  739. };
  740. u16 idx = q->head;
  741. int err;
  742. if (q->queued == q->ndesc)
  743. return -ENOSPC;
  744. skb->prev = skb->next = NULL;
  745. err = dev->drv->tx_prepare_skb(dev, NULL, q->qid, wcid, sta, &tx_info);
  746. if (err < 0)
  747. return err;
  748. err = mt76u_tx_setup_buffers(dev, tx_info.skb, q->entry[idx].urb);
  749. if (err < 0)
  750. return err;
  751. mt76u_fill_bulk_urb(dev, USB_DIR_OUT, q2ep(q->hw_idx),
  752. q->entry[idx].urb, mt76u_complete_tx,
  753. &q->entry[idx]);
  754. q->head = (q->head + 1) % q->ndesc;
  755. q->entry[idx].skb = tx_info.skb;
  756. q->entry[idx].wcid = 0xffff;
  757. q->queued++;
  758. return idx;
  759. }
  760. static void mt76u_tx_kick(struct mt76_dev *dev, struct mt76_queue *q)
  761. {
  762. struct urb *urb;
  763. int err;
  764. while (q->first != q->head) {
  765. urb = q->entry[q->first].urb;
  766. trace_submit_urb(dev, urb);
  767. err = usb_submit_urb(urb, GFP_ATOMIC);
  768. if (err < 0) {
  769. if (err == -ENODEV)
  770. set_bit(MT76_REMOVED, &dev->phy.state);
  771. else
  772. dev_err(dev->dev, "tx urb submit failed:%d\n",
  773. err);
  774. break;
  775. }
  776. q->first = (q->first + 1) % q->ndesc;
  777. }
  778. }
  779. static u8 mt76u_ac_to_hwq(struct mt76_dev *dev, u8 ac)
  780. {
  781. if (mt76_chip(dev) == 0x7663) {
  782. static const u8 lmac_queue_map[] = {
  783. /* ac to lmac mapping */
  784. [IEEE80211_AC_BK] = 0,
  785. [IEEE80211_AC_BE] = 1,
  786. [IEEE80211_AC_VI] = 2,
  787. [IEEE80211_AC_VO] = 4,
  788. };
  789. if (WARN_ON(ac >= ARRAY_SIZE(lmac_queue_map)))
  790. return 1; /* BE */
  791. return lmac_queue_map[ac];
  792. }
  793. return mt76_ac_to_hwq(ac);
  794. }
  795. static int mt76u_alloc_tx(struct mt76_dev *dev)
  796. {
  797. struct mt76_queue *q;
  798. int i, j, err;
  799. for (i = 0; i <= MT_TXQ_PSD; i++) {
  800. if (i >= IEEE80211_NUM_ACS) {
  801. dev->phy.q_tx[i] = dev->phy.q_tx[0];
  802. continue;
  803. }
  804. q = devm_kzalloc(dev->dev, sizeof(*q), GFP_KERNEL);
  805. if (!q)
  806. return -ENOMEM;
  807. spin_lock_init(&q->lock);
  808. q->hw_idx = mt76u_ac_to_hwq(dev, i);
  809. q->qid = i;
  810. dev->phy.q_tx[i] = q;
  811. q->entry = devm_kcalloc(dev->dev,
  812. MT_NUM_TX_ENTRIES, sizeof(*q->entry),
  813. GFP_KERNEL);
  814. if (!q->entry)
  815. return -ENOMEM;
  816. q->ndesc = MT_NUM_TX_ENTRIES;
  817. for (j = 0; j < q->ndesc; j++) {
  818. err = mt76u_urb_alloc(dev, &q->entry[j],
  819. MT_TX_SG_MAX_SIZE);
  820. if (err < 0)
  821. return err;
  822. }
  823. }
  824. return 0;
  825. }
  826. static void mt76u_free_tx(struct mt76_dev *dev)
  827. {
  828. int i;
  829. mt76_worker_teardown(&dev->usb.status_worker);
  830. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  831. struct mt76_queue *q;
  832. int j;
  833. q = dev->phy.q_tx[i];
  834. if (!q)
  835. continue;
  836. for (j = 0; j < q->ndesc; j++) {
  837. usb_free_urb(q->entry[j].urb);
  838. q->entry[j].urb = NULL;
  839. }
  840. }
  841. }
  842. void mt76u_stop_tx(struct mt76_dev *dev)
  843. {
  844. int ret;
  845. mt76_worker_disable(&dev->usb.status_worker);
  846. ret = wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(&dev->phy),
  847. HZ / 5);
  848. if (!ret) {
  849. struct mt76_queue_entry entry;
  850. struct mt76_queue *q;
  851. int i, j;
  852. dev_err(dev->dev, "timed out waiting for pending tx\n");
  853. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  854. q = dev->phy.q_tx[i];
  855. if (!q)
  856. continue;
  857. for (j = 0; j < q->ndesc; j++)
  858. usb_kill_urb(q->entry[j].urb);
  859. }
  860. mt76_worker_disable(&dev->tx_worker);
  861. /* On device removal we maight queue skb's, but mt76u_tx_kick()
  862. * will fail to submit urb, cleanup those skb's manually.
  863. */
  864. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  865. q = dev->phy.q_tx[i];
  866. if (!q)
  867. continue;
  868. while (q->queued > 0) {
  869. entry = q->entry[q->tail];
  870. q->entry[q->tail].done = false;
  871. mt76_queue_tx_complete(dev, q, &entry);
  872. }
  873. }
  874. mt76_worker_enable(&dev->tx_worker);
  875. }
  876. cancel_work_sync(&dev->usb.stat_work);
  877. clear_bit(MT76_READING_STATS, &dev->phy.state);
  878. mt76_worker_enable(&dev->usb.status_worker);
  879. mt76_tx_status_check(dev, NULL, true);
  880. }
  881. EXPORT_SYMBOL_GPL(mt76u_stop_tx);
  882. void mt76u_queues_deinit(struct mt76_dev *dev)
  883. {
  884. mt76u_stop_rx(dev);
  885. mt76u_stop_tx(dev);
  886. mt76u_free_rx(dev);
  887. mt76u_free_tx(dev);
  888. }
  889. EXPORT_SYMBOL_GPL(mt76u_queues_deinit);
  890. int mt76u_alloc_queues(struct mt76_dev *dev)
  891. {
  892. int err;
  893. err = mt76u_alloc_rx_queue(dev, MT_RXQ_MAIN);
  894. if (err < 0)
  895. return err;
  896. return mt76u_alloc_tx(dev);
  897. }
  898. EXPORT_SYMBOL_GPL(mt76u_alloc_queues);
  899. static const struct mt76_queue_ops usb_queue_ops = {
  900. .tx_queue_skb = mt76u_tx_queue_skb,
  901. .kick = mt76u_tx_kick,
  902. };
  903. int mt76u_init(struct mt76_dev *dev,
  904. struct usb_interface *intf, bool ext)
  905. {
  906. static struct mt76_bus_ops mt76u_ops = {
  907. .read_copy = mt76u_read_copy_ext,
  908. .wr_rp = mt76u_wr_rp,
  909. .rd_rp = mt76u_rd_rp,
  910. .type = MT76_BUS_USB,
  911. };
  912. struct usb_device *udev = interface_to_usbdev(intf);
  913. struct mt76_usb *usb = &dev->usb;
  914. int err;
  915. mt76u_ops.rr = ext ? mt76u_rr_ext : mt76u_rr;
  916. mt76u_ops.wr = ext ? mt76u_wr_ext : mt76u_wr;
  917. mt76u_ops.rmw = ext ? mt76u_rmw_ext : mt76u_rmw;
  918. mt76u_ops.write_copy = ext ? mt76u_copy_ext : mt76u_copy;
  919. INIT_WORK(&usb->stat_work, mt76u_tx_status_data);
  920. usb->data_len = usb_maxpacket(udev, usb_sndctrlpipe(udev, 0), 1);
  921. if (usb->data_len < 32)
  922. usb->data_len = 32;
  923. usb->data = devm_kmalloc(dev->dev, usb->data_len, GFP_KERNEL);
  924. if (!usb->data)
  925. return -ENOMEM;
  926. mutex_init(&usb->usb_ctrl_mtx);
  927. dev->bus = &mt76u_ops;
  928. dev->queue_ops = &usb_queue_ops;
  929. dev_set_drvdata(&udev->dev, dev);
  930. usb->sg_en = mt76u_check_sg(dev);
  931. err = mt76u_set_endpoints(intf, usb);
  932. if (err < 0)
  933. return err;
  934. err = mt76_worker_setup(dev->hw, &usb->rx_worker, mt76u_rx_worker,
  935. "usb-rx");
  936. if (err)
  937. return err;
  938. err = mt76_worker_setup(dev->hw, &usb->status_worker,
  939. mt76u_status_worker, "usb-status");
  940. if (err)
  941. return err;
  942. sched_set_fifo_low(usb->rx_worker.task);
  943. sched_set_fifo_low(usb->status_worker.task);
  944. return 0;
  945. }
  946. EXPORT_SYMBOL_GPL(mt76u_init);
  947. MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
  948. MODULE_LICENSE("Dual BSD/GPL");