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