if_msk.c revision 1.5 1 /* $NetBSD: if_msk.c,v 1.5 2007/01/30 11:34:38 msaitoh 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 *);
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, caddr_t);
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 *, caddr_t, 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 *, caddr_t, 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, caddr_t addr, int slot)
328 {
329 int base = XM_RXFILT_ENTRY(slot);
330
331 SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
332 SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
333 SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
334 }
335
336 void
337 msk_setmulti(struct sk_if_softc *sc_if)
338 {
339 struct ifnet *ifp= &sc_if->sk_ethercom.ec_if;
340 u_int32_t hashes[2] = { 0, 0 };
341 int h;
342 struct ethercom *ec = &sc_if->sk_ethercom;
343 struct ether_multi *enm;
344 struct ether_multistep step;
345
346 /* First, zot all the existing filters. */
347 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
348 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
349 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
350 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
351
352
353 /* Now program new ones. */
354 allmulti:
355 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
356 hashes[0] = 0xFFFFFFFF;
357 hashes[1] = 0xFFFFFFFF;
358 } else {
359 /* First find the tail of the list. */
360 ETHER_FIRST_MULTI(step, ec, enm);
361 while (enm != NULL) {
362 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
363 ETHER_ADDR_LEN)) {
364 ifp->if_flags |= IFF_ALLMULTI;
365 goto allmulti;
366 }
367 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) &
368 ((1 << HASH_BITS) - 1);
369 if (h < 32)
370 hashes[0] |= (1 << h);
371 else
372 hashes[1] |= (1 << (h - 32));
373
374 ETHER_NEXT_MULTI(step, enm);
375 }
376 }
377
378 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
379 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
380 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
381 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
382 }
383
384 void
385 msk_setpromisc(struct sk_if_softc *sc_if)
386 {
387 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
388
389 if (ifp->if_flags & IFF_PROMISC)
390 SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
391 YU_RCR_UFLEN | YU_RCR_MUFLEN);
392 else
393 SK_YU_SETBIT_2(sc_if, YUKON_RCR,
394 YU_RCR_UFLEN | YU_RCR_MUFLEN);
395 }
396
397 int
398 msk_init_rx_ring(struct sk_if_softc *sc_if)
399 {
400 struct msk_chain_data *cd = &sc_if->sk_cdata;
401 struct msk_ring_data *rd = sc_if->sk_rdata;
402 int i, nexti;
403
404 bzero((char *)rd->sk_rx_ring,
405 sizeof(struct msk_rx_desc) * MSK_RX_RING_CNT);
406
407 for (i = 0; i < MSK_RX_RING_CNT; i++) {
408 cd->sk_rx_chain[i].sk_le = &rd->sk_rx_ring[i];
409 if (i == (MSK_RX_RING_CNT - 1))
410 nexti = 0;
411 else
412 nexti = i + 1;
413 cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[nexti];
414 }
415
416 for (i = 0; i < MSK_RX_RING_CNT; i++) {
417 if (msk_newbuf(sc_if, i, NULL,
418 sc_if->sk_cdata.sk_rx_jumbo_map) == ENOBUFS) {
419 aprint_error("%s: failed alloc of %dth mbuf\n",
420 sc_if->sk_dev.dv_xname, i);
421 return (ENOBUFS);
422 }
423 }
424
425 sc_if->sk_cdata.sk_rx_prod = MSK_RX_RING_CNT - 1;
426 sc_if->sk_cdata.sk_rx_cons = 0;
427
428 return (0);
429 }
430
431 int
432 msk_init_tx_ring(struct sk_if_softc *sc_if)
433 {
434 struct sk_softc *sc = sc_if->sk_softc;
435 struct msk_chain_data *cd = &sc_if->sk_cdata;
436 struct msk_ring_data *rd = sc_if->sk_rdata;
437 bus_dmamap_t dmamap;
438 struct sk_txmap_entry *entry;
439 int i, nexti;
440
441 bzero((char *)sc_if->sk_rdata->sk_tx_ring,
442 sizeof(struct msk_tx_desc) * MSK_TX_RING_CNT);
443
444 SIMPLEQ_INIT(&sc_if->sk_txmap_head);
445 for (i = 0; i < MSK_TX_RING_CNT; i++) {
446 cd->sk_tx_chain[i].sk_le = &rd->sk_tx_ring[i];
447 if (i == (MSK_TX_RING_CNT - 1))
448 nexti = 0;
449 else
450 nexti = i + 1;
451 cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[nexti];
452
453 if (bus_dmamap_create(sc->sc_dmatag, SK_JLEN, SK_NTXSEG,
454 SK_JLEN, 0, BUS_DMA_NOWAIT, &dmamap))
455 return (ENOBUFS);
456
457 entry = malloc(sizeof(*entry), M_DEVBUF, M_NOWAIT);
458 if (!entry) {
459 bus_dmamap_destroy(sc->sc_dmatag, dmamap);
460 return (ENOBUFS);
461 }
462 entry->dmamap = dmamap;
463 SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head, entry, link);
464 }
465
466 sc_if->sk_cdata.sk_tx_prod = 0;
467 sc_if->sk_cdata.sk_tx_cons = 0;
468 sc_if->sk_cdata.sk_tx_cnt = 0;
469
470 MSK_CDTXSYNC(sc_if, 0, MSK_TX_RING_CNT,
471 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
472
473 return (0);
474 }
475
476 int
477 msk_newbuf(struct sk_if_softc *sc_if, int i, struct mbuf *m,
478 bus_dmamap_t dmamap)
479 {
480 struct mbuf *m_new = NULL;
481 struct sk_chain *c;
482 struct msk_rx_desc *r;
483
484 if (m == NULL) {
485 caddr_t buf = NULL;
486
487 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
488 if (m_new == NULL)
489 return (ENOBUFS);
490
491 /* Allocate the jumbo buffer */
492 buf = msk_jalloc(sc_if);
493 if (buf == NULL) {
494 m_freem(m_new);
495 DPRINTFN(1, ("%s jumbo allocation failed -- packet "
496 "dropped!\n", sc_if->sk_ethercom.ec_if.if_xname));
497 return (ENOBUFS);
498 }
499
500 /* Attach the buffer to the mbuf */
501 m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
502 MEXTADD(m_new, buf, SK_JLEN, 0, msk_jfree, sc_if);
503 } else {
504 /*
505 * We're re-using a previously allocated mbuf;
506 * be sure to re-init pointers and lengths to
507 * default values.
508 */
509 m_new = m;
510 m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
511 m_new->m_data = m_new->m_ext.ext_buf;
512 }
513 m_adj(m_new, ETHER_ALIGN);
514
515 c = &sc_if->sk_cdata.sk_rx_chain[i];
516 r = c->sk_le;
517 c->sk_mbuf = m_new;
518 r->sk_addr = htole32(dmamap->dm_segs[0].ds_addr +
519 (((vaddr_t)m_new->m_data
520 - (vaddr_t)sc_if->sk_cdata.sk_jumbo_buf)));
521 r->sk_len = htole16(SK_JLEN);
522 r->sk_ctl = 0;
523 r->sk_opcode = SK_Y2_RXOPC_PACKET | SK_Y2_RXOPC_OWN;
524
525 MSK_CDRXSYNC(sc_if, i, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
526
527 return (0);
528 }
529
530 /*
531 * Memory management for jumbo frames.
532 */
533
534 int
535 msk_alloc_jumbo_mem(struct sk_if_softc *sc_if)
536 {
537 struct sk_softc *sc = sc_if->sk_softc;
538 caddr_t ptr, kva;
539 bus_dma_segment_t seg;
540 int i, rseg, state, error;
541 struct sk_jpool_entry *entry;
542
543 state = error = 0;
544
545 /* Grab a big chunk o' storage. */
546 if (bus_dmamem_alloc(sc->sc_dmatag, MSK_JMEM, PAGE_SIZE, 0,
547 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
548 aprint_error(": can't alloc rx buffers");
549 return (ENOBUFS);
550 }
551
552 state = 1;
553 if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg, MSK_JMEM, &kva,
554 BUS_DMA_NOWAIT)) {
555 aprint_error(": can't map dma buffers (%d bytes)", MSK_JMEM);
556 error = ENOBUFS;
557 goto out;
558 }
559
560 state = 2;
561 if (bus_dmamap_create(sc->sc_dmatag, MSK_JMEM, 1, MSK_JMEM, 0,
562 BUS_DMA_NOWAIT, &sc_if->sk_cdata.sk_rx_jumbo_map)) {
563 aprint_error(": can't create dma map");
564 error = ENOBUFS;
565 goto out;
566 }
567
568 state = 3;
569 if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_cdata.sk_rx_jumbo_map,
570 kva, MSK_JMEM, NULL, BUS_DMA_NOWAIT)) {
571 aprint_error(": can't load dma map");
572 error = ENOBUFS;
573 goto out;
574 }
575
576 state = 4;
577 sc_if->sk_cdata.sk_jumbo_buf = (caddr_t)kva;
578 DPRINTFN(1,("msk_jumbo_buf = %p\n", (caddr_t)sc_if->sk_cdata.sk_jumbo_buf));
579
580 LIST_INIT(&sc_if->sk_jfree_listhead);
581 LIST_INIT(&sc_if->sk_jinuse_listhead);
582
583 /*
584 * Now divide it up into 9K pieces and save the addresses
585 * in an array.
586 */
587 ptr = sc_if->sk_cdata.sk_jumbo_buf;
588 for (i = 0; i < MSK_JSLOTS; i++) {
589 sc_if->sk_cdata.sk_jslots[i] = ptr;
590 ptr += SK_JLEN;
591 entry = malloc(sizeof(struct sk_jpool_entry),
592 M_DEVBUF, M_NOWAIT);
593 if (entry == NULL) {
594 sc_if->sk_cdata.sk_jumbo_buf = NULL;
595 aprint_error(": no memory for jumbo buffer queue!");
596 error = ENOBUFS;
597 goto out;
598 }
599 entry->slot = i;
600 LIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
601 entry, jpool_entries);
602 }
603 out:
604 if (error != 0) {
605 switch (state) {
606 case 4:
607 bus_dmamap_unload(sc->sc_dmatag,
608 sc_if->sk_cdata.sk_rx_jumbo_map);
609 case 3:
610 bus_dmamap_destroy(sc->sc_dmatag,
611 sc_if->sk_cdata.sk_rx_jumbo_map);
612 case 2:
613 bus_dmamem_unmap(sc->sc_dmatag, kva, MSK_JMEM);
614 case 1:
615 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
616 break;
617 default:
618 break;
619 }
620 }
621
622 return (error);
623 }
624
625 /*
626 * Allocate a jumbo buffer.
627 */
628 void *
629 msk_jalloc(struct sk_if_softc *sc_if)
630 {
631 struct sk_jpool_entry *entry;
632
633 entry = LIST_FIRST(&sc_if->sk_jfree_listhead);
634
635 if (entry == NULL)
636 return (NULL);
637
638 LIST_REMOVE(entry, jpool_entries);
639 LIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries);
640 return (sc_if->sk_cdata.sk_jslots[entry->slot]);
641 }
642
643 /*
644 * Release a jumbo buffer.
645 */
646 void
647 msk_jfree(struct mbuf *m, caddr_t buf, size_t size, void *arg)
648 {
649 struct sk_jpool_entry *entry;
650 struct sk_if_softc *sc;
651 int i, s;
652
653 /* Extract the softc struct pointer. */
654 sc = (struct sk_if_softc *)arg;
655
656 if (sc == NULL)
657 panic("msk_jfree: can't find softc pointer!");
658
659 /* calculate the slot this buffer belongs to */
660 i = ((vaddr_t)buf
661 - (vaddr_t)sc->sk_cdata.sk_jumbo_buf) / SK_JLEN;
662
663 if ((i < 0) || (i >= MSK_JSLOTS))
664 panic("sk_jfree: asked to free buffer that we don't manage!");
665
666 s = splvm();
667 entry = LIST_FIRST(&sc->sk_jinuse_listhead);
668 if (entry == NULL)
669 panic("msk_jfree: buffer not in use!");
670 entry->slot = i;
671 LIST_REMOVE(entry, jpool_entries);
672 LIST_INSERT_HEAD(&sc->sk_jfree_listhead, entry, jpool_entries);
673
674 if (__predict_true(m != NULL))
675 pool_cache_put(&mbpool_cache, m);
676 splx(s);
677 }
678
679 /*
680 * Set media options.
681 */
682 int
683 msk_ifmedia_upd(struct ifnet *ifp)
684 {
685 struct sk_if_softc *sc_if = ifp->if_softc;
686
687 mii_mediachg(&sc_if->sk_mii);
688 return (0);
689 }
690
691 /*
692 * Report current media status.
693 */
694 void
695 msk_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
696 {
697 struct sk_if_softc *sc_if = ifp->if_softc;
698
699 mii_pollstat(&sc_if->sk_mii);
700 ifmr->ifm_active = sc_if->sk_mii.mii_media_active;
701 ifmr->ifm_status = sc_if->sk_mii.mii_media_status;
702 }
703
704 int
705 msk_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
706 {
707 struct sk_if_softc *sc_if = ifp->if_softc;
708 struct ifreq *ifr = (struct ifreq *) data;
709 struct mii_data *mii;
710 int s, error = 0;
711
712 s = splnet();
713
714 switch(command) {
715 case SIOCSIFMTU:
716 if (ifr->ifr_mtu < ETHERMIN)
717 return EINVAL;
718 else if (sc_if->sk_softc->sk_type != SK_YUKON_FE) {
719 if (ifr->ifr_mtu > SK_JUMBO_MTU)
720 error = EINVAL;
721 } else if (ifr->ifr_mtu > ETHERMTU)
722 error = EINVAL;
723 ifp->if_mtu = ifr->ifr_mtu;
724 break;
725 case SIOCGIFMEDIA:
726 case SIOCSIFMEDIA:
727 DPRINTFN(2,("msk_ioctl: SIOC[GS]IFMEDIA\n"));
728 mii = &sc_if->sk_mii;
729 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
730 DPRINTFN(2,("msk_ioctl: SIOC[GS]IFMEDIA done\n"));
731 break;
732 default:
733 DPRINTFN(2, ("msk_ioctl ETHER\n"));
734 error = ether_ioctl(ifp, command, data);
735
736 if (error == ENETRESET) {
737 /*
738 * Multicast list has changed; set the hardware
739 * filter accordingly.
740 */
741 if (ifp->if_flags & IFF_RUNNING)
742 msk_setmulti(sc_if);
743 error = 0;
744 }
745 break;
746 }
747
748 splx(s);
749 return (error);
750 }
751
752 void
753 msk_update_int_mod(struct sk_softc *sc)
754 {
755 u_int32_t imtimer_ticks;
756
757 /*
758 * Configure interrupt moderation. The moderation timer
759 * defers interrupts specified in the interrupt moderation
760 * timer mask based on the timeout specified in the interrupt
761 * moderation timer init register. Each bit in the timer
762 * register represents one tick, so to specify a timeout in
763 * microseconds, we have to multiply by the correct number of
764 * ticks-per-microsecond.
765 */
766 switch (sc->sk_type) {
767 case SK_YUKON_EC:
768 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
769 break;
770 default:
771 imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
772 }
773 aprint_verbose("%s: interrupt moderation is %d us\n",
774 sc->sk_dev.dv_xname, sc->sk_int_mod);
775 sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(sc->sk_int_mod));
776 sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
777 SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
778 sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
779 sc->sk_int_mod_pending = 0;
780 }
781
782 static int
783 msk_lookup(const struct pci_attach_args *pa)
784 {
785 const struct msk_product *pmsk;
786
787 for ( pmsk = &msk_products[0]; pmsk->msk_vendor != 0; pmsk++) {
788 if (PCI_VENDOR(pa->pa_id) == pmsk->msk_vendor &&
789 PCI_PRODUCT(pa->pa_id) == pmsk->msk_product)
790 return 1;
791 }
792 return 0;
793 }
794
795 /*
796 * Probe for a SysKonnect GEnesis chip. Check the PCI vendor and device
797 * IDs against our list and return a device name if we find a match.
798 */
799 int
800 mskc_probe(struct device *parent, struct cfdata *match,
801 void *aux)
802 {
803 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
804
805 return msk_lookup(pa);
806 }
807
808 /*
809 * Force the GEnesis into reset, then bring it out of reset.
810 */
811 void msk_reset(struct sk_softc *sc)
812 {
813 u_int32_t imtimer_ticks, reg1;
814 int reg;
815
816 DPRINTFN(2, ("msk_reset\n"));
817
818 CSR_WRITE_1(sc, SK_CSR, SK_CSR_SW_RESET);
819 CSR_WRITE_1(sc, SK_CSR, SK_CSR_MASTER_RESET);
820
821 DELAY(1000);
822 CSR_WRITE_1(sc, SK_CSR, SK_CSR_SW_UNRESET);
823 DELAY(2);
824 CSR_WRITE_1(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
825 sk_win_write_1(sc, SK_TESTCTL1, 2);
826
827 reg1 = sk_win_read_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG1));
828 if (sc->sk_type == SK_YUKON_XL && sc->sk_rev > SK_YUKON_XL_REV_A1)
829 reg1 |= (SK_Y2_REG1_PHY1_COMA | SK_Y2_REG1_PHY2_COMA);
830 else
831 reg1 &= ~(SK_Y2_REG1_PHY1_COMA | SK_Y2_REG1_PHY2_COMA);
832 sk_win_write_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG1), reg1);
833
834 if (sc->sk_type == SK_YUKON_XL && sc->sk_rev > SK_YUKON_XL_REV_A1)
835 sk_win_write_1(sc, SK_Y2_CLKGATE,
836 SK_Y2_CLKGATE_LINK1_GATE_DIS |
837 SK_Y2_CLKGATE_LINK2_GATE_DIS |
838 SK_Y2_CLKGATE_LINK1_CORE_DIS |
839 SK_Y2_CLKGATE_LINK2_CORE_DIS |
840 SK_Y2_CLKGATE_LINK1_PCI_DIS | SK_Y2_CLKGATE_LINK2_PCI_DIS);
841 else
842 sk_win_write_1(sc, SK_Y2_CLKGATE, 0);
843
844 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
845 CSR_WRITE_2(sc, SK_LINK_CTRL + SK_WIN_LEN, SK_LINK_RESET_SET);
846 DELAY(1000);
847 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
848 CSR_WRITE_2(sc, SK_LINK_CTRL + SK_WIN_LEN, SK_LINK_RESET_CLEAR);
849
850 sk_win_write_1(sc, SK_TESTCTL1, 1);
851
852 DPRINTFN(2, ("msk_reset: sk_csr=%x\n", CSR_READ_1(sc, SK_CSR)));
853 DPRINTFN(2, ("msk_reset: sk_link_ctrl=%x\n",
854 CSR_READ_2(sc, SK_LINK_CTRL)));
855
856 /* Disable ASF */
857 CSR_WRITE_1(sc, SK_Y2_ASF_CSR, SK_Y2_ASF_RESET);
858 CSR_WRITE_2(sc, SK_CSR, SK_CSR_ASF_OFF);
859
860 /* Clear I2C IRQ noise */
861 CSR_WRITE_4(sc, SK_I2CHWIRQ, 1);
862
863 /* Disable hardware timer */
864 CSR_WRITE_1(sc, SK_TIMERCTL, SK_IMCTL_STOP);
865 CSR_WRITE_1(sc, SK_TIMERCTL, SK_IMCTL_IRQ_CLEAR);
866
867 /* Disable descriptor polling */
868 CSR_WRITE_4(sc, SK_DPT_TIMER_CTRL, SK_DPT_TCTL_STOP);
869
870 /* Disable time stamps */
871 CSR_WRITE_1(sc, SK_TSTAMP_CTL, SK_TSTAMP_STOP);
872 CSR_WRITE_1(sc, SK_TSTAMP_CTL, SK_TSTAMP_IRQ_CLEAR);
873
874 /* Enable RAM interface */
875 sk_win_write_1(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
876 for (reg = SK_TO0;reg <= SK_TO11; reg++)
877 sk_win_write_1(sc, reg, 36);
878 sk_win_write_1(sc, SK_RAMCTL + (SK_WIN_LEN / 2), SK_RAMCTL_UNRESET);
879 for (reg = SK_TO0;reg <= SK_TO11; reg++)
880 sk_win_write_1(sc, reg + (SK_WIN_LEN / 2), 36);
881
882 /*
883 * Configure interrupt moderation. The moderation timer
884 * defers interrupts specified in the interrupt moderation
885 * timer mask based on the timeout specified in the interrupt
886 * moderation timer init register. Each bit in the timer
887 * register represents one tick, so to specify a timeout in
888 * microseconds, we have to multiply by the correct number of
889 * ticks-per-microsecond.
890 */
891 switch (sc->sk_type) {
892 case SK_YUKON_EC:
893 case SK_YUKON_XL:
894 case SK_YUKON_FE:
895 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
896 break;
897 default:
898 imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
899 }
900
901 /* Reset status ring. */
902 bzero((char *)sc->sk_status_ring,
903 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
904 sc->sk_status_idx = 0;
905
906 sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_RESET);
907 sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_UNRESET);
908
909 sk_win_write_2(sc, SK_STAT_BMU_LIDX, MSK_STATUS_RING_CNT - 1);
910 sk_win_write_4(sc, SK_STAT_BMU_ADDRLO,
911 sc->sk_status_map->dm_segs[0].ds_addr);
912 sk_win_write_4(sc, SK_STAT_BMU_ADDRHI,
913 (u_int64_t)sc->sk_status_map->dm_segs[0].ds_addr >> 32);
914 sk_win_write_2(sc, SK_STAT_BMU_TX_THRESH, 10);
915 sk_win_write_1(sc, SK_STAT_BMU_FIFOWM, 16);
916 sk_win_write_1(sc, SK_STAT_BMU_FIFOIWM, 16);
917
918 #if 0
919 sk_win_write_4(sc, SK_Y2_LEV_ITIMERINIT, SK_IM_USECS(100));
920 sk_win_write_4(sc, SK_Y2_TX_ITIMERINIT, SK_IM_USECS(1000));
921
922 sk_win_write_4(sc, SK_Y2_ISR_ITIMERINIT, SK_IM_USECS(20));
923 #else
924 sk_win_write_4(sc, SK_Y2_ISR_ITIMERINIT, SK_IM_USECS(4));
925 #endif
926
927 sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_ON);
928
929 sk_win_write_1(sc, SK_Y2_LEV_ITIMERCTL, SK_IMCTL_START);
930 sk_win_write_1(sc, SK_Y2_TX_ITIMERCTL, SK_IMCTL_START);
931 sk_win_write_1(sc, SK_Y2_ISR_ITIMERCTL, SK_IMCTL_START);
932
933 msk_update_int_mod(sc);
934 }
935
936 int
937 msk_probe(struct device *parent, struct cfdata *match,
938 void *aux)
939 {
940 struct skc_attach_args *sa = aux;
941
942 if (sa->skc_port != SK_PORT_A && sa->skc_port != SK_PORT_B)
943 return (0);
944
945 switch (sa->skc_type) {
946 case SK_YUKON_XL:
947 case SK_YUKON_EC_U:
948 case SK_YUKON_EC:
949 case SK_YUKON_FE:
950 return (1);
951 }
952
953 return (0);
954 }
955
956 /*
957 * Each XMAC chip is attached as a separate logical IP interface.
958 * Single port cards will have only one logical interface of course.
959 */
960 void
961 msk_attach(struct device *parent, struct device *self, void *aux)
962 {
963 struct sk_if_softc *sc_if = (struct sk_if_softc *) self;
964 struct sk_softc *sc = (struct sk_softc *)parent;
965 struct skc_attach_args *sa = aux;
966 struct ifnet *ifp;
967 caddr_t kva;
968 bus_dma_segment_t seg;
969 int i, rseg;
970 u_int32_t chunk, val;
971
972 sc_if->sk_port = sa->skc_port;
973 sc_if->sk_softc = sc;
974 sc->sk_if[sa->skc_port] = sc_if;
975
976 DPRINTFN(2, ("begin msk_attach: port=%d\n", sc_if->sk_port));
977
978 /*
979 * Get station address for this interface. Note that
980 * dual port cards actually come with three station
981 * addresses: one for each port, plus an extra. The
982 * extra one is used by the SysKonnect driver software
983 * as a 'virtual' station address for when both ports
984 * are operating in failover mode. Currently we don't
985 * use this extra address.
986 */
987 for (i = 0; i < ETHER_ADDR_LEN; i++)
988 sc_if->sk_enaddr[i] =
989 sk_win_read_1(sc, SK_MAC0_0 + (sa->skc_port * 8) + i);
990
991 aprint_normal(": Ethernet address %s\n",
992 ether_sprintf(sc_if->sk_enaddr));
993
994 /*
995 * Set up RAM buffer addresses. The NIC will have a certain
996 * amount of SRAM on it, somewhere between 512K and 2MB. We
997 * need to divide this up a) between the transmitter and
998 * receiver and b) between the two XMACs, if this is a
999 * dual port NIC. Our algorithm is to divide up the memory
1000 * evenly so that everyone gets a fair share.
1001 *
1002 * Just to be contrary, Yukon2 appears to have separate memory
1003 * for each MAC.
1004 */
1005 chunk = sc->sk_ramsize - (sc->sk_ramsize + 2) / 3;
1006 val = sc->sk_rboff / sizeof(u_int64_t);
1007 sc_if->sk_rx_ramstart = val;
1008 val += (chunk / sizeof(u_int64_t));
1009 sc_if->sk_rx_ramend = val - 1;
1010 chunk = sc->sk_ramsize - chunk;
1011 sc_if->sk_tx_ramstart = val;
1012 val += (chunk / sizeof(u_int64_t));
1013 sc_if->sk_tx_ramend = val - 1;
1014
1015 DPRINTFN(2, ("msk_attach: rx_ramstart=%#x rx_ramend=%#x\n"
1016 " tx_ramstart=%#x tx_ramend=%#x\n",
1017 sc_if->sk_rx_ramstart, sc_if->sk_rx_ramend,
1018 sc_if->sk_tx_ramstart, sc_if->sk_tx_ramend));
1019
1020 /* Allocate the descriptor queues. */
1021 if (bus_dmamem_alloc(sc->sc_dmatag, sizeof(struct msk_ring_data),
1022 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1023 aprint_error(": can't alloc rx buffers\n");
1024 goto fail;
1025 }
1026 if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
1027 sizeof(struct msk_ring_data), &kva, BUS_DMA_NOWAIT)) {
1028 aprint_error(": can't map dma buffers (%zu bytes)\n",
1029 sizeof(struct msk_ring_data));
1030 goto fail_1;
1031 }
1032 if (bus_dmamap_create(sc->sc_dmatag, sizeof(struct msk_ring_data), 1,
1033 sizeof(struct msk_ring_data), 0, BUS_DMA_NOWAIT,
1034 &sc_if->sk_ring_map)) {
1035 aprint_error(": can't create dma map\n");
1036 goto fail_2;
1037 }
1038 if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_ring_map, kva,
1039 sizeof(struct msk_ring_data), NULL, BUS_DMA_NOWAIT)) {
1040 aprint_error(": can't load dma map\n");
1041 goto fail_3;
1042 }
1043 sc_if->sk_rdata = (struct msk_ring_data *)kva;
1044 bzero(sc_if->sk_rdata, sizeof(struct msk_ring_data));
1045
1046 ifp = &sc_if->sk_ethercom.ec_if;
1047 /* Try to allocate memory for jumbo buffers. */
1048 if (msk_alloc_jumbo_mem(sc_if)) {
1049 aprint_error(": jumbo buffer allocation failed\n");
1050 goto fail_3;
1051 }
1052 sc_if->sk_ethercom.ec_capabilities = ETHERCAP_VLAN_MTU
1053 | ETHERCAP_JUMBO_MTU;
1054
1055 ifp->if_softc = sc_if;
1056 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1057 ifp->if_ioctl = msk_ioctl;
1058 ifp->if_start = msk_start;
1059 ifp->if_stop = msk_stop;
1060 ifp->if_init = msk_init;
1061 ifp->if_watchdog = msk_watchdog;
1062 ifp->if_baudrate = 1000000000;
1063 IFQ_SET_MAXLEN(&ifp->if_snd, MSK_TX_RING_CNT - 1);
1064 IFQ_SET_READY(&ifp->if_snd);
1065 strcpy(ifp->if_xname, sc_if->sk_dev.dv_xname);
1066
1067 /*
1068 * Do miibus setup.
1069 */
1070 msk_init_yukon(sc_if);
1071
1072 DPRINTFN(2, ("msk_attach: 1\n"));
1073
1074 sc_if->sk_mii.mii_ifp = ifp;
1075 sc_if->sk_mii.mii_readreg = msk_miibus_readreg;
1076 sc_if->sk_mii.mii_writereg = msk_miibus_writereg;
1077 sc_if->sk_mii.mii_statchg = msk_miibus_statchg;
1078
1079 ifmedia_init(&sc_if->sk_mii.mii_media, 0,
1080 msk_ifmedia_upd, msk_ifmedia_sts);
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);
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 caddr_t 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 bzero(sc->sk_status_ring,
1263 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
1264
1265 /* Reset the adapter. */
1266 msk_reset(sc);
1267
1268 skrs = sk_win_read_1(sc, SK_EPROM0);
1269 if (skrs == 0x00)
1270 sc->sk_ramsize = 0x20000;
1271 else
1272 sc->sk_ramsize = skrs * (1<<12);
1273 sc->sk_rboff = SK_RBOFF_0;
1274
1275 DPRINTFN(2, ("mskc_attach: ramsize=%d (%dk), rboff=%d\n",
1276 sc->sk_ramsize, sc->sk_ramsize / 1024,
1277 sc->sk_rboff));
1278
1279 switch (sc->sk_type) {
1280 case SK_YUKON_XL:
1281 sc->sk_name = "Yukon-2 XL";
1282 break;
1283 case SK_YUKON_EC_U:
1284 sc->sk_name = "Yukon-2 EC Ultra";
1285 break;
1286 case SK_YUKON_EC:
1287 sc->sk_name = "Yukon-2 EC";
1288 break;
1289 case SK_YUKON_FE:
1290 sc->sk_name = "Yukon-2 FE";
1291 break;
1292 default:
1293 sc->sk_name = "Yukon (Unknown)";
1294 }
1295
1296 if (sc->sk_type == SK_YUKON_XL) {
1297 switch (sc->sk_rev) {
1298 case SK_YUKON_XL_REV_A0:
1299 revstr = "A0";
1300 break;
1301 case SK_YUKON_XL_REV_A1:
1302 revstr = "A1";
1303 break;
1304 case SK_YUKON_XL_REV_A2:
1305 revstr = "A2";
1306 break;
1307 case SK_YUKON_XL_REV_A3:
1308 revstr = "A3";
1309 break;
1310 default:
1311 ;
1312 }
1313 }
1314
1315 if (sc->sk_type == SK_YUKON_EC) {
1316 switch (sc->sk_rev) {
1317 case SK_YUKON_EC_REV_A1:
1318 revstr = "A1";
1319 break;
1320 case SK_YUKON_EC_REV_A2:
1321 revstr = "A2";
1322 break;
1323 case SK_YUKON_EC_REV_A3:
1324 revstr = "A3";
1325 break;
1326 default:
1327 ;
1328 }
1329 }
1330
1331 if (sc->sk_type == SK_YUKON_EC_U) {
1332 switch (sc->sk_rev) {
1333 case SK_YUKON_EC_U_REV_A0:
1334 revstr = "A0";
1335 break;
1336 case SK_YUKON_EC_U_REV_A1:
1337 revstr = "A1";
1338 break;
1339 default:
1340 ;
1341 }
1342 }
1343
1344 /* Announce the product name. */
1345 aprint_normal(", %s", sc->sk_name);
1346 if (revstr != NULL)
1347 aprint_normal(" rev. %s", revstr);
1348 aprint_normal(" (0x%x): %s\n", sc->sk_rev, intrstr);
1349
1350 sc->sk_macs = 1;
1351
1352 hw = sk_win_read_1(sc, SK_Y2_HWRES);
1353 if ((hw & SK_Y2_HWRES_LINK_MASK) == SK_Y2_HWRES_LINK_DUAL) {
1354 if ((sk_win_read_1(sc, SK_Y2_CLKGATE) &
1355 SK_Y2_CLKGATE_LINK2_INACTIVE) == 0)
1356 sc->sk_macs++;
1357 }
1358
1359 skca.skc_port = SK_PORT_A;
1360 skca.skc_type = sc->sk_type;
1361 skca.skc_rev = sc->sk_rev;
1362 (void)config_found(&sc->sk_dev, &skca, mskcprint);
1363
1364 if (sc->sk_macs > 1) {
1365 skca.skc_port = SK_PORT_B;
1366 skca.skc_type = sc->sk_type;
1367 skca.skc_rev = sc->sk_rev;
1368 (void)config_found(&sc->sk_dev, &skca, mskcprint);
1369 }
1370
1371 /* Turn on the 'driver is loaded' LED. */
1372 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
1373
1374 /* skc sysctl setup */
1375
1376 sc->sk_int_mod = SK_IM_DEFAULT;
1377 sc->sk_int_mod_pending = 0;
1378
1379 if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1380 0, CTLTYPE_NODE, sc->sk_dev.dv_xname,
1381 SYSCTL_DESCR("mskc per-controller controls"),
1382 NULL, 0, NULL, 0, CTL_HW, msk_root_num, CTL_CREATE,
1383 CTL_EOL)) != 0) {
1384 aprint_normal("%s: couldn't create sysctl node\n",
1385 sc->sk_dev.dv_xname);
1386 goto fail_6;
1387 }
1388
1389 sk_nodenum = node->sysctl_num;
1390
1391 /* interrupt moderation time in usecs */
1392 if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1393 CTLFLAG_READWRITE,
1394 CTLTYPE_INT, "int_mod",
1395 SYSCTL_DESCR("msk interrupt moderation timer"),
1396 msk_sysctl_handler, 0, sc,
1397 0, CTL_HW, msk_root_num, sk_nodenum, CTL_CREATE,
1398 CTL_EOL)) != 0) {
1399 aprint_normal("%s: couldn't create int_mod sysctl node\n",
1400 sc->sk_dev.dv_xname);
1401 goto fail_6;
1402 }
1403
1404 return;
1405
1406 fail_6:
1407 bus_dmamap_unload(sc->sc_dmatag, sc->sk_status_map);
1408 fail_5:
1409 bus_dmamap_destroy(sc->sc_dmatag, sc->sk_status_map);
1410 fail_4:
1411 bus_dmamem_unmap(sc->sc_dmatag, kva,
1412 MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
1413 fail_3:
1414 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1415 fail_2:
1416 pci_intr_disestablish(pc, sc->sk_intrhand);
1417 fail_1:
1418 bus_space_unmap(sc->sk_btag, sc->sk_bhandle, size);
1419 }
1420
1421 int
1422 msk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, u_int32_t *txidx)
1423 {
1424 struct sk_softc *sc = sc_if->sk_softc;
1425 struct msk_tx_desc *f = NULL;
1426 u_int32_t frag, cur;
1427 int i;
1428 struct sk_txmap_entry *entry;
1429 bus_dmamap_t txmap;
1430
1431 DPRINTFN(2, ("msk_encap\n"));
1432
1433 entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head);
1434 if (entry == NULL) {
1435 DPRINTFN(2, ("msk_encap: no txmap available\n"));
1436 return (ENOBUFS);
1437 }
1438 txmap = entry->dmamap;
1439
1440 cur = frag = *txidx;
1441
1442 #ifdef MSK_DEBUG
1443 if (mskdebug >= 2)
1444 msk_dump_mbuf(m_head);
1445 #endif
1446
1447 /*
1448 * Start packing the mbufs in this chain into
1449 * the fragment pointers. Stop when we run out
1450 * of fragments or hit the end of the mbuf chain.
1451 */
1452 if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
1453 BUS_DMA_NOWAIT)) {
1454 DPRINTFN(2, ("msk_encap: dmamap failed\n"));
1455 return (ENOBUFS);
1456 }
1457
1458 if (txmap->dm_nsegs > (MSK_TX_RING_CNT - sc_if->sk_cdata.sk_tx_cnt - 2)) {
1459 DPRINTFN(2, ("msk_encap: too few descriptors free\n"));
1460 bus_dmamap_unload(sc->sc_dmatag, txmap);
1461 return (ENOBUFS);
1462 }
1463
1464 DPRINTFN(2, ("msk_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1465
1466 /* Sync the DMA map. */
1467 bus_dmamap_sync(sc->sc_dmatag, txmap, 0, txmap->dm_mapsize,
1468 BUS_DMASYNC_PREWRITE);
1469
1470 for (i = 0; i < txmap->dm_nsegs; i++) {
1471 f = &sc_if->sk_rdata->sk_tx_ring[frag];
1472 f->sk_addr = htole32(txmap->dm_segs[i].ds_addr);
1473 f->sk_len = htole16(txmap->dm_segs[i].ds_len);
1474 f->sk_ctl = 0;
1475 if (i == 0)
1476 f->sk_opcode = SK_Y2_TXOPC_PACKET;
1477 else
1478 f->sk_opcode = SK_Y2_TXOPC_BUFFER | SK_Y2_TXOPC_OWN;
1479 cur = frag;
1480 SK_INC(frag, MSK_TX_RING_CNT);
1481 }
1482
1483 sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
1484 SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
1485
1486 sc_if->sk_cdata.sk_tx_map[cur] = entry;
1487 sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |= SK_Y2_TXCTL_LASTFRAG;
1488
1489 /* Sync descriptors before handing to chip */
1490 MSK_CDTXSYNC(sc_if, *txidx, txmap->dm_nsegs,
1491 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1492
1493 sc_if->sk_rdata->sk_tx_ring[*txidx].sk_opcode |= SK_Y2_TXOPC_OWN;
1494
1495 /* Sync first descriptor to hand it off */
1496 MSK_CDTXSYNC(sc_if, *txidx, 1,
1497 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1498
1499 sc_if->sk_cdata.sk_tx_cnt += txmap->dm_nsegs;
1500
1501 #ifdef MSK_DEBUG
1502 if (mskdebug >= 2) {
1503 struct msk_tx_desc *le;
1504 u_int32_t idx;
1505 for (idx = *txidx; idx != frag; SK_INC(idx, MSK_TX_RING_CNT)) {
1506 le = &sc_if->sk_rdata->sk_tx_ring[idx];
1507 msk_dump_txdesc(le, idx);
1508 }
1509 }
1510 #endif
1511
1512 *txidx = frag;
1513
1514 DPRINTFN(2, ("msk_encap: completed successfully\n"));
1515
1516 return (0);
1517 }
1518
1519 void
1520 msk_start(struct ifnet *ifp)
1521 {
1522 struct sk_if_softc *sc_if = ifp->if_softc;
1523 struct mbuf *m_head = NULL;
1524 u_int32_t idx = sc_if->sk_cdata.sk_tx_prod;
1525 int pkts = 0;
1526
1527 DPRINTFN(2, ("msk_start\n"));
1528
1529 while (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
1530 IFQ_POLL(&ifp->if_snd, m_head);
1531 if (m_head == NULL)
1532 break;
1533
1534 /*
1535 * Pack the data into the transmit ring. If we
1536 * don't have room, set the OACTIVE flag and wait
1537 * for the NIC to drain the ring.
1538 */
1539 if (msk_encap(sc_if, m_head, &idx)) {
1540 ifp->if_flags |= IFF_OACTIVE;
1541 break;
1542 }
1543
1544 /* now we are committed to transmit the packet */
1545 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1546 pkts++;
1547
1548 /*
1549 * If there's a BPF listener, bounce a copy of this frame
1550 * to him.
1551 */
1552 #if NBPFILTER > 0
1553 if (ifp->if_bpf)
1554 bpf_mtap(ifp->if_bpf, m_head);
1555 #endif
1556 }
1557 if (pkts == 0)
1558 return;
1559
1560 /* Transmit */
1561 if (idx != sc_if->sk_cdata.sk_tx_prod) {
1562 sc_if->sk_cdata.sk_tx_prod = idx;
1563 SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_PREF_PUTIDX, idx);
1564
1565 /* Set a timeout in case the chip goes out to lunch. */
1566 ifp->if_timer = 5;
1567 }
1568 }
1569
1570 void
1571 msk_watchdog(struct ifnet *ifp)
1572 {
1573 struct sk_if_softc *sc_if = ifp->if_softc;
1574
1575 /*
1576 * Reclaim first as there is a possibility of losing Tx completion
1577 * interrupts.
1578 */
1579 msk_txeof(sc_if);
1580 if (sc_if->sk_cdata.sk_tx_cnt != 0) {
1581 aprint_error("%s: watchdog timeout\n", sc_if->sk_dev.dv_xname);
1582
1583 ifp->if_oerrors++;
1584
1585 /* XXX Resets both ports; we shouldn't do that. */
1586 msk_reset(sc_if->sk_softc);
1587 msk_init(ifp);
1588 }
1589 }
1590
1591 void
1592 mskc_shutdown(void *v)
1593 {
1594 struct sk_softc *sc = v;
1595
1596 DPRINTFN(2, ("msk_shutdown\n"));
1597
1598 /* Turn off the 'driver is loaded' LED. */
1599 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
1600
1601 msk_reset(sc);
1602 }
1603
1604 __inline int
1605 msk_rxvalid(struct sk_softc *sc, u_int32_t stat, u_int32_t len)
1606 {
1607 if ((stat & (YU_RXSTAT_CRCERR | YU_RXSTAT_LONGERR |
1608 YU_RXSTAT_MIIERR | YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC |
1609 YU_RXSTAT_JABBER)) != 0 ||
1610 (stat & YU_RXSTAT_RXOK) != YU_RXSTAT_RXOK ||
1611 YU_RXSTAT_BYTES(stat) != len)
1612 return (0);
1613
1614 return (1);
1615 }
1616
1617 void
1618 msk_rxeof(struct sk_if_softc *sc_if, u_int16_t len, u_int32_t rxstat)
1619 {
1620 struct sk_softc *sc = sc_if->sk_softc;
1621 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1622 struct mbuf *m;
1623 struct sk_chain *cur_rx;
1624 int cur, total_len = len;
1625 bus_dmamap_t dmamap;
1626
1627 DPRINTFN(2, ("msk_rxeof\n"));
1628
1629 cur = sc_if->sk_cdata.sk_rx_cons;
1630 SK_INC(sc_if->sk_cdata.sk_rx_cons, MSK_RX_RING_CNT);
1631 SK_INC(sc_if->sk_cdata.sk_rx_prod, MSK_RX_RING_CNT);
1632
1633 /* Sync the descriptor */
1634 MSK_CDRXSYNC(sc_if, cur, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1635
1636 cur_rx = &sc_if->sk_cdata.sk_rx_chain[cur];
1637 dmamap = sc_if->sk_cdata.sk_rx_jumbo_map;
1638
1639 bus_dmamap_sync(sc_if->sk_softc->sc_dmatag, dmamap, 0,
1640 dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1641
1642 m = cur_rx->sk_mbuf;
1643 cur_rx->sk_mbuf = NULL;
1644
1645 if (total_len < SK_MIN_FRAMELEN ||
1646 total_len > SK_JUMBO_FRAMELEN ||
1647 msk_rxvalid(sc, rxstat, total_len) == 0) {
1648 ifp->if_ierrors++;
1649 msk_newbuf(sc_if, cur, m, dmamap);
1650 return;
1651 }
1652
1653 /*
1654 * Try to allocate a new jumbo buffer. If that fails, copy the
1655 * packet to mbufs and put the jumbo buffer back in the ring
1656 * so it can be re-used. If allocating mbufs fails, then we
1657 * have to drop the packet.
1658 */
1659 if (msk_newbuf(sc_if, cur, NULL, dmamap) == ENOBUFS) {
1660 struct mbuf *m0;
1661 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1662 total_len + ETHER_ALIGN, 0, ifp, NULL);
1663 msk_newbuf(sc_if, cur, m, dmamap);
1664 if (m0 == NULL) {
1665 ifp->if_ierrors++;
1666 return;
1667 }
1668 m_adj(m0, ETHER_ALIGN);
1669 m = m0;
1670 } else {
1671 m->m_pkthdr.rcvif = ifp;
1672 m->m_pkthdr.len = m->m_len = total_len;
1673 }
1674
1675 ifp->if_ipackets++;
1676
1677 #if NBPFILTER > 0
1678 if (ifp->if_bpf)
1679 bpf_mtap(ifp->if_bpf, m);
1680 #endif
1681
1682 /* pass it on. */
1683 (*ifp->if_input)(ifp, m);
1684 }
1685
1686 void
1687 msk_txeof(struct sk_if_softc *sc_if)
1688 {
1689 struct sk_softc *sc = sc_if->sk_softc;
1690 struct msk_tx_desc *cur_tx;
1691 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1692 u_int32_t idx, reg, sk_ctl;
1693 struct sk_txmap_entry *entry;
1694
1695 DPRINTFN(2, ("msk_txeof\n"));
1696
1697 if (sc_if->sk_port == SK_PORT_A)
1698 reg = SK_STAT_BMU_TXA1_RIDX;
1699 else
1700 reg = SK_STAT_BMU_TXA2_RIDX;
1701
1702 /*
1703 * Go through our tx ring and free mbufs for those
1704 * frames that have been sent.
1705 */
1706 idx = sc_if->sk_cdata.sk_tx_cons;
1707 while (idx != sk_win_read_2(sc, reg)) {
1708 MSK_CDTXSYNC(sc_if, idx, 1,
1709 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1710
1711 cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
1712 sk_ctl = cur_tx->sk_ctl;
1713 #ifdef MSK_DEBUG
1714 if (mskdebug >= 2)
1715 msk_dump_txdesc(cur_tx, idx);
1716 #endif
1717 if (sk_ctl & SK_Y2_TXCTL_LASTFRAG)
1718 ifp->if_opackets++;
1719 if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
1720 entry = sc_if->sk_cdata.sk_tx_map[idx];
1721
1722 m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
1723 sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
1724
1725 bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
1726 entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1727
1728 bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
1729 SIMPLEQ_INSERT_TAIL(&sc_if->sk_txmap_head, entry,
1730 link);
1731 sc_if->sk_cdata.sk_tx_map[idx] = NULL;
1732 }
1733 sc_if->sk_cdata.sk_tx_cnt--;
1734 SK_INC(idx, MSK_TX_RING_CNT);
1735 }
1736 ifp->if_timer = sc_if->sk_cdata.sk_tx_cnt > 0 ? 5 : 0;
1737
1738 if (sc_if->sk_cdata.sk_tx_cnt < MSK_TX_RING_CNT - 2)
1739 ifp->if_flags &= ~IFF_OACTIVE;
1740
1741 sc_if->sk_cdata.sk_tx_cons = idx;
1742 }
1743
1744 void
1745 msk_tick(void *xsc_if)
1746 {
1747 struct sk_if_softc *sc_if = xsc_if;
1748 struct mii_data *mii = &sc_if->sk_mii;
1749
1750 mii_tick(mii);
1751 callout_schedule(&sc_if->sk_tick_ch, hz);
1752 }
1753
1754 void
1755 msk_intr_yukon(struct sk_if_softc *sc_if)
1756 {
1757 u_int8_t status;
1758
1759 status = SK_IF_READ_1(sc_if, 0, SK_GMAC_ISR);
1760 /* RX overrun */
1761 if ((status & SK_GMAC_INT_RX_OVER) != 0) {
1762 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST,
1763 SK_RFCTL_RX_FIFO_OVER);
1764 }
1765 /* TX underrun */
1766 if ((status & SK_GMAC_INT_TX_UNDER) != 0) {
1767 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST,
1768 SK_TFCTL_TX_FIFO_UNDER);
1769 }
1770
1771 DPRINTFN(2, ("msk_intr_yukon status=%#x\n", status));
1772 }
1773
1774 int
1775 msk_intr(void *xsc)
1776 {
1777 struct sk_softc *sc = xsc;
1778 struct sk_if_softc *sc_if0 = sc->sk_if[SK_PORT_A];
1779 struct sk_if_softc *sc_if1 = sc->sk_if[SK_PORT_B];
1780 struct ifnet *ifp0 = NULL, *ifp1 = NULL;
1781 int claimed = 0;
1782 u_int32_t status;
1783 struct msk_status_desc *cur_st;
1784
1785 status = CSR_READ_4(sc, SK_Y2_ISSR2);
1786 if (status == 0) {
1787 CSR_WRITE_4(sc, SK_Y2_ICR, 2);
1788 return (0);
1789 }
1790
1791 status = CSR_READ_4(sc, SK_ISR);
1792
1793 if (sc_if0 != NULL)
1794 ifp0 = &sc_if0->sk_ethercom.ec_if;
1795 if (sc_if1 != NULL)
1796 ifp1 = &sc_if1->sk_ethercom.ec_if;
1797
1798 if (sc_if0 && (status & SK_Y2_IMR_MAC1) &&
1799 (ifp0->if_flags & IFF_RUNNING)) {
1800 msk_intr_yukon(sc_if0);
1801 }
1802
1803 if (sc_if1 && (status & SK_Y2_IMR_MAC2) &&
1804 (ifp1->if_flags & IFF_RUNNING)) {
1805 msk_intr_yukon(sc_if1);
1806 }
1807
1808 MSK_CDSTSYNC(sc, sc->sk_status_idx,
1809 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1810 cur_st = &sc->sk_status_ring[sc->sk_status_idx];
1811
1812 while (cur_st->sk_opcode & SK_Y2_STOPC_OWN) {
1813 cur_st->sk_opcode &= ~SK_Y2_STOPC_OWN;
1814 switch (cur_st->sk_opcode) {
1815 case SK_Y2_STOPC_RXSTAT:
1816 msk_rxeof(sc->sk_if[cur_st->sk_link],
1817 letoh16(cur_st->sk_len),
1818 letoh32(cur_st->sk_status));
1819 SK_IF_WRITE_2(sc->sk_if[cur_st->sk_link], 0,
1820 SK_RXQ1_Y2_PREF_PUTIDX,
1821 sc->sk_if[cur_st->sk_link]->sk_cdata.sk_rx_prod);
1822 break;
1823 case SK_Y2_STOPC_TXSTAT:
1824 if (sc_if0)
1825 msk_txeof(sc_if0);
1826 if (sc_if1)
1827 msk_txeof(sc_if1);
1828 break;
1829 default:
1830 aprint_error("opcode=0x%x\n", cur_st->sk_opcode);
1831 break;
1832 }
1833 SK_INC(sc->sk_status_idx, MSK_STATUS_RING_CNT);
1834
1835 MSK_CDSTSYNC(sc, sc->sk_status_idx,
1836 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1837 cur_st = &sc->sk_status_ring[sc->sk_status_idx];
1838 }
1839
1840 if (status & SK_Y2_IMR_BMU) {
1841 CSR_WRITE_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_IRQ_CLEAR);
1842 claimed = 1;
1843 }
1844
1845 CSR_WRITE_4(sc, SK_Y2_ICR, 2);
1846
1847 if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
1848 msk_start(ifp0);
1849 if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
1850 msk_start(ifp1);
1851
1852 #if NRND > 0
1853 if (RND_ENABLED(&sc->rnd_source))
1854 rnd_add_uint32(&sc->rnd_source, status);
1855 #endif
1856
1857 if (sc->sk_int_mod_pending)
1858 msk_update_int_mod(sc);
1859
1860 return claimed;
1861 }
1862
1863 void
1864 msk_init_yukon(struct sk_if_softc *sc_if)
1865 {
1866 u_int32_t v;
1867 u_int16_t reg;
1868 struct sk_softc *sc;
1869 int i;
1870
1871 sc = sc_if->sk_softc;
1872
1873 DPRINTFN(2, ("msk_init_yukon: start: sk_csr=%#x\n",
1874 CSR_READ_4(sc_if->sk_softc, SK_CSR)));
1875
1876 DPRINTFN(6, ("msk_init_yukon: 1\n"));
1877
1878 /* GMAC and GPHY Reset */
1879 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
1880 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
1881 DELAY(1000);
1882
1883 DPRINTFN(6, ("msk_init_yukon: 2\n"));
1884
1885 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_CLEAR);
1886 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
1887 SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
1888
1889 DPRINTFN(3, ("msk_init_yukon: gmac_ctrl=%#x\n",
1890 SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
1891
1892 DPRINTFN(6, ("msk_init_yukon: 3\n"));
1893
1894 /* unused read of the interrupt source register */
1895 DPRINTFN(6, ("msk_init_yukon: 4\n"));
1896 SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
1897
1898 DPRINTFN(6, ("msk_init_yukon: 4a\n"));
1899 reg = SK_YU_READ_2(sc_if, YUKON_PAR);
1900 DPRINTFN(6, ("msk_init_yukon: YUKON_PAR=%#x\n", reg));
1901
1902 /* MIB Counter Clear Mode set */
1903 reg |= YU_PAR_MIB_CLR;
1904 DPRINTFN(6, ("msk_init_yukon: YUKON_PAR=%#x\n", reg));
1905 DPRINTFN(6, ("msk_init_yukon: 4b\n"));
1906 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
1907
1908 /* MIB Counter Clear Mode clear */
1909 DPRINTFN(6, ("msk_init_yukon: 5\n"));
1910 reg &= ~YU_PAR_MIB_CLR;
1911 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
1912
1913 /* receive control reg */
1914 DPRINTFN(6, ("msk_init_yukon: 7\n"));
1915 SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_CRCR);
1916
1917 /* transmit parameter register */
1918 DPRINTFN(6, ("msk_init_yukon: 8\n"));
1919 SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
1920 YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) );
1921
1922 /* serial mode register */
1923 DPRINTFN(6, ("msk_init_yukon: 9\n"));
1924 reg = YU_SMR_DATA_BLIND(0x1c) |
1925 YU_SMR_MFL_VLAN |
1926 YU_SMR_IPG_DATA(0x1e);
1927
1928 if (sc->sk_type != SK_YUKON_FE)
1929 reg |= YU_SMR_MFL_JUMBO;
1930
1931 SK_YU_WRITE_2(sc_if, YUKON_SMR, reg);
1932
1933 DPRINTFN(6, ("msk_init_yukon: 10\n"));
1934 /* Setup Yukon's address */
1935 for (i = 0; i < 3; i++) {
1936 /* Write Source Address 1 (unicast filter) */
1937 SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
1938 sc_if->sk_enaddr[i * 2] |
1939 sc_if->sk_enaddr[i * 2 + 1] << 8);
1940 }
1941
1942 for (i = 0; i < 3; i++) {
1943 reg = sk_win_read_2(sc_if->sk_softc,
1944 SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
1945 SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
1946 }
1947
1948 /* Set promiscuous mode */
1949 msk_setpromisc(sc_if);
1950
1951 /* Set multicast filter */
1952 DPRINTFN(6, ("msk_init_yukon: 11\n"));
1953 msk_setmulti(sc_if);
1954
1955 /* enable interrupt mask for counter overflows */
1956 DPRINTFN(6, ("msk_init_yukon: 12\n"));
1957 SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
1958 SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
1959 SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
1960
1961 /* Configure RX MAC FIFO Flush Mask */
1962 v = YU_RXSTAT_FOFL | YU_RXSTAT_CRCERR | YU_RXSTAT_MIIERR |
1963 YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC | YU_RXSTAT_RUNT |
1964 YU_RXSTAT_JABBER;
1965 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_MASK, v);
1966
1967 /* Disable RX MAC FIFO Flush for YUKON-Lite Rev. A0 only */
1968 if (sc->sk_type == SK_YUKON_LITE && sc->sk_rev == SK_YUKON_LITE_REV_A0)
1969 v = SK_TFCTL_OPERATION_ON;
1970 else
1971 v = SK_TFCTL_OPERATION_ON | SK_RFCTL_FIFO_FLUSH_ON;
1972 /* Configure RX MAC FIFO */
1973 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
1974 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_CTRL_TEST, v);
1975
1976 /* Increase flush threshould to 64 bytes */
1977 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_THRESHOLD,
1978 SK_RFCTL_FIFO_THRESHOLD + 1);
1979
1980 /* Configure TX MAC FIFO */
1981 SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
1982 SK_IF_WRITE_2(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
1983
1984 #if 1
1985 SK_YU_WRITE_2(sc_if, YUKON_GPCR, YU_GPCR_TXEN | YU_GPCR_RXEN);
1986 #endif
1987 DPRINTFN(6, ("msk_init_yukon: end\n"));
1988 }
1989
1990 /*
1991 * Note that to properly initialize any part of the GEnesis chip,
1992 * you first have to take it out of reset mode.
1993 */
1994 int
1995 msk_init(struct ifnet *ifp)
1996 {
1997 struct sk_if_softc *sc_if = ifp->if_softc;
1998 struct sk_softc *sc = sc_if->sk_softc;
1999 struct mii_data *mii = &sc_if->sk_mii;
2000 int s;
2001 uint32_t imr, imtimer_ticks;
2002
2003
2004 DPRINTFN(2, ("msk_init\n"));
2005
2006 s = splnet();
2007
2008 /* Cancel pending I/O and free all RX/TX buffers. */
2009 msk_stop(ifp,0);
2010
2011 /* Configure I2C registers */
2012
2013 /* Configure XMAC(s) */
2014 msk_init_yukon(sc_if);
2015 mii_mediachg(mii);
2016
2017 /* Configure transmit arbiter(s) */
2018 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_ON);
2019 #if 0
2020 SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
2021 #endif
2022
2023 /* Configure RAMbuffers */
2024 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
2025 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
2026 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
2027 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
2028 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
2029 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
2030
2031 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_UNRESET);
2032 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_STORENFWD_ON);
2033 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_START, sc_if->sk_tx_ramstart);
2034 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_WR_PTR, sc_if->sk_tx_ramstart);
2035 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_RD_PTR, sc_if->sk_tx_ramstart);
2036 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_END, sc_if->sk_tx_ramend);
2037 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_ON);
2038
2039 /* Configure BMUs */
2040 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000016);
2041 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000d28);
2042 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000080);
2043 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_WATERMARK, 0x00000600);
2044
2045 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000016);
2046 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000d28);
2047 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000080);
2048 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_WATERMARK, 0x00000600);
2049
2050 /* Make sure the sync transmit queue is disabled. */
2051 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET);
2052
2053 /* Init descriptors */
2054 if (msk_init_rx_ring(sc_if) == ENOBUFS) {
2055 aprint_error("%s: initialization failed: no "
2056 "memory for rx buffers\n", sc_if->sk_dev.dv_xname);
2057 msk_stop(ifp,0);
2058 splx(s);
2059 return ENOBUFS;
2060 }
2061
2062 if (msk_init_tx_ring(sc_if) == ENOBUFS) {
2063 aprint_error("%s: initialization failed: no "
2064 "memory for tx buffers\n", sc_if->sk_dev.dv_xname);
2065 msk_stop(ifp,0);
2066 splx(s);
2067 return ENOBUFS;
2068 }
2069
2070 /* Set interrupt moderation if changed via sysctl. */
2071 switch (sc->sk_type) {
2072 case SK_YUKON_EC:
2073 imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
2074 break;
2075 default:
2076 imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
2077 }
2078 imr = sk_win_read_4(sc, SK_IMTIMERINIT);
2079 if (imr != SK_IM_USECS(sc->sk_int_mod)) {
2080 sk_win_write_4(sc, SK_IMTIMERINIT,
2081 SK_IM_USECS(sc->sk_int_mod));
2082 aprint_verbose("%s: interrupt moderation is %d us\n",
2083 sc->sk_dev.dv_xname, sc->sk_int_mod);
2084 }
2085
2086 /* Initialize prefetch engine. */
2087 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000001);
2088 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000002);
2089 SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_LIDX, MSK_RX_RING_CNT - 1);
2090 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_ADDRLO,
2091 MSK_RX_RING_ADDR(sc_if, 0));
2092 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_ADDRHI,
2093 (u_int64_t)MSK_RX_RING_ADDR(sc_if, 0) >> 32);
2094 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000008);
2095 SK_IF_READ_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR);
2096
2097 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000001);
2098 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000002);
2099 SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_PREF_LIDX, MSK_TX_RING_CNT - 1);
2100 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_ADDRLO,
2101 MSK_TX_RING_ADDR(sc_if, 0));
2102 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_ADDRHI,
2103 (u_int64_t)MSK_TX_RING_ADDR(sc_if, 0) >> 32);
2104 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000008);
2105 SK_IF_READ_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR);
2106
2107 SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_PUTIDX,
2108 sc_if->sk_cdata.sk_rx_prod);
2109
2110 /* Configure interrupt handling */
2111 if (sc_if->sk_port == SK_PORT_A)
2112 sc->sk_intrmask |= SK_Y2_INTRS1;
2113 else
2114 sc->sk_intrmask |= SK_Y2_INTRS2;
2115 sc->sk_intrmask |= SK_Y2_IMR_BMU;
2116 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2117
2118 ifp->if_flags |= IFF_RUNNING;
2119 ifp->if_flags &= ~IFF_OACTIVE;
2120
2121 callout_schedule(&sc_if->sk_tick_ch, hz);
2122
2123 splx(s);
2124 return 0;
2125 }
2126
2127 void
2128 msk_stop(struct ifnet *ifp, int disable)
2129 {
2130 struct sk_if_softc *sc_if = ifp->if_softc;
2131 struct sk_softc *sc = sc_if->sk_softc;
2132 struct sk_txmap_entry *dma;
2133 int i;
2134
2135 DPRINTFN(2, ("msk_stop\n"));
2136
2137 callout_stop(&sc_if->sk_tick_ch);
2138
2139 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2140
2141 /* Stop transfer of Tx descriptors */
2142
2143 /* Stop transfer of Rx descriptors */
2144
2145 /* Turn off various components of this interface. */
2146 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2147 SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
2148 SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
2149 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
2150 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2151 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, SK_TXBMU_OFFLINE);
2152 SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2153 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
2154 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2155 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_TXLEDCTL_COUNTER_STOP);
2156 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
2157 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
2158
2159 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000001);
2160 SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000001);
2161
2162 /* Disable interrupts */
2163 if (sc_if->sk_port == SK_PORT_A)
2164 sc->sk_intrmask &= ~SK_Y2_INTRS1;
2165 else
2166 sc->sk_intrmask &= ~SK_Y2_INTRS2;
2167 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2168
2169 SK_XM_READ_2(sc_if, XM_ISR);
2170 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2171
2172 /* Free RX and TX mbufs still in the queues. */
2173 for (i = 0; i < MSK_RX_RING_CNT; i++) {
2174 if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
2175 m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
2176 sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
2177 }
2178 }
2179
2180 for (i = 0; i < MSK_TX_RING_CNT; i++) {
2181 if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
2182 m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
2183 sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
2184 #if 1
2185 SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head,
2186 sc_if->sk_cdata.sk_tx_map[i], link);
2187 sc_if->sk_cdata.sk_tx_map[i] = 0;
2188 #endif
2189 }
2190 }
2191
2192 #if 1
2193 while ((dma = SIMPLEQ_FIRST(&sc_if->sk_txmap_head))) {
2194 SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
2195 bus_dmamap_destroy(sc->sc_dmatag, dma->dmamap);
2196 free(dma, M_DEVBUF);
2197 }
2198 #endif
2199 }
2200
2201 CFATTACH_DECL(mskc, sizeof(struct sk_softc), mskc_probe, mskc_attach,
2202 NULL, NULL);
2203
2204 CFATTACH_DECL(msk, sizeof(struct sk_if_softc), msk_probe, msk_attach,
2205 NULL, NULL);
2206
2207 #ifdef MSK_DEBUG
2208 void
2209 msk_dump_txdesc(struct msk_tx_desc *le, int idx)
2210 {
2211 #define DESC_PRINT(X) \
2212 if (X) \
2213 printf("txdesc[%d]." #X "=%#x\n", \
2214 idx, X);
2215
2216 DESC_PRINT(letoh32(le->sk_addr));
2217 DESC_PRINT(letoh16(le->sk_len));
2218 DESC_PRINT(le->sk_ctl);
2219 DESC_PRINT(le->sk_opcode);
2220 #undef DESC_PRINT
2221 }
2222
2223 void
2224 msk_dump_bytes(const char *data, int len)
2225 {
2226 int c, i, j;
2227
2228 for (i = 0; i < len; i += 16) {
2229 printf("%08x ", i);
2230 c = len - i;
2231 if (c > 16) c = 16;
2232
2233 for (j = 0; j < c; j++) {
2234 printf("%02x ", data[i + j] & 0xff);
2235 if ((j & 0xf) == 7 && j > 0)
2236 printf(" ");
2237 }
2238
2239 for (; j < 16; j++)
2240 printf(" ");
2241 printf(" ");
2242
2243 for (j = 0; j < c; j++) {
2244 int ch = data[i + j] & 0xff;
2245 printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
2246 }
2247
2248 printf("\n");
2249
2250 if (c < 16)
2251 break;
2252 }
2253 }
2254
2255 void
2256 msk_dump_mbuf(struct mbuf *m)
2257 {
2258 int count = m->m_pkthdr.len;
2259
2260 printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
2261
2262 while (count > 0 && m) {
2263 printf("m=%p, m->m_data=%p, m->m_len=%d\n",
2264 m, m->m_data, m->m_len);
2265 msk_dump_bytes(mtod(m, char *), m->m_len);
2266
2267 count -= m->m_len;
2268 m = m->m_next;
2269 }
2270 }
2271 #endif
2272
2273 static int
2274 msk_sysctl_handler(SYSCTLFN_ARGS)
2275 {
2276 int error, t;
2277 struct sysctlnode node;
2278 struct sk_softc *sc;
2279
2280 node = *rnode;
2281 sc = node.sysctl_data;
2282 t = sc->sk_int_mod;
2283 node.sysctl_data = &t;
2284 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2285 if (error || newp == NULL)
2286 return error;
2287
2288 if (t < SK_IM_MIN || t > SK_IM_MAX)
2289 return EINVAL;
2290
2291 /* update the softc with sysctl-changed value, and mark
2292 for hardware update */
2293 sc->sk_int_mod = t;
2294 sc->sk_int_mod_pending = 1;
2295 return 0;
2296 }
2297
2298 /*
2299 * Set up sysctl(3) MIB, hw.sk.* - Individual controllers will be
2300 * set up in skc_attach()
2301 */
2302 SYSCTL_SETUP(sysctl_msk, "sysctl msk subtree setup")
2303 {
2304 int rc;
2305 const struct sysctlnode *node;
2306
2307 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
2308 0, CTLTYPE_NODE, "hw", NULL,
2309 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
2310 goto err;
2311 }
2312
2313 if ((rc = sysctl_createv(clog, 0, NULL, &node,
2314 0, CTLTYPE_NODE, "msk",
2315 SYSCTL_DESCR("msk interface controls"),
2316 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
2317 goto err;
2318 }
2319
2320 msk_root_num = node->sysctl_num;
2321 return;
2322
2323 err:
2324 aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
2325 }
2326