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