mb86960.c revision 1.25 1 /* $NetBSD: mb86960.c,v 1.25 1998/07/05 00:51:19 jonathan Exp $ */
2
3 /*
4 * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
5 *
6 * This software may be used, modified, copied, distributed, and sold, in
7 * both source and binary form provided that the above copyright, these
8 * terms and the following disclaimer are retained. The name of the author
9 * and/or the contributor may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22 * SUCH DAMAGE.
23 */
24
25 /*
26 * Portions copyright (C) 1993, David Greenman. This software may be used,
27 * modified, copied, distributed, and sold, in both source and binary form
28 * provided that the above copyright and these terms are retained. Under no
29 * circumstances is the author responsible for the proper functioning of this
30 * software, nor does the author assume any responsibility for damages
31 * incurred with its use.
32 */
33
34 #define FE_VERSION "if_fe.c ver. 0.8"
35
36 /*
37 * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
38 * Contributed by M.S. <seki (at) sysrap.cs.fujitsu.co.jp>
39 *
40 * This version is intended to be a generic template for various
41 * MB86960A/MB86965A based Ethernet cards. It currently supports
42 * Fujitsu FMV-180 series (i.e., FMV-181 and FMV-182) and Allied-
43 * Telesis AT1700 series and RE2000 series. There are some
44 * unnecessary hooks embedded, which are primarily intended to support
45 * other types of Ethernet cards, but the author is not sure whether
46 * they are useful.
47 */
48
49 #include "opt_inet.h"
50 #include "bpfilter.h"
51 #include "rnd.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/errno.h>
56 #include <sys/ioctl.h>
57 #include <sys/mbuf.h>
58 #include <sys/socket.h>
59 #include <sys/syslog.h>
60 #include <sys/device.h>
61 #if NRND > 0
62 #include <sys/rnd.h>
63 #endif
64
65 #include <net/if.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/if_media.h>
69 #include <net/if_ether.h>
70
71 #ifdef INET
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/if_inarp.h>
77 #endif
78
79 #ifdef NS
80 #include <netns/ns.h>
81 #include <netns/ns_if.h>
82 #endif
83
84 #if NBPFILTER > 0
85 #include <net/bpf.h>
86 #include <net/bpfdesc.h>
87 #endif
88
89 #include <machine/bus.h>
90
91 #include <dev/ic/mb86960reg.h>
92 #include <dev/ic/mb86960var.h>
93
94 /* Standard driver entry points. These can be static. */
95 void mb86960_init __P((struct mb86960_softc *));
96 int mb86960_ioctl __P((struct ifnet *, u_long, caddr_t));
97 void mb86960_start __P((struct ifnet *));
98 void mb86960_reset __P((struct mb86960_softc *));
99 void mb86960_watchdog __P((struct ifnet *));
100
101 /* Local functions. Order of declaration is confused. FIXME. */
102 int mb86960_get_packet __P((struct mb86960_softc *, int));
103 void mb86960_stop __P((struct mb86960_softc *));
104 void mb86960_tint __P((struct mb86960_softc *, u_char));
105 void mb86960_rint __P((struct mb86960_softc *, u_char));
106 static __inline__
107 void mb86960_xmit __P((struct mb86960_softc *));
108 void mb86960_write_mbufs __P((struct mb86960_softc *, struct mbuf *));
109 static __inline__
110 void mb86960_droppacket __P((struct mb86960_softc *));
111 void mb86960_getmcaf __P((struct ethercom *, u_char *));
112 void mb86960_setmode __P((struct mb86960_softc *));
113 void mb86960_loadmar __P((struct mb86960_softc *));
114
115 int mb86960_enable __P((struct mb86960_softc *));
116 void mb86960_disable __P((struct mb86960_softc *));
117
118 int mb86960_mediachange __P((struct ifnet *));
119 void mb86960_mediastatus __P((struct ifnet *, struct ifmediareq *));
120
121 #if FE_DEBUG >= 1
122 void mb86960_dump __P((int, struct mb86960_softc *));
123 #endif
124
125 void
126 mb86960_attach(sc, type, myea)
127 struct mb86960_softc *sc;
128 enum mb86960_type type;
129 u_int8_t *myea;
130 {
131 bus_space_tag_t bst = sc->sc_bst;
132 bus_space_handle_t bsh = sc->sc_bsh;
133
134 sc->type = type;
135
136 /* Register values which depend on board design. */
137 sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
138 sc->proto_dlcr5 = 0;
139 sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
140
141 switch (sc->type) {
142 case MB86960_TYPE_86960:
143 sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
144 break;
145 case MB86960_TYPE_86965:
146 sc->proto_dlcr7 = FE_D7_BYTSWP_LH;
147 break;
148 }
149
150 /*
151 * Program the 86960 as follows:
152 * SRAM: 32KB, 100ns, byte-wide access.
153 * Transmission buffer: 4KB x 2.
154 * System bus interface: 16 bits.
155 * We cannot change these values but TXBSIZE, because they
156 * are hard-wired on the board. Modifying TXBSIZE will affect
157 * the driver performance.
158 */
159 sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB |
160 FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
161
162 /*
163 * Minimum initialization of the hardware.
164 * We write into registers; hope I/O ports have no
165 * overlap with other boards.
166 */
167
168 /* Initialize 86960. */
169 bus_space_write_1(bst, bsh, FE_DLCR6,
170 sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
171 delay(200);
172
173 #ifdef DIAGNOSTIC
174 if (myea == NULL) {
175 printf("%s: ethernet address shouldn't be NULL\n",
176 sc->sc_dev.dv_xname);
177 panic("NULL ethernet address");
178 }
179 #endif
180 bcopy(myea, sc->sc_enaddr, sizeof(sc->sc_enaddr));
181
182 /* Disable all interrupts. */
183 bus_space_write_1(bst, bsh, FE_DLCR2, 0);
184 bus_space_write_1(bst, bsh, FE_DLCR3, 0);
185 }
186
187 /*
188 * Install interface into kernel networking data structures
189 */
190 void
191 mb86960_config(sc, media, nmedia, defmedia)
192 struct mb86960_softc *sc;
193 int *media, nmedia, defmedia;
194 {
195 struct cfdata *cf = sc->sc_dev.dv_cfdata;
196 struct ifnet *ifp = &sc->sc_ec.ec_if;
197 int i;
198
199 /* Stop the 86960. */
200 mb86960_stop(sc);
201
202 /* Initialize ifnet structure. */
203 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
204 ifp->if_softc = sc;
205 ifp->if_start = mb86960_start;
206 ifp->if_ioctl = mb86960_ioctl;
207 ifp->if_watchdog = mb86960_watchdog;
208 ifp->if_flags =
209 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
210
211 #if FE_DEBUG >= 3
212 log(LOG_INFO, "%s: mb86960_config()\n", sc->sc_dev.dv_xname);
213 mb86960_dump(LOG_INFO, sc);
214 #endif
215
216 #if FE_SINGLE_TRANSMISSION
217 /* Override txb config to allocate minimum. */
218 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
219 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
220 #endif
221
222 /* Modify hardware config if it is requested. */
223 if ((cf->cf_flags & FE_FLAGS_OVERRIDE_DLCR6) != 0)
224 sc->proto_dlcr6 = cf->cf_flags & FE_FLAGS_DLCR6_VALUE;
225
226 /* Find TX buffer size, based on the hardware dependent proto. */
227 switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
228 case FE_D6_TXBSIZ_2x2KB:
229 sc->txb_size = 2048;
230 break;
231 case FE_D6_TXBSIZ_2x4KB:
232 sc->txb_size = 4096;
233 break;
234 case FE_D6_TXBSIZ_2x8KB:
235 sc->txb_size = 8192;
236 break;
237 default:
238 /* Oops, we can't work with single buffer configuration. */
239 #if FE_DEBUG >= 2
240 log(LOG_WARNING, "%s: strange TXBSIZ config; fixing\n",
241 sc->sc_dev.dv_xname);
242 #endif
243 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
244 sc->proto_dlcr6 |= FE_D6_TXBSIZ_2x2KB;
245 sc->txb_size = 2048;
246 break;
247 }
248
249 /* Initialize media goo. */
250 ifmedia_init(&sc->sc_media, 0, mb86960_mediachange,
251 mb86960_mediastatus);
252 if (media != NULL) {
253 for (i = 0; i < nmedia; i++)
254 ifmedia_add(&sc->sc_media, media[i], 0, NULL);
255 ifmedia_set(&sc->sc_media, defmedia);
256 } else {
257 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
258 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
259 }
260
261 /* Attach the interface. */
262 if_attach(ifp);
263 ether_ifattach(ifp, sc->sc_enaddr);
264
265 #if NBPFILTER > 0
266 /* If BPF is in the kernel, call the attach for it. */
267 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
268 #endif
269 #if NRND > 0
270 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
271 RND_TYPE_NET);
272 #endif
273 /* Print additional info when attached. */
274 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
275 ether_sprintf(sc->sc_enaddr));
276
277 #if FE_DEBUG >= 3
278 {
279 int buf, txb, bbw, sbw, ram;
280
281 buf = txb = bbw = sbw = ram = -1;
282 switch (sc->proto_dlcr6 & FE_D6_BUFSIZ) {
283 case FE_D6_BUFSIZ_8KB:
284 buf = 8;
285 break;
286 case FE_D6_BUFSIZ_16KB:
287 buf = 16;
288 break;
289 case FE_D6_BUFSIZ_32KB:
290 buf = 32;
291 break;
292 case FE_D6_BUFSIZ_64KB:
293 buf = 64;
294 break;
295 }
296 switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
297 case FE_D6_TXBSIZ_2x2KB:
298 txb = 2;
299 break;
300 case FE_D6_TXBSIZ_2x4KB:
301 txb = 4;
302 break;
303 case FE_D6_TXBSIZ_2x8KB:
304 txb = 8;
305 break;
306 }
307 switch (sc->proto_dlcr6 & FE_D6_BBW) {
308 case FE_D6_BBW_BYTE:
309 bbw = 8;
310 break;
311 case FE_D6_BBW_WORD:
312 bbw = 16;
313 break;
314 }
315 switch (sc->proto_dlcr6 & FE_D6_SBW) {
316 case FE_D6_SBW_BYTE:
317 sbw = 8;
318 break;
319 case FE_D6_SBW_WORD:
320 sbw = 16;
321 break;
322 }
323 switch (sc->proto_dlcr6 & FE_D6_SRAM) {
324 case FE_D6_SRAM_100ns:
325 ram = 100;
326 break;
327 case FE_D6_SRAM_150ns:
328 ram = 150;
329 break;
330 }
331 printf("%s: SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
332 sc->sc_dev.dv_xname, buf, bbw, ram, txb, sbw);
333 }
334 #endif
335 }
336
337 /*
338 * Media change callback.
339 */
340 int
341 mb86960_mediachange(ifp)
342 struct ifnet *ifp;
343 {
344 struct mb86960_softc *sc = ifp->if_softc;
345
346 if (sc->sc_mediachange)
347 return ((*sc->sc_mediachange)(sc));
348 return (EINVAL);
349 }
350
351 /*
352 * Media status callback.
353 */
354 void
355 mb86960_mediastatus(ifp, ifmr)
356 struct ifnet *ifp;
357 struct ifmediareq *ifmr;
358 {
359 struct mb86960_softc *sc = ifp->if_softc;
360
361 if (sc->sc_enabled == 0) {
362 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
363 ifmr->ifm_status = 0;
364 return;
365 }
366
367 if (sc->sc_mediastatus)
368 (*sc->sc_mediastatus)(sc, ifmr);
369 }
370
371 /*
372 * Reset interface.
373 */
374 void
375 mb86960_reset(sc)
376 struct mb86960_softc *sc;
377 {
378 int s;
379
380 s = splnet();
381 mb86960_stop(sc);
382 mb86960_init(sc);
383 splx(s);
384 }
385
386 /*
387 * Stop everything on the interface.
388 *
389 * All buffered packets, both transmitting and receiving,
390 * if any, will be lost by stopping the interface.
391 */
392 void
393 mb86960_stop(sc)
394 struct mb86960_softc *sc;
395 {
396 bus_space_tag_t bst = sc->sc_bst;
397 bus_space_handle_t bsh = sc->sc_bsh;
398
399 #if FE_DEBUG >= 3
400 log(LOG_INFO, "%s: top of mb86960_stop()\n", sc->sc_dev.dv_xname);
401 mb86960_dump(LOG_INFO, sc);
402 #endif
403
404 /* Disable interrupts. */
405 bus_space_write_1(bst, bsh, FE_DLCR2, 0x00);
406 bus_space_write_1(bst, bsh, FE_DLCR3, 0x00);
407
408 /* Stop interface hardware. */
409 delay(200);
410 bus_space_write_1(bst, bsh, FE_DLCR6,
411 sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
412 delay(200);
413
414 /* Clear all interrupt status. */
415 bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF);
416 bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF);
417
418 /* Put the chip in stand-by mode. */
419 delay(200);
420 bus_space_write_1(bst, bsh, FE_DLCR7,
421 sc->proto_dlcr7 | FE_D7_POWER_DOWN);
422 delay(200);
423
424 /* MAR loading can be delayed. */
425 sc->filter_change = 0;
426
427 /* Call a hook. */
428 if (sc->stop_card)
429 (*sc->stop_card)(sc);
430
431 #if DEBUG >= 3
432 log(LOG_INFO, "%s: end of mb86960_stop()\n", sc->sc_dev.dv_xname);
433 mb86960_dump(LOG_INFO, sc);
434 #endif
435 }
436
437 /*
438 * Device timeout/watchdog routine. Entered if the device neglects to
439 * generate an interrupt after a transmit has been started on it.
440 */
441 void
442 mb86960_watchdog(ifp)
443 struct ifnet *ifp;
444 {
445 struct mb86960_softc *sc = ifp->if_softc;
446
447 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
448 #if FE_DEBUG >= 3
449 mb86960_dump(LOG_INFO, sc);
450 #endif
451
452 /* Record how many packets are lost by this accident. */
453 sc->sc_ec.ec_if.if_oerrors += sc->txb_sched + sc->txb_count;
454
455 mb86960_reset(sc);
456 }
457
458 /*
459 * Drop (skip) a packet from receive buffer in 86960 memory.
460 */
461 static __inline__ void
462 mb86960_droppacket(sc)
463 struct mb86960_softc *sc;
464 {
465 bus_space_tag_t bst = sc->sc_bst;
466 bus_space_handle_t bsh = sc->sc_bsh;
467
468 bus_space_write_1(bst, bsh, FE_BMPR14, FE_B14_FILTER | FE_B14_SKIP);
469 }
470
471 /*
472 * Initialize device.
473 */
474 void
475 mb86960_init(sc)
476 struct mb86960_softc *sc;
477 {
478 bus_space_tag_t bst = sc->sc_bst;
479 bus_space_handle_t bsh = sc->sc_bsh;
480 struct ifnet *ifp = &sc->sc_ec.ec_if;
481 int i;
482
483 #if FE_DEBUG >= 3
484 log(LOG_INFO, "%s: top of mb86960_init()\n", sc->sc_dev.dv_xname);
485 mb86960_dump(LOG_INFO, sc);
486 #endif
487
488 /* Reset transmitter flags. */
489 ifp->if_flags &= ~IFF_OACTIVE;
490 ifp->if_timer = 0;
491
492 sc->txb_free = sc->txb_size;
493 sc->txb_count = 0;
494 sc->txb_sched = 0;
495
496 /* Do any card-specific initialization, if applicable. */
497 if (sc->init_card)
498 (*sc->init_card)(sc);
499
500 #if FE_DEBUG >= 3
501 log(LOG_INFO, "%s: after init hook\n", sc->sc_dev.dv_xname);
502 mb86960_dump(LOG_INFO, sc);
503 #endif
504
505 /*
506 * Make sure to disable the chip, also.
507 * This may also help re-programming the chip after
508 * hot insertion of PCMCIAs.
509 */
510 bus_space_write_1(bst, bsh, FE_DLCR6,
511 sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
512 delay(200);
513
514 /* Power up the chip and select register bank for DLCRs. */
515 bus_space_write_1(bst, bsh, FE_DLCR7,
516 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP);
517 delay(200);
518
519 /* Feed the station address. */
520 bus_space_write_region_1(bst, bsh, FE_DLCR8,
521 sc->sc_enaddr, ETHER_ADDR_LEN);
522
523 /* Select the BMPR bank for runtime register access. */
524 bus_space_write_1(bst, bsh, FE_DLCR7,
525 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
526
527 /* Initialize registers. */
528 bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF); /* Clear all bits. */
529 bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF); /* ditto. */
530 bus_space_write_1(bst, bsh, FE_DLCR2, 0x00);
531 bus_space_write_1(bst, bsh, FE_DLCR3, 0x00);
532 bus_space_write_1(bst, bsh, FE_DLCR4, sc->proto_dlcr4);
533 bus_space_write_1(bst, bsh, FE_DLCR5, sc->proto_dlcr5);
534 bus_space_write_1(bst, bsh, FE_BMPR10, 0x00);
535 bus_space_write_1(bst, bsh, FE_BMPR11, FE_B11_CTRL_SKIP);
536 bus_space_write_1(bst, bsh, FE_BMPR12, 0x00);
537 bus_space_write_1(bst, bsh, FE_BMPR13, sc->proto_bmpr13);
538 bus_space_write_1(bst, bsh, FE_BMPR14, FE_B14_FILTER);
539 bus_space_write_1(bst, bsh, FE_BMPR15, 0x00);
540
541 #if FE_DEBUG >= 3
542 log(LOG_INFO, "%s: just before enabling DLC\n", sc->sc_dev.dv_xname);
543 mb86960_dump(LOG_INFO, sc);
544 #endif
545
546 /* Enable interrupts. */
547 bus_space_write_1(bst, bsh, FE_DLCR2, FE_TMASK);
548 bus_space_write_1(bst, bsh, FE_DLCR3, FE_RMASK);
549
550 /* Enable transmitter and receiver. */
551 delay(200);
552 bus_space_write_1(bst, bsh, FE_DLCR6,
553 sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
554 delay(200);
555
556 #if FE_DEBUG >= 3
557 log(LOG_INFO, "%s: just after enabling DLC\n", sc->sc_dev.dv_xname);
558 mb86960_dump(LOG_INFO, sc);
559 #endif
560
561 /*
562 * Make sure to empty the receive buffer.
563 *
564 * This may be redundant, but *if* the receive buffer were full
565 * at this point, the driver would hang. I have experienced
566 * some strange hangups just after UP. I hope the following
567 * code solve the problem.
568 *
569 * I have changed the order of hardware initialization.
570 * I think the receive buffer cannot have any packets at this
571 * point in this version. The following code *must* be
572 * redundant now. FIXME.
573 */
574 for (i = 0; i < FE_MAX_RECV_COUNT; i++) {
575 if (bus_space_read_1(bst, bsh, FE_DLCR5) & FE_D5_BUFEMP)
576 break;
577 mb86960_droppacket(sc);
578 }
579 #if FE_DEBUG >= 1
580 if (i >= FE_MAX_RECV_COUNT)
581 log(LOG_ERR, "%s: cannot empty receive buffer\n",
582 sc->sc_dev.dv_xname);
583 #endif
584 #if FE_DEBUG >= 3
585 if (i < FE_MAX_RECV_COUNT)
586 log(LOG_INFO, "%s: receive buffer emptied (%d)\n",
587 sc->sc_dev.dv_xname, i);
588 #endif
589
590 #if FE_DEBUG >= 3
591 log(LOG_INFO, "%s: after ERB loop\n", sc->sc_dev.dv_xname);
592 mb86960_dump(LOG_INFO, sc);
593 #endif
594
595 /* Do we need this here? */
596 bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF); /* Clear all bits. */
597 bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF); /* ditto. */
598
599 #if FE_DEBUG >= 3
600 log(LOG_INFO, "%s: after FIXME\n", sc->sc_dev.dv_xname);
601 mb86960_dump(LOG_INFO, sc);
602 #endif
603
604 /* Set 'running' flag. */
605 ifp->if_flags |= IFF_RUNNING;
606
607 /*
608 * At this point, the interface is runnung properly,
609 * except that it receives *no* packets. we then call
610 * mb86960_setmode() to tell the chip what packets to be
611 * received, based on the if_flags and multicast group
612 * list. It completes the initialization process.
613 */
614 mb86960_setmode(sc);
615
616 #if FE_DEBUG >= 3
617 log(LOG_INFO, "%s: after setmode\n", sc->sc_dev.dv_xname);
618 mb86960_dump(LOG_INFO, sc);
619 #endif
620
621 /* ...and attempt to start output. */
622 mb86960_start(ifp);
623
624 #if FE_DEBUG >= 3
625 log(LOG_INFO, "%s: end of mb86960_init()\n", sc->sc_dev.dv_xname);
626 mb86960_dump(LOG_INFO, sc);
627 #endif
628 }
629
630 /*
631 * This routine actually starts the transmission on the interface
632 */
633 static __inline__ void
634 mb86960_xmit(sc)
635 struct mb86960_softc *sc;
636 {
637 bus_space_tag_t bst = sc->sc_bst;
638 bus_space_handle_t bsh = sc->sc_bsh;
639
640 /*
641 * Set a timer just in case we never hear from the board again.
642 * We use longer timeout for multiple packet transmission.
643 * I'm not sure this timer value is appropriate. FIXME.
644 */
645 sc->sc_ec.ec_if.if_timer = 1 + sc->txb_count;
646
647 /* Update txb variables. */
648 sc->txb_sched = sc->txb_count;
649 sc->txb_count = 0;
650 sc->txb_free = sc->txb_size;
651
652 #if FE_DELAYED_PADDING
653 /* Omit the postponed padding process. */
654 sc->txb_padding = 0;
655 #endif
656
657 /* Start transmitter, passing packets in TX buffer. */
658 bus_space_write_1(bst, bsh, FE_BMPR10, sc->txb_sched | FE_B10_START);
659 }
660
661 /*
662 * Start output on interface.
663 * We make two assumptions here:
664 * 1) that the current priority is set to splnet _before_ this code
665 * is called *and* is returned to the appropriate priority after
666 * return
667 * 2) that the IFF_OACTIVE flag is checked before this code is called
668 * (i.e. that the output part of the interface is idle)
669 */
670 void
671 mb86960_start(ifp)
672 struct ifnet *ifp;
673 {
674 struct mb86960_softc *sc = ifp->if_softc;
675 struct mbuf *m;
676
677 #if FE_DEBUG >= 1
678 /* Just a sanity check. */
679 if ((sc->txb_count == 0) != (sc->txb_free == sc->txb_size)) {
680 /*
681 * Txb_count and txb_free co-works to manage the
682 * transmission buffer. Txb_count keeps track of the
683 * used potion of the buffer, while txb_free does unused
684 * potion. So, as long as the driver runs properly,
685 * txb_count is zero if and only if txb_free is same
686 * as txb_size (which represents whole buffer.)
687 */
688 log(LOG_ERR, "%s: inconsistent txb variables (%d, %d)\n",
689 sc->sc_dev.dv_xname, sc->txb_count, sc->txb_free);
690 /*
691 * So, what should I do, then?
692 *
693 * We now know txb_count and txb_free contradicts. We
694 * cannot, however, tell which is wrong. More
695 * over, we cannot peek 86960 transmission buffer or
696 * reset the transmission buffer. (In fact, we can
697 * reset the entire interface. I don't want to do it.)
698 *
699 * If txb_count is incorrect, leaving it as is will cause
700 * sending of gabages after next interrupt. We have to
701 * avoid it. Hence, we reset the txb_count here. If
702 * txb_free was incorrect, resetting txb_count just loose
703 * some packets. We can live with it.
704 */
705 sc->txb_count = 0;
706 }
707 #endif
708
709 #if FE_DEBUG >= 1
710 /*
711 * First, see if there are buffered packets and an idle
712 * transmitter - should never happen at this point.
713 */
714 if ((sc->txb_count > 0) && (sc->txb_sched == 0)) {
715 log(LOG_ERR, "%s: transmitter idle with %d buffered packets\n",
716 sc->sc_dev.dv_xname, sc->txb_count);
717 mb86960_xmit(sc);
718 }
719 #endif
720
721 /*
722 * Stop accepting more transmission packets temporarily, when
723 * a filter change request is delayed. Updating the MARs on
724 * 86960 flushes the transmisstion buffer, so it is delayed
725 * until all buffered transmission packets have been sent
726 * out.
727 */
728 if (sc->filter_change) {
729 /*
730 * Filter change requst is delayed only when the DLC is
731 * working. DLC soon raise an interrupt after finishing
732 * the work.
733 */
734 goto indicate_active;
735 }
736
737 for (;;) {
738 /*
739 * See if there is room to put another packet in the buffer.
740 * We *could* do better job by peeking the send queue to
741 * know the length of the next packet. Current version just
742 * tests against the worst case (i.e., longest packet). FIXME.
743 *
744 * When adding the packet-peek feature, don't forget adding a
745 * test on txb_count against QUEUEING_MAX.
746 * There is a little chance the packet count exceeds
747 * the limit. Assume transmission buffer is 8KB (2x8KB
748 * configuration) and an application sends a bunch of small
749 * (i.e., minimum packet sized) packets rapidly. An 8KB
750 * buffer can hold 130 blocks of 62 bytes long...
751 */
752 if (sc->txb_free < ETHER_MAX_LEN + FE_DATA_LEN_LEN) {
753 /* No room. */
754 goto indicate_active;
755 }
756
757 #if FE_SINGLE_TRANSMISSION
758 if (sc->txb_count > 0) {
759 /* Just one packet per a transmission buffer. */
760 goto indicate_active;
761 }
762 #endif
763
764 /*
765 * Get the next mbuf chain for a packet to send.
766 */
767 IF_DEQUEUE(&ifp->if_snd, m);
768 if (m == 0) {
769 /* No more packets to send. */
770 goto indicate_inactive;
771 }
772
773 #if NBPFILTER > 0
774 /* Tap off here if there is a BPF listener. */
775 if (ifp->if_bpf)
776 bpf_mtap(ifp->if_bpf, m);
777 #endif
778
779 /*
780 * Copy the mbuf chain into the transmission buffer.
781 * txb_* variables are updated as necessary.
782 */
783 mb86960_write_mbufs(sc, m);
784
785 m_freem(m);
786
787 /* Start transmitter if it's idle. */
788 if (sc->txb_sched == 0)
789 mb86960_xmit(sc);
790 }
791
792 indicate_inactive:
793 /*
794 * We are using the !OACTIVE flag to indicate to
795 * the outside world that we can accept an
796 * additional packet rather than that the
797 * transmitter is _actually_ active. Indeed, the
798 * transmitter may be active, but if we haven't
799 * filled all the buffers with data then we still
800 * want to accept more.
801 */
802 ifp->if_flags &= ~IFF_OACTIVE;
803 return;
804
805 indicate_active:
806 /*
807 * The transmitter is active, and there are no room for
808 * more outgoing packets in the transmission buffer.
809 */
810 ifp->if_flags |= IFF_OACTIVE;
811 return;
812 }
813
814 /*
815 * Transmission interrupt handler
816 * The control flow of this function looks silly. FIXME.
817 */
818 void
819 mb86960_tint(sc, tstat)
820 struct mb86960_softc *sc;
821 u_char tstat;
822 {
823 bus_space_tag_t bst = sc->sc_bst;
824 bus_space_handle_t bsh = sc->sc_bsh;
825 struct ifnet *ifp = &sc->sc_ec.ec_if;
826 int left;
827 int col;
828
829 /*
830 * Handle "excessive collision" interrupt.
831 */
832 if (tstat & FE_D0_COLL16) {
833 /*
834 * Find how many packets (including this collided one)
835 * are left unsent in transmission buffer.
836 */
837 left = bus_space_read_1(bst, bsh, FE_BMPR10);
838
839 #if FE_DEBUG >= 2
840 log(LOG_WARNING, "%s: excessive collision (%d/%d)\n",
841 sc->sc_dev.dv_xname, left, sc->txb_sched);
842 #endif
843 #if FE_DEBUG >= 3
844 mb86960_dump(LOG_INFO, sc);
845 #endif
846
847 /*
848 * Update statistics.
849 */
850 ifp->if_collisions += 16;
851 ifp->if_oerrors++;
852 ifp->if_opackets += sc->txb_sched - left;
853
854 /*
855 * Collision statistics has been updated.
856 * Clear the collision flag on 86960 now to avoid confusion.
857 */
858 bus_space_write_1(bst, bsh, FE_DLCR0, FE_D0_COLLID);
859
860 /*
861 * Restart transmitter, skipping the
862 * collided packet.
863 *
864 * We *must* skip the packet to keep network running
865 * properly. Excessive collision error is an
866 * indication of the network overload. If we
867 * tried sending the same packet after excessive
868 * collision, the network would be filled with
869 * out-of-time packets. Packets belonging
870 * to reliable transport (such as TCP) are resent
871 * by some upper layer.
872 */
873 bus_space_write_1(bst, bsh, FE_BMPR11,
874 FE_B11_CTRL_SKIP | FE_B11_MODE1);
875 sc->txb_sched = left - 1;
876 }
877
878 /*
879 * Handle "transmission complete" interrupt.
880 */
881 if (tstat & FE_D0_TXDONE) {
882 /*
883 * Add in total number of collisions on last
884 * transmission. We also clear "collision occurred" flag
885 * here.
886 *
887 * 86960 has a design flow on collision count on multiple
888 * packet transmission. When we send two or more packets
889 * with one start command (that's what we do when the
890 * transmission queue is clauded), 86960 informs us number
891 * of collisions occured on the last packet on the
892 * transmission only. Number of collisions on previous
893 * packets are lost. I have told that the fact is clearly
894 * stated in the Fujitsu document.
895 *
896 * I considered not to mind it seriously. Collision
897 * count is not so important, anyway. Any comments? FIXME.
898 */
899
900 if (bus_space_read_1(bst, bsh, FE_DLCR0) & FE_D0_COLLID) {
901 /* Clear collision flag. */
902 bus_space_write_1(bst, bsh, FE_DLCR0, FE_D0_COLLID);
903
904 /* Extract collision count from 86960. */
905 col = bus_space_read_1(bst, bsh, FE_DLCR4) & FE_D4_COL;
906 if (col == 0) {
907 /*
908 * Status register indicates collisions,
909 * while the collision count is zero.
910 * This can happen after multiple packet
911 * transmission, indicating that one or more
912 * previous packet(s) had been collided.
913 *
914 * Since the accurate number of collisions
915 * has been lost, we just guess it as 1;
916 * Am I too optimistic? FIXME.
917 */
918 col = 1;
919 } else
920 col >>= FE_D4_COL_SHIFT;
921 ifp->if_collisions += col;
922 #if FE_DEBUG >= 4
923 log(LOG_WARNING, "%s: %d collision%s (%d)\n",
924 sc->sc_dev.dv_xname, col, col == 1 ? "" : "s",
925 sc->txb_sched);
926 #endif
927 }
928
929 /*
930 * Update total number of successfully
931 * transmitted packets.
932 */
933 ifp->if_opackets += sc->txb_sched;
934 sc->txb_sched = 0;
935 }
936
937 if (sc->txb_sched == 0) {
938 /*
939 * The transmitter is no more active.
940 * Reset output active flag and watchdog timer.
941 */
942 ifp->if_flags &= ~IFF_OACTIVE;
943 ifp->if_timer = 0;
944
945 /*
946 * If more data is ready to transmit in the buffer, start
947 * transmitting them. Otherwise keep transmitter idle,
948 * even if more data is queued. This gives receive
949 * process a slight priority.
950 */
951 if (sc->txb_count > 0)
952 mb86960_xmit(sc);
953 }
954 }
955
956 /*
957 * Ethernet interface receiver interrupt.
958 */
959 void
960 mb86960_rint(sc, rstat)
961 struct mb86960_softc *sc;
962 u_char rstat;
963 {
964 bus_space_tag_t bst = sc->sc_bst;
965 bus_space_handle_t bsh = sc->sc_bsh;
966 struct ifnet *ifp = &sc->sc_ec.ec_if;
967 int len;
968 u_char status;
969 int i;
970
971 /*
972 * Update statistics if this interrupt is caused by an error.
973 */
974 if (rstat & (FE_D1_OVRFLO | FE_D1_CRCERR | FE_D1_ALGERR |
975 FE_D1_SRTPKT)) {
976 #if FE_DEBUG >= 3
977 log(LOG_WARNING, "%s: receive error: %b\n",
978 sc->sc_dev.dv_xname, rstat, FE_D1_ERRBITS);
979 #endif
980 ifp->if_ierrors++;
981 }
982
983 /*
984 * MB86960 has a flag indicating "receive queue empty."
985 * We just loop cheking the flag to pull out all received
986 * packets.
987 *
988 * We limit the number of iterrations to avoid infinite loop.
989 * It can be caused by a very slow CPU (some broken
990 * peripheral may insert incredible number of wait cycles)
991 * or, worse, by a broken MB86960 chip.
992 */
993 for (i = 0; i < FE_MAX_RECV_COUNT; i++) {
994 /* Stop the iterration if 86960 indicates no packets. */
995 if (bus_space_read_1(bst, bsh, FE_DLCR5) & FE_D5_BUFEMP)
996 break;
997
998 /*
999 * Extract A receive status byte.
1000 * As our 86960 is in 16 bit bus access mode, we have to
1001 * use inw() to get the status byte. The significant
1002 * value is returned in lower 8 bits.
1003 */
1004 status = (u_char)bus_space_read_2(bst, bsh, FE_BMPR8);
1005 #if FE_DEBUG >= 4
1006 log(LOG_INFO, "%s: receive status = %02x\n",
1007 sc->sc_dev.dv_xname, status);
1008 #endif
1009
1010 /*
1011 * If there was an error, update statistics and drop
1012 * the packet, unless the interface is in promiscuous
1013 * mode.
1014 */
1015 if ((status & 0xF0) != 0x20) { /* XXXX ? */
1016 if ((ifp->if_flags & IFF_PROMISC) == 0) {
1017 ifp->if_ierrors++;
1018 mb86960_droppacket(sc);
1019 continue;
1020 }
1021 }
1022
1023 /*
1024 * Extract the packet length.
1025 * It is a sum of a header (14 bytes) and a payload.
1026 * CRC has been stripped off by the 86960.
1027 */
1028 len = bus_space_read_2(bst, bsh, FE_BMPR8);
1029
1030 /*
1031 * MB86965 checks the packet length and drop big packet
1032 * before passing it to us. There are no chance we can
1033 * get [crufty] packets. Hence, if the length exceeds
1034 * the specified limit, it means some serious failure,
1035 * such as out-of-sync on receive buffer management.
1036 *
1037 * Is this statement true? FIXME.
1038 */
1039 if (len > ETHER_MAX_LEN || len < ETHER_HDR_SIZE) {
1040 #if FE_DEBUG >= 2
1041 log(LOG_WARNING,
1042 "%s: received a %s packet? (%u bytes)\n",
1043 sc->sc_dev.dv_xname,
1044 len < ETHER_HDR_SIZE ? "partial" : "big", len);
1045 #endif
1046 ifp->if_ierrors++;
1047 mb86960_droppacket(sc);
1048 continue;
1049 }
1050
1051 /*
1052 * Check for a short (RUNT) packet. We *do* check
1053 * but do nothing other than print a message.
1054 * Short packets are illegal, but does nothing bad
1055 * if it carries data for upper layer.
1056 */
1057 #if FE_DEBUG >= 2
1058 if (len < ETHER_MIN_LEN) {
1059 log(LOG_WARNING,
1060 "%s: received a short packet? (%u bytes)\n",
1061 sc->sc_dev.dv_xname, len);
1062 }
1063 #endif
1064
1065 /*
1066 * Go get a packet.
1067 */
1068 if (!mb86960_get_packet(sc, len)) {
1069 /* Skip a packet, updating statistics. */
1070 #if FE_DEBUG >= 2
1071 log(LOG_WARNING,
1072 "%s: out of mbufs; dropping packet (%u bytes)\n",
1073 sc->sc_dev.dv_xname, len);
1074 #endif
1075 ifp->if_ierrors++;
1076 mb86960_droppacket(sc);
1077
1078 /*
1079 * We stop receiving packets, even if there are
1080 * more in the buffer. We hope we can get more
1081 * mbufs next time.
1082 */
1083 return;
1084 }
1085
1086 /* Successfully received a packet. Update stat. */
1087 ifp->if_ipackets++;
1088 }
1089 }
1090
1091 /*
1092 * Ethernet interface interrupt processor
1093 */
1094 int
1095 mb86960_intr(arg)
1096 void *arg;
1097 {
1098 struct mb86960_softc *sc = arg;
1099 bus_space_tag_t bst = sc->sc_bst;
1100 bus_space_handle_t bsh = sc->sc_bsh;
1101 struct ifnet *ifp = &sc->sc_ec.ec_if;
1102 u_char tstat, rstat;
1103
1104 if (sc->sc_enabled == 0)
1105 return (0);
1106
1107 #if FE_DEBUG >= 4
1108 log(LOG_INFO, "%s: mb86960_intr()\n", sc->sc_dev.dv_xname);
1109 mb86960_dump(LOG_INFO, sc);
1110 #endif
1111
1112 /*
1113 * Get interrupt conditions, masking unneeded flags.
1114 */
1115 tstat = bus_space_read_1(bst, bsh, FE_DLCR0) & FE_TMASK;
1116 rstat = bus_space_read_1(bst, bsh, FE_DLCR1) & FE_RMASK;
1117 if (tstat == 0 && rstat == 0)
1118 return (0);
1119
1120 /*
1121 * Loop until there are no more new interrupt conditions.
1122 */
1123 for (;;) {
1124 /*
1125 * Reset the conditions we are acknowledging.
1126 */
1127 bus_space_write_1(bst, bsh, FE_DLCR0, tstat);
1128 bus_space_write_1(bst, bsh, FE_DLCR1, rstat);
1129
1130 /*
1131 * Handle transmitter interrupts. Handle these first because
1132 * the receiver will reset the board under some conditions.
1133 */
1134 if (tstat != 0)
1135 mb86960_tint(sc, tstat);
1136
1137 /*
1138 * Handle receiver interrupts.
1139 */
1140 if (rstat != 0)
1141 mb86960_rint(sc, rstat);
1142
1143 /*
1144 * Update the multicast address filter if it is
1145 * needed and possible. We do it now, because
1146 * we can make sure the transmission buffer is empty,
1147 * and there is a good chance that the receive queue
1148 * is empty. It will minimize the possibility of
1149 * packet lossage.
1150 */
1151 if (sc->filter_change &&
1152 sc->txb_count == 0 && sc->txb_sched == 0) {
1153 mb86960_loadmar(sc);
1154 ifp->if_flags &= ~IFF_OACTIVE;
1155 }
1156
1157 /*
1158 * If it looks like the transmitter can take more data,
1159 * attempt to start output on the interface. This is done
1160 * after handling the receiver interrupt to give the
1161 * receive operation priority.
1162 */
1163 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1164 mb86960_start(ifp);
1165
1166 #if NRND > 0
1167 if (rstat != 0 || tstat != 0)
1168 rnd_add_uint32(&sc->rnd_source, rstat + tstat);
1169 #endif
1170
1171 /*
1172 * Get interrupt conditions, masking unneeded flags.
1173 */
1174 tstat = bus_space_read_1(bst, bsh, FE_DLCR0) & FE_TMASK;
1175 rstat = bus_space_read_1(bst, bsh, FE_DLCR1) & FE_RMASK;
1176 if (tstat == 0 && rstat == 0)
1177 return (1);
1178 }
1179 }
1180
1181 /*
1182 * Process an ioctl request. This code needs some work - it looks pretty ugly.
1183 */
1184 int
1185 mb86960_ioctl(ifp, cmd, data)
1186 struct ifnet *ifp;
1187 u_long cmd;
1188 caddr_t data;
1189 {
1190 struct mb86960_softc *sc = ifp->if_softc;
1191 struct ifaddr *ifa = (struct ifaddr *)data;
1192 struct ifreq *ifr = (struct ifreq *)data;
1193 int s, error = 0;
1194
1195 #if FE_DEBUG >= 3
1196 log(LOG_INFO, "%s: ioctl(%lx)\n", sc->sc_dev.dv_xname, cmd);
1197 #endif
1198
1199 s = splnet();
1200
1201 switch (cmd) {
1202 case SIOCSIFADDR:
1203 if ((error = mb86960_enable(sc)) != 0)
1204 break;
1205 ifp->if_flags |= IFF_UP;
1206
1207 switch (ifa->ifa_addr->sa_family) {
1208 #ifdef INET
1209 case AF_INET:
1210 mb86960_init(sc);
1211 arp_ifinit(ifp, ifa);
1212 break;
1213 #endif
1214 #ifdef NS
1215 case AF_NS:
1216 {
1217 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1218
1219 if (ns_nullhost(*ina))
1220 ina->x_host =
1221 *(union ns_host *)LLADDR(ifp->if_sadl);
1222 else {
1223 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1224 ETHER_ADDR_LEN);
1225 }
1226 /* Set new address. */
1227 mb86960_init(sc);
1228 break;
1229 }
1230 #endif
1231 default:
1232 mb86960_init(sc);
1233 break;
1234 }
1235 break;
1236
1237 case SIOCSIFFLAGS:
1238 if ((ifp->if_flags & IFF_UP) == 0 &&
1239 (ifp->if_flags & IFF_RUNNING) != 0) {
1240 /*
1241 * If interface is marked down and it is running, then
1242 * stop it.
1243 */
1244 mb86960_stop(sc);
1245 ifp->if_flags &= ~IFF_RUNNING;
1246 mb86960_disable(sc);
1247 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1248 (ifp->if_flags & IFF_RUNNING) == 0) {
1249 /*
1250 * If interface is marked up and it is stopped, then
1251 * start it.
1252 */
1253 if ((error = mb86960_enable(sc)) != 0)
1254 break;
1255 mb86960_init(sc);
1256 } else if (sc->sc_enabled) {
1257 /*
1258 * Reset the interface to pick up changes in any other
1259 * flags that affect hardware registers.
1260 */
1261 mb86960_setmode(sc);
1262 }
1263 #if DEBUG >= 1
1264 /* "ifconfig fe0 debug" to print register dump. */
1265 if (ifp->if_flags & IFF_DEBUG) {
1266 log(LOG_INFO, "%s: SIOCSIFFLAGS(DEBUG)\n",
1267 sc->sc_dev.dv_xname);
1268 mb86960_dump(LOG_DEBUG, sc);
1269 }
1270 #endif
1271 break;
1272
1273 case SIOCADDMULTI:
1274 case SIOCDELMULTI:
1275 if (sc->sc_enabled == 0) {
1276 error = EIO;
1277 break;
1278 }
1279
1280 /* Update our multicast list. */
1281 error = (cmd == SIOCADDMULTI) ?
1282 ether_addmulti(ifr, &sc->sc_ec) :
1283 ether_delmulti(ifr, &sc->sc_ec);
1284
1285 if (error == ENETRESET) {
1286 /*
1287 * Multicast list has changed; set the hardware filter
1288 * accordingly.
1289 */
1290 mb86960_setmode(sc);
1291 error = 0;
1292 }
1293 break;
1294
1295 case SIOCGIFMEDIA:
1296 case SIOCSIFMEDIA:
1297 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1298 break;
1299
1300 default:
1301 error = EINVAL;
1302 break;
1303 }
1304
1305 splx(s);
1306 return (error);
1307 }
1308
1309 /*
1310 * Retreive packet from receive buffer and send to the next level up via
1311 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1312 * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
1313 */
1314 int
1315 mb86960_get_packet(sc, len)
1316 struct mb86960_softc *sc;
1317 int len;
1318 {
1319 bus_space_tag_t bst = sc->sc_bst;
1320 bus_space_handle_t bsh = sc->sc_bsh;
1321 struct ifnet *ifp = &sc->sc_ec.ec_if;
1322 struct ether_header *eh;
1323 struct mbuf *m;
1324
1325 /* Allocate a header mbuf. */
1326 MGETHDR(m, M_DONTWAIT, MT_DATA);
1327 if (m == 0)
1328 return (0);
1329 m->m_pkthdr.rcvif = ifp;
1330 m->m_pkthdr.len = len;
1331
1332 /* The following silliness is to make NFS happy. */
1333 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1334 #define EOFF (EROUND - sizeof(struct ether_header))
1335
1336 /*
1337 * Our strategy has one more problem. There is a policy on
1338 * mbuf cluster allocation. It says that we must have at
1339 * least MINCLSIZE (208 bytes) to allocate a cluster. For a
1340 * packet of a size between (MHLEN - 2) to (MINCLSIZE - 2),
1341 * our code violates the rule...
1342 * On the other hand, the current code is short, simle,
1343 * and fast, however. It does no harmful thing, just waists
1344 * some memory. Any comments? FIXME.
1345 */
1346
1347 /* Attach a cluster if this packet doesn't fit in a normal mbuf. */
1348 if (len > MHLEN - EOFF) {
1349 MCLGET(m, M_DONTWAIT);
1350 if ((m->m_flags & M_EXT) == 0) {
1351 m_freem(m);
1352 return (0);
1353 }
1354 }
1355
1356 /*
1357 * The following assumes there is room for the ether header in the
1358 * header mbuf.
1359 */
1360 m->m_data += EOFF;
1361 eh = mtod(m, struct ether_header *);
1362
1363 /* Set the length of this packet. */
1364 m->m_len = len;
1365
1366 /* Get a packet. */
1367 bus_space_read_multi_2(bst, bsh, FE_BMPR8, m->m_data, (len + 1) >> 1);
1368
1369 #if NBPFILTER > 0
1370 /*
1371 * Check if there's a BPF listener on this interface. If so, hand off
1372 * the raw packet to bpf.
1373 */
1374 if (ifp->if_bpf) {
1375 bpf_mtap(ifp->if_bpf, m);
1376
1377 /*
1378 * Note that the interface cannot be in promiscuous mode if
1379 * there are no BPF listeners. And if we are in promiscuous
1380 * mode, we have to check if this packet is really ours.
1381 */
1382 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1383 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1384 bcmp(eh->ether_dhost, sc->sc_enaddr,
1385 sizeof(eh->ether_dhost)) != 0) {
1386 m_freem(m);
1387 return (1);
1388 }
1389 }
1390 #endif
1391
1392 /* Fix up data start offset in mbuf to point past ether header. */
1393 m_adj(m, sizeof(struct ether_header));
1394 ether_input(ifp, eh, m);
1395 return (1);
1396 }
1397
1398 /*
1399 * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
1400 * Returns number of bytes actually written, including length word.
1401 *
1402 * If an mbuf chain is too long for an Ethernet frame, it is not sent.
1403 * Packets shorter than Ethernet minimum are legal, and we pad them
1404 * before sending out. An exception is "partial" packets which are
1405 * shorter than mandatory Ethernet header.
1406 *
1407 * I wrote a code for an experimental "delayed padding" technique.
1408 * When employed, it postpones the padding process for short packets.
1409 * If xmit() occured at the moment, the padding process is omitted, and
1410 * garbages are sent as pad data. If next packet is stored in the
1411 * transmission buffer before xmit(), write_mbuf() pads the previous
1412 * packet before transmitting new packet. This *may* gain the
1413 * system performance (slightly).
1414 */
1415 void
1416 mb86960_write_mbufs(sc, m)
1417 struct mb86960_softc *sc;
1418 struct mbuf *m;
1419 {
1420 bus_space_tag_t bst = sc->sc_bst;
1421 bus_space_handle_t bsh = sc->sc_bsh;
1422 u_char *data;
1423 u_short savebyte; /* WARNING: Architecture dependent! */
1424 int totlen, len, wantbyte;
1425 #if FE_DEBUG >= 2
1426 struct mbuf *mp;
1427 #endif
1428
1429 /* XXX thorpej 960116 - quiet bogus compiler warning. */
1430 savebyte = 0;
1431
1432 #if FE_DELAYED_PADDING
1433 /* Do the "delayed padding." */
1434 len = sc->txb_padding >> 1;
1435 if (len > 0) {
1436 while (--len >= 0)
1437 bus_space_write_2(bst, bsh, FE_BMPR8, 0);
1438 sc->txb_padding = 0;
1439 }
1440 #endif
1441
1442 /* We need to use m->m_pkthdr.len, so require the header */
1443 if ((m->m_flags & M_PKTHDR) == 0)
1444 panic("mb86960_write_mbufs: no header mbuf");
1445
1446 #if FE_DEBUG >= 2
1447 /* First, count up the total number of bytes to copy. */
1448 for (totlen = 0, mp = m; mp != 0; mp = mp->m_next)
1449 totlen += mp->m_len;
1450 /* Check if this matches the one in the packet header. */
1451 if (totlen != m->m_pkthdr.len)
1452 log(LOG_WARNING, "%s: packet length mismatch? (%d/%d)\n",
1453 sc->sc_dev.dv_xname, totlen, m->m_pkthdr.len);
1454 #else
1455 /* Just use the length value in the packet header. */
1456 totlen = m->m_pkthdr.len;
1457 #endif
1458
1459 #if FE_DEBUG >= 1
1460 /*
1461 * Should never send big packets. If such a packet is passed,
1462 * it should be a bug of upper layer. We just ignore it.
1463 * ... Partial (too short) packets, neither.
1464 */
1465 if (totlen > ETHER_MAX_LEN || totlen < ETHER_HDR_SIZE) {
1466 log(LOG_ERR, "%s: got a %s packet (%u bytes) to send\n",
1467 sc->sc_dev.dv_xname,
1468 totlen < ETHER_HDR_SIZE ? "partial" : "big", totlen);
1469 sc->sc_ec.ec_if.if_oerrors++;
1470 return;
1471 }
1472 #endif
1473
1474 /*
1475 * Put the length word for this frame.
1476 * Does 86960 accept odd length? -- Yes.
1477 * Do we need to pad the length to minimum size by ourselves?
1478 * -- Generally yes. But for (or will be) the last
1479 * packet in the transmission buffer, we can skip the
1480 * padding process. It may gain performance slightly. FIXME.
1481 */
1482 bus_space_write_2(bst, bsh, FE_BMPR8, max(totlen, ETHER_MIN_LEN));
1483
1484 /*
1485 * Update buffer status now.
1486 * Truncate the length up to an even number, since we use outw().
1487 */
1488 totlen = (totlen + 1) & ~1;
1489 sc->txb_free -= FE_DATA_LEN_LEN + max(totlen, ETHER_MIN_LEN);
1490 sc->txb_count++;
1491
1492 #if FE_DELAYED_PADDING
1493 /* Postpone the packet padding if necessary. */
1494 if (totlen < ETHER_MIN_LEN)
1495 sc->txb_padding = ETHER_MIN_LEN - totlen;
1496 #endif
1497
1498 /*
1499 * Transfer the data from mbuf chain to the transmission buffer.
1500 * MB86960 seems to require that data be transferred as words, and
1501 * only words. So that we require some extra code to patch
1502 * over odd-length mbufs.
1503 */
1504 wantbyte = 0;
1505 for (; m != 0; m = m->m_next) {
1506 /* Ignore empty mbuf. */
1507 len = m->m_len;
1508 if (len == 0)
1509 continue;
1510
1511 /* Find the actual data to send. */
1512 data = mtod(m, caddr_t);
1513
1514 /* Finish the last byte. */
1515 if (wantbyte) {
1516 bus_space_write_2(bst, bsh, FE_BMPR8,
1517 savebyte | (*data << 8));
1518 data++;
1519 len--;
1520 wantbyte = 0;
1521 }
1522
1523 /* Output contiguous words. */
1524 if (len > 1)
1525 bus_space_write_multi_2(bst, bsh, FE_BMPR8, data,
1526 len >> 1);
1527
1528 /* Save remaining byte, if there is one. */
1529 if (len & 1) {
1530 data += len & ~1;
1531 savebyte = *data;
1532 wantbyte = 1;
1533 }
1534 }
1535
1536 /* Spit the last byte, if the length is odd. */
1537 if (wantbyte)
1538 bus_space_write_2(bst, bsh, FE_BMPR8, savebyte);
1539
1540 #if ! FE_DELAYED_PADDING
1541 /*
1542 * Pad the packet to the minimum length if necessary.
1543 */
1544 len = (ETHER_MIN_LEN >> 1) - (totlen >> 1);
1545 while (--len >= 0)
1546 bus_space_write_2(bst, bsh, FE_BMPR8, 0);
1547 #endif
1548 }
1549
1550 /*
1551 * Compute the multicast address filter from the
1552 * list of multicast addresses we need to listen to.
1553 */
1554 void
1555 mb86960_getmcaf(ec, af)
1556 struct ethercom *ec;
1557 u_char *af;
1558 {
1559 struct ifnet *ifp = &ec->ec_if;
1560 struct ether_multi *enm;
1561 register u_char *cp;
1562 register u_int32_t crc;
1563 static const u_int32_t crctab[] = {
1564 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1565 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1566 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1567 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1568 };
1569 register int len;
1570 struct ether_multistep step;
1571
1572 /*
1573 * Set up multicast address filter by passing all multicast addresses
1574 * through a crc generator, and then using the high order 6 bits as an
1575 * index into the 64 bit logical address filter. The high order bit
1576 * selects the word, while the rest of the bits select the bit within
1577 * the word.
1578 */
1579
1580 if ((ifp->if_flags & IFF_PROMISC) != 0)
1581 goto allmulti;
1582
1583 af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0x00;
1584 ETHER_FIRST_MULTI(step, ec, enm);
1585 while (enm != NULL) {
1586 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1587 sizeof(enm->enm_addrlo)) != 0) {
1588 /*
1589 * We must listen to a range of multicast addresses.
1590 * For now, just accept all multicasts, rather than
1591 * trying to set only those filter bits needed to match
1592 * the range. (At this time, the only use of address
1593 * ranges is for IP multicast routing, for which the
1594 * range is big enough to require all bits set.)
1595 */
1596 goto allmulti;
1597 }
1598
1599 cp = enm->enm_addrlo;
1600 crc = 0xffffffff;
1601 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1602 crc ^= *cp++;
1603 crc = (crc >> 4) ^ crctab[crc & 0xf];
1604 crc = (crc >> 4) ^ crctab[crc & 0xf];
1605 }
1606 /* Just want the 6 most significant bits. */
1607 crc >>= 26;
1608
1609 /* Turn on the corresponding bit in the filter. */
1610 af[crc >> 3] |= 1 << (crc & 7);
1611
1612 ETHER_NEXT_MULTI(step, enm);
1613 }
1614 ifp->if_flags &= ~IFF_ALLMULTI;
1615 return;
1616
1617 allmulti:
1618 ifp->if_flags |= IFF_ALLMULTI;
1619 af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0xff;
1620 }
1621
1622 /*
1623 * Calculate a new "multicast packet filter" and put the 86960
1624 * receiver in appropriate mode.
1625 */
1626 void
1627 mb86960_setmode(sc)
1628 struct mb86960_softc *sc;
1629 {
1630 bus_space_tag_t bst = sc->sc_bst;
1631 bus_space_handle_t bsh = sc->sc_bsh;
1632 int flags = sc->sc_ec.ec_if.if_flags;
1633
1634 /*
1635 * If the interface is not running, we postpone the update
1636 * process for receive modes and multicast address filter
1637 * until the interface is restarted. It reduces some
1638 * complicated job on maintaining chip states. (Earlier versions
1639 * of this driver had a bug on that point...)
1640 *
1641 * To complete the trick, mb86960_init() calls mb86960_setmode() after
1642 * restarting the interface.
1643 */
1644 if ((flags & IFF_RUNNING) == 0)
1645 return;
1646
1647 /*
1648 * Promiscuous mode is handled separately.
1649 */
1650 if ((flags & IFF_PROMISC) != 0) {
1651 /*
1652 * Program 86960 to receive all packets on the segment
1653 * including those directed to other stations.
1654 * Multicast filter stored in MARs are ignored
1655 * under this setting, so we don't need to update it.
1656 *
1657 * Promiscuous mode is used solely by BPF, and BPF only
1658 * listens to valid (no error) packets. So, we ignore
1659 * errornous ones even in this mode.
1660 */
1661 bus_space_write_1(bst, bsh, FE_DLCR5,
1662 sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1);
1663 sc->filter_change = 0;
1664
1665 #if FE_DEBUG >= 3
1666 log(LOG_INFO, "%s: promiscuous mode\n", sc->sc_dev.dv_xname);
1667 #endif
1668 return;
1669 }
1670
1671 /*
1672 * Turn the chip to the normal (non-promiscuous) mode.
1673 */
1674 bus_space_write_1(bst, bsh, FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1);
1675
1676 /*
1677 * Find the new multicast filter value.
1678 */
1679 mb86960_getmcaf(&sc->sc_ec, sc->filter);
1680 sc->filter_change = 1;
1681
1682 #if FE_DEBUG >= 3
1683 log(LOG_INFO,
1684 "%s: address filter: [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
1685 sc->sc_dev.dv_xname,
1686 sc->filter[0], sc->filter[1], sc->filter[2], sc->filter[3],
1687 sc->filter[4], sc->filter[5], sc->filter[6], sc->filter[7]);
1688 #endif
1689
1690 /*
1691 * We have to update the multicast filter in the 86960, A.S.A.P.
1692 *
1693 * Note that the DLC (Data Linc Control unit, i.e. transmitter
1694 * and receiver) must be stopped when feeding the filter, and
1695 * DLC trushes all packets in both transmission and receive
1696 * buffers when stopped.
1697 *
1698 * ... Are the above sentenses correct? I have to check the
1699 * manual of the MB86960A. FIXME.
1700 *
1701 * To reduce the packet lossage, we delay the filter update
1702 * process until buffers are empty.
1703 */
1704 if (sc->txb_sched == 0 && sc->txb_count == 0 &&
1705 (bus_space_read_1(bst, bsh, FE_DLCR1) & FE_D1_PKTRDY) == 0) {
1706 /*
1707 * Buffers are (apparently) empty. Load
1708 * the new filter value into MARs now.
1709 */
1710 mb86960_loadmar(sc);
1711 } else {
1712 /*
1713 * Buffers are not empty. Mark that we have to update
1714 * the MARs. The new filter will be loaded by mb86960_intr()
1715 * later.
1716 */
1717 #if FE_DEBUG >= 4
1718 log(LOG_INFO, "%s: filter change delayed\n",
1719 sc->sc_dev.dv_xname);
1720 #endif
1721 }
1722 }
1723
1724 /*
1725 * Load a new multicast address filter into MARs.
1726 *
1727 * The caller must have splnet'ed befor mb86960_loadmar.
1728 * This function starts the DLC upon return. So it can be called only
1729 * when the chip is working, i.e., from the driver's point of view, when
1730 * a device is RUNNING. (I mistook the point in previous versions.)
1731 */
1732 void
1733 mb86960_loadmar(sc)
1734 struct mb86960_softc *sc;
1735 {
1736 bus_space_tag_t bst = sc->sc_bst;
1737 bus_space_handle_t bsh = sc->sc_bsh;
1738
1739 /* Stop the DLC (transmitter and receiver). */
1740 bus_space_write_1(bst, bsh, FE_DLCR6,
1741 sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
1742
1743 /* Select register bank 1 for MARs. */
1744 bus_space_write_1(bst, bsh, FE_DLCR7,
1745 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
1746
1747 /* Copy filter value into the registers. */
1748 bus_space_write_region_1(bst, bsh, FE_MAR8, sc->filter, FE_FILTER_LEN);
1749
1750 /* Restore the bank selection for BMPRs (i.e., runtime registers). */
1751 bus_space_write_1(bst, bsh, FE_DLCR7,
1752 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
1753
1754 /* Restart the DLC. */
1755 bus_space_write_1(bst, bsh, FE_DLCR6,
1756 sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
1757
1758 /* We have just updated the filter. */
1759 sc->filter_change = 0;
1760
1761 #if FE_DEBUG >= 3
1762 log(LOG_INFO, "%s: address filter changed\n", sc->sc_dev.dv_xname);
1763 #endif
1764 }
1765
1766 /*
1767 * Enable power on the interface.
1768 */
1769 int
1770 mb86960_enable(sc)
1771 struct mb86960_softc *sc;
1772 {
1773
1774 #if FE_DEBUG >= 3
1775 log(LOG_INFO, "%s: mb86960_enable()\n", sc->sc_dev.dv_xname);
1776 #endif
1777
1778 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1779 if ((*sc->sc_enable)(sc) != 0) {
1780 printf("%s: device enable failed\n",
1781 sc->sc_dev.dv_xname);
1782 return (EIO);
1783 }
1784 }
1785
1786 sc->sc_enabled = 1;
1787 return (0);
1788 }
1789
1790 /*
1791 * Disable power on the interface.
1792 */
1793 void
1794 mb86960_disable(sc)
1795 struct mb86960_softc *sc;
1796 {
1797
1798 #if FE_DEBUG >= 3
1799 log(LOG_INFO, "%s: mb86960_disable()\n", sc->sc_dev.dv_xname);
1800 #endif
1801
1802 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1803 (*sc->sc_disable)(sc);
1804 sc->sc_enabled = 0;
1805 }
1806 }
1807
1808 #if FE_DEBUG >= 1
1809 void
1810 mb86960_dump(level, sc)
1811 int level;
1812 struct mb86960_softc *sc;
1813 {
1814 bus_space_tag_t bst = sc->sc_bst;
1815 bus_space_handle_t bsh = sc->sc_bsh;
1816 u_char save_dlcr7;
1817
1818 save_dlcr7 = bus_space_read_1(bst, bsh, FE_DLCR7);
1819
1820 log(level, "\tDLCR = %02x %02x %02x %02x %02x %02x %02x %02x\n",
1821 bus_space_read_1(bst, bsh, FE_DLCR0),
1822 bus_space_read_1(bst, bsh, FE_DLCR1),
1823 bus_space_read_1(bst, bsh, FE_DLCR2),
1824 bus_space_read_1(bst, bsh, FE_DLCR3),
1825 bus_space_read_1(bst, bsh, FE_DLCR4),
1826 bus_space_read_1(bst, bsh, FE_DLCR5),
1827 bus_space_read_1(bst, bsh, FE_DLCR6),
1828 bus_space_read_1(bst, bsh, FE_DLCR7));
1829
1830 bus_space_write_1(bst, bsh, FE_DLCR7,
1831 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_DLCR);
1832 log(level, "\t %02x %02x %02x %02x %02x %02x %02x %02x\n",
1833 bus_space_read_1(bst, bsh, FE_DLCR8),
1834 bus_space_read_1(bst, bsh, FE_DLCR9),
1835 bus_space_read_1(bst, bsh, FE_DLCR10),
1836 bus_space_read_1(bst, bsh, FE_DLCR11),
1837 bus_space_read_1(bst, bsh, FE_DLCR12),
1838 bus_space_read_1(bst, bsh, FE_DLCR13),
1839 bus_space_read_1(bst, bsh, FE_DLCR14),
1840 bus_space_read_1(bst, bsh, FE_DLCR15));
1841
1842 bus_space_write_1(bst, bsh, FE_DLCR7,
1843 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_MAR);
1844 log(level, "\tMAR = %02x %02x %02x %02x %02x %02x %02x %02x\n",
1845 bus_space_read_1(bst, bsh, FE_MAR8),
1846 bus_space_read_1(bst, bsh, FE_MAR9),
1847 bus_space_read_1(bst, bsh, FE_MAR10),
1848 bus_space_read_1(bst, bsh, FE_MAR11),
1849 bus_space_read_1(bst, bsh, FE_MAR12),
1850 bus_space_read_1(bst, bsh, FE_MAR13),
1851 bus_space_read_1(bst, bsh, FE_MAR14),
1852 bus_space_read_1(bst, bsh, FE_MAR15));
1853
1854 bus_space_write_1(bst, bsh, FE_DLCR7,
1855 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_BMPR);
1856 log(level,
1857 "\tBMPR = xx xx %02x %02x %02x %02x %02x %02x %02x %02x xx %02x\n",
1858 bus_space_read_1(bst, bsh, FE_BMPR10),
1859 bus_space_read_1(bst, bsh, FE_BMPR11),
1860 bus_space_read_1(bst, bsh, FE_BMPR12),
1861 bus_space_read_1(bst, bsh, FE_BMPR13),
1862 bus_space_read_1(bst, bsh, FE_BMPR14),
1863 bus_space_read_1(bst, bsh, FE_BMPR15),
1864 bus_space_read_1(bst, bsh, FE_BMPR16),
1865 bus_space_read_1(bst, bsh, FE_BMPR17),
1866 bus_space_read_1(bst, bsh, FE_BMPR19));
1867
1868 bus_space_write_1(bst, bsh, FE_DLCR7, save_dlcr7);
1869 }
1870 #endif
1871