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