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