if_dge.c revision 1.1 1 /* $NetBSD: if_dge.c,v 1.1 2004/03/12 13:46:52 ragge Exp $ */
2
3 /*
4 * Copyright (c) 2004, SUNET, Swedish University Computer Network.
5 * All rights reserved.
6 *
7 * Written by Anders Magnusson for SUNET, Swedish University Computer Network.
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 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * SUNET, Swedish University Computer Network.
21 * 4. The name of SUNET may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY SUNET ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 /*
38 * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
39 * All rights reserved.
40 *
41 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed for the NetBSD Project by
54 * Wasabi Systems, Inc.
55 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
56 * or promote products derived from this software without specific prior
57 * written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
70 */
71
72 /*
73 * Device driver for the Intel 82597EX Ten Gigabit Ethernet controller.
74 *
75 * TODO (in no specific order):
76 * HW VLAN support.
77 * Large jumbo buffers (16k).
78 * TSE offloading (needs kernel changes...)
79 * RAIDC (receive interrupt delay adaptation)
80 * Use memory > 4GB.
81 */
82
83 #include <sys/cdefs.h>
84 __KERNEL_RCSID(0, "$NetBSD: if_dge.c,v 1.1 2004/03/12 13:46:52 ragge Exp $");
85
86 #include "bpfilter.h"
87 #include "rnd.h"
88
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/callout.h>
92 #include <sys/mbuf.h>
93 #include <sys/malloc.h>
94 #include <sys/kernel.h>
95 #include <sys/socket.h>
96 #include <sys/ioctl.h>
97 #include <sys/errno.h>
98 #include <sys/device.h>
99 #include <sys/queue.h>
100
101 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
102
103 #if NRND > 0
104 #include <sys/rnd.h>
105 #endif
106
107 #include <net/if.h>
108 #include <net/if_dl.h>
109 #include <net/if_media.h>
110 #include <net/if_ether.h>
111
112 #if NBPFILTER > 0
113 #include <net/bpf.h>
114 #endif
115
116 #include <netinet/in.h> /* XXX for struct ip */
117 #include <netinet/in_systm.h> /* XXX for struct ip */
118 #include <netinet/ip.h> /* XXX for struct ip */
119 #include <netinet/tcp.h> /* XXX for struct tcphdr */
120
121 #include <machine/bus.h>
122 #include <machine/intr.h>
123 #include <machine/endian.h>
124
125 #include <dev/mii/mii.h>
126 #include <dev/mii/miivar.h>
127 #include <dev/mii/mii_bitbang.h>
128
129 #include <dev/pci/pcireg.h>
130 #include <dev/pci/pcivar.h>
131 #include <dev/pci/pcidevs.h>
132
133 #include <dev/pci/if_dgereg.h>
134
135 #define DGE_EVENT_COUNTERS
136 #define DGE_DEBUG
137
138 #ifdef DGE_DEBUG
139 #define DGE_DEBUG_LINK 0x01
140 #define DGE_DEBUG_TX 0x02
141 #define DGE_DEBUG_RX 0x04
142 #define DGE_DEBUG_CKSUM 0x08
143 int dge_debug = 0;
144
145 #define DPRINTF(x, y) if (dge_debug & (x)) printf y
146 #else
147 #define DPRINTF(x, y) /* nothing */
148 #endif /* DGE_DEBUG */
149
150 /*
151 * Transmit descriptor list size. We allow up to 100 DMA segments per
152 * packet (Intel reports of jumbo frame packets with as
153 * many as 80 DMA segments when using 16k buffers).
154 */
155 #define DGE_NTXSEGS 100
156 #define DGE_IFQUEUELEN 20000
157 #define DGE_TXQUEUELEN 2048
158 #define DGE_TXQUEUELEN_MASK (DGE_TXQUEUELEN - 1)
159 #define DGE_TXQUEUE_GC (DGE_TXQUEUELEN / 8)
160 #define DGE_NTXDESC 1024
161 #define DGE_NTXDESC_MASK (DGE_NTXDESC - 1)
162 #define DGE_NEXTTX(x) (((x) + 1) & DGE_NTXDESC_MASK)
163 #define DGE_NEXTTXS(x) (((x) + 1) & DGE_TXQUEUELEN_MASK)
164
165 /*
166 * Receive descriptor list size.
167 * Packet is of size MCLBYTES, and for jumbo packets buffers may
168 * be chained. Due to the nature of the card (high-speed), keep this
169 * ring large. With 2k buffers the ring can store 400 jumbo packets,
170 * which at full speed will be received in just under 3ms.
171 */
172 #define DGE_NRXDESC 2048
173 #define DGE_NRXDESC_MASK (DGE_NRXDESC - 1)
174 #define DGE_NEXTRX(x) (((x) + 1) & DGE_NRXDESC_MASK)
175 /*
176 * # of descriptors between head and written descriptors.
177 * This is to work-around two erratas.
178 */
179 #define DGE_RXSPACE 10
180 #define DGE_PREVRX(x) (((x) - DGE_RXSPACE) & DGE_NRXDESC_MASK)
181 /*
182 * Receive descriptor fetch threshholds. These are values recommended
183 * by Intel, do not touch them unless you know what you are doing.
184 */
185 #define RXDCTL_PTHRESH_VAL 128
186 #define RXDCTL_HTHRESH_VAL 16
187 #define RXDCTL_WTHRESH_VAL 16
188
189
190 /*
191 * Tweakable parameters; default values.
192 */
193 #define FCRTH 0x30000 /* Send XOFF water mark */
194 #define FCRTL 0x28000 /* Send XON water mark */
195 #define RDTR 0x20 /* Interrupt delay after receive, .8192us units */
196 #define TIDV 0x20 /* Interrupt delay after send, .8192us units */
197
198 /*
199 * Control structures are DMA'd to the i82597 chip. We allocate them in
200 * a single clump that maps to a single DMA segment to make serveral things
201 * easier.
202 */
203 struct dge_control_data {
204 /*
205 * The transmit descriptors.
206 */
207 struct dge_tdes wcd_txdescs[DGE_NTXDESC];
208
209 /*
210 * The receive descriptors.
211 */
212 struct dge_rdes wcd_rxdescs[DGE_NRXDESC];
213 };
214
215 #define DGE_CDOFF(x) offsetof(struct dge_control_data, x)
216 #define DGE_CDTXOFF(x) DGE_CDOFF(wcd_txdescs[(x)])
217 #define DGE_CDRXOFF(x) DGE_CDOFF(wcd_rxdescs[(x)])
218
219 /*
220 * Software state for transmit jobs.
221 */
222 struct dge_txsoft {
223 struct mbuf *txs_mbuf; /* head of our mbuf chain */
224 bus_dmamap_t txs_dmamap; /* our DMA map */
225 int txs_firstdesc; /* first descriptor in packet */
226 int txs_lastdesc; /* last descriptor in packet */
227 int txs_ndesc; /* # of descriptors used */
228 };
229
230 /*
231 * Software state for receive buffers. Each descriptor gets a
232 * 2k (MCLBYTES) buffer and a DMA map. For packets which fill
233 * more than one buffer, we chain them together.
234 */
235 struct dge_rxsoft {
236 struct mbuf *rxs_mbuf; /* head of our mbuf chain */
237 bus_dmamap_t rxs_dmamap; /* our DMA map */
238 };
239
240 /*
241 * Software state per device.
242 */
243 struct dge_softc {
244 struct device sc_dev; /* generic device information */
245 bus_space_tag_t sc_st; /* bus space tag */
246 bus_space_handle_t sc_sh; /* bus space handle */
247 bus_dma_tag_t sc_dmat; /* bus DMA tag */
248 struct ethercom sc_ethercom; /* ethernet common data */
249 void *sc_sdhook; /* shutdown hook */
250
251 int sc_flags; /* flags; see below */
252 int sc_bus_speed; /* PCI/PCIX bus speed */
253 int sc_pcix_offset; /* PCIX capability register offset */
254
255 pci_chipset_tag_t sc_pc;
256 pcitag_t sc_pt;
257 int sc_mmrbc; /* Max PCIX memory read byte count */
258
259 void *sc_ih; /* interrupt cookie */
260
261 struct ifmedia sc_media;
262
263 bus_dmamap_t sc_cddmamap; /* control data DMA map */
264 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
265
266 int sc_align_tweak;
267
268 /*
269 * Software state for the transmit and receive descriptors.
270 */
271 struct dge_txsoft sc_txsoft[DGE_TXQUEUELEN];
272 struct dge_rxsoft sc_rxsoft[DGE_NRXDESC];
273
274 /*
275 * Control data structures.
276 */
277 struct dge_control_data *sc_control_data;
278 #define sc_txdescs sc_control_data->wcd_txdescs
279 #define sc_rxdescs sc_control_data->wcd_rxdescs
280
281 #ifdef DGE_EVENT_COUNTERS
282 /* Event counters. */
283 struct evcnt sc_ev_txsstall; /* Tx stalled due to no txs */
284 struct evcnt sc_ev_txdstall; /* Tx stalled due to no txd */
285 struct evcnt sc_ev_txforceintr; /* Tx interrupts forced */
286 struct evcnt sc_ev_txdw; /* Tx descriptor interrupts */
287 struct evcnt sc_ev_txqe; /* Tx queue empty interrupts */
288 struct evcnt sc_ev_rxintr; /* Rx interrupts */
289 struct evcnt sc_ev_linkintr; /* Link interrupts */
290
291 struct evcnt sc_ev_rxipsum; /* IP checksums checked in-bound */
292 struct evcnt sc_ev_rxtusum; /* TCP/UDP cksums checked in-bound */
293 struct evcnt sc_ev_txipsum; /* IP checksums comp. out-bound */
294 struct evcnt sc_ev_txtusum; /* TCP/UDP cksums comp. out-bound */
295
296 struct evcnt sc_ev_txctx_init; /* Tx cksum context cache initialized */
297 struct evcnt sc_ev_txctx_hit; /* Tx cksum context cache hit */
298 struct evcnt sc_ev_txctx_miss; /* Tx cksum context cache miss */
299
300 struct evcnt sc_ev_txseg[DGE_NTXSEGS]; /* Tx packets w/ N segments */
301 struct evcnt sc_ev_txdrop; /* Tx packets dropped (too many segs) */
302 #endif /* DGE_EVENT_COUNTERS */
303
304 int sc_txfree; /* number of free Tx descriptors */
305 int sc_txnext; /* next ready Tx descriptor */
306
307 int sc_txsfree; /* number of free Tx jobs */
308 int sc_txsnext; /* next free Tx job */
309 int sc_txsdirty; /* dirty Tx jobs */
310
311 uint32_t sc_txctx_ipcs; /* cached Tx IP cksum ctx */
312 uint32_t sc_txctx_tucs; /* cached Tx TCP/UDP cksum ctx */
313
314 int sc_rxptr; /* next ready Rx descriptor/queue ent */
315 int sc_rxdiscard;
316 int sc_rxlen;
317 struct mbuf *sc_rxhead;
318 struct mbuf *sc_rxtail;
319 struct mbuf **sc_rxtailp;
320
321 uint32_t sc_ctrl0; /* prototype CTRL0 register */
322 uint32_t sc_icr; /* prototype interrupt bits */
323 uint32_t sc_tctl; /* prototype TCTL register */
324 uint32_t sc_rctl; /* prototype RCTL register */
325
326 int sc_mchash_type; /* multicast filter offset */
327
328 uint16_t sc_eeprom[EEPROM_SIZE];
329
330 #if NRND > 0
331 rndsource_element_t rnd_source; /* random source */
332 #endif
333 };
334
335 #define DGE_RXCHAIN_RESET(sc) \
336 do { \
337 (sc)->sc_rxtailp = &(sc)->sc_rxhead; \
338 *(sc)->sc_rxtailp = NULL; \
339 (sc)->sc_rxlen = 0; \
340 } while (/*CONSTCOND*/0)
341
342 #define DGE_RXCHAIN_LINK(sc, m) \
343 do { \
344 *(sc)->sc_rxtailp = (sc)->sc_rxtail = (m); \
345 (sc)->sc_rxtailp = &(m)->m_next; \
346 } while (/*CONSTCOND*/0)
347
348 /* sc_flags */
349 #define DGE_F_BUS64 0x20 /* bus is 64-bit */
350 #define DGE_F_PCIX 0x40 /* bus is PCI-X */
351
352 #ifdef DGE_EVENT_COUNTERS
353 #define DGE_EVCNT_INCR(ev) (ev)->ev_count++
354 #else
355 #define DGE_EVCNT_INCR(ev) /* nothing */
356 #endif
357
358 #define CSR_READ(sc, reg) \
359 bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (reg))
360 #define CSR_WRITE(sc, reg, val) \
361 bus_space_write_4((sc)->sc_st, (sc)->sc_sh, (reg), (val))
362
363 #define DGE_CDTXADDR(sc, x) ((sc)->sc_cddma + DGE_CDTXOFF((x)))
364 #define DGE_CDRXADDR(sc, x) ((sc)->sc_cddma + DGE_CDRXOFF((x)))
365
366 #define DGE_CDTXSYNC(sc, x, n, ops) \
367 do { \
368 int __x, __n; \
369 \
370 __x = (x); \
371 __n = (n); \
372 \
373 /* If it will wrap around, sync to the end of the ring. */ \
374 if ((__x + __n) > DGE_NTXDESC) { \
375 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
376 DGE_CDTXOFF(__x), sizeof(struct dge_tdes) * \
377 (DGE_NTXDESC - __x), (ops)); \
378 __n -= (DGE_NTXDESC - __x); \
379 __x = 0; \
380 } \
381 \
382 /* Now sync whatever is left. */ \
383 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
384 DGE_CDTXOFF(__x), sizeof(struct dge_tdes) * __n, (ops)); \
385 } while (/*CONSTCOND*/0)
386
387 #define DGE_CDRXSYNC(sc, x, ops) \
388 do { \
389 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
390 DGE_CDRXOFF((x)), sizeof(struct dge_rdes), (ops)); \
391 } while (/*CONSTCOND*/0)
392
393 #define DGE_INIT_RXDESC(sc, x) \
394 do { \
395 struct dge_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \
396 struct dge_rdes *__rxd = &(sc)->sc_rxdescs[(x)]; \
397 struct mbuf *__m = __rxs->rxs_mbuf; \
398 \
399 /* \
400 * Note: We scoot the packet forward 2 bytes in the buffer \
401 * so that the payload after the Ethernet header is aligned \
402 * to a 4-byte boundary. \
403 * \
404 * XXX BRAINDAMAGE ALERT! \
405 * The stupid chip uses the same size for every buffer, which \
406 * is set in the Receive Control register. We are using the 2K \
407 * size option, but what we REALLY want is (2K - 2)! For this \
408 * reason, we can't "scoot" packets longer than the standard \
409 * Ethernet MTU. On strict-alignment platforms, if the total \
410 * size exceeds (2K - 2) we set align_tweak to 0 and let \
411 * the upper layer copy the headers. \
412 */ \
413 __m->m_data = __m->m_ext.ext_buf + (sc)->sc_align_tweak; \
414 \
415 __rxd->dr_baddrl = \
416 htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + \
417 (sc)->sc_align_tweak); \
418 __rxd->dr_baddrh = 0; \
419 __rxd->dr_len = 0; \
420 __rxd->dr_cksum = 0; \
421 __rxd->dr_status = 0; \
422 __rxd->dr_errors = 0; \
423 __rxd->dr_special = 0; \
424 DGE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
425 \
426 CSR_WRITE((sc), DGE_RDT, (x)); \
427 } while (/*CONSTCOND*/0)
428
429 static void dge_start(struct ifnet *);
430 static void dge_watchdog(struct ifnet *);
431 static int dge_ioctl(struct ifnet *, u_long, caddr_t);
432 static int dge_init(struct ifnet *);
433 static void dge_stop(struct ifnet *, int);
434
435 static void dge_shutdown(void *);
436
437 static void dge_reset(struct dge_softc *);
438 static void dge_rxdrain(struct dge_softc *);
439 static int dge_add_rxbuf(struct dge_softc *, int);
440
441 static void dge_set_filter(struct dge_softc *);
442
443 static int dge_intr(void *);
444 static void dge_txintr(struct dge_softc *);
445 static void dge_rxintr(struct dge_softc *);
446 static void dge_linkintr(struct dge_softc *, uint32_t);
447
448 static int dge_match(struct device *, struct cfdata *, void *);
449 static void dge_attach(struct device *, struct device *, void *);
450
451 static int dge_read_eeprom(struct dge_softc *sc);
452 static int dge_eeprom_clockin(struct dge_softc *sc);
453 static void dge_eeprom_clockout(struct dge_softc *sc, int bit);
454 static uint16_t dge_eeprom_word(struct dge_softc *sc, int addr);
455 static int dge_xgmii_mediachange(struct ifnet *);
456 static void dge_xgmii_mediastatus(struct ifnet *, struct ifmediareq *);
457 static void dge_xgmii_reset(struct dge_softc *);
458 static void dge_xgmii_writereg(struct device *, int, int, int);
459
460
461 CFATTACH_DECL(dge, sizeof(struct dge_softc),
462 dge_match, dge_attach, NULL, NULL);
463
464 #ifdef DGE_EVENT_COUNTERS
465 #if DGE_NTXSEGS > 100
466 #error Update dge_txseg_evcnt_names
467 #endif
468 static char (*dge_txseg_evcnt_names)[DGE_NTXSEGS][8 /* "txseg00" + \0 */];
469 #endif /* DGE_EVENT_COUNTERS */
470
471 static int
472 dge_match(struct device *parent, struct cfdata *cf, void *aux)
473 {
474 struct pci_attach_args *pa = aux;
475
476 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL &&
477 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82597EX)
478 return (1);
479
480 return (0);
481 }
482
483 static void
484 dge_attach(struct device *parent, struct device *self, void *aux)
485 {
486 struct dge_softc *sc = (void *) self;
487 struct pci_attach_args *pa = aux;
488 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
489 pci_chipset_tag_t pc = pa->pa_pc;
490 pci_intr_handle_t ih;
491 const char *intrstr = NULL;
492 bus_dma_segment_t seg;
493 int i, rseg, error;
494 uint8_t enaddr[ETHER_ADDR_LEN];
495 pcireg_t preg, memtype;
496 uint32_t reg;
497
498 sc->sc_dmat = pa->pa_dmat;
499 sc->sc_pc = pa->pa_pc;
500 sc->sc_pt = pa->pa_tag;
501
502 preg = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
503 aprint_naive(": Ethernet controller\n");
504 aprint_normal(": Intel i82597EX 10GbE-LR Ethernet, rev. %d\n", preg);
505
506 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, DGE_PCI_BAR);
507 if (pci_mapreg_map(pa, DGE_PCI_BAR, memtype, 0,
508 &sc->sc_st, &sc->sc_sh, NULL, NULL)) {
509 aprint_error("%s: unable to map device registers\n",
510 sc->sc_dev.dv_xname);
511 return;
512 }
513
514 /* Enable bus mastering */
515 preg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
516 preg |= PCI_COMMAND_MASTER_ENABLE;
517 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, preg);
518
519 /*
520 * Map and establish our interrupt.
521 */
522 if (pci_intr_map(pa, &ih)) {
523 aprint_error("%s: unable to map interrupt\n",
524 sc->sc_dev.dv_xname);
525 return;
526 }
527 intrstr = pci_intr_string(pc, ih);
528 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dge_intr, sc);
529 if (sc->sc_ih == NULL) {
530 aprint_error("%s: unable to establish interrupt",
531 sc->sc_dev.dv_xname);
532 if (intrstr != NULL)
533 aprint_normal(" at %s", intrstr);
534 aprint_normal("\n");
535 return;
536 }
537 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
538
539 /*
540 * Determine a few things about the bus we're connected to.
541 */
542 reg = CSR_READ(sc, DGE_STATUS);
543 if (reg & STATUS_BUS64)
544 sc->sc_flags |= DGE_F_BUS64;
545
546 sc->sc_flags |= DGE_F_PCIX;
547 if (pci_get_capability(pa->pa_pc, pa->pa_tag,
548 PCI_CAP_PCIX,
549 &sc->sc_pcix_offset, NULL) == 0)
550 aprint_error("%s: unable to find PCIX "
551 "capability\n", sc->sc_dev.dv_xname);
552
553 if (sc->sc_flags & DGE_F_PCIX) {
554 switch (reg & STATUS_PCIX_MSK) {
555 case STATUS_PCIX_66:
556 sc->sc_bus_speed = 66;
557 break;
558 case STATUS_PCIX_100:
559 sc->sc_bus_speed = 100;
560 break;
561 case STATUS_PCIX_133:
562 sc->sc_bus_speed = 133;
563 break;
564 default:
565 aprint_error(
566 "%s: unknown PCIXSPD %d; assuming 66MHz\n",
567 sc->sc_dev.dv_xname,
568 reg & STATUS_PCIX_MSK);
569 sc->sc_bus_speed = 66;
570 }
571 } else
572 sc->sc_bus_speed = (reg & STATUS_BUS64) ? 66 : 33;
573 aprint_verbose("%s: %d-bit %dMHz %s bus\n", sc->sc_dev.dv_xname,
574 (sc->sc_flags & DGE_F_BUS64) ? 64 : 32, sc->sc_bus_speed,
575 (sc->sc_flags & DGE_F_PCIX) ? "PCIX" : "PCI");
576
577 /*
578 * Allocate the control data structures, and create and load the
579 * DMA map for it.
580 */
581 if ((error = bus_dmamem_alloc(sc->sc_dmat,
582 sizeof(struct dge_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
583 0)) != 0) {
584 aprint_error(
585 "%s: unable to allocate control data, error = %d\n",
586 sc->sc_dev.dv_xname, error);
587 goto fail_0;
588 }
589
590 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
591 sizeof(struct dge_control_data), (caddr_t *)&sc->sc_control_data,
592 0)) != 0) {
593 aprint_error("%s: unable to map control data, error = %d\n",
594 sc->sc_dev.dv_xname, error);
595 goto fail_1;
596 }
597
598 if ((error = bus_dmamap_create(sc->sc_dmat,
599 sizeof(struct dge_control_data), 1,
600 sizeof(struct dge_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
601 aprint_error("%s: unable to create control data DMA map, "
602 "error = %d\n", sc->sc_dev.dv_xname, error);
603 goto fail_2;
604 }
605
606 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
607 sc->sc_control_data, sizeof(struct dge_control_data), NULL,
608 0)) != 0) {
609 aprint_error(
610 "%s: unable to load control data DMA map, error = %d\n",
611 sc->sc_dev.dv_xname, error);
612 goto fail_3;
613 }
614
615 /*
616 * Create the transmit buffer DMA maps.
617 */
618 for (i = 0; i < DGE_TXQUEUELEN; i++) {
619 if ((error = bus_dmamap_create(sc->sc_dmat, ETHER_MAX_LEN_JUMBO,
620 DGE_NTXSEGS, MCLBYTES, 0, 0,
621 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
622 aprint_error("%s: unable to create Tx DMA map %d, "
623 "error = %d\n", sc->sc_dev.dv_xname, i, error);
624 goto fail_4;
625 }
626 }
627
628 /*
629 * Create the receive buffer DMA maps.
630 */
631 for (i = 0; i < DGE_NRXDESC; i++) {
632 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
633 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
634 aprint_error("%s: unable to create Rx DMA map %d, "
635 "error = %d\n", sc->sc_dev.dv_xname, i, error);
636 goto fail_5;
637 }
638 sc->sc_rxsoft[i].rxs_mbuf = NULL;
639 }
640
641 /*
642 * Set bits in ctrl0 register.
643 * Should get the software defined pins out of EEPROM?
644 */
645 sc->sc_ctrl0 |= CTRL0_RPE | CTRL0_TPE; /* XON/XOFF */
646 sc->sc_ctrl0 |= CTRL0_SDP3_DIR | CTRL0_SDP2_DIR | CTRL0_SDP1_DIR |
647 CTRL0_SDP0_DIR | CTRL0_SDP3 | CTRL0_SDP2 | CTRL0_SDP0;
648
649 /*
650 * Reset the chip to a known state.
651 */
652 dge_reset(sc);
653
654 /*
655 * Reset the PHY.
656 */
657 dge_xgmii_reset(sc);
658
659 /*
660 * Read in EEPROM data.
661 */
662 if (dge_read_eeprom(sc)) {
663 aprint_error("%s: couldn't read EEPROM\n", sc->sc_dev.dv_xname);
664 return;
665 }
666
667 /*
668 * Get the ethernet address.
669 */
670 enaddr[0] = sc->sc_eeprom[EE_ADDR01] & 0377;
671 enaddr[1] = sc->sc_eeprom[EE_ADDR01] >> 8;
672 enaddr[2] = sc->sc_eeprom[EE_ADDR23] & 0377;
673 enaddr[3] = sc->sc_eeprom[EE_ADDR23] >> 8;
674 enaddr[4] = sc->sc_eeprom[EE_ADDR45] & 0377;
675 enaddr[5] = sc->sc_eeprom[EE_ADDR45] >> 8;
676
677 aprint_normal("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
678 ether_sprintf(enaddr));
679
680 /*
681 * Setup media stuff.
682 */
683 ifmedia_init(&sc->sc_media, IFM_IMASK, dge_xgmii_mediachange,
684 dge_xgmii_mediastatus);
685 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10G_LR, 0, NULL);
686 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10G_LR);
687
688 ifp = &sc->sc_ethercom.ec_if;
689 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
690 ifp->if_softc = sc;
691 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
692 ifp->if_ioctl = dge_ioctl;
693 ifp->if_start = dge_start;
694 ifp->if_watchdog = dge_watchdog;
695 ifp->if_init = dge_init;
696 ifp->if_stop = dge_stop;
697 IFQ_SET_MAXLEN(&ifp->if_snd, max(DGE_IFQUEUELEN, IFQ_MAXLEN));
698 IFQ_SET_READY(&ifp->if_snd);
699
700 sc->sc_ethercom.ec_capabilities |=
701 ETHERCAP_JUMBO_MTU | ETHERCAP_VLAN_MTU;
702
703 /*
704 * We can perform TCPv4 and UDPv4 checkums in-bound.
705 */
706 ifp->if_capabilities |=
707 IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
708
709 /*
710 * Attach the interface.
711 */
712 if_attach(ifp);
713 ether_ifattach(ifp, enaddr);
714 #if NRND > 0
715 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
716 RND_TYPE_NET, 0);
717 #endif
718
719 #ifdef DGE_EVENT_COUNTERS
720 /* Fix segment event naming */
721 if (dge_txseg_evcnt_names == NULL) {
722 dge_txseg_evcnt_names =
723 malloc(sizeof(*dge_txseg_evcnt_names), M_DEVBUF, M_WAITOK);
724 for (i = 0; i < DGE_NTXSEGS; i++)
725 sprintf((*dge_txseg_evcnt_names)[i], "txseg%d", i);
726 }
727
728 /* Attach event counters. */
729 evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
730 NULL, sc->sc_dev.dv_xname, "txsstall");
731 evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
732 NULL, sc->sc_dev.dv_xname, "txdstall");
733 evcnt_attach_dynamic(&sc->sc_ev_txforceintr, EVCNT_TYPE_MISC,
734 NULL, sc->sc_dev.dv_xname, "txforceintr");
735 evcnt_attach_dynamic(&sc->sc_ev_txdw, EVCNT_TYPE_INTR,
736 NULL, sc->sc_dev.dv_xname, "txdw");
737 evcnt_attach_dynamic(&sc->sc_ev_txqe, EVCNT_TYPE_INTR,
738 NULL, sc->sc_dev.dv_xname, "txqe");
739 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
740 NULL, sc->sc_dev.dv_xname, "rxintr");
741 evcnt_attach_dynamic(&sc->sc_ev_linkintr, EVCNT_TYPE_INTR,
742 NULL, sc->sc_dev.dv_xname, "linkintr");
743
744 evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
745 NULL, sc->sc_dev.dv_xname, "rxipsum");
746 evcnt_attach_dynamic(&sc->sc_ev_rxtusum, EVCNT_TYPE_MISC,
747 NULL, sc->sc_dev.dv_xname, "rxtusum");
748 evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
749 NULL, sc->sc_dev.dv_xname, "txipsum");
750 evcnt_attach_dynamic(&sc->sc_ev_txtusum, EVCNT_TYPE_MISC,
751 NULL, sc->sc_dev.dv_xname, "txtusum");
752
753 evcnt_attach_dynamic(&sc->sc_ev_txctx_init, EVCNT_TYPE_MISC,
754 NULL, sc->sc_dev.dv_xname, "txctx init");
755 evcnt_attach_dynamic(&sc->sc_ev_txctx_hit, EVCNT_TYPE_MISC,
756 NULL, sc->sc_dev.dv_xname, "txctx hit");
757 evcnt_attach_dynamic(&sc->sc_ev_txctx_miss, EVCNT_TYPE_MISC,
758 NULL, sc->sc_dev.dv_xname, "txctx miss");
759
760 for (i = 0; i < DGE_NTXSEGS; i++)
761 evcnt_attach_dynamic(&sc->sc_ev_txseg[i], EVCNT_TYPE_MISC,
762 NULL, sc->sc_dev.dv_xname, (*dge_txseg_evcnt_names)[i]);
763
764 evcnt_attach_dynamic(&sc->sc_ev_txdrop, EVCNT_TYPE_MISC,
765 NULL, sc->sc_dev.dv_xname, "txdrop");
766
767 #endif /* DGE_EVENT_COUNTERS */
768
769 /*
770 * Make sure the interface is shutdown during reboot.
771 */
772 sc->sc_sdhook = shutdownhook_establish(dge_shutdown, sc);
773 if (sc->sc_sdhook == NULL)
774 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
775 sc->sc_dev.dv_xname);
776 return;
777
778 /*
779 * Free any resources we've allocated during the failed attach
780 * attempt. Do this in reverse order and fall through.
781 */
782 fail_5:
783 for (i = 0; i < DGE_NRXDESC; i++) {
784 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
785 bus_dmamap_destroy(sc->sc_dmat,
786 sc->sc_rxsoft[i].rxs_dmamap);
787 }
788 fail_4:
789 for (i = 0; i < DGE_TXQUEUELEN; i++) {
790 if (sc->sc_txsoft[i].txs_dmamap != NULL)
791 bus_dmamap_destroy(sc->sc_dmat,
792 sc->sc_txsoft[i].txs_dmamap);
793 }
794 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
795 fail_3:
796 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
797 fail_2:
798 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
799 sizeof(struct dge_control_data));
800 fail_1:
801 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
802 fail_0:
803 return;
804 }
805
806 /*
807 * dge_shutdown:
808 *
809 * Make sure the interface is stopped at reboot time.
810 */
811 static void
812 dge_shutdown(void *arg)
813 {
814 struct dge_softc *sc = arg;
815
816 dge_stop(&sc->sc_ethercom.ec_if, 1);
817 }
818
819 /*
820 * dge_tx_cksum:
821 *
822 * Set up TCP/IP checksumming parameters for the
823 * specified packet.
824 */
825 static int
826 dge_tx_cksum(struct dge_softc *sc, struct dge_txsoft *txs, uint8_t *fieldsp)
827 {
828 struct mbuf *m0 = txs->txs_mbuf;
829 struct dge_ctdes *t;
830 uint32_t ipcs, tucs;
831 struct ip *ip;
832 struct ether_header *eh;
833 int offset, iphl;
834 uint8_t fields = 0;
835
836 /*
837 * XXX It would be nice if the mbuf pkthdr had offset
838 * fields for the protocol headers.
839 */
840
841 eh = mtod(m0, struct ether_header *);
842 switch (htons(eh->ether_type)) {
843 case ETHERTYPE_IP:
844 iphl = sizeof(struct ip);
845 offset = ETHER_HDR_LEN;
846 break;
847
848 case ETHERTYPE_VLAN:
849 iphl = sizeof(struct ip);
850 offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
851 break;
852
853 default:
854 /*
855 * Don't support this protocol or encapsulation.
856 */
857 *fieldsp = 0;
858 return (0);
859 }
860
861 if (m0->m_len < (offset + iphl)) {
862 if ((txs->txs_mbuf = m_pullup(m0, offset + iphl)) == NULL) {
863 printf("%s: dge_tx_cksum: mbuf allocation failed, "
864 "packet dropped\n", sc->sc_dev.dv_xname);
865 return (ENOMEM);
866 }
867 m0 = txs->txs_mbuf;
868 }
869
870 ip = (struct ip *) (mtod(m0, caddr_t) + offset);
871 iphl = ip->ip_hl << 2;
872
873 /*
874 * NOTE: Even if we're not using the IP or TCP/UDP checksum
875 * offload feature, if we load the context descriptor, we
876 * MUST provide valid values for IPCSS and TUCSS fields.
877 */
878
879 if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
880 DGE_EVCNT_INCR(&sc->sc_ev_txipsum);
881 fields |= TDESC_POPTS_IXSM;
882 ipcs = DGE_TCPIP_IPCSS(offset) |
883 DGE_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
884 DGE_TCPIP_IPCSE(offset + iphl - 1);
885 } else if (__predict_true(sc->sc_txctx_ipcs != 0xffffffff)) {
886 /* Use the cached value. */
887 ipcs = sc->sc_txctx_ipcs;
888 } else {
889 /* Just initialize it to the likely value anyway. */
890 ipcs = DGE_TCPIP_IPCSS(offset) |
891 DGE_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
892 DGE_TCPIP_IPCSE(offset + iphl - 1);
893 }
894 DPRINTF(DGE_DEBUG_CKSUM,
895 ("%s: CKSUM: offset %d ipcs 0x%x\n",
896 sc->sc_dev.dv_xname, offset, ipcs));
897
898 offset += iphl;
899
900 if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
901 DGE_EVCNT_INCR(&sc->sc_ev_txtusum);
902 fields |= TDESC_POPTS_TXSM;
903 tucs = DGE_TCPIP_TUCSS(offset) |
904 DGE_TCPIP_TUCSO(offset + m0->m_pkthdr.csum_data) |
905 DGE_TCPIP_TUCSE(0) /* rest of packet */;
906 } else if (__predict_true(sc->sc_txctx_tucs != 0xffffffff)) {
907 /* Use the cached value. */
908 tucs = sc->sc_txctx_tucs;
909 } else {
910 /* Just initialize it to a valid TCP context. */
911 tucs = DGE_TCPIP_TUCSS(offset) |
912 DGE_TCPIP_TUCSO(offset + offsetof(struct tcphdr, th_sum)) |
913 DGE_TCPIP_TUCSE(0) /* rest of packet */;
914 }
915
916 DPRINTF(DGE_DEBUG_CKSUM,
917 ("%s: CKSUM: offset %d tucs 0x%x\n",
918 sc->sc_dev.dv_xname, offset, tucs));
919
920 if (sc->sc_txctx_ipcs == ipcs &&
921 sc->sc_txctx_tucs == tucs) {
922 /* Cached context is fine. */
923 DGE_EVCNT_INCR(&sc->sc_ev_txctx_hit);
924 } else {
925 /* Fill in the context descriptor. */
926 #ifdef DGE_EVENT_COUNTERS
927 if (sc->sc_txctx_ipcs == 0xffffffff &&
928 sc->sc_txctx_tucs == 0xffffffff)
929 DGE_EVCNT_INCR(&sc->sc_ev_txctx_init);
930 else
931 DGE_EVCNT_INCR(&sc->sc_ev_txctx_miss);
932 #endif
933 t = (struct dge_ctdes *)&sc->sc_txdescs[sc->sc_txnext];
934 t->dc_tcpip_ipcs = htole32(ipcs);
935 t->dc_tcpip_tucs = htole32(tucs);
936 t->dc_tcpip_cmdlen = htole32(TDESC_DTYP_CTD);
937 t->dc_tcpip_seg = 0;
938 DGE_CDTXSYNC(sc, sc->sc_txnext, 1, BUS_DMASYNC_PREWRITE);
939
940 sc->sc_txctx_ipcs = ipcs;
941 sc->sc_txctx_tucs = tucs;
942
943 sc->sc_txnext = DGE_NEXTTX(sc->sc_txnext);
944 txs->txs_ndesc++;
945 }
946
947 *fieldsp = fields;
948
949 return (0);
950 }
951
952 /*
953 * dge_start: [ifnet interface function]
954 *
955 * Start packet transmission on the interface.
956 */
957 static void
958 dge_start(struct ifnet *ifp)
959 {
960 struct dge_softc *sc = ifp->if_softc;
961 struct mbuf *m0;
962 struct dge_txsoft *txs;
963 bus_dmamap_t dmamap;
964 int error, nexttx, lasttx = -1, ofree, seg;
965 uint32_t cksumcmd;
966 uint8_t cksumfields;
967
968 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
969 return;
970
971 /*
972 * Remember the previous number of free descriptors.
973 */
974 ofree = sc->sc_txfree;
975
976 /*
977 * Loop through the send queue, setting up transmit descriptors
978 * until we drain the queue, or use up all available transmit
979 * descriptors.
980 */
981 for (;;) {
982 /* Grab a packet off the queue. */
983 IFQ_POLL(&ifp->if_snd, m0);
984 if (m0 == NULL)
985 break;
986
987 DPRINTF(DGE_DEBUG_TX,
988 ("%s: TX: have packet to transmit: %p\n",
989 sc->sc_dev.dv_xname, m0));
990
991 /* Get a work queue entry. */
992 if (sc->sc_txsfree < DGE_TXQUEUE_GC) {
993 dge_txintr(sc);
994 if (sc->sc_txsfree == 0) {
995 DPRINTF(DGE_DEBUG_TX,
996 ("%s: TX: no free job descriptors\n",
997 sc->sc_dev.dv_xname));
998 DGE_EVCNT_INCR(&sc->sc_ev_txsstall);
999 break;
1000 }
1001 }
1002
1003 txs = &sc->sc_txsoft[sc->sc_txsnext];
1004 dmamap = txs->txs_dmamap;
1005
1006 /*
1007 * Load the DMA map. If this fails, the packet either
1008 * didn't fit in the allotted number of segments, or we
1009 * were short on resources. For the too-many-segments
1010 * case, we simply report an error and drop the packet,
1011 * since we can't sanely copy a jumbo packet to a single
1012 * buffer.
1013 */
1014 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
1015 BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1016 if (error) {
1017 if (error == EFBIG) {
1018 DGE_EVCNT_INCR(&sc->sc_ev_txdrop);
1019 printf("%s: Tx packet consumes too many "
1020 "DMA segments, dropping...\n",
1021 sc->sc_dev.dv_xname);
1022 IFQ_DEQUEUE(&ifp->if_snd, m0);
1023 m_freem(m0);
1024 continue;
1025 }
1026 /*
1027 * Short on resources, just stop for now.
1028 */
1029 DPRINTF(DGE_DEBUG_TX,
1030 ("%s: TX: dmamap load failed: %d\n",
1031 sc->sc_dev.dv_xname, error));
1032 break;
1033 }
1034
1035 /*
1036 * Ensure we have enough descriptors free to describe
1037 * the packet. Note, we always reserve one descriptor
1038 * at the end of the ring due to the semantics of the
1039 * TDT register, plus one more in the event we need
1040 * to re-load checksum offload context.
1041 */
1042 if (dmamap->dm_nsegs > (sc->sc_txfree - 2)) {
1043 /*
1044 * Not enough free descriptors to transmit this
1045 * packet. We haven't committed anything yet,
1046 * so just unload the DMA map, put the packet
1047 * pack on the queue, and punt. Notify the upper
1048 * layer that there are no more slots left.
1049 */
1050 DPRINTF(DGE_DEBUG_TX,
1051 ("%s: TX: need %d descriptors, have %d\n",
1052 sc->sc_dev.dv_xname, dmamap->dm_nsegs,
1053 sc->sc_txfree - 1));
1054 ifp->if_flags |= IFF_OACTIVE;
1055 bus_dmamap_unload(sc->sc_dmat, dmamap);
1056 DGE_EVCNT_INCR(&sc->sc_ev_txdstall);
1057 break;
1058 }
1059
1060 IFQ_DEQUEUE(&ifp->if_snd, m0);
1061
1062 /*
1063 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1064 */
1065
1066 /* Sync the DMA map. */
1067 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
1068 BUS_DMASYNC_PREWRITE);
1069
1070 DPRINTF(DGE_DEBUG_TX,
1071 ("%s: TX: packet has %d DMA segments\n",
1072 sc->sc_dev.dv_xname, dmamap->dm_nsegs));
1073
1074 DGE_EVCNT_INCR(&sc->sc_ev_txseg[dmamap->dm_nsegs - 1]);
1075
1076 /*
1077 * Store a pointer to the packet so that we can free it
1078 * later.
1079 *
1080 * Initially, we consider the number of descriptors the
1081 * packet uses the number of DMA segments. This may be
1082 * incremented by 1 if we do checksum offload (a descriptor
1083 * is used to set the checksum context).
1084 */
1085 txs->txs_mbuf = m0;
1086 txs->txs_firstdesc = sc->sc_txnext;
1087 txs->txs_ndesc = dmamap->dm_nsegs;
1088
1089 /*
1090 * Set up checksum offload parameters for
1091 * this packet.
1092 */
1093 if (m0->m_pkthdr.csum_flags &
1094 (M_CSUM_IPv4|M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1095 if (dge_tx_cksum(sc, txs, &cksumfields) != 0) {
1096 /* Error message already displayed. */
1097 bus_dmamap_unload(sc->sc_dmat, dmamap);
1098 continue;
1099 }
1100 } else {
1101 cksumfields = 0;
1102 }
1103
1104 cksumcmd = TDESC_DCMD_IDE | TDESC_DTYP_DATA;
1105
1106 /*
1107 * Initialize the transmit descriptor.
1108 */
1109 for (nexttx = sc->sc_txnext, seg = 0;
1110 seg < dmamap->dm_nsegs;
1111 seg++, nexttx = DGE_NEXTTX(nexttx)) {
1112 /*
1113 * Note: we currently only use 32-bit DMA
1114 * addresses.
1115 */
1116 sc->sc_txdescs[nexttx].dt_baddrh = 0;
1117 sc->sc_txdescs[nexttx].dt_baddrl =
1118 htole32(dmamap->dm_segs[seg].ds_addr);
1119 sc->sc_txdescs[nexttx].dt_ctl =
1120 htole32(cksumcmd | dmamap->dm_segs[seg].ds_len);
1121 sc->sc_txdescs[nexttx].dt_status = 0;
1122 sc->sc_txdescs[nexttx].dt_popts = cksumfields;
1123 sc->sc_txdescs[nexttx].dt_vlan = 0;
1124 lasttx = nexttx;
1125
1126 DPRINTF(DGE_DEBUG_TX,
1127 ("%s: TX: desc %d: low 0x%08lx, len 0x%04lx\n",
1128 sc->sc_dev.dv_xname, nexttx,
1129 le32toh(dmamap->dm_segs[seg].ds_addr),
1130 le32toh(dmamap->dm_segs[seg].ds_len)));
1131 }
1132
1133 KASSERT(lasttx != -1);
1134
1135 /*
1136 * Set up the command byte on the last descriptor of
1137 * the packet. If we're in the interrupt delay window,
1138 * delay the interrupt.
1139 */
1140 sc->sc_txdescs[lasttx].dt_ctl |=
1141 htole32(TDESC_DCMD_EOP | TDESC_DCMD_RS);
1142
1143 txs->txs_lastdesc = lasttx;
1144
1145 DPRINTF(DGE_DEBUG_TX,
1146 ("%s: TX: desc %d: cmdlen 0x%08x\n", sc->sc_dev.dv_xname,
1147 lasttx, le32toh(sc->sc_txdescs[lasttx].dt_ctl)));
1148
1149 /* Sync the descriptors we're using. */
1150 DGE_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
1151 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1152
1153 /* Give the packet to the chip. */
1154 CSR_WRITE(sc, DGE_TDT, nexttx);
1155
1156 DPRINTF(DGE_DEBUG_TX,
1157 ("%s: TX: TDT -> %d\n", sc->sc_dev.dv_xname, nexttx));
1158
1159 DPRINTF(DGE_DEBUG_TX,
1160 ("%s: TX: finished transmitting packet, job %d\n",
1161 sc->sc_dev.dv_xname, sc->sc_txsnext));
1162
1163 /* Advance the tx pointer. */
1164 sc->sc_txfree -= txs->txs_ndesc;
1165 sc->sc_txnext = nexttx;
1166
1167 sc->sc_txsfree--;
1168 sc->sc_txsnext = DGE_NEXTTXS(sc->sc_txsnext);
1169
1170 #if NBPFILTER > 0
1171 /* Pass the packet to any BPF listeners. */
1172 if (ifp->if_bpf)
1173 bpf_mtap(ifp->if_bpf, m0);
1174 #endif /* NBPFILTER > 0 */
1175 }
1176
1177 if (sc->sc_txsfree == 0 || sc->sc_txfree <= 2) {
1178 /* No more slots; notify upper layer. */
1179 ifp->if_flags |= IFF_OACTIVE;
1180 }
1181
1182 if (sc->sc_txfree != ofree) {
1183 /* Set a watchdog timer in case the chip flakes out. */
1184 ifp->if_timer = 5;
1185 }
1186 }
1187
1188 /*
1189 * dge_watchdog: [ifnet interface function]
1190 *
1191 * Watchdog timer handler.
1192 */
1193 static void
1194 dge_watchdog(struct ifnet *ifp)
1195 {
1196 struct dge_softc *sc = ifp->if_softc;
1197
1198 /*
1199 * Since we're using delayed interrupts, sweep up
1200 * before we report an error.
1201 */
1202 dge_txintr(sc);
1203
1204 if (sc->sc_txfree != DGE_NTXDESC) {
1205 printf("%s: device timeout (txfree %d txsfree %d txnext %d)\n",
1206 sc->sc_dev.dv_xname, sc->sc_txfree, sc->sc_txsfree,
1207 sc->sc_txnext);
1208 ifp->if_oerrors++;
1209
1210 /* Reset the interface. */
1211 (void) dge_init(ifp);
1212 }
1213
1214 /* Try to get more packets going. */
1215 dge_start(ifp);
1216 }
1217
1218 /*
1219 * dge_ioctl: [ifnet interface function]
1220 *
1221 * Handle control requests from the operator.
1222 */
1223 static int
1224 dge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1225 {
1226 struct dge_softc *sc = ifp->if_softc;
1227 struct ifreq *ifr = (struct ifreq *) data;
1228 pcireg_t preg;
1229 int s, error, mmrbc;
1230
1231 s = splnet();
1232
1233 switch (cmd) {
1234 case SIOCSIFMEDIA:
1235 case SIOCGIFMEDIA:
1236 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1237 break;
1238
1239 case SIOCSIFFLAGS:
1240 /* extract link flags */
1241 if ((ifp->if_flags & IFF_LINK0) == 0 &&
1242 (ifp->if_flags & IFF_LINK1) == 0)
1243 mmrbc = PCIX_MMRBC_512;
1244 else if ((ifp->if_flags & IFF_LINK0) == 0 &&
1245 (ifp->if_flags & IFF_LINK1) != 0)
1246 mmrbc = PCIX_MMRBC_1024;
1247 else if ((ifp->if_flags & IFF_LINK0) != 0 &&
1248 (ifp->if_flags & IFF_LINK1) == 0)
1249 mmrbc = PCIX_MMRBC_2048;
1250 else
1251 mmrbc = PCIX_MMRBC_4096;
1252 if (mmrbc != sc->sc_mmrbc) {
1253 preg = pci_conf_read(sc->sc_pc, sc->sc_pt,DGE_PCIX_CMD);
1254 preg &= ~PCIX_MMRBC_MSK;
1255 preg |= mmrbc;
1256 pci_conf_write(sc->sc_pc, sc->sc_pt,DGE_PCIX_CMD, preg);
1257 sc->sc_mmrbc = mmrbc;
1258 }
1259 /* FALLTHROUGH */
1260 default:
1261 error = ether_ioctl(ifp, cmd, data);
1262 if (error == ENETRESET) {
1263 /*
1264 * Multicast list has changed; set the hardware filter
1265 * accordingly.
1266 */
1267 dge_set_filter(sc);
1268 error = 0;
1269 }
1270 break;
1271 }
1272
1273 /* Try to get more packets going. */
1274 dge_start(ifp);
1275
1276 splx(s);
1277 return (error);
1278 }
1279
1280 /*
1281 * dge_intr:
1282 *
1283 * Interrupt service routine.
1284 */
1285 static int
1286 dge_intr(void *arg)
1287 {
1288 struct dge_softc *sc = arg;
1289 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1290 uint32_t icr;
1291 int wantinit, handled = 0;
1292
1293 for (wantinit = 0; wantinit == 0;) {
1294 icr = CSR_READ(sc, DGE_ICR);
1295 if ((icr & sc->sc_icr) == 0)
1296 break;
1297
1298 #if 0 /*NRND > 0*/
1299 if (RND_ENABLED(&sc->rnd_source))
1300 rnd_add_uint32(&sc->rnd_source, icr);
1301 #endif
1302
1303 handled = 1;
1304
1305 #if defined(DGE_DEBUG) || defined(DGE_EVENT_COUNTERS)
1306 if (icr & (ICR_RXDMT0|ICR_RXT0)) {
1307 DPRINTF(DGE_DEBUG_RX,
1308 ("%s: RX: got Rx intr 0x%08x\n",
1309 sc->sc_dev.dv_xname,
1310 icr & (ICR_RXDMT0|ICR_RXT0)));
1311 DGE_EVCNT_INCR(&sc->sc_ev_rxintr);
1312 }
1313 #endif
1314 dge_rxintr(sc);
1315
1316 #if defined(DGE_DEBUG) || defined(DGE_EVENT_COUNTERS)
1317 if (icr & ICR_TXDW) {
1318 DPRINTF(DGE_DEBUG_TX,
1319 ("%s: TX: got TXDW interrupt\n",
1320 sc->sc_dev.dv_xname));
1321 DGE_EVCNT_INCR(&sc->sc_ev_txdw);
1322 }
1323 if (icr & ICR_TXQE)
1324 DGE_EVCNT_INCR(&sc->sc_ev_txqe);
1325 #endif
1326 dge_txintr(sc);
1327
1328 if (icr & (ICR_LSC|ICR_RXSEQ)) {
1329 DGE_EVCNT_INCR(&sc->sc_ev_linkintr);
1330 dge_linkintr(sc, icr);
1331 }
1332
1333 if (icr & ICR_RXO) {
1334 printf("%s: Receive overrun\n", sc->sc_dev.dv_xname);
1335 wantinit = 1;
1336 }
1337 }
1338
1339 if (handled) {
1340 if (wantinit)
1341 dge_init(ifp);
1342
1343 /* Try to get more packets going. */
1344 dge_start(ifp);
1345 }
1346
1347 return (handled);
1348 }
1349
1350 /*
1351 * dge_txintr:
1352 *
1353 * Helper; handle transmit interrupts.
1354 */
1355 static void
1356 dge_txintr(struct dge_softc *sc)
1357 {
1358 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1359 struct dge_txsoft *txs;
1360 uint8_t status;
1361 int i;
1362
1363 ifp->if_flags &= ~IFF_OACTIVE;
1364
1365 /*
1366 * Go through the Tx list and free mbufs for those
1367 * frames which have been transmitted.
1368 */
1369 for (i = sc->sc_txsdirty; sc->sc_txsfree != DGE_TXQUEUELEN;
1370 i = DGE_NEXTTXS(i), sc->sc_txsfree++) {
1371 txs = &sc->sc_txsoft[i];
1372
1373 DPRINTF(DGE_DEBUG_TX,
1374 ("%s: TX: checking job %d\n", sc->sc_dev.dv_xname, i));
1375
1376 DGE_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
1377 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1378
1379 status =
1380 sc->sc_txdescs[txs->txs_lastdesc].dt_status;
1381 if ((status & TDESC_STA_DD) == 0) {
1382 DGE_CDTXSYNC(sc, txs->txs_lastdesc, 1,
1383 BUS_DMASYNC_PREREAD);
1384 break;
1385 }
1386
1387 DPRINTF(DGE_DEBUG_TX,
1388 ("%s: TX: job %d done: descs %d..%d\n",
1389 sc->sc_dev.dv_xname, i, txs->txs_firstdesc,
1390 txs->txs_lastdesc));
1391
1392 ifp->if_opackets++;
1393 sc->sc_txfree += txs->txs_ndesc;
1394 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1395 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1396 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1397 m_freem(txs->txs_mbuf);
1398 txs->txs_mbuf = NULL;
1399 }
1400
1401 /* Update the dirty transmit buffer pointer. */
1402 sc->sc_txsdirty = i;
1403 DPRINTF(DGE_DEBUG_TX,
1404 ("%s: TX: txsdirty -> %d\n", sc->sc_dev.dv_xname, i));
1405
1406 /*
1407 * If there are no more pending transmissions, cancel the watchdog
1408 * timer.
1409 */
1410 if (sc->sc_txsfree == DGE_TXQUEUELEN)
1411 ifp->if_timer = 0;
1412 }
1413
1414 /*
1415 * dge_rxintr:
1416 *
1417 * Helper; handle receive interrupts.
1418 */
1419 static void
1420 dge_rxintr(struct dge_softc *sc)
1421 {
1422 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1423 struct dge_rxsoft *rxs;
1424 struct mbuf *m;
1425 int i, len;
1426 uint8_t status, errors;
1427
1428 for (i = sc->sc_rxptr;; i = DGE_NEXTRX(i)) {
1429 rxs = &sc->sc_rxsoft[i];
1430
1431 DPRINTF(DGE_DEBUG_RX,
1432 ("%s: RX: checking descriptor %d\n",
1433 sc->sc_dev.dv_xname, i));
1434
1435 DGE_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1436
1437 status = sc->sc_rxdescs[i].dr_status;
1438 errors = sc->sc_rxdescs[i].dr_errors;
1439 len = le16toh(sc->sc_rxdescs[i].dr_len);
1440
1441 if ((status & RDESC_STS_DD) == 0) {
1442 /*
1443 * We have processed all of the receive descriptors.
1444 */
1445 DGE_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD);
1446 break;
1447 }
1448
1449 if (__predict_false(sc->sc_rxdiscard)) {
1450 DPRINTF(DGE_DEBUG_RX,
1451 ("%s: RX: discarding contents of descriptor %d\n",
1452 sc->sc_dev.dv_xname, i));
1453 DGE_INIT_RXDESC(sc, i);
1454 if (status & RDESC_STS_EOP) {
1455 /* Reset our state. */
1456 DPRINTF(DGE_DEBUG_RX,
1457 ("%s: RX: resetting rxdiscard -> 0\n",
1458 sc->sc_dev.dv_xname));
1459 sc->sc_rxdiscard = 0;
1460 }
1461 continue;
1462 }
1463
1464 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1465 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1466
1467 m = rxs->rxs_mbuf;
1468
1469 /*
1470 * Add a new receive buffer to the ring.
1471 */
1472 if (dge_add_rxbuf(sc, i) != 0) {
1473 /*
1474 * Failed, throw away what we've done so
1475 * far, and discard the rest of the packet.
1476 */
1477 ifp->if_ierrors++;
1478 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1479 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1480 DGE_INIT_RXDESC(sc, i);
1481 if ((status & RDESC_STS_EOP) == 0)
1482 sc->sc_rxdiscard = 1;
1483 if (sc->sc_rxhead != NULL)
1484 m_freem(sc->sc_rxhead);
1485 DGE_RXCHAIN_RESET(sc);
1486 DPRINTF(DGE_DEBUG_RX,
1487 ("%s: RX: Rx buffer allocation failed, "
1488 "dropping packet%s\n", sc->sc_dev.dv_xname,
1489 sc->sc_rxdiscard ? " (discard)" : ""));
1490 continue;
1491 }
1492 DGE_INIT_RXDESC(sc, DGE_PREVRX(i)); /* Write the descriptor */
1493
1494 DGE_RXCHAIN_LINK(sc, m);
1495
1496 m->m_len = len;
1497
1498 DPRINTF(DGE_DEBUG_RX,
1499 ("%s: RX: buffer at %p len %d\n",
1500 sc->sc_dev.dv_xname, m->m_data, len));
1501
1502 /*
1503 * If this is not the end of the packet, keep
1504 * looking.
1505 */
1506 if ((status & RDESC_STS_EOP) == 0) {
1507 sc->sc_rxlen += len;
1508 DPRINTF(DGE_DEBUG_RX,
1509 ("%s: RX: not yet EOP, rxlen -> %d\n",
1510 sc->sc_dev.dv_xname, sc->sc_rxlen));
1511 continue;
1512 }
1513
1514 /*
1515 * Okay, we have the entire packet now...
1516 */
1517 *sc->sc_rxtailp = NULL;
1518 m = sc->sc_rxhead;
1519 len += sc->sc_rxlen;
1520
1521 DGE_RXCHAIN_RESET(sc);
1522
1523 DPRINTF(DGE_DEBUG_RX,
1524 ("%s: RX: have entire packet, len -> %d\n",
1525 sc->sc_dev.dv_xname, len));
1526
1527 /*
1528 * If an error occurred, update stats and drop the packet.
1529 */
1530 if (errors &
1531 (RDESC_ERR_CE|RDESC_ERR_SE|RDESC_ERR_P|RDESC_ERR_RXE)) {
1532 ifp->if_ierrors++;
1533 if (errors & RDESC_ERR_SE)
1534 printf("%s: symbol error\n",
1535 sc->sc_dev.dv_xname);
1536 else if (errors & RDESC_ERR_P)
1537 printf("%s: parity error\n",
1538 sc->sc_dev.dv_xname);
1539 else if (errors & RDESC_ERR_CE)
1540 printf("%s: CRC error\n",
1541 sc->sc_dev.dv_xname);
1542 m_freem(m);
1543 continue;
1544 }
1545
1546 /*
1547 * No errors. Receive the packet.
1548 */
1549 m->m_pkthdr.rcvif = ifp;
1550 m->m_pkthdr.len = len;
1551
1552 /*
1553 * Set up checksum info for this packet.
1554 */
1555 if (status & RDESC_STS_IPCS) {
1556 DGE_EVCNT_INCR(&sc->sc_ev_rxipsum);
1557 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1558 if (errors & RDESC_ERR_IPE)
1559 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1560 }
1561 if (status & RDESC_STS_TCPCS) {
1562 /*
1563 * Note: we don't know if this was TCP or UDP,
1564 * so we just set both bits, and expect the
1565 * upper layers to deal.
1566 */
1567 DGE_EVCNT_INCR(&sc->sc_ev_rxtusum);
1568 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4|M_CSUM_UDPv4;
1569 if (errors & RDESC_ERR_TCPE)
1570 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1571 }
1572
1573 ifp->if_ipackets++;
1574
1575 #if NBPFILTER > 0
1576 /* Pass this up to any BPF listeners. */
1577 if (ifp->if_bpf)
1578 bpf_mtap(ifp->if_bpf, m);
1579 #endif /* NBPFILTER > 0 */
1580
1581 /* Pass it on. */
1582 (*ifp->if_input)(ifp, m);
1583 }
1584
1585 /* Update the receive pointer. */
1586 sc->sc_rxptr = i;
1587
1588 DPRINTF(DGE_DEBUG_RX,
1589 ("%s: RX: rxptr -> %d\n", sc->sc_dev.dv_xname, i));
1590 }
1591
1592 /*
1593 * dge_linkintr:
1594 *
1595 * Helper; handle link interrupts.
1596 */
1597 static void
1598 dge_linkintr(struct dge_softc *sc, uint32_t icr)
1599 {
1600 uint32_t status;
1601
1602 if (icr & ICR_LSC) {
1603 status = CSR_READ(sc, DGE_STATUS);
1604 if (status & STATUS_LINKUP) {
1605 DPRINTF(DGE_DEBUG_LINK, ("%s: LINK: LSC -> up\n",
1606 sc->sc_dev.dv_xname));
1607 } else {
1608 DPRINTF(DGE_DEBUG_LINK, ("%s: LINK: LSC -> down\n",
1609 sc->sc_dev.dv_xname));
1610 }
1611 } else if (icr & ICR_RXSEQ) {
1612 DPRINTF(DGE_DEBUG_LINK,
1613 ("%s: LINK: Receive sequence error\n",
1614 sc->sc_dev.dv_xname));
1615 }
1616 /* XXX - fix errata */
1617 }
1618
1619 /*
1620 * dge_reset:
1621 *
1622 * Reset the i82597 chip.
1623 */
1624 static void
1625 dge_reset(struct dge_softc *sc)
1626 {
1627 int i;
1628
1629 /*
1630 * Do a chip reset.
1631 */
1632 CSR_WRITE(sc, DGE_CTRL0, CTRL0_RST | sc->sc_ctrl0);
1633
1634 delay(10000);
1635
1636 for (i = 0; i < 1000; i++) {
1637 if ((CSR_READ(sc, DGE_CTRL0) & CTRL0_RST) == 0)
1638 break;
1639 delay(20);
1640 }
1641
1642 if (CSR_READ(sc, DGE_CTRL0) & CTRL0_RST)
1643 printf("%s: WARNING: reset failed to complete\n",
1644 sc->sc_dev.dv_xname);
1645 /*
1646 * Reset the EEPROM logic.
1647 * This will cause the chip to reread its default values,
1648 * which doesn't happen otherwise (errata).
1649 */
1650 CSR_WRITE(sc, DGE_CTRL1, CTRL1_EE_RST);
1651 delay(10000);
1652 }
1653
1654 /*
1655 * dge_init: [ifnet interface function]
1656 *
1657 * Initialize the interface. Must be called at splnet().
1658 */
1659 static int
1660 dge_init(struct ifnet *ifp)
1661 {
1662 struct dge_softc *sc = ifp->if_softc;
1663 struct dge_rxsoft *rxs;
1664 int i, error = 0;
1665 uint32_t reg;
1666
1667 /*
1668 * *_HDR_ALIGNED_P is constant 1 if __NO_STRICT_ALIGMENT is set.
1669 * There is a small but measurable benefit to avoiding the adjusment
1670 * of the descriptor so that the headers are aligned, for normal mtu,
1671 * on such platforms. One possibility is that the DMA itself is
1672 * slightly more efficient if the front of the entire packet (instead
1673 * of the front of the headers) is aligned.
1674 *
1675 * Note we must always set align_tweak to 0 if we are using
1676 * jumbo frames.
1677 */
1678 #ifdef __NO_STRICT_ALIGNMENT
1679 sc->sc_align_tweak = 0;
1680 #else
1681 if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN) > (MCLBYTES - 2))
1682 sc->sc_align_tweak = 0;
1683 else
1684 sc->sc_align_tweak = 2;
1685 #endif /* __NO_STRICT_ALIGNMENT */
1686
1687 /* Cancel any pending I/O. */
1688 dge_stop(ifp, 0);
1689
1690 /* Reset the chip to a known state. */
1691 dge_reset(sc);
1692
1693 /* Initialize the transmit descriptor ring. */
1694 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1695 DGE_CDTXSYNC(sc, 0, DGE_NTXDESC,
1696 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1697 sc->sc_txfree = DGE_NTXDESC;
1698 sc->sc_txnext = 0;
1699
1700 sc->sc_txctx_ipcs = 0xffffffff;
1701 sc->sc_txctx_tucs = 0xffffffff;
1702
1703 CSR_WRITE(sc, DGE_TDBAH, 0);
1704 CSR_WRITE(sc, DGE_TDBAL, DGE_CDTXADDR(sc, 0));
1705 CSR_WRITE(sc, DGE_TDLEN, sizeof(sc->sc_txdescs));
1706 CSR_WRITE(sc, DGE_TDH, 0);
1707 CSR_WRITE(sc, DGE_TDT, 0);
1708 CSR_WRITE(sc, DGE_TIDV, TIDV);
1709
1710 #if 0
1711 CSR_WRITE(sc, DGE_TXDCTL, TXDCTL_PTHRESH(0) |
1712 TXDCTL_HTHRESH(0) | TXDCTL_WTHRESH(0));
1713 #endif
1714 CSR_WRITE(sc, DGE_RXDCTL,
1715 RXDCTL_PTHRESH(RXDCTL_PTHRESH_VAL) |
1716 RXDCTL_HTHRESH(RXDCTL_HTHRESH_VAL) |
1717 RXDCTL_WTHRESH(RXDCTL_WTHRESH_VAL));
1718
1719 /* Initialize the transmit job descriptors. */
1720 for (i = 0; i < DGE_TXQUEUELEN; i++)
1721 sc->sc_txsoft[i].txs_mbuf = NULL;
1722 sc->sc_txsfree = DGE_TXQUEUELEN;
1723 sc->sc_txsnext = 0;
1724 sc->sc_txsdirty = 0;
1725
1726 /*
1727 * Initialize the receive descriptor and receive job
1728 * descriptor rings.
1729 */
1730 CSR_WRITE(sc, DGE_RDBAH, 0);
1731 CSR_WRITE(sc, DGE_RDBAL, DGE_CDRXADDR(sc, 0));
1732 CSR_WRITE(sc, DGE_RDLEN, sizeof(sc->sc_rxdescs));
1733 CSR_WRITE(sc, DGE_RDH, DGE_RXSPACE);
1734 CSR_WRITE(sc, DGE_RDT, 0);
1735 CSR_WRITE(sc, DGE_RDTR, RDTR | 0x80000000);
1736 CSR_WRITE(sc, DGE_FCRTL, FCRTL | FCRTL_XONE);
1737 CSR_WRITE(sc, DGE_FCRTH, FCRTH);
1738
1739 for (i = 0; i < DGE_NRXDESC; i++) {
1740 rxs = &sc->sc_rxsoft[i];
1741 if (rxs->rxs_mbuf == NULL) {
1742 if ((error = dge_add_rxbuf(sc, i)) != 0) {
1743 printf("%s: unable to allocate or map rx "
1744 "buffer %d, error = %d\n",
1745 sc->sc_dev.dv_xname, i, error);
1746 /*
1747 * XXX Should attempt to run with fewer receive
1748 * XXX buffers instead of just failing.
1749 */
1750 dge_rxdrain(sc);
1751 goto out;
1752 }
1753 }
1754 DGE_INIT_RXDESC(sc, i);
1755 }
1756 sc->sc_rxptr = DGE_RXSPACE;
1757 sc->sc_rxdiscard = 0;
1758 DGE_RXCHAIN_RESET(sc);
1759
1760 if (sc->sc_ethercom.ec_capabilities & ETHERCAP_JUMBO_MTU) {
1761 sc->sc_ctrl0 |= CTRL0_JFE;
1762 CSR_WRITE(sc, DGE_MFS, ETHER_MAX_LEN_JUMBO << 16);
1763 }
1764
1765 /* Write the control registers. */
1766 CSR_WRITE(sc, DGE_CTRL0, sc->sc_ctrl0);
1767
1768 /*
1769 * Set up checksum offload parameters.
1770 */
1771 reg = CSR_READ(sc, DGE_RXCSUM);
1772 if (ifp->if_capenable & IFCAP_CSUM_IPv4)
1773 reg |= RXCSUM_IPOFL;
1774 else
1775 reg &= ~RXCSUM_IPOFL;
1776 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4))
1777 reg |= RXCSUM_IPOFL | RXCSUM_TUOFL;
1778 else {
1779 reg &= ~RXCSUM_TUOFL;
1780 if ((ifp->if_capenable & IFCAP_CSUM_IPv4) == 0)
1781 reg &= ~RXCSUM_IPOFL;
1782 }
1783 CSR_WRITE(sc, DGE_RXCSUM, reg);
1784
1785 /*
1786 * Set up the interrupt registers.
1787 */
1788 CSR_WRITE(sc, DGE_IMC, 0xffffffffU);
1789 sc->sc_icr = ICR_TXDW | ICR_LSC | ICR_RXSEQ | ICR_RXDMT0 |
1790 ICR_RXO | ICR_RXT0;
1791
1792 CSR_WRITE(sc, DGE_IMS, sc->sc_icr);
1793
1794 /*
1795 * Set up the transmit control register.
1796 */
1797 sc->sc_tctl = TCTL_TCE|TCTL_TPDE|TCTL_TXEN;
1798 CSR_WRITE(sc, DGE_TCTL, sc->sc_tctl);
1799
1800 /*
1801 * Set up the receive control register; we actually program
1802 * the register when we set the receive filter. Use multicast
1803 * address offset type 0.
1804 */
1805 sc->sc_mchash_type = 0;
1806
1807 sc->sc_rctl = RCTL_RXEN | RCTL_RDMTS_12 | RCTL_RPDA_MC |
1808 RCTL_CFF | RCTL_SECRC | RCTL_MO(sc->sc_mchash_type);
1809
1810 switch(MCLBYTES) {
1811 case 2048:
1812 sc->sc_rctl |= RCTL_BSIZE_2k;
1813 break;
1814 case 4096:
1815 sc->sc_rctl |= RCTL_BSIZE_4k;
1816 break;
1817 case 8192:
1818 sc->sc_rctl |= RCTL_BSIZE_8k;
1819 break;
1820 case 16384:
1821 sc->sc_rctl |= RCTL_BSIZE_16k;
1822 break;
1823 default:
1824 panic("dge_init: MCLBYTES %d unsupported", MCLBYTES);
1825 }
1826
1827 /* Set the receive filter. */
1828 /* Also sets RCTL */
1829 dge_set_filter(sc);
1830
1831 /* ...all done! */
1832 ifp->if_flags |= IFF_RUNNING;
1833 ifp->if_flags &= ~IFF_OACTIVE;
1834
1835 out:
1836 if (error)
1837 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1838 return (error);
1839 }
1840
1841 /*
1842 * dge_rxdrain:
1843 *
1844 * Drain the receive queue.
1845 */
1846 static void
1847 dge_rxdrain(struct dge_softc *sc)
1848 {
1849 struct dge_rxsoft *rxs;
1850 int i;
1851
1852 for (i = 0; i < DGE_NRXDESC; i++) {
1853 rxs = &sc->sc_rxsoft[i];
1854 if (rxs->rxs_mbuf != NULL) {
1855 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1856 m_freem(rxs->rxs_mbuf);
1857 rxs->rxs_mbuf = NULL;
1858 }
1859 }
1860 }
1861
1862 /*
1863 * dge_stop: [ifnet interface function]
1864 *
1865 * Stop transmission on the interface.
1866 */
1867 static void
1868 dge_stop(struct ifnet *ifp, int disable)
1869 {
1870 struct dge_softc *sc = ifp->if_softc;
1871 struct dge_txsoft *txs;
1872 int i;
1873
1874 /* Stop the transmit and receive processes. */
1875 CSR_WRITE(sc, DGE_TCTL, 0);
1876 CSR_WRITE(sc, DGE_RCTL, 0);
1877
1878 /* Release any queued transmit buffers. */
1879 for (i = 0; i < DGE_TXQUEUELEN; i++) {
1880 txs = &sc->sc_txsoft[i];
1881 if (txs->txs_mbuf != NULL) {
1882 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1883 m_freem(txs->txs_mbuf);
1884 txs->txs_mbuf = NULL;
1885 }
1886 }
1887
1888 if (disable)
1889 dge_rxdrain(sc);
1890
1891 /* Mark the interface as down and cancel the watchdog timer. */
1892 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1893 ifp->if_timer = 0;
1894 }
1895
1896 /*
1897 * dge_add_rxbuf:
1898 *
1899 * Add a receive buffer to the indiciated descriptor.
1900 */
1901 static int
1902 dge_add_rxbuf(struct dge_softc *sc, int idx)
1903 {
1904 struct dge_rxsoft *rxs = &sc->sc_rxsoft[idx];
1905 struct mbuf *m;
1906 int error;
1907
1908 MGETHDR(m, M_DONTWAIT, MT_DATA);
1909 if (m == NULL)
1910 return (ENOBUFS);
1911
1912 MCLGET(m, M_DONTWAIT);
1913 if ((m->m_flags & M_EXT) == 0) {
1914 m_freem(m);
1915 return (ENOBUFS);
1916 }
1917
1918 if (rxs->rxs_mbuf != NULL)
1919 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1920
1921 rxs->rxs_mbuf = m;
1922
1923 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
1924 error = bus_dmamap_load_mbuf(sc->sc_dmat, rxs->rxs_dmamap, m,
1925 BUS_DMA_READ|BUS_DMA_NOWAIT);
1926 if (error) {
1927 printf("%s: unable to load rx DMA map %d, error = %d\n",
1928 sc->sc_dev.dv_xname, idx, error);
1929 panic("dge_add_rxbuf"); /* XXX XXX XXX */
1930 }
1931
1932 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1933 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1934
1935 return (0);
1936 }
1937
1938 /*
1939 * dge_set_ral:
1940 *
1941 * Set an entry in the receive address list.
1942 */
1943 static void
1944 dge_set_ral(struct dge_softc *sc, const uint8_t *enaddr, int idx)
1945 {
1946 uint32_t ral_lo, ral_hi;
1947
1948 if (enaddr != NULL) {
1949 ral_lo = enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) |
1950 (enaddr[3] << 24);
1951 ral_hi = enaddr[4] | (enaddr[5] << 8);
1952 ral_hi |= RAH_AV;
1953 } else {
1954 ral_lo = 0;
1955 ral_hi = 0;
1956 }
1957 CSR_WRITE(sc, RA_ADDR(DGE_RAL, idx), ral_lo);
1958 CSR_WRITE(sc, RA_ADDR(DGE_RAH, idx), ral_hi);
1959 }
1960
1961 /*
1962 * dge_mchash:
1963 *
1964 * Compute the hash of the multicast address for the 4096-bit
1965 * multicast filter.
1966 */
1967 static uint32_t
1968 dge_mchash(struct dge_softc *sc, const uint8_t *enaddr)
1969 {
1970 static const int lo_shift[4] = { 4, 3, 2, 0 };
1971 static const int hi_shift[4] = { 4, 5, 6, 8 };
1972 uint32_t hash;
1973
1974 hash = (enaddr[4] >> lo_shift[sc->sc_mchash_type]) |
1975 (((uint16_t) enaddr[5]) << hi_shift[sc->sc_mchash_type]);
1976
1977 return (hash & 0xfff);
1978 }
1979
1980 /*
1981 * dge_set_filter:
1982 *
1983 * Set up the receive filter.
1984 */
1985 static void
1986 dge_set_filter(struct dge_softc *sc)
1987 {
1988 struct ethercom *ec = &sc->sc_ethercom;
1989 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1990 struct ether_multi *enm;
1991 struct ether_multistep step;
1992 uint32_t hash, reg, bit;
1993 int i;
1994
1995 sc->sc_rctl &= ~(RCTL_BAM | RCTL_UPE | RCTL_MPE);
1996
1997 if (ifp->if_flags & IFF_BROADCAST)
1998 sc->sc_rctl |= RCTL_BAM;
1999 if (ifp->if_flags & IFF_PROMISC) {
2000 sc->sc_rctl |= RCTL_UPE;
2001 goto allmulti;
2002 }
2003
2004 /*
2005 * Set the station address in the first RAL slot, and
2006 * clear the remaining slots.
2007 */
2008 dge_set_ral(sc, LLADDR(ifp->if_sadl), 0);
2009 for (i = 1; i < RA_TABSIZE; i++)
2010 dge_set_ral(sc, NULL, i);
2011
2012 /* Clear out the multicast table. */
2013 for (i = 0; i < MC_TABSIZE; i++)
2014 CSR_WRITE(sc, DGE_MTA + (i << 2), 0);
2015
2016 ETHER_FIRST_MULTI(step, ec, enm);
2017 while (enm != NULL) {
2018 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2019 /*
2020 * We must listen to a range of multicast addresses.
2021 * For now, just accept all multicasts, rather than
2022 * trying to set only those filter bits needed to match
2023 * the range. (At this time, the only use of address
2024 * ranges is for IP multicast routing, for which the
2025 * range is big enough to require all bits set.)
2026 */
2027 goto allmulti;
2028 }
2029
2030 hash = dge_mchash(sc, enm->enm_addrlo);
2031
2032 reg = (hash >> 5) & 0x7f;
2033 bit = hash & 0x1f;
2034
2035 hash = CSR_READ(sc, DGE_MTA + (reg << 2));
2036 hash |= 1U << bit;
2037
2038 CSR_WRITE(sc, DGE_MTA + (reg << 2), hash);
2039
2040 ETHER_NEXT_MULTI(step, enm);
2041 }
2042
2043 ifp->if_flags &= ~IFF_ALLMULTI;
2044 goto setit;
2045
2046 allmulti:
2047 ifp->if_flags |= IFF_ALLMULTI;
2048 sc->sc_rctl |= RCTL_MPE;
2049
2050 setit:
2051 CSR_WRITE(sc, DGE_RCTL, sc->sc_rctl);
2052 }
2053
2054 /*
2055 * Read in the EEPROM info and verify checksum.
2056 */
2057 int
2058 dge_read_eeprom(struct dge_softc *sc)
2059 {
2060 uint16_t cksum;
2061 int i;
2062
2063 cksum = 0;
2064 for (i = 0; i < EEPROM_SIZE; i++) {
2065 sc->sc_eeprom[i] = dge_eeprom_word(sc, i);
2066 cksum += sc->sc_eeprom[i];
2067 }
2068 return cksum != EEPROM_CKSUM;
2069 }
2070
2071
2072 /*
2073 * Read a 16-bit word from address addr in the serial EEPROM.
2074 */
2075 uint16_t
2076 dge_eeprom_word(struct dge_softc *sc, int addr)
2077 {
2078 uint32_t reg;
2079 uint16_t rval = 0;
2080 int i;
2081
2082 reg = CSR_READ(sc, DGE_EECD) & ~(EECD_SK|EECD_DI|EECD_CS);
2083
2084 /* Lower clock pulse (and data in to chip) */
2085 CSR_WRITE(sc, DGE_EECD, reg);
2086 /* Select chip */
2087 CSR_WRITE(sc, DGE_EECD, reg|EECD_CS);
2088
2089 /* Send read command */
2090 dge_eeprom_clockout(sc, 1);
2091 dge_eeprom_clockout(sc, 1);
2092 dge_eeprom_clockout(sc, 0);
2093
2094 /* Send address */
2095 for (i = 5; i >= 0; i--)
2096 dge_eeprom_clockout(sc, (addr >> i) & 1);
2097
2098 /* Read data */
2099 for (i = 0; i < 16; i++) {
2100 rval <<= 1;
2101 rval |= dge_eeprom_clockin(sc);
2102 }
2103
2104 /* Deselect chip */
2105 CSR_WRITE(sc, DGE_EECD, reg);
2106
2107 return rval;
2108 }
2109
2110 /*
2111 * Clock out a single bit to the EEPROM.
2112 */
2113 void
2114 dge_eeprom_clockout(struct dge_softc *sc, int bit)
2115 {
2116 int reg;
2117
2118 reg = CSR_READ(sc, DGE_EECD) & ~(EECD_DI|EECD_SK);
2119 if (bit)
2120 reg |= EECD_DI;
2121
2122 CSR_WRITE(sc, DGE_EECD, reg);
2123 delay(2);
2124 CSR_WRITE(sc, DGE_EECD, reg|EECD_SK);
2125 delay(2);
2126 CSR_WRITE(sc, DGE_EECD, reg);
2127 delay(2);
2128 }
2129
2130 /*
2131 * Clock in a single bit from EEPROM.
2132 */
2133 int
2134 dge_eeprom_clockin(struct dge_softc *sc)
2135 {
2136 int reg, rv;
2137
2138 reg = CSR_READ(sc, DGE_EECD) & ~(EECD_DI|EECD_DO|EECD_SK);
2139
2140 CSR_WRITE(sc, DGE_EECD, reg|EECD_SK); /* Raise clock */
2141 delay(2);
2142 rv = (CSR_READ(sc, DGE_EECD) & EECD_DO) != 0; /* Get bit */
2143 CSR_WRITE(sc, DGE_EECD, reg); /* Lower clock */
2144 delay(2);
2145
2146 return rv;
2147 }
2148
2149 static void
2150 dge_xgmii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2151 {
2152 struct dge_softc *sc = ifp->if_softc;
2153
2154 ifmr->ifm_status = IFM_AVALID;
2155 ifmr->ifm_active = IFM_ETHER|IFM_10G_LR;
2156
2157 if (CSR_READ(sc, DGE_STATUS) & STATUS_LINKUP)
2158 ifmr->ifm_status |= IFM_ACTIVE;
2159 }
2160
2161 static inline int
2162 phwait(struct dge_softc *sc, int p, int r, int d, int type)
2163 {
2164 int i, mdic;
2165
2166 CSR_WRITE(sc, DGE_MDIO,
2167 MDIO_PHY(p) | MDIO_REG(r) | MDIO_DEV(d) | type | MDIO_CMD);
2168 for (i = 0; i < 10; i++) {
2169 delay(10);
2170 if (((mdic = CSR_READ(sc, DGE_MDIO)) & MDIO_CMD) == 0)
2171 break;
2172 }
2173 return mdic;
2174 }
2175
2176
2177 static void
2178 dge_xgmii_writereg(struct device *self, int phy, int reg, int val)
2179 {
2180 struct dge_softc *sc = (void *) self;
2181 int mdic;
2182
2183 CSR_WRITE(sc, DGE_MDIRW, val);
2184 if (((mdic = phwait(sc, phy, reg, 1, MDIO_ADDR)) & MDIO_CMD)) {
2185 printf("%s: address cycle timeout; phy %d reg %d\n",
2186 sc->sc_dev.dv_xname, phy, reg);
2187 return;
2188 }
2189 if (((mdic = phwait(sc, phy, reg, 1, MDIO_WRITE)) & MDIO_CMD)) {
2190 printf("%s: read cycle timeout; phy %d reg %d\n",
2191 sc->sc_dev.dv_xname, phy, reg);
2192 return;
2193 }
2194 }
2195
2196 static void
2197 dge_xgmii_reset(struct dge_softc *sc)
2198 {
2199 dge_xgmii_writereg((void *)sc, 0, 0, BMCR_RESET);
2200 }
2201
2202 static int
2203 dge_xgmii_mediachange(struct ifnet *ifp)
2204 {
2205 return 0;
2206 }
2207
2208