2 * Copyright (c) 2005 by David E. O'Brien <obrien@FreeBSD.org>.
3 * Copyright (c) 2003,2004 by Quinton Dolan <q@onthenet.com.au>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $Id: if_nv.c,v 1.20 2005/03/12 01:11:00 q Exp $
28 * $FreeBSD: src/sys/dev/nve/if_nve.c,v 1.20 2005/12/12 06:23:43 bz Exp $
29 * $DragonFly: src/sys/dev/netif/nv/Attic/if_nv.c,v 1.29 2006/12/22 23:26:21 swildner Exp $
33 * NVIDIA nForce MCP Networking Adapter driver
35 * This is a port of the NVIDIA MCP Linux ethernet driver distributed by NVIDIA
36 * through their web site.
38 * All mainstream nForce and nForce2 motherboards are supported. This module
39 * is as stable, sometimes more stable, than the linux version. (Recent
40 * Linux stability issues seem to be related to some issues with newer
41 * distributions using GCC 3.x, however this don't appear to effect FreeBSD
44 * In accordance with the NVIDIA distribution license it is necessary to
45 * link this module against the nvlibnet.o binary object included in the
46 * Linux driver source distribution. The binary component is not modified in
47 * any way and is simply linked against a FreeBSD equivalent of the nvnet.c
48 * linux kernel module "wrapper".
50 * The Linux driver uses a common code API that is shared between Win32 and
51 * i386 Linux. This abstracts the low level driver functions and uses
52 * callbacks and hooks to access the underlying hardware device. By using
53 * this same API in a FreeBSD kernel module it is possible to support the
54 * hardware without breaching the Linux source distributions licensing
55 * requirements, or obtaining the hardware programming specifications.
57 * Although not conventional, it works, and given the relatively small
58 * amount of hardware centric code, it's hopefully no more buggy than its
61 * NVIDIA now support the nForce3 AMD64 platform, however I have been
62 * unable to access such a system to verify support. However, the code is
63 * reported to work with little modification when compiled with the AMD64
64 * version of the NVIDIA Linux library. All that should be necessary to make
65 * the driver work is to link it directly into the kernel, instead of as a
66 * module, and apply the docs/amd64.diff patch in this source distribution to
67 * the NVIDIA Linux driver source.
69 * This driver should work on all versions of FreeBSD since 4.9/5.1 as well
70 * as recent versions of DragonFly.
72 * Written by Quinton Dolan <q@onthenet.com.au>
73 * Portions based on existing FreeBSD network drivers.
74 * NVIDIA API usage derived from distributed NVIDIA NVNET driver source files.
76 * $Id: if_nv.c,v 1.9 2003/12/13 15:27:40 q Exp $
79 #include "opt_polling.h"
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/sockio.h>
85 #include <sys/malloc.h>
86 #include <sys/kernel.h>
87 #include <sys/socket.h>
88 #include <sys/sysctl.h>
89 #include <sys/queue.h>
90 #include <sys/module.h>
93 #include <sys/serialize.h>
94 #include <sys/thread2.h>
97 #include <net/ifq_var.h>
98 #include <net/if_arp.h>
99 #include <net/ethernet.h>
100 #include <net/if_dl.h>
101 #include <net/if_media.h>
104 #include <net/vlan/if_vlan_var.h>
106 #include <vm/vm.h> /* for vtophys */
107 #include <vm/pmap.h> /* for vtophys */
108 #include <machine/clock.h> /* for DELAY */
110 #include <bus/pci/pcireg.h>
111 #include <bus/pci/pcivar.h>
113 #include <dev/netif/mii_layer/mii.h>
114 #include <dev/netif/mii_layer/miivar.h>
116 MODULE_DEPEND(nv, pci, 1, 1, 1);
117 MODULE_DEPEND(nv, miibus, 1, 1, 1);
119 #include "if_nvreg.h"
120 #include "miibus_if.h"
122 static int nv_probe(device_t);
123 static int nv_attach(device_t);
124 static int nv_detach(device_t);
125 static void nv_init(void *);
126 static void nv_stop(struct nv_softc *);
127 static void nv_shutdown(device_t);
128 static int nv_init_rings(struct nv_softc *);
129 static void nv_free_rings(struct nv_softc *);
131 static void nv_ifstart(struct ifnet *);
132 static int nv_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
133 static void nv_intr(void *);
134 static void nv_tick(void *);
135 static void nv_setmulti(struct nv_softc *);
136 static void nv_watchdog(struct ifnet *);
137 static void nv_update_stats(struct nv_softc *);
138 #ifdef DEVICE_POLLING
139 static void nv_poll(struct ifnet *, enum poll_cmd, int);
142 static int nv_ifmedia_upd(struct ifnet *);
143 static void nv_ifmedia_sts(struct ifnet *, struct ifmediareq *);
144 static int nv_miibus_readreg(device_t, int, int);
145 static void nv_miibus_writereg(device_t, int, int, int);
147 static void nv_dmamap_cb(void *, bus_dma_segment_t *, int, int);
148 static void nv_dmamap_tx_cb(void *, bus_dma_segment_t *, int, bus_size_t, int);
150 static NV_SINT32 nv_osalloc(PNV_VOID, PMEMORY_BLOCK);
151 static NV_SINT32 nv_osfree(PNV_VOID, PMEMORY_BLOCK);
152 static NV_SINT32 nv_osallocex(PNV_VOID, PMEMORY_BLOCKEX);
153 static NV_SINT32 nv_osfreeex(PNV_VOID, PMEMORY_BLOCKEX);
154 static NV_SINT32 nv_osclear(PNV_VOID, PNV_VOID, NV_SINT32);
155 static NV_SINT32 nv_osdelay(PNV_VOID, NV_UINT32);
156 static NV_SINT32 nv_osallocrxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID *);
157 static NV_SINT32 nv_osfreerxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID);
158 static NV_SINT32 nv_ospackettx(PNV_VOID, PNV_VOID, NV_UINT32);
159 static NV_SINT32 nv_ospacketrx(PNV_VOID, PNV_VOID, NV_UINT32, NV_UINT8 *, NV_UINT8);
160 static NV_SINT32 nv_oslinkchg(PNV_VOID, NV_SINT32);
161 static NV_SINT32 nv_osalloctimer(PNV_VOID, PNV_VOID *);
162 static NV_SINT32 nv_osfreetimer(PNV_VOID, PNV_VOID);
163 static NV_SINT32 nv_osinittimer(PNV_VOID, PNV_VOID, PTIMER_FUNC, PNV_VOID);
164 static NV_SINT32 nv_ossettimer(PNV_VOID, PNV_VOID, NV_UINT32);
165 static NV_SINT32 nv_oscanceltimer(PNV_VOID, PNV_VOID);
167 static NV_SINT32 nv_ospreprocpkt(PNV_VOID, PNV_VOID, PNV_VOID *, NV_UINT8 *, NV_UINT8);
168 static PNV_VOID nv_ospreprocpktnopq(PNV_VOID, PNV_VOID);
169 static NV_SINT32 nv_osindicatepkt(PNV_VOID, PNV_VOID *, NV_UINT32);
170 static NV_SINT32 nv_oslockalloc(PNV_VOID, NV_SINT32, PNV_VOID *);
171 static NV_SINT32 nv_oslockacquire(PNV_VOID, NV_SINT32, PNV_VOID);
172 static NV_SINT32 nv_oslockrelease(PNV_VOID, NV_SINT32, PNV_VOID);
173 static PNV_VOID nv_osreturnbufvirt(PNV_VOID, PNV_VOID);
175 static device_method_t nv_methods[] = {
176 /* Device interface */
177 DEVMETHOD(device_probe, nv_probe),
178 DEVMETHOD(device_attach, nv_attach),
179 DEVMETHOD(device_detach, nv_detach),
180 DEVMETHOD(device_shutdown, nv_shutdown),
183 DEVMETHOD(bus_print_child, bus_generic_print_child),
184 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
187 DEVMETHOD(miibus_readreg, nv_miibus_readreg),
188 DEVMETHOD(miibus_writereg, nv_miibus_writereg),
193 static driver_t nv_driver = {
196 sizeof(struct nv_softc)
199 static devclass_t nv_devclass;
201 static int nv_pollinterval = 0;
202 SYSCTL_INT(_hw, OID_AUTO, nv_pollinterval, CTLFLAG_RW,
203 &nv_pollinterval, 0, "delay between interface polls");
205 DRIVER_MODULE(nv, pci, nv_driver, nv_devclass, 0, 0);
206 DRIVER_MODULE(miibus, nv, miibus_driver, miibus_devclass, 0, 0);
208 static struct nv_type nv_devs[] = {
209 {NVIDIA_VENDORID, NFORCE_MCPNET1_DEVICEID,
210 "NVIDIA nForce MCP Networking Adapter"},
211 {NVIDIA_VENDORID, NFORCE_MCPNET2_DEVICEID,
212 "NVIDIA nForce MCP2 Networking Adapter"},
213 {NVIDIA_VENDORID, NFORCE_MCPNET3_DEVICEID,
214 "NVIDIA nForce MCP3 Networking Adapter"},
215 {NVIDIA_VENDORID, NFORCE_MCPNET4_DEVICEID,
216 "NVIDIA nForce MCP4 Networking Adapter"},
217 {NVIDIA_VENDORID, NFORCE_MCPNET5_DEVICEID,
218 "NVIDIA nForce MCP5 Networking Adapter"},
219 {NVIDIA_VENDORID, NFORCE_MCPNET6_DEVICEID,
220 "NVIDIA nForce MCP6 Networking Adapter"},
221 {NVIDIA_VENDORID, NFORCE_MCPNET7_DEVICEID,
222 "NVIDIA nForce MCP7 Networking Adapter"},
223 {NVIDIA_VENDORID, NFORCE_MCPNET8_DEVICEID,
224 "NVIDIA nForce MCP8 Networking Adapter"},
225 {NVIDIA_VENDORID, NFORCE_MCPNET9_DEVICEID,
226 "NVIDIA nForce MCP9 Networking Adapter"},
227 {NVIDIA_VENDORID, NFORCE_MCPNET10_DEVICEID,
228 "NVIDIA nForce MCP10 Networking Adapter"},
229 {NVIDIA_VENDORID, NFORCE_MCPNET11_DEVICEID,
230 "NVIDIA nForce MCP11 Networking Adapter"},
231 {NVIDIA_VENDORID, NFORCE_MCPNET12_DEVICEID,
232 "NVIDIA nForce MCP12 Networking Adapter"},
233 {NVIDIA_VENDORID, NFORCE_MCPNET13_DEVICEID,
234 "NVIDIA nForce MCP13 Networking Adapter"},
238 /* DMA MEM map callback function to get data segment physical address */
240 nv_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nsegs, int error)
246 ("Too many DMA segments returned when mapping DMA memory"));
247 *(bus_addr_t *)arg = segs->ds_addr;
250 /* DMA RX map callback function to get data segment physical address */
252 nv_dmamap_rx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
256 *(bus_addr_t *)arg = segs->ds_addr;
260 * DMA TX buffer callback function to allocate fragment data segment
264 nv_dmamap_tx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
266 struct nv_tx_desc *info = arg;
270 KASSERT(nsegs < NV_MAX_FRAGS,
271 ("Too many DMA segments returned when mapping mbuf"));
272 info->numfrags = nsegs;
273 bcopy(segs, info->frags, nsegs * sizeof(bus_dma_segment_t));
276 /* Probe for supported hardware ID's */
278 nv_probe(device_t dev)
280 struct nv_type *t = nv_devs;
282 /* Check for matching PCI DEVICE ID's */
283 while (t->name != NULL) {
284 if ((pci_get_vendor(dev) == t->vid_id) &&
285 (pci_get_device(dev) == t->dev_id)) {
286 device_set_desc(dev, t->name);
295 /* Attach driver and initialise hardware for use */
297 nv_attach(device_t dev)
299 u_char eaddr[ETHER_ADDR_LEN];
303 ADAPTER_OPEN_PARAMS OpenParams;
304 int error = 0, i, rid;
308 device_printf(dev, "nvenetlib.o version %s\n", DRIVER_VERSION);
310 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_attach - entry\n");
312 sc = device_get_softc(dev);
313 unit = device_get_unit(dev);
317 callout_init(&sc->nv_stat_timer);
319 /* Preinitialize data structures */
320 bzero(&OpenParams, sizeof(ADAPTER_OPEN_PARAMS));
322 /* Enable bus mastering */
323 pci_enable_busmaster(dev);
325 /* Allocate memory mapped address space */
327 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
329 if (sc->res == NULL) {
330 device_printf(dev, "couldn't map memory\n");
334 sc->sc_st = rman_get_bustag(sc->res);
335 sc->sc_sh = rman_get_bushandle(sc->res);
337 /* Allocate interrupt */
339 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
340 RF_SHAREABLE | RF_ACTIVE);
342 if (sc->irq == NULL) {
343 device_printf(dev, "couldn't map interrupt\n");
347 /* Allocate DMA tags */
348 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
349 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * NV_MAX_FRAGS,
350 NV_MAX_FRAGS, MCLBYTES, 0,
353 device_printf(dev, "couldn't allocate dma tag\n");
356 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
357 BUS_SPACE_MAXADDR, NULL, NULL,
358 sizeof(struct nv_rx_desc) * RX_RING_SIZE, 1,
359 sizeof(struct nv_rx_desc) * RX_RING_SIZE, 0,
362 device_printf(dev, "couldn't allocate dma tag\n");
365 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
366 BUS_SPACE_MAXADDR, NULL, NULL,
367 sizeof(struct nv_tx_desc) * TX_RING_SIZE, 1,
368 sizeof(struct nv_tx_desc) * TX_RING_SIZE, 0,
371 device_printf(dev, "couldn't allocate dma tag\n");
375 error = bus_dmamap_create(sc->ttag, 0, &sc->tmap);
377 device_printf(dev, "couldn't create dma map\n");
381 /* Allocate DMA safe memory and get the DMA addresses. */
382 error = bus_dmamem_alloc(sc->ttag, (void **)&sc->tx_desc,
383 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tmap);
385 device_printf(dev, "couldn't allocate dma memory\n");
388 error = bus_dmamap_load(sc->ttag, sc->tmap, sc->tx_desc,
389 sizeof(struct nv_tx_desc) * TX_RING_SIZE, nv_dmamap_cb,
392 device_printf(dev, "couldn't map dma memory\n");
396 error = bus_dmamap_create(sc->rtag, 0, &sc->rmap);
398 device_printf(dev, "couldn't create dma map\n");
402 error = bus_dmamem_alloc(sc->rtag, (void **)&sc->rx_desc,
403 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rmap);
405 device_printf(dev, "couldn't allocate dma memory\n");
408 error = bus_dmamap_load(sc->rtag, sc->rmap, sc->rx_desc,
409 sizeof(struct nv_rx_desc) * RX_RING_SIZE, nv_dmamap_cb,
412 device_printf(dev, "couldn't map dma memory\n");
415 /* Initialize rings. */
416 if (nv_init_rings(sc)) {
417 device_printf(dev, "failed to init rings\n");
421 /* Setup NVIDIA API callback routines */
424 osapi->pfnAllocMemory = nv_osalloc;
425 osapi->pfnFreeMemory = nv_osfree;
426 osapi->pfnAllocMemoryEx = nv_osallocex;
427 osapi->pfnFreeMemoryEx = nv_osfreeex;
428 osapi->pfnClearMemory = nv_osclear;
429 osapi->pfnStallExecution = nv_osdelay;
430 osapi->pfnAllocReceiveBuffer = nv_osallocrxbuf;
431 osapi->pfnFreeReceiveBuffer = nv_osfreerxbuf;
432 osapi->pfnPacketWasSent = nv_ospackettx;
433 osapi->pfnPacketWasReceived = nv_ospacketrx;
434 osapi->pfnLinkStateHasChanged = nv_oslinkchg;
435 osapi->pfnAllocTimer = nv_osalloctimer;
436 osapi->pfnFreeTimer = nv_osfreetimer;
437 osapi->pfnInitializeTimer = nv_osinittimer;
438 osapi->pfnSetTimer = nv_ossettimer;
439 osapi->pfnCancelTimer = nv_oscanceltimer;
440 osapi->pfnPreprocessPacket = nv_ospreprocpkt;
441 osapi->pfnPreprocessPacketNopq = nv_ospreprocpktnopq;
442 osapi->pfnIndicatePackets = nv_osindicatepkt;
443 osapi->pfnLockAlloc = nv_oslockalloc;
444 osapi->pfnLockAcquire = nv_oslockacquire;
445 osapi->pfnLockRelease = nv_oslockrelease;
446 osapi->pfnReturnBufferVirtual = nv_osreturnbufvirt;
449 sc->max_frame_size = ETHERMTU + ETHER_HDR_LEN + FCS_LEN;
451 /* TODO - We don't support hardware offload yet */
455 /* Set NVIDIA API startup parameters */
456 OpenParams.MaxDpcLoop = 2;
457 OpenParams.MaxRxPkt = RX_RING_SIZE;
458 OpenParams.MaxTxPkt = TX_RING_SIZE;
459 OpenParams.SentPacketStatusSuccess = 1;
460 OpenParams.SentPacketStatusFailure = 0;
461 OpenParams.MaxRxPktToAccumulate = 6;
462 OpenParams.ulPollInterval = nv_pollinterval;
463 OpenParams.SetForcedModeEveryNthRxPacket = 0;
464 OpenParams.SetForcedModeEveryNthTxPacket = 0;
465 OpenParams.RxForcedInterrupt = 0;
466 OpenParams.TxForcedInterrupt = 0;
467 OpenParams.pOSApi = osapi;
468 OpenParams.pvHardwareBaseAddress = rman_get_virtual(sc->res);
469 OpenParams.bASFEnabled = 0;
470 OpenParams.ulDescriptorVersion = sc->hwmode;
471 OpenParams.ulMaxPacketSize = sc->max_frame_size;
472 OpenParams.DeviceId = pci_get_device(dev);
474 /* Open NVIDIA Hardware API */
475 error = ADAPTER_Open(&OpenParams, (void **)&(sc->hwapi), &sc->phyaddr);
477 device_printf(dev, "failed to open NVIDIA Hardware API: 0x%x\n", error);
481 /* TODO - Add support for MODE2 hardware offload */
483 bzero(&sc->adapterdata, sizeof(sc->adapterdata));
485 sc->adapterdata.ulMediaIF = sc->media;
486 sc->adapterdata.ulModeRegTxReadCompleteEnable = 1;
487 sc->hwapi->pfnSetCommonData(sc->hwapi->pADCX, &sc->adapterdata);
489 sc->hwapi->pfnInit(sc->hwapi->pADCX,
491 0, /* force full duplex */
493 0, /* force async mode */
496 /* MAC is loaded backwards into h/w reg */
497 sc->hwapi->pfnGetNodeAddress(sc->hwapi->pADCX, sc->original_mac_addr);
498 for (i = 0; i < 6; i++) {
499 eaddr[i] = sc->original_mac_addr[5 - i];
501 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, eaddr);
502 bcopy(eaddr, (char *)&sc->sc_macaddr, ETHER_ADDR_LEN);
504 DEBUGOUT(NV_DEBUG_INIT, "nv: do mii_phy_probe\n");
506 /* Probe device for MII interface to PHY */
507 if (mii_phy_probe(dev, &sc->miibus, nv_ifmedia_upd, nv_ifmedia_sts)) {
508 device_printf(dev, "MII without any phy!\n");
512 /* Setup interface parameters */
515 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
516 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
517 ifp->if_ioctl = nv_ioctl;
518 ifp->if_start = nv_ifstart;
519 #ifdef DEVICE_POLLING
520 ifp->if_poll = nv_poll;
522 ifp->if_watchdog = nv_watchdog;
524 ifp->if_init = nv_init;
525 ifp->if_mtu = ETHERMTU;
526 ifp->if_baudrate = IF_Mbps(100);
527 ifp->if_capabilities |= IFCAP_VLAN_MTU;
528 ifq_set_maxlen(&ifp->if_snd, TX_RING_SIZE - 1);
529 ifq_set_ready(&ifp->if_snd);
531 /* Attach to OS's managers. */
532 ether_ifattach(ifp, sc->sc_macaddr, NULL);
534 /* Activate our interrupt handler. - attach last to avoid lock */
535 error = bus_setup_intr(sc->dev, sc->irq, INTR_NETSAFE,
536 nv_intr, sc, &sc->sc_ih, ifp->if_serializer);
539 device_printf(sc->dev, "couldn't set up interrupt handler\n");
542 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_attach - exit\n");
551 /* Detach interface for module unload */
553 nv_detach(device_t dev)
555 struct nv_softc *sc = device_get_softc(dev);
559 ifp = &sc->arpcom.ac_if;
561 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_detach - entry\n");
563 lwkt_serialize_enter(ifp->if_serializer);
565 is_attached = device_is_attached(dev);
570 device_delete_child(dev, sc->miibus);
571 bus_generic_detach(dev);
573 /* Reload unreversed address back into MAC in original state */
574 if (sc->original_mac_addr)
575 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->original_mac_addr);
577 DEBUGOUT(NV_DEBUG_DEINIT, "nv: do pfnClose\n");
578 /* Detach from NVIDIA hardware API */
579 if (sc->hwapi->pfnClose)
580 sc->hwapi->pfnClose(sc->hwapi->pADCX, FALSE);
581 /* Release resources */
583 bus_teardown_intr(sc->dev, sc->irq, sc->sc_ih);
585 lwkt_serialize_exit(ifp->if_serializer);
591 bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
593 bus_release_resource(sc->dev, SYS_RES_MEMORY, NV_RID, sc->res);
598 bus_dmamap_unload(sc->rtag, sc->rmap);
599 bus_dmamem_free(sc->rtag, sc->rx_desc, sc->rmap);
600 bus_dmamap_destroy(sc->rtag, sc->rmap);
603 bus_dma_tag_destroy(sc->mtag);
605 bus_dma_tag_destroy(sc->ttag);
607 bus_dma_tag_destroy(sc->rtag);
609 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_detach - exit\n");
613 /* Initialise interface and start it "RUNNING" */
617 struct nv_softc *sc = xsc;
621 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init - entry (%d)\n", sc->linkup);
625 /* Do nothing if already running */
626 if (ifp->if_flags & IFF_RUNNING)
631 DEBUGOUT(NV_DEBUG_INIT, "nv: do pfnInit\n");
635 /* Setup Hardware interface and allocate memory structures */
636 error = sc->hwapi->pfnInit(sc->hwapi->pADCX,
638 0, /* force full duplex */
640 0, /* force async mode */
644 device_printf(sc->dev, "failed to start NVIDIA Hardware interface\n");
647 /* Set the MAC address */
648 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->sc_macaddr);
650 /* Setup multicast filter */
653 sc->hwapi->pfnStart(sc->hwapi->pADCX);
655 /* Update interface parameters */
656 ifp->if_flags |= IFF_RUNNING;
657 ifp->if_flags &= ~IFF_OACTIVE;
660 * Enable the interrupt. Currently the nvidia API does not support
661 * polling, if we do not call pfnEnableInterrupts() we cannot
662 * issue the nvidia callback to process interrupts. Call the
663 * interrupt service routine in case the interrupt got stuck during
664 * a reset, renegotiation, or timeout.
667 lwkt_serialize_handler_enable(ifp->if_serializer);
668 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
670 if ((ifp->if_flags & IFF_POLLING) == 0) {
671 lwkt_serialize_handler_enable(ifp->if_serializer);
672 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
678 * Reset watchdog and ring queue indexes. XXX if the interface
679 * is reset with pending tx packets queued to the actual device,
680 * the mbufs are currently lost.
685 callout_reset(&sc->nv_stat_timer, hz, nv_tick, sc);
687 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init - exit\n");
690 #ifdef DEVICE_POLLING
693 nv_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
695 struct nv_softc *sc = ifp->if_softc;
700 * We must disable the hardware interrupt on the device
701 * as well as ensure that any interrupt queued prior to
702 * this point does not execute the handler function.
704 * NOTE! The nvidia API does not support polling with
705 * interrupts disabled, so we have to leave them turned on
709 sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
710 lwkt_serialize_handler_disable(ifp->if_serializer);
713 case POLL_DEREGISTER:
715 lwkt_serialize_handler_enable(ifp->if_serializer);
716 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
719 case POLL_AND_CHECK_STATUS:
722 if (ifp->if_flags & IFF_RUNNING) {
725 if (ifp->if_flags & IFF_RUNNING) {
726 if (!ifq_is_empty(&ifp->if_snd))
735 /* Stop interface activity ie. not "RUNNING" */
737 nv_stop(struct nv_softc *sc)
741 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_stop - entry\n");
746 /* Cancel tick timer */
747 callout_stop(&sc->nv_stat_timer);
750 * Stop hardware activity. The serializer handler disablement call
751 * prevents any interrupt scheduled prior to this call from calling
754 sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
755 lwkt_serialize_handler_disable(ifp->if_serializer);
757 sc->hwapi->pfnStop(sc->hwapi->pADCX,
758 AFFECT_RECEIVER | AFFECT_TRANSMITTER);
759 sc->hwapi->pfnClearTxDesc(sc->hwapi->pADCX);
761 DEBUGOUT(NV_DEBUG_DEINIT, "nv: do pfnDeinit\n");
762 /* Shutdown interface and deallocate memory buffers */
763 if (sc->hwapi->pfnDeinit)
764 sc->hwapi->pfnDeinit(sc->hwapi->pADCX, 0);
771 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
773 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_stop - exit\n");
776 /* Shutdown interface for unload/reboot */
778 nv_shutdown(device_t dev)
782 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_shutdown\n");
784 sc = device_get_softc(dev);
786 /* Stop hardware activity */
787 lwkt_serialize_enter(sc->sc_if.if_serializer);
789 lwkt_serialize_exit(sc->sc_if.if_serializer);
792 /* Allocate TX ring buffers */
794 nv_init_rings(struct nv_softc *sc)
798 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init_rings - entry\n");
800 sc->cur_rx = sc->cur_tx = sc->pending_rxs = sc->pending_txs = 0;
801 /* Initialise RX ring */
802 for (i = 0; i < RX_RING_SIZE; i++) {
803 struct nv_rx_desc *desc = sc->rx_desc + i;
804 struct nv_map_buffer *buf = &desc->buf;
806 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
807 if (buf->mbuf == NULL) {
808 device_printf(sc->dev, "couldn't allocate mbuf\n");
813 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
814 m_adj(buf->mbuf, ETHER_ALIGN);
816 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
818 device_printf(sc->dev, "couldn't create dma map\n");
822 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
823 nv_dmamap_rx_cb, &desc->paddr, 0);
825 device_printf(sc->dev, "couldn't dma map mbuf\n");
829 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
831 desc->buflength = buf->mbuf->m_len;
832 desc->vaddr = mtod(buf->mbuf, caddr_t);
834 bus_dmamap_sync(sc->rtag, sc->rmap,
835 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
837 /* Initialize TX ring */
838 for (i = 0; i < TX_RING_SIZE; i++) {
839 struct nv_tx_desc *desc = sc->tx_desc + i;
840 struct nv_map_buffer *buf = &desc->buf;
844 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
846 device_printf(sc->dev, "couldn't create dma map\n");
851 bus_dmamap_sync(sc->ttag, sc->tmap,
852 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
854 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init_rings - exit\n");
860 /* Free the TX ring buffers */
862 nv_free_rings(struct nv_softc *sc)
866 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_free_rings - entry\n");
868 for (i = 0; i < RX_RING_SIZE; i++) {
869 struct nv_rx_desc *desc = sc->rx_desc + i;
870 struct nv_map_buffer *buf = &desc->buf;
873 bus_dmamap_unload(sc->mtag, buf->map);
874 bus_dmamap_destroy(sc->mtag, buf->map);
880 for (i = 0; i < TX_RING_SIZE; i++) {
881 struct nv_tx_desc *desc = sc->tx_desc + i;
882 struct nv_map_buffer *buf = &desc->buf;
885 bus_dmamap_unload(sc->mtag, buf->map);
886 bus_dmamap_destroy(sc->mtag, buf->map);
892 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_free_rings - exit\n");
895 /* Main loop for sending packets from OS to interface */
898 nv_ifstart(struct ifnet *ifp)
900 struct nv_softc *sc = ifp->if_softc;
901 struct nv_map_buffer *buf;
903 struct nv_tx_desc *desc;
904 ADAPTER_WRITE_DATA txdata;
907 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_ifstart - entry\n");
909 /* If link is down/busy or queue is empty do nothing */
910 if ((ifp->if_flags & IFF_OACTIVE) || ifq_is_empty(&ifp->if_snd))
913 /* Transmit queued packets until sent or TX ring is full */
914 while (sc->pending_txs < TX_RING_SIZE) {
915 desc = sc->tx_desc + sc->cur_tx;
918 /* Get next packet to send. */
919 m0 = ifq_dequeue(&ifp->if_snd, NULL);
921 /* If nothing to send, return. */
925 /* Map MBUF for DMA access */
926 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m0,
927 nv_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
929 if (error && error != EFBIG) {
935 * Packet has too many fragments - defrag into new mbuf
939 m = m_defrag(m0, MB_DONTWAIT);
947 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m,
948 nv_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
955 /* Do sync on DMA bounce buffer */
956 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREWRITE);
959 txdata.ulNumberOfElements = desc->numfrags;
960 txdata.pvID = (PVOID)desc;
962 /* Put fragments into API element list */
963 txdata.ulTotalLength = buf->mbuf->m_len;
964 for (i = 0; i < desc->numfrags; i++) {
965 txdata.sElement[i].ulLength = (ulong)desc->frags[i].ds_len;
966 txdata.sElement[i].pPhysical = (PVOID)desc->frags[i].ds_addr;
969 /* Send packet to Nvidia API for transmission */
970 error = sc->hwapi->pfnWrite(sc->hwapi->pADCX, &txdata);
973 case ADAPTERERR_NONE:
974 /* Packet was queued in API TX queue successfully */
976 sc->cur_tx = (sc->cur_tx + 1) % TX_RING_SIZE;
979 case ADAPTERERR_TRANSMIT_QUEUE_FULL:
980 /* The API TX queue is full - requeue the packet */
981 device_printf(sc->dev, "nv_ifstart: transmit queue is full\n");
982 ifp->if_flags |= IFF_OACTIVE;
983 bus_dmamap_unload(sc->mtag, buf->map);
985 m_freem(m0); /* XXX requeue */
989 /* The API failed to queue/send the packet so dump it */
990 device_printf(sc->dev, "nv_ifstart: transmit error\n");
991 bus_dmamap_unload(sc->mtag, buf->map);
997 /* Set watchdog timer. */
1000 /* Copy packet to BPF tap */
1003 ifp->if_flags |= IFF_OACTIVE;
1005 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_ifstart - exit\n");
1008 /* Handle IOCTL events */
1010 nv_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1012 struct nv_softc *sc = ifp->if_softc;
1013 struct ifreq *ifr = (struct ifreq *) data;
1014 struct mii_data *mii;
1017 DEBUGOUT(NV_DEBUG_IOCTL, "nv: nv_ioctl - entry\n");
1022 if (ifp->if_mtu == ifr->ifr_mtu)
1024 if (ifr->ifr_mtu + ifp->if_hdrlen <= MAX_PACKET_SIZE_1518) {
1025 ifp->if_mtu = ifr->ifr_mtu;
1033 /* Setup interface flags */
1034 if (ifp->if_flags & IFF_UP) {
1035 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1040 if (ifp->if_flags & IFF_RUNNING) {
1046 /* Handle IFF_PROMISC and IFF_ALLMULTI flags. */
1052 /* Setup multicast filter */
1053 if (ifp->if_flags & IFF_RUNNING) {
1059 /* Get/Set interface media parameters */
1060 mii = device_get_softc(sc->miibus);
1061 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1065 /* Everything else we forward to generic ether ioctl */
1066 error = ether_ioctl(ifp, command, data);
1070 DEBUGOUT(NV_DEBUG_IOCTL, "nv: nv_ioctl - exit\n");
1076 * Interrupt service routine. The serializer has already been entered
1077 * since we installed it in our bus_setup_intr() call.
1082 struct nv_softc *sc = arg;
1083 struct ifnet *ifp = &sc->sc_if;
1085 DEBUGOUT(NV_DEBUG_INTERRUPT, "nv: nv_intr - entry\n");
1088 * Handle an interrupt event. Unfortunately the nvidia API
1089 * does not support interrupt disablement when polling, so we
1090 * have to re-enable after the query masks them off.
1092 if (sc->hwapi->pfnQueryInterrupt(sc->hwapi->pADCX)) {
1093 sc->hwapi->pfnHandleInterrupt(sc->hwapi->pADCX);
1095 lwkt_serialize_handler_enable(ifp->if_serializer);
1096 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1098 if ((ifp->if_flags & IFF_POLLING) == 0) {
1099 lwkt_serialize_handler_enable(ifp->if_serializer);
1100 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1104 if (!ifq_is_empty(&ifp->if_snd))
1107 /* If no pending packets we don't need a timeout */
1108 if (sc->pending_txs == 0)
1109 sc->sc_if.if_timer = 0;
1111 DEBUGOUT(NV_DEBUG_INTERRUPT, "nv: nv_intr - exit\n");
1115 * Setup multicast filters
1117 * Serialized on call
1120 nv_setmulti(struct nv_softc *sc)
1123 struct ifmultiaddr *ifma;
1124 PACKET_FILTER hwfilter;
1127 u_int8_t andaddr[6];
1129 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_setmulti - entry\n");
1133 /* Initialize filter */
1134 hwfilter.ulFilterFlags = 0;
1135 for (i = 0; i < 6; i++) {
1136 hwfilter.acMulticastAddress[i] = 0;
1137 hwfilter.acMulticastMask[i] = 0;
1140 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1141 /* Accept all packets */
1142 hwfilter.ulFilterFlags |= ACCEPT_ALL_PACKETS;
1143 sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1146 /* Setup multicast filter */
1147 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1150 if (ifma->ifma_addr->sa_family != AF_LINK)
1153 addrp = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
1154 for (i = 0; i < 6; i++) {
1155 u_int8_t mcaddr = addrp[i];
1156 andaddr[i] &= mcaddr;
1157 oraddr[i] |= mcaddr;
1160 for (i = 0; i < 6; i++) {
1161 hwfilter.acMulticastAddress[i] = andaddr[i] & oraddr[i];
1162 hwfilter.acMulticastMask[i] = andaddr[i] | (~oraddr[i]);
1165 /* Send filter to NVIDIA API */
1166 sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1168 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_setmulti - exit\n");
1172 * Change the current media/mediaopts
1174 * Serialized on call
1177 nv_ifmedia_upd(struct ifnet *ifp)
1179 struct nv_softc *sc = ifp->if_softc;
1180 struct mii_data *mii;
1182 DEBUGOUT(NV_DEBUG_MII, "nv: nv_ifmedia_upd\n");
1184 mii = device_get_softc(sc->miibus);
1186 if (mii->mii_instance) {
1187 struct mii_softc *miisc;
1188 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1189 miisc = LIST_NEXT(miisc, mii_list)) {
1190 mii_phy_reset(miisc);
1199 * Update current miibus PHY status of media
1201 * Serialized on call
1204 nv_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1206 struct nv_softc *sc;
1207 struct mii_data *mii;
1209 DEBUGOUT(NV_DEBUG_MII, "nv: nv_ifmedia_sts\n");
1212 mii = device_get_softc(sc->miibus);
1215 ifmr->ifm_active = mii->mii_media_active;
1216 ifmr->ifm_status = mii->mii_media_status;
1219 /* miibus tick timer - maintain link status */
1223 struct nv_softc *sc = xsc;
1224 struct mii_data *mii;
1228 lwkt_serialize_enter(ifp->if_serializer);
1229 nv_update_stats(sc);
1231 mii = device_get_softc(sc->miibus);
1234 if ((mii->mii_media_status & IFM_ACTIVE) &&
1235 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1236 if (!ifq_is_empty(&ifp->if_snd))
1239 callout_reset(&sc->nv_stat_timer, hz, nv_tick, sc);
1240 lwkt_serialize_exit(ifp->if_serializer);
1243 /* Update ifnet data structure with collected interface stats from API */
1245 nv_update_stats(struct nv_softc *sc)
1247 struct ifnet *ifp = &sc->sc_if;
1248 ADAPTER_STATS stats;
1251 sc->hwapi->pfnGetStatistics(sc->hwapi->pADCX, &stats);
1253 ifp->if_ipackets = stats.ulSuccessfulReceptions;
1254 ifp->if_ierrors = stats.ulMissedFrames +
1255 stats.ulFailedReceptions +
1257 stats.ulFramingErrors +
1258 stats.ulOverFlowErrors;
1260 ifp->if_opackets = stats.ulSuccessfulTransmissions;
1261 ifp->if_oerrors = sc->tx_errors +
1262 stats.ulFailedTransmissions +
1263 stats.ulRetryErrors +
1264 stats.ulUnderflowErrors +
1265 stats.ulLossOfCarrierErrors +
1266 stats.ulLateCollisionErrors;
1268 ifp->if_collisions = stats.ulLateCollisionErrors;
1272 /* miibus Read PHY register wrapper - calls Nvidia API entry point */
1274 nv_miibus_readreg(device_t dev, int phy, int reg)
1276 struct nv_softc *sc = device_get_softc(dev);
1279 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_readreg - entry\n");
1281 ADAPTER_ReadPhy(sc->hwapi->pADCX, phy, reg, &data);
1283 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_readreg - exit\n");
1288 /* miibus Write PHY register wrapper - calls Nvidia API entry point */
1290 nv_miibus_writereg(device_t dev, int phy, int reg, int data)
1292 struct nv_softc *sc = device_get_softc(dev);
1294 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_writereg - entry\n");
1296 ADAPTER_WritePhy(sc->hwapi->pADCX, phy, reg, (ulong)data);
1298 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_writereg - exit\n");
1301 /* Watchdog timer to prevent PHY lockups */
1303 nv_watchdog(struct ifnet *ifp)
1305 struct nv_softc *sc = ifp->if_softc;
1307 device_printf(sc->dev, "device timeout (%d) flags %d\n",
1308 sc->pending_txs, ifp->if_flags & IFF_OACTIVE);
1313 ifp->if_flags &= ~IFF_RUNNING;
1316 if (!ifq_is_empty(&ifp->if_snd))
1320 /* --- Start of NVOSAPI interface --- */
1322 /* Allocate DMA enabled general use memory for API */
1324 nv_osalloc(PNV_VOID ctx, PMEMORY_BLOCK mem)
1326 struct nv_softc *sc;
1327 bus_addr_t mem_physical;
1329 DEBUGOUT(NV_DEBUG_API, "nv: nv_osalloc - %d\n", mem->uiLength);
1331 sc = (struct nv_softc *)ctx;
1333 mem->pLogical = (PVOID)contigmalloc(mem->uiLength, M_DEVBUF,
1334 M_NOWAIT | M_ZERO, 0, 0xffffffff, PAGE_SIZE, 0);
1336 if (!mem->pLogical) {
1337 device_printf(sc->dev, "memory allocation failed\n");
1340 memset(mem->pLogical, 0, (ulong)mem->uiLength);
1341 mem_physical = vtophys(mem->pLogical);
1342 mem->pPhysical = (PVOID)mem_physical;
1344 DEBUGOUT(NV_DEBUG_API, "nv: nv_osalloc %p/%p - %d\n",
1345 mem->pLogical, mem->pPhysical, mem->uiLength);
1350 /* Free allocated memory */
1352 nv_osfree(PNV_VOID ctx, PMEMORY_BLOCK mem)
1354 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfree - %p - %d\n",
1355 mem->pLogical, mem->uiLength);
1357 contigfree(mem->pLogical, PAGE_SIZE, M_DEVBUF);
1361 /* Copied directly from nvnet.c */
1363 nv_osallocex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1365 MEMORY_BLOCK mem_block;
1367 DEBUGOUT(NV_DEBUG_API, "nv: nv_osallocex\n");
1369 mem_block_ex->pLogical = NULL;
1370 mem_block_ex->uiLengthOrig = mem_block_ex->uiLength;
1372 if ((mem_block_ex->AllocFlags & ALLOC_MEMORY_ALIGNED) &&
1373 (mem_block_ex->AlignmentSize > 1)) {
1374 DEBUGOUT(NV_DEBUG_API, " aligning on %d\n",
1375 mem_block_ex->AlignmentSize);
1376 mem_block_ex->uiLengthOrig += mem_block_ex->AlignmentSize;
1378 mem_block.uiLength = mem_block_ex->uiLengthOrig;
1380 if (nv_osalloc(ctx, &mem_block) == 0) {
1383 mem_block_ex->pLogicalOrig = mem_block.pLogical;
1384 mem_block_ex->pPhysicalOrigLow = (uintptr_t)mem_block.pPhysical;
1385 mem_block_ex->pPhysicalOrigHigh = 0;
1387 mem_block_ex->pPhysical = mem_block.pPhysical;
1388 mem_block_ex->pLogical = mem_block.pLogical;
1390 if (mem_block_ex->uiLength != mem_block_ex->uiLengthOrig) {
1391 unsigned int offset;
1392 offset = mem_block_ex->pPhysicalOrigLow & (mem_block_ex->AlignmentSize - 1);
1395 mem_block_ex->pPhysical = (PVOID)((uintptr_t)mem_block_ex->pPhysical +
1396 mem_block_ex->AlignmentSize - offset);
1397 mem_block_ex->pLogical = (PVOID)((uintptr_t)mem_block_ex->pLogical +
1398 mem_block_ex->AlignmentSize - offset);
1400 } /* if (mem_block_ex->uiLength !=
1401 * mem_block_ex->uiLengthOrig) */
1405 /* Copied directly from nvnet.c */
1407 nv_osfreeex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1409 MEMORY_BLOCK mem_block;
1411 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfreeex\n");
1413 mem_block.pLogical = mem_block_ex->pLogicalOrig;
1414 mem_block.pPhysical = (PVOID)((uintptr_t)mem_block_ex->pPhysicalOrigLow);
1415 mem_block.uiLength = mem_block_ex->uiLengthOrig;
1417 return (nv_osfree(ctx, &mem_block));
1420 /* Clear memory region */
1422 nv_osclear(PNV_VOID ctx, PNV_VOID mem, NV_SINT32 length)
1424 DEBUGOUT(NV_DEBUG_API, "nv: nv_osclear\n");
1425 memset(mem, 0, length);
1429 /* Sleep for a tick */
1431 nv_osdelay(PNV_VOID ctx, NV_UINT32 usec)
1433 if (usec >= 1000000 / hz) {
1434 tsleep(nv_osdelay, 0, "nvdelay", (usec * hz / 1000000) + 1);
1441 /* Allocate memory for rx buffer */
1443 nv_osallocrxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID *id)
1445 struct nv_softc *sc = ctx;
1446 struct nv_rx_desc *desc;
1447 struct nv_map_buffer *buf;
1450 DEBUGOUT(NV_DEBUG_API, "nv: nv_osallocrxbuf\n");
1452 if (sc->pending_rxs == RX_RING_SIZE) {
1453 device_printf(sc->dev, "rx ring buffer is full\n");
1456 desc = sc->rx_desc + sc->cur_rx;
1459 if (buf->mbuf == NULL) {
1460 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1461 if (buf->mbuf == NULL) {
1462 device_printf(sc->dev, "failed to allocate memory\n");
1465 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
1466 m_adj(buf->mbuf, ETHER_ALIGN);
1468 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
1469 nv_dmamap_rx_cb, &desc->paddr, 0);
1471 device_printf(sc->dev, "failed to dmamap mbuf\n");
1476 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
1477 desc->buflength = buf->mbuf->m_len;
1478 desc->vaddr = mtod(buf->mbuf, PVOID);
1481 sc->cur_rx = (sc->cur_rx + 1) % RX_RING_SIZE;
1483 mem->pLogical = (void *)desc->vaddr;
1484 mem->pPhysical = (void *)desc->paddr;
1485 mem->uiLength = desc->buflength;
1494 /* Free the rx buffer */
1496 nv_osfreerxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID id)
1498 struct nv_softc *sc = ctx;
1499 struct nv_rx_desc *desc;
1500 struct nv_map_buffer *buf;
1502 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfreerxbuf\n");
1504 desc = (struct nv_rx_desc *) id;
1508 bus_dmamap_unload(sc->mtag, buf->map);
1509 bus_dmamap_destroy(sc->mtag, buf->map);
1518 /* This gets called by the Nvidia API after our TX packet has been sent */
1520 nv_ospackettx(PNV_VOID ctx, PNV_VOID id, NV_UINT32 success)
1522 struct nv_softc *sc = ctx;
1523 struct nv_map_buffer *buf;
1524 struct nv_tx_desc *desc = (struct nv_tx_desc *) id;
1527 DEBUGOUT(NV_DEBUG_API, "nv: nv_ospackettx\n");
1533 /* Unload and free mbuf cluster */
1534 if (buf->mbuf == NULL)
1537 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTWRITE);
1538 bus_dmamap_unload(sc->mtag, buf->map);
1543 * Make sure we are clear to go if we previously stalled due
1546 if (sc->pending_txs < TX_RING_SIZE) {
1547 ifp->if_flags &= ~IFF_OACTIVE;
1548 if (!ifq_is_empty(&ifp->if_snd))
1555 /* This gets called by the Nvidia API when a new packet has been received */
1556 /* XXX What is newbuf used for? XXX */
1558 nv_ospacketrx(PNV_VOID ctx, PNV_VOID data, NV_UINT32 success,
1559 NV_UINT8 *newbuf, NV_UINT8 priority)
1561 struct nv_softc *sc = ctx;
1563 struct nv_rx_desc *desc;
1564 struct nv_map_buffer *buf;
1565 ADAPTER_READ_DATA *readdata;
1567 DEBUGOUT(NV_DEBUG_API, "nv: nv_ospacketrx\n");
1571 readdata = (ADAPTER_READ_DATA *) data;
1572 desc = readdata->pvID;
1574 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1577 /* Sync DMA bounce buffer. */
1578 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1580 /* First mbuf in packet holds the ethernet and packet headers */
1581 buf->mbuf->m_pkthdr.rcvif = ifp;
1582 buf->mbuf->m_pkthdr.len = buf->mbuf->m_len = readdata->ulTotalLength;
1584 bus_dmamap_unload(sc->mtag, buf->map);
1586 /* Give mbuf to OS. */
1587 ifp->if_input(ifp, buf->mbuf);
1588 if (readdata->ulFilterMatch & ADREADFL_MULTICAST_MATCH)
1591 /* Blat the mbuf pointer, kernel will free the mbuf cluster */
1594 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1595 bus_dmamap_unload(sc->mtag, buf->map);
1600 sc->cur_rx = desc - sc->rx_desc;
1606 /* This gets called by NVIDIA API when the PHY link state changes */
1608 nv_oslinkchg(PNV_VOID ctx, NV_SINT32 enabled)
1610 DEBUGOUT(NV_DEBUG_API, "nv: nv_oslinkchg\n");
1616 /* Setup a watchdog timer */
1618 nv_osalloctimer(PNV_VOID ctx, PNV_VOID *timer)
1620 struct nv_softc *sc = (struct nv_softc *)ctx;
1622 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osalloctimer\n");
1624 callout_init(&sc->ostimer);
1625 *timer = &sc->ostimer;
1630 /* Free the timer */
1632 nv_osfreetimer(PNV_VOID ctx, PNV_VOID timer)
1634 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osfreetimer\n");
1639 /* Setup timer parameters */
1641 nv_osinittimer(PNV_VOID ctx, PNV_VOID timer, PTIMER_FUNC func, PNV_VOID parameters)
1643 struct nv_softc *sc = (struct nv_softc *)ctx;
1645 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osinittimer\n");
1647 sc->ostimer_func = func;
1648 sc->ostimer_params = parameters;
1654 nv_ostimer_callback(void *data)
1656 struct nv_softc *sc = data;
1657 struct ifnet *ifp = &sc->sc_if;
1659 lwkt_serialize_enter(ifp->if_serializer);
1660 sc->ostimer_func(sc->ostimer_params);
1661 lwkt_serialize_exit(ifp->if_serializer);
1665 * Set the timer to go off
1667 * XXX what the hell are the units for 'delay' ? They sure aren't ticks!
1670 nv_ossettimer(PNV_VOID ctx, PNV_VOID timer, NV_UINT32 delay)
1672 struct nv_softc *sc = ctx;
1674 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ossettimer\n");
1675 kprintf("nv_ossettimer %d\n", (int)delay);
1677 callout_reset(&sc->ostimer, delay, nv_ostimer_callback, sc);
1682 /* Cancel the timer */
1684 nv_oscanceltimer(PNV_VOID ctx, PNV_VOID timer)
1686 struct nv_softc *sc = ctx;
1688 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_oscanceltimer\n");
1690 callout_stop(&sc->ostimer);
1696 nv_ospreprocpkt(PNV_VOID ctx, PNV_VOID readdata, PNV_VOID *id, NV_UINT8 *newbuffer,
1699 /* Not implemented */
1700 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ospreprocpkt\n");
1706 nv_ospreprocpktnopq(PNV_VOID ctx, PNV_VOID readdata)
1708 /* Not implemented */
1709 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ospreprocpkt\n");
1715 nv_osindicatepkt(PNV_VOID ctx, PNV_VOID *id, NV_UINT32 pktno)
1717 /* Not implemented */
1718 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osindicatepkt\n");
1723 /* Allocate mutex context (already done in nv_attach) */
1725 nv_oslockalloc(PNV_VOID ctx, NV_SINT32 type, PNV_VOID *pLock)
1727 struct nv_softc *sc = (struct nv_softc *)ctx;
1729 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockalloc\n");
1731 *pLock = (void **)sc;
1736 /* Obtain a spin lock */
1738 nv_oslockacquire(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1740 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockacquire\n");
1747 nv_oslockrelease(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1749 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockrelease\n");
1754 /* I have no idea what this is for */
1756 nv_osreturnbufvirt(PNV_VOID ctx, PNV_VOID readdata)
1758 /* Not implemented */
1759 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_osreturnbufvirt\n");
1760 panic("nv: nv_osreturnbufvirtual not implemented\n");
1766 /* --- End on NVOSAPI interface --- */