if_vge.c revision 1.12 1 /* $NetBSD: if_vge.c,v 1.12 2006/10/14 15:53:23 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.12 2006/10/14 15:53:23 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 uint8_t *eaddr;
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 eaddr = sc->vge_eaddr;
905 val = vge_read_eeprom(sc, VGE_EE_EADDR + 0);
906 eaddr[0] = val & 0xff;
907 eaddr[1] = val >> 8;
908 val = vge_read_eeprom(sc, VGE_EE_EADDR + 1);
909 eaddr[2] = val & 0xff;
910 eaddr[3] = val >> 8;
911 val = vge_read_eeprom(sc, VGE_EE_EADDR + 2);
912 eaddr[4] = val & 0xff;
913 eaddr[5] = val >> 8;
914
915 printf("%s: Ethernet address: %s\n", sc->sc_dev.dv_xname,
916 ether_sprintf(eaddr));
917
918 /*
919 * Use the 32bit tag. Hardware supports 48bit physical addresses,
920 * but we don't use that for now.
921 */
922 sc->vge_dmat = pa->pa_dmat;
923
924 if (vge_allocmem(sc))
925 return;
926
927 ifp = &sc->sc_ethercom.ec_if;
928 ifp->if_softc = sc;
929 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
930 ifp->if_mtu = ETHERMTU;
931 ifp->if_baudrate = IF_Gbps(1);
932 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
933 ifp->if_ioctl = vge_ioctl;
934 ifp->if_start = vge_start;
935
936 /*
937 * We can support 802.1Q VLAN-sized frames and jumbo
938 * Ethernet frames.
939 */
940 sc->sc_ethercom.ec_capabilities |=
941 ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU |
942 ETHERCAP_VLAN_HWTAGGING;
943
944 /*
945 * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
946 */
947 ifp->if_capabilities |=
948 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
949 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
950 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
951
952 #ifdef DEVICE_POLLING
953 #ifdef IFCAP_POLLING
954 ifp->if_capabilities |= IFCAP_POLLING;
955 #endif
956 #endif
957 ifp->if_watchdog = vge_watchdog;
958 ifp->if_init = vge_init;
959 IFQ_SET_MAXLEN(&ifp->if_snd, max(VGE_IFQ_MAXLEN, IFQ_MAXLEN));
960
961 /*
962 * Initialize our media structures and probe the MII.
963 */
964 sc->sc_mii.mii_ifp = ifp;
965 sc->sc_mii.mii_readreg = vge_miibus_readreg;
966 sc->sc_mii.mii_writereg = vge_miibus_writereg;
967 sc->sc_mii.mii_statchg = vge_miibus_statchg;
968 ifmedia_init(&sc->sc_mii.mii_media, 0, vge_ifmedia_upd,
969 vge_ifmedia_sts);
970 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
971 MII_OFFSET_ANY, MIIF_DOPAUSE);
972 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
973 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
974 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
975 } else
976 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
977
978 /*
979 * Attach the interface.
980 */
981 if_attach(ifp);
982 ether_ifattach(ifp, eaddr);
983
984 callout_init(&sc->vge_timeout);
985 callout_setfunc(&sc->vge_timeout, vge_tick, sc);
986
987 /*
988 * Make sure the interface is shutdown during reboot.
989 */
990 if (shutdownhook_establish(vge_shutdown, sc) == NULL) {
991 printf("%s: WARNING: unable to establish shutdown hook\n",
992 sc->sc_dev.dv_xname);
993 }
994 }
995
996 static int
997 vge_newbuf(sc, idx, m)
998 struct vge_softc *sc;
999 int idx;
1000 struct mbuf *m;
1001 {
1002 struct mbuf *n = NULL;
1003 int i, error;
1004
1005 if (m == NULL) {
1006 n = m_gethdr(M_DONTWAIT, MT_DATA);
1007 if (n == NULL)
1008 return (ENOBUFS);
1009
1010 m_clget(n, M_DONTWAIT);
1011 if ((n->m_flags & M_EXT) == 0) {
1012 m_freem(n);
1013 return (ENOBUFS);
1014 }
1015
1016 m = n;
1017 } else
1018 m->m_data = m->m_ext.ext_buf;
1019
1020
1021 #ifdef VGE_FIXUP_RX
1022 /*
1023 * This is part of an evil trick to deal with non-x86 platforms.
1024 * The VIA chip requires RX buffers to be aligned on 32-bit
1025 * boundaries, but that will hose non-x86 machines. To get around
1026 * this, we leave some empty space at the start of each buffer
1027 * and for non-x86 hosts, we copy the buffer back two bytes
1028 * to achieve word alignment. This is slightly more efficient
1029 * than allocating a new buffer, copying the contents, and
1030 * discarding the old buffer.
1031 */
1032 m->m_len = m->m_pkthdr.len = MCLBYTES - VGE_ETHER_ALIGN;
1033 m_adj(m, VGE_ETHER_ALIGN);
1034 #else
1035 m->m_len = m->m_pkthdr.len = MCLBYTES;
1036 #endif
1037
1038 error = bus_dmamap_load_mbuf(sc->vge_dmat,
1039 sc->vge_ldata.vge_rx_dmamap[idx], m, BUS_DMA_NOWAIT);
1040 if (error || vge_dma_map_rx_desc(sc, idx)) {
1041 if (n != NULL)
1042 m_freem(n);
1043 return (ENOMEM);
1044 }
1045
1046 /*
1047 * Note: the manual fails to document the fact that for
1048 * proper opration, the driver needs to replentish the RX
1049 * DMA ring 4 descriptors at a time (rather than one at a
1050 * time, like most chips). We can allocate the new buffers
1051 * but we should not set the OWN bits until we're ready
1052 * to hand back 4 of them in one shot.
1053 */
1054
1055 #define VGE_RXCHUNK 4
1056 sc->vge_rx_consumed++;
1057 if (sc->vge_rx_consumed == VGE_RXCHUNK) {
1058 for (i = idx; i != idx - sc->vge_rx_consumed; i--)
1059 sc->vge_ldata.vge_rx_list[i].vge_sts |=
1060 htole32(VGE_RDSTS_OWN);
1061 sc->vge_rx_consumed = 0;
1062 }
1063
1064 sc->vge_ldata.vge_rx_mbuf[idx] = m;
1065
1066 bus_dmamap_sync(sc->vge_dmat,
1067 sc->vge_ldata.vge_rx_dmamap[idx],
1068 0, sc->vge_ldata.vge_rx_dmamap[idx]->dm_mapsize,
1069 BUS_DMASYNC_PREREAD);
1070
1071 return (0);
1072 }
1073
1074 static int
1075 vge_tx_list_init(sc)
1076 struct vge_softc *sc;
1077 {
1078 bzero ((char *)sc->vge_ldata.vge_tx_list, VGE_TX_LIST_SZ);
1079 bzero ((char *)&sc->vge_ldata.vge_tx_mbuf,
1080 (VGE_TX_DESC_CNT * sizeof(struct mbuf *)));
1081
1082 bus_dmamap_sync(sc->vge_dmat,
1083 sc->vge_ldata.vge_tx_list_map,
1084 0, sc->vge_ldata.vge_tx_list_map->dm_mapsize,
1085 BUS_DMASYNC_PREWRITE);
1086
1087 sc->vge_ldata.vge_tx_prodidx = 0;
1088 sc->vge_ldata.vge_tx_considx = 0;
1089 sc->vge_ldata.vge_tx_free = VGE_TX_DESC_CNT;
1090
1091 return (0);
1092 }
1093
1094 static int
1095 vge_rx_list_init(sc)
1096 struct vge_softc *sc;
1097 {
1098 int i;
1099
1100 bzero ((char *)sc->vge_ldata.vge_rx_list, VGE_RX_LIST_SZ);
1101 bzero ((char *)&sc->vge_ldata.vge_rx_mbuf,
1102 (VGE_RX_DESC_CNT * sizeof(struct mbuf *)));
1103
1104 sc->vge_rx_consumed = 0;
1105
1106 for (i = 0; i < VGE_RX_DESC_CNT; i++) {
1107 if (vge_newbuf(sc, i, NULL) == ENOBUFS)
1108 return (ENOBUFS);
1109 }
1110
1111 /* Flush the RX descriptors */
1112
1113 bus_dmamap_sync(sc->vge_dmat,
1114 sc->vge_ldata.vge_rx_list_map,
1115 0, sc->vge_ldata.vge_rx_list_map->dm_mapsize,
1116 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1117
1118 sc->vge_ldata.vge_rx_prodidx = 0;
1119 sc->vge_rx_consumed = 0;
1120 sc->vge_head = sc->vge_tail = NULL;
1121
1122 return (0);
1123 }
1124
1125 #ifdef VGE_FIXUP_RX
1126 static inline void
1127 vge_fixup_rx(m)
1128 struct mbuf *m;
1129 {
1130 int i;
1131 uint16_t *src, *dst;
1132
1133 src = mtod(m, uint16_t *);
1134 dst = src - 1;
1135
1136 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1137 *dst++ = *src++;
1138
1139 m->m_data -= ETHER_ALIGN;
1140
1141 return;
1142 }
1143 #endif
1144
1145 /*
1146 * RX handler. We support the reception of jumbo frames that have
1147 * been fragmented across multiple 2K mbuf cluster buffers.
1148 */
1149 static void
1150 vge_rxeof(sc)
1151 struct vge_softc *sc;
1152 {
1153 struct mbuf *m;
1154 struct ifnet *ifp;
1155 int i, total_len;
1156 int lim = 0;
1157 struct vge_rx_desc *cur_rx;
1158 u_int32_t rxstat, rxctl;
1159
1160 VGE_LOCK_ASSERT(sc);
1161 ifp = &sc->sc_ethercom.ec_if;
1162 i = sc->vge_ldata.vge_rx_prodidx;
1163
1164 /* Invalidate the descriptor memory */
1165
1166 bus_dmamap_sync(sc->vge_dmat,
1167 sc->vge_ldata.vge_rx_list_map,
1168 0, sc->vge_ldata.vge_rx_list_map->dm_mapsize,
1169 BUS_DMASYNC_POSTREAD);
1170
1171 while (!VGE_OWN(&sc->vge_ldata.vge_rx_list[i])) {
1172
1173 #ifdef DEVICE_POLLING
1174 if (ifp->if_flags & IFF_POLLING) {
1175 if (sc->rxcycles <= 0)
1176 break;
1177 sc->rxcycles--;
1178 }
1179 #endif /* DEVICE_POLLING */
1180
1181 cur_rx = &sc->vge_ldata.vge_rx_list[i];
1182 m = sc->vge_ldata.vge_rx_mbuf[i];
1183 total_len = VGE_RXBYTES(cur_rx);
1184 rxstat = le32toh(cur_rx->vge_sts);
1185 rxctl = le32toh(cur_rx->vge_ctl);
1186
1187 /* Invalidate the RX mbuf and unload its map */
1188
1189 bus_dmamap_sync(sc->vge_dmat,
1190 sc->vge_ldata.vge_rx_dmamap[i],
1191 0, sc->vge_ldata.vge_rx_dmamap[i]->dm_mapsize,
1192 BUS_DMASYNC_POSTWRITE);
1193 bus_dmamap_unload(sc->vge_dmat,
1194 sc->vge_ldata.vge_rx_dmamap[i]);
1195
1196 /*
1197 * If the 'start of frame' bit is set, this indicates
1198 * either the first fragment in a multi-fragment receive,
1199 * or an intermediate fragment. Either way, we want to
1200 * accumulate the buffers.
1201 */
1202 if (rxstat & VGE_RXPKT_SOF) {
1203 m->m_len = MCLBYTES - VGE_ETHER_ALIGN;
1204 if (sc->vge_head == NULL)
1205 sc->vge_head = sc->vge_tail = m;
1206 else {
1207 m->m_flags &= ~M_PKTHDR;
1208 sc->vge_tail->m_next = m;
1209 sc->vge_tail = m;
1210 }
1211 vge_newbuf(sc, i, NULL);
1212 VGE_RX_DESC_INC(i);
1213 continue;
1214 }
1215
1216 /*
1217 * Bad/error frames will have the RXOK bit cleared.
1218 * However, there's one error case we want to allow:
1219 * if a VLAN tagged frame arrives and the chip can't
1220 * match it against the CAM filter, it considers this
1221 * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
1222 * We don't want to drop the frame though: our VLAN
1223 * filtering is done in software.
1224 */
1225 if (!(rxstat & VGE_RDSTS_RXOK) && !(rxstat & VGE_RDSTS_VIDM)
1226 && !(rxstat & VGE_RDSTS_CSUMERR)) {
1227 ifp->if_ierrors++;
1228 /*
1229 * If this is part of a multi-fragment packet,
1230 * discard all the pieces.
1231 */
1232 if (sc->vge_head != NULL) {
1233 m_freem(sc->vge_head);
1234 sc->vge_head = sc->vge_tail = NULL;
1235 }
1236 vge_newbuf(sc, i, m);
1237 VGE_RX_DESC_INC(i);
1238 continue;
1239 }
1240
1241 /*
1242 * If allocating a replacement mbuf fails,
1243 * reload the current one.
1244 */
1245
1246 if (vge_newbuf(sc, i, NULL)) {
1247 ifp->if_ierrors++;
1248 if (sc->vge_head != NULL) {
1249 m_freem(sc->vge_head);
1250 sc->vge_head = sc->vge_tail = NULL;
1251 }
1252 vge_newbuf(sc, i, m);
1253 VGE_RX_DESC_INC(i);
1254 continue;
1255 }
1256
1257 VGE_RX_DESC_INC(i);
1258
1259 if (sc->vge_head != NULL) {
1260 m->m_len = total_len % (MCLBYTES - VGE_ETHER_ALIGN);
1261 /*
1262 * Special case: if there's 4 bytes or less
1263 * in this buffer, the mbuf can be discarded:
1264 * the last 4 bytes is the CRC, which we don't
1265 * care about anyway.
1266 */
1267 if (m->m_len <= ETHER_CRC_LEN) {
1268 sc->vge_tail->m_len -=
1269 (ETHER_CRC_LEN - m->m_len);
1270 m_freem(m);
1271 } else {
1272 m->m_len -= ETHER_CRC_LEN;
1273 m->m_flags &= ~M_PKTHDR;
1274 sc->vge_tail->m_next = m;
1275 }
1276 m = sc->vge_head;
1277 sc->vge_head = sc->vge_tail = NULL;
1278 m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1279 } else
1280 m->m_pkthdr.len = m->m_len =
1281 (total_len - ETHER_CRC_LEN);
1282
1283 #ifdef VGE_FIXUP_RX
1284 vge_fixup_rx(m);
1285 #endif
1286 ifp->if_ipackets++;
1287 m->m_pkthdr.rcvif = ifp;
1288
1289 /* Do RX checksumming if enabled */
1290 if (ifp->if_csum_flags_rx & M_CSUM_IPv4) {
1291
1292 /* Check IP header checksum */
1293 if (rxctl & VGE_RDCTL_IPPKT)
1294 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1295 if ((rxctl & VGE_RDCTL_IPCSUMOK) == 0)
1296 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1297 }
1298
1299 if (ifp->if_csum_flags_rx & M_CSUM_TCPv4) {
1300 /* Check UDP checksum */
1301 if (rxctl & VGE_RDCTL_TCPPKT)
1302 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1303
1304 if ((rxctl & VGE_RDCTL_PROTOCSUMOK) == 0)
1305 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1306 }
1307
1308 if (ifp->if_csum_flags_rx & M_CSUM_UDPv4) {
1309 /* Check UDP checksum */
1310 if (rxctl & VGE_RDCTL_UDPPKT)
1311 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1312
1313 if ((rxctl & VGE_RDCTL_PROTOCSUMOK) == 0)
1314 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1315 }
1316
1317 if (rxstat & VGE_RDSTS_VTAG)
1318 VLAN_INPUT_TAG(ifp, m,
1319 ntohs((rxctl & VGE_RDCTL_VLANID)), continue);
1320
1321 #if NBPFILTER > 0
1322 /*
1323 * Handle BPF listeners.
1324 */
1325 if (ifp->if_bpf)
1326 bpf_mtap(ifp->if_bpf, m);
1327 #endif
1328
1329 VGE_UNLOCK(sc);
1330 (*ifp->if_input)(ifp, m);
1331 VGE_LOCK(sc);
1332
1333 lim++;
1334 if (lim == VGE_RX_DESC_CNT)
1335 break;
1336
1337 }
1338
1339 /* Flush the RX DMA ring */
1340
1341 bus_dmamap_sync(sc->vge_dmat,
1342 sc->vge_ldata.vge_rx_list_map,
1343 0, sc->vge_ldata.vge_rx_list_map->dm_mapsize,
1344 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1345
1346 sc->vge_ldata.vge_rx_prodidx = i;
1347 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, lim);
1348
1349
1350 return;
1351 }
1352
1353 static void
1354 vge_txeof(sc)
1355 struct vge_softc *sc;
1356 {
1357 struct ifnet *ifp;
1358 u_int32_t txstat;
1359 int idx;
1360
1361 ifp = &sc->sc_ethercom.ec_if;
1362 idx = sc->vge_ldata.vge_tx_considx;
1363
1364 /* Invalidate the TX descriptor list */
1365
1366 bus_dmamap_sync(sc->vge_dmat,
1367 sc->vge_ldata.vge_tx_list_map,
1368 0, sc->vge_ldata.vge_tx_list_map->dm_mapsize,
1369 BUS_DMASYNC_POSTREAD);
1370
1371 while (idx != sc->vge_ldata.vge_tx_prodidx) {
1372
1373 txstat = le32toh(sc->vge_ldata.vge_tx_list[idx].vge_sts);
1374 if (txstat & VGE_TDSTS_OWN)
1375 break;
1376
1377 m_freem(sc->vge_ldata.vge_tx_mbuf[idx]);
1378 sc->vge_ldata.vge_tx_mbuf[idx] = NULL;
1379 bus_dmamap_unload(sc->vge_dmat,
1380 sc->vge_ldata.vge_tx_dmamap[idx]);
1381 if (txstat & (VGE_TDSTS_EXCESSCOLL|VGE_TDSTS_COLL))
1382 ifp->if_collisions++;
1383 if (txstat & VGE_TDSTS_TXERR)
1384 ifp->if_oerrors++;
1385 else
1386 ifp->if_opackets++;
1387
1388 sc->vge_ldata.vge_tx_free++;
1389 VGE_TX_DESC_INC(idx);
1390 }
1391
1392 /* No changes made to the TX ring, so no flush needed */
1393
1394 if (idx != sc->vge_ldata.vge_tx_considx) {
1395 sc->vge_ldata.vge_tx_considx = idx;
1396 ifp->if_flags &= ~IFF_OACTIVE;
1397 ifp->if_timer = 0;
1398 }
1399
1400 /*
1401 * If not all descriptors have been released reaped yet,
1402 * reload the timer so that we will eventually get another
1403 * interrupt that will cause us to re-enter this routine.
1404 * This is done in case the transmitter has gone idle.
1405 */
1406 if (sc->vge_ldata.vge_tx_free != VGE_TX_DESC_CNT) {
1407 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1408 }
1409
1410 return;
1411 }
1412
1413 static void
1414 vge_tick(xsc)
1415 void *xsc;
1416 {
1417 struct vge_softc *sc = xsc;
1418 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1419 struct mii_data *mii = &sc->sc_mii;
1420 int s;
1421
1422 s = splnet();
1423
1424 VGE_LOCK(sc);
1425
1426 callout_schedule(&sc->vge_timeout, hz);
1427
1428 mii_tick(mii);
1429 if (sc->vge_link) {
1430 if (!(mii->mii_media_status & IFM_ACTIVE))
1431 sc->vge_link = 0;
1432 } else {
1433 if (mii->mii_media_status & IFM_ACTIVE &&
1434 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1435 sc->vge_link = 1;
1436 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1437 vge_start(ifp);
1438 }
1439 }
1440
1441 VGE_UNLOCK(sc);
1442
1443 splx(s);
1444 }
1445
1446 #ifdef DEVICE_POLLING
1447 static void
1448 vge_poll (struct ifnet *ifp, enum poll_cmd cmd, int count)
1449 {
1450 struct vge_softc *sc = ifp->if_softc;
1451
1452 VGE_LOCK(sc);
1453 #ifdef IFCAP_POLLING
1454 if (!(ifp->if_capenable & IFCAP_POLLING)) {
1455 ether_poll_deregister(ifp);
1456 cmd = POLL_DEREGISTER;
1457 }
1458 #endif
1459 if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
1460 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
1461 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
1462 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1463 goto done;
1464 }
1465
1466 sc->rxcycles = count;
1467 vge_rxeof(sc);
1468 vge_txeof(sc);
1469
1470 #if __FreeBSD_version < 502114
1471 if (ifp->if_snd.ifq_head != NULL)
1472 #else
1473 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1474 #endif
1475 taskqueue_enqueue(taskqueue_swi, &sc->vge_txtask);
1476
1477 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1478 u_int32_t status;
1479 status = CSR_READ_4(sc, VGE_ISR);
1480 if (status == 0xFFFFFFFF)
1481 goto done;
1482 if (status)
1483 CSR_WRITE_4(sc, VGE_ISR, status);
1484
1485 /*
1486 * XXX check behaviour on receiver stalls.
1487 */
1488
1489 if (status & VGE_ISR_TXDMA_STALL ||
1490 status & VGE_ISR_RXDMA_STALL)
1491 vge_init(sc);
1492
1493 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1494 vge_rxeof(sc);
1495 ifp->if_ierrors++;
1496 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1497 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1498 }
1499 }
1500 done:
1501 VGE_UNLOCK(sc);
1502 }
1503 #endif /* DEVICE_POLLING */
1504
1505 static int
1506 vge_intr(arg)
1507 void *arg;
1508 {
1509 struct vge_softc *sc = arg;
1510 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1511 u_int32_t status;
1512 int claim = 0;
1513
1514 if (sc->suspended) {
1515 return claim;
1516 }
1517
1518 VGE_LOCK(sc);
1519
1520 if (!(ifp->if_flags & IFF_UP)) {
1521 VGE_UNLOCK(sc);
1522 return claim;
1523 }
1524
1525 #ifdef DEVICE_POLLING
1526 if (ifp->if_flags & IFF_POLLING)
1527 goto done;
1528 if (
1529 #ifdef IFCAP_POLLING
1530 (ifp->if_capenable & IFCAP_POLLING) &&
1531 #endif
1532 ether_poll_register(vge_poll, ifp)) { /* ok, disable interrupts */
1533 CSR_WRITE_4(sc, VGE_IMR, 0);
1534 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1535 vge_poll(ifp, 0, 1);
1536 goto done;
1537 }
1538
1539 #endif /* DEVICE_POLLING */
1540
1541 /* Disable interrupts */
1542 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1543
1544 for (;;) {
1545
1546 status = CSR_READ_4(sc, VGE_ISR);
1547 /* If the card has gone away the read returns 0xffff. */
1548 if (status == 0xFFFFFFFF)
1549 break;
1550
1551 if (status) {
1552 claim = 1;
1553 CSR_WRITE_4(sc, VGE_ISR, status);
1554 }
1555
1556 if ((status & VGE_INTRS) == 0)
1557 break;
1558
1559 if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
1560 vge_rxeof(sc);
1561
1562 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1563 vge_rxeof(sc);
1564 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1565 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1566 }
1567
1568 if (status & (VGE_ISR_TXOK0|VGE_ISR_TIMER0))
1569 vge_txeof(sc);
1570
1571 if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL))
1572 vge_init(ifp);
1573
1574 if (status & VGE_ISR_LINKSTS)
1575 vge_tick(sc);
1576 }
1577
1578 /* Re-enable interrupts */
1579 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1580
1581 #ifdef DEVICE_POLLING
1582 done:
1583 #endif
1584 VGE_UNLOCK(sc);
1585
1586 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1587 vge_start(ifp);
1588
1589 return claim;
1590 }
1591
1592 static int
1593 vge_encap(sc, m_head, idx)
1594 struct vge_softc *sc;
1595 struct mbuf *m_head;
1596 int idx;
1597 {
1598 struct mbuf *m_new = NULL;
1599 bus_dmamap_t map;
1600 int error, flags;
1601 struct m_tag *mtag;
1602
1603 /* If this descriptor is still owned by the chip, bail. */
1604 if (sc->vge_ldata.vge_tx_free <= 2
1605 || le32toh(sc->vge_ldata.vge_tx_list[idx].vge_sts) & VGE_TDSTS_OWN)
1606 return (ENOBUFS);
1607
1608 flags = 0;
1609
1610 if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
1611 flags |= VGE_TDCTL_IPCSUM;
1612 if (m_head->m_pkthdr.csum_flags & M_CSUM_TCPv4)
1613 flags |= VGE_TDCTL_TCPCSUM;
1614 if (m_head->m_pkthdr.csum_flags & M_CSUM_UDPv4)
1615 flags |= VGE_TDCTL_UDPCSUM;
1616
1617 map = sc->vge_ldata.vge_tx_dmamap[idx];
1618 error = bus_dmamap_load_mbuf(sc->vge_dmat, map,
1619 m_head, BUS_DMA_NOWAIT);
1620
1621 /* If too many segments to map, coalesce */
1622 if (error == EFBIG) {
1623 m_new = m_defrag(m_head, M_DONTWAIT);
1624 if (m_new == NULL)
1625 return (error);
1626
1627 error = bus_dmamap_load_mbuf(sc->vge_dmat, map,
1628 m_new, BUS_DMA_NOWAIT);
1629 if (error) {
1630 m_freem(m_new);
1631 return (error);
1632 }
1633
1634 m_head = m_new;
1635 } else if (error)
1636 return (error);
1637
1638 vge_dma_map_tx_desc(sc, m_head, idx, flags);
1639
1640 sc->vge_ldata.vge_tx_mbuf[idx] = m_head;
1641 sc->vge_ldata.vge_tx_free--;
1642
1643 /*
1644 * Set up hardware VLAN tagging.
1645 */
1646
1647 mtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m_head);
1648 if (mtag != NULL)
1649 sc->vge_ldata.vge_tx_list[idx].vge_ctl |=
1650 htole32(htons(VLAN_TAG_VALUE(mtag)) | VGE_TDCTL_VTAG);
1651
1652 sc->vge_ldata.vge_tx_list[idx].vge_sts |= htole32(VGE_TDSTS_OWN);
1653
1654 return (0);
1655 }
1656
1657 /*
1658 * Main transmit routine.
1659 */
1660
1661 static void
1662 vge_start(ifp)
1663 struct ifnet *ifp;
1664 {
1665 struct vge_softc *sc;
1666 struct mbuf *m_head = NULL;
1667 int idx, pidx = 0, error;
1668
1669 sc = ifp->if_softc;
1670 VGE_LOCK(sc);
1671
1672 if (!sc->vge_link
1673 || (ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) {
1674 VGE_UNLOCK(sc);
1675 return;
1676 }
1677
1678 idx = sc->vge_ldata.vge_tx_prodidx;
1679
1680 pidx = idx - 1;
1681 if (pidx < 0)
1682 pidx = VGE_TX_DESC_CNT - 1;
1683
1684 /*
1685 * Loop through the send queue, setting up transmit descriptors
1686 * until we drain the queue, or use up all available transmit
1687 * descriptors.
1688 */
1689 for(;;) {
1690 /* Grab a packet off the queue. */
1691 IFQ_POLL(&ifp->if_snd, m_head);
1692 if (m_head == NULL)
1693 break;
1694
1695 if (sc->vge_ldata.vge_tx_mbuf[idx] != NULL) {
1696 /*
1697 * Slot already used, stop for now.
1698 */
1699 ifp->if_flags |= IFF_OACTIVE;
1700 break;
1701 }
1702
1703 if ((error = vge_encap(sc, m_head, idx))) {
1704 if (error == EFBIG) {
1705 printf("%s: Tx packet consumes too many "
1706 "DMA segments, dropping...\n",
1707 sc->sc_dev.dv_xname);
1708 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1709 m_freem(m_head);
1710 continue;
1711 }
1712
1713 /*
1714 * Short on resources, just stop for now.
1715 */
1716 if (error == ENOBUFS)
1717 ifp->if_flags |= IFF_OACTIVE;
1718 break;
1719 }
1720
1721 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1722
1723 /*
1724 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1725 */
1726
1727 sc->vge_ldata.vge_tx_list[pidx].vge_frag[0].vge_buflen |=
1728 htole16(VGE_TXDESC_Q);
1729
1730 if (sc->vge_ldata.vge_tx_mbuf[idx] != m_head) {
1731 m_freem(m_head);
1732 m_head = sc->vge_ldata.vge_tx_mbuf[idx];
1733 }
1734
1735 pidx = idx;
1736 VGE_TX_DESC_INC(idx);
1737
1738 /*
1739 * If there's a BPF listener, bounce a copy of this frame
1740 * to him.
1741 */
1742 #if NBPFILTER > 0
1743 if (ifp->if_bpf)
1744 bpf_mtap(ifp->if_bpf, m_head);
1745 #endif
1746 }
1747
1748 if (idx == sc->vge_ldata.vge_tx_prodidx) {
1749 VGE_UNLOCK(sc);
1750 return;
1751 }
1752
1753 /* Flush the TX descriptors */
1754
1755 bus_dmamap_sync(sc->vge_dmat,
1756 sc->vge_ldata.vge_tx_list_map,
1757 0, sc->vge_ldata.vge_tx_list_map->dm_mapsize,
1758 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1759
1760 /* Issue a transmit command. */
1761 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
1762
1763 sc->vge_ldata.vge_tx_prodidx = idx;
1764
1765 /*
1766 * Use the countdown timer for interrupt moderation.
1767 * 'TX done' interrupts are disabled. Instead, we reset the
1768 * countdown timer, which will begin counting until it hits
1769 * the value in the SSTIMER register, and then trigger an
1770 * interrupt. Each time we set the TIMER0_ENABLE bit, the
1771 * the timer count is reloaded. Only when the transmitter
1772 * is idle will the timer hit 0 and an interrupt fire.
1773 */
1774 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1775
1776 VGE_UNLOCK(sc);
1777
1778 /*
1779 * Set a timeout in case the chip goes out to lunch.
1780 */
1781 ifp->if_timer = 5;
1782
1783 return;
1784 }
1785
1786 static int
1787 vge_init(ifp)
1788 struct ifnet *ifp;
1789 {
1790 struct vge_softc *sc = ifp->if_softc;
1791 struct mii_data *mii = &sc->sc_mii;
1792 int i;
1793
1794 VGE_LOCK(sc);
1795
1796 /*
1797 * Cancel pending I/O and free all RX/TX buffers.
1798 */
1799 vge_stop(sc);
1800 vge_reset(sc);
1801
1802 /*
1803 * Initialize the RX and TX descriptors and mbufs.
1804 */
1805
1806 vge_rx_list_init(sc);
1807 vge_tx_list_init(sc);
1808
1809 /* Set our station address */
1810 for (i = 0; i < ETHER_ADDR_LEN; i++)
1811 CSR_WRITE_1(sc, VGE_PAR0 + i, sc->vge_eaddr[i]);
1812
1813 /*
1814 * Set receive FIFO threshold. Also allow transmission and
1815 * reception of VLAN tagged frames.
1816 */
1817 CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
1818 CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES|VGE_VTAG_OPT2);
1819
1820 /* Set DMA burst length */
1821 CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
1822 CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
1823
1824 CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
1825
1826 /* Set collision backoff algorithm */
1827 CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
1828 VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
1829 CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
1830
1831 /* Disable LPSEL field in priority resolution */
1832 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
1833
1834 /*
1835 * Load the addresses of the DMA queues into the chip.
1836 * Note that we only use one transmit queue.
1837 */
1838
1839 CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0,
1840 VGE_ADDR_LO(sc->vge_ldata.vge_tx_list_addr));
1841 CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_TX_DESC_CNT - 1);
1842
1843 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO,
1844 VGE_ADDR_LO(sc->vge_ldata.vge_rx_list_addr));
1845 CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_RX_DESC_CNT - 1);
1846 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_RX_DESC_CNT);
1847
1848 /* Enable and wake up the RX descriptor queue */
1849 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1850 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1851
1852 /* Enable the TX descriptor queue */
1853 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
1854
1855 /* Set up the receive filter -- allow large frames for VLANs. */
1856 CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST|VGE_RXCTL_RX_GIANT);
1857
1858 /* If we want promiscuous mode, set the allframes bit. */
1859 if (ifp->if_flags & IFF_PROMISC) {
1860 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
1861 }
1862
1863 /* Set capture broadcast bit to capture broadcast frames. */
1864 if (ifp->if_flags & IFF_BROADCAST) {
1865 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST);
1866 }
1867
1868 /* Set multicast bit to capture multicast frames. */
1869 if (ifp->if_flags & IFF_MULTICAST) {
1870 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST);
1871 }
1872
1873 /* Init the cam filter. */
1874 vge_cam_clear(sc);
1875
1876 /* Init the multicast filter. */
1877 vge_setmulti(sc);
1878
1879 /* Enable flow control */
1880
1881 CSR_WRITE_1(sc, VGE_CRS2, 0x8B);
1882
1883 /* Enable jumbo frame reception (if desired) */
1884
1885 /* Start the MAC. */
1886 CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
1887 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
1888 CSR_WRITE_1(sc, VGE_CRS0,
1889 VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
1890
1891 /*
1892 * Configure one-shot timer for microsecond
1893 * resulution and load it for 500 usecs.
1894 */
1895 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES);
1896 CSR_WRITE_2(sc, VGE_SSTIMER, 400);
1897
1898 /*
1899 * Configure interrupt moderation for receive. Enable
1900 * the holdoff counter and load it, and set the RX
1901 * suppression count to the number of descriptors we
1902 * want to allow before triggering an interrupt.
1903 * The holdoff timer is in units of 20 usecs.
1904 */
1905
1906 #ifdef notyet
1907 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE);
1908 /* Select the interrupt holdoff timer page. */
1909 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1910 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
1911 CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */
1912
1913 /* Enable use of the holdoff timer. */
1914 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
1915 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD);
1916
1917 /* Select the RX suppression threshold page. */
1918 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1919 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
1920 CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */
1921
1922 /* Restore the page select bits. */
1923 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1924 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
1925 #endif
1926
1927 #ifdef DEVICE_POLLING
1928 /*
1929 * Disable interrupts if we are polling.
1930 */
1931 if (ifp->if_flags & IFF_POLLING) {
1932 CSR_WRITE_4(sc, VGE_IMR, 0);
1933 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1934 } else /* otherwise ... */
1935 #endif /* DEVICE_POLLING */
1936 {
1937 /*
1938 * Enable interrupts.
1939 */
1940 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
1941 CSR_WRITE_4(sc, VGE_ISR, 0);
1942 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1943 }
1944
1945 mii_mediachg(mii);
1946
1947 ifp->if_flags |= IFF_RUNNING;
1948 ifp->if_flags &= ~IFF_OACTIVE;
1949
1950 sc->vge_if_flags = 0;
1951 sc->vge_link = 0;
1952
1953 VGE_UNLOCK(sc);
1954
1955 callout_schedule(&sc->vge_timeout, hz);
1956
1957 return (0);
1958 }
1959
1960 /*
1961 * Set media options.
1962 */
1963 static int
1964 vge_ifmedia_upd(ifp)
1965 struct ifnet *ifp;
1966 {
1967 struct vge_softc *sc = ifp->if_softc;
1968 struct mii_data *mii = &sc->sc_mii;
1969
1970 mii_mediachg(mii);
1971
1972 return (0);
1973 }
1974
1975 /*
1976 * Report current media status.
1977 */
1978 static void
1979 vge_ifmedia_sts(ifp, ifmr)
1980 struct ifnet *ifp;
1981 struct ifmediareq *ifmr;
1982 {
1983 struct vge_softc *sc = ifp->if_softc;
1984 struct mii_data *mii = &sc->sc_mii;
1985
1986 mii_pollstat(mii);
1987 ifmr->ifm_active = mii->mii_media_active;
1988 ifmr->ifm_status = mii->mii_media_status;
1989
1990 return;
1991 }
1992
1993 static void
1994 vge_miibus_statchg(self)
1995 struct device *self;
1996 {
1997 struct vge_softc *sc = (struct vge_softc *) self;
1998 struct mii_data *mii = &sc->sc_mii;
1999 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
2000
2001 /*
2002 * If the user manually selects a media mode, we need to turn
2003 * on the forced MAC mode bit in the DIAGCTL register. If the
2004 * user happens to choose a full duplex mode, we also need to
2005 * set the 'force full duplex' bit. This applies only to
2006 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
2007 * mode is disabled, and in 1000baseT mode, full duplex is
2008 * always implied, so we turn on the forced mode bit but leave
2009 * the FDX bit cleared.
2010 */
2011
2012 switch (IFM_SUBTYPE(ife->ifm_media)) {
2013 case IFM_AUTO:
2014 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2015 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2016 break;
2017 case IFM_1000_T:
2018 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2019 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2020 break;
2021 case IFM_100_TX:
2022 case IFM_10_T:
2023 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2024 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2025 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2026 } else {
2027 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2028 }
2029 break;
2030 default:
2031 printf("%s: unknown media type: %x\n",
2032 sc->sc_dev.dv_xname,
2033 IFM_SUBTYPE(ife->ifm_media));
2034 break;
2035 }
2036
2037 return;
2038 }
2039
2040 static int
2041 vge_ioctl(ifp, command, data)
2042 struct ifnet *ifp;
2043 u_long command;
2044 caddr_t data;
2045 {
2046 struct vge_softc *sc = ifp->if_softc;
2047 struct ifreq *ifr = (struct ifreq *) data;
2048 struct mii_data *mii;
2049 int s, error = 0;
2050
2051 s = splnet();
2052
2053 switch (command) {
2054 case SIOCSIFMTU:
2055 if (ifr->ifr_mtu > VGE_JUMBO_MTU)
2056 error = EINVAL;
2057 ifp->if_mtu = ifr->ifr_mtu;
2058 break;
2059 case SIOCSIFFLAGS:
2060 if (ifp->if_flags & IFF_UP) {
2061 if (ifp->if_flags & IFF_RUNNING &&
2062 ifp->if_flags & IFF_PROMISC &&
2063 !(sc->vge_if_flags & IFF_PROMISC)) {
2064 CSR_SETBIT_1(sc, VGE_RXCTL,
2065 VGE_RXCTL_RX_PROMISC);
2066 vge_setmulti(sc);
2067 } else if (ifp->if_flags & IFF_RUNNING &&
2068 !(ifp->if_flags & IFF_PROMISC) &&
2069 sc->vge_if_flags & IFF_PROMISC) {
2070 CSR_CLRBIT_1(sc, VGE_RXCTL,
2071 VGE_RXCTL_RX_PROMISC);
2072 vge_setmulti(sc);
2073 } else
2074 vge_init(ifp);
2075 } else {
2076 if (ifp->if_flags & IFF_RUNNING)
2077 vge_stop(sc);
2078 }
2079 sc->vge_if_flags = ifp->if_flags;
2080 break;
2081 case SIOCADDMULTI:
2082 case SIOCDELMULTI:
2083 error = (command == SIOCADDMULTI) ?
2084 ether_addmulti(ifr, &sc->sc_ethercom) :
2085 ether_delmulti(ifr, &sc->sc_ethercom);
2086
2087 if (error == ENETRESET) {
2088 /*
2089 * Multicast list has changed; set the hardware filter
2090 * accordingly.
2091 */
2092 if (ifp->if_flags & IFF_RUNNING)
2093 vge_setmulti(sc);
2094 error = 0;
2095 }
2096 break;
2097 case SIOCGIFMEDIA:
2098 case SIOCSIFMEDIA:
2099 mii = &sc->sc_mii;
2100 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2101 break;
2102 default:
2103 error = ether_ioctl(ifp, command, data);
2104 break;
2105 }
2106
2107 splx(s);
2108 return (error);
2109 }
2110
2111 static void
2112 vge_watchdog(ifp)
2113 struct ifnet *ifp;
2114 {
2115 struct vge_softc *sc;
2116
2117 sc = ifp->if_softc;
2118 VGE_LOCK(sc);
2119 printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
2120 ifp->if_oerrors++;
2121
2122 vge_txeof(sc);
2123 vge_rxeof(sc);
2124
2125 vge_init(ifp);
2126
2127 VGE_UNLOCK(sc);
2128
2129 return;
2130 }
2131
2132 /*
2133 * Stop the adapter and free any mbufs allocated to the
2134 * RX and TX lists.
2135 */
2136 static void
2137 vge_stop(sc)
2138 struct vge_softc *sc;
2139 {
2140 register int i;
2141 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2142
2143 VGE_LOCK(sc);
2144 ifp->if_timer = 0;
2145
2146 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2147 #ifdef DEVICE_POLLING
2148 ether_poll_deregister(ifp);
2149 #endif /* DEVICE_POLLING */
2150
2151 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2152 CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
2153 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2154 CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
2155 CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
2156 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
2157
2158 if (sc->vge_head != NULL) {
2159 m_freem(sc->vge_head);
2160 sc->vge_head = sc->vge_tail = NULL;
2161 }
2162
2163 /* Free the TX list buffers. */
2164
2165 for (i = 0; i < VGE_TX_DESC_CNT; i++) {
2166 if (sc->vge_ldata.vge_tx_mbuf[i] != NULL) {
2167 bus_dmamap_unload(sc->vge_dmat,
2168 sc->vge_ldata.vge_tx_dmamap[i]);
2169 m_freem(sc->vge_ldata.vge_tx_mbuf[i]);
2170 sc->vge_ldata.vge_tx_mbuf[i] = NULL;
2171 }
2172 }
2173
2174 /* Free the RX list buffers. */
2175
2176 for (i = 0; i < VGE_RX_DESC_CNT; i++) {
2177 if (sc->vge_ldata.vge_rx_mbuf[i] != NULL) {
2178 bus_dmamap_unload(sc->vge_dmat,
2179 sc->vge_ldata.vge_rx_dmamap[i]);
2180 m_freem(sc->vge_ldata.vge_rx_mbuf[i]);
2181 sc->vge_ldata.vge_rx_mbuf[i] = NULL;
2182 }
2183 }
2184
2185 VGE_UNLOCK(sc);
2186
2187 return;
2188 }
2189
2190 #if VGE_POWER_MANAGEMENT
2191 /*
2192 * Device suspend routine. Stop the interface and save some PCI
2193 * settings in case the BIOS doesn't restore them properly on
2194 * resume.
2195 */
2196 static int
2197 vge_suspend(dev)
2198 struct device * dev;
2199 {
2200 struct vge_softc *sc;
2201 int i;
2202
2203 sc = device_get_softc(dev);
2204
2205 vge_stop(sc);
2206
2207 for (i = 0; i < 5; i++)
2208 sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
2209 sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
2210 sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
2211 sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
2212 sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
2213
2214 sc->suspended = 1;
2215
2216 return (0);
2217 }
2218
2219 /*
2220 * Device resume routine. Restore some PCI settings in case the BIOS
2221 * doesn't, re-enable busmastering, and restart the interface if
2222 * appropriate.
2223 */
2224 static int
2225 vge_resume(dev)
2226 struct device * dev;
2227 {
2228 struct vge_softc *sc = (struct vge_softc *)dev;
2229 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2230 int i;
2231
2232 /* better way to do this? */
2233 for (i = 0; i < 5; i++)
2234 pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
2235 pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
2236 pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
2237 pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
2238 pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
2239
2240 /* reenable busmastering */
2241 pci_enable_busmaster(dev);
2242 pci_enable_io(dev, SYS_RES_MEMORY);
2243
2244 /* reinitialize interface if necessary */
2245 if (ifp->if_flags & IFF_UP)
2246 vge_init(sc);
2247
2248 sc->suspended = 0;
2249
2250 return (0);
2251 }
2252 #endif
2253
2254 /*
2255 * Stop all chip I/O so that the kernel's probe routines don't
2256 * get confused by errant DMAs when rebooting.
2257 */
2258 static void
2259 vge_shutdown(arg)
2260 void *arg;
2261 {
2262 struct vge_softc *sc = (struct vge_softc *)arg;
2263
2264 vge_stop(sc);
2265 }
2266