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