if_vge.c revision 1.78 1 /* $NetBSD: if_vge.c,v 1.78 2019/12/27 07:41:23 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.78 2019/12/27 07:41:23 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_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 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 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 if (txstat & (VGE_TDSTS_EXCESSCOLL | VGE_TDSTS_COLL))
1367 ifp->if_collisions++;
1368 if (txstat & VGE_TDSTS_TXERR)
1369 ifp->if_oerrors++;
1370 else
1371 ifp->if_opackets++;
1372 }
1373
1374 sc->sc_tx_considx = idx;
1375
1376 if (sc->sc_tx_free > 0) {
1377 ifp->if_flags &= ~IFF_OACTIVE;
1378 }
1379
1380 /*
1381 * If not all descriptors have been released reaped yet,
1382 * reload the timer so that we will eventually get another
1383 * interrupt that will cause us to re-enter this routine.
1384 * This is done in case the transmitter has gone idle.
1385 */
1386 if (sc->sc_tx_free < VGE_NTXDESC)
1387 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1388 else
1389 ifp->if_timer = 0;
1390 }
1391
1392 static void
1393 vge_tick(void *arg)
1394 {
1395 struct vge_softc *sc;
1396 struct ifnet *ifp;
1397 struct mii_data *mii;
1398 int s;
1399
1400 sc = arg;
1401 ifp = &sc->sc_ethercom.ec_if;
1402 mii = &sc->sc_mii;
1403
1404 s = splnet();
1405
1406 callout_schedule(&sc->sc_timeout, hz);
1407
1408 mii_tick(mii);
1409 if (sc->sc_link) {
1410 if ((mii->mii_media_status & IFM_ACTIVE) == 0)
1411 sc->sc_link = 0;
1412 } else {
1413 if (mii->mii_media_status & IFM_ACTIVE &&
1414 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1415 sc->sc_link = 1;
1416 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1417 vge_start(ifp);
1418 }
1419 }
1420
1421 splx(s);
1422 }
1423
1424 static int
1425 vge_intr(void *arg)
1426 {
1427 struct vge_softc *sc;
1428 struct ifnet *ifp;
1429 uint32_t status;
1430 int claim;
1431
1432 sc = arg;
1433 claim = 0;
1434 if (sc->sc_suspended) {
1435 return claim;
1436 }
1437
1438 ifp = &sc->sc_ethercom.ec_if;
1439
1440 if ((ifp->if_flags & IFF_UP) == 0) {
1441 return claim;
1442 }
1443
1444 /* Disable interrupts */
1445 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1446
1447 for (;;) {
1448
1449 status = CSR_READ_4(sc, VGE_ISR);
1450 /* If the card has gone away the read returns 0xffffffff. */
1451 if (status == 0xFFFFFFFF)
1452 break;
1453
1454 if (status) {
1455 claim = 1;
1456 CSR_WRITE_4(sc, VGE_ISR, status);
1457 }
1458
1459 if ((status & VGE_INTRS) == 0)
1460 break;
1461
1462 if (status & (VGE_ISR_RXOK | VGE_ISR_RXOK_HIPRIO))
1463 vge_rxeof(sc);
1464
1465 if (status & (VGE_ISR_RXOFLOW | VGE_ISR_RXNODESC)) {
1466 vge_rxeof(sc);
1467 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1468 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1469 }
1470
1471 if (status & (VGE_ISR_TXOK0 | VGE_ISR_TIMER0))
1472 vge_txeof(sc);
1473
1474 if (status & (VGE_ISR_TXDMA_STALL | VGE_ISR_RXDMA_STALL))
1475 vge_init(ifp);
1476
1477 if (status & VGE_ISR_LINKSTS)
1478 vge_tick(sc);
1479 }
1480
1481 /* Re-enable interrupts */
1482 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1483
1484 if (claim)
1485 if_schedule_deferred_start(ifp);
1486
1487 return claim;
1488 }
1489
1490 static int
1491 vge_encap(struct vge_softc *sc, struct mbuf *m_head, int idx)
1492 {
1493 struct vge_txsoft *txs;
1494 struct vge_txdesc *txd;
1495 struct vge_txfrag *f;
1496 struct mbuf *m_new;
1497 bus_dmamap_t map;
1498 int m_csumflags, seg, error, flags;
1499 size_t sz;
1500 uint32_t td_sts, td_ctl;
1501
1502 KASSERT(sc->sc_tx_free > 0);
1503
1504 txd = &sc->sc_txdescs[idx];
1505
1506 #ifdef DIAGNOSTIC
1507 /* If this descriptor is still owned by the chip, bail. */
1508 VGE_TXDESCSYNC(sc, idx,
1509 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1510 td_sts = le32toh(txd->td_sts);
1511 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1512 if (td_sts & VGE_TDSTS_OWN) {
1513 return ENOBUFS;
1514 }
1515 #endif
1516
1517 /*
1518 * Preserve m_pkthdr.csum_flags here since m_head might be
1519 * updated by m_defrag()
1520 */
1521 m_csumflags = m_head->m_pkthdr.csum_flags;
1522
1523 txs = &sc->sc_txsoft[idx];
1524 map = txs->txs_dmamap;
1525 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m_head, BUS_DMA_NOWAIT);
1526
1527 /* If too many segments to map, coalesce */
1528 if (error == EFBIG ||
1529 (m_head->m_pkthdr.len < ETHER_PAD_LEN &&
1530 map->dm_nsegs == VGE_TX_FRAGS)) {
1531 m_new = m_defrag(m_head, M_DONTWAIT);
1532 if (m_new == NULL)
1533 return EFBIG;
1534
1535 error = bus_dmamap_load_mbuf(sc->sc_dmat, map,
1536 m_new, BUS_DMA_NOWAIT);
1537 if (error) {
1538 m_freem(m_new);
1539 return error;
1540 }
1541
1542 m_head = m_new;
1543 } else if (error)
1544 return error;
1545
1546 txs->txs_mbuf = m_head;
1547
1548 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1549 BUS_DMASYNC_PREWRITE);
1550
1551 for (seg = 0, f = &txd->td_frag[0]; seg < map->dm_nsegs; seg++, f++) {
1552 f->tf_buflen = htole16(VGE_BUFLEN(map->dm_segs[seg].ds_len));
1553 vge_set_txaddr(f, map->dm_segs[seg].ds_addr);
1554 }
1555
1556 /* Argh. This chip does not autopad short frames */
1557 sz = m_head->m_pkthdr.len;
1558 if (sz < ETHER_PAD_LEN) {
1559 f->tf_buflen = htole16(VGE_BUFLEN(ETHER_PAD_LEN - sz));
1560 vge_set_txaddr(f, VGE_CDPADADDR(sc));
1561 sz = ETHER_PAD_LEN;
1562 seg++;
1563 }
1564 VGE_TXFRAGSYNC(sc, idx, seg, BUS_DMASYNC_PREWRITE);
1565
1566 /*
1567 * When telling the chip how many segments there are, we
1568 * must use nsegs + 1 instead of just nsegs. Darned if I
1569 * know why.
1570 */
1571 seg++;
1572
1573 flags = 0;
1574 if (m_csumflags & M_CSUM_IPv4)
1575 flags |= VGE_TDCTL_IPCSUM;
1576 if (m_csumflags & M_CSUM_TCPv4)
1577 flags |= VGE_TDCTL_TCPCSUM;
1578 if (m_csumflags & M_CSUM_UDPv4)
1579 flags |= VGE_TDCTL_UDPCSUM;
1580 td_sts = sz << 16;
1581 td_ctl = flags | (seg << 28) | VGE_TD_LS_NORM;
1582
1583 if (sz > ETHERMTU + ETHER_HDR_LEN)
1584 td_ctl |= VGE_TDCTL_JUMBO;
1585
1586 /*
1587 * Set up hardware VLAN tagging.
1588 */
1589 if (vlan_has_tag(m_head)) {
1590 /*
1591 * No need htons() here since vge(4) chip assumes
1592 * that tags are written in little endian and
1593 * we already use htole32() here.
1594 */
1595 td_ctl |= vlan_get_tag(m_head) | VGE_TDCTL_VTAG;
1596 }
1597 txd->td_ctl = htole32(td_ctl);
1598 txd->td_sts = htole32(td_sts);
1599 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1600
1601 txd->td_sts = htole32(VGE_TDSTS_OWN | td_sts);
1602 VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1603
1604 sc->sc_tx_free--;
1605
1606 return 0;
1607 }
1608
1609 /*
1610 * Main transmit routine.
1611 */
1612
1613 static void
1614 vge_start(struct ifnet *ifp)
1615 {
1616 struct vge_softc *sc;
1617 struct vge_txsoft *txs;
1618 struct mbuf *m_head;
1619 int idx, pidx, ofree, error;
1620
1621 sc = ifp->if_softc;
1622
1623 if (!sc->sc_link ||
1624 (ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
1625 return;
1626 }
1627
1628 m_head = NULL;
1629 idx = sc->sc_tx_prodidx;
1630 pidx = VGE_PREV_TXDESC(idx);
1631 ofree = sc->sc_tx_free;
1632
1633 /*
1634 * Loop through the send queue, setting up transmit descriptors
1635 * until we drain the queue, or use up all available transmit
1636 * descriptors.
1637 */
1638 for (;;) {
1639 /* Grab a packet off the queue. */
1640 IFQ_POLL(&ifp->if_snd, m_head);
1641 if (m_head == NULL)
1642 break;
1643
1644 if (sc->sc_tx_free == 0) {
1645 /*
1646 * All slots used, stop for now.
1647 */
1648 ifp->if_flags |= IFF_OACTIVE;
1649 break;
1650 }
1651
1652 txs = &sc->sc_txsoft[idx];
1653 KASSERT(txs->txs_mbuf == NULL);
1654
1655 if ((error = vge_encap(sc, m_head, idx))) {
1656 if (error == EFBIG) {
1657 printf("%s: Tx packet consumes too many "
1658 "DMA segments, dropping...\n",
1659 device_xname(sc->sc_dev));
1660 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1661 m_freem(m_head);
1662 continue;
1663 }
1664
1665 /*
1666 * Short on resources, just stop for now.
1667 */
1668 if (error == ENOBUFS)
1669 ifp->if_flags |= IFF_OACTIVE;
1670 break;
1671 }
1672
1673 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1674
1675 /*
1676 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1677 */
1678
1679 sc->sc_txdescs[pidx].td_frag[0].tf_buflen |=
1680 htole16(VGE_TXDESC_Q);
1681 VGE_TXFRAGSYNC(sc, pidx, 1,
1682 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1683
1684 if (txs->txs_mbuf != m_head) {
1685 m_freem(m_head);
1686 m_head = txs->txs_mbuf;
1687 }
1688
1689 pidx = idx;
1690 idx = VGE_NEXT_TXDESC(idx);
1691
1692 /*
1693 * If there's a BPF listener, bounce a copy of this frame
1694 * to him.
1695 */
1696 bpf_mtap(ifp, m_head, BPF_D_OUT);
1697 }
1698
1699 if (sc->sc_tx_free < ofree) {
1700 /* TX packet queued */
1701
1702 sc->sc_tx_prodidx = idx;
1703
1704 /* Issue a transmit command. */
1705 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
1706
1707 /*
1708 * Use the countdown timer for interrupt moderation.
1709 * 'TX done' interrupts are disabled. Instead, we reset the
1710 * countdown timer, which will begin counting until it hits
1711 * the value in the SSTIMER register, and then trigger an
1712 * interrupt. Each time we set the TIMER0_ENABLE bit, the
1713 * the timer count is reloaded. Only when the transmitter
1714 * is idle will the timer hit 0 and an interrupt fire.
1715 */
1716 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1717
1718 /*
1719 * Set a timeout in case the chip goes out to lunch.
1720 */
1721 ifp->if_timer = 5;
1722 }
1723 }
1724
1725 static int
1726 vge_init(struct ifnet *ifp)
1727 {
1728 struct vge_softc *sc;
1729 int i, rc = 0;
1730
1731 sc = ifp->if_softc;
1732
1733 /*
1734 * Cancel pending I/O and free all RX/TX buffers.
1735 */
1736 vge_stop(ifp, 0);
1737 vge_reset(sc);
1738
1739 /* Initialize the RX descriptors and mbufs. */
1740 memset(sc->sc_rxdescs, 0, sizeof(sc->sc_rxdescs));
1741 sc->sc_rx_consumed = 0;
1742 for (i = 0; i < VGE_NRXDESC; i++) {
1743 if (vge_newbuf(sc, i, NULL) == ENOBUFS) {
1744 printf("%s: unable to allocate or map rx buffer\n",
1745 device_xname(sc->sc_dev));
1746 return 1; /* XXX */
1747 }
1748 }
1749 sc->sc_rx_prodidx = 0;
1750 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
1751
1752 /* Initialize the TX descriptors and mbufs. */
1753 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1754 bus_dmamap_sync(sc->sc_dmat, sc->sc_cddmamap,
1755 VGE_CDTXOFF(0), sizeof(sc->sc_txdescs),
1756 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1757 for (i = 0; i < VGE_NTXDESC; i++)
1758 sc->sc_txsoft[i].txs_mbuf = NULL;
1759
1760 sc->sc_tx_prodidx = 0;
1761 sc->sc_tx_considx = 0;
1762 sc->sc_tx_free = VGE_NTXDESC;
1763
1764 /* Set our station address */
1765 for (i = 0; i < ETHER_ADDR_LEN; i++)
1766 CSR_WRITE_1(sc, VGE_PAR0 + i, sc->sc_eaddr[i]);
1767
1768 /*
1769 * Set receive FIFO threshold. Also allow transmission and
1770 * reception of VLAN tagged frames.
1771 */
1772 CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR | VGE_RXCFG_VTAGOPT);
1773 CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES | VGE_VTAG_OPT2);
1774
1775 /* Set DMA burst length */
1776 CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
1777 CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
1778
1779 CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO | VGE_TXCFG_NONBLK);
1780
1781 /* Set collision backoff algorithm */
1782 CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM |
1783 VGE_CHIPCFG1_CAP | VGE_CHIPCFG1_MBA | VGE_CHIPCFG1_BAKOPT);
1784 CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
1785
1786 /* Disable LPSEL field in priority resolution */
1787 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
1788
1789 /*
1790 * Load the addresses of the DMA queues into the chip.
1791 * Note that we only use one transmit queue.
1792 */
1793
1794 CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0, VGE_ADDR_LO(VGE_CDTXADDR(sc, 0)));
1795 CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_NTXDESC - 1);
1796
1797 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, VGE_ADDR_LO(VGE_CDRXADDR(sc, 0)));
1798 CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_NRXDESC - 1);
1799 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_NRXDESC);
1800
1801 /* Enable and wake up the RX descriptor queue */
1802 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1803 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1804
1805 /* Enable the TX descriptor queue */
1806 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
1807
1808 /* Set up the receive filter -- allow large frames for VLANs. */
1809 CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST | VGE_RXCTL_RX_GIANT);
1810
1811 /* If we want promiscuous mode, set the allframes bit. */
1812 if (ifp->if_flags & IFF_PROMISC) {
1813 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
1814 }
1815
1816 /* Set capture broadcast bit to capture broadcast frames. */
1817 if (ifp->if_flags & IFF_BROADCAST) {
1818 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST);
1819 }
1820
1821 /* Set multicast bit to capture multicast frames. */
1822 if (ifp->if_flags & IFF_MULTICAST) {
1823 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST);
1824 }
1825
1826 /* Init the cam filter. */
1827 vge_cam_clear(sc);
1828
1829 /* Init the multicast filter. */
1830 vge_setmulti(sc);
1831
1832 /* Enable flow control */
1833
1834 CSR_WRITE_1(sc, VGE_CRS2, 0x8B);
1835
1836 /* Enable jumbo frame reception (if desired) */
1837
1838 /* Start the MAC. */
1839 CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
1840 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
1841 CSR_WRITE_1(sc, VGE_CRS0,
1842 VGE_CR0_TX_ENABLE | VGE_CR0_RX_ENABLE | VGE_CR0_START);
1843
1844 /*
1845 * Configure one-shot timer for microsecond
1846 * resulution and load it for 500 usecs.
1847 */
1848 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES);
1849 CSR_WRITE_2(sc, VGE_SSTIMER, 400);
1850
1851 /*
1852 * Configure interrupt moderation for receive. Enable
1853 * the holdoff counter and load it, and set the RX
1854 * suppression count to the number of descriptors we
1855 * want to allow before triggering an interrupt.
1856 * The holdoff timer is in units of 20 usecs.
1857 */
1858
1859 #ifdef notyet
1860 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE);
1861 /* Select the interrupt holdoff timer page. */
1862 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1863 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
1864 CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */
1865
1866 /* Enable use of the holdoff timer. */
1867 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
1868 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD);
1869
1870 /* Select the RX suppression threshold page. */
1871 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1872 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
1873 CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */
1874
1875 /* Restore the page select bits. */
1876 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1877 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
1878 #endif
1879
1880 #ifdef DEVICE_POLLING
1881 /*
1882 * Disable interrupts if we are polling.
1883 */
1884 if (ifp->if_flags & IFF_POLLING) {
1885 CSR_WRITE_4(sc, VGE_IMR, 0);
1886 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1887 } else /* otherwise ... */
1888 #endif /* DEVICE_POLLING */
1889 {
1890 /*
1891 * Enable interrupts.
1892 */
1893 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
1894 CSR_WRITE_4(sc, VGE_ISR, 0);
1895 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1896 }
1897
1898 if ((rc = ether_mediachange(ifp)) != 0)
1899 goto out;
1900
1901 ifp->if_flags |= IFF_RUNNING;
1902 ifp->if_flags &= ~IFF_OACTIVE;
1903
1904 sc->sc_if_flags = 0;
1905 sc->sc_link = 0;
1906
1907 callout_schedule(&sc->sc_timeout, hz);
1908
1909 out:
1910 return rc;
1911 }
1912
1913 static void
1914 vge_miibus_statchg(struct ifnet *ifp)
1915 {
1916 struct vge_softc *sc = ifp->if_softc;
1917 struct mii_data *mii = &sc->sc_mii;
1918 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
1919 uint8_t dctl;
1920
1921 /*
1922 * If the user manually selects a media mode, we need to turn
1923 * on the forced MAC mode bit in the DIAGCTL register. If the
1924 * user happens to choose a full duplex mode, we also need to
1925 * set the 'force full duplex' bit. This applies only to
1926 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
1927 * mode is disabled, and in 1000baseT mode, full duplex is
1928 * always implied, so we turn on the forced mode bit but leave
1929 * the FDX bit cleared.
1930 */
1931 dctl = CSR_READ_1(sc, VGE_DIAGCTL);
1932
1933 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
1934 dctl &= ~VGE_DIAGCTL_MACFORCE;
1935 dctl &= ~VGE_DIAGCTL_FDXFORCE;
1936 } else {
1937 u_int ifmword;
1938
1939 /* If the link is up, use the current active media. */
1940 if ((mii->mii_media_status & IFM_ACTIVE) != 0)
1941 ifmword = mii->mii_media_active;
1942 else
1943 ifmword = ife->ifm_media;
1944
1945 dctl |= VGE_DIAGCTL_MACFORCE;
1946 if ((ifmword & IFM_FDX) != 0)
1947 dctl |= VGE_DIAGCTL_FDXFORCE;
1948 else
1949 dctl &= ~VGE_DIAGCTL_FDXFORCE;
1950
1951 if (IFM_SUBTYPE(ifmword) == IFM_1000_T) {
1952 /*
1953 * It means the user setting is not auto but it's
1954 * 1000baseT-FDX or 1000baseT.
1955 */
1956 dctl |= VGE_DIAGCTL_GMII;
1957 } else
1958 dctl &= ~VGE_DIAGCTL_GMII;
1959 }
1960
1961 CSR_WRITE_1(sc, VGE_DIAGCTL, dctl);
1962 }
1963
1964 static int
1965 vge_ifflags_cb(struct ethercom *ec)
1966 {
1967 struct ifnet *ifp = &ec->ec_if;
1968 struct vge_softc *sc = ifp->if_softc;
1969 u_short change = ifp->if_flags ^ sc->sc_if_flags;
1970
1971 if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0)
1972 return ENETRESET;
1973 else if ((change & IFF_PROMISC) == 0)
1974 return 0;
1975
1976 if ((ifp->if_flags & IFF_PROMISC) == 0)
1977 CSR_CLRBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
1978 else
1979 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
1980 vge_setmulti(sc);
1981 return 0;
1982 }
1983
1984 static int
1985 vge_ioctl(struct ifnet *ifp, u_long command, void *data)
1986 {
1987 struct vge_softc *sc;
1988 int s, error;
1989
1990 sc = ifp->if_softc;
1991 error = 0;
1992
1993 s = splnet();
1994
1995 if ((error = ether_ioctl(ifp, command, data)) == ENETRESET) {
1996 error = 0;
1997 if (command != SIOCADDMULTI && command != SIOCDELMULTI)
1998 ;
1999 else if (ifp->if_flags & IFF_RUNNING) {
2000 /*
2001 * Multicast list has changed; set the hardware filter
2002 * accordingly.
2003 */
2004 vge_setmulti(sc);
2005 }
2006 }
2007 sc->sc_if_flags = ifp->if_flags;
2008
2009 splx(s);
2010 return error;
2011 }
2012
2013 static void
2014 vge_watchdog(struct ifnet *ifp)
2015 {
2016 struct vge_softc *sc;
2017 int s;
2018
2019 sc = ifp->if_softc;
2020 s = splnet();
2021 printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
2022 ifp->if_oerrors++;
2023
2024 vge_txeof(sc);
2025 vge_rxeof(sc);
2026
2027 vge_init(ifp);
2028
2029 splx(s);
2030 }
2031
2032 /*
2033 * Stop the adapter and free any mbufs allocated to the
2034 * RX and TX lists.
2035 */
2036 static void
2037 vge_stop(struct ifnet *ifp, int disable)
2038 {
2039 struct vge_softc *sc = ifp->if_softc;
2040 struct vge_txsoft *txs;
2041 struct vge_rxsoft *rxs;
2042 int i, s;
2043
2044 s = splnet();
2045 ifp->if_timer = 0;
2046
2047 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2048 #ifdef DEVICE_POLLING
2049 ether_poll_deregister(ifp);
2050 #endif /* DEVICE_POLLING */
2051
2052 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2053 CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
2054 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2055 CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
2056 CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
2057 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
2058
2059 if (sc->sc_rx_mhead != NULL) {
2060 m_freem(sc->sc_rx_mhead);
2061 sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
2062 }
2063
2064 /* Free the TX list buffers. */
2065
2066 for (i = 0; i < VGE_NTXDESC; i++) {
2067 txs = &sc->sc_txsoft[i];
2068 if (txs->txs_mbuf != NULL) {
2069 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2070 m_freem(txs->txs_mbuf);
2071 txs->txs_mbuf = NULL;
2072 }
2073 }
2074
2075 /* Free the RX list buffers. */
2076
2077 for (i = 0; i < VGE_NRXDESC; i++) {
2078 rxs = &sc->sc_rxsoft[i];
2079 if (rxs->rxs_mbuf != NULL) {
2080 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2081 m_freem(rxs->rxs_mbuf);
2082 rxs->rxs_mbuf = NULL;
2083 }
2084 }
2085
2086 splx(s);
2087 }
2088
2089 #if VGE_POWER_MANAGEMENT
2090 /*
2091 * Device suspend routine. Stop the interface and save some PCI
2092 * settings in case the BIOS doesn't restore them properly on
2093 * resume.
2094 */
2095 static int
2096 vge_suspend(device_t dev)
2097 {
2098 struct vge_softc *sc;
2099 int i;
2100
2101 sc = device_get_softc(dev);
2102
2103 vge_stop(sc);
2104
2105 for (i = 0; i < 5; i++)
2106 sc->sc_saved_maps[i] =
2107 pci_read_config(dev, PCIR_MAPS + i * 4, 4);
2108 sc->sc_saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
2109 sc->sc_saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
2110 sc->sc_saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
2111 sc->sc_saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
2112
2113 sc->suspended = 1;
2114
2115 return 0;
2116 }
2117
2118 /*
2119 * Device resume routine. Restore some PCI settings in case the BIOS
2120 * doesn't, re-enable busmastering, and restart the interface if
2121 * appropriate.
2122 */
2123 static int
2124 vge_resume(device_t dev)
2125 {
2126 struct vge_softc *sc;
2127 struct ifnet *ifp;
2128 int i;
2129
2130 sc = device_private(dev);
2131 ifp = &sc->sc_ethercom.ec_if;
2132
2133 /* better way to do this? */
2134 for (i = 0; i < 5; i++)
2135 pci_write_config(dev, PCIR_MAPS + i * 4,
2136 sc->sc_saved_maps[i], 4);
2137 pci_write_config(dev, PCIR_BIOS, sc->sc_saved_biosaddr, 4);
2138 pci_write_config(dev, PCIR_INTLINE, sc->sc_saved_intline, 1);
2139 pci_write_config(dev, PCIR_CACHELNSZ, sc->sc_saved_cachelnsz, 1);
2140 pci_write_config(dev, PCIR_LATTIMER, sc->sc_saved_lattimer, 1);
2141
2142 /* reenable busmastering */
2143 pci_enable_busmaster(dev);
2144 pci_enable_io(dev, SYS_RES_MEMORY);
2145
2146 /* reinitialize interface if necessary */
2147 if (ifp->if_flags & IFF_UP)
2148 vge_init(sc);
2149
2150 sc->suspended = 0;
2151
2152 return 0;
2153 }
2154 #endif
2155
2156 /*
2157 * Stop all chip I/O so that the kernel's probe routines don't
2158 * get confused by errant DMAs when rebooting.
2159 */
2160 static bool
2161 vge_shutdown(device_t self, int howto)
2162 {
2163 struct vge_softc *sc;
2164
2165 sc = device_private(self);
2166 vge_stop(&sc->sc_ethercom.ec_if, 1);
2167
2168 return true;
2169 }
2170
2171 static void
2172 vge_clrwol(struct vge_softc *sc)
2173 {
2174 uint8_t val;
2175
2176 val = CSR_READ_1(sc, VGE_PWRSTAT);
2177 val &= ~VGE_STICKHW_SWPTAG;
2178 CSR_WRITE_1(sc, VGE_PWRSTAT, val);
2179 /* Disable WOL and clear power state indicator. */
2180 val = CSR_READ_1(sc, VGE_PWRSTAT);
2181 val &= ~(VGE_STICKHW_DS0 | VGE_STICKHW_DS1);
2182 CSR_WRITE_1(sc, VGE_PWRSTAT, val);
2183
2184 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_GMII);
2185 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2186
2187 /* Clear WOL on pattern match. */
2188 CSR_WRITE_1(sc, VGE_WOLCR0C, VGE_WOLCR0_PATTERN_ALL);
2189 /* Disable WOL on magic/unicast packet. */
2190 CSR_WRITE_1(sc, VGE_WOLCR1C, 0x0F);
2191 CSR_WRITE_1(sc, VGE_WOLCFGC, VGE_WOLCFG_SAB | VGE_WOLCFG_SAM |
2192 VGE_WOLCFG_PMEOVR);
2193 /* Clear WOL status on pattern match. */
2194 CSR_WRITE_1(sc, VGE_WOLSR0C, 0xFF);
2195 CSR_WRITE_1(sc, VGE_WOLSR1C, 0xFF);
2196 }
2197