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