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