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