i82557.c revision 1.62 1 /* $NetBSD: i82557.c,v 1.62 2002/04/04 21:11:16 thorpej 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.62 2002/04/04 21:11:16 thorpej 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 static void
583 fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int len)
584 {
585 uint16_t reg;
586 int x;
587
588 for (x = 1 << (len - 1); x != 0; x >>= 1) {
589 if (data & x)
590 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
591 else
592 reg = FXP_EEPROM_EECS;
593 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
594 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
595 reg | FXP_EEPROM_EESK);
596 DELAY(4);
597 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
598 DELAY(4);
599 }
600 }
601
602 /*
603 * Figure out EEPROM size.
604 *
605 * 559's can have either 64-word or 256-word EEPROMs, the 558
606 * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
607 * talks about the existance of 16 to 256 word EEPROMs.
608 *
609 * The only known sizes are 64 and 256, where the 256 version is used
610 * by CardBus cards to store CIS information.
611 *
612 * The address is shifted in msb-to-lsb, and after the last
613 * address-bit the EEPROM is supposed to output a `dummy zero' bit,
614 * after which follows the actual data. We try to detect this zero, by
615 * probing the data-out bit in the EEPROM control register just after
616 * having shifted in a bit. If the bit is zero, we assume we've
617 * shifted enough address bits. The data-out should be tri-state,
618 * before this, which should translate to a logical one.
619 *
620 * Other ways to do this would be to try to read a register with known
621 * contents with a varying number of address bits, but no such
622 * register seem to be available. The high bits of register 10 are 01
623 * on the 558 and 559, but apparently not on the 557.
624 *
625 * The Linux driver computes a checksum on the EEPROM data, but the
626 * value of this checksum is not very well documented.
627 */
628
629 void
630 fxp_autosize_eeprom(struct fxp_softc *sc)
631 {
632 int x;
633
634 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
635
636 /* Shift in read opcode. */
637 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
638
639 /*
640 * Shift in address, wait for the dummy zero following a correct
641 * address shift.
642 */
643 for (x = 1; x <= 8; x++) {
644 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
645 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
646 FXP_EEPROM_EECS | FXP_EEPROM_EESK);
647 DELAY(4);
648 if((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
649 FXP_EEPROM_EEDO) == 0)
650 break;
651 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
652 DELAY(4);
653 }
654 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
655 DELAY(4);
656 if(x != 6 && x != 8) {
657 #ifdef DEBUG
658 printf("%s: strange EEPROM size (%d)\n",
659 sc->sc_dev.dv_xname, 1 << x);
660 #endif
661 } else
662 sc->sc_eeprom_size = x;
663 }
664
665 /*
666 * Read from the serial EEPROM. Basically, you manually shift in
667 * the read opcode (one bit at a time) and then shift in the address,
668 * and then you shift out the data (all of this one bit at a time).
669 * The word size is 16 bits, so you have to provide the address for
670 * every 16 bits of data.
671 */
672 void
673 fxp_read_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
674 {
675 u_int16_t reg;
676 int i, x;
677
678 for (i = 0; i < words; i++) {
679 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
680
681 /* Shift in read opcode. */
682 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
683
684 /* Shift in address. */
685 fxp_eeprom_shiftin(sc, i + offset, sc->sc_eeprom_size);
686
687 reg = FXP_EEPROM_EECS;
688 data[i] = 0;
689
690 /* Shift out data. */
691 for (x = 16; x > 0; x--) {
692 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
693 reg | FXP_EEPROM_EESK);
694 DELAY(4);
695 if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
696 FXP_EEPROM_EEDO)
697 data[i] |= (1 << (x - 1));
698 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
699 DELAY(4);
700 }
701 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
702 DELAY(4);
703 }
704 }
705
706 /*
707 * Start packet transmission on the interface.
708 */
709 void
710 fxp_start(struct ifnet *ifp)
711 {
712 struct fxp_softc *sc = ifp->if_softc;
713 struct mbuf *m0, *m;
714 struct fxp_txdesc *txd;
715 struct fxp_txsoft *txs;
716 bus_dmamap_t dmamap;
717 int error, lasttx, nexttx, opending, seg;
718
719 /*
720 * If we want a re-init, bail out now.
721 */
722 if (sc->sc_flags & FXPF_WANTINIT) {
723 ifp->if_flags |= IFF_OACTIVE;
724 return;
725 }
726
727 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
728 return;
729
730 /*
731 * Remember the previous txpending and the current lasttx.
732 */
733 opending = sc->sc_txpending;
734 lasttx = sc->sc_txlast;
735
736 /*
737 * Loop through the send queue, setting up transmit descriptors
738 * until we drain the queue, or use up all available transmit
739 * descriptors.
740 */
741 for (;;) {
742 /*
743 * Grab a packet off the queue.
744 */
745 IFQ_POLL(&ifp->if_snd, m0);
746 if (m0 == NULL)
747 break;
748 m = NULL;
749
750 if (sc->sc_txpending == FXP_NTXCB) {
751 FXP_EVCNT_INCR(&sc->sc_ev_txstall);
752 break;
753 }
754
755 /*
756 * Get the next available transmit descriptor.
757 */
758 nexttx = FXP_NEXTTX(sc->sc_txlast);
759 txd = FXP_CDTX(sc, nexttx);
760 txs = FXP_DSTX(sc, nexttx);
761 dmamap = txs->txs_dmamap;
762
763 /*
764 * Load the DMA map. If this fails, the packet either
765 * didn't fit in the allotted number of frags, or we were
766 * short on resources. In this case, we'll copy and try
767 * again.
768 */
769 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
770 BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
771 MGETHDR(m, M_DONTWAIT, MT_DATA);
772 if (m == NULL) {
773 printf("%s: unable to allocate Tx mbuf\n",
774 sc->sc_dev.dv_xname);
775 break;
776 }
777 if (m0->m_pkthdr.len > MHLEN) {
778 MCLGET(m, M_DONTWAIT);
779 if ((m->m_flags & M_EXT) == 0) {
780 printf("%s: unable to allocate Tx "
781 "cluster\n", sc->sc_dev.dv_xname);
782 m_freem(m);
783 break;
784 }
785 }
786 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
787 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
788 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
789 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
790 if (error) {
791 printf("%s: unable to load Tx buffer, "
792 "error = %d\n", sc->sc_dev.dv_xname, error);
793 break;
794 }
795 }
796
797 IFQ_DEQUEUE(&ifp->if_snd, m0);
798 if (m != NULL) {
799 m_freem(m0);
800 m0 = m;
801 }
802
803 /* Initialize the fraglist. */
804 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
805 txd->txd_tbd[seg].tb_addr =
806 htole32(dmamap->dm_segs[seg].ds_addr);
807 txd->txd_tbd[seg].tb_size =
808 htole32(dmamap->dm_segs[seg].ds_len);
809 }
810
811 /* Sync the DMA map. */
812 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
813 BUS_DMASYNC_PREWRITE);
814
815 /*
816 * Store a pointer to the packet so we can free it later.
817 */
818 txs->txs_mbuf = m0;
819
820 /*
821 * Initialize the transmit descriptor.
822 */
823 /* BIG_ENDIAN: no need to swap to store 0 */
824 txd->txd_txcb.cb_status = 0;
825 txd->txd_txcb.cb_command =
826 htole16(FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF);
827 txd->txd_txcb.tx_threshold = tx_threshold;
828 txd->txd_txcb.tbd_number = dmamap->dm_nsegs;
829
830 FXP_CDTXSYNC(sc, nexttx,
831 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
832
833 /* Advance the tx pointer. */
834 sc->sc_txpending++;
835 sc->sc_txlast = nexttx;
836
837 #if NBPFILTER > 0
838 /*
839 * Pass packet to bpf if there is a listener.
840 */
841 if (ifp->if_bpf)
842 bpf_mtap(ifp->if_bpf, m0);
843 #endif
844 }
845
846 if (sc->sc_txpending == FXP_NTXCB) {
847 /* No more slots; notify upper layer. */
848 ifp->if_flags |= IFF_OACTIVE;
849 }
850
851 if (sc->sc_txpending != opending) {
852 /*
853 * We enqueued packets. If the transmitter was idle,
854 * reset the txdirty pointer.
855 */
856 if (opending == 0)
857 sc->sc_txdirty = FXP_NEXTTX(lasttx);
858
859 /*
860 * Cause the chip to interrupt and suspend command
861 * processing once the last packet we've enqueued
862 * has been transmitted.
863 */
864 FXP_CDTX(sc, sc->sc_txlast)->txd_txcb.cb_command |=
865 htole16(FXP_CB_COMMAND_I | FXP_CB_COMMAND_S);
866 FXP_CDTXSYNC(sc, sc->sc_txlast,
867 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
868
869 /*
870 * The entire packet chain is set up. Clear the suspend bit
871 * on the command prior to the first packet we set up.
872 */
873 FXP_CDTXSYNC(sc, lasttx,
874 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
875 FXP_CDTX(sc, lasttx)->txd_txcb.cb_command &=
876 htole16(~FXP_CB_COMMAND_S);
877 FXP_CDTXSYNC(sc, lasttx,
878 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
879
880 /*
881 * Issue a Resume command in case the chip was suspended.
882 */
883 fxp_scb_wait(sc);
884 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
885
886 /* Set a watchdog timer in case the chip flakes out. */
887 ifp->if_timer = 5;
888 }
889 }
890
891 /*
892 * Process interface interrupts.
893 */
894 int
895 fxp_intr(void *arg)
896 {
897 struct fxp_softc *sc = arg;
898 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
899 bus_dmamap_t rxmap;
900 int claimed = 0;
901 u_int8_t statack;
902
903 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
904 return (0);
905 /*
906 * If the interface isn't running, don't try to
907 * service the interrupt.. just ack it and bail.
908 */
909 if ((ifp->if_flags & IFF_RUNNING) == 0) {
910 statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
911 if (statack) {
912 claimed = 1;
913 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
914 }
915 return (claimed);
916 }
917
918 while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
919 claimed = 1;
920
921 /*
922 * First ACK all the interrupts in this pass.
923 */
924 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
925
926 /*
927 * Process receiver interrupts. If a no-resource (RNR)
928 * condition exists, get whatever packets we can and
929 * re-start the receiver.
930 */
931 if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
932 FXP_EVCNT_INCR(&sc->sc_ev_rxintr);
933 fxp_rxintr(sc);
934 }
935
936 if (statack & FXP_SCB_STATACK_RNR) {
937 rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
938 fxp_scb_wait(sc);
939 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
940 rxmap->dm_segs[0].ds_addr +
941 RFA_ALIGNMENT_FUDGE);
942 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
943 }
944
945 /*
946 * Free any finished transmit mbuf chains.
947 */
948 if (statack & (FXP_SCB_STATACK_CXTNO|FXP_SCB_STATACK_CNA)) {
949 FXP_EVCNT_INCR(&sc->sc_ev_txintr);
950 fxp_txintr(sc);
951
952 /*
953 * Try to get more packets going.
954 */
955 fxp_start(ifp);
956
957 if (sc->sc_txpending == 0) {
958 /*
959 * If we want a re-init, do that now.
960 */
961 if (sc->sc_flags & FXPF_WANTINIT)
962 (void) fxp_init(ifp);
963 }
964 }
965 }
966
967 #if NRND > 0
968 if (claimed)
969 rnd_add_uint32(&sc->rnd_source, statack);
970 #endif
971 return (claimed);
972 }
973
974 /*
975 * Handle transmit completion interrupts.
976 */
977 void
978 fxp_txintr(struct fxp_softc *sc)
979 {
980 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
981 struct fxp_txdesc *txd;
982 struct fxp_txsoft *txs;
983 int i;
984 u_int16_t txstat;
985
986 ifp->if_flags &= ~IFF_OACTIVE;
987 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
988 i = FXP_NEXTTX(i), sc->sc_txpending--) {
989 txd = FXP_CDTX(sc, i);
990 txs = FXP_DSTX(sc, i);
991
992 FXP_CDTXSYNC(sc, i,
993 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
994
995 txstat = le16toh(txd->txd_txcb.cb_status);
996
997 if ((txstat & FXP_CB_STATUS_C) == 0)
998 break;
999
1000 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1001 0, txs->txs_dmamap->dm_mapsize,
1002 BUS_DMASYNC_POSTWRITE);
1003 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1004 m_freem(txs->txs_mbuf);
1005 txs->txs_mbuf = NULL;
1006 }
1007
1008 /* Update the dirty transmit buffer pointer. */
1009 sc->sc_txdirty = i;
1010
1011 /*
1012 * Cancel the watchdog timer if there are no pending
1013 * transmissions.
1014 */
1015 if (sc->sc_txpending == 0)
1016 ifp->if_timer = 0;
1017 }
1018
1019 /*
1020 * Handle receive interrupts.
1021 */
1022 void
1023 fxp_rxintr(struct fxp_softc *sc)
1024 {
1025 struct ethercom *ec = &sc->sc_ethercom;
1026 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1027 struct mbuf *m, *m0;
1028 bus_dmamap_t rxmap;
1029 struct fxp_rfa *rfa;
1030 u_int16_t len, rxstat;
1031
1032 for (;;) {
1033 m = sc->sc_rxq.ifq_head;
1034 rfa = FXP_MTORFA(m);
1035 rxmap = M_GETCTX(m, bus_dmamap_t);
1036
1037 FXP_RFASYNC(sc, m,
1038 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1039
1040 rxstat = le16toh(rfa->rfa_status);
1041
1042 if ((rxstat & FXP_RFA_STATUS_C) == 0) {
1043 /*
1044 * We have processed all of the
1045 * receive buffers.
1046 */
1047 FXP_RFASYNC(sc, m, BUS_DMASYNC_PREREAD);
1048 return;
1049 }
1050
1051 IF_DEQUEUE(&sc->sc_rxq, m);
1052
1053 FXP_RXBUFSYNC(sc, m, BUS_DMASYNC_POSTREAD);
1054
1055 len = le16toh(rfa->actual_size) &
1056 (m->m_ext.ext_size - 1);
1057
1058 if (len < sizeof(struct ether_header)) {
1059 /*
1060 * Runt packet; drop it now.
1061 */
1062 FXP_INIT_RFABUF(sc, m);
1063 continue;
1064 }
1065
1066 /*
1067 * If support for 802.1Q VLAN sized frames is
1068 * enabled, we need to do some additional error
1069 * checking (as we are saving bad frames, in
1070 * order to receive the larger ones).
1071 */
1072 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) != 0 &&
1073 (rxstat & (FXP_RFA_STATUS_OVERRUN|
1074 FXP_RFA_STATUS_RNR|
1075 FXP_RFA_STATUS_ALIGN|
1076 FXP_RFA_STATUS_CRC)) != 0) {
1077 FXP_INIT_RFABUF(sc, m);
1078 continue;
1079 }
1080
1081 /*
1082 * If the packet is small enough to fit in a
1083 * single header mbuf, allocate one and copy
1084 * the data into it. This greatly reduces
1085 * memory consumption when we receive lots
1086 * of small packets.
1087 *
1088 * Otherwise, we add a new buffer to the receive
1089 * chain. If this fails, we drop the packet and
1090 * recycle the old buffer.
1091 */
1092 if (fxp_copy_small != 0 && len <= MHLEN) {
1093 MGETHDR(m0, M_DONTWAIT, MT_DATA);
1094 if (m == NULL)
1095 goto dropit;
1096 memcpy(mtod(m0, caddr_t),
1097 mtod(m, caddr_t), len);
1098 FXP_INIT_RFABUF(sc, m);
1099 m = m0;
1100 } else {
1101 if (fxp_add_rfabuf(sc, rxmap, 1) != 0) {
1102 dropit:
1103 ifp->if_ierrors++;
1104 FXP_INIT_RFABUF(sc, m);
1105 continue;
1106 }
1107 }
1108
1109 m->m_pkthdr.rcvif = ifp;
1110 m->m_pkthdr.len = m->m_len = len;
1111
1112 #if NBPFILTER > 0
1113 /*
1114 * Pass this up to any BPF listeners, but only
1115 * pass it up the stack it its for us.
1116 */
1117 if (ifp->if_bpf)
1118 bpf_mtap(ifp->if_bpf, m);
1119 #endif
1120
1121 /* Pass it on. */
1122 (*ifp->if_input)(ifp, m);
1123 }
1124 }
1125
1126 /*
1127 * Update packet in/out/collision statistics. The i82557 doesn't
1128 * allow you to access these counters without doing a fairly
1129 * expensive DMA to get _all_ of the statistics it maintains, so
1130 * we do this operation here only once per second. The statistics
1131 * counters in the kernel are updated from the previous dump-stats
1132 * DMA and then a new dump-stats DMA is started. The on-chip
1133 * counters are zeroed when the DMA completes. If we can't start
1134 * the DMA immediately, we don't wait - we just prepare to read
1135 * them again next time.
1136 */
1137 void
1138 fxp_tick(void *arg)
1139 {
1140 struct fxp_softc *sc = arg;
1141 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1142 struct fxp_stats *sp = &sc->sc_control_data->fcd_stats;
1143 int s;
1144
1145 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1146 return;
1147
1148 s = splnet();
1149
1150 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_POSTREAD);
1151
1152 ifp->if_opackets += le32toh(sp->tx_good);
1153 ifp->if_collisions += le32toh(sp->tx_total_collisions);
1154 if (sp->rx_good) {
1155 ifp->if_ipackets += le32toh(sp->rx_good);
1156 sc->sc_rxidle = 0;
1157 } else {
1158 sc->sc_rxidle++;
1159 }
1160 ifp->if_ierrors +=
1161 le32toh(sp->rx_crc_errors) +
1162 le32toh(sp->rx_alignment_errors) +
1163 le32toh(sp->rx_rnr_errors) +
1164 le32toh(sp->rx_overrun_errors);
1165 /*
1166 * If any transmit underruns occurred, bump up the transmit
1167 * threshold by another 512 bytes (64 * 8).
1168 */
1169 if (sp->tx_underruns) {
1170 ifp->if_oerrors += le32toh(sp->tx_underruns);
1171 if (tx_threshold < 192)
1172 tx_threshold += 64;
1173 }
1174
1175 /*
1176 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1177 * then assume the receiver has locked up and attempt to clear
1178 * the condition by reprogramming the multicast filter (actually,
1179 * resetting the interface). This is a work-around for a bug in
1180 * the 82557 where the receiver locks up if it gets certain types
1181 * of garbage in the syncronization bits prior to the packet header.
1182 * This bug is supposed to only occur in 10Mbps mode, but has been
1183 * seen to occur in 100Mbps mode as well (perhaps due to a 10/100
1184 * speed transition).
1185 */
1186 if (sc->sc_rxidle > FXP_MAX_RX_IDLE) {
1187 (void) fxp_init(ifp);
1188 splx(s);
1189 return;
1190 }
1191 /*
1192 * If there is no pending command, start another stats
1193 * dump. Otherwise punt for now.
1194 */
1195 if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1196 /*
1197 * Start another stats dump.
1198 */
1199 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
1200 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1201 } else {
1202 /*
1203 * A previous command is still waiting to be accepted.
1204 * Just zero our copy of the stats and wait for the
1205 * next timer event to update them.
1206 */
1207 /* BIG_ENDIAN: no swap required to store 0 */
1208 sp->tx_good = 0;
1209 sp->tx_underruns = 0;
1210 sp->tx_total_collisions = 0;
1211
1212 sp->rx_good = 0;
1213 sp->rx_crc_errors = 0;
1214 sp->rx_alignment_errors = 0;
1215 sp->rx_rnr_errors = 0;
1216 sp->rx_overrun_errors = 0;
1217 }
1218
1219 if (sc->sc_flags & FXPF_MII) {
1220 /* Tick the MII clock. */
1221 mii_tick(&sc->sc_mii);
1222 }
1223
1224 splx(s);
1225
1226 /*
1227 * Schedule another timeout one second from now.
1228 */
1229 callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
1230 }
1231
1232 /*
1233 * Drain the receive queue.
1234 */
1235 void
1236 fxp_rxdrain(struct fxp_softc *sc)
1237 {
1238 bus_dmamap_t rxmap;
1239 struct mbuf *m;
1240
1241 for (;;) {
1242 IF_DEQUEUE(&sc->sc_rxq, m);
1243 if (m == NULL)
1244 break;
1245 rxmap = M_GETCTX(m, bus_dmamap_t);
1246 bus_dmamap_unload(sc->sc_dmat, rxmap);
1247 FXP_RXMAP_PUT(sc, rxmap);
1248 m_freem(m);
1249 }
1250 }
1251
1252 /*
1253 * Stop the interface. Cancels the statistics updater and resets
1254 * the interface.
1255 */
1256 void
1257 fxp_stop(struct ifnet *ifp, int disable)
1258 {
1259 struct fxp_softc *sc = ifp->if_softc;
1260 struct fxp_txsoft *txs;
1261 int i;
1262
1263 /*
1264 * Turn down interface (done early to avoid bad interactions
1265 * between panics, shutdown hooks, and the watchdog timer)
1266 */
1267 ifp->if_timer = 0;
1268 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1269
1270 /*
1271 * Cancel stats updater.
1272 */
1273 callout_stop(&sc->sc_callout);
1274 if (sc->sc_flags & FXPF_MII) {
1275 /* Down the MII. */
1276 mii_down(&sc->sc_mii);
1277 }
1278
1279 /*
1280 * Issue software reset
1281 */
1282 CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1283 DELAY(10);
1284
1285 /*
1286 * Release any xmit buffers.
1287 */
1288 for (i = 0; i < FXP_NTXCB; i++) {
1289 txs = FXP_DSTX(sc, i);
1290 if (txs->txs_mbuf != NULL) {
1291 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1292 m_freem(txs->txs_mbuf);
1293 txs->txs_mbuf = NULL;
1294 }
1295 }
1296 sc->sc_txpending = 0;
1297
1298 if (disable) {
1299 fxp_rxdrain(sc);
1300 fxp_disable(sc);
1301 }
1302
1303 }
1304
1305 /*
1306 * Watchdog/transmission transmit timeout handler. Called when a
1307 * transmission is started on the interface, but no interrupt is
1308 * received before the timeout. This usually indicates that the
1309 * card has wedged for some reason.
1310 */
1311 void
1312 fxp_watchdog(struct ifnet *ifp)
1313 {
1314 struct fxp_softc *sc = ifp->if_softc;
1315
1316 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1317 ifp->if_oerrors++;
1318
1319 (void) fxp_init(ifp);
1320 }
1321
1322 /*
1323 * Initialize the interface. Must be called at splnet().
1324 */
1325 int
1326 fxp_init(struct ifnet *ifp)
1327 {
1328 struct fxp_softc *sc = ifp->if_softc;
1329 struct fxp_cb_config *cbp;
1330 struct fxp_cb_ias *cb_ias;
1331 struct fxp_txdesc *txd;
1332 bus_dmamap_t rxmap;
1333 int i, prm, save_bf, lrxen, allm, error = 0;
1334
1335 if ((error = fxp_enable(sc)) != 0)
1336 goto out;
1337
1338 /*
1339 * Cancel any pending I/O
1340 */
1341 fxp_stop(ifp, 0);
1342
1343 /*
1344 * XXX just setting sc_flags to 0 here clears any FXPF_MII
1345 * flag, and this prevents the MII from detaching resulting in
1346 * a panic. The flags field should perhaps be split in runtime
1347 * flags and more static information. For now, just clear the
1348 * only other flag set.
1349 */
1350
1351 sc->sc_flags &= ~FXPF_WANTINIT;
1352
1353 /*
1354 * Initialize base of CBL and RFA memory. Loading with zero
1355 * sets it up for regular linear addressing.
1356 */
1357 fxp_scb_wait(sc);
1358 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1359 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1360
1361 fxp_scb_wait(sc);
1362 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1363
1364 /*
1365 * Initialize the multicast filter. Do this now, since we might
1366 * have to setup the config block differently.
1367 */
1368 fxp_mc_setup(sc);
1369
1370 prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1371 allm = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1372
1373 /*
1374 * In order to support receiving 802.1Q VLAN frames, we have to
1375 * enable "save bad frames", since they are 4 bytes larger than
1376 * the normal Ethernet maximum frame length. On i82558 and later,
1377 * we have a better mechanism for this.
1378 */
1379 save_bf = 0;
1380 lrxen = 0;
1381 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
1382 if (sc->sc_rev < FXP_REV_82558_A4)
1383 save_bf = 1;
1384 else
1385 lrxen = 1;
1386 }
1387
1388 /*
1389 * Initialize base of dump-stats buffer.
1390 */
1391 fxp_scb_wait(sc);
1392 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1393 sc->sc_cddma + FXP_CDSTATSOFF);
1394 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
1395 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1396
1397 cbp = &sc->sc_control_data->fcd_configcb;
1398 memset(cbp, 0, sizeof(struct fxp_cb_config));
1399
1400 /*
1401 * This copy is kind of disgusting, but there are a bunch of must be
1402 * zero and must be one bits in this structure and this is the easiest
1403 * way to initialize them all to proper values.
1404 */
1405 memcpy(cbp, fxp_cb_config_template, sizeof(fxp_cb_config_template));
1406
1407 /* BIG_ENDIAN: no need to swap to store 0 */
1408 cbp->cb_status = 0;
1409 cbp->cb_command = htole16(FXP_CB_COMMAND_CONFIG |
1410 FXP_CB_COMMAND_EL);
1411 /* BIG_ENDIAN: no need to swap to store 0xffffffff */
1412 cbp->link_addr = 0xffffffff; /* (no) next command */
1413 /* bytes in config block */
1414 cbp->byte_count = FXP_CONFIG_LEN;
1415 cbp->rx_fifo_limit = 8; /* rx fifo threshold (32 bytes) */
1416 cbp->tx_fifo_limit = 0; /* tx fifo threshold (0 bytes) */
1417 cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */
1418 cbp->mwi_enable = (sc->sc_flags & FXPF_MWI) ? 1 : 0;
1419 cbp->type_enable = 0; /* actually reserved */
1420 cbp->read_align_en = (sc->sc_flags & FXPF_READ_ALIGN) ? 1 : 0;
1421 cbp->end_wr_on_cl = (sc->sc_flags & FXPF_WRITE_ALIGN) ? 1 : 0;
1422 cbp->rx_dma_bytecount = 0; /* (no) rx DMA max */
1423 cbp->tx_dma_bytecount = 0; /* (no) tx DMA max */
1424 cbp->dma_mbce = 0; /* (disable) dma max counters */
1425 cbp->late_scb = 0; /* (don't) defer SCB update */
1426 cbp->tno_int_or_tco_en =0; /* (disable) tx not okay interrupt */
1427 cbp->ci_int = 1; /* interrupt on CU idle */
1428 cbp->ext_txcb_dis = (sc->sc_flags & FXPF_EXT_TXCB) ? 0 : 1;
1429 cbp->ext_stats_dis = 1; /* disable extended counters */
1430 cbp->keep_overrun_rx = 0; /* don't pass overrun frames to host */
1431 cbp->save_bf = save_bf;/* save bad frames */
1432 cbp->disc_short_rx = !prm; /* discard short packets */
1433 cbp->underrun_retry = 1; /* retry mode (1) on DMA underrun */
1434 cbp->two_frames = 0; /* do not limit FIFO to 2 frames */
1435 cbp->dyn_tbd = 0; /* (no) dynamic TBD mode */
1436 /* interface mode */
1437 cbp->mediatype = (sc->sc_flags & FXPF_MII) ? 1 : 0;
1438 cbp->csma_dis = 0; /* (don't) disable link */
1439 cbp->tcp_udp_cksum = 0; /* (don't) enable checksum */
1440 cbp->vlan_tco = 0; /* (don't) enable vlan wakeup */
1441 cbp->link_wake_en = 0; /* (don't) assert PME# on link change */
1442 cbp->arp_wake_en = 0; /* (don't) assert PME# on arp */
1443 cbp->mc_wake_en = 0; /* (don't) assert PME# on mcmatch */
1444 cbp->nsai = 1; /* (don't) disable source addr insert */
1445 cbp->preamble_length = 2; /* (7 byte) preamble */
1446 cbp->loopback = 0; /* (don't) loopback */
1447 cbp->linear_priority = 0; /* (normal CSMA/CD operation) */
1448 cbp->linear_pri_mode = 0; /* (wait after xmit only) */
1449 cbp->interfrm_spacing = 6; /* (96 bits of) interframe spacing */
1450 cbp->promiscuous = prm; /* promiscuous mode */
1451 cbp->bcast_disable = 0; /* (don't) disable broadcasts */
1452 cbp->wait_after_win = 0; /* (don't) enable modified backoff alg*/
1453 cbp->ignore_ul = 0; /* consider U/L bit in IA matching */
1454 cbp->crc16_en = 0; /* (don't) enable crc-16 algorithm */
1455 cbp->crscdt = (sc->sc_flags & FXPF_MII) ? 0 : 1;
1456 cbp->stripping = !prm; /* truncate rx packet to byte count */
1457 cbp->padding = 1; /* (do) pad short tx packets */
1458 cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */
1459 cbp->long_rx_en = lrxen; /* long packet receive enable */
1460 cbp->ia_wake_en = 0; /* (don't) wake up on address match */
1461 cbp->magic_pkt_dis = 0; /* (don't) disable magic packet */
1462 /* must set wake_en in PMCSR also */
1463 cbp->force_fdx = 0; /* (don't) force full duplex */
1464 cbp->fdx_pin_en = 1; /* (enable) FDX# pin */
1465 cbp->multi_ia = 0; /* (don't) accept multiple IAs */
1466 cbp->mc_all = allm; /* accept all multicasts */
1467
1468 if (sc->sc_rev < FXP_REV_82558_A4) {
1469 /*
1470 * The i82557 has no hardware flow control, the values
1471 * here are the defaults for the chip.
1472 */
1473 cbp->fc_delay_lsb = 0;
1474 cbp->fc_delay_msb = 0x40;
1475 cbp->pri_fc_thresh = 3;
1476 cbp->tx_fc_dis = 0;
1477 cbp->rx_fc_restop = 0;
1478 cbp->rx_fc_restart = 0;
1479 cbp->fc_filter = 0;
1480 cbp->pri_fc_loc = 1;
1481 } else {
1482 cbp->fc_delay_lsb = 0x1f;
1483 cbp->fc_delay_msb = 0x01;
1484 cbp->pri_fc_thresh = 3;
1485 cbp->tx_fc_dis = 0; /* enable transmit FC */
1486 cbp->rx_fc_restop = 1; /* enable FC restop frames */
1487 cbp->rx_fc_restart = 1; /* enable FC restart frames */
1488 cbp->fc_filter = !prm; /* drop FC frames to host */
1489 cbp->pri_fc_loc = 1; /* FC pri location (byte31) */
1490 }
1491
1492 FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1493
1494 /*
1495 * Start the config command/DMA.
1496 */
1497 fxp_scb_wait(sc);
1498 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDCONFIGOFF);
1499 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1500 /* ...and wait for it to complete. */
1501 i = 1000;
1502 do {
1503 FXP_CDCONFIGSYNC(sc,
1504 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1505 DELAY(1);
1506 } while ((le16toh(cbp->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
1507 if (i == 0) {
1508 printf("%s at line %d: dmasync timeout\n",
1509 sc->sc_dev.dv_xname, __LINE__);
1510 return ETIMEDOUT;
1511 }
1512
1513 /*
1514 * Initialize the station address.
1515 */
1516 cb_ias = &sc->sc_control_data->fcd_iascb;
1517 /* BIG_ENDIAN: no need to swap to store 0 */
1518 cb_ias->cb_status = 0;
1519 cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
1520 /* BIG_ENDIAN: no need to swap to store 0xffffffff */
1521 cb_ias->link_addr = 0xffffffff;
1522 memcpy((void *)cb_ias->macaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1523
1524 FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1525
1526 /*
1527 * Start the IAS (Individual Address Setup) command/DMA.
1528 */
1529 fxp_scb_wait(sc);
1530 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDIASOFF);
1531 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1532 /* ...and wait for it to complete. */
1533 i = 1000;
1534 do {
1535 FXP_CDIASSYNC(sc,
1536 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1537 DELAY(1);
1538 } while ((le16toh(cb_ias->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
1539 if (i == 0) {
1540 printf("%s at line %d: dmasync timeout\n",
1541 sc->sc_dev.dv_xname, __LINE__);
1542 return ETIMEDOUT;
1543 }
1544
1545 /*
1546 * Initialize the transmit descriptor ring. txlast is initialized
1547 * to the end of the list so that it will wrap around to the first
1548 * descriptor when the first packet is transmitted.
1549 */
1550 for (i = 0; i < FXP_NTXCB; i++) {
1551 txd = FXP_CDTX(sc, i);
1552 memset(txd, 0, sizeof(*txd));
1553 txd->txd_txcb.cb_command =
1554 htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
1555 txd->txd_txcb.link_addr =
1556 htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(i)));
1557 if (sc->sc_flags & FXPF_EXT_TXCB)
1558 txd->txd_txcb.tbd_array_addr =
1559 htole32(FXP_CDTBDADDR(sc, i) +
1560 (2 * sizeof(struct fxp_tbd)));
1561 else
1562 txd->txd_txcb.tbd_array_addr =
1563 htole32(FXP_CDTBDADDR(sc, i));
1564 FXP_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1565 }
1566 sc->sc_txpending = 0;
1567 sc->sc_txdirty = 0;
1568 sc->sc_txlast = FXP_NTXCB - 1;
1569
1570 /*
1571 * Initialize the receive buffer list.
1572 */
1573 sc->sc_rxq.ifq_maxlen = FXP_NRFABUFS;
1574 while (sc->sc_rxq.ifq_len < FXP_NRFABUFS) {
1575 rxmap = FXP_RXMAP_GET(sc);
1576 if ((error = fxp_add_rfabuf(sc, rxmap, 0)) != 0) {
1577 printf("%s: unable to allocate or map rx "
1578 "buffer %d, error = %d\n",
1579 sc->sc_dev.dv_xname,
1580 sc->sc_rxq.ifq_len, error);
1581 /*
1582 * XXX Should attempt to run with fewer receive
1583 * XXX buffers instead of just failing.
1584 */
1585 FXP_RXMAP_PUT(sc, rxmap);
1586 fxp_rxdrain(sc);
1587 goto out;
1588 }
1589 }
1590 sc->sc_rxidle = 0;
1591
1592 /*
1593 * Give the transmit ring to the chip. We do this by pointing
1594 * the chip at the last descriptor (which is a NOP|SUSPEND), and
1595 * issuing a start command. It will execute the NOP and then
1596 * suspend, pointing at the first descriptor.
1597 */
1598 fxp_scb_wait(sc);
1599 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, FXP_CDTXADDR(sc, sc->sc_txlast));
1600 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1601
1602 /*
1603 * Initialize receiver buffer area - RFA.
1604 */
1605 rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
1606 fxp_scb_wait(sc);
1607 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1608 rxmap->dm_segs[0].ds_addr + RFA_ALIGNMENT_FUDGE);
1609 fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1610
1611 if (sc->sc_flags & FXPF_MII) {
1612 /*
1613 * Set current media.
1614 */
1615 mii_mediachg(&sc->sc_mii);
1616 }
1617
1618 /*
1619 * ...all done!
1620 */
1621 ifp->if_flags |= IFF_RUNNING;
1622 ifp->if_flags &= ~IFF_OACTIVE;
1623
1624 /*
1625 * Start the one second timer.
1626 */
1627 callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
1628
1629 /*
1630 * Attempt to start output on the interface.
1631 */
1632 fxp_start(ifp);
1633
1634 out:
1635 if (error) {
1636 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1637 ifp->if_timer = 0;
1638 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1639 }
1640 return (error);
1641 }
1642
1643 /*
1644 * Change media according to request.
1645 */
1646 int
1647 fxp_mii_mediachange(struct ifnet *ifp)
1648 {
1649 struct fxp_softc *sc = ifp->if_softc;
1650
1651 if (ifp->if_flags & IFF_UP)
1652 mii_mediachg(&sc->sc_mii);
1653 return (0);
1654 }
1655
1656 /*
1657 * Notify the world which media we're using.
1658 */
1659 void
1660 fxp_mii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1661 {
1662 struct fxp_softc *sc = ifp->if_softc;
1663
1664 if(sc->sc_enabled == 0) {
1665 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1666 ifmr->ifm_status = 0;
1667 return;
1668 }
1669
1670 mii_pollstat(&sc->sc_mii);
1671 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1672 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1673 }
1674
1675 int
1676 fxp_80c24_mediachange(struct ifnet *ifp)
1677 {
1678
1679 /* Nothing to do here. */
1680 return (0);
1681 }
1682
1683 void
1684 fxp_80c24_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1685 {
1686 struct fxp_softc *sc = ifp->if_softc;
1687
1688 /*
1689 * Media is currently-selected media. We cannot determine
1690 * the link status.
1691 */
1692 ifmr->ifm_status = 0;
1693 ifmr->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
1694 }
1695
1696 /*
1697 * Add a buffer to the end of the RFA buffer list.
1698 * Return 0 if successful, error code on failure.
1699 *
1700 * The RFA struct is stuck at the beginning of mbuf cluster and the
1701 * data pointer is fixed up to point just past it.
1702 */
1703 int
1704 fxp_add_rfabuf(struct fxp_softc *sc, bus_dmamap_t rxmap, int unload)
1705 {
1706 struct mbuf *m;
1707 int error;
1708
1709 MGETHDR(m, M_DONTWAIT, MT_DATA);
1710 if (m == NULL)
1711 return (ENOBUFS);
1712
1713 MCLGET(m, M_DONTWAIT);
1714 if ((m->m_flags & M_EXT) == 0) {
1715 m_freem(m);
1716 return (ENOBUFS);
1717 }
1718
1719 if (unload)
1720 bus_dmamap_unload(sc->sc_dmat, rxmap);
1721
1722 M_SETCTX(m, rxmap);
1723
1724 error = bus_dmamap_load(sc->sc_dmat, rxmap,
1725 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1726 BUS_DMA_READ|BUS_DMA_NOWAIT);
1727 if (error) {
1728 printf("%s: can't load rx DMA map %d, error = %d\n",
1729 sc->sc_dev.dv_xname, sc->sc_rxq.ifq_len, error);
1730 panic("fxp_add_rfabuf"); /* XXX */
1731 }
1732
1733 FXP_INIT_RFABUF(sc, m);
1734
1735 return (0);
1736 }
1737
1738 int
1739 fxp_mdi_read(struct device *self, int phy, int reg)
1740 {
1741 struct fxp_softc *sc = (struct fxp_softc *)self;
1742 int count = 10000;
1743 int value;
1744
1745 CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1746 (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1747
1748 while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1749 && count--)
1750 DELAY(10);
1751
1752 if (count <= 0)
1753 printf("%s: fxp_mdi_read: timed out\n", sc->sc_dev.dv_xname);
1754
1755 return (value & 0xffff);
1756 }
1757
1758 void
1759 fxp_statchg(struct device *self)
1760 {
1761 struct fxp_softc *sc = (void *) self;
1762
1763 /*
1764 * Determine whether or not we have to work-around the
1765 * Resume Bug.
1766 */
1767 if (sc->sc_flags & FXPF_HAS_RESUME_BUG) {
1768 if (IFM_TYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
1769 sc->sc_flags |= FXPF_FIX_RESUME_BUG;
1770 else
1771 sc->sc_flags &= ~FXPF_FIX_RESUME_BUG;
1772 }
1773 }
1774
1775 void
1776 fxp_mdi_write(struct device *self, int phy, int reg, int value)
1777 {
1778 struct fxp_softc *sc = (struct fxp_softc *)self;
1779 int count = 10000;
1780
1781 CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1782 (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1783 (value & 0xffff));
1784
1785 while((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1786 count--)
1787 DELAY(10);
1788
1789 if (count <= 0)
1790 printf("%s: fxp_mdi_write: timed out\n", sc->sc_dev.dv_xname);
1791 }
1792
1793 int
1794 fxp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1795 {
1796 struct fxp_softc *sc = ifp->if_softc;
1797 struct ifreq *ifr = (struct ifreq *)data;
1798 int s, error;
1799
1800 s = splnet();
1801
1802 switch (cmd) {
1803 case SIOCSIFMEDIA:
1804 case SIOCGIFMEDIA:
1805 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1806 break;
1807
1808 default:
1809 error = ether_ioctl(ifp, cmd, data);
1810 if (error == ENETRESET) {
1811 if (sc->sc_enabled) {
1812 /*
1813 * Multicast list has changed; set the
1814 * hardware filter accordingly.
1815 */
1816 if (sc->sc_txpending) {
1817 sc->sc_flags |= FXPF_WANTINIT;
1818 error = 0;
1819 } else
1820 error = fxp_init(ifp);
1821 } else
1822 error = 0;
1823 }
1824 break;
1825 }
1826
1827 /* Try to get more packets going. */
1828 if (sc->sc_enabled)
1829 fxp_start(ifp);
1830
1831 splx(s);
1832 return (error);
1833 }
1834
1835 /*
1836 * Program the multicast filter.
1837 *
1838 * This function must be called at splnet().
1839 */
1840 void
1841 fxp_mc_setup(struct fxp_softc *sc)
1842 {
1843 struct fxp_cb_mcs *mcsp = &sc->sc_control_data->fcd_mcscb;
1844 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1845 struct ethercom *ec = &sc->sc_ethercom;
1846 struct ether_multi *enm;
1847 struct ether_multistep step;
1848 int count, nmcasts;
1849
1850 #ifdef DIAGNOSTIC
1851 if (sc->sc_txpending)
1852 panic("fxp_mc_setup: pending transmissions");
1853 #endif
1854
1855 ifp->if_flags &= ~IFF_ALLMULTI;
1856
1857 /*
1858 * Initialize multicast setup descriptor.
1859 */
1860 nmcasts = 0;
1861 ETHER_FIRST_MULTI(step, ec, enm);
1862 while (enm != NULL) {
1863 /*
1864 * Check for too many multicast addresses or if we're
1865 * listening to a range. Either way, we simply have
1866 * to accept all multicasts.
1867 */
1868 if (nmcasts >= MAXMCADDR ||
1869 memcmp(enm->enm_addrlo, enm->enm_addrhi,
1870 ETHER_ADDR_LEN) != 0) {
1871 /*
1872 * Callers of this function must do the
1873 * right thing with this. If we're called
1874 * from outside fxp_init(), the caller must
1875 * detect if the state if IFF_ALLMULTI changes.
1876 * If it does, the caller must then call
1877 * fxp_init(), since allmulti is handled by
1878 * the config block.
1879 */
1880 ifp->if_flags |= IFF_ALLMULTI;
1881 return;
1882 }
1883 memcpy((void *)&mcsp->mc_addr[nmcasts][0], enm->enm_addrlo,
1884 ETHER_ADDR_LEN);
1885 nmcasts++;
1886 ETHER_NEXT_MULTI(step, enm);
1887 }
1888
1889 /* BIG_ENDIAN: no need to swap to store 0 */
1890 mcsp->cb_status = 0;
1891 mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
1892 mcsp->link_addr = htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(sc->sc_txlast)));
1893 mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
1894
1895 FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1896
1897 /*
1898 * Wait until the command unit is not active. This should never
1899 * happen since nothing is queued, but make sure anyway.
1900 */
1901 count = 100;
1902 while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
1903 FXP_SCB_CUS_ACTIVE && --count)
1904 DELAY(1);
1905 if (count == 0) {
1906 printf("%s at line %d: command queue timeout\n",
1907 sc->sc_dev.dv_xname, __LINE__);
1908 return;
1909 }
1910
1911 /*
1912 * Start the multicast setup command/DMA.
1913 */
1914 fxp_scb_wait(sc);
1915 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDMCSOFF);
1916 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1917
1918 /* ...and wait for it to complete. */
1919 count = 1000;
1920 do {
1921 FXP_CDMCSSYNC(sc,
1922 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1923 DELAY(1);
1924 } while ((le16toh(mcsp->cb_status) & FXP_CB_STATUS_C) == 0 && --count);
1925 if (count == 0) {
1926 printf("%s at line %d: dmasync timeout\n",
1927 sc->sc_dev.dv_xname, __LINE__);
1928 return;
1929 }
1930 }
1931
1932 int
1933 fxp_enable(struct fxp_softc *sc)
1934 {
1935
1936 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1937 if ((*sc->sc_enable)(sc) != 0) {
1938 printf("%s: device enable failed\n",
1939 sc->sc_dev.dv_xname);
1940 return (EIO);
1941 }
1942 }
1943
1944 sc->sc_enabled = 1;
1945 return (0);
1946 }
1947
1948 void
1949 fxp_disable(struct fxp_softc *sc)
1950 {
1951
1952 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1953 (*sc->sc_disable)(sc);
1954 sc->sc_enabled = 0;
1955 }
1956 }
1957
1958 /*
1959 * fxp_activate:
1960 *
1961 * Handle device activation/deactivation requests.
1962 */
1963 int
1964 fxp_activate(struct device *self, enum devact act)
1965 {
1966 struct fxp_softc *sc = (void *) self;
1967 int s, error = 0;
1968
1969 s = splnet();
1970 switch (act) {
1971 case DVACT_ACTIVATE:
1972 error = EOPNOTSUPP;
1973 break;
1974
1975 case DVACT_DEACTIVATE:
1976 if (sc->sc_flags & FXPF_MII)
1977 mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
1978 MII_OFFSET_ANY);
1979 if_deactivate(&sc->sc_ethercom.ec_if);
1980 break;
1981 }
1982 splx(s);
1983
1984 return (error);
1985 }
1986
1987 /*
1988 * fxp_detach:
1989 *
1990 * Detach an i82557 interface.
1991 */
1992 int
1993 fxp_detach(struct fxp_softc *sc)
1994 {
1995 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1996 int i;
1997
1998 /* Succeed now if there's no work to do. */
1999 if ((sc->sc_flags & FXPF_ATTACHED) == 0)
2000 return (0);
2001
2002 /* Unhook our tick handler. */
2003 callout_stop(&sc->sc_callout);
2004
2005 if (sc->sc_flags & FXPF_MII) {
2006 /* Detach all PHYs */
2007 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
2008 }
2009
2010 /* Delete all remaining media. */
2011 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
2012
2013 #if NRND > 0
2014 rnd_detach_source(&sc->rnd_source);
2015 #endif
2016 ether_ifdetach(ifp);
2017 if_detach(ifp);
2018
2019 for (i = 0; i < FXP_NRFABUFS; i++) {
2020 bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmaps[i]);
2021 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
2022 }
2023
2024 for (i = 0; i < FXP_NTXCB; i++) {
2025 bus_dmamap_unload(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
2026 bus_dmamap_destroy(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
2027 }
2028
2029 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
2030 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
2031 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
2032 sizeof(struct fxp_control_data));
2033 bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
2034
2035 shutdownhook_disestablish(sc->sc_sdhook);
2036 powerhook_disestablish(sc->sc_powerhook);
2037
2038 return (0);
2039 }
2040