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