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