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