2 * Copyright (c) 2003, 2004 by Quinton Dolan <q@onthenet.com.au>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $Id: if_nv.c,v 1.20 2005/03/12 01:11:00 q Exp $
27 * $DragonFly: src/sys/dev/netif/nv/Attic/if_nv.c,v 1.23 2005/11/28 17:13:43 dillon Exp $
31 * NVIDIA nForce MCP Networking Adapter driver
33 * This is a port of the NVIDIA MCP Linux ethernet driver distributed by NVIDIA
34 * through their web site.
36 * All mainstream nForce and nForce2 motherboards are supported. This module
37 * is as stable, sometimes more stable, than the linux version. (Recent
38 * Linux stability issues seem to be related to some issues with newer
39 * distributions using GCC 3.x, however this don't appear to effect FreeBSD
42 * In accordance with the NVIDIA distribution license it is necessary to
43 * link this module against the nvlibnet.o binary object included in the
44 * Linux driver source distribution. The binary component is not modified in
45 * any way and is simply linked against a FreeBSD equivalent of the nvnet.c
46 * linux kernel module "wrapper".
48 * The Linux driver uses a common code API that is shared between Win32 and
49 * i386 Linux. This abstracts the low level driver functions and uses
50 * callbacks and hooks to access the underlying hardware device. By using
51 * this same API in a FreeBSD kernel module it is possible to support the
52 * hardware without breaching the Linux source distributions licensing
53 * requirements, or obtaining the hardware programming specifications.
55 * Although not conventional, it works, and given the relatively small
56 * amount of hardware centric code, it's hopefully no more buggy than its
59 * NVIDIA now suppport the nForce3 AMD64 platform, however I have been
60 * unable to access such a system to verify support. However, the code is
61 * reported to work with little modification when compiled with the AMD64
62 * version of the NVIDIA Linux library. All that should be necessary to make
63 * the driver work is to link it directly into the kernel, instead of as a
64 * module, and apply the docs/amd64.diff patch in this source distribution to
65 * the NVIDIA Linux driver source.
67 * This driver should work on all versions of FreeBSD since 4.9/5.1 as well
68 * as recent versions of DragonFly.
70 * Written by Quinton Dolan <q@onthenet.com.au>
71 * Portions based on existing FreeBSD network drivers.
72 * NVIDIA API usage derived from distributed NVIDIA NVNET driver source files.
74 * $Id: if_nv.c,v 1.9 2003/12/13 15:27:40 q Exp $
77 #include "opt_polling.h"
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/sockio.h>
83 #include <sys/malloc.h>
84 #include <sys/kernel.h>
85 #include <sys/socket.h>
86 #include <sys/sysctl.h>
87 #include <sys/queue.h>
88 #include <sys/module.h>
89 #include <sys/thread2.h>
92 #include <net/ifq_var.h>
93 #include <net/if_arp.h>
94 #include <net/ethernet.h>
95 #include <net/if_dl.h>
96 #include <net/if_media.h>
100 #include <net/vlan/if_vlan_var.h>
102 #include <machine/bus_memio.h>
103 #include <machine/bus.h>
104 #include <machine/resource.h>
106 #include <vm/vm.h> /* for vtophys */
107 #include <vm/pmap.h> /* for vtophys */
108 #include <machine/clock.h> /* for DELAY */
110 #include <sys/rman.h>
111 #include <sys/serialize.h>
113 #include <bus/pci/pcireg.h>
114 #include <bus/pci/pcivar.h>
116 #include <dev/netif/mii_layer/mii.h>
117 #include <dev/netif/mii_layer/miivar.h>
119 MODULE_DEPEND(nv, pci, 1, 1, 1);
120 MODULE_DEPEND(nv, miibus, 1, 1, 1);
122 #include "if_nvreg.h"
123 #include "miibus_if.h"
125 static int nv_probe(device_t);
126 static int nv_attach(device_t);
127 static int nv_detach(device_t);
128 static void nv_init(void *);
129 static void nv_stop(struct nv_softc *);
130 static void nv_shutdown(device_t);
131 static int nv_init_rings(struct nv_softc *);
132 static void nv_free_rings(struct nv_softc *);
134 static void nv_ifstart(struct ifnet *);
135 static int nv_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
136 static void nv_intr(void *);
137 static void nv_tick(void *);
138 static void nv_setmulti(struct nv_softc *);
139 static void nv_watchdog(struct ifnet *);
140 static void nv_update_stats(struct nv_softc *);
141 #ifdef DEVICE_POLLING
142 static void nv_poll(struct ifnet *, enum poll_cmd, int);
145 static int nv_ifmedia_upd(struct ifnet *);
146 static void nv_ifmedia_sts(struct ifnet *, struct ifmediareq *);
147 static int nv_miibus_readreg(device_t, int, int);
148 static void nv_miibus_writereg(device_t, int, int, int);
150 static void nv_dmamap_cb(void *, bus_dma_segment_t *, int, int);
151 static void nv_dmamap_tx_cb(void *, bus_dma_segment_t *, int, bus_size_t, int);
153 static NV_SINT32 nv_osalloc(PNV_VOID, PMEMORY_BLOCK);
154 static NV_SINT32 nv_osfree(PNV_VOID, PMEMORY_BLOCK);
155 static NV_SINT32 nv_osallocex(PNV_VOID, PMEMORY_BLOCKEX);
156 static NV_SINT32 nv_osfreeex(PNV_VOID, PMEMORY_BLOCKEX);
157 static NV_SINT32 nv_osclear(PNV_VOID, PNV_VOID, NV_SINT32);
158 static NV_SINT32 nv_osdelay(PNV_VOID, NV_UINT32);
159 static NV_SINT32 nv_osallocrxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID *);
160 static NV_SINT32 nv_osfreerxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID);
161 static NV_SINT32 nv_ospackettx(PNV_VOID, PNV_VOID, NV_UINT32);
162 static NV_SINT32 nv_ospacketrx(PNV_VOID, PNV_VOID, NV_UINT32, NV_UINT8 *, NV_UINT8);
163 static NV_SINT32 nv_oslinkchg(PNV_VOID, NV_SINT32);
164 static NV_SINT32 nv_osalloctimer(PNV_VOID, PNV_VOID *);
165 static NV_SINT32 nv_osfreetimer(PNV_VOID, PNV_VOID);
166 static NV_SINT32 nv_osinittimer(PNV_VOID, PNV_VOID, PTIMER_FUNC, PNV_VOID);
167 static NV_SINT32 nv_ossettimer(PNV_VOID, PNV_VOID, NV_UINT32);
168 static NV_SINT32 nv_oscanceltimer(PNV_VOID, PNV_VOID);
170 static NV_SINT32 nv_ospreprocpkt(PNV_VOID, PNV_VOID, PNV_VOID *, NV_UINT8 *, NV_UINT8);
171 static PNV_VOID nv_ospreprocpktnopq(PNV_VOID, PNV_VOID);
172 static NV_SINT32 nv_osindicatepkt(PNV_VOID, PNV_VOID *, NV_UINT32);
173 static NV_SINT32 nv_oslockalloc(PNV_VOID, NV_SINT32, PNV_VOID *);
174 static NV_SINT32 nv_oslockacquire(PNV_VOID, NV_SINT32, PNV_VOID);
175 static NV_SINT32 nv_oslockrelease(PNV_VOID, NV_SINT32, PNV_VOID);
176 static PNV_VOID nv_osreturnbufvirt(PNV_VOID, PNV_VOID);
178 static device_method_t nv_methods[] = {
179 /* Device interface */
180 DEVMETHOD(device_probe, nv_probe),
181 DEVMETHOD(device_attach, nv_attach),
182 DEVMETHOD(device_detach, nv_detach),
183 DEVMETHOD(device_shutdown, nv_shutdown),
186 DEVMETHOD(bus_print_child, bus_generic_print_child),
187 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
190 DEVMETHOD(miibus_readreg, nv_miibus_readreg),
191 DEVMETHOD(miibus_writereg, nv_miibus_writereg),
196 static driver_t nv_driver = {
199 sizeof(struct nv_softc)
202 static devclass_t nv_devclass;
204 static int nv_pollinterval = 0;
205 SYSCTL_INT(_hw, OID_AUTO, nv_pollinterval, CTLFLAG_RW,
206 &nv_pollinterval, 0, "delay between interface polls");
208 DRIVER_MODULE(nv, pci, nv_driver, nv_devclass, 0, 0);
209 DRIVER_MODULE(miibus, nv, miibus_driver, miibus_devclass, 0, 0);
211 static struct nv_type nv_devs[] = {
212 {NVIDIA_VENDORID, NFORCE_MCPNET1_DEVICEID,
213 "NVIDIA nForce MCP Networking Adapter"},
214 {NVIDIA_VENDORID, NFORCE_MCPNET2_DEVICEID,
215 "NVIDIA nForce MCP2 Networking Adapter"},
216 {NVIDIA_VENDORID, NFORCE_MCPNET3_DEVICEID,
217 "NVIDIA nForce MCP3 Networking Adapter"},
218 {NVIDIA_VENDORID, NFORCE_MCPNET4_DEVICEID,
219 "NVIDIA nForce MCP4 Networking Adapter"},
220 {NVIDIA_VENDORID, NFORCE_MCPNET5_DEVICEID,
221 "NVIDIA nForce MCP5 Networking Adapter"},
222 {NVIDIA_VENDORID, NFORCE_MCPNET6_DEVICEID,
223 "NVIDIA nForce MCP6 Networking Adapter"},
224 {NVIDIA_VENDORID, NFORCE_MCPNET7_DEVICEID,
225 "NVIDIA nForce MCP7 Networking Adapter"},
226 {NVIDIA_VENDORID, NFORCE_MCPNET8_DEVICEID,
227 "NVIDIA nForce MCP8 Networking Adapter"},
228 {NVIDIA_VENDORID, NFORCE_MCPNET9_DEVICEID,
229 "NVIDIA nForce MCP9 Networking Adapter"},
230 {NVIDIA_VENDORID, NFORCE_MCPNET10_DEVICEID,
231 "NVIDIA nForce MCP10 Networking Adapter"},
232 {NVIDIA_VENDORID, NFORCE_MCPNET11_DEVICEID,
233 "NVIDIA nForce MCP11 Networking Adapter"},
237 /* DMA MEM map callback function to get data segment physical address */
239 nv_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nsegs, int error)
245 ("Too many DMA segments returned when mapping DMA memory"));
246 *(bus_addr_t *)arg = segs->ds_addr;
249 /* DMA RX map callback function to get data segment physical address */
251 nv_dmamap_rx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
255 *(bus_addr_t *)arg = segs->ds_addr;
259 * DMA TX buffer callback function to allocate fragment data segment
263 nv_dmamap_tx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
265 struct nv_tx_desc *info = arg;
269 KASSERT(nsegs < NV_MAX_FRAGS,
270 ("Too many DMA segments returned when mapping mbuf"));
271 info->numfrags = nsegs;
272 bcopy(segs, info->frags, nsegs * sizeof(bus_dma_segment_t));
275 /* Probe for supported hardware ID's */
277 nv_probe(device_t dev)
279 struct nv_type *t = nv_devs;
281 /* Check for matching PCI DEVICE ID's */
282 while (t->name != NULL) {
283 if ((pci_get_vendor(dev) == t->vid_id) &&
284 (pci_get_device(dev) == t->dev_id)) {
285 device_set_desc(dev, t->name);
294 /* Attach driver and initialise hardware for use */
296 nv_attach(device_t dev)
298 u_char eaddr[ETHER_ADDR_LEN];
302 ADAPTER_OPEN_PARAMS OpenParams;
303 int error = 0, i, rid;
306 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_attach - entry\n");
308 sc = device_get_softc(dev);
309 unit = device_get_unit(dev);
313 callout_init(&sc->nv_stat_timer);
315 /* Preinitialize data structures */
316 bzero(&OpenParams, sizeof(ADAPTER_OPEN_PARAMS));
318 /* Enable bus mastering */
319 pci_enable_busmaster(dev);
321 /* Allocate memory mapped address space */
323 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
325 if (sc->res == NULL) {
326 device_printf(dev, "couldn't map memory\n");
330 sc->sc_st = rman_get_bustag(sc->res);
331 sc->sc_sh = rman_get_bushandle(sc->res);
333 /* Allocate interrupt */
335 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
336 RF_SHAREABLE | RF_ACTIVE);
338 if (sc->irq == NULL) {
339 device_printf(dev, "couldn't map interrupt\n");
343 /* Allocate DMA tags */
344 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
345 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * NV_MAX_FRAGS,
346 NV_MAX_FRAGS, MCLBYTES, 0,
349 device_printf(dev, "couldn't allocate dma tag\n");
352 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
353 BUS_SPACE_MAXADDR, NULL, NULL,
354 sizeof(struct nv_rx_desc) * RX_RING_SIZE, 1,
355 sizeof(struct nv_rx_desc) * RX_RING_SIZE, 0,
358 device_printf(dev, "couldn't allocate dma tag\n");
361 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
362 BUS_SPACE_MAXADDR, NULL, NULL,
363 sizeof(struct nv_tx_desc) * TX_RING_SIZE, 1,
364 sizeof(struct nv_tx_desc) * TX_RING_SIZE, 0,
367 device_printf(dev, "couldn't allocate dma tag\n");
371 error = bus_dmamap_create(sc->ttag, 0, &sc->tmap);
373 device_printf(dev, "couldn't create dma map\n");
377 /* Allocate DMA safe memory and get the DMA addresses. */
378 error = bus_dmamem_alloc(sc->ttag, (void **)&sc->tx_desc,
379 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tmap);
381 device_printf(dev, "couldn't allocate dma memory\n");
384 error = bus_dmamap_load(sc->ttag, sc->tmap, sc->tx_desc,
385 sizeof(struct nv_tx_desc) * TX_RING_SIZE, nv_dmamap_cb,
388 device_printf(dev, "couldn't map dma memory\n");
392 error = bus_dmamap_create(sc->rtag, 0, &sc->rmap);
394 device_printf(dev, "couldn't create dma map\n");
398 error = bus_dmamem_alloc(sc->rtag, (void **)&sc->rx_desc,
399 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rmap);
401 device_printf(dev, "couldn't allocate dma memory\n");
404 error = bus_dmamap_load(sc->rtag, sc->rmap, sc->rx_desc,
405 sizeof(struct nv_rx_desc) * RX_RING_SIZE, nv_dmamap_cb,
408 device_printf(dev, "couldn't map dma memory\n");
411 /* Initialize rings. */
412 if (nv_init_rings(sc)) {
413 device_printf(dev, "failed to init rings\n");
417 /* Setup NVIDIA API callback routines */
420 osapi->pfnAllocMemory = nv_osalloc;
421 osapi->pfnFreeMemory = nv_osfree;
422 osapi->pfnAllocMemoryEx = nv_osallocex;
423 osapi->pfnFreeMemoryEx = nv_osfreeex;
424 osapi->pfnClearMemory = nv_osclear;
425 osapi->pfnStallExecution = nv_osdelay;
426 osapi->pfnAllocReceiveBuffer = nv_osallocrxbuf;
427 osapi->pfnFreeReceiveBuffer = nv_osfreerxbuf;
428 osapi->pfnPacketWasSent = nv_ospackettx;
429 osapi->pfnPacketWasReceived = nv_ospacketrx;
430 osapi->pfnLinkStateHasChanged = nv_oslinkchg;
431 osapi->pfnAllocTimer = nv_osalloctimer;
432 osapi->pfnFreeTimer = nv_osfreetimer;
433 osapi->pfnInitializeTimer = nv_osinittimer;
434 osapi->pfnSetTimer = nv_ossettimer;
435 osapi->pfnCancelTimer = nv_oscanceltimer;
436 osapi->pfnPreprocessPacket = nv_ospreprocpkt;
437 osapi->pfnPreprocessPacketNopq = nv_ospreprocpktnopq;
438 osapi->pfnIndicatePackets = nv_osindicatepkt;
439 osapi->pfnLockAlloc = nv_oslockalloc;
440 osapi->pfnLockAcquire = nv_oslockacquire;
441 osapi->pfnLockRelease = nv_oslockrelease;
442 osapi->pfnReturnBufferVirtual = nv_osreturnbufvirt;
445 sc->max_frame_size = ETHERMTU + ETHER_HDR_LEN + FCS_LEN;
447 /* TODO - We don't support hardware offload yet */
451 /* Set NVIDIA API startup parameters */
452 OpenParams.MaxDpcLoop = 2;
453 OpenParams.MaxRxPkt = RX_RING_SIZE;
454 OpenParams.MaxTxPkt = TX_RING_SIZE;
455 OpenParams.SentPacketStatusSuccess = 1;
456 OpenParams.SentPacketStatusFailure = 0;
457 OpenParams.MaxRxPktToAccumulate = 6;
458 OpenParams.ulPollInterval = nv_pollinterval;
459 OpenParams.SetForcedModeEveryNthRxPacket = 0;
460 OpenParams.SetForcedModeEveryNthTxPacket = 0;
461 OpenParams.RxForcedInterrupt = 0;
462 OpenParams.TxForcedInterrupt = 0;
463 OpenParams.pOSApi = osapi;
464 OpenParams.pvHardwareBaseAddress = rman_get_virtual(sc->res);
465 OpenParams.bASFEnabled = 0;
466 OpenParams.ulDescriptorVersion = sc->hwmode;
467 OpenParams.ulMaxPacketSize = sc->max_frame_size;
468 OpenParams.DeviceId = pci_get_device(dev);
470 /* Open NVIDIA Hardware API */
471 error = ADAPTER_Open(&OpenParams, (void **)&(sc->hwapi), &sc->phyaddr);
473 device_printf(dev, "failed to open NVIDIA Hardware API: 0x%x\n", error);
477 /* TODO - Add support for MODE2 hardware offload */
479 bzero(&sc->adapterdata, sizeof(sc->adapterdata));
481 sc->adapterdata.ulMediaIF = sc->media;
482 sc->adapterdata.ulModeRegTxReadCompleteEnable = 1;
483 sc->hwapi->pfnSetCommonData(sc->hwapi->pADCX, &sc->adapterdata);
485 sc->hwapi->pfnInit(sc->hwapi->pADCX,
487 0, /* force full duplex */
489 0, /* force async mode */
492 /* MAC is loaded backwards into h/w reg */
493 sc->hwapi->pfnGetNodeAddress(sc->hwapi->pADCX, sc->original_mac_addr);
494 for (i = 0; i < 6; i++) {
495 eaddr[i] = sc->original_mac_addr[5 - i];
497 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, eaddr);
498 bcopy(eaddr, (char *)&sc->sc_macaddr, ETHER_ADDR_LEN);
500 DEBUGOUT(NV_DEBUG_INIT, "nv: do mii_phy_probe\n");
502 /* Probe device for MII interface to PHY */
503 if (mii_phy_probe(dev, &sc->miibus, nv_ifmedia_upd, nv_ifmedia_sts)) {
504 device_printf(dev, "MII without any phy!\n");
508 /* Setup interface parameters */
511 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
512 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
513 ifp->if_ioctl = nv_ioctl;
514 ifp->if_start = nv_ifstart;
515 #ifdef DEVICE_POLLING
516 ifp->if_poll = nv_poll;
518 ifp->if_watchdog = nv_watchdog;
520 ifp->if_init = nv_init;
521 ifp->if_mtu = ETHERMTU;
522 ifp->if_baudrate = IF_Mbps(100);
523 ifp->if_capabilities |= IFCAP_VLAN_MTU;
524 ifq_set_maxlen(&ifp->if_snd, TX_RING_SIZE - 1);
525 ifq_set_ready(&ifp->if_snd);
527 /* Attach to OS's managers. */
528 ether_ifattach(ifp, sc->sc_macaddr, NULL);
530 /* Activate our interrupt handler. - attach last to avoid lock */
531 error = bus_setup_intr(sc->dev, sc->irq, INTR_NETSAFE,
532 nv_intr, sc, &sc->sc_ih, ifp->if_serializer);
535 device_printf(sc->dev, "couldn't set up interrupt handler\n");
538 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_attach - exit\n");
547 /* Detach interface for module unload */
549 nv_detach(device_t dev)
551 struct nv_softc *sc = device_get_softc(dev);
554 ifp = &sc->arpcom.ac_if;
555 lwkt_serialize_enter(ifp->if_serializer);
557 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_detach - entry\n");
559 if (device_is_attached(dev)) {
565 device_delete_child(dev, sc->miibus);
566 bus_generic_detach(dev);
568 /* Reload unreversed address back into MAC in original state */
569 if (sc->original_mac_addr)
570 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->original_mac_addr);
572 DEBUGOUT(NV_DEBUG_DEINIT, "nv: do pfnClose\n");
573 /* Detach from NVIDIA hardware API */
574 if (sc->hwapi->pfnClose)
575 sc->hwapi->pfnClose(sc->hwapi->pADCX, FALSE);
576 /* Release resources */
578 bus_teardown_intr(sc->dev, sc->irq, sc->sc_ih);
580 bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
582 bus_release_resource(sc->dev, SYS_RES_MEMORY, NV_RID, sc->res);
587 bus_dmamap_unload(sc->rtag, sc->rmap);
588 bus_dmamem_free(sc->rtag, sc->rx_desc, sc->rmap);
589 bus_dmamap_destroy(sc->rtag, sc->rmap);
592 bus_dma_tag_destroy(sc->mtag);
594 bus_dma_tag_destroy(sc->ttag);
596 bus_dma_tag_destroy(sc->rtag);
598 lwkt_serialize_exit(ifp->if_serializer);
600 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_detach - exit\n");
604 /* Initialise interface and start it "RUNNING" */
608 struct nv_softc *sc = xsc;
612 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init - entry (%d)\n", sc->linkup);
616 /* Do nothing if already running */
617 if (ifp->if_flags & IFF_RUNNING)
622 DEBUGOUT(NV_DEBUG_INIT, "nv: do pfnInit\n");
623 /* Setup Hardware interface and allocate memory structures */
624 error = sc->hwapi->pfnInit(sc->hwapi->pADCX,
626 0, /* force full duplex */
628 0, /* force async mode */
632 device_printf(sc->dev, "failed to start NVIDIA Hardware interface\n");
635 /* Set the MAC address */
636 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->sc_macaddr);
638 sc->hwapi->pfnStart(sc->hwapi->pADCX);
640 /* Setup multicast filter */
644 /* Update interface parameters */
645 ifp->if_flags |= IFF_RUNNING;
646 ifp->if_flags &= ~IFF_OACTIVE;
649 * Enable the interrupt. Currently the nvidia API does not support
650 * polling, if we do not call pfnEnableInterrupts() we cannot
651 * issue the nvidia callback to process interrupts. Call the
652 * interrupt service routine in case the interrupt got stuck during
653 * a reset, renegotiation, or timeout.
656 lwkt_serialize_handler_enable(ifp->if_serializer);
657 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
659 if ((ifp->if_flags & IFF_POLLING) == 0) {
660 lwkt_serialize_handler_enable(ifp->if_serializer);
661 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
667 * Reset watchdog and ring queue indexes. XXX if the interface
668 * is reset with pending tx packets queued to the actual device,
669 * the mbufs are currently lost.
674 callout_reset(&sc->nv_stat_timer, hz, nv_tick, sc);
676 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init - exit\n");
679 #ifdef DEVICE_POLLING
682 nv_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
684 struct nv_softc *sc = ifp->if_softc;
689 * We must disable the hardware interrupt on the device
690 * as well as ensure that any interrupt queued prior to
691 * this point does not execute the handler function.
693 * NOTE! The nvidia API does not support polling with
694 * interrupts disabled, so we have to leave them turned on
698 sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
699 lwkt_serialize_handler_disable(ifp->if_serializer);
702 case POLL_DEREGISTER:
704 lwkt_serialize_handler_enable(ifp->if_serializer);
705 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
708 case POLL_AND_CHECK_STATUS:
711 if (ifp->if_flags & IFF_RUNNING) {
714 if (ifp->if_flags & IFF_RUNNING) {
715 if (!ifq_is_empty(&ifp->if_snd))
724 /* Stop interface activity ie. not "RUNNING" */
726 nv_stop(struct nv_softc *sc)
730 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_stop - entry\n");
735 /* Cancel tick timer */
736 callout_stop(&sc->nv_stat_timer);
739 * Stop hardware activity. The serializer handler disablement call
740 * prevents any interrupt scheduled prior to this call from calling
743 sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
744 lwkt_serialize_handler_disable(ifp->if_serializer);
746 sc->hwapi->pfnStop(sc->hwapi->pADCX, 0);
747 sc->hwapi->pfnClearTxDesc(sc->hwapi->pADCX);
749 DEBUGOUT(NV_DEBUG_DEINIT, "nv: do pfnDeinit\n");
750 /* Shutdown interface and deallocate memory buffers */
751 if (sc->hwapi->pfnDeinit)
752 sc->hwapi->pfnDeinit(sc->hwapi->pADCX, 0);
758 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
760 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_stop - exit\n");
763 /* Shutdown interface for unload/reboot */
765 nv_shutdown(device_t dev)
769 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_shutdown\n");
771 sc = device_get_softc(dev);
773 /* Stop hardware activity */
774 lwkt_serialize_enter(sc->sc_if.if_serializer);
776 lwkt_serialize_exit(sc->sc_if.if_serializer);
779 /* Allocate TX ring buffers */
781 nv_init_rings(struct nv_softc *sc)
785 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init_rings - entry\n");
787 sc->cur_rx = sc->cur_tx = sc->pending_rxs = sc->pending_txs = 0;
788 /* Initialise RX ring */
789 for (i = 0; i < RX_RING_SIZE; i++) {
790 struct nv_rx_desc *desc = sc->rx_desc + i;
791 struct nv_map_buffer *buf = &desc->buf;
793 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
794 if (buf->mbuf == NULL) {
795 device_printf(sc->dev, "couldn't allocate mbuf\n");
800 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
801 m_adj(buf->mbuf, ETHER_ALIGN);
803 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
805 device_printf(sc->dev, "couldn't create dma map\n");
809 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
810 nv_dmamap_rx_cb, &desc->paddr, 0);
812 device_printf(sc->dev, "couldn't dma map mbuf\n");
816 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
818 desc->buflength = buf->mbuf->m_len;
819 desc->vaddr = mtod(buf->mbuf, caddr_t);
821 bus_dmamap_sync(sc->rtag, sc->rmap,
822 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
824 /* Initialize TX ring */
825 for (i = 0; i < TX_RING_SIZE; i++) {
826 struct nv_tx_desc *desc = sc->tx_desc + i;
827 struct nv_map_buffer *buf = &desc->buf;
831 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
833 device_printf(sc->dev, "couldn't create dma map\n");
838 bus_dmamap_sync(sc->ttag, sc->tmap,
839 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
841 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init_rings - exit\n");
847 /* Free the TX ring buffers */
849 nv_free_rings(struct nv_softc *sc)
853 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_free_rings - entry\n");
855 for (i = 0; i < RX_RING_SIZE; i++) {
856 struct nv_rx_desc *desc = sc->rx_desc + i;
857 struct nv_map_buffer *buf = &desc->buf;
860 bus_dmamap_unload(sc->mtag, buf->map);
861 bus_dmamap_destroy(sc->mtag, buf->map);
867 for (i = 0; i < TX_RING_SIZE; i++) {
868 struct nv_tx_desc *desc = sc->tx_desc + i;
869 struct nv_map_buffer *buf = &desc->buf;
872 bus_dmamap_unload(sc->mtag, buf->map);
873 bus_dmamap_destroy(sc->mtag, buf->map);
879 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_free_rings - exit\n");
882 /* Main loop for sending packets from OS to interface */
885 nv_ifstart(struct ifnet *ifp)
887 struct nv_softc *sc = ifp->if_softc;
888 struct nv_map_buffer *buf;
890 struct nv_tx_desc *desc;
891 ADAPTER_WRITE_DATA txdata;
894 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_ifstart - entry\n");
896 /* If link is down/busy or queue is empty do nothing */
897 if ((ifp->if_flags & IFF_OACTIVE) || ifq_is_empty(&ifp->if_snd))
900 /* Transmit queued packets until sent or TX ring is full */
901 while (sc->pending_txs < TX_RING_SIZE) {
902 desc = sc->tx_desc + sc->cur_tx;
905 /* Get next packet to send. */
906 m0 = ifq_dequeue(&ifp->if_snd, NULL);
908 /* If nothing to send, return. */
912 /* Map MBUF for DMA access */
913 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m0,
914 nv_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
916 if (error && error != EFBIG) {
922 * Packet has too many fragments - defrag into new mbuf
926 m = m_defrag(m0, MB_DONTWAIT);
934 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m,
935 nv_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
942 /* Do sync on DMA bounce buffer */
943 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREWRITE);
946 txdata.ulNumberOfElements = desc->numfrags;
947 txdata.pvID = (PVOID)desc;
949 /* Put fragments into API element list */
950 txdata.ulTotalLength = buf->mbuf->m_len;
951 for (i = 0; i < desc->numfrags; i++) {
952 txdata.sElement[i].ulLength = (ulong)desc->frags[i].ds_len;
953 txdata.sElement[i].pPhysical = (PVOID)desc->frags[i].ds_addr;
956 /* Send packet to Nvidia API for transmission */
957 error = sc->hwapi->pfnWrite(sc->hwapi->pADCX, &txdata);
960 case ADAPTERERR_NONE:
961 /* Packet was queued in API TX queue successfully */
963 sc->cur_tx = (sc->cur_tx + 1) % TX_RING_SIZE;
966 case ADAPTERERR_TRANSMIT_QUEUE_FULL:
967 /* The API TX queue is full - requeue the packet */
968 device_printf(sc->dev, "nv_ifstart: transmit queue is full\n");
969 ifp->if_flags |= IFF_OACTIVE;
970 bus_dmamap_unload(sc->mtag, buf->map);
972 m_freem(m0); /* XXX requeue */
976 /* The API failed to queue/send the packet so dump it */
977 device_printf(sc->dev, "nv_ifstart: transmit error\n");
978 bus_dmamap_unload(sc->mtag, buf->map);
984 /* Set watchdog timer. */
987 /* Copy packet to BPF tap */
990 ifp->if_flags |= IFF_OACTIVE;
992 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_ifstart - exit\n");
995 /* Handle IOCTL events */
997 nv_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
999 struct nv_softc *sc = ifp->if_softc;
1000 struct ifreq *ifr = (struct ifreq *) data;
1001 struct mii_data *mii;
1004 DEBUGOUT(NV_DEBUG_IOCTL, "nv: nv_ioctl - entry\n");
1009 if (ifp->if_mtu == ifr->ifr_mtu)
1011 if (ifr->ifr_mtu + ifp->if_hdrlen <= MAX_PACKET_SIZE_1518) {
1012 ifp->if_mtu = ifr->ifr_mtu;
1020 /* Setup interface flags */
1021 if (ifp->if_flags & IFF_UP) {
1022 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1027 if (ifp->if_flags & IFF_RUNNING) {
1033 /* Handle IFF_PROMISC and IFF_ALLMULTI flags. */
1039 /* Setup multicast filter */
1040 if (ifp->if_flags & IFF_RUNNING) {
1046 /* Get/Set interface media parameters */
1047 mii = device_get_softc(sc->miibus);
1048 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1052 /* Everything else we forward to generic ether ioctl */
1053 error = ether_ioctl(ifp, command, data);
1057 DEBUGOUT(NV_DEBUG_IOCTL, "nv: nv_ioctl - exit\n");
1063 * Interrupt service routine. The serializer has already been entered
1064 * since we installed it in our bus_setup_intr() call.
1069 struct nv_softc *sc = arg;
1070 struct ifnet *ifp = &sc->sc_if;
1072 DEBUGOUT(NV_DEBUG_INTERRUPT, "nv: nv_intr - entry\n");
1075 * Handle an interrupt event. Unfortunately the nvidia API
1076 * does not support interrupt disablement when polling, so we
1077 * have to re-enable after the query masks them off.
1079 if (sc->hwapi->pfnQueryInterrupt(sc->hwapi->pADCX)) {
1080 sc->hwapi->pfnHandleInterrupt(sc->hwapi->pADCX);
1082 lwkt_serialize_handler_enable(ifp->if_serializer);
1083 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1085 if ((ifp->if_flags & IFF_POLLING) == 0) {
1086 lwkt_serialize_handler_enable(ifp->if_serializer);
1087 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1091 if (!ifq_is_empty(&ifp->if_snd))
1094 /* If no pending packets we don't need a timeout */
1095 if (sc->pending_txs == 0)
1096 sc->sc_if.if_timer = 0;
1098 DEBUGOUT(NV_DEBUG_INTERRUPT, "nv: nv_intr - exit\n");
1102 * Setup multicast filters
1104 * Serialized on call
1107 nv_setmulti(struct nv_softc *sc)
1110 struct ifmultiaddr *ifma;
1111 PACKET_FILTER hwfilter;
1114 u_int8_t andaddr[6];
1116 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_setmulti - entry\n");
1120 /* Initialize filter */
1121 hwfilter.ulFilterFlags = 0;
1122 for (i = 0; i < 6; i++) {
1123 hwfilter.acMulticastAddress[i] = 0;
1124 hwfilter.acMulticastMask[i] = 0;
1127 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1128 /* Accept all packets */
1129 hwfilter.ulFilterFlags |= ACCEPT_ALL_PACKETS;
1130 sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1133 /* Setup multicast filter */
1134 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1137 if (ifma->ifma_addr->sa_family != AF_LINK)
1140 addrp = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
1141 for (i = 0; i < 6; i++) {
1142 u_int8_t mcaddr = addrp[i];
1143 andaddr[i] &= mcaddr;
1144 oraddr[i] |= mcaddr;
1147 for (i = 0; i < 6; i++) {
1148 hwfilter.acMulticastAddress[i] = andaddr[i] & oraddr[i];
1149 hwfilter.acMulticastMask[i] = andaddr[i] | (~oraddr[i]);
1152 /* Send filter to NVIDIA API */
1153 sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1155 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_setmulti - exit\n");
1159 * Change the current media/mediaopts
1161 * Serialized on call
1164 nv_ifmedia_upd(struct ifnet *ifp)
1166 struct nv_softc *sc = ifp->if_softc;
1167 struct mii_data *mii;
1169 DEBUGOUT(NV_DEBUG_MII, "nv: nv_ifmedia_upd\n");
1171 mii = device_get_softc(sc->miibus);
1173 if (mii->mii_instance) {
1174 struct mii_softc *miisc;
1175 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1176 miisc = LIST_NEXT(miisc, mii_list)) {
1177 mii_phy_reset(miisc);
1186 * Update current miibus PHY status of media
1188 * Serialized on call
1191 nv_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1193 struct nv_softc *sc;
1194 struct mii_data *mii;
1196 DEBUGOUT(NV_DEBUG_MII, "nv: nv_ifmedia_sts\n");
1199 mii = device_get_softc(sc->miibus);
1202 ifmr->ifm_active = mii->mii_media_active;
1203 ifmr->ifm_status = mii->mii_media_status;
1206 /* miibus tick timer - maintain link status */
1210 struct nv_softc *sc = xsc;
1211 struct mii_data *mii;
1215 lwkt_serialize_enter(ifp->if_serializer);
1216 nv_update_stats(sc);
1218 mii = device_get_softc(sc->miibus);
1221 if ((mii->mii_media_status & IFM_ACTIVE) &&
1222 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1223 if (!ifq_is_empty(&ifp->if_snd))
1226 callout_reset(&sc->nv_stat_timer, hz, nv_tick, sc);
1227 lwkt_serialize_exit(ifp->if_serializer);
1230 /* Update ifnet data structure with collected interface stats from API */
1232 nv_update_stats(struct nv_softc *sc)
1234 struct ifnet *ifp = &sc->sc_if;
1235 ADAPTER_STATS stats;
1238 sc->hwapi->pfnGetStatistics(sc->hwapi->pADCX, &stats);
1240 ifp->if_ipackets = stats.ulSuccessfulReceptions;
1241 ifp->if_ierrors = stats.ulMissedFrames +
1242 stats.ulFailedReceptions +
1244 stats.ulFramingErrors +
1245 stats.ulOverFlowErrors;
1247 ifp->if_opackets = stats.ulSuccessfulTransmissions;
1248 ifp->if_oerrors = sc->tx_errors +
1249 stats.ulFailedTransmissions +
1250 stats.ulRetryErrors +
1251 stats.ulUnderflowErrors +
1252 stats.ulLossOfCarrierErrors +
1253 stats.ulLateCollisionErrors;
1255 ifp->if_collisions = stats.ulLateCollisionErrors;
1259 /* miibus Read PHY register wrapper - calls Nvidia API entry point */
1261 nv_miibus_readreg(device_t dev, int phy, int reg)
1263 struct nv_softc *sc = device_get_softc(dev);
1266 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_readreg - entry\n");
1268 ADAPTER_ReadPhy(sc->hwapi->pADCX, phy, reg, &data);
1270 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_readreg - exit\n");
1275 /* miibus Write PHY register wrapper - calls Nvidia API entry point */
1277 nv_miibus_writereg(device_t dev, int phy, int reg, int data)
1279 struct nv_softc *sc = device_get_softc(dev);
1281 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_writereg - entry\n");
1283 ADAPTER_WritePhy(sc->hwapi->pADCX, phy, reg, (ulong)data);
1285 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_writereg - exit\n");
1288 /* Watchdog timer to prevent PHY lockups */
1290 nv_watchdog(struct ifnet *ifp)
1292 struct nv_softc *sc = ifp->if_softc;
1294 device_printf(sc->dev, "device timeout (%d) flags %d\n",
1295 sc->pending_txs, ifp->if_flags & IFF_OACTIVE);
1300 ifp->if_flags &= ~IFF_RUNNING;
1303 if (!ifq_is_empty(&ifp->if_snd))
1307 /* --- Start of NVOSAPI interface --- */
1309 /* Allocate DMA enabled general use memory for API */
1311 nv_osalloc(PNV_VOID ctx, PMEMORY_BLOCK mem)
1313 struct nv_softc *sc;
1314 bus_addr_t mem_physical;
1316 DEBUGOUT(NV_DEBUG_API, "nv: nv_osalloc - %d\n", mem->uiLength);
1318 sc = (struct nv_softc *)ctx;
1320 mem->pLogical = (PVOID)contigmalloc(mem->uiLength, M_DEVBUF,
1321 M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE, 0);
1323 if (!mem->pLogical) {
1324 device_printf(sc->dev, "memory allocation failed\n");
1327 memset(mem->pLogical, 0, (ulong)mem->uiLength);
1328 mem_physical = vtophys(mem->pLogical);
1329 mem->pPhysical = (PVOID)mem_physical;
1331 DEBUGOUT(NV_DEBUG_API, "nv: nv_osalloc %p/%p - %d\n",
1332 mem->pLogical, mem->pPhysical, mem->uiLength);
1337 /* Free allocated memory */
1339 nv_osfree(PNV_VOID ctx, PMEMORY_BLOCK mem)
1341 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfree - %p - %d\n",
1342 mem->pLogical, mem->uiLength);
1344 contigfree(mem->pLogical, PAGE_SIZE, M_DEVBUF);
1348 /* Copied directly from nvnet.c */
1350 nv_osallocex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1352 MEMORY_BLOCK mem_block;
1354 DEBUGOUT(NV_DEBUG_API, "nv: nv_osallocex\n");
1356 mem_block_ex->pLogical = NULL;
1357 mem_block_ex->uiLengthOrig = mem_block_ex->uiLength;
1359 if ((mem_block_ex->AllocFlags & ALLOC_MEMORY_ALIGNED) &&
1360 (mem_block_ex->AlignmentSize > 1)) {
1361 DEBUGOUT(NV_DEBUG_API, " aligning on %d\n",
1362 mem_block_ex->AlignmentSize);
1363 mem_block_ex->uiLengthOrig += mem_block_ex->AlignmentSize;
1365 mem_block.uiLength = mem_block_ex->uiLengthOrig;
1367 if (nv_osalloc(ctx, &mem_block) == 0) {
1370 mem_block_ex->pLogicalOrig = mem_block.pLogical;
1371 mem_block_ex->pPhysicalOrigLow = (uintptr_t)mem_block.pPhysical;
1372 mem_block_ex->pPhysicalOrigHigh = 0;
1374 mem_block_ex->pPhysical = mem_block.pPhysical;
1375 mem_block_ex->pLogical = mem_block.pLogical;
1377 if (mem_block_ex->uiLength != mem_block_ex->uiLengthOrig) {
1378 unsigned int offset;
1379 offset = mem_block_ex->pPhysicalOrigLow & (mem_block_ex->AlignmentSize - 1);
1382 mem_block_ex->pPhysical = (PVOID)((uintptr_t)mem_block_ex->pPhysical +
1383 mem_block_ex->AlignmentSize - offset);
1384 mem_block_ex->pLogical = (PVOID)((uintptr_t)mem_block_ex->pLogical +
1385 mem_block_ex->AlignmentSize - offset);
1387 } /* if (mem_block_ex->uiLength !=
1388 * mem_block_ex->uiLengthOrig) */
1392 /* Copied directly from nvnet.c */
1394 nv_osfreeex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1396 MEMORY_BLOCK mem_block;
1398 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfreeex\n");
1400 mem_block.pLogical = mem_block_ex->pLogicalOrig;
1401 mem_block.pPhysical = (PVOID)((uintptr_t)mem_block_ex->pPhysicalOrigLow);
1402 mem_block.uiLength = mem_block_ex->uiLengthOrig;
1404 return (nv_osfree(ctx, &mem_block));
1407 /* Clear memory region */
1409 nv_osclear(PNV_VOID ctx, PNV_VOID mem, NV_SINT32 length)
1411 DEBUGOUT(NV_DEBUG_API, "nv: nv_osclear\n");
1412 memset(mem, 0, length);
1416 /* Sleep for a tick */
1418 nv_osdelay(PNV_VOID ctx, NV_UINT32 usec)
1420 if (usec >= 1000000 / hz) {
1421 tsleep(nv_osdelay, 0, "nvdelay", (usec * hz / 1000000) + 1);
1428 /* Allocate memory for rx buffer */
1430 nv_osallocrxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID *id)
1432 struct nv_softc *sc = ctx;
1433 struct nv_rx_desc *desc;
1434 struct nv_map_buffer *buf;
1437 DEBUGOUT(NV_DEBUG_API, "nv: nv_osallocrxbuf\n");
1439 if (sc->pending_rxs == RX_RING_SIZE) {
1440 device_printf(sc->dev, "rx ring buffer is full\n");
1443 desc = sc->rx_desc + sc->cur_rx;
1446 if (buf->mbuf == NULL) {
1447 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1448 if (buf->mbuf == NULL) {
1449 device_printf(sc->dev, "failed to allocate memory\n");
1452 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
1453 m_adj(buf->mbuf, ETHER_ALIGN);
1455 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
1456 nv_dmamap_rx_cb, &desc->paddr, 0);
1458 device_printf(sc->dev, "failed to dmamap mbuf\n");
1463 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
1464 desc->buflength = buf->mbuf->m_len;
1465 desc->vaddr = mtod(buf->mbuf, PVOID);
1468 sc->cur_rx = (sc->cur_rx + 1) % RX_RING_SIZE;
1470 mem->pLogical = (void *)desc->vaddr;
1471 mem->pPhysical = (void *)desc->paddr;
1472 mem->uiLength = desc->buflength;
1481 /* Free the rx buffer */
1483 nv_osfreerxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID id)
1485 struct nv_softc *sc = ctx;
1486 struct nv_rx_desc *desc;
1487 struct nv_map_buffer *buf;
1489 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfreerxbuf\n");
1491 desc = (struct nv_rx_desc *) id;
1495 bus_dmamap_unload(sc->mtag, buf->map);
1496 bus_dmamap_destroy(sc->mtag, buf->map);
1505 /* This gets called by the Nvidia API after our TX packet has been sent */
1507 nv_ospackettx(PNV_VOID ctx, PNV_VOID id, NV_UINT32 success)
1509 struct nv_softc *sc = ctx;
1510 struct nv_map_buffer *buf;
1511 struct nv_tx_desc *desc = (struct nv_tx_desc *) id;
1514 DEBUGOUT(NV_DEBUG_API, "nv: nv_ospackettx\n");
1520 /* Unload and free mbuf cluster */
1521 if (buf->mbuf == NULL)
1524 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTWRITE);
1525 bus_dmamap_unload(sc->mtag, buf->map);
1530 * Make sure we are clear to go if we previously stalled due
1533 if (sc->pending_txs < TX_RING_SIZE) {
1534 ifp->if_flags &= ~IFF_OACTIVE;
1535 if (!ifq_is_empty(&ifp->if_snd))
1542 /* This gets called by the Nvidia API when a new packet has been received */
1543 /* XXX What is newbuf used for? XXX */
1545 nv_ospacketrx(PNV_VOID ctx, PNV_VOID data, NV_UINT32 success,
1546 NV_UINT8 *newbuf, NV_UINT8 priority)
1548 struct nv_softc *sc = ctx;
1550 struct nv_rx_desc *desc;
1551 struct nv_map_buffer *buf;
1552 ADAPTER_READ_DATA *readdata;
1554 DEBUGOUT(NV_DEBUG_API, "nv: nv_ospacketrx\n");
1558 readdata = (ADAPTER_READ_DATA *) data;
1559 desc = readdata->pvID;
1561 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1564 /* Sync DMA bounce buffer. */
1565 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1567 /* First mbuf in packet holds the ethernet and packet headers */
1568 buf->mbuf->m_pkthdr.rcvif = ifp;
1569 buf->mbuf->m_pkthdr.len = buf->mbuf->m_len = readdata->ulTotalLength;
1571 bus_dmamap_unload(sc->mtag, buf->map);
1573 /* Give mbuf to OS. */
1574 ifp->if_input(ifp, buf->mbuf);
1575 if (readdata->ulFilterMatch & ADREADFL_MULTICAST_MATCH)
1578 /* Blat the mbuf pointer, kernel will free the mbuf cluster */
1581 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1582 bus_dmamap_unload(sc->mtag, buf->map);
1587 sc->cur_rx = desc - sc->rx_desc;
1593 /* This gets called by NVIDIA API when the PHY link state changes */
1595 nv_oslinkchg(PNV_VOID ctx, NV_SINT32 enabled)
1597 struct nv_softc *sc = (struct nv_softc *)ctx;
1600 DEBUGOUT(NV_DEBUG_API, "nv: nv_oslinkchg\n");
1605 ifp->if_flags |= IFF_UP;
1607 ifp->if_flags &= ~IFF_UP;
1614 /* Setup a watchdog timer */
1616 nv_osalloctimer(PNV_VOID ctx, PNV_VOID *timer)
1618 struct nv_softc *sc = (struct nv_softc *)ctx;
1620 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osalloctimer\n");
1622 callout_init(&sc->ostimer);
1623 *timer = &sc->ostimer;
1628 /* Free the timer */
1630 nv_osfreetimer(PNV_VOID ctx, PNV_VOID timer)
1632 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osfreetimer\n");
1637 /* Setup timer parameters */
1639 nv_osinittimer(PNV_VOID ctx, PNV_VOID timer, PTIMER_FUNC func, PNV_VOID parameters)
1641 struct nv_softc *sc = (struct nv_softc *)ctx;
1643 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osinittimer\n");
1645 sc->ostimer_func = func;
1646 sc->ostimer_params = parameters;
1652 nv_ostimer_callback(void *data)
1654 struct nv_softc *sc = data;
1655 struct ifnet *ifp = &sc->sc_if;
1657 lwkt_serialize_enter(ifp->if_serializer);
1658 sc->ostimer_func(sc->ostimer_params);
1659 lwkt_serialize_exit(ifp->if_serializer);
1663 * Set the timer to go off
1665 * XXX what the hell are the units for 'delay' ? They sure aren't ticks!
1668 nv_ossettimer(PNV_VOID ctx, PNV_VOID timer, NV_UINT32 delay)
1670 struct nv_softc *sc = ctx;
1672 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ossettimer\n");
1673 printf("nv_ossettimer %d\n", (int)delay);
1675 callout_reset(&sc->ostimer, delay, nv_ostimer_callback, sc);
1680 /* Cancel the timer */
1682 nv_oscanceltimer(PNV_VOID ctx, PNV_VOID timer)
1684 struct nv_softc *sc = ctx;
1686 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_oscanceltimer\n");
1688 callout_stop(&sc->ostimer);
1694 nv_ospreprocpkt(PNV_VOID ctx, PNV_VOID readdata, PNV_VOID *id, NV_UINT8 *newbuffer,
1697 /* Not implemented */
1698 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ospreprocpkt\n");
1704 nv_ospreprocpktnopq(PNV_VOID ctx, PNV_VOID readdata)
1706 /* Not implemented */
1707 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ospreprocpkt\n");
1713 nv_osindicatepkt(PNV_VOID ctx, PNV_VOID *id, NV_UINT32 pktno)
1715 /* Not implemented */
1716 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osindicatepkt\n");
1721 /* Allocate mutex context (already done in nv_attach) */
1723 nv_oslockalloc(PNV_VOID ctx, NV_SINT32 type, PNV_VOID *pLock)
1725 struct nv_softc *sc = (struct nv_softc *)ctx;
1727 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockalloc\n");
1729 *pLock = (void **)sc;
1734 /* Obtain a spin lock */
1736 nv_oslockacquire(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1738 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockacquire\n");
1740 NV_OSLOCK((struct nv_softc *)lock);
1747 nv_oslockrelease(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1749 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockrelease\n");
1751 NV_OSUNLOCK((struct nv_softc *)lock);
1756 /* I have no idea what this is for */
1758 nv_osreturnbufvirt(PNV_VOID ctx, PNV_VOID readdata)
1760 /* Not implemented */
1761 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_osreturnbufvirt\n");
1762 panic("nv: nv_osreturnbufvirtual not implemented\n");
1768 /* --- End on NVOSAPI interface --- */