i82557.c revision 1.61 1 /* $NetBSD: i82557.c,v 1.61 2001/11/13 13:14:38 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2001 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.61 2001/11/13 13:14:38 lukem 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 /*
118 * NOTE! On the Alpha, we have an alignment constraint. The
119 * card DMAs the packet immediately following the RFA. However,
120 * the first thing in the packet is a 14-byte Ethernet header.
121 * This means that the packet is misaligned. To compensate,
122 * we actually offset the RFA 2 bytes into the cluster. This
123 * alignes the packet after the Ethernet header at a 32-bit
124 * boundary. HOWEVER! This means that the RFA is misaligned!
125 */
126 #define RFA_ALIGNMENT_FUDGE 2
127
128 /*
129 * The configuration byte map has several undefined fields which
130 * must be one or must be zero. Set up a template for these bits
131 * only (assuming an i82557 chip), leaving the actual configuration
132 * for fxp_init().
133 *
134 * See the definition of struct fxp_cb_config for the bit definitions.
135 */
136 const u_int8_t fxp_cb_config_template[] = {
137 0x0, 0x0, /* cb_status */
138 0x0, 0x0, /* cb_command */
139 0x0, 0x0, 0x0, 0x0, /* link_addr */
140 0x0, /* 0 */
141 0x0, /* 1 */
142 0x0, /* 2 */
143 0x0, /* 3 */
144 0x0, /* 4 */
145 0x0, /* 5 */
146 0x32, /* 6 */
147 0x0, /* 7 */
148 0x0, /* 8 */
149 0x0, /* 9 */
150 0x6, /* 10 */
151 0x0, /* 11 */
152 0x0, /* 12 */
153 0x0, /* 13 */
154 0xf2, /* 14 */
155 0x48, /* 15 */
156 0x0, /* 16 */
157 0x40, /* 17 */
158 0xf0, /* 18 */
159 0x0, /* 19 */
160 0x3f, /* 20 */
161 0x5, /* 21 */
162 0x0, /* 22 */
163 0x0, /* 23 */
164 0x0, /* 24 */
165 0x0, /* 25 */
166 0x0, /* 26 */
167 0x0, /* 27 */
168 0x0, /* 28 */
169 0x0, /* 29 */
170 0x0, /* 30 */
171 0x0, /* 31 */
172 };
173
174 void fxp_mii_initmedia(struct fxp_softc *);
175 int fxp_mii_mediachange(struct ifnet *);
176 void fxp_mii_mediastatus(struct ifnet *, struct ifmediareq *);
177
178 void fxp_80c24_initmedia(struct fxp_softc *);
179 int fxp_80c24_mediachange(struct ifnet *);
180 void fxp_80c24_mediastatus(struct ifnet *, struct ifmediareq *);
181
182 void fxp_start(struct ifnet *);
183 int fxp_ioctl(struct ifnet *, u_long, caddr_t);
184 void fxp_watchdog(struct ifnet *);
185 int fxp_init(struct ifnet *);
186 void fxp_stop(struct ifnet *, int);
187
188 void fxp_txintr(struct fxp_softc *);
189 void fxp_rxintr(struct fxp_softc *);
190
191 void fxp_rxdrain(struct fxp_softc *);
192 int fxp_add_rfabuf(struct fxp_softc *, bus_dmamap_t, int);
193 int fxp_mdi_read(struct device *, int, int);
194 void fxp_statchg(struct device *);
195 void fxp_mdi_write(struct device *, int, int, int);
196 void fxp_autosize_eeprom(struct fxp_softc*);
197 void fxp_read_eeprom(struct fxp_softc *, u_int16_t *, int, int);
198 void fxp_get_info(struct fxp_softc *, u_int8_t *);
199 void fxp_tick(void *);
200 void fxp_mc_setup(struct fxp_softc *);
201
202 void fxp_shutdown(void *);
203 void fxp_power(int, void *);
204
205 int fxp_copy_small = 0;
206
207 struct fxp_phytype {
208 int fp_phy; /* type of PHY, -1 for MII at the end. */
209 void (*fp_init)(struct fxp_softc *);
210 } fxp_phytype_table[] = {
211 { FXP_PHY_80C24, fxp_80c24_initmedia },
212 { -1, fxp_mii_initmedia },
213 };
214
215 /*
216 * Set initial transmit threshold at 64 (512 bytes). This is
217 * increased by 64 (512 bytes) at a time, to maximum of 192
218 * (1536 bytes), if an underrun occurs.
219 */
220 static int tx_threshold = 64;
221
222 /*
223 * Wait for the previous command to be accepted (but not necessarily
224 * completed).
225 */
226 static __inline void
227 fxp_scb_wait(struct fxp_softc *sc)
228 {
229 int i = 10000;
230
231 while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
232 delay(2);
233 if (i == 0)
234 printf("%s: WARNING: SCB timed out!\n", sc->sc_dev.dv_xname);
235 }
236
237 /*
238 * Submit a command to the i82557.
239 */
240 static __inline void
241 fxp_scb_cmd(struct fxp_softc *sc, u_int8_t cmd)
242 {
243
244 if (cmd == FXP_SCB_COMMAND_CU_RESUME &&
245 (sc->sc_flags & FXPF_FIX_RESUME_BUG) != 0) {
246 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_NOP);
247 fxp_scb_wait(sc);
248 }
249 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
250 }
251
252 /*
253 * Finish attaching an i82557 interface. Called by bus-specific front-end.
254 */
255 void
256 fxp_attach(struct fxp_softc *sc)
257 {
258 u_int8_t enaddr[ETHER_ADDR_LEN];
259 struct ifnet *ifp;
260 bus_dma_segment_t seg;
261 int rseg, i, error;
262 struct fxp_phytype *fp;
263
264 callout_init(&sc->sc_callout);
265
266 /* Start out using the standard RFA. */
267 sc->sc_rfa_size = RFA_SIZE;
268
269 /*
270 * Enable some good stuff on i82558 and later.
271 */
272 if (sc->sc_rev >= FXP_REV_82558_A4) {
273 /* Enable the extended TxCB. */
274 sc->sc_flags |= FXPF_EXT_TXCB;
275 }
276
277 /*
278 * Allocate the control data structures, and create and load the
279 * DMA map for it.
280 */
281 if ((error = bus_dmamem_alloc(sc->sc_dmat,
282 sizeof(struct fxp_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
283 0)) != 0) {
284 printf("%s: unable to allocate control data, error = %d\n",
285 sc->sc_dev.dv_xname, error);
286 goto fail_0;
287 }
288
289 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
290 sizeof(struct fxp_control_data), (caddr_t *)&sc->sc_control_data,
291 BUS_DMA_COHERENT)) != 0) {
292 printf("%s: unable to map control data, error = %d\n",
293 sc->sc_dev.dv_xname, error);
294 goto fail_1;
295 }
296 sc->sc_cdseg = seg;
297 sc->sc_cdnseg = rseg;
298
299 memset(sc->sc_control_data, 0, sizeof(struct fxp_control_data));
300
301 if ((error = bus_dmamap_create(sc->sc_dmat,
302 sizeof(struct fxp_control_data), 1,
303 sizeof(struct fxp_control_data), 0, 0, &sc->sc_dmamap)) != 0) {
304 printf("%s: unable to create control data DMA map, "
305 "error = %d\n", sc->sc_dev.dv_xname, error);
306 goto fail_2;
307 }
308
309 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
310 sc->sc_control_data, sizeof(struct fxp_control_data), NULL,
311 0)) != 0) {
312 printf("%s: can't load control data DMA map, error = %d\n",
313 sc->sc_dev.dv_xname, error);
314 goto fail_3;
315 }
316
317 /*
318 * Create the transmit buffer DMA maps.
319 */
320 for (i = 0; i < FXP_NTXCB; i++) {
321 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
322 FXP_NTXSEG, MCLBYTES, 0, 0,
323 &FXP_DSTX(sc, i)->txs_dmamap)) != 0) {
324 printf("%s: unable to create tx DMA map %d, "
325 "error = %d\n", sc->sc_dev.dv_xname, i, error);
326 goto fail_4;
327 }
328 }
329
330 /*
331 * Create the receive buffer DMA maps.
332 */
333 for (i = 0; i < FXP_NRFABUFS; i++) {
334 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
335 MCLBYTES, 0, 0, &sc->sc_rxmaps[i])) != 0) {
336 printf("%s: unable to create rx DMA map %d, "
337 "error = %d\n", sc->sc_dev.dv_xname, i, error);
338 goto fail_5;
339 }
340 }
341
342 /* Initialize MAC address and media structures. */
343 fxp_get_info(sc, enaddr);
344
345 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
346 ether_sprintf(enaddr));
347
348 ifp = &sc->sc_ethercom.ec_if;
349
350 /*
351 * Get info about our media interface, and initialize it. Note
352 * the table terminates itself with a phy of -1, indicating
353 * that we're using MII.
354 */
355 for (fp = fxp_phytype_table; fp->fp_phy != -1; fp++)
356 if (fp->fp_phy == sc->phy_primary_device)
357 break;
358 (*fp->fp_init)(sc);
359
360 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
361 ifp->if_softc = sc;
362 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
363 ifp->if_ioctl = fxp_ioctl;
364 ifp->if_start = fxp_start;
365 ifp->if_watchdog = fxp_watchdog;
366 ifp->if_init = fxp_init;
367 ifp->if_stop = fxp_stop;
368 IFQ_SET_READY(&ifp->if_snd);
369
370 /*
371 * We can support 802.1Q VLAN-sized frames.
372 */
373 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
374
375 /*
376 * Attach the interface.
377 */
378 if_attach(ifp);
379 ether_ifattach(ifp, enaddr);
380 #if NRND > 0
381 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
382 RND_TYPE_NET, 0);
383 #endif
384
385 #ifdef FXP_EVENT_COUNTERS
386 evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
387 NULL, sc->sc_dev.dv_xname, "txstall");
388 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
389 NULL, sc->sc_dev.dv_xname, "txintr");
390 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
391 NULL, sc->sc_dev.dv_xname, "rxintr");
392 #endif /* FXP_EVENT_COUNTERS */
393
394 /*
395 * Add shutdown hook so that DMA is disabled prior to reboot. Not
396 * doing do could allow DMA to corrupt kernel memory during the
397 * reboot before the driver initializes.
398 */
399 sc->sc_sdhook = shutdownhook_establish(fxp_shutdown, sc);
400 if (sc->sc_sdhook == NULL)
401 printf("%s: WARNING: unable to establish shutdown hook\n",
402 sc->sc_dev.dv_xname);
403 /*
404 * Add suspend hook, for similar reasons..
405 */
406 sc->sc_powerhook = powerhook_establish(fxp_power, sc);
407 if (sc->sc_powerhook == NULL)
408 printf("%s: WARNING: unable to establish power hook\n",
409 sc->sc_dev.dv_xname);
410
411 /* The attach is successful. */
412 sc->sc_flags |= FXPF_ATTACHED;
413
414 return;
415
416 /*
417 * Free any resources we've allocated during the failed attach
418 * attempt. Do this in reverse order and fall though.
419 */
420 fail_5:
421 for (i = 0; i < FXP_NRFABUFS; i++) {
422 if (sc->sc_rxmaps[i] != NULL)
423 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
424 }
425 fail_4:
426 for (i = 0; i < FXP_NTXCB; i++) {
427 if (FXP_DSTX(sc, i)->txs_dmamap != NULL)
428 bus_dmamap_destroy(sc->sc_dmat,
429 FXP_DSTX(sc, i)->txs_dmamap);
430 }
431 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
432 fail_3:
433 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
434 fail_2:
435 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
436 sizeof(struct fxp_control_data));
437 fail_1:
438 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
439 fail_0:
440 return;
441 }
442
443 void
444 fxp_mii_initmedia(struct fxp_softc *sc)
445 {
446 int flags;
447
448 sc->sc_flags |= FXPF_MII;
449
450 sc->sc_mii.mii_ifp = &sc->sc_ethercom.ec_if;
451 sc->sc_mii.mii_readreg = fxp_mdi_read;
452 sc->sc_mii.mii_writereg = fxp_mdi_write;
453 sc->sc_mii.mii_statchg = fxp_statchg;
454 ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_mii_mediachange,
455 fxp_mii_mediastatus);
456
457 flags = MIIF_NOISOLATE;
458 if (sc->sc_rev >= FXP_REV_82558_A4)
459 flags |= MIIF_DOPAUSE;
460 /*
461 * The i82557 wedges if all of its PHYs are isolated!
462 */
463 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
464 MII_OFFSET_ANY, flags);
465 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
466 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
467 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
468 } else
469 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
470 }
471
472 void
473 fxp_80c24_initmedia(struct fxp_softc *sc)
474 {
475
476 /*
477 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
478 * doesn't have a programming interface of any sort. The
479 * media is sensed automatically based on how the link partner
480 * is configured. This is, in essence, manual configuration.
481 */
482 printf("%s: Seeq 80c24 AutoDUPLEX media interface present\n",
483 sc->sc_dev.dv_xname);
484 ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_80c24_mediachange,
485 fxp_80c24_mediastatus);
486 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
487 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
488 }
489
490 /*
491 * Device shutdown routine. Called at system shutdown after sync. The
492 * main purpose of this routine is to shut off receiver DMA so that
493 * kernel memory doesn't get clobbered during warmboot.
494 */
495 void
496 fxp_shutdown(void *arg)
497 {
498 struct fxp_softc *sc = arg;
499
500 /*
501 * Since the system's going to halt shortly, don't bother
502 * freeing mbufs.
503 */
504 fxp_stop(&sc->sc_ethercom.ec_if, 0);
505 }
506 /*
507 * Power handler routine. Called when the system is transitioning
508 * into/out of power save modes. As with fxp_shutdown, the main
509 * purpose of this routine is to shut off receiver DMA so it doesn't
510 * clobber kernel memory at the wrong time.
511 */
512 void
513 fxp_power(int why, void *arg)
514 {
515 struct fxp_softc *sc = arg;
516 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
517 int s;
518
519 s = splnet();
520 switch (why) {
521 case PWR_SUSPEND:
522 case PWR_STANDBY:
523 fxp_stop(ifp, 0);
524 break;
525 case PWR_RESUME:
526 if (ifp->if_flags & IFF_UP)
527 fxp_init(ifp);
528 break;
529 case PWR_SOFTSUSPEND:
530 case PWR_SOFTSTANDBY:
531 case PWR_SOFTRESUME:
532 break;
533 }
534 splx(s);
535 }
536
537 /*
538 * Initialize the interface media.
539 */
540 void
541 fxp_get_info(struct fxp_softc *sc, u_int8_t *enaddr)
542 {
543 u_int16_t data, myea[ETHER_ADDR_LEN / 2];
544
545 /*
546 * Reset to a stable state.
547 */
548 CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
549 DELAY(10);
550
551 sc->sc_eeprom_size = 0;
552 fxp_autosize_eeprom(sc);
553 if(sc->sc_eeprom_size == 0) {
554 printf("%s: failed to detect EEPROM size\n", sc->sc_dev.dv_xname);
555 sc->sc_eeprom_size = 6; /* XXX panic here? */
556 }
557 #ifdef DEBUG
558 printf("%s: detected %d word EEPROM\n",
559 sc->sc_dev.dv_xname,
560 1 << sc->sc_eeprom_size);
561 #endif
562
563 /*
564 * Get info about the primary PHY
565 */
566 fxp_read_eeprom(sc, &data, 6, 1);
567 sc->phy_primary_device =
568 (data & FXP_PHY_DEVICE_MASK) >> FXP_PHY_DEVICE_SHIFT;
569
570 /*
571 * Read MAC address.
572 */
573 fxp_read_eeprom(sc, myea, 0, 3);
574 enaddr[0] = myea[0] & 0xff;
575 enaddr[1] = myea[0] >> 8;
576 enaddr[2] = myea[1] & 0xff;
577 enaddr[3] = myea[1] >> 8;
578 enaddr[4] = myea[2] & 0xff;
579 enaddr[5] = myea[2] >> 8;
580 }
581
582 /*
583 * Figure out EEPROM size.
584 *
585 * 559's can have either 64-word or 256-word EEPROMs, the 558
586 * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
587 * talks about the existance of 16 to 256 word EEPROMs.
588 *
589 * The only known sizes are 64 and 256, where the 256 version is used
590 * by CardBus cards to store CIS information.
591 *
592 * The address is shifted in msb-to-lsb, and after the last
593 * address-bit the EEPROM is supposed to output a `dummy zero' bit,
594 * after which follows the actual data. We try to detect this zero, by
595 * probing the data-out bit in the EEPROM control register just after
596 * having shifted in a bit. If the bit is zero, we assume we've
597 * shifted enough address bits. The data-out should be tri-state,
598 * before this, which should translate to a logical one.
599 *
600 * Other ways to do this would be to try to read a register with known
601 * contents with a varying number of address bits, but no such
602 * register seem to be available. The high bits of register 10 are 01
603 * on the 558 and 559, but apparently not on the 557.
604 *
605 * The Linux driver computes a checksum on the EEPROM data, but the
606 * value of this checksum is not very well documented.
607 */
608
609 void
610 fxp_autosize_eeprom(struct fxp_softc *sc)
611 {
612 u_int16_t reg;
613 int x;
614
615 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
616 /*
617 * Shift in read opcode.
618 */
619 for (x = 3; x > 0; x--) {
620 if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
621 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
622 } else {
623 reg = FXP_EEPROM_EECS;
624 }
625 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
626 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
627 reg | FXP_EEPROM_EESK);
628 DELAY(4);
629 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
630 DELAY(4);
631 }
632 /*
633 * Shift in address, wait for the dummy zero following a correct
634 * address shift.
635 */
636 for (x = 1; x <= 8; x++) {
637 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
638 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
639 FXP_EEPROM_EECS | FXP_EEPROM_EESK);
640 DELAY(4);
641 if((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
642 FXP_EEPROM_EEDO) == 0)
643 break;
644 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
645 DELAY(4);
646 }
647 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
648 DELAY(4);
649 if(x != 6 && x != 8) {
650 #ifdef DEBUG
651 printf("%s: strange EEPROM size (%d)\n",
652 sc->sc_dev.dv_xname, 1 << x);
653 #endif
654 } else
655 sc->sc_eeprom_size = x;
656 }
657
658 /*
659 * Read from the serial EEPROM. Basically, you manually shift in
660 * the read opcode (one bit at a time) and then shift in the address,
661 * and then you shift out the data (all of this one bit at a time).
662 * The word size is 16 bits, so you have to provide the address for
663 * every 16 bits of data.
664 */
665 void
666 fxp_read_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
667 {
668 u_int16_t reg;
669 int i, x;
670
671 for (i = 0; i < words; i++) {
672 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
673 /*
674 * Shift in read opcode.
675 */
676 for (x = 3; x > 0; x--) {
677 if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
678 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
679 } else {
680 reg = FXP_EEPROM_EECS;
681 }
682 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
683 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
684 reg | FXP_EEPROM_EESK);
685 DELAY(4);
686 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
687 DELAY(4);
688 }
689 /*
690 * Shift in address.
691 */
692 for (x = sc->sc_eeprom_size; x > 0; x--) {
693 if ((i + offset) & (1 << (x - 1))) {
694 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
695 } else {
696 reg = FXP_EEPROM_EECS;
697 }
698 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
699 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
700 reg | FXP_EEPROM_EESK);
701 DELAY(4);
702 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
703 DELAY(4);
704 }
705 reg = FXP_EEPROM_EECS;
706 data[i] = 0;
707 /*
708 * Shift out data.
709 */
710 for (x = 16; x > 0; x--) {
711 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
712 reg | FXP_EEPROM_EESK);
713 DELAY(4);
714 if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
715 FXP_EEPROM_EEDO)
716 data[i] |= (1 << (x - 1));
717 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
718 DELAY(4);
719 }
720 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
721 DELAY(4);
722 }
723 }
724
725 /*
726 * Start packet transmission on the interface.
727 */
728 void
729 fxp_start(struct ifnet *ifp)
730 {
731 struct fxp_softc *sc = ifp->if_softc;
732 struct mbuf *m0, *m;
733 struct fxp_txdesc *txd;
734 struct fxp_txsoft *txs;
735 bus_dmamap_t dmamap;
736 int error, lasttx, nexttx, opending, seg;
737
738 /*
739 * If we want a re-init, bail out now.
740 */
741 if (sc->sc_flags & FXPF_WANTINIT) {
742 ifp->if_flags |= IFF_OACTIVE;
743 return;
744 }
745
746 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
747 return;
748
749 /*
750 * Remember the previous txpending and the current lasttx.
751 */
752 opending = sc->sc_txpending;
753 lasttx = sc->sc_txlast;
754
755 /*
756 * Loop through the send queue, setting up transmit descriptors
757 * until we drain the queue, or use up all available transmit
758 * descriptors.
759 */
760 for (;;) {
761 /*
762 * Grab a packet off the queue.
763 */
764 IFQ_POLL(&ifp->if_snd, m0);
765 if (m0 == NULL)
766 break;
767 m = NULL;
768
769 if (sc->sc_txpending == FXP_NTXCB) {
770 FXP_EVCNT_INCR(&sc->sc_ev_txstall);
771 break;
772 }
773
774 /*
775 * Get the next available transmit descriptor.
776 */
777 nexttx = FXP_NEXTTX(sc->sc_txlast);
778 txd = FXP_CDTX(sc, nexttx);
779 txs = FXP_DSTX(sc, nexttx);
780 dmamap = txs->txs_dmamap;
781
782 /*
783 * Load the DMA map. If this fails, the packet either
784 * didn't fit in the allotted number of frags, or we were
785 * short on resources. In this case, we'll copy and try
786 * again.
787 */
788 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
789 BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
790 MGETHDR(m, M_DONTWAIT, MT_DATA);
791 if (m == NULL) {
792 printf("%s: unable to allocate Tx mbuf\n",
793 sc->sc_dev.dv_xname);
794 break;
795 }
796 if (m0->m_pkthdr.len > MHLEN) {
797 MCLGET(m, M_DONTWAIT);
798 if ((m->m_flags & M_EXT) == 0) {
799 printf("%s: unable to allocate Tx "
800 "cluster\n", sc->sc_dev.dv_xname);
801 m_freem(m);
802 break;
803 }
804 }
805 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
806 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
807 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
808 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
809 if (error) {
810 printf("%s: unable to load Tx buffer, "
811 "error = %d\n", sc->sc_dev.dv_xname, error);
812 break;
813 }
814 }
815
816 IFQ_DEQUEUE(&ifp->if_snd, m0);
817 if (m != NULL) {
818 m_freem(m0);
819 m0 = m;
820 }
821
822 /* Initialize the fraglist. */
823 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
824 txd->txd_tbd[seg].tb_addr =
825 htole32(dmamap->dm_segs[seg].ds_addr);
826 txd->txd_tbd[seg].tb_size =
827 htole32(dmamap->dm_segs[seg].ds_len);
828 }
829
830 /* Sync the DMA map. */
831 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
832 BUS_DMASYNC_PREWRITE);
833
834 /*
835 * Store a pointer to the packet so we can free it later.
836 */
837 txs->txs_mbuf = m0;
838
839 /*
840 * Initialize the transmit descriptor.
841 */
842 /* BIG_ENDIAN: no need to swap to store 0 */
843 txd->txd_txcb.cb_status = 0;
844 txd->txd_txcb.cb_command =
845 htole16(FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF);
846 txd->txd_txcb.tx_threshold = tx_threshold;
847 txd->txd_txcb.tbd_number = dmamap->dm_nsegs;
848
849 FXP_CDTXSYNC(sc, nexttx,
850 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
851
852 /* Advance the tx pointer. */
853 sc->sc_txpending++;
854 sc->sc_txlast = nexttx;
855
856 #if NBPFILTER > 0
857 /*
858 * Pass packet to bpf if there is a listener.
859 */
860 if (ifp->if_bpf)
861 bpf_mtap(ifp->if_bpf, m0);
862 #endif
863 }
864
865 if (sc->sc_txpending == FXP_NTXCB) {
866 /* No more slots; notify upper layer. */
867 ifp->if_flags |= IFF_OACTIVE;
868 }
869
870 if (sc->sc_txpending != opending) {
871 /*
872 * We enqueued packets. If the transmitter was idle,
873 * reset the txdirty pointer.
874 */
875 if (opending == 0)
876 sc->sc_txdirty = FXP_NEXTTX(lasttx);
877
878 /*
879 * Cause the chip to interrupt and suspend command
880 * processing once the last packet we've enqueued
881 * has been transmitted.
882 */
883 FXP_CDTX(sc, sc->sc_txlast)->txd_txcb.cb_command |=
884 htole16(FXP_CB_COMMAND_I | FXP_CB_COMMAND_S);
885 FXP_CDTXSYNC(sc, sc->sc_txlast,
886 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
887
888 /*
889 * The entire packet chain is set up. Clear the suspend bit
890 * on the command prior to the first packet we set up.
891 */
892 FXP_CDTXSYNC(sc, lasttx,
893 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
894 FXP_CDTX(sc, lasttx)->txd_txcb.cb_command &=
895 htole16(~FXP_CB_COMMAND_S);
896 FXP_CDTXSYNC(sc, lasttx,
897 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
898
899 /*
900 * Issue a Resume command in case the chip was suspended.
901 */
902 fxp_scb_wait(sc);
903 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
904
905 /* Set a watchdog timer in case the chip flakes out. */
906 ifp->if_timer = 5;
907 }
908 }
909
910 /*
911 * Process interface interrupts.
912 */
913 int
914 fxp_intr(void *arg)
915 {
916 struct fxp_softc *sc = arg;
917 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
918 bus_dmamap_t rxmap;
919 int claimed = 0;
920 u_int8_t statack;
921
922 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
923 return (0);
924 /*
925 * If the interface isn't running, don't try to
926 * service the interrupt.. just ack it and bail.
927 */
928 if ((ifp->if_flags & IFF_RUNNING) == 0) {
929 statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
930 if (statack) {
931 claimed = 1;
932 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
933 }
934 return (claimed);
935 }
936
937 while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
938 claimed = 1;
939
940 /*
941 * First ACK all the interrupts in this pass.
942 */
943 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
944
945 /*
946 * Process receiver interrupts. If a no-resource (RNR)
947 * condition exists, get whatever packets we can and
948 * re-start the receiver.
949 */
950 if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
951 FXP_EVCNT_INCR(&sc->sc_ev_rxintr);
952 fxp_rxintr(sc);
953 }
954
955 if (statack & FXP_SCB_STATACK_RNR) {
956 rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
957 fxp_scb_wait(sc);
958 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
959 rxmap->dm_segs[0].ds_addr +
960 RFA_ALIGNMENT_FUDGE);
961 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
962 }
963
964 /*
965 * Free any finished transmit mbuf chains.
966 */
967 if (statack & (FXP_SCB_STATACK_CXTNO|FXP_SCB_STATACK_CNA)) {
968 FXP_EVCNT_INCR(&sc->sc_ev_txintr);
969 fxp_txintr(sc);
970
971 /*
972 * Try to get more packets going.
973 */
974 fxp_start(ifp);
975
976 if (sc->sc_txpending == 0) {
977 /*
978 * If we want a re-init, do that now.
979 */
980 if (sc->sc_flags & FXPF_WANTINIT)
981 (void) fxp_init(ifp);
982 }
983 }
984 }
985
986 #if NRND > 0
987 if (claimed)
988 rnd_add_uint32(&sc->rnd_source, statack);
989 #endif
990 return (claimed);
991 }
992
993 /*
994 * Handle transmit completion interrupts.
995 */
996 void
997 fxp_txintr(struct fxp_softc *sc)
998 {
999 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1000 struct fxp_txdesc *txd;
1001 struct fxp_txsoft *txs;
1002 int i;
1003 u_int16_t txstat;
1004
1005 ifp->if_flags &= ~IFF_OACTIVE;
1006 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
1007 i = FXP_NEXTTX(i), sc->sc_txpending--) {
1008 txd = FXP_CDTX(sc, i);
1009 txs = FXP_DSTX(sc, i);
1010
1011 FXP_CDTXSYNC(sc, i,
1012 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1013
1014 txstat = le16toh(txd->txd_txcb.cb_status);
1015
1016 if ((txstat & FXP_CB_STATUS_C) == 0)
1017 break;
1018
1019 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1020 0, txs->txs_dmamap->dm_mapsize,
1021 BUS_DMASYNC_POSTWRITE);
1022 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1023 m_freem(txs->txs_mbuf);
1024 txs->txs_mbuf = NULL;
1025 }
1026
1027 /* Update the dirty transmit buffer pointer. */
1028 sc->sc_txdirty = i;
1029
1030 /*
1031 * Cancel the watchdog timer if there are no pending
1032 * transmissions.
1033 */
1034 if (sc->sc_txpending == 0)
1035 ifp->if_timer = 0;
1036 }
1037
1038 /*
1039 * Handle receive interrupts.
1040 */
1041 void
1042 fxp_rxintr(struct fxp_softc *sc)
1043 {
1044 struct ethercom *ec = &sc->sc_ethercom;
1045 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1046 struct mbuf *m, *m0;
1047 bus_dmamap_t rxmap;
1048 struct fxp_rfa *rfa;
1049 u_int16_t len, rxstat;
1050
1051 for (;;) {
1052 m = sc->sc_rxq.ifq_head;
1053 rfa = FXP_MTORFA(m);
1054 rxmap = M_GETCTX(m, bus_dmamap_t);
1055
1056 FXP_RFASYNC(sc, m,
1057 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1058
1059 rxstat = le16toh(rfa->rfa_status);
1060
1061 if ((rxstat & FXP_RFA_STATUS_C) == 0) {
1062 /*
1063 * We have processed all of the
1064 * receive buffers.
1065 */
1066 FXP_RFASYNC(sc, m, BUS_DMASYNC_PREREAD);
1067 return;
1068 }
1069
1070 IF_DEQUEUE(&sc->sc_rxq, m);
1071
1072 FXP_RXBUFSYNC(sc, m, BUS_DMASYNC_POSTREAD);
1073
1074 len = le16toh(rfa->actual_size) &
1075 (m->m_ext.ext_size - 1);
1076
1077 if (len < sizeof(struct ether_header)) {
1078 /*
1079 * Runt packet; drop it now.
1080 */
1081 FXP_INIT_RFABUF(sc, m);
1082 continue;
1083 }
1084
1085 /*
1086 * If support for 802.1Q VLAN sized frames is
1087 * enabled, we need to do some additional error
1088 * checking (as we are saving bad frames, in
1089 * order to receive the larger ones).
1090 */
1091 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) != 0 &&
1092 (rxstat & (FXP_RFA_STATUS_OVERRUN|
1093 FXP_RFA_STATUS_RNR|
1094 FXP_RFA_STATUS_ALIGN|
1095 FXP_RFA_STATUS_CRC)) != 0) {
1096 FXP_INIT_RFABUF(sc, m);
1097 continue;
1098 }
1099
1100 /*
1101 * If the packet is small enough to fit in a
1102 * single header mbuf, allocate one and copy
1103 * the data into it. This greatly reduces
1104 * memory consumption when we receive lots
1105 * of small packets.
1106 *
1107 * Otherwise, we add a new buffer to the receive
1108 * chain. If this fails, we drop the packet and
1109 * recycle the old buffer.
1110 */
1111 if (fxp_copy_small != 0 && len <= MHLEN) {
1112 MGETHDR(m0, M_DONTWAIT, MT_DATA);
1113 if (m == NULL)
1114 goto dropit;
1115 memcpy(mtod(m0, caddr_t),
1116 mtod(m, caddr_t), len);
1117 FXP_INIT_RFABUF(sc, m);
1118 m = m0;
1119 } else {
1120 if (fxp_add_rfabuf(sc, rxmap, 1) != 0) {
1121 dropit:
1122 ifp->if_ierrors++;
1123 FXP_INIT_RFABUF(sc, m);
1124 continue;
1125 }
1126 }
1127
1128 m->m_pkthdr.rcvif = ifp;
1129 m->m_pkthdr.len = m->m_len = len;
1130
1131 #if NBPFILTER > 0
1132 /*
1133 * Pass this up to any BPF listeners, but only
1134 * pass it up the stack it its for us.
1135 */
1136 if (ifp->if_bpf)
1137 bpf_mtap(ifp->if_bpf, m);
1138 #endif
1139
1140 /* Pass it on. */
1141 (*ifp->if_input)(ifp, m);
1142 }
1143 }
1144
1145 /*
1146 * Update packet in/out/collision statistics. The i82557 doesn't
1147 * allow you to access these counters without doing a fairly
1148 * expensive DMA to get _all_ of the statistics it maintains, so
1149 * we do this operation here only once per second. The statistics
1150 * counters in the kernel are updated from the previous dump-stats
1151 * DMA and then a new dump-stats DMA is started. The on-chip
1152 * counters are zeroed when the DMA completes. If we can't start
1153 * the DMA immediately, we don't wait - we just prepare to read
1154 * them again next time.
1155 */
1156 void
1157 fxp_tick(void *arg)
1158 {
1159 struct fxp_softc *sc = arg;
1160 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1161 struct fxp_stats *sp = &sc->sc_control_data->fcd_stats;
1162 int s;
1163
1164 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1165 return;
1166
1167 s = splnet();
1168
1169 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_POSTREAD);
1170
1171 ifp->if_opackets += le32toh(sp->tx_good);
1172 ifp->if_collisions += le32toh(sp->tx_total_collisions);
1173 if (sp->rx_good) {
1174 ifp->if_ipackets += le32toh(sp->rx_good);
1175 sc->sc_rxidle = 0;
1176 } else {
1177 sc->sc_rxidle++;
1178 }
1179 ifp->if_ierrors +=
1180 le32toh(sp->rx_crc_errors) +
1181 le32toh(sp->rx_alignment_errors) +
1182 le32toh(sp->rx_rnr_errors) +
1183 le32toh(sp->rx_overrun_errors);
1184 /*
1185 * If any transmit underruns occurred, bump up the transmit
1186 * threshold by another 512 bytes (64 * 8).
1187 */
1188 if (sp->tx_underruns) {
1189 ifp->if_oerrors += le32toh(sp->tx_underruns);
1190 if (tx_threshold < 192)
1191 tx_threshold += 64;
1192 }
1193
1194 /*
1195 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1196 * then assume the receiver has locked up and attempt to clear
1197 * the condition by reprogramming the multicast filter (actually,
1198 * resetting the interface). This is a work-around for a bug in
1199 * the 82557 where the receiver locks up if it gets certain types
1200 * of garbage in the syncronization bits prior to the packet header.
1201 * This bug is supposed to only occur in 10Mbps mode, but has been
1202 * seen to occur in 100Mbps mode as well (perhaps due to a 10/100
1203 * speed transition).
1204 */
1205 if (sc->sc_rxidle > FXP_MAX_RX_IDLE) {
1206 (void) fxp_init(ifp);
1207 splx(s);
1208 return;
1209 }
1210 /*
1211 * If there is no pending command, start another stats
1212 * dump. Otherwise punt for now.
1213 */
1214 if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1215 /*
1216 * Start another stats dump.
1217 */
1218 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
1219 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1220 } else {
1221 /*
1222 * A previous command is still waiting to be accepted.
1223 * Just zero our copy of the stats and wait for the
1224 * next timer event to update them.
1225 */
1226 /* BIG_ENDIAN: no swap required to store 0 */
1227 sp->tx_good = 0;
1228 sp->tx_underruns = 0;
1229 sp->tx_total_collisions = 0;
1230
1231 sp->rx_good = 0;
1232 sp->rx_crc_errors = 0;
1233 sp->rx_alignment_errors = 0;
1234 sp->rx_rnr_errors = 0;
1235 sp->rx_overrun_errors = 0;
1236 }
1237
1238 if (sc->sc_flags & FXPF_MII) {
1239 /* Tick the MII clock. */
1240 mii_tick(&sc->sc_mii);
1241 }
1242
1243 splx(s);
1244
1245 /*
1246 * Schedule another timeout one second from now.
1247 */
1248 callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
1249 }
1250
1251 /*
1252 * Drain the receive queue.
1253 */
1254 void
1255 fxp_rxdrain(struct fxp_softc *sc)
1256 {
1257 bus_dmamap_t rxmap;
1258 struct mbuf *m;
1259
1260 for (;;) {
1261 IF_DEQUEUE(&sc->sc_rxq, m);
1262 if (m == NULL)
1263 break;
1264 rxmap = M_GETCTX(m, bus_dmamap_t);
1265 bus_dmamap_unload(sc->sc_dmat, rxmap);
1266 FXP_RXMAP_PUT(sc, rxmap);
1267 m_freem(m);
1268 }
1269 }
1270
1271 /*
1272 * Stop the interface. Cancels the statistics updater and resets
1273 * the interface.
1274 */
1275 void
1276 fxp_stop(struct ifnet *ifp, int disable)
1277 {
1278 struct fxp_softc *sc = ifp->if_softc;
1279 struct fxp_txsoft *txs;
1280 int i;
1281
1282 /*
1283 * Turn down interface (done early to avoid bad interactions
1284 * between panics, shutdown hooks, and the watchdog timer)
1285 */
1286 ifp->if_timer = 0;
1287 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1288
1289 /*
1290 * Cancel stats updater.
1291 */
1292 callout_stop(&sc->sc_callout);
1293 if (sc->sc_flags & FXPF_MII) {
1294 /* Down the MII. */
1295 mii_down(&sc->sc_mii);
1296 }
1297
1298 /*
1299 * Issue software reset
1300 */
1301 CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1302 DELAY(10);
1303
1304 /*
1305 * Release any xmit buffers.
1306 */
1307 for (i = 0; i < FXP_NTXCB; i++) {
1308 txs = FXP_DSTX(sc, i);
1309 if (txs->txs_mbuf != NULL) {
1310 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1311 m_freem(txs->txs_mbuf);
1312 txs->txs_mbuf = NULL;
1313 }
1314 }
1315 sc->sc_txpending = 0;
1316
1317 if (disable) {
1318 fxp_rxdrain(sc);
1319 fxp_disable(sc);
1320 }
1321
1322 }
1323
1324 /*
1325 * Watchdog/transmission transmit timeout handler. Called when a
1326 * transmission is started on the interface, but no interrupt is
1327 * received before the timeout. This usually indicates that the
1328 * card has wedged for some reason.
1329 */
1330 void
1331 fxp_watchdog(struct ifnet *ifp)
1332 {
1333 struct fxp_softc *sc = ifp->if_softc;
1334
1335 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1336 ifp->if_oerrors++;
1337
1338 (void) fxp_init(ifp);
1339 }
1340
1341 /*
1342 * Initialize the interface. Must be called at splnet().
1343 */
1344 int
1345 fxp_init(struct ifnet *ifp)
1346 {
1347 struct fxp_softc *sc = ifp->if_softc;
1348 struct fxp_cb_config *cbp;
1349 struct fxp_cb_ias *cb_ias;
1350 struct fxp_txdesc *txd;
1351 bus_dmamap_t rxmap;
1352 int i, prm, save_bf, lrxen, allm, error = 0;
1353
1354 if ((error = fxp_enable(sc)) != 0)
1355 goto out;
1356
1357 /*
1358 * Cancel any pending I/O
1359 */
1360 fxp_stop(ifp, 0);
1361
1362 /*
1363 * XXX just setting sc_flags to 0 here clears any FXPF_MII
1364 * flag, and this prevents the MII from detaching resulting in
1365 * a panic. The flags field should perhaps be split in runtime
1366 * flags and more static information. For now, just clear the
1367 * only other flag set.
1368 */
1369
1370 sc->sc_flags &= ~FXPF_WANTINIT;
1371
1372 /*
1373 * Initialize base of CBL and RFA memory. Loading with zero
1374 * sets it up for regular linear addressing.
1375 */
1376 fxp_scb_wait(sc);
1377 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1378 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1379
1380 fxp_scb_wait(sc);
1381 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1382
1383 /*
1384 * Initialize the multicast filter. Do this now, since we might
1385 * have to setup the config block differently.
1386 */
1387 fxp_mc_setup(sc);
1388
1389 prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1390 allm = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1391
1392 /*
1393 * In order to support receiving 802.1Q VLAN frames, we have to
1394 * enable "save bad frames", since they are 4 bytes larger than
1395 * the normal Ethernet maximum frame length. On i82558 and later,
1396 * we have a better mechanism for this.
1397 */
1398 save_bf = 0;
1399 lrxen = 0;
1400 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
1401 if (sc->sc_rev < FXP_REV_82558_A4)
1402 save_bf = 1;
1403 else
1404 lrxen = 1;
1405 }
1406
1407 /*
1408 * Initialize base of dump-stats buffer.
1409 */
1410 fxp_scb_wait(sc);
1411 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1412 sc->sc_cddma + FXP_CDSTATSOFF);
1413 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
1414 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1415
1416 cbp = &sc->sc_control_data->fcd_configcb;
1417 memset(cbp, 0, sizeof(struct fxp_cb_config));
1418
1419 /*
1420 * This copy is kind of disgusting, but there are a bunch of must be
1421 * zero and must be one bits in this structure and this is the easiest
1422 * way to initialize them all to proper values.
1423 */
1424 memcpy(cbp, fxp_cb_config_template, sizeof(fxp_cb_config_template));
1425
1426 /* BIG_ENDIAN: no need to swap to store 0 */
1427 cbp->cb_status = 0;
1428 cbp->cb_command = htole16(FXP_CB_COMMAND_CONFIG |
1429 FXP_CB_COMMAND_EL);
1430 /* BIG_ENDIAN: no need to swap to store 0xffffffff */
1431 cbp->link_addr = 0xffffffff; /* (no) next command */
1432 /* bytes in config block */
1433 cbp->byte_count = FXP_CONFIG_LEN;
1434 cbp->rx_fifo_limit = 8; /* rx fifo threshold (32 bytes) */
1435 cbp->tx_fifo_limit = 0; /* tx fifo threshold (0 bytes) */
1436 cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */
1437 cbp->mwi_enable = (sc->sc_flags & FXPF_MWI) ? 1 : 0;
1438 cbp->type_enable = 0; /* actually reserved */
1439 cbp->read_align_en = (sc->sc_flags & FXPF_READ_ALIGN) ? 1 : 0;
1440 cbp->end_wr_on_cl = (sc->sc_flags & FXPF_WRITE_ALIGN) ? 1 : 0;
1441 cbp->rx_dma_bytecount = 0; /* (no) rx DMA max */
1442 cbp->tx_dma_bytecount = 0; /* (no) tx DMA max */
1443 cbp->dma_mbce = 0; /* (disable) dma max counters */
1444 cbp->late_scb = 0; /* (don't) defer SCB update */
1445 cbp->tno_int_or_tco_en =0; /* (disable) tx not okay interrupt */
1446 cbp->ci_int = 1; /* interrupt on CU idle */
1447 cbp->ext_txcb_dis = (sc->sc_flags & FXPF_EXT_TXCB) ? 0 : 1;
1448 cbp->ext_stats_dis = 1; /* disable extended counters */
1449 cbp->keep_overrun_rx = 0; /* don't pass overrun frames to host */
1450 cbp->save_bf = save_bf;/* save bad frames */
1451 cbp->disc_short_rx = !prm; /* discard short packets */
1452 cbp->underrun_retry = 1; /* retry mode (1) on DMA underrun */
1453 cbp->two_frames = 0; /* do not limit FIFO to 2 frames */
1454 cbp->dyn_tbd = 0; /* (no) dynamic TBD mode */
1455 /* interface mode */
1456 cbp->mediatype = (sc->sc_flags & FXPF_MII) ? 1 : 0;
1457 cbp->csma_dis = 0; /* (don't) disable link */
1458 cbp->tcp_udp_cksum = 0; /* (don't) enable checksum */
1459 cbp->vlan_tco = 0; /* (don't) enable vlan wakeup */
1460 cbp->link_wake_en = 0; /* (don't) assert PME# on link change */
1461 cbp->arp_wake_en = 0; /* (don't) assert PME# on arp */
1462 cbp->mc_wake_en = 0; /* (don't) assert PME# on mcmatch */
1463 cbp->nsai = 1; /* (don't) disable source addr insert */
1464 cbp->preamble_length = 2; /* (7 byte) preamble */
1465 cbp->loopback = 0; /* (don't) loopback */
1466 cbp->linear_priority = 0; /* (normal CSMA/CD operation) */
1467 cbp->linear_pri_mode = 0; /* (wait after xmit only) */
1468 cbp->interfrm_spacing = 6; /* (96 bits of) interframe spacing */
1469 cbp->promiscuous = prm; /* promiscuous mode */
1470 cbp->bcast_disable = 0; /* (don't) disable broadcasts */
1471 cbp->wait_after_win = 0; /* (don't) enable modified backoff alg*/
1472 cbp->ignore_ul = 0; /* consider U/L bit in IA matching */
1473 cbp->crc16_en = 0; /* (don't) enable crc-16 algorithm */
1474 cbp->crscdt = (sc->sc_flags & FXPF_MII) ? 0 : 1;
1475 cbp->stripping = !prm; /* truncate rx packet to byte count */
1476 cbp->padding = 1; /* (do) pad short tx packets */
1477 cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */
1478 cbp->long_rx_en = lrxen; /* long packet receive enable */
1479 cbp->ia_wake_en = 0; /* (don't) wake up on address match */
1480 cbp->magic_pkt_dis = 0; /* (don't) disable magic packet */
1481 /* must set wake_en in PMCSR also */
1482 cbp->force_fdx = 0; /* (don't) force full duplex */
1483 cbp->fdx_pin_en = 1; /* (enable) FDX# pin */
1484 cbp->multi_ia = 0; /* (don't) accept multiple IAs */
1485 cbp->mc_all = allm; /* accept all multicasts */
1486
1487 if (sc->sc_rev < FXP_REV_82558_A4) {
1488 /*
1489 * The i82557 has no hardware flow control, the values
1490 * here are the defaults for the chip.
1491 */
1492 cbp->fc_delay_lsb = 0;
1493 cbp->fc_delay_msb = 0x40;
1494 cbp->pri_fc_thresh = 3;
1495 cbp->tx_fc_dis = 0;
1496 cbp->rx_fc_restop = 0;
1497 cbp->rx_fc_restart = 0;
1498 cbp->fc_filter = 0;
1499 cbp->pri_fc_loc = 1;
1500 } else {
1501 cbp->fc_delay_lsb = 0x1f;
1502 cbp->fc_delay_msb = 0x01;
1503 cbp->pri_fc_thresh = 3;
1504 cbp->tx_fc_dis = 0; /* enable transmit FC */
1505 cbp->rx_fc_restop = 1; /* enable FC restop frames */
1506 cbp->rx_fc_restart = 1; /* enable FC restart frames */
1507 cbp->fc_filter = !prm; /* drop FC frames to host */
1508 cbp->pri_fc_loc = 1; /* FC pri location (byte31) */
1509 }
1510
1511 FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1512
1513 /*
1514 * Start the config command/DMA.
1515 */
1516 fxp_scb_wait(sc);
1517 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDCONFIGOFF);
1518 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1519 /* ...and wait for it to complete. */
1520 i = 1000;
1521 do {
1522 FXP_CDCONFIGSYNC(sc,
1523 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1524 DELAY(1);
1525 } while ((le16toh(cbp->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
1526 if (i == 0) {
1527 printf("%s at line %d: dmasync timeout\n",
1528 sc->sc_dev.dv_xname, __LINE__);
1529 return ETIMEDOUT;
1530 }
1531
1532 /*
1533 * Initialize the station address.
1534 */
1535 cb_ias = &sc->sc_control_data->fcd_iascb;
1536 /* BIG_ENDIAN: no need to swap to store 0 */
1537 cb_ias->cb_status = 0;
1538 cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
1539 /* BIG_ENDIAN: no need to swap to store 0xffffffff */
1540 cb_ias->link_addr = 0xffffffff;
1541 memcpy((void *)cb_ias->macaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1542
1543 FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1544
1545 /*
1546 * Start the IAS (Individual Address Setup) command/DMA.
1547 */
1548 fxp_scb_wait(sc);
1549 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDIASOFF);
1550 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1551 /* ...and wait for it to complete. */
1552 i = 1000;
1553 do {
1554 FXP_CDIASSYNC(sc,
1555 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1556 DELAY(1);
1557 } while ((le16toh(cb_ias->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
1558 if (i == 0) {
1559 printf("%s at line %d: dmasync timeout\n",
1560 sc->sc_dev.dv_xname, __LINE__);
1561 return ETIMEDOUT;
1562 }
1563
1564 /*
1565 * Initialize the transmit descriptor ring. txlast is initialized
1566 * to the end of the list so that it will wrap around to the first
1567 * descriptor when the first packet is transmitted.
1568 */
1569 for (i = 0; i < FXP_NTXCB; i++) {
1570 txd = FXP_CDTX(sc, i);
1571 memset(txd, 0, sizeof(*txd));
1572 txd->txd_txcb.cb_command =
1573 htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
1574 txd->txd_txcb.link_addr =
1575 htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(i)));
1576 if (sc->sc_flags & FXPF_EXT_TXCB)
1577 txd->txd_txcb.tbd_array_addr =
1578 htole32(FXP_CDTBDADDR(sc, i) +
1579 (2 * sizeof(struct fxp_tbd)));
1580 else
1581 txd->txd_txcb.tbd_array_addr =
1582 htole32(FXP_CDTBDADDR(sc, i));
1583 FXP_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1584 }
1585 sc->sc_txpending = 0;
1586 sc->sc_txdirty = 0;
1587 sc->sc_txlast = FXP_NTXCB - 1;
1588
1589 /*
1590 * Initialize the receive buffer list.
1591 */
1592 sc->sc_rxq.ifq_maxlen = FXP_NRFABUFS;
1593 while (sc->sc_rxq.ifq_len < FXP_NRFABUFS) {
1594 rxmap = FXP_RXMAP_GET(sc);
1595 if ((error = fxp_add_rfabuf(sc, rxmap, 0)) != 0) {
1596 printf("%s: unable to allocate or map rx "
1597 "buffer %d, error = %d\n",
1598 sc->sc_dev.dv_xname,
1599 sc->sc_rxq.ifq_len, error);
1600 /*
1601 * XXX Should attempt to run with fewer receive
1602 * XXX buffers instead of just failing.
1603 */
1604 FXP_RXMAP_PUT(sc, rxmap);
1605 fxp_rxdrain(sc);
1606 goto out;
1607 }
1608 }
1609 sc->sc_rxidle = 0;
1610
1611 /*
1612 * Give the transmit ring to the chip. We do this by pointing
1613 * the chip at the last descriptor (which is a NOP|SUSPEND), and
1614 * issuing a start command. It will execute the NOP and then
1615 * suspend, pointing at the first descriptor.
1616 */
1617 fxp_scb_wait(sc);
1618 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, FXP_CDTXADDR(sc, sc->sc_txlast));
1619 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1620
1621 /*
1622 * Initialize receiver buffer area - RFA.
1623 */
1624 rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
1625 fxp_scb_wait(sc);
1626 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1627 rxmap->dm_segs[0].ds_addr + RFA_ALIGNMENT_FUDGE);
1628 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1629
1630 if (sc->sc_flags & FXPF_MII) {
1631 /*
1632 * Set current media.
1633 */
1634 mii_mediachg(&sc->sc_mii);
1635 }
1636
1637 /*
1638 * ...all done!
1639 */
1640 ifp->if_flags |= IFF_RUNNING;
1641 ifp->if_flags &= ~IFF_OACTIVE;
1642
1643 /*
1644 * Start the one second timer.
1645 */
1646 callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
1647
1648 /*
1649 * Attempt to start output on the interface.
1650 */
1651 fxp_start(ifp);
1652
1653 out:
1654 if (error) {
1655 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1656 ifp->if_timer = 0;
1657 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1658 }
1659 return (error);
1660 }
1661
1662 /*
1663 * Change media according to request.
1664 */
1665 int
1666 fxp_mii_mediachange(struct ifnet *ifp)
1667 {
1668 struct fxp_softc *sc = ifp->if_softc;
1669
1670 if (ifp->if_flags & IFF_UP)
1671 mii_mediachg(&sc->sc_mii);
1672 return (0);
1673 }
1674
1675 /*
1676 * Notify the world which media we're using.
1677 */
1678 void
1679 fxp_mii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1680 {
1681 struct fxp_softc *sc = ifp->if_softc;
1682
1683 if(sc->sc_enabled == 0) {
1684 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1685 ifmr->ifm_status = 0;
1686 return;
1687 }
1688
1689 mii_pollstat(&sc->sc_mii);
1690 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1691 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1692 }
1693
1694 int
1695 fxp_80c24_mediachange(struct ifnet *ifp)
1696 {
1697
1698 /* Nothing to do here. */
1699 return (0);
1700 }
1701
1702 void
1703 fxp_80c24_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1704 {
1705 struct fxp_softc *sc = ifp->if_softc;
1706
1707 /*
1708 * Media is currently-selected media. We cannot determine
1709 * the link status.
1710 */
1711 ifmr->ifm_status = 0;
1712 ifmr->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
1713 }
1714
1715 /*
1716 * Add a buffer to the end of the RFA buffer list.
1717 * Return 0 if successful, error code on failure.
1718 *
1719 * The RFA struct is stuck at the beginning of mbuf cluster and the
1720 * data pointer is fixed up to point just past it.
1721 */
1722 int
1723 fxp_add_rfabuf(struct fxp_softc *sc, bus_dmamap_t rxmap, int unload)
1724 {
1725 struct mbuf *m;
1726 int error;
1727
1728 MGETHDR(m, M_DONTWAIT, MT_DATA);
1729 if (m == NULL)
1730 return (ENOBUFS);
1731
1732 MCLGET(m, M_DONTWAIT);
1733 if ((m->m_flags & M_EXT) == 0) {
1734 m_freem(m);
1735 return (ENOBUFS);
1736 }
1737
1738 if (unload)
1739 bus_dmamap_unload(sc->sc_dmat, rxmap);
1740
1741 M_SETCTX(m, rxmap);
1742
1743 error = bus_dmamap_load(sc->sc_dmat, rxmap,
1744 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1745 BUS_DMA_READ|BUS_DMA_NOWAIT);
1746 if (error) {
1747 printf("%s: can't load rx DMA map %d, error = %d\n",
1748 sc->sc_dev.dv_xname, sc->sc_rxq.ifq_len, error);
1749 panic("fxp_add_rfabuf"); /* XXX */
1750 }
1751
1752 FXP_INIT_RFABUF(sc, m);
1753
1754 return (0);
1755 }
1756
1757 int
1758 fxp_mdi_read(struct device *self, int phy, int reg)
1759 {
1760 struct fxp_softc *sc = (struct fxp_softc *)self;
1761 int count = 10000;
1762 int value;
1763
1764 CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1765 (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1766
1767 while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1768 && count--)
1769 DELAY(10);
1770
1771 if (count <= 0)
1772 printf("%s: fxp_mdi_read: timed out\n", sc->sc_dev.dv_xname);
1773
1774 return (value & 0xffff);
1775 }
1776
1777 void
1778 fxp_statchg(struct device *self)
1779 {
1780 struct fxp_softc *sc = (void *) self;
1781
1782 /*
1783 * Determine whether or not we have to work-around the
1784 * Resume Bug.
1785 */
1786 if (sc->sc_flags & FXPF_HAS_RESUME_BUG) {
1787 if (IFM_TYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
1788 sc->sc_flags |= FXPF_FIX_RESUME_BUG;
1789 else
1790 sc->sc_flags &= ~FXPF_FIX_RESUME_BUG;
1791 }
1792 }
1793
1794 void
1795 fxp_mdi_write(struct device *self, int phy, int reg, int value)
1796 {
1797 struct fxp_softc *sc = (struct fxp_softc *)self;
1798 int count = 10000;
1799
1800 CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1801 (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1802 (value & 0xffff));
1803
1804 while((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1805 count--)
1806 DELAY(10);
1807
1808 if (count <= 0)
1809 printf("%s: fxp_mdi_write: timed out\n", sc->sc_dev.dv_xname);
1810 }
1811
1812 int
1813 fxp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1814 {
1815 struct fxp_softc *sc = ifp->if_softc;
1816 struct ifreq *ifr = (struct ifreq *)data;
1817 int s, error;
1818
1819 s = splnet();
1820
1821 switch (cmd) {
1822 case SIOCSIFMEDIA:
1823 case SIOCGIFMEDIA:
1824 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1825 break;
1826
1827 default:
1828 error = ether_ioctl(ifp, cmd, data);
1829 if (error == ENETRESET) {
1830 if (sc->sc_enabled) {
1831 /*
1832 * Multicast list has changed; set the
1833 * hardware filter accordingly.
1834 */
1835 if (sc->sc_txpending) {
1836 sc->sc_flags |= FXPF_WANTINIT;
1837 error = 0;
1838 } else
1839 error = fxp_init(ifp);
1840 } else
1841 error = 0;
1842 }
1843 break;
1844 }
1845
1846 /* Try to get more packets going. */
1847 if (sc->sc_enabled)
1848 fxp_start(ifp);
1849
1850 splx(s);
1851 return (error);
1852 }
1853
1854 /*
1855 * Program the multicast filter.
1856 *
1857 * This function must be called at splnet().
1858 */
1859 void
1860 fxp_mc_setup(struct fxp_softc *sc)
1861 {
1862 struct fxp_cb_mcs *mcsp = &sc->sc_control_data->fcd_mcscb;
1863 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1864 struct ethercom *ec = &sc->sc_ethercom;
1865 struct ether_multi *enm;
1866 struct ether_multistep step;
1867 int count, nmcasts;
1868
1869 #ifdef DIAGNOSTIC
1870 if (sc->sc_txpending)
1871 panic("fxp_mc_setup: pending transmissions");
1872 #endif
1873
1874 ifp->if_flags &= ~IFF_ALLMULTI;
1875
1876 /*
1877 * Initialize multicast setup descriptor.
1878 */
1879 nmcasts = 0;
1880 ETHER_FIRST_MULTI(step, ec, enm);
1881 while (enm != NULL) {
1882 /*
1883 * Check for too many multicast addresses or if we're
1884 * listening to a range. Either way, we simply have
1885 * to accept all multicasts.
1886 */
1887 if (nmcasts >= MAXMCADDR ||
1888 memcmp(enm->enm_addrlo, enm->enm_addrhi,
1889 ETHER_ADDR_LEN) != 0) {
1890 /*
1891 * Callers of this function must do the
1892 * right thing with this. If we're called
1893 * from outside fxp_init(), the caller must
1894 * detect if the state if IFF_ALLMULTI changes.
1895 * If it does, the caller must then call
1896 * fxp_init(), since allmulti is handled by
1897 * the config block.
1898 */
1899 ifp->if_flags |= IFF_ALLMULTI;
1900 return;
1901 }
1902 memcpy((void *)&mcsp->mc_addr[nmcasts][0], enm->enm_addrlo,
1903 ETHER_ADDR_LEN);
1904 nmcasts++;
1905 ETHER_NEXT_MULTI(step, enm);
1906 }
1907
1908 /* BIG_ENDIAN: no need to swap to store 0 */
1909 mcsp->cb_status = 0;
1910 mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
1911 mcsp->link_addr = htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(sc->sc_txlast)));
1912 mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
1913
1914 FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1915
1916 /*
1917 * Wait until the command unit is not active. This should never
1918 * happen since nothing is queued, but make sure anyway.
1919 */
1920 count = 100;
1921 while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
1922 FXP_SCB_CUS_ACTIVE && --count)
1923 DELAY(1);
1924 if (count == 0) {
1925 printf("%s at line %d: command queue timeout\n",
1926 sc->sc_dev.dv_xname, __LINE__);
1927 return;
1928 }
1929
1930 /*
1931 * Start the multicast setup command/DMA.
1932 */
1933 fxp_scb_wait(sc);
1934 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDMCSOFF);
1935 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1936
1937 /* ...and wait for it to complete. */
1938 count = 1000;
1939 do {
1940 FXP_CDMCSSYNC(sc,
1941 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1942 DELAY(1);
1943 } while ((le16toh(mcsp->cb_status) & FXP_CB_STATUS_C) == 0 && --count);
1944 if (count == 0) {
1945 printf("%s at line %d: dmasync timeout\n",
1946 sc->sc_dev.dv_xname, __LINE__);
1947 return;
1948 }
1949 }
1950
1951 int
1952 fxp_enable(struct fxp_softc *sc)
1953 {
1954
1955 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1956 if ((*sc->sc_enable)(sc) != 0) {
1957 printf("%s: device enable failed\n",
1958 sc->sc_dev.dv_xname);
1959 return (EIO);
1960 }
1961 }
1962
1963 sc->sc_enabled = 1;
1964 return (0);
1965 }
1966
1967 void
1968 fxp_disable(struct fxp_softc *sc)
1969 {
1970
1971 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1972 (*sc->sc_disable)(sc);
1973 sc->sc_enabled = 0;
1974 }
1975 }
1976
1977 /*
1978 * fxp_activate:
1979 *
1980 * Handle device activation/deactivation requests.
1981 */
1982 int
1983 fxp_activate(struct device *self, enum devact act)
1984 {
1985 struct fxp_softc *sc = (void *) self;
1986 int s, error = 0;
1987
1988 s = splnet();
1989 switch (act) {
1990 case DVACT_ACTIVATE:
1991 error = EOPNOTSUPP;
1992 break;
1993
1994 case DVACT_DEACTIVATE:
1995 if (sc->sc_flags & FXPF_MII)
1996 mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
1997 MII_OFFSET_ANY);
1998 if_deactivate(&sc->sc_ethercom.ec_if);
1999 break;
2000 }
2001 splx(s);
2002
2003 return (error);
2004 }
2005
2006 /*
2007 * fxp_detach:
2008 *
2009 * Detach an i82557 interface.
2010 */
2011 int
2012 fxp_detach(struct fxp_softc *sc)
2013 {
2014 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2015 int i;
2016
2017 /* Succeed now if there's no work to do. */
2018 if ((sc->sc_flags & FXPF_ATTACHED) == 0)
2019 return (0);
2020
2021 /* Unhook our tick handler. */
2022 callout_stop(&sc->sc_callout);
2023
2024 if (sc->sc_flags & FXPF_MII) {
2025 /* Detach all PHYs */
2026 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
2027 }
2028
2029 /* Delete all remaining media. */
2030 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
2031
2032 #if NRND > 0
2033 rnd_detach_source(&sc->rnd_source);
2034 #endif
2035 ether_ifdetach(ifp);
2036 if_detach(ifp);
2037
2038 for (i = 0; i < FXP_NRFABUFS; i++) {
2039 bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmaps[i]);
2040 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
2041 }
2042
2043 for (i = 0; i < FXP_NTXCB; i++) {
2044 bus_dmamap_unload(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
2045 bus_dmamap_destroy(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
2046 }
2047
2048 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
2049 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
2050 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
2051 sizeof(struct fxp_control_data));
2052 bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
2053
2054 shutdownhook_disestablish(sc->sc_sdhook);
2055 powerhook_disestablish(sc->sc_powerhook);
2056
2057 return (0);
2058 }
2059