History log of /openbsd-current/sys/dev/pci/if_nfe.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.126 24-May-2024 jsg

remove unneeded includes; ok miod@


Revision tags: OPENBSD_7_5_BASE
# 1.125 10-Nov-2023 bluhm

Make ifq and ifiq interface MP safe.

Rename ifq_set_maxlen() to ifq_init_maxlen(). This function neither
uses WRITE_ONCE() nor a mutex and is called before the ifq mutex
is initialized. The new name expresses that it should be used only
during interface attach when there is no concurrency.

Protect ifq_len(), ifq_empty(), ifiq_len(), and ifiq_empty() with
READ_ONCE(). They can be used without lock as they only read a
single integer.

OK dlg@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.124 11-Mar-2022 mpi

Constify struct cfattach.


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.123 12-Dec-2020 jan

Rename the macro MCLGETI to MCLGETL and removes the dead parameter ifp.

OK dlg@, bluhm@
No Opinion mpi@
Not against it claudio@


Revision tags: OPENBSD_6_8_BASE
# 1.122 10-Jul-2020 patrick

Change users of IFQ_SET_MAXLEN() and IFQ_IS_EMPTY() to use the "new" API.

ok dlg@ tobhe@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.121 25-Sep-2019 kevlo

Argument order fix for MCLGETI.

ok claudio@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.120 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.119 22-Jan-2017 dlg

move counting if_opackets next to counting if_obytes in if_enqueue.

this means packets are consistently counted in one place, unlike the
many and various ways that drivers thought they should do it.

ok mpi@ deraadt@


# 1.118 29-Nov-2016 jsg

m_free() and m_freem() test for NULL. Simplify callers which had their own
NULL tests.

ok mpi@


Revision tags: OPENBSD_6_0_BASE
# 1.117 13-Apr-2016 mpi

G/C IFQ_SET_READY().


Revision tags: OPENBSD_5_9_BASE
# 1.116 25-Nov-2015 dlg

replace IFF_OACTIVE manipulation with mpsafe operations.

there are two things shared between the network stack and drivers
in the send path: the send queue and the IFF_OACTIVE flag. the send
queue is now protected by a mutex. this diff makes the oactive
functionality mpsafe too.

IFF_OACTIVE is part of if_flags. there are two problems with that.
firstly, if_flags is a short and we dont have any MI atomic operations
to manipulate a short. secondly, while we could make the IFF_OACTIVE
operates mpsafe, all changes to other flags would have to be made
safe at the same time, otherwise a read-modify-write cycle on their
updates could clobber the oactive change.

instead, this moves the oactive mark into struct ifqueue and provides
an API for changing it. there's ifq_set_oactive, ifq_clr_oactive,
and ifq_is_oactive. these are modelled on ifsq_set_oactive,
ifsq_clr_oactive, and ifsq_is_oactive in dragonflybsd.

this diff includes changes to all the drivers manipulating IFF_OACTIVE
to now use the ifsq_{set,clr_is}_oactive API too.

ok kettenis@ mpi@ jmatthew@ deraadt@


# 1.115 24-Nov-2015 mpi

You only need <net/if_dl.h> if you're using LLADDR() or a sockaddr_dl.


# 1.114 20-Nov-2015 dlg

shuffle struct ifqueue so in flight mbufs are protected by a mutex.

the code is refactored so the IFQ macros call newly implemented ifq
functions. the ifq code is split so each discipline (priq and hfsc
in our case) is an opaque set of operations that the common ifq
code can call. the common code does the locking, accounting (ifq_len
manipulation), and freeing of the mbuf if the disciplines enqueue
function rejects it. theyre kind of like bufqs in the block layer
with their fifo and nscan disciplines.

the new api also supports atomic switching of disciplines at runtime.
the hfsc setup in pf_ioctl.c has been tweaked to build a complete
hfsc_if structure which it attaches to the send queue in a single
operation, rather than attaching to the interface up front and
building up a list of queues.

the send queue is now mutexed, which raises the expectation that
packets can be enqueued or purged on one cpu while another cpu is
dequeueing them in a driver for transmission. a lot of drivers use
IFQ_POLL to peek at an mbuf and attempt to fit it on the ring before
committing to it with a later IFQ_DEQUEUE operation. if the mbuf
gets freed in between the POLL and DEQUEUE operations, fireworks
will ensue.

to avoid this, the ifq api introduces ifq_deq_begin, ifq_deq_rollback,
and ifq_deq_commit. ifq_deq_begin allows a driver to take the ifq
mutex and get a reference to the mbuf they wish to try and tx. if
there's space, they can ifq_deq_commit it to remove the mbuf and
release the mutex. if there's no space, ifq_deq_rollback simply
releases the mutex. this api was developed to make updating the
drivers using IFQ_POLL easy, instead of having to do significant
semantic changes to avoid POLL that we cannot test on all the
hardware.

the common code has been tested pretty hard, and all the driver
modifications are straightforward except for de(4). if that breaks
it can be dealt with later.

ok mpi@ jmatthew@


# 1.113 14-Nov-2015 mpi

Do not include <net/if_vlan_var.h> when it's not necessary.

Because of the VLAN hacks in mpw(4) this file still contains the definition
of "struct ifvlan" which depends on <sys/refcnt.h> which in turns pull
<sys/atomic.h>...


# 1.112 25-Oct-2015 mpi

arp_ifinit() is no longer needed.


Revision tags: OPENBSD_5_8_BASE
# 1.111 24-Jun-2015 mpi

Increment if_ipackets in if_input().

Note that pseudo-drivers not using if_input() are not affected by this
conversion.

ok mikeb@, kettenis@, claudio@, dlg@


# 1.110 20-Mar-2015 mpi

Convert to if_input(), thanks to krw@ for testing.


# 1.109 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.108 22-Dec-2014 tedu

unifdef INET


# 1.107 20-Aug-2014 dlg

after allocating an mbuf and cluster you still need to init the length
fields.

found by steven roberts, who also tested this fix for me


# 1.106 20-Aug-2014 dlg

remove the custom jumbo allocator. its never been enabled or used.

putting this into the tree to make it easier to test.


Revision tags: OPENBSD_5_6_BASE
# 1.105 22-Jul-2014 mpi

Fewer <netinet/in_systm.h>


Revision tags: OPENBSD_5_5_BASE
# 1.104 28-Dec-2013 deraadt

The few network drivers that called their children's (ie. mii PHY
drivers) activate functions at DVACT_RESUME time do not need to do
so, since their PHYs are repaired by IFF_UP.


# 1.103 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.102 07-Aug-2013 bluhm

Most network drivers include netinet/in_var.h, but apparently they
don't have to. Just remove these include lines.
Compiled on amd64 i386 sparc64; OK henning@ mikeb@


Revision tags: OPENBSD_5_4_BASE
# 1.101 01-Apr-2013 brad

Rewrite receive filter and ioctl handling code.

ok jsg@


Revision tags: OPENBSD_5_3_BASE
# 1.100 29-Nov-2012 brad

Remove setting an initial assumed baudrate upon driver attach which is not
necessarily correct, there might not even be a link when attaching.

ok mikeb@ reyk@


# 1.99 31-Aug-2012 stsp

Wake on LAN support for nfe(4). Tested by me, russel on misc@, and jsg.
ok jsg


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.98 05-Apr-2011 henning

mechanic rename M_{TCP|UDP}V4_CSUM_OUT -> M_{TCP|UDP}_CSUM_OUT
ok claudio krw


Revision tags: OPENBSD_4_9_BASE
# 1.97 10-Jan-2011 kettenis

Some nfe(4)/rlphy(4) combos don't work, because the PHY responds to all
addresses on the mii bus. As a countereasure, only attach the first PHY we
encounter. It is very unlikely we're going to ever see nfe(4) with multiple
PHYs. The same is probably true for any modern NIC.

ok mikeb@, deraadt@


# 1.96 07-Sep-2010 deraadt

remove the powerhook code. All architectures now use the ca_activate tree
traversal code to suspend/resume
ok oga kettenis blambert


# 1.95 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.94 31-Aug-2010 deraadt

activate function should return result of config_activate_children


# 1.93 27-Aug-2010 deraadt

Make the powerhook use the activate functions, which actually do the full
job.


# 1.92 27-Aug-2010 jsg

remove the unused if_init callback in struct ifnet
ok deraadt@ henning@ claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.91 06-Aug-2010 mlarkin

ca_activate function for nfe(4) for suspend/resume
tested on nvidia mcp51

ok deraadt@


# 1.90 19-May-2010 oga

BUS_DMA_ZERO instead of alloc, map, bzero.

ok krw@


Revision tags: OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.89 18-Jun-2009 jsg

Remove support for cancelled NVIDIA MCP7B and add initial support for
MCP89.

From Brad based on information from Peer Chen @ NVIDIA via Linux.


# 1.88 29-Mar-2009 sthen

make various strings ("can't map mem space" and similar) more consistent
between instances, saving space in the kernel. feedback from many (some
incorporated, some left for future work).

ok deraadt, kettenis, "why not" miod.


Revision tags: OPENBSD_4_5_BASE
# 1.87 28-Nov-2008 brad

Eliminate the redundant bits of code for MTU and multicast handling
from the individual drivers now that ether_ioctl() handles this.

Shrinks the i386 kernels by..
RAMDISK - 2176 bytes
RAMDISKB - 1504 bytes
RAMDISKC - 736 bytes

Tested by naddy@/okan@/sthen@/brad@/todd@/jmc@ and lots of users.
Build tested on almost all archs by todd@/brad@

ok naddy@


# 1.86 09-Nov-2008 naddy

Introduce bpf_mtap_ether(), which for the benefit of bpf listeners
creates the VLAN encapsulation from the tag stored in the mbuf
header. Idea from FreeBSD, input from claudio@ and canacar@.

Switch all hardware VLAN enabled drivers to the new function.

ok claudio@


# 1.85 28-Oct-2008 brad

Remove #if NVLAN.


# 1.84 28-Oct-2008 brad

Re-add support for RX VLAN tag stripping.


# 1.83 16-Oct-2008 naddy

Switch the existing TX VLAN hardware support over to having the
tag in the header. Convert TX tagging in the drivers.

Help and ok brad@


# 1.82 02-Oct-2008 brad

First step towards cleaning up the Ethernet driver ioctl handling.
Move calling ether_ioctl() from the top of the ioctl function, which
at the moment does absolutely nothing, to the default switch case.
Thus allowing drivers to define their own ioctl handlers and then
falling back on ether_ioctl(). The only functional change this results
in at the moment is having all Ethernet drivers returning the proper
errno of ENOTTY instead of EINVAL/ENXIO when encountering unknown
ioctl's.

Shrinks the i386 kernels by..
RAMDISK - 1024 bytes
RAMDISKB - 1120 bytes
RAMDISKC - 832 bytes

Tested by martin@/jsing@/todd@/brad@
Build tested on almost all archs by todd@/brad@

ok jsing@


# 1.81 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


# 1.80 09-Aug-2008 brad

MCP79 are also capable of Jumbo frames. Add the Jumbo support flag.

From: Linux forcedeth


Revision tags: OPENBSD_4_4_BASE
# 1.79 23-May-2008 brad

Simplify the combination use of pci_mapreg_type()/pci_mapreg_map() as
suggested by dlg@ awhile ago.

ok dlg@


# 1.78 19-May-2008 fgsch

correct format after %x -> %b convertion. deraadt jsg ok.


Revision tags: OPENBSD_4_3_BASE
# 1.77 05-Feb-2008 brad

Add the NFE_CORRECT_MACADDR flag for MCP77/79 chipsets.

ok jsg@ dlg@


# 1.76 02-Jan-2008 brad

add the NVIDIA MCP77/79 ids.

ok jsg@ dlg@


# 1.75 11-Dec-2007 mikeb

Fix check for pending interrupts as some other device is causing
the status register to change.

From form@.

ok jsg


# 1.74 05-Dec-2007 jsg

Make sure newer adapters are not in powerdown mode.
From Oleg Safiullin <form@pdp-11.org.ru> similiar to changes
in Linux/FreeBSD driver.


# 1.73 17-Nov-2007 jsg

Disable jumbo allocator until the low number of buffers case
is better handled.

This will likely resolve stalls some people are seeing under high
load.

ok damien@


# 1.72 12-Sep-2007 jsg

In nfe_start() do a fast return if IFF_OACTIVE is set, in
this case we need a Tx interrupt to clean up the DMA ring
before if_start can be properly called.

Diff based on a comment by & ok claudio@


# 1.71 07-Sep-2007 damien

use new malloc M_ZERO flag to shrink kernel.
remove <malloc.h> from files where malloc is not used.


# 1.70 01-Sep-2007 ckuethe

Correct the backwards ethernet address that some NVidia MACs have.
diff from brad. "commit this" jsg


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.69 02-Mar-2007 jsg

MCP65 has no hardware checksum support.
Diff from brad after he noticed recent changes
in the Linux driver by an NVIDIA employee.


# 1.68 08-Jan-2007 damien

Add support for HW TCP/IP checksum offload for adapters that support it.

Tested by many (IP/UDP/TCP):

Jason McIntyre <jmc@>
Chris Kuethe <chris.kuethe AT gmail.com>
Alf Schlichting <a.schlichting AT lemarit.com>
Rodolfo Gouveia <rgouveia AT cosmico.net>
Peter Stromberg <wilfried@>

Has been in snaps for weeks too with noone complaining so far.


# 1.67 15-Nov-2006 brad

Correct the WOL magic value and rename NFE_WOL_MAGIC to NFE_WOL_ENABLE.

WOL magic value from Peer Chen@NVIDIA via FreeBSD.

ok jsg@ damien@


# 1.66 10-Nov-2006 damien

Defer setting of the valid bit in the first TX descriptor after
all descriptors have been setup. Otherwise, hardware may start
processing descriptors faster than us and crap out.
Fixes "watchdog timeout" errors.

Original idea from Matthew Dillon @DragonFly.

ok deraadt@ jsg@ wim@
tested by many


# 1.65 05-Nov-2006 brad

add the NVIDIA MCP67 LAN PCI ids.


Revision tags: OPENBSD_4_0_BASE
# 1.64 23-Jul-2006 brad

branches: 1.64.2;
add NVidia MCP61/65 ids.


# 1.63 17-Jun-2006 brad

add sys/timeout.h


# 1.62 29-May-2006 brad

garbage collect NFE_NO_JUMBO.


# 1.61 28-May-2006 brad

- remove ETHER_MAX_LEN_JUMBO and ETHERMTU_JUMBO.
- use if_hardmtu for MTU ioctl handlers.

ok reyk@


# 1.60 28-May-2006 jason

unknown ioctl is ENOTTY not EINVAL


# 1.59 27-May-2006 brad

remove IFCAP_JUMBO_MTU interface capabilities flag and set if_hardmtu in a few
more drivers.

ok reyk@


# 1.58 20-May-2006 brad

set if_jumbo_mtu and the IFCAP_JUMBO_MTU capabilities flag where
appropriate.

ok reyk@


# 1.57 26-Apr-2006 jsg

Use %b in error flag printfs to describe meaning of error bits.
requested by deraadt@


# 1.56 26-Apr-2006 brad

And commit the diff from the right system.


# 1.55 26-Apr-2006 brad

check for IFF_RUNNING being set before calling nfe_init().

ok damien@ jsg@


# 1.54 07-Apr-2006 jsg

Add work around for mbuf leak in the tx path until we
can come up with a better guess as to how the hardware works.
From Chuck Silvers.
ok damien@


# 1.53 25-Mar-2006 djm

allow bpf(4) to ignore packets based on their direction (inbound or
outbound), using a new BIOCSDIRFILT ioctl;
guidance, feedback and ok canacar@


Revision tags: OPENBSD_3_9_BASE
# 1.52 02-Mar-2006 jsg

branches: 1.52.2;
Put the PHY update/reset call in nfe_init back to the spot it was at
before Rx/Tx/Interrupts are enabled.
This makes <fredd at cse.sc.edu>'s nfe+icsphy setup work again.

Tested on nfe+eephy by otto@ and myself, nfe+ciphy by otto@ and
nfe+rlphy by wilfried@

ok deraadt@


# 1.51 26-Feb-2006 damien

use sc->sc_dev.dv_xname consistently.


# 1.50 26-Feb-2006 krw

Let if_nfe.c compile again by putting declaration of ifp inside #ifdef
NFE_DEBUG.

ok dlg@


# 1.49 26-Feb-2006 jsg

Don't define NFE_DEBUG by default.
Set default debug level to 0.
Make sure to include interface or function name in debug strings.
Print Tx errors from the MAC when debug is not on.


# 1.48 24-Feb-2006 brad

check for M_PKTHDR.


# 1.47 22-Feb-2006 damien

fix nfe_txeof() to reset the wathdog timeout only when a full tx frame
has been sent.


# 1.46 22-Feb-2006 damien

re-enable interrupt mitigation and mask out NFE_IRQ_TIMER that was causing
interrupts flood.


# 1.45 22-Feb-2006 brad

update the media settings after MAC setup.


# 1.44 21-Feb-2006 damien

disable interrupt mitigation until i figure out why we're spending 10% of
CPU time in interrupts with it.


# 1.43 20-Feb-2006 damien

- remove mbuf linearization code. it is broken in this context and it is very
unlikely that we will see mbuf chains with 62 fragments anytime soon
(anyway, it would not crash, it would just freeze TX).
- fix max scatter value so we don't end up filling the ring with one mbuf
chain.


# 1.42 19-Feb-2006 damien

- fix h/w VLAN tagging and enable it for adapters that support it (VLAN tag
stripping job is left to the network stack).
- enable interrupt mitigation by default.
- add some magic to the initialization sequence in the hope that it will fix
TX issues seen on some adapters.


# 1.41 16-Feb-2006 damien

- stop enabling/disabling interrupts in nfe_intr().
- store RX/TX settings in nfe_softc so we don't recompute them all the time.
- fix h/w VLAN tagging flags.


# 1.40 15-Feb-2006 brad

move setting IFCAP_VLAN_MTU back to where it is supposed to be.


# 1.39 15-Feb-2006 damien

add support for 64bit rings base addresses.


# 1.38 15-Feb-2006 damien

actually disable h/w VLAN tagging for now.


# 1.37 15-Feb-2006 damien

- enable jumbo frames for adapters that support it.
- extend TX DMA mappings size from MCLBYTES to NFE_JBYTES.
- add initial (disabled) bits for interrupts mitigation.
- add initial (disabled) bits for h/w VLAN tagging.
- did some consistency tweaks while i'm here.


# 1.36 13-Feb-2006 brad

use pci_mapreg_type().

ok damien@


# 1.35 13-Feb-2006 brad

nVidia/nvidia -> NVIDIA


# 1.34 12-Feb-2006 damien

fix a printf


# 1.33 12-Feb-2006 damien

don't print shared interrupts when debugging is on.


# 1.32 12-Feb-2006 damien

use BUS_DMA_READ flag when mapping RX buffers.


# 1.31 12-Feb-2006 damien

reduce the number of DMA sync operations by sincing multiple TX descriptors
at once.


# 1.30 12-Feb-2006 damien

initial jumbo frames support (disabled for now).
#define'ing NFE_NO_JUMBO can save a few hundred KB of wired memory.


# 1.29 11-Feb-2006 brad

set IFCAP_VLAN_MTU in the capabilites field.

ok damien@


# 1.28 11-Feb-2006 damien

- call nfe_ifmedia_upd() early in nfe_init() to reset the PHY.
- set RX buffer size register properly


# 1.27 11-Feb-2006 damien

fix nfe_free_tx_ring() and nfe_reset_tx_ring() to unmap the good items.


# 1.26 11-Feb-2006 damien

don't free the mbuf in nfe_encap() if it can't be DMA'mapped since it is
left in if_snd. prettify nfe_start() a bit while i'm here.


# 1.25 11-Feb-2006 damien

MCP51 boards don't support jumbo frames..


# 1.24 11-Feb-2006 damien

force a wakeup of the MAC in nfe_init().
this makes my MCP51 board working.

committed over a nfe.


# 1.23 10-Feb-2006 brad

Only kick the TX engine once in nfe_start() and only if there are
packets to be transmitted, after iterating through the queue and
queuing up as many packets as possible as opposed to kicking
it each time through nfe_encap().

ok damien@ jsg@


# 1.22 08-Feb-2006 jsg

Add back minimal debugging to help track down tx errors some MACs
seem to be reporting.


# 1.21 08-Feb-2006 jsg

Use __LP64__ instead of __amd64__ for portability.
Pointed out by miod@.


# 1.20 07-Feb-2006 jsg

Disable checksum offload for the moment as it appears to not
work for some people.


# 1.19 05-Feb-2006 brad

Simplify SIOCSIFADDR switch case a bit.


# 1.18 05-Feb-2006 brad

Eliminate a reset when configuring the IP address.

ok jsg@


# 1.17 05-Feb-2006 jsg

Don't force NFE_DEBUG, set default debug level to 0.
Committed via an nfe.


# 1.16 05-Feb-2006 damien

quiet nfe_intr().


# 1.15 05-Feb-2006 damien

unbreak the tree.

pointed at by Peter Stromberg.


# 1.14 05-Feb-2006 damien

fix nfe_txeof().
with these changes, the connection seems to work just fine.

with help from jsg@


# 1.13 04-Feb-2006 damien

- start MII timer in nfe_init() and move timer initialization in nfe_attach()
- set RX filter before enabling RX in nfe_init()
- call mii_down() in nfe_stop()
- fix setting of full/half-duplex mode
- call mii_phy_reset() for each PHY attached and call mii_mediachg() in
nfe_ifmedia_upd()
- some cleaning while i'm here


# 1.12 04-Feb-2006 damien

fix clearing of h/w csum flags in all but the first fragment


# 1.11 04-Feb-2006 damien

handle link state change interrupts but do nothing for now


# 1.10 04-Feb-2006 damien

- add support for multicast filters.
- fix setting of if_capabilities flags for chips supporting checksum offload.
- fix dmesg output in case we can't establish the intr handler.
- fix a call to bus_dmamap_unload() in nfe_encap().


# 1.9 22-Jan-2006 damien

fixes miibus_{read,write}reg routines.
this resolves the ghost ukphy problem.
did some cleanup while i'm here.

tested by and ok jsg@


# 1.8 20-Jan-2006 brad

remove redundant code from nfe_attach().

ok jsg@


# 1.7 18-Jan-2006 damien

initial Tx/Rx bits. not working yet.

joint work with jsg@


# 1.6 15-Jan-2006 damien

fix dma mapping of tx ring.


# 1.5 14-Jan-2006 jsg

Make sure interrupt is properly ack'd.
From damien@


# 1.4 17-Dec-2005 jsg

Don't dynamically assign ring size, simplifies ring structures
and code that deals with rings. We can get away with this
as we only have one type of rx and one type of tx ring at a time
unlike ral(4).

Suggested by and ok damien@


# 1.3 17-Dec-2005 jsg

Split length and flags up into seperate variables in
descriptors and make use of MCLBYTES for length setting.
Sugested by damien@


# 1.2 14-Dec-2005 jsg

We aren't likely to need to keep track of crypted/decrypted ring
bits any time soon... pointed out by damien@


# 1.1 14-Dec-2005 jsg

Initial bits for an nvidia nforce Ethernet driver.
bus_dma usage modelled after ral. Does not yet see rx interrupts
when testing with ck804.

Nvidia won't give out documentation for this, various "free" operating
systems include a closed source driver, and the Linux people who reverse
engineered it to create a specification won't give it out.


# 1.125 10-Nov-2023 bluhm

Make ifq and ifiq interface MP safe.

Rename ifq_set_maxlen() to ifq_init_maxlen(). This function neither
uses WRITE_ONCE() nor a mutex and is called before the ifq mutex
is initialized. The new name expresses that it should be used only
during interface attach when there is no concurrency.

Protect ifq_len(), ifq_empty(), ifiq_len(), and ifiq_empty() with
READ_ONCE(). They can be used without lock as they only read a
single integer.

OK dlg@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.124 11-Mar-2022 mpi

Constify struct cfattach.


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.123 12-Dec-2020 jan

Rename the macro MCLGETI to MCLGETL and removes the dead parameter ifp.

OK dlg@, bluhm@
No Opinion mpi@
Not against it claudio@


Revision tags: OPENBSD_6_8_BASE
# 1.122 10-Jul-2020 patrick

Change users of IFQ_SET_MAXLEN() and IFQ_IS_EMPTY() to use the "new" API.

ok dlg@ tobhe@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.121 25-Sep-2019 kevlo

Argument order fix for MCLGETI.

ok claudio@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.120 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.119 22-Jan-2017 dlg

move counting if_opackets next to counting if_obytes in if_enqueue.

this means packets are consistently counted in one place, unlike the
many and various ways that drivers thought they should do it.

ok mpi@ deraadt@


# 1.118 29-Nov-2016 jsg

m_free() and m_freem() test for NULL. Simplify callers which had their own
NULL tests.

ok mpi@


Revision tags: OPENBSD_6_0_BASE
# 1.117 13-Apr-2016 mpi

G/C IFQ_SET_READY().


Revision tags: OPENBSD_5_9_BASE
# 1.116 25-Nov-2015 dlg

replace IFF_OACTIVE manipulation with mpsafe operations.

there are two things shared between the network stack and drivers
in the send path: the send queue and the IFF_OACTIVE flag. the send
queue is now protected by a mutex. this diff makes the oactive
functionality mpsafe too.

IFF_OACTIVE is part of if_flags. there are two problems with that.
firstly, if_flags is a short and we dont have any MI atomic operations
to manipulate a short. secondly, while we could make the IFF_OACTIVE
operates mpsafe, all changes to other flags would have to be made
safe at the same time, otherwise a read-modify-write cycle on their
updates could clobber the oactive change.

instead, this moves the oactive mark into struct ifqueue and provides
an API for changing it. there's ifq_set_oactive, ifq_clr_oactive,
and ifq_is_oactive. these are modelled on ifsq_set_oactive,
ifsq_clr_oactive, and ifsq_is_oactive in dragonflybsd.

this diff includes changes to all the drivers manipulating IFF_OACTIVE
to now use the ifsq_{set,clr_is}_oactive API too.

ok kettenis@ mpi@ jmatthew@ deraadt@


# 1.115 24-Nov-2015 mpi

You only need <net/if_dl.h> if you're using LLADDR() or a sockaddr_dl.


# 1.114 20-Nov-2015 dlg

shuffle struct ifqueue so in flight mbufs are protected by a mutex.

the code is refactored so the IFQ macros call newly implemented ifq
functions. the ifq code is split so each discipline (priq and hfsc
in our case) is an opaque set of operations that the common ifq
code can call. the common code does the locking, accounting (ifq_len
manipulation), and freeing of the mbuf if the disciplines enqueue
function rejects it. theyre kind of like bufqs in the block layer
with their fifo and nscan disciplines.

the new api also supports atomic switching of disciplines at runtime.
the hfsc setup in pf_ioctl.c has been tweaked to build a complete
hfsc_if structure which it attaches to the send queue in a single
operation, rather than attaching to the interface up front and
building up a list of queues.

the send queue is now mutexed, which raises the expectation that
packets can be enqueued or purged on one cpu while another cpu is
dequeueing them in a driver for transmission. a lot of drivers use
IFQ_POLL to peek at an mbuf and attempt to fit it on the ring before
committing to it with a later IFQ_DEQUEUE operation. if the mbuf
gets freed in between the POLL and DEQUEUE operations, fireworks
will ensue.

to avoid this, the ifq api introduces ifq_deq_begin, ifq_deq_rollback,
and ifq_deq_commit. ifq_deq_begin allows a driver to take the ifq
mutex and get a reference to the mbuf they wish to try and tx. if
there's space, they can ifq_deq_commit it to remove the mbuf and
release the mutex. if there's no space, ifq_deq_rollback simply
releases the mutex. this api was developed to make updating the
drivers using IFQ_POLL easy, instead of having to do significant
semantic changes to avoid POLL that we cannot test on all the
hardware.

the common code has been tested pretty hard, and all the driver
modifications are straightforward except for de(4). if that breaks
it can be dealt with later.

ok mpi@ jmatthew@


# 1.113 14-Nov-2015 mpi

Do not include <net/if_vlan_var.h> when it's not necessary.

Because of the VLAN hacks in mpw(4) this file still contains the definition
of "struct ifvlan" which depends on <sys/refcnt.h> which in turns pull
<sys/atomic.h>...


# 1.112 25-Oct-2015 mpi

arp_ifinit() is no longer needed.


Revision tags: OPENBSD_5_8_BASE
# 1.111 24-Jun-2015 mpi

Increment if_ipackets in if_input().

Note that pseudo-drivers not using if_input() are not affected by this
conversion.

ok mikeb@, kettenis@, claudio@, dlg@


# 1.110 20-Mar-2015 mpi

Convert to if_input(), thanks to krw@ for testing.


# 1.109 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.108 22-Dec-2014 tedu

unifdef INET


# 1.107 20-Aug-2014 dlg

after allocating an mbuf and cluster you still need to init the length
fields.

found by steven roberts, who also tested this fix for me


# 1.106 20-Aug-2014 dlg

remove the custom jumbo allocator. its never been enabled or used.

putting this into the tree to make it easier to test.


Revision tags: OPENBSD_5_6_BASE
# 1.105 22-Jul-2014 mpi

Fewer <netinet/in_systm.h>


Revision tags: OPENBSD_5_5_BASE
# 1.104 28-Dec-2013 deraadt

The few network drivers that called their children's (ie. mii PHY
drivers) activate functions at DVACT_RESUME time do not need to do
so, since their PHYs are repaired by IFF_UP.


# 1.103 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.102 07-Aug-2013 bluhm

Most network drivers include netinet/in_var.h, but apparently they
don't have to. Just remove these include lines.
Compiled on amd64 i386 sparc64; OK henning@ mikeb@


Revision tags: OPENBSD_5_4_BASE
# 1.101 01-Apr-2013 brad

Rewrite receive filter and ioctl handling code.

ok jsg@


Revision tags: OPENBSD_5_3_BASE
# 1.100 29-Nov-2012 brad

Remove setting an initial assumed baudrate upon driver attach which is not
necessarily correct, there might not even be a link when attaching.

ok mikeb@ reyk@


# 1.99 31-Aug-2012 stsp

Wake on LAN support for nfe(4). Tested by me, russel on misc@, and jsg.
ok jsg


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.98 05-Apr-2011 henning

mechanic rename M_{TCP|UDP}V4_CSUM_OUT -> M_{TCP|UDP}_CSUM_OUT
ok claudio krw


Revision tags: OPENBSD_4_9_BASE
# 1.97 10-Jan-2011 kettenis

Some nfe(4)/rlphy(4) combos don't work, because the PHY responds to all
addresses on the mii bus. As a countereasure, only attach the first PHY we
encounter. It is very unlikely we're going to ever see nfe(4) with multiple
PHYs. The same is probably true for any modern NIC.

ok mikeb@, deraadt@


# 1.96 07-Sep-2010 deraadt

remove the powerhook code. All architectures now use the ca_activate tree
traversal code to suspend/resume
ok oga kettenis blambert


# 1.95 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.94 31-Aug-2010 deraadt

activate function should return result of config_activate_children


# 1.93 27-Aug-2010 deraadt

Make the powerhook use the activate functions, which actually do the full
job.


# 1.92 27-Aug-2010 jsg

remove the unused if_init callback in struct ifnet
ok deraadt@ henning@ claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.91 06-Aug-2010 mlarkin

ca_activate function for nfe(4) for suspend/resume
tested on nvidia mcp51

ok deraadt@


# 1.90 19-May-2010 oga

BUS_DMA_ZERO instead of alloc, map, bzero.

ok krw@


Revision tags: OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.89 18-Jun-2009 jsg

Remove support for cancelled NVIDIA MCP7B and add initial support for
MCP89.

From Brad based on information from Peer Chen @ NVIDIA via Linux.


# 1.88 29-Mar-2009 sthen

make various strings ("can't map mem space" and similar) more consistent
between instances, saving space in the kernel. feedback from many (some
incorporated, some left for future work).

ok deraadt, kettenis, "why not" miod.


Revision tags: OPENBSD_4_5_BASE
# 1.87 28-Nov-2008 brad

Eliminate the redundant bits of code for MTU and multicast handling
from the individual drivers now that ether_ioctl() handles this.

Shrinks the i386 kernels by..
RAMDISK - 2176 bytes
RAMDISKB - 1504 bytes
RAMDISKC - 736 bytes

Tested by naddy@/okan@/sthen@/brad@/todd@/jmc@ and lots of users.
Build tested on almost all archs by todd@/brad@

ok naddy@


# 1.86 09-Nov-2008 naddy

Introduce bpf_mtap_ether(), which for the benefit of bpf listeners
creates the VLAN encapsulation from the tag stored in the mbuf
header. Idea from FreeBSD, input from claudio@ and canacar@.

Switch all hardware VLAN enabled drivers to the new function.

ok claudio@


# 1.85 28-Oct-2008 brad

Remove #if NVLAN.


# 1.84 28-Oct-2008 brad

Re-add support for RX VLAN tag stripping.


# 1.83 16-Oct-2008 naddy

Switch the existing TX VLAN hardware support over to having the
tag in the header. Convert TX tagging in the drivers.

Help and ok brad@


# 1.82 02-Oct-2008 brad

First step towards cleaning up the Ethernet driver ioctl handling.
Move calling ether_ioctl() from the top of the ioctl function, which
at the moment does absolutely nothing, to the default switch case.
Thus allowing drivers to define their own ioctl handlers and then
falling back on ether_ioctl(). The only functional change this results
in at the moment is having all Ethernet drivers returning the proper
errno of ENOTTY instead of EINVAL/ENXIO when encountering unknown
ioctl's.

Shrinks the i386 kernels by..
RAMDISK - 1024 bytes
RAMDISKB - 1120 bytes
RAMDISKC - 832 bytes

Tested by martin@/jsing@/todd@/brad@
Build tested on almost all archs by todd@/brad@

ok jsing@


# 1.81 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


# 1.80 09-Aug-2008 brad

MCP79 are also capable of Jumbo frames. Add the Jumbo support flag.

From: Linux forcedeth


Revision tags: OPENBSD_4_4_BASE
# 1.79 23-May-2008 brad

Simplify the combination use of pci_mapreg_type()/pci_mapreg_map() as
suggested by dlg@ awhile ago.

ok dlg@


# 1.78 19-May-2008 fgsch

correct format after %x -> %b convertion. deraadt jsg ok.


Revision tags: OPENBSD_4_3_BASE
# 1.77 05-Feb-2008 brad

Add the NFE_CORRECT_MACADDR flag for MCP77/79 chipsets.

ok jsg@ dlg@


# 1.76 02-Jan-2008 brad

add the NVIDIA MCP77/79 ids.

ok jsg@ dlg@


# 1.75 11-Dec-2007 mikeb

Fix check for pending interrupts as some other device is causing
the status register to change.

From form@.

ok jsg


# 1.74 05-Dec-2007 jsg

Make sure newer adapters are not in powerdown mode.
From Oleg Safiullin <form@pdp-11.org.ru> similiar to changes
in Linux/FreeBSD driver.


# 1.73 17-Nov-2007 jsg

Disable jumbo allocator until the low number of buffers case
is better handled.

This will likely resolve stalls some people are seeing under high
load.

ok damien@


# 1.72 12-Sep-2007 jsg

In nfe_start() do a fast return if IFF_OACTIVE is set, in
this case we need a Tx interrupt to clean up the DMA ring
before if_start can be properly called.

Diff based on a comment by & ok claudio@


# 1.71 07-Sep-2007 damien

use new malloc M_ZERO flag to shrink kernel.
remove <malloc.h> from files where malloc is not used.


# 1.70 01-Sep-2007 ckuethe

Correct the backwards ethernet address that some NVidia MACs have.
diff from brad. "commit this" jsg


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.69 02-Mar-2007 jsg

MCP65 has no hardware checksum support.
Diff from brad after he noticed recent changes
in the Linux driver by an NVIDIA employee.


# 1.68 08-Jan-2007 damien

Add support for HW TCP/IP checksum offload for adapters that support it.

Tested by many (IP/UDP/TCP):

Jason McIntyre <jmc@>
Chris Kuethe <chris.kuethe AT gmail.com>
Alf Schlichting <a.schlichting AT lemarit.com>
Rodolfo Gouveia <rgouveia AT cosmico.net>
Peter Stromberg <wilfried@>

Has been in snaps for weeks too with noone complaining so far.


# 1.67 15-Nov-2006 brad

Correct the WOL magic value and rename NFE_WOL_MAGIC to NFE_WOL_ENABLE.

WOL magic value from Peer Chen@NVIDIA via FreeBSD.

ok jsg@ damien@


# 1.66 10-Nov-2006 damien

Defer setting of the valid bit in the first TX descriptor after
all descriptors have been setup. Otherwise, hardware may start
processing descriptors faster than us and crap out.
Fixes "watchdog timeout" errors.

Original idea from Matthew Dillon @DragonFly.

ok deraadt@ jsg@ wim@
tested by many


# 1.65 05-Nov-2006 brad

add the NVIDIA MCP67 LAN PCI ids.


Revision tags: OPENBSD_4_0_BASE
# 1.64 23-Jul-2006 brad

branches: 1.64.2;
add NVidia MCP61/65 ids.


# 1.63 17-Jun-2006 brad

add sys/timeout.h


# 1.62 29-May-2006 brad

garbage collect NFE_NO_JUMBO.


# 1.61 28-May-2006 brad

- remove ETHER_MAX_LEN_JUMBO and ETHERMTU_JUMBO.
- use if_hardmtu for MTU ioctl handlers.

ok reyk@


# 1.60 28-May-2006 jason

unknown ioctl is ENOTTY not EINVAL


# 1.59 27-May-2006 brad

remove IFCAP_JUMBO_MTU interface capabilities flag and set if_hardmtu in a few
more drivers.

ok reyk@


# 1.58 20-May-2006 brad

set if_jumbo_mtu and the IFCAP_JUMBO_MTU capabilities flag where
appropriate.

ok reyk@


# 1.57 26-Apr-2006 jsg

Use %b in error flag printfs to describe meaning of error bits.
requested by deraadt@


# 1.56 26-Apr-2006 brad

And commit the diff from the right system.


# 1.55 26-Apr-2006 brad

check for IFF_RUNNING being set before calling nfe_init().

ok damien@ jsg@


# 1.54 07-Apr-2006 jsg

Add work around for mbuf leak in the tx path until we
can come up with a better guess as to how the hardware works.
From Chuck Silvers.
ok damien@


# 1.53 25-Mar-2006 djm

allow bpf(4) to ignore packets based on their direction (inbound or
outbound), using a new BIOCSDIRFILT ioctl;
guidance, feedback and ok canacar@


Revision tags: OPENBSD_3_9_BASE
# 1.52 02-Mar-2006 jsg

branches: 1.52.2;
Put the PHY update/reset call in nfe_init back to the spot it was at
before Rx/Tx/Interrupts are enabled.
This makes <fredd at cse.sc.edu>'s nfe+icsphy setup work again.

Tested on nfe+eephy by otto@ and myself, nfe+ciphy by otto@ and
nfe+rlphy by wilfried@

ok deraadt@


# 1.51 26-Feb-2006 damien

use sc->sc_dev.dv_xname consistently.


# 1.50 26-Feb-2006 krw

Let if_nfe.c compile again by putting declaration of ifp inside #ifdef
NFE_DEBUG.

ok dlg@


# 1.49 26-Feb-2006 jsg

Don't define NFE_DEBUG by default.
Set default debug level to 0.
Make sure to include interface or function name in debug strings.
Print Tx errors from the MAC when debug is not on.


# 1.48 24-Feb-2006 brad

check for M_PKTHDR.


# 1.47 22-Feb-2006 damien

fix nfe_txeof() to reset the wathdog timeout only when a full tx frame
has been sent.


# 1.46 22-Feb-2006 damien

re-enable interrupt mitigation and mask out NFE_IRQ_TIMER that was causing
interrupts flood.


# 1.45 22-Feb-2006 brad

update the media settings after MAC setup.


# 1.44 21-Feb-2006 damien

disable interrupt mitigation until i figure out why we're spending 10% of
CPU time in interrupts with it.


# 1.43 20-Feb-2006 damien

- remove mbuf linearization code. it is broken in this context and it is very
unlikely that we will see mbuf chains with 62 fragments anytime soon
(anyway, it would not crash, it would just freeze TX).
- fix max scatter value so we don't end up filling the ring with one mbuf
chain.


# 1.42 19-Feb-2006 damien

- fix h/w VLAN tagging and enable it for adapters that support it (VLAN tag
stripping job is left to the network stack).
- enable interrupt mitigation by default.
- add some magic to the initialization sequence in the hope that it will fix
TX issues seen on some adapters.


# 1.41 16-Feb-2006 damien

- stop enabling/disabling interrupts in nfe_intr().
- store RX/TX settings in nfe_softc so we don't recompute them all the time.
- fix h/w VLAN tagging flags.


# 1.40 15-Feb-2006 brad

move setting IFCAP_VLAN_MTU back to where it is supposed to be.


# 1.39 15-Feb-2006 damien

add support for 64bit rings base addresses.


# 1.38 15-Feb-2006 damien

actually disable h/w VLAN tagging for now.


# 1.37 15-Feb-2006 damien

- enable jumbo frames for adapters that support it.
- extend TX DMA mappings size from MCLBYTES to NFE_JBYTES.
- add initial (disabled) bits for interrupts mitigation.
- add initial (disabled) bits for h/w VLAN tagging.
- did some consistency tweaks while i'm here.


# 1.36 13-Feb-2006 brad

use pci_mapreg_type().

ok damien@


# 1.35 13-Feb-2006 brad

nVidia/nvidia -> NVIDIA


# 1.34 12-Feb-2006 damien

fix a printf


# 1.33 12-Feb-2006 damien

don't print shared interrupts when debugging is on.


# 1.32 12-Feb-2006 damien

use BUS_DMA_READ flag when mapping RX buffers.


# 1.31 12-Feb-2006 damien

reduce the number of DMA sync operations by sincing multiple TX descriptors
at once.


# 1.30 12-Feb-2006 damien

initial jumbo frames support (disabled for now).
#define'ing NFE_NO_JUMBO can save a few hundred KB of wired memory.


# 1.29 11-Feb-2006 brad

set IFCAP_VLAN_MTU in the capabilites field.

ok damien@


# 1.28 11-Feb-2006 damien

- call nfe_ifmedia_upd() early in nfe_init() to reset the PHY.
- set RX buffer size register properly


# 1.27 11-Feb-2006 damien

fix nfe_free_tx_ring() and nfe_reset_tx_ring() to unmap the good items.


# 1.26 11-Feb-2006 damien

don't free the mbuf in nfe_encap() if it can't be DMA'mapped since it is
left in if_snd. prettify nfe_start() a bit while i'm here.


# 1.25 11-Feb-2006 damien

MCP51 boards don't support jumbo frames..


# 1.24 11-Feb-2006 damien

force a wakeup of the MAC in nfe_init().
this makes my MCP51 board working.

committed over a nfe.


# 1.23 10-Feb-2006 brad

Only kick the TX engine once in nfe_start() and only if there are
packets to be transmitted, after iterating through the queue and
queuing up as many packets as possible as opposed to kicking
it each time through nfe_encap().

ok damien@ jsg@


# 1.22 08-Feb-2006 jsg

Add back minimal debugging to help track down tx errors some MACs
seem to be reporting.


# 1.21 08-Feb-2006 jsg

Use __LP64__ instead of __amd64__ for portability.
Pointed out by miod@.


# 1.20 07-Feb-2006 jsg

Disable checksum offload for the moment as it appears to not
work for some people.


# 1.19 05-Feb-2006 brad

Simplify SIOCSIFADDR switch case a bit.


# 1.18 05-Feb-2006 brad

Eliminate a reset when configuring the IP address.

ok jsg@


# 1.17 05-Feb-2006 jsg

Don't force NFE_DEBUG, set default debug level to 0.
Committed via an nfe.


# 1.16 05-Feb-2006 damien

quiet nfe_intr().


# 1.15 05-Feb-2006 damien

unbreak the tree.

pointed at by Peter Stromberg.


# 1.14 05-Feb-2006 damien

fix nfe_txeof().
with these changes, the connection seems to work just fine.

with help from jsg@


# 1.13 04-Feb-2006 damien

- start MII timer in nfe_init() and move timer initialization in nfe_attach()
- set RX filter before enabling RX in nfe_init()
- call mii_down() in nfe_stop()
- fix setting of full/half-duplex mode
- call mii_phy_reset() for each PHY attached and call mii_mediachg() in
nfe_ifmedia_upd()
- some cleaning while i'm here


# 1.12 04-Feb-2006 damien

fix clearing of h/w csum flags in all but the first fragment


# 1.11 04-Feb-2006 damien

handle link state change interrupts but do nothing for now


# 1.10 04-Feb-2006 damien

- add support for multicast filters.
- fix setting of if_capabilities flags for chips supporting checksum offload.
- fix dmesg output in case we can't establish the intr handler.
- fix a call to bus_dmamap_unload() in nfe_encap().


# 1.9 22-Jan-2006 damien

fixes miibus_{read,write}reg routines.
this resolves the ghost ukphy problem.
did some cleanup while i'm here.

tested by and ok jsg@


# 1.8 20-Jan-2006 brad

remove redundant code from nfe_attach().

ok jsg@


# 1.7 18-Jan-2006 damien

initial Tx/Rx bits. not working yet.

joint work with jsg@


# 1.6 15-Jan-2006 damien

fix dma mapping of tx ring.


# 1.5 14-Jan-2006 jsg

Make sure interrupt is properly ack'd.
From damien@


# 1.4 17-Dec-2005 jsg

Don't dynamically assign ring size, simplifies ring structures
and code that deals with rings. We can get away with this
as we only have one type of rx and one type of tx ring at a time
unlike ral(4).

Suggested by and ok damien@


# 1.3 17-Dec-2005 jsg

Split length and flags up into seperate variables in
descriptors and make use of MCLBYTES for length setting.
Sugested by damien@


# 1.2 14-Dec-2005 jsg

We aren't likely to need to keep track of crypted/decrypted ring
bits any time soon... pointed out by damien@


# 1.1 14-Dec-2005 jsg

Initial bits for an nvidia nforce Ethernet driver.
bus_dma usage modelled after ral. Does not yet see rx interrupts
when testing with ck804.

Nvidia won't give out documentation for this, various "free" operating
systems include a closed source driver, and the Linux people who reverse
engineered it to create a specification won't give it out.


# 1.124 11-Mar-2022 mpi

Constify struct cfattach.


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.123 12-Dec-2020 jan

Rename the macro MCLGETI to MCLGETL and removes the dead parameter ifp.

OK dlg@, bluhm@
No Opinion mpi@
Not against it claudio@


Revision tags: OPENBSD_6_8_BASE
# 1.122 10-Jul-2020 patrick

Change users of IFQ_SET_MAXLEN() and IFQ_IS_EMPTY() to use the "new" API.

ok dlg@ tobhe@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.121 25-Sep-2019 kevlo

Argument order fix for MCLGETI.

ok claudio@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.120 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.119 22-Jan-2017 dlg

move counting if_opackets next to counting if_obytes in if_enqueue.

this means packets are consistently counted in one place, unlike the
many and various ways that drivers thought they should do it.

ok mpi@ deraadt@


# 1.118 29-Nov-2016 jsg

m_free() and m_freem() test for NULL. Simplify callers which had their own
NULL tests.

ok mpi@


Revision tags: OPENBSD_6_0_BASE
# 1.117 13-Apr-2016 mpi

G/C IFQ_SET_READY().


Revision tags: OPENBSD_5_9_BASE
# 1.116 25-Nov-2015 dlg

replace IFF_OACTIVE manipulation with mpsafe operations.

there are two things shared between the network stack and drivers
in the send path: the send queue and the IFF_OACTIVE flag. the send
queue is now protected by a mutex. this diff makes the oactive
functionality mpsafe too.

IFF_OACTIVE is part of if_flags. there are two problems with that.
firstly, if_flags is a short and we dont have any MI atomic operations
to manipulate a short. secondly, while we could make the IFF_OACTIVE
operates mpsafe, all changes to other flags would have to be made
safe at the same time, otherwise a read-modify-write cycle on their
updates could clobber the oactive change.

instead, this moves the oactive mark into struct ifqueue and provides
an API for changing it. there's ifq_set_oactive, ifq_clr_oactive,
and ifq_is_oactive. these are modelled on ifsq_set_oactive,
ifsq_clr_oactive, and ifsq_is_oactive in dragonflybsd.

this diff includes changes to all the drivers manipulating IFF_OACTIVE
to now use the ifsq_{set,clr_is}_oactive API too.

ok kettenis@ mpi@ jmatthew@ deraadt@


# 1.115 24-Nov-2015 mpi

You only need <net/if_dl.h> if you're using LLADDR() or a sockaddr_dl.


# 1.114 20-Nov-2015 dlg

shuffle struct ifqueue so in flight mbufs are protected by a mutex.

the code is refactored so the IFQ macros call newly implemented ifq
functions. the ifq code is split so each discipline (priq and hfsc
in our case) is an opaque set of operations that the common ifq
code can call. the common code does the locking, accounting (ifq_len
manipulation), and freeing of the mbuf if the disciplines enqueue
function rejects it. theyre kind of like bufqs in the block layer
with their fifo and nscan disciplines.

the new api also supports atomic switching of disciplines at runtime.
the hfsc setup in pf_ioctl.c has been tweaked to build a complete
hfsc_if structure which it attaches to the send queue in a single
operation, rather than attaching to the interface up front and
building up a list of queues.

the send queue is now mutexed, which raises the expectation that
packets can be enqueued or purged on one cpu while another cpu is
dequeueing them in a driver for transmission. a lot of drivers use
IFQ_POLL to peek at an mbuf and attempt to fit it on the ring before
committing to it with a later IFQ_DEQUEUE operation. if the mbuf
gets freed in between the POLL and DEQUEUE operations, fireworks
will ensue.

to avoid this, the ifq api introduces ifq_deq_begin, ifq_deq_rollback,
and ifq_deq_commit. ifq_deq_begin allows a driver to take the ifq
mutex and get a reference to the mbuf they wish to try and tx. if
there's space, they can ifq_deq_commit it to remove the mbuf and
release the mutex. if there's no space, ifq_deq_rollback simply
releases the mutex. this api was developed to make updating the
drivers using IFQ_POLL easy, instead of having to do significant
semantic changes to avoid POLL that we cannot test on all the
hardware.

the common code has been tested pretty hard, and all the driver
modifications are straightforward except for de(4). if that breaks
it can be dealt with later.

ok mpi@ jmatthew@


# 1.113 14-Nov-2015 mpi

Do not include <net/if_vlan_var.h> when it's not necessary.

Because of the VLAN hacks in mpw(4) this file still contains the definition
of "struct ifvlan" which depends on <sys/refcnt.h> which in turns pull
<sys/atomic.h>...


# 1.112 25-Oct-2015 mpi

arp_ifinit() is no longer needed.


Revision tags: OPENBSD_5_8_BASE
# 1.111 24-Jun-2015 mpi

Increment if_ipackets in if_input().

Note that pseudo-drivers not using if_input() are not affected by this
conversion.

ok mikeb@, kettenis@, claudio@, dlg@


# 1.110 20-Mar-2015 mpi

Convert to if_input(), thanks to krw@ for testing.


# 1.109 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.108 22-Dec-2014 tedu

unifdef INET


# 1.107 20-Aug-2014 dlg

after allocating an mbuf and cluster you still need to init the length
fields.

found by steven roberts, who also tested this fix for me


# 1.106 20-Aug-2014 dlg

remove the custom jumbo allocator. its never been enabled or used.

putting this into the tree to make it easier to test.


Revision tags: OPENBSD_5_6_BASE
# 1.105 22-Jul-2014 mpi

Fewer <netinet/in_systm.h>


Revision tags: OPENBSD_5_5_BASE
# 1.104 28-Dec-2013 deraadt

The few network drivers that called their children's (ie. mii PHY
drivers) activate functions at DVACT_RESUME time do not need to do
so, since their PHYs are repaired by IFF_UP.


# 1.103 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.102 07-Aug-2013 bluhm

Most network drivers include netinet/in_var.h, but apparently they
don't have to. Just remove these include lines.
Compiled on amd64 i386 sparc64; OK henning@ mikeb@


Revision tags: OPENBSD_5_4_BASE
# 1.101 01-Apr-2013 brad

Rewrite receive filter and ioctl handling code.

ok jsg@


Revision tags: OPENBSD_5_3_BASE
# 1.100 29-Nov-2012 brad

Remove setting an initial assumed baudrate upon driver attach which is not
necessarily correct, there might not even be a link when attaching.

ok mikeb@ reyk@


# 1.99 31-Aug-2012 stsp

Wake on LAN support for nfe(4). Tested by me, russel on misc@, and jsg.
ok jsg


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.98 05-Apr-2011 henning

mechanic rename M_{TCP|UDP}V4_CSUM_OUT -> M_{TCP|UDP}_CSUM_OUT
ok claudio krw


Revision tags: OPENBSD_4_9_BASE
# 1.97 10-Jan-2011 kettenis

Some nfe(4)/rlphy(4) combos don't work, because the PHY responds to all
addresses on the mii bus. As a countereasure, only attach the first PHY we
encounter. It is very unlikely we're going to ever see nfe(4) with multiple
PHYs. The same is probably true for any modern NIC.

ok mikeb@, deraadt@


# 1.96 07-Sep-2010 deraadt

remove the powerhook code. All architectures now use the ca_activate tree
traversal code to suspend/resume
ok oga kettenis blambert


# 1.95 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.94 31-Aug-2010 deraadt

activate function should return result of config_activate_children


# 1.93 27-Aug-2010 deraadt

Make the powerhook use the activate functions, which actually do the full
job.


# 1.92 27-Aug-2010 jsg

remove the unused if_init callback in struct ifnet
ok deraadt@ henning@ claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.91 06-Aug-2010 mlarkin

ca_activate function for nfe(4) for suspend/resume
tested on nvidia mcp51

ok deraadt@


# 1.90 19-May-2010 oga

BUS_DMA_ZERO instead of alloc, map, bzero.

ok krw@


Revision tags: OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.89 18-Jun-2009 jsg

Remove support for cancelled NVIDIA MCP7B and add initial support for
MCP89.

From Brad based on information from Peer Chen @ NVIDIA via Linux.


# 1.88 29-Mar-2009 sthen

make various strings ("can't map mem space" and similar) more consistent
between instances, saving space in the kernel. feedback from many (some
incorporated, some left for future work).

ok deraadt, kettenis, "why not" miod.


Revision tags: OPENBSD_4_5_BASE
# 1.87 28-Nov-2008 brad

Eliminate the redundant bits of code for MTU and multicast handling
from the individual drivers now that ether_ioctl() handles this.

Shrinks the i386 kernels by..
RAMDISK - 2176 bytes
RAMDISKB - 1504 bytes
RAMDISKC - 736 bytes

Tested by naddy@/okan@/sthen@/brad@/todd@/jmc@ and lots of users.
Build tested on almost all archs by todd@/brad@

ok naddy@


# 1.86 09-Nov-2008 naddy

Introduce bpf_mtap_ether(), which for the benefit of bpf listeners
creates the VLAN encapsulation from the tag stored in the mbuf
header. Idea from FreeBSD, input from claudio@ and canacar@.

Switch all hardware VLAN enabled drivers to the new function.

ok claudio@


# 1.85 28-Oct-2008 brad

Remove #if NVLAN.


# 1.84 28-Oct-2008 brad

Re-add support for RX VLAN tag stripping.


# 1.83 16-Oct-2008 naddy

Switch the existing TX VLAN hardware support over to having the
tag in the header. Convert TX tagging in the drivers.

Help and ok brad@


# 1.82 02-Oct-2008 brad

First step towards cleaning up the Ethernet driver ioctl handling.
Move calling ether_ioctl() from the top of the ioctl function, which
at the moment does absolutely nothing, to the default switch case.
Thus allowing drivers to define their own ioctl handlers and then
falling back on ether_ioctl(). The only functional change this results
in at the moment is having all Ethernet drivers returning the proper
errno of ENOTTY instead of EINVAL/ENXIO when encountering unknown
ioctl's.

Shrinks the i386 kernels by..
RAMDISK - 1024 bytes
RAMDISKB - 1120 bytes
RAMDISKC - 832 bytes

Tested by martin@/jsing@/todd@/brad@
Build tested on almost all archs by todd@/brad@

ok jsing@


# 1.81 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


# 1.80 09-Aug-2008 brad

MCP79 are also capable of Jumbo frames. Add the Jumbo support flag.

From: Linux forcedeth


Revision tags: OPENBSD_4_4_BASE
# 1.79 23-May-2008 brad

Simplify the combination use of pci_mapreg_type()/pci_mapreg_map() as
suggested by dlg@ awhile ago.

ok dlg@


# 1.78 19-May-2008 fgsch

correct format after %x -> %b convertion. deraadt jsg ok.


Revision tags: OPENBSD_4_3_BASE
# 1.77 05-Feb-2008 brad

Add the NFE_CORRECT_MACADDR flag for MCP77/79 chipsets.

ok jsg@ dlg@


# 1.76 02-Jan-2008 brad

add the NVIDIA MCP77/79 ids.

ok jsg@ dlg@


# 1.75 11-Dec-2007 mikeb

Fix check for pending interrupts as some other device is causing
the status register to change.

From form@.

ok jsg


# 1.74 05-Dec-2007 jsg

Make sure newer adapters are not in powerdown mode.
From Oleg Safiullin <form@pdp-11.org.ru> similiar to changes
in Linux/FreeBSD driver.


# 1.73 17-Nov-2007 jsg

Disable jumbo allocator until the low number of buffers case
is better handled.

This will likely resolve stalls some people are seeing under high
load.

ok damien@


# 1.72 12-Sep-2007 jsg

In nfe_start() do a fast return if IFF_OACTIVE is set, in
this case we need a Tx interrupt to clean up the DMA ring
before if_start can be properly called.

Diff based on a comment by & ok claudio@


# 1.71 07-Sep-2007 damien

use new malloc M_ZERO flag to shrink kernel.
remove <malloc.h> from files where malloc is not used.


# 1.70 01-Sep-2007 ckuethe

Correct the backwards ethernet address that some NVidia MACs have.
diff from brad. "commit this" jsg


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.69 02-Mar-2007 jsg

MCP65 has no hardware checksum support.
Diff from brad after he noticed recent changes
in the Linux driver by an NVIDIA employee.


# 1.68 08-Jan-2007 damien

Add support for HW TCP/IP checksum offload for adapters that support it.

Tested by many (IP/UDP/TCP):

Jason McIntyre <jmc@>
Chris Kuethe <chris.kuethe AT gmail.com>
Alf Schlichting <a.schlichting AT lemarit.com>
Rodolfo Gouveia <rgouveia AT cosmico.net>
Peter Stromberg <wilfried@>

Has been in snaps for weeks too with noone complaining so far.


# 1.67 15-Nov-2006 brad

Correct the WOL magic value and rename NFE_WOL_MAGIC to NFE_WOL_ENABLE.

WOL magic value from Peer Chen@NVIDIA via FreeBSD.

ok jsg@ damien@


# 1.66 10-Nov-2006 damien

Defer setting of the valid bit in the first TX descriptor after
all descriptors have been setup. Otherwise, hardware may start
processing descriptors faster than us and crap out.
Fixes "watchdog timeout" errors.

Original idea from Matthew Dillon @DragonFly.

ok deraadt@ jsg@ wim@
tested by many


# 1.65 05-Nov-2006 brad

add the NVIDIA MCP67 LAN PCI ids.


Revision tags: OPENBSD_4_0_BASE
# 1.64 23-Jul-2006 brad

branches: 1.64.2;
add NVidia MCP61/65 ids.


# 1.63 17-Jun-2006 brad

add sys/timeout.h


# 1.62 29-May-2006 brad

garbage collect NFE_NO_JUMBO.


# 1.61 28-May-2006 brad

- remove ETHER_MAX_LEN_JUMBO and ETHERMTU_JUMBO.
- use if_hardmtu for MTU ioctl handlers.

ok reyk@


# 1.60 28-May-2006 jason

unknown ioctl is ENOTTY not EINVAL


# 1.59 27-May-2006 brad

remove IFCAP_JUMBO_MTU interface capabilities flag and set if_hardmtu in a few
more drivers.

ok reyk@


# 1.58 20-May-2006 brad

set if_jumbo_mtu and the IFCAP_JUMBO_MTU capabilities flag where
appropriate.

ok reyk@


# 1.57 26-Apr-2006 jsg

Use %b in error flag printfs to describe meaning of error bits.
requested by deraadt@


# 1.56 26-Apr-2006 brad

And commit the diff from the right system.


# 1.55 26-Apr-2006 brad

check for IFF_RUNNING being set before calling nfe_init().

ok damien@ jsg@


# 1.54 07-Apr-2006 jsg

Add work around for mbuf leak in the tx path until we
can come up with a better guess as to how the hardware works.
From Chuck Silvers.
ok damien@


# 1.53 25-Mar-2006 djm

allow bpf(4) to ignore packets based on their direction (inbound or
outbound), using a new BIOCSDIRFILT ioctl;
guidance, feedback and ok canacar@


Revision tags: OPENBSD_3_9_BASE
# 1.52 02-Mar-2006 jsg

branches: 1.52.2;
Put the PHY update/reset call in nfe_init back to the spot it was at
before Rx/Tx/Interrupts are enabled.
This makes <fredd at cse.sc.edu>'s nfe+icsphy setup work again.

Tested on nfe+eephy by otto@ and myself, nfe+ciphy by otto@ and
nfe+rlphy by wilfried@

ok deraadt@


# 1.51 26-Feb-2006 damien

use sc->sc_dev.dv_xname consistently.


# 1.50 26-Feb-2006 krw

Let if_nfe.c compile again by putting declaration of ifp inside #ifdef
NFE_DEBUG.

ok dlg@


# 1.49 26-Feb-2006 jsg

Don't define NFE_DEBUG by default.
Set default debug level to 0.
Make sure to include interface or function name in debug strings.
Print Tx errors from the MAC when debug is not on.


# 1.48 24-Feb-2006 brad

check for M_PKTHDR.


# 1.47 22-Feb-2006 damien

fix nfe_txeof() to reset the wathdog timeout only when a full tx frame
has been sent.


# 1.46 22-Feb-2006 damien

re-enable interrupt mitigation and mask out NFE_IRQ_TIMER that was causing
interrupts flood.


# 1.45 22-Feb-2006 brad

update the media settings after MAC setup.


# 1.44 21-Feb-2006 damien

disable interrupt mitigation until i figure out why we're spending 10% of
CPU time in interrupts with it.


# 1.43 20-Feb-2006 damien

- remove mbuf linearization code. it is broken in this context and it is very
unlikely that we will see mbuf chains with 62 fragments anytime soon
(anyway, it would not crash, it would just freeze TX).
- fix max scatter value so we don't end up filling the ring with one mbuf
chain.


# 1.42 19-Feb-2006 damien

- fix h/w VLAN tagging and enable it for adapters that support it (VLAN tag
stripping job is left to the network stack).
- enable interrupt mitigation by default.
- add some magic to the initialization sequence in the hope that it will fix
TX issues seen on some adapters.


# 1.41 16-Feb-2006 damien

- stop enabling/disabling interrupts in nfe_intr().
- store RX/TX settings in nfe_softc so we don't recompute them all the time.
- fix h/w VLAN tagging flags.


# 1.40 15-Feb-2006 brad

move setting IFCAP_VLAN_MTU back to where it is supposed to be.


# 1.39 15-Feb-2006 damien

add support for 64bit rings base addresses.


# 1.38 15-Feb-2006 damien

actually disable h/w VLAN tagging for now.


# 1.37 15-Feb-2006 damien

- enable jumbo frames for adapters that support it.
- extend TX DMA mappings size from MCLBYTES to NFE_JBYTES.
- add initial (disabled) bits for interrupts mitigation.
- add initial (disabled) bits for h/w VLAN tagging.
- did some consistency tweaks while i'm here.


# 1.36 13-Feb-2006 brad

use pci_mapreg_type().

ok damien@


# 1.35 13-Feb-2006 brad

nVidia/nvidia -> NVIDIA


# 1.34 12-Feb-2006 damien

fix a printf


# 1.33 12-Feb-2006 damien

don't print shared interrupts when debugging is on.


# 1.32 12-Feb-2006 damien

use BUS_DMA_READ flag when mapping RX buffers.


# 1.31 12-Feb-2006 damien

reduce the number of DMA sync operations by sincing multiple TX descriptors
at once.


# 1.30 12-Feb-2006 damien

initial jumbo frames support (disabled for now).
#define'ing NFE_NO_JUMBO can save a few hundred KB of wired memory.


# 1.29 11-Feb-2006 brad

set IFCAP_VLAN_MTU in the capabilites field.

ok damien@


# 1.28 11-Feb-2006 damien

- call nfe_ifmedia_upd() early in nfe_init() to reset the PHY.
- set RX buffer size register properly


# 1.27 11-Feb-2006 damien

fix nfe_free_tx_ring() and nfe_reset_tx_ring() to unmap the good items.


# 1.26 11-Feb-2006 damien

don't free the mbuf in nfe_encap() if it can't be DMA'mapped since it is
left in if_snd. prettify nfe_start() a bit while i'm here.


# 1.25 11-Feb-2006 damien

MCP51 boards don't support jumbo frames..


# 1.24 11-Feb-2006 damien

force a wakeup of the MAC in nfe_init().
this makes my MCP51 board working.

committed over a nfe.


# 1.23 10-Feb-2006 brad

Only kick the TX engine once in nfe_start() and only if there are
packets to be transmitted, after iterating through the queue and
queuing up as many packets as possible as opposed to kicking
it each time through nfe_encap().

ok damien@ jsg@


# 1.22 08-Feb-2006 jsg

Add back minimal debugging to help track down tx errors some MACs
seem to be reporting.


# 1.21 08-Feb-2006 jsg

Use __LP64__ instead of __amd64__ for portability.
Pointed out by miod@.


# 1.20 07-Feb-2006 jsg

Disable checksum offload for the moment as it appears to not
work for some people.


# 1.19 05-Feb-2006 brad

Simplify SIOCSIFADDR switch case a bit.


# 1.18 05-Feb-2006 brad

Eliminate a reset when configuring the IP address.

ok jsg@


# 1.17 05-Feb-2006 jsg

Don't force NFE_DEBUG, set default debug level to 0.
Committed via an nfe.


# 1.16 05-Feb-2006 damien

quiet nfe_intr().


# 1.15 05-Feb-2006 damien

unbreak the tree.

pointed at by Peter Stromberg.


# 1.14 05-Feb-2006 damien

fix nfe_txeof().
with these changes, the connection seems to work just fine.

with help from jsg@


# 1.13 04-Feb-2006 damien

- start MII timer in nfe_init() and move timer initialization in nfe_attach()
- set RX filter before enabling RX in nfe_init()
- call mii_down() in nfe_stop()
- fix setting of full/half-duplex mode
- call mii_phy_reset() for each PHY attached and call mii_mediachg() in
nfe_ifmedia_upd()
- some cleaning while i'm here


# 1.12 04-Feb-2006 damien

fix clearing of h/w csum flags in all but the first fragment


# 1.11 04-Feb-2006 damien

handle link state change interrupts but do nothing for now


# 1.10 04-Feb-2006 damien

- add support for multicast filters.
- fix setting of if_capabilities flags for chips supporting checksum offload.
- fix dmesg output in case we can't establish the intr handler.
- fix a call to bus_dmamap_unload() in nfe_encap().


# 1.9 22-Jan-2006 damien

fixes miibus_{read,write}reg routines.
this resolves the ghost ukphy problem.
did some cleanup while i'm here.

tested by and ok jsg@


# 1.8 20-Jan-2006 brad

remove redundant code from nfe_attach().

ok jsg@


# 1.7 18-Jan-2006 damien

initial Tx/Rx bits. not working yet.

joint work with jsg@


# 1.6 15-Jan-2006 damien

fix dma mapping of tx ring.


# 1.5 14-Jan-2006 jsg

Make sure interrupt is properly ack'd.
From damien@


# 1.4 17-Dec-2005 jsg

Don't dynamically assign ring size, simplifies ring structures
and code that deals with rings. We can get away with this
as we only have one type of rx and one type of tx ring at a time
unlike ral(4).

Suggested by and ok damien@


# 1.3 17-Dec-2005 jsg

Split length and flags up into seperate variables in
descriptors and make use of MCLBYTES for length setting.
Sugested by damien@


# 1.2 14-Dec-2005 jsg

We aren't likely to need to keep track of crypted/decrypted ring
bits any time soon... pointed out by damien@


# 1.1 14-Dec-2005 jsg

Initial bits for an nvidia nforce Ethernet driver.
bus_dma usage modelled after ral. Does not yet see rx interrupts
when testing with ck804.

Nvidia won't give out documentation for this, various "free" operating
systems include a closed source driver, and the Linux people who reverse
engineered it to create a specification won't give it out.


# 1.123 12-Dec-2020 jan

Rename the macro MCLGETI to MCLGETL and removes the dead parameter ifp.

OK dlg@, bluhm@
No Opinion mpi@
Not against it claudio@


Revision tags: OPENBSD_6_8_BASE
# 1.122 10-Jul-2020 patrick

Change users of IFQ_SET_MAXLEN() and IFQ_IS_EMPTY() to use the "new" API.

ok dlg@ tobhe@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.121 25-Sep-2019 kevlo

Argument order fix for MCLGETI.

ok claudio@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.120 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.119 22-Jan-2017 dlg

move counting if_opackets next to counting if_obytes in if_enqueue.

this means packets are consistently counted in one place, unlike the
many and various ways that drivers thought they should do it.

ok mpi@ deraadt@


# 1.118 29-Nov-2016 jsg

m_free() and m_freem() test for NULL. Simplify callers which had their own
NULL tests.

ok mpi@


Revision tags: OPENBSD_6_0_BASE
# 1.117 13-Apr-2016 mpi

G/C IFQ_SET_READY().


Revision tags: OPENBSD_5_9_BASE
# 1.116 25-Nov-2015 dlg

replace IFF_OACTIVE manipulation with mpsafe operations.

there are two things shared between the network stack and drivers
in the send path: the send queue and the IFF_OACTIVE flag. the send
queue is now protected by a mutex. this diff makes the oactive
functionality mpsafe too.

IFF_OACTIVE is part of if_flags. there are two problems with that.
firstly, if_flags is a short and we dont have any MI atomic operations
to manipulate a short. secondly, while we could make the IFF_OACTIVE
operates mpsafe, all changes to other flags would have to be made
safe at the same time, otherwise a read-modify-write cycle on their
updates could clobber the oactive change.

instead, this moves the oactive mark into struct ifqueue and provides
an API for changing it. there's ifq_set_oactive, ifq_clr_oactive,
and ifq_is_oactive. these are modelled on ifsq_set_oactive,
ifsq_clr_oactive, and ifsq_is_oactive in dragonflybsd.

this diff includes changes to all the drivers manipulating IFF_OACTIVE
to now use the ifsq_{set,clr_is}_oactive API too.

ok kettenis@ mpi@ jmatthew@ deraadt@


# 1.115 24-Nov-2015 mpi

You only need <net/if_dl.h> if you're using LLADDR() or a sockaddr_dl.


# 1.114 20-Nov-2015 dlg

shuffle struct ifqueue so in flight mbufs are protected by a mutex.

the code is refactored so the IFQ macros call newly implemented ifq
functions. the ifq code is split so each discipline (priq and hfsc
in our case) is an opaque set of operations that the common ifq
code can call. the common code does the locking, accounting (ifq_len
manipulation), and freeing of the mbuf if the disciplines enqueue
function rejects it. theyre kind of like bufqs in the block layer
with their fifo and nscan disciplines.

the new api also supports atomic switching of disciplines at runtime.
the hfsc setup in pf_ioctl.c has been tweaked to build a complete
hfsc_if structure which it attaches to the send queue in a single
operation, rather than attaching to the interface up front and
building up a list of queues.

the send queue is now mutexed, which raises the expectation that
packets can be enqueued or purged on one cpu while another cpu is
dequeueing them in a driver for transmission. a lot of drivers use
IFQ_POLL to peek at an mbuf and attempt to fit it on the ring before
committing to it with a later IFQ_DEQUEUE operation. if the mbuf
gets freed in between the POLL and DEQUEUE operations, fireworks
will ensue.

to avoid this, the ifq api introduces ifq_deq_begin, ifq_deq_rollback,
and ifq_deq_commit. ifq_deq_begin allows a driver to take the ifq
mutex and get a reference to the mbuf they wish to try and tx. if
there's space, they can ifq_deq_commit it to remove the mbuf and
release the mutex. if there's no space, ifq_deq_rollback simply
releases the mutex. this api was developed to make updating the
drivers using IFQ_POLL easy, instead of having to do significant
semantic changes to avoid POLL that we cannot test on all the
hardware.

the common code has been tested pretty hard, and all the driver
modifications are straightforward except for de(4). if that breaks
it can be dealt with later.

ok mpi@ jmatthew@


# 1.113 14-Nov-2015 mpi

Do not include <net/if_vlan_var.h> when it's not necessary.

Because of the VLAN hacks in mpw(4) this file still contains the definition
of "struct ifvlan" which depends on <sys/refcnt.h> which in turns pull
<sys/atomic.h>...


# 1.112 25-Oct-2015 mpi

arp_ifinit() is no longer needed.


Revision tags: OPENBSD_5_8_BASE
# 1.111 24-Jun-2015 mpi

Increment if_ipackets in if_input().

Note that pseudo-drivers not using if_input() are not affected by this
conversion.

ok mikeb@, kettenis@, claudio@, dlg@


# 1.110 20-Mar-2015 mpi

Convert to if_input(), thanks to krw@ for testing.


# 1.109 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.108 22-Dec-2014 tedu

unifdef INET


# 1.107 20-Aug-2014 dlg

after allocating an mbuf and cluster you still need to init the length
fields.

found by steven roberts, who also tested this fix for me


# 1.106 20-Aug-2014 dlg

remove the custom jumbo allocator. its never been enabled or used.

putting this into the tree to make it easier to test.


Revision tags: OPENBSD_5_6_BASE
# 1.105 22-Jul-2014 mpi

Fewer <netinet/in_systm.h>


Revision tags: OPENBSD_5_5_BASE
# 1.104 28-Dec-2013 deraadt

The few network drivers that called their children's (ie. mii PHY
drivers) activate functions at DVACT_RESUME time do not need to do
so, since their PHYs are repaired by IFF_UP.


# 1.103 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.102 07-Aug-2013 bluhm

Most network drivers include netinet/in_var.h, but apparently they
don't have to. Just remove these include lines.
Compiled on amd64 i386 sparc64; OK henning@ mikeb@


Revision tags: OPENBSD_5_4_BASE
# 1.101 01-Apr-2013 brad

Rewrite receive filter and ioctl handling code.

ok jsg@


Revision tags: OPENBSD_5_3_BASE
# 1.100 29-Nov-2012 brad

Remove setting an initial assumed baudrate upon driver attach which is not
necessarily correct, there might not even be a link when attaching.

ok mikeb@ reyk@


# 1.99 31-Aug-2012 stsp

Wake on LAN support for nfe(4). Tested by me, russel on misc@, and jsg.
ok jsg


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.98 05-Apr-2011 henning

mechanic rename M_{TCP|UDP}V4_CSUM_OUT -> M_{TCP|UDP}_CSUM_OUT
ok claudio krw


Revision tags: OPENBSD_4_9_BASE
# 1.97 10-Jan-2011 kettenis

Some nfe(4)/rlphy(4) combos don't work, because the PHY responds to all
addresses on the mii bus. As a countereasure, only attach the first PHY we
encounter. It is very unlikely we're going to ever see nfe(4) with multiple
PHYs. The same is probably true for any modern NIC.

ok mikeb@, deraadt@


# 1.96 07-Sep-2010 deraadt

remove the powerhook code. All architectures now use the ca_activate tree
traversal code to suspend/resume
ok oga kettenis blambert


# 1.95 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.94 31-Aug-2010 deraadt

activate function should return result of config_activate_children


# 1.93 27-Aug-2010 deraadt

Make the powerhook use the activate functions, which actually do the full
job.


# 1.92 27-Aug-2010 jsg

remove the unused if_init callback in struct ifnet
ok deraadt@ henning@ claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.91 06-Aug-2010 mlarkin

ca_activate function for nfe(4) for suspend/resume
tested on nvidia mcp51

ok deraadt@


# 1.90 19-May-2010 oga

BUS_DMA_ZERO instead of alloc, map, bzero.

ok krw@


Revision tags: OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.89 18-Jun-2009 jsg

Remove support for cancelled NVIDIA MCP7B and add initial support for
MCP89.

From Brad based on information from Peer Chen @ NVIDIA via Linux.


# 1.88 29-Mar-2009 sthen

make various strings ("can't map mem space" and similar) more consistent
between instances, saving space in the kernel. feedback from many (some
incorporated, some left for future work).

ok deraadt, kettenis, "why not" miod.


Revision tags: OPENBSD_4_5_BASE
# 1.87 28-Nov-2008 brad

Eliminate the redundant bits of code for MTU and multicast handling
from the individual drivers now that ether_ioctl() handles this.

Shrinks the i386 kernels by..
RAMDISK - 2176 bytes
RAMDISKB - 1504 bytes
RAMDISKC - 736 bytes

Tested by naddy@/okan@/sthen@/brad@/todd@/jmc@ and lots of users.
Build tested on almost all archs by todd@/brad@

ok naddy@


# 1.86 09-Nov-2008 naddy

Introduce bpf_mtap_ether(), which for the benefit of bpf listeners
creates the VLAN encapsulation from the tag stored in the mbuf
header. Idea from FreeBSD, input from claudio@ and canacar@.

Switch all hardware VLAN enabled drivers to the new function.

ok claudio@


# 1.85 28-Oct-2008 brad

Remove #if NVLAN.


# 1.84 28-Oct-2008 brad

Re-add support for RX VLAN tag stripping.


# 1.83 16-Oct-2008 naddy

Switch the existing TX VLAN hardware support over to having the
tag in the header. Convert TX tagging in the drivers.

Help and ok brad@


# 1.82 02-Oct-2008 brad

First step towards cleaning up the Ethernet driver ioctl handling.
Move calling ether_ioctl() from the top of the ioctl function, which
at the moment does absolutely nothing, to the default switch case.
Thus allowing drivers to define their own ioctl handlers and then
falling back on ether_ioctl(). The only functional change this results
in at the moment is having all Ethernet drivers returning the proper
errno of ENOTTY instead of EINVAL/ENXIO when encountering unknown
ioctl's.

Shrinks the i386 kernels by..
RAMDISK - 1024 bytes
RAMDISKB - 1120 bytes
RAMDISKC - 832 bytes

Tested by martin@/jsing@/todd@/brad@
Build tested on almost all archs by todd@/brad@

ok jsing@


# 1.81 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


# 1.80 09-Aug-2008 brad

MCP79 are also capable of Jumbo frames. Add the Jumbo support flag.

From: Linux forcedeth


Revision tags: OPENBSD_4_4_BASE
# 1.79 23-May-2008 brad

Simplify the combination use of pci_mapreg_type()/pci_mapreg_map() as
suggested by dlg@ awhile ago.

ok dlg@


# 1.78 19-May-2008 fgsch

correct format after %x -> %b convertion. deraadt jsg ok.


Revision tags: OPENBSD_4_3_BASE
# 1.77 05-Feb-2008 brad

Add the NFE_CORRECT_MACADDR flag for MCP77/79 chipsets.

ok jsg@ dlg@


# 1.76 02-Jan-2008 brad

add the NVIDIA MCP77/79 ids.

ok jsg@ dlg@


# 1.75 11-Dec-2007 mikeb

Fix check for pending interrupts as some other device is causing
the status register to change.

From form@.

ok jsg


# 1.74 05-Dec-2007 jsg

Make sure newer adapters are not in powerdown mode.
From Oleg Safiullin <form@pdp-11.org.ru> similiar to changes
in Linux/FreeBSD driver.


# 1.73 17-Nov-2007 jsg

Disable jumbo allocator until the low number of buffers case
is better handled.

This will likely resolve stalls some people are seeing under high
load.

ok damien@


# 1.72 12-Sep-2007 jsg

In nfe_start() do a fast return if IFF_OACTIVE is set, in
this case we need a Tx interrupt to clean up the DMA ring
before if_start can be properly called.

Diff based on a comment by & ok claudio@


# 1.71 07-Sep-2007 damien

use new malloc M_ZERO flag to shrink kernel.
remove <malloc.h> from files where malloc is not used.


# 1.70 01-Sep-2007 ckuethe

Correct the backwards ethernet address that some NVidia MACs have.
diff from brad. "commit this" jsg


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.69 02-Mar-2007 jsg

MCP65 has no hardware checksum support.
Diff from brad after he noticed recent changes
in the Linux driver by an NVIDIA employee.


# 1.68 08-Jan-2007 damien

Add support for HW TCP/IP checksum offload for adapters that support it.

Tested by many (IP/UDP/TCP):

Jason McIntyre <jmc@>
Chris Kuethe <chris.kuethe AT gmail.com>
Alf Schlichting <a.schlichting AT lemarit.com>
Rodolfo Gouveia <rgouveia AT cosmico.net>
Peter Stromberg <wilfried@>

Has been in snaps for weeks too with noone complaining so far.


# 1.67 15-Nov-2006 brad

Correct the WOL magic value and rename NFE_WOL_MAGIC to NFE_WOL_ENABLE.

WOL magic value from Peer Chen@NVIDIA via FreeBSD.

ok jsg@ damien@


# 1.66 10-Nov-2006 damien

Defer setting of the valid bit in the first TX descriptor after
all descriptors have been setup. Otherwise, hardware may start
processing descriptors faster than us and crap out.
Fixes "watchdog timeout" errors.

Original idea from Matthew Dillon @DragonFly.

ok deraadt@ jsg@ wim@
tested by many


# 1.65 05-Nov-2006 brad

add the NVIDIA MCP67 LAN PCI ids.


Revision tags: OPENBSD_4_0_BASE
# 1.64 23-Jul-2006 brad

branches: 1.64.2;
add NVidia MCP61/65 ids.


# 1.63 17-Jun-2006 brad

add sys/timeout.h


# 1.62 29-May-2006 brad

garbage collect NFE_NO_JUMBO.


# 1.61 28-May-2006 brad

- remove ETHER_MAX_LEN_JUMBO and ETHERMTU_JUMBO.
- use if_hardmtu for MTU ioctl handlers.

ok reyk@


# 1.60 28-May-2006 jason

unknown ioctl is ENOTTY not EINVAL


# 1.59 27-May-2006 brad

remove IFCAP_JUMBO_MTU interface capabilities flag and set if_hardmtu in a few
more drivers.

ok reyk@


# 1.58 20-May-2006 brad

set if_jumbo_mtu and the IFCAP_JUMBO_MTU capabilities flag where
appropriate.

ok reyk@


# 1.57 26-Apr-2006 jsg

Use %b in error flag printfs to describe meaning of error bits.
requested by deraadt@


# 1.56 26-Apr-2006 brad

And commit the diff from the right system.


# 1.55 26-Apr-2006 brad

check for IFF_RUNNING being set before calling nfe_init().

ok damien@ jsg@


# 1.54 07-Apr-2006 jsg

Add work around for mbuf leak in the tx path until we
can come up with a better guess as to how the hardware works.
From Chuck Silvers.
ok damien@


# 1.53 25-Mar-2006 djm

allow bpf(4) to ignore packets based on their direction (inbound or
outbound), using a new BIOCSDIRFILT ioctl;
guidance, feedback and ok canacar@


Revision tags: OPENBSD_3_9_BASE
# 1.52 02-Mar-2006 jsg

branches: 1.52.2;
Put the PHY update/reset call in nfe_init back to the spot it was at
before Rx/Tx/Interrupts are enabled.
This makes <fredd at cse.sc.edu>'s nfe+icsphy setup work again.

Tested on nfe+eephy by otto@ and myself, nfe+ciphy by otto@ and
nfe+rlphy by wilfried@

ok deraadt@


# 1.51 26-Feb-2006 damien

use sc->sc_dev.dv_xname consistently.


# 1.50 26-Feb-2006 krw

Let if_nfe.c compile again by putting declaration of ifp inside #ifdef
NFE_DEBUG.

ok dlg@


# 1.49 26-Feb-2006 jsg

Don't define NFE_DEBUG by default.
Set default debug level to 0.
Make sure to include interface or function name in debug strings.
Print Tx errors from the MAC when debug is not on.


# 1.48 24-Feb-2006 brad

check for M_PKTHDR.


# 1.47 22-Feb-2006 damien

fix nfe_txeof() to reset the wathdog timeout only when a full tx frame
has been sent.


# 1.46 22-Feb-2006 damien

re-enable interrupt mitigation and mask out NFE_IRQ_TIMER that was causing
interrupts flood.


# 1.45 22-Feb-2006 brad

update the media settings after MAC setup.


# 1.44 21-Feb-2006 damien

disable interrupt mitigation until i figure out why we're spending 10% of
CPU time in interrupts with it.


# 1.43 20-Feb-2006 damien

- remove mbuf linearization code. it is broken in this context and it is very
unlikely that we will see mbuf chains with 62 fragments anytime soon
(anyway, it would not crash, it would just freeze TX).
- fix max scatter value so we don't end up filling the ring with one mbuf
chain.


# 1.42 19-Feb-2006 damien

- fix h/w VLAN tagging and enable it for adapters that support it (VLAN tag
stripping job is left to the network stack).
- enable interrupt mitigation by default.
- add some magic to the initialization sequence in the hope that it will fix
TX issues seen on some adapters.


# 1.41 16-Feb-2006 damien

- stop enabling/disabling interrupts in nfe_intr().
- store RX/TX settings in nfe_softc so we don't recompute them all the time.
- fix h/w VLAN tagging flags.


# 1.40 15-Feb-2006 brad

move setting IFCAP_VLAN_MTU back to where it is supposed to be.


# 1.39 15-Feb-2006 damien

add support for 64bit rings base addresses.


# 1.38 15-Feb-2006 damien

actually disable h/w VLAN tagging for now.


# 1.37 15-Feb-2006 damien

- enable jumbo frames for adapters that support it.
- extend TX DMA mappings size from MCLBYTES to NFE_JBYTES.
- add initial (disabled) bits for interrupts mitigation.
- add initial (disabled) bits for h/w VLAN tagging.
- did some consistency tweaks while i'm here.


# 1.36 13-Feb-2006 brad

use pci_mapreg_type().

ok damien@


# 1.35 13-Feb-2006 brad

nVidia/nvidia -> NVIDIA


# 1.34 12-Feb-2006 damien

fix a printf


# 1.33 12-Feb-2006 damien

don't print shared interrupts when debugging is on.


# 1.32 12-Feb-2006 damien

use BUS_DMA_READ flag when mapping RX buffers.


# 1.31 12-Feb-2006 damien

reduce the number of DMA sync operations by sincing multiple TX descriptors
at once.


# 1.30 12-Feb-2006 damien

initial jumbo frames support (disabled for now).
#define'ing NFE_NO_JUMBO can save a few hundred KB of wired memory.


# 1.29 11-Feb-2006 brad

set IFCAP_VLAN_MTU in the capabilites field.

ok damien@


# 1.28 11-Feb-2006 damien

- call nfe_ifmedia_upd() early in nfe_init() to reset the PHY.
- set RX buffer size register properly


# 1.27 11-Feb-2006 damien

fix nfe_free_tx_ring() and nfe_reset_tx_ring() to unmap the good items.


# 1.26 11-Feb-2006 damien

don't free the mbuf in nfe_encap() if it can't be DMA'mapped since it is
left in if_snd. prettify nfe_start() a bit while i'm here.


# 1.25 11-Feb-2006 damien

MCP51 boards don't support jumbo frames..


# 1.24 11-Feb-2006 damien

force a wakeup of the MAC in nfe_init().
this makes my MCP51 board working.

committed over a nfe.


# 1.23 10-Feb-2006 brad

Only kick the TX engine once in nfe_start() and only if there are
packets to be transmitted, after iterating through the queue and
queuing up as many packets as possible as opposed to kicking
it each time through nfe_encap().

ok damien@ jsg@


# 1.22 08-Feb-2006 jsg

Add back minimal debugging to help track down tx errors some MACs
seem to be reporting.


# 1.21 08-Feb-2006 jsg

Use __LP64__ instead of __amd64__ for portability.
Pointed out by miod@.


# 1.20 07-Feb-2006 jsg

Disable checksum offload for the moment as it appears to not
work for some people.


# 1.19 05-Feb-2006 brad

Simplify SIOCSIFADDR switch case a bit.


# 1.18 05-Feb-2006 brad

Eliminate a reset when configuring the IP address.

ok jsg@


# 1.17 05-Feb-2006 jsg

Don't force NFE_DEBUG, set default debug level to 0.
Committed via an nfe.


# 1.16 05-Feb-2006 damien

quiet nfe_intr().


# 1.15 05-Feb-2006 damien

unbreak the tree.

pointed at by Peter Stromberg.


# 1.14 05-Feb-2006 damien

fix nfe_txeof().
with these changes, the connection seems to work just fine.

with help from jsg@


# 1.13 04-Feb-2006 damien

- start MII timer in nfe_init() and move timer initialization in nfe_attach()
- set RX filter before enabling RX in nfe_init()
- call mii_down() in nfe_stop()
- fix setting of full/half-duplex mode
- call mii_phy_reset() for each PHY attached and call mii_mediachg() in
nfe_ifmedia_upd()
- some cleaning while i'm here


# 1.12 04-Feb-2006 damien

fix clearing of h/w csum flags in all but the first fragment


# 1.11 04-Feb-2006 damien

handle link state change interrupts but do nothing for now


# 1.10 04-Feb-2006 damien

- add support for multicast filters.
- fix setting of if_capabilities flags for chips supporting checksum offload.
- fix dmesg output in case we can't establish the intr handler.
- fix a call to bus_dmamap_unload() in nfe_encap().


# 1.9 22-Jan-2006 damien

fixes miibus_{read,write}reg routines.
this resolves the ghost ukphy problem.
did some cleanup while i'm here.

tested by and ok jsg@


# 1.8 20-Jan-2006 brad

remove redundant code from nfe_attach().

ok jsg@


# 1.7 18-Jan-2006 damien

initial Tx/Rx bits. not working yet.

joint work with jsg@


# 1.6 15-Jan-2006 damien

fix dma mapping of tx ring.


# 1.5 14-Jan-2006 jsg

Make sure interrupt is properly ack'd.
From damien@


# 1.4 17-Dec-2005 jsg

Don't dynamically assign ring size, simplifies ring structures
and code that deals with rings. We can get away with this
as we only have one type of rx and one type of tx ring at a time
unlike ral(4).

Suggested by and ok damien@


# 1.3 17-Dec-2005 jsg

Split length and flags up into seperate variables in
descriptors and make use of MCLBYTES for length setting.
Sugested by damien@


# 1.2 14-Dec-2005 jsg

We aren't likely to need to keep track of crypted/decrypted ring
bits any time soon... pointed out by damien@


# 1.1 14-Dec-2005 jsg

Initial bits for an nvidia nforce Ethernet driver.
bus_dma usage modelled after ral. Does not yet see rx interrupts
when testing with ck804.

Nvidia won't give out documentation for this, various "free" operating
systems include a closed source driver, and the Linux people who reverse
engineered it to create a specification won't give it out.


# 1.122 10-Jul-2020 patrick

Change users of IFQ_SET_MAXLEN() and IFQ_IS_EMPTY() to use the "new" API.

ok dlg@ tobhe@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.121 25-Sep-2019 kevlo

Argument order fix for MCLGETI.

ok claudio@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.120 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.119 22-Jan-2017 dlg

move counting if_opackets next to counting if_obytes in if_enqueue.

this means packets are consistently counted in one place, unlike the
many and various ways that drivers thought they should do it.

ok mpi@ deraadt@


# 1.118 29-Nov-2016 jsg

m_free() and m_freem() test for NULL. Simplify callers which had their own
NULL tests.

ok mpi@


Revision tags: OPENBSD_6_0_BASE
# 1.117 13-Apr-2016 mpi

G/C IFQ_SET_READY().


Revision tags: OPENBSD_5_9_BASE
# 1.116 25-Nov-2015 dlg

replace IFF_OACTIVE manipulation with mpsafe operations.

there are two things shared between the network stack and drivers
in the send path: the send queue and the IFF_OACTIVE flag. the send
queue is now protected by a mutex. this diff makes the oactive
functionality mpsafe too.

IFF_OACTIVE is part of if_flags. there are two problems with that.
firstly, if_flags is a short and we dont have any MI atomic operations
to manipulate a short. secondly, while we could make the IFF_OACTIVE
operates mpsafe, all changes to other flags would have to be made
safe at the same time, otherwise a read-modify-write cycle on their
updates could clobber the oactive change.

instead, this moves the oactive mark into struct ifqueue and provides
an API for changing it. there's ifq_set_oactive, ifq_clr_oactive,
and ifq_is_oactive. these are modelled on ifsq_set_oactive,
ifsq_clr_oactive, and ifsq_is_oactive in dragonflybsd.

this diff includes changes to all the drivers manipulating IFF_OACTIVE
to now use the ifsq_{set,clr_is}_oactive API too.

ok kettenis@ mpi@ jmatthew@ deraadt@


# 1.115 24-Nov-2015 mpi

You only need <net/if_dl.h> if you're using LLADDR() or a sockaddr_dl.


# 1.114 20-Nov-2015 dlg

shuffle struct ifqueue so in flight mbufs are protected by a mutex.

the code is refactored so the IFQ macros call newly implemented ifq
functions. the ifq code is split so each discipline (priq and hfsc
in our case) is an opaque set of operations that the common ifq
code can call. the common code does the locking, accounting (ifq_len
manipulation), and freeing of the mbuf if the disciplines enqueue
function rejects it. theyre kind of like bufqs in the block layer
with their fifo and nscan disciplines.

the new api also supports atomic switching of disciplines at runtime.
the hfsc setup in pf_ioctl.c has been tweaked to build a complete
hfsc_if structure which it attaches to the send queue in a single
operation, rather than attaching to the interface up front and
building up a list of queues.

the send queue is now mutexed, which raises the expectation that
packets can be enqueued or purged on one cpu while another cpu is
dequeueing them in a driver for transmission. a lot of drivers use
IFQ_POLL to peek at an mbuf and attempt to fit it on the ring before
committing to it with a later IFQ_DEQUEUE operation. if the mbuf
gets freed in between the POLL and DEQUEUE operations, fireworks
will ensue.

to avoid this, the ifq api introduces ifq_deq_begin, ifq_deq_rollback,
and ifq_deq_commit. ifq_deq_begin allows a driver to take the ifq
mutex and get a reference to the mbuf they wish to try and tx. if
there's space, they can ifq_deq_commit it to remove the mbuf and
release the mutex. if there's no space, ifq_deq_rollback simply
releases the mutex. this api was developed to make updating the
drivers using IFQ_POLL easy, instead of having to do significant
semantic changes to avoid POLL that we cannot test on all the
hardware.

the common code has been tested pretty hard, and all the driver
modifications are straightforward except for de(4). if that breaks
it can be dealt with later.

ok mpi@ jmatthew@


# 1.113 14-Nov-2015 mpi

Do not include <net/if_vlan_var.h> when it's not necessary.

Because of the VLAN hacks in mpw(4) this file still contains the definition
of "struct ifvlan" which depends on <sys/refcnt.h> which in turns pull
<sys/atomic.h>...


# 1.112 25-Oct-2015 mpi

arp_ifinit() is no longer needed.


Revision tags: OPENBSD_5_8_BASE
# 1.111 24-Jun-2015 mpi

Increment if_ipackets in if_input().

Note that pseudo-drivers not using if_input() are not affected by this
conversion.

ok mikeb@, kettenis@, claudio@, dlg@


# 1.110 20-Mar-2015 mpi

Convert to if_input(), thanks to krw@ for testing.


# 1.109 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.108 22-Dec-2014 tedu

unifdef INET


# 1.107 20-Aug-2014 dlg

after allocating an mbuf and cluster you still need to init the length
fields.

found by steven roberts, who also tested this fix for me


# 1.106 20-Aug-2014 dlg

remove the custom jumbo allocator. its never been enabled or used.

putting this into the tree to make it easier to test.


Revision tags: OPENBSD_5_6_BASE
# 1.105 22-Jul-2014 mpi

Fewer <netinet/in_systm.h>


Revision tags: OPENBSD_5_5_BASE
# 1.104 28-Dec-2013 deraadt

The few network drivers that called their children's (ie. mii PHY
drivers) activate functions at DVACT_RESUME time do not need to do
so, since their PHYs are repaired by IFF_UP.


# 1.103 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.102 07-Aug-2013 bluhm

Most network drivers include netinet/in_var.h, but apparently they
don't have to. Just remove these include lines.
Compiled on amd64 i386 sparc64; OK henning@ mikeb@


Revision tags: OPENBSD_5_4_BASE
# 1.101 01-Apr-2013 brad

Rewrite receive filter and ioctl handling code.

ok jsg@


Revision tags: OPENBSD_5_3_BASE
# 1.100 29-Nov-2012 brad

Remove setting an initial assumed baudrate upon driver attach which is not
necessarily correct, there might not even be a link when attaching.

ok mikeb@ reyk@


# 1.99 31-Aug-2012 stsp

Wake on LAN support for nfe(4). Tested by me, russel on misc@, and jsg.
ok jsg


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.98 05-Apr-2011 henning

mechanic rename M_{TCP|UDP}V4_CSUM_OUT -> M_{TCP|UDP}_CSUM_OUT
ok claudio krw


Revision tags: OPENBSD_4_9_BASE
# 1.97 10-Jan-2011 kettenis

Some nfe(4)/rlphy(4) combos don't work, because the PHY responds to all
addresses on the mii bus. As a countereasure, only attach the first PHY we
encounter. It is very unlikely we're going to ever see nfe(4) with multiple
PHYs. The same is probably true for any modern NIC.

ok mikeb@, deraadt@


# 1.96 07-Sep-2010 deraadt

remove the powerhook code. All architectures now use the ca_activate tree
traversal code to suspend/resume
ok oga kettenis blambert


# 1.95 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.94 31-Aug-2010 deraadt

activate function should return result of config_activate_children


# 1.93 27-Aug-2010 deraadt

Make the powerhook use the activate functions, which actually do the full
job.


# 1.92 27-Aug-2010 jsg

remove the unused if_init callback in struct ifnet
ok deraadt@ henning@ claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.91 06-Aug-2010 mlarkin

ca_activate function for nfe(4) for suspend/resume
tested on nvidia mcp51

ok deraadt@


# 1.90 19-May-2010 oga

BUS_DMA_ZERO instead of alloc, map, bzero.

ok krw@


Revision tags: OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.89 18-Jun-2009 jsg

Remove support for cancelled NVIDIA MCP7B and add initial support for
MCP89.

From Brad based on information from Peer Chen @ NVIDIA via Linux.


# 1.88 29-Mar-2009 sthen

make various strings ("can't map mem space" and similar) more consistent
between instances, saving space in the kernel. feedback from many (some
incorporated, some left for future work).

ok deraadt, kettenis, "why not" miod.


Revision tags: OPENBSD_4_5_BASE
# 1.87 28-Nov-2008 brad

Eliminate the redundant bits of code for MTU and multicast handling
from the individual drivers now that ether_ioctl() handles this.

Shrinks the i386 kernels by..
RAMDISK - 2176 bytes
RAMDISKB - 1504 bytes
RAMDISKC - 736 bytes

Tested by naddy@/okan@/sthen@/brad@/todd@/jmc@ and lots of users.
Build tested on almost all archs by todd@/brad@

ok naddy@


# 1.86 09-Nov-2008 naddy

Introduce bpf_mtap_ether(), which for the benefit of bpf listeners
creates the VLAN encapsulation from the tag stored in the mbuf
header. Idea from FreeBSD, input from claudio@ and canacar@.

Switch all hardware VLAN enabled drivers to the new function.

ok claudio@


# 1.85 28-Oct-2008 brad

Remove #if NVLAN.


# 1.84 28-Oct-2008 brad

Re-add support for RX VLAN tag stripping.


# 1.83 16-Oct-2008 naddy

Switch the existing TX VLAN hardware support over to having the
tag in the header. Convert TX tagging in the drivers.

Help and ok brad@


# 1.82 02-Oct-2008 brad

First step towards cleaning up the Ethernet driver ioctl handling.
Move calling ether_ioctl() from the top of the ioctl function, which
at the moment does absolutely nothing, to the default switch case.
Thus allowing drivers to define their own ioctl handlers and then
falling back on ether_ioctl(). The only functional change this results
in at the moment is having all Ethernet drivers returning the proper
errno of ENOTTY instead of EINVAL/ENXIO when encountering unknown
ioctl's.

Shrinks the i386 kernels by..
RAMDISK - 1024 bytes
RAMDISKB - 1120 bytes
RAMDISKC - 832 bytes

Tested by martin@/jsing@/todd@/brad@
Build tested on almost all archs by todd@/brad@

ok jsing@


# 1.81 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


# 1.80 09-Aug-2008 brad

MCP79 are also capable of Jumbo frames. Add the Jumbo support flag.

From: Linux forcedeth


Revision tags: OPENBSD_4_4_BASE
# 1.79 23-May-2008 brad

Simplify the combination use of pci_mapreg_type()/pci_mapreg_map() as
suggested by dlg@ awhile ago.

ok dlg@


# 1.78 19-May-2008 fgsch

correct format after %x -> %b convertion. deraadt jsg ok.


Revision tags: OPENBSD_4_3_BASE
# 1.77 05-Feb-2008 brad

Add the NFE_CORRECT_MACADDR flag for MCP77/79 chipsets.

ok jsg@ dlg@


# 1.76 02-Jan-2008 brad

add the NVIDIA MCP77/79 ids.

ok jsg@ dlg@


# 1.75 11-Dec-2007 mikeb

Fix check for pending interrupts as some other device is causing
the status register to change.

From form@.

ok jsg


# 1.74 05-Dec-2007 jsg

Make sure newer adapters are not in powerdown mode.
From Oleg Safiullin <form@pdp-11.org.ru> similiar to changes
in Linux/FreeBSD driver.


# 1.73 17-Nov-2007 jsg

Disable jumbo allocator until the low number of buffers case
is better handled.

This will likely resolve stalls some people are seeing under high
load.

ok damien@


# 1.72 12-Sep-2007 jsg

In nfe_start() do a fast return if IFF_OACTIVE is set, in
this case we need a Tx interrupt to clean up the DMA ring
before if_start can be properly called.

Diff based on a comment by & ok claudio@


# 1.71 07-Sep-2007 damien

use new malloc M_ZERO flag to shrink kernel.
remove <malloc.h> from files where malloc is not used.


# 1.70 01-Sep-2007 ckuethe

Correct the backwards ethernet address that some NVidia MACs have.
diff from brad. "commit this" jsg


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.69 02-Mar-2007 jsg

MCP65 has no hardware checksum support.
Diff from brad after he noticed recent changes
in the Linux driver by an NVIDIA employee.


# 1.68 08-Jan-2007 damien

Add support for HW TCP/IP checksum offload for adapters that support it.

Tested by many (IP/UDP/TCP):

Jason McIntyre <jmc@>
Chris Kuethe <chris.kuethe AT gmail.com>
Alf Schlichting <a.schlichting AT lemarit.com>
Rodolfo Gouveia <rgouveia AT cosmico.net>
Peter Stromberg <wilfried@>

Has been in snaps for weeks too with noone complaining so far.


# 1.67 15-Nov-2006 brad

Correct the WOL magic value and rename NFE_WOL_MAGIC to NFE_WOL_ENABLE.

WOL magic value from Peer Chen@NVIDIA via FreeBSD.

ok jsg@ damien@


# 1.66 10-Nov-2006 damien

Defer setting of the valid bit in the first TX descriptor after
all descriptors have been setup. Otherwise, hardware may start
processing descriptors faster than us and crap out.
Fixes "watchdog timeout" errors.

Original idea from Matthew Dillon @DragonFly.

ok deraadt@ jsg@ wim@
tested by many


# 1.65 05-Nov-2006 brad

add the NVIDIA MCP67 LAN PCI ids.


Revision tags: OPENBSD_4_0_BASE
# 1.64 23-Jul-2006 brad

branches: 1.64.2;
add NVidia MCP61/65 ids.


# 1.63 17-Jun-2006 brad

add sys/timeout.h


# 1.62 29-May-2006 brad

garbage collect NFE_NO_JUMBO.


# 1.61 28-May-2006 brad

- remove ETHER_MAX_LEN_JUMBO and ETHERMTU_JUMBO.
- use if_hardmtu for MTU ioctl handlers.

ok reyk@


# 1.60 28-May-2006 jason

unknown ioctl is ENOTTY not EINVAL


# 1.59 27-May-2006 brad

remove IFCAP_JUMBO_MTU interface capabilities flag and set if_hardmtu in a few
more drivers.

ok reyk@


# 1.58 20-May-2006 brad

set if_jumbo_mtu and the IFCAP_JUMBO_MTU capabilities flag where
appropriate.

ok reyk@


# 1.57 26-Apr-2006 jsg

Use %b in error flag printfs to describe meaning of error bits.
requested by deraadt@


# 1.56 26-Apr-2006 brad

And commit the diff from the right system.


# 1.55 26-Apr-2006 brad

check for IFF_RUNNING being set before calling nfe_init().

ok damien@ jsg@


# 1.54 07-Apr-2006 jsg

Add work around for mbuf leak in the tx path until we
can come up with a better guess as to how the hardware works.
From Chuck Silvers.
ok damien@


# 1.53 25-Mar-2006 djm

allow bpf(4) to ignore packets based on their direction (inbound or
outbound), using a new BIOCSDIRFILT ioctl;
guidance, feedback and ok canacar@


Revision tags: OPENBSD_3_9_BASE
# 1.52 02-Mar-2006 jsg

branches: 1.52.2;
Put the PHY update/reset call in nfe_init back to the spot it was at
before Rx/Tx/Interrupts are enabled.
This makes <fredd at cse.sc.edu>'s nfe+icsphy setup work again.

Tested on nfe+eephy by otto@ and myself, nfe+ciphy by otto@ and
nfe+rlphy by wilfried@

ok deraadt@


# 1.51 26-Feb-2006 damien

use sc->sc_dev.dv_xname consistently.


# 1.50 26-Feb-2006 krw

Let if_nfe.c compile again by putting declaration of ifp inside #ifdef
NFE_DEBUG.

ok dlg@


# 1.49 26-Feb-2006 jsg

Don't define NFE_DEBUG by default.
Set default debug level to 0.
Make sure to include interface or function name in debug strings.
Print Tx errors from the MAC when debug is not on.


# 1.48 24-Feb-2006 brad

check for M_PKTHDR.


# 1.47 22-Feb-2006 damien

fix nfe_txeof() to reset the wathdog timeout only when a full tx frame
has been sent.


# 1.46 22-Feb-2006 damien

re-enable interrupt mitigation and mask out NFE_IRQ_TIMER that was causing
interrupts flood.


# 1.45 22-Feb-2006 brad

update the media settings after MAC setup.


# 1.44 21-Feb-2006 damien

disable interrupt mitigation until i figure out why we're spending 10% of
CPU time in interrupts with it.


# 1.43 20-Feb-2006 damien

- remove mbuf linearization code. it is broken in this context and it is very
unlikely that we will see mbuf chains with 62 fragments anytime soon
(anyway, it would not crash, it would just freeze TX).
- fix max scatter value so we don't end up filling the ring with one mbuf
chain.


# 1.42 19-Feb-2006 damien

- fix h/w VLAN tagging and enable it for adapters that support it (VLAN tag
stripping job is left to the network stack).
- enable interrupt mitigation by default.
- add some magic to the initialization sequence in the hope that it will fix
TX issues seen on some adapters.


# 1.41 16-Feb-2006 damien

- stop enabling/disabling interrupts in nfe_intr().
- store RX/TX settings in nfe_softc so we don't recompute them all the time.
- fix h/w VLAN tagging flags.


# 1.40 15-Feb-2006 brad

move setting IFCAP_VLAN_MTU back to where it is supposed to be.


# 1.39 15-Feb-2006 damien

add support for 64bit rings base addresses.


# 1.38 15-Feb-2006 damien

actually disable h/w VLAN tagging for now.


# 1.37 15-Feb-2006 damien

- enable jumbo frames for adapters that support it.
- extend TX DMA mappings size from MCLBYTES to NFE_JBYTES.
- add initial (disabled) bits for interrupts mitigation.
- add initial (disabled) bits for h/w VLAN tagging.
- did some consistency tweaks while i'm here.


# 1.36 13-Feb-2006 brad

use pci_mapreg_type().

ok damien@


# 1.35 13-Feb-2006 brad

nVidia/nvidia -> NVIDIA


# 1.34 12-Feb-2006 damien

fix a printf


# 1.33 12-Feb-2006 damien

don't print shared interrupts when debugging is on.


# 1.32 12-Feb-2006 damien

use BUS_DMA_READ flag when mapping RX buffers.


# 1.31 12-Feb-2006 damien

reduce the number of DMA sync operations by sincing multiple TX descriptors
at once.


# 1.30 12-Feb-2006 damien

initial jumbo frames support (disabled for now).
#define'ing NFE_NO_JUMBO can save a few hundred KB of wired memory.


# 1.29 11-Feb-2006 brad

set IFCAP_VLAN_MTU in the capabilites field.

ok damien@


# 1.28 11-Feb-2006 damien

- call nfe_ifmedia_upd() early in nfe_init() to reset the PHY.
- set RX buffer size register properly


# 1.27 11-Feb-2006 damien

fix nfe_free_tx_ring() and nfe_reset_tx_ring() to unmap the good items.


# 1.26 11-Feb-2006 damien

don't free the mbuf in nfe_encap() if it can't be DMA'mapped since it is
left in if_snd. prettify nfe_start() a bit while i'm here.


# 1.25 11-Feb-2006 damien

MCP51 boards don't support jumbo frames..


# 1.24 11-Feb-2006 damien

force a wakeup of the MAC in nfe_init().
this makes my MCP51 board working.

committed over a nfe.


# 1.23 10-Feb-2006 brad

Only kick the TX engine once in nfe_start() and only if there are
packets to be transmitted, after iterating through the queue and
queuing up as many packets as possible as opposed to kicking
it each time through nfe_encap().

ok damien@ jsg@


# 1.22 08-Feb-2006 jsg

Add back minimal debugging to help track down tx errors some MACs
seem to be reporting.


# 1.21 08-Feb-2006 jsg

Use __LP64__ instead of __amd64__ for portability.
Pointed out by miod@.


# 1.20 07-Feb-2006 jsg

Disable checksum offload for the moment as it appears to not
work for some people.


# 1.19 05-Feb-2006 brad

Simplify SIOCSIFADDR switch case a bit.


# 1.18 05-Feb-2006 brad

Eliminate a reset when configuring the IP address.

ok jsg@


# 1.17 05-Feb-2006 jsg

Don't force NFE_DEBUG, set default debug level to 0.
Committed via an nfe.


# 1.16 05-Feb-2006 damien

quiet nfe_intr().


# 1.15 05-Feb-2006 damien

unbreak the tree.

pointed at by Peter Stromberg.


# 1.14 05-Feb-2006 damien

fix nfe_txeof().
with these changes, the connection seems to work just fine.

with help from jsg@


# 1.13 04-Feb-2006 damien

- start MII timer in nfe_init() and move timer initialization in nfe_attach()
- set RX filter before enabling RX in nfe_init()
- call mii_down() in nfe_stop()
- fix setting of full/half-duplex mode
- call mii_phy_reset() for each PHY attached and call mii_mediachg() in
nfe_ifmedia_upd()
- some cleaning while i'm here


# 1.12 04-Feb-2006 damien

fix clearing of h/w csum flags in all but the first fragment


# 1.11 04-Feb-2006 damien

handle link state change interrupts but do nothing for now


# 1.10 04-Feb-2006 damien

- add support for multicast filters.
- fix setting of if_capabilities flags for chips supporting checksum offload.
- fix dmesg output in case we can't establish the intr handler.
- fix a call to bus_dmamap_unload() in nfe_encap().


# 1.9 22-Jan-2006 damien

fixes miibus_{read,write}reg routines.
this resolves the ghost ukphy problem.
did some cleanup while i'm here.

tested by and ok jsg@


# 1.8 20-Jan-2006 brad

remove redundant code from nfe_attach().

ok jsg@


# 1.7 18-Jan-2006 damien

initial Tx/Rx bits. not working yet.

joint work with jsg@


# 1.6 15-Jan-2006 damien

fix dma mapping of tx ring.


# 1.5 14-Jan-2006 jsg

Make sure interrupt is properly ack'd.
From damien@


# 1.4 17-Dec-2005 jsg

Don't dynamically assign ring size, simplifies ring structures
and code that deals with rings. We can get away with this
as we only have one type of rx and one type of tx ring at a time
unlike ral(4).

Suggested by and ok damien@


# 1.3 17-Dec-2005 jsg

Split length and flags up into seperate variables in
descriptors and make use of MCLBYTES for length setting.
Sugested by damien@


# 1.2 14-Dec-2005 jsg

We aren't likely to need to keep track of crypted/decrypted ring
bits any time soon... pointed out by damien@


# 1.1 14-Dec-2005 jsg

Initial bits for an nvidia nforce Ethernet driver.
bus_dma usage modelled after ral. Does not yet see rx interrupts
when testing with ck804.

Nvidia won't give out documentation for this, various "free" operating
systems include a closed source driver, and the Linux people who reverse
engineered it to create a specification won't give it out.


# 1.121 25-Sep-2019 kevlo

Argument order fix for MCLGETI.

ok claudio@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.120 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.119 22-Jan-2017 dlg

move counting if_opackets next to counting if_obytes in if_enqueue.

this means packets are consistently counted in one place, unlike the
many and various ways that drivers thought they should do it.

ok mpi@ deraadt@


# 1.118 29-Nov-2016 jsg

m_free() and m_freem() test for NULL. Simplify callers which had their own
NULL tests.

ok mpi@


Revision tags: OPENBSD_6_0_BASE
# 1.117 13-Apr-2016 mpi

G/C IFQ_SET_READY().


Revision tags: OPENBSD_5_9_BASE
# 1.116 25-Nov-2015 dlg

replace IFF_OACTIVE manipulation with mpsafe operations.

there are two things shared between the network stack and drivers
in the send path: the send queue and the IFF_OACTIVE flag. the send
queue is now protected by a mutex. this diff makes the oactive
functionality mpsafe too.

IFF_OACTIVE is part of if_flags. there are two problems with that.
firstly, if_flags is a short and we dont have any MI atomic operations
to manipulate a short. secondly, while we could make the IFF_OACTIVE
operates mpsafe, all changes to other flags would have to be made
safe at the same time, otherwise a read-modify-write cycle on their
updates could clobber the oactive change.

instead, this moves the oactive mark into struct ifqueue and provides
an API for changing it. there's ifq_set_oactive, ifq_clr_oactive,
and ifq_is_oactive. these are modelled on ifsq_set_oactive,
ifsq_clr_oactive, and ifsq_is_oactive in dragonflybsd.

this diff includes changes to all the drivers manipulating IFF_OACTIVE
to now use the ifsq_{set,clr_is}_oactive API too.

ok kettenis@ mpi@ jmatthew@ deraadt@


# 1.115 24-Nov-2015 mpi

You only need <net/if_dl.h> if you're using LLADDR() or a sockaddr_dl.


# 1.114 20-Nov-2015 dlg

shuffle struct ifqueue so in flight mbufs are protected by a mutex.

the code is refactored so the IFQ macros call newly implemented ifq
functions. the ifq code is split so each discipline (priq and hfsc
in our case) is an opaque set of operations that the common ifq
code can call. the common code does the locking, accounting (ifq_len
manipulation), and freeing of the mbuf if the disciplines enqueue
function rejects it. theyre kind of like bufqs in the block layer
with their fifo and nscan disciplines.

the new api also supports atomic switching of disciplines at runtime.
the hfsc setup in pf_ioctl.c has been tweaked to build a complete
hfsc_if structure which it attaches to the send queue in a single
operation, rather than attaching to the interface up front and
building up a list of queues.

the send queue is now mutexed, which raises the expectation that
packets can be enqueued or purged on one cpu while another cpu is
dequeueing them in a driver for transmission. a lot of drivers use
IFQ_POLL to peek at an mbuf and attempt to fit it on the ring before
committing to it with a later IFQ_DEQUEUE operation. if the mbuf
gets freed in between the POLL and DEQUEUE operations, fireworks
will ensue.

to avoid this, the ifq api introduces ifq_deq_begin, ifq_deq_rollback,
and ifq_deq_commit. ifq_deq_begin allows a driver to take the ifq
mutex and get a reference to the mbuf they wish to try and tx. if
there's space, they can ifq_deq_commit it to remove the mbuf and
release the mutex. if there's no space, ifq_deq_rollback simply
releases the mutex. this api was developed to make updating the
drivers using IFQ_POLL easy, instead of having to do significant
semantic changes to avoid POLL that we cannot test on all the
hardware.

the common code has been tested pretty hard, and all the driver
modifications are straightforward except for de(4). if that breaks
it can be dealt with later.

ok mpi@ jmatthew@


# 1.113 14-Nov-2015 mpi

Do not include <net/if_vlan_var.h> when it's not necessary.

Because of the VLAN hacks in mpw(4) this file still contains the definition
of "struct ifvlan" which depends on <sys/refcnt.h> which in turns pull
<sys/atomic.h>...


# 1.112 25-Oct-2015 mpi

arp_ifinit() is no longer needed.


Revision tags: OPENBSD_5_8_BASE
# 1.111 24-Jun-2015 mpi

Increment if_ipackets in if_input().

Note that pseudo-drivers not using if_input() are not affected by this
conversion.

ok mikeb@, kettenis@, claudio@, dlg@


# 1.110 20-Mar-2015 mpi

Convert to if_input(), thanks to krw@ for testing.


# 1.109 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.108 22-Dec-2014 tedu

unifdef INET


# 1.107 20-Aug-2014 dlg

after allocating an mbuf and cluster you still need to init the length
fields.

found by steven roberts, who also tested this fix for me


# 1.106 20-Aug-2014 dlg

remove the custom jumbo allocator. its never been enabled or used.

putting this into the tree to make it easier to test.


Revision tags: OPENBSD_5_6_BASE
# 1.105 22-Jul-2014 mpi

Fewer <netinet/in_systm.h>


Revision tags: OPENBSD_5_5_BASE
# 1.104 28-Dec-2013 deraadt

The few network drivers that called their children's (ie. mii PHY
drivers) activate functions at DVACT_RESUME time do not need to do
so, since their PHYs are repaired by IFF_UP.


# 1.103 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.102 07-Aug-2013 bluhm

Most network drivers include netinet/in_var.h, but apparently they
don't have to. Just remove these include lines.
Compiled on amd64 i386 sparc64; OK henning@ mikeb@


Revision tags: OPENBSD_5_4_BASE
# 1.101 01-Apr-2013 brad

Rewrite receive filter and ioctl handling code.

ok jsg@


Revision tags: OPENBSD_5_3_BASE
# 1.100 29-Nov-2012 brad

Remove setting an initial assumed baudrate upon driver attach which is not
necessarily correct, there might not even be a link when attaching.

ok mikeb@ reyk@


# 1.99 31-Aug-2012 stsp

Wake on LAN support for nfe(4). Tested by me, russel on misc@, and jsg.
ok jsg


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.98 05-Apr-2011 henning

mechanic rename M_{TCP|UDP}V4_CSUM_OUT -> M_{TCP|UDP}_CSUM_OUT
ok claudio krw


Revision tags: OPENBSD_4_9_BASE
# 1.97 10-Jan-2011 kettenis

Some nfe(4)/rlphy(4) combos don't work, because the PHY responds to all
addresses on the mii bus. As a countereasure, only attach the first PHY we
encounter. It is very unlikely we're going to ever see nfe(4) with multiple
PHYs. The same is probably true for any modern NIC.

ok mikeb@, deraadt@


# 1.96 07-Sep-2010 deraadt

remove the powerhook code. All architectures now use the ca_activate tree
traversal code to suspend/resume
ok oga kettenis blambert


# 1.95 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.94 31-Aug-2010 deraadt

activate function should return result of config_activate_children


# 1.93 27-Aug-2010 deraadt

Make the powerhook use the activate functions, which actually do the full
job.


# 1.92 27-Aug-2010 jsg

remove the unused if_init callback in struct ifnet
ok deraadt@ henning@ claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.91 06-Aug-2010 mlarkin

ca_activate function for nfe(4) for suspend/resume
tested on nvidia mcp51

ok deraadt@


# 1.90 19-May-2010 oga

BUS_DMA_ZERO instead of alloc, map, bzero.

ok krw@


Revision tags: OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.89 18-Jun-2009 jsg

Remove support for cancelled NVIDIA MCP7B and add initial support for
MCP89.

From Brad based on information from Peer Chen @ NVIDIA via Linux.


# 1.88 29-Mar-2009 sthen

make various strings ("can't map mem space" and similar) more consistent
between instances, saving space in the kernel. feedback from many (some
incorporated, some left for future work).

ok deraadt, kettenis, "why not" miod.


Revision tags: OPENBSD_4_5_BASE
# 1.87 28-Nov-2008 brad

Eliminate the redundant bits of code for MTU and multicast handling
from the individual drivers now that ether_ioctl() handles this.

Shrinks the i386 kernels by..
RAMDISK - 2176 bytes
RAMDISKB - 1504 bytes
RAMDISKC - 736 bytes

Tested by naddy@/okan@/sthen@/brad@/todd@/jmc@ and lots of users.
Build tested on almost all archs by todd@/brad@

ok naddy@


# 1.86 09-Nov-2008 naddy

Introduce bpf_mtap_ether(), which for the benefit of bpf listeners
creates the VLAN encapsulation from the tag stored in the mbuf
header. Idea from FreeBSD, input from claudio@ and canacar@.

Switch all hardware VLAN enabled drivers to the new function.

ok claudio@


# 1.85 28-Oct-2008 brad

Remove #if NVLAN.


# 1.84 28-Oct-2008 brad

Re-add support for RX VLAN tag stripping.


# 1.83 16-Oct-2008 naddy

Switch the existing TX VLAN hardware support over to having the
tag in the header. Convert TX tagging in the drivers.

Help and ok brad@


# 1.82 02-Oct-2008 brad

First step towards cleaning up the Ethernet driver ioctl handling.
Move calling ether_ioctl() from the top of the ioctl function, which
at the moment does absolutely nothing, to the default switch case.
Thus allowing drivers to define their own ioctl handlers and then
falling back on ether_ioctl(). The only functional change this results
in at the moment is having all Ethernet drivers returning the proper
errno of ENOTTY instead of EINVAL/ENXIO when encountering unknown
ioctl's.

Shrinks the i386 kernels by..
RAMDISK - 1024 bytes
RAMDISKB - 1120 bytes
RAMDISKC - 832 bytes

Tested by martin@/jsing@/todd@/brad@
Build tested on almost all archs by todd@/brad@

ok jsing@


# 1.81 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


# 1.80 09-Aug-2008 brad

MCP79 are also capable of Jumbo frames. Add the Jumbo support flag.

From: Linux forcedeth


Revision tags: OPENBSD_4_4_BASE
# 1.79 23-May-2008 brad

Simplify the combination use of pci_mapreg_type()/pci_mapreg_map() as
suggested by dlg@ awhile ago.

ok dlg@


# 1.78 19-May-2008 fgsch

correct format after %x -> %b convertion. deraadt jsg ok.


Revision tags: OPENBSD_4_3_BASE
# 1.77 05-Feb-2008 brad

Add the NFE_CORRECT_MACADDR flag for MCP77/79 chipsets.

ok jsg@ dlg@


# 1.76 02-Jan-2008 brad

add the NVIDIA MCP77/79 ids.

ok jsg@ dlg@


# 1.75 11-Dec-2007 mikeb

Fix check for pending interrupts as some other device is causing
the status register to change.

From form@.

ok jsg


# 1.74 05-Dec-2007 jsg

Make sure newer adapters are not in powerdown mode.
From Oleg Safiullin <form@pdp-11.org.ru> similiar to changes
in Linux/FreeBSD driver.


# 1.73 17-Nov-2007 jsg

Disable jumbo allocator until the low number of buffers case
is better handled.

This will likely resolve stalls some people are seeing under high
load.

ok damien@


# 1.72 12-Sep-2007 jsg

In nfe_start() do a fast return if IFF_OACTIVE is set, in
this case we need a Tx interrupt to clean up the DMA ring
before if_start can be properly called.

Diff based on a comment by & ok claudio@


# 1.71 07-Sep-2007 damien

use new malloc M_ZERO flag to shrink kernel.
remove <malloc.h> from files where malloc is not used.


# 1.70 01-Sep-2007 ckuethe

Correct the backwards ethernet address that some NVidia MACs have.
diff from brad. "commit this" jsg


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.69 02-Mar-2007 jsg

MCP65 has no hardware checksum support.
Diff from brad after he noticed recent changes
in the Linux driver by an NVIDIA employee.


# 1.68 08-Jan-2007 damien

Add support for HW TCP/IP checksum offload for adapters that support it.

Tested by many (IP/UDP/TCP):

Jason McIntyre <jmc@>
Chris Kuethe <chris.kuethe AT gmail.com>
Alf Schlichting <a.schlichting AT lemarit.com>
Rodolfo Gouveia <rgouveia AT cosmico.net>
Peter Stromberg <wilfried@>

Has been in snaps for weeks too with noone complaining so far.


# 1.67 15-Nov-2006 brad

Correct the WOL magic value and rename NFE_WOL_MAGIC to NFE_WOL_ENABLE.

WOL magic value from Peer Chen@NVIDIA via FreeBSD.

ok jsg@ damien@


# 1.66 10-Nov-2006 damien

Defer setting of the valid bit in the first TX descriptor after
all descriptors have been setup. Otherwise, hardware may start
processing descriptors faster than us and crap out.
Fixes "watchdog timeout" errors.

Original idea from Matthew Dillon @DragonFly.

ok deraadt@ jsg@ wim@
tested by many


# 1.65 05-Nov-2006 brad

add the NVIDIA MCP67 LAN PCI ids.


Revision tags: OPENBSD_4_0_BASE
# 1.64 23-Jul-2006 brad

branches: 1.64.2;
add NVidia MCP61/65 ids.


# 1.63 17-Jun-2006 brad

add sys/timeout.h


# 1.62 29-May-2006 brad

garbage collect NFE_NO_JUMBO.


# 1.61 28-May-2006 brad

- remove ETHER_MAX_LEN_JUMBO and ETHERMTU_JUMBO.
- use if_hardmtu for MTU ioctl handlers.

ok reyk@


# 1.60 28-May-2006 jason

unknown ioctl is ENOTTY not EINVAL


# 1.59 27-May-2006 brad

remove IFCAP_JUMBO_MTU interface capabilities flag and set if_hardmtu in a few
more drivers.

ok reyk@


# 1.58 20-May-2006 brad

set if_jumbo_mtu and the IFCAP_JUMBO_MTU capabilities flag where
appropriate.

ok reyk@


# 1.57 26-Apr-2006 jsg

Use %b in error flag printfs to describe meaning of error bits.
requested by deraadt@


# 1.56 26-Apr-2006 brad

And commit the diff from the right system.


# 1.55 26-Apr-2006 brad

check for IFF_RUNNING being set before calling nfe_init().

ok damien@ jsg@


# 1.54 07-Apr-2006 jsg

Add work around for mbuf leak in the tx path until we
can come up with a better guess as to how the hardware works.
From Chuck Silvers.
ok damien@


# 1.53 25-Mar-2006 djm

allow bpf(4) to ignore packets based on their direction (inbound or
outbound), using a new BIOCSDIRFILT ioctl;
guidance, feedback and ok canacar@


Revision tags: OPENBSD_3_9_BASE
# 1.52 02-Mar-2006 jsg

branches: 1.52.2;
Put the PHY update/reset call in nfe_init back to the spot it was at
before Rx/Tx/Interrupts are enabled.
This makes <fredd at cse.sc.edu>'s nfe+icsphy setup work again.

Tested on nfe+eephy by otto@ and myself, nfe+ciphy by otto@ and
nfe+rlphy by wilfried@

ok deraadt@


# 1.51 26-Feb-2006 damien

use sc->sc_dev.dv_xname consistently.


# 1.50 26-Feb-2006 krw

Let if_nfe.c compile again by putting declaration of ifp inside #ifdef
NFE_DEBUG.

ok dlg@


# 1.49 26-Feb-2006 jsg

Don't define NFE_DEBUG by default.
Set default debug level to 0.
Make sure to include interface or function name in debug strings.
Print Tx errors from the MAC when debug is not on.


# 1.48 24-Feb-2006 brad

check for M_PKTHDR.


# 1.47 22-Feb-2006 damien

fix nfe_txeof() to reset the wathdog timeout only when a full tx frame
has been sent.


# 1.46 22-Feb-2006 damien

re-enable interrupt mitigation and mask out NFE_IRQ_TIMER that was causing
interrupts flood.


# 1.45 22-Feb-2006 brad

update the media settings after MAC setup.


# 1.44 21-Feb-2006 damien

disable interrupt mitigation until i figure out why we're spending 10% of
CPU time in interrupts with it.


# 1.43 20-Feb-2006 damien

- remove mbuf linearization code. it is broken in this context and it is very
unlikely that we will see mbuf chains with 62 fragments anytime soon
(anyway, it would not crash, it would just freeze TX).
- fix max scatter value so we don't end up filling the ring with one mbuf
chain.


# 1.42 19-Feb-2006 damien

- fix h/w VLAN tagging and enable it for adapters that support it (VLAN tag
stripping job is left to the network stack).
- enable interrupt mitigation by default.
- add some magic to the initialization sequence in the hope that it will fix
TX issues seen on some adapters.


# 1.41 16-Feb-2006 damien

- stop enabling/disabling interrupts in nfe_intr().
- store RX/TX settings in nfe_softc so we don't recompute them all the time.
- fix h/w VLAN tagging flags.


# 1.40 15-Feb-2006 brad

move setting IFCAP_VLAN_MTU back to where it is supposed to be.


# 1.39 15-Feb-2006 damien

add support for 64bit rings base addresses.


# 1.38 15-Feb-2006 damien

actually disable h/w VLAN tagging for now.


# 1.37 15-Feb-2006 damien

- enable jumbo frames for adapters that support it.
- extend TX DMA mappings size from MCLBYTES to NFE_JBYTES.
- add initial (disabled) bits for interrupts mitigation.
- add initial (disabled) bits for h/w VLAN tagging.
- did some consistency tweaks while i'm here.


# 1.36 13-Feb-2006 brad

use pci_mapreg_type().

ok damien@


# 1.35 13-Feb-2006 brad

nVidia/nvidia -> NVIDIA


# 1.34 12-Feb-2006 damien

fix a printf


# 1.33 12-Feb-2006 damien

don't print shared interrupts when debugging is on.


# 1.32 12-Feb-2006 damien

use BUS_DMA_READ flag when mapping RX buffers.


# 1.31 12-Feb-2006 damien

reduce the number of DMA sync operations by sincing multiple TX descriptors
at once.


# 1.30 12-Feb-2006 damien

initial jumbo frames support (disabled for now).
#define'ing NFE_NO_JUMBO can save a few hundred KB of wired memory.


# 1.29 11-Feb-2006 brad

set IFCAP_VLAN_MTU in the capabilites field.

ok damien@


# 1.28 11-Feb-2006 damien

- call nfe_ifmedia_upd() early in nfe_init() to reset the PHY.
- set RX buffer size register properly


# 1.27 11-Feb-2006 damien

fix nfe_free_tx_ring() and nfe_reset_tx_ring() to unmap the good items.


# 1.26 11-Feb-2006 damien

don't free the mbuf in nfe_encap() if it can't be DMA'mapped since it is
left in if_snd. prettify nfe_start() a bit while i'm here.


# 1.25 11-Feb-2006 damien

MCP51 boards don't support jumbo frames..


# 1.24 11-Feb-2006 damien

force a wakeup of the MAC in nfe_init().
this makes my MCP51 board working.

committed over a nfe.


# 1.23 10-Feb-2006 brad

Only kick the TX engine once in nfe_start() and only if there are
packets to be transmitted, after iterating through the queue and
queuing up as many packets as possible as opposed to kicking
it each time through nfe_encap().

ok damien@ jsg@


# 1.22 08-Feb-2006 jsg

Add back minimal debugging to help track down tx errors some MACs
seem to be reporting.


# 1.21 08-Feb-2006 jsg

Use __LP64__ instead of __amd64__ for portability.
Pointed out by miod@.


# 1.20 07-Feb-2006 jsg

Disable checksum offload for the moment as it appears to not
work for some people.


# 1.19 05-Feb-2006 brad

Simplify SIOCSIFADDR switch case a bit.


# 1.18 05-Feb-2006 brad

Eliminate a reset when configuring the IP address.

ok jsg@


# 1.17 05-Feb-2006 jsg

Don't force NFE_DEBUG, set default debug level to 0.
Committed via an nfe.


# 1.16 05-Feb-2006 damien

quiet nfe_intr().


# 1.15 05-Feb-2006 damien

unbreak the tree.

pointed at by Peter Stromberg.


# 1.14 05-Feb-2006 damien

fix nfe_txeof().
with these changes, the connection seems to work just fine.

with help from jsg@


# 1.13 04-Feb-2006 damien

- start MII timer in nfe_init() and move timer initialization in nfe_attach()
- set RX filter before enabling RX in nfe_init()
- call mii_down() in nfe_stop()
- fix setting of full/half-duplex mode
- call mii_phy_reset() for each PHY attached and call mii_mediachg() in
nfe_ifmedia_upd()
- some cleaning while i'm here


# 1.12 04-Feb-2006 damien

fix clearing of h/w csum flags in all but the first fragment


# 1.11 04-Feb-2006 damien

handle link state change interrupts but do nothing for now


# 1.10 04-Feb-2006 damien

- add support for multicast filters.
- fix setting of if_capabilities flags for chips supporting checksum offload.
- fix dmesg output in case we can't establish the intr handler.
- fix a call to bus_dmamap_unload() in nfe_encap().


# 1.9 22-Jan-2006 damien

fixes miibus_{read,write}reg routines.
this resolves the ghost ukphy problem.
did some cleanup while i'm here.

tested by and ok jsg@


# 1.8 20-Jan-2006 brad

remove redundant code from nfe_attach().

ok jsg@


# 1.7 18-Jan-2006 damien

initial Tx/Rx bits. not working yet.

joint work with jsg@


# 1.6 15-Jan-2006 damien

fix dma mapping of tx ring.


# 1.5 14-Jan-2006 jsg

Make sure interrupt is properly ack'd.
From damien@


# 1.4 17-Dec-2005 jsg

Don't dynamically assign ring size, simplifies ring structures
and code that deals with rings. We can get away with this
as we only have one type of rx and one type of tx ring at a time
unlike ral(4).

Suggested by and ok damien@


# 1.3 17-Dec-2005 jsg

Split length and flags up into seperate variables in
descriptors and make use of MCLBYTES for length setting.
Sugested by damien@


# 1.2 14-Dec-2005 jsg

We aren't likely to need to keep track of crypted/decrypted ring
bits any time soon... pointed out by damien@


# 1.1 14-Dec-2005 jsg

Initial bits for an nvidia nforce Ethernet driver.
bus_dma usage modelled after ral. Does not yet see rx interrupts
when testing with ck804.

Nvidia won't give out documentation for this, various "free" operating
systems include a closed source driver, and the Linux people who reverse
engineered it to create a specification won't give it out.


Revision tags: OPENBSD_6_2_BASE
# 1.120 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.119 22-Jan-2017 dlg

move counting if_opackets next to counting if_obytes in if_enqueue.

this means packets are consistently counted in one place, unlike the
many and various ways that drivers thought they should do it.

ok mpi@ deraadt@


# 1.118 29-Nov-2016 jsg

m_free() and m_freem() test for NULL. Simplify callers which had their own
NULL tests.

ok mpi@


Revision tags: OPENBSD_6_0_BASE
# 1.117 13-Apr-2016 mpi

G/C IFQ_SET_READY().


Revision tags: OPENBSD_5_9_BASE
# 1.116 25-Nov-2015 dlg

replace IFF_OACTIVE manipulation with mpsafe operations.

there are two things shared between the network stack and drivers
in the send path: the send queue and the IFF_OACTIVE flag. the send
queue is now protected by a mutex. this diff makes the oactive
functionality mpsafe too.

IFF_OACTIVE is part of if_flags. there are two problems with that.
firstly, if_flags is a short and we dont have any MI atomic operations
to manipulate a short. secondly, while we could make the IFF_OACTIVE
operates mpsafe, all changes to other flags would have to be made
safe at the same time, otherwise a read-modify-write cycle on their
updates could clobber the oactive change.

instead, this moves the oactive mark into struct ifqueue and provides
an API for changing it. there's ifq_set_oactive, ifq_clr_oactive,
and ifq_is_oactive. these are modelled on ifsq_set_oactive,
ifsq_clr_oactive, and ifsq_is_oactive in dragonflybsd.

this diff includes changes to all the drivers manipulating IFF_OACTIVE
to now use the ifsq_{set,clr_is}_oactive API too.

ok kettenis@ mpi@ jmatthew@ deraadt@


# 1.115 24-Nov-2015 mpi

You only need <net/if_dl.h> if you're using LLADDR() or a sockaddr_dl.


# 1.114 20-Nov-2015 dlg

shuffle struct ifqueue so in flight mbufs are protected by a mutex.

the code is refactored so the IFQ macros call newly implemented ifq
functions. the ifq code is split so each discipline (priq and hfsc
in our case) is an opaque set of operations that the common ifq
code can call. the common code does the locking, accounting (ifq_len
manipulation), and freeing of the mbuf if the disciplines enqueue
function rejects it. theyre kind of like bufqs in the block layer
with their fifo and nscan disciplines.

the new api also supports atomic switching of disciplines at runtime.
the hfsc setup in pf_ioctl.c has been tweaked to build a complete
hfsc_if structure which it attaches to the send queue in a single
operation, rather than attaching to the interface up front and
building up a list of queues.

the send queue is now mutexed, which raises the expectation that
packets can be enqueued or purged on one cpu while another cpu is
dequeueing them in a driver for transmission. a lot of drivers use
IFQ_POLL to peek at an mbuf and attempt to fit it on the ring before
committing to it with a later IFQ_DEQUEUE operation. if the mbuf
gets freed in between the POLL and DEQUEUE operations, fireworks
will ensue.

to avoid this, the ifq api introduces ifq_deq_begin, ifq_deq_rollback,
and ifq_deq_commit. ifq_deq_begin allows a driver to take the ifq
mutex and get a reference to the mbuf they wish to try and tx. if
there's space, they can ifq_deq_commit it to remove the mbuf and
release the mutex. if there's no space, ifq_deq_rollback simply
releases the mutex. this api was developed to make updating the
drivers using IFQ_POLL easy, instead of having to do significant
semantic changes to avoid POLL that we cannot test on all the
hardware.

the common code has been tested pretty hard, and all the driver
modifications are straightforward except for de(4). if that breaks
it can be dealt with later.

ok mpi@ jmatthew@


# 1.113 14-Nov-2015 mpi

Do not include <net/if_vlan_var.h> when it's not necessary.

Because of the VLAN hacks in mpw(4) this file still contains the definition
of "struct ifvlan" which depends on <sys/refcnt.h> which in turns pull
<sys/atomic.h>...


# 1.112 25-Oct-2015 mpi

arp_ifinit() is no longer needed.


Revision tags: OPENBSD_5_8_BASE
# 1.111 24-Jun-2015 mpi

Increment if_ipackets in if_input().

Note that pseudo-drivers not using if_input() are not affected by this
conversion.

ok mikeb@, kettenis@, claudio@, dlg@


# 1.110 20-Mar-2015 mpi

Convert to if_input(), thanks to krw@ for testing.


# 1.109 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.108 22-Dec-2014 tedu

unifdef INET


# 1.107 20-Aug-2014 dlg

after allocating an mbuf and cluster you still need to init the length
fields.

found by steven roberts, who also tested this fix for me


# 1.106 20-Aug-2014 dlg

remove the custom jumbo allocator. its never been enabled or used.

putting this into the tree to make it easier to test.


Revision tags: OPENBSD_5_6_BASE
# 1.105 22-Jul-2014 mpi

Fewer <netinet/in_systm.h>


Revision tags: OPENBSD_5_5_BASE
# 1.104 28-Dec-2013 deraadt

The few network drivers that called their children's (ie. mii PHY
drivers) activate functions at DVACT_RESUME time do not need to do
so, since their PHYs are repaired by IFF_UP.


# 1.103 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.102 07-Aug-2013 bluhm

Most network drivers include netinet/in_var.h, but apparently they
don't have to. Just remove these include lines.
Compiled on amd64 i386 sparc64; OK henning@ mikeb@


Revision tags: OPENBSD_5_4_BASE
# 1.101 01-Apr-2013 brad

Rewrite receive filter and ioctl handling code.

ok jsg@


Revision tags: OPENBSD_5_3_BASE
# 1.100 29-Nov-2012 brad

Remove setting an initial assumed baudrate upon driver attach which is not
necessarily correct, there might not even be a link when attaching.

ok mikeb@ reyk@


# 1.99 31-Aug-2012 stsp

Wake on LAN support for nfe(4). Tested by me, russel on misc@, and jsg.
ok jsg


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.98 05-Apr-2011 henning

mechanic rename M_{TCP|UDP}V4_CSUM_OUT -> M_{TCP|UDP}_CSUM_OUT
ok claudio krw


Revision tags: OPENBSD_4_9_BASE
# 1.97 10-Jan-2011 kettenis

Some nfe(4)/rlphy(4) combos don't work, because the PHY responds to all
addresses on the mii bus. As a countereasure, only attach the first PHY we
encounter. It is very unlikely we're going to ever see nfe(4) with multiple
PHYs. The same is probably true for any modern NIC.

ok mikeb@, deraadt@


# 1.96 07-Sep-2010 deraadt

remove the powerhook code. All architectures now use the ca_activate tree
traversal code to suspend/resume
ok oga kettenis blambert


# 1.95 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.94 31-Aug-2010 deraadt

activate function should return result of config_activate_children


# 1.93 27-Aug-2010 deraadt

Make the powerhook use the activate functions, which actually do the full
job.


# 1.92 27-Aug-2010 jsg

remove the unused if_init callback in struct ifnet
ok deraadt@ henning@ claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.91 06-Aug-2010 mlarkin

ca_activate function for nfe(4) for suspend/resume
tested on nvidia mcp51

ok deraadt@


# 1.90 19-May-2010 oga

BUS_DMA_ZERO instead of alloc, map, bzero.

ok krw@


Revision tags: OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.89 18-Jun-2009 jsg

Remove support for cancelled NVIDIA MCP7B and add initial support for
MCP89.

From Brad based on information from Peer Chen @ NVIDIA via Linux.


# 1.88 29-Mar-2009 sthen

make various strings ("can't map mem space" and similar) more consistent
between instances, saving space in the kernel. feedback from many (some
incorporated, some left for future work).

ok deraadt, kettenis, "why not" miod.


Revision tags: OPENBSD_4_5_BASE
# 1.87 28-Nov-2008 brad

Eliminate the redundant bits of code for MTU and multicast handling
from the individual drivers now that ether_ioctl() handles this.

Shrinks the i386 kernels by..
RAMDISK - 2176 bytes
RAMDISKB - 1504 bytes
RAMDISKC - 736 bytes

Tested by naddy@/okan@/sthen@/brad@/todd@/jmc@ and lots of users.
Build tested on almost all archs by todd@/brad@

ok naddy@


# 1.86 09-Nov-2008 naddy

Introduce bpf_mtap_ether(), which for the benefit of bpf listeners
creates the VLAN encapsulation from the tag stored in the mbuf
header. Idea from FreeBSD, input from claudio@ and canacar@.

Switch all hardware VLAN enabled drivers to the new function.

ok claudio@


# 1.85 28-Oct-2008 brad

Remove #if NVLAN.


# 1.84 28-Oct-2008 brad

Re-add support for RX VLAN tag stripping.


# 1.83 16-Oct-2008 naddy

Switch the existing TX VLAN hardware support over to having the
tag in the header. Convert TX tagging in the drivers.

Help and ok brad@


# 1.82 02-Oct-2008 brad

First step towards cleaning up the Ethernet driver ioctl handling.
Move calling ether_ioctl() from the top of the ioctl function, which
at the moment does absolutely nothing, to the default switch case.
Thus allowing drivers to define their own ioctl handlers and then
falling back on ether_ioctl(). The only functional change this results
in at the moment is having all Ethernet drivers returning the proper
errno of ENOTTY instead of EINVAL/ENXIO when encountering unknown
ioctl's.

Shrinks the i386 kernels by..
RAMDISK - 1024 bytes
RAMDISKB - 1120 bytes
RAMDISKC - 832 bytes

Tested by martin@/jsing@/todd@/brad@
Build tested on almost all archs by todd@/brad@

ok jsing@


# 1.81 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


# 1.80 09-Aug-2008 brad

MCP79 are also capable of Jumbo frames. Add the Jumbo support flag.

From: Linux forcedeth


Revision tags: OPENBSD_4_4_BASE
# 1.79 23-May-2008 brad

Simplify the combination use of pci_mapreg_type()/pci_mapreg_map() as
suggested by dlg@ awhile ago.

ok dlg@


# 1.78 19-May-2008 fgsch

correct format after %x -> %b convertion. deraadt jsg ok.


Revision tags: OPENBSD_4_3_BASE
# 1.77 05-Feb-2008 brad

Add the NFE_CORRECT_MACADDR flag for MCP77/79 chipsets.

ok jsg@ dlg@


# 1.76 02-Jan-2008 brad

add the NVIDIA MCP77/79 ids.

ok jsg@ dlg@


# 1.75 11-Dec-2007 mikeb

Fix check for pending interrupts as some other device is causing
the status register to change.

From form@.

ok jsg


# 1.74 05-Dec-2007 jsg

Make sure newer adapters are not in powerdown mode.
From Oleg Safiullin <form@pdp-11.org.ru> similiar to changes
in Linux/FreeBSD driver.


# 1.73 17-Nov-2007 jsg

Disable jumbo allocator until the low number of buffers case
is better handled.

This will likely resolve stalls some people are seeing under high
load.

ok damien@


# 1.72 12-Sep-2007 jsg

In nfe_start() do a fast return if IFF_OACTIVE is set, in
this case we need a Tx interrupt to clean up the DMA ring
before if_start can be properly called.

Diff based on a comment by & ok claudio@


# 1.71 07-Sep-2007 damien

use new malloc M_ZERO flag to shrink kernel.
remove <malloc.h> from files where malloc is not used.


# 1.70 01-Sep-2007 ckuethe

Correct the backwards ethernet address that some NVidia MACs have.
diff from brad. "commit this" jsg


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.69 02-Mar-2007 jsg

MCP65 has no hardware checksum support.
Diff from brad after he noticed recent changes
in the Linux driver by an NVIDIA employee.


# 1.68 08-Jan-2007 damien

Add support for HW TCP/IP checksum offload for adapters that support it.

Tested by many (IP/UDP/TCP):

Jason McIntyre <jmc@>
Chris Kuethe <chris.kuethe AT gmail.com>
Alf Schlichting <a.schlichting AT lemarit.com>
Rodolfo Gouveia <rgouveia AT cosmico.net>
Peter Stromberg <wilfried@>

Has been in snaps for weeks too with noone complaining so far.


# 1.67 15-Nov-2006 brad

Correct the WOL magic value and rename NFE_WOL_MAGIC to NFE_WOL_ENABLE.

WOL magic value from Peer Chen@NVIDIA via FreeBSD.

ok jsg@ damien@


# 1.66 10-Nov-2006 damien

Defer setting of the valid bit in the first TX descriptor after
all descriptors have been setup. Otherwise, hardware may start
processing descriptors faster than us and crap out.
Fixes "watchdog timeout" errors.

Original idea from Matthew Dillon @DragonFly.

ok deraadt@ jsg@ wim@
tested by many


# 1.65 05-Nov-2006 brad

add the NVIDIA MCP67 LAN PCI ids.


Revision tags: OPENBSD_4_0_BASE
# 1.64 23-Jul-2006 brad

branches: 1.64.2;
add NVidia MCP61/65 ids.


# 1.63 17-Jun-2006 brad

add sys/timeout.h


# 1.62 29-May-2006 brad

garbage collect NFE_NO_JUMBO.


# 1.61 28-May-2006 brad

- remove ETHER_MAX_LEN_JUMBO and ETHERMTU_JUMBO.
- use if_hardmtu for MTU ioctl handlers.

ok reyk@


# 1.60 28-May-2006 jason

unknown ioctl is ENOTTY not EINVAL


# 1.59 27-May-2006 brad

remove IFCAP_JUMBO_MTU interface capabilities flag and set if_hardmtu in a few
more drivers.

ok reyk@


# 1.58 20-May-2006 brad

set if_jumbo_mtu and the IFCAP_JUMBO_MTU capabilities flag where
appropriate.

ok reyk@


# 1.57 26-Apr-2006 jsg

Use %b in error flag printfs to describe meaning of error bits.
requested by deraadt@


# 1.56 26-Apr-2006 brad

And commit the diff from the right system.


# 1.55 26-Apr-2006 brad

check for IFF_RUNNING being set before calling nfe_init().

ok damien@ jsg@


# 1.54 07-Apr-2006 jsg

Add work around for mbuf leak in the tx path until we
can come up with a better guess as to how the hardware works.
From Chuck Silvers.
ok damien@


# 1.53 25-Mar-2006 djm

allow bpf(4) to ignore packets based on their direction (inbound or
outbound), using a new BIOCSDIRFILT ioctl;
guidance, feedback and ok canacar@


Revision tags: OPENBSD_3_9_BASE
# 1.52 02-Mar-2006 jsg

branches: 1.52.2;
Put the PHY update/reset call in nfe_init back to the spot it was at
before Rx/Tx/Interrupts are enabled.
This makes <fredd at cse.sc.edu>'s nfe+icsphy setup work again.

Tested on nfe+eephy by otto@ and myself, nfe+ciphy by otto@ and
nfe+rlphy by wilfried@

ok deraadt@


# 1.51 26-Feb-2006 damien

use sc->sc_dev.dv_xname consistently.


# 1.50 26-Feb-2006 krw

Let if_nfe.c compile again by putting declaration of ifp inside #ifdef
NFE_DEBUG.

ok dlg@


# 1.49 26-Feb-2006 jsg

Don't define NFE_DEBUG by default.
Set default debug level to 0.
Make sure to include interface or function name in debug strings.
Print Tx errors from the MAC when debug is not on.


# 1.48 24-Feb-2006 brad

check for M_PKTHDR.


# 1.47 22-Feb-2006 damien

fix nfe_txeof() to reset the wathdog timeout only when a full tx frame
has been sent.


# 1.46 22-Feb-2006 damien

re-enable interrupt mitigation and mask out NFE_IRQ_TIMER that was causing
interrupts flood.


# 1.45 22-Feb-2006 brad

update the media settings after MAC setup.


# 1.44 21-Feb-2006 damien

disable interrupt mitigation until i figure out why we're spending 10% of
CPU time in interrupts with it.


# 1.43 20-Feb-2006 damien

- remove mbuf linearization code. it is broken in this context and it is very
unlikely that we will see mbuf chains with 62 fragments anytime soon
(anyway, it would not crash, it would just freeze TX).
- fix max scatter value so we don't end up filling the ring with one mbuf
chain.


# 1.42 19-Feb-2006 damien

- fix h/w VLAN tagging and enable it for adapters that support it (VLAN tag
stripping job is left to the network stack).
- enable interrupt mitigation by default.
- add some magic to the initialization sequence in the hope that it will fix
TX issues seen on some adapters.


# 1.41 16-Feb-2006 damien

- stop enabling/disabling interrupts in nfe_intr().
- store RX/TX settings in nfe_softc so we don't recompute them all the time.
- fix h/w VLAN tagging flags.


# 1.40 15-Feb-2006 brad

move setting IFCAP_VLAN_MTU back to where it is supposed to be.


# 1.39 15-Feb-2006 damien

add support for 64bit rings base addresses.


# 1.38 15-Feb-2006 damien

actually disable h/w VLAN tagging for now.


# 1.37 15-Feb-2006 damien

- enable jumbo frames for adapters that support it.
- extend TX DMA mappings size from MCLBYTES to NFE_JBYTES.
- add initial (disabled) bits for interrupts mitigation.
- add initial (disabled) bits for h/w VLAN tagging.
- did some consistency tweaks while i'm here.


# 1.36 13-Feb-2006 brad

use pci_mapreg_type().

ok damien@


# 1.35 13-Feb-2006 brad

nVidia/nvidia -> NVIDIA


# 1.34 12-Feb-2006 damien

fix a printf


# 1.33 12-Feb-2006 damien

don't print shared interrupts when debugging is on.


# 1.32 12-Feb-2006 damien

use BUS_DMA_READ flag when mapping RX buffers.


# 1.31 12-Feb-2006 damien

reduce the number of DMA sync operations by sincing multiple TX descriptors
at once.


# 1.30 12-Feb-2006 damien

initial jumbo frames support (disabled for now).
#define'ing NFE_NO_JUMBO can save a few hundred KB of wired memory.


# 1.29 11-Feb-2006 brad

set IFCAP_VLAN_MTU in the capabilites field.

ok damien@


# 1.28 11-Feb-2006 damien

- call nfe_ifmedia_upd() early in nfe_init() to reset the PHY.
- set RX buffer size register properly


# 1.27 11-Feb-2006 damien

fix nfe_free_tx_ring() and nfe_reset_tx_ring() to unmap the good items.


# 1.26 11-Feb-2006 damien

don't free the mbuf in nfe_encap() if it can't be DMA'mapped since it is
left in if_snd. prettify nfe_start() a bit while i'm here.


# 1.25 11-Feb-2006 damien

MCP51 boards don't support jumbo frames..


# 1.24 11-Feb-2006 damien

force a wakeup of the MAC in nfe_init().
this makes my MCP51 board working.

committed over a nfe.


# 1.23 10-Feb-2006 brad

Only kick the TX engine once in nfe_start() and only if there are
packets to be transmitted, after iterating through the queue and
queuing up as many packets as possible as opposed to kicking
it each time through nfe_encap().

ok damien@ jsg@


# 1.22 08-Feb-2006 jsg

Add back minimal debugging to help track down tx errors some MACs
seem to be reporting.


# 1.21 08-Feb-2006 jsg

Use __LP64__ instead of __amd64__ for portability.
Pointed out by miod@.


# 1.20 07-Feb-2006 jsg

Disable checksum offload for the moment as it appears to not
work for some people.


# 1.19 05-Feb-2006 brad

Simplify SIOCSIFADDR switch case a bit.


# 1.18 05-Feb-2006 brad

Eliminate a reset when configuring the IP address.

ok jsg@


# 1.17 05-Feb-2006 jsg

Don't force NFE_DEBUG, set default debug level to 0.
Committed via an nfe.


# 1.16 05-Feb-2006 damien

quiet nfe_intr().


# 1.15 05-Feb-2006 damien

unbreak the tree.

pointed at by Peter Stromberg.


# 1.14 05-Feb-2006 damien

fix nfe_txeof().
with these changes, the connection seems to work just fine.

with help from jsg@


# 1.13 04-Feb-2006 damien

- start MII timer in nfe_init() and move timer initialization in nfe_attach()
- set RX filter before enabling RX in nfe_init()
- call mii_down() in nfe_stop()
- fix setting of full/half-duplex mode
- call mii_phy_reset() for each PHY attached and call mii_mediachg() in
nfe_ifmedia_upd()
- some cleaning while i'm here


# 1.12 04-Feb-2006 damien

fix clearing of h/w csum flags in all but the first fragment


# 1.11 04-Feb-2006 damien

handle link state change interrupts but do nothing for now


# 1.10 04-Feb-2006 damien

- add support for multicast filters.
- fix setting of if_capabilities flags for chips supporting checksum offload.
- fix dmesg output in case we can't establish the intr handler.
- fix a call to bus_dmamap_unload() in nfe_encap().


# 1.9 22-Jan-2006 damien

fixes miibus_{read,write}reg routines.
this resolves the ghost ukphy problem.
did some cleanup while i'm here.

tested by and ok jsg@


# 1.8 20-Jan-2006 brad

remove redundant code from nfe_attach().

ok jsg@


# 1.7 18-Jan-2006 damien

initial Tx/Rx bits. not working yet.

joint work with jsg@


# 1.6 15-Jan-2006 damien

fix dma mapping of tx ring.


# 1.5 14-Jan-2006 jsg

Make sure interrupt is properly ack'd.
From damien@


# 1.4 17-Dec-2005 jsg

Don't dynamically assign ring size, simplifies ring structures
and code that deals with rings. We can get away with this
as we only have one type of rx and one type of tx ring at a time
unlike ral(4).

Suggested by and ok damien@


# 1.3 17-Dec-2005 jsg

Split length and flags up into seperate variables in
descriptors and make use of MCLBYTES for length setting.
Sugested by damien@


# 1.2 14-Dec-2005 jsg

We aren't likely to need to keep track of crypted/decrypted ring
bits any time soon... pointed out by damien@


# 1.1 14-Dec-2005 jsg

Initial bits for an nvidia nforce Ethernet driver.
bus_dma usage modelled after ral. Does not yet see rx interrupts
when testing with ck804.

Nvidia won't give out documentation for this, various "free" operating
systems include a closed source driver, and the Linux people who reverse
engineered it to create a specification won't give it out.