mb86960.c revision 1.35 1 /* $NetBSD: mb86960.c,v 1.35 1999/09/13 10:31:35 itojun 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 return (0);
1111
1112 #if FE_DEBUG >= 4
1113 log(LOG_INFO, "%s: mb86960_intr()\n", sc->sc_dev.dv_xname);
1114 mb86960_dump(LOG_INFO, sc);
1115 #endif
1116
1117 /*
1118 * Get interrupt conditions, masking unneeded flags.
1119 */
1120 tstat = bus_space_read_1(bst, bsh, FE_DLCR0) & FE_TMASK;
1121 rstat = bus_space_read_1(bst, bsh, FE_DLCR1) & FE_RMASK;
1122 if (tstat == 0 && rstat == 0)
1123 return (0);
1124
1125 /*
1126 * Loop until there are no more new interrupt conditions.
1127 */
1128 for (;;) {
1129 /*
1130 * Reset the conditions we are acknowledging.
1131 */
1132 bus_space_write_1(bst, bsh, FE_DLCR0, tstat);
1133 bus_space_write_1(bst, bsh, FE_DLCR1, rstat);
1134
1135 /*
1136 * Handle transmitter interrupts. Handle these first because
1137 * the receiver will reset the board under some conditions.
1138 */
1139 if (tstat != 0)
1140 mb86960_tint(sc, tstat);
1141
1142 /*
1143 * Handle receiver interrupts.
1144 */
1145 if (rstat != 0)
1146 mb86960_rint(sc, rstat);
1147
1148 /*
1149 * Update the multicast address filter if it is
1150 * needed and possible. We do it now, because
1151 * we can make sure the transmission buffer is empty,
1152 * and there is a good chance that the receive queue
1153 * is empty. It will minimize the possibility of
1154 * packet lossage.
1155 */
1156 if (sc->filter_change &&
1157 sc->txb_count == 0 && sc->txb_sched == 0) {
1158 mb86960_loadmar(sc);
1159 ifp->if_flags &= ~IFF_OACTIVE;
1160 }
1161
1162 /*
1163 * If it looks like the transmitter can take more data,
1164 * attempt to start output on the interface. This is done
1165 * after handling the receiver interrupt to give the
1166 * receive operation priority.
1167 */
1168 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1169 mb86960_start(ifp);
1170
1171 #if NRND > 0
1172 if (rstat != 0 || tstat != 0)
1173 rnd_add_uint32(&sc->rnd_source, rstat + tstat);
1174 #endif
1175
1176 /*
1177 * Get interrupt conditions, masking unneeded flags.
1178 */
1179 tstat = bus_space_read_1(bst, bsh, FE_DLCR0) & FE_TMASK;
1180 rstat = bus_space_read_1(bst, bsh, FE_DLCR1) & FE_RMASK;
1181 if (tstat == 0 && rstat == 0)
1182 return (1);
1183 }
1184 }
1185
1186 /*
1187 * Process an ioctl request. This code needs some work - it looks pretty ugly.
1188 */
1189 int
1190 mb86960_ioctl(ifp, cmd, data)
1191 struct ifnet *ifp;
1192 u_long cmd;
1193 caddr_t data;
1194 {
1195 struct mb86960_softc *sc = ifp->if_softc;
1196 struct ifaddr *ifa = (struct ifaddr *)data;
1197 struct ifreq *ifr = (struct ifreq *)data;
1198 int s, error = 0;
1199
1200 #if FE_DEBUG >= 3
1201 log(LOG_INFO, "%s: ioctl(%lx)\n", sc->sc_dev.dv_xname, cmd);
1202 #endif
1203
1204 s = splnet();
1205
1206 switch (cmd) {
1207 case SIOCSIFADDR:
1208 if ((error = mb86960_enable(sc)) != 0)
1209 break;
1210 ifp->if_flags |= IFF_UP;
1211
1212 switch (ifa->ifa_addr->sa_family) {
1213 #ifdef INET
1214 case AF_INET:
1215 mb86960_init(sc);
1216 arp_ifinit(ifp, ifa);
1217 break;
1218 #endif
1219 #ifdef NS
1220 case AF_NS:
1221 {
1222 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1223
1224 if (ns_nullhost(*ina))
1225 ina->x_host =
1226 *(union ns_host *)LLADDR(ifp->if_sadl);
1227 else {
1228 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1229 ETHER_ADDR_LEN);
1230 }
1231 /* Set new address. */
1232 mb86960_init(sc);
1233 break;
1234 }
1235 #endif
1236 default:
1237 mb86960_init(sc);
1238 break;
1239 }
1240 break;
1241
1242 case SIOCSIFFLAGS:
1243 if ((ifp->if_flags & IFF_UP) == 0 &&
1244 (ifp->if_flags & IFF_RUNNING) != 0) {
1245 /*
1246 * If interface is marked down and it is running, then
1247 * stop it.
1248 */
1249 mb86960_stop(sc);
1250 ifp->if_flags &= ~IFF_RUNNING;
1251 mb86960_disable(sc);
1252 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1253 (ifp->if_flags & IFF_RUNNING) == 0) {
1254 /*
1255 * If interface is marked up and it is stopped, then
1256 * start it.
1257 */
1258 if ((error = mb86960_enable(sc)) != 0)
1259 break;
1260 mb86960_init(sc);
1261 } else if ((ifp->if_flags & IFF_UP) != 0) {
1262 /*
1263 * Reset the interface to pick up changes in any other
1264 * flags that affect hardware registers.
1265 */
1266 mb86960_setmode(sc);
1267 }
1268 #if DEBUG >= 1
1269 /* "ifconfig fe0 debug" to print register dump. */
1270 if (ifp->if_flags & IFF_DEBUG) {
1271 log(LOG_INFO, "%s: SIOCSIFFLAGS(DEBUG)\n",
1272 sc->sc_dev.dv_xname);
1273 mb86960_dump(LOG_DEBUG, sc);
1274 }
1275 #endif
1276 break;
1277
1278 case SIOCADDMULTI:
1279 case SIOCDELMULTI:
1280 if (sc->sc_enabled == 0) {
1281 error = EIO;
1282 break;
1283 }
1284
1285 /* Update our multicast list. */
1286 error = (cmd == SIOCADDMULTI) ?
1287 ether_addmulti(ifr, &sc->sc_ec) :
1288 ether_delmulti(ifr, &sc->sc_ec);
1289
1290 if (error == ENETRESET) {
1291 /*
1292 * Multicast list has changed; set the hardware filter
1293 * accordingly.
1294 */
1295 mb86960_setmode(sc);
1296 error = 0;
1297 }
1298 break;
1299
1300 case SIOCGIFMEDIA:
1301 case SIOCSIFMEDIA:
1302 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1303 break;
1304
1305 default:
1306 error = EINVAL;
1307 break;
1308 }
1309
1310 splx(s);
1311 return (error);
1312 }
1313
1314 /*
1315 * Retreive packet from receive buffer and send to the next level up via
1316 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1317 * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
1318 */
1319 int
1320 mb86960_get_packet(sc, len)
1321 struct mb86960_softc *sc;
1322 int len;
1323 {
1324 bus_space_tag_t bst = sc->sc_bst;
1325 bus_space_handle_t bsh = sc->sc_bsh;
1326 struct ifnet *ifp = &sc->sc_ec.ec_if;
1327 struct ether_header *eh;
1328 struct mbuf *m;
1329
1330 /* Allocate a header mbuf. */
1331 MGETHDR(m, M_DONTWAIT, MT_DATA);
1332 if (m == 0)
1333 return (0);
1334 m->m_pkthdr.rcvif = ifp;
1335 m->m_pkthdr.len = len;
1336
1337 /* The following silliness is to make NFS happy. */
1338 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1339 #define EOFF (EROUND - sizeof(struct ether_header))
1340
1341 /*
1342 * Our strategy has one more problem. There is a policy on
1343 * mbuf cluster allocation. It says that we must have at
1344 * least MINCLSIZE (208 bytes) to allocate a cluster. For a
1345 * packet of a size between (MHLEN - 2) to (MINCLSIZE - 2),
1346 * our code violates the rule...
1347 * On the other hand, the current code is short, simle,
1348 * and fast, however. It does no harmful thing, just waists
1349 * some memory. Any comments? FIXME.
1350 */
1351
1352 /* Attach a cluster if this packet doesn't fit in a normal mbuf. */
1353 if (len > MHLEN - EOFF) {
1354 MCLGET(m, M_DONTWAIT);
1355 if ((m->m_flags & M_EXT) == 0) {
1356 m_freem(m);
1357 return (0);
1358 }
1359 }
1360
1361 /*
1362 * The following assumes there is room for the ether header in the
1363 * header mbuf.
1364 */
1365 m->m_data += EOFF;
1366 eh = mtod(m, struct ether_header *);
1367
1368 /* Set the length of this packet. */
1369 m->m_len = len;
1370
1371 /* Get a packet. */
1372 bus_space_read_multi_stream_2(bst, bsh, FE_BMPR8, mtod(m, u_int16_t *),
1373 (len + 1) >> 1);
1374
1375 #if NBPFILTER > 0
1376 /*
1377 * Check if there's a BPF listener on this interface. If so, hand off
1378 * the raw packet to bpf.
1379 */
1380 if (ifp->if_bpf) {
1381 bpf_mtap(ifp->if_bpf, m);
1382
1383 /*
1384 * Note that the interface cannot be in promiscuous mode if
1385 * there are no BPF listeners. And if we are in promiscuous
1386 * mode, we have to check if this packet is really ours.
1387 */
1388 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1389 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1390 bcmp(eh->ether_dhost, sc->sc_enaddr,
1391 sizeof(eh->ether_dhost)) != 0) {
1392 m_freem(m);
1393 return (1);
1394 }
1395 }
1396 #endif
1397
1398 (*ifp->if_input)(ifp, m);
1399 return (1);
1400 }
1401
1402 /*
1403 * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
1404 * Returns number of bytes actually written, including length word.
1405 *
1406 * If an mbuf chain is too long for an Ethernet frame, it is not sent.
1407 * Packets shorter than Ethernet minimum are legal, and we pad them
1408 * before sending out. An exception is "partial" packets which are
1409 * shorter than mandatory Ethernet header.
1410 *
1411 * I wrote a code for an experimental "delayed padding" technique.
1412 * When employed, it postpones the padding process for short packets.
1413 * If xmit() occured at the moment, the padding process is omitted, and
1414 * garbages are sent as pad data. If next packet is stored in the
1415 * transmission buffer before xmit(), write_mbuf() pads the previous
1416 * packet before transmitting new packet. This *may* gain the
1417 * system performance (slightly).
1418 */
1419 void
1420 mb86960_write_mbufs(sc, m)
1421 struct mb86960_softc *sc;
1422 struct mbuf *m;
1423 {
1424 bus_space_tag_t bst = sc->sc_bst;
1425 bus_space_handle_t bsh = sc->sc_bsh;
1426 u_char *data;
1427 u_short savebyte; /* WARNING: Architecture dependent! */
1428 int totlen, len, wantbyte;
1429 #if FE_DEBUG >= 2
1430 struct mbuf *mp;
1431 #endif
1432
1433 /* XXX thorpej 960116 - quiet bogus compiler warning. */
1434 savebyte = 0;
1435
1436 #if FE_DELAYED_PADDING
1437 /* Do the "delayed padding." */
1438 len = sc->txb_padding >> 1;
1439 if (len > 0) {
1440 while (--len >= 0)
1441 bus_space_write_2(bst, bsh, FE_BMPR8, 0);
1442 sc->txb_padding = 0;
1443 }
1444 #endif
1445
1446 /* We need to use m->m_pkthdr.len, so require the header */
1447 if ((m->m_flags & M_PKTHDR) == 0)
1448 panic("mb86960_write_mbufs: no header mbuf");
1449
1450 #if FE_DEBUG >= 2
1451 /* First, count up the total number of bytes to copy. */
1452 for (totlen = 0, mp = m; mp != 0; mp = mp->m_next)
1453 totlen += mp->m_len;
1454 /* Check if this matches the one in the packet header. */
1455 if (totlen != m->m_pkthdr.len)
1456 log(LOG_WARNING, "%s: packet length mismatch? (%d/%d)\n",
1457 sc->sc_dev.dv_xname, totlen, m->m_pkthdr.len);
1458 #else
1459 /* Just use the length value in the packet header. */
1460 totlen = m->m_pkthdr.len;
1461 #endif
1462
1463 #if FE_DEBUG >= 1
1464 /*
1465 * Should never send big packets. If such a packet is passed,
1466 * it should be a bug of upper layer. We just ignore it.
1467 * ... Partial (too short) packets, neither.
1468 */
1469 if (totlen > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
1470 totlen < ETHER_HDR_LEN) {
1471 log(LOG_ERR, "%s: got a %s packet (%u bytes) to send\n",
1472 sc->sc_dev.dv_xname,
1473 totlen < ETHER_HDR_LEN ? "partial" : "big", totlen);
1474 sc->sc_ec.ec_if.if_oerrors++;
1475 return;
1476 }
1477 #endif
1478
1479 /*
1480 * Put the length word for this frame.
1481 * Does 86960 accept odd length? -- Yes.
1482 * Do we need to pad the length to minimum size by ourselves?
1483 * -- Generally yes. But for (or will be) the last
1484 * packet in the transmission buffer, we can skip the
1485 * padding process. It may gain performance slightly. FIXME.
1486 */
1487 bus_space_write_2(bst, bsh, FE_BMPR8,
1488 max(totlen, (ETHER_MIN_LEN - ETHER_CRC_LEN)));
1489
1490 /*
1491 * Update buffer status now.
1492 * Truncate the length up to an even number, since we use outw().
1493 */
1494 totlen = (totlen + 1) & ~1;
1495 sc->txb_free -= FE_DATA_LEN_LEN +
1496 max(totlen, (ETHER_MIN_LEN - ETHER_CRC_LEN));
1497 sc->txb_count++;
1498
1499 #if FE_DELAYED_PADDING
1500 /* Postpone the packet padding if necessary. */
1501 if (totlen < (ETHER_MIN_LEN - ETHER_CRC_LEN))
1502 sc->txb_padding = (ETHER_MIN_LEN - ETHER_CRC_LEN) - totlen;
1503 #endif
1504
1505 /*
1506 * Transfer the data from mbuf chain to the transmission buffer.
1507 * MB86960 seems to require that data be transferred as words, and
1508 * only words. So that we require some extra code to patch
1509 * over odd-length mbufs.
1510 */
1511 wantbyte = 0;
1512 for (; m != 0; m = m->m_next) {
1513 /* Ignore empty mbuf. */
1514 len = m->m_len;
1515 if (len == 0)
1516 continue;
1517
1518 /* Find the actual data to send. */
1519 data = mtod(m, caddr_t);
1520
1521 /* Finish the last byte. */
1522 if (wantbyte) {
1523 bus_space_write_2(bst, bsh, FE_BMPR8,
1524 savebyte | (*data << 8));
1525 data++;
1526 len--;
1527 wantbyte = 0;
1528 }
1529
1530 /* Output contiguous words. */
1531 if (len > 1)
1532 bus_space_write_multi_stream_2(bst, bsh, FE_BMPR8,
1533 (u_int16_t *)data, len >> 1);
1534
1535 /* Save remaining byte, if there is one. */
1536 if (len & 1) {
1537 data += len & ~1;
1538 savebyte = *data;
1539 wantbyte = 1;
1540 }
1541 }
1542
1543 /* Spit the last byte, if the length is odd. */
1544 if (wantbyte)
1545 bus_space_write_2(bst, bsh, FE_BMPR8, savebyte);
1546
1547 #if ! FE_DELAYED_PADDING
1548 /*
1549 * Pad the packet to the minimum length if necessary.
1550 */
1551 len = ((ETHER_MIN_LEN - ETHER_CRC_LEN) >> 1) - (totlen >> 1);
1552 while (--len >= 0)
1553 bus_space_write_2(bst, bsh, FE_BMPR8, 0);
1554 #endif
1555 }
1556
1557 /*
1558 * Compute the multicast address filter from the
1559 * list of multicast addresses we need to listen to.
1560 */
1561 void
1562 mb86960_getmcaf(ec, af)
1563 struct ethercom *ec;
1564 u_char *af;
1565 {
1566 struct ifnet *ifp = &ec->ec_if;
1567 struct ether_multi *enm;
1568 register u_char *cp;
1569 register u_int32_t crc;
1570 static const u_int32_t crctab[] = {
1571 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1572 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1573 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1574 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1575 };
1576 register int len;
1577 struct ether_multistep step;
1578
1579 /*
1580 * Set up multicast address filter by passing all multicast addresses
1581 * through a crc generator, and then using the high order 6 bits as an
1582 * index into the 64 bit logical address filter. The high order bit
1583 * selects the word, while the rest of the bits select the bit within
1584 * the word.
1585 */
1586
1587 if ((ifp->if_flags & IFF_PROMISC) != 0)
1588 goto allmulti;
1589
1590 af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0x00;
1591 ETHER_FIRST_MULTI(step, ec, enm);
1592 while (enm != NULL) {
1593 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1594 sizeof(enm->enm_addrlo)) != 0) {
1595 /*
1596 * We must listen to a range of multicast addresses.
1597 * For now, just accept all multicasts, rather than
1598 * trying to set only those filter bits needed to match
1599 * the range. (At this time, the only use of address
1600 * ranges is for IP multicast routing, for which the
1601 * range is big enough to require all bits set.)
1602 */
1603 goto allmulti;
1604 }
1605
1606 cp = enm->enm_addrlo;
1607 crc = 0xffffffff;
1608 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1609 crc ^= *cp++;
1610 crc = (crc >> 4) ^ crctab[crc & 0xf];
1611 crc = (crc >> 4) ^ crctab[crc & 0xf];
1612 }
1613 /* Just want the 6 most significant bits. */
1614 crc >>= 26;
1615
1616 /* Turn on the corresponding bit in the filter. */
1617 af[crc >> 3] |= 1 << (crc & 7);
1618
1619 ETHER_NEXT_MULTI(step, enm);
1620 }
1621 ifp->if_flags &= ~IFF_ALLMULTI;
1622 return;
1623
1624 allmulti:
1625 ifp->if_flags |= IFF_ALLMULTI;
1626 af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0xff;
1627 }
1628
1629 /*
1630 * Calculate a new "multicast packet filter" and put the 86960
1631 * receiver in appropriate mode.
1632 */
1633 void
1634 mb86960_setmode(sc)
1635 struct mb86960_softc *sc;
1636 {
1637 bus_space_tag_t bst = sc->sc_bst;
1638 bus_space_handle_t bsh = sc->sc_bsh;
1639 int flags = sc->sc_ec.ec_if.if_flags;
1640
1641 /*
1642 * If the interface is not running, we postpone the update
1643 * process for receive modes and multicast address filter
1644 * until the interface is restarted. It reduces some
1645 * complicated job on maintaining chip states. (Earlier versions
1646 * of this driver had a bug on that point...)
1647 *
1648 * To complete the trick, mb86960_init() calls mb86960_setmode() after
1649 * restarting the interface.
1650 */
1651 if ((flags & IFF_RUNNING) == 0)
1652 return;
1653
1654 /*
1655 * Promiscuous mode is handled separately.
1656 */
1657 if ((flags & IFF_PROMISC) != 0) {
1658 /*
1659 * Program 86960 to receive all packets on the segment
1660 * including those directed to other stations.
1661 * Multicast filter stored in MARs are ignored
1662 * under this setting, so we don't need to update it.
1663 *
1664 * Promiscuous mode is used solely by BPF, and BPF only
1665 * listens to valid (no error) packets. So, we ignore
1666 * errornous ones even in this mode.
1667 */
1668 bus_space_write_1(bst, bsh, FE_DLCR5,
1669 sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1);
1670 sc->filter_change = 0;
1671
1672 #if FE_DEBUG >= 3
1673 log(LOG_INFO, "%s: promiscuous mode\n", sc->sc_dev.dv_xname);
1674 #endif
1675 return;
1676 }
1677
1678 /*
1679 * Turn the chip to the normal (non-promiscuous) mode.
1680 */
1681 bus_space_write_1(bst, bsh, FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1);
1682
1683 /*
1684 * Find the new multicast filter value.
1685 */
1686 mb86960_getmcaf(&sc->sc_ec, sc->filter);
1687 sc->filter_change = 1;
1688
1689 #if FE_DEBUG >= 3
1690 log(LOG_INFO,
1691 "%s: address filter: [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
1692 sc->sc_dev.dv_xname,
1693 sc->filter[0], sc->filter[1], sc->filter[2], sc->filter[3],
1694 sc->filter[4], sc->filter[5], sc->filter[6], sc->filter[7]);
1695 #endif
1696
1697 /*
1698 * We have to update the multicast filter in the 86960, A.S.A.P.
1699 *
1700 * Note that the DLC (Data Linc Control unit, i.e. transmitter
1701 * and receiver) must be stopped when feeding the filter, and
1702 * DLC trushes all packets in both transmission and receive
1703 * buffers when stopped.
1704 *
1705 * ... Are the above sentenses correct? I have to check the
1706 * manual of the MB86960A. FIXME.
1707 *
1708 * To reduce the packet lossage, we delay the filter update
1709 * process until buffers are empty.
1710 */
1711 if (sc->txb_sched == 0 && sc->txb_count == 0 &&
1712 (bus_space_read_1(bst, bsh, FE_DLCR1) & FE_D1_PKTRDY) == 0) {
1713 /*
1714 * Buffers are (apparently) empty. Load
1715 * the new filter value into MARs now.
1716 */
1717 mb86960_loadmar(sc);
1718 } else {
1719 /*
1720 * Buffers are not empty. Mark that we have to update
1721 * the MARs. The new filter will be loaded by mb86960_intr()
1722 * later.
1723 */
1724 #if FE_DEBUG >= 4
1725 log(LOG_INFO, "%s: filter change delayed\n",
1726 sc->sc_dev.dv_xname);
1727 #endif
1728 }
1729 }
1730
1731 /*
1732 * Load a new multicast address filter into MARs.
1733 *
1734 * The caller must have splnet'ed befor mb86960_loadmar.
1735 * This function starts the DLC upon return. So it can be called only
1736 * when the chip is working, i.e., from the driver's point of view, when
1737 * a device is RUNNING. (I mistook the point in previous versions.)
1738 */
1739 void
1740 mb86960_loadmar(sc)
1741 struct mb86960_softc *sc;
1742 {
1743 bus_space_tag_t bst = sc->sc_bst;
1744 bus_space_handle_t bsh = sc->sc_bsh;
1745
1746 /* Stop the DLC (transmitter and receiver). */
1747 bus_space_write_1(bst, bsh, FE_DLCR6,
1748 sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
1749
1750 /* Select register bank 1 for MARs. */
1751 bus_space_write_1(bst, bsh, FE_DLCR7,
1752 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
1753
1754 /* Copy filter value into the registers. */
1755 bus_space_write_region_1(bst, bsh, FE_MAR8, sc->filter, FE_FILTER_LEN);
1756
1757 /* Restore the bank selection for BMPRs (i.e., runtime registers). */
1758 bus_space_write_1(bst, bsh, FE_DLCR7,
1759 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
1760
1761 /* Restart the DLC. */
1762 bus_space_write_1(bst, bsh, FE_DLCR6,
1763 sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
1764
1765 /* We have just updated the filter. */
1766 sc->filter_change = 0;
1767
1768 #if FE_DEBUG >= 3
1769 log(LOG_INFO, "%s: address filter changed\n", sc->sc_dev.dv_xname);
1770 #endif
1771 }
1772
1773 /*
1774 * Enable power on the interface.
1775 */
1776 int
1777 mb86960_enable(sc)
1778 struct mb86960_softc *sc;
1779 {
1780
1781 #if FE_DEBUG >= 3
1782 log(LOG_INFO, "%s: mb86960_enable()\n", sc->sc_dev.dv_xname);
1783 #endif
1784
1785 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1786 if ((*sc->sc_enable)(sc) != 0) {
1787 printf("%s: device enable failed\n",
1788 sc->sc_dev.dv_xname);
1789 return (EIO);
1790 }
1791 }
1792
1793 sc->sc_enabled = 1;
1794 return (0);
1795 }
1796
1797 /*
1798 * Disable power on the interface.
1799 */
1800 void
1801 mb86960_disable(sc)
1802 struct mb86960_softc *sc;
1803 {
1804
1805 #if FE_DEBUG >= 3
1806 log(LOG_INFO, "%s: mb86960_disable()\n", sc->sc_dev.dv_xname);
1807 #endif
1808
1809 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1810 (*sc->sc_disable)(sc);
1811 sc->sc_enabled = 0;
1812 }
1813 }
1814
1815 int
1816 mb86960_activate(self, act)
1817 struct device *self;
1818 enum devact act;
1819 {
1820 struct mb86960_softc *sc = (struct mb86960_softc *)self;
1821 int rv = 0, s;
1822
1823 s = splnet();
1824 switch (act) {
1825 case DVACT_ACTIVATE:
1826 rv = EOPNOTSUPP;
1827 break;
1828
1829 case DVACT_DEACTIVATE:
1830 #ifdef notyet
1831 /* First, kill off the interface. */
1832 if_detach(sc->sc_ec.ec_if);
1833 #endif
1834
1835 /* Now disable the interface. */
1836 mb86960_disable(sc);
1837 break;
1838 }
1839 splx(s);
1840 return (rv);
1841 }
1842
1843 #if FE_DEBUG >= 1
1844 void
1845 mb86960_dump(level, sc)
1846 int level;
1847 struct mb86960_softc *sc;
1848 {
1849 bus_space_tag_t bst = sc->sc_bst;
1850 bus_space_handle_t bsh = sc->sc_bsh;
1851 u_char save_dlcr7;
1852
1853 save_dlcr7 = bus_space_read_1(bst, bsh, FE_DLCR7);
1854
1855 log(level, "\tDLCR = %02x %02x %02x %02x %02x %02x %02x %02x\n",
1856 bus_space_read_1(bst, bsh, FE_DLCR0),
1857 bus_space_read_1(bst, bsh, FE_DLCR1),
1858 bus_space_read_1(bst, bsh, FE_DLCR2),
1859 bus_space_read_1(bst, bsh, FE_DLCR3),
1860 bus_space_read_1(bst, bsh, FE_DLCR4),
1861 bus_space_read_1(bst, bsh, FE_DLCR5),
1862 bus_space_read_1(bst, bsh, FE_DLCR6),
1863 bus_space_read_1(bst, bsh, FE_DLCR7));
1864
1865 bus_space_write_1(bst, bsh, FE_DLCR7,
1866 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_DLCR);
1867 log(level, "\t %02x %02x %02x %02x %02x %02x %02x %02x\n",
1868 bus_space_read_1(bst, bsh, FE_DLCR8),
1869 bus_space_read_1(bst, bsh, FE_DLCR9),
1870 bus_space_read_1(bst, bsh, FE_DLCR10),
1871 bus_space_read_1(bst, bsh, FE_DLCR11),
1872 bus_space_read_1(bst, bsh, FE_DLCR12),
1873 bus_space_read_1(bst, bsh, FE_DLCR13),
1874 bus_space_read_1(bst, bsh, FE_DLCR14),
1875 bus_space_read_1(bst, bsh, FE_DLCR15));
1876
1877 bus_space_write_1(bst, bsh, FE_DLCR7,
1878 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_MAR);
1879 log(level, "\tMAR = %02x %02x %02x %02x %02x %02x %02x %02x\n",
1880 bus_space_read_1(bst, bsh, FE_MAR8),
1881 bus_space_read_1(bst, bsh, FE_MAR9),
1882 bus_space_read_1(bst, bsh, FE_MAR10),
1883 bus_space_read_1(bst, bsh, FE_MAR11),
1884 bus_space_read_1(bst, bsh, FE_MAR12),
1885 bus_space_read_1(bst, bsh, FE_MAR13),
1886 bus_space_read_1(bst, bsh, FE_MAR14),
1887 bus_space_read_1(bst, bsh, FE_MAR15));
1888
1889 bus_space_write_1(bst, bsh, FE_DLCR7,
1890 (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_BMPR);
1891 log(level,
1892 "\tBMPR = xx xx %02x %02x %02x %02x %02x %02x %02x %02x xx %02x\n",
1893 bus_space_read_1(bst, bsh, FE_BMPR10),
1894 bus_space_read_1(bst, bsh, FE_BMPR11),
1895 bus_space_read_1(bst, bsh, FE_BMPR12),
1896 bus_space_read_1(bst, bsh, FE_BMPR13),
1897 bus_space_read_1(bst, bsh, FE_BMPR14),
1898 bus_space_read_1(bst, bsh, FE_BMPR15),
1899 bus_space_read_1(bst, bsh, FE_BMPR16),
1900 bus_space_read_1(bst, bsh, FE_BMPR17),
1901 bus_space_read_1(bst, bsh, FE_BMPR19));
1902
1903 bus_space_write_1(bst, bsh, FE_DLCR7, save_dlcr7);
1904 }
1905 #endif
1906