xref: /freebsd/sys/dev/oce/oce_if.c (revision ad5129589c6b9299c36f0735b6fe534a162d718f)
12f345d8eSLuigi Rizzo /*-
22f345d8eSLuigi Rizzo  * Copyright (C) 2012 Emulex
32f345d8eSLuigi Rizzo  * All rights reserved.
42f345d8eSLuigi Rizzo  *
52f345d8eSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
62f345d8eSLuigi Rizzo  * modification, are permitted provided that the following conditions are met:
72f345d8eSLuigi Rizzo  *
82f345d8eSLuigi Rizzo  * 1. Redistributions of source code must retain the above copyright notice,
92f345d8eSLuigi Rizzo  *    this list of conditions and the following disclaimer.
102f345d8eSLuigi Rizzo  *
112f345d8eSLuigi Rizzo  * 2. Redistributions in binary form must reproduce the above copyright
122f345d8eSLuigi Rizzo  *    notice, this list of conditions and the following disclaimer in the
132f345d8eSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
142f345d8eSLuigi Rizzo  *
152f345d8eSLuigi Rizzo  * 3. Neither the name of the Emulex Corporation nor the names of its
162f345d8eSLuigi Rizzo  *    contributors may be used to endorse or promote products derived from
172f345d8eSLuigi Rizzo  *    this software without specific prior written permission.
182f345d8eSLuigi Rizzo  *
192f345d8eSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
202f345d8eSLuigi Rizzo  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212f345d8eSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222f345d8eSLuigi Rizzo  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
232f345d8eSLuigi Rizzo  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242f345d8eSLuigi Rizzo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252f345d8eSLuigi Rizzo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262f345d8eSLuigi Rizzo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272f345d8eSLuigi Rizzo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282f345d8eSLuigi Rizzo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292f345d8eSLuigi Rizzo  * POSSIBILITY OF SUCH DAMAGE.
302f345d8eSLuigi Rizzo  *
312f345d8eSLuigi Rizzo  * Contact Information:
322f345d8eSLuigi Rizzo  * freebsd-drivers@emulex.com
332f345d8eSLuigi Rizzo  *
342f345d8eSLuigi Rizzo  * Emulex
352f345d8eSLuigi Rizzo  * 3333 Susan Street
362f345d8eSLuigi Rizzo  * Costa Mesa, CA 92626
372f345d8eSLuigi Rizzo  */
382f345d8eSLuigi Rizzo 
392f345d8eSLuigi Rizzo 
402f345d8eSLuigi Rizzo /* $FreeBSD$ */
412f345d8eSLuigi Rizzo 
42*ad512958SBjoern A. Zeeb #include "opt_inet6.h"
43*ad512958SBjoern A. Zeeb #include "opt_inet.h"
44*ad512958SBjoern A. Zeeb 
452f345d8eSLuigi Rizzo #include "oce_if.h"
462f345d8eSLuigi Rizzo 
472f345d8eSLuigi Rizzo 
482f345d8eSLuigi Rizzo /* Driver entry points prototypes */
492f345d8eSLuigi Rizzo static int  oce_probe(device_t dev);
502f345d8eSLuigi Rizzo static int  oce_attach(device_t dev);
512f345d8eSLuigi Rizzo static int  oce_detach(device_t dev);
522f345d8eSLuigi Rizzo static int  oce_shutdown(device_t dev);
532f345d8eSLuigi Rizzo static int  oce_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
542f345d8eSLuigi Rizzo static void oce_init(void *xsc);
552f345d8eSLuigi Rizzo static int  oce_multiq_start(struct ifnet *ifp, struct mbuf *m);
562f345d8eSLuigi Rizzo static void oce_multiq_flush(struct ifnet *ifp);
572f345d8eSLuigi Rizzo 
582f345d8eSLuigi Rizzo /* Driver interrupt routines protypes */
592f345d8eSLuigi Rizzo static void oce_intr(void *arg, int pending);
602f345d8eSLuigi Rizzo static int  oce_setup_intr(POCE_SOFTC sc);
612f345d8eSLuigi Rizzo static int  oce_fast_isr(void *arg);
622f345d8eSLuigi Rizzo static int  oce_alloc_intr(POCE_SOFTC sc, int vector,
632f345d8eSLuigi Rizzo 			  void (*isr) (void *arg, int pending));
642f345d8eSLuigi Rizzo 
652f345d8eSLuigi Rizzo /* Media callbacks prototypes */
662f345d8eSLuigi Rizzo static void oce_media_status(struct ifnet *ifp, struct ifmediareq *req);
672f345d8eSLuigi Rizzo static int  oce_media_change(struct ifnet *ifp);
682f345d8eSLuigi Rizzo 
692f345d8eSLuigi Rizzo /* Transmit routines prototypes */
702f345d8eSLuigi Rizzo static int  oce_tx(POCE_SOFTC sc, struct mbuf **mpp, int wq_index);
712f345d8eSLuigi Rizzo static void oce_tx_restart(POCE_SOFTC sc, struct oce_wq *wq);
722f345d8eSLuigi Rizzo static void oce_tx_complete(struct oce_wq *wq, uint32_t wqe_idx,
732f345d8eSLuigi Rizzo 					uint32_t status);
74*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
752f345d8eSLuigi Rizzo static struct mbuf * oce_tso_setup(POCE_SOFTC sc, struct mbuf **mpp,
762f345d8eSLuigi Rizzo 					uint16_t *mss);
77*ad512958SBjoern A. Zeeb #endif
782f345d8eSLuigi Rizzo static int  oce_multiq_transmit(struct ifnet *ifp, struct mbuf *m,
792f345d8eSLuigi Rizzo 				 struct oce_wq *wq);
802f345d8eSLuigi Rizzo 
812f345d8eSLuigi Rizzo /* Receive routines prototypes */
822f345d8eSLuigi Rizzo static void oce_discard_rx_comp(struct oce_rq *rq, struct oce_nic_rx_cqe *cqe);
832f345d8eSLuigi Rizzo static int  oce_cqe_vtp_valid(POCE_SOFTC sc, struct oce_nic_rx_cqe *cqe);
842f345d8eSLuigi Rizzo static int  oce_cqe_portid_valid(POCE_SOFTC sc, struct oce_nic_rx_cqe *cqe);
85*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
862f345d8eSLuigi Rizzo static void oce_rx_flush_lro(struct oce_rq *rq);
87*ad512958SBjoern A. Zeeb #endif
882f345d8eSLuigi Rizzo static void oce_rx(struct oce_rq *rq, uint32_t rqe_idx,
892f345d8eSLuigi Rizzo 						struct oce_nic_rx_cqe *cqe);
902f345d8eSLuigi Rizzo 
912f345d8eSLuigi Rizzo /* Helper function prototypes in this file */
922f345d8eSLuigi Rizzo static int  oce_attach_ifp(POCE_SOFTC sc);
932f345d8eSLuigi Rizzo static void oce_add_vlan(void *arg, struct ifnet *ifp, uint16_t vtag);
942f345d8eSLuigi Rizzo static void oce_del_vlan(void *arg, struct ifnet *ifp, uint16_t vtag);
952f345d8eSLuigi Rizzo static int  oce_vid_config(POCE_SOFTC sc);
962f345d8eSLuigi Rizzo static void oce_mac_addr_set(POCE_SOFTC sc);
972f345d8eSLuigi Rizzo static int  oce_handle_passthrough(struct ifnet *ifp, caddr_t data);
982f345d8eSLuigi Rizzo static void oce_local_timer(void *arg);
99*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
1002f345d8eSLuigi Rizzo static int  oce_init_lro(POCE_SOFTC sc);
101*ad512958SBjoern A. Zeeb #endif
1022f345d8eSLuigi Rizzo static void oce_if_deactivate(POCE_SOFTC sc);
1032f345d8eSLuigi Rizzo static void oce_if_activate(POCE_SOFTC sc);
1042f345d8eSLuigi Rizzo static void setup_max_queues_want(POCE_SOFTC sc);
1052f345d8eSLuigi Rizzo static void update_queues_got(POCE_SOFTC sc);
1062f345d8eSLuigi Rizzo 
1072f345d8eSLuigi Rizzo static device_method_t oce_dispatch[] = {
1082f345d8eSLuigi Rizzo 	DEVMETHOD(device_probe, oce_probe),
1092f345d8eSLuigi Rizzo 	DEVMETHOD(device_attach, oce_attach),
1102f345d8eSLuigi Rizzo 	DEVMETHOD(device_detach, oce_detach),
1112f345d8eSLuigi Rizzo 	DEVMETHOD(device_shutdown, oce_shutdown),
1122f345d8eSLuigi Rizzo 	{0, 0}
1132f345d8eSLuigi Rizzo };
1142f345d8eSLuigi Rizzo 
1152f345d8eSLuigi Rizzo static driver_t oce_driver = {
1162f345d8eSLuigi Rizzo 	"oce",
1172f345d8eSLuigi Rizzo 	oce_dispatch,
1182f345d8eSLuigi Rizzo 	sizeof(OCE_SOFTC)
1192f345d8eSLuigi Rizzo };
1202f345d8eSLuigi Rizzo static devclass_t oce_devclass;
1212f345d8eSLuigi Rizzo 
1222f345d8eSLuigi Rizzo 
1232f345d8eSLuigi Rizzo DRIVER_MODULE(oce, pci, oce_driver, oce_devclass, 0, 0);
1242f345d8eSLuigi Rizzo MODULE_DEPEND(oce, pci, 1, 1, 1);
1252f345d8eSLuigi Rizzo MODULE_DEPEND(oce, ether, 1, 1, 1);
1262f345d8eSLuigi Rizzo MODULE_VERSION(oce, 1);
1272f345d8eSLuigi Rizzo 
1282f345d8eSLuigi Rizzo 
1292f345d8eSLuigi Rizzo /* global vars */
1302f345d8eSLuigi Rizzo const char component_revision[32] = {"///" COMPONENT_REVISION "///"};
1312f345d8eSLuigi Rizzo 
1322f345d8eSLuigi Rizzo /* Module capabilites and parameters */
1332f345d8eSLuigi Rizzo uint32_t oce_max_rsp_handled = OCE_MAX_RSP_HANDLED;
1342f345d8eSLuigi Rizzo uint32_t oce_enable_rss = OCE_MODCAP_RSS;
1352f345d8eSLuigi Rizzo 
1362f345d8eSLuigi Rizzo 
1372f345d8eSLuigi Rizzo TUNABLE_INT("hw.oce.max_rsp_handled", &oce_max_rsp_handled);
1382f345d8eSLuigi Rizzo TUNABLE_INT("hw.oce.enable_rss", &oce_enable_rss);
1392f345d8eSLuigi Rizzo 
1402f345d8eSLuigi Rizzo 
1412f345d8eSLuigi Rizzo /* Supported devices table */
1422f345d8eSLuigi Rizzo static uint32_t supportedDevices[] =  {
1432f345d8eSLuigi Rizzo 	(PCI_VENDOR_SERVERENGINES << 16) | PCI_PRODUCT_BE2,
1442f345d8eSLuigi Rizzo 	(PCI_VENDOR_SERVERENGINES << 16) | PCI_PRODUCT_BE3,
1452f345d8eSLuigi Rizzo 	(PCI_VENDOR_EMULEX << 16) | PCI_PRODUCT_BE3,
1462f345d8eSLuigi Rizzo 	(PCI_VENDOR_EMULEX << 16) | PCI_PRODUCT_XE201,
1472f345d8eSLuigi Rizzo 	(PCI_VENDOR_EMULEX << 16) | PCI_PRODUCT_XE201_VF,
1482f345d8eSLuigi Rizzo };
1492f345d8eSLuigi Rizzo 
1502f345d8eSLuigi Rizzo 
1512f345d8eSLuigi Rizzo 
1522f345d8eSLuigi Rizzo 
1532f345d8eSLuigi Rizzo /*****************************************************************************
1542f345d8eSLuigi Rizzo  *			Driver entry points functions                        *
1552f345d8eSLuigi Rizzo  *****************************************************************************/
1562f345d8eSLuigi Rizzo 
1572f345d8eSLuigi Rizzo static int
1582f345d8eSLuigi Rizzo oce_probe(device_t dev)
1592f345d8eSLuigi Rizzo {
1602f345d8eSLuigi Rizzo 	uint16_t vendor;
1612f345d8eSLuigi Rizzo 	uint16_t device;
1622f345d8eSLuigi Rizzo 	int i;
1632f345d8eSLuigi Rizzo 	char str[80];
1642f345d8eSLuigi Rizzo 	POCE_SOFTC sc;
1652f345d8eSLuigi Rizzo 
1662f345d8eSLuigi Rizzo 	sc = device_get_softc(dev);
1672f345d8eSLuigi Rizzo 	bzero(sc, sizeof(OCE_SOFTC));
1682f345d8eSLuigi Rizzo 	sc->dev = dev;
1692f345d8eSLuigi Rizzo 
1702f345d8eSLuigi Rizzo 	vendor = pci_get_vendor(dev);
1712f345d8eSLuigi Rizzo 	device = pci_get_device(dev);
1722f345d8eSLuigi Rizzo 
1732f345d8eSLuigi Rizzo 	for (i = 0; i < (sizeof(supportedDevices) / sizeof(uint16_t)); i++) {
1742f345d8eSLuigi Rizzo 		if (vendor == ((supportedDevices[i] >> 16) & 0xffff)) {
1752f345d8eSLuigi Rizzo 			if (device == (supportedDevices[i] & 0xffff)) {
1762f345d8eSLuigi Rizzo 				sprintf(str, "%s:%s",
1772f345d8eSLuigi Rizzo 					"Emulex CNA NIC function",
1782f345d8eSLuigi Rizzo 					component_revision);
1792f345d8eSLuigi Rizzo 				device_set_desc_copy(dev, str);
1802f345d8eSLuigi Rizzo 
1812f345d8eSLuigi Rizzo 				switch (device) {
1822f345d8eSLuigi Rizzo 				case PCI_PRODUCT_BE2:
1832f345d8eSLuigi Rizzo 					sc->flags |= OCE_FLAGS_BE2;
1842f345d8eSLuigi Rizzo 					break;
1852f345d8eSLuigi Rizzo 				case PCI_PRODUCT_BE3:
1862f345d8eSLuigi Rizzo 					sc->flags |= OCE_FLAGS_BE3;
1872f345d8eSLuigi Rizzo 					break;
1882f345d8eSLuigi Rizzo 				case PCI_PRODUCT_XE201:
1892f345d8eSLuigi Rizzo 				case PCI_PRODUCT_XE201_VF:
1902f345d8eSLuigi Rizzo 					sc->flags |= OCE_FLAGS_XE201;
1912f345d8eSLuigi Rizzo 					break;
1922f345d8eSLuigi Rizzo 				default:
1932f345d8eSLuigi Rizzo 					return ENXIO;
1942f345d8eSLuigi Rizzo 				}
1952f345d8eSLuigi Rizzo 				return BUS_PROBE_DEFAULT;
1962f345d8eSLuigi Rizzo 			}
1972f345d8eSLuigi Rizzo 		}
1982f345d8eSLuigi Rizzo 	}
1992f345d8eSLuigi Rizzo 
2002f345d8eSLuigi Rizzo 	return ENXIO;
2012f345d8eSLuigi Rizzo }
2022f345d8eSLuigi Rizzo 
2032f345d8eSLuigi Rizzo 
2042f345d8eSLuigi Rizzo static int
2052f345d8eSLuigi Rizzo oce_attach(device_t dev)
2062f345d8eSLuigi Rizzo {
2072f345d8eSLuigi Rizzo 	POCE_SOFTC sc;
2082f345d8eSLuigi Rizzo 	int rc = 0;
2092f345d8eSLuigi Rizzo 
2102f345d8eSLuigi Rizzo 	sc = device_get_softc(dev);
2112f345d8eSLuigi Rizzo 
2122f345d8eSLuigi Rizzo 	rc = oce_hw_pci_alloc(sc);
2132f345d8eSLuigi Rizzo 	if (rc)
2142f345d8eSLuigi Rizzo 		return rc;
2152f345d8eSLuigi Rizzo 
2162f345d8eSLuigi Rizzo 	sc->rss_enable 	 = oce_enable_rss;
2172f345d8eSLuigi Rizzo 	sc->tx_ring_size = OCE_TX_RING_SIZE;
2182f345d8eSLuigi Rizzo 	sc->rx_ring_size = OCE_RX_RING_SIZE;
2192f345d8eSLuigi Rizzo 	sc->rq_frag_size = OCE_RQ_BUF_SIZE;
2202f345d8eSLuigi Rizzo 	sc->flow_control = OCE_DEFAULT_FLOW_CONTROL;
2212f345d8eSLuigi Rizzo 	sc->promisc	 = OCE_DEFAULT_PROMISCUOUS;
2222f345d8eSLuigi Rizzo 
2232f345d8eSLuigi Rizzo 	LOCK_CREATE(&sc->bmbx_lock, "Mailbox_lock");
2242f345d8eSLuigi Rizzo 	LOCK_CREATE(&sc->dev_lock,  "Device_lock");
2252f345d8eSLuigi Rizzo 
2262f345d8eSLuigi Rizzo 	/* initialise the hardware */
2272f345d8eSLuigi Rizzo 	rc = oce_hw_init(sc);
2282f345d8eSLuigi Rizzo 	if (rc)
2292f345d8eSLuigi Rizzo 		goto pci_res_free;
2302f345d8eSLuigi Rizzo 
2312f345d8eSLuigi Rizzo 
2322f345d8eSLuigi Rizzo 	setup_max_queues_want(sc);
2332f345d8eSLuigi Rizzo 
2342f345d8eSLuigi Rizzo 
2352f345d8eSLuigi Rizzo 	rc = oce_setup_intr(sc);
2362f345d8eSLuigi Rizzo 	if (rc)
2372f345d8eSLuigi Rizzo 		goto mbox_free;
2382f345d8eSLuigi Rizzo 
2392f345d8eSLuigi Rizzo 
2402f345d8eSLuigi Rizzo 	rc = oce_queue_init_all(sc);
2412f345d8eSLuigi Rizzo 	if (rc)
2422f345d8eSLuigi Rizzo 		goto intr_free;
2432f345d8eSLuigi Rizzo 
2442f345d8eSLuigi Rizzo 
2452f345d8eSLuigi Rizzo 	rc = oce_attach_ifp(sc);
2462f345d8eSLuigi Rizzo 	if (rc)
2472f345d8eSLuigi Rizzo 		goto queues_free;
2482f345d8eSLuigi Rizzo 
2492f345d8eSLuigi Rizzo 
250*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
2512f345d8eSLuigi Rizzo 	rc = oce_init_lro(sc);
2522f345d8eSLuigi Rizzo 	if (rc)
2532f345d8eSLuigi Rizzo 		goto ifp_free;
254*ad512958SBjoern A. Zeeb #endif
2552f345d8eSLuigi Rizzo 
2562f345d8eSLuigi Rizzo 
2572f345d8eSLuigi Rizzo 	rc = oce_hw_start(sc);
2582f345d8eSLuigi Rizzo 	if (rc)
2592f345d8eSLuigi Rizzo 		goto lro_free;;
2602f345d8eSLuigi Rizzo 
2612f345d8eSLuigi Rizzo 
2622f345d8eSLuigi Rizzo 	sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
2632f345d8eSLuigi Rizzo 				oce_add_vlan, sc, EVENTHANDLER_PRI_FIRST);
2642f345d8eSLuigi Rizzo 	sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
2652f345d8eSLuigi Rizzo 				oce_del_vlan, sc, EVENTHANDLER_PRI_FIRST);
2662f345d8eSLuigi Rizzo 
2672f345d8eSLuigi Rizzo 	rc = oce_stats_init(sc);
2682f345d8eSLuigi Rizzo 	if (rc)
2692f345d8eSLuigi Rizzo 		goto vlan_free;
2702f345d8eSLuigi Rizzo 
2712f345d8eSLuigi Rizzo 	oce_add_sysctls(sc);
2722f345d8eSLuigi Rizzo 
2732f345d8eSLuigi Rizzo 
2742f345d8eSLuigi Rizzo 	callout_init(&sc->timer, CALLOUT_MPSAFE);
2752f345d8eSLuigi Rizzo 	rc = callout_reset(&sc->timer, 2 * hz, oce_local_timer, sc);
2762f345d8eSLuigi Rizzo 	if (rc)
2772f345d8eSLuigi Rizzo 		goto stats_free;
2782f345d8eSLuigi Rizzo 
2792f345d8eSLuigi Rizzo 	return 0;
2802f345d8eSLuigi Rizzo 
2812f345d8eSLuigi Rizzo stats_free:
2822f345d8eSLuigi Rizzo 	callout_drain(&sc->timer);
2832f345d8eSLuigi Rizzo 	oce_stats_free(sc);
2842f345d8eSLuigi Rizzo vlan_free:
2852f345d8eSLuigi Rizzo 	if (sc->vlan_attach)
2862f345d8eSLuigi Rizzo 		EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
2872f345d8eSLuigi Rizzo 	if (sc->vlan_detach)
2882f345d8eSLuigi Rizzo 		EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
2892f345d8eSLuigi Rizzo 	oce_hw_intr_disable(sc);
2902f345d8eSLuigi Rizzo lro_free:
291*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
2922f345d8eSLuigi Rizzo 	oce_free_lro(sc);
2932f345d8eSLuigi Rizzo ifp_free:
294*ad512958SBjoern A. Zeeb #endif
2952f345d8eSLuigi Rizzo 	ether_ifdetach(sc->ifp);
2962f345d8eSLuigi Rizzo 	if_free(sc->ifp);
2972f345d8eSLuigi Rizzo queues_free:
2982f345d8eSLuigi Rizzo 	oce_queue_release_all(sc);
2992f345d8eSLuigi Rizzo intr_free:
3002f345d8eSLuigi Rizzo 	oce_intr_free(sc);
3012f345d8eSLuigi Rizzo mbox_free:
3022f345d8eSLuigi Rizzo 	oce_dma_free(sc, &sc->bsmbx);
3032f345d8eSLuigi Rizzo pci_res_free:
3042f345d8eSLuigi Rizzo 	oce_hw_pci_free(sc);
3052f345d8eSLuigi Rizzo 	LOCK_DESTROY(&sc->dev_lock);
3062f345d8eSLuigi Rizzo 	LOCK_DESTROY(&sc->bmbx_lock);
3072f345d8eSLuigi Rizzo 	return rc;
3082f345d8eSLuigi Rizzo 
3092f345d8eSLuigi Rizzo }
3102f345d8eSLuigi Rizzo 
3112f345d8eSLuigi Rizzo 
3122f345d8eSLuigi Rizzo static int
3132f345d8eSLuigi Rizzo oce_detach(device_t dev)
3142f345d8eSLuigi Rizzo {
3152f345d8eSLuigi Rizzo 	POCE_SOFTC sc = device_get_softc(dev);
3162f345d8eSLuigi Rizzo 
3172f345d8eSLuigi Rizzo 	LOCK(&sc->dev_lock);
3182f345d8eSLuigi Rizzo 
3192f345d8eSLuigi Rizzo 	oce_if_deactivate(sc);
3202f345d8eSLuigi Rizzo 
3212f345d8eSLuigi Rizzo 	UNLOCK(&sc->dev_lock);
3222f345d8eSLuigi Rizzo 
3232f345d8eSLuigi Rizzo 	callout_drain(&sc->timer);
3242f345d8eSLuigi Rizzo 
3252f345d8eSLuigi Rizzo 	if (sc->vlan_attach != NULL)
3262f345d8eSLuigi Rizzo 		EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
3272f345d8eSLuigi Rizzo 	if (sc->vlan_detach != NULL)
3282f345d8eSLuigi Rizzo 		EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
3292f345d8eSLuigi Rizzo 
3302f345d8eSLuigi Rizzo 	ether_ifdetach(sc->ifp);
3312f345d8eSLuigi Rizzo 
3322f345d8eSLuigi Rizzo 	if_free(sc->ifp);
3332f345d8eSLuigi Rizzo 
3342f345d8eSLuigi Rizzo 	oce_hw_shutdown(sc);
3352f345d8eSLuigi Rizzo 
3362f345d8eSLuigi Rizzo 	bus_generic_detach(dev);
3372f345d8eSLuigi Rizzo 
3382f345d8eSLuigi Rizzo 	return 0;
3392f345d8eSLuigi Rizzo }
3402f345d8eSLuigi Rizzo 
3412f345d8eSLuigi Rizzo 
3422f345d8eSLuigi Rizzo static int
3432f345d8eSLuigi Rizzo oce_shutdown(device_t dev)
3442f345d8eSLuigi Rizzo {
3452f345d8eSLuigi Rizzo 	int rc;
3462f345d8eSLuigi Rizzo 
3472f345d8eSLuigi Rizzo 	rc = oce_detach(dev);
3482f345d8eSLuigi Rizzo 
3492f345d8eSLuigi Rizzo 	return rc;
3502f345d8eSLuigi Rizzo }
3512f345d8eSLuigi Rizzo 
3522f345d8eSLuigi Rizzo 
3532f345d8eSLuigi Rizzo static int
3542f345d8eSLuigi Rizzo oce_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3552f345d8eSLuigi Rizzo {
3562f345d8eSLuigi Rizzo 	struct ifreq *ifr = (struct ifreq *)data;
3572f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ifp->if_softc;
3582f345d8eSLuigi Rizzo 	int rc = 0;
3592f345d8eSLuigi Rizzo 	uint32_t u;
3602f345d8eSLuigi Rizzo 
3612f345d8eSLuigi Rizzo 	switch (command) {
3622f345d8eSLuigi Rizzo 	case SIOCGIFPSRCADDR_IN6:
3632f345d8eSLuigi Rizzo 		rc = ether_ioctl(ifp, command, data);
3642f345d8eSLuigi Rizzo 		break;
3652f345d8eSLuigi Rizzo 
3662f345d8eSLuigi Rizzo 	case SIOCGIFPSRCADDR:
3672f345d8eSLuigi Rizzo 		rc = ether_ioctl(ifp, command, data);
3682f345d8eSLuigi Rizzo 		break;
3692f345d8eSLuigi Rizzo 
3702f345d8eSLuigi Rizzo 	case SIOCGIFSTATUS:
3712f345d8eSLuigi Rizzo 		rc = ether_ioctl(ifp, command, data);
3722f345d8eSLuigi Rizzo 		break;
3732f345d8eSLuigi Rizzo 
3742f345d8eSLuigi Rizzo 	case SIOCGIFMEDIA:
3752f345d8eSLuigi Rizzo 		rc = ifmedia_ioctl(ifp, ifr, &sc->media, command);
3762f345d8eSLuigi Rizzo 		break;
3772f345d8eSLuigi Rizzo 
3782f345d8eSLuigi Rizzo 	case SIOCSIFMEDIA:
3792f345d8eSLuigi Rizzo 		rc = ether_ioctl(ifp, command, data);
3802f345d8eSLuigi Rizzo 		break;
3812f345d8eSLuigi Rizzo 
3822f345d8eSLuigi Rizzo 	case SIOCGIFGENERIC:
3832f345d8eSLuigi Rizzo 		rc = ether_ioctl(ifp, command, data);
3842f345d8eSLuigi Rizzo 		break;
3852f345d8eSLuigi Rizzo 
3862f345d8eSLuigi Rizzo 	case SIOCGETMIFCNT_IN6:
3872f345d8eSLuigi Rizzo 		rc = ether_ioctl(ifp, command, data);
3882f345d8eSLuigi Rizzo 		break;
3892f345d8eSLuigi Rizzo 
3902f345d8eSLuigi Rizzo 	case SIOCSIFMTU:
3912f345d8eSLuigi Rizzo 		if (ifr->ifr_mtu > OCE_MAX_MTU)
3922f345d8eSLuigi Rizzo 			rc = EINVAL;
3932f345d8eSLuigi Rizzo 		else
3942f345d8eSLuigi Rizzo 			ifp->if_mtu = ifr->ifr_mtu;
3952f345d8eSLuigi Rizzo 		break;
3962f345d8eSLuigi Rizzo 
3972f345d8eSLuigi Rizzo 	case SIOCSIFFLAGS:
3982f345d8eSLuigi Rizzo 		if (ifp->if_flags & IFF_UP) {
3992f345d8eSLuigi Rizzo 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
4002f345d8eSLuigi Rizzo 				sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
4012f345d8eSLuigi Rizzo 				oce_init(sc);
4022f345d8eSLuigi Rizzo 			}
4032f345d8eSLuigi Rizzo 			device_printf(sc->dev, "Interface Up\n");
4042f345d8eSLuigi Rizzo 		} else {
4052f345d8eSLuigi Rizzo 			LOCK(&sc->dev_lock);
4062f345d8eSLuigi Rizzo 
4072f345d8eSLuigi Rizzo 			sc->ifp->if_drv_flags &=
4082f345d8eSLuigi Rizzo 			    ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4092f345d8eSLuigi Rizzo 			oce_if_deactivate(sc);
4102f345d8eSLuigi Rizzo 
4112f345d8eSLuigi Rizzo 			UNLOCK(&sc->dev_lock);
4122f345d8eSLuigi Rizzo 
4132f345d8eSLuigi Rizzo 			device_printf(sc->dev, "Interface Down\n");
4142f345d8eSLuigi Rizzo 		}
4152f345d8eSLuigi Rizzo 
4162f345d8eSLuigi Rizzo 		if ((ifp->if_flags & IFF_PROMISC) && !sc->promisc) {
4172f345d8eSLuigi Rizzo 			sc->promisc = TRUE;
4182f345d8eSLuigi Rizzo 			oce_rxf_set_promiscuous(sc, sc->promisc);
4192f345d8eSLuigi Rizzo 		} else if (!(ifp->if_flags & IFF_PROMISC) && sc->promisc) {
4202f345d8eSLuigi Rizzo 			sc->promisc = FALSE;
4212f345d8eSLuigi Rizzo 			oce_rxf_set_promiscuous(sc, sc->promisc);
4222f345d8eSLuigi Rizzo 		}
4232f345d8eSLuigi Rizzo 
4242f345d8eSLuigi Rizzo 		break;
4252f345d8eSLuigi Rizzo 
4262f345d8eSLuigi Rizzo 	case SIOCADDMULTI:
4272f345d8eSLuigi Rizzo 	case SIOCDELMULTI:
4282f345d8eSLuigi Rizzo 		rc = oce_hw_update_multicast(sc);
4292f345d8eSLuigi Rizzo 		if (rc)
4302f345d8eSLuigi Rizzo 			device_printf(sc->dev,
4312f345d8eSLuigi Rizzo 				"Update multicast address failed\n");
4322f345d8eSLuigi Rizzo 		break;
4332f345d8eSLuigi Rizzo 
4342f345d8eSLuigi Rizzo 	case SIOCSIFCAP:
4352f345d8eSLuigi Rizzo 		u = ifr->ifr_reqcap ^ ifp->if_capenable;
4362f345d8eSLuigi Rizzo 
4372f345d8eSLuigi Rizzo 		if (u & IFCAP_TXCSUM) {
4382f345d8eSLuigi Rizzo 			ifp->if_capenable ^= IFCAP_TXCSUM;
4392f345d8eSLuigi Rizzo 			ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
4402f345d8eSLuigi Rizzo 
4412f345d8eSLuigi Rizzo 			if (IFCAP_TSO & ifp->if_capenable &&
4422f345d8eSLuigi Rizzo 			    !(IFCAP_TXCSUM & ifp->if_capenable)) {
4432f345d8eSLuigi Rizzo 				ifp->if_capenable &= ~IFCAP_TSO;
4442f345d8eSLuigi Rizzo 				ifp->if_hwassist &= ~CSUM_TSO;
4452f345d8eSLuigi Rizzo 				if_printf(ifp,
4462f345d8eSLuigi Rizzo 					 "TSO disabled due to -txcsum.\n");
4472f345d8eSLuigi Rizzo 			}
4482f345d8eSLuigi Rizzo 		}
4492f345d8eSLuigi Rizzo 
4502f345d8eSLuigi Rizzo 		if (u & IFCAP_RXCSUM)
4512f345d8eSLuigi Rizzo 			ifp->if_capenable ^= IFCAP_RXCSUM;
4522f345d8eSLuigi Rizzo 
4532f345d8eSLuigi Rizzo 		if (u & IFCAP_TSO4) {
4542f345d8eSLuigi Rizzo 			ifp->if_capenable ^= IFCAP_TSO4;
4552f345d8eSLuigi Rizzo 
4562f345d8eSLuigi Rizzo 			if (IFCAP_TSO & ifp->if_capenable) {
4572f345d8eSLuigi Rizzo 				if (IFCAP_TXCSUM & ifp->if_capenable)
4582f345d8eSLuigi Rizzo 					ifp->if_hwassist |= CSUM_TSO;
4592f345d8eSLuigi Rizzo 				else {
4602f345d8eSLuigi Rizzo 					ifp->if_capenable &= ~IFCAP_TSO;
4612f345d8eSLuigi Rizzo 					ifp->if_hwassist &= ~CSUM_TSO;
4622f345d8eSLuigi Rizzo 					if_printf(ifp,
4632f345d8eSLuigi Rizzo 					    "Enable txcsum first.\n");
4642f345d8eSLuigi Rizzo 					rc = EAGAIN;
4652f345d8eSLuigi Rizzo 				}
4662f345d8eSLuigi Rizzo 			} else
4672f345d8eSLuigi Rizzo 				ifp->if_hwassist &= ~CSUM_TSO;
4682f345d8eSLuigi Rizzo 		}
4692f345d8eSLuigi Rizzo 
4702f345d8eSLuigi Rizzo 		if (u & IFCAP_VLAN_HWTAGGING)
4712f345d8eSLuigi Rizzo 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
4722f345d8eSLuigi Rizzo 
4732f345d8eSLuigi Rizzo 		if (u & IFCAP_VLAN_HWFILTER) {
4742f345d8eSLuigi Rizzo 			ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
4752f345d8eSLuigi Rizzo 			oce_vid_config(sc);
4762f345d8eSLuigi Rizzo 		}
4772f345d8eSLuigi Rizzo 
478*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
4792f345d8eSLuigi Rizzo 		if (u & IFCAP_LRO)
4802f345d8eSLuigi Rizzo 			ifp->if_capenable ^= IFCAP_LRO;
481*ad512958SBjoern A. Zeeb #endif
4822f345d8eSLuigi Rizzo 
4832f345d8eSLuigi Rizzo 		break;
4842f345d8eSLuigi Rizzo 
4852f345d8eSLuigi Rizzo 	case SIOCGPRIVATE_0:
4862f345d8eSLuigi Rizzo 		rc = oce_handle_passthrough(ifp, data);
4872f345d8eSLuigi Rizzo 		break;
4882f345d8eSLuigi Rizzo 	default:
4892f345d8eSLuigi Rizzo 		rc = ether_ioctl(ifp, command, data);
4902f345d8eSLuigi Rizzo 		break;
4912f345d8eSLuigi Rizzo 	}
4922f345d8eSLuigi Rizzo 
4932f345d8eSLuigi Rizzo 	return rc;
4942f345d8eSLuigi Rizzo }
4952f345d8eSLuigi Rizzo 
4962f345d8eSLuigi Rizzo 
4972f345d8eSLuigi Rizzo static void
4982f345d8eSLuigi Rizzo oce_init(void *arg)
4992f345d8eSLuigi Rizzo {
5002f345d8eSLuigi Rizzo 	POCE_SOFTC sc = arg;
5012f345d8eSLuigi Rizzo 
5022f345d8eSLuigi Rizzo 	LOCK(&sc->dev_lock);
5032f345d8eSLuigi Rizzo 
5042f345d8eSLuigi Rizzo 	if (sc->ifp->if_flags & IFF_UP) {
5052f345d8eSLuigi Rizzo 		oce_if_deactivate(sc);
5062f345d8eSLuigi Rizzo 		oce_if_activate(sc);
5072f345d8eSLuigi Rizzo 	}
5082f345d8eSLuigi Rizzo 
5092f345d8eSLuigi Rizzo 	UNLOCK(&sc->dev_lock);
5102f345d8eSLuigi Rizzo 
5112f345d8eSLuigi Rizzo }
5122f345d8eSLuigi Rizzo 
5132f345d8eSLuigi Rizzo 
5142f345d8eSLuigi Rizzo static int
5152f345d8eSLuigi Rizzo oce_multiq_start(struct ifnet *ifp, struct mbuf *m)
5162f345d8eSLuigi Rizzo {
5172f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ifp->if_softc;
5182f345d8eSLuigi Rizzo 	struct oce_wq *wq = NULL;
5192f345d8eSLuigi Rizzo 	int queue_index = 0;
5202f345d8eSLuigi Rizzo 	int status = 0;
5212f345d8eSLuigi Rizzo 
5222f345d8eSLuigi Rizzo 	if ((m->m_flags & M_FLOWID) != 0)
5232f345d8eSLuigi Rizzo 		queue_index = m->m_pkthdr.flowid % sc->nwqs;
5242f345d8eSLuigi Rizzo 
5252f345d8eSLuigi Rizzo 	wq = sc->wq[queue_index];
5262f345d8eSLuigi Rizzo 
5272f345d8eSLuigi Rizzo 	if (TRY_LOCK(&wq->tx_lock)) {
5282f345d8eSLuigi Rizzo 		status = oce_multiq_transmit(ifp, m, wq);
5292f345d8eSLuigi Rizzo 		UNLOCK(&wq->tx_lock);
5302f345d8eSLuigi Rizzo 	} else {
5312f345d8eSLuigi Rizzo 		status = drbr_enqueue(ifp, wq->br, m);
5322f345d8eSLuigi Rizzo 	}
5332f345d8eSLuigi Rizzo 	return status;
5342f345d8eSLuigi Rizzo 
5352f345d8eSLuigi Rizzo }
5362f345d8eSLuigi Rizzo 
5372f345d8eSLuigi Rizzo 
5382f345d8eSLuigi Rizzo static void
5392f345d8eSLuigi Rizzo oce_multiq_flush(struct ifnet *ifp)
5402f345d8eSLuigi Rizzo {
5412f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ifp->if_softc;
5422f345d8eSLuigi Rizzo 	struct mbuf     *m;
5432f345d8eSLuigi Rizzo 	int i = 0;
5442f345d8eSLuigi Rizzo 
5452f345d8eSLuigi Rizzo 	for (i = 0; i < sc->nwqs; i++) {
5462f345d8eSLuigi Rizzo 		while ((m = buf_ring_dequeue_sc(sc->wq[i]->br)) != NULL)
5472f345d8eSLuigi Rizzo 			m_freem(m);
5482f345d8eSLuigi Rizzo 	}
5492f345d8eSLuigi Rizzo 	if_qflush(ifp);
5502f345d8eSLuigi Rizzo }
5512f345d8eSLuigi Rizzo 
5522f345d8eSLuigi Rizzo 
5532f345d8eSLuigi Rizzo 
5542f345d8eSLuigi Rizzo /*****************************************************************************
5552f345d8eSLuigi Rizzo  *                   Driver interrupt routines functions                     *
5562f345d8eSLuigi Rizzo  *****************************************************************************/
5572f345d8eSLuigi Rizzo 
5582f345d8eSLuigi Rizzo static void
5592f345d8eSLuigi Rizzo oce_intr(void *arg, int pending)
5602f345d8eSLuigi Rizzo {
5612f345d8eSLuigi Rizzo 
5622f345d8eSLuigi Rizzo 	POCE_INTR_INFO ii = (POCE_INTR_INFO) arg;
5632f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ii->sc;
5642f345d8eSLuigi Rizzo 	struct oce_eq *eq = ii->eq;
5652f345d8eSLuigi Rizzo 	struct oce_eqe *eqe;
5662f345d8eSLuigi Rizzo 	struct oce_cq *cq = NULL;
5672f345d8eSLuigi Rizzo 	int i, num_eqes = 0;
5682f345d8eSLuigi Rizzo 
5692f345d8eSLuigi Rizzo 
5702f345d8eSLuigi Rizzo 	bus_dmamap_sync(eq->ring->dma.tag, eq->ring->dma.map,
5712f345d8eSLuigi Rizzo 				 BUS_DMASYNC_POSTWRITE);
5722f345d8eSLuigi Rizzo 	do {
5732f345d8eSLuigi Rizzo 		eqe = RING_GET_CONSUMER_ITEM_VA(eq->ring, struct oce_eqe);
5742f345d8eSLuigi Rizzo 		if (eqe->evnt == 0)
5752f345d8eSLuigi Rizzo 			break;
5762f345d8eSLuigi Rizzo 		eqe->evnt = 0;
5772f345d8eSLuigi Rizzo 		bus_dmamap_sync(eq->ring->dma.tag, eq->ring->dma.map,
5782f345d8eSLuigi Rizzo 					BUS_DMASYNC_POSTWRITE);
5792f345d8eSLuigi Rizzo 		RING_GET(eq->ring, 1);
5802f345d8eSLuigi Rizzo 		num_eqes++;
5812f345d8eSLuigi Rizzo 
5822f345d8eSLuigi Rizzo 	} while (TRUE);
5832f345d8eSLuigi Rizzo 
5842f345d8eSLuigi Rizzo 	if (!num_eqes)
5852f345d8eSLuigi Rizzo 		goto eq_arm; /* Spurious */
5862f345d8eSLuigi Rizzo 
5872f345d8eSLuigi Rizzo  	/* Clear EQ entries, but dont arm */
5882f345d8eSLuigi Rizzo 	oce_arm_eq(sc, eq->eq_id, num_eqes, FALSE, FALSE);
5892f345d8eSLuigi Rizzo 
5902f345d8eSLuigi Rizzo 	/* Process TX, RX and MCC. But dont arm CQ*/
5912f345d8eSLuigi Rizzo 	for (i = 0; i < eq->cq_valid; i++) {
5922f345d8eSLuigi Rizzo 		cq = eq->cq[i];
5932f345d8eSLuigi Rizzo 		(*cq->cq_handler)(cq->cb_arg);
5942f345d8eSLuigi Rizzo 	}
5952f345d8eSLuigi Rizzo 
5962f345d8eSLuigi Rizzo 	/* Arm all cqs connected to this EQ */
5972f345d8eSLuigi Rizzo 	for (i = 0; i < eq->cq_valid; i++) {
5982f345d8eSLuigi Rizzo 		cq = eq->cq[i];
5992f345d8eSLuigi Rizzo 		oce_arm_cq(sc, cq->cq_id, 0, TRUE);
6002f345d8eSLuigi Rizzo 	}
6012f345d8eSLuigi Rizzo 
6022f345d8eSLuigi Rizzo eq_arm:
6032f345d8eSLuigi Rizzo 	oce_arm_eq(sc, eq->eq_id, 0, TRUE, FALSE);
6042f345d8eSLuigi Rizzo 	return;
6052f345d8eSLuigi Rizzo }
6062f345d8eSLuigi Rizzo 
6072f345d8eSLuigi Rizzo 
6082f345d8eSLuigi Rizzo static int
6092f345d8eSLuigi Rizzo oce_setup_intr(POCE_SOFTC sc)
6102f345d8eSLuigi Rizzo {
6112f345d8eSLuigi Rizzo 	int rc = 0, use_intx = 0;
6122f345d8eSLuigi Rizzo 	int vector = 0, req_vectors = 0;
6132f345d8eSLuigi Rizzo 
6142f345d8eSLuigi Rizzo 	if (sc->rss_enable)
6152f345d8eSLuigi Rizzo 		req_vectors = MAX((sc->nrqs - 1), sc->nwqs);
6162f345d8eSLuigi Rizzo 	else
6172f345d8eSLuigi Rizzo 		req_vectors = 1;
6182f345d8eSLuigi Rizzo 
6192f345d8eSLuigi Rizzo 	if (sc->flags & OCE_FLAGS_MSIX_CAPABLE) {
6202f345d8eSLuigi Rizzo 		sc->intr_count = req_vectors;
6212f345d8eSLuigi Rizzo 		rc = pci_alloc_msix(sc->dev, &sc->intr_count);
6222f345d8eSLuigi Rizzo 		if (rc != 0) {
6232f345d8eSLuigi Rizzo 			use_intx = 1;
6242f345d8eSLuigi Rizzo 			pci_release_msi(sc->dev);
6252f345d8eSLuigi Rizzo 		} else
6262f345d8eSLuigi Rizzo 			sc->flags |= OCE_FLAGS_USING_MSIX;
6272f345d8eSLuigi Rizzo 	} else
6282f345d8eSLuigi Rizzo 		use_intx = 1;
6292f345d8eSLuigi Rizzo 
6302f345d8eSLuigi Rizzo 	if (use_intx)
6312f345d8eSLuigi Rizzo 		sc->intr_count = 1;
6322f345d8eSLuigi Rizzo 
6332f345d8eSLuigi Rizzo 	/* Scale number of queues based on intr we got */
6342f345d8eSLuigi Rizzo 	update_queues_got(sc);
6352f345d8eSLuigi Rizzo 
6362f345d8eSLuigi Rizzo 	if (use_intx) {
6372f345d8eSLuigi Rizzo 		device_printf(sc->dev, "Using legacy interrupt\n");
6382f345d8eSLuigi Rizzo 		rc = oce_alloc_intr(sc, vector, oce_intr);
6392f345d8eSLuigi Rizzo 		if (rc)
6402f345d8eSLuigi Rizzo 			goto error;
6412f345d8eSLuigi Rizzo 	} else {
6422f345d8eSLuigi Rizzo 		for (; vector < sc->intr_count; vector++) {
6432f345d8eSLuigi Rizzo 			rc = oce_alloc_intr(sc, vector, oce_intr);
6442f345d8eSLuigi Rizzo 			if (rc)
6452f345d8eSLuigi Rizzo 				goto error;
6462f345d8eSLuigi Rizzo 		}
6472f345d8eSLuigi Rizzo 	}
6482f345d8eSLuigi Rizzo 
6492f345d8eSLuigi Rizzo 	return 0;
6502f345d8eSLuigi Rizzo error:
6512f345d8eSLuigi Rizzo 	oce_intr_free(sc);
6522f345d8eSLuigi Rizzo 	return rc;
6532f345d8eSLuigi Rizzo }
6542f345d8eSLuigi Rizzo 
6552f345d8eSLuigi Rizzo 
6562f345d8eSLuigi Rizzo static int
6572f345d8eSLuigi Rizzo oce_fast_isr(void *arg)
6582f345d8eSLuigi Rizzo {
6592f345d8eSLuigi Rizzo 	POCE_INTR_INFO ii = (POCE_INTR_INFO) arg;
6602f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ii->sc;
6612f345d8eSLuigi Rizzo 
6622f345d8eSLuigi Rizzo 	if (ii->eq == NULL)
6632f345d8eSLuigi Rizzo 		return FILTER_STRAY;
6642f345d8eSLuigi Rizzo 
6652f345d8eSLuigi Rizzo 	oce_arm_eq(sc, ii->eq->eq_id, 0, FALSE, TRUE);
6662f345d8eSLuigi Rizzo 
6672f345d8eSLuigi Rizzo 	taskqueue_enqueue_fast(ii->tq, &ii->task);
6682f345d8eSLuigi Rizzo 
6692f345d8eSLuigi Rizzo 	return FILTER_HANDLED;
6702f345d8eSLuigi Rizzo }
6712f345d8eSLuigi Rizzo 
6722f345d8eSLuigi Rizzo 
6732f345d8eSLuigi Rizzo static int
6742f345d8eSLuigi Rizzo oce_alloc_intr(POCE_SOFTC sc, int vector, void (*isr) (void *arg, int pending))
6752f345d8eSLuigi Rizzo {
6762f345d8eSLuigi Rizzo 	POCE_INTR_INFO ii = &sc->intrs[vector];
6772f345d8eSLuigi Rizzo 	int rc = 0, rr;
6782f345d8eSLuigi Rizzo 
6792f345d8eSLuigi Rizzo 	if (vector >= OCE_MAX_EQ)
6802f345d8eSLuigi Rizzo 		return (EINVAL);
6812f345d8eSLuigi Rizzo 
6822f345d8eSLuigi Rizzo 	/* Set the resource id for the interrupt.
6832f345d8eSLuigi Rizzo 	 * MSIx is vector + 1 for the resource id,
6842f345d8eSLuigi Rizzo 	 * INTx is 0 for the resource id.
6852f345d8eSLuigi Rizzo 	 */
6862f345d8eSLuigi Rizzo 	if (sc->flags & OCE_FLAGS_USING_MSIX)
6872f345d8eSLuigi Rizzo 		rr = vector + 1;
6882f345d8eSLuigi Rizzo 	else
6892f345d8eSLuigi Rizzo 		rr = 0;
6902f345d8eSLuigi Rizzo 	ii->intr_res = bus_alloc_resource_any(sc->dev,
6912f345d8eSLuigi Rizzo 					      SYS_RES_IRQ,
6922f345d8eSLuigi Rizzo 					      &rr, RF_ACTIVE|RF_SHAREABLE);
6932f345d8eSLuigi Rizzo 	ii->irq_rr = rr;
6942f345d8eSLuigi Rizzo 	if (ii->intr_res == NULL) {
6952f345d8eSLuigi Rizzo 		device_printf(sc->dev,
6962f345d8eSLuigi Rizzo 			  "Could not allocate interrupt\n");
6972f345d8eSLuigi Rizzo 		rc = ENXIO;
6982f345d8eSLuigi Rizzo 		return rc;
6992f345d8eSLuigi Rizzo 	}
7002f345d8eSLuigi Rizzo 
7012f345d8eSLuigi Rizzo 	TASK_INIT(&ii->task, 0, isr, ii);
7022f345d8eSLuigi Rizzo 	ii->vector = vector;
7032f345d8eSLuigi Rizzo 	sprintf(ii->task_name, "oce_task[%d]", ii->vector);
7042f345d8eSLuigi Rizzo 	ii->tq = taskqueue_create_fast(ii->task_name,
7052f345d8eSLuigi Rizzo 			M_NOWAIT,
7062f345d8eSLuigi Rizzo 			taskqueue_thread_enqueue,
7072f345d8eSLuigi Rizzo 			&ii->tq);
7082f345d8eSLuigi Rizzo 	taskqueue_start_threads(&ii->tq, 1, PI_NET, "%s taskq",
7092f345d8eSLuigi Rizzo 			device_get_nameunit(sc->dev));
7102f345d8eSLuigi Rizzo 
7112f345d8eSLuigi Rizzo 	ii->sc = sc;
7122f345d8eSLuigi Rizzo 	rc = bus_setup_intr(sc->dev,
7132f345d8eSLuigi Rizzo 			ii->intr_res,
7142f345d8eSLuigi Rizzo 			INTR_TYPE_NET,
7152f345d8eSLuigi Rizzo 			oce_fast_isr, NULL, ii, &ii->tag);
7162f345d8eSLuigi Rizzo 	return rc;
7172f345d8eSLuigi Rizzo 
7182f345d8eSLuigi Rizzo }
7192f345d8eSLuigi Rizzo 
7202f345d8eSLuigi Rizzo 
7212f345d8eSLuigi Rizzo void
7222f345d8eSLuigi Rizzo oce_intr_free(POCE_SOFTC sc)
7232f345d8eSLuigi Rizzo {
7242f345d8eSLuigi Rizzo 	int i = 0;
7252f345d8eSLuigi Rizzo 
7262f345d8eSLuigi Rizzo 	for (i = 0; i < sc->intr_count; i++) {
7272f345d8eSLuigi Rizzo 
7282f345d8eSLuigi Rizzo 		if (sc->intrs[i].tag != NULL)
7292f345d8eSLuigi Rizzo 			bus_teardown_intr(sc->dev, sc->intrs[i].intr_res,
7302f345d8eSLuigi Rizzo 						sc->intrs[i].tag);
7312f345d8eSLuigi Rizzo 		if (sc->intrs[i].tq != NULL)
7322f345d8eSLuigi Rizzo 			taskqueue_free(sc->intrs[i].tq);
7332f345d8eSLuigi Rizzo 
7342f345d8eSLuigi Rizzo 		if (sc->intrs[i].intr_res != NULL)
7352f345d8eSLuigi Rizzo 			bus_release_resource(sc->dev, SYS_RES_IRQ,
7362f345d8eSLuigi Rizzo 						sc->intrs[i].irq_rr,
7372f345d8eSLuigi Rizzo 						sc->intrs[i].intr_res);
7382f345d8eSLuigi Rizzo 		sc->intrs[i].tag = NULL;
7392f345d8eSLuigi Rizzo 		sc->intrs[i].intr_res = NULL;
7402f345d8eSLuigi Rizzo 	}
7412f345d8eSLuigi Rizzo 
7422f345d8eSLuigi Rizzo 	if (sc->flags & OCE_FLAGS_USING_MSIX)
7432f345d8eSLuigi Rizzo 		pci_release_msi(sc->dev);
7442f345d8eSLuigi Rizzo 
7452f345d8eSLuigi Rizzo }
7462f345d8eSLuigi Rizzo 
7472f345d8eSLuigi Rizzo 
7482f345d8eSLuigi Rizzo 
7492f345d8eSLuigi Rizzo /******************************************************************************
7502f345d8eSLuigi Rizzo *			  Media callbacks functions 			      *
7512f345d8eSLuigi Rizzo ******************************************************************************/
7522f345d8eSLuigi Rizzo 
7532f345d8eSLuigi Rizzo static void
7542f345d8eSLuigi Rizzo oce_media_status(struct ifnet *ifp, struct ifmediareq *req)
7552f345d8eSLuigi Rizzo {
7562f345d8eSLuigi Rizzo 	POCE_SOFTC sc = (POCE_SOFTC) ifp->if_softc;
7572f345d8eSLuigi Rizzo 
7582f345d8eSLuigi Rizzo 
7592f345d8eSLuigi Rizzo 	req->ifm_status = IFM_AVALID;
7602f345d8eSLuigi Rizzo 	req->ifm_active = IFM_ETHER;
7612f345d8eSLuigi Rizzo 
7622f345d8eSLuigi Rizzo 	if (sc->link_status == 1)
7632f345d8eSLuigi Rizzo 		req->ifm_status |= IFM_ACTIVE;
7642f345d8eSLuigi Rizzo 	else
7652f345d8eSLuigi Rizzo 		return;
7662f345d8eSLuigi Rizzo 
7672f345d8eSLuigi Rizzo 	switch (sc->link_speed) {
7682f345d8eSLuigi Rizzo 	case 1: /* 10 Mbps */
7692f345d8eSLuigi Rizzo 		req->ifm_active |= IFM_10_T | IFM_FDX;
7702f345d8eSLuigi Rizzo 		sc->speed = 10;
7712f345d8eSLuigi Rizzo 		break;
7722f345d8eSLuigi Rizzo 	case 2: /* 100 Mbps */
7732f345d8eSLuigi Rizzo 		req->ifm_active |= IFM_100_TX | IFM_FDX;
7742f345d8eSLuigi Rizzo 		sc->speed = 100;
7752f345d8eSLuigi Rizzo 		break;
7762f345d8eSLuigi Rizzo 	case 3: /* 1 Gbps */
7772f345d8eSLuigi Rizzo 		req->ifm_active |= IFM_1000_T | IFM_FDX;
7782f345d8eSLuigi Rizzo 		sc->speed = 1000;
7792f345d8eSLuigi Rizzo 		break;
7802f345d8eSLuigi Rizzo 	case 4: /* 10 Gbps */
7812f345d8eSLuigi Rizzo 		req->ifm_active |= IFM_10G_SR | IFM_FDX;
7822f345d8eSLuigi Rizzo 		sc->speed = 10000;
7832f345d8eSLuigi Rizzo 		break;
7842f345d8eSLuigi Rizzo 	}
7852f345d8eSLuigi Rizzo 
7862f345d8eSLuigi Rizzo 	return;
7872f345d8eSLuigi Rizzo }
7882f345d8eSLuigi Rizzo 
7892f345d8eSLuigi Rizzo 
7902f345d8eSLuigi Rizzo int
7912f345d8eSLuigi Rizzo oce_media_change(struct ifnet *ifp)
7922f345d8eSLuigi Rizzo {
7932f345d8eSLuigi Rizzo 	return 0;
7942f345d8eSLuigi Rizzo }
7952f345d8eSLuigi Rizzo 
7962f345d8eSLuigi Rizzo 
7972f345d8eSLuigi Rizzo 
7982f345d8eSLuigi Rizzo 
7992f345d8eSLuigi Rizzo /*****************************************************************************
8002f345d8eSLuigi Rizzo  *			  Transmit routines functions			     *
8012f345d8eSLuigi Rizzo  *****************************************************************************/
8022f345d8eSLuigi Rizzo 
8032f345d8eSLuigi Rizzo static int
8042f345d8eSLuigi Rizzo oce_tx(POCE_SOFTC sc, struct mbuf **mpp, int wq_index)
8052f345d8eSLuigi Rizzo {
8062f345d8eSLuigi Rizzo 	int rc = 0, i, retry_cnt = 0;
8072f345d8eSLuigi Rizzo 	bus_dma_segment_t segs[OCE_MAX_TX_ELEMENTS];
8082f345d8eSLuigi Rizzo 	struct mbuf *m, *m_temp;
8092f345d8eSLuigi Rizzo 	struct oce_wq *wq = sc->wq[wq_index];
8102f345d8eSLuigi Rizzo 	struct oce_packet_desc *pd;
8112f345d8eSLuigi Rizzo 	uint32_t out;
8122f345d8eSLuigi Rizzo 	struct oce_nic_hdr_wqe *nichdr;
8132f345d8eSLuigi Rizzo 	struct oce_nic_frag_wqe *nicfrag;
8142f345d8eSLuigi Rizzo 	int num_wqes;
8152f345d8eSLuigi Rizzo 	uint32_t reg_value;
816*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
8172f345d8eSLuigi Rizzo 	uint16_t mss = 0;
818*ad512958SBjoern A. Zeeb #endif
8192f345d8eSLuigi Rizzo 
8202f345d8eSLuigi Rizzo 	m = *mpp;
8212f345d8eSLuigi Rizzo 	if (!m)
8222f345d8eSLuigi Rizzo 		return EINVAL;
8232f345d8eSLuigi Rizzo 
8242f345d8eSLuigi Rizzo 	if (!(m->m_flags & M_PKTHDR)) {
8252f345d8eSLuigi Rizzo 		rc = ENXIO;
8262f345d8eSLuigi Rizzo 		goto free_ret;
8272f345d8eSLuigi Rizzo 	}
8282f345d8eSLuigi Rizzo 
8292f345d8eSLuigi Rizzo 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
830*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
8312f345d8eSLuigi Rizzo 		/* consolidate packet buffers for TSO/LSO segment offload */
8322f345d8eSLuigi Rizzo 		m = oce_tso_setup(sc, mpp, &mss);
833*ad512958SBjoern A. Zeeb #else
834*ad512958SBjoern A. Zeeb 		m = NULL;
835*ad512958SBjoern A. Zeeb #endif
8362f345d8eSLuigi Rizzo 		if (m == NULL) {
8372f345d8eSLuigi Rizzo 			rc = ENXIO;
8382f345d8eSLuigi Rizzo 			goto free_ret;
8392f345d8eSLuigi Rizzo 		}
8402f345d8eSLuigi Rizzo 	}
8412f345d8eSLuigi Rizzo 
8422f345d8eSLuigi Rizzo 	out = wq->packets_out + 1;
8432f345d8eSLuigi Rizzo 	if (out == OCE_WQ_PACKET_ARRAY_SIZE)
8442f345d8eSLuigi Rizzo 		out = 0;
8452f345d8eSLuigi Rizzo 	if (out == wq->packets_in)
8462f345d8eSLuigi Rizzo 		return EBUSY;
8472f345d8eSLuigi Rizzo 
8482f345d8eSLuigi Rizzo 	pd = &wq->pckts[wq->packets_out];
8492f345d8eSLuigi Rizzo retry:
8502f345d8eSLuigi Rizzo 	rc = bus_dmamap_load_mbuf_sg(wq->tag,
8512f345d8eSLuigi Rizzo 				     pd->map,
8522f345d8eSLuigi Rizzo 				     m, segs, &pd->nsegs, BUS_DMA_NOWAIT);
8532f345d8eSLuigi Rizzo 	if (rc == 0) {
8542f345d8eSLuigi Rizzo 		num_wqes = pd->nsegs + 1;
8552f345d8eSLuigi Rizzo 		if (IS_BE(sc)) {
8562f345d8eSLuigi Rizzo 			/*Dummy required only for BE3.*/
8572f345d8eSLuigi Rizzo 			if (num_wqes & 1)
8582f345d8eSLuigi Rizzo 				num_wqes++;
8592f345d8eSLuigi Rizzo 		}
8602f345d8eSLuigi Rizzo 		if (num_wqes >= RING_NUM_FREE(wq->ring)) {
8612f345d8eSLuigi Rizzo 			bus_dmamap_unload(wq->tag, pd->map);
8622f345d8eSLuigi Rizzo 			return EBUSY;
8632f345d8eSLuigi Rizzo 		}
8642f345d8eSLuigi Rizzo 
8652f345d8eSLuigi Rizzo 		bus_dmamap_sync(wq->tag, pd->map, BUS_DMASYNC_PREWRITE);
8662f345d8eSLuigi Rizzo 		pd->mbuf = m;
8672f345d8eSLuigi Rizzo 		wq->packets_out = out;
8682f345d8eSLuigi Rizzo 
8692f345d8eSLuigi Rizzo 		nichdr =
8702f345d8eSLuigi Rizzo 		    RING_GET_PRODUCER_ITEM_VA(wq->ring, struct oce_nic_hdr_wqe);
8712f345d8eSLuigi Rizzo 		nichdr->u0.dw[0] = 0;
8722f345d8eSLuigi Rizzo 		nichdr->u0.dw[1] = 0;
8732f345d8eSLuigi Rizzo 		nichdr->u0.dw[2] = 0;
8742f345d8eSLuigi Rizzo 		nichdr->u0.dw[3] = 0;
8752f345d8eSLuigi Rizzo 
8762f345d8eSLuigi Rizzo 		nichdr->u0.s.complete = 1;
8772f345d8eSLuigi Rizzo 		nichdr->u0.s.event = 1;
8782f345d8eSLuigi Rizzo 		nichdr->u0.s.crc = 1;
8792f345d8eSLuigi Rizzo 		nichdr->u0.s.forward = 0;
8802f345d8eSLuigi Rizzo 		nichdr->u0.s.ipcs = (m->m_pkthdr.csum_flags & CSUM_IP) ? 1 : 0;
8812f345d8eSLuigi Rizzo 		nichdr->u0.s.udpcs =
8822f345d8eSLuigi Rizzo 		    (m->m_pkthdr.csum_flags & CSUM_UDP) ? 1 : 0;
8832f345d8eSLuigi Rizzo 		nichdr->u0.s.tcpcs =
8842f345d8eSLuigi Rizzo 		    (m->m_pkthdr.csum_flags & CSUM_TCP) ? 1 : 0;
8852f345d8eSLuigi Rizzo 		nichdr->u0.s.num_wqe = num_wqes;
8862f345d8eSLuigi Rizzo 		nichdr->u0.s.total_length = m->m_pkthdr.len;
8872f345d8eSLuigi Rizzo 		if (m->m_flags & M_VLANTAG) {
8882f345d8eSLuigi Rizzo 			nichdr->u0.s.vlan = 1; /*Vlan present*/
8892f345d8eSLuigi Rizzo 			nichdr->u0.s.vlan_tag = m->m_pkthdr.ether_vtag;
8902f345d8eSLuigi Rizzo 		}
8912f345d8eSLuigi Rizzo 		if (m->m_pkthdr.csum_flags & CSUM_TSO) {
8922f345d8eSLuigi Rizzo 			if (m->m_pkthdr.tso_segsz) {
8932f345d8eSLuigi Rizzo 				nichdr->u0.s.lso = 1;
8942f345d8eSLuigi Rizzo 				nichdr->u0.s.lso_mss  = m->m_pkthdr.tso_segsz;
8952f345d8eSLuigi Rizzo 			}
8962f345d8eSLuigi Rizzo 			if (!IS_BE(sc))
8972f345d8eSLuigi Rizzo 				nichdr->u0.s.ipcs = 1;
8982f345d8eSLuigi Rizzo 		}
8992f345d8eSLuigi Rizzo 
9002f345d8eSLuigi Rizzo 		RING_PUT(wq->ring, 1);
9012f345d8eSLuigi Rizzo 		wq->ring->num_used++;
9022f345d8eSLuigi Rizzo 
9032f345d8eSLuigi Rizzo 		for (i = 0; i < pd->nsegs; i++) {
9042f345d8eSLuigi Rizzo 			nicfrag =
9052f345d8eSLuigi Rizzo 			    RING_GET_PRODUCER_ITEM_VA(wq->ring,
9062f345d8eSLuigi Rizzo 						      struct oce_nic_frag_wqe);
9072f345d8eSLuigi Rizzo 			nicfrag->u0.s.rsvd0 = 0;
9082f345d8eSLuigi Rizzo 			nicfrag->u0.s.frag_pa_hi = ADDR_HI(segs[i].ds_addr);
9092f345d8eSLuigi Rizzo 			nicfrag->u0.s.frag_pa_lo = ADDR_LO(segs[i].ds_addr);
9102f345d8eSLuigi Rizzo 			nicfrag->u0.s.frag_len = segs[i].ds_len;
9112f345d8eSLuigi Rizzo 			pd->wqe_idx = wq->ring->pidx;
9122f345d8eSLuigi Rizzo 			RING_PUT(wq->ring, 1);
9132f345d8eSLuigi Rizzo 			wq->ring->num_used++;
9142f345d8eSLuigi Rizzo 		}
9152f345d8eSLuigi Rizzo 		if (num_wqes > (pd->nsegs + 1)) {
9162f345d8eSLuigi Rizzo 			nicfrag =
9172f345d8eSLuigi Rizzo 			    RING_GET_PRODUCER_ITEM_VA(wq->ring,
9182f345d8eSLuigi Rizzo 						      struct oce_nic_frag_wqe);
9192f345d8eSLuigi Rizzo 			nicfrag->u0.dw[0] = 0;
9202f345d8eSLuigi Rizzo 			nicfrag->u0.dw[1] = 0;
9212f345d8eSLuigi Rizzo 			nicfrag->u0.dw[2] = 0;
9222f345d8eSLuigi Rizzo 			nicfrag->u0.dw[3] = 0;
9232f345d8eSLuigi Rizzo 			pd->wqe_idx = wq->ring->pidx;
9242f345d8eSLuigi Rizzo 			RING_PUT(wq->ring, 1);
9252f345d8eSLuigi Rizzo 			wq->ring->num_used++;
9262f345d8eSLuigi Rizzo 			pd->nsegs++;
9272f345d8eSLuigi Rizzo 		}
9282f345d8eSLuigi Rizzo 
9292f345d8eSLuigi Rizzo 		sc->ifp->if_opackets++;
9302f345d8eSLuigi Rizzo 		wq->tx_stats.tx_reqs++;
9312f345d8eSLuigi Rizzo 		wq->tx_stats.tx_wrbs += num_wqes;
9322f345d8eSLuigi Rizzo 		wq->tx_stats.tx_bytes += m->m_pkthdr.len;
9332f345d8eSLuigi Rizzo 		wq->tx_stats.tx_pkts++;
9342f345d8eSLuigi Rizzo 
9352f345d8eSLuigi Rizzo 		bus_dmamap_sync(wq->ring->dma.tag, wq->ring->dma.map,
9362f345d8eSLuigi Rizzo 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
9372f345d8eSLuigi Rizzo 		reg_value = (num_wqes << 16) | wq->wq_id;
9382f345d8eSLuigi Rizzo 		OCE_WRITE_REG32(sc, db, PD_TXULP_DB, reg_value);
9392f345d8eSLuigi Rizzo 
9402f345d8eSLuigi Rizzo 	} else if (rc == EFBIG)	{
9412f345d8eSLuigi Rizzo 		if (retry_cnt == 0) {
9422f345d8eSLuigi Rizzo 			m_temp = m_defrag(m, M_DONTWAIT);
9432f345d8eSLuigi Rizzo 			if (m_temp == NULL)
9442f345d8eSLuigi Rizzo 				goto free_ret;
9452f345d8eSLuigi Rizzo 			m = m_temp;
9462f345d8eSLuigi Rizzo 			*mpp = m_temp;
9472f345d8eSLuigi Rizzo 			retry_cnt = retry_cnt + 1;
9482f345d8eSLuigi Rizzo 			goto retry;
9492f345d8eSLuigi Rizzo 		} else
9502f345d8eSLuigi Rizzo 			goto free_ret;
9512f345d8eSLuigi Rizzo 	} else if (rc == ENOMEM)
9522f345d8eSLuigi Rizzo 		return rc;
9532f345d8eSLuigi Rizzo 	else
9542f345d8eSLuigi Rizzo 		goto free_ret;
9552f345d8eSLuigi Rizzo 
9562f345d8eSLuigi Rizzo 	return 0;
9572f345d8eSLuigi Rizzo 
9582f345d8eSLuigi Rizzo free_ret:
9592f345d8eSLuigi Rizzo 	m_freem(*mpp);
9602f345d8eSLuigi Rizzo 	*mpp = NULL;
9612f345d8eSLuigi Rizzo 	return rc;
9622f345d8eSLuigi Rizzo }
9632f345d8eSLuigi Rizzo 
9642f345d8eSLuigi Rizzo 
9652f345d8eSLuigi Rizzo static void
9662f345d8eSLuigi Rizzo oce_tx_complete(struct oce_wq *wq, uint32_t wqe_idx, uint32_t status)
9672f345d8eSLuigi Rizzo {
9682f345d8eSLuigi Rizzo 	uint32_t in;
9692f345d8eSLuigi Rizzo 	struct oce_packet_desc *pd;
9702f345d8eSLuigi Rizzo 	POCE_SOFTC sc = (POCE_SOFTC) wq->parent;
9712f345d8eSLuigi Rizzo 	struct mbuf *m;
9722f345d8eSLuigi Rizzo 
9732f345d8eSLuigi Rizzo 	if (wq->packets_out == wq->packets_in)
9742f345d8eSLuigi Rizzo 		device_printf(sc->dev, "WQ transmit descriptor missing\n");
9752f345d8eSLuigi Rizzo 
9762f345d8eSLuigi Rizzo 	in = wq->packets_in + 1;
9772f345d8eSLuigi Rizzo 	if (in == OCE_WQ_PACKET_ARRAY_SIZE)
9782f345d8eSLuigi Rizzo 		in = 0;
9792f345d8eSLuigi Rizzo 
9802f345d8eSLuigi Rizzo 	pd = &wq->pckts[wq->packets_in];
9812f345d8eSLuigi Rizzo 	wq->packets_in = in;
9822f345d8eSLuigi Rizzo 	wq->ring->num_used -= (pd->nsegs + 1);
9832f345d8eSLuigi Rizzo 	bus_dmamap_sync(wq->tag, pd->map, BUS_DMASYNC_POSTWRITE);
9842f345d8eSLuigi Rizzo 	bus_dmamap_unload(wq->tag, pd->map);
9852f345d8eSLuigi Rizzo 
9862f345d8eSLuigi Rizzo 	m = pd->mbuf;
9872f345d8eSLuigi Rizzo 	m_freem(m);
9882f345d8eSLuigi Rizzo 	pd->mbuf = NULL;
9892f345d8eSLuigi Rizzo 
9902f345d8eSLuigi Rizzo 	if (sc->ifp->if_drv_flags & IFF_DRV_OACTIVE) {
9912f345d8eSLuigi Rizzo 		if (wq->ring->num_used < (wq->ring->num_items / 2)) {
9922f345d8eSLuigi Rizzo 			sc->ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
9932f345d8eSLuigi Rizzo 			oce_tx_restart(sc, wq);
9942f345d8eSLuigi Rizzo 		}
9952f345d8eSLuigi Rizzo 	}
9962f345d8eSLuigi Rizzo }
9972f345d8eSLuigi Rizzo 
9982f345d8eSLuigi Rizzo 
9992f345d8eSLuigi Rizzo static void
10002f345d8eSLuigi Rizzo oce_tx_restart(POCE_SOFTC sc, struct oce_wq *wq)
10012f345d8eSLuigi Rizzo {
10022f345d8eSLuigi Rizzo 
10032f345d8eSLuigi Rizzo 	if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING)
10042f345d8eSLuigi Rizzo 		return;
10052f345d8eSLuigi Rizzo 
10062f345d8eSLuigi Rizzo #if __FreeBSD_version >= 800000
10072f345d8eSLuigi Rizzo 	if (!drbr_empty(sc->ifp, wq->br))
10082f345d8eSLuigi Rizzo #else
10092f345d8eSLuigi Rizzo 	if (!IFQ_DRV_IS_EMPTY(&sc->ifp->if_snd))
10102f345d8eSLuigi Rizzo #endif
10112f345d8eSLuigi Rizzo 		taskqueue_enqueue_fast(taskqueue_swi, &wq->txtask);
10122f345d8eSLuigi Rizzo 
10132f345d8eSLuigi Rizzo }
10142f345d8eSLuigi Rizzo 
1015*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
10162f345d8eSLuigi Rizzo static struct mbuf *
10172f345d8eSLuigi Rizzo oce_tso_setup(POCE_SOFTC sc, struct mbuf **mpp, uint16_t *mss)
10182f345d8eSLuigi Rizzo {
10192f345d8eSLuigi Rizzo 	struct mbuf *m;
1020*ad512958SBjoern A. Zeeb #ifdef INET
10212f345d8eSLuigi Rizzo 	struct ip *ip;
1022*ad512958SBjoern A. Zeeb #endif
1023*ad512958SBjoern A. Zeeb #ifdef INET6
10242f345d8eSLuigi Rizzo 	struct ip6_hdr *ip6;
1025*ad512958SBjoern A. Zeeb #endif
10262f345d8eSLuigi Rizzo 	struct ether_vlan_header *eh;
10272f345d8eSLuigi Rizzo 	struct tcphdr *th;
1028*ad512958SBjoern A. Zeeb 	int total_len = 0;
10292f345d8eSLuigi Rizzo 	uint16_t etype;
1030*ad512958SBjoern A. Zeeb 	int ehdrlen = 0;
10312f345d8eSLuigi Rizzo 
10322f345d8eSLuigi Rizzo 	m = *mpp;
10332f345d8eSLuigi Rizzo 	*mss = m->m_pkthdr.tso_segsz;
10342f345d8eSLuigi Rizzo 
10352f345d8eSLuigi Rizzo 	if (M_WRITABLE(m) == 0) {
10362f345d8eSLuigi Rizzo 		m = m_dup(*mpp, M_DONTWAIT);
10372f345d8eSLuigi Rizzo 		if (!m)
10382f345d8eSLuigi Rizzo 			return NULL;
10392f345d8eSLuigi Rizzo 		m_freem(*mpp);
10402f345d8eSLuigi Rizzo 		*mpp = m;
10412f345d8eSLuigi Rizzo 	}
10422f345d8eSLuigi Rizzo 
10432f345d8eSLuigi Rizzo 	eh = mtod(m, struct ether_vlan_header *);
10442f345d8eSLuigi Rizzo 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
10452f345d8eSLuigi Rizzo 		etype = ntohs(eh->evl_proto);
10462f345d8eSLuigi Rizzo 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
10472f345d8eSLuigi Rizzo 	} else {
10482f345d8eSLuigi Rizzo 		etype = ntohs(eh->evl_encap_proto);
10492f345d8eSLuigi Rizzo 		ehdrlen = ETHER_HDR_LEN;
10502f345d8eSLuigi Rizzo 	}
10512f345d8eSLuigi Rizzo 
10522f345d8eSLuigi Rizzo 
10532f345d8eSLuigi Rizzo 	switch (etype) {
1054*ad512958SBjoern A. Zeeb #ifdef INET
10552f345d8eSLuigi Rizzo 	case ETHERTYPE_IP:
10562f345d8eSLuigi Rizzo 		ip = (struct ip *)(m->m_data + ehdrlen);
10572f345d8eSLuigi Rizzo 		if (ip->ip_p != IPPROTO_TCP)
10582f345d8eSLuigi Rizzo 			return NULL;
10592f345d8eSLuigi Rizzo 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
10602f345d8eSLuigi Rizzo 
10612f345d8eSLuigi Rizzo 		total_len = ehdrlen + (ip->ip_hl << 2) + (th->th_off << 2);
10622f345d8eSLuigi Rizzo 		break;
1063*ad512958SBjoern A. Zeeb #endif
1064*ad512958SBjoern A. Zeeb #ifdef INET6
10652f345d8eSLuigi Rizzo 	case ETHERTYPE_IPV6:
10662f345d8eSLuigi Rizzo 		ip6 = (struct ip6_hdr *)(m->m_data + ehdrlen);
10672f345d8eSLuigi Rizzo 		if (ip6->ip6_nxt != IPPROTO_TCP)
10682f345d8eSLuigi Rizzo 			return NULL;
10692f345d8eSLuigi Rizzo 		th = (struct tcphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
10702f345d8eSLuigi Rizzo 
10712f345d8eSLuigi Rizzo 		total_len = ehdrlen + sizeof(struct ip6_hdr) + (th->th_off << 2);
10722f345d8eSLuigi Rizzo 		break;
1073*ad512958SBjoern A. Zeeb #endif
10742f345d8eSLuigi Rizzo 	default:
10752f345d8eSLuigi Rizzo 		return NULL;
10762f345d8eSLuigi Rizzo 	}
10772f345d8eSLuigi Rizzo 
10782f345d8eSLuigi Rizzo 	m = m_pullup(m, total_len);
10792f345d8eSLuigi Rizzo 	if (!m)
10802f345d8eSLuigi Rizzo 		return NULL;
10812f345d8eSLuigi Rizzo 	*mpp = m;
10822f345d8eSLuigi Rizzo 	return m;
10832f345d8eSLuigi Rizzo 
10842f345d8eSLuigi Rizzo }
1085*ad512958SBjoern A. Zeeb #endif /* INET6 || INET */
10862f345d8eSLuigi Rizzo 
10872f345d8eSLuigi Rizzo 
10882f345d8eSLuigi Rizzo void
10892f345d8eSLuigi Rizzo oce_tx_task(void *arg, int npending)
10902f345d8eSLuigi Rizzo {
10912f345d8eSLuigi Rizzo 	struct oce_wq *wq = arg;
10922f345d8eSLuigi Rizzo 	POCE_SOFTC sc = wq->parent;
10932f345d8eSLuigi Rizzo 	struct ifnet *ifp = sc->ifp;
10942f345d8eSLuigi Rizzo 	int rc = 0;
10952f345d8eSLuigi Rizzo 
10962f345d8eSLuigi Rizzo #if __FreeBSD_version >= 800000
10972f345d8eSLuigi Rizzo 	if (TRY_LOCK(&wq->tx_lock)) {
10982f345d8eSLuigi Rizzo 		rc = oce_multiq_transmit(ifp, NULL, wq);
10992f345d8eSLuigi Rizzo 		if (rc) {
11002f345d8eSLuigi Rizzo 			device_printf(sc->dev,
11012f345d8eSLuigi Rizzo 			 "TX[%d] restart failed\n", wq->queue_index);
11022f345d8eSLuigi Rizzo 		}
11032f345d8eSLuigi Rizzo 		UNLOCK(&wq->tx_lock);
11042f345d8eSLuigi Rizzo 	}
11052f345d8eSLuigi Rizzo #else
11062f345d8eSLuigi Rizzo 	oce_start(ifp);
11072f345d8eSLuigi Rizzo #endif
11082f345d8eSLuigi Rizzo 
11092f345d8eSLuigi Rizzo }
11102f345d8eSLuigi Rizzo 
11112f345d8eSLuigi Rizzo 
11122f345d8eSLuigi Rizzo void
11132f345d8eSLuigi Rizzo oce_start(struct ifnet *ifp)
11142f345d8eSLuigi Rizzo {
11152f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ifp->if_softc;
11162f345d8eSLuigi Rizzo 	struct mbuf *m;
11172f345d8eSLuigi Rizzo 	int rc = 0;
11182f345d8eSLuigi Rizzo 
11192f345d8eSLuigi Rizzo 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
11202f345d8eSLuigi Rizzo 			IFF_DRV_RUNNING)
11212f345d8eSLuigi Rizzo 		return;
11222f345d8eSLuigi Rizzo 
11232f345d8eSLuigi Rizzo 	do {
11242f345d8eSLuigi Rizzo 		IF_DEQUEUE(&sc->ifp->if_snd, m);
11252f345d8eSLuigi Rizzo 		if (m == NULL)
11262f345d8eSLuigi Rizzo 			break;
11272f345d8eSLuigi Rizzo 		/* oce_start always uses default TX queue 0 */
11282f345d8eSLuigi Rizzo 		LOCK(&sc->wq[0]->tx_lock);
11292f345d8eSLuigi Rizzo 		rc = oce_tx(sc, &m, 0);
11302f345d8eSLuigi Rizzo 		UNLOCK(&sc->wq[0]->tx_lock);
11312f345d8eSLuigi Rizzo 		if (rc) {
11322f345d8eSLuigi Rizzo 			if (m != NULL) {
11332f345d8eSLuigi Rizzo 				sc->wq[0]->tx_stats.tx_stops ++;
11342f345d8eSLuigi Rizzo 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
11352f345d8eSLuigi Rizzo 				IFQ_DRV_PREPEND(&ifp->if_snd, m);
11362f345d8eSLuigi Rizzo 				m = NULL;
11372f345d8eSLuigi Rizzo 			}
11382f345d8eSLuigi Rizzo 			break;
11392f345d8eSLuigi Rizzo 		}
11402f345d8eSLuigi Rizzo 		if (m != NULL)
11412f345d8eSLuigi Rizzo 			ETHER_BPF_MTAP(ifp, m);
11422f345d8eSLuigi Rizzo 
11432f345d8eSLuigi Rizzo 	} while (1);
11442f345d8eSLuigi Rizzo 
11452f345d8eSLuigi Rizzo 	return;
11462f345d8eSLuigi Rizzo }
11472f345d8eSLuigi Rizzo 
11482f345d8eSLuigi Rizzo 
11492f345d8eSLuigi Rizzo /* Handle the Completion Queue for transmit */
11502f345d8eSLuigi Rizzo uint16_t
11512f345d8eSLuigi Rizzo oce_wq_handler(void *arg)
11522f345d8eSLuigi Rizzo {
11532f345d8eSLuigi Rizzo 	struct oce_wq *wq = (struct oce_wq *)arg;
11542f345d8eSLuigi Rizzo 	POCE_SOFTC sc = wq->parent;
11552f345d8eSLuigi Rizzo 	struct oce_cq *cq = wq->cq;
11562f345d8eSLuigi Rizzo 	struct oce_nic_tx_cqe *cqe;
11572f345d8eSLuigi Rizzo 	int num_cqes = 0;
11582f345d8eSLuigi Rizzo 
11592f345d8eSLuigi Rizzo 	LOCK(&wq->tx_lock);
11602f345d8eSLuigi Rizzo 	bus_dmamap_sync(cq->ring->dma.tag,
11612f345d8eSLuigi Rizzo 			cq->ring->dma.map, BUS_DMASYNC_POSTWRITE);
11622f345d8eSLuigi Rizzo 	cqe = RING_GET_CONSUMER_ITEM_VA(cq->ring, struct oce_nic_tx_cqe);
11632f345d8eSLuigi Rizzo 	while (cqe->u0.dw[3]) {
11642f345d8eSLuigi Rizzo 		DW_SWAP((uint32_t *) cqe, sizeof(oce_wq_cqe));
11652f345d8eSLuigi Rizzo 
11662f345d8eSLuigi Rizzo 		wq->ring->cidx = cqe->u0.s.wqe_index + 1;
11672f345d8eSLuigi Rizzo 		if (wq->ring->cidx >= wq->ring->num_items)
11682f345d8eSLuigi Rizzo 			wq->ring->cidx -= wq->ring->num_items;
11692f345d8eSLuigi Rizzo 
11702f345d8eSLuigi Rizzo 		oce_tx_complete(wq, cqe->u0.s.wqe_index, cqe->u0.s.status);
11712f345d8eSLuigi Rizzo 		wq->tx_stats.tx_compl++;
11722f345d8eSLuigi Rizzo 		cqe->u0.dw[3] = 0;
11732f345d8eSLuigi Rizzo 		RING_GET(cq->ring, 1);
11742f345d8eSLuigi Rizzo 		bus_dmamap_sync(cq->ring->dma.tag,
11752f345d8eSLuigi Rizzo 				cq->ring->dma.map, BUS_DMASYNC_POSTWRITE);
11762f345d8eSLuigi Rizzo 		cqe =
11772f345d8eSLuigi Rizzo 		    RING_GET_CONSUMER_ITEM_VA(cq->ring, struct oce_nic_tx_cqe);
11782f345d8eSLuigi Rizzo 		num_cqes++;
11792f345d8eSLuigi Rizzo 	}
11802f345d8eSLuigi Rizzo 
11812f345d8eSLuigi Rizzo 	if (num_cqes)
11822f345d8eSLuigi Rizzo 		oce_arm_cq(sc, cq->cq_id, num_cqes, FALSE);
11832f345d8eSLuigi Rizzo 	UNLOCK(&wq->tx_lock);
11842f345d8eSLuigi Rizzo 
11852f345d8eSLuigi Rizzo 	return 0;
11862f345d8eSLuigi Rizzo }
11872f345d8eSLuigi Rizzo 
11882f345d8eSLuigi Rizzo 
11892f345d8eSLuigi Rizzo static int
11902f345d8eSLuigi Rizzo oce_multiq_transmit(struct ifnet *ifp, struct mbuf *m, struct oce_wq *wq)
11912f345d8eSLuigi Rizzo {
11922f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ifp->if_softc;
11932f345d8eSLuigi Rizzo 	int status = 0, queue_index = 0;
11942f345d8eSLuigi Rizzo 	struct mbuf *next = NULL;
11952f345d8eSLuigi Rizzo 	struct buf_ring *br = NULL;
11962f345d8eSLuigi Rizzo 
11972f345d8eSLuigi Rizzo 	br  = wq->br;
11982f345d8eSLuigi Rizzo 	queue_index = wq->queue_index;
11992f345d8eSLuigi Rizzo 
12002f345d8eSLuigi Rizzo 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
12012f345d8eSLuigi Rizzo 		IFF_DRV_RUNNING) {
12022f345d8eSLuigi Rizzo 		if (m != NULL)
12032f345d8eSLuigi Rizzo 			status = drbr_enqueue(ifp, br, m);
12042f345d8eSLuigi Rizzo 		return status;
12052f345d8eSLuigi Rizzo 	}
12062f345d8eSLuigi Rizzo 
12072f345d8eSLuigi Rizzo 	if (m == NULL)
12082f345d8eSLuigi Rizzo 		next = drbr_dequeue(ifp, br);
12092f345d8eSLuigi Rizzo 	else if (drbr_needs_enqueue(ifp, br)) {
12102f345d8eSLuigi Rizzo 		if ((status = drbr_enqueue(ifp, br, m)) != 0)
12112f345d8eSLuigi Rizzo 			return status;
12122f345d8eSLuigi Rizzo 		next = drbr_dequeue(ifp, br);
12132f345d8eSLuigi Rizzo 	} else
12142f345d8eSLuigi Rizzo 		next = m;
12152f345d8eSLuigi Rizzo 
12162f345d8eSLuigi Rizzo 	while (next != NULL) {
12172f345d8eSLuigi Rizzo 		if (oce_tx(sc, &next, queue_index)) {
12182f345d8eSLuigi Rizzo 			if (next != NULL) {
12192f345d8eSLuigi Rizzo 				wq->tx_stats.tx_stops ++;
12202f345d8eSLuigi Rizzo 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
12212f345d8eSLuigi Rizzo 				status = drbr_enqueue(ifp, br, next);
12222f345d8eSLuigi Rizzo 			}
12232f345d8eSLuigi Rizzo 			break;
12242f345d8eSLuigi Rizzo 		}
12252f345d8eSLuigi Rizzo 		drbr_stats_update(ifp, next->m_pkthdr.len, next->m_flags);
12262f345d8eSLuigi Rizzo 		ETHER_BPF_MTAP(ifp, next);
12272f345d8eSLuigi Rizzo 		next = drbr_dequeue(ifp, br);
12282f345d8eSLuigi Rizzo 	}
12292f345d8eSLuigi Rizzo 
12302f345d8eSLuigi Rizzo 	return status;
12312f345d8eSLuigi Rizzo }
12322f345d8eSLuigi Rizzo 
12332f345d8eSLuigi Rizzo 
12342f345d8eSLuigi Rizzo 
12352f345d8eSLuigi Rizzo 
12362f345d8eSLuigi Rizzo /*****************************************************************************
12372f345d8eSLuigi Rizzo  *			    Receive  routines functions 		     *
12382f345d8eSLuigi Rizzo  *****************************************************************************/
12392f345d8eSLuigi Rizzo 
12402f345d8eSLuigi Rizzo static void
12412f345d8eSLuigi Rizzo oce_rx(struct oce_rq *rq, uint32_t rqe_idx, struct oce_nic_rx_cqe *cqe)
12422f345d8eSLuigi Rizzo {
12432f345d8eSLuigi Rizzo 	uint32_t out;
12442f345d8eSLuigi Rizzo 	struct oce_packet_desc *pd;
12452f345d8eSLuigi Rizzo 	POCE_SOFTC sc = (POCE_SOFTC) rq->parent;
12462f345d8eSLuigi Rizzo 	int i, len, frag_len;
12472f345d8eSLuigi Rizzo 	struct mbuf *m = NULL, *tail = NULL;
12482f345d8eSLuigi Rizzo 	uint16_t vtag;
12492f345d8eSLuigi Rizzo 
12502f345d8eSLuigi Rizzo 	len = cqe->u0.s.pkt_size;
12512f345d8eSLuigi Rizzo 	vtag = cqe->u0.s.vlan_tag;
12522f345d8eSLuigi Rizzo 	if (!len) {
12532f345d8eSLuigi Rizzo 		/*partial DMA workaround for Lancer*/
12542f345d8eSLuigi Rizzo 		oce_discard_rx_comp(rq, cqe);
12552f345d8eSLuigi Rizzo 		goto exit;
12562f345d8eSLuigi Rizzo 	}
12572f345d8eSLuigi Rizzo 
12582f345d8eSLuigi Rizzo 	for (i = 0; i < cqe->u0.s.num_fragments; i++) {
12592f345d8eSLuigi Rizzo 
12602f345d8eSLuigi Rizzo 		if (rq->packets_out == rq->packets_in) {
12612f345d8eSLuigi Rizzo 			device_printf(sc->dev,
12622f345d8eSLuigi Rizzo 				  "RQ transmit descriptor missing\n");
12632f345d8eSLuigi Rizzo 		}
12642f345d8eSLuigi Rizzo 		out = rq->packets_out + 1;
12652f345d8eSLuigi Rizzo 		if (out == OCE_RQ_PACKET_ARRAY_SIZE)
12662f345d8eSLuigi Rizzo 			out = 0;
12672f345d8eSLuigi Rizzo 		pd = &rq->pckts[rq->packets_out];
12682f345d8eSLuigi Rizzo 		rq->packets_out = out;
12692f345d8eSLuigi Rizzo 
12702f345d8eSLuigi Rizzo 		bus_dmamap_sync(rq->tag, pd->map, BUS_DMASYNC_POSTWRITE);
12712f345d8eSLuigi Rizzo 		bus_dmamap_unload(rq->tag, pd->map);
12722f345d8eSLuigi Rizzo 		rq->pending--;
12732f345d8eSLuigi Rizzo 
12742f345d8eSLuigi Rizzo 		frag_len = (len > rq->cfg.frag_size) ? rq->cfg.frag_size : len;
12752f345d8eSLuigi Rizzo 		pd->mbuf->m_len = frag_len;
12762f345d8eSLuigi Rizzo 
12772f345d8eSLuigi Rizzo 		if (tail != NULL) {
12782f345d8eSLuigi Rizzo 			/* additional fragments */
12792f345d8eSLuigi Rizzo 			pd->mbuf->m_flags &= ~M_PKTHDR;
12802f345d8eSLuigi Rizzo 			tail->m_next = pd->mbuf;
12812f345d8eSLuigi Rizzo 			tail = pd->mbuf;
12822f345d8eSLuigi Rizzo 		} else {
12832f345d8eSLuigi Rizzo 			/* first fragment, fill out much of the packet header */
12842f345d8eSLuigi Rizzo 			pd->mbuf->m_pkthdr.len = len;
12852f345d8eSLuigi Rizzo 			pd->mbuf->m_pkthdr.csum_flags = 0;
12862f345d8eSLuigi Rizzo 			if (IF_CSUM_ENABLED(sc)) {
12872f345d8eSLuigi Rizzo 				if (cqe->u0.s.l4_cksum_pass) {
12882f345d8eSLuigi Rizzo 					pd->mbuf->m_pkthdr.csum_flags |=
12892f345d8eSLuigi Rizzo 					    (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
12902f345d8eSLuigi Rizzo 					pd->mbuf->m_pkthdr.csum_data = 0xffff;
12912f345d8eSLuigi Rizzo 				}
12922f345d8eSLuigi Rizzo 				if (cqe->u0.s.ip_cksum_pass) {
12932f345d8eSLuigi Rizzo 					if (!cqe->u0.s.ip_ver) { //IPV4
12942f345d8eSLuigi Rizzo 						pd->mbuf->m_pkthdr.csum_flags |=
12952f345d8eSLuigi Rizzo 						(CSUM_IP_CHECKED|CSUM_IP_VALID);
12962f345d8eSLuigi Rizzo 					}
12972f345d8eSLuigi Rizzo 				}
12982f345d8eSLuigi Rizzo 			}
12992f345d8eSLuigi Rizzo 			m = tail = pd->mbuf;
13002f345d8eSLuigi Rizzo 		}
13012f345d8eSLuigi Rizzo 		pd->mbuf = NULL;
13022f345d8eSLuigi Rizzo 		len -= frag_len;
13032f345d8eSLuigi Rizzo 	}
13042f345d8eSLuigi Rizzo 
13052f345d8eSLuigi Rizzo 	if (m) {
13062f345d8eSLuigi Rizzo 		if (!oce_cqe_portid_valid(sc, cqe)) {
13072f345d8eSLuigi Rizzo 			 m_freem(m);
13082f345d8eSLuigi Rizzo 			 goto exit;
13092f345d8eSLuigi Rizzo 		}
13102f345d8eSLuigi Rizzo 
13112f345d8eSLuigi Rizzo 		m->m_pkthdr.rcvif = sc->ifp;
13122f345d8eSLuigi Rizzo #if __FreeBSD_version >= 800000
13132f345d8eSLuigi Rizzo 		m->m_pkthdr.flowid = rq->queue_index;
13142f345d8eSLuigi Rizzo 		m->m_flags |= M_FLOWID;
13152f345d8eSLuigi Rizzo #endif
13162f345d8eSLuigi Rizzo 		//This deternies if vlan tag is present
13172f345d8eSLuigi Rizzo 		if (oce_cqe_vtp_valid(sc, cqe)) {
13182f345d8eSLuigi Rizzo 			if (sc->function_mode & FNM_FLEX10_MODE) {
13192f345d8eSLuigi Rizzo 				/* FLEX10 */
13202f345d8eSLuigi Rizzo 				if (cqe->u0.s.qnq) {
13212f345d8eSLuigi Rizzo 					/* If QnQ is not set, neglect VLAN */
13222f345d8eSLuigi Rizzo 					if (IS_BE(sc))
13232f345d8eSLuigi Rizzo 						m->m_pkthdr.ether_vtag =
13242f345d8eSLuigi Rizzo 								BSWAP_16(vtag);
13252f345d8eSLuigi Rizzo 					else
13262f345d8eSLuigi Rizzo 						m->m_pkthdr.ether_vtag = vtag;
13272f345d8eSLuigi Rizzo 					m->m_flags |= M_VLANTAG;
13282f345d8eSLuigi Rizzo 				}
13292f345d8eSLuigi Rizzo 			} else {
13302f345d8eSLuigi Rizzo 				if (IS_BE(sc))
13312f345d8eSLuigi Rizzo 					m->m_pkthdr.ether_vtag = BSWAP_16(vtag);
13322f345d8eSLuigi Rizzo 				else
13332f345d8eSLuigi Rizzo 					m->m_pkthdr.ether_vtag = vtag;
13342f345d8eSLuigi Rizzo 				m->m_flags |= M_VLANTAG;
13352f345d8eSLuigi Rizzo 			}
13362f345d8eSLuigi Rizzo 		}
13372f345d8eSLuigi Rizzo 
13382f345d8eSLuigi Rizzo 		sc->ifp->if_ipackets++;
1339*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
13402f345d8eSLuigi Rizzo 		/* Try to queue to LRO */
13412f345d8eSLuigi Rizzo 		if (IF_LRO_ENABLED(sc) &&
13422f345d8eSLuigi Rizzo 		    !(m->m_flags & M_VLANTAG) &&
13432f345d8eSLuigi Rizzo 		    (cqe->u0.s.ip_cksum_pass) &&
13442f345d8eSLuigi Rizzo 		    (cqe->u0.s.l4_cksum_pass) &&
13452f345d8eSLuigi Rizzo 		    (!cqe->u0.s.ip_ver)       &&
13462f345d8eSLuigi Rizzo 		    (rq->lro.lro_cnt != 0)) {
13472f345d8eSLuigi Rizzo 
13482f345d8eSLuigi Rizzo 			if (tcp_lro_rx(&rq->lro, m, 0) == 0) {
13492f345d8eSLuigi Rizzo 				rq->lro_pkts_queued ++;
13502f345d8eSLuigi Rizzo 				goto post_done;
13512f345d8eSLuigi Rizzo 			}
13522f345d8eSLuigi Rizzo 			/* If LRO posting fails then try to post to STACK */
13532f345d8eSLuigi Rizzo 		}
1354*ad512958SBjoern A. Zeeb #endif
13552f345d8eSLuigi Rizzo 
13562f345d8eSLuigi Rizzo 		(*sc->ifp->if_input) (sc->ifp, m);
1357*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
13582f345d8eSLuigi Rizzo post_done:
1359*ad512958SBjoern A. Zeeb #endif
13602f345d8eSLuigi Rizzo 		/* Update rx stats per queue */
13612f345d8eSLuigi Rizzo 		rq->rx_stats.rx_pkts++;
13622f345d8eSLuigi Rizzo 		rq->rx_stats.rx_bytes += cqe->u0.s.pkt_size;
13632f345d8eSLuigi Rizzo 		rq->rx_stats.rx_frags += cqe->u0.s.num_fragments;
13642f345d8eSLuigi Rizzo 		if (cqe->u0.s.pkt_type == OCE_MULTICAST_PACKET)
13652f345d8eSLuigi Rizzo 			rq->rx_stats.rx_mcast_pkts++;
13662f345d8eSLuigi Rizzo 		if (cqe->u0.s.pkt_type == OCE_UNICAST_PACKET)
13672f345d8eSLuigi Rizzo 			rq->rx_stats.rx_ucast_pkts++;
13682f345d8eSLuigi Rizzo 	}
13692f345d8eSLuigi Rizzo exit:
13702f345d8eSLuigi Rizzo 	return;
13712f345d8eSLuigi Rizzo }
13722f345d8eSLuigi Rizzo 
13732f345d8eSLuigi Rizzo 
13742f345d8eSLuigi Rizzo static void
13752f345d8eSLuigi Rizzo oce_discard_rx_comp(struct oce_rq *rq, struct oce_nic_rx_cqe *cqe)
13762f345d8eSLuigi Rizzo {
13772f345d8eSLuigi Rizzo 	uint32_t out, i = 0;
13782f345d8eSLuigi Rizzo 	struct oce_packet_desc *pd;
13792f345d8eSLuigi Rizzo 	POCE_SOFTC sc = (POCE_SOFTC) rq->parent;
13802f345d8eSLuigi Rizzo 	int num_frags = cqe->u0.s.num_fragments;
13812f345d8eSLuigi Rizzo 
13822f345d8eSLuigi Rizzo 	if (IS_XE201(sc) && cqe->u0.s.error) {
13832f345d8eSLuigi Rizzo 		/* Lancer A0 workaround
13842f345d8eSLuigi Rizzo 		* num_frags will be 1 more than actual in case of error
13852f345d8eSLuigi Rizzo 		 */
13862f345d8eSLuigi Rizzo 		if (num_frags)
13872f345d8eSLuigi Rizzo 			num_frags -= 1;
13882f345d8eSLuigi Rizzo 	}
13892f345d8eSLuigi Rizzo 	for (i = 0; i < num_frags; i++) {
13902f345d8eSLuigi Rizzo 		if (rq->packets_out == rq->packets_in) {
13912f345d8eSLuigi Rizzo 			device_printf(sc->dev,
13922f345d8eSLuigi Rizzo 				"RQ transmit descriptor missing\n");
13932f345d8eSLuigi Rizzo 		}
13942f345d8eSLuigi Rizzo 		out = rq->packets_out + 1;
13952f345d8eSLuigi Rizzo 		if (out == OCE_RQ_PACKET_ARRAY_SIZE)
13962f345d8eSLuigi Rizzo 			out = 0;
13972f345d8eSLuigi Rizzo 		pd = &rq->pckts[rq->packets_out];
13982f345d8eSLuigi Rizzo 		rq->packets_out = out;
13992f345d8eSLuigi Rizzo 
14002f345d8eSLuigi Rizzo 		bus_dmamap_sync(rq->tag, pd->map, BUS_DMASYNC_POSTWRITE);
14012f345d8eSLuigi Rizzo 		bus_dmamap_unload(rq->tag, pd->map);
14022f345d8eSLuigi Rizzo 		rq->pending--;
14032f345d8eSLuigi Rizzo 		m_freem(pd->mbuf);
14042f345d8eSLuigi Rizzo 	}
14052f345d8eSLuigi Rizzo 
14062f345d8eSLuigi Rizzo }
14072f345d8eSLuigi Rizzo 
14082f345d8eSLuigi Rizzo 
14092f345d8eSLuigi Rizzo static int
14102f345d8eSLuigi Rizzo oce_cqe_vtp_valid(POCE_SOFTC sc, struct oce_nic_rx_cqe *cqe)
14112f345d8eSLuigi Rizzo {
14122f345d8eSLuigi Rizzo 	struct oce_nic_rx_cqe_v1 *cqe_v1;
14132f345d8eSLuigi Rizzo 	int vtp = 0;
14142f345d8eSLuigi Rizzo 
14152f345d8eSLuigi Rizzo 	if (sc->be3_native) {
14162f345d8eSLuigi Rizzo 		cqe_v1 = (struct oce_nic_rx_cqe_v1 *)cqe;
14172f345d8eSLuigi Rizzo 		vtp =  cqe_v1->u0.s.vlan_tag_present;
14182f345d8eSLuigi Rizzo 	} else {
14192f345d8eSLuigi Rizzo 		vtp = cqe->u0.s.vlan_tag_present;
14202f345d8eSLuigi Rizzo 	}
14212f345d8eSLuigi Rizzo 
14222f345d8eSLuigi Rizzo 	return vtp;
14232f345d8eSLuigi Rizzo 
14242f345d8eSLuigi Rizzo }
14252f345d8eSLuigi Rizzo 
14262f345d8eSLuigi Rizzo 
14272f345d8eSLuigi Rizzo static int
14282f345d8eSLuigi Rizzo oce_cqe_portid_valid(POCE_SOFTC sc, struct oce_nic_rx_cqe *cqe)
14292f345d8eSLuigi Rizzo {
14302f345d8eSLuigi Rizzo 	struct oce_nic_rx_cqe_v1 *cqe_v1;
14312f345d8eSLuigi Rizzo 	int port_id = 0;
14322f345d8eSLuigi Rizzo 
14332f345d8eSLuigi Rizzo 	if (sc->be3_native && IS_BE(sc)) {
14342f345d8eSLuigi Rizzo 		cqe_v1 = (struct oce_nic_rx_cqe_v1 *)cqe;
14352f345d8eSLuigi Rizzo 		port_id =  cqe_v1->u0.s.port;
14362f345d8eSLuigi Rizzo 		if (sc->port_id != port_id)
14372f345d8eSLuigi Rizzo 			return 0;
14382f345d8eSLuigi Rizzo 	} else
14392f345d8eSLuigi Rizzo 		;/* For BE3 legacy and Lancer this is dummy */
14402f345d8eSLuigi Rizzo 
14412f345d8eSLuigi Rizzo 	return 1;
14422f345d8eSLuigi Rizzo 
14432f345d8eSLuigi Rizzo }
14442f345d8eSLuigi Rizzo 
14452f345d8eSLuigi Rizzo 
1446*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
14472f345d8eSLuigi Rizzo static void
14482f345d8eSLuigi Rizzo oce_rx_flush_lro(struct oce_rq *rq)
14492f345d8eSLuigi Rizzo {
14502f345d8eSLuigi Rizzo 	struct lro_ctrl	*lro = &rq->lro;
14512f345d8eSLuigi Rizzo 	struct lro_entry *queued;
14522f345d8eSLuigi Rizzo 	POCE_SOFTC sc = (POCE_SOFTC) rq->parent;
14532f345d8eSLuigi Rizzo 
14542f345d8eSLuigi Rizzo 	if (!IF_LRO_ENABLED(sc))
14552f345d8eSLuigi Rizzo 		return;
14562f345d8eSLuigi Rizzo 
14572f345d8eSLuigi Rizzo 	while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
14582f345d8eSLuigi Rizzo 		SLIST_REMOVE_HEAD(&lro->lro_active, next);
14592f345d8eSLuigi Rizzo 		tcp_lro_flush(lro, queued);
14602f345d8eSLuigi Rizzo 	}
14612f345d8eSLuigi Rizzo 	rq->lro_pkts_queued = 0;
14622f345d8eSLuigi Rizzo 
14632f345d8eSLuigi Rizzo 	return;
14642f345d8eSLuigi Rizzo }
14652f345d8eSLuigi Rizzo 
14662f345d8eSLuigi Rizzo 
14672f345d8eSLuigi Rizzo static int
14682f345d8eSLuigi Rizzo oce_init_lro(POCE_SOFTC sc)
14692f345d8eSLuigi Rizzo {
14702f345d8eSLuigi Rizzo 	struct lro_ctrl *lro = NULL;
14712f345d8eSLuigi Rizzo 	int i = 0, rc = 0;
14722f345d8eSLuigi Rizzo 
14732f345d8eSLuigi Rizzo 	for (i = 0; i < sc->nrqs; i++) {
14742f345d8eSLuigi Rizzo 		lro = &sc->rq[i]->lro;
14752f345d8eSLuigi Rizzo 		rc = tcp_lro_init(lro);
14762f345d8eSLuigi Rizzo 		if (rc != 0) {
14772f345d8eSLuigi Rizzo 			device_printf(sc->dev, "LRO init failed\n");
14782f345d8eSLuigi Rizzo 			return rc;
14792f345d8eSLuigi Rizzo 		}
14802f345d8eSLuigi Rizzo 		lro->ifp = sc->ifp;
14812f345d8eSLuigi Rizzo 	}
14822f345d8eSLuigi Rizzo 
14832f345d8eSLuigi Rizzo 	return rc;
14842f345d8eSLuigi Rizzo }
1485*ad512958SBjoern A. Zeeb #endif /* INET6 || INET */
14862f345d8eSLuigi Rizzo 
14872f345d8eSLuigi Rizzo void
14882f345d8eSLuigi Rizzo oce_free_lro(POCE_SOFTC sc)
14892f345d8eSLuigi Rizzo {
1490*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
14912f345d8eSLuigi Rizzo 	struct lro_ctrl *lro = NULL;
14922f345d8eSLuigi Rizzo 	int i = 0;
14932f345d8eSLuigi Rizzo 
14942f345d8eSLuigi Rizzo 	for (i = 0; i < sc->nrqs; i++) {
14952f345d8eSLuigi Rizzo 		lro = &sc->rq[i]->lro;
14962f345d8eSLuigi Rizzo 		if (lro)
14972f345d8eSLuigi Rizzo 			tcp_lro_free(lro);
14982f345d8eSLuigi Rizzo 	}
1499*ad512958SBjoern A. Zeeb #endif
15002f345d8eSLuigi Rizzo }
15012f345d8eSLuigi Rizzo 
15022f345d8eSLuigi Rizzo 
15032f345d8eSLuigi Rizzo int
15042f345d8eSLuigi Rizzo oce_alloc_rx_bufs(struct oce_rq *rq, int count)
15052f345d8eSLuigi Rizzo {
15062f345d8eSLuigi Rizzo 	POCE_SOFTC sc = (POCE_SOFTC) rq->parent;
15072f345d8eSLuigi Rizzo 	int i, in, rc;
15082f345d8eSLuigi Rizzo 	struct oce_packet_desc *pd;
15092f345d8eSLuigi Rizzo 	bus_dma_segment_t segs[6];
15102f345d8eSLuigi Rizzo 	int nsegs, added = 0;
15112f345d8eSLuigi Rizzo 	struct oce_nic_rqe *rqe;
15122f345d8eSLuigi Rizzo 	pd_rxulp_db_t rxdb_reg;
15132f345d8eSLuigi Rizzo 
15142f345d8eSLuigi Rizzo 
15152f345d8eSLuigi Rizzo 	for (i = 0; i < count; i++) {
15162f345d8eSLuigi Rizzo 		in = rq->packets_in + 1;
15172f345d8eSLuigi Rizzo 		if (in == OCE_RQ_PACKET_ARRAY_SIZE)
15182f345d8eSLuigi Rizzo 			in = 0;
15192f345d8eSLuigi Rizzo 		if (in == rq->packets_out)
15202f345d8eSLuigi Rizzo 			break;	/* no more room */
15212f345d8eSLuigi Rizzo 
15222f345d8eSLuigi Rizzo 		pd = &rq->pckts[rq->packets_in];
15232f345d8eSLuigi Rizzo 		pd->mbuf = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
15242f345d8eSLuigi Rizzo 		if (pd->mbuf == NULL)
15252f345d8eSLuigi Rizzo 			break;
15262f345d8eSLuigi Rizzo 
15272f345d8eSLuigi Rizzo 		pd->mbuf->m_len = pd->mbuf->m_pkthdr.len = MCLBYTES;
15282f345d8eSLuigi Rizzo 		rc = bus_dmamap_load_mbuf_sg(rq->tag,
15292f345d8eSLuigi Rizzo 					     pd->map,
15302f345d8eSLuigi Rizzo 					     pd->mbuf,
15312f345d8eSLuigi Rizzo 					     segs, &nsegs, BUS_DMA_NOWAIT);
15322f345d8eSLuigi Rizzo 		if (rc) {
15332f345d8eSLuigi Rizzo 			m_free(pd->mbuf);
15342f345d8eSLuigi Rizzo 			break;
15352f345d8eSLuigi Rizzo 		}
15362f345d8eSLuigi Rizzo 
15372f345d8eSLuigi Rizzo 		if (nsegs != 1) {
15382f345d8eSLuigi Rizzo 			i--;
15392f345d8eSLuigi Rizzo 			continue;
15402f345d8eSLuigi Rizzo 		}
15412f345d8eSLuigi Rizzo 
15422f345d8eSLuigi Rizzo 		rq->packets_in = in;
15432f345d8eSLuigi Rizzo 		bus_dmamap_sync(rq->tag, pd->map, BUS_DMASYNC_PREREAD);
15442f345d8eSLuigi Rizzo 
15452f345d8eSLuigi Rizzo 		rqe = RING_GET_PRODUCER_ITEM_VA(rq->ring, struct oce_nic_rqe);
15462f345d8eSLuigi Rizzo 		rqe->u0.s.frag_pa_hi = ADDR_HI(segs[0].ds_addr);
15472f345d8eSLuigi Rizzo 		rqe->u0.s.frag_pa_lo = ADDR_LO(segs[0].ds_addr);
15482f345d8eSLuigi Rizzo 		DW_SWAP(u32ptr(rqe), sizeof(struct oce_nic_rqe));
15492f345d8eSLuigi Rizzo 		RING_PUT(rq->ring, 1);
15502f345d8eSLuigi Rizzo 		added++;
15512f345d8eSLuigi Rizzo 		rq->pending++;
15522f345d8eSLuigi Rizzo 	}
15532f345d8eSLuigi Rizzo 	if (added != 0) {
15542f345d8eSLuigi Rizzo 		for (i = added / OCE_MAX_RQ_POSTS; i > 0; i--) {
15552f345d8eSLuigi Rizzo 			DELAY(1);
15562f345d8eSLuigi Rizzo 			rxdb_reg.bits.num_posted = OCE_MAX_RQ_POSTS;
15572f345d8eSLuigi Rizzo 			rxdb_reg.bits.qid = rq->rq_id;
15582f345d8eSLuigi Rizzo 			OCE_WRITE_REG32(sc, db, PD_RXULP_DB, rxdb_reg.dw0);
15592f345d8eSLuigi Rizzo 			added -= OCE_MAX_RQ_POSTS;
15602f345d8eSLuigi Rizzo 		}
15612f345d8eSLuigi Rizzo 		if (added > 0) {
15622f345d8eSLuigi Rizzo 			DELAY(1);
15632f345d8eSLuigi Rizzo 			rxdb_reg.bits.qid = rq->rq_id;
15642f345d8eSLuigi Rizzo 			rxdb_reg.bits.num_posted = added;
15652f345d8eSLuigi Rizzo 			OCE_WRITE_REG32(sc, db, PD_RXULP_DB, rxdb_reg.dw0);
15662f345d8eSLuigi Rizzo 		}
15672f345d8eSLuigi Rizzo 	}
15682f345d8eSLuigi Rizzo 
15692f345d8eSLuigi Rizzo 	return 0;
15702f345d8eSLuigi Rizzo }
15712f345d8eSLuigi Rizzo 
15722f345d8eSLuigi Rizzo 
15732f345d8eSLuigi Rizzo /* Handle the Completion Queue for receive */
15742f345d8eSLuigi Rizzo uint16_t
15752f345d8eSLuigi Rizzo oce_rq_handler(void *arg)
15762f345d8eSLuigi Rizzo {
15772f345d8eSLuigi Rizzo 	struct oce_rq *rq = (struct oce_rq *)arg;
15782f345d8eSLuigi Rizzo 	struct oce_cq *cq = rq->cq;
15792f345d8eSLuigi Rizzo 	POCE_SOFTC sc = rq->parent;
15802f345d8eSLuigi Rizzo 	struct oce_nic_rx_cqe *cqe;
15812f345d8eSLuigi Rizzo 	int num_cqes = 0, rq_buffers_used = 0;
15822f345d8eSLuigi Rizzo 
15832f345d8eSLuigi Rizzo 
15842f345d8eSLuigi Rizzo 	LOCK(&rq->rx_lock);
15852f345d8eSLuigi Rizzo 	bus_dmamap_sync(cq->ring->dma.tag,
15862f345d8eSLuigi Rizzo 			cq->ring->dma.map, BUS_DMASYNC_POSTWRITE);
15872f345d8eSLuigi Rizzo 	cqe = RING_GET_CONSUMER_ITEM_VA(cq->ring, struct oce_nic_rx_cqe);
15882f345d8eSLuigi Rizzo 	while (cqe->u0.dw[2]) {
15892f345d8eSLuigi Rizzo 		DW_SWAP((uint32_t *) cqe, sizeof(oce_rq_cqe));
15902f345d8eSLuigi Rizzo 
15912f345d8eSLuigi Rizzo 		RING_GET(rq->ring, 1);
15922f345d8eSLuigi Rizzo 		if (cqe->u0.s.error == 0) {
15932f345d8eSLuigi Rizzo 			oce_rx(rq, cqe->u0.s.frag_index, cqe);
15942f345d8eSLuigi Rizzo 		} else {
15952f345d8eSLuigi Rizzo 			rq->rx_stats.rxcp_err++;
15962f345d8eSLuigi Rizzo 			sc->ifp->if_ierrors++;
15972f345d8eSLuigi Rizzo 			if (IS_XE201(sc))
15982f345d8eSLuigi Rizzo 				/* Lancer A0 no buffer workaround */
15992f345d8eSLuigi Rizzo 				oce_discard_rx_comp(rq, cqe);
16002f345d8eSLuigi Rizzo 			else
16012f345d8eSLuigi Rizzo 				/* Post L3/L4 errors to stack.*/
16022f345d8eSLuigi Rizzo 				oce_rx(rq, cqe->u0.s.frag_index, cqe);
16032f345d8eSLuigi Rizzo 
16042f345d8eSLuigi Rizzo 		}
16052f345d8eSLuigi Rizzo 		rq->rx_stats.rx_compl++;
16062f345d8eSLuigi Rizzo 		cqe->u0.dw[2] = 0;
16072f345d8eSLuigi Rizzo 
1608*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
16092f345d8eSLuigi Rizzo 		if (IF_LRO_ENABLED(sc) && rq->lro_pkts_queued >= 16) {
16102f345d8eSLuigi Rizzo 			oce_rx_flush_lro(rq);
16112f345d8eSLuigi Rizzo 		}
1612*ad512958SBjoern A. Zeeb #endif
16132f345d8eSLuigi Rizzo 
16142f345d8eSLuigi Rizzo 		RING_GET(cq->ring, 1);
16152f345d8eSLuigi Rizzo 		bus_dmamap_sync(cq->ring->dma.tag,
16162f345d8eSLuigi Rizzo 				cq->ring->dma.map, BUS_DMASYNC_POSTWRITE);
16172f345d8eSLuigi Rizzo 		cqe =
16182f345d8eSLuigi Rizzo 		    RING_GET_CONSUMER_ITEM_VA(cq->ring, struct oce_nic_rx_cqe);
16192f345d8eSLuigi Rizzo 		num_cqes++;
16202f345d8eSLuigi Rizzo 		if (num_cqes >= (IS_XE201(sc) ? 8 : oce_max_rsp_handled))
16212f345d8eSLuigi Rizzo 			break;
16222f345d8eSLuigi Rizzo 	}
1623*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
16242f345d8eSLuigi Rizzo 	if (IF_LRO_ENABLED(sc))
16252f345d8eSLuigi Rizzo 		oce_rx_flush_lro(rq);
1626*ad512958SBjoern A. Zeeb #endif
16272f345d8eSLuigi Rizzo 
16282f345d8eSLuigi Rizzo 	if (num_cqes) {
16292f345d8eSLuigi Rizzo 		oce_arm_cq(sc, cq->cq_id, num_cqes, FALSE);
16302f345d8eSLuigi Rizzo 		rq_buffers_used = OCE_RQ_PACKET_ARRAY_SIZE - rq->pending;
16312f345d8eSLuigi Rizzo 		if (rq_buffers_used > 1)
16322f345d8eSLuigi Rizzo 			oce_alloc_rx_bufs(rq, (rq_buffers_used - 1));
16332f345d8eSLuigi Rizzo 	}
16342f345d8eSLuigi Rizzo 
16352f345d8eSLuigi Rizzo 	UNLOCK(&rq->rx_lock);
16362f345d8eSLuigi Rizzo 
16372f345d8eSLuigi Rizzo 	return 0;
16382f345d8eSLuigi Rizzo 
16392f345d8eSLuigi Rizzo }
16402f345d8eSLuigi Rizzo 
16412f345d8eSLuigi Rizzo 
16422f345d8eSLuigi Rizzo 
16432f345d8eSLuigi Rizzo 
16442f345d8eSLuigi Rizzo /*****************************************************************************
16452f345d8eSLuigi Rizzo  *		   Helper function prototypes in this file 		     *
16462f345d8eSLuigi Rizzo  *****************************************************************************/
16472f345d8eSLuigi Rizzo 
16482f345d8eSLuigi Rizzo static int
16492f345d8eSLuigi Rizzo oce_attach_ifp(POCE_SOFTC sc)
16502f345d8eSLuigi Rizzo {
16512f345d8eSLuigi Rizzo 
16522f345d8eSLuigi Rizzo 	sc->ifp = if_alloc(IFT_ETHER);
16532f345d8eSLuigi Rizzo 	if (!sc->ifp)
16542f345d8eSLuigi Rizzo 		return ENOMEM;
16552f345d8eSLuigi Rizzo 
16562f345d8eSLuigi Rizzo 	ifmedia_init(&sc->media, IFM_IMASK, oce_media_change, oce_media_status);
16572f345d8eSLuigi Rizzo 	ifmedia_add(&sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
16582f345d8eSLuigi Rizzo 	ifmedia_set(&sc->media, IFM_ETHER | IFM_AUTO);
16592f345d8eSLuigi Rizzo 
16602f345d8eSLuigi Rizzo 	sc->ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
16612f345d8eSLuigi Rizzo 	sc->ifp->if_ioctl = oce_ioctl;
16622f345d8eSLuigi Rizzo 	sc->ifp->if_start = oce_start;
16632f345d8eSLuigi Rizzo 	sc->ifp->if_init = oce_init;
16642f345d8eSLuigi Rizzo 	sc->ifp->if_mtu = ETHERMTU;
16652f345d8eSLuigi Rizzo 	sc->ifp->if_softc = sc;
16662f345d8eSLuigi Rizzo #if __FreeBSD_version >= 800000
16672f345d8eSLuigi Rizzo 	sc->ifp->if_transmit = oce_multiq_start;
16682f345d8eSLuigi Rizzo 	sc->ifp->if_qflush = oce_multiq_flush;
16692f345d8eSLuigi Rizzo #endif
16702f345d8eSLuigi Rizzo 
16712f345d8eSLuigi Rizzo 	if_initname(sc->ifp,
16722f345d8eSLuigi Rizzo 		    device_get_name(sc->dev), device_get_unit(sc->dev));
16732f345d8eSLuigi Rizzo 
16742f345d8eSLuigi Rizzo 	sc->ifp->if_snd.ifq_drv_maxlen = OCE_MAX_TX_DESC - 1;
16752f345d8eSLuigi Rizzo 	IFQ_SET_MAXLEN(&sc->ifp->if_snd, sc->ifp->if_snd.ifq_drv_maxlen);
16762f345d8eSLuigi Rizzo 	IFQ_SET_READY(&sc->ifp->if_snd);
16772f345d8eSLuigi Rizzo 
16782f345d8eSLuigi Rizzo 	sc->ifp->if_hwassist = OCE_IF_HWASSIST;
16792f345d8eSLuigi Rizzo 	sc->ifp->if_hwassist |= CSUM_TSO;
16802f345d8eSLuigi Rizzo 	sc->ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
16812f345d8eSLuigi Rizzo 
16822f345d8eSLuigi Rizzo 	sc->ifp->if_capabilities = OCE_IF_CAPABILITIES;
16832f345d8eSLuigi Rizzo 	sc->ifp->if_capabilities |= IFCAP_HWCSUM;
16842f345d8eSLuigi Rizzo 	sc->ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
1685*ad512958SBjoern A. Zeeb #if defined(INET6) || defined(INET)
1686*ad512958SBjoern A. Zeeb 	sc->ifp->if_capabilities |= IFCAP_TSO;
16872f345d8eSLuigi Rizzo 	sc->ifp->if_capabilities |= IFCAP_LRO;
1688*ad512958SBjoern A. Zeeb #endif
16892f345d8eSLuigi Rizzo 
16902f345d8eSLuigi Rizzo 	sc->ifp->if_capenable = sc->ifp->if_capabilities;
169184c2fd2fSBjoern A. Zeeb 	sc->ifp->if_baudrate = IF_Gbps(10UL);
16922f345d8eSLuigi Rizzo 
16932f345d8eSLuigi Rizzo 	ether_ifattach(sc->ifp, sc->macaddr.mac_addr);
16942f345d8eSLuigi Rizzo 
16952f345d8eSLuigi Rizzo 	return 0;
16962f345d8eSLuigi Rizzo }
16972f345d8eSLuigi Rizzo 
16982f345d8eSLuigi Rizzo 
16992f345d8eSLuigi Rizzo static void
17002f345d8eSLuigi Rizzo oce_add_vlan(void *arg, struct ifnet *ifp, uint16_t vtag)
17012f345d8eSLuigi Rizzo {
17022f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ifp->if_softc;
17032f345d8eSLuigi Rizzo 
17042f345d8eSLuigi Rizzo 	if (ifp->if_softc !=  arg)
17052f345d8eSLuigi Rizzo 		return;
17062f345d8eSLuigi Rizzo 	if ((vtag == 0) || (vtag > 4095))
17072f345d8eSLuigi Rizzo 		return;
17082f345d8eSLuigi Rizzo 
17092f345d8eSLuigi Rizzo 	sc->vlan_tag[vtag] = 1;
17102f345d8eSLuigi Rizzo 	sc->vlans_added++;
17112f345d8eSLuigi Rizzo 	oce_vid_config(sc);
17122f345d8eSLuigi Rizzo }
17132f345d8eSLuigi Rizzo 
17142f345d8eSLuigi Rizzo 
17152f345d8eSLuigi Rizzo static void
17162f345d8eSLuigi Rizzo oce_del_vlan(void *arg, struct ifnet *ifp, uint16_t vtag)
17172f345d8eSLuigi Rizzo {
17182f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ifp->if_softc;
17192f345d8eSLuigi Rizzo 
17202f345d8eSLuigi Rizzo 	if (ifp->if_softc !=  arg)
17212f345d8eSLuigi Rizzo 		return;
17222f345d8eSLuigi Rizzo 	if ((vtag == 0) || (vtag > 4095))
17232f345d8eSLuigi Rizzo 		return;
17242f345d8eSLuigi Rizzo 
17252f345d8eSLuigi Rizzo 	sc->vlan_tag[vtag] = 0;
17262f345d8eSLuigi Rizzo 	sc->vlans_added--;
17272f345d8eSLuigi Rizzo 	oce_vid_config(sc);
17282f345d8eSLuigi Rizzo }
17292f345d8eSLuigi Rizzo 
17302f345d8eSLuigi Rizzo 
17312f345d8eSLuigi Rizzo /*
17322f345d8eSLuigi Rizzo  * A max of 64 vlans can be configured in BE. If the user configures
17332f345d8eSLuigi Rizzo  * more, place the card in vlan promiscuous mode.
17342f345d8eSLuigi Rizzo  */
17352f345d8eSLuigi Rizzo static int
17362f345d8eSLuigi Rizzo oce_vid_config(POCE_SOFTC sc)
17372f345d8eSLuigi Rizzo {
17382f345d8eSLuigi Rizzo 	struct normal_vlan vtags[MAX_VLANFILTER_SIZE];
17392f345d8eSLuigi Rizzo 	uint16_t ntags = 0, i;
17402f345d8eSLuigi Rizzo 	int status = 0;
17412f345d8eSLuigi Rizzo 
17422f345d8eSLuigi Rizzo 	if ((sc->vlans_added <= MAX_VLANFILTER_SIZE) &&
17432f345d8eSLuigi Rizzo 			(sc->ifp->if_capenable & IFCAP_VLAN_HWFILTER)) {
17442f345d8eSLuigi Rizzo 		for (i = 0; i < MAX_VLANS; i++) {
17452f345d8eSLuigi Rizzo 			if (sc->vlan_tag[i]) {
17462f345d8eSLuigi Rizzo 				vtags[ntags].vtag = i;
17472f345d8eSLuigi Rizzo 				ntags++;
17482f345d8eSLuigi Rizzo 			}
17492f345d8eSLuigi Rizzo 		}
17502f345d8eSLuigi Rizzo 		if (ntags)
17512f345d8eSLuigi Rizzo 			status = oce_config_vlan(sc, (uint8_t) sc->if_id,
17522f345d8eSLuigi Rizzo 						vtags, ntags, 1, 0);
17532f345d8eSLuigi Rizzo 	} else
17542f345d8eSLuigi Rizzo 		status = oce_config_vlan(sc, (uint8_t) sc->if_id,
17552f345d8eSLuigi Rizzo 					 	NULL, 0, 1, 1);
17562f345d8eSLuigi Rizzo 	return status;
17572f345d8eSLuigi Rizzo }
17582f345d8eSLuigi Rizzo 
17592f345d8eSLuigi Rizzo 
17602f345d8eSLuigi Rizzo static void
17612f345d8eSLuigi Rizzo oce_mac_addr_set(POCE_SOFTC sc)
17622f345d8eSLuigi Rizzo {
17632f345d8eSLuigi Rizzo 	uint32_t old_pmac_id = sc->pmac_id;
17642f345d8eSLuigi Rizzo 	int status = 0;
17652f345d8eSLuigi Rizzo 
17662f345d8eSLuigi Rizzo 
17672f345d8eSLuigi Rizzo 	status = bcmp((IF_LLADDR(sc->ifp)), sc->macaddr.mac_addr,
17682f345d8eSLuigi Rizzo 			 sc->macaddr.size_of_struct);
17692f345d8eSLuigi Rizzo 	if (!status)
17702f345d8eSLuigi Rizzo 		return;
17712f345d8eSLuigi Rizzo 
17722f345d8eSLuigi Rizzo 	status = oce_mbox_macaddr_add(sc, (uint8_t *)(IF_LLADDR(sc->ifp)),
17732f345d8eSLuigi Rizzo 					sc->if_id, &sc->pmac_id);
17742f345d8eSLuigi Rizzo 	if (!status) {
17752f345d8eSLuigi Rizzo 		status = oce_mbox_macaddr_del(sc, sc->if_id, old_pmac_id);
17762f345d8eSLuigi Rizzo 		bcopy((IF_LLADDR(sc->ifp)), sc->macaddr.mac_addr,
17772f345d8eSLuigi Rizzo 				 sc->macaddr.size_of_struct);
17782f345d8eSLuigi Rizzo 	}
17792f345d8eSLuigi Rizzo 	if (status)
17802f345d8eSLuigi Rizzo 		device_printf(sc->dev, "Failed update macaddress\n");
17812f345d8eSLuigi Rizzo 
17822f345d8eSLuigi Rizzo }
17832f345d8eSLuigi Rizzo 
17842f345d8eSLuigi Rizzo 
17852f345d8eSLuigi Rizzo static int
17862f345d8eSLuigi Rizzo oce_handle_passthrough(struct ifnet *ifp, caddr_t data)
17872f345d8eSLuigi Rizzo {
17882f345d8eSLuigi Rizzo 	POCE_SOFTC sc = ifp->if_softc;
17892f345d8eSLuigi Rizzo 	struct ifreq *ifr = (struct ifreq *)data;
17902f345d8eSLuigi Rizzo 	int rc = ENXIO;
17912f345d8eSLuigi Rizzo 	char cookie[32] = {0};
17922f345d8eSLuigi Rizzo 	void *priv_data = (void *)ifr->ifr_data;
17932f345d8eSLuigi Rizzo 	void *ioctl_ptr;
17942f345d8eSLuigi Rizzo 	uint32_t req_size;
17952f345d8eSLuigi Rizzo 	struct mbx_hdr req;
17962f345d8eSLuigi Rizzo 	OCE_DMA_MEM dma_mem;
17972f345d8eSLuigi Rizzo 
17982f345d8eSLuigi Rizzo 
17992f345d8eSLuigi Rizzo 	if (copyin(priv_data, cookie, strlen(IOCTL_COOKIE)))
18002f345d8eSLuigi Rizzo 		return EFAULT;
18012f345d8eSLuigi Rizzo 
18022f345d8eSLuigi Rizzo 	if (memcmp(cookie, IOCTL_COOKIE, strlen(IOCTL_COOKIE)))
18032f345d8eSLuigi Rizzo 		return EINVAL;
18042f345d8eSLuigi Rizzo 
18052f345d8eSLuigi Rizzo 	ioctl_ptr = (char *)priv_data + strlen(IOCTL_COOKIE);
18062f345d8eSLuigi Rizzo 	if (copyin(ioctl_ptr, &req, sizeof(struct mbx_hdr)))
18072f345d8eSLuigi Rizzo 		return EFAULT;
18082f345d8eSLuigi Rizzo 
18092f345d8eSLuigi Rizzo 	req_size = le32toh(req.u0.req.request_length);
18102f345d8eSLuigi Rizzo 	if (req_size > 65536)
18112f345d8eSLuigi Rizzo 		return EINVAL;
18122f345d8eSLuigi Rizzo 
18132f345d8eSLuigi Rizzo 	req_size += sizeof(struct mbx_hdr);
18142f345d8eSLuigi Rizzo 	rc = oce_dma_alloc(sc, req_size, &dma_mem, 0);
18152f345d8eSLuigi Rizzo 	if (rc)
18162f345d8eSLuigi Rizzo 		return ENOMEM;
18172f345d8eSLuigi Rizzo 
18182f345d8eSLuigi Rizzo 	if (copyin(ioctl_ptr, OCE_DMAPTR(&dma_mem,char), req_size)) {
18192f345d8eSLuigi Rizzo 		rc = EFAULT;
18202f345d8eSLuigi Rizzo 		goto dma_free;
18212f345d8eSLuigi Rizzo 	}
18222f345d8eSLuigi Rizzo 
18232f345d8eSLuigi Rizzo 	rc = oce_pass_through_mbox(sc, &dma_mem, req_size);
18242f345d8eSLuigi Rizzo 	if (rc) {
18252f345d8eSLuigi Rizzo 		rc = EIO;
18262f345d8eSLuigi Rizzo 		goto dma_free;
18272f345d8eSLuigi Rizzo 	}
18282f345d8eSLuigi Rizzo 
18292f345d8eSLuigi Rizzo 	if (copyout(OCE_DMAPTR(&dma_mem,char), ioctl_ptr, req_size))
18302f345d8eSLuigi Rizzo 		rc =  EFAULT;
18312f345d8eSLuigi Rizzo 
18322f345d8eSLuigi Rizzo dma_free:
18332f345d8eSLuigi Rizzo 	oce_dma_free(sc, &dma_mem);
18342f345d8eSLuigi Rizzo 	return rc;
18352f345d8eSLuigi Rizzo 
18362f345d8eSLuigi Rizzo }
18372f345d8eSLuigi Rizzo 
18382f345d8eSLuigi Rizzo 
18392f345d8eSLuigi Rizzo static void
18402f345d8eSLuigi Rizzo oce_local_timer(void *arg)
18412f345d8eSLuigi Rizzo {
18422f345d8eSLuigi Rizzo 	POCE_SOFTC sc = arg;
18432f345d8eSLuigi Rizzo 	int i = 0;
18442f345d8eSLuigi Rizzo 
18452f345d8eSLuigi Rizzo 	oce_refresh_nic_stats(sc);
18462f345d8eSLuigi Rizzo 	oce_refresh_queue_stats(sc);
18472f345d8eSLuigi Rizzo 	oce_mac_addr_set(sc);
18482f345d8eSLuigi Rizzo 
18492f345d8eSLuigi Rizzo 	/* TX Watch Dog*/
18502f345d8eSLuigi Rizzo 	for (i = 0; i < sc->nwqs; i++)
18512f345d8eSLuigi Rizzo 		oce_tx_restart(sc, sc->wq[i]);
18522f345d8eSLuigi Rizzo 
18532f345d8eSLuigi Rizzo 	callout_reset(&sc->timer, hz, oce_local_timer, sc);
18542f345d8eSLuigi Rizzo }
18552f345d8eSLuigi Rizzo 
18562f345d8eSLuigi Rizzo 
18572f345d8eSLuigi Rizzo static void
18582f345d8eSLuigi Rizzo oce_if_deactivate(POCE_SOFTC sc)
18592f345d8eSLuigi Rizzo {
18602f345d8eSLuigi Rizzo 	int i, mtime = 0;
18612f345d8eSLuigi Rizzo 	int wait_req = 0;
18622f345d8eSLuigi Rizzo 	struct oce_rq *rq;
18632f345d8eSLuigi Rizzo 	struct oce_wq *wq;
18642f345d8eSLuigi Rizzo 	struct oce_eq *eq;
18652f345d8eSLuigi Rizzo 
18662f345d8eSLuigi Rizzo 	sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
18672f345d8eSLuigi Rizzo 
18682f345d8eSLuigi Rizzo 	/*Wait for max of 400ms for TX completions to be done */
18692f345d8eSLuigi Rizzo 	while (mtime < 400) {
18702f345d8eSLuigi Rizzo 		wait_req = 0;
18712f345d8eSLuigi Rizzo 		for_all_wq_queues(sc, wq, i) {
18722f345d8eSLuigi Rizzo 			if (wq->ring->num_used) {
18732f345d8eSLuigi Rizzo 				wait_req = 1;
18742f345d8eSLuigi Rizzo 				DELAY(1);
18752f345d8eSLuigi Rizzo 				break;
18762f345d8eSLuigi Rizzo 			}
18772f345d8eSLuigi Rizzo 		}
18782f345d8eSLuigi Rizzo 		mtime += 1;
18792f345d8eSLuigi Rizzo 		if (!wait_req)
18802f345d8eSLuigi Rizzo 			break;
18812f345d8eSLuigi Rizzo 	}
18822f345d8eSLuigi Rizzo 
18832f345d8eSLuigi Rizzo 	/* Stop intrs and finish any bottom halves pending */
18842f345d8eSLuigi Rizzo 	oce_hw_intr_disable(sc);
18852f345d8eSLuigi Rizzo 
18862f345d8eSLuigi Rizzo 	for (i = 0; i < sc->intr_count; i++) {
18872f345d8eSLuigi Rizzo 		if (sc->intrs[i].tq != NULL) {
18882f345d8eSLuigi Rizzo 			taskqueue_drain(sc->intrs[i].tq, &sc->intrs[i].task);
18892f345d8eSLuigi Rizzo 		}
18902f345d8eSLuigi Rizzo 	}
18912f345d8eSLuigi Rizzo 
18922f345d8eSLuigi Rizzo 	/* Delete RX queue in card with flush param */
18932f345d8eSLuigi Rizzo 	oce_stop_rx(sc);
18942f345d8eSLuigi Rizzo 
18952f345d8eSLuigi Rizzo 	/* Invalidate any pending cq and eq entries*/
18962f345d8eSLuigi Rizzo 	for_all_evnt_queues(sc, eq, i)
18972f345d8eSLuigi Rizzo 		oce_drain_eq(eq);
18982f345d8eSLuigi Rizzo 	for_all_rq_queues(sc, rq, i)
18992f345d8eSLuigi Rizzo 		oce_drain_rq_cq(rq);
19002f345d8eSLuigi Rizzo 	for_all_wq_queues(sc, wq, i)
19012f345d8eSLuigi Rizzo 		oce_drain_wq_cq(wq);
19022f345d8eSLuigi Rizzo 
19032f345d8eSLuigi Rizzo 	/* But still we need to get MCC aync events.
19042f345d8eSLuigi Rizzo 	   So enable intrs and also arm first EQ
19052f345d8eSLuigi Rizzo         */
19062f345d8eSLuigi Rizzo 	oce_hw_intr_enable(sc);
19072f345d8eSLuigi Rizzo 	oce_arm_eq(sc, sc->eq[0]->eq_id, 0, TRUE, FALSE);
19082f345d8eSLuigi Rizzo 
19092f345d8eSLuigi Rizzo 	DELAY(10);
19102f345d8eSLuigi Rizzo }
19112f345d8eSLuigi Rizzo 
19122f345d8eSLuigi Rizzo 
19132f345d8eSLuigi Rizzo static void
19142f345d8eSLuigi Rizzo oce_if_activate(POCE_SOFTC sc)
19152f345d8eSLuigi Rizzo {
19162f345d8eSLuigi Rizzo 	struct oce_eq *eq;
19172f345d8eSLuigi Rizzo 	struct oce_rq *rq;
19182f345d8eSLuigi Rizzo 	struct oce_wq *wq;
19192f345d8eSLuigi Rizzo 	int i, rc = 0;
19202f345d8eSLuigi Rizzo 
19212f345d8eSLuigi Rizzo 	sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
19222f345d8eSLuigi Rizzo 
19232f345d8eSLuigi Rizzo 	oce_hw_intr_disable(sc);
19242f345d8eSLuigi Rizzo 
19252f345d8eSLuigi Rizzo 	oce_start_rx(sc);
19262f345d8eSLuigi Rizzo 
19272f345d8eSLuigi Rizzo 	for_all_rq_queues(sc, rq, i) {
19282f345d8eSLuigi Rizzo 		rc = oce_start_rq(rq);
19292f345d8eSLuigi Rizzo 		if (rc)
19302f345d8eSLuigi Rizzo 			device_printf(sc->dev, "Unable to start RX\n");
19312f345d8eSLuigi Rizzo 	}
19322f345d8eSLuigi Rizzo 
19332f345d8eSLuigi Rizzo 	for_all_wq_queues(sc, wq, i) {
19342f345d8eSLuigi Rizzo 		rc = oce_start_wq(wq);
19352f345d8eSLuigi Rizzo 		if (rc)
19362f345d8eSLuigi Rizzo 			device_printf(sc->dev, "Unable to start TX\n");
19372f345d8eSLuigi Rizzo 	}
19382f345d8eSLuigi Rizzo 
19392f345d8eSLuigi Rizzo 
19402f345d8eSLuigi Rizzo 	for_all_evnt_queues(sc, eq, i)
19412f345d8eSLuigi Rizzo 		oce_arm_eq(sc, eq->eq_id, 0, TRUE, FALSE);
19422f345d8eSLuigi Rizzo 
19432f345d8eSLuigi Rizzo 	oce_hw_intr_enable(sc);
19442f345d8eSLuigi Rizzo 
19452f345d8eSLuigi Rizzo }
19462f345d8eSLuigi Rizzo 
19472f345d8eSLuigi Rizzo /* Handle the Completion Queue for the Mailbox/Async notifications */
19482f345d8eSLuigi Rizzo uint16_t
19492f345d8eSLuigi Rizzo oce_mq_handler(void *arg)
19502f345d8eSLuigi Rizzo {
19512f345d8eSLuigi Rizzo 	struct oce_mq *mq = (struct oce_mq *)arg;
19522f345d8eSLuigi Rizzo 	POCE_SOFTC sc = mq->parent;
19532f345d8eSLuigi Rizzo 	struct oce_cq *cq = mq->cq;
19542f345d8eSLuigi Rizzo 	int num_cqes = 0;
19552f345d8eSLuigi Rizzo 	struct oce_mq_cqe *cqe;
19562f345d8eSLuigi Rizzo 	struct oce_async_cqe_link_state *acqe;
19572f345d8eSLuigi Rizzo 
19582f345d8eSLuigi Rizzo 	bus_dmamap_sync(cq->ring->dma.tag,
19592f345d8eSLuigi Rizzo 			cq->ring->dma.map, BUS_DMASYNC_POSTWRITE);
19602f345d8eSLuigi Rizzo 	cqe = RING_GET_CONSUMER_ITEM_VA(cq->ring, struct oce_mq_cqe);
19612f345d8eSLuigi Rizzo 	while (cqe->u0.dw[3]) {
19622f345d8eSLuigi Rizzo 		DW_SWAP((uint32_t *) cqe, sizeof(oce_mq_cqe));
19632f345d8eSLuigi Rizzo 		if (cqe->u0.s.async_event) {
19642f345d8eSLuigi Rizzo 			acqe = (struct oce_async_cqe_link_state *)cqe;
19652f345d8eSLuigi Rizzo 			if ((acqe->u0.s.link_status & ~ASYNC_EVENT_LOGICAL) ==
19662f345d8eSLuigi Rizzo 			    ASYNC_EVENT_LINK_UP) {
19672f345d8eSLuigi Rizzo 				sc->link_status = ASYNC_EVENT_LINK_UP;
19682f345d8eSLuigi Rizzo 				if_link_state_change(sc->ifp, LINK_STATE_UP);
19692f345d8eSLuigi Rizzo 			} else {
19702f345d8eSLuigi Rizzo 				sc->link_status = ASYNC_EVENT_LINK_DOWN;
19712f345d8eSLuigi Rizzo 				if_link_state_change(sc->ifp, LINK_STATE_DOWN);
19722f345d8eSLuigi Rizzo 			}
19732f345d8eSLuigi Rizzo 
19742f345d8eSLuigi Rizzo 			if (acqe->u0.s.event_code ==
19752f345d8eSLuigi Rizzo 				ASYNC_EVENT_CODE_LINK_STATE) {
19762f345d8eSLuigi Rizzo 				sc->link_speed = acqe->u0.s.speed;
19772f345d8eSLuigi Rizzo 				sc->qos_link_speed =
19782f345d8eSLuigi Rizzo 				(uint32_t )acqe->u0.s.qos_link_speed * 10;
19792f345d8eSLuigi Rizzo 			}
19802f345d8eSLuigi Rizzo 		}
19812f345d8eSLuigi Rizzo 		cqe->u0.dw[3] = 0;
19822f345d8eSLuigi Rizzo 		RING_GET(cq->ring, 1);
19832f345d8eSLuigi Rizzo 		RING_GET(mq->ring, 1);
19842f345d8eSLuigi Rizzo 		bus_dmamap_sync(cq->ring->dma.tag,
19852f345d8eSLuigi Rizzo 				cq->ring->dma.map, BUS_DMASYNC_POSTWRITE);
19862f345d8eSLuigi Rizzo 		cqe = RING_GET_CONSUMER_ITEM_VA(cq->ring, struct oce_mq_cqe);
19872f345d8eSLuigi Rizzo 		num_cqes++;
19882f345d8eSLuigi Rizzo 	}
19892f345d8eSLuigi Rizzo 
19902f345d8eSLuigi Rizzo 	if (num_cqes)
19912f345d8eSLuigi Rizzo 		oce_arm_cq(sc, cq->cq_id, num_cqes, FALSE);
19922f345d8eSLuigi Rizzo 
19932f345d8eSLuigi Rizzo 	return 0;
19942f345d8eSLuigi Rizzo }
19952f345d8eSLuigi Rizzo 
19962f345d8eSLuigi Rizzo 
19972f345d8eSLuigi Rizzo static void
19982f345d8eSLuigi Rizzo setup_max_queues_want(POCE_SOFTC sc)
19992f345d8eSLuigi Rizzo {
20002f345d8eSLuigi Rizzo 	int max_rss = 0;
20012f345d8eSLuigi Rizzo 
20022f345d8eSLuigi Rizzo 	/* Check if it is FLEX machine. Is so dont use RSS */
20032f345d8eSLuigi Rizzo 	if ((sc->function_mode & FNM_FLEX10_MODE) ||
20042f345d8eSLuigi Rizzo 		(!sc->rss_enable) ||
20052f345d8eSLuigi Rizzo 		(sc->flags & OCE_FLAGS_BE2)) {
20062f345d8eSLuigi Rizzo 		sc->nrqs = 1;
20072f345d8eSLuigi Rizzo 		sc->nwqs = 1;
20082f345d8eSLuigi Rizzo 		sc->rss_enable = 0;
20092f345d8eSLuigi Rizzo 	} else {
20102f345d8eSLuigi Rizzo 		/* For multiq, our deisgn is to have TX rings equal to
20112f345d8eSLuigi Rizzo 		   RSS rings. So that we can pair up one RSS ring and TX
20122f345d8eSLuigi Rizzo 		   to a single intr, which improves CPU cache efficiency.
20132f345d8eSLuigi Rizzo 		 */
20142f345d8eSLuigi Rizzo 		if (IS_BE(sc) && (!sc->be3_native))
20152f345d8eSLuigi Rizzo 			max_rss = OCE_LEGACY_MODE_RSS;
20162f345d8eSLuigi Rizzo 		else
20172f345d8eSLuigi Rizzo 			max_rss = OCE_MAX_RSS;
20182f345d8eSLuigi Rizzo 
20192f345d8eSLuigi Rizzo 		sc->nrqs = MIN(OCE_NCPUS, max_rss) + 1; /* 1 for def RX */
20202f345d8eSLuigi Rizzo 		sc->nwqs = MIN(OCE_NCPUS, max_rss);
20212f345d8eSLuigi Rizzo 
20222f345d8eSLuigi Rizzo 		/*Hardware issue. Turn off multi TX for be2 */
20232f345d8eSLuigi Rizzo 		if (IS_BE(sc) && (sc->flags & OCE_FLAGS_BE2))
20242f345d8eSLuigi Rizzo 			sc->nwqs = 1;
20252f345d8eSLuigi Rizzo 
20262f345d8eSLuigi Rizzo 	}
20272f345d8eSLuigi Rizzo 
20282f345d8eSLuigi Rizzo }
20292f345d8eSLuigi Rizzo 
20302f345d8eSLuigi Rizzo 
20312f345d8eSLuigi Rizzo static void
20322f345d8eSLuigi Rizzo update_queues_got(POCE_SOFTC sc)
20332f345d8eSLuigi Rizzo {
20342f345d8eSLuigi Rizzo 	if (sc->rss_enable) {
20352f345d8eSLuigi Rizzo 		sc->nrqs = sc->intr_count + 1;
20362f345d8eSLuigi Rizzo 		sc->nwqs = sc->intr_count;
20372f345d8eSLuigi Rizzo 		if (IS_BE(sc) && (sc->flags & OCE_FLAGS_BE2))
20382f345d8eSLuigi Rizzo 			sc->nwqs = 1;
20392f345d8eSLuigi Rizzo 	} else {
20402f345d8eSLuigi Rizzo 		sc->nrqs = 1;
20412f345d8eSLuigi Rizzo 		sc->nwqs = 1;
20422f345d8eSLuigi Rizzo 	}
20432f345d8eSLuigi Rizzo }
20442f345d8eSLuigi Rizzo 
2045