Merge from vendor branch FILE:
[dragonfly.git] / sys / dev / netif / wx / if_wxvar.h
1 /* $FreeBSD: src/sys/pci/if_wxvar.h,v 1.4.2.6 2001/10/20 17:44:12 mjacob Exp $ */
2 /* $DragonFly: src/sys/dev/netif/wx/Attic/if_wxvar.h,v 1.7 2004/09/15 01:19:13 joerg Exp $ */
3 /*                  
4  * Principal Author: Matthew Jacob
5  * Copyright (c) 1999, 2001 by Traakan Software
6  * All rights reserved.
7  *              
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:             
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.  
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * Additional Copyright (c) 2001 by Parag Patel
31  * under same licence for MII PHY code.
32  */
33
34 /*
35  * Softc definitions for the Intel Gigabit Ethernet driver.
36  *
37  * Guidance and inspiration from David Greenman's
38  * if_fxp driver gratefully acknowledged here.
39  */
40
41 /*
42  * Platform specific defines and inline functions go here.
43  * Look further below for more generic structures.
44  */
45
46 /*
47  * Enable for FreeBSD 5.0 SMP code
48  */
49 /* #define      SMPNG           1 */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/mbuf.h>
54 #include <sys/protosw.h>
55 #include <sys/socket.h>
56 #include <sys/malloc.h>
57 #include <sys/kernel.h>
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #ifdef NS
62 #include <netns/ns.h>
63 #include <netns/ns_if.h>
64 #endif
65 #include <net/bpf.h>
66 #include <sys/sockio.h>
67 #include <sys/bus.h>
68 #include <machine/bus.h>
69 #include <sys/rman.h>
70 #include <machine/resource.h>
71 #include <net/ethernet.h>
72 #include <net/if_arp.h>
73 #include <vm/vm.h>
74 #include <vm/pmap.h>
75 #include <machine/clock.h>
76 #include <bus/pci/pcivar.h>
77 #include <bus/pci/pcireg.h>
78 #include "if_wxreg.h"
79 #include "../mii_layer/mii.h"
80 #include "../mii_layer/miivar.h"
81 #include <sys/sysctl.h>
82
83 #define NBPFILTER       1
84
85 #include "miibus_if.h"
86
87 #include "opt_bdg.h"
88 #ifdef BRIDGE
89 #include <net/if_types.h>
90 #include <net/bridge/bridge.h>
91 #endif
92
93 struct wxmdvar {
94         /*
95          * arpcom must be first
96          */
97         struct arpcom           arpcom; /* per-interface network data */
98         struct device *         dev;    /* backpointer to device */
99         struct resource *       mem;    /* resource descriptor for registers */
100         struct resource *       irq;    /* resource descriptor for interrupt */
101         void *                  ih;     /* interrupt handler cookie */
102         u_int16_t               cmdw;
103         char                    name[8];
104         bus_space_tag_t         st;     /* bus space tag */
105         bus_space_handle_t      sh;     /* bus space handle */
106         struct ifmedia          ifm;
107         device_t                miibus;
108         struct wx_softc *       next;
109 #ifdef  SMPNG
110         struct mtx              wxmtx;
111 #else
112         int                     spl;
113 #endif
114 };
115 #define wx_dev          w.dev
116 #define wx_enaddr       w.arpcom.ac_enaddr
117 #define wx_cmdw         w.cmdw
118 #define wx_media        w.ifm
119 #define wx_next         w.next
120
121 #define wx_if           w.arpcom.ac_if
122 #define wx_name         w.name
123 #define wx_mtx          w.wxmtx
124
125 #define IOCTL_CMD_TYPE                  u_long
126 #define WXMALLOC(len)                   malloc(len, M_DEVBUF, M_WAITOK)
127 #define WXFREE(ptr)                     free(ptr, M_DEVBUF)
128 #define SOFTC_IFP(ifp)                  ifp->if_softc
129 #define WX_BPFTAP_ARG(ifp)              ifp
130 #define INLINE                          __inline
131 #ifdef  SMPNG
132 #define WX_LOCK(_sc)                    mtx_lock(&(_sc)->wx_mtx)
133 #define WX_UNLOCK(_sc)                  mtx_unlock(&(_sc)->wx_mtx)
134 #define WX_ILOCK(_sc)                   mtx_lock(&(_sc)->wx_mtx)
135 #define WX_IUNLK(_sc)                   mtx_unlock(&(_sc)->wx_mtx)
136 #else
137 #define WX_LOCK(_sc)                    _sc->w.spl = splimp()
138 #define WX_UNLOCK(_sc)                  splx(_sc->w.spl)
139 #define WX_ILOCK(_sc)
140 #define WX_IUNLK(_sc)
141 #endif
142 #define WX_SOFTC_FROM_MII_ARG(x)        device_get_softc(x)
143 #define WX_MII_FROM_SOFTC(x)            device_get_softc((x)->w.miibus)
144
145
146 #define READ_CSR(sc, reg)                                               \
147         bus_space_read_4((sc)->w.st, (sc)->w.sh, (reg))
148 #define WRITE_CSR(sc, reg, val)                                         \
149         bus_space_write_4((sc)->w.st, (sc)->w.sh, (reg), (val))
150
151 /*
152  * Transmit soft descriptor, used to manage packets as they come in.
153  */
154 typedef struct rxpkt {
155         struct mbuf *dptr;      /* pointer to receive frame */
156         u_int32_t dma_addr;     /* dma address */
157 } rxpkt_t;
158
159         
160 /*
161  * Transmit soft descriptor, used to manage packets as they are transmitted.
162  */
163 typedef struct txpkt {
164         struct txpkt *next;     /* next in a chain */
165         struct mbuf *dptr;      /* pointer to mbuf being sent */
166         u_int32_t sidx;         /* start index */
167         u_int32_t eidx;         /* end index */
168 } txpkt_t;
169
170
171 typedef struct wx_softc {
172         /*
173          * OS dependent storage... must be first...
174          */
175         struct wxmdvar w;
176         struct callout watchdog_timer;
177
178         /*
179          * misc goodies
180          */
181         u_int32_t               :       22,
182                 wx_needreinit   :       1,
183                 wx_mii          :       1,      /* non-zero if we have a PHY */
184                 wx_no_flow      :       1,
185                 wx_ilos         :       1,
186                 wx_no_ilos      :       1,
187                 wx_verbose      :       1,
188                 wx_debug        :       1,
189                 ane_failed      :       1,
190                 linkup          :       1,
191                 all_mcasts      :       1;
192         u_int32_t wx_idnrev;            /* chip revision && PCI ID */
193         u_int16_t wx_cfg1;
194         u_int16_t wx_unused;
195         u_int32_t wx_ienable;           /* current ienable to use */
196         u_int32_t wx_dcr;               /* dcr used */
197         u_int32_t wx_icr;               /* last icr */
198
199         /*
200          * Statistics, soft && hard
201          */
202         u_int32_t       wx_intr;
203         u_int32_t       wx_linkintr;
204         u_int32_t       wx_rxintr;
205         u_int32_t       wx_txqe;
206         u_int32_t       wx_xmitgc;
207         u_int32_t       wx_xmitpullup;
208         u_int32_t       wx_xmitcluster;
209         u_int32_t       wx_xmitputback;
210         u_int32_t       wx_xmitwanted;
211         u_int32_t       wx_xmitblocked;
212         u_int32_t       wx_xmitrunt;
213         u_int32_t       wx_rxnobuf;
214         u_int32_t       wx_oddpkt;
215
216         /*
217          * Soft copies of multicast addresses. We're only
218          * using (right now) the rest of the receive address
219          * registers- not the hashed multicast table.
220          */
221         u_int8_t        wx_mcaddr[WX_RAL_TAB_SIZE-1][6];
222         u_int8_t        wx_nmca;                /* # active multicast addrs */
223
224
225         /*
226          * Receive Management
227          * We have software and shared memory rings in a buddy store format.
228          */
229         wxrd_t  *rdescriptors;          /* receive descriptor ring */
230         rxpkt_t *rbase;                 /* base of soft rdesc list */
231         u_int16_t rnxt;                 /* next descriptor to check */
232         u_int16_t _pad;
233         struct mbuf *rpending;          /* pending partial packet */
234
235         /*
236          * Transmit Management
237          * We have software and shared memory rings in a buddy store format.
238          */
239         txpkt_t *tbase;                 /* base of soft soft management */
240         txpkt_t *tbsyf, *tbsyl;         /* linked busy list */
241         wxtd_t  *tdescriptors;          /* transmit descriptor ring */
242         u_int16_t tnxtfree;             /* next free index (circular) */
243         u_int16_t tactive;              /* # active */
244 } wx_softc_t;
245
246 /*
247  * We offset the the receive frame header by two bytes so that the actual
248  * payload is 32 bit aligned. On platforms that require strict structure
249  * alignment, this means that the ethernet frame header may have to be shifted
250  * to align it at interrupt time, but because it's such a small amount
251  * (fourteen bytes) and processors have gotten pretty fast, that's okay.
252  * It may even turn out on some platforms that this doesn't have to happen.
253  */
254 #define WX_RX_OFFSET_VALUE      2
255
256 /*
257  * Tunable Parameters.
258  *
259  * Descriptor lengths must be in multiples of 8.
260  */
261 #define WX_MAX_TDESC    256     /* number of transmit descriptors */
262 #define T_NXT_IDX(x)    ((x + 1) & (WX_MAX_TDESC - 1))
263 #define T_PREV_IDX(x)   ((x - 1) & (WX_MAX_TDESC - 1))
264 #define WX_MAX_RDESC    256     /* number of receive descriptors */
265 #ifdef  PADDED_CELL
266 #define RXINCR          2
267 #else
268 #define RXINCR          1
269 #endif
270 #define R_NXT_IDX(x)    ((x + RXINCR) & (WX_MAX_RDESC - 1))
271 #define R_PREV_IDX(x)   ((x - RXINCR) & (WX_MAX_RDESC - 1))
272
273 /*
274  * Link Up timeout, in milliseconds.
275  */
276
277 #define WX_LINK_UP_TIMEOUT      500