if_ec.c revision 1.12 1 /* $NetBSD: if_ec.c,v 1.12 2001/02/12 18:49:04 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
42 * adapters.
43 *
44 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
45 *
46 * Copyright (C) 1993, David Greenman. This software may be used, modified,
47 * copied, distributed, and sold, in both source and binary form provided that
48 * the above copyright and these terms are retained. Under no circumstances is
49 * the author responsible for the proper functioning of this software, nor does
50 * the author assume any responsibility for damages incurred with its use.
51 */
52
53 /*
54 * Device driver for the 3Com Etherlink II (3c503).
55 */
56
57 #include "opt_inet.h"
58 #include "opt_ns.h"
59 #include "bpfilter.h"
60 #include "rnd.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/device.h>
65 #include <sys/socket.h>
66 #include <sys/mbuf.h>
67 #include <sys/syslog.h>
68
69 #include <net/if.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72 #include <net/if_media.h>
73
74 #include <net/if_ether.h>
75
76 #ifdef INET
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/if_inarp.h>
82 #endif
83
84 #ifdef NS
85 #include <netns/ns.h>
86 #include <netns/ns_if.h>
87 #endif
88
89 #if NBPFILTER > 0
90 #include <net/bpf.h>
91 #include <net/bpfdesc.h>
92 #endif
93
94 #include <machine/bus.h>
95 #include <machine/intr.h>
96
97 #include <dev/isa/isareg.h>
98 #include <dev/isa/isavar.h>
99
100 #include <dev/ic/dp8390reg.h>
101 #include <dev/ic/dp8390var.h>
102
103 #include <dev/isa/if_ecreg.h>
104
105 struct ec_softc {
106 struct dp8390_softc sc_dp8390;
107
108 bus_space_tag_t sc_asict; /* space tag for ASIC */
109 bus_space_handle_t sc_asich; /* space handle for ASIC */
110
111 int sc_16bitp; /* are we 16 bit? */
112
113 void *sc_ih; /* interrupt handle */
114 };
115
116 int ec_probe __P((struct device *, struct cfdata *, void *));
117 void ec_attach __P((struct device *, struct device *, void *));
118
119 struct cfattach ec_ca = {
120 sizeof(struct ec_softc), ec_probe, ec_attach
121 };
122
123 int ec_set_media __P((struct ec_softc *, int));
124
125 void ec_media_init __P((struct dp8390_softc *));
126
127 int ec_mediachange __P((struct dp8390_softc *));
128 void ec_mediastatus __P((struct dp8390_softc *, struct ifmediareq *));
129
130 void ec_init_card __P((struct dp8390_softc *));
131 int ec_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
132 int ec_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
133 void ec_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
134 int ec_fake_test_mem __P((struct dp8390_softc *));
135 int ec_test_mem __P((struct dp8390_softc *));
136
137 __inline void ec_readmem __P((struct ec_softc *, int, u_int8_t *, int));
138
139 static const int ec_iobase[] = {
140 0x2e0, 0x2a0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300,
141 };
142 #define NEC_IOBASE (sizeof(ec_iobase) / sizeof(ec_iobase[0]))
143
144 static const int ec_membase[] = {
145 MADDRUNK, MADDRUNK, MADDRUNK, MADDRUNK, 0xc8000, 0xcc000,
146 0xd8000, 0xdc000,
147 };
148 #define NEC_MEMBASE (sizeof(ec_membase) / sizeof(ec_membase[0]))
149
150 int
151 ec_probe(parent, match, aux)
152 struct device *parent;
153 struct cfdata *match;
154 void *aux;
155 {
156 struct isa_attach_args *ia = aux;
157 bus_space_tag_t nict, asict, memt;
158 bus_space_handle_t nich, asich, memh;
159 bus_size_t memsize;
160 int nich_valid, asich_valid, memh_valid;
161 int i, rv = 0;
162 u_int8_t x;
163
164 nict = asict = ia->ia_iot;
165 memt = ia->ia_memt;
166
167 nich_valid = asich_valid = memh_valid = 0;
168
169 /*
170 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
171 * to it.
172 */
173 memsize = 8192;
174
175 /* Disallow wildcarded i/o addresses. */
176 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
177 return (0);
178
179 /* Disallow wildcarded mem address. */
180 if (ia->ia_maddr == ISACF_IOMEM_DEFAULT)
181 return (0);
182
183 /* Validate the i/o base. */
184 for (i = 0; i < NEC_IOBASE; i++)
185 if (ia->ia_iobase == ec_iobase[i])
186 break;
187 if (i == NEC_IOBASE)
188 return (0);
189
190 /* Validate the mem base. */
191 for (i = 0; i < NEC_MEMBASE; i++) {
192 if (ec_membase[i] == MADDRUNK)
193 continue;
194 if (ia->ia_maddr == ec_membase[i])
195 break;
196 }
197 if (i == NEC_MEMBASE)
198 return (0);
199
200 /* Attempt to map the NIC space. */
201 if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
202 ELINK2_NIC_PORTS, 0, &nich))
203 goto out;
204 nich_valid = 1;
205
206 /* Attempt to map the ASIC space. */
207 if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
208 ELINK2_ASIC_PORTS, 0, &asich))
209 goto out;
210 asich_valid = 1;
211
212 /* Attempt to map the memory space. */
213 if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh))
214 goto out;
215 memh_valid = 1;
216
217 /*
218 * Verify that the kernel configured I/O address matches the
219 * board configured I/O address.
220 *
221 * This is really only useful to see if something that looks like
222 * the board is there; after all, we're already talking to it at
223 * this point.
224 */
225 x = bus_space_read_1(asict, asich, ELINK2_BCFR);
226 if (x == 0 || (x & (x - 1)) != 0)
227 goto out;
228 i = ffs(x) - 1;
229 if (ia->ia_iobase != ec_iobase[i])
230 goto out;
231
232 /*
233 * ...and for the memory address. Note we do not support
234 * cards configured with shared memory disabled.
235 */
236 x = bus_space_read_1(asict, asich, ELINK2_PCFR);
237 if (x == 0 || (x & (x - 1)) != 0)
238 goto out;
239 i = ffs(x) - 1;
240 if (ia->ia_maddr != ec_membase[i])
241 goto out;
242
243 /* So, we say we've found it! */
244 ia->ia_iosize = ELINK2_NIC_PORTS;
245 ia->ia_msize = memsize;
246 rv = 1;
247
248 out:
249 if (nich_valid)
250 bus_space_unmap(nict, nich, ELINK2_NIC_PORTS);
251 if (asich_valid)
252 bus_space_unmap(asict, asich, ELINK2_ASIC_PORTS);
253 if (memh_valid)
254 bus_space_unmap(memt, memh, memsize);
255 return (rv);
256 }
257
258 void
259 ec_attach(parent, self, aux)
260 struct device *parent, *self;
261 void *aux;
262 {
263 struct ec_softc *esc = (struct ec_softc *)self;
264 struct dp8390_softc *sc = &esc->sc_dp8390;
265 struct isa_attach_args *ia = aux;
266 bus_space_tag_t nict, asict, memt;
267 bus_space_handle_t nich, asich, memh;
268 bus_size_t memsize;
269 u_int8_t tmp;
270 int i;
271
272 printf("\n");
273
274 nict = asict = ia->ia_iot;
275 memt = ia->ia_memt;
276
277 /*
278 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
279 * to it.
280 */
281 memsize = 8192;
282
283 /* Map the NIC space. */
284 if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
285 ELINK2_NIC_PORTS, 0, &nich)) {
286 printf("%s: can't map nic i/o space\n",
287 sc->sc_dev.dv_xname);
288 return;
289 }
290
291 /* Map the ASIC space. */
292 if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
293 ELINK2_ASIC_PORTS, 0, &asich)) {
294 printf("%s: can't map asic i/o space\n",
295 sc->sc_dev.dv_xname);
296 return;
297 }
298
299 /* Map the memory space. */
300 if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh)) {
301 printf("%s: can't map shared memory\n",
302 sc->sc_dev.dv_xname);
303 return;
304 }
305
306 esc->sc_asict = asict;
307 esc->sc_asich = asich;
308
309 sc->sc_regt = nict;
310 sc->sc_regh = nich;
311
312 sc->sc_buft = memt;
313 sc->sc_bufh = memh;
314
315 /* Interface is always enabled. */
316 sc->sc_enabled = 1;
317
318 /* Registers are linear. */
319 for (i = 0; i < 16; i++)
320 sc->sc_reg_map[i] = i;
321
322 /* Now we can use the NIC_{GET,PUT}() macros. */
323
324 /*
325 * Reset NIC and ASIC. Enable on-board transeiver throughout
326 * reset sequence since it will lock up if the cable isn't
327 * connected if we don't.
328 */
329 bus_space_write_1(asict, asich, ELINK2_CR,
330 ELINK2_CR_RST | ELINK2_CR_XSEL);
331
332 /* Wait for a while, then un-reset it. */
333 delay(50);
334
335 /*
336 * The 3Com ASIC defaults to rather strange settings for the CR
337 * after a reset. It's important to set it again after the
338 * following write (this is done when we map the PROM below).
339 */
340 bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
341
342 /* Wait a bit for the NIC to recover from the reset. */
343 delay(5000);
344
345 /*
346 * Get the station address from on-board ROM.
347 *
348 * First, map Ethernet address PROM over the top of where the NIC
349 * registers normally appear.
350 */
351 bus_space_write_1(asict, asich, ELINK2_CR,
352 ELINK2_CR_XSEL | ELINK2_CR_EALO);
353
354 for (i = 0; i < ETHER_ADDR_LEN; i++)
355 sc->sc_enaddr[i] = NIC_GET(nict, nich, i);
356
357 /*
358 * Unmap PROM - select NIC registers. The proper setting of the
359 * transciever is set in later in ec_init_card() via dp8390_init().
360 */
361 bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
362
363 /* Determine if this is an 8-bit or 16-bit board. */
364
365 /* Select page 0 registers. */
366 NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
367
368 /*
369 * Attempt to clear WTS. If it doesn't clear, then this is a
370 * 16-bit board.
371 */
372 NIC_PUT(nict, nich, ED_P0_DCR, 0);
373
374 /* Select page 2 registers. */
375 NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_2 | ED_CR_STP);
376
377 /* The 3c503 forces the WTS bit to a one if this is a 16-bit board. */
378 if (NIC_GET(nict, nich, ED_P2_DCR) & ED_DCR_WTS)
379 esc->sc_16bitp = 1;
380 else
381 esc->sc_16bitp = 0;
382
383 printf("%s: 3Com 3c503 Ethernet (%s-bit)\n",
384 sc->sc_dev.dv_xname, esc->sc_16bitp ? "16" : "8");
385
386 /* Select page 0 registers. */
387 NIC_PUT(nict, nich, ED_P2_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
388
389 sc->cr_proto = ED_CR_RD2;
390
391 /*
392 * DCR gets:
393 *
394 * FIFO threshold to 8, No auto-init Remote DMA,
395 * byte order=80x86.
396 *
397 * 16-bit cards also get word-wide DMA transfers.
398 */
399 sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
400 (esc->sc_16bitp ? ED_DCR_WTS : 0);
401
402 sc->test_mem = ec_fake_test_mem;
403 sc->ring_copy = ec_ring_copy;
404 sc->write_mbuf = ec_write_mbuf;
405 sc->read_hdr = ec_read_hdr;
406 sc->init_card = ec_init_card;
407
408 sc->sc_media_init = ec_media_init;
409
410 sc->sc_mediachange = ec_mediachange;
411 sc->sc_mediastatus = ec_mediastatus;
412
413 sc->mem_start = 0;
414 sc->mem_size = memsize;
415
416 /* Do generic parts of attach. */
417 if (dp8390_config(sc)) {
418 printf("%s: configuration failed\n", sc->sc_dev.dv_xname);
419 return;
420 }
421
422 /*
423 * We need to override the way dp8390_config() set up our
424 * shared memory.
425 *
426 * We have an entire 8k window to put the transmit buffers on the
427 * 16-bit boards. But since the 16bit 3c503's shared memory is only
428 * fast enough to overlap the loading of one full-size packet, trying
429 * to load more than 2 buffers can actually leave the transmitter idle
430 * during the load. So 2 seems the best value. (Although a mix of
431 * variable-sized packets might change this assumption. Nonetheless,
432 * we optimize for linear transfers of same-size packets.)
433 */
434 if (esc->sc_16bitp) {
435 if (sc->sc_dev.dv_cfdata->cf_flags & DP8390_NO_MULTI_BUFFERING)
436 sc->txb_cnt = 1;
437 else
438 sc->txb_cnt = 2;
439
440 sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_16BIT;
441 sc->rec_page_start = ELINK2_RX_PAGE_OFFSET_16BIT;
442 sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
443 sc->rec_page_start;
444 sc->mem_ring = sc->mem_start;
445 } else {
446 sc->txb_cnt = 1;
447 sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_8BIT;
448 sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE;
449 sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
450 sc->tx_page_start;
451 sc->mem_ring = sc->mem_start +
452 (ED_TXBUF_SIZE << ED_PAGE_SHIFT);
453 }
454
455 /*
456 * Initialize CA page start/stop registers. Probably only needed
457 * if doing DMA, but what the Hell.
458 */
459 bus_space_write_1(asict, asich, ELINK2_PSTR, sc->rec_page_start);
460 bus_space_write_1(asict, asich, ELINK2_PSPR, sc->rec_page_stop);
461
462 /*
463 * Program the IRQ.
464 */
465 switch (ia->ia_irq) {
466 case 9: tmp = ELINK2_IDCFR_IRQ2; break;
467 case 3: tmp = ELINK2_IDCFR_IRQ3; break;
468 case 4: tmp = ELINK2_IDCFR_IRQ4; break;
469 case 5: tmp = ELINK2_IDCFR_IRQ5; break;
470 break;
471
472 case IRQUNK:
473 printf("%s: wildcarded IRQ is not allowed\n",
474 sc->sc_dev.dv_xname);
475 return;
476
477 default:
478 printf("%s: invalid IRQ %d, must be 3, 4, 5, or 9\n",
479 sc->sc_dev.dv_xname, ia->ia_irq);
480 return;
481 }
482
483 bus_space_write_1(asict, asich, ELINK2_IDCFR, tmp);
484
485 /*
486 * Initialize the GA configuration register. Set bank and enable
487 * shared memory.
488 */
489 bus_space_write_1(asict, asich, ELINK2_GACFR,
490 ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
491
492 /*
493 * Intialize "Vector Pointer" registers. These gawd-awful things
494 * are compared to 20 bits of the address on the ISA, and if they
495 * match, the shared memory is disabled. We se them to 0xffff0...
496 * allegedly the reset vector.
497 */
498 bus_space_write_1(asict, asich, ELINK2_VPTR2, 0xff);
499 bus_space_write_1(asict, asich, ELINK2_VPTR1, 0xff);
500 bus_space_write_1(asict, asich, ELINK2_VPTR0, 0x00);
501
502 /*
503 * Now run the real memory test.
504 */
505 if (ec_test_mem(sc)) {
506 printf("%s: memory test failed\n", sc->sc_dev.dv_xname);
507 return;
508 }
509
510 /* Establish interrupt handler. */
511 esc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
512 IPL_NET, dp8390_intr, sc);
513 if (esc->sc_ih == NULL)
514 printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
515 }
516
517 int
518 ec_fake_test_mem(sc)
519 struct dp8390_softc *sc;
520 {
521
522 /*
523 * We have to do this after we initialize the GA, but we
524 * have to do that after calling dp8390_config(), which
525 * wants to test memory. Put this noop here, and then
526 * actually test memory later.
527 */
528 return (0);
529 }
530
531 int
532 ec_test_mem(sc)
533 struct dp8390_softc *sc;
534 {
535 struct ec_softc *esc = (struct ec_softc *)sc;
536 bus_space_tag_t memt = sc->sc_buft;
537 bus_space_handle_t memh = sc->sc_bufh;
538 bus_size_t memsize = sc->mem_size;
539 int i;
540
541 if (esc->sc_16bitp)
542 bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
543 else
544 bus_space_set_region_1(memt, memh, 0, 0, memsize);
545
546 if (esc->sc_16bitp) {
547 for (i = 0; i < memsize; i += 2) {
548 if (bus_space_read_2(memt, memh, i) != 0)
549 goto fail;
550 }
551 } else {
552 for (i = 0; i < memsize; i++) {
553 if (bus_space_read_1(memt, memh, i) != 0)
554 goto fail;
555 }
556 }
557
558 return (0);
559
560 fail:
561 printf("%s: failed to clear shared memory at offset 0x%x\n",
562 sc->sc_dev.dv_xname, i);
563 return (1);
564 }
565
566 /*
567 * Given a NIC memory source address and a host memory destination address,
568 * copy 'len' from NIC to host using shared memory. The 'len' is rounded
569 * up to a word - ok as long as mbufs are word-sized.
570 */
571 __inline void
572 ec_readmem(esc, from, to, len)
573 struct ec_softc *esc;
574 int from;
575 u_int8_t *to;
576 int len;
577 {
578 bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
579 bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
580
581 if (len & 1)
582 ++len;
583
584 if (esc->sc_16bitp)
585 bus_space_read_region_2(memt, memh, from, (u_int16_t *)to,
586 len >> 1);
587 else
588 bus_space_read_region_1(memt, memh, from, to, len);
589 }
590
591 int
592 ec_write_mbuf(sc, m, buf)
593 struct dp8390_softc *sc;
594 struct mbuf *m;
595 int buf;
596 {
597 struct ec_softc *esc = (struct ec_softc *)sc;
598 bus_space_tag_t asict = esc->sc_asict;
599 bus_space_handle_t asich = esc->sc_asich;
600 bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
601 bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
602 u_int8_t *data, savebyte[2];
603 int savelen, len, leftover;
604 #ifdef DIAGNOSTIC
605 u_int8_t *lim;
606 #endif
607
608 savelen = m->m_pkthdr.len;
609
610 /*
611 * 8-bit boards are simple: we're already in the correct
612 * page, and no alignment tricks are necessary.
613 */
614 if (esc->sc_16bitp == 0) {
615 for (; m != NULL; buf += m->m_len, m = m->m_next)
616 bus_space_write_region_1(memt, memh, buf,
617 mtod(m, u_int8_t *), m->m_len);
618 return (savelen);
619 }
620
621 /*
622 * If it's a 16-bit board, we have transmit buffers
623 * in a different page; switch to it.
624 */
625 if (esc->sc_16bitp)
626 bus_space_write_1(asict, asich, ELINK2_GACFR,
627 ELINK2_GACFR_RSEL);
628
629 /* Start out with no leftover data. */
630 leftover = 0;
631 savebyte[0] = savebyte[1] = 0;
632
633 for (; m != NULL; m = m->m_next) {
634 len = m->m_len;
635 if (len == 0)
636 continue;
637 data = mtod(m, u_int8_t *);
638 #ifdef DIAGNOSTIC
639 lim = data + len;
640 #endif
641 while (len > 0) {
642 if (leftover) {
643 /*
644 * Data left over (from mbuf or realignment).
645 * Buffer the next byte, and write it and
646 * the leftover data out.
647 */
648 savebyte[1] = *data++;
649 len--;
650 bus_space_write_2(memt, memh, buf,
651 *(u_int16_t *)savebyte);
652 buf += 2;
653 leftover = 0;
654 } else if (BUS_SPACE_ALIGNED_POINTER(data, u_int16_t)
655 == 0) {
656 /*
657 * Unaligned data; buffer the next byte.
658 */
659 savebyte[0] = *data++;
660 len--;
661 leftover = 1;
662 } else {
663 /*
664 * Aligned data; output contiguous words as
665 * much as we can, then buffer the remaining
666 * byte, if any.
667 */
668 leftover = len & 1;
669 len &= ~1;
670 bus_space_write_region_2(memt, memh, buf,
671 (u_int16_t *)data, len >> 1);
672 data += len;
673 buf += len;
674 if (leftover)
675 savebyte[0] = *data++;
676 len = 0;
677 }
678 }
679 if (len < 0)
680 panic("ec_write_mbuf: negative len");
681 #ifdef DIAGNOSTIC
682 if (data != lim)
683 panic("ec_write_mbuf: data != lim");
684 #endif
685 }
686 if (leftover) {
687 savebyte[1] = 0;
688 bus_space_write_2(memt, memh, buf, *(u_int16_t *)savebyte);
689 }
690
691 /*
692 * Switch back to receive page.
693 */
694 if (esc->sc_16bitp)
695 bus_space_write_1(asict, asich, ELINK2_GACFR,
696 ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
697
698 return (savelen);
699 }
700
701 int
702 ec_ring_copy(sc, src, dst, amount)
703 struct dp8390_softc *sc;
704 int src;
705 caddr_t dst;
706 u_short amount;
707 {
708 struct ec_softc *esc = (struct ec_softc *)sc;
709 u_short tmp_amount;
710
711 /* Does copy wrap to lower addr in ring buffer? */
712 if (src + amount > sc->mem_end) {
713 tmp_amount = sc->mem_end - src;
714
715 /* Copy amount up to end of NIC memory. */
716 ec_readmem(esc, src, dst, tmp_amount);
717
718 amount -= tmp_amount;
719 src = sc->mem_ring;
720 dst += tmp_amount;
721 }
722
723 ec_readmem(esc, src, dst, amount);
724
725 return (src + amount);
726 }
727
728 void
729 ec_read_hdr(sc, packet_ptr, packet_hdrp)
730 struct dp8390_softc *sc;
731 int packet_ptr;
732 struct dp8390_ring *packet_hdrp;
733 {
734 struct ec_softc *esc = (struct ec_softc *)sc;
735
736 ec_readmem(esc, packet_ptr, (u_int8_t *)packet_hdrp,
737 sizeof(struct dp8390_ring));
738 #if BYTE_ORDER == BIG_ENDIAN
739 packet_hdrp->count = bswap16(packet_hdrp->count);
740 #endif
741 }
742
743 void
744 ec_media_init(struct dp8390_softc *sc)
745 {
746
747 ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
748 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL);
749 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL);
750 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_2);
751 }
752
753 int
754 ec_mediachange(sc)
755 struct dp8390_softc *sc;
756 {
757 struct ec_softc *esc = (struct ec_softc *)sc;
758 struct ifmedia *ifm = &sc->sc_media;
759
760 return (ec_set_media(esc, ifm->ifm_media));
761 }
762
763 void
764 ec_mediastatus(sc, ifmr)
765 struct dp8390_softc *sc;
766 struct ifmediareq *ifmr;
767 {
768 struct ifmedia *ifm = &sc->sc_media;
769
770 /*
771 * The currently selected media is always the active media.
772 */
773 ifmr->ifm_active = ifm->ifm_cur->ifm_media;
774 }
775
776 void
777 ec_init_card(sc)
778 struct dp8390_softc *sc;
779 {
780 struct ec_softc *esc = (struct ec_softc *)sc;
781 struct ifmedia *ifm = &sc->sc_media;
782
783 (void) ec_set_media(esc, ifm->ifm_cur->ifm_media);
784 }
785
786 int
787 ec_set_media(esc, media)
788 struct ec_softc *esc;
789 int media;
790 {
791 u_int8_t new;
792
793 if (IFM_TYPE(media) != IFM_ETHER)
794 return (EINVAL);
795
796 switch (IFM_SUBTYPE(media)) {
797 case IFM_10_2:
798 new = ELINK2_CR_XSEL;
799 break;
800
801 case IFM_10_5:
802 new = 0;
803 break;
804
805 default:
806 return (EINVAL);
807 }
808
809 bus_space_write_1(esc->sc_asict, esc->sc_asich, ELINK2_CR, new);
810 return (0);
811 }
812