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