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