if_nve.c revision 145556
1191783Srmacklem/*
2191783Srmacklem * Copyright (c) 2005 by David E. O'Brien <obrien@FreeBSD.org>.
3191783Srmacklem * Copyright (c) 2003,2004 by Quinton Dolan <q@onthenet.com.au>.
4191783Srmacklem * All rights reserved.
5191783Srmacklem *
6191783Srmacklem * Redistribution and use in source and binary forms, with or without
7191783Srmacklem * modification, are permitted provided that the following conditions
8191783Srmacklem * are met:
9191783Srmacklem * 1. Redistributions of source code must retain the above copyright
10191783Srmacklem *    notice, this list of conditions and the following disclaimer.
11191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright
12191783Srmacklem *    notice, this list of conditions and the following disclaimer in the
13191783Srmacklem *    documentation and/or other materials provided with the distribution.
14191783Srmacklem *
15191783Srmacklem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
16191783Srmacklem * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17191783Srmacklem * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18191783Srmacklem * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19191783Srmacklem * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20191783Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21191783Srmacklem * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22191783Srmacklem * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23191783Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25191783Srmacklem * SUCH DAMAGE.
26191783Srmacklem *
27191783Srmacklem * $Id: if_nv.c,v 1.19 2004/08/12 14:00:05 q Exp $
28191783Srmacklem */
29191783Srmacklem
30191783Srmacklem/*
31191783Srmacklem * NVIDIA nForce MCP Networking Adapter driver
32191783Srmacklem *
33191783Srmacklem * This is a port of the NVIDIA MCP Linux ethernet driver distributed by NVIDIA
34191783Srmacklem * through their web site.
35191783Srmacklem *
36191783Srmacklem * All mainstream nForce and nForce2 motherboards are supported. This module
37191783Srmacklem * is as stable, sometimes more stable, than the linux version. (Recent
38191783Srmacklem * Linux stability issues seem to be related to some issues with newer
39191783Srmacklem * distributions using GCC 3.x, however this don't appear to effect FreeBSD
40191783Srmacklem * 5.x).
41191783Srmacklem *
42191783Srmacklem * In accordance with the NVIDIA distribution license it is necessary to
43191783Srmacklem * link this module against the nvlibnet.o binary object included in the
44191783Srmacklem * Linux driver source distribution. The binary component is not modified in
45191783Srmacklem * any way and is simply linked against a FreeBSD equivalent of the nvnet.c
46191783Srmacklem * linux kernel module "wrapper".
47191783Srmacklem *
48191783Srmacklem * The Linux driver uses a common code API that is shared between Win32 and
49191783Srmacklem * i386 Linux. This abstracts the low level driver functions and uses
50191783Srmacklem * callbacks and hooks to access the underlying hardware device. By using
51191783Srmacklem * this same API in a FreeBSD kernel module it is possible to support the
52191783Srmacklem * hardware without breaching the Linux source distributions licensing
53191783Srmacklem * requirements, or obtaining the hardware programming specifications.
54191783Srmacklem *
55191783Srmacklem * Although not conventional, it works, and given the relatively small
56191783Srmacklem * amount of hardware centric code, it's hopefully no more buggy than its
57191783Srmacklem * linux counterpart.
58191783Srmacklem *
59191783Srmacklem * NVIDIA now suppport the nForce3 AMD64 platform, however I have been
60191783Srmacklem * unable to access such a system to verify support. However, the code is
61191783Srmacklem * reported to work with little modification when compiled with the AMD64
62191783Srmacklem * version of the NVIDIA Linux library. All that should be necessary to make
63191783Srmacklem * the driver work is to link it directly into the kernel, instead of as a
64191783Srmacklem * module, and apply the docs/amd64.diff patch in this source distribution to
65191783Srmacklem * the NVIDIA Linux driver source.
66191783Srmacklem *
67191783Srmacklem * This driver should work on all versions of FreeBSD since 4.9/5.1 as well
68191783Srmacklem * as recent versions of DragonFly.
69191783Srmacklem *
70191783Srmacklem * Written by Quinton Dolan <q@onthenet.com.au>
71191783Srmacklem * Portions based on existing FreeBSD network drivers.
72191783Srmacklem * NVIDIA API usage derived from distributed NVIDIA NVNET driver source files.
73191783Srmacklem *
74191783Srmacklem */
75191783Srmacklem
76191783Srmacklem#include <sys/cdefs.h>
77191783Srmacklem__FBSDID("$FreeBSD: head/sys/dev/nve/if_nve.c 145556 2005-04-26 16:07:50Z obrien $");
78191783Srmacklem
79191783Srmacklem#include <sys/param.h>
80191783Srmacklem#include <sys/systm.h>
81191783Srmacklem#include <sys/sockio.h>
82191783Srmacklem#include <sys/mbuf.h>
83191783Srmacklem#include <sys/malloc.h>
84191783Srmacklem#include <sys/kernel.h>
85191783Srmacklem#include <sys/socket.h>
86191783Srmacklem#include <sys/sysctl.h>
87191783Srmacklem#include <sys/queue.h>
88191783Srmacklem#include <sys/module.h>
89191783Srmacklem
90191783Srmacklem#include <net/if.h>
91191783Srmacklem#include <net/if_arp.h>
92191783Srmacklem#include <net/ethernet.h>
93191783Srmacklem#include <net/if_dl.h>
94191783Srmacklem#include <net/if_media.h>
95191783Srmacklem#include <net/bpf.h>
96191783Srmacklem#include <net/if_vlan_var.h>
97191783Srmacklem
98191783Srmacklem#include <machine/bus_memio.h>
99191783Srmacklem#include <machine/bus.h>
100191783Srmacklem#include <machine/resource.h>
101191783Srmacklem
102191783Srmacklem#include <vm/vm.h>		/* for vtophys */
103191783Srmacklem#include <vm/pmap.h>		/* for vtophys */
104191783Srmacklem#include <machine/clock.h>	/* for DELAY */
105191783Srmacklem#include <sys/bus.h>
106191783Srmacklem#include <sys/rman.h>
107191783Srmacklem
108191783Srmacklem#include <dev/pci/pcireg.h>
109191783Srmacklem#include <dev/pci/pcivar.h>
110191783Srmacklem#include <dev/mii/mii.h>
111191783Srmacklem#include <dev/mii/miivar.h>
112191783Srmacklem#include "miibus_if.h"
113191783Srmacklem
114191783Srmacklem/* Include NVIDIA Linux driver header files */
115191783Srmacklem#define	linux
116191783Srmacklem#include <contrib/dev/nve/basetype.h>
117191783Srmacklem#include <contrib/dev/nve/phy.h>
118191783Srmacklem#include "os+%DIKED-nve.h"
119191783Srmacklem#include <contrib/dev/nve/drvinfo.h>
120191783Srmacklem#include <contrib/dev/nve/adapter.h>
121191783Srmacklem#undef linux
122191783Srmacklem
123191783Srmacklem#include <dev/nve/if_nvereg.h>
124191783Srmacklem
125191783SrmacklemMODULE_DEPEND(nve, pci, 1, 1, 1);
126191783SrmacklemMODULE_DEPEND(nve, ether, 1, 1, 1);
127191783SrmacklemMODULE_DEPEND(nve, miibus, 1, 1, 1);
128191783Srmacklem
129191783Srmacklemstatic int      nve_probe(device_t);
130191783Srmacklemstatic int      nve_attach(device_t);
131191783Srmacklemstatic int      nve_detach(device_t);
132191783Srmacklemstatic void     nve_init(void *);
133191783Srmacklemstatic void     nve_stop(struct nve_softc *);
134191783Srmacklemstatic void     nve_shutdown(device_t);
135191783Srmacklemstatic int      nve_init_rings(struct nve_softc *);
136191783Srmacklemstatic void     nve_free_rings(struct nve_softc *);
137191783Srmacklem
138191783Srmacklemstatic void     nve_ifstart(struct ifnet *);
139191783Srmacklemstatic int      nve_ioctl(struct ifnet *, u_long, caddr_t);
140191783Srmacklemstatic void     nve_intr(void *);
141191783Srmacklemstatic void     nve_tick(void *);
142191783Srmacklemstatic void     nve_setmulti(struct nve_softc *);
143191783Srmacklemstatic void     nve_watchdog(struct ifnet *);
144191783Srmacklemstatic void     nve_update_stats(struct nve_softc *);
145191783Srmacklem
146191783Srmacklemstatic int      nve_ifmedia_upd(struct ifnet *);
147191783Srmacklemstatic void     nve_ifmedia_sts(struct ifnet *, struct ifmediareq *);
148191783Srmacklemstatic int      nve_miibus_readreg(device_t, int, int);
149191783Srmacklemstatic void     nve_miibus_writereg(device_t, int, int, int);
150191783Srmacklem
151191783Srmacklemstatic void     nve_dmamap_cb(void *, bus_dma_segment_t *, int, int);
152191783Srmacklemstatic void     nve_dmamap_tx_cb(void *, bus_dma_segment_t *, int, bus_size_t, int);
153191783Srmacklem
154191783Srmacklemstatic NV_SINT32 nve_osalloc(PNV_VOID, PMEMORY_BLOCK);
155191783Srmacklemstatic NV_SINT32 nve_osfree(PNV_VOID, PMEMORY_BLOCK);
156191783Srmacklemstatic NV_SINT32 nve_osallocex(PNV_VOID, PMEMORY_BLOCKEX);
157191783Srmacklemstatic NV_SINT32 nve_osfreeex(PNV_VOID, PMEMORY_BLOCKEX);
158191783Srmacklemstatic NV_SINT32 nve_osclear(PNV_VOID, PNV_VOID, NV_SINT32);
159191783Srmacklemstatic NV_SINT32 nve_osdelay(PNV_VOID, NV_UINT32);
160192000Srmacklemstatic NV_SINT32 nve_osallocrxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID *);
161191783Srmacklemstatic NV_SINT32 nve_osfreerxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID);
162191783Srmacklemstatic NV_SINT32 nve_ospackettx(PNV_VOID, PNV_VOID, NV_UINT32);
163191783Srmacklemstatic NV_SINT32 nve_ospacketrx(PNV_VOID, PNV_VOID, NV_UINT32, NV_UINT8 *, NV_UINT8);
164191783Srmacklemstatic NV_SINT32 nve_oslinkchg(PNV_VOID, NV_SINT32);
165191783Srmacklemstatic NV_SINT32 nve_osalloctimer(PNV_VOID, PNV_VOID *);
166191783Srmacklemstatic NV_SINT32 nve_osfreetimer(PNV_VOID, PNV_VOID);
167191783Srmacklemstatic NV_SINT32 nve_osinittimer(PNV_VOID, PNV_VOID, PTIMER_FUNC, PNV_VOID);
168191783Srmacklemstatic NV_SINT32 nve_ossettimer(PNV_VOID, PNV_VOID, NV_UINT32);
169191783Srmacklemstatic NV_SINT32 nve_oscanceltimer(PNV_VOID, PNV_VOID);
170191783Srmacklem
171191783Srmacklemstatic NV_SINT32 nve_ospreprocpkt(PNV_VOID, PNV_VOID, PNV_VOID *, NV_UINT8 *, NV_UINT8);
172191783Srmacklemstatic PNV_VOID  nve_ospreprocpktnopq(PNV_VOID, PNV_VOID);
173191783Srmacklemstatic NV_SINT32 nve_osindicatepkt(PNV_VOID, PNV_VOID *, NV_UINT32);
174191783Srmacklemstatic NV_SINT32 nve_oslockalloc(PNV_VOID, NV_SINT32, PNV_VOID *);
175191783Srmacklemstatic NV_SINT32 nve_oslockacquire(PNV_VOID, NV_SINT32, PNV_VOID);
176191783Srmacklemstatic NV_SINT32 nve_oslockrelease(PNV_VOID, NV_SINT32, PNV_VOID);
177191783Srmacklemstatic PNV_VOID  nve_osreturnbufvirt(PNV_VOID, PNV_VOID);
178191783Srmacklem
179191783Srmacklemstatic device_method_t nve_methods[] = {
180191783Srmacklem	/* Device interface */
181191783Srmacklem	DEVMETHOD(device_probe, nve_probe),
182191783Srmacklem	DEVMETHOD(device_attach, nve_attach),
183191783Srmacklem	DEVMETHOD(device_detach, nve_detach),
184191783Srmacklem	DEVMETHOD(device_shutdown, nve_shutdown),
185191783Srmacklem
186191783Srmacklem	/* Bus interface */
187191783Srmacklem	DEVMETHOD(bus_print_child, bus_generic_print_child),
188191783Srmacklem	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
189191783Srmacklem
190191783Srmacklem	/* MII interface */
191191783Srmacklem	DEVMETHOD(miibus_readreg, nve_miibus_readreg),
192191783Srmacklem	DEVMETHOD(miibus_writereg, nve_miibus_writereg),
193191783Srmacklem
194191783Srmacklem	{0, 0}
195191783Srmacklem};
196191783Srmacklem
197191783Srmacklemstatic driver_t nve_driver = {
198191783Srmacklem	"nve",
199191783Srmacklem	nve_methods,
200191783Srmacklem	sizeof(struct nve_softc)
201191783Srmacklem};
202191783Srmacklem
203191783Srmacklemstatic devclass_t nve_devclass;
204191783Srmacklem
205191783Srmacklemstatic int      nve_pollinterval = 0;
206191783SrmacklemSYSCTL_INT(_hw, OID_AUTO, nve_pollinterval, CTLFLAG_RW,
207191783Srmacklem	   &nve_pollinterval, 0, "delay between interface polls");
208191783Srmacklem
209191783SrmacklemDRIVER_MODULE(nve, pci, nve_driver, nve_devclass, 0, 0);
210191783SrmacklemDRIVER_MODULE(miibus, nve, miibus_driver, miibus_devclass, 0, 0);
211191783Srmacklem
212191783Srmacklemstatic struct nve_type nve_devs[] = {
213191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET1_DEVICEID,
214191783Srmacklem	"NVIDIA nForce MCP Networking Adapter"},
215191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET2_DEVICEID,
216191783Srmacklem	"NVIDIA nForce MCP2 Networking Adapter"},
217191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET3_DEVICEID,
218191783Srmacklem	"NVIDIA nForce MCP3 Networking Adapter"},
219191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET4_DEVICEID,
220191783Srmacklem	"NVIDIA nForce MCP4 Networking Adapter"},
221191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET5_DEVICEID,
222191783Srmacklem	"NVIDIA nForce MCP5 Networking Adapter"},
223191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET6_DEVICEID,
224191783Srmacklem	"NVIDIA nForce MCP6 Networking Adapter"},
225191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET7_DEVICEID,
226191783Srmacklem	"NVIDIA nForce MCP7 Networking Adapter"},
227191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET8_DEVICEID,
228191783Srmacklem	"NVIDIA nForce MCP8 Networking Adapter"},
229191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET9_DEVICEID,
230191783Srmacklem	"NVIDIA nForce MCP9 Networking Adapter"},
231191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET10_DEVICEID,
232191783Srmacklem	"NVIDIA nForce MCP10 Networking Adapter"},
233191783Srmacklem	{NVIDIA_VENDORID, NFORCE_MCPNET11_DEVICEID,
234191783Srmacklem	"NVIDIA nForce MCP11 Networking Adapter"},
235191783Srmacklem	{0, 0, NULL}
236191783Srmacklem};
237191783Srmacklem
238191783Srmacklem/* DMA MEM map callback function to get data segment physical address */
239191783Srmacklemstatic void
240191783Srmacklemnve_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nsegs, int error)
241191783Srmacklem{
242191783Srmacklem	if (error)
243191783Srmacklem		return;
244191783Srmacklem
245191783Srmacklem	KASSERT(nsegs == 1,
246191783Srmacklem	    ("Too many DMA segments returned when mapping DMA memory"));
247191783Srmacklem	*(bus_addr_t *)arg = segs->ds_addr;
248191783Srmacklem}
249191783Srmacklem
250191783Srmacklem/* DMA RX map callback function to get data segment physical address */
251191783Srmacklemstatic void
252191783Srmacklemnve_dmamap_rx_cb(void *arg, bus_dma_segment_t * segs, int nsegs,
253191783Srmacklem    bus_size_t mapsize, int error)
254191783Srmacklem{
255191783Srmacklem	if (error)
256191783Srmacklem		return;
257191783Srmacklem	*(bus_addr_t *)arg = segs->ds_addr;
258191783Srmacklem}
259191783Srmacklem
260191783Srmacklem/*
261191783Srmacklem * DMA TX buffer callback function to allocate fragment data segment
262191783Srmacklem * addresses
263191783Srmacklem */
264191783Srmacklemstatic void
265191783Srmacklemnve_dmamap_tx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
266191783Srmacklem{
267191783Srmacklem	struct nve_tx_desc *info;
268191783Srmacklem
269191783Srmacklem	info = arg;
270191783Srmacklem	if (error)
271191783Srmacklem		return;
272191783Srmacklem	KASSERT(nsegs < NV_MAX_FRAGS,
273191783Srmacklem	    ("Too many DMA segments returned when mapping mbuf"));
274191783Srmacklem	info->numfrags = nsegs;
275191783Srmacklem	bcopy(segs, info->frags, nsegs * sizeof(bus_dma_segment_t));
276191783Srmacklem}
277191783Srmacklem
278191783Srmacklem/* Probe for supported hardware ID's */
279191783Srmacklemstatic int
280191783Srmacklemnve_probe(device_t dev)
281191783Srmacklem{
282191783Srmacklem	struct nve_type *t;
283191783Srmacklem
284191783Srmacklem	t = nve_devs;
285191783Srmacklem	/* Check for matching PCI DEVICE ID's */
286191783Srmacklem	while (t->name != NULL) {
287191783Srmacklem		if ((pci_get_vendor(dev) == t->vid_id) &&
288191783Srmacklem		    (pci_get_device(dev) == t->dev_id)) {
289191783Srmacklem			device_set_desc(dev, t->name);
290191783Srmacklem			return (0);
291191783Srmacklem		}
292191783Srmacklem		t++;
293191783Srmacklem	}
294191783Srmacklem
295191783Srmacklem	return (ENXIO);
296191783Srmacklem}
297191783Srmacklem
298191783Srmacklem/* Attach driver and initialise hardware for use */
299191783Srmacklemstatic int
300191783Srmacklemnve_attach(device_t dev)
301191783Srmacklem{
302191783Srmacklem	u_char			eaddr[ETHER_ADDR_LEN];
303191783Srmacklem	struct nve_softc	*sc;
304191783Srmacklem	struct ifnet		*ifp;
305191783Srmacklem	OS_API			*osapi;
306191783Srmacklem	ADAPTER_OPEN_PARAMS	OpenParams;
307191783Srmacklem	int			error = 0, i, rid, unit;
308191783Srmacklem
309191783Srmacklem	DEBUGOUT(NVE_DEBUG_INIT, "nve: nve_attach - entry\n");
310191783Srmacklem
311191783Srmacklem	sc = device_get_softc(dev);
312191783Srmacklem	unit = device_get_unit(dev);
313191783Srmacklem
314191783Srmacklem	/* Allocate mutex */
315191783Srmacklem	mtx_init(&sc->mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
316191783Srmacklem	    MTX_DEF | MTX_RECURSE);
317191783Srmacklem	mtx_init(&sc->osmtx, device_get_nameunit(dev), NULL, MTX_SPIN);
318191783Srmacklem
319191783Srmacklem	sc->dev = dev;
320191783Srmacklem	sc->unit = unit;
321191783Srmacklem
322191783Srmacklem	/* Preinitialize data structures */
323191783Srmacklem	bzero(&OpenParams, sizeof(ADAPTER_OPEN_PARAMS));
324191783Srmacklem
325191783Srmacklem	/* Enable bus mastering */
326191783Srmacklem	pci_enable_busmaster(dev);
327191783Srmacklem
328191783Srmacklem	/* Allocate memory mapped address space */
329191783Srmacklem	rid = NV_RID;
330191783Srmacklem	sc->res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 1,
331191783Srmacklem	    RF_ACTIVE);
332191783Srmacklem
333191783Srmacklem	if (sc->res == NULL) {
334191783Srmacklem		device_printf(dev, "couldn't map memory\n");
335191783Srmacklem		error = ENXIO;
336191783Srmacklem		goto fail;
337191783Srmacklem	}
338191783Srmacklem	sc->sc_st = rman_get_bustag(sc->res);
339191783Srmacklem	sc->sc_sh = rman_get_bushandle(sc->res);
340191783Srmacklem
341191783Srmacklem	/* Allocate interrupt */
342191783Srmacklem	rid = 0;
343191783Srmacklem	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
344191783Srmacklem	    RF_SHAREABLE | RF_ACTIVE);
345191783Srmacklem
346191783Srmacklem	if (sc->irq == NULL) {
347191783Srmacklem		device_printf(dev, "couldn't map interrupt\n");
348191783Srmacklem		error = ENXIO;
349191783Srmacklem		goto fail;
350191783Srmacklem	}
351191783Srmacklem	/* Allocate DMA tags */
352191783Srmacklem	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
353191783Srmacklem		     BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * NV_MAX_FRAGS,
354191783Srmacklem				   NV_MAX_FRAGS, MCLBYTES, 0,
355191783Srmacklem				   busdma_lock_mutex, &Giant,
356191783Srmacklem				   &sc->mtag);
357191783Srmacklem	if (error) {
358191783Srmacklem		device_printf(dev, "couldn't allocate dma tag\n");
359191783Srmacklem		goto fail;
360191783Srmacklem	}
361191783Srmacklem	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
362191783Srmacklem	    BUS_SPACE_MAXADDR, NULL, NULL,
363191783Srmacklem	    sizeof(struct nve_rx_desc) * RX_RING_SIZE, 1,
364191783Srmacklem	    sizeof(struct nve_rx_desc) * RX_RING_SIZE, 0,
365191783Srmacklem	    busdma_lock_mutex, &Giant,
366191783Srmacklem	    &sc->rtag);
367191783Srmacklem	if (error) {
368191783Srmacklem		device_printf(dev, "couldn't allocate dma tag\n");
369191783Srmacklem		goto fail;
370191783Srmacklem	}
371191783Srmacklem	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
372191783Srmacklem	    BUS_SPACE_MAXADDR, NULL, NULL,
373191783Srmacklem	    sizeof(struct nve_tx_desc) * TX_RING_SIZE, 1,
374191783Srmacklem	    sizeof(struct nve_tx_desc) * TX_RING_SIZE, 0,
375191783Srmacklem	    busdma_lock_mutex, &Giant,
376191783Srmacklem	    &sc->ttag);
377191783Srmacklem	if (error) {
378191783Srmacklem		device_printf(dev, "couldn't allocate dma tag\n");
379191783Srmacklem		goto fail;
380191783Srmacklem	}
381191783Srmacklem	/* Allocate DMA safe memory and get the DMA addresses. */
382191783Srmacklem	error = bus_dmamem_alloc(sc->ttag, (void **)&sc->tx_desc,
383191783Srmacklem	    BUS_DMA_WAITOK, &sc->tmap);
384191783Srmacklem	if (error) {
385191783Srmacklem		device_printf(dev, "couldn't allocate dma memory\n");
386191783Srmacklem		goto fail;
387191783Srmacklem	}
388191783Srmacklem	bzero(sc->tx_desc, sizeof(struct nve_tx_desc) * TX_RING_SIZE);
389191783Srmacklem	error = bus_dmamap_load(sc->ttag, sc->tmap, sc->tx_desc,
390191783Srmacklem		    sizeof(struct nve_tx_desc) * TX_RING_SIZE, nve_dmamap_cb,
391191783Srmacklem		    &sc->tx_addr, 0);
392191783Srmacklem	if (error) {
393191783Srmacklem		device_printf(dev, "couldn't map dma memory\n");
394191783Srmacklem		goto fail;
395191783Srmacklem	}
396191783Srmacklem	error = bus_dmamem_alloc(sc->rtag, (void **)&sc->rx_desc,
397191783Srmacklem	    BUS_DMA_WAITOK, &sc->rmap);
398191783Srmacklem	if (error) {
399191783Srmacklem		device_printf(dev, "couldn't allocate dma memory\n");
400191783Srmacklem		goto fail;
401191783Srmacklem	}
402191783Srmacklem	bzero(sc->rx_desc, sizeof(struct nve_rx_desc) * RX_RING_SIZE);
403191783Srmacklem	error = bus_dmamap_load(sc->rtag, sc->rmap, sc->rx_desc,
404191783Srmacklem	    sizeof(struct nve_rx_desc) * RX_RING_SIZE, nve_dmamap_cb,
405191783Srmacklem	    &sc->rx_addr, 0);
406191783Srmacklem	if (error) {
407191783Srmacklem		device_printf(dev, "couldn't map dma memory\n");
408191783Srmacklem		goto fail;
409191783Srmacklem	}
410191783Srmacklem	/* Initialize rings. */
411191783Srmacklem	if (nve_init_rings(sc)) {
412191783Srmacklem		device_printf(dev, "failed to init rings\n");
413191783Srmacklem		error = ENXIO;
414191783Srmacklem		goto fail;
415191783Srmacklem	}
416191783Srmacklem	/* Setup NVIDIA API callback routines */
417191783Srmacklem	osapi				= &sc->osapi;
418191783Srmacklem	osapi->pOSCX			= sc;
419191783Srmacklem	osapi->pfnAllocMemory		= nve_osalloc;
420191783Srmacklem	osapi->pfnFreeMemory		= nve_osfree;
421191783Srmacklem	osapi->pfnAllocMemoryEx		= nve_osallocex;
422191783Srmacklem	osapi->pfnFreeMemoryEx		= nve_osfreeex;
423191783Srmacklem	osapi->pfnClearMemory		= nve_osclear;
424191783Srmacklem	osapi->pfnStallExecution	= nve_osdelay;
425191783Srmacklem	osapi->pfnAllocReceiveBuffer	= nve_osallocrxbuf;
426191783Srmacklem	osapi->pfnFreeReceiveBuffer	= nve_osfreerxbuf;
427191783Srmacklem	osapi->pfnPacketWasSent		= nve_ospackettx;
428191783Srmacklem	osapi->pfnPacketWasReceived	= nve_ospacketrx;
429191783Srmacklem	osapi->pfnLinkStateHasChanged	= nve_oslinkchg;
430191783Srmacklem	osapi->pfnAllocTimer		= nve_osalloctimer;
431191783Srmacklem	osapi->pfnFreeTimer		= nve_osfreetimer;
432191783Srmacklem	osapi->pfnInitializeTimer	= nve_osinittimer;
433191783Srmacklem	osapi->pfnSetTimer		= nve_ossettimer;
434191783Srmacklem	osapi->pfnCancelTimer		= nve_oscanceltimer;
435191783Srmacklem	osapi->pfnPreprocessPacket	= nve_ospreprocpkt;
436191783Srmacklem	osapi->pfnPreprocessPacketNopq	= nve_ospreprocpktnopq;
437191783Srmacklem	osapi->pfnIndicatePackets	= nve_osindicatepkt;
438191783Srmacklem	osapi->pfnLockAlloc		= nve_oslockalloc;
439191783Srmacklem	osapi->pfnLockAcquire		= nve_oslockacquire;
440191783Srmacklem	osapi->pfnLockRelease		= nve_oslockrelease;
441191783Srmacklem	osapi->pfnReturnBufferVirtual	= nve_osreturnbufvirt;
442191783Srmacklem
443191783Srmacklem	sc->linkup = FALSE;
444191783Srmacklem	sc->max_frame_size = ETHERMTU + ETHER_HDR_LEN + FCS_LEN;
445191783Srmacklem
446191783Srmacklem	/* TODO - We don't support hardware offload yet */
447191783Srmacklem	sc->hwmode = 1;
448191783Srmacklem	sc->media = 0;
449191783Srmacklem
450191783Srmacklem	/* Set NVIDIA API startup parameters */
451191783Srmacklem	OpenParams.MaxDpcLoop = 2;
452191783Srmacklem	OpenParams.MaxRxPkt = RX_RING_SIZE;
453191783Srmacklem	OpenParams.MaxTxPkt = TX_RING_SIZE;
454191783Srmacklem	OpenParams.SentPacketStatusSuccess = 1;
455191783Srmacklem	OpenParams.SentPacketStatusFailure = 0;
456191783Srmacklem	OpenParams.MaxRxPktToAccumulate = 6;
457191783Srmacklem	OpenParams.ulPollInterval = nve_pollinterval;
458191783Srmacklem	OpenParams.SetForcedModeEveryNthRxPacket = 0;
459191783Srmacklem	OpenParams.SetForcedModeEveryNthTxPacket = 0;
460191783Srmacklem	OpenParams.RxForcedInterrupt = 0;
461191783Srmacklem	OpenParams.TxForcedInterrupt = 0;
462191783Srmacklem	OpenParams.pOSApi = osapi;
463191783Srmacklem	OpenParams.pvHardwareBaseAddress = rman_get_virtual(sc->res);
464191783Srmacklem	OpenParams.bASFEnabled = 0;
465191783Srmacklem	OpenParams.ulDescriptorVersion = sc->hwmode;
466191783Srmacklem	OpenParams.ulMaxPacketSize = sc->max_frame_size;
467191783Srmacklem	OpenParams.DeviceId = pci_get_device(dev);
468191783Srmacklem
469191783Srmacklem	/* Open NVIDIA Hardware API */
470191783Srmacklem	error = ADAPTER_Open(&OpenParams, (void **)&(sc->hwapi), &sc->phyaddr);
471191783Srmacklem	if (error) {
472191783Srmacklem		device_printf(dev,
473191783Srmacklem		    "failed to open NVIDIA Hardware API: 0x%x\n", error);
474191783Srmacklem		goto fail;
475191783Srmacklem	}
476191783Srmacklem
477191783Srmacklem	/* TODO - Add support for MODE2 hardware offload */
478191783Srmacklem
479191783Srmacklem	bzero(&sc->adapterdata, sizeof(sc->adapterdata));
480191783Srmacklem
481191783Srmacklem	sc->adapterdata.ulMediaIF = sc->media;
482191783Srmacklem	sc->adapterdata.ulModeRegTxReadCompleteEnable = 1;
483191783Srmacklem	sc->hwapi->pfnSetCommonData(sc->hwapi->pADCX, &sc->adapterdata);
484191783Srmacklem
485191783Srmacklem	/* MAC is loaded backwards into h/w reg */
486191783Srmacklem	sc->hwapi->pfnGetNodeAddress(sc->hwapi->pADCX, sc->original_mac_addr);
487191783Srmacklem	for (i = 0; i < 6; i++) {
488191783Srmacklem		eaddr[i] = sc->original_mac_addr[5 - i];
489191783Srmacklem	}
490191783Srmacklem	sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, eaddr);
491191783Srmacklem	bcopy(eaddr, (char *)&sc->sc_macaddr, ETHER_ADDR_LEN);
492191783Srmacklem
493191783Srmacklem	/* Display ethernet address ,... */
494191783Srmacklem	device_printf(dev, "Ethernet address %6D\n", sc->sc_macaddr, ":");
495191783Srmacklem
496191783Srmacklem	DEBUGOUT(NVE_DEBUG_INIT, "nve: do mii_phy_probe\n");
497191783Srmacklem
498191783Srmacklem	/* Probe device for MII interface to PHY */
499191783Srmacklem	if (mii_phy_probe(dev, &sc->miibus, nve_ifmedia_upd, nve_ifmedia_sts)) {
500191783Srmacklem		device_printf(dev, "MII without any phy!\n");
501191783Srmacklem		error = ENXIO;
502191783Srmacklem		goto fail;
503191783Srmacklem	}
504191783Srmacklem	/* Setup interface parameters */
505191783Srmacklem	ifp = &sc->sc_if;
506191783Srmacklem	ifp->if_softc = sc;
507191783Srmacklem	if_initname(ifp, "nve", unit);
508191783Srmacklem	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
509191783Srmacklem	ifp->if_ioctl = nve_ioctl;
510191783Srmacklem	ifp->if_output = ether_output;
511191783Srmacklem	ifp->if_start = nve_ifstart;
512191783Srmacklem	ifp->if_watchdog = nve_watchdog;
513191783Srmacklem	ifp->if_timer = 0;
514191783Srmacklem	ifp->if_init = nve_init;
515191783Srmacklem	ifp->if_mtu = ETHERMTU;
516191783Srmacklem	ifp->if_baudrate = IF_Mbps(100);
517191783Srmacklem	ifp->if_snd.ifq_maxlen = TX_RING_SIZE - 1;
518191783Srmacklem	ifp->if_capabilities |= IFCAP_VLAN_MTU;
519191783Srmacklem
520191783Srmacklem	/* Attach to OS's managers. */
521191783Srmacklem	ether_ifattach(ifp, sc->sc_macaddr);
522191783Srmacklem	callout_handle_init(&sc->stat_ch);
523191783Srmacklem
524191783Srmacklem	/* Activate our interrupt handler. - attach last to avoid lock */
525191783Srmacklem	error = bus_setup_intr(sc->dev, sc->irq, INTR_TYPE_NET, nve_intr,
526191783Srmacklem	    sc, &sc->sc_ih);
527191783Srmacklem	if (error) {
528191783Srmacklem		device_printf(sc->dev, "couldn't set up interrupt handler\n");
529191783Srmacklem		goto fail;
530191783Srmacklem	}
531191783Srmacklem	DEBUGOUT(NVE_DEBUG_INIT, "nve: nve_attach - exit\n");
532191783Srmacklem
533191783Srmacklemfail:
534191783Srmacklem	if (error)
535191783Srmacklem		nve_detach(dev);
536191783Srmacklem
537191783Srmacklem	return (error);
538191783Srmacklem}
539191783Srmacklem
540191783Srmacklem/* Detach interface for module unload */
541191783Srmacklemstatic int
542191783Srmacklemnve_detach(device_t dev)
543191783Srmacklem{
544191783Srmacklem	struct nve_softc *sc = device_get_softc(dev);
545191783Srmacklem	struct ifnet *ifp;
546191783Srmacklem
547191783Srmacklem	KASSERT(mtx_initialized(&sc->mtx), ("mutex not initialized"));
548191783Srmacklem	NVE_LOCK(sc);
549191783Srmacklem
550191783Srmacklem	DEBUGOUT(NVE_DEBUG_DEINIT, "nve: nve_detach - entry\n");
551191783Srmacklem
552191783Srmacklem	ifp = &sc->arpcom.ac_if;
553191783Srmacklem
554191783Srmacklem	if (device_is_attached(dev)) {
555191783Srmacklem		nve_stop(sc);
556191783Srmacklem		ether_ifdetach(ifp);
557191783Srmacklem	}
558191783Srmacklem
559191783Srmacklem	if (sc->miibus)
560191783Srmacklem		device_delete_child(dev, sc->miibus);
561191783Srmacklem	bus_generic_detach(dev);
562191783Srmacklem
563191783Srmacklem	/* Reload unreversed address back into MAC in original state */
564191783Srmacklem	if (sc->original_mac_addr)
565191783Srmacklem		sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX,
566191783Srmacklem		    sc->original_mac_addr);
567191783Srmacklem
568191783Srmacklem	DEBUGOUT(NVE_DEBUG_DEINIT, "nve: do pfnClose\n");
569191783Srmacklem	/* Detach from NVIDIA hardware API */
570191783Srmacklem	if (sc->hwapi->pfnClose)
571191783Srmacklem		sc->hwapi->pfnClose(sc->hwapi->pADCX, FALSE);
572191783Srmacklem	/* Release resources */
573191783Srmacklem	if (sc->sc_ih)
574191783Srmacklem		bus_teardown_intr(sc->dev, sc->irq, sc->sc_ih);
575191783Srmacklem	if (sc->irq)
576191783Srmacklem		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
577191783Srmacklem	if (sc->res)
578191783Srmacklem		bus_release_resource(sc->dev, SYS_RES_MEMORY, NV_RID, sc->res);
579191783Srmacklem
580191783Srmacklem	nve_free_rings(sc);
581191783Srmacklem
582191783Srmacklem	if (sc->tx_desc) {
583191783Srmacklem		bus_dmamap_unload(sc->rtag, sc->rmap);
584191783Srmacklem		bus_dmamem_free(sc->rtag, sc->rx_desc, sc->rmap);
585191783Srmacklem		bus_dmamap_destroy(sc->rtag, sc->rmap);
586191783Srmacklem	}
587191783Srmacklem	if (sc->mtag)
588191783Srmacklem		bus_dma_tag_destroy(sc->mtag);
589191783Srmacklem	if (sc->ttag)
590191783Srmacklem		bus_dma_tag_destroy(sc->ttag);
591191783Srmacklem	if (sc->rtag)
592191783Srmacklem		bus_dma_tag_destroy(sc->rtag);
593191783Srmacklem
594191783Srmacklem	NVE_UNLOCK(sc);
595191783Srmacklem	mtx_destroy(&sc->mtx);
596191783Srmacklem	mtx_destroy(&sc->osmtx);
597191783Srmacklem
598191783Srmacklem	DEBUGOUT(NVE_DEBUG_DEINIT, "nve: nve_detach - exit\n");
599191783Srmacklem
600191783Srmacklem	return (0);
601191783Srmacklem}
602191783Srmacklem
603191783Srmacklem/* Initialise interface and start it "RUNNING" */
604191783Srmacklemstatic void
605191783Srmacklemnve_init(void *xsc)
606191783Srmacklem{
607191783Srmacklem	struct nve_softc *sc = xsc;
608191783Srmacklem	struct ifnet *ifp;
609191783Srmacklem	int error;
610191783Srmacklem
611191783Srmacklem	NVE_LOCK(sc);
612191783Srmacklem	DEBUGOUT(NVE_DEBUG_INIT, "nve: nve_init - entry (%d)\n", sc->linkup);
613191783Srmacklem
614191783Srmacklem	ifp = &sc->sc_if;
615191783Srmacklem
616191783Srmacklem	/* Do nothing if already running */
617191783Srmacklem	if (ifp->if_flags & IFF_RUNNING)
618191783Srmacklem		goto fail;
619191783Srmacklem
620191783Srmacklem	nve_stop(sc);
621191783Srmacklem	DEBUGOUT(NVE_DEBUG_INIT, "nve: do pfnInit\n");
622191783Srmacklem
623191783Srmacklem	/* Setup Hardware interface and allocate memory structures */
624191783Srmacklem	error = sc->hwapi->pfnInit(sc->hwapi->pADCX,
625191783Srmacklem	    0, /* force speed */
626191783Srmacklem	    0, /* force full duplex */
627191783Srmacklem	    0, /* force mode */
628191783Srmacklem	    0, /* force async mode */
629191783Srmacklem	    &sc->linkup);
630191783Srmacklem
631191783Srmacklem	if (error) {
632191783Srmacklem		device_printf(sc->dev,
633191783Srmacklem		    "failed to start NVIDIA Hardware interface\n");
634191783Srmacklem		goto fail;
635191783Srmacklem	}
636191783Srmacklem	/* Set the MAC address */
637191783Srmacklem	sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->sc_macaddr);
638191783Srmacklem	sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
639191783Srmacklem	sc->hwapi->pfnStart(sc->hwapi->pADCX);
640191783Srmacklem
641191783Srmacklem	/* Setup multicast filter */
642191783Srmacklem	nve_setmulti(sc);
643191783Srmacklem	nve_ifmedia_upd(ifp);
644191783Srmacklem
645191783Srmacklem	/* Update interface parameters */
646191783Srmacklem	ifp->if_flags |= IFF_RUNNING;
647191783Srmacklem	ifp->if_flags &= ~IFF_OACTIVE;
648191783Srmacklem
649191783Srmacklem	sc->stat_ch = timeout(nve_tick, sc, hz);
650191783Srmacklem
651191783Srmacklem	DEBUGOUT(NVE_DEBUG_INIT, "nve: nve_init - exit\n");
652191783Srmacklem
653191783Srmacklemfail:
654191783Srmacklem	NVE_UNLOCK(sc);
655191783Srmacklem
656191783Srmacklem	return;
657191783Srmacklem}
658191783Srmacklem
659191783Srmacklem/* Stop interface activity ie. not "RUNNING" */
660191783Srmacklemstatic void
661191783Srmacklemnve_stop(struct nve_softc *sc)
662191783Srmacklem{
663191783Srmacklem	struct ifnet *ifp;
664191783Srmacklem
665191783Srmacklem	NVE_LOCK(sc);
666191783Srmacklem
667191783Srmacklem	DEBUGOUT(NVE_DEBUG_RUNNING, "nve: nve_stop - entry\n");
668191783Srmacklem
669191783Srmacklem	ifp = &sc->sc_if;
670191783Srmacklem	ifp->if_timer = 0;
671191783Srmacklem
672191783Srmacklem	/* Cancel tick timer */
673191783Srmacklem	untimeout(nve_tick, sc, sc->stat_ch);
674191783Srmacklem
675191783Srmacklem	/* Stop hardware activity */
676191783Srmacklem	sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
677191783Srmacklem	sc->hwapi->pfnStop(sc->hwapi->pADCX, 0);
678191783Srmacklem
679191783Srmacklem	DEBUGOUT(NVE_DEBUG_DEINIT, "nve: do pfnDeinit\n");
680191783Srmacklem	/* Shutdown interface and deallocate memory buffers */
681191783Srmacklem	if (sc->hwapi->pfnDeinit)
682191783Srmacklem		sc->hwapi->pfnDeinit(sc->hwapi->pADCX, 0);
683191783Srmacklem
684191783Srmacklem	sc->linkup = 0;
685191783Srmacklem	sc->cur_rx = 0;
686191783Srmacklem	sc->pending_rxs = 0;
687191783Srmacklem
688191783Srmacklem	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
689191783Srmacklem
690191783Srmacklem	DEBUGOUT(NVE_DEBUG_RUNNING, "nve: nve_stop - exit\n");
691191783Srmacklem
692191783Srmacklem	NVE_UNLOCK(sc);
693191783Srmacklem
694191783Srmacklem	return;
695191783Srmacklem}
696191783Srmacklem
697191783Srmacklem/* Shutdown interface for unload/reboot */
698191783Srmacklemstatic void
699191783Srmacklemnve_shutdown(device_t dev)
700191783Srmacklem{
701191783Srmacklem	struct nve_softc *sc;
702191783Srmacklem
703191783Srmacklem	DEBUGOUT(NVE_DEBUG_DEINIT, "nve: nve_shutdown\n");
704191783Srmacklem
705	sc = device_get_softc(dev);
706
707	/* Stop hardware activity */
708	nve_stop(sc);
709}
710
711/* Allocate TX ring buffers */
712static int
713nve_init_rings(struct nve_softc *sc)
714{
715	int error, i;
716
717	NVE_LOCK(sc);
718
719	DEBUGOUT(NVE_DEBUG_INIT, "nve: nve_init_rings - entry\n");
720
721	sc->cur_rx = sc->cur_tx = sc->pending_rxs = sc->pending_txs = 0;
722	/* Initialise RX ring */
723	for (i = 0; i < RX_RING_SIZE; i++) {
724		struct nve_rx_desc *desc = sc->rx_desc + i;
725		struct nve_map_buffer *buf = &desc->buf;
726
727		buf->mbuf = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
728		if (buf->mbuf == NULL) {
729			device_printf(sc->dev, "couldn't allocate mbuf\n");
730			nve_free_rings(sc);
731			error = ENOBUFS;
732			goto fail;
733		}
734		buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
735		m_adj(buf->mbuf, ETHER_ALIGN);
736
737		error = bus_dmamap_create(sc->mtag, 0, &buf->map);
738		if (error) {
739			device_printf(sc->dev, "couldn't create dma map\n");
740			nve_free_rings(sc);
741			goto fail;
742		}
743		error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
744					  nve_dmamap_rx_cb, &desc->paddr, 0);
745		if (error) {
746			device_printf(sc->dev, "couldn't dma map mbuf\n");
747			nve_free_rings(sc);
748			goto fail;
749		}
750		bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
751
752		desc->buflength = buf->mbuf->m_len;
753		desc->vaddr = mtod(buf->mbuf, caddr_t);
754	}
755	bus_dmamap_sync(sc->rtag, sc->rmap,
756	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
757
758	/* Initialize TX ring */
759	for (i = 0; i < TX_RING_SIZE; i++) {
760		struct nve_tx_desc *desc = sc->tx_desc + i;
761		struct nve_map_buffer *buf = &desc->buf;
762
763		buf->mbuf = NULL;
764
765		error = bus_dmamap_create(sc->mtag, 0, &buf->map);
766		if (error) {
767			device_printf(sc->dev, "couldn't create dma map\n");
768			nve_free_rings(sc);
769			goto fail;
770		}
771	}
772	bus_dmamap_sync(sc->ttag, sc->tmap,
773	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
774
775	DEBUGOUT(NVE_DEBUG_INIT, "nve: nve_init_rings - exit\n");
776
777fail:
778	NVE_UNLOCK(sc);
779
780	return (error);
781}
782
783/* Free the TX ring buffers */
784static void
785nve_free_rings(struct nve_softc *sc)
786{
787	int i;
788
789	NVE_LOCK(sc);
790
791	DEBUGOUT(NVE_DEBUG_DEINIT, "nve: nve_free_rings - entry\n");
792
793	for (i = 0; i < RX_RING_SIZE; i++) {
794		struct nve_rx_desc *desc = sc->rx_desc + i;
795		struct nve_map_buffer *buf = &desc->buf;
796
797		if (buf->mbuf) {
798			bus_dmamap_unload(sc->mtag, buf->map);
799			bus_dmamap_destroy(sc->mtag, buf->map);
800			m_freem(buf->mbuf);
801		}
802		buf->mbuf = NULL;
803	}
804
805	for (i = 0; i < TX_RING_SIZE; i++) {
806		struct nve_tx_desc *desc = sc->tx_desc + i;
807		struct nve_map_buffer *buf = &desc->buf;
808
809		if (buf->mbuf) {
810			bus_dmamap_unload(sc->mtag, buf->map);
811			bus_dmamap_destroy(sc->mtag, buf->map);
812			m_freem(buf->mbuf);
813		}
814		buf->mbuf = NULL;
815	}
816
817	DEBUGOUT(NVE_DEBUG_DEINIT, "nve: nve_free_rings - exit\n");
818
819	NVE_UNLOCK(sc);
820}
821
822/* Main loop for sending packets from OS to interface */
823static void
824nve_ifstart(struct ifnet *ifp)
825{
826	struct nve_softc *sc = ifp->if_softc;
827	struct nve_map_buffer *buf;
828	struct mbuf    *m0, *m;
829	struct nve_tx_desc *desc;
830	ADAPTER_WRITE_DATA txdata;
831	int error, i;
832
833	DEBUGOUT(NVE_DEBUG_RUNNING, "nve: nve_ifstart - entry\n");
834
835	/* If link is down/busy or queue is empty do nothing */
836	if (ifp->if_flags & IFF_OACTIVE || ifp->if_snd.ifq_head == NULL)
837		return;
838
839	/* Transmit queued packets until sent or TX ring is full */
840	while (sc->pending_txs < TX_RING_SIZE) {
841		desc = sc->tx_desc + sc->cur_tx;
842		buf = &desc->buf;
843
844		/* Get next packet to send. */
845		IF_DEQUEUE(&ifp->if_snd, m0);
846
847		/* If nothing to send, return. */
848		if (m0 == NULL)
849			return;
850
851		/* Map MBUF for DMA access */
852		error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m0,
853		    nve_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
854
855		if (error && error != EFBIG) {
856			m_freem(m0);
857			sc->tx_errors++;
858			continue;
859		}
860		/*
861		 * Packet has too many fragments - defrag into new mbuf
862		 * cluster
863		 */
864		if (error) {
865			m = m_defrag(m0, M_DONTWAIT);
866			if (m == NULL) {
867				m_freem(m0);
868				sc->tx_errors++;
869				continue;
870			}
871			m_freem(m0);
872			m0 = m;
873
874			error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m,
875			    nve_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
876			if (error) {
877				m_freem(m);
878				sc->tx_errors++;
879				continue;
880			}
881		}
882		/* Do sync on DMA bounce buffer */
883		bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREWRITE);
884
885		buf->mbuf = m0;
886		txdata.ulNumberOfElements = desc->numfrags;
887		txdata.pvID = (PVOID)desc;
888
889		/* Put fragments into API element list */
890		txdata.ulTotalLength = buf->mbuf->m_len;
891		for (i = 0; i < desc->numfrags; i++) {
892			txdata.sElement[i].ulLength =
893			    (ulong)desc->frags[i].ds_len;
894			txdata.sElement[i].pPhysical =
895			    (PVOID)desc->frags[i].ds_addr;
896		}
897
898		/* Send packet to Nvidia API for transmission */
899		error = sc->hwapi->pfnWrite(sc->hwapi->pADCX, &txdata);
900
901		switch (error) {
902		case ADAPTERERR_NONE:
903			/* Packet was queued in API TX queue successfully */
904			sc->pending_txs++;
905			sc->cur_tx = (sc->cur_tx + 1) % TX_RING_SIZE;
906			break;
907
908		case ADAPTERERR_TRANSMIT_QUEUE_FULL:
909			/* The API TX queue is full - requeue the packet */
910			device_printf(sc->dev,
911			    "nve_ifstart: transmit queue is full\n");
912			ifp->if_flags |= IFF_OACTIVE;
913			bus_dmamap_unload(sc->mtag, buf->map);
914			IF_PREPEND(&ifp->if_snd, buf->mbuf);
915			buf->mbuf = NULL;
916			return;
917
918		default:
919			/* The API failed to queue/send the packet so dump it */
920			device_printf(sc->dev, "nve_ifstart: transmit error\n");
921			bus_dmamap_unload(sc->mtag, buf->map);
922			m_freem(buf->mbuf);
923			buf->mbuf = NULL;
924			sc->tx_errors++;
925			return;
926		}
927		/* Set watchdog timer. */
928		ifp->if_timer = 8;
929
930		/* Copy packet to BPF tap */
931		BPF_MTAP(ifp, m0);
932	}
933	ifp->if_flags |= IFF_OACTIVE;
934
935	DEBUGOUT(NVE_DEBUG_RUNNING, "nve: nve_ifstart - exit\n");
936}
937
938/* Handle IOCTL events */
939static int
940nve_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
941{
942	struct nve_softc *sc = ifp->if_softc;
943	struct ifreq *ifr = (struct ifreq *) data;
944	struct mii_data *mii;
945	int error = 0;
946
947	NVE_LOCK(sc);
948
949	DEBUGOUT(NVE_DEBUG_IOCTL, "nve: nve_ioctl - entry\n");
950
951	switch (command) {
952	case SIOCSIFMTU:
953		/* Set MTU size */
954		if (ifp->if_mtu == ifr->ifr_mtu)
955			break;
956		if (ifr->ifr_mtu + ifp->if_hdrlen <= MAX_PACKET_SIZE_1518) {
957			ifp->if_mtu = ifr->ifr_mtu;
958			nve_stop(sc);
959			nve_init(sc);
960		} else
961			error = EINVAL;
962		break;
963
964	case SIOCSIFFLAGS:
965		/* Setup interface flags */
966		if (ifp->if_flags & IFF_UP) {
967			if ((ifp->if_flags & IFF_RUNNING) == 0) {
968				nve_init(sc);
969				break;
970			}
971		} else {
972			if (ifp->if_flags & IFF_RUNNING) {
973				nve_stop(sc);
974				break;
975			}
976		}
977		/* Handle IFF_PROMISC and IFF_ALLMULTI flags. */
978		nve_setmulti(sc);
979		break;
980
981	case SIOCADDMULTI:
982	case SIOCDELMULTI:
983		/* Setup multicast filter */
984		if (ifp->if_flags & IFF_RUNNING) {
985			nve_setmulti(sc);
986		}
987		break;
988
989	case SIOCGIFMEDIA:
990	case SIOCSIFMEDIA:
991		/* Get/Set interface media parameters */
992		mii = device_get_softc(sc->miibus);
993		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
994		break;
995
996	default:
997		/* Everything else we forward to generic ether ioctl */
998		error = ether_ioctl(ifp, (int)command, data);
999		break;
1000	}
1001
1002	DEBUGOUT(NVE_DEBUG_IOCTL, "nve: nve_ioctl - exit\n");
1003
1004	NVE_UNLOCK(sc);
1005
1006	return (error);
1007}
1008
1009/* Interrupt service routine */
1010static void
1011nve_intr(void *arg)
1012{
1013	struct nve_softc *sc = arg;
1014	struct ifnet *ifp = &sc->sc_if;
1015
1016	DEBUGOUT(NVE_DEBUG_INTERRUPT, "nve: nve_intr - entry\n");
1017
1018	if (!ifp->if_flags & IFF_UP) {
1019		nve_stop(sc);
1020		return;
1021	}
1022	/* Handle interrupt event */
1023	if (sc->hwapi->pfnQueryInterrupt(sc->hwapi->pADCX)) {
1024		sc->hwapi->pfnHandleInterrupt(sc->hwapi->pADCX);
1025		sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1026	}
1027	if (ifp->if_snd.ifq_head != NULL)
1028		nve_ifstart(ifp);
1029
1030	/* If no pending packets we don't need a timeout */
1031	if (sc->pending_txs == 0)
1032		sc->sc_if.if_timer = 0;
1033
1034	DEBUGOUT(NVE_DEBUG_INTERRUPT, "nve: nve_intr - exit\n");
1035
1036	return;
1037}
1038
1039/* Setup multicast filters */
1040static void
1041nve_setmulti(struct nve_softc *sc)
1042{
1043	struct ifnet *ifp;
1044	struct ifmultiaddr *ifma;
1045	PACKET_FILTER hwfilter;
1046	int i;
1047	u_int8_t andaddr[6], oraddr[6];
1048
1049	NVE_LOCK(sc);
1050
1051	DEBUGOUT(NVE_DEBUG_RUNNING, "nve: nve_setmulti - entry\n");
1052
1053	ifp = &sc->sc_if;
1054
1055	/* Initialize filter */
1056	hwfilter.ulFilterFlags = 0;
1057	for (i = 0; i < 6; i++) {
1058		hwfilter.acMulticastAddress[i] = 0;
1059		hwfilter.acMulticastMask[i] = 0;
1060	}
1061
1062	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1063		/* Accept all packets */
1064		hwfilter.ulFilterFlags |= ACCEPT_ALL_PACKETS;
1065		sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1066		NVE_UNLOCK(sc);
1067		return;
1068	}
1069	/* Setup multicast filter */
1070	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1071		u_char *addrp;
1072
1073		if (ifma->ifma_addr->sa_family != AF_LINK)
1074			continue;
1075
1076		addrp = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
1077		for (i = 0; i < 6; i++) {
1078			u_int8_t mcaddr = addrp[i];
1079			andaddr[i] &= mcaddr;
1080			oraddr[i] |= mcaddr;
1081		}
1082	}
1083	for (i = 0; i < 6; i++) {
1084		hwfilter.acMulticastAddress[i] = andaddr[i] & oraddr[i];
1085		hwfilter.acMulticastMask[i] = andaddr[i] | (~oraddr[i]);
1086	}
1087
1088	/* Send filter to NVIDIA API */
1089	sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1090
1091	NVE_UNLOCK(sc);
1092
1093	DEBUGOUT(NVE_DEBUG_RUNNING, "nve: nve_setmulti - exit\n");
1094
1095	return;
1096}
1097
1098/* Change the current media/mediaopts */
1099static int
1100nve_ifmedia_upd(struct ifnet *ifp)
1101{
1102	struct nve_softc *sc = ifp->if_softc;
1103	struct mii_data *mii;
1104
1105	DEBUGOUT(NVE_DEBUG_MII, "nve: nve_ifmedia_upd\n");
1106
1107	mii = device_get_softc(sc->miibus);
1108
1109	if (mii->mii_instance) {
1110		struct mii_softc *miisc;
1111		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1112		    miisc = LIST_NEXT(miisc, mii_list)) {
1113			mii_phy_reset(miisc);
1114		}
1115	}
1116	mii_mediachg(mii);
1117
1118	return (0);
1119}
1120
1121/* Update current miibus PHY status of media */
1122static void
1123nve_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1124{
1125	struct nve_softc *sc;
1126	struct mii_data *mii;
1127
1128	DEBUGOUT(NVE_DEBUG_MII, "nve: nve_ifmedia_sts\n");
1129
1130	sc = ifp->if_softc;
1131	mii = device_get_softc(sc->miibus);
1132	mii_pollstat(mii);
1133
1134	ifmr->ifm_active = mii->mii_media_active;
1135	ifmr->ifm_status = mii->mii_media_status;
1136
1137	return;
1138}
1139
1140/* miibus tick timer - maintain link status */
1141static void
1142nve_tick(void *xsc)
1143{
1144	struct nve_softc *sc = xsc;
1145	struct mii_data *mii;
1146	struct ifnet *ifp;
1147
1148	NVE_LOCK(sc);
1149
1150	ifp = &sc->sc_if;
1151	nve_update_stats(sc);
1152
1153	mii = device_get_softc(sc->miibus);
1154	mii_tick(mii);
1155
1156	if (mii->mii_media_status & IFM_ACTIVE &&
1157	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1158		if (ifp->if_snd.ifq_head != NULL)
1159			nve_ifstart(ifp);
1160	}
1161	sc->stat_ch = timeout(nve_tick, sc, hz);
1162
1163	NVE_UNLOCK(sc);
1164
1165	return;
1166}
1167
1168/* Update ifnet data structure with collected interface stats from API */
1169static void
1170nve_update_stats(struct nve_softc *sc)
1171{
1172	struct ifnet *ifp = &sc->sc_if;
1173	ADAPTER_STATS stats;
1174
1175	NVE_LOCK(sc);
1176
1177	if (sc->hwapi) {
1178		sc->hwapi->pfnGetStatistics(sc->hwapi->pADCX, &stats);
1179
1180		ifp->if_ipackets = stats.ulSuccessfulReceptions;
1181		ifp->if_ierrors = stats.ulMissedFrames +
1182			stats.ulFailedReceptions +
1183			stats.ulCRCErrors +
1184			stats.ulFramingErrors +
1185			stats.ulOverFlowErrors;
1186
1187		ifp->if_opackets = stats.ulSuccessfulTransmissions;
1188		ifp->if_oerrors = sc->tx_errors +
1189			stats.ulFailedTransmissions +
1190			stats.ulRetryErrors +
1191			stats.ulUnderflowErrors +
1192			stats.ulLossOfCarrierErrors +
1193			stats.ulLateCollisionErrors;
1194
1195		ifp->if_collisions = stats.ulLateCollisionErrors;
1196	}
1197	NVE_UNLOCK(sc);
1198
1199	return;
1200}
1201
1202/* miibus Read PHY register wrapper - calls Nvidia API entry point */
1203static int
1204nve_miibus_readreg(device_t dev, int phy, int reg)
1205{
1206	struct nve_softc *sc = device_get_softc(dev);
1207	ULONG data;
1208
1209	DEBUGOUT(NVE_DEBUG_MII, "nve: nve_miibus_readreg - entry\n");
1210
1211	ADAPTER_ReadPhy(sc->hwapi->pADCX, phy, reg, &data);
1212
1213	DEBUGOUT(NVE_DEBUG_MII, "nve: nve_miibus_readreg - exit\n");
1214
1215	return (data);
1216}
1217
1218/* miibus Write PHY register wrapper - calls Nvidia API entry point */
1219static void
1220nve_miibus_writereg(device_t dev, int phy, int reg, int data)
1221{
1222	struct nve_softc *sc = device_get_softc(dev);
1223
1224	DEBUGOUT(NVE_DEBUG_MII, "nve: nve_miibus_writereg - entry\n");
1225
1226	ADAPTER_WritePhy(sc->hwapi->pADCX, phy, reg, (ulong)data);
1227
1228	DEBUGOUT(NVE_DEBUG_MII, "nve: nve_miibus_writereg - exit\n");
1229
1230	return;
1231}
1232
1233/* Watchdog timer to prevent PHY lockups */
1234static void
1235nve_watchdog(struct ifnet *ifp)
1236{
1237	struct nve_softc *sc = ifp->if_softc;
1238
1239	device_printf(sc->dev, "device timeout (%d)\n", sc->pending_txs);
1240
1241	sc->tx_errors++;
1242
1243	nve_stop(sc);
1244	ifp->if_flags &= ~IFF_RUNNING;
1245	nve_init(sc);
1246
1247	if (ifp->if_snd.ifq_head != NULL)
1248		nve_ifstart(ifp);
1249
1250	return;
1251}
1252
1253/* --- Start of NVOSAPI interface --- */
1254
1255/* Allocate DMA enabled general use memory for API */
1256static NV_SINT32
1257nve_osalloc(PNV_VOID ctx, PMEMORY_BLOCK mem)
1258{
1259	struct nve_softc *sc;
1260	bus_addr_t mem_physical;
1261
1262	DEBUGOUT(NVE_DEBUG_API, "nve: nve_osalloc - %d\n", mem->uiLength);
1263
1264	sc = (struct nve_softc *)ctx;
1265
1266	mem->pLogical = (PVOID)contigmalloc(mem->uiLength, M_DEVBUF,
1267	    M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE, 0);
1268
1269	if (!mem->pLogical) {
1270		device_printf(sc->dev, "memory allocation failed\n");
1271		return (0);
1272	}
1273	memset(mem->pLogical, 0, (ulong)mem->uiLength);
1274	mem_physical = vtophys(mem->pLogical);
1275	mem->pPhysical = (PVOID)mem_physical;
1276
1277	DEBUGOUT(NVE_DEBUG_API, "nve: nve_osalloc 0x%x/0x%x - %d\n",
1278	    (uint)mem->pLogical, (uint)mem->pPhysical, (uint)mem->uiLength);
1279
1280	return (1);
1281}
1282
1283/* Free allocated memory */
1284static NV_SINT32
1285nve_osfree(PNV_VOID ctx, PMEMORY_BLOCK mem)
1286{
1287	DEBUGOUT(NVE_DEBUG_API, "nve: nve_osfree - 0x%x - %d\n",
1288	    (uint)mem->pLogical, (uint) mem->uiLength);
1289
1290	contigfree(mem->pLogical, PAGE_SIZE, M_DEVBUF);
1291	return (1);
1292}
1293
1294/* Copied directly from nvnet.c */
1295static NV_SINT32
1296nve_osallocex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1297{
1298	MEMORY_BLOCK mem_block;
1299
1300	DEBUGOUT(NVE_DEBUG_API, "nve: nve_osallocex\n");
1301
1302	mem_block_ex->pLogical = NULL;
1303	mem_block_ex->uiLengthOrig = mem_block_ex->uiLength;
1304
1305	if ((mem_block_ex->AllocFlags & ALLOC_MEMORY_ALIGNED) &&
1306	    (mem_block_ex->AlignmentSize > 1)) {
1307		DEBUGOUT(NVE_DEBUG_API, "     aligning on %d\n",
1308		    mem_block_ex->AlignmentSize);
1309		mem_block_ex->uiLengthOrig += mem_block_ex->AlignmentSize;
1310	}
1311	mem_block.uiLength = mem_block_ex->uiLengthOrig;
1312
1313	if (nve_osalloc(ctx, &mem_block) == 0) {
1314		return (0);
1315	}
1316	mem_block_ex->pLogicalOrig = mem_block.pLogical;
1317	mem_block_ex->pPhysicalOrigLow = (unsigned long)mem_block.pPhysical;
1318	mem_block_ex->pPhysicalOrigHigh = 0;
1319
1320	mem_block_ex->pPhysical = mem_block.pPhysical;
1321	mem_block_ex->pLogical = mem_block.pLogical;
1322
1323	if (mem_block_ex->uiLength != mem_block_ex->uiLengthOrig) {
1324		unsigned int offset;
1325		offset = mem_block_ex->pPhysicalOrigLow &
1326		    (mem_block_ex->AlignmentSize - 1);
1327
1328		if (offset) {
1329			mem_block_ex->pPhysical =
1330			    (PVOID)((ulong)mem_block_ex->pPhysical +
1331			    mem_block_ex->AlignmentSize - offset);
1332			mem_block_ex->pLogical =
1333			    (PVOID)((ulong)mem_block_ex->pLogical +
1334			    mem_block_ex->AlignmentSize - offset);
1335		} /* if (offset) */
1336	} /* if (mem_block_ex->uiLength != *mem_block_ex->uiLengthOrig) */
1337	return (1);
1338}
1339
1340/* Copied directly from nvnet.c */
1341static NV_SINT32
1342nve_osfreeex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1343{
1344	MEMORY_BLOCK mem_block;
1345
1346	DEBUGOUT(NVE_DEBUG_API, "nve: nve_osfreeex\n");
1347
1348	mem_block.pLogical = mem_block_ex->pLogicalOrig;
1349	mem_block.pPhysical = (PVOID)((ulong)mem_block_ex->pPhysicalOrigLow);
1350	mem_block.uiLength = mem_block_ex->uiLengthOrig;
1351
1352	return (nve_osfree(ctx, &mem_block));
1353}
1354
1355/* Clear memory region */
1356static NV_SINT32
1357nve_osclear(PNV_VOID ctx, PNV_VOID mem, NV_SINT32 length)
1358{
1359	DEBUGOUT(NVE_DEBUG_API, "nve: nve_osclear\n");
1360	memset(mem, 0, length);
1361	return (1);
1362}
1363
1364/* Sleep for a tick */
1365static NV_SINT32
1366nve_osdelay(PNV_VOID ctx, NV_UINT32 usec)
1367{
1368	DELAY(usec);
1369	return (1);
1370}
1371
1372/* Allocate memory for rx buffer */
1373static NV_SINT32
1374nve_osallocrxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID *id)
1375{
1376	struct nve_softc *sc = ctx;
1377	struct nve_rx_desc *desc;
1378	struct nve_map_buffer *buf;
1379	int error;
1380
1381	NVE_LOCK(sc);
1382
1383	DEBUGOUT(NVE_DEBUG_API, "nve: nve_osallocrxbuf\n");
1384
1385	if (sc->pending_rxs == RX_RING_SIZE) {
1386		device_printf(sc->dev, "rx ring buffer is full\n");
1387		goto fail;
1388	}
1389	desc = sc->rx_desc + sc->cur_rx;
1390	buf = &desc->buf;
1391
1392	if (buf->mbuf == NULL) {
1393		buf->mbuf = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1394		if (buf->mbuf == NULL) {
1395			device_printf(sc->dev, "failed to allocate memory\n");
1396			goto fail;
1397		}
1398		buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
1399		m_adj(buf->mbuf, ETHER_ALIGN);
1400
1401		error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
1402		    nve_dmamap_rx_cb, &desc->paddr, 0);
1403		if (error) {
1404			device_printf(sc->dev, "failed to dmamap mbuf\n");
1405			m_freem(buf->mbuf);
1406			buf->mbuf = NULL;
1407			goto fail;
1408		}
1409		bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
1410		desc->buflength = buf->mbuf->m_len;
1411		desc->vaddr = mtod(buf->mbuf, caddr_t);
1412	}
1413	sc->pending_rxs++;
1414	sc->cur_rx = (sc->cur_rx + 1) % RX_RING_SIZE;
1415
1416	mem->pLogical = (void *)desc->vaddr;
1417	mem->pPhysical = (void *)desc->paddr;
1418	mem->uiLength = desc->buflength;
1419	*id = (void *)desc;
1420
1421	NVE_UNLOCK(sc);
1422	return (1);
1423
1424fail:
1425	NVE_UNLOCK(sc);
1426	return (0);
1427}
1428
1429/* Free the rx buffer */
1430static NV_SINT32
1431nve_osfreerxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID id)
1432{
1433	struct nve_softc *sc = ctx;
1434	struct nve_rx_desc *desc;
1435	struct nve_map_buffer *buf;
1436
1437	NVE_LOCK(sc);
1438
1439	DEBUGOUT(NVE_DEBUG_API, "nve: nve_osfreerxbuf\n");
1440
1441	desc = (struct nve_rx_desc *) id;
1442	buf = &desc->buf;
1443
1444	if (buf->mbuf) {
1445		bus_dmamap_unload(sc->mtag, buf->map);
1446		bus_dmamap_destroy(sc->mtag, buf->map);
1447		m_freem(buf->mbuf);
1448	}
1449	sc->pending_rxs--;
1450	buf->mbuf = NULL;
1451
1452	NVE_UNLOCK(sc);
1453
1454	return (1);
1455}
1456
1457/* This gets called by the Nvidia API after our TX packet has been sent */
1458static NV_SINT32
1459nve_ospackettx(PNV_VOID ctx, PNV_VOID id, NV_UINT32 success)
1460{
1461	struct nve_softc *sc = ctx;
1462	struct nve_map_buffer *buf;
1463	struct nve_tx_desc *desc = (struct nve_tx_desc *) id;
1464	struct ifnet *ifp;
1465
1466	NVE_LOCK(sc);
1467
1468	DEBUGOUT(NVE_DEBUG_API, "nve: nve_ospackettx\n");
1469
1470	ifp = &sc->sc_if;
1471	buf = &desc->buf;
1472	sc->pending_txs--;
1473
1474	/* Unload and free mbuf cluster */
1475	if (buf->mbuf == NULL)
1476		goto fail;
1477
1478	bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTWRITE);
1479	bus_dmamap_unload(sc->mtag, buf->map);
1480	m_freem(buf->mbuf);
1481	buf->mbuf = NULL;
1482
1483	/* Send more packets if we have them */
1484	if (sc->pending_txs < TX_RING_SIZE)
1485		sc->sc_if.if_flags &= ~IFF_OACTIVE;
1486
1487	if (ifp->if_snd.ifq_head != NULL && sc->pending_txs < TX_RING_SIZE)
1488		nve_ifstart(ifp);
1489
1490fail:
1491	NVE_UNLOCK(sc);
1492
1493	return (1);
1494}
1495
1496/* This gets called by the Nvidia API when a new packet has been received */
1497/* XXX What is newbuf used for? XXX */
1498static NV_SINT32
1499nve_ospacketrx(PNV_VOID ctx, PNV_VOID data, NV_UINT32 success, NV_UINT8 *newbuf,
1500    NV_UINT8 priority)
1501{
1502	struct nve_softc *sc = ctx;
1503	struct ifnet *ifp;
1504	struct nve_rx_desc *desc;
1505	struct nve_map_buffer *buf;
1506	ADAPTER_READ_DATA *readdata;
1507
1508	NVE_LOCK(sc);
1509
1510	DEBUGOUT(NVE_DEBUG_API, "nve: nve_ospacketrx\n");
1511
1512	ifp = &sc->sc_if;
1513
1514	readdata = (ADAPTER_READ_DATA *) data;
1515	desc = readdata->pvID;
1516	buf = &desc->buf;
1517	bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1518
1519	if (success) {
1520		/* Sync DMA bounce buffer. */
1521		bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1522
1523		/* First mbuf in packet holds the ethernet and packet headers */
1524		buf->mbuf->m_pkthdr.rcvif = ifp;
1525		buf->mbuf->m_pkthdr.len = buf->mbuf->m_len =
1526		    readdata->ulTotalLength;
1527
1528		bus_dmamap_unload(sc->mtag, buf->map);
1529
1530		/* Give mbuf to OS. */
1531		(*ifp->if_input) (ifp, buf->mbuf);
1532		if (readdata->ulFilterMatch & ADREADFL_MULTICAST_MATCH)
1533			ifp->if_imcasts++;
1534
1535		/* Blat the mbuf pointer, kernel will free the mbuf cluster */
1536		buf->mbuf = NULL;
1537	} else {
1538		bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1539		bus_dmamap_unload(sc->mtag, buf->map);
1540		m_freem(buf->mbuf);
1541		buf->mbuf = NULL;
1542	}
1543
1544	sc->cur_rx = desc - sc->rx_desc;
1545	sc->pending_rxs--;
1546
1547	NVE_UNLOCK(sc);
1548
1549	return (1);
1550}
1551
1552/* This gets called by NVIDIA API when the PHY link state changes */
1553static NV_SINT32
1554nve_oslinkchg(PNV_VOID ctx, NV_SINT32 enabled)
1555{
1556	struct nve_softc *sc = (struct nve_softc *)ctx;
1557	struct ifnet *ifp;
1558
1559	DEBUGOUT(NVE_DEBUG_API, "nve: nve_oslinkchg\n");
1560
1561	ifp = &sc->sc_if;
1562
1563	if (enabled)
1564		ifp->if_flags |= IFF_UP;
1565	else
1566		ifp->if_flags &= ~IFF_UP;
1567
1568	return (1);
1569}
1570
1571/* Setup a watchdog timer */
1572static NV_SINT32
1573nve_osalloctimer(PNV_VOID ctx, PNV_VOID *timer)
1574{
1575	struct nve_softc *sc = (struct nve_softc *)ctx;
1576
1577	DEBUGOUT(NVE_DEBUG_BROKEN, "nve: nve_osalloctimer\n");
1578
1579	callout_handle_init(&sc->ostimer);
1580	*timer = &sc->ostimer;
1581
1582	return (1);
1583}
1584
1585/* Free the timer */
1586static NV_SINT32
1587nve_osfreetimer(PNV_VOID ctx, PNV_VOID timer)
1588{
1589
1590	DEBUGOUT(NVE_DEBUG_BROKEN, "nve: nve_osfreetimer\n");
1591
1592	return (1);
1593}
1594
1595/* Setup timer parameters */
1596static NV_SINT32
1597nve_osinittimer(PNV_VOID ctx, PNV_VOID timer, PTIMER_FUNC func, PNV_VOID parameters)
1598{
1599	struct nve_softc *sc = (struct nve_softc *)ctx;
1600
1601	DEBUGOUT(NVE_DEBUG_BROKEN, "nve: nve_osinittimer\n");
1602
1603	sc->ostimer_func = func;
1604	sc->ostimer_params = parameters;
1605
1606	return (1);
1607}
1608
1609/* Set the timer to go off */
1610static NV_SINT32
1611nve_ossettimer(PNV_VOID ctx, PNV_VOID timer, NV_UINT32 delay)
1612{
1613	struct nve_softc *sc = ctx;
1614
1615	DEBUGOUT(NVE_DEBUG_BROKEN, "nve: nve_ossettimer\n");
1616
1617	*(struct callout_handle *)timer = timeout(sc->ostimer_func,
1618	    sc->ostimer_params, delay);
1619
1620	return (1);
1621}
1622
1623/* Cancel the timer */
1624static NV_SINT32
1625nve_oscanceltimer(PNV_VOID ctx, PNV_VOID timer)
1626{
1627	struct nve_softc *sc = ctx;
1628
1629	DEBUGOUT(NVE_DEBUG_BROKEN, "nve: nve_oscanceltimer\n");
1630
1631	untimeout(sc->ostimer_func, sc->ostimer_params,
1632	    *(struct callout_handle *)timer);
1633
1634	return (1);
1635}
1636
1637static NV_SINT32
1638nve_ospreprocpkt(PNV_VOID ctx, PNV_VOID readdata, PNV_VOID *id,
1639    NV_UINT8 *newbuffer, NV_UINT8 priority)
1640{
1641
1642	/* Not implemented */
1643	DEBUGOUT(NVE_DEBUG_BROKEN, "nve: nve_ospreprocpkt\n");
1644
1645	return (1);
1646}
1647
1648static PNV_VOID
1649nve_ospreprocpktnopq(PNV_VOID ctx, PNV_VOID readdata)
1650{
1651
1652	/* Not implemented */
1653	DEBUGOUT(NVE_DEBUG_BROKEN, "nve: nve_ospreprocpkt\n");
1654
1655	return (NULL);
1656}
1657
1658static NV_SINT32
1659nve_osindicatepkt(PNV_VOID ctx, PNV_VOID *id, NV_UINT32 pktno)
1660{
1661
1662	/* Not implemented */
1663	DEBUGOUT(NVE_DEBUG_BROKEN, "nve: nve_osindicatepkt\n");
1664
1665	return (1);
1666}
1667
1668/* Allocate mutex context (already done in nve_attach) */
1669static NV_SINT32
1670nve_oslockalloc(PNV_VOID ctx, NV_SINT32 type, PNV_VOID *pLock)
1671{
1672	struct nve_softc *sc = (struct nve_softc *)ctx;
1673
1674	DEBUGOUT(NVE_DEBUG_LOCK, "nve: nve_oslockalloc\n");
1675
1676	*pLock = (void **)sc;
1677
1678	return (1);
1679}
1680
1681/* Obtain a spin lock */
1682static NV_SINT32
1683nve_oslockacquire(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1684{
1685
1686	DEBUGOUT(NVE_DEBUG_LOCK, "nve: nve_oslockacquire\n");
1687
1688	NVE_OSLOCK((struct nve_softc *)lock);
1689
1690	return (1);
1691}
1692
1693/* Release lock */
1694static NV_SINT32
1695nve_oslockrelease(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1696{
1697
1698	DEBUGOUT(NVE_DEBUG_LOCK, "nve: nve_oslockrelease\n");
1699
1700	NVE_OSUNLOCK((struct nve_softc *)lock);
1701
1702	return (1);
1703}
1704
1705/* I have no idea what this is for */
1706static PNV_VOID
1707nve_osreturnbufvirt(PNV_VOID ctx, PNV_VOID readdata)
1708{
1709
1710	/* Not implemented */
1711	DEBUGOUT(NVE_DEBUG_LOCK, "nve: nve_osreturnbufvirt\n");
1712	panic("nve: nve_osreturnbufvirtual not implemented\n");
1713
1714	return (NULL);
1715}
1716
1717/* --- End on NVOSAPI interface --- */
1718