Linux Audio

Check our new training course

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