if_wm.c revision 1.82 1 /* $NetBSD: if_wm.c,v 1.82 2004/10/06 05:29:51 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001, 2002, 2003, 2004 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
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 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Device driver for the Intel i8254x family of Gigabit Ethernet chips.
40 *
41 * TODO (in order of importance):
42 *
43 * - Rework how parameters are loaded from the EEPROM.
44 * - Figure out what to do with the i82545GM and i82546GB
45 * SERDES controllers.
46 * - Fix hw VLAN assist.
47 */
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: if_wm.c,v 1.82 2004/10/06 05:29:51 thorpej Exp $");
51
52 #include "bpfilter.h"
53 #include "rnd.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/callout.h>
58 #include <sys/mbuf.h>
59 #include <sys/malloc.h>
60 #include <sys/kernel.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
63 #include <sys/errno.h>
64 #include <sys/device.h>
65 #include <sys/queue.h>
66
67 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
68
69 #if NRND > 0
70 #include <sys/rnd.h>
71 #endif
72
73 #include <net/if.h>
74 #include <net/if_dl.h>
75 #include <net/if_media.h>
76 #include <net/if_ether.h>
77
78 #if NBPFILTER > 0
79 #include <net/bpf.h>
80 #endif
81
82 #include <netinet/in.h> /* XXX for struct ip */
83 #include <netinet/in_systm.h> /* XXX for struct ip */
84 #include <netinet/ip.h> /* XXX for struct ip */
85 #include <netinet/tcp.h> /* XXX for struct tcphdr */
86
87 #include <machine/bus.h>
88 #include <machine/intr.h>
89 #include <machine/endian.h>
90
91 #include <dev/mii/mii.h>
92 #include <dev/mii/miivar.h>
93 #include <dev/mii/mii_bitbang.h>
94
95 #include <dev/pci/pcireg.h>
96 #include <dev/pci/pcivar.h>
97 #include <dev/pci/pcidevs.h>
98
99 #include <dev/pci/if_wmreg.h>
100
101 #ifdef WM_DEBUG
102 #define WM_DEBUG_LINK 0x01
103 #define WM_DEBUG_TX 0x02
104 #define WM_DEBUG_RX 0x04
105 #define WM_DEBUG_GMII 0x08
106 int wm_debug = WM_DEBUG_TX|WM_DEBUG_RX|WM_DEBUG_LINK;
107
108 #define DPRINTF(x, y) if (wm_debug & (x)) printf y
109 #else
110 #define DPRINTF(x, y) /* nothing */
111 #endif /* WM_DEBUG */
112
113 /*
114 * Transmit descriptor list size. Due to errata, we can only have
115 * 256 hardware descriptors in the ring on < 82544, but we use 4096
116 * on >= 82544. We tell the upper layers that they can queue a lot
117 * of packets, and we go ahead and manage up to 64 (16 for the i82547)
118 * of them at a time.
119 *
120 * We allow up to 256 (!) DMA segments per packet. Pathological packet
121 * chains containing many small mbufs have been observed in zero-copy
122 * situations with jumbo frames.
123 */
124 #define WM_NTXSEGS 256
125 #define WM_IFQUEUELEN 256
126 #define WM_TXQUEUELEN_MAX 64
127 #define WM_TXQUEUELEN_MAX_82547 16
128 #define WM_TXQUEUELEN(sc) ((sc)->sc_txnum)
129 #define WM_TXQUEUELEN_MASK(sc) (WM_TXQUEUELEN(sc) - 1)
130 #define WM_TXQUEUE_GC(sc) (WM_TXQUEUELEN(sc) / 8)
131 #define WM_NTXDESC_82542 256
132 #define WM_NTXDESC_82544 4096
133 #define WM_NTXDESC(sc) ((sc)->sc_ntxdesc)
134 #define WM_NTXDESC_MASK(sc) (WM_NTXDESC(sc) - 1)
135 #define WM_TXDESCSIZE(sc) (WM_NTXDESC(sc) * sizeof(wiseman_txdesc_t))
136 #define WM_NEXTTX(sc, x) (((x) + 1) & WM_NTXDESC_MASK(sc))
137 #define WM_NEXTTXS(sc, x) (((x) + 1) & WM_TXQUEUELEN_MASK(sc))
138
139 #define WM_MAXTXDMA ETHER_MAX_LEN_JUMBO
140
141 /*
142 * Receive descriptor list size. We have one Rx buffer for normal
143 * sized packets. Jumbo packets consume 5 Rx buffers for a full-sized
144 * packet. We allocate 256 receive descriptors, each with a 2k
145 * buffer (MCLBYTES), which gives us room for 50 jumbo packets.
146 */
147 #define WM_NRXDESC 256
148 #define WM_NRXDESC_MASK (WM_NRXDESC - 1)
149 #define WM_NEXTRX(x) (((x) + 1) & WM_NRXDESC_MASK)
150 #define WM_PREVRX(x) (((x) - 1) & WM_NRXDESC_MASK)
151
152 /*
153 * Control structures are DMA'd to the i82542 chip. We allocate them in
154 * a single clump that maps to a single DMA segment to make serveral things
155 * easier.
156 */
157 struct wm_control_data_82544 {
158 /*
159 * The receive descriptors.
160 */
161 wiseman_rxdesc_t wcd_rxdescs[WM_NRXDESC];
162
163 /*
164 * The transmit descriptors. Put these at the end, because
165 * we might use a smaller number of them.
166 */
167 wiseman_txdesc_t wcd_txdescs[WM_NTXDESC_82544];
168 };
169
170 struct wm_control_data_82542 {
171 wiseman_rxdesc_t wcd_rxdescs[WM_NRXDESC];
172 wiseman_txdesc_t wcd_txdescs[WM_NTXDESC_82542];
173 };
174
175 #define WM_CDOFF(x) offsetof(struct wm_control_data_82544, x)
176 #define WM_CDTXOFF(x) WM_CDOFF(wcd_txdescs[(x)])
177 #define WM_CDRXOFF(x) WM_CDOFF(wcd_rxdescs[(x)])
178
179 /*
180 * Software state for transmit jobs.
181 */
182 struct wm_txsoft {
183 struct mbuf *txs_mbuf; /* head of our mbuf chain */
184 bus_dmamap_t txs_dmamap; /* our DMA map */
185 int txs_firstdesc; /* first descriptor in packet */
186 int txs_lastdesc; /* last descriptor in packet */
187 int txs_ndesc; /* # of descriptors used */
188 };
189
190 /*
191 * Software state for receive buffers. Each descriptor gets a
192 * 2k (MCLBYTES) buffer and a DMA map. For packets which fill
193 * more than one buffer, we chain them together.
194 */
195 struct wm_rxsoft {
196 struct mbuf *rxs_mbuf; /* head of our mbuf chain */
197 bus_dmamap_t rxs_dmamap; /* our DMA map */
198 };
199
200 typedef enum {
201 WM_T_unknown = 0,
202 WM_T_82542_2_0, /* i82542 2.0 (really old) */
203 WM_T_82542_2_1, /* i82542 2.1+ (old) */
204 WM_T_82543, /* i82543 */
205 WM_T_82544, /* i82544 */
206 WM_T_82540, /* i82540 */
207 WM_T_82545, /* i82545 */
208 WM_T_82545_3, /* i82545 3.0+ */
209 WM_T_82546, /* i82546 */
210 WM_T_82546_3, /* i82546 3.0+ */
211 WM_T_82541, /* i82541 */
212 WM_T_82541_2, /* i82541 2.0+ */
213 WM_T_82547, /* i82547 */
214 WM_T_82547_2, /* i82547 2.0+ */
215 } wm_chip_type;
216
217 /*
218 * Software state per device.
219 */
220 struct wm_softc {
221 struct device sc_dev; /* generic device information */
222 bus_space_tag_t sc_st; /* bus space tag */
223 bus_space_handle_t sc_sh; /* bus space handle */
224 bus_space_tag_t sc_iot; /* I/O space tag */
225 bus_space_handle_t sc_ioh; /* I/O space handle */
226 bus_dma_tag_t sc_dmat; /* bus DMA tag */
227 struct ethercom sc_ethercom; /* ethernet common data */
228 void *sc_sdhook; /* shutdown hook */
229
230 wm_chip_type sc_type; /* chip type */
231 int sc_flags; /* flags; see below */
232 int sc_bus_speed; /* PCI/PCIX bus speed */
233 int sc_pcix_offset; /* PCIX capability register offset */
234 int sc_flowflags; /* 802.3x flow control flags */
235
236 void *sc_ih; /* interrupt cookie */
237
238 int sc_ee_addrbits; /* EEPROM address bits */
239
240 struct mii_data sc_mii; /* MII/media information */
241
242 struct callout sc_tick_ch; /* tick callout */
243
244 bus_dmamap_t sc_cddmamap; /* control data DMA map */
245 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
246
247 int sc_align_tweak;
248
249 /*
250 * Software state for the transmit and receive descriptors.
251 */
252 int sc_txnum; /* must be a power of two */
253 struct wm_txsoft sc_txsoft[WM_TXQUEUELEN_MAX];
254 struct wm_rxsoft sc_rxsoft[WM_NRXDESC];
255
256 /*
257 * Control data structures.
258 */
259 int sc_ntxdesc; /* must be a power of two */
260 struct wm_control_data_82544 *sc_control_data;
261 #define sc_txdescs sc_control_data->wcd_txdescs
262 #define sc_rxdescs sc_control_data->wcd_rxdescs
263
264 #ifdef WM_EVENT_COUNTERS
265 /* Event counters. */
266 struct evcnt sc_ev_txsstall; /* Tx stalled due to no txs */
267 struct evcnt sc_ev_txdstall; /* Tx stalled due to no txd */
268 struct evcnt sc_ev_txfifo_stall;/* Tx FIFO stalls (82547) */
269 struct evcnt sc_ev_txdw; /* Tx descriptor interrupts */
270 struct evcnt sc_ev_txqe; /* Tx queue empty interrupts */
271 struct evcnt sc_ev_rxintr; /* Rx interrupts */
272 struct evcnt sc_ev_linkintr; /* Link interrupts */
273
274 struct evcnt sc_ev_rxipsum; /* IP checksums checked in-bound */
275 struct evcnt sc_ev_rxtusum; /* TCP/UDP cksums checked in-bound */
276 struct evcnt sc_ev_txipsum; /* IP checksums comp. out-bound */
277 struct evcnt sc_ev_txtusum; /* TCP/UDP cksums comp. out-bound */
278
279 struct evcnt sc_ev_txctx_init; /* Tx cksum context cache initialized */
280 struct evcnt sc_ev_txctx_hit; /* Tx cksum context cache hit */
281 struct evcnt sc_ev_txctx_miss; /* Tx cksum context cache miss */
282
283 struct evcnt sc_ev_txseg[WM_NTXSEGS]; /* Tx packets w/ N segments */
284 struct evcnt sc_ev_txdrop; /* Tx packets dropped (too many segs) */
285
286 struct evcnt sc_ev_tu; /* Tx underrun */
287
288 struct evcnt sc_ev_tx_xoff; /* Tx PAUSE(!0) frames */
289 struct evcnt sc_ev_tx_xon; /* Tx PAUSE(0) frames */
290 struct evcnt sc_ev_rx_xoff; /* Rx PAUSE(!0) frames */
291 struct evcnt sc_ev_rx_xon; /* Rx PAUSE(0) frames */
292 struct evcnt sc_ev_rx_macctl; /* Rx Unsupported */
293 #endif /* WM_EVENT_COUNTERS */
294
295 bus_addr_t sc_tdt_reg; /* offset of TDT register */
296
297 int sc_txfree; /* number of free Tx descriptors */
298 int sc_txnext; /* next ready Tx descriptor */
299
300 int sc_txsfree; /* number of free Tx jobs */
301 int sc_txsnext; /* next free Tx job */
302 int sc_txsdirty; /* dirty Tx jobs */
303
304 /* These 5 variables are used only on the 82547. */
305 int sc_txfifo_size; /* Tx FIFO size */
306 int sc_txfifo_head; /* current head of FIFO */
307 uint32_t sc_txfifo_addr; /* internal address of start of FIFO */
308 int sc_txfifo_stall; /* Tx FIFO is stalled */
309 struct callout sc_txfifo_ch; /* Tx FIFO stall work-around timer */
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 bus_addr_t sc_rdt_reg; /* offset of RDT register */
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_ctrl; /* prototype CTRL register */
324 #if 0
325 uint32_t sc_ctrl_ext; /* prototype CTRL_EXT register */
326 #endif
327 uint32_t sc_icr; /* prototype interrupt bits */
328 uint32_t sc_tctl; /* prototype TCTL register */
329 uint32_t sc_rctl; /* prototype RCTL register */
330 uint32_t sc_txcw; /* prototype TXCW register */
331 uint32_t sc_tipg; /* prototype TIPG register */
332 uint32_t sc_fcrtl; /* prototype FCRTL register */
333 uint32_t sc_pba; /* prototype PBA register */
334
335 int sc_tbi_linkup; /* TBI link status */
336 int sc_tbi_anstate; /* autonegotiation state */
337
338 int sc_mchash_type; /* multicast filter offset */
339
340 #if NRND > 0
341 rndsource_element_t rnd_source; /* random source */
342 #endif
343 };
344
345 #define WM_RXCHAIN_RESET(sc) \
346 do { \
347 (sc)->sc_rxtailp = &(sc)->sc_rxhead; \
348 *(sc)->sc_rxtailp = NULL; \
349 (sc)->sc_rxlen = 0; \
350 } while (/*CONSTCOND*/0)
351
352 #define WM_RXCHAIN_LINK(sc, m) \
353 do { \
354 *(sc)->sc_rxtailp = (sc)->sc_rxtail = (m); \
355 (sc)->sc_rxtailp = &(m)->m_next; \
356 } while (/*CONSTCOND*/0)
357
358 /* sc_flags */
359 #define WM_F_HAS_MII 0x01 /* has MII */
360 #define WM_F_EEPROM_HANDSHAKE 0x02 /* requires EEPROM handshake */
361 #define WM_F_EEPROM_SPI 0x04 /* EEPROM is SPI */
362 #define WM_F_IOH_VALID 0x10 /* I/O handle is valid */
363 #define WM_F_BUS64 0x20 /* bus is 64-bit */
364 #define WM_F_PCIX 0x40 /* bus is PCI-X */
365 #define WM_F_CSA 0x80 /* bus is CSA */
366
367 #ifdef WM_EVENT_COUNTERS
368 #define WM_EVCNT_INCR(ev) (ev)->ev_count++
369 #define WM_EVCNT_ADD(ev, val) (ev)->ev_count += (val)
370 #else
371 #define WM_EVCNT_INCR(ev) /* nothing */
372 #define WM_EVCNT_ADD(ev, val) /* nothing */
373 #endif
374
375 #define CSR_READ(sc, reg) \
376 bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (reg))
377 #define CSR_WRITE(sc, reg, val) \
378 bus_space_write_4((sc)->sc_st, (sc)->sc_sh, (reg), (val))
379 #define CSR_WRITE_FLUSH(sc) \
380 (void) CSR_READ((sc), WMREG_STATUS)
381
382 #define WM_CDTXADDR(sc, x) ((sc)->sc_cddma + WM_CDTXOFF((x)))
383 #define WM_CDRXADDR(sc, x) ((sc)->sc_cddma + WM_CDRXOFF((x)))
384
385 #define WM_CDTXADDR_LO(sc, x) (WM_CDTXADDR((sc), (x)) & 0xffffffffU)
386 #define WM_CDTXADDR_HI(sc, x) \
387 (sizeof(bus_addr_t) == 8 ? \
388 (uint64_t)WM_CDTXADDR((sc), (x)) >> 32 : 0)
389
390 #define WM_CDRXADDR_LO(sc, x) (WM_CDRXADDR((sc), (x)) & 0xffffffffU)
391 #define WM_CDRXADDR_HI(sc, x) \
392 (sizeof(bus_addr_t) == 8 ? \
393 (uint64_t)WM_CDRXADDR((sc), (x)) >> 32 : 0)
394
395 #define WM_CDTXSYNC(sc, x, n, ops) \
396 do { \
397 int __x, __n; \
398 \
399 __x = (x); \
400 __n = (n); \
401 \
402 /* If it will wrap around, sync to the end of the ring. */ \
403 if ((__x + __n) > WM_NTXDESC(sc)) { \
404 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
405 WM_CDTXOFF(__x), sizeof(wiseman_txdesc_t) * \
406 (WM_NTXDESC(sc) - __x), (ops)); \
407 __n -= (WM_NTXDESC(sc) - __x); \
408 __x = 0; \
409 } \
410 \
411 /* Now sync whatever is left. */ \
412 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
413 WM_CDTXOFF(__x), sizeof(wiseman_txdesc_t) * __n, (ops)); \
414 } while (/*CONSTCOND*/0)
415
416 #define WM_CDRXSYNC(sc, x, ops) \
417 do { \
418 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
419 WM_CDRXOFF((x)), sizeof(wiseman_rxdesc_t), (ops)); \
420 } while (/*CONSTCOND*/0)
421
422 #define WM_INIT_RXDESC(sc, x) \
423 do { \
424 struct wm_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \
425 wiseman_rxdesc_t *__rxd = &(sc)->sc_rxdescs[(x)]; \
426 struct mbuf *__m = __rxs->rxs_mbuf; \
427 \
428 /* \
429 * Note: We scoot the packet forward 2 bytes in the buffer \
430 * so that the payload after the Ethernet header is aligned \
431 * to a 4-byte boundary. \
432 * \
433 * XXX BRAINDAMAGE ALERT! \
434 * The stupid chip uses the same size for every buffer, which \
435 * is set in the Receive Control register. We are using the 2K \
436 * size option, but what we REALLY want is (2K - 2)! For this \
437 * reason, we can't "scoot" packets longer than the standard \
438 * Ethernet MTU. On strict-alignment platforms, if the total \
439 * size exceeds (2K - 2) we set align_tweak to 0 and let \
440 * the upper layer copy the headers. \
441 */ \
442 __m->m_data = __m->m_ext.ext_buf + (sc)->sc_align_tweak; \
443 \
444 wm_set_dma_addr(&__rxd->wrx_addr, \
445 __rxs->rxs_dmamap->dm_segs[0].ds_addr + (sc)->sc_align_tweak); \
446 __rxd->wrx_len = 0; \
447 __rxd->wrx_cksum = 0; \
448 __rxd->wrx_status = 0; \
449 __rxd->wrx_errors = 0; \
450 __rxd->wrx_special = 0; \
451 WM_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
452 \
453 CSR_WRITE((sc), (sc)->sc_rdt_reg, (x)); \
454 } while (/*CONSTCOND*/0)
455
456 static void wm_start(struct ifnet *);
457 static void wm_watchdog(struct ifnet *);
458 static int wm_ioctl(struct ifnet *, u_long, caddr_t);
459 static int wm_init(struct ifnet *);
460 static void wm_stop(struct ifnet *, int);
461
462 static void wm_shutdown(void *);
463
464 static void wm_reset(struct wm_softc *);
465 static void wm_rxdrain(struct wm_softc *);
466 static int wm_add_rxbuf(struct wm_softc *, int);
467 static int wm_read_eeprom(struct wm_softc *, int, int, u_int16_t *);
468 static void wm_tick(void *);
469
470 static void wm_set_filter(struct wm_softc *);
471
472 static int wm_intr(void *);
473 static void wm_txintr(struct wm_softc *);
474 static void wm_rxintr(struct wm_softc *);
475 static void wm_linkintr(struct wm_softc *, uint32_t);
476
477 static void wm_tbi_mediainit(struct wm_softc *);
478 static int wm_tbi_mediachange(struct ifnet *);
479 static void wm_tbi_mediastatus(struct ifnet *, struct ifmediareq *);
480
481 static void wm_tbi_set_linkled(struct wm_softc *);
482 static void wm_tbi_check_link(struct wm_softc *);
483
484 static void wm_gmii_reset(struct wm_softc *);
485
486 static int wm_gmii_i82543_readreg(struct device *, int, int);
487 static void wm_gmii_i82543_writereg(struct device *, int, int, int);
488
489 static int wm_gmii_i82544_readreg(struct device *, int, int);
490 static void wm_gmii_i82544_writereg(struct device *, int, int, int);
491
492 static void wm_gmii_statchg(struct device *);
493
494 static void wm_gmii_mediainit(struct wm_softc *);
495 static int wm_gmii_mediachange(struct ifnet *);
496 static void wm_gmii_mediastatus(struct ifnet *, struct ifmediareq *);
497
498 static int wm_match(struct device *, struct cfdata *, void *);
499 static void wm_attach(struct device *, struct device *, void *);
500
501 CFATTACH_DECL(wm, sizeof(struct wm_softc),
502 wm_match, wm_attach, NULL, NULL);
503
504 static void wm_82547_txfifo_stall(void *);
505
506 /*
507 * Devices supported by this driver.
508 */
509 static const struct wm_product {
510 pci_vendor_id_t wmp_vendor;
511 pci_product_id_t wmp_product;
512 const char *wmp_name;
513 wm_chip_type wmp_type;
514 int wmp_flags;
515 #define WMP_F_1000X 0x01
516 #define WMP_F_1000T 0x02
517 } wm_products[] = {
518 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82542,
519 "Intel i82542 1000BASE-X Ethernet",
520 WM_T_82542_2_1, WMP_F_1000X },
521
522 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82543GC_FIBER,
523 "Intel i82543GC 1000BASE-X Ethernet",
524 WM_T_82543, WMP_F_1000X },
525
526 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82543GC_COPPER,
527 "Intel i82543GC 1000BASE-T Ethernet",
528 WM_T_82543, WMP_F_1000T },
529
530 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544EI_COPPER,
531 "Intel i82544EI 1000BASE-T Ethernet",
532 WM_T_82544, WMP_F_1000T },
533
534 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544EI_FIBER,
535 "Intel i82544EI 1000BASE-X Ethernet",
536 WM_T_82544, WMP_F_1000X },
537
538 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544GC_COPPER,
539 "Intel i82544GC 1000BASE-T Ethernet",
540 WM_T_82544, WMP_F_1000T },
541
542 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544GC_LOM,
543 "Intel i82544GC (LOM) 1000BASE-T Ethernet",
544 WM_T_82544, WMP_F_1000T },
545
546 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EM,
547 "Intel i82540EM 1000BASE-T Ethernet",
548 WM_T_82540, WMP_F_1000T },
549
550 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EM_LOM,
551 "Intel i82540EM (LOM) 1000BASE-T Ethernet",
552 WM_T_82540, WMP_F_1000T },
553
554 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EP_LOM,
555 "Intel i82540EP 1000BASE-T Ethernet",
556 WM_T_82540, WMP_F_1000T },
557
558 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EP,
559 "Intel i82540EP 1000BASE-T Ethernet",
560 WM_T_82540, WMP_F_1000T },
561
562 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EP_LP,
563 "Intel i82540EP 1000BASE-T Ethernet",
564 WM_T_82540, WMP_F_1000T },
565
566 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545EM_COPPER,
567 "Intel i82545EM 1000BASE-T Ethernet",
568 WM_T_82545, WMP_F_1000T },
569
570 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545GM_COPPER,
571 "Intel i82545GM 1000BASE-T Ethernet",
572 WM_T_82545_3, WMP_F_1000T },
573
574 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545GM_FIBER,
575 "Intel i82545GM 1000BASE-X Ethernet",
576 WM_T_82545_3, WMP_F_1000X },
577 #if 0
578 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545GM_SERDES,
579 "Intel i82545GM Gigabit Ethernet (SERDES)",
580 WM_T_82545_3, WMP_F_SERDES },
581 #endif
582 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546EB_COPPER,
583 "Intel i82546EB 1000BASE-T Ethernet",
584 WM_T_82546, WMP_F_1000T },
585
586 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546EB_QUAD,
587 "Intel i82546EB 1000BASE-T Ethernet",
588 WM_T_82546, WMP_F_1000T },
589
590 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545EM_FIBER,
591 "Intel i82545EM 1000BASE-X Ethernet",
592 WM_T_82545, WMP_F_1000X },
593
594 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546EB_FIBER,
595 "Intel i82546EB 1000BASE-X Ethernet",
596 WM_T_82546, WMP_F_1000X },
597
598 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_COPPER,
599 "Intel i82546GB 1000BASE-T Ethernet",
600 WM_T_82546_3, WMP_F_1000T },
601
602 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_FIBER,
603 "Intel i82546GB 1000BASE-X Ethernet",
604 WM_T_82546_3, WMP_F_1000X },
605 #if 0
606 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_SERDES,
607 "Intel i82546GB Gigabit Ethernet (SERDES)",
608 WM_T_82546_3, WMP_F_SERDES },
609 #endif
610 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541EI,
611 "Intel i82541EI 1000BASE-T Ethernet",
612 WM_T_82541, WMP_F_1000T },
613
614 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541EI_MOBILE,
615 "Intel i82541EI Mobile 1000BASE-T Ethernet",
616 WM_T_82541, WMP_F_1000T },
617
618 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541ER,
619 "Intel i82541ER 1000BASE-T Ethernet",
620 WM_T_82541_2, WMP_F_1000T },
621
622 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541GI,
623 "Intel i82541GI 1000BASE-T Ethernet",
624 WM_T_82541_2, WMP_F_1000T },
625
626 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541GI_MOBILE,
627 "Intel i82541GI Mobile 1000BASE-T Ethernet",
628 WM_T_82541_2, WMP_F_1000T },
629
630 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82547EI,
631 "Intel i82547EI 1000BASE-T Ethernet",
632 WM_T_82547, WMP_F_1000T },
633
634 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82547GI,
635 "Intel i82547GI 1000BASE-T Ethernet",
636 WM_T_82547_2, WMP_F_1000T },
637 { 0, 0,
638 NULL,
639 0, 0 },
640 };
641
642 #ifdef WM_EVENT_COUNTERS
643 static char wm_txseg_evcnt_names[WM_NTXSEGS][sizeof("txsegXXX")];
644 #endif /* WM_EVENT_COUNTERS */
645
646 #if 0 /* Not currently used */
647 static __inline uint32_t
648 wm_io_read(struct wm_softc *sc, int reg)
649 {
650
651 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 0, reg);
652 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, 4));
653 }
654 #endif
655
656 static __inline void
657 wm_io_write(struct wm_softc *sc, int reg, uint32_t val)
658 {
659
660 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 0, reg);
661 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 4, val);
662 }
663
664 static __inline void
665 wm_set_dma_addr(__volatile wiseman_addr_t *wa, bus_addr_t v)
666 {
667 wa->wa_low = htole32(v & 0xffffffffU);
668 if (sizeof(bus_addr_t) == 8)
669 wa->wa_high = htole32((uint64_t) v >> 32);
670 else
671 wa->wa_high = 0;
672 }
673
674 static const struct wm_product *
675 wm_lookup(const struct pci_attach_args *pa)
676 {
677 const struct wm_product *wmp;
678
679 for (wmp = wm_products; wmp->wmp_name != NULL; wmp++) {
680 if (PCI_VENDOR(pa->pa_id) == wmp->wmp_vendor &&
681 PCI_PRODUCT(pa->pa_id) == wmp->wmp_product)
682 return (wmp);
683 }
684 return (NULL);
685 }
686
687 static int
688 wm_match(struct device *parent, struct cfdata *cf, void *aux)
689 {
690 struct pci_attach_args *pa = aux;
691
692 if (wm_lookup(pa) != NULL)
693 return (1);
694
695 return (0);
696 }
697
698 static void
699 wm_attach(struct device *parent, struct device *self, void *aux)
700 {
701 struct wm_softc *sc = (void *) self;
702 struct pci_attach_args *pa = aux;
703 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
704 pci_chipset_tag_t pc = pa->pa_pc;
705 pci_intr_handle_t ih;
706 size_t cdata_size;
707 const char *intrstr = NULL;
708 const char *eetype;
709 bus_space_tag_t memt;
710 bus_space_handle_t memh;
711 bus_dma_segment_t seg;
712 int memh_valid;
713 int i, rseg, error;
714 const struct wm_product *wmp;
715 uint8_t enaddr[ETHER_ADDR_LEN];
716 uint16_t myea[ETHER_ADDR_LEN / 2], cfg1, cfg2, swdpin;
717 pcireg_t preg, memtype;
718 uint32_t reg;
719 int pmreg;
720
721 callout_init(&sc->sc_tick_ch);
722
723 wmp = wm_lookup(pa);
724 if (wmp == NULL) {
725 printf("\n");
726 panic("wm_attach: impossible");
727 }
728
729 if (pci_dma64_available(pa))
730 sc->sc_dmat = pa->pa_dmat64;
731 else
732 sc->sc_dmat = pa->pa_dmat;
733
734 preg = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
735 aprint_naive(": Ethernet controller\n");
736 aprint_normal(": %s, rev. %d\n", wmp->wmp_name, preg);
737
738 sc->sc_type = wmp->wmp_type;
739 if (sc->sc_type < WM_T_82543) {
740 if (preg < 2) {
741 aprint_error("%s: i82542 must be at least rev. 2\n",
742 sc->sc_dev.dv_xname);
743 return;
744 }
745 if (preg < 3)
746 sc->sc_type = WM_T_82542_2_0;
747 }
748
749 /*
750 * Map the device. All devices support memory-mapped acccess,
751 * and it is really required for normal operation.
752 */
753 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, WM_PCI_MMBA);
754 switch (memtype) {
755 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
756 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
757 memh_valid = (pci_mapreg_map(pa, WM_PCI_MMBA,
758 memtype, 0, &memt, &memh, NULL, NULL) == 0);
759 break;
760 default:
761 memh_valid = 0;
762 }
763
764 if (memh_valid) {
765 sc->sc_st = memt;
766 sc->sc_sh = memh;
767 } else {
768 aprint_error("%s: unable to map device registers\n",
769 sc->sc_dev.dv_xname);
770 return;
771 }
772
773 /*
774 * In addition, i82544 and later support I/O mapped indirect
775 * register access. It is not desirable (nor supported in
776 * this driver) to use it for normal operation, though it is
777 * required to work around bugs in some chip versions.
778 */
779 if (sc->sc_type >= WM_T_82544) {
780 /* First we have to find the I/O BAR. */
781 for (i = PCI_MAPREG_START; i < PCI_MAPREG_END; i += 4) {
782 if (pci_mapreg_type(pa->pa_pc, pa->pa_tag, i) ==
783 PCI_MAPREG_TYPE_IO)
784 break;
785 }
786 if (i == PCI_MAPREG_END)
787 aprint_error("%s: WARNING: unable to find I/O BAR\n",
788 sc->sc_dev.dv_xname);
789 else if (pci_mapreg_map(pa, i, PCI_MAPREG_TYPE_IO,
790 0, &sc->sc_iot, &sc->sc_ioh,
791 NULL, NULL) == 0)
792 sc->sc_flags |= WM_F_IOH_VALID;
793 else
794 aprint_error("%s: WARNING: unable to map I/O space\n",
795 sc->sc_dev.dv_xname);
796 }
797
798 /* Enable bus mastering. Disable MWI on the i82542 2.0. */
799 preg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
800 preg |= PCI_COMMAND_MASTER_ENABLE;
801 if (sc->sc_type < WM_T_82542_2_1)
802 preg &= ~PCI_COMMAND_INVALIDATE_ENABLE;
803 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, preg);
804
805 /* Get it out of power save mode, if needed. */
806 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
807 preg = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR) &
808 PCI_PMCSR_STATE_MASK;
809 if (preg == PCI_PMCSR_STATE_D3) {
810 /*
811 * The card has lost all configuration data in
812 * this state, so punt.
813 */
814 aprint_error("%s: unable to wake from power state D3\n",
815 sc->sc_dev.dv_xname);
816 return;
817 }
818 if (preg != PCI_PMCSR_STATE_D0) {
819 aprint_normal("%s: waking up from power state D%d\n",
820 sc->sc_dev.dv_xname, preg);
821 pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
822 PCI_PMCSR_STATE_D0);
823 }
824 }
825
826 /*
827 * Map and establish our interrupt.
828 */
829 if (pci_intr_map(pa, &ih)) {
830 aprint_error("%s: unable to map interrupt\n",
831 sc->sc_dev.dv_xname);
832 return;
833 }
834 intrstr = pci_intr_string(pc, ih);
835 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wm_intr, sc);
836 if (sc->sc_ih == NULL) {
837 aprint_error("%s: unable to establish interrupt",
838 sc->sc_dev.dv_xname);
839 if (intrstr != NULL)
840 aprint_normal(" at %s", intrstr);
841 aprint_normal("\n");
842 return;
843 }
844 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
845
846 /*
847 * Determine a few things about the bus we're connected to.
848 */
849 if (sc->sc_type < WM_T_82543) {
850 /* We don't really know the bus characteristics here. */
851 sc->sc_bus_speed = 33;
852 } else if (sc->sc_type == WM_T_82547 || sc->sc_type == WM_T_82547_2) {
853 /*
854 * CSA (Communication Streaming Architecture) is about as fast
855 * a 32-bit 66MHz PCI Bus.
856 */
857 sc->sc_flags |= WM_F_CSA;
858 sc->sc_bus_speed = 66;
859 aprint_verbose("%s: Communication Streaming Architecture\n",
860 sc->sc_dev.dv_xname);
861 if (sc->sc_type == WM_T_82547) {
862 callout_init(&sc->sc_txfifo_ch);
863 callout_setfunc(&sc->sc_txfifo_ch,
864 wm_82547_txfifo_stall, sc);
865 aprint_verbose("%s: using 82547 Tx FIFO stall "
866 "work-around\n", sc->sc_dev.dv_xname);
867 }
868 } else {
869 reg = CSR_READ(sc, WMREG_STATUS);
870 if (reg & STATUS_BUS64)
871 sc->sc_flags |= WM_F_BUS64;
872 if (sc->sc_type >= WM_T_82544 &&
873 (reg & STATUS_PCIX_MODE) != 0) {
874 pcireg_t pcix_cmd, pcix_sts, bytecnt, maxb;
875
876 sc->sc_flags |= WM_F_PCIX;
877 if (pci_get_capability(pa->pa_pc, pa->pa_tag,
878 PCI_CAP_PCIX,
879 &sc->sc_pcix_offset, NULL) == 0)
880 aprint_error("%s: unable to find PCIX "
881 "capability\n", sc->sc_dev.dv_xname);
882 else if (sc->sc_type != WM_T_82545_3 &&
883 sc->sc_type != WM_T_82546_3) {
884 /*
885 * Work around a problem caused by the BIOS
886 * setting the max memory read byte count
887 * incorrectly.
888 */
889 pcix_cmd = pci_conf_read(pa->pa_pc, pa->pa_tag,
890 sc->sc_pcix_offset + PCI_PCIX_CMD);
891 pcix_sts = pci_conf_read(pa->pa_pc, pa->pa_tag,
892 sc->sc_pcix_offset + PCI_PCIX_STATUS);
893
894 bytecnt =
895 (pcix_cmd & PCI_PCIX_CMD_BYTECNT_MASK) >>
896 PCI_PCIX_CMD_BYTECNT_SHIFT;
897 maxb =
898 (pcix_sts & PCI_PCIX_STATUS_MAXB_MASK) >>
899 PCI_PCIX_STATUS_MAXB_SHIFT;
900 if (bytecnt > maxb) {
901 aprint_verbose("%s: resetting PCI-X "
902 "MMRBC: %d -> %d\n",
903 sc->sc_dev.dv_xname,
904 512 << bytecnt, 512 << maxb);
905 pcix_cmd = (pcix_cmd &
906 ~PCI_PCIX_CMD_BYTECNT_MASK) |
907 (maxb << PCI_PCIX_CMD_BYTECNT_SHIFT);
908 pci_conf_write(pa->pa_pc, pa->pa_tag,
909 sc->sc_pcix_offset + PCI_PCIX_CMD,
910 pcix_cmd);
911 }
912 }
913 }
914 /*
915 * The quad port adapter is special; it has a PCIX-PCIX
916 * bridge on the board, and can run the secondary bus at
917 * a higher speed.
918 */
919 if (wmp->wmp_product == PCI_PRODUCT_INTEL_82546EB_QUAD) {
920 sc->sc_bus_speed = (sc->sc_flags & WM_F_PCIX) ? 120
921 : 66;
922 } else if (sc->sc_flags & WM_F_PCIX) {
923 switch (reg & STATUS_PCIXSPD_MASK) {
924 case STATUS_PCIXSPD_50_66:
925 sc->sc_bus_speed = 66;
926 break;
927 case STATUS_PCIXSPD_66_100:
928 sc->sc_bus_speed = 100;
929 break;
930 case STATUS_PCIXSPD_100_133:
931 sc->sc_bus_speed = 133;
932 break;
933 default:
934 aprint_error(
935 "%s: unknown PCIXSPD %d; assuming 66MHz\n",
936 sc->sc_dev.dv_xname,
937 reg & STATUS_PCIXSPD_MASK);
938 sc->sc_bus_speed = 66;
939 }
940 } else
941 sc->sc_bus_speed = (reg & STATUS_PCI66) ? 66 : 33;
942 aprint_verbose("%s: %d-bit %dMHz %s bus\n", sc->sc_dev.dv_xname,
943 (sc->sc_flags & WM_F_BUS64) ? 64 : 32, sc->sc_bus_speed,
944 (sc->sc_flags & WM_F_PCIX) ? "PCIX" : "PCI");
945 }
946
947 /*
948 * Allocate the control data structures, and create and load the
949 * DMA map for it.
950 *
951 * NOTE: All Tx descriptors must be in the same 4G segment of
952 * memory. So must Rx descriptors. We simplify by allocating
953 * both sets within the same 4G segment.
954 */
955 WM_NTXDESC(sc) = sc->sc_type < WM_T_82544 ?
956 WM_NTXDESC_82542 : WM_NTXDESC_82544;
957 cdata_size = sc->sc_type < WM_T_82544 ?
958 sizeof(struct wm_control_data_82542) :
959 sizeof(struct wm_control_data_82544);
960 if ((error = bus_dmamem_alloc(sc->sc_dmat, cdata_size, PAGE_SIZE,
961 (bus_size_t) 0x100000000ULL,
962 &seg, 1, &rseg, 0)) != 0) {
963 aprint_error(
964 "%s: unable to allocate control data, error = %d\n",
965 sc->sc_dev.dv_xname, error);
966 goto fail_0;
967 }
968
969 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, cdata_size,
970 (caddr_t *)&sc->sc_control_data, 0)) != 0) {
971 aprint_error("%s: unable to map control data, error = %d\n",
972 sc->sc_dev.dv_xname, error);
973 goto fail_1;
974 }
975
976 if ((error = bus_dmamap_create(sc->sc_dmat, cdata_size, 1, cdata_size,
977 0, 0, &sc->sc_cddmamap)) != 0) {
978 aprint_error("%s: unable to create control data DMA map, "
979 "error = %d\n", sc->sc_dev.dv_xname, error);
980 goto fail_2;
981 }
982
983 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
984 sc->sc_control_data, cdata_size, NULL,
985 0)) != 0) {
986 aprint_error(
987 "%s: unable to load control data DMA map, error = %d\n",
988 sc->sc_dev.dv_xname, error);
989 goto fail_3;
990 }
991
992
993 /*
994 * Create the transmit buffer DMA maps.
995 */
996 WM_TXQUEUELEN(sc) =
997 (sc->sc_type == WM_T_82547 || sc->sc_type == WM_T_82547_2) ?
998 WM_TXQUEUELEN_MAX_82547 : WM_TXQUEUELEN_MAX;
999 for (i = 0; i < WM_TXQUEUELEN(sc); i++) {
1000 if ((error = bus_dmamap_create(sc->sc_dmat, WM_MAXTXDMA,
1001 WM_NTXSEGS, WTX_MAX_LEN, 0, 0,
1002 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
1003 aprint_error("%s: unable to create Tx DMA map %d, "
1004 "error = %d\n", sc->sc_dev.dv_xname, i, error);
1005 goto fail_4;
1006 }
1007 }
1008
1009 /*
1010 * Create the receive buffer DMA maps.
1011 */
1012 for (i = 0; i < WM_NRXDESC; i++) {
1013 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
1014 MCLBYTES, 0, 0,
1015 &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
1016 aprint_error("%s: unable to create Rx DMA map %d, "
1017 "error = %d\n", sc->sc_dev.dv_xname, i, error);
1018 goto fail_5;
1019 }
1020 sc->sc_rxsoft[i].rxs_mbuf = NULL;
1021 }
1022
1023 /*
1024 * Reset the chip to a known state.
1025 */
1026 wm_reset(sc);
1027
1028 /*
1029 * Get some information about the EEPROM.
1030 */
1031 if (sc->sc_type >= WM_T_82540)
1032 sc->sc_flags |= WM_F_EEPROM_HANDSHAKE;
1033 if (sc->sc_type <= WM_T_82544)
1034 sc->sc_ee_addrbits = 6;
1035 else if (sc->sc_type <= WM_T_82546_3) {
1036 reg = CSR_READ(sc, WMREG_EECD);
1037 if (reg & EECD_EE_SIZE)
1038 sc->sc_ee_addrbits = 8;
1039 else
1040 sc->sc_ee_addrbits = 6;
1041 } else if (sc->sc_type <= WM_T_82547_2) {
1042 reg = CSR_READ(sc, WMREG_EECD);
1043 if (reg & EECD_EE_TYPE) {
1044 sc->sc_flags |= WM_F_EEPROM_SPI;
1045 sc->sc_ee_addrbits = (reg & EECD_EE_ABITS) ? 16 : 8;
1046 } else
1047 sc->sc_ee_addrbits = (reg & EECD_EE_ABITS) ? 8 : 6;
1048 } else {
1049 /* Assume everything else is SPI. */
1050 reg = CSR_READ(sc, WMREG_EECD);
1051 sc->sc_flags |= WM_F_EEPROM_SPI;
1052 sc->sc_ee_addrbits = (reg & EECD_EE_ABITS) ? 16 : 8;
1053 }
1054 if (sc->sc_flags & WM_F_EEPROM_SPI)
1055 eetype = "SPI";
1056 else
1057 eetype = "MicroWire";
1058 aprint_verbose("%s: %u word (%d address bits) %s EEPROM\n",
1059 sc->sc_dev.dv_xname, 1U << sc->sc_ee_addrbits,
1060 sc->sc_ee_addrbits, eetype);
1061
1062 /*
1063 * Read the Ethernet address from the EEPROM.
1064 */
1065 if (wm_read_eeprom(sc, EEPROM_OFF_MACADDR,
1066 sizeof(myea) / sizeof(myea[0]), myea)) {
1067 aprint_error("%s: unable to read Ethernet address\n",
1068 sc->sc_dev.dv_xname);
1069 return;
1070 }
1071 enaddr[0] = myea[0] & 0xff;
1072 enaddr[1] = myea[0] >> 8;
1073 enaddr[2] = myea[1] & 0xff;
1074 enaddr[3] = myea[1] >> 8;
1075 enaddr[4] = myea[2] & 0xff;
1076 enaddr[5] = myea[2] >> 8;
1077
1078 /*
1079 * Toggle the LSB of the MAC address on the second port
1080 * of the i82546.
1081 */
1082 if (sc->sc_type == WM_T_82546) {
1083 if ((CSR_READ(sc, WMREG_STATUS) >> STATUS_FUNCID_SHIFT) & 1)
1084 enaddr[5] ^= 1;
1085 }
1086
1087 aprint_normal("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
1088 ether_sprintf(enaddr));
1089
1090 /*
1091 * Read the config info from the EEPROM, and set up various
1092 * bits in the control registers based on their contents.
1093 */
1094 if (wm_read_eeprom(sc, EEPROM_OFF_CFG1, 1, &cfg1)) {
1095 aprint_error("%s: unable to read CFG1 from EEPROM\n",
1096 sc->sc_dev.dv_xname);
1097 return;
1098 }
1099 if (wm_read_eeprom(sc, EEPROM_OFF_CFG2, 1, &cfg2)) {
1100 aprint_error("%s: unable to read CFG2 from EEPROM\n",
1101 sc->sc_dev.dv_xname);
1102 return;
1103 }
1104 if (sc->sc_type >= WM_T_82544) {
1105 if (wm_read_eeprom(sc, EEPROM_OFF_SWDPIN, 1, &swdpin)) {
1106 aprint_error("%s: unable to read SWDPIN from EEPROM\n",
1107 sc->sc_dev.dv_xname);
1108 return;
1109 }
1110 }
1111
1112 if (cfg1 & EEPROM_CFG1_ILOS)
1113 sc->sc_ctrl |= CTRL_ILOS;
1114 if (sc->sc_type >= WM_T_82544) {
1115 sc->sc_ctrl |=
1116 ((swdpin >> EEPROM_SWDPIN_SWDPIO_SHIFT) & 0xf) <<
1117 CTRL_SWDPIO_SHIFT;
1118 sc->sc_ctrl |=
1119 ((swdpin >> EEPROM_SWDPIN_SWDPIN_SHIFT) & 0xf) <<
1120 CTRL_SWDPINS_SHIFT;
1121 } else {
1122 sc->sc_ctrl |=
1123 ((cfg1 >> EEPROM_CFG1_SWDPIO_SHIFT) & 0xf) <<
1124 CTRL_SWDPIO_SHIFT;
1125 }
1126
1127 #if 0
1128 if (sc->sc_type >= WM_T_82544) {
1129 if (cfg1 & EEPROM_CFG1_IPS0)
1130 sc->sc_ctrl_ext |= CTRL_EXT_IPS;
1131 if (cfg1 & EEPROM_CFG1_IPS1)
1132 sc->sc_ctrl_ext |= CTRL_EXT_IPS1;
1133 sc->sc_ctrl_ext |=
1134 ((swdpin >> (EEPROM_SWDPIN_SWDPIO_SHIFT + 4)) & 0xd) <<
1135 CTRL_EXT_SWDPIO_SHIFT;
1136 sc->sc_ctrl_ext |=
1137 ((swdpin >> (EEPROM_SWDPIN_SWDPIN_SHIFT + 4)) & 0xd) <<
1138 CTRL_EXT_SWDPINS_SHIFT;
1139 } else {
1140 sc->sc_ctrl_ext |=
1141 ((cfg2 >> EEPROM_CFG2_SWDPIO_SHIFT) & 0xf) <<
1142 CTRL_EXT_SWDPIO_SHIFT;
1143 }
1144 #endif
1145
1146 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
1147 #if 0
1148 CSR_WRITE(sc, WMREG_CTRL_EXT, sc->sc_ctrl_ext);
1149 #endif
1150
1151 /*
1152 * Set up some register offsets that are different between
1153 * the i82542 and the i82543 and later chips.
1154 */
1155 if (sc->sc_type < WM_T_82543) {
1156 sc->sc_rdt_reg = WMREG_OLD_RDT0;
1157 sc->sc_tdt_reg = WMREG_OLD_TDT;
1158 } else {
1159 sc->sc_rdt_reg = WMREG_RDT;
1160 sc->sc_tdt_reg = WMREG_TDT;
1161 }
1162
1163 /*
1164 * Determine if we're TBI or GMII mode, and initialize the
1165 * media structures accordingly.
1166 */
1167 if (sc->sc_type < WM_T_82543 ||
1168 (CSR_READ(sc, WMREG_STATUS) & STATUS_TBIMODE) != 0) {
1169 if (wmp->wmp_flags & WMP_F_1000T)
1170 aprint_error("%s: WARNING: TBIMODE set on 1000BASE-T "
1171 "product!\n", sc->sc_dev.dv_xname);
1172 wm_tbi_mediainit(sc);
1173 } else {
1174 if (wmp->wmp_flags & WMP_F_1000X)
1175 aprint_error("%s: WARNING: TBIMODE clear on 1000BASE-X "
1176 "product!\n", sc->sc_dev.dv_xname);
1177 wm_gmii_mediainit(sc);
1178 }
1179
1180 ifp = &sc->sc_ethercom.ec_if;
1181 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
1182 ifp->if_softc = sc;
1183 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1184 ifp->if_ioctl = wm_ioctl;
1185 ifp->if_start = wm_start;
1186 ifp->if_watchdog = wm_watchdog;
1187 ifp->if_init = wm_init;
1188 ifp->if_stop = wm_stop;
1189 IFQ_SET_MAXLEN(&ifp->if_snd, max(WM_IFQUEUELEN, IFQ_MAXLEN));
1190 IFQ_SET_READY(&ifp->if_snd);
1191
1192 sc->sc_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
1193
1194 /*
1195 * If we're a i82543 or greater, we can support VLANs.
1196 */
1197 if (sc->sc_type >= WM_T_82543)
1198 sc->sc_ethercom.ec_capabilities |=
1199 ETHERCAP_VLAN_MTU /* XXXJRT | ETHERCAP_VLAN_HWTAGGING */;
1200
1201 /*
1202 * We can perform TCPv4 and UDPv4 checkums in-bound. Only
1203 * on i82543 and later.
1204 */
1205 if (sc->sc_type >= WM_T_82543)
1206 ifp->if_capabilities |=
1207 IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
1208
1209 /*
1210 * Attach the interface.
1211 */
1212 if_attach(ifp);
1213 ether_ifattach(ifp, enaddr);
1214 #if NRND > 0
1215 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
1216 RND_TYPE_NET, 0);
1217 #endif
1218
1219 #ifdef WM_EVENT_COUNTERS
1220 /* Attach event counters. */
1221 evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
1222 NULL, sc->sc_dev.dv_xname, "txsstall");
1223 evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
1224 NULL, sc->sc_dev.dv_xname, "txdstall");
1225 evcnt_attach_dynamic(&sc->sc_ev_txfifo_stall, EVCNT_TYPE_MISC,
1226 NULL, sc->sc_dev.dv_xname, "txfifo_stall");
1227 evcnt_attach_dynamic(&sc->sc_ev_txdw, EVCNT_TYPE_INTR,
1228 NULL, sc->sc_dev.dv_xname, "txdw");
1229 evcnt_attach_dynamic(&sc->sc_ev_txqe, EVCNT_TYPE_INTR,
1230 NULL, sc->sc_dev.dv_xname, "txqe");
1231 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
1232 NULL, sc->sc_dev.dv_xname, "rxintr");
1233 evcnt_attach_dynamic(&sc->sc_ev_linkintr, EVCNT_TYPE_INTR,
1234 NULL, sc->sc_dev.dv_xname, "linkintr");
1235
1236 evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
1237 NULL, sc->sc_dev.dv_xname, "rxipsum");
1238 evcnt_attach_dynamic(&sc->sc_ev_rxtusum, EVCNT_TYPE_MISC,
1239 NULL, sc->sc_dev.dv_xname, "rxtusum");
1240 evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
1241 NULL, sc->sc_dev.dv_xname, "txipsum");
1242 evcnt_attach_dynamic(&sc->sc_ev_txtusum, EVCNT_TYPE_MISC,
1243 NULL, sc->sc_dev.dv_xname, "txtusum");
1244
1245 evcnt_attach_dynamic(&sc->sc_ev_txctx_init, EVCNT_TYPE_MISC,
1246 NULL, sc->sc_dev.dv_xname, "txctx init");
1247 evcnt_attach_dynamic(&sc->sc_ev_txctx_hit, EVCNT_TYPE_MISC,
1248 NULL, sc->sc_dev.dv_xname, "txctx hit");
1249 evcnt_attach_dynamic(&sc->sc_ev_txctx_miss, EVCNT_TYPE_MISC,
1250 NULL, sc->sc_dev.dv_xname, "txctx miss");
1251
1252 for (i = 0; i < WM_NTXSEGS; i++) {
1253 sprintf(wm_txseg_evcnt_names[i], "txseg%d", i);
1254 evcnt_attach_dynamic(&sc->sc_ev_txseg[i], EVCNT_TYPE_MISC,
1255 NULL, sc->sc_dev.dv_xname, wm_txseg_evcnt_names[i]);
1256 }
1257
1258 evcnt_attach_dynamic(&sc->sc_ev_txdrop, EVCNT_TYPE_MISC,
1259 NULL, sc->sc_dev.dv_xname, "txdrop");
1260
1261 evcnt_attach_dynamic(&sc->sc_ev_tu, EVCNT_TYPE_MISC,
1262 NULL, sc->sc_dev.dv_xname, "tu");
1263
1264 evcnt_attach_dynamic(&sc->sc_ev_tx_xoff, EVCNT_TYPE_MISC,
1265 NULL, sc->sc_dev.dv_xname, "tx_xoff");
1266 evcnt_attach_dynamic(&sc->sc_ev_tx_xon, EVCNT_TYPE_MISC,
1267 NULL, sc->sc_dev.dv_xname, "tx_xon");
1268 evcnt_attach_dynamic(&sc->sc_ev_rx_xoff, EVCNT_TYPE_MISC,
1269 NULL, sc->sc_dev.dv_xname, "rx_xoff");
1270 evcnt_attach_dynamic(&sc->sc_ev_rx_xon, EVCNT_TYPE_MISC,
1271 NULL, sc->sc_dev.dv_xname, "rx_xon");
1272 evcnt_attach_dynamic(&sc->sc_ev_rx_macctl, EVCNT_TYPE_MISC,
1273 NULL, sc->sc_dev.dv_xname, "rx_macctl");
1274 #endif /* WM_EVENT_COUNTERS */
1275
1276 /*
1277 * Make sure the interface is shutdown during reboot.
1278 */
1279 sc->sc_sdhook = shutdownhook_establish(wm_shutdown, sc);
1280 if (sc->sc_sdhook == NULL)
1281 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
1282 sc->sc_dev.dv_xname);
1283 return;
1284
1285 /*
1286 * Free any resources we've allocated during the failed attach
1287 * attempt. Do this in reverse order and fall through.
1288 */
1289 fail_5:
1290 for (i = 0; i < WM_NRXDESC; i++) {
1291 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
1292 bus_dmamap_destroy(sc->sc_dmat,
1293 sc->sc_rxsoft[i].rxs_dmamap);
1294 }
1295 fail_4:
1296 for (i = 0; i < WM_TXQUEUELEN(sc); i++) {
1297 if (sc->sc_txsoft[i].txs_dmamap != NULL)
1298 bus_dmamap_destroy(sc->sc_dmat,
1299 sc->sc_txsoft[i].txs_dmamap);
1300 }
1301 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
1302 fail_3:
1303 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
1304 fail_2:
1305 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
1306 cdata_size);
1307 fail_1:
1308 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1309 fail_0:
1310 return;
1311 }
1312
1313 /*
1314 * wm_shutdown:
1315 *
1316 * Make sure the interface is stopped at reboot time.
1317 */
1318 static void
1319 wm_shutdown(void *arg)
1320 {
1321 struct wm_softc *sc = arg;
1322
1323 wm_stop(&sc->sc_ethercom.ec_if, 1);
1324 }
1325
1326 /*
1327 * wm_tx_cksum:
1328 *
1329 * Set up TCP/IP checksumming parameters for the
1330 * specified packet.
1331 */
1332 static int
1333 wm_tx_cksum(struct wm_softc *sc, struct wm_txsoft *txs, uint32_t *cmdp,
1334 uint8_t *fieldsp)
1335 {
1336 struct mbuf *m0 = txs->txs_mbuf;
1337 struct livengood_tcpip_ctxdesc *t;
1338 uint32_t ipcs, tucs;
1339 struct ip *ip;
1340 struct ether_header *eh;
1341 int offset, iphl;
1342 uint8_t fields = 0;
1343
1344 /*
1345 * XXX It would be nice if the mbuf pkthdr had offset
1346 * fields for the protocol headers.
1347 */
1348
1349 eh = mtod(m0, struct ether_header *);
1350 switch (htons(eh->ether_type)) {
1351 case ETHERTYPE_IP:
1352 iphl = sizeof(struct ip);
1353 offset = ETHER_HDR_LEN;
1354 break;
1355
1356 case ETHERTYPE_VLAN:
1357 iphl = sizeof(struct ip);
1358 offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
1359 break;
1360
1361 default:
1362 /*
1363 * Don't support this protocol or encapsulation.
1364 */
1365 *fieldsp = 0;
1366 *cmdp = 0;
1367 return (0);
1368 }
1369
1370 if (m0->m_len < (offset + iphl)) {
1371 if ((txs->txs_mbuf = m_pullup(m0, offset + iphl)) == NULL) {
1372 printf("%s: wm_tx_cksum: mbuf allocation failed, "
1373 "packet dropped\n", sc->sc_dev.dv_xname);
1374 return (ENOMEM);
1375 }
1376 m0 = txs->txs_mbuf;
1377 }
1378
1379 ip = (struct ip *) (mtod(m0, caddr_t) + offset);
1380 iphl = ip->ip_hl << 2;
1381
1382 /*
1383 * NOTE: Even if we're not using the IP or TCP/UDP checksum
1384 * offload feature, if we load the context descriptor, we
1385 * MUST provide valid values for IPCSS and TUCSS fields.
1386 */
1387
1388 if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
1389 WM_EVCNT_INCR(&sc->sc_ev_txipsum);
1390 fields |= WTX_IXSM;
1391 ipcs = WTX_TCPIP_IPCSS(offset) |
1392 WTX_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
1393 WTX_TCPIP_IPCSE(offset + iphl - 1);
1394 } else if (__predict_true(sc->sc_txctx_ipcs != 0xffffffff)) {
1395 /* Use the cached value. */
1396 ipcs = sc->sc_txctx_ipcs;
1397 } else {
1398 /* Just initialize it to the likely value anyway. */
1399 ipcs = WTX_TCPIP_IPCSS(offset) |
1400 WTX_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
1401 WTX_TCPIP_IPCSE(offset + iphl - 1);
1402 }
1403
1404 offset += iphl;
1405
1406 if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1407 WM_EVCNT_INCR(&sc->sc_ev_txtusum);
1408 fields |= WTX_TXSM;
1409 tucs = WTX_TCPIP_TUCSS(offset) |
1410 WTX_TCPIP_TUCSO(offset + m0->m_pkthdr.csum_data) |
1411 WTX_TCPIP_TUCSE(0) /* rest of packet */;
1412 } else if (__predict_true(sc->sc_txctx_tucs != 0xffffffff)) {
1413 /* Use the cached value. */
1414 tucs = sc->sc_txctx_tucs;
1415 } else {
1416 /* Just initialize it to a valid TCP context. */
1417 tucs = WTX_TCPIP_TUCSS(offset) |
1418 WTX_TCPIP_TUCSO(offset + offsetof(struct tcphdr, th_sum)) |
1419 WTX_TCPIP_TUCSE(0) /* rest of packet */;
1420 }
1421
1422 if (sc->sc_txctx_ipcs == ipcs &&
1423 sc->sc_txctx_tucs == tucs) {
1424 /* Cached context is fine. */
1425 WM_EVCNT_INCR(&sc->sc_ev_txctx_hit);
1426 } else {
1427 /* Fill in the context descriptor. */
1428 #ifdef WM_EVENT_COUNTERS
1429 if (sc->sc_txctx_ipcs == 0xffffffff &&
1430 sc->sc_txctx_tucs == 0xffffffff)
1431 WM_EVCNT_INCR(&sc->sc_ev_txctx_init);
1432 else
1433 WM_EVCNT_INCR(&sc->sc_ev_txctx_miss);
1434 #endif
1435 t = (struct livengood_tcpip_ctxdesc *)
1436 &sc->sc_txdescs[sc->sc_txnext];
1437 t->tcpip_ipcs = htole32(ipcs);
1438 t->tcpip_tucs = htole32(tucs);
1439 t->tcpip_cmdlen = htole32(WTX_CMD_DEXT | WTX_DTYP_C);
1440 t->tcpip_seg = 0;
1441 WM_CDTXSYNC(sc, sc->sc_txnext, 1, BUS_DMASYNC_PREWRITE);
1442
1443 sc->sc_txctx_ipcs = ipcs;
1444 sc->sc_txctx_tucs = tucs;
1445
1446 sc->sc_txnext = WM_NEXTTX(sc, sc->sc_txnext);
1447 txs->txs_ndesc++;
1448 }
1449
1450 *cmdp = WTX_CMD_DEXT | WTX_DTYP_D;
1451 *fieldsp = fields;
1452
1453 return (0);
1454 }
1455
1456 static void
1457 wm_dump_mbuf_chain(struct wm_softc *sc, struct mbuf *m0)
1458 {
1459 struct mbuf *m;
1460 int i;
1461
1462 printf("%s: mbuf chain:\n", sc->sc_dev.dv_xname);
1463 for (m = m0, i = 0; m != NULL; m = m->m_next, i++)
1464 printf("\tm_data = %p, m_len = %d, m_flags = 0x%08x\n",
1465 m->m_data, m->m_len, m->m_flags);
1466 printf("\t%d mbuf%s in chain\n", i, i == 1 ? "" : "s");
1467 }
1468
1469 /*
1470 * wm_82547_txfifo_stall:
1471 *
1472 * Callout used to wait for the 82547 Tx FIFO to drain,
1473 * reset the FIFO pointers, and restart packet transmission.
1474 */
1475 static void
1476 wm_82547_txfifo_stall(void *arg)
1477 {
1478 struct wm_softc *sc = arg;
1479 int s;
1480
1481 s = splnet();
1482
1483 if (sc->sc_txfifo_stall) {
1484 if (CSR_READ(sc, WMREG_TDT) == CSR_READ(sc, WMREG_TDH) &&
1485 CSR_READ(sc, WMREG_TDFT) == CSR_READ(sc, WMREG_TDFH) &&
1486 CSR_READ(sc, WMREG_TDFTS) == CSR_READ(sc, WMREG_TDFHS)) {
1487 /*
1488 * Packets have drained. Stop transmitter, reset
1489 * FIFO pointers, restart transmitter, and kick
1490 * the packet queue.
1491 */
1492 uint32_t tctl = CSR_READ(sc, WMREG_TCTL);
1493 CSR_WRITE(sc, WMREG_TCTL, tctl & ~TCTL_EN);
1494 CSR_WRITE(sc, WMREG_TDFT, sc->sc_txfifo_addr);
1495 CSR_WRITE(sc, WMREG_TDFH, sc->sc_txfifo_addr);
1496 CSR_WRITE(sc, WMREG_TDFTS, sc->sc_txfifo_addr);
1497 CSR_WRITE(sc, WMREG_TDFHS, sc->sc_txfifo_addr);
1498 CSR_WRITE(sc, WMREG_TCTL, tctl);
1499 CSR_WRITE_FLUSH(sc);
1500
1501 sc->sc_txfifo_head = 0;
1502 sc->sc_txfifo_stall = 0;
1503 wm_start(&sc->sc_ethercom.ec_if);
1504 } else {
1505 /*
1506 * Still waiting for packets to drain; try again in
1507 * another tick.
1508 */
1509 callout_schedule(&sc->sc_txfifo_ch, 1);
1510 }
1511 }
1512
1513 splx(s);
1514 }
1515
1516 /*
1517 * wm_82547_txfifo_bugchk:
1518 *
1519 * Check for bug condition in the 82547 Tx FIFO. We need to
1520 * prevent enqueueing a packet that would wrap around the end
1521 * if the Tx FIFO ring buffer, otherwise the chip will croak.
1522 *
1523 * We do this by checking the amount of space before the end
1524 * of the Tx FIFO buffer. If the packet will not fit, we "stall"
1525 * the Tx FIFO, wait for all remaining packets to drain, reset
1526 * the internal FIFO pointers to the beginning, and restart
1527 * transmission on the interface.
1528 */
1529 #define WM_FIFO_HDR 0x10
1530 #define WM_82547_PAD_LEN 0x3e0
1531 static int
1532 wm_82547_txfifo_bugchk(struct wm_softc *sc, struct mbuf *m0)
1533 {
1534 int space = sc->sc_txfifo_size - sc->sc_txfifo_head;
1535 int len = roundup(m0->m_pkthdr.len + WM_FIFO_HDR, WM_FIFO_HDR);
1536
1537 /* Just return if already stalled. */
1538 if (sc->sc_txfifo_stall)
1539 return (1);
1540
1541 if (sc->sc_mii.mii_media_active & IFM_FDX) {
1542 /* Stall only occurs in half-duplex mode. */
1543 goto send_packet;
1544 }
1545
1546 if (len >= WM_82547_PAD_LEN + space) {
1547 sc->sc_txfifo_stall = 1;
1548 callout_schedule(&sc->sc_txfifo_ch, 1);
1549 return (1);
1550 }
1551
1552 send_packet:
1553 sc->sc_txfifo_head += len;
1554 if (sc->sc_txfifo_head >= sc->sc_txfifo_size)
1555 sc->sc_txfifo_head -= sc->sc_txfifo_size;
1556
1557 return (0);
1558 }
1559
1560 /*
1561 * wm_start: [ifnet interface function]
1562 *
1563 * Start packet transmission on the interface.
1564 */
1565 static void
1566 wm_start(struct ifnet *ifp)
1567 {
1568 struct wm_softc *sc = ifp->if_softc;
1569 struct mbuf *m0;
1570 #if 0 /* XXXJRT */
1571 struct m_tag *mtag;
1572 #endif
1573 struct wm_txsoft *txs;
1574 bus_dmamap_t dmamap;
1575 int error, nexttx, lasttx = -1, ofree, seg, segs_needed;
1576 bus_addr_t curaddr;
1577 bus_size_t seglen, curlen;
1578 uint32_t cksumcmd;
1579 uint8_t cksumfields;
1580
1581 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
1582 return;
1583
1584 /*
1585 * Remember the previous number of free descriptors.
1586 */
1587 ofree = sc->sc_txfree;
1588
1589 /*
1590 * Loop through the send queue, setting up transmit descriptors
1591 * until we drain the queue, or use up all available transmit
1592 * descriptors.
1593 */
1594 for (;;) {
1595 /* Grab a packet off the queue. */
1596 IFQ_POLL(&ifp->if_snd, m0);
1597 if (m0 == NULL)
1598 break;
1599
1600 DPRINTF(WM_DEBUG_TX,
1601 ("%s: TX: have packet to transmit: %p\n",
1602 sc->sc_dev.dv_xname, m0));
1603
1604 /* Get a work queue entry. */
1605 if (sc->sc_txsfree < WM_TXQUEUE_GC(sc)) {
1606 wm_txintr(sc);
1607 if (sc->sc_txsfree == 0) {
1608 DPRINTF(WM_DEBUG_TX,
1609 ("%s: TX: no free job descriptors\n",
1610 sc->sc_dev.dv_xname));
1611 WM_EVCNT_INCR(&sc->sc_ev_txsstall);
1612 break;
1613 }
1614 }
1615
1616 txs = &sc->sc_txsoft[sc->sc_txsnext];
1617 dmamap = txs->txs_dmamap;
1618
1619 /*
1620 * Load the DMA map. If this fails, the packet either
1621 * didn't fit in the allotted number of segments, or we
1622 * were short on resources. For the too-many-segments
1623 * case, we simply report an error and drop the packet,
1624 * since we can't sanely copy a jumbo packet to a single
1625 * buffer.
1626 */
1627 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
1628 BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1629 if (error) {
1630 if (error == EFBIG) {
1631 WM_EVCNT_INCR(&sc->sc_ev_txdrop);
1632 printf("%s: Tx packet consumes too many "
1633 "DMA segments, dropping...\n",
1634 sc->sc_dev.dv_xname);
1635 IFQ_DEQUEUE(&ifp->if_snd, m0);
1636 wm_dump_mbuf_chain(sc, m0);
1637 m_freem(m0);
1638 continue;
1639 }
1640 /*
1641 * Short on resources, just stop for now.
1642 */
1643 DPRINTF(WM_DEBUG_TX,
1644 ("%s: TX: dmamap load failed: %d\n",
1645 sc->sc_dev.dv_xname, error));
1646 break;
1647 }
1648
1649 segs_needed = dmamap->dm_nsegs;
1650
1651 /*
1652 * Ensure we have enough descriptors free to describe
1653 * the packet. Note, we always reserve one descriptor
1654 * at the end of the ring due to the semantics of the
1655 * TDT register, plus one more in the event we need
1656 * to re-load checksum offload context.
1657 */
1658 if (segs_needed > sc->sc_txfree - 2) {
1659 /*
1660 * Not enough free descriptors to transmit this
1661 * packet. We haven't committed anything yet,
1662 * so just unload the DMA map, put the packet
1663 * pack on the queue, and punt. Notify the upper
1664 * layer that there are no more slots left.
1665 */
1666 DPRINTF(WM_DEBUG_TX,
1667 ("%s: TX: need %d (%) descriptors, have %d\n",
1668 sc->sc_dev.dv_xname, dmamap->dm_nsegs, segs_needed,
1669 sc->sc_txfree - 1));
1670 ifp->if_flags |= IFF_OACTIVE;
1671 bus_dmamap_unload(sc->sc_dmat, dmamap);
1672 WM_EVCNT_INCR(&sc->sc_ev_txdstall);
1673 break;
1674 }
1675
1676 /*
1677 * Check for 82547 Tx FIFO bug. We need to do this
1678 * once we know we can transmit the packet, since we
1679 * do some internal FIFO space accounting here.
1680 */
1681 if (sc->sc_type == WM_T_82547 &&
1682 wm_82547_txfifo_bugchk(sc, m0)) {
1683 DPRINTF(WM_DEBUG_TX,
1684 ("%s: TX: 82547 Tx FIFO bug detected\n",
1685 sc->sc_dev.dv_xname));
1686 ifp->if_flags |= IFF_OACTIVE;
1687 bus_dmamap_unload(sc->sc_dmat, dmamap);
1688 WM_EVCNT_INCR(&sc->sc_ev_txfifo_stall);
1689 break;
1690 }
1691
1692 IFQ_DEQUEUE(&ifp->if_snd, m0);
1693
1694 /*
1695 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1696 */
1697
1698 DPRINTF(WM_DEBUG_TX,
1699 ("%s: TX: packet has %d (%d) DMA segments\n",
1700 sc->sc_dev.dv_xname, dmamap->dm_nsegs, segs_needed));
1701
1702 WM_EVCNT_INCR(&sc->sc_ev_txseg[dmamap->dm_nsegs - 1]);
1703
1704 /*
1705 * Store a pointer to the packet so that we can free it
1706 * later.
1707 *
1708 * Initially, we consider the number of descriptors the
1709 * packet uses the number of DMA segments. This may be
1710 * incremented by 1 if we do checksum offload (a descriptor
1711 * is used to set the checksum context).
1712 */
1713 txs->txs_mbuf = m0;
1714 txs->txs_firstdesc = sc->sc_txnext;
1715 txs->txs_ndesc = segs_needed;
1716
1717 /*
1718 * Set up checksum offload parameters for
1719 * this packet.
1720 */
1721 if (m0->m_pkthdr.csum_flags &
1722 (M_CSUM_IPv4|M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1723 if (wm_tx_cksum(sc, txs, &cksumcmd,
1724 &cksumfields) != 0) {
1725 /* Error message already displayed. */
1726 bus_dmamap_unload(sc->sc_dmat, dmamap);
1727 continue;
1728 }
1729 } else {
1730 cksumcmd = 0;
1731 cksumfields = 0;
1732 }
1733
1734 cksumcmd |= WTX_CMD_IDE;
1735
1736 /* Sync the DMA map. */
1737 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
1738 BUS_DMASYNC_PREWRITE);
1739
1740 /*
1741 * Initialize the transmit descriptor.
1742 */
1743 for (nexttx = sc->sc_txnext, seg = 0;
1744 seg < dmamap->dm_nsegs; seg++) {
1745 for (seglen = dmamap->dm_segs[seg].ds_len,
1746 curaddr = dmamap->dm_segs[seg].ds_addr;
1747 seglen != 0;
1748 curaddr += curlen, seglen -= curlen,
1749 nexttx = WM_NEXTTX(sc, nexttx)) {
1750 curlen = seglen;
1751
1752 wm_set_dma_addr(
1753 &sc->sc_txdescs[nexttx].wtx_addr,
1754 curaddr);
1755 sc->sc_txdescs[nexttx].wtx_cmdlen =
1756 htole32(cksumcmd | curlen);
1757 sc->sc_txdescs[nexttx].wtx_fields.wtxu_status =
1758 0;
1759 sc->sc_txdescs[nexttx].wtx_fields.wtxu_options =
1760 cksumfields;
1761 sc->sc_txdescs[nexttx].wtx_fields.wtxu_vlan = 0;
1762 lasttx = nexttx;
1763
1764 DPRINTF(WM_DEBUG_TX,
1765 ("%s: TX: desc %d: low 0x%08x, "
1766 "len 0x%04x\n",
1767 sc->sc_dev.dv_xname, nexttx,
1768 curaddr & 0xffffffffU, curlen, curlen));
1769 }
1770 }
1771
1772 KASSERT(lasttx != -1);
1773
1774 /*
1775 * Set up the command byte on the last descriptor of
1776 * the packet. If we're in the interrupt delay window,
1777 * delay the interrupt.
1778 */
1779 sc->sc_txdescs[lasttx].wtx_cmdlen |=
1780 htole32(WTX_CMD_EOP | WTX_CMD_IFCS | WTX_CMD_RS);
1781
1782 #if 0 /* XXXJRT */
1783 /*
1784 * If VLANs are enabled and the packet has a VLAN tag, set
1785 * up the descriptor to encapsulate the packet for us.
1786 *
1787 * This is only valid on the last descriptor of the packet.
1788 */
1789 if (sc->sc_ethercom.ec_nvlans != 0 &&
1790 (mtag = m_tag_find(m0, PACKET_TAG_VLAN, NULL)) != NULL) {
1791 sc->sc_txdescs[lasttx].wtx_cmdlen |=
1792 htole32(WTX_CMD_VLE);
1793 sc->sc_txdescs[lasttx].wtx_fields.wtxu_vlan
1794 = htole16(*(u_int *)(mtag + 1) & 0xffff);
1795 }
1796 #endif /* XXXJRT */
1797
1798 txs->txs_lastdesc = lasttx;
1799
1800 DPRINTF(WM_DEBUG_TX,
1801 ("%s: TX: desc %d: cmdlen 0x%08x\n", sc->sc_dev.dv_xname,
1802 lasttx, le32toh(sc->sc_txdescs[lasttx].wtx_cmdlen)));
1803
1804 /* Sync the descriptors we're using. */
1805 WM_CDTXSYNC(sc, sc->sc_txnext, txs->txs_ndesc,
1806 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1807
1808 /* Give the packet to the chip. */
1809 CSR_WRITE(sc, sc->sc_tdt_reg, nexttx);
1810
1811 DPRINTF(WM_DEBUG_TX,
1812 ("%s: TX: TDT -> %d\n", sc->sc_dev.dv_xname, nexttx));
1813
1814 DPRINTF(WM_DEBUG_TX,
1815 ("%s: TX: finished transmitting packet, job %d\n",
1816 sc->sc_dev.dv_xname, sc->sc_txsnext));
1817
1818 /* Advance the tx pointer. */
1819 sc->sc_txfree -= txs->txs_ndesc;
1820 sc->sc_txnext = nexttx;
1821
1822 sc->sc_txsfree--;
1823 sc->sc_txsnext = WM_NEXTTXS(sc, sc->sc_txsnext);
1824
1825 #if NBPFILTER > 0
1826 /* Pass the packet to any BPF listeners. */
1827 if (ifp->if_bpf)
1828 bpf_mtap(ifp->if_bpf, m0);
1829 #endif /* NBPFILTER > 0 */
1830 }
1831
1832 if (sc->sc_txsfree == 0 || sc->sc_txfree <= 2) {
1833 /* No more slots; notify upper layer. */
1834 ifp->if_flags |= IFF_OACTIVE;
1835 }
1836
1837 if (sc->sc_txfree != ofree) {
1838 /* Set a watchdog timer in case the chip flakes out. */
1839 ifp->if_timer = 5;
1840 }
1841 }
1842
1843 /*
1844 * wm_watchdog: [ifnet interface function]
1845 *
1846 * Watchdog timer handler.
1847 */
1848 static void
1849 wm_watchdog(struct ifnet *ifp)
1850 {
1851 struct wm_softc *sc = ifp->if_softc;
1852
1853 /*
1854 * Since we're using delayed interrupts, sweep up
1855 * before we report an error.
1856 */
1857 wm_txintr(sc);
1858
1859 if (sc->sc_txfree != WM_NTXDESC(sc)) {
1860 printf("%s: device timeout (txfree %d txsfree %d txnext %d)\n",
1861 sc->sc_dev.dv_xname, sc->sc_txfree, sc->sc_txsfree,
1862 sc->sc_txnext);
1863 ifp->if_oerrors++;
1864
1865 /* Reset the interface. */
1866 (void) wm_init(ifp);
1867 }
1868
1869 /* Try to get more packets going. */
1870 wm_start(ifp);
1871 }
1872
1873 /*
1874 * wm_ioctl: [ifnet interface function]
1875 *
1876 * Handle control requests from the operator.
1877 */
1878 static int
1879 wm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1880 {
1881 struct wm_softc *sc = ifp->if_softc;
1882 struct ifreq *ifr = (struct ifreq *) data;
1883 int s, error;
1884
1885 s = splnet();
1886
1887 switch (cmd) {
1888 case SIOCSIFMEDIA:
1889 case SIOCGIFMEDIA:
1890 /* Flow control requires full-duplex mode. */
1891 if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
1892 (ifr->ifr_media & IFM_FDX) == 0)
1893 ifr->ifr_media &= ~IFM_ETH_FMASK;
1894 if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
1895 if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
1896 /* We can do both TXPAUSE and RXPAUSE. */
1897 ifr->ifr_media |=
1898 IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
1899 }
1900 sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
1901 }
1902 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1903 break;
1904 default:
1905 error = ether_ioctl(ifp, cmd, data);
1906 if (error == ENETRESET) {
1907 /*
1908 * Multicast list has changed; set the hardware filter
1909 * accordingly.
1910 */
1911 wm_set_filter(sc);
1912 error = 0;
1913 }
1914 break;
1915 }
1916
1917 /* Try to get more packets going. */
1918 wm_start(ifp);
1919
1920 splx(s);
1921 return (error);
1922 }
1923
1924 /*
1925 * wm_intr:
1926 *
1927 * Interrupt service routine.
1928 */
1929 static int
1930 wm_intr(void *arg)
1931 {
1932 struct wm_softc *sc = arg;
1933 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1934 uint32_t icr;
1935 int wantinit, handled = 0;
1936
1937 for (wantinit = 0; wantinit == 0;) {
1938 icr = CSR_READ(sc, WMREG_ICR);
1939 if ((icr & sc->sc_icr) == 0)
1940 break;
1941
1942 #if 0 /*NRND > 0*/
1943 if (RND_ENABLED(&sc->rnd_source))
1944 rnd_add_uint32(&sc->rnd_source, icr);
1945 #endif
1946
1947 handled = 1;
1948
1949 #if defined(WM_DEBUG) || defined(WM_EVENT_COUNTERS)
1950 if (icr & (ICR_RXDMT0|ICR_RXT0)) {
1951 DPRINTF(WM_DEBUG_RX,
1952 ("%s: RX: got Rx intr 0x%08x\n",
1953 sc->sc_dev.dv_xname,
1954 icr & (ICR_RXDMT0|ICR_RXT0)));
1955 WM_EVCNT_INCR(&sc->sc_ev_rxintr);
1956 }
1957 #endif
1958 wm_rxintr(sc);
1959
1960 #if defined(WM_DEBUG) || defined(WM_EVENT_COUNTERS)
1961 if (icr & ICR_TXDW) {
1962 DPRINTF(WM_DEBUG_TX,
1963 ("%s: TX: got TXDW interrupt\n",
1964 sc->sc_dev.dv_xname));
1965 WM_EVCNT_INCR(&sc->sc_ev_txdw);
1966 }
1967 #endif
1968 wm_txintr(sc);
1969
1970 if (icr & (ICR_LSC|ICR_RXSEQ|ICR_RXCFG)) {
1971 WM_EVCNT_INCR(&sc->sc_ev_linkintr);
1972 wm_linkintr(sc, icr);
1973 }
1974
1975 if (icr & ICR_RXO) {
1976 printf("%s: Receive overrun\n", sc->sc_dev.dv_xname);
1977 wantinit = 1;
1978 }
1979 }
1980
1981 if (handled) {
1982 if (wantinit)
1983 wm_init(ifp);
1984
1985 /* Try to get more packets going. */
1986 wm_start(ifp);
1987 }
1988
1989 return (handled);
1990 }
1991
1992 /*
1993 * wm_txintr:
1994 *
1995 * Helper; handle transmit interrupts.
1996 */
1997 static void
1998 wm_txintr(struct wm_softc *sc)
1999 {
2000 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2001 struct wm_txsoft *txs;
2002 uint8_t status;
2003 int i;
2004
2005 ifp->if_flags &= ~IFF_OACTIVE;
2006
2007 /*
2008 * Go through the Tx list and free mbufs for those
2009 * frames which have been transmitted.
2010 */
2011 for (i = sc->sc_txsdirty; sc->sc_txsfree != WM_TXQUEUELEN(sc);
2012 i = WM_NEXTTXS(sc, i), sc->sc_txsfree++) {
2013 txs = &sc->sc_txsoft[i];
2014
2015 DPRINTF(WM_DEBUG_TX,
2016 ("%s: TX: checking job %d\n", sc->sc_dev.dv_xname, i));
2017
2018 WM_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndesc,
2019 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2020
2021 status =
2022 sc->sc_txdescs[txs->txs_lastdesc].wtx_fields.wtxu_status;
2023 if ((status & WTX_ST_DD) == 0) {
2024 WM_CDTXSYNC(sc, txs->txs_lastdesc, 1,
2025 BUS_DMASYNC_PREREAD);
2026 break;
2027 }
2028
2029 DPRINTF(WM_DEBUG_TX,
2030 ("%s: TX: job %d done: descs %d..%d\n",
2031 sc->sc_dev.dv_xname, i, txs->txs_firstdesc,
2032 txs->txs_lastdesc));
2033
2034 /*
2035 * XXX We should probably be using the statistics
2036 * XXX registers, but I don't know if they exist
2037 * XXX on chips before the i82544.
2038 */
2039
2040 #ifdef WM_EVENT_COUNTERS
2041 if (status & WTX_ST_TU)
2042 WM_EVCNT_INCR(&sc->sc_ev_tu);
2043 #endif /* WM_EVENT_COUNTERS */
2044
2045 if (status & (WTX_ST_EC|WTX_ST_LC)) {
2046 ifp->if_oerrors++;
2047 if (status & WTX_ST_LC)
2048 printf("%s: late collision\n",
2049 sc->sc_dev.dv_xname);
2050 else if (status & WTX_ST_EC) {
2051 ifp->if_collisions += 16;
2052 printf("%s: excessive collisions\n",
2053 sc->sc_dev.dv_xname);
2054 }
2055 } else
2056 ifp->if_opackets++;
2057
2058 sc->sc_txfree += txs->txs_ndesc;
2059 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
2060 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2061 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2062 m_freem(txs->txs_mbuf);
2063 txs->txs_mbuf = NULL;
2064 }
2065
2066 /* Update the dirty transmit buffer pointer. */
2067 sc->sc_txsdirty = i;
2068 DPRINTF(WM_DEBUG_TX,
2069 ("%s: TX: txsdirty -> %d\n", sc->sc_dev.dv_xname, i));
2070
2071 /*
2072 * If there are no more pending transmissions, cancel the watchdog
2073 * timer.
2074 */
2075 if (sc->sc_txsfree == WM_TXQUEUELEN(sc))
2076 ifp->if_timer = 0;
2077 }
2078
2079 /*
2080 * wm_rxintr:
2081 *
2082 * Helper; handle receive interrupts.
2083 */
2084 static void
2085 wm_rxintr(struct wm_softc *sc)
2086 {
2087 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2088 struct wm_rxsoft *rxs;
2089 struct mbuf *m;
2090 int i, len;
2091 uint8_t status, errors;
2092
2093 for (i = sc->sc_rxptr;; i = WM_NEXTRX(i)) {
2094 rxs = &sc->sc_rxsoft[i];
2095
2096 DPRINTF(WM_DEBUG_RX,
2097 ("%s: RX: checking descriptor %d\n",
2098 sc->sc_dev.dv_xname, i));
2099
2100 WM_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2101
2102 status = sc->sc_rxdescs[i].wrx_status;
2103 errors = sc->sc_rxdescs[i].wrx_errors;
2104 len = le16toh(sc->sc_rxdescs[i].wrx_len);
2105
2106 if ((status & WRX_ST_DD) == 0) {
2107 /*
2108 * We have processed all of the receive descriptors.
2109 */
2110 WM_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD);
2111 break;
2112 }
2113
2114 if (__predict_false(sc->sc_rxdiscard)) {
2115 DPRINTF(WM_DEBUG_RX,
2116 ("%s: RX: discarding contents of descriptor %d\n",
2117 sc->sc_dev.dv_xname, i));
2118 WM_INIT_RXDESC(sc, i);
2119 if (status & WRX_ST_EOP) {
2120 /* Reset our state. */
2121 DPRINTF(WM_DEBUG_RX,
2122 ("%s: RX: resetting rxdiscard -> 0\n",
2123 sc->sc_dev.dv_xname));
2124 sc->sc_rxdiscard = 0;
2125 }
2126 continue;
2127 }
2128
2129 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2130 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
2131
2132 m = rxs->rxs_mbuf;
2133
2134 /*
2135 * Add a new receive buffer to the ring.
2136 */
2137 if (wm_add_rxbuf(sc, i) != 0) {
2138 /*
2139 * Failed, throw away what we've done so
2140 * far, and discard the rest of the packet.
2141 */
2142 ifp->if_ierrors++;
2143 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2144 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
2145 WM_INIT_RXDESC(sc, i);
2146 if ((status & WRX_ST_EOP) == 0)
2147 sc->sc_rxdiscard = 1;
2148 if (sc->sc_rxhead != NULL)
2149 m_freem(sc->sc_rxhead);
2150 WM_RXCHAIN_RESET(sc);
2151 DPRINTF(WM_DEBUG_RX,
2152 ("%s: RX: Rx buffer allocation failed, "
2153 "dropping packet%s\n", sc->sc_dev.dv_xname,
2154 sc->sc_rxdiscard ? " (discard)" : ""));
2155 continue;
2156 }
2157
2158 WM_RXCHAIN_LINK(sc, m);
2159
2160 m->m_len = len;
2161
2162 DPRINTF(WM_DEBUG_RX,
2163 ("%s: RX: buffer at %p len %d\n",
2164 sc->sc_dev.dv_xname, m->m_data, len));
2165
2166 /*
2167 * If this is not the end of the packet, keep
2168 * looking.
2169 */
2170 if ((status & WRX_ST_EOP) == 0) {
2171 sc->sc_rxlen += len;
2172 DPRINTF(WM_DEBUG_RX,
2173 ("%s: RX: not yet EOP, rxlen -> %d\n",
2174 sc->sc_dev.dv_xname, sc->sc_rxlen));
2175 continue;
2176 }
2177
2178 /*
2179 * Okay, we have the entire packet now...
2180 */
2181 *sc->sc_rxtailp = NULL;
2182 m = sc->sc_rxhead;
2183 len += sc->sc_rxlen;
2184
2185 WM_RXCHAIN_RESET(sc);
2186
2187 DPRINTF(WM_DEBUG_RX,
2188 ("%s: RX: have entire packet, len -> %d\n",
2189 sc->sc_dev.dv_xname, len));
2190
2191 /*
2192 * If an error occurred, update stats and drop the packet.
2193 */
2194 if (errors &
2195 (WRX_ER_CE|WRX_ER_SE|WRX_ER_SEQ|WRX_ER_CXE|WRX_ER_RXE)) {
2196 ifp->if_ierrors++;
2197 if (errors & WRX_ER_SE)
2198 printf("%s: symbol error\n",
2199 sc->sc_dev.dv_xname);
2200 else if (errors & WRX_ER_SEQ)
2201 printf("%s: receive sequence error\n",
2202 sc->sc_dev.dv_xname);
2203 else if (errors & WRX_ER_CE)
2204 printf("%s: CRC error\n",
2205 sc->sc_dev.dv_xname);
2206 m_freem(m);
2207 continue;
2208 }
2209
2210 /*
2211 * No errors. Receive the packet.
2212 *
2213 * Note, we have configured the chip to include the
2214 * CRC with every packet.
2215 */
2216 m->m_flags |= M_HASFCS;
2217 m->m_pkthdr.rcvif = ifp;
2218 m->m_pkthdr.len = len;
2219
2220 #if 0 /* XXXJRT */
2221 /*
2222 * If VLANs are enabled, VLAN packets have been unwrapped
2223 * for us. Associate the tag with the packet.
2224 */
2225 if (sc->sc_ethercom.ec_nvlans != 0 &&
2226 (status & WRX_ST_VP) != 0) {
2227 struct m_tag *vtag;
2228
2229 vtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
2230 M_NOWAIT);
2231 if (vtag == NULL) {
2232 ifp->if_ierrors++;
2233 printf("%s: unable to allocate VLAN tag\n",
2234 sc->sc_dev.dv_xname);
2235 m_freem(m);
2236 continue;
2237 }
2238
2239 *(u_int *)(vtag + 1) =
2240 le16toh(sc->sc_rxdescs[i].wrx_special);
2241 }
2242 #endif /* XXXJRT */
2243
2244 /*
2245 * Set up checksum info for this packet.
2246 */
2247 if (status & WRX_ST_IPCS) {
2248 WM_EVCNT_INCR(&sc->sc_ev_rxipsum);
2249 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
2250 if (errors & WRX_ER_IPE)
2251 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
2252 }
2253 if (status & WRX_ST_TCPCS) {
2254 /*
2255 * Note: we don't know if this was TCP or UDP,
2256 * so we just set both bits, and expect the
2257 * upper layers to deal.
2258 */
2259 WM_EVCNT_INCR(&sc->sc_ev_rxtusum);
2260 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4|M_CSUM_UDPv4;
2261 if (errors & WRX_ER_TCPE)
2262 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
2263 }
2264
2265 ifp->if_ipackets++;
2266
2267 #if NBPFILTER > 0
2268 /* Pass this up to any BPF listeners. */
2269 if (ifp->if_bpf)
2270 bpf_mtap(ifp->if_bpf, m);
2271 #endif /* NBPFILTER > 0 */
2272
2273 /* Pass it on. */
2274 (*ifp->if_input)(ifp, m);
2275 }
2276
2277 /* Update the receive pointer. */
2278 sc->sc_rxptr = i;
2279
2280 DPRINTF(WM_DEBUG_RX,
2281 ("%s: RX: rxptr -> %d\n", sc->sc_dev.dv_xname, i));
2282 }
2283
2284 /*
2285 * wm_linkintr:
2286 *
2287 * Helper; handle link interrupts.
2288 */
2289 static void
2290 wm_linkintr(struct wm_softc *sc, uint32_t icr)
2291 {
2292 uint32_t status;
2293
2294 /*
2295 * If we get a link status interrupt on a 1000BASE-T
2296 * device, just fall into the normal MII tick path.
2297 */
2298 if (sc->sc_flags & WM_F_HAS_MII) {
2299 if (icr & ICR_LSC) {
2300 DPRINTF(WM_DEBUG_LINK,
2301 ("%s: LINK: LSC -> mii_tick\n",
2302 sc->sc_dev.dv_xname));
2303 mii_tick(&sc->sc_mii);
2304 } else if (icr & ICR_RXSEQ) {
2305 DPRINTF(WM_DEBUG_LINK,
2306 ("%s: LINK Receive sequence error\n",
2307 sc->sc_dev.dv_xname));
2308 }
2309 return;
2310 }
2311
2312 /*
2313 * If we are now receiving /C/, check for link again in
2314 * a couple of link clock ticks.
2315 */
2316 if (icr & ICR_RXCFG) {
2317 DPRINTF(WM_DEBUG_LINK, ("%s: LINK: receiving /C/\n",
2318 sc->sc_dev.dv_xname));
2319 sc->sc_tbi_anstate = 2;
2320 }
2321
2322 if (icr & ICR_LSC) {
2323 status = CSR_READ(sc, WMREG_STATUS);
2324 if (status & STATUS_LU) {
2325 DPRINTF(WM_DEBUG_LINK, ("%s: LINK: LSC -> up %s\n",
2326 sc->sc_dev.dv_xname,
2327 (status & STATUS_FD) ? "FDX" : "HDX"));
2328 sc->sc_tctl &= ~TCTL_COLD(0x3ff);
2329 sc->sc_fcrtl &= ~FCRTL_XONE;
2330 if (status & STATUS_FD)
2331 sc->sc_tctl |=
2332 TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
2333 else
2334 sc->sc_tctl |=
2335 TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
2336 if (CSR_READ(sc, WMREG_CTRL) & CTRL_TFCE)
2337 sc->sc_fcrtl |= FCRTL_XONE;
2338 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
2339 CSR_WRITE(sc, (sc->sc_type < WM_T_82543) ?
2340 WMREG_OLD_FCRTL : WMREG_FCRTL,
2341 sc->sc_fcrtl);
2342 sc->sc_tbi_linkup = 1;
2343 } else {
2344 DPRINTF(WM_DEBUG_LINK, ("%s: LINK: LSC -> down\n",
2345 sc->sc_dev.dv_xname));
2346 sc->sc_tbi_linkup = 0;
2347 }
2348 sc->sc_tbi_anstate = 2;
2349 wm_tbi_set_linkled(sc);
2350 } else if (icr & ICR_RXSEQ) {
2351 DPRINTF(WM_DEBUG_LINK,
2352 ("%s: LINK: Receive sequence error\n",
2353 sc->sc_dev.dv_xname));
2354 }
2355 }
2356
2357 /*
2358 * wm_tick:
2359 *
2360 * One second timer, used to check link status, sweep up
2361 * completed transmit jobs, etc.
2362 */
2363 static void
2364 wm_tick(void *arg)
2365 {
2366 struct wm_softc *sc = arg;
2367 int s;
2368
2369 s = splnet();
2370
2371 if (sc->sc_type >= WM_T_82542_2_1) {
2372 WM_EVCNT_ADD(&sc->sc_ev_rx_xon, CSR_READ(sc, WMREG_XONRXC));
2373 WM_EVCNT_ADD(&sc->sc_ev_tx_xon, CSR_READ(sc, WMREG_XONTXC));
2374 WM_EVCNT_ADD(&sc->sc_ev_rx_xoff, CSR_READ(sc, WMREG_XOFFRXC));
2375 WM_EVCNT_ADD(&sc->sc_ev_tx_xoff, CSR_READ(sc, WMREG_XOFFTXC));
2376 WM_EVCNT_ADD(&sc->sc_ev_rx_macctl, CSR_READ(sc, WMREG_FCRUC));
2377 }
2378
2379 if (sc->sc_flags & WM_F_HAS_MII)
2380 mii_tick(&sc->sc_mii);
2381 else
2382 wm_tbi_check_link(sc);
2383
2384 splx(s);
2385
2386 callout_reset(&sc->sc_tick_ch, hz, wm_tick, sc);
2387 }
2388
2389 /*
2390 * wm_reset:
2391 *
2392 * Reset the i82542 chip.
2393 */
2394 static void
2395 wm_reset(struct wm_softc *sc)
2396 {
2397 int i;
2398
2399 /*
2400 * Allocate on-chip memory according to the MTU size.
2401 * The Packet Buffer Allocation register must be written
2402 * before the chip is reset.
2403 */
2404 if (sc->sc_type < WM_T_82547) {
2405 sc->sc_pba = sc->sc_ethercom.ec_if.if_mtu > 8192 ?
2406 PBA_40K : PBA_48K;
2407 } else {
2408 sc->sc_pba = sc->sc_ethercom.ec_if.if_mtu > 8192 ?
2409 PBA_22K : PBA_30K;
2410 sc->sc_txfifo_head = 0;
2411 sc->sc_txfifo_addr = sc->sc_pba << PBA_ADDR_SHIFT;
2412 sc->sc_txfifo_size =
2413 (PBA_40K - sc->sc_pba) << PBA_BYTE_SHIFT;
2414 sc->sc_txfifo_stall = 0;
2415 }
2416 CSR_WRITE(sc, WMREG_PBA, sc->sc_pba);
2417
2418 switch (sc->sc_type) {
2419 case WM_T_82544:
2420 case WM_T_82540:
2421 case WM_T_82545:
2422 case WM_T_82546:
2423 case WM_T_82541:
2424 case WM_T_82541_2:
2425 /*
2426 * These chips have a problem with the memory-mapped
2427 * write cycle when issuing the reset, so use I/O-mapped
2428 * access, if possible.
2429 */
2430 if (sc->sc_flags & WM_F_IOH_VALID)
2431 wm_io_write(sc, WMREG_CTRL, CTRL_RST);
2432 else
2433 CSR_WRITE(sc, WMREG_CTRL, CTRL_RST);
2434 break;
2435
2436 case WM_T_82545_3:
2437 case WM_T_82546_3:
2438 /* Use the shadow control register on these chips. */
2439 CSR_WRITE(sc, WMREG_CTRL_SHADOW, CTRL_RST);
2440 break;
2441
2442 default:
2443 /* Everything else can safely use the documented method. */
2444 CSR_WRITE(sc, WMREG_CTRL, CTRL_RST);
2445 break;
2446 }
2447 delay(10000);
2448
2449 for (i = 0; i < 1000; i++) {
2450 if ((CSR_READ(sc, WMREG_CTRL) & CTRL_RST) == 0)
2451 return;
2452 delay(20);
2453 }
2454
2455 if (CSR_READ(sc, WMREG_CTRL) & CTRL_RST)
2456 printf("%s: WARNING: reset failed to complete\n",
2457 sc->sc_dev.dv_xname);
2458 }
2459
2460 /*
2461 * wm_init: [ifnet interface function]
2462 *
2463 * Initialize the interface. Must be called at splnet().
2464 */
2465 static int
2466 wm_init(struct ifnet *ifp)
2467 {
2468 struct wm_softc *sc = ifp->if_softc;
2469 struct wm_rxsoft *rxs;
2470 int i, error = 0;
2471 uint32_t reg;
2472
2473 /*
2474 * *_HDR_ALIGNED_P is constant 1 if __NO_STRICT_ALIGMENT is set.
2475 * There is a small but measurable benefit to avoiding the adjusment
2476 * of the descriptor so that the headers are aligned, for normal mtu,
2477 * on such platforms. One possibility is that the DMA itself is
2478 * slightly more efficient if the front of the entire packet (instead
2479 * of the front of the headers) is aligned.
2480 *
2481 * Note we must always set align_tweak to 0 if we are using
2482 * jumbo frames.
2483 */
2484 #ifdef __NO_STRICT_ALIGNMENT
2485 sc->sc_align_tweak = 0;
2486 #else
2487 if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN) > (MCLBYTES - 2))
2488 sc->sc_align_tweak = 0;
2489 else
2490 sc->sc_align_tweak = 2;
2491 #endif /* __NO_STRICT_ALIGNMENT */
2492
2493 /* Cancel any pending I/O. */
2494 wm_stop(ifp, 0);
2495
2496 /* Reset the chip to a known state. */
2497 wm_reset(sc);
2498
2499 /* Initialize the transmit descriptor ring. */
2500 memset(sc->sc_txdescs, 0, WM_TXDESCSIZE(sc));
2501 WM_CDTXSYNC(sc, 0, WM_NTXDESC(sc),
2502 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2503 sc->sc_txfree = WM_NTXDESC(sc);
2504 sc->sc_txnext = 0;
2505
2506 sc->sc_txctx_ipcs = 0xffffffff;
2507 sc->sc_txctx_tucs = 0xffffffff;
2508
2509 if (sc->sc_type < WM_T_82543) {
2510 CSR_WRITE(sc, WMREG_OLD_TBDAH, WM_CDTXADDR_HI(sc, 0));
2511 CSR_WRITE(sc, WMREG_OLD_TBDAL, WM_CDTXADDR_LO(sc, 0));
2512 CSR_WRITE(sc, WMREG_OLD_TDLEN, WM_TXDESCSIZE(sc));
2513 CSR_WRITE(sc, WMREG_OLD_TDH, 0);
2514 CSR_WRITE(sc, WMREG_OLD_TDT, 0);
2515 CSR_WRITE(sc, WMREG_OLD_TIDV, 128);
2516 } else {
2517 CSR_WRITE(sc, WMREG_TBDAH, WM_CDTXADDR_HI(sc, 0));
2518 CSR_WRITE(sc, WMREG_TBDAL, WM_CDTXADDR_LO(sc, 0));
2519 CSR_WRITE(sc, WMREG_TDLEN, WM_TXDESCSIZE(sc));
2520 CSR_WRITE(sc, WMREG_TDH, 0);
2521 CSR_WRITE(sc, WMREG_TDT, 0);
2522 CSR_WRITE(sc, WMREG_TIDV, 128);
2523
2524 CSR_WRITE(sc, WMREG_TXDCTL, TXDCTL_PTHRESH(0) |
2525 TXDCTL_HTHRESH(0) | TXDCTL_WTHRESH(0));
2526 CSR_WRITE(sc, WMREG_RXDCTL, RXDCTL_PTHRESH(0) |
2527 RXDCTL_HTHRESH(0) | RXDCTL_WTHRESH(1));
2528 }
2529 CSR_WRITE(sc, WMREG_TQSA_LO, 0);
2530 CSR_WRITE(sc, WMREG_TQSA_HI, 0);
2531
2532 /* Initialize the transmit job descriptors. */
2533 for (i = 0; i < WM_TXQUEUELEN(sc); i++)
2534 sc->sc_txsoft[i].txs_mbuf = NULL;
2535 sc->sc_txsfree = WM_TXQUEUELEN(sc);
2536 sc->sc_txsnext = 0;
2537 sc->sc_txsdirty = 0;
2538
2539 /*
2540 * Initialize the receive descriptor and receive job
2541 * descriptor rings.
2542 */
2543 if (sc->sc_type < WM_T_82543) {
2544 CSR_WRITE(sc, WMREG_OLD_RDBAH0, WM_CDRXADDR_HI(sc, 0));
2545 CSR_WRITE(sc, WMREG_OLD_RDBAL0, WM_CDRXADDR_LO(sc, 0));
2546 CSR_WRITE(sc, WMREG_OLD_RDLEN0, sizeof(sc->sc_rxdescs));
2547 CSR_WRITE(sc, WMREG_OLD_RDH0, 0);
2548 CSR_WRITE(sc, WMREG_OLD_RDT0, 0);
2549 CSR_WRITE(sc, WMREG_OLD_RDTR0, 28 | RDTR_FPD);
2550
2551 CSR_WRITE(sc, WMREG_OLD_RDBA1_HI, 0);
2552 CSR_WRITE(sc, WMREG_OLD_RDBA1_LO, 0);
2553 CSR_WRITE(sc, WMREG_OLD_RDLEN1, 0);
2554 CSR_WRITE(sc, WMREG_OLD_RDH1, 0);
2555 CSR_WRITE(sc, WMREG_OLD_RDT1, 0);
2556 CSR_WRITE(sc, WMREG_OLD_RDTR1, 0);
2557 } else {
2558 CSR_WRITE(sc, WMREG_RDBAH, WM_CDRXADDR_HI(sc, 0));
2559 CSR_WRITE(sc, WMREG_RDBAL, WM_CDRXADDR_LO(sc, 0));
2560 CSR_WRITE(sc, WMREG_RDLEN, sizeof(sc->sc_rxdescs));
2561 CSR_WRITE(sc, WMREG_RDH, 0);
2562 CSR_WRITE(sc, WMREG_RDT, 0);
2563 CSR_WRITE(sc, WMREG_RDTR, 28 | RDTR_FPD);
2564 }
2565 for (i = 0; i < WM_NRXDESC; i++) {
2566 rxs = &sc->sc_rxsoft[i];
2567 if (rxs->rxs_mbuf == NULL) {
2568 if ((error = wm_add_rxbuf(sc, i)) != 0) {
2569 printf("%s: unable to allocate or map rx "
2570 "buffer %d, error = %d\n",
2571 sc->sc_dev.dv_xname, i, error);
2572 /*
2573 * XXX Should attempt to run with fewer receive
2574 * XXX buffers instead of just failing.
2575 */
2576 wm_rxdrain(sc);
2577 goto out;
2578 }
2579 } else
2580 WM_INIT_RXDESC(sc, i);
2581 }
2582 sc->sc_rxptr = 0;
2583 sc->sc_rxdiscard = 0;
2584 WM_RXCHAIN_RESET(sc);
2585
2586 /*
2587 * Clear out the VLAN table -- we don't use it (yet).
2588 */
2589 CSR_WRITE(sc, WMREG_VET, 0);
2590 for (i = 0; i < WM_VLAN_TABSIZE; i++)
2591 CSR_WRITE(sc, WMREG_VFTA + (i << 2), 0);
2592
2593 /*
2594 * Set up flow-control parameters.
2595 *
2596 * XXX Values could probably stand some tuning.
2597 */
2598 CSR_WRITE(sc, WMREG_FCAL, FCAL_CONST);
2599 CSR_WRITE(sc, WMREG_FCAH, FCAH_CONST);
2600 CSR_WRITE(sc, WMREG_FCT, ETHERTYPE_FLOWCONTROL);
2601
2602 sc->sc_fcrtl = FCRTL_DFLT;
2603 if (sc->sc_type < WM_T_82543) {
2604 CSR_WRITE(sc, WMREG_OLD_FCRTH, FCRTH_DFLT);
2605 CSR_WRITE(sc, WMREG_OLD_FCRTL, sc->sc_fcrtl);
2606 } else {
2607 CSR_WRITE(sc, WMREG_FCRTH, FCRTH_DFLT);
2608 CSR_WRITE(sc, WMREG_FCRTL, sc->sc_fcrtl);
2609 }
2610 CSR_WRITE(sc, WMREG_FCTTV, FCTTV_DFLT);
2611
2612 #if 0 /* XXXJRT */
2613 /* Deal with VLAN enables. */
2614 if (sc->sc_ethercom.ec_nvlans != 0)
2615 sc->sc_ctrl |= CTRL_VME;
2616 else
2617 #endif /* XXXJRT */
2618 sc->sc_ctrl &= ~CTRL_VME;
2619
2620 /* Write the control registers. */
2621 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
2622 #if 0
2623 CSR_WRITE(sc, WMREG_CTRL_EXT, sc->sc_ctrl_ext);
2624 #endif
2625
2626 /*
2627 * Set up checksum offload parameters.
2628 */
2629 reg = CSR_READ(sc, WMREG_RXCSUM);
2630 if (ifp->if_capenable & IFCAP_CSUM_IPv4)
2631 reg |= RXCSUM_IPOFL;
2632 else
2633 reg &= ~RXCSUM_IPOFL;
2634 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4))
2635 reg |= RXCSUM_IPOFL | RXCSUM_TUOFL;
2636 else {
2637 reg &= ~RXCSUM_TUOFL;
2638 if ((ifp->if_capenable & IFCAP_CSUM_IPv4) == 0)
2639 reg &= ~RXCSUM_IPOFL;
2640 }
2641 CSR_WRITE(sc, WMREG_RXCSUM, reg);
2642
2643 /*
2644 * Set up the interrupt registers.
2645 */
2646 CSR_WRITE(sc, WMREG_IMC, 0xffffffffU);
2647 sc->sc_icr = ICR_TXDW | ICR_LSC | ICR_RXSEQ | ICR_RXDMT0 |
2648 ICR_RXO | ICR_RXT0;
2649 if ((sc->sc_flags & WM_F_HAS_MII) == 0)
2650 sc->sc_icr |= ICR_RXCFG;
2651 CSR_WRITE(sc, WMREG_IMS, sc->sc_icr);
2652
2653 /* Set up the inter-packet gap. */
2654 CSR_WRITE(sc, WMREG_TIPG, sc->sc_tipg);
2655
2656 #if 0 /* XXXJRT */
2657 /* Set the VLAN ethernetype. */
2658 CSR_WRITE(sc, WMREG_VET, ETHERTYPE_VLAN);
2659 #endif
2660
2661 /*
2662 * Set up the transmit control register; we start out with
2663 * a collision distance suitable for FDX, but update it whe
2664 * we resolve the media type.
2665 */
2666 sc->sc_tctl = TCTL_EN | TCTL_PSP | TCTL_CT(TX_COLLISION_THRESHOLD) |
2667 TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
2668 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
2669
2670 /* Set the media. */
2671 (void) (*sc->sc_mii.mii_media.ifm_change)(ifp);
2672
2673 /*
2674 * Set up the receive control register; we actually program
2675 * the register when we set the receive filter. Use multicast
2676 * address offset type 0.
2677 *
2678 * Only the i82544 has the ability to strip the incoming
2679 * CRC, so we don't enable that feature.
2680 */
2681 sc->sc_mchash_type = 0;
2682 sc->sc_rctl = RCTL_EN | RCTL_LBM_NONE | RCTL_RDMTS_1_2 | RCTL_LPE |
2683 RCTL_DPF | RCTL_MO(sc->sc_mchash_type);
2684
2685 if(MCLBYTES == 2048) {
2686 sc->sc_rctl |= RCTL_2k;
2687 } else {
2688 if(sc->sc_type >= WM_T_82543) {
2689 switch(MCLBYTES) {
2690 case 4096:
2691 sc->sc_rctl |= RCTL_BSEX | RCTL_BSEX_4k;
2692 break;
2693 case 8192:
2694 sc->sc_rctl |= RCTL_BSEX | RCTL_BSEX_8k;
2695 break;
2696 case 16384:
2697 sc->sc_rctl |= RCTL_BSEX | RCTL_BSEX_16k;
2698 break;
2699 default:
2700 panic("wm_init: MCLBYTES %d unsupported",
2701 MCLBYTES);
2702 break;
2703 }
2704 } else panic("wm_init: i82542 requires MCLBYTES = 2048");
2705 }
2706
2707 /* Set the receive filter. */
2708 wm_set_filter(sc);
2709
2710 /* Start the one second link check clock. */
2711 callout_reset(&sc->sc_tick_ch, hz, wm_tick, sc);
2712
2713 /* ...all done! */
2714 ifp->if_flags |= IFF_RUNNING;
2715 ifp->if_flags &= ~IFF_OACTIVE;
2716
2717 out:
2718 if (error)
2719 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
2720 return (error);
2721 }
2722
2723 /*
2724 * wm_rxdrain:
2725 *
2726 * Drain the receive queue.
2727 */
2728 static void
2729 wm_rxdrain(struct wm_softc *sc)
2730 {
2731 struct wm_rxsoft *rxs;
2732 int i;
2733
2734 for (i = 0; i < WM_NRXDESC; i++) {
2735 rxs = &sc->sc_rxsoft[i];
2736 if (rxs->rxs_mbuf != NULL) {
2737 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2738 m_freem(rxs->rxs_mbuf);
2739 rxs->rxs_mbuf = NULL;
2740 }
2741 }
2742 }
2743
2744 /*
2745 * wm_stop: [ifnet interface function]
2746 *
2747 * Stop transmission on the interface.
2748 */
2749 static void
2750 wm_stop(struct ifnet *ifp, int disable)
2751 {
2752 struct wm_softc *sc = ifp->if_softc;
2753 struct wm_txsoft *txs;
2754 int i;
2755
2756 /* Stop the one second clock. */
2757 callout_stop(&sc->sc_tick_ch);
2758
2759 /* Stop the 82547 Tx FIFO stall check timer. */
2760 if (sc->sc_type == WM_T_82547)
2761 callout_stop(&sc->sc_txfifo_ch);
2762
2763 if (sc->sc_flags & WM_F_HAS_MII) {
2764 /* Down the MII. */
2765 mii_down(&sc->sc_mii);
2766 }
2767
2768 /* Stop the transmit and receive processes. */
2769 CSR_WRITE(sc, WMREG_TCTL, 0);
2770 CSR_WRITE(sc, WMREG_RCTL, 0);
2771
2772 /* Release any queued transmit buffers. */
2773 for (i = 0; i < WM_TXQUEUELEN(sc); i++) {
2774 txs = &sc->sc_txsoft[i];
2775 if (txs->txs_mbuf != NULL) {
2776 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2777 m_freem(txs->txs_mbuf);
2778 txs->txs_mbuf = NULL;
2779 }
2780 }
2781
2782 if (disable)
2783 wm_rxdrain(sc);
2784
2785 /* Mark the interface as down and cancel the watchdog timer. */
2786 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2787 ifp->if_timer = 0;
2788 }
2789
2790 /*
2791 * wm_acquire_eeprom:
2792 *
2793 * Perform the EEPROM handshake required on some chips.
2794 */
2795 static int
2796 wm_acquire_eeprom(struct wm_softc *sc)
2797 {
2798 uint32_t reg;
2799 int x;
2800
2801 if (sc->sc_flags & WM_F_EEPROM_HANDSHAKE) {
2802 reg = CSR_READ(sc, WMREG_EECD);
2803
2804 /* Request EEPROM access. */
2805 reg |= EECD_EE_REQ;
2806 CSR_WRITE(sc, WMREG_EECD, reg);
2807
2808 /* ..and wait for it to be granted. */
2809 for (x = 0; x < 100; x++) {
2810 reg = CSR_READ(sc, WMREG_EECD);
2811 if (reg & EECD_EE_GNT)
2812 break;
2813 delay(5);
2814 }
2815 if ((reg & EECD_EE_GNT) == 0) {
2816 aprint_error("%s: could not acquire EEPROM GNT\n",
2817 sc->sc_dev.dv_xname);
2818 reg &= ~EECD_EE_REQ;
2819 CSR_WRITE(sc, WMREG_EECD, reg);
2820 return (1);
2821 }
2822 }
2823
2824 return (0);
2825 }
2826
2827 /*
2828 * wm_release_eeprom:
2829 *
2830 * Release the EEPROM mutex.
2831 */
2832 static void
2833 wm_release_eeprom(struct wm_softc *sc)
2834 {
2835 uint32_t reg;
2836
2837 if (sc->sc_flags & WM_F_EEPROM_HANDSHAKE) {
2838 reg = CSR_READ(sc, WMREG_EECD);
2839 reg &= ~EECD_EE_REQ;
2840 CSR_WRITE(sc, WMREG_EECD, reg);
2841 }
2842 }
2843
2844 /*
2845 * wm_eeprom_sendbits:
2846 *
2847 * Send a series of bits to the EEPROM.
2848 */
2849 static void
2850 wm_eeprom_sendbits(struct wm_softc *sc, uint32_t bits, int nbits)
2851 {
2852 uint32_t reg;
2853 int x;
2854
2855 reg = CSR_READ(sc, WMREG_EECD);
2856
2857 for (x = nbits; x > 0; x--) {
2858 if (bits & (1U << (x - 1)))
2859 reg |= EECD_DI;
2860 else
2861 reg &= ~EECD_DI;
2862 CSR_WRITE(sc, WMREG_EECD, reg);
2863 delay(2);
2864 CSR_WRITE(sc, WMREG_EECD, reg | EECD_SK);
2865 delay(2);
2866 CSR_WRITE(sc, WMREG_EECD, reg);
2867 delay(2);
2868 }
2869 }
2870
2871 /*
2872 * wm_eeprom_recvbits:
2873 *
2874 * Receive a series of bits from the EEPROM.
2875 */
2876 static void
2877 wm_eeprom_recvbits(struct wm_softc *sc, uint32_t *valp, int nbits)
2878 {
2879 uint32_t reg, val;
2880 int x;
2881
2882 reg = CSR_READ(sc, WMREG_EECD) & ~EECD_DI;
2883
2884 val = 0;
2885 for (x = nbits; x > 0; x--) {
2886 CSR_WRITE(sc, WMREG_EECD, reg | EECD_SK);
2887 delay(2);
2888 if (CSR_READ(sc, WMREG_EECD) & EECD_DO)
2889 val |= (1U << (x - 1));
2890 CSR_WRITE(sc, WMREG_EECD, reg);
2891 delay(2);
2892 }
2893 *valp = val;
2894 }
2895
2896 /*
2897 * wm_read_eeprom_uwire:
2898 *
2899 * Read a word from the EEPROM using the MicroWire protocol.
2900 */
2901 static int
2902 wm_read_eeprom_uwire(struct wm_softc *sc, int word, int wordcnt, uint16_t *data)
2903 {
2904 uint32_t reg, val;
2905 int i;
2906
2907 for (i = 0; i < wordcnt; i++) {
2908 /* Clear SK and DI. */
2909 reg = CSR_READ(sc, WMREG_EECD) & ~(EECD_SK | EECD_DI);
2910 CSR_WRITE(sc, WMREG_EECD, reg);
2911
2912 /* Set CHIP SELECT. */
2913 reg |= EECD_CS;
2914 CSR_WRITE(sc, WMREG_EECD, reg);
2915 delay(2);
2916
2917 /* Shift in the READ command. */
2918 wm_eeprom_sendbits(sc, UWIRE_OPC_READ, 3);
2919
2920 /* Shift in address. */
2921 wm_eeprom_sendbits(sc, word + i, sc->sc_ee_addrbits);
2922
2923 /* Shift out the data. */
2924 wm_eeprom_recvbits(sc, &val, 16);
2925 data[i] = val & 0xffff;
2926
2927 /* Clear CHIP SELECT. */
2928 reg = CSR_READ(sc, WMREG_EECD) & ~EECD_CS;
2929 CSR_WRITE(sc, WMREG_EECD, reg);
2930 delay(2);
2931 }
2932
2933 return (0);
2934 }
2935
2936 /*
2937 * wm_spi_eeprom_ready:
2938 *
2939 * Wait for a SPI EEPROM to be ready for commands.
2940 */
2941 static int
2942 wm_spi_eeprom_ready(struct wm_softc *sc)
2943 {
2944 uint32_t val;
2945 int usec;
2946
2947 for (usec = 0; usec < SPI_MAX_RETRIES; delay(5), usec += 5) {
2948 wm_eeprom_sendbits(sc, SPI_OPC_RDSR, 8);
2949 wm_eeprom_recvbits(sc, &val, 8);
2950 if ((val & SPI_SR_RDY) == 0)
2951 break;
2952 }
2953 if (usec >= SPI_MAX_RETRIES) {
2954 aprint_error("%s: EEPROM failed to become ready\n",
2955 sc->sc_dev.dv_xname);
2956 return (1);
2957 }
2958 return (0);
2959 }
2960
2961 /*
2962 * wm_read_eeprom_spi:
2963 *
2964 * Read a work from the EEPROM using the SPI protocol.
2965 */
2966 static int
2967 wm_read_eeprom_spi(struct wm_softc *sc, int word, int wordcnt, uint16_t *data)
2968 {
2969 uint32_t reg, val;
2970 int i;
2971 uint8_t opc;
2972
2973 /* Clear SK and CS. */
2974 reg = CSR_READ(sc, WMREG_EECD) & ~(EECD_SK | EECD_CS);
2975 CSR_WRITE(sc, WMREG_EECD, reg);
2976 delay(2);
2977
2978 if (wm_spi_eeprom_ready(sc))
2979 return (1);
2980
2981 /* Toggle CS to flush commands. */
2982 CSR_WRITE(sc, WMREG_EECD, reg | EECD_CS);
2983 delay(2);
2984 CSR_WRITE(sc, WMREG_EECD, reg);
2985 delay(2);
2986
2987 opc = SPI_OPC_READ;
2988 if (sc->sc_ee_addrbits == 8 && word >= 128)
2989 opc |= SPI_OPC_A8;
2990
2991 wm_eeprom_sendbits(sc, opc, 8);
2992 wm_eeprom_sendbits(sc, word << 1, sc->sc_ee_addrbits);
2993
2994 for (i = 0; i < wordcnt; i++) {
2995 wm_eeprom_recvbits(sc, &val, 16);
2996 data[i] = ((val >> 8) & 0xff) | ((val & 0xff) << 8);
2997 }
2998
2999 /* Raise CS and clear SK. */
3000 reg = (CSR_READ(sc, WMREG_EECD) & ~EECD_SK) | EECD_CS;
3001 CSR_WRITE(sc, WMREG_EECD, reg);
3002 delay(2);
3003
3004 return (0);
3005 }
3006
3007 /*
3008 * wm_read_eeprom:
3009 *
3010 * Read data from the serial EEPROM.
3011 */
3012 static int
3013 wm_read_eeprom(struct wm_softc *sc, int word, int wordcnt, uint16_t *data)
3014 {
3015 int rv;
3016
3017 if (wm_acquire_eeprom(sc))
3018 return (1);
3019
3020 if (sc->sc_flags & WM_F_EEPROM_SPI)
3021 rv = wm_read_eeprom_spi(sc, word, wordcnt, data);
3022 else
3023 rv = wm_read_eeprom_uwire(sc, word, wordcnt, data);
3024
3025 wm_release_eeprom(sc);
3026 return (rv);
3027 }
3028
3029 /*
3030 * wm_add_rxbuf:
3031 *
3032 * Add a receive buffer to the indiciated descriptor.
3033 */
3034 static int
3035 wm_add_rxbuf(struct wm_softc *sc, int idx)
3036 {
3037 struct wm_rxsoft *rxs = &sc->sc_rxsoft[idx];
3038 struct mbuf *m;
3039 int error;
3040
3041 MGETHDR(m, M_DONTWAIT, MT_DATA);
3042 if (m == NULL)
3043 return (ENOBUFS);
3044
3045 MCLGET(m, M_DONTWAIT);
3046 if ((m->m_flags & M_EXT) == 0) {
3047 m_freem(m);
3048 return (ENOBUFS);
3049 }
3050
3051 if (rxs->rxs_mbuf != NULL)
3052 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
3053
3054 rxs->rxs_mbuf = m;
3055
3056 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
3057 error = bus_dmamap_load_mbuf(sc->sc_dmat, rxs->rxs_dmamap, m,
3058 BUS_DMA_READ|BUS_DMA_NOWAIT);
3059 if (error) {
3060 printf("%s: unable to load rx DMA map %d, error = %d\n",
3061 sc->sc_dev.dv_xname, idx, error);
3062 panic("wm_add_rxbuf"); /* XXX XXX XXX */
3063 }
3064
3065 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
3066 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
3067
3068 WM_INIT_RXDESC(sc, idx);
3069
3070 return (0);
3071 }
3072
3073 /*
3074 * wm_set_ral:
3075 *
3076 * Set an entery in the receive address list.
3077 */
3078 static void
3079 wm_set_ral(struct wm_softc *sc, const uint8_t *enaddr, int idx)
3080 {
3081 uint32_t ral_lo, ral_hi;
3082
3083 if (enaddr != NULL) {
3084 ral_lo = enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) |
3085 (enaddr[3] << 24);
3086 ral_hi = enaddr[4] | (enaddr[5] << 8);
3087 ral_hi |= RAL_AV;
3088 } else {
3089 ral_lo = 0;
3090 ral_hi = 0;
3091 }
3092
3093 if (sc->sc_type >= WM_T_82544) {
3094 CSR_WRITE(sc, WMREG_RAL_LO(WMREG_CORDOVA_RAL_BASE, idx),
3095 ral_lo);
3096 CSR_WRITE(sc, WMREG_RAL_HI(WMREG_CORDOVA_RAL_BASE, idx),
3097 ral_hi);
3098 } else {
3099 CSR_WRITE(sc, WMREG_RAL_LO(WMREG_RAL_BASE, idx), ral_lo);
3100 CSR_WRITE(sc, WMREG_RAL_HI(WMREG_RAL_BASE, idx), ral_hi);
3101 }
3102 }
3103
3104 /*
3105 * wm_mchash:
3106 *
3107 * Compute the hash of the multicast address for the 4096-bit
3108 * multicast filter.
3109 */
3110 static uint32_t
3111 wm_mchash(struct wm_softc *sc, const uint8_t *enaddr)
3112 {
3113 static const int lo_shift[4] = { 4, 3, 2, 0 };
3114 static const int hi_shift[4] = { 4, 5, 6, 8 };
3115 uint32_t hash;
3116
3117 hash = (enaddr[4] >> lo_shift[sc->sc_mchash_type]) |
3118 (((uint16_t) enaddr[5]) << hi_shift[sc->sc_mchash_type]);
3119
3120 return (hash & 0xfff);
3121 }
3122
3123 /*
3124 * wm_set_filter:
3125 *
3126 * Set up the receive filter.
3127 */
3128 static void
3129 wm_set_filter(struct wm_softc *sc)
3130 {
3131 struct ethercom *ec = &sc->sc_ethercom;
3132 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
3133 struct ether_multi *enm;
3134 struct ether_multistep step;
3135 bus_addr_t mta_reg;
3136 uint32_t hash, reg, bit;
3137 int i;
3138
3139 if (sc->sc_type >= WM_T_82544)
3140 mta_reg = WMREG_CORDOVA_MTA;
3141 else
3142 mta_reg = WMREG_MTA;
3143
3144 sc->sc_rctl &= ~(RCTL_BAM | RCTL_UPE | RCTL_MPE);
3145
3146 if (ifp->if_flags & IFF_BROADCAST)
3147 sc->sc_rctl |= RCTL_BAM;
3148 if (ifp->if_flags & IFF_PROMISC) {
3149 sc->sc_rctl |= RCTL_UPE;
3150 goto allmulti;
3151 }
3152
3153 /*
3154 * Set the station address in the first RAL slot, and
3155 * clear the remaining slots.
3156 */
3157 wm_set_ral(sc, LLADDR(ifp->if_sadl), 0);
3158 for (i = 1; i < WM_RAL_TABSIZE; i++)
3159 wm_set_ral(sc, NULL, i);
3160
3161 /* Clear out the multicast table. */
3162 for (i = 0; i < WM_MC_TABSIZE; i++)
3163 CSR_WRITE(sc, mta_reg + (i << 2), 0);
3164
3165 ETHER_FIRST_MULTI(step, ec, enm);
3166 while (enm != NULL) {
3167 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
3168 /*
3169 * We must listen to a range of multicast addresses.
3170 * For now, just accept all multicasts, rather than
3171 * trying to set only those filter bits needed to match
3172 * the range. (At this time, the only use of address
3173 * ranges is for IP multicast routing, for which the
3174 * range is big enough to require all bits set.)
3175 */
3176 goto allmulti;
3177 }
3178
3179 hash = wm_mchash(sc, enm->enm_addrlo);
3180
3181 reg = (hash >> 5) & 0x7f;
3182 bit = hash & 0x1f;
3183
3184 hash = CSR_READ(sc, mta_reg + (reg << 2));
3185 hash |= 1U << bit;
3186
3187 /* XXX Hardware bug?? */
3188 if (sc->sc_type == WM_T_82544 && (reg & 0xe) == 1) {
3189 bit = CSR_READ(sc, mta_reg + ((reg - 1) << 2));
3190 CSR_WRITE(sc, mta_reg + (reg << 2), hash);
3191 CSR_WRITE(sc, mta_reg + ((reg - 1) << 2), bit);
3192 } else
3193 CSR_WRITE(sc, mta_reg + (reg << 2), hash);
3194
3195 ETHER_NEXT_MULTI(step, enm);
3196 }
3197
3198 ifp->if_flags &= ~IFF_ALLMULTI;
3199 goto setit;
3200
3201 allmulti:
3202 ifp->if_flags |= IFF_ALLMULTI;
3203 sc->sc_rctl |= RCTL_MPE;
3204
3205 setit:
3206 CSR_WRITE(sc, WMREG_RCTL, sc->sc_rctl);
3207 }
3208
3209 /*
3210 * wm_tbi_mediainit:
3211 *
3212 * Initialize media for use on 1000BASE-X devices.
3213 */
3214 static void
3215 wm_tbi_mediainit(struct wm_softc *sc)
3216 {
3217 const char *sep = "";
3218
3219 if (sc->sc_type < WM_T_82543)
3220 sc->sc_tipg = TIPG_WM_DFLT;
3221 else
3222 sc->sc_tipg = TIPG_LG_DFLT;
3223
3224 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, wm_tbi_mediachange,
3225 wm_tbi_mediastatus);
3226
3227 /*
3228 * SWD Pins:
3229 *
3230 * 0 = Link LED (output)
3231 * 1 = Loss Of Signal (input)
3232 */
3233 sc->sc_ctrl |= CTRL_SWDPIO(0);
3234 sc->sc_ctrl &= ~CTRL_SWDPIO(1);
3235
3236 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
3237
3238 #define ADD(ss, mm, dd) \
3239 do { \
3240 printf("%s%s", sep, ss); \
3241 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|(mm), (dd), NULL); \
3242 sep = ", "; \
3243 } while (/*CONSTCOND*/0)
3244
3245 printf("%s: ", sc->sc_dev.dv_xname);
3246 ADD("1000baseSX", IFM_1000_SX, ANAR_X_HD);
3247 ADD("1000baseSX-FDX", IFM_1000_SX|IFM_FDX, ANAR_X_FD);
3248 ADD("auto", IFM_AUTO, ANAR_X_FD|ANAR_X_HD);
3249 printf("\n");
3250
3251 #undef ADD
3252
3253 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
3254 }
3255
3256 /*
3257 * wm_tbi_mediastatus: [ifmedia interface function]
3258 *
3259 * Get the current interface media status on a 1000BASE-X device.
3260 */
3261 static void
3262 wm_tbi_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
3263 {
3264 struct wm_softc *sc = ifp->if_softc;
3265 uint32_t ctrl;
3266
3267 ifmr->ifm_status = IFM_AVALID;
3268 ifmr->ifm_active = IFM_ETHER;
3269
3270 if (sc->sc_tbi_linkup == 0) {
3271 ifmr->ifm_active |= IFM_NONE;
3272 return;
3273 }
3274
3275 ifmr->ifm_status |= IFM_ACTIVE;
3276 ifmr->ifm_active |= IFM_1000_SX;
3277 if (CSR_READ(sc, WMREG_STATUS) & STATUS_FD)
3278 ifmr->ifm_active |= IFM_FDX;
3279 ctrl = CSR_READ(sc, WMREG_CTRL);
3280 if (ctrl & CTRL_RFCE)
3281 ifmr->ifm_active |= IFM_FLOW | IFM_ETH_RXPAUSE;
3282 if (ctrl & CTRL_TFCE)
3283 ifmr->ifm_active |= IFM_FLOW | IFM_ETH_TXPAUSE;
3284 }
3285
3286 /*
3287 * wm_tbi_mediachange: [ifmedia interface function]
3288 *
3289 * Set hardware to newly-selected media on a 1000BASE-X device.
3290 */
3291 static int
3292 wm_tbi_mediachange(struct ifnet *ifp)
3293 {
3294 struct wm_softc *sc = ifp->if_softc;
3295 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3296 uint32_t status;
3297 int i;
3298
3299 sc->sc_txcw = ife->ifm_data;
3300 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO ||
3301 (sc->sc_mii.mii_media.ifm_media & IFM_FLOW) != 0)
3302 sc->sc_txcw |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM;
3303 sc->sc_txcw |= TXCW_ANE;
3304
3305 CSR_WRITE(sc, WMREG_TXCW, sc->sc_txcw);
3306 delay(10000);
3307
3308 /* NOTE: CTRL will update TFCE and RFCE automatically. */
3309
3310 sc->sc_tbi_anstate = 0;
3311
3312 if ((CSR_READ(sc, WMREG_CTRL) & CTRL_SWDPIN(1)) == 0) {
3313 /* Have signal; wait for the link to come up. */
3314 for (i = 0; i < 50; i++) {
3315 delay(10000);
3316 if (CSR_READ(sc, WMREG_STATUS) & STATUS_LU)
3317 break;
3318 }
3319
3320 status = CSR_READ(sc, WMREG_STATUS);
3321 if (status & STATUS_LU) {
3322 /* Link is up. */
3323 DPRINTF(WM_DEBUG_LINK,
3324 ("%s: LINK: set media -> link up %s\n",
3325 sc->sc_dev.dv_xname,
3326 (status & STATUS_FD) ? "FDX" : "HDX"));
3327 sc->sc_tctl &= ~TCTL_COLD(0x3ff);
3328 sc->sc_fcrtl &= ~FCRTL_XONE;
3329 if (status & STATUS_FD)
3330 sc->sc_tctl |=
3331 TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
3332 else
3333 sc->sc_tctl |=
3334 TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
3335 if (CSR_READ(sc, WMREG_CTRL) & CTRL_TFCE)
3336 sc->sc_fcrtl |= FCRTL_XONE;
3337 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
3338 CSR_WRITE(sc, (sc->sc_type < WM_T_82543) ?
3339 WMREG_OLD_FCRTL : WMREG_FCRTL,
3340 sc->sc_fcrtl);
3341 sc->sc_tbi_linkup = 1;
3342 } else {
3343 /* Link is down. */
3344 DPRINTF(WM_DEBUG_LINK,
3345 ("%s: LINK: set media -> link down\n",
3346 sc->sc_dev.dv_xname));
3347 sc->sc_tbi_linkup = 0;
3348 }
3349 } else {
3350 DPRINTF(WM_DEBUG_LINK, ("%s: LINK: set media -> no signal\n",
3351 sc->sc_dev.dv_xname));
3352 sc->sc_tbi_linkup = 0;
3353 }
3354
3355 wm_tbi_set_linkled(sc);
3356
3357 return (0);
3358 }
3359
3360 /*
3361 * wm_tbi_set_linkled:
3362 *
3363 * Update the link LED on 1000BASE-X devices.
3364 */
3365 static void
3366 wm_tbi_set_linkled(struct wm_softc *sc)
3367 {
3368
3369 if (sc->sc_tbi_linkup)
3370 sc->sc_ctrl |= CTRL_SWDPIN(0);
3371 else
3372 sc->sc_ctrl &= ~CTRL_SWDPIN(0);
3373
3374 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
3375 }
3376
3377 /*
3378 * wm_tbi_check_link:
3379 *
3380 * Check the link on 1000BASE-X devices.
3381 */
3382 static void
3383 wm_tbi_check_link(struct wm_softc *sc)
3384 {
3385 uint32_t rxcw, ctrl, status;
3386
3387 if (sc->sc_tbi_anstate == 0)
3388 return;
3389 else if (sc->sc_tbi_anstate > 1) {
3390 DPRINTF(WM_DEBUG_LINK,
3391 ("%s: LINK: anstate %d\n", sc->sc_dev.dv_xname,
3392 sc->sc_tbi_anstate));
3393 sc->sc_tbi_anstate--;
3394 return;
3395 }
3396
3397 sc->sc_tbi_anstate = 0;
3398
3399 rxcw = CSR_READ(sc, WMREG_RXCW);
3400 ctrl = CSR_READ(sc, WMREG_CTRL);
3401 status = CSR_READ(sc, WMREG_STATUS);
3402
3403 if ((status & STATUS_LU) == 0) {
3404 DPRINTF(WM_DEBUG_LINK,
3405 ("%s: LINK: checklink -> down\n", sc->sc_dev.dv_xname));
3406 sc->sc_tbi_linkup = 0;
3407 } else {
3408 DPRINTF(WM_DEBUG_LINK,
3409 ("%s: LINK: checklink -> up %s\n", sc->sc_dev.dv_xname,
3410 (status & STATUS_FD) ? "FDX" : "HDX"));
3411 sc->sc_tctl &= ~TCTL_COLD(0x3ff);
3412 sc->sc_fcrtl &= ~FCRTL_XONE;
3413 if (status & STATUS_FD)
3414 sc->sc_tctl |=
3415 TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
3416 else
3417 sc->sc_tctl |=
3418 TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
3419 if (ctrl & CTRL_TFCE)
3420 sc->sc_fcrtl |= FCRTL_XONE;
3421 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
3422 CSR_WRITE(sc, (sc->sc_type < WM_T_82543) ?
3423 WMREG_OLD_FCRTL : WMREG_FCRTL,
3424 sc->sc_fcrtl);
3425 sc->sc_tbi_linkup = 1;
3426 }
3427
3428 wm_tbi_set_linkled(sc);
3429 }
3430
3431 /*
3432 * wm_gmii_reset:
3433 *
3434 * Reset the PHY.
3435 */
3436 static void
3437 wm_gmii_reset(struct wm_softc *sc)
3438 {
3439 uint32_t reg;
3440
3441 if (sc->sc_type >= WM_T_82544) {
3442 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl | CTRL_PHY_RESET);
3443 delay(20000);
3444
3445 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
3446 delay(20000);
3447 } else {
3448 /* The PHY reset pin is active-low. */
3449 reg = CSR_READ(sc, WMREG_CTRL_EXT);
3450 reg &= ~((CTRL_EXT_SWDPIO_MASK << CTRL_EXT_SWDPIO_SHIFT) |
3451 CTRL_EXT_SWDPIN(4));
3452 reg |= CTRL_EXT_SWDPIO(4);
3453
3454 CSR_WRITE(sc, WMREG_CTRL_EXT, reg | CTRL_EXT_SWDPIN(4));
3455 delay(10);
3456
3457 CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
3458 delay(10);
3459
3460 CSR_WRITE(sc, WMREG_CTRL_EXT, reg | CTRL_EXT_SWDPIN(4));
3461 delay(10);
3462 #if 0
3463 sc->sc_ctrl_ext = reg | CTRL_EXT_SWDPIN(4);
3464 #endif
3465 }
3466 }
3467
3468 /*
3469 * wm_gmii_mediainit:
3470 *
3471 * Initialize media for use on 1000BASE-T devices.
3472 */
3473 static void
3474 wm_gmii_mediainit(struct wm_softc *sc)
3475 {
3476 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
3477
3478 /* We have MII. */
3479 sc->sc_flags |= WM_F_HAS_MII;
3480
3481 sc->sc_tipg = TIPG_1000T_DFLT;
3482
3483 /*
3484 * Let the chip set speed/duplex on its own based on
3485 * signals from the PHY.
3486 */
3487 sc->sc_ctrl |= CTRL_SLU | CTRL_ASDE;
3488 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
3489
3490 /* Initialize our media structures and probe the GMII. */
3491 sc->sc_mii.mii_ifp = ifp;
3492
3493 if (sc->sc_type >= WM_T_82544) {
3494 sc->sc_mii.mii_readreg = wm_gmii_i82544_readreg;
3495 sc->sc_mii.mii_writereg = wm_gmii_i82544_writereg;
3496 } else {
3497 sc->sc_mii.mii_readreg = wm_gmii_i82543_readreg;
3498 sc->sc_mii.mii_writereg = wm_gmii_i82543_writereg;
3499 }
3500 sc->sc_mii.mii_statchg = wm_gmii_statchg;
3501
3502 wm_gmii_reset(sc);
3503
3504 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, wm_gmii_mediachange,
3505 wm_gmii_mediastatus);
3506
3507 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
3508 MII_OFFSET_ANY, MIIF_DOPAUSE);
3509 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
3510 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
3511 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
3512 } else
3513 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
3514 }
3515
3516 /*
3517 * wm_gmii_mediastatus: [ifmedia interface function]
3518 *
3519 * Get the current interface media status on a 1000BASE-T device.
3520 */
3521 static void
3522 wm_gmii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
3523 {
3524 struct wm_softc *sc = ifp->if_softc;
3525
3526 mii_pollstat(&sc->sc_mii);
3527 ifmr->ifm_status = sc->sc_mii.mii_media_status;
3528 ifmr->ifm_active = (sc->sc_mii.mii_media_active & ~IFM_ETH_FMASK) |
3529 sc->sc_flowflags;
3530 }
3531
3532 /*
3533 * wm_gmii_mediachange: [ifmedia interface function]
3534 *
3535 * Set hardware to newly-selected media on a 1000BASE-T device.
3536 */
3537 static int
3538 wm_gmii_mediachange(struct ifnet *ifp)
3539 {
3540 struct wm_softc *sc = ifp->if_softc;
3541
3542 if (ifp->if_flags & IFF_UP)
3543 mii_mediachg(&sc->sc_mii);
3544 return (0);
3545 }
3546
3547 #define MDI_IO CTRL_SWDPIN(2)
3548 #define MDI_DIR CTRL_SWDPIO(2) /* host -> PHY */
3549 #define MDI_CLK CTRL_SWDPIN(3)
3550
3551 static void
3552 i82543_mii_sendbits(struct wm_softc *sc, uint32_t data, int nbits)
3553 {
3554 uint32_t i, v;
3555
3556 v = CSR_READ(sc, WMREG_CTRL);
3557 v &= ~(MDI_IO|MDI_CLK|(CTRL_SWDPIO_MASK << CTRL_SWDPIO_SHIFT));
3558 v |= MDI_DIR | CTRL_SWDPIO(3);
3559
3560 for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
3561 if (data & i)
3562 v |= MDI_IO;
3563 else
3564 v &= ~MDI_IO;
3565 CSR_WRITE(sc, WMREG_CTRL, v);
3566 delay(10);
3567 CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
3568 delay(10);
3569 CSR_WRITE(sc, WMREG_CTRL, v);
3570 delay(10);
3571 }
3572 }
3573
3574 static uint32_t
3575 i82543_mii_recvbits(struct wm_softc *sc)
3576 {
3577 uint32_t v, i, data = 0;
3578
3579 v = CSR_READ(sc, WMREG_CTRL);
3580 v &= ~(MDI_IO|MDI_CLK|(CTRL_SWDPIO_MASK << CTRL_SWDPIO_SHIFT));
3581 v |= CTRL_SWDPIO(3);
3582
3583 CSR_WRITE(sc, WMREG_CTRL, v);
3584 delay(10);
3585 CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
3586 delay(10);
3587 CSR_WRITE(sc, WMREG_CTRL, v);
3588 delay(10);
3589
3590 for (i = 0; i < 16; i++) {
3591 data <<= 1;
3592 CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
3593 delay(10);
3594 if (CSR_READ(sc, WMREG_CTRL) & MDI_IO)
3595 data |= 1;
3596 CSR_WRITE(sc, WMREG_CTRL, v);
3597 delay(10);
3598 }
3599
3600 CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
3601 delay(10);
3602 CSR_WRITE(sc, WMREG_CTRL, v);
3603 delay(10);
3604
3605 return (data);
3606 }
3607
3608 #undef MDI_IO
3609 #undef MDI_DIR
3610 #undef MDI_CLK
3611
3612 /*
3613 * wm_gmii_i82543_readreg: [mii interface function]
3614 *
3615 * Read a PHY register on the GMII (i82543 version).
3616 */
3617 static int
3618 wm_gmii_i82543_readreg(struct device *self, int phy, int reg)
3619 {
3620 struct wm_softc *sc = (void *) self;
3621 int rv;
3622
3623 i82543_mii_sendbits(sc, 0xffffffffU, 32);
3624 i82543_mii_sendbits(sc, reg | (phy << 5) |
3625 (MII_COMMAND_READ << 10) | (MII_COMMAND_START << 12), 14);
3626 rv = i82543_mii_recvbits(sc) & 0xffff;
3627
3628 DPRINTF(WM_DEBUG_GMII,
3629 ("%s: GMII: read phy %d reg %d -> 0x%04x\n",
3630 sc->sc_dev.dv_xname, phy, reg, rv));
3631
3632 return (rv);
3633 }
3634
3635 /*
3636 * wm_gmii_i82543_writereg: [mii interface function]
3637 *
3638 * Write a PHY register on the GMII (i82543 version).
3639 */
3640 static void
3641 wm_gmii_i82543_writereg(struct device *self, int phy, int reg, int val)
3642 {
3643 struct wm_softc *sc = (void *) self;
3644
3645 i82543_mii_sendbits(sc, 0xffffffffU, 32);
3646 i82543_mii_sendbits(sc, val | (MII_COMMAND_ACK << 16) |
3647 (reg << 18) | (phy << 23) | (MII_COMMAND_WRITE << 28) |
3648 (MII_COMMAND_START << 30), 32);
3649 }
3650
3651 /*
3652 * wm_gmii_i82544_readreg: [mii interface function]
3653 *
3654 * Read a PHY register on the GMII.
3655 */
3656 static int
3657 wm_gmii_i82544_readreg(struct device *self, int phy, int reg)
3658 {
3659 struct wm_softc *sc = (void *) self;
3660 uint32_t mdic = 0;
3661 int i, rv;
3662
3663 CSR_WRITE(sc, WMREG_MDIC, MDIC_OP_READ | MDIC_PHYADD(phy) |
3664 MDIC_REGADD(reg));
3665
3666 for (i = 0; i < 100; i++) {
3667 mdic = CSR_READ(sc, WMREG_MDIC);
3668 if (mdic & MDIC_READY)
3669 break;
3670 delay(10);
3671 }
3672
3673 if ((mdic & MDIC_READY) == 0) {
3674 printf("%s: MDIC read timed out: phy %d reg %d\n",
3675 sc->sc_dev.dv_xname, phy, reg);
3676 rv = 0;
3677 } else if (mdic & MDIC_E) {
3678 #if 0 /* This is normal if no PHY is present. */
3679 printf("%s: MDIC read error: phy %d reg %d\n",
3680 sc->sc_dev.dv_xname, phy, reg);
3681 #endif
3682 rv = 0;
3683 } else {
3684 rv = MDIC_DATA(mdic);
3685 if (rv == 0xffff)
3686 rv = 0;
3687 }
3688
3689 return (rv);
3690 }
3691
3692 /*
3693 * wm_gmii_i82544_writereg: [mii interface function]
3694 *
3695 * Write a PHY register on the GMII.
3696 */
3697 static void
3698 wm_gmii_i82544_writereg(struct device *self, int phy, int reg, int val)
3699 {
3700 struct wm_softc *sc = (void *) self;
3701 uint32_t mdic = 0;
3702 int i;
3703
3704 CSR_WRITE(sc, WMREG_MDIC, MDIC_OP_WRITE | MDIC_PHYADD(phy) |
3705 MDIC_REGADD(reg) | MDIC_DATA(val));
3706
3707 for (i = 0; i < 100; i++) {
3708 mdic = CSR_READ(sc, WMREG_MDIC);
3709 if (mdic & MDIC_READY)
3710 break;
3711 delay(10);
3712 }
3713
3714 if ((mdic & MDIC_READY) == 0)
3715 printf("%s: MDIC write timed out: phy %d reg %d\n",
3716 sc->sc_dev.dv_xname, phy, reg);
3717 else if (mdic & MDIC_E)
3718 printf("%s: MDIC write error: phy %d reg %d\n",
3719 sc->sc_dev.dv_xname, phy, reg);
3720 }
3721
3722 /*
3723 * wm_gmii_statchg: [mii interface function]
3724 *
3725 * Callback from MII layer when media changes.
3726 */
3727 static void
3728 wm_gmii_statchg(struct device *self)
3729 {
3730 struct wm_softc *sc = (void *) self;
3731 struct mii_data *mii = &sc->sc_mii;
3732
3733 sc->sc_ctrl &= ~(CTRL_TFCE | CTRL_RFCE);
3734 sc->sc_tctl &= ~TCTL_COLD(0x3ff);
3735 sc->sc_fcrtl &= ~FCRTL_XONE;
3736
3737 /*
3738 * Get flow control negotiation result.
3739 */
3740 if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
3741 (mii->mii_media_active & IFM_ETH_FMASK) != sc->sc_flowflags) {
3742 sc->sc_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
3743 mii->mii_media_active &= ~IFM_ETH_FMASK;
3744 }
3745
3746 if (sc->sc_flowflags & IFM_FLOW) {
3747 if (sc->sc_flowflags & IFM_ETH_TXPAUSE) {
3748 sc->sc_ctrl |= CTRL_TFCE;
3749 sc->sc_fcrtl |= FCRTL_XONE;
3750 }
3751 if (sc->sc_flowflags & IFM_ETH_RXPAUSE)
3752 sc->sc_ctrl |= CTRL_RFCE;
3753 }
3754
3755 if (sc->sc_mii.mii_media_active & IFM_FDX) {
3756 DPRINTF(WM_DEBUG_LINK,
3757 ("%s: LINK: statchg: FDX\n", sc->sc_dev.dv_xname));
3758 sc->sc_tctl |= TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
3759 } else {
3760 DPRINTF(WM_DEBUG_LINK,
3761 ("%s: LINK: statchg: HDX\n", sc->sc_dev.dv_xname));
3762 sc->sc_tctl |= TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
3763 }
3764
3765 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
3766 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
3767 CSR_WRITE(sc, (sc->sc_type < WM_T_82543) ? WMREG_OLD_FCRTL
3768 : WMREG_FCRTL, sc->sc_fcrtl);
3769 }
3770