mtd803.c revision 1.26 1 /* $NetBSD: mtd803.c,v 1.26 2012/07/22 14:32:57 matt Exp $ */
2
3 /*-
4 *
5 * Copyright (c) 2002 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Peter Bex <Peter.Bex (at) student.kun.nl>.
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * TODO:
35 * - Most importantly, get some bus_dmamap_syncs in the correct places.
36 * I don't have access to a computer with PCI other than i386, and i386
37 * is just such a machine where dmamap_syncs don't do anything.
38 * - Powerhook for when resuming after standby.
39 * - Watchdog stuff doesn't work yet, the system crashes.
40 * - There seems to be a CardBus version of the card. (see datasheet)
41 * Perhaps a detach function is necessary then? (free buffs, stop rx/tx etc)
42 * - When you enable the TXBUN (Tx buffer unavailable) interrupt, it gets
43 * raised every time a packet is sent. Strange, since everything works anyway
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: mtd803.c,v 1.26 2012/07/22 14:32:57 matt Exp $");
48
49
50 #include <sys/param.h>
51 #include <sys/mbuf.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/syslog.h>
57
58 #include <net/if.h>
59 #include <net/if_ether.h>
60 #include <net/if_media.h>
61
62 #ifdef INET
63 #include <netinet/in.h>
64 #include <netinet/if_inarp.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
68 #endif
69
70 #include <net/bpf.h>
71 #include <net/bpfdesc.h>
72
73 #include <sys/bus.h>
74
75 #include <dev/ic/mtd803reg.h>
76 #include <dev/ic/mtd803var.h>
77 #include <dev/mii/mii.h>
78 #include <dev/mii/miivar.h>
79
80 /*
81 * Device driver for the MTD803 3-in-1 Fast Ethernet Controller
82 * Written by Peter Bex (peter.bex (at) student.kun.nl)
83 *
84 * Datasheet at: http://www.myson.com.tw or http://www.century-semi.com
85 */
86
87 #define MTD_READ_1(sc, reg) \
88 bus_space_read_1((sc)->bus_tag, (sc)->bus_handle, (reg))
89 #define MTD_WRITE_1(sc, reg, data) \
90 bus_space_write_1((sc)->bus_tag, (sc)->bus_handle, (reg), (data))
91
92 #define MTD_READ_2(sc, reg) \
93 bus_space_read_2((sc)->bus_tag, (sc)->bus_handle, (reg))
94 #define MTD_WRITE_2(sc, reg, data) \
95 bus_space_write_2((sc)->bus_tag, (sc)->bus_handle, (reg), (data))
96
97 #define MTD_READ_4(sc, reg) \
98 bus_space_read_4((sc)->bus_tag, (sc)->bus_handle, (reg))
99 #define MTD_WRITE_4(sc, reg, data) \
100 bus_space_write_4((sc)->bus_tag, (sc)->bus_handle, (reg), (data))
101
102 #define MTD_SETBIT(sc, reg, x) \
103 MTD_WRITE_4((sc), (reg), MTD_READ_4((sc), (reg)) | (x))
104 #define MTD_CLRBIT(sc, reg, x) \
105 MTD_WRITE_4((sc), (reg), MTD_READ_4((sc), (reg)) & ~(x))
106
107 #define ETHER_CRC32(buf, len) (ether_crc32_be((buf), (len)))
108
109 int mtd_mii_readreg(device_t, int, int);
110 void mtd_mii_writereg(device_t, int, int, int);
111 void mtd_mii_statchg(struct ifnet *);
112
113 void mtd_start(struct ifnet *);
114 void mtd_stop(struct ifnet *, int);
115 int mtd_ioctl(struct ifnet *, u_long, void *);
116 void mtd_setmulti(struct mtd_softc *);
117 void mtd_watchdog(struct ifnet *);
118
119 int mtd_init(struct ifnet *);
120 void mtd_reset(struct mtd_softc *);
121 void mtd_shutdown(void *);
122 int mtd_init_desc(struct mtd_softc *);
123 int mtd_put(struct mtd_softc *, int, struct mbuf *);
124 struct mbuf *mtd_get(struct mtd_softc *, int, int);
125
126 int mtd_rxirq(struct mtd_softc *);
127 int mtd_txirq(struct mtd_softc *);
128 int mtd_bufirq(struct mtd_softc *);
129
130
131 int
132 mtd_config(struct mtd_softc *sc)
133 {
134 struct ifnet *ifp = &sc->ethercom.ec_if;
135 int i;
136
137 /* Read station address */
138 for (i = 0; i < ETHER_ADDR_LEN; ++i)
139 sc->eaddr[i] = MTD_READ_1(sc, MTD_PAR0 + i);
140
141 /* Initialize ifnet structure */
142 memcpy(ifp->if_xname, device_xname(&sc->dev), IFNAMSIZ);
143 ifp->if_softc = sc;
144 ifp->if_init = mtd_init;
145 ifp->if_start = mtd_start;
146 ifp->if_stop = mtd_stop;
147 ifp->if_ioctl = mtd_ioctl;
148 ifp->if_watchdog = mtd_watchdog;
149 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
150 IFQ_SET_READY(&ifp->if_snd);
151
152 /* Setup MII interface */
153 sc->mii.mii_ifp = ifp;
154 sc->mii.mii_readreg = mtd_mii_readreg;
155 sc->mii.mii_writereg = mtd_mii_writereg;
156 sc->mii.mii_statchg = mtd_mii_statchg;
157
158 sc->ethercom.ec_mii = &sc->mii;
159 ifmedia_init(&sc->mii.mii_media, 0, ether_mediachange,
160 ether_mediastatus);
161
162 mii_attach(&sc->dev, &sc->mii, 0xffffffff, MII_PHY_ANY, 0, 0);
163
164 if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
165 aprint_error_dev(&sc->dev, "Unable to configure MII\n");
166 return 1;
167 } else {
168 ifmedia_set(&sc->mii.mii_media, IFM_ETHER | IFM_AUTO);
169 }
170
171 if (mtd_init_desc(sc))
172 return 1;
173
174 /* Attach interface */
175 if_attach(ifp);
176 ether_ifattach(ifp, sc->eaddr);
177
178 /* Initialise random source */
179 rnd_attach_source(&sc->rnd_src, device_xname(&sc->dev),
180 RND_TYPE_NET, 0);
181
182 /* Add shutdown hook to reset card when we reboot */
183 sc->sd_hook = shutdownhook_establish(mtd_shutdown, sc);
184
185 return 0;
186 }
187
188
189 /*
190 * mtd_init
191 * Must be called at splnet()
192 */
193 int
194 mtd_init(struct ifnet *ifp)
195 {
196 struct mtd_softc *sc = ifp->if_softc;
197
198 mtd_reset(sc);
199
200 /*
201 * Set cache alignment and burst length. Don't really know what these
202 * mean, so their values are probably suboptimal.
203 */
204 MTD_WRITE_4(sc, MTD_BCR, MTD_BCR_BLEN16);
205
206 MTD_WRITE_4(sc, MTD_RXTXR, MTD_TX_STFWD | MTD_TX_FDPLX);
207
208 /* Promiscuous mode? */
209 if (ifp->if_flags & IFF_PROMISC)
210 MTD_SETBIT(sc, MTD_RXTXR, MTD_RX_PROM);
211 else
212 MTD_CLRBIT(sc, MTD_RXTXR, MTD_RX_PROM);
213
214 /* Broadcast mode? */
215 if (ifp->if_flags & IFF_BROADCAST)
216 MTD_SETBIT(sc, MTD_RXTXR, MTD_RX_ABROAD);
217 else
218 MTD_CLRBIT(sc, MTD_RXTXR, MTD_RX_ABROAD);
219
220 mtd_setmulti(sc);
221
222 /* Enable interrupts */
223 MTD_WRITE_4(sc, MTD_IMR, MTD_IMR_MASK);
224 MTD_WRITE_4(sc, MTD_ISR, MTD_ISR_ENABLE);
225
226 /* Set descriptor base addresses */
227 MTD_WRITE_4(sc, MTD_TXLBA, htole32(sc->desc_dma_map->dm_segs[0].ds_addr
228 + sizeof(struct mtd_desc) * MTD_NUM_RXD));
229 MTD_WRITE_4(sc, MTD_RXLBA,
230 htole32(sc->desc_dma_map->dm_segs[0].ds_addr));
231
232 /* Enable receiver and transmitter */
233 MTD_SETBIT(sc, MTD_RXTXR, MTD_RX_ENABLE);
234 MTD_SETBIT(sc, MTD_RXTXR, MTD_TX_ENABLE);
235
236 /* Interface is running */
237 ifp->if_flags |= IFF_RUNNING;
238 ifp->if_flags &= ~IFF_OACTIVE;
239
240 return 0;
241 }
242
243
244 int
245 mtd_init_desc(struct mtd_softc *sc)
246 {
247 int rseg, err, i;
248 bus_dma_segment_t seg;
249 bus_size_t size;
250
251 /* Allocate memory for descriptors */
252 size = (MTD_NUM_RXD + MTD_NUM_TXD) * sizeof(struct mtd_desc);
253
254 /* Allocate DMA-safe memory */
255 if ((err = bus_dmamem_alloc(sc->dma_tag, size, MTD_DMA_ALIGN,
256 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
257 aprint_error_dev(&sc->dev, "unable to allocate DMA buffer, error = %d\n", err);
258 return 1;
259 }
260
261 /* Map memory to kernel addressable space */
262 if ((err = bus_dmamem_map(sc->dma_tag, &seg, 1, size,
263 (void **)&sc->desc, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
264 aprint_error_dev(&sc->dev, "unable to map DMA buffer, error = %d\n", err);
265 bus_dmamem_free(sc->dma_tag, &seg, rseg);
266 return 1;
267 }
268
269 /* Create a DMA map */
270 if ((err = bus_dmamap_create(sc->dma_tag, size, 1,
271 size, 0, BUS_DMA_NOWAIT, &sc->desc_dma_map)) != 0) {
272 aprint_error_dev(&sc->dev, "unable to create DMA map, error = %d\n", err);
273 bus_dmamem_unmap(sc->dma_tag, (void *)sc->desc, size);
274 bus_dmamem_free(sc->dma_tag, &seg, rseg);
275 return 1;
276 }
277
278 /* Load the DMA map */
279 if ((err = bus_dmamap_load(sc->dma_tag, sc->desc_dma_map, sc->desc,
280 size, NULL, BUS_DMA_NOWAIT)) != 0) {
281 aprint_error_dev(&sc->dev, "unable to load DMA map, error = %d\n",
282 err);
283 bus_dmamap_destroy(sc->dma_tag, sc->desc_dma_map);
284 bus_dmamem_unmap(sc->dma_tag, (void *)sc->desc, size);
285 bus_dmamem_free(sc->dma_tag, &seg, rseg);
286 return 1;
287 }
288
289 /* Allocate memory for the buffers */
290 size = MTD_NUM_RXD * MTD_RXBUF_SIZE + MTD_NUM_TXD * MTD_TXBUF_SIZE;
291
292 /* Allocate DMA-safe memory */
293 if ((err = bus_dmamem_alloc(sc->dma_tag, size, MTD_DMA_ALIGN,
294 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
295 aprint_error_dev(&sc->dev, "unable to allocate DMA buffer, error = %d\n",
296 err);
297
298 /* Undo DMA map for descriptors */
299 bus_dmamap_unload(sc->dma_tag, sc->desc_dma_map);
300 bus_dmamap_destroy(sc->dma_tag, sc->desc_dma_map);
301 bus_dmamem_unmap(sc->dma_tag, (void *)sc->desc, size);
302 bus_dmamem_free(sc->dma_tag, &seg, rseg);
303 return 1;
304 }
305
306 /* Map memory to kernel addressable space */
307 if ((err = bus_dmamem_map(sc->dma_tag, &seg, 1, size,
308 &sc->buf, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
309 aprint_error_dev(&sc->dev, "unable to map DMA buffer, error = %d\n",
310 err);
311 bus_dmamem_free(sc->dma_tag, &seg, rseg);
312
313 /* Undo DMA map for descriptors */
314 bus_dmamap_unload(sc->dma_tag, sc->desc_dma_map);
315 bus_dmamap_destroy(sc->dma_tag, sc->desc_dma_map);
316 bus_dmamem_unmap(sc->dma_tag, (void *)sc->desc, size);
317 bus_dmamem_free(sc->dma_tag, &seg, rseg);
318 return 1;
319 }
320
321 /* Create a DMA map */
322 if ((err = bus_dmamap_create(sc->dma_tag, size, 1,
323 size, 0, BUS_DMA_NOWAIT, &sc->buf_dma_map)) != 0) {
324 aprint_error_dev(&sc->dev, "unable to create DMA map, error = %d\n",
325 err);
326 bus_dmamem_unmap(sc->dma_tag, sc->buf, size);
327 bus_dmamem_free(sc->dma_tag, &seg, rseg);
328
329 /* Undo DMA map for descriptors */
330 bus_dmamap_unload(sc->dma_tag, sc->desc_dma_map);
331 bus_dmamap_destroy(sc->dma_tag, sc->desc_dma_map);
332 bus_dmamem_unmap(sc->dma_tag, (void *)sc->desc, size);
333 bus_dmamem_free(sc->dma_tag, &seg, rseg);
334 return 1;
335 }
336
337 /* Load the DMA map */
338 if ((err = bus_dmamap_load(sc->dma_tag, sc->buf_dma_map, sc->buf,
339 size, NULL, BUS_DMA_NOWAIT)) != 0) {
340 aprint_error_dev(&sc->dev, "unable to load DMA map, error = %d\n",
341 err);
342 bus_dmamap_destroy(sc->dma_tag, sc->buf_dma_map);
343 bus_dmamem_unmap(sc->dma_tag, sc->buf, size);
344 bus_dmamem_free(sc->dma_tag, &seg, rseg);
345
346 /* Undo DMA map for descriptors */
347 bus_dmamap_unload(sc->dma_tag, sc->desc_dma_map);
348 bus_dmamap_destroy(sc->dma_tag, sc->desc_dma_map);
349 bus_dmamem_unmap(sc->dma_tag, (void *)sc->desc, size);
350 bus_dmamem_free(sc->dma_tag, &seg, rseg);
351 return 1;
352 }
353
354 /* Descriptors are stored as a circular linked list */
355 /* Fill in rx descriptors */
356 for (i = 0; i < MTD_NUM_RXD; ++i) {
357 sc->desc[i].stat = MTD_RXD_OWNER;
358 if (i == MTD_NUM_RXD - 1) { /* Last descriptor */
359 /* Link back to first rx descriptor */
360 sc->desc[i].next =
361 htole32(sc->desc_dma_map->dm_segs[0].ds_addr);
362 } else {
363 /* Link forward to next rx descriptor */
364 sc->desc[i].next =
365 htole32(sc->desc_dma_map->dm_segs[0].ds_addr
366 + (i + 1) * sizeof(struct mtd_desc));
367 }
368 sc->desc[i].conf = MTD_RXBUF_SIZE & MTD_RXD_CONF_BUFS;
369 /* Set buffer's address */
370 sc->desc[i].data = htole32(sc->buf_dma_map->dm_segs[0].ds_addr
371 + i * MTD_RXBUF_SIZE);
372 }
373
374 /* Fill in tx descriptors */
375 for (/* i = MTD_NUM_RXD */; i < (MTD_NUM_TXD + MTD_NUM_RXD); ++i) {
376 sc->desc[i].stat = 0; /* At least, NOT MTD_TXD_OWNER! */
377 if (i == (MTD_NUM_RXD + MTD_NUM_TXD - 1)) { /* Last descr */
378 /* Link back to first tx descriptor */
379 sc->desc[i].next =
380 htole32(sc->desc_dma_map->dm_segs[0].ds_addr
381 +MTD_NUM_RXD * sizeof(struct mtd_desc));
382 } else {
383 /* Link forward to next tx descriptor */
384 sc->desc[i].next =
385 htole32(sc->desc_dma_map->dm_segs[0].ds_addr
386 + (i + 1) * sizeof(struct mtd_desc));
387 }
388 /* sc->desc[i].conf = MTD_TXBUF_SIZE & MTD_TXD_CONF_BUFS; */
389 /* Set buffer's address */
390 sc->desc[i].data = htole32(sc->buf_dma_map->dm_segs[0].ds_addr
391 + MTD_NUM_RXD * MTD_RXBUF_SIZE
392 + (i - MTD_NUM_RXD) * MTD_TXBUF_SIZE);
393 }
394
395 return 0;
396 }
397
398
399 void
400 mtd_mii_statchg(struct ifnet *ifp)
401 {
402 /* Should we do something here? :) */
403 }
404
405
406 int
407 mtd_mii_readreg(device_t self, int phy, int reg)
408 {
409 struct mtd_softc *sc = device_private(self);
410
411 return (MTD_READ_2(sc, MTD_PHYBASE + reg * 2));
412 }
413
414
415 void
416 mtd_mii_writereg(device_t self, int phy, int reg, int val)
417 {
418 struct mtd_softc *sc = device_private(self);
419
420 MTD_WRITE_2(sc, MTD_PHYBASE + reg * 2, val);
421 }
422
423
424 int
425 mtd_put(struct mtd_softc *sc, int index, struct mbuf *m)
426 {
427 int len, tlen;
428 char *buf = (char *)sc->buf + MTD_NUM_RXD * MTD_RXBUF_SIZE
429 + index * MTD_TXBUF_SIZE;
430 struct mbuf *n;
431
432 for (tlen = 0; m != NULL; m = n) {
433 len = m->m_len;
434 if (len == 0) {
435 MFREE(m, n);
436 continue;
437 } else if (tlen > MTD_TXBUF_SIZE) {
438 /* XXX FIXME: No idea what to do here. */
439 aprint_error_dev(&sc->dev, "packet too large! Size = %i\n",
440 tlen);
441 MFREE(m, n);
442 continue;
443 }
444 memcpy(buf, mtod(m, void *), len);
445 buf += len;
446 tlen += len;
447 MFREE(m, n);
448 }
449 sc->desc[MTD_NUM_RXD + index].conf = MTD_TXD_CONF_PAD | MTD_TXD_CONF_CRC
450 | MTD_TXD_CONF_IRQC
451 | ((tlen << MTD_TXD_PKTS_SHIFT) & MTD_TXD_CONF_PKTS)
452 | (tlen & MTD_TXD_CONF_BUFS);
453
454 return tlen;
455 }
456
457
458 void
459 mtd_start(struct ifnet *ifp)
460 {
461 struct mtd_softc *sc = ifp->if_softc;
462 struct mbuf *m;
463 int len;
464 int first_tx = sc->cur_tx;
465
466 /* Don't transmit when the interface is busy or inactive */
467 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
468 return;
469
470 for (;;) {
471 IF_DEQUEUE(&ifp->if_snd, m);
472
473 if (m == NULL)
474 break;
475
476 bpf_mtap(ifp, m);
477
478 /* Copy mbuf chain into tx buffer */
479 len = mtd_put(sc, sc->cur_tx, m);
480
481 if (sc->cur_tx != first_tx)
482 sc->desc[MTD_NUM_RXD + sc->cur_tx].stat = MTD_TXD_OWNER;
483
484 if (++sc->cur_tx >= MTD_NUM_TXD)
485 sc->cur_tx = 0;
486 }
487 /* Mark first & last descriptor */
488 sc->desc[MTD_NUM_RXD + first_tx].conf |= MTD_TXD_CONF_FSD;
489
490 if (sc->cur_tx == 0) {
491 sc->desc[MTD_NUM_RXD + MTD_NUM_TXD - 1].conf |=MTD_TXD_CONF_LSD;
492 } else {
493 sc->desc[MTD_NUM_RXD + sc->cur_tx - 1].conf |= MTD_TXD_CONF_LSD;
494 }
495
496 /* Give first descriptor to chip to complete transaction */
497 sc->desc[MTD_NUM_RXD + first_tx].stat = MTD_TXD_OWNER;
498
499 /* Transmit polling demand */
500 MTD_WRITE_4(sc, MTD_TXPDR, MTD_TXPDR_DEMAND);
501
502 /* XXX FIXME: Set up a watchdog timer */
503 /* ifp->if_timer = 5; */
504 }
505
506
507 void
508 mtd_stop(struct ifnet *ifp, int disable)
509 {
510 struct mtd_softc *sc = ifp->if_softc;
511
512 /* Disable transmitter and receiver */
513 MTD_CLRBIT(sc, MTD_RXTXR, MTD_TX_ENABLE);
514 MTD_CLRBIT(sc, MTD_RXTXR, MTD_RX_ENABLE);
515
516 /* Disable interrupts */
517 MTD_WRITE_4(sc, MTD_IMR, 0x00000000);
518
519 /* Must do more at disable??... */
520 if (disable) {
521 /* Delete tx and rx descriptor base addresses */
522 MTD_WRITE_4(sc, MTD_RXLBA, 0x00000000);
523 MTD_WRITE_4(sc, MTD_TXLBA, 0x00000000);
524 }
525
526 ifp->if_timer = 0;
527 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
528 }
529
530
531 void
532 mtd_watchdog(struct ifnet *ifp)
533 {
534 struct mtd_softc *sc = ifp->if_softc;
535 int s;
536
537 log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->dev));
538 ++sc->ethercom.ec_if.if_oerrors;
539
540 mtd_stop(ifp, 0);
541
542 s = splnet();
543 mtd_init(ifp);
544 splx(s);
545
546 return;
547 }
548
549
550 int
551 mtd_ioctl(struct ifnet *ifp, u_long cmd, void *data)
552 {
553 struct mtd_softc *sc = ifp->if_softc;
554 int s, error = 0;
555
556 s = splnet();
557
558 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
559 /*
560 * Multicast list has changed; set the hardware
561 * filter accordingly.
562 */
563 if (ifp->if_flags & IFF_RUNNING)
564 mtd_setmulti(sc);
565 error = 0;
566 }
567
568 splx(s);
569 return error;
570 }
571
572
573 struct mbuf *
574 mtd_get(struct mtd_softc *sc, int index, int totlen)
575 {
576 struct ifnet *ifp = &sc->ethercom.ec_if;
577 struct mbuf *m, *m0, *newm;
578 int len;
579 char *buf = (char *)sc->buf + index * MTD_RXBUF_SIZE;
580
581 MGETHDR(m0, M_DONTWAIT, MT_DATA);
582 if (m0 == NULL)
583 return NULL;
584
585 m0->m_pkthdr.rcvif = ifp;
586 m0->m_pkthdr.len = totlen;
587 m = m0;
588 len = MHLEN;
589
590 while (totlen > 0) {
591 if (totlen >= MINCLSIZE) {
592 MCLGET(m, M_DONTWAIT);
593 if (!(m->m_flags & M_EXT)) {
594 m_freem(m0);
595 return NULL;
596 }
597 len = MCLBYTES;
598 }
599
600 if (m == m0) {
601 char *newdata = (char *)
602 ALIGN(m->m_data + sizeof(struct ether_header)) -
603 sizeof(struct ether_header);
604 len -= newdata - m->m_data;
605 m->m_data = newdata;
606 }
607
608 m->m_len = len = min(totlen, len);
609 memcpy(mtod(m, void *), buf, len);
610 buf += len;
611
612 totlen -= len;
613 if (totlen > 0) {
614 MGET(newm, M_DONTWAIT, MT_DATA);
615 if (newm == NULL) {
616 m_freem(m0);
617 return NULL;
618 }
619 len = MLEN;
620 m = m->m_next = newm;
621 }
622 }
623
624 return m0;
625 }
626
627
628 int
629 mtd_rxirq(struct mtd_softc *sc)
630 {
631 struct ifnet *ifp = &sc->ethercom.ec_if;
632 int len;
633 struct mbuf *m;
634
635 for (; !(sc->desc[sc->cur_rx].stat & MTD_RXD_OWNER);) {
636 /* Error summary set? */
637 if (sc->desc[sc->cur_rx].stat & MTD_RXD_ERRSUM) {
638 aprint_error_dev(&sc->dev, "received packet with errors\n");
639 /* Give up packet, since an error occurred */
640 sc->desc[sc->cur_rx].stat = MTD_RXD_OWNER;
641 sc->desc[sc->cur_rx].conf = MTD_RXBUF_SIZE &
642 MTD_RXD_CONF_BUFS;
643 ++ifp->if_ierrors;
644 if (++sc->cur_rx >= MTD_NUM_RXD)
645 sc->cur_rx = 0;
646 continue;
647 }
648 /* Get buffer length */
649 len = (sc->desc[sc->cur_rx].stat & MTD_RXD_FLEN)
650 >> MTD_RXD_FLEN_SHIFT;
651 len -= ETHER_CRC_LEN;
652
653 /* Check packet size */
654 if (len <= sizeof(struct ether_header)) {
655 aprint_error_dev(&sc->dev, "invalid packet size %d; dropping\n",
656 len);
657 sc->desc[sc->cur_rx].stat = MTD_RXD_OWNER;
658 sc->desc[sc->cur_rx].conf = MTD_RXBUF_SIZE &
659 MTD_RXD_CONF_BUFS;
660 ++ifp->if_ierrors;
661 if (++sc->cur_rx >= MTD_NUM_RXD)
662 sc->cur_rx = 0;
663 continue;
664 }
665
666 m = mtd_get(sc, (sc->cur_rx), len);
667
668 /* Give descriptor back to card */
669 sc->desc[sc->cur_rx].conf = MTD_RXBUF_SIZE & MTD_RXD_CONF_BUFS;
670 sc->desc[sc->cur_rx].stat = MTD_RXD_OWNER;
671
672 if (++sc->cur_rx >= MTD_NUM_RXD)
673 sc->cur_rx = 0;
674
675 if (m == NULL) {
676 aprint_error_dev(&sc->dev, "error pulling packet off interface\n");
677 ++ifp->if_ierrors;
678 continue;
679 }
680
681 ++ifp->if_ipackets;
682
683 bpf_mtap(ifp, m);
684 /* Pass the packet up */
685 (*ifp->if_input)(ifp, m);
686 }
687
688 return 1;
689 }
690
691
692 int
693 mtd_txirq(struct mtd_softc *sc)
694 {
695 struct ifnet *ifp = &sc->ethercom.ec_if;
696
697 /* Clear timeout */
698 ifp->if_timer = 0;
699
700 ifp->if_flags &= ~IFF_OACTIVE;
701 ++ifp->if_opackets;
702
703 /* XXX FIXME If there is some queued, do an mtd_start? */
704
705 return 1;
706 }
707
708
709 int
710 mtd_bufirq(struct mtd_softc *sc)
711 {
712 struct ifnet *ifp = &sc->ethercom.ec_if;
713
714 /* Clear timeout */
715 ifp->if_timer = 0;
716
717 /* XXX FIXME: Do something here to make sure we get some buffers! */
718
719 return 1;
720 }
721
722
723 int
724 mtd_irq_h(void *args)
725 {
726 struct mtd_softc *sc = args;
727 struct ifnet *ifp = &sc->ethercom.ec_if;
728 u_int32_t status;
729 int r = 0;
730
731 if (!(ifp->if_flags & IFF_RUNNING) || !device_is_active(&sc->dev))
732 return 0;
733
734 /* Disable interrupts */
735 MTD_WRITE_4(sc, MTD_IMR, 0x00000000);
736
737 for(;;) {
738 status = MTD_READ_4(sc, MTD_ISR);
739
740 /* Add random seed before masking out bits */
741 if (status)
742 rnd_add_uint32(&sc->rnd_src, status);
743
744 status &= MTD_ISR_MASK;
745 if (!status) /* We didn't ask for this */
746 break;
747
748 MTD_WRITE_4(sc, MTD_ISR, status);
749
750 /* NOTE: Perhaps we should reset with some of these errors? */
751
752 if (status & MTD_ISR_RXBUN) {
753 aprint_error_dev(&sc->dev, "receive buffer unavailable\n");
754 ++ifp->if_ierrors;
755 }
756
757 if (status & MTD_ISR_RXERR) {
758 aprint_error_dev(&sc->dev, "receive error\n");
759 ++ifp->if_ierrors;
760 }
761
762 if (status & MTD_ISR_TXBUN) {
763 aprint_error_dev(&sc->dev, "transmit buffer unavailable\n");
764 ++ifp->if_ierrors;
765 }
766
767 if ((status & MTD_ISR_PDF)) {
768 aprint_error_dev(&sc->dev, "parallel detection fault\n");
769 ++ifp->if_ierrors;
770 }
771
772 if (status & MTD_ISR_FBUSERR) {
773 aprint_error_dev(&sc->dev, "fatal bus error\n");
774 ++ifp->if_ierrors;
775 }
776
777 if (status & MTD_ISR_TARERR) {
778 aprint_error_dev(&sc->dev, "target error\n");
779 ++ifp->if_ierrors;
780 }
781
782 if (status & MTD_ISR_MASTERR) {
783 aprint_error_dev(&sc->dev, "master error\n");
784 ++ifp->if_ierrors;
785 }
786
787 if (status & MTD_ISR_PARERR) {
788 aprint_error_dev(&sc->dev, "parity error\n");
789 ++ifp->if_ierrors;
790 }
791
792 if (status & MTD_ISR_RXIRQ) /* Receive interrupt */
793 r |= mtd_rxirq(sc);
794
795 if (status & MTD_ISR_TXIRQ) /* Transmit interrupt */
796 r |= mtd_txirq(sc);
797
798 if (status & MTD_ISR_TXEARLY) /* Transmit early */
799 r |= mtd_txirq(sc);
800
801 if (status & MTD_ISR_TXBUN) /* Transmit buffer n/a */
802 r |= mtd_bufirq(sc);
803
804 }
805
806 /* Enable interrupts */
807 MTD_WRITE_4(sc, MTD_IMR, MTD_IMR_MASK);
808
809 return r;
810 }
811
812
813 void
814 mtd_setmulti(struct mtd_softc *sc)
815 {
816 struct ifnet *ifp = &sc->ethercom.ec_if;
817 u_int32_t rxtx_stat;
818 u_int32_t hash[2] = {0, 0};
819 u_int32_t crc;
820 struct ether_multi *enm;
821 struct ether_multistep step;
822 int mcnt = 0;
823
824 /* Get old status */
825 rxtx_stat = MTD_READ_4(sc, MTD_RXTXR);
826
827 if ((ifp->if_flags & IFF_ALLMULTI) || (ifp->if_flags & IFF_PROMISC)) {
828 rxtx_stat |= MTD_RX_AMULTI;
829 MTD_WRITE_4(sc, MTD_RXTXR, rxtx_stat);
830 MTD_WRITE_4(sc, MTD_MAR0, MTD_ALL_ADDR);
831 MTD_WRITE_4(sc, MTD_MAR1, MTD_ALL_ADDR);
832 return;
833 }
834
835 ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
836 while (enm != NULL) {
837 /* We need the 6 most significant bits of the CRC */
838 crc = ETHER_CRC32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
839
840 hash[crc >> 5] |= 1 << (crc & 0xf);
841
842 ++mcnt;
843 ETHER_NEXT_MULTI(step, enm);
844 }
845
846 /* Accept multicast bit needs to be on? */
847 if (mcnt)
848 rxtx_stat |= MTD_RX_AMULTI;
849 else
850 rxtx_stat &= ~MTD_RX_AMULTI;
851
852 /* Write out the hash */
853 MTD_WRITE_4(sc, MTD_MAR0, hash[0]);
854 MTD_WRITE_4(sc, MTD_MAR1, hash[1]);
855 MTD_WRITE_4(sc, MTD_RXTXR, rxtx_stat);
856 }
857
858
859 void
860 mtd_reset(struct mtd_softc *sc)
861 {
862 int i;
863
864 MTD_SETBIT(sc, MTD_BCR, MTD_BCR_RESET);
865
866 /* Reset descriptor status */
867 sc->cur_tx = 0;
868 sc->cur_rx = 0;
869
870 /* Wait until done with reset */
871 for (i = 0; i < MTD_TIMEOUT; ++i) {
872 DELAY(10);
873 if (!(MTD_READ_4(sc, MTD_BCR) & MTD_BCR_RESET))
874 break;
875 }
876
877 if (i == MTD_TIMEOUT) {
878 aprint_error_dev(&sc->dev, "reset timed out\n");
879 }
880
881 /* Wait a little so chip can stabilize */
882 DELAY(1000);
883 }
884
885
886 void
887 mtd_shutdown (void *arg)
888 {
889 struct mtd_softc *sc = arg;
890 struct ifnet *ifp = &sc->ethercom.ec_if;
891
892 rnd_detach_source(&sc->rnd_src);
893 mtd_stop(ifp, 1);
894 }
895