if_sk.c revision 1.1 1 /* $NetBSD: if_sk.c,v 1.1 2003/08/26 21:11:00 jdolecek 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
126 #include <sys/param.h>
127 #include <sys/systm.h>
128 #include <sys/sockio.h>
129 #include <sys/mbuf.h>
130 #include <sys/malloc.h>
131 #include <sys/kernel.h>
132 #include <sys/socket.h>
133 #include <sys/device.h>
134 #include <sys/queue.h>
135 #include <sys/callout.h>
136
137 #include <net/if.h>
138 #include <net/if_dl.h>
139 #include <net/if_types.h>
140
141 #ifdef INET
142 #include <netinet/in.h>
143 #include <netinet/in_systm.h>
144 #include <netinet/in_var.h>
145 #include <netinet/ip.h>
146 #include <netinet/if_ether.h>
147 #endif
148
149 #include <net/if_media.h>
150
151 #if NBPFILTER > 0
152 #include <net/bpf.h>
153 #endif
154
155 #include <dev/mii/mii.h>
156 #include <dev/mii/miivar.h>
157 #include <dev/mii/brgphyreg.h>
158
159 #include <dev/pci/pcireg.h>
160 #include <dev/pci/pcivar.h>
161 #include <dev/pci/pcidevs.h>
162
163 #define SK_VERBOSE
164 /* #define SK_USEIOSPACE */
165
166 #include <dev/pci/if_skreg.h>
167 #include <dev/pci/if_skvar.h>
168
169 int skc_probe(struct device *, struct cfdata *, void *);
170 void skc_attach(struct device *, struct device *self, void *aux);
171 int sk_probe(struct device *, struct cfdata *, void *);
172 void sk_attach(struct device *, struct device *self, void *aux);
173 int skcprint(void *, const char *);
174 int sk_intr(void *);
175 void sk_intr_bcom(struct sk_if_softc *);
176 void sk_intr_xmac(struct sk_if_softc *);
177 void sk_intr_yukon(struct sk_if_softc *);
178 void sk_rxeof(struct sk_if_softc *);
179 void sk_txeof(struct sk_if_softc *);
180 int sk_encap(struct sk_if_softc *, struct mbuf *, u_int32_t *);
181 void sk_start(struct ifnet *);
182 int sk_ioctl(struct ifnet *, u_long, caddr_t);
183 int sk_init(struct ifnet *);
184 void sk_init_xmac(struct sk_if_softc *);
185 void sk_init_yukon(struct sk_if_softc *);
186 void sk_stop(struct ifnet *, int);
187 void sk_watchdog(struct ifnet *);
188 void sk_shutdown(void *);
189 int sk_ifmedia_upd(struct ifnet *);
190 void sk_ifmedia_sts(struct ifnet *, struct ifmediareq *);
191 void sk_reset(struct sk_softc *);
192 int sk_newbuf(struct sk_if_softc *, int, struct mbuf *, bus_dmamap_t);
193 int sk_init_rx_ring(struct sk_if_softc *);
194 int sk_init_tx_ring(struct sk_if_softc *);
195 u_int8_t sk_vpd_readbyte(struct sk_softc *, int);
196 void sk_vpd_read_res(struct sk_softc *,
197 struct vpd_res *, int);
198 void sk_vpd_read(struct sk_softc *);
199
200 int sk_xmac_miibus_readreg(struct device *, int, int);
201 void sk_xmac_miibus_writereg(struct device *, int, int, int);
202 void sk_xmac_miibus_statchg(struct device *);
203
204 int sk_marv_miibus_readreg(struct device *, int, int);
205 void sk_marv_miibus_writereg(struct device *, int, int, int);
206 void sk_marv_miibus_statchg(struct device *);
207
208 u_int32_t sk_calchash(caddr_t);
209 void sk_setfilt(struct sk_if_softc *, caddr_t, int);
210 void sk_setmulti(struct sk_if_softc *);
211 void sk_tick(void *);
212
213 /* #define SK_DEBUG 2 */
214 #ifdef SK_DEBUG
215 #define DPRINTF(x) if (skdebug) printf x
216 #define DPRINTFN(n,x) if (skdebug >= (n)) printf x
217 int skdebug = SK_DEBUG;
218
219 void sk_dump_txdesc(struct sk_tx_desc *, int);
220 void sk_dump_mbuf(struct mbuf *);
221 void sk_dump_bytes(const char *, int);
222 #else
223 #define DPRINTF(x)
224 #define DPRINTFN(n,x)
225 #endif
226
227 #define SK_SETBIT(sc, reg, x) \
228 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x)
229
230 #define SK_CLRBIT(sc, reg, x) \
231 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x)
232
233 #define SK_WIN_SETBIT_4(sc, reg, x) \
234 sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) | x)
235
236 #define SK_WIN_CLRBIT_4(sc, reg, x) \
237 sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) & ~x)
238
239 #define SK_WIN_SETBIT_2(sc, reg, x) \
240 sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) | x)
241
242 #define SK_WIN_CLRBIT_2(sc, reg, x) \
243 sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) & ~x)
244
245 /* supported device vendors */
246 static const struct sk_product {
247 pci_vendor_id_t sk_vendor;
248 pci_product_id_t sk_product;
249 } sk_products[] = {
250 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C940, },
251 { PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE, },
252 { PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2, },
253 { 0, 0, }
254 };
255
256 static inline u_int32_t
257 sk_win_read_4(struct sk_softc *sc, u_int32_t reg)
258 {
259 #ifdef SK_USEIOSPACE
260 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
261 return CSR_READ_4(sc, SK_WIN_BASE + SK_REG(reg));
262 #else
263 return CSR_READ_4(sc, reg);
264 #endif
265 }
266
267 static inline u_int16_t
268 sk_win_read_2(struct sk_softc *sc, u_int32_t reg)
269 {
270 #ifdef SK_USEIOSPACE
271 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
272 return CSR_READ_2(sc, SK_WIN_BASE + SK_REG(reg));
273 #else
274 return CSR_READ_2(sc, reg);
275 #endif
276 }
277
278 static inline u_int8_t
279 sk_win_read_1(struct sk_softc *sc, u_int32_t reg)
280 {
281 #ifdef SK_USEIOSPACE
282 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
283 return CSR_READ_1(sc, SK_WIN_BASE + SK_REG(reg));
284 #else
285 return CSR_READ_1(sc, reg);
286 #endif
287 }
288
289 static inline void
290 sk_win_write_4(struct sk_softc *sc, u_int32_t reg, u_int32_t x)
291 {
292 #ifdef SK_USEIOSPACE
293 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
294 CSR_WRITE_4(sc, SK_WIN_BASE + SK_REG(reg), x);
295 #else
296 CSR_WRITE_4(sc, reg, x);
297 #endif
298 }
299
300 static inline void
301 sk_win_write_2(struct sk_softc *sc, u_int32_t reg, u_int16_t x)
302 {
303 #ifdef SK_USEIOSPACE
304 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
305 CSR_WRITE_2(sc, SK_WIN_BASE + SK_REG(reg), x);
306 #else
307 CSR_WRITE_2(sc, reg, x);
308 #endif
309 }
310
311 static inline void
312 sk_win_write_1(struct sk_softc *sc, u_int32_t reg, u_int8_t x)
313 {
314 #ifdef SK_USEIOSPACE
315 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
316 CSR_WRITE_1(sc, SK_WIN_BASE + SK_REG(reg), x);
317 #else
318 CSR_WRITE_1(sc, reg, x);
319 #endif
320 }
321
322 /*
323 * The VPD EEPROM contains Vital Product Data, as suggested in
324 * the PCI 2.1 specification. The VPD data is separared into areas
325 * denoted by resource IDs. The SysKonnect VPD contains an ID string
326 * resource (the name of the adapter), a read-only area resource
327 * containing various key/data fields and a read/write area which
328 * can be used to store asset management information or log messages.
329 * We read the ID string and read-only into buffers attached to
330 * the controller softc structure for later use. At the moment,
331 * we only use the ID string during sk_attach().
332 */
333 u_int8_t
334 sk_vpd_readbyte(struct sk_softc *sc, int addr)
335 {
336 int i;
337
338 sk_win_write_2(sc, SK_PCI_REG(SK_PCI_VPD_ADDR), addr);
339 for (i = 0; i < SK_TIMEOUT; i++) {
340 DELAY(1);
341 if (sk_win_read_2(sc,
342 SK_PCI_REG(SK_PCI_VPD_ADDR)) & SK_VPD_FLAG)
343 break;
344 }
345
346 if (i == SK_TIMEOUT)
347 return(0);
348
349 return(sk_win_read_1(sc, SK_PCI_REG(SK_PCI_VPD_DATA)));
350 }
351
352 void
353 sk_vpd_read_res(struct sk_softc *sc, struct vpd_res *res, int addr)
354 {
355 int i;
356 u_int8_t *ptr;
357
358 ptr = (u_int8_t *)res;
359 for (i = 0; i < sizeof(struct vpd_res); i++)
360 ptr[i] = sk_vpd_readbyte(sc, i + addr);
361 }
362
363 void
364 sk_vpd_read(struct sk_softc *sc)
365 {
366 int pos = 0, i;
367 struct vpd_res res;
368
369 if (sc->sk_vpd_prodname != NULL)
370 free(sc->sk_vpd_prodname, M_DEVBUF);
371 if (sc->sk_vpd_readonly != NULL)
372 free(sc->sk_vpd_readonly, M_DEVBUF);
373 sc->sk_vpd_prodname = NULL;
374 sc->sk_vpd_readonly = NULL;
375
376 sk_vpd_read_res(sc, &res, pos);
377
378 if (res.vr_id != VPD_RES_ID) {
379 printf("%s: bad VPD resource id: expected %x got %x\n",
380 sc->sk_dev.dv_xname, VPD_RES_ID, res.vr_id);
381 return;
382 }
383
384 pos += sizeof(res);
385 sc->sk_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
386 if (sc->sk_vpd_prodname == NULL)
387 panic("sk_vpd_read");
388 for (i = 0; i < res.vr_len; i++)
389 sc->sk_vpd_prodname[i] = sk_vpd_readbyte(sc, i + pos);
390 sc->sk_vpd_prodname[i] = '\0';
391 pos += i;
392
393 sk_vpd_read_res(sc, &res, pos);
394
395 if (res.vr_id != VPD_RES_READ) {
396 printf("%s: bad VPD resource id: expected %x got %x\n",
397 sc->sk_dev.dv_xname, VPD_RES_READ, res.vr_id);
398 return;
399 }
400
401 pos += sizeof(res);
402 sc->sk_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
403 if (sc->sk_vpd_readonly == NULL)
404 panic("sk_vpd_read");
405 for (i = 0; i < res.vr_len + 1; i++)
406 sc->sk_vpd_readonly[i] = sk_vpd_readbyte(sc, i + pos);
407 }
408
409 int
410 sk_xmac_miibus_readreg(struct device *dev, int phy, int reg)
411 {
412 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
413 int i;
414
415 DPRINTFN(9, ("sk_xmac_miibus_readreg\n"));
416
417 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC && phy != 0)
418 return(0);
419
420 SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
421 SK_XM_READ_2(sc_if, XM_PHY_DATA);
422 if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
423 for (i = 0; i < SK_TIMEOUT; i++) {
424 DELAY(1);
425 if (SK_XM_READ_2(sc_if, XM_MMUCMD) &
426 XM_MMUCMD_PHYDATARDY)
427 break;
428 }
429
430 if (i == SK_TIMEOUT) {
431 printf("%s: phy failed to come ready\n",
432 sc_if->sk_dev.dv_xname);
433 return(0);
434 }
435 }
436 DELAY(1);
437 return(SK_XM_READ_2(sc_if, XM_PHY_DATA));
438 }
439
440 void
441 sk_xmac_miibus_writereg(struct device *dev, int phy, int reg, int val)
442 {
443 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
444 int i;
445
446 DPRINTFN(9, ("sk_xmac_miibus_writereg\n"));
447
448 SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
449 for (i = 0; i < SK_TIMEOUT; i++) {
450 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
451 break;
452 }
453
454 if (i == SK_TIMEOUT) {
455 printf("%s: phy failed to come ready\n",
456 sc_if->sk_dev.dv_xname);
457 return;
458 }
459
460 SK_XM_WRITE_2(sc_if, XM_PHY_DATA, val);
461 for (i = 0; i < SK_TIMEOUT; i++) {
462 DELAY(1);
463 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
464 break;
465 }
466
467 if (i == SK_TIMEOUT)
468 printf("%s: phy write timed out\n", sc_if->sk_dev.dv_xname);
469 }
470
471 void
472 sk_xmac_miibus_statchg(struct device *dev)
473 {
474 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
475 struct mii_data *mii = &sc_if->sk_mii;
476
477 DPRINTFN(9, ("sk_xmac_miibus_statchg\n"));
478
479 /*
480 * If this is a GMII PHY, manually set the XMAC's
481 * duplex mode accordingly.
482 */
483 if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
484 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
485 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
486 } else {
487 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
488 }
489 }
490 }
491
492 int
493 sk_marv_miibus_readreg(dev, phy, reg)
494 struct device *dev;
495 int phy, reg;
496 {
497 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
498 u_int16_t val;
499 int i;
500
501 if (phy != 0 ||
502 (sc_if->sk_phytype != SK_PHYTYPE_MARV_COPPER &&
503 sc_if->sk_phytype != SK_PHYTYPE_MARV_FIBER)) {
504 DPRINTFN(9, ("sk_marv_miibus_readreg (skip) phy=%d, reg=%#x\n",
505 phy, reg));
506 return(0);
507 }
508
509 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
510 YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
511
512 for (i = 0; i < SK_TIMEOUT; i++) {
513 DELAY(1);
514 val = SK_YU_READ_2(sc_if, YUKON_SMICR);
515 if (val & YU_SMICR_READ_VALID)
516 break;
517 }
518
519 if (i == SK_TIMEOUT) {
520 printf("%s: phy failed to come ready\n",
521 sc_if->sk_dev.dv_xname);
522 return 0;
523 }
524
525 DPRINTFN(9, ("sk_marv_miibus_readreg: i=%d, timeout=%d\n", i,
526 SK_TIMEOUT));
527
528 val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
529
530 DPRINTFN(9, ("sk_marv_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
531 phy, reg, val));
532
533 return val;
534 }
535
536 void
537 sk_marv_miibus_writereg(dev, phy, reg, val)
538 struct device *dev;
539 int phy, reg, val;
540 {
541 struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
542 int i;
543
544 DPRINTFN(9, ("sk_marv_miibus_writereg phy=%d reg=%#x val=%#x\n",
545 phy, reg, val));
546
547 SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
548 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
549 YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
550
551 for (i = 0; i < SK_TIMEOUT; i++) {
552 DELAY(1);
553 if (SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY)
554 break;
555 }
556 }
557
558 void
559 sk_marv_miibus_statchg(dev)
560 struct device *dev;
561 {
562 DPRINTFN(9, ("sk_marv_miibus_statchg: gpcr=%x\n",
563 SK_YU_READ_2(((struct sk_if_softc *)dev), YUKON_GPCR)));
564 }
565
566 #define SK_BITS 6
567 #define SK_POLY 0xEDB88320
568
569 u_int32_t
570 sk_calchash(caddr_t addr)
571 {
572 u_int32_t crc;
573
574 crc = ether_crc32_be(addr,ETHER_ADDR_LEN);
575 crc = ~crc & ((1<< SK_BITS) - 1);
576 DPRINTFN(2,("multicast hash for %s is %x\n",ether_sprintf(addr),crc));
577 return (crc);
578 }
579
580 void
581 sk_setfilt(struct sk_if_softc *sc_if, caddr_t addr, int slot)
582 {
583 int base = XM_RXFILT_ENTRY(slot);
584
585 SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
586 SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
587 SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
588 }
589
590 void
591 sk_setmulti(struct sk_if_softc *sc_if)
592 {
593 struct sk_softc *sc = sc_if->sk_softc;
594 struct ifnet *ifp= &sc_if->sk_ethercom.ec_if;
595 u_int32_t hashes[2] = { 0, 0 };
596 int h, i;
597 struct ethercom *ec = &sc_if->sk_ethercom;
598 struct ether_multi *enm;
599 struct ether_multistep step;
600 u_int8_t dummy[] = { 0, 0, 0, 0, 0 ,0 };
601
602 /* First, zot all the existing filters. */
603 switch(sc->sk_type) {
604 case SK_GENESIS:
605 for (i = 1; i < XM_RXFILT_MAX; i++)
606 sk_setfilt(sc_if, (caddr_t)&dummy, i);
607
608 SK_XM_WRITE_4(sc_if, XM_MAR0, 0);
609 SK_XM_WRITE_4(sc_if, XM_MAR2, 0);
610 break;
611 case SK_YUKON:
612 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
613 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
614 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
615 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
616 break;
617 }
618
619 /* Now program new ones. */
620 allmulti:
621 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
622 hashes[0] = 0xFFFFFFFF;
623 hashes[1] = 0xFFFFFFFF;
624 } else {
625 i = 1;
626 /* First find the tail of the list. */
627 ETHER_FIRST_MULTI(step, ec, enm);
628 while (enm != NULL) {
629 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
630 ETHER_ADDR_LEN)) {
631 ifp->if_flags |= IFF_ALLMULTI;
632 goto allmulti;
633 }
634 DPRINTFN(2,("multicast address %s\n",
635 ether_sprintf(enm->enm_addrlo)));
636 /*
637 * Program the first XM_RXFILT_MAX multicast groups
638 * into the perfect filter. For all others,
639 * use the hash table.
640 */
641 if (sc->sk_type == SK_GENESIS && i < XM_RXFILT_MAX) {
642 sk_setfilt(sc_if, enm->enm_addrlo, i);
643 i++;
644 }
645 else {
646 h = sk_calchash(enm->enm_addrlo);
647 if (h < 32)
648 hashes[0] |= (1 << h);
649 else
650 hashes[1] |= (1 << (h - 32));
651 }
652
653 ETHER_NEXT_MULTI(step, enm);
654 }
655 }
656
657 switch(sc->sk_type) {
658 case SK_GENESIS:
659 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_HASH|
660 XM_MODE_RX_USE_PERFECT);
661 SK_XM_WRITE_4(sc_if, XM_MAR0, hashes[0]);
662 SK_XM_WRITE_4(sc_if, XM_MAR2, hashes[1]);
663 break;
664 case SK_YUKON:
665 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
666 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
667 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
668 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
669 break;
670 }
671 }
672
673 int
674 sk_init_rx_ring(struct sk_if_softc *sc_if)
675 {
676 struct sk_chain_data *cd = &sc_if->sk_cdata;
677 struct sk_ring_data *rd = sc_if->sk_rdata;
678 int i;
679
680 bzero((char *)rd->sk_rx_ring,
681 sizeof(struct sk_rx_desc) * SK_RX_RING_CNT);
682
683 for (i = 0; i < SK_RX_RING_CNT; i++) {
684 cd->sk_rx_chain[i].sk_desc = &rd->sk_rx_ring[i];
685 if (i == (SK_RX_RING_CNT - 1)) {
686 cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[0];
687 rd->sk_rx_ring[i].sk_next = SK_RX_RING_ADDR(sc_if, 0);
688 } else {
689 cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[i + 1];
690 rd->sk_rx_ring[i].sk_next = SK_RX_RING_ADDR(sc_if,i+1);
691 }
692 }
693
694 for (i = 0; i < SK_RX_RING_CNT; i++) {
695 if (sk_newbuf(sc_if, i, NULL, NULL) == ENOBUFS) {
696 printf("%s: failed alloc of %dth mbuf\n",
697 sc_if->sk_dev.dv_xname, i);
698 return(ENOBUFS);
699 }
700 }
701 sc_if->sk_cdata.sk_rx_prod = 0;
702 sc_if->sk_cdata.sk_rx_cons = 0;
703
704 return(0);
705 }
706
707 int
708 sk_init_tx_ring(struct sk_if_softc *sc_if)
709 {
710 struct sk_softc *sc = sc_if->sk_softc;
711 struct sk_chain_data *cd = &sc_if->sk_cdata;
712 struct sk_ring_data *rd = sc_if->sk_rdata;
713 bus_dmamap_t dmamap;
714 struct sk_txmap_entry *entry;
715 int i;
716
717 bzero((char *)sc_if->sk_rdata->sk_tx_ring,
718 sizeof(struct sk_tx_desc) * SK_TX_RING_CNT);
719
720 SLIST_INIT(&sc_if->sk_txmap_listhead);
721 for (i = 0; i < SK_TX_RING_CNT; i++) {
722 cd->sk_tx_chain[i].sk_desc = &rd->sk_tx_ring[i];
723 if (i == (SK_TX_RING_CNT - 1)) {
724 cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[0];
725 rd->sk_tx_ring[i].sk_next = SK_TX_RING_ADDR(sc_if, 0);
726 } else {
727 cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[i + 1];
728 rd->sk_tx_ring[i].sk_next = SK_TX_RING_ADDR(sc_if,i+1);
729 }
730
731 if (bus_dmamap_create(sc->sc_dmatag, MCLBYTES, SK_NTXSEG,
732 MCLBYTES, 0, BUS_DMA_NOWAIT, &dmamap))
733 return (ENOBUFS);
734
735 entry = malloc(sizeof(*entry), M_DEVBUF, M_NOWAIT);
736 if (!entry) {
737 bus_dmamap_destroy(sc->sc_dmatag, dmamap);
738 return (ENOBUFS);
739 }
740 entry->dmamap = dmamap;
741 SLIST_INSERT_HEAD(&sc_if->sk_txmap_listhead, entry, link);
742 }
743
744 sc_if->sk_cdata.sk_tx_prod = 0;
745 sc_if->sk_cdata.sk_tx_cons = 0;
746 sc_if->sk_cdata.sk_tx_cnt = 0;
747
748 return (0);
749 }
750
751 int
752 sk_newbuf(struct sk_if_softc *sc_if, int i, struct mbuf *m,
753 bus_dmamap_t dmamap)
754 {
755 struct sk_softc *sc = sc_if->sk_softc;
756 struct mbuf *m_new = NULL;
757 struct sk_chain *c;
758 struct sk_rx_desc *r;
759
760 if (dmamap == NULL) {
761 /* if (m) panic() */
762
763 if (bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1, MCLBYTES,
764 0, BUS_DMA_NOWAIT, &dmamap)) {
765 printf("%s: can't create recv map\n",
766 sc_if->sk_dev.dv_xname);
767 return(ENOMEM);
768 }
769 } else if (m == NULL)
770 bus_dmamap_unload(sc->sc_dmatag, dmamap);
771
772 sc_if->sk_cdata.sk_rx_map[i] = dmamap;
773
774 if (m == NULL) {
775 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
776 if (m_new == NULL) {
777 printf("%s: no memory for rx list -- "
778 "packet dropped!\n", sc_if->sk_dev.dv_xname);
779 return(ENOBUFS);
780 }
781
782 /* Allocate the jumbo buffer */
783 MCLGET(m_new, M_DONTWAIT);
784 if (!(m_new->m_flags & M_EXT)) {
785 m_freem(m_new);
786 return (ENOBUFS);
787 }
788
789 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
790
791 m_adj(m_new, ETHER_ALIGN);
792
793 if (bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap, m_new,
794 BUS_DMA_NOWAIT))
795 return(ENOBUFS);
796 } else {
797 /*
798 * We're re-using a previously allocated mbuf;
799 * be sure to re-init pointers and lengths to
800 * default values.
801 */
802 m_new = m;
803 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
804 m_adj(m_new, ETHER_ALIGN);
805 m_new->m_data = m_new->m_ext.ext_buf;
806 }
807
808 c = &sc_if->sk_cdata.sk_rx_chain[i];
809 r = c->sk_desc;
810 c->sk_mbuf = m_new;
811 r->sk_data_lo = dmamap->dm_segs[0].ds_addr;
812 r->sk_ctl = dmamap->dm_segs[0].ds_len | SK_RXSTAT;
813
814 return(0);
815 }
816
817 /*
818 * Set media options.
819 */
820 int
821 sk_ifmedia_upd(struct ifnet *ifp)
822 {
823 struct sk_if_softc *sc_if = ifp->if_softc;
824
825 (void) sk_init(ifp);
826 mii_mediachg(&sc_if->sk_mii);
827 return(0);
828 }
829
830 /*
831 * Report current media status.
832 */
833 void
834 sk_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
835 {
836 struct sk_if_softc *sc_if = ifp->if_softc;
837
838 mii_pollstat(&sc_if->sk_mii);
839 ifmr->ifm_active = sc_if->sk_mii.mii_media_active;
840 ifmr->ifm_status = sc_if->sk_mii.mii_media_status;
841 }
842
843 int
844 sk_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
845 {
846 struct sk_if_softc *sc_if = ifp->if_softc;
847 struct sk_softc *sc = sc_if->sk_softc;
848 struct ifreq *ifr = (struct ifreq *) data;
849 /* struct ifaddr *ifa = (struct ifaddr *) data; */
850 struct mii_data *mii;
851 int s, error = 0;
852
853 /* DPRINTFN(2, ("sk_ioctl\n")); */
854
855 s = splnet();
856
857 switch(command) {
858
859 case SIOCSIFFLAGS:
860 DPRINTFN(2, ("sk_ioctl IFFLAGS\n"));
861 if (ifp->if_flags & IFF_UP) {
862 if (ifp->if_flags & IFF_RUNNING &&
863 ifp->if_flags & IFF_PROMISC &&
864 !(sc_if->sk_if_flags & IFF_PROMISC)) {
865 switch(sc->sk_type) {
866 case SK_GENESIS:
867 SK_XM_SETBIT_4(sc_if, XM_MODE,
868 XM_MODE_RX_PROMISC);
869 break;
870 case SK_YUKON:
871 SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
872 YU_RCR_UFLEN | YU_RCR_MUFLEN);
873 break;
874 }
875 sk_setmulti(sc_if);
876 } else if (ifp->if_flags & IFF_RUNNING &&
877 !(ifp->if_flags & IFF_PROMISC) &&
878 sc_if->sk_if_flags & IFF_PROMISC) {
879 switch(sc->sk_type) {
880 case SK_GENESIS:
881 SK_XM_CLRBIT_4(sc_if, XM_MODE,
882 XM_MODE_RX_PROMISC);
883 break;
884 case SK_YUKON:
885 SK_YU_SETBIT_2(sc_if, YUKON_RCR,
886 YU_RCR_UFLEN | YU_RCR_MUFLEN);
887 break;
888 }
889
890 sk_setmulti(sc_if);
891 } else
892 (void) sk_init(ifp);
893 } else {
894 if (ifp->if_flags & IFF_RUNNING)
895 sk_stop(ifp,0);
896 }
897 sc_if->sk_if_flags = ifp->if_flags;
898 error = 0;
899 break;
900
901 case SIOCGIFMEDIA:
902 case SIOCSIFMEDIA:
903 DPRINTFN(2, ("sk_ioctl MEDIA\n"));
904 mii = &sc_if->sk_mii;
905 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
906 break;
907 default:
908 DPRINTFN(2, ("sk_ioctl ETHER\n"));
909 error = ether_ioctl(ifp, command, data);
910
911 if ( error == ENETRESET) {
912 sk_setmulti(sc_if);
913 DPRINTFN(2, ("sk_ioctl setmulti called\n"));
914 error = 0;
915 } else if ( error ) {
916 splx(s);
917 return error;
918 }
919 break;
920 }
921
922 splx(s);
923 return(error);
924 }
925
926 /*
927 * Lookup: Check the PCI vendor and device, and return a pointer to
928 * The structure if the IDs match against our list.
929 */
930
931 static const struct sk_product *
932 sk_lookup(const struct pci_attach_args *pa)
933 {
934 const struct sk_product *psk;
935
936 for ( psk = &sk_products[0]; psk->sk_vendor != NULL; psk++ ) {
937 if (PCI_VENDOR(pa->pa_id) == psk->sk_vendor &&
938 PCI_PRODUCT(pa->pa_id) == psk->sk_product)
939 return (psk);
940 }
941 return (NULL);
942 }
943
944 /*
945 * Probe for a SysKonnect GEnesis chip.
946 */
947
948 int
949 skc_probe(struct device *parent, struct cfdata *match, void *aux)
950 {
951 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
952 const struct sk_product *psk;
953
954 if ((psk = sk_lookup(pa))) {
955 return(1);
956 }
957 return(0);
958 }
959
960 /*
961 * Force the GEnesis into reset, then bring it out of reset.
962 */
963 void sk_reset(struct sk_softc *sc)
964 {
965 DPRINTFN(2, ("sk_reset\n"));
966
967 CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_RESET);
968 CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_RESET);
969 if (sc->sk_type == SK_YUKON)
970 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
971
972 DELAY(1000);
973 CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_UNRESET);
974 DELAY(2);
975 CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
976 if (sc->sk_type == SK_YUKON)
977 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
978
979 DPRINTFN(2, ("sk_reset: sk_csr=%x\n", CSR_READ_2(sc, SK_CSR)));
980 DPRINTFN(2, ("sk_reset: sk_link_ctrl=%x\n",
981 CSR_READ_2(sc, SK_LINK_CTRL)));
982
983 if (sc->sk_type == SK_GENESIS) {
984 /* Configure packet arbiter */
985 sk_win_write_2(sc, SK_PKTARB_CTL, SK_PKTARBCTL_UNRESET);
986 sk_win_write_2(sc, SK_RXPA1_TINIT, SK_PKTARB_TIMEOUT);
987 sk_win_write_2(sc, SK_TXPA1_TINIT, SK_PKTARB_TIMEOUT);
988 sk_win_write_2(sc, SK_RXPA2_TINIT, SK_PKTARB_TIMEOUT);
989 sk_win_write_2(sc, SK_TXPA2_TINIT, SK_PKTARB_TIMEOUT);
990 }
991
992 /* Enable RAM interface */
993 sk_win_write_4(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
994
995 /*
996 * Configure interrupt moderation. The moderation timer
997 * defers interrupts specified in the interrupt moderation
998 * timer mask based on the timeout specified in the interrupt
999 * moderation timer init register. Each bit in the timer
1000 * register represents 18.825ns, so to specify a timeout in
1001 * microseconds, we have to multiply by 54.
1002 */
1003 sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(200));
1004 sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
1005 SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
1006 sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
1007 }
1008
1009 int
1010 sk_probe(struct device *parent, struct cfdata *match, void *aux)
1011 {
1012 struct skc_attach_args *sa = aux;
1013
1014 if (sa->skc_port != SK_PORT_A && sa->skc_port != SK_PORT_B)
1015 return(0);
1016
1017 return (1);
1018 }
1019
1020 /*
1021 * Each XMAC chip is attached as a separate logical IP interface.
1022 * Single port cards will have only one logical interface of course.
1023 */
1024 void
1025 sk_attach(struct device *parent, struct device *self, void *aux)
1026 {
1027 struct sk_if_softc *sc_if = (struct sk_if_softc *) self;
1028 struct sk_softc *sc = (struct sk_softc *)parent;
1029 struct skc_attach_args *sa = aux;
1030 struct ifnet *ifp;
1031 caddr_t kva;
1032 bus_dma_segment_t seg;
1033 int i, rseg;
1034
1035 sc_if->sk_port = sa->skc_port;
1036 sc_if->sk_softc = sc;
1037 sc->sk_if[sa->skc_port] = sc_if;
1038
1039 if (sa->skc_port == SK_PORT_A)
1040 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR0;
1041 if (sa->skc_port == SK_PORT_B)
1042 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR1;
1043
1044 DPRINTFN(2, ("begin sk_attach: port=%d\n", sc_if->sk_port));
1045
1046 /*
1047 * Get station address for this interface. Note that
1048 * dual port cards actually come with three station
1049 * addresses: one for each port, plus an extra. The
1050 * extra one is used by the SysKonnect driver software
1051 * as a 'virtual' station address for when both ports
1052 * are operating in failover mode. Currently we don't
1053 * use this extra address.
1054 */
1055 for (i = 0; i < ETHER_ADDR_LEN; i++)
1056 sc_if->sk_enaddr[i] =
1057 sk_win_read_1(sc, SK_MAC0_0 + (sa->skc_port * 8) + i);
1058
1059
1060 printf(": Ethernet address %s\n",
1061 ether_sprintf(sc_if->sk_enaddr));
1062
1063 /*
1064 * Set up RAM buffer addresses. The NIC will have a certain
1065 * amount of SRAM on it, somewhere between 512K and 2MB. We
1066 * need to divide this up a) between the transmitter and
1067 * receiver and b) between the two XMACs, if this is a
1068 * dual port NIC. Our algotithm is to divide up the memory
1069 * evenly so that everyone gets a fair share.
1070 */
1071 if (sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC) {
1072 u_int32_t chunk, val;
1073
1074 chunk = sc->sk_ramsize / 2;
1075 val = sc->sk_rboff / sizeof(u_int64_t);
1076 sc_if->sk_rx_ramstart = val;
1077 val += (chunk / sizeof(u_int64_t));
1078 sc_if->sk_rx_ramend = val - 1;
1079 sc_if->sk_tx_ramstart = val;
1080 val += (chunk / sizeof(u_int64_t));
1081 sc_if->sk_tx_ramend = val - 1;
1082 } else {
1083 u_int32_t chunk, val;
1084
1085 chunk = sc->sk_ramsize / 4;
1086 val = (sc->sk_rboff + (chunk * 2 * sc_if->sk_port)) /
1087 sizeof(u_int64_t);
1088 sc_if->sk_rx_ramstart = val;
1089 val += (chunk / sizeof(u_int64_t));
1090 sc_if->sk_rx_ramend = val - 1;
1091 sc_if->sk_tx_ramstart = val;
1092 val += (chunk / sizeof(u_int64_t));
1093 sc_if->sk_tx_ramend = val - 1;
1094 }
1095
1096 DPRINTFN(2, ("sk_attach: rx_ramstart=%#x rx_ramend=%#x\n"
1097 " tx_ramstart=%#x tx_ramend=%#x\n",
1098 sc_if->sk_rx_ramstart, sc_if->sk_rx_ramend,
1099 sc_if->sk_tx_ramstart, sc_if->sk_tx_ramend));
1100
1101 /* Read and save PHY type and set PHY address */
1102 sc_if->sk_phytype = sk_win_read_1(sc, SK_EPROM1) & 0xF;
1103 switch (sc_if->sk_phytype) {
1104 case SK_PHYTYPE_XMAC:
1105 sc_if->sk_phyaddr = SK_PHYADDR_XMAC;
1106 break;
1107 case SK_PHYTYPE_BCOM:
1108 sc_if->sk_phyaddr = SK_PHYADDR_BCOM;
1109 break;
1110 case SK_PHYTYPE_MARV_COPPER:
1111 sc_if->sk_phyaddr = SK_PHYADDR_MARV;
1112 break;
1113 default:
1114 printf("%s: unsupported PHY type: %d\n",
1115 sc->sk_dev.dv_xname, sc_if->sk_phytype);
1116 return;
1117 }
1118
1119 /* Allocate the descriptor queues. */
1120 if (bus_dmamem_alloc(sc->sc_dmatag, sizeof(struct sk_ring_data),
1121 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1122 printf("%s: can't alloc rx buffers\n", sc->sk_dev.dv_xname);
1123 goto fail;
1124 }
1125 if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
1126 sizeof(struct sk_ring_data), &kva, BUS_DMA_NOWAIT)) {
1127 printf("%s: can't map dma buffers (%d bytes)\n",
1128 sc_if->sk_dev.dv_xname, sizeof(struct sk_ring_data));
1129 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1130 goto fail;
1131 }
1132 if (bus_dmamap_create(sc->sc_dmatag, sizeof(struct sk_ring_data), 1,
1133 sizeof(struct sk_ring_data), 0, BUS_DMA_NOWAIT,
1134 &sc_if->sk_ring_map)) {
1135 printf("%s: can't create dma map\n", sc_if->sk_dev.dv_xname);
1136 bus_dmamem_unmap(sc->sc_dmatag, kva,
1137 sizeof(struct sk_ring_data));
1138 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1139 goto fail;
1140 }
1141 if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_ring_map, kva,
1142 sizeof(struct sk_ring_data), NULL, BUS_DMA_NOWAIT)) {
1143 printf("%s: can't load dma map\n", sc_if->sk_dev.dv_xname);
1144 bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
1145 bus_dmamem_unmap(sc->sc_dmatag, kva,
1146 sizeof(struct sk_ring_data));
1147 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1148 goto fail;
1149 }
1150 sc_if->sk_rdata = (struct sk_ring_data *)kva;
1151 bzero(sc_if->sk_rdata, sizeof(struct sk_ring_data));
1152
1153 ifp = &sc_if->sk_ethercom.ec_if;
1154 ifp->if_softc = sc_if;
1155 ifp->if_mtu = ETHERMTU;
1156 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1157 ifp->if_ioctl = sk_ioctl;
1158 ifp->if_start = sk_start;
1159 ifp->if_stop = sk_stop;
1160 ifp->if_init = sk_init;
1161 ifp->if_watchdog = sk_watchdog;
1162 ifp->if_baudrate = 1000000000;
1163 ifp->if_capabilities |= ETHERCAP_VLAN_MTU;
1164 IFQ_SET_MAXLEN(&ifp->if_snd, SK_TX_RING_CNT - 1);
1165 IFQ_SET_READY(&ifp->if_snd);
1166 bcopy(sc_if->sk_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
1167
1168 /*
1169 * Do miibus setup.
1170 */
1171 switch (sc->sk_type) {
1172 case SK_GENESIS:
1173 sk_init_xmac(sc_if);
1174 break;
1175 case SK_YUKON:
1176 sk_init_yukon(sc_if);
1177 break;
1178 default:
1179 panic("%s: unknown device type %d", sc->sk_dev.dv_xname,
1180 sc->sk_type);
1181 }
1182
1183 DPRINTFN(2, ("sk_attach: 1\n"));
1184
1185 sc_if->sk_mii.mii_ifp = ifp;
1186 switch (sc->sk_type) {
1187 case SK_GENESIS:
1188 sc_if->sk_mii.mii_readreg = sk_xmac_miibus_readreg;
1189 sc_if->sk_mii.mii_writereg = sk_xmac_miibus_writereg;
1190 sc_if->sk_mii.mii_statchg = sk_xmac_miibus_statchg;
1191 break;
1192 case SK_YUKON:
1193 sc_if->sk_mii.mii_readreg = sk_marv_miibus_readreg;
1194 sc_if->sk_mii.mii_writereg = sk_marv_miibus_writereg;
1195 sc_if->sk_mii.mii_statchg = sk_marv_miibus_statchg;
1196 break;
1197 }
1198
1199 ifmedia_init(&sc_if->sk_mii.mii_media, 0,
1200 sk_ifmedia_upd, sk_ifmedia_sts);
1201 mii_attach(self, &sc_if->sk_mii, 0xffffffff, MII_PHY_ANY,
1202 MII_OFFSET_ANY, 0);
1203 if (LIST_FIRST(&sc_if->sk_mii.mii_phys) == NULL) {
1204 printf("%s: no PHY found!\n", sc_if->sk_dev.dv_xname);
1205 ifmedia_add(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL,
1206 0, NULL);
1207 ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL);
1208 }
1209 else
1210 ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_AUTO);
1211
1212 callout_init(&sc_if->sk_tick_ch);
1213 callout_reset(&sc_if->sk_tick_ch,hz,sk_tick,sc_if);
1214
1215 DPRINTFN(2, ("sk_attach: 1\n"));
1216
1217 /*
1218 * Call MI attach routines.
1219 */
1220 if_attach(ifp);
1221
1222 ether_ifattach(ifp, sc_if->sk_enaddr);
1223
1224 #if NRND > 0
1225 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
1226 RND_TYPE_NET, 0);
1227 #endif
1228
1229 DPRINTFN(2, ("sk_attach: end\n"));
1230
1231 return;
1232
1233 fail:
1234 sc->sk_if[sa->skc_port] = NULL;
1235 }
1236
1237 int
1238 skcprint(void *aux, const char *pnp)
1239 {
1240 struct skc_attach_args *sa = aux;
1241
1242 if (pnp)
1243 printf("sk port %c at %s",
1244 (sa->skc_port == SK_PORT_A) ? 'A' : 'B', pnp);
1245 else
1246 printf(" port %c", (sa->skc_port == SK_PORT_A) ? 'A' : 'B');
1247 return (UNCONF);
1248 }
1249
1250 /*
1251 * Attach the interface. Allocate softc structures, do ifmedia
1252 * setup and ethernet/BPF attach.
1253 */
1254 void
1255 skc_attach(struct device *parent, struct device *self, void *aux)
1256 {
1257 struct sk_softc *sc = (struct sk_softc *)self;
1258 struct pci_attach_args *pa = aux;
1259 struct skc_attach_args skca;
1260 pci_chipset_tag_t pc = pa->pa_pc;
1261 pcireg_t memtype;
1262 pci_intr_handle_t ih;
1263 const char *intrstr = NULL;
1264 bus_addr_t iobase;
1265 bus_size_t iosize;
1266 int s;
1267 u_int32_t command;
1268
1269 DPRINTFN(2, ("begin skc_attach\n"));
1270
1271 s = splnet();
1272
1273 /*
1274 * Handle power management nonsense.
1275 */
1276 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_CAPID) & 0x000000FF;
1277
1278 if (command == 0x01) {
1279 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_PWRMGMTCTRL);
1280 if (command & SK_PSTATE_MASK) {
1281 u_int32_t iobase, membase, irq;
1282
1283 /* Save important PCI config data. */
1284 iobase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOIO);
1285 membase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOMEM);
1286 irq = pci_conf_read(pc, pa->pa_tag, SK_PCI_INTLINE);
1287
1288 /* Reset the power state. */
1289 printf("%s chip is in D%d power mode "
1290 "-- setting to D0\n", sc->sk_dev.dv_xname,
1291 command & SK_PSTATE_MASK);
1292 command &= 0xFFFFFFFC;
1293 pci_conf_write(pc, pa->pa_tag,
1294 SK_PCI_PWRMGMTCTRL, command);
1295
1296 /* Restore PCI config data. */
1297 pci_conf_write(pc, pa->pa_tag, SK_PCI_LOIO, iobase);
1298 pci_conf_write(pc, pa->pa_tag, SK_PCI_LOMEM, membase);
1299 pci_conf_write(pc, pa->pa_tag, SK_PCI_INTLINE, irq);
1300 }
1301 }
1302
1303 /*
1304 * Map control/status registers.
1305 */
1306 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1307 command |= PCI_COMMAND_IO_ENABLE |
1308 PCI_COMMAND_MEM_ENABLE |
1309 PCI_COMMAND_MASTER_ENABLE;
1310 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
1311 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1312
1313 switch (PCI_PRODUCT(pa->pa_id)) {
1314 case PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE:
1315 sc->sk_type = SK_GENESIS;
1316 break;
1317 case PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2:
1318 case PCI_PRODUCT_3COM_3C940:
1319 sc->sk_type = SK_YUKON;
1320 break;
1321 default:
1322 printf(": unknown device!\n");
1323 goto fail;
1324 }
1325
1326 #ifdef SK_USEIOSPACE
1327 if (!(command & PCI_COMMAND_IO_ENABLE)) {
1328 printf(": failed to enable I/O ports!\n");
1329 goto fail;
1330 }
1331 /*
1332 * Map control/status registers.
1333 */
1334 if (pci_mapreg_map(pa, SK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
1335 &iobase, &iosize)) {
1336 printf(": can't find i/o space\n");
1337 goto fail;
1338 }
1339 #else
1340 if (!(command & PCI_COMMAND_MEM_ENABLE)) {
1341 printf(": failed to enable memory mapping!\n");
1342 goto fail;
1343 }
1344 memtype = pci_mapreg_type(pc, pa->pa_tag, SK_PCI_LOMEM);
1345 switch (memtype) {
1346 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1347 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1348 if (pci_mapreg_map(pa, SK_PCI_LOMEM,
1349 memtype, 0, &sc->sk_btag, &sc->sk_bhandle,
1350 &iobase, &iosize) == 0)
1351 break;
1352 default:
1353 printf("%s: can't find mem space\n",
1354 sc->sk_dev.dv_xname);
1355 return;
1356 }
1357
1358 DPRINTFN(2, ("skc_attach: iobase=%lx, iosize=%lx\n", iobase, iosize));
1359 #endif
1360 sc->sc_dmatag = pa->pa_dmat;
1361
1362 DPRINTFN(2, ("skc_attach: allocate interrupt\n"));
1363
1364 /* Allocate interrupt */
1365 if (pci_intr_map(pa, &ih)) {
1366 printf(": couldn't map interrupt\n");
1367 goto fail;
1368 }
1369
1370 intrstr = pci_intr_string(pc, ih);
1371 sc->sk_intrhand = pci_intr_establish(pc, ih, IPL_NET, sk_intr, sc);
1372 if (sc->sk_intrhand == NULL) {
1373 printf(": couldn't establish interrupt");
1374 if (intrstr != NULL)
1375 printf(" at %s", intrstr);
1376 goto fail;
1377 }
1378 printf(": %s\n", intrstr);
1379
1380 /* Reset the adapter. */
1381 sk_reset(sc);
1382
1383 /* Read and save vital product data from EEPROM. */
1384 sk_vpd_read(sc);
1385
1386 if (sc->sk_type == SK_GENESIS) {
1387 u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
1388 /* Read and save RAM size and RAMbuffer offset */
1389 switch(val) {
1390 case SK_RAMSIZE_512K_64:
1391 sc->sk_ramsize = 0x80000;
1392 sc->sk_rboff = SK_RBOFF_0;
1393 break;
1394 case SK_RAMSIZE_1024K_64:
1395 sc->sk_ramsize = 0x100000;
1396 sc->sk_rboff = SK_RBOFF_80000;
1397 break;
1398 case SK_RAMSIZE_1024K_128:
1399 sc->sk_ramsize = 0x100000;
1400 sc->sk_rboff = SK_RBOFF_0;
1401 break;
1402 case SK_RAMSIZE_2048K_128:
1403 sc->sk_ramsize = 0x200000;
1404 sc->sk_rboff = SK_RBOFF_0;
1405 break;
1406 default:
1407 printf("%s: unknown ram size: %d\n",
1408 sc->sk_dev.dv_xname, val);
1409 goto fail;
1410 break;
1411 }
1412
1413 DPRINTFN(2, ("skc_attach: ramsize=%d(%dk), rboff=%d\n",
1414 sc->sk_ramsize, sc->sk_ramsize / 1024,
1415 sc->sk_rboff));
1416 } else {
1417 sc->sk_ramsize = 0x20000;
1418 sc->sk_rboff = SK_RBOFF_0;
1419
1420 DPRINTFN(2, ("skc_attach: ramsize=%dk (%d), rboff=%d\n",
1421 sc->sk_ramsize / 1024, sc->sk_ramsize,
1422 sc->sk_rboff));
1423 }
1424
1425 /* Read and save physical media type */
1426 switch(sk_win_read_1(sc, SK_PMDTYPE)) {
1427 case SK_PMD_1000BASESX:
1428 sc->sk_pmd = IFM_1000_SX;
1429 break;
1430 case SK_PMD_1000BASELX:
1431 sc->sk_pmd = IFM_1000_LX;
1432 break;
1433 case SK_PMD_1000BASECX:
1434 sc->sk_pmd = IFM_1000_CX;
1435 break;
1436 case SK_PMD_1000BASETX:
1437 sc->sk_pmd = IFM_1000_T;
1438 break;
1439 default:
1440 printf("%s: unknown media type: 0x%x\n",
1441 sc->sk_dev.dv_xname, sk_win_read_1(sc, SK_PMDTYPE));
1442 goto fail;
1443 }
1444
1445 /* Announce the product name. */
1446 printf("%s: %s\n", sc->sk_dev.dv_xname, sc->sk_vpd_prodname);
1447
1448 skca.skc_port = SK_PORT_A;
1449 (void)config_found(&sc->sk_dev, &skca, skcprint);
1450
1451 if (!(sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC)) {
1452 skca.skc_port = SK_PORT_B;
1453 (void)config_found(&sc->sk_dev, &skca, skcprint);
1454 }
1455
1456 /* Turn on the 'driver is loaded' LED. */
1457 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
1458
1459 fail:
1460 splx(s);
1461 }
1462
1463 int
1464 sk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, u_int32_t *txidx)
1465 {
1466 struct sk_softc *sc = sc_if->sk_softc;
1467 struct sk_tx_desc *f = NULL;
1468 u_int32_t frag, cur, cnt = 0;
1469 int i;
1470 struct sk_txmap_entry *entry;
1471 bus_dmamap_t txmap;
1472
1473 DPRINTFN(3, ("sk_encap\n"));
1474
1475 entry = SLIST_FIRST(&sc_if->sk_txmap_listhead);
1476 if (entry == NULL) {
1477 DPRINTFN(3, ("sk_encap: no txmap available\n"));
1478 return ENOBUFS;
1479 }
1480 txmap = entry->dmamap;
1481
1482 cur = frag = *txidx;
1483
1484 #ifdef SK_DEBUG
1485 if (skdebug >= 3)
1486 sk_dump_mbuf(m_head);
1487 #endif
1488
1489 /*
1490 * Start packing the mbufs in this chain into
1491 * the fragment pointers. Stop when we run out
1492 * of fragments or hit the end of the mbuf chain.
1493 */
1494 if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
1495 BUS_DMA_NOWAIT)) {
1496 DPRINTFN(1, ("sk_encap: dmamap failed\n"));
1497 return(ENOBUFS);
1498 }
1499
1500 DPRINTFN(3, ("sk_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1501
1502 for (i = 0; i < txmap->dm_nsegs; i++) {
1503 if ((SK_TX_RING_CNT - (sc_if->sk_cdata.sk_tx_cnt + cnt)) < 2) {
1504 DPRINTFN(1, ("sk_encap: too few descriptors free\n"));
1505 return(ENOBUFS);
1506 }
1507 f = &sc_if->sk_rdata->sk_tx_ring[frag];
1508 f->sk_data_lo = txmap->dm_segs[i].ds_addr;
1509 f->sk_ctl = txmap->dm_segs[i].ds_len | SK_OPCODE_DEFAULT;
1510 if (cnt == 0)
1511 f->sk_ctl |= SK_TXCTL_FIRSTFRAG;
1512 else
1513 f->sk_ctl |= SK_TXCTL_OWN;
1514
1515 cur = frag;
1516 SK_INC(frag, SK_TX_RING_CNT);
1517 cnt++;
1518 }
1519
1520 sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
1521 SLIST_REMOVE_HEAD(&sc_if->sk_txmap_listhead, link);
1522 sc_if->sk_cdata.sk_tx_map[cur] = entry;
1523 sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |=
1524 SK_TXCTL_LASTFRAG|SK_TXCTL_EOF_INTR;
1525 sc_if->sk_rdata->sk_tx_ring[*txidx].sk_ctl |= SK_TXCTL_OWN;
1526 sc_if->sk_cdata.sk_tx_cnt += cnt;
1527
1528 #ifdef SK_DEBUG
1529 if (skdebug >= 3) {
1530 struct sk_tx_desc *desc;
1531 u_int32_t idx;
1532 for (idx = *txidx; idx != frag; SK_INC(idx, SK_TX_RING_CNT)) {
1533 desc = &sc_if->sk_rdata->sk_tx_ring[idx];
1534 sk_dump_txdesc(desc, idx);
1535 }
1536 }
1537 #endif
1538
1539 *txidx = frag;
1540
1541 DPRINTFN(3, ("sk_encap: completed successfully\n"));
1542
1543 return(0);
1544 }
1545
1546 void
1547 sk_start(struct ifnet *ifp)
1548 {
1549 struct sk_if_softc *sc_if = ifp->if_softc;
1550 struct sk_softc *sc = sc_if->sk_softc;
1551 struct mbuf *m_head = NULL;
1552 u_int32_t idx = sc_if->sk_cdata.sk_tx_prod;
1553 int pkts = 0;
1554
1555 DPRINTFN(3, ("sk_start\n"));
1556
1557 while(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
1558 IFQ_POLL(&ifp->if_snd, m_head);
1559 if (m_head == NULL)
1560 break;
1561
1562 /*
1563 * Pack the data into the transmit ring. If we
1564 * don't have room, set the OACTIVE flag and wait
1565 * for the NIC to drain the ring.
1566 */
1567 if (sk_encap(sc_if, m_head, &idx)) {
1568 ifp->if_flags |= IFF_OACTIVE;
1569 break;
1570 }
1571
1572 /* now we are committed to transmit the packet */
1573 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1574 pkts++;
1575
1576 /*
1577 * If there's a BPF listener, bounce a copy of this frame
1578 * to him.
1579 */
1580 #if NBPFILTER > 0
1581 if (ifp->if_bpf)
1582 bpf_mtap(ifp->if_bpf, m_head);
1583 #endif
1584 }
1585 if (pkts == 0)
1586 return;
1587
1588 /* Transmit */
1589 sc_if->sk_cdata.sk_tx_prod = idx;
1590 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
1591
1592 /* Set a timeout in case the chip goes out to lunch. */
1593 ifp->if_timer = 5;
1594 }
1595
1596
1597 void
1598 sk_watchdog(struct ifnet *ifp)
1599 {
1600 struct sk_if_softc *sc_if = ifp->if_softc;
1601
1602 printf("%s: watchdog timeout\n", sc_if->sk_dev.dv_xname);
1603 (void) sk_init(ifp);
1604 }
1605
1606 void
1607 sk_shutdown(void * v)
1608 {
1609 struct sk_if_softc *sc_if = (struct sk_if_softc *)v;
1610 struct sk_softc *sc = sc_if->sk_softc;
1611 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1612
1613 DPRINTFN(2, ("sk_shutdown\n"));
1614 sk_stop(ifp,1);
1615
1616 /* Turn off the 'driver is loaded' LED. */
1617 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
1618
1619 /*
1620 * Reset the GEnesis controller. Doing this should also
1621 * assert the resets on the attached XMAC(s).
1622 */
1623 sk_reset(sc);
1624 }
1625
1626 void
1627 sk_rxeof(struct sk_if_softc *sc_if)
1628 {
1629 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1630 struct mbuf *m;
1631 struct sk_chain *cur_rx;
1632 struct sk_rx_desc *cur_desc;
1633 int i, cur, total_len = 0;
1634 u_int32_t rxstat;
1635 bus_dmamap_t dmamap;
1636
1637 DPRINTFN(3, ("sk_rxeof\n"));
1638
1639 i = sc_if->sk_cdata.sk_rx_prod;
1640
1641 while(!(sc_if->sk_rdata->sk_rx_ring[i].sk_ctl & SK_RXCTL_OWN)) {
1642 cur = i;
1643 cur_rx = &sc_if->sk_cdata.sk_rx_chain[cur];
1644 cur_desc = &sc_if->sk_rdata->sk_rx_ring[cur];
1645
1646 rxstat = cur_desc->sk_xmac_rxstat;
1647 m = cur_rx->sk_mbuf;
1648 cur_rx->sk_mbuf = NULL;
1649 total_len = SK_RXBYTES(cur_desc->sk_ctl);
1650
1651 dmamap = sc_if->sk_cdata.sk_rx_map[cur];
1652 sc_if->sk_cdata.sk_rx_map[cur] = 0;
1653
1654 SK_INC(i, SK_RX_RING_CNT);
1655
1656 if (rxstat & XM_RXSTAT_ERRFRAME) {
1657 ifp->if_ierrors++;
1658 sk_newbuf(sc_if, cur, m, dmamap);
1659 continue;
1660 }
1661
1662 /*
1663 * Try to allocate a new jumbo buffer. If that
1664 * fails, copy the packet to mbufs and put the
1665 * jumbo buffer back in the ring so it can be
1666 * re-used. If allocating mbufs fails, then we
1667 * have to drop the packet.
1668 */
1669 if (sk_newbuf(sc_if, cur, NULL, dmamap) == ENOBUFS) {
1670 struct mbuf *m0;
1671 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1672 total_len + ETHER_ALIGN, 0, ifp, NULL);
1673 sk_newbuf(sc_if, cur, m, dmamap);
1674 if (m0 == NULL) {
1675 printf("%s: no receive buffers "
1676 "available -- packet dropped!\n",
1677 sc_if->sk_dev.dv_xname);
1678 ifp->if_ierrors++;
1679 continue;
1680 }
1681 m_adj(m0, ETHER_ALIGN);
1682 m = m0;
1683 } else {
1684 m->m_pkthdr.rcvif = ifp;
1685 m->m_pkthdr.len = m->m_len = total_len;
1686 }
1687
1688 ifp->if_ipackets++;
1689
1690 #if NBPFILTER > 0
1691 if (ifp->if_bpf)
1692 bpf_mtap(ifp->if_bpf, m);
1693 #endif
1694 /* pass it on. */
1695 (*ifp->if_input)(ifp, m);
1696 }
1697
1698 sc_if->sk_cdata.sk_rx_prod = i;
1699 }
1700
1701 void
1702 sk_txeof(struct sk_if_softc *sc_if)
1703 {
1704 struct sk_softc *sc = sc_if->sk_softc;
1705 struct sk_tx_desc *cur_tx = NULL;
1706 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1707 u_int32_t idx;
1708 struct sk_txmap_entry *entry;
1709
1710 DPRINTFN(3, ("sk_txeof\n"));
1711
1712 /*
1713 * Go through our tx ring and free mbufs for those
1714 * frames that have been sent.
1715 */
1716 idx = sc_if->sk_cdata.sk_tx_cons;
1717 while(idx != sc_if->sk_cdata.sk_tx_prod) {
1718 cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
1719 #ifdef SK_DEBUG
1720 if (skdebug >= 3)
1721 sk_dump_txdesc(cur_tx, idx);
1722 #endif
1723 if (cur_tx->sk_ctl & SK_TXCTL_OWN)
1724 break;
1725 if (cur_tx->sk_ctl & SK_TXCTL_LASTFRAG)
1726 ifp->if_opackets++;
1727 if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
1728 m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
1729 sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
1730
1731 entry = sc_if->sk_cdata.sk_tx_map[idx];
1732 bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
1733 entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1734
1735 bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
1736 SLIST_INSERT_HEAD(&sc_if->sk_txmap_listhead, entry,
1737 link);
1738 sc_if->sk_cdata.sk_tx_map[idx] = NULL;
1739 }
1740 sc_if->sk_cdata.sk_tx_cnt--;
1741 SK_INC(idx, SK_TX_RING_CNT);
1742 ifp->if_timer = 0;
1743 }
1744
1745 sc_if->sk_cdata.sk_tx_cons = idx;
1746
1747 if (cur_tx != NULL)
1748 ifp->if_flags &= ~IFF_OACTIVE;
1749 }
1750
1751 void
1752 sk_tick(void *xsc_if)
1753 {
1754 struct sk_if_softc *sc_if = xsc_if;
1755 struct mii_data *mii = &sc_if->sk_mii;
1756 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1757 int i;
1758
1759 DPRINTFN(3, ("sk_tick\n"));
1760
1761 if (!(ifp->if_flags & IFF_UP))
1762 return;
1763
1764 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
1765 sk_intr_bcom(sc_if);
1766 return;
1767 }
1768
1769 /*
1770 * According to SysKonnect, the correct way to verify that
1771 * the link has come back up is to poll bit 0 of the GPIO
1772 * register three times. This pin has the signal from the
1773 * link sync pin connected to it; if we read the same link
1774 * state 3 times in a row, we know the link is up.
1775 */
1776 for (i = 0; i < 3; i++) {
1777 if (SK_XM_READ_2(sc_if, XM_GPIO) & XM_GPIO_GP0_SET)
1778 break;
1779 }
1780
1781 if (i != 3) {
1782 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
1783 return;
1784 }
1785
1786 /* Turn the GP0 interrupt back on. */
1787 SK_XM_CLRBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
1788 SK_XM_READ_2(sc_if, XM_ISR);
1789 mii_tick(mii);
1790 mii_pollstat(mii);
1791 callout_stop(&sc_if->sk_tick_ch);
1792 }
1793
1794 void
1795 sk_intr_bcom(struct sk_if_softc *sc_if)
1796 {
1797 struct mii_data *mii = &sc_if->sk_mii;
1798 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1799 int status;
1800
1801
1802 DPRINTFN(3, ("sk_intr_bcom\n"));
1803
1804 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
1805
1806 /*
1807 * Read the PHY interrupt register to make sure
1808 * we clear any pending interrupts.
1809 */
1810 status = sk_xmac_miibus_readreg((struct device *)sc_if,
1811 SK_PHYADDR_BCOM, BRGPHY_MII_ISR);
1812
1813 if (!(ifp->if_flags & IFF_RUNNING)) {
1814 sk_init_xmac(sc_if);
1815 return;
1816 }
1817
1818 if (status & (BRGPHY_ISR_LNK_CHG|BRGPHY_ISR_AN_PR)) {
1819 int lstat;
1820 lstat = sk_xmac_miibus_readreg((struct device *)sc_if,
1821 SK_PHYADDR_BCOM, BRGPHY_MII_AUXSTS);
1822
1823 if (!(lstat & BRGPHY_AUXSTS_LINK) && sc_if->sk_link) {
1824 mii_mediachg(mii);
1825 /* Turn off the link LED. */
1826 SK_IF_WRITE_1(sc_if, 0,
1827 SK_LINKLED1_CTL, SK_LINKLED_OFF);
1828 sc_if->sk_link = 0;
1829 } else if (status & BRGPHY_ISR_LNK_CHG) {
1830 sk_xmac_miibus_writereg((struct device *)sc_if,
1831 SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFF00);
1832 mii_tick(mii);
1833 sc_if->sk_link = 1;
1834 /* Turn on the link LED. */
1835 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
1836 SK_LINKLED_ON|SK_LINKLED_LINKSYNC_OFF|
1837 SK_LINKLED_BLINK_OFF);
1838 mii_pollstat(mii);
1839 } else {
1840 mii_tick(mii);
1841 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick,sc_if);
1842 }
1843 }
1844
1845 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
1846 }
1847
1848 void
1849 sk_intr_xmac(struct sk_if_softc *sc_if)
1850 {
1851 u_int16_t status = SK_XM_READ_2(sc_if, XM_ISR);
1852
1853 DPRINTFN(3, ("sk_intr_xmac\n"));
1854
1855 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) {
1856 if (status & XM_ISR_GP0_SET) {
1857 SK_XM_SETBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
1858 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
1859 }
1860
1861 if (status & XM_ISR_AUTONEG_DONE) {
1862 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
1863 }
1864 }
1865
1866 if (status & XM_IMR_TX_UNDERRUN)
1867 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_TXFIFO);
1868
1869 if (status & XM_IMR_RX_OVERRUN)
1870 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_RXFIFO);
1871 }
1872
1873 void
1874 sk_intr_yukon(sc_if)
1875 struct sk_if_softc *sc_if;
1876 {
1877 int status;
1878
1879 status = SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
1880
1881 DPRINTFN(3, ("sk_intr_yukon status=%#x\n", status));
1882 }
1883
1884 int
1885 sk_intr(void *xsc)
1886 {
1887 struct sk_softc *sc = xsc;
1888 struct sk_if_softc *sc_if0 = sc->sk_if[SK_PORT_A];
1889 struct sk_if_softc *sc_if1 = sc->sk_if[SK_PORT_B];
1890 struct ifnet *ifp0 = NULL, *ifp1 = NULL;
1891 u_int32_t status;
1892 int claimed = 0;
1893
1894 if (sc_if0 != NULL)
1895 ifp0 = &sc_if0->sk_ethercom.ec_if;
1896 if (sc_if1 != NULL)
1897 ifp1 = &sc_if1->sk_ethercom.ec_if;
1898
1899 for (;;) {
1900 status = CSR_READ_4(sc, SK_ISSR);
1901 DPRINTFN(3, ("sk_intr: status=%#x\n", status));
1902
1903 if (!(status & sc->sk_intrmask))
1904 break;
1905
1906 claimed = 1;
1907
1908 /* Handle receive interrupts first. */
1909 if (status & SK_ISR_RX1_EOF) {
1910 sk_rxeof(sc_if0);
1911 CSR_WRITE_4(sc, SK_BMU_RX_CSR0,
1912 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
1913 }
1914 if (status & SK_ISR_RX2_EOF) {
1915 sk_rxeof(sc_if1);
1916 CSR_WRITE_4(sc, SK_BMU_RX_CSR1,
1917 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
1918 }
1919
1920 /* Then transmit interrupts. */
1921 if (status & SK_ISR_TX1_S_EOF) {
1922 sk_txeof(sc_if0);
1923 CSR_WRITE_4(sc, SK_BMU_TXS_CSR0,
1924 SK_TXBMU_CLR_IRQ_EOF);
1925 }
1926 if (status & SK_ISR_TX2_S_EOF) {
1927 sk_txeof(sc_if1);
1928 CSR_WRITE_4(sc, SK_BMU_TXS_CSR1,
1929 SK_TXBMU_CLR_IRQ_EOF);
1930 }
1931
1932 /* Then MAC interrupts. */
1933 if (status & SK_ISR_MAC1 && (ifp0->if_flags & IFF_RUNNING)) {
1934 if (sc->sk_type == SK_GENESIS)
1935 sk_intr_xmac(sc_if0);
1936 else
1937 sk_intr_yukon(sc_if0);
1938 }
1939
1940 if (status & SK_ISR_MAC2 && (ifp1->if_flags & IFF_RUNNING)) {
1941 if (sc->sk_type == SK_GENESIS)
1942 sk_intr_xmac(sc_if1);
1943 else
1944 sk_intr_yukon(sc_if1);
1945
1946 }
1947
1948 if (status & SK_ISR_EXTERNAL_REG) {
1949 if (ifp0 != NULL &&
1950 sc_if0->sk_phytype == SK_PHYTYPE_BCOM)
1951 sk_intr_bcom(sc_if0);
1952
1953 if (ifp1 != NULL &&
1954 sc_if1->sk_phytype == SK_PHYTYPE_BCOM)
1955 sk_intr_bcom(sc_if1);
1956 }
1957 }
1958
1959 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
1960
1961 if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
1962 sk_start(ifp0);
1963 if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
1964 sk_start(ifp1);
1965
1966 return (claimed);
1967 }
1968
1969 void
1970 sk_init_xmac(struct sk_if_softc *sc_if)
1971 {
1972 struct sk_softc *sc = sc_if->sk_softc;
1973 struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
1974 static const struct sk_bcom_hack bhack[] = {
1975 { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 }, { 0x17, 0x0013 },
1976 { 0x15, 0x0404 }, { 0x17, 0x8006 }, { 0x15, 0x0132 }, { 0x17, 0x8006 },
1977 { 0x15, 0x0232 }, { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
1978 { 0, 0 } };
1979
1980 DPRINTFN(1, ("sk_init_xmac\n"));
1981
1982 /* Unreset the XMAC. */
1983 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_UNRESET);
1984 DELAY(1000);
1985
1986 /* Reset the XMAC's internal state. */
1987 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
1988
1989 /* Save the XMAC II revision */
1990 sc_if->sk_xmac_rev = XM_XMAC_REV(SK_XM_READ_4(sc_if, XM_DEVID));
1991
1992 /*
1993 * Perform additional initialization for external PHYs,
1994 * namely for the 1000baseTX cards that use the XMAC's
1995 * GMII mode.
1996 */
1997 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
1998 int i = 0;
1999 u_int32_t val;
2000
2001 /* Take PHY out of reset. */
2002 val = sk_win_read_4(sc, SK_GPIO);
2003 if (sc_if->sk_port == SK_PORT_A)
2004 val |= SK_GPIO_DIR0|SK_GPIO_DAT0;
2005 else
2006 val |= SK_GPIO_DIR2|SK_GPIO_DAT2;
2007 sk_win_write_4(sc, SK_GPIO, val);
2008
2009 /* Enable GMII mode on the XMAC. */
2010 SK_XM_SETBIT_2(sc_if, XM_HWCFG, XM_HWCFG_GMIIMODE);
2011
2012 sk_xmac_miibus_writereg((struct device *)sc_if,
2013 SK_PHYADDR_BCOM, MII_BMCR, BMCR_RESET);
2014 DELAY(10000);
2015 sk_xmac_miibus_writereg((struct device *)sc_if,
2016 SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFFF0);
2017
2018 /*
2019 * Early versions of the BCM5400 apparently have
2020 * a bug that requires them to have their reserved
2021 * registers initialized to some magic values. I don't
2022 * know what the numbers do, I'm just the messenger.
2023 */
2024 if (sk_xmac_miibus_readreg((struct device *)sc_if,
2025 SK_PHYADDR_BCOM, 0x03) == 0x6041) {
2026 while(bhack[i].reg) {
2027 sk_xmac_miibus_writereg((struct device *)sc_if,
2028 SK_PHYADDR_BCOM, bhack[i].reg,
2029 bhack[i].val);
2030 i++;
2031 }
2032 }
2033 }
2034
2035 /* Set station address */
2036 SK_XM_WRITE_2(sc_if, XM_PAR0,
2037 *(u_int16_t *)(&sc_if->sk_enaddr[0]));
2038 SK_XM_WRITE_2(sc_if, XM_PAR1,
2039 *(u_int16_t *)(&sc_if->sk_enaddr[2]));
2040 SK_XM_WRITE_2(sc_if, XM_PAR2,
2041 *(u_int16_t *)(&sc_if->sk_enaddr[4]));
2042 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_STATION);
2043
2044 if (ifp->if_flags & IFF_PROMISC) {
2045 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
2046 } else {
2047 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
2048 }
2049
2050 if (ifp->if_flags & IFF_BROADCAST) {
2051 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2052 } else {
2053 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2054 }
2055
2056 /* We don't need the FCS appended to the packet. */
2057 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_STRIPFCS);
2058
2059 /* We want short frames padded to 60 bytes. */
2060 SK_XM_SETBIT_2(sc_if, XM_TXCMD, XM_TXCMD_AUTOPAD);
2061
2062 /*
2063 * Enable the reception of all error frames. This is is
2064 * a necessary evil due to the design of the XMAC. The
2065 * XMAC's receive FIFO is only 8K in size, however jumbo
2066 * frames can be up to 9000 bytes in length. When bad
2067 * frame filtering is enabled, the XMAC's RX FIFO operates
2068 * in 'store and forward' mode. For this to work, the
2069 * entire frame has to fit into the FIFO, but that means
2070 * that jumbo frames larger than 8192 bytes will be
2071 * truncated. Disabling all bad frame filtering causes
2072 * the RX FIFO to operate in streaming mode, in which
2073 * case the XMAC will start transfering frames out of the
2074 * RX FIFO as soon as the FIFO threshold is reached.
2075 */
2076 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_BADFRAMES|
2077 XM_MODE_RX_GIANTS|XM_MODE_RX_RUNTS|XM_MODE_RX_CRCERRS|
2078 XM_MODE_RX_INRANGELEN);
2079
2080 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2081 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2082 else
2083 SK_XM_CLRBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2084
2085 /*
2086 * Bump up the transmit threshold. This helps hold off transmit
2087 * underruns when we're blasting traffic from both ports at once.
2088 */
2089 SK_XM_WRITE_2(sc_if, XM_TX_REQTHRESH, SK_XM_TX_FIFOTHRESH);
2090
2091 /* Set multicast filter */
2092 sk_setmulti(sc_if);
2093
2094 /* Clear and enable interrupts */
2095 SK_XM_READ_2(sc_if, XM_ISR);
2096 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC)
2097 SK_XM_WRITE_2(sc_if, XM_IMR, XM_INTRS);
2098 else
2099 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2100
2101 /* Configure MAC arbiter */
2102 switch(sc_if->sk_xmac_rev) {
2103 case XM_XMAC_REV_B2:
2104 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_B2);
2105 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_B2);
2106 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_B2);
2107 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_B2);
2108 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_B2);
2109 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_B2);
2110 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_B2);
2111 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_B2);
2112 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2113 break;
2114 case XM_XMAC_REV_C1:
2115 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_C1);
2116 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_C1);
2117 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_C1);
2118 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_C1);
2119 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_C1);
2120 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_C1);
2121 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_C1);
2122 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_C1);
2123 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2124 break;
2125 default:
2126 break;
2127 }
2128 sk_win_write_2(sc, SK_MACARB_CTL,
2129 SK_MACARBCTL_UNRESET|SK_MACARBCTL_FASTOE_OFF);
2130
2131 sc_if->sk_link = 1;
2132 }
2133
2134 void sk_init_yukon(sc_if)
2135 struct sk_if_softc *sc_if;
2136 {
2137 u_int32_t /*mac, */phy;
2138 u_int16_t reg;
2139 int i;
2140
2141 DPRINTFN(1, ("sk_init_yukon: start: sk_csr=%#x\n",
2142 CSR_READ_4(sc_if->sk_softc, SK_CSR)));
2143
2144 /* GMAC and GPHY Reset */
2145 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
2146
2147 DPRINTFN(6, ("sk_init_yukon: 1\n"));
2148
2149 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2150 DELAY(1000);
2151 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_CLEAR);
2152 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2153 DELAY(1000);
2154
2155
2156 DPRINTFN(6, ("sk_init_yukon: 2\n"));
2157
2158 phy = SK_GPHY_INT_POL_HI | SK_GPHY_DIS_FC | SK_GPHY_DIS_SLEEP |
2159 SK_GPHY_ENA_XC | SK_GPHY_ANEG_ALL | SK_GPHY_ENA_PAUSE;
2160
2161 switch(sc_if->sk_softc->sk_pmd) {
2162 case IFM_1000_SX:
2163 case IFM_1000_LX:
2164 phy |= SK_GPHY_FIBER;
2165 break;
2166
2167 case IFM_1000_CX:
2168 case IFM_1000_T:
2169 phy |= SK_GPHY_COPPER;
2170 break;
2171 }
2172
2173 DPRINTFN(3, ("sk_init_yukon: phy=%#x\n", phy));
2174
2175 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_SET);
2176 DELAY(1000);
2177 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_CLEAR);
2178 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
2179 SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
2180
2181 DPRINTFN(3, ("sk_init_yukon: gmac_ctrl=%#x\n",
2182 SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
2183
2184 DPRINTFN(6, ("sk_init_yukon: 3\n"));
2185
2186 /* unused read of the interrupt source register */
2187 DPRINTFN(6, ("sk_init_yukon: 4\n"));
2188 SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2189
2190 DPRINTFN(6, ("sk_init_yukon: 4a\n"));
2191 reg = SK_YU_READ_2(sc_if, YUKON_PAR);
2192 DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
2193
2194 /* MIB Counter Clear Mode set */
2195 reg |= YU_PAR_MIB_CLR;
2196 DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
2197 DPRINTFN(6, ("sk_init_yukon: 4b\n"));
2198 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2199
2200 /* MIB Counter Clear Mode clear */
2201 DPRINTFN(6, ("sk_init_yukon: 5\n"));
2202 reg &= ~YU_PAR_MIB_CLR;
2203 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2204
2205 /* receive control reg */
2206 DPRINTFN(6, ("sk_init_yukon: 7\n"));
2207 SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_UFLEN | YU_RCR_MUFLEN |
2208 YU_RCR_CRCR);
2209
2210 /* transmit parameter register */
2211 DPRINTFN(6, ("sk_init_yukon: 8\n"));
2212 SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
2213 YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) );
2214
2215 /* serial mode register */
2216 DPRINTFN(6, ("sk_init_yukon: 9\n"));
2217 SK_YU_WRITE_2(sc_if, YUKON_SMR, YU_SMR_DATA_BLIND(0x1c) |
2218 YU_SMR_MFL_VLAN | YU_SMR_IPG_DATA(0x1e));
2219
2220 DPRINTFN(6, ("sk_init_yukon: 10\n"));
2221 /* Setup Yukon's address */
2222 for (i = 0; i < 3; i++) {
2223 /* Write Source Address 1 (unicast filter) */
2224 SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
2225 sc_if->sk_enaddr[i * 2] |
2226 sc_if->sk_enaddr[i * 2 + 1] << 8);
2227 }
2228
2229 for (i = 0; i < 3; i++) {
2230 reg = sk_win_read_2(sc_if->sk_softc,
2231 SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
2232 SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
2233 }
2234
2235 /* clear all Multicast filter hash registers */
2236 DPRINTFN(6, ("sk_init_yukon: 11\n"));
2237 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
2238 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
2239 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
2240 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
2241
2242 /* enable interrupt mask for counter overflows */
2243 DPRINTFN(6, ("sk_init_yukon: 12\n"));
2244 SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
2245 SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
2246 SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
2247
2248 /* Configure RX MAC FIFO */
2249 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
2250 SK_IF_WRITE_4(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON);
2251
2252 /* Configure TX MAC FIFO */
2253 SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
2254 SK_IF_WRITE_4(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
2255
2256 DPRINTFN(6, ("sk_init_yukon: end\n"));
2257 }
2258
2259 /*
2260 * Note that to properly initialize any part of the GEnesis chip,
2261 * you first have to take it out of reset mode.
2262 */
2263 int
2264 sk_init(struct ifnet *ifp)
2265 {
2266 struct sk_if_softc *sc_if = ifp->if_softc;
2267 struct sk_softc *sc = sc_if->sk_softc;
2268 struct mii_data *mii = &sc_if->sk_mii;
2269 int s;
2270
2271 DPRINTFN(1, ("sk_init\n"));
2272
2273 s = splnet();
2274
2275 /* Cancel pending I/O and free all RX/TX buffers. */
2276 sk_stop(ifp,0);
2277
2278 if (sc->sk_type == SK_GENESIS) {
2279 /* Configure LINK_SYNC LED */
2280 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_ON);
2281 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2282 SK_LINKLED_LINKSYNC_ON);
2283
2284 /* Configure RX LED */
2285 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL,
2286 SK_RXLEDCTL_COUNTER_START);
2287
2288 /* Configure TX LED */
2289 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL,
2290 SK_TXLEDCTL_COUNTER_START);
2291 }
2292
2293 /* Configure I2C registers */
2294
2295 /* Configure XMAC(s) */
2296 switch (sc->sk_type) {
2297 case SK_GENESIS:
2298 sk_init_xmac(sc_if);
2299 break;
2300 case SK_YUKON:
2301 sk_init_yukon(sc_if);
2302 break;
2303 }
2304 mii_mediachg(mii);
2305
2306 if (sc->sk_type == SK_GENESIS) {
2307 /* Configure MAC FIFOs */
2308 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_UNRESET);
2309 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_END, SK_FIFO_END);
2310 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_ON);
2311
2312 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_UNRESET);
2313 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_END, SK_FIFO_END);
2314 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_ON);
2315 }
2316
2317 /* Configure transmit arbiter(s) */
2318 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL,
2319 SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
2320
2321 /* Configure RAMbuffers */
2322 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
2323 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
2324 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
2325 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
2326 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
2327 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
2328
2329 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_UNRESET);
2330 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_STORENFWD_ON);
2331 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_START, sc_if->sk_tx_ramstart);
2332 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_WR_PTR, sc_if->sk_tx_ramstart);
2333 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_RD_PTR, sc_if->sk_tx_ramstart);
2334 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_END, sc_if->sk_tx_ramend);
2335 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_ON);
2336
2337 /* Configure BMUs */
2338 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_ONLINE);
2339 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO,
2340 SK_RX_RING_ADDR(sc_if, 0));
2341 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 0);
2342
2343 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_ONLINE);
2344 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_LO,
2345 SK_TX_RING_ADDR(sc_if, 0));
2346 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_HI, 0);
2347
2348 /* Init descriptors */
2349 if (sk_init_rx_ring(sc_if) == ENOBUFS) {
2350 printf("%s: initialization failed: no "
2351 "memory for rx buffers\n", sc_if->sk_dev.dv_xname);
2352 sk_stop(ifp,0);
2353 splx(s);
2354 return(ENOBUFS);
2355 }
2356
2357 if (sk_init_tx_ring(sc_if) == ENOBUFS) {
2358 printf("%s: initialization failed: no "
2359 "memory for tx buffers\n", sc_if->sk_dev.dv_xname);
2360 sk_stop(ifp,0);
2361 splx(s);
2362 return(ENOBUFS);
2363 }
2364
2365 /* Configure interrupt handling */
2366 CSR_READ_4(sc, SK_ISSR);
2367 if (sc_if->sk_port == SK_PORT_A)
2368 sc->sk_intrmask |= SK_INTRS1;
2369 else
2370 sc->sk_intrmask |= SK_INTRS2;
2371
2372 sc->sk_intrmask |= SK_ISR_EXTERNAL_REG;
2373
2374 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2375
2376 /* Start BMUs. */
2377 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_START);
2378
2379 if (sc->sk_type == SK_GENESIS) {
2380 /* Enable XMACs TX and RX state machines */
2381 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_IGNPAUSE);
2382 SK_XM_SETBIT_2(sc_if, XM_MMUCMD,
2383 XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2384 }
2385
2386 if (sc->sk_type == SK_YUKON) {
2387 u_int16_t reg = SK_YU_READ_2(sc_if, YUKON_GPCR);
2388 reg |= YU_GPCR_TXEN | YU_GPCR_RXEN;
2389 reg &= ~(YU_GPCR_SPEED_EN | YU_GPCR_DPLX_EN);
2390 SK_YU_WRITE_2(sc_if, YUKON_GPCR, reg);
2391 }
2392
2393
2394 ifp->if_flags |= IFF_RUNNING;
2395 ifp->if_flags &= ~IFF_OACTIVE;
2396
2397 splx(s);
2398 return(0);
2399 }
2400
2401 void
2402 sk_stop(struct ifnet *ifp, int disable)
2403 {
2404 struct sk_if_softc *sc_if = ifp->if_softc;
2405 struct sk_softc *sc = sc_if->sk_softc;
2406 int i;
2407
2408 DPRINTFN(1, ("sk_stop\n"));
2409
2410 callout_stop(&sc_if->sk_tick_ch);
2411
2412 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2413 u_int32_t val;
2414
2415 /* Put PHY back into reset. */
2416 val = sk_win_read_4(sc, SK_GPIO);
2417 if (sc_if->sk_port == SK_PORT_A) {
2418 val |= SK_GPIO_DIR0;
2419 val &= ~SK_GPIO_DAT0;
2420 } else {
2421 val |= SK_GPIO_DIR2;
2422 val &= ~SK_GPIO_DAT2;
2423 }
2424 sk_win_write_4(sc, SK_GPIO, val);
2425 }
2426
2427 /* Turn off various components of this interface. */
2428 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2429 switch (sc->sk_type) {
2430 case SK_GENESIS:
2431 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL,
2432 SK_TXMACCTL_XMAC_RESET);
2433 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_RESET);
2434 break;
2435 case SK_YUKON:
2436 SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
2437 SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
2438 break;
2439 }
2440 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
2441 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2442 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_OFFLINE);
2443 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2444 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
2445 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2446 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2447 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
2448 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
2449
2450 /* Disable interrupts */
2451 if (sc_if->sk_port == SK_PORT_A)
2452 sc->sk_intrmask &= ~SK_INTRS1;
2453 else
2454 sc->sk_intrmask &= ~SK_INTRS2;
2455 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2456
2457 SK_XM_READ_2(sc_if, XM_ISR);
2458 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2459
2460 /* Free RX and TX mbufs still in the queues. */
2461 if ( disable ) {
2462 for (i = 0; i < SK_RX_RING_CNT; i++) {
2463 if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
2464 m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
2465 sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
2466 }
2467 }
2468
2469 for (i = 0; i < SK_TX_RING_CNT; i++) {
2470 if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
2471 m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
2472 sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
2473 }
2474 }
2475 }
2476
2477 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2478 }
2479
2480 CFATTACH_DECL(skc,sizeof(struct sk_softc), skc_probe, skc_attach, NULL, NULL);
2481
2482 /*
2483 struct cfdriver skc_cd = {
2484 0, "skc", DV_DULL
2485 };
2486 */
2487
2488 CFATTACH_DECL(sk,sizeof(struct sk_if_softc), sk_probe, sk_attach, NULL, NULL);
2489
2490 /*
2491 struct cfdriver sk_cd = {
2492 0, "sk", DV_IFNET
2493 };
2494 */
2495
2496 #ifdef SK_DEBUG
2497 void
2498 sk_dump_txdesc(struct sk_tx_desc *desc, int idx)
2499 {
2500 #define DESC_PRINT(X) \
2501 if (desc->X) \
2502 printf("txdesc[%d]." #X "=%#x\n", \
2503 idx, desc->X);
2504
2505 DESC_PRINT(sk_ctl);
2506 DESC_PRINT(sk_next);
2507 DESC_PRINT(sk_data_lo);
2508 DESC_PRINT(sk_data_hi);
2509 DESC_PRINT(sk_xmac_txstat);
2510 DESC_PRINT(sk_rsvd0);
2511 DESC_PRINT(sk_csum_startval);
2512 DESC_PRINT(sk_csum_startpos);
2513 DESC_PRINT(sk_csum_writepos);
2514 DESC_PRINT(sk_rsvd1);
2515 #undef PRINT
2516 }
2517
2518 void
2519 sk_dump_bytes(const char *data, int len)
2520 {
2521 int c, i, j;
2522
2523 for (i = 0; i < len; i += 16) {
2524 printf("%08x ", i);
2525 c = len - i;
2526 if (c > 16) c = 16;
2527
2528 for (j = 0; j < c; j++) {
2529 printf("%02x ", data[i + j] & 0xff);
2530 if ((j & 0xf) == 7 && j > 0)
2531 printf(" ");
2532 }
2533
2534 for (; j < 16; j++)
2535 printf(" ");
2536 printf(" ");
2537
2538 for (j = 0; j < c; j++) {
2539 int ch = data[i + j] & 0xff;
2540 printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
2541 }
2542
2543 printf("\n");
2544
2545 if (c < 16)
2546 break;
2547 }
2548 }
2549
2550 void
2551 sk_dump_mbuf(struct mbuf *m)
2552 {
2553 int count = m->m_pkthdr.len;
2554
2555 printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
2556
2557 while (count > 0 && m) {
2558 printf("m=%p, m->m_data=%p, m->m_len=%d\n",
2559 m, m->m_data, m->m_len);
2560 sk_dump_bytes(mtod(m, char *), m->m_len);
2561
2562 count -= m->m_len;
2563 m = m->m_next;
2564 }
2565 }
2566 #endif
2567