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