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