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