if_vge.c revision 1.29 1 /* $NetBSD: if_vge.c,v 1.29 2006/11/26 15:04:15 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2004
5 * Bill Paul <wpaul (at) windriver.com>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * FreeBSD: src/sys/dev/vge/if_vge.c,v 1.5 2005/02/07 19:39:29 glebius Exp
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: if_vge.c,v 1.29 2006/11/26 15:04:15 tsutsui Exp $");
39
40 /*
41 * VIA Networking Technologies VT612x PCI gigabit ethernet NIC driver.
42 *
43 * Written by Bill Paul <wpaul (at) windriver.com>
44 * Senior Networking Software Engineer
45 * Wind River Systems
46 */
47
48 /*
49 * The VIA Networking VT6122 is a 32bit, 33/66 MHz PCI device that
50 * combines a tri-speed ethernet MAC and PHY, with the following
51 * features:
52 *
53 * o Jumbo frame support up to 16K
54 * o Transmit and receive flow control
55 * o IPv4 checksum offload
56 * o VLAN tag insertion and stripping
57 * o TCP large send
58 * o 64-bit multicast hash table filter
59 * o 64 entry CAM filter
60 * o 16K RX FIFO and 48K TX FIFO memory
61 * o Interrupt moderation
62 *
63 * The VT6122 supports up to four transmit DMA queues. The descriptors
64 * in the transmit ring can address up to 7 data fragments; frames which
65 * span more than 7 data buffers must be coalesced, but in general the
66 * BSD TCP/IP stack rarely generates frames more than 2 or 3 fragments
67 * long. The receive descriptors address only a single buffer.
68 *
69 * There are two peculiar design issues with the VT6122. One is that
70 * receive data buffers must be aligned on a 32-bit boundary. This is
71 * not a problem where the VT6122 is used as a LOM device in x86-based
72 * systems, but on architectures that generate unaligned access traps, we
73 * have to do some copying.
74 *
75 * The other issue has to do with the way 64-bit addresses are handled.
76 * The DMA descriptors only allow you to specify 48 bits of addressing
77 * information. The remaining 16 bits are specified using one of the
78 * I/O registers. If you only have a 32-bit system, then this isn't
79 * an issue, but if you have a 64-bit system and more than 4GB of
80 * memory, you must have to make sure your network data buffers reside
81 * in the same 48-bit 'segment.'
82 *
83 * Special thanks to Ryan Fu at VIA Networking for providing documentation
84 * and sample NICs for testing.
85 */
86
87 #include "bpfilter.h"
88
89 #include <sys/param.h>
90 #include <sys/endian.h>
91 #include <sys/systm.h>
92 #include <sys/sockio.h>
93 #include <sys/mbuf.h>
94 #include <sys/malloc.h>
95 #include <sys/kernel.h>
96 #include <sys/socket.h>
97
98 #include <net/if.h>
99 #include <net/if_arp.h>
100 #include <net/if_ether.h>
101 #include <net/if_dl.h>
102 #include <net/if_media.h>
103
104 #include <net/bpf.h>
105
106 #include <machine/bus.h>
107
108 #include <dev/mii/mii.h>
109 #include <dev/mii/miivar.h>
110
111 #include <dev/pci/pcireg.h>
112 #include <dev/pci/pcivar.h>
113 #include <dev/pci/pcidevs.h>
114
115 #include <dev/pci/if_vgereg.h>
116
117 #define VGE_JUMBO_MTU 9000
118
119 #define VGE_IFQ_MAXLEN 64
120
121 #define VGE_RING_ALIGN 256
122
123 #define VGE_NTXDESC 256
124 #define VGE_NTXDESC_MASK (VGE_NTXDESC - 1)
125 #define VGE_NEXT_TXDESC(x) ((x + 1) & VGE_NTXDESC_MASK)
126 #define VGE_PREV_TXDESC(x) ((x - 1) & VGE_NTXDESC_MASK)
127
128 #define VGE_NRXDESC 256 /* Must be a multiple of 4!! */
129 #define VGE_NRXDESC_MASK (VGE_NRXDESC - 1)
130 #define VGE_NEXT_RXDESC(x) ((x + 1) & VGE_NRXDESC_MASK)
131 #define VGE_PREV_RXDESC(x) ((x - 1) & VGE_NRXDESC_MASK)
132
133 #define VGE_ADDR_LO(y) ((uint64_t)(y) & 0xFFFFFFFF)
134 #define VGE_ADDR_HI(y) ((uint64_t)(y) >> 32)
135 #define VGE_BUFLEN(y) ((y) & 0x7FFF)
136 #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
137
138 #define VGE_POWER_MANAGEMENT 0 /* disabled for now */
139
140 /*
141 * Mbuf adjust factor to force 32-bit alignment of IP header.
142 * Drivers should pad ETHER_ALIGN bytes when setting up a
143 * RX mbuf so the upper layers get the IP header properly aligned
144 * past the 14-byte Ethernet header.
145 *
146 * See also comment in vge_encap().
147 */
148 #define ETHER_ALIGN 2
149
150 #ifdef __NO_STRICT_ALIGNMENT
151 #define VGE_RX_BUFSIZE MCLBYTES
152 #else
153 #define VGE_RX_PAD sizeof(uint32_t)
154 #define VGE_RX_BUFSIZE (MCLBYTES - VGE_RX_PAD)
155 #endif
156
157 /*
158 * Control structures are DMA'd to the vge chip. We allocate them in
159 * a single clump that maps to a single DMA segment to make several things
160 * easier.
161 */
162 struct vge_control_data {
163 /* TX descriptors */
164 struct vge_txdesc vcd_txdescs[VGE_NTXDESC];
165 /* RX descriptors */
166 struct vge_rxdesc vcd_rxdescs[VGE_NRXDESC];
167 /* dummy data for TX padding */
168 uint8_t vcd_pad[ETHER_PAD_LEN];
169 };
170
171 #define VGE_CDOFF(x) offsetof(struct vge_control_data, x)
172 #define VGE_CDTXOFF(x) VGE_CDOFF(vcd_txdescs[(x)])
173 #define VGE_CDRXOFF(x) VGE_CDOFF(vcd_rxdescs[(x)])
174 #define VGE_CDPADOFF() VGE_CDOFF(vcd_pad[0])
175
176 /*
177 * Software state for TX jobs.
178 */
179 struct vge_txsoft {
180 struct mbuf *txs_mbuf; /* head of our mbuf chain */
181 bus_dmamap_t txs_dmamap; /* our DMA map */
182 };
183
184 /*
185 * Software state for RX jobs.
186 */
187 struct vge_rxsoft {
188 struct mbuf *rxs_mbuf; /* head of our mbuf chain */
189 bus_dmamap_t rxs_dmamap; /* our DMA map */
190 };
191
192
193 struct vge_softc {
194 struct device sc_dev;
195
196 bus_space_tag_t sc_bst; /* bus space tag */
197 bus_space_handle_t sc_bsh; /* bus space handle */
198 bus_dma_tag_t sc_dmat;
199
200 struct ethercom sc_ethercom; /* interface info */
201 uint8_t sc_eaddr[ETHER_ADDR_LEN];
202
203 void *sc_intrhand;
204 struct mii_data sc_mii;
205 uint8_t sc_type;
206 int sc_if_flags;
207 int sc_link;
208 int sc_camidx;
209 struct callout sc_timeout;
210
211 bus_dmamap_t sc_cddmamap;
212 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
213
214 struct vge_txsoft sc_txsoft[VGE_NTXDESC];
215 struct vge_rxsoft sc_rxsoft[VGE_NRXDESC];
216 struct vge_control_data *sc_control_data;
217 #define sc_txdescs sc_control_data->vcd_txdescs
218 #define sc_rxdescs sc_control_data->vcd_rxdescs
219
220 int sc_tx_prodidx;
221 int sc_tx_considx;
222 int sc_tx_free;
223
224 struct mbuf *sc_rx_mhead;
225 struct mbuf *sc_rx_mtail;
226 int sc_rx_prodidx;
227 int sc_rx_consumed;
228
229 int sc_suspended; /* 0 = normal 1 = suspended */
230 uint32_t sc_saved_maps[5]; /* pci data */
231 uint32_t sc_saved_biosaddr;
232 uint8_t sc_saved_intline;
233 uint8_t sc_saved_cachelnsz;
234 uint8_t sc_saved_lattimer;
235 };
236
237 #define VGE_CDTXADDR(sc, x) ((sc)->sc_cddma + VGE_CDTXOFF(x))
238 #define VGE_CDRXADDR(sc, x) ((sc)->sc_cddma + VGE_CDRXOFF(x))
239 #define VGE_CDPADADDR(sc) ((sc)->sc_cddma + VGE_CDPADOFF())
240
241 #define VGE_TXDESCSYNC(sc, idx, ops) \
242 bus_dmamap_sync((sc)->sc_dmat,(sc)->sc_cddmamap, \
243 VGE_CDTXOFF(idx), \
244 offsetof(struct vge_txdesc, td_frag[0]), \
245 (ops))
246 #define VGE_TXFRAGSYNC(sc, idx, nsegs, ops) \
247 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
248 VGE_CDTXOFF(idx) + \
249 offsetof(struct vge_txdesc, td_frag[0]), \
250 sizeof(struct vge_txfrag) * (nsegs), \
251 (ops))
252 #define VGE_RXDESCSYNC(sc, idx, ops) \
253 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
254 VGE_CDRXOFF(idx), \
255 sizeof(struct vge_rxdesc), \
256 (ops))
257
258 /*
259 * register space access macros
260 */
261 #define CSR_WRITE_4(sc, reg, val) \
262 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
263 #define CSR_WRITE_2(sc, reg, val) \
264 bus_space_write_2((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
265 #define CSR_WRITE_1(sc, reg, val) \
266 bus_space_write_1((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
267
268 #define CSR_READ_4(sc, reg) \
269 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
270 #define CSR_READ_2(sc, reg) \
271 bus_space_read_2((sc)->sc_bst, (sc)->sc_bsh, (reg))
272 #define CSR_READ_1(sc, reg) \
273 bus_space_read_1((sc)->sc_bst, (sc)->sc_bsh, (reg))
274
275 #define CSR_SETBIT_1(sc, reg, x) \
276 CSR_WRITE_1((sc), (reg), CSR_READ_1((sc), (reg)) | (x))
277 #define CSR_SETBIT_2(sc, reg, x) \
278 CSR_WRITE_2((sc), (reg), CSR_READ_2((sc), (reg)) | (x))
279 #define CSR_SETBIT_4(sc, reg, x) \
280 CSR_WRITE_4((sc), (reg), CSR_READ_4((sc), (reg)) | (x))
281
282 #define CSR_CLRBIT_1(sc, reg, x) \
283 CSR_WRITE_1((sc), (reg), CSR_READ_1((sc), (reg)) & ~(x))
284 #define CSR_CLRBIT_2(sc, reg, x) \
285 CSR_WRITE_2((sc), (reg), CSR_READ_2((sc), (reg)) & ~(x))
286 #define CSR_CLRBIT_4(sc, reg, x) \
287 CSR_WRITE_4((sc), (reg), CSR_READ_4((sc), (reg)) & ~(x))
288
289 #define VGE_TIMEOUT 10000
290
291 #define VGE_PCI_LOIO 0x10
292 #define VGE_PCI_LOMEM 0x14
293
294 static inline void vge_set_txaddr(struct vge_txfrag *, bus_addr_t);
295 static inline void vge_set_rxaddr(struct vge_rxdesc *, bus_addr_t);
296
297 static int vge_probe(struct device *, struct cfdata *, void *);
298 static void vge_attach(struct device *, struct device *, void *);
299
300 static int vge_encap(struct vge_softc *, struct mbuf *, int);
301
302 static int vge_allocmem(struct vge_softc *);
303 static int vge_newbuf(struct vge_softc *, int, struct mbuf *);
304 #ifndef __NO_STRICT_ALIGNMENT
305 static inline void vge_fixup_rx(struct mbuf *);
306 #endif
307 static void vge_rxeof(struct vge_softc *);
308 static void vge_txeof(struct vge_softc *);
309 static int vge_intr(void *);
310 static void vge_tick(void *);
311 static void vge_start(struct ifnet *);
312 static int vge_ioctl(struct ifnet *, u_long, caddr_t);
313 static int vge_init(struct ifnet *);
314 static void vge_stop(struct vge_softc *);
315 static void vge_watchdog(struct ifnet *);
316 #if VGE_POWER_MANAGEMENT
317 static int vge_suspend(struct device *);
318 static int vge_resume(struct device *);
319 #endif
320 static void vge_shutdown(void *);
321 static int vge_ifmedia_upd(struct ifnet *);
322 static void vge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
323
324 static uint16_t vge_read_eeprom(struct vge_softc *, int);
325
326 static void vge_miipoll_start(struct vge_softc *);
327 static void vge_miipoll_stop(struct vge_softc *);
328 static int vge_miibus_readreg(struct device *, int, int);
329 static void vge_miibus_writereg(struct device *, int, int, int);
330 static void vge_miibus_statchg(struct device *);
331
332 static void vge_cam_clear(struct vge_softc *);
333 static int vge_cam_set(struct vge_softc *, uint8_t *);
334 static void vge_setmulti(struct vge_softc *);
335 static void vge_reset(struct vge_softc *);
336
337 CFATTACH_DECL(vge, sizeof(struct vge_softc),
338 vge_probe, vge_attach, NULL, NULL);
339
340 static inline void
341 vge_set_txaddr(struct vge_txfrag *f, bus_addr_t daddr)
342 {
343
344 f->tf_addrlo = htole32((uint32_t)daddr);
345 if (sizeof(bus_addr_t) == sizeof(uint64_t))
346 f->tf_addrhi = htole16(((uint64_t)daddr >> 32) & 0xFFFF);
347 else
348 f->tf_addrhi = 0;
349 }
350
351 static inline void
352 vge_set_rxaddr(struct vge_rxdesc *rxd, bus_addr_t daddr)
353 {
354
355 rxd->rd_addrlo = htole32((uint32_t)daddr);
356 if (sizeof(bus_addr_t) == sizeof(uint64_t))
357 rxd->rd_addrhi = htole16(((uint64_t)daddr >> 32) & 0xFFFF);
358 else
359 rxd->rd_addrhi = 0;
360 }
361
362 /*
363 * Defragment mbuf chain contents to be as linear as possible.
364 * Returns new mbuf chain on success, NULL on failure. Old mbuf
365 * chain is always freed.
366 * XXX temporary until there would be generic function doing this.
367 */
368 #define m_defrag vge_m_defrag
369 struct mbuf * vge_m_defrag(struct mbuf *, int);
370
371 struct mbuf *
372 vge_m_defrag(struct mbuf *mold, int flags)
373 {
374 struct mbuf *m0, *mn, *n;
375 size_t sz = mold->m_pkthdr.len;
376
377 #ifdef DIAGNOSTIC
378 if ((mold->m_flags & M_PKTHDR) == 0)
379 panic("m_defrag: not a mbuf chain header");
380 #endif
381
382 MGETHDR(m0, flags, MT_DATA);
383 if (m0 == NULL)
384 return NULL;
385 m0->m_pkthdr.len = mold->m_pkthdr.len;
386 mn = m0;
387
388 do {
389 if (sz > MHLEN) {
390 MCLGET(mn, M_DONTWAIT);
391 if ((mn->m_flags & M_EXT) == 0) {
392 m_freem(m0);
393 return NULL;
394 }
395 }
396
397 mn->m_len = MIN(sz, MCLBYTES);
398
399 m_copydata(mold, mold->m_pkthdr.len - sz, mn->m_len,
400 mtod(mn, caddr_t));
401
402 sz -= mn->m_len;
403
404 if (sz > 0) {
405 /* need more mbufs */
406 MGET(n, M_NOWAIT, MT_DATA);
407 if (n == NULL) {
408 m_freem(m0);
409 return NULL;
410 }
411
412 mn->m_next = n;
413 mn = n;
414 }
415 } while (sz > 0);
416
417 return m0;
418 }
419
420 /*
421 * Read a word of data stored in the EEPROM at address 'addr.'
422 */
423 static uint16_t
424 vge_read_eeprom(struct vge_softc *sc, int addr)
425 {
426 int i;
427 uint16_t word = 0;
428
429 /*
430 * Enter EEPROM embedded programming mode. In order to
431 * access the EEPROM at all, we first have to set the
432 * EELOAD bit in the CHIPCFG2 register.
433 */
434 CSR_SETBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
435 CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
436
437 /* Select the address of the word we want to read */
438 CSR_WRITE_1(sc, VGE_EEADDR, addr);
439
440 /* Issue read command */
441 CSR_SETBIT_1(sc, VGE_EECMD, VGE_EECMD_ERD);
442
443 /* Wait for the done bit to be set. */
444 for (i = 0; i < VGE_TIMEOUT; i++) {
445 if (CSR_READ_1(sc, VGE_EECMD) & VGE_EECMD_EDONE)
446 break;
447 }
448
449 if (i == VGE_TIMEOUT) {
450 aprint_error("%s: EEPROM read timed out\n",
451 sc->sc_dev.dv_xname);
452 return 0;
453 }
454
455 /* Read the result */
456 word = CSR_READ_2(sc, VGE_EERDDAT);
457
458 /* Turn off EEPROM access mode. */
459 CSR_CLRBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
460 CSR_CLRBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
461
462 return word;
463 }
464
465 static void
466 vge_miipoll_stop(struct vge_softc *sc)
467 {
468 int i;
469
470 CSR_WRITE_1(sc, VGE_MIICMD, 0);
471
472 for (i = 0; i < VGE_TIMEOUT; i++) {
473 DELAY(1);
474 if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
475 break;
476 }
477
478 if (i == VGE_TIMEOUT) {
479 aprint_error("%s: failed to idle MII autopoll\n",
480 sc->sc_dev.dv_xname);
481 }
482 }
483
484 static void
485 vge_miipoll_start(struct vge_softc *sc)
486 {
487 int i;
488
489 /* First, make sure we're idle. */
490
491 CSR_WRITE_1(sc, VGE_MIICMD, 0);
492 CSR_WRITE_1(sc, VGE_MIIADDR, VGE_MIIADDR_SWMPL);
493
494 for (i = 0; i < VGE_TIMEOUT; i++) {
495 DELAY(1);
496 if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
497 break;
498 }
499
500 if (i == VGE_TIMEOUT) {
501 aprint_error("%s: failed to idle MII autopoll\n",
502 sc->sc_dev.dv_xname);
503 return;
504 }
505
506 /* Now enable auto poll mode. */
507
508 CSR_WRITE_1(sc, VGE_MIICMD, VGE_MIICMD_MAUTO);
509
510 /* And make sure it started. */
511
512 for (i = 0; i < VGE_TIMEOUT; i++) {
513 DELAY(1);
514 if ((CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) == 0)
515 break;
516 }
517
518 if (i == VGE_TIMEOUT) {
519 aprint_error("%s: failed to start MII autopoll\n",
520 sc->sc_dev.dv_xname);
521 }
522 }
523
524 static int
525 vge_miibus_readreg(struct device *dev, int phy, int reg)
526 {
527 struct vge_softc *sc;
528 int i, s;
529 uint16_t rval;
530
531 sc = (void *)dev;
532 rval = 0;
533 if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
534 return 0;
535
536 s = splnet();
537 vge_miipoll_stop(sc);
538
539 /* Specify the register we want to read. */
540 CSR_WRITE_1(sc, VGE_MIIADDR, reg);
541
542 /* Issue read command. */
543 CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_RCMD);
544
545 /* Wait for the read command bit to self-clear. */
546 for (i = 0; i < VGE_TIMEOUT; i++) {
547 DELAY(1);
548 if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_RCMD) == 0)
549 break;
550 }
551
552 if (i == VGE_TIMEOUT)
553 aprint_error("%s: MII read timed out\n", sc->sc_dev.dv_xname);
554 else
555 rval = CSR_READ_2(sc, VGE_MIIDATA);
556
557 vge_miipoll_start(sc);
558 splx(s);
559
560 return rval;
561 }
562
563 static void
564 vge_miibus_writereg(struct device *dev, int phy, int reg, int data)
565 {
566 struct vge_softc *sc;
567 int i, s;
568
569 sc = (void *)dev;
570 if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
571 return;
572
573 s = splnet();
574 vge_miipoll_stop(sc);
575
576 /* Specify the register we want to write. */
577 CSR_WRITE_1(sc, VGE_MIIADDR, reg);
578
579 /* Specify the data we want to write. */
580 CSR_WRITE_2(sc, VGE_MIIDATA, data);
581
582 /* Issue write command. */
583 CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_WCMD);
584
585 /* Wait for the write command bit to self-clear. */
586 for (i = 0; i < VGE_TIMEOUT; i++) {
587 DELAY(1);
588 if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_WCMD) == 0)
589 break;
590 }
591
592 if (i == VGE_TIMEOUT) {
593 aprint_error("%s: MII write timed out\n", sc->sc_dev.dv_xname);
594 }
595
596 vge_miipoll_start(sc);
597 splx(s);
598 }
599
600 static void
601 vge_cam_clear(struct vge_softc *sc)
602 {
603 int i;
604
605 /*
606 * Turn off all the mask bits. This tells the chip
607 * that none of the entries in the CAM filter are valid.
608 * desired entries will be enabled as we fill the filter in.
609 */
610
611 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
612 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
613 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE);
614 for (i = 0; i < 8; i++)
615 CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
616
617 /* Clear the VLAN filter too. */
618
619 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|VGE_CAMADDR_AVSEL|0);
620 for (i = 0; i < 8; i++)
621 CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
622
623 CSR_WRITE_1(sc, VGE_CAMADDR, 0);
624 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
625 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
626
627 sc->sc_camidx = 0;
628 }
629
630 static int
631 vge_cam_set(struct vge_softc *sc, uint8_t *addr)
632 {
633 int i, error;
634
635 error = 0;
636
637 if (sc->sc_camidx == VGE_CAM_MAXADDRS)
638 return ENOSPC;
639
640 /* Select the CAM data page. */
641 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
642 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMDATA);
643
644 /* Set the filter entry we want to update and enable writing. */
645 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE | sc->sc_camidx);
646
647 /* Write the address to the CAM registers */
648 for (i = 0; i < ETHER_ADDR_LEN; i++)
649 CSR_WRITE_1(sc, VGE_CAM0 + i, addr[i]);
650
651 /* Issue a write command. */
652 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_WRITE);
653
654 /* Wake for it to clear. */
655 for (i = 0; i < VGE_TIMEOUT; i++) {
656 DELAY(1);
657 if ((CSR_READ_1(sc, VGE_CAMCTL) & VGE_CAMCTL_WRITE) == 0)
658 break;
659 }
660
661 if (i == VGE_TIMEOUT) {
662 aprint_error("%s: setting CAM filter failed\n",
663 sc->sc_dev.dv_xname);
664 error = EIO;
665 goto fail;
666 }
667
668 /* Select the CAM mask page. */
669 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
670 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
671
672 /* Set the mask bit that enables this filter. */
673 CSR_SETBIT_1(sc, VGE_CAM0 + (sc->sc_camidx / 8),
674 1 << (sc->sc_camidx & 7));
675
676 sc->sc_camidx++;
677
678 fail:
679 /* Turn off access to CAM. */
680 CSR_WRITE_1(sc, VGE_CAMADDR, 0);
681 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
682 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
683
684 return error;
685 }
686
687 /*
688 * Program the multicast filter. We use the 64-entry CAM filter
689 * for perfect filtering. If there's more than 64 multicast addresses,
690 * we use the hash filter instead.
691 */
692 static void
693 vge_setmulti(struct vge_softc *sc)
694 {
695 struct ifnet *ifp;
696 int error;
697 uint32_t h, hashes[2] = { 0, 0 };
698 struct ether_multi *enm;
699 struct ether_multistep step;
700
701 error = 0;
702 ifp = &sc->sc_ethercom.ec_if;
703
704 /* First, zot all the multicast entries. */
705 vge_cam_clear(sc);
706 CSR_WRITE_4(sc, VGE_MAR0, 0);
707 CSR_WRITE_4(sc, VGE_MAR1, 0);
708 ifp->if_flags &= ~IFF_ALLMULTI;
709
710 /*
711 * If the user wants allmulti or promisc mode, enable reception
712 * of all multicast frames.
713 */
714 if (ifp->if_flags & IFF_PROMISC) {
715 allmulti:
716 CSR_WRITE_4(sc, VGE_MAR0, 0xFFFFFFFF);
717 CSR_WRITE_4(sc, VGE_MAR1, 0xFFFFFFFF);
718 ifp->if_flags |= IFF_ALLMULTI;
719 return;
720 }
721
722 /* Now program new ones */
723 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
724 while (enm != NULL) {
725 /*
726 * If multicast range, fall back to ALLMULTI.
727 */
728 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
729 ETHER_ADDR_LEN) != 0)
730 goto allmulti;
731
732 error = vge_cam_set(sc, enm->enm_addrlo);
733 if (error)
734 break;
735
736 ETHER_NEXT_MULTI(step, enm);
737 }
738
739 /* If there were too many addresses, use the hash filter. */
740 if (error) {
741 vge_cam_clear(sc);
742
743 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
744 while (enm != NULL) {
745 /*
746 * If multicast range, fall back to ALLMULTI.
747 */
748 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
749 ETHER_ADDR_LEN) != 0)
750 goto allmulti;
751
752 h = ether_crc32_be(enm->enm_addrlo,
753 ETHER_ADDR_LEN) >> 26;
754 hashes[h >> 5] |= 1 << (h & 0x1f);
755
756 ETHER_NEXT_MULTI(step, enm);
757 }
758
759 CSR_WRITE_4(sc, VGE_MAR0, hashes[0]);
760 CSR_WRITE_4(sc, VGE_MAR1, hashes[1]);
761 }
762 }
763
764 static void
765 vge_reset(struct vge_softc *sc)
766 {
767 int i;
768
769 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_SOFTRESET);
770
771 for (i = 0; i < VGE_TIMEOUT; i++) {
772 DELAY(5);
773 if ((CSR_READ_1(sc, VGE_CRS1) & VGE_CR1_SOFTRESET) == 0)
774 break;
775 }
776
777 if (i == VGE_TIMEOUT) {
778 aprint_error("%s: soft reset timed out", sc->sc_dev.dv_xname);
779 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_STOP_FORCE);
780 DELAY(2000);
781 }
782
783 DELAY(5000);
784
785 CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_RELOAD);
786
787 for (i = 0; i < VGE_TIMEOUT; i++) {
788 DELAY(5);
789 if ((CSR_READ_1(sc, VGE_EECSR) & VGE_EECSR_RELOAD) == 0)
790 break;
791 }
792
793 if (i == VGE_TIMEOUT) {
794 aprint_error("%s: EEPROM reload timed out\n",
795 sc->sc_dev.dv_xname);
796 return;
797 }
798
799 /*
800 * On some machine, the first read data from EEPROM could be
801 * messed up, so read one dummy data here to avoid the mess.
802 */
803 (void)vge_read_eeprom(sc, 0);
804
805 CSR_CLRBIT_1(sc, VGE_CHIPCFG0, VGE_CHIPCFG0_PACPI);
806 }
807
808 /*
809 * Probe for a VIA gigabit chip. Check the PCI vendor and device
810 * IDs against our list and return a device name if we find a match.
811 */
812 static int
813 vge_probe(struct device *parent, struct cfdata *match,
814 void *aux)
815 {
816 struct pci_attach_args *pa = aux;
817
818 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_VIATECH
819 && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT612X)
820 return 1;
821
822 return 0;
823 }
824
825 static int
826 vge_allocmem(struct vge_softc *sc)
827 {
828 int error;
829 int nseg;
830 int i;
831 bus_dma_segment_t seg;
832
833 /*
834 * Allocate memory for control data.
835 */
836
837 error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct vge_control_data),
838 VGE_RING_ALIGN, 0, &seg, 1, &nseg, BUS_DMA_NOWAIT);
839 if (error) {
840 aprint_error("%s: could not allocate control data dma memory\n",
841 sc->sc_dev.dv_xname);
842 return ENOMEM;
843 }
844
845 /* Map the memory to kernel VA space */
846
847 error = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
848 sizeof(struct vge_control_data), (caddr_t *)&sc->sc_control_data,
849 BUS_DMA_NOWAIT);
850 if (error) {
851 aprint_error("%s: could not map control data dma memory\n",
852 sc->sc_dev.dv_xname);
853 return ENOMEM;
854 }
855 memset(sc->sc_control_data, 0, sizeof(struct vge_control_data));
856
857 /*
858 * Create map for control data.
859 */
860 error = bus_dmamap_create(sc->sc_dmat,
861 sizeof(struct vge_control_data), 1,
862 sizeof(struct vge_control_data), 0, BUS_DMA_NOWAIT,
863 &sc->sc_cddmamap);
864 if (error) {
865 aprint_error("%s: could not create control data dmamap\n",
866 sc->sc_dev.dv_xname);
867 return ENOMEM;
868 }
869
870 /* Load the map for the control data. */
871 error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
872 sc->sc_control_data, sizeof(struct vge_control_data), NULL,
873 BUS_DMA_NOWAIT);
874 if (error) {
875 aprint_error("%s: could not load control data dma memory\n",
876 sc->sc_dev.dv_xname);
877 return ENOMEM;
878 }
879
880 /* Create DMA maps for TX buffers */
881
882 for (i = 0; i < VGE_NTXDESC; i++) {
883 error = bus_dmamap_create(sc->sc_dmat, VGE_TX_MAXLEN,
884 VGE_TX_FRAGS, VGE_TX_MAXLEN, 0, BUS_DMA_NOWAIT,
885 &sc->sc_txsoft[i].txs_dmamap);
886 if (error) {
887 aprint_error("%s: can't create DMA map for TX descs\n",
888 sc->sc_dev.dv_xname);
889 return ENOMEM;
890 }
891 }
892
893 /* Create DMA maps for RX buffers */
894
895 for (i = 0; i < VGE_NRXDESC; i++) {
896 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
897 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
898 &sc->sc_rxsoft[i].rxs_dmamap);
899 if (error) {
900 aprint_error("%s: can't create DMA map for RX descs\n",
901 sc->sc_dev.dv_xname);
902 return ENOMEM;
903 }
904 sc->sc_rxsoft[i].rxs_mbuf = NULL;
905 }
906
907 return 0;
908 }
909
910 /*
911 * Attach the interface. Allocate softc structures, do ifmedia
912 * setup and ethernet/BPF attach.
913 */
914 static void
915 vge_attach(struct device *parent, struct device *self, void *aux)
916 {
917 uint8_t *eaddr;
918 struct vge_softc *sc = (void *)self;
919 struct ifnet *ifp;
920 struct pci_attach_args *pa = aux;
921 pci_chipset_tag_t pc = pa->pa_pc;
922 const char *intrstr;
923 pci_intr_handle_t ih;
924 uint16_t val;
925
926 aprint_normal(": VIA VT612X Gigabit Ethernet (rev. %#x)\n",
927 PCI_REVISION(pa->pa_class));
928
929 /* Make sure bus-mastering is enabled */
930 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
931 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
932 PCI_COMMAND_MASTER_ENABLE);
933
934 /*
935 * Map control/status registers.
936 */
937 if (pci_mapreg_map(pa, VGE_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
938 &sc->sc_bst, &sc->sc_bsh, NULL, NULL) != 0) {
939 aprint_error("%s: couldn't map memory\n", sc->sc_dev.dv_xname);
940 return;
941 }
942
943 /*
944 * Map and establish our interrupt.
945 */
946 if (pci_intr_map(pa, &ih)) {
947 aprint_error("%s: unable to map interrupt\n",
948 sc->sc_dev.dv_xname);
949 return;
950 }
951 intrstr = pci_intr_string(pc, ih);
952 sc->sc_intrhand = pci_intr_establish(pc, ih, IPL_NET, vge_intr, sc);
953 if (sc->sc_intrhand == NULL) {
954 aprint_error("%s: unable to establish interrupt",
955 sc->sc_dev.dv_xname);
956 if (intrstr != NULL)
957 aprint_error(" at %s", intrstr);
958 aprint_error("\n");
959 return;
960 }
961 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
962
963 /* Reset the adapter. */
964 vge_reset(sc);
965
966 /*
967 * Get station address from the EEPROM.
968 */
969 eaddr = sc->sc_eaddr;
970 val = vge_read_eeprom(sc, VGE_EE_EADDR + 0);
971 eaddr[0] = val & 0xff;
972 eaddr[1] = val >> 8;
973 val = vge_read_eeprom(sc, VGE_EE_EADDR + 1);
974 eaddr[2] = val & 0xff;
975 eaddr[3] = val >> 8;
976 val = vge_read_eeprom(sc, VGE_EE_EADDR + 2);
977 eaddr[4] = val & 0xff;
978 eaddr[5] = val >> 8;
979
980 aprint_normal("%s: Ethernet address: %s\n", sc->sc_dev.dv_xname,
981 ether_sprintf(eaddr));
982
983 /*
984 * Use the 32bit tag. Hardware supports 48bit physical addresses,
985 * but we don't use that for now.
986 */
987 sc->sc_dmat = pa->pa_dmat;
988
989 if (vge_allocmem(sc))
990 return;
991
992 ifp = &sc->sc_ethercom.ec_if;
993 ifp->if_softc = sc;
994 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
995 ifp->if_mtu = ETHERMTU;
996 ifp->if_baudrate = IF_Gbps(1);
997 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
998 ifp->if_ioctl = vge_ioctl;
999 ifp->if_start = vge_start;
1000
1001 /*
1002 * We can support 802.1Q VLAN-sized frames and jumbo
1003 * Ethernet frames.
1004 */
1005 sc->sc_ethercom.ec_capabilities |=
1006 ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU |
1007 ETHERCAP_VLAN_HWTAGGING;
1008
1009 /*
1010 * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
1011 */
1012 ifp->if_capabilities |=
1013 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
1014 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
1015 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
1016
1017 #ifdef DEVICE_POLLING
1018 #ifdef IFCAP_POLLING
1019 ifp->if_capabilities |= IFCAP_POLLING;
1020 #endif
1021 #endif
1022 ifp->if_watchdog = vge_watchdog;
1023 ifp->if_init = vge_init;
1024 IFQ_SET_MAXLEN(&ifp->if_snd, max(VGE_IFQ_MAXLEN, IFQ_MAXLEN));
1025
1026 /*
1027 * Initialize our media structures and probe the MII.
1028 */
1029 sc->sc_mii.mii_ifp = ifp;
1030 sc->sc_mii.mii_readreg = vge_miibus_readreg;
1031 sc->sc_mii.mii_writereg = vge_miibus_writereg;
1032 sc->sc_mii.mii_statchg = vge_miibus_statchg;
1033 ifmedia_init(&sc->sc_mii.mii_media, 0, vge_ifmedia_upd,
1034 vge_ifmedia_sts);
1035 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
1036 MII_OFFSET_ANY, MIIF_DOPAUSE);
1037 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
1038 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
1039 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
1040 } else
1041 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
1042
1043 /*
1044 * Attach the interface.
1045 */
1046 if_attach(ifp);
1047 ether_ifattach(ifp, eaddr);
1048
1049 callout_init(&sc->sc_timeout);
1050 callout_setfunc(&sc->sc_timeout, vge_tick, sc);
1051
1052 /*
1053 * Make sure the interface is shutdown during reboot.
1054 */
1055 if (shutdownhook_establish(vge_shutdown, sc) == NULL) {
1056 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
1057 sc->sc_dev.dv_xname);
1058 }
1059 }
1060
1061 static int
1062 vge_newbuf(struct vge_softc *sc, int idx, struct mbuf *m)
1063 {
1064 struct mbuf *m_new;
1065 struct vge_rxdesc *rxd;
1066 struct vge_rxsoft *rxs;
1067 bus_dmamap_t map;
1068 int i;
1069 #ifdef DIAGNOSTIC
1070 uint32_t rd_sts;
1071 #endif
1072
1073 m_new = NULL;
1074 if (m == NULL) {
1075 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1076 if (m_new == NULL)
1077 return ENOBUFS;
1078
1079 MCLGET(m_new, M_DONTWAIT);
1080 if ((m_new->m_flags & M_EXT) == 0) {
1081 m_freem(m_new);
1082 return ENOBUFS;
1083 }
1084
1085 m = m_new;
1086 } else
1087 m->m_data = m->m_ext.ext_buf;
1088
1089
1090 /*
1091 * This is part of an evil trick to deal with non-x86 platforms.
1092 * The VIA chip requires RX buffers to be aligned on 32-bit
1093 * boundaries, but that will hose non-x86 machines. To get around
1094 * this, we leave some empty space at the start of each buffer
1095 * and for non-x86 hosts, we copy the buffer back two bytes
1096 * to achieve word alignment. This is slightly more efficient
1097 * than allocating a new buffer, copying the contents, and
1098 * discarding the old buffer.
1099 */
1100 m->m_len = m->m_pkthdr.len = VGE_RX_BUFSIZE;
1101 #ifndef __NO_STRICT_ALIGNMENT
1102 m->m_data += VGE_RX_PAD;
1103 #endif
1104 rxs = &sc->sc_rxsoft[idx];
1105 map = rxs->rxs_dmamap;
1106
1107 if (bus_dmamap_load_mbuf(sc->sc_dmat, map, m, BUS_DMA_NOWAIT) != 0)
1108 goto out;
1109
1110 rxd = &sc->sc_rxdescs[idx];
1111
1112 #ifdef DIAGNOSTIC
1113 /* If this descriptor is still owned by the chip, bail. */
1114 VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1115 rd_sts = le32toh(rxd->rd_sts);
1116 VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1117 if (rd_sts & VGE_RDSTS_OWN) {
1118 panic("%s: tried to map busy RX descriptor",
1119 sc->sc_dev.dv_xname);
1120 }
1121 #endif
1122
1123 rxs->rxs_mbuf = m;
1124 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1125 BUS_DMASYNC_PREREAD);
1126
1127 rxd->rd_buflen =
1128 htole16(VGE_BUFLEN(map->dm_segs[0].ds_len) | VGE_RXDESC_I);
1129 vge_set_rxaddr(rxd, map->dm_segs[0].ds_addr);
1130 rxd->rd_sts = 0;
1131 rxd->rd_ctl = 0;
1132 VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1133
1134 /*
1135 * Note: the manual fails to document the fact that for
1136 * proper opration, the driver needs to replentish the RX
1137 * DMA ring 4 descriptors at a time (rather than one at a
1138 * time, like most chips). We can allocate the new buffers
1139 * but we should not set the OWN bits until we're ready
1140 * to hand back 4 of them in one shot.
1141 */
1142
1143 #define VGE_RXCHUNK 4
1144 sc->sc_rx_consumed++;
1145 if (sc->sc_rx_consumed == VGE_RXCHUNK) {
1146 for (i = idx; i != idx - VGE_RXCHUNK; i--) {
1147 KASSERT(i >= 0);
1148 sc->sc_rxdescs[i].rd_sts |= htole32(VGE_RDSTS_OWN);
1149 VGE_RXDESCSYNC(sc, i,
1150 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1151 }
1152 sc->sc_rx_consumed = 0;
1153 }
1154
1155 return 0;
1156 out:
1157 if (m_new != NULL)
1158 m_freem(m_new);
1159 return ENOMEM;
1160 }
1161
1162 #ifndef __NO_STRICT_ALIGNMENT
1163 static inline void
1164 vge_fixup_rx(struct mbuf *m)
1165 {
1166 int i;
1167 uint16_t *src, *dst;
1168
1169 src = mtod(m, uint16_t *);
1170 dst = src - 1;
1171
1172 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1173 *dst++ = *src++;
1174
1175 m->m_data -= ETHER_ALIGN;
1176 }
1177 #endif
1178
1179 /*
1180 * RX handler. We support the reception of jumbo frames that have
1181 * been fragmented across multiple 2K mbuf cluster buffers.
1182 */
1183 static void
1184 vge_rxeof(struct vge_softc *sc)
1185 {
1186 struct mbuf *m;
1187 struct ifnet *ifp;
1188 int idx, total_len, lim;
1189 struct vge_rxdesc *cur_rxd;
1190 struct vge_rxsoft *rxs;
1191 uint32_t rxstat, rxctl;
1192
1193 ifp = &sc->sc_ethercom.ec_if;
1194 lim = 0;
1195
1196 /* Invalidate the descriptor memory */
1197
1198 for (idx = sc->sc_rx_prodidx;; idx = VGE_NEXT_RXDESC(idx)) {
1199 cur_rxd = &sc->sc_rxdescs[idx];
1200
1201 VGE_RXDESCSYNC(sc, idx,
1202 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1203 rxstat = le32toh(cur_rxd->rd_sts);
1204 if ((rxstat & VGE_RDSTS_OWN) != 0) {
1205 VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1206 break;
1207 }
1208
1209 rxctl = le32toh(cur_rxd->rd_ctl);
1210 rxs = &sc->sc_rxsoft[idx];
1211 m = rxs->rxs_mbuf;
1212 total_len = (rxstat & VGE_RDSTS_BUFSIZ) >> 16;
1213
1214 /* Invalidate the RX mbuf and unload its map */
1215
1216 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap,
1217 0, rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1218 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1219
1220 /*
1221 * If the 'start of frame' bit is set, this indicates
1222 * either the first fragment in a multi-fragment receive,
1223 * or an intermediate fragment. Either way, we want to
1224 * accumulate the buffers.
1225 */
1226 if (rxstat & VGE_RXPKT_SOF) {
1227 m->m_len = VGE_RX_BUFSIZE;
1228 if (sc->sc_rx_mhead == NULL)
1229 sc->sc_rx_mhead = sc->sc_rx_mtail = m;
1230 else {
1231 m->m_flags &= ~M_PKTHDR;
1232 sc->sc_rx_mtail->m_next = m;
1233 sc->sc_rx_mtail = m;
1234 }
1235 vge_newbuf(sc, idx, NULL);
1236 continue;
1237 }
1238
1239 /*
1240 * Bad/error frames will have the RXOK bit cleared.
1241 * However, there's one error case we want to allow:
1242 * if a VLAN tagged frame arrives and the chip can't
1243 * match it against the CAM filter, it considers this
1244 * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
1245 * We don't want to drop the frame though: our VLAN
1246 * filtering is done in software.
1247 */
1248 if (!(rxstat & VGE_RDSTS_RXOK) && !(rxstat & VGE_RDSTS_VIDM)
1249 && !(rxstat & VGE_RDSTS_CSUMERR)) {
1250 ifp->if_ierrors++;
1251 /*
1252 * If this is part of a multi-fragment packet,
1253 * discard all the pieces.
1254 */
1255 if (sc->sc_rx_mhead != NULL) {
1256 m_freem(sc->sc_rx_mhead);
1257 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1258 }
1259 vge_newbuf(sc, idx, m);
1260 continue;
1261 }
1262
1263 /*
1264 * If allocating a replacement mbuf fails,
1265 * reload the current one.
1266 */
1267
1268 if (vge_newbuf(sc, idx, NULL)) {
1269 ifp->if_ierrors++;
1270 if (sc->sc_rx_mhead != NULL) {
1271 m_freem(sc->sc_rx_mhead);
1272 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1273 }
1274 vge_newbuf(sc, idx, m);
1275 continue;
1276 }
1277
1278 if (sc->sc_rx_mhead != NULL) {
1279 m->m_len = total_len % VGE_RX_BUFSIZE;
1280 /*
1281 * Special case: if there's 4 bytes or less
1282 * in this buffer, the mbuf can be discarded:
1283 * the last 4 bytes is the CRC, which we don't
1284 * care about anyway.
1285 */
1286 if (m->m_len <= ETHER_CRC_LEN) {
1287 sc->sc_rx_mtail->m_len -=
1288 (ETHER_CRC_LEN - m->m_len);
1289 m_freem(m);
1290 } else {
1291 m->m_len -= ETHER_CRC_LEN;
1292 m->m_flags &= ~M_PKTHDR;
1293 sc->sc_rx_mtail->m_next = m;
1294 }
1295 m = sc->sc_rx_mhead;
1296 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1297 m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1298 } else
1299 m->m_pkthdr.len = m->m_len = total_len - ETHER_CRC_LEN;
1300
1301 #ifndef __NO_STRICT_ALIGNMENT
1302 vge_fixup_rx(m);
1303 #endif
1304 ifp->if_ipackets++;
1305 m->m_pkthdr.rcvif = ifp;
1306
1307 /* Do RX checksumming if enabled */
1308 if (ifp->if_csum_flags_rx & M_CSUM_IPv4) {
1309
1310 /* Check IP header checksum */
1311 if (rxctl & VGE_RDCTL_IPPKT)
1312 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1313 if ((rxctl & VGE_RDCTL_IPCSUMOK) == 0)
1314 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1315 }
1316
1317 if (ifp->if_csum_flags_rx & M_CSUM_TCPv4) {
1318 /* Check UDP checksum */
1319 if (rxctl & VGE_RDCTL_TCPPKT)
1320 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1321
1322 if ((rxctl & VGE_RDCTL_PROTOCSUMOK) == 0)
1323 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1324 }
1325
1326 if (ifp->if_csum_flags_rx & M_CSUM_UDPv4) {
1327 /* Check UDP checksum */
1328 if (rxctl & VGE_RDCTL_UDPPKT)
1329 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1330
1331 if ((rxctl & VGE_RDCTL_PROTOCSUMOK) == 0)
1332 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1333 }
1334
1335 if (rxstat & VGE_RDSTS_VTAG) {
1336 /*
1337 * We use bswap16() here because:
1338 * On LE machines, tag is stored in BE as stream data.
1339 * On BE machines, tag is stored in BE as stream data
1340 * but it was already swapped by le32toh() above.
1341 */
1342 VLAN_INPUT_TAG(ifp, m,
1343 bswap16(rxctl & VGE_RDCTL_VLANID), continue);
1344 }
1345
1346 #if NBPFILTER > 0
1347 /*
1348 * Handle BPF listeners.
1349 */
1350 if (ifp->if_bpf)
1351 bpf_mtap(ifp->if_bpf, m);
1352 #endif
1353
1354 (*ifp->if_input)(ifp, m);
1355
1356 lim++;
1357 if (lim == VGE_NRXDESC)
1358 break;
1359
1360 }
1361
1362 sc->sc_rx_prodidx = idx;
1363 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, lim);
1364 }
1365
1366 static void
1367 vge_txeof(struct vge_softc *sc)
1368 {
1369 struct ifnet *ifp;
1370 struct vge_txsoft *txs;
1371 uint32_t txstat;
1372 int idx;
1373
1374 ifp = &sc->sc_ethercom.ec_if;
1375
1376 for (idx = sc->sc_tx_considx;
1377 sc->sc_tx_free < VGE_NTXDESC;
1378 idx = VGE_NEXT_TXDESC(idx), sc->sc_tx_free++) {
1379 VGE_TXDESCSYNC(sc, idx,
1380 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1381 txstat = le32toh(sc->sc_txdescs[idx].td_sts);
1382 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1383 if (txstat & VGE_TDSTS_OWN) {
1384 break;
1385 }
1386
1387 txs = &sc->sc_txsoft[idx];
1388 m_freem(txs->txs_mbuf);
1389 txs->txs_mbuf = NULL;
1390 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap, 0,
1391 txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1392 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1393 if (txstat & (VGE_TDSTS_EXCESSCOLL|VGE_TDSTS_COLL))
1394 ifp->if_collisions++;
1395 if (txstat & VGE_TDSTS_TXERR)
1396 ifp->if_oerrors++;
1397 else
1398 ifp->if_opackets++;
1399 }
1400
1401 sc->sc_tx_considx = idx;
1402
1403 if (sc->sc_tx_free > 0) {
1404 ifp->if_flags &= ~IFF_OACTIVE;
1405 }
1406
1407 /*
1408 * If not all descriptors have been released reaped yet,
1409 * reload the timer so that we will eventually get another
1410 * interrupt that will cause us to re-enter this routine.
1411 * This is done in case the transmitter has gone idle.
1412 */
1413 if (sc->sc_tx_free < VGE_NTXDESC)
1414 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1415 else
1416 ifp->if_timer = 0;
1417 }
1418
1419 static void
1420 vge_tick(void *xsc)
1421 {
1422 struct vge_softc *sc;
1423 struct ifnet *ifp;
1424 struct mii_data *mii;
1425 int s;
1426
1427 sc = xsc;
1428 ifp = &sc->sc_ethercom.ec_if;
1429 mii = &sc->sc_mii;
1430
1431 s = splnet();
1432
1433 callout_schedule(&sc->sc_timeout, hz);
1434
1435 mii_tick(mii);
1436 if (sc->sc_link) {
1437 if (!(mii->mii_media_status & IFM_ACTIVE))
1438 sc->sc_link = 0;
1439 } else {
1440 if (mii->mii_media_status & IFM_ACTIVE &&
1441 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1442 sc->sc_link = 1;
1443 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1444 vge_start(ifp);
1445 }
1446 }
1447
1448 splx(s);
1449 }
1450
1451 static int
1452 vge_intr(void *arg)
1453 {
1454 struct vge_softc *sc;
1455 struct ifnet *ifp;
1456 uint32_t status;
1457 int claim;
1458
1459 sc = arg;
1460 claim = 0;
1461 if (sc->sc_suspended) {
1462 return claim;
1463 }
1464
1465 ifp = &sc->sc_ethercom.ec_if;
1466
1467 if (!(ifp->if_flags & IFF_UP)) {
1468 return claim;
1469 }
1470
1471 /* Disable interrupts */
1472 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1473
1474 for (;;) {
1475
1476 status = CSR_READ_4(sc, VGE_ISR);
1477 /* If the card has gone away the read returns 0xffff. */
1478 if (status == 0xFFFFFFFF)
1479 break;
1480
1481 if (status) {
1482 claim = 1;
1483 CSR_WRITE_4(sc, VGE_ISR, status);
1484 }
1485
1486 if ((status & VGE_INTRS) == 0)
1487 break;
1488
1489 if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
1490 vge_rxeof(sc);
1491
1492 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1493 vge_rxeof(sc);
1494 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1495 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1496 }
1497
1498 if (status & (VGE_ISR_TXOK0|VGE_ISR_TIMER0))
1499 vge_txeof(sc);
1500
1501 if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL))
1502 vge_init(ifp);
1503
1504 if (status & VGE_ISR_LINKSTS)
1505 vge_tick(sc);
1506 }
1507
1508 /* Re-enable interrupts */
1509 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1510
1511 if (claim && !IFQ_IS_EMPTY(&ifp->if_snd))
1512 vge_start(ifp);
1513
1514 return claim;
1515 }
1516
1517 static int
1518 vge_encap(struct vge_softc *sc, struct mbuf *m_head, int idx)
1519 {
1520 struct vge_txsoft *txs;
1521 struct vge_txdesc *txd;
1522 struct vge_txfrag *f;
1523 struct mbuf *m_new;
1524 bus_dmamap_t map;
1525 int m_csumflags, seg, error, flags;
1526 struct m_tag *mtag;
1527 size_t sz;
1528 uint32_t td_sts, td_ctl;
1529
1530 KASSERT(sc->sc_tx_free > 0);
1531
1532 txd = &sc->sc_txdescs[idx];
1533
1534 #ifdef DIAGNOSTIC
1535 /* If this descriptor is still owned by the chip, bail. */
1536 VGE_TXDESCSYNC(sc, idx,
1537 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1538 td_sts = le32toh(txd->td_sts);
1539 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1540 if (td_sts & VGE_TDSTS_OWN) {
1541 return ENOBUFS;
1542 }
1543 #endif
1544
1545 /*
1546 * Preserve m_pkthdr.csum_flags here since m_head might be
1547 * updated by m_defrag()
1548 */
1549 m_csumflags = m_head->m_pkthdr.csum_flags;
1550
1551 txs = &sc->sc_txsoft[idx];
1552 map = txs->txs_dmamap;
1553 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m_head, BUS_DMA_NOWAIT);
1554
1555 /* If too many segments to map, coalesce */
1556 if (error == EFBIG ||
1557 (m_head->m_pkthdr.len < ETHER_PAD_LEN &&
1558 map->dm_nsegs == VGE_TX_FRAGS)) {
1559 m_new = m_defrag(m_head, M_DONTWAIT);
1560 if (m_new == NULL)
1561 return EFBIG;
1562
1563 error = bus_dmamap_load_mbuf(sc->sc_dmat, map,
1564 m_new, BUS_DMA_NOWAIT);
1565 if (error) {
1566 m_freem(m_new);
1567 return error;
1568 }
1569
1570 m_head = m_new;
1571 } else if (error)
1572 return error;
1573
1574 txs->txs_mbuf = m_head;
1575
1576 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1577 BUS_DMASYNC_PREWRITE);
1578
1579 for (seg = 0, f = &txd->td_frag[0]; seg < map->dm_nsegs; seg++, f++) {
1580 f->tf_buflen = htole16(VGE_BUFLEN(map->dm_segs[seg].ds_len));
1581 vge_set_txaddr(f, map->dm_segs[seg].ds_addr);
1582 }
1583
1584 /* Argh. This chip does not autopad short frames */
1585 sz = m_head->m_pkthdr.len;
1586 if (sz < ETHER_PAD_LEN) {
1587 f->tf_buflen = htole16(VGE_BUFLEN(ETHER_PAD_LEN - sz));
1588 vge_set_txaddr(f, VGE_CDPADADDR(sc));
1589 sz = ETHER_PAD_LEN;
1590 seg++;
1591 }
1592 VGE_TXFRAGSYNC(sc, idx, seg, BUS_DMASYNC_PREWRITE);
1593
1594 /*
1595 * When telling the chip how many segments there are, we
1596 * must use nsegs + 1 instead of just nsegs. Darned if I
1597 * know why.
1598 */
1599 seg++;
1600
1601 flags = 0;
1602 if (m_csumflags & M_CSUM_IPv4)
1603 flags |= VGE_TDCTL_IPCSUM;
1604 if (m_csumflags & M_CSUM_TCPv4)
1605 flags |= VGE_TDCTL_TCPCSUM;
1606 if (m_csumflags & M_CSUM_UDPv4)
1607 flags |= VGE_TDCTL_UDPCSUM;
1608 td_sts = sz << 16;
1609 td_ctl = flags | (seg << 28) | VGE_TD_LS_NORM;
1610
1611 if (sz > ETHERMTU + ETHER_HDR_LEN)
1612 td_ctl |= VGE_TDCTL_JUMBO;
1613
1614 /*
1615 * Set up hardware VLAN tagging.
1616 */
1617 mtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m_head);
1618 if (mtag != NULL) {
1619 /*
1620 * No need htons() here since vge(4) chip assumes
1621 * that tags are written in little endian and
1622 * we already use htole32() here.
1623 */
1624 td_ctl |= VLAN_TAG_VALUE(mtag) | VGE_TDCTL_VTAG;
1625 }
1626 txd->td_ctl = htole32(td_ctl);
1627 txd->td_sts = htole32(td_sts);
1628 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1629
1630 txd->td_sts = htole32(VGE_TDSTS_OWN | td_sts);
1631 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1632
1633 sc->sc_tx_free--;
1634
1635 return 0;
1636 }
1637
1638 /*
1639 * Main transmit routine.
1640 */
1641
1642 static void
1643 vge_start(struct ifnet *ifp)
1644 {
1645 struct vge_softc *sc;
1646 struct vge_txsoft *txs;
1647 struct mbuf *m_head;
1648 int idx, pidx, ofree, error;
1649
1650 sc = ifp->if_softc;
1651
1652 if (!sc->sc_link ||
1653 (ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) {
1654 return;
1655 }
1656
1657 m_head = NULL;
1658 idx = sc->sc_tx_prodidx;
1659 pidx = VGE_PREV_TXDESC(idx);
1660 ofree = sc->sc_tx_free;
1661
1662 /*
1663 * Loop through the send queue, setting up transmit descriptors
1664 * until we drain the queue, or use up all available transmit
1665 * descriptors.
1666 */
1667 for (;;) {
1668 /* Grab a packet off the queue. */
1669 IFQ_POLL(&ifp->if_snd, m_head);
1670 if (m_head == NULL)
1671 break;
1672
1673 if (sc->sc_tx_free == 0) {
1674 /*
1675 * All slots used, stop for now.
1676 */
1677 ifp->if_flags |= IFF_OACTIVE;
1678 break;
1679 }
1680
1681 txs = &sc->sc_txsoft[idx];
1682 KASSERT(txs->txs_mbuf == NULL);
1683
1684 if ((error = vge_encap(sc, m_head, idx))) {
1685 if (error == EFBIG) {
1686 aprint_error("%s: Tx packet consumes too many "
1687 "DMA segments, dropping...\n",
1688 sc->sc_dev.dv_xname);
1689 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1690 m_freem(m_head);
1691 continue;
1692 }
1693
1694 /*
1695 * Short on resources, just stop for now.
1696 */
1697 if (error == ENOBUFS)
1698 ifp->if_flags |= IFF_OACTIVE;
1699 break;
1700 }
1701
1702 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1703
1704 /*
1705 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1706 */
1707
1708 sc->sc_txdescs[pidx].td_frag[0].tf_buflen |=
1709 htole16(VGE_TXDESC_Q);
1710 VGE_TXFRAGSYNC(sc, pidx, 1,
1711 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1712
1713 if (txs->txs_mbuf != m_head) {
1714 m_freem(m_head);
1715 m_head = txs->txs_mbuf;
1716 }
1717
1718 pidx = idx;
1719 idx = VGE_NEXT_TXDESC(idx);
1720
1721 /*
1722 * If there's a BPF listener, bounce a copy of this frame
1723 * to him.
1724 */
1725 #if NBPFILTER > 0
1726 if (ifp->if_bpf)
1727 bpf_mtap(ifp->if_bpf, m_head);
1728 #endif
1729 }
1730
1731 if (sc->sc_tx_free < ofree) {
1732 /* TX packet queued */
1733
1734 sc->sc_tx_prodidx = idx;
1735
1736 /* Issue a transmit command. */
1737 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
1738
1739 /*
1740 * Use the countdown timer for interrupt moderation.
1741 * 'TX done' interrupts are disabled. Instead, we reset the
1742 * countdown timer, which will begin counting until it hits
1743 * the value in the SSTIMER register, and then trigger an
1744 * interrupt. Each time we set the TIMER0_ENABLE bit, the
1745 * the timer count is reloaded. Only when the transmitter
1746 * is idle will the timer hit 0 and an interrupt fire.
1747 */
1748 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1749
1750 /*
1751 * Set a timeout in case the chip goes out to lunch.
1752 */
1753 ifp->if_timer = 5;
1754 }
1755 }
1756
1757 static int
1758 vge_init(struct ifnet *ifp)
1759 {
1760 struct vge_softc *sc;
1761 struct vge_rxsoft *rxs;
1762 int i;
1763
1764 sc = ifp->if_softc;
1765
1766 /*
1767 * Cancel pending I/O and free all RX/TX buffers.
1768 */
1769 vge_stop(sc);
1770 vge_reset(sc);
1771
1772 /* Initialize the RX descriptors and mbufs. */
1773 memset(sc->sc_rxdescs, 0, sizeof(sc->sc_rxdescs));
1774 for (i = 0; i < VGE_NRXDESC; i++) {
1775 rxs = &sc->sc_rxsoft[i];
1776 if (rxs->rxs_mbuf) {
1777 m_freem(rxs->rxs_mbuf);
1778 rxs->rxs_mbuf = NULL;
1779 }
1780 if (rxs->rxs_dmamap)
1781 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1782 if (vge_newbuf(sc, i, NULL) == ENOBUFS) {
1783 aprint_error("%s: unable to allocate or map "
1784 "rx buffer\n", sc->sc_dev.dv_xname);
1785 return 1; /* XXX */
1786 }
1787 }
1788 sc->sc_rx_prodidx = 0;
1789 sc->sc_rx_consumed = 0;
1790 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1791
1792 /* Initialize the TX descriptors and mbufs. */
1793 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1794 bus_dmamap_sync(sc->sc_dmat, sc->sc_cddmamap,
1795 VGE_CDTXOFF(0), sizeof(sc->sc_txdescs),
1796 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1797 for (i = 0; i < VGE_NTXDESC; i++)
1798 sc->sc_txsoft[i].txs_mbuf = NULL;
1799
1800 sc->sc_tx_prodidx = 0;
1801 sc->sc_tx_considx = 0;
1802 sc->sc_tx_free = VGE_NTXDESC;
1803
1804 /* Set our station address */
1805 for (i = 0; i < ETHER_ADDR_LEN; i++)
1806 CSR_WRITE_1(sc, VGE_PAR0 + i, sc->sc_eaddr[i]);
1807
1808 /*
1809 * Set receive FIFO threshold. Also allow transmission and
1810 * reception of VLAN tagged frames.
1811 */
1812 CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
1813 CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES|VGE_VTAG_OPT2);
1814
1815 /* Set DMA burst length */
1816 CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
1817 CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
1818
1819 CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
1820
1821 /* Set collision backoff algorithm */
1822 CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
1823 VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
1824 CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
1825
1826 /* Disable LPSEL field in priority resolution */
1827 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
1828
1829 /*
1830 * Load the addresses of the DMA queues into the chip.
1831 * Note that we only use one transmit queue.
1832 */
1833
1834 CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0, VGE_ADDR_LO(VGE_CDTXADDR(sc, 0)));
1835 CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_NTXDESC - 1);
1836
1837 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, VGE_ADDR_LO(VGE_CDRXADDR(sc, 0)));
1838 CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_NRXDESC - 1);
1839 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_NRXDESC);
1840
1841 /* Enable and wake up the RX descriptor queue */
1842 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1843 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1844
1845 /* Enable the TX descriptor queue */
1846 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
1847
1848 /* Set up the receive filter -- allow large frames for VLANs. */
1849 CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST|VGE_RXCTL_RX_GIANT);
1850
1851 /* If we want promiscuous mode, set the allframes bit. */
1852 if (ifp->if_flags & IFF_PROMISC) {
1853 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
1854 }
1855
1856 /* Set capture broadcast bit to capture broadcast frames. */
1857 if (ifp->if_flags & IFF_BROADCAST) {
1858 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST);
1859 }
1860
1861 /* Set multicast bit to capture multicast frames. */
1862 if (ifp->if_flags & IFF_MULTICAST) {
1863 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST);
1864 }
1865
1866 /* Init the cam filter. */
1867 vge_cam_clear(sc);
1868
1869 /* Init the multicast filter. */
1870 vge_setmulti(sc);
1871
1872 /* Enable flow control */
1873
1874 CSR_WRITE_1(sc, VGE_CRS2, 0x8B);
1875
1876 /* Enable jumbo frame reception (if desired) */
1877
1878 /* Start the MAC. */
1879 CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
1880 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
1881 CSR_WRITE_1(sc, VGE_CRS0,
1882 VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
1883
1884 /*
1885 * Configure one-shot timer for microsecond
1886 * resulution and load it for 500 usecs.
1887 */
1888 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES);
1889 CSR_WRITE_2(sc, VGE_SSTIMER, 400);
1890
1891 /*
1892 * Configure interrupt moderation for receive. Enable
1893 * the holdoff counter and load it, and set the RX
1894 * suppression count to the number of descriptors we
1895 * want to allow before triggering an interrupt.
1896 * The holdoff timer is in units of 20 usecs.
1897 */
1898
1899 #ifdef notyet
1900 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE);
1901 /* Select the interrupt holdoff timer page. */
1902 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1903 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
1904 CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */
1905
1906 /* Enable use of the holdoff timer. */
1907 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
1908 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD);
1909
1910 /* Select the RX suppression threshold page. */
1911 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1912 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
1913 CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */
1914
1915 /* Restore the page select bits. */
1916 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1917 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
1918 #endif
1919
1920 #ifdef DEVICE_POLLING
1921 /*
1922 * Disable interrupts if we are polling.
1923 */
1924 if (ifp->if_flags & IFF_POLLING) {
1925 CSR_WRITE_4(sc, VGE_IMR, 0);
1926 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1927 } else /* otherwise ... */
1928 #endif /* DEVICE_POLLING */
1929 {
1930 /*
1931 * Enable interrupts.
1932 */
1933 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
1934 CSR_WRITE_4(sc, VGE_ISR, 0);
1935 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1936 }
1937
1938 mii_mediachg(&sc->sc_mii);
1939
1940 ifp->if_flags |= IFF_RUNNING;
1941 ifp->if_flags &= ~IFF_OACTIVE;
1942
1943 sc->sc_if_flags = 0;
1944 sc->sc_link = 0;
1945
1946 callout_schedule(&sc->sc_timeout, hz);
1947
1948 return 0;
1949 }
1950
1951 /*
1952 * Set media options.
1953 */
1954 static int
1955 vge_ifmedia_upd(struct ifnet *ifp)
1956 {
1957 struct vge_softc *sc;
1958
1959 sc = ifp->if_softc;
1960 mii_mediachg(&sc->sc_mii);
1961
1962 return 0;
1963 }
1964
1965 /*
1966 * Report current media status.
1967 */
1968 static void
1969 vge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1970 {
1971 struct vge_softc *sc;
1972 struct mii_data *mii;
1973
1974 sc = ifp->if_softc;
1975 mii = &sc->sc_mii;
1976
1977 mii_pollstat(mii);
1978 ifmr->ifm_active = mii->mii_media_active;
1979 ifmr->ifm_status = mii->mii_media_status;
1980 }
1981
1982 static void
1983 vge_miibus_statchg(struct device *self)
1984 {
1985 struct vge_softc *sc;
1986 struct mii_data *mii;
1987 struct ifmedia_entry *ife;
1988
1989 sc = (void *)self;
1990 mii = &sc->sc_mii;
1991 ife = mii->mii_media.ifm_cur;
1992 /*
1993 * If the user manually selects a media mode, we need to turn
1994 * on the forced MAC mode bit in the DIAGCTL register. If the
1995 * user happens to choose a full duplex mode, we also need to
1996 * set the 'force full duplex' bit. This applies only to
1997 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
1998 * mode is disabled, and in 1000baseT mode, full duplex is
1999 * always implied, so we turn on the forced mode bit but leave
2000 * the FDX bit cleared.
2001 */
2002
2003 switch (IFM_SUBTYPE(ife->ifm_media)) {
2004 case IFM_AUTO:
2005 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2006 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2007 break;
2008 case IFM_1000_T:
2009 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2010 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2011 break;
2012 case IFM_100_TX:
2013 case IFM_10_T:
2014 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2015 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2016 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2017 } else {
2018 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2019 }
2020 break;
2021 default:
2022 aprint_error("%s: unknown media type: %x\n",
2023 sc->sc_dev.dv_xname,
2024 IFM_SUBTYPE(ife->ifm_media));
2025 break;
2026 }
2027 }
2028
2029 static int
2030 vge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2031 {
2032 struct vge_softc *sc;
2033 struct ifreq *ifr;
2034 struct mii_data *mii;
2035 int s, error;
2036
2037 sc = ifp->if_softc;
2038 ifr = (struct ifreq *)data;
2039 error = 0;
2040
2041 s = splnet();
2042
2043 switch (command) {
2044 case SIOCSIFMTU:
2045 if (ifr->ifr_mtu > VGE_JUMBO_MTU)
2046 error = EINVAL;
2047 ifp->if_mtu = ifr->ifr_mtu;
2048 break;
2049 case SIOCSIFFLAGS:
2050 if (ifp->if_flags & IFF_UP) {
2051 if (ifp->if_flags & IFF_RUNNING &&
2052 ifp->if_flags & IFF_PROMISC &&
2053 !(sc->sc_if_flags & IFF_PROMISC)) {
2054 CSR_SETBIT_1(sc, VGE_RXCTL,
2055 VGE_RXCTL_RX_PROMISC);
2056 vge_setmulti(sc);
2057 } else if (ifp->if_flags & IFF_RUNNING &&
2058 !(ifp->if_flags & IFF_PROMISC) &&
2059 sc->sc_if_flags & IFF_PROMISC) {
2060 CSR_CLRBIT_1(sc, VGE_RXCTL,
2061 VGE_RXCTL_RX_PROMISC);
2062 vge_setmulti(sc);
2063 } else
2064 vge_init(ifp);
2065 } else {
2066 if (ifp->if_flags & IFF_RUNNING)
2067 vge_stop(sc);
2068 }
2069 sc->sc_if_flags = ifp->if_flags;
2070 break;
2071 case SIOCADDMULTI:
2072 case SIOCDELMULTI:
2073 error = (command == SIOCADDMULTI) ?
2074 ether_addmulti(ifr, &sc->sc_ethercom) :
2075 ether_delmulti(ifr, &sc->sc_ethercom);
2076
2077 if (error == ENETRESET) {
2078 /*
2079 * Multicast list has changed; set the hardware filter
2080 * accordingly.
2081 */
2082 if (ifp->if_flags & IFF_RUNNING)
2083 vge_setmulti(sc);
2084 error = 0;
2085 }
2086 break;
2087 case SIOCGIFMEDIA:
2088 case SIOCSIFMEDIA:
2089 mii = &sc->sc_mii;
2090 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2091 break;
2092 default:
2093 error = ether_ioctl(ifp, command, data);
2094 break;
2095 }
2096
2097 splx(s);
2098 return error;
2099 }
2100
2101 static void
2102 vge_watchdog(struct ifnet *ifp)
2103 {
2104 struct vge_softc *sc;
2105 int s;
2106
2107 sc = ifp->if_softc;
2108 s = splnet();
2109 aprint_error("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
2110 ifp->if_oerrors++;
2111
2112 vge_txeof(sc);
2113 vge_rxeof(sc);
2114
2115 vge_init(ifp);
2116
2117 splx(s);
2118 }
2119
2120 /*
2121 * Stop the adapter and free any mbufs allocated to the
2122 * RX and TX lists.
2123 */
2124 static void
2125 vge_stop(struct vge_softc *sc)
2126 {
2127 struct ifnet *ifp;
2128 struct vge_txsoft *txs;
2129 struct vge_rxsoft *rxs;
2130 int i, s;
2131
2132 ifp = &sc->sc_ethercom.ec_if;
2133
2134 s = splnet();
2135 ifp->if_timer = 0;
2136
2137 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2138 #ifdef DEVICE_POLLING
2139 ether_poll_deregister(ifp);
2140 #endif /* DEVICE_POLLING */
2141
2142 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2143 CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
2144 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2145 CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
2146 CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
2147 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
2148
2149 if (sc->sc_rx_mhead != NULL) {
2150 m_freem(sc->sc_rx_mhead);
2151 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
2152 }
2153
2154 /* Free the TX list buffers. */
2155
2156 for (i = 0; i < VGE_NTXDESC; i++) {
2157 txs = &sc->sc_txsoft[i];
2158 if (txs->txs_mbuf != NULL) {
2159 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2160 m_freem(txs->txs_mbuf);
2161 txs->txs_mbuf = NULL;
2162 }
2163 }
2164
2165 /* Free the RX list buffers. */
2166
2167 for (i = 0; i < VGE_NRXDESC; i++) {
2168 rxs = &sc->sc_rxsoft[i];
2169 if (rxs->rxs_mbuf != NULL) {
2170 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2171 m_freem(rxs->rxs_mbuf);
2172 rxs->rxs_mbuf = NULL;
2173 }
2174 }
2175
2176 splx(s);
2177 }
2178
2179 #if VGE_POWER_MANAGEMENT
2180 /*
2181 * Device suspend routine. Stop the interface and save some PCI
2182 * settings in case the BIOS doesn't restore them properly on
2183 * resume.
2184 */
2185 static int
2186 vge_suspend(struct device *dev)
2187 {
2188 struct vge_softc *sc;
2189 int i;
2190
2191 sc = device_get_softc(dev);
2192
2193 vge_stop(sc);
2194
2195 for (i = 0; i < 5; i++)
2196 sc->sc_saved_maps[i] =
2197 pci_read_config(dev, PCIR_MAPS + i * 4, 4);
2198 sc->sc_saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
2199 sc->sc_saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
2200 sc->sc_saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
2201 sc->sc_saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
2202
2203 sc->suspended = 1;
2204
2205 return 0;
2206 }
2207
2208 /*
2209 * Device resume routine. Restore some PCI settings in case the BIOS
2210 * doesn't, re-enable busmastering, and restart the interface if
2211 * appropriate.
2212 */
2213 static int
2214 vge_resume(struct device *dev)
2215 {
2216 struct vge_softc *sc;
2217 struct ifnet *ifp;
2218 int i;
2219
2220 sc = (void *)dev;
2221 ifp = &sc->sc_ethercom.ec_if;
2222
2223 /* better way to do this? */
2224 for (i = 0; i < 5; i++)
2225 pci_write_config(dev, PCIR_MAPS + i * 4,
2226 sc->sc_saved_maps[i], 4);
2227 pci_write_config(dev, PCIR_BIOS, sc->sc_saved_biosaddr, 4);
2228 pci_write_config(dev, PCIR_INTLINE, sc->sc_saved_intline, 1);
2229 pci_write_config(dev, PCIR_CACHELNSZ, sc->sc_saved_cachelnsz, 1);
2230 pci_write_config(dev, PCIR_LATTIMER, sc->sc_saved_lattimer, 1);
2231
2232 /* reenable busmastering */
2233 pci_enable_busmaster(dev);
2234 pci_enable_io(dev, SYS_RES_MEMORY);
2235
2236 /* reinitialize interface if necessary */
2237 if (ifp->if_flags & IFF_UP)
2238 vge_init(sc);
2239
2240 sc->suspended = 0;
2241
2242 return 0;
2243 }
2244 #endif
2245
2246 /*
2247 * Stop all chip I/O so that the kernel's probe routines don't
2248 * get confused by errant DMAs when rebooting.
2249 */
2250 static void
2251 vge_shutdown(void *arg)
2252 {
2253 struct vge_softc *sc;
2254
2255 sc = arg;
2256 vge_stop(sc);
2257 }
2258