i82557.c revision 1.82 1 /* $NetBSD: i82557.c,v 1.82 2004/03/31 14:48:31 briggs Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1995, David Greenman
42 * Copyright (c) 2001 Jonathan Lemon <jlemon (at) freebsd.org>
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice unmodified, this list of conditions, and the following
50 * disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * Id: if_fxp.c,v 1.113 2001/05/17 23:50:24 jlemon
68 */
69
70 /*
71 * Device driver for the Intel i82557 fast Ethernet controller,
72 * and its successors, the i82558 and i82559.
73 */
74
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: i82557.c,v 1.82 2004/03/31 14:48:31 briggs Exp $");
77
78 #include "bpfilter.h"
79 #include "rnd.h"
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/callout.h>
84 #include <sys/mbuf.h>
85 #include <sys/malloc.h>
86 #include <sys/kernel.h>
87 #include <sys/socket.h>
88 #include <sys/ioctl.h>
89 #include <sys/errno.h>
90 #include <sys/device.h>
91
92 #include <machine/endian.h>
93
94 #include <uvm/uvm_extern.h>
95
96 #if NRND > 0
97 #include <sys/rnd.h>
98 #endif
99
100 #include <net/if.h>
101 #include <net/if_dl.h>
102 #include <net/if_media.h>
103 #include <net/if_ether.h>
104
105 #if NBPFILTER > 0
106 #include <net/bpf.h>
107 #endif
108
109 #include <machine/bus.h>
110 #include <machine/intr.h>
111
112 #include <dev/mii/miivar.h>
113
114 #include <dev/ic/i82557reg.h>
115 #include <dev/ic/i82557var.h>
116
117 #include <dev/microcode/i8255x/rcvbundl.h>
118
119 /*
120 * NOTE! On the Alpha, we have an alignment constraint. The
121 * card DMAs the packet immediately following the RFA. However,
122 * the first thing in the packet is a 14-byte Ethernet header.
123 * This means that the packet is misaligned. To compensate,
124 * we actually offset the RFA 2 bytes into the cluster. This
125 * alignes the packet after the Ethernet header at a 32-bit
126 * boundary. HOWEVER! This means that the RFA is misaligned!
127 */
128 #define RFA_ALIGNMENT_FUDGE 2
129
130 /*
131 * The configuration byte map has several undefined fields which
132 * must be one or must be zero. Set up a template for these bits
133 * only (assuming an i82557 chip), leaving the actual configuration
134 * for fxp_init().
135 *
136 * See the definition of struct fxp_cb_config for the bit definitions.
137 */
138 const u_int8_t fxp_cb_config_template[] = {
139 0x0, 0x0, /* cb_status */
140 0x0, 0x0, /* cb_command */
141 0x0, 0x0, 0x0, 0x0, /* link_addr */
142 0x0, /* 0 */
143 0x0, /* 1 */
144 0x0, /* 2 */
145 0x0, /* 3 */
146 0x0, /* 4 */
147 0x0, /* 5 */
148 0x32, /* 6 */
149 0x0, /* 7 */
150 0x0, /* 8 */
151 0x0, /* 9 */
152 0x6, /* 10 */
153 0x0, /* 11 */
154 0x0, /* 12 */
155 0x0, /* 13 */
156 0xf2, /* 14 */
157 0x48, /* 15 */
158 0x0, /* 16 */
159 0x40, /* 17 */
160 0xf0, /* 18 */
161 0x0, /* 19 */
162 0x3f, /* 20 */
163 0x5, /* 21 */
164 0x0, /* 22 */
165 0x0, /* 23 */
166 0x0, /* 24 */
167 0x0, /* 25 */
168 0x0, /* 26 */
169 0x0, /* 27 */
170 0x0, /* 28 */
171 0x0, /* 29 */
172 0x0, /* 30 */
173 0x0, /* 31 */
174 };
175
176 void fxp_mii_initmedia(struct fxp_softc *);
177 int fxp_mii_mediachange(struct ifnet *);
178 void fxp_mii_mediastatus(struct ifnet *, struct ifmediareq *);
179
180 void fxp_80c24_initmedia(struct fxp_softc *);
181 int fxp_80c24_mediachange(struct ifnet *);
182 void fxp_80c24_mediastatus(struct ifnet *, struct ifmediareq *);
183
184 void fxp_start(struct ifnet *);
185 int fxp_ioctl(struct ifnet *, u_long, caddr_t);
186 void fxp_watchdog(struct ifnet *);
187 int fxp_init(struct ifnet *);
188 void fxp_stop(struct ifnet *, int);
189
190 void fxp_txintr(struct fxp_softc *);
191 void fxp_rxintr(struct fxp_softc *);
192
193 int fxp_rx_hwcksum(struct mbuf *, const struct fxp_rfa *);
194
195 void fxp_rxdrain(struct fxp_softc *);
196 int fxp_add_rfabuf(struct fxp_softc *, bus_dmamap_t, int);
197 int fxp_mdi_read(struct device *, int, int);
198 void fxp_statchg(struct device *);
199 void fxp_mdi_write(struct device *, int, int, int);
200 void fxp_autosize_eeprom(struct fxp_softc*);
201 void fxp_read_eeprom(struct fxp_softc *, u_int16_t *, int, int);
202 void fxp_write_eeprom(struct fxp_softc *, u_int16_t *, int, int);
203 void fxp_eeprom_update_cksum(struct fxp_softc *);
204 void fxp_get_info(struct fxp_softc *, u_int8_t *);
205 void fxp_tick(void *);
206 void fxp_mc_setup(struct fxp_softc *);
207 void fxp_load_ucode(struct fxp_softc *);
208
209 void fxp_shutdown(void *);
210 void fxp_power(int, void *);
211
212 int fxp_copy_small = 0;
213
214 /*
215 * Variables for interrupt mitigating microcode.
216 */
217 int fxp_int_delay = 1000; /* usec */
218 int fxp_bundle_max = 6; /* packets */
219
220 struct fxp_phytype {
221 int fp_phy; /* type of PHY, -1 for MII at the end. */
222 void (*fp_init)(struct fxp_softc *);
223 } fxp_phytype_table[] = {
224 { FXP_PHY_80C24, fxp_80c24_initmedia },
225 { -1, fxp_mii_initmedia },
226 };
227
228 /*
229 * Set initial transmit threshold at 64 (512 bytes). This is
230 * increased by 64 (512 bytes) at a time, to maximum of 192
231 * (1536 bytes), if an underrun occurs.
232 */
233 static int tx_threshold = 64;
234
235 /*
236 * Wait for the previous command to be accepted (but not necessarily
237 * completed).
238 */
239 static __inline void
240 fxp_scb_wait(struct fxp_softc *sc)
241 {
242 int i = 10000;
243
244 while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
245 delay(2);
246 if (i == 0)
247 printf("%s: WARNING: SCB timed out!\n", sc->sc_dev.dv_xname);
248 }
249
250 /*
251 * Submit a command to the i82557.
252 */
253 static __inline void
254 fxp_scb_cmd(struct fxp_softc *sc, u_int8_t cmd)
255 {
256
257 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
258 }
259
260 /*
261 * Finish attaching an i82557 interface. Called by bus-specific front-end.
262 */
263 void
264 fxp_attach(struct fxp_softc *sc)
265 {
266 u_int8_t enaddr[ETHER_ADDR_LEN];
267 struct ifnet *ifp;
268 bus_dma_segment_t seg;
269 int rseg, i, error;
270 struct fxp_phytype *fp;
271
272 callout_init(&sc->sc_callout);
273
274 /*
275 * Enable some good stuff on i82558 and later.
276 */
277 if (sc->sc_rev >= FXP_REV_82558_A4) {
278 /* Enable the extended TxCB. */
279 sc->sc_flags |= FXPF_EXT_TXCB;
280 }
281
282 /*
283 * Enable use of extended RFDs and TCBs for 82550
284 * and later chips. Note: we need extended TXCB support
285 * too, but that's already enabled by the code above.
286 * Be careful to do this only on the right devices.
287 */
288 if (sc->sc_rev == FXP_REV_82550 || sc->sc_rev == FXP_REV_82550_C) {
289 sc->sc_flags |= FXPF_EXT_RFA | FXPF_IPCB;
290 sc->sc_txcmd = htole16(FXP_CB_COMMAND_IPCBXMIT);
291 } else {
292 sc->sc_txcmd = htole16(FXP_CB_COMMAND_XMIT);
293 }
294
295 sc->sc_rfa_size =
296 (sc->sc_flags & FXPF_EXT_RFA) ? RFA_EXT_SIZE : RFA_SIZE;
297
298 /*
299 * Allocate the control data structures, and create and load the
300 * DMA map for it.
301 */
302 if ((error = bus_dmamem_alloc(sc->sc_dmat,
303 sizeof(struct fxp_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
304 0)) != 0) {
305 aprint_error(
306 "%s: unable to allocate control data, error = %d\n",
307 sc->sc_dev.dv_xname, error);
308 goto fail_0;
309 }
310
311 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
312 sizeof(struct fxp_control_data), (caddr_t *)&sc->sc_control_data,
313 BUS_DMA_COHERENT)) != 0) {
314 aprint_error("%s: unable to map control data, error = %d\n",
315 sc->sc_dev.dv_xname, error);
316 goto fail_1;
317 }
318 sc->sc_cdseg = seg;
319 sc->sc_cdnseg = rseg;
320
321 memset(sc->sc_control_data, 0, sizeof(struct fxp_control_data));
322
323 if ((error = bus_dmamap_create(sc->sc_dmat,
324 sizeof(struct fxp_control_data), 1,
325 sizeof(struct fxp_control_data), 0, 0, &sc->sc_dmamap)) != 0) {
326 aprint_error("%s: unable to create control data DMA map, "
327 "error = %d\n", sc->sc_dev.dv_xname, error);
328 goto fail_2;
329 }
330
331 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
332 sc->sc_control_data, sizeof(struct fxp_control_data), NULL,
333 0)) != 0) {
334 aprint_error(
335 "%s: can't load control data DMA map, error = %d\n",
336 sc->sc_dev.dv_xname, error);
337 goto fail_3;
338 }
339
340 /*
341 * Create the transmit buffer DMA maps.
342 */
343 for (i = 0; i < FXP_NTXCB; i++) {
344 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
345 (sc->sc_flags & FXPF_IPCB) ? FXP_IPCB_NTXSEG : FXP_NTXSEG,
346 MCLBYTES, 0, 0, &FXP_DSTX(sc, i)->txs_dmamap)) != 0) {
347 aprint_error("%s: unable to create tx DMA map %d, "
348 "error = %d\n", sc->sc_dev.dv_xname, i, error);
349 goto fail_4;
350 }
351 }
352
353 /*
354 * Create the receive buffer DMA maps.
355 */
356 for (i = 0; i < FXP_NRFABUFS; i++) {
357 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
358 MCLBYTES, 0, 0, &sc->sc_rxmaps[i])) != 0) {
359 aprint_error("%s: unable to create rx DMA map %d, "
360 "error = %d\n", sc->sc_dev.dv_xname, i, error);
361 goto fail_5;
362 }
363 }
364
365 /* Initialize MAC address and media structures. */
366 fxp_get_info(sc, enaddr);
367
368 aprint_normal("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
369 ether_sprintf(enaddr));
370
371 ifp = &sc->sc_ethercom.ec_if;
372
373 /*
374 * Get info about our media interface, and initialize it. Note
375 * the table terminates itself with a phy of -1, indicating
376 * that we're using MII.
377 */
378 for (fp = fxp_phytype_table; fp->fp_phy != -1; fp++)
379 if (fp->fp_phy == sc->phy_primary_device)
380 break;
381 (*fp->fp_init)(sc);
382
383 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
384 ifp->if_softc = sc;
385 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
386 ifp->if_ioctl = fxp_ioctl;
387 ifp->if_start = fxp_start;
388 ifp->if_watchdog = fxp_watchdog;
389 ifp->if_init = fxp_init;
390 ifp->if_stop = fxp_stop;
391 IFQ_SET_READY(&ifp->if_snd);
392
393 if (sc->sc_flags & FXPF_IPCB) {
394 KASSERT(sc->sc_flags & FXPF_EXT_RFA); /* we have both or none */
395 /*
396 * IFCAP_CSUM_IPv4 seems to have a problem,
397 * at least, on i82550 rev.12.
398 * specifically, it doesn't calculate ipv4 checksum correctly
399 * when sending 20 byte ipv4 header + 1 or 2 byte data.
400 * FreeBSD driver has related comments.
401 *
402 * XXX we should have separate IFCAP flags
403 * for transmit and receive.
404 */
405 ifp->if_capabilities =
406 /*IFCAP_CSUM_IPv4 |*/ IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
407 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
408 }
409
410 /*
411 * We can support 802.1Q VLAN-sized frames.
412 */
413 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
414
415 /*
416 * Attach the interface.
417 */
418 if_attach(ifp);
419 ether_ifattach(ifp, enaddr);
420 #if NRND > 0
421 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
422 RND_TYPE_NET, 0);
423 #endif
424
425 #ifdef FXP_EVENT_COUNTERS
426 evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
427 NULL, sc->sc_dev.dv_xname, "txstall");
428 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
429 NULL, sc->sc_dev.dv_xname, "txintr");
430 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
431 NULL, sc->sc_dev.dv_xname, "rxintr");
432 #endif /* FXP_EVENT_COUNTERS */
433
434 /*
435 * Add shutdown hook so that DMA is disabled prior to reboot. Not
436 * doing do could allow DMA to corrupt kernel memory during the
437 * reboot before the driver initializes.
438 */
439 sc->sc_sdhook = shutdownhook_establish(fxp_shutdown, sc);
440 if (sc->sc_sdhook == NULL)
441 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
442 sc->sc_dev.dv_xname);
443 /*
444 * Add suspend hook, for similar reasons..
445 */
446 sc->sc_powerhook = powerhook_establish(fxp_power, sc);
447 if (sc->sc_powerhook == NULL)
448 aprint_error("%s: WARNING: unable to establish power hook\n",
449 sc->sc_dev.dv_xname);
450
451 /* The attach is successful. */
452 sc->sc_flags |= FXPF_ATTACHED;
453
454 return;
455
456 /*
457 * Free any resources we've allocated during the failed attach
458 * attempt. Do this in reverse order and fall though.
459 */
460 fail_5:
461 for (i = 0; i < FXP_NRFABUFS; i++) {
462 if (sc->sc_rxmaps[i] != NULL)
463 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
464 }
465 fail_4:
466 for (i = 0; i < FXP_NTXCB; i++) {
467 if (FXP_DSTX(sc, i)->txs_dmamap != NULL)
468 bus_dmamap_destroy(sc->sc_dmat,
469 FXP_DSTX(sc, i)->txs_dmamap);
470 }
471 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
472 fail_3:
473 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
474 fail_2:
475 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
476 sizeof(struct fxp_control_data));
477 fail_1:
478 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
479 fail_0:
480 return;
481 }
482
483 void
484 fxp_mii_initmedia(struct fxp_softc *sc)
485 {
486 int flags;
487
488 sc->sc_flags |= FXPF_MII;
489
490 sc->sc_mii.mii_ifp = &sc->sc_ethercom.ec_if;
491 sc->sc_mii.mii_readreg = fxp_mdi_read;
492 sc->sc_mii.mii_writereg = fxp_mdi_write;
493 sc->sc_mii.mii_statchg = fxp_statchg;
494 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, fxp_mii_mediachange,
495 fxp_mii_mediastatus);
496
497 flags = MIIF_NOISOLATE;
498 if (sc->sc_rev >= FXP_REV_82558_A4)
499 flags |= MIIF_DOPAUSE;
500 /*
501 * The i82557 wedges if all of its PHYs are isolated!
502 */
503 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
504 MII_OFFSET_ANY, flags);
505 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
506 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
507 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
508 } else
509 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
510 }
511
512 void
513 fxp_80c24_initmedia(struct fxp_softc *sc)
514 {
515
516 /*
517 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
518 * doesn't have a programming interface of any sort. The
519 * media is sensed automatically based on how the link partner
520 * is configured. This is, in essence, manual configuration.
521 */
522 aprint_normal("%s: Seeq 80c24 AutoDUPLEX media interface present\n",
523 sc->sc_dev.dv_xname);
524 ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_80c24_mediachange,
525 fxp_80c24_mediastatus);
526 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
527 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
528 }
529
530 /*
531 * Device shutdown routine. Called at system shutdown after sync. The
532 * main purpose of this routine is to shut off receiver DMA so that
533 * kernel memory doesn't get clobbered during warmboot.
534 */
535 void
536 fxp_shutdown(void *arg)
537 {
538 struct fxp_softc *sc = arg;
539
540 /*
541 * Since the system's going to halt shortly, don't bother
542 * freeing mbufs.
543 */
544 fxp_stop(&sc->sc_ethercom.ec_if, 0);
545 }
546 /*
547 * Power handler routine. Called when the system is transitioning
548 * into/out of power save modes. As with fxp_shutdown, the main
549 * purpose of this routine is to shut off receiver DMA so it doesn't
550 * clobber kernel memory at the wrong time.
551 */
552 void
553 fxp_power(int why, void *arg)
554 {
555 struct fxp_softc *sc = arg;
556 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
557 int s;
558
559 s = splnet();
560 switch (why) {
561 case PWR_SUSPEND:
562 case PWR_STANDBY:
563 fxp_stop(ifp, 0);
564 break;
565 case PWR_RESUME:
566 if (ifp->if_flags & IFF_UP)
567 fxp_init(ifp);
568 break;
569 case PWR_SOFTSUSPEND:
570 case PWR_SOFTSTANDBY:
571 case PWR_SOFTRESUME:
572 break;
573 }
574 splx(s);
575 }
576
577 /*
578 * Initialize the interface media.
579 */
580 void
581 fxp_get_info(struct fxp_softc *sc, u_int8_t *enaddr)
582 {
583 u_int16_t data, myea[ETHER_ADDR_LEN / 2];
584
585 /*
586 * Reset to a stable state.
587 */
588 CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
589 DELAY(100);
590
591 sc->sc_eeprom_size = 0;
592 fxp_autosize_eeprom(sc);
593 if (sc->sc_eeprom_size == 0) {
594 aprint_error("%s: failed to detect EEPROM size\n",
595 sc->sc_dev.dv_xname);
596 sc->sc_eeprom_size = 6; /* XXX panic here? */
597 }
598 #ifdef DEBUG
599 aprint_debug("%s: detected %d word EEPROM\n",
600 sc->sc_dev.dv_xname, 1 << sc->sc_eeprom_size);
601 #endif
602
603 /*
604 * Get info about the primary PHY
605 */
606 fxp_read_eeprom(sc, &data, 6, 1);
607 sc->phy_primary_device =
608 (data & FXP_PHY_DEVICE_MASK) >> FXP_PHY_DEVICE_SHIFT;
609
610 /*
611 * Read MAC address.
612 */
613 fxp_read_eeprom(sc, myea, 0, 3);
614 enaddr[0] = myea[0] & 0xff;
615 enaddr[1] = myea[0] >> 8;
616 enaddr[2] = myea[1] & 0xff;
617 enaddr[3] = myea[1] >> 8;
618 enaddr[4] = myea[2] & 0xff;
619 enaddr[5] = myea[2] >> 8;
620
621 /*
622 * Systems based on the ICH2/ICH2-M chip from Intel, as well
623 * as some i82559 designs, have a defect where the chip can
624 * cause a PCI protocol violation if it receives a CU_RESUME
625 * command when it is entering the IDLE state.
626 *
627 * The work-around is to disable Dynamic Standby Mode, so that
628 * the chip never deasserts #CLKRUN, and always remains in the
629 * active state.
630 *
631 * Unfortunately, the only way to disable Dynamic Standby is
632 * to frob an EEPROM setting and reboot (the EEPROM setting
633 * is only consulted when the PCI bus comes out of reset).
634 *
635 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
636 */
637 if (sc->sc_flags & FXPF_HAS_RESUME_BUG) {
638 fxp_read_eeprom(sc, &data, 10, 1);
639 if (data & 0x02) { /* STB enable */
640 aprint_error("%s: WARNING: "
641 "Disabling dynamic standby mode in EEPROM "
642 "to work around a\n",
643 sc->sc_dev.dv_xname);
644 aprint_normal(
645 "%s: WARNING: hardware bug. You must reset "
646 "the system before using this\n",
647 sc->sc_dev.dv_xname);
648 aprint_normal("%s: WARNING: interface.\n",
649 sc->sc_dev.dv_xname);
650 data &= ~0x02;
651 fxp_write_eeprom(sc, &data, 10, 1);
652 aprint_normal("%s: new EEPROM ID: 0x%04x\n",
653 sc->sc_dev.dv_xname, data);
654 fxp_eeprom_update_cksum(sc);
655 }
656 }
657 }
658
659 static void
660 fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int len)
661 {
662 uint16_t reg;
663 int x;
664
665 for (x = 1 << (len - 1); x != 0; x >>= 1) {
666 DELAY(40);
667 if (data & x)
668 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
669 else
670 reg = FXP_EEPROM_EECS;
671 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
672 DELAY(40);
673 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
674 reg | FXP_EEPROM_EESK);
675 DELAY(40);
676 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
677 }
678 DELAY(40);
679 }
680
681 /*
682 * Figure out EEPROM size.
683 *
684 * 559's can have either 64-word or 256-word EEPROMs, the 558
685 * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
686 * talks about the existence of 16 to 256 word EEPROMs.
687 *
688 * The only known sizes are 64 and 256, where the 256 version is used
689 * by CardBus cards to store CIS information.
690 *
691 * The address is shifted in msb-to-lsb, and after the last
692 * address-bit the EEPROM is supposed to output a `dummy zero' bit,
693 * after which follows the actual data. We try to detect this zero, by
694 * probing the data-out bit in the EEPROM control register just after
695 * having shifted in a bit. If the bit is zero, we assume we've
696 * shifted enough address bits. The data-out should be tri-state,
697 * before this, which should translate to a logical one.
698 *
699 * Other ways to do this would be to try to read a register with known
700 * contents with a varying number of address bits, but no such
701 * register seem to be available. The high bits of register 10 are 01
702 * on the 558 and 559, but apparently not on the 557.
703 *
704 * The Linux driver computes a checksum on the EEPROM data, but the
705 * value of this checksum is not very well documented.
706 */
707
708 void
709 fxp_autosize_eeprom(struct fxp_softc *sc)
710 {
711 int x;
712
713 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
714 DELAY(40);
715
716 /* Shift in read opcode. */
717 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
718
719 /*
720 * Shift in address, wait for the dummy zero following a correct
721 * address shift.
722 */
723 for (x = 1; x <= 8; x++) {
724 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
725 DELAY(40);
726 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
727 FXP_EEPROM_EECS | FXP_EEPROM_EESK);
728 DELAY(40);
729 if ((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
730 FXP_EEPROM_EEDO) == 0)
731 break;
732 DELAY(40);
733 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
734 DELAY(40);
735 }
736 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
737 DELAY(40);
738 if (x != 6 && x != 8) {
739 #ifdef DEBUG
740 printf("%s: strange EEPROM size (%d)\n",
741 sc->sc_dev.dv_xname, 1 << x);
742 #endif
743 } else
744 sc->sc_eeprom_size = x;
745 }
746
747 /*
748 * Read from the serial EEPROM. Basically, you manually shift in
749 * the read opcode (one bit at a time) and then shift in the address,
750 * and then you shift out the data (all of this one bit at a time).
751 * The word size is 16 bits, so you have to provide the address for
752 * every 16 bits of data.
753 */
754 void
755 fxp_read_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
756 {
757 u_int16_t reg;
758 int i, x;
759
760 for (i = 0; i < words; i++) {
761 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
762
763 /* Shift in read opcode. */
764 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
765
766 /* Shift in address. */
767 fxp_eeprom_shiftin(sc, i + offset, sc->sc_eeprom_size);
768
769 reg = FXP_EEPROM_EECS;
770 data[i] = 0;
771
772 /* Shift out data. */
773 for (x = 16; x > 0; x--) {
774 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
775 reg | FXP_EEPROM_EESK);
776 DELAY(40);
777 if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
778 FXP_EEPROM_EEDO)
779 data[i] |= (1 << (x - 1));
780 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
781 DELAY(40);
782 }
783 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
784 DELAY(40);
785 }
786 }
787
788 /*
789 * Write data to the serial EEPROM.
790 */
791 void
792 fxp_write_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
793 {
794 int i, j;
795
796 for (i = 0; i < words; i++) {
797 /* Erase/write enable. */
798 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
799 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_ERASE, 3);
800 fxp_eeprom_shiftin(sc, 0x3 << (sc->sc_eeprom_size - 2),
801 sc->sc_eeprom_size);
802 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
803 DELAY(4);
804
805 /* Shift in write opcode, address, data. */
806 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
807 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
808 fxp_eeprom_shiftin(sc, offset, sc->sc_eeprom_size);
809 fxp_eeprom_shiftin(sc, data[i], 16);
810 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
811 DELAY(4);
812
813 /* Wait for the EEPROM to finish up. */
814 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
815 DELAY(4);
816 for (j = 0; j < 1000; j++) {
817 if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
818 FXP_EEPROM_EEDO)
819 break;
820 DELAY(50);
821 }
822 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
823 DELAY(4);
824
825 /* Erase/write disable. */
826 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
827 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_ERASE, 3);
828 fxp_eeprom_shiftin(sc, 0, sc->sc_eeprom_size);
829 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
830 DELAY(4);
831 }
832 }
833
834 /*
835 * Update the checksum of the EEPROM.
836 */
837 void
838 fxp_eeprom_update_cksum(struct fxp_softc *sc)
839 {
840 int i;
841 uint16_t data, cksum;
842
843 cksum = 0;
844 for (i = 0; i < (1 << sc->sc_eeprom_size) - 1; i++) {
845 fxp_read_eeprom(sc, &data, i, 1);
846 cksum += data;
847 }
848 i = (1 << sc->sc_eeprom_size) - 1;
849 cksum = 0xbaba - cksum;
850 fxp_read_eeprom(sc, &data, i, 1);
851 fxp_write_eeprom(sc, &cksum, i, 1);
852 printf("%s: EEPROM checksum @ 0x%x: 0x%04x -> 0x%04x\n",
853 sc->sc_dev.dv_xname, i, data, cksum);
854 }
855
856 /*
857 * Start packet transmission on the interface.
858 */
859 void
860 fxp_start(struct ifnet *ifp)
861 {
862 struct fxp_softc *sc = ifp->if_softc;
863 struct mbuf *m0, *m;
864 struct fxp_txdesc *txd;
865 struct fxp_txsoft *txs;
866 bus_dmamap_t dmamap;
867 int error, lasttx, nexttx, opending, seg;
868
869 /*
870 * If we want a re-init, bail out now.
871 */
872 if (sc->sc_flags & FXPF_WANTINIT) {
873 ifp->if_flags |= IFF_OACTIVE;
874 return;
875 }
876
877 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
878 return;
879
880 /*
881 * Remember the previous txpending and the current lasttx.
882 */
883 opending = sc->sc_txpending;
884 lasttx = sc->sc_txlast;
885
886 /*
887 * Loop through the send queue, setting up transmit descriptors
888 * until we drain the queue, or use up all available transmit
889 * descriptors.
890 */
891 for (;;) {
892 struct fxp_tbd *tbdp;
893 int csum_flags;
894
895 /*
896 * Grab a packet off the queue.
897 */
898 IFQ_POLL(&ifp->if_snd, m0);
899 if (m0 == NULL)
900 break;
901 m = NULL;
902
903 if (sc->sc_txpending == FXP_NTXCB) {
904 FXP_EVCNT_INCR(&sc->sc_ev_txstall);
905 break;
906 }
907
908 /*
909 * Get the next available transmit descriptor.
910 */
911 nexttx = FXP_NEXTTX(sc->sc_txlast);
912 txd = FXP_CDTX(sc, nexttx);
913 txs = FXP_DSTX(sc, nexttx);
914 dmamap = txs->txs_dmamap;
915
916 /*
917 * Load the DMA map. If this fails, the packet either
918 * didn't fit in the allotted number of frags, or we were
919 * short on resources. In this case, we'll copy and try
920 * again.
921 */
922 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
923 BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
924 MGETHDR(m, M_DONTWAIT, MT_DATA);
925 if (m == NULL) {
926 printf("%s: unable to allocate Tx mbuf\n",
927 sc->sc_dev.dv_xname);
928 break;
929 }
930 MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
931 if (m0->m_pkthdr.len > MHLEN) {
932 MCLGET(m, M_DONTWAIT);
933 if ((m->m_flags & M_EXT) == 0) {
934 printf("%s: unable to allocate Tx "
935 "cluster\n", sc->sc_dev.dv_xname);
936 m_freem(m);
937 break;
938 }
939 }
940 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
941 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
942 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
943 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
944 if (error) {
945 printf("%s: unable to load Tx buffer, "
946 "error = %d\n", sc->sc_dev.dv_xname, error);
947 break;
948 }
949 }
950
951 IFQ_DEQUEUE(&ifp->if_snd, m0);
952 csum_flags = m0->m_pkthdr.csum_flags;
953 if (m != NULL) {
954 m_freem(m0);
955 m0 = m;
956 }
957
958 /* Initialize the fraglist. */
959 tbdp = txd->txd_tbd;
960 if (sc->sc_flags & FXPF_IPCB)
961 tbdp++;
962 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
963 tbdp[seg].tb_addr =
964 htole32(dmamap->dm_segs[seg].ds_addr);
965 tbdp[seg].tb_size =
966 htole32(dmamap->dm_segs[seg].ds_len);
967 }
968
969 /* Sync the DMA map. */
970 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
971 BUS_DMASYNC_PREWRITE);
972
973 /*
974 * Store a pointer to the packet so we can free it later.
975 */
976 txs->txs_mbuf = m0;
977
978 /*
979 * Initialize the transmit descriptor.
980 */
981 /* BIG_ENDIAN: no need to swap to store 0 */
982 txd->txd_txcb.cb_status = 0;
983 txd->txd_txcb.cb_command =
984 sc->sc_txcmd | htole16(FXP_CB_COMMAND_SF);
985 txd->txd_txcb.tx_threshold = tx_threshold;
986 txd->txd_txcb.tbd_number = dmamap->dm_nsegs;
987
988 KASSERT((csum_flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) == 0);
989 if (sc->sc_flags & FXPF_IPCB) {
990 struct fxp_ipcb *ipcb;
991 /*
992 * Deal with TCP/IP checksum offload. Note that
993 * in order for TCP checksum offload to work,
994 * the pseudo header checksum must have already
995 * been computed and stored in the checksum field
996 * in the TCP header. The stack should have
997 * already done this for us.
998 */
999 ipcb = &txd->txd_u.txdu_ipcb;
1000 memset(ipcb, 0, sizeof(*ipcb));
1001 /*
1002 * always do hardware parsing.
1003 */
1004 ipcb->ipcb_ip_activation_high =
1005 FXP_IPCB_HARDWAREPARSING_ENABLE;
1006 /*
1007 * ip checksum offloading.
1008 */
1009 if (csum_flags & M_CSUM_IPv4) {
1010 ipcb->ipcb_ip_schedule |=
1011 FXP_IPCB_IP_CHECKSUM_ENABLE;
1012 }
1013 /*
1014 * TCP/UDP checksum offloading.
1015 */
1016 if (csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
1017 ipcb->ipcb_ip_schedule |=
1018 FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1019 }
1020
1021 /*
1022 * request VLAN tag insertion if needed.
1023 */
1024 if (sc->sc_ethercom.ec_nvlans != 0) {
1025 struct m_tag *vtag;
1026
1027 vtag = m_tag_find(m0, PACKET_TAG_VLAN, NULL);
1028 if (vtag) {
1029 ipcb->ipcb_vlan_id =
1030 htobe16(*(u_int *)(vtag + 1));
1031 ipcb->ipcb_ip_activation_high |=
1032 FXP_IPCB_INSERTVLAN_ENABLE;
1033 }
1034 }
1035 } else {
1036 KASSERT((csum_flags &
1037 (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) == 0);
1038 }
1039
1040 FXP_CDTXSYNC(sc, nexttx,
1041 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1042
1043 /* Advance the tx pointer. */
1044 sc->sc_txpending++;
1045 sc->sc_txlast = nexttx;
1046
1047 #if NBPFILTER > 0
1048 /*
1049 * Pass packet to bpf if there is a listener.
1050 */
1051 if (ifp->if_bpf)
1052 bpf_mtap(ifp->if_bpf, m0);
1053 #endif
1054 }
1055
1056 if (sc->sc_txpending == FXP_NTXCB) {
1057 /* No more slots; notify upper layer. */
1058 ifp->if_flags |= IFF_OACTIVE;
1059 }
1060
1061 if (sc->sc_txpending != opending) {
1062 /*
1063 * We enqueued packets. If the transmitter was idle,
1064 * reset the txdirty pointer.
1065 */
1066 if (opending == 0)
1067 sc->sc_txdirty = FXP_NEXTTX(lasttx);
1068
1069 /*
1070 * Cause the chip to interrupt and suspend command
1071 * processing once the last packet we've enqueued
1072 * has been transmitted.
1073 */
1074 FXP_CDTX(sc, sc->sc_txlast)->txd_txcb.cb_command |=
1075 htole16(FXP_CB_COMMAND_I | FXP_CB_COMMAND_S);
1076 FXP_CDTXSYNC(sc, sc->sc_txlast,
1077 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1078
1079 /*
1080 * The entire packet chain is set up. Clear the suspend bit
1081 * on the command prior to the first packet we set up.
1082 */
1083 FXP_CDTXSYNC(sc, lasttx,
1084 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1085 FXP_CDTX(sc, lasttx)->txd_txcb.cb_command &=
1086 htole16(~FXP_CB_COMMAND_S);
1087 FXP_CDTXSYNC(sc, lasttx,
1088 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1089
1090 /*
1091 * Issue a Resume command in case the chip was suspended.
1092 * We only do this if we know we were idle before. If we
1093 * weren't idle before, we might be now, but we should also
1094 * have a pending interrupt, and we'll kick it again, there.
1095 * This might result in a tiny delay, but it also prevents
1096 * us from slamming the chip with CU_RESUME commands, which
1097 * might sometimes fail, resulting in SCB timeouts in
1098 * fxp_scb_wait().
1099 */
1100 if (opending == 0) {
1101 fxp_scb_wait(sc);
1102 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1103 }
1104
1105 /* Set a watchdog timer in case the chip flakes out. */
1106 ifp->if_timer = 5;
1107 }
1108 }
1109
1110 /*
1111 * Process interface interrupts.
1112 */
1113 int
1114 fxp_intr(void *arg)
1115 {
1116 struct fxp_softc *sc = arg;
1117 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1118 bus_dmamap_t rxmap;
1119 int claimed = 0;
1120 u_int8_t statack;
1121
1122 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || sc->sc_enabled == 0)
1123 return (0);
1124 /*
1125 * If the interface isn't running, don't try to
1126 * service the interrupt.. just ack it and bail.
1127 */
1128 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1129 statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
1130 if (statack) {
1131 claimed = 1;
1132 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1133 }
1134 return (claimed);
1135 }
1136
1137 while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1138 claimed = 1;
1139
1140 /*
1141 * First ACK all the interrupts in this pass.
1142 */
1143 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1144
1145 /*
1146 * Process receiver interrupts. If a no-resource (RNR)
1147 * condition exists, get whatever packets we can and
1148 * re-start the receiver.
1149 */
1150 if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1151 FXP_EVCNT_INCR(&sc->sc_ev_rxintr);
1152 fxp_rxintr(sc);
1153 }
1154
1155 if (statack & FXP_SCB_STATACK_RNR) {
1156 rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
1157 fxp_scb_wait(sc);
1158 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1159 rxmap->dm_segs[0].ds_addr +
1160 RFA_ALIGNMENT_FUDGE);
1161 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1162 }
1163
1164 /*
1165 * Free any finished transmit mbuf chains.
1166 */
1167 if (statack & (FXP_SCB_STATACK_CXTNO|FXP_SCB_STATACK_CNA)) {
1168 FXP_EVCNT_INCR(&sc->sc_ev_txintr);
1169 fxp_txintr(sc);
1170
1171 /*
1172 * Try to get more packets going.
1173 */
1174 fxp_start(ifp);
1175
1176 if (sc->sc_txpending == 0) {
1177 /*
1178 * If we want a re-init, do that now.
1179 */
1180 if (sc->sc_flags & FXPF_WANTINIT)
1181 (void) fxp_init(ifp);
1182 }
1183 }
1184 }
1185
1186 #if NRND > 0
1187 if (claimed)
1188 rnd_add_uint32(&sc->rnd_source, statack);
1189 #endif
1190 return (claimed);
1191 }
1192
1193 /*
1194 * Handle transmit completion interrupts.
1195 */
1196 void
1197 fxp_txintr(struct fxp_softc *sc)
1198 {
1199 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1200 struct fxp_txdesc *txd;
1201 struct fxp_txsoft *txs;
1202 int i;
1203 u_int16_t txstat;
1204
1205 ifp->if_flags &= ~IFF_OACTIVE;
1206 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
1207 i = FXP_NEXTTX(i), sc->sc_txpending--) {
1208 txd = FXP_CDTX(sc, i);
1209 txs = FXP_DSTX(sc, i);
1210
1211 FXP_CDTXSYNC(sc, i,
1212 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1213
1214 txstat = le16toh(txd->txd_txcb.cb_status);
1215
1216 if ((txstat & FXP_CB_STATUS_C) == 0)
1217 break;
1218
1219 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1220 0, txs->txs_dmamap->dm_mapsize,
1221 BUS_DMASYNC_POSTWRITE);
1222 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1223 m_freem(txs->txs_mbuf);
1224 txs->txs_mbuf = NULL;
1225 }
1226
1227 /* Update the dirty transmit buffer pointer. */
1228 sc->sc_txdirty = i;
1229
1230 /*
1231 * Cancel the watchdog timer if there are no pending
1232 * transmissions.
1233 */
1234 if (sc->sc_txpending == 0)
1235 ifp->if_timer = 0;
1236 else {
1237 fxp_scb_wait(sc);
1238 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1239 }
1240 }
1241
1242 /*
1243 * fxp_rx_hwcksum: check status of H/W offloading for received packets.
1244 */
1245
1246 int
1247 fxp_rx_hwcksum(struct mbuf *m, const struct fxp_rfa *rfa)
1248 {
1249 u_int16_t rxparsestat;
1250 u_int16_t csum_stat;
1251 u_int32_t csum_data;
1252 int csum_flags;
1253
1254 /*
1255 * check VLAN tag stripping.
1256 */
1257
1258 if (rfa->rfa_status & htole16(FXP_RFA_STATUS_VLAN)) {
1259 struct m_tag *vtag;
1260
1261 vtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int), M_NOWAIT);
1262 if (vtag == NULL)
1263 return ENOMEM;
1264 *(u_int *)(vtag + 1) = be16toh(rfa->vlan_id);
1265 m_tag_prepend(m, vtag);
1266 }
1267
1268 /*
1269 * check H/W Checksumming.
1270 */
1271
1272 csum_stat = le16toh(rfa->cksum_stat);
1273 rxparsestat = le16toh(rfa->rx_parse_stat);
1274 if (!(rfa->rfa_status & htole16(FXP_RFA_STATUS_PARSE)))
1275 return 0;
1276
1277 csum_flags = 0;
1278 csum_data = 0;
1279
1280 if (csum_stat & FXP_RFDX_CS_IP_CSUM_BIT_VALID) {
1281 csum_flags = M_CSUM_IPv4;
1282 if (!(csum_stat & FXP_RFDX_CS_IP_CSUM_VALID))
1283 csum_flags |= M_CSUM_IPv4_BAD;
1284 }
1285
1286 if (csum_stat & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) {
1287 csum_flags |= (M_CSUM_TCPv4|M_CSUM_UDPv4); /* XXX */
1288 if (!(csum_stat & FXP_RFDX_CS_TCPUDP_CSUM_VALID))
1289 csum_flags |= M_CSUM_TCP_UDP_BAD;
1290 }
1291
1292 m->m_pkthdr.csum_flags = csum_flags;
1293 m->m_pkthdr.csum_data = csum_data;
1294
1295 return 0;
1296 }
1297
1298 /*
1299 * Handle receive interrupts.
1300 */
1301 void
1302 fxp_rxintr(struct fxp_softc *sc)
1303 {
1304 struct ethercom *ec = &sc->sc_ethercom;
1305 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1306 struct mbuf *m, *m0;
1307 bus_dmamap_t rxmap;
1308 struct fxp_rfa *rfa;
1309 u_int16_t len, rxstat;
1310
1311 for (;;) {
1312 m = sc->sc_rxq.ifq_head;
1313 rfa = FXP_MTORFA(m);
1314 rxmap = M_GETCTX(m, bus_dmamap_t);
1315
1316 FXP_RFASYNC(sc, m,
1317 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1318
1319 rxstat = le16toh(rfa->rfa_status);
1320
1321 if ((rxstat & FXP_RFA_STATUS_C) == 0) {
1322 /*
1323 * We have processed all of the
1324 * receive buffers.
1325 */
1326 FXP_RFASYNC(sc, m, BUS_DMASYNC_PREREAD);
1327 return;
1328 }
1329
1330 IF_DEQUEUE(&sc->sc_rxq, m);
1331
1332 FXP_RXBUFSYNC(sc, m, BUS_DMASYNC_POSTREAD);
1333
1334 len = le16toh(rfa->actual_size) &
1335 (m->m_ext.ext_size - 1);
1336
1337 if (len < sizeof(struct ether_header)) {
1338 /*
1339 * Runt packet; drop it now.
1340 */
1341 FXP_INIT_RFABUF(sc, m);
1342 continue;
1343 }
1344
1345 /*
1346 * If support for 802.1Q VLAN sized frames is
1347 * enabled, we need to do some additional error
1348 * checking (as we are saving bad frames, in
1349 * order to receive the larger ones).
1350 */
1351 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) != 0 &&
1352 (rxstat & (FXP_RFA_STATUS_OVERRUN|
1353 FXP_RFA_STATUS_RNR|
1354 FXP_RFA_STATUS_ALIGN|
1355 FXP_RFA_STATUS_CRC)) != 0) {
1356 FXP_INIT_RFABUF(sc, m);
1357 continue;
1358 }
1359
1360 /* Do checksum checking. */
1361 m->m_pkthdr.csum_flags = 0;
1362 if (sc->sc_flags & FXPF_EXT_RFA)
1363 if (fxp_rx_hwcksum(m, rfa))
1364 goto dropit;
1365
1366 /*
1367 * If the packet is small enough to fit in a
1368 * single header mbuf, allocate one and copy
1369 * the data into it. This greatly reduces
1370 * memory consumption when we receive lots
1371 * of small packets.
1372 *
1373 * Otherwise, we add a new buffer to the receive
1374 * chain. If this fails, we drop the packet and
1375 * recycle the old buffer.
1376 */
1377 if (fxp_copy_small != 0 && len <= MHLEN) {
1378 MGETHDR(m0, M_DONTWAIT, MT_DATA);
1379 if (m0 == NULL)
1380 goto dropit;
1381 MCLAIM(m0, &sc->sc_ethercom.ec_rx_mowner);
1382 memcpy(mtod(m0, caddr_t),
1383 mtod(m, caddr_t), len);
1384 m0->m_pkthdr.csum_flags = m->m_pkthdr.csum_flags;
1385 m0->m_pkthdr.csum_data = m->m_pkthdr.csum_data;
1386 FXP_INIT_RFABUF(sc, m);
1387 m = m0;
1388 } else {
1389 if (fxp_add_rfabuf(sc, rxmap, 1) != 0) {
1390 dropit:
1391 ifp->if_ierrors++;
1392 FXP_INIT_RFABUF(sc, m);
1393 continue;
1394 }
1395 }
1396
1397 m->m_pkthdr.rcvif = ifp;
1398 m->m_pkthdr.len = m->m_len = len;
1399
1400 #if NBPFILTER > 0
1401 /*
1402 * Pass this up to any BPF listeners, but only
1403 * pass it up the stack it its for us.
1404 */
1405 if (ifp->if_bpf)
1406 bpf_mtap(ifp->if_bpf, m);
1407 #endif
1408
1409 /* Pass it on. */
1410 (*ifp->if_input)(ifp, m);
1411 }
1412 }
1413
1414 /*
1415 * Update packet in/out/collision statistics. The i82557 doesn't
1416 * allow you to access these counters without doing a fairly
1417 * expensive DMA to get _all_ of the statistics it maintains, so
1418 * we do this operation here only once per second. The statistics
1419 * counters in the kernel are updated from the previous dump-stats
1420 * DMA and then a new dump-stats DMA is started. The on-chip
1421 * counters are zeroed when the DMA completes. If we can't start
1422 * the DMA immediately, we don't wait - we just prepare to read
1423 * them again next time.
1424 */
1425 void
1426 fxp_tick(void *arg)
1427 {
1428 struct fxp_softc *sc = arg;
1429 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1430 struct fxp_stats *sp = &sc->sc_control_data->fcd_stats;
1431 int s;
1432
1433 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1434 return;
1435
1436 s = splnet();
1437
1438 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_POSTREAD);
1439
1440 ifp->if_opackets += le32toh(sp->tx_good);
1441 ifp->if_collisions += le32toh(sp->tx_total_collisions);
1442 if (sp->rx_good) {
1443 ifp->if_ipackets += le32toh(sp->rx_good);
1444 sc->sc_rxidle = 0;
1445 } else {
1446 sc->sc_rxidle++;
1447 }
1448 ifp->if_ierrors +=
1449 le32toh(sp->rx_crc_errors) +
1450 le32toh(sp->rx_alignment_errors) +
1451 le32toh(sp->rx_rnr_errors) +
1452 le32toh(sp->rx_overrun_errors);
1453 /*
1454 * If any transmit underruns occurred, bump up the transmit
1455 * threshold by another 512 bytes (64 * 8).
1456 */
1457 if (sp->tx_underruns) {
1458 ifp->if_oerrors += le32toh(sp->tx_underruns);
1459 if (tx_threshold < 192)
1460 tx_threshold += 64;
1461 }
1462
1463 /*
1464 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1465 * then assume the receiver has locked up and attempt to clear
1466 * the condition by reprogramming the multicast filter (actually,
1467 * resetting the interface). This is a work-around for a bug in
1468 * the 82557 where the receiver locks up if it gets certain types
1469 * of garbage in the synchronization bits prior to the packet header.
1470 * This bug is supposed to only occur in 10Mbps mode, but has been
1471 * seen to occur in 100Mbps mode as well (perhaps due to a 10/100
1472 * speed transition).
1473 */
1474 if (sc->sc_rxidle > FXP_MAX_RX_IDLE) {
1475 (void) fxp_init(ifp);
1476 splx(s);
1477 return;
1478 }
1479 /*
1480 * If there is no pending command, start another stats
1481 * dump. Otherwise punt for now.
1482 */
1483 if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1484 /*
1485 * Start another stats dump.
1486 */
1487 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
1488 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1489 } else {
1490 /*
1491 * A previous command is still waiting to be accepted.
1492 * Just zero our copy of the stats and wait for the
1493 * next timer event to update them.
1494 */
1495 /* BIG_ENDIAN: no swap required to store 0 */
1496 sp->tx_good = 0;
1497 sp->tx_underruns = 0;
1498 sp->tx_total_collisions = 0;
1499
1500 sp->rx_good = 0;
1501 sp->rx_crc_errors = 0;
1502 sp->rx_alignment_errors = 0;
1503 sp->rx_rnr_errors = 0;
1504 sp->rx_overrun_errors = 0;
1505 }
1506
1507 if (sc->sc_flags & FXPF_MII) {
1508 /* Tick the MII clock. */
1509 mii_tick(&sc->sc_mii);
1510 }
1511
1512 splx(s);
1513
1514 /*
1515 * Schedule another timeout one second from now.
1516 */
1517 callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
1518 }
1519
1520 /*
1521 * Drain the receive queue.
1522 */
1523 void
1524 fxp_rxdrain(struct fxp_softc *sc)
1525 {
1526 bus_dmamap_t rxmap;
1527 struct mbuf *m;
1528
1529 for (;;) {
1530 IF_DEQUEUE(&sc->sc_rxq, m);
1531 if (m == NULL)
1532 break;
1533 rxmap = M_GETCTX(m, bus_dmamap_t);
1534 bus_dmamap_unload(sc->sc_dmat, rxmap);
1535 FXP_RXMAP_PUT(sc, rxmap);
1536 m_freem(m);
1537 }
1538 }
1539
1540 /*
1541 * Stop the interface. Cancels the statistics updater and resets
1542 * the interface.
1543 */
1544 void
1545 fxp_stop(struct ifnet *ifp, int disable)
1546 {
1547 struct fxp_softc *sc = ifp->if_softc;
1548 struct fxp_txsoft *txs;
1549 int i;
1550
1551 /*
1552 * Turn down interface (done early to avoid bad interactions
1553 * between panics, shutdown hooks, and the watchdog timer)
1554 */
1555 ifp->if_timer = 0;
1556 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1557
1558 /*
1559 * Cancel stats updater.
1560 */
1561 callout_stop(&sc->sc_callout);
1562 if (sc->sc_flags & FXPF_MII) {
1563 /* Down the MII. */
1564 mii_down(&sc->sc_mii);
1565 }
1566
1567 /*
1568 * Issue software reset. This unloads any microcode that
1569 * might already be loaded.
1570 */
1571 sc->sc_flags &= ~FXPF_UCODE_LOADED;
1572 CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
1573 DELAY(50);
1574
1575 /*
1576 * Release any xmit buffers.
1577 */
1578 for (i = 0; i < FXP_NTXCB; i++) {
1579 txs = FXP_DSTX(sc, i);
1580 if (txs->txs_mbuf != NULL) {
1581 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1582 m_freem(txs->txs_mbuf);
1583 txs->txs_mbuf = NULL;
1584 }
1585 }
1586 sc->sc_txpending = 0;
1587
1588 if (disable) {
1589 fxp_rxdrain(sc);
1590 fxp_disable(sc);
1591 }
1592
1593 }
1594
1595 /*
1596 * Watchdog/transmission transmit timeout handler. Called when a
1597 * transmission is started on the interface, but no interrupt is
1598 * received before the timeout. This usually indicates that the
1599 * card has wedged for some reason.
1600 */
1601 void
1602 fxp_watchdog(struct ifnet *ifp)
1603 {
1604 struct fxp_softc *sc = ifp->if_softc;
1605
1606 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1607 ifp->if_oerrors++;
1608
1609 (void) fxp_init(ifp);
1610 }
1611
1612 /*
1613 * Initialize the interface. Must be called at splnet().
1614 */
1615 int
1616 fxp_init(struct ifnet *ifp)
1617 {
1618 struct fxp_softc *sc = ifp->if_softc;
1619 struct fxp_cb_config *cbp;
1620 struct fxp_cb_ias *cb_ias;
1621 struct fxp_txdesc *txd;
1622 bus_dmamap_t rxmap;
1623 int i, prm, save_bf, lrxen, vlan_drop, allm, error = 0;
1624
1625 if ((error = fxp_enable(sc)) != 0)
1626 goto out;
1627
1628 /*
1629 * Cancel any pending I/O
1630 */
1631 fxp_stop(ifp, 0);
1632
1633 /*
1634 * XXX just setting sc_flags to 0 here clears any FXPF_MII
1635 * flag, and this prevents the MII from detaching resulting in
1636 * a panic. The flags field should perhaps be split in runtime
1637 * flags and more static information. For now, just clear the
1638 * only other flag set.
1639 */
1640
1641 sc->sc_flags &= ~FXPF_WANTINIT;
1642
1643 /*
1644 * Initialize base of CBL and RFA memory. Loading with zero
1645 * sets it up for regular linear addressing.
1646 */
1647 fxp_scb_wait(sc);
1648 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1649 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1650
1651 fxp_scb_wait(sc);
1652 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1653
1654 /*
1655 * Initialize the multicast filter. Do this now, since we might
1656 * have to setup the config block differently.
1657 */
1658 fxp_mc_setup(sc);
1659
1660 prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1661 allm = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1662
1663 /*
1664 * In order to support receiving 802.1Q VLAN frames, we have to
1665 * enable "save bad frames", since they are 4 bytes larger than
1666 * the normal Ethernet maximum frame length. On i82558 and later,
1667 * we have a better mechanism for this.
1668 */
1669 save_bf = 0;
1670 lrxen = 0;
1671 vlan_drop = 0;
1672 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
1673 if (sc->sc_rev < FXP_REV_82558_A4)
1674 save_bf = 1;
1675 else
1676 lrxen = 1;
1677 if (sc->sc_rev >= FXP_REV_82550)
1678 vlan_drop = 1;
1679 }
1680
1681 /*
1682 * Initialize base of dump-stats buffer.
1683 */
1684 fxp_scb_wait(sc);
1685 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1686 sc->sc_cddma + FXP_CDSTATSOFF);
1687 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
1688 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1689
1690 cbp = &sc->sc_control_data->fcd_configcb;
1691 memset(cbp, 0, sizeof(struct fxp_cb_config));
1692
1693 /*
1694 * Load microcode for this controller.
1695 */
1696 fxp_load_ucode(sc);
1697
1698 /*
1699 * This copy is kind of disgusting, but there are a bunch of must be
1700 * zero and must be one bits in this structure and this is the easiest
1701 * way to initialize them all to proper values.
1702 */
1703 memcpy(cbp, fxp_cb_config_template, sizeof(fxp_cb_config_template));
1704
1705 /* BIG_ENDIAN: no need to swap to store 0 */
1706 cbp->cb_status = 0;
1707 cbp->cb_command = htole16(FXP_CB_COMMAND_CONFIG |
1708 FXP_CB_COMMAND_EL);
1709 /* BIG_ENDIAN: no need to swap to store 0xffffffff */
1710 cbp->link_addr = 0xffffffff; /* (no) next command */
1711 /* bytes in config block */
1712 cbp->byte_count = (sc->sc_flags & FXPF_EXT_RFA) ?
1713 FXP_EXT_CONFIG_LEN : FXP_CONFIG_LEN;
1714 cbp->rx_fifo_limit = 8; /* rx fifo threshold (32 bytes) */
1715 cbp->tx_fifo_limit = 0; /* tx fifo threshold (0 bytes) */
1716 cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */
1717 cbp->mwi_enable = (sc->sc_flags & FXPF_MWI) ? 1 : 0;
1718 cbp->type_enable = 0; /* actually reserved */
1719 cbp->read_align_en = (sc->sc_flags & FXPF_READ_ALIGN) ? 1 : 0;
1720 cbp->end_wr_on_cl = (sc->sc_flags & FXPF_WRITE_ALIGN) ? 1 : 0;
1721 cbp->rx_dma_bytecount = 0; /* (no) rx DMA max */
1722 cbp->tx_dma_bytecount = 0; /* (no) tx DMA max */
1723 cbp->dma_mbce = 0; /* (disable) dma max counters */
1724 cbp->late_scb = 0; /* (don't) defer SCB update */
1725 cbp->tno_int_or_tco_en =0; /* (disable) tx not okay interrupt */
1726 cbp->ci_int = 1; /* interrupt on CU idle */
1727 cbp->ext_txcb_dis = (sc->sc_flags & FXPF_EXT_TXCB) ? 0 : 1;
1728 cbp->ext_stats_dis = 1; /* disable extended counters */
1729 cbp->keep_overrun_rx = 0; /* don't pass overrun frames to host */
1730 cbp->save_bf = save_bf;/* save bad frames */
1731 cbp->disc_short_rx = !prm; /* discard short packets */
1732 cbp->underrun_retry = 1; /* retry mode (1) on DMA underrun */
1733 cbp->ext_rfa = (sc->sc_flags & FXPF_EXT_RFA) ? 1 : 0;
1734 cbp->two_frames = 0; /* do not limit FIFO to 2 frames */
1735 cbp->dyn_tbd = 0; /* (no) dynamic TBD mode */
1736 /* interface mode */
1737 cbp->mediatype = (sc->sc_flags & FXPF_MII) ? 1 : 0;
1738 cbp->csma_dis = 0; /* (don't) disable link */
1739 cbp->tcp_udp_cksum = 0; /* (don't) enable checksum */
1740 cbp->vlan_tco = 0; /* (don't) enable vlan wakeup */
1741 cbp->link_wake_en = 0; /* (don't) assert PME# on link change */
1742 cbp->arp_wake_en = 0; /* (don't) assert PME# on arp */
1743 cbp->mc_wake_en = 0; /* (don't) assert PME# on mcmatch */
1744 cbp->nsai = 1; /* (don't) disable source addr insert */
1745 cbp->preamble_length = 2; /* (7 byte) preamble */
1746 cbp->loopback = 0; /* (don't) loopback */
1747 cbp->linear_priority = 0; /* (normal CSMA/CD operation) */
1748 cbp->linear_pri_mode = 0; /* (wait after xmit only) */
1749 cbp->interfrm_spacing = 6; /* (96 bits of) interframe spacing */
1750 cbp->promiscuous = prm; /* promiscuous mode */
1751 cbp->bcast_disable = 0; /* (don't) disable broadcasts */
1752 cbp->wait_after_win = 0; /* (don't) enable modified backoff alg*/
1753 cbp->ignore_ul = 0; /* consider U/L bit in IA matching */
1754 cbp->crc16_en = 0; /* (don't) enable crc-16 algorithm */
1755 cbp->crscdt = (sc->sc_flags & FXPF_MII) ? 0 : 1;
1756 cbp->stripping = !prm; /* truncate rx packet to byte count */
1757 cbp->padding = 1; /* (do) pad short tx packets */
1758 cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */
1759 cbp->long_rx_en = lrxen; /* long packet receive enable */
1760 cbp->ia_wake_en = 0; /* (don't) wake up on address match */
1761 cbp->magic_pkt_dis = 0; /* (don't) disable magic packet */
1762 /* must set wake_en in PMCSR also */
1763 cbp->force_fdx = 0; /* (don't) force full duplex */
1764 cbp->fdx_pin_en = 1; /* (enable) FDX# pin */
1765 cbp->multi_ia = 0; /* (don't) accept multiple IAs */
1766 cbp->mc_all = allm; /* accept all multicasts */
1767 cbp->ext_rx_mode = (sc->sc_flags & FXPF_EXT_RFA) ? 1 : 0;
1768 cbp->vlan_drop_en = vlan_drop;
1769
1770 if (sc->sc_rev < FXP_REV_82558_A4) {
1771 /*
1772 * The i82557 has no hardware flow control, the values
1773 * here are the defaults for the chip.
1774 */
1775 cbp->fc_delay_lsb = 0;
1776 cbp->fc_delay_msb = 0x40;
1777 cbp->pri_fc_thresh = 3;
1778 cbp->tx_fc_dis = 0;
1779 cbp->rx_fc_restop = 0;
1780 cbp->rx_fc_restart = 0;
1781 cbp->fc_filter = 0;
1782 cbp->pri_fc_loc = 1;
1783 } else {
1784 cbp->fc_delay_lsb = 0x1f;
1785 cbp->fc_delay_msb = 0x01;
1786 cbp->pri_fc_thresh = 3;
1787 cbp->tx_fc_dis = 0; /* enable transmit FC */
1788 cbp->rx_fc_restop = 1; /* enable FC restop frames */
1789 cbp->rx_fc_restart = 1; /* enable FC restart frames */
1790 cbp->fc_filter = !prm; /* drop FC frames to host */
1791 cbp->pri_fc_loc = 1; /* FC pri location (byte31) */
1792 }
1793
1794 FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1795
1796 /*
1797 * Start the config command/DMA.
1798 */
1799 fxp_scb_wait(sc);
1800 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDCONFIGOFF);
1801 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1802 /* ...and wait for it to complete. */
1803 i = 1000;
1804 do {
1805 FXP_CDCONFIGSYNC(sc,
1806 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1807 DELAY(1);
1808 } while ((le16toh(cbp->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
1809 if (i == 0) {
1810 printf("%s at line %d: dmasync timeout\n",
1811 sc->sc_dev.dv_xname, __LINE__);
1812 return (ETIMEDOUT);
1813 }
1814
1815 /*
1816 * Initialize the station address.
1817 */
1818 cb_ias = &sc->sc_control_data->fcd_iascb;
1819 /* BIG_ENDIAN: no need to swap to store 0 */
1820 cb_ias->cb_status = 0;
1821 cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
1822 /* BIG_ENDIAN: no need to swap to store 0xffffffff */
1823 cb_ias->link_addr = 0xffffffff;
1824 memcpy((void *)cb_ias->macaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1825
1826 FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1827
1828 /*
1829 * Start the IAS (Individual Address Setup) command/DMA.
1830 */
1831 fxp_scb_wait(sc);
1832 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDIASOFF);
1833 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1834 /* ...and wait for it to complete. */
1835 i = 1000;
1836 do {
1837 FXP_CDIASSYNC(sc,
1838 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1839 DELAY(1);
1840 } while ((le16toh(cb_ias->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
1841 if (i == 0) {
1842 printf("%s at line %d: dmasync timeout\n",
1843 sc->sc_dev.dv_xname, __LINE__);
1844 return (ETIMEDOUT);
1845 }
1846
1847 /*
1848 * Initialize the transmit descriptor ring. txlast is initialized
1849 * to the end of the list so that it will wrap around to the first
1850 * descriptor when the first packet is transmitted.
1851 */
1852 for (i = 0; i < FXP_NTXCB; i++) {
1853 txd = FXP_CDTX(sc, i);
1854 memset(txd, 0, sizeof(*txd));
1855 txd->txd_txcb.cb_command =
1856 htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
1857 txd->txd_txcb.link_addr =
1858 htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(i)));
1859 if (sc->sc_flags & FXPF_EXT_TXCB)
1860 txd->txd_txcb.tbd_array_addr =
1861 htole32(FXP_CDTBDADDR(sc, i) +
1862 (2 * sizeof(struct fxp_tbd)));
1863 else
1864 txd->txd_txcb.tbd_array_addr =
1865 htole32(FXP_CDTBDADDR(sc, i));
1866 FXP_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1867 }
1868 sc->sc_txpending = 0;
1869 sc->sc_txdirty = 0;
1870 sc->sc_txlast = FXP_NTXCB - 1;
1871
1872 /*
1873 * Initialize the receive buffer list.
1874 */
1875 sc->sc_rxq.ifq_maxlen = FXP_NRFABUFS;
1876 while (sc->sc_rxq.ifq_len < FXP_NRFABUFS) {
1877 rxmap = FXP_RXMAP_GET(sc);
1878 if ((error = fxp_add_rfabuf(sc, rxmap, 0)) != 0) {
1879 printf("%s: unable to allocate or map rx "
1880 "buffer %d, error = %d\n",
1881 sc->sc_dev.dv_xname,
1882 sc->sc_rxq.ifq_len, error);
1883 /*
1884 * XXX Should attempt to run with fewer receive
1885 * XXX buffers instead of just failing.
1886 */
1887 FXP_RXMAP_PUT(sc, rxmap);
1888 fxp_rxdrain(sc);
1889 goto out;
1890 }
1891 }
1892 sc->sc_rxidle = 0;
1893
1894 /*
1895 * Give the transmit ring to the chip. We do this by pointing
1896 * the chip at the last descriptor (which is a NOP|SUSPEND), and
1897 * issuing a start command. It will execute the NOP and then
1898 * suspend, pointing at the first descriptor.
1899 */
1900 fxp_scb_wait(sc);
1901 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, FXP_CDTXADDR(sc, sc->sc_txlast));
1902 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1903
1904 /*
1905 * Initialize receiver buffer area - RFA.
1906 */
1907 rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
1908 fxp_scb_wait(sc);
1909 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1910 rxmap->dm_segs[0].ds_addr + RFA_ALIGNMENT_FUDGE);
1911 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1912
1913 if (sc->sc_flags & FXPF_MII) {
1914 /*
1915 * Set current media.
1916 */
1917 mii_mediachg(&sc->sc_mii);
1918 }
1919
1920 /*
1921 * ...all done!
1922 */
1923 ifp->if_flags |= IFF_RUNNING;
1924 ifp->if_flags &= ~IFF_OACTIVE;
1925
1926 /*
1927 * Start the one second timer.
1928 */
1929 callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
1930
1931 /*
1932 * Attempt to start output on the interface.
1933 */
1934 fxp_start(ifp);
1935
1936 out:
1937 if (error) {
1938 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1939 ifp->if_timer = 0;
1940 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1941 }
1942 return (error);
1943 }
1944
1945 /*
1946 * Change media according to request.
1947 */
1948 int
1949 fxp_mii_mediachange(struct ifnet *ifp)
1950 {
1951 struct fxp_softc *sc = ifp->if_softc;
1952
1953 if (ifp->if_flags & IFF_UP)
1954 mii_mediachg(&sc->sc_mii);
1955 return (0);
1956 }
1957
1958 /*
1959 * Notify the world which media we're using.
1960 */
1961 void
1962 fxp_mii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1963 {
1964 struct fxp_softc *sc = ifp->if_softc;
1965
1966 if (sc->sc_enabled == 0) {
1967 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1968 ifmr->ifm_status = 0;
1969 return;
1970 }
1971
1972 mii_pollstat(&sc->sc_mii);
1973 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1974 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1975 }
1976
1977 int
1978 fxp_80c24_mediachange(struct ifnet *ifp)
1979 {
1980
1981 /* Nothing to do here. */
1982 return (0);
1983 }
1984
1985 void
1986 fxp_80c24_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1987 {
1988 struct fxp_softc *sc = ifp->if_softc;
1989
1990 /*
1991 * Media is currently-selected media. We cannot determine
1992 * the link status.
1993 */
1994 ifmr->ifm_status = 0;
1995 ifmr->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
1996 }
1997
1998 /*
1999 * Add a buffer to the end of the RFA buffer list.
2000 * Return 0 if successful, error code on failure.
2001 *
2002 * The RFA struct is stuck at the beginning of mbuf cluster and the
2003 * data pointer is fixed up to point just past it.
2004 */
2005 int
2006 fxp_add_rfabuf(struct fxp_softc *sc, bus_dmamap_t rxmap, int unload)
2007 {
2008 struct mbuf *m;
2009 int error;
2010
2011 MGETHDR(m, M_DONTWAIT, MT_DATA);
2012 if (m == NULL)
2013 return (ENOBUFS);
2014
2015 MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
2016 MCLGET(m, M_DONTWAIT);
2017 if ((m->m_flags & M_EXT) == 0) {
2018 m_freem(m);
2019 return (ENOBUFS);
2020 }
2021
2022 if (unload)
2023 bus_dmamap_unload(sc->sc_dmat, rxmap);
2024
2025 M_SETCTX(m, rxmap);
2026
2027 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
2028 error = bus_dmamap_load_mbuf(sc->sc_dmat, rxmap, m,
2029 BUS_DMA_READ|BUS_DMA_NOWAIT);
2030 if (error) {
2031 printf("%s: can't load rx DMA map %d, error = %d\n",
2032 sc->sc_dev.dv_xname, sc->sc_rxq.ifq_len, error);
2033 panic("fxp_add_rfabuf"); /* XXX */
2034 }
2035
2036 FXP_INIT_RFABUF(sc, m);
2037
2038 return (0);
2039 }
2040
2041 int
2042 fxp_mdi_read(struct device *self, int phy, int reg)
2043 {
2044 struct fxp_softc *sc = (struct fxp_softc *)self;
2045 int count = 10000;
2046 int value;
2047
2048 CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2049 (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2050
2051 while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) &
2052 0x10000000) == 0 && count--)
2053 DELAY(10);
2054
2055 if (count <= 0)
2056 printf("%s: fxp_mdi_read: timed out\n", sc->sc_dev.dv_xname);
2057
2058 return (value & 0xffff);
2059 }
2060
2061 void
2062 fxp_statchg(struct device *self)
2063 {
2064
2065 /* Nothing to do. */
2066 }
2067
2068 void
2069 fxp_mdi_write(struct device *self, int phy, int reg, int value)
2070 {
2071 struct fxp_softc *sc = (struct fxp_softc *)self;
2072 int count = 10000;
2073
2074 CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2075 (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2076 (value & 0xffff));
2077
2078 while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2079 count--)
2080 DELAY(10);
2081
2082 if (count <= 0)
2083 printf("%s: fxp_mdi_write: timed out\n", sc->sc_dev.dv_xname);
2084 }
2085
2086 int
2087 fxp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2088 {
2089 struct fxp_softc *sc = ifp->if_softc;
2090 struct ifreq *ifr = (struct ifreq *)data;
2091 int s, error;
2092
2093 s = splnet();
2094
2095 switch (cmd) {
2096 case SIOCSIFMEDIA:
2097 case SIOCGIFMEDIA:
2098 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
2099 break;
2100
2101 default:
2102 error = ether_ioctl(ifp, cmd, data);
2103 if (error == ENETRESET) {
2104 if (sc->sc_enabled) {
2105 /*
2106 * Multicast list has changed; set the
2107 * hardware filter accordingly.
2108 */
2109 if (sc->sc_txpending) {
2110 sc->sc_flags |= FXPF_WANTINIT;
2111 error = 0;
2112 } else
2113 error = fxp_init(ifp);
2114 } else
2115 error = 0;
2116 }
2117 break;
2118 }
2119
2120 /* Try to get more packets going. */
2121 if (sc->sc_enabled)
2122 fxp_start(ifp);
2123
2124 splx(s);
2125 return (error);
2126 }
2127
2128 /*
2129 * Program the multicast filter.
2130 *
2131 * This function must be called at splnet().
2132 */
2133 void
2134 fxp_mc_setup(struct fxp_softc *sc)
2135 {
2136 struct fxp_cb_mcs *mcsp = &sc->sc_control_data->fcd_mcscb;
2137 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2138 struct ethercom *ec = &sc->sc_ethercom;
2139 struct ether_multi *enm;
2140 struct ether_multistep step;
2141 int count, nmcasts;
2142
2143 #ifdef DIAGNOSTIC
2144 if (sc->sc_txpending)
2145 panic("fxp_mc_setup: pending transmissions");
2146 #endif
2147
2148 ifp->if_flags &= ~IFF_ALLMULTI;
2149
2150 /*
2151 * Initialize multicast setup descriptor.
2152 */
2153 nmcasts = 0;
2154 ETHER_FIRST_MULTI(step, ec, enm);
2155 while (enm != NULL) {
2156 /*
2157 * Check for too many multicast addresses or if we're
2158 * listening to a range. Either way, we simply have
2159 * to accept all multicasts.
2160 */
2161 if (nmcasts >= MAXMCADDR ||
2162 memcmp(enm->enm_addrlo, enm->enm_addrhi,
2163 ETHER_ADDR_LEN) != 0) {
2164 /*
2165 * Callers of this function must do the
2166 * right thing with this. If we're called
2167 * from outside fxp_init(), the caller must
2168 * detect if the state if IFF_ALLMULTI changes.
2169 * If it does, the caller must then call
2170 * fxp_init(), since allmulti is handled by
2171 * the config block.
2172 */
2173 ifp->if_flags |= IFF_ALLMULTI;
2174 return;
2175 }
2176 memcpy((void *)&mcsp->mc_addr[nmcasts][0], enm->enm_addrlo,
2177 ETHER_ADDR_LEN);
2178 nmcasts++;
2179 ETHER_NEXT_MULTI(step, enm);
2180 }
2181
2182 /* BIG_ENDIAN: no need to swap to store 0 */
2183 mcsp->cb_status = 0;
2184 mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
2185 mcsp->link_addr = htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(sc->sc_txlast)));
2186 mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
2187
2188 FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2189
2190 /*
2191 * Wait until the command unit is not active. This should never
2192 * happen since nothing is queued, but make sure anyway.
2193 */
2194 count = 100;
2195 while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
2196 FXP_SCB_CUS_ACTIVE && --count)
2197 DELAY(1);
2198 if (count == 0) {
2199 printf("%s at line %d: command queue timeout\n",
2200 sc->sc_dev.dv_xname, __LINE__);
2201 return;
2202 }
2203
2204 /*
2205 * Start the multicast setup command/DMA.
2206 */
2207 fxp_scb_wait(sc);
2208 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDMCSOFF);
2209 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2210
2211 /* ...and wait for it to complete. */
2212 count = 1000;
2213 do {
2214 FXP_CDMCSSYNC(sc,
2215 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2216 DELAY(1);
2217 } while ((le16toh(mcsp->cb_status) & FXP_CB_STATUS_C) == 0 && --count);
2218 if (count == 0) {
2219 printf("%s at line %d: dmasync timeout\n",
2220 sc->sc_dev.dv_xname, __LINE__);
2221 return;
2222 }
2223 }
2224
2225 static const uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
2226 static const uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
2227 static const uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
2228 static const uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
2229 static const uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
2230 static const uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
2231
2232 #define UCODE(x) x, sizeof(x)
2233
2234 static const struct ucode {
2235 int32_t revision;
2236 const uint32_t *ucode;
2237 size_t length;
2238 uint16_t int_delay_offset;
2239 uint16_t bundle_max_offset;
2240 } ucode_table[] = {
2241 { FXP_REV_82558_A4, UCODE(fxp_ucode_d101a),
2242 D101_CPUSAVER_DWORD, 0 },
2243
2244 { FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0),
2245 D101_CPUSAVER_DWORD, 0 },
2246
2247 { FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
2248 D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
2249
2250 { FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
2251 D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
2252
2253 { FXP_REV_82550, UCODE(fxp_ucode_d102),
2254 D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
2255
2256 { FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
2257 D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
2258
2259 { 0, NULL, 0, 0, 0 }
2260 };
2261
2262 void
2263 fxp_load_ucode(struct fxp_softc *sc)
2264 {
2265 const struct ucode *uc;
2266 struct fxp_cb_ucode *cbp = &sc->sc_control_data->fcd_ucode;
2267 int count;
2268
2269 if (sc->sc_flags & FXPF_UCODE_LOADED)
2270 return;
2271
2272 /*
2273 * Only load the uCode if the user has requested that
2274 * we do so.
2275 */
2276 if ((sc->sc_ethercom.ec_if.if_flags & IFF_LINK0) == 0) {
2277 sc->sc_int_delay = 0;
2278 sc->sc_bundle_max = 0;
2279 return;
2280 }
2281
2282 for (uc = ucode_table; uc->ucode != NULL; uc++) {
2283 if (sc->sc_rev == uc->revision)
2284 break;
2285 }
2286 if (uc->ucode == NULL)
2287 return;
2288
2289 /* BIG ENDIAN: no need to swap to store 0 */
2290 cbp->cb_status = 0;
2291 cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
2292 cbp->link_addr = 0xffffffff; /* (no) next command */
2293 memcpy((void *) cbp->ucode, uc->ucode, uc->length);
2294
2295 if (uc->int_delay_offset)
2296 *(uint16_t *) &cbp->ucode[uc->int_delay_offset] =
2297 htole16(fxp_int_delay + (fxp_int_delay / 2));
2298
2299 if (uc->bundle_max_offset)
2300 *(uint16_t *) &cbp->ucode[uc->bundle_max_offset] =
2301 htole16(fxp_bundle_max);
2302
2303 FXP_CDUCODESYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2304
2305 /*
2306 * Download the uCode to the chip.
2307 */
2308 fxp_scb_wait(sc);
2309 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDUCODEOFF);
2310 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2311
2312 /* ...and wait for it to complete. */
2313 count = 10000;
2314 do {
2315 FXP_CDUCODESYNC(sc,
2316 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2317 DELAY(2);
2318 } while ((le16toh(cbp->cb_status) & FXP_CB_STATUS_C) == 0 && --count);
2319 if (count == 0) {
2320 sc->sc_int_delay = 0;
2321 sc->sc_bundle_max = 0;
2322 printf("%s: timeout loading microcode\n",
2323 sc->sc_dev.dv_xname);
2324 return;
2325 }
2326
2327 if (sc->sc_int_delay != fxp_int_delay ||
2328 sc->sc_bundle_max != fxp_bundle_max) {
2329 sc->sc_int_delay = fxp_int_delay;
2330 sc->sc_bundle_max = fxp_bundle_max;
2331 printf("%s: Microcode loaded: int delay: %d usec, "
2332 "max bundle: %d\n", sc->sc_dev.dv_xname,
2333 sc->sc_int_delay,
2334 uc->bundle_max_offset == 0 ? 0 : sc->sc_bundle_max);
2335 }
2336
2337 sc->sc_flags |= FXPF_UCODE_LOADED;
2338 }
2339
2340 int
2341 fxp_enable(struct fxp_softc *sc)
2342 {
2343
2344 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
2345 if ((*sc->sc_enable)(sc) != 0) {
2346 printf("%s: device enable failed\n",
2347 sc->sc_dev.dv_xname);
2348 return (EIO);
2349 }
2350 }
2351
2352 sc->sc_enabled = 1;
2353 return (0);
2354 }
2355
2356 void
2357 fxp_disable(struct fxp_softc *sc)
2358 {
2359
2360 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
2361 (*sc->sc_disable)(sc);
2362 sc->sc_enabled = 0;
2363 }
2364 }
2365
2366 /*
2367 * fxp_activate:
2368 *
2369 * Handle device activation/deactivation requests.
2370 */
2371 int
2372 fxp_activate(struct device *self, enum devact act)
2373 {
2374 struct fxp_softc *sc = (void *) self;
2375 int s, error = 0;
2376
2377 s = splnet();
2378 switch (act) {
2379 case DVACT_ACTIVATE:
2380 error = EOPNOTSUPP;
2381 break;
2382
2383 case DVACT_DEACTIVATE:
2384 if (sc->sc_flags & FXPF_MII)
2385 mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
2386 MII_OFFSET_ANY);
2387 if_deactivate(&sc->sc_ethercom.ec_if);
2388 break;
2389 }
2390 splx(s);
2391
2392 return (error);
2393 }
2394
2395 /*
2396 * fxp_detach:
2397 *
2398 * Detach an i82557 interface.
2399 */
2400 int
2401 fxp_detach(struct fxp_softc *sc)
2402 {
2403 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2404 int i;
2405
2406 /* Succeed now if there's no work to do. */
2407 if ((sc->sc_flags & FXPF_ATTACHED) == 0)
2408 return (0);
2409
2410 /* Unhook our tick handler. */
2411 callout_stop(&sc->sc_callout);
2412
2413 if (sc->sc_flags & FXPF_MII) {
2414 /* Detach all PHYs */
2415 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
2416 }
2417
2418 /* Delete all remaining media. */
2419 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
2420
2421 #if NRND > 0
2422 rnd_detach_source(&sc->rnd_source);
2423 #endif
2424 ether_ifdetach(ifp);
2425 if_detach(ifp);
2426
2427 for (i = 0; i < FXP_NRFABUFS; i++) {
2428 bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmaps[i]);
2429 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
2430 }
2431
2432 for (i = 0; i < FXP_NTXCB; i++) {
2433 bus_dmamap_unload(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
2434 bus_dmamap_destroy(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
2435 }
2436
2437 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
2438 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
2439 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
2440 sizeof(struct fxp_control_data));
2441 bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
2442
2443 shutdownhook_disestablish(sc->sc_sdhook);
2444 powerhook_disestablish(sc->sc_powerhook);
2445
2446 return (0);
2447 }
2448