]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/pci/if_ax.c
This commit was generated by cvs2svn to compensate for changes in r42788,
[FreeBSD/FreeBSD.git] / sys / pci / if_ax.c
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *      $Id: if_ax.c,v 1.8 1999/01/16 20:33:34 wpaul Exp $
33  */
34
35 /*
36  * ASIX AX88140A fast ethernet PCI NIC driver.
37  *
38  * Written by Bill Paul <wpaul@ctr.columbia.edu>
39  * Electrical Engineering Department
40  * Columbia University, New York City
41  */
42
43 /*
44  * The ASIX Electronics AX88140A is still another DEC 21x4x clone. It's
45  * a reasonably close copy of the tulip, except for the receiver filter
46  * programming. Where the DEC chip has a special setup frame that
47  * needs to be downloaded into the transmit DMA engine, the ASIX chip
48  * has a less complicated setup frame which is written into one of
49  * the registers.
50  */
51
52 #include "bpfilter.h"
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/sockio.h>
57 #include <sys/mbuf.h>
58 #include <sys/malloc.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
61
62 #include <net/if.h>
63 #include <net/if_arp.h>
64 #include <net/ethernet.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67
68 #if NBPFILTER > 0
69 #include <net/bpf.h>
70 #endif
71
72 #include <vm/vm.h>              /* for vtophys */
73 #include <vm/pmap.h>            /* for vtophys */
74 #include <machine/clock.h>      /* for DELAY */
75 #include <machine/bus_pio.h>
76 #include <machine/bus_memio.h>
77 #include <machine/bus.h>
78
79 #include <pci/pcireg.h>
80 #include <pci/pcivar.h>
81
82 #define AX_USEIOSPACE
83
84 /* #define AX_BACKGROUND_AUTONEG */
85
86 #include <pci/if_axreg.h>
87
88 #ifndef lint
89 static const char rcsid[] =
90         "$Id: if_ax.c,v 1.8 1999/01/16 20:33:34 wpaul Exp $";
91 #endif
92
93 /*
94  * Various supported device vendors/types and their names.
95  */
96 static struct ax_type ax_devs[] = {
97         { AX_VENDORID, AX_DEVICEID_AX88140A,
98                 "ASIX AX88140A 10/100BaseTX" },
99         { 0, 0, NULL }
100 };
101
102 /*
103  * Various supported PHY vendors/types and their names. Note that
104  * this driver will work with pretty much any MII-compliant PHY,
105  * so failure to positively identify the chip is not a fatal error.
106  */
107
108 static struct ax_type ax_phys[] = {
109         { TI_PHY_VENDORID, TI_PHY_10BT, "<TI ThunderLAN 10BT (internal)>" },
110         { TI_PHY_VENDORID, TI_PHY_100VGPMI, "<TI TNETE211 100VG Any-LAN>" },
111         { NS_PHY_VENDORID, NS_PHY_83840A, "<National Semiconductor DP83840A>"},
112         { LEVEL1_PHY_VENDORID, LEVEL1_PHY_LXT970, "<Level 1 LXT970>" }, 
113         { INTEL_PHY_VENDORID, INTEL_PHY_82555, "<Intel 82555>" },
114         { SEEQ_PHY_VENDORID, SEEQ_PHY_80220, "<SEEQ 80220>" },
115         { 0, 0, "<MII-compliant physical interface>" }
116 };
117
118 static unsigned long ax_count = 0;
119 static const char *ax_probe     __P((pcici_t, pcidi_t));
120 static void ax_attach           __P((pcici_t, int));
121
122 static int ax_newbuf            __P((struct ax_softc *,
123                                                 struct ax_chain_onefrag *));
124 static int ax_encap             __P((struct ax_softc *, struct ax_chain *,
125                                                 struct mbuf *));
126
127 static void ax_rxeof            __P((struct ax_softc *));
128 static void ax_rxeoc            __P((struct ax_softc *));
129 static void ax_txeof            __P((struct ax_softc *));
130 static void ax_txeoc            __P((struct ax_softc *));
131 static void ax_intr             __P((void *));
132 static void ax_start            __P((struct ifnet *));
133 static int ax_ioctl             __P((struct ifnet *, u_long, caddr_t));
134 static void ax_init             __P((void *));
135 static void ax_stop             __P((struct ax_softc *));
136 static void ax_watchdog         __P((struct ifnet *));
137 static void ax_shutdown         __P((int, void *));
138 static int ax_ifmedia_upd       __P((struct ifnet *));
139 static void ax_ifmedia_sts      __P((struct ifnet *, struct ifmediareq *));
140
141 static void ax_delay            __P((struct ax_softc *));
142 static void ax_eeprom_idle      __P((struct ax_softc *));
143 static void ax_eeprom_putbyte   __P((struct ax_softc *, int));
144 static void ax_eeprom_getword   __P((struct ax_softc *, int, u_int16_t *));
145 static void ax_read_eeprom      __P((struct ax_softc *, caddr_t, int,
146                                                         int, int));
147
148 static void ax_mii_writebit     __P((struct ax_softc *, int));
149 static int ax_mii_readbit       __P((struct ax_softc *));
150 static void ax_mii_sync         __P((struct ax_softc *));
151 static void ax_mii_send         __P((struct ax_softc *, u_int32_t, int));
152 static int ax_mii_readreg       __P((struct ax_softc *, struct ax_mii_frame *));
153 static int ax_mii_writereg      __P((struct ax_softc *, struct ax_mii_frame *));
154 static u_int16_t ax_phy_readreg __P((struct ax_softc *, int));
155 static void ax_phy_writereg     __P((struct ax_softc *, int, int));
156
157 static void ax_autoneg_xmit     __P((struct ax_softc *));
158 static void ax_autoneg_mii      __P((struct ax_softc *, int, int));
159 static void ax_setmode_mii      __P((struct ax_softc *, int));
160 static void ax_setmode          __P((struct ax_softc *, int, int));
161 static void ax_getmode_mii      __P((struct ax_softc *));
162 static void ax_setcfg           __P((struct ax_softc *, int));
163 static u_int32_t ax_calchash    __P((caddr_t));
164 static void ax_setmulti         __P((struct ax_softc *));
165 static void ax_reset            __P((struct ax_softc *));
166 static int ax_list_rx_init      __P((struct ax_softc *));
167 static int ax_list_tx_init      __P((struct ax_softc *));
168
169 #define AX_SETBIT(sc, reg, x)                           \
170         CSR_WRITE_4(sc, reg,                            \
171                 CSR_READ_4(sc, reg) | x)
172
173 #define AX_CLRBIT(sc, reg, x)                           \
174         CSR_WRITE_4(sc, reg,                            \
175                 CSR_READ_4(sc, reg) & ~x)
176
177 #define SIO_SET(x)                                      \
178         CSR_WRITE_4(sc, AX_SIO,                         \
179                 CSR_READ_4(sc, AX_SIO) | x)
180
181 #define SIO_CLR(x)                                      \
182         CSR_WRITE_4(sc, AX_SIO,                         \
183                 CSR_READ_4(sc, AX_SIO) & ~x)
184
185 static void ax_delay(sc)
186         struct ax_softc         *sc;
187 {
188         int                     idx;
189
190         for (idx = (300 / 33) + 1; idx > 0; idx--)
191                 CSR_READ_4(sc, AX_BUSCTL);
192 }
193
194 static void ax_eeprom_idle(sc)
195         struct ax_softc         *sc;
196 {
197         register int            i;
198
199         CSR_WRITE_4(sc, AX_SIO, AX_SIO_EESEL);
200         ax_delay(sc);
201         AX_SETBIT(sc, AX_SIO,  AX_SIO_ROMCTL_READ);
202         ax_delay(sc);
203         AX_SETBIT(sc, AX_SIO, AX_SIO_EE_CS);
204         ax_delay(sc);
205         AX_SETBIT(sc, AX_SIO, AX_SIO_EE_CLK);
206         ax_delay(sc);
207
208         for (i = 0; i < 25; i++) {
209                 AX_CLRBIT(sc, AX_SIO, AX_SIO_EE_CLK);
210                 ax_delay(sc);
211                 AX_SETBIT(sc, AX_SIO, AX_SIO_EE_CLK);
212                 ax_delay(sc);
213         }
214
215         AX_CLRBIT(sc, AX_SIO, AX_SIO_EE_CLK);
216         ax_delay(sc);
217         AX_CLRBIT(sc, AX_SIO, AX_SIO_EE_CS);
218         ax_delay(sc);
219         CSR_WRITE_4(sc, AX_SIO, 0x00000000);
220
221         return;
222 }
223
224 /*
225  * Send a read command and address to the EEPROM, check for ACK.
226  */
227 static void ax_eeprom_putbyte(sc, addr)
228         struct ax_softc         *sc;
229         int                     addr;
230 {
231         register int            d, i;
232
233         d = addr | AX_EECMD_READ;
234
235         /*
236          * Feed in each bit and stobe the clock.
237          */
238         for (i = 0x400; i; i >>= 1) {
239                 if (d & i) {
240                         SIO_SET(AX_SIO_EE_DATAIN);
241                 } else {
242                         SIO_CLR(AX_SIO_EE_DATAIN);
243                 }
244                 ax_delay(sc);
245                 SIO_SET(AX_SIO_EE_CLK);
246                 ax_delay(sc);
247                 SIO_CLR(AX_SIO_EE_CLK);
248                 ax_delay(sc);
249         }
250
251         return;
252 }
253
254 /*
255  * Read a word of data stored in the EEPROM at address 'addr.'
256  */
257 static void ax_eeprom_getword(sc, addr, dest)
258         struct ax_softc         *sc;
259         int                     addr;
260         u_int16_t               *dest;
261 {
262         register int            i;
263         u_int16_t               word = 0;
264
265         /* Force EEPROM to idle state. */
266         ax_eeprom_idle(sc);
267
268         /* Enter EEPROM access mode. */
269         CSR_WRITE_4(sc, AX_SIO, AX_SIO_EESEL);
270         ax_delay(sc);
271         AX_SETBIT(sc, AX_SIO,  AX_SIO_ROMCTL_READ);
272         ax_delay(sc);
273         AX_SETBIT(sc, AX_SIO, AX_SIO_EE_CS);
274         ax_delay(sc);
275         AX_SETBIT(sc, AX_SIO, AX_SIO_EE_CLK);
276         ax_delay(sc);
277
278         /*
279          * Send address of word we want to read.
280          */
281         ax_eeprom_putbyte(sc, addr);
282
283         /*
284          * Start reading bits from EEPROM.
285          */
286         for (i = 0x8000; i; i >>= 1) {
287                 SIO_SET(AX_SIO_EE_CLK);
288                 ax_delay(sc);
289                 if (CSR_READ_4(sc, AX_SIO) & AX_SIO_EE_DATAOUT)
290                         word |= i;
291                 ax_delay(sc);
292                 SIO_CLR(AX_SIO_EE_CLK);
293                 ax_delay(sc);
294         }
295
296         /* Turn off EEPROM access mode. */
297         ax_eeprom_idle(sc);
298
299         *dest = word;
300
301         return;
302 }
303
304 /*
305  * Read a sequence of words from the EEPROM.
306  */
307 static void ax_read_eeprom(sc, dest, off, cnt, swap)
308         struct ax_softc         *sc;
309         caddr_t                 dest;
310         int                     off;
311         int                     cnt;
312         int                     swap;
313 {
314         int                     i;
315         u_int16_t               word = 0, *ptr;
316
317         for (i = 0; i < cnt; i++) {
318                 ax_eeprom_getword(sc, off + i, &word);
319                 ptr = (u_int16_t *)(dest + (i * 2));
320                 if (swap)
321                         *ptr = ntohs(word);
322                 else
323                         *ptr = word;
324         }
325
326         return;
327 }
328
329 /*
330  * Write a bit to the MII bus.
331  */
332 static void ax_mii_writebit(sc, bit)
333         struct ax_softc         *sc;
334         int                     bit;
335 {
336         if (bit)
337                 CSR_WRITE_4(sc, AX_SIO, AX_SIO_ROMCTL_WRITE|AX_SIO_MII_DATAOUT);
338         else
339                 CSR_WRITE_4(sc, AX_SIO, AX_SIO_ROMCTL_WRITE);
340
341         AX_SETBIT(sc, AX_SIO, AX_SIO_MII_CLK);
342         AX_CLRBIT(sc, AX_SIO, AX_SIO_MII_CLK);
343
344         return;
345 }
346
347 /*
348  * Read a bit from the MII bus.
349  */
350 static int ax_mii_readbit(sc)
351         struct ax_softc         *sc;
352 {
353         CSR_WRITE_4(sc, AX_SIO, AX_SIO_ROMCTL_READ|AX_SIO_MII_DIR);
354         CSR_READ_4(sc, AX_SIO);
355         AX_SETBIT(sc, AX_SIO, AX_SIO_MII_CLK);
356         AX_CLRBIT(sc, AX_SIO, AX_SIO_MII_CLK);
357         if (CSR_READ_4(sc, AX_SIO) & AX_SIO_MII_DATAIN)
358                 return(1);
359
360         return(0);
361 }
362
363 /*
364  * Sync the PHYs by setting data bit and strobing the clock 32 times.
365  */
366 static void ax_mii_sync(sc)
367         struct ax_softc         *sc;
368 {
369         register int            i;
370
371         CSR_WRITE_4(sc, AX_SIO, AX_SIO_ROMCTL_WRITE);
372
373         for (i = 0; i < 32; i++)
374                 ax_mii_writebit(sc, 1);
375
376         return;
377 }
378
379 /*
380  * Clock a series of bits through the MII.
381  */
382 static void ax_mii_send(sc, bits, cnt)
383         struct ax_softc         *sc;
384         u_int32_t               bits;
385         int                     cnt;
386 {
387         int                     i;
388
389         for (i = (0x1 << (cnt - 1)); i; i >>= 1)
390                 ax_mii_writebit(sc, bits & i);
391 }
392
393 /*
394  * Read an PHY register through the MII.
395  */
396 static int ax_mii_readreg(sc, frame)
397         struct ax_softc         *sc;
398         struct ax_mii_frame     *frame;
399         
400 {
401         int                     i, ack, s;
402
403         s = splimp();
404
405         /*
406          * Set up frame for RX.
407          */
408         frame->mii_stdelim = AX_MII_STARTDELIM;
409         frame->mii_opcode = AX_MII_READOP;
410         frame->mii_turnaround = 0;
411         frame->mii_data = 0;
412         
413         /*
414          * Sync the PHYs.
415          */
416         ax_mii_sync(sc);
417
418         /*
419          * Send command/address info.
420          */
421         ax_mii_send(sc, frame->mii_stdelim, 2);
422         ax_mii_send(sc, frame->mii_opcode, 2);
423         ax_mii_send(sc, frame->mii_phyaddr, 5);
424         ax_mii_send(sc, frame->mii_regaddr, 5);
425
426 #ifdef notdef
427         /* Idle bit */
428         ax_mii_writebit(sc, 1);
429         ax_mii_writebit(sc, 0);
430 #endif
431
432         /* Check for ack */
433         ack = ax_mii_readbit(sc);
434
435         /*
436          * Now try reading data bits. If the ack failed, we still
437          * need to clock through 16 cycles to keep the PHY(s) in sync.
438          */
439         if (ack) {
440                 for(i = 0; i < 16; i++) {
441                         ax_mii_readbit(sc);
442                 }
443                 goto fail;
444         }
445
446         for (i = 0x8000; i; i >>= 1) {
447                 if (!ack) {
448                         if (ax_mii_readbit(sc))
449                                 frame->mii_data |= i;
450                 }
451         }
452
453 fail:
454
455         ax_mii_writebit(sc, 0);
456         ax_mii_writebit(sc, 0);
457
458         splx(s);
459
460         if (ack)
461                 return(1);
462         return(0);
463 }
464
465 /*
466  * Write to a PHY register through the MII.
467  */
468 static int ax_mii_writereg(sc, frame)
469         struct ax_softc         *sc;
470         struct ax_mii_frame     *frame;
471         
472 {
473         int                     s;
474
475         s = splimp();
476         /*
477          * Set up frame for TX.
478          */
479
480         frame->mii_stdelim = AX_MII_STARTDELIM;
481         frame->mii_opcode = AX_MII_WRITEOP;
482         frame->mii_turnaround = AX_MII_TURNAROUND;
483
484         /*
485          * Sync the PHYs.
486          */     
487         ax_mii_sync(sc);
488
489         ax_mii_send(sc, frame->mii_stdelim, 2);
490         ax_mii_send(sc, frame->mii_opcode, 2);
491         ax_mii_send(sc, frame->mii_phyaddr, 5);
492         ax_mii_send(sc, frame->mii_regaddr, 5);
493         ax_mii_send(sc, frame->mii_turnaround, 2);
494         ax_mii_send(sc, frame->mii_data, 16);
495
496         /* Idle bit. */
497         ax_mii_writebit(sc, 0);
498         ax_mii_writebit(sc, 0);
499
500         splx(s);
501
502         return(0);
503 }
504
505 static u_int16_t ax_phy_readreg(sc, reg)
506         struct ax_softc         *sc;
507         int                     reg;
508 {
509         struct ax_mii_frame     frame;
510
511         bzero((char *)&frame, sizeof(frame));
512
513         frame.mii_phyaddr = sc->ax_phy_addr;
514         frame.mii_regaddr = reg;
515         ax_mii_readreg(sc, &frame);
516
517         return(frame.mii_data);
518 }
519
520 static void ax_phy_writereg(sc, reg, data)
521         struct ax_softc         *sc;
522         int                     reg;
523         int                     data;
524 {
525         struct ax_mii_frame     frame;
526
527         bzero((char *)&frame, sizeof(frame));
528
529         frame.mii_phyaddr = sc->ax_phy_addr;
530         frame.mii_regaddr = reg;
531         frame.mii_data = data;
532
533         ax_mii_writereg(sc, &frame);
534
535         return;
536 }
537
538 /*
539  * Calculate CRC of a multicast group address, return the lower 6 bits.
540  */
541 static u_int32_t ax_calchash(addr)
542         caddr_t                 addr;
543 {
544         u_int32_t               crc, carry;
545         int                     i, j;
546         u_int8_t                c;
547
548         /* Compute CRC for the address value. */
549         crc = 0xFFFFFFFF; /* initial value */
550
551         for (i = 0; i < 6; i++) {
552                 c = *(addr + i);
553                 for (j = 0; j < 8; j++) {
554                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
555                         crc <<= 1;
556                         c >>= 1;
557                         if (carry)
558                                 crc = (crc ^ 0x04c11db6) | carry;
559                 }
560         }
561
562         /* return the filter bit position */
563         return((crc >> 26) & 0x0000003F);
564 }
565
566 static void ax_setmulti(sc)
567         struct ax_softc         *sc;
568 {
569         struct ifnet            *ifp;
570         int                     h = 0;
571         u_int32_t               hashes[2] = { 0, 0 };
572         struct ifmultiaddr      *ifma;
573         u_int32_t               rxfilt;
574
575         ifp = &sc->arpcom.ac_if;
576
577         rxfilt = CSR_READ_4(sc, AX_NETCFG);
578
579         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
580                 rxfilt |= AX_NETCFG_RX_ALLMULTI;
581                 CSR_WRITE_4(sc, AX_NETCFG, rxfilt);
582                 return;
583         } else
584                 rxfilt &= ~AX_NETCFG_RX_ALLMULTI;
585
586         /* first, zot all the existing hash bits */
587         CSR_WRITE_4(sc, AX_FILTIDX, AX_FILTIDX_MAR0);
588         CSR_WRITE_4(sc, AX_FILTDATA, 0);
589         CSR_WRITE_4(sc, AX_FILTIDX, AX_FILTIDX_MAR1);
590         CSR_WRITE_4(sc, AX_FILTDATA, 0);
591
592         /* now program new ones */
593         for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
594                                 ifma = ifma->ifma_link.le_next) {
595                 if (ifma->ifma_addr->sa_family != AF_LINK)
596                         continue;
597                 h = ax_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
598                 if (h < 32)
599                         hashes[0] |= (1 << h);
600                 else
601                         hashes[1] |= (1 << (h - 32));
602         }
603
604         CSR_WRITE_4(sc, AX_FILTIDX, AX_FILTIDX_MAR0);
605         CSR_WRITE_4(sc, AX_FILTDATA, hashes[0]);
606         CSR_WRITE_4(sc, AX_FILTIDX, AX_FILTIDX_MAR1);
607         CSR_WRITE_4(sc, AX_FILTDATA, hashes[1]);
608         CSR_WRITE_4(sc, AX_NETCFG, rxfilt);
609
610         return;
611 }
612
613 /*
614  * Initiate an autonegotiation session.
615  */
616 static void ax_autoneg_xmit(sc)
617         struct ax_softc         *sc;
618 {
619         u_int16_t               phy_sts;
620
621         ax_phy_writereg(sc, PHY_BMCR, PHY_BMCR_RESET);
622         DELAY(500);
623         while(ax_phy_readreg(sc, PHY_BMCR)
624                         & PHY_BMCR_RESET);
625
626         phy_sts = ax_phy_readreg(sc, PHY_BMCR);
627         phy_sts |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
628         ax_phy_writereg(sc, PHY_BMCR, phy_sts);
629
630         return;
631 }
632
633 /*
634  * Invoke autonegotiation on a PHY.
635  */
636 static void ax_autoneg_mii(sc, flag, verbose)
637         struct ax_softc         *sc;
638         int                     flag;
639         int                     verbose;
640 {
641         u_int16_t               phy_sts = 0, media, advert, ability;
642         struct ifnet            *ifp;
643         struct ifmedia          *ifm;
644
645         ifm = &sc->ifmedia;
646         ifp = &sc->arpcom.ac_if;
647
648         ifm->ifm_media = IFM_ETHER | IFM_AUTO;
649
650         /*
651          * The 100baseT4 PHY on the 3c905-T4 has the 'autoneg supported'
652          * bit cleared in the status register, but has the 'autoneg enabled'
653          * bit set in the control register. This is a contradiction, and
654          * I'm not sure how to handle it. If you want to force an attempt
655          * to autoneg for 100baseT4 PHYs, #define FORCE_AUTONEG_TFOUR
656          * and see what happens.
657          */
658 #ifndef FORCE_AUTONEG_TFOUR
659         /*
660          * First, see if autoneg is supported. If not, there's
661          * no point in continuing.
662          */
663         phy_sts = ax_phy_readreg(sc, PHY_BMSR);
664         if (!(phy_sts & PHY_BMSR_CANAUTONEG)) {
665                 if (verbose)
666                         printf("ax%d: autonegotiation not supported\n",
667                                                         sc->ax_unit);
668                 ifm->ifm_media = IFM_ETHER|IFM_10_T|IFM_HDX;    
669                 return;
670         }
671 #endif
672
673         switch (flag) {
674         case AX_FLAG_FORCEDELAY:
675                 /*
676                  * XXX Never use this option anywhere but in the probe
677                  * routine: making the kernel stop dead in its tracks
678                  * for three whole seconds after we've gone multi-user
679                  * is really bad manners.
680                  */
681                 ax_autoneg_xmit(sc);
682                 DELAY(5000000);
683                 break;
684         case AX_FLAG_SCHEDDELAY:
685                 /*
686                  * Wait for the transmitter to go idle before starting
687                  * an autoneg session, otherwise ax_start() may clobber
688                  * our timeout, and we don't want to allow transmission
689                  * during an autoneg session since that can screw it up.
690                  */
691                 if (sc->ax_cdata.ax_tx_head != NULL) {
692                         sc->ax_want_auto = 1;
693                         return;
694                 }
695                 ax_autoneg_xmit(sc);
696                 ifp->if_timer = 5;
697                 sc->ax_autoneg = 1;
698                 sc->ax_want_auto = 0;
699                 return;
700                 break;
701         case AX_FLAG_DELAYTIMEO:
702                 ifp->if_timer = 0;
703                 sc->ax_autoneg = 0;
704                 break;
705         default:
706                 printf("ax%d: invalid autoneg flag: %d\n", sc->ax_unit, flag);
707                 return;
708         }
709
710         if (ax_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_AUTONEGCOMP) {
711                 if (verbose)
712                         printf("ax%d: autoneg complete, ", sc->ax_unit);
713                 phy_sts = ax_phy_readreg(sc, PHY_BMSR);
714         } else {
715                 if (verbose)
716                         printf("ax%d: autoneg not complete, ", sc->ax_unit);
717         }
718
719         media = ax_phy_readreg(sc, PHY_BMCR);
720
721         /* Link is good. Report modes and set duplex mode. */
722         if (ax_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT) {
723                 if (verbose)
724                         printf("link status good ");
725                 advert = ax_phy_readreg(sc, PHY_ANAR);
726                 ability = ax_phy_readreg(sc, PHY_LPAR);
727
728                 if (advert & PHY_ANAR_100BT4 && ability & PHY_ANAR_100BT4) {
729                         ifm->ifm_media = IFM_ETHER|IFM_100_T4;
730                         media |= PHY_BMCR_SPEEDSEL;
731                         media &= ~PHY_BMCR_DUPLEX;
732                         printf("(100baseT4)\n");
733                 } else if (advert & PHY_ANAR_100BTXFULL &&
734                         ability & PHY_ANAR_100BTXFULL) {
735                         ifm->ifm_media = IFM_ETHER|IFM_100_TX|IFM_FDX;
736                         media |= PHY_BMCR_SPEEDSEL;
737                         media |= PHY_BMCR_DUPLEX;
738                         printf("(full-duplex, 100Mbps)\n");
739                 } else if (advert & PHY_ANAR_100BTXHALF &&
740                         ability & PHY_ANAR_100BTXHALF) {
741                         ifm->ifm_media = IFM_ETHER|IFM_100_TX|IFM_HDX;
742                         media |= PHY_BMCR_SPEEDSEL;
743                         media &= ~PHY_BMCR_DUPLEX;
744                         printf("(half-duplex, 100Mbps)\n");
745                 } else if (advert & PHY_ANAR_10BTFULL &&
746                         ability & PHY_ANAR_10BTFULL) {
747                         ifm->ifm_media = IFM_ETHER|IFM_10_T|IFM_FDX;
748                         media &= ~PHY_BMCR_SPEEDSEL;
749                         media |= PHY_BMCR_DUPLEX;
750                         printf("(full-duplex, 10Mbps)\n");
751                 } else if (advert & PHY_ANAR_10BTHALF &&
752                         ability & PHY_ANAR_10BTHALF) {
753                         ifm->ifm_media = IFM_ETHER|IFM_10_T|IFM_HDX;
754                         media &= ~PHY_BMCR_SPEEDSEL;
755                         media &= ~PHY_BMCR_DUPLEX;
756                         printf("(half-duplex, 10Mbps)\n");
757                 }
758
759                 media &= ~PHY_BMCR_AUTONEGENBL;
760
761                 /* Set ASIC's duplex mode to match the PHY. */
762                 ax_setcfg(sc, media);
763                 ax_phy_writereg(sc, PHY_BMCR, media);
764         } else {
765                 if (verbose)
766                         printf("no carrier\n");
767         }
768
769         ax_init(sc);
770
771         if (sc->ax_tx_pend) {
772                 sc->ax_autoneg = 0;
773                 sc->ax_tx_pend = 0;
774                 ax_start(ifp);
775         }
776
777         return;
778 }
779
780 static void ax_getmode_mii(sc)
781         struct ax_softc         *sc;
782 {
783         u_int16_t               bmsr;
784         struct ifnet            *ifp;
785
786         ifp = &sc->arpcom.ac_if;
787
788         bmsr = ax_phy_readreg(sc, PHY_BMSR);
789         if (bootverbose)
790                 printf("ax%d: PHY status word: %x\n", sc->ax_unit, bmsr);
791
792         /* fallback */
793         sc->ifmedia.ifm_media = IFM_ETHER|IFM_10_T|IFM_HDX;
794
795         if (bmsr & PHY_BMSR_10BTHALF) {
796                 if (bootverbose)
797                         printf("ax%d: 10Mbps half-duplex mode supported\n",
798                                                                 sc->ax_unit);
799                 ifmedia_add(&sc->ifmedia,
800                         IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
801                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
802         }
803
804         if (bmsr & PHY_BMSR_10BTFULL) {
805                 if (bootverbose)
806                         printf("ax%d: 10Mbps full-duplex mode supported\n",
807                                                                 sc->ax_unit);
808                 ifmedia_add(&sc->ifmedia,
809                         IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
810                 sc->ifmedia.ifm_media = IFM_ETHER|IFM_10_T|IFM_FDX;
811         }
812
813         if (bmsr & PHY_BMSR_100BTXHALF) {
814                 if (bootverbose)
815                         printf("ax%d: 100Mbps half-duplex mode supported\n",
816                                                                 sc->ax_unit);
817                 ifp->if_baudrate = 100000000;
818                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
819                 ifmedia_add(&sc->ifmedia,
820                         IFM_ETHER|IFM_100_TX|IFM_HDX, 0, NULL);
821                 sc->ifmedia.ifm_media = IFM_ETHER|IFM_100_TX|IFM_HDX;
822         }
823
824         if (bmsr & PHY_BMSR_100BTXFULL) {
825                 if (bootverbose)
826                         printf("ax%d: 100Mbps full-duplex mode supported\n",
827                                                                 sc->ax_unit);
828                 ifp->if_baudrate = 100000000;
829                 ifmedia_add(&sc->ifmedia,
830                         IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
831                 sc->ifmedia.ifm_media = IFM_ETHER|IFM_100_TX|IFM_FDX;
832         }
833
834         /* Some also support 100BaseT4. */
835         if (bmsr & PHY_BMSR_100BT4) {
836                 if (bootverbose)
837                         printf("ax%d: 100baseT4 mode supported\n", sc->ax_unit);
838                 ifp->if_baudrate = 100000000;
839                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_T4, 0, NULL);
840                 sc->ifmedia.ifm_media = IFM_ETHER|IFM_100_T4;
841 #ifdef FORCE_AUTONEG_TFOUR
842                 if (bootverbose)
843                         printf("ax%d: forcing on autoneg support for BT4\n",
844                                                          sc->ax_unit);
845                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0 NULL):
846                 sc->ifmedia.ifm_media = IFM_ETHER|IFM_AUTO;
847 #endif
848         }
849
850         if (bmsr & PHY_BMSR_CANAUTONEG) {
851                 if (bootverbose)
852                         printf("ax%d: autoneg supported\n", sc->ax_unit);
853                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
854                 sc->ifmedia.ifm_media = IFM_ETHER|IFM_AUTO;
855         }
856
857         return;
858 }
859
860 /*
861  * Set speed and duplex mode.
862  */
863 static void ax_setmode_mii(sc, media)
864         struct ax_softc         *sc;
865         int                     media;
866 {
867         u_int16_t               bmcr;
868         struct ifnet            *ifp;
869
870         ifp = &sc->arpcom.ac_if;
871
872         /*
873          * If an autoneg session is in progress, stop it.
874          */
875         if (sc->ax_autoneg) {
876                 printf("ax%d: canceling autoneg session\n", sc->ax_unit);
877                 ifp->if_timer = sc->ax_autoneg = sc->ax_want_auto = 0;
878                 bmcr = ax_phy_readreg(sc, PHY_BMCR);
879                 bmcr &= ~PHY_BMCR_AUTONEGENBL;
880                 ax_phy_writereg(sc, PHY_BMCR, bmcr);
881         }
882
883         printf("ax%d: selecting MII, ", sc->ax_unit);
884
885         bmcr = ax_phy_readreg(sc, PHY_BMCR);
886
887         bmcr &= ~(PHY_BMCR_AUTONEGENBL|PHY_BMCR_SPEEDSEL|
888                         PHY_BMCR_DUPLEX|PHY_BMCR_LOOPBK);
889
890         if (IFM_SUBTYPE(media) == IFM_100_T4) {
891                 printf("100Mbps/T4, half-duplex\n");
892                 bmcr |= PHY_BMCR_SPEEDSEL;
893                 bmcr &= ~PHY_BMCR_DUPLEX;
894         }
895
896         if (IFM_SUBTYPE(media) == IFM_100_TX) {
897                 printf("100Mbps, ");
898                 bmcr |= PHY_BMCR_SPEEDSEL;
899         }
900
901         if (IFM_SUBTYPE(media) == IFM_10_T) {
902                 printf("10Mbps, ");
903                 bmcr &= ~PHY_BMCR_SPEEDSEL;
904         }
905
906         if ((media & IFM_GMASK) == IFM_FDX) {
907                 printf("full duplex\n");
908                 bmcr |= PHY_BMCR_DUPLEX;
909         } else {
910                 printf("half duplex\n");
911                 bmcr &= ~PHY_BMCR_DUPLEX;
912         }
913
914         ax_setcfg(sc, bmcr);
915         ax_phy_writereg(sc, PHY_BMCR, bmcr);
916
917         return;
918 }
919
920 /*
921  * Set speed and duplex mode on internal transceiver.
922  */
923 static void ax_setmode(sc, media, verbose)
924         struct ax_softc         *sc;
925         int                     media;
926         int                     verbose;
927 {
928         struct ifnet            *ifp;
929         u_int32_t               mode;
930
931         ifp = &sc->arpcom.ac_if;
932
933         if (verbose)
934                 printf("ax%d: selecting internal xcvr, ", sc->ax_unit);
935
936         mode = CSR_READ_4(sc, AX_NETCFG);
937
938         mode &= ~(AX_NETCFG_FULLDUPLEX|AX_NETCFG_PORTSEL|
939                 AX_NETCFG_PCS|AX_NETCFG_SCRAMBLER|AX_NETCFG_SPEEDSEL);
940
941         if (IFM_SUBTYPE(media) == IFM_100_T4) {
942                 if (verbose)
943                         printf("100Mbps/T4, half-duplex\n");
944                 mode |= AX_NETCFG_PORTSEL|AX_NETCFG_PCS|AX_NETCFG_SCRAMBLER;
945         }
946
947         if (IFM_SUBTYPE(media) == IFM_100_TX) {
948                 if (verbose)
949                         printf("100Mbps, ");
950                 mode |= AX_NETCFG_PORTSEL|AX_NETCFG_PCS|AX_NETCFG_SCRAMBLER;
951         }
952
953         if (IFM_SUBTYPE(media) == IFM_10_T) {
954                 if (verbose)
955                         printf("10Mbps, ");
956                 mode &= ~AX_NETCFG_PORTSEL;
957                 mode |= AX_NETCFG_SPEEDSEL;
958         }
959
960         if ((media & IFM_GMASK) == IFM_FDX) {
961                 if (verbose)
962                         printf("full duplex\n");
963                 mode |= AX_NETCFG_FULLDUPLEX;
964         } else {
965                 if (verbose)
966                         printf("half duplex\n");
967                 mode &= ~AX_NETCFG_FULLDUPLEX;
968         }
969
970         CSR_WRITE_4(sc, AX_NETCFG, mode);
971
972         return;
973 }
974
975 /*
976  * In order to fiddle with the
977  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
978  * first have to put the transmit and/or receive logic in the idle state.
979  */
980 static void ax_setcfg(sc, bmcr)
981         struct ax_softc         *sc;
982         int                     bmcr;
983 {
984         int                     i, restart = 0;
985
986         if (CSR_READ_4(sc, AX_NETCFG) & (AX_NETCFG_TX_ON|AX_NETCFG_RX_ON)) {
987                 restart = 1;
988                 AX_CLRBIT(sc, AX_NETCFG, (AX_NETCFG_TX_ON|AX_NETCFG_RX_ON));
989
990                 for (i = 0; i < AX_TIMEOUT; i++) {
991                         DELAY(10);
992                         if (CSR_READ_4(sc, AX_ISR) & AX_ISR_TX_IDLE)
993                                 break;
994                 }
995
996                 if (i == AX_TIMEOUT)
997                         printf("ax%d: failed to force tx and "
998                                 "rx to idle state\n", sc->ax_unit);
999
1000         }
1001
1002         if (bmcr & PHY_BMCR_SPEEDSEL)
1003                 AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_SPEEDSEL);
1004         else
1005                 AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_SPEEDSEL);
1006
1007         if (bmcr & PHY_BMCR_DUPLEX)
1008                 AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_FULLDUPLEX);
1009         else
1010                 AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_FULLDUPLEX);
1011
1012         if (restart)
1013                 AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_TX_ON|AX_NETCFG_RX_ON);
1014
1015         return;
1016 }
1017
1018 static void ax_reset(sc)
1019         struct ax_softc         *sc;
1020 {
1021         register int            i;
1022
1023         AX_SETBIT(sc, AX_BUSCTL, AX_BUSCTL_RESET);
1024
1025         for (i = 0; i < AX_TIMEOUT; i++) {
1026                 DELAY(10);
1027                 if (!(CSR_READ_4(sc, AX_BUSCTL) & AX_BUSCTL_RESET))
1028                         break;
1029         }
1030 #ifdef notdef
1031         if (i == AX_TIMEOUT)
1032                 printf("ax%d: reset never completed!\n", sc->ax_unit);
1033 #endif
1034         CSR_WRITE_4(sc, AX_BUSCTL, AX_BUSCTL_CONFIG);
1035
1036         /* Wait a little while for the chip to get its brains in order. */
1037         DELAY(1000);
1038         return;
1039 }
1040
1041 /*
1042  * Probe for an ASIX chip. Check the PCI vendor and device
1043  * IDs against our list and return a device name if we find a match.
1044  */
1045 static const char *
1046 ax_probe(config_id, device_id)
1047         pcici_t                 config_id;
1048         pcidi_t                 device_id;
1049 {
1050         struct ax_type          *t;
1051
1052         t = ax_devs;
1053
1054         while(t->ax_name != NULL) {
1055                 if ((device_id & 0xFFFF) == t->ax_vid &&
1056                     ((device_id >> 16) & 0xFFFF) == t->ax_did) {
1057                         return(t->ax_name);
1058                 }
1059                 t++;
1060         }
1061
1062         return(NULL);
1063 }
1064
1065 /*
1066  * Attach the interface. Allocate softc structures, do ifmedia
1067  * setup and ethernet/BPF attach.
1068  */
1069 static void
1070 ax_attach(config_id, unit)
1071         pcici_t                 config_id;
1072         int                     unit;
1073 {
1074         int                     s, i;
1075 #ifndef AX_USEIOSPACE
1076         vm_offset_t             pbase, vbase;
1077 #endif
1078         u_char                  eaddr[ETHER_ADDR_LEN];
1079         u_int32_t               command;
1080         struct ax_softc         *sc;
1081         struct ifnet            *ifp;
1082         int                     media = IFM_ETHER|IFM_100_TX|IFM_FDX;
1083         unsigned int            round;
1084         caddr_t                 roundptr;
1085         struct ax_type          *p;
1086         u_int16_t               phy_vid, phy_did, phy_sts;
1087
1088         s = splimp();
1089
1090         sc = malloc(sizeof(struct ax_softc), M_DEVBUF, M_NOWAIT);
1091         if (sc == NULL) {
1092                 printf("ax%d: no memory for softc struct!\n", unit);
1093                 goto fail;
1094         }
1095         bzero(sc, sizeof(struct ax_softc));
1096
1097         /*
1098          * Handle power management nonsense.
1099          */
1100
1101         command = pci_conf_read(config_id, AX_PCI_CAPID) & 0x000000FF;
1102         if (command == 0x01) {
1103
1104                 command = pci_conf_read(config_id, AX_PCI_PWRMGMTCTRL);
1105                 if (command & AX_PSTATE_MASK) {
1106                         u_int32_t               iobase, membase, irq;
1107
1108                         /* Save important PCI config data. */
1109                         iobase = pci_conf_read(config_id, AX_PCI_LOIO);
1110                         membase = pci_conf_read(config_id, AX_PCI_LOMEM);
1111                         irq = pci_conf_read(config_id, AX_PCI_INTLINE);
1112
1113                         /* Reset the power state. */
1114                         printf("ax%d: chip is in D%d power mode "
1115                         "-- setting to D0\n", unit, command & AX_PSTATE_MASK);
1116                         command &= 0xFFFFFFFC;
1117                         pci_conf_write(config_id, AX_PCI_PWRMGMTCTRL, command);
1118
1119                         /* Restore PCI config data. */
1120                         pci_conf_write(config_id, AX_PCI_LOIO, iobase);
1121                         pci_conf_write(config_id, AX_PCI_LOMEM, membase);
1122                         pci_conf_write(config_id, AX_PCI_INTLINE, irq);
1123                 }
1124         }
1125
1126         /*
1127          * Map control/status registers.
1128          */
1129         command = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG);
1130         command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
1131         pci_conf_write(config_id, PCI_COMMAND_STATUS_REG, command);
1132         command = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG);
1133
1134 #ifdef AX_USEIOSPACE
1135         if (!(command & PCIM_CMD_PORTEN)) {
1136                 printf("ax%d: failed to enable I/O ports!\n", unit);
1137                 free(sc, M_DEVBUF);
1138                 goto fail;
1139         }
1140
1141         if (!pci_map_port(config_id, AX_PCI_LOIO,
1142                                         (u_short *)&(sc->ax_bhandle))) {
1143                 printf ("ax%d: couldn't map ports\n", unit);
1144                 goto fail;
1145         }
1146         sc->ax_btag = I386_BUS_SPACE_IO;
1147 #else
1148         if (!(command & PCIM_CMD_MEMEN)) {
1149                 printf("ax%d: failed to enable memory mapping!\n", unit);
1150                 goto fail;
1151         }
1152
1153         if (!pci_map_mem(config_id, AX_PCI_LOMEM, &vbase, &pbase)) {
1154                 printf ("ax%d: couldn't map memory\n", unit);
1155                 goto fail;
1156         }
1157         sc->ax_btag = I386_BUS_SPACE_MEM;
1158         sc->ax_bhandle = vbase;
1159 #endif
1160
1161         /* Allocate interrupt */
1162         if (!pci_map_int(config_id, ax_intr, sc, &net_imask)) {
1163                 printf("ax%d: couldn't map interrupt\n", unit);
1164                 goto fail;
1165         }
1166
1167         /* Reset the adapter. */
1168         ax_reset(sc);
1169
1170         /*
1171          * Get station address from the EEPROM.
1172          */
1173         ax_read_eeprom(sc, (caddr_t)&eaddr, AX_EE_NODEADDR, 3, 0);
1174
1175         /*
1176          * An ASIX chip was detected. Inform the world.
1177          */
1178         printf("ax%d: Ethernet address: %6D\n", unit, eaddr, ":");
1179
1180         sc->ax_unit = unit;
1181         bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
1182
1183         sc->ax_ldata_ptr = malloc(sizeof(struct ax_list_data) + 8,
1184                                 M_DEVBUF, M_NOWAIT);
1185         if (sc->ax_ldata_ptr == NULL) {
1186                 free(sc, M_DEVBUF);
1187                 printf("ax%d: no memory for list buffers!\n", unit);
1188                 goto fail;
1189         }
1190
1191         sc->ax_ldata = (struct ax_list_data *)sc->ax_ldata_ptr;
1192         round = (unsigned int)sc->ax_ldata_ptr & 0xF;
1193         roundptr = sc->ax_ldata_ptr;
1194         for (i = 0; i < 8; i++) {
1195                 if (round % 8) {
1196                         round++;
1197                         roundptr++;
1198                 } else
1199                         break;
1200         }
1201         sc->ax_ldata = (struct ax_list_data *)roundptr;
1202         bzero(sc->ax_ldata, sizeof(struct ax_list_data));
1203
1204         ifp = &sc->arpcom.ac_if;
1205         ifp->if_softc = sc;
1206         ifp->if_unit = unit;
1207         ifp->if_name = "ax";
1208         ifp->if_mtu = ETHERMTU;
1209         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1210         ifp->if_ioctl = ax_ioctl;
1211         ifp->if_output = ether_output;
1212         ifp->if_start = ax_start;
1213         ifp->if_watchdog = ax_watchdog;
1214         ifp->if_init = ax_init;
1215         ifp->if_baudrate = 10000000;
1216
1217
1218         if (bootverbose)
1219                 printf("ax%d: probing for a PHY\n", sc->ax_unit);
1220         for (i = AX_PHYADDR_MIN; i < AX_PHYADDR_MAX + 1; i++) {
1221                 if (bootverbose)
1222                         printf("ax%d: checking address: %d\n",
1223                                                 sc->ax_unit, i);
1224                 sc->ax_phy_addr = i;
1225                 ax_phy_writereg(sc, PHY_BMCR, PHY_BMCR_RESET);
1226                 DELAY(500);
1227                 while(ax_phy_readreg(sc, PHY_BMCR)
1228                                 & PHY_BMCR_RESET);
1229                 if ((phy_sts = ax_phy_readreg(sc, PHY_BMSR)))
1230                         break;
1231         }
1232         if (phy_sts) {
1233                 phy_vid = ax_phy_readreg(sc, PHY_VENID);
1234                 phy_did = ax_phy_readreg(sc, PHY_DEVID);
1235                 if (bootverbose)
1236                         printf("ax%d: found PHY at address %d, ",
1237                                 sc->ax_unit, sc->ax_phy_addr);
1238                 if (bootverbose)
1239                         printf("vendor id: %x device id: %x\n",
1240                         phy_vid, phy_did);
1241                 p = ax_phys;
1242                 while(p->ax_vid) {
1243                         if (phy_vid == p->ax_vid &&
1244                                 (phy_did | 0x000F) == p->ax_did) {
1245                                 sc->ax_pinfo = p;
1246                                 break;
1247                         }
1248                         p++;
1249                 }
1250                 if (sc->ax_pinfo == NULL)
1251                         sc->ax_pinfo = &ax_phys[PHY_UNKNOWN];
1252                 if (bootverbose)
1253                         printf("ax%d: PHY type: %s\n",
1254                                 sc->ax_unit, sc->ax_pinfo->ax_name);
1255         } else {
1256 #ifdef DIAGNOSTIC
1257                 printf("ax%d: MII without any phy!\n", sc->ax_unit);
1258 #endif
1259         }
1260
1261         /*
1262          * Do ifmedia setup.
1263          */
1264         ifmedia_init(&sc->ifmedia, 0, ax_ifmedia_upd, ax_ifmedia_sts);
1265
1266         if (sc->ax_pinfo != NULL) {
1267                 ax_getmode_mii(sc);
1268                 ax_autoneg_mii(sc, AX_FLAG_FORCEDELAY, 1);
1269         } else {
1270                 ifmedia_add(&sc->ifmedia,
1271                         IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
1272                 ifmedia_add(&sc->ifmedia,
1273                         IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
1274                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
1275                 ifmedia_add(&sc->ifmedia,
1276                         IFM_ETHER|IFM_100_TX|IFM_HDX, 0, NULL);
1277                 ifmedia_add(&sc->ifmedia,
1278                         IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
1279                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
1280                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
1281         }
1282
1283         media = sc->ifmedia.ifm_media;
1284         ax_stop(sc);
1285
1286         ifmedia_set(&sc->ifmedia, media);
1287
1288         /*
1289          * Call MI attach routines.
1290          */
1291         if_attach(ifp);
1292         ether_ifattach(ifp);
1293
1294 #if NBPFILTER > 0
1295         bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1296 #endif
1297         at_shutdown(ax_shutdown, sc, SHUTDOWN_POST_SYNC);
1298
1299 fail:
1300         splx(s);
1301         return;
1302 }
1303
1304 /*
1305  * Initialize the transmit descriptors.
1306  */
1307 static int ax_list_tx_init(sc)
1308         struct ax_softc         *sc;
1309 {
1310         struct ax_chain_data    *cd;
1311         struct ax_list_data     *ld;
1312         int                     i;
1313
1314         cd = &sc->ax_cdata;
1315         ld = sc->ax_ldata;
1316         for (i = 0; i < AX_TX_LIST_CNT; i++) {
1317                 cd->ax_tx_chain[i].ax_ptr = &ld->ax_tx_list[i];
1318                 if (i == (AX_TX_LIST_CNT - 1))
1319                         cd->ax_tx_chain[i].ax_nextdesc =
1320                                 &cd->ax_tx_chain[0];
1321                 else
1322                         cd->ax_tx_chain[i].ax_nextdesc =
1323                                 &cd->ax_tx_chain[i + 1];
1324         }
1325
1326         cd->ax_tx_free = &cd->ax_tx_chain[0];
1327         cd->ax_tx_tail = cd->ax_tx_head = NULL;
1328
1329         return(0);
1330 }
1331
1332
1333 /*
1334  * Initialize the RX descriptors and allocate mbufs for them. Note that
1335  * we arrange the descriptors in a closed ring, so that the last descriptor
1336  * points back to the first.
1337  */
1338 static int ax_list_rx_init(sc)
1339         struct ax_softc         *sc;
1340 {
1341         struct ax_chain_data    *cd;
1342         struct ax_list_data     *ld;
1343         int                     i;
1344
1345         cd = &sc->ax_cdata;
1346         ld = sc->ax_ldata;
1347
1348         for (i = 0; i < AX_RX_LIST_CNT; i++) {
1349                 cd->ax_rx_chain[i].ax_ptr =
1350                         (struct ax_desc *)&ld->ax_rx_list[i];
1351                 if (ax_newbuf(sc, &cd->ax_rx_chain[i]) == ENOBUFS)
1352                         return(ENOBUFS);
1353                 if (i == (AX_RX_LIST_CNT - 1)) {
1354                         cd->ax_rx_chain[i].ax_nextdesc =
1355                                                 &cd->ax_rx_chain[0];
1356                         ld->ax_rx_list[i].ax_next =
1357                                         vtophys(&ld->ax_rx_list[0]);
1358                 } else {
1359                         cd->ax_rx_chain[i].ax_nextdesc =
1360                                                 &cd->ax_rx_chain[i + 1];
1361                         ld->ax_rx_list[i].ax_next =
1362                                         vtophys(&ld->ax_rx_list[i + 1]);
1363                 }
1364         }
1365
1366         cd->ax_rx_head = &cd->ax_rx_chain[0];
1367
1368         return(0);
1369 }
1370
1371 /*
1372  * Initialize an RX descriptor and attach an MBUF cluster.
1373  * Note: the length fields are only 11 bits wide, which means the
1374  * largest size we can specify is 2047. This is important because
1375  * MCLBYTES is 2048, so we have to subtract one otherwise we'll
1376  * overflow the field and make a mess.
1377  */
1378 static int ax_newbuf(sc, c)
1379         struct ax_softc         *sc;
1380         struct ax_chain_onefrag *c;
1381 {
1382         struct mbuf             *m_new = NULL;
1383
1384         MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1385         if (m_new == NULL) {
1386                 printf("ax%d: no memory for rx list -- packet dropped!\n",
1387                                                                 sc->ax_unit);
1388                 return(ENOBUFS);
1389         }
1390
1391         MCLGET(m_new, M_DONTWAIT);
1392         if (!(m_new->m_flags & M_EXT)) {
1393                 printf("ax%d: no memory for rx list -- packet dropped!\n",
1394                                                                 sc->ax_unit);
1395                 m_freem(m_new);
1396                 return(ENOBUFS);
1397         }
1398
1399         c->ax_mbuf = m_new;
1400         c->ax_ptr->ax_status = AX_RXSTAT;
1401         c->ax_ptr->ax_data = vtophys(mtod(m_new, caddr_t));
1402         c->ax_ptr->ax_ctl = MCLBYTES - 1;
1403
1404         return(0);
1405 }
1406
1407 /*
1408  * A frame has been uploaded: pass the resulting mbuf chain up to
1409  * the higher level protocols.
1410  */
1411 static void ax_rxeof(sc)
1412         struct ax_softc         *sc;
1413 {
1414         struct ether_header     *eh;
1415         struct mbuf             *m;
1416         struct ifnet            *ifp;
1417         struct ax_chain_onefrag *cur_rx;
1418         int                     total_len = 0;
1419         u_int32_t               rxstat;
1420
1421         ifp = &sc->arpcom.ac_if;
1422
1423         while(!((rxstat = sc->ax_cdata.ax_rx_head->ax_ptr->ax_status) &
1424                                                         AX_RXSTAT_OWN)) {
1425                 cur_rx = sc->ax_cdata.ax_rx_head;
1426                 sc->ax_cdata.ax_rx_head = cur_rx->ax_nextdesc;
1427
1428                 /*
1429                  * If an error occurs, update stats, clear the
1430                  * status word and leave the mbuf cluster in place:
1431                  * it should simply get re-used next time this descriptor
1432                  * comes up in the ring.
1433                  */
1434                 if (rxstat & AX_RXSTAT_RXERR) {
1435                         ifp->if_ierrors++;
1436                         if (rxstat & AX_RXSTAT_COLLSEEN)
1437                                 ifp->if_collisions++;
1438                         cur_rx->ax_ptr->ax_status = AX_RXSTAT;
1439                         cur_rx->ax_ptr->ax_ctl = (MCLBYTES - 1);
1440                         continue;
1441                 }
1442
1443                 /* No errors; receive the packet. */    
1444                 m = cur_rx->ax_mbuf;
1445                 total_len = AX_RXBYTES(cur_rx->ax_ptr->ax_status);
1446
1447                 total_len -= ETHER_CRC_LEN;
1448
1449                 if (total_len < MINCLSIZE) {
1450                         m = m_devget(mtod(cur_rx->ax_mbuf, char *),
1451                                 total_len, 0, ifp, NULL);
1452                         cur_rx->ax_ptr->ax_status = AX_RXSTAT;
1453                         cur_rx->ax_ptr->ax_ctl = (MCLBYTES - 1);
1454                         if (m == NULL) {
1455                                 ifp->if_ierrors++;
1456                                 continue;
1457                         }
1458                 } else {
1459                         m = cur_rx->ax_mbuf;
1460                 /*
1461                  * Try to conjure up a new mbuf cluster. If that
1462                  * fails, it means we have an out of memory condition and
1463                  * should leave the buffer in place and continue. This will
1464                  * result in a lost packet, but there's little else we
1465                  * can do in this situation.
1466                  */
1467                         if (ax_newbuf(sc, cur_rx) == ENOBUFS) {
1468                                 ifp->if_ierrors++;
1469                                 cur_rx->ax_ptr->ax_status = AX_RXSTAT;
1470                                 cur_rx->ax_ptr->ax_ctl = (MCLBYTES - 1);
1471                                 continue;
1472                         }
1473                         m->m_pkthdr.rcvif = ifp;
1474                         m->m_pkthdr.len = m->m_len = total_len;
1475                 }
1476
1477                 ifp->if_ipackets++;
1478                 eh = mtod(m, struct ether_header *);
1479 #if NBPFILTER > 0
1480                 /*
1481                  * Handle BPF listeners. Let the BPF user see the packet, but
1482                  * don't pass it up to the ether_input() layer unless it's
1483                  * a broadcast packet, multicast packet, matches our ethernet
1484                  * address or the interface is in promiscuous mode.
1485                  */
1486                 if (ifp->if_bpf) {
1487                         bpf_mtap(ifp, m);
1488                         if (ifp->if_flags & IFF_PROMISC &&
1489                                 (bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1490                                                 ETHER_ADDR_LEN) &&
1491                                         (eh->ether_dhost[0] & 1) == 0)) {
1492                                 m_freem(m);
1493                                 continue;
1494                         }
1495                 }
1496 #endif
1497                 /* Remove header from mbuf and pass it on. */
1498                 m_adj(m, sizeof(struct ether_header));
1499                 ether_input(ifp, eh, m);
1500         }
1501
1502         return;
1503 }
1504
1505 void ax_rxeoc(sc)
1506         struct ax_softc         *sc;
1507 {
1508
1509         ax_rxeof(sc);
1510         AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_RX_ON);
1511         CSR_WRITE_4(sc, AX_RXADDR, vtophys(sc->ax_cdata.ax_rx_head->ax_ptr));
1512         AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_RX_ON);
1513         CSR_WRITE_4(sc, AX_RXSTART, 0xFFFFFFFF);
1514
1515         return;
1516 }
1517
1518 /*
1519  * A frame was downloaded to the chip. It's safe for us to clean up
1520  * the list buffers.
1521  */
1522
1523 static void ax_txeof(sc)
1524         struct ax_softc         *sc;
1525 {
1526         struct ax_chain         *cur_tx;
1527         struct ifnet            *ifp;
1528
1529         ifp = &sc->arpcom.ac_if;
1530
1531         /* Clear the timeout timer. */
1532         ifp->if_timer = 0;
1533
1534         if (sc->ax_cdata.ax_tx_head == NULL)
1535                 return;
1536
1537         /*
1538          * Go through our tx list and free mbufs for those
1539          * frames that have been transmitted.
1540          */
1541         while(sc->ax_cdata.ax_tx_head->ax_mbuf != NULL) {
1542                 u_int32_t               txstat;
1543
1544                 cur_tx = sc->ax_cdata.ax_tx_head;
1545                 txstat = AX_TXSTATUS(cur_tx);
1546
1547                 if (txstat & AX_TXSTAT_OWN)
1548                         break;
1549
1550                 if (txstat & AX_TXSTAT_ERRSUM) {
1551                         ifp->if_oerrors++;
1552                         if (txstat & AX_TXSTAT_EXCESSCOLL)
1553                                 ifp->if_collisions++;
1554                         if (txstat & AX_TXSTAT_LATECOLL)
1555                                 ifp->if_collisions++;
1556                 }
1557
1558                 ifp->if_collisions += (txstat & AX_TXSTAT_COLLCNT) >> 3;
1559
1560                 ifp->if_opackets++;
1561                 m_freem(cur_tx->ax_mbuf);
1562                 cur_tx->ax_mbuf = NULL;
1563
1564                 if (sc->ax_cdata.ax_tx_head == sc->ax_cdata.ax_tx_tail) {
1565                         sc->ax_cdata.ax_tx_head = NULL;
1566                         sc->ax_cdata.ax_tx_tail = NULL;
1567                         break;
1568                 }
1569
1570                 sc->ax_cdata.ax_tx_head = cur_tx->ax_nextdesc;
1571         }
1572
1573         return;
1574 }
1575
1576 /*
1577  * TX 'end of channel' interrupt handler.
1578  */
1579 static void ax_txeoc(sc)
1580         struct ax_softc         *sc;
1581 {
1582         struct ifnet            *ifp;
1583
1584         ifp = &sc->arpcom.ac_if;
1585
1586         ifp->if_timer = 0;
1587
1588         if (sc->ax_cdata.ax_tx_head == NULL) {
1589                 ifp->if_flags &= ~IFF_OACTIVE;
1590                 sc->ax_cdata.ax_tx_tail = NULL;
1591                 if (sc->ax_want_auto)
1592                         ax_autoneg_mii(sc, AX_FLAG_DELAYTIMEO, 1);
1593         }
1594
1595         return;
1596 }
1597
1598 static void ax_intr(arg)
1599         void                    *arg;
1600 {
1601         struct ax_softc         *sc;
1602         struct ifnet            *ifp;
1603         u_int32_t               status;
1604
1605         sc = arg;
1606         ifp = &sc->arpcom.ac_if;
1607
1608         /* Supress unwanted interrupts */
1609         if (!(ifp->if_flags & IFF_UP)) {
1610                 ax_stop(sc);
1611                 return;
1612         }
1613
1614         /* Disable interrupts. */
1615         CSR_WRITE_4(sc, AX_IMR, 0x00000000);
1616
1617         for (;;) {
1618                 status = CSR_READ_4(sc, AX_ISR);
1619                 if (status)
1620                         CSR_WRITE_4(sc, AX_ISR, status);
1621
1622                 if ((status & AX_INTRS) == 0)
1623                         break;
1624
1625                 if ((status & AX_ISR_TX_OK) || (status & AX_ISR_TX_EARLY))
1626                         ax_txeof(sc);
1627
1628                 if (status & AX_ISR_TX_NOBUF)
1629                         ax_txeoc(sc);
1630
1631                 if (status & AX_ISR_TX_IDLE) {
1632                         ax_txeof(sc);
1633                         if (sc->ax_cdata.ax_tx_head != NULL) {
1634                                 AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_TX_ON);
1635                                 CSR_WRITE_4(sc, AX_TXSTART, 0xFFFFFFFF);
1636                         }
1637                 }
1638
1639                 if (status & AX_ISR_TX_UNDERRUN) {
1640                         u_int32_t               cfg;
1641                         cfg = CSR_READ_4(sc, AX_NETCFG);
1642                         if ((cfg & AX_NETCFG_TX_THRESH) == AX_TXTHRESH_160BYTES)
1643                                 AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_STORENFWD);
1644                         else
1645                                 CSR_WRITE_4(sc, AX_NETCFG, cfg + 0x4000);
1646                 }
1647
1648                 if (status & AX_ISR_RX_OK)
1649                         ax_rxeof(sc);
1650
1651                 if ((status & AX_ISR_RX_WATDOGTIMEO)
1652                                         || (status & AX_ISR_RX_NOBUF))
1653                         ax_rxeoc(sc);
1654
1655                 if (status & AX_ISR_BUS_ERR) {
1656                         ax_reset(sc);
1657                         ax_init(sc);
1658                 }
1659         }
1660
1661         /* Re-enable interrupts. */
1662         CSR_WRITE_4(sc, AX_IMR, AX_INTRS);
1663
1664         if (ifp->if_snd.ifq_head != NULL) {
1665                 ax_start(ifp);
1666         }
1667
1668         return;
1669 }
1670
1671 /*
1672  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1673  * pointers to the fragment pointers.
1674  */
1675 static int ax_encap(sc, c, m_head)
1676         struct ax_softc         *sc;
1677         struct ax_chain         *c;
1678         struct mbuf             *m_head;
1679 {
1680         int                     frag = 0;
1681         volatile struct ax_desc *f = NULL;
1682         int                     total_len;
1683         struct mbuf             *m;
1684
1685         /*
1686          * Start packing the mbufs in this chain into
1687          * the fragment pointers. Stop when we run out
1688          * of fragments or hit the end of the mbuf chain.
1689          */
1690         m = m_head;
1691         total_len = 0;
1692
1693         for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
1694                 if (m->m_len != 0) {
1695                         if (frag == AX_MAXFRAGS)
1696                                 break;
1697                         total_len += m->m_len;
1698                         f = &c->ax_ptr->ax_frag[frag];
1699                         f->ax_ctl = m->m_len;
1700                         if (frag == 0) {
1701                                 f->ax_status = 0;
1702                                 f->ax_ctl |= AX_TXCTL_FIRSTFRAG;
1703                         } else
1704                                 f->ax_status = AX_TXSTAT_OWN;
1705                         f->ax_next = vtophys(&c->ax_ptr->ax_frag[frag + 1]);
1706                         f->ax_data = vtophys(mtod(m, vm_offset_t));
1707                         frag++;
1708                 }
1709         }
1710
1711         /*
1712          * Handle special case: we ran out of fragments,
1713          * but we have more mbufs left in the chain. Copy the
1714          * data into an mbuf cluster. Note that we don't
1715          * bother clearing the values in the other fragment
1716          * pointers/counters; it wouldn't gain us anything,
1717          * and would waste cycles.
1718          */
1719         if (m != NULL) {
1720                 struct mbuf             *m_new = NULL;
1721
1722                 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1723                 if (m_new == NULL) {
1724                         printf("ax%d: no memory for tx list", sc->ax_unit);
1725                         return(1);
1726                 }
1727                 if (m_head->m_pkthdr.len > MHLEN) {
1728                         MCLGET(m_new, M_DONTWAIT);
1729                         if (!(m_new->m_flags & M_EXT)) {
1730                                 m_freem(m_new);
1731                                 printf("ax%d: no memory for tx list",
1732                                                 sc->ax_unit);
1733                                 return(1);
1734                         }
1735                 }
1736                 m_copydata(m_head, 0, m_head->m_pkthdr.len,     
1737                                         mtod(m_new, caddr_t));
1738                 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1739                 m_freem(m_head);
1740                 m_head = m_new;
1741                 f = &c->ax_ptr->ax_frag[0];
1742                 f->ax_status = 0;
1743                 f->ax_data = vtophys(mtod(m_new, caddr_t));
1744                 f->ax_ctl = total_len = m_new->m_len;
1745                 f->ax_ctl |= AX_TXCTL_FIRSTFRAG;
1746                 frag = 1;
1747         }
1748
1749         c->ax_mbuf = m_head;
1750         c->ax_lastdesc = frag - 1;
1751         AX_TXCTL(c) |= AX_TXCTL_LASTFRAG|AX_TXCTL_FINT;
1752         c->ax_ptr->ax_frag[0].ax_ctl |= AX_TXCTL_FINT;
1753         AX_TXNEXT(c) = vtophys(&c->ax_nextdesc->ax_ptr->ax_frag[0]);
1754         return(0);
1755 }
1756
1757 /*
1758  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1759  * to the mbuf data regions directly in the transmit lists. We also save a
1760  * copy of the pointers since the transmit list fragment pointers are
1761  * physical addresses.
1762  */
1763
1764 static void ax_start(ifp)
1765         struct ifnet            *ifp;
1766 {
1767         struct ax_softc         *sc;
1768         struct mbuf             *m_head = NULL;
1769         struct ax_chain         *cur_tx = NULL, *start_tx;
1770
1771         sc = ifp->if_softc;
1772
1773         if (sc->ax_autoneg) {
1774                 sc->ax_tx_pend = 1;
1775                 return;
1776         }
1777
1778         /*
1779          * Check for an available queue slot. If there are none,
1780          * punt.
1781          */
1782         if (sc->ax_cdata.ax_tx_free->ax_mbuf != NULL) {
1783                 ifp->if_flags |= IFF_OACTIVE;
1784                 return;
1785         }
1786
1787         start_tx = sc->ax_cdata.ax_tx_free;
1788
1789         while(sc->ax_cdata.ax_tx_free->ax_mbuf == NULL) {
1790                 IF_DEQUEUE(&ifp->if_snd, m_head);
1791                 if (m_head == NULL)
1792                         break;
1793
1794                 /* Pick a descriptor off the free list. */
1795                 cur_tx = sc->ax_cdata.ax_tx_free;
1796                 sc->ax_cdata.ax_tx_free = cur_tx->ax_nextdesc;
1797
1798                 /* Pack the data into the descriptor. */
1799                 ax_encap(sc, cur_tx, m_head);
1800                 if (cur_tx != start_tx)
1801                         AX_TXOWN(cur_tx) = AX_TXSTAT_OWN;
1802
1803 #if NBPFILTER > 0
1804                 /*
1805                  * If there's a BPF listener, bounce a copy of this frame
1806                  * to him.
1807                  */
1808                 if (ifp->if_bpf)
1809                         bpf_mtap(ifp, cur_tx->ax_mbuf);
1810 #endif
1811                 AX_TXOWN(cur_tx) = AX_TXSTAT_OWN;
1812                 CSR_WRITE_4(sc, AX_TXSTART, 0xFFFFFFFF);
1813         }
1814
1815         sc->ax_cdata.ax_tx_tail = cur_tx;
1816         if (sc->ax_cdata.ax_tx_head == NULL)
1817                 sc->ax_cdata.ax_tx_head = start_tx;
1818
1819         /*
1820          * Set a timeout in case the chip goes out to lunch.
1821          */
1822         ifp->if_timer = 5;
1823
1824         return;
1825 }
1826
1827 static void ax_init(xsc)
1828         void                    *xsc;
1829 {
1830         struct ax_softc         *sc = xsc;
1831         struct ifnet            *ifp = &sc->arpcom.ac_if;
1832         u_int16_t               phy_bmcr = 0;
1833         int                     s;
1834
1835         if (sc->ax_autoneg)
1836                 return;
1837
1838         s = splimp();
1839
1840         if (sc->ax_pinfo != NULL)
1841                 phy_bmcr = ax_phy_readreg(sc, PHY_BMCR);
1842
1843         /*
1844          * Cancel pending I/O and free all RX/TX buffers.
1845          */
1846         ax_stop(sc);
1847         ax_reset(sc);
1848
1849         /*
1850          * Set cache alignment and burst length.
1851          */
1852         CSR_WRITE_4(sc, AX_BUSCTL, AX_BUSCTL_CONFIG);
1853
1854         AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_HEARTBEAT);
1855         AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_STORENFWD);
1856
1857         if (sc->ax_pinfo != NULL) {
1858                 AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_PORTSEL);
1859                 ax_setcfg(sc, ax_phy_readreg(sc, PHY_BMCR));
1860         } else
1861                 ax_setmode(sc, sc->ifmedia.ifm_media, 0);
1862
1863         AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_TX_THRESH);
1864         AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_SPEEDSEL);
1865
1866         if (IFM_SUBTYPE(sc->ifmedia.ifm_media) == IFM_10_T)
1867                 AX_SETBIT(sc, AX_NETCFG, AX_TXTHRESH_160BYTES);
1868         else
1869                 AX_SETBIT(sc, AX_NETCFG, AX_TXTHRESH_72BYTES);
1870
1871         /* Init our MAC address */
1872         CSR_WRITE_4(sc, AX_FILTIDX, AX_FILTIDX_PAR0);
1873         CSR_WRITE_4(sc, AX_FILTDATA, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
1874         CSR_WRITE_4(sc, AX_FILTIDX, AX_FILTIDX_PAR1);
1875         CSR_WRITE_4(sc, AX_FILTDATA, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
1876
1877         /* Init circular RX list. */
1878         if (ax_list_rx_init(sc) == ENOBUFS) {
1879                 printf("ax%d: initialization failed: no "
1880                         "memory for rx buffers\n", sc->ax_unit);
1881                 ax_stop(sc);
1882                 (void)splx(s);
1883                 return;
1884         }
1885
1886         /*
1887          * Init tx descriptors.
1888          */
1889         ax_list_tx_init(sc);
1890
1891          /* If we want promiscuous mode, set the allframes bit. */
1892         if (ifp->if_flags & IFF_PROMISC) {
1893                 AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_RX_PROMISC);
1894         } else {
1895                 AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_RX_PROMISC);
1896         }
1897
1898         /*
1899          * Set the capture broadcast bit to capture broadcast frames.
1900          */
1901         if (ifp->if_flags & IFF_BROADCAST) {
1902                 AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_RX_BROAD);
1903         } else {
1904                 AX_CLRBIT(sc, AX_NETCFG, AX_NETCFG_RX_BROAD);
1905         }
1906
1907         /*
1908          * Load the multicast filter.
1909          */
1910         ax_setmulti(sc);
1911
1912         /*
1913          * Load the address of the RX list.
1914          */
1915         CSR_WRITE_4(sc, AX_RXADDR, vtophys(sc->ax_cdata.ax_rx_head->ax_ptr));
1916         CSR_WRITE_4(sc, AX_TXADDR, vtophys(&sc->ax_ldata->ax_tx_list[0]));
1917
1918         /*
1919          * Enable interrupts.
1920          */
1921         CSR_WRITE_4(sc, AX_IMR, AX_INTRS);
1922         CSR_WRITE_4(sc, AX_ISR, 0xFFFFFFFF);
1923
1924         /* Enable receiver and transmitter. */
1925         AX_SETBIT(sc, AX_NETCFG, AX_NETCFG_TX_ON|AX_NETCFG_RX_ON);
1926         CSR_WRITE_4(sc, AX_RXSTART, 0xFFFFFFFF);
1927
1928         /* Restore state of BMCR */
1929         if (sc->ax_pinfo != NULL)
1930                 ax_phy_writereg(sc, PHY_BMCR, phy_bmcr);
1931
1932         ifp->if_flags |= IFF_RUNNING;
1933         ifp->if_flags &= ~IFF_OACTIVE;
1934
1935         (void)splx(s);
1936
1937         return;
1938 }
1939
1940 /*
1941  * Set media options.
1942  */
1943 static int ax_ifmedia_upd(ifp)
1944         struct ifnet            *ifp;
1945 {
1946         struct ax_softc         *sc;
1947         struct ifmedia          *ifm;
1948
1949         sc = ifp->if_softc;
1950         ifm = &sc->ifmedia;
1951
1952         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1953                 return(EINVAL);
1954
1955         if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO)
1956                 ax_autoneg_mii(sc, AX_FLAG_SCHEDDELAY, 1);
1957         else {
1958                 if (sc->ax_pinfo == NULL)
1959                         ax_setmode(sc, ifm->ifm_media, 1);
1960                 else
1961                         ax_setmode_mii(sc, ifm->ifm_media);
1962         }
1963
1964         return(0);
1965 }
1966
1967 /*
1968  * Report current media status.
1969  */
1970 static void ax_ifmedia_sts(ifp, ifmr)
1971         struct ifnet            *ifp;
1972         struct ifmediareq       *ifmr;
1973 {
1974         struct ax_softc         *sc;
1975         u_int16_t               advert = 0, ability = 0;
1976         u_int32_t               media = 0;
1977
1978         sc = ifp->if_softc;
1979
1980         ifmr->ifm_active = IFM_ETHER;
1981
1982         if (sc->ax_pinfo == NULL) {
1983                 media = CSR_READ_4(sc, AX_NETCFG);
1984                 if (media & AX_NETCFG_PORTSEL)
1985                         ifmr->ifm_active = IFM_ETHER|IFM_100_TX;
1986                 else
1987                         ifmr->ifm_active = IFM_ETHER|IFM_10_T;
1988                 if (media & AX_NETCFG_FULLDUPLEX)
1989                         ifmr->ifm_active |= IFM_FDX;
1990                 else
1991                         ifmr->ifm_active |= IFM_HDX;
1992                 return;
1993         }
1994
1995         if (!(ax_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_AUTONEGENBL)) {
1996                 if (ax_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_SPEEDSEL)
1997                         ifmr->ifm_active = IFM_ETHER|IFM_100_TX;
1998                 else
1999                         ifmr->ifm_active = IFM_ETHER|IFM_10_T;
2000                 if (ax_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_DUPLEX)
2001                         ifmr->ifm_active |= IFM_FDX;
2002                 else
2003                         ifmr->ifm_active |= IFM_HDX;
2004                 return;
2005         }
2006
2007         ability = ax_phy_readreg(sc, PHY_LPAR);
2008         advert = ax_phy_readreg(sc, PHY_ANAR);
2009         if (advert & PHY_ANAR_100BT4 &&
2010                 ability & PHY_ANAR_100BT4) {
2011                 ifmr->ifm_active = IFM_ETHER|IFM_100_T4;
2012         } else if (advert & PHY_ANAR_100BTXFULL &&
2013                 ability & PHY_ANAR_100BTXFULL) {
2014                 ifmr->ifm_active = IFM_ETHER|IFM_100_TX|IFM_FDX;
2015         } else if (advert & PHY_ANAR_100BTXHALF &&
2016                 ability & PHY_ANAR_100BTXHALF) {
2017                 ifmr->ifm_active = IFM_ETHER|IFM_100_TX|IFM_HDX;
2018         } else if (advert & PHY_ANAR_10BTFULL &&
2019                 ability & PHY_ANAR_10BTFULL) {
2020                 ifmr->ifm_active = IFM_ETHER|IFM_10_T|IFM_FDX;
2021         } else if (advert & PHY_ANAR_10BTHALF &&
2022                 ability & PHY_ANAR_10BTHALF) {
2023                 ifmr->ifm_active = IFM_ETHER|IFM_10_T|IFM_HDX;
2024         }
2025
2026         return;
2027 }
2028
2029 static int ax_ioctl(ifp, command, data)
2030         struct ifnet            *ifp;
2031         u_long                  command;
2032         caddr_t                 data;
2033 {
2034         struct ax_softc         *sc = ifp->if_softc;
2035         struct ifreq            *ifr = (struct ifreq *) data;
2036         int                     s, error = 0;
2037
2038         s = splimp();
2039
2040         switch(command) {
2041         case SIOCSIFADDR:
2042         case SIOCGIFADDR:
2043         case SIOCSIFMTU:
2044                 error = ether_ioctl(ifp, command, data);
2045                 break;
2046         case SIOCSIFFLAGS:
2047                 if (ifp->if_flags & IFF_UP) {
2048                         ax_init(sc);
2049                 } else {
2050                         if (ifp->if_flags & IFF_RUNNING)
2051                                 ax_stop(sc);
2052                 }
2053                 error = 0;
2054                 break;
2055         case SIOCADDMULTI:
2056         case SIOCDELMULTI:
2057                 ax_setmulti(sc);
2058                 error = 0;
2059                 break;
2060         case SIOCGIFMEDIA:
2061         case SIOCSIFMEDIA:
2062                 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
2063                 break;
2064         default:
2065                 error = EINVAL;
2066                 break;
2067         }
2068
2069         (void)splx(s);
2070
2071         return(error);
2072 }
2073
2074 static void ax_watchdog(ifp)
2075         struct ifnet            *ifp;
2076 {
2077         struct ax_softc         *sc;
2078
2079         sc = ifp->if_softc;
2080
2081         if (sc->ax_autoneg) {
2082                 ax_autoneg_mii(sc, AX_FLAG_DELAYTIMEO, 1);
2083                 return;
2084         }
2085
2086         ifp->if_oerrors++;
2087         printf("ax%d: watchdog timeout\n", sc->ax_unit);
2088
2089         if (sc->ax_pinfo != NULL) {
2090                 if (!(ax_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT))
2091                         printf("ax%d: no carrier - transceiver "
2092                                 "cable problem?\n", sc->ax_unit);
2093         }
2094
2095         ax_stop(sc);
2096         ax_reset(sc);
2097         ax_init(sc);
2098
2099         if (ifp->if_snd.ifq_head != NULL)
2100                 ax_start(ifp);
2101
2102         return;
2103 }
2104
2105 /*
2106  * Stop the adapter and free any mbufs allocated to the
2107  * RX and TX lists.
2108  */
2109 static void ax_stop(sc)
2110         struct ax_softc         *sc;
2111 {
2112         register int            i;
2113         struct ifnet            *ifp;
2114
2115         ifp = &sc->arpcom.ac_if;
2116         ifp->if_timer = 0;
2117
2118         AX_CLRBIT(sc, AX_NETCFG, (AX_NETCFG_RX_ON|AX_NETCFG_TX_ON));
2119         CSR_WRITE_4(sc, AX_IMR, 0x00000000);
2120         CSR_WRITE_4(sc, AX_TXADDR, 0x00000000);
2121         CSR_WRITE_4(sc, AX_RXADDR, 0x00000000);
2122
2123         /*
2124          * Free data in the RX lists.
2125          */
2126         for (i = 0; i < AX_RX_LIST_CNT; i++) {
2127                 if (sc->ax_cdata.ax_rx_chain[i].ax_mbuf != NULL) {
2128                         m_freem(sc->ax_cdata.ax_rx_chain[i].ax_mbuf);
2129                         sc->ax_cdata.ax_rx_chain[i].ax_mbuf = NULL;
2130                 }
2131         }
2132         bzero((char *)&sc->ax_ldata->ax_rx_list,
2133                 sizeof(sc->ax_ldata->ax_rx_list));
2134
2135         /*
2136          * Free the TX list buffers.
2137          */
2138         for (i = 0; i < AX_TX_LIST_CNT; i++) {
2139                 if (sc->ax_cdata.ax_tx_chain[i].ax_mbuf != NULL) {
2140                         m_freem(sc->ax_cdata.ax_tx_chain[i].ax_mbuf);
2141                         sc->ax_cdata.ax_tx_chain[i].ax_mbuf = NULL;
2142                 }
2143         }
2144
2145         bzero((char *)&sc->ax_ldata->ax_tx_list,
2146                 sizeof(sc->ax_ldata->ax_tx_list));
2147
2148         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2149
2150         return;
2151 }
2152
2153 /*
2154  * Stop all chip I/O so that the kernel's probe routines don't
2155  * get confused by errant DMAs when rebooting.
2156  */
2157 static void ax_shutdown(howto, arg)
2158         int                     howto;
2159         void                    *arg;
2160 {
2161         struct ax_softc         *sc = (struct ax_softc *)arg;
2162
2163         ax_stop(sc);
2164
2165         return;
2166 }
2167
2168 static struct pci_device ax_device = {
2169         "ax",
2170         ax_probe,
2171         ax_attach,
2172         &ax_count,
2173         NULL
2174 };
2175 DATA_SET(pcidevice_set, ax_device);