if_msk.c revision 1.17 1 /* $NetBSD: if_msk.c,v 1.17 2008/03/28 08:46:01 kiyohara Exp $ */
2 /* $OpenBSD: if_msk.c,v 1.42 2007/01/17 02:43:02 krw Exp $ */
3
4 /*
5 * Copyright (c) 1997, 1998, 1999, 2000
6 * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Bill Paul.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * $FreeBSD: /c/ncvs/src/sys/pci/if_sk.c,v 1.20 2000/04/22 02:16:37 wpaul Exp $
36 */
37
38 /*
39 * Copyright (c) 2003 Nathan L. Binkert <binkertn (at) umich.edu>
40 *
41 * Permission to use, copy, modify, and distribute this software for any
42 * purpose with or without fee is hereby granted, provided that the above
43 * copyright notice and this permission notice appear in all copies.
44 *
45 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
46 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
47 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
48 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
49 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
50 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
51 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
52 */
53
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: if_msk.c,v 1.17 2008/03/28 08:46:01 kiyohara Exp $");
56
57 #include "bpfilter.h"
58 #include "rnd.h"
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/sockio.h>
63 #include <sys/mbuf.h>
64 #include <sys/malloc.h>
65 #include <sys/kernel.h>
66 #include <sys/socket.h>
67 #include <sys/device.h>
68 #include <sys/queue.h>
69 #include <sys/callout.h>
70 #include <sys/sysctl.h>
71 #include <sys/endian.h>
72 #ifdef __NetBSD__
73 #define letoh16 htole16
74 #define letoh32 htole32
75 #endif
76
77 #include <net/if.h>
78 #include <net/if_dl.h>
79 #include <net/if_types.h>
80
81 #include <net/if_media.h>
82
83 #if NBPFILTER > 0
84 #include <net/bpf.h>
85 #endif
86 #if NRND > 0
87 #include <sys/rnd.h>
88 #endif
89
90 #include <dev/mii/mii.h>
91 #include <dev/mii/miivar.h>
92 #include <dev/mii/brgphyreg.h>
93
94 #include <dev/pci/pcireg.h>
95 #include <dev/pci/pcivar.h>
96 #include <dev/pci/pcidevs.h>
97
98 #include <dev/pci/if_skreg.h>
99 #include <dev/pci/if_mskvar.h>
100
101 int mskc_probe(struct device *, struct cfdata *, void *);
102 void mskc_attach(struct device *, struct device *self, void *aux);
103 void mskc_shutdown(void *);
104 int msk_probe(struct device *, struct cfdata *, void *);
105 void msk_attach(struct device *, struct device *self, void *aux);
106 int mskcprint(void *, const char *);
107 int msk_intr(void *);
108 void msk_intr_yukon(struct sk_if_softc *);
109 __inline int msk_rxvalid(struct sk_softc *, u_int32_t, u_int32_t);
110 void msk_rxeof(struct sk_if_softc *, u_int16_t, u_int32_t);
111 void msk_txeof(struct sk_if_softc *, int);
112 int msk_encap(struct sk_if_softc *, struct mbuf *, u_int32_t *);
113 void msk_start(struct ifnet *);
114 int msk_ioctl(struct ifnet *, u_long, void *);
115 int msk_init(struct ifnet *);
116 void msk_init_yukon(struct sk_if_softc *);
117 void msk_stop(struct ifnet *, int);
118 void msk_watchdog(struct ifnet *);
119 void msk_reset(struct sk_softc *);
120 int msk_newbuf(struct sk_if_softc *, int, struct mbuf *, bus_dmamap_t);
121 int msk_alloc_jumbo_mem(struct sk_if_softc *);
122 void *msk_jalloc(struct sk_if_softc *);
123 void msk_jfree(struct mbuf *, void *, size_t, void *);
124 int msk_init_rx_ring(struct sk_if_softc *);
125 int msk_init_tx_ring(struct sk_if_softc *);
126
127 void msk_update_int_mod(struct sk_softc *);
128
129 int msk_miibus_readreg(struct device *, int, int);
130 void msk_miibus_writereg(struct device *, int, int, int);
131 void msk_miibus_statchg(struct device *);
132
133 void msk_setfilt(struct sk_if_softc *, void *, int);
134 void msk_setmulti(struct sk_if_softc *);
135 void msk_setpromisc(struct sk_if_softc *);
136 void msk_tick(void *);
137
138 /* #define MSK_DEBUG 1 */
139 #ifdef MSK_DEBUG
140 #define DPRINTF(x) if (mskdebug) printf x
141 #define DPRINTFN(n,x) if (mskdebug >= (n)) printf x
142 int mskdebug = MSK_DEBUG;
143
144 void msk_dump_txdesc(struct msk_tx_desc *, int);
145 void msk_dump_mbuf(struct mbuf *);
146 void msk_dump_bytes(const char *, int);
147 #else
148 #define DPRINTF(x)
149 #define DPRINTFN(n,x)
150 #endif
151
152 static int msk_sysctl_handler(SYSCTLFN_PROTO);
153 static int msk_root_num;
154
155 /* supported device vendors */
156 static const struct msk_product {
157 pci_vendor_id_t msk_vendor;
158 pci_product_id_t msk_product;
159 } msk_products[] = {
160 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE550SX },
161 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE560SX },
162 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE560T },
163 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_1 },
164 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_C032 },
165 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_C033 },
166 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_C034 },
167 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_C036 },
168 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_C042 },
169 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_C055 },
170 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8035 },
171 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8036 },
172 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8038 },
173 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8039 },
174 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8050 },
175 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8052 },
176 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8053 },
177 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8055 },
178 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKON_8056 },
179 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKONII_8021CU },
180 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKONII_8021X },
181 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKONII_8022CU },
182 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKONII_8022X },
183 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKONII_8061CU },
184 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKONII_8061X },
185 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKONII_8062CU },
186 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_YUKONII_8062X },
187 { PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SK_9SXX },
188 { PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SK_9E21 }
189 };
190
191 static inline u_int32_t
192 sk_win_read_4(struct sk_softc *sc, u_int32_t reg)
193 {
194 return CSR_READ_4(sc, reg);
195 }
196
197 static inline u_int16_t
198 sk_win_read_2(struct sk_softc *sc, u_int32_t reg)
199 {
200 return CSR_READ_2(sc, reg);
201 }
202
203 static inline u_int8_t
204 sk_win_read_1(struct sk_softc *sc, u_int32_t reg)
205 {
206 return CSR_READ_1(sc, reg);
207 }
208
209 static inline void
210 sk_win_write_4(struct sk_softc *sc, u_int32_t reg, u_int32_t x)
211 {
212 CSR_WRITE_4(sc, reg, x);
213 }
214
215 static inline void
216 sk_win_write_2(struct sk_softc *sc, u_int32_t reg, u_int16_t x)
217 {
218 CSR_WRITE_2(sc, reg, x);
219 }
220
221 static inline void
222 sk_win_write_1(struct sk_softc *sc, u_int32_t reg, u_int8_t x)
223 {
224 CSR_WRITE_1(sc, reg, x);
225 }
226
227 int
228 msk_miibus_readreg(struct device *dev, int phy, int reg)
229 {
230 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
231 u_int16_t val;
232 int i;
233
234 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
235 YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
236
237 for (i = 0; i < SK_TIMEOUT; i++) {
238 DELAY(1);
239 val = SK_YU_READ_2(sc_if, YUKON_SMICR);
240 if (val & YU_SMICR_READ_VALID)
241 break;
242 }
243
244 if (i == SK_TIMEOUT) {
245 aprint_error("%s: phy failed to come ready\n",
246 sc_if->sk_dev.dv_xname);
247 return (0);
248 }
249
250 DPRINTFN(9, ("msk_miibus_readreg: i=%d, timeout=%d\n", i,
251 SK_TIMEOUT));
252
253 val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
254
255 DPRINTFN(9, ("msk_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
256 phy, reg, val));
257
258 return (val);
259 }
260
261 void
262 msk_miibus_writereg(struct device *dev, int phy, int reg, int val)
263 {
264 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
265 int i;
266
267 DPRINTFN(9, ("msk_miibus_writereg phy=%d reg=%#x val=%#x\n",
268 phy, reg, val));
269
270 SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
271 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
272 YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
273
274 for (i = 0; i < SK_TIMEOUT; i++) {
275 DELAY(1);
276 if (!(SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY))
277 break;
278 }
279
280 if (i == SK_TIMEOUT)
281 aprint_error("%s: phy write timed out\n", sc_if->sk_dev.dv_xname);
282 }
283
284 void
285 msk_miibus_statchg(struct device *dev)
286 {
287 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
288 struct mii_data *mii = &sc_if->sk_mii;
289 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
290 int gpcr;
291
292 gpcr = SK_YU_READ_2(sc_if, YUKON_GPCR);
293 gpcr &= (YU_GPCR_TXEN | YU_GPCR_RXEN);
294
295 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
296 /* Set speed. */
297 gpcr |= YU_GPCR_SPEED_DIS;
298 switch (IFM_SUBTYPE(mii->mii_media_active)) {
299 case IFM_1000_SX:
300 case IFM_1000_LX:
301 case IFM_1000_CX:
302 case IFM_1000_T:
303 gpcr |= (YU_GPCR_GIG | YU_GPCR_SPEED);
304 break;
305 case IFM_100_TX:
306 gpcr |= YU_GPCR_SPEED;
307 break;
308 }
309
310 /* Set duplex. */
311 gpcr |= YU_GPCR_DPLX_DIS;
312 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
313 gpcr |= YU_GPCR_DUPLEX;
314
315 /* Disable flow control. */
316 gpcr |= YU_GPCR_FCTL_DIS;
317 gpcr |= (YU_GPCR_FCTL_TX_DIS | YU_GPCR_FCTL_RX_DIS);
318 }
319
320 SK_YU_WRITE_2(sc_if, YUKON_GPCR, gpcr);
321
322 DPRINTFN(9, ("msk_miibus_statchg: gpcr=%x\n",
323 SK_YU_READ_2(((struct sk_if_softc *)dev), YUKON_GPCR)));
324 }
325
326 #define HASH_BITS 6
327
328 void
329 msk_setfilt(struct sk_if_softc *sc_if, void *addrv, int slot)
330 {
331 char *addr = addrv;
332 int base = XM_RXFILT_ENTRY(slot);
333
334 SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
335 SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
336 SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
337 }
338
339 void
340 msk_setmulti(struct sk_if_softc *sc_if)
341 {
342 struct ifnet *ifp= &sc_if->sk_ethercom.ec_if;
343 u_int32_t hashes[2] = { 0, 0 };
344 int h;
345 struct ethercom *ec = &sc_if->sk_ethercom;
346 struct ether_multi *enm;
347 struct ether_multistep step;
348 u_int16_t reg;
349
350 /* First, zot all the existing filters. */
351 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
352 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
353 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
354 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
355
356
357 /* Now program new ones. */
358 reg = SK_YU_READ_2(sc_if, YUKON_RCR);
359 reg |= YU_RCR_UFLEN;
360 allmulti:
361 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
362 if ((ifp->if_flags & IFF_PROMISC) != 0)
363 reg &= ~(YU_RCR_UFLEN | YU_RCR_MUFLEN);
364 else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
365 hashes[0] = 0xFFFFFFFF;
366 hashes[1] = 0xFFFFFFFF;
367 }
368 } else {
369 /* First find the tail of the list. */
370 ETHER_FIRST_MULTI(step, ec, enm);
371 while (enm != NULL) {
372 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
373 ETHER_ADDR_LEN)) {
374 ifp->if_flags |= IFF_ALLMULTI;
375 goto allmulti;
376 }
377 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) &
378 ((1 << HASH_BITS) - 1);
379 if (h < 32)
380 hashes[0] |= (1 << h);
381 else
382 hashes[1] |= (1 << (h - 32));
383
384 ETHER_NEXT_MULTI(step, enm);
385 }
386 reg |= YU_RCR_MUFLEN;
387 }
388
389 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
390 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
391 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
392 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
393 SK_YU_WRITE_2(sc_if, YUKON_RCR, reg);
394 }
395
396 void
397 msk_setpromisc(struct sk_if_softc *sc_if)
398 {
399 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
400
401 if (ifp->if_flags & IFF_PROMISC)
402 SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
403 YU_RCR_UFLEN | YU_RCR_MUFLEN);
404 else
405 SK_YU_SETBIT_2(sc_if, YUKON_RCR,
406 YU_RCR_UFLEN | YU_RCR_MUFLEN);
407 }
408
409 int
410 msk_init_rx_ring(struct sk_if_softc *sc_if)
411 {
412 struct msk_chain_data *cd = &sc_if->sk_cdata;
413 struct msk_ring_data *rd = sc_if->sk_rdata;
414 int i, nexti;
415
416 bzero((char *)rd->sk_rx_ring,
417 sizeof(struct msk_rx_desc) * MSK_RX_RING_CNT);
418
419 for (i = 0; i < MSK_RX_RING_CNT; i++) {
420 cd->sk_rx_chain[i].sk_le = &rd->sk_rx_ring[i];
421 if (i == (MSK_RX_RING_CNT - 1))
422 nexti = 0;
423 else
424 nexti = i + 1;
425 cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[nexti];
426 }
427
428 for (i = 0; i < MSK_RX_RING_CNT; i++) {
429 if (msk_newbuf(sc_if, i, NULL,
430 sc_if->sk_cdata.sk_rx_jumbo_map) == ENOBUFS) {
431 aprint_error("%s: failed alloc of %dth mbuf\n",
432 sc_if->sk_dev.dv_xname, i);
433 return (ENOBUFS);
434 }
435 }
436
437 sc_if->sk_cdata.sk_rx_prod = MSK_RX_RING_CNT - 1;
438 sc_if->sk_cdata.sk_rx_cons = 0;
439
440 return (0);
441 }
442
443 int
444 msk_init_tx_ring(struct sk_if_softc *sc_if)
445 {
446 struct sk_softc *sc = sc_if->sk_softc;
447 struct msk_chain_data *cd = &sc_if->sk_cdata;
448 struct msk_ring_data *rd = sc_if->sk_rdata;
449 bus_dmamap_t dmamap;
450 struct sk_txmap_entry *entry;
451 int i, nexti;
452
453 bzero((char *)sc_if->sk_rdata->sk_tx_ring,
454 sizeof(struct msk_tx_desc) * MSK_TX_RING_CNT);
455
456 SIMPLEQ_INIT(&sc_if->sk_txmap_head);
457 for (i = 0; i < MSK_TX_RING_CNT; i++) {
458 cd->sk_tx_chain[i].sk_le = &rd->sk_tx_ring[i];
459 if (i == (MSK_TX_RING_CNT - 1))
460 nexti = 0;
461 else
462 nexti = i + 1;
463 cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[nexti];
464
465 if (bus_dmamap_create(sc->sc_dmatag, SK_JLEN, SK_NTXSEG,
466 SK_JLEN, 0, BUS_DMA_NOWAIT, &dmamap))
467 return (ENOBUFS);
468
469 entry = malloc(sizeof(*entry), M_DEVBUF, M_NOWAIT);
470 if (!entry) {
471 bus_dmamap_destroy(sc->sc_dmatag, dmamap);
472 return (ENOBUFS);
473 }
474 entry->dmamap = dmamap;
475 SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head, entry, link);
476 }
477
478 sc_if->sk_cdata.sk_tx_prod = 0;
479 sc_if->sk_cdata.sk_tx_cons = 0;
480 sc_if->sk_cdata.sk_tx_cnt = 0;
481
482 MSK_CDTXSYNC(sc_if, 0, MSK_TX_RING_CNT,
483 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
484
485 return (0);
486 }
487
488 int
489 msk_newbuf(struct sk_if_softc *sc_if, int i, struct mbuf *m,
490 bus_dmamap_t dmamap)
491 {
492 struct mbuf *m_new = NULL;
493 struct sk_chain *c;
494 struct msk_rx_desc *r;
495
496 if (m == NULL) {
497 void *buf = NULL;
498
499 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
500 if (m_new == NULL)
501 return (ENOBUFS);
502
503 /* Allocate the jumbo buffer */
504 buf = msk_jalloc(sc_if);
505 if (buf == NULL) {
506 m_freem(m_new);
507 DPRINTFN(1, ("%s jumbo allocation failed -- packet "
508 "dropped!\n", sc_if->sk_ethercom.ec_if.if_xname));
509 return (ENOBUFS);
510 }
511
512 /* Attach the buffer to the mbuf */
513 m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
514 MEXTADD(m_new, buf, SK_JLEN, 0, msk_jfree, sc_if);
515 } else {
516 /*
517 * We're re-using a previously allocated mbuf;
518 * be sure to re-init pointers and lengths to
519 * default values.
520 */
521 m_new = m;
522 m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
523 m_new->m_data = m_new->m_ext.ext_buf;
524 }
525 m_adj(m_new, ETHER_ALIGN);
526
527 c = &sc_if->sk_cdata.sk_rx_chain[i];
528 r = c->sk_le;
529 c->sk_mbuf = m_new;
530 r->sk_addr = htole32(dmamap->dm_segs[0].ds_addr +
531 (((vaddr_t)m_new->m_data
532 - (vaddr_t)sc_if->sk_cdata.sk_jumbo_buf)));
533 r->sk_len = htole16(SK_JLEN);
534 r->sk_ctl = 0;
535 r->sk_opcode = SK_Y2_RXOPC_PACKET | SK_Y2_RXOPC_OWN;
536
537 MSK_CDRXSYNC(sc_if, i, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
538
539 return (0);
540 }
541
542 /*
543 * Memory management for jumbo frames.
544 */
545
546 int
547 msk_alloc_jumbo_mem(struct sk_if_softc *sc_if)
548 {
549 struct sk_softc *sc = sc_if->sk_softc;
550 char *ptr, *kva;
551 bus_dma_segment_t seg;
552 int i, rseg, state, error;
553 struct sk_jpool_entry *entry;
554
555 state = error = 0;
556
557 /* Grab a big chunk o' storage. */
558 if (bus_dmamem_alloc(sc->sc_dmatag, MSK_JMEM, PAGE_SIZE, 0,
559 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
560 aprint_error(": can't alloc rx buffers");
561 return (ENOBUFS);
562 }
563
564 state = 1;
565 if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg, MSK_JMEM, (void **)&kva,
566 BUS_DMA_NOWAIT)) {
567 aprint_error(": can't map dma buffers (%d bytes)", MSK_JMEM);
568 error = ENOBUFS;
569 goto out;
570 }
571
572 state = 2;
573 if (bus_dmamap_create(sc->sc_dmatag, MSK_JMEM, 1, MSK_JMEM, 0,
574 BUS_DMA_NOWAIT, &sc_if->sk_cdata.sk_rx_jumbo_map)) {
575 aprint_error(": can't create dma map");
576 error = ENOBUFS;
577 goto out;
578 }
579
580 state = 3;
581 if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_cdata.sk_rx_jumbo_map,
582 kva, MSK_JMEM, NULL, BUS_DMA_NOWAIT)) {
583 aprint_error(": can't load dma map");
584 error = ENOBUFS;
585 goto out;
586 }
587
588 state = 4;
589 sc_if->sk_cdata.sk_jumbo_buf = (void *)kva;
590 DPRINTFN(1,("msk_jumbo_buf = %p\n", (void *)sc_if->sk_cdata.sk_jumbo_buf));
591
592 LIST_INIT(&sc_if->sk_jfree_listhead);
593 LIST_INIT(&sc_if->sk_jinuse_listhead);
594
595 /*
596 * Now divide it up into 9K pieces and save the addresses
597 * in an array.
598 */
599 ptr = sc_if->sk_cdata.sk_jumbo_buf;
600 for (i = 0; i < MSK_JSLOTS; i++) {
601 sc_if->sk_cdata.sk_jslots[i] = ptr;
602 ptr += SK_JLEN;
603 entry = malloc(sizeof(struct sk_jpool_entry),
604 M_DEVBUF, M_NOWAIT);
605 if (entry == NULL) {
606 sc_if->sk_cdata.sk_jumbo_buf = NULL;
607 aprint_error(": no memory for jumbo buffer queue!");
608 error = ENOBUFS;
609 goto out;
610 }
611 entry->slot = i;
612 LIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
613 entry, jpool_entries);
614 }
615 out:
616 if (error != 0) {
617 switch (state) {
618 case 4:
619 bus_dmamap_unload(sc->sc_dmatag,
620 sc_if->sk_cdata.sk_rx_jumbo_map);
621 case 3:
622 bus_dmamap_destroy(sc->sc_dmatag,
623 sc_if->sk_cdata.sk_rx_jumbo_map);
624 case 2:
625 bus_dmamem_unmap(sc->sc_dmatag, kva, MSK_JMEM);
626 case 1:
627 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
628 break;
629 default:
630 break;
631 }
632 }
633
634 return (error);
635 }
636
637 /*
638 * Allocate a jumbo buffer.
639 */
640 void *
641 msk_jalloc(struct sk_if_softc *sc_if)
642 {
643 struct sk_jpool_entry *entry;
644
645 entry = LIST_FIRST(&sc_if->sk_jfree_listhead);
646
647 if (entry == NULL)
648 return (NULL);
649
650 LIST_REMOVE(entry, jpool_entries);
651 LIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries);
652 return (sc_if->sk_cdata.sk_jslots[entry->slot]);
653 }
654
655 /*
656 * Release a jumbo buffer.
657 */
658 void
659 msk_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
660 {
661 struct sk_jpool_entry *entry;
662 struct sk_if_softc *sc;
663 int i, s;
664
665 /* Extract the softc struct pointer. */
666 sc = (struct sk_if_softc *)arg;
667
668 if (sc == NULL)
669 panic("msk_jfree: can't find softc pointer!");
670
671 /* calculate the slot this buffer belongs to */
672 i = ((vaddr_t)buf
673 - (vaddr_t)sc->sk_cdata.sk_jumbo_buf) / SK_JLEN;
674
675 if ((i < 0) || (i >= MSK_JSLOTS))
676 panic("msk_jfree: asked to free buffer that we don't manage!");
677
678 s = splvm();
679 entry = LIST_FIRST(&sc->sk_jinuse_listhead);
680 if (entry == NULL)
681 panic("msk_jfree: buffer not in use!");
682 entry->slot = i;
683 LIST_REMOVE(entry, jpool_entries);
684 LIST_INSERT_HEAD(&sc->sk_jfree_listhead, entry, jpool_entries);
685
686 if (__predict_true(m != NULL))
687 pool_cache_put(mb_cache, m);
688 splx(s);
689 }
690
691 int
692 msk_ioctl(struct ifnet *ifp, u_long command, void *data)
693 {
694 struct sk_if_softc *sc_if = ifp->if_softc;
695 struct ifreq *ifr = (struct ifreq *) data;
696 int s, error = 0;
697
698 s = splnet();
699
700 switch(command) {
701 case SIOCSIFMTU:
702 if (ifr->ifr_mtu < ETHERMIN)
703 return EINVAL;
704 else if (sc_if->sk_softc->sk_type != SK_YUKON_FE) {
705 if (ifr->ifr_mtu > SK_JUMBO_MTU)
706 error = EINVAL;
707 } else if (ifr->ifr_mtu > ETHERMTU)
708 error = EINVAL;
709 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
710 error = 0;
711 break;
712 default:
713 DPRINTFN(2, ("msk_ioctl ETHER\n"));
714 error = ether_ioctl(ifp, command, data);
715
716 if (error == ENETRESET) {
717 /*
718 * Multicast list has changed; set the hardware
719 * filter accordingly.
720 */
721 if (ifp->if_flags & IFF_RUNNING)
722 msk_setmulti(sc_if);
723 error = 0;
724 }
725 break;
726 }
727
728 splx(s);
729 return (error);
730 }
731
732 void
733 msk_update_int_mod(struct sk_softc *sc)
734 {
735 u_int32_t imtimer_ticks;
736
737 /*
738 * Configure interrupt moderation. The moderation timer
739 * defers interrupts specified in the interrupt moderation
740 * timer mask based on the timeout specified in the interrupt
741 * moderation timer init register. Each bit in the timer
742 * register represents one tick, so to specify a timeout in
743 * microseconds, we have to multiply by the correct number of
744 * ticks-per-microsecond.
745 */
746 switch (sc->sk_type) {
747 case SK_YUKON_EC:
748 case SK_YUKON_EC_U:
749 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
750 break;
751 case SK_YUKON_FE:
752 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
753 break;
754 case SK_YUKON_XL:
755 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
756 break;
757 default:
758 imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
759 }
760 aprint_verbose("%s: interrupt moderation is %d us\n",
761 sc->sk_dev.dv_xname, sc->sk_int_mod);
762 sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(sc->sk_int_mod));
763 sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
764 SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
765 sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
766 sc->sk_int_mod_pending = 0;
767 }
768
769 static int
770 msk_lookup(const struct pci_attach_args *pa)
771 {
772 const struct msk_product *pmsk;
773
774 for ( pmsk = &msk_products[0]; pmsk->msk_vendor != 0; pmsk++) {
775 if (PCI_VENDOR(pa->pa_id) == pmsk->msk_vendor &&
776 PCI_PRODUCT(pa->pa_id) == pmsk->msk_product)
777 return 1;
778 }
779 return 0;
780 }
781
782 /*
783 * Probe for a SysKonnect GEnesis chip. Check the PCI vendor and device
784 * IDs against our list and return a device name if we find a match.
785 */
786 int
787 mskc_probe(struct device *parent, struct cfdata *match,
788 void *aux)
789 {
790 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
791
792 return msk_lookup(pa);
793 }
794
795 /*
796 * Force the GEnesis into reset, then bring it out of reset.
797 */
798 void msk_reset(struct sk_softc *sc)
799 {
800 u_int32_t imtimer_ticks, reg1;
801 int reg;
802
803 DPRINTFN(2, ("msk_reset\n"));
804
805 CSR_WRITE_1(sc, SK_CSR, SK_CSR_SW_RESET);
806 CSR_WRITE_1(sc, SK_CSR, SK_CSR_MASTER_RESET);
807
808 DELAY(1000);
809 CSR_WRITE_1(sc, SK_CSR, SK_CSR_SW_UNRESET);
810 DELAY(2);
811 CSR_WRITE_1(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
812 sk_win_write_1(sc, SK_TESTCTL1, 2);
813
814 reg1 = sk_win_read_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG1));
815 if (sc->sk_type == SK_YUKON_XL && sc->sk_rev > SK_YUKON_XL_REV_A1)
816 reg1 |= (SK_Y2_REG1_PHY1_COMA | SK_Y2_REG1_PHY2_COMA);
817 else
818 reg1 &= ~(SK_Y2_REG1_PHY1_COMA | SK_Y2_REG1_PHY2_COMA);
819 sk_win_write_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG1), reg1);
820
821 if (sc->sk_type == SK_YUKON_XL && sc->sk_rev > SK_YUKON_XL_REV_A1)
822 sk_win_write_1(sc, SK_Y2_CLKGATE,
823 SK_Y2_CLKGATE_LINK1_GATE_DIS |
824 SK_Y2_CLKGATE_LINK2_GATE_DIS |
825 SK_Y2_CLKGATE_LINK1_CORE_DIS |
826 SK_Y2_CLKGATE_LINK2_CORE_DIS |
827 SK_Y2_CLKGATE_LINK1_PCI_DIS | SK_Y2_CLKGATE_LINK2_PCI_DIS);
828 else
829 sk_win_write_1(sc, SK_Y2_CLKGATE, 0);
830
831 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
832 CSR_WRITE_2(sc, SK_LINK_CTRL + SK_WIN_LEN, SK_LINK_RESET_SET);
833 DELAY(1000);
834 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
835 CSR_WRITE_2(sc, SK_LINK_CTRL + SK_WIN_LEN, SK_LINK_RESET_CLEAR);
836
837 sk_win_write_1(sc, SK_TESTCTL1, 1);
838
839 DPRINTFN(2, ("msk_reset: sk_csr=%x\n", CSR_READ_1(sc, SK_CSR)));
840 DPRINTFN(2, ("msk_reset: sk_link_ctrl=%x\n",
841 CSR_READ_2(sc, SK_LINK_CTRL)));
842
843 /* Disable ASF */
844 CSR_WRITE_1(sc, SK_Y2_ASF_CSR, SK_Y2_ASF_RESET);
845 CSR_WRITE_2(sc, SK_CSR, SK_CSR_ASF_OFF);
846
847 /* Clear I2C IRQ noise */
848 CSR_WRITE_4(sc, SK_I2CHWIRQ, 1);
849
850 /* Disable hardware timer */
851 CSR_WRITE_1(sc, SK_TIMERCTL, SK_IMCTL_STOP);
852 CSR_WRITE_1(sc, SK_TIMERCTL, SK_IMCTL_IRQ_CLEAR);
853
854 /* Disable descriptor polling */
855 CSR_WRITE_4(sc, SK_DPT_TIMER_CTRL, SK_DPT_TCTL_STOP);
856
857 /* Disable time stamps */
858 CSR_WRITE_1(sc, SK_TSTAMP_CTL, SK_TSTAMP_STOP);
859 CSR_WRITE_1(sc, SK_TSTAMP_CTL, SK_TSTAMP_IRQ_CLEAR);
860
861 /* Enable RAM interface */
862 sk_win_write_1(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
863 for (reg = SK_TO0;reg <= SK_TO11; reg++)
864 sk_win_write_1(sc, reg, 36);
865 sk_win_write_1(sc, SK_RAMCTL + (SK_WIN_LEN / 2), SK_RAMCTL_UNRESET);
866 for (reg = SK_TO0;reg <= SK_TO11; reg++)
867 sk_win_write_1(sc, reg + (SK_WIN_LEN / 2), 36);
868
869 /*
870 * Configure interrupt moderation. The moderation timer
871 * defers interrupts specified in the interrupt moderation
872 * timer mask based on the timeout specified in the interrupt
873 * moderation timer init register. Each bit in the timer
874 * register represents one tick, so to specify a timeout in
875 * microseconds, we have to multiply by the correct number of
876 * ticks-per-microsecond.
877 */
878 switch (sc->sk_type) {
879 case SK_YUKON_EC:
880 case SK_YUKON_EC_U:
881 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
882 break;
883 case SK_YUKON_FE:
884 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
885 break;
886 case SK_YUKON_XL:
887 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
888 break;
889 default:
890 imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
891 }
892
893 /* Reset status ring. */
894 bzero((char *)sc->sk_status_ring,
895 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
896 bus_dmamap_sync(sc->sc_dmatag, sc->sk_status_map, 0,
897 sc->sk_status_map->dm_mapsize, BUS_DMASYNC_PREREAD);
898 sc->sk_status_idx = 0;
899 sc->sk_status_own_idx = 0;
900
901 sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_RESET);
902 sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_UNRESET);
903
904 sk_win_write_2(sc, SK_STAT_BMU_LIDX, MSK_STATUS_RING_CNT - 1);
905 sk_win_write_4(sc, SK_STAT_BMU_ADDRLO,
906 sc->sk_status_map->dm_segs[0].ds_addr);
907 sk_win_write_4(sc, SK_STAT_BMU_ADDRHI,
908 (u_int64_t)sc->sk_status_map->dm_segs[0].ds_addr >> 32);
909 if ((sc->sk_workaround & SK_STAT_BMU_FIFOIWM) != 0) {
910 sk_win_write_2(sc, SK_STAT_BMU_TX_THRESH, SK_STAT_BMU_TXTHIDX_MSK);
911 sk_win_write_1(sc, SK_STAT_BMU_FIFOWM, 0x21);
912 sk_win_write_1(sc, SK_STAT_BMU_FIFOIWM, 0x07);
913 } else {
914 sk_win_write_2(sc, SK_STAT_BMU_TX_THRESH, 0x000a);
915 sk_win_write_1(sc, SK_STAT_BMU_FIFOWM, 0x10);
916 sk_win_write_1(sc, SK_STAT_BMU_FIFOIWM,
917 ((sc->sk_workaround & SK_WA_4109) != 0) ? 0x10 : 0x04);
918 sk_win_write_4(sc, SK_Y2_ISR_ITIMERINIT, 0x0190); /* 3.2us on Yukon-EC */
919 }
920
921 #if 0
922 sk_win_write_4(sc, SK_Y2_LEV_ITIMERINIT, SK_IM_USECS(100));
923 #endif
924 sk_win_write_4(sc, SK_Y2_TX_ITIMERINIT, SK_IM_USECS(1000));
925
926 sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_ON);
927
928 sk_win_write_1(sc, SK_Y2_LEV_ITIMERCTL, SK_IMCTL_START);
929 sk_win_write_1(sc, SK_Y2_TX_ITIMERCTL, SK_IMCTL_START);
930 sk_win_write_1(sc, SK_Y2_ISR_ITIMERCTL, SK_IMCTL_START);
931
932 msk_update_int_mod(sc);
933 }
934
935 int
936 msk_probe(struct device *parent, struct cfdata *match,
937 void *aux)
938 {
939 struct skc_attach_args *sa = aux;
940
941 if (sa->skc_port != SK_PORT_A && sa->skc_port != SK_PORT_B)
942 return (0);
943
944 switch (sa->skc_type) {
945 case SK_YUKON_XL:
946 case SK_YUKON_EC_U:
947 case SK_YUKON_EC:
948 case SK_YUKON_FE:
949 return (1);
950 }
951
952 return (0);
953 }
954
955 /*
956 * Each XMAC chip is attached as a separate logical IP interface.
957 * Single port cards will have only one logical interface of course.
958 */
959 void
960 msk_attach(struct device *parent, struct device *self, void *aux)
961 {
962 struct sk_if_softc *sc_if = (struct sk_if_softc *) self;
963 struct sk_softc *sc = (struct sk_softc *)parent;
964 struct skc_attach_args *sa = aux;
965 struct ifnet *ifp;
966 void *kva;
967 bus_dma_segment_t seg;
968 int i, rseg;
969 u_int32_t chunk, val;
970
971 sc_if->sk_port = sa->skc_port;
972 sc_if->sk_softc = sc;
973 sc->sk_if[sa->skc_port] = sc_if;
974
975 DPRINTFN(2, ("begin msk_attach: port=%d\n", sc_if->sk_port));
976
977 /*
978 * Get station address for this interface. Note that
979 * dual port cards actually come with three station
980 * addresses: one for each port, plus an extra. The
981 * extra one is used by the SysKonnect driver software
982 * as a 'virtual' station address for when both ports
983 * are operating in failover mode. Currently we don't
984 * use this extra address.
985 */
986 for (i = 0; i < ETHER_ADDR_LEN; i++)
987 sc_if->sk_enaddr[i] =
988 sk_win_read_1(sc, SK_MAC0_0 + (sa->skc_port * 8) + i);
989
990 aprint_normal(": Ethernet address %s\n",
991 ether_sprintf(sc_if->sk_enaddr));
992
993 /*
994 * Set up RAM buffer addresses. The NIC will have a certain
995 * amount of SRAM on it, somewhere between 512K and 2MB. We
996 * need to divide this up a) between the transmitter and
997 * receiver and b) between the two XMACs, if this is a
998 * dual port NIC. Our algorithm is to divide up the memory
999 * evenly so that everyone gets a fair share.
1000 *
1001 * Just to be contrary, Yukon2 appears to have separate memory
1002 * for each MAC.
1003 */
1004 chunk = sc->sk_ramsize - (sc->sk_ramsize + 2) / 3;
1005 val = sc->sk_rboff / sizeof(u_int64_t);
1006 sc_if->sk_rx_ramstart = val;
1007 val += (chunk / sizeof(u_int64_t));
1008 sc_if->sk_rx_ramend = val - 1;
1009 chunk = sc->sk_ramsize - chunk;
1010 sc_if->sk_tx_ramstart = val;
1011 val += (chunk / sizeof(u_int64_t));
1012 sc_if->sk_tx_ramend = val - 1;
1013
1014 DPRINTFN(2, ("msk_attach: rx_ramstart=%#x rx_ramend=%#x\n"
1015 " tx_ramstart=%#x tx_ramend=%#x\n",
1016 sc_if->sk_rx_ramstart, sc_if->sk_rx_ramend,
1017 sc_if->sk_tx_ramstart, sc_if->sk_tx_ramend));
1018
1019 /* Allocate the descriptor queues. */
1020 if (bus_dmamem_alloc(sc->sc_dmatag, sizeof(struct msk_ring_data),
1021 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1022 aprint_error(": can't alloc rx buffers\n");
1023 goto fail;
1024 }
1025 if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
1026 sizeof(struct msk_ring_data), &kva, BUS_DMA_NOWAIT)) {
1027 aprint_error(": can't map dma buffers (%zu bytes)\n",
1028 sizeof(struct msk_ring_data));
1029 goto fail_1;
1030 }
1031 if (bus_dmamap_create(sc->sc_dmatag, sizeof(struct msk_ring_data), 1,
1032 sizeof(struct msk_ring_data), 0, BUS_DMA_NOWAIT,
1033 &sc_if->sk_ring_map)) {
1034 aprint_error(": can't create dma map\n");
1035 goto fail_2;
1036 }
1037 if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_ring_map, kva,
1038 sizeof(struct msk_ring_data), NULL, BUS_DMA_NOWAIT)) {
1039 aprint_error(": can't load dma map\n");
1040 goto fail_3;
1041 }
1042 sc_if->sk_rdata = (struct msk_ring_data *)kva;
1043 bzero(sc_if->sk_rdata, sizeof(struct msk_ring_data));
1044
1045 ifp = &sc_if->sk_ethercom.ec_if;
1046 /* Try to allocate memory for jumbo buffers. */
1047 if (msk_alloc_jumbo_mem(sc_if)) {
1048 aprint_error(": jumbo buffer allocation failed\n");
1049 goto fail_3;
1050 }
1051 sc_if->sk_ethercom.ec_capabilities = ETHERCAP_VLAN_MTU
1052 | ETHERCAP_JUMBO_MTU;
1053
1054 ifp->if_softc = sc_if;
1055 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1056 ifp->if_ioctl = msk_ioctl;
1057 ifp->if_start = msk_start;
1058 ifp->if_stop = msk_stop;
1059 ifp->if_init = msk_init;
1060 ifp->if_watchdog = msk_watchdog;
1061 ifp->if_baudrate = 1000000000;
1062 IFQ_SET_MAXLEN(&ifp->if_snd, MSK_TX_RING_CNT - 1);
1063 IFQ_SET_READY(&ifp->if_snd);
1064 strcpy(ifp->if_xname, sc_if->sk_dev.dv_xname);
1065
1066 /*
1067 * Do miibus setup.
1068 */
1069 msk_init_yukon(sc_if);
1070
1071 DPRINTFN(2, ("msk_attach: 1\n"));
1072
1073 sc_if->sk_mii.mii_ifp = ifp;
1074 sc_if->sk_mii.mii_readreg = msk_miibus_readreg;
1075 sc_if->sk_mii.mii_writereg = msk_miibus_writereg;
1076 sc_if->sk_mii.mii_statchg = msk_miibus_statchg;
1077
1078 sc_if->sk_ethercom.ec_mii = &sc_if->sk_mii;
1079 ifmedia_init(&sc_if->sk_mii.mii_media, 0,
1080 ether_mediachange, ether_mediastatus);
1081 mii_attach(self, &sc_if->sk_mii, 0xffffffff, MII_PHY_ANY,
1082 MII_OFFSET_ANY, MIIF_DOPAUSE|MIIF_FORCEANEG);
1083 if (LIST_FIRST(&sc_if->sk_mii.mii_phys) == NULL) {
1084 aprint_error("%s: no PHY found!\n", sc_if->sk_dev.dv_xname);
1085 ifmedia_add(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL,
1086 0, NULL);
1087 ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL);
1088 } else
1089 ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_AUTO);
1090
1091 callout_init(&sc_if->sk_tick_ch, 0);
1092 callout_setfunc(&sc_if->sk_tick_ch, msk_tick, sc_if);
1093 callout_schedule(&sc_if->sk_tick_ch, hz);
1094
1095 /*
1096 * Call MI attach routines.
1097 */
1098 if_attach(ifp);
1099 ether_ifattach(ifp, sc_if->sk_enaddr);
1100
1101 shutdownhook_establish(mskc_shutdown, sc);
1102
1103 #if NRND > 0
1104 rnd_attach_source(&sc->rnd_source, sc->sk_dev.dv_xname,
1105 RND_TYPE_NET, 0);
1106 #endif
1107
1108 DPRINTFN(2, ("msk_attach: end\n"));
1109 return;
1110
1111 fail_3:
1112 bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
1113 fail_2:
1114 bus_dmamem_unmap(sc->sc_dmatag, kva, sizeof(struct msk_ring_data));
1115 fail_1:
1116 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1117 fail:
1118 sc->sk_if[sa->skc_port] = NULL;
1119 }
1120
1121 int
1122 mskcprint(void *aux, const char *pnp)
1123 {
1124 struct skc_attach_args *sa = aux;
1125
1126 if (pnp)
1127 aprint_normal("sk port %c at %s",
1128 (sa->skc_port == SK_PORT_A) ? 'A' : 'B', pnp);
1129 else
1130 aprint_normal(" port %c", (sa->skc_port == SK_PORT_A) ? 'A' : 'B');
1131 return (UNCONF);
1132 }
1133
1134 /*
1135 * Attach the interface. Allocate softc structures, do ifmedia
1136 * setup and ethernet/BPF attach.
1137 */
1138 void
1139 mskc_attach(struct device *parent, struct device *self, void *aux)
1140 {
1141 struct sk_softc *sc = (struct sk_softc *)self;
1142 struct pci_attach_args *pa = aux;
1143 struct skc_attach_args skca;
1144 pci_chipset_tag_t pc = pa->pa_pc;
1145 pcireg_t command, memtype;
1146 pci_intr_handle_t ih;
1147 const char *intrstr = NULL;
1148 bus_size_t size;
1149 int rc, sk_nodenum;
1150 u_int8_t hw, skrs;
1151 const char *revstr = NULL;
1152 const struct sysctlnode *node;
1153 void *kva;
1154 bus_dma_segment_t seg;
1155 int rseg;
1156
1157 DPRINTFN(2, ("begin mskc_attach\n"));
1158
1159 /*
1160 * Handle power management nonsense.
1161 */
1162 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_CAPID) & 0x000000FF;
1163
1164 if (command == 0x01) {
1165 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_PWRMGMTCTRL);
1166 if (command & SK_PSTATE_MASK) {
1167 u_int32_t iobase, membase, irq;
1168
1169 /* Save important PCI config data. */
1170 iobase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOIO);
1171 membase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOMEM);
1172 irq = pci_conf_read(pc, pa->pa_tag, SK_PCI_INTLINE);
1173
1174 /* Reset the power state. */
1175 aprint_normal("%s chip is in D%d power mode "
1176 "-- setting to D0\n", sc->sk_dev.dv_xname,
1177 command & SK_PSTATE_MASK);
1178 command &= 0xFFFFFFFC;
1179 pci_conf_write(pc, pa->pa_tag,
1180 SK_PCI_PWRMGMTCTRL, command);
1181
1182 /* Restore PCI config data. */
1183 pci_conf_write(pc, pa->pa_tag, SK_PCI_LOIO, iobase);
1184 pci_conf_write(pc, pa->pa_tag, SK_PCI_LOMEM, membase);
1185 pci_conf_write(pc, pa->pa_tag, SK_PCI_INTLINE, irq);
1186 }
1187 }
1188
1189 /*
1190 * Map control/status registers.
1191 */
1192
1193 memtype = pci_mapreg_type(pc, pa->pa_tag, SK_PCI_LOMEM);
1194 switch (memtype) {
1195 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1196 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1197 if (pci_mapreg_map(pa, SK_PCI_LOMEM,
1198 memtype, 0, &sc->sk_btag, &sc->sk_bhandle,
1199 NULL, &size) == 0)
1200 break;
1201 default:
1202 aprint_error(": can't map mem space\n");
1203 return;
1204 }
1205
1206 sc->sc_dmatag = pa->pa_dmat;
1207
1208 sc->sk_type = sk_win_read_1(sc, SK_CHIPVER);
1209 sc->sk_rev = (sk_win_read_1(sc, SK_CONFIG) >> 4);
1210
1211 /* bail out here if chip is not recognized */
1212 if (!(SK_IS_YUKON2(sc))) {
1213 aprint_error(": unknown chip type: %d\n", sc->sk_type);
1214 goto fail_1;
1215 }
1216 DPRINTFN(2, ("mskc_attach: allocate interrupt\n"));
1217
1218 /* Allocate interrupt */
1219 if (pci_intr_map(pa, &ih)) {
1220 aprint_error(": couldn't map interrupt\n");
1221 goto fail_1;
1222 }
1223
1224 intrstr = pci_intr_string(pc, ih);
1225 sc->sk_intrhand = pci_intr_establish(pc, ih, IPL_NET, msk_intr, sc);
1226 if (sc->sk_intrhand == NULL) {
1227 aprint_error(": couldn't establish interrupt");
1228 if (intrstr != NULL)
1229 aprint_error(" at %s", intrstr);
1230 aprint_error("\n");
1231 goto fail_1;
1232 }
1233
1234 if (bus_dmamem_alloc(sc->sc_dmatag,
1235 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
1236 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1237 aprint_error(": can't alloc status buffers\n");
1238 goto fail_2;
1239 }
1240
1241 if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
1242 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
1243 &kva, BUS_DMA_NOWAIT)) {
1244 aprint_error(": can't map dma buffers (%zu bytes)\n",
1245 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
1246 goto fail_3;
1247 }
1248 if (bus_dmamap_create(sc->sc_dmatag,
1249 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc), 1,
1250 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc), 0,
1251 BUS_DMA_NOWAIT, &sc->sk_status_map)) {
1252 aprint_error(": can't create dma map\n");
1253 goto fail_4;
1254 }
1255 if (bus_dmamap_load(sc->sc_dmatag, sc->sk_status_map, kva,
1256 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
1257 NULL, BUS_DMA_NOWAIT)) {
1258 aprint_error(": can't load dma map\n");
1259 goto fail_5;
1260 }
1261 sc->sk_status_ring = (struct msk_status_desc *)kva;
1262
1263 /* Reset the adapter. */
1264 msk_reset(sc);
1265
1266 skrs = sk_win_read_1(sc, SK_EPROM0);
1267 if (skrs == 0x00)
1268 sc->sk_ramsize = 0x20000;
1269 else
1270 sc->sk_ramsize = skrs * (1<<12);
1271 sc->sk_rboff = SK_RBOFF_0;
1272
1273 DPRINTFN(2, ("mskc_attach: ramsize=%d (%dk), rboff=%d\n",
1274 sc->sk_ramsize, sc->sk_ramsize / 1024,
1275 sc->sk_rboff));
1276
1277 switch (sc->sk_type) {
1278 case SK_YUKON_XL:
1279 sc->sk_name = "Yukon-2 XL";
1280 break;
1281 case SK_YUKON_EC_U:
1282 sc->sk_name = "Yukon-2 EC Ultra";
1283 break;
1284 case SK_YUKON_EC:
1285 sc->sk_name = "Yukon-2 EC";
1286 break;
1287 case SK_YUKON_FE:
1288 sc->sk_name = "Yukon-2 FE";
1289 break;
1290 default:
1291 sc->sk_name = "Yukon (Unknown)";
1292 }
1293
1294 if (sc->sk_type == SK_YUKON_XL) {
1295 switch (sc->sk_rev) {
1296 case SK_YUKON_XL_REV_A0:
1297 sc->sk_workaround = 0;
1298 revstr = "A0";
1299 break;
1300 case SK_YUKON_XL_REV_A1:
1301 sc->sk_workaround = SK_WA_4109;
1302 revstr = "A1";
1303 break;
1304 case SK_YUKON_XL_REV_A2:
1305 sc->sk_workaround = SK_WA_4109;
1306 revstr = "A2";
1307 break;
1308 case SK_YUKON_XL_REV_A3:
1309 sc->sk_workaround = SK_WA_4109;
1310 revstr = "A3";
1311 break;
1312 default:
1313 sc->sk_workaround = 0;
1314 break;
1315 }
1316 }
1317
1318 if (sc->sk_type == SK_YUKON_EC) {
1319 switch (sc->sk_rev) {
1320 case SK_YUKON_EC_REV_A1:
1321 sc->sk_workaround = SK_WA_43_418 | SK_WA_4109;
1322 revstr = "A1";
1323 break;
1324 case SK_YUKON_EC_REV_A2:
1325 sc->sk_workaround = SK_WA_4109;
1326 revstr = "A2";
1327 break;
1328 case SK_YUKON_EC_REV_A3:
1329 sc->sk_workaround = SK_WA_4109;
1330 revstr = "A3";
1331 break;
1332 default:
1333 sc->sk_workaround = 0;
1334 break;
1335 }
1336 }
1337
1338 if (sc->sk_type == SK_YUKON_FE) {
1339 sc->sk_workaround = SK_WA_4109;
1340 switch (sc->sk_rev) {
1341 case SK_YUKON_FE_REV_A1:
1342 revstr = "A1";
1343 break;
1344 case SK_YUKON_FE_REV_A2:
1345 revstr = "A2";
1346 break;
1347 default:
1348 sc->sk_workaround = 0;
1349 break;
1350 }
1351 }
1352
1353 if (sc->sk_type == SK_YUKON_EC_U) {
1354 sc->sk_workaround = SK_WA_4109;
1355 switch (sc->sk_rev) {
1356 case SK_YUKON_EC_U_REV_A0:
1357 revstr = "A0";
1358 break;
1359 case SK_YUKON_EC_U_REV_A1:
1360 revstr = "A1";
1361 break;
1362 case SK_YUKON_EC_U_REV_B0:
1363 revstr = "B0";
1364 break;
1365 default:
1366 sc->sk_workaround = 0;
1367 break;
1368 }
1369 }
1370
1371 /* Announce the product name. */
1372 aprint_normal(", %s", sc->sk_name);
1373 if (revstr != NULL)
1374 aprint_normal(" rev. %s", revstr);
1375 aprint_normal(" (0x%x): %s\n", sc->sk_rev, intrstr);
1376
1377 sc->sk_macs = 1;
1378
1379 hw = sk_win_read_1(sc, SK_Y2_HWRES);
1380 if ((hw & SK_Y2_HWRES_LINK_MASK) == SK_Y2_HWRES_LINK_DUAL) {
1381 if ((sk_win_read_1(sc, SK_Y2_CLKGATE) &
1382 SK_Y2_CLKGATE_LINK2_INACTIVE) == 0)
1383 sc->sk_macs++;
1384 }
1385
1386 skca.skc_port = SK_PORT_A;
1387 skca.skc_type = sc->sk_type;
1388 skca.skc_rev = sc->sk_rev;
1389 (void)config_found(&sc->sk_dev, &skca, mskcprint);
1390
1391 if (sc->sk_macs > 1) {
1392 skca.skc_port = SK_PORT_B;
1393 skca.skc_type = sc->sk_type;
1394 skca.skc_rev = sc->sk_rev;
1395 (void)config_found(&sc->sk_dev, &skca, mskcprint);
1396 }
1397
1398 /* Turn on the 'driver is loaded' LED. */
1399 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
1400
1401 /* skc sysctl setup */
1402
1403 sc->sk_int_mod = SK_IM_DEFAULT;
1404 sc->sk_int_mod_pending = 0;
1405
1406 if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1407 0, CTLTYPE_NODE, sc->sk_dev.dv_xname,
1408 SYSCTL_DESCR("mskc per-controller controls"),
1409 NULL, 0, NULL, 0, CTL_HW, msk_root_num, CTL_CREATE,
1410 CTL_EOL)) != 0) {
1411 aprint_normal("%s: couldn't create sysctl node\n",
1412 sc->sk_dev.dv_xname);
1413 goto fail_6;
1414 }
1415
1416 sk_nodenum = node->sysctl_num;
1417
1418 /* interrupt moderation time in usecs */
1419 if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1420 CTLFLAG_READWRITE,
1421 CTLTYPE_INT, "int_mod",
1422 SYSCTL_DESCR("msk interrupt moderation timer"),
1423 msk_sysctl_handler, 0, sc,
1424 0, CTL_HW, msk_root_num, sk_nodenum, CTL_CREATE,
1425 CTL_EOL)) != 0) {
1426 aprint_normal("%s: couldn't create int_mod sysctl node\n",
1427 sc->sk_dev.dv_xname);
1428 goto fail_6;
1429 }
1430
1431 return;
1432
1433 fail_6:
1434 bus_dmamap_unload(sc->sc_dmatag, sc->sk_status_map);
1435 fail_5:
1436 bus_dmamap_destroy(sc->sc_dmatag, sc->sk_status_map);
1437 fail_4:
1438 bus_dmamem_unmap(sc->sc_dmatag, kva,
1439 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
1440 fail_3:
1441 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1442 fail_2:
1443 pci_intr_disestablish(pc, sc->sk_intrhand);
1444 fail_1:
1445 bus_space_unmap(sc->sk_btag, sc->sk_bhandle, size);
1446 }
1447
1448 int
1449 msk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, u_int32_t *txidx)
1450 {
1451 struct sk_softc *sc = sc_if->sk_softc;
1452 struct msk_tx_desc *f = NULL;
1453 u_int32_t frag, cur;
1454 int i;
1455 struct sk_txmap_entry *entry;
1456 bus_dmamap_t txmap;
1457
1458 DPRINTFN(2, ("msk_encap\n"));
1459
1460 entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head);
1461 if (entry == NULL) {
1462 DPRINTFN(2, ("msk_encap: no txmap available\n"));
1463 return (ENOBUFS);
1464 }
1465 txmap = entry->dmamap;
1466
1467 cur = frag = *txidx;
1468
1469 #ifdef MSK_DEBUG
1470 if (mskdebug >= 2)
1471 msk_dump_mbuf(m_head);
1472 #endif
1473
1474 /*
1475 * Start packing the mbufs in this chain into
1476 * the fragment pointers. Stop when we run out
1477 * of fragments or hit the end of the mbuf chain.
1478 */
1479 if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
1480 BUS_DMA_NOWAIT)) {
1481 DPRINTFN(2, ("msk_encap: dmamap failed\n"));
1482 return (ENOBUFS);
1483 }
1484
1485 if (txmap->dm_nsegs > (MSK_TX_RING_CNT - sc_if->sk_cdata.sk_tx_cnt - 2)) {
1486 DPRINTFN(2, ("msk_encap: too few descriptors free\n"));
1487 bus_dmamap_unload(sc->sc_dmatag, txmap);
1488 return (ENOBUFS);
1489 }
1490
1491 DPRINTFN(2, ("msk_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1492
1493 /* Sync the DMA map. */
1494 bus_dmamap_sync(sc->sc_dmatag, txmap, 0, txmap->dm_mapsize,
1495 BUS_DMASYNC_PREWRITE);
1496
1497 for (i = 0; i < txmap->dm_nsegs; i++) {
1498 f = &sc_if->sk_rdata->sk_tx_ring[frag];
1499 f->sk_addr = htole32(txmap->dm_segs[i].ds_addr);
1500 f->sk_len = htole16(txmap->dm_segs[i].ds_len);
1501 f->sk_ctl = 0;
1502 if (i == 0)
1503 f->sk_opcode = SK_Y2_TXOPC_PACKET;
1504 else
1505 f->sk_opcode = SK_Y2_TXOPC_BUFFER | SK_Y2_TXOPC_OWN;
1506 cur = frag;
1507 SK_INC(frag, MSK_TX_RING_CNT);
1508 }
1509
1510 sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
1511 SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
1512
1513 sc_if->sk_cdata.sk_tx_map[cur] = entry;
1514 sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |= SK_Y2_TXCTL_LASTFRAG;
1515
1516 /* Sync descriptors before handing to chip */
1517 MSK_CDTXSYNC(sc_if, *txidx, txmap->dm_nsegs,
1518 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1519
1520 sc_if->sk_rdata->sk_tx_ring[*txidx].sk_opcode |= SK_Y2_TXOPC_OWN;
1521
1522 /* Sync first descriptor to hand it off */
1523 MSK_CDTXSYNC(sc_if, *txidx, 1,
1524 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1525
1526 sc_if->sk_cdata.sk_tx_cnt += txmap->dm_nsegs;
1527
1528 #ifdef MSK_DEBUG
1529 if (mskdebug >= 2) {
1530 struct msk_tx_desc *le;
1531 u_int32_t idx;
1532 for (idx = *txidx; idx != frag; SK_INC(idx, MSK_TX_RING_CNT)) {
1533 le = &sc_if->sk_rdata->sk_tx_ring[idx];
1534 msk_dump_txdesc(le, idx);
1535 }
1536 }
1537 #endif
1538
1539 *txidx = frag;
1540
1541 DPRINTFN(2, ("msk_encap: completed successfully\n"));
1542
1543 return (0);
1544 }
1545
1546 void
1547 msk_start(struct ifnet *ifp)
1548 {
1549 struct sk_if_softc *sc_if = ifp->if_softc;
1550 struct mbuf *m_head = NULL;
1551 u_int32_t idx = sc_if->sk_cdata.sk_tx_prod;
1552 int pkts = 0;
1553
1554 DPRINTFN(2, ("msk_start\n"));
1555
1556 while (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
1557 IFQ_POLL(&ifp->if_snd, m_head);
1558 if (m_head == NULL)
1559 break;
1560
1561 /*
1562 * Pack the data into the transmit ring. If we
1563 * don't have room, set the OACTIVE flag and wait
1564 * for the NIC to drain the ring.
1565 */
1566 if (msk_encap(sc_if, m_head, &idx)) {
1567 ifp->if_flags |= IFF_OACTIVE;
1568 break;
1569 }
1570
1571 /* now we are committed to transmit the packet */
1572 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1573 pkts++;
1574
1575 /*
1576 * If there's a BPF listener, bounce a copy of this frame
1577 * to him.
1578 */
1579 #if NBPFILTER > 0
1580 if (ifp->if_bpf)
1581 bpf_mtap(ifp->if_bpf, m_head);
1582 #endif
1583 }
1584 if (pkts == 0)
1585 return;
1586
1587 /* Transmit */
1588 if (idx != sc_if->sk_cdata.sk_tx_prod) {
1589 sc_if->sk_cdata.sk_tx_prod = idx;
1590 SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_PREF_PUTIDX, idx);
1591
1592 /* Set a timeout in case the chip goes out to lunch. */
1593 ifp->if_timer = 5;
1594 }
1595 }
1596
1597 void
1598 msk_watchdog(struct ifnet *ifp)
1599 {
1600 struct sk_if_softc *sc_if = ifp->if_softc;
1601 u_int32_t reg;
1602 int idx;
1603
1604 /*
1605 * Reclaim first as there is a possibility of losing Tx completion
1606 * interrupts.
1607 */
1608 if (sc_if->sk_port == SK_PORT_A)
1609 reg = SK_STAT_BMU_TXA1_RIDX;
1610 else
1611 reg = SK_STAT_BMU_TXA2_RIDX;
1612
1613 idx = sk_win_read_2(sc_if->sk_softc, reg);
1614 if (sc_if->sk_cdata.sk_tx_cons != idx) {
1615 msk_txeof(sc_if, idx);
1616 if (sc_if->sk_cdata.sk_tx_cnt != 0) {
1617 aprint_error("%s: watchdog timeout\n", sc_if->sk_dev.dv_xname);
1618
1619 ifp->if_oerrors++;
1620
1621 /* XXX Resets both ports; we shouldn't do that. */
1622 msk_reset(sc_if->sk_softc);
1623 msk_init(ifp);
1624 }
1625 }
1626 }
1627
1628 void
1629 mskc_shutdown(void *v)
1630 {
1631 struct sk_softc *sc = v;
1632
1633 DPRINTFN(2, ("msk_shutdown\n"));
1634
1635 /* Turn off the 'driver is loaded' LED. */
1636 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
1637
1638 msk_reset(sc);
1639 }
1640
1641 __inline int
1642 msk_rxvalid(struct sk_softc *sc, u_int32_t stat, u_int32_t len)
1643 {
1644 if ((stat & (YU_RXSTAT_CRCERR | YU_RXSTAT_LONGERR |
1645 YU_RXSTAT_MIIERR | YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC |
1646 YU_RXSTAT_JABBER)) != 0 ||
1647 (stat & YU_RXSTAT_RXOK) != YU_RXSTAT_RXOK ||
1648 YU_RXSTAT_BYTES(stat) != len)
1649 return (0);
1650
1651 return (1);
1652 }
1653
1654 void
1655 msk_rxeof(struct sk_if_softc *sc_if, u_int16_t len, u_int32_t rxstat)
1656 {
1657 struct sk_softc *sc = sc_if->sk_softc;
1658 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1659 struct mbuf *m;
1660 struct sk_chain *cur_rx;
1661 int cur, total_len = len;
1662 bus_dmamap_t dmamap;
1663
1664 DPRINTFN(2, ("msk_rxeof\n"));
1665
1666 cur = sc_if->sk_cdata.sk_rx_cons;
1667 SK_INC(sc_if->sk_cdata.sk_rx_cons, MSK_RX_RING_CNT);
1668 SK_INC(sc_if->sk_cdata.sk_rx_prod, MSK_RX_RING_CNT);
1669
1670 /* Sync the descriptor */
1671 MSK_CDRXSYNC(sc_if, cur, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1672
1673 cur_rx = &sc_if->sk_cdata.sk_rx_chain[cur];
1674 dmamap = sc_if->sk_cdata.sk_rx_jumbo_map;
1675
1676 bus_dmamap_sync(sc_if->sk_softc->sc_dmatag, dmamap, 0,
1677 dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1678
1679 m = cur_rx->sk_mbuf;
1680 cur_rx->sk_mbuf = NULL;
1681
1682 if (total_len < SK_MIN_FRAMELEN ||
1683 total_len > SK_JUMBO_FRAMELEN ||
1684 msk_rxvalid(sc, rxstat, total_len) == 0) {
1685 ifp->if_ierrors++;
1686 msk_newbuf(sc_if, cur, m, dmamap);
1687 return;
1688 }
1689
1690 /*
1691 * Try to allocate a new jumbo buffer. If that fails, copy the
1692 * packet to mbufs and put the jumbo buffer back in the ring
1693 * so it can be re-used. If allocating mbufs fails, then we
1694 * have to drop the packet.
1695 */
1696 if (msk_newbuf(sc_if, cur, NULL, dmamap) == ENOBUFS) {
1697 struct mbuf *m0;
1698 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1699 total_len + ETHER_ALIGN, 0, ifp, NULL);
1700 msk_newbuf(sc_if, cur, m, dmamap);
1701 if (m0 == NULL) {
1702 ifp->if_ierrors++;
1703 return;
1704 }
1705 m_adj(m0, ETHER_ALIGN);
1706 m = m0;
1707 } else {
1708 m->m_pkthdr.rcvif = ifp;
1709 m->m_pkthdr.len = m->m_len = total_len;
1710 }
1711
1712 ifp->if_ipackets++;
1713
1714 #if NBPFILTER > 0
1715 if (ifp->if_bpf)
1716 bpf_mtap(ifp->if_bpf, m);
1717 #endif
1718
1719 /* pass it on. */
1720 (*ifp->if_input)(ifp, m);
1721 }
1722
1723 void
1724 msk_txeof(struct sk_if_softc *sc_if, int idx)
1725 {
1726 struct sk_softc *sc = sc_if->sk_softc;
1727 struct msk_tx_desc *cur_tx;
1728 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1729 u_int32_t sk_ctl;
1730 struct sk_txmap_entry *entry;
1731 int cons, prog;
1732
1733 DPRINTFN(2, ("msk_txeof\n"));
1734
1735 /*
1736 * Go through our tx ring and free mbufs for those
1737 * frames that have been sent.
1738 */
1739 cons = sc_if->sk_cdata.sk_tx_cons;
1740 prog = 0;
1741 while (cons != idx) {
1742 if (sc_if->sk_cdata.sk_tx_cnt <= 0)
1743 break;
1744 prog++;
1745 cur_tx = &sc_if->sk_rdata->sk_tx_ring[cons];
1746
1747 MSK_CDTXSYNC(sc_if, cons, 1,
1748 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1749 sk_ctl = cur_tx->sk_ctl;
1750 MSK_CDTXSYNC(sc_if, cons, 1, BUS_DMASYNC_PREREAD);
1751 #ifdef MSK_DEBUG
1752 if (mskdebug >= 2)
1753 msk_dump_txdesc(cur_tx, cons);
1754 #endif
1755 if (sk_ctl & SK_Y2_TXCTL_LASTFRAG)
1756 ifp->if_opackets++;
1757 if (sc_if->sk_cdata.sk_tx_chain[cons].sk_mbuf != NULL) {
1758 entry = sc_if->sk_cdata.sk_tx_map[cons];
1759
1760 bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
1761 entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1762
1763 bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
1764 SIMPLEQ_INSERT_TAIL(&sc_if->sk_txmap_head, entry,
1765 link);
1766 sc_if->sk_cdata.sk_tx_map[cons] = NULL;
1767 m_freem(sc_if->sk_cdata.sk_tx_chain[cons].sk_mbuf);
1768 sc_if->sk_cdata.sk_tx_chain[cons].sk_mbuf = NULL;
1769 }
1770 sc_if->sk_cdata.sk_tx_cnt--;
1771 SK_INC(cons, MSK_TX_RING_CNT);
1772 }
1773 ifp->if_timer = sc_if->sk_cdata.sk_tx_cnt > 0 ? 5 : 0;
1774
1775 if (sc_if->sk_cdata.sk_tx_cnt < MSK_TX_RING_CNT - 2)
1776 ifp->if_flags &= ~IFF_OACTIVE;
1777
1778 if (prog > 0)
1779 sc_if->sk_cdata.sk_tx_cons = cons;
1780 }
1781
1782 void
1783 msk_tick(void *xsc_if)
1784 {
1785 struct sk_if_softc *sc_if = xsc_if;
1786 struct mii_data *mii = &sc_if->sk_mii;
1787
1788 mii_tick(mii);
1789 callout_schedule(&sc_if->sk_tick_ch, hz);
1790 }
1791
1792 void
1793 msk_intr_yukon(struct sk_if_softc *sc_if)
1794 {
1795 u_int8_t status;
1796
1797 status = SK_IF_READ_1(sc_if, 0, SK_GMAC_ISR);
1798 /* RX overrun */
1799 if ((status & SK_GMAC_INT_RX_OVER) != 0) {
1800 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST,
1801 SK_RFCTL_RX_FIFO_OVER);
1802 }
1803 /* TX underrun */
1804 if ((status & SK_GMAC_INT_TX_UNDER) != 0) {
1805 SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST,
1806 SK_TFCTL_TX_FIFO_UNDER);
1807 }
1808
1809 DPRINTFN(2, ("msk_intr_yukon status=%#x\n", status));
1810 }
1811
1812 int
1813 msk_intr(void *xsc)
1814 {
1815 struct sk_softc *sc = xsc;
1816 struct sk_if_softc *sc_if0 = sc->sk_if[SK_PORT_A];
1817 struct sk_if_softc *sc_if1 = sc->sk_if[SK_PORT_B];
1818 struct ifnet *ifp0 = NULL, *ifp1 = NULL;
1819 int claimed = 0;
1820 u_int32_t status;
1821 uint32_t st_status;
1822 uint16_t st_len;
1823 uint8_t st_opcode, st_link;
1824 struct msk_status_desc *cur_st;
1825
1826 status = CSR_READ_4(sc, SK_Y2_ISSR2);
1827 if (status == 0) {
1828 CSR_WRITE_4(sc, SK_Y2_ICR, 2);
1829 return (0);
1830 }
1831
1832 status = CSR_READ_4(sc, SK_ISR);
1833
1834 if (sc_if0 != NULL)
1835 ifp0 = &sc_if0->sk_ethercom.ec_if;
1836 if (sc_if1 != NULL)
1837 ifp1 = &sc_if1->sk_ethercom.ec_if;
1838
1839 if (sc_if0 && (status & SK_Y2_IMR_MAC1) &&
1840 (ifp0->if_flags & IFF_RUNNING)) {
1841 msk_intr_yukon(sc_if0);
1842 }
1843
1844 if (sc_if1 && (status & SK_Y2_IMR_MAC2) &&
1845 (ifp1->if_flags & IFF_RUNNING)) {
1846 msk_intr_yukon(sc_if1);
1847 }
1848
1849 for (;;) {
1850 cur_st = &sc->sk_status_ring[sc->sk_status_idx];
1851 MSK_CDSTSYNC(sc, sc->sk_status_idx,
1852 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1853 st_opcode = cur_st->sk_opcode;
1854 if ((st_opcode & SK_Y2_STOPC_OWN) == 0) {
1855 MSK_CDSTSYNC(sc, sc->sk_status_idx,
1856 BUS_DMASYNC_PREREAD);
1857 break;
1858 }
1859 st_status = le32toh(cur_st->sk_status);
1860 st_len = le16toh(cur_st->sk_len);
1861 st_link = cur_st->sk_link;
1862 st_opcode &= ~SK_Y2_STOPC_OWN;
1863
1864 switch (st_opcode) {
1865 case SK_Y2_STOPC_RXSTAT:
1866 msk_rxeof(sc->sk_if[st_link], st_len, st_status);
1867 SK_IF_WRITE_2(sc->sk_if[st_link], 0,
1868 SK_RXQ1_Y2_PREF_PUTIDX,
1869 sc->sk_if[st_link]->sk_cdata.sk_rx_prod);
1870 break;
1871 case SK_Y2_STOPC_TXSTAT:
1872 if (sc_if0)
1873 msk_txeof(sc_if0, st_status
1874 & SK_Y2_ST_TXA1_MSKL);
1875 if (sc_if1)
1876 msk_txeof(sc_if1,
1877 ((st_status & SK_Y2_ST_TXA2_MSKL)
1878 >> SK_Y2_ST_TXA2_SHIFTL)
1879 | ((st_len & SK_Y2_ST_TXA2_MSKH) << SK_Y2_ST_TXA2_SHIFTH));
1880 break;
1881 default:
1882 aprint_error("opcode=0x%x\n", st_opcode);
1883 break;
1884 }
1885 SK_INC(sc->sk_status_idx, MSK_STATUS_RING_CNT);
1886 }
1887
1888 #define MSK_STATUS_RING_OWN_CNT(sc) \
1889 (((sc)->sk_status_idx + MSK_STATUS_RING_CNT - \
1890 (sc)->sk_status_own_idx) % MSK_STATUS_RING_CNT)
1891
1892 while (MSK_STATUS_RING_OWN_CNT(sc) > MSK_STATUS_RING_CNT / 2) {
1893 cur_st = &sc->sk_status_ring[sc->sk_status_own_idx];
1894 cur_st->sk_opcode &= ~SK_Y2_STOPC_OWN;
1895 MSK_CDSTSYNC(sc, sc->sk_status_own_idx,
1896 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1897
1898 SK_INC(sc->sk_status_own_idx, MSK_STATUS_RING_CNT);
1899 }
1900
1901 if (status & SK_Y2_IMR_BMU) {
1902 CSR_WRITE_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_IRQ_CLEAR);
1903 claimed = 1;
1904 }
1905
1906 CSR_WRITE_4(sc, SK_Y2_ICR, 2);
1907
1908 if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
1909 msk_start(ifp0);
1910 if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
1911 msk_start(ifp1);
1912
1913 #if NRND > 0
1914 if (RND_ENABLED(&sc->rnd_source))
1915 rnd_add_uint32(&sc->rnd_source, status);
1916 #endif
1917
1918 if (sc->sk_int_mod_pending)
1919 msk_update_int_mod(sc);
1920
1921 return claimed;
1922 }
1923
1924 void
1925 msk_init_yukon(struct sk_if_softc *sc_if)
1926 {
1927 u_int32_t v;
1928 u_int16_t reg;
1929 struct sk_softc *sc;
1930 int i;
1931
1932 sc = sc_if->sk_softc;
1933
1934 DPRINTFN(2, ("msk_init_yukon: start: sk_csr=%#x\n",
1935 CSR_READ_4(sc_if->sk_softc, SK_CSR)));
1936
1937 DPRINTFN(6, ("msk_init_yukon: 1\n"));
1938
1939 /* GMAC and GPHY Reset */
1940 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
1941 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
1942 DELAY(1000);
1943
1944 DPRINTFN(6, ("msk_init_yukon: 2\n"));
1945
1946 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_CLEAR);
1947 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
1948 SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
1949
1950 DPRINTFN(3, ("msk_init_yukon: gmac_ctrl=%#x\n",
1951 SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
1952
1953 DPRINTFN(6, ("msk_init_yukon: 3\n"));
1954
1955 /* unused read of the interrupt source register */
1956 DPRINTFN(6, ("msk_init_yukon: 4\n"));
1957 SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
1958
1959 DPRINTFN(6, ("msk_init_yukon: 4a\n"));
1960 reg = SK_YU_READ_2(sc_if, YUKON_PAR);
1961 DPRINTFN(6, ("msk_init_yukon: YUKON_PAR=%#x\n", reg));
1962
1963 /* MIB Counter Clear Mode set */
1964 reg |= YU_PAR_MIB_CLR;
1965 DPRINTFN(6, ("msk_init_yukon: YUKON_PAR=%#x\n", reg));
1966 DPRINTFN(6, ("msk_init_yukon: 4b\n"));
1967 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
1968
1969 /* MIB Counter Clear Mode clear */
1970 DPRINTFN(6, ("msk_init_yukon: 5\n"));
1971 reg &= ~YU_PAR_MIB_CLR;
1972 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
1973
1974 /* receive control reg */
1975 DPRINTFN(6, ("msk_init_yukon: 7\n"));
1976 SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_CRCR);
1977
1978 /* transmit control register */
1979 SK_YU_WRITE_2(sc_if, YUKON_TCR, (0x04 << 10));
1980
1981 /* transmit flow control register */
1982 SK_YU_WRITE_2(sc_if, YUKON_TFCR, 0xffff);
1983
1984 /* transmit parameter register */
1985 DPRINTFN(6, ("msk_init_yukon: 8\n"));
1986 SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
1987 YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1c) | 0x04);
1988
1989 /* serial mode register */
1990 DPRINTFN(6, ("msk_init_yukon: 9\n"));
1991 reg = YU_SMR_DATA_BLIND(0x1c) |
1992 YU_SMR_MFL_VLAN |
1993 YU_SMR_IPG_DATA(0x1e);
1994
1995 if (sc->sk_type != SK_YUKON_FE)
1996 reg |= YU_SMR_MFL_JUMBO;
1997
1998 SK_YU_WRITE_2(sc_if, YUKON_SMR, reg);
1999
2000 DPRINTFN(6, ("msk_init_yukon: 10\n"));
2001 /* Setup Yukon's address */
2002 for (i = 0; i < 3; i++) {
2003 /* Write Source Address 1 (unicast filter) */
2004 SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
2005 sc_if->sk_enaddr[i * 2] |
2006 sc_if->sk_enaddr[i * 2 + 1] << 8);
2007 }
2008
2009 for (i = 0; i < 3; i++) {
2010 reg = sk_win_read_2(sc_if->sk_softc,
2011 SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
2012 SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
2013 }
2014
2015 /* Set promiscuous mode */
2016 msk_setpromisc(sc_if);
2017
2018 /* Set multicast filter */
2019 DPRINTFN(6, ("msk_init_yukon: 11\n"));
2020 msk_setmulti(sc_if);
2021
2022 /* enable interrupt mask for counter overflows */
2023 DPRINTFN(6, ("msk_init_yukon: 12\n"));
2024 SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
2025 SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
2026 SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
2027
2028 /* Configure RX MAC FIFO Flush Mask */
2029 v = YU_RXSTAT_FOFL | YU_RXSTAT_CRCERR | YU_RXSTAT_MIIERR |
2030 YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC | YU_RXSTAT_RUNT |
2031 YU_RXSTAT_JABBER;
2032 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_MASK, v);
2033
2034 /* Configure RX MAC FIFO */
2035 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
2036 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON |
2037 SK_RFCTL_FIFO_FLUSH_ON);
2038
2039 /* Increase flush threshould to 64 bytes */
2040 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_THRESHOLD,
2041 SK_RFCTL_FIFO_THRESHOLD + 1);
2042
2043 /* Configure TX MAC FIFO */
2044 SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
2045 SK_IF_WRITE_2(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
2046
2047 #if 1
2048 SK_YU_WRITE_2(sc_if, YUKON_GPCR, YU_GPCR_TXEN | YU_GPCR_RXEN);
2049 #endif
2050 DPRINTFN(6, ("msk_init_yukon: end\n"));
2051 }
2052
2053 /*
2054 * Note that to properly initialize any part of the GEnesis chip,
2055 * you first have to take it out of reset mode.
2056 */
2057 int
2058 msk_init(struct ifnet *ifp)
2059 {
2060 struct sk_if_softc *sc_if = ifp->if_softc;
2061 struct sk_softc *sc = sc_if->sk_softc;
2062 int rc = 0, s;
2063 uint32_t imr, imtimer_ticks;
2064
2065
2066 DPRINTFN(2, ("msk_init\n"));
2067
2068 s = splnet();
2069
2070 /* Cancel pending I/O and free all RX/TX buffers. */
2071 msk_stop(ifp,0);
2072
2073 /* Configure I2C registers */
2074
2075 /* Configure XMAC(s) */
2076 msk_init_yukon(sc_if);
2077 if ((rc = ether_mediachange(ifp)) != 0)
2078 goto out;
2079
2080 /* Configure transmit arbiter(s) */
2081 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_ON);
2082 #if 0
2083 SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
2084 #endif
2085
2086 /* Configure RAMbuffers */
2087 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
2088 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
2089 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
2090 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
2091 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
2092 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
2093
2094 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_UNRESET);
2095 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_STORENFWD_ON);
2096 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_START, sc_if->sk_tx_ramstart);
2097 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_WR_PTR, sc_if->sk_tx_ramstart);
2098 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_RD_PTR, sc_if->sk_tx_ramstart);
2099 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_END, sc_if->sk_tx_ramend);
2100 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_ON);
2101
2102 /* Configure BMUs */
2103 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000016);
2104 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000d28);
2105 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000080);
2106 SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_WM, 0x0600); /* XXX ??? */
2107
2108 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000016);
2109 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000d28);
2110 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000080);
2111 SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_WM, 0x0600); /* XXX ??? */
2112
2113 /* Make sure the sync transmit queue is disabled. */
2114 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET);
2115
2116 /* Init descriptors */
2117 if (msk_init_rx_ring(sc_if) == ENOBUFS) {
2118 aprint_error("%s: initialization failed: no "
2119 "memory for rx buffers\n", sc_if->sk_dev.dv_xname);
2120 msk_stop(ifp,0);
2121 splx(s);
2122 return ENOBUFS;
2123 }
2124
2125 if (msk_init_tx_ring(sc_if) == ENOBUFS) {
2126 aprint_error("%s: initialization failed: no "
2127 "memory for tx buffers\n", sc_if->sk_dev.dv_xname);
2128 msk_stop(ifp,0);
2129 splx(s);
2130 return ENOBUFS;
2131 }
2132
2133 /* Set interrupt moderation if changed via sysctl. */
2134 switch (sc->sk_type) {
2135 case SK_YUKON_EC:
2136 case SK_YUKON_EC_U:
2137 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
2138 break;
2139 case SK_YUKON_FE:
2140 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
2141 break;
2142 case SK_YUKON_XL:
2143 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
2144 break;
2145 default:
2146 imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
2147 }
2148 imr = sk_win_read_4(sc, SK_IMTIMERINIT);
2149 if (imr != SK_IM_USECS(sc->sk_int_mod)) {
2150 sk_win_write_4(sc, SK_IMTIMERINIT,
2151 SK_IM_USECS(sc->sk_int_mod));
2152 aprint_verbose("%s: interrupt moderation is %d us\n",
2153 sc->sk_dev.dv_xname, sc->sk_int_mod);
2154 }
2155
2156 /* Initialize prefetch engine. */
2157 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000001);
2158 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000002);
2159 SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_LIDX, MSK_RX_RING_CNT - 1);
2160 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_ADDRLO,
2161 MSK_RX_RING_ADDR(sc_if, 0));
2162 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_ADDRHI,
2163 (u_int64_t)MSK_RX_RING_ADDR(sc_if, 0) >> 32);
2164 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000008);
2165 SK_IF_READ_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR);
2166
2167 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000001);
2168 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000002);
2169 SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_PREF_LIDX, MSK_TX_RING_CNT - 1);
2170 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_ADDRLO,
2171 MSK_TX_RING_ADDR(sc_if, 0));
2172 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_ADDRHI,
2173 (u_int64_t)MSK_TX_RING_ADDR(sc_if, 0) >> 32);
2174 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000008);
2175 SK_IF_READ_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR);
2176
2177 SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_PUTIDX,
2178 sc_if->sk_cdata.sk_rx_prod);
2179
2180 /* Configure interrupt handling */
2181 if (sc_if->sk_port == SK_PORT_A)
2182 sc->sk_intrmask |= SK_Y2_INTRS1;
2183 else
2184 sc->sk_intrmask |= SK_Y2_INTRS2;
2185 sc->sk_intrmask |= SK_Y2_IMR_BMU;
2186 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2187
2188 ifp->if_flags |= IFF_RUNNING;
2189 ifp->if_flags &= ~IFF_OACTIVE;
2190
2191 callout_schedule(&sc_if->sk_tick_ch, hz);
2192
2193 out:
2194 splx(s);
2195 return rc;
2196 }
2197
2198 void
2199 msk_stop(struct ifnet *ifp, int disable)
2200 {
2201 struct sk_if_softc *sc_if = ifp->if_softc;
2202 struct sk_softc *sc = sc_if->sk_softc;
2203 struct sk_txmap_entry *dma;
2204 int i;
2205
2206 DPRINTFN(2, ("msk_stop\n"));
2207
2208 callout_stop(&sc_if->sk_tick_ch);
2209
2210 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2211
2212 /* Stop transfer of Tx descriptors */
2213
2214 /* Stop transfer of Rx descriptors */
2215
2216 /* Turn off various components of this interface. */
2217 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2218 SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
2219 SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
2220 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
2221 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2222 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, SK_TXBMU_OFFLINE);
2223 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2224 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
2225 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2226 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_TXLEDCTL_COUNTER_STOP);
2227 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
2228 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
2229
2230 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000001);
2231 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000001);
2232
2233 /* Disable interrupts */
2234 if (sc_if->sk_port == SK_PORT_A)
2235 sc->sk_intrmask &= ~SK_Y2_INTRS1;
2236 else
2237 sc->sk_intrmask &= ~SK_Y2_INTRS2;
2238 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2239
2240 SK_XM_READ_2(sc_if, XM_ISR);
2241 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2242
2243 /* Free RX and TX mbufs still in the queues. */
2244 for (i = 0; i < MSK_RX_RING_CNT; i++) {
2245 if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
2246 m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
2247 sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
2248 }
2249 }
2250
2251 for (i = 0; i < MSK_TX_RING_CNT; i++) {
2252 if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
2253 m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
2254 sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
2255 #if 1
2256 SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head,
2257 sc_if->sk_cdata.sk_tx_map[i], link);
2258 sc_if->sk_cdata.sk_tx_map[i] = 0;
2259 #endif
2260 }
2261 }
2262
2263 #if 1
2264 while ((dma = SIMPLEQ_FIRST(&sc_if->sk_txmap_head))) {
2265 SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
2266 bus_dmamap_destroy(sc->sc_dmatag, dma->dmamap);
2267 free(dma, M_DEVBUF);
2268 }
2269 #endif
2270 }
2271
2272 CFATTACH_DECL(mskc, sizeof(struct sk_softc), mskc_probe, mskc_attach,
2273 NULL, NULL);
2274
2275 CFATTACH_DECL(msk, sizeof(struct sk_if_softc), msk_probe, msk_attach,
2276 NULL, NULL);
2277
2278 #ifdef MSK_DEBUG
2279 void
2280 msk_dump_txdesc(struct msk_tx_desc *le, int idx)
2281 {
2282 #define DESC_PRINT(X) \
2283 if (X) \
2284 printf("txdesc[%d]." #X "=%#x\n", \
2285 idx, X);
2286
2287 DESC_PRINT(letoh32(le->sk_addr));
2288 DESC_PRINT(letoh16(le->sk_len));
2289 DESC_PRINT(le->sk_ctl);
2290 DESC_PRINT(le->sk_opcode);
2291 #undef DESC_PRINT
2292 }
2293
2294 void
2295 msk_dump_bytes(const char *data, int len)
2296 {
2297 int c, i, j;
2298
2299 for (i = 0; i < len; i += 16) {
2300 printf("%08x ", i);
2301 c = len - i;
2302 if (c > 16) c = 16;
2303
2304 for (j = 0; j < c; j++) {
2305 printf("%02x ", data[i + j] & 0xff);
2306 if ((j & 0xf) == 7 && j > 0)
2307 printf(" ");
2308 }
2309
2310 for (; j < 16; j++)
2311 printf(" ");
2312 printf(" ");
2313
2314 for (j = 0; j < c; j++) {
2315 int ch = data[i + j] & 0xff;
2316 printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
2317 }
2318
2319 printf("\n");
2320
2321 if (c < 16)
2322 break;
2323 }
2324 }
2325
2326 void
2327 msk_dump_mbuf(struct mbuf *m)
2328 {
2329 int count = m->m_pkthdr.len;
2330
2331 printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
2332
2333 while (count > 0 && m) {
2334 printf("m=%p, m->m_data=%p, m->m_len=%d\n",
2335 m, m->m_data, m->m_len);
2336 msk_dump_bytes(mtod(m, char *), m->m_len);
2337
2338 count -= m->m_len;
2339 m = m->m_next;
2340 }
2341 }
2342 #endif
2343
2344 static int
2345 msk_sysctl_handler(SYSCTLFN_ARGS)
2346 {
2347 int error, t;
2348 struct sysctlnode node;
2349 struct sk_softc *sc;
2350
2351 node = *rnode;
2352 sc = node.sysctl_data;
2353 t = sc->sk_int_mod;
2354 node.sysctl_data = &t;
2355 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2356 if (error || newp == NULL)
2357 return error;
2358
2359 if (t < SK_IM_MIN || t > SK_IM_MAX)
2360 return EINVAL;
2361
2362 /* update the softc with sysctl-changed value, and mark
2363 for hardware update */
2364 sc->sk_int_mod = t;
2365 sc->sk_int_mod_pending = 1;
2366 return 0;
2367 }
2368
2369 /*
2370 * Set up sysctl(3) MIB, hw.sk.* - Individual controllers will be
2371 * set up in skc_attach()
2372 */
2373 SYSCTL_SETUP(sysctl_msk, "sysctl msk subtree setup")
2374 {
2375 int rc;
2376 const struct sysctlnode *node;
2377
2378 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
2379 0, CTLTYPE_NODE, "hw", NULL,
2380 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
2381 goto err;
2382 }
2383
2384 if ((rc = sysctl_createv(clog, 0, NULL, &node,
2385 0, CTLTYPE_NODE, "msk",
2386 SYSCTL_DESCR("msk interface controls"),
2387 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
2388 goto err;
2389 }
2390
2391 msk_root_num = node->sysctl_num;
2392 return;
2393
2394 err:
2395 aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
2396 }
2397