]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/dc/if_dc.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / dc / if_dc.c
1 /*-
2  * Copyright (c) 1997, 1998, 1999
3  *      Bill Paul <wpaul@ee.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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
38  * series chips and several workalikes including the following:
39  *
40  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
41  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
42  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
43  * ASIX Electronics AX88140A (www.asix.com.tw)
44  * ASIX Electronics AX88141 (www.asix.com.tw)
45  * ADMtek AL981 (www.admtek.com.tw)
46  * ADMtek AN983 (www.admtek.com.tw)
47  * ADMtek CardBus AN985 (www.admtek.com.tw)
48  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek CardBus AN985
49  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
50  * Accton EN1217 (www.accton.com)
51  * Xircom X3201 (www.xircom.com)
52  * Abocom FE2500
53  * Conexant LANfinity (www.conexant.com)
54  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
55  *
56  * Datasheets for the 21143 are available at developer.intel.com.
57  * Datasheets for the clone parts can be found at their respective sites.
58  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
59  * The PNIC II is essentially a Macronix 98715A chip; the only difference
60  * worth noting is that its multicast hash table is only 128 bits wide
61  * instead of 512.
62  *
63  * Written by Bill Paul <wpaul@ee.columbia.edu>
64  * Electrical Engineering Department
65  * Columbia University, New York City
66  */
67 /*
68  * The Intel 21143 is the successor to the DEC 21140. It is basically
69  * the same as the 21140 but with a few new features. The 21143 supports
70  * three kinds of media attachments:
71  *
72  * o MII port, for 10Mbps and 100Mbps support and NWAY
73  *   autonegotiation provided by an external PHY.
74  * o SYM port, for symbol mode 100Mbps support.
75  * o 10baseT port.
76  * o AUI/BNC port.
77  *
78  * The 100Mbps SYM port and 10baseT port can be used together in
79  * combination with the internal NWAY support to create a 10/100
80  * autosensing configuration.
81  *
82  * Note that not all tulip workalikes are handled in this driver: we only
83  * deal with those which are relatively well behaved. The Winbond is
84  * handled separately due to its different register offsets and the
85  * special handling needed for its various bugs. The PNIC is handled
86  * here, but I'm not thrilled about it.
87  *
88  * All of the workalike chips use some form of MII transceiver support
89  * with the exception of the Macronix chips, which also have a SYM port.
90  * The ASIX AX88140A is also documented to have a SYM port, but all
91  * the cards I've seen use an MII transceiver, probably because the
92  * AX88140A doesn't support internal NWAY.
93  */
94
95 #ifdef HAVE_KERNEL_OPTION_HEADERS
96 #include "opt_device_polling.h"
97 #endif
98
99 #include <sys/param.h>
100 #include <sys/endian.h>
101 #include <sys/systm.h>
102 #include <sys/sockio.h>
103 #include <sys/mbuf.h>
104 #include <sys/malloc.h>
105 #include <sys/kernel.h>
106 #include <sys/module.h>
107 #include <sys/socket.h>
108
109 #include <net/if.h>
110 #include <net/if_arp.h>
111 #include <net/ethernet.h>
112 #include <net/if_dl.h>
113 #include <net/if_media.h>
114 #include <net/if_types.h>
115 #include <net/if_vlan_var.h>
116
117 #include <net/bpf.h>
118
119 #include <machine/bus.h>
120 #include <machine/resource.h>
121 #include <sys/bus.h>
122 #include <sys/rman.h>
123
124 #include <dev/mii/mii.h>
125 #include <dev/mii/mii_bitbang.h>
126 #include <dev/mii/miivar.h>
127
128 #include <dev/pci/pcireg.h>
129 #include <dev/pci/pcivar.h>
130
131 #define DC_USEIOSPACE
132
133 #include <dev/dc/if_dcreg.h>
134
135 #ifdef __sparc64__
136 #include <dev/ofw/openfirm.h>
137 #include <machine/ofw_machdep.h>
138 #endif
139
140 MODULE_DEPEND(dc, pci, 1, 1, 1);
141 MODULE_DEPEND(dc, ether, 1, 1, 1);
142 MODULE_DEPEND(dc, miibus, 1, 1, 1);
143
144 /*
145  * "device miibus" is required in kernel config.  See GENERIC if you get
146  * errors here.
147  */
148 #include "miibus_if.h"
149
150 /*
151  * Various supported device vendors/types and their names.
152  */
153 static const struct dc_type const dc_devs[] = {
154         { DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143), 0,
155                 "Intel 21143 10/100BaseTX" },
156         { DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009), 0,
157                 "Davicom DM9009 10/100BaseTX" },
158         { DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100), 0,
159                 "Davicom DM9100 10/100BaseTX" },
160         { DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), DC_REVISION_DM9102A,
161                 "Davicom DM9102A 10/100BaseTX" },
162         { DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), 0,
163                 "Davicom DM9102 10/100BaseTX" },
164         { DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981), 0,
165                 "ADMtek AL981 10/100BaseTX" },
166         { DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983), 0,
167                 "ADMtek AN983 10/100BaseTX" },
168         { DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
169                 "ADMtek AN985 CardBus 10/100BaseTX or clone" },
170         { DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
171                 "ADMtek ADM9511 10/100BaseTX" },
172         { DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
173                 "ADMtek ADM9513 10/100BaseTX" },
174         { DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), DC_REVISION_88141,
175                 "ASIX AX88141 10/100BaseTX" },
176         { DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), 0,
177                 "ASIX AX88140A 10/100BaseTX" },
178         { DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), DC_REVISION_98713A,
179                 "Macronix 98713A 10/100BaseTX" },
180         { DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), 0,
181                 "Macronix 98713 10/100BaseTX" },
182         { DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), DC_REVISION_98713A,
183                 "Compex RL100-TX 10/100BaseTX" },
184         { DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), 0,
185                 "Compex RL100-TX 10/100BaseTX" },
186         { DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98725,
187                 "Macronix 98725 10/100BaseTX" },
188         { DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98715AEC_C,
189                 "Macronix 98715AEC-C 10/100BaseTX" },
190         { DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), 0,
191                 "Macronix 98715/98715A 10/100BaseTX" },
192         { DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727), 0,
193                 "Macronix 98727/98732 10/100BaseTX" },
194         { DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115), 0,
195                 "LC82C115 PNIC II 10/100BaseTX" },
196         { DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), DC_REVISION_82C169,
197                 "82c169 PNIC 10/100BaseTX" },
198         { DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), 0,
199                 "82c168 PNIC 10/100BaseTX" },
200         { DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217), 0,
201                 "Accton EN1217 10/100BaseTX" },
202         { DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242), 0,
203                 "Accton EN2242 MiniPCI 10/100BaseTX" },
204         { DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201), 0,
205                 "Xircom X3201 10/100BaseTX" },
206         { DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD), 0,
207                 "Neteasy DRP-32TXD Cardbus 10/100" },
208         { DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500), 0,
209                 "Abocom FE2500 10/100BaseTX" },
210         { DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX), 0,
211                 "Abocom FE2500MX 10/100BaseTX" },
212         { DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112), 0,
213                 "Conexant LANfinity MiniPCI 10/100BaseTX" },
214         { DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX), 0,
215                 "Hawking CB102 CardBus 10/100" },
216         { DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T), 0,
217                 "PlaneX FNW-3602-T CardBus 10/100" },
218         { DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB), 0,
219                 "3Com OfficeConnect 10/100B" },
220         { DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120), 0,
221                 "Microsoft MN-120 CardBus 10/100" },
222         { DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130), 0,
223                 "Microsoft MN-130 10/100" },
224         { DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08), 0,
225                 "Linksys PCMPC200 CardBus 10/100" },
226         { DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09), 0,
227                 "Linksys PCMPC200 CardBus 10/100" },
228         { 0, 0, NULL }
229 };
230
231 static int dc_probe(device_t);
232 static int dc_attach(device_t);
233 static int dc_detach(device_t);
234 static int dc_suspend(device_t);
235 static int dc_resume(device_t);
236 static const struct dc_type *dc_devtype(device_t);
237 static void dc_discard_rxbuf(struct dc_softc *, int);
238 static int dc_newbuf(struct dc_softc *, int);
239 static int dc_encap(struct dc_softc *, struct mbuf **);
240 static void dc_pnic_rx_bug_war(struct dc_softc *, int);
241 static int dc_rx_resync(struct dc_softc *);
242 static int dc_rxeof(struct dc_softc *);
243 static void dc_txeof(struct dc_softc *);
244 static void dc_tick(void *);
245 static void dc_tx_underrun(struct dc_softc *);
246 static void dc_intr(void *);
247 static void dc_start(struct ifnet *);
248 static void dc_start_locked(struct ifnet *);
249 static int dc_ioctl(struct ifnet *, u_long, caddr_t);
250 static void dc_init(void *);
251 static void dc_init_locked(struct dc_softc *);
252 static void dc_stop(struct dc_softc *);
253 static void dc_watchdog(void *);
254 static int dc_shutdown(device_t);
255 static int dc_ifmedia_upd(struct ifnet *);
256 static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
257
258 static int dc_dma_alloc(struct dc_softc *);
259 static void dc_dma_free(struct dc_softc *);
260 static void dc_dma_map_addr(void *, bus_dma_segment_t *, int, int);
261
262 static void dc_delay(struct dc_softc *);
263 static void dc_eeprom_idle(struct dc_softc *);
264 static void dc_eeprom_putbyte(struct dc_softc *, int);
265 static void dc_eeprom_getword(struct dc_softc *, int, uint16_t *);
266 static void dc_eeprom_getword_pnic(struct dc_softc *, int, uint16_t *);
267 static void dc_eeprom_getword_xircom(struct dc_softc *, int, uint16_t *);
268 static void dc_eeprom_width(struct dc_softc *);
269 static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
270
271 static int dc_miibus_readreg(device_t, int, int);
272 static int dc_miibus_writereg(device_t, int, int, int);
273 static void dc_miibus_statchg(device_t);
274 static void dc_miibus_mediainit(device_t);
275
276 static void dc_setcfg(struct dc_softc *, int);
277 static void dc_netcfg_wait(struct dc_softc *);
278 static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
279 static uint32_t dc_mchash_be(const uint8_t *);
280 static void dc_setfilt_21143(struct dc_softc *);
281 static void dc_setfilt_asix(struct dc_softc *);
282 static void dc_setfilt_admtek(struct dc_softc *);
283 static void dc_setfilt_xircom(struct dc_softc *);
284
285 static void dc_setfilt(struct dc_softc *);
286
287 static void dc_reset(struct dc_softc *);
288 static int dc_list_rx_init(struct dc_softc *);
289 static int dc_list_tx_init(struct dc_softc *);
290
291 static int dc_read_srom(struct dc_softc *, int);
292 static int dc_parse_21143_srom(struct dc_softc *);
293 static int dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
294 static int dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
295 static int dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
296 static void dc_apply_fixup(struct dc_softc *, int);
297 static int dc_check_multiport(struct dc_softc *);
298
299 /*
300  * MII bit-bang glue
301  */
302 static uint32_t dc_mii_bitbang_read(device_t);
303 static void dc_mii_bitbang_write(device_t, uint32_t);
304
305 static const struct mii_bitbang_ops dc_mii_bitbang_ops = {
306         dc_mii_bitbang_read,
307         dc_mii_bitbang_write,
308         {
309                 DC_SIO_MII_DATAOUT,     /* MII_BIT_MDO */
310                 DC_SIO_MII_DATAIN,      /* MII_BIT_MDI */
311                 DC_SIO_MII_CLK,         /* MII_BIT_MDC */
312                 0,                      /* MII_BIT_DIR_HOST_PHY */
313                 DC_SIO_MII_DIR,         /* MII_BIT_DIR_PHY_HOST */
314         }
315 };
316
317 #ifdef DC_USEIOSPACE
318 #define DC_RES                  SYS_RES_IOPORT
319 #define DC_RID                  DC_PCI_CFBIO
320 #else
321 #define DC_RES                  SYS_RES_MEMORY
322 #define DC_RID                  DC_PCI_CFBMA
323 #endif
324
325 static device_method_t dc_methods[] = {
326         /* Device interface */
327         DEVMETHOD(device_probe,         dc_probe),
328         DEVMETHOD(device_attach,        dc_attach),
329         DEVMETHOD(device_detach,        dc_detach),
330         DEVMETHOD(device_suspend,       dc_suspend),
331         DEVMETHOD(device_resume,        dc_resume),
332         DEVMETHOD(device_shutdown,      dc_shutdown),
333
334         /* bus interface */
335         DEVMETHOD(bus_print_child,      bus_generic_print_child),
336         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
337
338         /* MII interface */
339         DEVMETHOD(miibus_readreg,       dc_miibus_readreg),
340         DEVMETHOD(miibus_writereg,      dc_miibus_writereg),
341         DEVMETHOD(miibus_statchg,       dc_miibus_statchg),
342         DEVMETHOD(miibus_mediainit,     dc_miibus_mediainit),
343
344         { 0, 0 }
345 };
346
347 static driver_t dc_driver = {
348         "dc",
349         dc_methods,
350         sizeof(struct dc_softc)
351 };
352
353 static devclass_t dc_devclass;
354
355 DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
356 DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
357
358 #define DC_SETBIT(sc, reg, x)                           \
359         CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
360
361 #define DC_CLRBIT(sc, reg, x)                           \
362         CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
363
364 #define SIO_SET(x)      DC_SETBIT(sc, DC_SIO, (x))
365 #define SIO_CLR(x)      DC_CLRBIT(sc, DC_SIO, (x))
366
367 static void
368 dc_delay(struct dc_softc *sc)
369 {
370         int idx;
371
372         for (idx = (300 / 33) + 1; idx > 0; idx--)
373                 CSR_READ_4(sc, DC_BUSCTL);
374 }
375
376 static void
377 dc_eeprom_width(struct dc_softc *sc)
378 {
379         int i;
380
381         /* Force EEPROM to idle state. */
382         dc_eeprom_idle(sc);
383
384         /* Enter EEPROM access mode. */
385         CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
386         dc_delay(sc);
387         DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
388         dc_delay(sc);
389         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
390         dc_delay(sc);
391         DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
392         dc_delay(sc);
393
394         for (i = 3; i--;) {
395                 if (6 & (1 << i))
396                         DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
397                 else
398                         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
399                 dc_delay(sc);
400                 DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
401                 dc_delay(sc);
402                 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
403                 dc_delay(sc);
404         }
405
406         for (i = 1; i <= 12; i++) {
407                 DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
408                 dc_delay(sc);
409                 if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
410                         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
411                         dc_delay(sc);
412                         break;
413                 }
414                 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
415                 dc_delay(sc);
416         }
417
418         /* Turn off EEPROM access mode. */
419         dc_eeprom_idle(sc);
420
421         if (i < 4 || i > 12)
422                 sc->dc_romwidth = 6;
423         else
424                 sc->dc_romwidth = i;
425
426         /* Enter EEPROM access mode. */
427         CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
428         dc_delay(sc);
429         DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
430         dc_delay(sc);
431         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
432         dc_delay(sc);
433         DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
434         dc_delay(sc);
435
436         /* Turn off EEPROM access mode. */
437         dc_eeprom_idle(sc);
438 }
439
440 static void
441 dc_eeprom_idle(struct dc_softc *sc)
442 {
443         int i;
444
445         CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
446         dc_delay(sc);
447         DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
448         dc_delay(sc);
449         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
450         dc_delay(sc);
451         DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
452         dc_delay(sc);
453
454         for (i = 0; i < 25; i++) {
455                 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
456                 dc_delay(sc);
457                 DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
458                 dc_delay(sc);
459         }
460
461         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
462         dc_delay(sc);
463         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
464         dc_delay(sc);
465         CSR_WRITE_4(sc, DC_SIO, 0x00000000);
466 }
467
468 /*
469  * Send a read command and address to the EEPROM, check for ACK.
470  */
471 static void
472 dc_eeprom_putbyte(struct dc_softc *sc, int addr)
473 {
474         int d, i;
475
476         d = DC_EECMD_READ >> 6;
477         for (i = 3; i--; ) {
478                 if (d & (1 << i))
479                         DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
480                 else
481                         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
482                 dc_delay(sc);
483                 DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
484                 dc_delay(sc);
485                 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
486                 dc_delay(sc);
487         }
488
489         /*
490          * Feed in each bit and strobe the clock.
491          */
492         for (i = sc->dc_romwidth; i--;) {
493                 if (addr & (1 << i)) {
494                         SIO_SET(DC_SIO_EE_DATAIN);
495                 } else {
496                         SIO_CLR(DC_SIO_EE_DATAIN);
497                 }
498                 dc_delay(sc);
499                 SIO_SET(DC_SIO_EE_CLK);
500                 dc_delay(sc);
501                 SIO_CLR(DC_SIO_EE_CLK);
502                 dc_delay(sc);
503         }
504 }
505
506 /*
507  * Read a word of data stored in the EEPROM at address 'addr.'
508  * The PNIC 82c168/82c169 has its own non-standard way to read
509  * the EEPROM.
510  */
511 static void
512 dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, uint16_t *dest)
513 {
514         int i;
515         uint32_t r;
516
517         CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
518
519         for (i = 0; i < DC_TIMEOUT; i++) {
520                 DELAY(1);
521                 r = CSR_READ_4(sc, DC_SIO);
522                 if (!(r & DC_PN_SIOCTL_BUSY)) {
523                         *dest = (uint16_t)(r & 0xFFFF);
524                         return;
525                 }
526         }
527 }
528
529 /*
530  * Read a word of data stored in the EEPROM at address 'addr.'
531  * The Xircom X3201 has its own non-standard way to read
532  * the EEPROM, too.
533  */
534 static void
535 dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, uint16_t *dest)
536 {
537
538         SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
539
540         addr *= 2;
541         CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
542         *dest = (uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
543         addr += 1;
544         CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
545         *dest |= ((uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
546
547         SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
548 }
549
550 /*
551  * Read a word of data stored in the EEPROM at address 'addr.'
552  */
553 static void
554 dc_eeprom_getword(struct dc_softc *sc, int addr, uint16_t *dest)
555 {
556         int i;
557         uint16_t word = 0;
558
559         /* Force EEPROM to idle state. */
560         dc_eeprom_idle(sc);
561
562         /* Enter EEPROM access mode. */
563         CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
564         dc_delay(sc);
565         DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
566         dc_delay(sc);
567         DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
568         dc_delay(sc);
569         DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
570         dc_delay(sc);
571
572         /*
573          * Send address of word we want to read.
574          */
575         dc_eeprom_putbyte(sc, addr);
576
577         /*
578          * Start reading bits from EEPROM.
579          */
580         for (i = 0x8000; i; i >>= 1) {
581                 SIO_SET(DC_SIO_EE_CLK);
582                 dc_delay(sc);
583                 if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
584                         word |= i;
585                 dc_delay(sc);
586                 SIO_CLR(DC_SIO_EE_CLK);
587                 dc_delay(sc);
588         }
589
590         /* Turn off EEPROM access mode. */
591         dc_eeprom_idle(sc);
592
593         *dest = word;
594 }
595
596 /*
597  * Read a sequence of words from the EEPROM.
598  */
599 static void
600 dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
601 {
602         int i;
603         uint16_t word = 0, *ptr;
604
605         for (i = 0; i < cnt; i++) {
606                 if (DC_IS_PNIC(sc))
607                         dc_eeprom_getword_pnic(sc, off + i, &word);
608                 else if (DC_IS_XIRCOM(sc))
609                         dc_eeprom_getword_xircom(sc, off + i, &word);
610                 else
611                         dc_eeprom_getword(sc, off + i, &word);
612                 ptr = (uint16_t *)(dest + (i * 2));
613                 if (be)
614                         *ptr = be16toh(word);
615                 else
616                         *ptr = le16toh(word);
617         }
618 }
619
620 /*
621  * Write the MII serial port for the MII bit-bang module.
622  */
623 static void
624 dc_mii_bitbang_write(device_t dev, uint32_t val)
625 {
626         struct dc_softc *sc;
627
628         sc = device_get_softc(dev);
629
630         CSR_WRITE_4(sc, DC_SIO, val);
631         CSR_BARRIER_4(sc, DC_SIO,
632             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
633 }
634
635 /*
636  * Read the MII serial port for the MII bit-bang module.
637  */
638 static uint32_t
639 dc_mii_bitbang_read(device_t dev)
640 {
641         struct dc_softc *sc;
642         uint32_t val;
643
644         sc = device_get_softc(dev);
645
646         val = CSR_READ_4(sc, DC_SIO);
647         CSR_BARRIER_4(sc, DC_SIO,
648             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
649
650         return (val);
651 }
652
653 static int
654 dc_miibus_readreg(device_t dev, int phy, int reg)
655 {
656         struct dc_softc *sc;
657         int i, rval, phy_reg = 0;
658
659         sc = device_get_softc(dev);
660
661         if (sc->dc_pmode != DC_PMODE_MII) {
662                 if (phy == (MII_NPHY - 1)) {
663                         switch (reg) {
664                         case MII_BMSR:
665                         /*
666                          * Fake something to make the probe
667                          * code think there's a PHY here.
668                          */
669                                 return (BMSR_MEDIAMASK);
670                                 break;
671                         case MII_PHYIDR1:
672                                 if (DC_IS_PNIC(sc))
673                                         return (DC_VENDORID_LO);
674                                 return (DC_VENDORID_DEC);
675                                 break;
676                         case MII_PHYIDR2:
677                                 if (DC_IS_PNIC(sc))
678                                         return (DC_DEVICEID_82C168);
679                                 return (DC_DEVICEID_21143);
680                                 break;
681                         default:
682                                 return (0);
683                                 break;
684                         }
685                 } else
686                         return (0);
687         }
688
689         if (DC_IS_PNIC(sc)) {
690                 CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
691                     (phy << 23) | (reg << 18));
692                 for (i = 0; i < DC_TIMEOUT; i++) {
693                         DELAY(1);
694                         rval = CSR_READ_4(sc, DC_PN_MII);
695                         if (!(rval & DC_PN_MII_BUSY)) {
696                                 rval &= 0xFFFF;
697                                 return (rval == 0xFFFF ? 0 : rval);
698                         }
699                 }
700                 return (0);
701         }
702
703         if (DC_IS_COMET(sc)) {
704                 switch (reg) {
705                 case MII_BMCR:
706                         phy_reg = DC_AL_BMCR;
707                         break;
708                 case MII_BMSR:
709                         phy_reg = DC_AL_BMSR;
710                         break;
711                 case MII_PHYIDR1:
712                         phy_reg = DC_AL_VENID;
713                         break;
714                 case MII_PHYIDR2:
715                         phy_reg = DC_AL_DEVID;
716                         break;
717                 case MII_ANAR:
718                         phy_reg = DC_AL_ANAR;
719                         break;
720                 case MII_ANLPAR:
721                         phy_reg = DC_AL_LPAR;
722                         break;
723                 case MII_ANER:
724                         phy_reg = DC_AL_ANER;
725                         break;
726                 default:
727                         device_printf(dev, "phy_read: bad phy register %x\n",
728                             reg);
729                         return (0);
730                         break;
731                 }
732
733                 rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
734                 if (rval == 0xFFFF)
735                         return (0);
736                 return (rval);
737         }
738
739         if (sc->dc_type == DC_TYPE_98713) {
740                 phy_reg = CSR_READ_4(sc, DC_NETCFG);
741                 CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
742         }
743         rval = mii_bitbang_readreg(dev, &dc_mii_bitbang_ops, phy, reg);
744         if (sc->dc_type == DC_TYPE_98713)
745                 CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
746
747         return (rval);
748 }
749
750 static int
751 dc_miibus_writereg(device_t dev, int phy, int reg, int data)
752 {
753         struct dc_softc *sc;
754         int i, phy_reg = 0;
755
756         sc = device_get_softc(dev);
757
758         if (DC_IS_PNIC(sc)) {
759                 CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
760                     (phy << 23) | (reg << 10) | data);
761                 for (i = 0; i < DC_TIMEOUT; i++) {
762                         if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
763                                 break;
764                 }
765                 return (0);
766         }
767
768         if (DC_IS_COMET(sc)) {
769                 switch (reg) {
770                 case MII_BMCR:
771                         phy_reg = DC_AL_BMCR;
772                         break;
773                 case MII_BMSR:
774                         phy_reg = DC_AL_BMSR;
775                         break;
776                 case MII_PHYIDR1:
777                         phy_reg = DC_AL_VENID;
778                         break;
779                 case MII_PHYIDR2:
780                         phy_reg = DC_AL_DEVID;
781                         break;
782                 case MII_ANAR:
783                         phy_reg = DC_AL_ANAR;
784                         break;
785                 case MII_ANLPAR:
786                         phy_reg = DC_AL_LPAR;
787                         break;
788                 case MII_ANER:
789                         phy_reg = DC_AL_ANER;
790                         break;
791                 default:
792                         device_printf(dev, "phy_write: bad phy register %x\n",
793                             reg);
794                         return (0);
795                         break;
796                 }
797
798                 CSR_WRITE_4(sc, phy_reg, data);
799                 return (0);
800         }
801
802         if (sc->dc_type == DC_TYPE_98713) {
803                 phy_reg = CSR_READ_4(sc, DC_NETCFG);
804                 CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
805         }
806         mii_bitbang_writereg(dev, &dc_mii_bitbang_ops, phy, reg, data);
807         if (sc->dc_type == DC_TYPE_98713)
808                 CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
809
810         return (0);
811 }
812
813 static void
814 dc_miibus_statchg(device_t dev)
815 {
816         struct dc_softc *sc;
817         struct ifnet *ifp;
818         struct mii_data *mii;
819         struct ifmedia *ifm;
820
821         sc = device_get_softc(dev);
822
823         mii = device_get_softc(sc->dc_miibus);
824         ifp = sc->dc_ifp;
825         if (mii == NULL || ifp == NULL ||
826             (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
827                 return;
828
829         ifm = &mii->mii_media;
830         if (DC_IS_DAVICOM(sc) &&
831             IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
832                 dc_setcfg(sc, ifm->ifm_media);
833                 sc->dc_if_media = ifm->ifm_media;
834                 return;
835         }
836
837         sc->dc_link = 0;
838         if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
839             (IFM_ACTIVE | IFM_AVALID)) {
840                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
841                 case IFM_10_T:
842                 case IFM_100_TX:
843                         sc->dc_link = 1;
844                         break;
845                 default:
846                         break;
847                 }
848         }
849         if (sc->dc_link == 0)
850                 return;
851
852         sc->dc_if_media = mii->mii_media_active;
853         if (DC_IS_ADMTEK(sc))
854                 return;
855         dc_setcfg(sc, mii->mii_media_active);
856 }
857
858 /*
859  * Special support for DM9102A cards with HomePNA PHYs. Note:
860  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
861  * to be impossible to talk to the management interface of the DM9801
862  * PHY (its MDIO pin is not connected to anything). Consequently,
863  * the driver has to just 'know' about the additional mode and deal
864  * with it itself. *sigh*
865  */
866 static void
867 dc_miibus_mediainit(device_t dev)
868 {
869         struct dc_softc *sc;
870         struct mii_data *mii;
871         struct ifmedia *ifm;
872         int rev;
873
874         rev = pci_get_revid(dev);
875
876         sc = device_get_softc(dev);
877         mii = device_get_softc(sc->dc_miibus);
878         ifm = &mii->mii_media;
879
880         if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
881                 ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
882 }
883
884 #define DC_BITS_512     9
885 #define DC_BITS_128     7
886 #define DC_BITS_64      6
887
888 static uint32_t
889 dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
890 {
891         uint32_t crc;
892
893         /* Compute CRC for the address value. */
894         crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
895
896         /*
897          * The hash table on the PNIC II and the MX98715AEC-C/D/E
898          * chips is only 128 bits wide.
899          */
900         if (sc->dc_flags & DC_128BIT_HASH)
901                 return (crc & ((1 << DC_BITS_128) - 1));
902
903         /* The hash table on the MX98715BEC is only 64 bits wide. */
904         if (sc->dc_flags & DC_64BIT_HASH)
905                 return (crc & ((1 << DC_BITS_64) - 1));
906
907         /* Xircom's hash filtering table is different (read: weird) */
908         /* Xircom uses the LEAST significant bits */
909         if (DC_IS_XIRCOM(sc)) {
910                 if ((crc & 0x180) == 0x180)
911                         return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
912                 else
913                         return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
914                             (12 << 4));
915         }
916
917         return (crc & ((1 << DC_BITS_512) - 1));
918 }
919
920 /*
921  * Calculate CRC of a multicast group address, return the lower 6 bits.
922  */
923 static uint32_t
924 dc_mchash_be(const uint8_t *addr)
925 {
926         uint32_t crc;
927
928         /* Compute CRC for the address value. */
929         crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
930
931         /* Return the filter bit position. */
932         return ((crc >> 26) & 0x0000003F);
933 }
934
935 /*
936  * 21143-style RX filter setup routine. Filter programming is done by
937  * downloading a special setup frame into the TX engine. 21143, Macronix,
938  * PNIC, PNIC II and Davicom chips are programmed this way.
939  *
940  * We always program the chip using 'hash perfect' mode, i.e. one perfect
941  * address (our node address) and a 512-bit hash filter for multicast
942  * frames. We also sneak the broadcast address into the hash filter since
943  * we need that too.
944  */
945 static void
946 dc_setfilt_21143(struct dc_softc *sc)
947 {
948         uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
949         struct dc_desc *sframe;
950         uint32_t h, *sp;
951         struct ifmultiaddr *ifma;
952         struct ifnet *ifp;
953         int i;
954
955         ifp = sc->dc_ifp;
956
957         i = sc->dc_cdata.dc_tx_prod;
958         DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
959         sc->dc_cdata.dc_tx_cnt++;
960         sframe = &sc->dc_ldata.dc_tx_list[i];
961         sp = sc->dc_cdata.dc_sbuf;
962         bzero(sp, DC_SFRAME_LEN);
963
964         sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
965         sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
966             DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
967
968         sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
969
970         /* If we want promiscuous mode, set the allframes bit. */
971         if (ifp->if_flags & IFF_PROMISC)
972                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
973         else
974                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
975
976         if (ifp->if_flags & IFF_ALLMULTI)
977                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
978         else
979                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
980
981         if_maddr_rlock(ifp);
982         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
983                 if (ifma->ifma_addr->sa_family != AF_LINK)
984                         continue;
985                 h = dc_mchash_le(sc,
986                     LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
987                 sp[h >> 4] |= htole32(1 << (h & 0xF));
988         }
989         if_maddr_runlock(ifp);
990
991         if (ifp->if_flags & IFF_BROADCAST) {
992                 h = dc_mchash_le(sc, ifp->if_broadcastaddr);
993                 sp[h >> 4] |= htole32(1 << (h & 0xF));
994         }
995
996         /* Set our MAC address. */
997         bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
998         sp[39] = DC_SP_MAC(eaddr[0]);
999         sp[40] = DC_SP_MAC(eaddr[1]);
1000         sp[41] = DC_SP_MAC(eaddr[2]);
1001
1002         sframe->dc_status = htole32(DC_TXSTAT_OWN);
1003         bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
1004             BUS_DMASYNC_PREWRITE);
1005         bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1006         CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1007
1008         /*
1009          * The PNIC takes an exceedingly long time to process its
1010          * setup frame; wait 10ms after posting the setup frame
1011          * before proceeding, just so it has time to swallow its
1012          * medicine.
1013          */
1014         DELAY(10000);
1015
1016         sc->dc_wdog_timer = 5;
1017 }
1018
1019 static void
1020 dc_setfilt_admtek(struct dc_softc *sc)
1021 {
1022         uint8_t eaddr[ETHER_ADDR_LEN];
1023         struct ifnet *ifp;
1024         struct ifmultiaddr *ifma;
1025         int h = 0;
1026         uint32_t hashes[2] = { 0, 0 };
1027
1028         ifp = sc->dc_ifp;
1029
1030         /* Init our MAC address. */
1031         bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
1032         CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
1033             eaddr[1] << 8 | eaddr[0]);
1034         CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
1035
1036         /* If we want promiscuous mode, set the allframes bit. */
1037         if (ifp->if_flags & IFF_PROMISC)
1038                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1039         else
1040                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1041
1042         if (ifp->if_flags & IFF_ALLMULTI)
1043                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1044         else
1045                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1046
1047         /* First, zot all the existing hash bits. */
1048         CSR_WRITE_4(sc, DC_AL_MAR0, 0);
1049         CSR_WRITE_4(sc, DC_AL_MAR1, 0);
1050
1051         /*
1052          * If we're already in promisc or allmulti mode, we
1053          * don't have to bother programming the multicast filter.
1054          */
1055         if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
1056                 return;
1057
1058         /* Now program new ones. */
1059         if_maddr_rlock(ifp);
1060         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1061                 if (ifma->ifma_addr->sa_family != AF_LINK)
1062                         continue;
1063                 if (DC_IS_CENTAUR(sc))
1064                         h = dc_mchash_le(sc,
1065                             LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1066                 else
1067                         h = dc_mchash_be(
1068                             LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1069                 if (h < 32)
1070                         hashes[0] |= (1 << h);
1071                 else
1072                         hashes[1] |= (1 << (h - 32));
1073         }
1074         if_maddr_runlock(ifp);
1075
1076         CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
1077         CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
1078 }
1079
1080 static void
1081 dc_setfilt_asix(struct dc_softc *sc)
1082 {
1083         uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
1084         struct ifnet *ifp;
1085         struct ifmultiaddr *ifma;
1086         int h = 0;
1087         uint32_t hashes[2] = { 0, 0 };
1088
1089         ifp = sc->dc_ifp;
1090
1091         /* Init our MAC address. */
1092         bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
1093         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
1094         CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
1095         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
1096         CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
1097
1098         /* If we want promiscuous mode, set the allframes bit. */
1099         if (ifp->if_flags & IFF_PROMISC)
1100                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1101         else
1102                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1103
1104         if (ifp->if_flags & IFF_ALLMULTI)
1105                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1106         else
1107                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1108
1109         /*
1110          * The ASIX chip has a special bit to enable reception
1111          * of broadcast frames.
1112          */
1113         if (ifp->if_flags & IFF_BROADCAST)
1114                 DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
1115         else
1116                 DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
1117
1118         /* first, zot all the existing hash bits */
1119         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
1120         CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
1121         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
1122         CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
1123
1124         /*
1125          * If we're already in promisc or allmulti mode, we
1126          * don't have to bother programming the multicast filter.
1127          */
1128         if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
1129                 return;
1130
1131         /* now program new ones */
1132         if_maddr_rlock(ifp);
1133         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1134                 if (ifma->ifma_addr->sa_family != AF_LINK)
1135                         continue;
1136                 h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1137                 if (h < 32)
1138                         hashes[0] |= (1 << h);
1139                 else
1140                         hashes[1] |= (1 << (h - 32));
1141         }
1142         if_maddr_runlock(ifp);
1143
1144         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
1145         CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
1146         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
1147         CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
1148 }
1149
1150 static void
1151 dc_setfilt_xircom(struct dc_softc *sc)
1152 {
1153         uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
1154         struct ifnet *ifp;
1155         struct ifmultiaddr *ifma;
1156         struct dc_desc *sframe;
1157         uint32_t h, *sp;
1158         int i;
1159
1160         ifp = sc->dc_ifp;
1161         DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1162
1163         i = sc->dc_cdata.dc_tx_prod;
1164         DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1165         sc->dc_cdata.dc_tx_cnt++;
1166         sframe = &sc->dc_ldata.dc_tx_list[i];
1167         sp = sc->dc_cdata.dc_sbuf;
1168         bzero(sp, DC_SFRAME_LEN);
1169
1170         sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1171         sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1172             DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1173
1174         sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1175
1176         /* If we want promiscuous mode, set the allframes bit. */
1177         if (ifp->if_flags & IFF_PROMISC)
1178                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1179         else
1180                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1181
1182         if (ifp->if_flags & IFF_ALLMULTI)
1183                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1184         else
1185                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1186
1187         if_maddr_rlock(ifp);
1188         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1189                 if (ifma->ifma_addr->sa_family != AF_LINK)
1190                         continue;
1191                 h = dc_mchash_le(sc,
1192                     LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1193                 sp[h >> 4] |= htole32(1 << (h & 0xF));
1194         }
1195         if_maddr_runlock(ifp);
1196
1197         if (ifp->if_flags & IFF_BROADCAST) {
1198                 h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1199                 sp[h >> 4] |= htole32(1 << (h & 0xF));
1200         }
1201
1202         /* Set our MAC address. */
1203         bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
1204         sp[0] = DC_SP_MAC(eaddr[0]);
1205         sp[1] = DC_SP_MAC(eaddr[1]);
1206         sp[2] = DC_SP_MAC(eaddr[2]);
1207
1208         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1209         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1210         sframe->dc_status = htole32(DC_TXSTAT_OWN);
1211         bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
1212             BUS_DMASYNC_PREWRITE);
1213         bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1214         CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1215
1216         /*
1217          * Wait some time...
1218          */
1219         DELAY(1000);
1220
1221         sc->dc_wdog_timer = 5;
1222 }
1223
1224 static void
1225 dc_setfilt(struct dc_softc *sc)
1226 {
1227
1228         if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
1229             DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
1230                 dc_setfilt_21143(sc);
1231
1232         if (DC_IS_ASIX(sc))
1233                 dc_setfilt_asix(sc);
1234
1235         if (DC_IS_ADMTEK(sc))
1236                 dc_setfilt_admtek(sc);
1237
1238         if (DC_IS_XIRCOM(sc))
1239                 dc_setfilt_xircom(sc);
1240 }
1241
1242 static void
1243 dc_netcfg_wait(struct dc_softc *sc)
1244 {
1245         uint32_t isr;
1246         int i;
1247
1248         for (i = 0; i < DC_TIMEOUT; i++) {
1249                 isr = CSR_READ_4(sc, DC_ISR);
1250                 if (isr & DC_ISR_TX_IDLE &&
1251                     ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1252                     (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
1253                         break;
1254                 DELAY(10);
1255         }
1256         if (i == DC_TIMEOUT && bus_child_present(sc->dc_dev)) {
1257                 if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
1258                         device_printf(sc->dc_dev,
1259                             "%s: failed to force tx to idle state\n", __func__);
1260                 if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1261                     (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1262                     !DC_HAS_BROKEN_RXSTATE(sc))
1263                         device_printf(sc->dc_dev,
1264                             "%s: failed to force rx to idle state\n", __func__);
1265         }
1266 }
1267
1268 /*
1269  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
1270  * the netconfig register, we first have to put the transmit and/or
1271  * receive logic in the idle state.
1272  */
1273 static void
1274 dc_setcfg(struct dc_softc *sc, int media)
1275 {
1276         int restart = 0, watchdogreg;
1277
1278         if (IFM_SUBTYPE(media) == IFM_NONE)
1279                 return;
1280
1281         if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
1282                 restart = 1;
1283                 DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1284                 dc_netcfg_wait(sc);
1285         }
1286
1287         if (IFM_SUBTYPE(media) == IFM_100_TX) {
1288                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1289                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
1290                 if (sc->dc_pmode == DC_PMODE_MII) {
1291                         if (DC_IS_INTEL(sc)) {
1292                         /* There's a write enable bit here that reads as 1. */
1293                                 watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
1294                                 watchdogreg &= ~DC_WDOG_CTLWREN;
1295                                 watchdogreg |= DC_WDOG_JABBERDIS;
1296                                 CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1297                         } else {
1298                                 DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1299                         }
1300                         DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
1301                             DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
1302                         if (sc->dc_type == DC_TYPE_98713)
1303                                 DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
1304                                     DC_NETCFG_SCRAMBLER));
1305                         if (!DC_IS_DAVICOM(sc))
1306                                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1307                         DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1308                 } else {
1309                         if (DC_IS_PNIC(sc)) {
1310                                 DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
1311                                 DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
1312                                 DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
1313                         }
1314                         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1315                         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1316                         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
1317                 }
1318         }
1319
1320         if (IFM_SUBTYPE(media) == IFM_10_T) {
1321                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1322                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
1323                 if (sc->dc_pmode == DC_PMODE_MII) {
1324                         /* There's a write enable bit here that reads as 1. */
1325                         if (DC_IS_INTEL(sc)) {
1326                                 watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
1327                                 watchdogreg &= ~DC_WDOG_CTLWREN;
1328                                 watchdogreg |= DC_WDOG_JABBERDIS;
1329                                 CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1330                         } else {
1331                                 DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1332                         }
1333                         DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
1334                             DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
1335                         if (sc->dc_type == DC_TYPE_98713)
1336                                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1337                         if (!DC_IS_DAVICOM(sc))
1338                                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1339                         DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1340                 } else {
1341                         if (DC_IS_PNIC(sc)) {
1342                                 DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
1343                                 DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
1344                                 DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
1345                         }
1346                         DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1347                         DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1348                         DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
1349                         if (DC_IS_INTEL(sc)) {
1350                                 DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
1351                                 DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1352                                 if ((media & IFM_GMASK) == IFM_FDX)
1353                                         DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
1354                                 else
1355                                         DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
1356                                 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1357                                 DC_CLRBIT(sc, DC_10BTCTRL,
1358                                     DC_TCTL_AUTONEGENBL);
1359                                 DELAY(20000);
1360                         }
1361                 }
1362         }
1363
1364         /*
1365          * If this is a Davicom DM9102A card with a DM9801 HomePNA
1366          * PHY and we want HomePNA mode, set the portsel bit to turn
1367          * on the external MII port.
1368          */
1369         if (DC_IS_DAVICOM(sc)) {
1370                 if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1371                         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1372                         sc->dc_link = 1;
1373                 } else {
1374                         DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1375                 }
1376         }
1377
1378         if ((media & IFM_GMASK) == IFM_FDX) {
1379                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
1380                 if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
1381                         DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
1382         } else {
1383                 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
1384                 if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
1385                         DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
1386         }
1387
1388         if (restart)
1389                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
1390 }
1391
1392 static void
1393 dc_reset(struct dc_softc *sc)
1394 {
1395         int i;
1396
1397         DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
1398
1399         for (i = 0; i < DC_TIMEOUT; i++) {
1400                 DELAY(10);
1401                 if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
1402                         break;
1403         }
1404
1405         if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
1406             DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
1407                 DELAY(10000);
1408                 DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
1409                 i = 0;
1410         }
1411
1412         if (i == DC_TIMEOUT)
1413                 device_printf(sc->dc_dev, "reset never completed!\n");
1414
1415         /* Wait a little while for the chip to get its brains in order. */
1416         DELAY(1000);
1417
1418         CSR_WRITE_4(sc, DC_IMR, 0x00000000);
1419         CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
1420         CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
1421
1422         /*
1423          * Bring the SIA out of reset. In some cases, it looks
1424          * like failing to unreset the SIA soon enough gets it
1425          * into a state where it will never come out of reset
1426          * until we reset the whole chip again.
1427          */
1428         if (DC_IS_INTEL(sc)) {
1429                 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1430                 CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFFFFFF);
1431                 CSR_WRITE_4(sc, DC_WATCHDOG, 0);
1432         }
1433 }
1434
1435 static const struct dc_type *
1436 dc_devtype(device_t dev)
1437 {
1438         const struct dc_type *t;
1439         uint32_t devid;
1440         uint8_t rev;
1441
1442         t = dc_devs;
1443         devid = pci_get_devid(dev);
1444         rev = pci_get_revid(dev);
1445
1446         while (t->dc_name != NULL) {
1447                 if (devid == t->dc_devid && rev >= t->dc_minrev)
1448                         return (t);
1449                 t++;
1450         }
1451
1452         return (NULL);
1453 }
1454
1455 /*
1456  * Probe for a 21143 or clone chip. Check the PCI vendor and device
1457  * IDs against our list and return a device name if we find a match.
1458  * We do a little bit of extra work to identify the exact type of
1459  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
1460  * but different revision IDs. The same is true for 98715/98715A
1461  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
1462  * cases, the exact chip revision affects driver behavior.
1463  */
1464 static int
1465 dc_probe(device_t dev)
1466 {
1467         const struct dc_type *t;
1468
1469         t = dc_devtype(dev);
1470
1471         if (t != NULL) {
1472                 device_set_desc(dev, t->dc_name);
1473                 return (BUS_PROBE_DEFAULT);
1474         }
1475
1476         return (ENXIO);
1477 }
1478
1479 static void
1480 dc_apply_fixup(struct dc_softc *sc, int media)
1481 {
1482         struct dc_mediainfo *m;
1483         uint8_t *p;
1484         int i;
1485         uint32_t reg;
1486
1487         m = sc->dc_mi;
1488
1489         while (m != NULL) {
1490                 if (m->dc_media == media)
1491                         break;
1492                 m = m->dc_next;
1493         }
1494
1495         if (m == NULL)
1496                 return;
1497
1498         for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
1499                 reg = (p[0] | (p[1] << 8)) << 16;
1500                 CSR_WRITE_4(sc, DC_WATCHDOG, reg);
1501         }
1502
1503         for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
1504                 reg = (p[0] | (p[1] << 8)) << 16;
1505                 CSR_WRITE_4(sc, DC_WATCHDOG, reg);
1506         }
1507 }
1508
1509 static int
1510 dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
1511 {
1512         struct dc_mediainfo *m;
1513
1514         m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1515         if (m == NULL) {
1516                 device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1517                 return (ENOMEM);
1518         }
1519         switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
1520         case DC_SIA_CODE_10BT:
1521                 m->dc_media = IFM_10_T;
1522                 break;
1523         case DC_SIA_CODE_10BT_FDX:
1524                 m->dc_media = IFM_10_T | IFM_FDX;
1525                 break;
1526         case DC_SIA_CODE_10B2:
1527                 m->dc_media = IFM_10_2;
1528                 break;
1529         case DC_SIA_CODE_10B5:
1530                 m->dc_media = IFM_10_5;
1531                 break;
1532         default:
1533                 break;
1534         }
1535
1536         /*
1537          * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
1538          * Things apparently already work for cards that do
1539          * supply Media Specific Data.
1540          */
1541         if (l->dc_sia_code & DC_SIA_CODE_EXT) {
1542                 m->dc_gp_len = 2;
1543                 m->dc_gp_ptr =
1544                 (uint8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
1545         } else {
1546                 m->dc_gp_len = 2;
1547                 m->dc_gp_ptr =
1548                 (uint8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
1549         }
1550
1551         m->dc_next = sc->dc_mi;
1552         sc->dc_mi = m;
1553
1554         sc->dc_pmode = DC_PMODE_SIA;
1555         return (0);
1556 }
1557
1558 static int
1559 dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
1560 {
1561         struct dc_mediainfo *m;
1562
1563         m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1564         if (m == NULL) {
1565                 device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1566                 return (ENOMEM);
1567         }
1568         if (l->dc_sym_code == DC_SYM_CODE_100BT)
1569                 m->dc_media = IFM_100_TX;
1570
1571         if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
1572                 m->dc_media = IFM_100_TX | IFM_FDX;
1573
1574         m->dc_gp_len = 2;
1575         m->dc_gp_ptr = (uint8_t *)&l->dc_sym_gpio_ctl;
1576
1577         m->dc_next = sc->dc_mi;
1578         sc->dc_mi = m;
1579
1580         sc->dc_pmode = DC_PMODE_SYM;
1581         return (0);
1582 }
1583
1584 static int
1585 dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
1586 {
1587         struct dc_mediainfo *m;
1588         uint8_t *p;
1589
1590         m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1591         if (m == NULL) {
1592                 device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1593                 return (ENOMEM);
1594         }
1595         /* We abuse IFM_AUTO to represent MII. */
1596         m->dc_media = IFM_AUTO;
1597         m->dc_gp_len = l->dc_gpr_len;
1598
1599         p = (uint8_t *)l;
1600         p += sizeof(struct dc_eblock_mii);
1601         m->dc_gp_ptr = p;
1602         p += 2 * l->dc_gpr_len;
1603         m->dc_reset_len = *p;
1604         p++;
1605         m->dc_reset_ptr = p;
1606
1607         m->dc_next = sc->dc_mi;
1608         sc->dc_mi = m;
1609         return (0);
1610 }
1611
1612 static int
1613 dc_read_srom(struct dc_softc *sc, int bits)
1614 {
1615         int size;
1616
1617         size = DC_ROM_SIZE(bits);
1618         sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
1619         if (sc->dc_srom == NULL) {
1620                 device_printf(sc->dc_dev, "Could not allocate SROM buffer\n");
1621                 return (ENOMEM);
1622         }
1623         dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1624         return (0);
1625 }
1626
1627 static int
1628 dc_parse_21143_srom(struct dc_softc *sc)
1629 {
1630         struct dc_leaf_hdr *lhdr;
1631         struct dc_eblock_hdr *hdr;
1632         int error, have_mii, i, loff;
1633         char *ptr;
1634
1635         have_mii = 0;
1636         loff = sc->dc_srom[27];
1637         lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
1638
1639         ptr = (char *)lhdr;
1640         ptr += sizeof(struct dc_leaf_hdr) - 1;
1641         /*
1642          * Look if we got a MII media block.
1643          */
1644         for (i = 0; i < lhdr->dc_mcnt; i++) {
1645                 hdr = (struct dc_eblock_hdr *)ptr;
1646                 if (hdr->dc_type == DC_EBLOCK_MII)
1647                     have_mii++;
1648
1649                 ptr += (hdr->dc_len & 0x7F);
1650                 ptr++;
1651         }
1652
1653         /*
1654          * Do the same thing again. Only use SIA and SYM media
1655          * blocks if no MII media block is available.
1656          */
1657         ptr = (char *)lhdr;
1658         ptr += sizeof(struct dc_leaf_hdr) - 1;
1659         error = 0;
1660         for (i = 0; i < lhdr->dc_mcnt; i++) {
1661                 hdr = (struct dc_eblock_hdr *)ptr;
1662                 switch (hdr->dc_type) {
1663                 case DC_EBLOCK_MII:
1664                         error = dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
1665                         break;
1666                 case DC_EBLOCK_SIA:
1667                         if (! have_mii)
1668                                 error = dc_decode_leaf_sia(sc,
1669                                     (struct dc_eblock_sia *)hdr);
1670                         break;
1671                 case DC_EBLOCK_SYM:
1672                         if (! have_mii)
1673                                 error = dc_decode_leaf_sym(sc,
1674                                     (struct dc_eblock_sym *)hdr);
1675                         break;
1676                 default:
1677                         /* Don't care. Yet. */
1678                         break;
1679                 }
1680                 ptr += (hdr->dc_len & 0x7F);
1681                 ptr++;
1682         }
1683         return (error);
1684 }
1685
1686 static void
1687 dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1688 {
1689         bus_addr_t *paddr;
1690
1691         KASSERT(nseg == 1,
1692             ("%s: wrong number of segments (%d)", __func__, nseg));
1693         paddr = arg;
1694         *paddr = segs->ds_addr;
1695 }
1696
1697 static int
1698 dc_dma_alloc(struct dc_softc *sc)
1699 {
1700         int error, i;
1701
1702         error = bus_dma_tag_create(bus_get_dma_tag(sc->dc_dev), 1, 0,
1703             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1704             BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
1705             NULL, NULL, &sc->dc_ptag);
1706         if (error) {
1707                 device_printf(sc->dc_dev,
1708                     "failed to allocate parent DMA tag\n");
1709                 goto fail;
1710         }
1711
1712         /* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
1713         error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
1714             BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_RX_LIST_SZ, 1,
1715             DC_RX_LIST_SZ, 0, NULL, NULL, &sc->dc_rx_ltag);
1716         if (error) {
1717                 device_printf(sc->dc_dev, "failed to create RX list DMA tag\n");
1718                 goto fail;
1719         }
1720
1721         error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
1722             BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_TX_LIST_SZ, 1,
1723             DC_TX_LIST_SZ, 0, NULL, NULL, &sc->dc_tx_ltag);
1724         if (error) {
1725                 device_printf(sc->dc_dev, "failed to create TX list DMA tag\n");
1726                 goto fail;
1727         }
1728
1729         /* RX descriptor list. */
1730         error = bus_dmamem_alloc(sc->dc_rx_ltag,
1731             (void **)&sc->dc_ldata.dc_rx_list, BUS_DMA_NOWAIT |
1732             BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_rx_lmap);
1733         if (error) {
1734                 device_printf(sc->dc_dev,
1735                     "failed to allocate DMA'able memory for RX list\n");
1736                 goto fail;
1737         }
1738         error = bus_dmamap_load(sc->dc_rx_ltag, sc->dc_rx_lmap,
1739             sc->dc_ldata.dc_rx_list, DC_RX_LIST_SZ, dc_dma_map_addr,
1740             &sc->dc_ldata.dc_rx_list_paddr, BUS_DMA_NOWAIT);
1741         if (error) {
1742                 device_printf(sc->dc_dev,
1743                     "failed to load DMA'able memory for RX list\n");
1744                 goto fail;
1745         }
1746         /* TX descriptor list. */
1747         error = bus_dmamem_alloc(sc->dc_tx_ltag,
1748             (void **)&sc->dc_ldata.dc_tx_list, BUS_DMA_NOWAIT |
1749             BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_tx_lmap);
1750         if (error) {
1751                 device_printf(sc->dc_dev,
1752                     "failed to allocate DMA'able memory for TX list\n");
1753                 goto fail;
1754         }
1755         error = bus_dmamap_load(sc->dc_tx_ltag, sc->dc_tx_lmap,
1756             sc->dc_ldata.dc_tx_list, DC_TX_LIST_SZ, dc_dma_map_addr,
1757             &sc->dc_ldata.dc_tx_list_paddr, BUS_DMA_NOWAIT);
1758         if (error) {
1759                 device_printf(sc->dc_dev,
1760                     "cannot load DMA'able memory for TX list\n");
1761                 goto fail;
1762         }
1763
1764         /*
1765          * Allocate a busdma tag and DMA safe memory for the multicast
1766          * setup frame.
1767          */
1768         error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
1769             BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1770             DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
1771             0, NULL, NULL, &sc->dc_stag);
1772         if (error) {
1773                 device_printf(sc->dc_dev,
1774                     "failed to create DMA tag for setup frame\n");
1775                 goto fail;
1776         }
1777         error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
1778             BUS_DMA_NOWAIT, &sc->dc_smap);
1779         if (error) {
1780                 device_printf(sc->dc_dev,
1781                     "failed to allocate DMA'able memory for setup frame\n");
1782                 goto fail;
1783         }
1784         error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
1785             DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
1786         if (error) {
1787                 device_printf(sc->dc_dev,
1788                     "cannot load DMA'able memory for setup frame\n");
1789                 goto fail;
1790         }
1791
1792         /* Allocate a busdma tag for RX mbufs. */
1793         error = bus_dma_tag_create(sc->dc_ptag, DC_RXBUF_ALIGN, 0,
1794             BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1795             MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->dc_rx_mtag);
1796         if (error) {
1797                 device_printf(sc->dc_dev, "failed to create RX mbuf tag\n");
1798                 goto fail;
1799         }
1800
1801         /* Allocate a busdma tag for TX mbufs. */
1802         error = bus_dma_tag_create(sc->dc_ptag, 1, 0,
1803             BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1804             MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
1805             0, NULL, NULL, &sc->dc_tx_mtag);
1806         if (error) {
1807                 device_printf(sc->dc_dev, "failed to create TX mbuf tag\n");
1808                 goto fail;
1809         }
1810
1811         /* Create the TX/RX busdma maps. */
1812         for (i = 0; i < DC_TX_LIST_CNT; i++) {
1813                 error = bus_dmamap_create(sc->dc_tx_mtag, 0,
1814                     &sc->dc_cdata.dc_tx_map[i]);
1815                 if (error) {
1816                         device_printf(sc->dc_dev,
1817                             "failed to create TX mbuf dmamap\n");
1818                         goto fail;
1819                 }
1820         }
1821         for (i = 0; i < DC_RX_LIST_CNT; i++) {
1822                 error = bus_dmamap_create(sc->dc_rx_mtag, 0,
1823                     &sc->dc_cdata.dc_rx_map[i]);
1824                 if (error) {
1825                         device_printf(sc->dc_dev,
1826                             "failed to create RX mbuf dmamap\n");
1827                         goto fail;
1828                 }
1829         }
1830         error = bus_dmamap_create(sc->dc_rx_mtag, 0, &sc->dc_sparemap);
1831         if (error) {
1832                 device_printf(sc->dc_dev,
1833                     "failed to create spare RX mbuf dmamap\n");
1834                 goto fail;
1835         }
1836
1837 fail:
1838         return (error);
1839 }
1840
1841 static void
1842 dc_dma_free(struct dc_softc *sc)
1843 {
1844         int i;
1845
1846         /* RX buffers. */
1847         if (sc->dc_rx_mtag != NULL) {
1848                 for (i = 0; i < DC_RX_LIST_CNT; i++) {
1849                         if (sc->dc_cdata.dc_rx_map[i] != NULL)
1850                                 bus_dmamap_destroy(sc->dc_rx_mtag,
1851                                     sc->dc_cdata.dc_rx_map[i]);
1852                 }
1853                 if (sc->dc_sparemap != NULL)
1854                         bus_dmamap_destroy(sc->dc_rx_mtag, sc->dc_sparemap);
1855                 bus_dma_tag_destroy(sc->dc_rx_mtag);
1856         }
1857
1858         /* TX buffers. */
1859         if (sc->dc_rx_mtag != NULL) {
1860                 for (i = 0; i < DC_TX_LIST_CNT; i++) {
1861                         if (sc->dc_cdata.dc_tx_map[i] != NULL)
1862                                 bus_dmamap_destroy(sc->dc_tx_mtag,
1863                                     sc->dc_cdata.dc_tx_map[i]);
1864                 }
1865                 bus_dma_tag_destroy(sc->dc_tx_mtag);
1866         }
1867
1868         /* RX descriptor list. */
1869         if (sc->dc_rx_ltag) {
1870                 if (sc->dc_rx_lmap != NULL)
1871                         bus_dmamap_unload(sc->dc_rx_ltag, sc->dc_rx_lmap);
1872                 if (sc->dc_rx_lmap != NULL && sc->dc_ldata.dc_rx_list != NULL)
1873                         bus_dmamem_free(sc->dc_rx_ltag, sc->dc_ldata.dc_rx_list,
1874                             sc->dc_rx_lmap);
1875                 bus_dma_tag_destroy(sc->dc_rx_ltag);
1876         }
1877
1878         /* TX descriptor list. */
1879         if (sc->dc_tx_ltag) {
1880                 if (sc->dc_tx_lmap != NULL)
1881                         bus_dmamap_unload(sc->dc_tx_ltag, sc->dc_tx_lmap);
1882                 if (sc->dc_tx_lmap != NULL && sc->dc_ldata.dc_tx_list != NULL)
1883                         bus_dmamem_free(sc->dc_tx_ltag, sc->dc_ldata.dc_tx_list,
1884                             sc->dc_tx_lmap);
1885                 bus_dma_tag_destroy(sc->dc_tx_ltag);
1886         }
1887
1888         /* multicast setup frame. */
1889         if (sc->dc_stag) {
1890                 if (sc->dc_smap != NULL)
1891                         bus_dmamap_unload(sc->dc_stag, sc->dc_smap);
1892                 if (sc->dc_smap != NULL && sc->dc_cdata.dc_sbuf != NULL)
1893                         bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf,
1894                             sc->dc_smap);
1895                 bus_dma_tag_destroy(sc->dc_stag);
1896         }
1897 }
1898
1899 /*
1900  * Attach the interface. Allocate softc structures, do ifmedia
1901  * setup and ethernet/BPF attach.
1902  */
1903 static int
1904 dc_attach(device_t dev)
1905 {
1906         uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
1907         uint32_t command;
1908         struct dc_softc *sc;
1909         struct ifnet *ifp;
1910         struct dc_mediainfo *m;
1911         uint32_t reg, revision;
1912         int error, mac_offset, phy, rid, tmp;
1913         uint8_t *mac;
1914
1915         sc = device_get_softc(dev);
1916         sc->dc_dev = dev;
1917
1918         mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1919             MTX_DEF);
1920
1921         /*
1922          * Map control/status registers.
1923          */
1924         pci_enable_busmaster(dev);
1925
1926         rid = DC_RID;
1927         sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
1928
1929         if (sc->dc_res == NULL) {
1930                 device_printf(dev, "couldn't map ports/memory\n");
1931                 error = ENXIO;
1932                 goto fail;
1933         }
1934
1935         sc->dc_btag = rman_get_bustag(sc->dc_res);
1936         sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
1937
1938         /* Allocate interrupt. */
1939         rid = 0;
1940         sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1941             RF_SHAREABLE | RF_ACTIVE);
1942
1943         if (sc->dc_irq == NULL) {
1944                 device_printf(dev, "couldn't map interrupt\n");
1945                 error = ENXIO;
1946                 goto fail;
1947         }
1948
1949         /* Need this info to decide on a chip type. */
1950         sc->dc_info = dc_devtype(dev);
1951         revision = pci_get_revid(dev);
1952
1953         error = 0;
1954         /* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
1955         if (sc->dc_info->dc_devid !=
1956             DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
1957             sc->dc_info->dc_devid !=
1958             DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
1959                 dc_eeprom_width(sc);
1960
1961         switch (sc->dc_info->dc_devid) {
1962         case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
1963                 sc->dc_type = DC_TYPE_21143;
1964                 sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1965                 sc->dc_flags |= DC_REDUCED_MII_POLL;
1966                 /* Save EEPROM contents so we can parse them later. */
1967                 error = dc_read_srom(sc, sc->dc_romwidth);
1968                 if (error != 0)
1969                         goto fail;
1970                 break;
1971         case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
1972         case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
1973         case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
1974                 sc->dc_type = DC_TYPE_DM9102;
1975                 sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
1976                 sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
1977                 sc->dc_flags |= DC_TX_ALIGN;
1978                 sc->dc_pmode = DC_PMODE_MII;
1979
1980                 /* Increase the latency timer value. */
1981                 pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
1982                 break;
1983         case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
1984                 sc->dc_type = DC_TYPE_AL981;
1985                 sc->dc_flags |= DC_TX_USE_TX_INTR;
1986                 sc->dc_flags |= DC_TX_ADMTEK_WAR;
1987                 sc->dc_pmode = DC_PMODE_MII;
1988                 error = dc_read_srom(sc, sc->dc_romwidth);
1989                 if (error != 0)
1990                         goto fail;
1991                 break;
1992         case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983):
1993         case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
1994         case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
1995         case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
1996         case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
1997         case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
1998         case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
1999         case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
2000         case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
2001         case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
2002         case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
2003         case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
2004         case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
2005         case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
2006         case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
2007                 sc->dc_type = DC_TYPE_AN983;
2008                 sc->dc_flags |= DC_64BIT_HASH;
2009                 sc->dc_flags |= DC_TX_USE_TX_INTR;
2010                 sc->dc_flags |= DC_TX_ADMTEK_WAR;
2011                 sc->dc_pmode = DC_PMODE_MII;
2012                 /* Don't read SROM for - auto-loaded on reset */
2013                 break;
2014         case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
2015         case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
2016                 if (revision < DC_REVISION_98713A) {
2017                         sc->dc_type = DC_TYPE_98713;
2018                 }
2019                 if (revision >= DC_REVISION_98713A) {
2020                         sc->dc_type = DC_TYPE_98713A;
2021                         sc->dc_flags |= DC_21143_NWAY;
2022                 }
2023                 sc->dc_flags |= DC_REDUCED_MII_POLL;
2024                 sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2025                 break;
2026         case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
2027         case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
2028                 /*
2029                  * Macronix MX98715AEC-C/D/E parts have only a
2030                  * 128-bit hash table. We need to deal with these
2031                  * in the same manner as the PNIC II so that we
2032                  * get the right number of bits out of the
2033                  * CRC routine.
2034                  */
2035                 if (revision >= DC_REVISION_98715AEC_C &&
2036                     revision < DC_REVISION_98725)
2037                         sc->dc_flags |= DC_128BIT_HASH;
2038                 sc->dc_type = DC_TYPE_987x5;
2039                 sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2040                 sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2041                 break;
2042         case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
2043                 sc->dc_type = DC_TYPE_987x5;
2044                 sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2045                 sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2046                 break;
2047         case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
2048                 sc->dc_type = DC_TYPE_PNICII;
2049                 sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
2050                 sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2051                 break;
2052         case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
2053                 sc->dc_type = DC_TYPE_PNIC;
2054                 sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
2055                 sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
2056                 sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
2057                 if (sc->dc_pnic_rx_buf == NULL) {
2058                         device_printf(sc->dc_dev,
2059                             "Could not allocate PNIC RX buffer\n");
2060                         error = ENOMEM;
2061                         goto fail;
2062                 }
2063                 if (revision < DC_REVISION_82C169)
2064                         sc->dc_pmode = DC_PMODE_SYM;
2065                 break;
2066         case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
2067                 sc->dc_type = DC_TYPE_ASIX;
2068                 sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
2069                 sc->dc_flags |= DC_REDUCED_MII_POLL;
2070                 sc->dc_pmode = DC_PMODE_MII;
2071                 break;
2072         case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
2073                 sc->dc_type = DC_TYPE_XIRCOM;
2074                 sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
2075                                 DC_TX_ALIGN;
2076                 /*
2077                  * We don't actually need to coalesce, but we're doing
2078                  * it to obtain a double word aligned buffer.
2079                  * The DC_TX_COALESCE flag is required.
2080                  */
2081                 sc->dc_pmode = DC_PMODE_MII;
2082                 break;
2083         case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
2084                 sc->dc_type = DC_TYPE_CONEXANT;
2085                 sc->dc_flags |= DC_TX_INTR_ALWAYS;
2086                 sc->dc_flags |= DC_REDUCED_MII_POLL;
2087                 sc->dc_pmode = DC_PMODE_MII;
2088                 error = dc_read_srom(sc, sc->dc_romwidth);
2089                 if (error != 0)
2090                         goto fail;
2091                 break;
2092         default:
2093                 device_printf(dev, "unknown device: %x\n",
2094                     sc->dc_info->dc_devid);
2095                 break;
2096         }
2097
2098         /* Save the cache line size. */
2099         if (DC_IS_DAVICOM(sc))
2100                 sc->dc_cachesize = 0;
2101         else
2102                 sc->dc_cachesize = pci_get_cachelnsz(dev);
2103
2104         /* Reset the adapter. */
2105         dc_reset(sc);
2106
2107         /* Take 21143 out of snooze mode */
2108         if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
2109                 command = pci_read_config(dev, DC_PCI_CFDD, 4);
2110                 command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
2111                 pci_write_config(dev, DC_PCI_CFDD, command, 4);
2112         }
2113
2114         /*
2115          * Try to learn something about the supported media.
2116          * We know that ASIX and ADMtek and Davicom devices
2117          * will *always* be using MII media, so that's a no-brainer.
2118          * The tricky ones are the Macronix/PNIC II and the
2119          * Intel 21143.
2120          */
2121         if (DC_IS_INTEL(sc)) {
2122                 error = dc_parse_21143_srom(sc);
2123                 if (error != 0)
2124                         goto fail;
2125         } else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
2126                 if (sc->dc_type == DC_TYPE_98713)
2127                         sc->dc_pmode = DC_PMODE_MII;
2128                 else
2129                         sc->dc_pmode = DC_PMODE_SYM;
2130         } else if (!sc->dc_pmode)
2131                 sc->dc_pmode = DC_PMODE_MII;
2132
2133         /*
2134          * Get station address from the EEPROM.
2135          */
2136         switch(sc->dc_type) {
2137         case DC_TYPE_98713:
2138         case DC_TYPE_98713A:
2139         case DC_TYPE_987x5:
2140         case DC_TYPE_PNICII:
2141                 dc_read_eeprom(sc, (caddr_t)&mac_offset,
2142                     (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
2143                 dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
2144                 break;
2145         case DC_TYPE_PNIC:
2146                 dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
2147                 break;
2148         case DC_TYPE_DM9102:
2149                 dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2150 #ifdef __sparc64__
2151                 /*
2152                  * If this is an onboard dc(4) the station address read from
2153                  * the EEPROM is all zero and we have to get it from the FCode.
2154                  */
2155                 if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
2156                         OF_getetheraddr(dev, (caddr_t)&eaddr);
2157 #endif
2158                 break;
2159         case DC_TYPE_21143:
2160         case DC_TYPE_ASIX:
2161                 dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2162                 break;
2163         case DC_TYPE_AL981:
2164         case DC_TYPE_AN983:
2165                 reg = CSR_READ_4(sc, DC_AL_PAR0);
2166                 mac = (uint8_t *)&eaddr[0];
2167                 mac[0] = (reg >> 0) & 0xff;
2168                 mac[1] = (reg >> 8) & 0xff;
2169                 mac[2] = (reg >> 16) & 0xff;
2170                 mac[3] = (reg >> 24) & 0xff;
2171                 reg = CSR_READ_4(sc, DC_AL_PAR1);
2172                 mac[4] = (reg >> 0) & 0xff;
2173                 mac[5] = (reg >> 8) & 0xff;
2174                 break;
2175         case DC_TYPE_CONEXANT:
2176                 bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
2177                     ETHER_ADDR_LEN);
2178                 break;
2179         case DC_TYPE_XIRCOM:
2180                 /* The MAC comes from the CIS. */
2181                 mac = pci_get_ether(dev);
2182                 if (!mac) {
2183                         device_printf(dev, "No station address in CIS!\n");
2184                         error = ENXIO;
2185                         goto fail;
2186                 }
2187                 bcopy(mac, eaddr, ETHER_ADDR_LEN);
2188                 break;
2189         default:
2190                 dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2191                 break;
2192         }
2193
2194         bcopy(eaddr, sc->dc_eaddr, sizeof(eaddr));
2195         /*
2196          * If we still have invalid station address, see whether we can
2197          * find station address for chip 0.  Some multi-port controllers
2198          * just store station address for chip 0 if they have a shared
2199          * SROM.
2200          */
2201         if ((sc->dc_eaddr[0] == 0 && (sc->dc_eaddr[1] & ~0xffff) == 0) ||
2202             (sc->dc_eaddr[0] == 0xffffffff &&
2203             (sc->dc_eaddr[1] & 0xffff) == 0xffff)) {
2204                 error = dc_check_multiport(sc);
2205                 if (error == 0) {
2206                         bcopy(sc->dc_eaddr, eaddr, sizeof(eaddr));
2207                         /* Extract media information. */
2208                         if (DC_IS_INTEL(sc) && sc->dc_srom != NULL) {
2209                                 while (sc->dc_mi != NULL) {
2210                                         m = sc->dc_mi->dc_next;
2211                                         free(sc->dc_mi, M_DEVBUF);
2212                                         sc->dc_mi = m;
2213                                 }
2214                                 error = dc_parse_21143_srom(sc);
2215                                 if (error != 0)
2216                                         goto fail;
2217                         }
2218                 } else if (error == ENOMEM)
2219                         goto fail;
2220                 else
2221                         error = 0;
2222         }
2223
2224         if ((error = dc_dma_alloc(sc)) != 0)
2225                 goto fail;
2226
2227         ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2228         if (ifp == NULL) {
2229                 device_printf(dev, "can not if_alloc()\n");
2230                 error = ENOSPC;
2231                 goto fail;
2232         }
2233         ifp->if_softc = sc;
2234         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2235         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2236         ifp->if_ioctl = dc_ioctl;
2237         ifp->if_start = dc_start;
2238         ifp->if_init = dc_init;
2239         IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2240         ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2241         IFQ_SET_READY(&ifp->if_snd);
2242
2243         /*
2244          * Do MII setup. If this is a 21143, check for a PHY on the
2245          * MII bus after applying any necessary fixups to twiddle the
2246          * GPIO bits. If we don't end up finding a PHY, restore the
2247          * old selection (SIA only or SIA/SYM) and attach the dcphy
2248          * driver instead.
2249          */
2250         tmp = 0;
2251         if (DC_IS_INTEL(sc)) {
2252                 dc_apply_fixup(sc, IFM_AUTO);
2253                 tmp = sc->dc_pmode;
2254                 sc->dc_pmode = DC_PMODE_MII;
2255         }
2256
2257         /*
2258          * Setup General Purpose port mode and data so the tulip can talk
2259          * to the MII.  This needs to be done before mii_attach so that
2260          * we can actually see them.
2261          */
2262         if (DC_IS_XIRCOM(sc)) {
2263                 CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
2264                     DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2265                 DELAY(10);
2266                 CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
2267                     DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2268                 DELAY(10);
2269         }
2270
2271         phy = MII_PHY_ANY;
2272         /*
2273          * Note: both the AL981 and AN983 have internal PHYs, however the
2274          * AL981 provides direct access to the PHY registers while the AN983
2275          * uses a serial MII interface. The AN983's MII interface is also
2276          * buggy in that you can read from any MII address (0 to 31), but
2277          * only address 1 behaves normally. To deal with both cases, we
2278          * pretend that the PHY is at MII address 1.
2279          */
2280         if (DC_IS_ADMTEK(sc))
2281                 phy = DC_ADMTEK_PHYADDR;
2282
2283         /*
2284          * Note: the ukphy probes of the RS7112 report a PHY at MII address
2285          * 0 (possibly HomePNA?) and 1 (ethernet) so we only respond to the
2286          * correct one.
2287          */
2288         if (DC_IS_CONEXANT(sc))
2289                 phy = DC_CONEXANT_PHYADDR;
2290
2291         error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
2292             dc_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
2293
2294         if (error && DC_IS_INTEL(sc)) {
2295                 sc->dc_pmode = tmp;
2296                 if (sc->dc_pmode != DC_PMODE_SIA)
2297                         sc->dc_pmode = DC_PMODE_SYM;
2298                 sc->dc_flags |= DC_21143_NWAY;
2299                 mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
2300                     dc_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
2301                     MII_OFFSET_ANY, 0);
2302                 /*
2303                  * For non-MII cards, we need to have the 21143
2304                  * drive the LEDs. Except there are some systems
2305                  * like the NEC VersaPro NoteBook PC which have no
2306                  * LEDs, and twiddling these bits has adverse effects
2307                  * on them. (I.e. you suddenly can't get a link.)
2308                  */
2309                 if (!(pci_get_subvendor(dev) == 0x1033 &&
2310                     pci_get_subdevice(dev) == 0x8028))
2311                         sc->dc_flags |= DC_TULIP_LEDS;
2312                 error = 0;
2313         }
2314
2315         if (error) {
2316                 device_printf(dev, "attaching PHYs failed\n");
2317                 goto fail;
2318         }
2319
2320         if (DC_IS_ADMTEK(sc)) {
2321                 /*
2322                  * Set automatic TX underrun recovery for the ADMtek chips
2323                  */
2324                 DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2325         }
2326
2327         /*
2328          * Tell the upper layer(s) we support long frames.
2329          */
2330         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
2331         ifp->if_capabilities |= IFCAP_VLAN_MTU;
2332         ifp->if_capenable = ifp->if_capabilities;
2333 #ifdef DEVICE_POLLING
2334         ifp->if_capabilities |= IFCAP_POLLING;
2335 #endif
2336
2337         callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2338         callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
2339
2340         /*
2341          * Call MI attach routine.
2342          */
2343         ether_ifattach(ifp, (caddr_t)eaddr);
2344
2345         /* Hook interrupt last to avoid having to lock softc */
2346         error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2347             NULL, dc_intr, sc, &sc->dc_intrhand);
2348
2349         if (error) {
2350                 device_printf(dev, "couldn't set up irq\n");
2351                 ether_ifdetach(ifp);
2352                 goto fail;
2353         }
2354
2355 fail:
2356         if (error)
2357                 dc_detach(dev);
2358         return (error);
2359 }
2360
2361 /*
2362  * Shutdown hardware and free up resources. This can be called any
2363  * time after the mutex has been initialized. It is called in both
2364  * the error case in attach and the normal detach case so it needs
2365  * to be careful about only freeing resources that have actually been
2366  * allocated.
2367  */
2368 static int
2369 dc_detach(device_t dev)
2370 {
2371         struct dc_softc *sc;
2372         struct ifnet *ifp;
2373         struct dc_mediainfo *m;
2374
2375         sc = device_get_softc(dev);
2376         KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2377
2378         ifp = sc->dc_ifp;
2379
2380 #ifdef DEVICE_POLLING
2381         if (ifp->if_capenable & IFCAP_POLLING)
2382                 ether_poll_deregister(ifp);
2383 #endif
2384
2385         /* These should only be active if attach succeeded */
2386         if (device_is_attached(dev)) {
2387                 DC_LOCK(sc);
2388                 dc_stop(sc);
2389                 DC_UNLOCK(sc);
2390                 callout_drain(&sc->dc_stat_ch);
2391                 callout_drain(&sc->dc_wdog_ch);
2392                 ether_ifdetach(ifp);
2393         }
2394         if (sc->dc_miibus)
2395                 device_delete_child(dev, sc->dc_miibus);
2396         bus_generic_detach(dev);
2397
2398         if (sc->dc_intrhand)
2399                 bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
2400         if (sc->dc_irq)
2401                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
2402         if (sc->dc_res)
2403                 bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
2404
2405         if (ifp)
2406                 if_free(ifp);
2407
2408         dc_dma_free(sc);
2409
2410         free(sc->dc_pnic_rx_buf, M_DEVBUF);
2411
2412         while (sc->dc_mi != NULL) {
2413                 m = sc->dc_mi->dc_next;
2414                 free(sc->dc_mi, M_DEVBUF);
2415                 sc->dc_mi = m;
2416         }
2417         free(sc->dc_srom, M_DEVBUF);
2418
2419         mtx_destroy(&sc->dc_mtx);
2420
2421         return (0);
2422 }
2423
2424 /*
2425  * Initialize the transmit descriptors.
2426  */
2427 static int
2428 dc_list_tx_init(struct dc_softc *sc)
2429 {
2430         struct dc_chain_data *cd;
2431         struct dc_list_data *ld;
2432         int i, nexti;
2433
2434         cd = &sc->dc_cdata;
2435         ld = &sc->dc_ldata;
2436         for (i = 0; i < DC_TX_LIST_CNT; i++) {
2437                 if (i == DC_TX_LIST_CNT - 1)
2438                         nexti = 0;
2439                 else
2440                         nexti = i + 1;
2441                 ld->dc_tx_list[i].dc_status = 0;
2442                 ld->dc_tx_list[i].dc_ctl = 0;
2443                 ld->dc_tx_list[i].dc_data = 0;
2444                 ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
2445                 cd->dc_tx_chain[i] = NULL;
2446         }
2447
2448         cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
2449         cd->dc_tx_pkts = 0;
2450         bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
2451             BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2452         return (0);
2453 }
2454
2455
2456 /*
2457  * Initialize the RX descriptors and allocate mbufs for them. Note that
2458  * we arrange the descriptors in a closed ring, so that the last descriptor
2459  * points back to the first.
2460  */
2461 static int
2462 dc_list_rx_init(struct dc_softc *sc)
2463 {
2464         struct dc_chain_data *cd;
2465         struct dc_list_data *ld;
2466         int i, nexti;
2467
2468         cd = &sc->dc_cdata;
2469         ld = &sc->dc_ldata;
2470
2471         for (i = 0; i < DC_RX_LIST_CNT; i++) {
2472                 if (dc_newbuf(sc, i) != 0)
2473                         return (ENOBUFS);
2474                 if (i == DC_RX_LIST_CNT - 1)
2475                         nexti = 0;
2476                 else
2477                         nexti = i + 1;
2478                 ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
2479         }
2480
2481         cd->dc_rx_prod = 0;
2482         bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
2483             BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2484         return (0);
2485 }
2486
2487 /*
2488  * Initialize an RX descriptor and attach an MBUF cluster.
2489  */
2490 static int
2491 dc_newbuf(struct dc_softc *sc, int i)
2492 {
2493         struct mbuf *m;
2494         bus_dmamap_t map;
2495         bus_dma_segment_t segs[1];
2496         int error, nseg;
2497
2498         m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2499         if (m == NULL)
2500                 return (ENOBUFS);
2501         m->m_len = m->m_pkthdr.len = MCLBYTES;
2502         m_adj(m, sizeof(u_int64_t));
2503
2504         /*
2505          * If this is a PNIC chip, zero the buffer. This is part
2506          * of the workaround for the receive bug in the 82c168 and
2507          * 82c169 chips.
2508          */
2509         if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
2510                 bzero(mtod(m, char *), m->m_len);
2511
2512         error = bus_dmamap_load_mbuf_sg(sc->dc_rx_mtag, sc->dc_sparemap,
2513             m, segs, &nseg, 0);
2514         if (error) {
2515                 m_freem(m);
2516                 return (error);
2517         }
2518         KASSERT(nseg == 1, ("%s: wrong number of segments (%d)", __func__,
2519             nseg));
2520         if (sc->dc_cdata.dc_rx_chain[i] != NULL)
2521                 bus_dmamap_unload(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i]);
2522
2523         map = sc->dc_cdata.dc_rx_map[i];
2524         sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
2525         sc->dc_sparemap = map;
2526         sc->dc_cdata.dc_rx_chain[i] = m;
2527         bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
2528             BUS_DMASYNC_PREREAD);
2529
2530         sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
2531         sc->dc_ldata.dc_rx_list[i].dc_data =
2532             htole32(DC_ADDR_LO(segs[0].ds_addr));
2533         sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
2534         bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
2535             BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2536         return (0);
2537 }
2538
2539 /*
2540  * Grrrrr.
2541  * The PNIC chip has a terrible bug in it that manifests itself during
2542  * periods of heavy activity. The exact mode of failure if difficult to
2543  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
2544  * will happen on slow machines. The bug is that sometimes instead of
2545  * uploading one complete frame during reception, it uploads what looks
2546  * like the entire contents of its FIFO memory. The frame we want is at
2547  * the end of the whole mess, but we never know exactly how much data has
2548  * been uploaded, so salvaging the frame is hard.
2549  *
2550  * There is only one way to do it reliably, and it's disgusting.
2551  * Here's what we know:
2552  *
2553  * - We know there will always be somewhere between one and three extra
2554  *   descriptors uploaded.
2555  *
2556  * - We know the desired received frame will always be at the end of the
2557  *   total data upload.
2558  *
2559  * - We know the size of the desired received frame because it will be
2560  *   provided in the length field of the status word in the last descriptor.
2561  *
2562  * Here's what we do:
2563  *
2564  * - When we allocate buffers for the receive ring, we bzero() them.
2565  *   This means that we know that the buffer contents should be all
2566  *   zeros, except for data uploaded by the chip.
2567  *
2568  * - We also force the PNIC chip to upload frames that include the
2569  *   ethernet CRC at the end.
2570  *
2571  * - We gather all of the bogus frame data into a single buffer.
2572  *
2573  * - We then position a pointer at the end of this buffer and scan
2574  *   backwards until we encounter the first non-zero byte of data.
2575  *   This is the end of the received frame. We know we will encounter
2576  *   some data at the end of the frame because the CRC will always be
2577  *   there, so even if the sender transmits a packet of all zeros,
2578  *   we won't be fooled.
2579  *
2580  * - We know the size of the actual received frame, so we subtract
2581  *   that value from the current pointer location. This brings us
2582  *   to the start of the actual received packet.
2583  *
2584  * - We copy this into an mbuf and pass it on, along with the actual
2585  *   frame length.
2586  *
2587  * The performance hit is tremendous, but it beats dropping frames all
2588  * the time.
2589  */
2590
2591 #define DC_WHOLEFRAME   (DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2592 static void
2593 dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
2594 {
2595         struct dc_desc *cur_rx;
2596         struct dc_desc *c = NULL;
2597         struct mbuf *m = NULL;
2598         unsigned char *ptr;
2599         int i, total_len;
2600         uint32_t rxstat = 0;
2601
2602         i = sc->dc_pnic_rx_bug_save;
2603         cur_rx = &sc->dc_ldata.dc_rx_list[idx];
2604         ptr = sc->dc_pnic_rx_buf;
2605         bzero(ptr, DC_RXLEN * 5);
2606
2607         /* Copy all the bytes from the bogus buffers. */
2608         while (1) {
2609                 c = &sc->dc_ldata.dc_rx_list[i];
2610                 rxstat = le32toh(c->dc_status);
2611                 m = sc->dc_cdata.dc_rx_chain[i];
2612                 bcopy(mtod(m, char *), ptr, DC_RXLEN);
2613                 ptr += DC_RXLEN;
2614                 /* If this is the last buffer, break out. */
2615                 if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
2616                         break;
2617                 dc_discard_rxbuf(sc, i);
2618                 DC_INC(i, DC_RX_LIST_CNT);
2619         }
2620
2621         /* Find the length of the actual receive frame. */
2622         total_len = DC_RXBYTES(rxstat);
2623
2624         /* Scan backwards until we hit a non-zero byte. */
2625         while (*ptr == 0x00)
2626                 ptr--;
2627
2628         /* Round off. */
2629         if ((uintptr_t)(ptr) & 0x3)
2630                 ptr -= 1;
2631
2632         /* Now find the start of the frame. */
2633         ptr -= total_len;
2634         if (ptr < sc->dc_pnic_rx_buf)
2635                 ptr = sc->dc_pnic_rx_buf;
2636
2637         /*
2638          * Now copy the salvaged frame to the last mbuf and fake up
2639          * the status word to make it look like a successful
2640          * frame reception.
2641          */
2642         bcopy(ptr, mtod(m, char *), total_len);
2643         cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
2644 }
2645
2646 /*
2647  * This routine searches the RX ring for dirty descriptors in the
2648  * event that the rxeof routine falls out of sync with the chip's
2649  * current descriptor pointer. This may happen sometimes as a result
2650  * of a "no RX buffer available" condition that happens when the chip
2651  * consumes all of the RX buffers before the driver has a chance to
2652  * process the RX ring. This routine may need to be called more than
2653  * once to bring the driver back in sync with the chip, however we
2654  * should still be getting RX DONE interrupts to drive the search
2655  * for new packets in the RX ring, so we should catch up eventually.
2656  */
2657 static int
2658 dc_rx_resync(struct dc_softc *sc)
2659 {
2660         struct dc_desc *cur_rx;
2661         int i, pos;
2662
2663         pos = sc->dc_cdata.dc_rx_prod;
2664
2665         for (i = 0; i < DC_RX_LIST_CNT; i++) {
2666                 cur_rx = &sc->dc_ldata.dc_rx_list[pos];
2667                 if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
2668                         break;
2669                 DC_INC(pos, DC_RX_LIST_CNT);
2670         }
2671
2672         /* If the ring really is empty, then just return. */
2673         if (i == DC_RX_LIST_CNT)
2674                 return (0);
2675
2676         /* We've fallen behing the chip: catch it. */
2677         sc->dc_cdata.dc_rx_prod = pos;
2678
2679         return (EAGAIN);
2680 }
2681
2682 static void
2683 dc_discard_rxbuf(struct dc_softc *sc, int i)
2684 {
2685         struct mbuf *m;
2686
2687         if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
2688                 m = sc->dc_cdata.dc_rx_chain[i];
2689                 bzero(mtod(m, char *), m->m_len);
2690         }
2691
2692         sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
2693         sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
2694         bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_PREREAD |
2695             BUS_DMASYNC_PREWRITE);
2696 }
2697
2698 /*
2699  * A frame has been uploaded: pass the resulting mbuf chain up to
2700  * the higher level protocols.
2701  */
2702 static int
2703 dc_rxeof(struct dc_softc *sc)
2704 {
2705         struct mbuf *m;
2706         struct ifnet *ifp;
2707         struct dc_desc *cur_rx;
2708         int i, total_len, rx_npkts;
2709         uint32_t rxstat;
2710
2711         DC_LOCK_ASSERT(sc);
2712
2713         ifp = sc->dc_ifp;
2714         rx_npkts = 0;
2715
2716         bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_POSTREAD |
2717             BUS_DMASYNC_POSTWRITE);
2718         for (i = sc->dc_cdata.dc_rx_prod;
2719             (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
2720             DC_INC(i, DC_RX_LIST_CNT)) {
2721 #ifdef DEVICE_POLLING
2722                 if (ifp->if_capenable & IFCAP_POLLING) {
2723                         if (sc->rxcycles <= 0)
2724                                 break;
2725                         sc->rxcycles--;
2726                 }
2727 #endif
2728                 cur_rx = &sc->dc_ldata.dc_rx_list[i];
2729                 rxstat = le32toh(cur_rx->dc_status);
2730                 if ((rxstat & DC_RXSTAT_OWN) != 0)
2731                         break;
2732                 m = sc->dc_cdata.dc_rx_chain[i];
2733                 bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
2734                     BUS_DMASYNC_POSTREAD);
2735                 total_len = DC_RXBYTES(rxstat);
2736                 rx_npkts++;
2737
2738                 if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
2739                         if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
2740                                 if (rxstat & DC_RXSTAT_FIRSTFRAG)
2741                                         sc->dc_pnic_rx_bug_save = i;
2742                                 if ((rxstat & DC_RXSTAT_LASTFRAG) == 0)
2743                                         continue;
2744                                 dc_pnic_rx_bug_war(sc, i);
2745                                 rxstat = le32toh(cur_rx->dc_status);
2746                                 total_len = DC_RXBYTES(rxstat);
2747                         }
2748                 }
2749
2750                 /*
2751                  * If an error occurs, update stats, clear the
2752                  * status word and leave the mbuf cluster in place:
2753                  * it should simply get re-used next time this descriptor
2754                  * comes up in the ring.  However, don't report long
2755                  * frames as errors since they could be vlans.
2756                  */
2757                 if ((rxstat & DC_RXSTAT_RXERR)) {
2758                         if (!(rxstat & DC_RXSTAT_GIANT) ||
2759                             (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2760                                        DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2761                                        DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
2762                                 ifp->if_ierrors++;
2763                                 if (rxstat & DC_RXSTAT_COLLSEEN)
2764                                         ifp->if_collisions++;
2765                                 dc_discard_rxbuf(sc, i);
2766                                 if (rxstat & DC_RXSTAT_CRCERR)
2767                                         continue;
2768                                 else {
2769                                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2770                                         dc_init_locked(sc);
2771                                         return (rx_npkts);
2772                                 }
2773                         }
2774                 }
2775
2776                 /* No errors; receive the packet. */
2777                 total_len -= ETHER_CRC_LEN;
2778 #ifdef __NO_STRICT_ALIGNMENT
2779                 /*
2780                  * On architectures without alignment problems we try to
2781                  * allocate a new buffer for the receive ring, and pass up
2782                  * the one where the packet is already, saving the expensive
2783                  * copy done in m_devget().
2784                  * If we are on an architecture with alignment problems, or
2785                  * if the allocation fails, then use m_devget and leave the
2786                  * existing buffer in the receive ring.
2787                  */
2788                 if (dc_newbuf(sc, i) != 0) {
2789                         dc_discard_rxbuf(sc, i);
2790                         ifp->if_iqdrops++;
2791                         continue;
2792                 }
2793                 m->m_pkthdr.rcvif = ifp;
2794                 m->m_pkthdr.len = m->m_len = total_len;
2795 #else
2796                 {
2797                         struct mbuf *m0;
2798
2799                         m0 = m_devget(mtod(m, char *), total_len,
2800                                 ETHER_ALIGN, ifp, NULL);
2801                         dc_discard_rxbuf(sc, i);
2802                         if (m0 == NULL) {
2803                                 ifp->if_iqdrops++;
2804                                 continue;
2805                         }
2806                         m = m0;
2807                 }
2808 #endif
2809
2810                 ifp->if_ipackets++;
2811                 DC_UNLOCK(sc);
2812                 (*ifp->if_input)(ifp, m);
2813                 DC_LOCK(sc);
2814         }
2815
2816         sc->dc_cdata.dc_rx_prod = i;
2817         return (rx_npkts);
2818 }
2819
2820 /*
2821  * A frame was downloaded to the chip. It's safe for us to clean up
2822  * the list buffers.
2823  */
2824 static void
2825 dc_txeof(struct dc_softc *sc)
2826 {
2827         struct dc_desc *cur_tx;
2828         struct ifnet *ifp;
2829         int idx, setup;
2830         uint32_t ctl, txstat;
2831
2832         if (sc->dc_cdata.dc_tx_cnt == 0)
2833                 return;
2834
2835         ifp = sc->dc_ifp;
2836
2837         /*
2838          * Go through our tx list and free mbufs for those
2839          * frames that have been transmitted.
2840          */
2841         bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_POSTREAD |
2842             BUS_DMASYNC_POSTWRITE);
2843         setup = 0;
2844         for (idx = sc->dc_cdata.dc_tx_cons; idx != sc->dc_cdata.dc_tx_prod;
2845             DC_INC(idx, DC_TX_LIST_CNT), sc->dc_cdata.dc_tx_cnt--) {
2846                 cur_tx = &sc->dc_ldata.dc_tx_list[idx];
2847                 txstat = le32toh(cur_tx->dc_status);
2848                 ctl = le32toh(cur_tx->dc_ctl);
2849
2850                 if (txstat & DC_TXSTAT_OWN)
2851                         break;
2852
2853                 if (sc->dc_cdata.dc_tx_chain[idx] == NULL)
2854                         continue;
2855
2856                 if (ctl & DC_TXCTL_SETUP) {
2857                         cur_tx->dc_ctl = htole32(ctl & ~DC_TXCTL_SETUP);
2858                         setup++;
2859                         bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
2860                             BUS_DMASYNC_POSTWRITE);
2861                         /*
2862                          * Yes, the PNIC is so brain damaged
2863                          * that it will sometimes generate a TX
2864                          * underrun error while DMAing the RX
2865                          * filter setup frame. If we detect this,
2866                          * we have to send the setup frame again,
2867                          * or else the filter won't be programmed
2868                          * correctly.
2869                          */
2870                         if (DC_IS_PNIC(sc)) {
2871                                 if (txstat & DC_TXSTAT_ERRSUM)
2872                                         dc_setfilt(sc);
2873                         }
2874                         sc->dc_cdata.dc_tx_chain[idx] = NULL;
2875                         continue;
2876                 }
2877
2878                 if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2879                         /*
2880                          * XXX: Why does my Xircom taunt me so?
2881                          * For some reason it likes setting the CARRLOST flag
2882                          * even when the carrier is there. wtf?!?
2883                          * Who knows, but Conexant chips have the
2884                          * same problem. Maybe they took lessons
2885                          * from Xircom.
2886                          */
2887                         if (/*sc->dc_type == DC_TYPE_21143 &&*/
2888                             sc->dc_pmode == DC_PMODE_MII &&
2889                             ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2890                             DC_TXSTAT_NOCARRIER)))
2891                                 txstat &= ~DC_TXSTAT_ERRSUM;
2892                 } else {
2893                         if (/*sc->dc_type == DC_TYPE_21143 &&*/
2894                             sc->dc_pmode == DC_PMODE_MII &&
2895                             ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2896                             DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
2897                                 txstat &= ~DC_TXSTAT_ERRSUM;
2898                 }
2899
2900                 if (txstat & DC_TXSTAT_ERRSUM) {
2901                         ifp->if_oerrors++;
2902                         if (txstat & DC_TXSTAT_EXCESSCOLL)
2903                                 ifp->if_collisions++;
2904                         if (txstat & DC_TXSTAT_LATECOLL)
2905                                 ifp->if_collisions++;
2906                         if (!(txstat & DC_TXSTAT_UNDERRUN)) {
2907                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2908                                 dc_init_locked(sc);
2909                                 return;
2910                         }
2911                 } else
2912                         ifp->if_opackets++;
2913                 ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
2914
2915                 bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
2916                     BUS_DMASYNC_POSTWRITE);
2917                 bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
2918                 m_freem(sc->dc_cdata.dc_tx_chain[idx]);
2919                 sc->dc_cdata.dc_tx_chain[idx] = NULL;
2920         }
2921         sc->dc_cdata.dc_tx_cons = idx;
2922
2923         if (sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
2924                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2925                 if (sc->dc_cdata.dc_tx_cnt == 0)
2926                         sc->dc_wdog_timer = 0;
2927         }
2928         if (setup > 0)
2929                 bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
2930                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2931 }
2932
2933 static void
2934 dc_tick(void *xsc)
2935 {
2936         struct dc_softc *sc;
2937         struct mii_data *mii;
2938         struct ifnet *ifp;
2939         uint32_t r;
2940
2941         sc = xsc;
2942         DC_LOCK_ASSERT(sc);
2943         ifp = sc->dc_ifp;
2944         mii = device_get_softc(sc->dc_miibus);
2945
2946         /*
2947          * Reclaim transmitted frames for controllers that do
2948          * not generate TX completion interrupt for every frame.
2949          */
2950         if (sc->dc_flags & DC_TX_USE_TX_INTR)
2951                 dc_txeof(sc);
2952
2953         if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2954                 if (sc->dc_flags & DC_21143_NWAY) {
2955                         r = CSR_READ_4(sc, DC_10BTSTAT);
2956                         if (IFM_SUBTYPE(mii->mii_media_active) ==
2957                             IFM_100_TX && (r & DC_TSTAT_LS100)) {
2958                                 sc->dc_link = 0;
2959                                 mii_mediachg(mii);
2960                         }
2961                         if (IFM_SUBTYPE(mii->mii_media_active) ==
2962                             IFM_10_T && (r & DC_TSTAT_LS10)) {
2963                                 sc->dc_link = 0;
2964                                 mii_mediachg(mii);
2965                         }
2966                         if (sc->dc_link == 0)
2967                                 mii_tick(mii);
2968                 } else {
2969                         /*
2970                          * For NICs which never report DC_RXSTATE_WAIT, we
2971                          * have to bite the bullet...
2972                          */
2973                         if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
2974                             DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
2975                             sc->dc_cdata.dc_tx_cnt == 0)
2976                                 mii_tick(mii);
2977                 }
2978         } else
2979                 mii_tick(mii);
2980
2981         /*
2982          * When the init routine completes, we expect to be able to send
2983          * packets right away, and in fact the network code will send a
2984          * gratuitous ARP the moment the init routine marks the interface
2985          * as running. However, even though the MAC may have been initialized,
2986          * there may be a delay of a few seconds before the PHY completes
2987          * autonegotiation and the link is brought up. Any transmissions
2988          * made during that delay will be lost. Dealing with this is tricky:
2989          * we can't just pause in the init routine while waiting for the
2990          * PHY to come ready since that would bring the whole system to
2991          * a screeching halt for several seconds.
2992          *
2993          * What we do here is prevent the TX start routine from sending
2994          * any packets until a link has been established. After the
2995          * interface has been initialized, the tick routine will poll
2996          * the state of the PHY until the IFM_ACTIVE flag is set. Until
2997          * that time, packets will stay in the send queue, and once the
2998          * link comes up, they will be flushed out to the wire.
2999          */
3000         if (sc->dc_link != 0 && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3001                 dc_start_locked(ifp);
3002
3003         if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
3004                 callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3005         else
3006                 callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3007 }
3008
3009 /*
3010  * A transmit underrun has occurred.  Back off the transmit threshold,
3011  * or switch to store and forward mode if we have to.
3012  */
3013 static void
3014 dc_tx_underrun(struct dc_softc *sc)
3015 {
3016         uint32_t netcfg, isr;
3017         int i, reinit;
3018
3019         reinit = 0;
3020         netcfg = CSR_READ_4(sc, DC_NETCFG);
3021         device_printf(sc->dc_dev, "TX underrun -- ");
3022         if ((sc->dc_flags & DC_TX_STORENFWD) == 0) {
3023                 if (sc->dc_txthresh + DC_TXTHRESH_INC > DC_TXTHRESH_MAX) {
3024                         printf("using store and forward mode\n");
3025                         netcfg |= DC_NETCFG_STORENFWD;
3026                 } else {
3027                         printf("increasing TX threshold\n");
3028                         sc->dc_txthresh += DC_TXTHRESH_INC;
3029                         netcfg &= ~DC_NETCFG_TX_THRESH;
3030                         netcfg |= sc->dc_txthresh;
3031                 }
3032
3033                 if (DC_IS_INTEL(sc)) {
3034                         /*
3035                          * The real 21143 requires that the transmitter be idle
3036                          * in order to change the transmit threshold or store
3037                          * and forward state.
3038                          */
3039                         CSR_WRITE_4(sc, DC_NETCFG, netcfg & ~DC_NETCFG_TX_ON);
3040
3041                         for (i = 0; i < DC_TIMEOUT; i++) {
3042                                 isr = CSR_READ_4(sc, DC_ISR);
3043                                 if (isr & DC_ISR_TX_IDLE)
3044                                         break;
3045                                 DELAY(10);
3046                         }
3047                         if (i == DC_TIMEOUT) {
3048                                 device_printf(sc->dc_dev,
3049                                     "%s: failed to force tx to idle state\n",
3050                                     __func__);
3051                                 reinit++;
3052                         }
3053                 }
3054         } else {
3055                 printf("resetting\n");
3056                 reinit++;
3057         }
3058
3059         if (reinit == 0) {
3060                 CSR_WRITE_4(sc, DC_NETCFG, netcfg);
3061                 if (DC_IS_INTEL(sc))
3062                         CSR_WRITE_4(sc, DC_NETCFG, netcfg | DC_NETCFG_TX_ON);
3063         } else {
3064                 sc->dc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3065                 dc_init_locked(sc);
3066         }
3067 }
3068
3069 #ifdef DEVICE_POLLING
3070 static poll_handler_t dc_poll;
3071
3072 static int
3073 dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3074 {
3075         struct dc_softc *sc = ifp->if_softc;
3076         int rx_npkts = 0;
3077
3078         DC_LOCK(sc);
3079
3080         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3081                 DC_UNLOCK(sc);
3082                 return (rx_npkts);
3083         }
3084
3085         sc->rxcycles = count;
3086         rx_npkts = dc_rxeof(sc);
3087         dc_txeof(sc);
3088         if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
3089             !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3090                 dc_start_locked(ifp);
3091
3092         if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3093                 uint32_t        status;
3094
3095                 status = CSR_READ_4(sc, DC_ISR);
3096                 status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3097                         DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3098                         DC_ISR_BUS_ERR);
3099                 if (!status) {
3100                         DC_UNLOCK(sc);
3101                         return (rx_npkts);
3102                 }
3103                 /* ack what we have */
3104                 CSR_WRITE_4(sc, DC_ISR, status);
3105
3106                 if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3107                         uint32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3108                         ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3109
3110                         if (dc_rx_resync(sc))
3111                                 dc_rxeof(sc);
3112                 }
3113                 /* restart transmit unit if necessary */
3114                 if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3115                         CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3116
3117                 if (status & DC_ISR_TX_UNDERRUN)
3118                         dc_tx_underrun(sc);
3119
3120                 if (status & DC_ISR_BUS_ERR) {
3121                         if_printf(ifp, "%s: bus error\n", __func__);
3122                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3123                         dc_init_locked(sc);
3124                 }
3125         }
3126         DC_UNLOCK(sc);
3127         return (rx_npkts);
3128 }
3129 #endif /* DEVICE_POLLING */
3130
3131 static void
3132 dc_intr(void *arg)
3133 {
3134         struct dc_softc *sc;
3135         struct ifnet *ifp;
3136         uint32_t r, status;
3137         int n;
3138
3139         sc = arg;
3140
3141         if (sc->suspended)
3142                 return;
3143
3144         DC_LOCK(sc);
3145         status = CSR_READ_4(sc, DC_ISR);
3146         if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0) {
3147                 DC_UNLOCK(sc);
3148                 return;
3149         }
3150         ifp = sc->dc_ifp;
3151 #ifdef DEVICE_POLLING
3152         if (ifp->if_capenable & IFCAP_POLLING) {
3153                 DC_UNLOCK(sc);
3154                 return;
3155         }
3156 #endif
3157         /* Disable interrupts. */
3158         CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3159
3160         for (n = 16; n > 0; n--) {
3161                 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3162                         break;
3163                 /* Ack interrupts. */
3164                 CSR_WRITE_4(sc, DC_ISR, status);
3165
3166                 if (status & DC_ISR_RX_OK) {
3167                         if (dc_rxeof(sc) == 0) {
3168                                 while (dc_rx_resync(sc))
3169                                         dc_rxeof(sc);
3170                         }
3171                 }
3172
3173                 if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
3174                         dc_txeof(sc);
3175
3176                 if (status & DC_ISR_TX_IDLE) {
3177                         dc_txeof(sc);
3178                         if (sc->dc_cdata.dc_tx_cnt) {
3179                                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
3180                                 CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3181                         }
3182                 }
3183
3184                 if (status & DC_ISR_TX_UNDERRUN)
3185                         dc_tx_underrun(sc);
3186
3187                 if ((status & DC_ISR_RX_WATDOGTIMEO)
3188                     || (status & DC_ISR_RX_NOBUF)) {
3189                         r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3190                         ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3191                         if (dc_rxeof(sc) == 0) {
3192                                 while (dc_rx_resync(sc))
3193                                         dc_rxeof(sc);
3194                         }
3195                 }
3196
3197                 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3198                         dc_start_locked(ifp);
3199
3200                 if (status & DC_ISR_BUS_ERR) {
3201                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3202                         dc_init_locked(sc);
3203                         DC_UNLOCK(sc);
3204                         return;
3205                 }
3206                 status = CSR_READ_4(sc, DC_ISR);
3207                 if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0)
3208                         break;
3209         }
3210
3211         /* Re-enable interrupts. */
3212         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3213                 CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
3214
3215         DC_UNLOCK(sc);
3216 }
3217
3218 /*
3219  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
3220  * pointers to the fragment pointers.
3221  */
3222 static int
3223 dc_encap(struct dc_softc *sc, struct mbuf **m_head)
3224 {
3225         bus_dma_segment_t segs[DC_MAXFRAGS];
3226         bus_dmamap_t map;
3227         struct dc_desc *f;
3228         struct mbuf *m;
3229         int cur, defragged, error, first, frag, i, idx, nseg;
3230
3231         m = NULL;
3232         defragged = 0;
3233         if (sc->dc_flags & DC_TX_COALESCE &&
3234             ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3235                 m = m_defrag(*m_head, M_DONTWAIT);
3236                 defragged = 1;
3237         } else {
3238                 /*
3239                  * Count the number of frags in this chain to see if we
3240                  * need to m_collapse.  Since the descriptor list is shared
3241                  * by all packets, we'll m_collapse long chains so that they
3242                  * do not use up the entire list, even if they would fit.
3243                  */
3244                 i = 0;
3245                 for (m = *m_head; m != NULL; m = m->m_next)
3246                         i++;
3247                 if (i > DC_TX_LIST_CNT / 4 ||
3248                     DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3249                     DC_TX_LIST_RSVD) {
3250                         m = m_collapse(*m_head, M_DONTWAIT, DC_MAXFRAGS);
3251                         defragged = 1;
3252                 }
3253         }
3254         if (defragged != 0) {
3255                 if (m == NULL) {
3256                         m_freem(*m_head);
3257                         *m_head = NULL;
3258                         return (ENOBUFS);
3259                 }
3260                 *m_head = m;
3261         }
3262
3263         idx = sc->dc_cdata.dc_tx_prod;
3264         error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3265             sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3266         if (error == EFBIG) {
3267                 if (defragged != 0 || (m = m_collapse(*m_head, M_DONTWAIT,
3268                     DC_MAXFRAGS)) == NULL) {
3269                         m_freem(*m_head);
3270                         *m_head = NULL;
3271                         return (defragged != 0 ? error : ENOBUFS);
3272                 }
3273                 *m_head = m;
3274                 error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3275                     sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3276                 if (error != 0) {
3277                         m_freem(*m_head);
3278                         *m_head = NULL;
3279                         return (error);
3280                 }
3281         } else if (error != 0)
3282                 return (error);
3283         KASSERT(nseg <= DC_MAXFRAGS,
3284             ("%s: wrong number of segments (%d)", __func__, nseg));
3285         if (nseg == 0) {
3286                 m_freem(*m_head);
3287                 *m_head = NULL;
3288                 return (EIO);
3289         }
3290
3291         /* Check descriptor overruns. */
3292         if (sc->dc_cdata.dc_tx_cnt + nseg > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
3293                 bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
3294                 return (ENOBUFS);
3295         }
3296         bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
3297             BUS_DMASYNC_PREWRITE);
3298
3299         first = cur = frag = sc->dc_cdata.dc_tx_prod;
3300         for (i = 0; i < nseg; i++) {
3301                 if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3302                     (frag == (DC_TX_LIST_CNT - 1)) &&
3303                     (first != sc->dc_cdata.dc_tx_first)) {
3304                         bus_dmamap_unload(sc->dc_tx_mtag,
3305                             sc->dc_cdata.dc_tx_map[first]);
3306                         m_freem(*m_head);
3307                         *m_head = NULL;
3308                         return (ENOBUFS);
3309                 }
3310
3311                 f = &sc->dc_ldata.dc_tx_list[frag];
3312                 f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3313                 if (i == 0) {
3314                         f->dc_status = 0;
3315                         f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3316                 } else
3317                         f->dc_status = htole32(DC_TXSTAT_OWN);
3318                 f->dc_data = htole32(DC_ADDR_LO(segs[i].ds_addr));
3319                 cur = frag;
3320                 DC_INC(frag, DC_TX_LIST_CNT);
3321         }
3322
3323         sc->dc_cdata.dc_tx_prod = frag;
3324         sc->dc_cdata.dc_tx_cnt += nseg;
3325         sc->dc_cdata.dc_tx_chain[cur] = *m_head;
3326         sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3327         if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
3328                 sc->dc_ldata.dc_tx_list[first].dc_ctl |=
3329                     htole32(DC_TXCTL_FINT);
3330         if (sc->dc_flags & DC_TX_INTR_ALWAYS)
3331                 sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3332         if (sc->dc_flags & DC_TX_USE_TX_INTR &&
3333             ++sc->dc_cdata.dc_tx_pkts >= 8) {
3334                 sc->dc_cdata.dc_tx_pkts = 0;
3335                 sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3336         }
3337         sc->dc_ldata.dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3338
3339         bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
3340             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3341
3342         /*
3343          * Swap the last and the first dmamaps to ensure the map for
3344          * this transmission is placed at the last descriptor.
3345          */
3346         map = sc->dc_cdata.dc_tx_map[cur];
3347         sc->dc_cdata.dc_tx_map[cur] = sc->dc_cdata.dc_tx_map[first];
3348         sc->dc_cdata.dc_tx_map[first] = map;
3349
3350         return (0);
3351 }
3352
3353 static void
3354 dc_start(struct ifnet *ifp)
3355 {
3356         struct dc_softc *sc;
3357
3358         sc = ifp->if_softc;
3359         DC_LOCK(sc);
3360         dc_start_locked(ifp);
3361         DC_UNLOCK(sc);
3362 }
3363
3364 /*
3365  * Main transmit routine
3366  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3367  * regions directly in the transmit lists.  We also save a copy of the
3368  * pointers since the transmit list fragment pointers are physical
3369  * addresses.
3370  */
3371 static void
3372 dc_start_locked(struct ifnet *ifp)
3373 {
3374         struct dc_softc *sc;
3375         struct mbuf *m_head;
3376         int queued;
3377
3378         sc = ifp->if_softc;
3379
3380         DC_LOCK_ASSERT(sc);
3381
3382         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
3383             IFF_DRV_RUNNING || sc->dc_link == 0)
3384                 return;
3385
3386         sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
3387
3388         for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
3389                 /*
3390                  * If there's no way we can send any packets, return now.
3391                  */
3392                 if (sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
3393                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3394                         break;
3395                 }
3396                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
3397                 if (m_head == NULL)
3398                         break;
3399
3400                 if (dc_encap(sc, &m_head)) {
3401                         if (m_head == NULL)
3402                                 break;
3403                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
3404                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3405                         break;
3406                 }
3407
3408                 queued++;
3409                 /*
3410                  * If there's a BPF listener, bounce a copy of this frame
3411                  * to him.
3412                  */
3413                 BPF_MTAP(ifp, m_head);
3414         }
3415
3416         if (queued > 0) {
3417                 /* Transmit */
3418                 if (!(sc->dc_flags & DC_TX_POLL))
3419                         CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3420
3421                 /*
3422                  * Set a timeout in case the chip goes out to lunch.
3423                  */
3424                 sc->dc_wdog_timer = 5;
3425         }
3426 }
3427
3428 static void
3429 dc_init(void *xsc)
3430 {
3431         struct dc_softc *sc = xsc;
3432
3433         DC_LOCK(sc);
3434         dc_init_locked(sc);
3435         DC_UNLOCK(sc);
3436 }
3437
3438 static void
3439 dc_init_locked(struct dc_softc *sc)
3440 {
3441         struct ifnet *ifp = sc->dc_ifp;
3442         struct mii_data *mii;
3443         struct ifmedia *ifm;
3444
3445         DC_LOCK_ASSERT(sc);
3446
3447         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3448                 return;
3449
3450         mii = device_get_softc(sc->dc_miibus);
3451
3452         /*
3453          * Cancel pending I/O and free all RX/TX buffers.
3454          */
3455         dc_stop(sc);
3456         dc_reset(sc);
3457         if (DC_IS_INTEL(sc)) {
3458                 ifm = &mii->mii_media;
3459                 dc_apply_fixup(sc, ifm->ifm_media);
3460         }
3461
3462         /*
3463          * Set cache alignment and burst length.
3464          */
3465         if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
3466                 CSR_WRITE_4(sc, DC_BUSCTL, 0);
3467         else
3468                 CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3469         /*
3470          * Evenly share the bus between receive and transmit process.
3471          */
3472         if (DC_IS_INTEL(sc))
3473                 DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
3474         if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
3475                 DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
3476         } else {
3477                 DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
3478         }
3479         if (sc->dc_flags & DC_TX_POLL)
3480                 DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
3481         switch(sc->dc_cachesize) {
3482         case 32:
3483                 DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
3484                 break;
3485         case 16:
3486                 DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
3487                 break;
3488         case 8:
3489                 DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
3490                 break;
3491         case 0:
3492         default:
3493                 DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
3494                 break;
3495         }
3496
3497         if (sc->dc_flags & DC_TX_STORENFWD)
3498                 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
3499         else {
3500                 if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
3501                         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
3502                 } else {
3503                         DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
3504                         DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
3505                 }
3506         }
3507
3508         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
3509         DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
3510
3511         if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
3512                 /*
3513                  * The app notes for the 98713 and 98715A say that
3514                  * in order to have the chips operate properly, a magic
3515                  * number must be written to CSR16. Macronix does not
3516                  * document the meaning of these bits so there's no way
3517                  * to know exactly what they do. The 98713 has a magic
3518                  * number all its own; the rest all use a different one.
3519                  */
3520                 DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
3521                 if (sc->dc_type == DC_TYPE_98713)
3522                         DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
3523                 else
3524                         DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
3525         }
3526
3527         if (DC_IS_XIRCOM(sc)) {
3528                 /*
3529                  * setup General Purpose Port mode and data so the tulip
3530                  * can talk to the MII.
3531                  */
3532                 CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3533                            DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3534                 DELAY(10);
3535                 CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3536                            DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3537                 DELAY(10);
3538         }
3539
3540         DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3541         DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
3542
3543         /* Init circular RX list. */
3544         if (dc_list_rx_init(sc) == ENOBUFS) {
3545                 device_printf(sc->dc_dev,
3546                     "initialization failed: no memory for rx buffers\n");
3547                 dc_stop(sc);
3548                 return;
3549         }
3550
3551         /*
3552          * Init TX descriptors.
3553          */
3554         dc_list_tx_init(sc);
3555
3556         /*
3557          * Load the address of the RX list.
3558          */
3559         CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
3560         CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
3561
3562         /*
3563          * Enable interrupts.
3564          */
3565 #ifdef DEVICE_POLLING
3566         /*
3567          * ... but only if we are not polling, and make sure they are off in
3568          * the case of polling. Some cards (e.g. fxp) turn interrupts on
3569          * after a reset.
3570          */
3571         if (ifp->if_capenable & IFCAP_POLLING)
3572                 CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3573         else
3574 #endif
3575         CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
3576         CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
3577
3578         /* Enable transmitter. */
3579         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
3580
3581         /*
3582          * If this is an Intel 21143 and we're not using the
3583          * MII port, program the LED control pins so we get
3584          * link and activity indications.
3585          */
3586         if (sc->dc_flags & DC_TULIP_LEDS) {
3587                 CSR_WRITE_4(sc, DC_WATCHDOG,
3588                     DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
3589                 CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3590         }
3591
3592         /*
3593          * Load the RX/multicast filter. We do this sort of late
3594          * because the filter programming scheme on the 21143 and
3595          * some clones requires DMAing a setup frame via the TX
3596          * engine, and we need the transmitter enabled for that.
3597          */
3598         dc_setfilt(sc);
3599
3600         /* Enable receiver. */
3601         DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
3602         CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
3603
3604         ifp->if_drv_flags |= IFF_DRV_RUNNING;
3605         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3606
3607         mii_mediachg(mii);
3608         dc_setcfg(sc, sc->dc_if_media);
3609
3610         /* Clear missed frames and overflow counter. */
3611         CSR_READ_4(sc, DC_FRAMESDISCARDED);
3612
3613         /* Don't start the ticker if this is a homePNA link. */
3614         if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3615                 sc->dc_link = 1;
3616         else {
3617                 if (sc->dc_flags & DC_21143_NWAY)
3618                         callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3619                 else
3620                         callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3621         }
3622
3623         sc->dc_wdog_timer = 0;
3624         callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3625 }
3626
3627 /*
3628  * Set media options.
3629  */
3630 static int
3631 dc_ifmedia_upd(struct ifnet *ifp)
3632 {
3633         struct dc_softc *sc;
3634         struct mii_data *mii;
3635         struct ifmedia *ifm;
3636
3637         sc = ifp->if_softc;
3638         mii = device_get_softc(sc->dc_miibus);
3639         DC_LOCK(sc);
3640         mii_mediachg(mii);
3641         ifm = &mii->mii_media;
3642
3643         if (DC_IS_INTEL(sc))
3644                 dc_setcfg(sc, ifm->ifm_media);
3645         else if (DC_IS_DAVICOM(sc) &&
3646             IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3647                 dc_setcfg(sc, ifm->ifm_media);
3648         else
3649                 sc->dc_link = 0;
3650         DC_UNLOCK(sc);
3651
3652         return (0);
3653 }
3654
3655 /*
3656  * Report current media status.
3657  */
3658 static void
3659 dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3660 {
3661         struct dc_softc *sc;
3662         struct mii_data *mii;
3663         struct ifmedia *ifm;
3664
3665         sc = ifp->if_softc;
3666         mii = device_get_softc(sc->dc_miibus);
3667         DC_LOCK(sc);
3668         mii_pollstat(mii);
3669         ifm = &mii->mii_media;
3670         if (DC_IS_DAVICOM(sc)) {
3671                 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3672                         ifmr->ifm_active = ifm->ifm_media;
3673                         ifmr->ifm_status = 0;
3674                         DC_UNLOCK(sc);
3675                         return;
3676                 }
3677         }
3678         ifmr->ifm_active = mii->mii_media_active;
3679         ifmr->ifm_status = mii->mii_media_status;
3680         DC_UNLOCK(sc);
3681 }
3682
3683 static int
3684 dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3685 {
3686         struct dc_softc *sc = ifp->if_softc;
3687         struct ifreq *ifr = (struct ifreq *)data;
3688         struct mii_data *mii;
3689         int error = 0;
3690
3691         switch (command) {
3692         case SIOCSIFFLAGS:
3693                 DC_LOCK(sc);
3694                 if (ifp->if_flags & IFF_UP) {
3695                         int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
3696                                 (IFF_PROMISC | IFF_ALLMULTI);
3697
3698                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3699                                 if (need_setfilt)
3700                                         dc_setfilt(sc);
3701                         } else {
3702                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3703                                 dc_init_locked(sc);
3704                         }
3705                 } else {
3706                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3707                                 dc_stop(sc);
3708                 }
3709                 sc->dc_if_flags = ifp->if_flags;
3710                 DC_UNLOCK(sc);
3711                 break;
3712         case SIOCADDMULTI:
3713         case SIOCDELMULTI:
3714                 DC_LOCK(sc);
3715                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3716                         dc_setfilt(sc);
3717                 DC_UNLOCK(sc);
3718                 break;
3719         case SIOCGIFMEDIA:
3720         case SIOCSIFMEDIA:
3721                 mii = device_get_softc(sc->dc_miibus);
3722                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3723                 break;
3724         case SIOCSIFCAP:
3725 #ifdef DEVICE_POLLING
3726                 if (ifr->ifr_reqcap & IFCAP_POLLING &&
3727                     !(ifp->if_capenable & IFCAP_POLLING)) {
3728                         error = ether_poll_register(dc_poll, ifp);
3729                         if (error)
3730                                 return(error);
3731                         DC_LOCK(sc);
3732                         /* Disable interrupts */
3733                         CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3734                         ifp->if_capenable |= IFCAP_POLLING;
3735                         DC_UNLOCK(sc);
3736                         return (error);
3737                 }
3738                 if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
3739                     ifp->if_capenable & IFCAP_POLLING) {
3740                         error = ether_poll_deregister(ifp);
3741                         /* Enable interrupts. */
3742                         DC_LOCK(sc);
3743                         CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
3744                         ifp->if_capenable &= ~IFCAP_POLLING;
3745                         DC_UNLOCK(sc);
3746                         return (error);
3747                 }
3748 #endif /* DEVICE_POLLING */
3749                 break;
3750         default:
3751                 error = ether_ioctl(ifp, command, data);
3752                 break;
3753         }
3754
3755         return (error);
3756 }
3757
3758 static void
3759 dc_watchdog(void *xsc)
3760 {
3761         struct dc_softc *sc = xsc;
3762         struct ifnet *ifp;
3763
3764         DC_LOCK_ASSERT(sc);
3765
3766         if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3767                 callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3768                 return;
3769         }
3770
3771         ifp = sc->dc_ifp;
3772         ifp->if_oerrors++;
3773         device_printf(sc->dc_dev, "watchdog timeout\n");
3774
3775         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3776         dc_init_locked(sc);
3777
3778         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3779                 dc_start_locked(ifp);
3780 }
3781
3782 /*
3783  * Stop the adapter and free any mbufs allocated to the
3784  * RX and TX lists.
3785  */
3786 static void
3787 dc_stop(struct dc_softc *sc)
3788 {
3789         struct ifnet *ifp;
3790         struct dc_list_data *ld;
3791         struct dc_chain_data *cd;
3792         int i;
3793         uint32_t ctl, netcfg;
3794
3795         DC_LOCK_ASSERT(sc);
3796
3797         ifp = sc->dc_ifp;
3798         ld = &sc->dc_ldata;
3799         cd = &sc->dc_cdata;
3800
3801         callout_stop(&sc->dc_stat_ch);
3802         callout_stop(&sc->dc_wdog_ch);
3803         sc->dc_wdog_timer = 0;
3804         sc->dc_link = 0;
3805
3806         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3807
3808         netcfg = CSR_READ_4(sc, DC_NETCFG);
3809         if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
3810                 CSR_WRITE_4(sc, DC_NETCFG,
3811                    netcfg & ~(DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
3812         CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3813         /* Wait the completion of TX/RX SM. */
3814         if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
3815                 dc_netcfg_wait(sc);
3816
3817         CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
3818         CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
3819
3820         /*
3821          * Free data in the RX lists.
3822          */
3823         for (i = 0; i < DC_RX_LIST_CNT; i++) {
3824                 if (cd->dc_rx_chain[i] != NULL) {
3825                         bus_dmamap_sync(sc->dc_rx_mtag,
3826                             cd->dc_rx_map[i], BUS_DMASYNC_POSTREAD);
3827                         bus_dmamap_unload(sc->dc_rx_mtag,
3828                             cd->dc_rx_map[i]);
3829                         m_freem(cd->dc_rx_chain[i]);
3830                         cd->dc_rx_chain[i] = NULL;
3831                 }
3832         }
3833         bzero(ld->dc_rx_list, DC_RX_LIST_SZ);
3834         bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
3835             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3836
3837         /*
3838          * Free the TX list buffers.
3839          */
3840         for (i = 0; i < DC_TX_LIST_CNT; i++) {
3841                 if (cd->dc_tx_chain[i] != NULL) {
3842                         ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
3843                         if (ctl & DC_TXCTL_SETUP) {
3844                                 bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
3845                                     BUS_DMASYNC_POSTWRITE);
3846                         } else {
3847                                 bus_dmamap_sync(sc->dc_tx_mtag,
3848                                     cd->dc_tx_map[i], BUS_DMASYNC_POSTWRITE);
3849                                 bus_dmamap_unload(sc->dc_tx_mtag,
3850                                     cd->dc_tx_map[i]);
3851                                 m_freem(cd->dc_tx_chain[i]);
3852                         }
3853                         cd->dc_tx_chain[i] = NULL;
3854                 }
3855         }
3856         bzero(ld->dc_tx_list, DC_TX_LIST_SZ);
3857         bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
3858             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3859 }
3860
3861 /*
3862  * Device suspend routine.  Stop the interface and save some PCI
3863  * settings in case the BIOS doesn't restore them properly on
3864  * resume.
3865  */
3866 static int
3867 dc_suspend(device_t dev)
3868 {
3869         struct dc_softc *sc;
3870
3871         sc = device_get_softc(dev);
3872         DC_LOCK(sc);
3873         dc_stop(sc);
3874         sc->suspended = 1;
3875         DC_UNLOCK(sc);
3876
3877         return (0);
3878 }
3879
3880 /*
3881  * Device resume routine.  Restore some PCI settings in case the BIOS
3882  * doesn't, re-enable busmastering, and restart the interface if
3883  * appropriate.
3884  */
3885 static int
3886 dc_resume(device_t dev)
3887 {
3888         struct dc_softc *sc;
3889         struct ifnet *ifp;
3890
3891         sc = device_get_softc(dev);
3892         ifp = sc->dc_ifp;
3893
3894         /* reinitialize interface if necessary */
3895         DC_LOCK(sc);
3896         if (ifp->if_flags & IFF_UP)
3897                 dc_init_locked(sc);
3898
3899         sc->suspended = 0;
3900         DC_UNLOCK(sc);
3901
3902         return (0);
3903 }
3904
3905 /*
3906  * Stop all chip I/O so that the kernel's probe routines don't
3907  * get confused by errant DMAs when rebooting.
3908  */
3909 static int
3910 dc_shutdown(device_t dev)
3911 {
3912         struct dc_softc *sc;
3913
3914         sc = device_get_softc(dev);
3915
3916         DC_LOCK(sc);
3917         dc_stop(sc);
3918         DC_UNLOCK(sc);
3919
3920         return (0);
3921 }
3922
3923 static int
3924 dc_check_multiport(struct dc_softc *sc)
3925 {
3926         struct dc_softc *dsc;
3927         devclass_t dc;
3928         device_t child;
3929         uint8_t *eaddr;
3930         int unit;
3931
3932         dc = devclass_find("dc");
3933         for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
3934                 child = devclass_get_device(dc, unit);
3935                 if (child == NULL)
3936                         continue;
3937                 if (child == sc->dc_dev)
3938                         continue;
3939                 if (device_get_parent(child) != device_get_parent(sc->dc_dev))
3940                         continue;
3941                 if (unit > device_get_unit(sc->dc_dev))
3942                         continue;
3943                 if (device_is_attached(child) == 0)
3944                         continue;
3945                 dsc = device_get_softc(child);
3946                 device_printf(sc->dc_dev,
3947                     "Using station address of %s as base\n",
3948                     device_get_nameunit(child));
3949                 bcopy(dsc->dc_eaddr, sc->dc_eaddr, ETHER_ADDR_LEN);
3950                 eaddr = (uint8_t *)sc->dc_eaddr;
3951                 eaddr[5]++;
3952                 /* Prepare SROM to parse again. */
3953                 if (DC_IS_INTEL(sc) && dsc->dc_srom != NULL &&
3954                     sc->dc_romwidth != 0) {
3955                         free(sc->dc_srom, M_DEVBUF);
3956                         sc->dc_romwidth = dsc->dc_romwidth;
3957                         sc->dc_srom = malloc(DC_ROM_SIZE(sc->dc_romwidth),
3958                             M_DEVBUF, M_NOWAIT);
3959                         if (sc->dc_srom == NULL) {
3960                                 device_printf(sc->dc_dev,
3961                                     "Could not allocate SROM buffer\n");
3962                                 return (ENOMEM);
3963                         }
3964                         bcopy(dsc->dc_srom, sc->dc_srom,
3965                             DC_ROM_SIZE(sc->dc_romwidth));
3966                 }
3967                 return (0);
3968         }
3969         return (ENOENT);
3970 }