if_vr.c revision 1.17 1 1.17 thorpej /* $NetBSD: if_vr.c,v 1.17 1999/02/05 22:09:46 thorpej Exp $ */
2 1.2 sakamoto
3 1.1 sakamoto /*
4 1.1 sakamoto * Copyright (c) 1997, 1998
5 1.1 sakamoto * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
6 1.1 sakamoto *
7 1.1 sakamoto * Redistribution and use in source and binary forms, with or without
8 1.1 sakamoto * modification, are permitted provided that the following conditions
9 1.1 sakamoto * are met:
10 1.1 sakamoto * 1. Redistributions of source code must retain the above copyright
11 1.1 sakamoto * notice, this list of conditions and the following disclaimer.
12 1.1 sakamoto * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 sakamoto * notice, this list of conditions and the following disclaimer in the
14 1.1 sakamoto * documentation and/or other materials provided with the distribution.
15 1.1 sakamoto * 3. All advertising materials mentioning features or use of this software
16 1.1 sakamoto * must display the following acknowledgement:
17 1.1 sakamoto * This product includes software developed by Bill Paul.
18 1.1 sakamoto * 4. Neither the name of the author nor the names of any co-contributors
19 1.1 sakamoto * may be used to endorse or promote products derived from this software
20 1.1 sakamoto * without specific prior written permission.
21 1.1 sakamoto *
22 1.1 sakamoto * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 1.1 sakamoto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 sakamoto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 sakamoto * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 1.1 sakamoto * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 sakamoto * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 sakamoto * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 sakamoto * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 sakamoto * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 sakamoto * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.1 sakamoto * THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 sakamoto *
34 1.2 sakamoto * $FreeBSD: if_vr.c,v 1.7 1999/01/10 18:51:49 wpaul Exp $
35 1.1 sakamoto */
36 1.1 sakamoto
37 1.1 sakamoto /*
38 1.1 sakamoto * VIA Rhine fast ethernet PCI NIC driver
39 1.1 sakamoto *
40 1.1 sakamoto * Supports various network adapters based on the VIA Rhine
41 1.1 sakamoto * and Rhine II PCI controllers, including the D-Link DFE530TX.
42 1.1 sakamoto * Datasheets are available at http://www.via.com.tw.
43 1.1 sakamoto *
44 1.1 sakamoto * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
45 1.1 sakamoto * Electrical Engineering Department
46 1.1 sakamoto * Columbia University, New York City
47 1.1 sakamoto */
48 1.1 sakamoto
49 1.1 sakamoto /*
50 1.1 sakamoto * The VIA Rhine controllers are similar in some respects to the
51 1.1 sakamoto * the DEC tulip chips, except less complicated. The controller
52 1.1 sakamoto * uses an MII bus and an external physical layer interface. The
53 1.1 sakamoto * receiver has a one entry perfect filter and a 64-bit hash table
54 1.1 sakamoto * multicast filter. Transmit and receive descriptors are similar
55 1.1 sakamoto * to the tulip.
56 1.1 sakamoto *
57 1.1 sakamoto * The Rhine has a serious flaw in its transmit DMA mechanism:
58 1.1 sakamoto * transmit buffers must be longword aligned. Unfortunately,
59 1.17 thorpej * the kernel doesn't guarantee that mbufs will be filled in starting
60 1.1 sakamoto * at longword boundaries, so we have to do a buffer copy before
61 1.1 sakamoto * transmission.
62 1.17 thorpej *
63 1.17 thorpej * Apparently, the receive DMA mechanism also has the same flaw. This
64 1.17 thorpej * means that on systems with struct alignment requirements, incoming
65 1.17 thorpej * frames must be copied to a new buffer which shifts the data forward
66 1.17 thorpej * 2 bytes so that the payload is aligned on a 4-byte boundary.
67 1.1 sakamoto */
68 1.1 sakamoto
69 1.2 sakamoto #include "opt_inet.h"
70 1.1 sakamoto
71 1.1 sakamoto #include <sys/param.h>
72 1.1 sakamoto #include <sys/systm.h>
73 1.1 sakamoto #include <sys/sockio.h>
74 1.1 sakamoto #include <sys/mbuf.h>
75 1.1 sakamoto #include <sys/malloc.h>
76 1.1 sakamoto #include <sys/kernel.h>
77 1.1 sakamoto #include <sys/socket.h>
78 1.6 thorpej #include <sys/device.h>
79 1.1 sakamoto
80 1.1 sakamoto #include <net/if.h>
81 1.1 sakamoto #include <net/if_arp.h>
82 1.1 sakamoto #include <net/if_dl.h>
83 1.1 sakamoto #include <net/if_media.h>
84 1.2 sakamoto #include <net/if_ether.h>
85 1.6 thorpej
86 1.2 sakamoto #if defined(INET)
87 1.2 sakamoto #include <netinet/in.h>
88 1.2 sakamoto #include <netinet/if_inarp.h>
89 1.2 sakamoto #endif
90 1.1 sakamoto
91 1.2 sakamoto #include "bpfilter.h"
92 1.1 sakamoto #if NBPFILTER > 0
93 1.1 sakamoto #include <net/bpf.h>
94 1.1 sakamoto #endif
95 1.1 sakamoto
96 1.2 sakamoto #include <vm/vm.h> /* for vtophys */
97 1.2 sakamoto
98 1.1 sakamoto #include <machine/bus.h>
99 1.6 thorpej #include <machine/intr.h>
100 1.1 sakamoto
101 1.10 thorpej #include <dev/mii/mii.h>
102 1.11 thorpej #include <dev/mii/miivar.h>
103 1.10 thorpej
104 1.2 sakamoto #include <dev/pci/pcireg.h>
105 1.2 sakamoto #include <dev/pci/pcivar.h>
106 1.8 thorpej #include <dev/pci/pcidevs.h>
107 1.8 thorpej
108 1.2 sakamoto #include <dev/pci/if_vrreg.h>
109 1.1 sakamoto
110 1.5 thorpej #if defined(__NetBSD__) && defined(__alpha__)
111 1.5 thorpej /* XXX XXX NEED REAL DMA MAPPING SUPPORT XXX XXX */
112 1.5 thorpej #undef vtophys
113 1.5 thorpej #define vtophys(va) alpha_XXX_dmamap((vaddr_t)(va))
114 1.5 thorpej #endif
115 1.5 thorpej
116 1.2 sakamoto #define VR_USEIOSPACE
117 1.1 sakamoto
118 1.6 thorpej #define ETHER_CRC_LEN 4 /* XXX Should be in a common header. */
119 1.1 sakamoto
120 1.1 sakamoto /*
121 1.1 sakamoto * Various supported device vendors/types and their names.
122 1.1 sakamoto */
123 1.7 thorpej static struct vr_type {
124 1.7 thorpej pci_vendor_id_t vr_vid;
125 1.7 thorpej pci_product_id_t vr_did;
126 1.7 thorpej const char *vr_name;
127 1.7 thorpej } vr_devs[] = {
128 1.8 thorpej { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT3043,
129 1.1 sakamoto "VIA VT3043 Rhine I 10/100BaseTX" },
130 1.8 thorpej { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C100A,
131 1.1 sakamoto "VIA VT86C100A Rhine II 10/100BaseTX" },
132 1.1 sakamoto { 0, 0, NULL }
133 1.1 sakamoto };
134 1.1 sakamoto
135 1.7 thorpej struct vr_list_data {
136 1.7 thorpej struct vr_desc vr_rx_list[VR_RX_LIST_CNT];
137 1.7 thorpej struct vr_desc vr_tx_list[VR_TX_LIST_CNT];
138 1.7 thorpej };
139 1.7 thorpej
140 1.7 thorpej struct vr_chain {
141 1.7 thorpej struct vr_desc *vr_ptr;
142 1.7 thorpej struct mbuf *vr_mbuf;
143 1.7 thorpej struct vr_chain *vr_nextdesc;
144 1.7 thorpej };
145 1.7 thorpej
146 1.7 thorpej struct vr_chain_onefrag {
147 1.7 thorpej struct vr_desc *vr_ptr;
148 1.7 thorpej struct mbuf *vr_mbuf;
149 1.7 thorpej struct vr_chain_onefrag *vr_nextdesc;
150 1.7 thorpej };
151 1.7 thorpej
152 1.7 thorpej struct vr_chain_data {
153 1.7 thorpej struct vr_chain_onefrag vr_rx_chain[VR_RX_LIST_CNT];
154 1.7 thorpej struct vr_chain vr_tx_chain[VR_TX_LIST_CNT];
155 1.7 thorpej
156 1.7 thorpej struct vr_chain_onefrag *vr_rx_head;
157 1.7 thorpej
158 1.7 thorpej struct vr_chain *vr_tx_head;
159 1.7 thorpej struct vr_chain *vr_tx_tail;
160 1.7 thorpej struct vr_chain *vr_tx_free;
161 1.7 thorpej };
162 1.7 thorpej
163 1.7 thorpej struct vr_softc {
164 1.14 thorpej struct device vr_dev; /* generic device glue */
165 1.14 thorpej void *vr_ih; /* interrupt cookie */
166 1.14 thorpej void *vr_ats; /* shutdown hook */
167 1.14 thorpej bus_space_tag_t vr_bst; /* bus space tag */
168 1.14 thorpej bus_space_handle_t vr_bsh; /* bus space handle */
169 1.14 thorpej pci_chipset_tag_t vr_pc; /* PCI chipset info */
170 1.14 thorpej struct ethercom vr_ec; /* Ethernet common info */
171 1.7 thorpej u_int8_t vr_enaddr[ETHER_ADDR_LEN];
172 1.11 thorpej struct mii_data vr_mii; /* MII/media info */
173 1.7 thorpej caddr_t vr_ldata_ptr;
174 1.7 thorpej struct vr_list_data *vr_ldata;
175 1.7 thorpej struct vr_chain_data vr_cdata;
176 1.7 thorpej };
177 1.7 thorpej
178 1.7 thorpej /*
179 1.7 thorpej * register space access macros
180 1.7 thorpej */
181 1.7 thorpej #define CSR_WRITE_4(sc, reg, val) \
182 1.14 thorpej bus_space_write_4(sc->vr_bst, sc->vr_bsh, reg, val)
183 1.7 thorpej #define CSR_WRITE_2(sc, reg, val) \
184 1.14 thorpej bus_space_write_2(sc->vr_bst, sc->vr_bsh, reg, val)
185 1.7 thorpej #define CSR_WRITE_1(sc, reg, val) \
186 1.14 thorpej bus_space_write_1(sc->vr_bst, sc->vr_bsh, reg, val)
187 1.7 thorpej
188 1.7 thorpej #define CSR_READ_4(sc, reg) \
189 1.14 thorpej bus_space_read_4(sc->vr_bst, sc->vr_bsh, reg)
190 1.7 thorpej #define CSR_READ_2(sc, reg) \
191 1.14 thorpej bus_space_read_2(sc->vr_bst, sc->vr_bsh, reg)
192 1.7 thorpej #define CSR_READ_1(sc, reg) \
193 1.14 thorpej bus_space_read_1(sc->vr_bst, sc->vr_bsh, reg)
194 1.7 thorpej
195 1.7 thorpej #define VR_TIMEOUT 1000
196 1.1 sakamoto
197 1.1 sakamoto static int vr_newbuf __P((struct vr_softc *,
198 1.1 sakamoto struct vr_chain_onefrag *));
199 1.1 sakamoto static int vr_encap __P((struct vr_softc *, struct vr_chain *,
200 1.2 sakamoto struct mbuf *));
201 1.1 sakamoto
202 1.1 sakamoto static void vr_rxeof __P((struct vr_softc *));
203 1.1 sakamoto static void vr_rxeoc __P((struct vr_softc *));
204 1.1 sakamoto static void vr_txeof __P((struct vr_softc *));
205 1.1 sakamoto static void vr_txeoc __P((struct vr_softc *));
206 1.16 thorpej static int vr_intr __P((void *));
207 1.1 sakamoto static void vr_start __P((struct ifnet *));
208 1.1 sakamoto static int vr_ioctl __P((struct ifnet *, u_long, caddr_t));
209 1.1 sakamoto static void vr_init __P((void *));
210 1.1 sakamoto static void vr_stop __P((struct vr_softc *));
211 1.1 sakamoto static void vr_watchdog __P((struct ifnet *));
212 1.11 thorpej static void vr_tick __P((void *));
213 1.11 thorpej
214 1.1 sakamoto static int vr_ifmedia_upd __P((struct ifnet *));
215 1.1 sakamoto static void vr_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
216 1.1 sakamoto
217 1.1 sakamoto static void vr_mii_sync __P((struct vr_softc *));
218 1.1 sakamoto static void vr_mii_send __P((struct vr_softc *, u_int32_t, int));
219 1.11 thorpej static int vr_mii_readreg __P((struct device *, int, int));
220 1.11 thorpej static void vr_mii_writereg __P((struct device *, int, int, int));
221 1.11 thorpej static void vr_mii_statchg __P((struct device *));
222 1.11 thorpej
223 1.1 sakamoto static u_int8_t vr_calchash __P((u_int8_t *));
224 1.1 sakamoto static void vr_setmulti __P((struct vr_softc *));
225 1.1 sakamoto static void vr_reset __P((struct vr_softc *));
226 1.1 sakamoto static int vr_list_rx_init __P((struct vr_softc *));
227 1.1 sakamoto static int vr_list_tx_init __P((struct vr_softc *));
228 1.1 sakamoto
229 1.2 sakamoto #define VR_SETBIT(sc, reg, x) \
230 1.1 sakamoto CSR_WRITE_1(sc, reg, \
231 1.1 sakamoto CSR_READ_1(sc, reg) | x)
232 1.1 sakamoto
233 1.2 sakamoto #define VR_CLRBIT(sc, reg, x) \
234 1.1 sakamoto CSR_WRITE_1(sc, reg, \
235 1.1 sakamoto CSR_READ_1(sc, reg) & ~x)
236 1.1 sakamoto
237 1.2 sakamoto #define VR_SETBIT16(sc, reg, x) \
238 1.1 sakamoto CSR_WRITE_2(sc, reg, \
239 1.1 sakamoto CSR_READ_2(sc, reg) | x)
240 1.1 sakamoto
241 1.2 sakamoto #define VR_CLRBIT16(sc, reg, x) \
242 1.1 sakamoto CSR_WRITE_2(sc, reg, \
243 1.1 sakamoto CSR_READ_2(sc, reg) & ~x)
244 1.1 sakamoto
245 1.2 sakamoto #define VR_SETBIT32(sc, reg, x) \
246 1.1 sakamoto CSR_WRITE_4(sc, reg, \
247 1.1 sakamoto CSR_READ_4(sc, reg) | x)
248 1.1 sakamoto
249 1.2 sakamoto #define VR_CLRBIT32(sc, reg, x) \
250 1.1 sakamoto CSR_WRITE_4(sc, reg, \
251 1.1 sakamoto CSR_READ_4(sc, reg) & ~x)
252 1.1 sakamoto
253 1.2 sakamoto #define SIO_SET(x) \
254 1.1 sakamoto CSR_WRITE_1(sc, VR_MIICMD, \
255 1.1 sakamoto CSR_READ_1(sc, VR_MIICMD) | x)
256 1.1 sakamoto
257 1.2 sakamoto #define SIO_CLR(x) \
258 1.1 sakamoto CSR_WRITE_1(sc, VR_MIICMD, \
259 1.1 sakamoto CSR_READ_1(sc, VR_MIICMD) & ~x)
260 1.1 sakamoto
261 1.1 sakamoto /*
262 1.1 sakamoto * Sync the PHYs by setting data bit and strobing the clock 32 times.
263 1.1 sakamoto */
264 1.15 thorpej static void
265 1.15 thorpej vr_mii_sync(sc)
266 1.15 thorpej struct vr_softc *sc;
267 1.1 sakamoto {
268 1.15 thorpej int i;
269 1.1 sakamoto
270 1.9 thorpej SIO_SET(VR_MIICMD_DIR|VR_MIICMD_DATAOUT);
271 1.1 sakamoto
272 1.1 sakamoto for (i = 0; i < 32; i++) {
273 1.1 sakamoto SIO_SET(VR_MIICMD_CLK);
274 1.1 sakamoto DELAY(1);
275 1.1 sakamoto SIO_CLR(VR_MIICMD_CLK);
276 1.1 sakamoto DELAY(1);
277 1.1 sakamoto }
278 1.1 sakamoto
279 1.1 sakamoto return;
280 1.1 sakamoto }
281 1.1 sakamoto
282 1.1 sakamoto /*
283 1.1 sakamoto * Clock a series of bits through the MII.
284 1.1 sakamoto */
285 1.15 thorpej static void
286 1.15 thorpej vr_mii_send(sc, bits, cnt)
287 1.15 thorpej struct vr_softc *sc;
288 1.15 thorpej u_int32_t bits;
289 1.15 thorpej int cnt;
290 1.1 sakamoto {
291 1.15 thorpej int i;
292 1.1 sakamoto
293 1.1 sakamoto SIO_CLR(VR_MIICMD_CLK);
294 1.1 sakamoto
295 1.1 sakamoto for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
296 1.2 sakamoto if (bits & i) {
297 1.9 thorpej SIO_SET(VR_MIICMD_DATAOUT);
298 1.2 sakamoto } else {
299 1.9 thorpej SIO_CLR(VR_MIICMD_DATAOUT);
300 1.2 sakamoto }
301 1.1 sakamoto DELAY(1);
302 1.1 sakamoto SIO_CLR(VR_MIICMD_CLK);
303 1.1 sakamoto DELAY(1);
304 1.1 sakamoto SIO_SET(VR_MIICMD_CLK);
305 1.1 sakamoto }
306 1.1 sakamoto }
307 1.1 sakamoto
308 1.1 sakamoto /*
309 1.1 sakamoto * Read an PHY register through the MII.
310 1.1 sakamoto */
311 1.15 thorpej static int
312 1.15 thorpej vr_mii_readreg(self, phy, reg)
313 1.11 thorpej struct device *self;
314 1.11 thorpej int phy, reg;
315 1.1 sakamoto {
316 1.11 thorpej struct vr_softc *sc = (struct vr_softc *)self;
317 1.13 thorpej int i, ack, val = 0;
318 1.1 sakamoto
319 1.1 sakamoto CSR_WRITE_1(sc, VR_MIICMD, 0);
320 1.1 sakamoto VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
321 1.1 sakamoto
322 1.1 sakamoto /*
323 1.2 sakamoto * Turn on data xmit.
324 1.1 sakamoto */
325 1.1 sakamoto SIO_SET(VR_MIICMD_DIR);
326 1.1 sakamoto
327 1.1 sakamoto vr_mii_sync(sc);
328 1.1 sakamoto
329 1.1 sakamoto /*
330 1.1 sakamoto * Send command/address info.
331 1.1 sakamoto */
332 1.11 thorpej vr_mii_send(sc, MII_COMMAND_START, 2);
333 1.11 thorpej vr_mii_send(sc, MII_COMMAND_READ, 2);
334 1.11 thorpej vr_mii_send(sc, phy, 5);
335 1.11 thorpej vr_mii_send(sc, reg, 5);
336 1.1 sakamoto
337 1.1 sakamoto /* Idle bit */
338 1.9 thorpej SIO_CLR((VR_MIICMD_CLK|VR_MIICMD_DATAOUT));
339 1.1 sakamoto DELAY(1);
340 1.1 sakamoto SIO_SET(VR_MIICMD_CLK);
341 1.1 sakamoto DELAY(1);
342 1.1 sakamoto
343 1.1 sakamoto /* Turn off xmit. */
344 1.1 sakamoto SIO_CLR(VR_MIICMD_DIR);
345 1.1 sakamoto
346 1.1 sakamoto /* Check for ack */
347 1.1 sakamoto SIO_CLR(VR_MIICMD_CLK);
348 1.1 sakamoto DELAY(1);
349 1.1 sakamoto SIO_SET(VR_MIICMD_CLK);
350 1.1 sakamoto DELAY(1);
351 1.9 thorpej ack = CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAIN;
352 1.1 sakamoto
353 1.1 sakamoto /*
354 1.1 sakamoto * Now try reading data bits. If the ack failed, we still
355 1.1 sakamoto * need to clock through 16 cycles to keep the PHY(s) in sync.
356 1.1 sakamoto */
357 1.1 sakamoto if (ack) {
358 1.2 sakamoto for (i = 0; i < 16; i++) {
359 1.1 sakamoto SIO_CLR(VR_MIICMD_CLK);
360 1.1 sakamoto DELAY(1);
361 1.1 sakamoto SIO_SET(VR_MIICMD_CLK);
362 1.1 sakamoto DELAY(1);
363 1.1 sakamoto }
364 1.1 sakamoto goto fail;
365 1.1 sakamoto }
366 1.1 sakamoto
367 1.1 sakamoto for (i = 0x8000; i; i >>= 1) {
368 1.1 sakamoto SIO_CLR(VR_MIICMD_CLK);
369 1.1 sakamoto DELAY(1);
370 1.1 sakamoto if (!ack) {
371 1.9 thorpej if (CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAIN)
372 1.11 thorpej val |= i;
373 1.1 sakamoto DELAY(1);
374 1.1 sakamoto }
375 1.1 sakamoto SIO_SET(VR_MIICMD_CLK);
376 1.1 sakamoto DELAY(1);
377 1.1 sakamoto }
378 1.1 sakamoto
379 1.11 thorpej fail:
380 1.1 sakamoto
381 1.1 sakamoto SIO_CLR(VR_MIICMD_CLK);
382 1.1 sakamoto DELAY(1);
383 1.1 sakamoto SIO_SET(VR_MIICMD_CLK);
384 1.1 sakamoto DELAY(1);
385 1.1 sakamoto
386 1.11 thorpej return (val);
387 1.1 sakamoto }
388 1.1 sakamoto
389 1.1 sakamoto /*
390 1.1 sakamoto * Write to a PHY register through the MII.
391 1.1 sakamoto */
392 1.15 thorpej static void
393 1.15 thorpej vr_mii_writereg(self, phy, reg, val)
394 1.11 thorpej struct device *self;
395 1.11 thorpej int phy, reg, val;
396 1.1 sakamoto {
397 1.11 thorpej struct vr_softc *sc = (struct vr_softc *)self;
398 1.1 sakamoto
399 1.1 sakamoto CSR_WRITE_1(sc, VR_MIICMD, 0);
400 1.1 sakamoto VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
401 1.1 sakamoto
402 1.1 sakamoto /*
403 1.2 sakamoto * Turn on data output.
404 1.1 sakamoto */
405 1.1 sakamoto SIO_SET(VR_MIICMD_DIR);
406 1.1 sakamoto
407 1.1 sakamoto vr_mii_sync(sc);
408 1.1 sakamoto
409 1.11 thorpej vr_mii_send(sc, MII_COMMAND_START, 2);
410 1.11 thorpej vr_mii_send(sc, MII_COMMAND_WRITE, 2);
411 1.11 thorpej vr_mii_send(sc, phy, 5);
412 1.11 thorpej vr_mii_send(sc, reg, 5);
413 1.11 thorpej vr_mii_send(sc, MII_COMMAND_ACK, 2);
414 1.11 thorpej vr_mii_send(sc, val, 16);
415 1.1 sakamoto
416 1.1 sakamoto /* Idle bit. */
417 1.1 sakamoto SIO_SET(VR_MIICMD_CLK);
418 1.1 sakamoto DELAY(1);
419 1.1 sakamoto SIO_CLR(VR_MIICMD_CLK);
420 1.1 sakamoto DELAY(1);
421 1.1 sakamoto
422 1.1 sakamoto /*
423 1.1 sakamoto * Turn off xmit.
424 1.1 sakamoto */
425 1.1 sakamoto SIO_CLR(VR_MIICMD_DIR);
426 1.1 sakamoto }
427 1.1 sakamoto
428 1.15 thorpej static void
429 1.15 thorpej vr_mii_statchg(self)
430 1.11 thorpej struct device *self;
431 1.1 sakamoto {
432 1.11 thorpej struct vr_softc *sc = (struct vr_softc *)self;
433 1.11 thorpej int restart = 0;
434 1.1 sakamoto
435 1.11 thorpej /*
436 1.11 thorpej * In order to fiddle with the 'full-duplex' bit in the netconfig
437 1.11 thorpej * register, we first have to put the transmit and/or receive logic
438 1.11 thorpej * in the idle state.
439 1.11 thorpej */
440 1.11 thorpej if (CSR_READ_2(sc, VR_COMMAND) & (VR_CMD_TX_ON|VR_CMD_RX_ON)) {
441 1.11 thorpej restart = 1;
442 1.11 thorpej VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_TX_ON|VR_CMD_RX_ON));
443 1.11 thorpej }
444 1.1 sakamoto
445 1.11 thorpej if (sc->vr_mii.mii_media_active & IFM_FDX)
446 1.11 thorpej VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
447 1.11 thorpej else
448 1.11 thorpej VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
449 1.1 sakamoto
450 1.11 thorpej if (restart)
451 1.11 thorpej VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_RX_ON);
452 1.1 sakamoto
453 1.11 thorpej /* XXX Update ifp->if_baudrate */
454 1.1 sakamoto }
455 1.1 sakamoto
456 1.1 sakamoto /*
457 1.1 sakamoto * Calculate CRC of a multicast group address, return the lower 6 bits.
458 1.1 sakamoto */
459 1.15 thorpej static u_int8_t
460 1.15 thorpej vr_calchash(addr)
461 1.15 thorpej u_int8_t *addr;
462 1.15 thorpej {
463 1.15 thorpej u_int32_t crc, carry;
464 1.15 thorpej int i, j;
465 1.15 thorpej u_int8_t c;
466 1.1 sakamoto
467 1.1 sakamoto /* Compute CRC for the address value. */
468 1.1 sakamoto crc = 0xFFFFFFFF; /* initial value */
469 1.1 sakamoto
470 1.1 sakamoto for (i = 0; i < 6; i++) {
471 1.1 sakamoto c = *(addr + i);
472 1.1 sakamoto for (j = 0; j < 8; j++) {
473 1.1 sakamoto carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
474 1.1 sakamoto crc <<= 1;
475 1.1 sakamoto c >>= 1;
476 1.1 sakamoto if (carry)
477 1.1 sakamoto crc = (crc ^ 0x04c11db6) | carry;
478 1.1 sakamoto }
479 1.1 sakamoto }
480 1.1 sakamoto
481 1.1 sakamoto /* return the filter bit position */
482 1.2 sakamoto return ((crc >> 26) & 0x0000003F);
483 1.1 sakamoto }
484 1.1 sakamoto
485 1.1 sakamoto /*
486 1.1 sakamoto * Program the 64-bit multicast hash filter.
487 1.1 sakamoto */
488 1.15 thorpej static void
489 1.15 thorpej vr_setmulti(sc)
490 1.15 thorpej struct vr_softc *sc;
491 1.1 sakamoto {
492 1.15 thorpej struct ifnet *ifp;
493 1.15 thorpej int h = 0;
494 1.15 thorpej u_int32_t hashes[2] = { 0, 0 };
495 1.15 thorpej struct ether_multistep step;
496 1.15 thorpej struct ether_multi *enm;
497 1.15 thorpej int mcnt = 0;
498 1.15 thorpej u_int8_t rxfilt;
499 1.1 sakamoto
500 1.6 thorpej ifp = &sc->vr_ec.ec_if;
501 1.1 sakamoto
502 1.1 sakamoto rxfilt = CSR_READ_1(sc, VR_RXCFG);
503 1.1 sakamoto
504 1.1 sakamoto if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
505 1.1 sakamoto rxfilt |= VR_RXCFG_RX_MULTI;
506 1.1 sakamoto CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
507 1.1 sakamoto CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
508 1.1 sakamoto CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
509 1.1 sakamoto return;
510 1.1 sakamoto }
511 1.1 sakamoto
512 1.1 sakamoto /* first, zot all the existing hash bits */
513 1.1 sakamoto CSR_WRITE_4(sc, VR_MAR0, 0);
514 1.1 sakamoto CSR_WRITE_4(sc, VR_MAR1, 0);
515 1.1 sakamoto
516 1.1 sakamoto /* now program new ones */
517 1.2 sakamoto ETHER_FIRST_MULTI(step, &sc->vr_ec, enm);
518 1.2 sakamoto while (enm != NULL) {
519 1.2 sakamoto if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0)
520 1.2 sakamoto continue;
521 1.2 sakamoto
522 1.2 sakamoto h = vr_calchash(enm->enm_addrlo);
523 1.2 sakamoto
524 1.1 sakamoto if (h < 32)
525 1.1 sakamoto hashes[0] |= (1 << h);
526 1.1 sakamoto else
527 1.1 sakamoto hashes[1] |= (1 << (h - 32));
528 1.2 sakamoto ETHER_NEXT_MULTI(step, enm);
529 1.1 sakamoto mcnt++;
530 1.1 sakamoto }
531 1.1 sakamoto
532 1.1 sakamoto if (mcnt)
533 1.1 sakamoto rxfilt |= VR_RXCFG_RX_MULTI;
534 1.1 sakamoto else
535 1.1 sakamoto rxfilt &= ~VR_RXCFG_RX_MULTI;
536 1.1 sakamoto
537 1.1 sakamoto CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
538 1.1 sakamoto CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
539 1.1 sakamoto CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
540 1.1 sakamoto
541 1.1 sakamoto return;
542 1.1 sakamoto }
543 1.1 sakamoto
544 1.15 thorpej static void
545 1.15 thorpej vr_reset(sc)
546 1.15 thorpej struct vr_softc *sc;
547 1.1 sakamoto {
548 1.15 thorpej int i;
549 1.1 sakamoto
550 1.1 sakamoto VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET);
551 1.1 sakamoto
552 1.1 sakamoto for (i = 0; i < VR_TIMEOUT; i++) {
553 1.1 sakamoto DELAY(10);
554 1.1 sakamoto if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET))
555 1.1 sakamoto break;
556 1.1 sakamoto }
557 1.1 sakamoto if (i == VR_TIMEOUT)
558 1.6 thorpej printf("%s: reset never completed!\n",
559 1.6 thorpej sc->vr_dev.dv_xname);
560 1.1 sakamoto
561 1.1 sakamoto /* Wait a little while for the chip to get its brains in order. */
562 1.1 sakamoto DELAY(1000);
563 1.1 sakamoto
564 1.1 sakamoto return;
565 1.1 sakamoto }
566 1.1 sakamoto
567 1.1 sakamoto /*
568 1.1 sakamoto * Initialize the transmit descriptors.
569 1.1 sakamoto */
570 1.15 thorpej static int
571 1.15 thorpej vr_list_tx_init(sc)
572 1.15 thorpej struct vr_softc *sc;
573 1.1 sakamoto {
574 1.15 thorpej struct vr_chain_data *cd;
575 1.15 thorpej struct vr_list_data *ld;
576 1.15 thorpej int i;
577 1.1 sakamoto
578 1.1 sakamoto cd = &sc->vr_cdata;
579 1.1 sakamoto ld = sc->vr_ldata;
580 1.1 sakamoto for (i = 0; i < VR_TX_LIST_CNT; i++) {
581 1.1 sakamoto cd->vr_tx_chain[i].vr_ptr = &ld->vr_tx_list[i];
582 1.1 sakamoto if (i == (VR_TX_LIST_CNT - 1))
583 1.2 sakamoto cd->vr_tx_chain[i].vr_nextdesc =
584 1.1 sakamoto &cd->vr_tx_chain[0];
585 1.1 sakamoto else
586 1.1 sakamoto cd->vr_tx_chain[i].vr_nextdesc =
587 1.1 sakamoto &cd->vr_tx_chain[i + 1];
588 1.1 sakamoto }
589 1.1 sakamoto
590 1.1 sakamoto cd->vr_tx_free = &cd->vr_tx_chain[0];
591 1.1 sakamoto cd->vr_tx_tail = cd->vr_tx_head = NULL;
592 1.1 sakamoto
593 1.2 sakamoto return (0);
594 1.1 sakamoto }
595 1.1 sakamoto
596 1.1 sakamoto
597 1.1 sakamoto /*
598 1.1 sakamoto * Initialize the RX descriptors and allocate mbufs for them. Note that
599 1.1 sakamoto * we arrange the descriptors in a closed ring, so that the last descriptor
600 1.1 sakamoto * points back to the first.
601 1.1 sakamoto */
602 1.15 thorpej static int
603 1.15 thorpej vr_list_rx_init(sc)
604 1.15 thorpej struct vr_softc *sc;
605 1.1 sakamoto {
606 1.15 thorpej struct vr_chain_data *cd;
607 1.15 thorpej struct vr_list_data *ld;
608 1.15 thorpej int i;
609 1.1 sakamoto
610 1.1 sakamoto cd = &sc->vr_cdata;
611 1.1 sakamoto ld = sc->vr_ldata;
612 1.1 sakamoto
613 1.1 sakamoto for (i = 0; i < VR_RX_LIST_CNT; i++) {
614 1.1 sakamoto cd->vr_rx_chain[i].vr_ptr =
615 1.1 sakamoto (struct vr_desc *)&ld->vr_rx_list[i];
616 1.1 sakamoto if (vr_newbuf(sc, &cd->vr_rx_chain[i]) == ENOBUFS)
617 1.2 sakamoto return (ENOBUFS);
618 1.1 sakamoto if (i == (VR_RX_LIST_CNT - 1)) {
619 1.1 sakamoto cd->vr_rx_chain[i].vr_nextdesc =
620 1.1 sakamoto &cd->vr_rx_chain[0];
621 1.1 sakamoto ld->vr_rx_list[i].vr_next =
622 1.1 sakamoto vtophys(&ld->vr_rx_list[0]);
623 1.1 sakamoto } else {
624 1.1 sakamoto cd->vr_rx_chain[i].vr_nextdesc =
625 1.1 sakamoto &cd->vr_rx_chain[i + 1];
626 1.1 sakamoto ld->vr_rx_list[i].vr_next =
627 1.1 sakamoto vtophys(&ld->vr_rx_list[i + 1]);
628 1.1 sakamoto }
629 1.1 sakamoto }
630 1.1 sakamoto
631 1.1 sakamoto cd->vr_rx_head = &cd->vr_rx_chain[0];
632 1.1 sakamoto
633 1.2 sakamoto return (0);
634 1.1 sakamoto }
635 1.1 sakamoto
636 1.1 sakamoto /*
637 1.1 sakamoto * Initialize an RX descriptor and attach an MBUF cluster.
638 1.1 sakamoto * Note: the length fields are only 11 bits wide, which means the
639 1.1 sakamoto * largest size we can specify is 2047. This is important because
640 1.1 sakamoto * MCLBYTES is 2048, so we have to subtract one otherwise we'll
641 1.1 sakamoto * overflow the field and make a mess.
642 1.1 sakamoto */
643 1.15 thorpej static int
644 1.15 thorpej vr_newbuf(sc, c)
645 1.15 thorpej struct vr_softc *sc;
646 1.15 thorpej struct vr_chain_onefrag *c;
647 1.1 sakamoto {
648 1.15 thorpej struct mbuf *m_new = NULL;
649 1.1 sakamoto
650 1.1 sakamoto MGETHDR(m_new, M_DONTWAIT, MT_DATA);
651 1.1 sakamoto if (m_new == NULL) {
652 1.6 thorpej printf("%s: no memory for rx list -- packet dropped!\n",
653 1.6 thorpej sc->vr_dev.dv_xname);
654 1.2 sakamoto return (ENOBUFS);
655 1.1 sakamoto }
656 1.1 sakamoto
657 1.1 sakamoto MCLGET(m_new, M_DONTWAIT);
658 1.1 sakamoto if (!(m_new->m_flags & M_EXT)) {
659 1.6 thorpej printf("%s: no memory for rx list -- packet dropped!\n",
660 1.6 thorpej sc->vr_dev.dv_xname);
661 1.1 sakamoto m_freem(m_new);
662 1.2 sakamoto return (ENOBUFS);
663 1.1 sakamoto }
664 1.1 sakamoto
665 1.1 sakamoto c->vr_mbuf = m_new;
666 1.1 sakamoto c->vr_ptr->vr_status = VR_RXSTAT;
667 1.1 sakamoto c->vr_ptr->vr_data = vtophys(mtod(m_new, caddr_t));
668 1.1 sakamoto c->vr_ptr->vr_ctl = VR_RXCTL | VR_RXLEN;
669 1.1 sakamoto
670 1.2 sakamoto return (0);
671 1.1 sakamoto }
672 1.1 sakamoto
673 1.1 sakamoto /*
674 1.1 sakamoto * A frame has been uploaded: pass the resulting mbuf chain up to
675 1.1 sakamoto * the higher level protocols.
676 1.1 sakamoto */
677 1.15 thorpej static void
678 1.15 thorpej vr_rxeof(sc)
679 1.15 thorpej struct vr_softc *sc;
680 1.1 sakamoto {
681 1.15 thorpej struct ether_header *eh;
682 1.15 thorpej struct mbuf *m;
683 1.15 thorpej struct ifnet *ifp;
684 1.15 thorpej struct vr_chain_onefrag *cur_rx;
685 1.15 thorpej int total_len = 0;
686 1.15 thorpej u_int32_t rxstat;
687 1.1 sakamoto
688 1.6 thorpej ifp = &sc->vr_ec.ec_if;
689 1.1 sakamoto
690 1.2 sakamoto while (!((rxstat = sc->vr_cdata.vr_rx_head->vr_ptr->vr_status) &
691 1.1 sakamoto VR_RXSTAT_OWN)) {
692 1.1 sakamoto cur_rx = sc->vr_cdata.vr_rx_head;
693 1.1 sakamoto sc->vr_cdata.vr_rx_head = cur_rx->vr_nextdesc;
694 1.1 sakamoto
695 1.1 sakamoto /*
696 1.1 sakamoto * If an error occurs, update stats, clear the
697 1.1 sakamoto * status word and leave the mbuf cluster in place:
698 1.1 sakamoto * it should simply get re-used next time this descriptor
699 1.2 sakamoto * comes up in the ring.
700 1.1 sakamoto */
701 1.1 sakamoto if (rxstat & VR_RXSTAT_RXERR) {
702 1.1 sakamoto ifp->if_ierrors++;
703 1.6 thorpej printf("%s: rx error: ", sc->vr_dev.dv_xname);
704 1.2 sakamoto switch (rxstat & 0x000000FF) {
705 1.1 sakamoto case VR_RXSTAT_CRCERR:
706 1.1 sakamoto printf("crc error\n");
707 1.1 sakamoto break;
708 1.1 sakamoto case VR_RXSTAT_FRAMEALIGNERR:
709 1.1 sakamoto printf("frame alignment error\n");
710 1.1 sakamoto break;
711 1.1 sakamoto case VR_RXSTAT_FIFOOFLOW:
712 1.1 sakamoto printf("FIFO overflow\n");
713 1.1 sakamoto break;
714 1.1 sakamoto case VR_RXSTAT_GIANT:
715 1.1 sakamoto printf("received giant packet\n");
716 1.1 sakamoto break;
717 1.1 sakamoto case VR_RXSTAT_RUNT:
718 1.1 sakamoto printf("received runt packet\n");
719 1.1 sakamoto break;
720 1.1 sakamoto case VR_RXSTAT_BUSERR:
721 1.1 sakamoto printf("system bus error\n");
722 1.1 sakamoto break;
723 1.1 sakamoto case VR_RXSTAT_BUFFERR:
724 1.1 sakamoto printf("rx buffer error\n");
725 1.1 sakamoto break;
726 1.1 sakamoto default:
727 1.1 sakamoto printf("unknown rx error\n");
728 1.1 sakamoto break;
729 1.1 sakamoto }
730 1.1 sakamoto cur_rx->vr_ptr->vr_status = VR_RXSTAT;
731 1.1 sakamoto cur_rx->vr_ptr->vr_ctl = VR_RXCTL|VR_RXLEN;
732 1.1 sakamoto continue;
733 1.1 sakamoto }
734 1.1 sakamoto
735 1.2 sakamoto /* No errors; receive the packet. */
736 1.1 sakamoto total_len = VR_RXBYTES(cur_rx->vr_ptr->vr_status);
737 1.1 sakamoto
738 1.1 sakamoto /*
739 1.1 sakamoto * XXX The VIA Rhine chip includes the CRC with every
740 1.1 sakamoto * received frame, and there's no way to turn this
741 1.1 sakamoto * behavior off (at least, I can't find anything in
742 1.2 sakamoto * the manual that explains how to do it) so we have
743 1.1 sakamoto * to trim off the CRC manually.
744 1.1 sakamoto */
745 1.1 sakamoto total_len -= ETHER_CRC_LEN;
746 1.1 sakamoto
747 1.17 thorpej #ifdef __NO_STRICT_ALIGNMENT
748 1.1 sakamoto /*
749 1.1 sakamoto * Try to conjure up a new mbuf cluster. If that
750 1.1 sakamoto * fails, it means we have an out of memory condition and
751 1.1 sakamoto * should leave the buffer in place and continue. This will
752 1.1 sakamoto * result in a lost packet, but there's little else we
753 1.1 sakamoto * can do in this situation.
754 1.1 sakamoto */
755 1.17 thorpej m = cur_rx->vr_mbuf;
756 1.1 sakamoto if (vr_newbuf(sc, cur_rx) == ENOBUFS) {
757 1.1 sakamoto ifp->if_ierrors++;
758 1.1 sakamoto cur_rx->vr_ptr->vr_status = VR_RXSTAT;
759 1.1 sakamoto cur_rx->vr_ptr->vr_ctl = VR_RXCTL|VR_RXLEN;
760 1.1 sakamoto continue;
761 1.1 sakamoto }
762 1.17 thorpej #else
763 1.17 thorpej /*
764 1.17 thorpej * The Rhine's packet buffers must be 4-byte aligned.
765 1.17 thorpej * But this means that the data after the Ethernet header
766 1.17 thorpej * is misaligned. We must allocate a new buffer and
767 1.17 thorpej * copy the data, shifted forward 2 bytes.
768 1.17 thorpej */
769 1.17 thorpej MGETHDR(m, M_DONTWAIT, MT_DATA);
770 1.17 thorpej if (m == NULL) {
771 1.17 thorpej dropit:
772 1.17 thorpej ifp->if_ierrors++;
773 1.17 thorpej cur_rx->vr_ptr->vr_status = VR_RXSTAT;
774 1.17 thorpej cur_rx->vr_ptr->vr_ctl = VR_RXCTL|VR_RXLEN;
775 1.17 thorpej continue;
776 1.17 thorpej }
777 1.17 thorpej if (total_len > (MHLEN - 2)) {
778 1.17 thorpej MCLGET(m, M_DONTWAIT);
779 1.17 thorpej if (m == NULL)
780 1.17 thorpej goto dropit;
781 1.17 thorpej }
782 1.17 thorpej m->m_data += 2;
783 1.17 thorpej
784 1.17 thorpej /*
785 1.17 thorpej * Note that we use clusters for incoming frames, so the
786 1.17 thorpej * buffer is virtually contiguous.
787 1.17 thorpej */
788 1.17 thorpej memcpy(mtod(m, caddr_t), mtod(cur_rx->vr_mbuf, caddr_t),
789 1.17 thorpej total_len);
790 1.17 thorpej
791 1.17 thorpej /* Allow the recieve descriptor to continue using its mbuf. */
792 1.17 thorpej cur_rx->vr_ptr->vr_status = VR_RXSTAT;
793 1.17 thorpej cur_rx->vr_ptr->vr_ctl = VR_RXCTL|VR_RXLEN;
794 1.17 thorpej #endif /* __NO_STRICT_ALIGNMENT */
795 1.1 sakamoto
796 1.1 sakamoto ifp->if_ipackets++;
797 1.1 sakamoto eh = mtod(m, struct ether_header *);
798 1.1 sakamoto m->m_pkthdr.rcvif = ifp;
799 1.1 sakamoto m->m_pkthdr.len = m->m_len = total_len;
800 1.1 sakamoto #if NBPFILTER > 0
801 1.1 sakamoto /*
802 1.1 sakamoto * Handle BPF listeners. Let the BPF user see the packet, but
803 1.1 sakamoto * don't pass it up to the ether_input() layer unless it's
804 1.1 sakamoto * a broadcast packet, multicast packet, matches our ethernet
805 1.1 sakamoto * address or the interface is in promiscuous mode.
806 1.1 sakamoto */
807 1.1 sakamoto if (ifp->if_bpf) {
808 1.2 sakamoto bpf_mtap(ifp->if_bpf, m);
809 1.1 sakamoto if (ifp->if_flags & IFF_PROMISC &&
810 1.2 sakamoto (memcmp(eh->ether_dhost, sc->vr_enaddr,
811 1.1 sakamoto ETHER_ADDR_LEN) &&
812 1.1 sakamoto (eh->ether_dhost[0] & 1) == 0)) {
813 1.1 sakamoto m_freem(m);
814 1.1 sakamoto continue;
815 1.1 sakamoto }
816 1.1 sakamoto }
817 1.1 sakamoto #endif
818 1.1 sakamoto /* Remove header from mbuf and pass it on. */
819 1.2 sakamoto m_adj(m, sizeof (struct ether_header));
820 1.1 sakamoto ether_input(ifp, eh, m);
821 1.1 sakamoto }
822 1.1 sakamoto }
823 1.1 sakamoto
824 1.15 thorpej void
825 1.15 thorpej vr_rxeoc(sc)
826 1.15 thorpej struct vr_softc *sc;
827 1.1 sakamoto {
828 1.1 sakamoto
829 1.1 sakamoto vr_rxeof(sc);
830 1.1 sakamoto VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
831 1.1 sakamoto CSR_WRITE_4(sc, VR_RXADDR, vtophys(sc->vr_cdata.vr_rx_head->vr_ptr));
832 1.1 sakamoto VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
833 1.1 sakamoto VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
834 1.1 sakamoto }
835 1.1 sakamoto
836 1.1 sakamoto /*
837 1.1 sakamoto * A frame was downloaded to the chip. It's safe for us to clean up
838 1.1 sakamoto * the list buffers.
839 1.1 sakamoto */
840 1.1 sakamoto
841 1.15 thorpej static void
842 1.15 thorpej vr_txeof(sc)
843 1.15 thorpej struct vr_softc *sc;
844 1.1 sakamoto {
845 1.15 thorpej struct vr_chain *cur_tx;
846 1.15 thorpej struct ifnet *ifp;
847 1.15 thorpej register struct mbuf *n;
848 1.1 sakamoto
849 1.6 thorpej ifp = &sc->vr_ec.ec_if;
850 1.1 sakamoto
851 1.1 sakamoto /* Clear the timeout timer. */
852 1.1 sakamoto ifp->if_timer = 0;
853 1.1 sakamoto
854 1.1 sakamoto /* Sanity check. */
855 1.1 sakamoto if (sc->vr_cdata.vr_tx_head == NULL)
856 1.1 sakamoto return;
857 1.1 sakamoto
858 1.1 sakamoto /*
859 1.1 sakamoto * Go through our tx list and free mbufs for those
860 1.1 sakamoto * frames that have been transmitted.
861 1.1 sakamoto */
862 1.2 sakamoto while (sc->vr_cdata.vr_tx_head->vr_mbuf != NULL) {
863 1.15 thorpej u_int32_t txstat;
864 1.1 sakamoto
865 1.1 sakamoto cur_tx = sc->vr_cdata.vr_tx_head;
866 1.1 sakamoto txstat = cur_tx->vr_ptr->vr_status;
867 1.1 sakamoto
868 1.1 sakamoto if (txstat & VR_TXSTAT_OWN)
869 1.1 sakamoto break;
870 1.1 sakamoto
871 1.1 sakamoto if (txstat & VR_TXSTAT_ERRSUM) {
872 1.1 sakamoto ifp->if_oerrors++;
873 1.1 sakamoto if (txstat & VR_TXSTAT_DEFER)
874 1.1 sakamoto ifp->if_collisions++;
875 1.1 sakamoto if (txstat & VR_TXSTAT_LATECOLL)
876 1.1 sakamoto ifp->if_collisions++;
877 1.1 sakamoto }
878 1.1 sakamoto
879 1.1 sakamoto ifp->if_collisions +=(txstat & VR_TXSTAT_COLLCNT) >> 3;
880 1.1 sakamoto
881 1.1 sakamoto ifp->if_opackets++;
882 1.2 sakamoto MFREE(cur_tx->vr_mbuf, n);
883 1.1 sakamoto cur_tx->vr_mbuf = NULL;
884 1.1 sakamoto
885 1.1 sakamoto if (sc->vr_cdata.vr_tx_head == sc->vr_cdata.vr_tx_tail) {
886 1.1 sakamoto sc->vr_cdata.vr_tx_head = NULL;
887 1.1 sakamoto sc->vr_cdata.vr_tx_tail = NULL;
888 1.1 sakamoto break;
889 1.1 sakamoto }
890 1.1 sakamoto
891 1.1 sakamoto sc->vr_cdata.vr_tx_head = cur_tx->vr_nextdesc;
892 1.1 sakamoto }
893 1.1 sakamoto }
894 1.1 sakamoto
895 1.1 sakamoto /*
896 1.1 sakamoto * TX 'end of channel' interrupt handler.
897 1.1 sakamoto */
898 1.15 thorpej static void
899 1.15 thorpej vr_txeoc(sc)
900 1.15 thorpej struct vr_softc *sc;
901 1.1 sakamoto {
902 1.15 thorpej struct ifnet *ifp;
903 1.1 sakamoto
904 1.6 thorpej ifp = &sc->vr_ec.ec_if;
905 1.1 sakamoto
906 1.1 sakamoto ifp->if_timer = 0;
907 1.1 sakamoto
908 1.1 sakamoto if (sc->vr_cdata.vr_tx_head == NULL) {
909 1.1 sakamoto ifp->if_flags &= ~IFF_OACTIVE;
910 1.1 sakamoto sc->vr_cdata.vr_tx_tail = NULL;
911 1.1 sakamoto }
912 1.1 sakamoto }
913 1.1 sakamoto
914 1.16 thorpej static int
915 1.15 thorpej vr_intr(arg)
916 1.15 thorpej void *arg;
917 1.1 sakamoto {
918 1.15 thorpej struct vr_softc *sc;
919 1.15 thorpej struct ifnet *ifp;
920 1.15 thorpej u_int16_t status;
921 1.16 thorpej int handled = 0;
922 1.1 sakamoto
923 1.1 sakamoto sc = arg;
924 1.6 thorpej ifp = &sc->vr_ec.ec_if;
925 1.1 sakamoto
926 1.1 sakamoto /* Supress unwanted interrupts. */
927 1.16 thorpej if ((ifp->if_flags & IFF_UP) == 0) {
928 1.1 sakamoto vr_stop(sc);
929 1.16 thorpej return (0);
930 1.1 sakamoto }
931 1.1 sakamoto
932 1.1 sakamoto /* Disable interrupts. */
933 1.1 sakamoto CSR_WRITE_2(sc, VR_IMR, 0x0000);
934 1.1 sakamoto
935 1.1 sakamoto for (;;) {
936 1.1 sakamoto status = CSR_READ_2(sc, VR_ISR);
937 1.1 sakamoto if (status)
938 1.1 sakamoto CSR_WRITE_2(sc, VR_ISR, status);
939 1.1 sakamoto
940 1.1 sakamoto if ((status & VR_INTRS) == 0)
941 1.1 sakamoto break;
942 1.1 sakamoto
943 1.16 thorpej handled = 1;
944 1.16 thorpej
945 1.1 sakamoto if (status & VR_ISR_RX_OK)
946 1.1 sakamoto vr_rxeof(sc);
947 1.1 sakamoto
948 1.1 sakamoto if ((status & VR_ISR_RX_ERR) || (status & VR_ISR_RX_NOBUF) ||
949 1.1 sakamoto (status & VR_ISR_RX_NOBUF) || (status & VR_ISR_RX_OFLOW) ||
950 1.1 sakamoto (status & VR_ISR_RX_DROPPED)) {
951 1.1 sakamoto vr_rxeof(sc);
952 1.1 sakamoto vr_rxeoc(sc);
953 1.1 sakamoto }
954 1.1 sakamoto
955 1.1 sakamoto if (status & VR_ISR_TX_OK) {
956 1.1 sakamoto vr_txeof(sc);
957 1.1 sakamoto vr_txeoc(sc);
958 1.1 sakamoto }
959 1.1 sakamoto
960 1.2 sakamoto if ((status & VR_ISR_TX_UNDERRUN)||(status & VR_ISR_TX_ABRT)) {
961 1.1 sakamoto ifp->if_oerrors++;
962 1.1 sakamoto vr_txeof(sc);
963 1.1 sakamoto if (sc->vr_cdata.vr_tx_head != NULL) {
964 1.1 sakamoto VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
965 1.1 sakamoto VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
966 1.1 sakamoto }
967 1.1 sakamoto }
968 1.1 sakamoto
969 1.1 sakamoto if (status & VR_ISR_BUSERR) {
970 1.1 sakamoto vr_reset(sc);
971 1.1 sakamoto vr_init(sc);
972 1.1 sakamoto }
973 1.1 sakamoto }
974 1.1 sakamoto
975 1.1 sakamoto /* Re-enable interrupts. */
976 1.1 sakamoto CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
977 1.1 sakamoto
978 1.1 sakamoto if (ifp->if_snd.ifq_head != NULL) {
979 1.1 sakamoto vr_start(ifp);
980 1.1 sakamoto }
981 1.16 thorpej
982 1.16 thorpej return (handled);
983 1.1 sakamoto }
984 1.1 sakamoto
985 1.1 sakamoto /*
986 1.1 sakamoto * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
987 1.1 sakamoto * pointers to the fragment pointers.
988 1.1 sakamoto */
989 1.15 thorpej static int
990 1.15 thorpej vr_encap(sc, c, m_head)
991 1.15 thorpej struct vr_softc *sc;
992 1.15 thorpej struct vr_chain *c;
993 1.15 thorpej struct mbuf *m_head;
994 1.15 thorpej {
995 1.15 thorpej int frag = 0;
996 1.15 thorpej struct vr_desc *f = NULL;
997 1.15 thorpej int total_len;
998 1.15 thorpej struct mbuf *m;
999 1.1 sakamoto
1000 1.1 sakamoto m = m_head;
1001 1.1 sakamoto total_len = 0;
1002 1.1 sakamoto
1003 1.1 sakamoto /*
1004 1.1 sakamoto * The VIA Rhine wants packet buffers to be longword
1005 1.1 sakamoto * aligned, but very often our mbufs aren't. Rather than
1006 1.1 sakamoto * waste time trying to decide when to copy and when not
1007 1.1 sakamoto * to copy, just do it all the time.
1008 1.1 sakamoto */
1009 1.1 sakamoto if (m != NULL) {
1010 1.1 sakamoto struct mbuf *m_new = NULL;
1011 1.1 sakamoto
1012 1.1 sakamoto MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1013 1.1 sakamoto if (m_new == NULL) {
1014 1.6 thorpej printf("%s: no memory for tx list",
1015 1.6 thorpej sc->vr_dev.dv_xname);
1016 1.2 sakamoto return (1);
1017 1.1 sakamoto }
1018 1.1 sakamoto if (m_head->m_pkthdr.len > MHLEN) {
1019 1.1 sakamoto MCLGET(m_new, M_DONTWAIT);
1020 1.1 sakamoto if (!(m_new->m_flags & M_EXT)) {
1021 1.1 sakamoto m_freem(m_new);
1022 1.6 thorpej printf("%s: no memory for tx list",
1023 1.6 thorpej sc->vr_dev.dv_xname);
1024 1.2 sakamoto return (1);
1025 1.1 sakamoto }
1026 1.1 sakamoto }
1027 1.2 sakamoto m_copydata(m_head, 0, m_head->m_pkthdr.len,
1028 1.1 sakamoto mtod(m_new, caddr_t));
1029 1.1 sakamoto m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1030 1.1 sakamoto m_freem(m_head);
1031 1.1 sakamoto m_head = m_new;
1032 1.1 sakamoto /*
1033 1.1 sakamoto * The Rhine chip doesn't auto-pad, so we have to make
1034 1.1 sakamoto * sure to pad short frames out to the minimum frame length
1035 1.1 sakamoto * ourselves.
1036 1.1 sakamoto */
1037 1.1 sakamoto if (m_head->m_len < VR_MIN_FRAMELEN) {
1038 1.1 sakamoto m_new->m_pkthdr.len += VR_MIN_FRAMELEN - m_new->m_len;
1039 1.1 sakamoto m_new->m_len = m_new->m_pkthdr.len;
1040 1.1 sakamoto }
1041 1.1 sakamoto f = c->vr_ptr;
1042 1.1 sakamoto f->vr_data = vtophys(mtod(m_new, caddr_t));
1043 1.1 sakamoto f->vr_ctl = total_len = m_new->m_len;
1044 1.1 sakamoto f->vr_ctl |= VR_TXCTL_TLINK|VR_TXCTL_FIRSTFRAG;
1045 1.1 sakamoto f->vr_status = 0;
1046 1.1 sakamoto frag = 1;
1047 1.1 sakamoto }
1048 1.1 sakamoto
1049 1.1 sakamoto c->vr_mbuf = m_head;
1050 1.1 sakamoto c->vr_ptr->vr_ctl |= VR_TXCTL_LASTFRAG|VR_TXCTL_FINT;
1051 1.1 sakamoto c->vr_ptr->vr_next = vtophys(c->vr_nextdesc->vr_ptr);
1052 1.1 sakamoto
1053 1.2 sakamoto return (0);
1054 1.1 sakamoto }
1055 1.1 sakamoto
1056 1.1 sakamoto /*
1057 1.1 sakamoto * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1058 1.1 sakamoto * to the mbuf data regions directly in the transmit lists. We also save a
1059 1.1 sakamoto * copy of the pointers since the transmit list fragment pointers are
1060 1.1 sakamoto * physical addresses.
1061 1.1 sakamoto */
1062 1.15 thorpej static void
1063 1.15 thorpej vr_start(ifp)
1064 1.15 thorpej struct ifnet *ifp;
1065 1.1 sakamoto {
1066 1.15 thorpej struct vr_softc *sc;
1067 1.15 thorpej struct mbuf *m_head = NULL;
1068 1.15 thorpej struct vr_chain *cur_tx = NULL, *start_tx;
1069 1.1 sakamoto
1070 1.1 sakamoto sc = ifp->if_softc;
1071 1.1 sakamoto
1072 1.1 sakamoto /*
1073 1.1 sakamoto * Check for an available queue slot. If there are none,
1074 1.1 sakamoto * punt.
1075 1.1 sakamoto */
1076 1.1 sakamoto if (sc->vr_cdata.vr_tx_free->vr_mbuf != NULL) {
1077 1.1 sakamoto ifp->if_flags |= IFF_OACTIVE;
1078 1.1 sakamoto return;
1079 1.1 sakamoto }
1080 1.1 sakamoto
1081 1.1 sakamoto start_tx = sc->vr_cdata.vr_tx_free;
1082 1.1 sakamoto
1083 1.2 sakamoto while (sc->vr_cdata.vr_tx_free->vr_mbuf == NULL) {
1084 1.1 sakamoto IF_DEQUEUE(&ifp->if_snd, m_head);
1085 1.1 sakamoto if (m_head == NULL)
1086 1.1 sakamoto break;
1087 1.1 sakamoto
1088 1.1 sakamoto /* Pick a descriptor off the free list. */
1089 1.1 sakamoto cur_tx = sc->vr_cdata.vr_tx_free;
1090 1.1 sakamoto sc->vr_cdata.vr_tx_free = cur_tx->vr_nextdesc;
1091 1.1 sakamoto
1092 1.1 sakamoto /* Pack the data into the descriptor. */
1093 1.1 sakamoto vr_encap(sc, cur_tx, m_head);
1094 1.1 sakamoto
1095 1.1 sakamoto if (cur_tx != start_tx)
1096 1.1 sakamoto VR_TXOWN(cur_tx) = VR_TXSTAT_OWN;
1097 1.1 sakamoto
1098 1.1 sakamoto #if NBPFILTER > 0
1099 1.1 sakamoto /*
1100 1.1 sakamoto * If there's a BPF listener, bounce a copy of this frame
1101 1.1 sakamoto * to him.
1102 1.1 sakamoto */
1103 1.1 sakamoto if (ifp->if_bpf)
1104 1.2 sakamoto bpf_mtap(ifp->if_bpf, cur_tx->vr_mbuf);
1105 1.2 sakamoto #endif
1106 1.1 sakamoto VR_TXOWN(cur_tx) = VR_TXSTAT_OWN;
1107 1.1 sakamoto VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_TX_GO);
1108 1.1 sakamoto }
1109 1.1 sakamoto
1110 1.1 sakamoto /*
1111 1.1 sakamoto * If there are no frames queued, bail.
1112 1.1 sakamoto */
1113 1.1 sakamoto if (cur_tx == NULL)
1114 1.1 sakamoto return;
1115 1.1 sakamoto
1116 1.1 sakamoto sc->vr_cdata.vr_tx_tail = cur_tx;
1117 1.1 sakamoto
1118 1.1 sakamoto if (sc->vr_cdata.vr_tx_head == NULL)
1119 1.1 sakamoto sc->vr_cdata.vr_tx_head = start_tx;
1120 1.1 sakamoto
1121 1.1 sakamoto /*
1122 1.1 sakamoto * Set a timeout in case the chip goes out to lunch.
1123 1.1 sakamoto */
1124 1.1 sakamoto ifp->if_timer = 5;
1125 1.1 sakamoto }
1126 1.1 sakamoto
1127 1.13 thorpej /*
1128 1.13 thorpej * Initialize the interface. Must be called at splnet.
1129 1.13 thorpej */
1130 1.15 thorpej static void
1131 1.15 thorpej vr_init(xsc)
1132 1.15 thorpej void *xsc;
1133 1.1 sakamoto {
1134 1.15 thorpej struct vr_softc *sc = xsc;
1135 1.15 thorpej struct ifnet *ifp = &sc->vr_ec.ec_if;
1136 1.1 sakamoto
1137 1.1 sakamoto /*
1138 1.1 sakamoto * Cancel pending I/O and free all RX/TX buffers.
1139 1.1 sakamoto */
1140 1.1 sakamoto vr_stop(sc);
1141 1.1 sakamoto vr_reset(sc);
1142 1.1 sakamoto
1143 1.1 sakamoto VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
1144 1.1 sakamoto VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_STORENFWD);
1145 1.1 sakamoto
1146 1.1 sakamoto VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
1147 1.1 sakamoto VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
1148 1.1 sakamoto
1149 1.1 sakamoto /* Init circular RX list. */
1150 1.1 sakamoto if (vr_list_rx_init(sc) == ENOBUFS) {
1151 1.6 thorpej printf("%s: initialization failed: no "
1152 1.6 thorpej "memory for rx buffers\n", sc->vr_dev.dv_xname);
1153 1.1 sakamoto vr_stop(sc);
1154 1.1 sakamoto return;
1155 1.1 sakamoto }
1156 1.1 sakamoto
1157 1.1 sakamoto /*
1158 1.1 sakamoto * Init tx descriptors.
1159 1.1 sakamoto */
1160 1.1 sakamoto vr_list_tx_init(sc);
1161 1.1 sakamoto
1162 1.1 sakamoto /* If we want promiscuous mode, set the allframes bit. */
1163 1.1 sakamoto if (ifp->if_flags & IFF_PROMISC)
1164 1.1 sakamoto VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1165 1.1 sakamoto else
1166 1.1 sakamoto VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1167 1.1 sakamoto
1168 1.1 sakamoto /* Set capture broadcast bit to capture broadcast frames. */
1169 1.1 sakamoto if (ifp->if_flags & IFF_BROADCAST)
1170 1.1 sakamoto VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1171 1.1 sakamoto else
1172 1.1 sakamoto VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1173 1.1 sakamoto
1174 1.1 sakamoto /*
1175 1.1 sakamoto * Program the multicast filter, if necessary.
1176 1.1 sakamoto */
1177 1.1 sakamoto vr_setmulti(sc);
1178 1.1 sakamoto
1179 1.1 sakamoto /*
1180 1.1 sakamoto * Load the address of the RX list.
1181 1.1 sakamoto */
1182 1.1 sakamoto CSR_WRITE_4(sc, VR_RXADDR, vtophys(sc->vr_cdata.vr_rx_head->vr_ptr));
1183 1.1 sakamoto
1184 1.1 sakamoto /* Enable receiver and transmitter. */
1185 1.1 sakamoto CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START|
1186 1.1 sakamoto VR_CMD_TX_ON|VR_CMD_RX_ON|
1187 1.1 sakamoto VR_CMD_RX_GO);
1188 1.1 sakamoto
1189 1.11 thorpej /* Set current media. */
1190 1.11 thorpej mii_mediachg(&sc->vr_mii);
1191 1.1 sakamoto
1192 1.1 sakamoto CSR_WRITE_4(sc, VR_TXADDR, vtophys(&sc->vr_ldata->vr_tx_list[0]));
1193 1.1 sakamoto
1194 1.1 sakamoto /*
1195 1.1 sakamoto * Enable interrupts.
1196 1.1 sakamoto */
1197 1.1 sakamoto CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
1198 1.1 sakamoto CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1199 1.1 sakamoto
1200 1.1 sakamoto ifp->if_flags |= IFF_RUNNING;
1201 1.1 sakamoto ifp->if_flags &= ~IFF_OACTIVE;
1202 1.1 sakamoto
1203 1.11 thorpej /* Start one second timer. */
1204 1.11 thorpej timeout(vr_tick, sc, hz);
1205 1.1 sakamoto }
1206 1.1 sakamoto
1207 1.1 sakamoto /*
1208 1.1 sakamoto * Set media options.
1209 1.1 sakamoto */
1210 1.15 thorpej static int
1211 1.15 thorpej vr_ifmedia_upd(ifp)
1212 1.15 thorpej struct ifnet *ifp;
1213 1.1 sakamoto {
1214 1.11 thorpej struct vr_softc *sc = ifp->if_softc;
1215 1.1 sakamoto
1216 1.11 thorpej if (ifp->if_flags & IFF_UP)
1217 1.11 thorpej mii_mediachg(&sc->vr_mii);
1218 1.2 sakamoto return (0);
1219 1.1 sakamoto }
1220 1.1 sakamoto
1221 1.1 sakamoto /*
1222 1.1 sakamoto * Report current media status.
1223 1.1 sakamoto */
1224 1.15 thorpej static void
1225 1.15 thorpej vr_ifmedia_sts(ifp, ifmr)
1226 1.15 thorpej struct ifnet *ifp;
1227 1.15 thorpej struct ifmediareq *ifmr;
1228 1.1 sakamoto {
1229 1.11 thorpej struct vr_softc *sc = ifp->if_softc;
1230 1.1 sakamoto
1231 1.11 thorpej mii_pollstat(&sc->vr_mii);
1232 1.11 thorpej ifmr->ifm_status = sc->vr_mii.mii_media_status;
1233 1.11 thorpej ifmr->ifm_active = sc->vr_mii.mii_media_active;
1234 1.1 sakamoto }
1235 1.1 sakamoto
1236 1.15 thorpej static int
1237 1.15 thorpej vr_ioctl(ifp, command, data)
1238 1.15 thorpej struct ifnet *ifp;
1239 1.15 thorpej u_long command;
1240 1.15 thorpej caddr_t data;
1241 1.15 thorpej {
1242 1.15 thorpej struct vr_softc *sc = ifp->if_softc;
1243 1.15 thorpej struct ifreq *ifr = (struct ifreq *)data;
1244 1.15 thorpej struct ifaddr *ifa = (struct ifaddr *)data;
1245 1.15 thorpej int s, error = 0;
1246 1.1 sakamoto
1247 1.12 thorpej s = splnet();
1248 1.1 sakamoto
1249 1.2 sakamoto switch (command) {
1250 1.2 sakamoto case SIOCSIFADDR:
1251 1.2 sakamoto ifp->if_flags |= IFF_UP;
1252 1.2 sakamoto
1253 1.2 sakamoto switch (ifa->ifa_addr->sa_family) {
1254 1.2 sakamoto #ifdef INET
1255 1.2 sakamoto case AF_INET:
1256 1.2 sakamoto vr_init(sc);
1257 1.2 sakamoto arp_ifinit(ifp, ifa);
1258 1.2 sakamoto break;
1259 1.2 sakamoto #endif /* INET */
1260 1.2 sakamoto default:
1261 1.2 sakamoto vr_init(sc);
1262 1.2 sakamoto break;
1263 1.2 sakamoto }
1264 1.2 sakamoto break;
1265 1.2 sakamoto
1266 1.2 sakamoto case SIOCGIFADDR:
1267 1.2 sakamoto bcopy((caddr_t) sc->vr_enaddr,
1268 1.2 sakamoto (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
1269 1.2 sakamoto ETHER_ADDR_LEN);
1270 1.2 sakamoto break;
1271 1.2 sakamoto
1272 1.2 sakamoto case SIOCSIFMTU:
1273 1.2 sakamoto if (ifr->ifr_mtu > ETHERMTU)
1274 1.2 sakamoto error = EINVAL;
1275 1.2 sakamoto else
1276 1.2 sakamoto ifp->if_mtu = ifr->ifr_mtu;
1277 1.2 sakamoto break;
1278 1.2 sakamoto
1279 1.1 sakamoto case SIOCSIFFLAGS:
1280 1.1 sakamoto if (ifp->if_flags & IFF_UP) {
1281 1.1 sakamoto vr_init(sc);
1282 1.1 sakamoto } else {
1283 1.1 sakamoto if (ifp->if_flags & IFF_RUNNING)
1284 1.1 sakamoto vr_stop(sc);
1285 1.1 sakamoto }
1286 1.1 sakamoto error = 0;
1287 1.1 sakamoto break;
1288 1.1 sakamoto case SIOCADDMULTI:
1289 1.1 sakamoto case SIOCDELMULTI:
1290 1.2 sakamoto if (command == SIOCADDMULTI)
1291 1.2 sakamoto error = ether_addmulti(ifr, &sc->vr_ec);
1292 1.2 sakamoto else
1293 1.2 sakamoto error = ether_delmulti(ifr, &sc->vr_ec);
1294 1.2 sakamoto
1295 1.2 sakamoto if (error == ENETRESET) {
1296 1.2 sakamoto vr_setmulti(sc);
1297 1.2 sakamoto error = 0;
1298 1.2 sakamoto }
1299 1.1 sakamoto break;
1300 1.1 sakamoto case SIOCGIFMEDIA:
1301 1.1 sakamoto case SIOCSIFMEDIA:
1302 1.11 thorpej error = ifmedia_ioctl(ifp, ifr, &sc->vr_mii.mii_media, command);
1303 1.1 sakamoto break;
1304 1.1 sakamoto default:
1305 1.1 sakamoto error = EINVAL;
1306 1.1 sakamoto break;
1307 1.1 sakamoto }
1308 1.1 sakamoto
1309 1.13 thorpej splx(s);
1310 1.1 sakamoto
1311 1.2 sakamoto return (error);
1312 1.1 sakamoto }
1313 1.1 sakamoto
1314 1.15 thorpej static void
1315 1.15 thorpej vr_watchdog(ifp)
1316 1.15 thorpej struct ifnet *ifp;
1317 1.1 sakamoto {
1318 1.15 thorpej struct vr_softc *sc;
1319 1.1 sakamoto
1320 1.1 sakamoto sc = ifp->if_softc;
1321 1.1 sakamoto
1322 1.1 sakamoto ifp->if_oerrors++;
1323 1.6 thorpej printf("%s: watchdog timeout\n", sc->vr_dev.dv_xname);
1324 1.1 sakamoto
1325 1.1 sakamoto vr_stop(sc);
1326 1.1 sakamoto vr_reset(sc);
1327 1.1 sakamoto vr_init(sc);
1328 1.1 sakamoto
1329 1.1 sakamoto if (ifp->if_snd.ifq_head != NULL)
1330 1.1 sakamoto vr_start(ifp);
1331 1.1 sakamoto
1332 1.1 sakamoto return;
1333 1.1 sakamoto }
1334 1.1 sakamoto
1335 1.1 sakamoto /*
1336 1.11 thorpej * One second timer, used to tick MII.
1337 1.11 thorpej */
1338 1.11 thorpej static void
1339 1.11 thorpej vr_tick(arg)
1340 1.11 thorpej void *arg;
1341 1.11 thorpej {
1342 1.11 thorpej struct vr_softc *sc = arg;
1343 1.11 thorpej int s;
1344 1.11 thorpej
1345 1.12 thorpej s = splnet();
1346 1.11 thorpej mii_tick(&sc->vr_mii);
1347 1.11 thorpej splx(s);
1348 1.11 thorpej
1349 1.11 thorpej timeout(vr_tick, sc, hz);
1350 1.11 thorpej }
1351 1.11 thorpej
1352 1.11 thorpej /*
1353 1.1 sakamoto * Stop the adapter and free any mbufs allocated to the
1354 1.1 sakamoto * RX and TX lists.
1355 1.1 sakamoto */
1356 1.15 thorpej static void
1357 1.15 thorpej vr_stop(sc)
1358 1.15 thorpej struct vr_softc *sc;
1359 1.1 sakamoto {
1360 1.15 thorpej struct ifnet *ifp;
1361 1.15 thorpej int i;
1362 1.1 sakamoto
1363 1.11 thorpej /* Cancel one second timer. */
1364 1.11 thorpej untimeout(vr_tick, sc);
1365 1.11 thorpej
1366 1.6 thorpej ifp = &sc->vr_ec.ec_if;
1367 1.1 sakamoto ifp->if_timer = 0;
1368 1.1 sakamoto
1369 1.1 sakamoto VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
1370 1.1 sakamoto VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON));
1371 1.1 sakamoto CSR_WRITE_2(sc, VR_IMR, 0x0000);
1372 1.1 sakamoto CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
1373 1.1 sakamoto CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
1374 1.1 sakamoto
1375 1.1 sakamoto /*
1376 1.1 sakamoto * Free data in the RX lists.
1377 1.1 sakamoto */
1378 1.1 sakamoto for (i = 0; i < VR_RX_LIST_CNT; i++) {
1379 1.1 sakamoto if (sc->vr_cdata.vr_rx_chain[i].vr_mbuf != NULL) {
1380 1.1 sakamoto m_freem(sc->vr_cdata.vr_rx_chain[i].vr_mbuf);
1381 1.1 sakamoto sc->vr_cdata.vr_rx_chain[i].vr_mbuf = NULL;
1382 1.1 sakamoto }
1383 1.1 sakamoto }
1384 1.1 sakamoto bzero((char *)&sc->vr_ldata->vr_rx_list,
1385 1.2 sakamoto sizeof (sc->vr_ldata->vr_rx_list));
1386 1.1 sakamoto
1387 1.1 sakamoto /*
1388 1.1 sakamoto * Free the TX list buffers.
1389 1.1 sakamoto */
1390 1.1 sakamoto for (i = 0; i < VR_TX_LIST_CNT; i++) {
1391 1.1 sakamoto if (sc->vr_cdata.vr_tx_chain[i].vr_mbuf != NULL) {
1392 1.1 sakamoto m_freem(sc->vr_cdata.vr_tx_chain[i].vr_mbuf);
1393 1.1 sakamoto sc->vr_cdata.vr_tx_chain[i].vr_mbuf = NULL;
1394 1.1 sakamoto }
1395 1.1 sakamoto }
1396 1.1 sakamoto
1397 1.1 sakamoto bzero((char *)&sc->vr_ldata->vr_tx_list,
1398 1.2 sakamoto sizeof (sc->vr_ldata->vr_tx_list));
1399 1.1 sakamoto
1400 1.1 sakamoto ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1401 1.1 sakamoto }
1402 1.1 sakamoto
1403 1.3 sakamoto static struct vr_type *vr_lookup __P((struct pci_attach_args *));
1404 1.2 sakamoto static int vr_probe __P((struct device *, struct cfdata *, void *));
1405 1.2 sakamoto static void vr_attach __P((struct device *, struct device *, void *));
1406 1.2 sakamoto static void vr_shutdown __P((void *));
1407 1.2 sakamoto
1408 1.2 sakamoto struct cfattach vr_ca = {
1409 1.2 sakamoto sizeof (struct vr_softc), vr_probe, vr_attach
1410 1.2 sakamoto };
1411 1.2 sakamoto
1412 1.3 sakamoto static struct vr_type *
1413 1.3 sakamoto vr_lookup(pa)
1414 1.3 sakamoto struct pci_attach_args *pa;
1415 1.3 sakamoto {
1416 1.3 sakamoto struct vr_type *vrt;
1417 1.3 sakamoto
1418 1.3 sakamoto for (vrt = vr_devs; vrt->vr_name != NULL; vrt++) {
1419 1.3 sakamoto if (PCI_VENDOR(pa->pa_id) == vrt->vr_vid &&
1420 1.3 sakamoto PCI_PRODUCT(pa->pa_id) == vrt->vr_did)
1421 1.3 sakamoto return (vrt);
1422 1.3 sakamoto }
1423 1.3 sakamoto return (NULL);
1424 1.3 sakamoto }
1425 1.3 sakamoto
1426 1.2 sakamoto static int
1427 1.2 sakamoto vr_probe(parent, match, aux)
1428 1.2 sakamoto struct device *parent;
1429 1.2 sakamoto struct cfdata *match;
1430 1.2 sakamoto void *aux;
1431 1.2 sakamoto {
1432 1.2 sakamoto struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1433 1.2 sakamoto
1434 1.3 sakamoto if (vr_lookup(pa) != NULL)
1435 1.3 sakamoto return (1);
1436 1.2 sakamoto
1437 1.2 sakamoto return (0);
1438 1.2 sakamoto }
1439 1.2 sakamoto
1440 1.2 sakamoto /*
1441 1.2 sakamoto * Stop all chip I/O so that the kernel's probe routines don't
1442 1.2 sakamoto * get confused by errant DMAs when rebooting.
1443 1.2 sakamoto */
1444 1.15 thorpej static void
1445 1.15 thorpej vr_shutdown(arg)
1446 1.2 sakamoto void *arg;
1447 1.2 sakamoto {
1448 1.15 thorpej struct vr_softc *sc = (struct vr_softc *)arg;
1449 1.2 sakamoto
1450 1.2 sakamoto vr_stop(sc);
1451 1.2 sakamoto }
1452 1.2 sakamoto
1453 1.2 sakamoto /*
1454 1.2 sakamoto * Attach the interface. Allocate softc structures, do ifmedia
1455 1.2 sakamoto * setup and ethernet/BPF attach.
1456 1.2 sakamoto */
1457 1.2 sakamoto static void
1458 1.2 sakamoto vr_attach(parent, self, aux)
1459 1.15 thorpej struct device *parent;
1460 1.15 thorpej struct device *self;
1461 1.15 thorpej void *aux;
1462 1.2 sakamoto {
1463 1.15 thorpej struct vr_softc *sc = (struct vr_softc *) self;
1464 1.15 thorpej struct pci_attach_args *pa = (struct pci_attach_args *) aux;
1465 1.15 thorpej struct vr_type *vrt;
1466 1.15 thorpej int i;
1467 1.15 thorpej u_int32_t command;
1468 1.15 thorpej struct ifnet *ifp;
1469 1.15 thorpej unsigned int round;
1470 1.15 thorpej caddr_t roundptr;
1471 1.15 thorpej u_char eaddr[ETHER_ADDR_LEN];
1472 1.15 thorpej
1473 1.2 sakamoto #define PCI_CONF_WRITE(r, v) pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
1474 1.2 sakamoto #define PCI_CONF_READ(r) pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
1475 1.2 sakamoto
1476 1.3 sakamoto vrt = vr_lookup(pa);
1477 1.3 sakamoto if (vrt == NULL) {
1478 1.3 sakamoto printf("\n");
1479 1.3 sakamoto panic("vr_attach: impossible");
1480 1.3 sakamoto }
1481 1.3 sakamoto
1482 1.3 sakamoto printf(": %s Ethernet\n", vrt->vr_name);
1483 1.2 sakamoto
1484 1.2 sakamoto /*
1485 1.2 sakamoto * Handle power management nonsense.
1486 1.2 sakamoto */
1487 1.2 sakamoto
1488 1.2 sakamoto command = PCI_CONF_READ(VR_PCI_CAPID) & 0x000000FF;
1489 1.2 sakamoto if (command == 0x01) {
1490 1.2 sakamoto command = PCI_CONF_READ(VR_PCI_PWRMGMTCTRL);
1491 1.2 sakamoto if (command & VR_PSTATE_MASK) {
1492 1.15 thorpej u_int32_t iobase, membase, irq;
1493 1.2 sakamoto
1494 1.2 sakamoto /* Save important PCI config data. */
1495 1.2 sakamoto iobase = PCI_CONF_READ(VR_PCI_LOIO);
1496 1.2 sakamoto membase = PCI_CONF_READ(VR_PCI_LOMEM);
1497 1.2 sakamoto irq = PCI_CONF_READ(VR_PCI_INTLINE);
1498 1.2 sakamoto
1499 1.2 sakamoto /* Reset the power state. */
1500 1.6 thorpej printf("%s: chip is in D%d power mode "
1501 1.2 sakamoto "-- setting to D0\n",
1502 1.6 thorpej sc->vr_dev.dv_xname, command & VR_PSTATE_MASK);
1503 1.2 sakamoto command &= 0xFFFFFFFC;
1504 1.2 sakamoto PCI_CONF_WRITE(VR_PCI_PWRMGMTCTRL, command);
1505 1.2 sakamoto
1506 1.2 sakamoto /* Restore PCI config data. */
1507 1.2 sakamoto PCI_CONF_WRITE(VR_PCI_LOIO, iobase);
1508 1.2 sakamoto PCI_CONF_WRITE(VR_PCI_LOMEM, membase);
1509 1.2 sakamoto PCI_CONF_WRITE(VR_PCI_INTLINE, irq);
1510 1.2 sakamoto }
1511 1.2 sakamoto }
1512 1.2 sakamoto
1513 1.2 sakamoto /*
1514 1.2 sakamoto * Map control/status registers.
1515 1.2 sakamoto */
1516 1.2 sakamoto command = PCI_CONF_READ(PCI_COMMAND_STATUS_REG);
1517 1.2 sakamoto command |= (PCI_COMMAND_IO_ENABLE |
1518 1.2 sakamoto PCI_COMMAND_MEM_ENABLE |
1519 1.2 sakamoto PCI_COMMAND_MASTER_ENABLE);
1520 1.2 sakamoto PCI_CONF_WRITE(PCI_COMMAND_STATUS_REG, command);
1521 1.2 sakamoto command = PCI_CONF_READ(PCI_COMMAND_STATUS_REG);
1522 1.2 sakamoto
1523 1.2 sakamoto {
1524 1.2 sakamoto bus_space_tag_t iot, memt;
1525 1.2 sakamoto bus_space_handle_t ioh, memh;
1526 1.2 sakamoto int ioh_valid, memh_valid;
1527 1.2 sakamoto pci_intr_handle_t intrhandle;
1528 1.2 sakamoto const char *intrstr;
1529 1.2 sakamoto
1530 1.2 sakamoto ioh_valid = (pci_mapreg_map(pa, VR_PCI_LOIO,
1531 1.2 sakamoto PCI_MAPREG_TYPE_IO, 0,
1532 1.2 sakamoto &iot, &ioh, NULL, NULL) == 0);
1533 1.2 sakamoto memh_valid = (pci_mapreg_map(pa, VR_PCI_LOMEM,
1534 1.2 sakamoto PCI_MAPREG_TYPE_MEM |
1535 1.2 sakamoto PCI_MAPREG_MEM_TYPE_32BIT,
1536 1.2 sakamoto 0, &memt, &memh, NULL, NULL) == 0);
1537 1.2 sakamoto #if defined(VR_USEIOSPACE)
1538 1.2 sakamoto if (ioh_valid) {
1539 1.14 thorpej sc->vr_bst = iot;
1540 1.14 thorpej sc->vr_bsh = ioh;
1541 1.2 sakamoto } else if (memh_valid) {
1542 1.14 thorpej sc->vr_bst = memt;
1543 1.14 thorpej sc->vr_bsh = memh;
1544 1.2 sakamoto }
1545 1.2 sakamoto #else
1546 1.2 sakamoto if (memh_valid) {
1547 1.14 thorpej sc->vr_bst = memt;
1548 1.14 thorpej sc->vr_bsh = memh;
1549 1.2 sakamoto } else if (ioh_valid) {
1550 1.14 thorpej sc->vr_bst = iot;
1551 1.14 thorpej sc->vr_bsh = ioh;
1552 1.2 sakamoto }
1553 1.2 sakamoto #endif
1554 1.2 sakamoto else {
1555 1.2 sakamoto printf(": unable to map device registers\n");
1556 1.2 sakamoto return;
1557 1.2 sakamoto }
1558 1.2 sakamoto
1559 1.2 sakamoto /* Allocate interrupt */
1560 1.2 sakamoto if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
1561 1.2 sakamoto pa->pa_intrline, &intrhandle)) {
1562 1.6 thorpej printf("%s: couldn't map interrupt\n",
1563 1.6 thorpej sc->vr_dev.dv_xname);
1564 1.15 thorpej return;
1565 1.2 sakamoto }
1566 1.2 sakamoto intrstr = pci_intr_string(pa->pa_pc, intrhandle);
1567 1.2 sakamoto sc->vr_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
1568 1.16 thorpej vr_intr, sc);
1569 1.2 sakamoto if (sc->vr_ih == NULL) {
1570 1.6 thorpej printf("%s: couldn't establish interrupt",
1571 1.6 thorpej sc->vr_dev.dv_xname);
1572 1.2 sakamoto if (intrstr != NULL)
1573 1.2 sakamoto printf(" at %s", intrstr);
1574 1.2 sakamoto printf("\n");
1575 1.2 sakamoto }
1576 1.6 thorpej printf("%s: interrupting at %s\n",
1577 1.6 thorpej sc->vr_dev.dv_xname, intrstr);
1578 1.2 sakamoto }
1579 1.2 sakamoto sc->vr_ats = shutdownhook_establish(vr_shutdown, sc);
1580 1.2 sakamoto if (sc->vr_ats == NULL)
1581 1.6 thorpej printf("%s: warning: couldn't establish shutdown hook\n",
1582 1.6 thorpej sc->vr_dev.dv_xname);
1583 1.2 sakamoto
1584 1.2 sakamoto /* Reset the adapter. */
1585 1.2 sakamoto vr_reset(sc);
1586 1.2 sakamoto
1587 1.2 sakamoto /*
1588 1.2 sakamoto * Get station address. The way the Rhine chips work,
1589 1.2 sakamoto * you're not allowed to directly access the EEPROM once
1590 1.2 sakamoto * they've been programmed a special way. Consequently,
1591 1.2 sakamoto * we need to read the node address from the PAR0 and PAR1
1592 1.2 sakamoto * registers.
1593 1.2 sakamoto */
1594 1.2 sakamoto VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
1595 1.2 sakamoto DELAY(200);
1596 1.2 sakamoto for (i = 0; i < ETHER_ADDR_LEN; i++)
1597 1.2 sakamoto eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
1598 1.2 sakamoto
1599 1.2 sakamoto /*
1600 1.2 sakamoto * A Rhine chip was detected. Inform the world.
1601 1.2 sakamoto */
1602 1.6 thorpej printf("%s: Ethernet address: %s\n",
1603 1.6 thorpej sc->vr_dev.dv_xname, ether_sprintf(eaddr));
1604 1.2 sakamoto
1605 1.2 sakamoto bcopy(eaddr, sc->vr_enaddr, ETHER_ADDR_LEN);
1606 1.2 sakamoto
1607 1.2 sakamoto sc->vr_ldata_ptr = malloc(sizeof (struct vr_list_data) + 8,
1608 1.2 sakamoto M_DEVBUF, M_NOWAIT);
1609 1.2 sakamoto if (sc->vr_ldata_ptr == NULL) {
1610 1.2 sakamoto free(sc, M_DEVBUF);
1611 1.6 thorpej printf("%s: no memory for list buffers!\n",
1612 1.6 thorpej sc->vr_dev.dv_xname);
1613 1.2 sakamoto return;
1614 1.2 sakamoto }
1615 1.2 sakamoto
1616 1.2 sakamoto sc->vr_ldata = (struct vr_list_data *)sc->vr_ldata_ptr;
1617 1.5 thorpej round = (unsigned long)sc->vr_ldata_ptr & 0xF;
1618 1.2 sakamoto roundptr = sc->vr_ldata_ptr;
1619 1.2 sakamoto for (i = 0; i < 8; i++) {
1620 1.2 sakamoto if (round % 8) {
1621 1.2 sakamoto round++;
1622 1.2 sakamoto roundptr++;
1623 1.2 sakamoto } else
1624 1.2 sakamoto break;
1625 1.2 sakamoto }
1626 1.2 sakamoto sc->vr_ldata = (struct vr_list_data *)roundptr;
1627 1.2 sakamoto bzero(sc->vr_ldata, sizeof (struct vr_list_data));
1628 1.2 sakamoto
1629 1.6 thorpej ifp = &sc->vr_ec.ec_if;
1630 1.2 sakamoto ifp->if_softc = sc;
1631 1.2 sakamoto ifp->if_mtu = ETHERMTU;
1632 1.2 sakamoto ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1633 1.2 sakamoto ifp->if_ioctl = vr_ioctl;
1634 1.2 sakamoto ifp->if_output = ether_output;
1635 1.2 sakamoto ifp->if_start = vr_start;
1636 1.2 sakamoto ifp->if_watchdog = vr_watchdog;
1637 1.2 sakamoto ifp->if_baudrate = 10000000;
1638 1.2 sakamoto bcopy(sc->vr_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
1639 1.2 sakamoto
1640 1.2 sakamoto /*
1641 1.11 thorpej * Initialize MII/media info.
1642 1.2 sakamoto */
1643 1.11 thorpej sc->vr_mii.mii_ifp = ifp;
1644 1.11 thorpej sc->vr_mii.mii_readreg = vr_mii_readreg;
1645 1.11 thorpej sc->vr_mii.mii_writereg = vr_mii_writereg;
1646 1.11 thorpej sc->vr_mii.mii_statchg = vr_mii_statchg;
1647 1.11 thorpej ifmedia_init(&sc->vr_mii.mii_media, 0, vr_ifmedia_upd, vr_ifmedia_sts);
1648 1.11 thorpej mii_phy_probe(&sc->vr_dev, &sc->vr_mii, 0xffffffff);
1649 1.11 thorpej if (LIST_FIRST(&sc->vr_mii.mii_phys) == NULL) {
1650 1.11 thorpej ifmedia_add(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
1651 1.11 thorpej ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE);
1652 1.11 thorpej } else
1653 1.11 thorpej ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_AUTO);
1654 1.2 sakamoto
1655 1.2 sakamoto /*
1656 1.2 sakamoto * Call MI attach routines.
1657 1.2 sakamoto */
1658 1.2 sakamoto if_attach(ifp);
1659 1.2 sakamoto ether_ifattach(ifp, sc->vr_enaddr);
1660 1.2 sakamoto
1661 1.2 sakamoto #if NBPFILTER > 0
1662 1.6 thorpej bpfattach(&sc->vr_ec.ec_if.if_bpf,
1663 1.2 sakamoto ifp, DLT_EN10MB, sizeof (struct ether_header));
1664 1.2 sakamoto #endif
1665 1.2 sakamoto
1666 1.2 sakamoto sc->vr_ats = shutdownhook_establish(vr_shutdown, sc);
1667 1.2 sakamoto if (sc->vr_ats == NULL)
1668 1.2 sakamoto printf("%s: warning: couldn't establish shutdown hook\n",
1669 1.2 sakamoto sc->vr_dev.dv_xname);
1670 1.2 sakamoto }
1671