xref: /openbmc/linux/drivers/net/ethernet/cavium/thunder/nic_main.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24863dea3SSunil Goutham /*
34863dea3SSunil Goutham  * Copyright (C) 2015 Cavium, Inc.
44863dea3SSunil Goutham  */
54863dea3SSunil Goutham 
64863dea3SSunil Goutham #include <linux/module.h>
74863dea3SSunil Goutham #include <linux/interrupt.h>
84863dea3SSunil Goutham #include <linux/pci.h>
94863dea3SSunil Goutham #include <linux/etherdevice.h>
104863dea3SSunil Goutham #include <linux/of.h>
11712c3185SSunil Goutham #include <linux/if_vlan.h>
124863dea3SSunil Goutham 
134863dea3SSunil Goutham #include "nic_reg.h"
144863dea3SSunil Goutham #include "nic.h"
154863dea3SSunil Goutham #include "q_struct.h"
164863dea3SSunil Goutham #include "thunder_bgx.h"
174863dea3SSunil Goutham 
186b9e6547SVadim Lomovtsev #define DRV_NAME	"nicpf"
194863dea3SSunil Goutham #define DRV_VERSION	"1.0"
204863dea3SSunil Goutham 
21aba4a263SVadim Lomovtsev #define NIC_VF_PER_MBX_REG      64
22aba4a263SVadim Lomovtsev 
23a5c3d498SSunil Goutham struct hw_info {
24a5c3d498SSunil Goutham 	u8		bgx_cnt;
25a5c3d498SSunil Goutham 	u8		chans_per_lmac;
26a5c3d498SSunil Goutham 	u8		chans_per_bgx; /* Rx/Tx chans */
270025d93eSSunil Goutham 	u8		chans_per_rgx;
280025d93eSSunil Goutham 	u8		chans_per_lbk;
29a5c3d498SSunil Goutham 	u16		cpi_cnt;
30a5c3d498SSunil Goutham 	u16		rssi_cnt;
31a5c3d498SSunil Goutham 	u16		rss_ind_tbl_size;
32a5c3d498SSunil Goutham 	u16		tl4_cnt;
33a5c3d498SSunil Goutham 	u16		tl3_cnt;
34a5c3d498SSunil Goutham 	u8		tl2_cnt;
35a5c3d498SSunil Goutham 	u8		tl1_cnt;
36a5c3d498SSunil Goutham 	bool		tl1_per_bgx; /* TL1 per BGX or per LMAC */
37a5c3d498SSunil Goutham };
38a5c3d498SSunil Goutham 
394863dea3SSunil Goutham struct nicpf {
404863dea3SSunil Goutham 	struct pci_dev		*pdev;
41a5c3d498SSunil Goutham 	struct hw_info          *hw;
424863dea3SSunil Goutham 	u8			node;
434863dea3SSunil Goutham 	unsigned int		flags;
444863dea3SSunil Goutham 	u8			num_vf_en;      /* No of VF enabled */
454863dea3SSunil Goutham 	bool			vf_enabled[MAX_NUM_VFS_SUPPORTED];
464863dea3SSunil Goutham 	void __iomem		*reg_base;       /* Register start address */
4792dc8769SSunil Goutham 	u8			num_sqs_en;	/* Secondary qsets enabled */
4892dc8769SSunil Goutham 	u64			nicvf[MAX_NUM_VFS_SUPPORTED];
4992dc8769SSunil Goutham 	u8			vf_sqs[MAX_NUM_VFS_SUPPORTED][MAX_SQS_PER_VF];
5092dc8769SSunil Goutham 	u8			pqs_vf[MAX_NUM_VFS_SUPPORTED];
5192dc8769SSunil Goutham 	bool			sqs_used[MAX_NUM_VFS_SUPPORTED];
524863dea3SSunil Goutham 	struct pkind_cfg	pkind;
534863dea3SSunil Goutham #define	NIC_SET_VF_LMAC_MAP(bgx, lmac)	(((bgx & 0xF) << 4) | (lmac & 0xF))
544863dea3SSunil Goutham #define	NIC_GET_BGX_FROM_VF_LMAC_MAP(map)	((map >> 4) & 0xF)
554863dea3SSunil Goutham #define	NIC_GET_LMAC_FROM_VF_LMAC_MAP(map)	(map & 0xF)
56949b5331SSunil Goutham 	u8			*vf_lmac_map;
574863dea3SSunil Goutham 	u16			cpi_base[MAX_NUM_VFS_SUPPORTED];
5834411b68SThanneeru Srinivasulu 	u16			rssi_base[MAX_NUM_VFS_SUPPORTED];
594863dea3SSunil Goutham 
604863dea3SSunil Goutham 	/* MSI-X */
614863dea3SSunil Goutham 	u8			num_vec;
62*6b292a04SThomas Gleixner 	unsigned int		irq_allocated[NIC_PF_MSIX_VECTORS];
6352358aadSSunil Goutham 	char			irq_name[NIC_PF_MSIX_VECTORS][20];
644863dea3SSunil Goutham };
654863dea3SSunil Goutham 
664863dea3SSunil Goutham /* Supported devices */
674863dea3SSunil Goutham static const struct pci_device_id nic_id_table[] = {
684863dea3SSunil Goutham 	{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_NIC_PF) },
694863dea3SSunil Goutham 	{ 0, }  /* end of table */
704863dea3SSunil Goutham };
714863dea3SSunil Goutham 
724863dea3SSunil Goutham MODULE_AUTHOR("Sunil Goutham");
734863dea3SSunil Goutham MODULE_DESCRIPTION("Cavium Thunder NIC Physical Function Driver");
744863dea3SSunil Goutham MODULE_LICENSE("GPL v2");
754863dea3SSunil Goutham MODULE_VERSION(DRV_VERSION);
764863dea3SSunil Goutham MODULE_DEVICE_TABLE(pci, nic_id_table);
774863dea3SSunil Goutham 
784863dea3SSunil Goutham /* The Cavium ThunderX network controller can *only* be found in SoCs
794863dea3SSunil Goutham  * containing the ThunderX ARM64 CPU implementation.  All accesses to the device
804863dea3SSunil Goutham  * registers on this platform are implicitly strongly ordered with respect
814863dea3SSunil Goutham  * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
824863dea3SSunil Goutham  * with no memory barriers in this driver.  The readq()/writeq() functions add
834863dea3SSunil Goutham  * explicit ordering operation which in this case are redundant, and only
844863dea3SSunil Goutham  * add overhead.
854863dea3SSunil Goutham  */
864863dea3SSunil Goutham 
874863dea3SSunil Goutham /* Register read/write APIs */
nic_reg_write(struct nicpf * nic,u64 offset,u64 val)884863dea3SSunil Goutham static void nic_reg_write(struct nicpf *nic, u64 offset, u64 val)
894863dea3SSunil Goutham {
904863dea3SSunil Goutham 	writeq_relaxed(val, nic->reg_base + offset);
914863dea3SSunil Goutham }
924863dea3SSunil Goutham 
nic_reg_read(struct nicpf * nic,u64 offset)934863dea3SSunil Goutham static u64 nic_reg_read(struct nicpf *nic, u64 offset)
944863dea3SSunil Goutham {
954863dea3SSunil Goutham 	return readq_relaxed(nic->reg_base + offset);
964863dea3SSunil Goutham }
974863dea3SSunil Goutham 
984863dea3SSunil Goutham /* PF -> VF mailbox communication APIs */
nic_enable_mbx_intr(struct nicpf * nic)994863dea3SSunil Goutham static void nic_enable_mbx_intr(struct nicpf *nic)
1004863dea3SSunil Goutham {
10152358aadSSunil Goutham 	int vf_cnt = pci_sriov_get_totalvfs(nic->pdev);
10252358aadSSunil Goutham 
10352358aadSSunil Goutham #define INTR_MASK(vfs) ((vfs < 64) ? (BIT_ULL(vfs) - 1) : (~0ull))
10452358aadSSunil Goutham 
10552358aadSSunil Goutham 	/* Clear it, to avoid spurious interrupts (if any) */
10652358aadSSunil Goutham 	nic_reg_write(nic, NIC_PF_MAILBOX_INT, INTR_MASK(vf_cnt));
10752358aadSSunil Goutham 
10852358aadSSunil Goutham 	/* Enable mailbox interrupt for all VFs */
10952358aadSSunil Goutham 	nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S, INTR_MASK(vf_cnt));
11052358aadSSunil Goutham 	/* One mailbox intr enable reg per 64 VFs */
11152358aadSSunil Goutham 	if (vf_cnt > 64) {
11252358aadSSunil Goutham 		nic_reg_write(nic, NIC_PF_MAILBOX_INT + sizeof(u64),
11352358aadSSunil Goutham 			      INTR_MASK(vf_cnt - 64));
11452358aadSSunil Goutham 		nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S + sizeof(u64),
11552358aadSSunil Goutham 			      INTR_MASK(vf_cnt - 64));
11652358aadSSunil Goutham 	}
1174863dea3SSunil Goutham }
1184863dea3SSunil Goutham 
nic_clear_mbx_intr(struct nicpf * nic,int vf,int mbx_reg)1194863dea3SSunil Goutham static void nic_clear_mbx_intr(struct nicpf *nic, int vf, int mbx_reg)
1204863dea3SSunil Goutham {
1214863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_MAILBOX_INT + (mbx_reg << 3), BIT_ULL(vf));
1224863dea3SSunil Goutham }
1234863dea3SSunil Goutham 
nic_get_mbx_addr(int vf)1244863dea3SSunil Goutham static u64 nic_get_mbx_addr(int vf)
1254863dea3SSunil Goutham {
1264863dea3SSunil Goutham 	return NIC_PF_VF_0_127_MAILBOX_0_1 + (vf << NIC_VF_NUM_SHIFT);
1274863dea3SSunil Goutham }
1284863dea3SSunil Goutham 
1294863dea3SSunil Goutham /* Send a mailbox message to VF
1304863dea3SSunil Goutham  * @vf: vf to which this message to be sent
1314863dea3SSunil Goutham  * @mbx: Message to be sent
1324863dea3SSunil Goutham  */
nic_send_msg_to_vf(struct nicpf * nic,int vf,union nic_mbx * mbx)1334863dea3SSunil Goutham static void nic_send_msg_to_vf(struct nicpf *nic, int vf, union nic_mbx *mbx)
1344863dea3SSunil Goutham {
1354863dea3SSunil Goutham 	void __iomem *mbx_addr = nic->reg_base + nic_get_mbx_addr(vf);
1364863dea3SSunil Goutham 	u64 *msg = (u64 *)mbx;
1374863dea3SSunil Goutham 
1384863dea3SSunil Goutham 	/* In first revision HW, mbox interrupt is triggerred
1394863dea3SSunil Goutham 	 * when PF writes to MBOX(1), in next revisions when
1404863dea3SSunil Goutham 	 * PF writes to MBOX(0)
1414863dea3SSunil Goutham 	 */
14240fb5f8aSSunil Goutham 	if (pass1_silicon(nic->pdev)) {
1434863dea3SSunil Goutham 		/* see the comment for nic_reg_write()/nic_reg_read()
1444863dea3SSunil Goutham 		 * functions above
1454863dea3SSunil Goutham 		 */
1464863dea3SSunil Goutham 		writeq_relaxed(msg[0], mbx_addr);
1474863dea3SSunil Goutham 		writeq_relaxed(msg[1], mbx_addr + 8);
1484863dea3SSunil Goutham 	} else {
1494863dea3SSunil Goutham 		writeq_relaxed(msg[1], mbx_addr + 8);
1504863dea3SSunil Goutham 		writeq_relaxed(msg[0], mbx_addr);
1514863dea3SSunil Goutham 	}
1524863dea3SSunil Goutham }
1534863dea3SSunil Goutham 
1544863dea3SSunil Goutham /* Responds to VF's READY message with VF's
1554863dea3SSunil Goutham  * ID, node, MAC address e.t.c
1564863dea3SSunil Goutham  * @vf: VF which sent READY message
1574863dea3SSunil Goutham  */
nic_mbx_send_ready(struct nicpf * nic,int vf)1584863dea3SSunil Goutham static void nic_mbx_send_ready(struct nicpf *nic, int vf)
1594863dea3SSunil Goutham {
1604863dea3SSunil Goutham 	union nic_mbx mbx = {};
1614863dea3SSunil Goutham 	int bgx_idx, lmac;
1624863dea3SSunil Goutham 	const char *mac;
1634863dea3SSunil Goutham 
1644863dea3SSunil Goutham 	mbx.nic_cfg.msg = NIC_MBOX_MSG_READY;
1654863dea3SSunil Goutham 	mbx.nic_cfg.vf_id = vf;
1664863dea3SSunil Goutham 
1674863dea3SSunil Goutham 	mbx.nic_cfg.tns_mode = NIC_TNS_BYPASS_MODE;
1684863dea3SSunil Goutham 
169949b5331SSunil Goutham 	if (vf < nic->num_vf_en) {
1704863dea3SSunil Goutham 		bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1714863dea3SSunil Goutham 		lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1724863dea3SSunil Goutham 
1734863dea3SSunil Goutham 		mac = bgx_get_lmac_mac(nic->node, bgx_idx, lmac);
1744863dea3SSunil Goutham 		if (mac)
1754863dea3SSunil Goutham 			ether_addr_copy((u8 *)&mbx.nic_cfg.mac_addr, mac);
17692dc8769SSunil Goutham 	}
17792dc8769SSunil Goutham 	mbx.nic_cfg.sqs_mode = (vf >= nic->num_vf_en) ? true : false;
1784863dea3SSunil Goutham 	mbx.nic_cfg.node_id = nic->node;
179d77a2384SSunil Goutham 
180949b5331SSunil Goutham 	mbx.nic_cfg.loopback_supported = vf < nic->num_vf_en;
181d77a2384SSunil Goutham 
1824863dea3SSunil Goutham 	nic_send_msg_to_vf(nic, vf, &mbx);
1834863dea3SSunil Goutham }
1844863dea3SSunil Goutham 
1854863dea3SSunil Goutham /* ACKs VF's mailbox message
1864863dea3SSunil Goutham  * @vf: VF to which ACK to be sent
1874863dea3SSunil Goutham  */
nic_mbx_send_ack(struct nicpf * nic,int vf)1884863dea3SSunil Goutham static void nic_mbx_send_ack(struct nicpf *nic, int vf)
1894863dea3SSunil Goutham {
1904863dea3SSunil Goutham 	union nic_mbx mbx = {};
1914863dea3SSunil Goutham 
1924863dea3SSunil Goutham 	mbx.msg.msg = NIC_MBOX_MSG_ACK;
1934863dea3SSunil Goutham 	nic_send_msg_to_vf(nic, vf, &mbx);
1944863dea3SSunil Goutham }
1954863dea3SSunil Goutham 
1964863dea3SSunil Goutham /* NACKs VF's mailbox message that PF is not able to
1974863dea3SSunil Goutham  * complete the action
1984863dea3SSunil Goutham  * @vf: VF to which ACK to be sent
1994863dea3SSunil Goutham  */
nic_mbx_send_nack(struct nicpf * nic,int vf)2004863dea3SSunil Goutham static void nic_mbx_send_nack(struct nicpf *nic, int vf)
2014863dea3SSunil Goutham {
2024863dea3SSunil Goutham 	union nic_mbx mbx = {};
2034863dea3SSunil Goutham 
2044863dea3SSunil Goutham 	mbx.msg.msg = NIC_MBOX_MSG_NACK;
2054863dea3SSunil Goutham 	nic_send_msg_to_vf(nic, vf, &mbx);
2064863dea3SSunil Goutham }
2074863dea3SSunil Goutham 
2084863dea3SSunil Goutham /* Flush all in flight receive packets to memory and
2094863dea3SSunil Goutham  * bring down an active RQ
2104863dea3SSunil Goutham  */
nic_rcv_queue_sw_sync(struct nicpf * nic)2114863dea3SSunil Goutham static int nic_rcv_queue_sw_sync(struct nicpf *nic)
2124863dea3SSunil Goutham {
2134863dea3SSunil Goutham 	u16 timeout = ~0x00;
2144863dea3SSunil Goutham 
2154863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x01);
2164863dea3SSunil Goutham 	/* Wait till sync cycle is finished */
2174863dea3SSunil Goutham 	while (timeout) {
2184863dea3SSunil Goutham 		if (nic_reg_read(nic, NIC_PF_SW_SYNC_RX_DONE) & 0x1)
2194863dea3SSunil Goutham 			break;
2204863dea3SSunil Goutham 		timeout--;
2214863dea3SSunil Goutham 	}
2224863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x00);
2234863dea3SSunil Goutham 	if (!timeout) {
2244863dea3SSunil Goutham 		dev_err(&nic->pdev->dev, "Receive queue software sync failed");
2254863dea3SSunil Goutham 		return 1;
2264863dea3SSunil Goutham 	}
2274863dea3SSunil Goutham 	return 0;
2284863dea3SSunil Goutham }
2294863dea3SSunil Goutham 
2304863dea3SSunil Goutham /* Get BGX Rx/Tx stats and respond to VF's request */
nic_get_bgx_stats(struct nicpf * nic,struct bgx_stats_msg * bgx)2314863dea3SSunil Goutham static void nic_get_bgx_stats(struct nicpf *nic, struct bgx_stats_msg *bgx)
2324863dea3SSunil Goutham {
2334863dea3SSunil Goutham 	int bgx_idx, lmac;
2344863dea3SSunil Goutham 	union nic_mbx mbx = {};
2354863dea3SSunil Goutham 
2364863dea3SSunil Goutham 	bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
2374863dea3SSunil Goutham 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
2384863dea3SSunil Goutham 
2394863dea3SSunil Goutham 	mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
2404863dea3SSunil Goutham 	mbx.bgx_stats.vf_id = bgx->vf_id;
2414863dea3SSunil Goutham 	mbx.bgx_stats.rx = bgx->rx;
2424863dea3SSunil Goutham 	mbx.bgx_stats.idx = bgx->idx;
2434863dea3SSunil Goutham 	if (bgx->rx)
2444863dea3SSunil Goutham 		mbx.bgx_stats.stats = bgx_get_rx_stats(nic->node, bgx_idx,
2454863dea3SSunil Goutham 							    lmac, bgx->idx);
2464863dea3SSunil Goutham 	else
2474863dea3SSunil Goutham 		mbx.bgx_stats.stats = bgx_get_tx_stats(nic->node, bgx_idx,
2484863dea3SSunil Goutham 							    lmac, bgx->idx);
2494863dea3SSunil Goutham 	nic_send_msg_to_vf(nic, bgx->vf_id, &mbx);
2504863dea3SSunil Goutham }
2514863dea3SSunil Goutham 
2524863dea3SSunil Goutham /* Update hardware min/max frame size */
nic_update_hw_frs(struct nicpf * nic,int new_frs,int vf)2534863dea3SSunil Goutham static int nic_update_hw_frs(struct nicpf *nic, int new_frs, int vf)
2544863dea3SSunil Goutham {
255712c3185SSunil Goutham 	int bgx, lmac, lmac_cnt;
256712c3185SSunil Goutham 	u64 lmac_credits;
2574863dea3SSunil Goutham 
258712c3185SSunil Goutham 	if ((new_frs > NIC_HW_MAX_FRS) || (new_frs < NIC_HW_MIN_FRS))
259712c3185SSunil Goutham 		return 1;
260712c3185SSunil Goutham 
261712c3185SSunil Goutham 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
262712c3185SSunil Goutham 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
263712c3185SSunil Goutham 	lmac += bgx * MAX_LMAC_PER_BGX;
264712c3185SSunil Goutham 
265712c3185SSunil Goutham 	new_frs += VLAN_ETH_HLEN + ETH_FCS_LEN + 4;
266712c3185SSunil Goutham 
267712c3185SSunil Goutham 	/* Update corresponding LMAC credits */
268712c3185SSunil Goutham 	lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
269712c3185SSunil Goutham 	lmac_credits = nic_reg_read(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8));
270712c3185SSunil Goutham 	lmac_credits &= ~(0xFFFFFULL << 12);
271712c3185SSunil Goutham 	lmac_credits |= (((((48 * 1024) / lmac_cnt) - new_frs) / 16) << 12);
272712c3185SSunil Goutham 	nic_reg_write(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8), lmac_credits);
273712c3185SSunil Goutham 
274712c3185SSunil Goutham 	/* Enforce MTU in HW
275712c3185SSunil Goutham 	 * This config is supported only from 88xx pass 2.0 onwards.
276712c3185SSunil Goutham 	 */
277712c3185SSunil Goutham 	if (!pass1_silicon(nic->pdev))
278712c3185SSunil Goutham 		nic_reg_write(nic,
279712c3185SSunil Goutham 			      NIC_PF_LMAC_0_7_CFG2 + (lmac * 8), new_frs);
2804863dea3SSunil Goutham 	return 0;
2814863dea3SSunil Goutham }
2824863dea3SSunil Goutham 
2834863dea3SSunil Goutham /* Set minimum transmit packet size */
nic_set_tx_pkt_pad(struct nicpf * nic,int size)2844863dea3SSunil Goutham static void nic_set_tx_pkt_pad(struct nicpf *nic, int size)
2854863dea3SSunil Goutham {
286949b5331SSunil Goutham 	int lmac, max_lmac;
287949b5331SSunil Goutham 	u16 sdevid;
2884863dea3SSunil Goutham 	u64 lmac_cfg;
2894863dea3SSunil Goutham 
29057e81d44SSunil Goutham 	/* There is a issue in HW where-in while sending GSO sized
29157e81d44SSunil Goutham 	 * pkts as part of TSO, if pkt len falls below this size
29257e81d44SSunil Goutham 	 * NIC will zero PAD packet and also updates IP total length.
29357e81d44SSunil Goutham 	 * Hence set this value to lessthan min pkt size of MAC+IP+TCP
29457e81d44SSunil Goutham 	 * headers, BGX will do the padding to transmit 64 byte pkt.
29557e81d44SSunil Goutham 	 */
29657e81d44SSunil Goutham 	if (size > 52)
29757e81d44SSunil Goutham 		size = 52;
2984863dea3SSunil Goutham 
299949b5331SSunil Goutham 	pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
300949b5331SSunil Goutham 	/* 81xx's RGX has only one LMAC */
301949b5331SSunil Goutham 	if (sdevid == PCI_SUBSYS_DEVID_81XX_NIC_PF)
302949b5331SSunil Goutham 		max_lmac = ((nic->hw->bgx_cnt - 1) * MAX_LMAC_PER_BGX) + 1;
303949b5331SSunil Goutham 	else
304949b5331SSunil Goutham 		max_lmac = nic->hw->bgx_cnt * MAX_LMAC_PER_BGX;
305949b5331SSunil Goutham 
306949b5331SSunil Goutham 	for (lmac = 0; lmac < max_lmac; lmac++) {
3074863dea3SSunil Goutham 		lmac_cfg = nic_reg_read(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3));
3084863dea3SSunil Goutham 		lmac_cfg &= ~(0xF << 2);
3094863dea3SSunil Goutham 		lmac_cfg |= ((size / 4) << 2);
3104863dea3SSunil Goutham 		nic_reg_write(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3), lmac_cfg);
3114863dea3SSunil Goutham 	}
3124863dea3SSunil Goutham }
3134863dea3SSunil Goutham 
3144863dea3SSunil Goutham /* Function to check number of LMACs present and set VF::LMAC mapping.
3154863dea3SSunil Goutham  * Mapping will be used while initializing channels.
3164863dea3SSunil Goutham  */
nic_set_lmac_vf_mapping(struct nicpf * nic)3174863dea3SSunil Goutham static void nic_set_lmac_vf_mapping(struct nicpf *nic)
3184863dea3SSunil Goutham {
3194863dea3SSunil Goutham 	unsigned bgx_map = bgx_get_map(nic->node);
3204863dea3SSunil Goutham 	int bgx, next_bgx_lmac = 0;
3214863dea3SSunil Goutham 	int lmac, lmac_cnt = 0;
3224863dea3SSunil Goutham 	u64 lmac_credit;
3234863dea3SSunil Goutham 
3244863dea3SSunil Goutham 	nic->num_vf_en = 0;
3254863dea3SSunil Goutham 
326a5c3d498SSunil Goutham 	for (bgx = 0; bgx < nic->hw->bgx_cnt; bgx++) {
3274863dea3SSunil Goutham 		if (!(bgx_map & (1 << bgx)))
3284863dea3SSunil Goutham 			continue;
3294863dea3SSunil Goutham 		lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
3304863dea3SSunil Goutham 		for (lmac = 0; lmac < lmac_cnt; lmac++)
3314863dea3SSunil Goutham 			nic->vf_lmac_map[next_bgx_lmac++] =
3324863dea3SSunil Goutham 						NIC_SET_VF_LMAC_MAP(bgx, lmac);
3334863dea3SSunil Goutham 		nic->num_vf_en += lmac_cnt;
3344863dea3SSunil Goutham 
3354863dea3SSunil Goutham 		/* Program LMAC credits */
3364863dea3SSunil Goutham 		lmac_credit = (1ull << 1); /* channel credit enable */
3374863dea3SSunil Goutham 		lmac_credit |= (0x1ff << 2); /* Max outstanding pkt count */
3384863dea3SSunil Goutham 		/* 48KB BGX Tx buffer size, each unit is of size 16bytes */
3394863dea3SSunil Goutham 		lmac_credit |= (((((48 * 1024) / lmac_cnt) -
3404863dea3SSunil Goutham 				NIC_HW_MAX_FRS) / 16) << 12);
3414863dea3SSunil Goutham 		lmac = bgx * MAX_LMAC_PER_BGX;
3424863dea3SSunil Goutham 		for (; lmac < lmac_cnt + (bgx * MAX_LMAC_PER_BGX); lmac++)
3434863dea3SSunil Goutham 			nic_reg_write(nic,
3444863dea3SSunil Goutham 				      NIC_PF_LMAC_0_7_CREDIT + (lmac * 8),
3454863dea3SSunil Goutham 				      lmac_credit);
3466465859aSSunil Goutham 
3476465859aSSunil Goutham 		/* On CN81XX there are only 8 VFs but max possible no of
3486465859aSSunil Goutham 		 * interfaces are 9.
3496465859aSSunil Goutham 		 */
3506465859aSSunil Goutham 		if (nic->num_vf_en >= pci_sriov_get_totalvfs(nic->pdev)) {
3516465859aSSunil Goutham 			nic->num_vf_en = pci_sriov_get_totalvfs(nic->pdev);
3526465859aSSunil Goutham 			break;
3536465859aSSunil Goutham 		}
3544863dea3SSunil Goutham 	}
3554863dea3SSunil Goutham }
3564863dea3SSunil Goutham 
nic_get_hw_info(struct nicpf * nic)3573d67a507SAleksey Makarov static void nic_get_hw_info(struct nicpf *nic)
358a5c3d498SSunil Goutham {
359a5c3d498SSunil Goutham 	u16 sdevid;
360a5c3d498SSunil Goutham 	struct hw_info *hw = nic->hw;
361a5c3d498SSunil Goutham 
362a5c3d498SSunil Goutham 	pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
363a5c3d498SSunil Goutham 
364a5c3d498SSunil Goutham 	switch (sdevid) {
365a5c3d498SSunil Goutham 	case PCI_SUBSYS_DEVID_88XX_NIC_PF:
366a5c3d498SSunil Goutham 		hw->bgx_cnt = MAX_BGX_PER_CN88XX;
367a5c3d498SSunil Goutham 		hw->chans_per_lmac = 16;
368a5c3d498SSunil Goutham 		hw->chans_per_bgx = 128;
369a5c3d498SSunil Goutham 		hw->cpi_cnt = 2048;
370a5c3d498SSunil Goutham 		hw->rssi_cnt = 4096;
371a5c3d498SSunil Goutham 		hw->rss_ind_tbl_size = NIC_MAX_RSS_IDR_TBL_SIZE;
372a5c3d498SSunil Goutham 		hw->tl3_cnt = 256;
373a5c3d498SSunil Goutham 		hw->tl2_cnt = 64;
374a5c3d498SSunil Goutham 		hw->tl1_cnt = 2;
375a5c3d498SSunil Goutham 		hw->tl1_per_bgx = true;
376a5c3d498SSunil Goutham 		break;
3770025d93eSSunil Goutham 	case PCI_SUBSYS_DEVID_81XX_NIC_PF:
3780025d93eSSunil Goutham 		hw->bgx_cnt = MAX_BGX_PER_CN81XX;
3790025d93eSSunil Goutham 		hw->chans_per_lmac = 8;
3800025d93eSSunil Goutham 		hw->chans_per_bgx = 32;
3810025d93eSSunil Goutham 		hw->chans_per_rgx = 8;
3820025d93eSSunil Goutham 		hw->chans_per_lbk = 24;
3830025d93eSSunil Goutham 		hw->cpi_cnt = 512;
3840025d93eSSunil Goutham 		hw->rssi_cnt = 256;
3850025d93eSSunil Goutham 		hw->rss_ind_tbl_size = 32; /* Max RSSI / Max interfaces */
3860025d93eSSunil Goutham 		hw->tl3_cnt = 64;
3870025d93eSSunil Goutham 		hw->tl2_cnt = 16;
3880025d93eSSunil Goutham 		hw->tl1_cnt = 10;
3890025d93eSSunil Goutham 		hw->tl1_per_bgx = false;
3900025d93eSSunil Goutham 		break;
3910025d93eSSunil Goutham 	case PCI_SUBSYS_DEVID_83XX_NIC_PF:
3920025d93eSSunil Goutham 		hw->bgx_cnt = MAX_BGX_PER_CN83XX;
3930025d93eSSunil Goutham 		hw->chans_per_lmac = 8;
3940025d93eSSunil Goutham 		hw->chans_per_bgx = 32;
3950025d93eSSunil Goutham 		hw->chans_per_lbk = 64;
3960025d93eSSunil Goutham 		hw->cpi_cnt = 2048;
3970025d93eSSunil Goutham 		hw->rssi_cnt = 1024;
3980025d93eSSunil Goutham 		hw->rss_ind_tbl_size = 64; /* Max RSSI / Max interfaces */
3990025d93eSSunil Goutham 		hw->tl3_cnt = 256;
4000025d93eSSunil Goutham 		hw->tl2_cnt = 64;
4010025d93eSSunil Goutham 		hw->tl1_cnt = 18;
4020025d93eSSunil Goutham 		hw->tl1_per_bgx = false;
4030025d93eSSunil Goutham 		break;
404a5c3d498SSunil Goutham 	}
405a5c3d498SSunil Goutham 	hw->tl4_cnt = MAX_QUEUES_PER_QSET * pci_sriov_get_totalvfs(nic->pdev);
406a5c3d498SSunil Goutham }
407a5c3d498SSunil Goutham 
4084863dea3SSunil Goutham #define BGX0_BLOCK 8
4094863dea3SSunil Goutham #define BGX1_BLOCK 9
4104863dea3SSunil Goutham 
nic_init_hw(struct nicpf * nic)4113d67a507SAleksey Makarov static void nic_init_hw(struct nicpf *nic)
4124863dea3SSunil Goutham {
4133d67a507SAleksey Makarov 	int i;
4144c0b6eafSSunil Goutham 	u64 cqm_cfg;
4154863dea3SSunil Goutham 
4164863dea3SSunil Goutham 	/* Enable NIC HW block */
4174863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_CFG, 0x3);
4184863dea3SSunil Goutham 
4194863dea3SSunil Goutham 	/* Enable backpressure */
4204863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_BP_CFG, (1ULL << 6) | 0x03);
4214863dea3SSunil Goutham 
4224a875509SSunil Goutham 	/* TNS and TNS bypass modes are present only on 88xx
4234a875509SSunil Goutham 	 * Also offset of this CSR has changed in 81xx and 83xx.
4244a875509SSunil Goutham 	 */
4250025d93eSSunil Goutham 	if (nic->pdev->subsystem_device == PCI_SUBSYS_DEVID_88XX_NIC_PF) {
4264863dea3SSunil Goutham 		/* Disable TNS mode on both interfaces */
4274863dea3SSunil Goutham 		nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG,
4284a875509SSunil Goutham 			      (NIC_TNS_BYPASS_MODE << 7) |
4294a875509SSunil Goutham 			      BGX0_BLOCK | (1ULL << 16));
4304863dea3SSunil Goutham 		nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG | (1 << 8),
4314a875509SSunil Goutham 			      (NIC_TNS_BYPASS_MODE << 7) |
4324a875509SSunil Goutham 			      BGX1_BLOCK | (1ULL << 16));
4334a875509SSunil Goutham 	} else {
4344a875509SSunil Goutham 		/* Configure timestamp generation timeout to 10us */
4354a875509SSunil Goutham 		for (i = 0; i < nic->hw->bgx_cnt; i++)
4364a875509SSunil Goutham 			nic_reg_write(nic, NIC_PF_INTFX_SEND_CFG | (i << 3),
4374a875509SSunil Goutham 				      (1ULL << 16));
4380025d93eSSunil Goutham 	}
4390025d93eSSunil Goutham 
4404863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG,
4414863dea3SSunil Goutham 		      (1ULL << 63) | BGX0_BLOCK);
4424863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG + (1 << 8),
4434863dea3SSunil Goutham 		      (1ULL << 63) | BGX1_BLOCK);
4444863dea3SSunil Goutham 
4454863dea3SSunil Goutham 	/* PKIND configuration */
4464863dea3SSunil Goutham 	nic->pkind.minlen = 0;
447712c3185SSunil Goutham 	nic->pkind.maxlen = NIC_HW_MAX_FRS + VLAN_ETH_HLEN + ETH_FCS_LEN + 4;
4484863dea3SSunil Goutham 	nic->pkind.lenerr_en = 1;
4494863dea3SSunil Goutham 	nic->pkind.rx_hdr = 0;
4504863dea3SSunil Goutham 	nic->pkind.hdr_sl = 0;
4514863dea3SSunil Goutham 
4524863dea3SSunil Goutham 	for (i = 0; i < NIC_MAX_PKIND; i++)
4534863dea3SSunil Goutham 		nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (i << 3),
4544863dea3SSunil Goutham 			      *(u64 *)&nic->pkind);
4554863dea3SSunil Goutham 
4564863dea3SSunil Goutham 	nic_set_tx_pkt_pad(nic, NIC_HW_MIN_FRS);
4574863dea3SSunil Goutham 
4584863dea3SSunil Goutham 	/* Timer config */
4594863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_INTR_TIMER_CFG, NICPF_CLK_PER_INT_TICK);
460aa2e259bSSunil Goutham 
461aa2e259bSSunil Goutham 	/* Enable VLAN ethertype matching and stripping */
462aa2e259bSSunil Goutham 	nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7,
463aa2e259bSSunil Goutham 		      (2 << 19) | (ETYPE_ALG_VLAN_STRIP << 16) | ETH_P_8021Q);
4644c0b6eafSSunil Goutham 
4654c0b6eafSSunil Goutham 	/* Check if HW expected value is higher (could be in future chips) */
4664c0b6eafSSunil Goutham 	cqm_cfg = nic_reg_read(nic, NIC_PF_CQM_CFG);
4674c0b6eafSSunil Goutham 	if (cqm_cfg < NICPF_CQM_MIN_DROP_LEVEL)
4684c0b6eafSSunil Goutham 		nic_reg_write(nic, NIC_PF_CQM_CFG, NICPF_CQM_MIN_DROP_LEVEL);
4694863dea3SSunil Goutham }
4704863dea3SSunil Goutham 
4714863dea3SSunil Goutham /* Channel parse index configuration */
nic_config_cpi(struct nicpf * nic,struct cpi_cfg_msg * cfg)4724863dea3SSunil Goutham static void nic_config_cpi(struct nicpf *nic, struct cpi_cfg_msg *cfg)
4734863dea3SSunil Goutham {
474a5c3d498SSunil Goutham 	struct hw_info *hw = nic->hw;
4754863dea3SSunil Goutham 	u32 vnic, bgx, lmac, chan;
4764863dea3SSunil Goutham 	u32 padd, cpi_count = 0;
4774863dea3SSunil Goutham 	u64 cpi_base, cpi, rssi_base, rssi;
4784863dea3SSunil Goutham 	u8  qset, rq_idx = 0;
4794863dea3SSunil Goutham 
4804863dea3SSunil Goutham 	vnic = cfg->vf_id;
4814863dea3SSunil Goutham 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
4824863dea3SSunil Goutham 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
4834863dea3SSunil Goutham 
484a5c3d498SSunil Goutham 	chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
4856465859aSSunil Goutham 	cpi_base = vnic * NIC_MAX_CPI_PER_LMAC;
4866465859aSSunil Goutham 	rssi_base = vnic * hw->rss_ind_tbl_size;
4874863dea3SSunil Goutham 
4884863dea3SSunil Goutham 	/* Rx channel configuration */
4894863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_BP_CFG | (chan << 3),
4904863dea3SSunil Goutham 		      (1ull << 63) | (vnic << 0));
4914863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_CFG | (chan << 3),
4924863dea3SSunil Goutham 		      ((u64)cfg->cpi_alg << 62) | (cpi_base << 48));
4934863dea3SSunil Goutham 
4944863dea3SSunil Goutham 	if (cfg->cpi_alg == CPI_ALG_NONE)
4954863dea3SSunil Goutham 		cpi_count = 1;
4964863dea3SSunil Goutham 	else if (cfg->cpi_alg == CPI_ALG_VLAN) /* 3 bits of PCP */
4974863dea3SSunil Goutham 		cpi_count = 8;
4984863dea3SSunil Goutham 	else if (cfg->cpi_alg == CPI_ALG_VLAN16) /* 3 bits PCP + DEI */
4994863dea3SSunil Goutham 		cpi_count = 16;
5004863dea3SSunil Goutham 	else if (cfg->cpi_alg == CPI_ALG_DIFF) /* 6bits DSCP */
5014863dea3SSunil Goutham 		cpi_count = NIC_MAX_CPI_PER_LMAC;
5024863dea3SSunil Goutham 
5034863dea3SSunil Goutham 	/* RSS Qset, Qidx mapping */
5044863dea3SSunil Goutham 	qset = cfg->vf_id;
5054863dea3SSunil Goutham 	rssi = rssi_base;
5064863dea3SSunil Goutham 	for (; rssi < (rssi_base + cfg->rq_cnt); rssi++) {
5074863dea3SSunil Goutham 		nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
5084863dea3SSunil Goutham 			      (qset << 3) | rq_idx);
5094863dea3SSunil Goutham 		rq_idx++;
5104863dea3SSunil Goutham 	}
5114863dea3SSunil Goutham 
5124863dea3SSunil Goutham 	rssi = 0;
5134863dea3SSunil Goutham 	cpi = cpi_base;
5144863dea3SSunil Goutham 	for (; cpi < (cpi_base + cpi_count); cpi++) {
5154863dea3SSunil Goutham 		/* Determine port to channel adder */
5164863dea3SSunil Goutham 		if (cfg->cpi_alg != CPI_ALG_DIFF)
5174863dea3SSunil Goutham 			padd = cpi % cpi_count;
5184863dea3SSunil Goutham 		else
5194863dea3SSunil Goutham 			padd = cpi % 8; /* 3 bits CS out of 6bits DSCP */
5204863dea3SSunil Goutham 
5214863dea3SSunil Goutham 		/* Leave RSS_SIZE as '0' to disable RSS */
52240fb5f8aSSunil Goutham 		if (pass1_silicon(nic->pdev)) {
5234863dea3SSunil Goutham 			nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
52434411b68SThanneeru Srinivasulu 				      (vnic << 24) | (padd << 16) |
52534411b68SThanneeru Srinivasulu 				      (rssi_base + rssi));
52634411b68SThanneeru Srinivasulu 		} else {
52734411b68SThanneeru Srinivasulu 			/* Set MPI_ALG to '0' to disable MCAM parsing */
52834411b68SThanneeru Srinivasulu 			nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
52934411b68SThanneeru Srinivasulu 				      (padd << 16));
53034411b68SThanneeru Srinivasulu 			/* MPI index is same as CPI if MPI_ALG is not enabled */
53134411b68SThanneeru Srinivasulu 			nic_reg_write(nic, NIC_PF_MPI_0_2047_CFG | (cpi << 3),
53234411b68SThanneeru Srinivasulu 				      (vnic << 24) | (rssi_base + rssi));
53334411b68SThanneeru Srinivasulu 		}
5344863dea3SSunil Goutham 
5354863dea3SSunil Goutham 		if ((rssi + 1) >= cfg->rq_cnt)
5364863dea3SSunil Goutham 			continue;
5374863dea3SSunil Goutham 
5384863dea3SSunil Goutham 		if (cfg->cpi_alg == CPI_ALG_VLAN)
5394863dea3SSunil Goutham 			rssi++;
5404863dea3SSunil Goutham 		else if (cfg->cpi_alg == CPI_ALG_VLAN16)
5414863dea3SSunil Goutham 			rssi = ((cpi - cpi_base) & 0xe) >> 1;
5424863dea3SSunil Goutham 		else if (cfg->cpi_alg == CPI_ALG_DIFF)
5434863dea3SSunil Goutham 			rssi = ((cpi - cpi_base) & 0x38) >> 3;
5444863dea3SSunil Goutham 	}
5454863dea3SSunil Goutham 	nic->cpi_base[cfg->vf_id] = cpi_base;
54634411b68SThanneeru Srinivasulu 	nic->rssi_base[cfg->vf_id] = rssi_base;
5474863dea3SSunil Goutham }
5484863dea3SSunil Goutham 
5494863dea3SSunil Goutham /* Responsds to VF with its RSS indirection table size */
nic_send_rss_size(struct nicpf * nic,int vf)5504863dea3SSunil Goutham static void nic_send_rss_size(struct nicpf *nic, int vf)
5514863dea3SSunil Goutham {
5524863dea3SSunil Goutham 	union nic_mbx mbx = {};
5534863dea3SSunil Goutham 
5544863dea3SSunil Goutham 	mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
555a5c3d498SSunil Goutham 	mbx.rss_size.ind_tbl_size = nic->hw->rss_ind_tbl_size;
5564863dea3SSunil Goutham 	nic_send_msg_to_vf(nic, vf, &mbx);
5574863dea3SSunil Goutham }
5584863dea3SSunil Goutham 
5594863dea3SSunil Goutham /* Receive side scaling configuration
5604863dea3SSunil Goutham  * configure:
5614863dea3SSunil Goutham  * - RSS index
5624863dea3SSunil Goutham  * - indir table i.e hash::RQ mapping
5634863dea3SSunil Goutham  * - no of hash bits to consider
5644863dea3SSunil Goutham  */
nic_config_rss(struct nicpf * nic,struct rss_cfg_msg * cfg)5654863dea3SSunil Goutham static void nic_config_rss(struct nicpf *nic, struct rss_cfg_msg *cfg)
5664863dea3SSunil Goutham {
5674863dea3SSunil Goutham 	u8  qset, idx = 0;
5684863dea3SSunil Goutham 	u64 cpi_cfg, cpi_base, rssi_base, rssi;
56934411b68SThanneeru Srinivasulu 	u64 idx_addr;
5704863dea3SSunil Goutham 
57134411b68SThanneeru Srinivasulu 	rssi_base = nic->rssi_base[cfg->vf_id] + cfg->tbl_offset;
5724863dea3SSunil Goutham 
5734863dea3SSunil Goutham 	rssi = rssi_base;
5744863dea3SSunil Goutham 
5754863dea3SSunil Goutham 	for (; rssi < (rssi_base + cfg->tbl_len); rssi++) {
57692dc8769SSunil Goutham 		u8 svf = cfg->ind_tbl[idx] >> 3;
57792dc8769SSunil Goutham 
57892dc8769SSunil Goutham 		if (svf)
57992dc8769SSunil Goutham 			qset = nic->vf_sqs[cfg->vf_id][svf - 1];
58092dc8769SSunil Goutham 		else
58192dc8769SSunil Goutham 			qset = cfg->vf_id;
5824863dea3SSunil Goutham 		nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
5834863dea3SSunil Goutham 			      (qset << 3) | (cfg->ind_tbl[idx] & 0x7));
5844863dea3SSunil Goutham 		idx++;
5854863dea3SSunil Goutham 	}
5864863dea3SSunil Goutham 
58734411b68SThanneeru Srinivasulu 	cpi_base = nic->cpi_base[cfg->vf_id];
58840fb5f8aSSunil Goutham 	if (pass1_silicon(nic->pdev))
58934411b68SThanneeru Srinivasulu 		idx_addr = NIC_PF_CPI_0_2047_CFG;
59034411b68SThanneeru Srinivasulu 	else
59134411b68SThanneeru Srinivasulu 		idx_addr = NIC_PF_MPI_0_2047_CFG;
59234411b68SThanneeru Srinivasulu 	cpi_cfg = nic_reg_read(nic, idx_addr | (cpi_base << 3));
5934863dea3SSunil Goutham 	cpi_cfg &= ~(0xFULL << 20);
5944863dea3SSunil Goutham 	cpi_cfg |= (cfg->hash_bits << 20);
59534411b68SThanneeru Srinivasulu 	nic_reg_write(nic, idx_addr | (cpi_base << 3), cpi_cfg);
5964863dea3SSunil Goutham }
5974863dea3SSunil Goutham 
5984863dea3SSunil Goutham /* 4 level transmit side scheduler configutation
5994863dea3SSunil Goutham  * for TNS bypass mode
6004863dea3SSunil Goutham  *
6010025d93eSSunil Goutham  * Sample configuration for SQ0 on 88xx
6024863dea3SSunil Goutham  * VNIC0-SQ0 -> TL4(0)   -> TL3[0]   -> TL2[0]  -> TL1[0] -> BGX0
6034863dea3SSunil Goutham  * VNIC1-SQ0 -> TL4(8)   -> TL3[2]   -> TL2[0]  -> TL1[0] -> BGX0
6044863dea3SSunil Goutham  * VNIC2-SQ0 -> TL4(16)  -> TL3[4]   -> TL2[1]  -> TL1[0] -> BGX0
6054863dea3SSunil Goutham  * VNIC3-SQ0 -> TL4(24)  -> TL3[6]   -> TL2[1]  -> TL1[0] -> BGX0
6064863dea3SSunil Goutham  * VNIC4-SQ0 -> TL4(512) -> TL3[128] -> TL2[32] -> TL1[1] -> BGX1
6074863dea3SSunil Goutham  * VNIC5-SQ0 -> TL4(520) -> TL3[130] -> TL2[32] -> TL1[1] -> BGX1
6084863dea3SSunil Goutham  * VNIC6-SQ0 -> TL4(528) -> TL3[132] -> TL2[33] -> TL1[1] -> BGX1
6094863dea3SSunil Goutham  * VNIC7-SQ0 -> TL4(536) -> TL3[134] -> TL2[33] -> TL1[1] -> BGX1
6104863dea3SSunil Goutham  */
nic_tx_channel_cfg(struct nicpf * nic,u8 vnic,struct sq_cfg_msg * sq)61192dc8769SSunil Goutham static void nic_tx_channel_cfg(struct nicpf *nic, u8 vnic,
61292dc8769SSunil Goutham 			       struct sq_cfg_msg *sq)
6134863dea3SSunil Goutham {
614a5c3d498SSunil Goutham 	struct hw_info *hw = nic->hw;
6154863dea3SSunil Goutham 	u32 bgx, lmac, chan;
6164863dea3SSunil Goutham 	u32 tl2, tl3, tl4;
6174863dea3SSunil Goutham 	u32 rr_quantum;
61892dc8769SSunil Goutham 	u8 sq_idx = sq->sq_num;
61992dc8769SSunil Goutham 	u8 pqs_vnic;
6203e29adbaSSunil Goutham 	int svf;
6214863dea3SSunil Goutham 
62292dc8769SSunil Goutham 	if (sq->sqs_mode)
62392dc8769SSunil Goutham 		pqs_vnic = nic->pqs_vf[vnic];
62492dc8769SSunil Goutham 	else
62592dc8769SSunil Goutham 		pqs_vnic = vnic;
62692dc8769SSunil Goutham 
62792dc8769SSunil Goutham 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
62892dc8769SSunil Goutham 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
62992dc8769SSunil Goutham 
6304863dea3SSunil Goutham 	/* 24 bytes for FCS, IPG and preamble */
6314863dea3SSunil Goutham 	rr_quantum = ((NIC_HW_MAX_FRS + 24) / 4);
6324863dea3SSunil Goutham 
633a5c3d498SSunil Goutham 	/* For 88xx 0-511 TL4 transmits via BGX0 and
634a5c3d498SSunil Goutham 	 * 512-1023 TL4s transmit via BGX1.
635a5c3d498SSunil Goutham 	 */
6360025d93eSSunil Goutham 	if (hw->tl1_per_bgx) {
637a5c3d498SSunil Goutham 		tl4 = bgx * (hw->tl4_cnt / hw->bgx_cnt);
6383e29adbaSSunil Goutham 		if (!sq->sqs_mode) {
639a5c3d498SSunil Goutham 			tl4 += (lmac * MAX_QUEUES_PER_QSET);
6403e29adbaSSunil Goutham 		} else {
6413e29adbaSSunil Goutham 			for (svf = 0; svf < MAX_SQS_PER_VF; svf++) {
6423e29adbaSSunil Goutham 				if (nic->vf_sqs[pqs_vnic][svf] == vnic)
6433e29adbaSSunil Goutham 					break;
6443e29adbaSSunil Goutham 			}
645a5c3d498SSunil Goutham 			tl4 += (MAX_LMAC_PER_BGX * MAX_QUEUES_PER_QSET);
646a5c3d498SSunil Goutham 			tl4 += (lmac * MAX_QUEUES_PER_QSET * MAX_SQS_PER_VF);
647a5c3d498SSunil Goutham 			tl4 += (svf * MAX_QUEUES_PER_QSET);
6483e29adbaSSunil Goutham 		}
6490025d93eSSunil Goutham 	} else {
6500025d93eSSunil Goutham 		tl4 = (vnic * MAX_QUEUES_PER_QSET);
6510025d93eSSunil Goutham 	}
6524863dea3SSunil Goutham 	tl4 += sq_idx;
65392dc8769SSunil Goutham 
654a5c3d498SSunil Goutham 	tl3 = tl4 / (hw->tl4_cnt / hw->tl3_cnt);
6554863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_QSET_0_127_SQ_0_7_CFG2 |
6564863dea3SSunil Goutham 		      ((u64)vnic << NIC_QS_ID_SHIFT) |
6574863dea3SSunil Goutham 		      ((u32)sq_idx << NIC_Q_NUM_SHIFT), tl4);
6584863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_TL4_0_1023_CFG | (tl4 << 3),
6594863dea3SSunil Goutham 		      ((u64)vnic << 27) | ((u32)sq_idx << 24) | rr_quantum);
6604863dea3SSunil Goutham 
6614863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_TL3_0_255_CFG | (tl3 << 3), rr_quantum);
662a5c3d498SSunil Goutham 
663a5c3d498SSunil Goutham 	/* On 88xx 0-127 channels are for BGX0 and
664a5c3d498SSunil Goutham 	 * 127-255 channels for BGX1.
6650025d93eSSunil Goutham 	 *
6660025d93eSSunil Goutham 	 * On 81xx/83xx TL3_CHAN reg should be configured with channel
6670025d93eSSunil Goutham 	 * within LMAC i.e 0-7 and not the actual channel number like on 88xx
668a5c3d498SSunil Goutham 	 */
669a5c3d498SSunil Goutham 	chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
6700025d93eSSunil Goutham 	if (hw->tl1_per_bgx)
6714863dea3SSunil Goutham 		nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), chan);
6720025d93eSSunil Goutham 	else
6730025d93eSSunil Goutham 		nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), 0);
674a5c3d498SSunil Goutham 
6754863dea3SSunil Goutham 	/* Enable backpressure on the channel */
6764863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_CHAN_0_255_TX_CFG | (chan << 3), 1);
6774863dea3SSunil Goutham 
6784863dea3SSunil Goutham 	tl2 = tl3 >> 2;
6794863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_TL3A_0_63_CFG | (tl2 << 3), tl2);
6804863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_TL2_0_63_CFG | (tl2 << 3), rr_quantum);
6814863dea3SSunil Goutham 	/* No priorities as of now */
6824863dea3SSunil Goutham 	nic_reg_write(nic, NIC_PF_TL2_0_63_PRI | (tl2 << 3), 0x00);
6830025d93eSSunil Goutham 
6840025d93eSSunil Goutham 	/* Unlike 88xx where TL2s 0-31 transmits to TL1 '0' and rest to TL1 '1'
6850025d93eSSunil Goutham 	 * on 81xx/83xx TL2 needs to be configured to transmit to one of the
6860025d93eSSunil Goutham 	 * possible LMACs.
6870025d93eSSunil Goutham 	 *
6880025d93eSSunil Goutham 	 * This register doesn't exist on 88xx.
6890025d93eSSunil Goutham 	 */
6900025d93eSSunil Goutham 	if (!hw->tl1_per_bgx)
6910025d93eSSunil Goutham 		nic_reg_write(nic, NIC_PF_TL2_LMAC | (tl2 << 3),
6920025d93eSSunil Goutham 			      lmac + (bgx * MAX_LMAC_PER_BGX));
6934863dea3SSunil Goutham }
6944863dea3SSunil Goutham 
69592dc8769SSunil Goutham /* Send primary nicvf pointer to secondary QS's VF */
nic_send_pnicvf(struct nicpf * nic,int sqs)69692dc8769SSunil Goutham static void nic_send_pnicvf(struct nicpf *nic, int sqs)
69792dc8769SSunil Goutham {
69892dc8769SSunil Goutham 	union nic_mbx mbx = {};
69992dc8769SSunil Goutham 
70092dc8769SSunil Goutham 	mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
70192dc8769SSunil Goutham 	mbx.nicvf.nicvf = nic->nicvf[nic->pqs_vf[sqs]];
70292dc8769SSunil Goutham 	nic_send_msg_to_vf(nic, sqs, &mbx);
70392dc8769SSunil Goutham }
70492dc8769SSunil Goutham 
70592dc8769SSunil Goutham /* Send SQS's nicvf pointer to primary QS's VF */
nic_send_snicvf(struct nicpf * nic,struct nicvf_ptr * nicvf)70692dc8769SSunil Goutham static void nic_send_snicvf(struct nicpf *nic, struct nicvf_ptr *nicvf)
70792dc8769SSunil Goutham {
70892dc8769SSunil Goutham 	union nic_mbx mbx = {};
70992dc8769SSunil Goutham 	int sqs_id = nic->vf_sqs[nicvf->vf_id][nicvf->sqs_id];
71092dc8769SSunil Goutham 
71192dc8769SSunil Goutham 	mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
71292dc8769SSunil Goutham 	mbx.nicvf.sqs_id = nicvf->sqs_id;
71392dc8769SSunil Goutham 	mbx.nicvf.nicvf = nic->nicvf[sqs_id];
71492dc8769SSunil Goutham 	nic_send_msg_to_vf(nic, nicvf->vf_id, &mbx);
71592dc8769SSunil Goutham }
71692dc8769SSunil Goutham 
71792dc8769SSunil Goutham /* Find next available Qset that can be assigned as a
71892dc8769SSunil Goutham  * secondary Qset to a VF.
71992dc8769SSunil Goutham  */
nic_nxt_avail_sqs(struct nicpf * nic)72092dc8769SSunil Goutham static int nic_nxt_avail_sqs(struct nicpf *nic)
72192dc8769SSunil Goutham {
72292dc8769SSunil Goutham 	int sqs;
72392dc8769SSunil Goutham 
72492dc8769SSunil Goutham 	for (sqs = 0; sqs < nic->num_sqs_en; sqs++) {
72592dc8769SSunil Goutham 		if (!nic->sqs_used[sqs])
72692dc8769SSunil Goutham 			nic->sqs_used[sqs] = true;
72792dc8769SSunil Goutham 		else
72892dc8769SSunil Goutham 			continue;
72992dc8769SSunil Goutham 		return sqs + nic->num_vf_en;
73092dc8769SSunil Goutham 	}
73192dc8769SSunil Goutham 	return -1;
73292dc8769SSunil Goutham }
73392dc8769SSunil Goutham 
73492dc8769SSunil Goutham /* Allocate additional Qsets for requested VF */
nic_alloc_sqs(struct nicpf * nic,struct sqs_alloc * sqs)73592dc8769SSunil Goutham static void nic_alloc_sqs(struct nicpf *nic, struct sqs_alloc *sqs)
73692dc8769SSunil Goutham {
73792dc8769SSunil Goutham 	union nic_mbx mbx = {};
73892dc8769SSunil Goutham 	int idx, alloc_qs = 0;
73992dc8769SSunil Goutham 	int sqs_id;
74092dc8769SSunil Goutham 
74192dc8769SSunil Goutham 	if (!nic->num_sqs_en)
74292dc8769SSunil Goutham 		goto send_mbox;
74392dc8769SSunil Goutham 
74492dc8769SSunil Goutham 	for (idx = 0; idx < sqs->qs_count; idx++) {
74592dc8769SSunil Goutham 		sqs_id = nic_nxt_avail_sqs(nic);
74692dc8769SSunil Goutham 		if (sqs_id < 0)
74792dc8769SSunil Goutham 			break;
74892dc8769SSunil Goutham 		nic->vf_sqs[sqs->vf_id][idx] = sqs_id;
74992dc8769SSunil Goutham 		nic->pqs_vf[sqs_id] = sqs->vf_id;
75092dc8769SSunil Goutham 		alloc_qs++;
75192dc8769SSunil Goutham 	}
75292dc8769SSunil Goutham 
75392dc8769SSunil Goutham send_mbox:
75492dc8769SSunil Goutham 	mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
75592dc8769SSunil Goutham 	mbx.sqs_alloc.vf_id = sqs->vf_id;
75692dc8769SSunil Goutham 	mbx.sqs_alloc.qs_count = alloc_qs;
75792dc8769SSunil Goutham 	nic_send_msg_to_vf(nic, sqs->vf_id, &mbx);
75892dc8769SSunil Goutham }
75992dc8769SSunil Goutham 
nic_config_loopback(struct nicpf * nic,struct set_loopback * lbk)760d77a2384SSunil Goutham static int nic_config_loopback(struct nicpf *nic, struct set_loopback *lbk)
761d77a2384SSunil Goutham {
762d77a2384SSunil Goutham 	int bgx_idx, lmac_idx;
763d77a2384SSunil Goutham 
764949b5331SSunil Goutham 	if (lbk->vf_id >= nic->num_vf_en)
765d77a2384SSunil Goutham 		return -1;
766d77a2384SSunil Goutham 
767d77a2384SSunil Goutham 	bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
768d77a2384SSunil Goutham 	lmac_idx = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
769d77a2384SSunil Goutham 
770d77a2384SSunil Goutham 	bgx_lmac_internal_loopback(nic->node, bgx_idx, lmac_idx, lbk->enable);
771d77a2384SSunil Goutham 
772d5b2d7a7SSunil Goutham 	/* Enable moving average calculation.
773d5b2d7a7SSunil Goutham 	 * Keep the LVL/AVG delay to HW enforced minimum so that, not too many
774d5b2d7a7SSunil Goutham 	 * packets sneek in between average calculations.
775d5b2d7a7SSunil Goutham 	 */
776d5b2d7a7SSunil Goutham 	nic_reg_write(nic, NIC_PF_CQ_AVG_CFG,
777d5b2d7a7SSunil Goutham 		      (BIT_ULL(20) | 0x2ull << 14 | 0x1));
778d5b2d7a7SSunil Goutham 	nic_reg_write(nic, NIC_PF_RRM_AVG_CFG,
779d5b2d7a7SSunil Goutham 		      (BIT_ULL(20) | 0x3ull << 14 | 0x1));
780d5b2d7a7SSunil Goutham 
781d77a2384SSunil Goutham 	return 0;
782d77a2384SSunil Goutham }
783d77a2384SSunil Goutham 
7843458c40dSJerin Jacob /* Reset statistics counters */
nic_reset_stat_counters(struct nicpf * nic,int vf,struct reset_stat_cfg * cfg)7853458c40dSJerin Jacob static int nic_reset_stat_counters(struct nicpf *nic,
7863458c40dSJerin Jacob 				   int vf, struct reset_stat_cfg *cfg)
7873458c40dSJerin Jacob {
7883458c40dSJerin Jacob 	int i, stat, qnum;
7893458c40dSJerin Jacob 	u64 reg_addr;
7903458c40dSJerin Jacob 
7913458c40dSJerin Jacob 	for (i = 0; i < RX_STATS_ENUM_LAST; i++) {
7923458c40dSJerin Jacob 		if (cfg->rx_stat_mask & BIT(i)) {
7933458c40dSJerin Jacob 			reg_addr = NIC_PF_VNIC_0_127_RX_STAT_0_13 |
7943458c40dSJerin Jacob 				   (vf << NIC_QS_ID_SHIFT) |
7953458c40dSJerin Jacob 				   (i << 3);
7963458c40dSJerin Jacob 			nic_reg_write(nic, reg_addr, 0);
7973458c40dSJerin Jacob 		}
7983458c40dSJerin Jacob 	}
7993458c40dSJerin Jacob 
8003458c40dSJerin Jacob 	for (i = 0; i < TX_STATS_ENUM_LAST; i++) {
8013458c40dSJerin Jacob 		if (cfg->tx_stat_mask & BIT(i)) {
8023458c40dSJerin Jacob 			reg_addr = NIC_PF_VNIC_0_127_TX_STAT_0_4 |
8033458c40dSJerin Jacob 				   (vf << NIC_QS_ID_SHIFT) |
8043458c40dSJerin Jacob 				   (i << 3);
8053458c40dSJerin Jacob 			nic_reg_write(nic, reg_addr, 0);
8063458c40dSJerin Jacob 		}
8073458c40dSJerin Jacob 	}
8083458c40dSJerin Jacob 
8093458c40dSJerin Jacob 	for (i = 0; i <= 15; i++) {
8103458c40dSJerin Jacob 		qnum = i >> 1;
8113458c40dSJerin Jacob 		stat = i & 1 ? 1 : 0;
8123458c40dSJerin Jacob 		reg_addr = (vf << NIC_QS_ID_SHIFT) |
8133458c40dSJerin Jacob 			   (qnum << NIC_Q_NUM_SHIFT) | (stat << 3);
8143458c40dSJerin Jacob 		if (cfg->rq_stat_mask & BIT(i)) {
8153458c40dSJerin Jacob 			reg_addr |= NIC_PF_QSET_0_127_RQ_0_7_STAT_0_1;
8163458c40dSJerin Jacob 			nic_reg_write(nic, reg_addr, 0);
8173458c40dSJerin Jacob 		}
8183458c40dSJerin Jacob 		if (cfg->sq_stat_mask & BIT(i)) {
8193458c40dSJerin Jacob 			reg_addr |= NIC_PF_QSET_0_127_SQ_0_7_STAT_0_1;
8203458c40dSJerin Jacob 			nic_reg_write(nic, reg_addr, 0);
8213458c40dSJerin Jacob 		}
8223458c40dSJerin Jacob 	}
823964cb69bSSunil Goutham 
8243458c40dSJerin Jacob 	return 0;
8253458c40dSJerin Jacob }
8263458c40dSJerin Jacob 
nic_enable_tunnel_parsing(struct nicpf * nic,int vf)827e22e86eaSZyta Szpak static void nic_enable_tunnel_parsing(struct nicpf *nic, int vf)
828e22e86eaSZyta Szpak {
829e22e86eaSZyta Szpak 	u64 prot_def = (IPV6_PROT << 32) | (IPV4_PROT << 16) | ET_PROT;
830e22e86eaSZyta Szpak 	u64 vxlan_prot_def = (IPV6_PROT_DEF << 32) |
831e22e86eaSZyta Szpak 			      (IPV4_PROT_DEF) << 16 | ET_PROT_DEF;
832e22e86eaSZyta Szpak 
833e22e86eaSZyta Szpak 	/* Configure tunnel parsing parameters */
834e22e86eaSZyta Szpak 	nic_reg_write(nic, NIC_PF_RX_GENEVE_DEF,
835e22e86eaSZyta Szpak 		      (1ULL << 63 | UDP_GENEVE_PORT_NUM));
836e22e86eaSZyta Szpak 	nic_reg_write(nic, NIC_PF_RX_GENEVE_PROT_DEF,
837e22e86eaSZyta Szpak 		      ((7ULL << 61) | prot_def));
838e22e86eaSZyta Szpak 	nic_reg_write(nic, NIC_PF_RX_NVGRE_PROT_DEF,
839e22e86eaSZyta Szpak 		      ((7ULL << 61) | prot_def));
840e22e86eaSZyta Szpak 	nic_reg_write(nic, NIC_PF_RX_VXLAN_DEF_0_1,
841e22e86eaSZyta Szpak 		      ((1ULL << 63) | UDP_VXLAN_PORT_NUM));
842e22e86eaSZyta Szpak 	nic_reg_write(nic, NIC_PF_RX_VXLAN_PROT_DEF,
843e22e86eaSZyta Szpak 		      ((0xfULL << 60) | vxlan_prot_def));
844e22e86eaSZyta Szpak }
845e22e86eaSZyta Szpak 
nic_enable_vf(struct nicpf * nic,int vf,bool enable)846f406ce42SPavel Fedin static void nic_enable_vf(struct nicpf *nic, int vf, bool enable)
847f406ce42SPavel Fedin {
848f406ce42SPavel Fedin 	int bgx, lmac;
849f406ce42SPavel Fedin 
850f406ce42SPavel Fedin 	nic->vf_enabled[vf] = enable;
851f406ce42SPavel Fedin 
852f406ce42SPavel Fedin 	if (vf >= nic->num_vf_en)
853f406ce42SPavel Fedin 		return;
854f406ce42SPavel Fedin 
855f406ce42SPavel Fedin 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
856f406ce42SPavel Fedin 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
857f406ce42SPavel Fedin 
858f406ce42SPavel Fedin 	bgx_lmac_rx_tx_enable(nic->node, bgx, lmac, enable);
859f406ce42SPavel Fedin }
860f406ce42SPavel Fedin 
nic_pause_frame(struct nicpf * nic,int vf,struct pfc * cfg)861430da208SSunil Goutham static void nic_pause_frame(struct nicpf *nic, int vf, struct pfc *cfg)
862430da208SSunil Goutham {
863430da208SSunil Goutham 	int bgx, lmac;
864430da208SSunil Goutham 	struct pfc pfc;
865430da208SSunil Goutham 	union nic_mbx mbx = {};
866430da208SSunil Goutham 
867430da208SSunil Goutham 	if (vf >= nic->num_vf_en)
868430da208SSunil Goutham 		return;
869430da208SSunil Goutham 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
870430da208SSunil Goutham 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
871430da208SSunil Goutham 
872430da208SSunil Goutham 	if (cfg->get) {
873430da208SSunil Goutham 		bgx_lmac_get_pfc(nic->node, bgx, lmac, &pfc);
874430da208SSunil Goutham 		mbx.pfc.msg = NIC_MBOX_MSG_PFC;
875430da208SSunil Goutham 		mbx.pfc.autoneg = pfc.autoneg;
876430da208SSunil Goutham 		mbx.pfc.fc_rx = pfc.fc_rx;
877430da208SSunil Goutham 		mbx.pfc.fc_tx = pfc.fc_tx;
878430da208SSunil Goutham 		nic_send_msg_to_vf(nic, vf, &mbx);
879430da208SSunil Goutham 	} else {
880430da208SSunil Goutham 		bgx_lmac_set_pfc(nic->node, bgx, lmac, cfg);
881430da208SSunil Goutham 		nic_mbx_send_ack(nic, vf);
882430da208SSunil Goutham 	}
883430da208SSunil Goutham }
884430da208SSunil Goutham 
8854a875509SSunil Goutham /* Enable or disable HW timestamping by BGX for pkts received on a LMAC */
nic_config_timestamp(struct nicpf * nic,int vf,struct set_ptp * ptp)8864a875509SSunil Goutham static void nic_config_timestamp(struct nicpf *nic, int vf, struct set_ptp *ptp)
8874a875509SSunil Goutham {
8884a875509SSunil Goutham 	struct pkind_cfg *pkind;
8894a875509SSunil Goutham 	u8 lmac, bgx_idx;
8904a875509SSunil Goutham 	u64 pkind_val, pkind_idx;
8914a875509SSunil Goutham 
8924a875509SSunil Goutham 	if (vf >= nic->num_vf_en)
8934a875509SSunil Goutham 		return;
8944a875509SSunil Goutham 
8954a875509SSunil Goutham 	bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
8964a875509SSunil Goutham 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
8974a875509SSunil Goutham 
8984a875509SSunil Goutham 	pkind_idx = lmac + bgx_idx * MAX_LMAC_PER_BGX;
8994a875509SSunil Goutham 	pkind_val = nic_reg_read(nic, NIC_PF_PKIND_0_15_CFG | (pkind_idx << 3));
9004a875509SSunil Goutham 	pkind = (struct pkind_cfg *)&pkind_val;
9014a875509SSunil Goutham 
9024a875509SSunil Goutham 	if (ptp->enable && !pkind->hdr_sl) {
9034a875509SSunil Goutham 		/* Skiplen to exclude 8byte timestamp while parsing pkt
9044a875509SSunil Goutham 		 * If not configured, will result in L2 errors.
9054a875509SSunil Goutham 		 */
9064a875509SSunil Goutham 		pkind->hdr_sl = 4;
9074a875509SSunil Goutham 		/* Adjust max packet length allowed */
9084a875509SSunil Goutham 		pkind->maxlen += (pkind->hdr_sl * 2);
9094a875509SSunil Goutham 		bgx_config_timestamping(nic->node, bgx_idx, lmac, true);
9104a875509SSunil Goutham 		nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7 | (1 << 3),
9114a875509SSunil Goutham 			      (ETYPE_ALG_ENDPARSE << 16) | ETH_P_1588);
9124a875509SSunil Goutham 	} else if (!ptp->enable && pkind->hdr_sl) {
9134a875509SSunil Goutham 		pkind->maxlen -= (pkind->hdr_sl * 2);
9144a875509SSunil Goutham 		pkind->hdr_sl = 0;
9154a875509SSunil Goutham 		bgx_config_timestamping(nic->node, bgx_idx, lmac, false);
9164a875509SSunil Goutham 		nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7 | (1 << 3),
9174a875509SSunil Goutham 			      (ETYPE_ALG_SKIP << 16) | ETH_P_8021Q);
9184a875509SSunil Goutham 	}
9194a875509SSunil Goutham 
9204a875509SSunil Goutham 	nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (pkind_idx << 3), pkind_val);
9214a875509SSunil Goutham }
9224a875509SSunil Goutham 
9232e1c3fffSVadim Lomovtsev /* Get BGX LMAC link status and update corresponding VF
9242e1c3fffSVadim Lomovtsev  * if there is a change, valid only if internal L2 switch
9252e1c3fffSVadim Lomovtsev  * is not present otherwise VF link is always treated as up
9262e1c3fffSVadim Lomovtsev  */
nic_link_status_get(struct nicpf * nic,u8 vf)9272c632ad8SVadim Lomovtsev static void nic_link_status_get(struct nicpf *nic, u8 vf)
9282c632ad8SVadim Lomovtsev {
9292c632ad8SVadim Lomovtsev 	union nic_mbx mbx = {};
9302c632ad8SVadim Lomovtsev 	struct bgx_link_status link;
9312c632ad8SVadim Lomovtsev 	u8 bgx, lmac;
9322c632ad8SVadim Lomovtsev 
9332c632ad8SVadim Lomovtsev 	mbx.link_status.msg = NIC_MBOX_MSG_BGX_LINK_CHANGE;
9342c632ad8SVadim Lomovtsev 
9352c632ad8SVadim Lomovtsev 	/* Get BGX, LMAC indices for the VF */
9362c632ad8SVadim Lomovtsev 	bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
9372c632ad8SVadim Lomovtsev 	lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
9382c632ad8SVadim Lomovtsev 
9392c632ad8SVadim Lomovtsev 	/* Get interface link status */
9402c632ad8SVadim Lomovtsev 	bgx_get_lmac_link_state(nic->node, bgx, lmac, &link);
9412c632ad8SVadim Lomovtsev 
9422c632ad8SVadim Lomovtsev 	/* Send a mbox message to VF with current link status */
9432c632ad8SVadim Lomovtsev 	mbx.link_status.link_up = link.link_up;
9442c632ad8SVadim Lomovtsev 	mbx.link_status.duplex = link.duplex;
9452c632ad8SVadim Lomovtsev 	mbx.link_status.speed = link.speed;
9462c632ad8SVadim Lomovtsev 	mbx.link_status.mac_type = link.mac_type;
9472c632ad8SVadim Lomovtsev 
9482c632ad8SVadim Lomovtsev 	/* reply with link status */
9492c632ad8SVadim Lomovtsev 	nic_send_msg_to_vf(nic, vf, &mbx);
9502c632ad8SVadim Lomovtsev }
9512c632ad8SVadim Lomovtsev 
9524863dea3SSunil Goutham /* Interrupt handler to handle mailbox messages from VFs */
nic_handle_mbx_intr(struct nicpf * nic,int vf)9534863dea3SSunil Goutham static void nic_handle_mbx_intr(struct nicpf *nic, int vf)
9544863dea3SSunil Goutham {
9554863dea3SSunil Goutham 	union nic_mbx mbx = {};
9564863dea3SSunil Goutham 	u64 *mbx_data;
9574863dea3SSunil Goutham 	u64 mbx_addr;
9584863dea3SSunil Goutham 	u64 reg_addr;
95992dc8769SSunil Goutham 	u64 cfg;
9604863dea3SSunil Goutham 	int bgx, lmac;
9614863dea3SSunil Goutham 	int i;
9624863dea3SSunil Goutham 	int ret = 0;
9634863dea3SSunil Goutham 
9644863dea3SSunil Goutham 	mbx_addr = nic_get_mbx_addr(vf);
9654863dea3SSunil Goutham 	mbx_data = (u64 *)&mbx;
9664863dea3SSunil Goutham 
9674863dea3SSunil Goutham 	for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
9684863dea3SSunil Goutham 		*mbx_data = nic_reg_read(nic, mbx_addr);
9694863dea3SSunil Goutham 		mbx_data++;
9704863dea3SSunil Goutham 		mbx_addr += sizeof(u64);
9714863dea3SSunil Goutham 	}
9724863dea3SSunil Goutham 
973ecae29cbSRadoslaw Biernacki 	dev_dbg(&nic->pdev->dev, "%s: Mailbox msg 0x%02x from VF%d\n",
9744863dea3SSunil Goutham 		__func__, mbx.msg.msg, vf);
9754863dea3SSunil Goutham 	switch (mbx.msg.msg) {
9764863dea3SSunil Goutham 	case NIC_MBOX_MSG_READY:
9774863dea3SSunil Goutham 		nic_mbx_send_ready(nic, vf);
9782e1c3fffSVadim Lomovtsev 		return;
9794863dea3SSunil Goutham 	case NIC_MBOX_MSG_QS_CFG:
9804863dea3SSunil Goutham 		reg_addr = NIC_PF_QSET_0_127_CFG |
9814863dea3SSunil Goutham 			   (mbx.qs.num << NIC_QS_ID_SHIFT);
98292dc8769SSunil Goutham 		cfg = mbx.qs.cfg;
98392dc8769SSunil Goutham 		/* Check if its a secondary Qset */
98492dc8769SSunil Goutham 		if (vf >= nic->num_vf_en) {
98592dc8769SSunil Goutham 			cfg = cfg & (~0x7FULL);
98692dc8769SSunil Goutham 			/* Assign this Qset to primary Qset's VF */
98792dc8769SSunil Goutham 			cfg |= nic->pqs_vf[vf];
98892dc8769SSunil Goutham 		}
98992dc8769SSunil Goutham 		nic_reg_write(nic, reg_addr, cfg);
9904863dea3SSunil Goutham 		break;
9914863dea3SSunil Goutham 	case NIC_MBOX_MSG_RQ_CFG:
9924863dea3SSunil Goutham 		reg_addr = NIC_PF_QSET_0_127_RQ_0_7_CFG |
9934863dea3SSunil Goutham 			   (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
9944863dea3SSunil Goutham 			   (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
9954863dea3SSunil Goutham 		nic_reg_write(nic, reg_addr, mbx.rq.cfg);
99602a72bd8SSunil Goutham 		/* Enable CQE_RX2_S extension in CQE_RX descriptor.
99702a72bd8SSunil Goutham 		 * This gets appended by default on 81xx/83xx chips,
99802a72bd8SSunil Goutham 		 * for consistency enabling the same on 88xx pass2
99902a72bd8SSunil Goutham 		 * where this is introduced.
100002a72bd8SSunil Goutham 		 */
100102a72bd8SSunil Goutham 		if (pass2_silicon(nic->pdev))
100202a72bd8SSunil Goutham 			nic_reg_write(nic, NIC_PF_RX_CFG, 0x01);
1003e22e86eaSZyta Szpak 		if (!pass1_silicon(nic->pdev))
1004e22e86eaSZyta Szpak 			nic_enable_tunnel_parsing(nic, vf);
10054863dea3SSunil Goutham 		break;
10064863dea3SSunil Goutham 	case NIC_MBOX_MSG_RQ_BP_CFG:
10074863dea3SSunil Goutham 		reg_addr = NIC_PF_QSET_0_127_RQ_0_7_BP_CFG |
10084863dea3SSunil Goutham 			   (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
10094863dea3SSunil Goutham 			   (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
10104863dea3SSunil Goutham 		nic_reg_write(nic, reg_addr, mbx.rq.cfg);
10114863dea3SSunil Goutham 		break;
10124863dea3SSunil Goutham 	case NIC_MBOX_MSG_RQ_SW_SYNC:
10134863dea3SSunil Goutham 		ret = nic_rcv_queue_sw_sync(nic);
10144863dea3SSunil Goutham 		break;
10154863dea3SSunil Goutham 	case NIC_MBOX_MSG_RQ_DROP_CFG:
10164863dea3SSunil Goutham 		reg_addr = NIC_PF_QSET_0_127_RQ_0_7_DROP_CFG |
10174863dea3SSunil Goutham 			   (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
10184863dea3SSunil Goutham 			   (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
10194863dea3SSunil Goutham 		nic_reg_write(nic, reg_addr, mbx.rq.cfg);
10204863dea3SSunil Goutham 		break;
10214863dea3SSunil Goutham 	case NIC_MBOX_MSG_SQ_CFG:
10224863dea3SSunil Goutham 		reg_addr = NIC_PF_QSET_0_127_SQ_0_7_CFG |
10234863dea3SSunil Goutham 			   (mbx.sq.qs_num << NIC_QS_ID_SHIFT) |
10244863dea3SSunil Goutham 			   (mbx.sq.sq_num << NIC_Q_NUM_SHIFT);
10254863dea3SSunil Goutham 		nic_reg_write(nic, reg_addr, mbx.sq.cfg);
102692dc8769SSunil Goutham 		nic_tx_channel_cfg(nic, mbx.qs.num, &mbx.sq);
10274863dea3SSunil Goutham 		break;
10284863dea3SSunil Goutham 	case NIC_MBOX_MSG_SET_MAC:
1029ecae29cbSRadoslaw Biernacki 		if (vf >= nic->num_vf_en) {
1030ecae29cbSRadoslaw Biernacki 			ret = -1; /* NACK */
103192dc8769SSunil Goutham 			break;
1032ecae29cbSRadoslaw Biernacki 		}
10334863dea3SSunil Goutham 		lmac = mbx.mac.vf_id;
10344863dea3SSunil Goutham 		bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
10354863dea3SSunil Goutham 		lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
1036e610cb32SAleksey Makarov 		bgx_set_lmac_mac(nic->node, bgx, lmac, mbx.mac.mac_addr);
10374863dea3SSunil Goutham 		break;
10384863dea3SSunil Goutham 	case NIC_MBOX_MSG_SET_MAX_FRS:
10394863dea3SSunil Goutham 		ret = nic_update_hw_frs(nic, mbx.frs.max_frs,
10404863dea3SSunil Goutham 					mbx.frs.vf_id);
10414863dea3SSunil Goutham 		break;
10424863dea3SSunil Goutham 	case NIC_MBOX_MSG_CPI_CFG:
10434863dea3SSunil Goutham 		nic_config_cpi(nic, &mbx.cpi_cfg);
10444863dea3SSunil Goutham 		break;
10454863dea3SSunil Goutham 	case NIC_MBOX_MSG_RSS_SIZE:
10464863dea3SSunil Goutham 		nic_send_rss_size(nic, vf);
10472e1c3fffSVadim Lomovtsev 		return;
10484863dea3SSunil Goutham 	case NIC_MBOX_MSG_RSS_CFG:
10494863dea3SSunil Goutham 	case NIC_MBOX_MSG_RSS_CFG_CONT:
10504863dea3SSunil Goutham 		nic_config_rss(nic, &mbx.rss_cfg);
10514863dea3SSunil Goutham 		break;
10524863dea3SSunil Goutham 	case NIC_MBOX_MSG_CFG_DONE:
10534863dea3SSunil Goutham 		/* Last message of VF config msg sequence */
1054f406ce42SPavel Fedin 		nic_enable_vf(nic, vf, true);
10550dd563b9SVadim Lomovtsev 		break;
10564863dea3SSunil Goutham 	case NIC_MBOX_MSG_SHUTDOWN:
10574863dea3SSunil Goutham 		/* First msg in VF teardown sequence */
105892dc8769SSunil Goutham 		if (vf >= nic->num_vf_en)
105992dc8769SSunil Goutham 			nic->sqs_used[vf - nic->num_vf_en] = false;
106092dc8769SSunil Goutham 		nic->pqs_vf[vf] = 0;
1061f406ce42SPavel Fedin 		nic_enable_vf(nic, vf, false);
10624863dea3SSunil Goutham 		break;
106392dc8769SSunil Goutham 	case NIC_MBOX_MSG_ALLOC_SQS:
106492dc8769SSunil Goutham 		nic_alloc_sqs(nic, &mbx.sqs_alloc);
10652e1c3fffSVadim Lomovtsev 		return;
106692dc8769SSunil Goutham 	case NIC_MBOX_MSG_NICVF_PTR:
106792dc8769SSunil Goutham 		nic->nicvf[vf] = mbx.nicvf.nicvf;
106892dc8769SSunil Goutham 		break;
106992dc8769SSunil Goutham 	case NIC_MBOX_MSG_PNICVF_PTR:
107092dc8769SSunil Goutham 		nic_send_pnicvf(nic, vf);
10712e1c3fffSVadim Lomovtsev 		return;
107292dc8769SSunil Goutham 	case NIC_MBOX_MSG_SNICVF_PTR:
107392dc8769SSunil Goutham 		nic_send_snicvf(nic, &mbx.nicvf);
10742e1c3fffSVadim Lomovtsev 		return;
10754863dea3SSunil Goutham 	case NIC_MBOX_MSG_BGX_STATS:
10764863dea3SSunil Goutham 		nic_get_bgx_stats(nic, &mbx.bgx_stats);
10772e1c3fffSVadim Lomovtsev 		return;
1078d77a2384SSunil Goutham 	case NIC_MBOX_MSG_LOOPBACK:
1079d77a2384SSunil Goutham 		ret = nic_config_loopback(nic, &mbx.lbk);
1080d77a2384SSunil Goutham 		break;
10813458c40dSJerin Jacob 	case NIC_MBOX_MSG_RESET_STAT_COUNTER:
10823458c40dSJerin Jacob 		ret = nic_reset_stat_counters(nic, vf, &mbx.reset_stat);
10833458c40dSJerin Jacob 		break;
1084430da208SSunil Goutham 	case NIC_MBOX_MSG_PFC:
1085430da208SSunil Goutham 		nic_pause_frame(nic, vf, &mbx.pfc);
10862e1c3fffSVadim Lomovtsev 		return;
10874a875509SSunil Goutham 	case NIC_MBOX_MSG_PTP_CFG:
10884a875509SSunil Goutham 		nic_config_timestamp(nic, vf, &mbx.ptp);
10894a875509SSunil Goutham 		break;
1090aba4a263SVadim Lomovtsev 	case NIC_MBOX_MSG_RESET_XCAST:
1091aba4a263SVadim Lomovtsev 		if (vf >= nic->num_vf_en) {
1092aba4a263SVadim Lomovtsev 			ret = -1; /* NACK */
1093aba4a263SVadim Lomovtsev 			break;
1094aba4a263SVadim Lomovtsev 		}
1095aba4a263SVadim Lomovtsev 		bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1096aba4a263SVadim Lomovtsev 		lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1097aba4a263SVadim Lomovtsev 		bgx_reset_xcast_mode(nic->node, bgx, lmac,
1098aba4a263SVadim Lomovtsev 				     vf < NIC_VF_PER_MBX_REG ? vf :
1099aba4a263SVadim Lomovtsev 				     vf - NIC_VF_PER_MBX_REG);
1100aba4a263SVadim Lomovtsev 		break;
1101aba4a263SVadim Lomovtsev 
1102aba4a263SVadim Lomovtsev 	case NIC_MBOX_MSG_ADD_MCAST:
1103aba4a263SVadim Lomovtsev 		if (vf >= nic->num_vf_en) {
1104aba4a263SVadim Lomovtsev 			ret = -1; /* NACK */
1105aba4a263SVadim Lomovtsev 			break;
1106aba4a263SVadim Lomovtsev 		}
1107aba4a263SVadim Lomovtsev 		bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1108aba4a263SVadim Lomovtsev 		lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1109aba4a263SVadim Lomovtsev 		bgx_set_dmac_cam_filter(nic->node, bgx, lmac,
111053544396SVadim Lomovtsev 					mbx.xcast.mac,
1111aba4a263SVadim Lomovtsev 					vf < NIC_VF_PER_MBX_REG ? vf :
1112aba4a263SVadim Lomovtsev 					vf - NIC_VF_PER_MBX_REG);
1113aba4a263SVadim Lomovtsev 		break;
1114aba4a263SVadim Lomovtsev 
1115aba4a263SVadim Lomovtsev 	case NIC_MBOX_MSG_SET_XCAST:
1116aba4a263SVadim Lomovtsev 		if (vf >= nic->num_vf_en) {
1117aba4a263SVadim Lomovtsev 			ret = -1; /* NACK */
1118aba4a263SVadim Lomovtsev 			break;
1119aba4a263SVadim Lomovtsev 		}
1120aba4a263SVadim Lomovtsev 		bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1121aba4a263SVadim Lomovtsev 		lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
112253544396SVadim Lomovtsev 		bgx_set_xcast_mode(nic->node, bgx, lmac, mbx.xcast.mode);
1123aba4a263SVadim Lomovtsev 		break;
11242c632ad8SVadim Lomovtsev 	case NIC_MBOX_MSG_BGX_LINK_CHANGE:
11252c632ad8SVadim Lomovtsev 		if (vf >= nic->num_vf_en) {
11262c632ad8SVadim Lomovtsev 			ret = -1; /* NACK */
11272c632ad8SVadim Lomovtsev 			break;
11282c632ad8SVadim Lomovtsev 		}
11292c632ad8SVadim Lomovtsev 		nic_link_status_get(nic, vf);
11302e1c3fffSVadim Lomovtsev 		return;
11314863dea3SSunil Goutham 	default:
11324863dea3SSunil Goutham 		dev_err(&nic->pdev->dev,
11334863dea3SSunil Goutham 			"Invalid msg from VF%d, msg 0x%x\n", vf, mbx.msg.msg);
11344863dea3SSunil Goutham 		break;
11354863dea3SSunil Goutham 	}
11364863dea3SSunil Goutham 
1137ecae29cbSRadoslaw Biernacki 	if (!ret) {
11384863dea3SSunil Goutham 		nic_mbx_send_ack(nic, vf);
1139ecae29cbSRadoslaw Biernacki 	} else if (mbx.msg.msg != NIC_MBOX_MSG_READY) {
1140ecae29cbSRadoslaw Biernacki 		dev_err(&nic->pdev->dev, "NACK for MBOX 0x%02x from VF %d\n",
1141ecae29cbSRadoslaw Biernacki 			mbx.msg.msg, vf);
11424863dea3SSunil Goutham 		nic_mbx_send_nack(nic, vf);
1143ecae29cbSRadoslaw Biernacki 	}
11444863dea3SSunil Goutham }
11454863dea3SSunil Goutham 
nic_mbx_intr_handler(int irq,void * nic_irq)114652358aadSSunil Goutham static irqreturn_t nic_mbx_intr_handler(int irq, void *nic_irq)
11474863dea3SSunil Goutham {
114852358aadSSunil Goutham 	struct nicpf *nic = (struct nicpf *)nic_irq;
114952358aadSSunil Goutham 	int mbx;
11504863dea3SSunil Goutham 	u64 intr;
1151aba4a263SVadim Lomovtsev 	u8  vf;
11524863dea3SSunil Goutham 
1153*6b292a04SThomas Gleixner 	if (irq == nic->irq_allocated[NIC_PF_INTR_ID_MBOX0])
115452358aadSSunil Goutham 		mbx = 0;
115552358aadSSunil Goutham 	else
115652358aadSSunil Goutham 		mbx = 1;
115752358aadSSunil Goutham 
11584863dea3SSunil Goutham 	intr = nic_reg_read(nic, NIC_PF_MAILBOX_INT + (mbx << 3));
11594863dea3SSunil Goutham 	dev_dbg(&nic->pdev->dev, "PF interrupt Mbox%d 0x%llx\n", mbx, intr);
1160aba4a263SVadim Lomovtsev 	for (vf = 0; vf < NIC_VF_PER_MBX_REG; vf++) {
11614863dea3SSunil Goutham 		if (intr & (1ULL << vf)) {
11624863dea3SSunil Goutham 			dev_dbg(&nic->pdev->dev, "Intr from VF %d\n",
1163aba4a263SVadim Lomovtsev 				vf + (mbx * NIC_VF_PER_MBX_REG));
116492dc8769SSunil Goutham 
1165aba4a263SVadim Lomovtsev 			nic_handle_mbx_intr(nic, vf +
1166aba4a263SVadim Lomovtsev 					    (mbx * NIC_VF_PER_MBX_REG));
11674863dea3SSunil Goutham 			nic_clear_mbx_intr(nic, vf, mbx);
11684863dea3SSunil Goutham 		}
11694863dea3SSunil Goutham 	}
11704863dea3SSunil Goutham 	return IRQ_HANDLED;
11714863dea3SSunil Goutham }
11724863dea3SSunil Goutham 
nic_free_all_interrupts(struct nicpf * nic)11734863dea3SSunil Goutham static void nic_free_all_interrupts(struct nicpf *nic)
11744863dea3SSunil Goutham {
11754863dea3SSunil Goutham 	int irq;
11764863dea3SSunil Goutham 
11774863dea3SSunil Goutham 	for (irq = 0; irq < nic->num_vec; irq++) {
11784863dea3SSunil Goutham 		if (nic->irq_allocated[irq])
1179*6b292a04SThomas Gleixner 			free_irq(nic->irq_allocated[irq], nic);
1180*6b292a04SThomas Gleixner 		nic->irq_allocated[irq] = 0;
11814863dea3SSunil Goutham 	}
11824863dea3SSunil Goutham }
11834863dea3SSunil Goutham 
nic_register_interrupts(struct nicpf * nic)11844863dea3SSunil Goutham static int nic_register_interrupts(struct nicpf *nic)
11854863dea3SSunil Goutham {
1186*6b292a04SThomas Gleixner 	int i, ret, irq;
1187ce211b17SThanneeru Srinivasulu 	nic->num_vec = pci_msix_vec_count(nic->pdev);
11884863dea3SSunil Goutham 
11894863dea3SSunil Goutham 	/* Enable MSI-X */
1190ce211b17SThanneeru Srinivasulu 	ret = pci_alloc_irq_vectors(nic->pdev, nic->num_vec, nic->num_vec,
1191ce211b17SThanneeru Srinivasulu 				    PCI_IRQ_MSIX);
1192ce211b17SThanneeru Srinivasulu 	if (ret < 0) {
1193ce211b17SThanneeru Srinivasulu 		dev_err(&nic->pdev->dev,
1194ce211b17SThanneeru Srinivasulu 			"Request for #%d msix vectors failed, returned %d\n",
1195ce211b17SThanneeru Srinivasulu 			   nic->num_vec, ret);
1196b2cddb44SZheyu Ma 		return ret;
1197ce211b17SThanneeru Srinivasulu 	}
11984863dea3SSunil Goutham 
119952358aadSSunil Goutham 	/* Register mailbox interrupt handler */
120052358aadSSunil Goutham 	for (i = NIC_PF_INTR_ID_MBOX0; i < nic->num_vec; i++) {
120152358aadSSunil Goutham 		sprintf(nic->irq_name[i],
120252358aadSSunil Goutham 			"NICPF Mbox%d", (i - NIC_PF_INTR_ID_MBOX0));
120352358aadSSunil Goutham 
1204*6b292a04SThomas Gleixner 		irq = pci_irq_vector(nic->pdev, i);
1205*6b292a04SThomas Gleixner 		ret = request_irq(irq, nic_mbx_intr_handler, 0,
120652358aadSSunil Goutham 				  nic->irq_name[i], nic);
12074863dea3SSunil Goutham 		if (ret)
12084863dea3SSunil Goutham 			goto fail;
12094863dea3SSunil Goutham 
1210*6b292a04SThomas Gleixner 		nic->irq_allocated[i] = irq;
121152358aadSSunil Goutham 	}
12124863dea3SSunil Goutham 
12134863dea3SSunil Goutham 	/* Enable mailbox interrupt */
12144863dea3SSunil Goutham 	nic_enable_mbx_intr(nic);
12154863dea3SSunil Goutham 	return 0;
12164863dea3SSunil Goutham 
12174863dea3SSunil Goutham fail:
12184863dea3SSunil Goutham 	dev_err(&nic->pdev->dev, "Request irq failed\n");
12194863dea3SSunil Goutham 	nic_free_all_interrupts(nic);
1220ce211b17SThanneeru Srinivasulu 	pci_free_irq_vectors(nic->pdev);
1221ce211b17SThanneeru Srinivasulu 	nic->num_vec = 0;
12224863dea3SSunil Goutham 	return ret;
12234863dea3SSunil Goutham }
12244863dea3SSunil Goutham 
nic_unregister_interrupts(struct nicpf * nic)12254863dea3SSunil Goutham static void nic_unregister_interrupts(struct nicpf *nic)
12264863dea3SSunil Goutham {
12274863dea3SSunil Goutham 	nic_free_all_interrupts(nic);
1228ce211b17SThanneeru Srinivasulu 	pci_free_irq_vectors(nic->pdev);
1229ce211b17SThanneeru Srinivasulu 	nic->num_vec = 0;
12304863dea3SSunil Goutham }
12314863dea3SSunil Goutham 
nic_num_sqs_en(struct nicpf * nic,int vf_en)123292dc8769SSunil Goutham static int nic_num_sqs_en(struct nicpf *nic, int vf_en)
123392dc8769SSunil Goutham {
123492dc8769SSunil Goutham 	int pos, sqs_per_vf = MAX_SQS_PER_VF_SINGLE_NODE;
123592dc8769SSunil Goutham 	u16 total_vf;
123692dc8769SSunil Goutham 
12373a397ebeSSunil Goutham 	/* Secondary Qsets are needed only if CPU count is
12383a397ebeSSunil Goutham 	 * morethan MAX_QUEUES_PER_QSET.
12393a397ebeSSunil Goutham 	 */
12403a397ebeSSunil Goutham 	if (num_online_cpus() <= MAX_QUEUES_PER_QSET)
12413a397ebeSSunil Goutham 		return 0;
12423a397ebeSSunil Goutham 
124392dc8769SSunil Goutham 	/* Check if its a multi-node environment */
124492dc8769SSunil Goutham 	if (nr_node_ids > 1)
124592dc8769SSunil Goutham 		sqs_per_vf = MAX_SQS_PER_VF;
124692dc8769SSunil Goutham 
124792dc8769SSunil Goutham 	pos = pci_find_ext_capability(nic->pdev, PCI_EXT_CAP_ID_SRIOV);
124892dc8769SSunil Goutham 	pci_read_config_word(nic->pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf);
124992dc8769SSunil Goutham 	return min(total_vf - vf_en, vf_en * sqs_per_vf);
125092dc8769SSunil Goutham }
125192dc8769SSunil Goutham 
nic_sriov_init(struct pci_dev * pdev,struct nicpf * nic)12524863dea3SSunil Goutham static int nic_sriov_init(struct pci_dev *pdev, struct nicpf *nic)
12534863dea3SSunil Goutham {
12544863dea3SSunil Goutham 	int pos = 0;
125592dc8769SSunil Goutham 	int vf_en;
12564863dea3SSunil Goutham 	int err;
12574863dea3SSunil Goutham 	u16 total_vf_cnt;
12584863dea3SSunil Goutham 
12594863dea3SSunil Goutham 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
12604863dea3SSunil Goutham 	if (!pos) {
12614863dea3SSunil Goutham 		dev_err(&pdev->dev, "SRIOV capability is not found in PCIe config space\n");
12624863dea3SSunil Goutham 		return -ENODEV;
12634863dea3SSunil Goutham 	}
12644863dea3SSunil Goutham 
12654863dea3SSunil Goutham 	pci_read_config_word(pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf_cnt);
12664863dea3SSunil Goutham 	if (total_vf_cnt < nic->num_vf_en)
12674863dea3SSunil Goutham 		nic->num_vf_en = total_vf_cnt;
12684863dea3SSunil Goutham 
12694863dea3SSunil Goutham 	if (!total_vf_cnt)
12704863dea3SSunil Goutham 		return 0;
12714863dea3SSunil Goutham 
127292dc8769SSunil Goutham 	vf_en = nic->num_vf_en;
127392dc8769SSunil Goutham 	nic->num_sqs_en = nic_num_sqs_en(nic, nic->num_vf_en);
127492dc8769SSunil Goutham 	vf_en += nic->num_sqs_en;
127592dc8769SSunil Goutham 
127692dc8769SSunil Goutham 	err = pci_enable_sriov(pdev, vf_en);
12774863dea3SSunil Goutham 	if (err) {
12784863dea3SSunil Goutham 		dev_err(&pdev->dev, "SRIOV enable failed, num VF is %d\n",
127992dc8769SSunil Goutham 			vf_en);
12804863dea3SSunil Goutham 		nic->num_vf_en = 0;
12814863dea3SSunil Goutham 		return err;
12824863dea3SSunil Goutham 	}
12834863dea3SSunil Goutham 
12844863dea3SSunil Goutham 	dev_info(&pdev->dev, "SRIOV enabled, number of VF available %d\n",
128592dc8769SSunil Goutham 		 vf_en);
12864863dea3SSunil Goutham 
12874863dea3SSunil Goutham 	nic->flags |= NIC_SRIOV_ENABLED;
12884863dea3SSunil Goutham 	return 0;
12894863dea3SSunil Goutham }
12904863dea3SSunil Goutham 
nic_probe(struct pci_dev * pdev,const struct pci_device_id * ent)12914863dea3SSunil Goutham static int nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
12924863dea3SSunil Goutham {
12934863dea3SSunil Goutham 	struct device *dev = &pdev->dev;
12944863dea3SSunil Goutham 	struct nicpf *nic;
12953d67a507SAleksey Makarov 	u8     max_lmac;
12964863dea3SSunil Goutham 	int    err;
12974863dea3SSunil Goutham 
12984863dea3SSunil Goutham 	BUILD_BUG_ON(sizeof(union nic_mbx) > 16);
12994863dea3SSunil Goutham 
13004863dea3SSunil Goutham 	nic = devm_kzalloc(dev, sizeof(*nic), GFP_KERNEL);
13014863dea3SSunil Goutham 	if (!nic)
13024863dea3SSunil Goutham 		return -ENOMEM;
13034863dea3SSunil Goutham 
1304a5c3d498SSunil Goutham 	nic->hw = devm_kzalloc(dev, sizeof(struct hw_info), GFP_KERNEL);
13053d67a507SAleksey Makarov 	if (!nic->hw)
1306a5c3d498SSunil Goutham 		return -ENOMEM;
1307a5c3d498SSunil Goutham 
13084863dea3SSunil Goutham 	pci_set_drvdata(pdev, nic);
13094863dea3SSunil Goutham 
13104863dea3SSunil Goutham 	nic->pdev = pdev;
13114863dea3SSunil Goutham 
13124863dea3SSunil Goutham 	err = pci_enable_device(pdev);
13134863dea3SSunil Goutham 	if (err) {
13144863dea3SSunil Goutham 		pci_set_drvdata(pdev, NULL);
131552583c8dSCai Huoqing 		return dev_err_probe(dev, err, "Failed to enable PCI device\n");
13164863dea3SSunil Goutham 	}
13174863dea3SSunil Goutham 
13184863dea3SSunil Goutham 	err = pci_request_regions(pdev, DRV_NAME);
13194863dea3SSunil Goutham 	if (err) {
13204863dea3SSunil Goutham 		dev_err(dev, "PCI request regions failed 0x%x\n", err);
13214863dea3SSunil Goutham 		goto err_disable_device;
13224863dea3SSunil Goutham 	}
13234863dea3SSunil Goutham 
13241e0dd56eSChristophe JAILLET 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
13254863dea3SSunil Goutham 	if (err) {
13264863dea3SSunil Goutham 		dev_err(dev, "Unable to get usable DMA configuration\n");
13274863dea3SSunil Goutham 		goto err_release_regions;
13284863dea3SSunil Goutham 	}
13294863dea3SSunil Goutham 
13304863dea3SSunil Goutham 	/* MAP PF's configuration registers */
13314863dea3SSunil Goutham 	nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
13324863dea3SSunil Goutham 	if (!nic->reg_base) {
13334863dea3SSunil Goutham 		dev_err(dev, "Cannot map config register space, aborting\n");
13344863dea3SSunil Goutham 		err = -ENOMEM;
13354863dea3SSunil Goutham 		goto err_release_regions;
13364863dea3SSunil Goutham 	}
13374863dea3SSunil Goutham 
1338d768b678SRobert Richter 	nic->node = nic_get_node_id(pdev);
13394863dea3SSunil Goutham 
13403d67a507SAleksey Makarov 	/* Get HW capability info */
13413d67a507SAleksey Makarov 	nic_get_hw_info(nic);
13423d67a507SAleksey Makarov 
13433d67a507SAleksey Makarov 	/* Allocate memory for LMAC tracking elements */
13443d67a507SAleksey Makarov 	err = -ENOMEM;
13453d67a507SAleksey Makarov 	max_lmac = nic->hw->bgx_cnt * MAX_LMAC_PER_BGX;
13463d67a507SAleksey Makarov 
13473d67a507SAleksey Makarov 	nic->vf_lmac_map = devm_kmalloc_array(dev, max_lmac, sizeof(u8),
13483d67a507SAleksey Makarov 					      GFP_KERNEL);
13493d67a507SAleksey Makarov 	if (!nic->vf_lmac_map)
1350949b5331SSunil Goutham 		goto err_release_regions;
13514863dea3SSunil Goutham 
13523d67a507SAleksey Makarov 	/* Initialize hardware */
13533d67a507SAleksey Makarov 	nic_init_hw(nic);
13543d67a507SAleksey Makarov 
1355a5c3d498SSunil Goutham 	nic_set_lmac_vf_mapping(nic);
13564863dea3SSunil Goutham 
13574863dea3SSunil Goutham 	/* Register interrupts */
13584863dea3SSunil Goutham 	err = nic_register_interrupts(nic);
13594863dea3SSunil Goutham 	if (err)
13604863dea3SSunil Goutham 		goto err_release_regions;
13614863dea3SSunil Goutham 
13624863dea3SSunil Goutham 	/* Configure SRIOV */
13634863dea3SSunil Goutham 	err = nic_sriov_init(pdev, nic);
13644863dea3SSunil Goutham 	if (err)
13654863dea3SSunil Goutham 		goto err_unregister_interrupts;
13664863dea3SSunil Goutham 
13674863dea3SSunil Goutham 	return 0;
13684863dea3SSunil Goutham 
13694863dea3SSunil Goutham err_unregister_interrupts:
13704863dea3SSunil Goutham 	nic_unregister_interrupts(nic);
13714863dea3SSunil Goutham err_release_regions:
13724863dea3SSunil Goutham 	pci_release_regions(pdev);
13734863dea3SSunil Goutham err_disable_device:
13744863dea3SSunil Goutham 	pci_disable_device(pdev);
13754863dea3SSunil Goutham 	pci_set_drvdata(pdev, NULL);
13764863dea3SSunil Goutham 	return err;
13774863dea3SSunil Goutham }
13784863dea3SSunil Goutham 
nic_remove(struct pci_dev * pdev)13794863dea3SSunil Goutham static void nic_remove(struct pci_dev *pdev)
13804863dea3SSunil Goutham {
13814863dea3SSunil Goutham 	struct nicpf *nic = pci_get_drvdata(pdev);
13824863dea3SSunil Goutham 
138324a6d2ddSLorenzo Bianconi 	if (!nic)
138424a6d2ddSLorenzo Bianconi 		return;
138524a6d2ddSLorenzo Bianconi 
13864863dea3SSunil Goutham 	if (nic->flags & NIC_SRIOV_ENABLED)
13874863dea3SSunil Goutham 		pci_disable_sriov(pdev);
13884863dea3SSunil Goutham 
13894863dea3SSunil Goutham 	nic_unregister_interrupts(nic);
13904863dea3SSunil Goutham 	pci_release_regions(pdev);
1391a5c3d498SSunil Goutham 
13924863dea3SSunil Goutham 	pci_disable_device(pdev);
13934863dea3SSunil Goutham 	pci_set_drvdata(pdev, NULL);
13944863dea3SSunil Goutham }
13954863dea3SSunil Goutham 
13964863dea3SSunil Goutham static struct pci_driver nic_driver = {
13974863dea3SSunil Goutham 	.name = DRV_NAME,
13984863dea3SSunil Goutham 	.id_table = nic_id_table,
13994863dea3SSunil Goutham 	.probe = nic_probe,
14004863dea3SSunil Goutham 	.remove = nic_remove,
14014863dea3SSunil Goutham };
14024863dea3SSunil Goutham 
nic_init_module(void)14034863dea3SSunil Goutham static int __init nic_init_module(void)
14044863dea3SSunil Goutham {
14054863dea3SSunil Goutham 	pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
14064863dea3SSunil Goutham 
14074863dea3SSunil Goutham 	return pci_register_driver(&nic_driver);
14084863dea3SSunil Goutham }
14094863dea3SSunil Goutham 
nic_cleanup_module(void)14104863dea3SSunil Goutham static void __exit nic_cleanup_module(void)
14114863dea3SSunil Goutham {
14124863dea3SSunil Goutham 	pci_unregister_driver(&nic_driver);
14134863dea3SSunil Goutham }
14144863dea3SSunil Goutham 
14154863dea3SSunil Goutham module_init(nic_init_module);
14164863dea3SSunil Goutham module_exit(nic_cleanup_module);
1417