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