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