i82557.c revision 1.13 1 /* $NetBSD: i82557.c,v 1.13 1999/11/19 15:19:14 joda Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 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 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice unmodified, this list of conditions, and the following
49 * disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * Id: if_fxp.c,v 1.47 1998/01/08 23:42:29 eivind Exp
67 */
68
69 /*
70 * Device driver for the Intel i82557 fast Ethernet controller.
71 */
72
73 #include "opt_inet.h"
74 #include "opt_ns.h"
75 #include "bpfilter.h"
76 #include "rnd.h"
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/mbuf.h>
81 #include <sys/malloc.h>
82 #include <sys/kernel.h>
83 #include <sys/socket.h>
84 #include <sys/ioctl.h>
85 #include <sys/errno.h>
86 #include <sys/device.h>
87
88 #include <vm/vm.h> /* for PAGE_SIZE */
89
90 #if NRND > 0
91 #include <sys/rnd.h>
92 #endif
93
94 #include <net/if.h>
95 #include <net/if_dl.h>
96 #include <net/if_media.h>
97 #include <net/if_ether.h>
98
99 #if NBPFILTER > 0
100 #include <net/bpf.h>
101 #endif
102
103 #ifdef INET
104 #include <netinet/in.h>
105 #include <netinet/if_inarp.h>
106 #endif
107
108 #ifdef NS
109 #include <netns/ns.h>
110 #include <netns/ns_if.h>
111 #endif
112
113 #include <machine/bus.h>
114 #include <machine/intr.h>
115
116 #include <dev/mii/miivar.h>
117
118 #include <dev/ic/i82557reg.h>
119 #include <dev/ic/i82557var.h>
120
121 /*
122 * NOTE! On the Alpha, we have an alignment constraint. The
123 * card DMAs the packet immediately following the RFA. However,
124 * the first thing in the packet is a 14-byte Ethernet header.
125 * This means that the packet is misaligned. To compensate,
126 * we actually offset the RFA 2 bytes into the cluster. This
127 * alignes the packet after the Ethernet header at a 32-bit
128 * boundary. HOWEVER! This means that the RFA is misaligned!
129 */
130 #define RFA_ALIGNMENT_FUDGE 2
131
132 /*
133 * Template for default configuration parameters.
134 * See struct fxp_cb_config for the bit definitions.
135 */
136 u_int8_t fxp_cb_config_template[] = {
137 0x0, 0x0, /* cb_status */
138 0x80, 0x2, /* cb_command */
139 0xff, 0xff, 0xff, 0xff, /* link_addr */
140 0x16, /* 0 */
141 0x8, /* 1 */
142 0x0, /* 2 */
143 0x0, /* 3 */
144 0x0, /* 4 */
145 0x80, /* 5 */
146 0xb2, /* 6 */
147 0x3, /* 7 */
148 0x1, /* 8 */
149 0x0, /* 9 */
150 0x26, /* 10 */
151 0x0, /* 11 */
152 0x60, /* 12 */
153 0x0, /* 13 */
154 0xf2, /* 14 */
155 0x48, /* 15 */
156 0x0, /* 16 */
157 0x40, /* 17 */
158 0xf3, /* 18 */
159 0x0, /* 19 */
160 0x3f, /* 20 */
161 0x5 /* 21 */
162 };
163
164 void fxp_mii_initmedia __P((struct fxp_softc *));
165 int fxp_mii_mediachange __P((struct ifnet *));
166 void fxp_mii_mediastatus __P((struct ifnet *, struct ifmediareq *));
167
168 void fxp_80c24_initmedia __P((struct fxp_softc *));
169 int fxp_80c24_mediachange __P((struct ifnet *));
170 void fxp_80c24_mediastatus __P((struct ifnet *, struct ifmediareq *));
171
172 inline void fxp_scb_wait __P((struct fxp_softc *));
173
174 void fxp_start __P((struct ifnet *));
175 int fxp_ioctl __P((struct ifnet *, u_long, caddr_t));
176 int fxp_init __P((struct fxp_softc *));
177 void fxp_rxdrain __P((struct fxp_softc *));
178 void fxp_stop __P((struct fxp_softc *, int));
179 void fxp_watchdog __P((struct ifnet *));
180 int fxp_add_rfabuf __P((struct fxp_softc *, bus_dmamap_t, int));
181 int fxp_mdi_read __P((struct device *, int, int));
182 void fxp_statchg __P((struct device *));
183 void fxp_mdi_write __P((struct device *, int, int, int));
184 void fxp_autosize_eeprom __P((struct fxp_softc*));
185 void fxp_read_eeprom __P((struct fxp_softc *, u_int16_t *, int, int));
186 void fxp_get_info __P((struct fxp_softc *, u_int8_t *));
187 void fxp_tick __P((void *));
188 void fxp_mc_setup __P((struct fxp_softc *));
189
190 void fxp_shutdown __P((void *));
191 void fxp_power __P((int, void *));
192
193 int fxp_copy_small = 0;
194
195 int fxp_enable __P((struct fxp_softc*));
196 void fxp_disable __P((struct fxp_softc*));
197
198 struct fxp_phytype {
199 int fp_phy; /* type of PHY, -1 for MII at the end. */
200 void (*fp_init) __P((struct fxp_softc *));
201 } fxp_phytype_table[] = {
202 { FXP_PHY_80C24, fxp_80c24_initmedia },
203 { -1, fxp_mii_initmedia },
204 };
205
206 /*
207 * Set initial transmit threshold at 64 (512 bytes). This is
208 * increased by 64 (512 bytes) at a time, to maximum of 192
209 * (1536 bytes), if an underrun occurs.
210 */
211 static int tx_threshold = 64;
212
213 /*
214 * Wait for the previous command to be accepted (but not necessarily
215 * completed).
216 */
217 inline void
218 fxp_scb_wait(sc)
219 struct fxp_softc *sc;
220 {
221 int i = 10000;
222
223 while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
224 delay(2);
225 if (i == 0)
226 printf("%s: WARNING: SCB timed out!\n", sc->sc_dev.dv_xname);
227 }
228
229 /*
230 * Finish attaching an i82557 interface. Called by bus-specific front-end.
231 */
232 void
233 fxp_attach(sc)
234 struct fxp_softc *sc;
235 {
236 u_int8_t enaddr[6];
237 struct ifnet *ifp;
238 bus_dma_segment_t seg;
239 int rseg, i, error;
240 struct fxp_phytype *fp;
241
242 /*
243 * Allocate the control data structures, and create and load the
244 * DMA map for it.
245 */
246 if ((error = bus_dmamem_alloc(sc->sc_dmat,
247 sizeof(struct fxp_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
248 0)) != 0) {
249 printf("%s: unable to allocate control data, error = %d\n",
250 sc->sc_dev.dv_xname, error);
251 goto fail_0;
252 }
253
254 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
255 sizeof(struct fxp_control_data), (caddr_t *)&sc->sc_control_data,
256 BUS_DMA_COHERENT)) != 0) {
257 printf("%s: unable to map control data, error = %d\n",
258 sc->sc_dev.dv_xname, error);
259 goto fail_1;
260 }
261 bzero(sc->sc_control_data, sizeof(struct fxp_control_data));
262
263 if ((error = bus_dmamap_create(sc->sc_dmat,
264 sizeof(struct fxp_control_data), 1,
265 sizeof(struct fxp_control_data), 0, 0, &sc->sc_dmamap)) != 0) {
266 printf("%s: unable to create control data DMA map, "
267 "error = %d\n", sc->sc_dev.dv_xname, error);
268 goto fail_2;
269 }
270
271 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
272 sc->sc_control_data, sizeof(struct fxp_control_data), NULL,
273 0)) != 0) {
274 printf("%s: can't load control data DMA map, error = %d\n",
275 sc->sc_dev.dv_xname, error);
276 goto fail_3;
277 }
278
279 /*
280 * Create the transmit buffer DMA maps.
281 */
282 for (i = 0; i < FXP_NTXCB; i++) {
283 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
284 FXP_NTXSEG, MCLBYTES, 0, 0,
285 &FXP_DSTX(sc, i)->txs_dmamap)) != 0) {
286 printf("%s: unable to create tx DMA map %d, "
287 "error = %d\n", sc->sc_dev.dv_xname, i, error);
288 goto fail_4;
289 }
290 }
291
292 /*
293 * Create the receive buffer DMA maps.
294 */
295 for (i = 0; i < FXP_NRFABUFS; i++) {
296 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
297 MCLBYTES, 0, 0, &sc->sc_rxmaps[i])) != 0) {
298 printf("%s: unable to create rx DMA map %d, "
299 "error = %d\n", sc->sc_dev.dv_xname, i, error);
300 goto fail_5;
301 }
302 }
303
304 /* Initialize MAC address and media structures. */
305 fxp_get_info(sc, enaddr);
306
307 printf("%s: Ethernet address %s, %s Mb/s\n", sc->sc_dev.dv_xname,
308 ether_sprintf(enaddr), sc->phy_10Mbps_only ? "10" : "10/100");
309
310 ifp = &sc->sc_ethercom.ec_if;
311
312 /*
313 * Get info about our media interface, and initialize it. Note
314 * the table terminates itself with a phy of -1, indicating
315 * that we're using MII.
316 */
317 for (fp = fxp_phytype_table; fp->fp_phy != -1; fp++)
318 if (fp->fp_phy == sc->phy_primary_device)
319 break;
320 (*fp->fp_init)(sc);
321
322 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
323 ifp->if_softc = sc;
324 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
325 ifp->if_ioctl = fxp_ioctl;
326 ifp->if_start = fxp_start;
327 ifp->if_watchdog = fxp_watchdog;
328
329 /*
330 * Attach the interface.
331 */
332 if_attach(ifp);
333 ether_ifattach(ifp, enaddr);
334 #if NBPFILTER > 0
335 bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
336 sizeof(struct ether_header));
337 #endif
338 #if NRND > 0
339 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
340 RND_TYPE_NET, 0);
341 #endif
342
343 /*
344 * Add shutdown hook so that DMA is disabled prior to reboot. Not
345 * doing do could allow DMA to corrupt kernel memory during the
346 * reboot before the driver initializes.
347 */
348 sc->sc_sdhook = shutdownhook_establish(fxp_shutdown, sc);
349 if (sc->sc_sdhook == NULL)
350 printf("%s: WARNING: unable to establish shutdown hook\n",
351 sc->sc_dev.dv_xname);
352 /*
353 * Add suspend hook, for similar reasons..
354 */
355 sc->sc_powerhook = powerhook_establish(fxp_power, sc);
356 if (sc->sc_powerhook == NULL)
357 printf("%s: WARNING: unable to establish power hook\n",
358 sc->sc_dev.dv_xname);
359 return;
360
361 /*
362 * Free any resources we've allocated during the failed attach
363 * attempt. Do this in reverse order and fall though.
364 */
365 fail_5:
366 for (i = 0; i < FXP_NRFABUFS; i++) {
367 if (sc->sc_rxmaps[i] != NULL)
368 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
369 }
370 fail_4:
371 for (i = 0; i < FXP_NTXCB; i++) {
372 if (FXP_DSTX(sc, i)->txs_dmamap != NULL)
373 bus_dmamap_destroy(sc->sc_dmat,
374 FXP_DSTX(sc, i)->txs_dmamap);
375 }
376 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
377 fail_3:
378 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
379 fail_2:
380 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
381 sizeof(struct fxp_control_data));
382 fail_1:
383 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
384 fail_0:
385 return;
386 }
387
388 void
389 fxp_mii_initmedia(sc)
390 struct fxp_softc *sc;
391 {
392
393 sc->sc_flags |= FXPF_MII;
394
395 sc->sc_mii.mii_ifp = &sc->sc_ethercom.ec_if;
396 sc->sc_mii.mii_readreg = fxp_mdi_read;
397 sc->sc_mii.mii_writereg = fxp_mdi_write;
398 sc->sc_mii.mii_statchg = fxp_statchg;
399 ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_mii_mediachange,
400 fxp_mii_mediastatus);
401 mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
402 MII_OFFSET_ANY);
403 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
404 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
405 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
406 } else
407 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
408 }
409
410 void
411 fxp_80c24_initmedia(sc)
412 struct fxp_softc *sc;
413 {
414
415 /*
416 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
417 * doesn't have a programming interface of any sort. The
418 * media is sensed automatically based on how the link partner
419 * is configured. This is, in essence, manual configuration.
420 */
421 printf("%s: Seeq 80c24 AutoDUPLEX media interface present\n",
422 sc->sc_dev.dv_xname);
423 ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_80c24_mediachange,
424 fxp_80c24_mediastatus);
425 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
426 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
427 }
428
429 /*
430 * Device shutdown routine. Called at system shutdown after sync. The
431 * main purpose of this routine is to shut off receiver DMA so that
432 * kernel memory doesn't get clobbered during warmboot.
433 */
434 void
435 fxp_shutdown(arg)
436 void *arg;
437 {
438 struct fxp_softc *sc = arg;
439
440 /*
441 * Since the system's going to halt shortly, don't bother
442 * freeing mbufs.
443 */
444 fxp_stop(sc, 0);
445 }
446 /*
447 * Power handler routine. Called when the system is transitioning
448 * into/out of power save modes. As with fxp_shutdown, the main
449 * purpose of this routine is to shut off receiver DMA so it doesn't
450 * clobber kernel memory at the wrong time.
451 */
452 void
453 fxp_power(why, arg)
454 int why;
455 void *arg;
456 {
457 struct fxp_softc *sc = arg;
458 struct ifnet *ifp;
459 int s;
460
461 s = splnet();
462 if (why != PWR_RESUME)
463 fxp_stop(sc, 0);
464 else {
465 ifp = &sc->sc_ethercom.ec_if;
466 if (ifp->if_flags & IFF_UP)
467 fxp_init(sc);
468 }
469 splx(s);
470 }
471
472 /*
473 * Initialize the interface media.
474 */
475 void
476 fxp_get_info(sc, enaddr)
477 struct fxp_softc *sc;
478 u_int8_t *enaddr;
479 {
480 u_int16_t data, myea[3];
481
482 /*
483 * Reset to a stable state.
484 */
485 CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
486 DELAY(10);
487
488 sc->sc_eeprom_size = 0;
489 fxp_autosize_eeprom(sc);
490 if(sc->sc_eeprom_size == 0) {
491 printf("%s: failed to detect EEPROM size", sc->sc_dev.dv_xname);
492 sc->sc_eeprom_size = 6; /* XXX panic here? */
493 }
494 #ifdef DEBUG
495 printf("%s: detected %d word EEPROM\n",
496 sc->sc_dev.dv_xname,
497 1 << sc->sc_eeprom_size);
498 #endif
499
500 /*
501 * Get info about the primary PHY
502 */
503 fxp_read_eeprom(sc, &data, 6, 1);
504 sc->phy_primary_addr = data & 0xff;
505 sc->phy_primary_device = (data >> 8) & 0x3f;
506 sc->phy_10Mbps_only = data >> 15;
507
508 /*
509 * Read MAC address.
510 */
511 fxp_read_eeprom(sc, myea, 0, 3);
512 bcopy(myea, enaddr, ETHER_ADDR_LEN);
513 }
514
515 /*
516 * Figure out EEPROM size.
517 *
518 * 559's can have either 64-word or 256-word EEPROMs, the 558
519 * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
520 * talks about the existance of 16 to 256 word EEPROMs.
521 *
522 * The only known sizes are 64 and 256, where the 256 version is used
523 * by CardBus cards to store CIS information.
524 *
525 * The address is shifted in msb-to-lsb, and after the last
526 * address-bit the EEPROM is supposed to output a `dummy zero' bit,
527 * after which follows the actual data. We try to detect this zero, by
528 * probing the data-out bit in the EEPROM control register just after
529 * having shifted in a bit. If the bit is zero, we assume we've
530 * shifted enough address bits. The data-out should be tri-state,
531 * before this, which should translate to a logical one.
532 *
533 * Other ways to do this would be to try to read a register with known
534 * contents with a varying number of address bits, but no such
535 * register seem to be available. The high bits of register 10 are 01
536 * on the 558 and 559, but apparently not on the 557.
537 *
538 * The Linux driver computes a checksum on the EEPROM data, but the
539 * value of this checksum is not very well documented.
540 */
541
542 void
543 fxp_autosize_eeprom(sc)
544 struct fxp_softc *sc;
545 {
546 u_int16_t reg;
547 int x;
548
549 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
550 /*
551 * Shift in read opcode.
552 */
553 for (x = 3; x > 0; x--) {
554 if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
555 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
556 } else {
557 reg = FXP_EEPROM_EECS;
558 }
559 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
560 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
561 reg | FXP_EEPROM_EESK);
562 DELAY(1);
563 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
564 DELAY(1);
565 }
566 /*
567 * Shift in address, wait for the dummy zero following a correct
568 * address shift.
569 */
570 for (x = 1; x <= 8; x++) {
571 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
572 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
573 FXP_EEPROM_EECS | FXP_EEPROM_EESK);
574 DELAY(1);
575 if((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
576 FXP_EEPROM_EEDO) == 0)
577 break;
578 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
579 DELAY(1);
580 }
581 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
582 DELAY(1);
583 if(x != 6 && x != 8) {
584 #ifdef DEBUG
585 printf("%s: strange EEPROM size (%d)\n",
586 sc->sc_dev.dv_xname, 1 << x);
587 #endif
588 } else
589 sc->sc_eeprom_size = x;
590 }
591
592 /*
593 * Read from the serial EEPROM. Basically, you manually shift in
594 * the read opcode (one bit at a time) and then shift in the address,
595 * and then you shift out the data (all of this one bit at a time).
596 * The word size is 16 bits, so you have to provide the address for
597 * every 16 bits of data.
598 */
599 void
600 fxp_read_eeprom(sc, data, offset, words)
601 struct fxp_softc *sc;
602 u_int16_t *data;
603 int offset;
604 int words;
605 {
606 u_int16_t reg;
607 int i, x;
608
609 for (i = 0; i < words; i++) {
610 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
611 /*
612 * Shift in read opcode.
613 */
614 for (x = 3; x > 0; x--) {
615 if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
616 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
617 } else {
618 reg = FXP_EEPROM_EECS;
619 }
620 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
621 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
622 reg | FXP_EEPROM_EESK);
623 DELAY(1);
624 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
625 DELAY(1);
626 }
627 /*
628 * Shift in address.
629 */
630 for (x = sc->sc_eeprom_size; x > 0; x--) {
631 if ((i + offset) & (1 << (x - 1))) {
632 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
633 } else {
634 reg = FXP_EEPROM_EECS;
635 }
636 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
637 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
638 reg | FXP_EEPROM_EESK);
639 DELAY(1);
640 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
641 DELAY(1);
642 }
643 reg = FXP_EEPROM_EECS;
644 data[i] = 0;
645 /*
646 * Shift out data.
647 */
648 for (x = 16; x > 0; x--) {
649 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
650 reg | FXP_EEPROM_EESK);
651 DELAY(1);
652 if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
653 FXP_EEPROM_EEDO)
654 data[i] |= (1 << (x - 1));
655 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
656 DELAY(1);
657 }
658 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
659 DELAY(1);
660 }
661 }
662
663 /*
664 * Start packet transmission on the interface.
665 */
666 void
667 fxp_start(ifp)
668 struct ifnet *ifp;
669 {
670 struct fxp_softc *sc = ifp->if_softc;
671 struct mbuf *m0, *m;
672 struct fxp_cb_tx *txd;
673 struct fxp_txsoft *txs;
674 struct fxp_tbdlist *tbd;
675 bus_dmamap_t dmamap;
676 int error, lasttx, nexttx, opending, seg;
677
678 /*
679 * If we want a re-init, bail out now.
680 */
681 if (sc->sc_flags & FXPF_WANTINIT) {
682 ifp->if_flags |= IFF_OACTIVE;
683 return;
684 }
685
686 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
687 return;
688
689 /*
690 * Remember the previous txpending and the current lasttx.
691 */
692 opending = sc->sc_txpending;
693 lasttx = sc->sc_txlast;
694
695 /*
696 * Loop through the send queue, setting up transmit descriptors
697 * until we drain the queue, or use up all available transmit
698 * descriptors.
699 */
700 while (sc->sc_txpending < FXP_NTXCB) {
701 /*
702 * Grab a packet off the queue.
703 */
704 IF_DEQUEUE(&ifp->if_snd, m0);
705 if (m0 == NULL)
706 break;
707
708 /*
709 * Get the next available transmit descriptor.
710 */
711 nexttx = FXP_NEXTTX(sc->sc_txlast);
712 txd = FXP_CDTX(sc, nexttx);
713 tbd = FXP_CDTBD(sc, nexttx);
714 txs = FXP_DSTX(sc, nexttx);
715 dmamap = txs->txs_dmamap;
716
717 /*
718 * Load the DMA map. If this fails, the packet either
719 * didn't fit in the allotted number of frags, or we were
720 * short on resources. In this case, we'll copy and try
721 * again.
722 */
723 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
724 BUS_DMA_NOWAIT) != 0) {
725 MGETHDR(m, M_DONTWAIT, MT_DATA);
726 if (m == NULL) {
727 printf("%s: unable to allocate Tx mbuf\n",
728 sc->sc_dev.dv_xname);
729 IF_PREPEND(&ifp->if_snd, m0);
730 break;
731 }
732 if (m0->m_pkthdr.len > MHLEN) {
733 MCLGET(m, M_DONTWAIT);
734 if ((m->m_flags & M_EXT) == 0) {
735 printf("%s: unable to allocate Tx "
736 "cluster\n", sc->sc_dev.dv_xname);
737 m_freem(m);
738 IF_PREPEND(&ifp->if_snd, m0);
739 break;
740 }
741 }
742 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
743 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
744 m_freem(m0);
745 m0 = m;
746 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
747 m0, BUS_DMA_NOWAIT);
748 if (error) {
749 printf("%s: unable to load Tx buffer, "
750 "error = %d\n", sc->sc_dev.dv_xname, error);
751 IF_PREPEND(&ifp->if_snd, m0);
752 break;
753 }
754 }
755
756 /* Initialize the fraglist. */
757 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
758 tbd->tbd_d[seg].tb_addr =
759 dmamap->dm_segs[seg].ds_addr;
760 tbd->tbd_d[seg].tb_size =
761 dmamap->dm_segs[seg].ds_len;
762 }
763
764 FXP_CDTBDSYNC(sc, nexttx, BUS_DMASYNC_PREWRITE);
765
766 /* Sync the DMA map. */
767 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
768 BUS_DMASYNC_PREWRITE);
769
770 /*
771 * Store a pointer to the packet so we can free it later.
772 */
773 txs->txs_mbuf = m0;
774
775 /*
776 * Initialize the transmit descriptor.
777 */
778 txd->cb_status = 0;
779 txd->cb_command =
780 FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF;
781 txd->tx_threshold = tx_threshold;
782 txd->tbd_number = dmamap->dm_nsegs;
783
784 FXP_CDTXSYNC(sc, nexttx,
785 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
786
787 /* Advance the tx pointer. */
788 sc->sc_txpending++;
789 sc->sc_txlast = nexttx;
790
791 #if NBPFILTER > 0
792 /*
793 * Pass packet to bpf if there is a listener.
794 */
795 if (ifp->if_bpf)
796 bpf_mtap(ifp->if_bpf, m0);
797 #endif
798 }
799
800 if (sc->sc_txpending == FXP_NTXCB) {
801 /* No more slots; notify upper layer. */
802 ifp->if_flags |= IFF_OACTIVE;
803 }
804
805 if (sc->sc_txpending != opending) {
806 /*
807 * We enqueued packets. If the transmitter was idle,
808 * reset the txdirty pointer.
809 */
810 if (opending == 0)
811 sc->sc_txdirty = FXP_NEXTTX(lasttx);
812
813 /*
814 * Cause the chip to interrupt and suspend command
815 * processing once the last packet we've enqueued
816 * has been transmitted.
817 */
818 FXP_CDTX(sc, sc->sc_txlast)->cb_command |=
819 FXP_CB_COMMAND_I | FXP_CB_COMMAND_S;
820 FXP_CDTXSYNC(sc, sc->sc_txlast,
821 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
822
823 /*
824 * The entire packet chain is set up. Clear the suspend bit
825 * on the command prior to the first packet we set up.
826 */
827 FXP_CDTXSYNC(sc, lasttx,
828 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
829 FXP_CDTX(sc, lasttx)->cb_command &= ~FXP_CB_COMMAND_S;
830 FXP_CDTXSYNC(sc, lasttx,
831 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
832
833 /*
834 * Issue a Resume command in case the chip was suspended.
835 */
836 fxp_scb_wait(sc);
837 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
838
839 /* Set a watchdog timer in case the chip flakes out. */
840 ifp->if_timer = 5;
841 }
842 }
843
844 /*
845 * Process interface interrupts.
846 */
847 int
848 fxp_intr(arg)
849 void *arg;
850 {
851 struct fxp_softc *sc = arg;
852 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
853 struct fxp_cb_tx *txd;
854 struct fxp_txsoft *txs;
855 struct mbuf *m, *m0;
856 bus_dmamap_t rxmap;
857 struct fxp_rfa *rfa;
858 struct ether_header *eh;
859 int i, claimed = 0;
860 u_int16_t len;
861 u_int8_t statack;
862
863 /*
864 * If the interface isn't running, don't try to
865 * service the interrupt.. just ack it and bail.
866 */
867 if ((ifp->if_flags & IFF_RUNNING) == 0) {
868 statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
869 if (statack) {
870 claimed = 1;
871 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
872 }
873 return claimed;
874 }
875
876 while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
877 claimed = 1;
878
879 /*
880 * First ACK all the interrupts in this pass.
881 */
882 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
883
884 /*
885 * Process receiver interrupts. If a no-resource (RNR)
886 * condition exists, get whatever packets we can and
887 * re-start the receiver.
888 */
889 if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
890 rcvloop:
891 m = sc->sc_rxq.ifq_head;
892 rfa = FXP_MTORFA(m);
893 rxmap = M_GETCTX(m, bus_dmamap_t);
894
895 FXP_RFASYNC(sc, m,
896 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
897
898 if ((rfa->rfa_status & FXP_RFA_STATUS_C) == 0) {
899 /*
900 * We have processed all of the
901 * receive buffers.
902 */
903 goto do_transmit;
904 }
905
906 IF_DEQUEUE(&sc->sc_rxq, m);
907
908 FXP_RXBUFSYNC(sc, m, BUS_DMASYNC_POSTREAD);
909
910 len = rfa->actual_size & (m->m_ext.ext_size - 1);
911
912 if (len < sizeof(struct ether_header)) {
913 /*
914 * Runt packet; drop it now.
915 */
916 FXP_INIT_RFABUF(sc, m);
917 goto rcvloop;
918 }
919
920 /*
921 * If the packet is small enough to fit in a
922 * single header mbuf, allocate one and copy
923 * the data into it. This greatly reduces
924 * memory consumption when we receive lots
925 * of small packets.
926 *
927 * Otherwise, we add a new buffer to the receive
928 * chain. If this fails, we drop the packet and
929 * recycle the old buffer.
930 */
931 if (fxp_copy_small != 0 && len <= MHLEN) {
932 MGETHDR(m0, M_DONTWAIT, MT_DATA);
933 if (m == NULL)
934 goto dropit;
935 memcpy(mtod(m0, caddr_t),
936 mtod(m, caddr_t), len);
937 FXP_INIT_RFABUF(sc, m);
938 m = m0;
939 } else {
940 if (fxp_add_rfabuf(sc, rxmap, 1) != 0) {
941 dropit:
942 ifp->if_ierrors++;
943 FXP_INIT_RFABUF(sc, m);
944 goto rcvloop;
945 }
946 }
947
948 m->m_pkthdr.rcvif = ifp;
949 m->m_pkthdr.len = m->m_len = len;
950 eh = mtod(m, struct ether_header *);
951
952 #if NBPFILTER > 0
953 /*
954 * Pass this up to any BPF listeners, but only
955 * pass it up the stack it its for us.
956 */
957 if (ifp->if_bpf) {
958 bpf_mtap(ifp->if_bpf, m);
959
960 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
961 (rfa->rfa_status &
962 FXP_RFA_STATUS_IAMATCH) != 0 &&
963 (eh->ether_dhost[0] & 1) == 0) {
964 m_freem(m);
965 goto rcvloop;
966 }
967 }
968 #endif /* NBPFILTER > 0 */
969
970 /* Pass it on. */
971 (*ifp->if_input)(ifp, m);
972 goto rcvloop;
973 }
974
975 do_transmit:
976 if (statack & FXP_SCB_STATACK_RNR) {
977 rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
978 fxp_scb_wait(sc);
979 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
980 rxmap->dm_segs[0].ds_addr +
981 RFA_ALIGNMENT_FUDGE);
982 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
983 FXP_SCB_COMMAND_RU_START);
984 }
985
986 /*
987 * Free any finished transmit mbuf chains.
988 */
989 if (statack & (FXP_SCB_STATACK_CXTNO|FXP_SCB_STATACK_CNA)) {
990 ifp->if_flags &= ~IFF_OACTIVE;
991 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
992 i = FXP_NEXTTX(i), sc->sc_txpending--) {
993 txd = FXP_CDTX(sc, i);
994 txs = FXP_DSTX(sc, i);
995
996 FXP_CDTXSYNC(sc, i,
997 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
998
999 if ((txd->cb_status & FXP_CB_STATUS_C) == 0)
1000 break;
1001
1002 FXP_CDTBDSYNC(sc, i, BUS_DMASYNC_POSTWRITE);
1003
1004 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1005 0, txs->txs_dmamap->dm_mapsize,
1006 BUS_DMASYNC_POSTWRITE);
1007 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1008 m_freem(txs->txs_mbuf);
1009 txs->txs_mbuf = NULL;
1010 }
1011
1012 /* Update the dirty transmit buffer pointer. */
1013 sc->sc_txdirty = i;
1014
1015 /*
1016 * Cancel the watchdog timer if there are no pending
1017 * transmissions.
1018 */
1019 if (sc->sc_txpending == 0) {
1020 ifp->if_timer = 0;
1021
1022 /*
1023 * If we want a re-init, do that now.
1024 */
1025 if (sc->sc_flags & FXPF_WANTINIT)
1026 (void) fxp_init(sc);
1027 }
1028
1029 /*
1030 * Try to get more packets going.
1031 */
1032 fxp_start(ifp);
1033 }
1034 }
1035
1036 #if NRND > 0
1037 if (claimed)
1038 rnd_add_uint32(&sc->rnd_source, statack);
1039 #endif
1040 return (claimed);
1041 }
1042
1043 /*
1044 * Update packet in/out/collision statistics. The i82557 doesn't
1045 * allow you to access these counters without doing a fairly
1046 * expensive DMA to get _all_ of the statistics it maintains, so
1047 * we do this operation here only once per second. The statistics
1048 * counters in the kernel are updated from the previous dump-stats
1049 * DMA and then a new dump-stats DMA is started. The on-chip
1050 * counters are zeroed when the DMA completes. If we can't start
1051 * the DMA immediately, we don't wait - we just prepare to read
1052 * them again next time.
1053 */
1054 void
1055 fxp_tick(arg)
1056 void *arg;
1057 {
1058 struct fxp_softc *sc = arg;
1059 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1060 struct fxp_stats *sp = &sc->sc_control_data->fcd_stats;
1061 int s;
1062
1063 s = splnet();
1064
1065 ifp->if_opackets += sp->tx_good;
1066 ifp->if_collisions += sp->tx_total_collisions;
1067 if (sp->rx_good) {
1068 ifp->if_ipackets += sp->rx_good;
1069 sc->sc_rxidle = 0;
1070 } else {
1071 sc->sc_rxidle++;
1072 }
1073 ifp->if_ierrors +=
1074 sp->rx_crc_errors +
1075 sp->rx_alignment_errors +
1076 sp->rx_rnr_errors +
1077 sp->rx_overrun_errors;
1078 /*
1079 * If any transmit underruns occured, bump up the transmit
1080 * threshold by another 512 bytes (64 * 8).
1081 */
1082 if (sp->tx_underruns) {
1083 ifp->if_oerrors += sp->tx_underruns;
1084 if (tx_threshold < 192)
1085 tx_threshold += 64;
1086 }
1087
1088 /*
1089 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1090 * then assume the receiver has locked up and attempt to clear
1091 * the condition by reprogramming the multicast filter (actually,
1092 * resetting the interface). This is a work-around for a bug in
1093 * the 82557 where the receiver locks up if it gets certain types
1094 * of garbage in the syncronization bits prior to the packet header.
1095 * This bug is supposed to only occur in 10Mbps mode, but has been
1096 * seen to occur in 100Mbps mode as well (perhaps due to a 10/100
1097 * speed transition).
1098 */
1099 if (sc->sc_rxidle > FXP_MAX_RX_IDLE) {
1100 (void) fxp_init(sc);
1101 splx(s);
1102 return;
1103 }
1104 /*
1105 * If there is no pending command, start another stats
1106 * dump. Otherwise punt for now.
1107 */
1108 if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1109 /*
1110 * Start another stats dump.
1111 */
1112 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1113 FXP_SCB_COMMAND_CU_DUMPRESET);
1114 } else {
1115 /*
1116 * A previous command is still waiting to be accepted.
1117 * Just zero our copy of the stats and wait for the
1118 * next timer event to update them.
1119 */
1120 sp->tx_good = 0;
1121 sp->tx_underruns = 0;
1122 sp->tx_total_collisions = 0;
1123
1124 sp->rx_good = 0;
1125 sp->rx_crc_errors = 0;
1126 sp->rx_alignment_errors = 0;
1127 sp->rx_rnr_errors = 0;
1128 sp->rx_overrun_errors = 0;
1129 }
1130
1131 if (sc->sc_flags & FXPF_MII) {
1132 /* Tick the MII clock. */
1133 mii_tick(&sc->sc_mii);
1134 }
1135
1136 splx(s);
1137
1138 /*
1139 * Schedule another timeout one second from now.
1140 */
1141 timeout(fxp_tick, sc, hz);
1142 }
1143
1144 /*
1145 * Drain the receive queue.
1146 */
1147 void
1148 fxp_rxdrain(sc)
1149 struct fxp_softc *sc;
1150 {
1151 bus_dmamap_t rxmap;
1152 struct mbuf *m;
1153
1154 for (;;) {
1155 IF_DEQUEUE(&sc->sc_rxq, m);
1156 if (m == NULL)
1157 break;
1158 rxmap = M_GETCTX(m, bus_dmamap_t);
1159 bus_dmamap_unload(sc->sc_dmat, rxmap);
1160 FXP_RXMAP_PUT(sc, rxmap);
1161 m_freem(m);
1162 }
1163 }
1164
1165 /*
1166 * Stop the interface. Cancels the statistics updater and resets
1167 * the interface.
1168 */
1169 void
1170 fxp_stop(sc, drain)
1171 struct fxp_softc *sc;
1172 int drain;
1173 {
1174 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1175 struct fxp_txsoft *txs;
1176 int i;
1177
1178 /*
1179 * Turn down interface (done early to avoid bad interactions
1180 * between panics, shutdown hooks, and the watchdog timer)
1181 */
1182 ifp->if_timer = 0;
1183 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1184
1185 /*
1186 * Cancel stats updater.
1187 */
1188 untimeout(fxp_tick, sc);
1189 if (sc->sc_flags & FXPF_MII) {
1190 /* Down the MII. */
1191 mii_down(&sc->sc_mii);
1192 }
1193
1194 /*
1195 * Issue software reset
1196 */
1197 CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1198 DELAY(10);
1199
1200 /*
1201 * Release any xmit buffers.
1202 */
1203 for (i = 0; i < FXP_NTXCB; i++) {
1204 txs = FXP_DSTX(sc, i);
1205 if (txs->txs_mbuf != NULL) {
1206 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1207 m_freem(txs->txs_mbuf);
1208 txs->txs_mbuf = NULL;
1209 }
1210 }
1211 sc->sc_txpending = 0;
1212
1213 if (drain) {
1214 /*
1215 * Release the receive buffers.
1216 */
1217 fxp_rxdrain(sc);
1218 }
1219
1220 }
1221
1222 /*
1223 * Watchdog/transmission transmit timeout handler. Called when a
1224 * transmission is started on the interface, but no interrupt is
1225 * received before the timeout. This usually indicates that the
1226 * card has wedged for some reason.
1227 */
1228 void
1229 fxp_watchdog(ifp)
1230 struct ifnet *ifp;
1231 {
1232 struct fxp_softc *sc = ifp->if_softc;
1233
1234 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1235 ifp->if_oerrors++;
1236
1237 (void) fxp_init(sc);
1238 }
1239
1240 /*
1241 * Initialize the interface. Must be called at splnet().
1242 */
1243 int
1244 fxp_init(sc)
1245 struct fxp_softc *sc;
1246 {
1247 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1248 struct fxp_cb_config *cbp;
1249 struct fxp_cb_ias *cb_ias;
1250 struct fxp_cb_tx *txd;
1251 bus_dmamap_t rxmap;
1252 int i, prm, allm, error = 0;
1253
1254 /*
1255 * Cancel any pending I/O
1256 */
1257 fxp_stop(sc, 0);
1258
1259 sc->sc_flags = 0;
1260
1261 /*
1262 * Initialize base of CBL and RFA memory. Loading with zero
1263 * sets it up for regular linear addressing.
1264 */
1265 fxp_scb_wait(sc);
1266 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1267 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_BASE);
1268
1269 fxp_scb_wait(sc);
1270 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_BASE);
1271
1272 /*
1273 * Initialize the multicast filter. Do this now, since we might
1274 * have to setup the config block differently.
1275 */
1276 fxp_mc_setup(sc);
1277
1278 prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1279 allm = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1280
1281 /*
1282 * Initialize base of dump-stats buffer.
1283 */
1284 fxp_scb_wait(sc);
1285 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1286 sc->sc_cddma + FXP_CDSTATSOFF);
1287 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_DUMP_ADR);
1288
1289 cbp = &sc->sc_control_data->fcd_configcb;
1290 memset(cbp, 0, sizeof(struct fxp_cb_config));
1291
1292 /*
1293 * This copy is kind of disgusting, but there are a bunch of must be
1294 * zero and must be one bits in this structure and this is the easiest
1295 * way to initialize them all to proper values.
1296 */
1297 memcpy(cbp, fxp_cb_config_template, sizeof(fxp_cb_config_template));
1298
1299 cbp->cb_status = 0;
1300 cbp->cb_command = FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1301 cbp->link_addr = -1; /* (no) next command */
1302 cbp->byte_count = 22; /* (22) bytes to config */
1303 cbp->rx_fifo_limit = 8; /* rx fifo threshold (32 bytes) */
1304 cbp->tx_fifo_limit = 0; /* tx fifo threshold (0 bytes) */
1305 cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */
1306 cbp->rx_dma_bytecount = 0; /* (no) rx DMA max */
1307 cbp->tx_dma_bytecount = 0; /* (no) tx DMA max */
1308 cbp->dma_bce = 0; /* (disable) dma max counters */
1309 cbp->late_scb = 0; /* (don't) defer SCB update */
1310 cbp->tno_int = 0; /* (disable) tx not okay interrupt */
1311 cbp->ci_int = 1; /* interrupt on CU idle */
1312 cbp->save_bf = prm; /* save bad frames */
1313 cbp->disc_short_rx = !prm; /* discard short packets */
1314 cbp->underrun_retry = 1; /* retry mode (1) on DMA underrun */
1315 cbp->mediatype = !sc->phy_10Mbps_only; /* interface mode */
1316 cbp->nsai = 1; /* (don't) disable source addr insert */
1317 cbp->preamble_length = 2; /* (7 byte) preamble */
1318 cbp->loopback = 0; /* (don't) loopback */
1319 cbp->linear_priority = 0; /* (normal CSMA/CD operation) */
1320 cbp->linear_pri_mode = 0; /* (wait after xmit only) */
1321 cbp->interfrm_spacing = 6; /* (96 bits of) interframe spacing */
1322 cbp->promiscuous = prm; /* promiscuous mode */
1323 cbp->bcast_disable = 0; /* (don't) disable broadcasts */
1324 cbp->crscdt = 0; /* (CRS only) */
1325 cbp->stripping = !prm; /* truncate rx packet to byte count */
1326 cbp->padding = 1; /* (do) pad short tx packets */
1327 cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */
1328 cbp->force_fdx = 0; /* (don't) force full duplex */
1329 cbp->fdx_pin_en = 1; /* (enable) FDX# pin */
1330 cbp->multi_ia = 0; /* (don't) accept multiple IAs */
1331 cbp->mc_all = allm; /* accept all multicasts */
1332
1333 FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1334
1335 /*
1336 * Start the config command/DMA.
1337 */
1338 fxp_scb_wait(sc);
1339 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDCONFIGOFF);
1340 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1341 /* ...and wait for it to complete. */
1342 do {
1343 FXP_CDCONFIGSYNC(sc,
1344 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1345 } while ((cbp->cb_status & FXP_CB_STATUS_C) == 0);
1346
1347 /*
1348 * Initialize the station address.
1349 */
1350 cb_ias = &sc->sc_control_data->fcd_iascb;
1351 cb_ias->cb_status = 0;
1352 cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1353 cb_ias->link_addr = -1;
1354 memcpy((void *)cb_ias->macaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1355
1356 FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1357
1358 /*
1359 * Start the IAS (Individual Address Setup) command/DMA.
1360 */
1361 fxp_scb_wait(sc);
1362 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDIASOFF);
1363 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1364 /* ...and wait for it to complete. */
1365 do {
1366 FXP_CDIASSYNC(sc,
1367 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1368 } while ((cb_ias->cb_status & FXP_CB_STATUS_C) == 0);
1369
1370 /*
1371 * Initialize the transmit descriptor ring. txlast is initialized
1372 * to the end of the list so that it will wrap around to the first
1373 * descriptor when the first packet is transmitted.
1374 */
1375 for (i = 0; i < FXP_NTXCB; i++) {
1376 txd = FXP_CDTX(sc, i);
1377 memset(txd, 0, sizeof(struct fxp_cb_tx));
1378 txd->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1379 txd->tbd_array_addr = FXP_CDTBDADDR(sc, i);
1380 txd->link_addr = FXP_CDTXADDR(sc, FXP_NEXTTX(i));
1381 FXP_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1382 }
1383 sc->sc_txpending = 0;
1384 sc->sc_txdirty = 0;
1385 sc->sc_txlast = FXP_NTXCB - 1;
1386
1387 /*
1388 * Initialize the receive buffer list.
1389 */
1390 sc->sc_rxq.ifq_maxlen = FXP_NRFABUFS;
1391 while (sc->sc_rxq.ifq_len < FXP_NRFABUFS) {
1392 rxmap = FXP_RXMAP_GET(sc);
1393 if ((error = fxp_add_rfabuf(sc, rxmap, 0)) != 0) {
1394 printf("%s: unable to allocate or map rx "
1395 "buffer %d, error = %d\n",
1396 sc->sc_dev.dv_xname,
1397 sc->sc_rxq.ifq_len, error);
1398 /*
1399 * XXX Should attempt to run with fewer receive
1400 * XXX buffers instead of just failing.
1401 */
1402 FXP_RXMAP_PUT(sc, rxmap);
1403 fxp_rxdrain(sc);
1404 goto out;
1405 }
1406 }
1407 sc->sc_rxidle = 0;
1408
1409 /*
1410 * Give the transmit ring to the chip. We do this by pointing
1411 * the chip at the last descriptor (which is a NOP|SUSPEND), and
1412 * issuing a start command. It will execute the NOP and then
1413 * suspend, pointing at the first descriptor.
1414 */
1415 fxp_scb_wait(sc);
1416 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, FXP_CDTXADDR(sc, sc->sc_txlast));
1417 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1418
1419 /*
1420 * Initialize receiver buffer area - RFA.
1421 */
1422 rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
1423 fxp_scb_wait(sc);
1424 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1425 rxmap->dm_segs[0].ds_addr + RFA_ALIGNMENT_FUDGE);
1426 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_START);
1427
1428 if (sc->sc_flags & FXPF_MII) {
1429 /*
1430 * Set current media.
1431 */
1432 mii_mediachg(&sc->sc_mii);
1433 }
1434
1435 /*
1436 * ...all done!
1437 */
1438 ifp->if_flags |= IFF_RUNNING;
1439 ifp->if_flags &= ~IFF_OACTIVE;
1440
1441 /*
1442 * Start the one second timer.
1443 */
1444 timeout(fxp_tick, sc, hz);
1445
1446 /*
1447 * Attempt to start output on the interface.
1448 */
1449 fxp_start(ifp);
1450
1451 out:
1452 if (error)
1453 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1454 return (error);
1455 }
1456
1457 /*
1458 * Change media according to request.
1459 */
1460 int
1461 fxp_mii_mediachange(ifp)
1462 struct ifnet *ifp;
1463 {
1464 struct fxp_softc *sc = ifp->if_softc;
1465
1466 if (ifp->if_flags & IFF_UP)
1467 mii_mediachg(&sc->sc_mii);
1468 return (0);
1469 }
1470
1471 /*
1472 * Notify the world which media we're using.
1473 */
1474 void
1475 fxp_mii_mediastatus(ifp, ifmr)
1476 struct ifnet *ifp;
1477 struct ifmediareq *ifmr;
1478 {
1479 struct fxp_softc *sc = ifp->if_softc;
1480
1481 if(sc->sc_enabled == 0) {
1482 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1483 ifmr->ifm_status = 0;
1484 return;
1485 }
1486
1487 mii_pollstat(&sc->sc_mii);
1488 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1489 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1490 }
1491
1492 int
1493 fxp_80c24_mediachange(ifp)
1494 struct ifnet *ifp;
1495 {
1496
1497 /* Nothing to do here. */
1498 return (0);
1499 }
1500
1501 void
1502 fxp_80c24_mediastatus(ifp, ifmr)
1503 struct ifnet *ifp;
1504 struct ifmediareq *ifmr;
1505 {
1506 struct fxp_softc *sc = ifp->if_softc;
1507
1508 /*
1509 * Media is currently-selected media. We cannot determine
1510 * the link status.
1511 */
1512 ifmr->ifm_status = 0;
1513 ifmr->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
1514 }
1515
1516 /*
1517 * Add a buffer to the end of the RFA buffer list.
1518 * Return 0 if successful, error code on failure.
1519 *
1520 * The RFA struct is stuck at the beginning of mbuf cluster and the
1521 * data pointer is fixed up to point just past it.
1522 */
1523 int
1524 fxp_add_rfabuf(sc, rxmap, unload)
1525 struct fxp_softc *sc;
1526 bus_dmamap_t rxmap;
1527 int unload;
1528 {
1529 struct mbuf *m;
1530 int error;
1531
1532 MGETHDR(m, M_DONTWAIT, MT_DATA);
1533 if (m == NULL)
1534 return (ENOBUFS);
1535
1536 MCLGET(m, M_DONTWAIT);
1537 if ((m->m_flags & M_EXT) == 0) {
1538 m_freem(m);
1539 return (ENOBUFS);
1540 }
1541
1542 if (unload)
1543 bus_dmamap_unload(sc->sc_dmat, rxmap);
1544
1545 M_SETCTX(m, rxmap);
1546
1547 error = bus_dmamap_load(sc->sc_dmat, rxmap,
1548 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1549 if (error) {
1550 printf("%s: can't load rx DMA map %d, error = %d\n",
1551 sc->sc_dev.dv_xname, sc->sc_rxq.ifq_len, error);
1552 panic("fxp_add_rfabuf"); /* XXX */
1553 }
1554
1555 FXP_INIT_RFABUF(sc, m);
1556
1557 return (0);
1558 }
1559
1560 volatile int
1561 fxp_mdi_read(self, phy, reg)
1562 struct device *self;
1563 int phy;
1564 int reg;
1565 {
1566 struct fxp_softc *sc = (struct fxp_softc *)self;
1567 int count = 10000;
1568 int value;
1569
1570 CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1571 (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1572
1573 while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1574 && count--)
1575 DELAY(10);
1576
1577 if (count <= 0)
1578 printf("%s: fxp_mdi_read: timed out\n", sc->sc_dev.dv_xname);
1579
1580 return (value & 0xffff);
1581 }
1582
1583 void
1584 fxp_statchg(self)
1585 struct device *self;
1586 {
1587
1588 /* XXX Update ifp->if_baudrate */
1589 }
1590
1591 void
1592 fxp_mdi_write(self, phy, reg, value)
1593 struct device *self;
1594 int phy;
1595 int reg;
1596 int value;
1597 {
1598 struct fxp_softc *sc = (struct fxp_softc *)self;
1599 int count = 10000;
1600
1601 CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1602 (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1603 (value & 0xffff));
1604
1605 while((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1606 count--)
1607 DELAY(10);
1608
1609 if (count <= 0)
1610 printf("%s: fxp_mdi_write: timed out\n", sc->sc_dev.dv_xname);
1611 }
1612
1613 int
1614 fxp_ioctl(ifp, command, data)
1615 struct ifnet *ifp;
1616 u_long command;
1617 caddr_t data;
1618 {
1619 struct fxp_softc *sc = ifp->if_softc;
1620 struct ifreq *ifr = (struct ifreq *)data;
1621 struct ifaddr *ifa = (struct ifaddr *)data;
1622 int s, error = 0;
1623
1624 s = splnet();
1625
1626 switch (command) {
1627 case SIOCSIFADDR:
1628 if ((error = fxp_enable(sc)) != 0)
1629 break;
1630 ifp->if_flags |= IFF_UP;
1631
1632 switch (ifa->ifa_addr->sa_family) {
1633 #ifdef INET
1634 case AF_INET:
1635 if ((error = fxp_init(sc)) != 0)
1636 break;
1637 arp_ifinit(ifp, ifa);
1638 break;
1639 #endif /* INET */
1640 #ifdef NS
1641 case AF_NS:
1642 {
1643 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1644
1645 if (ns_nullhost(*ina))
1646 ina->x_host = *(union ns_host *)
1647 LLADDR(ifp->if_sadl);
1648 else
1649 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1650 ifp->if_addrlen);
1651 /* Set new address. */
1652 error = fxp_init(sc);
1653 break;
1654 }
1655 #endif /* NS */
1656 default:
1657 error = fxp_init(sc);
1658 break;
1659 }
1660 break;
1661
1662 case SIOCSIFMTU:
1663 if (ifr->ifr_mtu > ETHERMTU)
1664 error = EINVAL;
1665 else
1666 ifp->if_mtu = ifr->ifr_mtu;
1667 break;
1668
1669 case SIOCSIFFLAGS:
1670 if ((ifp->if_flags & IFF_UP) == 0 &&
1671 (ifp->if_flags & IFF_RUNNING) != 0) {
1672 /*
1673 * If interface is marked down and it is running, then
1674 * stop it.
1675 */
1676 fxp_stop(sc, 1);
1677 fxp_disable(sc);
1678 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1679 (ifp->if_flags & IFF_RUNNING) == 0) {
1680 /*
1681 * If interface is marked up and it is stopped, then
1682 * start it.
1683 */
1684 if((error = fxp_enable(sc)) != 0)
1685 break;
1686 error = fxp_init(sc);
1687 } else if ((ifp->if_flags & IFF_UP) != 0) {
1688 /*
1689 * Reset the interface to pick up change in any other
1690 * flags that affect the hardware state.
1691 */
1692 if((error = fxp_enable(sc)) != 0)
1693 break;
1694 error = fxp_init(sc);
1695 }
1696 break;
1697
1698 case SIOCADDMULTI:
1699 case SIOCDELMULTI:
1700 if(sc->sc_enabled == 0) {
1701 error = EIO;
1702 break;
1703 }
1704 error = (command == SIOCADDMULTI) ?
1705 ether_addmulti(ifr, &sc->sc_ethercom) :
1706 ether_delmulti(ifr, &sc->sc_ethercom);
1707
1708 if (error == ENETRESET) {
1709 /*
1710 * Multicast list has changed; set the hardware
1711 * filter accordingly.
1712 */
1713 if (sc->sc_txpending) {
1714 sc->sc_flags |= FXPF_WANTINIT;
1715 error = 0;
1716 } else
1717 error = fxp_init(sc);
1718 }
1719 break;
1720
1721 case SIOCSIFMEDIA:
1722 case SIOCGIFMEDIA:
1723 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
1724 break;
1725
1726 default:
1727 error = EINVAL;
1728 break;
1729 }
1730
1731 splx(s);
1732 return (error);
1733 }
1734
1735 /*
1736 * Program the multicast filter.
1737 *
1738 * This function must be called at splnet().
1739 */
1740 void
1741 fxp_mc_setup(sc)
1742 struct fxp_softc *sc;
1743 {
1744 struct fxp_cb_mcs *mcsp = &sc->sc_control_data->fcd_mcscb;
1745 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1746 struct ethercom *ec = &sc->sc_ethercom;
1747 struct ether_multi *enm;
1748 struct ether_multistep step;
1749 int nmcasts;
1750
1751 #ifdef DIAGNOSTIC
1752 if (sc->sc_txpending)
1753 panic("fxp_mc_setup: pending transmissions");
1754 #endif
1755
1756 ifp->if_flags &= ~IFF_ALLMULTI;
1757
1758 /*
1759 * Initialize multicast setup descriptor.
1760 */
1761 nmcasts = 0;
1762 ETHER_FIRST_MULTI(step, ec, enm);
1763 while (enm != NULL) {
1764 /*
1765 * Check for too many multicast addresses or if we're
1766 * listening to a range. Either way, we simply have
1767 * to accept all multicasts.
1768 */
1769 if (nmcasts >= MAXMCADDR ||
1770 memcmp(enm->enm_addrlo, enm->enm_addrhi,
1771 ETHER_ADDR_LEN) != 0) {
1772 /*
1773 * Callers of this function must do the
1774 * right thing with this. If we're called
1775 * from outside fxp_init(), the caller must
1776 * detect if the state if IFF_ALLMULTI changes.
1777 * If it does, the caller must then call
1778 * fxp_init(), since allmulti is handled by
1779 * the config block.
1780 */
1781 ifp->if_flags |= IFF_ALLMULTI;
1782 return;
1783 }
1784 memcpy((void *)&mcsp->mc_addr[nmcasts][0], enm->enm_addrlo,
1785 ETHER_ADDR_LEN);
1786 nmcasts++;
1787 ETHER_NEXT_MULTI(step, enm);
1788 }
1789
1790 mcsp->cb_status = 0;
1791 mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL;
1792 mcsp->link_addr = FXP_CDTXADDR(sc, FXP_NEXTTX(sc->sc_txlast));
1793 mcsp->mc_cnt = nmcasts * ETHER_ADDR_LEN;
1794
1795 FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1796
1797 /*
1798 * Wait until the command unit is not active. This should never
1799 * happen since nothing is queued, but make sure anyway.
1800 */
1801 while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
1802 FXP_SCB_CUS_ACTIVE)
1803 /* nothing */ ;
1804
1805 /*
1806 * Start the multicast setup command/DMA.
1807 */
1808 fxp_scb_wait(sc);
1809 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDMCSOFF);
1810 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1811
1812 /* ...and wait for it to complete. */
1813 do {
1814 FXP_CDMCSSYNC(sc,
1815 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1816 } while ((mcsp->cb_status & FXP_CB_STATUS_C) == 0);
1817 }
1818
1819 int
1820 fxp_enable(sc)
1821 struct fxp_softc *sc;
1822 {
1823
1824 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1825 if ((*sc->sc_enable)(sc) != 0) {
1826 printf("%s: device enable failed\n",
1827 sc->sc_dev.dv_xname);
1828 return (EIO);
1829 }
1830 }
1831
1832 sc->sc_enabled = 1;
1833
1834 return 0;
1835 }
1836
1837 void
1838 fxp_disable(sc)
1839 struct fxp_softc *sc;
1840 {
1841 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1842 (*sc->sc_disable)(sc);
1843 sc->sc_enabled = 0;
1844 }
1845 }
1846