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.22 2005/11/22 00:24:33 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_init_serialized(struct nv_softc *);
130 static void nv_stop(struct nv_softc *);
131 static void nv_stop_serialized(struct nv_softc *);
132 static void nv_shutdown(device_t);
133 static int nv_init_rings(struct nv_softc *);
134 static void nv_free_rings(struct nv_softc *);
136 static void nv_ifstart(struct ifnet *);
137 static void nv_ifstart_serialized(struct ifnet *);
138 static int nv_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
139 static void nv_intr(void *);
140 static void nv_tick(void *);
141 static void nv_setmulti(struct nv_softc *);
142 static void nv_watchdog(struct ifnet *);
143 static void nv_update_stats(struct nv_softc *);
144 #ifdef DEVICE_POLLING
145 static void nv_poll(struct ifnet *, enum poll_cmd, int);
148 static int nv_ifmedia_upd(struct ifnet *);
149 static void nv_ifmedia_sts(struct ifnet *, struct ifmediareq *);
150 static int nv_miibus_readreg(device_t, int, int);
151 static void nv_miibus_writereg(device_t, int, int, int);
153 static void nv_dmamap_cb(void *, bus_dma_segment_t *, int, int);
154 static void nv_dmamap_tx_cb(void *, bus_dma_segment_t *, int, bus_size_t, int);
156 static NV_SINT32 nv_osalloc(PNV_VOID, PMEMORY_BLOCK);
157 static NV_SINT32 nv_osfree(PNV_VOID, PMEMORY_BLOCK);
158 static NV_SINT32 nv_osallocex(PNV_VOID, PMEMORY_BLOCKEX);
159 static NV_SINT32 nv_osfreeex(PNV_VOID, PMEMORY_BLOCKEX);
160 static NV_SINT32 nv_osclear(PNV_VOID, PNV_VOID, NV_SINT32);
161 static NV_SINT32 nv_osdelay(PNV_VOID, NV_UINT32);
162 static NV_SINT32 nv_osallocrxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID *);
163 static NV_SINT32 nv_osfreerxbuf(PNV_VOID, PMEMORY_BLOCK, PNV_VOID);
164 static NV_SINT32 nv_ospackettx(PNV_VOID, PNV_VOID, NV_UINT32);
165 static NV_SINT32 nv_ospacketrx(PNV_VOID, PNV_VOID, NV_UINT32, NV_UINT8 *, NV_UINT8);
166 static NV_SINT32 nv_oslinkchg(PNV_VOID, NV_SINT32);
167 static NV_SINT32 nv_osalloctimer(PNV_VOID, PNV_VOID *);
168 static NV_SINT32 nv_osfreetimer(PNV_VOID, PNV_VOID);
169 static NV_SINT32 nv_osinittimer(PNV_VOID, PNV_VOID, PTIMER_FUNC, PNV_VOID);
170 static NV_SINT32 nv_ossettimer(PNV_VOID, PNV_VOID, NV_UINT32);
171 static NV_SINT32 nv_oscanceltimer(PNV_VOID, PNV_VOID);
173 static NV_SINT32 nv_ospreprocpkt(PNV_VOID, PNV_VOID, PNV_VOID *, NV_UINT8 *, NV_UINT8);
174 static PNV_VOID nv_ospreprocpktnopq(PNV_VOID, PNV_VOID);
175 static NV_SINT32 nv_osindicatepkt(PNV_VOID, PNV_VOID *, NV_UINT32);
176 static NV_SINT32 nv_oslockalloc(PNV_VOID, NV_SINT32, PNV_VOID *);
177 static NV_SINT32 nv_oslockacquire(PNV_VOID, NV_SINT32, PNV_VOID);
178 static NV_SINT32 nv_oslockrelease(PNV_VOID, NV_SINT32, PNV_VOID);
179 static PNV_VOID nv_osreturnbufvirt(PNV_VOID, PNV_VOID);
181 static device_method_t nv_methods[] = {
182 /* Device interface */
183 DEVMETHOD(device_probe, nv_probe),
184 DEVMETHOD(device_attach, nv_attach),
185 DEVMETHOD(device_detach, nv_detach),
186 DEVMETHOD(device_shutdown, nv_shutdown),
189 DEVMETHOD(bus_print_child, bus_generic_print_child),
190 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
193 DEVMETHOD(miibus_readreg, nv_miibus_readreg),
194 DEVMETHOD(miibus_writereg, nv_miibus_writereg),
199 static driver_t nv_driver = {
202 sizeof(struct nv_softc)
205 static devclass_t nv_devclass;
207 static int nv_pollinterval = 0;
208 SYSCTL_INT(_hw, OID_AUTO, nv_pollinterval, CTLFLAG_RW,
209 &nv_pollinterval, 0, "delay between interface polls");
211 DRIVER_MODULE(nv, pci, nv_driver, nv_devclass, 0, 0);
212 DRIVER_MODULE(miibus, nv, miibus_driver, miibus_devclass, 0, 0);
214 static struct nv_type nv_devs[] = {
215 {NVIDIA_VENDORID, NFORCE_MCPNET1_DEVICEID,
216 "NVIDIA nForce MCP Networking Adapter"},
217 {NVIDIA_VENDORID, NFORCE_MCPNET2_DEVICEID,
218 "NVIDIA nForce MCP2 Networking Adapter"},
219 {NVIDIA_VENDORID, NFORCE_MCPNET3_DEVICEID,
220 "NVIDIA nForce MCP3 Networking Adapter"},
221 {NVIDIA_VENDORID, NFORCE_MCPNET4_DEVICEID,
222 "NVIDIA nForce MCP4 Networking Adapter"},
223 {NVIDIA_VENDORID, NFORCE_MCPNET5_DEVICEID,
224 "NVIDIA nForce MCP5 Networking Adapter"},
225 {NVIDIA_VENDORID, NFORCE_MCPNET6_DEVICEID,
226 "NVIDIA nForce MCP6 Networking Adapter"},
227 {NVIDIA_VENDORID, NFORCE_MCPNET7_DEVICEID,
228 "NVIDIA nForce MCP7 Networking Adapter"},
229 {NVIDIA_VENDORID, NFORCE_MCPNET8_DEVICEID,
230 "NVIDIA nForce MCP8 Networking Adapter"},
231 {NVIDIA_VENDORID, NFORCE_MCPNET9_DEVICEID,
232 "NVIDIA nForce MCP9 Networking Adapter"},
233 {NVIDIA_VENDORID, NFORCE_MCPNET10_DEVICEID,
234 "NVIDIA nForce MCP10 Networking Adapter"},
235 {NVIDIA_VENDORID, NFORCE_MCPNET11_DEVICEID,
236 "NVIDIA nForce MCP11 Networking Adapter"},
240 /* DMA MEM map callback function to get data segment physical address */
242 nv_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nsegs, int error)
248 ("Too many DMA segments returned when mapping DMA memory"));
249 *(bus_addr_t *)arg = segs->ds_addr;
252 /* DMA RX map callback function to get data segment physical address */
254 nv_dmamap_rx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
258 *(bus_addr_t *)arg = segs->ds_addr;
262 * DMA TX buffer callback function to allocate fragment data segment
266 nv_dmamap_tx_cb(void *arg, bus_dma_segment_t * segs, int nsegs, bus_size_t mapsize, int error)
268 struct nv_tx_desc *info = arg;
272 KASSERT(nsegs < NV_MAX_FRAGS,
273 ("Too many DMA segments returned when mapping mbuf"));
274 info->numfrags = nsegs;
275 bcopy(segs, info->frags, nsegs * sizeof(bus_dma_segment_t));
278 /* Probe for supported hardware ID's */
280 nv_probe(device_t dev)
282 struct nv_type *t = nv_devs;
284 /* Check for matching PCI DEVICE ID's */
285 while (t->name != NULL) {
286 if ((pci_get_vendor(dev) == t->vid_id) &&
287 (pci_get_device(dev) == t->dev_id)) {
288 device_set_desc(dev, t->name);
297 /* Attach driver and initialise hardware for use */
299 nv_attach(device_t dev)
301 u_char eaddr[ETHER_ADDR_LEN];
305 ADAPTER_OPEN_PARAMS OpenParams;
306 int error = 0, i, rid;
309 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_attach - entry\n");
311 sc = device_get_softc(dev);
312 unit = device_get_unit(dev);
316 lwkt_serialize_init(&sc->serializer);
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);
534 /* Activate our interrupt handler. - attach last to avoid lock */
535 error = bus_setup_intr(sc->dev, sc->irq, 0,
536 nv_intr, sc, &sc->sc_ih, &sc->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);
558 lwkt_serialize_enter(&sc->serializer);
560 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_detach - entry\n");
562 ifp = &sc->arpcom.ac_if;
564 if (device_is_attached(dev)) {
565 nv_stop_serialized(sc);
566 lwkt_serialize_exit(&sc->serializer);
568 lwkt_serialize_enter(&sc->serializer);
572 device_delete_child(dev, sc->miibus);
573 bus_generic_detach(dev);
575 /* Reload unreversed address back into MAC in original state */
576 if (sc->original_mac_addr)
577 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->original_mac_addr);
579 DEBUGOUT(NV_DEBUG_DEINIT, "nv: do pfnClose\n");
580 /* Detach from NVIDIA hardware API */
581 if (sc->hwapi->pfnClose)
582 sc->hwapi->pfnClose(sc->hwapi->pADCX, FALSE);
583 /* Release resources */
585 bus_teardown_intr(sc->dev, sc->irq, sc->sc_ih);
587 bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
589 bus_release_resource(sc->dev, SYS_RES_MEMORY, NV_RID, sc->res);
594 bus_dmamap_unload(sc->rtag, sc->rmap);
595 bus_dmamem_free(sc->rtag, sc->rx_desc, sc->rmap);
596 bus_dmamap_destroy(sc->rtag, sc->rmap);
599 bus_dma_tag_destroy(sc->mtag);
601 bus_dma_tag_destroy(sc->ttag);
603 bus_dma_tag_destroy(sc->rtag);
605 lwkt_serialize_exit(&sc->serializer);
607 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_detach - exit\n");
615 struct nv_softc *sc = xsc;
617 lwkt_serialize_enter(&sc->serializer);
618 nv_init_serialized(sc);
619 lwkt_serialize_exit(&sc->serializer);
622 /* Initialise interface and start it "RUNNING" */
624 nv_init_serialized(struct nv_softc *sc)
629 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init - entry (%d)\n", sc->linkup);
633 /* Do nothing if already running */
634 if (ifp->if_flags & IFF_RUNNING)
637 nv_stop_serialized(sc);
639 DEBUGOUT(NV_DEBUG_INIT, "nv: do pfnInit\n");
640 /* Setup Hardware interface and allocate memory structures */
641 error = sc->hwapi->pfnInit(sc->hwapi->pADCX,
643 0, /* force full duplex */
645 0, /* force async mode */
649 device_printf(sc->dev, "failed to start NVIDIA Hardware interface\n");
652 /* Set the MAC address */
653 sc->hwapi->pfnSetNodeAddress(sc->hwapi->pADCX, sc->sc_macaddr);
655 sc->hwapi->pfnStart(sc->hwapi->pADCX);
657 /* Setup multicast filter */
661 /* Update interface parameters */
662 ifp->if_flags |= IFF_RUNNING;
663 ifp->if_flags &= ~IFF_OACTIVE;
666 * Enable the interrupt. Currently the nvidia API does not support
667 * polling, if we do not call pfnEnableInterrupts() we cannot
668 * issue the nvidia callback to process interrupts. Call the
669 * interrupt service routine in case the interrupt got stuck during
670 * a reset, renegotiation, or timeout.
673 lwkt_serialize_handler_enable(&sc->serializer);
674 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
676 if ((ifp->if_flags & IFF_POLLING) == 0) {
677 lwkt_serialize_handler_enable(&sc->serializer);
678 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
684 * Reset watchdog and ring queue indexes. XXX if the interface
685 * is reset with pending tx packets queued to the actual device,
686 * the mbufs are currently lost.
691 callout_reset(&sc->nv_stat_timer, hz, nv_tick, sc);
693 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init - exit\n");
696 #ifdef DEVICE_POLLING
699 nv_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
701 struct nv_softc *sc = ifp->if_softc;
703 lwkt_serialize_enter(&sc->serializer);
708 * We must disable the hardware interrupt on the device
709 * as well as ensure that any interrupt queued prior to
710 * this point does not execute the handler function.
712 * NOTE! The nvidia API does not support polling with
713 * interrupts disabled, so we have to leave them turned on
717 sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
718 lwkt_serialize_handler_disable(&sc->serializer);
721 case POLL_DEREGISTER:
723 lwkt_serialize_handler_enable(&sc->serializer);
724 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
727 case POLL_AND_CHECK_STATUS:
730 if (ifp->if_flags & IFF_RUNNING) {
733 if (ifp->if_flags & IFF_RUNNING) {
734 if (!ifq_is_empty(&ifp->if_snd))
735 nv_ifstart_serialized(ifp);
739 lwkt_serialize_exit(&sc->serializer);
745 nv_stop(struct nv_softc *sc)
747 lwkt_serialize_enter(&sc->serializer);
748 nv_stop_serialized(sc);
749 lwkt_serialize_exit(&sc->serializer);
752 /* Stop interface activity ie. not "RUNNING" */
754 nv_stop_serialized(struct nv_softc *sc)
758 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_stop - entry\n");
763 /* Cancel tick timer */
764 callout_stop(&sc->nv_stat_timer);
767 * Stop hardware activity. The serializer handler disablement call
768 * prevents any interrupt scheduled prior to this call from calling
771 sc->hwapi->pfnDisableInterrupts(sc->hwapi->pADCX);
772 lwkt_serialize_handler_disable(&sc->serializer);
774 sc->hwapi->pfnStop(sc->hwapi->pADCX, 0);
775 sc->hwapi->pfnClearTxDesc(sc->hwapi->pADCX);
777 DEBUGOUT(NV_DEBUG_DEINIT, "nv: do pfnDeinit\n");
778 /* Shutdown interface and deallocate memory buffers */
779 if (sc->hwapi->pfnDeinit)
780 sc->hwapi->pfnDeinit(sc->hwapi->pADCX, 0);
786 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
788 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_stop - exit\n");
791 /* Shutdown interface for unload/reboot */
793 nv_shutdown(device_t dev)
797 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_shutdown\n");
799 sc = device_get_softc(dev);
801 /* Stop hardware activity */
805 /* Allocate TX ring buffers */
807 nv_init_rings(struct nv_softc *sc)
811 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init_rings - entry\n");
813 sc->cur_rx = sc->cur_tx = sc->pending_rxs = sc->pending_txs = 0;
814 /* Initialise RX ring */
815 for (i = 0; i < RX_RING_SIZE; i++) {
816 struct nv_rx_desc *desc = sc->rx_desc + i;
817 struct nv_map_buffer *buf = &desc->buf;
819 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
820 if (buf->mbuf == NULL) {
821 device_printf(sc->dev, "couldn't allocate mbuf\n");
826 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
827 m_adj(buf->mbuf, ETHER_ALIGN);
829 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
831 device_printf(sc->dev, "couldn't create dma map\n");
835 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
836 nv_dmamap_rx_cb, &desc->paddr, 0);
838 device_printf(sc->dev, "couldn't dma map mbuf\n");
842 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
844 desc->buflength = buf->mbuf->m_len;
845 desc->vaddr = mtod(buf->mbuf, caddr_t);
847 bus_dmamap_sync(sc->rtag, sc->rmap,
848 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
850 /* Initialize TX ring */
851 for (i = 0; i < TX_RING_SIZE; i++) {
852 struct nv_tx_desc *desc = sc->tx_desc + i;
853 struct nv_map_buffer *buf = &desc->buf;
857 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
859 device_printf(sc->dev, "couldn't create dma map\n");
864 bus_dmamap_sync(sc->ttag, sc->tmap,
865 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
867 DEBUGOUT(NV_DEBUG_INIT, "nv: nv_init_rings - exit\n");
873 /* Free the TX ring buffers */
875 nv_free_rings(struct nv_softc *sc)
879 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_free_rings - entry\n");
881 for (i = 0; i < RX_RING_SIZE; i++) {
882 struct nv_rx_desc *desc = sc->rx_desc + i;
883 struct nv_map_buffer *buf = &desc->buf;
886 bus_dmamap_unload(sc->mtag, buf->map);
887 bus_dmamap_destroy(sc->mtag, buf->map);
893 for (i = 0; i < TX_RING_SIZE; i++) {
894 struct nv_tx_desc *desc = sc->tx_desc + i;
895 struct nv_map_buffer *buf = &desc->buf;
898 bus_dmamap_unload(sc->mtag, buf->map);
899 bus_dmamap_destroy(sc->mtag, buf->map);
905 DEBUGOUT(NV_DEBUG_DEINIT, "nv: nv_free_rings - exit\n");
908 /* Main loop for sending packets from OS to interface */
911 nv_ifstart(struct ifnet *ifp)
913 struct nv_softc *sc = ifp->if_softc;
915 lwkt_serialize_enter(&sc->serializer);
916 nv_ifstart_serialized(ifp);
917 lwkt_serialize_exit(&sc->serializer);
921 nv_ifstart_serialized(struct ifnet *ifp)
923 struct nv_softc *sc = ifp->if_softc;
924 struct nv_map_buffer *buf;
926 struct nv_tx_desc *desc;
927 ADAPTER_WRITE_DATA txdata;
930 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_ifstart - entry\n");
932 /* If link is down/busy or queue is empty do nothing */
933 if ((ifp->if_flags & IFF_OACTIVE) || ifq_is_empty(&ifp->if_snd))
936 /* Transmit queued packets until sent or TX ring is full */
937 while (sc->pending_txs < TX_RING_SIZE) {
938 desc = sc->tx_desc + sc->cur_tx;
941 /* Get next packet to send. */
942 m0 = ifq_dequeue(&ifp->if_snd, NULL);
944 /* If nothing to send, return. */
948 /* Map MBUF for DMA access */
949 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m0,
950 nv_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
952 if (error && error != EFBIG) {
958 * Packet has too many fragments - defrag into new mbuf
962 m = m_defrag(m0, MB_DONTWAIT);
970 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m,
971 nv_dmamap_tx_cb, desc, BUS_DMA_NOWAIT);
978 /* Do sync on DMA bounce buffer */
979 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREWRITE);
982 txdata.ulNumberOfElements = desc->numfrags;
983 txdata.pvID = (PVOID)desc;
985 /* Put fragments into API element list */
986 txdata.ulTotalLength = buf->mbuf->m_len;
987 for (i = 0; i < desc->numfrags; i++) {
988 txdata.sElement[i].ulLength = (ulong)desc->frags[i].ds_len;
989 txdata.sElement[i].pPhysical = (PVOID)desc->frags[i].ds_addr;
992 /* Send packet to Nvidia API for transmission */
993 error = sc->hwapi->pfnWrite(sc->hwapi->pADCX, &txdata);
996 case ADAPTERERR_NONE:
997 /* Packet was queued in API TX queue successfully */
999 sc->cur_tx = (sc->cur_tx + 1) % TX_RING_SIZE;
1002 case ADAPTERERR_TRANSMIT_QUEUE_FULL:
1003 /* The API TX queue is full - requeue the packet */
1004 device_printf(sc->dev, "nv_ifstart: transmit queue is full\n");
1005 ifp->if_flags |= IFF_OACTIVE;
1006 bus_dmamap_unload(sc->mtag, buf->map);
1008 m_freem(m0); /* XXX requeue */
1012 /* The API failed to queue/send the packet so dump it */
1013 device_printf(sc->dev, "nv_ifstart: transmit error\n");
1014 bus_dmamap_unload(sc->mtag, buf->map);
1020 /* Set watchdog timer. */
1023 /* Copy packet to BPF tap */
1026 ifp->if_flags |= IFF_OACTIVE;
1028 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_ifstart - exit\n");
1031 /* Handle IOCTL events */
1033 nv_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1035 struct nv_softc *sc = ifp->if_softc;
1036 struct ifreq *ifr = (struct ifreq *) data;
1037 struct mii_data *mii;
1040 lwkt_serialize_enter(&sc->serializer);
1042 DEBUGOUT(NV_DEBUG_IOCTL, "nv: nv_ioctl - entry\n");
1047 if (ifp->if_mtu == ifr->ifr_mtu)
1049 if (ifr->ifr_mtu + ifp->if_hdrlen <= MAX_PACKET_SIZE_1518) {
1050 ifp->if_mtu = ifr->ifr_mtu;
1051 nv_stop_serialized(sc);
1052 nv_init_serialized(sc);
1058 /* Setup interface flags */
1059 if (ifp->if_flags & IFF_UP) {
1060 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1061 nv_init_serialized(sc);
1065 if (ifp->if_flags & IFF_RUNNING) {
1066 nv_stop_serialized(sc);
1071 /* Handle IFF_PROMISC and IFF_ALLMULTI flags. */
1077 /* Setup multicast filter */
1078 if (ifp->if_flags & IFF_RUNNING) {
1084 /* Get/Set interface media parameters */
1085 mii = device_get_softc(sc->miibus);
1086 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1090 /* Everything else we forward to generic ether ioctl */
1091 lwkt_serialize_exit(&sc->serializer);
1092 error = ether_ioctl(ifp, command, data);
1093 lwkt_serialize_enter(&sc->serializer);
1097 DEBUGOUT(NV_DEBUG_IOCTL, "nv: nv_ioctl - exit\n");
1099 lwkt_serialize_exit(&sc->serializer);
1105 * Interrupt service routine. The serializer has already been entered
1106 * since we installed it in our bus_setup_intr() call.
1111 struct nv_softc *sc = arg;
1112 struct ifnet *ifp = &sc->sc_if;
1114 DEBUGOUT(NV_DEBUG_INTERRUPT, "nv: nv_intr - entry\n");
1117 * Handle an interrupt event. Unfortunately the nvidia API
1118 * does not support interrupt disablement when polling, so we
1119 * have to re-enable after the query masks them off.
1121 if (sc->hwapi->pfnQueryInterrupt(sc->hwapi->pADCX)) {
1122 sc->hwapi->pfnHandleInterrupt(sc->hwapi->pADCX);
1124 lwkt_serialize_handler_enable(&sc->serializer);
1125 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1127 if ((ifp->if_flags & IFF_POLLING) == 0) {
1128 lwkt_serialize_handler_enable(&sc->serializer);
1129 sc->hwapi->pfnEnableInterrupts(sc->hwapi->pADCX);
1133 if (!ifq_is_empty(&ifp->if_snd))
1134 nv_ifstart_serialized(ifp);
1136 /* If no pending packets we don't need a timeout */
1137 if (sc->pending_txs == 0)
1138 sc->sc_if.if_timer = 0;
1140 DEBUGOUT(NV_DEBUG_INTERRUPT, "nv: nv_intr - exit\n");
1144 * Setup multicast filters
1146 * Serialized on call
1149 nv_setmulti(struct nv_softc *sc)
1152 struct ifmultiaddr *ifma;
1153 PACKET_FILTER hwfilter;
1156 u_int8_t andaddr[6];
1158 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_setmulti - entry\n");
1162 /* Initialize filter */
1163 hwfilter.ulFilterFlags = 0;
1164 for (i = 0; i < 6; i++) {
1165 hwfilter.acMulticastAddress[i] = 0;
1166 hwfilter.acMulticastMask[i] = 0;
1169 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1170 /* Accept all packets */
1171 hwfilter.ulFilterFlags |= ACCEPT_ALL_PACKETS;
1172 sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1175 /* Setup multicast filter */
1176 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1179 if (ifma->ifma_addr->sa_family != AF_LINK)
1182 addrp = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
1183 for (i = 0; i < 6; i++) {
1184 u_int8_t mcaddr = addrp[i];
1185 andaddr[i] &= mcaddr;
1186 oraddr[i] |= mcaddr;
1189 for (i = 0; i < 6; i++) {
1190 hwfilter.acMulticastAddress[i] = andaddr[i] & oraddr[i];
1191 hwfilter.acMulticastMask[i] = andaddr[i] | (~oraddr[i]);
1194 /* Send filter to NVIDIA API */
1195 sc->hwapi->pfnSetPacketFilter(sc->hwapi->pADCX, &hwfilter);
1197 DEBUGOUT(NV_DEBUG_RUNNING, "nv: nv_setmulti - exit\n");
1201 * Change the current media/mediaopts
1203 * Serialized on call
1206 nv_ifmedia_upd(struct ifnet *ifp)
1208 struct nv_softc *sc = ifp->if_softc;
1209 struct mii_data *mii;
1211 DEBUGOUT(NV_DEBUG_MII, "nv: nv_ifmedia_upd\n");
1213 mii = device_get_softc(sc->miibus);
1215 if (mii->mii_instance) {
1216 struct mii_softc *miisc;
1217 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1218 miisc = LIST_NEXT(miisc, mii_list)) {
1219 mii_phy_reset(miisc);
1228 * Update current miibus PHY status of media
1230 * Serialized on call
1233 nv_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1235 struct nv_softc *sc;
1236 struct mii_data *mii;
1238 DEBUGOUT(NV_DEBUG_MII, "nv: nv_ifmedia_sts\n");
1241 mii = device_get_softc(sc->miibus);
1244 ifmr->ifm_active = mii->mii_media_active;
1245 ifmr->ifm_status = mii->mii_media_status;
1248 /* miibus tick timer - maintain link status */
1252 struct nv_softc *sc = xsc;
1253 struct mii_data *mii;
1256 lwkt_serialize_enter(&sc->serializer);
1259 nv_update_stats(sc);
1261 mii = device_get_softc(sc->miibus);
1264 if ((mii->mii_media_status & IFM_ACTIVE) &&
1265 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1266 if (!ifq_is_empty(&ifp->if_snd))
1267 nv_ifstart_serialized(ifp);
1269 callout_reset(&sc->nv_stat_timer, hz, nv_tick, sc);
1270 lwkt_serialize_exit(&sc->serializer);
1273 /* Update ifnet data structure with collected interface stats from API */
1275 nv_update_stats(struct nv_softc *sc)
1277 struct ifnet *ifp = &sc->sc_if;
1278 ADAPTER_STATS stats;
1281 sc->hwapi->pfnGetStatistics(sc->hwapi->pADCX, &stats);
1283 ifp->if_ipackets = stats.ulSuccessfulReceptions;
1284 ifp->if_ierrors = stats.ulMissedFrames +
1285 stats.ulFailedReceptions +
1287 stats.ulFramingErrors +
1288 stats.ulOverFlowErrors;
1290 ifp->if_opackets = stats.ulSuccessfulTransmissions;
1291 ifp->if_oerrors = sc->tx_errors +
1292 stats.ulFailedTransmissions +
1293 stats.ulRetryErrors +
1294 stats.ulUnderflowErrors +
1295 stats.ulLossOfCarrierErrors +
1296 stats.ulLateCollisionErrors;
1298 ifp->if_collisions = stats.ulLateCollisionErrors;
1302 /* miibus Read PHY register wrapper - calls Nvidia API entry point */
1304 nv_miibus_readreg(device_t dev, int phy, int reg)
1306 struct nv_softc *sc = device_get_softc(dev);
1309 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_readreg - entry\n");
1311 ADAPTER_ReadPhy(sc->hwapi->pADCX, phy, reg, &data);
1313 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_readreg - exit\n");
1318 /* miibus Write PHY register wrapper - calls Nvidia API entry point */
1320 nv_miibus_writereg(device_t dev, int phy, int reg, int data)
1322 struct nv_softc *sc = device_get_softc(dev);
1324 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_writereg - entry\n");
1326 ADAPTER_WritePhy(sc->hwapi->pADCX, phy, reg, (ulong)data);
1328 DEBUGOUT(NV_DEBUG_MII, "nv: nv_miibus_writereg - exit\n");
1331 /* Watchdog timer to prevent PHY lockups */
1333 nv_watchdog(struct ifnet *ifp)
1335 struct nv_softc *sc = ifp->if_softc;
1337 lwkt_serialize_enter(&sc->serializer);
1339 device_printf(sc->dev, "device timeout (%d) flags %d\n",
1340 sc->pending_txs, ifp->if_flags & IFF_OACTIVE);
1344 nv_stop_serialized(sc);
1345 ifp->if_flags &= ~IFF_RUNNING;
1346 nv_init_serialized(sc);
1348 if (!ifq_is_empty(&ifp->if_snd))
1349 nv_ifstart_serialized(ifp);
1351 lwkt_serialize_exit(&sc->serializer);
1354 /* --- Start of NVOSAPI interface --- */
1356 /* Allocate DMA enabled general use memory for API */
1358 nv_osalloc(PNV_VOID ctx, PMEMORY_BLOCK mem)
1360 struct nv_softc *sc;
1361 bus_addr_t mem_physical;
1363 DEBUGOUT(NV_DEBUG_API, "nv: nv_osalloc - %d\n", mem->uiLength);
1365 sc = (struct nv_softc *)ctx;
1367 mem->pLogical = (PVOID)contigmalloc(mem->uiLength, M_DEVBUF,
1368 M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE, 0);
1370 if (!mem->pLogical) {
1371 device_printf(sc->dev, "memory allocation failed\n");
1374 memset(mem->pLogical, 0, (ulong)mem->uiLength);
1375 mem_physical = vtophys(mem->pLogical);
1376 mem->pPhysical = (PVOID)mem_physical;
1378 DEBUGOUT(NV_DEBUG_API, "nv: nv_osalloc %p/%p - %d\n",
1379 mem->pLogical, mem->pPhysical, mem->uiLength);
1384 /* Free allocated memory */
1386 nv_osfree(PNV_VOID ctx, PMEMORY_BLOCK mem)
1388 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfree - %p - %d\n",
1389 mem->pLogical, mem->uiLength);
1391 contigfree(mem->pLogical, PAGE_SIZE, M_DEVBUF);
1395 /* Copied directly from nvnet.c */
1397 nv_osallocex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1399 MEMORY_BLOCK mem_block;
1401 DEBUGOUT(NV_DEBUG_API, "nv: nv_osallocex\n");
1403 mem_block_ex->pLogical = NULL;
1404 mem_block_ex->uiLengthOrig = mem_block_ex->uiLength;
1406 if ((mem_block_ex->AllocFlags & ALLOC_MEMORY_ALIGNED) &&
1407 (mem_block_ex->AlignmentSize > 1)) {
1408 DEBUGOUT(NV_DEBUG_API, " aligning on %d\n",
1409 mem_block_ex->AlignmentSize);
1410 mem_block_ex->uiLengthOrig += mem_block_ex->AlignmentSize;
1412 mem_block.uiLength = mem_block_ex->uiLengthOrig;
1414 if (nv_osalloc(ctx, &mem_block) == 0) {
1417 mem_block_ex->pLogicalOrig = mem_block.pLogical;
1418 mem_block_ex->pPhysicalOrigLow = (uintptr_t)mem_block.pPhysical;
1419 mem_block_ex->pPhysicalOrigHigh = 0;
1421 mem_block_ex->pPhysical = mem_block.pPhysical;
1422 mem_block_ex->pLogical = mem_block.pLogical;
1424 if (mem_block_ex->uiLength != mem_block_ex->uiLengthOrig) {
1425 unsigned int offset;
1426 offset = mem_block_ex->pPhysicalOrigLow & (mem_block_ex->AlignmentSize - 1);
1429 mem_block_ex->pPhysical = (PVOID)((uintptr_t)mem_block_ex->pPhysical +
1430 mem_block_ex->AlignmentSize - offset);
1431 mem_block_ex->pLogical = (PVOID)((uintptr_t)mem_block_ex->pLogical +
1432 mem_block_ex->AlignmentSize - offset);
1434 } /* if (mem_block_ex->uiLength !=
1435 * mem_block_ex->uiLengthOrig) */
1439 /* Copied directly from nvnet.c */
1441 nv_osfreeex(PNV_VOID ctx, PMEMORY_BLOCKEX mem_block_ex)
1443 MEMORY_BLOCK mem_block;
1445 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfreeex\n");
1447 mem_block.pLogical = mem_block_ex->pLogicalOrig;
1448 mem_block.pPhysical = (PVOID)((uintptr_t)mem_block_ex->pPhysicalOrigLow);
1449 mem_block.uiLength = mem_block_ex->uiLengthOrig;
1451 return (nv_osfree(ctx, &mem_block));
1454 /* Clear memory region */
1456 nv_osclear(PNV_VOID ctx, PNV_VOID mem, NV_SINT32 length)
1458 DEBUGOUT(NV_DEBUG_API, "nv: nv_osclear\n");
1459 memset(mem, 0, length);
1463 /* Sleep for a tick */
1465 nv_osdelay(PNV_VOID ctx, NV_UINT32 usec)
1467 if (usec >= 1000000 / hz) {
1468 tsleep(nv_osdelay, 0, "nvdelay", (usec * hz / 1000000) + 1);
1475 /* Allocate memory for rx buffer */
1477 nv_osallocrxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID *id)
1479 struct nv_softc *sc = ctx;
1480 struct nv_rx_desc *desc;
1481 struct nv_map_buffer *buf;
1484 DEBUGOUT(NV_DEBUG_API, "nv: nv_osallocrxbuf\n");
1486 if (sc->pending_rxs == RX_RING_SIZE) {
1487 device_printf(sc->dev, "rx ring buffer is full\n");
1490 desc = sc->rx_desc + sc->cur_rx;
1493 if (buf->mbuf == NULL) {
1494 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1495 if (buf->mbuf == NULL) {
1496 device_printf(sc->dev, "failed to allocate memory\n");
1499 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
1500 m_adj(buf->mbuf, ETHER_ALIGN);
1502 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
1503 nv_dmamap_rx_cb, &desc->paddr, 0);
1505 device_printf(sc->dev, "failed to dmamap mbuf\n");
1510 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
1511 desc->buflength = buf->mbuf->m_len;
1512 desc->vaddr = mtod(buf->mbuf, PVOID);
1515 sc->cur_rx = (sc->cur_rx + 1) % RX_RING_SIZE;
1517 mem->pLogical = (void *)desc->vaddr;
1518 mem->pPhysical = (void *)desc->paddr;
1519 mem->uiLength = desc->buflength;
1528 /* Free the rx buffer */
1530 nv_osfreerxbuf(PNV_VOID ctx, PMEMORY_BLOCK mem, PNV_VOID id)
1532 struct nv_softc *sc = ctx;
1533 struct nv_rx_desc *desc;
1534 struct nv_map_buffer *buf;
1536 DEBUGOUT(NV_DEBUG_API, "nv: nv_osfreerxbuf\n");
1538 desc = (struct nv_rx_desc *) id;
1542 bus_dmamap_unload(sc->mtag, buf->map);
1543 bus_dmamap_destroy(sc->mtag, buf->map);
1552 /* This gets called by the Nvidia API after our TX packet has been sent */
1554 nv_ospackettx(PNV_VOID ctx, PNV_VOID id, NV_UINT32 success)
1556 struct nv_softc *sc = ctx;
1557 struct nv_map_buffer *buf;
1558 struct nv_tx_desc *desc = (struct nv_tx_desc *) id;
1561 DEBUGOUT(NV_DEBUG_API, "nv: nv_ospackettx\n");
1567 /* Unload and free mbuf cluster */
1568 if (buf->mbuf == NULL)
1571 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTWRITE);
1572 bus_dmamap_unload(sc->mtag, buf->map);
1577 * Make sure we are clear to go if we previously stalled due
1580 if (sc->pending_txs < TX_RING_SIZE) {
1581 ifp->if_flags &= ~IFF_OACTIVE;
1582 if (!ifq_is_empty(&ifp->if_snd))
1583 nv_ifstart_serialized(ifp);
1589 /* This gets called by the Nvidia API when a new packet has been received */
1590 /* XXX What is newbuf used for? XXX */
1592 nv_ospacketrx(PNV_VOID ctx, PNV_VOID data, NV_UINT32 success,
1593 NV_UINT8 *newbuf, NV_UINT8 priority)
1595 struct nv_softc *sc = ctx;
1597 struct nv_rx_desc *desc;
1598 struct nv_map_buffer *buf;
1599 ADAPTER_READ_DATA *readdata;
1601 DEBUGOUT(NV_DEBUG_API, "nv: nv_ospacketrx\n");
1605 readdata = (ADAPTER_READ_DATA *) data;
1606 desc = readdata->pvID;
1608 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1611 /* Sync DMA bounce buffer. */
1612 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1614 /* First mbuf in packet holds the ethernet and packet headers */
1615 buf->mbuf->m_pkthdr.rcvif = ifp;
1616 buf->mbuf->m_pkthdr.len = buf->mbuf->m_len = readdata->ulTotalLength;
1618 bus_dmamap_unload(sc->mtag, buf->map);
1620 /* Give mbuf to OS. */
1621 (*ifp->if_input) (ifp, buf->mbuf);
1622 if (readdata->ulFilterMatch & ADREADFL_MULTICAST_MATCH)
1625 /* Blat the mbuf pointer, kernel will free the mbuf cluster */
1628 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
1629 bus_dmamap_unload(sc->mtag, buf->map);
1634 sc->cur_rx = desc - sc->rx_desc;
1640 /* This gets called by NVIDIA API when the PHY link state changes */
1642 nv_oslinkchg(PNV_VOID ctx, NV_SINT32 enabled)
1644 struct nv_softc *sc = (struct nv_softc *)ctx;
1647 DEBUGOUT(NV_DEBUG_API, "nv: nv_oslinkchg\n");
1652 ifp->if_flags |= IFF_UP;
1654 ifp->if_flags &= ~IFF_UP;
1661 /* Setup a watchdog timer */
1663 nv_osalloctimer(PNV_VOID ctx, PNV_VOID *timer)
1665 struct nv_softc *sc = (struct nv_softc *)ctx;
1667 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osalloctimer\n");
1669 callout_init(&sc->ostimer);
1670 *timer = &sc->ostimer;
1675 /* Free the timer */
1677 nv_osfreetimer(PNV_VOID ctx, PNV_VOID timer)
1679 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osfreetimer\n");
1684 /* Setup timer parameters */
1686 nv_osinittimer(PNV_VOID ctx, PNV_VOID timer, PTIMER_FUNC func, PNV_VOID parameters)
1688 struct nv_softc *sc = (struct nv_softc *)ctx;
1690 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osinittimer\n");
1692 sc->ostimer_func = func;
1693 sc->ostimer_params = parameters;
1699 * Set the timer to go off
1701 * XXX what the hell are the units for 'delay' ? They sure aren't ticks!
1704 nv_ossettimer(PNV_VOID ctx, PNV_VOID timer, NV_UINT32 delay)
1706 struct nv_softc *sc = ctx;
1708 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ossettimer\n");
1709 printf("nv_ossettimer %d\n", (int)delay);
1711 callout_reset(&sc->ostimer, delay, sc->ostimer_func,
1712 sc->ostimer_params);
1717 /* Cancel the timer */
1719 nv_oscanceltimer(PNV_VOID ctx, PNV_VOID timer)
1721 struct nv_softc *sc = ctx;
1723 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_oscanceltimer\n");
1725 callout_stop(&sc->ostimer);
1731 nv_ospreprocpkt(PNV_VOID ctx, PNV_VOID readdata, PNV_VOID *id, NV_UINT8 *newbuffer,
1734 /* Not implemented */
1735 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ospreprocpkt\n");
1741 nv_ospreprocpktnopq(PNV_VOID ctx, PNV_VOID readdata)
1743 /* Not implemented */
1744 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_ospreprocpkt\n");
1750 nv_osindicatepkt(PNV_VOID ctx, PNV_VOID *id, NV_UINT32 pktno)
1752 /* Not implemented */
1753 DEBUGOUT(NV_DEBUG_BROKEN, "nv: nv_osindicatepkt\n");
1758 /* Allocate mutex context (already done in nv_attach) */
1760 nv_oslockalloc(PNV_VOID ctx, NV_SINT32 type, PNV_VOID *pLock)
1762 struct nv_softc *sc = (struct nv_softc *)ctx;
1764 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockalloc\n");
1766 *pLock = (void **)sc;
1771 /* Obtain a spin lock */
1773 nv_oslockacquire(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1775 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockacquire\n");
1777 NV_OSLOCK((struct nv_softc *)lock);
1784 nv_oslockrelease(PNV_VOID ctx, NV_SINT32 type, PNV_VOID lock)
1786 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_oslockrelease\n");
1788 NV_OSUNLOCK((struct nv_softc *)lock);
1793 /* I have no idea what this is for */
1795 nv_osreturnbufvirt(PNV_VOID ctx, PNV_VOID readdata)
1797 /* Not implemented */
1798 DEBUGOUT(NV_DEBUG_LOCK, "nv: nv_osreturnbufvirt\n");
1799 panic("nv: nv_osreturnbufvirtual not implemented\n");
1805 /* --- End on NVOSAPI interface --- */