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