b879fb726cffe444cdfbee473a11a077231f7160
[dragonfly.git] / sys / dev / netif / nv / if_nv.c
1 /*
2  * Copyright (c) 2003, 2004 by Quinton Dolan <q@onthenet.com.au>. 
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions 
7  * are met: 
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  * 2. Redistributions in binary form must reproduce the above copyright 
11  *    notice, this list of conditions and the following disclaimer in the 
12  *    documentation and/or other materials provided with the distribution.
13  * 
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  * 
26  * $Id: if_nv.c,v 1.20 2005/03/12 01:11:00 q Exp $
27  * $DragonFly: src/sys/dev/netif/nv/Attic/if_nv.c,v 1.22 2005/11/22 00:24:33 dillon Exp $
28  */
29
30 /*
31  * NVIDIA nForce MCP Networking Adapter driver
32  * 
33  * This is a port of the NVIDIA MCP Linux ethernet driver distributed by NVIDIA
34  * through their web site.
35  * 
36  * All mainstream nForce and nForce2 motherboards are supported. This module
37  * is as stable, sometimes more stable, than the linux version. (Recent
38  * Linux stability issues seem to be related to some issues with newer
39  * distributions using GCC 3.x, however this don't appear to effect FreeBSD
40  * 5.x).
41  * 
42  * In accordance with the NVIDIA distribution license it is necessary to
43  * link this module against the nvlibnet.o binary object included in the
44  * Linux driver source distribution. The binary component is not modified in
45  * any way and is simply linked against a FreeBSD equivalent of the nvnet.c
46  * linux kernel module "wrapper".
47  * 
48  * The Linux driver uses a common code API that is shared between Win32 and
49  * i386 Linux. This abstracts the low level driver functions and uses
50  * callbacks and hooks to access the underlying hardware device. By using
51  * this same API in a FreeBSD kernel module it is possible to support the
52  * hardware without breaching the Linux source distributions licensing
53  * requirements, or obtaining the hardware programming specifications.
54  * 
55  * Although not conventional, it works, and given the relatively small
56  * amount of hardware centric code, it's hopefully no more buggy than its
57  * linux counterpart.
58  *
59  * NVIDIA now suppport the nForce3 AMD64 platform, however I have been
60  * unable to access such a system to verify support. However, the code is
61  * reported to work with little modification when compiled with the AMD64
62  * version of the NVIDIA Linux library. All that should be necessary to make
63  * the driver work is to link it directly into the kernel, instead of as a
64  * module, and apply the docs/amd64.diff patch in this source distribution to
65  * the NVIDIA Linux driver source.
66  *
67  * This driver should work on all versions of FreeBSD since 4.9/5.1 as well
68  * as recent versions of DragonFly.
69  *
70  * Written by Quinton Dolan <q@onthenet.com.au> 
71  * Portions based on existing FreeBSD network drivers. 
72  * NVIDIA API usage derived from distributed NVIDIA NVNET driver source files.
73  * 
74  * $Id: if_nv.c,v 1.9 2003/12/13 15:27:40 q Exp $
75  */
76
77 #include "opt_polling.h"
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/sockio.h>
82 #include <sys/mbuf.h>
83 #include <sys/malloc.h>
84 #include <sys/kernel.h>
85 #include <sys/socket.h>
86 #include <sys/sysctl.h>
87 #include <sys/queue.h>
88 #include <sys/module.h>
89 #include <sys/thread2.h>
90
91 #include <net/if.h>
92 #include <net/ifq_var.h>
93 #include <net/if_arp.h>
94 #include <net/ethernet.h>
95 #include <net/if_dl.h>
96 #include <net/if_media.h>
97
98 #include <net/bpf.h>
99
100 #include <net/vlan/if_vlan_var.h>
101
102 #include <machine/bus_memio.h>
103 #include <machine/bus.h>
104 #include <machine/resource.h>
105
106 #include <vm/vm.h>              /* for vtophys */
107 #include <vm/pmap.h>            /* for vtophys */
108 #include <machine/clock.h>      /* for DELAY */
109 #include <sys/bus.h>
110 #include <sys/rman.h>
111 #include <sys/serialize.h>
112
113 #include <bus/pci/pcireg.h>
114 #include <bus/pci/pcivar.h>
115
116 #include <dev/netif/mii_layer/mii.h>
117 #include <dev/netif/mii_layer/miivar.h>
118
119 MODULE_DEPEND(nv, pci, 1, 1, 1);
120 MODULE_DEPEND(nv, miibus, 1, 1, 1);
121
122 #include "if_nvreg.h"
123 #include "miibus_if.h"
124
125 static int      nv_probe(device_t);
126 static int      nv_attach(device_t);
127 static int      nv_detach(device_t);
128 static void     nv_init(void *);
129 static void     nv_init_serialized(struct nv_softc *);
130 static void     nv_stop(struct nv_softc *);
131 static void     nv_stop_serialized(struct nv_softc *);
132 static void     nv_shutdown(device_t);
133 static int      nv_init_rings(struct nv_softc *);
134 static void     nv_free_rings(struct nv_softc *);
135
136 static void     nv_ifstart(struct ifnet *);
137 static void     nv_ifstart_serialized(struct ifnet *);
138 static int      nv_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
139 static void     nv_intr(void *);
140 static void     nv_tick(void *);
141 static void     nv_setmulti(struct nv_softc *);
142 static void     nv_watchdog(struct ifnet *);
143 static void     nv_update_stats(struct nv_softc *);
144 #ifdef DEVICE_POLLING
145 static void     nv_poll(struct ifnet *, enum poll_cmd, int);
146 #endif
147
148 static int      nv_ifmedia_upd(struct ifnet *);
149 static void     nv_ifmedia_sts(struct ifnet *, struct ifmediareq *);
150 static int      nv_miibus_readreg(device_t, int, int);
151 static void     nv_miibus_writereg(device_t, int, int, int);
152
153 static void     nv_dmamap_cb(void *, bus_dma_segment_t *, int, int);
154 static void     nv_dmamap_tx_cb(void *, bus_dma_segment_t *, int, bus_size_t, int);
155
156 static NV_SINT32 nv_osalloc(PNV_VOID, PMEMORY_BLOCK);
157 static NV_SINT32 nv_osfree(PNV_VOID, PMEMORY_BLOCK);
158 static NV_SINT32 nv_osallocex(PNV_VOID, PMEMORY_BLOCKEX);
159 static NV_SINT32 nv_osfreeex(PNV_VOID, PMEMORY_BLOCKEX);
160 static NV_SINT32 nv_osclear(PNV_VOID, PNV_VOID, NV_SINT32);
161 static NV_SINT32 nv_osdelay(PNV_VOID, NV_UINT32);
162 static NV_SINT32 nv_osallocrxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID *);
163 static NV_SINT32 nv_osfreerxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID);
164 static NV_SINT32 nv_ospackettx(PNV_VOID, PNV_VOID, NV_UINT32);
165 static NV_SINT32 nv_ospacketrx(PNV_VOID, PNV_VOID, NV_UINT32, NV_UINT8 *, NV_UINT8);
166 static NV_SINT32 nv_oslinkchg(PNV_VOID, NV_SINT32);
167 static NV_SINT32 nv_osalloctimer(PNV_VOID, PNV_VOID *);
168 static NV_SINT32 nv_osfreetimer(PNV_VOID, PNV_VOID);
169 static NV_SINT32 nv_osinittimer(PNV_VOID, PNV_VOID, PTIMER_FUNC, PNV_VOID);
170 static NV_SINT32 nv_ossettimer(PNV_VOID, PNV_VOID, NV_UINT32);
171 static NV_SINT32 nv_oscanceltimer(PNV_VOID, PNV_VOID);
172
173 static NV_SINT32 nv_ospreprocpkt(PNV_VOID, PNV_VOID, PNV_VOID *, NV_UINT8 *, NV_UINT8);
174 static PNV_VOID  nv_ospreprocpktnopq(PNV_VOID, PNV_VOID);
175 static NV_SINT32 nv_osindicatepkt(PNV_VOID, PNV_VOID *, NV_UINT32);
176 static NV_SINT32 nv_oslockalloc(PNV_VOID, NV_SINT32, PNV_VOID *);
177 static NV_SINT32 nv_oslockacquire(PNV_VOID, NV_SINT32, PNV_VOID);
178 static NV_SINT32 nv_oslockrelease(PNV_VOID, NV_SINT32, PNV_VOID);
179 static PNV_VOID  nv_osreturnbufvirt(PNV_VOID, PNV_VOID);
180
181 static device_method_t nv_methods[] = {
182         /* Device interface */
183         DEVMETHOD(device_probe, nv_probe),
184         DEVMETHOD(device_attach, nv_attach),
185         DEVMETHOD(device_detach, nv_detach),
186         DEVMETHOD(device_shutdown, nv_shutdown),
187
188         /* Bus interface */
189         DEVMETHOD(bus_print_child, bus_generic_print_child),
190         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
191
192         /* MII interface */
193         DEVMETHOD(miibus_readreg, nv_miibus_readreg),
194         DEVMETHOD(miibus_writereg, nv_miibus_writereg),
195
196         {0, 0}
197 };
198
199 static driver_t nv_driver = {
200         "nv",
201         nv_methods,
202         sizeof(struct nv_softc)
203 };
204
205 static devclass_t nv_devclass;
206
207 static int      nv_pollinterval = 0;
208 SYSCTL_INT(_hw, OID_AUTO, nv_pollinterval, CTLFLAG_RW,
209            &nv_pollinterval, 0, "delay between interface polls");
210
211 DRIVER_MODULE(nv, pci, nv_driver, nv_devclass, 0, 0);
212 DRIVER_MODULE(miibus, nv, miibus_driver, miibus_devclass, 0, 0);
213
214 static struct nv_type nv_devs[] = {
215         {NVIDIA_VENDORID, NFORCE_MCPNET1_DEVICEID,
216                 "NVIDIA nForce MCP Networking Adapter"},
217         {NVIDIA_VENDORID, NFORCE_MCPNET2_DEVICEID,
218                 "NVIDIA nForce MCP2 Networking Adapter"},
219         {NVIDIA_VENDORID, NFORCE_MCPNET3_DEVICEID,
220                 "NVIDIA nForce MCP3 Networking Adapter"},
221         {NVIDIA_VENDORID, NFORCE_MCPNET4_DEVICEID,
222                 "NVIDIA nForce MCP4 Networking Adapter"},
223         {NVIDIA_VENDORID, NFORCE_MCPNET5_DEVICEID,
224                 "NVIDIA nForce MCP5 Networking Adapter"},
225         {NVIDIA_VENDORID, NFORCE_MCPNET6_DEVICEID,
226                 "NVIDIA nForce MCP6 Networking Adapter"},
227         {NVIDIA_VENDORID, NFORCE_MCPNET7_DEVICEID,
228                 "NVIDIA nForce MCP7 Networking Adapter"},
229         {NVIDIA_VENDORID, NFORCE_MCPNET8_DEVICEID,
230                 "NVIDIA nForce MCP8 Networking Adapter"},
231         {NVIDIA_VENDORID, NFORCE_MCPNET9_DEVICEID,
232                 "NVIDIA nForce MCP9 Networking Adapter"},
233         {NVIDIA_VENDORID, NFORCE_MCPNET10_DEVICEID,
234                 "NVIDIA nForce MCP10 Networking Adapter"},
235         {NVIDIA_VENDORID, NFORCE_MCPNET11_DEVICEID,
236                 "NVIDIA nForce MCP11 Networking Adapter"},
237         {0, 0, NULL}
238 };
239
240 /* DMA MEM map callback function to get data segment physical address */
241 static void
242 nv_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nsegs, int error)
243 {
244         if (error)
245                 return;
246
247         KASSERT(nsegs == 1,
248                 ("Too many DMA segments returned when mapping DMA memory"));
249         *(bus_addr_t *)arg = segs->ds_addr;
250 }
251
252 /* DMA RX map callback function to get data segment physical address */
253 static void
254 nv_dmamap_rx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
255 {
256         if (error)
257                 return;
258         *(bus_addr_t *)arg = segs->ds_addr;
259 }
260
261 /*
262  * DMA TX buffer callback function to allocate fragment data segment
263  * addresses
264  */
265 static void
266 nv_dmamap_tx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
267 {
268         struct nv_tx_desc *info = arg;
269
270         if (error)
271                 return;
272         KASSERT(nsegs < NV_MAX_FRAGS,
273                 ("Too many DMA segments returned when mapping mbuf"));
274         info->numfrags = nsegs;
275         bcopy(segs, info->frags, nsegs * sizeof(bus_dma_segment_t));
276 }
277
278 /* Probe for supported hardware ID's */
279 static int
280 nv_probe(device_t dev)
281 {
282         struct nv_type *t = nv_devs;
283
284         /* Check for matching PCI DEVICE ID's */
285         while (t->name != NULL) {
286                 if ((pci_get_vendor(dev) == t->vid_id) &&
287                     (pci_get_device(dev) == t->dev_id)) {
288                         device_set_desc(dev, t->name);
289                         return (0);
290                 }
291                 t++;
292         }
293
294         return (ENXIO);
295 }
296
297 /* Attach driver and initialise hardware for use */
298 static int
299 nv_attach(device_t dev)
300 {
301         u_char          eaddr[ETHER_ADDR_LEN];
302         struct nv_softc *sc;
303         struct ifnet   *ifp;
304         OS_API         *osapi;
305         ADAPTER_OPEN_PARAMS OpenParams;
306         int             error = 0, i, rid;
307         u_int32_t       unit;
308
309         DEBUGOUT(NV_DEBUG_INIT, "nv: nv_attach - entry\n");
310
311         sc = device_get_softc(dev);
312         unit = device_get_unit(dev);
313
314         sc->dev = dev;
315         sc->unit = unit;
316         lwkt_serialize_init(&sc->serializer);
317         callout_init(&sc->nv_stat_timer);
318
319         /* Preinitialize data structures */
320         bzero(&OpenParams, sizeof(ADAPTER_OPEN_PARAMS));
321
322         /* Enable bus mastering */
323         pci_enable_busmaster(dev);
324
325         /* Allocate memory mapped address space */
326         rid = NV_RID;
327         sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
328
329         if (sc->res == NULL) {
330                 device_printf(dev, "couldn't map memory\n");
331                 error = ENXIO;
332                 goto fail;
333         }
334         sc->sc_st = rman_get_bustag(sc->res);
335         sc->sc_sh = rman_get_bushandle(sc->res);
336
337         /* Allocate interrupt */
338         rid = 0;
339         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
340             RF_SHAREABLE | RF_ACTIVE);
341
342         if (sc->irq == NULL) {
343                 device_printf(dev, "couldn't map interrupt\n");
344                 error = ENXIO;
345                 goto fail;
346         }
347         /* Allocate DMA tags */
348         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
349                      BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * NV_MAX_FRAGS,
350                                    NV_MAX_FRAGS, MCLBYTES, 0,
351                                    &sc->mtag);
352         if (error) {
353                 device_printf(dev, "couldn't allocate dma tag\n");
354                 goto fail;
355         }
356         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
357                                    BUS_SPACE_MAXADDR, NULL, NULL,
358                                 sizeof(struct nv_rx_desc) * RX_RING_SIZE, 1,
359                                 sizeof(struct nv_rx_desc) * RX_RING_SIZE, 0,
360                                    &sc->rtag);
361         if (error) {
362                 device_printf(dev, "couldn't allocate dma tag\n");
363                 goto fail;
364         }
365         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
366                                    BUS_SPACE_MAXADDR, NULL, NULL,
367                                 sizeof(struct nv_tx_desc) * TX_RING_SIZE, 1,
368                                 sizeof(struct nv_tx_desc) * TX_RING_SIZE, 0,
369                                    &sc->ttag);
370         if (error) {
371                 device_printf(dev, "couldn't allocate dma tag\n");
372                 goto fail;
373         }
374
375         error = bus_dmamap_create(sc->ttag, 0, &sc->tmap);
376         if (error) {
377                 device_printf(dev, "couldn't create dma map\n");
378                 goto fail;
379         }
380
381         /* Allocate DMA safe memory and get the DMA addresses. */
382         error = bus_dmamem_alloc(sc->ttag, (void **)&sc->tx_desc,
383                                  BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tmap);
384         if (error) {
385                 device_printf(dev, "couldn't allocate dma memory\n");
386                 goto fail;
387         }
388         error = bus_dmamap_load(sc->ttag, sc->tmap, sc->tx_desc,
389                      sizeof(struct nv_tx_desc) * TX_RING_SIZE, nv_dmamap_cb,
390                                 &sc->tx_addr, 0);
391         if (error) {
392                 device_printf(dev, "couldn't map dma memory\n");
393                 goto fail;
394         }
395
396         error = bus_dmamap_create(sc->rtag, 0, &sc->rmap);
397         if (error) {
398                 device_printf(dev, "couldn't create dma map\n");
399                 goto fail;
400         }
401
402         error = bus_dmamem_alloc(sc->rtag, (void **)&sc->rx_desc,
403                                  BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rmap);
404         if (error) {
405                 device_printf(dev, "couldn't allocate dma memory\n");
406                 goto fail;
407         }
408         error = bus_dmamap_load(sc->rtag, sc->rmap, sc->rx_desc,
409                      sizeof(struct nv_rx_desc) * RX_RING_SIZE, nv_dmamap_cb,
410                                 &sc->rx_addr, 0);
411         if (error) {
412                 device_printf(dev, "couldn't map dma memory\n");
413                 goto fail;
414         }
415         /* Initialize rings. */
416         if (nv_init_rings(sc)) {
417                 device_printf(dev, "failed to init rings\n");
418                 error = ENXIO;
419                 goto fail;
420         }
421         /* Setup NVIDIA API callback routines */
422         osapi = &sc->osapi;
423         osapi->pOSCX = sc;
424         osapi->pfnAllocMemory = nv_osalloc;
425         osapi->pfnFreeMemory = nv_osfree;
426         osapi->pfnAllocMemoryEx = nv_osallocex;
427         osapi->pfnFreeMemoryEx = nv_osfreeex;
428         osapi->pfnClearMemory = nv_osclear;
429         osapi->pfnStallExecution = nv_osdelay;
430         osapi->pfnAllocReceiveBuffer = nv_osallocrxbuf;
431         osapi->pfnFreeReceiveBuffer = nv_osfreerxbuf;
432         osapi->pfnPacketWasSent = nv_ospackettx;
433         osapi->pfnPacketWasReceived = nv_ospacketrx;
434         osapi->pfnLinkStateHasChanged = nv_oslinkchg;
435         osapi->pfnAllocTimer = nv_osalloctimer;
436         osapi->pfnFreeTimer = nv_osfreetimer;
437         osapi->pfnInitializeTimer = nv_osinittimer;
438         osapi->pfnSetTimer = nv_ossettimer;
439         osapi->pfnCancelTimer = nv_oscanceltimer;
440         osapi->pfnPreprocessPacket = nv_ospreprocpkt;
441         osapi->pfnPreprocessPacketNopq = nv_ospreprocpktnopq;
442         osapi->pfnIndicatePackets = nv_osindicatepkt;
443         osapi->pfnLockAlloc = nv_oslockalloc;
444         osapi->pfnLockAcquire = nv_oslockacquire;
445         osapi->pfnLockRelease = nv_oslockrelease;
446         osapi->pfnReturnBufferVirtual = nv_osreturnbufvirt;
447
448         sc->linkup = FALSE;
449         sc->max_frame_size = ETHERMTU + ETHER_HDR_LEN + FCS_LEN;
450
451         /* TODO - We don't support hardware offload yet */
452         sc->hwmode = 1;
453         sc->media = 0;
454
455         /* Set NVIDIA API startup parameters */
456         OpenParams.MaxDpcLoop = 2;
457         OpenParams.MaxRxPkt = RX_RING_SIZE;
458         OpenParams.MaxTxPkt = TX_RING_SIZE;
459         OpenParams.SentPacketStatusSuccess = 1;
460         OpenParams.SentPacketStatusFailure = 0;
461         OpenParams.MaxRxPktToAccumulate = 6;
462         OpenParams.ulPollInterval = nv_pollinterval;
463         OpenParams.SetForcedModeEveryNthRxPacket = 0;
464         OpenParams.SetForcedModeEveryNthTxPacket = 0;
465         OpenParams.RxForcedInterrupt = 0;
466         OpenParams.TxForcedInterrupt = 0;
467         OpenParams.pOSApi = osapi;
468         OpenParams.pvHardwareBaseAddress = rman_get_virtual(sc->res);
469         OpenParams.bASFEnabled = 0;
470         OpenParams.ulDescriptorVersion = sc->hwmode;
471         OpenParams.ulMaxPacketSize = sc->max_frame_size;
472         OpenParams.DeviceId = pci_get_device(dev);
473
474         /* Open NVIDIA Hardware API */
475         error = ADAPTER_Open(&OpenParams, (void **)&(sc->hwapi), &sc->phyaddr);
476         if (error) {
477                 device_printf(dev, "failed to open NVIDIA Hardware API: 0x%x\n", error);
478                 goto fail;
479         }
480         
481         /* TODO - Add support for MODE2 hardware offload */ 
482
483         bzero(&sc->adapterdata, sizeof(sc->adapterdata));
484
485         sc->adapterdata.ulMediaIF = sc->media;
486         sc->adapterdata.ulModeRegTxReadCompleteEnable = 1;
487         sc->hwapi->pfnSetCommonData(sc->hwapi->pADCX, &sc->adapterdata);
488
489         sc->hwapi->pfnInit(sc->hwapi->pADCX, 
490                                    0, /* force speed */ 
491                                    0, /* force full duplex */
492                                    0, /* force mode */
493                                    0, /* force async mode */
494                                    &sc->linkup);
495
496         /* MAC is loaded backwards into h/w reg */
497         sc->hwapi->pfnGetNodeAddress(sc->hwapi->pADCX, sc->original_mac_addr);
498         for (i = 0; i < 6; i++) {
499                 eaddr[i] = sc->original_mac_addr[5 - i];
500         }
501         sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, eaddr);
502         bcopy(eaddr, (char *)&sc->sc_macaddr, ETHER_ADDR_LEN);
503
504         DEBUGOUT(NV_DEBUG_INIT, "nv: do mii_phy_probe\n");
505
506         /* Probe device for MII interface to PHY */
507         if (mii_phy_probe(dev, &sc->miibus, nv_ifmedia_upd, nv_ifmedia_sts)) {
508                 device_printf(dev, "MII without any phy!\n");
509                 error = ENXIO;
510                 goto fail;
511         }
512         /* Setup interface parameters */
513         ifp = &sc->sc_if;
514         ifp->if_softc = sc;
515         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
516         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
517         ifp->if_ioctl = nv_ioctl;
518         ifp->if_start = nv_ifstart;
519 #ifdef DEVICE_POLLING
520         ifp->if_poll = nv_poll;
521 #endif
522         ifp->if_watchdog = nv_watchdog;
523         ifp->if_timer = 0;
524         ifp->if_init = nv_init;
525         ifp->if_mtu = ETHERMTU;
526         ifp->if_baudrate = IF_Mbps(100);
527         ifp->if_capabilities |= IFCAP_VLAN_MTU;
528         ifq_set_maxlen(&ifp->if_snd, TX_RING_SIZE - 1);
529         ifq_set_ready(&ifp->if_snd);
530
531         /* Attach to OS's managers. */
532         ether_ifattach(ifp, sc->sc_macaddr);
533
534         /* Activate our interrupt handler. - attach last to avoid lock */
535         error = bus_setup_intr(sc->dev, sc->irq, 0,
536                                nv_intr, sc, &sc->sc_ih, &sc->serializer);
537         if (error) {
538                 ether_ifdetach(ifp);
539                 device_printf(sc->dev, "couldn't set up interrupt handler\n");
540                 goto fail;
541         }
542         DEBUGOUT(NV_DEBUG_INIT, "nv: nv_attach - exit\n");
543
544 fail:
545         if (error)
546                 nv_detach(dev);
547
548         return (error);
549 }
550
551 /* Detach interface for module unload */
552 static int
553 nv_detach(device_t dev)
554 {
555         struct nv_softc *sc = device_get_softc(dev);
556         struct ifnet   *ifp;
557
558         lwkt_serialize_enter(&sc->serializer);
559
560         DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_detach - entry\n");
561
562         ifp = &sc->arpcom.ac_if;
563
564         if (device_is_attached(dev)) {
565                 nv_stop_serialized(sc);
566                 lwkt_serialize_exit(&sc->serializer);
567                 ether_ifdetach(ifp);
568                 lwkt_serialize_enter(&sc->serializer);
569         }
570
571         if (sc->miibus)
572                 device_delete_child(dev, sc->miibus);
573         bus_generic_detach(dev);
574
575         /* Reload unreversed address back into MAC in original state */
576         if (sc->original_mac_addr)
577                 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->original_mac_addr);
578
579         DEBUGOUT(NV_DEBUG_DEINIT, "nv: do pfnClose\n");
580         /* Detach from NVIDIA hardware API */
581         if (sc->hwapi->pfnClose)
582                 sc->hwapi->pfnClose(sc->hwapi->pADCX, FALSE);
583         /* Release resources */
584         if (sc->sc_ih)
585                 bus_teardown_intr(sc->dev, sc->irq, sc->sc_ih);
586         if (sc->irq)
587                 bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
588         if (sc->res)
589                 bus_release_resource(sc->dev, SYS_RES_MEMORY, NV_RID, sc->res);
590
591         nv_free_rings(sc);
592
593         if (sc->tx_desc) {
594                 bus_dmamap_unload(sc->rtag, sc->rmap);
595                 bus_dmamem_free(sc->rtag, sc->rx_desc, sc->rmap);
596                 bus_dmamap_destroy(sc->rtag, sc->rmap);
597         }
598         if (sc->mtag)
599                 bus_dma_tag_destroy(sc->mtag);
600         if (sc->ttag)
601                 bus_dma_tag_destroy(sc->ttag);
602         if (sc->rtag)
603                 bus_dma_tag_destroy(sc->rtag);
604
605         lwkt_serialize_exit(&sc->serializer);
606
607         DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_detach - exit\n");
608
609         return (0);
610 }
611
612 static void
613 nv_init(void *xsc)
614 {
615         struct nv_softc *sc = xsc;
616
617         lwkt_serialize_enter(&sc->serializer);
618         nv_init_serialized(sc);
619         lwkt_serialize_exit(&sc->serializer);
620 }
621
622 /* Initialise interface and start it "RUNNING" */
623 static void
624 nv_init_serialized(struct nv_softc *sc)
625 {
626         struct ifnet   *ifp;
627         int             error;
628
629         DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init - entry (%d)\n", sc->linkup);
630
631         ifp = &sc->sc_if;
632
633         /* Do nothing if already running */
634         if (ifp->if_flags & IFF_RUNNING)
635                 return;
636
637         nv_stop_serialized(sc);
638
639         DEBUGOUT(NV_DEBUG_INIT, "nv: do pfnInit\n");
640         /* Setup Hardware interface and allocate memory structures */
641         error = sc->hwapi->pfnInit(sc->hwapi->pADCX, 
642                                    0, /* force speed */ 
643                                    0, /* force full duplex */
644                                    0, /* force mode */
645                                    0, /* force async mode */
646                                    &sc->linkup);
647
648         if (error) {
649                 device_printf(sc->dev, "failed to start NVIDIA Hardware interface\n");
650                 return;
651         }
652         /* Set the MAC address */
653         sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->sc_macaddr);
654
655         sc->hwapi->pfnStart(sc->hwapi->pADCX);
656
657         /* Setup multicast filter */
658         nv_setmulti(sc);
659         nv_ifmedia_upd(ifp);
660
661         /* Update interface parameters */
662         ifp->if_flags |= IFF_RUNNING;
663         ifp->if_flags &= ~IFF_OACTIVE;
664
665         /*
666          * Enable the interrupt.  Currently the nvidia API does not support
667          * polling, if we do not call pfnEnableInterrupts() we cannot
668          * issue the nvidia callback to process interrupts.  Call the
669          * interrupt service routine in case the interrupt got stuck during
670          * a reset, renegotiation, or timeout.
671          */
672 #if 1
673         lwkt_serialize_handler_enable(&sc->serializer);
674         sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
675 #else
676         if ((ifp->if_flags & IFF_POLLING) == 0) {
677                 lwkt_serialize_handler_enable(&sc->serializer);
678                 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
679         }
680 #endif
681         nv_intr(sc);
682
683         /* 
684          * Reset watchdog and ring queue indexes.  XXX if the interface
685          * is reset with pending tx packets queued to the actual device,
686          * the mbufs are currently lost.
687          */
688         ifp->if_timer = 0;
689         sc->pending_txs = 0;
690
691         callout_reset(&sc->nv_stat_timer, hz, nv_tick, sc);
692
693         DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init - exit\n");
694 }
695
696 #ifdef DEVICE_POLLING
697
698 static void
699 nv_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
700 {
701         struct nv_softc *sc = ifp->if_softc;
702
703         lwkt_serialize_enter(&sc->serializer);
704
705         switch(cmd) {
706         case POLL_REGISTER:
707                 /*
708                  * We must disable the hardware interrupt on the device
709                  * as well as ensure that any interrupt queued prior to
710                  * this point does not execute the handler function.
711                  *
712                  * NOTE!  The nvidia API does not support polling with
713                  * interrupts disabled, so we have to leave them turned on
714                  * unfortunately.
715                  */
716 #if 0
717                 sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
718                 lwkt_serialize_handler_disable(&sc->serializer);
719 #endif
720                 break;
721         case POLL_DEREGISTER:
722 #if 0
723                 lwkt_serialize_handler_enable(&sc->serializer);
724                 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
725 #endif
726                 break;
727         case POLL_AND_CHECK_STATUS:
728                 /* fall through */
729         case POLL_ONLY:
730                 if (ifp->if_flags & IFF_RUNNING) {
731                         nv_intr(sc);
732                 }
733                 if (ifp->if_flags & IFF_RUNNING) {
734                         if (!ifq_is_empty(&ifp->if_snd))
735                                 nv_ifstart_serialized(ifp);
736                 }
737                 break;
738         }
739         lwkt_serialize_exit(&sc->serializer);
740 }
741
742 #endif
743
744 static void
745 nv_stop(struct nv_softc *sc)
746 {
747         lwkt_serialize_enter(&sc->serializer);
748         nv_stop_serialized(sc);
749         lwkt_serialize_exit(&sc->serializer);
750 }
751
752 /* Stop interface activity ie. not "RUNNING" */
753 static void
754 nv_stop_serialized(struct nv_softc *sc)
755 {
756         struct ifnet   *ifp;
757
758         DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_stop - entry\n");
759
760         ifp = &sc->sc_if;
761         ifp->if_timer = 0;
762
763         /* Cancel tick timer */
764         callout_stop(&sc->nv_stat_timer);
765
766         /*
767          * Stop hardware activity.  The serializer handler disablement call
768          * prevents any interrupt scheduled prior to this call from calling
769          * the handler.
770          */
771         sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
772         lwkt_serialize_handler_disable(&sc->serializer);
773
774         sc->hwapi->pfnStop(sc->hwapi->pADCX, 0);
775         sc->hwapi->pfnClearTxDesc(sc->hwapi->pADCX);
776
777         DEBUGOUT(NV_DEBUG_DEINIT, "nv: do pfnDeinit\n");
778         /* Shutdown interface and deallocate memory buffers */
779         if (sc->hwapi->pfnDeinit)
780                 sc->hwapi->pfnDeinit(sc->hwapi->pADCX, 0);
781
782         sc->linkup = 0;
783         sc->cur_rx = 0;
784         sc->pending_rxs = 0;
785
786         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
787
788         DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_stop - exit\n");
789 }
790
791 /* Shutdown interface for unload/reboot */
792 static void
793 nv_shutdown(device_t dev)
794 {
795         struct nv_softc *sc;
796
797         DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_shutdown\n");
798
799         sc = device_get_softc(dev);
800
801         /* Stop hardware activity */
802         nv_stop(sc);
803 }
804
805 /* Allocate TX ring buffers */
806 static int
807 nv_init_rings(struct nv_softc *sc)
808 {
809         int             error, i;
810
811         DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init_rings - entry\n");
812
813         sc->cur_rx = sc->cur_tx = sc->pending_rxs = sc->pending_txs = 0;
814         /* Initialise RX ring */
815         for (i = 0; i < RX_RING_SIZE; i++) {
816                 struct nv_rx_desc *desc = sc->rx_desc + i;
817                 struct nv_map_buffer *buf = &desc->buf;
818
819                 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
820                 if (buf->mbuf == NULL) {
821                         device_printf(sc->dev, "couldn't allocate mbuf\n");
822                         nv_free_rings(sc);
823                         error = ENOBUFS;
824                         goto fail;
825                 }
826                 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
827                 m_adj(buf->mbuf, ETHER_ALIGN);
828
829                 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
830                 if (error) {
831                         device_printf(sc->dev, "couldn't create dma map\n");
832                         nv_free_rings(sc);
833                         goto fail;
834                 }
835                 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
836                                           nv_dmamap_rx_cb, &desc->paddr, 0);
837                 if (error) {
838                         device_printf(sc->dev, "couldn't dma map mbuf\n");
839                         nv_free_rings(sc);
840                         goto fail;
841                 }
842                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
843
844                 desc->buflength = buf->mbuf->m_len;
845                 desc->vaddr = mtod(buf->mbuf, caddr_t);
846         }
847         bus_dmamap_sync(sc->rtag, sc->rmap,
848                         BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
849
850         /* Initialize TX ring */
851         for (i = 0; i < TX_RING_SIZE; i++) {
852                 struct nv_tx_desc *desc = sc->tx_desc + i;
853                 struct nv_map_buffer *buf = &desc->buf;
854
855                 buf->mbuf = NULL;
856
857                 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
858                 if (error) {
859                         device_printf(sc->dev, "couldn't create dma map\n");
860                         nv_free_rings(sc);
861                         goto fail;
862                 }
863         }
864         bus_dmamap_sync(sc->ttag, sc->tmap,
865                         BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
866
867         DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init_rings - exit\n");
868
869 fail:
870         return (error);
871 }
872
873 /* Free the TX ring buffers */
874 static void
875 nv_free_rings(struct nv_softc *sc)
876 {
877         int             i;
878
879         DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_free_rings - entry\n");
880
881         for (i = 0; i < RX_RING_SIZE; i++) {
882                 struct nv_rx_desc *desc = sc->rx_desc + i;
883                 struct nv_map_buffer *buf = &desc->buf;
884
885                 if (buf->mbuf) {
886                         bus_dmamap_unload(sc->mtag, buf->map);
887                         bus_dmamap_destroy(sc->mtag, buf->map);
888                         m_freem(buf->mbuf);
889                 }
890                 buf->mbuf = NULL;
891         }
892
893         for (i = 0; i < TX_RING_SIZE; i++) {
894                 struct nv_tx_desc *desc = sc->tx_desc + i;
895                 struct nv_map_buffer *buf = &desc->buf;
896
897                 if (buf->mbuf) {
898                         bus_dmamap_unload(sc->mtag, buf->map);
899                         bus_dmamap_destroy(sc->mtag, buf->map);
900                         m_freem(buf->mbuf);
901                 }
902                 buf->mbuf = NULL;
903         }
904
905         DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_free_rings - exit\n");
906 }
907
908 /* Main loop for sending packets from OS to interface */
909
910 static void
911 nv_ifstart(struct ifnet *ifp)
912 {
913         struct nv_softc *sc = ifp->if_softc;
914
915         lwkt_serialize_enter(&sc->serializer);
916         nv_ifstart_serialized(ifp);
917         lwkt_serialize_exit(&sc->serializer);
918 }
919
920 static void
921 nv_ifstart_serialized(struct ifnet *ifp)
922 {
923         struct nv_softc *sc = ifp->if_softc;
924         struct nv_map_buffer *buf;
925         struct mbuf    *m0, *m;
926         struct nv_tx_desc *desc;
927         ADAPTER_WRITE_DATA txdata;
928         int             error, i;
929
930         DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_ifstart - entry\n");
931
932         /* If link is down/busy or queue is empty do nothing */
933         if ((ifp->if_flags & IFF_OACTIVE) || ifq_is_empty(&ifp->if_snd))
934                 return;
935
936         /* Transmit queued packets until sent or TX ring is full */
937         while (sc->pending_txs < TX_RING_SIZE) {
938                 desc = sc->tx_desc + sc->cur_tx;
939                 buf = &desc->buf;
940
941                 /* Get next packet to send. */
942                 m0 = ifq_dequeue(&ifp->if_snd, NULL);
943
944                 /* If nothing to send, return. */
945                 if (m0 == NULL)
946                         return;
947
948                 /* Map MBUF for DMA access */
949                 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m0,
950                                      nv_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
951
952                 if (error && error != EFBIG) {
953                         m_freem(m0);
954                         sc->tx_errors++;
955                         continue;
956                 }
957                 /*
958                  * Packet has too many fragments - defrag into new mbuf
959                  * cluster
960                  */
961                 if (error) {
962                         m = m_defrag(m0, MB_DONTWAIT);
963                         if (m == NULL) {
964                                 m_freem(m0);
965                                 sc->tx_errors++;
966                                 continue;
967                         }
968                         m0 = m;
969
970                         error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m,
971                                      nv_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
972                         if (error) {
973                                 m_freem(m);
974                                 sc->tx_errors++;
975                                 continue;
976                         }
977                 }
978                 /* Do sync on DMA bounce buffer */
979                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREWRITE);
980
981                 buf->mbuf = m0;
982                 txdata.ulNumberOfElements = desc->numfrags;
983                 txdata.pvID = (PVOID)desc;
984
985                 /* Put fragments into API element list */
986                 txdata.ulTotalLength = buf->mbuf->m_len;
987                 for (i = 0; i < desc->numfrags; i++) {
988                         txdata.sElement[i].ulLength = (ulong)desc->frags[i].ds_len;
989                         txdata.sElement[i].pPhysical = (PVOID)desc->frags[i].ds_addr;
990                 }
991
992                 /* Send packet to Nvidia API for transmission */
993                 error = sc->hwapi->pfnWrite(sc->hwapi->pADCX, &txdata);
994
995                 switch (error) {
996                 case ADAPTERERR_NONE:
997                         /* Packet was queued in API TX queue successfully */
998                         sc->pending_txs++;
999                         sc->cur_tx = (sc->cur_tx + 1) % TX_RING_SIZE;
1000                         break;
1001
1002                 case ADAPTERERR_TRANSMIT_QUEUE_FULL:
1003                         /* The API TX queue is full - requeue the packet */
1004                         device_printf(sc->dev, "nv_ifstart: transmit queue is full\n");
1005                         ifp->if_flags |= IFF_OACTIVE;
1006                         bus_dmamap_unload(sc->mtag, buf->map);
1007                         buf->mbuf = NULL;
1008                         m_freem(m0);    /* XXX requeue */
1009                         return;
1010
1011                 default:
1012                         /* The API failed to queue/send the packet so dump it */
1013                         device_printf(sc->dev, "nv_ifstart: transmit error\n");
1014                         bus_dmamap_unload(sc->mtag, buf->map);
1015                         m_freem(buf->mbuf);
1016                         buf->mbuf = NULL;
1017                         sc->tx_errors++;
1018                         return;
1019                 }
1020                 /* Set watchdog timer. */
1021                 ifp->if_timer = 8;
1022
1023                 /* Copy packet to BPF tap */
1024                 BPF_MTAP(ifp, m0);
1025         }
1026         ifp->if_flags |= IFF_OACTIVE;
1027
1028         DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_ifstart - exit\n");
1029 }
1030
1031 /* Handle IOCTL events */
1032 static int
1033 nv_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1034 {
1035         struct nv_softc *sc = ifp->if_softc;
1036         struct ifreq   *ifr = (struct ifreq *) data;
1037         struct mii_data *mii;
1038         int             error = 0;
1039
1040         lwkt_serialize_enter(&sc->serializer);
1041
1042         DEBUGOUT(NV_DEBUG_IOCTL, "nv: nv_ioctl - entry\n");
1043
1044         switch (command) {
1045         case SIOCSIFMTU:
1046                 /* Set MTU size */
1047                 if (ifp->if_mtu == ifr->ifr_mtu)
1048                         break;
1049                 if (ifr->ifr_mtu + ifp->if_hdrlen <= MAX_PACKET_SIZE_1518) {
1050                         ifp->if_mtu = ifr->ifr_mtu;
1051                         nv_stop_serialized(sc);
1052                         nv_init_serialized(sc);
1053                 } else
1054                         error = EINVAL;
1055                 break;
1056
1057         case SIOCSIFFLAGS:
1058                 /* Setup interface flags */
1059                 if (ifp->if_flags & IFF_UP) {
1060                         if ((ifp->if_flags & IFF_RUNNING) == 0) {
1061                                 nv_init_serialized(sc);
1062                                 break;
1063                         }
1064                 } else {
1065                         if (ifp->if_flags & IFF_RUNNING) {
1066                                 nv_stop_serialized(sc);
1067                                 break;
1068                         }
1069                 }
1070
1071                 /* Handle IFF_PROMISC and IFF_ALLMULTI flags. */
1072                 nv_setmulti(sc);
1073                 break;
1074
1075         case SIOCADDMULTI:
1076         case SIOCDELMULTI:
1077                 /* Setup multicast filter */
1078                 if (ifp->if_flags & IFF_RUNNING) {
1079                         nv_setmulti(sc);
1080                 }
1081                 break;
1082         case SIOCGIFMEDIA:
1083         case SIOCSIFMEDIA:
1084                 /* Get/Set interface media parameters */
1085                 mii = device_get_softc(sc->miibus);
1086                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1087                 break;
1088
1089         default:
1090                 /* Everything else we forward to generic ether ioctl */
1091                 lwkt_serialize_exit(&sc->serializer);
1092                 error = ether_ioctl(ifp, command, data);
1093                 lwkt_serialize_enter(&sc->serializer);
1094                 break;
1095         }
1096
1097         DEBUGOUT(NV_DEBUG_IOCTL, "nv: nv_ioctl - exit\n");
1098
1099         lwkt_serialize_exit(&sc->serializer);
1100
1101         return (error);
1102 }
1103
1104 /*
1105  * Interrupt service routine.  The serializer has already been entered
1106  * since we installed it in our bus_setup_intr() call.
1107  */
1108 static void
1109 nv_intr(void *arg)
1110 {
1111         struct nv_softc *sc = arg;
1112         struct ifnet   *ifp = &sc->sc_if;
1113
1114         DEBUGOUT(NV_DEBUG_INTERRUPT, "nv: nv_intr - entry\n");
1115
1116         /*
1117          * Handle an interrupt event.  Unfortunately the nvidia API
1118          * does not support interrupt disablement when polling, so we
1119          * have to re-enable after the query masks them off.
1120          */
1121         if (sc->hwapi->pfnQueryInterrupt(sc->hwapi->pADCX)) {
1122                 sc->hwapi->pfnHandleInterrupt(sc->hwapi->pADCX);
1123 #if 1
1124                 lwkt_serialize_handler_enable(&sc->serializer);
1125                 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1126 #else
1127                 if ((ifp->if_flags & IFF_POLLING) == 0) {
1128                         lwkt_serialize_handler_enable(&sc->serializer);
1129                         sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1130                 }
1131 #endif
1132         }
1133         if (!ifq_is_empty(&ifp->if_snd))
1134                 nv_ifstart_serialized(ifp);
1135
1136         /* If no pending packets we don't need a timeout */
1137         if (sc->pending_txs == 0)
1138                 sc->sc_if.if_timer = 0;
1139
1140         DEBUGOUT(NV_DEBUG_INTERRUPT, "nv: nv_intr - exit\n");
1141 }
1142
1143 /*
1144  * Setup multicast filters 
1145  *
1146  * Serialized on call
1147  */
1148 static void
1149 nv_setmulti(struct nv_softc *sc)
1150 {
1151         struct ifnet   *ifp;
1152         struct ifmultiaddr *ifma;
1153         PACKET_FILTER   hwfilter;
1154         int             i;
1155         u_int8_t        oraddr[6];
1156         u_int8_t        andaddr[6];
1157
1158         DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_setmulti - entry\n");
1159
1160         ifp = &sc->sc_if;
1161
1162         /* Initialize filter */
1163         hwfilter.ulFilterFlags = 0;
1164         for (i = 0; i < 6; i++) {
1165                 hwfilter.acMulticastAddress[i] = 0;
1166                 hwfilter.acMulticastMask[i] = 0;
1167         }
1168
1169         if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1170                 /* Accept all packets */
1171                 hwfilter.ulFilterFlags |= ACCEPT_ALL_PACKETS;
1172                 sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1173                 return;
1174         }
1175         /* Setup multicast filter */
1176         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1177                 u_char         *addrp;
1178
1179                 if (ifma->ifma_addr->sa_family != AF_LINK)
1180                         continue;
1181
1182                 addrp = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
1183                 for (i = 0; i < 6; i++) {
1184                         u_int8_t        mcaddr = addrp[i];
1185                         andaddr[i] &= mcaddr;
1186                         oraddr[i] |= mcaddr;
1187                 }
1188         }
1189         for (i = 0; i < 6; i++) {
1190                 hwfilter.acMulticastAddress[i] = andaddr[i] & oraddr[i];
1191                 hwfilter.acMulticastMask[i] = andaddr[i] | (~oraddr[i]);
1192         }
1193
1194         /* Send filter to NVIDIA API */
1195         sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1196
1197         DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_setmulti - exit\n");
1198 }
1199
1200 /*
1201  * Change the current media/mediaopts
1202  *
1203  * Serialized on call
1204  */
1205 static int
1206 nv_ifmedia_upd(struct ifnet *ifp)
1207 {
1208         struct nv_softc *sc = ifp->if_softc;
1209         struct mii_data *mii;
1210
1211         DEBUGOUT(NV_DEBUG_MII, "nv: nv_ifmedia_upd\n");
1212
1213         mii = device_get_softc(sc->miibus);
1214
1215         if (mii->mii_instance) {
1216                 struct mii_softc *miisc;
1217                 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1218                      miisc = LIST_NEXT(miisc, mii_list)) {
1219                         mii_phy_reset(miisc);
1220                 }
1221         }
1222         mii_mediachg(mii);
1223
1224         return (0);
1225 }
1226
1227 /*
1228  * Update current miibus PHY status of media 
1229  *
1230  * Serialized on call
1231  */
1232 static void
1233 nv_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1234 {
1235         struct nv_softc *sc;
1236         struct mii_data *mii;
1237
1238         DEBUGOUT(NV_DEBUG_MII, "nv: nv_ifmedia_sts\n");
1239
1240         sc = ifp->if_softc;
1241         mii = device_get_softc(sc->miibus);
1242         mii_pollstat(mii);
1243
1244         ifmr->ifm_active = mii->mii_media_active;
1245         ifmr->ifm_status = mii->mii_media_status;
1246 }
1247
1248 /* miibus tick timer - maintain link status */
1249 static void
1250 nv_tick(void *xsc)
1251 {
1252         struct nv_softc *sc = xsc;
1253         struct mii_data *mii;
1254         struct ifnet   *ifp;
1255
1256         lwkt_serialize_enter(&sc->serializer);
1257
1258         ifp = &sc->sc_if;
1259         nv_update_stats(sc);
1260
1261         mii = device_get_softc(sc->miibus);
1262         mii_tick(mii);
1263
1264         if ((mii->mii_media_status & IFM_ACTIVE) &&
1265             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1266                 if (!ifq_is_empty(&ifp->if_snd))
1267                         nv_ifstart_serialized(ifp);
1268         }
1269         callout_reset(&sc->nv_stat_timer, hz, nv_tick, sc);
1270         lwkt_serialize_exit(&sc->serializer);
1271 }
1272
1273 /* Update ifnet data structure with collected interface stats from API */
1274 static void
1275 nv_update_stats(struct nv_softc *sc)
1276 {
1277         struct ifnet   *ifp = &sc->sc_if;
1278         ADAPTER_STATS   stats;
1279
1280         if (sc->hwapi) {
1281                 sc->hwapi->pfnGetStatistics(sc->hwapi->pADCX, &stats);
1282
1283                 ifp->if_ipackets = stats.ulSuccessfulReceptions;
1284                 ifp->if_ierrors = stats.ulMissedFrames +
1285                         stats.ulFailedReceptions +
1286                         stats.ulCRCErrors +
1287                         stats.ulFramingErrors +
1288                         stats.ulOverFlowErrors;
1289
1290                 ifp->if_opackets = stats.ulSuccessfulTransmissions;
1291                 ifp->if_oerrors = sc->tx_errors +
1292                         stats.ulFailedTransmissions +
1293                         stats.ulRetryErrors +
1294                         stats.ulUnderflowErrors +
1295                         stats.ulLossOfCarrierErrors +
1296                         stats.ulLateCollisionErrors;
1297
1298                 ifp->if_collisions = stats.ulLateCollisionErrors;
1299         }
1300 }
1301
1302 /* miibus Read PHY register wrapper - calls Nvidia API entry point */
1303 static int
1304 nv_miibus_readreg(device_t dev, int phy, int reg)
1305 {
1306         struct nv_softc *sc = device_get_softc(dev);
1307         ULONG           data;
1308
1309         DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_readreg - entry\n");
1310
1311         ADAPTER_ReadPhy(sc->hwapi->pADCX, phy, reg, &data);
1312
1313         DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_readreg - exit\n");
1314
1315         return (data);
1316 }
1317
1318 /* miibus Write PHY register wrapper - calls Nvidia API entry point */
1319 static void
1320 nv_miibus_writereg(device_t dev, int phy, int reg, int data)
1321 {
1322         struct nv_softc *sc = device_get_softc(dev);
1323
1324         DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_writereg - entry\n");
1325
1326         ADAPTER_WritePhy(sc->hwapi->pADCX, phy, reg, (ulong)data);
1327
1328         DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_writereg - exit\n");
1329 }
1330
1331 /* Watchdog timer to prevent PHY lockups */
1332 static void
1333 nv_watchdog(struct ifnet *ifp)
1334 {
1335         struct nv_softc *sc = ifp->if_softc;
1336
1337         lwkt_serialize_enter(&sc->serializer);
1338
1339         device_printf(sc->dev, "device timeout (%d) flags %d\n",
1340                         sc->pending_txs, ifp->if_flags & IFF_OACTIVE);
1341
1342         sc->tx_errors++;
1343
1344         nv_stop_serialized(sc);
1345         ifp->if_flags &= ~IFF_RUNNING;
1346         nv_init_serialized(sc);
1347
1348         if (!ifq_is_empty(&ifp->if_snd))
1349                 nv_ifstart_serialized(ifp);
1350
1351         lwkt_serialize_exit(&sc->serializer);
1352 }
1353
1354 /* --- Start of NVOSAPI interface --- */
1355
1356 /* Allocate DMA enabled general use memory for API */
1357 static NV_SINT32
1358 nv_osalloc(PNV_VOID ctx, PMEMORY_BLOCK mem)
1359 {
1360         struct nv_softc *sc;
1361         bus_addr_t      mem_physical;
1362
1363         DEBUGOUT(NV_DEBUG_API, "nv: nv_osalloc - %d\n", mem->uiLength);
1364
1365         sc = (struct nv_softc *)ctx;
1366
1367         mem->pLogical = (PVOID)contigmalloc(mem->uiLength, M_DEVBUF,
1368                                     M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE, 0);
1369
1370         if (!mem->pLogical) {
1371                 device_printf(sc->dev, "memory allocation failed\n");
1372                 return (0);
1373         }
1374         memset(mem->pLogical, 0, (ulong)mem->uiLength);
1375         mem_physical = vtophys(mem->pLogical);
1376         mem->pPhysical = (PVOID)mem_physical;
1377
1378         DEBUGOUT(NV_DEBUG_API, "nv: nv_osalloc %p/%p - %d\n",
1379                  mem->pLogical, mem->pPhysical, mem->uiLength);
1380
1381         return (1);
1382 }
1383
1384 /* Free allocated memory */
1385 static NV_SINT32
1386 nv_osfree(PNV_VOID ctx, PMEMORY_BLOCK mem)
1387 {
1388         DEBUGOUT(NV_DEBUG_API, "nv: nv_osfree - %p - %d\n",
1389                  mem->pLogical, mem->uiLength);
1390
1391         contigfree(mem->pLogical, PAGE_SIZE, M_DEVBUF);
1392         return (1);
1393 }
1394
1395 /* Copied directly from nvnet.c */
1396 static NV_SINT32
1397 nv_osallocex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1398 {
1399         MEMORY_BLOCK    mem_block;
1400
1401         DEBUGOUT(NV_DEBUG_API, "nv: nv_osallocex\n");
1402
1403         mem_block_ex->pLogical = NULL;
1404         mem_block_ex->uiLengthOrig = mem_block_ex->uiLength;
1405
1406         if ((mem_block_ex->AllocFlags & ALLOC_MEMORY_ALIGNED) &&
1407             (mem_block_ex->AlignmentSize > 1)) {
1408                 DEBUGOUT(NV_DEBUG_API, "     aligning on %d\n",
1409                          mem_block_ex->AlignmentSize);
1410                 mem_block_ex->uiLengthOrig += mem_block_ex->AlignmentSize;
1411         }
1412         mem_block.uiLength = mem_block_ex->uiLengthOrig;
1413
1414         if (nv_osalloc(ctx, &mem_block) == 0) {
1415                 return (0);
1416         }
1417         mem_block_ex->pLogicalOrig = mem_block.pLogical;
1418         mem_block_ex->pPhysicalOrigLow = (uintptr_t)mem_block.pPhysical;
1419         mem_block_ex->pPhysicalOrigHigh = 0;
1420
1421         mem_block_ex->pPhysical = mem_block.pPhysical;
1422         mem_block_ex->pLogical = mem_block.pLogical;
1423
1424         if (mem_block_ex->uiLength != mem_block_ex->uiLengthOrig) {
1425                 unsigned int    offset;
1426                 offset = mem_block_ex->pPhysicalOrigLow & (mem_block_ex->AlignmentSize - 1);
1427
1428                 if (offset) {
1429                         mem_block_ex->pPhysical = (PVOID)((uintptr_t)mem_block_ex->pPhysical +
1430                                       mem_block_ex->AlignmentSize - offset);
1431                         mem_block_ex->pLogical = (PVOID)((uintptr_t)mem_block_ex->pLogical +
1432                                       mem_block_ex->AlignmentSize - offset);
1433                 }               /* if (offset) */
1434         }                       /* if (mem_block_ex->uiLength !=
1435                                  * mem_block_ex->uiLengthOrig) */
1436         return (1);
1437 }
1438
1439 /* Copied directly from nvnet.c */
1440 static NV_SINT32
1441 nv_osfreeex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1442 {
1443         MEMORY_BLOCK    mem_block;
1444
1445         DEBUGOUT(NV_DEBUG_API, "nv: nv_osfreeex\n");
1446
1447         mem_block.pLogical = mem_block_ex->pLogicalOrig;
1448         mem_block.pPhysical = (PVOID)((uintptr_t)mem_block_ex->pPhysicalOrigLow);
1449         mem_block.uiLength = mem_block_ex->uiLengthOrig;
1450
1451         return (nv_osfree(ctx, &mem_block));
1452 }
1453
1454 /* Clear memory region */
1455 static NV_SINT32
1456 nv_osclear(PNV_VOID ctx, PNV_VOID mem, NV_SINT32 length)
1457 {
1458         DEBUGOUT(NV_DEBUG_API, "nv: nv_osclear\n");
1459         memset(mem, 0, length);
1460         return (1);
1461 }
1462
1463 /* Sleep for a tick */
1464 static NV_SINT32
1465 nv_osdelay(PNV_VOID ctx, NV_UINT32 usec)
1466 {
1467         if (usec >= 1000000 / hz) {
1468             tsleep(nv_osdelay, 0, "nvdelay", (usec * hz / 1000000) + 1);
1469         } else {
1470             DELAY(usec);
1471         }
1472         return (1);
1473 }
1474
1475 /* Allocate memory for rx buffer */
1476 static NV_SINT32
1477 nv_osallocrxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID *id)
1478 {
1479         struct nv_softc *sc = ctx;
1480         struct nv_rx_desc *desc;
1481         struct nv_map_buffer *buf;
1482         int             error;
1483
1484         DEBUGOUT(NV_DEBUG_API, "nv: nv_osallocrxbuf\n");
1485
1486         if (sc->pending_rxs == RX_RING_SIZE) {
1487                 device_printf(sc->dev, "rx ring buffer is full\n");
1488                 goto fail;
1489         }
1490         desc = sc->rx_desc + sc->cur_rx;
1491         buf = &desc->buf;
1492
1493         if (buf->mbuf == NULL) {
1494                 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1495                 if (buf->mbuf == NULL) {
1496                         device_printf(sc->dev, "failed to allocate memory\n");
1497                         goto fail;
1498                 }
1499                 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
1500                 m_adj(buf->mbuf, ETHER_ALIGN);
1501
1502                 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
1503                                           nv_dmamap_rx_cb, &desc->paddr, 0);
1504                 if (error) {
1505                         device_printf(sc->dev, "failed to dmamap mbuf\n");
1506                         m_freem(buf->mbuf);
1507                         buf->mbuf = NULL;
1508                         goto fail;
1509                 }
1510                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
1511                 desc->buflength = buf->mbuf->m_len;
1512                 desc->vaddr = mtod(buf->mbuf, PVOID);
1513         }
1514         sc->pending_rxs++;
1515         sc->cur_rx = (sc->cur_rx + 1) % RX_RING_SIZE;
1516
1517         mem->pLogical = (void *)desc->vaddr;
1518         mem->pPhysical = (void *)desc->paddr;
1519         mem->uiLength = desc->buflength;
1520         *id = (void *)desc;
1521
1522         return (1);
1523 fail:
1524         return (0);
1525 }
1526
1527
1528 /* Free the rx buffer */
1529 static NV_SINT32
1530 nv_osfreerxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID id)
1531 {
1532         struct nv_softc *sc = ctx;
1533         struct nv_rx_desc *desc;
1534         struct nv_map_buffer *buf;
1535
1536         DEBUGOUT(NV_DEBUG_API, "nv: nv_osfreerxbuf\n");
1537
1538         desc = (struct nv_rx_desc *) id;
1539         buf = &desc->buf;
1540
1541         if (buf->mbuf) {
1542                 bus_dmamap_unload(sc->mtag, buf->map);
1543                 bus_dmamap_destroy(sc->mtag, buf->map);
1544                 m_freem(buf->mbuf);
1545         }
1546         sc->pending_rxs--;
1547         buf->mbuf = NULL;
1548
1549         return (1);
1550 }
1551
1552 /* This gets called by the Nvidia API after our TX packet has been sent */
1553 static NV_SINT32
1554 nv_ospackettx(PNV_VOID ctx, PNV_VOID id, NV_UINT32 success)
1555 {
1556         struct nv_softc *sc = ctx;
1557         struct nv_map_buffer *buf;
1558         struct nv_tx_desc *desc = (struct nv_tx_desc *) id;
1559         struct ifnet   *ifp;
1560
1561         DEBUGOUT(NV_DEBUG_API, "nv: nv_ospackettx\n");
1562
1563         ifp = &sc->sc_if;
1564         buf = &desc->buf;
1565         sc->pending_txs--;
1566
1567         /* Unload and free mbuf cluster */
1568         if (buf->mbuf == NULL)
1569                 goto fail;
1570
1571         bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTWRITE);
1572         bus_dmamap_unload(sc->mtag, buf->map);
1573         m_freem(buf->mbuf);
1574         buf->mbuf = NULL;
1575
1576         /*
1577          * Make sure we are clear to go if we previously stalled due
1578          * to a full ring.
1579          */
1580         if (sc->pending_txs < TX_RING_SIZE) {
1581                 ifp->if_flags &= ~IFF_OACTIVE;
1582                 if (!ifq_is_empty(&ifp->if_snd))
1583                         nv_ifstart_serialized(ifp);
1584         }
1585 fail:
1586         return (1);
1587 }
1588
1589 /* This gets called by the Nvidia API when a new packet has been received */
1590 /* XXX What is newbuf used for? XXX */
1591 static NV_SINT32
1592 nv_ospacketrx(PNV_VOID ctx, PNV_VOID data, NV_UINT32 success,
1593               NV_UINT8 *newbuf, NV_UINT8 priority)
1594 {
1595         struct nv_softc *sc = ctx;
1596         struct ifnet   *ifp;
1597         struct nv_rx_desc *desc;
1598         struct nv_map_buffer *buf;
1599         ADAPTER_READ_DATA *readdata;
1600
1601         DEBUGOUT(NV_DEBUG_API, "nv: nv_ospacketrx\n");
1602
1603         ifp = &sc->sc_if;
1604
1605         readdata = (ADAPTER_READ_DATA *) data;
1606         desc = readdata->pvID;
1607         buf = &desc->buf;
1608         bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1609
1610         if (success) {
1611                 /* Sync DMA bounce buffer. */
1612                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1613
1614                 /* First mbuf in packet holds the ethernet and packet headers */
1615                 buf->mbuf->m_pkthdr.rcvif = ifp;
1616                 buf->mbuf->m_pkthdr.len = buf->mbuf->m_len = readdata->ulTotalLength;
1617
1618                 bus_dmamap_unload(sc->mtag, buf->map);
1619
1620                 /* Give mbuf to OS. */
1621                 (*ifp->if_input) (ifp, buf->mbuf);
1622                 if (readdata->ulFilterMatch & ADREADFL_MULTICAST_MATCH)
1623                         ifp->if_imcasts++;
1624
1625                 /* Blat the mbuf pointer, kernel will free the mbuf cluster */
1626                 buf->mbuf = NULL;
1627         } else {
1628                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1629                 bus_dmamap_unload(sc->mtag, buf->map);
1630                 m_freem(buf->mbuf);
1631                 buf->mbuf = NULL;
1632         }
1633
1634         sc->cur_rx = desc - sc->rx_desc;
1635         sc->pending_rxs--;
1636
1637         return (1);
1638 }
1639
1640 /* This gets called by NVIDIA API when the PHY link state changes */
1641 static NV_SINT32
1642 nv_oslinkchg(PNV_VOID ctx, NV_SINT32 enabled)
1643 {
1644         struct nv_softc *sc = (struct nv_softc *)ctx;
1645         struct ifnet   *ifp;
1646
1647         DEBUGOUT(NV_DEBUG_API, "nv: nv_oslinkchg\n");
1648
1649         ifp = &sc->sc_if;
1650
1651         if (enabled)
1652                 ifp->if_flags |= IFF_UP;
1653         else
1654                 ifp->if_flags &= ~IFF_UP;
1655
1656
1657         return (1);
1658 }
1659
1660
1661 /* Setup a watchdog timer */
1662 static NV_SINT32
1663 nv_osalloctimer(PNV_VOID ctx, PNV_VOID *timer)
1664 {
1665         struct nv_softc *sc = (struct nv_softc *)ctx;
1666
1667         DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osalloctimer\n");
1668
1669         callout_init(&sc->ostimer);
1670         *timer = &sc->ostimer;
1671
1672         return (1);
1673 }
1674
1675 /* Free the timer */
1676 static NV_SINT32
1677 nv_osfreetimer(PNV_VOID ctx, PNV_VOID timer)
1678 {
1679         DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osfreetimer\n");
1680
1681         return (1);
1682 }
1683
1684 /* Setup timer parameters */
1685 static NV_SINT32
1686 nv_osinittimer(PNV_VOID ctx, PNV_VOID timer, PTIMER_FUNC func, PNV_VOID parameters)
1687 {
1688         struct nv_softc *sc = (struct nv_softc *)ctx;
1689
1690         DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osinittimer\n");
1691
1692         sc->ostimer_func = func;
1693         sc->ostimer_params = parameters;
1694
1695         return (1);
1696 }
1697
1698 /* 
1699  * Set the timer to go off 
1700  *
1701  * XXX what the hell are the units for 'delay' ?  They sure aren't ticks!
1702  */
1703 static NV_SINT32
1704 nv_ossettimer(PNV_VOID ctx, PNV_VOID timer, NV_UINT32 delay)
1705 {
1706         struct nv_softc *sc = ctx;
1707
1708         DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ossettimer\n");
1709         printf("nv_ossettimer %d\n", (int)delay);
1710
1711         callout_reset(&sc->ostimer, delay, sc->ostimer_func,
1712                       sc->ostimer_params);
1713
1714         return (1);
1715 }
1716
1717 /* Cancel the timer */
1718 static NV_SINT32
1719 nv_oscanceltimer(PNV_VOID ctx, PNV_VOID timer)
1720 {
1721         struct nv_softc *sc = ctx;
1722
1723         DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_oscanceltimer\n");
1724
1725         callout_stop(&sc->ostimer);
1726
1727         return (1);
1728 }
1729
1730 static NV_SINT32
1731 nv_ospreprocpkt(PNV_VOID ctx, PNV_VOID readdata, PNV_VOID *id, NV_UINT8 *newbuffer,
1732                 NV_UINT8 priority)
1733 {
1734         /* Not implemented */
1735         DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ospreprocpkt\n");
1736
1737         return (1);
1738 }
1739
1740 static PNV_VOID
1741 nv_ospreprocpktnopq(PNV_VOID ctx, PNV_VOID readdata)
1742 {
1743         /* Not implemented */
1744         DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ospreprocpkt\n");
1745
1746         return (NULL);
1747 }
1748
1749 static NV_SINT32
1750 nv_osindicatepkt(PNV_VOID ctx, PNV_VOID *id, NV_UINT32 pktno)
1751 {
1752         /* Not implemented */
1753         DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osindicatepkt\n");
1754
1755         return (1);
1756 }
1757
1758 /* Allocate mutex context (already done in nv_attach) */
1759 static NV_SINT32
1760 nv_oslockalloc(PNV_VOID ctx, NV_SINT32 type, PNV_VOID *pLock)
1761 {
1762         struct nv_softc *sc = (struct nv_softc *)ctx;
1763
1764         DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockalloc\n");
1765
1766         *pLock = (void **)sc;
1767
1768         return (1);
1769 }
1770
1771 /* Obtain a spin lock */
1772 static NV_SINT32
1773 nv_oslockacquire(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1774 {
1775         DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockacquire\n");
1776
1777         NV_OSLOCK((struct nv_softc *)lock);
1778
1779         return (1);
1780 }
1781
1782 /* Release lock */
1783 static NV_SINT32
1784 nv_oslockrelease(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1785 {
1786         DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockrelease\n");
1787
1788         NV_OSUNLOCK((struct nv_softc *)lock);
1789
1790         return (1);
1791 }
1792
1793 /* I have no idea what this is for */
1794 static PNV_VOID
1795 nv_osreturnbufvirt(PNV_VOID ctx, PNV_VOID readdata)
1796 {
1797         /* Not implemented */
1798         DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_osreturnbufvirt\n");
1799         panic("nv: nv_osreturnbufvirtual not implemented\n");
1800
1801         return (NULL);
1802 }
1803
1804
1805 /* --- End on NVOSAPI interface --- */