Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / netif / lge / if_lge.c
1 /*
2  * Copyright (c) 2001 Wind River Systems
3  * Copyright (c) 1997, 1998, 1999, 2000, 2001
4  *      Bill Paul <william.paul@windriver.com>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/lge/if_lge.c,v 1.5.2.2 2001/12/14 19:49:23 jlemon Exp $
34  */
35
36 /*
37  * Level 1 LXT1001 gigabit ethernet driver for FreeBSD. Public
38  * documentation not available, but ask me nicely.
39  *
40  * Written by Bill Paul <william.paul@windriver.com>
41  * Wind River Systems
42  */
43
44 /*
45  * The Level 1 chip is used on some D-Link, SMC and Addtron NICs.
46  * It's a 64-bit PCI part that supports TCP/IP checksum offload,
47  * VLAN tagging/insertion, GMII and TBI (1000baseX) ports. There
48  * are three supported methods for data transfer between host and
49  * NIC: programmed I/O, traditional scatter/gather DMA and Packet
50  * Propulsion Technology (tm) DMA. The latter mechanism is a form
51  * of double buffer DMA where the packet data is copied to a
52  * pre-allocated DMA buffer who's physical address has been loaded
53  * into a table at device initialization time. The rationale is that
54  * the virtual to physical address translation needed for normal
55  * scatter/gather DMA is more expensive than the data copy needed
56  * for double buffering. This may be true in Windows NT and the like,
57  * but it isn't true for us, at least on the x86 arch. This driver
58  * uses the scatter/gather I/O method for both TX and RX.
59  *
60  * The LXT1001 only supports TCP/IP checksum offload on receive.
61  * Also, the VLAN tagging is done using a 16-entry table which allows
62  * the chip to perform hardware filtering based on VLAN tags. Sadly,
63  * our vlan support doesn't currently play well with this kind of
64  * hardware support.
65  *
66  * Special thanks to:
67  * - Jeff James at Intel, for arranging to have the LXT1001 manual
68  *   released (at long last)
69  * - Beny Chen at D-Link, for actually sending it to me
70  * - Brad Short and Keith Alexis at SMC, for sending me sample
71  *   SMC9462SX and SMC9462TX adapters for testing
72  * - Paul Saab at Y!, for not killing me (though it remains to be seen
73  *   if in fact he did me much of a favor)
74  */
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/sockio.h>
79 #include <sys/mbuf.h>
80 #include <sys/malloc.h>
81 #include <sys/kernel.h>
82 #include <sys/socket.h>
83
84 #include <net/if.h>
85 #include <net/if_arp.h>
86 #include <net/ethernet.h>
87 #include <net/if_dl.h>
88 #include <net/if_media.h>
89
90 #include <net/bpf.h>
91
92 #include <vm/vm.h>              /* for vtophys */
93 #include <vm/pmap.h>            /* for vtophys */
94 #include <machine/clock.h>      /* for DELAY */
95 #include <machine/bus_pio.h>
96 #include <machine/bus_memio.h>
97 #include <machine/bus.h>
98 #include <machine/resource.h>
99 #include <sys/bus.h>
100 #include <sys/rman.h>
101
102 #include <dev/mii/mii.h>
103 #include <dev/mii/miivar.h>
104
105 #include <pci/pcireg.h>
106 #include <pci/pcivar.h>
107
108 #define LGE_USEIOSPACE
109
110 #include <dev/lge/if_lgereg.h>
111
112 /* "controller miibus0" required.  See GENERIC if you get errors here. */
113 #include "miibus_if.h"
114
115 #ifndef lint
116 static const char rcsid[] =
117   "$FreeBSD: src/sys/dev/lge/if_lge.c,v 1.5.2.2 2001/12/14 19:49:23 jlemon Exp $";
118 #endif
119
120 /*
121  * Various supported device vendors/types and their names.
122  */
123 static struct lge_type lge_devs[] = {
124         { LGE_VENDORID, LGE_DEVICEID, "Level 1 Gigabit Ethernet" },
125         { 0, 0, NULL }
126 };
127
128 static int lge_probe            __P((device_t));
129 static int lge_attach           __P((device_t));
130 static int lge_detach           __P((device_t));
131
132 static int lge_alloc_jumbo_mem  __P((struct lge_softc *));
133 static void lge_free_jumbo_mem  __P((struct lge_softc *));
134 static void *lge_jalloc         __P((struct lge_softc *));
135 static void lge_jfree           __P((caddr_t, u_int));
136 static void lge_jref            __P((caddr_t, u_int));
137
138 static int lge_newbuf           __P((struct lge_softc *,
139                                         struct lge_rx_desc *,
140                                         struct mbuf *));
141 static int lge_encap            __P((struct lge_softc *,
142                                         struct mbuf *, u_int32_t *));
143 static void lge_rxeof           __P((struct lge_softc *, int));
144 static void lge_rxeoc           __P((struct lge_softc *));
145 static void lge_txeof           __P((struct lge_softc *));
146 static void lge_intr            __P((void *));
147 static void lge_tick            __P((void *));
148 static void lge_start           __P((struct ifnet *));
149 static int lge_ioctl            __P((struct ifnet *, u_long, caddr_t));
150 static void lge_init            __P((void *));
151 static void lge_stop            __P((struct lge_softc *));
152 static void lge_watchdog                __P((struct ifnet *));
153 static void lge_shutdown                __P((device_t));
154 static int lge_ifmedia_upd      __P((struct ifnet *));
155 static void lge_ifmedia_sts     __P((struct ifnet *, struct ifmediareq *));
156
157 static void lge_eeprom_getword  __P((struct lge_softc *, int, u_int16_t *));
158 static void lge_read_eeprom     __P((struct lge_softc *, caddr_t, int,
159                                                         int, int));
160
161 static int lge_miibus_readreg   __P((device_t, int, int));
162 static int lge_miibus_writereg  __P((device_t, int, int, int));
163 static void lge_miibus_statchg  __P((device_t));
164
165 static void lge_setmulti        __P((struct lge_softc *));
166 static u_int32_t lge_crc        __P((struct lge_softc *, caddr_t));
167 static void lge_reset           __P((struct lge_softc *));
168 static int lge_list_rx_init     __P((struct lge_softc *));
169 static int lge_list_tx_init     __P((struct lge_softc *));
170
171 #ifdef LGE_USEIOSPACE
172 #define LGE_RES                 SYS_RES_IOPORT
173 #define LGE_RID                 LGE_PCI_LOIO
174 #else
175 #define LGE_RES                 SYS_RES_MEMORY
176 #define LGE_RID                 LGE_PCI_LOMEM
177 #endif
178
179 static device_method_t lge_methods[] = {
180         /* Device interface */
181         DEVMETHOD(device_probe,         lge_probe),
182         DEVMETHOD(device_attach,        lge_attach),
183         DEVMETHOD(device_detach,        lge_detach),
184         DEVMETHOD(device_shutdown,      lge_shutdown),
185
186         /* bus interface */
187         DEVMETHOD(bus_print_child,      bus_generic_print_child),
188         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
189
190         /* MII interface */
191         DEVMETHOD(miibus_readreg,       lge_miibus_readreg),
192         DEVMETHOD(miibus_writereg,      lge_miibus_writereg),
193         DEVMETHOD(miibus_statchg,       lge_miibus_statchg),
194
195         { 0, 0 }
196 };
197
198 static driver_t lge_driver = {
199         "lge",
200         lge_methods,
201         sizeof(struct lge_softc)
202 };
203
204 static devclass_t lge_devclass;
205
206 DRIVER_MODULE(if_lge, pci, lge_driver, lge_devclass, 0, 0);
207 DRIVER_MODULE(miibus, lge, miibus_driver, miibus_devclass, 0, 0);
208
209 #define LGE_SETBIT(sc, reg, x)                          \
210         CSR_WRITE_4(sc, reg,                            \
211                 CSR_READ_4(sc, reg) | (x))
212
213 #define LGE_CLRBIT(sc, reg, x)                          \
214         CSR_WRITE_4(sc, reg,                            \
215                 CSR_READ_4(sc, reg) & ~(x))
216
217 #define SIO_SET(x)                                      \
218         CSR_WRITE_4(sc, LGE_MEAR, CSR_READ_4(sc, LGE_MEAR) | x)
219
220 #define SIO_CLR(x)                                      \
221         CSR_WRITE_4(sc, LGE_MEAR, CSR_READ_4(sc, LGE_MEAR) & ~x)
222
223 /*
224  * Read a word of data stored in the EEPROM at address 'addr.'
225  */
226 static void lge_eeprom_getword(sc, addr, dest)
227         struct lge_softc        *sc;
228         int                     addr;
229         u_int16_t               *dest;
230 {
231         register int            i;
232         u_int32_t               val;
233
234         CSR_WRITE_4(sc, LGE_EECTL, LGE_EECTL_CMD_READ|
235             LGE_EECTL_SINGLEACCESS|((addr >> 1) << 8));
236
237         for (i = 0; i < LGE_TIMEOUT; i++)
238                 if (!(CSR_READ_4(sc, LGE_EECTL) & LGE_EECTL_CMD_READ))
239                         break;
240
241         if (i == LGE_TIMEOUT) {
242                 printf("lge%d: EEPROM read timed out\n", sc->lge_unit);
243                 return;
244         }
245
246         val = CSR_READ_4(sc, LGE_EEDATA);
247
248         if (addr & 1)
249                 *dest = (val >> 16) & 0xFFFF;
250         else
251                 *dest = val & 0xFFFF;
252
253         return;
254 }
255
256 /*
257  * Read a sequence of words from the EEPROM.
258  */
259 static void lge_read_eeprom(sc, dest, off, cnt, swap)
260         struct lge_softc        *sc;
261         caddr_t                 dest;
262         int                     off;
263         int                     cnt;
264         int                     swap;
265 {
266         int                     i;
267         u_int16_t               word = 0, *ptr;
268
269         for (i = 0; i < cnt; i++) {
270                 lge_eeprom_getword(sc, off + i, &word);
271                 ptr = (u_int16_t *)(dest + (i * 2));
272                 if (swap)
273                         *ptr = ntohs(word);
274                 else
275                         *ptr = word;
276         }
277
278         return;
279 }
280
281 static int lge_miibus_readreg(dev, phy, reg)
282         device_t                dev;
283         int                     phy, reg;
284 {
285         struct lge_softc        *sc;
286         int                     i;
287
288         sc = device_get_softc(dev);
289
290         /*
291          * If we have a non-PCS PHY, pretend that the internal
292          * autoneg stuff at PHY address 0 isn't there so that
293          * the miibus code will find only the GMII PHY.
294          */
295         if (sc->lge_pcs == 0 && phy == 0)
296                 return(0);
297
298         CSR_WRITE_4(sc, LGE_GMIICTL, (phy << 8) | reg | LGE_GMIICMD_READ);
299
300         for (i = 0; i < LGE_TIMEOUT; i++)
301                 if (!(CSR_READ_4(sc, LGE_GMIICTL) & LGE_GMIICTL_CMDBUSY))
302                         break;
303
304         if (i == LGE_TIMEOUT) {
305                 printf("lge%d: PHY read timed out\n", sc->lge_unit);
306                 return(0);
307         }
308
309         return(CSR_READ_4(sc, LGE_GMIICTL) >> 16);
310 }
311
312 static int lge_miibus_writereg(dev, phy, reg, data)
313         device_t                dev;
314         int                     phy, reg, data;
315 {
316         struct lge_softc        *sc;
317         int                     i;
318
319         sc = device_get_softc(dev);
320
321         CSR_WRITE_4(sc, LGE_GMIICTL,
322             (data << 16) | (phy << 8) | reg | LGE_GMIICMD_WRITE);
323
324         for (i = 0; i < LGE_TIMEOUT; i++)
325                 if (!(CSR_READ_4(sc, LGE_GMIICTL) & LGE_GMIICTL_CMDBUSY))
326                         break;
327
328         if (i == LGE_TIMEOUT) {
329                 printf("lge%d: PHY write timed out\n", sc->lge_unit);
330                 return(0);
331         }
332
333         return(0);
334 }
335
336 static void lge_miibus_statchg(dev)
337         device_t                dev;
338 {
339         struct lge_softc        *sc;
340         struct mii_data         *mii;
341
342         sc = device_get_softc(dev);
343         mii = device_get_softc(sc->lge_miibus);
344
345         LGE_CLRBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_SPEED);
346         switch (IFM_SUBTYPE(mii->mii_media_active)) {
347         case IFM_1000_TX:
348         case IFM_1000_SX:
349                 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_1000);
350                 break;
351         case IFM_100_TX:
352                 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_100);
353                 break;
354         case IFM_10_T:
355                 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_10);
356                 break;
357         default:
358                 /*
359                  * Choose something, even if it's wrong. Clearing
360                  * all the bits will hose autoneg on the internal
361                  * PHY.
362                  */
363                 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_1000);
364                 break;
365         }
366
367         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
368                 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_FDX);
369         } else {
370                 LGE_CLRBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_FDX);
371         }
372
373         return;
374 }
375
376 static u_int32_t lge_crc(sc, addr)
377         struct lge_softc        *sc;
378         caddr_t                 addr;
379 {
380         u_int32_t               crc, carry;
381         int                     i, j;
382         u_int8_t                c;
383
384         /* Compute CRC for the address value. */
385         crc = 0xFFFFFFFF; /* initial value */
386
387         for (i = 0; i < 6; i++) {
388                 c = *(addr + i);
389                 for (j = 0; j < 8; j++) {
390                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
391                         crc <<= 1;
392                         c >>= 1;
393                         if (carry)
394                                 crc = (crc ^ 0x04c11db6) | carry;
395                 }
396         }
397
398         /*
399          * return the filter bit position
400          */
401         return((crc >> 26) & 0x0000003F);
402 }
403
404 static void lge_setmulti(sc)
405         struct lge_softc        *sc;
406 {
407         struct ifnet            *ifp;
408         struct ifmultiaddr      *ifma;
409         u_int32_t               h = 0, hashes[2] = { 0, 0 };
410
411         ifp = &sc->arpcom.ac_if;
412
413         /* Make sure multicast hash table is enabled. */
414         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_MCAST);
415
416         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
417                 CSR_WRITE_4(sc, LGE_MAR0, 0xFFFFFFFF);
418                 CSR_WRITE_4(sc, LGE_MAR1, 0xFFFFFFFF);
419                 return;
420         }
421
422         /* first, zot all the existing hash bits */
423         CSR_WRITE_4(sc, LGE_MAR0, 0);
424         CSR_WRITE_4(sc, LGE_MAR1, 0);
425
426         /* now program new ones */
427         for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
428             ifma = ifma->ifma_link.le_next) {
429                 if (ifma->ifma_addr->sa_family != AF_LINK)
430                         continue;
431                 h = lge_crc(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
432                 if (h < 32)
433                         hashes[0] |= (1 << h);
434                 else
435                         hashes[1] |= (1 << (h - 32));
436         }
437
438         CSR_WRITE_4(sc, LGE_MAR0, hashes[0]);
439         CSR_WRITE_4(sc, LGE_MAR1, hashes[1]);
440
441         return;
442 }
443
444 static void lge_reset(sc)
445         struct lge_softc        *sc;
446 {
447         register int            i;
448
449         LGE_SETBIT(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL0|LGE_MODE1_SOFTRST);
450
451         for (i = 0; i < LGE_TIMEOUT; i++) {
452                 if (!(CSR_READ_4(sc, LGE_MODE1) & LGE_MODE1_SOFTRST))
453                         break;
454         }
455
456         if (i == LGE_TIMEOUT)
457                 printf("lge%d: reset never completed\n", sc->lge_unit);
458
459         /* Wait a little while for the chip to get its brains in order. */
460         DELAY(1000);
461
462         return;
463 }
464
465 /*
466  * Probe for a Level 1 chip. Check the PCI vendor and device
467  * IDs against our list and return a device name if we find a match.
468  */
469 static int lge_probe(dev)
470         device_t                dev;
471 {
472         struct lge_type         *t;
473
474         t = lge_devs;
475
476         while(t->lge_name != NULL) {
477                 if ((pci_get_vendor(dev) == t->lge_vid) &&
478                     (pci_get_device(dev) == t->lge_did)) {
479                         device_set_desc(dev, t->lge_name);
480                         return(0);
481                 }
482                 t++;
483         }
484
485         return(ENXIO);
486 }
487
488 /*
489  * Attach the interface. Allocate softc structures, do ifmedia
490  * setup and ethernet/BPF attach.
491  */
492 static int lge_attach(dev)
493         device_t                dev;
494 {
495         int                     s;
496         u_char                  eaddr[ETHER_ADDR_LEN];
497         u_int32_t               command;
498         struct lge_softc        *sc;
499         struct ifnet            *ifp;
500         int                     unit, error = 0, rid;
501
502         s = splimp();
503
504         sc = device_get_softc(dev);
505         unit = device_get_unit(dev);
506         bzero(sc, sizeof(struct lge_softc));
507
508         /*
509          * Handle power management nonsense.
510          */
511         command = pci_read_config(dev, LGE_PCI_CAPID, 4) & 0x000000FF;
512         if (command == 0x01) {
513
514                 command = pci_read_config(dev, LGE_PCI_PWRMGMTCTRL, 4);
515                 if (command & LGE_PSTATE_MASK) {
516                         u_int32_t               iobase, membase, irq;
517
518                         /* Save important PCI config data. */
519                         iobase = pci_read_config(dev, LGE_PCI_LOIO, 4);
520                         membase = pci_read_config(dev, LGE_PCI_LOMEM, 4);
521                         irq = pci_read_config(dev, LGE_PCI_INTLINE, 4);
522
523                         /* Reset the power state. */
524                         printf("lge%d: chip is in D%d power mode "
525                         "-- setting to D0\n", unit, command & LGE_PSTATE_MASK);
526                         command &= 0xFFFFFFFC;
527                         pci_write_config(dev, LGE_PCI_PWRMGMTCTRL, command, 4);
528
529                         /* Restore PCI config data. */
530                         pci_write_config(dev, LGE_PCI_LOIO, iobase, 4);
531                         pci_write_config(dev, LGE_PCI_LOMEM, membase, 4);
532                         pci_write_config(dev, LGE_PCI_INTLINE, irq, 4);
533                 }
534         }
535
536         /*
537          * Map control/status registers.
538          */
539         command = pci_read_config(dev, PCIR_COMMAND, 4);
540         command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
541         pci_write_config(dev, PCIR_COMMAND, command, 4);
542         command = pci_read_config(dev, PCIR_COMMAND, 4);
543
544 #ifdef LGE_USEIOSPACE
545         if (!(command & PCIM_CMD_PORTEN)) {
546                 printf("lge%d: failed to enable I/O ports!\n", unit);
547                 error = ENXIO;;
548                 goto fail;
549         }
550 #else
551         if (!(command & PCIM_CMD_MEMEN)) {
552                 printf("lge%d: failed to enable memory mapping!\n", unit);
553                 error = ENXIO;;
554                 goto fail;
555         }
556 #endif
557
558         rid = LGE_RID;
559         sc->lge_res = bus_alloc_resource(dev, LGE_RES, &rid,
560             0, ~0, 1, RF_ACTIVE);
561
562         if (sc->lge_res == NULL) {
563                 printf("lge%d: couldn't map ports/memory\n", unit);
564                 error = ENXIO;
565                 goto fail;
566         }
567
568         sc->lge_btag = rman_get_bustag(sc->lge_res);
569         sc->lge_bhandle = rman_get_bushandle(sc->lge_res);
570
571         /* Allocate interrupt */
572         rid = 0;
573         sc->lge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
574             RF_SHAREABLE | RF_ACTIVE);
575
576         if (sc->lge_irq == NULL) {
577                 printf("lge%d: couldn't map interrupt\n", unit);
578                 bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
579                 error = ENXIO;
580                 goto fail;
581         }
582
583         error = bus_setup_intr(dev, sc->lge_irq, INTR_TYPE_NET,
584             lge_intr, sc, &sc->lge_intrhand);
585
586         if (error) {
587                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
588                 bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
589                 printf("lge%d: couldn't set up irq\n", unit);
590                 goto fail;
591         }
592
593         /* Reset the adapter. */
594         lge_reset(sc);
595
596         /*
597          * Get station address from the EEPROM.
598          */
599         lge_read_eeprom(sc, (caddr_t)&eaddr[0], LGE_EE_NODEADDR_0, 1, 0);
600         lge_read_eeprom(sc, (caddr_t)&eaddr[2], LGE_EE_NODEADDR_1, 1, 0);
601         lge_read_eeprom(sc, (caddr_t)&eaddr[4], LGE_EE_NODEADDR_2, 1, 0);
602
603         /*
604          * A Level 1 chip was detected. Inform the world.
605          */
606         printf("lge%d: Ethernet address: %6D\n", unit, eaddr, ":");
607
608         sc->lge_unit = unit;
609         callout_handle_init(&sc->lge_stat_ch);
610         bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
611
612         sc->lge_ldata = contigmalloc(sizeof(struct lge_list_data), M_DEVBUF,
613             M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
614
615         if (sc->lge_ldata == NULL) {
616                 printf("lge%d: no memory for list buffers!\n", unit);
617                 bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
618                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
619                 bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
620                 error = ENXIO;
621                 goto fail;
622         }
623         bzero(sc->lge_ldata, sizeof(struct lge_list_data));
624
625         /* Try to allocate memory for jumbo buffers. */
626         if (lge_alloc_jumbo_mem(sc)) {
627                 printf("lge%d: jumbo buffer allocation failed\n",
628                     sc->lge_unit);
629                 contigfree(sc->lge_ldata,
630                     sizeof(struct lge_list_data), M_DEVBUF);
631                 bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
632                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
633                 bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
634                 error = ENXIO;
635                 goto fail;
636         }
637
638         ifp = &sc->arpcom.ac_if;
639         ifp->if_softc = sc;
640         ifp->if_unit = unit;
641         ifp->if_name = "lge";
642         ifp->if_mtu = ETHERMTU;
643         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
644         ifp->if_ioctl = lge_ioctl;
645         ifp->if_output = ether_output;
646         ifp->if_start = lge_start;
647         ifp->if_watchdog = lge_watchdog;
648         ifp->if_init = lge_init;
649         ifp->if_baudrate = 1000000000;
650         ifp->if_snd.ifq_maxlen = LGE_TX_LIST_CNT - 1;
651         ifp->if_capabilities = IFCAP_RXCSUM;
652         ifp->if_capenable = ifp->if_capabilities;
653
654         if (CSR_READ_4(sc, LGE_GMIIMODE) & LGE_GMIIMODE_PCSENH)
655                 sc->lge_pcs = 1;
656         else
657                 sc->lge_pcs = 0;
658
659         /*
660          * Do MII setup.
661          */
662         if (mii_phy_probe(dev, &sc->lge_miibus,
663             lge_ifmedia_upd, lge_ifmedia_sts)) {
664                 printf("lge%d: MII without any PHY!\n", sc->lge_unit);
665                 contigfree(sc->lge_ldata,
666                     sizeof(struct lge_list_data), M_DEVBUF);
667                 lge_free_jumbo_mem(sc);
668                 bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
669                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
670                 bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
671                 error = ENXIO;
672                 goto fail;
673         }
674
675         /*
676          * Call MI attach routine.
677          */
678         ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
679         callout_handle_init(&sc->lge_stat_ch);
680
681 fail:
682         splx(s);
683         return(error);
684 }
685
686 static int lge_detach(dev)
687         device_t                dev;
688 {
689         struct lge_softc        *sc;
690         struct ifnet            *ifp;
691         int                     s;
692
693         s = splimp();
694
695         sc = device_get_softc(dev);
696         ifp = &sc->arpcom.ac_if;
697
698         lge_reset(sc);
699         lge_stop(sc);
700         ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
701
702         bus_generic_detach(dev);
703         device_delete_child(dev, sc->lge_miibus);
704
705         bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
706         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
707         bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
708
709         contigfree(sc->lge_ldata, sizeof(struct lge_list_data), M_DEVBUF);
710         lge_free_jumbo_mem(sc);
711
712         splx(s);
713
714         return(0);
715 }
716
717 /*
718  * Initialize the transmit descriptors.
719  */
720 static int lge_list_tx_init(sc)
721         struct lge_softc        *sc;
722 {
723         struct lge_list_data    *ld;
724         struct lge_ring_data    *cd;
725         int                     i;
726
727         cd = &sc->lge_cdata;
728         ld = sc->lge_ldata;
729         for (i = 0; i < LGE_TX_LIST_CNT; i++) {
730                 ld->lge_tx_list[i].lge_mbuf = NULL;
731                 ld->lge_tx_list[i].lge_ctl = 0;
732         }
733
734         cd->lge_tx_prod = cd->lge_tx_cons = 0;
735
736         return(0);
737 }
738
739
740 /*
741  * Initialize the RX descriptors and allocate mbufs for them. Note that
742  * we arralge the descriptors in a closed ring, so that the last descriptor
743  * points back to the first.
744  */
745 static int lge_list_rx_init(sc)
746         struct lge_softc        *sc;
747 {
748         struct lge_list_data    *ld;
749         struct lge_ring_data    *cd;
750         int                     i;
751
752         ld = sc->lge_ldata;
753         cd = &sc->lge_cdata;
754
755         cd->lge_rx_prod = cd->lge_rx_cons = 0;
756
757         CSR_WRITE_4(sc, LGE_RXDESC_ADDR_HI, 0);
758
759         for (i = 0; i < LGE_RX_LIST_CNT; i++) {
760                 if (CSR_READ_1(sc, LGE_RXCMDFREE_8BIT) == 0)
761                         break;
762                 if (lge_newbuf(sc, &ld->lge_rx_list[i], NULL) == ENOBUFS)
763                         return(ENOBUFS);
764         }
765
766         /* Clear possible 'rx command queue empty' interrupt. */
767         CSR_READ_4(sc, LGE_ISR);
768
769         return(0);
770 }
771
772 /*
773  * Initialize an RX descriptor and attach an MBUF cluster.
774  */
775 static int lge_newbuf(sc, c, m)
776         struct lge_softc        *sc;
777         struct lge_rx_desc      *c;
778         struct mbuf             *m;
779 {
780         struct mbuf             *m_new = NULL;
781         caddr_t                 *buf = NULL;
782
783         if (m == NULL) {
784                 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
785                 if (m_new == NULL) {
786                         printf("lge%d: no memory for rx list "
787                             "-- packet dropped!\n", sc->lge_unit);
788                         return(ENOBUFS);
789                 }
790
791                 /* Allocate the jumbo buffer */
792                 buf = lge_jalloc(sc);
793                 if (buf == NULL) {
794 #ifdef LGE_VERBOSE
795                         printf("lge%d: jumbo allocation failed "
796                             "-- packet dropped!\n", sc->lge_unit);
797 #endif
798                         m_freem(m_new);
799                         return(ENOBUFS);
800                 }
801                 /* Attach the buffer to the mbuf */
802                 m_new->m_data = m_new->m_ext.ext_buf = (void *)buf;
803                 m_new->m_flags |= M_EXT;
804                 m_new->m_ext.ext_size = m_new->m_pkthdr.len =
805                     m_new->m_len = LGE_MCLBYTES;
806                 m_new->m_ext.ext_free = lge_jfree;
807                 m_new->m_ext.ext_ref = lge_jref;
808         } else {
809                 m_new = m;
810                 m_new->m_len = m_new->m_pkthdr.len = LGE_MCLBYTES;
811                 m_new->m_data = m_new->m_ext.ext_buf;
812         }
813
814         /*
815          * Adjust alignment so packet payload begins on a
816          * longword boundary. Mandatory for Alpha, useful on
817          * x86 too.
818         */
819         m_adj(m_new, ETHER_ALIGN);
820
821         c->lge_mbuf = m_new;
822         c->lge_fragptr_hi = 0;
823         c->lge_fragptr_lo = vtophys(mtod(m_new, caddr_t));
824         c->lge_fraglen = m_new->m_len;
825         c->lge_ctl = m_new->m_len | LGE_RXCTL_WANTINTR | LGE_FRAGCNT(1);
826         c->lge_sts = 0;
827
828         /*
829          * Put this buffer in the RX command FIFO. To do this,
830          * we just write the physical address of the descriptor
831          * into the RX descriptor address registers. Note that
832          * there are two registers, one high DWORD and one low
833          * DWORD, which lets us specify a 64-bit address if
834          * desired. We only use a 32-bit address for now.
835          * Writing to the low DWORD register is what actually
836          * causes the command to be issued, so we do that
837          * last.
838          */
839         CSR_WRITE_4(sc, LGE_RXDESC_ADDR_LO, vtophys(c));
840         LGE_INC(sc->lge_cdata.lge_rx_prod, LGE_RX_LIST_CNT);
841
842         return(0);
843 }
844
845 static int lge_alloc_jumbo_mem(sc)
846         struct lge_softc        *sc;
847 {
848         caddr_t                 ptr;
849         register int            i;
850         struct lge_jpool_entry   *entry;
851
852         /* Grab a big chunk o' storage. */
853         sc->lge_cdata.lge_jumbo_buf = contigmalloc(LGE_JMEM, M_DEVBUF,
854             M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
855
856         if (sc->lge_cdata.lge_jumbo_buf == NULL) {
857                 printf("lge%d: no memory for jumbo buffers!\n", sc->lge_unit);
858                 return(ENOBUFS);
859         }
860
861         SLIST_INIT(&sc->lge_jfree_listhead);
862         SLIST_INIT(&sc->lge_jinuse_listhead);
863
864         /*
865          * Now divide it up into 9K pieces and save the addresses
866          * in an array.
867          */
868         ptr = sc->lge_cdata.lge_jumbo_buf;
869         for (i = 0; i < LGE_JSLOTS; i++) {
870                 u_int64_t               **aptr;
871                 aptr = (u_int64_t **)ptr;
872                 aptr[0] = (u_int64_t *)sc;
873                 ptr += sizeof(u_int64_t);
874                 sc->lge_cdata.lge_jslots[i].lge_buf = ptr;
875                 sc->lge_cdata.lge_jslots[i].lge_inuse = 0;
876                 ptr += LGE_MCLBYTES;
877                 entry = malloc(sizeof(struct lge_jpool_entry), 
878                     M_DEVBUF, M_NOWAIT);
879                 if (entry == NULL) {
880                         printf("lge%d: no memory for jumbo "
881                             "buffer queue!\n", sc->lge_unit);
882                         return(ENOBUFS);
883                 }
884                 entry->slot = i;
885                 SLIST_INSERT_HEAD(&sc->lge_jfree_listhead,
886                     entry, jpool_entries);
887         }
888
889         return(0);
890 }
891
892 static void lge_free_jumbo_mem(sc)
893         struct lge_softc        *sc;
894 {
895         int                     i;
896         struct lge_jpool_entry  *entry;
897
898         for (i = 0; i < LGE_JSLOTS; i++) {
899                 entry = SLIST_FIRST(&sc->lge_jfree_listhead);
900                 SLIST_REMOVE_HEAD(&sc->lge_jfree_listhead, jpool_entries);
901                 free(entry, M_DEVBUF);
902         }
903
904         contigfree(sc->lge_cdata.lge_jumbo_buf, LGE_JMEM, M_DEVBUF);
905
906         return;
907 }
908
909 /*
910  * Allocate a jumbo buffer.
911  */
912 static void *lge_jalloc(sc)
913         struct lge_softc        *sc;
914 {
915         struct lge_jpool_entry   *entry;
916         
917         entry = SLIST_FIRST(&sc->lge_jfree_listhead);
918         
919         if (entry == NULL) {
920 #ifdef LGE_VERBOSE
921                 printf("lge%d: no free jumbo buffers\n", sc->lge_unit);
922 #endif
923                 return(NULL);
924         }
925
926         SLIST_REMOVE_HEAD(&sc->lge_jfree_listhead, jpool_entries);
927         SLIST_INSERT_HEAD(&sc->lge_jinuse_listhead, entry, jpool_entries);
928         sc->lge_cdata.lge_jslots[entry->slot].lge_inuse = 1;
929         return(sc->lge_cdata.lge_jslots[entry->slot].lge_buf);
930 }
931
932 /*
933  * Adjust usage count on a jumbo buffer. In general this doesn't
934  * get used much because our jumbo buffers don't get passed around
935  * a lot, but it's implemented for correctness.
936  */
937 static void lge_jref(buf, size)
938         caddr_t                 buf;
939         u_int                   size;
940 {
941         struct lge_softc        *sc;
942         u_int64_t               **aptr;
943         register int            i;
944
945         /* Extract the softc struct pointer. */
946         aptr = (u_int64_t **)(buf - sizeof(u_int64_t));
947         sc = (struct lge_softc *)(aptr[0]);
948
949         if (sc == NULL)
950                 panic("lge_jref: can't find softc pointer!");
951
952         if (size != LGE_MCLBYTES)
953                 panic("lge_jref: adjusting refcount of buf of wrong size!");
954
955         /* calculate the slot this buffer belongs to */
956
957         i = ((vm_offset_t)aptr 
958              - (vm_offset_t)sc->lge_cdata.lge_jumbo_buf) / LGE_JLEN;
959
960         if ((i < 0) || (i >= LGE_JSLOTS))
961                 panic("lge_jref: asked to reference buffer "
962                     "that we don't manage!");
963         else if (sc->lge_cdata.lge_jslots[i].lge_inuse == 0)
964                 panic("lge_jref: buffer already free!");
965         else
966                 sc->lge_cdata.lge_jslots[i].lge_inuse++;
967
968         return;
969 }
970
971 /*
972  * Release a jumbo buffer.
973  */
974 static void lge_jfree(buf, size)
975         caddr_t                 buf;
976         u_int                   size;
977 {
978         struct lge_softc        *sc;
979         u_int64_t               **aptr;
980         int                     i;
981         struct lge_jpool_entry   *entry;
982
983         /* Extract the softc struct pointer. */
984         aptr = (u_int64_t **)(buf - sizeof(u_int64_t));
985         sc = (struct lge_softc *)(aptr[0]);
986
987         if (sc == NULL)
988                 panic("lge_jfree: can't find softc pointer!");
989
990         if (size != LGE_MCLBYTES)
991                 panic("lge_jfree: freeing buffer of wrong size!");
992
993         /* calculate the slot this buffer belongs to */
994         i = ((vm_offset_t)aptr
995              - (vm_offset_t)sc->lge_cdata.lge_jumbo_buf) / LGE_JLEN;
996
997         if ((i < 0) || (i >= LGE_JSLOTS))
998                 panic("lge_jfree: asked to free buffer that we don't manage!");
999         else if (sc->lge_cdata.lge_jslots[i].lge_inuse == 0)
1000                 panic("lge_jfree: buffer already free!");
1001         else {
1002                 sc->lge_cdata.lge_jslots[i].lge_inuse--;
1003                 if(sc->lge_cdata.lge_jslots[i].lge_inuse == 0) {
1004                         entry = SLIST_FIRST(&sc->lge_jinuse_listhead);
1005                         if (entry == NULL)
1006                                 panic("lge_jfree: buffer not in use!");
1007                         entry->slot = i;
1008                         SLIST_REMOVE_HEAD(&sc->lge_jinuse_listhead,
1009                             jpool_entries);
1010                         SLIST_INSERT_HEAD(&sc->lge_jfree_listhead,
1011                             entry, jpool_entries);
1012                 }
1013         }
1014
1015         return;
1016 }
1017
1018 /*
1019  * A frame has been uploaded: pass the resulting mbuf chain up to
1020  * the higher level protocols.
1021  */
1022 static void lge_rxeof(sc, cnt)
1023         struct lge_softc        *sc;
1024         int                     cnt;
1025 {
1026         struct ether_header     *eh;
1027         struct mbuf             *m;
1028         struct ifnet            *ifp;
1029         struct lge_rx_desc      *cur_rx;
1030         int                     c, i, total_len = 0;
1031         u_int32_t               rxsts, rxctl;
1032
1033         ifp = &sc->arpcom.ac_if;
1034
1035         /* Find out how many frames were processed. */
1036         c = cnt;
1037         i = sc->lge_cdata.lge_rx_cons;
1038
1039         /* Suck them in. */
1040         while(c) {
1041                 struct mbuf             *m0 = NULL;
1042
1043                 cur_rx = &sc->lge_ldata->lge_rx_list[i];
1044                 rxctl = cur_rx->lge_ctl;
1045                 rxsts = cur_rx->lge_sts;
1046                 m = cur_rx->lge_mbuf;
1047                 cur_rx->lge_mbuf = NULL;
1048                 total_len = LGE_RXBYTES(cur_rx);
1049                 LGE_INC(i, LGE_RX_LIST_CNT);
1050                 c--;
1051
1052                 /*
1053                  * If an error occurs, update stats, clear the
1054                  * status word and leave the mbuf cluster in place:
1055                  * it should simply get re-used next time this descriptor
1056                  * comes up in the ring.
1057                  */
1058                 if (rxctl & LGE_RXCTL_ERRMASK) {
1059                         ifp->if_ierrors++;
1060                         lge_newbuf(sc, &LGE_RXTAIL(sc), m);
1061                         continue;
1062                 }
1063
1064                 if (lge_newbuf(sc, &LGE_RXTAIL(sc), NULL) == ENOBUFS) {
1065                         m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1066                             total_len + ETHER_ALIGN, 0, ifp, NULL);
1067                         lge_newbuf(sc, &LGE_RXTAIL(sc), m);
1068                         if (m0 == NULL) {
1069                                 printf("lge%d: no receive buffers "
1070                                     "available -- packet dropped!\n",
1071                                     sc->lge_unit);
1072                                 ifp->if_ierrors++;
1073                                 continue;
1074                         }
1075                         m_adj(m0, ETHER_ALIGN);
1076                         m = m0;
1077                 } else {
1078                         m->m_pkthdr.rcvif = ifp;
1079                         m->m_pkthdr.len = m->m_len = total_len;
1080                 }
1081
1082                 ifp->if_ipackets++;
1083                 eh = mtod(m, struct ether_header *);
1084
1085                 /* Remove header from mbuf and pass it on. */
1086                 m_adj(m, sizeof(struct ether_header));
1087
1088                 /* Do IP checksum checking. */
1089                 if (rxsts & LGE_RXSTS_ISIP)
1090                         m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1091                 if (!(rxsts & LGE_RXSTS_IPCSUMERR))
1092                         m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1093                 if ((rxsts & LGE_RXSTS_ISTCP &&
1094                     !(rxsts & LGE_RXSTS_TCPCSUMERR)) ||
1095                     (rxsts & LGE_RXSTS_ISUDP &&
1096                     !(rxsts & LGE_RXSTS_UDPCSUMERR))) {
1097                         m->m_pkthdr.csum_flags |=
1098                             CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1099                         m->m_pkthdr.csum_data = 0xffff;
1100                 }
1101
1102                 ether_input(ifp, eh, m);
1103         }
1104
1105         sc->lge_cdata.lge_rx_cons = i;
1106
1107         return;
1108 }
1109
1110 void lge_rxeoc(sc)
1111         struct lge_softc        *sc;
1112 {
1113         struct ifnet            *ifp;
1114
1115         ifp = &sc->arpcom.ac_if;
1116         ifp->if_flags &= ~IFF_RUNNING;
1117         lge_init(sc);
1118         return;
1119 }
1120
1121 /*
1122  * A frame was downloaded to the chip. It's safe for us to clean up
1123  * the list buffers.
1124  */
1125
1126 static void lge_txeof(sc)
1127         struct lge_softc        *sc;
1128 {
1129         struct lge_tx_desc      *cur_tx = NULL;
1130         struct ifnet            *ifp;
1131         u_int32_t               idx, txdone;
1132
1133         ifp = &sc->arpcom.ac_if;
1134
1135         /* Clear the timeout timer. */
1136         ifp->if_timer = 0;
1137
1138         /*
1139          * Go through our tx list and free mbufs for those
1140          * frames that have been transmitted.
1141          */
1142         idx = sc->lge_cdata.lge_tx_cons;
1143         txdone = CSR_READ_1(sc, LGE_TXDMADONE_8BIT);
1144
1145         while (idx != sc->lge_cdata.lge_tx_prod && txdone) {
1146                 cur_tx = &sc->lge_ldata->lge_tx_list[idx];
1147
1148                 ifp->if_opackets++;
1149                 if (cur_tx->lge_mbuf != NULL) {
1150                         m_freem(cur_tx->lge_mbuf);
1151                         cur_tx->lge_mbuf = NULL;
1152                 }
1153                 cur_tx->lge_ctl = 0;
1154
1155                 txdone--;
1156                 LGE_INC(idx, LGE_TX_LIST_CNT);
1157                 ifp->if_timer = 0;
1158         }
1159
1160         sc->lge_cdata.lge_tx_cons = idx;
1161
1162         if (cur_tx != NULL)
1163                 ifp->if_flags &= ~IFF_OACTIVE;
1164
1165         return;
1166 }
1167
1168 static void lge_tick(xsc)
1169         void                    *xsc;
1170 {
1171         struct lge_softc        *sc;
1172         struct mii_data         *mii;
1173         struct ifnet            *ifp;
1174         int                     s;
1175
1176         s = splimp();
1177
1178         sc = xsc;
1179         ifp = &sc->arpcom.ac_if;
1180
1181         CSR_WRITE_4(sc, LGE_STATSIDX, LGE_STATS_SINGLE_COLL_PKTS);
1182         ifp->if_collisions += CSR_READ_4(sc, LGE_STATSVAL);
1183         CSR_WRITE_4(sc, LGE_STATSIDX, LGE_STATS_MULTI_COLL_PKTS);
1184         ifp->if_collisions += CSR_READ_4(sc, LGE_STATSVAL);
1185
1186         if (!sc->lge_link) {
1187                 mii = device_get_softc(sc->lge_miibus);
1188                 mii_tick(mii);
1189                 mii_pollstat(mii);
1190                 if (mii->mii_media_status & IFM_ACTIVE &&
1191                     IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1192                         sc->lge_link++;
1193                         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX||
1194                             IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_TX)
1195                                 printf("lge%d: gigabit link up\n",
1196                                     sc->lge_unit);
1197                         if (ifp->if_snd.ifq_head != NULL)
1198                                 lge_start(ifp);
1199                 }
1200         }
1201
1202         sc->lge_stat_ch = timeout(lge_tick, sc, hz);
1203
1204         splx(s);
1205
1206         return;
1207 }
1208
1209 static void lge_intr(arg)
1210         void                    *arg;
1211 {
1212         struct lge_softc        *sc;
1213         struct ifnet            *ifp;
1214         u_int32_t               status;
1215
1216         sc = arg;
1217         ifp = &sc->arpcom.ac_if;
1218
1219         /* Supress unwanted interrupts */
1220         if (!(ifp->if_flags & IFF_UP)) {
1221                 lge_stop(sc);
1222                 return;
1223         }
1224
1225         for (;;) {
1226                 /*
1227                  * Reading the ISR register clears all interrupts, and
1228                  * clears the 'interrupts enabled' bit in the IMR
1229                  * register.
1230                  */
1231                 status = CSR_READ_4(sc, LGE_ISR);
1232
1233                 if ((status & LGE_INTRS) == 0)
1234                         break;
1235
1236                 if ((status & (LGE_ISR_TXCMDFIFO_EMPTY|LGE_ISR_TXDMA_DONE)))
1237                         lge_txeof(sc);
1238
1239                 if (status & LGE_ISR_RXDMA_DONE)
1240                         lge_rxeof(sc, LGE_RX_DMACNT(status));
1241
1242                 if (status & LGE_ISR_RXCMDFIFO_EMPTY)
1243                         lge_rxeoc(sc);
1244
1245                 if (status & LGE_ISR_PHY_INTR) {
1246                         sc->lge_link = 0;
1247                         untimeout(lge_tick, sc, sc->lge_stat_ch);
1248                         lge_tick(sc);
1249                 }
1250         }
1251
1252         /* Re-enable interrupts. */
1253         CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL0|LGE_IMR_INTR_ENB);
1254
1255         if (ifp->if_snd.ifq_head != NULL)
1256                 lge_start(ifp);
1257
1258         return;
1259 }
1260
1261 /*
1262  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1263  * pointers to the fragment pointers.
1264  */
1265 static int lge_encap(sc, m_head, txidx)
1266         struct lge_softc        *sc;
1267         struct mbuf             *m_head;
1268         u_int32_t               *txidx;
1269 {
1270         struct lge_frag         *f = NULL;
1271         struct lge_tx_desc      *cur_tx;
1272         struct mbuf             *m;
1273         int                     frag = 0, tot_len = 0;
1274
1275         /*
1276          * Start packing the mbufs in this chain into
1277          * the fragment pointers. Stop when we run out
1278          * of fragments or hit the end of the mbuf chain.
1279          */
1280         m = m_head;
1281         cur_tx = &sc->lge_ldata->lge_tx_list[*txidx];
1282         frag = 0;
1283
1284         for (m = m_head; m != NULL; m = m->m_next) {
1285                 if (m->m_len != 0) {
1286                         tot_len += m->m_len;
1287                         f = &cur_tx->lge_frags[frag];
1288                         f->lge_fraglen = m->m_len;
1289                         f->lge_fragptr_lo = vtophys(mtod(m, vm_offset_t));
1290                         f->lge_fragptr_hi = 0;
1291                         frag++;
1292                 }
1293         }
1294
1295         if (m != NULL)
1296                 return(ENOBUFS);
1297
1298         cur_tx->lge_mbuf = m_head;
1299         cur_tx->lge_ctl = LGE_TXCTL_WANTINTR|LGE_FRAGCNT(frag)|tot_len;
1300         LGE_INC((*txidx), LGE_TX_LIST_CNT);
1301
1302         /* Queue for transmit */
1303         CSR_WRITE_4(sc, LGE_TXDESC_ADDR_LO, vtophys(cur_tx));
1304
1305         return(0);
1306 }
1307
1308 /*
1309  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1310  * to the mbuf data regions directly in the transmit lists. We also save a
1311  * copy of the pointers since the transmit list fragment pointers are
1312  * physical addresses.
1313  */
1314
1315 static void lge_start(ifp)
1316         struct ifnet            *ifp;
1317 {
1318         struct lge_softc        *sc;
1319         struct mbuf             *m_head = NULL;
1320         u_int32_t               idx;
1321
1322         sc = ifp->if_softc;
1323
1324         if (!sc->lge_link)
1325                 return;
1326
1327         idx = sc->lge_cdata.lge_tx_prod;
1328
1329         if (ifp->if_flags & IFF_OACTIVE)
1330                 return;
1331
1332         while(sc->lge_ldata->lge_tx_list[idx].lge_mbuf == NULL) {
1333                 if (CSR_READ_1(sc, LGE_TXCMDFREE_8BIT) == 0)
1334                         break;
1335
1336                 IF_DEQUEUE(&ifp->if_snd, m_head);
1337                 if (m_head == NULL)
1338                         break;
1339
1340                 if (lge_encap(sc, m_head, &idx)) {
1341                         IF_PREPEND(&ifp->if_snd, m_head);
1342                         ifp->if_flags |= IFF_OACTIVE;
1343                         break;
1344                 }
1345
1346                 /*
1347                  * If there's a BPF listener, bounce a copy of this frame
1348                  * to him.
1349                  */
1350                 if (ifp->if_bpf)
1351                         bpf_mtap(ifp, m_head);
1352         }
1353
1354         sc->lge_cdata.lge_tx_prod = idx;
1355
1356         /*
1357          * Set a timeout in case the chip goes out to lunch.
1358          */
1359         ifp->if_timer = 5;
1360
1361         return;
1362 }
1363
1364 static void lge_init(xsc)
1365         void                    *xsc;
1366 {
1367         struct lge_softc        *sc = xsc;
1368         struct ifnet            *ifp = &sc->arpcom.ac_if;
1369         struct mii_data         *mii;
1370         int                     s;
1371
1372         if (ifp->if_flags & IFF_RUNNING)
1373                 return;
1374
1375         s = splimp();
1376
1377         /*
1378          * Cancel pending I/O and free all RX/TX buffers.
1379          */
1380         lge_stop(sc);
1381         lge_reset(sc);
1382
1383         mii = device_get_softc(sc->lge_miibus);
1384
1385         /* Set MAC address */
1386         CSR_WRITE_4(sc, LGE_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
1387         CSR_WRITE_4(sc, LGE_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
1388
1389         /* Init circular RX list. */
1390         if (lge_list_rx_init(sc) == ENOBUFS) {
1391                 printf("lge%d: initialization failed: no "
1392                     "memory for rx buffers\n", sc->lge_unit);
1393                 lge_stop(sc);
1394                 (void)splx(s);
1395                 return;
1396         }
1397
1398         /*
1399          * Init tx descriptors.
1400          */
1401         lge_list_tx_init(sc);
1402
1403         /* Set initial value for MODE1 register. */
1404         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_UCAST|
1405             LGE_MODE1_TX_CRC|LGE_MODE1_TXPAD|
1406             LGE_MODE1_RX_FLOWCTL|LGE_MODE1_SETRST_CTL0|
1407             LGE_MODE1_SETRST_CTL1|LGE_MODE1_SETRST_CTL2);
1408
1409          /* If we want promiscuous mode, set the allframes bit. */
1410         if (ifp->if_flags & IFF_PROMISC) {
1411                 CSR_WRITE_4(sc, LGE_MODE1,
1412                     LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_PROMISC);
1413         } else {
1414                 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_PROMISC);
1415         }
1416
1417         /*
1418          * Set the capture broadcast bit to capture broadcast frames.
1419          */
1420         if (ifp->if_flags & IFF_BROADCAST) {
1421                 CSR_WRITE_4(sc, LGE_MODE1,
1422                     LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_BCAST);
1423         } else {
1424                 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_BCAST);
1425         }
1426
1427         /* Packet padding workaround? */
1428         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RMVPAD);
1429
1430         /* No error frames */
1431         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_ERRPKTS);
1432
1433         /* Receive large frames */
1434         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_GIANTS);
1435
1436         /* Workaround: disable RX/TX flow control */
1437         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_TX_FLOWCTL);
1438         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_FLOWCTL);
1439
1440         /* Make sure to strip CRC from received frames */
1441         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_CRC);
1442
1443         /* Turn off magic packet mode */
1444         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_MPACK_ENB);
1445
1446         /* Turn off all VLAN stuff */
1447         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_VLAN_RX|LGE_MODE1_VLAN_TX|
1448             LGE_MODE1_VLAN_STRIP|LGE_MODE1_VLAN_INSERT);
1449
1450         /* Workarond: FIFO overflow */
1451         CSR_WRITE_2(sc, LGE_RXFIFO_HIWAT, 0x3FFF);
1452         CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL1|LGE_IMR_RXFIFO_WAT);
1453
1454         /*
1455          * Load the multicast filter.
1456          */
1457         lge_setmulti(sc);
1458
1459         /*
1460          * Enable hardware checksum validation for all received IPv4
1461          * packets, do not reject packets with bad checksums.
1462          */
1463         CSR_WRITE_4(sc, LGE_MODE2, LGE_MODE2_RX_IPCSUM|
1464             LGE_MODE2_RX_TCPCSUM|LGE_MODE2_RX_UDPCSUM|
1465             LGE_MODE2_RX_ERRCSUM);
1466
1467         /*
1468          * Enable the delivery of PHY interrupts based on
1469          * link/speed/duplex status chalges.
1470          */
1471         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL0|LGE_MODE1_GMIIPOLL);
1472
1473         /* Enable receiver and transmitter. */
1474         CSR_WRITE_4(sc, LGE_RXDESC_ADDR_HI, 0);
1475         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_ENB);
1476
1477         CSR_WRITE_4(sc, LGE_TXDESC_ADDR_HI, 0);
1478         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_TX_ENB);
1479
1480         /*
1481          * Enable interrupts.
1482          */
1483         CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL0|
1484             LGE_IMR_SETRST_CTL1|LGE_IMR_INTR_ENB|LGE_INTRS);
1485
1486         lge_ifmedia_upd(ifp);
1487
1488         ifp->if_flags |= IFF_RUNNING;
1489         ifp->if_flags &= ~IFF_OACTIVE;
1490
1491         (void)splx(s);
1492
1493         sc->lge_stat_ch = timeout(lge_tick, sc, hz);
1494
1495         return;
1496 }
1497
1498 /*
1499  * Set media options.
1500  */
1501 static int lge_ifmedia_upd(ifp)
1502         struct ifnet            *ifp;
1503 {
1504         struct lge_softc        *sc;
1505         struct mii_data         *mii;
1506
1507         sc = ifp->if_softc;
1508
1509         mii = device_get_softc(sc->lge_miibus);
1510         sc->lge_link = 0;
1511         if (mii->mii_instance) {
1512                 struct mii_softc        *miisc;
1513                 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1514                     miisc = LIST_NEXT(miisc, mii_list))
1515                         mii_phy_reset(miisc);
1516         }
1517         mii_mediachg(mii);
1518
1519         return(0);
1520 }
1521
1522 /*
1523  * Report current media status.
1524  */
1525 static void lge_ifmedia_sts(ifp, ifmr)
1526         struct ifnet            *ifp;
1527         struct ifmediareq       *ifmr;
1528 {
1529         struct lge_softc        *sc;
1530         struct mii_data         *mii;
1531
1532         sc = ifp->if_softc;
1533
1534         mii = device_get_softc(sc->lge_miibus);
1535         mii_pollstat(mii);
1536         ifmr->ifm_active = mii->mii_media_active;
1537         ifmr->ifm_status = mii->mii_media_status;
1538
1539         return;
1540 }
1541
1542 static int lge_ioctl(ifp, command, data)
1543         struct ifnet            *ifp;
1544         u_long                  command;
1545         caddr_t                 data;
1546 {
1547         struct lge_softc        *sc = ifp->if_softc;
1548         struct ifreq            *ifr = (struct ifreq *) data;
1549         struct mii_data         *mii;
1550         int                     s, error = 0;
1551
1552         s = splimp();
1553
1554         switch(command) {
1555         case SIOCSIFADDR:
1556         case SIOCGIFADDR:
1557                 error = ether_ioctl(ifp, command, data);
1558                 break;
1559         case SIOCSIFMTU:
1560                 if (ifr->ifr_mtu > LGE_JUMBO_MTU)
1561                         error = EINVAL;
1562                 else
1563                         ifp->if_mtu = ifr->ifr_mtu;
1564                 break;
1565         case SIOCSIFFLAGS:
1566                 if (ifp->if_flags & IFF_UP) {
1567                         if (ifp->if_flags & IFF_RUNNING &&
1568                             ifp->if_flags & IFF_PROMISC &&
1569                             !(sc->lge_if_flags & IFF_PROMISC)) {
1570                                 CSR_WRITE_4(sc, LGE_MODE1,
1571                                     LGE_MODE1_SETRST_CTL1|
1572                                     LGE_MODE1_RX_PROMISC);
1573                         } else if (ifp->if_flags & IFF_RUNNING &&
1574                             !(ifp->if_flags & IFF_PROMISC) &&
1575                             sc->lge_if_flags & IFF_PROMISC) {
1576                                 CSR_WRITE_4(sc, LGE_MODE1,
1577                                     LGE_MODE1_RX_PROMISC);
1578                         } else {
1579                                 ifp->if_flags &= ~IFF_RUNNING;
1580                                 lge_init(sc);
1581                         }
1582                 } else {
1583                         if (ifp->if_flags & IFF_RUNNING)
1584                                 lge_stop(sc);
1585                 }
1586                 sc->lge_if_flags = ifp->if_flags;
1587                 error = 0;
1588                 break;
1589         case SIOCADDMULTI:
1590         case SIOCDELMULTI:
1591                 lge_setmulti(sc);
1592                 error = 0;
1593                 break;
1594         case SIOCGIFMEDIA:
1595         case SIOCSIFMEDIA:
1596                 mii = device_get_softc(sc->lge_miibus);
1597                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1598                 break;
1599         default:
1600                 error = EINVAL;
1601                 break;
1602         }
1603
1604         (void)splx(s);
1605
1606         return(error);
1607 }
1608
1609 static void lge_watchdog(ifp)
1610         struct ifnet            *ifp;
1611 {
1612         struct lge_softc        *sc;
1613
1614         sc = ifp->if_softc;
1615
1616         ifp->if_oerrors++;
1617         printf("lge%d: watchdog timeout\n", sc->lge_unit);
1618
1619         lge_stop(sc);
1620         lge_reset(sc);
1621         ifp->if_flags &= ~IFF_RUNNING;
1622         lge_init(sc);
1623
1624         if (ifp->if_snd.ifq_head != NULL)
1625                 lge_start(ifp);
1626
1627         return;
1628 }
1629
1630 /*
1631  * Stop the adapter and free any mbufs allocated to the
1632  * RX and TX lists.
1633  */
1634 static void lge_stop(sc)
1635         struct lge_softc        *sc;
1636 {
1637         register int            i;
1638         struct ifnet            *ifp;
1639
1640         ifp = &sc->arpcom.ac_if;
1641         ifp->if_timer = 0;
1642         untimeout(lge_tick, sc, sc->lge_stat_ch);
1643         CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_INTR_ENB);
1644
1645         /* Disable receiver and transmitter. */
1646         CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_ENB|LGE_MODE1_TX_ENB);
1647         sc->lge_link = 0;
1648
1649         /*
1650          * Free data in the RX lists.
1651          */
1652         for (i = 0; i < LGE_RX_LIST_CNT; i++) {
1653                 if (sc->lge_ldata->lge_rx_list[i].lge_mbuf != NULL) {
1654                         m_freem(sc->lge_ldata->lge_rx_list[i].lge_mbuf);
1655                         sc->lge_ldata->lge_rx_list[i].lge_mbuf = NULL;
1656                 }
1657         }
1658         bzero((char *)&sc->lge_ldata->lge_rx_list,
1659                 sizeof(sc->lge_ldata->lge_rx_list));
1660
1661         /*
1662          * Free the TX list buffers.
1663          */
1664         for (i = 0; i < LGE_TX_LIST_CNT; i++) {
1665                 if (sc->lge_ldata->lge_tx_list[i].lge_mbuf != NULL) {
1666                         m_freem(sc->lge_ldata->lge_tx_list[i].lge_mbuf);
1667                         sc->lge_ldata->lge_tx_list[i].lge_mbuf = NULL;
1668                 }
1669         }
1670
1671         bzero((char *)&sc->lge_ldata->lge_tx_list,
1672                 sizeof(sc->lge_ldata->lge_tx_list));
1673
1674         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1675
1676         return;
1677 }
1678
1679 /*
1680  * Stop all chip I/O so that the kernel's probe routines don't
1681  * get confused by errant DMAs when rebooting.
1682  */
1683 static void lge_shutdown(dev)
1684         device_t                dev;
1685 {
1686         struct lge_softc        *sc;
1687
1688         sc = device_get_softc(dev);
1689
1690         lge_reset(sc);
1691         lge_stop(sc);
1692
1693         return;
1694 }