- Add ciphy for PHY modules produced by Cicada Semiconductor
[dragonfly.git] / sys / dev / netif / vge / if_vge.c
1 /*
2  * Copyright (c) 2004
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/dev/vge/if_vge.c,v 1.24 2006/02/14 12:44:56 glebius Exp $
33  * $DragonFly: src/sys/dev/netif/vge/if_vge.c,v 1.1 2006/05/20 07:15:17 sephe Exp $
34  */
35
36 /*
37  * VIA Networking Technologies VT612x PCI gigabit ethernet NIC driver.
38  *
39  * Written by Bill Paul <wpaul@windriver.com>
40  * Senior Networking Software Engineer
41  * Wind River Systems
42  */
43
44 /*
45  * The VIA Networking VT6122 is a 32bit, 33/66Mhz PCI device that
46  * combines a tri-speed ethernet MAC and PHY, with the following
47  * features:
48  *
49  *      o Jumbo frame support up to 16K
50  *      o Transmit and receive flow control
51  *      o IPv4 checksum offload
52  *      o VLAN tag insertion and stripping
53  *      o TCP large send
54  *      o 64-bit multicast hash table filter
55  *      o 64 entry CAM filter
56  *      o 16K RX FIFO and 48K TX FIFO memory
57  *      o Interrupt moderation
58  *
59  * The VT6122 supports up to four transmit DMA queues. The descriptors
60  * in the transmit ring can address up to 7 data fragments; frames which
61  * span more than 7 data buffers must be coalesced, but in general the
62  * BSD TCP/IP stack rarely generates frames more than 2 or 3 fragments
63  * long. The receive descriptors address only a single buffer.
64  *
65  * There are two peculiar design issues with the VT6122. One is that
66  * receive data buffers must be aligned on a 32-bit boundary. This is
67  * not a problem where the VT6122 is used as a LOM device in x86-based
68  * systems, but on architectures that generate unaligned access traps, we
69  * have to do some copying.
70  *
71  * The other issue has to do with the way 64-bit addresses are handled.
72  * The DMA descriptors only allow you to specify 48 bits of addressing
73  * information. The remaining 16 bits are specified using one of the
74  * I/O registers. If you only have a 32-bit system, then this isn't
75  * an issue, but if you have a 64-bit system and more than 4GB of
76  * memory, you must have to make sure your network data buffers reside
77  * in the same 48-bit 'segment.'
78  *
79  * Special thanks to Ryan Fu at VIA Networking for providing documentation
80  * and sample NICs for testing.
81  */
82
83 #include "opt_polling.h"
84
85 #include <sys/param.h>
86 #include <sys/endian.h>
87 #include <sys/systm.h>
88 #include <sys/sockio.h>
89 #include <sys/mbuf.h>
90 #include <sys/malloc.h>
91 #include <sys/module.h>
92 #include <sys/kernel.h>
93 #include <sys/socket.h>
94 #include <sys/serialize.h>
95 #include <sys/proc.h>
96
97 #include <net/if.h>
98 #include <net/if_arp.h>
99 #include <net/ethernet.h>
100 #include <net/if_dl.h>
101 #include <net/if_media.h>
102 #include <net/ifq_var.h>
103 #include <net/if_types.h>
104 #include <net/vlan/if_vlan_var.h>
105
106 #include <net/bpf.h>
107
108 #include <machine/bus.h>
109 #include <machine/resource.h>
110 #include <sys/bus.h>
111 #include <sys/rman.h>
112
113 #include <dev/netif/mii_layer/mii.h>
114 #include <dev/netif/mii_layer/miivar.h>
115
116 #include <bus/pci/pcireg.h>
117 #include <bus/pci/pcivar.h>
118 #include <bus/pci/pcidevs.h>
119
120 #include "miibus_if.h"
121
122 #include <dev/netif/vge/if_vgereg.h>
123 #include <dev/netif/vge/if_vgevar.h>
124
125 #define VGE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
126
127 /*
128  * Various supported device vendors/types and their names.
129  */
130 static const struct vge_type vge_devs[] = {
131         { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT612X,
132           "VIA Networking Gigabit Ethernet" },
133         { 0, 0, NULL }
134 };
135
136 static int vge_probe            (device_t);
137 static int vge_attach           (device_t);
138 static int vge_detach           (device_t);
139
140 static int vge_encap            (struct vge_softc *, struct mbuf *, int);
141
142 static void vge_dma_map_addr    (void *, bus_dma_segment_t *, int, int);
143 static void vge_dma_map_rx_desc (void *, bus_dma_segment_t *, int,
144                                     bus_size_t, int);
145 static void vge_dma_map_tx_desc (void *, bus_dma_segment_t *, int,
146                                     bus_size_t, int);
147 static int vge_dma_alloc        (device_t);
148 static void vge_dma_free        (struct vge_softc *);
149 static int vge_newbuf           (struct vge_softc *, int, struct mbuf *);
150 static int vge_rx_list_init     (struct vge_softc *);
151 static int vge_tx_list_init     (struct vge_softc *);
152 #ifdef VGE_FIXUP_RX
153 static __inline void vge_fixup_rx
154                                 (struct mbuf *);
155 #endif
156 static void vge_rxeof           (struct vge_softc *, int);
157 static void vge_txeof           (struct vge_softc *);
158 static void vge_intr            (void *);
159 static void vge_tick            (struct vge_softc *);
160 static void vge_start           (struct ifnet *);
161 static int vge_ioctl            (struct ifnet *, u_long, caddr_t,
162                                  struct ucred *);
163 static void vge_init            (void *);
164 static void vge_stop            (struct vge_softc *);
165 static void vge_watchdog        (struct ifnet *);
166 static int vge_suspend          (device_t);
167 static int vge_resume           (device_t);
168 static void vge_shutdown        (device_t);
169 static int vge_ifmedia_upd      (struct ifnet *);
170 static void vge_ifmedia_sts     (struct ifnet *, struct ifmediareq *);
171
172 #ifdef VGE_EEPROM
173 static void vge_eeprom_getword  (struct vge_softc *, int, u_int16_t *);
174 #endif
175 static void vge_read_eeprom     (struct vge_softc *, uint8_t *, int, int, int);
176
177 static void vge_miipoll_start   (struct vge_softc *);
178 static void vge_miipoll_stop    (struct vge_softc *);
179 static int vge_miibus_readreg   (device_t, int, int);
180 static int vge_miibus_writereg  (device_t, int, int, int);
181 static void vge_miibus_statchg  (device_t);
182
183 static void vge_cam_clear       (struct vge_softc *);
184 static int vge_cam_set          (struct vge_softc *, uint8_t *);
185 static void vge_setmulti        (struct vge_softc *);
186 static void vge_reset           (struct vge_softc *);
187
188 #ifdef DEVICE_POLLING
189 static void     vge_poll(struct ifnet *, enum poll_cmd, int);
190 static void     vge_disable_intr(struct vge_softc *);
191 #endif
192 static void     vge_enable_intr(struct vge_softc *, uint32_t);
193
194 #define VGE_PCI_LOIO             0x10
195 #define VGE_PCI_LOMEM            0x14
196
197 static device_method_t vge_methods[] = {
198         /* Device interface */
199         DEVMETHOD(device_probe,         vge_probe),
200         DEVMETHOD(device_attach,        vge_attach),
201         DEVMETHOD(device_detach,        vge_detach),
202         DEVMETHOD(device_suspend,       vge_suspend),
203         DEVMETHOD(device_resume,        vge_resume),
204         DEVMETHOD(device_shutdown,      vge_shutdown),
205
206         /* bus interface */
207         DEVMETHOD(bus_print_child,      bus_generic_print_child),
208         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
209
210         /* MII interface */
211         DEVMETHOD(miibus_readreg,       vge_miibus_readreg),
212         DEVMETHOD(miibus_writereg,      vge_miibus_writereg),
213         DEVMETHOD(miibus_statchg,       vge_miibus_statchg),
214
215         { 0, 0 }
216 };
217
218 static driver_t vge_driver = {
219         "vge",
220         vge_methods,
221         sizeof(struct vge_softc)
222 };
223
224 static devclass_t vge_devclass;
225
226 DECLARE_DUMMY_MODULE(if_vge);
227 MODULE_DEPEND(if_vge, miibus, 1, 1, 1);
228 DRIVER_MODULE(if_vge, pci, vge_driver, vge_devclass, 0, 0);
229 DRIVER_MODULE(if_vge, cardbus, vge_driver, vge_devclass, 0, 0);
230 DRIVER_MODULE(miibus, vge, miibus_driver, miibus_devclass, 0, 0);
231
232 #ifdef VGE_EEPROM
233 /*
234  * Read a word of data stored in the EEPROM at address 'addr.'
235  */
236 static void
237 vge_eeprom_getword(struct vge_softc *sc, int addr, uint16_t dest)
238 {
239         uint16_t word = 0;
240         int i;
241
242         /*
243          * Enter EEPROM embedded programming mode. In order to
244          * access the EEPROM at all, we first have to set the
245          * EELOAD bit in the CHIPCFG2 register.
246          */
247         CSR_SETBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
248         CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
249
250         /* Select the address of the word we want to read */
251         CSR_WRITE_1(sc, VGE_EEADDR, addr);
252
253         /* Issue read command */
254         CSR_SETBIT_1(sc, VGE_EECMD, VGE_EECMD_ERD);
255
256         /* Wait for the done bit to be set. */
257         for (i = 0; i < VGE_TIMEOUT; i++) {
258                 if (CSR_READ_1(sc, VGE_EECMD) & VGE_EECMD_EDONE)
259                         break;
260         }
261         if (i == VGE_TIMEOUT) {
262                 device_printf(sc->vge_dev, "EEPROM read timed out\n");
263                 *dest = 0;
264                 return;
265         }
266
267         /* Read the result */
268         word = CSR_READ_2(sc, VGE_EERDDAT);
269
270         /* Turn off EEPROM access mode. */
271         CSR_CLRBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
272         CSR_CLRBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
273
274         *dest = word;
275 }
276 #endif
277
278 /*
279  * Read a sequence of words from the EEPROM.
280  */
281 static void
282 vge_read_eeprom(struct vge_softc *sc, uint8_t *dest, int off, int cnt, int swap)
283 {
284         int i;
285 #ifdef VGE_EEPROM
286         uint16_t word = 0, *ptr;
287
288         for (i = 0; i < cnt; i++) {
289                 vge_eeprom_getword(sc, off + i, &word);
290                 ptr = (uint16_t *)(dest + (i * 2));
291                 if (swap)
292                         *ptr = ntohs(word);
293                 else
294                         *ptr = word;
295         }
296 #else
297         for (i = 0; i < ETHER_ADDR_LEN; i++)
298                 dest[i] = CSR_READ_1(sc, VGE_PAR0 + i);
299 #endif
300 }
301
302 static void
303 vge_miipoll_stop(struct vge_softc *sc)
304 {
305         int i;
306
307         CSR_WRITE_1(sc, VGE_MIICMD, 0);
308
309         for (i = 0; i < VGE_TIMEOUT; i++) {
310                 DELAY(1);
311                 if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
312                         break;
313         }
314         if (i == VGE_TIMEOUT)
315                 if_printf(&sc->arpcom.ac_if, "failed to idle MII autopoll\n");
316 }
317
318 static void
319 vge_miipoll_start(struct vge_softc *sc)
320 {
321         int i;
322
323         /* First, make sure we're idle. */
324         CSR_WRITE_1(sc, VGE_MIICMD, 0);
325         CSR_WRITE_1(sc, VGE_MIIADDR, VGE_MIIADDR_SWMPL);
326
327         for (i = 0; i < VGE_TIMEOUT; i++) {
328                 DELAY(1);
329                 if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
330                         break;
331         }
332         if (i == VGE_TIMEOUT) {
333                 if_printf(&sc->arpcom.ac_if, "failed to idle MII autopoll\n");
334                 return;
335         }
336
337         /* Now enable auto poll mode. */
338         CSR_WRITE_1(sc, VGE_MIICMD, VGE_MIICMD_MAUTO);
339
340         /* And make sure it started. */
341         for (i = 0; i < VGE_TIMEOUT; i++) {
342                 DELAY(1);
343                 if ((CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) == 0)
344                         break;
345         }
346         if (i == VGE_TIMEOUT)
347                 if_printf(&sc->arpcom.ac_if, "failed to start MII autopoll\n");
348 }
349
350 static int
351 vge_miibus_readreg(device_t dev, int phy, int reg)
352 {
353         struct vge_softc *sc;
354         int i;
355         uint16_t rval = 0;
356
357         sc = device_get_softc(dev);
358
359         if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
360                 return(0);
361
362         vge_miipoll_stop(sc);
363
364         /* Specify the register we want to read. */
365         CSR_WRITE_1(sc, VGE_MIIADDR, reg);
366
367         /* Issue read command. */
368         CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_RCMD);
369
370         /* Wait for the read command bit to self-clear. */
371         for (i = 0; i < VGE_TIMEOUT; i++) {
372                 DELAY(1);
373                 if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_RCMD) == 0)
374                         break;
375         }
376         if (i == VGE_TIMEOUT)
377                 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
378         else
379                 rval = CSR_READ_2(sc, VGE_MIIDATA);
380
381         vge_miipoll_start(sc);
382
383         return (rval);
384 }
385
386 static int
387 vge_miibus_writereg(device_t dev, int phy, int reg, int data)
388 {
389         struct vge_softc *sc;
390         int i, rval = 0;
391
392         sc = device_get_softc(dev);
393
394         if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
395                 return(0);
396
397         vge_miipoll_stop(sc);
398
399         /* Specify the register we want to write. */
400         CSR_WRITE_1(sc, VGE_MIIADDR, reg);
401
402         /* Specify the data we want to write. */
403         CSR_WRITE_2(sc, VGE_MIIDATA, data);
404
405         /* Issue write command. */
406         CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_WCMD);
407
408         /* Wait for the write command bit to self-clear. */
409         for (i = 0; i < VGE_TIMEOUT; i++) {
410                 DELAY(1);
411                 if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_WCMD) == 0)
412                         break;
413         }
414         if (i == VGE_TIMEOUT) {
415                 if_printf(&sc->arpcom.ac_if, "MII write timed out\n");
416                 rval = EIO;
417         }
418
419         vge_miipoll_start(sc);
420
421         return (rval);
422 }
423
424 static void
425 vge_cam_clear(struct vge_softc *sc)
426 {
427         int i;
428
429         /*
430          * Turn off all the mask bits. This tells the chip
431          * that none of the entries in the CAM filter are valid.
432          * desired entries will be enabled as we fill the filter in.
433          */
434         CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
435         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
436         CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE);
437         for (i = 0; i < 8; i++)
438                 CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
439
440         /* Clear the VLAN filter too. */
441         CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|VGE_CAMADDR_AVSEL|0);
442         for (i = 0; i < 8; i++)
443                 CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
444
445         CSR_WRITE_1(sc, VGE_CAMADDR, 0);
446         CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
447         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
448
449         sc->vge_camidx = 0;
450 }
451
452 static int
453 vge_cam_set(struct vge_softc *sc, uint8_t *addr)
454 {
455         int i, error = 0;
456
457         if (sc->vge_camidx == VGE_CAM_MAXADDRS)
458                 return(ENOSPC);
459
460         /* Select the CAM data page. */
461         CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
462         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMDATA);
463
464         /* Set the filter entry we want to update and enable writing. */
465         CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|sc->vge_camidx);
466
467         /* Write the address to the CAM registers */
468         for (i = 0; i < ETHER_ADDR_LEN; i++)
469                 CSR_WRITE_1(sc, VGE_CAM0 + i, addr[i]);
470
471         /* Issue a write command. */
472         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_WRITE);
473
474         /* Wake for it to clear. */
475         for (i = 0; i < VGE_TIMEOUT; i++) {
476                 DELAY(1);
477                 if ((CSR_READ_1(sc, VGE_CAMCTL) & VGE_CAMCTL_WRITE) == 0)
478                         break;
479         }
480         if (i == VGE_TIMEOUT) {
481                 if_printf(&sc->arpcom.ac_if, "setting CAM filter failed\n");
482                 error = EIO;
483                 goto fail;
484         }
485
486         /* Select the CAM mask page. */
487         CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
488         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
489
490         /* Set the mask bit that enables this filter. */
491         CSR_SETBIT_1(sc, VGE_CAM0 + (sc->vge_camidx/8),
492             1<<(sc->vge_camidx & 7));
493
494         sc->vge_camidx++;
495
496 fail:
497         /* Turn off access to CAM. */
498         CSR_WRITE_1(sc, VGE_CAMADDR, 0);
499         CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
500         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
501
502         return (error);
503 }
504
505 /*
506  * Program the multicast filter. We use the 64-entry CAM filter
507  * for perfect filtering. If there's more than 64 multicast addresses,
508  * we use the hash filter insted.
509  */
510 static void
511 vge_setmulti(struct vge_softc *sc)
512 {
513         struct ifnet *ifp = &sc->arpcom.ac_if;
514         int error = 0;
515         struct ifmultiaddr *ifma;
516         uint32_t h, hashes[2] = { 0, 0 };
517
518         /* First, zot all the multicast entries. */
519         vge_cam_clear(sc);
520         CSR_WRITE_4(sc, VGE_MAR0, 0);
521         CSR_WRITE_4(sc, VGE_MAR1, 0);
522
523         /*
524          * If the user wants allmulti or promisc mode, enable reception
525          * of all multicast frames.
526          */
527         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
528                 CSR_WRITE_4(sc, VGE_MAR0, 0xFFFFFFFF);
529                 CSR_WRITE_4(sc, VGE_MAR1, 0xFFFFFFFF);
530                 return;
531         }
532
533         /* Now program new ones */
534         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
535                 if (ifma->ifma_addr->sa_family != AF_LINK)
536                         continue;
537                 error = vge_cam_set(sc,
538                     LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
539                 if (error)
540                         break;
541         }
542
543         /* If there were too many addresses, use the hash filter. */
544         if (error) {
545                 vge_cam_clear(sc);
546
547                 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
548                         if (ifma->ifma_addr->sa_family != AF_LINK)
549                                 continue;
550                         h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
551                             ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
552                         if (h < 32)
553                                 hashes[0] |= (1 << h);
554                         else
555                                 hashes[1] |= (1 << (h - 32));
556                 }
557
558                 CSR_WRITE_4(sc, VGE_MAR0, hashes[0]);
559                 CSR_WRITE_4(sc, VGE_MAR1, hashes[1]);
560         }
561 }
562
563 static void
564 vge_reset(struct vge_softc *sc)
565 {
566         int i;
567
568         CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_SOFTRESET);
569
570         for (i = 0; i < VGE_TIMEOUT; i++) {
571                 DELAY(5);
572                 if ((CSR_READ_1(sc, VGE_CRS1) & VGE_CR1_SOFTRESET) == 0)
573                         break;
574         }
575
576         if (i == VGE_TIMEOUT) {
577                 if_printf(&sc->arpcom.ac_if, "soft reset timed out");
578                 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_STOP_FORCE);
579                 DELAY(2000);
580         }
581
582         DELAY(5000);
583
584         CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_RELOAD);
585
586         for (i = 0; i < VGE_TIMEOUT; i++) {
587                 DELAY(5);
588                 if ((CSR_READ_1(sc, VGE_EECSR) & VGE_EECSR_RELOAD) == 0)
589                         break;
590         }
591         if (i == VGE_TIMEOUT) {
592                 if_printf(&sc->arpcom.ac_if, "EEPROM reload timed out\n");
593                 return;
594         }
595
596         CSR_CLRBIT_1(sc, VGE_CHIPCFG0, VGE_CHIPCFG0_PACPI);
597 }
598
599 /*
600  * Probe for a VIA gigabit chip. Check the PCI vendor and device
601  * IDs against our list and return a device name if we find a match.
602  */
603 static int
604 vge_probe(device_t dev)
605 {
606         const struct vge_type *t;
607         uint16_t did, vid;
608
609         did = pci_get_device(dev);
610         vid = pci_get_vendor(dev);
611         for (t = vge_devs; t->vge_name != NULL; ++t) {
612                 if (vid == t->vge_vid && did == t->vge_did) {
613                         device_set_desc(dev, t->vge_name);
614                         return 0;
615                 }
616         }
617         return (ENXIO);
618 }
619
620 static void
621 vge_dma_map_rx_desc(void *arg, bus_dma_segment_t *segs, int nseg,
622                     bus_size_t mapsize, int error)
623 {
624
625         struct vge_dmaload_arg *ctx;
626         struct vge_rx_desc *d = NULL;
627
628         if (error)
629                 return;
630
631         ctx = arg;
632
633         /* Signal error to caller if there's too many segments */
634         if (nseg > ctx->vge_maxsegs) {
635                 ctx->vge_maxsegs = 0;
636                 return;
637         }
638
639         /*
640          * Map the segment array into descriptors.
641          */
642         d = &ctx->sc->vge_ldata.vge_rx_list[ctx->vge_idx];
643
644         /* If this descriptor is still owned by the chip, bail. */
645         if (le32toh(d->vge_sts) & VGE_RDSTS_OWN) {
646                 if_printf(&ctx->sc->arpcom.ac_if,
647                           "tried to map busy descriptor\n");
648                 ctx->vge_maxsegs = 0;
649                 return;
650         }
651
652         d->vge_buflen = htole16(VGE_BUFLEN(segs[0].ds_len) | VGE_RXDESC_I);
653         d->vge_addrlo = htole32(VGE_ADDR_LO(segs[0].ds_addr));
654         d->vge_addrhi = htole16(VGE_ADDR_HI(segs[0].ds_addr) & 0xFFFF);
655         d->vge_sts = 0;
656         d->vge_ctl = 0;
657
658         ctx->vge_maxsegs = 1;
659 }
660
661 static void
662 vge_dma_map_tx_desc(void *arg, bus_dma_segment_t *segs, int nseg,
663                     bus_size_t mapsize, int error)
664 {
665         struct vge_dmaload_arg *ctx;
666         struct vge_tx_desc *d = NULL;
667         struct vge_tx_frag *f;
668         int i = 0;
669
670         if (error)
671                 return;
672
673         ctx = arg;
674
675         /* Signal error to caller if there's too many segments */
676         if (nseg > ctx->vge_maxsegs) {
677                 ctx->vge_maxsegs = 0;
678                 return;
679         }
680
681         /* Map the segment array into descriptors. */
682         d = &ctx->sc->vge_ldata.vge_tx_list[ctx->vge_idx];
683
684         /* If this descriptor is still owned by the chip, bail. */
685         if (le32toh(d->vge_sts) & VGE_TDSTS_OWN) {
686                 ctx->vge_maxsegs = 0;
687                 return;
688         }
689
690         for (i = 0; i < nseg; i++) {
691                 f = &d->vge_frag[i];
692                 f->vge_buflen = htole16(VGE_BUFLEN(segs[i].ds_len));
693                 f->vge_addrlo = htole32(VGE_ADDR_LO(segs[i].ds_addr));
694                 f->vge_addrhi = htole16(VGE_ADDR_HI(segs[i].ds_addr) & 0xFFFF);
695         }
696
697         /* Argh. This chip does not autopad short frames */
698         if (ctx->vge_m0->m_pkthdr.len < VGE_MIN_FRAMELEN) {
699                 f = &d->vge_frag[i];
700                 f->vge_buflen = htole16(VGE_BUFLEN(VGE_MIN_FRAMELEN -
701                     ctx->vge_m0->m_pkthdr.len));
702                 f->vge_addrlo = htole32(VGE_ADDR_LO(segs[0].ds_addr));
703                 f->vge_addrhi = htole16(VGE_ADDR_HI(segs[0].ds_addr) & 0xFFFF);
704                 ctx->vge_m0->m_pkthdr.len = VGE_MIN_FRAMELEN;
705                 i++;
706         }
707
708         /*
709          * When telling the chip how many segments there are, we
710          * must use nsegs + 1 instead of just nsegs. Darned if I
711          * know why.
712          */
713         i++;
714
715         d->vge_sts = ctx->vge_m0->m_pkthdr.len << 16;
716         d->vge_ctl = ctx->vge_flags|(i << 28)|VGE_TD_LS_NORM;
717
718         if (ctx->vge_m0->m_pkthdr.len > ETHERMTU + ETHER_HDR_LEN)
719                 d->vge_ctl |= VGE_TDCTL_JUMBO;
720
721         ctx->vge_maxsegs = nseg;
722 }
723
724 /*
725  * Map a single buffer address.
726  */
727
728 static void
729 vge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
730 {
731         if (error)
732                 return;
733
734         KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
735         *((bus_addr_t *)arg) = segs->ds_addr;
736 }
737
738 static int
739 vge_dma_alloc(device_t dev)
740 {
741         struct vge_softc *sc = device_get_softc(dev);
742         int error, nseg, i, tx_pos = 0, rx_pos = 0;
743
744         /*
745          * Allocate the parent bus DMA tag appropriate for PCI.
746          */
747 #define VGE_NSEG_NEW 32
748         error = bus_dma_tag_create(NULL,        /* parent */
749                         1, 0,                   /* alignment, boundary */
750                         BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
751                         BUS_SPACE_MAXADDR,      /* highaddr */
752                         NULL, NULL,             /* filter, filterarg */
753                         MAXBSIZE, VGE_NSEG_NEW, /* maxsize, nsegments */
754                         BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
755                         BUS_DMA_ALLOCNOW,       /* flags */
756                         &sc->vge_parent_tag);
757         if (error) {
758                 device_printf(dev, "can't create parent dma tag\n");
759                 return error;
760         }
761
762         /*
763          * Allocate map for RX mbufs.
764          */
765         nseg = 32;
766         error = bus_dma_tag_create(sc->vge_parent_tag, ETHER_ALIGN, 0,
767                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
768                                    NULL, NULL,
769                                    MCLBYTES * nseg, nseg, MCLBYTES,
770                                    BUS_DMA_ALLOCNOW, &sc->vge_ldata.vge_mtag);
771         if (error) {
772                 device_printf(dev, "could not allocate mbuf dma tag\n");
773                 return error;
774         }
775
776         /*
777          * Allocate map for TX descriptor list.
778          */
779         error = bus_dma_tag_create(sc->vge_parent_tag, VGE_RING_ALIGN, 0,
780                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
781                                    NULL, NULL,
782                                    VGE_TX_LIST_SZ, 1, VGE_TX_LIST_SZ,
783                                    BUS_DMA_ALLOCNOW,
784                                    &sc->vge_ldata.vge_tx_list_tag);
785         if (error) {
786                 device_printf(dev, "could not allocate tx list dma tag\n");
787                 return error;
788         }
789
790         /* Allocate DMA'able memory for the TX ring */
791         error = bus_dmamem_alloc(sc->vge_ldata.vge_tx_list_tag,
792                                  (void **)&sc->vge_ldata.vge_tx_list,
793                                  BUS_DMA_WAITOK | BUS_DMA_ZERO,
794                                  &sc->vge_ldata.vge_tx_list_map);
795         if (error) {
796                 device_printf(dev, "could not allocate tx list dma memory\n");
797                 return error;
798         }
799
800         /* Load the map for the TX ring. */
801         error = bus_dmamap_load(sc->vge_ldata.vge_tx_list_tag,
802                                 sc->vge_ldata.vge_tx_list_map,
803                                 sc->vge_ldata.vge_tx_list, VGE_TX_LIST_SZ,
804                                 vge_dma_map_addr,
805                                 &sc->vge_ldata.vge_tx_list_addr,
806                                 BUS_DMA_WAITOK);
807         if (error) {
808                 device_printf(dev, "could not load tx list\n");
809                 bus_dmamem_free(sc->vge_ldata.vge_tx_list_tag, 
810                                 sc->vge_ldata.vge_tx_list,
811                                 sc->vge_ldata.vge_tx_list_map);
812                 sc->vge_ldata.vge_tx_list = NULL;
813                 return error;
814         }
815
816         /* Create DMA maps for TX buffers */
817         for (i = 0; i < VGE_TX_DESC_CNT; i++) {
818                 error = bus_dmamap_create(sc->vge_ldata.vge_mtag, 0,
819                                           &sc->vge_ldata.vge_tx_dmamap[i]);
820                 if (error) {
821                         device_printf(dev, "can't create DMA map for TX\n");
822                         tx_pos = i;
823                         goto map_fail;
824                 }
825         }
826         tx_pos = VGE_TX_DESC_CNT;
827
828         /*
829          * Allocate map for RX descriptor list.
830          */
831         error = bus_dma_tag_create(sc->vge_parent_tag, VGE_RING_ALIGN, 0,
832                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
833                                    NULL, NULL,
834                                    VGE_TX_LIST_SZ, 1, VGE_TX_LIST_SZ,
835                                    BUS_DMA_ALLOCNOW,
836                                    &sc->vge_ldata.vge_rx_list_tag);
837         if (error) {
838                 device_printf(dev, "could not allocate rx list dma tag\n");
839                 return error;
840         }
841
842         /* Allocate DMA'able memory for the RX ring */
843         error = bus_dmamem_alloc(sc->vge_ldata.vge_rx_list_tag,
844                                  (void **)&sc->vge_ldata.vge_rx_list,
845                                  BUS_DMA_WAITOK | BUS_DMA_ZERO,
846                                  &sc->vge_ldata.vge_rx_list_map);
847         if (error) {
848                 device_printf(dev, "could not allocate rx list dma memory\n");
849                 return error;
850         }
851
852         /* Load the map for the RX ring. */
853         error = bus_dmamap_load(sc->vge_ldata.vge_rx_list_tag,
854                                 sc->vge_ldata.vge_rx_list_map,
855                                 sc->vge_ldata.vge_rx_list, VGE_TX_LIST_SZ,
856                                 vge_dma_map_addr,
857                                 &sc->vge_ldata.vge_rx_list_addr,
858                                 BUS_DMA_WAITOK);
859         if (error) {
860                 device_printf(dev, "could not load rx list\n");
861                 bus_dmamem_free(sc->vge_ldata.vge_rx_list_tag,
862                                 sc->vge_ldata.vge_rx_list,
863                                 sc->vge_ldata.vge_rx_list_map);
864                 sc->vge_ldata.vge_rx_list = NULL;
865                 return error;
866         }
867
868         /* Create DMA maps for RX buffers */
869         for (i = 0; i < VGE_RX_DESC_CNT; i++) {
870                 error = bus_dmamap_create(sc->vge_ldata.vge_mtag, 0,
871                                           &sc->vge_ldata.vge_rx_dmamap[i]);
872                 if (error) {
873                         device_printf(dev, "can't create DMA map for RX\n");
874                         rx_pos = i;
875                         goto map_fail;
876                 }
877         }
878         return (0);
879
880 map_fail:
881         for (i = 0; i < tx_pos; ++i) {
882                 error = bus_dmamap_destroy(sc->vge_ldata.vge_mtag,
883                                            sc->vge_ldata.vge_tx_dmamap[i]);
884         }
885         for (i = 0; i < rx_pos; ++i) {
886                 error = bus_dmamap_destroy(sc->vge_ldata.vge_mtag,
887                                            sc->vge_ldata.vge_rx_dmamap[i]);
888         }
889         bus_dma_tag_destroy(sc->vge_ldata.vge_mtag);
890         sc->vge_ldata.vge_mtag = NULL;
891
892         return error;
893 }
894
895 static void
896 vge_dma_free(struct vge_softc *sc)
897 {
898         /* Unload and free the RX DMA ring memory and map */
899         if (sc->vge_ldata.vge_rx_list_tag) {
900                 bus_dmamap_unload(sc->vge_ldata.vge_rx_list_tag,
901                                   sc->vge_ldata.vge_rx_list_map);
902                 bus_dmamem_free(sc->vge_ldata.vge_rx_list_tag,
903                                 sc->vge_ldata.vge_rx_list,
904                                 sc->vge_ldata.vge_rx_list_map);
905         }
906
907         if (sc->vge_ldata.vge_rx_list_tag)
908                 bus_dma_tag_destroy(sc->vge_ldata.vge_rx_list_tag);
909
910         /* Unload and free the TX DMA ring memory and map */
911         if (sc->vge_ldata.vge_tx_list_tag) {
912                 bus_dmamap_unload(sc->vge_ldata.vge_tx_list_tag,
913                                   sc->vge_ldata.vge_tx_list_map);
914                 bus_dmamem_free(sc->vge_ldata.vge_tx_list_tag,
915                                 sc->vge_ldata.vge_tx_list,
916                                 sc->vge_ldata.vge_tx_list_map);
917         }
918
919         if (sc->vge_ldata.vge_tx_list_tag)
920                 bus_dma_tag_destroy(sc->vge_ldata.vge_tx_list_tag);
921
922         /* Destroy all the RX and TX buffer maps */
923         if (sc->vge_ldata.vge_mtag) {
924                 int i;
925
926                 for (i = 0; i < VGE_TX_DESC_CNT; i++) {
927                         bus_dmamap_destroy(sc->vge_ldata.vge_mtag,
928                                            sc->vge_ldata.vge_tx_dmamap[i]);
929                 }
930                 for (i = 0; i < VGE_RX_DESC_CNT; i++) {
931                         bus_dmamap_destroy(sc->vge_ldata.vge_mtag,
932                                            sc->vge_ldata.vge_rx_dmamap[i]);
933                 }
934                 bus_dma_tag_destroy(sc->vge_ldata.vge_mtag);
935         }
936
937         if (sc->vge_parent_tag)
938                 bus_dma_tag_destroy(sc->vge_parent_tag);
939 }
940
941 /*
942  * Attach the interface. Allocate softc structures, do ifmedia
943  * setup and ethernet/BPF attach.
944  */
945 static int
946 vge_attach(device_t dev)
947 {
948         uint8_t eaddr[ETHER_ADDR_LEN];
949         struct vge_softc *sc;
950         struct ifnet *ifp;
951         int error = 0;
952
953         sc = device_get_softc(dev);
954         ifp = &sc->arpcom.ac_if;
955
956         /* Initialize if_xname early, so if_printf() can be used */
957         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
958
959         /*
960          * Map control/status registers.
961          */
962         pci_enable_busmaster(dev);
963
964         sc->vge_res_rid = VGE_PCI_LOMEM;
965         sc->vge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
966                                              &sc->vge_res_rid, RF_ACTIVE);
967         if (sc->vge_res == NULL) {
968                 device_printf(dev, "couldn't map ports/memory\n");
969                 return ENXIO;
970         }
971
972         sc->vge_btag = rman_get_bustag(sc->vge_res);
973         sc->vge_bhandle = rman_get_bushandle(sc->vge_res);
974
975         /* Allocate interrupt */
976         sc->vge_irq_rid = 0;
977         sc->vge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->vge_irq_rid,
978                                              RF_SHAREABLE | RF_ACTIVE);
979         if (sc->vge_irq == NULL) {
980                 device_printf(dev, "couldn't map interrupt\n");
981                 error = ENXIO;
982                 goto fail;
983         }
984
985         /* Reset the adapter. */
986         vge_reset(sc);
987
988         /*
989          * Get station address from the EEPROM.
990          */
991         vge_read_eeprom(sc, eaddr, VGE_EE_EADDR, 3, 0);
992
993         /* Allocate DMA related stuffs */
994         error = vge_dma_alloc(dev);
995         if (error)
996                 goto fail;
997
998         /* Do MII setup */
999         error = mii_phy_probe(dev, &sc->vge_miibus, vge_ifmedia_upd,
1000                               vge_ifmedia_sts);
1001         if (error) {
1002                 device_printf(dev, "MII without any phy!\n");
1003                 goto fail;
1004         }
1005
1006         ifp->if_softc = sc;
1007         ifp->if_mtu = ETHERMTU;
1008         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1009         ifp->if_init = vge_init;
1010         ifp->if_start = vge_start;
1011         ifp->if_watchdog = vge_watchdog;
1012         ifp->if_ioctl = vge_ioctl;
1013 #ifdef DEVICE_POLLING
1014         ifp->if_poll = vge_poll;
1015 #endif
1016         ifp->if_hwassist = VGE_CSUM_FEATURES;
1017         ifp->if_capabilities = IFCAP_VLAN_MTU |
1018                                IFCAP_HWCSUM |
1019                                IFCAP_VLAN_HWTAGGING;
1020         ifp->if_capenable = ifp->if_capabilities;
1021         ifq_set_maxlen(&ifp->if_snd, VGE_IFQ_MAXLEN);
1022         ifq_set_ready(&ifp->if_snd);
1023
1024         /*
1025          * Call MI attach routine.
1026          */
1027         ether_ifattach(ifp, eaddr, NULL);
1028
1029         /* Hook interrupt last to avoid having to lock softc */
1030         error = bus_setup_intr(dev, sc->vge_irq, INTR_MPSAFE, vge_intr, sc,
1031                                &sc->vge_intrhand, ifp->if_serializer);
1032         if (error) {
1033                 device_printf(dev, "couldn't set up irq\n");
1034                 ether_ifdetach(ifp);
1035                 goto fail;
1036         }
1037
1038         return 0;
1039 fail:
1040         vge_detach(dev);
1041         return error;
1042 }
1043
1044 /*
1045  * Shutdown hardware and free up resources. This can be called any
1046  * time after the mutex has been initialized. It is called in both
1047  * the error case in attach and the normal detach case so it needs
1048  * to be careful about only freeing resources that have actually been
1049  * allocated.
1050  */
1051 static int
1052 vge_detach(device_t dev)
1053 {
1054         struct vge_softc *sc = device_get_softc(dev);
1055         struct ifnet *ifp = &sc->arpcom.ac_if;
1056
1057         /* These should only be active if attach succeeded */
1058         if (device_is_attached(dev)) {
1059                 lwkt_serialize_enter(ifp->if_serializer);
1060
1061                 vge_stop(sc);
1062                 bus_teardown_intr(dev, sc->vge_irq, sc->vge_intrhand);
1063                 /*
1064                  * Force off the IFF_UP flag here, in case someone
1065                  * still had a BPF descriptor attached to this
1066                  * interface. If they do, ether_ifattach() will cause
1067                  * the BPF code to try and clear the promisc mode
1068                  * flag, which will bubble down to vge_ioctl(),
1069                  * which will try to call vge_init() again. This will
1070                  * turn the NIC back on and restart the MII ticker,
1071                  * which will panic the system when the kernel tries
1072                  * to invoke the vge_tick() function that isn't there
1073                  * anymore.
1074                  */
1075                 ifp->if_flags &= ~IFF_UP;
1076
1077                 lwkt_serialize_exit(ifp->if_serializer);
1078
1079                 ether_ifdetach(ifp);
1080         }
1081
1082         if (sc->vge_miibus)
1083                 device_delete_child(dev, sc->vge_miibus);
1084         bus_generic_detach(dev);
1085
1086         if (sc->vge_irq) {
1087                 bus_release_resource(dev, SYS_RES_IRQ, sc->vge_irq_rid,
1088                                      sc->vge_irq);
1089         }
1090
1091         if (sc->vge_res) {
1092                 bus_release_resource(dev, SYS_RES_MEMORY, sc->vge_res_rid,
1093                                      sc->vge_res);
1094         }
1095
1096         vge_dma_free(sc);
1097         return (0);
1098 }
1099
1100 static int
1101 vge_newbuf(struct vge_softc *sc, int idx, struct mbuf *m)
1102 {
1103         struct vge_dmaload_arg arg;
1104         struct mbuf *n = NULL;
1105         int i, error;
1106
1107         if (m == NULL) {
1108                 n = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1109                 if (n == NULL)
1110                         return (ENOBUFS);
1111                 m = n;
1112         } else {
1113                 m->m_data = m->m_ext.ext_buf;
1114         }
1115
1116
1117 #ifdef VGE_FIXUP_RX
1118         /*
1119          * This is part of an evil trick to deal with non-x86 platforms.
1120          * The VIA chip requires RX buffers to be aligned on 32-bit
1121          * boundaries, but that will hose non-x86 machines. To get around
1122          * this, we leave some empty space at the start of each buffer
1123          * and for non-x86 hosts, we copy the buffer back two bytes
1124          * to achieve word alignment. This is slightly more efficient
1125          * than allocating a new buffer, copying the contents, and
1126          * discarding the old buffer.
1127          */
1128         m->m_len = m->m_pkthdr.len = MCLBYTES - VGE_ETHER_ALIGN;
1129         m_adj(m, VGE_ETHER_ALIGN);
1130 #else
1131         m->m_len = m->m_pkthdr.len = MCLBYTES;
1132 #endif
1133
1134         arg.sc = sc;
1135         arg.vge_idx = idx;
1136         arg.vge_maxsegs = 1;
1137         arg.vge_flags = 0;
1138
1139         error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag,
1140                                      sc->vge_ldata.vge_rx_dmamap[idx], m,
1141                                      vge_dma_map_rx_desc, &arg, BUS_DMA_NOWAIT);
1142         if (error || arg.vge_maxsegs != 1) {
1143                 if (n != NULL)
1144                         m_freem(n);
1145                 return (ENOMEM);
1146         }
1147
1148         /*
1149          * Note: the manual fails to document the fact that for
1150          * proper opration, the driver needs to replentish the RX
1151          * DMA ring 4 descriptors at a time (rather than one at a
1152          * time, like most chips). We can allocate the new buffers
1153          * but we should not set the OWN bits until we're ready
1154          * to hand back 4 of them in one shot.
1155          */
1156
1157 #define VGE_RXCHUNK 4
1158         sc->vge_rx_consumed++;
1159         if (sc->vge_rx_consumed == VGE_RXCHUNK) {
1160                 for (i = idx; i != idx - sc->vge_rx_consumed; i--) {
1161                         sc->vge_ldata.vge_rx_list[i].vge_sts |=
1162                             htole32(VGE_RDSTS_OWN);
1163                 }
1164                 sc->vge_rx_consumed = 0;
1165         }
1166
1167         sc->vge_ldata.vge_rx_mbuf[idx] = m;
1168
1169         bus_dmamap_sync(sc->vge_ldata.vge_mtag,
1170                         sc->vge_ldata.vge_rx_dmamap[idx], BUS_DMASYNC_PREREAD);
1171
1172         return (0);
1173 }
1174
1175 static int
1176 vge_tx_list_init(sc)
1177         struct vge_softc                *sc;
1178 {
1179         bzero ((char *)sc->vge_ldata.vge_tx_list, VGE_TX_LIST_SZ);
1180         bzero ((char *)&sc->vge_ldata.vge_tx_mbuf,
1181             (VGE_TX_DESC_CNT * sizeof(struct mbuf *)));
1182
1183         bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag,
1184             sc->vge_ldata.vge_tx_list_map, BUS_DMASYNC_PREWRITE);
1185         sc->vge_ldata.vge_tx_prodidx = 0;
1186         sc->vge_ldata.vge_tx_considx = 0;
1187         sc->vge_ldata.vge_tx_free = VGE_TX_DESC_CNT;
1188
1189         return (0);
1190 }
1191
1192 static int
1193 vge_rx_list_init(struct vge_softc *sc)
1194 {
1195         int i;
1196
1197         bzero(sc->vge_ldata.vge_rx_list, VGE_RX_LIST_SZ);
1198         bzero(&sc->vge_ldata.vge_rx_mbuf,
1199               VGE_RX_DESC_CNT * sizeof(struct mbuf *));
1200
1201         sc->vge_rx_consumed = 0;
1202
1203         for (i = 0; i < VGE_RX_DESC_CNT; i++) {
1204                 if (vge_newbuf(sc, i, NULL) == ENOBUFS)
1205                         return (ENOBUFS);
1206         }
1207
1208         /* Flush the RX descriptors */
1209         bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag,
1210                         sc->vge_ldata.vge_rx_list_map,
1211                         BUS_DMASYNC_PREWRITE);
1212
1213         sc->vge_ldata.vge_rx_prodidx = 0;
1214         sc->vge_rx_consumed = 0;
1215         sc->vge_head = sc->vge_tail = NULL;
1216         return (0);
1217 }
1218
1219 #ifdef VGE_FIXUP_RX
1220 static __inline void
1221 vge_fixup_rx(struct mbuf *m)
1222 {
1223         uint16_t *src, *dst;
1224         int i;
1225
1226         src = mtod(m, uint16_t *);
1227         dst = src - 1;
1228
1229         for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1230                 *dst++ = *src++;
1231
1232         m->m_data -= ETHER_ALIGN;
1233 }
1234 #endif
1235
1236 /*
1237  * RX handler. We support the reception of jumbo frames that have
1238  * been fragmented across multiple 2K mbuf cluster buffers.
1239  */
1240 static void
1241 vge_rxeof(struct vge_softc *sc, int count)
1242 {
1243         struct ifnet *ifp = &sc->arpcom.ac_if;
1244         struct mbuf *m;
1245         int i, total_len, lim = 0;
1246         struct vge_rx_desc *cur_rx;
1247         uint32_t rxstat, rxctl;
1248
1249         ASSERT_SERIALIZED(ifp->if_serializer);
1250
1251         i = sc->vge_ldata.vge_rx_prodidx;
1252
1253         /* Invalidate the descriptor memory */
1254
1255         bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag,
1256                         sc->vge_ldata.vge_rx_list_map, BUS_DMASYNC_POSTREAD);
1257
1258         while (!VGE_OWN(&sc->vge_ldata.vge_rx_list[i])) {
1259 #ifdef DEVICE_POLLING
1260                 if (count >= 0 && count-- == 0)
1261                         break;
1262 #endif
1263
1264                 cur_rx = &sc->vge_ldata.vge_rx_list[i];
1265                 m = sc->vge_ldata.vge_rx_mbuf[i];
1266                 total_len = VGE_RXBYTES(cur_rx);
1267                 rxstat = le32toh(cur_rx->vge_sts);
1268                 rxctl = le32toh(cur_rx->vge_ctl);
1269
1270                 /* Invalidate the RX mbuf and unload its map */
1271                 bus_dmamap_sync(sc->vge_ldata.vge_mtag,
1272                                 sc->vge_ldata.vge_rx_dmamap[i],
1273                                 BUS_DMASYNC_POSTWRITE);
1274                 bus_dmamap_unload(sc->vge_ldata.vge_mtag,
1275                                   sc->vge_ldata.vge_rx_dmamap[i]);
1276
1277                 /*
1278                  * If the 'start of frame' bit is set, this indicates
1279                  * either the first fragment in a multi-fragment receive,
1280                  * or an intermediate fragment. Either way, we want to
1281                  * accumulate the buffers.
1282                  */
1283                 if (rxstat & VGE_RXPKT_SOF) {
1284                         m->m_len = MCLBYTES - VGE_ETHER_ALIGN;
1285                         if (sc->vge_head == NULL) {
1286                                 sc->vge_head = sc->vge_tail = m;
1287                         } else {
1288                                 m->m_flags &= ~M_PKTHDR;
1289                                 sc->vge_tail->m_next = m;
1290                                 sc->vge_tail = m;
1291                         }
1292                         vge_newbuf(sc, i, NULL);
1293                         VGE_RX_DESC_INC(i);
1294                         continue;
1295                 }
1296
1297                 /*
1298                  * Bad/error frames will have the RXOK bit cleared.
1299                  * However, there's one error case we want to allow:
1300                  * if a VLAN tagged frame arrives and the chip can't
1301                  * match it against the CAM filter, it considers this
1302                  * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
1303                  * We don't want to drop the frame though: our VLAN
1304                  * filtering is done in software.
1305                  */
1306                 if (!(rxstat & VGE_RDSTS_RXOK) && !(rxstat & VGE_RDSTS_VIDM) &&
1307                     !(rxstat & VGE_RDSTS_CSUMERR)) {
1308                         ifp->if_ierrors++;
1309                         /*
1310                          * If this is part of a multi-fragment packet,
1311                          * discard all the pieces.
1312                          */
1313                         if (sc->vge_head != NULL) {
1314                                 m_freem(sc->vge_head);
1315                                 sc->vge_head = sc->vge_tail = NULL;
1316                         }
1317                         vge_newbuf(sc, i, m);
1318                         VGE_RX_DESC_INC(i);
1319                         continue;
1320                 }
1321
1322                 /*
1323                  * If allocating a replacement mbuf fails,
1324                  * reload the current one.
1325                  */
1326                 if (vge_newbuf(sc, i, NULL)) {
1327                         ifp->if_ierrors++;
1328                         if (sc->vge_head != NULL) {
1329                                 m_freem(sc->vge_head);
1330                                 sc->vge_head = sc->vge_tail = NULL;
1331                         }
1332                         vge_newbuf(sc, i, m);
1333                         VGE_RX_DESC_INC(i);
1334                         continue;
1335                 }
1336
1337                 VGE_RX_DESC_INC(i);
1338
1339                 if (sc->vge_head != NULL) {
1340                         m->m_len = total_len % (MCLBYTES - VGE_ETHER_ALIGN);
1341                         /*
1342                          * Special case: if there's 4 bytes or less
1343                          * in this buffer, the mbuf can be discarded:
1344                          * the last 4 bytes is the CRC, which we don't
1345                          * care about anyway.
1346                          */
1347                         if (m->m_len <= ETHER_CRC_LEN) {
1348                                 sc->vge_tail->m_len -=
1349                                     (ETHER_CRC_LEN - m->m_len);
1350                                 m_freem(m);
1351                         } else {
1352                                 m->m_len -= ETHER_CRC_LEN;
1353                                 m->m_flags &= ~M_PKTHDR;
1354                                 sc->vge_tail->m_next = m;
1355                         }
1356                         m = sc->vge_head;
1357                         sc->vge_head = sc->vge_tail = NULL;
1358                         m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1359                 } else {
1360                         m->m_pkthdr.len = m->m_len =
1361                             (total_len - ETHER_CRC_LEN);
1362                 }
1363
1364 #ifdef VGE_FIXUP_RX
1365                 vge_fixup_rx(m);
1366 #endif
1367                 ifp->if_ipackets++;
1368                 m->m_pkthdr.rcvif = ifp;
1369
1370                 /* Do RX checksumming if enabled */
1371                 if (ifp->if_capenable & IFCAP_RXCSUM) {
1372                         /* Check IP header checksum */
1373                         if (rxctl & VGE_RDCTL_IPPKT)
1374                                 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1375                         if (rxctl & VGE_RDCTL_IPCSUMOK)
1376                                 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1377
1378                         /* Check TCP/UDP checksum */
1379                         if (rxctl & (VGE_RDCTL_TCPPKT|VGE_RDCTL_UDPPKT) &&
1380                             rxctl & VGE_RDCTL_PROTOCSUMOK) {
1381                                 m->m_pkthdr.csum_flags |=
1382                                     CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1383                                 m->m_pkthdr.csum_data = 0xffff;
1384                         }
1385                 }
1386
1387                 if (rxstat & VGE_RDSTS_VTAG)
1388                         VLAN_INPUT_TAG(m, ntohs((rxctl & VGE_RDCTL_VLANID)));
1389                 else
1390                         ifp->if_input(ifp, m);
1391
1392                 lim++;
1393                 if (lim == VGE_RX_DESC_CNT)
1394                         break;
1395         }
1396
1397         /* Flush the RX DMA ring */
1398         bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag,
1399                         sc->vge_ldata.vge_rx_list_map,
1400                         BUS_DMASYNC_PREWRITE);
1401
1402         sc->vge_ldata.vge_rx_prodidx = i;
1403         CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, lim);
1404 }
1405
1406 static void
1407 vge_txeof(struct vge_softc *sc)
1408 {
1409         struct ifnet *ifp = &sc->arpcom.ac_if;
1410         uint32_t txstat;
1411         int idx;
1412
1413         idx = sc->vge_ldata.vge_tx_considx;
1414
1415         /* Invalidate the TX descriptor list */
1416
1417         bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag,
1418                         sc->vge_ldata.vge_tx_list_map, BUS_DMASYNC_POSTREAD);
1419
1420         while (idx != sc->vge_ldata.vge_tx_prodidx) {
1421
1422                 txstat = le32toh(sc->vge_ldata.vge_tx_list[idx].vge_sts);
1423                 if (txstat & VGE_TDSTS_OWN)
1424                         break;
1425
1426                 m_freem(sc->vge_ldata.vge_tx_mbuf[idx]);
1427                 sc->vge_ldata.vge_tx_mbuf[idx] = NULL;
1428                 bus_dmamap_unload(sc->vge_ldata.vge_mtag,
1429                                   sc->vge_ldata.vge_tx_dmamap[idx]);
1430                 if (txstat & (VGE_TDSTS_EXCESSCOLL|VGE_TDSTS_COLL))
1431                         ifp->if_collisions++;
1432                 if (txstat & VGE_TDSTS_TXERR)
1433                         ifp->if_oerrors++;
1434                 else
1435                         ifp->if_opackets++;
1436
1437                 sc->vge_ldata.vge_tx_free++;
1438                 VGE_TX_DESC_INC(idx);
1439         }
1440
1441         /* No changes made to the TX ring, so no flush needed */
1442         if (idx != sc->vge_ldata.vge_tx_considx) {
1443                 sc->vge_ldata.vge_tx_considx = idx;
1444                 ifp->if_flags &= ~IFF_OACTIVE;
1445                 ifp->if_timer = 0;
1446         }
1447
1448         /*
1449          * If not all descriptors have been released reaped yet,
1450          * reload the timer so that we will eventually get another
1451          * interrupt that will cause us to re-enter this routine.
1452          * This is done in case the transmitter has gone idle.
1453          */
1454         if (sc->vge_ldata.vge_tx_free != VGE_TX_DESC_CNT)
1455                 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1456 }
1457
1458 static void
1459 vge_tick(struct vge_softc *sc)
1460 {
1461         struct ifnet *ifp = &sc->arpcom.ac_if;
1462         struct mii_data *mii;
1463
1464         mii = device_get_softc(sc->vge_miibus);
1465
1466         mii_tick(mii);
1467         if (sc->vge_link) {
1468                 if (!(mii->mii_media_status & IFM_ACTIVE)) {
1469                         sc->vge_link = 0;
1470 #if 0
1471                         if_link_state_change(sc->vge_ifp,
1472                             LINK_STATE_DOWN);
1473 #endif
1474                 }
1475         } else {
1476                 if (mii->mii_media_status & IFM_ACTIVE &&
1477                     IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1478                         sc->vge_link = 1;
1479 #if 0
1480                         if_link_state_change(sc->vge_ifp,
1481                             LINK_STATE_UP);
1482 #endif
1483                         if (!ifq_is_empty(&ifp->if_snd))
1484                                 ifp->if_start(ifp);
1485                 }
1486         }
1487 }
1488
1489 #ifdef DEVICE_POLLING
1490 static void
1491 vge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1492 {
1493         struct vge_softc *sc = ifp->if_softc;
1494
1495         sc->rxcycles = count;
1496
1497         switch (cmd) {
1498         case POLL_REGISTER:
1499                 vge_disable_intr(sc);
1500                 break;
1501         case POLL_DEREGISTER:
1502                 vge_enable_intr(sc, 0xffffffff);
1503                 break;
1504         case POLL_ONLY:
1505         case POLL_AND_CHECK_STATUS:
1506                 vge_rxeof(sc, count);
1507                 vge_txeof(sc);
1508
1509                 if (!ifq_is_empty(&ifp->if_snd))
1510                         ifp->if_start(ifp);
1511
1512                 /* XXX copy & paste from vge_intr */
1513                 if (cmd == POLL_AND_CHECK_STATUS) {
1514                         uint32_t status = 0;
1515
1516                         status = CSR_READ_4(sc, VGE_ISR);
1517                         if (status == 0xffffffff)
1518                                 break;
1519
1520                         if (status)
1521                                 CSR_WRITE_4(sc, VGE_ISR, status);
1522
1523                         if (status & (VGE_ISR_TXDMA_STALL |
1524                                       VGE_ISR_RXDMA_STALL))
1525                                 vge_init(sc);
1526
1527                         if (status & (VGE_ISR_RXOFLOW | VGE_ISR_RXNODESC)) {
1528                                 ifp->if_ierrors++;
1529                                 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1530                                 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1531                         }
1532                 }
1533                 break;
1534         }
1535
1536 }
1537 #endif  /* DEVICE_POLLING */
1538
1539 static void
1540 vge_intr(void *arg)
1541 {
1542         struct vge_softc *sc = arg;
1543         struct ifnet *ifp = &sc->arpcom.ac_if;
1544         uint32_t status;
1545
1546         if (sc->suspended || !(ifp->if_flags & IFF_UP))
1547                 return;
1548
1549         /* Disable interrupts */
1550         CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1551
1552         for (;;) {
1553                 status = CSR_READ_4(sc, VGE_ISR);
1554                 /* If the card has gone away the read returns 0xffff. */
1555                 if (status == 0xFFFFFFFF)
1556                         break;
1557
1558                 if (status)
1559                         CSR_WRITE_4(sc, VGE_ISR, status);
1560
1561                 if ((status & VGE_INTRS) == 0)
1562                         break;
1563
1564                 if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
1565                         vge_rxeof(sc, -1);
1566
1567                 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1568                         vge_rxeof(sc, -1);
1569                         ifp->if_ierrors++;
1570                         CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1571                         CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1572                 }
1573
1574                 if (status & (VGE_ISR_TXOK0|VGE_ISR_TIMER0))
1575                         vge_txeof(sc);
1576
1577                 if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL))
1578                         vge_init(sc);
1579
1580                 if (status & VGE_ISR_LINKSTS)
1581                         vge_tick(sc);
1582         }
1583
1584         /* Re-enable interrupts */
1585         CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1586
1587         if (!ifq_is_empty(&ifp->if_snd))
1588                 ifp->if_start(ifp);
1589 }
1590
1591 static int
1592 vge_encap(struct vge_softc *sc, struct mbuf *m_head, int idx)
1593 {
1594         struct vge_dmaload_arg arg;
1595         bus_dmamap_t map;
1596         int error;
1597
1598         arg.vge_flags = 0;
1599
1600         if (m_head->m_pkthdr.csum_flags & CSUM_IP)
1601                 arg.vge_flags |= VGE_TDCTL_IPCSUM;
1602         if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
1603                 arg.vge_flags |= VGE_TDCTL_TCPCSUM;
1604         if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
1605                 arg.vge_flags |= VGE_TDCTL_UDPCSUM;
1606
1607         arg.sc = sc;
1608         arg.vge_idx = idx;
1609         arg.vge_m0 = m_head;
1610         arg.vge_maxsegs = VGE_TX_FRAGS;
1611
1612         map = sc->vge_ldata.vge_tx_dmamap[idx];
1613         error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag, map, m_head,
1614                                      vge_dma_map_tx_desc, &arg, BUS_DMA_NOWAIT);
1615         if (error && error != EFBIG) {
1616                 if_printf(&sc->arpcom.ac_if, "can't map mbuf (error %d)\n",
1617                           error);
1618                 goto fail;
1619         }
1620
1621         /* Too many segments to map, coalesce into a single mbuf */
1622         if (error || arg.vge_maxsegs == 0) {
1623                 struct mbuf *m_new;
1624
1625                 m_new = m_defrag(m_head, MB_DONTWAIT);
1626                 if (m_new == NULL) {
1627                         error = ENOBUFS;
1628                         goto fail;
1629                 } else {
1630                         m_head = m_new;
1631                 }
1632
1633                 arg.sc = sc;
1634                 arg.vge_m0 = m_head;
1635                 arg.vge_idx = idx;
1636                 arg.vge_maxsegs = 1;
1637
1638                 error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag, map,
1639                                              m_head, vge_dma_map_tx_desc, &arg,
1640                                              BUS_DMA_NOWAIT);
1641                 if (error) {
1642                         if_printf(&sc->arpcom.ac_if,
1643                                   "can't map mbuf (error %d)\n", error);
1644                         goto fail;
1645                 }
1646         }
1647
1648         sc->vge_ldata.vge_tx_mbuf[idx] = m_head;
1649         sc->vge_ldata.vge_tx_free--;
1650
1651         /*
1652          * Set up hardware VLAN tagging.
1653          */
1654         if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
1655             m_head->m_pkthdr.rcvif != NULL &&
1656             m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN) {
1657                 struct ifvlan *ifv = m_head->m_pkthdr.rcvif->if_softc;
1658
1659                 if (ifv != NULL) {
1660                         sc->vge_ldata.vge_tx_list[idx].vge_ctl |=
1661                                 htole32(htons(ifv->ifv_tag) | VGE_TDCTL_VTAG);
1662                 }
1663         }
1664
1665         sc->vge_ldata.vge_tx_list[idx].vge_sts |= htole32(VGE_TDSTS_OWN);
1666         return (0);
1667
1668 fail:
1669         m_freem(m_head);
1670         return error;
1671 }
1672
1673 /*
1674  * Main transmit routine.
1675  */
1676
1677 static void
1678 vge_start(struct ifnet *ifp)
1679 {
1680         struct vge_softc *sc = ifp->if_softc;
1681         struct mbuf *m_head = NULL;
1682         int idx, pidx = 0;
1683
1684         ASSERT_SERIALIZED(ifp->if_serializer);
1685
1686         if (!sc->vge_link || (ifp->if_flags & IFF_OACTIVE))
1687                 return;
1688
1689         if (ifq_is_empty(&ifp->if_snd))
1690                 return;
1691
1692         idx = sc->vge_ldata.vge_tx_prodidx;
1693
1694         pidx = idx - 1;
1695         if (pidx < 0)
1696                 pidx = VGE_TX_DESC_CNT - 1;
1697
1698         while (sc->vge_ldata.vge_tx_mbuf[idx] == NULL) {
1699                 m_head = ifq_poll(&ifp->if_snd);
1700                 if (m_head == NULL)
1701                         break;
1702
1703                 if (sc->vge_ldata.vge_tx_free <= 2) {
1704                         ifp->if_flags |= IFF_OACTIVE;
1705                         break;
1706                 }
1707
1708                 m_head = ifq_dequeue(&ifp->if_snd, m_head);
1709
1710                 if (vge_encap(sc, m_head, idx)) {
1711                         /* If vge_encap() failed, it will free m_head for us */
1712                         ifp->if_flags |= IFF_OACTIVE;
1713                         break;
1714                 }
1715
1716                 sc->vge_ldata.vge_tx_list[pidx].vge_frag[0].vge_buflen |=
1717                     htole16(VGE_TXDESC_Q);
1718
1719                 pidx = idx;
1720                 VGE_TX_DESC_INC(idx);
1721
1722                 /*
1723                  * If there's a BPF listener, bounce a copy of this frame
1724                  * to him.
1725                  */
1726                 BPF_MTAP(ifp, m_head);
1727         }
1728
1729         if (idx == sc->vge_ldata.vge_tx_prodidx)
1730                 return;
1731
1732         /* Flush the TX descriptors */
1733         bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag,
1734                         sc->vge_ldata.vge_tx_list_map,
1735                         BUS_DMASYNC_PREWRITE);
1736
1737         /* Issue a transmit command. */
1738         CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
1739
1740         sc->vge_ldata.vge_tx_prodidx = idx;
1741
1742         /*
1743          * Use the countdown timer for interrupt moderation.
1744          * 'TX done' interrupts are disabled. Instead, we reset the
1745          * countdown timer, which will begin counting until it hits
1746          * the value in the SSTIMER register, and then trigger an
1747          * interrupt. Each time we set the TIMER0_ENABLE bit, the
1748          * the timer count is reloaded. Only when the transmitter
1749          * is idle will the timer hit 0 and an interrupt fire.
1750          */
1751         CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1752
1753         /*
1754          * Set a timeout in case the chip goes out to lunch.
1755          */
1756         ifp->if_timer = 5;
1757 }
1758
1759 static void
1760 vge_init(void *xsc)
1761 {
1762         struct vge_softc *sc = xsc;
1763         struct ifnet *ifp = &sc->arpcom.ac_if;
1764         struct mii_data *mii;
1765         int i;
1766
1767         ASSERT_SERIALIZED(ifp->if_serializer);
1768
1769         mii = device_get_softc(sc->vge_miibus);
1770
1771         /*
1772          * Cancel pending I/O and free all RX/TX buffers.
1773          */
1774         vge_stop(sc);
1775         vge_reset(sc);
1776
1777         /*
1778          * Initialize the RX and TX descriptors and mbufs.
1779          */
1780         vge_rx_list_init(sc);
1781         vge_tx_list_init(sc);
1782
1783         /* Set our station address */
1784         for (i = 0; i < ETHER_ADDR_LEN; i++)
1785                 CSR_WRITE_1(sc, VGE_PAR0 + i, IF_LLADDR(ifp)[i]);
1786
1787         /*
1788          * Set receive FIFO threshold. Also allow transmission and
1789          * reception of VLAN tagged frames.
1790          */
1791         CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
1792         CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES|VGE_VTAG_OPT2);
1793
1794         /* Set DMA burst length */
1795         CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
1796         CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
1797
1798         CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
1799
1800         /* Set collision backoff algorithm */
1801         CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
1802             VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
1803         CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
1804
1805         /* Disable LPSEL field in priority resolution */
1806         CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
1807
1808         /*
1809          * Load the addresses of the DMA queues into the chip.
1810          * Note that we only use one transmit queue.
1811          */
1812         CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0,
1813             VGE_ADDR_LO(sc->vge_ldata.vge_tx_list_addr));
1814         CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_TX_DESC_CNT - 1);
1815
1816         CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO,
1817             VGE_ADDR_LO(sc->vge_ldata.vge_rx_list_addr));
1818         CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_RX_DESC_CNT - 1);
1819         CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_RX_DESC_CNT);
1820
1821         /* Enable and wake up the RX descriptor queue */
1822         CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1823         CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1824
1825         /* Enable the TX descriptor queue */
1826         CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
1827
1828         /* Set up the receive filter -- allow large frames for VLANs. */
1829         CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST|VGE_RXCTL_RX_GIANT);
1830
1831         /* If we want promiscuous mode, set the allframes bit. */
1832         if (ifp->if_flags & IFF_PROMISC)
1833                 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
1834
1835         /* Set capture broadcast bit to capture broadcast frames. */
1836         if (ifp->if_flags & IFF_BROADCAST)
1837                 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST);
1838
1839         /* Set multicast bit to capture multicast frames. */
1840         if (ifp->if_flags & IFF_MULTICAST)
1841                 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST);
1842
1843         /* Init the cam filter. */
1844         vge_cam_clear(sc);
1845
1846         /* Init the multicast filter. */
1847         vge_setmulti(sc);
1848
1849         /* Enable flow control */
1850
1851         CSR_WRITE_1(sc, VGE_CRS2, 0x8B);
1852
1853         /* Enable jumbo frame reception (if desired) */
1854
1855         /* Start the MAC. */
1856         CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
1857         CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
1858         CSR_WRITE_1(sc, VGE_CRS0,
1859             VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
1860
1861         /*
1862          * Configure one-shot timer for microsecond
1863          * resulution and load it for 500 usecs.
1864          */
1865         CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES);
1866         CSR_WRITE_2(sc, VGE_SSTIMER, 400);
1867
1868         /*
1869          * Configure interrupt moderation for receive. Enable
1870          * the holdoff counter and load it, and set the RX
1871          * suppression count to the number of descriptors we
1872          * want to allow before triggering an interrupt.
1873          * The holdoff timer is in units of 20 usecs.
1874          */
1875
1876 #ifdef notyet
1877         CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE);
1878         /* Select the interrupt holdoff timer page. */
1879         CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1880         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
1881         CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */
1882
1883         /* Enable use of the holdoff timer. */
1884         CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
1885         CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD);
1886
1887         /* Select the RX suppression threshold page. */
1888         CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1889         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
1890         CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */
1891
1892         /* Restore the page select bits. */
1893         CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1894         CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
1895 #endif
1896
1897 #ifdef DEVICE_POLLING
1898         /* Disable intr if polling(4) is enabled */
1899         if (ifp->if_flags & IFF_POLLING)
1900                 vge_disable_intr(sc);
1901         else
1902 #endif
1903         vge_enable_intr(sc, 0);
1904
1905         mii_mediachg(mii);
1906
1907         ifp->if_flags |= IFF_RUNNING;
1908         ifp->if_flags &= ~IFF_OACTIVE;
1909
1910         sc->vge_if_flags = 0;
1911         sc->vge_link = 0;
1912 }
1913
1914 /*
1915  * Set media options.
1916  */
1917 static int
1918 vge_ifmedia_upd(struct ifnet *ifp)
1919 {
1920         struct vge_softc *sc = ifp->if_softc;
1921         struct mii_data *mii = device_get_softc(sc->vge_miibus);
1922
1923         mii_mediachg(mii);
1924
1925         return (0);
1926 }
1927
1928 /*
1929  * Report current media status.
1930  */
1931 static void
1932 vge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1933 {
1934         struct vge_softc *sc = ifp->if_softc;
1935         struct mii_data *mii = device_get_softc(sc->vge_miibus);
1936
1937         mii_pollstat(mii);
1938         ifmr->ifm_active = mii->mii_media_active;
1939         ifmr->ifm_status = mii->mii_media_status;
1940 }
1941
1942 static void
1943 vge_miibus_statchg(device_t dev)
1944 {
1945         struct vge_softc *sc;
1946         struct mii_data *mii;
1947         struct ifmedia_entry *ife;
1948
1949         sc = device_get_softc(dev);
1950         mii = device_get_softc(sc->vge_miibus);
1951         ife = mii->mii_media.ifm_cur;
1952
1953         /*
1954          * If the user manually selects a media mode, we need to turn
1955          * on the forced MAC mode bit in the DIAGCTL register. If the
1956          * user happens to choose a full duplex mode, we also need to
1957          * set the 'force full duplex' bit. This applies only to
1958          * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
1959          * mode is disabled, and in 1000baseT mode, full duplex is
1960          * always implied, so we turn on the forced mode bit but leave
1961          * the FDX bit cleared.
1962          */
1963
1964         switch (IFM_SUBTYPE(ife->ifm_media)) {
1965         case IFM_AUTO:
1966                 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1967                 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1968                 break;
1969         case IFM_1000_T:
1970                 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1971                 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1972                 break;
1973         case IFM_100_TX:
1974         case IFM_10_T:
1975                 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1976                 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
1977                         CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1978                 else
1979                         CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1980                 break;
1981         default:
1982                 device_printf(dev, "unknown media type: %x\n",
1983                               IFM_SUBTYPE(ife->ifm_media));
1984                 break;
1985         }
1986 }
1987
1988 static int
1989 vge_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1990 {
1991         struct vge_softc *sc = ifp->if_softc;
1992         struct ifreq *ifr = (struct ifreq *)data;
1993         struct mii_data *mii;
1994         int error = 0;
1995
1996         switch (command) {
1997         case SIOCSIFMTU:
1998                 if (ifr->ifr_mtu > VGE_JUMBO_MTU)
1999                         error = EINVAL;
2000                 ifp->if_mtu = ifr->ifr_mtu;
2001                 break;
2002         case SIOCSIFFLAGS:
2003                 if (ifp->if_flags & IFF_UP) {
2004                         if ((ifp->if_flags & IFF_RUNNING) &&
2005                             (ifp->if_flags & IFF_PROMISC) &&
2006                             !(sc->vge_if_flags & IFF_PROMISC)) {
2007                                 CSR_SETBIT_1(sc, VGE_RXCTL,
2008                                     VGE_RXCTL_RX_PROMISC);
2009                                 vge_setmulti(sc);
2010                         } else if ((ifp->if_flags & IFF_RUNNING) &&
2011                                    !(ifp->if_flags & IFF_PROMISC) &&
2012                                    (sc->vge_if_flags & IFF_PROMISC)) {
2013                                 CSR_CLRBIT_1(sc, VGE_RXCTL,
2014                                              VGE_RXCTL_RX_PROMISC);
2015                                 vge_setmulti(sc);
2016                         } else {
2017                                 vge_init(sc);
2018                         }
2019                 } else {
2020                         if (ifp->if_flags & IFF_RUNNING)
2021                                 vge_stop(sc);
2022                 }
2023                 sc->vge_if_flags = ifp->if_flags;
2024                 break;
2025         case SIOCADDMULTI:
2026         case SIOCDELMULTI:
2027                 vge_setmulti(sc);
2028                 break;
2029         case SIOCGIFMEDIA:
2030         case SIOCSIFMEDIA:
2031                 mii = device_get_softc(sc->vge_miibus);
2032                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2033                 break;
2034         case SIOCSIFCAP:
2035             {
2036                 uint32_t mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2037
2038                 if (mask & IFCAP_HWCSUM) {
2039                         ifp->if_capenable |= ifr->ifr_reqcap & (IFCAP_HWCSUM);
2040                         if (ifp->if_capenable & IFCAP_TXCSUM)
2041                                 ifp->if_hwassist = VGE_CSUM_FEATURES;
2042                         else
2043                                 ifp->if_hwassist = 0;
2044                         if (ifp->if_flags & IFF_RUNNING)
2045                                 vge_init(sc);
2046                 }
2047             }
2048                 break;
2049         default:
2050                 error = ether_ioctl(ifp, command, data);
2051                 break;
2052         }
2053         return (error);
2054 }
2055
2056 static void
2057 vge_watchdog(struct ifnet *ifp)
2058 {
2059         struct vge_softc *sc = ifp->if_softc;
2060
2061         if_printf(ifp, "watchdog timeout\n");
2062         ifp->if_oerrors++;
2063
2064         vge_txeof(sc);
2065         vge_rxeof(sc, -1);
2066
2067         vge_init(sc);
2068 }
2069
2070 /*
2071  * Stop the adapter and free any mbufs allocated to the
2072  * RX and TX lists.
2073  */
2074 static void
2075 vge_stop(struct vge_softc *sc)
2076 {
2077         struct ifnet *ifp = &sc->arpcom.ac_if;
2078         int i;
2079
2080         ASSERT_SERIALIZED(ifp->if_serializer);
2081
2082         ifp->if_timer = 0;
2083
2084         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2085
2086         CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2087         CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
2088         CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2089         CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
2090         CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
2091         CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
2092
2093         if (sc->vge_head != NULL) {
2094                 m_freem(sc->vge_head);
2095                 sc->vge_head = sc->vge_tail = NULL;
2096         }
2097
2098         /* Free the TX list buffers. */
2099         for (i = 0; i < VGE_TX_DESC_CNT; i++) {
2100                 if (sc->vge_ldata.vge_tx_mbuf[i] != NULL) {
2101                         bus_dmamap_unload(sc->vge_ldata.vge_mtag,
2102                                           sc->vge_ldata.vge_tx_dmamap[i]);
2103                         m_freem(sc->vge_ldata.vge_tx_mbuf[i]);
2104                         sc->vge_ldata.vge_tx_mbuf[i] = NULL;
2105                 }
2106         }
2107
2108         /* Free the RX list buffers. */
2109         for (i = 0; i < VGE_RX_DESC_CNT; i++) {
2110                 if (sc->vge_ldata.vge_rx_mbuf[i] != NULL) {
2111                         bus_dmamap_unload(sc->vge_ldata.vge_mtag,
2112                                           sc->vge_ldata.vge_rx_dmamap[i]);
2113                         m_freem(sc->vge_ldata.vge_rx_mbuf[i]);
2114                         sc->vge_ldata.vge_rx_mbuf[i] = NULL;
2115                 }
2116         }
2117 }
2118
2119 /*
2120  * Device suspend routine.  Stop the interface and save some PCI
2121  * settings in case the BIOS doesn't restore them properly on
2122  * resume.
2123  */
2124 static int
2125 vge_suspend(device_t dev)
2126 {
2127         struct vge_softc *sc = device_get_softc(dev);
2128         struct ifnet *ifp = &sc->arpcom.ac_if;
2129
2130         lwkt_serialize_enter(ifp->if_serializer);
2131         vge_stop(sc);
2132         sc->suspended = 1;
2133         lwkt_serialize_exit(ifp->if_serializer);
2134
2135         return (0);
2136 }
2137
2138 /*
2139  * Device resume routine.  Restore some PCI settings in case the BIOS
2140  * doesn't, re-enable busmastering, and restart the interface if
2141  * appropriate.
2142  */
2143 static int
2144 vge_resume(device_t dev)
2145 {
2146         struct vge_softc *sc = device_get_softc(dev);
2147         struct ifnet *ifp = &sc->arpcom.ac_if;
2148
2149         /* reenable busmastering */
2150         pci_enable_busmaster(dev);
2151         pci_enable_io(dev, SYS_RES_MEMORY);
2152
2153         lwkt_serialize_enter(ifp->if_serializer);
2154         /* reinitialize interface if necessary */
2155         if (ifp->if_flags & IFF_UP)
2156                 vge_init(sc);
2157
2158         sc->suspended = 0;
2159         lwkt_serialize_exit(ifp->if_serializer);
2160
2161         return (0);
2162 }
2163
2164 /*
2165  * Stop all chip I/O so that the kernel's probe routines don't
2166  * get confused by errant DMAs when rebooting.
2167  */
2168 static void
2169 vge_shutdown(device_t dev)
2170 {
2171         struct vge_softc *sc = device_get_softc(dev);
2172         struct ifnet *ifp = &sc->arpcom.ac_if;
2173
2174         lwkt_serialize_enter(ifp->if_serializer);
2175         vge_stop(sc);
2176         lwkt_serialize_exit(ifp->if_serializer);
2177 }
2178
2179 static void
2180 vge_enable_intr(struct vge_softc *sc, uint32_t isr)
2181 {
2182         CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
2183         CSR_WRITE_4(sc, VGE_ISR, isr);
2184         CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
2185 }
2186
2187 #ifdef DEVICE_POLLING
2188 static void
2189 vge_disable_intr(struct vge_softc *sc)
2190 {
2191         CSR_WRITE_4(sc, VGE_IMR, 0);
2192         CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2193 }
2194 #endif