aq_ring.c 14.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5
/* Atlantic Network Driver
 *
 * Copyright (C) 2014-2019 aQuantia Corporation
 * Copyright (C) 2019-2020 Marvell International Ltd.
6 7 8 9 10 11 12
 */

/* File aq_ring.c: Definition of functions for Rx/Tx rings. */

#include "aq_ring.h"
#include "aq_nic.h"
#include "aq_hw.h"
13
#include "aq_hw_utils.h"
14
#include "aq_ptp.h"
15 16 17 18

#include <linux/netdevice.h>
#include <linux/etherdevice.h>

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
static inline void aq_free_rxpage(struct aq_rxpage *rxpage, struct device *dev)
{
	unsigned int len = PAGE_SIZE << rxpage->order;

	dma_unmap_page(dev, rxpage->daddr, len, DMA_FROM_DEVICE);

	/* Drop the ref for being in the ring. */
	__free_pages(rxpage->page, rxpage->order);
	rxpage->page = NULL;
}

static int aq_get_rxpage(struct aq_rxpage *rxpage, unsigned int order,
			 struct device *dev)
{
	struct page *page;
	int ret = -ENOMEM;
N
Nikita Danilov 已提交
35
	dma_addr_t daddr;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

	page = dev_alloc_pages(order);
	if (unlikely(!page))
		goto err_exit;

	daddr = dma_map_page(dev, page, 0, PAGE_SIZE << order,
			     DMA_FROM_DEVICE);

	if (unlikely(dma_mapping_error(dev, daddr)))
		goto free_page;

	rxpage->page = page;
	rxpage->daddr = daddr;
	rxpage->order = order;
	rxpage->pg_off = 0;

	return 0;

free_page:
	__free_pages(page, order);

err_exit:
	return ret;
}

static int aq_get_rxpages(struct aq_ring_s *self, struct aq_ring_buff_s *rxbuf,
			  int order)
{
	int ret;

	if (rxbuf->rxdata.page) {
		/* One means ring is the only user and can reuse */
		if (page_ref_count(rxbuf->rxdata.page) > 1) {
			/* Try reuse buffer */
			rxbuf->rxdata.pg_off += AQ_CFG_RX_FRAME_MAX;
			if (rxbuf->rxdata.pg_off + AQ_CFG_RX_FRAME_MAX <=
				(PAGE_SIZE << order)) {
73
				u64_stats_update_begin(&self->stats.rx.syncp);
74
				self->stats.rx.pg_flips++;
75
				u64_stats_update_end(&self->stats.rx.syncp);
76 77 78 79 80 81
			} else {
				/* Buffer exhausted. We have other users and
				 * should release this page and realloc
				 */
				aq_free_rxpage(&rxbuf->rxdata,
					       aq_nic_get_dev(self->aq_nic));
82
				u64_stats_update_begin(&self->stats.rx.syncp);
83
				self->stats.rx.pg_losts++;
84
				u64_stats_update_end(&self->stats.rx.syncp);
85 86 87
			}
		} else {
			rxbuf->rxdata.pg_off = 0;
88
			u64_stats_update_begin(&self->stats.rx.syncp);
89
			self->stats.rx.pg_reuses++;
90
			u64_stats_update_end(&self->stats.rx.syncp);
91 92 93 94 95 96
		}
	}

	if (!rxbuf->rxdata.page) {
		ret = aq_get_rxpage(&rxbuf->rxdata, order,
				    aq_nic_get_dev(self->aq_nic));
97 98 99 100 101
		if (ret) {
			u64_stats_update_begin(&self->stats.rx.syncp);
			self->stats.rx.alloc_fails++;
			u64_stats_update_end(&self->stats.rx.syncp);
		}
102 103 104 105 106 107
		return ret;
	}

	return 0;
}

108 109 110 111 112 113 114 115 116 117 118 119 120
static struct aq_ring_s *aq_ring_alloc(struct aq_ring_s *self,
				       struct aq_nic_s *aq_nic)
{
	int err = 0;

	self->buff_ring =
		kcalloc(self->size, sizeof(struct aq_ring_buff_s), GFP_KERNEL);

	if (!self->buff_ring) {
		err = -ENOMEM;
		goto err_exit;
	}
	self->dx_ring = dma_alloc_coherent(aq_nic_get_dev(aq_nic),
121 122
					   self->size * self->dx_size,
					   &self->dx_ring_pa, GFP_KERNEL);
123 124 125 126 127 128 129 130 131 132
	if (!self->dx_ring) {
		err = -ENOMEM;
		goto err_exit;
	}

err_exit:
	if (err < 0) {
		aq_ring_free(self);
		self = NULL;
	}
N
Nikita Danilov 已提交
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	return self;
}

struct aq_ring_s *aq_ring_tx_alloc(struct aq_ring_s *self,
				   struct aq_nic_s *aq_nic,
				   unsigned int idx,
				   struct aq_nic_cfg_s *aq_nic_cfg)
{
	int err = 0;

	self->aq_nic = aq_nic;
	self->idx = idx;
	self->size = aq_nic_cfg->txds;
	self->dx_size = aq_nic_cfg->aq_hw_caps->txd_size;

	self = aq_ring_alloc(self, aq_nic);
	if (!self) {
		err = -ENOMEM;
		goto err_exit;
	}

err_exit:
	if (err < 0) {
		aq_ring_free(self);
		self = NULL;
	}
N
Nikita Danilov 已提交
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174
	return self;
}

struct aq_ring_s *aq_ring_rx_alloc(struct aq_ring_s *self,
				   struct aq_nic_s *aq_nic,
				   unsigned int idx,
				   struct aq_nic_cfg_s *aq_nic_cfg)
{
	int err = 0;

	self->aq_nic = aq_nic;
	self->idx = idx;
	self->size = aq_nic_cfg->rxds;
	self->dx_size = aq_nic_cfg->aq_hw_caps->rxd_size;
175 176 177 178 179
	self->page_order = fls(AQ_CFG_RX_FRAME_MAX / PAGE_SIZE +
			       (AQ_CFG_RX_FRAME_MAX % PAGE_SIZE ? 1 : 0)) - 1;

	if (aq_nic_cfg->rxpageorder > self->page_order)
		self->page_order = aq_nic_cfg->rxpageorder;
180 181 182 183 184 185 186 187 188 189 190 191

	self = aq_ring_alloc(self, aq_nic);
	if (!self) {
		err = -ENOMEM;
		goto err_exit;
	}

err_exit:
	if (err < 0) {
		aq_ring_free(self);
		self = NULL;
	}
N
Nikita Danilov 已提交
192

193 194 195
	return self;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
struct aq_ring_s *
aq_ring_hwts_rx_alloc(struct aq_ring_s *self, struct aq_nic_s *aq_nic,
		      unsigned int idx, unsigned int size, unsigned int dx_size)
{
	struct device *dev = aq_nic_get_dev(aq_nic);
	size_t sz = size * dx_size + AQ_CFG_RXDS_DEF;

	memset(self, 0, sizeof(*self));

	self->aq_nic = aq_nic;
	self->idx = idx;
	self->size = size;
	self->dx_size = dx_size;

	self->dx_ring = dma_alloc_coherent(dev, sz, &self->dx_ring_pa,
					   GFP_KERNEL);
	if (!self->dx_ring) {
		aq_ring_free(self);
		return NULL;
	}

	return self;
}

220
int aq_ring_init(struct aq_ring_s *self, const enum atl_ring_type ring_type)
221 222 223 224
{
	self->hw_head = 0;
	self->sw_head = 0;
	self->sw_tail = 0;
225
	self->ring_type = ring_type;
N
Nikita Danilov 已提交
226

227 228 229 230 231
	if (self->ring_type == ATL_RING_RX)
		u64_stats_init(&self->stats.rx.syncp);
	else
		u64_stats_init(&self->stats.tx.syncp);

232 233 234
	return 0;
}

P
Pavel Belous 已提交
235 236 237 238 239 240
static inline bool aq_ring_dx_in_range(unsigned int h, unsigned int i,
				       unsigned int t)
{
	return (h < t) ? ((h < i) && (i < t)) : ((h < i) || (i < t));
}

I
Igor Russkikh 已提交
241 242 243 244 245 246 247 248 249 250 251 252
void aq_ring_update_queue_state(struct aq_ring_s *ring)
{
	if (aq_ring_avail_dx(ring) <= AQ_CFG_SKB_FRAGS_MAX)
		aq_ring_queue_stop(ring);
	else if (aq_ring_avail_dx(ring) > AQ_CFG_RESTART_DESC_THRES)
		aq_ring_queue_wake(ring);
}

void aq_ring_queue_wake(struct aq_ring_s *ring)
{
	struct net_device *ndev = aq_nic_get_ndev(ring->aq_nic);

253 254 255 256 257
	if (__netif_subqueue_stopped(ndev,
				     AQ_NIC_RING2QMAP(ring->aq_nic,
						      ring->idx))) {
		netif_wake_subqueue(ndev,
				    AQ_NIC_RING2QMAP(ring->aq_nic, ring->idx));
258
		u64_stats_update_begin(&ring->stats.tx.syncp);
I
Igor Russkikh 已提交
259
		ring->stats.tx.queue_restarts++;
260
		u64_stats_update_end(&ring->stats.tx.syncp);
I
Igor Russkikh 已提交
261 262 263 264 265 266 267
	}
}

void aq_ring_queue_stop(struct aq_ring_s *ring)
{
	struct net_device *ndev = aq_nic_get_ndev(ring->aq_nic);

268 269 270 271 272
	if (!__netif_subqueue_stopped(ndev,
				      AQ_NIC_RING2QMAP(ring->aq_nic,
						       ring->idx)))
		netif_stop_subqueue(ndev,
				    AQ_NIC_RING2QMAP(ring->aq_nic, ring->idx));
I
Igor Russkikh 已提交
273 274
}

275
bool aq_ring_tx_clean(struct aq_ring_s *self)
276 277
{
	struct device *dev = aq_nic_get_dev(self->aq_nic);
278
	unsigned int budget;
279

280 281
	for (budget = AQ_CFG_TX_CLEAN_BUDGET;
	     budget && self->sw_head != self->hw_head; budget--) {
282 283 284
		struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];

		if (likely(buff->is_mapped)) {
P
Pavel Belous 已提交
285 286 287 288 289 290 291 292
			if (unlikely(buff->is_sop)) {
				if (!buff->is_eop &&
				    buff->eop_index != 0xffffU &&
				    (!aq_ring_dx_in_range(self->sw_head,
						buff->eop_index,
						self->hw_head)))
					break;

293 294
				dma_unmap_single(dev, buff->pa, buff->len,
						 DMA_TO_DEVICE);
P
Pavel Belous 已提交
295
			} else {
296 297
				dma_unmap_page(dev, buff->pa, buff->len,
					       DMA_TO_DEVICE);
P
Pavel Belous 已提交
298
			}
299 300
		}

301
		if (unlikely(buff->is_eop && buff->skb)) {
302
			u64_stats_update_begin(&self->stats.tx.syncp);
303
			++self->stats.tx.packets;
304
			self->stats.tx.bytes += buff->skb->len;
305
			u64_stats_update_end(&self->stats.tx.syncp);
306

307
			dev_kfree_skb_any(buff->skb);
308
			buff->skb = NULL;
309
		}
P
Pavel Belous 已提交
310 311
		buff->pa = 0U;
		buff->eop_index = 0xffffU;
312
		self->sw_head = aq_ring_next_dx(self, self->sw_head);
P
Pavel Belous 已提交
313
	}
314 315

	return !!budget;
316 317
}

318 319 320 321 322 323 324 325
static void aq_rx_checksum(struct aq_ring_s *self,
			   struct aq_ring_buff_s *buff,
			   struct sk_buff *skb)
{
	if (!(self->aq_nic->ndev->features & NETIF_F_RXCSUM))
		return;

	if (unlikely(buff->is_cso_err)) {
326
		u64_stats_update_begin(&self->stats.rx.syncp);
327
		++self->stats.rx.errors;
328
		u64_stats_update_end(&self->stats.rx.syncp);
329 330 331 332 333 334 335 336
		skb->ip_summed = CHECKSUM_NONE;
		return;
	}
	if (buff->is_ip_cso) {
		__skb_incr_checksum_unnecessary(skb);
	} else {
		skb->ip_summed = CHECKSUM_NONE;
	}
337 338 339

	if (buff->is_udp_cso || buff->is_tcp_cso)
		__skb_incr_checksum_unnecessary(skb);
340 341
}

342
#define AQ_SKB_ALIGN SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
343 344 345 346
int aq_ring_rx_clean(struct aq_ring_s *self,
		     struct napi_struct *napi,
		     int *work_done,
		     int budget)
347 348 349
{
	struct net_device *ndev = aq_nic_get_ndev(self->aq_nic);
	bool is_rsc_completed = true;
350
	int err = 0;
351 352 353 354 355

	for (; (self->sw_head != self->hw_head) && budget;
		self->sw_head = aq_ring_next_dx(self, self->sw_head),
		--budget, ++(*work_done)) {
		struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];
356
		bool is_ptp_ring = aq_ptp_ring(self->aq_nic, self);
357
		struct aq_ring_buff_s *buff_ = NULL;
358 359 360
		struct sk_buff *skb = NULL;
		unsigned int next_ = 0U;
		unsigned int i = 0U;
361
		u16 hdr_len;
362 363 364 365 366

		if (buff->is_cleaned)
			continue;

		if (!buff->is_eop) {
367 368 369 370
			buff_ = buff;
			do {
				next_ = buff_->next,
				buff_ = &self->buff_ring[next_];
371 372 373 374 375
				is_rsc_completed =
					aq_ring_dx_in_range(self->sw_head,
							    next_,
							    self->hw_head);

376
				if (unlikely(!is_rsc_completed))
377 378
					break;

379
				buff->is_error |= buff_->is_error;
380
				buff->is_cso_err |= buff_->is_cso_err;
381 382

			} while (!buff_->is_eop);
383 384 385 386 387

			if (!is_rsc_completed) {
				err = 0;
				goto err_exit;
			}
388 389
			if (buff->is_error ||
			    (buff->is_lro && buff->is_cso_err)) {
390 391 392 393 394 395 396 397
				buff_ = buff;
				do {
					next_ = buff_->next,
					buff_ = &self->buff_ring[next_];

					buff_->is_cleaned = true;
				} while (!buff_->is_eop);

398
				u64_stats_update_begin(&self->stats.rx.syncp);
399
				++self->stats.rx.errors;
400
				u64_stats_update_end(&self->stats.rx.syncp);
401 402 403 404 405
				continue;
			}
		}

		if (buff->is_error) {
406
			u64_stats_update_begin(&self->stats.rx.syncp);
407
			++self->stats.rx.errors;
408
			u64_stats_update_end(&self->stats.rx.syncp);
409
			continue;
410 411
		}

412 413 414 415 416
		dma_sync_single_range_for_cpu(aq_nic_get_dev(self->aq_nic),
					      buff->rxdata.daddr,
					      buff->rxdata.pg_off,
					      buff->len, DMA_FROM_DEVICE);

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
		skb = napi_alloc_skb(napi, AQ_CFG_RX_HDR_SIZE);
		if (unlikely(!skb)) {
			u64_stats_update_begin(&self->stats.rx.syncp);
			self->stats.rx.skb_alloc_fails++;
			u64_stats_update_end(&self->stats.rx.syncp);
			err = -ENOMEM;
			goto err_exit;
		}
		if (is_ptp_ring)
			buff->len -=
				aq_ptp_extract_ts(self->aq_nic, skb,
						  aq_buf_vaddr(&buff->rxdata),
						  buff->len);

		hdr_len = buff->len;
		if (hdr_len > AQ_CFG_RX_HDR_SIZE)
			hdr_len = eth_get_headlen(skb->dev,
						  aq_buf_vaddr(&buff->rxdata),
						  AQ_CFG_RX_HDR_SIZE);

		memcpy(__skb_put(skb, hdr_len), aq_buf_vaddr(&buff->rxdata),
		       ALIGN(hdr_len, sizeof(long)));

		if (buff->len - hdr_len > 0) {
			skb_add_rx_frag(skb, 0, buff->rxdata.page,
					buff->rxdata.pg_off + hdr_len,
					buff->len - hdr_len,
444
					AQ_CFG_RX_FRAME_MAX);
445
			page_ref_inc(buff->rxdata.page);
446
		}
447

448 449 450 451 452 453
		if (!buff->is_eop) {
			buff_ = buff;
			i = 1U;
			do {
				next_ = buff_->next;
				buff_ = &self->buff_ring[next_];
454

455 456 457 458 459 460 461 462 463 464 465 466
				dma_sync_single_range_for_cpu(aq_nic_get_dev(self->aq_nic),
							      buff_->rxdata.daddr,
							      buff_->rxdata.pg_off,
							      buff_->len,
							      DMA_FROM_DEVICE);
				skb_add_rx_frag(skb, i++,
						buff_->rxdata.page,
						buff_->rxdata.pg_off,
						buff_->len,
						AQ_CFG_RX_FRAME_MAX);
				page_ref_inc(buff_->rxdata.page);
				buff_->is_cleaned = 1;
467

468 469 470 471 472 473
				buff->is_ip_cso &= buff_->is_ip_cso;
				buff->is_udp_cso &= buff_->is_udp_cso;
				buff->is_tcp_cso &= buff_->is_tcp_cso;
				buff->is_cso_err |= buff_->is_cso_err;

			} while (!buff_->is_eop);
474 475
		}

476 477 478 479
		if (buff->is_vlan)
			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
					       buff->vlan_rx_tag);

480
		skb->protocol = eth_type_trans(skb, ndev);
481 482

		aq_rx_checksum(self, buff, skb);
483 484 485 486

		skb_set_hash(skb, buff->rss_hash,
			     buff->is_hash_l4 ? PKT_HASH_TYPE_L4 :
			     PKT_HASH_TYPE_NONE);
487
		/* Send all PTP traffic to 0 queue */
488 489 490 491
		skb_record_rx_queue(skb,
				    is_ptp_ring ? 0
						: AQ_NIC_RING2QMAP(self->aq_nic,
								   self->idx));
492

493
		u64_stats_update_begin(&self->stats.rx.syncp);
494 495
		++self->stats.rx.packets;
		self->stats.rx.bytes += skb->len;
496
		u64_stats_update_end(&self->stats.rx.syncp);
497 498

		napi_gro_receive(napi, skb);
499 500 501 502 503 504
	}

err_exit:
	return err;
}

505 506
void aq_ring_hwts_rx_clean(struct aq_ring_s *self, struct aq_nic_s *aq_nic)
{
507
#if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
508 509 510 511 512 513 514 515 516 517 518
	while (self->sw_head != self->hw_head) {
		u64 ns;

		aq_nic->aq_hw_ops->extract_hwts(aq_nic->aq_hw,
						self->dx_ring +
						(self->sw_head * self->dx_size),
						self->dx_size, &ns);
		aq_ptp_tx_hwtstamp(aq_nic, ns);

		self->sw_head = aq_ring_next_dx(self, self->sw_head);
	}
519
#endif
520 521
}

522 523
int aq_ring_rx_fill(struct aq_ring_s *self)
{
524
	unsigned int page_order = self->page_order;
525 526 527 528
	struct aq_ring_buff_s *buff = NULL;
	int err = 0;
	int i = 0;

529 530 531 532
	if (aq_ring_avail_dx(self) < min_t(unsigned int, AQ_CFG_RX_REFILL_THRES,
					   self->size / 2))
		return err;

533 534 535 536 537 538 539
	for (i = aq_ring_avail_dx(self); i--;
		self->sw_tail = aq_ring_next_dx(self, self->sw_tail)) {
		buff = &self->buff_ring[self->sw_tail];

		buff->flags = 0U;
		buff->len = AQ_CFG_RX_FRAME_MAX;

540 541
		err = aq_get_rxpages(self, buff, page_order);
		if (err)
542 543
			goto err_exit;

544
		buff->pa = aq_buf_daddr(&buff->rxdata);
545 546 547 548 549 550 551 552 553 554
		buff = NULL;
	}

err_exit:
	return err;
}

void aq_ring_rx_deinit(struct aq_ring_s *self)
{
	if (!self)
555
		return;
556 557 558 559 560

	for (; self->sw_head != self->sw_tail;
		self->sw_head = aq_ring_next_dx(self, self->sw_head)) {
		struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];

561
		aq_free_rxpage(&buff->rxdata, aq_nic_get_dev(self->aq_nic));
562 563 564 565 566 567
	}
}

void aq_ring_free(struct aq_ring_s *self)
{
	if (!self)
568
		return;
569 570 571 572 573 574 575

	kfree(self->buff_ring);

	if (self->dx_ring)
		dma_free_coherent(aq_nic_get_dev(self->aq_nic),
				  self->size * self->dx_size, self->dx_ring,
				  self->dx_ring_pa);
576 577 578 579
}

unsigned int aq_ring_fill_stats_data(struct aq_ring_s *self, u64 *data)
{
580 581
	unsigned int count;
	unsigned int start;
582 583 584

	if (self->ring_type == ATL_RING_RX) {
		/* This data should mimic aq_ethtool_queue_rx_stat_names structure */
585 586 587 588 589 590 591
		do {
			count = 0;
			start = u64_stats_fetch_begin_irq(&self->stats.rx.syncp);
			data[count] = self->stats.rx.packets;
			data[++count] = self->stats.rx.jumbo_packets;
			data[++count] = self->stats.rx.lro_packets;
			data[++count] = self->stats.rx.errors;
592 593 594
			data[++count] = self->stats.rx.alloc_fails;
			data[++count] = self->stats.rx.skb_alloc_fails;
			data[++count] = self->stats.rx.polls;
595
		} while (u64_stats_fetch_retry_irq(&self->stats.rx.syncp, start));
596 597
	} else {
		/* This data should mimic aq_ethtool_queue_tx_stat_names structure */
598 599 600 601 602 603
		do {
			count = 0;
			start = u64_stats_fetch_begin_irq(&self->stats.tx.syncp);
			data[count] = self->stats.tx.packets;
			data[++count] = self->stats.tx.queue_restarts;
		} while (u64_stats_fetch_retry_irq(&self->stats.tx.syncp, start));
604
	}
605

606
	return ++count;
607
}