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