if_sk.c revision 1.26 1 /* $NetBSD: if_sk.c,v 1.26 2006/05/31 21:46:32 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 pcireg_t memtype;
1531 pci_intr_handle_t ih;
1532 const char *intrstr = NULL;
1533 bus_addr_t iobase;
1534 bus_size_t iosize;
1535 int s, rc, sk_nodenum;
1536 u_int32_t command;
1537 const char *revstr;
1538 const struct sysctlnode *node;
1539
1540 DPRINTFN(2, ("begin skc_attach\n"));
1541
1542 s = splnet();
1543
1544 /*
1545 * Handle power management nonsense.
1546 */
1547 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_CAPID) & 0x000000FF;
1548
1549 if (command == 0x01) {
1550 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_PWRMGMTCTRL);
1551 if (command & SK_PSTATE_MASK) {
1552 u_int32_t xiobase, membase, irq;
1553
1554 /* Save important PCI config data. */
1555 xiobase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOIO);
1556 membase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOMEM);
1557 irq = pci_conf_read(pc, pa->pa_tag, SK_PCI_INTLINE);
1558
1559 /* Reset the power state. */
1560 aprint_normal("%s chip is in D%d power mode "
1561 "-- setting to D0\n", sc->sk_dev.dv_xname,
1562 command & SK_PSTATE_MASK);
1563 command &= 0xFFFFFFFC;
1564 pci_conf_write(pc, pa->pa_tag,
1565 SK_PCI_PWRMGMTCTRL, command);
1566
1567 /* Restore PCI config data. */
1568 pci_conf_write(pc, pa->pa_tag, SK_PCI_LOIO, xiobase);
1569 pci_conf_write(pc, pa->pa_tag, SK_PCI_LOMEM, membase);
1570 pci_conf_write(pc, pa->pa_tag, SK_PCI_INTLINE, irq);
1571 }
1572 }
1573
1574 /*
1575 * Map control/status registers.
1576 */
1577 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1578 command |= PCI_COMMAND_IO_ENABLE |
1579 PCI_COMMAND_MEM_ENABLE |
1580 PCI_COMMAND_MASTER_ENABLE;
1581 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
1582 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1583
1584 #ifdef SK_USEIOSPACE
1585 if (!(command & PCI_COMMAND_IO_ENABLE)) {
1586 aprint_error(": failed to enable I/O ports!\n");
1587 goto fail;
1588 }
1589 /*
1590 * Map control/status registers.
1591 */
1592 if (pci_mapreg_map(pa, SK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
1593 &iobase, &iosize)) {
1594 aprint_error(": can't find i/o space\n");
1595 goto fail;
1596 }
1597 #else
1598 if (!(command & PCI_COMMAND_MEM_ENABLE)) {
1599 aprint_error(": failed to enable memory mapping!\n");
1600 goto fail;
1601 }
1602 memtype = pci_mapreg_type(pc, pa->pa_tag, SK_PCI_LOMEM);
1603 switch (memtype) {
1604 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1605 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1606 if (pci_mapreg_map(pa, SK_PCI_LOMEM,
1607 memtype, 0, &sc->sk_btag, &sc->sk_bhandle,
1608 &iobase, &iosize) == 0)
1609 break;
1610 default:
1611 aprint_error("%s: can't find mem space\n",
1612 sc->sk_dev.dv_xname);
1613 return;
1614 }
1615
1616 DPRINTFN(2, ("skc_attach: iobase=%lx, iosize=%lx\n", iobase, iosize));
1617 #endif
1618 sc->sc_dmatag = pa->pa_dmat;
1619
1620 sc->sk_type = sk_win_read_1(sc, SK_CHIPVER);
1621 sc->sk_rev = (sk_win_read_1(sc, SK_CONFIG) >> 4);
1622
1623 /* bail out here if chip is not recognized */
1624 if ( sc->sk_type != SK_GENESIS && ! SK_YUKON_FAMILY(sc->sk_type)) {
1625 aprint_error("%s: unknown chip type\n",sc->sk_dev.dv_xname);
1626 goto fail;
1627 }
1628 DPRINTFN(2, ("skc_attach: allocate interrupt\n"));
1629
1630 /* Allocate interrupt */
1631 if (pci_intr_map(pa, &ih)) {
1632 aprint_error(": couldn't map interrupt\n");
1633 goto fail;
1634 }
1635
1636 intrstr = pci_intr_string(pc, ih);
1637 sc->sk_intrhand = pci_intr_establish(pc, ih, IPL_NET, sk_intr, sc);
1638 if (sc->sk_intrhand == NULL) {
1639 aprint_error(": couldn't establish interrupt");
1640 if (intrstr != NULL)
1641 aprint_normal(" at %s", intrstr);
1642 goto fail;
1643 }
1644 aprint_normal(": %s\n", intrstr);
1645
1646 /* Reset the adapter. */
1647 sk_reset(sc);
1648
1649 /* Read and save vital product data from EEPROM. */
1650 sk_vpd_read(sc);
1651
1652 if (sc->sk_type == SK_GENESIS) {
1653 u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
1654 /* Read and save RAM size and RAMbuffer offset */
1655 switch(val) {
1656 case SK_RAMSIZE_512K_64:
1657 sc->sk_ramsize = 0x80000;
1658 sc->sk_rboff = SK_RBOFF_0;
1659 break;
1660 case SK_RAMSIZE_1024K_64:
1661 sc->sk_ramsize = 0x100000;
1662 sc->sk_rboff = SK_RBOFF_80000;
1663 break;
1664 case SK_RAMSIZE_1024K_128:
1665 sc->sk_ramsize = 0x100000;
1666 sc->sk_rboff = SK_RBOFF_0;
1667 break;
1668 case SK_RAMSIZE_2048K_128:
1669 sc->sk_ramsize = 0x200000;
1670 sc->sk_rboff = SK_RBOFF_0;
1671 break;
1672 default:
1673 aprint_error("%s: unknown ram size: %d\n",
1674 sc->sk_dev.dv_xname, val);
1675 goto fail;
1676 break;
1677 }
1678
1679 DPRINTFN(2, ("skc_attach: ramsize=%d(%dk), rboff=%d\n",
1680 sc->sk_ramsize, sc->sk_ramsize / 1024,
1681 sc->sk_rboff));
1682 } else {
1683 u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
1684 sc->sk_ramsize = ( val == 0 ) ? 0x20000 : (( val * 4 )*1024);
1685 sc->sk_rboff = SK_RBOFF_0;
1686
1687 DPRINTFN(2, ("skc_attach: ramsize=%dk (%d), rboff=%d\n",
1688 sc->sk_ramsize / 1024, sc->sk_ramsize,
1689 sc->sk_rboff));
1690 }
1691
1692 /* Read and save physical media type */
1693 switch(sk_win_read_1(sc, SK_PMDTYPE)) {
1694 case SK_PMD_1000BASESX:
1695 sc->sk_pmd = IFM_1000_SX;
1696 break;
1697 case SK_PMD_1000BASELX:
1698 sc->sk_pmd = IFM_1000_LX;
1699 break;
1700 case SK_PMD_1000BASECX:
1701 sc->sk_pmd = IFM_1000_CX;
1702 break;
1703 case SK_PMD_1000BASETX:
1704 case SK_PMD_1000BASETX_ALT:
1705 sc->sk_pmd = IFM_1000_T;
1706 break;
1707 default:
1708 aprint_error("%s: unknown media type: 0x%x\n",
1709 sc->sk_dev.dv_xname, sk_win_read_1(sc, SK_PMDTYPE));
1710 goto fail;
1711 }
1712
1713 /* determine whether to name it with vpd or just make it up */
1714 /* Marvell Yukon VPD's can freqently be bogus */
1715
1716 switch (pa->pa_id) {
1717 case PCI_ID_CODE(PCI_VENDOR_SCHNEIDERKOCH,
1718 PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE):
1719 case PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2:
1720 case PCI_PRODUCT_3COM_3C940:
1721 case PCI_PRODUCT_DLINK_DGE530T:
1722 case PCI_PRODUCT_DLINK_DGE560T:
1723 case PCI_PRODUCT_DLINK_DGE560T_2:
1724 case PCI_PRODUCT_LINKSYS_EG1032:
1725 case PCI_PRODUCT_LINKSYS_EG1064:
1726 case PCI_ID_CODE(PCI_VENDOR_SCHNEIDERKOCH,
1727 PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2):
1728 case PCI_ID_CODE(PCI_VENDOR_3COM,PCI_PRODUCT_3COM_3C940):
1729 case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE530T):
1730 case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE560T):
1731 case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE560T_2):
1732 case PCI_ID_CODE(PCI_VENDOR_LINKSYS,PCI_PRODUCT_LINKSYS_EG1032):
1733 case PCI_ID_CODE(PCI_VENDOR_LINKSYS,PCI_PRODUCT_LINKSYS_EG1064):
1734 sc->sk_name = sc->sk_vpd_prodname;
1735 break;
1736 case PCI_ID_CODE(PCI_VENDOR_GALILEO,PCI_PRODUCT_GALILEO_SKNET):
1737 /* whoops yukon vpd prodname bears no resemblance to reality */
1738 switch (sc->sk_type) {
1739 case SK_GENESIS:
1740 sc->sk_name = sc->sk_vpd_prodname;
1741 break;
1742 case SK_YUKON:
1743 sc->sk_name = "Marvell Yukon Gigabit Ethernet";
1744 break;
1745 case SK_YUKON_LITE:
1746 sc->sk_name = "Marvell Yukon Lite Gigabit Ethernet";
1747 break;
1748 case SK_YUKON_LP:
1749 sc->sk_name = "Marvell Yukon LP Gigabit Ethernet";
1750 break;
1751 default:
1752 sc->sk_name = "Marvell Yukon (Unknown) Gigabit Ethernet";
1753 }
1754
1755 /* Yukon Lite Rev A0 needs special test, from sk98lin driver */
1756
1757 if ( sc->sk_type == SK_YUKON ) {
1758 uint32_t flashaddr;
1759 uint8_t testbyte;
1760
1761 flashaddr = sk_win_read_4(sc,SK_EP_ADDR);
1762
1763 /* test Flash-Address Register */
1764 sk_win_write_1(sc,SK_EP_ADDR+3, 0xff);
1765 testbyte = sk_win_read_1(sc, SK_EP_ADDR+3);
1766
1767 if (testbyte != 0) {
1768 /* this is yukon lite Rev. A0 */
1769 sc->sk_type = SK_YUKON_LITE;
1770 sc->sk_rev = SK_YUKON_LITE_REV_A0;
1771 /* restore Flash-Address Register */
1772 sk_win_write_4(sc,SK_EP_ADDR,flashaddr);
1773 }
1774 }
1775 break;
1776 case PCI_ID_CODE(PCI_VENDOR_GALILEO,PCI_PRODUCT_GALILEO_BELKIN):
1777 sc->sk_name = sc->sk_vpd_prodname;
1778 break;
1779 default:
1780 sc->sk_name = "Unknown Marvell";
1781 }
1782
1783
1784 if ( sc->sk_type == SK_YUKON_LITE ) {
1785 switch (sc->sk_rev) {
1786 case SK_YUKON_LITE_REV_A0:
1787 revstr = "A0";
1788 break;
1789 case SK_YUKON_LITE_REV_A1:
1790 revstr = "A1";
1791 break;
1792 case SK_YUKON_LITE_REV_A3:
1793 revstr = "A3";
1794 break;
1795 default:
1796 revstr = "";
1797 }
1798 } else {
1799 revstr = "";
1800 }
1801
1802 /* Announce the product name. */
1803 aprint_normal("%s: %s rev. %s(0x%x)\n", sc->sk_dev.dv_xname,
1804 sc->sk_name, revstr, sc->sk_rev);
1805
1806 skca.skc_port = SK_PORT_A;
1807 (void)config_found(&sc->sk_dev, &skca, skcprint);
1808
1809 if (!(sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC)) {
1810 skca.skc_port = SK_PORT_B;
1811 (void)config_found(&sc->sk_dev, &skca, skcprint);
1812 }
1813
1814 /* Turn on the 'driver is loaded' LED. */
1815 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
1816
1817 /* skc sysctl setup */
1818
1819 sc->sk_int_mod = SK_IM_DEFAULT;
1820 sc->sk_int_mod_pending = 0;
1821
1822 if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1823 0, CTLTYPE_NODE, sc->sk_dev.dv_xname,
1824 SYSCTL_DESCR("skc per-controller controls"),
1825 NULL, 0, NULL, 0, CTL_HW, sk_root_num, CTL_CREATE,
1826 CTL_EOL)) != 0) {
1827 aprint_normal("%s: couldn't create sysctl node\n",
1828 sc->sk_dev.dv_xname);
1829 goto fail;
1830 }
1831
1832 sk_nodenum = node->sysctl_num;
1833
1834 /* interrupt moderation time in usecs */
1835 if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1836 CTLFLAG_READWRITE,
1837 CTLTYPE_INT, "int_mod",
1838 SYSCTL_DESCR("sk interrupt moderation timer"),
1839 sk_sysctl_handler, 0, sc,
1840 0, CTL_HW, sk_root_num, sk_nodenum, CTL_CREATE,
1841 CTL_EOL)) != 0) {
1842 aprint_normal("%s: couldn't create int_mod sysctl node\n",
1843 sc->sk_dev.dv_xname);
1844 goto fail;
1845 }
1846
1847 fail:
1848 splx(s);
1849 }
1850
1851 int
1852 sk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, u_int32_t *txidx)
1853 {
1854 struct sk_softc *sc = sc_if->sk_softc;
1855 struct sk_tx_desc *f = NULL;
1856 u_int32_t frag, cur, cnt = 0;
1857 int i;
1858 struct sk_txmap_entry *entry;
1859 bus_dmamap_t txmap;
1860
1861 DPRINTFN(3, ("sk_encap\n"));
1862
1863 entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head);
1864 if (entry == NULL) {
1865 DPRINTFN(3, ("sk_encap: no txmap available\n"));
1866 return ENOBUFS;
1867 }
1868 txmap = entry->dmamap;
1869
1870 cur = frag = *txidx;
1871
1872 #ifdef SK_DEBUG
1873 if (skdebug >= 3)
1874 sk_dump_mbuf(m_head);
1875 #endif
1876
1877 /*
1878 * Start packing the mbufs in this chain into
1879 * the fragment pointers. Stop when we run out
1880 * of fragments or hit the end of the mbuf chain.
1881 */
1882 if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
1883 BUS_DMA_NOWAIT)) {
1884 DPRINTFN(1, ("sk_encap: dmamap failed\n"));
1885 return(ENOBUFS);
1886 }
1887
1888 DPRINTFN(3, ("sk_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1889
1890 /* Sync the DMA map. */
1891 bus_dmamap_sync(sc->sc_dmatag, txmap, 0, txmap->dm_mapsize,
1892 BUS_DMASYNC_PREWRITE);
1893
1894 for (i = 0; i < txmap->dm_nsegs; i++) {
1895 if ((SK_TX_RING_CNT - (sc_if->sk_cdata.sk_tx_cnt + cnt)) < 2) {
1896 DPRINTFN(1, ("sk_encap: too few descriptors free\n"));
1897 return(ENOBUFS);
1898 }
1899 f = &sc_if->sk_rdata->sk_tx_ring[frag];
1900 f->sk_data_lo = txmap->dm_segs[i].ds_addr;
1901 f->sk_ctl = txmap->dm_segs[i].ds_len | SK_OPCODE_DEFAULT;
1902 if (cnt == 0)
1903 f->sk_ctl |= SK_TXCTL_FIRSTFRAG;
1904 else
1905 f->sk_ctl |= SK_TXCTL_OWN;
1906
1907 cur = frag;
1908 SK_INC(frag, SK_TX_RING_CNT);
1909 cnt++;
1910 }
1911
1912 sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
1913 SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
1914
1915 sc_if->sk_cdata.sk_tx_map[cur] = entry;
1916 sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |=
1917 SK_TXCTL_LASTFRAG|SK_TXCTL_EOF_INTR;
1918
1919 /* Sync descriptors before handing to chip */
1920 SK_CDTXSYNC(sc_if, *txidx, txmap->dm_nsegs,
1921 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1922
1923 sc_if->sk_rdata->sk_tx_ring[*txidx].sk_ctl |= SK_TXCTL_OWN;
1924
1925 /* Sync first descriptor to hand it off */
1926 SK_CDTXSYNC(sc_if, *txidx, 1, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1927
1928 sc_if->sk_cdata.sk_tx_cnt += cnt;
1929
1930 #ifdef SK_DEBUG
1931 if (skdebug >= 3) {
1932 struct sk_tx_desc *desc;
1933 u_int32_t idx;
1934 for (idx = *txidx; idx != frag; SK_INC(idx, SK_TX_RING_CNT)) {
1935 desc = &sc_if->sk_rdata->sk_tx_ring[idx];
1936 sk_dump_txdesc(desc, idx);
1937 }
1938 }
1939 #endif
1940
1941 *txidx = frag;
1942
1943 DPRINTFN(3, ("sk_encap: completed successfully\n"));
1944
1945 return(0);
1946 }
1947
1948 void
1949 sk_start(struct ifnet *ifp)
1950 {
1951 struct sk_if_softc *sc_if = ifp->if_softc;
1952 struct sk_softc *sc = sc_if->sk_softc;
1953 struct mbuf *m_head = NULL;
1954 u_int32_t idx = sc_if->sk_cdata.sk_tx_prod;
1955 int pkts = 0;
1956
1957 DPRINTFN(3, ("sk_start (idx %d, tx_chain[idx] %p)\n", idx,
1958 sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf));
1959
1960 while(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
1961
1962 IFQ_POLL(&ifp->if_snd, m_head);
1963 if (m_head == NULL)
1964 break;
1965
1966 /*
1967 * Pack the data into the transmit ring. If we
1968 * don't have room, set the OACTIVE flag and wait
1969 * for the NIC to drain the ring.
1970 */
1971 if (sk_encap(sc_if, m_head, &idx)) {
1972 ifp->if_flags |= IFF_OACTIVE;
1973 break;
1974 }
1975
1976 /* now we are committed to transmit the packet */
1977 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1978 pkts++;
1979
1980 /*
1981 * If there's a BPF listener, bounce a copy of this frame
1982 * to him.
1983 */
1984 #if NBPFILTER > 0
1985 if (ifp->if_bpf)
1986 bpf_mtap(ifp->if_bpf, m_head);
1987 #endif
1988 }
1989 if (pkts == 0)
1990 return;
1991
1992 /* Transmit */
1993 if (idx != sc_if->sk_cdata.sk_tx_prod) {
1994 sc_if->sk_cdata.sk_tx_prod = idx;
1995 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
1996
1997 /* Set a timeout in case the chip goes out to lunch. */
1998 ifp->if_timer = 5;
1999 }
2000 }
2001
2002
2003 void
2004 sk_watchdog(struct ifnet *ifp)
2005 {
2006 struct sk_if_softc *sc_if = ifp->if_softc;
2007
2008 printf("%s: watchdog timeout\n", sc_if->sk_dev.dv_xname);
2009 (void) sk_init(ifp);
2010 }
2011
2012 void
2013 sk_shutdown(void * v)
2014 {
2015 struct sk_if_softc *sc_if = (struct sk_if_softc *)v;
2016 struct sk_softc *sc = sc_if->sk_softc;
2017 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2018
2019 DPRINTFN(2, ("sk_shutdown\n"));
2020 sk_stop(ifp,1);
2021
2022 /* Turn off the 'driver is loaded' LED. */
2023 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
2024
2025 /*
2026 * Reset the GEnesis controller. Doing this should also
2027 * assert the resets on the attached XMAC(s).
2028 */
2029 sk_reset(sc);
2030 }
2031
2032 void
2033 sk_rxeof(struct sk_if_softc *sc_if)
2034 {
2035 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2036 struct mbuf *m;
2037 struct sk_chain *cur_rx;
2038 struct sk_rx_desc *cur_desc;
2039 int i, cur, total_len = 0;
2040 u_int32_t rxstat;
2041 bus_dmamap_t dmamap;
2042
2043 i = sc_if->sk_cdata.sk_rx_prod;
2044
2045 DPRINTFN(3, ("sk_rxeof %d\n", i));
2046
2047 for (;;) {
2048 cur = i;
2049
2050 /* Sync the descriptor */
2051 SK_CDRXSYNC(sc_if, cur,
2052 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2053
2054 if (sc_if->sk_rdata->sk_rx_ring[cur].sk_ctl & SK_RXCTL_OWN) {
2055 /* Invalidate the descriptor -- it's not ready yet */
2056 SK_CDRXSYNC(sc_if, cur, BUS_DMASYNC_PREREAD);
2057 sc_if->sk_cdata.sk_rx_prod = i;
2058 break;
2059 }
2060
2061 cur_rx = &sc_if->sk_cdata.sk_rx_chain[cur];
2062 cur_desc = &sc_if->sk_rdata->sk_rx_ring[cur];
2063 dmamap = sc_if->sk_cdata.sk_rx_jumbo_map;
2064
2065 bus_dmamap_sync(sc_if->sk_softc->sc_dmatag, dmamap, 0,
2066 dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
2067
2068 rxstat = cur_desc->sk_xmac_rxstat;
2069 m = cur_rx->sk_mbuf;
2070 cur_rx->sk_mbuf = NULL;
2071 total_len = SK_RXBYTES(cur_desc->sk_ctl);
2072
2073 sc_if->sk_cdata.sk_rx_map[cur] = 0;
2074
2075 SK_INC(i, SK_RX_RING_CNT);
2076
2077 if (rxstat & XM_RXSTAT_ERRFRAME) {
2078 ifp->if_ierrors++;
2079 sk_newbuf(sc_if, cur, m, dmamap);
2080 continue;
2081 }
2082
2083 /*
2084 * Try to allocate a new jumbo buffer. If that
2085 * fails, copy the packet to mbufs and put the
2086 * jumbo buffer back in the ring so it can be
2087 * re-used. If allocating mbufs fails, then we
2088 * have to drop the packet.
2089 */
2090 if (sk_newbuf(sc_if, cur, NULL, dmamap) == ENOBUFS) {
2091 struct mbuf *m0;
2092 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
2093 total_len + ETHER_ALIGN, 0, ifp, NULL);
2094 sk_newbuf(sc_if, cur, m, dmamap);
2095 if (m0 == NULL) {
2096 printf("%s: no receive buffers "
2097 "available -- packet dropped!\n",
2098 sc_if->sk_dev.dv_xname);
2099 ifp->if_ierrors++;
2100 continue;
2101 }
2102 m_adj(m0, ETHER_ALIGN);
2103 m = m0;
2104 } else {
2105 m->m_pkthdr.rcvif = ifp;
2106 m->m_pkthdr.len = m->m_len = total_len;
2107 }
2108
2109 ifp->if_ipackets++;
2110
2111 #if NBPFILTER > 0
2112 if (ifp->if_bpf)
2113 bpf_mtap(ifp->if_bpf, m);
2114 #endif
2115 /* pass it on. */
2116 (*ifp->if_input)(ifp, m);
2117 }
2118 }
2119
2120 void
2121 sk_txeof(struct sk_if_softc *sc_if)
2122 {
2123 struct sk_softc *sc = sc_if->sk_softc;
2124 struct sk_tx_desc *cur_tx;
2125 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2126 u_int32_t idx;
2127 struct sk_txmap_entry *entry;
2128
2129 DPRINTFN(3, ("sk_txeof\n"));
2130
2131 /*
2132 * Go through our tx ring and free mbufs for those
2133 * frames that have been sent.
2134 */
2135 idx = sc_if->sk_cdata.sk_tx_cons;
2136 while(idx != sc_if->sk_cdata.sk_tx_prod) {
2137 SK_CDTXSYNC(sc_if, idx, 1,
2138 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2139
2140 cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
2141 #ifdef SK_DEBUG
2142 if (skdebug >= 3)
2143 sk_dump_txdesc(cur_tx, idx);
2144 #endif
2145 if (cur_tx->sk_ctl & SK_TXCTL_OWN) {
2146 SK_CDTXSYNC(sc_if, idx, 1, BUS_DMASYNC_PREREAD);
2147 break;
2148 }
2149 if (cur_tx->sk_ctl & SK_TXCTL_LASTFRAG)
2150 ifp->if_opackets++;
2151 if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
2152 entry = sc_if->sk_cdata.sk_tx_map[idx];
2153
2154 m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
2155 sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
2156
2157 bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
2158 entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2159
2160 bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
2161 SIMPLEQ_INSERT_TAIL(&sc_if->sk_txmap_head, entry,
2162 link);
2163 sc_if->sk_cdata.sk_tx_map[idx] = NULL;
2164 }
2165 sc_if->sk_cdata.sk_tx_cnt--;
2166 SK_INC(idx, SK_TX_RING_CNT);
2167 }
2168 if (sc_if->sk_cdata.sk_tx_cnt == 0)
2169 ifp->if_timer = 0;
2170 else /* nudge chip to keep tx ring moving */
2171 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
2172
2173 if (sc_if->sk_cdata.sk_tx_cnt < SK_TX_RING_CNT - 2)
2174 ifp->if_flags &= ~IFF_OACTIVE;
2175
2176 sc_if->sk_cdata.sk_tx_cons = idx;
2177 }
2178
2179 void
2180 sk_tick(void *xsc_if)
2181 {
2182 struct sk_if_softc *sc_if = xsc_if;
2183 struct mii_data *mii = &sc_if->sk_mii;
2184 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2185 int i;
2186
2187 DPRINTFN(3, ("sk_tick\n"));
2188
2189 if (!(ifp->if_flags & IFF_UP))
2190 return;
2191
2192 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2193 sk_intr_bcom(sc_if);
2194 return;
2195 }
2196
2197 /*
2198 * According to SysKonnect, the correct way to verify that
2199 * the link has come back up is to poll bit 0 of the GPIO
2200 * register three times. This pin has the signal from the
2201 * link sync pin connected to it; if we read the same link
2202 * state 3 times in a row, we know the link is up.
2203 */
2204 for (i = 0; i < 3; i++) {
2205 if (SK_XM_READ_2(sc_if, XM_GPIO) & XM_GPIO_GP0_SET)
2206 break;
2207 }
2208
2209 if (i != 3) {
2210 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2211 return;
2212 }
2213
2214 /* Turn the GP0 interrupt back on. */
2215 SK_XM_CLRBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2216 SK_XM_READ_2(sc_if, XM_ISR);
2217 mii_tick(mii);
2218 mii_pollstat(mii);
2219 callout_stop(&sc_if->sk_tick_ch);
2220 }
2221
2222 void
2223 sk_intr_bcom(struct sk_if_softc *sc_if)
2224 {
2225 struct mii_data *mii = &sc_if->sk_mii;
2226 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2227 int status;
2228
2229
2230 DPRINTFN(3, ("sk_intr_bcom\n"));
2231
2232 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2233
2234 /*
2235 * Read the PHY interrupt register to make sure
2236 * we clear any pending interrupts.
2237 */
2238 status = sk_xmac_miibus_readreg((struct device *)sc_if,
2239 SK_PHYADDR_BCOM, BRGPHY_MII_ISR);
2240
2241 if (!(ifp->if_flags & IFF_RUNNING)) {
2242 sk_init_xmac(sc_if);
2243 return;
2244 }
2245
2246 if (status & (BRGPHY_ISR_LNK_CHG|BRGPHY_ISR_AN_PR)) {
2247 int lstat;
2248 lstat = sk_xmac_miibus_readreg((struct device *)sc_if,
2249 SK_PHYADDR_BCOM, BRGPHY_MII_AUXSTS);
2250
2251 if (!(lstat & BRGPHY_AUXSTS_LINK) && sc_if->sk_link) {
2252 mii_mediachg(mii);
2253 /* Turn off the link LED. */
2254 SK_IF_WRITE_1(sc_if, 0,
2255 SK_LINKLED1_CTL, SK_LINKLED_OFF);
2256 sc_if->sk_link = 0;
2257 } else if (status & BRGPHY_ISR_LNK_CHG) {
2258 sk_xmac_miibus_writereg((struct device *)sc_if,
2259 SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFF00);
2260 mii_tick(mii);
2261 sc_if->sk_link = 1;
2262 /* Turn on the link LED. */
2263 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2264 SK_LINKLED_ON|SK_LINKLED_LINKSYNC_OFF|
2265 SK_LINKLED_BLINK_OFF);
2266 mii_pollstat(mii);
2267 } else {
2268 mii_tick(mii);
2269 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick,sc_if);
2270 }
2271 }
2272
2273 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2274 }
2275
2276 void
2277 sk_intr_xmac(struct sk_if_softc *sc_if)
2278 {
2279 u_int16_t status = SK_XM_READ_2(sc_if, XM_ISR);
2280
2281 DPRINTFN(3, ("sk_intr_xmac\n"));
2282
2283 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) {
2284 if (status & XM_ISR_GP0_SET) {
2285 SK_XM_SETBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2286 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2287 }
2288
2289 if (status & XM_ISR_AUTONEG_DONE) {
2290 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2291 }
2292 }
2293
2294 if (status & XM_IMR_TX_UNDERRUN)
2295 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_TXFIFO);
2296
2297 if (status & XM_IMR_RX_OVERRUN)
2298 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_RXFIFO);
2299 }
2300
2301 void
2302 sk_intr_yukon(sc_if)
2303 struct sk_if_softc *sc_if;
2304 {
2305 int status;
2306
2307 status = SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2308
2309 DPRINTFN(3, ("sk_intr_yukon status=%#x\n", status));
2310 }
2311
2312 int
2313 sk_intr(void *xsc)
2314 {
2315 struct sk_softc *sc = xsc;
2316 struct sk_if_softc *sc_if0 = sc->sk_if[SK_PORT_A];
2317 struct sk_if_softc *sc_if1 = sc->sk_if[SK_PORT_B];
2318 struct ifnet *ifp0 = NULL, *ifp1 = NULL;
2319 u_int32_t status;
2320 int claimed = 0;
2321
2322 if (sc_if0 != NULL)
2323 ifp0 = &sc_if0->sk_ethercom.ec_if;
2324 if (sc_if1 != NULL)
2325 ifp1 = &sc_if1->sk_ethercom.ec_if;
2326
2327 for (;;) {
2328 status = CSR_READ_4(sc, SK_ISSR);
2329 DPRINTFN(3, ("sk_intr: status=%#x\n", status));
2330
2331 if (!(status & sc->sk_intrmask))
2332 break;
2333
2334 claimed = 1;
2335
2336 /* Handle receive interrupts first. */
2337 if (sc_if0 && (status & SK_ISR_RX1_EOF)) {
2338 sk_rxeof(sc_if0);
2339 CSR_WRITE_4(sc, SK_BMU_RX_CSR0,
2340 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2341 }
2342 if (sc_if1 && (status & SK_ISR_RX2_EOF)) {
2343 sk_rxeof(sc_if1);
2344 CSR_WRITE_4(sc, SK_BMU_RX_CSR1,
2345 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2346 }
2347
2348 /* Then transmit interrupts. */
2349 if (sc_if0 && (status & SK_ISR_TX1_S_EOF)) {
2350 sk_txeof(sc_if0);
2351 CSR_WRITE_4(sc, SK_BMU_TXS_CSR0,
2352 SK_TXBMU_CLR_IRQ_EOF);
2353 }
2354 if (sc_if1 && (status & SK_ISR_TX2_S_EOF)) {
2355 sk_txeof(sc_if1);
2356 CSR_WRITE_4(sc, SK_BMU_TXS_CSR1,
2357 SK_TXBMU_CLR_IRQ_EOF);
2358 }
2359
2360 /* Then MAC interrupts. */
2361 if (sc_if0 && (status & SK_ISR_MAC1) &&
2362 (ifp0->if_flags & IFF_RUNNING)) {
2363 if (sc->sk_type == SK_GENESIS)
2364 sk_intr_xmac(sc_if0);
2365 else
2366 sk_intr_yukon(sc_if0);
2367 }
2368
2369 if (sc_if1 && (status & SK_ISR_MAC2) &&
2370 (ifp1->if_flags & IFF_RUNNING)) {
2371 if (sc->sk_type == SK_GENESIS)
2372 sk_intr_xmac(sc_if1);
2373 else
2374 sk_intr_yukon(sc_if1);
2375
2376 }
2377
2378 if (status & SK_ISR_EXTERNAL_REG) {
2379 if (sc_if0 != NULL &&
2380 sc_if0->sk_phytype == SK_PHYTYPE_BCOM)
2381 sk_intr_bcom(sc_if0);
2382
2383 if (sc_if1 != NULL &&
2384 sc_if1->sk_phytype == SK_PHYTYPE_BCOM)
2385 sk_intr_bcom(sc_if1);
2386 }
2387 }
2388
2389 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2390
2391 if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
2392 sk_start(ifp0);
2393 if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
2394 sk_start(ifp1);
2395
2396 #if NRND > 0
2397 if (RND_ENABLED(&sc->rnd_source))
2398 rnd_add_uint32(&sc->rnd_source, status);
2399 #endif
2400
2401 if (sc->sk_int_mod_pending)
2402 sk_update_int_mod(sc);
2403
2404 return (claimed);
2405 }
2406
2407 void
2408 sk_init_xmac(struct sk_if_softc *sc_if)
2409 {
2410 struct sk_softc *sc = sc_if->sk_softc;
2411 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2412 static const struct sk_bcom_hack bhack[] = {
2413 { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 }, { 0x17, 0x0013 },
2414 { 0x15, 0x0404 }, { 0x17, 0x8006 }, { 0x15, 0x0132 }, { 0x17, 0x8006 },
2415 { 0x15, 0x0232 }, { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
2416 { 0, 0 } };
2417
2418 DPRINTFN(1, ("sk_init_xmac\n"));
2419
2420 /* Unreset the XMAC. */
2421 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_UNRESET);
2422 DELAY(1000);
2423
2424 /* Reset the XMAC's internal state. */
2425 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2426
2427 /* Save the XMAC II revision */
2428 sc_if->sk_xmac_rev = XM_XMAC_REV(SK_XM_READ_4(sc_if, XM_DEVID));
2429
2430 /*
2431 * Perform additional initialization for external PHYs,
2432 * namely for the 1000baseTX cards that use the XMAC's
2433 * GMII mode.
2434 */
2435 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2436 int i = 0;
2437 u_int32_t val;
2438
2439 /* Take PHY out of reset. */
2440 val = sk_win_read_4(sc, SK_GPIO);
2441 if (sc_if->sk_port == SK_PORT_A)
2442 val |= SK_GPIO_DIR0|SK_GPIO_DAT0;
2443 else
2444 val |= SK_GPIO_DIR2|SK_GPIO_DAT2;
2445 sk_win_write_4(sc, SK_GPIO, val);
2446
2447 /* Enable GMII mode on the XMAC. */
2448 SK_XM_SETBIT_2(sc_if, XM_HWCFG, XM_HWCFG_GMIIMODE);
2449
2450 sk_xmac_miibus_writereg((struct device *)sc_if,
2451 SK_PHYADDR_BCOM, MII_BMCR, BMCR_RESET);
2452 DELAY(10000);
2453 sk_xmac_miibus_writereg((struct device *)sc_if,
2454 SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFFF0);
2455
2456 /*
2457 * Early versions of the BCM5400 apparently have
2458 * a bug that requires them to have their reserved
2459 * registers initialized to some magic values. I don't
2460 * know what the numbers do, I'm just the messenger.
2461 */
2462 if (sk_xmac_miibus_readreg((struct device *)sc_if,
2463 SK_PHYADDR_BCOM, 0x03) == 0x6041) {
2464 while(bhack[i].reg) {
2465 sk_xmac_miibus_writereg((struct device *)sc_if,
2466 SK_PHYADDR_BCOM, bhack[i].reg,
2467 bhack[i].val);
2468 i++;
2469 }
2470 }
2471 }
2472
2473 /* Set station address */
2474 SK_XM_WRITE_2(sc_if, XM_PAR0,
2475 *(u_int16_t *)(&sc_if->sk_enaddr[0]));
2476 SK_XM_WRITE_2(sc_if, XM_PAR1,
2477 *(u_int16_t *)(&sc_if->sk_enaddr[2]));
2478 SK_XM_WRITE_2(sc_if, XM_PAR2,
2479 *(u_int16_t *)(&sc_if->sk_enaddr[4]));
2480 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_STATION);
2481
2482 if (ifp->if_flags & IFF_PROMISC) {
2483 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
2484 } else {
2485 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
2486 }
2487
2488 if (ifp->if_flags & IFF_BROADCAST) {
2489 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2490 } else {
2491 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2492 }
2493
2494 /* We don't need the FCS appended to the packet. */
2495 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_STRIPFCS);
2496
2497 /* We want short frames padded to 60 bytes. */
2498 SK_XM_SETBIT_2(sc_if, XM_TXCMD, XM_TXCMD_AUTOPAD);
2499
2500 /*
2501 * Enable the reception of all error frames. This is is
2502 * a necessary evil due to the design of the XMAC. The
2503 * XMAC's receive FIFO is only 8K in size, however jumbo
2504 * frames can be up to 9000 bytes in length. When bad
2505 * frame filtering is enabled, the XMAC's RX FIFO operates
2506 * in 'store and forward' mode. For this to work, the
2507 * entire frame has to fit into the FIFO, but that means
2508 * that jumbo frames larger than 8192 bytes will be
2509 * truncated. Disabling all bad frame filtering causes
2510 * the RX FIFO to operate in streaming mode, in which
2511 * case the XMAC will start transfering frames out of the
2512 * RX FIFO as soon as the FIFO threshold is reached.
2513 */
2514 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_BADFRAMES|
2515 XM_MODE_RX_GIANTS|XM_MODE_RX_RUNTS|XM_MODE_RX_CRCERRS|
2516 XM_MODE_RX_INRANGELEN);
2517
2518 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2519 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2520 else
2521 SK_XM_CLRBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2522
2523 /*
2524 * Bump up the transmit threshold. This helps hold off transmit
2525 * underruns when we're blasting traffic from both ports at once.
2526 */
2527 SK_XM_WRITE_2(sc_if, XM_TX_REQTHRESH, SK_XM_TX_FIFOTHRESH);
2528
2529 /* Set multicast filter */
2530 sk_setmulti(sc_if);
2531
2532 /* Clear and enable interrupts */
2533 SK_XM_READ_2(sc_if, XM_ISR);
2534 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC)
2535 SK_XM_WRITE_2(sc_if, XM_IMR, XM_INTRS);
2536 else
2537 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2538
2539 /* Configure MAC arbiter */
2540 switch(sc_if->sk_xmac_rev) {
2541 case XM_XMAC_REV_B2:
2542 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_B2);
2543 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_B2);
2544 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_B2);
2545 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_B2);
2546 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_B2);
2547 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_B2);
2548 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_B2);
2549 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_B2);
2550 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2551 break;
2552 case XM_XMAC_REV_C1:
2553 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_C1);
2554 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_C1);
2555 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_C1);
2556 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_C1);
2557 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_C1);
2558 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_C1);
2559 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_C1);
2560 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_C1);
2561 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2562 break;
2563 default:
2564 break;
2565 }
2566 sk_win_write_2(sc, SK_MACARB_CTL,
2567 SK_MACARBCTL_UNRESET|SK_MACARBCTL_FASTOE_OFF);
2568
2569 sc_if->sk_link = 1;
2570 }
2571
2572 void sk_init_yukon(sc_if)
2573 struct sk_if_softc *sc_if;
2574 {
2575 u_int32_t /*mac, */phy;
2576 u_int16_t reg;
2577 struct sk_softc *sc;
2578 int i;
2579
2580 DPRINTFN(1, ("sk_init_yukon: start: sk_csr=%#x\n",
2581 CSR_READ_4(sc_if->sk_softc, SK_CSR)));
2582
2583 sc = sc_if->sk_softc;
2584 if (sc->sk_type == SK_YUKON_LITE &&
2585 sc->sk_rev >= SK_YUKON_LITE_REV_A3) {
2586 /* Take PHY out of reset. */
2587 sk_win_write_4(sc, SK_GPIO,
2588 (sk_win_read_4(sc, SK_GPIO) | SK_GPIO_DIR9) & ~SK_GPIO_DAT9);
2589 }
2590
2591
2592 /* GMAC and GPHY Reset */
2593 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
2594
2595 DPRINTFN(6, ("sk_init_yukon: 1\n"));
2596
2597 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2598 DELAY(1000);
2599 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_CLEAR);
2600 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2601 DELAY(1000);
2602
2603
2604 DPRINTFN(6, ("sk_init_yukon: 2\n"));
2605
2606 phy = SK_GPHY_INT_POL_HI | SK_GPHY_DIS_FC | SK_GPHY_DIS_SLEEP |
2607 SK_GPHY_ENA_XC | SK_GPHY_ANEG_ALL | SK_GPHY_ENA_PAUSE;
2608
2609 switch(sc_if->sk_softc->sk_pmd) {
2610 case IFM_1000_SX:
2611 case IFM_1000_LX:
2612 phy |= SK_GPHY_FIBER;
2613 break;
2614
2615 case IFM_1000_CX:
2616 case IFM_1000_T:
2617 phy |= SK_GPHY_COPPER;
2618 break;
2619 }
2620
2621 DPRINTFN(3, ("sk_init_yukon: phy=%#x\n", phy));
2622
2623 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_SET);
2624 DELAY(1000);
2625 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_CLEAR);
2626 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
2627 SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
2628
2629 DPRINTFN(3, ("sk_init_yukon: gmac_ctrl=%#x\n",
2630 SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
2631
2632 DPRINTFN(6, ("sk_init_yukon: 3\n"));
2633
2634 /* unused read of the interrupt source register */
2635 DPRINTFN(6, ("sk_init_yukon: 4\n"));
2636 SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2637
2638 DPRINTFN(6, ("sk_init_yukon: 4a\n"));
2639 reg = SK_YU_READ_2(sc_if, YUKON_PAR);
2640 DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
2641
2642 /* MIB Counter Clear Mode set */
2643 reg |= YU_PAR_MIB_CLR;
2644 DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
2645 DPRINTFN(6, ("sk_init_yukon: 4b\n"));
2646 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2647
2648 /* MIB Counter Clear Mode clear */
2649 DPRINTFN(6, ("sk_init_yukon: 5\n"));
2650 reg &= ~YU_PAR_MIB_CLR;
2651 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2652
2653 /* receive control reg */
2654 DPRINTFN(6, ("sk_init_yukon: 7\n"));
2655 SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_UFLEN | YU_RCR_MUFLEN |
2656 YU_RCR_CRCR);
2657
2658 /* transmit parameter register */
2659 DPRINTFN(6, ("sk_init_yukon: 8\n"));
2660 SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
2661 YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) );
2662
2663 /* serial mode register */
2664 DPRINTFN(6, ("sk_init_yukon: 9\n"));
2665 SK_YU_WRITE_2(sc_if, YUKON_SMR, YU_SMR_DATA_BLIND(0x1c) |
2666 YU_SMR_MFL_VLAN | YU_SMR_MFL_JUMBO |
2667 YU_SMR_IPG_DATA(0x1e));
2668
2669 DPRINTFN(6, ("sk_init_yukon: 10\n"));
2670 /* Setup Yukon's address */
2671 for (i = 0; i < 3; i++) {
2672 /* Write Source Address 1 (unicast filter) */
2673 SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
2674 sc_if->sk_enaddr[i * 2] |
2675 sc_if->sk_enaddr[i * 2 + 1] << 8);
2676 }
2677
2678 for (i = 0; i < 3; i++) {
2679 reg = sk_win_read_2(sc_if->sk_softc,
2680 SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
2681 SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
2682 }
2683
2684 /* Set multicast filter */
2685 DPRINTFN(6, ("sk_init_yukon: 11\n"));
2686 sk_setmulti(sc_if);
2687
2688 /* enable interrupt mask for counter overflows */
2689 DPRINTFN(6, ("sk_init_yukon: 12\n"));
2690 SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
2691 SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
2692 SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
2693
2694 /* Configure RX MAC FIFO */
2695 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
2696 SK_IF_WRITE_4(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON);
2697
2698 /* Configure TX MAC FIFO */
2699 SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
2700 SK_IF_WRITE_4(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
2701
2702 DPRINTFN(6, ("sk_init_yukon: end\n"));
2703 }
2704
2705 /*
2706 * Note that to properly initialize any part of the GEnesis chip,
2707 * you first have to take it out of reset mode.
2708 */
2709 int
2710 sk_init(struct ifnet *ifp)
2711 {
2712 struct sk_if_softc *sc_if = ifp->if_softc;
2713 struct sk_softc *sc = sc_if->sk_softc;
2714 struct mii_data *mii = &sc_if->sk_mii;
2715 int s;
2716 u_int32_t imr, sk_imtimer_ticks;
2717
2718 DPRINTFN(1, ("sk_init\n"));
2719
2720 s = splnet();
2721
2722 if (ifp->if_flags & IFF_RUNNING) {
2723 splx(s);
2724 return 0;
2725 }
2726
2727 /* Cancel pending I/O and free all RX/TX buffers. */
2728 sk_stop(ifp,0);
2729
2730 if (sc->sk_type == SK_GENESIS) {
2731 /* Configure LINK_SYNC LED */
2732 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_ON);
2733 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2734 SK_LINKLED_LINKSYNC_ON);
2735
2736 /* Configure RX LED */
2737 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL,
2738 SK_RXLEDCTL_COUNTER_START);
2739
2740 /* Configure TX LED */
2741 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL,
2742 SK_TXLEDCTL_COUNTER_START);
2743 }
2744
2745 /* Configure I2C registers */
2746
2747 /* Configure XMAC(s) */
2748 switch (sc->sk_type) {
2749 case SK_GENESIS:
2750 sk_init_xmac(sc_if);
2751 break;
2752 case SK_YUKON:
2753 case SK_YUKON_LITE:
2754 case SK_YUKON_LP:
2755 sk_init_yukon(sc_if);
2756 break;
2757 }
2758 mii_mediachg(mii);
2759
2760 if (sc->sk_type == SK_GENESIS) {
2761 /* Configure MAC FIFOs */
2762 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_UNRESET);
2763 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_END, SK_FIFO_END);
2764 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_ON);
2765
2766 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_UNRESET);
2767 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_END, SK_FIFO_END);
2768 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_ON);
2769 }
2770
2771 /* Configure transmit arbiter(s) */
2772 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL,
2773 SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
2774
2775 /* Configure RAMbuffers */
2776 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
2777 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
2778 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
2779 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
2780 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
2781 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
2782
2783 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_UNRESET);
2784 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_STORENFWD_ON);
2785 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_START, sc_if->sk_tx_ramstart);
2786 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_WR_PTR, sc_if->sk_tx_ramstart);
2787 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_RD_PTR, sc_if->sk_tx_ramstart);
2788 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_END, sc_if->sk_tx_ramend);
2789 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_ON);
2790
2791 /* Configure BMUs */
2792 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_ONLINE);
2793 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO,
2794 SK_RX_RING_ADDR(sc_if, 0));
2795 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 0);
2796
2797 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_ONLINE);
2798 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_LO,
2799 SK_TX_RING_ADDR(sc_if, 0));
2800 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_HI, 0);
2801
2802 /* Init descriptors */
2803 if (sk_init_rx_ring(sc_if) == ENOBUFS) {
2804 printf("%s: initialization failed: no "
2805 "memory for rx buffers\n", sc_if->sk_dev.dv_xname);
2806 sk_stop(ifp,0);
2807 splx(s);
2808 return(ENOBUFS);
2809 }
2810
2811 if (sk_init_tx_ring(sc_if) == ENOBUFS) {
2812 printf("%s: initialization failed: no "
2813 "memory for tx buffers\n", sc_if->sk_dev.dv_xname);
2814 sk_stop(ifp,0);
2815 splx(s);
2816 return(ENOBUFS);
2817 }
2818
2819 /* Set interrupt moderation if changed via sysctl. */
2820 switch (sc->sk_type) {
2821 case SK_GENESIS:
2822 sk_imtimer_ticks = SK_IMTIMER_TICKS_GENESIS;
2823 break;
2824 case SK_YUKON_EC:
2825 sk_imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
2826 break;
2827 default:
2828 sk_imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
2829 }
2830 imr = sk_win_read_4(sc, SK_IMTIMERINIT);
2831 if (imr != SK_IM_USECS(sc->sk_int_mod)) {
2832 sk_win_write_4(sc, SK_IMTIMERINIT,
2833 SK_IM_USECS(sc->sk_int_mod));
2834 aprint_verbose("%s: interrupt moderation is %d us\n",
2835 sc->sk_dev.dv_xname, sc->sk_int_mod);
2836 }
2837
2838 /* Configure interrupt handling */
2839 CSR_READ_4(sc, SK_ISSR);
2840 if (sc_if->sk_port == SK_PORT_A)
2841 sc->sk_intrmask |= SK_INTRS1;
2842 else
2843 sc->sk_intrmask |= SK_INTRS2;
2844
2845 sc->sk_intrmask |= SK_ISR_EXTERNAL_REG;
2846
2847 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2848
2849 /* Start BMUs. */
2850 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_START);
2851
2852 if (sc->sk_type == SK_GENESIS) {
2853 /* Enable XMACs TX and RX state machines */
2854 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_IGNPAUSE);
2855 SK_XM_SETBIT_2(sc_if, XM_MMUCMD,
2856 XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2857 }
2858
2859 if (SK_YUKON_FAMILY(sc->sk_type)) {
2860 u_int16_t reg = SK_YU_READ_2(sc_if, YUKON_GPCR);
2861 reg |= YU_GPCR_TXEN | YU_GPCR_RXEN;
2862 reg &= ~(YU_GPCR_SPEED_EN | YU_GPCR_DPLX_EN);
2863 SK_YU_WRITE_2(sc_if, YUKON_GPCR, reg);
2864 }
2865
2866
2867 ifp->if_flags |= IFF_RUNNING;
2868 ifp->if_flags &= ~IFF_OACTIVE;
2869
2870 splx(s);
2871 return(0);
2872 }
2873
2874 void
2875 sk_stop(struct ifnet *ifp, int disable)
2876 {
2877 struct sk_if_softc *sc_if = ifp->if_softc;
2878 struct sk_softc *sc = sc_if->sk_softc;
2879 //struct sk_txmap_entry *dma;
2880 int i;
2881
2882 DPRINTFN(1, ("sk_stop\n"));
2883
2884 callout_stop(&sc_if->sk_tick_ch);
2885
2886 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2887 u_int32_t val;
2888
2889 /* Put PHY back into reset. */
2890 val = sk_win_read_4(sc, SK_GPIO);
2891 if (sc_if->sk_port == SK_PORT_A) {
2892 val |= SK_GPIO_DIR0;
2893 val &= ~SK_GPIO_DAT0;
2894 } else {
2895 val |= SK_GPIO_DIR2;
2896 val &= ~SK_GPIO_DAT2;
2897 }
2898 sk_win_write_4(sc, SK_GPIO, val);
2899 }
2900
2901 /* Turn off various components of this interface. */
2902 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2903 switch (sc->sk_type) {
2904 case SK_GENESIS:
2905 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL,
2906 SK_TXMACCTL_XMAC_RESET);
2907 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_RESET);
2908 break;
2909 case SK_YUKON:
2910 case SK_YUKON_LITE:
2911 case SK_YUKON_LP:
2912 SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
2913 SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
2914 break;
2915 }
2916 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
2917 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2918 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_OFFLINE);
2919 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2920 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
2921 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2922 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2923 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
2924 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
2925
2926 /* Disable interrupts */
2927 if (sc_if->sk_port == SK_PORT_A)
2928 sc->sk_intrmask &= ~SK_INTRS1;
2929 else
2930 sc->sk_intrmask &= ~SK_INTRS2;
2931 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2932
2933 SK_XM_READ_2(sc_if, XM_ISR);
2934 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2935
2936 /* Free RX and TX mbufs still in the queues. */
2937 for (i = 0; i < SK_RX_RING_CNT; i++) {
2938 if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
2939 m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
2940 sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
2941 }
2942 }
2943
2944 for (i = 0; i < SK_TX_RING_CNT; i++) {
2945 if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
2946 m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
2947 sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
2948 }
2949 }
2950
2951 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2952 }
2953
2954 CFATTACH_DECL(skc,sizeof(struct sk_softc), skc_probe, skc_attach, NULL, NULL);
2955
2956 /*
2957 struct cfdriver skc_cd = {
2958 0, "skc", DV_DULL
2959 };
2960 */
2961
2962 CFATTACH_DECL(sk,sizeof(struct sk_if_softc), sk_probe, sk_attach, NULL, NULL);
2963
2964 /*
2965 struct cfdriver sk_cd = {
2966 0, "sk", DV_IFNET
2967 };
2968 */
2969
2970 #ifdef SK_DEBUG
2971 void
2972 sk_dump_txdesc(struct sk_tx_desc *desc, int idx)
2973 {
2974 #define DESC_PRINT(X) \
2975 if (desc->X) \
2976 printf("txdesc[%d]." #X "=%#x\n", \
2977 idx, desc->X);
2978
2979 DESC_PRINT(sk_ctl);
2980 DESC_PRINT(sk_next);
2981 DESC_PRINT(sk_data_lo);
2982 DESC_PRINT(sk_data_hi);
2983 DESC_PRINT(sk_xmac_txstat);
2984 DESC_PRINT(sk_rsvd0);
2985 DESC_PRINT(sk_csum_startval);
2986 DESC_PRINT(sk_csum_startpos);
2987 DESC_PRINT(sk_csum_writepos);
2988 DESC_PRINT(sk_rsvd1);
2989 #undef PRINT
2990 }
2991
2992 void
2993 sk_dump_bytes(const char *data, int len)
2994 {
2995 int c, i, j;
2996
2997 for (i = 0; i < len; i += 16) {
2998 printf("%08x ", i);
2999 c = len - i;
3000 if (c > 16) c = 16;
3001
3002 for (j = 0; j < c; j++) {
3003 printf("%02x ", data[i + j] & 0xff);
3004 if ((j & 0xf) == 7 && j > 0)
3005 printf(" ");
3006 }
3007
3008 for (; j < 16; j++)
3009 printf(" ");
3010 printf(" ");
3011
3012 for (j = 0; j < c; j++) {
3013 int ch = data[i + j] & 0xff;
3014 printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
3015 }
3016
3017 printf("\n");
3018
3019 if (c < 16)
3020 break;
3021 }
3022 }
3023
3024 void
3025 sk_dump_mbuf(struct mbuf *m)
3026 {
3027 int count = m->m_pkthdr.len;
3028
3029 printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
3030
3031 while (count > 0 && m) {
3032 printf("m=%p, m->m_data=%p, m->m_len=%d\n",
3033 m, m->m_data, m->m_len);
3034 sk_dump_bytes(mtod(m, char *), m->m_len);
3035
3036 count -= m->m_len;
3037 m = m->m_next;
3038 }
3039 }
3040 #endif
3041
3042 static int
3043 sk_sysctl_handler(SYSCTLFN_ARGS)
3044 {
3045 int error, t;
3046 struct sysctlnode node;
3047 struct sk_softc *sc;
3048
3049 node = *rnode;
3050 sc = node.sysctl_data;
3051 t = sc->sk_int_mod;
3052 node.sysctl_data = &t;
3053 error = sysctl_lookup(SYSCTLFN_CALL(&node));
3054 if (error || newp == NULL)
3055 return (error);
3056
3057 if (t < SK_IM_MIN || t > SK_IM_MAX)
3058 return (EINVAL);
3059
3060 /* update the softc with sysctl-changed value, and mark
3061 for hardware update */
3062 sc->sk_int_mod = t;
3063 sc->sk_int_mod_pending = 1;
3064 return (0);
3065 }
3066
3067 /*
3068 * Set up sysctl(3) MIB, hw.sk.* - Individual controllers will be
3069 * set up in skc_attach()
3070 */
3071 SYSCTL_SETUP(sysctl_sk, "sysctl sk subtree setup")
3072 {
3073 int rc;
3074 const struct sysctlnode *node;
3075
3076 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
3077 0, CTLTYPE_NODE, "hw", NULL,
3078 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
3079 goto err;
3080 }
3081
3082 if ((rc = sysctl_createv(clog, 0, NULL, &node,
3083 0, CTLTYPE_NODE, "sk",
3084 SYSCTL_DESCR("sk interface controls"),
3085 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
3086 goto err;
3087 }
3088
3089 sk_root_num = node->sysctl_num;
3090 return;
3091
3092 err:
3093 printf("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
3094 }
3095