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