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