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