if_sk.c revision 1.27 1 /* $NetBSD: if_sk.c,v 1.27 2006/08/20 16:03:30 riz Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
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 THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /* $OpenBSD: if_sk.c,v 1.33 2003/08/12 05:23:06 nate Exp $ */
37
38 /*
39 * Copyright (c) 1997, 1998, 1999, 2000
40 * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by Bill Paul.
53 * 4. Neither the name of the author nor the names of any co-contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
61 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
67 * THE POSSIBILITY OF SUCH DAMAGE.
68 *
69 * $FreeBSD: /c/ncvs/src/sys/pci/if_sk.c,v 1.20 2000/04/22 02:16:37 wpaul Exp $
70 */
71
72 /*
73 * Copyright (c) 2003 Nathan L. Binkert <binkertn (at) umich.edu>
74 *
75 * Permission to use, copy, modify, and distribute this software for any
76 * purpose with or without fee is hereby granted, provided that the above
77 * copyright notice and this permission notice appear in all copies.
78 *
79 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
80 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
81 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
82 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
83 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
84 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
85 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
86 */
87
88 /*
89 * SysKonnect SK-NET gigabit ethernet driver for FreeBSD. Supports
90 * the SK-984x series adapters, both single port and dual port.
91 * References:
92 * The XaQti XMAC II datasheet,
93 * http://www.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
94 * The SysKonnect GEnesis manual, http://www.syskonnect.com
95 *
96 * Note: XaQti has been acquired by Vitesse, and Vitesse does not have the
97 * XMAC II datasheet online. I have put my copy at people.freebsd.org as a
98 * convenience to others until Vitesse corrects this problem:
99 *
100 * http://people.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
101 *
102 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
103 * Department of Electrical Engineering
104 * Columbia University, New York City
105 */
106
107 /*
108 * The SysKonnect gigabit ethernet adapters consist of two main
109 * components: the SysKonnect GEnesis controller chip and the XaQti Corp.
110 * XMAC II gigabit ethernet MAC. The XMAC provides all of the MAC
111 * components and a PHY while the GEnesis controller provides a PCI
112 * interface with DMA support. Each card may have between 512K and
113 * 2MB of SRAM on board depending on the configuration.
114 *
115 * The SysKonnect GEnesis controller can have either one or two XMAC
116 * chips connected to it, allowing single or dual port NIC configurations.
117 * SysKonnect has the distinction of being the only vendor on the market
118 * with a dual port gigabit ethernet NIC. The GEnesis provides dual FIFOs,
119 * dual DMA queues, packet/MAC/transmit arbiters and direct access to the
120 * XMAC registers. This driver takes advantage of these features to allow
121 * both XMACs to operate as independent interfaces.
122 */
123
124 #include "bpfilter.h"
125 #include "rnd.h"
126
127 #include <sys/param.h>
128 #include <sys/systm.h>
129 #include <sys/sockio.h>
130 #include <sys/mbuf.h>
131 #include <sys/malloc.h>
132 #include <sys/kernel.h>
133 #include <sys/socket.h>
134 #include <sys/device.h>
135 #include <sys/queue.h>
136 #include <sys/callout.h>
137 #include <sys/sysctl.h>
138
139 #include <net/if.h>
140 #include <net/if_dl.h>
141 #include <net/if_types.h>
142
143 #ifdef INET
144 #include <netinet/in.h>
145 #include <netinet/in_systm.h>
146 #include <netinet/in_var.h>
147 #include <netinet/ip.h>
148 #include <netinet/if_ether.h>
149 #endif
150
151 #include <net/if_media.h>
152
153 #if NBPFILTER > 0
154 #include <net/bpf.h>
155 #endif
156 #if NRND > 0
157 #include <sys/rnd.h>
158 #endif
159
160 #include <dev/mii/mii.h>
161 #include <dev/mii/miivar.h>
162 #include <dev/mii/brgphyreg.h>
163
164 #include <dev/pci/pcireg.h>
165 #include <dev/pci/pcivar.h>
166 #include <dev/pci/pcidevs.h>
167
168 #define SK_VERBOSE
169 /* #define SK_USEIOSPACE */
170
171 #include <dev/pci/if_skreg.h>
172 #include <dev/pci/if_skvar.h>
173
174 int skc_probe(struct device *, struct cfdata *, void *);
175 void skc_attach(struct device *, struct device *self, void *aux);
176 int sk_probe(struct device *, struct cfdata *, void *);
177 void sk_attach(struct device *, struct device *self, void *aux);
178 int skcprint(void *, const char *);
179 int sk_intr(void *);
180 void sk_intr_bcom(struct sk_if_softc *);
181 void sk_intr_xmac(struct sk_if_softc *);
182 void sk_intr_yukon(struct sk_if_softc *);
183 void sk_rxeof(struct sk_if_softc *);
184 void sk_txeof(struct sk_if_softc *);
185 int sk_encap(struct sk_if_softc *, struct mbuf *, u_int32_t *);
186 void sk_start(struct ifnet *);
187 int sk_ioctl(struct ifnet *, u_long, caddr_t);
188 int sk_init(struct ifnet *);
189 void sk_init_xmac(struct sk_if_softc *);
190 void sk_init_yukon(struct sk_if_softc *);
191 void sk_stop(struct ifnet *, int);
192 void sk_watchdog(struct ifnet *);
193 void sk_shutdown(void *);
194 int sk_ifmedia_upd(struct ifnet *);
195 void sk_ifmedia_sts(struct ifnet *, struct ifmediareq *);
196 void sk_reset(struct sk_softc *);
197 int sk_newbuf(struct sk_if_softc *, int, struct mbuf *, bus_dmamap_t);
198 int sk_alloc_jumbo_mem(struct sk_if_softc *);
199 void sk_free_jumbo_mem(struct sk_if_softc *);
200 void *sk_jalloc(struct sk_if_softc *);
201 void sk_jfree(struct mbuf *, caddr_t, size_t, void *);
202 int sk_init_rx_ring(struct sk_if_softc *);
203 int sk_init_tx_ring(struct sk_if_softc *);
204 u_int8_t sk_vpd_readbyte(struct sk_softc *, int);
205 void sk_vpd_read_res(struct sk_softc *,
206 struct vpd_res *, int);
207 void sk_vpd_read(struct sk_softc *);
208
209 void sk_update_int_mod(struct sk_softc *);
210
211 int sk_xmac_miibus_readreg(struct device *, int, int);
212 void sk_xmac_miibus_writereg(struct device *, int, int, int);
213 void sk_xmac_miibus_statchg(struct device *);
214
215 int sk_marv_miibus_readreg(struct device *, int, int);
216 void sk_marv_miibus_writereg(struct device *, int, int, int);
217 void sk_marv_miibus_statchg(struct device *);
218
219 u_int32_t sk_xmac_hash(caddr_t);
220 u_int32_t sk_yukon_hash(caddr_t);
221 void sk_setfilt(struct sk_if_softc *, caddr_t, int);
222 void sk_setmulti(struct sk_if_softc *);
223 void sk_tick(void *);
224
225 /* #define SK_DEBUG 2 */
226 #ifdef SK_DEBUG
227 #define DPRINTF(x) if (skdebug) printf x
228 #define DPRINTFN(n,x) if (skdebug >= (n)) printf x
229 int skdebug = SK_DEBUG;
230
231 void sk_dump_txdesc(struct sk_tx_desc *, int);
232 void sk_dump_mbuf(struct mbuf *);
233 void sk_dump_bytes(const char *, int);
234 #else
235 #define DPRINTF(x)
236 #define DPRINTFN(n,x)
237 #endif
238
239 #define SK_SETBIT(sc, reg, x) \
240 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x)
241
242 #define SK_CLRBIT(sc, reg, x) \
243 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x)
244
245 #define SK_WIN_SETBIT_4(sc, reg, x) \
246 sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) | x)
247
248 #define SK_WIN_CLRBIT_4(sc, reg, x) \
249 sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) & ~x)
250
251 #define SK_WIN_SETBIT_2(sc, reg, x) \
252 sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) | x)
253
254 #define SK_WIN_CLRBIT_2(sc, reg, x) \
255 sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) & ~x)
256
257 static int sk_sysctl_handler(SYSCTLFN_PROTO);
258 static int sk_root_num;
259
260 /* supported device vendors */
261 static const struct sk_product {
262 pci_vendor_id_t sk_vendor;
263 pci_product_id_t sk_product;
264 } sk_products[] = {
265 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C940, },
266 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE530T, },
267 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE560T, },
268 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE560T_2, },
269 { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1064, },
270 { PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE, },
271 { PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2, },
272 { PCI_VENDOR_GALILEO, PCI_PRODUCT_GALILEO_SKNET, },
273 { PCI_VENDOR_GALILEO, PCI_PRODUCT_GALILEO_BELKIN, },
274 { 0, 0, }
275 };
276
277 #define SK_LINKSYS_EG1032_SUBID 0x00151737
278
279 static inline u_int32_t
280 sk_win_read_4(struct sk_softc *sc, u_int32_t reg)
281 {
282 #ifdef SK_USEIOSPACE
283 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
284 return CSR_READ_4(sc, SK_WIN_BASE + SK_REG(reg));
285 #else
286 return CSR_READ_4(sc, reg);
287 #endif
288 }
289
290 static inline u_int16_t
291 sk_win_read_2(struct sk_softc *sc, u_int32_t reg)
292 {
293 #ifdef SK_USEIOSPACE
294 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
295 return CSR_READ_2(sc, SK_WIN_BASE + SK_REG(reg));
296 #else
297 return CSR_READ_2(sc, reg);
298 #endif
299 }
300
301 static inline u_int8_t
302 sk_win_read_1(struct sk_softc *sc, u_int32_t reg)
303 {
304 #ifdef SK_USEIOSPACE
305 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
306 return CSR_READ_1(sc, SK_WIN_BASE + SK_REG(reg));
307 #else
308 return CSR_READ_1(sc, reg);
309 #endif
310 }
311
312 static inline void
313 sk_win_write_4(struct sk_softc *sc, u_int32_t reg, u_int32_t x)
314 {
315 #ifdef SK_USEIOSPACE
316 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
317 CSR_WRITE_4(sc, SK_WIN_BASE + SK_REG(reg), x);
318 #else
319 CSR_WRITE_4(sc, reg, x);
320 #endif
321 }
322
323 static inline void
324 sk_win_write_2(struct sk_softc *sc, u_int32_t reg, u_int16_t x)
325 {
326 #ifdef SK_USEIOSPACE
327 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
328 CSR_WRITE_2(sc, SK_WIN_BASE + SK_REG(reg), x);
329 #else
330 CSR_WRITE_2(sc, reg, x);
331 #endif
332 }
333
334 static inline void
335 sk_win_write_1(struct sk_softc *sc, u_int32_t reg, u_int8_t x)
336 {
337 #ifdef SK_USEIOSPACE
338 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
339 CSR_WRITE_1(sc, SK_WIN_BASE + SK_REG(reg), x);
340 #else
341 CSR_WRITE_1(sc, reg, x);
342 #endif
343 }
344
345 /*
346 * The VPD EEPROM contains Vital Product Data, as suggested in
347 * the PCI 2.1 specification. The VPD data is separared into areas
348 * denoted by resource IDs. The SysKonnect VPD contains an ID string
349 * resource (the name of the adapter), a read-only area resource
350 * containing various key/data fields and a read/write area which
351 * can be used to store asset management information or log messages.
352 * We read the ID string and read-only into buffers attached to
353 * the controller softc structure for later use. At the moment,
354 * we only use the ID string during sk_attach().
355 */
356 u_int8_t
357 sk_vpd_readbyte(struct sk_softc *sc, int addr)
358 {
359 int i;
360
361 sk_win_write_2(sc, SK_PCI_REG(SK_PCI_VPD_ADDR), addr);
362 for (i = 0; i < SK_TIMEOUT; i++) {
363 DELAY(1);
364 if (sk_win_read_2(sc,
365 SK_PCI_REG(SK_PCI_VPD_ADDR)) & SK_VPD_FLAG)
366 break;
367 }
368
369 if (i == SK_TIMEOUT)
370 return(0);
371
372 return(sk_win_read_1(sc, SK_PCI_REG(SK_PCI_VPD_DATA)));
373 }
374
375 void
376 sk_vpd_read_res(struct sk_softc *sc, struct vpd_res *res, int addr)
377 {
378 int i;
379 u_int8_t *ptr;
380
381 ptr = (u_int8_t *)res;
382 for (i = 0; i < sizeof(struct vpd_res); i++)
383 ptr[i] = sk_vpd_readbyte(sc, i + addr);
384 }
385
386 void
387 sk_vpd_read(struct sk_softc *sc)
388 {
389 int pos = 0, i;
390 struct vpd_res res;
391
392 if (sc->sk_vpd_prodname != NULL)
393 free(sc->sk_vpd_prodname, M_DEVBUF);
394 if (sc->sk_vpd_readonly != NULL)
395 free(sc->sk_vpd_readonly, M_DEVBUF);
396 sc->sk_vpd_prodname = NULL;
397 sc->sk_vpd_readonly = NULL;
398
399 sk_vpd_read_res(sc, &res, pos);
400
401 if (res.vr_id != VPD_RES_ID) {
402 printf("%s: bad VPD resource id: expected %x got %x\n",
403 sc->sk_dev.dv_xname, VPD_RES_ID, res.vr_id);
404 return;
405 }
406
407 pos += sizeof(res);
408 sc->sk_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
409 if (sc->sk_vpd_prodname == NULL)
410 panic("sk_vpd_read");
411 for (i = 0; i < res.vr_len; i++)
412 sc->sk_vpd_prodname[i] = sk_vpd_readbyte(sc, i + pos);
413 sc->sk_vpd_prodname[i] = '\0';
414 pos += i;
415
416 sk_vpd_read_res(sc, &res, pos);
417
418 if (res.vr_id != VPD_RES_READ) {
419 printf("%s: bad VPD resource id: expected %x got %x\n",
420 sc->sk_dev.dv_xname, VPD_RES_READ, res.vr_id);
421 return;
422 }
423
424 pos += sizeof(res);
425 sc->sk_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
426 if (sc->sk_vpd_readonly == NULL)
427 panic("sk_vpd_read");
428 for (i = 0; i < res.vr_len ; i++)
429 sc->sk_vpd_readonly[i] = sk_vpd_readbyte(sc, i + pos);
430 }
431
432 int
433 sk_xmac_miibus_readreg(struct device *dev, int phy, int reg)
434 {
435 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
436 int i;
437
438 DPRINTFN(9, ("sk_xmac_miibus_readreg\n"));
439
440 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC && phy != 0)
441 return(0);
442
443 SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
444 SK_XM_READ_2(sc_if, XM_PHY_DATA);
445 if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
446 for (i = 0; i < SK_TIMEOUT; i++) {
447 DELAY(1);
448 if (SK_XM_READ_2(sc_if, XM_MMUCMD) &
449 XM_MMUCMD_PHYDATARDY)
450 break;
451 }
452
453 if (i == SK_TIMEOUT) {
454 printf("%s: phy failed to come ready\n",
455 sc_if->sk_dev.dv_xname);
456 return(0);
457 }
458 }
459 DELAY(1);
460 return(SK_XM_READ_2(sc_if, XM_PHY_DATA));
461 }
462
463 void
464 sk_xmac_miibus_writereg(struct device *dev, int phy, int reg, int val)
465 {
466 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
467 int i;
468
469 DPRINTFN(9, ("sk_xmac_miibus_writereg\n"));
470
471 SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
472 for (i = 0; i < SK_TIMEOUT; i++) {
473 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
474 break;
475 }
476
477 if (i == SK_TIMEOUT) {
478 printf("%s: phy failed to come ready\n",
479 sc_if->sk_dev.dv_xname);
480 return;
481 }
482
483 SK_XM_WRITE_2(sc_if, XM_PHY_DATA, val);
484 for (i = 0; i < SK_TIMEOUT; i++) {
485 DELAY(1);
486 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
487 break;
488 }
489
490 if (i == SK_TIMEOUT)
491 printf("%s: phy write timed out\n", sc_if->sk_dev.dv_xname);
492 }
493
494 void
495 sk_xmac_miibus_statchg(struct device *dev)
496 {
497 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
498 struct mii_data *mii = &sc_if->sk_mii;
499
500 DPRINTFN(9, ("sk_xmac_miibus_statchg\n"));
501
502 /*
503 * If this is a GMII PHY, manually set the XMAC's
504 * duplex mode accordingly.
505 */
506 if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
507 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
508 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
509 } else {
510 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
511 }
512 }
513 }
514
515 int
516 sk_marv_miibus_readreg(dev, phy, reg)
517 struct device *dev;
518 int phy, reg;
519 {
520 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
521 u_int16_t val;
522 int i;
523
524 if (phy != 0 ||
525 (sc_if->sk_phytype != SK_PHYTYPE_MARV_COPPER &&
526 sc_if->sk_phytype != SK_PHYTYPE_MARV_FIBER)) {
527 DPRINTFN(9, ("sk_marv_miibus_readreg (skip) phy=%d, reg=%#x\n",
528 phy, reg));
529 return(0);
530 }
531
532 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
533 YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
534
535 for (i = 0; i < SK_TIMEOUT; i++) {
536 DELAY(1);
537 val = SK_YU_READ_2(sc_if, YUKON_SMICR);
538 if (val & YU_SMICR_READ_VALID)
539 break;
540 }
541
542 if (i == SK_TIMEOUT) {
543 printf("%s: phy failed to come ready\n",
544 sc_if->sk_dev.dv_xname);
545 return 0;
546 }
547
548 DPRINTFN(9, ("sk_marv_miibus_readreg: i=%d, timeout=%d\n", i,
549 SK_TIMEOUT));
550
551 val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
552
553 DPRINTFN(9, ("sk_marv_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
554 phy, reg, val));
555
556 return val;
557 }
558
559 void
560 sk_marv_miibus_writereg(dev, phy, reg, val)
561 struct device *dev;
562 int phy, reg, val;
563 {
564 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
565 int i;
566
567 DPRINTFN(9, ("sk_marv_miibus_writereg phy=%d reg=%#x val=%#x\n",
568 phy, reg, val));
569
570 SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
571 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
572 YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
573
574 for (i = 0; i < SK_TIMEOUT; i++) {
575 DELAY(1);
576 if (SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY)
577 break;
578 }
579 }
580
581 void
582 sk_marv_miibus_statchg(dev)
583 struct device *dev;
584 {
585 DPRINTFN(9, ("sk_marv_miibus_statchg: gpcr=%x\n",
586 SK_YU_READ_2(((struct sk_if_softc *)dev), YUKON_GPCR)));
587 }
588
589 #define SK_HASH_BITS 6
590
591 u_int32_t
592 sk_xmac_hash(caddr_t addr)
593 {
594 u_int32_t crc;
595
596 crc = ether_crc32_le(addr,ETHER_ADDR_LEN);
597 crc = ~crc & ((1<< SK_HASH_BITS) - 1);
598 DPRINTFN(2,("multicast hash for %s is %x\n",ether_sprintf(addr),crc));
599 return (crc);
600 }
601
602 u_int32_t
603 sk_yukon_hash(caddr_t addr)
604 {
605 u_int32_t crc;
606
607 crc = ether_crc32_be(addr,ETHER_ADDR_LEN);
608 crc &= ((1 << SK_HASH_BITS) - 1);
609 DPRINTFN(2,("multicast hash for %s is %x\n",ether_sprintf(addr),crc));
610 return (crc);
611 }
612
613 void
614 sk_setfilt(struct sk_if_softc *sc_if, caddr_t addr, int slot)
615 {
616 int base = XM_RXFILT_ENTRY(slot);
617
618 SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
619 SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
620 SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
621 }
622
623 void
624 sk_setmulti(struct sk_if_softc *sc_if)
625 {
626 struct sk_softc *sc = sc_if->sk_softc;
627 struct ifnet *ifp= &sc_if->sk_ethercom.ec_if;
628 u_int32_t hashes[2] = { 0, 0 };
629 int h = 0, i;
630 struct ethercom *ec = &sc_if->sk_ethercom;
631 struct ether_multi *enm;
632 struct ether_multistep step;
633 u_int8_t dummy[] = { 0, 0, 0, 0, 0 ,0 };
634
635 /* First, zot all the existing filters. */
636 switch(sc->sk_type) {
637 case SK_GENESIS:
638 for (i = 1; i < XM_RXFILT_MAX; i++)
639 sk_setfilt(sc_if, (caddr_t)&dummy, i);
640
641 SK_XM_WRITE_4(sc_if, XM_MAR0, 0);
642 SK_XM_WRITE_4(sc_if, XM_MAR2, 0);
643 break;
644 case SK_YUKON:
645 case SK_YUKON_LITE:
646 case SK_YUKON_LP:
647 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
648 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
649 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
650 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
651 break;
652 }
653
654 /* Now program new ones. */
655 allmulti:
656 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
657 hashes[0] = 0xFFFFFFFF;
658 hashes[1] = 0xFFFFFFFF;
659 } else {
660 i = 1;
661 /* First find the tail of the list. */
662 ETHER_FIRST_MULTI(step, ec, enm);
663 while (enm != NULL) {
664 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
665 ETHER_ADDR_LEN)) {
666 ifp->if_flags |= IFF_ALLMULTI;
667 goto allmulti;
668 }
669 DPRINTFN(2,("multicast address %s\n",
670 ether_sprintf(enm->enm_addrlo)));
671 /*
672 * Program the first XM_RXFILT_MAX multicast groups
673 * into the perfect filter. For all others,
674 * use the hash table.
675 */
676 if (sc->sk_type == SK_GENESIS && i < XM_RXFILT_MAX) {
677 sk_setfilt(sc_if, enm->enm_addrlo, i);
678 i++;
679 }
680 else {
681 switch (sc->sk_type) {
682 case SK_GENESIS:
683 h = sk_xmac_hash(enm->enm_addrlo);
684 break;
685 case SK_YUKON:
686 case SK_YUKON_LITE:
687 case SK_YUKON_LP:
688 h = sk_yukon_hash(enm->enm_addrlo);
689 break;
690 }
691 if (h < 32)
692 hashes[0] |= (1 << h);
693 else
694 hashes[1] |= (1 << (h - 32));
695 }
696
697 ETHER_NEXT_MULTI(step, enm);
698 }
699 }
700
701 switch(sc->sk_type) {
702 case SK_GENESIS:
703 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_HASH|
704 XM_MODE_RX_USE_PERFECT);
705 SK_XM_WRITE_4(sc_if, XM_MAR0, hashes[0]);
706 SK_XM_WRITE_4(sc_if, XM_MAR2, hashes[1]);
707 break;
708 case SK_YUKON:
709 case SK_YUKON_LITE:
710 case SK_YUKON_LP:
711 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
712 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
713 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
714 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
715 break;
716 }
717 }
718
719 int
720 sk_init_rx_ring(struct sk_if_softc *sc_if)
721 {
722 struct sk_chain_data *cd = &sc_if->sk_cdata;
723 struct sk_ring_data *rd = sc_if->sk_rdata;
724 int i;
725
726 bzero((char *)rd->sk_rx_ring,
727 sizeof(struct sk_rx_desc) * SK_RX_RING_CNT);
728
729 for (i = 0; i < SK_RX_RING_CNT; i++) {
730 cd->sk_rx_chain[i].sk_desc = &rd->sk_rx_ring[i];
731 if (i == (SK_RX_RING_CNT - 1)) {
732 cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[0];
733 rd->sk_rx_ring[i].sk_next = SK_RX_RING_ADDR(sc_if, 0);
734 } else {
735 cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[i + 1];
736 rd->sk_rx_ring[i].sk_next = SK_RX_RING_ADDR(sc_if,i+1);
737 }
738 }
739
740 for (i = 0; i < SK_RX_RING_CNT; i++) {
741 if (sk_newbuf(sc_if, i, NULL,
742 sc_if->sk_cdata.sk_rx_jumbo_map) == ENOBUFS) {
743 printf("%s: failed alloc of %dth mbuf\n",
744 sc_if->sk_dev.dv_xname, i);
745 return(ENOBUFS);
746 }
747 }
748 sc_if->sk_cdata.sk_rx_prod = 0;
749 sc_if->sk_cdata.sk_rx_cons = 0;
750
751 return(0);
752 }
753
754 int
755 sk_init_tx_ring(struct sk_if_softc *sc_if)
756 {
757 struct sk_chain_data *cd = &sc_if->sk_cdata;
758 struct sk_ring_data *rd = sc_if->sk_rdata;
759 int i;
760
761 bzero((char *)sc_if->sk_rdata->sk_tx_ring,
762 sizeof(struct sk_tx_desc) * SK_TX_RING_CNT);
763
764 for (i = 0; i < SK_TX_RING_CNT; i++) {
765 cd->sk_tx_chain[i].sk_desc = &rd->sk_tx_ring[i];
766 if (i == (SK_TX_RING_CNT - 1)) {
767 cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[0];
768 rd->sk_tx_ring[i].sk_next = SK_TX_RING_ADDR(sc_if, 0);
769 } else {
770 cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[i + 1];
771 rd->sk_tx_ring[i].sk_next = SK_TX_RING_ADDR(sc_if,i+1);
772 }
773 }
774
775 sc_if->sk_cdata.sk_tx_prod = 0;
776 sc_if->sk_cdata.sk_tx_cons = 0;
777 sc_if->sk_cdata.sk_tx_cnt = 0;
778
779 SK_CDTXSYNC(sc_if, 0, SK_TX_RING_CNT,
780 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
781
782 return (0);
783 }
784
785 int
786 sk_newbuf(struct sk_if_softc *sc_if, int i, struct mbuf *m,
787 bus_dmamap_t dmamap)
788 {
789 struct mbuf *m_new = NULL;
790 struct sk_chain *c;
791 struct sk_rx_desc *r;
792
793 if (m == NULL) {
794 caddr_t buf = NULL;
795
796 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
797 if (m_new == NULL) {
798 printf("%s: no memory for rx list -- "
799 "packet dropped!\n", sc_if->sk_dev.dv_xname);
800 return(ENOBUFS);
801 }
802
803 /* Allocate the jumbo buffer */
804 buf = sk_jalloc(sc_if);
805 if (buf == NULL) {
806 m_freem(m_new);
807 DPRINTFN(1, ("%s jumbo allocation failed -- packet "
808 "dropped!\n", sc_if->sk_ethercom.ec_if.if_xname));
809 return(ENOBUFS);
810 }
811
812 /* Attach the buffer to the mbuf */
813 m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
814 MEXTADD(m_new, buf, SK_JLEN, 0, sk_jfree, sc_if);
815
816 } else {
817 /*
818 * We're re-using a previously allocated mbuf;
819 * be sure to re-init pointers and lengths to
820 * default values.
821 */
822 m_new = m;
823 m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
824 m_new->m_data = m_new->m_ext.ext_buf;
825 }
826 m_adj(m_new, ETHER_ALIGN);
827
828 c = &sc_if->sk_cdata.sk_rx_chain[i];
829 r = c->sk_desc;
830 c->sk_mbuf = m_new;
831 r->sk_data_lo = dmamap->dm_segs[0].ds_addr +
832 (((vaddr_t)m_new->m_data
833 - (vaddr_t)sc_if->sk_cdata.sk_jumbo_buf));
834 r->sk_ctl = SK_JLEN | SK_RXSTAT;
835
836 SK_CDRXSYNC(sc_if, i, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
837
838 return(0);
839 }
840
841 /*
842 * Memory management for jumbo frames.
843 */
844
845 int
846 sk_alloc_jumbo_mem(struct sk_if_softc *sc_if)
847 {
848 struct sk_softc *sc = sc_if->sk_softc;
849 caddr_t ptr, kva;
850 bus_dma_segment_t seg;
851 int i, rseg, state, error;
852 struct sk_jpool_entry *entry;
853
854 state = error = 0;
855
856 /* Grab a big chunk o' storage. */
857 if (bus_dmamem_alloc(sc->sc_dmatag, SK_JMEM, PAGE_SIZE, 0,
858 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
859 printf("%s: can't alloc rx buffers\n", sc->sk_dev.dv_xname);
860 return (ENOBUFS);
861 }
862
863 state = 1;
864 if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg, SK_JMEM, &kva,
865 BUS_DMA_NOWAIT)) {
866 printf("%s: can't map dma buffers (%d bytes)\n",
867 sc->sk_dev.dv_xname, SK_JMEM);
868 error = ENOBUFS;
869 goto out;
870 }
871
872 state = 2;
873 if (bus_dmamap_create(sc->sc_dmatag, SK_JMEM, 1, SK_JMEM, 0,
874 BUS_DMA_NOWAIT, &sc_if->sk_cdata.sk_rx_jumbo_map)) {
875 printf("%s: can't create dma map\n", sc->sk_dev.dv_xname);
876 error = ENOBUFS;
877 goto out;
878 }
879
880 state = 3;
881 if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_cdata.sk_rx_jumbo_map,
882 kva, SK_JMEM, NULL, BUS_DMA_NOWAIT)) {
883 printf("%s: can't load dma map\n", sc->sk_dev.dv_xname);
884 error = ENOBUFS;
885 goto out;
886 }
887
888 state = 4;
889 sc_if->sk_cdata.sk_jumbo_buf = (caddr_t)kva;
890 DPRINTFN(1,("sk_jumbo_buf = 0x%p\n", sc_if->sk_cdata.sk_jumbo_buf));
891
892 LIST_INIT(&sc_if->sk_jfree_listhead);
893 LIST_INIT(&sc_if->sk_jinuse_listhead);
894
895 /*
896 * Now divide it up into 9K pieces and save the addresses
897 * in an array.
898 */
899 ptr = sc_if->sk_cdata.sk_jumbo_buf;
900 for (i = 0; i < SK_JSLOTS; i++) {
901 sc_if->sk_cdata.sk_jslots[i] = ptr;
902 ptr += SK_JLEN;
903 entry = malloc(sizeof(struct sk_jpool_entry),
904 M_DEVBUF, M_NOWAIT);
905 if (entry == NULL) {
906 printf("%s: no memory for jumbo buffer queue!\n",
907 sc->sk_dev.dv_xname);
908 error = ENOBUFS;
909 goto out;
910 }
911 entry->slot = i;
912 if (i)
913 LIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
914 entry, jpool_entries);
915 else
916 LIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead,
917 entry, jpool_entries);
918 }
919 out:
920 if (error != 0) {
921 switch (state) {
922 case 4:
923 bus_dmamap_unload(sc->sc_dmatag,
924 sc_if->sk_cdata.sk_rx_jumbo_map);
925 case 3:
926 bus_dmamap_destroy(sc->sc_dmatag,
927 sc_if->sk_cdata.sk_rx_jumbo_map);
928 case 2:
929 bus_dmamem_unmap(sc->sc_dmatag, kva, SK_JMEM);
930 case 1:
931 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
932 break;
933 default:
934 break;
935 }
936 }
937
938 return (error);
939 }
940
941 /*
942 * Allocate a jumbo buffer.
943 */
944 void *
945 sk_jalloc(struct sk_if_softc *sc_if)
946 {
947 struct sk_jpool_entry *entry;
948
949 entry = LIST_FIRST(&sc_if->sk_jfree_listhead);
950
951 if (entry == NULL)
952 return (NULL);
953
954 LIST_REMOVE(entry, jpool_entries);
955 LIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries);
956 return (sc_if->sk_cdata.sk_jslots[entry->slot]);
957 }
958
959 /*
960 * Release a jumbo buffer.
961 */
962 void
963 sk_jfree(struct mbuf *m, caddr_t buf, size_t size, void *arg)
964 {
965 struct sk_jpool_entry *entry;
966 struct sk_if_softc *sc;
967 int i, s;
968
969 /* Extract the softc struct pointer. */
970 sc = (struct sk_if_softc *)arg;
971
972 if (sc == NULL)
973 panic("sk_jfree: can't find softc pointer!");
974
975 /* calculate the slot this buffer belongs to */
976
977 i = ((vaddr_t)buf
978 - (vaddr_t)sc->sk_cdata.sk_jumbo_buf) / SK_JLEN;
979
980 if ((i < 0) || (i >= SK_JSLOTS))
981 panic("sk_jfree: asked to free buffer that we don't manage!");
982
983 s = splvm();
984 entry = LIST_FIRST(&sc->sk_jinuse_listhead);
985 if (entry == NULL)
986 panic("sk_jfree: buffer not in use!");
987 entry->slot = i;
988 LIST_REMOVE(entry, jpool_entries);
989 LIST_INSERT_HEAD(&sc->sk_jfree_listhead, entry, jpool_entries);
990
991 if (__predict_true(m != NULL))
992 pool_cache_put(&mbpool_cache, m);
993 splx(s);
994 }
995
996 /*
997 * Set media options.
998 */
999 int
1000 sk_ifmedia_upd(struct ifnet *ifp)
1001 {
1002 struct sk_if_softc *sc_if = ifp->if_softc;
1003
1004 (void) sk_init(ifp);
1005 mii_mediachg(&sc_if->sk_mii);
1006 return(0);
1007 }
1008
1009 /*
1010 * Report current media status.
1011 */
1012 void
1013 sk_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1014 {
1015 struct sk_if_softc *sc_if = ifp->if_softc;
1016
1017 mii_pollstat(&sc_if->sk_mii);
1018 ifmr->ifm_active = sc_if->sk_mii.mii_media_active;
1019 ifmr->ifm_status = sc_if->sk_mii.mii_media_status;
1020 }
1021
1022 int
1023 sk_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1024 {
1025 struct sk_if_softc *sc_if = ifp->if_softc;
1026 struct sk_softc *sc = sc_if->sk_softc;
1027 struct ifreq *ifr = (struct ifreq *) data;
1028 /* struct ifaddr *ifa = (struct ifaddr *) data; */
1029 struct mii_data *mii;
1030 int s, error = 0;
1031
1032 /* DPRINTFN(2, ("sk_ioctl\n")); */
1033
1034 s = splnet();
1035
1036 switch(command) {
1037
1038 case SIOCSIFFLAGS:
1039 DPRINTFN(2, ("sk_ioctl IFFLAGS\n"));
1040 if (ifp->if_flags & IFF_UP) {
1041 if (ifp->if_flags & IFF_RUNNING &&
1042 ifp->if_flags & IFF_PROMISC &&
1043 !(sc_if->sk_if_flags & IFF_PROMISC)) {
1044 switch(sc->sk_type) {
1045 case SK_GENESIS:
1046 SK_XM_SETBIT_4(sc_if, XM_MODE,
1047 XM_MODE_RX_PROMISC);
1048 break;
1049 case SK_YUKON:
1050 case SK_YUKON_LITE:
1051 case SK_YUKON_LP:
1052 SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
1053 YU_RCR_UFLEN | YU_RCR_MUFLEN);
1054 break;
1055 }
1056 sk_setmulti(sc_if);
1057 } else if (ifp->if_flags & IFF_RUNNING &&
1058 !(ifp->if_flags & IFF_PROMISC) &&
1059 sc_if->sk_if_flags & IFF_PROMISC) {
1060 switch(sc->sk_type) {
1061 case SK_GENESIS:
1062 SK_XM_CLRBIT_4(sc_if, XM_MODE,
1063 XM_MODE_RX_PROMISC);
1064 break;
1065 case SK_YUKON:
1066 case SK_YUKON_LITE:
1067 case SK_YUKON_LP:
1068 SK_YU_SETBIT_2(sc_if, YUKON_RCR,
1069 YU_RCR_UFLEN | YU_RCR_MUFLEN);
1070 break;
1071 }
1072
1073 sk_setmulti(sc_if);
1074 } else
1075 (void) sk_init(ifp);
1076 } else {
1077 if (ifp->if_flags & IFF_RUNNING)
1078 sk_stop(ifp,0);
1079 }
1080 sc_if->sk_if_flags = ifp->if_flags;
1081 error = 0;
1082 break;
1083
1084 case SIOCGIFMEDIA:
1085 case SIOCSIFMEDIA:
1086 DPRINTFN(2, ("sk_ioctl MEDIA\n"));
1087 mii = &sc_if->sk_mii;
1088 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1089 break;
1090 default:
1091 DPRINTFN(2, ("sk_ioctl ETHER\n"));
1092 error = ether_ioctl(ifp, command, data);
1093
1094 if ( error == ENETRESET) {
1095 if (ifp->if_flags & IFF_RUNNING) {
1096 sk_setmulti(sc_if);
1097 DPRINTFN(2, ("sk_ioctl setmulti called\n"));
1098 }
1099 error = 0;
1100 } else if ( error ) {
1101 splx(s);
1102 return error;
1103 }
1104 break;
1105 }
1106
1107 splx(s);
1108 return(error);
1109 }
1110
1111 void
1112 sk_update_int_mod(struct sk_softc *sc)
1113 {
1114 u_int32_t sk_imtimer_ticks;
1115
1116 /*
1117 * Configure interrupt moderation. The moderation timer
1118 * defers interrupts specified in the interrupt moderation
1119 * timer mask based on the timeout specified in the interrupt
1120 * moderation timer init register. Each bit in the timer
1121 * register represents one tick, so to specify a timeout in
1122 * microseconds, we have to multiply by the correct number of
1123 * ticks-per-microsecond.
1124 */
1125 switch (sc->sk_type) {
1126 case SK_GENESIS:
1127 sk_imtimer_ticks = SK_IMTIMER_TICKS_GENESIS;
1128 break;
1129 case SK_YUKON_EC:
1130 sk_imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
1131 break;
1132 default:
1133 sk_imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
1134 }
1135 aprint_verbose("%s: interrupt moderation is %d us\n",
1136 sc->sk_dev.dv_xname, sc->sk_int_mod);
1137 sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(sc->sk_int_mod));
1138 sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
1139 SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
1140 sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
1141 sc->sk_int_mod_pending = 0;
1142 }
1143
1144 /*
1145 * Lookup: Check the PCI vendor and device, and return a pointer to
1146 * The structure if the IDs match against our list.
1147 */
1148
1149 static const struct sk_product *
1150 sk_lookup(const struct pci_attach_args *pa)
1151 {
1152 const struct sk_product *psk;
1153
1154 for ( psk = &sk_products[0]; psk->sk_vendor != 0; psk++ ) {
1155 if (PCI_VENDOR(pa->pa_id) == psk->sk_vendor &&
1156 PCI_PRODUCT(pa->pa_id) == psk->sk_product)
1157 return (psk);
1158 }
1159 return (NULL);
1160 }
1161
1162 /*
1163 * Probe for a SysKonnect GEnesis chip.
1164 */
1165
1166 int
1167 skc_probe(struct device *parent, struct cfdata *match, void *aux)
1168 {
1169 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1170 const struct sk_product *psk;
1171 pcireg_t subid;
1172
1173 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
1174
1175 /* special-case Linksys EG1032, since rev 3 uses re(4) */
1176 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
1177 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032 &&
1178 subid == SK_LINKSYS_EG1032_SUBID)
1179 return(1);
1180
1181 if ((psk = sk_lookup(pa))) {
1182 return(1);
1183 }
1184 return(0);
1185 }
1186
1187 /*
1188 * Force the GEnesis into reset, then bring it out of reset.
1189 */
1190 void sk_reset(struct sk_softc *sc)
1191 {
1192 DPRINTFN(2, ("sk_reset\n"));
1193
1194 CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_RESET);
1195 CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_RESET);
1196 if (SK_YUKON_FAMILY(sc->sk_type))
1197 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
1198
1199 DELAY(1000);
1200 CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_UNRESET);
1201 DELAY(2);
1202 CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
1203 if (SK_YUKON_FAMILY(sc->sk_type))
1204 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
1205
1206 DPRINTFN(2, ("sk_reset: sk_csr=%x\n", CSR_READ_2(sc, SK_CSR)));
1207 DPRINTFN(2, ("sk_reset: sk_link_ctrl=%x\n",
1208 CSR_READ_2(sc, SK_LINK_CTRL)));
1209
1210 if (sc->sk_type == SK_GENESIS) {
1211 /* Configure packet arbiter */
1212 sk_win_write_2(sc, SK_PKTARB_CTL, SK_PKTARBCTL_UNRESET);
1213 sk_win_write_2(sc, SK_RXPA1_TINIT, SK_PKTARB_TIMEOUT);
1214 sk_win_write_2(sc, SK_TXPA1_TINIT, SK_PKTARB_TIMEOUT);
1215 sk_win_write_2(sc, SK_RXPA2_TINIT, SK_PKTARB_TIMEOUT);
1216 sk_win_write_2(sc, SK_TXPA2_TINIT, SK_PKTARB_TIMEOUT);
1217 }
1218
1219 /* Enable RAM interface */
1220 sk_win_write_4(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
1221
1222 sk_update_int_mod(sc);
1223 }
1224
1225 int
1226 sk_probe(struct device *parent, struct cfdata *match, void *aux)
1227 {
1228 struct skc_attach_args *sa = aux;
1229
1230 if (sa->skc_port != SK_PORT_A && sa->skc_port != SK_PORT_B)
1231 return(0);
1232
1233 return (1);
1234 }
1235
1236 /*
1237 * Each XMAC chip is attached as a separate logical IP interface.
1238 * Single port cards will have only one logical interface of course.
1239 */
1240 void
1241 sk_attach(struct device *parent, struct device *self, void *aux)
1242 {
1243 struct sk_if_softc *sc_if = (struct sk_if_softc *) self;
1244 struct sk_softc *sc = (struct sk_softc *)parent;
1245 struct skc_attach_args *sa = aux;
1246 struct sk_txmap_entry *entry;
1247 struct ifnet *ifp;
1248 bus_dma_segment_t seg;
1249 bus_dmamap_t dmamap;
1250 caddr_t kva;
1251 int i, rseg;
1252
1253 sc_if->sk_port = sa->skc_port;
1254 sc_if->sk_softc = sc;
1255 sc->sk_if[sa->skc_port] = sc_if;
1256
1257 if (sa->skc_port == SK_PORT_A)
1258 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR0;
1259 if (sa->skc_port == SK_PORT_B)
1260 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR1;
1261
1262 DPRINTFN(2, ("begin sk_attach: port=%d\n", sc_if->sk_port));
1263
1264 /*
1265 * Get station address for this interface. Note that
1266 * dual port cards actually come with three station
1267 * addresses: one for each port, plus an extra. The
1268 * extra one is used by the SysKonnect driver software
1269 * as a 'virtual' station address for when both ports
1270 * are operating in failover mode. Currently we don't
1271 * use this extra address.
1272 */
1273 for (i = 0; i < ETHER_ADDR_LEN; i++)
1274 sc_if->sk_enaddr[i] =
1275 sk_win_read_1(sc, SK_MAC0_0 + (sa->skc_port * 8) + i);
1276
1277
1278 aprint_normal(": Ethernet address %s\n",
1279 ether_sprintf(sc_if->sk_enaddr));
1280
1281 /*
1282 * Set up RAM buffer addresses. The NIC will have a certain
1283 * amount of SRAM on it, somewhere between 512K and 2MB. We
1284 * need to divide this up a) between the transmitter and
1285 * receiver and b) between the two XMACs, if this is a
1286 * dual port NIC. Our algorithm is to divide up the memory
1287 * evenly so that everyone gets a fair share.
1288 */
1289 if (sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC) {
1290 u_int32_t chunk, val;
1291
1292 chunk = sc->sk_ramsize / 2;
1293 val = sc->sk_rboff / sizeof(u_int64_t);
1294 sc_if->sk_rx_ramstart = val;
1295 val += (chunk / sizeof(u_int64_t));
1296 sc_if->sk_rx_ramend = val - 1;
1297 sc_if->sk_tx_ramstart = val;
1298 val += (chunk / sizeof(u_int64_t));
1299 sc_if->sk_tx_ramend = val - 1;
1300 } else {
1301 u_int32_t chunk, val;
1302
1303 chunk = sc->sk_ramsize / 4;
1304 val = (sc->sk_rboff + (chunk * 2 * sc_if->sk_port)) /
1305 sizeof(u_int64_t);
1306 sc_if->sk_rx_ramstart = val;
1307 val += (chunk / sizeof(u_int64_t));
1308 sc_if->sk_rx_ramend = val - 1;
1309 sc_if->sk_tx_ramstart = val;
1310 val += (chunk / sizeof(u_int64_t));
1311 sc_if->sk_tx_ramend = val - 1;
1312 }
1313
1314 DPRINTFN(2, ("sk_attach: rx_ramstart=%#x rx_ramend=%#x\n"
1315 " tx_ramstart=%#x tx_ramend=%#x\n",
1316 sc_if->sk_rx_ramstart, sc_if->sk_rx_ramend,
1317 sc_if->sk_tx_ramstart, sc_if->sk_tx_ramend));
1318
1319 /* Read and save PHY type and set PHY address */
1320 sc_if->sk_phytype = sk_win_read_1(sc, SK_EPROM1) & 0xF;
1321 switch (sc_if->sk_phytype) {
1322 case SK_PHYTYPE_XMAC:
1323 sc_if->sk_phyaddr = SK_PHYADDR_XMAC;
1324 break;
1325 case SK_PHYTYPE_BCOM:
1326 sc_if->sk_phyaddr = SK_PHYADDR_BCOM;
1327 break;
1328 case SK_PHYTYPE_MARV_COPPER:
1329 sc_if->sk_phyaddr = SK_PHYADDR_MARV;
1330 break;
1331 default:
1332 aprint_error("%s: unsupported PHY type: %d\n",
1333 sc->sk_dev.dv_xname, sc_if->sk_phytype);
1334 return;
1335 }
1336
1337 /* Allocate the descriptor queues. */
1338 if (bus_dmamem_alloc(sc->sc_dmatag, sizeof(struct sk_ring_data),
1339 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1340 aprint_error("%s: can't alloc rx buffers\n",
1341 sc->sk_dev.dv_xname);
1342 goto fail;
1343 }
1344 if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
1345 sizeof(struct sk_ring_data), &kva, BUS_DMA_NOWAIT)) {
1346 aprint_error("%s: can't map dma buffers (%lu bytes)\n",
1347 sc_if->sk_dev.dv_xname,
1348 (u_long) sizeof(struct sk_ring_data));
1349 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1350 goto fail;
1351 }
1352 if (bus_dmamap_create(sc->sc_dmatag, sizeof(struct sk_ring_data), 1,
1353 sizeof(struct sk_ring_data), 0, BUS_DMA_NOWAIT,
1354 &sc_if->sk_ring_map)) {
1355 aprint_error("%s: can't create dma map\n",
1356 sc_if->sk_dev.dv_xname);
1357 bus_dmamem_unmap(sc->sc_dmatag, kva,
1358 sizeof(struct sk_ring_data));
1359 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1360 goto fail;
1361 }
1362 if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_ring_map, kva,
1363 sizeof(struct sk_ring_data), NULL, BUS_DMA_NOWAIT)) {
1364 aprint_error("%s: can't load dma map\n",
1365 sc_if->sk_dev.dv_xname);
1366 bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
1367 bus_dmamem_unmap(sc->sc_dmatag, kva,
1368 sizeof(struct sk_ring_data));
1369 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1370 goto fail;
1371 }
1372
1373 for (i = 0; i < SK_RX_RING_CNT; i++)
1374 sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
1375
1376 SIMPLEQ_INIT(&sc_if->sk_txmap_head);
1377 for (i = 0; i < SK_TX_RING_CNT; i++) {
1378 sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
1379
1380 if (bus_dmamap_create(sc->sc_dmatag, SK_JLEN, SK_NTXSEG,
1381 SK_JLEN, 0, BUS_DMA_NOWAIT, &dmamap)) {
1382 aprint_error("%s: Can't create TX dmamap\n",
1383 sc_if->sk_dev.dv_xname);
1384 bus_dmamap_unload(sc->sc_dmatag, sc_if->sk_ring_map);
1385 bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
1386 bus_dmamem_unmap(sc->sc_dmatag, kva,
1387 sizeof(struct sk_ring_data));
1388 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1389 goto fail;
1390 }
1391
1392 entry = malloc(sizeof(*entry), M_DEVBUF, M_NOWAIT);
1393 if (!entry) {
1394 aprint_error("%s: Can't alloc txmap entry\n",
1395 sc_if->sk_dev.dv_xname);
1396 bus_dmamap_destroy(sc->sc_dmatag, dmamap);
1397 bus_dmamap_unload(sc->sc_dmatag, sc_if->sk_ring_map);
1398 bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
1399 bus_dmamem_unmap(sc->sc_dmatag, kva,
1400 sizeof(struct sk_ring_data));
1401 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1402 goto fail;
1403 }
1404 entry->dmamap = dmamap;
1405 SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head, entry, link);
1406 }
1407
1408 sc_if->sk_rdata = (struct sk_ring_data *)kva;
1409 bzero(sc_if->sk_rdata, sizeof(struct sk_ring_data));
1410
1411 ifp = &sc_if->sk_ethercom.ec_if;
1412 /* Try to allocate memory for jumbo buffers. */
1413 if (sk_alloc_jumbo_mem(sc_if)) {
1414 printf("%s: jumbo buffer allocation failed\n", ifp->if_xname);
1415 goto fail;
1416 }
1417 sc_if->sk_ethercom.ec_capabilities = ETHERCAP_VLAN_MTU
1418 | ETHERCAP_JUMBO_MTU;
1419
1420 ifp->if_softc = sc_if;
1421 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1422 ifp->if_ioctl = sk_ioctl;
1423 ifp->if_start = sk_start;
1424 ifp->if_stop = sk_stop;
1425 ifp->if_init = sk_init;
1426 ifp->if_watchdog = sk_watchdog;
1427 ifp->if_capabilities = 0;
1428 IFQ_SET_MAXLEN(&ifp->if_snd, SK_TX_RING_CNT - 1);
1429 IFQ_SET_READY(&ifp->if_snd);
1430 strcpy(ifp->if_xname, sc_if->sk_dev.dv_xname);
1431
1432 /*
1433 * Do miibus setup.
1434 */
1435 switch (sc->sk_type) {
1436 case SK_GENESIS:
1437 sk_init_xmac(sc_if);
1438 break;
1439 case SK_YUKON:
1440 case SK_YUKON_LITE:
1441 case SK_YUKON_LP:
1442 sk_init_yukon(sc_if);
1443 break;
1444 default:
1445 panic("%s: unknown device type %d", sc->sk_dev.dv_xname,
1446 sc->sk_type);
1447 }
1448
1449 DPRINTFN(2, ("sk_attach: 1\n"));
1450
1451 sc_if->sk_mii.mii_ifp = ifp;
1452 switch (sc->sk_type) {
1453 case SK_GENESIS:
1454 sc_if->sk_mii.mii_readreg = sk_xmac_miibus_readreg;
1455 sc_if->sk_mii.mii_writereg = sk_xmac_miibus_writereg;
1456 sc_if->sk_mii.mii_statchg = sk_xmac_miibus_statchg;
1457 break;
1458 case SK_YUKON:
1459 case SK_YUKON_LITE:
1460 case SK_YUKON_LP:
1461 sc_if->sk_mii.mii_readreg = sk_marv_miibus_readreg;
1462 sc_if->sk_mii.mii_writereg = sk_marv_miibus_writereg;
1463 sc_if->sk_mii.mii_statchg = sk_marv_miibus_statchg;
1464 break;
1465 }
1466
1467 ifmedia_init(&sc_if->sk_mii.mii_media, 0,
1468 sk_ifmedia_upd, sk_ifmedia_sts);
1469 mii_attach(self, &sc_if->sk_mii, 0xffffffff, MII_PHY_ANY,
1470 MII_OFFSET_ANY, 0);
1471 if (LIST_FIRST(&sc_if->sk_mii.mii_phys) == NULL) {
1472 printf("%s: no PHY found!\n", sc_if->sk_dev.dv_xname);
1473 ifmedia_add(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL,
1474 0, NULL);
1475 ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL);
1476 }
1477 else
1478 ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_AUTO);
1479
1480 callout_init(&sc_if->sk_tick_ch);
1481 callout_reset(&sc_if->sk_tick_ch,hz,sk_tick,sc_if);
1482
1483 DPRINTFN(2, ("sk_attach: 1\n"));
1484
1485 /*
1486 * Call MI attach routines.
1487 */
1488 if_attach(ifp);
1489
1490 ether_ifattach(ifp, sc_if->sk_enaddr);
1491
1492 #if NRND > 0
1493 rnd_attach_source(&sc->rnd_source, sc->sk_dev.dv_xname,
1494 RND_TYPE_NET, 0);
1495 #endif
1496
1497 DPRINTFN(2, ("sk_attach: end\n"));
1498
1499 return;
1500
1501 fail:
1502 sc->sk_if[sa->skc_port] = NULL;
1503 }
1504
1505 int
1506 skcprint(void *aux, const char *pnp)
1507 {
1508 struct skc_attach_args *sa = aux;
1509
1510 if (pnp)
1511 aprint_normal("sk port %c at %s",
1512 (sa->skc_port == SK_PORT_A) ? 'A' : 'B', pnp);
1513 else
1514 aprint_normal(" port %c",
1515 (sa->skc_port == SK_PORT_A) ? 'A' : 'B');
1516 return (UNCONF);
1517 }
1518
1519 /*
1520 * Attach the interface. Allocate softc structures, do ifmedia
1521 * setup and ethernet/BPF attach.
1522 */
1523 void
1524 skc_attach(struct device *parent, struct device *self, void *aux)
1525 {
1526 struct sk_softc *sc = (struct sk_softc *)self;
1527 struct pci_attach_args *pa = aux;
1528 struct skc_attach_args skca;
1529 pci_chipset_tag_t pc = pa->pa_pc;
1530 #ifndef SK_USEIOSPACE
1531 pcireg_t memtype;
1532 #endif
1533 pci_intr_handle_t ih;
1534 const char *intrstr = NULL;
1535 bus_addr_t iobase;
1536 bus_size_t iosize;
1537 int s, rc, sk_nodenum;
1538 u_int32_t command;
1539 const char *revstr;
1540 const struct sysctlnode *node;
1541
1542 DPRINTFN(2, ("begin skc_attach\n"));
1543
1544 s = splnet();
1545
1546 /*
1547 * Handle power management nonsense.
1548 */
1549 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_CAPID) & 0x000000FF;
1550
1551 if (command == 0x01) {
1552 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_PWRMGMTCTRL);
1553 if (command & SK_PSTATE_MASK) {
1554 u_int32_t xiobase, membase, irq;
1555
1556 /* Save important PCI config data. */
1557 xiobase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOIO);
1558 membase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOMEM);
1559 irq = pci_conf_read(pc, pa->pa_tag, SK_PCI_INTLINE);
1560
1561 /* Reset the power state. */
1562 aprint_normal("%s chip is in D%d power mode "
1563 "-- setting to D0\n", sc->sk_dev.dv_xname,
1564 command & SK_PSTATE_MASK);
1565 command &= 0xFFFFFFFC;
1566 pci_conf_write(pc, pa->pa_tag,
1567 SK_PCI_PWRMGMTCTRL, command);
1568
1569 /* Restore PCI config data. */
1570 pci_conf_write(pc, pa->pa_tag, SK_PCI_LOIO, xiobase);
1571 pci_conf_write(pc, pa->pa_tag, SK_PCI_LOMEM, membase);
1572 pci_conf_write(pc, pa->pa_tag, SK_PCI_INTLINE, irq);
1573 }
1574 }
1575
1576 /*
1577 * Map control/status registers.
1578 */
1579 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1580 command |= PCI_COMMAND_IO_ENABLE |
1581 PCI_COMMAND_MEM_ENABLE |
1582 PCI_COMMAND_MASTER_ENABLE;
1583 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
1584 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1585
1586 #ifdef SK_USEIOSPACE
1587 if (!(command & PCI_COMMAND_IO_ENABLE)) {
1588 aprint_error(": failed to enable I/O ports!\n");
1589 goto fail;
1590 }
1591 /*
1592 * Map control/status registers.
1593 */
1594 if (pci_mapreg_map(pa, SK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
1595 &sc->sk_btag, &sc->sk_bhandle,
1596 &iobase, &iosize)) {
1597 aprint_error(": can't find i/o space\n");
1598 goto fail;
1599 }
1600 #else
1601 if (!(command & PCI_COMMAND_MEM_ENABLE)) {
1602 aprint_error(": failed to enable memory mapping!\n");
1603 goto fail;
1604 }
1605 memtype = pci_mapreg_type(pc, pa->pa_tag, SK_PCI_LOMEM);
1606 switch (memtype) {
1607 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1608 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1609 if (pci_mapreg_map(pa, SK_PCI_LOMEM,
1610 memtype, 0, &sc->sk_btag, &sc->sk_bhandle,
1611 &iobase, &iosize) == 0)
1612 break;
1613 default:
1614 aprint_error("%s: can't find mem space\n",
1615 sc->sk_dev.dv_xname);
1616 return;
1617 }
1618
1619 DPRINTFN(2, ("skc_attach: iobase=%lx, iosize=%lx\n", iobase, iosize));
1620 #endif
1621 sc->sc_dmatag = pa->pa_dmat;
1622
1623 sc->sk_type = sk_win_read_1(sc, SK_CHIPVER);
1624 sc->sk_rev = (sk_win_read_1(sc, SK_CONFIG) >> 4);
1625
1626 /* bail out here if chip is not recognized */
1627 if ( sc->sk_type != SK_GENESIS && ! SK_YUKON_FAMILY(sc->sk_type)) {
1628 aprint_error("%s: unknown chip type\n",sc->sk_dev.dv_xname);
1629 goto fail;
1630 }
1631 DPRINTFN(2, ("skc_attach: allocate interrupt\n"));
1632
1633 /* Allocate interrupt */
1634 if (pci_intr_map(pa, &ih)) {
1635 aprint_error(": couldn't map interrupt\n");
1636 goto fail;
1637 }
1638
1639 intrstr = pci_intr_string(pc, ih);
1640 sc->sk_intrhand = pci_intr_establish(pc, ih, IPL_NET, sk_intr, sc);
1641 if (sc->sk_intrhand == NULL) {
1642 aprint_error(": couldn't establish interrupt");
1643 if (intrstr != NULL)
1644 aprint_normal(" at %s", intrstr);
1645 goto fail;
1646 }
1647 aprint_normal(": %s\n", intrstr);
1648
1649 /* Reset the adapter. */
1650 sk_reset(sc);
1651
1652 /* Read and save vital product data from EEPROM. */
1653 sk_vpd_read(sc);
1654
1655 if (sc->sk_type == SK_GENESIS) {
1656 u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
1657 /* Read and save RAM size and RAMbuffer offset */
1658 switch(val) {
1659 case SK_RAMSIZE_512K_64:
1660 sc->sk_ramsize = 0x80000;
1661 sc->sk_rboff = SK_RBOFF_0;
1662 break;
1663 case SK_RAMSIZE_1024K_64:
1664 sc->sk_ramsize = 0x100000;
1665 sc->sk_rboff = SK_RBOFF_80000;
1666 break;
1667 case SK_RAMSIZE_1024K_128:
1668 sc->sk_ramsize = 0x100000;
1669 sc->sk_rboff = SK_RBOFF_0;
1670 break;
1671 case SK_RAMSIZE_2048K_128:
1672 sc->sk_ramsize = 0x200000;
1673 sc->sk_rboff = SK_RBOFF_0;
1674 break;
1675 default:
1676 aprint_error("%s: unknown ram size: %d\n",
1677 sc->sk_dev.dv_xname, val);
1678 goto fail;
1679 break;
1680 }
1681
1682 DPRINTFN(2, ("skc_attach: ramsize=%d(%dk), rboff=%d\n",
1683 sc->sk_ramsize, sc->sk_ramsize / 1024,
1684 sc->sk_rboff));
1685 } else {
1686 u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
1687 sc->sk_ramsize = ( val == 0 ) ? 0x20000 : (( val * 4 )*1024);
1688 sc->sk_rboff = SK_RBOFF_0;
1689
1690 DPRINTFN(2, ("skc_attach: ramsize=%dk (%d), rboff=%d\n",
1691 sc->sk_ramsize / 1024, sc->sk_ramsize,
1692 sc->sk_rboff));
1693 }
1694
1695 /* Read and save physical media type */
1696 switch(sk_win_read_1(sc, SK_PMDTYPE)) {
1697 case SK_PMD_1000BASESX:
1698 sc->sk_pmd = IFM_1000_SX;
1699 break;
1700 case SK_PMD_1000BASELX:
1701 sc->sk_pmd = IFM_1000_LX;
1702 break;
1703 case SK_PMD_1000BASECX:
1704 sc->sk_pmd = IFM_1000_CX;
1705 break;
1706 case SK_PMD_1000BASETX:
1707 case SK_PMD_1000BASETX_ALT:
1708 sc->sk_pmd = IFM_1000_T;
1709 break;
1710 default:
1711 aprint_error("%s: unknown media type: 0x%x\n",
1712 sc->sk_dev.dv_xname, sk_win_read_1(sc, SK_PMDTYPE));
1713 goto fail;
1714 }
1715
1716 /* determine whether to name it with vpd or just make it up */
1717 /* Marvell Yukon VPD's can freqently be bogus */
1718
1719 switch (pa->pa_id) {
1720 case PCI_ID_CODE(PCI_VENDOR_SCHNEIDERKOCH,
1721 PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE):
1722 case PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2:
1723 case PCI_PRODUCT_3COM_3C940:
1724 case PCI_PRODUCT_DLINK_DGE530T:
1725 case PCI_PRODUCT_DLINK_DGE560T:
1726 case PCI_PRODUCT_DLINK_DGE560T_2:
1727 case PCI_PRODUCT_LINKSYS_EG1032:
1728 case PCI_PRODUCT_LINKSYS_EG1064:
1729 case PCI_ID_CODE(PCI_VENDOR_SCHNEIDERKOCH,
1730 PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2):
1731 case PCI_ID_CODE(PCI_VENDOR_3COM,PCI_PRODUCT_3COM_3C940):
1732 case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE530T):
1733 case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE560T):
1734 case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE560T_2):
1735 case PCI_ID_CODE(PCI_VENDOR_LINKSYS,PCI_PRODUCT_LINKSYS_EG1032):
1736 case PCI_ID_CODE(PCI_VENDOR_LINKSYS,PCI_PRODUCT_LINKSYS_EG1064):
1737 sc->sk_name = sc->sk_vpd_prodname;
1738 break;
1739 case PCI_ID_CODE(PCI_VENDOR_GALILEO,PCI_PRODUCT_GALILEO_SKNET):
1740 /* whoops yukon vpd prodname bears no resemblance to reality */
1741 switch (sc->sk_type) {
1742 case SK_GENESIS:
1743 sc->sk_name = sc->sk_vpd_prodname;
1744 break;
1745 case SK_YUKON:
1746 sc->sk_name = "Marvell Yukon Gigabit Ethernet";
1747 break;
1748 case SK_YUKON_LITE:
1749 sc->sk_name = "Marvell Yukon Lite Gigabit Ethernet";
1750 break;
1751 case SK_YUKON_LP:
1752 sc->sk_name = "Marvell Yukon LP Gigabit Ethernet";
1753 break;
1754 default:
1755 sc->sk_name = "Marvell Yukon (Unknown) Gigabit Ethernet";
1756 }
1757
1758 /* Yukon Lite Rev A0 needs special test, from sk98lin driver */
1759
1760 if ( sc->sk_type == SK_YUKON ) {
1761 uint32_t flashaddr;
1762 uint8_t testbyte;
1763
1764 flashaddr = sk_win_read_4(sc,SK_EP_ADDR);
1765
1766 /* test Flash-Address Register */
1767 sk_win_write_1(sc,SK_EP_ADDR+3, 0xff);
1768 testbyte = sk_win_read_1(sc, SK_EP_ADDR+3);
1769
1770 if (testbyte != 0) {
1771 /* this is yukon lite Rev. A0 */
1772 sc->sk_type = SK_YUKON_LITE;
1773 sc->sk_rev = SK_YUKON_LITE_REV_A0;
1774 /* restore Flash-Address Register */
1775 sk_win_write_4(sc,SK_EP_ADDR,flashaddr);
1776 }
1777 }
1778 break;
1779 case PCI_ID_CODE(PCI_VENDOR_GALILEO,PCI_PRODUCT_GALILEO_BELKIN):
1780 sc->sk_name = sc->sk_vpd_prodname;
1781 break;
1782 default:
1783 sc->sk_name = "Unknown Marvell";
1784 }
1785
1786
1787 if ( sc->sk_type == SK_YUKON_LITE ) {
1788 switch (sc->sk_rev) {
1789 case SK_YUKON_LITE_REV_A0:
1790 revstr = "A0";
1791 break;
1792 case SK_YUKON_LITE_REV_A1:
1793 revstr = "A1";
1794 break;
1795 case SK_YUKON_LITE_REV_A3:
1796 revstr = "A3";
1797 break;
1798 default:
1799 revstr = "";
1800 }
1801 } else {
1802 revstr = "";
1803 }
1804
1805 /* Announce the product name. */
1806 aprint_normal("%s: %s rev. %s(0x%x)\n", sc->sk_dev.dv_xname,
1807 sc->sk_name, revstr, sc->sk_rev);
1808
1809 skca.skc_port = SK_PORT_A;
1810 (void)config_found(&sc->sk_dev, &skca, skcprint);
1811
1812 if (!(sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC)) {
1813 skca.skc_port = SK_PORT_B;
1814 (void)config_found(&sc->sk_dev, &skca, skcprint);
1815 }
1816
1817 /* Turn on the 'driver is loaded' LED. */
1818 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
1819
1820 /* skc sysctl setup */
1821
1822 sc->sk_int_mod = SK_IM_DEFAULT;
1823 sc->sk_int_mod_pending = 0;
1824
1825 if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1826 0, CTLTYPE_NODE, sc->sk_dev.dv_xname,
1827 SYSCTL_DESCR("skc per-controller controls"),
1828 NULL, 0, NULL, 0, CTL_HW, sk_root_num, CTL_CREATE,
1829 CTL_EOL)) != 0) {
1830 aprint_normal("%s: couldn't create sysctl node\n",
1831 sc->sk_dev.dv_xname);
1832 goto fail;
1833 }
1834
1835 sk_nodenum = node->sysctl_num;
1836
1837 /* interrupt moderation time in usecs */
1838 if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1839 CTLFLAG_READWRITE,
1840 CTLTYPE_INT, "int_mod",
1841 SYSCTL_DESCR("sk interrupt moderation timer"),
1842 sk_sysctl_handler, 0, sc,
1843 0, CTL_HW, sk_root_num, sk_nodenum, CTL_CREATE,
1844 CTL_EOL)) != 0) {
1845 aprint_normal("%s: couldn't create int_mod sysctl node\n",
1846 sc->sk_dev.dv_xname);
1847 goto fail;
1848 }
1849
1850 fail:
1851 splx(s);
1852 }
1853
1854 int
1855 sk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, u_int32_t *txidx)
1856 {
1857 struct sk_softc *sc = sc_if->sk_softc;
1858 struct sk_tx_desc *f = NULL;
1859 u_int32_t frag, cur, cnt = 0;
1860 int i;
1861 struct sk_txmap_entry *entry;
1862 bus_dmamap_t txmap;
1863
1864 DPRINTFN(3, ("sk_encap\n"));
1865
1866 entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head);
1867 if (entry == NULL) {
1868 DPRINTFN(3, ("sk_encap: no txmap available\n"));
1869 return ENOBUFS;
1870 }
1871 txmap = entry->dmamap;
1872
1873 cur = frag = *txidx;
1874
1875 #ifdef SK_DEBUG
1876 if (skdebug >= 3)
1877 sk_dump_mbuf(m_head);
1878 #endif
1879
1880 /*
1881 * Start packing the mbufs in this chain into
1882 * the fragment pointers. Stop when we run out
1883 * of fragments or hit the end of the mbuf chain.
1884 */
1885 if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
1886 BUS_DMA_NOWAIT)) {
1887 DPRINTFN(1, ("sk_encap: dmamap failed\n"));
1888 return(ENOBUFS);
1889 }
1890
1891 DPRINTFN(3, ("sk_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1892
1893 /* Sync the DMA map. */
1894 bus_dmamap_sync(sc->sc_dmatag, txmap, 0, txmap->dm_mapsize,
1895 BUS_DMASYNC_PREWRITE);
1896
1897 for (i = 0; i < txmap->dm_nsegs; i++) {
1898 if ((SK_TX_RING_CNT - (sc_if->sk_cdata.sk_tx_cnt + cnt)) < 2) {
1899 DPRINTFN(1, ("sk_encap: too few descriptors free\n"));
1900 return(ENOBUFS);
1901 }
1902 f = &sc_if->sk_rdata->sk_tx_ring[frag];
1903 f->sk_data_lo = txmap->dm_segs[i].ds_addr;
1904 f->sk_ctl = txmap->dm_segs[i].ds_len | SK_OPCODE_DEFAULT;
1905 if (cnt == 0)
1906 f->sk_ctl |= SK_TXCTL_FIRSTFRAG;
1907 else
1908 f->sk_ctl |= SK_TXCTL_OWN;
1909
1910 cur = frag;
1911 SK_INC(frag, SK_TX_RING_CNT);
1912 cnt++;
1913 }
1914
1915 sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
1916 SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
1917
1918 sc_if->sk_cdata.sk_tx_map[cur] = entry;
1919 sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |=
1920 SK_TXCTL_LASTFRAG|SK_TXCTL_EOF_INTR;
1921
1922 /* Sync descriptors before handing to chip */
1923 SK_CDTXSYNC(sc_if, *txidx, txmap->dm_nsegs,
1924 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1925
1926 sc_if->sk_rdata->sk_tx_ring[*txidx].sk_ctl |= SK_TXCTL_OWN;
1927
1928 /* Sync first descriptor to hand it off */
1929 SK_CDTXSYNC(sc_if, *txidx, 1, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1930
1931 sc_if->sk_cdata.sk_tx_cnt += cnt;
1932
1933 #ifdef SK_DEBUG
1934 if (skdebug >= 3) {
1935 struct sk_tx_desc *desc;
1936 u_int32_t idx;
1937 for (idx = *txidx; idx != frag; SK_INC(idx, SK_TX_RING_CNT)) {
1938 desc = &sc_if->sk_rdata->sk_tx_ring[idx];
1939 sk_dump_txdesc(desc, idx);
1940 }
1941 }
1942 #endif
1943
1944 *txidx = frag;
1945
1946 DPRINTFN(3, ("sk_encap: completed successfully\n"));
1947
1948 return(0);
1949 }
1950
1951 void
1952 sk_start(struct ifnet *ifp)
1953 {
1954 struct sk_if_softc *sc_if = ifp->if_softc;
1955 struct sk_softc *sc = sc_if->sk_softc;
1956 struct mbuf *m_head = NULL;
1957 u_int32_t idx = sc_if->sk_cdata.sk_tx_prod;
1958 int pkts = 0;
1959
1960 DPRINTFN(3, ("sk_start (idx %d, tx_chain[idx] %p)\n", idx,
1961 sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf));
1962
1963 while(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
1964
1965 IFQ_POLL(&ifp->if_snd, m_head);
1966 if (m_head == NULL)
1967 break;
1968
1969 /*
1970 * Pack the data into the transmit ring. If we
1971 * don't have room, set the OACTIVE flag and wait
1972 * for the NIC to drain the ring.
1973 */
1974 if (sk_encap(sc_if, m_head, &idx)) {
1975 ifp->if_flags |= IFF_OACTIVE;
1976 break;
1977 }
1978
1979 /* now we are committed to transmit the packet */
1980 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1981 pkts++;
1982
1983 /*
1984 * If there's a BPF listener, bounce a copy of this frame
1985 * to him.
1986 */
1987 #if NBPFILTER > 0
1988 if (ifp->if_bpf)
1989 bpf_mtap(ifp->if_bpf, m_head);
1990 #endif
1991 }
1992 if (pkts == 0)
1993 return;
1994
1995 /* Transmit */
1996 if (idx != sc_if->sk_cdata.sk_tx_prod) {
1997 sc_if->sk_cdata.sk_tx_prod = idx;
1998 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
1999
2000 /* Set a timeout in case the chip goes out to lunch. */
2001 ifp->if_timer = 5;
2002 }
2003 }
2004
2005
2006 void
2007 sk_watchdog(struct ifnet *ifp)
2008 {
2009 struct sk_if_softc *sc_if = ifp->if_softc;
2010
2011 printf("%s: watchdog timeout\n", sc_if->sk_dev.dv_xname);
2012 (void) sk_init(ifp);
2013 }
2014
2015 void
2016 sk_shutdown(void * v)
2017 {
2018 struct sk_if_softc *sc_if = (struct sk_if_softc *)v;
2019 struct sk_softc *sc = sc_if->sk_softc;
2020 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2021
2022 DPRINTFN(2, ("sk_shutdown\n"));
2023 sk_stop(ifp,1);
2024
2025 /* Turn off the 'driver is loaded' LED. */
2026 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
2027
2028 /*
2029 * Reset the GEnesis controller. Doing this should also
2030 * assert the resets on the attached XMAC(s).
2031 */
2032 sk_reset(sc);
2033 }
2034
2035 void
2036 sk_rxeof(struct sk_if_softc *sc_if)
2037 {
2038 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2039 struct mbuf *m;
2040 struct sk_chain *cur_rx;
2041 struct sk_rx_desc *cur_desc;
2042 int i, cur, total_len = 0;
2043 u_int32_t rxstat;
2044 bus_dmamap_t dmamap;
2045
2046 i = sc_if->sk_cdata.sk_rx_prod;
2047
2048 DPRINTFN(3, ("sk_rxeof %d\n", i));
2049
2050 for (;;) {
2051 cur = i;
2052
2053 /* Sync the descriptor */
2054 SK_CDRXSYNC(sc_if, cur,
2055 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2056
2057 if (sc_if->sk_rdata->sk_rx_ring[cur].sk_ctl & SK_RXCTL_OWN) {
2058 /* Invalidate the descriptor -- it's not ready yet */
2059 SK_CDRXSYNC(sc_if, cur, BUS_DMASYNC_PREREAD);
2060 sc_if->sk_cdata.sk_rx_prod = i;
2061 break;
2062 }
2063
2064 cur_rx = &sc_if->sk_cdata.sk_rx_chain[cur];
2065 cur_desc = &sc_if->sk_rdata->sk_rx_ring[cur];
2066 dmamap = sc_if->sk_cdata.sk_rx_jumbo_map;
2067
2068 bus_dmamap_sync(sc_if->sk_softc->sc_dmatag, dmamap, 0,
2069 dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
2070
2071 rxstat = cur_desc->sk_xmac_rxstat;
2072 m = cur_rx->sk_mbuf;
2073 cur_rx->sk_mbuf = NULL;
2074 total_len = SK_RXBYTES(cur_desc->sk_ctl);
2075
2076 sc_if->sk_cdata.sk_rx_map[cur] = 0;
2077
2078 SK_INC(i, SK_RX_RING_CNT);
2079
2080 if (rxstat & XM_RXSTAT_ERRFRAME) {
2081 ifp->if_ierrors++;
2082 sk_newbuf(sc_if, cur, m, dmamap);
2083 continue;
2084 }
2085
2086 /*
2087 * Try to allocate a new jumbo buffer. If that
2088 * fails, copy the packet to mbufs and put the
2089 * jumbo buffer back in the ring so it can be
2090 * re-used. If allocating mbufs fails, then we
2091 * have to drop the packet.
2092 */
2093 if (sk_newbuf(sc_if, cur, NULL, dmamap) == ENOBUFS) {
2094 struct mbuf *m0;
2095 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
2096 total_len + ETHER_ALIGN, 0, ifp, NULL);
2097 sk_newbuf(sc_if, cur, m, dmamap);
2098 if (m0 == NULL) {
2099 printf("%s: no receive buffers "
2100 "available -- packet dropped!\n",
2101 sc_if->sk_dev.dv_xname);
2102 ifp->if_ierrors++;
2103 continue;
2104 }
2105 m_adj(m0, ETHER_ALIGN);
2106 m = m0;
2107 } else {
2108 m->m_pkthdr.rcvif = ifp;
2109 m->m_pkthdr.len = m->m_len = total_len;
2110 }
2111
2112 ifp->if_ipackets++;
2113
2114 #if NBPFILTER > 0
2115 if (ifp->if_bpf)
2116 bpf_mtap(ifp->if_bpf, m);
2117 #endif
2118 /* pass it on. */
2119 (*ifp->if_input)(ifp, m);
2120 }
2121 }
2122
2123 void
2124 sk_txeof(struct sk_if_softc *sc_if)
2125 {
2126 struct sk_softc *sc = sc_if->sk_softc;
2127 struct sk_tx_desc *cur_tx;
2128 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2129 u_int32_t idx;
2130 struct sk_txmap_entry *entry;
2131
2132 DPRINTFN(3, ("sk_txeof\n"));
2133
2134 /*
2135 * Go through our tx ring and free mbufs for those
2136 * frames that have been sent.
2137 */
2138 idx = sc_if->sk_cdata.sk_tx_cons;
2139 while(idx != sc_if->sk_cdata.sk_tx_prod) {
2140 SK_CDTXSYNC(sc_if, idx, 1,
2141 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2142
2143 cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
2144 #ifdef SK_DEBUG
2145 if (skdebug >= 3)
2146 sk_dump_txdesc(cur_tx, idx);
2147 #endif
2148 if (cur_tx->sk_ctl & SK_TXCTL_OWN) {
2149 SK_CDTXSYNC(sc_if, idx, 1, BUS_DMASYNC_PREREAD);
2150 break;
2151 }
2152 if (cur_tx->sk_ctl & SK_TXCTL_LASTFRAG)
2153 ifp->if_opackets++;
2154 if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
2155 entry = sc_if->sk_cdata.sk_tx_map[idx];
2156
2157 m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
2158 sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
2159
2160 bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
2161 entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2162
2163 bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
2164 SIMPLEQ_INSERT_TAIL(&sc_if->sk_txmap_head, entry,
2165 link);
2166 sc_if->sk_cdata.sk_tx_map[idx] = NULL;
2167 }
2168 sc_if->sk_cdata.sk_tx_cnt--;
2169 SK_INC(idx, SK_TX_RING_CNT);
2170 }
2171 if (sc_if->sk_cdata.sk_tx_cnt == 0)
2172 ifp->if_timer = 0;
2173 else /* nudge chip to keep tx ring moving */
2174 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
2175
2176 if (sc_if->sk_cdata.sk_tx_cnt < SK_TX_RING_CNT - 2)
2177 ifp->if_flags &= ~IFF_OACTIVE;
2178
2179 sc_if->sk_cdata.sk_tx_cons = idx;
2180 }
2181
2182 void
2183 sk_tick(void *xsc_if)
2184 {
2185 struct sk_if_softc *sc_if = xsc_if;
2186 struct mii_data *mii = &sc_if->sk_mii;
2187 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2188 int i;
2189
2190 DPRINTFN(3, ("sk_tick\n"));
2191
2192 if (!(ifp->if_flags & IFF_UP))
2193 return;
2194
2195 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2196 sk_intr_bcom(sc_if);
2197 return;
2198 }
2199
2200 /*
2201 * According to SysKonnect, the correct way to verify that
2202 * the link has come back up is to poll bit 0 of the GPIO
2203 * register three times. This pin has the signal from the
2204 * link sync pin connected to it; if we read the same link
2205 * state 3 times in a row, we know the link is up.
2206 */
2207 for (i = 0; i < 3; i++) {
2208 if (SK_XM_READ_2(sc_if, XM_GPIO) & XM_GPIO_GP0_SET)
2209 break;
2210 }
2211
2212 if (i != 3) {
2213 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2214 return;
2215 }
2216
2217 /* Turn the GP0 interrupt back on. */
2218 SK_XM_CLRBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2219 SK_XM_READ_2(sc_if, XM_ISR);
2220 mii_tick(mii);
2221 mii_pollstat(mii);
2222 callout_stop(&sc_if->sk_tick_ch);
2223 }
2224
2225 void
2226 sk_intr_bcom(struct sk_if_softc *sc_if)
2227 {
2228 struct mii_data *mii = &sc_if->sk_mii;
2229 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2230 int status;
2231
2232
2233 DPRINTFN(3, ("sk_intr_bcom\n"));
2234
2235 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2236
2237 /*
2238 * Read the PHY interrupt register to make sure
2239 * we clear any pending interrupts.
2240 */
2241 status = sk_xmac_miibus_readreg((struct device *)sc_if,
2242 SK_PHYADDR_BCOM, BRGPHY_MII_ISR);
2243
2244 if (!(ifp->if_flags & IFF_RUNNING)) {
2245 sk_init_xmac(sc_if);
2246 return;
2247 }
2248
2249 if (status & (BRGPHY_ISR_LNK_CHG|BRGPHY_ISR_AN_PR)) {
2250 int lstat;
2251 lstat = sk_xmac_miibus_readreg((struct device *)sc_if,
2252 SK_PHYADDR_BCOM, BRGPHY_MII_AUXSTS);
2253
2254 if (!(lstat & BRGPHY_AUXSTS_LINK) && sc_if->sk_link) {
2255 mii_mediachg(mii);
2256 /* Turn off the link LED. */
2257 SK_IF_WRITE_1(sc_if, 0,
2258 SK_LINKLED1_CTL, SK_LINKLED_OFF);
2259 sc_if->sk_link = 0;
2260 } else if (status & BRGPHY_ISR_LNK_CHG) {
2261 sk_xmac_miibus_writereg((struct device *)sc_if,
2262 SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFF00);
2263 mii_tick(mii);
2264 sc_if->sk_link = 1;
2265 /* Turn on the link LED. */
2266 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2267 SK_LINKLED_ON|SK_LINKLED_LINKSYNC_OFF|
2268 SK_LINKLED_BLINK_OFF);
2269 mii_pollstat(mii);
2270 } else {
2271 mii_tick(mii);
2272 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick,sc_if);
2273 }
2274 }
2275
2276 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2277 }
2278
2279 void
2280 sk_intr_xmac(struct sk_if_softc *sc_if)
2281 {
2282 u_int16_t status = SK_XM_READ_2(sc_if, XM_ISR);
2283
2284 DPRINTFN(3, ("sk_intr_xmac\n"));
2285
2286 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) {
2287 if (status & XM_ISR_GP0_SET) {
2288 SK_XM_SETBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2289 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2290 }
2291
2292 if (status & XM_ISR_AUTONEG_DONE) {
2293 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2294 }
2295 }
2296
2297 if (status & XM_IMR_TX_UNDERRUN)
2298 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_TXFIFO);
2299
2300 if (status & XM_IMR_RX_OVERRUN)
2301 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_RXFIFO);
2302 }
2303
2304 void
2305 sk_intr_yukon(sc_if)
2306 struct sk_if_softc *sc_if;
2307 {
2308 int status;
2309
2310 status = SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2311
2312 DPRINTFN(3, ("sk_intr_yukon status=%#x\n", status));
2313 }
2314
2315 int
2316 sk_intr(void *xsc)
2317 {
2318 struct sk_softc *sc = xsc;
2319 struct sk_if_softc *sc_if0 = sc->sk_if[SK_PORT_A];
2320 struct sk_if_softc *sc_if1 = sc->sk_if[SK_PORT_B];
2321 struct ifnet *ifp0 = NULL, *ifp1 = NULL;
2322 u_int32_t status;
2323 int claimed = 0;
2324
2325 if (sc_if0 != NULL)
2326 ifp0 = &sc_if0->sk_ethercom.ec_if;
2327 if (sc_if1 != NULL)
2328 ifp1 = &sc_if1->sk_ethercom.ec_if;
2329
2330 for (;;) {
2331 status = CSR_READ_4(sc, SK_ISSR);
2332 DPRINTFN(3, ("sk_intr: status=%#x\n", status));
2333
2334 if (!(status & sc->sk_intrmask))
2335 break;
2336
2337 claimed = 1;
2338
2339 /* Handle receive interrupts first. */
2340 if (sc_if0 && (status & SK_ISR_RX1_EOF)) {
2341 sk_rxeof(sc_if0);
2342 CSR_WRITE_4(sc, SK_BMU_RX_CSR0,
2343 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2344 }
2345 if (sc_if1 && (status & SK_ISR_RX2_EOF)) {
2346 sk_rxeof(sc_if1);
2347 CSR_WRITE_4(sc, SK_BMU_RX_CSR1,
2348 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2349 }
2350
2351 /* Then transmit interrupts. */
2352 if (sc_if0 && (status & SK_ISR_TX1_S_EOF)) {
2353 sk_txeof(sc_if0);
2354 CSR_WRITE_4(sc, SK_BMU_TXS_CSR0,
2355 SK_TXBMU_CLR_IRQ_EOF);
2356 }
2357 if (sc_if1 && (status & SK_ISR_TX2_S_EOF)) {
2358 sk_txeof(sc_if1);
2359 CSR_WRITE_4(sc, SK_BMU_TXS_CSR1,
2360 SK_TXBMU_CLR_IRQ_EOF);
2361 }
2362
2363 /* Then MAC interrupts. */
2364 if (sc_if0 && (status & SK_ISR_MAC1) &&
2365 (ifp0->if_flags & IFF_RUNNING)) {
2366 if (sc->sk_type == SK_GENESIS)
2367 sk_intr_xmac(sc_if0);
2368 else
2369 sk_intr_yukon(sc_if0);
2370 }
2371
2372 if (sc_if1 && (status & SK_ISR_MAC2) &&
2373 (ifp1->if_flags & IFF_RUNNING)) {
2374 if (sc->sk_type == SK_GENESIS)
2375 sk_intr_xmac(sc_if1);
2376 else
2377 sk_intr_yukon(sc_if1);
2378
2379 }
2380
2381 if (status & SK_ISR_EXTERNAL_REG) {
2382 if (sc_if0 != NULL &&
2383 sc_if0->sk_phytype == SK_PHYTYPE_BCOM)
2384 sk_intr_bcom(sc_if0);
2385
2386 if (sc_if1 != NULL &&
2387 sc_if1->sk_phytype == SK_PHYTYPE_BCOM)
2388 sk_intr_bcom(sc_if1);
2389 }
2390 }
2391
2392 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2393
2394 if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
2395 sk_start(ifp0);
2396 if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
2397 sk_start(ifp1);
2398
2399 #if NRND > 0
2400 if (RND_ENABLED(&sc->rnd_source))
2401 rnd_add_uint32(&sc->rnd_source, status);
2402 #endif
2403
2404 if (sc->sk_int_mod_pending)
2405 sk_update_int_mod(sc);
2406
2407 return (claimed);
2408 }
2409
2410 void
2411 sk_init_xmac(struct sk_if_softc *sc_if)
2412 {
2413 struct sk_softc *sc = sc_if->sk_softc;
2414 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2415 static const struct sk_bcom_hack bhack[] = {
2416 { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 }, { 0x17, 0x0013 },
2417 { 0x15, 0x0404 }, { 0x17, 0x8006 }, { 0x15, 0x0132 }, { 0x17, 0x8006 },
2418 { 0x15, 0x0232 }, { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
2419 { 0, 0 } };
2420
2421 DPRINTFN(1, ("sk_init_xmac\n"));
2422
2423 /* Unreset the XMAC. */
2424 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_UNRESET);
2425 DELAY(1000);
2426
2427 /* Reset the XMAC's internal state. */
2428 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2429
2430 /* Save the XMAC II revision */
2431 sc_if->sk_xmac_rev = XM_XMAC_REV(SK_XM_READ_4(sc_if, XM_DEVID));
2432
2433 /*
2434 * Perform additional initialization for external PHYs,
2435 * namely for the 1000baseTX cards that use the XMAC's
2436 * GMII mode.
2437 */
2438 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2439 int i = 0;
2440 u_int32_t val;
2441
2442 /* Take PHY out of reset. */
2443 val = sk_win_read_4(sc, SK_GPIO);
2444 if (sc_if->sk_port == SK_PORT_A)
2445 val |= SK_GPIO_DIR0|SK_GPIO_DAT0;
2446 else
2447 val |= SK_GPIO_DIR2|SK_GPIO_DAT2;
2448 sk_win_write_4(sc, SK_GPIO, val);
2449
2450 /* Enable GMII mode on the XMAC. */
2451 SK_XM_SETBIT_2(sc_if, XM_HWCFG, XM_HWCFG_GMIIMODE);
2452
2453 sk_xmac_miibus_writereg((struct device *)sc_if,
2454 SK_PHYADDR_BCOM, MII_BMCR, BMCR_RESET);
2455 DELAY(10000);
2456 sk_xmac_miibus_writereg((struct device *)sc_if,
2457 SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFFF0);
2458
2459 /*
2460 * Early versions of the BCM5400 apparently have
2461 * a bug that requires them to have their reserved
2462 * registers initialized to some magic values. I don't
2463 * know what the numbers do, I'm just the messenger.
2464 */
2465 if (sk_xmac_miibus_readreg((struct device *)sc_if,
2466 SK_PHYADDR_BCOM, 0x03) == 0x6041) {
2467 while(bhack[i].reg) {
2468 sk_xmac_miibus_writereg((struct device *)sc_if,
2469 SK_PHYADDR_BCOM, bhack[i].reg,
2470 bhack[i].val);
2471 i++;
2472 }
2473 }
2474 }
2475
2476 /* Set station address */
2477 SK_XM_WRITE_2(sc_if, XM_PAR0,
2478 *(u_int16_t *)(&sc_if->sk_enaddr[0]));
2479 SK_XM_WRITE_2(sc_if, XM_PAR1,
2480 *(u_int16_t *)(&sc_if->sk_enaddr[2]));
2481 SK_XM_WRITE_2(sc_if, XM_PAR2,
2482 *(u_int16_t *)(&sc_if->sk_enaddr[4]));
2483 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_STATION);
2484
2485 if (ifp->if_flags & IFF_PROMISC) {
2486 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
2487 } else {
2488 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
2489 }
2490
2491 if (ifp->if_flags & IFF_BROADCAST) {
2492 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2493 } else {
2494 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2495 }
2496
2497 /* We don't need the FCS appended to the packet. */
2498 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_STRIPFCS);
2499
2500 /* We want short frames padded to 60 bytes. */
2501 SK_XM_SETBIT_2(sc_if, XM_TXCMD, XM_TXCMD_AUTOPAD);
2502
2503 /*
2504 * Enable the reception of all error frames. This is is
2505 * a necessary evil due to the design of the XMAC. The
2506 * XMAC's receive FIFO is only 8K in size, however jumbo
2507 * frames can be up to 9000 bytes in length. When bad
2508 * frame filtering is enabled, the XMAC's RX FIFO operates
2509 * in 'store and forward' mode. For this to work, the
2510 * entire frame has to fit into the FIFO, but that means
2511 * that jumbo frames larger than 8192 bytes will be
2512 * truncated. Disabling all bad frame filtering causes
2513 * the RX FIFO to operate in streaming mode, in which
2514 * case the XMAC will start transfering frames out of the
2515 * RX FIFO as soon as the FIFO threshold is reached.
2516 */
2517 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_BADFRAMES|
2518 XM_MODE_RX_GIANTS|XM_MODE_RX_RUNTS|XM_MODE_RX_CRCERRS|
2519 XM_MODE_RX_INRANGELEN);
2520
2521 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2522 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2523 else
2524 SK_XM_CLRBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2525
2526 /*
2527 * Bump up the transmit threshold. This helps hold off transmit
2528 * underruns when we're blasting traffic from both ports at once.
2529 */
2530 SK_XM_WRITE_2(sc_if, XM_TX_REQTHRESH, SK_XM_TX_FIFOTHRESH);
2531
2532 /* Set multicast filter */
2533 sk_setmulti(sc_if);
2534
2535 /* Clear and enable interrupts */
2536 SK_XM_READ_2(sc_if, XM_ISR);
2537 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC)
2538 SK_XM_WRITE_2(sc_if, XM_IMR, XM_INTRS);
2539 else
2540 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2541
2542 /* Configure MAC arbiter */
2543 switch(sc_if->sk_xmac_rev) {
2544 case XM_XMAC_REV_B2:
2545 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_B2);
2546 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_B2);
2547 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_B2);
2548 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_B2);
2549 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_B2);
2550 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_B2);
2551 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_B2);
2552 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_B2);
2553 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2554 break;
2555 case XM_XMAC_REV_C1:
2556 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_C1);
2557 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_C1);
2558 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_C1);
2559 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_C1);
2560 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_C1);
2561 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_C1);
2562 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_C1);
2563 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_C1);
2564 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2565 break;
2566 default:
2567 break;
2568 }
2569 sk_win_write_2(sc, SK_MACARB_CTL,
2570 SK_MACARBCTL_UNRESET|SK_MACARBCTL_FASTOE_OFF);
2571
2572 sc_if->sk_link = 1;
2573 }
2574
2575 void sk_init_yukon(sc_if)
2576 struct sk_if_softc *sc_if;
2577 {
2578 u_int32_t /*mac, */phy;
2579 u_int16_t reg;
2580 struct sk_softc *sc;
2581 int i;
2582
2583 DPRINTFN(1, ("sk_init_yukon: start: sk_csr=%#x\n",
2584 CSR_READ_4(sc_if->sk_softc, SK_CSR)));
2585
2586 sc = sc_if->sk_softc;
2587 if (sc->sk_type == SK_YUKON_LITE &&
2588 sc->sk_rev >= SK_YUKON_LITE_REV_A3) {
2589 /* Take PHY out of reset. */
2590 sk_win_write_4(sc, SK_GPIO,
2591 (sk_win_read_4(sc, SK_GPIO) | SK_GPIO_DIR9) & ~SK_GPIO_DAT9);
2592 }
2593
2594
2595 /* GMAC and GPHY Reset */
2596 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
2597
2598 DPRINTFN(6, ("sk_init_yukon: 1\n"));
2599
2600 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2601 DELAY(1000);
2602 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_CLEAR);
2603 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2604 DELAY(1000);
2605
2606
2607 DPRINTFN(6, ("sk_init_yukon: 2\n"));
2608
2609 phy = SK_GPHY_INT_POL_HI | SK_GPHY_DIS_FC | SK_GPHY_DIS_SLEEP |
2610 SK_GPHY_ENA_XC | SK_GPHY_ANEG_ALL | SK_GPHY_ENA_PAUSE;
2611
2612 switch(sc_if->sk_softc->sk_pmd) {
2613 case IFM_1000_SX:
2614 case IFM_1000_LX:
2615 phy |= SK_GPHY_FIBER;
2616 break;
2617
2618 case IFM_1000_CX:
2619 case IFM_1000_T:
2620 phy |= SK_GPHY_COPPER;
2621 break;
2622 }
2623
2624 DPRINTFN(3, ("sk_init_yukon: phy=%#x\n", phy));
2625
2626 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_SET);
2627 DELAY(1000);
2628 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_CLEAR);
2629 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
2630 SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
2631
2632 DPRINTFN(3, ("sk_init_yukon: gmac_ctrl=%#x\n",
2633 SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
2634
2635 DPRINTFN(6, ("sk_init_yukon: 3\n"));
2636
2637 /* unused read of the interrupt source register */
2638 DPRINTFN(6, ("sk_init_yukon: 4\n"));
2639 SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2640
2641 DPRINTFN(6, ("sk_init_yukon: 4a\n"));
2642 reg = SK_YU_READ_2(sc_if, YUKON_PAR);
2643 DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
2644
2645 /* MIB Counter Clear Mode set */
2646 reg |= YU_PAR_MIB_CLR;
2647 DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
2648 DPRINTFN(6, ("sk_init_yukon: 4b\n"));
2649 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2650
2651 /* MIB Counter Clear Mode clear */
2652 DPRINTFN(6, ("sk_init_yukon: 5\n"));
2653 reg &= ~YU_PAR_MIB_CLR;
2654 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2655
2656 /* receive control reg */
2657 DPRINTFN(6, ("sk_init_yukon: 7\n"));
2658 SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_UFLEN | YU_RCR_MUFLEN |
2659 YU_RCR_CRCR);
2660
2661 /* transmit parameter register */
2662 DPRINTFN(6, ("sk_init_yukon: 8\n"));
2663 SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
2664 YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) );
2665
2666 /* serial mode register */
2667 DPRINTFN(6, ("sk_init_yukon: 9\n"));
2668 SK_YU_WRITE_2(sc_if, YUKON_SMR, YU_SMR_DATA_BLIND(0x1c) |
2669 YU_SMR_MFL_VLAN | YU_SMR_MFL_JUMBO |
2670 YU_SMR_IPG_DATA(0x1e));
2671
2672 DPRINTFN(6, ("sk_init_yukon: 10\n"));
2673 /* Setup Yukon's address */
2674 for (i = 0; i < 3; i++) {
2675 /* Write Source Address 1 (unicast filter) */
2676 SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
2677 sc_if->sk_enaddr[i * 2] |
2678 sc_if->sk_enaddr[i * 2 + 1] << 8);
2679 }
2680
2681 for (i = 0; i < 3; i++) {
2682 reg = sk_win_read_2(sc_if->sk_softc,
2683 SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
2684 SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
2685 }
2686
2687 /* Set multicast filter */
2688 DPRINTFN(6, ("sk_init_yukon: 11\n"));
2689 sk_setmulti(sc_if);
2690
2691 /* enable interrupt mask for counter overflows */
2692 DPRINTFN(6, ("sk_init_yukon: 12\n"));
2693 SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
2694 SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
2695 SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
2696
2697 /* Configure RX MAC FIFO */
2698 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
2699 SK_IF_WRITE_4(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON);
2700
2701 /* Configure TX MAC FIFO */
2702 SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
2703 SK_IF_WRITE_4(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
2704
2705 DPRINTFN(6, ("sk_init_yukon: end\n"));
2706 }
2707
2708 /*
2709 * Note that to properly initialize any part of the GEnesis chip,
2710 * you first have to take it out of reset mode.
2711 */
2712 int
2713 sk_init(struct ifnet *ifp)
2714 {
2715 struct sk_if_softc *sc_if = ifp->if_softc;
2716 struct sk_softc *sc = sc_if->sk_softc;
2717 struct mii_data *mii = &sc_if->sk_mii;
2718 int s;
2719 u_int32_t imr, sk_imtimer_ticks;
2720
2721 DPRINTFN(1, ("sk_init\n"));
2722
2723 s = splnet();
2724
2725 if (ifp->if_flags & IFF_RUNNING) {
2726 splx(s);
2727 return 0;
2728 }
2729
2730 /* Cancel pending I/O and free all RX/TX buffers. */
2731 sk_stop(ifp,0);
2732
2733 if (sc->sk_type == SK_GENESIS) {
2734 /* Configure LINK_SYNC LED */
2735 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_ON);
2736 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2737 SK_LINKLED_LINKSYNC_ON);
2738
2739 /* Configure RX LED */
2740 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL,
2741 SK_RXLEDCTL_COUNTER_START);
2742
2743 /* Configure TX LED */
2744 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL,
2745 SK_TXLEDCTL_COUNTER_START);
2746 }
2747
2748 /* Configure I2C registers */
2749
2750 /* Configure XMAC(s) */
2751 switch (sc->sk_type) {
2752 case SK_GENESIS:
2753 sk_init_xmac(sc_if);
2754 break;
2755 case SK_YUKON:
2756 case SK_YUKON_LITE:
2757 case SK_YUKON_LP:
2758 sk_init_yukon(sc_if);
2759 break;
2760 }
2761 mii_mediachg(mii);
2762
2763 if (sc->sk_type == SK_GENESIS) {
2764 /* Configure MAC FIFOs */
2765 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_UNRESET);
2766 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_END, SK_FIFO_END);
2767 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_ON);
2768
2769 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_UNRESET);
2770 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_END, SK_FIFO_END);
2771 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_ON);
2772 }
2773
2774 /* Configure transmit arbiter(s) */
2775 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL,
2776 SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
2777
2778 /* Configure RAMbuffers */
2779 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
2780 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
2781 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
2782 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
2783 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
2784 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
2785
2786 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_UNRESET);
2787 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_STORENFWD_ON);
2788 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_START, sc_if->sk_tx_ramstart);
2789 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_WR_PTR, sc_if->sk_tx_ramstart);
2790 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_RD_PTR, sc_if->sk_tx_ramstart);
2791 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_END, sc_if->sk_tx_ramend);
2792 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_ON);
2793
2794 /* Configure BMUs */
2795 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_ONLINE);
2796 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO,
2797 SK_RX_RING_ADDR(sc_if, 0));
2798 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 0);
2799
2800 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_ONLINE);
2801 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_LO,
2802 SK_TX_RING_ADDR(sc_if, 0));
2803 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_HI, 0);
2804
2805 /* Init descriptors */
2806 if (sk_init_rx_ring(sc_if) == ENOBUFS) {
2807 printf("%s: initialization failed: no "
2808 "memory for rx buffers\n", sc_if->sk_dev.dv_xname);
2809 sk_stop(ifp,0);
2810 splx(s);
2811 return(ENOBUFS);
2812 }
2813
2814 if (sk_init_tx_ring(sc_if) == ENOBUFS) {
2815 printf("%s: initialization failed: no "
2816 "memory for tx buffers\n", sc_if->sk_dev.dv_xname);
2817 sk_stop(ifp,0);
2818 splx(s);
2819 return(ENOBUFS);
2820 }
2821
2822 /* Set interrupt moderation if changed via sysctl. */
2823 switch (sc->sk_type) {
2824 case SK_GENESIS:
2825 sk_imtimer_ticks = SK_IMTIMER_TICKS_GENESIS;
2826 break;
2827 case SK_YUKON_EC:
2828 sk_imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
2829 break;
2830 default:
2831 sk_imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
2832 }
2833 imr = sk_win_read_4(sc, SK_IMTIMERINIT);
2834 if (imr != SK_IM_USECS(sc->sk_int_mod)) {
2835 sk_win_write_4(sc, SK_IMTIMERINIT,
2836 SK_IM_USECS(sc->sk_int_mod));
2837 aprint_verbose("%s: interrupt moderation is %d us\n",
2838 sc->sk_dev.dv_xname, sc->sk_int_mod);
2839 }
2840
2841 /* Configure interrupt handling */
2842 CSR_READ_4(sc, SK_ISSR);
2843 if (sc_if->sk_port == SK_PORT_A)
2844 sc->sk_intrmask |= SK_INTRS1;
2845 else
2846 sc->sk_intrmask |= SK_INTRS2;
2847
2848 sc->sk_intrmask |= SK_ISR_EXTERNAL_REG;
2849
2850 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2851
2852 /* Start BMUs. */
2853 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_START);
2854
2855 if (sc->sk_type == SK_GENESIS) {
2856 /* Enable XMACs TX and RX state machines */
2857 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_IGNPAUSE);
2858 SK_XM_SETBIT_2(sc_if, XM_MMUCMD,
2859 XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2860 }
2861
2862 if (SK_YUKON_FAMILY(sc->sk_type)) {
2863 u_int16_t reg = SK_YU_READ_2(sc_if, YUKON_GPCR);
2864 reg |= YU_GPCR_TXEN | YU_GPCR_RXEN;
2865 reg &= ~(YU_GPCR_SPEED_EN | YU_GPCR_DPLX_EN);
2866 SK_YU_WRITE_2(sc_if, YUKON_GPCR, reg);
2867 }
2868
2869
2870 ifp->if_flags |= IFF_RUNNING;
2871 ifp->if_flags &= ~IFF_OACTIVE;
2872
2873 splx(s);
2874 return(0);
2875 }
2876
2877 void
2878 sk_stop(struct ifnet *ifp, int disable)
2879 {
2880 struct sk_if_softc *sc_if = ifp->if_softc;
2881 struct sk_softc *sc = sc_if->sk_softc;
2882 //struct sk_txmap_entry *dma;
2883 int i;
2884
2885 DPRINTFN(1, ("sk_stop\n"));
2886
2887 callout_stop(&sc_if->sk_tick_ch);
2888
2889 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2890 u_int32_t val;
2891
2892 /* Put PHY back into reset. */
2893 val = sk_win_read_4(sc, SK_GPIO);
2894 if (sc_if->sk_port == SK_PORT_A) {
2895 val |= SK_GPIO_DIR0;
2896 val &= ~SK_GPIO_DAT0;
2897 } else {
2898 val |= SK_GPIO_DIR2;
2899 val &= ~SK_GPIO_DAT2;
2900 }
2901 sk_win_write_4(sc, SK_GPIO, val);
2902 }
2903
2904 /* Turn off various components of this interface. */
2905 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2906 switch (sc->sk_type) {
2907 case SK_GENESIS:
2908 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL,
2909 SK_TXMACCTL_XMAC_RESET);
2910 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_RESET);
2911 break;
2912 case SK_YUKON:
2913 case SK_YUKON_LITE:
2914 case SK_YUKON_LP:
2915 SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
2916 SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
2917 break;
2918 }
2919 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
2920 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2921 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_OFFLINE);
2922 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2923 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
2924 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2925 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2926 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
2927 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
2928
2929 /* Disable interrupts */
2930 if (sc_if->sk_port == SK_PORT_A)
2931 sc->sk_intrmask &= ~SK_INTRS1;
2932 else
2933 sc->sk_intrmask &= ~SK_INTRS2;
2934 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2935
2936 SK_XM_READ_2(sc_if, XM_ISR);
2937 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2938
2939 /* Free RX and TX mbufs still in the queues. */
2940 for (i = 0; i < SK_RX_RING_CNT; i++) {
2941 if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
2942 m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
2943 sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
2944 }
2945 }
2946
2947 for (i = 0; i < SK_TX_RING_CNT; i++) {
2948 if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
2949 m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
2950 sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
2951 }
2952 }
2953
2954 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2955 }
2956
2957 CFATTACH_DECL(skc,sizeof(struct sk_softc), skc_probe, skc_attach, NULL, NULL);
2958
2959 /*
2960 struct cfdriver skc_cd = {
2961 0, "skc", DV_DULL
2962 };
2963 */
2964
2965 CFATTACH_DECL(sk,sizeof(struct sk_if_softc), sk_probe, sk_attach, NULL, NULL);
2966
2967 /*
2968 struct cfdriver sk_cd = {
2969 0, "sk", DV_IFNET
2970 };
2971 */
2972
2973 #ifdef SK_DEBUG
2974 void
2975 sk_dump_txdesc(struct sk_tx_desc *desc, int idx)
2976 {
2977 #define DESC_PRINT(X) \
2978 if (desc->X) \
2979 printf("txdesc[%d]." #X "=%#x\n", \
2980 idx, desc->X);
2981
2982 DESC_PRINT(sk_ctl);
2983 DESC_PRINT(sk_next);
2984 DESC_PRINT(sk_data_lo);
2985 DESC_PRINT(sk_data_hi);
2986 DESC_PRINT(sk_xmac_txstat);
2987 DESC_PRINT(sk_rsvd0);
2988 DESC_PRINT(sk_csum_startval);
2989 DESC_PRINT(sk_csum_startpos);
2990 DESC_PRINT(sk_csum_writepos);
2991 DESC_PRINT(sk_rsvd1);
2992 #undef PRINT
2993 }
2994
2995 void
2996 sk_dump_bytes(const char *data, int len)
2997 {
2998 int c, i, j;
2999
3000 for (i = 0; i < len; i += 16) {
3001 printf("%08x ", i);
3002 c = len - i;
3003 if (c > 16) c = 16;
3004
3005 for (j = 0; j < c; j++) {
3006 printf("%02x ", data[i + j] & 0xff);
3007 if ((j & 0xf) == 7 && j > 0)
3008 printf(" ");
3009 }
3010
3011 for (; j < 16; j++)
3012 printf(" ");
3013 printf(" ");
3014
3015 for (j = 0; j < c; j++) {
3016 int ch = data[i + j] & 0xff;
3017 printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
3018 }
3019
3020 printf("\n");
3021
3022 if (c < 16)
3023 break;
3024 }
3025 }
3026
3027 void
3028 sk_dump_mbuf(struct mbuf *m)
3029 {
3030 int count = m->m_pkthdr.len;
3031
3032 printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
3033
3034 while (count > 0 && m) {
3035 printf("m=%p, m->m_data=%p, m->m_len=%d\n",
3036 m, m->m_data, m->m_len);
3037 sk_dump_bytes(mtod(m, char *), m->m_len);
3038
3039 count -= m->m_len;
3040 m = m->m_next;
3041 }
3042 }
3043 #endif
3044
3045 static int
3046 sk_sysctl_handler(SYSCTLFN_ARGS)
3047 {
3048 int error, t;
3049 struct sysctlnode node;
3050 struct sk_softc *sc;
3051
3052 node = *rnode;
3053 sc = node.sysctl_data;
3054 t = sc->sk_int_mod;
3055 node.sysctl_data = &t;
3056 error = sysctl_lookup(SYSCTLFN_CALL(&node));
3057 if (error || newp == NULL)
3058 return (error);
3059
3060 if (t < SK_IM_MIN || t > SK_IM_MAX)
3061 return (EINVAL);
3062
3063 /* update the softc with sysctl-changed value, and mark
3064 for hardware update */
3065 sc->sk_int_mod = t;
3066 sc->sk_int_mod_pending = 1;
3067 return (0);
3068 }
3069
3070 /*
3071 * Set up sysctl(3) MIB, hw.sk.* - Individual controllers will be
3072 * set up in skc_attach()
3073 */
3074 SYSCTL_SETUP(sysctl_sk, "sysctl sk subtree setup")
3075 {
3076 int rc;
3077 const struct sysctlnode *node;
3078
3079 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
3080 0, CTLTYPE_NODE, "hw", NULL,
3081 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
3082 goto err;
3083 }
3084
3085 if ((rc = sysctl_createv(clog, 0, NULL, &node,
3086 0, CTLTYPE_NODE, "sk",
3087 SYSCTL_DESCR("sk interface controls"),
3088 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
3089 goto err;
3090 }
3091
3092 sk_root_num = node->sysctl_num;
3093 return;
3094
3095 err:
3096 printf("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
3097 }
3098