if_wm.c revision 1.1 1 /* $NetBSD: if_wm.c,v 1.1 2002/03/28 04:54:35 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001, 2002 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 * Register description for the Intel i82542 (``Wiseman''),
40 * i82543 (``Livengood''), and i82544 (``Cordova'') Gigabit
41 * Ethernet chips.
42 *
43 * TODO (in order of importance):
44 *
45 * - Fix hw VLAN assist.
46 *
47 * - Make GMII work on the Livengood.
48 *
49 * - Fix out-bound IP header checksums.
50 *
51 * - Fix UDP checksums.
52 *
53 * - Jumbo frames -- requires changes to network stack due to
54 * lame buffer length handling on chip.
55 *
56 * ...and, of course, performance tuning.
57 */
58
59 #include "bpfilter.h"
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/callout.h>
64 #include <sys/mbuf.h>
65 #include <sys/malloc.h>
66 #include <sys/kernel.h>
67 #include <sys/socket.h>
68 #include <sys/ioctl.h>
69 #include <sys/errno.h>
70 #include <sys/device.h>
71 #include <sys/queue.h>
72
73 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
74
75 #include <net/if.h>
76 #include <net/if_dl.h>
77 #include <net/if_media.h>
78 #include <net/if_ether.h>
79
80 #if NBPFILTER > 0
81 #include <net/bpf.h>
82 #endif
83
84 #include <netinet/in.h> /* XXX for struct ip */
85 #include <netinet/in_systm.h> /* XXX for struct ip */
86 #include <netinet/ip.h> /* XXX for struct ip */
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. This is arbitrary, but allocate
116 * enough descriptors for 128 pending transmissions, and 8 segments
117 * per packet. This MUST work out to a power of 2, and also must
118 * be evenly divisible by 8.
119 */
120 #define WM_NTXSEGS 8
121
122 #define WM_TXQUEUELEN 128
123 #define WM_TXQUEUELEN_MASK (WM_TXQUEUELEN - 1)
124 #define WM_NTXDESC (WM_TXQUEUELEN * WM_NTXSEGS)
125 #define WM_NTXDESC_MASK (WM_NTXDESC - 1)
126 #define WM_NEXTTX(x) (((x) + 1) & WM_NTXDESC_MASK)
127 #define WM_NEXTTXS(x) (((x) + 1) & WM_TXQUEUELEN_MASK)
128
129 /*
130 * The interrupt mitigation feature of the Wiseman is pretty cool -- as
131 * long as you're transmitting, you don't have to take an interrupt at
132 * all. However, we force an interrupt to happen every N + 1 packets
133 * in order to kick us in a reasonable amount of time when we run out
134 * of descriptors.
135 */
136 #define WM_TXINTR_MASK 7
137
138 /*
139 * Receive descriptor list size. We have one Rx buffer for normal
140 * sized packets. Jumbo packets consume 5 Rx buffers for a full-sized
141 * packet. We allocate 128 receive descriptors, each with a 2k
142 * buffer (MCLBYTES), which gives us room for 25 jumbo packets.
143 */
144 #define WM_NRXDESC 128
145 #define WM_NRXDESC_MASK (WM_NRXDESC - 1)
146 #define WM_NEXTRX(x) (((x) + 1) & WM_NRXDESC_MASK)
147 #define WM_PREVRX(x) (((x) - 1) & WM_NRXDESC_MASK)
148
149 /*
150 * Control structures are DMA'd to the i82542 chip. We allocate them in
151 * a single clump that maps to a single DMA segment to make serveral things
152 * easier.
153 */
154 struct wm_control_data {
155 /*
156 * The transmit descriptors.
157 */
158 wiseman_txdesc_t wcd_txdescs[WM_NTXDESC];
159
160 /*
161 * The receive descriptors.
162 */
163 wiseman_rxdesc_t wcd_rxdescs[WM_NRXDESC];
164 };
165
166 #define WM_CDOFF(x) offsetof(struct wm_control_data, x)
167 #define WM_CDTXOFF(x) WM_CDOFF(wcd_txdescs[(x)])
168 #define WM_CDRXOFF(x) WM_CDOFF(wcd_rxdescs[(x)])
169
170 /*
171 * Software state for transmit jobs.
172 */
173 struct wm_txsoft {
174 struct mbuf *txs_mbuf; /* head of our mbuf chain */
175 bus_dmamap_t txs_dmamap; /* our DMA map */
176 int txs_firstdesc; /* first descriptor in packet */
177 int txs_lastdesc; /* last descriptor in packet */
178 };
179
180 /*
181 * Software state for receive buffers. Each descriptor gets a
182 * 2k (MCLBYTES) buffer and a DMA map. For packets which fill
183 * more than one buffer, we chain them together.
184 */
185 struct wm_rxsoft {
186 struct mbuf *rxs_mbuf; /* head of our mbuf chain */
187 bus_dmamap_t rxs_dmamap; /* our DMA map */
188 };
189
190 /*
191 * Software state per device.
192 */
193 struct wm_softc {
194 struct device sc_dev; /* generic device information */
195 bus_space_tag_t sc_st; /* bus space tag */
196 bus_space_handle_t sc_sh; /* bus space handle */
197 bus_dma_tag_t sc_dmat; /* bus DMA tag */
198 struct ethercom sc_ethercom; /* ethernet common data */
199 void *sc_sdhook; /* shutdown hook */
200
201 int sc_type; /* chip type; see below */
202 int sc_flags; /* flags; see below */
203
204 void *sc_ih; /* interrupt cookie */
205
206 struct mii_data sc_mii; /* MII/media information */
207
208 struct callout sc_tick_ch; /* tick callout */
209
210 bus_dmamap_t sc_cddmamap; /* control data DMA map */
211 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
212
213 /*
214 * Software state for the transmit and receive descriptors.
215 */
216 struct wm_txsoft sc_txsoft[WM_TXQUEUELEN];
217 struct wm_rxsoft sc_rxsoft[WM_NRXDESC];
218
219 /*
220 * Control data structures.
221 */
222 struct wm_control_data *sc_control_data;
223 #define sc_txdescs sc_control_data->wcd_txdescs
224 #define sc_rxdescs sc_control_data->wcd_rxdescs
225
226 #ifdef WM_EVENT_COUNTERS
227 /* Event counters. */
228 struct evcnt sc_ev_txsstall; /* Tx stalled due to no txs */
229 struct evcnt sc_ev_txdstall; /* Tx stalled due to no txd */
230 struct evcnt sc_ev_txintr; /* Tx interrupts */
231 struct evcnt sc_ev_rxintr; /* Rx interrupts */
232 struct evcnt sc_ev_linkintr; /* Link interrupts */
233
234 struct evcnt sc_ev_rxipsum; /* IP checksums checked in-bound */
235 struct evcnt sc_ev_rxtusum; /* TCP/UDP cksums checked in-bound */
236 struct evcnt sc_ev_txipsum; /* IP checksums comp. out-bound */
237 struct evcnt sc_ev_txtusum; /* TCP/UDP cksums comp. out-bound */
238
239 struct evcnt sc_ev_txseg1; /* Tx packets w/ 1 segment */
240 struct evcnt sc_ev_txseg2; /* Tx packets w/ 2 segments */
241 struct evcnt sc_ev_txseg3; /* Tx packets w/ 3 segments */
242 struct evcnt sc_ev_txseg4; /* Tx packets w/ 4 segments */
243 struct evcnt sc_ev_txseg5; /* Tx packets w/ 5 segments */
244 struct evcnt sc_ev_txsegmore; /* Tx packets w/ more than 5 segments */
245 struct evcnt sc_ev_txdrop; /* Tx packets dropped (too many segs) */
246
247 struct evcnt sc_ev_tu; /* Tx underrun */
248 #endif /* WM_EVENT_COUNTERS */
249
250 bus_addr_t sc_tdt_reg; /* offset of TDT register */
251
252 int sc_txfree; /* number of free Tx descriptors */
253 int sc_txnext; /* next ready Tx descriptor */
254
255 int sc_txsfree; /* number of free Tx jobs */
256 int sc_txsnext; /* next free Tx job */
257 int sc_txsdirty; /* dirty Tx jobs */
258
259 bus_addr_t sc_rdt_reg; /* offset of RDT register */
260
261 int sc_rxptr; /* next ready Rx descriptor/queue ent */
262 int sc_rxdiscard;
263 int sc_rxlen;
264 struct mbuf *sc_rxhead;
265 struct mbuf *sc_rxtail;
266 struct mbuf **sc_rxtailp;
267
268 uint32_t sc_ctrl; /* prototype CTRL register */
269 #if 0
270 uint32_t sc_ctrl_ext; /* prototype CTRL_EXT register */
271 #endif
272 uint32_t sc_icr; /* prototype interrupt bits */
273 uint32_t sc_tctl; /* prototype TCTL register */
274 uint32_t sc_rctl; /* prototype RCTL register */
275 uint32_t sc_txcw; /* prototype TXCW register */
276 uint32_t sc_tipg; /* prototype TIPG register */
277
278 int sc_tbi_linkup; /* TBI link status */
279 int sc_tbi_anstate; /* autonegotiation state */
280
281 int sc_mchash_type; /* multicast filter offset */
282 };
283
284 #define WM_RXCHAIN_RESET(sc) \
285 do { \
286 (sc)->sc_rxtailp = &(sc)->sc_rxhead; \
287 *(sc)->sc_rxtailp = NULL; \
288 (sc)->sc_rxlen = 0; \
289 } while (/*CONSTCOND*/0)
290
291 #define WM_RXCHAIN_LINK(sc, m) \
292 do { \
293 *(sc)->sc_rxtailp = (sc)->sc_rxtail = (m); \
294 (sc)->sc_rxtailp = &(m)->m_next; \
295 } while (/*CONSTCOND*/0)
296
297 /* sc_type */
298 #define WM_T_WISEMAN_2_0 0 /* Wiseman (i82542) 2.0 (really old) */
299 #define WM_T_WISEMAN_2_1 1 /* Wiseman (i82542) 2.1+ (old) */
300 #define WM_T_LIVENGOOD 2 /* Livengood (i82543) */
301 #define WM_T_CORDOVA 3 /* Cordova (i82544) */
302
303 /* sc_flags */
304 #define WM_F_HAS_MII 0x01 /* has MII */
305
306 #ifdef WM_EVENT_COUNTERS
307 #define WM_EVCNT_INCR(ev) (ev)->ev_count++
308 #else
309 #define WM_EVCNT_INCR(ev) /* nothing */
310 #endif
311
312 #define CSR_READ(sc, reg) \
313 bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (reg))
314 #define CSR_WRITE(sc, reg, val) \
315 bus_space_write_4((sc)->sc_st, (sc)->sc_sh, (reg), (val))
316
317 #define WM_CDTXADDR(sc, x) ((sc)->sc_cddma + WM_CDTXOFF((x)))
318 #define WM_CDRXADDR(sc, x) ((sc)->sc_cddma + WM_CDRXOFF((x)))
319
320 #define WM_CDTXSYNC(sc, x, n, ops) \
321 do { \
322 int __x, __n; \
323 \
324 __x = (x); \
325 __n = (n); \
326 \
327 /* If it will wrap around, sync to the end of the ring. */ \
328 if ((__x + __n) > WM_NTXDESC) { \
329 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
330 WM_CDTXOFF(__x), sizeof(wiseman_txdesc_t) * \
331 (WM_NTXDESC - __x), (ops)); \
332 __n -= (WM_NTXDESC - __x); \
333 __x = 0; \
334 } \
335 \
336 /* Now sync whatever is left. */ \
337 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
338 WM_CDTXOFF(__x), sizeof(wiseman_txdesc_t) * __n, (ops)); \
339 } while (/*CONSTCOND*/0)
340
341 #define WM_CDRXSYNC(sc, x, ops) \
342 do { \
343 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
344 WM_CDRXOFF((x)), sizeof(wiseman_rxdesc_t), (ops)); \
345 } while (/*CONSTCOND*/0)
346
347 #define WM_INIT_RXDESC(sc, x) \
348 do { \
349 struct wm_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \
350 wiseman_rxdesc_t *__rxd = &(sc)->sc_rxdescs[(x)]; \
351 struct mbuf *__m = __rxs->rxs_mbuf; \
352 \
353 /* \
354 * Note: We scoot the packet forward 2 bytes in the buffer \
355 * so that the payload after the Ethernet header is aligned \
356 * to a 4-byte boundary. \
357 * \
358 * XXX BRAINDAMAGE ALERT! \
359 * The stupid chip uses the same size for every buffer, which \
360 * is set in the Receive Control register. We are using the 2K \
361 * size option, but what we REALLY want is (2K - 2)! For this \
362 * reason, we can't accept packets longer than the standard \
363 * Ethernet MTU, without incurring a big penalty to copy every \
364 * incoming packet to a new, suitably aligned buffer. \
365 * \
366 * We'll need to make some changes to the layer 3/4 parts of \
367 * the stack (to copy the headers to a new buffer if not \
368 * aligned) in order to support large MTU on this chip. Lame. \
369 */ \
370 __m->m_data = __m->m_ext.ext_buf + 2; \
371 \
372 __rxd->wrx_addr.wa_low = \
373 htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + 2); \
374 __rxd->wrx_addr.wa_high = 0; \
375 __rxd->wrx_len = 0; \
376 __rxd->wrx_cksum = 0; \
377 __rxd->wrx_status = 0; \
378 __rxd->wrx_errors = 0; \
379 __rxd->wrx_special = 0; \
380 WM_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
381 \
382 CSR_WRITE((sc), (sc)->sc_rdt_reg, (x)); \
383 } while (/*CONSTCOND*/0)
384
385 void wm_start(struct ifnet *);
386 void wm_watchdog(struct ifnet *);
387 int wm_ioctl(struct ifnet *, u_long, caddr_t);
388 int wm_init(struct ifnet *);
389 void wm_stop(struct ifnet *, int);
390
391 void wm_shutdown(void *);
392
393 void wm_reset(struct wm_softc *);
394 void wm_rxdrain(struct wm_softc *);
395 int wm_add_rxbuf(struct wm_softc *, int);
396 void wm_read_eeprom(struct wm_softc *, int, int, u_int16_t *);
397 void wm_tick(void *);
398
399 void wm_set_filter(struct wm_softc *);
400
401 int wm_intr(void *);
402 void wm_txintr(struct wm_softc *);
403 void wm_rxintr(struct wm_softc *);
404 void wm_linkintr(struct wm_softc *, uint32_t);
405
406 void wm_tbi_mediainit(struct wm_softc *);
407 int wm_tbi_mediachange(struct ifnet *);
408 void wm_tbi_mediastatus(struct ifnet *, struct ifmediareq *);
409
410 void wm_tbi_set_linkled(struct wm_softc *);
411 void wm_tbi_check_link(struct wm_softc *);
412
413 void wm_gmii_reset(struct wm_softc *);
414
415 int wm_gmii_livengood_readreg(struct device *, int, int);
416 void wm_gmii_livengood_writereg(struct device *, int, int, int);
417
418 int wm_gmii_cordova_readreg(struct device *, int, int);
419 void wm_gmii_cordova_writereg(struct device *, int, int, int);
420
421 void wm_gmii_statchg(struct device *);
422
423 void wm_gmii_mediainit(struct wm_softc *);
424 int wm_gmii_mediachange(struct ifnet *);
425 void wm_gmii_mediastatus(struct ifnet *, struct ifmediareq *);
426
427 int wm_match(struct device *, struct cfdata *, void *);
428 void wm_attach(struct device *, struct device *, void *);
429
430 int wm_copy_small = 0;
431
432 struct cfattach wm_ca = {
433 sizeof(struct wm_softc), wm_match, wm_attach,
434 };
435
436 /*
437 * Devices supported by this driver.
438 */
439 const struct wm_product {
440 pci_vendor_id_t wmp_vendor;
441 pci_product_id_t wmp_product;
442 const char *wmp_name;
443 int wmp_type;
444 int wmp_flags;
445 #define WMP_F_1000X 0x01
446 #define WMP_F_1000T 0x02
447 } wm_products[] = {
448 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82542,
449 "Intel i82542 1000BASE-X Ethernet",
450 WM_T_WISEMAN_2_1, WMP_F_1000X },
451
452 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82543_FIBER,
453 "Intel i82543 1000BASE-X Ethernet",
454 WM_T_LIVENGOOD, WMP_F_1000X },
455
456 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82543_SC,
457 "Intel i82543-SC 1000BASE-X Ethernet",
458 WM_T_LIVENGOOD, WMP_F_1000X },
459
460 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82543_COPPER,
461 "Intel i82543 1000BASE-T Ethernet",
462 WM_T_LIVENGOOD, WMP_F_1000T },
463
464 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544_XT,
465 "Intel i82544 1000BASE-T Ethernet",
466 WM_T_CORDOVA, WMP_F_1000T },
467
468 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544_XF,
469 "Intel i82544 1000BASE-X Ethernet",
470 WM_T_CORDOVA, WMP_F_1000X },
471
472 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544GC,
473 "Intel i82544GC 1000BASE-T Ethernet",
474 WM_T_CORDOVA, WMP_F_1000T },
475
476 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544GC_64,
477 "Intel i82544GC 1000BASE-T Ethernet",
478 WM_T_CORDOVA, WMP_F_1000T },
479
480 { 0, 0,
481 NULL,
482 0, 0 },
483 };
484
485 static const struct wm_product *
486 wm_lookup(const struct pci_attach_args *pa)
487 {
488 const struct wm_product *wmp;
489
490 for (wmp = wm_products; wmp->wmp_name != NULL; wmp++) {
491 if (PCI_VENDOR(pa->pa_id) == wmp->wmp_vendor &&
492 PCI_PRODUCT(pa->pa_id) == wmp->wmp_product)
493 return (wmp);
494 }
495 return (NULL);
496 }
497
498 int
499 wm_match(struct device *parent, struct cfdata *cf, void *aux)
500 {
501 struct pci_attach_args *pa = aux;
502
503 if (wm_lookup(pa) != NULL)
504 return (1);
505
506 return (0);
507 }
508
509 void
510 wm_attach(struct device *parent, struct device *self, void *aux)
511 {
512 struct wm_softc *sc = (void *) self;
513 struct pci_attach_args *pa = aux;
514 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
515 pci_chipset_tag_t pc = pa->pa_pc;
516 pci_intr_handle_t ih;
517 const char *intrstr = NULL;
518 bus_space_tag_t memt;
519 bus_space_handle_t memh;
520 bus_dma_segment_t seg;
521 int memh_valid;
522 int i, rseg, error;
523 const struct wm_product *wmp;
524 uint8_t enaddr[ETHER_ADDR_LEN];
525 uint16_t myea[ETHER_ADDR_LEN / 2], cfg1, cfg2, swdpin;
526 pcireg_t preg, memtype;
527 int pmreg;
528
529 callout_init(&sc->sc_tick_ch);
530
531 wmp = wm_lookup(pa);
532 if (wmp == NULL) {
533 printf("\n");
534 panic("wm_attach: impossible");
535 }
536
537 sc->sc_dmat = pa->pa_dmat;
538
539 preg = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
540 printf(": %s, rev. %d\n", wmp->wmp_name, preg);
541
542 sc->sc_type = wmp->wmp_type;
543 if (sc->sc_type < WM_T_LIVENGOOD) {
544 if (preg < 2) {
545 printf("%s: Wiseman must be at least rev. 2\n",
546 sc->sc_dev.dv_xname);
547 return;
548 }
549 if (preg < 3)
550 sc->sc_type = WM_T_WISEMAN_2_0;
551 }
552
553 /*
554 * Map the device.
555 */
556 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, WM_PCI_MMBA);
557 switch (memtype) {
558 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
559 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
560 memh_valid = (pci_mapreg_map(pa, WM_PCI_MMBA,
561 memtype, 0, &memt, &memh, NULL, NULL) == 0);
562 break;
563 default:
564 memh_valid = 0;
565 }
566
567 if (memh_valid) {
568 sc->sc_st = memt;
569 sc->sc_sh = memh;
570 } else {
571 printf("%s: unable to map device registers\n",
572 sc->sc_dev.dv_xname);
573 return;
574 }
575
576 /* Enable bus mastering. Disable MWI on the Wiseman 2.0. */
577 preg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
578 preg |= PCI_COMMAND_MASTER_ENABLE;
579 if (sc->sc_type < WM_T_WISEMAN_2_1)
580 preg &= ~PCI_COMMAND_INVALIDATE_ENABLE;
581 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, preg);
582
583 /* Get it out of power save mode, if needed. */
584 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
585 preg = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
586 if (preg == 3) {
587 /*
588 * The card has lost all configuration data in
589 * this state, so punt.
590 */
591 printf("%s: unable to wake from power state D3\n",
592 sc->sc_dev.dv_xname);
593 return;
594 }
595 if (preg != 0) {
596 printf("%s: waking up from power state D%d\n",
597 sc->sc_dev.dv_xname, preg);
598 pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
599 }
600 }
601
602 /*
603 * Map and establish our interrupt.
604 */
605 if (pci_intr_map(pa, &ih)) {
606 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
607 return;
608 }
609 intrstr = pci_intr_string(pc, ih);
610 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wm_intr, sc);
611 if (sc->sc_ih == NULL) {
612 printf("%s: unable to establish interrupt",
613 sc->sc_dev.dv_xname);
614 if (intrstr != NULL)
615 printf(" at %s", intrstr);
616 printf("\n");
617 return;
618 }
619 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
620
621 /*
622 * Allocate the control data structures, and create and load the
623 * DMA map for it.
624 */
625 if ((error = bus_dmamem_alloc(sc->sc_dmat,
626 sizeof(struct wm_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
627 0)) != 0) {
628 printf("%s: unable to allocate control data, error = %d\n",
629 sc->sc_dev.dv_xname, error);
630 goto fail_0;
631 }
632
633 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
634 sizeof(struct wm_control_data), (caddr_t *)&sc->sc_control_data,
635 BUS_DMA_COHERENT)) != 0) {
636 printf("%s: unable to map control data, error = %d\n",
637 sc->sc_dev.dv_xname, error);
638 goto fail_1;
639 }
640
641 if ((error = bus_dmamap_create(sc->sc_dmat,
642 sizeof(struct wm_control_data), 1,
643 sizeof(struct wm_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
644 printf("%s: unable to create control data DMA map, "
645 "error = %d\n", sc->sc_dev.dv_xname, error);
646 goto fail_2;
647 }
648
649 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
650 sc->sc_control_data, sizeof(struct wm_control_data), NULL,
651 0)) != 0) {
652 printf("%s: unable to load control data DMA map, error = %d\n",
653 sc->sc_dev.dv_xname, error);
654 goto fail_3;
655 }
656
657 /*
658 * Create the transmit buffer DMA maps.
659 */
660 for (i = 0; i < WM_TXQUEUELEN; i++) {
661 if ((error = bus_dmamap_create(sc->sc_dmat, ETHER_MAX_LEN_JUMBO,
662 WM_NTXSEGS, MCLBYTES, 0, 0,
663 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
664 printf("%s: unable to create Tx DMA map %d, "
665 "error = %d\n", sc->sc_dev.dv_xname, i, error);
666 goto fail_4;
667 }
668 }
669
670 /*
671 * Create the receive buffer DMA maps.
672 */
673 for (i = 0; i < WM_NRXDESC; i++) {
674 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
675 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
676 printf("%s: unable to create Rx DMA map %d, "
677 "error = %d\n", sc->sc_dev.dv_xname, i, error);
678 goto fail_5;
679 }
680 sc->sc_rxsoft[i].rxs_mbuf = NULL;
681 }
682
683 /*
684 * Reset the chip to a known state.
685 */
686 wm_reset(sc);
687
688 /*
689 * Read the Ethernet address from the EEPROM.
690 */
691 wm_read_eeprom(sc, EEPROM_OFF_MACADDR,
692 sizeof(myea) / sizeof(myea[0]), myea);
693 enaddr[0] = myea[0] & 0xff;
694 enaddr[1] = myea[0] >> 8;
695 enaddr[2] = myea[1] & 0xff;
696 enaddr[3] = myea[1] >> 8;
697 enaddr[4] = myea[2] & 0xff;
698 enaddr[5] = myea[2] >> 8;
699
700 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
701 ether_sprintf(enaddr));
702
703 /*
704 * Read the config info from the EEPROM, and set up various
705 * bits in the control registers based on their contents.
706 */
707 wm_read_eeprom(sc, EEPROM_OFF_CFG1, 1, &cfg1);
708 wm_read_eeprom(sc, EEPROM_OFF_CFG2, 1, &cfg2);
709 if (sc->sc_type >= WM_T_CORDOVA)
710 wm_read_eeprom(sc, EEPROM_OFF_SWDPIN, 1, &swdpin);
711
712 if (cfg1 & EEPROM_CFG1_ILOS)
713 sc->sc_ctrl |= CTRL_ILOS;
714 if (sc->sc_type >= WM_T_CORDOVA) {
715 sc->sc_ctrl |=
716 ((swdpin >> EEPROM_SWDPIN_SWDPIO_SHIFT) & 0xf) <<
717 CTRL_SWDPIO_SHIFT;
718 sc->sc_ctrl |=
719 ((swdpin >> EEPROM_SWDPIN_SWDPIN_SHIFT) & 0xf) <<
720 CTRL_SWDPINS_SHIFT;
721 } else {
722 sc->sc_ctrl |=
723 ((cfg1 >> EEPROM_CFG1_SWDPIO_SHIFT) & 0xf) <<
724 CTRL_SWDPIO_SHIFT;
725 }
726
727 #if 0
728 if (sc->sc_type >= WM_T_CORDOVA) {
729 if (cfg1 & EEPROM_CFG1_IPS0)
730 sc->sc_ctrl_ext |= CTRL_EXT_IPS;
731 if (cfg1 & EEPROM_CFG1_IPS1)
732 sc->sc_ctrl_ext |= CTRL_EXT_IPS1;
733 sc->sc_ctrl_ext |=
734 ((swdpin >> (EEPROM_SWDPIN_SWDPIO_SHIFT + 4)) & 0xd) <<
735 CTRL_EXT_SWDPIO_SHIFT;
736 sc->sc_ctrl_ext |=
737 ((swdpin >> (EEPROM_SWDPIN_SWDPIN_SHIFT + 4)) & 0xd) <<
738 CTRL_EXT_SWDPINS_SHIFT;
739 } else {
740 sc->sc_ctrl_ext |=
741 ((cfg2 >> EEPROM_CFG2_SWDPIO_SHIFT) & 0xf) <<
742 CTRL_EXT_SWDPIO_SHIFT;
743 }
744 #endif
745
746 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
747 #if 0
748 CSR_WRITE(sc, WMREG_CTRL_EXT, sc->sc_ctrl_ext);
749 #endif
750
751 /*
752 * Set up some register offsets that are different between
753 * the Wiseman and the Livengood and later chips.
754 */
755 if (sc->sc_type < WM_T_LIVENGOOD) {
756 sc->sc_rdt_reg = WMREG_OLD_RDT0;
757 sc->sc_tdt_reg = WMREG_OLD_TDT;
758 } else {
759 sc->sc_rdt_reg = WMREG_RDT;
760 sc->sc_tdt_reg = WMREG_TDT;
761 }
762
763 /*
764 * Determine if we should use flow control. We should
765 * always use it, unless we're on a Wiseman < 2.1.
766 */
767 if (sc->sc_type >= WM_T_WISEMAN_2_1)
768 sc->sc_ctrl |= CTRL_TFCE | CTRL_RFCE;
769
770 /*
771 * Determine if we're TBI or GMII mode, and initialize the
772 * media structures accordingly.
773 */
774 if (sc->sc_type < WM_T_LIVENGOOD ||
775 (CSR_READ(sc, WMREG_STATUS) & STATUS_TBIMODE) != 0) {
776 if (wmp->wmp_flags & WMP_F_1000T)
777 printf("%s: WARNING: TBIMODE set on 1000BASE-T "
778 "product!\n", sc->sc_dev.dv_xname);
779 wm_tbi_mediainit(sc);
780 } else {
781 if (wmp->wmp_flags & WMP_F_1000X)
782 printf("%s: WARNING: TBIMODE clear on 1000BASE-X "
783 "product!\n", sc->sc_dev.dv_xname);
784 wm_gmii_mediainit(sc);
785 }
786
787 ifp = &sc->sc_ethercom.ec_if;
788 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
789 ifp->if_softc = sc;
790 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
791 ifp->if_ioctl = wm_ioctl;
792 ifp->if_start = wm_start;
793 ifp->if_watchdog = wm_watchdog;
794 ifp->if_init = wm_init;
795 ifp->if_stop = wm_stop;
796 IFQ_SET_READY(&ifp->if_snd);
797
798 /*
799 * If we're a Livengood or greater, we can support VLANs.
800 */
801 if (sc->sc_type >= WM_T_LIVENGOOD)
802 sc->sc_ethercom.ec_capabilities |=
803 ETHERCAP_VLAN_MTU /* XXXJRT | ETHERCAP_VLAN_HWTAGGING */;
804
805 /*
806 * We can perform TCPv4 and UDPv4 checkums in-bound. Only
807 * on Livengood and later.
808 */
809 if (sc->sc_type >= WM_T_LIVENGOOD)
810 ifp->if_capabilities |=
811 IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
812
813 /*
814 * Attach the interface.
815 */
816 if_attach(ifp);
817 ether_ifattach(ifp, enaddr);
818
819 #ifdef WM_EVENT_COUNTERS
820 /* Attach event counters. */
821 evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
822 NULL, sc->sc_dev.dv_xname, "txsstall");
823 evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
824 NULL, sc->sc_dev.dv_xname, "txdstall");
825 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
826 NULL, sc->sc_dev.dv_xname, "txintr");
827 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
828 NULL, sc->sc_dev.dv_xname, "rxintr");
829 evcnt_attach_dynamic(&sc->sc_ev_linkintr, EVCNT_TYPE_INTR,
830 NULL, sc->sc_dev.dv_xname, "linkintr");
831
832 evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
833 NULL, sc->sc_dev.dv_xname, "rxipsum");
834 evcnt_attach_dynamic(&sc->sc_ev_rxtusum, EVCNT_TYPE_MISC,
835 NULL, sc->sc_dev.dv_xname, "rxtusum");
836 evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
837 NULL, sc->sc_dev.dv_xname, "txipsum");
838 evcnt_attach_dynamic(&sc->sc_ev_txtusum, EVCNT_TYPE_MISC,
839 NULL, sc->sc_dev.dv_xname, "txtusum");
840
841 evcnt_attach_dynamic(&sc->sc_ev_txseg1, EVCNT_TYPE_MISC,
842 NULL, sc->sc_dev.dv_xname, "txseg1");
843 evcnt_attach_dynamic(&sc->sc_ev_txseg2, EVCNT_TYPE_MISC,
844 NULL, sc->sc_dev.dv_xname, "txseg2");
845 evcnt_attach_dynamic(&sc->sc_ev_txseg3, EVCNT_TYPE_MISC,
846 NULL, sc->sc_dev.dv_xname, "txseg3");
847 evcnt_attach_dynamic(&sc->sc_ev_txseg4, EVCNT_TYPE_MISC,
848 NULL, sc->sc_dev.dv_xname, "txseg4");
849 evcnt_attach_dynamic(&sc->sc_ev_txseg5, EVCNT_TYPE_MISC,
850 NULL, sc->sc_dev.dv_xname, "txseg5");
851 evcnt_attach_dynamic(&sc->sc_ev_txsegmore, EVCNT_TYPE_MISC,
852 NULL, sc->sc_dev.dv_xname, "txsegmore");
853 evcnt_attach_dynamic(&sc->sc_ev_txdrop, EVCNT_TYPE_MISC,
854 NULL, sc->sc_dev.dv_xname, "txdrop");
855
856 evcnt_attach_dynamic(&sc->sc_ev_tu, EVCNT_TYPE_MISC,
857 NULL, sc->sc_dev.dv_xname, "tu");
858 #endif /* WM_EVENT_COUNTERS */
859
860 /*
861 * Make sure the interface is shutdown during reboot.
862 */
863 sc->sc_sdhook = shutdownhook_establish(wm_shutdown, sc);
864 if (sc->sc_sdhook == NULL)
865 printf("%s: WARNING: unable to establish shutdown hook\n",
866 sc->sc_dev.dv_xname);
867 return;
868
869 /*
870 * Free any resources we've allocated during the failed attach
871 * attempt. Do this in reverse order and fall through.
872 */
873 fail_5:
874 for (i = 0; i < WM_NRXDESC; i++) {
875 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
876 bus_dmamap_destroy(sc->sc_dmat,
877 sc->sc_rxsoft[i].rxs_dmamap);
878 }
879 fail_4:
880 for (i = 0; i < WM_TXQUEUELEN; i++) {
881 if (sc->sc_txsoft[i].txs_dmamap != NULL)
882 bus_dmamap_destroy(sc->sc_dmat,
883 sc->sc_txsoft[i].txs_dmamap);
884 }
885 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
886 fail_3:
887 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
888 fail_2:
889 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
890 sizeof(struct wm_control_data));
891 fail_1:
892 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
893 fail_0:
894 return;
895 }
896
897 /*
898 * wm_shutdown:
899 *
900 * Make sure the interface is stopped at reboot time.
901 */
902 void
903 wm_shutdown(void *arg)
904 {
905 struct wm_softc *sc = arg;
906
907 wm_stop(&sc->sc_ethercom.ec_if, 1);
908 }
909
910 /*
911 * wm_tx_cksum:
912 *
913 * Set up TCP/IP checksumming parameters for the
914 * specified packet.
915 */
916 static int
917 wm_tx_cksum(struct wm_softc *sc, struct mbuf *m0, uint32_t *cmdp,
918 uint32_t *fieldsp)
919 {
920 struct livengood_tcpip_ctxdesc *t;
921 uint32_t fields = 0, tcmd = 0, ipcs, tucs;
922 struct ip *ip;
923 int offset, iphl;
924
925 /*
926 * XXX It would be nice if the mbuf pkthdr had offset
927 * fields for the protocol headers.
928 */
929
930 /* XXX Assumes normal Ethernet encap. */
931 offset = ETHER_HDR_LEN;
932
933 /* XXX */
934 if (m0->m_len < (offset + sizeof(struct ip))) {
935 printf("%s: wm_tx_cksum: need to m_pullup, "
936 "packet dropped\n", sc->sc_dev.dv_xname);
937 return (EINVAL);
938 }
939
940 ip = (struct ip *) (mtod(m0, caddr_t) + offset);
941 iphl = ip->ip_hl << 2;
942
943 if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
944 WM_EVCNT_INCR(&sc->sc_ev_txipsum);
945 tcmd |= htole32(WTX_TCPIP_CMD_IP);
946 fields |= htole32(WTX_IXSM);
947 ipcs = htole32(WTX_TCPIP_IPCSS(offset) |
948 WTX_TCPIP_IPCSO(offsetof(struct ip, ip_sum)) |
949 WTX_TCPIP_IPCSE(offset + iphl - 1));
950 } else
951 ipcs = 0;
952
953 offset += iphl;
954
955 if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
956 WM_EVCNT_INCR(&sc->sc_ev_txtusum);
957 tcmd |= WTX_TCPIP_CMD_TCP;
958 fields |= htole32(WTX_TXSM);
959 tucs = htole32(WTX_TCPIP_TUCSS(offset) |
960 WTX_TCPIP_TUCSO(offset + m0->m_pkthdr.csum_data) |
961 WTX_TCPIP_TUCSE(0) /* rest of packet */);
962 } else
963 tucs = 0;
964
965 /* Fill in the context descriptor. */
966 t = (struct livengood_tcpip_ctxdesc *) &sc->sc_txdescs[sc->sc_txnext];
967 t->tcpip_ipcs = ipcs;
968 t->tcpip_tucs = tucs;
969 t->tcpip_cmdlen = htole32(WTX_CMD_DEXT | WTX_DTYP_C) | tcmd;
970 t->tcpip_seg = 0;
971 WM_CDTXSYNC(sc, sc->sc_txnext, 1, BUS_DMASYNC_PREWRITE);
972
973 sc->sc_txnext = WM_NEXTTX(sc->sc_txnext);
974
975 *cmdp = WTX_CMD_DEXT | WTC_DTYP_D;
976 *fieldsp = fields;
977
978 return (0);
979 }
980
981 /*
982 * wm_start: [ifnet interface function]
983 *
984 * Start packet transmission on the interface.
985 */
986 void
987 wm_start(struct ifnet *ifp)
988 {
989 struct wm_softc *sc = ifp->if_softc;
990 struct mbuf *m0/*, *m*/;
991 struct wm_txsoft *txs;
992 bus_dmamap_t dmamap;
993 int error, nexttx, lasttx, ofree, seg;
994 uint32_t cksumcmd, cksumfields;
995
996 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
997 return;
998
999 /*
1000 * Remember the previous number of free descriptors.
1001 */
1002 ofree = sc->sc_txfree;
1003
1004 /*
1005 * Loop through the send queue, setting up transmit descriptors
1006 * until we drain the queue, or use up all available transmit
1007 * descriptors.
1008 */
1009 for (;;) {
1010 /* Grab a packet off the queue. */
1011 IFQ_POLL(&ifp->if_snd, m0);
1012 if (m0 == NULL)
1013 break;
1014
1015 DPRINTF(WM_DEBUG_TX,
1016 ("%s: TX: have packet to transmit: %p\n",
1017 sc->sc_dev.dv_xname, m0));
1018
1019 /* Get a work queue entry. */
1020 if (sc->sc_txsfree == 0) {
1021 DPRINTF(WM_DEBUG_TX,
1022 ("%s: TX: no free job descriptors\n",
1023 sc->sc_dev.dv_xname));
1024 WM_EVCNT_INCR(&sc->sc_ev_txsstall);
1025 break;
1026 }
1027
1028 txs = &sc->sc_txsoft[sc->sc_txsnext];
1029 dmamap = txs->txs_dmamap;
1030
1031 /*
1032 * Load the DMA map. If this fails, the packet either
1033 * didn't fit in the allotted number of segments, or we
1034 * were short on resources. For the too-many-segments
1035 * case, we simply report an error and drop the packet,
1036 * since we can't sanely copy a jumbo packet to a single
1037 * buffer.
1038 */
1039 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
1040 BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1041 if (error) {
1042 if (error == EFBIG) {
1043 WM_EVCNT_INCR(&sc->sc_ev_txdrop);
1044 printf("%s: Tx packet consumes too many "
1045 "DMA segments, dropping...\n",
1046 sc->sc_dev.dv_xname);
1047 IFQ_DEQUEUE(&ifp->if_snd, m0);
1048 m_freem(m0);
1049 continue;
1050 }
1051 /*
1052 * Short on resources, just stop for now.
1053 */
1054 DPRINTF(WM_DEBUG_TX,
1055 ("%s: TX: dmamap load failed: %d\n",
1056 sc->sc_dev.dv_xname, error));
1057 break;
1058 }
1059
1060 /*
1061 * Ensure we have enough descriptors free to describe
1062 * the packet. Note, we always reserve one descriptor
1063 * at the end of the ring due to the semantics of the
1064 * TDT register, plus one more in the event we need
1065 * to re-load checksum offload context.
1066 */
1067 if (dmamap->dm_nsegs > (sc->sc_txfree - 2)) {
1068 /*
1069 * Not enough free descriptors to transmit this
1070 * packet. We haven't committed anything yet,
1071 * so just unload the DMA map, put the packet
1072 * pack on the queue, and punt. Notify the upper
1073 * layer that there are no more slots left.
1074 */
1075 DPRINTF(WM_DEBUG_TX,
1076 ("%s: TX: need %d descriptors, have %d\n",
1077 sc->sc_dev.dv_xname, dmamap->dm_nsegs,
1078 sc->sc_txfree - 1));
1079 ifp->if_flags |= IFF_OACTIVE;
1080 bus_dmamap_unload(sc->sc_dmat, dmamap);
1081 WM_EVCNT_INCR(&sc->sc_ev_txdstall);
1082 break;
1083 }
1084
1085 IFQ_DEQUEUE(&ifp->if_snd, m0);
1086
1087 /*
1088 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1089 */
1090
1091 /* Sync the DMA map. */
1092 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
1093 BUS_DMASYNC_PREWRITE);
1094
1095 DPRINTF(WM_DEBUG_TX,
1096 ("%s: TX: packet has %d DMA segments\n",
1097 sc->sc_dev.dv_xname, dmamap->dm_nsegs));
1098
1099 #ifdef WM_EVENT_COUNTERS
1100 switch (dmamap->dm_nsegs) {
1101 case 1:
1102 WM_EVCNT_INCR(&sc->sc_ev_txseg1);
1103 break;
1104 case 2:
1105 WM_EVCNT_INCR(&sc->sc_ev_txseg2);
1106 break;
1107 case 3:
1108 WM_EVCNT_INCR(&sc->sc_ev_txseg3);
1109 break;
1110 case 4:
1111 WM_EVCNT_INCR(&sc->sc_ev_txseg4);
1112 break;
1113 case 5:
1114 WM_EVCNT_INCR(&sc->sc_ev_txseg5);
1115 break;
1116 default:
1117 WM_EVCNT_INCR(&sc->sc_ev_txsegmore);
1118 break;
1119 }
1120 #endif /* WM_EVENT_COUNTERS */
1121
1122 /*
1123 * Set up checksum offload parameters for
1124 * this packet.
1125 */
1126 if (m0->m_pkthdr.csum_flags &
1127 (M_CSUM_IPv4|M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1128 if (wm_tx_cksum(sc, m0, &cksumcmd, &cksumfields) != 0) {
1129 /* Error message already displayed. */
1130 m_freem(m0);
1131 bus_dmamap_unload(sc->sc_dmat, dmamap);
1132 continue;
1133 }
1134 } else {
1135 cksumcmd = 0;
1136 cksumfields = 0;
1137 }
1138
1139 /*
1140 * Initialize the transmit descriptor.
1141 */
1142 for (nexttx = sc->sc_txnext, seg = 0;
1143 seg < dmamap->dm_nsegs;
1144 seg++, nexttx = WM_NEXTTX(nexttx)) {
1145 /*
1146 * Note: we currently only use 32-bit DMA
1147 * addresses.
1148 */
1149 sc->sc_txdescs[nexttx].wtx_addr.wa_low =
1150 htole32(dmamap->dm_segs[seg].ds_addr);
1151 sc->sc_txdescs[nexttx].wtx_cmdlen = cksumcmd |
1152 htole32(dmamap->dm_segs[seg].ds_len);
1153 sc->sc_txdescs[nexttx].wtx_fields.wtxu_bits =
1154 cksumfields;
1155 lasttx = nexttx;
1156
1157 DPRINTF(WM_DEBUG_TX,
1158 ("%s: TX: desc %d: low 0x%08x, len 0x%04x\n",
1159 sc->sc_dev.dv_xname, nexttx,
1160 (uint32_t) dmamap->dm_segs[seg].ds_addr,
1161 (uint32_t) dmamap->dm_segs[seg].ds_len));
1162 }
1163
1164 /*
1165 * Set up the command byte on the last descriptor of
1166 * the packet. If we're in the interrupt delay window,
1167 * delay the interrupt.
1168 */
1169 sc->sc_txdescs[lasttx].wtx_cmdlen |=
1170 htole32(WTX_CMD_EOP | WTX_CMD_IFCS | WTX_CMD_RS |
1171 WTX_CMD_RPS);
1172 if (sc->sc_txsnext & WM_TXINTR_MASK)
1173 sc->sc_txdescs[lasttx].wtx_cmdlen |=
1174 htole32(WTX_CMD_IDE);
1175
1176 #if 0 /* XXXJRT */
1177 /*
1178 * If VLANs are enabled and the packet has a VLAN tag, set
1179 * up the descriptor to encapsulate the packet for us.
1180 *
1181 * This is only valid on the last descriptor of the packet.
1182 */
1183 if (sc->sc_ethercom.ec_nvlans != 0 &&
1184 (m = m_aux_find(m0, AF_LINK, ETHERTYPE_VLAN)) != NULL) {
1185 sc->sc_txdescs[lasttx].wtx_cmdlen |=
1186 htole32(WTX_CMD_VLE);
1187 sc->sc_txdescs[lasttx].wtx_fields.wtxu_fields.wtxu_vlan
1188 = htole16(*mtod(m, int *) & 0xffff);
1189 }
1190 #endif /* XXXJRT */
1191
1192 DPRINTF(WM_DEBUG_TX,
1193 ("%s: TX: desc %d: cmdlen 0x%08x\n", sc->sc_dev.dv_xname,
1194 lasttx, sc->sc_txdescs[lasttx].wtx_cmdlen));
1195
1196 /* Sync the descriptors we're using. */
1197 WM_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
1198 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1199
1200 /* Give the packet to the chip. */
1201 CSR_WRITE(sc, sc->sc_tdt_reg, nexttx);
1202
1203 DPRINTF(WM_DEBUG_TX,
1204 ("%s: TX: TDT -> %d\n", sc->sc_dev.dv_xname, nexttx));
1205
1206 /*
1207 * Store a pointer to the packet so we can free it later,
1208 * and remember that txdirty will be once the packet is
1209 * done.
1210 */
1211 txs->txs_mbuf = m0;
1212 txs->txs_firstdesc = sc->sc_txnext;
1213 txs->txs_lastdesc = lasttx;
1214
1215 DPRINTF(WM_DEBUG_TX,
1216 ("%s: TX: finished transmitting packet, job %d\n",
1217 sc->sc_dev.dv_xname, sc->sc_txsnext));
1218
1219 /* Advance the tx pointer. */
1220 sc->sc_txfree -= dmamap->dm_nsegs;
1221 sc->sc_txnext = nexttx;
1222
1223 sc->sc_txsfree--;
1224 sc->sc_txsnext = WM_NEXTTXS(sc->sc_txsnext);
1225
1226 #if NBPFILTER > 0
1227 /* Pass the packet to any BPF listeners. */
1228 if (ifp->if_bpf)
1229 bpf_mtap(ifp->if_bpf, m0);
1230 #endif /* NBPFILTER > 0 */
1231 }
1232
1233 if (sc->sc_txsfree == 0 || sc->sc_txfree == 0) {
1234 /* No more slots; notify upper layer. */
1235 ifp->if_flags |= IFF_OACTIVE;
1236 }
1237
1238 if (sc->sc_txfree != ofree) {
1239 /* Set a watchdog timer in case the chip flakes out. */
1240 ifp->if_timer = 5;
1241 }
1242 }
1243
1244 /*
1245 * wm_watchdog: [ifnet interface function]
1246 *
1247 * Watchdog timer handler.
1248 */
1249 void
1250 wm_watchdog(struct ifnet *ifp)
1251 {
1252 struct wm_softc *sc = ifp->if_softc;
1253
1254 /*
1255 * Since we're using delayed interrupts, sweep up
1256 * before we report an error.
1257 */
1258 wm_txintr(sc);
1259
1260 if (sc->sc_txfree != WM_NTXDESC) {
1261 printf("%s: device timeout (txfree %d txsfree %d)\n",
1262 sc->sc_dev.dv_xname, sc->sc_txfree, sc->sc_txsfree);
1263 ifp->if_oerrors++;
1264
1265 /* Reset the interface. */
1266 (void) wm_init(ifp);
1267 }
1268
1269 /* Try to get more packets going. */
1270 wm_start(ifp);
1271 }
1272
1273 /*
1274 * wm_ioctl: [ifnet interface function]
1275 *
1276 * Handle control requests from the operator.
1277 */
1278 int
1279 wm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1280 {
1281 struct wm_softc *sc = ifp->if_softc;
1282 struct ifreq *ifr = (struct ifreq *) data;
1283 int s, error;
1284
1285 s = splnet();
1286
1287 switch (cmd) {
1288 case SIOCSIFMEDIA:
1289 case SIOCGIFMEDIA:
1290 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1291 break;
1292
1293 default:
1294 error = ether_ioctl(ifp, cmd, data);
1295 if (error == ENETRESET) {
1296 /*
1297 * Multicast list has changed; set the hardware filter
1298 * accordingly.
1299 */
1300 wm_set_filter(sc);
1301 error = 0;
1302 }
1303 break;
1304 }
1305
1306 /* Try to get more packets going. */
1307 wm_start(ifp);
1308
1309 splx(s);
1310 return (error);
1311 }
1312
1313 /*
1314 * wm_intr:
1315 *
1316 * Interrupt service routine.
1317 */
1318 int
1319 wm_intr(void *arg)
1320 {
1321 struct wm_softc *sc = arg;
1322 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1323 uint32_t icr;
1324 int wantinit, handled = 0;
1325
1326 for (wantinit = 0; wantinit == 0;) {
1327 icr = CSR_READ(sc, WMREG_ICR);
1328 if ((icr & sc->sc_icr) == 0)
1329 break;
1330
1331 handled = 1;
1332
1333 if (icr & (ICR_RXDMT0|ICR_RXT0)) {
1334 DPRINTF(WM_DEBUG_RX,
1335 ("%s: RX: got Rx intr 0x%08x\n",
1336 sc->sc_dev.dv_xname,
1337 icr & (ICR_RXDMT0|ICR_RXT0)));
1338 WM_EVCNT_INCR(&sc->sc_ev_rxintr);
1339 wm_rxintr(sc);
1340 }
1341
1342 if (icr & ICR_TXDW) {
1343 DPRINTF(WM_DEBUG_TX,
1344 ("%s: TX: got TXDW interrupt\n",
1345 sc->sc_dev.dv_xname));
1346 WM_EVCNT_INCR(&sc->sc_ev_txintr);
1347 wm_txintr(sc);
1348 }
1349
1350 if (icr & (ICR_LSC|ICR_RXSEQ|ICR_RXCFG)) {
1351 WM_EVCNT_INCR(&sc->sc_ev_linkintr);
1352 wm_linkintr(sc, icr);
1353 }
1354
1355 if (icr & ICR_RXO) {
1356 printf("%s: Receive overrun\n", sc->sc_dev.dv_xname);
1357 wantinit = 1;
1358 }
1359 }
1360
1361 if (handled) {
1362 if (wantinit)
1363 wm_init(ifp);
1364
1365 /* Try to get more packets going. */
1366 wm_start(ifp);
1367 }
1368
1369 return (handled);
1370 }
1371
1372 /*
1373 * wm_txintr:
1374 *
1375 * Helper; handle transmit interrupts.
1376 */
1377 void
1378 wm_txintr(struct wm_softc *sc)
1379 {
1380 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1381 struct wm_txsoft *txs;
1382 uint8_t status;
1383 int i;
1384
1385 ifp->if_flags &= ~IFF_OACTIVE;
1386
1387 /*
1388 * Go through the Tx list and free mbufs for those
1389 * frams which have been transmitted.
1390 */
1391 for (i = sc->sc_txsdirty; sc->sc_txsfree != WM_TXQUEUELEN;
1392 i = WM_NEXTTXS(i), sc->sc_txsfree++) {
1393 txs = &sc->sc_txsoft[i];
1394
1395 DPRINTF(WM_DEBUG_TX,
1396 ("%s: TX: checking job %d\n", sc->sc_dev.dv_xname, i));
1397
1398 WM_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
1399 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1400
1401 status = le32toh(sc->sc_txdescs[
1402 txs->txs_lastdesc].wtx_fields.wtxu_bits);
1403 if ((status & WTX_ST_DD) == 0)
1404 break;
1405
1406 DPRINTF(WM_DEBUG_TX,
1407 ("%s: TX: job %d done: descs %d..%d\n",
1408 sc->sc_dev.dv_xname, i, txs->txs_firstdesc,
1409 txs->txs_lastdesc));
1410
1411 /*
1412 * XXX We should probably be using the statistics
1413 * XXX registers, but I don't know if they exist
1414 * XXX on chips before the Cordova.
1415 */
1416
1417 #ifdef WM_EVENT_COUNTERS
1418 if (status & WTX_ST_TU)
1419 WM_EVCNT_INCR(&sc->sc_ev_tu);
1420 #endif /* WM_EVENT_COUNTERS */
1421
1422 if (status & (WTX_ST_EC|WTX_ST_LC)) {
1423 ifp->if_oerrors++;
1424 if (status & WTX_ST_LC)
1425 printf("%s: late collision\n",
1426 sc->sc_dev.dv_xname);
1427 else if (status & WTX_ST_EC) {
1428 ifp->if_collisions += 16;
1429 printf("%s: excessive collisions\n",
1430 sc->sc_dev.dv_xname);
1431 }
1432 } else
1433 ifp->if_opackets++;
1434
1435 sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
1436 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1437 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1438 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1439 m_freem(txs->txs_mbuf);
1440 txs->txs_mbuf = NULL;
1441 }
1442
1443 /* Update the dirty transmit buffer pointer. */
1444 sc->sc_txsdirty = i;
1445 DPRINTF(WM_DEBUG_TX,
1446 ("%s: TX: txsdirty -> %d\n", sc->sc_dev.dv_xname, i));
1447
1448 /*
1449 * If there are no more pending transmissions, cancel the watchdog
1450 * timer.
1451 */
1452 if (sc->sc_txsfree == WM_TXQUEUELEN)
1453 ifp->if_timer = 0;
1454 }
1455
1456 /*
1457 * wm_rxintr:
1458 *
1459 * Helper; handle receive interrupts.
1460 */
1461 void
1462 wm_rxintr(struct wm_softc *sc)
1463 {
1464 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1465 struct wm_rxsoft *rxs;
1466 struct mbuf *m;
1467 int i, len;
1468 uint8_t status, errors;
1469
1470 for (i = sc->sc_rxptr;; i = WM_NEXTRX(i)) {
1471 rxs = &sc->sc_rxsoft[i];
1472
1473 DPRINTF(WM_DEBUG_RX,
1474 ("%s: RX: checking descriptor %d\n",
1475 sc->sc_dev.dv_xname, i));
1476
1477 WM_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1478
1479 status = sc->sc_rxdescs[i].wrx_status;
1480 errors = sc->sc_rxdescs[i].wrx_errors;
1481 len = le16toh(sc->sc_rxdescs[i].wrx_len);
1482
1483 if ((status & WRX_ST_DD) == 0) {
1484 /*
1485 * We have processed all of the receive descriptors.
1486 */
1487 break;
1488 }
1489
1490 if (__predict_false(sc->sc_rxdiscard)) {
1491 DPRINTF(WM_DEBUG_RX,
1492 ("%s: RX: discarding contents of descriptor %d\n",
1493 sc->sc_dev.dv_xname, i));
1494 WM_INIT_RXDESC(sc, i);
1495 if (status & WRX_ST_EOP) {
1496 /* Reset our state. */
1497 DPRINTF(WM_DEBUG_RX,
1498 ("%s: RX: resetting rxdiscard -> 0\n",
1499 sc->sc_dev.dv_xname));
1500 sc->sc_rxdiscard = 0;
1501 }
1502 continue;
1503 }
1504
1505 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1506 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1507
1508 m = rxs->rxs_mbuf;
1509
1510 /*
1511 * Add a new receive buffer to the ring.
1512 */
1513 if (wm_add_rxbuf(sc, i) != 0) {
1514 /*
1515 * Failed, throw away what we've done so
1516 * far, and discard the rest of the packet.
1517 */
1518 ifp->if_ierrors++;
1519 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1520 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1521 WM_INIT_RXDESC(sc, i);
1522 if ((status & WRX_ST_EOP) == 0)
1523 sc->sc_rxdiscard = 1;
1524 if (sc->sc_rxhead != NULL)
1525 m_freem(sc->sc_rxhead);
1526 WM_RXCHAIN_RESET(sc);
1527 DPRINTF(WM_DEBUG_RX,
1528 ("%s: RX: Rx buffer allocation failed, "
1529 "dropping packet%s\n", sc->sc_dev.dv_xname,
1530 sc->sc_rxdiscard ? " (discard)" : ""));
1531 continue;
1532 }
1533
1534 WM_RXCHAIN_LINK(sc, m);
1535
1536 m->m_len = len;
1537
1538 DPRINTF(WM_DEBUG_RX,
1539 ("%s: RX: buffer at %p len %d\n",
1540 sc->sc_dev.dv_xname, m->m_data, len));
1541
1542 /*
1543 * If this is not the end of the packet, keep
1544 * looking.
1545 */
1546 if ((status & WRX_ST_EOP) == 0) {
1547 sc->sc_rxlen += len;
1548 DPRINTF(WM_DEBUG_RX,
1549 ("%s: RX: not yet EOP, rxlen -> %d\n",
1550 sc->sc_dev.dv_xname, sc->sc_rxlen));
1551 continue;
1552 }
1553
1554 /*
1555 * Okay, we have the entire packet now...
1556 */
1557 *sc->sc_rxtailp = NULL;
1558 m = sc->sc_rxhead;
1559 len += sc->sc_rxlen;
1560
1561 WM_RXCHAIN_RESET(sc);
1562
1563 DPRINTF(WM_DEBUG_RX,
1564 ("%s: RX: have entire packet, len -> %d\n",
1565 sc->sc_dev.dv_xname, len));
1566
1567 /*
1568 * If an error occurred, update stats and drop the packet.
1569 */
1570 if (errors &
1571 (WRX_ER_CE|WRX_ER_SE|WRX_ER_SEQ|WRX_ER_CXE|WRX_ER_RXE)) {
1572 ifp->if_ierrors++;
1573 if (errors & WRX_ER_SE)
1574 printf("%s: symbol error\n",
1575 sc->sc_dev.dv_xname);
1576 else if (errors & WRX_ER_SEQ)
1577 printf("%s: receive sequence error\n",
1578 sc->sc_dev.dv_xname);
1579 else if (errors & WRX_ER_CE)
1580 printf("%s: CRC error\n",
1581 sc->sc_dev.dv_xname);
1582 m_freem(m);
1583 continue;
1584 }
1585
1586 /*
1587 * No errors. Receive the packet.
1588 *
1589 * Note, we have configured the chip to include the
1590 * CRC with every packet.
1591 */
1592 m->m_flags |= M_HASFCS;
1593 m->m_pkthdr.rcvif = ifp;
1594 m->m_pkthdr.len = len;
1595
1596 #if 0 /* XXXJRT */
1597 /*
1598 * If VLANs are enabled, VLAN packets have been unwrapped
1599 * for us. Associate the tag with the packet.
1600 */
1601 if (sc->sc_ethercom.ec_nvlans != 0 &&
1602 (status & WRX_ST_VP) != 0) {
1603 struct mbuf *vtag;
1604
1605 vtag = m_aux_add(m, AF_LINK, ETHERTYPE_VLAN);
1606 if (vtag == NULL) {
1607 ifp->if_ierrors++;
1608 printf("%s: unable to allocate VLAN tag\n",
1609 sc->sc_dev.dv_xname);
1610 m_freem(m);
1611 continue;
1612 }
1613
1614 *mtod(m, int *) =
1615 le16toh(sc->sc_rxdescs[i].wrx_special);
1616 vtag->m_len = sizeof(int);
1617 }
1618 #endif /* XXXJRT */
1619
1620 /*
1621 * Set up checksum info for this packet.
1622 */
1623 if (status & WRX_ST_IPCS) {
1624 WM_EVCNT_INCR(&sc->sc_ev_rxipsum);
1625 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1626 if (errors & WRX_ER_IPE)
1627 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1628 }
1629 if (status & WRX_ST_TCPCS) {
1630 /*
1631 * Note: we don't know if this was TCP or UDP,
1632 * so we just set both bits, and expect the
1633 * upper layers to deal.
1634 */
1635 WM_EVCNT_INCR(&sc->sc_ev_rxtusum);
1636 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4|M_CSUM_UDPv4;
1637 if (errors & WRX_ER_TCPE)
1638 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1639 }
1640
1641 ifp->if_ipackets++;
1642
1643 #if NBPFILTER > 0
1644 /* Pass this up to any BPF listeners. */
1645 if (ifp->if_bpf)
1646 bpf_mtap(ifp->if_bpf, m);
1647 #endif /* NBPFILTER > 0 */
1648
1649 /* Pass it on. */
1650 (*ifp->if_input)(ifp, m);
1651 }
1652
1653 /* Update the receive pointer. */
1654 sc->sc_rxptr = i;
1655
1656 DPRINTF(WM_DEBUG_RX,
1657 ("%s: RX: rxptr -> %d\n", sc->sc_dev.dv_xname, i));
1658 }
1659
1660 /*
1661 * wm_linkintr:
1662 *
1663 * Helper; handle link interrupts.
1664 */
1665 void
1666 wm_linkintr(struct wm_softc *sc, uint32_t icr)
1667 {
1668 uint32_t status;
1669
1670 /*
1671 * If we get a link status interrupt on a 1000BASE-T
1672 * device, just fall into the normal MII tick path.
1673 */
1674 if (sc->sc_flags & WM_F_HAS_MII) {
1675 if (icr & ICR_LSC) {
1676 DPRINTF(WM_DEBUG_LINK,
1677 ("%s: LINK: LSC -> mii_tick\n",
1678 sc->sc_dev.dv_xname));
1679 mii_tick(&sc->sc_mii);
1680 } else if (icr & ICR_RXSEQ) {
1681 DPRINTF(WM_DEBUG_LINK,
1682 ("%s: LINK Receive sequence error\n",
1683 sc->sc_dev.dv_xname));
1684 }
1685 return;
1686 }
1687
1688 /*
1689 * If we are now receiving /C/, check for link again in
1690 * a couple of link clock ticks.
1691 */
1692 if (icr & ICR_RXCFG) {
1693 DPRINTF(WM_DEBUG_LINK, ("%s: LINK: receiving /C/\n",
1694 sc->sc_dev.dv_xname));
1695 sc->sc_tbi_anstate = 2;
1696 }
1697
1698 if (icr & ICR_LSC) {
1699 status = CSR_READ(sc, WMREG_STATUS);
1700 if (status & STATUS_LU) {
1701 DPRINTF(WM_DEBUG_LINK, ("%s: LINK: LSC -> up %s\n",
1702 sc->sc_dev.dv_xname,
1703 (status & STATUS_FD) ? "FDX" : "HDX"));
1704 sc->sc_tctl &= ~TCTL_COLD(0x3ff);
1705 if (status & STATUS_FD)
1706 sc->sc_tctl |=
1707 TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
1708 else
1709 sc->sc_tctl |=
1710 TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
1711 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
1712 sc->sc_tbi_linkup = 1;
1713 } else {
1714 DPRINTF(WM_DEBUG_LINK, ("%s: LINK: LSC -> down\n",
1715 sc->sc_dev.dv_xname));
1716 sc->sc_tbi_linkup = 0;
1717 }
1718 sc->sc_tbi_anstate = 2;
1719 wm_tbi_set_linkled(sc);
1720 } else if (icr & ICR_RXSEQ) {
1721 DPRINTF(WM_DEBUG_LINK,
1722 ("%s: LINK: Receive sequence error\n",
1723 sc->sc_dev.dv_xname));
1724 }
1725 }
1726
1727 /*
1728 * wm_tick:
1729 *
1730 * One second timer, used to check link status, sweep up
1731 * completed transmit jobs, etc.
1732 */
1733 void
1734 wm_tick(void *arg)
1735 {
1736 struct wm_softc *sc = arg;
1737 int s;
1738
1739 s = splnet();
1740
1741 if (sc->sc_flags & WM_F_HAS_MII)
1742 mii_tick(&sc->sc_mii);
1743 else
1744 wm_tbi_check_link(sc);
1745
1746 splx(s);
1747
1748 callout_reset(&sc->sc_tick_ch, hz, wm_tick, sc);
1749 }
1750
1751 /*
1752 * wm_reset:
1753 *
1754 * Reset the i82542 chip.
1755 */
1756 void
1757 wm_reset(struct wm_softc *sc)
1758 {
1759 int i;
1760
1761 CSR_WRITE(sc, WMREG_CTRL, CTRL_RST);
1762 delay(10000);
1763
1764 for (i = 0; i < 1000; i++) {
1765 if ((CSR_READ(sc, WMREG_CTRL) & CTRL_RST) == 0)
1766 return;
1767 delay(20);
1768 }
1769
1770 if (CSR_READ(sc, WMREG_CTRL) & CTRL_RST)
1771 printf("%s: WARNING: reset failed to complete\n",
1772 sc->sc_dev.dv_xname);
1773 }
1774
1775 /*
1776 * wm_init: [ifnet interface function]
1777 *
1778 * Initialize the interface. Must be called at splnet().
1779 */
1780 int
1781 wm_init(struct ifnet *ifp)
1782 {
1783 struct wm_softc *sc = ifp->if_softc;
1784 struct wm_rxsoft *rxs;
1785 int i, error = 0;
1786 uint32_t reg;
1787
1788 /* Cancel any pending I/O. */
1789 wm_stop(ifp, 0);
1790
1791 /* Reset the chip to a known state. */
1792 wm_reset(sc);
1793
1794 /* Initialize the transmit descriptor ring. */
1795 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1796 WM_CDTXSYNC(sc, 0, WM_NTXDESC,
1797 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1798 sc->sc_txfree = WM_NTXDESC;
1799 sc->sc_txnext = 0;
1800
1801 if (sc->sc_type < WM_T_LIVENGOOD) {
1802 CSR_WRITE(sc, WMREG_OLD_TBDAH, 0);
1803 CSR_WRITE(sc, WMREG_OLD_TBDAL, WM_CDTXADDR(sc, 0));
1804 CSR_WRITE(sc, WMREG_OLD_TDLEN, sizeof(sc->sc_txdescs));
1805 CSR_WRITE(sc, WMREG_OLD_TDH, 0);
1806 CSR_WRITE(sc, WMREG_OLD_TDT, 0);
1807 CSR_WRITE(sc, WMREG_OLD_TIDV, 64);
1808 } else {
1809 CSR_WRITE(sc, WMREG_TBDAH, 0);
1810 CSR_WRITE(sc, WMREG_TBDAL, WM_CDTXADDR(sc, 0));
1811 CSR_WRITE(sc, WMREG_TDLEN, sizeof(sc->sc_txdescs));
1812 CSR_WRITE(sc, WMREG_TDH, 0);
1813 CSR_WRITE(sc, WMREG_TDT, 0);
1814 CSR_WRITE(sc, WMREG_TIDV, 64);
1815
1816 CSR_WRITE(sc, WMREG_TXDCTL, TXDCTL_PTHRESH(0) |
1817 TXDCTL_HTHRESH(0) | TXDCTL_WTHRESH(0));
1818 CSR_WRITE(sc, WMREG_RXDCTL, RXDCTL_PTHRESH(0) |
1819 RXDCTL_HTHRESH(0) | RXDCTL_WTHRESH(1));
1820 }
1821 CSR_WRITE(sc, WMREG_TQSA_LO, 0);
1822 CSR_WRITE(sc, WMREG_TQSA_HI, 0);
1823
1824 /* Initialize the transmit job descriptors. */
1825 for (i = 0; i < WM_TXQUEUELEN; i++)
1826 sc->sc_txsoft[i].txs_mbuf = NULL;
1827 sc->sc_txsfree = WM_TXQUEUELEN;
1828 sc->sc_txsnext = 0;
1829 sc->sc_txsdirty = 0;
1830
1831 /*
1832 * Initialize the receive descriptor and receive job
1833 * descriptor rings.
1834 */
1835 if (sc->sc_type < WM_T_LIVENGOOD) {
1836 CSR_WRITE(sc, WMREG_OLD_RDBAH0, 0);
1837 CSR_WRITE(sc, WMREG_OLD_RDBAL0, WM_CDRXADDR(sc, 0));
1838 CSR_WRITE(sc, WMREG_OLD_RDLEN0, sizeof(sc->sc_rxdescs));
1839 CSR_WRITE(sc, WMREG_OLD_RDH0, 0);
1840 CSR_WRITE(sc, WMREG_OLD_RDT0, 0);
1841 CSR_WRITE(sc, WMREG_OLD_RDTR0, 64 | RDTR_FPD);
1842
1843 CSR_WRITE(sc, WMREG_OLD_RDBA1_HI, 0);
1844 CSR_WRITE(sc, WMREG_OLD_RDBA1_LO, 0);
1845 CSR_WRITE(sc, WMREG_OLD_RDLEN1, 0);
1846 CSR_WRITE(sc, WMREG_OLD_RDH1, 0);
1847 CSR_WRITE(sc, WMREG_OLD_RDT1, 0);
1848 CSR_WRITE(sc, WMREG_OLD_RDTR1, 0);
1849 } else {
1850 CSR_WRITE(sc, WMREG_RDBAH, 0);
1851 CSR_WRITE(sc, WMREG_RDBAL, WM_CDRXADDR(sc, 0));
1852 CSR_WRITE(sc, WMREG_RDLEN, sizeof(sc->sc_rxdescs));
1853 CSR_WRITE(sc, WMREG_RDH, 0);
1854 CSR_WRITE(sc, WMREG_RDT, 0);
1855 CSR_WRITE(sc, WMREG_RDTR, 64 | RDTR_FPD);
1856 }
1857 for (i = 0; i < WM_NRXDESC; i++) {
1858 rxs = &sc->sc_rxsoft[i];
1859 if (rxs->rxs_mbuf == NULL) {
1860 if ((error = wm_add_rxbuf(sc, i)) != 0) {
1861 printf("%s: unable to allocate or map rx "
1862 "buffer %d, error = %d\n",
1863 sc->sc_dev.dv_xname, i, error);
1864 /*
1865 * XXX Should attempt to run with fewer receive
1866 * XXX buffers instead of just failing.
1867 */
1868 wm_rxdrain(sc);
1869 goto out;
1870 }
1871 } else
1872 WM_INIT_RXDESC(sc, i);
1873 }
1874 sc->sc_rxptr = 0;
1875 sc->sc_rxdiscard = 0;
1876 WM_RXCHAIN_RESET(sc);
1877
1878 /*
1879 * Clear out the VLAN table -- we don't use it (yet).
1880 */
1881 CSR_WRITE(sc, WMREG_VET, 0);
1882 for (i = 0; i < WM_VLAN_TABSIZE; i++)
1883 CSR_WRITE(sc, WMREG_VFTA + (i << 2), 0);
1884
1885 /*
1886 * Set up flow-control parameters.
1887 *
1888 * XXX Values could probably stand some tuning.
1889 */
1890 if (sc->sc_ctrl & (CTRL_RFCE|CTRL_TFCE)) {
1891 CSR_WRITE(sc, WMREG_FCAL, FCAL_CONST);
1892 CSR_WRITE(sc, WMREG_FCAH, FCAH_CONST);
1893 CSR_WRITE(sc, WMREG_FCT, ETHERTYPE_FLOWCONTROL);
1894
1895 if (sc->sc_type < WM_T_LIVENGOOD) {
1896 CSR_WRITE(sc, WMREG_OLD_FCRTH, FCRTH_DFLT);
1897 CSR_WRITE(sc, WMREG_OLD_FCRTL, FCRTL_DFLT);
1898 } else {
1899 CSR_WRITE(sc, WMREG_FCRTH, FCRTH_DFLT);
1900 CSR_WRITE(sc, WMREG_FCRTL, FCRTL_DFLT);
1901 }
1902 CSR_WRITE(sc, WMREG_FCTTV, FCTTV_DFLT);
1903 }
1904
1905 #if 0 /* XXXJRT */
1906 /* Deal with VLAN enables. */
1907 if (sc->sc_ethercom.ec_nvlans != 0)
1908 sc->sc_ctrl |= CTRL_VME;
1909 else
1910 #endif /* XXXJRT */
1911 sc->sc_ctrl &= ~CTRL_VME;
1912
1913 /* Write the control registers. */
1914 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
1915 #if 0
1916 CSR_WRITE(sc, WMREG_CTRL_EXT, sc->sc_ctrl_ext);
1917 #endif
1918
1919 /*
1920 * Set up checksum offload parameters.
1921 */
1922 reg = CSR_READ(sc, WMREG_RXCSUM);
1923 if (ifp->if_capenable & IFCAP_CSUM_IPv4)
1924 reg |= RXCSUM_IPOFL;
1925 else
1926 reg &= ~RXCSUM_IPOFL;
1927 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4))
1928 reg |= RXCSUM_TUOFL;
1929 else
1930 reg &= ~RXCSUM_TUOFL;
1931 CSR_WRITE(sc, WMREG_RXCSUM, reg);
1932
1933 /*
1934 * Set up the interrupt registers.
1935 */
1936 CSR_WRITE(sc, WMREG_IMC, 0xffffffffU);
1937 sc->sc_icr = ICR_TXDW | ICR_LSC | ICR_RXSEQ | ICR_RXDMT0 |
1938 ICR_RXO | ICR_RXT0;
1939 if ((sc->sc_flags & WM_F_HAS_MII) == 0)
1940 sc->sc_icr |= ICR_RXCFG;
1941 CSR_WRITE(sc, WMREG_IMS, sc->sc_icr);
1942
1943 /* Set up the inter-packet gap. */
1944 CSR_WRITE(sc, WMREG_TIPG, sc->sc_tipg);
1945
1946 #if 0 /* XXXJRT */
1947 /* Set the VLAN ethernetype. */
1948 CSR_WRITE(sc, WMREG_VET, ETHERTYPE_VLAN);
1949 #endif
1950
1951 /*
1952 * Set up the transmit control register; we start out with
1953 * a collision distance suitable for FDX, but update it whe
1954 * we resolve the media type.
1955 */
1956 sc->sc_tctl = TCTL_EN | TCTL_PSP | TCTL_CT(TX_COLLISION_THRESHOLD) |
1957 TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
1958 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
1959
1960 /* Set the media. */
1961 (void) (*sc->sc_mii.mii_media.ifm_change)(ifp);
1962
1963 /*
1964 * Set up the receive control register; we actually program
1965 * the register when we set the receive filter. Use multicast
1966 * address offset type 0.
1967 *
1968 * Only the Cordova has the ability to strip the incoming
1969 * CRC, so we don't enable that feature.
1970 */
1971 sc->sc_mchash_type = 0;
1972 sc->sc_rctl = RCTL_EN | RCTL_LBM_NONE | RCTL_RDMTS_1_2 | RCTL_2k |
1973 RCTL_DPF | RCTL_MO(sc->sc_mchash_type);
1974
1975 /* Set the receive filter. */
1976 wm_set_filter(sc);
1977
1978 /* Start the one second link check clock. */
1979 callout_reset(&sc->sc_tick_ch, hz, wm_tick, sc);
1980
1981 /* ...all done! */
1982 ifp->if_flags |= IFF_RUNNING;
1983 ifp->if_flags &= ~IFF_OACTIVE;
1984
1985 out:
1986 if (error)
1987 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1988 return (error);
1989 }
1990
1991 /*
1992 * wm_rxdrain:
1993 *
1994 * Drain the receive queue.
1995 */
1996 void
1997 wm_rxdrain(struct wm_softc *sc)
1998 {
1999 struct wm_rxsoft *rxs;
2000 int i;
2001
2002 for (i = 0; i < WM_NRXDESC; i++) {
2003 rxs = &sc->sc_rxsoft[i];
2004 if (rxs->rxs_mbuf != NULL) {
2005 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2006 m_freem(rxs->rxs_mbuf);
2007 rxs->rxs_mbuf = NULL;
2008 }
2009 }
2010 }
2011
2012 /*
2013 * wm_stop: [ifnet interface function]
2014 *
2015 * Stop transmission on the interface.
2016 */
2017 void
2018 wm_stop(struct ifnet *ifp, int disable)
2019 {
2020 struct wm_softc *sc = ifp->if_softc;
2021 struct wm_txsoft *txs;
2022 int i;
2023
2024 /* Stop the one second clock. */
2025 callout_stop(&sc->sc_tick_ch);
2026
2027 if (sc->sc_flags & WM_F_HAS_MII) {
2028 /* Down the MII. */
2029 mii_down(&sc->sc_mii);
2030 }
2031
2032 /* Stop the transmit and receive processes. */
2033 CSR_WRITE(sc, WMREG_TCTL, 0);
2034 CSR_WRITE(sc, WMREG_RCTL, 0);
2035
2036 /* Release any queued transmit buffers. */
2037 for (i = 0; i < WM_TXQUEUELEN; i++) {
2038 txs = &sc->sc_txsoft[i];
2039 if (txs->txs_mbuf != NULL) {
2040 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2041 m_freem(txs->txs_mbuf);
2042 txs->txs_mbuf = NULL;
2043 }
2044 }
2045
2046 if (disable)
2047 wm_rxdrain(sc);
2048
2049 /* Mark the interface as down and cancel the watchdog timer. */
2050 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2051 ifp->if_timer = 0;
2052 }
2053
2054 /*
2055 * wm_read_eeprom:
2056 *
2057 * Read data from the serial EEPROM.
2058 */
2059 void
2060 wm_read_eeprom(struct wm_softc *sc, int word, int wordcnt, uint16_t *data)
2061 {
2062 uint32_t reg;
2063 int i, x;
2064
2065 for (i = 0; i < wordcnt; i++) {
2066 /* Send CHIP SELECT for one clock tick. */
2067 CSR_WRITE(sc, WMREG_EECD, EECD_CS);
2068 delay(2);
2069
2070 /* Shift in the READ command. */
2071 for (x = 3; x > 0; x--) {
2072 reg = EECD_CS;
2073 if (UWIRE_OPC_READ & (1 << (x - 1)))
2074 reg |= EECD_DI;
2075 CSR_WRITE(sc, WMREG_EECD, reg);
2076 delay(2);
2077 CSR_WRITE(sc, WMREG_EECD, reg | EECD_SK);
2078 delay(2);
2079 CSR_WRITE(sc, WMREG_EECD, reg);
2080 delay(2);
2081 }
2082
2083 /* Shift in address. */
2084 for (x = 6; x > 0; x--) {
2085 reg = EECD_CS;
2086 if ((word + i) & (1 << (x - 1)))
2087 reg |= EECD_DI;
2088 CSR_WRITE(sc, WMREG_EECD, reg);
2089 delay(2);
2090 CSR_WRITE(sc, WMREG_EECD, reg | EECD_SK);
2091 delay(2);
2092 CSR_WRITE(sc, WMREG_EECD, reg);
2093 delay(2);
2094 }
2095
2096 /* Shift out the data. */
2097 reg = EECD_CS;
2098 data[i] = 0;
2099 for (x = 16; x > 0; x--) {
2100 CSR_WRITE(sc, WMREG_EECD, reg | EECD_SK);
2101 delay(2);
2102 if (CSR_READ(sc, WMREG_EECD) & EECD_DO)
2103 data[i] |= (1 << (x - 1));
2104 CSR_WRITE(sc, WMREG_EECD, reg);
2105 delay(2);
2106 }
2107
2108 /* Clear CHIP SELECT. */
2109 CSR_WRITE(sc, WMREG_EECD, 0);
2110 }
2111 }
2112
2113 /*
2114 * wm_add_rxbuf:
2115 *
2116 * Add a receive buffer to the indiciated descriptor.
2117 */
2118 int
2119 wm_add_rxbuf(struct wm_softc *sc, int idx)
2120 {
2121 struct wm_rxsoft *rxs = &sc->sc_rxsoft[idx];
2122 struct mbuf *m;
2123 int error;
2124
2125 MGETHDR(m, M_DONTWAIT, MT_DATA);
2126 if (m == NULL)
2127 return (ENOBUFS);
2128
2129 MCLGET(m, M_DONTWAIT);
2130 if ((m->m_flags & M_EXT) == 0) {
2131 m_freem(m);
2132 return (ENOBUFS);
2133 }
2134
2135 if (rxs->rxs_mbuf != NULL)
2136 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2137
2138 rxs->rxs_mbuf = m;
2139
2140 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
2141 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
2142 BUS_DMA_READ|BUS_DMA_NOWAIT);
2143 if (error) {
2144 printf("%s: unable to load rx DMA map %d, error = %d\n",
2145 sc->sc_dev.dv_xname, idx, error);
2146 panic("wm_add_rxbuf"); /* XXX XXX XXX */
2147 }
2148
2149 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2150 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
2151
2152 WM_INIT_RXDESC(sc, idx);
2153
2154 return (0);
2155 }
2156
2157 /*
2158 * wm_set_ral:
2159 *
2160 * Set an entery in the receive address list.
2161 */
2162 static void
2163 wm_set_ral(struct wm_softc *sc, const uint8_t *enaddr, int idx)
2164 {
2165 uint32_t ral_lo, ral_hi;
2166
2167 if (enaddr != NULL) {
2168 ral_lo = enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) |
2169 (enaddr[3] << 24);
2170 ral_hi = enaddr[4] | (enaddr[5] << 8);
2171 ral_hi |= RAL_AV;
2172 } else {
2173 ral_lo = 0;
2174 ral_hi = 0;
2175 }
2176
2177 if (sc->sc_type >= WM_T_CORDOVA) {
2178 CSR_WRITE(sc, WMREG_RAL_LO(WMREG_CORDOVA_RAL_BASE, idx),
2179 ral_lo);
2180 CSR_WRITE(sc, WMREG_RAL_HI(WMREG_CORDOVA_RAL_BASE, idx),
2181 ral_hi);
2182 } else {
2183 CSR_WRITE(sc, WMREG_RAL_LO(WMREG_RAL_BASE, idx), ral_lo);
2184 CSR_WRITE(sc, WMREG_RAL_HI(WMREG_RAL_BASE, idx), ral_hi);
2185 }
2186 }
2187
2188 /*
2189 * wm_mchash:
2190 *
2191 * Compute the hash of the multicast address for the 4096-bit
2192 * multicast filter.
2193 */
2194 static uint32_t
2195 wm_mchash(struct wm_softc *sc, const uint8_t *enaddr)
2196 {
2197 static const int lo_shift[4] = { 4, 3, 2, 0 };
2198 static const int hi_shift[4] = { 4, 5, 6, 8 };
2199 uint32_t hash;
2200
2201 hash = (enaddr[4] >> lo_shift[sc->sc_mchash_type]) |
2202 (((uint16_t) enaddr[5]) << hi_shift[sc->sc_mchash_type]);
2203
2204 return (hash & 0xfff);
2205 }
2206
2207 /*
2208 * wm_set_filter:
2209 *
2210 * Set up the receive filter.
2211 */
2212 void
2213 wm_set_filter(struct wm_softc *sc)
2214 {
2215 struct ethercom *ec = &sc->sc_ethercom;
2216 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2217 struct ether_multi *enm;
2218 struct ether_multistep step;
2219 bus_addr_t mta_reg;
2220 uint32_t hash, reg, bit;
2221 int i;
2222
2223 if (sc->sc_type >= WM_T_CORDOVA)
2224 mta_reg = WMREG_CORDOVA_MTA;
2225 else
2226 mta_reg = WMREG_MTA;
2227
2228 sc->sc_rctl &= ~(RCTL_BAM | RCTL_UPE | RCTL_MPE);
2229
2230 if (ifp->if_flags & IFF_BROADCAST)
2231 sc->sc_rctl |= RCTL_BAM;
2232 if (ifp->if_flags & IFF_PROMISC) {
2233 sc->sc_rctl |= RCTL_UPE;
2234 goto allmulti;
2235 }
2236
2237 /*
2238 * Set the station address in the first RAL slot, and
2239 * clear the remaining slots.
2240 */
2241 wm_set_ral(sc, LLADDR(ifp->if_sadl), 0);
2242 for (i = 1; i < WM_RAL_TABSIZE; i++)
2243 wm_set_ral(sc, NULL, i);
2244
2245 /* Clear out the multicast table. */
2246 for (i = 0; i < WM_MC_TABSIZE; i++)
2247 CSR_WRITE(sc, mta_reg + (i << 2), 0);
2248
2249 ETHER_FIRST_MULTI(step, ec, enm);
2250 while (enm != NULL) {
2251 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2252 /*
2253 * We must listen to a range of multicast addresses.
2254 * For now, just accept all multicasts, rather than
2255 * trying to set only those filter bits needed to match
2256 * the range. (At this time, the only use of address
2257 * ranges is for IP multicast routing, for which the
2258 * range is big enough to require all bits set.)
2259 */
2260 goto allmulti;
2261 }
2262
2263 hash = wm_mchash(sc, enm->enm_addrlo);
2264
2265 reg = (hash >> 5) & 0x7f;
2266 bit = hash & 0x1f;
2267
2268 hash = CSR_READ(sc, mta_reg + (reg << 2));
2269 hash |= 1U << bit;
2270
2271 /* XXX Hardware bug?? */
2272 if (sc->sc_type == WM_T_CORDOVA && (reg & 0xe) == 1) {
2273 bit = CSR_READ(sc, mta_reg + ((reg - 1) << 2));
2274 CSR_WRITE(sc, mta_reg + (reg << 2), hash);
2275 CSR_WRITE(sc, mta_reg + ((reg - 1) << 2), bit);
2276 } else
2277 CSR_WRITE(sc, mta_reg + (reg << 2), hash);
2278
2279 ETHER_NEXT_MULTI(step, enm);
2280 }
2281
2282 ifp->if_flags &= ~IFF_ALLMULTI;
2283 goto setit;
2284
2285 allmulti:
2286 ifp->if_flags |= IFF_ALLMULTI;
2287 sc->sc_rctl |= RCTL_MPE;
2288
2289 setit:
2290 CSR_WRITE(sc, WMREG_RCTL, sc->sc_rctl);
2291 }
2292
2293 /*
2294 * wm_tbi_mediainit:
2295 *
2296 * Initialize media for use on 1000BASE-X devices.
2297 */
2298 void
2299 wm_tbi_mediainit(struct wm_softc *sc)
2300 {
2301 const char *sep = "";
2302
2303 if (sc->sc_type < WM_T_LIVENGOOD)
2304 sc->sc_tipg = TIPG_WM_DFLT;
2305 else
2306 sc->sc_tipg = TIPG_LG_DFLT;
2307
2308 ifmedia_init(&sc->sc_mii.mii_media, 0, wm_tbi_mediachange,
2309 wm_tbi_mediastatus);
2310
2311 /*
2312 * SWD Pins:
2313 *
2314 * 0 = Link LED (output)
2315 * 1 = Loss Of Signal (input)
2316 */
2317 sc->sc_ctrl |= CTRL_SWDPIO(0);
2318 sc->sc_ctrl &= ~CTRL_SWDPIO(1);
2319
2320 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
2321
2322 #define ADD(s, m, d) \
2323 do { \
2324 printf("%s%s", sep, s); \
2325 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|(m), (d), NULL); \
2326 sep = ", "; \
2327 } while (/*CONSTCOND*/0)
2328
2329 printf("%s: ", sc->sc_dev.dv_xname);
2330 ADD("1000baseSX", IFM_1000_SX, ANAR_X_HD);
2331 ADD("1000baseSX-FDX", IFM_1000_SX|IFM_FDX, ANAR_X_FD);
2332 ADD("auto", IFM_AUTO, ANAR_X_FD|ANAR_X_HD);
2333 printf("\n");
2334
2335 #undef ADD
2336
2337 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
2338 }
2339
2340 /*
2341 * wm_tbi_mediastatus: [ifmedia interface function]
2342 *
2343 * Get the current interface media status on a 1000BASE-X device.
2344 */
2345 void
2346 wm_tbi_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2347 {
2348 struct wm_softc *sc = ifp->if_softc;
2349
2350 ifmr->ifm_status = IFM_AVALID;
2351 ifmr->ifm_active = IFM_ETHER;
2352
2353 if (sc->sc_tbi_linkup == 0) {
2354 ifmr->ifm_active |= IFM_NONE;
2355 return;
2356 }
2357
2358 ifmr->ifm_status |= IFM_ACTIVE;
2359 ifmr->ifm_active |= IFM_1000_SX;
2360 if (CSR_READ(sc, WMREG_STATUS) & STATUS_FD)
2361 ifmr->ifm_active |= IFM_FDX;
2362 }
2363
2364 /*
2365 * wm_tbi_mediachange: [ifmedia interface function]
2366 *
2367 * Set hardware to newly-selected media on a 1000BASE-X device.
2368 */
2369 int
2370 wm_tbi_mediachange(struct ifnet *ifp)
2371 {
2372 struct wm_softc *sc = ifp->if_softc;
2373 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
2374 uint32_t status;
2375 int i;
2376
2377 sc->sc_txcw = ife->ifm_data;
2378 if (sc->sc_ctrl & CTRL_RFCE)
2379 sc->sc_txcw |= ANAR_X_PAUSE_TOWARDS;
2380 if (sc->sc_ctrl & CTRL_TFCE)
2381 sc->sc_txcw |= ANAR_X_PAUSE_ASYM;
2382 sc->sc_txcw |= TXCW_ANE;
2383
2384 CSR_WRITE(sc, WMREG_TXCW, sc->sc_txcw);
2385 delay(10000);
2386
2387 sc->sc_tbi_anstate = 0;
2388
2389 if ((CSR_READ(sc, WMREG_CTRL) & CTRL_SWDPIN(1)) == 0) {
2390 /* Have signal; wait for the link to come up. */
2391 for (i = 0; i < 50; i++) {
2392 delay(10000);
2393 if (CSR_READ(sc, WMREG_STATUS) & STATUS_LU)
2394 break;
2395 }
2396
2397 status = CSR_READ(sc, WMREG_STATUS);
2398 if (status & STATUS_LU) {
2399 /* Link is up. */
2400 DPRINTF(WM_DEBUG_LINK,
2401 ("%s: LINK: set media -> link up %s\n",
2402 sc->sc_dev.dv_xname,
2403 (status & STATUS_FD) ? "FDX" : "HDX"));
2404 sc->sc_tctl &= ~TCTL_COLD(0x3ff);
2405 if (status & STATUS_FD)
2406 sc->sc_tctl |=
2407 TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
2408 else
2409 sc->sc_tctl |=
2410 TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
2411 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
2412 sc->sc_tbi_linkup = 1;
2413 } else {
2414 /* Link is down. */
2415 DPRINTF(WM_DEBUG_LINK,
2416 ("%s: LINK: set media -> link down\n",
2417 sc->sc_dev.dv_xname));
2418 sc->sc_tbi_linkup = 0;
2419 }
2420 } else {
2421 DPRINTF(WM_DEBUG_LINK, ("%s: LINK: set media -> no signal\n",
2422 sc->sc_dev.dv_xname));
2423 sc->sc_tbi_linkup = 0;
2424 }
2425
2426 wm_tbi_set_linkled(sc);
2427
2428 return (0);
2429 }
2430
2431 /*
2432 * wm_tbi_set_linkled:
2433 *
2434 * Update the link LED on 1000BASE-X devices.
2435 */
2436 void
2437 wm_tbi_set_linkled(struct wm_softc *sc)
2438 {
2439
2440 if (sc->sc_tbi_linkup)
2441 sc->sc_ctrl |= CTRL_SWDPIN(0);
2442 else
2443 sc->sc_ctrl &= ~CTRL_SWDPIN(0);
2444
2445 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
2446 }
2447
2448 /*
2449 * wm_tbi_check_link:
2450 *
2451 * Check the link on 1000BASE-X devices.
2452 */
2453 void
2454 wm_tbi_check_link(struct wm_softc *sc)
2455 {
2456 uint32_t rxcw, ctrl, status;
2457
2458 if (sc->sc_tbi_anstate == 0)
2459 return;
2460 else if (sc->sc_tbi_anstate > 1) {
2461 DPRINTF(WM_DEBUG_LINK,
2462 ("%s: LINK: anstate %d\n", sc->sc_dev.dv_xname,
2463 sc->sc_tbi_anstate));
2464 sc->sc_tbi_anstate--;
2465 return;
2466 }
2467
2468 sc->sc_tbi_anstate = 0;
2469
2470 rxcw = CSR_READ(sc, WMREG_RXCW);
2471 ctrl = CSR_READ(sc, WMREG_CTRL);
2472 status = CSR_READ(sc, WMREG_STATUS);
2473
2474 if ((status & STATUS_LU) == 0) {
2475 DPRINTF(WM_DEBUG_LINK,
2476 ("%s: LINK: checklink -> down\n", sc->sc_dev.dv_xname));
2477 sc->sc_tbi_linkup = 0;
2478 } else {
2479 DPRINTF(WM_DEBUG_LINK,
2480 ("%s: LINK: checklink -> up %s\n", sc->sc_dev.dv_xname,
2481 (status & STATUS_FD) ? "FDX" : "HDX"));
2482 sc->sc_tctl &= ~TCTL_COLD(0x3ff);
2483 if (status & STATUS_FD)
2484 sc->sc_tctl |=
2485 TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
2486 else
2487 sc->sc_tctl |=
2488 TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
2489 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
2490 sc->sc_tbi_linkup = 1;
2491 }
2492
2493 wm_tbi_set_linkled(sc);
2494 }
2495
2496 /*
2497 * wm_gmii_reset:
2498 *
2499 * Reset the PHY.
2500 */
2501 void
2502 wm_gmii_reset(struct wm_softc *sc)
2503 {
2504 uint32_t reg;
2505
2506 if (sc->sc_type >= WM_T_CORDOVA) {
2507 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl | CTRL_PHY_RESET);
2508 delay(20000);
2509
2510 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
2511 delay(20000);
2512 } else {
2513 /* The PHY reset pin is active-low. */
2514 reg = CSR_READ(sc, WMREG_CTRL_EXT);
2515 reg &= ~((CTRL_EXT_SWDPIO_MASK << CTRL_EXT_SWDPIO_SHIFT) |
2516 CTRL_EXT_SWDPIN(4));
2517 reg |= CTRL_EXT_SWDPIO(4);
2518
2519 CSR_WRITE(sc, WMREG_CTRL_EXT, reg | CTRL_EXT_SWDPIN(4));
2520 delay(10);
2521
2522 CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
2523 delay(10);
2524
2525 CSR_WRITE(sc, WMREG_CTRL_EXT, reg | CTRL_EXT_SWDPIN(4));
2526 delay(10);
2527 #if 0
2528 sc->sc_ctrl_ext = reg | CTRL_EXT_SWDPIN(4);
2529 #endif
2530 }
2531 }
2532
2533 /*
2534 * wm_gmii_mediainit:
2535 *
2536 * Initialize media for use on 1000BASE-T devices.
2537 */
2538 void
2539 wm_gmii_mediainit(struct wm_softc *sc)
2540 {
2541 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2542
2543 /* We have MII. */
2544 sc->sc_flags |= WM_F_HAS_MII;
2545
2546 sc->sc_tipg = TIPG_1000T_DFLT;
2547
2548 /*
2549 * Let the chip set speed/duplex on its own based on
2550 * signals from the PHY.
2551 */
2552 sc->sc_ctrl |= CTRL_SLU | CTRL_ASDE;
2553 CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
2554
2555 /* Initialize our media structures and probe the GMII. */
2556 sc->sc_mii.mii_ifp = ifp;
2557
2558 if (sc->sc_type >= WM_T_CORDOVA) {
2559 sc->sc_mii.mii_readreg = wm_gmii_cordova_readreg;
2560 sc->sc_mii.mii_writereg = wm_gmii_cordova_writereg;
2561 } else {
2562 sc->sc_mii.mii_readreg = wm_gmii_livengood_readreg;
2563 sc->sc_mii.mii_writereg = wm_gmii_livengood_writereg;
2564 }
2565 sc->sc_mii.mii_statchg = wm_gmii_statchg;
2566
2567 wm_gmii_reset(sc);
2568
2569 ifmedia_init(&sc->sc_mii.mii_media, 0, wm_gmii_mediachange,
2570 wm_gmii_mediastatus);
2571
2572 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
2573 MII_OFFSET_ANY, 0);
2574 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
2575 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
2576 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
2577 } else
2578 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
2579 }
2580
2581 /*
2582 * wm_gmii_mediastatus: [ifmedia interface function]
2583 *
2584 * Get the current interface media status on a 1000BASE-T device.
2585 */
2586 void
2587 wm_gmii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2588 {
2589 struct wm_softc *sc = ifp->if_softc;
2590
2591 mii_pollstat(&sc->sc_mii);
2592 ifmr->ifm_status = sc->sc_mii.mii_media_status;
2593 ifmr->ifm_active = sc->sc_mii.mii_media_active;
2594 }
2595
2596 /*
2597 * wm_gmii_mediachange: [ifmedia interface function]
2598 *
2599 * Set hardware to newly-selected media on a 1000BASE-T device.
2600 */
2601 int
2602 wm_gmii_mediachange(struct ifnet *ifp)
2603 {
2604 struct wm_softc *sc = ifp->if_softc;
2605
2606 if (ifp->if_flags & IFF_UP)
2607 mii_mediachg(&sc->sc_mii);
2608 return (0);
2609 }
2610
2611 #define MDI_IO CTRL_SWDPIN(2)
2612 #define MDI_DIR CTRL_SWDPIO(2) /* host -> PHY */
2613 #define MDI_CLK CTRL_SWDPIN(3)
2614
2615 static void
2616 livengood_mii_sendbits(struct wm_softc *sc, uint32_t data, int nbits)
2617 {
2618 uint32_t i, v;
2619
2620 v = CSR_READ(sc, WMREG_CTRL);
2621 v &= ~(MDI_IO|MDI_CLK|(CTRL_SWDPIO_MASK << CTRL_SWDPIO_SHIFT));
2622 v |= MDI_DIR | CTRL_SWDPIO(3);
2623
2624 for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
2625 if (data & i)
2626 v |= MDI_IO;
2627 else
2628 v &= ~MDI_IO;
2629 CSR_WRITE(sc, WMREG_CTRL, v);
2630 delay(10);
2631 CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
2632 delay(10);
2633 CSR_WRITE(sc, WMREG_CTRL, v);
2634 delay(10);
2635 }
2636 }
2637
2638 static uint32_t
2639 livengood_mii_recvbits(struct wm_softc *sc)
2640 {
2641 uint32_t v, i, data = 0;
2642
2643 v = CSR_READ(sc, WMREG_CTRL);
2644 v &= ~(MDI_IO|MDI_CLK|(CTRL_SWDPIO_MASK << CTRL_SWDPIO_SHIFT));
2645 v |= CTRL_SWDPIO(3);
2646
2647 CSR_WRITE(sc, WMREG_CTRL, v);
2648 delay(10);
2649 CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
2650 delay(10);
2651 CSR_WRITE(sc, WMREG_CTRL, v);
2652 delay(10);
2653
2654 for (i = 0; i < 16; i++) {
2655 data <<= 1;
2656 CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
2657 delay(10);
2658 if (CSR_READ(sc, WMREG_CTRL) & MDI_IO)
2659 data |= 1;
2660 CSR_WRITE(sc, WMREG_CTRL, v);
2661 delay(10);
2662 }
2663
2664 CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
2665 delay(10);
2666 CSR_WRITE(sc, WMREG_CTRL, v);
2667 delay(10);
2668
2669 return (data);
2670 }
2671
2672 #undef MDI_IO
2673 #undef MDI_DIR
2674 #undef MDI_CLK
2675
2676 /*
2677 * wm_gmii_livengood_readreg: [mii interface function]
2678 *
2679 * Read a PHY register on the GMII (Livengood version).
2680 */
2681 int
2682 wm_gmii_livengood_readreg(struct device *self, int phy, int reg)
2683 {
2684 struct wm_softc *sc = (void *) self;
2685 int rv;
2686
2687 livengood_mii_sendbits(sc, 0xffffffffU, 32);
2688 livengood_mii_sendbits(sc, reg | (phy << 5) |
2689 (MII_COMMAND_READ << 10) | (MII_COMMAND_START << 12), 14);
2690 rv = livengood_mii_recvbits(sc) & 0xffff;
2691
2692 DPRINTF(WM_DEBUG_GMII,
2693 ("%s: GMII: read phy %d reg %d -> 0x%04x\n",
2694 sc->sc_dev.dv_xname, phy, reg, rv));
2695
2696 return (rv);
2697 }
2698
2699 /*
2700 * wm_gmii_livengood_writereg: [mii interface function]
2701 *
2702 * Write a PHY register on the GMII (Livengood version).
2703 */
2704 void
2705 wm_gmii_livengood_writereg(struct device *self, int phy, int reg, int val)
2706 {
2707 struct wm_softc *sc = (void *) self;
2708
2709 livengood_mii_sendbits(sc, 0xffffffffU, 32);
2710 livengood_mii_sendbits(sc, val | (MII_COMMAND_ACK << 16) |
2711 (reg << 18) | (phy << 23) | (MII_COMMAND_WRITE << 28) |
2712 (MII_COMMAND_START << 30), 32);
2713 }
2714
2715 /*
2716 * wm_gmii_cordova_readreg: [mii interface function]
2717 *
2718 * Read a PHY register on the GMII.
2719 */
2720 int
2721 wm_gmii_cordova_readreg(struct device *self, int phy, int reg)
2722 {
2723 struct wm_softc *sc = (void *) self;
2724 uint32_t mdic;
2725 int i, rv;
2726
2727 CSR_WRITE(sc, WMREG_MDIC, MDIC_OP_READ | MDIC_PHYADD(phy) |
2728 MDIC_REGADD(reg));
2729
2730 for (i = 0; i < 100; i++) {
2731 mdic = CSR_READ(sc, WMREG_MDIC);
2732 if (mdic & MDIC_READY)
2733 break;
2734 delay(10);
2735 }
2736
2737 if ((mdic & MDIC_READY) == 0) {
2738 printf("%s: MDIC read timed out: phy %d reg %d\n",
2739 sc->sc_dev.dv_xname, phy, reg);
2740 rv = 0;
2741 } else if (mdic & MDIC_E) {
2742 #if 0 /* This is normal if no PHY is present. */
2743 printf("%s: MDIC read error: phy %d reg %d\n",
2744 sc->sc_dev.dv_xname, phy, reg);
2745 #endif
2746 rv = 0;
2747 } else {
2748 rv = MDIC_DATA(mdic);
2749 if (rv == 0xffff)
2750 rv = 0;
2751 }
2752
2753 return (rv);
2754 }
2755
2756 /*
2757 * wm_gmii_cordova_writereg: [mii interface function]
2758 *
2759 * Write a PHY register on the GMII.
2760 */
2761 void
2762 wm_gmii_cordova_writereg(struct device *self, int phy, int reg, int val)
2763 {
2764 struct wm_softc *sc = (void *) self;
2765 uint32_t mdic;
2766 int i;
2767
2768 CSR_WRITE(sc, WMREG_MDIC, MDIC_OP_WRITE | MDIC_PHYADD(phy) |
2769 MDIC_REGADD(reg) | MDIC_DATA(val));
2770
2771 for (i = 0; i < 100; i++) {
2772 mdic = CSR_READ(sc, WMREG_MDIC);
2773 if (mdic & MDIC_READY)
2774 break;
2775 delay(10);
2776 }
2777
2778 if ((mdic & MDIC_READY) == 0)
2779 printf("%s: MDIC write timed out: phy %d reg %d\n",
2780 sc->sc_dev.dv_xname, phy, reg);
2781 else if (mdic & MDIC_E)
2782 printf("%s: MDIC write error: phy %d reg %d\n",
2783 sc->sc_dev.dv_xname, phy, reg);
2784 }
2785
2786 /*
2787 * wm_gmii_statchg: [mii interface function]
2788 *
2789 * Callback from MII layer when media changes.
2790 */
2791 void
2792 wm_gmii_statchg(struct device *self)
2793 {
2794 struct wm_softc *sc = (void *) self;
2795
2796 sc->sc_tctl &= ~TCTL_COLD(0x3ff);
2797
2798 if (sc->sc_mii.mii_media_active & IFM_FDX) {
2799 DPRINTF(WM_DEBUG_LINK,
2800 ("%s: LINK: statchg: FDX\n", sc->sc_dev.dv_xname));
2801 sc->sc_tctl |= TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
2802 } else {
2803 DPRINTF(WM_DEBUG_LINK,
2804 ("%s: LINK: statchg: HDX\n", sc->sc_dev.dv_xname));
2805 sc->sc_tctl |= TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
2806 }
2807
2808 CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
2809 }
2810