aq_nic.c 30.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 13 14
 */

/* 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"
15
#include "aq_macsec.h"
16
#include "aq_main.h"
17
#include "aq_phy.h"
18
#include "aq_ptp.h"
19
#include "aq_filters.h"
20

21
#include <linux/moduleparam.h>
22 23 24 25 26 27 28 29
#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>

30 31 32 33 34 35 36 37 38 39 40 41
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");

42 43
static void aq_nic_update_ndev_stats(struct aq_nic_s *self);

44 45
static void aq_nic_rss_init(struct aq_nic_s *self, unsigned int num_rss_queues)
{
46
	static u8 rss_key[AQ_CFG_RSS_HASHKEY_SIZE] = {
47 48 49 50 51 52
		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
	};
N
Nikita Danilov 已提交
53 54 55 56 57
	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
	struct aq_rss_parameters *rss_params;
	int i = 0;

	rss_params = &cfg->aq_rss;
58 59 60 61 62 63 64 65 66

	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);
}

67 68
/* Checks hw_caps and 'corrects' aq_nic_cfg in runtime */
void aq_nic_cfg_start(struct aq_nic_s *self)
69 70 71 72 73 74 75
{
	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;

	cfg->tcs = AQ_CFG_TCS_DEF;

	cfg->is_polling = AQ_CFG_IS_POLLING_DEF;

76 77 78
	cfg->itr = aq_itr;
	cfg->tx_itr = aq_itr_tx;
	cfg->rx_itr = aq_itr_rx;
79

80
	cfg->rxpageorder = AQ_CFG_RX_PAGEORDER;
81 82 83
	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;
84
	cfg->fc.req = AQ_CFG_FC_MODE;
85
	cfg->wol = AQ_CFG_WOL_MODES;
86 87 88 89 90 91 92 93

	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 */
94 95
	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);
96 97

	/*rss rings */
98
	cfg->vecs = min(cfg->aq_hw_caps->vecs, AQ_CFG_VECS_DEF);
99
	cfg->vecs = min(cfg->vecs, num_online_cpus());
100 101
	if (self->irqvecs > AQ_HW_SERVICE_IRQS)
		cfg->vecs = min(cfg->vecs, self->irqvecs - AQ_HW_SERVICE_IRQS);
102 103 104 105 106 107 108 109 110 111
	/* 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;

112 113
	cfg->num_rss_queues = min(cfg->vecs, AQ_CFG_NUM_RSS_QUEUES_DEF);

114 115
	aq_nic_rss_init(self, cfg->num_rss_queues);

116
	cfg->irq_type = aq_pci_func_get_irq_type(self);
117 118

	if ((cfg->irq_type == AQ_HW_IRQ_LEGACY) ||
119
	    (cfg->aq_hw_caps->vecs == 1U) ||
120 121 122 123 124
	    (cfg->vecs == 1U)) {
		cfg->is_rss = 0U;
		cfg->vecs = 1U;
	}

125 126 127 128 129 130 131 132 133
	/* Check if we have enough vectors allocated for
	 * link status IRQ. If no - we'll know link state from
	 * slower service task.
	 */
	if (AQ_HW_SERVICE_IRQS > 0 && cfg->vecs + 1 <= self->irqvecs)
		cfg->link_irq_vec = cfg->vecs;
	else
		cfg->link_irq_vec = 0;

134
	cfg->link_speed_msk &= cfg->aq_hw_caps->link_speed_msk;
135
	cfg->features = cfg->aq_hw_caps->hw_features;
136 137
	cfg->is_vlan_rx_strip = !!(cfg->features & NETIF_F_HW_VLAN_CTAG_RX);
	cfg->is_vlan_tx_insert = !!(cfg->features & NETIF_F_HW_VLAN_CTAG_TX);
138
	cfg->is_vlan_force_promisc = true;
139 140
}

I
Igor Russkikh 已提交
141 142
static int aq_nic_update_link_status(struct aq_nic_s *self)
{
143
	int err = self->aq_fw_ops->update_link_status(self->aq_hw);
144
	u32 fc = 0;
I
Igor Russkikh 已提交
145 146 147 148

	if (err)
		return err;

149 150 151 152
	if (self->aq_fw_ops->get_flow_control)
		self->aq_fw_ops->get_flow_control(self->aq_hw, &fc);
	self->aq_nic_cfg.fc.cur = fc;

153
	if (self->link_status.mbps != self->aq_hw->aq_link_status.mbps) {
154 155 156
		netdev_info(self->ndev, "%s: link change old %d new %d\n",
			    AQ_CFG_DRV_NAME, self->link_status.mbps,
			    self->aq_hw->aq_link_status.mbps);
157
		aq_nic_update_interrupt_moderation_settings(self);
158

159
		if (self->aq_ptp) {
160
			aq_ptp_clock_init(self);
161 162
			aq_ptp_tm_offset_set(self,
					     self->aq_hw->aq_link_status.mbps);
163
			aq_ptp_link_change(self);
164
		}
165

166 167 168 169 170 171
		/* 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_hw_ops->hw_set_fc)
			self->aq_hw_ops->hw_set_fc(self->aq_hw, fc, 0);
172
	}
I
Igor Russkikh 已提交
173 174 175

	self->link_status = self->aq_hw->aq_link_status;
	if (!netif_carrier_ok(self->ndev) && self->link_status.mbps) {
176
		aq_utils_obj_set(&self->flags,
I
Igor Russkikh 已提交
177
				 AQ_NIC_FLAG_STARTED);
178
		aq_utils_obj_clear(&self->flags,
I
Igor Russkikh 已提交
179 180
				   AQ_NIC_LINK_DOWN);
		netif_carrier_on(self->ndev);
181 182 183
#if IS_ENABLED(CONFIG_MACSEC)
		aq_macsec_enable(self);
#endif
I
Igor Russkikh 已提交
184 185 186 187 188
		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);
189
		aq_utils_obj_set(&self->flags, AQ_NIC_LINK_DOWN);
I
Igor Russkikh 已提交
190
	}
N
Nikita Danilov 已提交
191

I
Igor Russkikh 已提交
192 193 194
	return 0;
}

195 196 197 198 199 200 201 202 203 204 205
static irqreturn_t aq_linkstate_threaded_isr(int irq, void *private)
{
	struct aq_nic_s *self = private;

	if (!self)
		return IRQ_NONE;

	aq_nic_update_link_status(self);

	self->aq_hw_ops->hw_irq_enable(self->aq_hw,
				       BIT(self->aq_nic_cfg.link_irq_vec));
N
Nikita Danilov 已提交
206

207 208 209
	return IRQ_HANDLED;
}

210
static void aq_nic_service_task(struct work_struct *work)
211
{
212 213 214
	struct aq_nic_s *self = container_of(work, struct aq_nic_s,
					     service_task);
	int err;
215

216 217
	aq_ptp_service_task(self);

218
	if (aq_utils_obj_test(&self->flags, AQ_NIC_FLAGS_IS_NOT_READY))
219
		return;
220

I
Igor Russkikh 已提交
221 222
	err = aq_nic_update_link_status(self);
	if (err)
223
		return;
224

225 226 227 228
#if IS_ENABLED(CONFIG_MACSEC)
	aq_macsec_work(self);
#endif

229
	mutex_lock(&self->fwreq_mutex);
230 231
	if (self->aq_fw_ops->update_stats)
		self->aq_fw_ops->update_stats(self->aq_hw);
232
	mutex_unlock(&self->fwreq_mutex);
233

234
	aq_nic_update_ndev_stats(self);
235 236 237 238 239
}

static void aq_nic_service_timer_cb(struct timer_list *t)
{
	struct aq_nic_s *self = from_timer(self, t, service_timer);
240

N
Nikita Danilov 已提交
241 242
	mod_timer(&self->service_timer,
		  jiffies + AQ_CFG_SERVICE_TIMER_INTERVAL);
243 244

	aq_ndev_schedule_work(&self->service_task);
245 246
}

247
static void aq_nic_polling_timer_cb(struct timer_list *t)
248
{
249
	struct aq_nic_s *self = from_timer(self, t, polling_timer);
250 251 252 253 254 255 256 257
	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 +
258
		  AQ_CFG_POLLING_TIMER_INTERVAL);
259 260
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274
static int aq_nic_hw_prepare(struct aq_nic_s *self)
{
	int err = 0;

	err = self->aq_hw_ops->hw_soft_reset(self->aq_hw);
	if (err)
		goto exit;

	err = self->aq_hw_ops->hw_prepare(self->aq_hw, &self->aq_fw_ops);

exit:
	return err;
}

275 276 277 278 279 280 281 282
static bool aq_nic_is_valid_ether_addr(const u8 *addr)
{
	/* Some engineering samples of Aquantia NICs are provisioned with a
	 * partially populated MAC, which is still invalid.
	 */
	return !(addr[0] == 0 && addr[1] == 0 && addr[2] == 0);
}

283 284 285 286 287 288 289 290
int aq_nic_ndev_register(struct aq_nic_s *self)
{
	int err = 0;

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

292
	err = aq_nic_hw_prepare(self);
293 294 295
	if (err)
		goto err_exit;

296 297 298 299
#if IS_ENABLED(CONFIG_MACSEC)
	aq_macsec_init(self);
#endif

300
	mutex_lock(&self->fwreq_mutex);
301
	err = self->aq_fw_ops->get_mac_permanent(self->aq_hw,
302
			    self->ndev->dev_addr);
303
	mutex_unlock(&self->fwreq_mutex);
304
	if (err)
305 306
		goto err_exit;

307 308 309 310 311 312
	if (!is_valid_ether_addr(self->ndev->dev_addr) ||
	    !aq_nic_is_valid_ether_addr(self->ndev->dev_addr)) {
		netdev_warn(self->ndev, "MAC is invalid, will use random.");
		eth_hw_addr_random(self->ndev);
	}

313 314 315 316 317 318 319 320
#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

321 322 323 324 325 326 327 328 329 330
	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;
		}
	}

331 332
	netif_carrier_off(self->ndev);

I
Igor Russkikh 已提交
333
	netif_tx_disable(self->ndev);
334

335
	err = register_netdev(self->ndev);
336
	if (err)
337 338
		goto err_exit;

339
err_exit:
340 341 342 343
#if IS_ENABLED(CONFIG_MACSEC)
	if (err)
		aq_macsec_free(self);
#endif
344 345 346
	return err;
}

347
void aq_nic_ndev_init(struct aq_nic_s *self)
348
{
349
	const struct aq_hw_caps_s *aq_hw_caps = self->aq_nic_cfg.aq_hw_caps;
350 351 352 353
	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;
354
	self->ndev->vlan_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
355 356
				     NETIF_F_RXHASH | NETIF_F_SG |
				     NETIF_F_LRO | NETIF_F_TSO;
357
	self->ndev->gso_partial_features = NETIF_F_GSO_UDP_L4;
358
	self->ndev->priv_flags = aq_hw_caps->hw_priv_flags;
359 360
	self->ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;

361
	self->msg_enable = NETIF_MSG_DRV | NETIF_MSG_LINK;
362
	self->ndev->mtu = aq_nic_cfg->mtu - ETH_HLEN;
363
	self->ndev->max_mtu = aq_hw_caps->mtu - ETH_FCS_LEN - ETH_HLEN;
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

}

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;
	unsigned int i = 0U;
N
Nikita Danilov 已提交
382
	int err = 0;
383 384

	self->power_state = AQ_HW_POWER_STATE_D0;
385
	mutex_lock(&self->fwreq_mutex);
386
	err = self->aq_hw_ops->hw_reset(self->aq_hw);
387
	mutex_unlock(&self->fwreq_mutex);
388 389 390
	if (err < 0)
		goto err_exit;

391
	err = self->aq_hw_ops->hw_init(self->aq_hw,
392
				       aq_nic_get_ndev(self)->dev_addr);
393 394 395
	if (err < 0)
		goto err_exit;

396 397
	if (ATL_HW_IS_CHIP_FEATURE(self->aq_hw, ATLANTIC) &&
	    self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_TP) {
398 399 400 401
		self->aq_hw->phy_id = HW_ATL_PHY_ID_MAX;
		err = aq_phy_init(self->aq_hw);
	}

402 403 404 405 406 407 408
	for (i = 0U; i < self->aq_vecs; i++) {
		aq_vec = self->aq_vec[i];
		err = aq_vec_ring_alloc(aq_vec, self, i,
					aq_nic_get_cfg(self));
		if (err)
			goto err_exit;

409
		aq_vec_init(aq_vec, self->aq_hw_ops, self->aq_hw);
410
	}
411

412 413 414 415
	err = aq_ptp_init(self, self->irqvecs - 1);
	if (err < 0)
		goto err_exit;

416 417 418 419 420 421 422 423
	err = aq_ptp_ring_alloc(self);
	if (err < 0)
		goto err_exit;

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

424 425
	netif_carrier_off(self->ndev);

426 427 428 429 430 431 432
err_exit:
	return err;
}

int aq_nic_start(struct aq_nic_s *self)
{
	struct aq_vec_s *aq_vec = NULL;
433
	struct aq_nic_cfg_s *cfg;
434
	unsigned int i = 0U;
N
Nikita Danilov 已提交
435
	int err = 0;
436

437 438
	cfg = aq_nic_get_cfg(self);

439
	err = self->aq_hw_ops->hw_multicast_list_set(self->aq_hw,
440 441
						     self->mc_list.ar,
						     self->mc_list.count);
442 443 444
	if (err < 0)
		goto err_exit;

445
	err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw,
446
						    self->packet_filter);
447 448 449 450 451 452 453 454 455 456
	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;
	}

457 458 459 460
	err = aq_ptp_ring_start(self);
	if (err < 0)
		goto err_exit;

461 462
	aq_nic_set_loopback(self);

463
	err = self->aq_hw_ops->hw_start(self->aq_hw);
464 465 466
	if (err < 0)
		goto err_exit;

467 468
	err = aq_nic_update_interrupt_moderation_settings(self);
	if (err)
469
		goto err_exit;
470 471 472

	INIT_WORK(&self->service_task, aq_nic_service_task);

473
	timer_setup(&self->service_timer, aq_nic_service_timer_cb, 0);
474
	aq_nic_service_timer_cb(&self->service_timer);
475

476
	if (cfg->is_polling) {
477
		timer_setup(&self->polling_timer, aq_nic_polling_timer_cb, 0);
478 479 480 481 482
		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]) {
483 484
			err = aq_pci_func_alloc_irq(self, i, self->ndev->name,
						    aq_vec_isr, aq_vec,
485
						    aq_vec_get_affinity_mask(aq_vec));
486 487 488 489
			if (err < 0)
				goto err_exit;
		}

490 491 492 493
		err = aq_ptp_irq_alloc(self);
		if (err < 0)
			goto err_exit;

494
		if (cfg->link_irq_vec) {
495
			int irqvec = pci_irq_vector(self->pdev,
496
						    cfg->link_irq_vec);
497 498
			err = request_threaded_irq(irqvec, NULL,
						   aq_linkstate_threaded_isr,
499
						   IRQF_SHARED | IRQF_ONESHOT,
500 501 502
						   self->ndev->name, self);
			if (err < 0)
				goto err_exit;
503
			self->msix_entry_mask |= (1 << cfg->link_irq_vec);
504 505
		}

506
		err = self->aq_hw_ops->hw_irq_enable(self->aq_hw,
507
						     AQ_CFG_IRQ_MASK);
508 509 510 511 512 513 514 515 516 517 518 519
		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 已提交
520 521
	netif_tx_start_all_queues(self->ndev);

522 523 524 525
err_exit:
	return err;
}

526 527
unsigned int aq_nic_map_skb(struct aq_nic_s *self, struct sk_buff *skb,
			    struct aq_ring_s *ring)
528 529
{
	unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
530 531
	struct aq_nic_cfg_s *cfg = aq_nic_get_cfg(self);
	struct device *dev = aq_nic_get_dev(self);
P
Pavel Belous 已提交
532
	struct aq_ring_buff_s *first = NULL;
533
	u8 ipver = ip_hdr(skb)->version;
N
Nikita Danilov 已提交
534
	struct aq_ring_buff_s *dx_buff;
535
	bool need_context_tag = false;
N
Nikita Danilov 已提交
536 537 538
	unsigned int frag_count = 0U;
	unsigned int ret = 0U;
	unsigned int dx;
539 540 541 542 543 544
	u8 l4proto = 0;

	if (ipver == 4)
		l4proto = ip_hdr(skb)->protocol;
	else if (ipver == 6)
		l4proto = ipv6_hdr(skb)->nexthdr;
545

N
Nikita Danilov 已提交
546 547
	dx = ring->sw_tail;
	dx_buff = &ring->buff_ring[dx];
548
	dx_buff->flags = 0U;
549

550
	if (unlikely(skb_is_gso(skb))) {
551
		dx_buff->mss = skb_shinfo(skb)->gso_size;
552 553 554 555 556 557 558 559 560 561 562 563 564
		if (l4proto == IPPROTO_TCP) {
			dx_buff->is_gso_tcp = 1U;
			dx_buff->len_l4 = tcp_hdrlen(skb);
		} else if (l4proto == IPPROTO_UDP) {
			dx_buff->is_gso_udp = 1U;
			dx_buff->len_l4 = sizeof(struct udphdr);
			/* UDP GSO Hardware does not replace packet length. */
			udp_hdr(skb)->len = htons(dx_buff->mss +
						  dx_buff->len_l4);
		} else {
			WARN_ONCE(true, "Bad GSO mode");
			goto exit;
		}
565 566
		dx_buff->len_pkt = skb->len;
		dx_buff->len_l2 = ETH_HLEN;
567
		dx_buff->len_l3 = skb_network_header_len(skb);
P
Pavel Belous 已提交
568
		dx_buff->eop_index = 0xffffU;
569
		dx_buff->is_ipv6 = (ipver == 6);
570 571
		need_context_tag = true;
	}
572

573
	if (cfg->is_vlan_tx_insert && skb_vlan_tag_present(skb)) {
574 575 576 577 578 579 580
		dx_buff->vlan_tx_tag = skb_vlan_tag_get(skb);
		dx_buff->len_pkt = skb->len;
		dx_buff->is_vlan = 1U;
		need_context_tag = true;
	}

	if (need_context_tag) {
581 582
		dx = aq_ring_next_dx(ring, dx);
		dx_buff = &ring->buff_ring[dx];
583
		dx_buff->flags = 0U;
584 585 586 587
		++ret;
	}

	dx_buff->len = skb_headlen(skb);
588
	dx_buff->pa = dma_map_single(dev,
589 590 591
				     skb->data,
				     dx_buff->len,
				     DMA_TO_DEVICE);
592

593
	if (unlikely(dma_mapping_error(dev, dx_buff->pa))) {
594
		ret = 0;
595
		goto exit;
596
	}
597

P
Pavel Belous 已提交
598
	first = dx_buff;
599 600 601
	dx_buff->len_pkt = skb->len;
	dx_buff->is_sop = 1U;
	dx_buff->is_mapped = 1U;
602 603 604
	++ret;

	if (skb->ip_summed == CHECKSUM_PARTIAL) {
605 606 607
		dx_buff->is_ip_cso = (htons(ETH_P_IP) == skb->protocol);
		dx_buff->is_tcp_cso = (l4proto == IPPROTO_TCP);
		dx_buff->is_udp_cso = (l4proto == IPPROTO_UDP);
608 609 610
	}

	for (; nr_frags--; ++frag_count) {
611
		unsigned int frag_len = 0U;
P
Pavel Belous 已提交
612 613
		unsigned int buff_offset = 0U;
		unsigned int buff_size = 0U;
614 615 616 617 618
		dma_addr_t frag_pa;
		skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_count];

		frag_len = skb_frag_size(frag);

P
Pavel Belous 已提交
619 620 621 622 623 624
		while (frag_len) {
			if (frag_len > AQ_CFG_TX_FRAME_MAX)
				buff_size = AQ_CFG_TX_FRAME_MAX;
			else
				buff_size = frag_len;

625
			frag_pa = skb_frag_dma_map(dev,
P
Pavel Belous 已提交
626 627 628 629 630
						   frag,
						   buff_offset,
						   buff_size,
						   DMA_TO_DEVICE);

631
			if (unlikely(dma_mapping_error(dev,
P
Pavel Belous 已提交
632 633
						       frag_pa)))
				goto mapping_error;
634 635 636 637 638

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

			dx_buff->flags = 0U;
P
Pavel Belous 已提交
639
			dx_buff->len = buff_size;
640 641
			dx_buff->pa = frag_pa;
			dx_buff->is_mapped = 1U;
P
Pavel Belous 已提交
642 643 644 645
			dx_buff->eop_index = 0xffffU;

			frag_len -= buff_size;
			buff_offset += buff_size;
646

647
			++ret;
648 649 650
		}
	}

P
Pavel Belous 已提交
651
	first->eop_index = dx;
652 653 654 655 656 657 658 659 660 661
	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];

662 663
		if (!(dx_buff->is_gso_tcp || dx_buff->is_gso_udp) &&
		    !dx_buff->is_vlan && dx_buff->pa) {
664
			if (unlikely(dx_buff->is_sop)) {
665
				dma_unmap_single(dev,
666 667 668 669
						 dx_buff->pa,
						 dx_buff->len,
						 DMA_TO_DEVICE);
			} else {
670
				dma_unmap_page(dev,
671 672 673 674 675
					       dx_buff->pa,
					       dx_buff->len,
					       DMA_TO_DEVICE);
			}
		}
676 677
	}

678
exit:
679 680 681 682 683
	return ret;
}

int aq_nic_xmit(struct aq_nic_s *self, struct sk_buff *skb)
{
N
Nikita Danilov 已提交
684
	unsigned int vec = skb->queue_mapping % self->aq_nic_cfg.vecs;
685 686
	struct aq_ring_s *ring = NULL;
	unsigned int frags = 0U;
687
	int err = NETDEV_TX_OK;
N
Nikita Danilov 已提交
688
	unsigned int tc = 0U;
689 690 691 692 693 694 695 696 697 698

	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 已提交
699
	aq_ring_update_queue_state(ring);
700

701 702 703 704 705
	if (self->aq_nic_cfg.priv_flags & BIT(AQ_HW_LOOPBACK_DMA_NET)) {
		err = NETDEV_TX_BUSY;
		goto err_exit;
	}

I
Igor Russkikh 已提交
706 707
	/* Above status update may stop the queue. Check this. */
	if (__netif_subqueue_stopped(self->ndev, ring->idx)) {
708 709 710 711
		err = NETDEV_TX_BUSY;
		goto err_exit;
	}

712
	frags = aq_nic_map_skb(self, skb, ring);
713

714
	if (likely(frags)) {
715
		err = self->aq_hw_ops->hw_ring_tx_xmit(self->aq_hw,
716
						       ring, frags);
717
	} else {
718 719 720 721 722 723 724
		err = NETDEV_TX_BUSY;
	}

err_exit:
	return err;
}

725 726
int aq_nic_update_interrupt_moderation_settings(struct aq_nic_s *self)
{
727
	return self->aq_hw_ops->hw_interrupt_moderation_set(self->aq_hw);
728 729
}

730 731 732 733
int aq_nic_set_packet_filter(struct aq_nic_s *self, unsigned int flags)
{
	int err = 0;

734
	err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw, flags);
735 736 737 738 739 740 741 742 743 744 745
	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)
{
746 747 748
	const struct aq_hw_ops *hw_ops = self->aq_hw_ops;
	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
	unsigned int packet_filter = ndev->flags;
749 750
	struct netdev_hw_addr *ha = NULL;
	unsigned int i = 0U;
751
	int err = 0;
752

753 754 755 756 757 758 759
	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);
		}
760 761
	}

762 763 764 765 766 767 768 769 770
	cfg->is_mc_list_enabled = !!(packet_filter & IFF_MULTICAST);
	if (cfg->is_mc_list_enabled) {
		if (i + netdev_mc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
			packet_filter |= IFF_ALLMULTI;
		} else {
			netdev_for_each_mc_addr(ha, ndev) {
				ether_addr_copy(self->mc_list.ar[i++],
						ha->addr);
			}
771 772 773
		}
	}

774
	if (i > 0 && i <= AQ_HW_MULTICAST_ADDRESS_MAX) {
775
		self->mc_list.count = i;
776 777 778
		err = hw_ops->hw_multicast_list_set(self->aq_hw,
						    self->mc_list.ar,
						    self->mc_list.count);
779 780
		if (err < 0)
			return err;
781
	}
N
Nikita Danilov 已提交
782

783
	return aq_nic_set_packet_filter(self, packet_filter);
784 785 786 787 788 789
}

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

790
	return 0;
791 792 793 794
}

int aq_nic_set_mac(struct aq_nic_s *self, struct net_device *ndev)
{
795
	return self->aq_hw_ops->hw_set_mac_address(self->aq_hw, ndev->dev_addr);
796 797 798 799 800 801 802 803 804 805 806 807
}

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;

808 809 810
	if (unlikely(!self->aq_hw_ops->hw_get_regs))
		return -EOPNOTSUPP;

811 812
	regs->version = 1;

813 814 815
	err = self->aq_hw_ops->hw_get_regs(self->aq_hw,
					   self->aq_nic_cfg.aq_hw_caps,
					   regs_buff);
816 817 818 819 820 821 822 823 824
	if (err < 0)
		goto err_exit;

err_exit:
	return err;
}

int aq_nic_get_regs_count(struct aq_nic_s *self)
{
825 826 827
	if (unlikely(!self->aq_hw_ops->hw_get_regs))
		return 0;

828
	return self->aq_nic_cfg.aq_hw_caps->mac_regs_count;
829 830
}

831
u64 *aq_nic_get_stats(struct aq_nic_s *self, u64 *data)
832
{
833
	struct aq_vec_s *aq_vec = NULL;
834
	struct aq_stats_s *stats;
N
Nikita Danilov 已提交
835 836
	unsigned int count = 0U;
	unsigned int i = 0U;
837 838 839 840 841 842 843

	if (self->aq_fw_ops->update_stats) {
		mutex_lock(&self->fwreq_mutex);
		self->aq_fw_ops->update_stats(self->aq_hw);
		mutex_unlock(&self->fwreq_mutex);
	}
	stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
844

845
	if (!stats)
846 847
		goto err_exit;

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
	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;
874 875

	for (i = 0U, aq_vec = self->aq_vec[0];
876
		aq_vec && self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
877 878 879 880
		data += count;
		aq_vec_get_sw_stats(aq_vec, data, &count);
	}

881 882
	data += count;

883
err_exit:;
884
	return data;
885 886
}

887 888
static void aq_nic_update_ndev_stats(struct aq_nic_s *self)
{
889
	struct aq_stats_s *stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
N
Nikita Danilov 已提交
890
	struct net_device *ndev = self->ndev;
891

892 893
	ndev->stats.rx_packets = stats->dma_pkt_rc;
	ndev->stats.rx_bytes = stats->dma_oct_rc;
894
	ndev->stats.rx_errors = stats->erpr;
895 896 897
	ndev->stats.rx_dropped = stats->dpc;
	ndev->stats.tx_packets = stats->dma_pkt_tc;
	ndev->stats.tx_bytes = stats->dma_oct_tc;
898
	ndev->stats.tx_errors = stats->erpt;
899
	ndev->stats.multicast = stats->mprc;
900 901
}

902 903
void aq_nic_get_link_ksettings(struct aq_nic_s *self,
			       struct ethtool_link_ksettings *cmd)
904
{
905 906 907 908
	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;
909
	/* This driver supports only 10G capable adapters, so DUPLEX_FULL */
910 911 912
	cmd->base.duplex = DUPLEX_FULL;
	cmd->base.autoneg = self->aq_nic_cfg.is_autoneg;

913 914
	ethtool_link_ksettings_zero_link_mode(cmd, supported);

915
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_10G)
916 917 918
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     10000baseT_Full);

919
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_5G)
920 921 922
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     5000baseT_Full);

923
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_2G5)
924 925 926
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     2500baseT_Full);

927
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_1G)
928 929 930
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     1000baseT_Full);

931
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_100M)
932 933 934
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     100baseT_Full);

935 936 937 938
	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_10M)
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     10baseT_Full);

939
	if (self->aq_nic_cfg.aq_hw_caps->flow_control) {
940 941
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     Pause);
942 943 944
		ethtool_link_ksettings_add_link_mode(cmd, supported,
						     Asym_Pause);
	}
945 946

	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
947 948 949 950 951

	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);
952 953 954 955 956 957 958 959 960 961 962 963 964 965

	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);

966
	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_2G5)
967 968 969 970 971 972 973 974 975 976 977
		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);

978 979 980 981
	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_10M)
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     10baseT_Full);

982
	if (self->aq_nic_cfg.fc.cur & AQ_NIC_FC_RX)
983 984 985
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     Pause);

986
	/* Asym is when either RX or TX, but not both */
987 988
	if (!!(self->aq_nic_cfg.fc.cur & AQ_NIC_FC_TX) ^
	    !!(self->aq_nic_cfg.fc.cur & AQ_NIC_FC_RX))
989 990 991
		ethtool_link_ksettings_add_link_mode(cmd, advertising,
						     Asym_Pause);

992 993 994 995
	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);
996 997
}

998 999
int aq_nic_set_link_ksettings(struct aq_nic_s *self,
			      const struct ethtool_link_ksettings *cmd)
1000 1001 1002 1003 1004
{
	u32 speed = 0U;
	u32 rate = 0U;
	int err = 0;

1005
	if (cmd->base.autoneg == AUTONEG_ENABLE) {
1006
		rate = self->aq_nic_cfg.aq_hw_caps->link_speed_msk;
1007 1008
		self->aq_nic_cfg.is_autoneg = true;
	} else {
1009
		speed = cmd->base.speed;
1010 1011

		switch (speed) {
1012 1013 1014 1015
		case SPEED_10:
			rate = AQ_NIC_RATE_10M;
			break;

1016 1017 1018 1019 1020 1021 1022 1023 1024
		case SPEED_100:
			rate = AQ_NIC_RATE_100M;
			break;

		case SPEED_1000:
			rate = AQ_NIC_RATE_1G;
			break;

		case SPEED_2500:
1025
			rate = AQ_NIC_RATE_2G5;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
			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;
		}
1041
		if (!(self->aq_nic_cfg.aq_hw_caps->link_speed_msk & rate)) {
1042 1043 1044 1045 1046 1047 1048
			err = -1;
			goto err_exit;
		}

		self->aq_nic_cfg.is_autoneg = false;
	}

1049
	mutex_lock(&self->fwreq_mutex);
1050
	err = self->aq_fw_ops->set_link_speed(self->aq_hw, rate);
1051
	mutex_unlock(&self->fwreq_mutex);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
	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)
{
1068
	return self->aq_hw_ops->hw_get_fw_version(self->aq_hw);
1069 1070
}

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
int aq_nic_set_loopback(struct aq_nic_s *self)
{
	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;

	if (!self->aq_hw_ops->hw_set_loopback ||
	    !self->aq_fw_ops->set_phyloopback)
		return -ENOTSUPP;

	mutex_lock(&self->fwreq_mutex);
	self->aq_hw_ops->hw_set_loopback(self->aq_hw,
					 AQ_HW_LOOPBACK_DMA_SYS,
					 !!(cfg->priv_flags &
					    BIT(AQ_HW_LOOPBACK_DMA_SYS)));

	self->aq_hw_ops->hw_set_loopback(self->aq_hw,
					 AQ_HW_LOOPBACK_PKT_SYS,
					 !!(cfg->priv_flags &
					    BIT(AQ_HW_LOOPBACK_PKT_SYS)));

	self->aq_hw_ops->hw_set_loopback(self->aq_hw,
					 AQ_HW_LOOPBACK_DMA_NET,
					 !!(cfg->priv_flags &
					    BIT(AQ_HW_LOOPBACK_DMA_NET)));

	self->aq_fw_ops->set_phyloopback(self->aq_hw,
					 AQ_HW_LOOPBACK_PHYINT_SYS,
					 !!(cfg->priv_flags &
					    BIT(AQ_HW_LOOPBACK_PHYINT_SYS)));

	self->aq_fw_ops->set_phyloopback(self->aq_hw,
					 AQ_HW_LOOPBACK_PHYEXT_SYS,
					 !!(cfg->priv_flags &
					    BIT(AQ_HW_LOOPBACK_PHYEXT_SYS)));
	mutex_unlock(&self->fwreq_mutex);

	return 0;
}

1109 1110 1111 1112 1113
int aq_nic_stop(struct aq_nic_s *self)
{
	struct aq_vec_s *aq_vec = NULL;
	unsigned int i = 0U;

I
Igor Russkikh 已提交
1114
	netif_tx_disable(self->ndev);
1115
	netif_carrier_off(self->ndev);
1116 1117

	del_timer_sync(&self->service_timer);
1118
	cancel_work_sync(&self->service_task);
1119

1120
	self->aq_hw_ops->hw_irq_disable(self->aq_hw, AQ_CFG_IRQ_MASK);
1121 1122 1123 1124

	if (self->aq_nic_cfg.is_polling)
		del_timer_sync(&self->polling_timer);
	else
1125
		aq_pci_func_free_irqs(self);
1126

1127 1128
	aq_ptp_irq_free(self);

1129 1130 1131 1132
	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);

1133 1134
	aq_ptp_ring_stop(self);

1135
	return self->aq_hw_ops->hw_stop(self->aq_hw);
1136 1137
}

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
void aq_nic_set_power(struct aq_nic_s *self)
{
	if (self->power_state != AQ_HW_POWER_STATE_D0 ||
	    self->aq_hw->aq_nic_cfg->wol)
		if (likely(self->aq_fw_ops->set_power)) {
			mutex_lock(&self->fwreq_mutex);
			self->aq_fw_ops->set_power(self->aq_hw,
						   self->power_state,
						   self->ndev->dev_addr);
			mutex_unlock(&self->fwreq_mutex);
		}
}

void aq_nic_deinit(struct aq_nic_s *self, bool link_down)
1152 1153 1154 1155 1156 1157 1158
{
	struct aq_vec_s *aq_vec = NULL;
	unsigned int i = 0U;

	if (!self)
		goto err_exit;

1159 1160
	for (i = 0U; i < self->aq_vecs; i++) {
		aq_vec = self->aq_vec[i];
1161
		aq_vec_deinit(aq_vec);
1162 1163
		aq_vec_ring_free(aq_vec);
	}
1164

1165
	aq_ptp_unregister(self);
1166 1167
	aq_ptp_ring_deinit(self);
	aq_ptp_ring_free(self);
1168 1169
	aq_ptp_free(self);

1170
	if (likely(self->aq_fw_ops->deinit) && link_down) {
1171 1172 1173 1174
		mutex_lock(&self->fwreq_mutex);
		self->aq_fw_ops->deinit(self->aq_hw);
		mutex_unlock(&self->fwreq_mutex);
	}
1175

1176 1177 1178
err_exit:;
}

1179
void aq_nic_free_vectors(struct aq_nic_s *self)
1180 1181 1182 1183 1184 1185
{
	unsigned int i = 0U;

	if (!self)
		goto err_exit;

1186
	for (i = ARRAY_SIZE(self->aq_vec); i--;) {
1187
		if (self->aq_vec[i]) {
1188
			aq_vec_free(self->aq_vec[i]);
1189 1190
			self->aq_vec[i] = NULL;
		}
1191 1192 1193 1194 1195
	}

err_exit:;
}

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
void aq_nic_shutdown(struct aq_nic_s *self)
{
	int err = 0;

	if (!self->ndev)
		return;

	rtnl_lock();

	netif_device_detach(self->ndev);

1207 1208 1209 1210 1211
	if (netif_running(self->ndev)) {
		err = aq_nic_stop(self);
		if (err < 0)
			goto err_exit;
	}
1212 1213
	aq_nic_deinit(self, !self->aq_hw->aq_nic_cfg->wol);
	aq_nic_set_power(self);
1214 1215 1216

err_exit:
	rtnl_unlock();
1217
}
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260

u8 aq_nic_reserve_filter(struct aq_nic_s *self, enum aq_rx_filter_type type)
{
	u8 location = 0xFF;
	u32 fltr_cnt;
	u32 n_bit;

	switch (type) {
	case aq_rx_filter_ethertype:
		location = AQ_RX_LAST_LOC_FETHERT - AQ_RX_FIRST_LOC_FETHERT -
			   self->aq_hw_rx_fltrs.fet_reserved_count;
		self->aq_hw_rx_fltrs.fet_reserved_count++;
		break;
	case aq_rx_filter_l3l4:
		fltr_cnt = AQ_RX_LAST_LOC_FL3L4 - AQ_RX_FIRST_LOC_FL3L4;
		n_bit = fltr_cnt - self->aq_hw_rx_fltrs.fl3l4.reserved_count;

		self->aq_hw_rx_fltrs.fl3l4.active_ipv4 |= BIT(n_bit);
		self->aq_hw_rx_fltrs.fl3l4.reserved_count++;
		location = n_bit;
		break;
	default:
		break;
	}

	return location;
}

void aq_nic_release_filter(struct aq_nic_s *self, enum aq_rx_filter_type type,
			   u32 location)
{
	switch (type) {
	case aq_rx_filter_ethertype:
		self->aq_hw_rx_fltrs.fet_reserved_count--;
		break;
	case aq_rx_filter_l3l4:
		self->aq_hw_rx_fltrs.fl3l4.reserved_count--;
		self->aq_hw_rx_fltrs.fl3l4.active_ipv4 &= ~BIT(location);
		break;
	default:
		break;
	}
}