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