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