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