dma.c 12.7 KB
Newer Older
1
// SPDX-License-Identifier: ISC
2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
 */

#include <linux/dma-mapping.h>
#include "mt76.h"
#include "dma.h"

static int
11 12 13
mt76_dma_alloc_queue(struct mt76_dev *dev, struct mt76_queue *q,
		     int idx, int n_desc, int bufsize,
		     u32 ring_base)
14 15 16 17 18 19
{
	int size;
	int i;

	spin_lock_init(&q->lock);

20 21 22 23 24
	q->regs = dev->mmio.regs + ring_base + idx * MT_RING_SIZE;
	q->ndesc = n_desc;
	q->buf_size = bufsize;
	q->hw_idx = idx;

25 26 27 28 29 30 31 32 33 34 35 36 37 38
	size = q->ndesc * sizeof(struct mt76_desc);
	q->desc = dmam_alloc_coherent(dev->dev, size, &q->desc_dma, GFP_KERNEL);
	if (!q->desc)
		return -ENOMEM;

	size = q->ndesc * sizeof(*q->entry);
	q->entry = devm_kzalloc(dev->dev, size, GFP_KERNEL);
	if (!q->entry)
		return -ENOMEM;

	/* clear descriptors */
	for (i = 0; i < q->ndesc; i++)
		q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE);

39 40 41 42
	writel(q->desc_dma, &q->regs->desc_base);
	writel(0, &q->regs->cpu_idx);
	writel(0, &q->regs->dma_idx);
	writel(q->ndesc, &q->regs->ring_size);
43 44 45 46 47 48 49 50 51 52 53 54 55

	return 0;
}

static int
mt76_dma_add_buf(struct mt76_dev *dev, struct mt76_queue *q,
		 struct mt76_queue_buf *buf, int nbufs, u32 info,
		 struct sk_buff *skb, void *txwi)
{
	struct mt76_desc *desc;
	u32 ctrl;
	int i, idx = -1;

56
	if (txwi) {
57
		q->entry[q->head].txwi = DMA_DUMMY_DATA;
58 59
		q->entry[q->head].skip_buf0 = true;
	}
60 61 62 63

	for (i = 0; i < nbufs; i += 2, buf += 2) {
		u32 buf0 = buf[0].addr, buf1 = 0;

64 65 66 67
		if (buf[0].skip_unmap)
			q->entry[q->head].skip_buf0 = true;
		q->entry[q->head].skip_buf1 = i == nbufs - 1;

68 69 70 71
		ctrl = FIELD_PREP(MT_DMA_CTL_SD_LEN0, buf[0].len);
		if (i < nbufs - 1) {
			buf1 = buf[1].addr;
			ctrl |= FIELD_PREP(MT_DMA_CTL_SD_LEN1, buf[1].len);
72 73
			if (buf[1].skip_unmap)
				q->entry[q->head].skip_buf1 = true;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		}

		if (i == nbufs - 1)
			ctrl |= MT_DMA_CTL_LAST_SEC0;
		else if (i == nbufs - 2)
			ctrl |= MT_DMA_CTL_LAST_SEC1;

		idx = q->head;
		q->head = (q->head + 1) % q->ndesc;

		desc = &q->desc[idx];

		WRITE_ONCE(desc->buf0, cpu_to_le32(buf0));
		WRITE_ONCE(desc->buf1, cpu_to_le32(buf1));
		WRITE_ONCE(desc->info, cpu_to_le32(info));
		WRITE_ONCE(desc->ctrl, cpu_to_le32(ctrl));

		q->queued++;
	}

	q->entry[idx].txwi = txwi;
	q->entry[idx].skb = skb;

	return idx;
}

static void
mt76_dma_tx_cleanup_idx(struct mt76_dev *dev, struct mt76_queue *q, int idx,
			struct mt76_queue_entry *prev_e)
{
	struct mt76_queue_entry *e = &q->entry[idx];
	__le32 __ctrl = READ_ONCE(q->desc[idx].ctrl);
	u32 ctrl = le32_to_cpu(__ctrl);

108
	if (!e->skip_buf0) {
109 110 111 112 113 114 115
		__le32 addr = READ_ONCE(q->desc[idx].buf0);
		u32 len = FIELD_GET(MT_DMA_CTL_SD_LEN0, ctrl);

		dma_unmap_single(dev->dev, le32_to_cpu(addr), len,
				 DMA_TO_DEVICE);
	}

116
	if (!e->skip_buf1) {
117 118 119 120 121 122 123
		__le32 addr = READ_ONCE(q->desc[idx].buf1);
		u32 len = FIELD_GET(MT_DMA_CTL_SD_LEN1, ctrl);

		dma_unmap_single(dev->dev, le32_to_cpu(addr), len,
				 DMA_TO_DEVICE);
	}

124
	if (e->txwi == DMA_DUMMY_DATA)
125 126
		e->txwi = NULL;

127 128 129
	if (e->skb == DMA_DUMMY_DATA)
		e->skb = NULL;

130 131 132 133 134 135 136
	*prev_e = *e;
	memset(e, 0, sizeof(*e));
}

static void
mt76_dma_sync_idx(struct mt76_dev *dev, struct mt76_queue *q)
{
137 138 139
	writel(q->desc_dma, &q->regs->desc_base);
	writel(q->ndesc, &q->regs->ring_size);
	q->head = readl(&q->regs->dma_idx);
140
	q->tail = q->head;
141 142 143 144 145
}

static void
mt76_dma_kick_queue(struct mt76_dev *dev, struct mt76_queue *q)
{
146
	wmb();
147
	writel(q->head, &q->regs->cpu_idx);
148 149 150 151 152
}

static void
mt76_dma_tx_cleanup(struct mt76_dev *dev, enum mt76_txq_id qid, bool flush)
{
153 154
	struct mt76_sw_queue *sq = &dev->q_tx[qid];
	struct mt76_queue *q = sq->q;
155 156
	struct mt76_queue_entry entry;
	bool wake = false;
157
	int last;
158

159
	if (!q)
160 161 162 163 164
		return;

	if (flush)
		last = -1;
	else
165
		last = readl(&q->regs->dma_idx);
166

167
	while (q->queued > 0 && q->tail != last) {
168
		mt76_dma_tx_cleanup_idx(dev, q, q->tail, &entry);
169
		mt76_queue_tx_complete(dev, q, &entry);
170 171

		if (entry.txwi) {
172
			if (!(dev->drv->drv_flags & MT_DRV_TXWI_NO_FREE))
173
				mt76_put_txwi(dev, entry.txwi);
174
			wake = !flush;
175 176 177
		}

		if (!flush && q->tail == last)
178
			last = readl(&q->regs->dma_idx);
179

180 181
	}

182
	if (flush) {
183
		spin_lock_bh(&q->lock);
184
		mt76_dma_sync_idx(dev, q);
185
		mt76_dma_kick_queue(dev, q);
186
		spin_unlock_bh(&q->lock);
187
	}
188

189 190 191 192
	wake = wake && q->stopped &&
	       qid < IEEE80211_NUM_ACS && q->queued < q->ndesc - 8;
	if (wake)
		q->stopped = false;
193 194 195 196

	if (!q->queued)
		wake_up(&dev->tx_wait);

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	if (wake)
		ieee80211_wake_queue(dev->hw, qid);
}

static void *
mt76_dma_get_buf(struct mt76_dev *dev, struct mt76_queue *q, int idx,
		 int *len, u32 *info, bool *more)
{
	struct mt76_queue_entry *e = &q->entry[idx];
	struct mt76_desc *desc = &q->desc[idx];
	dma_addr_t buf_addr;
	void *buf = e->buf;
	int buf_len = SKB_WITH_OVERHEAD(q->buf_size);

	buf_addr = le32_to_cpu(READ_ONCE(desc->buf0));
	if (len) {
		u32 ctl = le32_to_cpu(READ_ONCE(desc->ctrl));
		*len = FIELD_GET(MT_DMA_CTL_SD_LEN0, ctl);
		*more = !(ctl & MT_DMA_CTL_LAST_SEC0);
	}

	if (info)
		*info = le32_to_cpu(desc->info);

	dma_unmap_single(dev->dev, buf_addr, buf_len, DMA_FROM_DEVICE);
	e->buf = NULL;

	return buf;
}

static void *
mt76_dma_dequeue(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
		 int *len, u32 *info, bool *more)
{
	int idx = q->tail;

	*more = false;
	if (!q->queued)
		return NULL;

237 238 239
	if (flush)
		q->desc[idx].ctrl |= cpu_to_le32(MT_DMA_CTL_DMA_DONE);
	else if (!(q->desc[idx].ctrl & cpu_to_le32(MT_DMA_CTL_DMA_DONE)))
240 241 242 243 244 245 246 247
		return NULL;

	q->tail = (q->tail + 1) % q->ndesc;
	q->queued--;

	return mt76_dma_get_buf(dev, q, idx, len, info, more);
}

248 249 250 251
static int
mt76_dma_tx_queue_skb_raw(struct mt76_dev *dev, enum mt76_txq_id qid,
			  struct sk_buff *skb, u32 tx_info)
{
252
	struct mt76_queue *q = dev->q_tx[qid].q;
253 254 255
	struct mt76_queue_buf buf;
	dma_addr_t addr;

256 257 258
	if (q->queued + 1 >= q->ndesc - 1)
		goto error;

259 260
	addr = dma_map_single(dev->dev, skb->data, skb->len,
			      DMA_TO_DEVICE);
261
	if (unlikely(dma_mapping_error(dev->dev, addr)))
262
		goto error;
263 264 265 266 267 268 269 270 271 272

	buf.addr = addr;
	buf.len = skb->len;

	spin_lock_bh(&q->lock);
	mt76_dma_add_buf(dev, q, &buf, 1, tx_info, skb, NULL);
	mt76_dma_kick_queue(dev, q);
	spin_unlock_bh(&q->lock);

	return 0;
273 274 275 276

error:
	dev_kfree_skb(skb);
	return -ENOMEM;
277 278
}

279 280 281 282
static int
mt76_dma_tx_queue_skb(struct mt76_dev *dev, enum mt76_txq_id qid,
		      struct sk_buff *skb, struct mt76_wcid *wcid,
		      struct ieee80211_sta *sta)
283
{
284
	struct mt76_queue *q = dev->q_tx[qid].q;
285 286 287
	struct mt76_tx_info tx_info = {
		.skb = skb,
	};
288
	struct ieee80211_hw *hw;
289
	int len, n = 0, ret = -ENOMEM;
290 291 292 293
	struct mt76_queue_entry e;
	struct mt76_txwi_cache *t;
	struct sk_buff *iter;
	dma_addr_t addr;
294
	u8 *txwi;
295 296 297

	t = mt76_get_txwi(dev);
	if (!t) {
298 299
		hw = mt76_tx_status_get_hw(dev, skb);
		ieee80211_free_txskb(hw, skb);
300 301
		return -ENOMEM;
	}
302
	txwi = mt76_get_txwi_ptr(dev, t);
303

304
	skb->prev = skb->next = NULL;
305
	if (dev->drv->drv_flags & MT_DRV_TX_ALIGNED4_SKBS)
306 307
		mt76_insert_hdr_pad(skb);

308
	len = skb_headlen(skb);
309
	addr = dma_map_single(dev->dev, skb->data, len, DMA_TO_DEVICE);
310
	if (unlikely(dma_mapping_error(dev->dev, addr)))
311 312
		goto free;

313 314 315 316
	tx_info.buf[n].addr = t->dma_addr;
	tx_info.buf[n++].len = dev->drv->txwi_size;
	tx_info.buf[n].addr = addr;
	tx_info.buf[n++].len = len;
317 318

	skb_walk_frags(skb, iter) {
319
		if (n == ARRAY_SIZE(tx_info.buf))
320 321 322 323
			goto unmap;

		addr = dma_map_single(dev->dev, iter->data, iter->len,
				      DMA_TO_DEVICE);
324
		if (unlikely(dma_mapping_error(dev->dev, addr)))
325 326
			goto unmap;

327 328
		tx_info.buf[n].addr = addr;
		tx_info.buf[n++].len = iter->len;
329
	}
330
	tx_info.nbuf = n;
331

332
	dma_sync_single_for_cpu(dev->dev, t->dma_addr, dev->drv->txwi_size,
333
				DMA_TO_DEVICE);
334
	ret = dev->drv->tx_prepare_skb(dev, txwi, qid, wcid, sta, &tx_info);
335
	dma_sync_single_for_device(dev->dev, t->dma_addr, dev->drv->txwi_size,
336 337
				   DMA_TO_DEVICE);
	if (ret < 0)
338 339
		goto unmap;

340
	if (q->queued + (tx_info.nbuf + 1) / 2 >= q->ndesc - 1) {
341 342 343 344
		ret = -ENOMEM;
		goto unmap;
	}

345
	return mt76_dma_add_buf(dev, q, tx_info.buf, tx_info.nbuf,
346
				tx_info.info, tx_info.skb, t);
347 348 349

unmap:
	for (n--; n > 0; n--)
350 351
		dma_unmap_single(dev->dev, tx_info.buf[n].addr,
				 tx_info.buf[n].len, DMA_TO_DEVICE);
352 353

free:
354 355 356 357 358 359
#ifdef CONFIG_NL80211_TESTMODE
	/* fix tx_done accounting on queue overflow */
	if (tx_info.skb == dev->test.tx_skb)
		dev->test.tx_done--;
#endif

360
	e.skb = tx_info.skb;
361
	e.txwi = t;
362
	dev->drv->tx_complete_skb(dev, qid, &e);
363 364 365 366
	mt76_put_txwi(dev, t);
	return ret;
}

367
static int
368
mt76_dma_rx_fill(struct mt76_dev *dev, struct mt76_queue *q)
369 370 371 372 373 374 375 376 377 378 379 380
{
	dma_addr_t addr;
	void *buf;
	int frames = 0;
	int len = SKB_WITH_OVERHEAD(q->buf_size);
	int offset = q->buf_offset;

	spin_lock_bh(&q->lock);

	while (q->queued < q->ndesc - 1) {
		struct mt76_queue_buf qbuf;

381
		buf = page_frag_alloc(&q->rx_page, q->buf_size, GFP_ATOMIC);
382 383 384 385
		if (!buf)
			break;

		addr = dma_map_single(dev->dev, buf, len, DMA_FROM_DEVICE);
386
		if (unlikely(dma_mapping_error(dev->dev, addr))) {
387 388 389 390 391 392
			skb_free_frag(buf);
			break;
		}

		qbuf.addr = addr + offset;
		qbuf.len = len - offset;
393
		mt76_dma_add_buf(dev, q, &qbuf, 1, 0, buf, NULL);
394 395 396 397 398 399 400 401 402 403 404 405 406 407
		frames++;
	}

	if (frames)
		mt76_dma_kick_queue(dev, q);

	spin_unlock_bh(&q->lock);

	return frames;
}

static void
mt76_dma_rx_cleanup(struct mt76_dev *dev, struct mt76_queue *q)
{
408
	struct page *page;
409 410 411 412 413 414 415 416 417 418 419 420
	void *buf;
	bool more;

	spin_lock_bh(&q->lock);
	do {
		buf = mt76_dma_dequeue(dev, q, true, NULL, NULL, &more);
		if (!buf)
			break;

		skb_free_frag(buf);
	} while (1);
	spin_unlock_bh(&q->lock);
421 422 423 424 425 426 427

	if (!q->rx_page.va)
		return;

	page = virt_to_page(q->rx_page.va);
	__page_frag_cache_drain(page, q->rx_page.pagecnt_bias);
	memset(&q->rx_page, 0, sizeof(q->rx_page));
428 429 430 431 432 433 434 435 436
}

static void
mt76_dma_rx_reset(struct mt76_dev *dev, enum mt76_rxq_id qid)
{
	struct mt76_queue *q = &dev->q_rx[qid];
	int i;

	for (i = 0; i < q->ndesc; i++)
437
		q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE);
438 439 440

	mt76_dma_rx_cleanup(dev, q);
	mt76_dma_sync_idx(dev, q);
441
	mt76_dma_rx_fill(dev, q);
442 443 444 445 446 447

	if (!q->rx_head)
		return;

	dev_kfree_skb(q->rx_head);
	q->rx_head = NULL;
448 449 450 451 452 453 454 455 456
}

static void
mt76_add_fragment(struct mt76_dev *dev, struct mt76_queue *q, void *data,
		  int len, bool more)
{
	struct page *page = virt_to_head_page(data);
	int offset = data - page_address(page);
	struct sk_buff *skb = q->rx_head;
457
	struct skb_shared_info *shinfo = skb_shinfo(skb);
458

459 460 461 462 463
	if (shinfo->nr_frags < ARRAY_SIZE(shinfo->frags)) {
		offset += q->buf_offset;
		skb_add_rx_frag(skb, shinfo->nr_frags, page, offset, len,
				q->buf_size);
	}
464 465 466 467 468 469 470 471 472 473 474

	if (more)
		return;

	q->rx_head = NULL;
	dev->drv->rx_skb(dev, q - dev->q_rx, skb);
}

static int
mt76_dma_rx_process(struct mt76_dev *dev, struct mt76_queue *q, int budget)
{
475
	int len, data_len, done = 0;
476 477 478 479 480 481 482 483 484 485 486
	struct sk_buff *skb;
	unsigned char *data;
	bool more;

	while (done < budget) {
		u32 info;

		data = mt76_dma_dequeue(dev, q, false, &len, &info, &more);
		if (!data)
			break;

487 488 489 490 491 492
		if (q->rx_head)
			data_len = q->buf_size;
		else
			data_len = SKB_WITH_OVERHEAD(q->buf_size);

		if (data_len < len + q->buf_offset) {
493 494 495 496 497 498 499
			dev_kfree_skb(q->rx_head);
			q->rx_head = NULL;

			skb_free_frag(data);
			continue;
		}

500 501 502 503 504 505 506 507 508 509 510 511 512
		if (q->rx_head) {
			mt76_add_fragment(dev, q, data, len, more);
			continue;
		}

		skb = build_skb(data, q->buf_size);
		if (!skb) {
			skb_free_frag(data);
			continue;
		}
		skb_reserve(skb, q->buf_offset);

		if (q == &dev->q_rx[MT_RXQ_MCU]) {
R
Ryder Lee 已提交
513
			u32 *rxfce = (u32 *)skb->cb;
514 515 516 517 518 519 520 521 522 523 524 525 526 527
			*rxfce = info;
		}

		__skb_put(skb, len);
		done++;

		if (more) {
			q->rx_head = skb;
			continue;
		}

		dev->drv->rx_skb(dev, q - dev->q_rx, skb);
	}

528
	mt76_dma_rx_fill(dev, q);
529 530 531 532 533 534 535
	return done;
}

static int
mt76_dma_rx_poll(struct napi_struct *napi, int budget)
{
	struct mt76_dev *dev;
536
	int qid, done = 0, cur;
537 538 539 540

	dev = container_of(napi->dev, struct mt76_dev, napi_dev);
	qid = napi - dev->napi;

541
	local_bh_disable();
542 543
	rcu_read_lock();

544 545
	do {
		cur = mt76_dma_rx_process(dev, &dev->q_rx[qid], budget - done);
546
		mt76_rx_poll_complete(dev, qid, napi);
547 548 549
		done += cur;
	} while (cur && done < budget);

550
	rcu_read_unlock();
551
	local_bh_enable();
552

553
	if (done < budget && napi_complete(napi))
554 555 556 557 558 559 560 561 562 563 564 565
		dev->drv->rx_poll_complete(dev, qid);

	return done;
}

static int
mt76_dma_init(struct mt76_dev *dev)
{
	int i;

	init_dummy_netdev(&dev->napi_dev);

566
	mt76_for_each_q_rx(dev, i) {
567 568
		netif_napi_add(&dev->napi_dev, &dev->napi[i], mt76_dma_rx_poll,
			       64);
569
		mt76_dma_rx_fill(dev, &dev->q_rx[i]);
570 571 572 573 574 575 576 577 578
		napi_enable(&dev->napi[i]);
	}

	return 0;
}

static const struct mt76_queue_ops mt76_dma_ops = {
	.init = mt76_dma_init,
	.alloc = mt76_dma_alloc_queue,
579
	.tx_queue_skb_raw = mt76_dma_tx_queue_skb_raw,
580
	.tx_queue_skb = mt76_dma_tx_queue_skb,
581 582 583 584 585
	.tx_cleanup = mt76_dma_tx_cleanup,
	.rx_reset = mt76_dma_rx_reset,
	.kick = mt76_dma_kick_queue,
};

586
void mt76_dma_attach(struct mt76_dev *dev)
587 588 589 590 591 592 593 594 595
{
	dev->queue_ops = &mt76_dma_ops;
}
EXPORT_SYMBOL_GPL(mt76_dma_attach);

void mt76_dma_cleanup(struct mt76_dev *dev)
{
	int i;

596
	netif_napi_del(&dev->tx_napi);
597 598 599
	for (i = 0; i < ARRAY_SIZE(dev->q_tx); i++)
		mt76_dma_tx_cleanup(dev, i, true);

600
	mt76_for_each_q_rx(dev, i) {
601 602 603 604 605
		netif_napi_del(&dev->napi[i]);
		mt76_dma_rx_cleanup(dev, &dev->q_rx[i]);
	}
}
EXPORT_SYMBOL_GPL(mt76_dma_cleanup);