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