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