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