aq_nic.c 23.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * aQuantia Corporation Network Driver
 * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 */

/* File aq_nic.c: Definition of common code for NIC. */

#include "aq_nic.h"
#include "aq_ring.h"
#include "aq_vec.h"
#include "aq_hw.h"
#include "aq_pci_func.h"

18
#include <linux/moduleparam.h>
19 20 21 22 23 24 25 26
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/timer.h>
#include <linux/cpu.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/ip.h>

27 28 29 30 31 32 33 34 35 36 37 38
static unsigned int aq_itr = AQ_CFG_INTERRUPT_MODERATION_AUTO;
module_param_named(aq_itr, aq_itr, uint, 0644);
MODULE_PARM_DESC(aq_itr, "Interrupt throttling mode");

static unsigned int aq_itr_tx;
module_param_named(aq_itr_tx, aq_itr_tx, uint, 0644);
MODULE_PARM_DESC(aq_itr_tx, "TX interrupt throttle rate");

static unsigned int aq_itr_rx;
module_param_named(aq_itr_rx, aq_itr_rx, uint, 0644);
MODULE_PARM_DESC(aq_itr_rx, "RX interrupt throttle rate");

39 40
static void aq_nic_update_ndev_stats(struct aq_nic_s *self);

41 42 43 44 45 46
static void aq_nic_rss_init(struct aq_nic_s *self, unsigned int num_rss_queues)
{
	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
	struct aq_rss_parameters *rss_params = &cfg->aq_rss;
	int i = 0;

47
	static u8 rss_key[AQ_CFG_RSS_HASHKEY_SIZE] = {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
		0x1e, 0xad, 0x71, 0x87, 0x65, 0xfc, 0x26, 0x7d,
		0x0d, 0x45, 0x67, 0x74, 0xcd, 0x06, 0x1a, 0x18,
		0xb6, 0xc1, 0xf0, 0xc7, 0xbb, 0x18, 0xbe, 0xf8,
		0x19, 0x13, 0x4b, 0xa9, 0xd0, 0x3e, 0xfe, 0x70,
		0x25, 0x03, 0xab, 0x50, 0x6a, 0x8b, 0x82, 0x0c
	};

	rss_params->hash_secret_key_size = sizeof(rss_key);
	memcpy(rss_params->hash_secret_key, rss_key, sizeof(rss_key));
	rss_params->indirection_table_size = AQ_CFG_RSS_INDIRECTION_TABLE_MAX;

	for (i = rss_params->indirection_table_size; i--;)
		rss_params->indirection_table[i] = i & (num_rss_queues - 1);
}

63 64
/* Checks hw_caps and 'corrects' aq_nic_cfg in runtime */
void aq_nic_cfg_start(struct aq_nic_s *self)
65 66 67 68 69 70 71
{
	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;

	cfg->tcs = AQ_CFG_TCS_DEF;

	cfg->is_polling = AQ_CFG_IS_POLLING_DEF;

72 73 74
	cfg->itr = aq_itr;
	cfg->tx_itr = aq_itr_tx;
	cfg->rx_itr = aq_itr_rx;
75 76 77 78 79 80 81 82 83 84 85 86 87

	cfg->is_rss = AQ_CFG_IS_RSS_DEF;
	cfg->num_rss_queues = AQ_CFG_NUM_RSS_QUEUES_DEF;
	cfg->aq_rss.base_cpu_number = AQ_CFG_RSS_BASE_CPU_NUM_DEF;
	cfg->flow_control = AQ_CFG_FC_MODE;

	cfg->mtu = AQ_CFG_MTU_DEF;
	cfg->link_speed_msk = AQ_CFG_SPEED_MSK;
	cfg->is_autoneg = AQ_CFG_IS_AUTONEG_DEF;

	cfg->is_lro = AQ_CFG_IS_LRO_DEF;

	/*descriptors */
88 89
	cfg->rxds = min(cfg->aq_hw_caps->rxds_max, AQ_CFG_RXDS_DEF);
	cfg->txds = min(cfg->aq_hw_caps->txds_max, AQ_CFG_TXDS_DEF);
90 91

	/*rss rings */
92
	cfg->vecs = min(cfg->aq_hw_caps->vecs, AQ_CFG_VECS_DEF);
93
	cfg->vecs = min(cfg->vecs, num_online_cpus());
94
	cfg->vecs = min(cfg->vecs, self->irqvecs);
95 96 97 98 99 100 101 102 103 104
	/* cfg->vecs should be power of 2 for RSS */
	if (cfg->vecs >= 8U)
		cfg->vecs = 8U;
	else if (cfg->vecs >= 4U)
		cfg->vecs = 4U;
	else if (cfg->vecs >= 2U)
		cfg->vecs = 2U;
	else
		cfg->vecs = 1U;

105 106
	cfg->num_rss_queues = min(cfg->vecs, AQ_CFG_NUM_RSS_QUEUES_DEF);

107 108
	aq_nic_rss_init(self, cfg->num_rss_queues);

109
	cfg->irq_type = aq_pci_func_get_irq_type(self);
110 111

	if ((cfg->irq_type == AQ_HW_IRQ_LEGACY) ||
112
	    (cfg->aq_hw_caps->vecs == 1U) ||
113 114 115 116 117
	    (cfg->vecs == 1U)) {
		cfg->is_rss = 0U;
		cfg->vecs = 1U;
	}

118
	cfg->link_speed_msk &= cfg->aq_hw_caps->link_speed_msk;
119
	cfg->features = cfg->aq_hw_caps->hw_features;
120 121
}

I
Igor Russkikh 已提交
122 123
static int aq_nic_update_link_status(struct aq_nic_s *self)
{
124
	int err = self->aq_fw_ops->update_link_status(self->aq_hw);
125
	u32 fc = 0;
I
Igor Russkikh 已提交
126 127 128 129

	if (err)
		return err;

130
	if (self->link_status.mbps != self->aq_hw->aq_link_status.mbps) {
I
Igor Russkikh 已提交
131 132 133
		pr_info("%s: link change old %d new %d\n",
			AQ_CFG_DRV_NAME, self->link_status.mbps,
			self->aq_hw->aq_link_status.mbps);
134
		aq_nic_update_interrupt_moderation_settings(self);
135 136 137 138 139 140 141 142 143

		/* Driver has to update flow control settings on RX block
		 * on any link event.
		 * We should query FW whether it negotiated FC.
		 */
		if (self->aq_fw_ops->get_flow_control)
			self->aq_fw_ops->get_flow_control(self->aq_hw, &fc);
		if (self->aq_hw_ops->hw_set_fc)
			self->aq_hw_ops->hw_set_fc(self->aq_hw, fc, 0);
144
	}
I
Igor Russkikh 已提交
145 146 147

	self->link_status = self->aq_hw->aq_link_status;
	if (!netif_carrier_ok(self->ndev) && self->link_status.mbps) {
148
		aq_utils_obj_set(&self->flags,
I
Igor Russkikh 已提交
149
				 AQ_NIC_FLAG_STARTED);
150
		aq_utils_obj_clear(&self->flags,
I
Igor Russkikh 已提交
151 152 153 154 155 156 157
				   AQ_NIC_LINK_DOWN);
		netif_carrier_on(self->ndev);
		netif_tx_wake_all_queues(self->ndev);
	}
	if (netif_carrier_ok(self->ndev) && !self->link_status.mbps) {
		netif_carrier_off(self->ndev);
		netif_tx_disable(self->ndev);
158
		aq_utils_obj_set(&self->flags, AQ_NIC_LINK_DOWN);
I
Igor Russkikh 已提交
159 160 161 162
	}
	return 0;
}

163
static void aq_nic_service_timer_cb(struct timer_list *t)
164
{
165
	struct aq_nic_s *self = from_timer(self, t, service_timer);
166
	int ctimer = AQ_CFG_SERVICE_TIMER_INTERVAL;
167 168
	int err = 0;

169
	if (aq_utils_obj_test(&self->flags, AQ_NIC_FLAGS_IS_NOT_READY))
170 171
		goto err_exit;

I
Igor Russkikh 已提交
172 173
	err = aq_nic_update_link_status(self);
	if (err)
174 175
		goto err_exit;

176 177
	if (self->aq_fw_ops->update_stats)
		self->aq_fw_ops->update_stats(self->aq_hw);
178

179
	aq_nic_update_ndev_stats(self);
180

181 182 183
	/* If no link - use faster timer rate to detect link up asap */
	if (!netif_carrier_ok(self->ndev))
		ctimer = max(ctimer / 2, 1);
184 185

err_exit:
186
	mod_timer(&self->service_timer, jiffies + ctimer);
187 188
}

189
static void aq_nic_polling_timer_cb(struct timer_list *t)
190
{
191
	struct aq_nic_s *self = from_timer(self, t, polling_timer);
192 193 194 195 196 197 198 199
	struct aq_vec_s *aq_vec = NULL;
	unsigned int i = 0U;

	for (i = 0U, aq_vec = self->aq_vec[0];
		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
		aq_vec_isr(i, (void *)aq_vec);

	mod_timer(&self->polling_timer, jiffies +
200
		  AQ_CFG_POLLING_TIMER_INTERVAL);
201 202 203 204 205 206 207 208 209 210
}

int aq_nic_ndev_register(struct aq_nic_s *self)
{
	int err = 0;

	if (!self->ndev) {
		err = -EINVAL;
		goto err_exit;
	}
211

212 213 214 215 216
	err = hw_atl_utils_initfw(self->aq_hw, &self->aq_fw_ops);
	if (err)
		goto err_exit;

	err = self->aq_fw_ops->get_mac_permanent(self->aq_hw,
217
			    self->ndev->dev_addr);
218
	if (err)
219 220 221 222 223 224 225 226 227 228
		goto err_exit;

#if defined(AQ_CFG_MAC_ADDR_PERMANENT)
	{
		static u8 mac_addr_permanent[] = AQ_CFG_MAC_ADDR_PERMANENT;

		ether_addr_copy(self->ndev->dev_addr, mac_addr_permanent);
	}
#endif

229 230 231 232 233 234 235 236 237 238
	for (self->aq_vecs = 0; self->aq_vecs < aq_nic_get_cfg(self)->vecs;
	     self->aq_vecs++) {
		self->aq_vec[self->aq_vecs] =
		    aq_vec_alloc(self, self->aq_vecs, aq_nic_get_cfg(self));
		if (!self->aq_vec[self->aq_vecs]) {
			err = -ENOMEM;
			goto err_exit;
		}
	}

239 240
	netif_carrier_off(self->ndev);

I
Igor Russkikh 已提交
241
	netif_tx_disable(self->ndev);
242

243
	err = register_netdev(self->ndev);
244
	if (err)
245 246
		goto err_exit;

247 248 249 250
err_exit:
	return err;
}

251
void aq_nic_ndev_init(struct aq_nic_s *self)
252
{
253
	const struct aq_hw_caps_s *aq_hw_caps = self->aq_nic_cfg.aq_hw_caps;
254 255 256 257
	struct aq_nic_cfg_s *aq_nic_cfg = &self->aq_nic_cfg;

	self->ndev->hw_features |= aq_hw_caps->hw_features;
	self->ndev->features = aq_hw_caps->hw_features;
258 259
	self->ndev->vlan_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
				     NETIF_F_RXHASH | NETIF_F_SG | NETIF_F_LRO;
260
	self->ndev->priv_flags = aq_hw_caps->hw_priv_flags;
261 262
	self->ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;

263
	self->ndev->mtu = aq_nic_cfg->mtu - ETH_HLEN;
264
	self->ndev->max_mtu = aq_hw_caps->mtu - ETH_FCS_LEN - ETH_HLEN;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

}

void aq_nic_set_tx_ring(struct aq_nic_s *self, unsigned int idx,
			struct aq_ring_s *ring)
{
	self->aq_ring_tx[idx] = ring;
}

struct net_device *aq_nic_get_ndev(struct aq_nic_s *self)
{
	return self->ndev;
}

int aq_nic_init(struct aq_nic_s *self)
{
	struct aq_vec_s *aq_vec = NULL;
	int err = 0;
	unsigned int i = 0U;

	self->power_state = AQ_HW_POWER_STATE_D0;
286
	err = self->aq_hw_ops->hw_reset(self->aq_hw);
287 288 289
	if (err < 0)
		goto err_exit;

290
	err = self->aq_hw_ops->hw_init(self->aq_hw,
291
				       aq_nic_get_ndev(self)->dev_addr);
292 293 294 295 296
	if (err < 0)
		goto err_exit;

	for (i = 0U, aq_vec = self->aq_vec[0];
		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
297
		aq_vec_init(aq_vec, self->aq_hw_ops, self->aq_hw);
298

299 300
	netif_carrier_off(self->ndev);

301 302 303 304 305 306 307 308 309 310
err_exit:
	return err;
}

int aq_nic_start(struct aq_nic_s *self)
{
	struct aq_vec_s *aq_vec = NULL;
	int err = 0;
	unsigned int i = 0U;

311
	err = self->aq_hw_ops->hw_multicast_list_set(self->aq_hw,
312 313
						     self->mc_list.ar,
						     self->mc_list.count);
314 315 316
	if (err < 0)
		goto err_exit;

317
	err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw,
318
						    self->packet_filter);
319 320 321 322 323 324 325 326 327 328
	if (err < 0)
		goto err_exit;

	for (i = 0U, aq_vec = self->aq_vec[0];
		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
		err = aq_vec_start(aq_vec);
		if (err < 0)
			goto err_exit;
	}

329
	err = self->aq_hw_ops->hw_start(self->aq_hw);
330 331 332
	if (err < 0)
		goto err_exit;

333 334
	err = aq_nic_update_interrupt_moderation_settings(self);
	if (err)
335
		goto err_exit;
336
	timer_setup(&self->service_timer, aq_nic_service_timer_cb, 0);
337
	mod_timer(&self->service_timer, jiffies +
338
		  AQ_CFG_SERVICE_TIMER_INTERVAL);
339 340

	if (self->aq_nic_cfg.is_polling) {
341
		timer_setup(&self->polling_timer, aq_nic_polling_timer_cb, 0);
342 343 344 345 346
		mod_timer(&self->polling_timer, jiffies +
			  AQ_CFG_POLLING_TIMER_INTERVAL);
	} else {
		for (i = 0U, aq_vec = self->aq_vec[0];
			self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
347
			err = aq_pci_func_alloc_irq(self, i,
348
						    self->ndev->name, aq_vec,
349
						    aq_vec_get_affinity_mask(aq_vec));
350 351 352 353
			if (err < 0)
				goto err_exit;
		}

354
		err = self->aq_hw_ops->hw_irq_enable(self->aq_hw,
355
						     AQ_CFG_IRQ_MASK);
356 357 358 359 360 361 362 363 364 365 366 367
		if (err < 0)
			goto err_exit;
	}

	err = netif_set_real_num_tx_queues(self->ndev, self->aq_vecs);
	if (err < 0)
		goto err_exit;

	err = netif_set_real_num_rx_queues(self->ndev, self->aq_vecs);
	if (err < 0)
		goto err_exit;

I
Igor Russkikh 已提交
368 369
	netif_tx_start_all_queues(self->ndev);

370 371 372 373
err_exit:
	return err;
}

374 375 376
static unsigned int aq_nic_map_skb(struct aq_nic_s *self,
				   struct sk_buff *skb,
				   struct aq_ring_s *ring)
377 378 379 380
{
	unsigned int ret = 0U;
	unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
	unsigned int frag_count = 0U;
381
	unsigned int dx = ring->sw_tail;
P
Pavel Belous 已提交
382
	struct aq_ring_buff_s *first = NULL;
383
	struct aq_ring_buff_s *dx_buff = &ring->buff_ring[dx];
384

385 386 387 388 389 390 391 392
	if (unlikely(skb_is_gso(skb))) {
		dx_buff->flags = 0U;
		dx_buff->len_pkt = skb->len;
		dx_buff->len_l2 = ETH_HLEN;
		dx_buff->len_l3 = ip_hdrlen(skb);
		dx_buff->len_l4 = tcp_hdrlen(skb);
		dx_buff->mss = skb_shinfo(skb)->gso_size;
		dx_buff->is_txc = 1U;
P
Pavel Belous 已提交
393
		dx_buff->eop_index = 0xffffU;
394

395 396 397
		dx_buff->is_ipv6 =
			(ip_hdr(skb)->version == 6) ? 1U : 0U;

398 399 400 401 402 403 404 405 406 407 408
		dx = aq_ring_next_dx(ring, dx);
		dx_buff = &ring->buff_ring[dx];
		++ret;
	}

	dx_buff->flags = 0U;
	dx_buff->len = skb_headlen(skb);
	dx_buff->pa = dma_map_single(aq_nic_get_dev(self),
				     skb->data,
				     dx_buff->len,
				     DMA_TO_DEVICE);
409

410 411 412
	if (unlikely(dma_mapping_error(aq_nic_get_dev(self), dx_buff->pa)))
		goto exit;

P
Pavel Belous 已提交
413
	first = dx_buff;
414 415 416
	dx_buff->len_pkt = skb->len;
	dx_buff->is_sop = 1U;
	dx_buff->is_mapped = 1U;
417 418 419
	++ret;

	if (skb->ip_summed == CHECKSUM_PARTIAL) {
420 421
		dx_buff->is_ip_cso = (htons(ETH_P_IP) == skb->protocol) ?
			1U : 0U;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

		if (ip_hdr(skb)->version == 4) {
			dx_buff->is_tcp_cso =
				(ip_hdr(skb)->protocol == IPPROTO_TCP) ?
					1U : 0U;
			dx_buff->is_udp_cso =
				(ip_hdr(skb)->protocol == IPPROTO_UDP) ?
					1U : 0U;
		} else if (ip_hdr(skb)->version == 6) {
			dx_buff->is_tcp_cso =
				(ipv6_hdr(skb)->nexthdr == NEXTHDR_TCP) ?
					1U : 0U;
			dx_buff->is_udp_cso =
				(ipv6_hdr(skb)->nexthdr == NEXTHDR_UDP) ?
					1U : 0U;
		}
438 439 440
	}

	for (; nr_frags--; ++frag_count) {
441
		unsigned int frag_len = 0U;
P
Pavel Belous 已提交
442 443
		unsigned int buff_offset = 0U;
		unsigned int buff_size = 0U;
444 445 446 447 448
		dma_addr_t frag_pa;
		skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_count];

		frag_len = skb_frag_size(frag);

P
Pavel Belous 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
		while (frag_len) {
			if (frag_len > AQ_CFG_TX_FRAME_MAX)
				buff_size = AQ_CFG_TX_FRAME_MAX;
			else
				buff_size = frag_len;

			frag_pa = skb_frag_dma_map(aq_nic_get_dev(self),
						   frag,
						   buff_offset,
						   buff_size,
						   DMA_TO_DEVICE);

			if (unlikely(dma_mapping_error(aq_nic_get_dev(self),
						       frag_pa)))
				goto mapping_error;
464 465 466 467 468

			dx = aq_ring_next_dx(ring, dx);
			dx_buff = &ring->buff_ring[dx];

			dx_buff->flags = 0U;
P
Pavel Belous 已提交
469
			dx_buff->len = buff_size;
470 471
			dx_buff->pa = frag_pa;
			dx_buff->is_mapped = 1U;
P
Pavel Belous 已提交
472 473 474 475
			dx_buff->eop_index = 0xffffU;

			frag_len -= buff_size;
			buff_offset += buff_size;
476

477
			++ret;
478 479 480
		}
	}

P
Pavel Belous 已提交
481
	first->eop_index = dx;
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	dx_buff->is_eop = 1U;
	dx_buff->skb = skb;
	goto exit;

mapping_error:
	for (dx = ring->sw_tail;
	     ret > 0;
	     --ret, dx = aq_ring_next_dx(ring, dx)) {
		dx_buff = &ring->buff_ring[dx];

		if (!dx_buff->is_txc && dx_buff->pa) {
			if (unlikely(dx_buff->is_sop)) {
				dma_unmap_single(aq_nic_get_dev(self),
						 dx_buff->pa,
						 dx_buff->len,
						 DMA_TO_DEVICE);
			} else {
				dma_unmap_page(aq_nic_get_dev(self),
					       dx_buff->pa,
					       dx_buff->len,
					       DMA_TO_DEVICE);
			}
		}
505 506
	}

507
exit:
508 509 510 511 512 513 514 515 516
	return ret;
}

int aq_nic_xmit(struct aq_nic_s *self, struct sk_buff *skb)
{
	struct aq_ring_s *ring = NULL;
	unsigned int frags = 0U;
	unsigned int vec = skb->queue_mapping % self->aq_nic_cfg.vecs;
	unsigned int tc = 0U;
517
	int err = NETDEV_TX_OK;
518 519 520 521 522 523 524 525 526 527

	frags = skb_shinfo(skb)->nr_frags + 1;

	ring = self->aq_ring_tx[AQ_NIC_TCVEC2RING(self, tc, vec)];

	if (frags > AQ_CFG_SKB_FRAGS_MAX) {
		dev_kfree_skb_any(skb);
		goto err_exit;
	}

I
Igor Russkikh 已提交
528
	aq_ring_update_queue_state(ring);
529

I
Igor Russkikh 已提交
530 531
	/* Above status update may stop the queue. Check this. */
	if (__netif_subqueue_stopped(self->ndev, ring->idx)) {
532 533 534 535
		err = NETDEV_TX_BUSY;
		goto err_exit;
	}

536
	frags = aq_nic_map_skb(self, skb, ring);
537

538
	if (likely(frags)) {
539
		err = self->aq_hw_ops->hw_ring_tx_xmit(self->aq_hw,
540
						       ring, frags);
541 542 543 544 545
		if (err >= 0) {
			++ring->stats.tx.packets;
			ring->stats.tx.bytes += skb->len;
		}
	} else {
546 547 548 549 550 551 552
		err = NETDEV_TX_BUSY;
	}

err_exit:
	return err;
}

553 554
int aq_nic_update_interrupt_moderation_settings(struct aq_nic_s *self)
{
555
	return self->aq_hw_ops->hw_interrupt_moderation_set(self->aq_hw);
556 557
}

558 559 560 561
int aq_nic_set_packet_filter(struct aq_nic_s *self, unsigned int flags)
{
	int err = 0;

562
	err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw, flags);
563 564 565 566 567 568 569 570 571 572 573
	if (err < 0)
		goto err_exit;

	self->packet_filter = flags;

err_exit:
	return err;
}

int aq_nic_set_multicast_list(struct aq_nic_s *self, struct net_device *ndev)
{
574
	unsigned int packet_filter = self->packet_filter;
575 576 577
	struct netdev_hw_addr *ha = NULL;
	unsigned int i = 0U;

578 579 580 581 582 583
	self->mc_list.count = 0;
	if (netdev_uc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
		packet_filter |= IFF_PROMISC;
	} else {
		netdev_for_each_uc_addr(ha, ndev) {
			ether_addr_copy(self->mc_list.ar[i++], ha->addr);
584

585 586 587
			if (i >= AQ_HW_MULTICAST_ADDRESS_MAX)
				break;
		}
588 589
	}

590 591
	if (i + netdev_mc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
		packet_filter |= IFF_ALLMULTI;
592
	} else {
593 594 595 596 597 598 599 600
		netdev_for_each_mc_addr(ha, ndev) {
			ether_addr_copy(self->mc_list.ar[i++], ha->addr);

			if (i >= AQ_HW_MULTICAST_ADDRESS_MAX)
				break;
		}
	}

601
	if (i > 0 && i <= AQ_HW_MULTICAST_ADDRESS_MAX) {
602 603 604 605 606
		packet_filter |= IFF_MULTICAST;
		self->mc_list.count = i;
		self->aq_hw_ops->hw_multicast_list_set(self->aq_hw,
						       self->mc_list.ar,
						       self->mc_list.count);
607
	}
608
	return aq_nic_set_packet_filter(self, packet_filter);
609 610 611 612 613 614
}

int aq_nic_set_mtu(struct aq_nic_s *self, int new_mtu)
{
	self->aq_nic_cfg.mtu = new_mtu;

615
	return 0;
616 617 618 619
}

int aq_nic_set_mac(struct aq_nic_s *self, struct net_device *ndev)
{
620
	return self->aq_hw_ops->hw_set_mac_address(self->aq_hw, ndev->dev_addr);
621 622 623 624 625 626 627 628 629 630 631 632 633 634
}

unsigned int aq_nic_get_link_speed(struct aq_nic_s *self)
{
	return self->link_status.mbps;
}

int aq_nic_get_regs(struct aq_nic_s *self, struct ethtool_regs *regs, void *p)
{
	u32 *regs_buff = p;
	int err = 0;

	regs->version = 1;

635 636 637
	err = self->aq_hw_ops->hw_get_regs(self->aq_hw,
					   self->aq_nic_cfg.aq_hw_caps,
					   regs_buff);
638 639 640 641 642 643 644 645 646
	if (err < 0)
		goto err_exit;

err_exit:
	return err;
}

int aq_nic_get_regs_count(struct aq_nic_s *self)
{
647
	return self->aq_nic_cfg.aq_hw_caps->mac_regs_count;
648 649 650 651 652 653
}

void aq_nic_get_stats(struct aq_nic_s *self, u64 *data)
{
	unsigned int i = 0U;
	unsigned int count = 0U;
654
	struct aq_vec_s *aq_vec = NULL;
655
	struct aq_stats_s *stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
656

657
	if (!stats)
658 659
		goto err_exit;

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	data[i] = stats->uprc + stats->mprc + stats->bprc;
	data[++i] = stats->uprc;
	data[++i] = stats->mprc;
	data[++i] = stats->bprc;
	data[++i] = stats->erpt;
	data[++i] = stats->uptc + stats->mptc + stats->bptc;
	data[++i] = stats->uptc;
	data[++i] = stats->mptc;
	data[++i] = stats->bptc;
	data[++i] = stats->ubrc;
	data[++i] = stats->ubtc;
	data[++i] = stats->mbrc;
	data[++i] = stats->mbtc;
	data[++i] = stats->bbrc;
	data[++i] = stats->bbtc;
	data[++i] = stats->ubrc + stats->mbrc + stats->bbrc;
	data[++i] = stats->ubtc + stats->mbtc + stats->bbtc;
	data[++i] = stats->dma_pkt_rc;
	data[++i] = stats->dma_pkt_tc;
	data[++i] = stats->dma_oct_rc;
	data[++i] = stats->dma_oct_tc;
	data[++i] = stats->dpc;

	i++;

	data += i;
686 687

	for (i = 0U, aq_vec = self->aq_vec[0];
688
		aq_vec && self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
689 690 691 692 693 694 695
		data += count;
		aq_vec_get_sw_stats(aq_vec, data, &count);
	}

err_exit:;
}

696 697 698
static void aq_nic_update_ndev_stats(struct aq_nic_s *self)
{
	struct net_device *ndev = self->ndev;
699
	struct aq_stats_s *stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
700 701 702 703 704 705 706

	ndev->stats.rx_packets = stats->uprc + stats->mprc + stats->bprc;
	ndev->stats.rx_bytes = stats->ubrc + stats->mbrc + stats->bbrc;
	ndev->stats.rx_errors = stats->erpr;
	ndev->stats.tx_packets = stats->uptc + stats->mptc + stats->bptc;
	ndev->stats.tx_bytes = stats->ubtc + stats->mbtc + stats->bbtc;
	ndev->stats.tx_errors = stats->erpt;
707
	ndev->stats.multicast = stats->mprc;
708 709
}

710 711
void aq_nic_get_link_ksettings(struct aq_nic_s *self,
			       struct ethtool_link_ksettings *cmd)
712
{
713 714 715 716
	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
		cmd->base.port = PORT_FIBRE;
	else
		cmd->base.port = PORT_TP;
717
	/* This driver supports only 10G capable adapters, so DUPLEX_FULL */
718 719 720
	cmd->base.duplex = DUPLEX_FULL;
	cmd->base.autoneg = self->aq_nic_cfg.is_autoneg;

721 722
	ethtool_link_ksettings_zero_link_mode(cmd, supported);

723
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_10G)
724 725 726
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     10000baseT_Full);

727
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_5G)
728 729 730
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     5000baseT_Full);

731
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_2GS)
732 733 734
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     2500baseT_Full);

735
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_1G)
736 737 738
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     1000baseT_Full);

739
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_100M)
740 741 742
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     100baseT_Full);

743
	if (self->aq_nic_cfg.aq_hw_caps->flow_control)
744 745 746 747
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     Pause);

	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
748 749 750 751 752

	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
	else
		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

	ethtool_link_ksettings_zero_link_mode(cmd, advertising);

	if (self->aq_nic_cfg.is_autoneg)
		ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);

	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_10G)
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     10000baseT_Full);

	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_5G)
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     5000baseT_Full);

	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_2GS)
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     2500baseT_Full);

	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_1G)
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     1000baseT_Full);

	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_100M)
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     100baseT_Full);

779
	if (self->aq_nic_cfg.flow_control & AQ_NIC_FC_RX)
780 781 782
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     Pause);

783 784 785
	/* Asym is when either RX or TX, but not both */
	if (!!(self->aq_nic_cfg.flow_control & AQ_NIC_FC_TX) ^
	    !!(self->aq_nic_cfg.flow_control & AQ_NIC_FC_RX))
786 787 788
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     Asym_Pause);

789 790 791 792
	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
	else
		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
793 794
}

795 796
int aq_nic_set_link_ksettings(struct aq_nic_s *self,
			      const struct ethtool_link_ksettings *cmd)
797 798 799 800 801
{
	u32 speed = 0U;
	u32 rate = 0U;
	int err = 0;

802
	if (cmd->base.autoneg == AUTONEG_ENABLE) {
803
		rate = self->aq_nic_cfg.aq_hw_caps->link_speed_msk;
804 805
		self->aq_nic_cfg.is_autoneg = true;
	} else {
806
		speed = cmd->base.speed;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

		switch (speed) {
		case SPEED_100:
			rate = AQ_NIC_RATE_100M;
			break;

		case SPEED_1000:
			rate = AQ_NIC_RATE_1G;
			break;

		case SPEED_2500:
			rate = AQ_NIC_RATE_2GS;
			break;

		case SPEED_5000:
			rate = AQ_NIC_RATE_5G;
			break;

		case SPEED_10000:
			rate = AQ_NIC_RATE_10G;
			break;

		default:
			err = -1;
			goto err_exit;
		break;
		}
834
		if (!(self->aq_nic_cfg.aq_hw_caps->link_speed_msk & rate)) {
835 836 837 838 839 840 841
			err = -1;
			goto err_exit;
		}

		self->aq_nic_cfg.is_autoneg = false;
	}

842
	err = self->aq_fw_ops->set_link_speed(self->aq_hw, rate);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	if (err < 0)
		goto err_exit;

	self->aq_nic_cfg.link_speed_msk = rate;

err_exit:
	return err;
}

struct aq_nic_cfg_s *aq_nic_get_cfg(struct aq_nic_s *self)
{
	return &self->aq_nic_cfg;
}

u32 aq_nic_get_fw_version(struct aq_nic_s *self)
{
	u32 fw_version = 0U;

861
	self->aq_hw_ops->hw_get_fw_version(self->aq_hw, &fw_version);
862 863 864 865 866 867 868 869 870

	return fw_version;
}

int aq_nic_stop(struct aq_nic_s *self)
{
	struct aq_vec_s *aq_vec = NULL;
	unsigned int i = 0U;

I
Igor Russkikh 已提交
871
	netif_tx_disable(self->ndev);
872
	netif_carrier_off(self->ndev);
873 874 875

	del_timer_sync(&self->service_timer);

876
	self->aq_hw_ops->hw_irq_disable(self->aq_hw, AQ_CFG_IRQ_MASK);
877 878 879 880

	if (self->aq_nic_cfg.is_polling)
		del_timer_sync(&self->polling_timer);
	else
881
		aq_pci_func_free_irqs(self);
882 883 884 885 886

	for (i = 0U, aq_vec = self->aq_vec[0];
		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
		aq_vec_stop(aq_vec);

887
	return self->aq_hw_ops->hw_stop(self->aq_hw);
888 889 890 891 892 893 894 895 896 897 898 899 900 901
}

void aq_nic_deinit(struct aq_nic_s *self)
{
	struct aq_vec_s *aq_vec = NULL;
	unsigned int i = 0U;

	if (!self)
		goto err_exit;

	for (i = 0U, aq_vec = self->aq_vec[0];
		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
		aq_vec_deinit(aq_vec);

902 903 904 905 906 907 908
	self->aq_fw_ops->deinit(self->aq_hw);

	if (self->power_state != AQ_HW_POWER_STATE_D0 ||
	    self->aq_hw->aq_nic_cfg->wol) {
		self->aq_fw_ops->set_power(self->aq_hw,
					   self->power_state,
					   self->ndev->dev_addr);
909 910 911 912 913
	}

err_exit:;
}

914
void aq_nic_free_vectors(struct aq_nic_s *self)
915 916 917 918 919 920
{
	unsigned int i = 0U;

	if (!self)
		goto err_exit;

921
	for (i = ARRAY_SIZE(self->aq_vec); i--;) {
922
		if (self->aq_vec[i]) {
923
			aq_vec_free(self->aq_vec[i]);
924 925
			self->aq_vec[i] = NULL;
		}
926 927 928 929 930 931 932 933 934 935 936
	}

err_exit:;
}

int aq_nic_change_pm_state(struct aq_nic_s *self, pm_message_t *pm_msg)
{
	int err = 0;

	if (!netif_running(self->ndev)) {
		err = 0;
937
		goto out;
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
	}
	rtnl_lock();
	if (pm_msg->event & PM_EVENT_SLEEP || pm_msg->event & PM_EVENT_FREEZE) {
		self->power_state = AQ_HW_POWER_STATE_D3;
		netif_device_detach(self->ndev);
		netif_tx_stop_all_queues(self->ndev);

		err = aq_nic_stop(self);
		if (err < 0)
			goto err_exit;

		aq_nic_deinit(self);
	} else {
		err = aq_nic_init(self);
		if (err < 0)
			goto err_exit;

		err = aq_nic_start(self);
		if (err < 0)
			goto err_exit;

		netif_device_attach(self->ndev);
		netif_tx_start_all_queues(self->ndev);
	}

err_exit:
964 965
	rtnl_unlock();
out:
966 967
	return err;
}
968 969 970 971 972 973 974 975 976 977 978 979

void aq_nic_shutdown(struct aq_nic_s *self)
{
	int err = 0;

	if (!self->ndev)
		return;

	rtnl_lock();

	netif_device_detach(self->ndev);

980 981 982 983 984
	if (netif_running(self->ndev)) {
		err = aq_nic_stop(self);
		if (err < 0)
			goto err_exit;
	}
985 986 987 988 989
	aq_nic_deinit(self);

err_exit:
	rtnl_unlock();
}