if_sip.c revision 1.11.4.1 1 /* $NetBSD: if_sip.c,v 1.11.4.1 2000/08/12 18:50:25 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 1999 Network Computer, 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. Neither the name of Network Computer, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY NETWORK COMPUTER, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Device driver for the Silicon Integrated Systems SiS 900 and
34 * SiS 7016 10/100 PCI Ethernet controllers.
35 *
36 * Written by Jason R. Thorpe for Network Computer, Inc.
37 */
38
39 #include "opt_inet.h"
40 #include "opt_ns.h"
41 #include "bpfilter.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/mbuf.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
53 #include <sys/queue.h>
54
55 #include <vm/vm.h> /* for PAGE_SIZE */
56
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_ether.h>
61
62 #if NBPFILTER > 0
63 #include <net/bpf.h>
64 #endif
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/if_inarp.h>
69 #endif
70
71 #ifdef NS
72 #include <netns/ns.h>
73 #include <netns/ns_if.h>
74 #endif
75
76 #include <machine/bus.h>
77 #include <machine/intr.h>
78 #include <machine/endian.h>
79
80 #include <dev/mii/miivar.h>
81
82 #include <dev/pci/pcireg.h>
83 #include <dev/pci/pcivar.h>
84 #include <dev/pci/pcidevs.h>
85
86 #include <dev/pci/if_sipreg.h>
87
88 /*
89 * Devices supported by this driver.
90 */
91 const struct sip_product {
92 pci_vendor_id_t sip_vendor;
93 pci_product_id_t sip_product;
94 const char *sip_name;
95 } sip_products[] = {
96 { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_900,
97 "SiS 900 10/100 Ethernet" },
98 { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_7016,
99 "SiS 7016 10/100 Ethernet" },
100
101 { 0, 0,
102 NULL },
103 };
104
105 /*
106 * Transmit descriptor list size. This is arbitrary, but allocate
107 * enough descriptors for 64 pending transmissions, and 16 segments
108 * per packet. This MUST work out to a power of 2.
109 */
110 #define SIP_NTXSEGS 16
111
112 #define SIP_TXQUEUELEN 64
113 #define SIP_NTXDESC (SIP_TXQUEUELEN * SIP_NTXSEGS)
114 #define SIP_NTXDESC_MASK (SIP_NTXDESC - 1)
115 #define SIP_NEXTTX(x) (((x) + 1) & SIP_NTXDESC_MASK)
116
117 /*
118 * Receive descriptor list size. We have one Rx buffer per incoming
119 * packet, so this logic is a little simpler.
120 */
121 #define SIP_NRXDESC 64
122 #define SIP_NRXDESC_MASK (SIP_NRXDESC - 1)
123 #define SIP_NEXTRX(x) (((x) + 1) & SIP_NRXDESC_MASK)
124
125 /*
126 * Control structures are DMA'd to the SiS900 chip. We allocate them in
127 * a single clump that maps to a single DMA segment to make several things
128 * easier.
129 */
130 struct sip_control_data {
131 /*
132 * The transmit descriptors.
133 */
134 struct sip_desc scd_txdescs[SIP_NTXDESC];
135
136 /*
137 * The receive descriptors.
138 */
139 struct sip_desc scd_rxdescs[SIP_NRXDESC];
140 };
141
142 #define SIP_CDOFF(x) offsetof(struct sip_control_data, x)
143 #define SIP_CDTXOFF(x) SIP_CDOFF(scd_txdescs[(x)])
144 #define SIP_CDRXOFF(x) SIP_CDOFF(scd_rxdescs[(x)])
145
146 /*
147 * Software state for transmit jobs.
148 */
149 struct sip_txsoft {
150 struct mbuf *txs_mbuf; /* head of our mbuf chain */
151 bus_dmamap_t txs_dmamap; /* our DMA map */
152 int txs_firstdesc; /* first descriptor in packet */
153 int txs_lastdesc; /* last descriptor in packet */
154 SIMPLEQ_ENTRY(sip_txsoft) txs_q;
155 };
156
157 SIMPLEQ_HEAD(sip_txsq, sip_txsoft);
158
159 /*
160 * Software state for receive jobs.
161 */
162 struct sip_rxsoft {
163 struct mbuf *rxs_mbuf; /* head of our mbuf chain */
164 bus_dmamap_t rxs_dmamap; /* our DMA map */
165 };
166
167 /*
168 * Software state per device.
169 */
170 struct sip_softc {
171 struct device sc_dev; /* generic device information */
172 bus_space_tag_t sc_st; /* bus space tag */
173 bus_space_handle_t sc_sh; /* bus space handle */
174 bus_dma_tag_t sc_dmat; /* bus DMA tag */
175 struct ethercom sc_ethercom; /* ethernet common data */
176 void *sc_sdhook; /* shutdown hook */
177 pci_product_id_t sc_model; /* which model are we? */
178
179 void *sc_ih; /* interrupt cookie */
180
181 struct mii_data sc_mii; /* MII/media information */
182
183 struct callout sc_tick_ch; /* tick callout */
184
185 bus_dmamap_t sc_cddmamap; /* control data DMA map */
186 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
187
188 /*
189 * Software state for transmit and receive descriptors.
190 */
191 struct sip_txsoft sc_txsoft[SIP_TXQUEUELEN];
192 struct sip_rxsoft sc_rxsoft[SIP_NRXDESC];
193
194 /*
195 * Control data structures.
196 */
197 struct sip_control_data *sc_control_data;
198 #define sc_txdescs sc_control_data->scd_txdescs
199 #define sc_rxdescs sc_control_data->scd_rxdescs
200
201 u_int32_t sc_txcfg; /* prototype TXCFG register */
202 u_int32_t sc_rxcfg; /* prototype RXCFG register */
203 u_int32_t sc_imr; /* prototype IMR register */
204 u_int32_t sc_rfcr; /* prototype RFCR register */
205
206 u_int32_t sc_tx_fill_thresh; /* transmit fill threshold */
207 u_int32_t sc_tx_drain_thresh; /* transmit drain threshold */
208
209 u_int32_t sc_rx_drain_thresh; /* receive drain threshold */
210
211 int sc_flags; /* misc. flags; see below */
212
213 int sc_txfree; /* number of free Tx descriptors */
214 int sc_txnext; /* next ready Tx descriptor */
215
216 struct sip_txsq sc_txfreeq; /* free Tx descsofts */
217 struct sip_txsq sc_txdirtyq; /* dirty Tx descsofts */
218
219 int sc_rxptr; /* next ready Rx descriptor/descsoft */
220 };
221
222 /* sc_flags */
223 #define SIPF_PAUSED 0x00000001 /* paused (802.3x flow control) */
224
225 #define SIP_CDTXADDR(sc, x) ((sc)->sc_cddma + SIP_CDTXOFF((x)))
226 #define SIP_CDRXADDR(sc, x) ((sc)->sc_cddma + SIP_CDRXOFF((x)))
227
228 #define SIP_CDTXSYNC(sc, x, n, ops) \
229 do { \
230 int __x, __n; \
231 \
232 __x = (x); \
233 __n = (n); \
234 \
235 /* If it will wrap around, sync to the end of the ring. */ \
236 if ((__x + __n) > SIP_NTXDESC) { \
237 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
238 SIP_CDTXOFF(__x), sizeof(struct sip_desc) * \
239 (SIP_NTXDESC - __x), (ops)); \
240 __n -= (SIP_NTXDESC - __x); \
241 __x = 0; \
242 } \
243 \
244 /* Now sync whatever is left. */ \
245 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
246 SIP_CDTXOFF(__x), sizeof(struct sip_desc) * __n, (ops)); \
247 } while (0)
248
249 #define SIP_CDRXSYNC(sc, x, ops) \
250 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
251 SIP_CDRXOFF((x)), sizeof(struct sip_desc), (ops))
252
253 /*
254 * Note we rely on MCLBYTES being a power of two below.
255 */
256 #define SIP_INIT_RXDESC(sc, x) \
257 do { \
258 struct sip_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \
259 struct sip_desc *__sipd = &(sc)->sc_rxdescs[(x)]; \
260 \
261 __sipd->sipd_link = htole32(SIP_CDRXADDR((sc), SIP_NEXTRX((x)))); \
262 __sipd->sipd_bufptr = htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr); \
263 __sipd->sipd_cmdsts = htole32(CMDSTS_INTR | \
264 ((MCLBYTES - 1) & CMDSTS_SIZE_MASK)); \
265 SIP_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
266 } while (0)
267
268 #define SIP_TIMEOUT 1000
269
270 void sip_start __P((struct ifnet *));
271 void sip_watchdog __P((struct ifnet *));
272 int sip_ioctl __P((struct ifnet *, u_long, caddr_t));
273
274 void sip_shutdown __P((void *));
275
276 void sip_reset __P((struct sip_softc *));
277 int sip_init __P((struct sip_softc *));
278 void sip_stop __P((struct sip_softc *, int));
279 void sip_rxdrain __P((struct sip_softc *));
280 int sip_add_rxbuf __P((struct sip_softc *, int));
281 void sip_read_eeprom __P((struct sip_softc *, int, int, u_int16_t *));
282 void sip_set_filter __P((struct sip_softc *));
283 void sip_tick __P((void *));
284
285 int sip_intr __P((void *));
286 void sip_txintr __P((struct sip_softc *));
287 void sip_rxintr __P((struct sip_softc *));
288
289 int sip_mii_readreg __P((struct device *, int, int));
290 void sip_mii_writereg __P((struct device *, int, int, int));
291 void sip_mii_statchg __P((struct device *));
292
293 int sip_mediachange __P((struct ifnet *));
294 void sip_mediastatus __P((struct ifnet *, struct ifmediareq *));
295
296 int sip_match __P((struct device *, struct cfdata *, void *));
297 void sip_attach __P((struct device *, struct device *, void *));
298
299 int sip_copy_small = 0;
300
301 struct cfattach sip_ca = {
302 sizeof(struct sip_softc), sip_match, sip_attach,
303 };
304
305 const struct sip_product *sip_lookup __P((const struct pci_attach_args *));
306
307 const struct sip_product *
308 sip_lookup(pa)
309 const struct pci_attach_args *pa;
310 {
311 const struct sip_product *sip;
312
313 for (sip = sip_products; sip->sip_name != NULL; sip++) {
314 if (PCI_VENDOR(pa->pa_id) == sip->sip_vendor &&
315 PCI_PRODUCT(pa->pa_id) == sip->sip_product)
316 return (sip);
317 }
318 return (NULL);
319 }
320
321 int
322 sip_match(parent, cf, aux)
323 struct device *parent;
324 struct cfdata *cf;
325 void *aux;
326 {
327 struct pci_attach_args *pa = aux;
328
329 if (sip_lookup(pa) != NULL)
330 return (1);
331
332 return (0);
333 }
334
335 void
336 sip_attach(parent, self, aux)
337 struct device *parent, *self;
338 void *aux;
339 {
340 struct sip_softc *sc = (struct sip_softc *) self;
341 struct pci_attach_args *pa = aux;
342 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
343 pci_chipset_tag_t pc = pa->pa_pc;
344 pci_intr_handle_t ih;
345 const char *intrstr = NULL;
346 bus_space_tag_t iot, memt;
347 bus_space_handle_t ioh, memh;
348 bus_dma_segment_t seg;
349 int ioh_valid, memh_valid;
350 int i, rseg, error;
351 const struct sip_product *sip;
352 pcireg_t pmode;
353 u_int16_t myea[ETHER_ADDR_LEN / 2];
354 u_int8_t enaddr[ETHER_ADDR_LEN];
355 int pmreg;
356
357 callout_init(&sc->sc_tick_ch);
358
359 sip = sip_lookup(pa);
360 if (sip == NULL) {
361 printf("\n");
362 panic("sip_attach: impossible");
363 }
364
365 printf(": %s\n", sip->sip_name);
366
367 sc->sc_model = PCI_PRODUCT(pa->pa_id);
368
369 /*
370 * Map the device.
371 */
372 ioh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGIOA,
373 PCI_MAPREG_TYPE_IO, 0,
374 &iot, &ioh, NULL, NULL) == 0);
375 memh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGMA,
376 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
377 &memt, &memh, NULL, NULL) == 0);
378
379 if (memh_valid) {
380 sc->sc_st = memt;
381 sc->sc_sh = memh;
382 } else if (ioh_valid) {
383 sc->sc_st = iot;
384 sc->sc_sh = ioh;
385 } else {
386 printf("%s: unable to map device registers\n",
387 sc->sc_dev.dv_xname);
388 return;
389 }
390
391 sc->sc_dmat = pa->pa_dmat;
392
393 /* Enable bus mastering. */
394 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
395 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
396 PCI_COMMAND_MASTER_ENABLE);
397
398 /* Get it out of power save mode if needed. */
399 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
400 pmode = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
401 if (pmode == 3) {
402 /*
403 * The card has lost all configuration data in
404 * this state, so punt.
405 */
406 printf("%s: unable to wake up from power state D3\n",
407 sc->sc_dev.dv_xname);
408 return;
409 }
410 if (pmode != 0) {
411 printf("%s: waking up from power state D%d\n",
412 sc->sc_dev.dv_xname, pmode);
413 pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
414 }
415 }
416
417 /*
418 * Map and establish our interrupt.
419 */
420 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
421 pa->pa_intrline, &ih)) {
422 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
423 return;
424 }
425 intrstr = pci_intr_string(pc, ih);
426 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, sip_intr, sc);
427 if (sc->sc_ih == NULL) {
428 printf("%s: unable to establish interrupt",
429 sc->sc_dev.dv_xname);
430 if (intrstr != NULL)
431 printf(" at %s", intrstr);
432 printf("\n");
433 return;
434 }
435 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
436
437 SIMPLEQ_INIT(&sc->sc_txfreeq);
438 SIMPLEQ_INIT(&sc->sc_txdirtyq);
439
440 /*
441 * Allocate the control data structures, and create and load the
442 * DMA map for it.
443 */
444 if ((error = bus_dmamem_alloc(sc->sc_dmat,
445 sizeof(struct sip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
446 0)) != 0) {
447 printf("%s: unable to allocate control data, error = %d\n",
448 sc->sc_dev.dv_xname, error);
449 goto fail_0;
450 }
451
452 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
453 sizeof(struct sip_control_data), (caddr_t *)&sc->sc_control_data,
454 BUS_DMA_COHERENT)) != 0) {
455 printf("%s: unable to map control data, error = %d\n",
456 sc->sc_dev.dv_xname, error);
457 goto fail_1;
458 }
459
460 if ((error = bus_dmamap_create(sc->sc_dmat,
461 sizeof(struct sip_control_data), 1,
462 sizeof(struct sip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
463 printf("%s: unable to create control data DMA map, "
464 "error = %d\n", sc->sc_dev.dv_xname, error);
465 goto fail_2;
466 }
467
468 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
469 sc->sc_control_data, sizeof(struct sip_control_data), NULL,
470 0)) != 0) {
471 printf("%s: unable to load control data DMA map, error = %d\n",
472 sc->sc_dev.dv_xname, error);
473 goto fail_3;
474 }
475
476 /*
477 * Create the transmit buffer DMA maps.
478 */
479 for (i = 0; i < SIP_TXQUEUELEN; i++) {
480 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
481 SIP_NTXSEGS, MCLBYTES, 0, 0,
482 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
483 printf("%s: unable to create tx DMA map %d, "
484 "error = %d\n", sc->sc_dev.dv_xname, i, error);
485 goto fail_4;
486 }
487 }
488
489 /*
490 * Create the receive buffer DMA maps.
491 */
492 for (i = 0; i < SIP_NRXDESC; i++) {
493 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
494 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
495 printf("%s: unable to create rx DMA map %d, "
496 "error = %d\n", sc->sc_dev.dv_xname, i, error);
497 goto fail_5;
498 }
499 sc->sc_rxsoft[i].rxs_mbuf = NULL;
500 }
501
502 /*
503 * Reset the chip to a known state.
504 */
505 sip_reset(sc);
506
507 /*
508 * Read the Ethernet address from the EEPROM.
509 */
510 sip_read_eeprom(sc, SIP_EEPROM_ETHERNET_ID0 >> 1,
511 sizeof(myea) / sizeof(myea[0]), myea);
512
513 enaddr[0] = myea[0] & 0xff;
514 enaddr[1] = myea[0] >> 8;
515 enaddr[2] = myea[1] & 0xff;
516 enaddr[3] = myea[1] >> 8;
517 enaddr[4] = myea[2] & 0xff;
518 enaddr[5] = myea[2] >> 8;
519
520 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
521 ether_sprintf(enaddr));
522
523 /*
524 * Initialize our media structures and probe the MII.
525 */
526 sc->sc_mii.mii_ifp = ifp;
527 sc->sc_mii.mii_readreg = sip_mii_readreg;
528 sc->sc_mii.mii_writereg = sip_mii_writereg;
529 sc->sc_mii.mii_statchg = sip_mii_statchg;
530 ifmedia_init(&sc->sc_mii.mii_media, 0, sip_mediachange,
531 sip_mediastatus);
532 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
533 MII_OFFSET_ANY, 0);
534 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
535 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
536 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
537 } else
538 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
539
540 ifp = &sc->sc_ethercom.ec_if;
541 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
542 ifp->if_softc = sc;
543 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
544 ifp->if_ioctl = sip_ioctl;
545 ifp->if_start = sip_start;
546 ifp->if_watchdog = sip_watchdog;
547
548 /*
549 * Attach the interface.
550 */
551 if_attach(ifp);
552 ether_ifattach(ifp, enaddr);
553 #if NBPFILTER > 0
554 bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
555 sizeof(struct ether_header));
556 #endif
557
558 /*
559 * Make sure the interface is shutdown during reboot.
560 */
561 sc->sc_sdhook = shutdownhook_establish(sip_shutdown, sc);
562 if (sc->sc_sdhook == NULL)
563 printf("%s: WARNING: unable to establish shutdown hook\n",
564 sc->sc_dev.dv_xname);
565 return;
566
567 /*
568 * Free any resources we've allocated during the failed attach
569 * attempt. Do this in reverse order and fall through.
570 */
571 fail_5:
572 for (i = 0; i < SIP_NRXDESC; i++) {
573 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
574 bus_dmamap_destroy(sc->sc_dmat,
575 sc->sc_rxsoft[i].rxs_dmamap);
576 }
577 fail_4:
578 for (i = 0; i < SIP_TXQUEUELEN; i++) {
579 if (sc->sc_txsoft[i].txs_dmamap != NULL)
580 bus_dmamap_destroy(sc->sc_dmat,
581 sc->sc_txsoft[i].txs_dmamap);
582 }
583 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
584 fail_3:
585 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
586 fail_2:
587 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
588 sizeof(struct sip_control_data));
589 fail_1:
590 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
591 fail_0:
592 return;
593 }
594
595 /*
596 * sip_shutdown:
597 *
598 * Make sure the interface is stopped at reboot time.
599 */
600 void
601 sip_shutdown(arg)
602 void *arg;
603 {
604 struct sip_softc *sc = arg;
605
606 sip_stop(sc, 1);
607 }
608
609 /*
610 * sip_start: [ifnet interface function]
611 *
612 * Start packet transmission on the interface.
613 */
614 void
615 sip_start(ifp)
616 struct ifnet *ifp;
617 {
618 struct sip_softc *sc = ifp->if_softc;
619 struct mbuf *m0, *m;
620 struct sip_txsoft *txs;
621 bus_dmamap_t dmamap;
622 int error, firsttx, nexttx, lasttx, ofree, seg;
623
624 /*
625 * If we've been told to pause, don't transmit any more packets.
626 */
627 if (sc->sc_flags & SIPF_PAUSED)
628 ifp->if_flags |= IFF_OACTIVE;
629
630 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
631 return;
632
633 /*
634 * Remember the previous number of free descriptors and
635 * the first descriptor we'll use.
636 */
637 ofree = sc->sc_txfree;
638 firsttx = sc->sc_txnext;
639
640 /*
641 * Loop through the send queue, setting up transmit descriptors
642 * until we drain the queue, or use up all available transmit
643 * descriptors.
644 */
645 while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
646 sc->sc_txfree != 0) {
647 /*
648 * Grab a packet off the queue.
649 */
650 IF_DEQUEUE(&ifp->if_snd, m0);
651 if (m0 == NULL)
652 break;
653
654 dmamap = txs->txs_dmamap;
655
656 /*
657 * Load the DMA map. If this fails, the packet either
658 * didn't fit in the alloted number of segments, or we
659 * were short on resources. In this case, we'll copy
660 * and try again.
661 */
662 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
663 BUS_DMA_NOWAIT) != 0) {
664 MGETHDR(m, M_DONTWAIT, MT_DATA);
665 if (m == NULL) {
666 printf("%s: unable to allocate Tx mbuf\n",
667 sc->sc_dev.dv_xname);
668 IF_PREPEND(&ifp->if_snd, m0);
669 break;
670 }
671 if (m0->m_pkthdr.len > MHLEN) {
672 MCLGET(m, M_DONTWAIT);
673 if ((m->m_flags & M_EXT) == 0) {
674 printf("%s: unable to allocate Tx "
675 "cluster\n", sc->sc_dev.dv_xname);
676 m_freem(m);
677 IF_PREPEND(&ifp->if_snd, m0);
678 break;
679 }
680 }
681 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
682 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
683 m_freem(m0);
684 m0 = m;
685 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
686 m0, BUS_DMA_NOWAIT);
687 if (error) {
688 printf("%s: unable to load Tx buffer, "
689 "error = %d\n", sc->sc_dev.dv_xname, error);
690 IF_PREPEND(&ifp->if_snd, m0);
691 break;
692 }
693 }
694
695 /*
696 * Ensure we have enough descriptors free to describe
697 * the packet.
698 */
699 if (dmamap->dm_nsegs > sc->sc_txfree) {
700 /*
701 * Not enough free descriptors to transmit this
702 * packet. We haven't committed anything yet,
703 * so just unload the DMA map, put the packet
704 * back on the queue, and punt. Notify the upper
705 * layer that there are not more slots left.
706 *
707 * XXX We could allocate an mbuf and copy, but
708 * XXX is it worth it?
709 */
710 ifp->if_flags |= IFF_OACTIVE;
711 bus_dmamap_unload(sc->sc_dmat, dmamap);
712 IF_PREPEND(&ifp->if_snd, m0);
713 break;
714 }
715
716 /*
717 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
718 */
719
720 /* Sync the DMA map. */
721 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
722 BUS_DMASYNC_PREWRITE);
723
724 /*
725 * Initialize the transmit descriptors.
726 */
727 for (nexttx = sc->sc_txnext, seg = 0;
728 seg < dmamap->dm_nsegs;
729 seg++, nexttx = SIP_NEXTTX(nexttx)) {
730 /*
731 * If this is the first descriptor we're
732 * enqueueing, don't set the OWN bit just
733 * yet. That could cause a race condition.
734 * We'll do it below.
735 */
736 sc->sc_txdescs[nexttx].sipd_bufptr =
737 htole32(dmamap->dm_segs[seg].ds_addr);
738 sc->sc_txdescs[nexttx].sipd_cmdsts =
739 htole32((nexttx == firsttx ? 0 : CMDSTS_OWN) |
740 CMDSTS_MORE | dmamap->dm_segs[seg].ds_len);
741 lasttx = nexttx;
742 }
743
744 /* Clear the MORE bit on the last segment. */
745 sc->sc_txdescs[lasttx].sipd_cmdsts &= htole32(~CMDSTS_MORE);
746
747 /* Sync the descriptors we're using. */
748 SIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
749 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
750
751 /*
752 * Store a pointer to the packet so we can free it later,
753 * and remember what txdirty will be once the packet is
754 * done.
755 */
756 txs->txs_mbuf = m0;
757 txs->txs_firstdesc = sc->sc_txnext;
758 txs->txs_lastdesc = lasttx;
759
760 /* Advance the tx pointer. */
761 sc->sc_txfree -= dmamap->dm_nsegs;
762 sc->sc_txnext = nexttx;
763
764 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
765 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
766
767 #if NBPFILTER > 0
768 /*
769 * Pass the packet to any BPF listeners.
770 */
771 if (ifp->if_bpf)
772 bpf_mtap(ifp->if_bpf, m0);
773 #endif /* NBPFILTER > 0 */
774 }
775
776 if (txs == NULL || sc->sc_txfree == 0) {
777 /* No more slots left; notify upper layer. */
778 ifp->if_flags |= IFF_OACTIVE;
779 }
780
781 if (sc->sc_txfree != ofree) {
782 /*
783 * Cause a descriptor interrupt to happen on the
784 * last packet we enqueued.
785 */
786 sc->sc_txdescs[lasttx].sipd_cmdsts |= htole32(CMDSTS_INTR);
787 SIP_CDTXSYNC(sc, lasttx, 1,
788 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
789
790 /*
791 * The entire packet chain is set up. Give the
792 * first descrptor to the chip now.
793 */
794 sc->sc_txdescs[firsttx].sipd_cmdsts |= htole32(CMDSTS_OWN);
795 SIP_CDTXSYNC(sc, firsttx, 1,
796 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
797
798 /* Start the transmit process. */
799 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CR) &
800 CR_TXE) == 0) {
801 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXDP,
802 SIP_CDTXADDR(sc, firsttx));
803 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
804 }
805
806 /* Set a watchdog timer in case the chip flakes out. */
807 ifp->if_timer = 5;
808 }
809 }
810
811 /*
812 * sip_watchdog: [ifnet interface function]
813 *
814 * Watchdog timer handler.
815 */
816 void
817 sip_watchdog(ifp)
818 struct ifnet *ifp;
819 {
820 struct sip_softc *sc = ifp->if_softc;
821
822 /*
823 * The chip seems to ignore the CMDSTS_INTR bit sometimes!
824 * If we get a timeout, try and sweep up transmit descriptors.
825 * If we manage to sweep them all up, ignore the lack of
826 * interrupt.
827 */
828 sip_txintr(sc);
829
830 if (sc->sc_txfree != SIP_NTXDESC) {
831 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
832 ifp->if_oerrors++;
833
834 /* Reset the interface. */
835 (void) sip_init(sc);
836 } else if (ifp->if_flags & IFF_DEBUG)
837 printf("%s: recovered from device timeout\n",
838 sc->sc_dev.dv_xname);
839
840 /* Try to get more packets going. */
841 sip_start(ifp);
842 }
843
844 /*
845 * sip_ioctl: [ifnet interface function]
846 *
847 * Handle control requests from the operator.
848 */
849 int
850 sip_ioctl(ifp, cmd, data)
851 struct ifnet *ifp;
852 u_long cmd;
853 caddr_t data;
854 {
855 struct sip_softc *sc = ifp->if_softc;
856 struct ifreq *ifr = (struct ifreq *)data;
857 struct ifaddr *ifa = (struct ifaddr *)data;
858 int s, error = 0;
859
860 s = splnet();
861
862 switch (cmd) {
863 case SIOCSIFADDR:
864 ifp->if_flags |= IFF_UP;
865
866 switch (ifa->ifa_addr->sa_family) {
867 #ifdef INET
868 case AF_INET:
869 if ((error = sip_init(sc)) != 0)
870 break;
871 arp_ifinit(ifp, ifa);
872 break;
873 #endif /* INET */
874 #ifdef NS
875 case AF_NS:
876 {
877 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
878
879 if (ns_nullhost(*ina))
880 ina->x_host = *(union ns_host *)
881 LLADDR(ifp->if_sadl);
882 else
883 memcpy(LLADDR(ifp->if_sadl),
884 ina->x_host.c_host, ifp->if_addrlen);
885 error = sip_init(sc);
886 break;
887 }
888 #endif /* NS */
889 default:
890 error = sip_init(sc);
891 break;
892 }
893 break;
894
895 case SIOCSIFMTU:
896 if (ifr->ifr_mtu > ETHERMTU)
897 error = EINVAL;
898 else
899 ifp->if_mtu = ifr->ifr_mtu;
900 break;
901
902 case SIOCSIFFLAGS:
903 if ((ifp->if_flags & IFF_UP) == 0 &&
904 (ifp->if_flags & IFF_RUNNING) != 0) {
905 /*
906 * If interface is marked down and it is running, then
907 * stop it.
908 */
909 sip_stop(sc, 1);
910 } else if ((ifp->if_flags & IFF_UP) != 0 &&
911 (ifp->if_flags & IFF_RUNNING) == 0) {
912 /*
913 * If interfase it marked up and it is stopped, then
914 * start it.
915 */
916 error = sip_init(sc);
917 } else if ((ifp->if_flags & IFF_UP) != 0) {
918 /*
919 * Reset the interface to pick up changes in any other
920 * flags that affect the hardware state.
921 */
922 error = sip_init(sc);
923 }
924 break;
925
926 case SIOCADDMULTI:
927 case SIOCDELMULTI:
928 error = (cmd == SIOCADDMULTI) ?
929 ether_addmulti(ifr, &sc->sc_ethercom) :
930 ether_delmulti(ifr, &sc->sc_ethercom);
931
932 if (error == ENETRESET) {
933 /*
934 * Multicast list has changed; set the hardware filter
935 * accordingly.
936 */
937 sip_set_filter(sc);
938 error = 0;
939 }
940 break;
941
942 case SIOCSIFMEDIA:
943 case SIOCGIFMEDIA:
944 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
945 break;
946
947 default:
948 error = EINVAL;
949 break;
950 }
951
952 /* Try to get more packets going. */
953 sip_start(ifp);
954
955 splx(s);
956 return (error);
957 }
958
959 /*
960 * sip_intr:
961 *
962 * Interrupt service routine.
963 */
964 int
965 sip_intr(arg)
966 void *arg;
967 {
968 struct sip_softc *sc = arg;
969 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
970 u_int32_t isr;
971 int handled = 0;
972
973 for (;;) {
974 /* Reading clears interrupt. */
975 isr = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ISR);
976 if ((isr & sc->sc_imr) == 0)
977 break;
978
979 handled = 1;
980
981 if (isr & (ISR_RXORN|ISR_RXIDLE|ISR_RXDESC)) {
982 /* Grab any new packets. */
983 sip_rxintr(sc);
984
985 if (isr & ISR_RXORN) {
986 printf("%s: receive FIFO overrun\n",
987 sc->sc_dev.dv_xname);
988
989 /* XXX adjust rx_drain_thresh? */
990 }
991
992 if (isr & ISR_RXIDLE) {
993 printf("%s: receive ring overrun\n",
994 sc->sc_dev.dv_xname);
995
996 /* Get the receive process going again. */
997 bus_space_write_4(sc->sc_st, sc->sc_sh,
998 SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
999 bus_space_write_4(sc->sc_st, sc->sc_sh,
1000 SIP_CR, CR_RXE);
1001 }
1002 }
1003
1004 if (isr & (ISR_TXURN|ISR_TXDESC)) {
1005 /* Sweep up transmit descriptors. */
1006 sip_txintr(sc);
1007
1008 if (isr & ISR_TXURN) {
1009 u_int32_t thresh;
1010
1011 printf("%s: transmit FIFO underrun",
1012 sc->sc_dev.dv_xname);
1013
1014 thresh = sc->sc_tx_drain_thresh + 1;
1015 if (thresh <= TXCFG_DRTH &&
1016 (thresh * 32) <= (SIP_TXFIFO_SIZE -
1017 (sc->sc_tx_fill_thresh * 32))) {
1018 printf("; increasing Tx drain "
1019 "threshold to %u bytes\n",
1020 thresh * 32);
1021 sc->sc_tx_drain_thresh = thresh;
1022 (void) sip_init(sc);
1023 } else {
1024 (void) sip_init(sc);
1025 printf("\n");
1026 }
1027 }
1028 }
1029
1030 if (sc->sc_imr & (ISR_PAUSE_END|ISR_PAUSE_ST)) {
1031 if (isr & ISR_PAUSE_ST) {
1032 sc->sc_flags |= SIPF_PAUSED;
1033 ifp->if_flags |= IFF_OACTIVE;
1034 }
1035 if (isr & ISR_PAUSE_END) {
1036 sc->sc_flags &= ~SIPF_PAUSED;
1037 ifp->if_flags &= ~IFF_OACTIVE;
1038 }
1039 }
1040
1041 if (isr & ISR_HIBERR) {
1042 #define PRINTERR(bit, str) \
1043 if (isr & (bit)) \
1044 printf("%s: %s\n", sc->sc_dev.dv_xname, str)
1045 PRINTERR(ISR_DPERR, "parity error");
1046 PRINTERR(ISR_SSERR, "system error");
1047 PRINTERR(ISR_RMABT, "master abort");
1048 PRINTERR(ISR_RTABT, "target abort");
1049 PRINTERR(ISR_RXSOVR, "receive status FIFO overrun");
1050 (void) sip_init(sc);
1051 #undef PRINTERR
1052 }
1053 }
1054
1055 /* Try to get more packets going. */
1056 sip_start(ifp);
1057
1058 return (handled);
1059 }
1060
1061 /*
1062 * sip_txintr:
1063 *
1064 * Helper; handle transmit interrupts.
1065 */
1066 void
1067 sip_txintr(sc)
1068 struct sip_softc *sc;
1069 {
1070 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1071 struct sip_txsoft *txs;
1072 u_int32_t cmdsts;
1073
1074 if ((sc->sc_flags & SIPF_PAUSED) == 0)
1075 ifp->if_flags &= ~IFF_OACTIVE;
1076
1077 /*
1078 * Go through our Tx list and free mbufs for those
1079 * frames which have been transmitted.
1080 */
1081 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1082 SIP_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
1083 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1084
1085 cmdsts = le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
1086 if (cmdsts & CMDSTS_OWN)
1087 break;
1088
1089 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1090
1091 sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
1092
1093 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1094 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1095 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1096 m_freem(txs->txs_mbuf);
1097 txs->txs_mbuf = NULL;
1098
1099 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1100
1101 /*
1102 * Check for errors and collisions.
1103 */
1104 if (cmdsts &
1105 (CMDSTS_Tx_TXA|CMDSTS_Tx_TFU|CMDSTS_Tx_ED|CMDSTS_Tx_EC)) {
1106 if (ifp->if_flags & IFF_DEBUG) {
1107 if (CMDSTS_Tx_ED)
1108 printf("%s: excessive deferral\n",
1109 sc->sc_dev.dv_xname);
1110 if (CMDSTS_Tx_EC) {
1111 printf("%s: excessive collisions\n",
1112 sc->sc_dev.dv_xname);
1113 ifp->if_collisions += 16;
1114 }
1115 }
1116 } else {
1117 /* Packet was transmitted successfully. */
1118 ifp->if_opackets++;
1119 ifp->if_collisions += CMDSTS_COLLISIONS(cmdsts);
1120 }
1121 }
1122
1123 /*
1124 * If there are no more pending transmissions, cancel the watchdog
1125 * timer.
1126 */
1127 if (txs == NULL)
1128 ifp->if_timer = 0;
1129 }
1130
1131 /*
1132 * sip_rxintr:
1133 *
1134 * Helper; handle receive interrupts.
1135 */
1136 void
1137 sip_rxintr(sc)
1138 struct sip_softc *sc;
1139 {
1140 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1141 struct ether_header *eh;
1142 struct sip_rxsoft *rxs;
1143 struct mbuf *m;
1144 u_int32_t cmdsts;
1145 int i, len;
1146
1147 for (i = sc->sc_rxptr;; i = SIP_NEXTRX(i)) {
1148 rxs = &sc->sc_rxsoft[i];
1149
1150 SIP_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1151
1152 cmdsts = le32toh(sc->sc_rxdescs[i].sipd_cmdsts);
1153
1154 /*
1155 * NOTE: OWN is set if owned by _consumer_. We're the
1156 * consumer of the receive ring, so if the bit is clear,
1157 * we have processed all of the packets.
1158 */
1159 if ((cmdsts & CMDSTS_OWN) == 0) {
1160 /*
1161 * We have processed all of the receive buffers.
1162 */
1163 break;
1164 }
1165
1166 /*
1167 * If any collisions were seen on the wire, count one.
1168 */
1169 if (cmdsts & CMDSTS_Rx_COL)
1170 ifp->if_collisions++;
1171
1172 /*
1173 * If an error occurred, update stats, clear the status
1174 * word, and leave the packet buffer in place. It will
1175 * simply be reused the next time the ring comes around.
1176 */
1177 if (cmdsts & (CMDSTS_Rx_RXA|CMDSTS_Rx_LONG|CMDSTS_Rx_RUNT|
1178 CMDSTS_Rx_ISE|CMDSTS_Rx_CRCE|CMDSTS_Rx_FAE)) {
1179 ifp->if_ierrors++;
1180 if ((cmdsts & CMDSTS_Rx_RXA) != 0 &&
1181 (cmdsts & CMDSTS_Rx_RXO) == 0) {
1182 /* Receive overrun handled elsewhere. */
1183 printf("%s: receive descriptor error\n",
1184 sc->sc_dev.dv_xname);
1185 }
1186 #define PRINTERR(bit, str) \
1187 if (cmdsts & (bit)) \
1188 printf("%s: %s\n", sc->sc_dev.dv_xname, str)
1189 PRINTERR(CMDSTS_Rx_LONG, "packet too long");
1190 PRINTERR(CMDSTS_Rx_RUNT, "runt packet");
1191 PRINTERR(CMDSTS_Rx_ISE, "invalid symbol error");
1192 PRINTERR(CMDSTS_Rx_CRCE, "CRC error");
1193 PRINTERR(CMDSTS_Rx_FAE, "frame alignment error");
1194 #undef PRINTERR
1195 SIP_INIT_RXDESC(sc, i);
1196 continue;
1197 }
1198
1199 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1200 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1201
1202 /*
1203 * No errors; receive the packet. Note, the SiS 900
1204 * includes the CRC with every packet; trim it.
1205 */
1206 len = CMDSTS_SIZE(cmdsts) - ETHER_CRC_LEN;
1207
1208 #ifdef __NO_STRICT_ALIGNMENT
1209 /*
1210 * If the packet is small enough to fit in a
1211 * single header mbuf, allocate one and copy
1212 * the data into it. This greatly reduces
1213 * memory consumption when we receive lots
1214 * of small packets.
1215 *
1216 * Otherwise, we add a new buffer to the receive
1217 * chain. If this fails, we drop the packet and
1218 * recycle the old buffer.
1219 */
1220 if (sip_copy_small != 0 && len <= MHLEN) {
1221 MGETHDR(m, M_DONTWAIT, MT_DATA);
1222 if (m == NULL)
1223 goto dropit;
1224 memcpy(mtod(m, caddr_t),
1225 mtod(rxs->rxs_mbuf, caddr_t), len);
1226 SIP_INIT_RXDESC(sc, i);
1227 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1228 rxs->rxs_dmamap->dm_mapsize,
1229 BUS_DMASYNC_PREREAD);
1230 } else {
1231 m = rxs->rxs_mbuf;
1232 if (sip_add_rxbuf(sc, i) != 0) {
1233 dropit:
1234 ifp->if_ierrors++;
1235 SIP_INIT_RXDESC(sc, i);
1236 bus_dmamap_sync(sc->sc_dmat,
1237 rxs->rxs_dmamap, 0,
1238 rxs->rxs_dmamap->dm_mapsize,
1239 BUS_DMASYNC_PREREAD);
1240 continue;
1241 }
1242 }
1243 #else
1244 /*
1245 * The SiS 900's receive buffers must be 4-byte aligned.
1246 * But this means that the data after the Ethernet header
1247 * is misaligned. We must allocate a new buffer and
1248 * copy the data, shifted forward 2 bytes.
1249 */
1250 MGETHDR(m, M_DONTWAIT, MT_DATA);
1251 if (m == NULL) {
1252 dropit:
1253 ifp->if_ierrors++;
1254 SIP_INIT_RXDESC(sc, i);
1255 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1256 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1257 continue;
1258 }
1259 if (len > (MHLEN - 2)) {
1260 MCLGET(m, M_DONTWAIT);
1261 if ((m->m_flags & M_EXT) == 0) {
1262 m_freem(m);
1263 goto dropit;
1264 }
1265 }
1266 m->m_data += 2;
1267
1268 /*
1269 * Note that we use clusters for incoming frames, so the
1270 * buffer is virtually contiguous.
1271 */
1272 memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
1273
1274 /* Allow the receive descriptor to continue using its mbuf. */
1275 SIP_INIT_RXDESC(sc, i);
1276 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1277 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1278 #endif /* __NO_STRICT_ALIGNMENT */
1279
1280 ifp->if_ipackets++;
1281 eh = mtod(m, struct ether_header *);
1282 m->m_pkthdr.rcvif = ifp;
1283 m->m_pkthdr.len = m->m_len = len;
1284
1285 #if NBPFILTER > 0
1286 /*
1287 * Pass this up to any BPF listeners, but only
1288 * pass if up the stack if it's for us.
1289 */
1290 if (ifp->if_bpf) {
1291 bpf_mtap(ifp->if_bpf, m);
1292 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1293 (cmdsts & CMDSTS_Rx_DEST) == CMDSTS_Rx_DEST_REJ) {
1294 m_freem(m);
1295 continue;
1296 }
1297 }
1298 #endif /* NBPFILTER > 0 */
1299
1300 /* Pass it on. */
1301 (*ifp->if_input)(ifp, m);
1302 }
1303
1304 /* Update the receive pointer. */
1305 sc->sc_rxptr = i;
1306 }
1307
1308 /*
1309 * sip_tick:
1310 *
1311 * One second timer, used to tick the MII.
1312 */
1313 void
1314 sip_tick(arg)
1315 void *arg;
1316 {
1317 struct sip_softc *sc = arg;
1318 int s;
1319
1320 s = splnet();
1321 mii_tick(&sc->sc_mii);
1322 splx(s);
1323
1324 callout_reset(&sc->sc_tick_ch, hz, sip_tick, sc);
1325 }
1326
1327 /*
1328 * sip_reset:
1329 *
1330 * Perform a soft reset on the SiS 900.
1331 */
1332 void
1333 sip_reset(sc)
1334 struct sip_softc *sc;
1335 {
1336 bus_space_tag_t st = sc->sc_st;
1337 bus_space_handle_t sh = sc->sc_sh;
1338 int i;
1339
1340 bus_space_write_4(st, sh, SIP_CR, CR_RST);
1341
1342 for (i = 0; i < SIP_TIMEOUT; i++) {
1343 if ((bus_space_read_4(st, sh, SIP_CR) & CR_RST) == 0)
1344 break;
1345 delay(2);
1346 }
1347
1348 if (i == SIP_TIMEOUT)
1349 printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1350
1351 delay(1000);
1352 }
1353
1354 /*
1355 * sip_init:
1356 *
1357 * Initialize the interface. Must be called at splnet().
1358 */
1359 int
1360 sip_init(sc)
1361 struct sip_softc *sc;
1362 {
1363 bus_space_tag_t st = sc->sc_st;
1364 bus_space_handle_t sh = sc->sc_sh;
1365 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1366 struct sip_txsoft *txs;
1367 struct sip_rxsoft *rxs;
1368 struct sip_desc *sipd;
1369 u_int32_t cfg;
1370 int i, error = 0;
1371
1372 /*
1373 * Cancel any pending I/O.
1374 */
1375 sip_stop(sc, 0);
1376
1377 /*
1378 * Reset the chip to a known state.
1379 */
1380 sip_reset(sc);
1381
1382 /*
1383 * Initialize the transmit descriptor ring.
1384 */
1385 for (i = 0; i < SIP_NTXDESC; i++) {
1386 sipd = &sc->sc_txdescs[i];
1387 memset(sipd, 0, sizeof(struct sip_desc));
1388 sipd->sipd_link = htole32(SIP_CDTXADDR(sc, SIP_NEXTTX(i)));
1389 }
1390 SIP_CDTXSYNC(sc, 0, SIP_NTXDESC,
1391 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1392 sc->sc_txfree = SIP_NTXDESC;
1393 sc->sc_txnext = 0;
1394
1395 /*
1396 * Initialize the transmit job descriptors.
1397 */
1398 SIMPLEQ_INIT(&sc->sc_txfreeq);
1399 SIMPLEQ_INIT(&sc->sc_txdirtyq);
1400 for (i = 0; i < SIP_TXQUEUELEN; i++) {
1401 txs = &sc->sc_txsoft[i];
1402 txs->txs_mbuf = NULL;
1403 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1404 }
1405
1406 /*
1407 * Initialize the receive descriptor and receive job
1408 * descriptor rings.
1409 */
1410 for (i = 0; i < SIP_NRXDESC; i++) {
1411 rxs = &sc->sc_rxsoft[i];
1412 if (rxs->rxs_mbuf == NULL) {
1413 if ((error = sip_add_rxbuf(sc, i)) != 0) {
1414 printf("%s: unable to allocate or map rx "
1415 "buffer %d, error = %d\n",
1416 sc->sc_dev.dv_xname, i, error);
1417 /*
1418 * XXX Should attempt to run with fewer receive
1419 * XXX buffers instead of just failing.
1420 */
1421 sip_rxdrain(sc);
1422 goto out;
1423 }
1424 }
1425 }
1426 sc->sc_rxptr = 0;
1427
1428 /*
1429 * Initialize the configuration register: aggressive PCI
1430 * bus request algorithm, default backoff, default OW timer,
1431 * default parity error detection.
1432 */
1433 cfg = 0;
1434 #if BYTE_ORDER == BIG_ENDIAN
1435 /*
1436 * ...descriptors in big-endian mode.
1437 */
1438 #if 0
1439 /* "Big endian mode" does not work properly. */
1440 cfg |= CFG_BEM;
1441 #endif
1442 #endif
1443 bus_space_write_4(st, sh, SIP_CFG, cfg);
1444
1445 /*
1446 * Initialize the transmit fill and drain thresholds if
1447 * we have never done so.
1448 */
1449 if (sc->sc_tx_fill_thresh == 0) {
1450 /*
1451 * XXX This value should be tuned. This is the
1452 * minimum (32 bytes), and we may be able to
1453 * improve performance by increasing it.
1454 */
1455 sc->sc_tx_fill_thresh = 1;
1456 }
1457 if (sc->sc_tx_drain_thresh == 0) {
1458 /*
1459 * Start at a drain threshold of 128 bytes. We will
1460 * increase it if a DMA underrun occurs.
1461 *
1462 * XXX The minimum value of this variable should be
1463 * tuned. We may be able to improve performance
1464 * by starting with a lower value. That, however,
1465 * may trash the first few outgoing packets if the
1466 * PCI bus is saturated.
1467 */
1468 sc->sc_tx_drain_thresh = 4;
1469 }
1470
1471 /*
1472 * Initialize the prototype TXCFG register.
1473 */
1474 sc->sc_txcfg = TXCFG_ATP | TXCFG_MXDMA_512 |
1475 (sc->sc_tx_fill_thresh << TXCFG_FLTH_SHIFT) |
1476 sc->sc_tx_drain_thresh;
1477 bus_space_write_4(st, sh, SIP_TXCFG, sc->sc_txcfg);
1478
1479 /*
1480 * Initialize the receive drain threshold if we have never
1481 * done so.
1482 */
1483 if (sc->sc_rx_drain_thresh == 0) {
1484 /*
1485 * XXX This value should be tuned. This is set to the
1486 * maximum of 248 bytes, and we may be able to improve
1487 * performance by decreasing it (although we should never
1488 * set this value lower than 2; 14 bytes are required to
1489 * filter the packet).
1490 */
1491 sc->sc_rx_drain_thresh = RXCFG_DRTH >> RXCFG_DRTH_SHIFT;
1492 }
1493
1494 /*
1495 * Initialize the prototype RXCFG register.
1496 */
1497 sc->sc_rxcfg = RXCFG_MXDMA_512 |
1498 (sc->sc_rx_drain_thresh << RXCFG_DRTH_SHIFT);
1499 bus_space_write_4(st, sh, SIP_RXCFG, sc->sc_rxcfg);
1500
1501 /* Set up the receive filter. */
1502 sip_set_filter(sc);
1503
1504 /*
1505 * Give the transmit and receive rings to the chip.
1506 */
1507 bus_space_write_4(st, sh, SIP_TXDP, SIP_CDTXADDR(sc, sc->sc_txnext));
1508 bus_space_write_4(st, sh, SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
1509
1510 /*
1511 * Initialize the interrupt mask.
1512 */
1513 sc->sc_imr = ISR_DPERR|ISR_SSERR|ISR_RMABT|ISR_RTABT|ISR_RXSOVR|
1514 ISR_TXURN|ISR_TXDESC|ISR_RXORN|ISR_RXIDLE|ISR_RXDESC;
1515 bus_space_write_4(st, sh, SIP_IMR, sc->sc_imr);
1516
1517 /*
1518 * Set the current media. Do this after initializing the prototype
1519 * IMR, since sip_mii_statchg() modifies the IMR for 802.3x flow
1520 * control.
1521 */
1522 mii_mediachg(&sc->sc_mii);
1523
1524 /*
1525 * Enable interrupts.
1526 */
1527 bus_space_write_4(st, sh, SIP_IER, IER_IE);
1528
1529 /*
1530 * Start the transmit and receive processes.
1531 */
1532 bus_space_write_4(st, sh, SIP_CR, CR_RXE | CR_TXE);
1533
1534 /*
1535 * Start the one second MII clock.
1536 */
1537 callout_reset(&sc->sc_tick_ch, hz, sip_tick, sc);
1538
1539 /*
1540 * ...all done!
1541 */
1542 ifp->if_flags |= IFF_RUNNING;
1543 ifp->if_flags &= ~IFF_OACTIVE;
1544
1545 out:
1546 if (error)
1547 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1548 return (error);
1549 }
1550
1551 /*
1552 * sip_drain:
1553 *
1554 * Drain the receive queue.
1555 */
1556 void
1557 sip_rxdrain(sc)
1558 struct sip_softc *sc;
1559 {
1560 struct sip_rxsoft *rxs;
1561 int i;
1562
1563 for (i = 0; i < SIP_NRXDESC; i++) {
1564 rxs = &sc->sc_rxsoft[i];
1565 if (rxs->rxs_mbuf != NULL) {
1566 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1567 m_freem(rxs->rxs_mbuf);
1568 rxs->rxs_mbuf = NULL;
1569 }
1570 }
1571 }
1572
1573 /*
1574 * sip_stop:
1575 *
1576 * Stop transmission on the interface.
1577 */
1578 void
1579 sip_stop(sc, drain)
1580 struct sip_softc *sc;
1581 {
1582 bus_space_tag_t st = sc->sc_st;
1583 bus_space_handle_t sh = sc->sc_sh;
1584 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1585 struct sip_txsoft *txs;
1586 u_int32_t cmdsts = 0; /* DEBUG */
1587
1588 /*
1589 * Stop the one second clock.
1590 */
1591 callout_stop(&sc->sc_tick_ch);
1592
1593 /* Down the MII. */
1594 mii_down(&sc->sc_mii);
1595
1596 /*
1597 * Disable interrupts.
1598 */
1599 bus_space_write_4(st, sh, SIP_IER, 0);
1600
1601 /*
1602 * Stop receiver and transmitter.
1603 */
1604 bus_space_write_4(st, sh, SIP_CR, CR_RXD | CR_TXD);
1605
1606 /*
1607 * Release any queued transmit buffers.
1608 */
1609 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1610 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
1611 SIMPLEQ_NEXT(txs, txs_q) == NULL &&
1612 (le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts) &
1613 CMDSTS_INTR) == 0)
1614 printf("%s: sip_stop: last descriptor does not "
1615 "have INTR bit set\n", sc->sc_dev.dv_xname);
1616 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1617 #ifdef DIAGNOSTIC
1618 if (txs->txs_mbuf == NULL) {
1619 printf("%s: dirty txsoft with no mbuf chain\n",
1620 sc->sc_dev.dv_xname);
1621 panic("sip_stop");
1622 }
1623 #endif
1624 cmdsts |= /* DEBUG */
1625 le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
1626 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1627 m_freem(txs->txs_mbuf);
1628 txs->txs_mbuf = NULL;
1629 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1630 }
1631
1632 if (drain) {
1633 /*
1634 * Release the receive buffers.
1635 */
1636 sip_rxdrain(sc);
1637 }
1638
1639 /*
1640 * Mark the interface down and cancel the watchdog timer.
1641 */
1642 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1643 ifp->if_timer = 0;
1644
1645 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
1646 (cmdsts & CMDSTS_INTR) == 0 && sc->sc_txfree != SIP_NTXDESC)
1647 printf("%s: sip_stop: no INTR bits set in dirty tx "
1648 "descriptors\n", sc->sc_dev.dv_xname);
1649 }
1650
1651 /*
1652 * sip_read_eeprom:
1653 *
1654 * Read data from the serial EEPROM.
1655 */
1656 void
1657 sip_read_eeprom(sc, word, wordcnt, data)
1658 struct sip_softc *sc;
1659 int word, wordcnt;
1660 u_int16_t *data;
1661 {
1662 bus_space_tag_t st = sc->sc_st;
1663 bus_space_handle_t sh = sc->sc_sh;
1664 u_int16_t reg;
1665 int i, x;
1666
1667 for (i = 0; i < wordcnt; i++) {
1668 /* Send CHIP SELECT. */
1669 reg = EROMAR_EECS;
1670 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1671
1672 /* Shift in the READ opcode. */
1673 for (x = 3; x > 0; x--) {
1674 if (SIP_EEPROM_OPC_READ & (1 << (x - 1)))
1675 reg |= EROMAR_EEDI;
1676 else
1677 reg &= ~EROMAR_EEDI;
1678 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1679 bus_space_write_4(st, sh, SIP_EROMAR,
1680 reg | EROMAR_EESK);
1681 delay(4);
1682 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1683 delay(4);
1684 }
1685
1686 /* Shift in address. */
1687 for (x = 6; x > 0; x--) {
1688 if ((word + i) & (1 << (x - 1)))
1689 reg |= EROMAR_EEDI;
1690 else
1691 reg &= ~EROMAR_EEDI;
1692 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1693 bus_space_write_4(st, sh, SIP_EROMAR,
1694 reg | EROMAR_EESK);
1695 delay(4);
1696 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1697 delay(4);
1698 }
1699
1700 /* Shift out data. */
1701 reg = EROMAR_EECS;
1702 data[i] = 0;
1703 for (x = 16; x > 0; x--) {
1704 bus_space_write_4(st, sh, SIP_EROMAR,
1705 reg | EROMAR_EESK);
1706 delay(4);
1707 if (bus_space_read_4(st, sh, SIP_EROMAR) & EROMAR_EEDO)
1708 data[i] |= (1 << (x - 1));
1709 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1710 delay(4);
1711 }
1712
1713 /* Clear CHIP SELECT. */
1714 bus_space_write_4(st, sh, SIP_EROMAR, 0);
1715 delay(4);
1716 }
1717 }
1718
1719 /*
1720 * sip_add_rxbuf:
1721 *
1722 * Add a receive buffer to the indicated descriptor.
1723 */
1724 int
1725 sip_add_rxbuf(sc, idx)
1726 struct sip_softc *sc;
1727 int idx;
1728 {
1729 struct sip_rxsoft *rxs = &sc->sc_rxsoft[idx];
1730 struct mbuf *m;
1731 int error;
1732
1733 MGETHDR(m, M_DONTWAIT, MT_DATA);
1734 if (m == NULL)
1735 return (ENOBUFS);
1736
1737 MCLGET(m, M_DONTWAIT);
1738 if ((m->m_flags & M_EXT) == 0) {
1739 m_freem(m);
1740 return (ENOBUFS);
1741 }
1742
1743 if (rxs->rxs_mbuf != NULL)
1744 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1745
1746 rxs->rxs_mbuf = m;
1747
1748 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1749 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1750 if (error) {
1751 printf("%s: can't load rx DMA map %d, error = %d\n",
1752 sc->sc_dev.dv_xname, idx, error);
1753 panic("sip_add_rxbuf"); /* XXX */
1754 }
1755
1756 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1757 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1758
1759 SIP_INIT_RXDESC(sc, idx);
1760
1761 return (0);
1762 }
1763
1764 /*
1765 * sip_set_filter:
1766 *
1767 * Set up the receive filter.
1768 */
1769 void
1770 sip_set_filter(sc)
1771 struct sip_softc *sc;
1772 {
1773 bus_space_tag_t st = sc->sc_st;
1774 bus_space_handle_t sh = sc->sc_sh;
1775 struct ethercom *ec = &sc->sc_ethercom;
1776 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1777 struct ether_multi *enm;
1778 u_int8_t *cp;
1779 struct ether_multistep step;
1780 u_int32_t crc, mchash[8];
1781
1782 /*
1783 * Initialize the prototype RFCR.
1784 */
1785 sc->sc_rfcr = RFCR_RFEN;
1786 if (ifp->if_flags & IFF_BROADCAST)
1787 sc->sc_rfcr |= RFCR_AAB;
1788 if (ifp->if_flags & IFF_PROMISC) {
1789 sc->sc_rfcr |= RFCR_AAP;
1790 goto allmulti;
1791 }
1792
1793 /*
1794 * Set up the multicast address filter by passing all multicast
1795 * addresses through a CRC generator, and then using the high-order
1796 * 6 bits as an index into the 128 bit multicast hash table (only
1797 * the lower 16 bits of each 32 bit multicast hash register are
1798 * valid). The high order bits select the register, while the
1799 * rest of the bits select the bit within the register.
1800 */
1801
1802 memset(mchash, 0, sizeof(mchash));
1803
1804 ETHER_FIRST_MULTI(step, ec, enm);
1805 while (enm != NULL) {
1806 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1807 /*
1808 * We must listen to a range of multicast addresses.
1809 * For now, just accept all multicasts, rather than
1810 * trying to set only those filter bits needed to match
1811 * the range. (At this time, the only use of address
1812 * ranges is for IP multicast routing, for which the
1813 * range is big enough to require all bits set.)
1814 */
1815 goto allmulti;
1816 }
1817
1818 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1819
1820 /* Just want the 7 most significant bits. */
1821 crc >>= 25;
1822
1823 /* Set the corresponding bit in the hash table. */
1824 mchash[crc >> 4] |= 1 << (crc & 0xf);
1825
1826 ETHER_NEXT_MULTI(step, enm);
1827 }
1828
1829 ifp->if_flags &= ~IFF_ALLMULTI;
1830 goto setit;
1831
1832 allmulti:
1833 ifp->if_flags |= IFF_ALLMULTI;
1834 sc->sc_rfcr |= RFCR_AAM;
1835
1836 setit:
1837 #define FILTER_EMIT(addr, data) \
1838 bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
1839 delay(1); \
1840 bus_space_write_4(st, sh, SIP_RFDR, (data)); \
1841 delay(1)
1842
1843 /*
1844 * Disable receive filter, and program the node address.
1845 */
1846 cp = LLADDR(ifp->if_sadl);
1847 FILTER_EMIT(RFCR_RFADDR_NODE0, (cp[1] << 8) | cp[0]);
1848 FILTER_EMIT(RFCR_RFADDR_NODE2, (cp[3] << 8) | cp[2]);
1849 FILTER_EMIT(RFCR_RFADDR_NODE4, (cp[5] << 8) | cp[4]);
1850
1851 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
1852 /*
1853 * Program the multicast hash table.
1854 */
1855 FILTER_EMIT(RFCR_RFADDR_MC0, mchash[0]);
1856 FILTER_EMIT(RFCR_RFADDR_MC1, mchash[1]);
1857 FILTER_EMIT(RFCR_RFADDR_MC2, mchash[2]);
1858 FILTER_EMIT(RFCR_RFADDR_MC3, mchash[3]);
1859 FILTER_EMIT(RFCR_RFADDR_MC4, mchash[4]);
1860 FILTER_EMIT(RFCR_RFADDR_MC5, mchash[5]);
1861 FILTER_EMIT(RFCR_RFADDR_MC6, mchash[6]);
1862 FILTER_EMIT(RFCR_RFADDR_MC7, mchash[7]);
1863 }
1864 #undef FILTER_EMIT
1865
1866 /*
1867 * Re-enable the receiver filter.
1868 */
1869 bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
1870 }
1871
1872 /*
1873 * sip_mii_readreg: [mii interface function]
1874 *
1875 * Read a PHY register on the MII.
1876 */
1877 int
1878 sip_mii_readreg(self, phy, reg)
1879 struct device *self;
1880 int phy, reg;
1881 {
1882 struct sip_softc *sc = (struct sip_softc *) self;
1883 u_int32_t enphy;
1884
1885 /*
1886 * The SiS 900 has only an internal PHY on the MII. Only allow
1887 * MII address 0.
1888 */
1889 if (sc->sc_model == PCI_PRODUCT_SIS_900 && phy != 0)
1890 return (0);
1891
1892 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
1893 (phy << ENPHY_PHYADDR_SHIFT) | (reg << ENPHY_REGADDR_SHIFT) |
1894 ENPHY_RWCMD | ENPHY_ACCESS);
1895 do {
1896 enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
1897 } while (enphy & ENPHY_ACCESS);
1898 return ((enphy & ENPHY_PHYDATA) >> ENPHY_DATA_SHIFT);
1899 }
1900
1901 /*
1902 * sip_mii_writereg: [mii interface function]
1903 *
1904 * Write a PHY register on the MII.
1905 */
1906 void
1907 sip_mii_writereg(self, phy, reg, val)
1908 struct device *self;
1909 int phy, reg, val;
1910 {
1911 struct sip_softc *sc = (struct sip_softc *) self;
1912 u_int32_t enphy;
1913
1914 /*
1915 * The SiS 900 has only an internal PHY on the MII. Only allow
1916 * MII address 0.
1917 */
1918 if (sc->sc_model == PCI_PRODUCT_SIS_900 && phy != 0)
1919 return;
1920
1921 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
1922 (val << ENPHY_DATA_SHIFT) | (phy << ENPHY_PHYADDR_SHIFT) |
1923 (reg << ENPHY_REGADDR_SHIFT) | ENPHY_ACCESS);
1924 do {
1925 enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
1926 } while (enphy & ENPHY_ACCESS);
1927 }
1928
1929 /*
1930 * sip_mii_statchg: [mii interface function]
1931 *
1932 * Callback from MII layer when media changes.
1933 */
1934 void
1935 sip_mii_statchg(self)
1936 struct device *self;
1937 {
1938 struct sip_softc *sc = (struct sip_softc *) self;
1939 u_int32_t flowctl;
1940
1941 /*
1942 * Update TXCFG for full-duplex operation.
1943 */
1944 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
1945 sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
1946 else
1947 sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
1948
1949 /*
1950 * Update RXCFG for full-duplex or loopback.
1951 */
1952 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
1953 IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
1954 sc->sc_rxcfg |= RXCFG_ATX;
1955 else
1956 sc->sc_rxcfg &= ~RXCFG_ATX;
1957
1958 /*
1959 * Update IMR for use of 802.3x flow control.
1960 */
1961 if ((sc->sc_mii.mii_media_active & IFM_FLOW) != 0) {
1962 sc->sc_imr |= (ISR_PAUSE_END|ISR_PAUSE_ST);
1963 flowctl = FLOWCTL_FLOWEN;
1964 } else {
1965 sc->sc_imr &= ~(ISR_PAUSE_END|ISR_PAUSE_ST);
1966 flowctl = 0;
1967 }
1968
1969 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
1970 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
1971 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_IMR, sc->sc_imr);
1972 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_FLOWCTL, flowctl);
1973 }
1974
1975 /*
1976 * sip_mediastatus: [ifmedia interface function]
1977 *
1978 * Get the current interface media status.
1979 */
1980 void
1981 sip_mediastatus(ifp, ifmr)
1982 struct ifnet *ifp;
1983 struct ifmediareq *ifmr;
1984 {
1985 struct sip_softc *sc = ifp->if_softc;
1986
1987 mii_pollstat(&sc->sc_mii);
1988 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1989 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1990 }
1991
1992 /*
1993 * sip_mediachange: [ifmedia interface function]
1994 *
1995 * Set hardware to newly-selected media.
1996 */
1997 int
1998 sip_mediachange(ifp)
1999 struct ifnet *ifp;
2000 {
2001 struct sip_softc *sc = ifp->if_softc;
2002
2003 if (ifp->if_flags & IFF_UP)
2004 mii_mediachg(&sc->sc_mii);
2005 return (0);
2006 }
2007