if_ec.c revision 1.7 1 /* $NetBSD: if_ec.c,v 1.7 1998/06/25 19:21:03 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 "bpfilter.h"
58 #include "rnd.h"
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/device.h>
63 #include <sys/socket.h>
64 #include <sys/mbuf.h>
65 #include <sys/syslog.h>
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
72 #include <net/if_ether.h>
73
74 #ifdef INET
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/if_inarp.h>
80 #endif
81
82 #ifdef NS
83 #include <netns/ns.h>
84 #include <netns/ns_if.h>
85 #endif
86
87 #if NBPFILTER > 0
88 #include <net/bpf.h>
89 #include <net/bpfdesc.h>
90 #endif
91
92 #include <machine/bus.h>
93 #include <machine/intr.h>
94
95 #include <dev/isa/isareg.h>
96 #include <dev/isa/isavar.h>
97
98 #include <dev/ic/dp8390reg.h>
99 #include <dev/ic/dp8390var.h>
100
101 #include <dev/isa/if_ecreg.h>
102
103 struct ec_softc {
104 struct dp8390_softc sc_dp8390;
105
106 bus_space_tag_t sc_asict; /* space tag for ASIC */
107 bus_space_handle_t sc_asich; /* space handle for ASIC */
108
109 int sc_16bitp; /* are we 16 bit? */
110
111 void *sc_ih; /* interrupt handle */
112 };
113
114 int ec_probe __P((struct device *, struct cfdata *, void *));
115 void ec_attach __P((struct device *, struct device *, void *));
116
117 struct cfattach ec_ca = {
118 sizeof(struct ec_softc), ec_probe, ec_attach
119 };
120
121 int ec_set_media __P((struct ec_softc *, int));
122
123 int ec_mediachange __P((struct dp8390_softc *));
124 void ec_mediastatus __P((struct dp8390_softc *, struct ifmediareq *));
125
126 void ec_init_card __P((struct dp8390_softc *));
127 int ec_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
128 int ec_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
129 void ec_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
130 int ec_fake_test_mem __P((struct dp8390_softc *));
131 int ec_test_mem __P((struct dp8390_softc *));
132
133 __inline void ec_readmem __P((struct ec_softc *, int, u_int8_t *, int));
134
135 static const int ec_iobase[] = {
136 0x2e0, 0x2a0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300,
137 };
138 #define NEC_IOBASE (sizeof(ec_iobase) / sizeof(ec_iobase[0]))
139
140 static const int ec_membase[] = {
141 MADDRUNK, MADDRUNK, MADDRUNK, MADDRUNK, 0xc8000, 0xcc000,
142 0xd8000, 0xdc000,
143 };
144 #define NEC_MEMBASE (sizeof(ec_membase) / sizeof(ec_membase[0]))
145
146 int ec_media[] = {
147 IFM_ETHER|IFM_10_2,
148 IFM_ETHER|IFM_10_5,
149 };
150 #define NEC_MEDIA (sizeof(ec_media) / sizeof(ec_media[0]))
151 #define EC_DEFMEDIA (IFM_ETHER|IFM_10_2)
152
153 int
154 ec_probe(parent, match, aux)
155 struct device *parent;
156 struct cfdata *match;
157 void *aux;
158 {
159 struct isa_attach_args *ia = aux;
160 bus_space_tag_t nict, asict, memt;
161 bus_space_handle_t nich, asich, memh;
162 bus_size_t memsize;
163 int nich_valid, asich_valid, memh_valid;
164 int i, rv = 0;
165 u_int8_t x;
166
167 nict = asict = ia->ia_iot;
168 memt = ia->ia_memt;
169
170 nich_valid = asich_valid = memh_valid = 0;
171
172 /*
173 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
174 * to it.
175 */
176 memsize = 8192;
177
178 /* Disallow wildcarded i/o addresses. */
179 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
180 return (0);
181
182 /* Disallow wildcarded mem address. */
183 if (ia->ia_maddr == ISACF_IOMEM_DEFAULT)
184 return (0);
185
186 /* Validate the i/o base. */
187 for (i = 0; i < NEC_IOBASE; i++)
188 if (ia->ia_iobase == ec_iobase[i])
189 break;
190 if (i == NEC_IOBASE)
191 return (0);
192
193 /* Validate the mem base. */
194 for (i = 0; i < NEC_MEMBASE; i++) {
195 if (ec_membase[i] == MADDRUNK)
196 continue;
197 if (ia->ia_maddr == ec_membase[i])
198 break;
199 }
200 if (i == NEC_MEMBASE)
201 return (0);
202
203 /* Attempt to map the NIC space. */
204 if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
205 ELINK2_NIC_PORTS, 0, &nich))
206 goto out;
207 nich_valid = 1;
208
209 /* Attempt to map the ASIC space. */
210 if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
211 ELINK2_ASIC_PORTS, 0, &asich))
212 goto out;
213 asich_valid = 1;
214
215 /* Attempt to map the memory space. */
216 if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh))
217 goto out;
218 memh_valid = 1;
219
220 /*
221 * Verify that the kernel configured I/O address matches the
222 * board configured I/O address.
223 *
224 * This is really only useful to see if something that looks like
225 * the board is there; after all, we're already talking to it at
226 * this point.
227 */
228 x = bus_space_read_1(asict, asich, ELINK2_BCFR);
229 if (x == 0 || (x & (x - 1)) != 0)
230 goto out;
231 i = ffs(x) - 1;
232 if (ia->ia_iobase != ec_iobase[i])
233 goto out;
234
235 /*
236 * ...and for the memory address. Note we do not support
237 * cards configured with shared memory disabled.
238 */
239 x = bus_space_read_1(asict, asich, ELINK2_PCFR);
240 if (x == 0 || (x & (x - 1)) != 0)
241 goto out;
242 i = ffs(x) - 1;
243 if (ia->ia_maddr != ec_membase[i])
244 goto out;
245
246 /* So, we say we've found it! */
247 ia->ia_iosize = ELINK2_NIC_PORTS;
248 ia->ia_msize = memsize;
249 rv = 1;
250
251 out:
252 if (nich_valid)
253 bus_space_unmap(nict, nich, ELINK2_NIC_PORTS);
254 if (asich_valid)
255 bus_space_unmap(asict, asich, ELINK2_ASIC_PORTS);
256 if (memh_valid)
257 bus_space_unmap(memt, memh, memsize);
258 return (rv);
259 }
260
261 void
262 ec_attach(parent, self, aux)
263 struct device *parent, *self;
264 void *aux;
265 {
266 struct ec_softc *esc = (struct ec_softc *)self;
267 struct dp8390_softc *sc = &esc->sc_dp8390;
268 struct isa_attach_args *ia = aux;
269 bus_space_tag_t nict, asict, memt;
270 bus_space_handle_t nich, asich, memh;
271 bus_size_t memsize;
272 u_int8_t tmp;
273 int i;
274
275 printf("\n");
276
277 nict = asict = ia->ia_iot;
278 memt = ia->ia_memt;
279
280 /*
281 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
282 * to it.
283 */
284 memsize = 8192;
285
286 /* Map the NIC space. */
287 if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
288 ELINK2_NIC_PORTS, 0, &nich)) {
289 printf("%s: can't map nic i/o space\n",
290 sc->sc_dev.dv_xname);
291 return;
292 }
293
294 /* Map the ASIC space. */
295 if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
296 ELINK2_ASIC_PORTS, 0, &asich)) {
297 printf("%s: can't map asic i/o space\n",
298 sc->sc_dev.dv_xname);
299 return;
300 }
301
302 /* Map the memory space. */
303 if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh)) {
304 printf("%s: can't map shared memory\n",
305 sc->sc_dev.dv_xname);
306 return;
307 }
308
309 esc->sc_asict = asict;
310 esc->sc_asich = asich;
311
312 sc->sc_regt = nict;
313 sc->sc_regh = nich;
314
315 sc->sc_buft = memt;
316 sc->sc_bufh = memh;
317
318 /* Interface is always enabled. */
319 sc->sc_enabled = 1;
320
321 /* Registers are linear. */
322 for (i = 0; i < 16; i++)
323 sc->sc_reg_map[i] = i;
324
325 /* Now we can use the NIC_{GET,PUT}() macros. */
326
327 /*
328 * Reset NIC and ASIC. Enable on-board transeiver throughout
329 * reset sequence since it will lock up if the cable isn't
330 * connected if we don't.
331 */
332 bus_space_write_1(asict, asich, ELINK2_CR,
333 ELINK2_CR_RST | ELINK2_CR_XSEL);
334
335 /* Wait for a while, then un-reset it. */
336 delay(50);
337
338 /*
339 * The 3Com ASIC defaults to rather strange settings for the CR
340 * after a reset. It's important to set it again after the
341 * following write (this is done when we map the PROM below).
342 */
343 bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
344
345 /* Wait a bit for the NIC to recover from the reset. */
346 delay(5000);
347
348 /*
349 * Get the station address from on-board ROM.
350 *
351 * First, map Ethernet address PROM over the top of where the NIC
352 * registers normally appear.
353 */
354 bus_space_write_1(asict, asich, ELINK2_CR,
355 ELINK2_CR_XSEL | ELINK2_CR_EALO);
356
357 for (i = 0; i < ETHER_ADDR_LEN; i++)
358 sc->sc_enaddr[i] = NIC_GET(nict, nich, i);
359
360 /*
361 * Unmap PROM - select NIC registers. The proper setting of the
362 * transciever is set in later in ec_init_card() via dp8390_init().
363 */
364 bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
365
366 /* Determine if this is an 8-bit or 16-bit board. */
367
368 /* Select page 0 registers. */
369 NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
370
371 /*
372 * Attempt to clear WTS. If it doesn't clear, then this is a
373 * 16-bit board.
374 */
375 NIC_PUT(nict, nich, ED_P0_DCR, 0);
376
377 /* Select page 2 registers. */
378 NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_2 | ED_CR_STP);
379
380 /* The 3c503 forces the WTS bit to a one if this is a 16-bit board. */
381 if (NIC_GET(nict, nich, ED_P2_DCR) & ED_DCR_WTS)
382 esc->sc_16bitp = 1;
383 else
384 esc->sc_16bitp = 0;
385
386 printf("%s: 3Com 3c503 Ethernet (%s-bit)\n",
387 sc->sc_dev.dv_xname, esc->sc_16bitp ? "16" : "8");
388
389 /* Select page 0 registers. */
390 NIC_PUT(nict, nich, ED_P2_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
391
392 sc->cr_proto = ED_CR_RD2;
393
394 /*
395 * DCR gets:
396 *
397 * FIFO threshold to 8, No auto-init Remote DMA,
398 * byte order=80x86.
399 *
400 * 16-bit cards also get word-wide DMA transfers.
401 */
402 sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
403 (esc->sc_16bitp ? ED_DCR_WTS : 0);
404
405 sc->test_mem = ec_fake_test_mem;
406 sc->ring_copy = ec_ring_copy;
407 sc->write_mbuf = ec_write_mbuf;
408 sc->read_hdr = ec_read_hdr;
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, ec_media, NEC_MEDIA, EC_DEFMEDIA)) {
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 (ALIGNED_POINTER(data, u_int16_t) == 0) {
655 /*
656 * Unaligned data; buffer the next byte.
657 */
658 savebyte[0] = *data++;
659 len--;
660 leftover = 1;
661 } else {
662 /*
663 * Aligned data; output contiguous words as
664 * much as we can, then buffer the remaining
665 * byte, if any.
666 */
667 leftover = len & 1;
668 len &= ~1;
669 bus_space_write_region_2(memt, memh, buf,
670 (u_int16_t *)data, len >> 1);
671 data += len;
672 buf += len;
673 if (leftover)
674 savebyte[0] = *data++;
675 len = 0;
676 }
677 }
678 if (len < 0)
679 panic("ec_write_mbuf: negative len");
680 #ifdef DIAGNOSTIC
681 if (data != lim)
682 panic("ec_write_mbuf: data != lim");
683 #endif
684 }
685 if (leftover) {
686 savebyte[1] = 0;
687 bus_space_write_2(memt, memh, buf, *(u_int16_t *)savebyte);
688 }
689
690 /*
691 * Switch back to receive page.
692 */
693 if (esc->sc_16bitp)
694 bus_space_write_1(asict, asich, ELINK2_GACFR,
695 ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
696
697 return (savelen);
698 }
699
700 int
701 ec_ring_copy(sc, src, dst, amount)
702 struct dp8390_softc *sc;
703 int src;
704 caddr_t dst;
705 u_short amount;
706 {
707 struct ec_softc *esc = (struct ec_softc *)sc;
708 u_short tmp_amount;
709
710 /* Does copy wrap to lower addr in ring buffer? */
711 if (src + amount > sc->mem_end) {
712 tmp_amount = sc->mem_end - src;
713
714 /* Copy amount up to end of NIC memory. */
715 ec_readmem(esc, src, dst, tmp_amount);
716
717 amount -= tmp_amount;
718 src = sc->mem_ring;
719 dst += tmp_amount;
720 }
721
722 ec_readmem(esc, src, dst, amount);
723
724 return (src + amount);
725 }
726
727 void
728 ec_read_hdr(sc, packet_ptr, packet_hdrp)
729 struct dp8390_softc *sc;
730 int packet_ptr;
731 struct dp8390_ring *packet_hdrp;
732 {
733 struct ec_softc *esc = (struct ec_softc *)sc;
734
735 ec_readmem(esc, packet_ptr, (u_int8_t *)packet_hdrp,
736 sizeof(struct dp8390_ring));
737 #if BYTE_ORDER == BIG_ENDIAN
738 packet_hdrp->count = bswap16(packet_hdrp->count);
739 #endif
740 }
741
742 int
743 ec_mediachange(sc)
744 struct dp8390_softc *sc;
745 {
746 struct ec_softc *esc = (struct ec_softc *)sc;
747 struct ifmedia *ifm = &sc->sc_media;
748
749 return (ec_set_media(esc, ifm->ifm_media));
750 }
751
752 void
753 ec_mediastatus(sc, ifmr)
754 struct dp8390_softc *sc;
755 struct ifmediareq *ifmr;
756 {
757 struct ifmedia *ifm = &sc->sc_media;
758
759 /*
760 * The currently selected media is always the active media.
761 */
762 ifmr->ifm_active = ifm->ifm_cur->ifm_media;
763 }
764
765 void
766 ec_init_card(sc)
767 struct dp8390_softc *sc;
768 {
769 struct ec_softc *esc = (struct ec_softc *)sc;
770 struct ifmedia *ifm = &sc->sc_media;
771
772 (void) ec_set_media(esc, ifm->ifm_cur->ifm_media);
773 }
774
775 int
776 ec_set_media(esc, media)
777 struct ec_softc *esc;
778 int media;
779 {
780 u_int8_t new;
781
782 if (IFM_TYPE(media) != IFM_ETHER)
783 return (EINVAL);
784
785 switch (IFM_SUBTYPE(media)) {
786 case IFM_10_2:
787 new = ELINK2_CR_XSEL;
788 break;
789
790 case IFM_10_5:
791 new = 0;
792 break;
793
794 default:
795 return (EINVAL);
796 }
797
798 bus_space_write_1(esc->sc_asict, esc->sc_asich, ELINK2_CR, new);
799 return (0);
800 }
801