if_cas.c revision 1.4 1 /* $NetBSD: if_cas.c,v 1.4 2010/01/19 22:07:00 pooka Exp $ */
2 /* $OpenBSD: if_cas.c,v 1.29 2009/11/29 16:19:38 kettenis Exp $ */
3
4 /*
5 *
6 * Copyright (C) 2007 Mark Kettenis.
7 * Copyright (C) 2001 Eduardo Horvath.
8 * All rights reserved.
9 *
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34 /*
35 * Driver for Sun Cassini ethernet controllers.
36 *
37 * There are basically two variants of this chip: Cassini and
38 * Cassini+. We can distinguish between the two by revision: 0x10 and
39 * up are Cassini+. The most important difference is that Cassini+
40 * has a second RX descriptor ring. Cassini+ will not work without
41 * configuring that second ring. However, since we don't use it we
42 * don't actually fill the descriptors, and only hand off the first
43 * four to the chip.
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: if_cas.c,v 1.4 2010/01/19 22:07:00 pooka Exp $");
48
49 #include "opt_inet.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/callout.h>
54 #include <sys/mbuf.h>
55 #include <sys/syslog.h>
56 #include <sys/malloc.h>
57 #include <sys/kernel.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
60 #include <sys/errno.h>
61 #include <sys/device.h>
62
63 #include <machine/endian.h>
64
65 #include <uvm/uvm_extern.h>
66
67 #include <net/if.h>
68 #include <net/if_dl.h>
69 #include <net/if_media.h>
70 #include <net/if_ether.h>
71
72 #ifdef INET
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/tcp.h>
78 #include <netinet/udp.h>
79 #endif
80
81 #include <net/bpf.h>
82
83 #include <sys/bus.h>
84 #include <sys/intr.h>
85
86 #include <dev/mii/mii.h>
87 #include <dev/mii/miivar.h>
88 #include <dev/mii/mii_bitbang.h>
89
90 #include <dev/pci/pcivar.h>
91 #include <dev/pci/pcireg.h>
92 #include <dev/pci/pcidevs.h>
93
94 #include <dev/pci/if_casreg.h>
95 #include <dev/pci/if_casvar.h>
96
97 /* XXX Should use Properties when that's fleshed out. */
98 #ifdef macppc
99 #include <dev/ofw/openfirm.h>
100 #endif /* macppc */
101 #ifdef __sparc__
102 #include <machine/promlib.h>
103 #endif
104
105 #ifndef CAS_USE_LOCAL_MAC_ADDRESS
106 #if defined (macppc) || defined (__sparc__)
107 #define CAS_USE_LOCAL_MAC_ADDRESS 0 /* use system-wide address */
108 #else
109 #define CAS_USE_LOCAL_MAC_ADDRESS 1
110 #endif
111 #endif
112
113 #define TRIES 10000
114
115 static bool cas_estintr(struct cas_softc *sc, int);
116 bool cas_shutdown(device_t, int);
117 static bool cas_suspend(device_t, pmf_qual_t);
118 static bool cas_resume(device_t, pmf_qual_t);
119 static int cas_detach(device_t, int);
120 static void cas_partial_detach(struct cas_softc *, enum cas_attach_stage);
121
122 int cas_match(device_t, cfdata_t, void *);
123 void cas_attach(device_t, device_t, void *);
124
125
126 CFATTACH_DECL3_NEW(cas, sizeof(struct cas_softc),
127 cas_match, cas_attach, cas_detach, NULL, NULL, NULL,
128 DVF_DETACH_SHUTDOWN);
129
130 #if CAS_USE_LOCAL_MAC_ADDRESS
131 int cas_pci_enaddr(struct cas_softc *, struct pci_attach_args *, uint8_t *);
132 #endif
133
134 void cas_config(struct cas_softc *, const uint8_t *);
135 void cas_start(struct ifnet *);
136 void cas_stop(struct ifnet *, int);
137 int cas_ioctl(struct ifnet *, u_long, void *);
138 void cas_tick(void *);
139 void cas_watchdog(struct ifnet *);
140 int cas_init(struct ifnet *);
141 void cas_init_regs(struct cas_softc *);
142 int cas_ringsize(int);
143 int cas_cringsize(int);
144 int cas_meminit(struct cas_softc *);
145 void cas_mifinit(struct cas_softc *);
146 int cas_bitwait(struct cas_softc *, bus_space_handle_t, int,
147 u_int32_t, u_int32_t);
148 void cas_reset(struct cas_softc *);
149 int cas_reset_rx(struct cas_softc *);
150 int cas_reset_tx(struct cas_softc *);
151 int cas_disable_rx(struct cas_softc *);
152 int cas_disable_tx(struct cas_softc *);
153 void cas_rxdrain(struct cas_softc *);
154 int cas_add_rxbuf(struct cas_softc *, int idx);
155 void cas_iff(struct cas_softc *);
156 int cas_encap(struct cas_softc *, struct mbuf *, u_int32_t *);
157
158 /* MII methods & callbacks */
159 int cas_mii_readreg(device_t, int, int);
160 void cas_mii_writereg(device_t, int, int, int);
161 void cas_mii_statchg(device_t);
162 int cas_pcs_readreg(device_t, int, int);
163 void cas_pcs_writereg(device_t, int, int, int);
164
165 int cas_mediachange(struct ifnet *);
166 void cas_mediastatus(struct ifnet *, struct ifmediareq *);
167
168 int cas_eint(struct cas_softc *, u_int);
169 int cas_rint(struct cas_softc *);
170 int cas_tint(struct cas_softc *, u_int32_t);
171 int cas_pint(struct cas_softc *);
172 int cas_intr(void *);
173
174 #ifdef CAS_DEBUG
175 #define DPRINTF(sc, x) if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
176 printf x
177 #else
178 #define DPRINTF(sc, x) /* nothing */
179 #endif
180
181 int
182 cas_match(device_t parent, cfdata_t cf, void *aux)
183 {
184 struct pci_attach_args *pa = aux;
185
186 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
187 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_CASSINI))
188 return 1;
189
190 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
191 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SATURN))
192 return 1;
193
194 return 0;
195 }
196
197 #if CAS_USE_LOCAL_MAC_ADDRESS
198 #define PROMHDR_PTR_DATA 0x18
199 #define PROMDATA_PTR_VPD 0x08
200 #define PROMDATA_DATA2 0x0a
201
202 static const u_int8_t cas_promhdr[] = { 0x55, 0xaa };
203 static const u_int8_t cas_promdat[] = {
204 'P', 'C', 'I', 'R',
205 PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
206 PCI_PRODUCT_SUN_CASSINI & 0xff, PCI_PRODUCT_SUN_CASSINI >> 8
207 };
208
209 static const u_int8_t cas_promdat2[] = {
210 0x18, 0x00, /* structure length */
211 0x00, /* structure revision */
212 0x00, /* interface revision */
213 PCI_SUBCLASS_NETWORK_ETHERNET, /* subclass code */
214 PCI_CLASS_NETWORK /* class code */
215 };
216
217 int
218 cas_pci_enaddr(struct cas_softc *sc, struct pci_attach_args *pa,
219 uint8_t *enaddr)
220 {
221 struct pci_vpd_largeres *res;
222 struct pci_vpd *vpd;
223 bus_space_handle_t romh;
224 bus_space_tag_t romt;
225 bus_size_t romsize = 0;
226 u_int8_t buf[32], *desc;
227 pcireg_t address;
228 int dataoff, vpdoff, len;
229 int rv = -1;
230
231 if (pci_mapreg_map(pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_MEM, 0,
232 &romt, &romh, NULL, &romsize))
233 return (-1);
234
235 address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
236 address |= PCI_MAPREG_ROM_ENABLE;
237 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START, address);
238
239 bus_space_read_region_1(romt, romh, 0, buf, sizeof(buf));
240 if (bcmp(buf, cas_promhdr, sizeof(cas_promhdr)))
241 goto fail;
242
243 dataoff = buf[PROMHDR_PTR_DATA] | (buf[PROMHDR_PTR_DATA + 1] << 8);
244 if (dataoff < 0x1c)
245 goto fail;
246
247 bus_space_read_region_1(romt, romh, dataoff, buf, sizeof(buf));
248 if (bcmp(buf, cas_promdat, sizeof(cas_promdat)) ||
249 bcmp(buf + PROMDATA_DATA2, cas_promdat2, sizeof(cas_promdat2)))
250 goto fail;
251
252 vpdoff = buf[PROMDATA_PTR_VPD] | (buf[PROMDATA_PTR_VPD + 1] << 8);
253 if (vpdoff < 0x1c)
254 goto fail;
255
256 next:
257 bus_space_read_region_1(romt, romh, vpdoff, buf, sizeof(buf));
258 if (!PCI_VPDRES_ISLARGE(buf[0]))
259 goto fail;
260
261 res = (struct pci_vpd_largeres *)buf;
262 vpdoff += sizeof(*res);
263
264 len = ((res->vpdres_len_msb << 8) + res->vpdres_len_lsb);
265 switch(PCI_VPDRES_LARGE_NAME(res->vpdres_byte0)) {
266 case PCI_VPDRES_TYPE_IDENTIFIER_STRING:
267 /* Skip identifier string. */
268 vpdoff += len;
269 goto next;
270
271 case PCI_VPDRES_TYPE_VPD:
272 while (len > 0) {
273 bus_space_read_region_1(romt, romh, vpdoff,
274 buf, sizeof(buf));
275
276 vpd = (struct pci_vpd *)buf;
277 vpdoff += sizeof(*vpd) + vpd->vpd_len;
278 len -= sizeof(*vpd) + vpd->vpd_len;
279
280 /*
281 * We're looking for an "Enhanced" VPD...
282 */
283 if (vpd->vpd_key0 != 'Z')
284 continue;
285
286 desc = buf + sizeof(*vpd);
287
288 /*
289 * ...which is an instance property...
290 */
291 if (desc[0] != 'I')
292 continue;
293 desc += 3;
294
295 /*
296 * ...that's a byte array with the proper
297 * length for a MAC address...
298 */
299 if (desc[0] != 'B' || desc[1] != ETHER_ADDR_LEN)
300 continue;
301 desc += 2;
302
303 /*
304 * ...named "local-mac-address".
305 */
306 if (strcmp(desc, "local-mac-address") != 0)
307 continue;
308 desc += strlen("local-mac-address") + 1;
309
310 memcpy(enaddr, enp, ETHER_ADDR_LEN);
311 rv = 0;
312 }
313 break;
314
315 default:
316 goto fail;
317 }
318
319 fail:
320 if (romsize != 0)
321 bus_space_unmap(romt, romh, romsize);
322
323 address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
324 address &= ~PCI_MAPREG_ROM_ENABLE;
325 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, address);
326
327 return (rv);
328 }
329 #endif /* CAS_USE_LOCAL_MAC_ADDRESS */
330
331 void
332 cas_attach(device_t parent, device_t self, void *aux)
333 {
334 struct pci_attach_args *pa = aux;
335 struct cas_softc *sc = device_private(self);
336 char devinfo[256];
337 uint8_t enaddr[ETHER_ADDR_LEN];
338
339 sc->sc_dev = self;
340 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
341 sc->sc_rev = PCI_REVISION(pa->pa_class);
342 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, sc->sc_rev);
343 sc->sc_dmatag = pa->pa_dmat;
344
345 #define PCI_CAS_BASEADDR 0x10
346 if (pci_mapreg_map(pa, PCI_CAS_BASEADDR, PCI_MAPREG_TYPE_MEM, 0,
347 &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_size) != 0) {
348 aprint_error_dev(sc->sc_dev,
349 "unable to map device registers\n");
350 return;
351 }
352
353 #if CAS_USE_LOCAL_MAC_ADDRESS
354 if (cas_pci_enaddr(sc, pa, enaddr) != 0)
355 aprint_error_dev(sc->sc_dev, "no Ethernet address found\n");
356 #endif
357 #ifdef __sparc64__
358 prom_getether(PCITAG_NODE(pa->pa_tag), enaddr);
359 #else
360 #ifdef macppc
361 {
362 int node;
363
364 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
365 if (node == 0) {
366 aprint_error_dev(sc->sc_dev, "unable to locate OpenFirmware node\n");
367 return;
368 }
369
370 OF_getprop(node, "local-mac-address", enaddr, sizeof(enaddr));
371 }
372 #endif /* macppc */
373 #endif /* __sparc__ */
374
375 sc->sc_burst = 16; /* XXX */
376
377 sc->sc_att_stage = CAS_ATT_BACKEND_0;
378
379 if (pci_intr_map(pa, &sc->sc_handle) != 0) {
380 aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
381 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
382 return;
383 }
384 sc->sc_pc = pa->pa_pc;
385 if (!cas_estintr(sc, CAS_INTR_PCI)) {
386 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
387 aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
388 return;
389 }
390
391 sc->sc_att_stage = CAS_ATT_BACKEND_1;
392
393 /*
394 * call the main configure
395 */
396 cas_config(sc, enaddr);
397
398 if (pmf_device_register1(sc->sc_dev,
399 cas_suspend, cas_resume, cas_shutdown))
400 pmf_class_network_register(sc->sc_dev, &sc->sc_ethercom.ec_if);
401 else
402 aprint_error_dev(sc->sc_dev,
403 "could not establish power handlers\n");
404
405 sc->sc_att_stage = CAS_ATT_FINISHED;
406 /*FALLTHROUGH*/
407 }
408
409 /*
410 * cas_config:
411 *
412 * Attach a Cassini interface to the system.
413 */
414 void
415 cas_config(struct cas_softc *sc, const uint8_t *enaddr)
416 {
417 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
418 struct mii_data *mii = &sc->sc_mii;
419 struct mii_softc *child;
420 int i, error;
421
422 /* Make sure the chip is stopped. */
423 ifp->if_softc = sc;
424 cas_reset(sc);
425
426 /*
427 * Allocate the control data structures, and create and load the
428 * DMA map for it.
429 */
430 if ((error = bus_dmamem_alloc(sc->sc_dmatag,
431 sizeof(struct cas_control_data), CAS_PAGE_SIZE, 0, &sc->sc_cdseg,
432 1, &sc->sc_cdnseg, 0)) != 0) {
433 aprint_error_dev(sc->sc_dev,
434 "unable to allocate control data, error = %d\n",
435 error);
436 cas_partial_detach(sc, CAS_ATT_0);
437 }
438
439 /* XXX should map this in with correct endianness */
440 if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg,
441 sizeof(struct cas_control_data), (void **)&sc->sc_control_data,
442 BUS_DMA_COHERENT)) != 0) {
443 aprint_error_dev(sc->sc_dev,
444 "unable to map control data, error = %d\n", error);
445 cas_partial_detach(sc, CAS_ATT_1);
446 }
447
448 if ((error = bus_dmamap_create(sc->sc_dmatag,
449 sizeof(struct cas_control_data), 1,
450 sizeof(struct cas_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
451 aprint_error_dev(sc->sc_dev,
452 "unable to create control data DMA map, error = %d\n", error);
453 cas_partial_detach(sc, CAS_ATT_2);
454 }
455
456 if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
457 sc->sc_control_data, sizeof(struct cas_control_data), NULL,
458 0)) != 0) {
459 aprint_error_dev(sc->sc_dev,
460 "unable to load control data DMA map, error = %d\n",
461 error);
462 cas_partial_detach(sc, CAS_ATT_3);
463 }
464
465 memset(sc->sc_control_data, 0, sizeof(struct cas_control_data));
466
467 /*
468 * Create the receive buffer DMA maps.
469 */
470 for (i = 0; i < CAS_NRXDESC; i++) {
471 bus_dma_segment_t seg;
472 char *kva;
473 int rseg;
474
475 if ((error = bus_dmamem_alloc(sc->sc_dmatag, CAS_PAGE_SIZE,
476 CAS_PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
477 aprint_error_dev(sc->sc_dev,
478 "unable to alloc rx DMA mem %d, error = %d\n",
479 i, error);
480 cas_partial_detach(sc, CAS_ATT_5);
481 }
482 sc->sc_rxsoft[i].rxs_dmaseg = seg;
483
484 if ((error = bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
485 CAS_PAGE_SIZE, (void **)&kva, BUS_DMA_NOWAIT)) != 0) {
486 aprint_error_dev(sc->sc_dev,
487 "unable to alloc rx DMA mem %d, error = %d\n",
488 i, error);
489 cas_partial_detach(sc, CAS_ATT_5);
490 }
491 sc->sc_rxsoft[i].rxs_kva = kva;
492
493 if ((error = bus_dmamap_create(sc->sc_dmatag, CAS_PAGE_SIZE, 1,
494 CAS_PAGE_SIZE, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
495 aprint_error_dev(sc->sc_dev,
496 "unable to create rx DMA map %d, error = %d\n",
497 i, error);
498 cas_partial_detach(sc, CAS_ATT_5);
499 }
500
501 if ((error = bus_dmamap_load(sc->sc_dmatag,
502 sc->sc_rxsoft[i].rxs_dmamap, kva, CAS_PAGE_SIZE, NULL,
503 BUS_DMA_NOWAIT)) != 0) {
504 aprint_error_dev(sc->sc_dev,
505 "unable to load rx DMA map %d, error = %d\n",
506 i, error);
507 cas_partial_detach(sc, CAS_ATT_5);
508 }
509 }
510
511 /*
512 * Create the transmit buffer DMA maps.
513 */
514 for (i = 0; i < CAS_NTXDESC; i++) {
515 if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES,
516 CAS_NTXSEGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
517 &sc->sc_txd[i].sd_map)) != 0) {
518 aprint_error_dev(sc->sc_dev,
519 "unable to create tx DMA map %d, error = %d\n",
520 i, error);
521 cas_partial_detach(sc, CAS_ATT_6);
522 }
523 sc->sc_txd[i].sd_mbuf = NULL;
524 }
525
526 /*
527 * From this point forward, the attachment cannot fail. A failure
528 * before this point releases all resources that may have been
529 * allocated.
530 */
531
532 /* Announce ourselves. */
533 aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
534 ether_sprintf(enaddr));
535
536 /* Get RX FIFO size */
537 sc->sc_rxfifosize = 16 * 1024;
538
539 /* Initialize ifnet structure. */
540 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
541 ifp->if_softc = sc;
542 ifp->if_flags =
543 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
544 ifp->if_start = cas_start;
545 ifp->if_ioctl = cas_ioctl;
546 ifp->if_watchdog = cas_watchdog;
547 ifp->if_stop = cas_stop;
548 ifp->if_init = cas_init;
549 IFQ_SET_MAXLEN(&ifp->if_snd, CAS_NTXDESC - 1);
550 IFQ_SET_READY(&ifp->if_snd);
551
552 /* Initialize ifmedia structures and MII info */
553 mii->mii_ifp = ifp;
554 mii->mii_readreg = cas_mii_readreg;
555 mii->mii_writereg = cas_mii_writereg;
556 mii->mii_statchg = cas_mii_statchg;
557
558 ifmedia_init(&mii->mii_media, 0, cas_mediachange, cas_mediastatus);
559 sc->sc_ethercom.ec_mii = mii;
560
561 bus_space_write_4(sc->sc_memt, sc->sc_memh, CAS_MII_DATAPATH_MODE, 0);
562
563 cas_mifinit(sc);
564
565 if (sc->sc_mif_config & CAS_MIF_CONFIG_MDI1) {
566 sc->sc_mif_config |= CAS_MIF_CONFIG_PHY_SEL;
567 bus_space_write_4(sc->sc_memt, sc->sc_memh,
568 CAS_MIF_CONFIG, sc->sc_mif_config);
569 }
570
571 mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
572 MII_OFFSET_ANY, 0);
573
574 child = LIST_FIRST(&mii->mii_phys);
575 if (child == NULL &&
576 sc->sc_mif_config & (CAS_MIF_CONFIG_MDI0|CAS_MIF_CONFIG_MDI1)) {
577 /*
578 * Try the external PCS SERDES if we didn't find any
579 * MII devices.
580 */
581 bus_space_write_4(sc->sc_memt, sc->sc_memh,
582 CAS_MII_DATAPATH_MODE, CAS_MII_DATAPATH_SERDES);
583
584 bus_space_write_4(sc->sc_memt, sc->sc_memh,
585 CAS_MII_CONFIG, CAS_MII_CONFIG_ENABLE);
586
587 mii->mii_readreg = cas_pcs_readreg;
588 mii->mii_writereg = cas_pcs_writereg;
589
590 mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
591 MII_OFFSET_ANY, MIIF_NOISOLATE);
592 }
593
594 child = LIST_FIRST(&mii->mii_phys);
595 if (child == NULL) {
596 /* No PHY attached */
597 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
598 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
599 } else {
600 /*
601 * Walk along the list of attached MII devices and
602 * establish an `MII instance' to `phy number'
603 * mapping. We'll use this mapping in media change
604 * requests to determine which phy to use to program
605 * the MIF configuration register.
606 */
607 for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
608 /*
609 * Note: we support just two PHYs: the built-in
610 * internal device and an external on the MII
611 * connector.
612 */
613 if (child->mii_phy > 1 || child->mii_inst > 1) {
614 aprint_error_dev(sc->sc_dev,
615 "cannot accommodate MII device %s"
616 " at phy %d, instance %d\n",
617 device_xname(child->mii_dev),
618 child->mii_phy, child->mii_inst);
619 continue;
620 }
621
622 sc->sc_phys[child->mii_inst] = child->mii_phy;
623 }
624
625 /*
626 * XXX - we can really do the following ONLY if the
627 * phy indeed has the auto negotiation capability!!
628 */
629 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
630 }
631
632 /* claim 802.1q capability */
633 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
634
635 /* Attach the interface. */
636 if_attach(ifp);
637 ether_ifattach(ifp, enaddr);
638
639 #if NRND > 0
640 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
641 RND_TYPE_NET, 0);
642 #endif
643
644 evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
645 NULL, device_xname(sc->sc_dev), "interrupts");
646
647 callout_init(&sc->sc_tick_ch, 0);
648
649 return;
650 }
651
652 int
653 cas_detach(device_t self, int flags)
654 {
655 int i;
656 struct cas_softc *sc = device_private(self);
657 bus_space_tag_t t = sc->sc_memt;
658 bus_space_handle_t h = sc->sc_memh;
659 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
660
661 /*
662 * Free any resources we've allocated during the failed attach
663 * attempt. Do this in reverse order and fall through.
664 */
665 switch (sc->sc_att_stage) {
666 case CAS_ATT_FINISHED:
667 bus_space_write_4(t, h, CAS_INTMASK, ~(uint32_t)0);
668 pmf_device_deregister(self);
669 cas_stop(&sc->sc_ethercom.ec_if, 1);
670 evcnt_detach(&sc->sc_ev_intr);
671
672 #if NRND > 0
673 rnd_detach_source(&sc->rnd_source);
674 #endif
675
676 ether_ifdetach(ifp);
677 if_detach(ifp);
678 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
679
680 callout_destroy(&sc->sc_tick_ch);
681
682 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
683
684 /*FALLTHROUGH*/
685 case CAS_ATT_MII:
686 case CAS_ATT_7:
687 case CAS_ATT_6:
688 for (i = 0; i < CAS_NTXDESC; i++) {
689 if (sc->sc_txd[i].sd_map != NULL)
690 bus_dmamap_destroy(sc->sc_dmatag,
691 sc->sc_txd[i].sd_map);
692 }
693 /*FALLTHROUGH*/
694 case CAS_ATT_5:
695 for (i = 0; i < CAS_NRXDESC; i++) {
696 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
697 bus_dmamap_unload(sc->sc_dmatag,
698 sc->sc_rxsoft[i].rxs_dmamap);
699 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
700 bus_dmamap_destroy(sc->sc_dmatag,
701 sc->sc_rxsoft[i].rxs_dmamap);
702 if (sc->sc_rxsoft[i].rxs_kva != NULL)
703 bus_dmamem_unmap(sc->sc_dmatag,
704 sc->sc_rxsoft[i].rxs_kva, CAS_PAGE_SIZE);
705 /* XXX need to check that bus_dmamem_alloc suceeded
706 if (sc->sc_rxsoft[i].rxs_dmaseg != NULL)
707 */
708 bus_dmamem_free(sc->sc_dmatag,
709 &(sc->sc_rxsoft[i].rxs_dmaseg), 1);
710 }
711 bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
712 /*FALLTHROUGH*/
713 case CAS_ATT_4:
714 case CAS_ATT_3:
715 bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
716 /*FALLTHROUGH*/
717 case CAS_ATT_2:
718 bus_dmamem_unmap(sc->sc_dmatag, sc->sc_control_data,
719 sizeof(struct cas_control_data));
720 /*FALLTHROUGH*/
721 case CAS_ATT_1:
722 bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
723 /*FALLTHROUGH*/
724 case CAS_ATT_0:
725 sc->sc_att_stage = CAS_ATT_0;
726 /*FALLTHROUGH*/
727 case CAS_ATT_BACKEND_2:
728 case CAS_ATT_BACKEND_1:
729 if (sc->sc_ih != NULL) {
730 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
731 sc->sc_ih = NULL;
732 }
733 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
734 /*FALLTHROUGH*/
735 case CAS_ATT_BACKEND_0:
736 break;
737 }
738 return 0;
739 }
740
741 static void
742 cas_partial_detach(struct cas_softc *sc, enum cas_attach_stage stage)
743 {
744 cfattach_t ca = device_cfattach(sc->sc_dev);
745
746 sc->sc_att_stage = stage;
747 (*ca->ca_detach)(sc->sc_dev, 0);
748 }
749
750 void
751 cas_tick(void *arg)
752 {
753 struct cas_softc *sc = arg;
754 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
755 bus_space_tag_t t = sc->sc_memt;
756 bus_space_handle_t mac = sc->sc_memh;
757 int s;
758 u_int32_t v;
759
760 /* unload collisions counters */
761 v = bus_space_read_4(t, mac, CAS_MAC_EXCESS_COLL_CNT) +
762 bus_space_read_4(t, mac, CAS_MAC_LATE_COLL_CNT);
763 ifp->if_collisions += v +
764 bus_space_read_4(t, mac, CAS_MAC_NORM_COLL_CNT) +
765 bus_space_read_4(t, mac, CAS_MAC_FIRST_COLL_CNT);
766 ifp->if_oerrors += v;
767
768 /* read error counters */
769 ifp->if_ierrors +=
770 bus_space_read_4(t, mac, CAS_MAC_RX_LEN_ERR_CNT) +
771 bus_space_read_4(t, mac, CAS_MAC_RX_ALIGN_ERR) +
772 bus_space_read_4(t, mac, CAS_MAC_RX_CRC_ERR_CNT) +
773 bus_space_read_4(t, mac, CAS_MAC_RX_CODE_VIOL);
774
775 /* clear the hardware counters */
776 bus_space_write_4(t, mac, CAS_MAC_NORM_COLL_CNT, 0);
777 bus_space_write_4(t, mac, CAS_MAC_FIRST_COLL_CNT, 0);
778 bus_space_write_4(t, mac, CAS_MAC_EXCESS_COLL_CNT, 0);
779 bus_space_write_4(t, mac, CAS_MAC_LATE_COLL_CNT, 0);
780 bus_space_write_4(t, mac, CAS_MAC_RX_LEN_ERR_CNT, 0);
781 bus_space_write_4(t, mac, CAS_MAC_RX_ALIGN_ERR, 0);
782 bus_space_write_4(t, mac, CAS_MAC_RX_CRC_ERR_CNT, 0);
783 bus_space_write_4(t, mac, CAS_MAC_RX_CODE_VIOL, 0);
784
785 s = splnet();
786 mii_tick(&sc->sc_mii);
787 splx(s);
788
789 callout_reset(&sc->sc_tick_ch, hz, cas_tick, sc);
790 }
791
792 int
793 cas_bitwait(struct cas_softc *sc, bus_space_handle_t h, int r,
794 u_int32_t clr, u_int32_t set)
795 {
796 int i;
797 u_int32_t reg;
798
799 for (i = TRIES; i--; DELAY(100)) {
800 reg = bus_space_read_4(sc->sc_memt, h, r);
801 if ((reg & clr) == 0 && (reg & set) == set)
802 return (1);
803 }
804
805 return (0);
806 }
807
808 void
809 cas_reset(struct cas_softc *sc)
810 {
811 bus_space_tag_t t = sc->sc_memt;
812 bus_space_handle_t h = sc->sc_memh;
813 int s;
814
815 s = splnet();
816 DPRINTF(sc, ("%s: cas_reset\n", device_xname(sc->sc_dev)));
817 cas_reset_rx(sc);
818 cas_reset_tx(sc);
819
820 /* Do a full reset */
821 bus_space_write_4(t, h, CAS_RESET,
822 CAS_RESET_RX | CAS_RESET_TX | CAS_RESET_BLOCK_PCS);
823 if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_RX | CAS_RESET_TX, 0))
824 aprint_error_dev(sc->sc_dev, "cannot reset device\n");
825 splx(s);
826 }
827
828
829 /*
830 * cas_rxdrain:
831 *
832 * Drain the receive queue.
833 */
834 void
835 cas_rxdrain(struct cas_softc *sc)
836 {
837 /* Nothing to do yet. */
838 }
839
840 /*
841 * Reset the whole thing.
842 */
843 void
844 cas_stop(struct ifnet *ifp, int disable)
845 {
846 struct cas_softc *sc = (struct cas_softc *)ifp->if_softc;
847 struct cas_sxd *sd;
848 u_int32_t i;
849
850 DPRINTF(sc, ("%s: cas_stop\n", device_xname(sc->sc_dev)));
851
852 callout_stop(&sc->sc_tick_ch);
853
854 /*
855 * Mark the interface down and cancel the watchdog timer.
856 */
857 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
858 ifp->if_timer = 0;
859
860 mii_down(&sc->sc_mii);
861
862 cas_reset_rx(sc);
863 cas_reset_tx(sc);
864
865 /*
866 * Release any queued transmit buffers.
867 */
868 for (i = 0; i < CAS_NTXDESC; i++) {
869 sd = &sc->sc_txd[i];
870 if (sd->sd_mbuf != NULL) {
871 bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0,
872 sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
873 bus_dmamap_unload(sc->sc_dmatag, sd->sd_map);
874 m_freem(sd->sd_mbuf);
875 sd->sd_mbuf = NULL;
876 }
877 }
878 sc->sc_tx_cnt = sc->sc_tx_prod = sc->sc_tx_cons = 0;
879
880 if (disable)
881 cas_rxdrain(sc);
882 }
883
884
885 /*
886 * Reset the receiver
887 */
888 int
889 cas_reset_rx(struct cas_softc *sc)
890 {
891 bus_space_tag_t t = sc->sc_memt;
892 bus_space_handle_t h = sc->sc_memh;
893
894 /*
895 * Resetting while DMA is in progress can cause a bus hang, so we
896 * disable DMA first.
897 */
898 cas_disable_rx(sc);
899 bus_space_write_4(t, h, CAS_RX_CONFIG, 0);
900 /* Wait till it finishes */
901 if (!cas_bitwait(sc, h, CAS_RX_CONFIG, 1, 0))
902 aprint_error_dev(sc->sc_dev, "cannot disable rx dma\n");
903 /* Wait 5ms extra. */
904 delay(5000);
905
906 /* Finally, reset the ERX */
907 bus_space_write_4(t, h, CAS_RESET, CAS_RESET_RX);
908 /* Wait till it finishes */
909 if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_RX, 0)) {
910 aprint_error_dev(sc->sc_dev, "cannot reset receiver\n");
911 return (1);
912 }
913 return (0);
914 }
915
916
917 /*
918 * Reset the transmitter
919 */
920 int
921 cas_reset_tx(struct cas_softc *sc)
922 {
923 bus_space_tag_t t = sc->sc_memt;
924 bus_space_handle_t h = sc->sc_memh;
925
926 /*
927 * Resetting while DMA is in progress can cause a bus hang, so we
928 * disable DMA first.
929 */
930 cas_disable_tx(sc);
931 bus_space_write_4(t, h, CAS_TX_CONFIG, 0);
932 /* Wait till it finishes */
933 if (!cas_bitwait(sc, h, CAS_TX_CONFIG, 1, 0))
934 aprint_error_dev(sc->sc_dev, "cannot disable tx dma\n");
935 /* Wait 5ms extra. */
936 delay(5000);
937
938 /* Finally, reset the ETX */
939 bus_space_write_4(t, h, CAS_RESET, CAS_RESET_TX);
940 /* Wait till it finishes */
941 if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_TX, 0)) {
942 aprint_error_dev(sc->sc_dev, "cannot reset transmitter\n");
943 return (1);
944 }
945 return (0);
946 }
947
948 /*
949 * Disable receiver.
950 */
951 int
952 cas_disable_rx(struct cas_softc *sc)
953 {
954 bus_space_tag_t t = sc->sc_memt;
955 bus_space_handle_t h = sc->sc_memh;
956 u_int32_t cfg;
957
958 /* Flip the enable bit */
959 cfg = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG);
960 cfg &= ~CAS_MAC_RX_ENABLE;
961 bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, cfg);
962
963 /* Wait for it to finish */
964 return (cas_bitwait(sc, h, CAS_MAC_RX_CONFIG, CAS_MAC_RX_ENABLE, 0));
965 }
966
967 /*
968 * Disable transmitter.
969 */
970 int
971 cas_disable_tx(struct cas_softc *sc)
972 {
973 bus_space_tag_t t = sc->sc_memt;
974 bus_space_handle_t h = sc->sc_memh;
975 u_int32_t cfg;
976
977 /* Flip the enable bit */
978 cfg = bus_space_read_4(t, h, CAS_MAC_TX_CONFIG);
979 cfg &= ~CAS_MAC_TX_ENABLE;
980 bus_space_write_4(t, h, CAS_MAC_TX_CONFIG, cfg);
981
982 /* Wait for it to finish */
983 return (cas_bitwait(sc, h, CAS_MAC_TX_CONFIG, CAS_MAC_TX_ENABLE, 0));
984 }
985
986 /*
987 * Initialize interface.
988 */
989 int
990 cas_meminit(struct cas_softc *sc)
991 {
992 struct cas_rxsoft *rxs;
993 int i, error;
994
995 rxs = (void *)&error;
996
997 /*
998 * Initialize the transmit descriptor ring.
999 */
1000 for (i = 0; i < CAS_NTXDESC; i++) {
1001 sc->sc_txdescs[i].cd_flags = 0;
1002 sc->sc_txdescs[i].cd_addr = 0;
1003 }
1004 CAS_CDTXSYNC(sc, 0, CAS_NTXDESC,
1005 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1006
1007 /*
1008 * Initialize the receive descriptor and receive job
1009 * descriptor rings.
1010 */
1011 for (i = 0; i < CAS_NRXDESC; i++)
1012 CAS_INIT_RXDESC(sc, i, i);
1013 sc->sc_rxdptr = 0;
1014 sc->sc_rxptr = 0;
1015
1016 /*
1017 * Initialize the receive completion ring.
1018 */
1019 for (i = 0; i < CAS_NRXCOMP; i++) {
1020 sc->sc_rxcomps[i].cc_word[0] = 0;
1021 sc->sc_rxcomps[i].cc_word[1] = 0;
1022 sc->sc_rxcomps[i].cc_word[2] = 0;
1023 sc->sc_rxcomps[i].cc_word[3] = CAS_DMA_WRITE(CAS_RC3_OWN);
1024 CAS_CDRXCSYNC(sc, i,
1025 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1026 }
1027
1028 return (0);
1029 }
1030
1031 int
1032 cas_ringsize(int sz)
1033 {
1034 switch (sz) {
1035 case 32:
1036 return CAS_RING_SZ_32;
1037 case 64:
1038 return CAS_RING_SZ_64;
1039 case 128:
1040 return CAS_RING_SZ_128;
1041 case 256:
1042 return CAS_RING_SZ_256;
1043 case 512:
1044 return CAS_RING_SZ_512;
1045 case 1024:
1046 return CAS_RING_SZ_1024;
1047 case 2048:
1048 return CAS_RING_SZ_2048;
1049 case 4096:
1050 return CAS_RING_SZ_4096;
1051 case 8192:
1052 return CAS_RING_SZ_8192;
1053 default:
1054 aprint_error("cas: invalid Receive Descriptor ring size %d\n",
1055 sz);
1056 return CAS_RING_SZ_32;
1057 }
1058 }
1059
1060 int
1061 cas_cringsize(int sz)
1062 {
1063 int i;
1064
1065 for (i = 0; i < 9; i++)
1066 if (sz == (128 << i))
1067 return i;
1068
1069 aprint_error("cas: invalid completion ring size %d\n", sz);
1070 return 128;
1071 }
1072
1073 /*
1074 * Initialization of interface; set up initialization block
1075 * and transmit/receive descriptor rings.
1076 */
1077 int
1078 cas_init(struct ifnet *ifp)
1079 {
1080 struct cas_softc *sc = (struct cas_softc *)ifp->if_softc;
1081 bus_space_tag_t t = sc->sc_memt;
1082 bus_space_handle_t h = sc->sc_memh;
1083 int s;
1084 u_int max_frame_size;
1085 u_int32_t v;
1086
1087 s = splnet();
1088
1089 DPRINTF(sc, ("%s: cas_init: calling stop\n", device_xname(sc->sc_dev)));
1090 /*
1091 * Initialization sequence. The numbered steps below correspond
1092 * to the sequence outlined in section 6.3.5.1 in the Ethernet
1093 * Channel Engine manual (part of the PCIO manual).
1094 * See also the STP2002-STQ document from Sun Microsystems.
1095 */
1096
1097 /* step 1 & 2. Reset the Ethernet Channel */
1098 cas_stop(ifp, 0);
1099 cas_reset(sc);
1100 DPRINTF(sc, ("%s: cas_init: restarting\n", device_xname(sc->sc_dev)));
1101
1102 /* Re-initialize the MIF */
1103 cas_mifinit(sc);
1104
1105 /* step 3. Setup data structures in host memory */
1106 cas_meminit(sc);
1107
1108 /* step 4. TX MAC registers & counters */
1109 cas_init_regs(sc);
1110 max_frame_size = ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN;
1111 v = (max_frame_size) | (0x2000 << 16) /* Burst size */;
1112 bus_space_write_4(t, h, CAS_MAC_MAC_MAX_FRAME, v);
1113
1114 /* step 5. RX MAC registers & counters */
1115 cas_iff(sc);
1116
1117 /* step 6 & 7. Program Descriptor Ring Base Addresses */
1118 KASSERT((CAS_CDTXADDR(sc, 0) & 0x1fff) == 0);
1119 bus_space_write_4(t, h, CAS_TX_RING_PTR_HI,
1120 (((uint64_t)CAS_CDTXADDR(sc,0)) >> 32));
1121 bus_space_write_4(t, h, CAS_TX_RING_PTR_LO, CAS_CDTXADDR(sc, 0));
1122
1123 KASSERT((CAS_CDRXADDR(sc, 0) & 0x1fff) == 0);
1124 bus_space_write_4(t, h, CAS_RX_DRING_PTR_HI,
1125 (((uint64_t)CAS_CDRXADDR(sc,0)) >> 32));
1126 bus_space_write_4(t, h, CAS_RX_DRING_PTR_LO, CAS_CDRXADDR(sc, 0));
1127
1128 KASSERT((CAS_CDRXCADDR(sc, 0) & 0x1fff) == 0);
1129 bus_space_write_4(t, h, CAS_RX_CRING_PTR_HI,
1130 (((uint64_t)CAS_CDRXCADDR(sc,0)) >> 32));
1131 bus_space_write_4(t, h, CAS_RX_CRING_PTR_LO, CAS_CDRXCADDR(sc, 0));
1132
1133 if (CAS_PLUS(sc)) {
1134 KASSERT((CAS_CDRXADDR2(sc, 0) & 0x1fff) == 0);
1135 bus_space_write_4(t, h, CAS_RX_DRING_PTR_HI2,
1136 (((uint64_t)CAS_CDRXADDR2(sc,0)) >> 32));
1137 bus_space_write_4(t, h, CAS_RX_DRING_PTR_LO2,
1138 CAS_CDRXADDR2(sc, 0));
1139 }
1140
1141 /* step 8. Global Configuration & Interrupt Mask */
1142 cas_estintr(sc, CAS_INTR_REG);
1143
1144 /* step 9. ETX Configuration: use mostly default values */
1145
1146 /* Enable DMA */
1147 v = cas_ringsize(CAS_NTXDESC /*XXX*/) << 10;
1148 bus_space_write_4(t, h, CAS_TX_CONFIG,
1149 v|CAS_TX_CONFIG_TXDMA_EN|(1<<24)|(1<<29));
1150 bus_space_write_4(t, h, CAS_TX_KICK, 0);
1151
1152 /* step 10. ERX Configuration */
1153
1154 /* Encode Receive Descriptor ring size */
1155 v = cas_ringsize(CAS_NRXDESC) << CAS_RX_CONFIG_RXDRNG_SZ_SHIFT;
1156 if (CAS_PLUS(sc))
1157 v |= cas_ringsize(32) << CAS_RX_CONFIG_RXDRNG2_SZ_SHIFT;
1158
1159 /* Encode Receive Completion ring size */
1160 v |= cas_cringsize(CAS_NRXCOMP) << CAS_RX_CONFIG_RXCRNG_SZ_SHIFT;
1161
1162 /* Enable DMA */
1163 bus_space_write_4(t, h, CAS_RX_CONFIG,
1164 v|(2<<CAS_RX_CONFIG_FBOFF_SHFT)|CAS_RX_CONFIG_RXDMA_EN);
1165
1166 /*
1167 * The following value is for an OFF Threshold of about 3/4 full
1168 * and an ON Threshold of 1/4 full.
1169 */
1170 bus_space_write_4(t, h, CAS_RX_PAUSE_THRESH,
1171 (3 * sc->sc_rxfifosize / 256) |
1172 ((sc->sc_rxfifosize / 256) << 12));
1173 bus_space_write_4(t, h, CAS_RX_BLANKING, (6 << 12) | 6);
1174
1175 /* step 11. Configure Media */
1176 mii_ifmedia_change(&sc->sc_mii);
1177
1178 /* step 12. RX_MAC Configuration Register */
1179 v = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG);
1180 v |= CAS_MAC_RX_ENABLE | CAS_MAC_RX_STRIP_CRC;
1181 bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, v);
1182
1183 /* step 14. Issue Transmit Pending command */
1184
1185 /* step 15. Give the receiver a swift kick */
1186 bus_space_write_4(t, h, CAS_RX_KICK, CAS_NRXDESC-4);
1187 if (CAS_PLUS(sc))
1188 bus_space_write_4(t, h, CAS_RX_KICK2, 4);
1189
1190 /* Start the one second timer. */
1191 callout_reset(&sc->sc_tick_ch, hz, cas_tick, sc);
1192
1193 ifp->if_flags |= IFF_RUNNING;
1194 ifp->if_flags &= ~IFF_OACTIVE;
1195 ifp->if_timer = 0;
1196 splx(s);
1197
1198 return (0);
1199 }
1200
1201 void
1202 cas_init_regs(struct cas_softc *sc)
1203 {
1204 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1205 bus_space_tag_t t = sc->sc_memt;
1206 bus_space_handle_t h = sc->sc_memh;
1207 const u_char *laddr = CLLADDR(ifp->if_sadl);
1208 u_int32_t v, r;
1209
1210 /* These regs are not cleared on reset */
1211 sc->sc_inited = 0;
1212 if (!sc->sc_inited) {
1213 /* Load recommended values */
1214 bus_space_write_4(t, h, CAS_MAC_IPG0, 0x00);
1215 bus_space_write_4(t, h, CAS_MAC_IPG1, 0x08);
1216 bus_space_write_4(t, h, CAS_MAC_IPG2, 0x04);
1217
1218 bus_space_write_4(t, h, CAS_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
1219 /* Max frame and max burst size */
1220 v = ETHER_MAX_LEN | (0x2000 << 16) /* Burst size */;
1221 bus_space_write_4(t, h, CAS_MAC_MAC_MAX_FRAME, v);
1222
1223 bus_space_write_4(t, h, CAS_MAC_PREAMBLE_LEN, 0x07);
1224 bus_space_write_4(t, h, CAS_MAC_JAM_SIZE, 0x04);
1225 bus_space_write_4(t, h, CAS_MAC_ATTEMPT_LIMIT, 0x10);
1226 bus_space_write_4(t, h, CAS_MAC_CONTROL_TYPE, 0x8088);
1227 bus_space_write_4(t, h, CAS_MAC_RANDOM_SEED,
1228 ((laddr[5]<<8)|laddr[4])&0x3ff);
1229
1230 /* Secondary MAC addresses set to 0:0:0:0:0:0 */
1231 for (r = CAS_MAC_ADDR3; r < CAS_MAC_ADDR42; r += 4)
1232 bus_space_write_4(t, h, r, 0);
1233
1234 /* MAC control addr set to 0:1:c2:0:1:80 */
1235 bus_space_write_4(t, h, CAS_MAC_ADDR42, 0x0001);
1236 bus_space_write_4(t, h, CAS_MAC_ADDR43, 0xc200);
1237 bus_space_write_4(t, h, CAS_MAC_ADDR44, 0x0180);
1238
1239 /* MAC filter addr set to 0:0:0:0:0:0 */
1240 bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER0, 0);
1241 bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER1, 0);
1242 bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER2, 0);
1243
1244 bus_space_write_4(t, h, CAS_MAC_ADR_FLT_MASK1_2, 0);
1245 bus_space_write_4(t, h, CAS_MAC_ADR_FLT_MASK0, 0);
1246
1247 /* Hash table initialized to 0 */
1248 for (r = CAS_MAC_HASH0; r <= CAS_MAC_HASH15; r += 4)
1249 bus_space_write_4(t, h, r, 0);
1250
1251 sc->sc_inited = 1;
1252 }
1253
1254 /* Counters need to be zeroed */
1255 bus_space_write_4(t, h, CAS_MAC_NORM_COLL_CNT, 0);
1256 bus_space_write_4(t, h, CAS_MAC_FIRST_COLL_CNT, 0);
1257 bus_space_write_4(t, h, CAS_MAC_EXCESS_COLL_CNT, 0);
1258 bus_space_write_4(t, h, CAS_MAC_LATE_COLL_CNT, 0);
1259 bus_space_write_4(t, h, CAS_MAC_DEFER_TMR_CNT, 0);
1260 bus_space_write_4(t, h, CAS_MAC_PEAK_ATTEMPTS, 0);
1261 bus_space_write_4(t, h, CAS_MAC_RX_FRAME_COUNT, 0);
1262 bus_space_write_4(t, h, CAS_MAC_RX_LEN_ERR_CNT, 0);
1263 bus_space_write_4(t, h, CAS_MAC_RX_ALIGN_ERR, 0);
1264 bus_space_write_4(t, h, CAS_MAC_RX_CRC_ERR_CNT, 0);
1265 bus_space_write_4(t, h, CAS_MAC_RX_CODE_VIOL, 0);
1266
1267 /* Un-pause stuff */
1268 bus_space_write_4(t, h, CAS_MAC_SEND_PAUSE_CMD, 0);
1269
1270 /*
1271 * Set the station address.
1272 */
1273 bus_space_write_4(t, h, CAS_MAC_ADDR0, (laddr[4]<<8) | laddr[5]);
1274 bus_space_write_4(t, h, CAS_MAC_ADDR1, (laddr[2]<<8) | laddr[3]);
1275 bus_space_write_4(t, h, CAS_MAC_ADDR2, (laddr[0]<<8) | laddr[1]);
1276 }
1277
1278 /*
1279 * Receive interrupt.
1280 */
1281 int
1282 cas_rint(struct cas_softc *sc)
1283 {
1284 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1285 bus_space_tag_t t = sc->sc_memt;
1286 bus_space_handle_t h = sc->sc_memh;
1287 struct cas_rxsoft *rxs;
1288 struct mbuf *m;
1289 u_int64_t word[4];
1290 int len, off, idx;
1291 int i, skip;
1292 void *cp;
1293
1294 for (i = sc->sc_rxptr;; i = CAS_NEXTRX(i + skip)) {
1295 CAS_CDRXCSYNC(sc, i,
1296 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1297
1298 word[0] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[0]);
1299 word[1] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[1]);
1300 word[2] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[2]);
1301 word[3] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[3]);
1302
1303 /* Stop if the hardware still owns the descriptor. */
1304 if ((word[0] & CAS_RC0_TYPE) == 0 || word[3] & CAS_RC3_OWN)
1305 break;
1306
1307 len = CAS_RC1_HDR_LEN(word[1]);
1308 if (len > 0) {
1309 off = CAS_RC1_HDR_OFF(word[1]);
1310 idx = CAS_RC1_HDR_IDX(word[1]);
1311 rxs = &sc->sc_rxsoft[idx];
1312
1313 DPRINTF(sc, ("hdr at idx %d, off %d, len %d\n",
1314 idx, off, len));
1315
1316 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1317 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1318
1319 cp = rxs->rxs_kva + off * 256 + ETHER_ALIGN;
1320 m = m_devget(cp, len, 0, ifp, NULL);
1321
1322 if (word[0] & CAS_RC0_RELEASE_HDR)
1323 cas_add_rxbuf(sc, idx);
1324
1325 if (m != NULL) {
1326
1327 /*
1328 * Pass this up to any BPF listeners, but only
1329 * pass it up the stack if its for us.
1330 */
1331 if (ifp->if_bpf)
1332 bpf_ops->bpf_mtap(ifp->if_bpf, m);
1333
1334 ifp->if_ipackets++;
1335 m->m_pkthdr.csum_flags = 0;
1336 (*ifp->if_input)(ifp, m);
1337 } else
1338 ifp->if_ierrors++;
1339 }
1340
1341 len = CAS_RC0_DATA_LEN(word[0]);
1342 if (len > 0) {
1343 off = CAS_RC0_DATA_OFF(word[0]);
1344 idx = CAS_RC0_DATA_IDX(word[0]);
1345 rxs = &sc->sc_rxsoft[idx];
1346
1347 DPRINTF(sc, ("data at idx %d, off %d, len %d\n",
1348 idx, off, len));
1349
1350 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1351 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1352
1353 /* XXX We should not be copying the packet here. */
1354 cp = rxs->rxs_kva + off + ETHER_ALIGN;
1355 m = m_devget(cp, len, 0, ifp, NULL);
1356
1357 if (word[0] & CAS_RC0_RELEASE_DATA)
1358 cas_add_rxbuf(sc, idx);
1359
1360 if (m != NULL) {
1361 /*
1362 * Pass this up to any BPF listeners, but only
1363 * pass it up the stack if its for us.
1364 */
1365 if (ifp->if_bpf)
1366 bpf_ops->bpf_mtap(ifp->if_bpf, m);
1367
1368 ifp->if_ipackets++;
1369 m->m_pkthdr.csum_flags = 0;
1370 (*ifp->if_input)(ifp, m);
1371 } else
1372 ifp->if_ierrors++;
1373 }
1374
1375 if (word[0] & CAS_RC0_SPLIT)
1376 aprint_error_dev(sc->sc_dev, "split packet\n");
1377
1378 skip = CAS_RC0_SKIP(word[0]);
1379 }
1380
1381 while (sc->sc_rxptr != i) {
1382 sc->sc_rxcomps[sc->sc_rxptr].cc_word[0] = 0;
1383 sc->sc_rxcomps[sc->sc_rxptr].cc_word[1] = 0;
1384 sc->sc_rxcomps[sc->sc_rxptr].cc_word[2] = 0;
1385 sc->sc_rxcomps[sc->sc_rxptr].cc_word[3] =
1386 CAS_DMA_WRITE(CAS_RC3_OWN);
1387 CAS_CDRXCSYNC(sc, sc->sc_rxptr,
1388 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1389
1390 sc->sc_rxptr = CAS_NEXTRX(sc->sc_rxptr);
1391 }
1392
1393 bus_space_write_4(t, h, CAS_RX_COMP_TAIL, sc->sc_rxptr);
1394
1395 DPRINTF(sc, ("cas_rint: done sc->rxptr %d, complete %d\n",
1396 sc->sc_rxptr, bus_space_read_4(t, h, CAS_RX_COMPLETION)));
1397
1398 return (1);
1399 }
1400
1401 /*
1402 * cas_add_rxbuf:
1403 *
1404 * Add a receive buffer to the indicated descriptor.
1405 */
1406 int
1407 cas_add_rxbuf(struct cas_softc *sc, int idx)
1408 {
1409 bus_space_tag_t t = sc->sc_memt;
1410 bus_space_handle_t h = sc->sc_memh;
1411
1412 CAS_INIT_RXDESC(sc, sc->sc_rxdptr, idx);
1413
1414 if ((sc->sc_rxdptr % 4) == 0)
1415 bus_space_write_4(t, h, CAS_RX_KICK, sc->sc_rxdptr);
1416
1417 if (++sc->sc_rxdptr == CAS_NRXDESC)
1418 sc->sc_rxdptr = 0;
1419
1420 return (0);
1421 }
1422
1423 int
1424 cas_eint(struct cas_softc *sc, u_int status)
1425 {
1426 char bits[128];
1427 if ((status & CAS_INTR_MIF) != 0) {
1428 DPRINTF(sc, ("%s: link status changed\n",
1429 device_xname(sc->sc_dev)));
1430 return (1);
1431 }
1432
1433 snprintb(bits, sizeof(bits), CAS_INTR_BITS, status);
1434 printf("%s: status=%s\n", device_xname(sc->sc_dev), bits);
1435 return (1);
1436 }
1437
1438 int
1439 cas_pint(struct cas_softc *sc)
1440 {
1441 bus_space_tag_t t = sc->sc_memt;
1442 bus_space_handle_t seb = sc->sc_memh;
1443 u_int32_t status;
1444
1445 status = bus_space_read_4(t, seb, CAS_MII_INTERRUP_STATUS);
1446 status |= bus_space_read_4(t, seb, CAS_MII_INTERRUP_STATUS);
1447 #ifdef CAS_DEBUG
1448 if (status)
1449 printf("%s: link status changed\n", device_xname(sc->sc_dev));
1450 #endif
1451 return (1);
1452 }
1453
1454 int
1455 cas_intr(void *v)
1456 {
1457 struct cas_softc *sc = (struct cas_softc *)v;
1458 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1459 bus_space_tag_t t = sc->sc_memt;
1460 bus_space_handle_t seb = sc->sc_memh;
1461 u_int32_t status;
1462 int r = 0;
1463 #ifdef CAS_DEBUG
1464 char bits[128];
1465 #endif
1466
1467 sc->sc_ev_intr.ev_count++;
1468
1469 status = bus_space_read_4(t, seb, CAS_STATUS);
1470 #ifdef CAS_DEBUG
1471 snprintb(bits, sizeof(bits), CAS_INTR_BITS, status);
1472 #endif
1473 DPRINTF(sc, ("%s: cas_intr: cplt %x status %s\n",
1474 device_xname(sc->sc_dev), (status>>19), bits));
1475
1476 if ((status & CAS_INTR_PCS) != 0)
1477 r |= cas_pint(sc);
1478
1479 if ((status & (CAS_INTR_TX_TAG_ERR | CAS_INTR_RX_TAG_ERR |
1480 CAS_INTR_RX_COMP_FULL | CAS_INTR_BERR)) != 0)
1481 r |= cas_eint(sc, status);
1482
1483 if ((status & (CAS_INTR_TX_EMPTY | CAS_INTR_TX_INTME)) != 0)
1484 r |= cas_tint(sc, status);
1485
1486 if ((status & (CAS_INTR_RX_DONE | CAS_INTR_RX_NOBUF)) != 0)
1487 r |= cas_rint(sc);
1488
1489 /* We should eventually do more than just print out error stats. */
1490 if (status & CAS_INTR_TX_MAC) {
1491 int txstat = bus_space_read_4(t, seb, CAS_MAC_TX_STATUS);
1492 #ifdef CAS_DEBUG
1493 if (txstat & ~CAS_MAC_TX_XMIT_DONE)
1494 printf("%s: MAC tx fault, status %x\n",
1495 device_xname(sc->sc_dev), txstat);
1496 #endif
1497 if (txstat & (CAS_MAC_TX_UNDERRUN | CAS_MAC_TX_PKT_TOO_LONG))
1498 cas_init(ifp);
1499 }
1500 if (status & CAS_INTR_RX_MAC) {
1501 int rxstat = bus_space_read_4(t, seb, CAS_MAC_RX_STATUS);
1502 #ifdef CAS_DEBUG
1503 if (rxstat & ~CAS_MAC_RX_DONE)
1504 printf("%s: MAC rx fault, status %x\n",
1505 device_xname(sc->sc_dev), rxstat);
1506 #endif
1507 /*
1508 * On some chip revisions CAS_MAC_RX_OVERFLOW happen often
1509 * due to a silicon bug so handle them silently.
1510 */
1511 if (rxstat & CAS_MAC_RX_OVERFLOW) {
1512 ifp->if_ierrors++;
1513 cas_init(ifp);
1514 }
1515 #ifdef CAS_DEBUG
1516 else if (rxstat & ~(CAS_MAC_RX_DONE | CAS_MAC_RX_FRAME_CNT))
1517 printf("%s: MAC rx fault, status %x\n",
1518 device_xname(sc->sc_dev), rxstat);
1519 #endif
1520 }
1521 #if NRND > 0
1522 rnd_add_uint32(&sc->rnd_source, status);
1523 #endif
1524 return (r);
1525 }
1526
1527
1528 void
1529 cas_watchdog(struct ifnet *ifp)
1530 {
1531 struct cas_softc *sc = ifp->if_softc;
1532
1533 DPRINTF(sc, ("cas_watchdog: CAS_RX_CONFIG %x CAS_MAC_RX_STATUS %x "
1534 "CAS_MAC_RX_CONFIG %x\n",
1535 bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_RX_CONFIG),
1536 bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_MAC_RX_STATUS),
1537 bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_MAC_RX_CONFIG)));
1538
1539 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
1540 ++ifp->if_oerrors;
1541
1542 /* Try to get more packets going. */
1543 cas_init(ifp);
1544 }
1545
1546 /*
1547 * Initialize the MII Management Interface
1548 */
1549 void
1550 cas_mifinit(struct cas_softc *sc)
1551 {
1552 bus_space_tag_t t = sc->sc_memt;
1553 bus_space_handle_t mif = sc->sc_memh;
1554
1555 /* Configure the MIF in frame mode */
1556 sc->sc_mif_config = bus_space_read_4(t, mif, CAS_MIF_CONFIG);
1557 sc->sc_mif_config &= ~CAS_MIF_CONFIG_BB_ENA;
1558 bus_space_write_4(t, mif, CAS_MIF_CONFIG, sc->sc_mif_config);
1559 }
1560
1561 /*
1562 * MII interface
1563 *
1564 * The Cassini MII interface supports at least three different operating modes:
1565 *
1566 * Bitbang mode is implemented using data, clock and output enable registers.
1567 *
1568 * Frame mode is implemented by loading a complete frame into the frame
1569 * register and polling the valid bit for completion.
1570 *
1571 * Polling mode uses the frame register but completion is indicated by
1572 * an interrupt.
1573 *
1574 */
1575 int
1576 cas_mii_readreg(device_t self, int phy, int reg)
1577 {
1578 struct cas_softc *sc = device_private(self);
1579 bus_space_tag_t t = sc->sc_memt;
1580 bus_space_handle_t mif = sc->sc_memh;
1581 int n;
1582 u_int32_t v;
1583
1584 #ifdef CAS_DEBUG
1585 if (sc->sc_debug)
1586 printf("cas_mii_readreg: phy %d reg %d\n", phy, reg);
1587 #endif
1588
1589 /* Construct the frame command */
1590 v = (reg << CAS_MIF_REG_SHIFT) | (phy << CAS_MIF_PHY_SHIFT) |
1591 CAS_MIF_FRAME_READ;
1592
1593 bus_space_write_4(t, mif, CAS_MIF_FRAME, v);
1594 for (n = 0; n < 100; n++) {
1595 DELAY(1);
1596 v = bus_space_read_4(t, mif, CAS_MIF_FRAME);
1597 if (v & CAS_MIF_FRAME_TA0)
1598 return (v & CAS_MIF_FRAME_DATA);
1599 }
1600
1601 printf("%s: mii_read timeout\n", device_xname(sc->sc_dev));
1602 return (0);
1603 }
1604
1605 void
1606 cas_mii_writereg(device_t self, int phy, int reg, int val)
1607 {
1608 struct cas_softc *sc = device_private(self);
1609 bus_space_tag_t t = sc->sc_memt;
1610 bus_space_handle_t mif = sc->sc_memh;
1611 int n;
1612 u_int32_t v;
1613
1614 #ifdef CAS_DEBUG
1615 if (sc->sc_debug)
1616 printf("cas_mii_writereg: phy %d reg %d val %x\n",
1617 phy, reg, val);
1618 #endif
1619
1620 /* Construct the frame command */
1621 v = CAS_MIF_FRAME_WRITE |
1622 (phy << CAS_MIF_PHY_SHIFT) |
1623 (reg << CAS_MIF_REG_SHIFT) |
1624 (val & CAS_MIF_FRAME_DATA);
1625
1626 bus_space_write_4(t, mif, CAS_MIF_FRAME, v);
1627 for (n = 0; n < 100; n++) {
1628 DELAY(1);
1629 v = bus_space_read_4(t, mif, CAS_MIF_FRAME);
1630 if (v & CAS_MIF_FRAME_TA0)
1631 return;
1632 }
1633
1634 printf("%s: mii_write timeout\n", device_xname(sc->sc_dev));
1635 }
1636
1637 void
1638 cas_mii_statchg(device_t self)
1639 {
1640 struct cas_softc *sc = device_private(self);
1641 #ifdef CAS_DEBUG
1642 int instance = IFM_INST(sc->sc_media.ifm_cur->ifm_media);
1643 #endif
1644 bus_space_tag_t t = sc->sc_memt;
1645 bus_space_handle_t mac = sc->sc_memh;
1646 u_int32_t v;
1647
1648 #ifdef CAS_DEBUG
1649 if (sc->sc_debug)
1650 printf("cas_mii_statchg: status change: phy = %d\n",
1651 sc->sc_phys[instance]);
1652 #endif
1653
1654 /* Set tx full duplex options */
1655 bus_space_write_4(t, mac, CAS_MAC_TX_CONFIG, 0);
1656 delay(10000); /* reg must be cleared and delay before changing. */
1657 v = CAS_MAC_TX_ENA_IPG0|CAS_MAC_TX_NGU|CAS_MAC_TX_NGU_LIMIT|
1658 CAS_MAC_TX_ENABLE;
1659 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
1660 v |= CAS_MAC_TX_IGN_CARRIER|CAS_MAC_TX_IGN_COLLIS;
1661 }
1662 bus_space_write_4(t, mac, CAS_MAC_TX_CONFIG, v);
1663
1664 /* XIF Configuration */
1665 v = CAS_MAC_XIF_TX_MII_ENA;
1666 v |= CAS_MAC_XIF_LINK_LED;
1667
1668 /* MII needs echo disable if half duplex. */
1669 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1670 /* turn on full duplex LED */
1671 v |= CAS_MAC_XIF_FDPLX_LED;
1672 else
1673 /* half duplex -- disable echo */
1674 v |= CAS_MAC_XIF_ECHO_DISABL;
1675
1676 switch (IFM_SUBTYPE(sc->sc_mii.mii_media_active)) {
1677 case IFM_1000_T: /* Gigabit using GMII interface */
1678 case IFM_1000_SX:
1679 v |= CAS_MAC_XIF_GMII_MODE;
1680 break;
1681 default:
1682 v &= ~CAS_MAC_XIF_GMII_MODE;
1683 }
1684 bus_space_write_4(t, mac, CAS_MAC_XIF_CONFIG, v);
1685 }
1686
1687 int
1688 cas_pcs_readreg(device_t self, int phy, int reg)
1689 {
1690 struct cas_softc *sc = device_private(self);
1691 bus_space_tag_t t = sc->sc_memt;
1692 bus_space_handle_t pcs = sc->sc_memh;
1693
1694 #ifdef CAS_DEBUG
1695 if (sc->sc_debug)
1696 printf("cas_pcs_readreg: phy %d reg %d\n", phy, reg);
1697 #endif
1698
1699 if (phy != CAS_PHYAD_EXTERNAL)
1700 return (0);
1701
1702 switch (reg) {
1703 case MII_BMCR:
1704 reg = CAS_MII_CONTROL;
1705 break;
1706 case MII_BMSR:
1707 reg = CAS_MII_STATUS;
1708 break;
1709 case MII_ANAR:
1710 reg = CAS_MII_ANAR;
1711 break;
1712 case MII_ANLPAR:
1713 reg = CAS_MII_ANLPAR;
1714 break;
1715 case MII_EXTSR:
1716 return (EXTSR_1000XFDX|EXTSR_1000XHDX);
1717 default:
1718 return (0);
1719 }
1720
1721 return bus_space_read_4(t, pcs, reg);
1722 }
1723
1724 void
1725 cas_pcs_writereg(device_t self, int phy, int reg, int val)
1726 {
1727 struct cas_softc *sc = device_private(self);
1728 bus_space_tag_t t = sc->sc_memt;
1729 bus_space_handle_t pcs = sc->sc_memh;
1730 int reset = 0;
1731
1732 #ifdef CAS_DEBUG
1733 if (sc->sc_debug)
1734 printf("cas_pcs_writereg: phy %d reg %d val %x\n",
1735 phy, reg, val);
1736 #endif
1737
1738 if (phy != CAS_PHYAD_EXTERNAL)
1739 return;
1740
1741 if (reg == MII_ANAR)
1742 bus_space_write_4(t, pcs, CAS_MII_CONFIG, 0);
1743
1744 switch (reg) {
1745 case MII_BMCR:
1746 reset = (val & CAS_MII_CONTROL_RESET);
1747 reg = CAS_MII_CONTROL;
1748 break;
1749 case MII_BMSR:
1750 reg = CAS_MII_STATUS;
1751 break;
1752 case MII_ANAR:
1753 reg = CAS_MII_ANAR;
1754 break;
1755 case MII_ANLPAR:
1756 reg = CAS_MII_ANLPAR;
1757 break;
1758 default:
1759 return;
1760 }
1761
1762 bus_space_write_4(t, pcs, reg, val);
1763
1764 if (reset)
1765 cas_bitwait(sc, pcs, CAS_MII_CONTROL, CAS_MII_CONTROL_RESET, 0);
1766
1767 if (reg == CAS_MII_ANAR || reset)
1768 bus_space_write_4(t, pcs, CAS_MII_CONFIG,
1769 CAS_MII_CONFIG_ENABLE);
1770 }
1771
1772 int
1773 cas_mediachange(struct ifnet *ifp)
1774 {
1775 struct cas_softc *sc = ifp->if_softc;
1776 struct mii_data *mii = &sc->sc_mii;
1777
1778 if (mii->mii_instance) {
1779 struct mii_softc *miisc;
1780 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1781 mii_phy_reset(miisc);
1782 }
1783
1784 return (mii_mediachg(&sc->sc_mii));
1785 }
1786
1787 void
1788 cas_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1789 {
1790 struct cas_softc *sc = ifp->if_softc;
1791
1792 mii_pollstat(&sc->sc_mii);
1793 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1794 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1795 }
1796
1797 /*
1798 * Process an ioctl request.
1799 */
1800 int
1801 cas_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1802 {
1803 struct cas_softc *sc = ifp->if_softc;
1804 int s, error = 0;
1805
1806 s = splnet();
1807
1808 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1809 error = 0;
1810 if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
1811 ;
1812 else if (ifp->if_flags & IFF_RUNNING) {
1813 /*
1814 * Multicast list has changed; set the hardware filter
1815 * accordingly.
1816 */
1817 cas_iff(sc);
1818 }
1819 }
1820
1821 splx(s);
1822 return (error);
1823 }
1824
1825 static bool
1826 cas_suspend(device_t self, pmf_qual_t qual)
1827 {
1828 struct cas_softc *sc = device_private(self);
1829 bus_space_tag_t t = sc->sc_memt;
1830 bus_space_handle_t h = sc->sc_memh;
1831
1832 bus_space_write_4(t, h, CAS_INTMASK, ~(uint32_t)0);
1833 if (sc->sc_ih != NULL) {
1834 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
1835 sc->sc_ih = NULL;
1836 }
1837
1838 return true;
1839 }
1840
1841 static bool
1842 cas_resume(device_t self, pmf_qual_t qual)
1843 {
1844 struct cas_softc *sc = device_private(self);
1845
1846 return cas_estintr(sc, CAS_INTR_PCI | CAS_INTR_REG);
1847 }
1848
1849 static bool
1850 cas_estintr(struct cas_softc *sc, int what)
1851 {
1852 bus_space_tag_t t = sc->sc_memt;
1853 bus_space_handle_t h = sc->sc_memh;
1854 const char *intrstr = NULL;
1855
1856 /* PCI interrupts */
1857 if (what & CAS_INTR_PCI) {
1858 intrstr = pci_intr_string(sc->sc_pc, sc->sc_handle);
1859 sc->sc_ih = pci_intr_establish(sc->sc_pc, sc->sc_handle,
1860 IPL_NET, cas_intr, sc);
1861 if (sc->sc_ih == NULL) {
1862 aprint_error_dev(sc->sc_dev,
1863 "unable to establish interrupt");
1864 if (intrstr != NULL)
1865 aprint_error(" at %s", intrstr);
1866 aprint_error("\n");
1867 return false;
1868 }
1869
1870 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
1871 }
1872
1873 /* Interrupt register */
1874 if (what & CAS_INTR_REG) {
1875 bus_space_write_4(t, h, CAS_INTMASK,
1876 ~(CAS_INTR_TX_INTME|CAS_INTR_TX_EMPTY|
1877 CAS_INTR_TX_TAG_ERR|
1878 CAS_INTR_RX_DONE|CAS_INTR_RX_NOBUF|
1879 CAS_INTR_RX_TAG_ERR|
1880 CAS_INTR_RX_COMP_FULL|CAS_INTR_PCS|
1881 CAS_INTR_MAC_CONTROL|CAS_INTR_MIF|
1882 CAS_INTR_BERR));
1883 bus_space_write_4(t, h, CAS_MAC_RX_MASK,
1884 CAS_MAC_RX_DONE|CAS_MAC_RX_FRAME_CNT);
1885 bus_space_write_4(t, h, CAS_MAC_TX_MASK, CAS_MAC_TX_XMIT_DONE);
1886 bus_space_write_4(t, h, CAS_MAC_CONTROL_MASK, 0); /* XXXX */
1887 }
1888 return true;
1889 }
1890
1891 bool
1892 cas_shutdown(device_t self, int howto)
1893 {
1894 struct cas_softc *sc = device_private(self);
1895 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1896
1897 cas_stop(ifp, 1);
1898
1899 return true;
1900 }
1901
1902 void
1903 cas_iff(struct cas_softc *sc)
1904 {
1905 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1906 struct ethercom *ec = &sc->sc_ethercom;
1907 struct ether_multi *enm;
1908 struct ether_multistep step;
1909 bus_space_tag_t t = sc->sc_memt;
1910 bus_space_handle_t h = sc->sc_memh;
1911 u_int32_t crc, hash[16], rxcfg;
1912 int i;
1913
1914 rxcfg = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG);
1915 rxcfg &= ~(CAS_MAC_RX_HASH_FILTER | CAS_MAC_RX_PROMISCUOUS |
1916 CAS_MAC_RX_PROMISC_GRP);
1917 ifp->if_flags &= ~IFF_ALLMULTI;
1918
1919 if (ifp->if_flags & IFF_PROMISC || ec->ec_multicnt > 0) {
1920 ifp->if_flags |= IFF_ALLMULTI;
1921 if (ifp->if_flags & IFF_PROMISC)
1922 rxcfg |= CAS_MAC_RX_PROMISCUOUS;
1923 else
1924 rxcfg |= CAS_MAC_RX_PROMISC_GRP;
1925 } else {
1926 /*
1927 * Set up multicast address filter by passing all multicast
1928 * addresses through a crc generator, and then using the
1929 * high order 8 bits as an index into the 256 bit logical
1930 * address filter. The high order 4 bits selects the word,
1931 * while the other 4 bits select the bit within the word
1932 * (where bit 0 is the MSB).
1933 */
1934
1935 rxcfg |= CAS_MAC_RX_HASH_FILTER;
1936
1937 /* Clear hash table */
1938 for (i = 0; i < 16; i++)
1939 hash[i] = 0;
1940
1941 ETHER_FIRST_MULTI(step, ec, enm);
1942 while (enm != NULL) {
1943 crc = ether_crc32_le(enm->enm_addrlo,
1944 ETHER_ADDR_LEN);
1945
1946 /* Just want the 8 most significant bits. */
1947 crc >>= 24;
1948
1949 /* Set the corresponding bit in the filter. */
1950 hash[crc >> 4] |= 1 << (15 - (crc & 15));
1951
1952 ETHER_NEXT_MULTI(step, enm);
1953 }
1954
1955 /* Now load the hash table into the chip (if we are using it) */
1956 for (i = 0; i < 16; i++) {
1957 bus_space_write_4(t, h,
1958 CAS_MAC_HASH0 + i * (CAS_MAC_HASH1 - CAS_MAC_HASH0),
1959 hash[i]);
1960 }
1961 }
1962
1963 bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, rxcfg);
1964 }
1965
1966 int
1967 cas_encap(struct cas_softc *sc, struct mbuf *mhead, u_int32_t *bixp)
1968 {
1969 u_int64_t flags;
1970 u_int32_t cur, frag, i;
1971 bus_dmamap_t map;
1972
1973 cur = frag = *bixp;
1974 map = sc->sc_txd[cur].sd_map;
1975
1976 if (bus_dmamap_load_mbuf(sc->sc_dmatag, map, mhead,
1977 BUS_DMA_NOWAIT) != 0) {
1978 return (ENOBUFS);
1979 }
1980
1981 if ((sc->sc_tx_cnt + map->dm_nsegs) > (CAS_NTXDESC - 2)) {
1982 bus_dmamap_unload(sc->sc_dmatag, map);
1983 return (ENOBUFS);
1984 }
1985
1986 bus_dmamap_sync(sc->sc_dmatag, map, 0, map->dm_mapsize,
1987 BUS_DMASYNC_PREWRITE);
1988
1989 for (i = 0; i < map->dm_nsegs; i++) {
1990 sc->sc_txdescs[frag].cd_addr =
1991 CAS_DMA_WRITE(map->dm_segs[i].ds_addr);
1992 flags = (map->dm_segs[i].ds_len & CAS_TD_BUFSIZE) |
1993 (i == 0 ? CAS_TD_START_OF_PACKET : 0) |
1994 ((i == (map->dm_nsegs - 1)) ? CAS_TD_END_OF_PACKET : 0);
1995 sc->sc_txdescs[frag].cd_flags = CAS_DMA_WRITE(flags);
1996 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cddmamap,
1997 CAS_CDTXOFF(frag), sizeof(struct cas_desc),
1998 BUS_DMASYNC_PREWRITE);
1999 cur = frag;
2000 if (++frag == CAS_NTXDESC)
2001 frag = 0;
2002 }
2003
2004 sc->sc_tx_cnt += map->dm_nsegs;
2005 sc->sc_txd[*bixp].sd_map = sc->sc_txd[cur].sd_map;
2006 sc->sc_txd[cur].sd_map = map;
2007 sc->sc_txd[cur].sd_mbuf = mhead;
2008
2009 bus_space_write_4(sc->sc_memt, sc->sc_memh, CAS_TX_KICK, frag);
2010
2011 *bixp = frag;
2012
2013 /* sync descriptors */
2014
2015 return (0);
2016 }
2017
2018 /*
2019 * Transmit interrupt.
2020 */
2021 int
2022 cas_tint(struct cas_softc *sc, u_int32_t status)
2023 {
2024 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2025 struct cas_sxd *sd;
2026 u_int32_t cons, comp;
2027
2028 comp = bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_TX_COMPLETION);
2029 cons = sc->sc_tx_cons;
2030 while (cons != comp) {
2031 sd = &sc->sc_txd[cons];
2032 if (sd->sd_mbuf != NULL) {
2033 bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0,
2034 sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2035 bus_dmamap_unload(sc->sc_dmatag, sd->sd_map);
2036 m_freem(sd->sd_mbuf);
2037 sd->sd_mbuf = NULL;
2038 ifp->if_opackets++;
2039 }
2040 sc->sc_tx_cnt--;
2041 if (++cons == CAS_NTXDESC)
2042 cons = 0;
2043 }
2044 sc->sc_tx_cons = cons;
2045
2046 if (sc->sc_tx_cnt < CAS_NTXDESC - 2)
2047 ifp->if_flags &= ~IFF_OACTIVE;
2048 if (sc->sc_tx_cnt == 0)
2049 ifp->if_timer = 0;
2050
2051 cas_start(ifp);
2052
2053 return (1);
2054 }
2055
2056 void
2057 cas_start(struct ifnet *ifp)
2058 {
2059 struct cas_softc *sc = ifp->if_softc;
2060 struct mbuf *m;
2061 u_int32_t bix;
2062
2063 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2064 return;
2065
2066 bix = sc->sc_tx_prod;
2067 while (sc->sc_txd[bix].sd_mbuf == NULL) {
2068 IFQ_POLL(&ifp->if_snd, m);
2069 if (m == NULL)
2070 break;
2071
2072 /*
2073 * If BPF is listening on this interface, let it see the
2074 * packet before we commit it to the wire.
2075 */
2076 if (ifp->if_bpf)
2077 bpf_ops->bpf_mtap(ifp->if_bpf, m);
2078
2079 /*
2080 * Encapsulate this packet and start it going...
2081 * or fail...
2082 */
2083 if (cas_encap(sc, m, &bix)) {
2084 ifp->if_flags |= IFF_OACTIVE;
2085 break;
2086 }
2087
2088 IFQ_DEQUEUE(&ifp->if_snd, m);
2089 ifp->if_timer = 5;
2090 }
2091
2092 sc->sc_tx_prod = bix;
2093 }
2094