if_et.c revision 1.23 1 /* $NetBSD: if_et.c,v 1.23 2019/05/23 10:51:39 msaitoh Exp $ */
2 /* $OpenBSD: if_et.c,v 1.11 2008/06/08 06:18:07 jsg Exp $ */
3 /*
4 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
5 *
6 * This code is derived from software contributed to The DragonFly Project
7 * by Sepherosa Ziehau <sepherosa (at) gmail.com>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * $DragonFly: src/sys/dev/netif/et/if_et.c,v 1.1 2007/10/12 14:12:42 sephe Exp $
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_et.c,v 1.23 2019/05/23 10:51:39 msaitoh Exp $");
41
42 #include "opt_inet.h"
43 #include "vlan.h"
44
45 #include <sys/param.h>
46 #include <sys/endian.h>
47 #include <sys/systm.h>
48 #include <sys/types.h>
49 #include <sys/sockio.h>
50 #include <sys/mbuf.h>
51 #include <sys/queue.h>
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/callout.h>
55 #include <sys/socket.h>
56
57 #include <sys/bus.h>
58
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_media.h>
62 #include <net/if_ether.h>
63 #include <net/if_arp.h>
64
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 #include <netinet/if_inarp.h>
71 #endif
72
73 #include <net/bpf.h>
74
75 #include <dev/mii/mii.h>
76 #include <dev/mii/miivar.h>
77
78 #include <dev/pci/pcireg.h>
79 #include <dev/pci/pcivar.h>
80 #include <dev/pci/pcidevs.h>
81
82 #include <dev/pci/if_etreg.h>
83
84 int et_match(device_t, cfdata_t, void *);
85 void et_attach(device_t, device_t, void *);
86 int et_detach(device_t, int flags);
87 int et_shutdown(device_t);
88
89 int et_miibus_readreg(device_t, int, int, uint16_t *);
90 int et_miibus_writereg(device_t, int, int, uint16_t);
91 void et_miibus_statchg(struct ifnet *);
92
93 int et_init(struct ifnet *ifp);
94 int et_ioctl(struct ifnet *, u_long, void *);
95 void et_start(struct ifnet *);
96 void et_watchdog(struct ifnet *);
97
98 int et_intr(void *);
99 void et_enable_intrs(struct et_softc *, uint32_t);
100 void et_disable_intrs(struct et_softc *);
101 void et_rxeof(struct et_softc *);
102 void et_txeof(struct et_softc *);
103 void et_txtick(void *);
104
105 int et_dma_alloc(struct et_softc *);
106 void et_dma_free(struct et_softc *);
107 int et_dma_mem_create(struct et_softc *, bus_size_t,
108 void **, bus_addr_t *, bus_dmamap_t *, bus_dma_segment_t *);
109 void et_dma_mem_destroy(struct et_softc *, void *, bus_dmamap_t);
110 int et_dma_mbuf_create(struct et_softc *);
111 void et_dma_mbuf_destroy(struct et_softc *, int, const int[]);
112
113 int et_init_tx_ring(struct et_softc *);
114 int et_init_rx_ring(struct et_softc *);
115 void et_free_tx_ring(struct et_softc *);
116 void et_free_rx_ring(struct et_softc *);
117 int et_encap(struct et_softc *, struct mbuf **);
118 int et_newbuf(struct et_rxbuf_data *, int, int, int);
119 int et_newbuf_cluster(struct et_rxbuf_data *, int, int);
120 int et_newbuf_hdr(struct et_rxbuf_data *, int, int);
121
122 void et_stop(struct et_softc *);
123 int et_chip_init(struct et_softc *);
124 void et_chip_attach(struct et_softc *);
125 void et_init_mac(struct et_softc *);
126 void et_init_rxmac(struct et_softc *);
127 void et_init_txmac(struct et_softc *);
128 int et_init_rxdma(struct et_softc *);
129 int et_init_txdma(struct et_softc *);
130 int et_start_rxdma(struct et_softc *);
131 int et_start_txdma(struct et_softc *);
132 int et_stop_rxdma(struct et_softc *);
133 int et_stop_txdma(struct et_softc *);
134 int et_enable_txrx(struct et_softc *);
135 void et_reset(struct et_softc *);
136 int et_bus_config(struct et_softc *);
137 void et_get_eaddr(struct et_softc *, uint8_t[]);
138 void et_setmulti(struct et_softc *);
139 void et_tick(void *);
140
141 static int et_rx_intr_npkts = 32;
142 static int et_rx_intr_delay = 20; /* x10 usec */
143 static int et_tx_intr_nsegs = 128;
144 static uint32_t et_timer = 1000 * 1000 * 1000; /* nanosec */
145
146 struct et_bsize {
147 int bufsize;
148 et_newbuf_t newbuf;
149 };
150
151 static const struct et_bsize et_bufsize[ET_RX_NRING] = {
152 { .bufsize = 0, .newbuf = et_newbuf_hdr },
153 { .bufsize = 0, .newbuf = et_newbuf_cluster },
154 };
155
156 const struct et_product {
157 pci_vendor_id_t vendor;
158 pci_product_id_t product;
159 } et_devices[] = {
160 { PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1310 },
161 { PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1301 }
162 };
163
164 CFATTACH_DECL_NEW(et, sizeof(struct et_softc), et_match, et_attach, et_detach,
165 NULL);
166
167 int
168 et_match(device_t dev, cfdata_t match, void *aux)
169 {
170 struct pci_attach_args *pa = aux;
171 const struct et_product *ep;
172 int i;
173
174 for (i = 0; i < __arraycount(et_devices); i++) {
175 ep = &et_devices[i];
176 if (PCI_VENDOR(pa->pa_id) == ep->vendor &&
177 PCI_PRODUCT(pa->pa_id) == ep->product)
178 return 1;
179 }
180 return 0;
181 }
182
183 void
184 et_attach(device_t parent, device_t self, void *aux)
185 {
186 struct et_softc *sc = device_private(self);
187 struct pci_attach_args *pa = aux;
188 pci_chipset_tag_t pc = pa->pa_pc;
189 pci_intr_handle_t ih;
190 const char *intrstr;
191 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
192 struct mii_data * const mii = &sc->sc_miibus;
193 pcireg_t memtype;
194 int error;
195 char intrbuf[PCI_INTRSTR_LEN];
196
197 pci_aprint_devinfo(pa, "Ethernet controller");
198
199 sc->sc_dev = self;
200
201 /*
202 * Initialize tunables
203 */
204 sc->sc_rx_intr_npkts = et_rx_intr_npkts;
205 sc->sc_rx_intr_delay = et_rx_intr_delay;
206 sc->sc_tx_intr_nsegs = et_tx_intr_nsegs;
207 sc->sc_timer = et_timer;
208
209 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, ET_PCIR_BAR);
210 if (pci_mapreg_map(pa, ET_PCIR_BAR, memtype, 0, &sc->sc_mem_bt,
211 &sc->sc_mem_bh, NULL, &sc->sc_mem_size)) {
212 aprint_error_dev(self, "could not map mem space\n");
213 return;
214 }
215
216 if (pci_intr_map(pa, &ih) != 0) {
217 aprint_error_dev(self, "could not map interrupt\n");
218 goto fail;
219 }
220
221 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
222 sc->sc_irq_handle = pci_intr_establish_xname(pc, ih, IPL_NET, et_intr,
223 sc, device_xname(self));
224 if (sc->sc_irq_handle == NULL) {
225 aprint_error_dev(self, "could not establish interrupt");
226 if (intrstr != NULL)
227 aprint_error(" at %s", intrstr);
228 aprint_error("\n");
229 goto fail;
230 }
231 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
232
233 sc->sc_dmat = pa->pa_dmat;
234 sc->sc_pct = pa->pa_pc;
235 sc->sc_pcitag = pa->pa_tag;
236
237 error = et_bus_config(sc);
238 if (error)
239 goto fail;
240
241 et_get_eaddr(sc, sc->sc_enaddr);
242
243 aprint_normal_dev(self, "Ethernet address %s\n",
244 ether_sprintf(sc->sc_enaddr));
245
246 CSR_WRITE_4(sc, ET_PM,
247 ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE | ET_PM_RXCLK_GATE);
248
249 et_reset(sc);
250
251 et_disable_intrs(sc);
252
253 error = et_dma_alloc(sc);
254 if (error)
255 goto fail;
256
257 ifp->if_softc = sc;
258 ifp->if_mtu = ETHERMTU;
259 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
260 ifp->if_init = et_init;
261 ifp->if_ioctl = et_ioctl;
262 ifp->if_start = et_start;
263 ifp->if_watchdog = et_watchdog;
264 IFQ_SET_MAXLEN(&ifp->if_snd, ET_TX_NDESC);
265 IFQ_SET_READY(&ifp->if_snd);
266 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
267
268 et_chip_attach(sc);
269
270 mii->mii_ifp = ifp;
271 mii->mii_readreg = et_miibus_readreg;
272 mii->mii_writereg = et_miibus_writereg;
273 mii->mii_statchg = et_miibus_statchg;
274
275 sc->sc_ethercom.ec_mii = mii;
276 ifmedia_init(&mii->mii_media, 0, ether_mediachange,
277 ether_mediastatus);
278 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
279 if (LIST_FIRST(&mii->mii_phys) == NULL) {
280 aprint_error_dev(self, "no PHY found!\n");
281 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL,
282 0, NULL);
283 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
284 } else
285 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
286
287 if_attach(ifp);
288 if_deferred_start_init(ifp, NULL);
289 ether_ifattach(ifp, sc->sc_enaddr);
290
291 callout_init(&sc->sc_tick, 0);
292 callout_setfunc(&sc->sc_tick, et_tick, sc);
293 callout_init(&sc->sc_txtick, 0);
294 callout_setfunc(&sc->sc_txtick, et_txtick, sc);
295
296 if (pmf_device_register(self, NULL, NULL))
297 pmf_class_network_register(self, ifp);
298 else
299 aprint_error_dev(self, "couldn't establish power handler\n");
300
301 return;
302
303 fail:
304 et_dma_free(sc);
305 if (sc->sc_irq_handle != NULL) {
306 pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
307 sc->sc_irq_handle = NULL;
308 }
309 if (sc->sc_mem_size) {
310 bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
311 sc->sc_mem_size = 0;
312 }
313 }
314
315 int
316 et_detach(device_t self, int flags)
317 {
318 struct et_softc *sc = device_private(self);
319 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
320 int s;
321
322 pmf_device_deregister(self);
323 s = splnet();
324 et_stop(sc);
325 splx(s);
326
327 mii_detach(&sc->sc_miibus, MII_PHY_ANY, MII_OFFSET_ANY);
328
329 /* Delete all remaining media. */
330 ifmedia_delete_instance(&sc->sc_miibus.mii_media, IFM_INST_ANY);
331
332 ether_ifdetach(ifp);
333 if_detach(ifp);
334 et_dma_free(sc);
335
336 if (sc->sc_irq_handle != NULL) {
337 pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
338 sc->sc_irq_handle = NULL;
339 }
340
341 if (sc->sc_mem_size) {
342 bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
343 sc->sc_mem_size = 0;
344 }
345
346 return 0;
347 }
348
349 int
350 et_shutdown(device_t self)
351 {
352 struct et_softc *sc = device_private(self);
353 int s;
354
355 s = splnet();
356 et_stop(sc);
357 splx(s);
358
359 return 0;
360 }
361
362 int
363 et_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
364 {
365 struct et_softc *sc = device_private(dev);
366 uint32_t data;
367 int i, ret;
368
369 /* Stop any pending operations */
370 CSR_WRITE_4(sc, ET_MII_CMD, 0);
371
372 data = __SHIFTIN(phy, ET_MII_ADDR_PHY) |
373 __SHIFTIN(reg, ET_MII_ADDR_REG);
374 CSR_WRITE_4(sc, ET_MII_ADDR, data);
375
376 /* Start reading */
377 CSR_WRITE_4(sc, ET_MII_CMD, ET_MII_CMD_READ);
378
379 #define NRETRY 50
380
381 for (i = 0; i < NRETRY; ++i) {
382 data = CSR_READ_4(sc, ET_MII_IND);
383 if ((data & (ET_MII_IND_BUSY | ET_MII_IND_INVALID)) == 0)
384 break;
385 DELAY(50);
386 }
387 if (i == NRETRY) {
388 aprint_error_dev(sc->sc_dev, "read phy %d, reg %d timed out\n",
389 phy, reg);
390 ret = ETIMEDOUT;
391 goto back;
392 }
393
394 #undef NRETRY
395
396 data = CSR_READ_4(sc, ET_MII_STAT);
397 *val = __SHIFTOUT(data, ET_MII_STAT_VALUE);
398 ret = 0;
399
400 back:
401 /* Make sure that the current operation is stopped */
402 CSR_WRITE_4(sc, ET_MII_CMD, 0);
403 return ret;
404 }
405
406 int
407 et_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
408 {
409 struct et_softc *sc = device_private(dev);
410 uint32_t data;
411 uint16_t tmp;
412 int rv = 0;
413 int i;
414
415 /* Stop any pending operations */
416 CSR_WRITE_4(sc, ET_MII_CMD, 0);
417
418 data = __SHIFTIN(phy, ET_MII_ADDR_PHY) |
419 __SHIFTIN(reg, ET_MII_ADDR_REG);
420 CSR_WRITE_4(sc, ET_MII_ADDR, data);
421
422 /* Start writing */
423 CSR_WRITE_4(sc, ET_MII_CTRL, __SHIFTIN(val, ET_MII_CTRL_VALUE));
424
425 #define NRETRY 100
426
427 for (i = 0; i < NRETRY; ++i) {
428 data = CSR_READ_4(sc, ET_MII_IND);
429 if ((data & ET_MII_IND_BUSY) == 0)
430 break;
431 DELAY(50);
432 }
433 if (i == NRETRY) {
434 aprint_error_dev(sc->sc_dev, "write phy %d, reg %d timed out\n",
435 phy, reg);
436 et_miibus_readreg(dev, phy, reg, &tmp);
437 rv = ETIMEDOUT;
438 }
439
440 #undef NRETRY
441
442 /* Make sure that the current operation is stopped */
443 CSR_WRITE_4(sc, ET_MII_CMD, 0);
444
445 return rv;
446 }
447
448 void
449 et_miibus_statchg(struct ifnet *ifp)
450 {
451 struct et_softc *sc = ifp->if_softc;
452 struct mii_data *mii = &sc->sc_miibus;
453 uint32_t cfg2, ctrl;
454
455 cfg2 = CSR_READ_4(sc, ET_MAC_CFG2);
456 cfg2 &= ~(ET_MAC_CFG2_MODE_MII | ET_MAC_CFG2_MODE_GMII |
457 ET_MAC_CFG2_FDX | ET_MAC_CFG2_BIGFRM);
458 cfg2 |= ET_MAC_CFG2_LENCHK | ET_MAC_CFG2_CRC | ET_MAC_CFG2_PADCRC |
459 __SHIFTIN(7, ET_MAC_CFG2_PREAMBLE_LEN);
460
461 ctrl = CSR_READ_4(sc, ET_MAC_CTRL);
462 ctrl &= ~(ET_MAC_CTRL_GHDX | ET_MAC_CTRL_MODE_MII);
463
464 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
465 cfg2 |= ET_MAC_CFG2_MODE_GMII;
466 } else {
467 cfg2 |= ET_MAC_CFG2_MODE_MII;
468 ctrl |= ET_MAC_CTRL_MODE_MII;
469 }
470
471 if ((mii->mii_media_active & IFM_FDX) != 0)
472 cfg2 |= ET_MAC_CFG2_FDX;
473 else
474 ctrl |= ET_MAC_CTRL_GHDX;
475
476 CSR_WRITE_4(sc, ET_MAC_CTRL, ctrl);
477 CSR_WRITE_4(sc, ET_MAC_CFG2, cfg2);
478 }
479
480 void
481 et_stop(struct et_softc *sc)
482 {
483 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
484
485 callout_stop(&sc->sc_tick);
486 callout_stop(&sc->sc_txtick);
487
488 et_stop_rxdma(sc);
489 et_stop_txdma(sc);
490
491 et_disable_intrs(sc);
492
493 et_free_tx_ring(sc);
494 et_free_rx_ring(sc);
495
496 et_reset(sc);
497
498 sc->sc_tx = 0;
499 sc->sc_tx_intr = 0;
500
501 ifp->if_timer = 0;
502 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
503 }
504
505 int
506 et_bus_config(struct et_softc *sc)
507 {
508 uint32_t val; //, max_plsz;
509 // uint16_t ack_latency, replay_timer;
510
511 /*
512 * Test whether EEPROM is valid
513 * NOTE: Read twice to get the correct value
514 */
515 pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_EEPROM_MISC);
516 val = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_EEPROM_MISC);
517
518 if (val & ET_PCIM_EEPROM_STATUS_ERROR) {
519 aprint_error_dev(sc->sc_dev, "EEPROM status error 0x%02x\n", val);
520 return ENXIO;
521 }
522
523 /* TODO: LED */
524 #if 0
525 /*
526 * Configure ACK latency and replay timer according to
527 * max playload size
528 */
529 val = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_DEVICE_CAPS);
530 max_plsz = val & ET_PCIM_DEVICE_CAPS_MAX_PLSZ;
531
532 switch (max_plsz) {
533 case ET_PCIV_DEVICE_CAPS_PLSZ_128:
534 ack_latency = ET_PCIV_ACK_LATENCY_128;
535 replay_timer = ET_PCIV_REPLAY_TIMER_128;
536 break;
537
538 case ET_PCIV_DEVICE_CAPS_PLSZ_256:
539 ack_latency = ET_PCIV_ACK_LATENCY_256;
540 replay_timer = ET_PCIV_REPLAY_TIMER_256;
541 break;
542
543 default:
544 ack_latency = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
545 ET_PCIR_ACK_LATENCY) >> 16;
546 replay_timer = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
547 ET_PCIR_REPLAY_TIMER) >> 16;
548 aprint_normal_dev(sc->sc_dev, "ack latency %u, replay timer %u\n",
549 ack_latency, replay_timer);
550 break;
551 }
552 if (ack_latency != 0) {
553 pci_conf_write(sc->sc_pct, sc->sc_pcitag,
554 ET_PCIR_ACK_LATENCY, ack_latency << 16);
555 pci_conf_write(sc->sc_pct, sc->sc_pcitag,
556 ET_PCIR_REPLAY_TIMER, replay_timer << 16);
557 }
558
559 /*
560 * Set L0s and L1 latency timer to 2us
561 */
562 val = ET_PCIV_L0S_LATENCY(2) | ET_PCIV_L1_LATENCY(2);
563 pci_conf_write(sc->sc_pct, sc->sc_pcitag, ET_PCIR_L0S_L1_LATENCY,
564 val << 24);
565
566 /*
567 * Set max read request size to 2048 bytes
568 */
569 val = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
570 ET_PCIR_DEVICE_CTRL) >> 16;
571 val &= ~ET_PCIM_DEVICE_CTRL_MAX_RRSZ;
572 val |= ET_PCIV_DEVICE_CTRL_RRSZ_2K;
573 pci_conf_write(sc->sc_pct, sc->sc_pcitag, ET_PCIR_DEVICE_CTRL,
574 val << 16);
575 #endif
576
577 return 0;
578 }
579
580 void
581 et_get_eaddr(struct et_softc *sc, uint8_t eaddr[])
582 {
583 uint32_t r;
584
585 r = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_MACADDR_LO);
586 eaddr[0] = r & 0xff;
587 eaddr[1] = (r >> 8) & 0xff;
588 eaddr[2] = (r >> 16) & 0xff;
589 eaddr[3] = (r >> 24) & 0xff;
590 r = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_MACADDR_HI);
591 eaddr[4] = r & 0xff;
592 eaddr[5] = (r >> 8) & 0xff;
593 }
594
595 void
596 et_reset(struct et_softc *sc)
597 {
598 CSR_WRITE_4(sc, ET_MAC_CFG1,
599 ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
600 ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
601 ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
602
603 CSR_WRITE_4(sc, ET_SWRST,
604 ET_SWRST_TXDMA | ET_SWRST_RXDMA |
605 ET_SWRST_TXMAC | ET_SWRST_RXMAC |
606 ET_SWRST_MAC | ET_SWRST_MAC_STAT | ET_SWRST_MMC);
607
608 CSR_WRITE_4(sc, ET_MAC_CFG1,
609 ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
610 ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC);
611 CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
612 }
613
614 void
615 et_disable_intrs(struct et_softc *sc)
616 {
617 CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
618 }
619
620 void
621 et_enable_intrs(struct et_softc *sc, uint32_t intrs)
622 {
623 CSR_WRITE_4(sc, ET_INTR_MASK, ~intrs);
624 }
625
626 int
627 et_dma_alloc(struct et_softc *sc)
628 {
629 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
630 struct et_txstatus_data *txsd = &sc->sc_tx_status;
631 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
632 struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
633 int i, error;
634
635 /*
636 * Create TX ring DMA stuffs
637 */
638 error = et_dma_mem_create(sc, ET_TX_RING_SIZE,
639 (void **)&tx_ring->tr_desc, &tx_ring->tr_paddr, &tx_ring->tr_dmap,
640 &tx_ring->tr_seg);
641 if (error) {
642 aprint_error_dev(sc->sc_dev, "can't create TX ring DMA stuffs\n");
643 return error;
644 }
645
646 /*
647 * Create TX status DMA stuffs
648 */
649 error = et_dma_mem_create(sc, sizeof(uint32_t),
650 (void **)&txsd->txsd_status,
651 &txsd->txsd_paddr, &txsd->txsd_dmap, &txsd->txsd_seg);
652 if (error) {
653 aprint_error_dev(sc->sc_dev, "can't create TX status DMA stuffs\n");
654 return error;
655 }
656
657 /*
658 * Create DMA stuffs for RX rings
659 */
660 for (i = 0; i < ET_RX_NRING; ++i) {
661 static const uint32_t rx_ring_posreg[ET_RX_NRING] =
662 { ET_RX_RING0_POS, ET_RX_RING1_POS };
663
664 struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[i];
665
666 error = et_dma_mem_create(sc, ET_RX_RING_SIZE,
667 (void **)&rx_ring->rr_desc,
668 &rx_ring->rr_paddr, &rx_ring->rr_dmap, &rx_ring->rr_seg);
669 if (error) {
670 aprint_error_dev(sc->sc_dev, "can't create DMA stuffs for "
671 "the %d RX ring\n", i);
672 return error;
673 }
674 rx_ring->rr_posreg = rx_ring_posreg[i];
675 }
676
677 /*
678 * Create RX stat ring DMA stuffs
679 */
680 error = et_dma_mem_create(sc, ET_RXSTAT_RING_SIZE,
681 (void **)&rxst_ring->rsr_stat,
682 &rxst_ring->rsr_paddr, &rxst_ring->rsr_dmap, &rxst_ring->rsr_seg);
683 if (error) {
684 aprint_error_dev(sc->sc_dev, "can't create RX stat ring DMA stuffs\n");
685 return error;
686 }
687
688 /*
689 * Create RX status DMA stuffs
690 */
691 error = et_dma_mem_create(sc, sizeof(struct et_rxstatus),
692 (void **)&rxsd->rxsd_status,
693 &rxsd->rxsd_paddr, &rxsd->rxsd_dmap, &rxsd->rxsd_seg);
694 if (error) {
695 aprint_error_dev(sc->sc_dev, "can't create RX status DMA stuffs\n");
696 return error;
697 }
698
699 /*
700 * Create mbuf DMA stuffs
701 */
702 error = et_dma_mbuf_create(sc);
703 if (error)
704 return error;
705
706 return 0;
707 }
708
709 void
710 et_dma_free(struct et_softc *sc)
711 {
712 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
713 struct et_txstatus_data *txsd = &sc->sc_tx_status;
714 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
715 struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
716 int i, rx_done[ET_RX_NRING];
717
718 /*
719 * Destroy TX ring DMA stuffs
720 */
721 et_dma_mem_destroy(sc, tx_ring->tr_desc, tx_ring->tr_dmap);
722
723 /*
724 * Destroy TX status DMA stuffs
725 */
726 et_dma_mem_destroy(sc, txsd->txsd_status, txsd->txsd_dmap);
727
728 /*
729 * Destroy DMA stuffs for RX rings
730 */
731 for (i = 0; i < ET_RX_NRING; ++i) {
732 struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[i];
733
734 et_dma_mem_destroy(sc, rx_ring->rr_desc, rx_ring->rr_dmap);
735 }
736
737 /*
738 * Destroy RX stat ring DMA stuffs
739 */
740 et_dma_mem_destroy(sc, rxst_ring->rsr_stat, rxst_ring->rsr_dmap);
741
742 /*
743 * Destroy RX status DMA stuffs
744 */
745 et_dma_mem_destroy(sc, rxsd->rxsd_status, rxsd->rxsd_dmap);
746
747 /*
748 * Destroy mbuf DMA stuffs
749 */
750 for (i = 0; i < ET_RX_NRING; ++i)
751 rx_done[i] = ET_RX_NDESC;
752 et_dma_mbuf_destroy(sc, ET_TX_NDESC, rx_done);
753 }
754
755 int
756 et_dma_mbuf_create(struct et_softc *sc)
757 {
758 struct et_txbuf_data *tbd = &sc->sc_tx_data;
759 int i, error, rx_done[ET_RX_NRING];
760
761 /*
762 * Create spare DMA map for RX mbufs
763 */
764 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
765 BUS_DMA_NOWAIT, &sc->sc_mbuf_tmp_dmap);
766 if (error) {
767 aprint_error_dev(sc->sc_dev, "can't create spare mbuf DMA map\n");
768 return error;
769 }
770
771 /*
772 * Create DMA maps for RX mbufs
773 */
774 bzero(rx_done, sizeof(rx_done));
775 for (i = 0; i < ET_RX_NRING; ++i) {
776 struct et_rxbuf_data *rbd = &sc->sc_rx_data[i];
777 int j;
778
779 for (j = 0; j < ET_RX_NDESC; ++j) {
780 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
781 MCLBYTES, 0, BUS_DMA_NOWAIT,
782 &rbd->rbd_buf[j].rb_dmap);
783 if (error) {
784 aprint_error_dev(sc->sc_dev, "can't create %d RX mbuf "
785 "for %d RX ring\n", j, i);
786 rx_done[i] = j;
787 et_dma_mbuf_destroy(sc, 0, rx_done);
788 return error;
789 }
790 }
791 rx_done[i] = ET_RX_NDESC;
792
793 rbd->rbd_softc = sc;
794 rbd->rbd_ring = &sc->sc_rx_ring[i];
795 }
796
797 /*
798 * Create DMA maps for TX mbufs
799 */
800 for (i = 0; i < ET_TX_NDESC; ++i) {
801 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
802 0, BUS_DMA_NOWAIT, &tbd->tbd_buf[i].tb_dmap);
803 if (error) {
804 aprint_error_dev(sc->sc_dev, "can't create %d TX mbuf "
805 "DMA map\n", i);
806 et_dma_mbuf_destroy(sc, i, rx_done);
807 return error;
808 }
809 }
810
811 return 0;
812 }
813
814 void
815 et_dma_mbuf_destroy(struct et_softc *sc, int tx_done, const int rx_done[])
816 {
817 struct et_txbuf_data *tbd = &sc->sc_tx_data;
818 int i;
819
820 /*
821 * Destroy DMA maps for RX mbufs
822 */
823 for (i = 0; i < ET_RX_NRING; ++i) {
824 struct et_rxbuf_data *rbd = &sc->sc_rx_data[i];
825 int j;
826
827 for (j = 0; j < rx_done[i]; ++j) {
828 struct et_rxbuf *rb = &rbd->rbd_buf[j];
829
830 KASSERTMSG(rb->rb_mbuf == NULL,
831 "RX mbuf in %d RX ring is not freed yet\n", i);
832 bus_dmamap_destroy(sc->sc_dmat, rb->rb_dmap);
833 }
834 }
835
836 /*
837 * Destroy DMA maps for TX mbufs
838 */
839 for (i = 0; i < tx_done; ++i) {
840 struct et_txbuf *tb = &tbd->tbd_buf[i];
841
842 KASSERTMSG(tb->tb_mbuf == NULL, "TX mbuf is not freed yet\n");
843 bus_dmamap_destroy(sc->sc_dmat, tb->tb_dmap);
844 }
845
846 /*
847 * Destroy spare mbuf DMA map
848 */
849 bus_dmamap_destroy(sc->sc_dmat, sc->sc_mbuf_tmp_dmap);
850 }
851
852 int
853 et_dma_mem_create(struct et_softc *sc, bus_size_t size,
854 void **addr, bus_addr_t *paddr, bus_dmamap_t *dmap, bus_dma_segment_t *seg)
855 {
856 int error, nsegs;
857
858 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, BUS_DMA_NOWAIT,
859 dmap);
860 if (error) {
861 aprint_error_dev(sc->sc_dev, "can't create DMA map\n");
862 return error;
863 }
864
865 error = bus_dmamem_alloc(sc->sc_dmat, size, ET_ALIGN, 0, seg,
866 1, &nsegs, BUS_DMA_WAITOK);
867 if (error) {
868 aprint_error_dev(sc->sc_dev, "can't allocate DMA mem\n");
869 return error;
870 }
871
872 error = bus_dmamem_map(sc->sc_dmat, seg, nsegs,
873 size, (void **)addr, BUS_DMA_NOWAIT);
874 if (error) {
875 aprint_error_dev(sc->sc_dev, "can't map DMA mem\n");
876 return (error);
877 }
878
879 error = bus_dmamap_load(sc->sc_dmat, *dmap, *addr, size, NULL,
880 BUS_DMA_WAITOK);
881 if (error) {
882 aprint_error_dev(sc->sc_dev, "can't load DMA mem\n");
883 bus_dmamem_free(sc->sc_dmat, (bus_dma_segment_t *)addr, 1);
884 return error;
885 }
886
887 memset(*addr, 0, size);
888
889 *paddr = (*dmap)->dm_segs[0].ds_addr;
890
891 return 0;
892 }
893
894 void
895 et_dma_mem_destroy(struct et_softc *sc, void *addr, bus_dmamap_t dmap)
896 {
897 bus_dmamap_unload(sc->sc_dmat, dmap);
898 bus_dmamem_free(sc->sc_dmat, (bus_dma_segment_t *)&addr, 1);
899 }
900
901 void
902 et_chip_attach(struct et_softc *sc)
903 {
904 uint32_t val;
905
906 /*
907 * Perform minimal initialization
908 */
909
910 /* Disable loopback */
911 CSR_WRITE_4(sc, ET_LOOPBACK, 0);
912
913 /* Reset MAC */
914 CSR_WRITE_4(sc, ET_MAC_CFG1,
915 ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
916 ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
917 ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
918
919 /*
920 * Setup half duplex mode
921 */
922 val = __SHIFTIN(10, ET_MAC_HDX_ALT_BEB_TRUNC) |
923 __SHIFTIN(15, ET_MAC_HDX_REXMIT_MAX) |
924 __SHIFTIN(55, ET_MAC_HDX_COLLWIN) |
925 ET_MAC_HDX_EXC_DEFER;
926 CSR_WRITE_4(sc, ET_MAC_HDX, val);
927
928 /* Clear MAC control */
929 CSR_WRITE_4(sc, ET_MAC_CTRL, 0);
930
931 /* Reset MII */
932 CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST);
933
934 /* Bring MAC out of reset state */
935 CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
936
937 /* Enable memory controllers */
938 CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE);
939 }
940
941 int
942 et_intr(void *xsc)
943 {
944 struct et_softc *sc = xsc;
945 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
946 uint32_t intrs;
947
948 if ((ifp->if_flags & IFF_RUNNING) == 0)
949 return (0);
950
951 intrs = CSR_READ_4(sc, ET_INTR_STATUS);
952 if (intrs == 0 || intrs == 0xffffffff)
953 return (0);
954
955 et_disable_intrs(sc);
956 intrs &= ET_INTRS;
957 if (intrs == 0) /* Not interested */
958 goto back;
959
960 if (intrs & ET_INTR_RXEOF)
961 et_rxeof(sc);
962 if (intrs & (ET_INTR_TXEOF | ET_INTR_TIMER))
963 et_txeof(sc);
964 if (intrs & ET_INTR_TIMER)
965 CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer);
966 back:
967 et_enable_intrs(sc, ET_INTRS);
968
969 return (1);
970 }
971
972 int
973 et_init(struct ifnet *ifp)
974 {
975 struct et_softc *sc = ifp->if_softc;
976 int error, i, s;
977
978 if (ifp->if_flags & IFF_RUNNING)
979 return 0;
980
981 s = splnet();
982
983 et_stop(sc);
984
985 for (i = 0; i < ET_RX_NRING; ++i) {
986 sc->sc_rx_data[i].rbd_bufsize = et_bufsize[i].bufsize;
987 sc->sc_rx_data[i].rbd_newbuf = et_bufsize[i].newbuf;
988 }
989
990 error = et_init_tx_ring(sc);
991 if (error)
992 goto back;
993
994 error = et_init_rx_ring(sc);
995 if (error)
996 goto back;
997
998 error = et_chip_init(sc);
999 if (error)
1000 goto back;
1001
1002 error = et_enable_txrx(sc);
1003 if (error)
1004 goto back;
1005
1006 error = et_start_rxdma(sc);
1007 if (error)
1008 goto back;
1009
1010 error = et_start_txdma(sc);
1011 if (error)
1012 goto back;
1013
1014 et_enable_intrs(sc, ET_INTRS);
1015
1016 callout_schedule(&sc->sc_tick, hz);
1017
1018 CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer);
1019
1020 ifp->if_flags |= IFF_RUNNING;
1021 ifp->if_flags &= ~IFF_OACTIVE;
1022 back:
1023 if (error)
1024 et_stop(sc);
1025
1026 splx(s);
1027
1028 return (0);
1029 }
1030
1031 int
1032 et_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1033 {
1034 struct et_softc *sc = ifp->if_softc;
1035 int s, error = 0;
1036
1037 s = splnet();
1038
1039 switch (cmd) {
1040 case SIOCSIFFLAGS:
1041 if (ifp->if_flags & IFF_UP) {
1042 /*
1043 * If only the PROMISC or ALLMULTI flag changes, then
1044 * don't do a full re-init of the chip, just update
1045 * the Rx filter.
1046 */
1047 if ((ifp->if_flags & IFF_RUNNING) &&
1048 ((ifp->if_flags ^ sc->sc_if_flags) &
1049 (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
1050 et_setmulti(sc);
1051 } else {
1052 if (!(ifp->if_flags & IFF_RUNNING))
1053 et_init(ifp);
1054 }
1055 } else {
1056 if (ifp->if_flags & IFF_RUNNING)
1057 et_stop(sc);
1058 }
1059 sc->sc_if_flags = ifp->if_flags;
1060 break;
1061 default:
1062 error = ether_ioctl(ifp, cmd, data);
1063 if (error == ENETRESET) {
1064 if (ifp->if_flags & IFF_RUNNING)
1065 et_setmulti(sc);
1066 error = 0;
1067 }
1068 break;
1069
1070 }
1071
1072 splx(s);
1073
1074 return error;
1075 }
1076
1077 void
1078 et_start(struct ifnet *ifp)
1079 {
1080 struct et_softc *sc = ifp->if_softc;
1081 struct et_txbuf_data *tbd = &sc->sc_tx_data;
1082 int trans;
1083 struct mbuf *m;
1084
1085 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1086 return;
1087
1088 trans = 0;
1089 for (;;) {
1090 IFQ_DEQUEUE(&ifp->if_snd, m);
1091 if (m == NULL)
1092 break;
1093
1094 if ((tbd->tbd_used + ET_NSEG_SPARE) > ET_TX_NDESC) {
1095 ifp->if_flags |= IFF_OACTIVE;
1096 break;
1097 }
1098
1099 if (et_encap(sc, &m)) {
1100 ifp->if_oerrors++;
1101 ifp->if_flags |= IFF_OACTIVE;
1102 break;
1103 }
1104
1105 trans = 1;
1106
1107 bpf_mtap(ifp, m, BPF_D_OUT);
1108 }
1109
1110 if (trans) {
1111 callout_schedule(&sc->sc_txtick, hz);
1112 ifp->if_timer = 5;
1113 }
1114 }
1115
1116 void
1117 et_watchdog(struct ifnet *ifp)
1118 {
1119 struct et_softc *sc = ifp->if_softc;
1120 aprint_error_dev(sc->sc_dev, "watchdog timed out\n");
1121
1122 ifp->if_flags &= ~IFF_RUNNING;
1123 et_init(ifp);
1124 et_start(ifp);
1125 }
1126
1127 int
1128 et_stop_rxdma(struct et_softc *sc)
1129 {
1130 CSR_WRITE_4(sc, ET_RXDMA_CTRL,
1131 ET_RXDMA_CTRL_HALT | ET_RXDMA_CTRL_RING1_ENABLE);
1132
1133 DELAY(5);
1134 if ((CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) == 0) {
1135 aprint_error_dev(sc->sc_dev, "can't stop RX DMA engine\n");
1136 return ETIMEDOUT;
1137 }
1138 return 0;
1139 }
1140
1141 int
1142 et_stop_txdma(struct et_softc *sc)
1143 {
1144 CSR_WRITE_4(sc, ET_TXDMA_CTRL,
1145 ET_TXDMA_CTRL_HALT | ET_TXDMA_CTRL_SINGLE_EPKT);
1146 return 0;
1147 }
1148
1149 void
1150 et_free_tx_ring(struct et_softc *sc)
1151 {
1152 struct et_txbuf_data *tbd = &sc->sc_tx_data;
1153 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
1154 int i;
1155
1156 for (i = 0; i < ET_TX_NDESC; ++i) {
1157 struct et_txbuf *tb = &tbd->tbd_buf[i];
1158
1159 if (tb->tb_mbuf != NULL) {
1160 bus_dmamap_unload(sc->sc_dmat, tb->tb_dmap);
1161 m_freem(tb->tb_mbuf);
1162 tb->tb_mbuf = NULL;
1163 }
1164 }
1165
1166 bzero(tx_ring->tr_desc, ET_TX_RING_SIZE);
1167 bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0,
1168 tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1169 }
1170
1171 void
1172 et_free_rx_ring(struct et_softc *sc)
1173 {
1174 int n;
1175
1176 for (n = 0; n < ET_RX_NRING; ++n) {
1177 struct et_rxbuf_data *rbd = &sc->sc_rx_data[n];
1178 struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[n];
1179 int i;
1180
1181 for (i = 0; i < ET_RX_NDESC; ++i) {
1182 struct et_rxbuf *rb = &rbd->rbd_buf[i];
1183
1184 if (rb->rb_mbuf != NULL) {
1185 bus_dmamap_unload(sc->sc_dmat, rb->rb_dmap);
1186 m_freem(rb->rb_mbuf);
1187 rb->rb_mbuf = NULL;
1188 }
1189 }
1190
1191 bzero(rx_ring->rr_desc, ET_RX_RING_SIZE);
1192 bus_dmamap_sync(sc->sc_dmat, rx_ring->rr_dmap, 0,
1193 rx_ring->rr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1194 }
1195 }
1196
1197 void
1198 et_setmulti(struct et_softc *sc)
1199 {
1200 struct ethercom *ec = &sc->sc_ethercom;
1201 struct ifnet *ifp = &ec->ec_if;
1202 uint32_t hash[4] = { 0, 0, 0, 0 };
1203 uint32_t rxmac_ctrl, pktfilt;
1204 struct ether_multi *enm;
1205 struct ether_multistep step;
1206 uint8_t addr[ETHER_ADDR_LEN];
1207 int i, count;
1208
1209 pktfilt = CSR_READ_4(sc, ET_PKTFILT);
1210 rxmac_ctrl = CSR_READ_4(sc, ET_RXMAC_CTRL);
1211
1212 pktfilt &= ~(ET_PKTFILT_BCAST | ET_PKTFILT_MCAST | ET_PKTFILT_UCAST);
1213 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1214 rxmac_ctrl |= ET_RXMAC_CTRL_NO_PKTFILT;
1215 goto back;
1216 }
1217
1218 bcopy(etherbroadcastaddr, addr, ETHER_ADDR_LEN);
1219
1220 count = 0;
1221 ETHER_FIRST_MULTI(step, ec, enm);
1222 while (enm != NULL) {
1223 uint32_t *hp, h;
1224
1225 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1226 addr[i] &= enm->enm_addrlo[i];
1227 }
1228
1229 h = ether_crc32_be(addr, ETHER_ADDR_LEN);
1230 h = (h & 0x3f800000) >> 23;
1231
1232 hp = &hash[0];
1233 if (h >= 32 && h < 64) {
1234 h -= 32;
1235 hp = &hash[1];
1236 } else if (h >= 64 && h < 96) {
1237 h -= 64;
1238 hp = &hash[2];
1239 } else if (h >= 96) {
1240 h -= 96;
1241 hp = &hash[3];
1242 }
1243 *hp |= (1 << h);
1244
1245 ++count;
1246 ETHER_NEXT_MULTI(step, enm);
1247 }
1248
1249 for (i = 0; i < 4; ++i)
1250 CSR_WRITE_4(sc, ET_MULTI_HASH + (i * 4), hash[i]);
1251
1252 if (count > 0)
1253 pktfilt |= ET_PKTFILT_MCAST;
1254 rxmac_ctrl &= ~ET_RXMAC_CTRL_NO_PKTFILT;
1255 back:
1256 CSR_WRITE_4(sc, ET_PKTFILT, pktfilt);
1257 CSR_WRITE_4(sc, ET_RXMAC_CTRL, rxmac_ctrl);
1258 }
1259
1260 int
1261 et_chip_init(struct et_softc *sc)
1262 {
1263 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1264 uint32_t rxq_end;
1265 int error;
1266
1267 /*
1268 * Split internal memory between TX and RX according to MTU
1269 */
1270 if (ifp->if_mtu < 2048)
1271 rxq_end = 0x2bc;
1272 else if (ifp->if_mtu < 8192)
1273 rxq_end = 0x1ff;
1274 else
1275 rxq_end = 0x1b3;
1276 CSR_WRITE_4(sc, ET_RXQ_START, 0);
1277 CSR_WRITE_4(sc, ET_RXQ_END, rxq_end);
1278 CSR_WRITE_4(sc, ET_TXQ_START, rxq_end + 1);
1279 CSR_WRITE_4(sc, ET_TXQ_END, ET_INTERN_MEM_END);
1280
1281 /* No loopback */
1282 CSR_WRITE_4(sc, ET_LOOPBACK, 0);
1283
1284 /* Clear MSI configure */
1285 CSR_WRITE_4(sc, ET_MSI_CFG, 0);
1286
1287 /* Disable timer */
1288 CSR_WRITE_4(sc, ET_TIMER, 0);
1289
1290 /* Initialize MAC */
1291 et_init_mac(sc);
1292
1293 /* Enable memory controllers */
1294 CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE);
1295
1296 /* Initialize RX MAC */
1297 et_init_rxmac(sc);
1298
1299 /* Initialize TX MAC */
1300 et_init_txmac(sc);
1301
1302 /* Initialize RX DMA engine */
1303 error = et_init_rxdma(sc);
1304 if (error)
1305 return error;
1306
1307 /* Initialize TX DMA engine */
1308 error = et_init_txdma(sc);
1309 if (error)
1310 return error;
1311
1312 return 0;
1313 }
1314
1315 int
1316 et_init_tx_ring(struct et_softc *sc)
1317 {
1318 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
1319 struct et_txstatus_data *txsd = &sc->sc_tx_status;
1320 struct et_txbuf_data *tbd = &sc->sc_tx_data;
1321
1322 bzero(tx_ring->tr_desc, ET_TX_RING_SIZE);
1323 bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0,
1324 tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1325
1326 tbd->tbd_start_index = 0;
1327 tbd->tbd_start_wrap = 0;
1328 tbd->tbd_used = 0;
1329
1330 bzero(txsd->txsd_status, sizeof(uint32_t));
1331 bus_dmamap_sync(sc->sc_dmat, txsd->txsd_dmap, 0,
1332 txsd->txsd_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1333 return 0;
1334 }
1335
1336 int
1337 et_init_rx_ring(struct et_softc *sc)
1338 {
1339 struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
1340 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
1341 int n;
1342
1343 for (n = 0; n < ET_RX_NRING; ++n) {
1344 struct et_rxbuf_data *rbd = &sc->sc_rx_data[n];
1345 int i, error;
1346
1347 for (i = 0; i < ET_RX_NDESC; ++i) {
1348 error = rbd->rbd_newbuf(rbd, i, 1);
1349 if (error) {
1350 aprint_error_dev(sc->sc_dev, "%d ring %d buf, newbuf failed: "
1351 "%d\n", n, i, error);
1352 return error;
1353 }
1354 }
1355 }
1356
1357 bzero(rxsd->rxsd_status, sizeof(struct et_rxstatus));
1358 bus_dmamap_sync(sc->sc_dmat, rxsd->rxsd_dmap, 0,
1359 rxsd->rxsd_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1360
1361 bzero(rxst_ring->rsr_stat, ET_RXSTAT_RING_SIZE);
1362 bus_dmamap_sync(sc->sc_dmat, rxst_ring->rsr_dmap, 0,
1363 rxst_ring->rsr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1364
1365 return 0;
1366 }
1367
1368 int
1369 et_init_rxdma(struct et_softc *sc)
1370 {
1371 struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
1372 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
1373 struct et_rxdesc_ring *rx_ring;
1374 int error;
1375
1376 error = et_stop_rxdma(sc);
1377 if (error) {
1378 aprint_error_dev(sc->sc_dev, "can't init RX DMA engine\n");
1379 return error;
1380 }
1381
1382 /*
1383 * Install RX status
1384 */
1385 CSR_WRITE_4(sc, ET_RX_STATUS_HI, ET_ADDR_HI(rxsd->rxsd_paddr));
1386 CSR_WRITE_4(sc, ET_RX_STATUS_LO, ET_ADDR_LO(rxsd->rxsd_paddr));
1387
1388 /*
1389 * Install RX stat ring
1390 */
1391 CSR_WRITE_4(sc, ET_RXSTAT_HI, ET_ADDR_HI(rxst_ring->rsr_paddr));
1392 CSR_WRITE_4(sc, ET_RXSTAT_LO, ET_ADDR_LO(rxst_ring->rsr_paddr));
1393 CSR_WRITE_4(sc, ET_RXSTAT_CNT, ET_RX_NSTAT - 1);
1394 CSR_WRITE_4(sc, ET_RXSTAT_POS, 0);
1395 CSR_WRITE_4(sc, ET_RXSTAT_MINCNT, ((ET_RX_NSTAT * 15) / 100) - 1);
1396
1397 /* Match ET_RXSTAT_POS */
1398 rxst_ring->rsr_index = 0;
1399 rxst_ring->rsr_wrap = 0;
1400
1401 /*
1402 * Install the 2nd RX descriptor ring
1403 */
1404 rx_ring = &sc->sc_rx_ring[1];
1405 CSR_WRITE_4(sc, ET_RX_RING1_HI, ET_ADDR_HI(rx_ring->rr_paddr));
1406 CSR_WRITE_4(sc, ET_RX_RING1_LO, ET_ADDR_LO(rx_ring->rr_paddr));
1407 CSR_WRITE_4(sc, ET_RX_RING1_CNT, ET_RX_NDESC - 1);
1408 CSR_WRITE_4(sc, ET_RX_RING1_POS, ET_RX_RING1_POS_WRAP);
1409 CSR_WRITE_4(sc, ET_RX_RING1_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1);
1410
1411 /* Match ET_RX_RING1_POS */
1412 rx_ring->rr_index = 0;
1413 rx_ring->rr_wrap = 1;
1414
1415 /*
1416 * Install the 1st RX descriptor ring
1417 */
1418 rx_ring = &sc->sc_rx_ring[0];
1419 CSR_WRITE_4(sc, ET_RX_RING0_HI, ET_ADDR_HI(rx_ring->rr_paddr));
1420 CSR_WRITE_4(sc, ET_RX_RING0_LO, ET_ADDR_LO(rx_ring->rr_paddr));
1421 CSR_WRITE_4(sc, ET_RX_RING0_CNT, ET_RX_NDESC - 1);
1422 CSR_WRITE_4(sc, ET_RX_RING0_POS, ET_RX_RING0_POS_WRAP);
1423 CSR_WRITE_4(sc, ET_RX_RING0_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1);
1424
1425 /* Match ET_RX_RING0_POS */
1426 rx_ring->rr_index = 0;
1427 rx_ring->rr_wrap = 1;
1428
1429 /*
1430 * RX intr moderation
1431 */
1432 CSR_WRITE_4(sc, ET_RX_INTR_NPKTS, sc->sc_rx_intr_npkts);
1433 CSR_WRITE_4(sc, ET_RX_INTR_DELAY, sc->sc_rx_intr_delay);
1434
1435 return 0;
1436 }
1437
1438 int
1439 et_init_txdma(struct et_softc *sc)
1440 {
1441 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
1442 struct et_txstatus_data *txsd = &sc->sc_tx_status;
1443 int error;
1444
1445 error = et_stop_txdma(sc);
1446 if (error) {
1447 aprint_error_dev(sc->sc_dev, "can't init TX DMA engine\n");
1448 return error;
1449 }
1450
1451 /*
1452 * Install TX descriptor ring
1453 */
1454 CSR_WRITE_4(sc, ET_TX_RING_HI, ET_ADDR_HI(tx_ring->tr_paddr));
1455 CSR_WRITE_4(sc, ET_TX_RING_LO, ET_ADDR_LO(tx_ring->tr_paddr));
1456 CSR_WRITE_4(sc, ET_TX_RING_CNT, ET_TX_NDESC - 1);
1457
1458 /*
1459 * Install TX status
1460 */
1461 CSR_WRITE_4(sc, ET_TX_STATUS_HI, ET_ADDR_HI(txsd->txsd_paddr));
1462 CSR_WRITE_4(sc, ET_TX_STATUS_LO, ET_ADDR_LO(txsd->txsd_paddr));
1463
1464 CSR_WRITE_4(sc, ET_TX_READY_POS, 0);
1465
1466 /* Match ET_TX_READY_POS */
1467 tx_ring->tr_ready_index = 0;
1468 tx_ring->tr_ready_wrap = 0;
1469
1470 return 0;
1471 }
1472
1473 void
1474 et_init_mac(struct et_softc *sc)
1475 {
1476 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1477 const uint8_t *eaddr = CLLADDR(ifp->if_sadl);
1478 uint32_t val;
1479
1480 /* Reset MAC */
1481 CSR_WRITE_4(sc, ET_MAC_CFG1,
1482 ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
1483 ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
1484 ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
1485
1486 /*
1487 * Setup inter packet gap
1488 */
1489 val = __SHIFTIN(56, ET_IPG_NONB2B_1) |
1490 __SHIFTIN(88, ET_IPG_NONB2B_2) |
1491 __SHIFTIN(80, ET_IPG_MINIFG) |
1492 __SHIFTIN(96, ET_IPG_B2B);
1493 CSR_WRITE_4(sc, ET_IPG, val);
1494
1495 /*
1496 * Setup half duplex mode
1497 */
1498 val = __SHIFTIN(10, ET_MAC_HDX_ALT_BEB_TRUNC) |
1499 __SHIFTIN(15, ET_MAC_HDX_REXMIT_MAX) |
1500 __SHIFTIN(55, ET_MAC_HDX_COLLWIN) |
1501 ET_MAC_HDX_EXC_DEFER;
1502 CSR_WRITE_4(sc, ET_MAC_HDX, val);
1503
1504 /* Clear MAC control */
1505 CSR_WRITE_4(sc, ET_MAC_CTRL, 0);
1506
1507 /* Reset MII */
1508 CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST);
1509
1510 /*
1511 * Set MAC address
1512 */
1513 val = eaddr[2] | (eaddr[3] << 8) | (eaddr[4] << 16) | (eaddr[5] << 24);
1514 CSR_WRITE_4(sc, ET_MAC_ADDR1, val);
1515 val = (eaddr[0] << 16) | (eaddr[1] << 24);
1516 CSR_WRITE_4(sc, ET_MAC_ADDR2, val);
1517
1518 /* Set max frame length */
1519 CSR_WRITE_4(sc, ET_MAX_FRMLEN,
1520 ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ifp->if_mtu + ETHER_CRC_LEN);
1521
1522 /* Bring MAC out of reset state */
1523 CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
1524 }
1525
1526 void
1527 et_init_rxmac(struct et_softc *sc)
1528 {
1529 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1530 const uint8_t *eaddr = CLLADDR(ifp->if_sadl);
1531 uint32_t val;
1532 int i;
1533
1534 /* Disable RX MAC and WOL */
1535 CSR_WRITE_4(sc, ET_RXMAC_CTRL, ET_RXMAC_CTRL_WOL_DISABLE);
1536
1537 /*
1538 * Clear all WOL related registers
1539 */
1540 for (i = 0; i < 3; ++i)
1541 CSR_WRITE_4(sc, ET_WOL_CRC + (i * 4), 0);
1542 for (i = 0; i < 20; ++i)
1543 CSR_WRITE_4(sc, ET_WOL_MASK + (i * 4), 0);
1544
1545 /*
1546 * Set WOL source address. XXX is this necessary?
1547 */
1548 val = (eaddr[2] << 24) | (eaddr[3] << 16) | (eaddr[4] << 8) | eaddr[5];
1549 CSR_WRITE_4(sc, ET_WOL_SA_LO, val);
1550 val = (eaddr[0] << 8) | eaddr[1];
1551 CSR_WRITE_4(sc, ET_WOL_SA_HI, val);
1552
1553 /* Clear packet filters */
1554 CSR_WRITE_4(sc, ET_PKTFILT, 0);
1555
1556 /* No ucast filtering */
1557 CSR_WRITE_4(sc, ET_UCAST_FILTADDR1, 0);
1558 CSR_WRITE_4(sc, ET_UCAST_FILTADDR2, 0);
1559 CSR_WRITE_4(sc, ET_UCAST_FILTADDR3, 0);
1560
1561 if (ifp->if_mtu > 8192) {
1562 /*
1563 * In order to transmit jumbo packets greater than 8k,
1564 * the FIFO between RX MAC and RX DMA needs to be reduced
1565 * in size to (16k - MTU). In order to implement this, we
1566 * must use "cut through" mode in the RX MAC, which chops
1567 * packets down into segments which are (max_size * 16).
1568 * In this case we selected 256 bytes, since this is the
1569 * size of the PCI-Express TLP's that the 1310 uses.
1570 */
1571 val = __SHIFTIN(16, ET_RXMAC_MC_SEGSZ_MAX) |
1572 ET_RXMAC_MC_SEGSZ_ENABLE;
1573 } else {
1574 val = 0;
1575 }
1576 CSR_WRITE_4(sc, ET_RXMAC_MC_SEGSZ, val);
1577
1578 CSR_WRITE_4(sc, ET_RXMAC_MC_WATERMARK, 0);
1579
1580 /* Initialize RX MAC management register */
1581 CSR_WRITE_4(sc, ET_RXMAC_MGT, 0);
1582
1583 CSR_WRITE_4(sc, ET_RXMAC_SPACE_AVL, 0);
1584
1585 CSR_WRITE_4(sc, ET_RXMAC_MGT,
1586 ET_RXMAC_MGT_PASS_ECRC |
1587 ET_RXMAC_MGT_PASS_ELEN |
1588 ET_RXMAC_MGT_PASS_ETRUNC |
1589 ET_RXMAC_MGT_CHECK_PKT);
1590
1591 /*
1592 * Configure runt filtering (may not work on certain chip generation)
1593 */
1594 val = __SHIFTIN(ETHER_MIN_LEN, ET_PKTFILT_MINLEN) | ET_PKTFILT_FRAG;
1595 CSR_WRITE_4(sc, ET_PKTFILT, val);
1596
1597 /* Enable RX MAC but leave WOL disabled */
1598 CSR_WRITE_4(sc, ET_RXMAC_CTRL,
1599 ET_RXMAC_CTRL_WOL_DISABLE | ET_RXMAC_CTRL_ENABLE);
1600
1601 /*
1602 * Setup multicast hash and allmulti/promisc mode
1603 */
1604 et_setmulti(sc);
1605 }
1606
1607 void
1608 et_init_txmac(struct et_softc *sc)
1609 {
1610 /* Disable TX MAC and FC(?) */
1611 CSR_WRITE_4(sc, ET_TXMAC_CTRL, ET_TXMAC_CTRL_FC_DISABLE);
1612
1613 /* No flow control yet */
1614 CSR_WRITE_4(sc, ET_TXMAC_FLOWCTRL, 0);
1615
1616 /* Enable TX MAC but leave FC(?) diabled */
1617 CSR_WRITE_4(sc, ET_TXMAC_CTRL,
1618 ET_TXMAC_CTRL_ENABLE | ET_TXMAC_CTRL_FC_DISABLE);
1619 }
1620
1621 int
1622 et_start_rxdma(struct et_softc *sc)
1623 {
1624 uint32_t val = 0;
1625
1626 val |= __SHIFTIN(sc->sc_rx_data[0].rbd_bufsize,
1627 ET_RXDMA_CTRL_RING0_SIZE) |
1628 ET_RXDMA_CTRL_RING0_ENABLE;
1629 val |= __SHIFTIN(sc->sc_rx_data[1].rbd_bufsize,
1630 ET_RXDMA_CTRL_RING1_SIZE) |
1631 ET_RXDMA_CTRL_RING1_ENABLE;
1632
1633 CSR_WRITE_4(sc, ET_RXDMA_CTRL, val);
1634
1635 DELAY(5);
1636
1637 if (CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) {
1638 aprint_error_dev(sc->sc_dev, "can't start RX DMA engine\n");
1639 return ETIMEDOUT;
1640 }
1641 return 0;
1642 }
1643
1644 int
1645 et_start_txdma(struct et_softc *sc)
1646 {
1647 CSR_WRITE_4(sc, ET_TXDMA_CTRL, ET_TXDMA_CTRL_SINGLE_EPKT);
1648 return 0;
1649 }
1650
1651 int
1652 et_enable_txrx(struct et_softc *sc)
1653 {
1654 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1655 uint32_t val;
1656 int i, rc = 0;
1657
1658 val = CSR_READ_4(sc, ET_MAC_CFG1);
1659 val |= ET_MAC_CFG1_TXEN | ET_MAC_CFG1_RXEN;
1660 val &= ~(ET_MAC_CFG1_TXFLOW | ET_MAC_CFG1_RXFLOW |
1661 ET_MAC_CFG1_LOOPBACK);
1662 CSR_WRITE_4(sc, ET_MAC_CFG1, val);
1663
1664 if ((rc = ether_mediachange(ifp)) != 0)
1665 goto out;
1666
1667 #define NRETRY 100
1668
1669 for (i = 0; i < NRETRY; ++i) {
1670 val = CSR_READ_4(sc, ET_MAC_CFG1);
1671 if ((val & (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN)) ==
1672 (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN))
1673 break;
1674
1675 DELAY(10);
1676 }
1677 if (i == NRETRY) {
1678 aprint_error_dev(sc->sc_dev, "can't enable RX/TX\n");
1679 return ETIMEDOUT;
1680 }
1681
1682 #undef NRETRY
1683 return 0;
1684 out:
1685 return rc;
1686 }
1687
1688 void
1689 et_rxeof(struct et_softc *sc)
1690 {
1691 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1692 struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
1693 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
1694 uint32_t rxs_stat_ring;
1695 int rxst_wrap, rxst_index;
1696
1697 bus_dmamap_sync(sc->sc_dmat, rxsd->rxsd_dmap, 0,
1698 rxsd->rxsd_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1699 bus_dmamap_sync(sc->sc_dmat, rxst_ring->rsr_dmap, 0,
1700 rxst_ring->rsr_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1701
1702 rxs_stat_ring = rxsd->rxsd_status->rxs_stat_ring;
1703 rxst_wrap = (rxs_stat_ring & ET_RXS_STATRING_WRAP) ? 1 : 0;
1704 rxst_index = __SHIFTOUT(rxs_stat_ring, ET_RXS_STATRING_INDEX);
1705
1706 while (rxst_index != rxst_ring->rsr_index ||
1707 rxst_wrap != rxst_ring->rsr_wrap) {
1708 struct et_rxbuf_data *rbd;
1709 struct et_rxdesc_ring *rx_ring;
1710 struct et_rxstat *st;
1711 struct et_rxbuf *rb;
1712 struct mbuf *m;
1713 int buflen, buf_idx, ring_idx;
1714 uint32_t rxstat_pos, rxring_pos;
1715
1716 KASSERT(rxst_ring->rsr_index < ET_RX_NSTAT);
1717 st = &rxst_ring->rsr_stat[rxst_ring->rsr_index];
1718
1719 buflen = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_LEN);
1720 buf_idx = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_BUFIDX);
1721 ring_idx = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_RINGIDX);
1722
1723 if (++rxst_ring->rsr_index == ET_RX_NSTAT) {
1724 rxst_ring->rsr_index = 0;
1725 rxst_ring->rsr_wrap ^= 1;
1726 }
1727 rxstat_pos = __SHIFTIN(rxst_ring->rsr_index,
1728 ET_RXSTAT_POS_INDEX);
1729 if (rxst_ring->rsr_wrap)
1730 rxstat_pos |= ET_RXSTAT_POS_WRAP;
1731 CSR_WRITE_4(sc, ET_RXSTAT_POS, rxstat_pos);
1732
1733 if (ring_idx >= ET_RX_NRING) {
1734 ifp->if_ierrors++;
1735 aprint_error_dev(sc->sc_dev, "invalid ring index %d\n",
1736 ring_idx);
1737 continue;
1738 }
1739 if (buf_idx >= ET_RX_NDESC) {
1740 ifp->if_ierrors++;
1741 aprint_error_dev(sc->sc_dev, "invalid buf index %d\n",
1742 buf_idx);
1743 continue;
1744 }
1745
1746 rbd = &sc->sc_rx_data[ring_idx];
1747 rb = &rbd->rbd_buf[buf_idx];
1748 m = rb->rb_mbuf;
1749 bus_dmamap_sync(sc->sc_dmat, rb->rb_dmap, 0,
1750 rb->rb_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1751
1752 if (rbd->rbd_newbuf(rbd, buf_idx, 0) == 0) {
1753 if (buflen < ETHER_CRC_LEN) {
1754 m_freem(m);
1755 ifp->if_ierrors++;
1756 } else {
1757 m->m_pkthdr.len = m->m_len = buflen -
1758 ETHER_CRC_LEN;
1759 m_set_rcvif(m, ifp);
1760
1761 if_percpuq_enqueue(ifp->if_percpuq, m);
1762 }
1763 } else {
1764 ifp->if_ierrors++;
1765 }
1766
1767 rx_ring = &sc->sc_rx_ring[ring_idx];
1768
1769 if (buf_idx != rx_ring->rr_index) {
1770 aprint_error_dev(sc->sc_dev, "WARNING!! ring %d, "
1771 "buf_idx %d, rr_idx %d\n",
1772 ring_idx, buf_idx, rx_ring->rr_index);
1773 }
1774
1775 KASSERT(rx_ring->rr_index < ET_RX_NDESC);
1776 if (++rx_ring->rr_index == ET_RX_NDESC) {
1777 rx_ring->rr_index = 0;
1778 rx_ring->rr_wrap ^= 1;
1779 }
1780 rxring_pos = __SHIFTIN(rx_ring->rr_index, ET_RX_RING_POS_INDEX);
1781 if (rx_ring->rr_wrap)
1782 rxring_pos |= ET_RX_RING_POS_WRAP;
1783 CSR_WRITE_4(sc, rx_ring->rr_posreg, rxring_pos);
1784 }
1785 }
1786
1787 int
1788 et_encap(struct et_softc *sc, struct mbuf **m0)
1789 {
1790 struct mbuf *m = *m0;
1791 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
1792 struct et_txbuf_data *tbd = &sc->sc_tx_data;
1793 struct et_txdesc *td;
1794 bus_dmamap_t map;
1795 int error, maxsegs, first_idx, last_idx, i;
1796 uint32_t tx_ready_pos, last_td_ctrl2;
1797
1798 maxsegs = ET_TX_NDESC - tbd->tbd_used;
1799 if (maxsegs > ET_NSEG_MAX)
1800 maxsegs = ET_NSEG_MAX;
1801 KASSERTMSG(maxsegs >= ET_NSEG_SPARE,
1802 "not enough spare TX desc (%d)\n", maxsegs);
1803
1804 KASSERT(tx_ring->tr_ready_index < ET_TX_NDESC);
1805 first_idx = tx_ring->tr_ready_index;
1806 map = tbd->tbd_buf[first_idx].tb_dmap;
1807
1808 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
1809 BUS_DMA_NOWAIT);
1810 if (!error && map->dm_nsegs == 0) {
1811 bus_dmamap_unload(sc->sc_dmat, map);
1812 error = EFBIG;
1813 }
1814 if (error && error != EFBIG) {
1815 aprint_error_dev(sc->sc_dev, "can't load TX mbuf");
1816 goto back;
1817 }
1818 if (error) { /* error == EFBIG */
1819 struct mbuf *m_new;
1820
1821 error = 0;
1822
1823 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1824 if (m_new == NULL) {
1825 aprint_error_dev(sc->sc_dev, "can't defrag TX mbuf\n");
1826 error = ENOBUFS;
1827 goto back;
1828 }
1829
1830 m_copy_pkthdr(m_new, m);
1831 if (m->m_pkthdr.len > MHLEN) {
1832 MCLGET(m_new, M_DONTWAIT);
1833 if (!(m_new->m_flags & M_EXT)) {
1834 m_freem(m_new);
1835 error = ENOBUFS;
1836 }
1837 }
1838
1839 if (error) {
1840 aprint_error_dev(sc->sc_dev, "can't defrag TX buffer\n");
1841 goto back;
1842 }
1843
1844 m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, void *));
1845 m_freem(m);
1846 m_new->m_len = m_new->m_pkthdr.len;
1847 *m0 = m = m_new;
1848
1849 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
1850 BUS_DMA_NOWAIT);
1851 if (error || map->dm_nsegs == 0) {
1852 if (map->dm_nsegs == 0) {
1853 bus_dmamap_unload(sc->sc_dmat, map);
1854 error = EFBIG;
1855 }
1856 aprint_error_dev(sc->sc_dev, "can't load defraged TX mbuf\n");
1857 goto back;
1858 }
1859 }
1860
1861 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1862 BUS_DMASYNC_PREWRITE);
1863
1864 last_td_ctrl2 = ET_TDCTRL2_LAST_FRAG;
1865 sc->sc_tx += map->dm_nsegs;
1866 if (sc->sc_tx / sc->sc_tx_intr_nsegs != sc->sc_tx_intr) {
1867 sc->sc_tx_intr = sc->sc_tx / sc->sc_tx_intr_nsegs;
1868 last_td_ctrl2 |= ET_TDCTRL2_INTR;
1869 }
1870
1871 last_idx = -1;
1872 for (i = 0; i < map->dm_nsegs; ++i) {
1873 int idx;
1874
1875 idx = (first_idx + i) % ET_TX_NDESC;
1876 td = &tx_ring->tr_desc[idx];
1877 td->td_addr_hi = ET_ADDR_HI(map->dm_segs[i].ds_addr);
1878 td->td_addr_lo = ET_ADDR_LO(map->dm_segs[i].ds_addr);
1879 td->td_ctrl1 =
1880 __SHIFTIN(map->dm_segs[i].ds_len, ET_TDCTRL1_LEN);
1881
1882 if (i == map->dm_nsegs - 1) { /* Last frag */
1883 td->td_ctrl2 = last_td_ctrl2;
1884 last_idx = idx;
1885 }
1886
1887 KASSERT(tx_ring->tr_ready_index < ET_TX_NDESC);
1888 if (++tx_ring->tr_ready_index == ET_TX_NDESC) {
1889 tx_ring->tr_ready_index = 0;
1890 tx_ring->tr_ready_wrap ^= 1;
1891 }
1892 }
1893 td = &tx_ring->tr_desc[first_idx];
1894 td->td_ctrl2 |= ET_TDCTRL2_FIRST_FRAG; /* First frag */
1895
1896 KASSERT(last_idx >= 0);
1897 tbd->tbd_buf[first_idx].tb_dmap = tbd->tbd_buf[last_idx].tb_dmap;
1898 tbd->tbd_buf[last_idx].tb_dmap = map;
1899 tbd->tbd_buf[last_idx].tb_mbuf = m;
1900
1901 tbd->tbd_used += map->dm_nsegs;
1902 KASSERT(tbd->tbd_used <= ET_TX_NDESC);
1903
1904 bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0,
1905 tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1906
1907 tx_ready_pos = __SHIFTIN(tx_ring->tr_ready_index,
1908 ET_TX_READY_POS_INDEX);
1909 if (tx_ring->tr_ready_wrap)
1910 tx_ready_pos |= ET_TX_READY_POS_WRAP;
1911 CSR_WRITE_4(sc, ET_TX_READY_POS, tx_ready_pos);
1912
1913 error = 0;
1914 back:
1915 if (error) {
1916 m_freem(m);
1917 *m0 = NULL;
1918 }
1919 return error;
1920 }
1921
1922 void
1923 et_txeof(struct et_softc *sc)
1924 {
1925 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1926 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
1927 struct et_txbuf_data *tbd = &sc->sc_tx_data;
1928 uint32_t tx_done;
1929 int end, wrap;
1930
1931 if (tbd->tbd_used == 0)
1932 return;
1933
1934 tx_done = CSR_READ_4(sc, ET_TX_DONE_POS);
1935 end = __SHIFTOUT(tx_done, ET_TX_DONE_POS_INDEX);
1936 wrap = (tx_done & ET_TX_DONE_POS_WRAP) ? 1 : 0;
1937
1938 while (tbd->tbd_start_index != end || tbd->tbd_start_wrap != wrap) {
1939 struct et_txbuf *tb;
1940
1941 KASSERT(tbd->tbd_start_index < ET_TX_NDESC);
1942 tb = &tbd->tbd_buf[tbd->tbd_start_index];
1943
1944 bzero(&tx_ring->tr_desc[tbd->tbd_start_index],
1945 sizeof(struct et_txdesc));
1946 bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0,
1947 tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1948
1949 if (tb->tb_mbuf != NULL) {
1950 bus_dmamap_unload(sc->sc_dmat, tb->tb_dmap);
1951 m_freem(tb->tb_mbuf);
1952 tb->tb_mbuf = NULL;
1953 ifp->if_opackets++;
1954 }
1955
1956 if (++tbd->tbd_start_index == ET_TX_NDESC) {
1957 tbd->tbd_start_index = 0;
1958 tbd->tbd_start_wrap ^= 1;
1959 }
1960
1961 KASSERT(tbd->tbd_used > 0);
1962 tbd->tbd_used--;
1963 }
1964
1965 if (tbd->tbd_used == 0) {
1966 callout_stop(&sc->sc_txtick);
1967 ifp->if_timer = 0;
1968 }
1969 if (tbd->tbd_used + ET_NSEG_SPARE <= ET_TX_NDESC)
1970 ifp->if_flags &= ~IFF_OACTIVE;
1971
1972 if_schedule_deferred_start(ifp);
1973 }
1974
1975 void
1976 et_txtick(void *xsc)
1977 {
1978 struct et_softc *sc = xsc;
1979 int s;
1980
1981 s = splnet();
1982 et_txeof(sc);
1983 splx(s);
1984 }
1985
1986 void
1987 et_tick(void *xsc)
1988 {
1989 struct et_softc *sc = xsc;
1990 int s;
1991
1992 s = splnet();
1993 mii_tick(&sc->sc_miibus);
1994 callout_schedule(&sc->sc_tick, hz);
1995 splx(s);
1996 }
1997
1998 int
1999 et_newbuf_cluster(struct et_rxbuf_data *rbd, int buf_idx, int init)
2000 {
2001 return et_newbuf(rbd, buf_idx, init, MCLBYTES);
2002 }
2003
2004 int
2005 et_newbuf_hdr(struct et_rxbuf_data *rbd, int buf_idx, int init)
2006 {
2007 return et_newbuf(rbd, buf_idx, init, MHLEN);
2008 }
2009
2010 int
2011 et_newbuf(struct et_rxbuf_data *rbd, int buf_idx, int init, int len0)
2012 {
2013 struct et_softc *sc = rbd->rbd_softc;
2014 struct et_rxdesc_ring *rx_ring;
2015 struct et_rxdesc *desc;
2016 struct et_rxbuf *rb;
2017 struct mbuf *m;
2018 bus_dmamap_t dmap;
2019 int error, len;
2020
2021 KASSERT(buf_idx < ET_RX_NDESC);
2022 rb = &rbd->rbd_buf[buf_idx];
2023
2024 if (len0 >= MINCLSIZE) {
2025 MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA);
2026 if (m == NULL)
2027 return (ENOBUFS);
2028 MCLGET(m, init ? M_WAITOK : M_DONTWAIT);
2029 if ((m->m_flags & M_EXT) == 0) {
2030 m_freem(m);
2031 return (ENOBUFS);
2032 }
2033 len = MCLBYTES;
2034 } else {
2035 MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA);
2036 len = MHLEN;
2037 }
2038
2039 if (m == NULL) {
2040 error = ENOBUFS;
2041
2042 /* XXX for debug */
2043 aprint_error_dev(sc->sc_dev, "M_CLGET failed, size %d\n", len0);
2044 if (init) {
2045 return error;
2046 } else {
2047 goto back;
2048 }
2049 }
2050 m->m_len = m->m_pkthdr.len = len;
2051
2052 /*
2053 * Try load RX mbuf into temporary DMA tag
2054 */
2055 error = bus_dmamap_load_mbuf(sc->sc_dmat, sc->sc_mbuf_tmp_dmap, m,
2056 init ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT);
2057 if (error) {
2058 m_freem(m);
2059
2060 /* XXX for debug */
2061 aprint_error_dev(sc->sc_dev, "can't load RX mbuf\n");
2062 if (init) {
2063 return error;
2064 } else {
2065 goto back;
2066 }
2067 }
2068
2069 if (!init)
2070 bus_dmamap_unload(sc->sc_dmat, rb->rb_dmap);
2071 rb->rb_mbuf = m;
2072
2073 /*
2074 * Swap RX buf's DMA map with the loaded temporary one
2075 */
2076 dmap = rb->rb_dmap;
2077 rb->rb_dmap = sc->sc_mbuf_tmp_dmap;
2078 rb->rb_paddr = rb->rb_dmap->dm_segs[0].ds_addr;
2079 sc->sc_mbuf_tmp_dmap = dmap;
2080
2081 error = 0;
2082 back:
2083 rx_ring = rbd->rbd_ring;
2084 desc = &rx_ring->rr_desc[buf_idx];
2085
2086 desc->rd_addr_hi = ET_ADDR_HI(rb->rb_paddr);
2087 desc->rd_addr_lo = ET_ADDR_LO(rb->rb_paddr);
2088 desc->rd_ctrl = __SHIFTIN(buf_idx, ET_RDCTRL_BUFIDX);
2089
2090 bus_dmamap_sync(sc->sc_dmat, rx_ring->rr_dmap, 0,
2091 rx_ring->rr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
2092 return error;
2093 }
2094