xref: /openbmc/linux/drivers/net/ethernet/cavium/thunder/nic_main.c (revision 45cc842d5b75ba8f9a958f2dd12b95c6dd0452bd)
1 /*
2  * Copyright (C) 2015 Cavium, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License
6  * as published by the Free Software Foundation.
7  */
8 
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/pci.h>
12 #include <linux/etherdevice.h>
13 #include <linux/of.h>
14 #include <linux/if_vlan.h>
15 
16 #include "nic_reg.h"
17 #include "nic.h"
18 #include "q_struct.h"
19 #include "thunder_bgx.h"
20 
21 #define DRV_NAME	"nicpf"
22 #define DRV_VERSION	"1.0"
23 
24 struct hw_info {
25 	u8		bgx_cnt;
26 	u8		chans_per_lmac;
27 	u8		chans_per_bgx; /* Rx/Tx chans */
28 	u8		chans_per_rgx;
29 	u8		chans_per_lbk;
30 	u16		cpi_cnt;
31 	u16		rssi_cnt;
32 	u16		rss_ind_tbl_size;
33 	u16		tl4_cnt;
34 	u16		tl3_cnt;
35 	u8		tl2_cnt;
36 	u8		tl1_cnt;
37 	bool		tl1_per_bgx; /* TL1 per BGX or per LMAC */
38 };
39 
40 struct nicpf {
41 	struct pci_dev		*pdev;
42 	struct hw_info          *hw;
43 	u8			node;
44 	unsigned int		flags;
45 	u8			num_vf_en;      /* No of VF enabled */
46 	bool			vf_enabled[MAX_NUM_VFS_SUPPORTED];
47 	void __iomem		*reg_base;       /* Register start address */
48 	u8			num_sqs_en;	/* Secondary qsets enabled */
49 	u64			nicvf[MAX_NUM_VFS_SUPPORTED];
50 	u8			vf_sqs[MAX_NUM_VFS_SUPPORTED][MAX_SQS_PER_VF];
51 	u8			pqs_vf[MAX_NUM_VFS_SUPPORTED];
52 	bool			sqs_used[MAX_NUM_VFS_SUPPORTED];
53 	struct pkind_cfg	pkind;
54 #define	NIC_SET_VF_LMAC_MAP(bgx, lmac)	(((bgx & 0xF) << 4) | (lmac & 0xF))
55 #define	NIC_GET_BGX_FROM_VF_LMAC_MAP(map)	((map >> 4) & 0xF)
56 #define	NIC_GET_LMAC_FROM_VF_LMAC_MAP(map)	(map & 0xF)
57 	u8			*vf_lmac_map;
58 	struct delayed_work     dwork;
59 	struct workqueue_struct *check_link;
60 	u8			*link;
61 	u8			*duplex;
62 	u32			*speed;
63 	u16			cpi_base[MAX_NUM_VFS_SUPPORTED];
64 	u16			rssi_base[MAX_NUM_VFS_SUPPORTED];
65 	bool			mbx_lock[MAX_NUM_VFS_SUPPORTED];
66 
67 	/* MSI-X */
68 	u8			num_vec;
69 	bool			irq_allocated[NIC_PF_MSIX_VECTORS];
70 	char			irq_name[NIC_PF_MSIX_VECTORS][20];
71 };
72 
73 /* Supported devices */
74 static const struct pci_device_id nic_id_table[] = {
75 	{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_NIC_PF) },
76 	{ 0, }  /* end of table */
77 };
78 
79 MODULE_AUTHOR("Sunil Goutham");
80 MODULE_DESCRIPTION("Cavium Thunder NIC Physical Function Driver");
81 MODULE_LICENSE("GPL v2");
82 MODULE_VERSION(DRV_VERSION);
83 MODULE_DEVICE_TABLE(pci, nic_id_table);
84 
85 /* The Cavium ThunderX network controller can *only* be found in SoCs
86  * containing the ThunderX ARM64 CPU implementation.  All accesses to the device
87  * registers on this platform are implicitly strongly ordered with respect
88  * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
89  * with no memory barriers in this driver.  The readq()/writeq() functions add
90  * explicit ordering operation which in this case are redundant, and only
91  * add overhead.
92  */
93 
94 /* Register read/write APIs */
95 static void nic_reg_write(struct nicpf *nic, u64 offset, u64 val)
96 {
97 	writeq_relaxed(val, nic->reg_base + offset);
98 }
99 
100 static u64 nic_reg_read(struct nicpf *nic, u64 offset)
101 {
102 	return readq_relaxed(nic->reg_base + offset);
103 }
104 
105 /* PF -> VF mailbox communication APIs */
106 static void nic_enable_mbx_intr(struct nicpf *nic)
107 {
108 	int vf_cnt = pci_sriov_get_totalvfs(nic->pdev);
109 
110 #define INTR_MASK(vfs) ((vfs < 64) ? (BIT_ULL(vfs) - 1) : (~0ull))
111 
112 	/* Clear it, to avoid spurious interrupts (if any) */
113 	nic_reg_write(nic, NIC_PF_MAILBOX_INT, INTR_MASK(vf_cnt));
114 
115 	/* Enable mailbox interrupt for all VFs */
116 	nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S, INTR_MASK(vf_cnt));
117 	/* One mailbox intr enable reg per 64 VFs */
118 	if (vf_cnt > 64) {
119 		nic_reg_write(nic, NIC_PF_MAILBOX_INT + sizeof(u64),
120 			      INTR_MASK(vf_cnt - 64));
121 		nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S + sizeof(u64),
122 			      INTR_MASK(vf_cnt - 64));
123 	}
124 }
125 
126 static void nic_clear_mbx_intr(struct nicpf *nic, int vf, int mbx_reg)
127 {
128 	nic_reg_write(nic, NIC_PF_MAILBOX_INT + (mbx_reg << 3), BIT_ULL(vf));
129 }
130 
131 static u64 nic_get_mbx_addr(int vf)
132 {
133 	return NIC_PF_VF_0_127_MAILBOX_0_1 + (vf << NIC_VF_NUM_SHIFT);
134 }
135 
136 /* Send a mailbox message to VF
137  * @vf: vf to which this message to be sent
138  * @mbx: Message to be sent
139  */
140 static void nic_send_msg_to_vf(struct nicpf *nic, int vf, union nic_mbx *mbx)
141 {
142 	void __iomem *mbx_addr = nic->reg_base + nic_get_mbx_addr(vf);
143 	u64 *msg = (u64 *)mbx;
144 
145 	/* In first revision HW, mbox interrupt is triggerred
146 	 * when PF writes to MBOX(1), in next revisions when
147 	 * PF writes to MBOX(0)
148 	 */
149 	if (pass1_silicon(nic->pdev)) {
150 		/* see the comment for nic_reg_write()/nic_reg_read()
151 		 * functions above
152 		 */
153 		writeq_relaxed(msg[0], mbx_addr);
154 		writeq_relaxed(msg[1], mbx_addr + 8);
155 	} else {
156 		writeq_relaxed(msg[1], mbx_addr + 8);
157 		writeq_relaxed(msg[0], mbx_addr);
158 	}
159 }
160 
161 /* Responds to VF's READY message with VF's
162  * ID, node, MAC address e.t.c
163  * @vf: VF which sent READY message
164  */
165 static void nic_mbx_send_ready(struct nicpf *nic, int vf)
166 {
167 	union nic_mbx mbx = {};
168 	int bgx_idx, lmac;
169 	const char *mac;
170 
171 	mbx.nic_cfg.msg = NIC_MBOX_MSG_READY;
172 	mbx.nic_cfg.vf_id = vf;
173 
174 	mbx.nic_cfg.tns_mode = NIC_TNS_BYPASS_MODE;
175 
176 	if (vf < nic->num_vf_en) {
177 		bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
178 		lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
179 
180 		mac = bgx_get_lmac_mac(nic->node, bgx_idx, lmac);
181 		if (mac)
182 			ether_addr_copy((u8 *)&mbx.nic_cfg.mac_addr, mac);
183 	}
184 	mbx.nic_cfg.sqs_mode = (vf >= nic->num_vf_en) ? true : false;
185 	mbx.nic_cfg.node_id = nic->node;
186 
187 	mbx.nic_cfg.loopback_supported = vf < nic->num_vf_en;
188 
189 	nic_send_msg_to_vf(nic, vf, &mbx);
190 }
191 
192 /* ACKs VF's mailbox message
193  * @vf: VF to which ACK to be sent
194  */
195 static void nic_mbx_send_ack(struct nicpf *nic, int vf)
196 {
197 	union nic_mbx mbx = {};
198 
199 	mbx.msg.msg = NIC_MBOX_MSG_ACK;
200 	nic_send_msg_to_vf(nic, vf, &mbx);
201 }
202 
203 /* NACKs VF's mailbox message that PF is not able to
204  * complete the action
205  * @vf: VF to which ACK to be sent
206  */
207 static void nic_mbx_send_nack(struct nicpf *nic, int vf)
208 {
209 	union nic_mbx mbx = {};
210 
211 	mbx.msg.msg = NIC_MBOX_MSG_NACK;
212 	nic_send_msg_to_vf(nic, vf, &mbx);
213 }
214 
215 /* Flush all in flight receive packets to memory and
216  * bring down an active RQ
217  */
218 static int nic_rcv_queue_sw_sync(struct nicpf *nic)
219 {
220 	u16 timeout = ~0x00;
221 
222 	nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x01);
223 	/* Wait till sync cycle is finished */
224 	while (timeout) {
225 		if (nic_reg_read(nic, NIC_PF_SW_SYNC_RX_DONE) & 0x1)
226 			break;
227 		timeout--;
228 	}
229 	nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x00);
230 	if (!timeout) {
231 		dev_err(&nic->pdev->dev, "Receive queue software sync failed");
232 		return 1;
233 	}
234 	return 0;
235 }
236 
237 /* Get BGX Rx/Tx stats and respond to VF's request */
238 static void nic_get_bgx_stats(struct nicpf *nic, struct bgx_stats_msg *bgx)
239 {
240 	int bgx_idx, lmac;
241 	union nic_mbx mbx = {};
242 
243 	bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
244 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
245 
246 	mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
247 	mbx.bgx_stats.vf_id = bgx->vf_id;
248 	mbx.bgx_stats.rx = bgx->rx;
249 	mbx.bgx_stats.idx = bgx->idx;
250 	if (bgx->rx)
251 		mbx.bgx_stats.stats = bgx_get_rx_stats(nic->node, bgx_idx,
252 							    lmac, bgx->idx);
253 	else
254 		mbx.bgx_stats.stats = bgx_get_tx_stats(nic->node, bgx_idx,
255 							    lmac, bgx->idx);
256 	nic_send_msg_to_vf(nic, bgx->vf_id, &mbx);
257 }
258 
259 /* Update hardware min/max frame size */
260 static int nic_update_hw_frs(struct nicpf *nic, int new_frs, int vf)
261 {
262 	int bgx, lmac, lmac_cnt;
263 	u64 lmac_credits;
264 
265 	if ((new_frs > NIC_HW_MAX_FRS) || (new_frs < NIC_HW_MIN_FRS))
266 		return 1;
267 
268 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
269 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
270 	lmac += bgx * MAX_LMAC_PER_BGX;
271 
272 	new_frs += VLAN_ETH_HLEN + ETH_FCS_LEN + 4;
273 
274 	/* Update corresponding LMAC credits */
275 	lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
276 	lmac_credits = nic_reg_read(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8));
277 	lmac_credits &= ~(0xFFFFFULL << 12);
278 	lmac_credits |= (((((48 * 1024) / lmac_cnt) - new_frs) / 16) << 12);
279 	nic_reg_write(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8), lmac_credits);
280 
281 	/* Enforce MTU in HW
282 	 * This config is supported only from 88xx pass 2.0 onwards.
283 	 */
284 	if (!pass1_silicon(nic->pdev))
285 		nic_reg_write(nic,
286 			      NIC_PF_LMAC_0_7_CFG2 + (lmac * 8), new_frs);
287 	return 0;
288 }
289 
290 /* Set minimum transmit packet size */
291 static void nic_set_tx_pkt_pad(struct nicpf *nic, int size)
292 {
293 	int lmac, max_lmac;
294 	u16 sdevid;
295 	u64 lmac_cfg;
296 
297 	/* There is a issue in HW where-in while sending GSO sized
298 	 * pkts as part of TSO, if pkt len falls below this size
299 	 * NIC will zero PAD packet and also updates IP total length.
300 	 * Hence set this value to lessthan min pkt size of MAC+IP+TCP
301 	 * headers, BGX will do the padding to transmit 64 byte pkt.
302 	 */
303 	if (size > 52)
304 		size = 52;
305 
306 	pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
307 	/* 81xx's RGX has only one LMAC */
308 	if (sdevid == PCI_SUBSYS_DEVID_81XX_NIC_PF)
309 		max_lmac = ((nic->hw->bgx_cnt - 1) * MAX_LMAC_PER_BGX) + 1;
310 	else
311 		max_lmac = nic->hw->bgx_cnt * MAX_LMAC_PER_BGX;
312 
313 	for (lmac = 0; lmac < max_lmac; lmac++) {
314 		lmac_cfg = nic_reg_read(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3));
315 		lmac_cfg &= ~(0xF << 2);
316 		lmac_cfg |= ((size / 4) << 2);
317 		nic_reg_write(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3), lmac_cfg);
318 	}
319 }
320 
321 /* Function to check number of LMACs present and set VF::LMAC mapping.
322  * Mapping will be used while initializing channels.
323  */
324 static void nic_set_lmac_vf_mapping(struct nicpf *nic)
325 {
326 	unsigned bgx_map = bgx_get_map(nic->node);
327 	int bgx, next_bgx_lmac = 0;
328 	int lmac, lmac_cnt = 0;
329 	u64 lmac_credit;
330 
331 	nic->num_vf_en = 0;
332 
333 	for (bgx = 0; bgx < nic->hw->bgx_cnt; bgx++) {
334 		if (!(bgx_map & (1 << bgx)))
335 			continue;
336 		lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
337 		for (lmac = 0; lmac < lmac_cnt; lmac++)
338 			nic->vf_lmac_map[next_bgx_lmac++] =
339 						NIC_SET_VF_LMAC_MAP(bgx, lmac);
340 		nic->num_vf_en += lmac_cnt;
341 
342 		/* Program LMAC credits */
343 		lmac_credit = (1ull << 1); /* channel credit enable */
344 		lmac_credit |= (0x1ff << 2); /* Max outstanding pkt count */
345 		/* 48KB BGX Tx buffer size, each unit is of size 16bytes */
346 		lmac_credit |= (((((48 * 1024) / lmac_cnt) -
347 				NIC_HW_MAX_FRS) / 16) << 12);
348 		lmac = bgx * MAX_LMAC_PER_BGX;
349 		for (; lmac < lmac_cnt + (bgx * MAX_LMAC_PER_BGX); lmac++)
350 			nic_reg_write(nic,
351 				      NIC_PF_LMAC_0_7_CREDIT + (lmac * 8),
352 				      lmac_credit);
353 
354 		/* On CN81XX there are only 8 VFs but max possible no of
355 		 * interfaces are 9.
356 		 */
357 		if (nic->num_vf_en >= pci_sriov_get_totalvfs(nic->pdev)) {
358 			nic->num_vf_en = pci_sriov_get_totalvfs(nic->pdev);
359 			break;
360 		}
361 	}
362 }
363 
364 static void nic_get_hw_info(struct nicpf *nic)
365 {
366 	u16 sdevid;
367 	struct hw_info *hw = nic->hw;
368 
369 	pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
370 
371 	switch (sdevid) {
372 	case PCI_SUBSYS_DEVID_88XX_NIC_PF:
373 		hw->bgx_cnt = MAX_BGX_PER_CN88XX;
374 		hw->chans_per_lmac = 16;
375 		hw->chans_per_bgx = 128;
376 		hw->cpi_cnt = 2048;
377 		hw->rssi_cnt = 4096;
378 		hw->rss_ind_tbl_size = NIC_MAX_RSS_IDR_TBL_SIZE;
379 		hw->tl3_cnt = 256;
380 		hw->tl2_cnt = 64;
381 		hw->tl1_cnt = 2;
382 		hw->tl1_per_bgx = true;
383 		break;
384 	case PCI_SUBSYS_DEVID_81XX_NIC_PF:
385 		hw->bgx_cnt = MAX_BGX_PER_CN81XX;
386 		hw->chans_per_lmac = 8;
387 		hw->chans_per_bgx = 32;
388 		hw->chans_per_rgx = 8;
389 		hw->chans_per_lbk = 24;
390 		hw->cpi_cnt = 512;
391 		hw->rssi_cnt = 256;
392 		hw->rss_ind_tbl_size = 32; /* Max RSSI / Max interfaces */
393 		hw->tl3_cnt = 64;
394 		hw->tl2_cnt = 16;
395 		hw->tl1_cnt = 10;
396 		hw->tl1_per_bgx = false;
397 		break;
398 	case PCI_SUBSYS_DEVID_83XX_NIC_PF:
399 		hw->bgx_cnt = MAX_BGX_PER_CN83XX;
400 		hw->chans_per_lmac = 8;
401 		hw->chans_per_bgx = 32;
402 		hw->chans_per_lbk = 64;
403 		hw->cpi_cnt = 2048;
404 		hw->rssi_cnt = 1024;
405 		hw->rss_ind_tbl_size = 64; /* Max RSSI / Max interfaces */
406 		hw->tl3_cnt = 256;
407 		hw->tl2_cnt = 64;
408 		hw->tl1_cnt = 18;
409 		hw->tl1_per_bgx = false;
410 		break;
411 	}
412 	hw->tl4_cnt = MAX_QUEUES_PER_QSET * pci_sriov_get_totalvfs(nic->pdev);
413 }
414 
415 #define BGX0_BLOCK 8
416 #define BGX1_BLOCK 9
417 
418 static void nic_init_hw(struct nicpf *nic)
419 {
420 	int i;
421 	u64 cqm_cfg;
422 
423 	/* Enable NIC HW block */
424 	nic_reg_write(nic, NIC_PF_CFG, 0x3);
425 
426 	/* Enable backpressure */
427 	nic_reg_write(nic, NIC_PF_BP_CFG, (1ULL << 6) | 0x03);
428 
429 	/* TNS and TNS bypass modes are present only on 88xx
430 	 * Also offset of this CSR has changed in 81xx and 83xx.
431 	 */
432 	if (nic->pdev->subsystem_device == PCI_SUBSYS_DEVID_88XX_NIC_PF) {
433 		/* Disable TNS mode on both interfaces */
434 		nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG,
435 			      (NIC_TNS_BYPASS_MODE << 7) |
436 			      BGX0_BLOCK | (1ULL << 16));
437 		nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG | (1 << 8),
438 			      (NIC_TNS_BYPASS_MODE << 7) |
439 			      BGX1_BLOCK | (1ULL << 16));
440 	} else {
441 		/* Configure timestamp generation timeout to 10us */
442 		for (i = 0; i < nic->hw->bgx_cnt; i++)
443 			nic_reg_write(nic, NIC_PF_INTFX_SEND_CFG | (i << 3),
444 				      (1ULL << 16));
445 	}
446 
447 	nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG,
448 		      (1ULL << 63) | BGX0_BLOCK);
449 	nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG + (1 << 8),
450 		      (1ULL << 63) | BGX1_BLOCK);
451 
452 	/* PKIND configuration */
453 	nic->pkind.minlen = 0;
454 	nic->pkind.maxlen = NIC_HW_MAX_FRS + VLAN_ETH_HLEN + ETH_FCS_LEN + 4;
455 	nic->pkind.lenerr_en = 1;
456 	nic->pkind.rx_hdr = 0;
457 	nic->pkind.hdr_sl = 0;
458 
459 	for (i = 0; i < NIC_MAX_PKIND; i++)
460 		nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (i << 3),
461 			      *(u64 *)&nic->pkind);
462 
463 	nic_set_tx_pkt_pad(nic, NIC_HW_MIN_FRS);
464 
465 	/* Timer config */
466 	nic_reg_write(nic, NIC_PF_INTR_TIMER_CFG, NICPF_CLK_PER_INT_TICK);
467 
468 	/* Enable VLAN ethertype matching and stripping */
469 	nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7,
470 		      (2 << 19) | (ETYPE_ALG_VLAN_STRIP << 16) | ETH_P_8021Q);
471 
472 	/* Check if HW expected value is higher (could be in future chips) */
473 	cqm_cfg = nic_reg_read(nic, NIC_PF_CQM_CFG);
474 	if (cqm_cfg < NICPF_CQM_MIN_DROP_LEVEL)
475 		nic_reg_write(nic, NIC_PF_CQM_CFG, NICPF_CQM_MIN_DROP_LEVEL);
476 }
477 
478 /* Channel parse index configuration */
479 static void nic_config_cpi(struct nicpf *nic, struct cpi_cfg_msg *cfg)
480 {
481 	struct hw_info *hw = nic->hw;
482 	u32 vnic, bgx, lmac, chan;
483 	u32 padd, cpi_count = 0;
484 	u64 cpi_base, cpi, rssi_base, rssi;
485 	u8  qset, rq_idx = 0;
486 
487 	vnic = cfg->vf_id;
488 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
489 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
490 
491 	chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
492 	cpi_base = vnic * NIC_MAX_CPI_PER_LMAC;
493 	rssi_base = vnic * hw->rss_ind_tbl_size;
494 
495 	/* Rx channel configuration */
496 	nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_BP_CFG | (chan << 3),
497 		      (1ull << 63) | (vnic << 0));
498 	nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_CFG | (chan << 3),
499 		      ((u64)cfg->cpi_alg << 62) | (cpi_base << 48));
500 
501 	if (cfg->cpi_alg == CPI_ALG_NONE)
502 		cpi_count = 1;
503 	else if (cfg->cpi_alg == CPI_ALG_VLAN) /* 3 bits of PCP */
504 		cpi_count = 8;
505 	else if (cfg->cpi_alg == CPI_ALG_VLAN16) /* 3 bits PCP + DEI */
506 		cpi_count = 16;
507 	else if (cfg->cpi_alg == CPI_ALG_DIFF) /* 6bits DSCP */
508 		cpi_count = NIC_MAX_CPI_PER_LMAC;
509 
510 	/* RSS Qset, Qidx mapping */
511 	qset = cfg->vf_id;
512 	rssi = rssi_base;
513 	for (; rssi < (rssi_base + cfg->rq_cnt); rssi++) {
514 		nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
515 			      (qset << 3) | rq_idx);
516 		rq_idx++;
517 	}
518 
519 	rssi = 0;
520 	cpi = cpi_base;
521 	for (; cpi < (cpi_base + cpi_count); cpi++) {
522 		/* Determine port to channel adder */
523 		if (cfg->cpi_alg != CPI_ALG_DIFF)
524 			padd = cpi % cpi_count;
525 		else
526 			padd = cpi % 8; /* 3 bits CS out of 6bits DSCP */
527 
528 		/* Leave RSS_SIZE as '0' to disable RSS */
529 		if (pass1_silicon(nic->pdev)) {
530 			nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
531 				      (vnic << 24) | (padd << 16) |
532 				      (rssi_base + rssi));
533 		} else {
534 			/* Set MPI_ALG to '0' to disable MCAM parsing */
535 			nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
536 				      (padd << 16));
537 			/* MPI index is same as CPI if MPI_ALG is not enabled */
538 			nic_reg_write(nic, NIC_PF_MPI_0_2047_CFG | (cpi << 3),
539 				      (vnic << 24) | (rssi_base + rssi));
540 		}
541 
542 		if ((rssi + 1) >= cfg->rq_cnt)
543 			continue;
544 
545 		if (cfg->cpi_alg == CPI_ALG_VLAN)
546 			rssi++;
547 		else if (cfg->cpi_alg == CPI_ALG_VLAN16)
548 			rssi = ((cpi - cpi_base) & 0xe) >> 1;
549 		else if (cfg->cpi_alg == CPI_ALG_DIFF)
550 			rssi = ((cpi - cpi_base) & 0x38) >> 3;
551 	}
552 	nic->cpi_base[cfg->vf_id] = cpi_base;
553 	nic->rssi_base[cfg->vf_id] = rssi_base;
554 }
555 
556 /* Responsds to VF with its RSS indirection table size */
557 static void nic_send_rss_size(struct nicpf *nic, int vf)
558 {
559 	union nic_mbx mbx = {};
560 
561 	mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
562 	mbx.rss_size.ind_tbl_size = nic->hw->rss_ind_tbl_size;
563 	nic_send_msg_to_vf(nic, vf, &mbx);
564 }
565 
566 /* Receive side scaling configuration
567  * configure:
568  * - RSS index
569  * - indir table i.e hash::RQ mapping
570  * - no of hash bits to consider
571  */
572 static void nic_config_rss(struct nicpf *nic, struct rss_cfg_msg *cfg)
573 {
574 	u8  qset, idx = 0;
575 	u64 cpi_cfg, cpi_base, rssi_base, rssi;
576 	u64 idx_addr;
577 
578 	rssi_base = nic->rssi_base[cfg->vf_id] + cfg->tbl_offset;
579 
580 	rssi = rssi_base;
581 
582 	for (; rssi < (rssi_base + cfg->tbl_len); rssi++) {
583 		u8 svf = cfg->ind_tbl[idx] >> 3;
584 
585 		if (svf)
586 			qset = nic->vf_sqs[cfg->vf_id][svf - 1];
587 		else
588 			qset = cfg->vf_id;
589 		nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
590 			      (qset << 3) | (cfg->ind_tbl[idx] & 0x7));
591 		idx++;
592 	}
593 
594 	cpi_base = nic->cpi_base[cfg->vf_id];
595 	if (pass1_silicon(nic->pdev))
596 		idx_addr = NIC_PF_CPI_0_2047_CFG;
597 	else
598 		idx_addr = NIC_PF_MPI_0_2047_CFG;
599 	cpi_cfg = nic_reg_read(nic, idx_addr | (cpi_base << 3));
600 	cpi_cfg &= ~(0xFULL << 20);
601 	cpi_cfg |= (cfg->hash_bits << 20);
602 	nic_reg_write(nic, idx_addr | (cpi_base << 3), cpi_cfg);
603 }
604 
605 /* 4 level transmit side scheduler configutation
606  * for TNS bypass mode
607  *
608  * Sample configuration for SQ0 on 88xx
609  * VNIC0-SQ0 -> TL4(0)   -> TL3[0]   -> TL2[0]  -> TL1[0] -> BGX0
610  * VNIC1-SQ0 -> TL4(8)   -> TL3[2]   -> TL2[0]  -> TL1[0] -> BGX0
611  * VNIC2-SQ0 -> TL4(16)  -> TL3[4]   -> TL2[1]  -> TL1[0] -> BGX0
612  * VNIC3-SQ0 -> TL4(24)  -> TL3[6]   -> TL2[1]  -> TL1[0] -> BGX0
613  * VNIC4-SQ0 -> TL4(512) -> TL3[128] -> TL2[32] -> TL1[1] -> BGX1
614  * VNIC5-SQ0 -> TL4(520) -> TL3[130] -> TL2[32] -> TL1[1] -> BGX1
615  * VNIC6-SQ0 -> TL4(528) -> TL3[132] -> TL2[33] -> TL1[1] -> BGX1
616  * VNIC7-SQ0 -> TL4(536) -> TL3[134] -> TL2[33] -> TL1[1] -> BGX1
617  */
618 static void nic_tx_channel_cfg(struct nicpf *nic, u8 vnic,
619 			       struct sq_cfg_msg *sq)
620 {
621 	struct hw_info *hw = nic->hw;
622 	u32 bgx, lmac, chan;
623 	u32 tl2, tl3, tl4;
624 	u32 rr_quantum;
625 	u8 sq_idx = sq->sq_num;
626 	u8 pqs_vnic;
627 	int svf;
628 
629 	if (sq->sqs_mode)
630 		pqs_vnic = nic->pqs_vf[vnic];
631 	else
632 		pqs_vnic = vnic;
633 
634 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
635 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
636 
637 	/* 24 bytes for FCS, IPG and preamble */
638 	rr_quantum = ((NIC_HW_MAX_FRS + 24) / 4);
639 
640 	/* For 88xx 0-511 TL4 transmits via BGX0 and
641 	 * 512-1023 TL4s transmit via BGX1.
642 	 */
643 	if (hw->tl1_per_bgx) {
644 		tl4 = bgx * (hw->tl4_cnt / hw->bgx_cnt);
645 		if (!sq->sqs_mode) {
646 			tl4 += (lmac * MAX_QUEUES_PER_QSET);
647 		} else {
648 			for (svf = 0; svf < MAX_SQS_PER_VF; svf++) {
649 				if (nic->vf_sqs[pqs_vnic][svf] == vnic)
650 					break;
651 			}
652 			tl4 += (MAX_LMAC_PER_BGX * MAX_QUEUES_PER_QSET);
653 			tl4 += (lmac * MAX_QUEUES_PER_QSET * MAX_SQS_PER_VF);
654 			tl4 += (svf * MAX_QUEUES_PER_QSET);
655 		}
656 	} else {
657 		tl4 = (vnic * MAX_QUEUES_PER_QSET);
658 	}
659 	tl4 += sq_idx;
660 
661 	tl3 = tl4 / (hw->tl4_cnt / hw->tl3_cnt);
662 	nic_reg_write(nic, NIC_PF_QSET_0_127_SQ_0_7_CFG2 |
663 		      ((u64)vnic << NIC_QS_ID_SHIFT) |
664 		      ((u32)sq_idx << NIC_Q_NUM_SHIFT), tl4);
665 	nic_reg_write(nic, NIC_PF_TL4_0_1023_CFG | (tl4 << 3),
666 		      ((u64)vnic << 27) | ((u32)sq_idx << 24) | rr_quantum);
667 
668 	nic_reg_write(nic, NIC_PF_TL3_0_255_CFG | (tl3 << 3), rr_quantum);
669 
670 	/* On 88xx 0-127 channels are for BGX0 and
671 	 * 127-255 channels for BGX1.
672 	 *
673 	 * On 81xx/83xx TL3_CHAN reg should be configured with channel
674 	 * within LMAC i.e 0-7 and not the actual channel number like on 88xx
675 	 */
676 	chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
677 	if (hw->tl1_per_bgx)
678 		nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), chan);
679 	else
680 		nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), 0);
681 
682 	/* Enable backpressure on the channel */
683 	nic_reg_write(nic, NIC_PF_CHAN_0_255_TX_CFG | (chan << 3), 1);
684 
685 	tl2 = tl3 >> 2;
686 	nic_reg_write(nic, NIC_PF_TL3A_0_63_CFG | (tl2 << 3), tl2);
687 	nic_reg_write(nic, NIC_PF_TL2_0_63_CFG | (tl2 << 3), rr_quantum);
688 	/* No priorities as of now */
689 	nic_reg_write(nic, NIC_PF_TL2_0_63_PRI | (tl2 << 3), 0x00);
690 
691 	/* Unlike 88xx where TL2s 0-31 transmits to TL1 '0' and rest to TL1 '1'
692 	 * on 81xx/83xx TL2 needs to be configured to transmit to one of the
693 	 * possible LMACs.
694 	 *
695 	 * This register doesn't exist on 88xx.
696 	 */
697 	if (!hw->tl1_per_bgx)
698 		nic_reg_write(nic, NIC_PF_TL2_LMAC | (tl2 << 3),
699 			      lmac + (bgx * MAX_LMAC_PER_BGX));
700 }
701 
702 /* Send primary nicvf pointer to secondary QS's VF */
703 static void nic_send_pnicvf(struct nicpf *nic, int sqs)
704 {
705 	union nic_mbx mbx = {};
706 
707 	mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
708 	mbx.nicvf.nicvf = nic->nicvf[nic->pqs_vf[sqs]];
709 	nic_send_msg_to_vf(nic, sqs, &mbx);
710 }
711 
712 /* Send SQS's nicvf pointer to primary QS's VF */
713 static void nic_send_snicvf(struct nicpf *nic, struct nicvf_ptr *nicvf)
714 {
715 	union nic_mbx mbx = {};
716 	int sqs_id = nic->vf_sqs[nicvf->vf_id][nicvf->sqs_id];
717 
718 	mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
719 	mbx.nicvf.sqs_id = nicvf->sqs_id;
720 	mbx.nicvf.nicvf = nic->nicvf[sqs_id];
721 	nic_send_msg_to_vf(nic, nicvf->vf_id, &mbx);
722 }
723 
724 /* Find next available Qset that can be assigned as a
725  * secondary Qset to a VF.
726  */
727 static int nic_nxt_avail_sqs(struct nicpf *nic)
728 {
729 	int sqs;
730 
731 	for (sqs = 0; sqs < nic->num_sqs_en; sqs++) {
732 		if (!nic->sqs_used[sqs])
733 			nic->sqs_used[sqs] = true;
734 		else
735 			continue;
736 		return sqs + nic->num_vf_en;
737 	}
738 	return -1;
739 }
740 
741 /* Allocate additional Qsets for requested VF */
742 static void nic_alloc_sqs(struct nicpf *nic, struct sqs_alloc *sqs)
743 {
744 	union nic_mbx mbx = {};
745 	int idx, alloc_qs = 0;
746 	int sqs_id;
747 
748 	if (!nic->num_sqs_en)
749 		goto send_mbox;
750 
751 	for (idx = 0; idx < sqs->qs_count; idx++) {
752 		sqs_id = nic_nxt_avail_sqs(nic);
753 		if (sqs_id < 0)
754 			break;
755 		nic->vf_sqs[sqs->vf_id][idx] = sqs_id;
756 		nic->pqs_vf[sqs_id] = sqs->vf_id;
757 		alloc_qs++;
758 	}
759 
760 send_mbox:
761 	mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
762 	mbx.sqs_alloc.vf_id = sqs->vf_id;
763 	mbx.sqs_alloc.qs_count = alloc_qs;
764 	nic_send_msg_to_vf(nic, sqs->vf_id, &mbx);
765 }
766 
767 static int nic_config_loopback(struct nicpf *nic, struct set_loopback *lbk)
768 {
769 	int bgx_idx, lmac_idx;
770 
771 	if (lbk->vf_id >= nic->num_vf_en)
772 		return -1;
773 
774 	bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
775 	lmac_idx = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
776 
777 	bgx_lmac_internal_loopback(nic->node, bgx_idx, lmac_idx, lbk->enable);
778 
779 	/* Enable moving average calculation.
780 	 * Keep the LVL/AVG delay to HW enforced minimum so that, not too many
781 	 * packets sneek in between average calculations.
782 	 */
783 	nic_reg_write(nic, NIC_PF_CQ_AVG_CFG,
784 		      (BIT_ULL(20) | 0x2ull << 14 | 0x1));
785 	nic_reg_write(nic, NIC_PF_RRM_AVG_CFG,
786 		      (BIT_ULL(20) | 0x3ull << 14 | 0x1));
787 
788 	return 0;
789 }
790 
791 /* Reset statistics counters */
792 static int nic_reset_stat_counters(struct nicpf *nic,
793 				   int vf, struct reset_stat_cfg *cfg)
794 {
795 	int i, stat, qnum;
796 	u64 reg_addr;
797 
798 	for (i = 0; i < RX_STATS_ENUM_LAST; i++) {
799 		if (cfg->rx_stat_mask & BIT(i)) {
800 			reg_addr = NIC_PF_VNIC_0_127_RX_STAT_0_13 |
801 				   (vf << NIC_QS_ID_SHIFT) |
802 				   (i << 3);
803 			nic_reg_write(nic, reg_addr, 0);
804 		}
805 	}
806 
807 	for (i = 0; i < TX_STATS_ENUM_LAST; i++) {
808 		if (cfg->tx_stat_mask & BIT(i)) {
809 			reg_addr = NIC_PF_VNIC_0_127_TX_STAT_0_4 |
810 				   (vf << NIC_QS_ID_SHIFT) |
811 				   (i << 3);
812 			nic_reg_write(nic, reg_addr, 0);
813 		}
814 	}
815 
816 	for (i = 0; i <= 15; i++) {
817 		qnum = i >> 1;
818 		stat = i & 1 ? 1 : 0;
819 		reg_addr = (vf << NIC_QS_ID_SHIFT) |
820 			   (qnum << NIC_Q_NUM_SHIFT) | (stat << 3);
821 		if (cfg->rq_stat_mask & BIT(i)) {
822 			reg_addr |= NIC_PF_QSET_0_127_RQ_0_7_STAT_0_1;
823 			nic_reg_write(nic, reg_addr, 0);
824 		}
825 		if (cfg->sq_stat_mask & BIT(i)) {
826 			reg_addr |= NIC_PF_QSET_0_127_SQ_0_7_STAT_0_1;
827 			nic_reg_write(nic, reg_addr, 0);
828 		}
829 	}
830 
831 	return 0;
832 }
833 
834 static void nic_enable_tunnel_parsing(struct nicpf *nic, int vf)
835 {
836 	u64 prot_def = (IPV6_PROT << 32) | (IPV4_PROT << 16) | ET_PROT;
837 	u64 vxlan_prot_def = (IPV6_PROT_DEF << 32) |
838 			      (IPV4_PROT_DEF) << 16 | ET_PROT_DEF;
839 
840 	/* Configure tunnel parsing parameters */
841 	nic_reg_write(nic, NIC_PF_RX_GENEVE_DEF,
842 		      (1ULL << 63 | UDP_GENEVE_PORT_NUM));
843 	nic_reg_write(nic, NIC_PF_RX_GENEVE_PROT_DEF,
844 		      ((7ULL << 61) | prot_def));
845 	nic_reg_write(nic, NIC_PF_RX_NVGRE_PROT_DEF,
846 		      ((7ULL << 61) | prot_def));
847 	nic_reg_write(nic, NIC_PF_RX_VXLAN_DEF_0_1,
848 		      ((1ULL << 63) | UDP_VXLAN_PORT_NUM));
849 	nic_reg_write(nic, NIC_PF_RX_VXLAN_PROT_DEF,
850 		      ((0xfULL << 60) | vxlan_prot_def));
851 }
852 
853 static void nic_enable_vf(struct nicpf *nic, int vf, bool enable)
854 {
855 	int bgx, lmac;
856 
857 	nic->vf_enabled[vf] = enable;
858 
859 	if (vf >= nic->num_vf_en)
860 		return;
861 
862 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
863 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
864 
865 	bgx_lmac_rx_tx_enable(nic->node, bgx, lmac, enable);
866 }
867 
868 static void nic_pause_frame(struct nicpf *nic, int vf, struct pfc *cfg)
869 {
870 	int bgx, lmac;
871 	struct pfc pfc;
872 	union nic_mbx mbx = {};
873 
874 	if (vf >= nic->num_vf_en)
875 		return;
876 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
877 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
878 
879 	if (cfg->get) {
880 		bgx_lmac_get_pfc(nic->node, bgx, lmac, &pfc);
881 		mbx.pfc.msg = NIC_MBOX_MSG_PFC;
882 		mbx.pfc.autoneg = pfc.autoneg;
883 		mbx.pfc.fc_rx = pfc.fc_rx;
884 		mbx.pfc.fc_tx = pfc.fc_tx;
885 		nic_send_msg_to_vf(nic, vf, &mbx);
886 	} else {
887 		bgx_lmac_set_pfc(nic->node, bgx, lmac, cfg);
888 		nic_mbx_send_ack(nic, vf);
889 	}
890 }
891 
892 /* Enable or disable HW timestamping by BGX for pkts received on a LMAC */
893 static void nic_config_timestamp(struct nicpf *nic, int vf, struct set_ptp *ptp)
894 {
895 	struct pkind_cfg *pkind;
896 	u8 lmac, bgx_idx;
897 	u64 pkind_val, pkind_idx;
898 
899 	if (vf >= nic->num_vf_en)
900 		return;
901 
902 	bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
903 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
904 
905 	pkind_idx = lmac + bgx_idx * MAX_LMAC_PER_BGX;
906 	pkind_val = nic_reg_read(nic, NIC_PF_PKIND_0_15_CFG | (pkind_idx << 3));
907 	pkind = (struct pkind_cfg *)&pkind_val;
908 
909 	if (ptp->enable && !pkind->hdr_sl) {
910 		/* Skiplen to exclude 8byte timestamp while parsing pkt
911 		 * If not configured, will result in L2 errors.
912 		 */
913 		pkind->hdr_sl = 4;
914 		/* Adjust max packet length allowed */
915 		pkind->maxlen += (pkind->hdr_sl * 2);
916 		bgx_config_timestamping(nic->node, bgx_idx, lmac, true);
917 		nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7 | (1 << 3),
918 			      (ETYPE_ALG_ENDPARSE << 16) | ETH_P_1588);
919 	} else if (!ptp->enable && pkind->hdr_sl) {
920 		pkind->maxlen -= (pkind->hdr_sl * 2);
921 		pkind->hdr_sl = 0;
922 		bgx_config_timestamping(nic->node, bgx_idx, lmac, false);
923 		nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7 | (1 << 3),
924 			      (ETYPE_ALG_SKIP << 16) | ETH_P_8021Q);
925 	}
926 
927 	nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (pkind_idx << 3), pkind_val);
928 }
929 
930 /* Interrupt handler to handle mailbox messages from VFs */
931 static void nic_handle_mbx_intr(struct nicpf *nic, int vf)
932 {
933 	union nic_mbx mbx = {};
934 	u64 *mbx_data;
935 	u64 mbx_addr;
936 	u64 reg_addr;
937 	u64 cfg;
938 	int bgx, lmac;
939 	int i;
940 	int ret = 0;
941 
942 	nic->mbx_lock[vf] = true;
943 
944 	mbx_addr = nic_get_mbx_addr(vf);
945 	mbx_data = (u64 *)&mbx;
946 
947 	for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
948 		*mbx_data = nic_reg_read(nic, mbx_addr);
949 		mbx_data++;
950 		mbx_addr += sizeof(u64);
951 	}
952 
953 	dev_dbg(&nic->pdev->dev, "%s: Mailbox msg 0x%02x from VF%d\n",
954 		__func__, mbx.msg.msg, vf);
955 	switch (mbx.msg.msg) {
956 	case NIC_MBOX_MSG_READY:
957 		nic_mbx_send_ready(nic, vf);
958 		if (vf < nic->num_vf_en) {
959 			nic->link[vf] = 0;
960 			nic->duplex[vf] = 0;
961 			nic->speed[vf] = 0;
962 		}
963 		goto unlock;
964 	case NIC_MBOX_MSG_QS_CFG:
965 		reg_addr = NIC_PF_QSET_0_127_CFG |
966 			   (mbx.qs.num << NIC_QS_ID_SHIFT);
967 		cfg = mbx.qs.cfg;
968 		/* Check if its a secondary Qset */
969 		if (vf >= nic->num_vf_en) {
970 			cfg = cfg & (~0x7FULL);
971 			/* Assign this Qset to primary Qset's VF */
972 			cfg |= nic->pqs_vf[vf];
973 		}
974 		nic_reg_write(nic, reg_addr, cfg);
975 		break;
976 	case NIC_MBOX_MSG_RQ_CFG:
977 		reg_addr = NIC_PF_QSET_0_127_RQ_0_7_CFG |
978 			   (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
979 			   (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
980 		nic_reg_write(nic, reg_addr, mbx.rq.cfg);
981 		/* Enable CQE_RX2_S extension in CQE_RX descriptor.
982 		 * This gets appended by default on 81xx/83xx chips,
983 		 * for consistency enabling the same on 88xx pass2
984 		 * where this is introduced.
985 		 */
986 		if (pass2_silicon(nic->pdev))
987 			nic_reg_write(nic, NIC_PF_RX_CFG, 0x01);
988 		if (!pass1_silicon(nic->pdev))
989 			nic_enable_tunnel_parsing(nic, vf);
990 		break;
991 	case NIC_MBOX_MSG_RQ_BP_CFG:
992 		reg_addr = NIC_PF_QSET_0_127_RQ_0_7_BP_CFG |
993 			   (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
994 			   (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
995 		nic_reg_write(nic, reg_addr, mbx.rq.cfg);
996 		break;
997 	case NIC_MBOX_MSG_RQ_SW_SYNC:
998 		ret = nic_rcv_queue_sw_sync(nic);
999 		break;
1000 	case NIC_MBOX_MSG_RQ_DROP_CFG:
1001 		reg_addr = NIC_PF_QSET_0_127_RQ_0_7_DROP_CFG |
1002 			   (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
1003 			   (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
1004 		nic_reg_write(nic, reg_addr, mbx.rq.cfg);
1005 		break;
1006 	case NIC_MBOX_MSG_SQ_CFG:
1007 		reg_addr = NIC_PF_QSET_0_127_SQ_0_7_CFG |
1008 			   (mbx.sq.qs_num << NIC_QS_ID_SHIFT) |
1009 			   (mbx.sq.sq_num << NIC_Q_NUM_SHIFT);
1010 		nic_reg_write(nic, reg_addr, mbx.sq.cfg);
1011 		nic_tx_channel_cfg(nic, mbx.qs.num, &mbx.sq);
1012 		break;
1013 	case NIC_MBOX_MSG_SET_MAC:
1014 		if (vf >= nic->num_vf_en) {
1015 			ret = -1; /* NACK */
1016 			break;
1017 		}
1018 		lmac = mbx.mac.vf_id;
1019 		bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
1020 		lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
1021 		bgx_set_lmac_mac(nic->node, bgx, lmac, mbx.mac.mac_addr);
1022 		break;
1023 	case NIC_MBOX_MSG_SET_MAX_FRS:
1024 		ret = nic_update_hw_frs(nic, mbx.frs.max_frs,
1025 					mbx.frs.vf_id);
1026 		break;
1027 	case NIC_MBOX_MSG_CPI_CFG:
1028 		nic_config_cpi(nic, &mbx.cpi_cfg);
1029 		break;
1030 	case NIC_MBOX_MSG_RSS_SIZE:
1031 		nic_send_rss_size(nic, vf);
1032 		goto unlock;
1033 	case NIC_MBOX_MSG_RSS_CFG:
1034 	case NIC_MBOX_MSG_RSS_CFG_CONT:
1035 		nic_config_rss(nic, &mbx.rss_cfg);
1036 		break;
1037 	case NIC_MBOX_MSG_CFG_DONE:
1038 		/* Last message of VF config msg sequence */
1039 		nic_enable_vf(nic, vf, true);
1040 		goto unlock;
1041 	case NIC_MBOX_MSG_SHUTDOWN:
1042 		/* First msg in VF teardown sequence */
1043 		if (vf >= nic->num_vf_en)
1044 			nic->sqs_used[vf - nic->num_vf_en] = false;
1045 		nic->pqs_vf[vf] = 0;
1046 		nic_enable_vf(nic, vf, false);
1047 		break;
1048 	case NIC_MBOX_MSG_ALLOC_SQS:
1049 		nic_alloc_sqs(nic, &mbx.sqs_alloc);
1050 		goto unlock;
1051 	case NIC_MBOX_MSG_NICVF_PTR:
1052 		nic->nicvf[vf] = mbx.nicvf.nicvf;
1053 		break;
1054 	case NIC_MBOX_MSG_PNICVF_PTR:
1055 		nic_send_pnicvf(nic, vf);
1056 		goto unlock;
1057 	case NIC_MBOX_MSG_SNICVF_PTR:
1058 		nic_send_snicvf(nic, &mbx.nicvf);
1059 		goto unlock;
1060 	case NIC_MBOX_MSG_BGX_STATS:
1061 		nic_get_bgx_stats(nic, &mbx.bgx_stats);
1062 		goto unlock;
1063 	case NIC_MBOX_MSG_LOOPBACK:
1064 		ret = nic_config_loopback(nic, &mbx.lbk);
1065 		break;
1066 	case NIC_MBOX_MSG_RESET_STAT_COUNTER:
1067 		ret = nic_reset_stat_counters(nic, vf, &mbx.reset_stat);
1068 		break;
1069 	case NIC_MBOX_MSG_PFC:
1070 		nic_pause_frame(nic, vf, &mbx.pfc);
1071 		goto unlock;
1072 	case NIC_MBOX_MSG_PTP_CFG:
1073 		nic_config_timestamp(nic, vf, &mbx.ptp);
1074 		break;
1075 	default:
1076 		dev_err(&nic->pdev->dev,
1077 			"Invalid msg from VF%d, msg 0x%x\n", vf, mbx.msg.msg);
1078 		break;
1079 	}
1080 
1081 	if (!ret) {
1082 		nic_mbx_send_ack(nic, vf);
1083 	} else if (mbx.msg.msg != NIC_MBOX_MSG_READY) {
1084 		dev_err(&nic->pdev->dev, "NACK for MBOX 0x%02x from VF %d\n",
1085 			mbx.msg.msg, vf);
1086 		nic_mbx_send_nack(nic, vf);
1087 	}
1088 unlock:
1089 	nic->mbx_lock[vf] = false;
1090 }
1091 
1092 static irqreturn_t nic_mbx_intr_handler(int irq, void *nic_irq)
1093 {
1094 	struct nicpf *nic = (struct nicpf *)nic_irq;
1095 	int mbx;
1096 	u64 intr;
1097 	u8  vf, vf_per_mbx_reg = 64;
1098 
1099 	if (irq == pci_irq_vector(nic->pdev, NIC_PF_INTR_ID_MBOX0))
1100 		mbx = 0;
1101 	else
1102 		mbx = 1;
1103 
1104 	intr = nic_reg_read(nic, NIC_PF_MAILBOX_INT + (mbx << 3));
1105 	dev_dbg(&nic->pdev->dev, "PF interrupt Mbox%d 0x%llx\n", mbx, intr);
1106 	for (vf = 0; vf < vf_per_mbx_reg; vf++) {
1107 		if (intr & (1ULL << vf)) {
1108 			dev_dbg(&nic->pdev->dev, "Intr from VF %d\n",
1109 				vf + (mbx * vf_per_mbx_reg));
1110 
1111 			nic_handle_mbx_intr(nic, vf + (mbx * vf_per_mbx_reg));
1112 			nic_clear_mbx_intr(nic, vf, mbx);
1113 		}
1114 	}
1115 	return IRQ_HANDLED;
1116 }
1117 
1118 static void nic_free_all_interrupts(struct nicpf *nic)
1119 {
1120 	int irq;
1121 
1122 	for (irq = 0; irq < nic->num_vec; irq++) {
1123 		if (nic->irq_allocated[irq])
1124 			free_irq(pci_irq_vector(nic->pdev, irq), nic);
1125 		nic->irq_allocated[irq] = false;
1126 	}
1127 }
1128 
1129 static int nic_register_interrupts(struct nicpf *nic)
1130 {
1131 	int i, ret;
1132 	nic->num_vec = pci_msix_vec_count(nic->pdev);
1133 
1134 	/* Enable MSI-X */
1135 	ret = pci_alloc_irq_vectors(nic->pdev, nic->num_vec, nic->num_vec,
1136 				    PCI_IRQ_MSIX);
1137 	if (ret < 0) {
1138 		dev_err(&nic->pdev->dev,
1139 			"Request for #%d msix vectors failed, returned %d\n",
1140 			   nic->num_vec, ret);
1141 		return 1;
1142 	}
1143 
1144 	/* Register mailbox interrupt handler */
1145 	for (i = NIC_PF_INTR_ID_MBOX0; i < nic->num_vec; i++) {
1146 		sprintf(nic->irq_name[i],
1147 			"NICPF Mbox%d", (i - NIC_PF_INTR_ID_MBOX0));
1148 
1149 		ret = request_irq(pci_irq_vector(nic->pdev, i),
1150 				  nic_mbx_intr_handler, 0,
1151 				  nic->irq_name[i], nic);
1152 		if (ret)
1153 			goto fail;
1154 
1155 		nic->irq_allocated[i] = true;
1156 	}
1157 
1158 	/* Enable mailbox interrupt */
1159 	nic_enable_mbx_intr(nic);
1160 	return 0;
1161 
1162 fail:
1163 	dev_err(&nic->pdev->dev, "Request irq failed\n");
1164 	nic_free_all_interrupts(nic);
1165 	pci_free_irq_vectors(nic->pdev);
1166 	nic->num_vec = 0;
1167 	return ret;
1168 }
1169 
1170 static void nic_unregister_interrupts(struct nicpf *nic)
1171 {
1172 	nic_free_all_interrupts(nic);
1173 	pci_free_irq_vectors(nic->pdev);
1174 	nic->num_vec = 0;
1175 }
1176 
1177 static int nic_num_sqs_en(struct nicpf *nic, int vf_en)
1178 {
1179 	int pos, sqs_per_vf = MAX_SQS_PER_VF_SINGLE_NODE;
1180 	u16 total_vf;
1181 
1182 	/* Secondary Qsets are needed only if CPU count is
1183 	 * morethan MAX_QUEUES_PER_QSET.
1184 	 */
1185 	if (num_online_cpus() <= MAX_QUEUES_PER_QSET)
1186 		return 0;
1187 
1188 	/* Check if its a multi-node environment */
1189 	if (nr_node_ids > 1)
1190 		sqs_per_vf = MAX_SQS_PER_VF;
1191 
1192 	pos = pci_find_ext_capability(nic->pdev, PCI_EXT_CAP_ID_SRIOV);
1193 	pci_read_config_word(nic->pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf);
1194 	return min(total_vf - vf_en, vf_en * sqs_per_vf);
1195 }
1196 
1197 static int nic_sriov_init(struct pci_dev *pdev, struct nicpf *nic)
1198 {
1199 	int pos = 0;
1200 	int vf_en;
1201 	int err;
1202 	u16 total_vf_cnt;
1203 
1204 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1205 	if (!pos) {
1206 		dev_err(&pdev->dev, "SRIOV capability is not found in PCIe config space\n");
1207 		return -ENODEV;
1208 	}
1209 
1210 	pci_read_config_word(pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf_cnt);
1211 	if (total_vf_cnt < nic->num_vf_en)
1212 		nic->num_vf_en = total_vf_cnt;
1213 
1214 	if (!total_vf_cnt)
1215 		return 0;
1216 
1217 	vf_en = nic->num_vf_en;
1218 	nic->num_sqs_en = nic_num_sqs_en(nic, nic->num_vf_en);
1219 	vf_en += nic->num_sqs_en;
1220 
1221 	err = pci_enable_sriov(pdev, vf_en);
1222 	if (err) {
1223 		dev_err(&pdev->dev, "SRIOV enable failed, num VF is %d\n",
1224 			vf_en);
1225 		nic->num_vf_en = 0;
1226 		return err;
1227 	}
1228 
1229 	dev_info(&pdev->dev, "SRIOV enabled, number of VF available %d\n",
1230 		 vf_en);
1231 
1232 	nic->flags |= NIC_SRIOV_ENABLED;
1233 	return 0;
1234 }
1235 
1236 /* Poll for BGX LMAC link status and update corresponding VF
1237  * if there is a change, valid only if internal L2 switch
1238  * is not present otherwise VF link is always treated as up
1239  */
1240 static void nic_poll_for_link(struct work_struct *work)
1241 {
1242 	union nic_mbx mbx = {};
1243 	struct nicpf *nic;
1244 	struct bgx_link_status link;
1245 	u8 vf, bgx, lmac;
1246 
1247 	nic = container_of(work, struct nicpf, dwork.work);
1248 
1249 	mbx.link_status.msg = NIC_MBOX_MSG_BGX_LINK_CHANGE;
1250 
1251 	for (vf = 0; vf < nic->num_vf_en; vf++) {
1252 		/* Poll only if VF is UP */
1253 		if (!nic->vf_enabled[vf])
1254 			continue;
1255 
1256 		/* Get BGX, LMAC indices for the VF */
1257 		bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1258 		lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1259 		/* Get interface link status */
1260 		bgx_get_lmac_link_state(nic->node, bgx, lmac, &link);
1261 
1262 		/* Inform VF only if link status changed */
1263 		if (nic->link[vf] == link.link_up)
1264 			continue;
1265 
1266 		if (!nic->mbx_lock[vf]) {
1267 			nic->link[vf] = link.link_up;
1268 			nic->duplex[vf] = link.duplex;
1269 			nic->speed[vf] = link.speed;
1270 
1271 			/* Send a mbox message to VF with current link status */
1272 			mbx.link_status.link_up = link.link_up;
1273 			mbx.link_status.duplex = link.duplex;
1274 			mbx.link_status.speed = link.speed;
1275 			mbx.link_status.mac_type = link.mac_type;
1276 			nic_send_msg_to_vf(nic, vf, &mbx);
1277 		}
1278 	}
1279 	queue_delayed_work(nic->check_link, &nic->dwork, HZ * 2);
1280 }
1281 
1282 static int nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1283 {
1284 	struct device *dev = &pdev->dev;
1285 	struct nicpf *nic;
1286 	u8     max_lmac;
1287 	int    err;
1288 
1289 	BUILD_BUG_ON(sizeof(union nic_mbx) > 16);
1290 
1291 	nic = devm_kzalloc(dev, sizeof(*nic), GFP_KERNEL);
1292 	if (!nic)
1293 		return -ENOMEM;
1294 
1295 	nic->hw = devm_kzalloc(dev, sizeof(struct hw_info), GFP_KERNEL);
1296 	if (!nic->hw)
1297 		return -ENOMEM;
1298 
1299 	pci_set_drvdata(pdev, nic);
1300 
1301 	nic->pdev = pdev;
1302 
1303 	err = pci_enable_device(pdev);
1304 	if (err) {
1305 		dev_err(dev, "Failed to enable PCI device\n");
1306 		pci_set_drvdata(pdev, NULL);
1307 		return err;
1308 	}
1309 
1310 	err = pci_request_regions(pdev, DRV_NAME);
1311 	if (err) {
1312 		dev_err(dev, "PCI request regions failed 0x%x\n", err);
1313 		goto err_disable_device;
1314 	}
1315 
1316 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
1317 	if (err) {
1318 		dev_err(dev, "Unable to get usable DMA configuration\n");
1319 		goto err_release_regions;
1320 	}
1321 
1322 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
1323 	if (err) {
1324 		dev_err(dev, "Unable to get 48-bit DMA for consistent allocations\n");
1325 		goto err_release_regions;
1326 	}
1327 
1328 	/* MAP PF's configuration registers */
1329 	nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1330 	if (!nic->reg_base) {
1331 		dev_err(dev, "Cannot map config register space, aborting\n");
1332 		err = -ENOMEM;
1333 		goto err_release_regions;
1334 	}
1335 
1336 	nic->node = nic_get_node_id(pdev);
1337 
1338 	/* Get HW capability info */
1339 	nic_get_hw_info(nic);
1340 
1341 	/* Allocate memory for LMAC tracking elements */
1342 	err = -ENOMEM;
1343 	max_lmac = nic->hw->bgx_cnt * MAX_LMAC_PER_BGX;
1344 
1345 	nic->vf_lmac_map = devm_kmalloc_array(dev, max_lmac, sizeof(u8),
1346 					      GFP_KERNEL);
1347 	if (!nic->vf_lmac_map)
1348 		goto err_release_regions;
1349 
1350 	nic->link = devm_kmalloc_array(dev, max_lmac, sizeof(u8), GFP_KERNEL);
1351 	if (!nic->link)
1352 		goto err_release_regions;
1353 
1354 	nic->duplex = devm_kmalloc_array(dev, max_lmac, sizeof(u8), GFP_KERNEL);
1355 	if (!nic->duplex)
1356 		goto err_release_regions;
1357 
1358 	nic->speed = devm_kmalloc_array(dev, max_lmac, sizeof(u32), GFP_KERNEL);
1359 	if (!nic->speed)
1360 		goto err_release_regions;
1361 
1362 	/* Initialize hardware */
1363 	nic_init_hw(nic);
1364 
1365 	nic_set_lmac_vf_mapping(nic);
1366 
1367 	/* Register interrupts */
1368 	err = nic_register_interrupts(nic);
1369 	if (err)
1370 		goto err_release_regions;
1371 
1372 	/* Configure SRIOV */
1373 	err = nic_sriov_init(pdev, nic);
1374 	if (err)
1375 		goto err_unregister_interrupts;
1376 
1377 	/* Register a physical link status poll fn() */
1378 	nic->check_link = alloc_workqueue("check_link_status",
1379 					  WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1380 	if (!nic->check_link) {
1381 		err = -ENOMEM;
1382 		goto err_disable_sriov;
1383 	}
1384 
1385 	INIT_DELAYED_WORK(&nic->dwork, nic_poll_for_link);
1386 	queue_delayed_work(nic->check_link, &nic->dwork, 0);
1387 
1388 	return 0;
1389 
1390 err_disable_sriov:
1391 	if (nic->flags & NIC_SRIOV_ENABLED)
1392 		pci_disable_sriov(pdev);
1393 err_unregister_interrupts:
1394 	nic_unregister_interrupts(nic);
1395 err_release_regions:
1396 	pci_release_regions(pdev);
1397 err_disable_device:
1398 	pci_disable_device(pdev);
1399 	pci_set_drvdata(pdev, NULL);
1400 	return err;
1401 }
1402 
1403 static void nic_remove(struct pci_dev *pdev)
1404 {
1405 	struct nicpf *nic = pci_get_drvdata(pdev);
1406 
1407 	if (nic->flags & NIC_SRIOV_ENABLED)
1408 		pci_disable_sriov(pdev);
1409 
1410 	if (nic->check_link) {
1411 		/* Destroy work Queue */
1412 		cancel_delayed_work_sync(&nic->dwork);
1413 		destroy_workqueue(nic->check_link);
1414 	}
1415 
1416 	nic_unregister_interrupts(nic);
1417 	pci_release_regions(pdev);
1418 
1419 	pci_disable_device(pdev);
1420 	pci_set_drvdata(pdev, NULL);
1421 }
1422 
1423 static struct pci_driver nic_driver = {
1424 	.name = DRV_NAME,
1425 	.id_table = nic_id_table,
1426 	.probe = nic_probe,
1427 	.remove = nic_remove,
1428 };
1429 
1430 static int __init nic_init_module(void)
1431 {
1432 	pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1433 
1434 	return pci_register_driver(&nic_driver);
1435 }
1436 
1437 static void __exit nic_cleanup_module(void)
1438 {
1439 	pci_unregister_driver(&nic_driver);
1440 }
1441 
1442 module_init(nic_init_module);
1443 module_exit(nic_cleanup_module);
1444