if_vge.c revision 1.22 1 /* $NetBSD: if_vge.c,v 1.22 2006/11/01 16:26:27 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.22 2006/11/01 16:26:27 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 panic("vge_newbuf");
1092 goto out;
1093 }
1094
1095 rxs->rxs_mbuf = m;
1096 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1097 BUS_DMASYNC_PREREAD);
1098
1099 rxd->rd_buflen =
1100 htole16(VGE_BUFLEN(map->dm_segs[0].ds_len) | VGE_RXDESC_I);
1101 rxd->rd_addrlo = htole32(VGE_ADDR_LO(map->dm_segs[0].ds_addr));
1102 rxd->rd_addrhi = htole16(VGE_ADDR_HI(map->dm_segs[0].ds_addr) & 0xFFFF);
1103 rxd->rd_sts = 0;
1104 rxd->rd_ctl = 0;
1105 VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1106
1107 /*
1108 * Note: the manual fails to document the fact that for
1109 * proper opration, the driver needs to replentish the RX
1110 * DMA ring 4 descriptors at a time (rather than one at a
1111 * time, like most chips). We can allocate the new buffers
1112 * but we should not set the OWN bits until we're ready
1113 * to hand back 4 of them in one shot.
1114 */
1115
1116 #define VGE_RXCHUNK 4
1117 sc->sc_rx_consumed++;
1118 if (sc->sc_rx_consumed == VGE_RXCHUNK) {
1119 for (i = idx; i != idx - VGE_RXCHUNK; i--) {
1120 KASSERT(i >= 0);
1121 sc->sc_rxdescs[i].rd_sts |= htole32(VGE_RDSTS_OWN);
1122 VGE_RXDESCSYNC(sc, i,
1123 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1124 }
1125 sc->sc_rx_consumed = 0;
1126 }
1127
1128 return 0;
1129 out:
1130 if (m_new != NULL)
1131 m_freem(m_new);
1132 return ENOMEM;
1133 }
1134
1135 #ifndef __NO_STRICT_ALIGNMENT
1136 static inline void
1137 vge_fixup_rx(struct mbuf *m)
1138 {
1139 int i;
1140 uint16_t *src, *dst;
1141
1142 src = mtod(m, uint16_t *);
1143 dst = src - 1;
1144
1145 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1146 *dst++ = *src++;
1147
1148 m->m_data -= ETHER_ALIGN;
1149 }
1150 #endif
1151
1152 /*
1153 * RX handler. We support the reception of jumbo frames that have
1154 * been fragmented across multiple 2K mbuf cluster buffers.
1155 */
1156 static void
1157 vge_rxeof(struct vge_softc *sc)
1158 {
1159 struct mbuf *m;
1160 struct ifnet *ifp;
1161 int idx, total_len, lim;
1162 struct vge_rxdesc *cur_rxd;
1163 struct vge_rxsoft *rxs;
1164 uint32_t rxstat, rxctl;
1165
1166 ifp = &sc->sc_ethercom.ec_if;
1167 lim = 0;
1168
1169 /* Invalidate the descriptor memory */
1170
1171 for (idx = sc->sc_rx_prodidx;; idx = VGE_NEXT_RXDESC(idx)) {
1172 cur_rxd = &sc->sc_rxdescs[idx];
1173
1174 VGE_RXDESCSYNC(sc, idx,
1175 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1176 rxstat = le32toh(cur_rxd->rd_sts);
1177 if ((rxstat & VGE_RDSTS_OWN) != 0) {
1178 VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1179 break;
1180 }
1181
1182 rxctl = le32toh(cur_rxd->rd_ctl);
1183 rxs = &sc->sc_rxsoft[idx];
1184 m = rxs->rxs_mbuf;
1185 total_len = (rxstat & VGE_RDSTS_BUFSIZ) >> 16;
1186
1187 /* Invalidate the RX mbuf and unload its map */
1188
1189 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap,
1190 0, rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1191 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1192
1193 /*
1194 * If the 'start of frame' bit is set, this indicates
1195 * either the first fragment in a multi-fragment receive,
1196 * or an intermediate fragment. Either way, we want to
1197 * accumulate the buffers.
1198 */
1199 if (rxstat & VGE_RXPKT_SOF) {
1200 m->m_len = MCLBYTES - VGE_RX_PAD;
1201 if (sc->sc_rx_mhead == NULL)
1202 sc->sc_rx_mhead = sc->sc_rx_mtail = m;
1203 else {
1204 m->m_flags &= ~M_PKTHDR;
1205 sc->sc_rx_mtail->m_next = m;
1206 sc->sc_rx_mtail = m;
1207 }
1208 vge_newbuf(sc, idx, NULL);
1209 continue;
1210 }
1211
1212 /*
1213 * Bad/error frames will have the RXOK bit cleared.
1214 * However, there's one error case we want to allow:
1215 * if a VLAN tagged frame arrives and the chip can't
1216 * match it against the CAM filter, it considers this
1217 * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
1218 * We don't want to drop the frame though: our VLAN
1219 * filtering is done in software.
1220 */
1221 if (!(rxstat & VGE_RDSTS_RXOK) && !(rxstat & VGE_RDSTS_VIDM)
1222 && !(rxstat & VGE_RDSTS_CSUMERR)) {
1223 ifp->if_ierrors++;
1224 /*
1225 * If this is part of a multi-fragment packet,
1226 * discard all the pieces.
1227 */
1228 if (sc->sc_rx_mhead != NULL) {
1229 m_freem(sc->sc_rx_mhead);
1230 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1231 }
1232 vge_newbuf(sc, idx, m);
1233 continue;
1234 }
1235
1236 /*
1237 * If allocating a replacement mbuf fails,
1238 * reload the current one.
1239 */
1240
1241 if (vge_newbuf(sc, idx, NULL)) {
1242 ifp->if_ierrors++;
1243 if (sc->sc_rx_mhead != NULL) {
1244 m_freem(sc->sc_rx_mhead);
1245 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1246 }
1247 vge_newbuf(sc, idx, m);
1248 continue;
1249 }
1250
1251 if (sc->sc_rx_mhead != NULL) {
1252 m->m_len = total_len % (MCLBYTES - VGE_RX_PAD);
1253 /*
1254 * Special case: if there's 4 bytes or less
1255 * in this buffer, the mbuf can be discarded:
1256 * the last 4 bytes is the CRC, which we don't
1257 * care about anyway.
1258 */
1259 if (m->m_len <= ETHER_CRC_LEN) {
1260 sc->sc_rx_mtail->m_len -=
1261 (ETHER_CRC_LEN - m->m_len);
1262 m_freem(m);
1263 } else {
1264 m->m_len -= ETHER_CRC_LEN;
1265 m->m_flags &= ~M_PKTHDR;
1266 sc->sc_rx_mtail->m_next = m;
1267 }
1268 m = sc->sc_rx_mhead;
1269 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1270 m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1271 } else
1272 m->m_pkthdr.len = m->m_len = total_len - ETHER_CRC_LEN;
1273
1274 #ifndef __NO_STRICT_ALIGNMENT
1275 vge_fixup_rx(m);
1276 #endif
1277 ifp->if_ipackets++;
1278 m->m_pkthdr.rcvif = ifp;
1279
1280 /* Do RX checksumming if enabled */
1281 if (ifp->if_csum_flags_rx & M_CSUM_IPv4) {
1282
1283 /* Check IP header checksum */
1284 if (rxctl & VGE_RDCTL_IPPKT)
1285 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1286 if ((rxctl & VGE_RDCTL_IPCSUMOK) == 0)
1287 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1288 }
1289
1290 if (ifp->if_csum_flags_rx & M_CSUM_TCPv4) {
1291 /* Check UDP checksum */
1292 if (rxctl & VGE_RDCTL_TCPPKT)
1293 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1294
1295 if ((rxctl & VGE_RDCTL_PROTOCSUMOK) == 0)
1296 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1297 }
1298
1299 if (ifp->if_csum_flags_rx & M_CSUM_UDPv4) {
1300 /* Check UDP checksum */
1301 if (rxctl & VGE_RDCTL_UDPPKT)
1302 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1303
1304 if ((rxctl & VGE_RDCTL_PROTOCSUMOK) == 0)
1305 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1306 }
1307
1308 if (rxstat & VGE_RDSTS_VTAG) {
1309 /*
1310 * We use bswap16() here because:
1311 * On LE machines, tag is stored in BE as stream data.
1312 * On BE machines, tag is stored in BE as stream data
1313 * but it was already swapped by le32toh() above.
1314 */
1315 VLAN_INPUT_TAG(ifp, m,
1316 bswap16(rxctl & VGE_RDCTL_VLANID), continue);
1317 }
1318
1319 #if NBPFILTER > 0
1320 /*
1321 * Handle BPF listeners.
1322 */
1323 if (ifp->if_bpf)
1324 bpf_mtap(ifp->if_bpf, m);
1325 #endif
1326
1327 (*ifp->if_input)(ifp, m);
1328
1329 lim++;
1330 if (lim == VGE_NRXDESC)
1331 break;
1332
1333 }
1334
1335 sc->sc_rx_prodidx = idx;
1336 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, lim);
1337 }
1338
1339 static void
1340 vge_txeof(struct vge_softc *sc)
1341 {
1342 struct ifnet *ifp;
1343 struct vge_txsoft *txs;
1344 uint32_t txstat;
1345 int idx;
1346
1347 ifp = &sc->sc_ethercom.ec_if;
1348 idx = sc->sc_tx_considx;
1349
1350 for (idx = sc->sc_tx_considx;
1351 idx != sc->sc_tx_prodidx;
1352 idx = VGE_NEXT_TXDESC(idx)) {
1353 VGE_TXDESCSYNC(sc, idx,
1354 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1355 txstat = le32toh(sc->sc_txdescs[idx].td_sts);
1356 if (txstat & VGE_TDSTS_OWN) {
1357 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1358 break;
1359 }
1360
1361 txs = &sc->sc_txsoft[idx];
1362 m_freem(txs->txs_mbuf);
1363 txs->txs_mbuf = NULL;
1364 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap, 0,
1365 txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1366 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1367 if (txstat & (VGE_TDSTS_EXCESSCOLL|VGE_TDSTS_COLL))
1368 ifp->if_collisions++;
1369 if (txstat & VGE_TDSTS_TXERR)
1370 ifp->if_oerrors++;
1371 else
1372 ifp->if_opackets++;
1373
1374 sc->sc_tx_free++;
1375 }
1376
1377 /* No changes made to the TX ring, so no flush needed */
1378
1379 if (idx != sc->sc_tx_considx) {
1380 sc->sc_tx_considx = idx;
1381 ifp->if_flags &= ~IFF_OACTIVE;
1382 }
1383
1384 /*
1385 * If not all descriptors have been released reaped yet,
1386 * reload the timer so that we will eventually get another
1387 * interrupt that will cause us to re-enter this routine.
1388 * This is done in case the transmitter has gone idle.
1389 */
1390 if (sc->sc_tx_free != VGE_NTXDESC)
1391 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1392 else
1393 ifp->if_timer = 0;
1394 }
1395
1396 static void
1397 vge_tick(void *xsc)
1398 {
1399 struct vge_softc *sc;
1400 struct ifnet *ifp;
1401 struct mii_data *mii;
1402 int s;
1403
1404 sc = xsc;
1405 ifp = &sc->sc_ethercom.ec_if;
1406 mii = &sc->sc_mii;
1407
1408 s = splnet();
1409
1410 callout_schedule(&sc->sc_timeout, hz);
1411
1412 mii_tick(mii);
1413 if (sc->sc_link) {
1414 if (!(mii->mii_media_status & IFM_ACTIVE))
1415 sc->sc_link = 0;
1416 } else {
1417 if (mii->mii_media_status & IFM_ACTIVE &&
1418 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1419 sc->sc_link = 1;
1420 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1421 vge_start(ifp);
1422 }
1423 }
1424
1425 splx(s);
1426 }
1427
1428 static int
1429 vge_intr(void *arg)
1430 {
1431 struct vge_softc *sc;
1432 struct ifnet *ifp;
1433 uint32_t status;
1434 int claim;
1435
1436 sc = arg;
1437 claim = 0;
1438 if (sc->sc_suspended) {
1439 return claim;
1440 }
1441
1442 ifp = &sc->sc_ethercom.ec_if;
1443
1444 if (!(ifp->if_flags & IFF_UP)) {
1445 return claim;
1446 }
1447
1448 /* Disable interrupts */
1449 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1450
1451 for (;;) {
1452
1453 status = CSR_READ_4(sc, VGE_ISR);
1454 /* If the card has gone away the read returns 0xffff. */
1455 if (status == 0xFFFFFFFF)
1456 break;
1457
1458 if (status) {
1459 claim = 1;
1460 CSR_WRITE_4(sc, VGE_ISR, status);
1461 }
1462
1463 if ((status & VGE_INTRS) == 0)
1464 break;
1465
1466 if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
1467 vge_rxeof(sc);
1468
1469 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1470 vge_rxeof(sc);
1471 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1472 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1473 }
1474
1475 if (status & (VGE_ISR_TXOK0|VGE_ISR_TIMER0))
1476 vge_txeof(sc);
1477
1478 if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL))
1479 vge_init(ifp);
1480
1481 if (status & VGE_ISR_LINKSTS)
1482 vge_tick(sc);
1483 }
1484
1485 /* Re-enable interrupts */
1486 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1487
1488 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1489 vge_start(ifp);
1490
1491 return claim;
1492 }
1493
1494 static int
1495 vge_encap(struct vge_softc *sc, struct mbuf *m_head, int idx)
1496 {
1497 struct vge_txsoft *txs;
1498 struct vge_txdesc *txd;
1499 struct vge_txfrag *f;
1500 struct mbuf *m_new;
1501 bus_dmamap_t map;
1502 int seg, error, flags;
1503 struct m_tag *mtag;
1504 size_t sz;
1505
1506 txd = &sc->sc_txdescs[idx];
1507
1508 /* If this descriptor is still owned by the chip, bail. */
1509 if (sc->sc_tx_free <= 2) {
1510 VGE_TXDESCSYNC(sc, idx,
1511 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1512 if (le32toh(txd->td_sts) & VGE_TDSTS_OWN) {
1513 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1514 return ENOBUFS;
1515 }
1516 }
1517
1518 txs = &sc->sc_txsoft[idx];
1519 map = txs->txs_dmamap;
1520 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m_head, BUS_DMA_NOWAIT);
1521
1522 /* If too many segments to map, coalesce */
1523 if (error == EFBIG ||
1524 (m_head->m_pkthdr.len < ETHER_PAD_LEN &&
1525 map->dm_nsegs == VGE_TX_FRAGS)) {
1526 m_new = m_defrag(m_head, M_DONTWAIT);
1527 if (m_new == NULL)
1528 return (error);
1529
1530 error = bus_dmamap_load_mbuf(sc->sc_dmat, map,
1531 m_new, BUS_DMA_NOWAIT);
1532 if (error) {
1533 m_freem(m_new);
1534 return error;
1535 }
1536
1537 m_head = m_new;
1538 } else if (error)
1539 return error;
1540
1541 txs->txs_mbuf = m_head;
1542
1543 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1544 BUS_DMASYNC_PREWRITE);
1545
1546 for (seg = 0, f = &txd->td_frag[0]; seg < map->dm_nsegs; seg++, f++) {
1547 f->tf_buflen = htole16(VGE_BUFLEN(map->dm_segs[seg].ds_len));
1548 f->tf_addrlo = htole32(VGE_ADDR_LO(map->dm_segs[seg].ds_addr));
1549 f->tf_addrhi = htole16(VGE_ADDR_HI(map->dm_segs[seg].ds_addr));
1550 }
1551
1552 /* Argh. This chip does not autopad short frames */
1553 sz = m_head->m_pkthdr.len;
1554 if (sz < ETHER_PAD_LEN) {
1555 f->tf_buflen = htole16(VGE_BUFLEN(ETHER_PAD_LEN - sz));
1556 f->tf_addrlo = htole32(VGE_ADDR_LO(VGE_CDPADADDR(sc)));
1557 f->tf_addrhi = htole16(VGE_ADDR_HI(VGE_CDPADADDR(sc)) & 0xFFFF);
1558 sz = ETHER_PAD_LEN;
1559 seg++;
1560 }
1561 VGE_TXFRAGSYNC(sc, idx, seg, BUS_DMASYNC_PREWRITE);
1562
1563 /*
1564 * When telling the chip how many segments there are, we
1565 * must use nsegs + 1 instead of just nsegs. Darned if I
1566 * know why.
1567 */
1568 seg++;
1569
1570 flags = 0;
1571 if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
1572 flags |= VGE_TDCTL_IPCSUM;
1573 if (m_head->m_pkthdr.csum_flags & M_CSUM_TCPv4)
1574 flags |= VGE_TDCTL_TCPCSUM;
1575 if (m_head->m_pkthdr.csum_flags & M_CSUM_UDPv4)
1576 flags |= VGE_TDCTL_UDPCSUM;
1577 txd->td_sts = htole32(sz << 16);
1578 txd->td_ctl = htole32(flags | (seg << 28) | VGE_TD_LS_NORM);
1579
1580 if (sz > ETHERMTU + ETHER_HDR_LEN)
1581 txd->td_ctl |= htole32(VGE_TDCTL_JUMBO);
1582
1583 /*
1584 * Set up hardware VLAN tagging.
1585 */
1586
1587 mtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m_head);
1588 if (mtag != NULL) {
1589 /*
1590 * No need htons() here since vge(4) chip assumes
1591 * that tags are written in little endian and
1592 * we already use htole32() here.
1593 */
1594 txd->td_ctl |= htole32(VLAN_TAG_VALUE(mtag) | VGE_TDCTL_VTAG);
1595 }
1596
1597 txd->td_sts |= htole32(VGE_TDSTS_OWN);
1598 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1599
1600 sc->sc_tx_free--;
1601
1602 return 0;
1603 }
1604
1605 /*
1606 * Main transmit routine.
1607 */
1608
1609 static void
1610 vge_start(struct ifnet *ifp)
1611 {
1612 struct vge_softc *sc;
1613 struct vge_txsoft *txs;
1614 struct mbuf *m_head;
1615 int idx, pidx, error;
1616
1617 sc = ifp->if_softc;
1618
1619 if (!sc->sc_link ||
1620 (ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) {
1621 return;
1622 }
1623
1624 m_head = NULL;
1625 idx = sc->sc_tx_prodidx;
1626
1627 pidx = (idx - 1) & VGE_NTXDESC_MASK;
1628
1629 /*
1630 * Loop through the send queue, setting up transmit descriptors
1631 * until we drain the queue, or use up all available transmit
1632 * descriptors.
1633 */
1634 for (;;) {
1635 /* Grab a packet off the queue. */
1636 IFQ_POLL(&ifp->if_snd, m_head);
1637 if (m_head == NULL)
1638 break;
1639
1640 txs = &sc->sc_txsoft[idx];
1641
1642 if (txs->txs_mbuf != NULL) {
1643 /*
1644 * Slot already used, stop for now.
1645 */
1646 ifp->if_flags |= IFF_OACTIVE;
1647 break;
1648 }
1649
1650 if ((error = vge_encap(sc, m_head, idx))) {
1651 if (error == EFBIG) {
1652 aprint_error("%s: Tx packet consumes too many "
1653 "DMA segments, dropping...\n",
1654 sc->sc_dev.dv_xname);
1655 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1656 m_freem(m_head);
1657 continue;
1658 }
1659
1660 /*
1661 * Short on resources, just stop for now.
1662 */
1663 if (error == ENOBUFS)
1664 ifp->if_flags |= IFF_OACTIVE;
1665 break;
1666 }
1667
1668 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1669
1670 /*
1671 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1672 */
1673
1674 sc->sc_txdescs[pidx].td_frag[0].tf_buflen |=
1675 htole16(VGE_TXDESC_Q);
1676 VGE_TXFRAGSYNC(sc, pidx, 1,
1677 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1678
1679 if (txs->txs_mbuf != m_head) {
1680 m_freem(m_head);
1681 m_head = txs->txs_mbuf;
1682 }
1683
1684 pidx = idx;
1685 idx = VGE_NEXT_TXDESC(idx);
1686
1687 /*
1688 * If there's a BPF listener, bounce a copy of this frame
1689 * to him.
1690 */
1691 #if NBPFILTER > 0
1692 if (ifp->if_bpf)
1693 bpf_mtap(ifp->if_bpf, m_head);
1694 #endif
1695 }
1696
1697 if (idx == sc->sc_tx_prodidx) {
1698 return;
1699 }
1700
1701 /* Issue a transmit command. */
1702 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
1703
1704 sc->sc_tx_prodidx = idx;
1705
1706 /*
1707 * Use the countdown timer for interrupt moderation.
1708 * 'TX done' interrupts are disabled. Instead, we reset the
1709 * countdown timer, which will begin counting until it hits
1710 * the value in the SSTIMER register, and then trigger an
1711 * interrupt. Each time we set the TIMER0_ENABLE bit, the
1712 * the timer count is reloaded. Only when the transmitter
1713 * is idle will the timer hit 0 and an interrupt fire.
1714 */
1715 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1716
1717 /*
1718 * Set a timeout in case the chip goes out to lunch.
1719 */
1720 ifp->if_timer = 5;
1721 }
1722
1723 static int
1724 vge_init(struct ifnet *ifp)
1725 {
1726 struct vge_softc *sc;
1727 struct vge_rxsoft *rxs;
1728 int i;
1729
1730 sc = ifp->if_softc;
1731
1732 /*
1733 * Cancel pending I/O and free all RX/TX buffers.
1734 */
1735 vge_stop(sc);
1736 vge_reset(sc);
1737
1738 /* Initialize the RX descriptors and mbufs. */
1739 memset(sc->sc_rxdescs, 0, sizeof(sc->sc_rxdescs));
1740 for (i = 0; i < VGE_NRXDESC; i++) {
1741 rxs = &sc->sc_rxsoft[i];
1742 if (rxs->rxs_mbuf) {
1743 m_freem(rxs->rxs_mbuf);
1744 rxs->rxs_mbuf = NULL;
1745 }
1746 if (rxs->rxs_dmamap)
1747 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1748 if (vge_newbuf(sc, i, NULL) == ENOBUFS) {
1749 aprint_error("%s: unable to allocate or map "
1750 "rx buffer\n", sc->sc_dev.dv_xname);
1751 return 1; /* XXX */
1752 }
1753 }
1754 sc->sc_rx_prodidx = 0;
1755 sc->sc_rx_consumed = 0;
1756 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1757
1758 /* Initialize the TX descriptors and mbufs. */
1759 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1760 bus_dmamap_sync(sc->sc_dmat, sc->sc_cddmamap,
1761 VGE_CDTXOFF(0), sizeof(sc->sc_txdescs),
1762 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1763 for (i = 0; i < VGE_NTXDESC; i++)
1764 sc->sc_txsoft[i].txs_mbuf = NULL;
1765
1766 sc->sc_tx_prodidx = 0;
1767 sc->sc_tx_considx = 0;
1768 sc->sc_tx_free = VGE_NTXDESC;
1769
1770 /* Set our station address */
1771 for (i = 0; i < ETHER_ADDR_LEN; i++)
1772 CSR_WRITE_1(sc, VGE_PAR0 + i, sc->sc_eaddr[i]);
1773
1774 /*
1775 * Set receive FIFO threshold. Also allow transmission and
1776 * reception of VLAN tagged frames.
1777 */
1778 CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
1779 CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES|VGE_VTAG_OPT2);
1780
1781 /* Set DMA burst length */
1782 CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
1783 CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
1784
1785 CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
1786
1787 /* Set collision backoff algorithm */
1788 CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
1789 VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
1790 CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
1791
1792 /* Disable LPSEL field in priority resolution */
1793 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
1794
1795 /*
1796 * Load the addresses of the DMA queues into the chip.
1797 * Note that we only use one transmit queue.
1798 */
1799
1800 CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0, VGE_ADDR_LO(VGE_CDTXADDR(sc, 0)));
1801 CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_NTXDESC - 1);
1802
1803 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, VGE_ADDR_LO(VGE_CDRXADDR(sc, 0)));
1804 CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_NRXDESC - 1);
1805 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_NRXDESC);
1806
1807 /* Enable and wake up the RX descriptor queue */
1808 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1809 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1810
1811 /* Enable the TX descriptor queue */
1812 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
1813
1814 /* Set up the receive filter -- allow large frames for VLANs. */
1815 CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST|VGE_RXCTL_RX_GIANT);
1816
1817 /* If we want promiscuous mode, set the allframes bit. */
1818 if (ifp->if_flags & IFF_PROMISC) {
1819 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
1820 }
1821
1822 /* Set capture broadcast bit to capture broadcast frames. */
1823 if (ifp->if_flags & IFF_BROADCAST) {
1824 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST);
1825 }
1826
1827 /* Set multicast bit to capture multicast frames. */
1828 if (ifp->if_flags & IFF_MULTICAST) {
1829 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST);
1830 }
1831
1832 /* Init the cam filter. */
1833 vge_cam_clear(sc);
1834
1835 /* Init the multicast filter. */
1836 vge_setmulti(sc);
1837
1838 /* Enable flow control */
1839
1840 CSR_WRITE_1(sc, VGE_CRS2, 0x8B);
1841
1842 /* Enable jumbo frame reception (if desired) */
1843
1844 /* Start the MAC. */
1845 CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
1846 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
1847 CSR_WRITE_1(sc, VGE_CRS0,
1848 VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
1849
1850 /*
1851 * Configure one-shot timer for microsecond
1852 * resulution and load it for 500 usecs.
1853 */
1854 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES);
1855 CSR_WRITE_2(sc, VGE_SSTIMER, 400);
1856
1857 /*
1858 * Configure interrupt moderation for receive. Enable
1859 * the holdoff counter and load it, and set the RX
1860 * suppression count to the number of descriptors we
1861 * want to allow before triggering an interrupt.
1862 * The holdoff timer is in units of 20 usecs.
1863 */
1864
1865 #ifdef notyet
1866 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE);
1867 /* Select the interrupt holdoff timer page. */
1868 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1869 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
1870 CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */
1871
1872 /* Enable use of the holdoff timer. */
1873 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
1874 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD);
1875
1876 /* Select the RX suppression threshold page. */
1877 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1878 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
1879 CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */
1880
1881 /* Restore the page select bits. */
1882 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1883 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
1884 #endif
1885
1886 #ifdef DEVICE_POLLING
1887 /*
1888 * Disable interrupts if we are polling.
1889 */
1890 if (ifp->if_flags & IFF_POLLING) {
1891 CSR_WRITE_4(sc, VGE_IMR, 0);
1892 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1893 } else /* otherwise ... */
1894 #endif /* DEVICE_POLLING */
1895 {
1896 /*
1897 * Enable interrupts.
1898 */
1899 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
1900 CSR_WRITE_4(sc, VGE_ISR, 0);
1901 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1902 }
1903
1904 mii_mediachg(&sc->sc_mii);
1905
1906 ifp->if_flags |= IFF_RUNNING;
1907 ifp->if_flags &= ~IFF_OACTIVE;
1908
1909 sc->sc_if_flags = 0;
1910 sc->sc_link = 0;
1911
1912 callout_schedule(&sc->sc_timeout, hz);
1913
1914 return 0;
1915 }
1916
1917 /*
1918 * Set media options.
1919 */
1920 static int
1921 vge_ifmedia_upd(struct ifnet *ifp)
1922 {
1923 struct vge_softc *sc;
1924
1925 sc = ifp->if_softc;
1926 mii_mediachg(&sc->sc_mii);
1927
1928 return 0;
1929 }
1930
1931 /*
1932 * Report current media status.
1933 */
1934 static void
1935 vge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1936 {
1937 struct vge_softc *sc;
1938 struct mii_data *mii;
1939
1940 sc = ifp->if_softc;
1941 mii = &sc->sc_mii;
1942
1943 mii_pollstat(mii);
1944 ifmr->ifm_active = mii->mii_media_active;
1945 ifmr->ifm_status = mii->mii_media_status;
1946 }
1947
1948 static void
1949 vge_miibus_statchg(struct device *self)
1950 {
1951 struct vge_softc *sc;
1952 struct mii_data *mii;
1953 struct ifmedia_entry *ife;
1954
1955 sc = (void *)self;
1956 mii = &sc->sc_mii;
1957 ife = mii->mii_media.ifm_cur;
1958 /*
1959 * If the user manually selects a media mode, we need to turn
1960 * on the forced MAC mode bit in the DIAGCTL register. If the
1961 * user happens to choose a full duplex mode, we also need to
1962 * set the 'force full duplex' bit. This applies only to
1963 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
1964 * mode is disabled, and in 1000baseT mode, full duplex is
1965 * always implied, so we turn on the forced mode bit but leave
1966 * the FDX bit cleared.
1967 */
1968
1969 switch (IFM_SUBTYPE(ife->ifm_media)) {
1970 case IFM_AUTO:
1971 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1972 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1973 break;
1974 case IFM_1000_T:
1975 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1976 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1977 break;
1978 case IFM_100_TX:
1979 case IFM_10_T:
1980 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1981 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
1982 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1983 } else {
1984 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1985 }
1986 break;
1987 default:
1988 aprint_error("%s: unknown media type: %x\n",
1989 sc->sc_dev.dv_xname,
1990 IFM_SUBTYPE(ife->ifm_media));
1991 break;
1992 }
1993 }
1994
1995 static int
1996 vge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1997 {
1998 struct vge_softc *sc;
1999 struct ifreq *ifr;
2000 struct mii_data *mii;
2001 int s, error;
2002
2003 sc = ifp->if_softc;
2004 ifr = (struct ifreq *)data;
2005 error = 0;
2006
2007 s = splnet();
2008
2009 switch (command) {
2010 case SIOCSIFMTU:
2011 if (ifr->ifr_mtu > VGE_JUMBO_MTU)
2012 error = EINVAL;
2013 ifp->if_mtu = ifr->ifr_mtu;
2014 break;
2015 case SIOCSIFFLAGS:
2016 if (ifp->if_flags & IFF_UP) {
2017 if (ifp->if_flags & IFF_RUNNING &&
2018 ifp->if_flags & IFF_PROMISC &&
2019 !(sc->sc_if_flags & IFF_PROMISC)) {
2020 CSR_SETBIT_1(sc, VGE_RXCTL,
2021 VGE_RXCTL_RX_PROMISC);
2022 vge_setmulti(sc);
2023 } else if (ifp->if_flags & IFF_RUNNING &&
2024 !(ifp->if_flags & IFF_PROMISC) &&
2025 sc->sc_if_flags & IFF_PROMISC) {
2026 CSR_CLRBIT_1(sc, VGE_RXCTL,
2027 VGE_RXCTL_RX_PROMISC);
2028 vge_setmulti(sc);
2029 } else
2030 vge_init(ifp);
2031 } else {
2032 if (ifp->if_flags & IFF_RUNNING)
2033 vge_stop(sc);
2034 }
2035 sc->sc_if_flags = ifp->if_flags;
2036 break;
2037 case SIOCADDMULTI:
2038 case SIOCDELMULTI:
2039 error = (command == SIOCADDMULTI) ?
2040 ether_addmulti(ifr, &sc->sc_ethercom) :
2041 ether_delmulti(ifr, &sc->sc_ethercom);
2042
2043 if (error == ENETRESET) {
2044 /*
2045 * Multicast list has changed; set the hardware filter
2046 * accordingly.
2047 */
2048 if (ifp->if_flags & IFF_RUNNING)
2049 vge_setmulti(sc);
2050 error = 0;
2051 }
2052 break;
2053 case SIOCGIFMEDIA:
2054 case SIOCSIFMEDIA:
2055 mii = &sc->sc_mii;
2056 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2057 break;
2058 default:
2059 error = ether_ioctl(ifp, command, data);
2060 break;
2061 }
2062
2063 splx(s);
2064 return error;
2065 }
2066
2067 static void
2068 vge_watchdog(struct ifnet *ifp)
2069 {
2070 struct vge_softc *sc;
2071 int s;
2072
2073 sc = ifp->if_softc;
2074 s = splnet();
2075 aprint_error("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
2076 ifp->if_oerrors++;
2077
2078 vge_txeof(sc);
2079 vge_rxeof(sc);
2080
2081 vge_init(ifp);
2082
2083 splx(s);
2084 }
2085
2086 /*
2087 * Stop the adapter and free any mbufs allocated to the
2088 * RX and TX lists.
2089 */
2090 static void
2091 vge_stop(struct vge_softc *sc)
2092 {
2093 struct ifnet *ifp;
2094 struct vge_txsoft *txs;
2095 struct vge_rxsoft *rxs;
2096 int i, s;
2097
2098 ifp = &sc->sc_ethercom.ec_if;
2099
2100 s = splnet();
2101 ifp->if_timer = 0;
2102
2103 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2104 #ifdef DEVICE_POLLING
2105 ether_poll_deregister(ifp);
2106 #endif /* DEVICE_POLLING */
2107
2108 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2109 CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
2110 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2111 CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
2112 CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
2113 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
2114
2115 if (sc->sc_rx_mhead != NULL) {
2116 m_freem(sc->sc_rx_mhead);
2117 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
2118 }
2119
2120 /* Free the TX list buffers. */
2121
2122 for (i = 0; i < VGE_NTXDESC; i++) {
2123 txs = &sc->sc_txsoft[i];
2124 if (txs->txs_mbuf != NULL) {
2125 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2126 m_freem(txs->txs_mbuf);
2127 txs->txs_mbuf = NULL;
2128 }
2129 }
2130
2131 /* Free the RX list buffers. */
2132
2133 for (i = 0; i < VGE_NRXDESC; i++) {
2134 rxs = &sc->sc_rxsoft[i];
2135 if (rxs->rxs_mbuf != NULL) {
2136 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2137 m_freem(rxs->rxs_mbuf);
2138 rxs->rxs_mbuf = NULL;
2139 }
2140 }
2141
2142 splx(s);
2143 }
2144
2145 #if VGE_POWER_MANAGEMENT
2146 /*
2147 * Device suspend routine. Stop the interface and save some PCI
2148 * settings in case the BIOS doesn't restore them properly on
2149 * resume.
2150 */
2151 static int
2152 vge_suspend(struct device *dev)
2153 {
2154 struct vge_softc *sc;
2155 int i;
2156
2157 sc = device_get_softc(dev);
2158
2159 vge_stop(sc);
2160
2161 for (i = 0; i < 5; i++)
2162 sc->sc_saved_maps[i] =
2163 pci_read_config(dev, PCIR_MAPS + i * 4, 4);
2164 sc->sc_saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
2165 sc->sc_saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
2166 sc->sc_saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
2167 sc->sc_saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
2168
2169 sc->suspended = 1;
2170
2171 return 0;
2172 }
2173
2174 /*
2175 * Device resume routine. Restore some PCI settings in case the BIOS
2176 * doesn't, re-enable busmastering, and restart the interface if
2177 * appropriate.
2178 */
2179 static int
2180 vge_resume(struct device *dev)
2181 {
2182 struct vge_softc *sc;
2183 struct ifnet *ifp;
2184 int i;
2185
2186 sc = (void *)dev;
2187 ifp = &sc->sc_ethercom.ec_if;
2188
2189 /* better way to do this? */
2190 for (i = 0; i < 5; i++)
2191 pci_write_config(dev, PCIR_MAPS + i * 4,
2192 sc->sc_saved_maps[i], 4);
2193 pci_write_config(dev, PCIR_BIOS, sc->sc_saved_biosaddr, 4);
2194 pci_write_config(dev, PCIR_INTLINE, sc->sc_saved_intline, 1);
2195 pci_write_config(dev, PCIR_CACHELNSZ, sc->sc_saved_cachelnsz, 1);
2196 pci_write_config(dev, PCIR_LATTIMER, sc->sc_saved_lattimer, 1);
2197
2198 /* reenable busmastering */
2199 pci_enable_busmaster(dev);
2200 pci_enable_io(dev, SYS_RES_MEMORY);
2201
2202 /* reinitialize interface if necessary */
2203 if (ifp->if_flags & IFF_UP)
2204 vge_init(sc);
2205
2206 sc->suspended = 0;
2207
2208 return 0;
2209 }
2210 #endif
2211
2212 /*
2213 * Stop all chip I/O so that the kernel's probe routines don't
2214 * get confused by errant DMAs when rebooting.
2215 */
2216 static void
2217 vge_shutdown(void *arg)
2218 {
2219 struct vge_softc *sc;
2220
2221 sc = arg;
2222 vge_stop(sc);
2223 }
2224