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