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