xref: /freebsd/sys/dev/cxgbe/t4_vf.c (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
16af45170SJohn Baldwin /*-
26af45170SJohn Baldwin  * Copyright (c) 2016 Chelsio Communications, Inc.
36af45170SJohn Baldwin  * All rights reserved.
46af45170SJohn Baldwin  * Written by: John Baldwin <jhb@FreeBSD.org>
56af45170SJohn Baldwin  *
66af45170SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
76af45170SJohn Baldwin  * modification, are permitted provided that the following conditions
86af45170SJohn Baldwin  * are met:
96af45170SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
106af45170SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
116af45170SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
126af45170SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
136af45170SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
146af45170SJohn Baldwin  *
156af45170SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
166af45170SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
176af45170SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
186af45170SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
196af45170SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
206af45170SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
216af45170SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
226af45170SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
236af45170SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
246af45170SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
256af45170SJohn Baldwin  * SUCH DAMAGE.
266af45170SJohn Baldwin  */
276af45170SJohn Baldwin 
286af45170SJohn Baldwin #include <sys/cdefs.h>
296af45170SJohn Baldwin #include "opt_inet.h"
306af45170SJohn Baldwin #include "opt_inet6.h"
316af45170SJohn Baldwin 
326af45170SJohn Baldwin #include <sys/param.h>
336af45170SJohn Baldwin #include <sys/bus.h>
346af45170SJohn Baldwin #include <sys/conf.h>
356af45170SJohn Baldwin #include <sys/kernel.h>
366af45170SJohn Baldwin #include <sys/module.h>
376af45170SJohn Baldwin #include <sys/priv.h>
386af45170SJohn Baldwin #include <dev/pci/pcivar.h>
396af45170SJohn Baldwin #if defined(__i386__) || defined(__amd64__)
406af45170SJohn Baldwin #include <vm/vm.h>
416af45170SJohn Baldwin #include <vm/pmap.h>
426af45170SJohn Baldwin #endif
436af45170SJohn Baldwin 
446af45170SJohn Baldwin #include "common/common.h"
456af45170SJohn Baldwin #include "common/t4_regs.h"
466af45170SJohn Baldwin #include "t4_ioctl.h"
476af45170SJohn Baldwin #include "t4_mp_ring.h"
486af45170SJohn Baldwin 
496af45170SJohn Baldwin /*
506af45170SJohn Baldwin  * Some notes:
516af45170SJohn Baldwin  *
526af45170SJohn Baldwin  * The Virtual Interfaces are connected to an internal switch on the chip
536af45170SJohn Baldwin  * which allows VIs attached to the same port to talk to each other even when
546af45170SJohn Baldwin  * the port link is down.  As a result, we might want to always report a
556af45170SJohn Baldwin  * VF's link as being "up".
566af45170SJohn Baldwin  *
576af45170SJohn Baldwin  * XXX: Add a TUNABLE and possible per-device sysctl for this?
586af45170SJohn Baldwin  */
596af45170SJohn Baldwin 
606af45170SJohn Baldwin struct intrs_and_queues {
616af45170SJohn Baldwin 	uint16_t intr_type;	/* MSI, or MSI-X */
626af45170SJohn Baldwin 	uint16_t nirq;		/* Total # of vectors */
638c61c6bbSNavdeep Parhar 	uint16_t ntxq;		/* # of NIC txq's for each port */
648c61c6bbSNavdeep Parhar 	uint16_t nrxq;		/* # of NIC rxq's for each port */
656af45170SJohn Baldwin };
666af45170SJohn Baldwin 
676af45170SJohn Baldwin struct {
686af45170SJohn Baldwin 	uint16_t device;
696af45170SJohn Baldwin 	char *desc;
706af45170SJohn Baldwin } t4vf_pciids[] = {
716af45170SJohn Baldwin 	{0x4800, "Chelsio T440-dbg VF"},
726af45170SJohn Baldwin 	{0x4801, "Chelsio T420-CR VF"},
736af45170SJohn Baldwin 	{0x4802, "Chelsio T422-CR VF"},
746af45170SJohn Baldwin 	{0x4803, "Chelsio T440-CR VF"},
756af45170SJohn Baldwin 	{0x4804, "Chelsio T420-BCH VF"},
766af45170SJohn Baldwin 	{0x4805, "Chelsio T440-BCH VF"},
776af45170SJohn Baldwin 	{0x4806, "Chelsio T440-CH VF"},
786af45170SJohn Baldwin 	{0x4807, "Chelsio T420-SO VF"},
796af45170SJohn Baldwin 	{0x4808, "Chelsio T420-CX VF"},
806af45170SJohn Baldwin 	{0x4809, "Chelsio T420-BT VF"},
816af45170SJohn Baldwin 	{0x480a, "Chelsio T404-BT VF"},
826af45170SJohn Baldwin 	{0x480e, "Chelsio T440-LP-CR VF"},
836af45170SJohn Baldwin }, t5vf_pciids[] = {
846af45170SJohn Baldwin 	{0x5800, "Chelsio T580-dbg VF"},
856af45170SJohn Baldwin 	{0x5801,  "Chelsio T520-CR VF"},	/* 2 x 10G */
866af45170SJohn Baldwin 	{0x5802,  "Chelsio T522-CR VF"},	/* 2 x 10G, 2 X 1G */
876af45170SJohn Baldwin 	{0x5803,  "Chelsio T540-CR VF"},	/* 4 x 10G */
886af45170SJohn Baldwin 	{0x5807,  "Chelsio T520-SO VF"},	/* 2 x 10G, nomem */
896af45170SJohn Baldwin 	{0x5809,  "Chelsio T520-BT VF"},	/* 2 x 10GBaseT */
906af45170SJohn Baldwin 	{0x580a,  "Chelsio T504-BT VF"},	/* 4 x 1G */
916af45170SJohn Baldwin 	{0x580d,  "Chelsio T580-CR VF"},	/* 2 x 40G */
926af45170SJohn Baldwin 	{0x580e,  "Chelsio T540-LP-CR VF"},	/* 4 x 10G */
936af45170SJohn Baldwin 	{0x5810,  "Chelsio T580-LP-CR VF"},	/* 2 x 40G */
946af45170SJohn Baldwin 	{0x5811,  "Chelsio T520-LL-CR VF"},	/* 2 x 10G */
956af45170SJohn Baldwin 	{0x5812,  "Chelsio T560-CR VF"},	/* 1 x 40G, 2 x 10G */
966af45170SJohn Baldwin 	{0x5814,  "Chelsio T580-LP-SO-CR VF"},	/* 2 x 40G, nomem */
976af45170SJohn Baldwin 	{0x5815,  "Chelsio T502-BT VF"},	/* 2 x 1G */
985877e649SNavdeep Parhar 	{0x5818,  "Chelsio T540-BT VF"},	/* 4 x 10GBaseT */
995877e649SNavdeep Parhar 	{0x5819,  "Chelsio T540-LP-BT VF"},	/* 4 x 10GBaseT */
1005877e649SNavdeep Parhar 	{0x581a,  "Chelsio T540-SO-BT VF"},	/* 4 x 10GBaseT, nomem */
1015877e649SNavdeep Parhar 	{0x581b,  "Chelsio T540-SO-CR VF"},	/* 4 x 10G, nomem */
102e6b81479SNavdeep Parhar }, t6vf_pciids[] = {
10349c0beb6SNavdeep Parhar 	{0x6800, "Chelsio T6-DBG-25 VF"},	/* 2 x 10/25G, debug */
104e6b81479SNavdeep Parhar 	{0x6801, "Chelsio T6225-CR VF"},	/* 2 x 10/25G */
105e6b81479SNavdeep Parhar 	{0x6802, "Chelsio T6225-SO-CR VF"},	/* 2 x 10/25G, nomem */
10649c0beb6SNavdeep Parhar 	{0x6803, "Chelsio T6425-CR VF"},	/* 4 x 10/25G */
10749c0beb6SNavdeep Parhar 	{0x6804, "Chelsio T6425-SO-CR VF"},	/* 4 x 10/25G, nomem */
10849c0beb6SNavdeep Parhar 	{0x6805, "Chelsio T6225-OCP-SO VF"},	/* 2 x 10/25G, nomem */
10949c0beb6SNavdeep Parhar 	{0x6806, "Chelsio T62100-OCP-SO VF"},	/* 2 x 40/50/100G, nomem */
110e6b81479SNavdeep Parhar 	{0x6807, "Chelsio T62100-LP-CR VF"},	/* 2 x 40/50/100G */
111e6b81479SNavdeep Parhar 	{0x6808, "Chelsio T62100-SO-CR VF"},	/* 2 x 40/50/100G, nomem */
11249c0beb6SNavdeep Parhar 	{0x6809, "Chelsio T6210-BT VF"},	/* 2 x 10GBASE-T */
113e6b81479SNavdeep Parhar 	{0x680d, "Chelsio T62100-CR VF"},	/* 2 x 40/50/100G */
11449c0beb6SNavdeep Parhar 	{0x6810, "Chelsio T6-DBG-100 VF"},	/* 2 x 40/50/100G, debug */
11549c0beb6SNavdeep Parhar 	{0x6811, "Chelsio T6225-LL-CR VF"},	/* 2 x 10/25G */
11649c0beb6SNavdeep Parhar 	{0x6814, "Chelsio T61100-OCP-SO VF"},	/* 1 x 40/50/100G, nomem */
11749c0beb6SNavdeep Parhar 	{0x6815, "Chelsio T6201-BT VF"},	/* 2 x 1000BASE-T */
11849c0beb6SNavdeep Parhar 
11949c0beb6SNavdeep Parhar 	/* Custom */
12049c0beb6SNavdeep Parhar 	{0x6880, "Chelsio T6225 80 VF"},
12149c0beb6SNavdeep Parhar 	{0x6881, "Chelsio T62100 81 VF"},
1225877e649SNavdeep Parhar 	{0x6882, "Chelsio T6225-CR 82 VF"},
1235877e649SNavdeep Parhar 	{0x6883, "Chelsio T62100-CR 83 VF"},
1245877e649SNavdeep Parhar 	{0x6884, "Chelsio T64100-CR 84 VF"},
1255877e649SNavdeep Parhar 	{0x6885, "Chelsio T6240-SO 85 VF"},
1265877e649SNavdeep Parhar 	{0x6886, "Chelsio T6225-SO-CR 86 VF"},
1275877e649SNavdeep Parhar 	{0x6887, "Chelsio T6225-CR 87 VF"},
1286af45170SJohn Baldwin };
1296af45170SJohn Baldwin 
1306af45170SJohn Baldwin static d_ioctl_t t4vf_ioctl;
1316af45170SJohn Baldwin 
1326af45170SJohn Baldwin static struct cdevsw t4vf_cdevsw = {
1336af45170SJohn Baldwin        .d_version = D_VERSION,
1346af45170SJohn Baldwin        .d_ioctl = t4vf_ioctl,
1356af45170SJohn Baldwin        .d_name = "t4vf",
1366af45170SJohn Baldwin };
1376af45170SJohn Baldwin 
1386af45170SJohn Baldwin static int
1396af45170SJohn Baldwin t4vf_probe(device_t dev)
1406af45170SJohn Baldwin {
1416af45170SJohn Baldwin 	uint16_t d;
1426af45170SJohn Baldwin 	size_t i;
1436af45170SJohn Baldwin 
1446af45170SJohn Baldwin 	d = pci_get_device(dev);
1456af45170SJohn Baldwin 	for (i = 0; i < nitems(t4vf_pciids); i++) {
1466af45170SJohn Baldwin 		if (d == t4vf_pciids[i].device) {
1476af45170SJohn Baldwin 			device_set_desc(dev, t4vf_pciids[i].desc);
1486af45170SJohn Baldwin 			return (BUS_PROBE_DEFAULT);
1496af45170SJohn Baldwin 		}
1506af45170SJohn Baldwin 	}
1516af45170SJohn Baldwin 	return (ENXIO);
1526af45170SJohn Baldwin }
1536af45170SJohn Baldwin 
1546af45170SJohn Baldwin static int
1556af45170SJohn Baldwin t5vf_probe(device_t dev)
1566af45170SJohn Baldwin {
1576af45170SJohn Baldwin 	uint16_t d;
1586af45170SJohn Baldwin 	size_t i;
1596af45170SJohn Baldwin 
1606af45170SJohn Baldwin 	d = pci_get_device(dev);
1616af45170SJohn Baldwin 	for (i = 0; i < nitems(t5vf_pciids); i++) {
1626af45170SJohn Baldwin 		if (d == t5vf_pciids[i].device) {
1636af45170SJohn Baldwin 			device_set_desc(dev, t5vf_pciids[i].desc);
1646af45170SJohn Baldwin 			return (BUS_PROBE_DEFAULT);
1656af45170SJohn Baldwin 		}
1666af45170SJohn Baldwin 	}
1676af45170SJohn Baldwin 	return (ENXIO);
1686af45170SJohn Baldwin }
1696af45170SJohn Baldwin 
170e6b81479SNavdeep Parhar static int
171e6b81479SNavdeep Parhar t6vf_probe(device_t dev)
172e6b81479SNavdeep Parhar {
173e6b81479SNavdeep Parhar 	uint16_t d;
174e6b81479SNavdeep Parhar 	size_t i;
175e6b81479SNavdeep Parhar 
176e6b81479SNavdeep Parhar 	d = pci_get_device(dev);
177e6b81479SNavdeep Parhar 	for (i = 0; i < nitems(t6vf_pciids); i++) {
178e6b81479SNavdeep Parhar 		if (d == t6vf_pciids[i].device) {
179e6b81479SNavdeep Parhar 			device_set_desc(dev, t6vf_pciids[i].desc);
180e6b81479SNavdeep Parhar 			return (BUS_PROBE_DEFAULT);
181e6b81479SNavdeep Parhar 		}
182e6b81479SNavdeep Parhar 	}
183e6b81479SNavdeep Parhar 	return (ENXIO);
184e6b81479SNavdeep Parhar }
185e6b81479SNavdeep Parhar 
1866af45170SJohn Baldwin #define FW_PARAM_DEV(param) \
1876af45170SJohn Baldwin 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
1886af45170SJohn Baldwin 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
1896af45170SJohn Baldwin #define FW_PARAM_PFVF(param) \
1906af45170SJohn Baldwin 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
1916af45170SJohn Baldwin 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
1926af45170SJohn Baldwin 
1936af45170SJohn Baldwin static int
1946af45170SJohn Baldwin get_params__pre_init(struct adapter *sc)
1956af45170SJohn Baldwin {
1966af45170SJohn Baldwin 	int rc;
1976af45170SJohn Baldwin 	uint32_t param[3], val[3];
1986af45170SJohn Baldwin 
1996af45170SJohn Baldwin 	param[0] = FW_PARAM_DEV(FWREV);
2006af45170SJohn Baldwin 	param[1] = FW_PARAM_DEV(TPREV);
2016af45170SJohn Baldwin 	param[2] = FW_PARAM_DEV(CCLK);
2026af45170SJohn Baldwin 	rc = -t4vf_query_params(sc, nitems(param), param, val);
2036af45170SJohn Baldwin 	if (rc != 0) {
2046af45170SJohn Baldwin 		device_printf(sc->dev,
2056af45170SJohn Baldwin 		    "failed to query parameters (pre_init): %d.\n", rc);
2066af45170SJohn Baldwin 		return (rc);
2076af45170SJohn Baldwin 	}
2086af45170SJohn Baldwin 
2096af45170SJohn Baldwin 	sc->params.fw_vers = val[0];
2106af45170SJohn Baldwin 	sc->params.tp_vers = val[1];
2116af45170SJohn Baldwin 	sc->params.vpd.cclk = val[2];
2126af45170SJohn Baldwin 
2136af45170SJohn Baldwin 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
2146af45170SJohn Baldwin 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
2156af45170SJohn Baldwin 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
2166af45170SJohn Baldwin 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
2176af45170SJohn Baldwin 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
2186af45170SJohn Baldwin 
2196af45170SJohn Baldwin 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
2206af45170SJohn Baldwin 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
2216af45170SJohn Baldwin 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
2226af45170SJohn Baldwin 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
2236af45170SJohn Baldwin 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
2246af45170SJohn Baldwin 
2256af45170SJohn Baldwin 	return (0);
2266af45170SJohn Baldwin }
2276af45170SJohn Baldwin 
2286af45170SJohn Baldwin static int
2296af45170SJohn Baldwin get_params__post_init(struct adapter *sc)
2306af45170SJohn Baldwin {
2316af45170SJohn Baldwin 	int rc;
232d735920dSNavdeep Parhar 	uint32_t param, val;
2336af45170SJohn Baldwin 
2346af45170SJohn Baldwin 	rc = -t4vf_get_sge_params(sc);
2356af45170SJohn Baldwin 	if (rc != 0) {
2366af45170SJohn Baldwin 		device_printf(sc->dev,
2376af45170SJohn Baldwin 		    "unable to retrieve adapter SGE parameters: %d\n", rc);
2386af45170SJohn Baldwin 		return (rc);
2396af45170SJohn Baldwin 	}
2406af45170SJohn Baldwin 
2416af45170SJohn Baldwin 	rc = -t4vf_get_rss_glb_config(sc);
2426af45170SJohn Baldwin 	if (rc != 0) {
2436af45170SJohn Baldwin 		device_printf(sc->dev,
2446af45170SJohn Baldwin 		    "unable to retrieve adapter RSS parameters: %d\n", rc);
2456af45170SJohn Baldwin 		return (rc);
2466af45170SJohn Baldwin 	}
2476af45170SJohn Baldwin 	if (sc->params.rss.mode != FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL) {
2486af45170SJohn Baldwin 		device_printf(sc->dev,
2496af45170SJohn Baldwin 		    "unable to operate with global RSS mode %d\n",
2506af45170SJohn Baldwin 		    sc->params.rss.mode);
2516af45170SJohn Baldwin 		return (EINVAL);
2526af45170SJohn Baldwin 	}
2536af45170SJohn Baldwin 
2546af45170SJohn Baldwin 	/*
2556af45170SJohn Baldwin 	 * Grab our Virtual Interface resource allocation, extract the
2566af45170SJohn Baldwin 	 * features that we're interested in and do a bit of sanity testing on
2576af45170SJohn Baldwin 	 * what we discover.
2586af45170SJohn Baldwin 	 */
2596af45170SJohn Baldwin 	rc = -t4vf_get_vfres(sc);
2606af45170SJohn Baldwin 	if (rc != 0) {
2616af45170SJohn Baldwin 		device_printf(sc->dev,
2626af45170SJohn Baldwin 		    "unable to get virtual interface resources: %d\n", rc);
2636af45170SJohn Baldwin 		return (rc);
2646af45170SJohn Baldwin 	}
2656af45170SJohn Baldwin 
2666af45170SJohn Baldwin 	/*
2676af45170SJohn Baldwin 	 * Check for various parameter sanity issues.
2686af45170SJohn Baldwin 	 */
2696af45170SJohn Baldwin 	if (sc->params.vfres.pmask == 0) {
2706af45170SJohn Baldwin 		device_printf(sc->dev, "no port access configured/usable!\n");
2716af45170SJohn Baldwin 		return (EINVAL);
2726af45170SJohn Baldwin 	}
2736af45170SJohn Baldwin 	if (sc->params.vfres.nvi == 0) {
2746af45170SJohn Baldwin 		device_printf(sc->dev,
2756af45170SJohn Baldwin 		    "no virtual interfaces configured/usable!\n");
2766af45170SJohn Baldwin 		return (EINVAL);
2776af45170SJohn Baldwin 	}
2786af45170SJohn Baldwin 	sc->params.portvec = sc->params.vfres.pmask;
2796af45170SJohn Baldwin 
280d735920dSNavdeep Parhar 	param = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
281d735920dSNavdeep Parhar 	rc = -t4vf_query_params(sc, 1, &param, &val);
282d735920dSNavdeep Parhar 	if (rc == 0)
283d735920dSNavdeep Parhar 		sc->params.max_pkts_per_eth_tx_pkts_wr = val;
284d735920dSNavdeep Parhar 	else
285d735920dSNavdeep Parhar 		sc->params.max_pkts_per_eth_tx_pkts_wr = 14;
286d735920dSNavdeep Parhar 
287fae028ddSNavdeep Parhar 	rc = t4_verify_chip_settings(sc);
288fae028ddSNavdeep Parhar 	if (rc != 0)
289fae028ddSNavdeep Parhar 		return (rc);
290fae028ddSNavdeep Parhar 	t4_init_rx_buf_info(sc);
291fae028ddSNavdeep Parhar 
2926af45170SJohn Baldwin 	return (0);
2936af45170SJohn Baldwin }
2946af45170SJohn Baldwin 
2956af45170SJohn Baldwin static int
2966af45170SJohn Baldwin set_params__post_init(struct adapter *sc)
2976af45170SJohn Baldwin {
2986af45170SJohn Baldwin 	uint32_t param, val;
2996af45170SJohn Baldwin 
3006af45170SJohn Baldwin 	/* ask for encapsulated CPLs */
3016af45170SJohn Baldwin 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3026af45170SJohn Baldwin 	val = 1;
3036af45170SJohn Baldwin 	(void)t4vf_set_params(sc, 1, &param, &val);
3046af45170SJohn Baldwin 
305b43e2d7dSNavdeep Parhar 	/* Enable 32b port caps if the firmware supports it. */
306b43e2d7dSNavdeep Parhar 	param = FW_PARAM_PFVF(PORT_CAPS32);
307b43e2d7dSNavdeep Parhar 	val = 1;
308b43e2d7dSNavdeep Parhar 	if (t4vf_set_params(sc, 1, &param, &val) == 0)
309b43e2d7dSNavdeep Parhar 		sc->params.port_caps32 = 1;
310b43e2d7dSNavdeep Parhar 
3116af45170SJohn Baldwin 	return (0);
3126af45170SJohn Baldwin }
3136af45170SJohn Baldwin 
3146af45170SJohn Baldwin #undef FW_PARAM_PFVF
3156af45170SJohn Baldwin #undef FW_PARAM_DEV
3166af45170SJohn Baldwin 
3176af45170SJohn Baldwin static int
3188c61c6bbSNavdeep Parhar cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
3196af45170SJohn Baldwin {
3206af45170SJohn Baldwin 	struct vf_resources *vfres;
3218c61c6bbSNavdeep Parhar 	int nrxq, ntxq, nports;
3226af45170SJohn Baldwin 	int itype, iq_avail, navail, rc;
3236af45170SJohn Baldwin 
3246af45170SJohn Baldwin 	/*
3256af45170SJohn Baldwin 	 * Figure out the layout of queues across our VIs and ensure
3266af45170SJohn Baldwin 	 * we can allocate enough interrupts for our layout.
3276af45170SJohn Baldwin 	 */
3286af45170SJohn Baldwin 	vfres = &sc->params.vfres;
3298c61c6bbSNavdeep Parhar 	nports = sc->params.nports;
3306af45170SJohn Baldwin 	bzero(iaq, sizeof(*iaq));
3316af45170SJohn Baldwin 
3326af45170SJohn Baldwin 	for (itype = INTR_MSIX; itype != 0; itype >>= 1) {
3336af45170SJohn Baldwin 		if (itype == INTR_INTX)
3346af45170SJohn Baldwin 			continue;
3356af45170SJohn Baldwin 
3366af45170SJohn Baldwin 		if (itype == INTR_MSIX)
3376af45170SJohn Baldwin 			navail = pci_msix_count(sc->dev);
3386af45170SJohn Baldwin 		else
3396af45170SJohn Baldwin 			navail = pci_msi_count(sc->dev);
3406af45170SJohn Baldwin 
3416af45170SJohn Baldwin 		if (navail == 0)
3426af45170SJohn Baldwin 			continue;
3436af45170SJohn Baldwin 
3446af45170SJohn Baldwin 		iaq->intr_type = itype;
3456af45170SJohn Baldwin 
3466af45170SJohn Baldwin 		/*
3476af45170SJohn Baldwin 		 * XXX: The Linux driver reserves an Ingress Queue for
3486af45170SJohn Baldwin 		 * forwarded interrupts when using MSI (but not MSI-X).
3496af45170SJohn Baldwin 		 * It seems it just always asks for 2 interrupts and
3506af45170SJohn Baldwin 		 * forwards all rxqs to the forwarded interrupt.
3516af45170SJohn Baldwin 		 *
3526af45170SJohn Baldwin 		 * We must reserve one IRQ for the for the firmware
3536af45170SJohn Baldwin 		 * event queue.
3546af45170SJohn Baldwin 		 *
3556af45170SJohn Baldwin 		 * Every rxq requires an ingress queue with a free
3566af45170SJohn Baldwin 		 * list and interrupts and an egress queue.  Every txq
3576af45170SJohn Baldwin 		 * requires an ETH egress queue.
3586af45170SJohn Baldwin 		 */
3596af45170SJohn Baldwin 		iaq->nirq = T4VF_EXTRA_INTR;
3606af45170SJohn Baldwin 
3616af45170SJohn Baldwin 		/*
3626af45170SJohn Baldwin 		 * First, determine how many queues we can allocate.
3636af45170SJohn Baldwin 		 * Start by finding the upper bound on rxqs from the
3646af45170SJohn Baldwin 		 * limit on ingress queues.
3656af45170SJohn Baldwin 		 */
3666af45170SJohn Baldwin 		iq_avail = vfres->niqflint - iaq->nirq;
3678c61c6bbSNavdeep Parhar 		if (iq_avail < nports) {
3686af45170SJohn Baldwin 			device_printf(sc->dev,
3696af45170SJohn Baldwin 			    "Not enough ingress queues (%d) for %d ports\n",
3708c61c6bbSNavdeep Parhar 			    vfres->niqflint, nports);
3716af45170SJohn Baldwin 			return (ENXIO);
3726af45170SJohn Baldwin 		}
3736af45170SJohn Baldwin 
3746af45170SJohn Baldwin 		/*
3756af45170SJohn Baldwin 		 * Try to honor the cap on interrupts.  If there aren't
3766af45170SJohn Baldwin 		 * enough interrupts for at least one interrupt per
3776af45170SJohn Baldwin 		 * port, then don't bother, we will just forward all
3786af45170SJohn Baldwin 		 * interrupts to one interrupt in that case.
3796af45170SJohn Baldwin 		 */
3808c61c6bbSNavdeep Parhar 		if (iaq->nirq + nports <= navail) {
3816af45170SJohn Baldwin 			if (iq_avail > navail - iaq->nirq)
3826af45170SJohn Baldwin 				iq_avail = navail - iaq->nirq;
3836af45170SJohn Baldwin 		}
3846af45170SJohn Baldwin 
3858c61c6bbSNavdeep Parhar 		nrxq = nports * t4_nrxq;
3866af45170SJohn Baldwin 		if (nrxq > iq_avail) {
3876af45170SJohn Baldwin 			/*
3888c61c6bbSNavdeep Parhar 			 * Too many ingress queues.  Use what we can.
3896af45170SJohn Baldwin 			 */
3908c61c6bbSNavdeep Parhar 			nrxq = (iq_avail / nports) * nports;
3916af45170SJohn Baldwin 		}
3926af45170SJohn Baldwin 		KASSERT(nrxq <= iq_avail, ("too many ingress queues"));
3936af45170SJohn Baldwin 
3946af45170SJohn Baldwin 		/*
3956af45170SJohn Baldwin 		 * Next, determine the upper bound on txqs from the limit
3966af45170SJohn Baldwin 		 * on ETH queues.
3976af45170SJohn Baldwin 		 */
3988c61c6bbSNavdeep Parhar 		if (vfres->nethctrl < nports) {
3996af45170SJohn Baldwin 			device_printf(sc->dev,
4006af45170SJohn Baldwin 			    "Not enough ETH queues (%d) for %d ports\n",
4018c61c6bbSNavdeep Parhar 			    vfres->nethctrl, nports);
4026af45170SJohn Baldwin 			return (ENXIO);
4036af45170SJohn Baldwin 		}
4046af45170SJohn Baldwin 
4058c61c6bbSNavdeep Parhar 		ntxq = nports * t4_ntxq;
4066af45170SJohn Baldwin 		if (ntxq > vfres->nethctrl) {
4076af45170SJohn Baldwin 			/*
4088c61c6bbSNavdeep Parhar 			 * Too many ETH queues.  Use what we can.
4096af45170SJohn Baldwin 			 */
4108c61c6bbSNavdeep Parhar 			ntxq = (vfres->nethctrl / nports) * nports;
4116af45170SJohn Baldwin 		}
4126af45170SJohn Baldwin 		KASSERT(ntxq <= vfres->nethctrl, ("too many ETH queues"));
4136af45170SJohn Baldwin 
4146af45170SJohn Baldwin 		/*
4156af45170SJohn Baldwin 		 * Finally, ensure we have enough egress queues.
4166af45170SJohn Baldwin 		 */
4178c61c6bbSNavdeep Parhar 		if (vfres->neq < nports * 2) {
4186af45170SJohn Baldwin 			device_printf(sc->dev,
4196af45170SJohn Baldwin 			    "Not enough egress queues (%d) for %d ports\n",
4208c61c6bbSNavdeep Parhar 			    vfres->neq, nports);
4216af45170SJohn Baldwin 			return (ENXIO);
4226af45170SJohn Baldwin 		}
4236af45170SJohn Baldwin 		if (nrxq + ntxq > vfres->neq) {
4246af45170SJohn Baldwin 			/* Just punt and use 1 for everything. */
4258c61c6bbSNavdeep Parhar 			nrxq = ntxq = nports;
4266af45170SJohn Baldwin 		}
4276af45170SJohn Baldwin 		KASSERT(nrxq <= iq_avail, ("too many ingress queues"));
4286af45170SJohn Baldwin 		KASSERT(ntxq <= vfres->nethctrl, ("too many ETH queues"));
4296af45170SJohn Baldwin 		KASSERT(nrxq + ntxq <= vfres->neq, ("too many egress queues"));
4306af45170SJohn Baldwin 
4316af45170SJohn Baldwin 		/*
4326af45170SJohn Baldwin 		 * Do we have enough interrupts?  For MSI the interrupts
4336af45170SJohn Baldwin 		 * have to be a power of 2 as well.
4346af45170SJohn Baldwin 		 */
4356af45170SJohn Baldwin 		iaq->nirq += nrxq;
4368c61c6bbSNavdeep Parhar 		iaq->ntxq = ntxq;
4378c61c6bbSNavdeep Parhar 		iaq->nrxq = nrxq;
4386af45170SJohn Baldwin 		if (iaq->nirq <= navail &&
4396af45170SJohn Baldwin 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4406af45170SJohn Baldwin 			navail = iaq->nirq;
4416af45170SJohn Baldwin 			if (itype == INTR_MSIX)
4426af45170SJohn Baldwin 				rc = pci_alloc_msix(sc->dev, &navail);
4436af45170SJohn Baldwin 			else
4446af45170SJohn Baldwin 				rc = pci_alloc_msi(sc->dev, &navail);
4456af45170SJohn Baldwin 			if (rc != 0) {
4466af45170SJohn Baldwin 				device_printf(sc->dev,
4476af45170SJohn Baldwin 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4486af45170SJohn Baldwin 				    itype, rc, iaq->nirq, navail);
4496af45170SJohn Baldwin 				return (rc);
4506af45170SJohn Baldwin 			}
4516af45170SJohn Baldwin 			if (navail == iaq->nirq) {
4526af45170SJohn Baldwin 				return (0);
4536af45170SJohn Baldwin 			}
4546af45170SJohn Baldwin 			pci_release_msi(sc->dev);
4556af45170SJohn Baldwin 		}
4566af45170SJohn Baldwin 
4576af45170SJohn Baldwin 		/* Fall back to a single interrupt. */
4586af45170SJohn Baldwin 		iaq->nirq = 1;
4596af45170SJohn Baldwin 		navail = iaq->nirq;
4606af45170SJohn Baldwin 		if (itype == INTR_MSIX)
4616af45170SJohn Baldwin 			rc = pci_alloc_msix(sc->dev, &navail);
4626af45170SJohn Baldwin 		else
4636af45170SJohn Baldwin 			rc = pci_alloc_msi(sc->dev, &navail);
4646af45170SJohn Baldwin 		if (rc != 0)
4656af45170SJohn Baldwin 			device_printf(sc->dev,
4666af45170SJohn Baldwin 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4676af45170SJohn Baldwin 			    itype, rc, iaq->nirq, navail);
4686af45170SJohn Baldwin 		return (rc);
4696af45170SJohn Baldwin 	}
4706af45170SJohn Baldwin 
4716af45170SJohn Baldwin 	device_printf(sc->dev,
4726af45170SJohn Baldwin 	    "failed to find a usable interrupt type.  "
4736af45170SJohn Baldwin 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4746af45170SJohn Baldwin 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4756af45170SJohn Baldwin 
4766af45170SJohn Baldwin 	return (ENXIO);
4776af45170SJohn Baldwin }
4786af45170SJohn Baldwin 
4796af45170SJohn Baldwin static int
4806af45170SJohn Baldwin t4vf_attach(device_t dev)
4816af45170SJohn Baldwin {
4826af45170SJohn Baldwin 	struct adapter *sc;
483dc0800a9SNavdeep Parhar 	int rc = 0, i, j, rqidx, tqidx, n, p, pmask;
4846af45170SJohn Baldwin 	struct make_dev_args mda;
4856af45170SJohn Baldwin 	struct intrs_and_queues iaq;
4866af45170SJohn Baldwin 	struct sge *s;
4876af45170SJohn Baldwin 
4886af45170SJohn Baldwin 	sc = device_get_softc(dev);
4896af45170SJohn Baldwin 	sc->dev = dev;
490a727d953SNavdeep Parhar 	sysctl_ctx_init(&sc->ctx);
4916af45170SJohn Baldwin 	pci_enable_busmaster(dev);
4926af45170SJohn Baldwin 	pci_set_max_read_req(dev, 4096);
4936af45170SJohn Baldwin 	sc->params.pci.mps = pci_get_max_payload(dev);
4946af45170SJohn Baldwin 
4956af45170SJohn Baldwin 	sc->flags |= IS_VF;
496c0a248efSNavdeep Parhar 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
4976af45170SJohn Baldwin 
4986af45170SJohn Baldwin 	sc->sge_gts_reg = VF_SGE_REG(A_SGE_VF_GTS);
4996af45170SJohn Baldwin 	sc->sge_kdoorbell_reg = VF_SGE_REG(A_SGE_VF_KDOORBELL);
5006af45170SJohn Baldwin 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
5016af45170SJohn Baldwin 	    device_get_nameunit(dev));
5026af45170SJohn Baldwin 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
5036af45170SJohn Baldwin 	t4_add_adapter(sc);
5046af45170SJohn Baldwin 
5056af45170SJohn Baldwin 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
5066af45170SJohn Baldwin 	TAILQ_INIT(&sc->sfl);
5076af45170SJohn Baldwin 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
5086af45170SJohn Baldwin 
5096af45170SJohn Baldwin 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
5106af45170SJohn Baldwin 
5116af45170SJohn Baldwin 	rc = t4_map_bars_0_and_4(sc);
5126af45170SJohn Baldwin 	if (rc != 0)
5136af45170SJohn Baldwin 		goto done; /* error message displayed already */
5146af45170SJohn Baldwin 
5156af45170SJohn Baldwin 	rc = -t4vf_prep_adapter(sc);
5166af45170SJohn Baldwin 	if (rc != 0)
5176af45170SJohn Baldwin 		goto done;
5186af45170SJohn Baldwin 
519e6b81479SNavdeep Parhar 	t4_init_devnames(sc);
520e6b81479SNavdeep Parhar 	if (sc->names == NULL) {
521e6b81479SNavdeep Parhar 		rc = ENOTSUP;
522e6b81479SNavdeep Parhar 		goto done; /* error message displayed already */
523e6b81479SNavdeep Parhar 	}
524e6b81479SNavdeep Parhar 
5256af45170SJohn Baldwin 	/*
5266af45170SJohn Baldwin 	 * Leave the 'pf' and 'mbox' values as zero.  This ensures
5276af45170SJohn Baldwin 	 * that various firmware messages do not set the fields which
5286af45170SJohn Baldwin 	 * is the correct thing to do for a VF.
5296af45170SJohn Baldwin 	 */
5306af45170SJohn Baldwin 
5316af45170SJohn Baldwin 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
5326af45170SJohn Baldwin 
5336af45170SJohn Baldwin 	make_dev_args_init(&mda);
5346af45170SJohn Baldwin 	mda.mda_devsw = &t4vf_cdevsw;
5356af45170SJohn Baldwin 	mda.mda_uid = UID_ROOT;
5366af45170SJohn Baldwin 	mda.mda_gid = GID_WHEEL;
5376af45170SJohn Baldwin 	mda.mda_mode = 0600;
5386af45170SJohn Baldwin 	mda.mda_si_drv1 = sc;
5396af45170SJohn Baldwin 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
5406af45170SJohn Baldwin 	if (rc != 0)
5416af45170SJohn Baldwin 		device_printf(dev, "failed to create nexus char device: %d.\n",
5426af45170SJohn Baldwin 		    rc);
5436af45170SJohn Baldwin 
5446af45170SJohn Baldwin #if defined(__i386__)
5456af45170SJohn Baldwin 	if ((cpu_feature & CPUID_CX8) == 0) {
5466af45170SJohn Baldwin 		device_printf(dev, "64 bit atomics not available.\n");
5476af45170SJohn Baldwin 		rc = ENOTSUP;
5486af45170SJohn Baldwin 		goto done;
5496af45170SJohn Baldwin 	}
5506af45170SJohn Baldwin #endif
5516af45170SJohn Baldwin 
5526af45170SJohn Baldwin 	/*
5536af45170SJohn Baldwin 	 * Some environments do not properly handle PCIE FLRs -- e.g. in Linux
5546af45170SJohn Baldwin 	 * 2.6.31 and later we can't call pci_reset_function() in order to
5556af45170SJohn Baldwin 	 * issue an FLR because of a self- deadlock on the device semaphore.
5566af45170SJohn Baldwin 	 * Meanwhile, the OS infrastructure doesn't issue FLRs in all the
5576af45170SJohn Baldwin 	 * cases where they're needed -- for instance, some versions of KVM
5586af45170SJohn Baldwin 	 * fail to reset "Assigned Devices" when the VM reboots.  Therefore we
5596af45170SJohn Baldwin 	 * use the firmware based reset in order to reset any per function
5606af45170SJohn Baldwin 	 * state.
5616af45170SJohn Baldwin 	 */
5626af45170SJohn Baldwin 	rc = -t4vf_fw_reset(sc);
5636af45170SJohn Baldwin 	if (rc != 0) {
5646af45170SJohn Baldwin 		device_printf(dev, "FW reset failed: %d\n", rc);
5656af45170SJohn Baldwin 		goto done;
5666af45170SJohn Baldwin 	}
5676af45170SJohn Baldwin 	sc->flags |= FW_OK;
5686af45170SJohn Baldwin 
5696af45170SJohn Baldwin 	/*
5706af45170SJohn Baldwin 	 * Grab basic operational parameters.  These will predominantly have
5716af45170SJohn Baldwin 	 * been set up by the Physical Function Driver or will be hard coded
5726af45170SJohn Baldwin 	 * into the adapter.  We just have to live with them ...  Note that
5736af45170SJohn Baldwin 	 * we _must_ get our VPD parameters before our SGE parameters because
5746af45170SJohn Baldwin 	 * we need to know the adapter's core clock from the VPD in order to
5756af45170SJohn Baldwin 	 * properly decode the SGE Timer Values.
5766af45170SJohn Baldwin 	 */
5776af45170SJohn Baldwin 	rc = get_params__pre_init(sc);
5786af45170SJohn Baldwin 	if (rc != 0)
5796af45170SJohn Baldwin 		goto done; /* error message displayed already */
5806af45170SJohn Baldwin 	rc = get_params__post_init(sc);
5816af45170SJohn Baldwin 	if (rc != 0)
5826af45170SJohn Baldwin 		goto done; /* error message displayed already */
5836af45170SJohn Baldwin 
5846af45170SJohn Baldwin 	rc = set_params__post_init(sc);
5856af45170SJohn Baldwin 	if (rc != 0)
5866af45170SJohn Baldwin 		goto done; /* error message displayed already */
5876af45170SJohn Baldwin 
5886af45170SJohn Baldwin 	rc = t4_map_bar_2(sc);
5896af45170SJohn Baldwin 	if (rc != 0)
5906af45170SJohn Baldwin 		goto done; /* error message displayed already */
5916af45170SJohn Baldwin 
592ba95b4aeSNavdeep Parhar 	rc = t4_adj_doorbells(sc);
593ba95b4aeSNavdeep Parhar 	if (rc != 0)
594ba95b4aeSNavdeep Parhar 		goto done; /* error message displayed already */
595ba95b4aeSNavdeep Parhar 
5966af45170SJohn Baldwin 	rc = t4_create_dma_tag(sc);
5976af45170SJohn Baldwin 	if (rc != 0)
5986af45170SJohn Baldwin 		goto done; /* error message displayed already */
5996af45170SJohn Baldwin 
6006af45170SJohn Baldwin 	/*
6016af45170SJohn Baldwin 	 * The number of "ports" which we support is equal to the number of
6026af45170SJohn Baldwin 	 * Virtual Interfaces with which we've been provisioned.
6036af45170SJohn Baldwin 	 */
6046af45170SJohn Baldwin 	sc->params.nports = imin(sc->params.vfres.nvi, MAX_NPORTS);
6056af45170SJohn Baldwin 
6066af45170SJohn Baldwin 	/*
6076af45170SJohn Baldwin 	 * We may have been provisioned with more VIs than the number of
6086af45170SJohn Baldwin 	 * ports we're allowed to access (our Port Access Rights Mask).
6096af45170SJohn Baldwin 	 * Just use a single VI for each port.
6106af45170SJohn Baldwin 	 */
6116af45170SJohn Baldwin 	sc->params.nports = imin(sc->params.nports,
6126af45170SJohn Baldwin 	    bitcount32(sc->params.vfres.pmask));
6136af45170SJohn Baldwin 
6146af45170SJohn Baldwin #ifdef notyet
6156af45170SJohn Baldwin 	/*
6166af45170SJohn Baldwin 	 * XXX: The Linux VF driver will lower nports if it thinks there
6176af45170SJohn Baldwin 	 * are too few resources in vfres (niqflint, nethctrl, neq).
6186af45170SJohn Baldwin 	 */
6196af45170SJohn Baldwin #endif
6206af45170SJohn Baldwin 
6216af45170SJohn Baldwin 	/*
6226af45170SJohn Baldwin 	 * First pass over all the ports - allocate VIs and initialize some
6238c61c6bbSNavdeep Parhar 	 * basic parameters like mac address, port type, etc.
6246af45170SJohn Baldwin 	 */
625dc0800a9SNavdeep Parhar 	pmask = sc->params.vfres.pmask;
6266af45170SJohn Baldwin 	for_each_port(sc, i) {
6276af45170SJohn Baldwin 		struct port_info *pi;
628dc0800a9SNavdeep Parhar 		uint8_t mac[ETHER_ADDR_LEN];
6296af45170SJohn Baldwin 
6306af45170SJohn Baldwin 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
6316af45170SJohn Baldwin 		sc->port[i] = pi;
6326af45170SJohn Baldwin 
6336af45170SJohn Baldwin 		/* These must be set before t4_port_init */
6346af45170SJohn Baldwin 		pi->adapter = sc;
6356af45170SJohn Baldwin 		pi->port_id = i;
6366af45170SJohn Baldwin 		pi->nvi = 1;
6376af45170SJohn Baldwin 		pi->vi = malloc(sizeof(struct vi_info) * pi->nvi, M_CXGBE,
6386af45170SJohn Baldwin 		    M_ZERO | M_WAITOK);
6396af45170SJohn Baldwin 
6406af45170SJohn Baldwin 		/*
6416af45170SJohn Baldwin 		 * Allocate the "main" VI and initialize parameters
6426af45170SJohn Baldwin 		 * like mac addr.
6436af45170SJohn Baldwin 		 */
6446af45170SJohn Baldwin 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
6456af45170SJohn Baldwin 		if (rc != 0) {
6466af45170SJohn Baldwin 			device_printf(dev, "unable to initialize port %d: %d\n",
6476af45170SJohn Baldwin 			    i, rc);
6486af45170SJohn Baldwin 			free(pi->vi, M_CXGBE);
6496af45170SJohn Baldwin 			free(pi, M_CXGBE);
6506af45170SJohn Baldwin 			sc->port[i] = NULL;
6516af45170SJohn Baldwin 			goto done;
6526af45170SJohn Baldwin 		}
6536af45170SJohn Baldwin 
654dc0800a9SNavdeep Parhar 		/* Prefer the MAC address set by the PF, if there is one. */
655dc0800a9SNavdeep Parhar 		n = 1;
656dc0800a9SNavdeep Parhar 		p = ffs(pmask) - 1;
657dc0800a9SNavdeep Parhar 		MPASS(p >= 0);
658dc0800a9SNavdeep Parhar 		rc = t4vf_get_vf_mac(sc, p, &n, mac);
659dc0800a9SNavdeep Parhar 		if (rc == 0 && n == 1)
660dc0800a9SNavdeep Parhar 			t4_os_set_hw_addr(pi, mac);
661dc0800a9SNavdeep Parhar 		pmask &= ~(1 << p);
662dc0800a9SNavdeep Parhar 
6632d0a0127SNavdeep Parhar 		sc->vlan_id = t4vf_get_vf_vlan(sc);
6642d0a0127SNavdeep Parhar 
6656af45170SJohn Baldwin 		/* No t4_link_start. */
6666af45170SJohn Baldwin 
6676af45170SJohn Baldwin 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
6686af45170SJohn Baldwin 		    device_get_nameunit(dev), i);
6696af45170SJohn Baldwin 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
6706af45170SJohn Baldwin 		sc->chan_map[pi->tx_chan] = i;
6716af45170SJohn Baldwin 
672b8bfcb71SNavdeep Parhar 		/* All VIs on this port share this media. */
673b8bfcb71SNavdeep Parhar 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
674b8bfcb71SNavdeep Parhar 		    cxgbe_media_status);
675b8bfcb71SNavdeep Parhar 
676*5b56413dSWarner Losh 		pi->dev = device_add_child(dev, sc->names->vf_ifnet_name, DEVICE_UNIT_ANY);
6776af45170SJohn Baldwin 		if (pi->dev == NULL) {
6786af45170SJohn Baldwin 			device_printf(dev,
6796af45170SJohn Baldwin 			    "failed to add device for port %d.\n", i);
6806af45170SJohn Baldwin 			rc = ENXIO;
6816af45170SJohn Baldwin 			goto done;
6826af45170SJohn Baldwin 		}
6836af45170SJohn Baldwin 		pi->vi[0].dev = pi->dev;
6846af45170SJohn Baldwin 		device_set_softc(pi->dev, pi);
6856af45170SJohn Baldwin 	}
6866af45170SJohn Baldwin 
6876af45170SJohn Baldwin 	/*
6886af45170SJohn Baldwin 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
6896af45170SJohn Baldwin 	 */
6908c61c6bbSNavdeep Parhar 	rc = cfg_itype_and_nqueues(sc, &iaq);
6916af45170SJohn Baldwin 	if (rc != 0)
6926af45170SJohn Baldwin 		goto done; /* error message displayed already */
6936af45170SJohn Baldwin 
6946af45170SJohn Baldwin 	sc->intr_type = iaq.intr_type;
6956af45170SJohn Baldwin 	sc->intr_count = iaq.nirq;
6966af45170SJohn Baldwin 
6976af45170SJohn Baldwin 	s = &sc->sge;
6988c61c6bbSNavdeep Parhar 	s->nrxq = sc->params.nports * iaq.nrxq;
6998c61c6bbSNavdeep Parhar 	s->ntxq = sc->params.nports * iaq.ntxq;
7006af45170SJohn Baldwin 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
70137310a98SNavdeep Parhar 	s->neq += sc->params.nports;	/* ctrl queues: 1 per port */
7026af45170SJohn Baldwin 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
7036af45170SJohn Baldwin 
704f14d7c95SNavdeep Parhar 	s->iqmap_sz = s->niq;
705f14d7c95SNavdeep Parhar 	s->eqmap_sz = s->neq;
706f14d7c95SNavdeep Parhar 
7076af45170SJohn Baldwin 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
7086af45170SJohn Baldwin 	    M_ZERO | M_WAITOK);
7096af45170SJohn Baldwin 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
7106af45170SJohn Baldwin 	    M_ZERO | M_WAITOK);
711f14d7c95SNavdeep Parhar 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
7126af45170SJohn Baldwin 	    M_ZERO | M_WAITOK);
713f14d7c95SNavdeep Parhar 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
7146af45170SJohn Baldwin 	    M_ZERO | M_WAITOK);
7156af45170SJohn Baldwin 
7166af45170SJohn Baldwin 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
7176af45170SJohn Baldwin 	    M_ZERO | M_WAITOK);
7186af45170SJohn Baldwin 
7196af45170SJohn Baldwin 	/*
7206af45170SJohn Baldwin 	 * Second pass over the ports.  This time we know the number of rx and
7216af45170SJohn Baldwin 	 * tx queues that each port should get.
7226af45170SJohn Baldwin 	 */
7236af45170SJohn Baldwin 	rqidx = tqidx = 0;
7246af45170SJohn Baldwin 	for_each_port(sc, i) {
7256af45170SJohn Baldwin 		struct port_info *pi = sc->port[i];
7266af45170SJohn Baldwin 		struct vi_info *vi;
7276af45170SJohn Baldwin 
7286af45170SJohn Baldwin 		if (pi == NULL)
7296af45170SJohn Baldwin 			continue;
7306af45170SJohn Baldwin 
7316af45170SJohn Baldwin 		for_each_vi(pi, j, vi) {
7326af45170SJohn Baldwin 			vi->pi = pi;
7337c228be3SNavdeep Parhar 			vi->adapter = sc;
7346af45170SJohn Baldwin 			vi->qsize_rxq = t4_qsize_rxq;
7356af45170SJohn Baldwin 			vi->qsize_txq = t4_qsize_txq;
7366af45170SJohn Baldwin 
7376af45170SJohn Baldwin 			vi->first_rxq = rqidx;
7386af45170SJohn Baldwin 			vi->first_txq = tqidx;
7398c61c6bbSNavdeep Parhar 			vi->tmr_idx = t4_tmr_idx;
7408c61c6bbSNavdeep Parhar 			vi->pktc_idx = t4_pktc_idx;
7418c61c6bbSNavdeep Parhar 			vi->nrxq = j == 0 ? iaq.nrxq: 1;
7428c61c6bbSNavdeep Parhar 			vi->ntxq = j == 0 ? iaq.ntxq: 1;
7438c61c6bbSNavdeep Parhar 
7446af45170SJohn Baldwin 			rqidx += vi->nrxq;
7456af45170SJohn Baldwin 			tqidx += vi->ntxq;
7466af45170SJohn Baldwin 
7476af45170SJohn Baldwin 			vi->rsrv_noflowq = 0;
7486af45170SJohn Baldwin 		}
7496af45170SJohn Baldwin 	}
7506af45170SJohn Baldwin 
7516af45170SJohn Baldwin 	rc = t4_setup_intr_handlers(sc);
7526af45170SJohn Baldwin 	if (rc != 0) {
7536af45170SJohn Baldwin 		device_printf(dev,
7546af45170SJohn Baldwin 		    "failed to setup interrupt handlers: %d\n", rc);
7556af45170SJohn Baldwin 		goto done;
7566af45170SJohn Baldwin 	}
7576af45170SJohn Baldwin 
7586af45170SJohn Baldwin 	rc = bus_generic_attach(dev);
7596af45170SJohn Baldwin 	if (rc != 0) {
7606af45170SJohn Baldwin 		device_printf(dev,
7616af45170SJohn Baldwin 		    "failed to attach all child ports: %d\n", rc);
7626af45170SJohn Baldwin 		goto done;
7636af45170SJohn Baldwin 	}
7646af45170SJohn Baldwin 
7656af45170SJohn Baldwin 	device_printf(dev,
7666af45170SJohn Baldwin 	    "%d ports, %d %s interrupt%s, %d eq, %d iq\n",
7676af45170SJohn Baldwin 	    sc->params.nports, sc->intr_count, sc->intr_type == INTR_MSIX ?
7686af45170SJohn Baldwin 	    "MSI-X" : "MSI", sc->intr_count > 1 ? "s" : "", sc->sge.neq,
7696af45170SJohn Baldwin 	    sc->sge.niq);
7706af45170SJohn Baldwin 
7716af45170SJohn Baldwin done:
7726af45170SJohn Baldwin 	if (rc != 0)
7736af45170SJohn Baldwin 		t4_detach_common(dev);
7746af45170SJohn Baldwin 	else
7756af45170SJohn Baldwin 		t4_sysctls(sc);
7766af45170SJohn Baldwin 
7776af45170SJohn Baldwin 	return (rc);
7786af45170SJohn Baldwin }
7796af45170SJohn Baldwin 
7806af45170SJohn Baldwin static void
7816af45170SJohn Baldwin get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7826af45170SJohn Baldwin {
7836af45170SJohn Baldwin 
7846af45170SJohn Baldwin 	/* 0x3f is used as the revision for VFs. */
7856af45170SJohn Baldwin 	regs->version = chip_id(sc) | (0x3f << 10);
7866af45170SJohn Baldwin 	t4_get_regs(sc, buf, regs->len);
7876af45170SJohn Baldwin }
7886af45170SJohn Baldwin 
7896af45170SJohn Baldwin static void
7906af45170SJohn Baldwin t4_clr_vi_stats(struct adapter *sc)
7916af45170SJohn Baldwin {
7926af45170SJohn Baldwin 	int reg;
7936af45170SJohn Baldwin 
7946af45170SJohn Baldwin 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7956af45170SJohn Baldwin 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7966af45170SJohn Baldwin 		t4_write_reg(sc, VF_MPS_REG(reg), 0);
7976af45170SJohn Baldwin }
7986af45170SJohn Baldwin 
7996af45170SJohn Baldwin static int
8006af45170SJohn Baldwin t4vf_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
8016af45170SJohn Baldwin     struct thread *td)
8026af45170SJohn Baldwin {
8036af45170SJohn Baldwin 	int rc;
8046af45170SJohn Baldwin 	struct adapter *sc = dev->si_drv1;
8056af45170SJohn Baldwin 
8066af45170SJohn Baldwin 	rc = priv_check(td, PRIV_DRIVER);
8076af45170SJohn Baldwin 	if (rc != 0)
8086af45170SJohn Baldwin 		return (rc);
8096af45170SJohn Baldwin 
8106af45170SJohn Baldwin 	switch (cmd) {
8116af45170SJohn Baldwin 	case CHELSIO_T4_GETREG: {
8126af45170SJohn Baldwin 		struct t4_reg *edata = (struct t4_reg *)data;
8136af45170SJohn Baldwin 
8146af45170SJohn Baldwin 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
8156af45170SJohn Baldwin 			return (EFAULT);
8166af45170SJohn Baldwin 
8176af45170SJohn Baldwin 		if (edata->size == 4)
8186af45170SJohn Baldwin 			edata->val = t4_read_reg(sc, edata->addr);
8196af45170SJohn Baldwin 		else if (edata->size == 8)
8206af45170SJohn Baldwin 			edata->val = t4_read_reg64(sc, edata->addr);
8216af45170SJohn Baldwin 		else
8226af45170SJohn Baldwin 			return (EINVAL);
8236af45170SJohn Baldwin 
8246af45170SJohn Baldwin 		break;
8256af45170SJohn Baldwin 	}
8266af45170SJohn Baldwin 	case CHELSIO_T4_SETREG: {
8276af45170SJohn Baldwin 		struct t4_reg *edata = (struct t4_reg *)data;
8286af45170SJohn Baldwin 
8296af45170SJohn Baldwin 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
8306af45170SJohn Baldwin 			return (EFAULT);
8316af45170SJohn Baldwin 
8326af45170SJohn Baldwin 		if (edata->size == 4) {
8336af45170SJohn Baldwin 			if (edata->val & 0xffffffff00000000)
8346af45170SJohn Baldwin 				return (EINVAL);
8356af45170SJohn Baldwin 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
8366af45170SJohn Baldwin 		} else if (edata->size == 8)
8376af45170SJohn Baldwin 			t4_write_reg64(sc, edata->addr, edata->val);
8386af45170SJohn Baldwin 		else
8396af45170SJohn Baldwin 			return (EINVAL);
8406af45170SJohn Baldwin 		break;
8416af45170SJohn Baldwin 	}
8426af45170SJohn Baldwin 	case CHELSIO_T4_REGDUMP: {
8436af45170SJohn Baldwin 		struct t4_regdump *regs = (struct t4_regdump *)data;
8446af45170SJohn Baldwin 		int reglen = t4_get_regs_len(sc);
8456af45170SJohn Baldwin 		uint8_t *buf;
8466af45170SJohn Baldwin 
8476af45170SJohn Baldwin 		if (regs->len < reglen) {
8486af45170SJohn Baldwin 			regs->len = reglen; /* hint to the caller */
8496af45170SJohn Baldwin 			return (ENOBUFS);
8506af45170SJohn Baldwin 		}
8516af45170SJohn Baldwin 
8526af45170SJohn Baldwin 		regs->len = reglen;
8536af45170SJohn Baldwin 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
8546af45170SJohn Baldwin 		get_regs(sc, regs, buf);
8556af45170SJohn Baldwin 		rc = copyout(buf, regs->data, reglen);
8566af45170SJohn Baldwin 		free(buf, M_CXGBE);
8576af45170SJohn Baldwin 		break;
8586af45170SJohn Baldwin 	}
8596af45170SJohn Baldwin 	case CHELSIO_T4_CLEAR_STATS: {
8606af45170SJohn Baldwin 		int i, v;
8616af45170SJohn Baldwin 		u_int port_id = *(uint32_t *)data;
8626af45170SJohn Baldwin 		struct port_info *pi;
8636af45170SJohn Baldwin 		struct vi_info *vi;
8646af45170SJohn Baldwin 
8656af45170SJohn Baldwin 		if (port_id >= sc->params.nports)
8666af45170SJohn Baldwin 			return (EINVAL);
8676af45170SJohn Baldwin 		pi = sc->port[port_id];
8686af45170SJohn Baldwin 
8696af45170SJohn Baldwin 		/* MAC stats */
8706af45170SJohn Baldwin 		pi->tx_parse_error = 0;
8716af45170SJohn Baldwin 		t4_clr_vi_stats(sc);
8726af45170SJohn Baldwin 
8736af45170SJohn Baldwin 		/*
8746af45170SJohn Baldwin 		 * Since this command accepts a port, clear stats for
8756af45170SJohn Baldwin 		 * all VIs on this port.
8766af45170SJohn Baldwin 		 */
8776af45170SJohn Baldwin 		for_each_vi(pi, v, vi) {
8786af45170SJohn Baldwin 			if (vi->flags & VI_INIT_DONE) {
8796af45170SJohn Baldwin 				struct sge_rxq *rxq;
8806af45170SJohn Baldwin 				struct sge_txq *txq;
8816af45170SJohn Baldwin 
8826af45170SJohn Baldwin 				for_each_rxq(vi, i, rxq) {
8836af45170SJohn Baldwin #if defined(INET) || defined(INET6)
8846af45170SJohn Baldwin 					rxq->lro.lro_queued = 0;
8856af45170SJohn Baldwin 					rxq->lro.lro_flushed = 0;
8866af45170SJohn Baldwin #endif
8876af45170SJohn Baldwin 					rxq->rxcsum = 0;
8886af45170SJohn Baldwin 					rxq->vlan_extraction = 0;
8896af45170SJohn Baldwin 				}
8906af45170SJohn Baldwin 
8916af45170SJohn Baldwin 				for_each_txq(vi, i, txq) {
8926af45170SJohn Baldwin 					txq->txcsum = 0;
8936af45170SJohn Baldwin 					txq->tso_wrs = 0;
8946af45170SJohn Baldwin 					txq->vlan_insertion = 0;
8956af45170SJohn Baldwin 					txq->imm_wrs = 0;
8966af45170SJohn Baldwin 					txq->sgl_wrs = 0;
8976af45170SJohn Baldwin 					txq->txpkt_wrs = 0;
8986af45170SJohn Baldwin 					txq->txpkts0_wrs = 0;
8996af45170SJohn Baldwin 					txq->txpkts1_wrs = 0;
9006af45170SJohn Baldwin 					txq->txpkts0_pkts = 0;
9016af45170SJohn Baldwin 					txq->txpkts1_pkts = 0;
9023447df8bSNavdeep Parhar 					txq->txpkts_flush = 0;
9036af45170SJohn Baldwin 					mp_ring_reset_stats(txq->r);
9046af45170SJohn Baldwin 				}
9056af45170SJohn Baldwin 			}
9066af45170SJohn Baldwin 		}
9076af45170SJohn Baldwin 		break;
9086af45170SJohn Baldwin 	}
9096af45170SJohn Baldwin 	case CHELSIO_T4_SCHED_CLASS:
9106af45170SJohn Baldwin 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
9116af45170SJohn Baldwin 		break;
9126af45170SJohn Baldwin 	case CHELSIO_T4_SCHED_QUEUE:
9136af45170SJohn Baldwin 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
9146af45170SJohn Baldwin 		break;
9156af45170SJohn Baldwin 	default:
9166af45170SJohn Baldwin 		rc = ENOTTY;
9176af45170SJohn Baldwin 	}
9186af45170SJohn Baldwin 
9196af45170SJohn Baldwin 	return (rc);
9206af45170SJohn Baldwin }
9216af45170SJohn Baldwin 
9226af45170SJohn Baldwin static device_method_t t4vf_methods[] = {
9236af45170SJohn Baldwin 	DEVMETHOD(device_probe,		t4vf_probe),
9246af45170SJohn Baldwin 	DEVMETHOD(device_attach,	t4vf_attach),
9256af45170SJohn Baldwin 	DEVMETHOD(device_detach,	t4_detach_common),
9266af45170SJohn Baldwin 
9276af45170SJohn Baldwin 	DEVMETHOD_END
9286af45170SJohn Baldwin };
9296af45170SJohn Baldwin 
9306af45170SJohn Baldwin static driver_t t4vf_driver = {
9316af45170SJohn Baldwin 	"t4vf",
9326af45170SJohn Baldwin 	t4vf_methods,
9336af45170SJohn Baldwin 	sizeof(struct adapter)
9346af45170SJohn Baldwin };
9356af45170SJohn Baldwin 
9366af45170SJohn Baldwin static device_method_t t5vf_methods[] = {
9376af45170SJohn Baldwin 	DEVMETHOD(device_probe,		t5vf_probe),
9386af45170SJohn Baldwin 	DEVMETHOD(device_attach,	t4vf_attach),
9396af45170SJohn Baldwin 	DEVMETHOD(device_detach,	t4_detach_common),
9406af45170SJohn Baldwin 
9416af45170SJohn Baldwin 	DEVMETHOD_END
9426af45170SJohn Baldwin };
9436af45170SJohn Baldwin 
9446af45170SJohn Baldwin static driver_t t5vf_driver = {
9456af45170SJohn Baldwin 	"t5vf",
9466af45170SJohn Baldwin 	t5vf_methods,
9476af45170SJohn Baldwin 	sizeof(struct adapter)
9486af45170SJohn Baldwin };
9496af45170SJohn Baldwin 
950e6b81479SNavdeep Parhar static device_method_t t6vf_methods[] = {
951e6b81479SNavdeep Parhar 	DEVMETHOD(device_probe,		t6vf_probe),
952e6b81479SNavdeep Parhar 	DEVMETHOD(device_attach,	t4vf_attach),
953e6b81479SNavdeep Parhar 	DEVMETHOD(device_detach,	t4_detach_common),
954e6b81479SNavdeep Parhar 
955e6b81479SNavdeep Parhar 	DEVMETHOD_END
956e6b81479SNavdeep Parhar };
957e6b81479SNavdeep Parhar 
958e6b81479SNavdeep Parhar static driver_t t6vf_driver = {
959e6b81479SNavdeep Parhar 	"t6vf",
960e6b81479SNavdeep Parhar 	t6vf_methods,
961e6b81479SNavdeep Parhar 	sizeof(struct adapter)
962e6b81479SNavdeep Parhar };
963e6b81479SNavdeep Parhar 
9646af45170SJohn Baldwin static driver_t cxgbev_driver = {
9656af45170SJohn Baldwin 	"cxgbev",
9666af45170SJohn Baldwin 	cxgbe_methods,
9676af45170SJohn Baldwin 	sizeof(struct port_info)
9686af45170SJohn Baldwin };
9696af45170SJohn Baldwin 
9706af45170SJohn Baldwin static driver_t cxlv_driver = {
9716af45170SJohn Baldwin 	"cxlv",
9726af45170SJohn Baldwin 	cxgbe_methods,
9736af45170SJohn Baldwin 	sizeof(struct port_info)
9746af45170SJohn Baldwin };
9756af45170SJohn Baldwin 
976e6b81479SNavdeep Parhar static driver_t ccv_driver = {
977e6b81479SNavdeep Parhar 	"ccv",
978e6b81479SNavdeep Parhar 	cxgbe_methods,
979e6b81479SNavdeep Parhar 	sizeof(struct port_info)
980e6b81479SNavdeep Parhar };
981e6b81479SNavdeep Parhar 
982e8d1145dSJohn Baldwin DRIVER_MODULE(t4vf, pci, t4vf_driver, 0, 0);
9836af45170SJohn Baldwin MODULE_VERSION(t4vf, 1);
9846af45170SJohn Baldwin MODULE_DEPEND(t4vf, t4nex, 1, 1, 1);
9856af45170SJohn Baldwin 
986e8d1145dSJohn Baldwin DRIVER_MODULE(t5vf, pci, t5vf_driver, 0, 0);
9876af45170SJohn Baldwin MODULE_VERSION(t5vf, 1);
9886af45170SJohn Baldwin MODULE_DEPEND(t5vf, t5nex, 1, 1, 1);
9896af45170SJohn Baldwin 
990e8d1145dSJohn Baldwin DRIVER_MODULE(t6vf, pci, t6vf_driver, 0, 0);
991e6b81479SNavdeep Parhar MODULE_VERSION(t6vf, 1);
992e6b81479SNavdeep Parhar MODULE_DEPEND(t6vf, t6nex, 1, 1, 1);
993e6b81479SNavdeep Parhar 
994e8d1145dSJohn Baldwin DRIVER_MODULE(cxgbev, t4vf, cxgbev_driver, 0, 0);
9956af45170SJohn Baldwin MODULE_VERSION(cxgbev, 1);
9966af45170SJohn Baldwin 
997e8d1145dSJohn Baldwin DRIVER_MODULE(cxlv, t5vf, cxlv_driver, 0, 0);
9986af45170SJohn Baldwin MODULE_VERSION(cxlv, 1);
999e6b81479SNavdeep Parhar 
1000e8d1145dSJohn Baldwin DRIVER_MODULE(ccv, t6vf, ccv_driver, 0, 0);
1001e6b81479SNavdeep Parhar MODULE_VERSION(ccv, 1);
1002