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