FreeBSD/Linux Kernel Cross Reference
sys/dev/vr/if_vr.c
1 /*-
2 * Copyright (c) 1997, 1998
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/7.4/sys/dev/vr/if_vr.c 214910 2010-11-07 11:12:30Z marius $");
35
36 /*
37 * VIA Rhine fast ethernet PCI NIC driver
38 *
39 * Supports various network adapters based on the VIA Rhine
40 * and Rhine II PCI controllers, including the D-Link DFE530TX.
41 * Datasheets are available at http://www.via.com.tw.
42 *
43 * Written by Bill Paul <wpaul@ctr.columbia.edu>
44 * Electrical Engineering Department
45 * Columbia University, New York City
46 */
47
48 /*
49 * The VIA Rhine controllers are similar in some respects to the
50 * the DEC tulip chips, except less complicated. The controller
51 * uses an MII bus and an external physical layer interface. The
52 * receiver has a one entry perfect filter and a 64-bit hash table
53 * multicast filter. Transmit and receive descriptors are similar
54 * to the tulip.
55 *
56 * Some Rhine chips has a serious flaw in its transmit DMA mechanism:
57 * transmit buffers must be longword aligned. Unfortunately,
58 * FreeBSD doesn't guarantee that mbufs will be filled in starting
59 * at longword boundaries, so we have to do a buffer copy before
60 * transmission.
61 */
62
63 #ifdef HAVE_KERNEL_OPTION_HEADERS
64 #include "opt_device_polling.h"
65 #endif
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/bus.h>
70 #include <sys/endian.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/module.h>
75 #include <sys/rman.h>
76 #include <sys/socket.h>
77 #include <sys/sockio.h>
78 #include <sys/sysctl.h>
79 #include <sys/taskqueue.h>
80
81 #include <net/bpf.h>
82 #include <net/if.h>
83 #include <net/ethernet.h>
84 #include <net/if_dl.h>
85 #include <net/if_media.h>
86 #include <net/if_types.h>
87 #include <net/if_vlan_var.h>
88
89 #include <dev/mii/mii.h>
90 #include <dev/mii/miivar.h>
91
92 #include <dev/pci/pcireg.h>
93 #include <dev/pci/pcivar.h>
94
95 #include <machine/bus.h>
96
97 #include <dev/vr/if_vrreg.h>
98
99 /* "device miibus" required. See GENERIC if you get errors here. */
100 #include "miibus_if.h"
101
102 MODULE_DEPEND(vr, pci, 1, 1, 1);
103 MODULE_DEPEND(vr, ether, 1, 1, 1);
104 MODULE_DEPEND(vr, miibus, 1, 1, 1);
105
106 /* Define to show Rx/Tx error status. */
107 #undef VR_SHOW_ERRORS
108 #define VR_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
109
110 /*
111 * Various supported device vendors/types, their names & quirks.
112 */
113 #define VR_Q_NEEDALIGN (1<<0)
114 #define VR_Q_CSUM (1<<1)
115 #define VR_Q_CAM (1<<2)
116
117 static struct vr_type {
118 u_int16_t vr_vid;
119 u_int16_t vr_did;
120 int vr_quirks;
121 char *vr_name;
122 } vr_devs[] = {
123 { VIA_VENDORID, VIA_DEVICEID_RHINE,
124 VR_Q_NEEDALIGN,
125 "VIA VT3043 Rhine I 10/100BaseTX" },
126 { VIA_VENDORID, VIA_DEVICEID_RHINE_II,
127 VR_Q_NEEDALIGN,
128 "VIA VT86C100A Rhine II 10/100BaseTX" },
129 { VIA_VENDORID, VIA_DEVICEID_RHINE_II_2,
130 0,
131 "VIA VT6102 Rhine II 10/100BaseTX" },
132 { VIA_VENDORID, VIA_DEVICEID_RHINE_III,
133 0,
134 "VIA VT6105 Rhine III 10/100BaseTX" },
135 { VIA_VENDORID, VIA_DEVICEID_RHINE_III_M,
136 VR_Q_CSUM,
137 "VIA VT6105M Rhine III 10/100BaseTX" },
138 { DELTA_VENDORID, DELTA_DEVICEID_RHINE_II,
139 VR_Q_NEEDALIGN,
140 "Delta Electronics Rhine II 10/100BaseTX" },
141 { ADDTRON_VENDORID, ADDTRON_DEVICEID_RHINE_II,
142 VR_Q_NEEDALIGN,
143 "Addtron Technology Rhine II 10/100BaseTX" },
144 { 0, 0, 0, NULL }
145 };
146
147 static int vr_probe(device_t);
148 static int vr_attach(device_t);
149 static int vr_detach(device_t);
150 static int vr_shutdown(device_t);
151 static int vr_suspend(device_t);
152 static int vr_resume(device_t);
153
154 static void vr_dmamap_cb(void *, bus_dma_segment_t *, int, int);
155 static int vr_dma_alloc(struct vr_softc *);
156 static void vr_dma_free(struct vr_softc *);
157 static __inline void vr_discard_rxbuf(struct vr_rxdesc *);
158 static int vr_newbuf(struct vr_softc *, int);
159
160 #ifndef __NO_STRICT_ALIGNMENT
161 static __inline void vr_fixup_rx(struct mbuf *);
162 #endif
163 static void vr_rxeof(struct vr_softc *);
164 static void vr_txeof(struct vr_softc *);
165 static void vr_tick(void *);
166 static int vr_error(struct vr_softc *, uint16_t);
167 static void vr_tx_underrun(struct vr_softc *);
168 static void vr_intr(void *);
169 static void vr_start(struct ifnet *);
170 static void vr_start_locked(struct ifnet *);
171 static int vr_encap(struct vr_softc *, struct mbuf **);
172 static int vr_ioctl(struct ifnet *, u_long, caddr_t);
173 static void vr_init(void *);
174 static void vr_init_locked(struct vr_softc *);
175 static void vr_tx_start(struct vr_softc *);
176 static void vr_rx_start(struct vr_softc *);
177 static int vr_tx_stop(struct vr_softc *);
178 static int vr_rx_stop(struct vr_softc *);
179 static void vr_stop(struct vr_softc *);
180 static void vr_watchdog(struct vr_softc *);
181 static int vr_ifmedia_upd(struct ifnet *);
182 static void vr_ifmedia_sts(struct ifnet *, struct ifmediareq *);
183
184 static int vr_miibus_readreg(device_t, int, int);
185 static int vr_miibus_writereg(device_t, int, int, int);
186 static void vr_miibus_statchg(device_t);
187
188 static void vr_link_task(void *, int);
189 static void vr_cam_mask(struct vr_softc *, uint32_t, int);
190 static int vr_cam_data(struct vr_softc *, int, int, uint8_t *);
191 static void vr_set_filter(struct vr_softc *);
192 static void vr_reset(const struct vr_softc *);
193 static int vr_tx_ring_init(struct vr_softc *);
194 static int vr_rx_ring_init(struct vr_softc *);
195 static void vr_setwol(struct vr_softc *);
196 static void vr_clrwol(struct vr_softc *);
197 static int vr_sysctl_stats(SYSCTL_HANDLER_ARGS);
198
199 static struct vr_tx_threshold_table {
200 int tx_cfg;
201 int bcr_cfg;
202 int value;
203 } vr_tx_threshold_tables[] = {
204 { VR_TXTHRESH_64BYTES, VR_BCR1_TXTHRESH64BYTES, 64 },
205 { VR_TXTHRESH_128BYTES, VR_BCR1_TXTHRESH128BYTES, 128 },
206 { VR_TXTHRESH_256BYTES, VR_BCR1_TXTHRESH256BYTES, 256 },
207 { VR_TXTHRESH_512BYTES, VR_BCR1_TXTHRESH512BYTES, 512 },
208 { VR_TXTHRESH_1024BYTES, VR_BCR1_TXTHRESH1024BYTES, 1024 },
209 { VR_TXTHRESH_STORENFWD, VR_BCR1_TXTHRESHSTORENFWD, 2048 }
210 };
211
212 static device_method_t vr_methods[] = {
213 /* Device interface */
214 DEVMETHOD(device_probe, vr_probe),
215 DEVMETHOD(device_attach, vr_attach),
216 DEVMETHOD(device_detach, vr_detach),
217 DEVMETHOD(device_shutdown, vr_shutdown),
218 DEVMETHOD(device_suspend, vr_suspend),
219 DEVMETHOD(device_resume, vr_resume),
220
221 /* bus interface */
222 DEVMETHOD(bus_print_child, bus_generic_print_child),
223 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
224
225 /* MII interface */
226 DEVMETHOD(miibus_readreg, vr_miibus_readreg),
227 DEVMETHOD(miibus_writereg, vr_miibus_writereg),
228 DEVMETHOD(miibus_statchg, vr_miibus_statchg),
229 DEVMETHOD(miibus_linkchg, vr_miibus_statchg),
230
231 { NULL, NULL }
232 };
233
234 static driver_t vr_driver = {
235 "vr",
236 vr_methods,
237 sizeof(struct vr_softc)
238 };
239
240 static devclass_t vr_devclass;
241
242 DRIVER_MODULE(vr, pci, vr_driver, vr_devclass, 0, 0);
243 DRIVER_MODULE(miibus, vr, miibus_driver, miibus_devclass, 0, 0);
244
245 static int
246 vr_miibus_readreg(device_t dev, int phy, int reg)
247 {
248 struct vr_softc *sc;
249 int i;
250
251 sc = device_get_softc(dev);
252
253 /* Set the register address. */
254 CSR_WRITE_1(sc, VR_MIIADDR, reg);
255 VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_READ_ENB);
256
257 for (i = 0; i < VR_MII_TIMEOUT; i++) {
258 DELAY(1);
259 if ((CSR_READ_1(sc, VR_MIICMD) & VR_MIICMD_READ_ENB) == 0)
260 break;
261 }
262 if (i == VR_MII_TIMEOUT)
263 device_printf(sc->vr_dev, "phy read timeout %d:%d\n", phy, reg);
264
265 return (CSR_READ_2(sc, VR_MIIDATA));
266 }
267
268 static int
269 vr_miibus_writereg(device_t dev, int phy, int reg, int data)
270 {
271 struct vr_softc *sc;
272 int i;
273
274 sc = device_get_softc(dev);
275
276 /* Set the register address and data to write. */
277 CSR_WRITE_1(sc, VR_MIIADDR, reg);
278 CSR_WRITE_2(sc, VR_MIIDATA, data);
279 VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_WRITE_ENB);
280
281 for (i = 0; i < VR_MII_TIMEOUT; i++) {
282 DELAY(1);
283 if ((CSR_READ_1(sc, VR_MIICMD) & VR_MIICMD_WRITE_ENB) == 0)
284 break;
285 }
286 if (i == VR_MII_TIMEOUT)
287 device_printf(sc->vr_dev, "phy write timeout %d:%d\n", phy,
288 reg);
289
290 return (0);
291 }
292
293 static void
294 vr_miibus_statchg(device_t dev)
295 {
296 struct vr_softc *sc;
297
298 sc = device_get_softc(dev);
299 taskqueue_enqueue(taskqueue_swi, &sc->vr_link_task);
300 }
301
302 /*
303 * In order to fiddle with the
304 * 'full-duplex' and '100Mbps' bits in the netconfig register, we
305 * first have to put the transmit and/or receive logic in the idle state.
306 */
307 static void
308 vr_link_task(void *arg, int pending)
309 {
310 struct vr_softc *sc;
311 struct mii_data *mii;
312 struct ifnet *ifp;
313 int lfdx, mfdx;
314 uint8_t cr0, cr1, fc;
315
316 sc = (struct vr_softc *)arg;
317
318 VR_LOCK(sc);
319 mii = device_get_softc(sc->vr_miibus);
320 ifp = sc->vr_ifp;
321 if (mii == NULL || ifp == NULL ||
322 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
323 VR_UNLOCK(sc);
324 return;
325 }
326
327 if (mii->mii_media_status & IFM_ACTIVE) {
328 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
329 sc->vr_link = 1;
330 } else
331 sc->vr_link = 0;
332
333 if (sc->vr_link != 0) {
334 cr0 = CSR_READ_1(sc, VR_CR0);
335 cr1 = CSR_READ_1(sc, VR_CR1);
336 mfdx = (cr1 & VR_CR1_FULLDUPLEX) != 0;
337 lfdx = (IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0;
338 if (mfdx != lfdx) {
339 if ((cr0 & (VR_CR0_TX_ON | VR_CR0_RX_ON)) != 0) {
340 if (vr_tx_stop(sc) != 0 ||
341 vr_rx_stop(sc) != 0) {
342 device_printf(sc->vr_dev,
343 "%s: Tx/Rx shutdown error -- "
344 "resetting\n", __func__);
345 sc->vr_flags |= VR_F_RESTART;
346 VR_UNLOCK(sc);
347 return;
348 }
349 }
350 if (lfdx)
351 cr1 |= VR_CR1_FULLDUPLEX;
352 else
353 cr1 &= ~VR_CR1_FULLDUPLEX;
354 CSR_WRITE_1(sc, VR_CR1, cr1);
355 }
356 fc = 0;
357 #ifdef notyet
358 /* Configure flow-control. */
359 if (sc->vr_revid >= REV_ID_VT6105_A0) {
360 fc = CSR_READ_1(sc, VR_FLOWCR1);
361 fc &= ~(VR_FLOWCR1_TXPAUSE | VR_FLOWCR1_RXPAUSE);
362 if ((IFM_OPTIONS(mii->mii_media_active) &
363 IFM_ETH_RXPAUSE) != 0)
364 fc |= VR_FLOWCR1_RXPAUSE;
365 if ((IFM_OPTIONS(mii->mii_media_active) &
366 IFM_ETH_TXPAUSE) != 0)
367 fc |= VR_FLOWCR1_TXPAUSE;
368 CSR_WRITE_1(sc, VR_FLOWCR1, fc);
369 } else if (sc->vr_revid >= REV_ID_VT6102_A) {
370 /* No Tx puase capability available for Rhine II. */
371 fc = CSR_READ_1(sc, VR_MISC_CR0);
372 fc &= ~VR_MISCCR0_RXPAUSE;
373 if ((IFM_OPTIONS(mii->mii_media_active) &
374 IFM_ETH_RXPAUSE) != 0)
375 fc |= VR_MISCCR0_RXPAUSE;
376 CSR_WRITE_1(sc, VR_MISC_CR0, fc);
377 }
378 #endif
379 vr_rx_start(sc);
380 vr_tx_start(sc);
381 } else {
382 if (vr_tx_stop(sc) != 0 || vr_rx_stop(sc) != 0) {
383 device_printf(sc->vr_dev,
384 "%s: Tx/Rx shutdown error -- resetting\n",
385 __func__);
386 sc->vr_flags |= VR_F_RESTART;
387 VR_UNLOCK(sc);
388 return;
389 }
390 }
391 VR_UNLOCK(sc);
392 }
393
394
395 static void
396 vr_cam_mask(struct vr_softc *sc, uint32_t mask, int type)
397 {
398
399 if (type == VR_MCAST_CAM)
400 CSR_WRITE_1(sc, VR_CAMCTL, VR_CAMCTL_ENA | VR_CAMCTL_MCAST);
401 else
402 CSR_WRITE_1(sc, VR_CAMCTL, VR_CAMCTL_ENA | VR_CAMCTL_VLAN);
403 CSR_WRITE_4(sc, VR_CAMMASK, mask);
404 CSR_WRITE_1(sc, VR_CAMCTL, 0);
405 }
406
407 static int
408 vr_cam_data(struct vr_softc *sc, int type, int idx, uint8_t *mac)
409 {
410 int i;
411
412 if (type == VR_MCAST_CAM) {
413 if (idx < 0 || idx >= VR_CAM_MCAST_CNT || mac == NULL)
414 return (EINVAL);
415 CSR_WRITE_1(sc, VR_CAMCTL, VR_CAMCTL_ENA | VR_CAMCTL_MCAST);
416 } else
417 CSR_WRITE_1(sc, VR_CAMCTL, VR_CAMCTL_ENA | VR_CAMCTL_VLAN);
418
419 /* Set CAM entry address. */
420 CSR_WRITE_1(sc, VR_CAMADDR, idx);
421 /* Set CAM entry data. */
422 if (type == VR_MCAST_CAM) {
423 for (i = 0; i < ETHER_ADDR_LEN; i++)
424 CSR_WRITE_1(sc, VR_MCAM0 + i, mac[i]);
425 } else {
426 CSR_WRITE_1(sc, VR_VCAM0, mac[0]);
427 CSR_WRITE_1(sc, VR_VCAM1, mac[1]);
428 }
429 DELAY(10);
430 /* Write CAM and wait for self-clear of VR_CAMCTL_WRITE bit. */
431 CSR_WRITE_1(sc, VR_CAMCTL, VR_CAMCTL_ENA | VR_CAMCTL_WRITE);
432 for (i = 0; i < VR_TIMEOUT; i++) {
433 DELAY(1);
434 if ((CSR_READ_1(sc, VR_CAMCTL) & VR_CAMCTL_WRITE) == 0)
435 break;
436 }
437
438 if (i == VR_TIMEOUT)
439 device_printf(sc->vr_dev, "%s: setting CAM filter timeout!\n",
440 __func__);
441 CSR_WRITE_1(sc, VR_CAMCTL, 0);
442
443 return (i == VR_TIMEOUT ? ETIMEDOUT : 0);
444 }
445
446 /*
447 * Program the 64-bit multicast hash filter.
448 */
449 static void
450 vr_set_filter(struct vr_softc *sc)
451 {
452 struct ifnet *ifp;
453 int h;
454 uint32_t hashes[2] = { 0, 0 };
455 struct ifmultiaddr *ifma;
456 uint8_t rxfilt;
457 int error, mcnt;
458 uint32_t cam_mask;
459
460 VR_LOCK_ASSERT(sc);
461
462 ifp = sc->vr_ifp;
463 rxfilt = CSR_READ_1(sc, VR_RXCFG);
464 rxfilt &= ~(VR_RXCFG_RX_PROMISC | VR_RXCFG_RX_BROAD |
465 VR_RXCFG_RX_MULTI);
466 if (ifp->if_flags & IFF_BROADCAST)
467 rxfilt |= VR_RXCFG_RX_BROAD;
468 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
469 rxfilt |= VR_RXCFG_RX_MULTI;
470 if (ifp->if_flags & IFF_PROMISC)
471 rxfilt |= VR_RXCFG_RX_PROMISC;
472 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
473 CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
474 CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
475 return;
476 }
477
478 /* Now program new ones. */
479 error = 0;
480 mcnt = 0;
481 IF_ADDR_LOCK(ifp);
482 if ((sc->vr_quirks & VR_Q_CAM) != 0) {
483 /*
484 * For hardwares that have CAM capability, use
485 * 32 entries multicast perfect filter.
486 */
487 cam_mask = 0;
488 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
489 if (ifma->ifma_addr->sa_family != AF_LINK)
490 continue;
491 error = vr_cam_data(sc, VR_MCAST_CAM, mcnt,
492 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
493 if (error != 0) {
494 cam_mask = 0;
495 break;
496 }
497 cam_mask |= 1 << mcnt;
498 mcnt++;
499 }
500 vr_cam_mask(sc, VR_MCAST_CAM, cam_mask);
501 }
502
503 if ((sc->vr_quirks & VR_Q_CAM) == 0 || error != 0) {
504 /*
505 * If there are too many multicast addresses or
506 * setting multicast CAM filter failed, use hash
507 * table based filtering.
508 */
509 mcnt = 0;
510 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
511 if (ifma->ifma_addr->sa_family != AF_LINK)
512 continue;
513 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
514 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
515 if (h < 32)
516 hashes[0] |= (1 << h);
517 else
518 hashes[1] |= (1 << (h - 32));
519 mcnt++;
520 }
521 }
522 IF_ADDR_UNLOCK(ifp);
523
524 if (mcnt > 0)
525 rxfilt |= VR_RXCFG_RX_MULTI;
526
527 CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
528 CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
529 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
530 }
531
532 static void
533 vr_reset(const struct vr_softc *sc)
534 {
535 int i;
536
537 /*VR_LOCK_ASSERT(sc);*/ /* XXX: Called during attach w/o lock. */
538
539 CSR_WRITE_1(sc, VR_CR1, VR_CR1_RESET);
540 if (sc->vr_revid < REV_ID_VT6102_A) {
541 /* VT86C100A needs more delay after reset. */
542 DELAY(100);
543 }
544 for (i = 0; i < VR_TIMEOUT; i++) {
545 DELAY(10);
546 if (!(CSR_READ_1(sc, VR_CR1) & VR_CR1_RESET))
547 break;
548 }
549 if (i == VR_TIMEOUT) {
550 if (sc->vr_revid < REV_ID_VT6102_A)
551 device_printf(sc->vr_dev, "reset never completed!\n");
552 else {
553 /* Use newer force reset command. */
554 device_printf(sc->vr_dev,
555 "Using force reset command.\n");
556 VR_SETBIT(sc, VR_MISC_CR1, VR_MISCCR1_FORSRST);
557 /*
558 * Wait a little while for the chip to get its brains
559 * in order.
560 */
561 DELAY(2000);
562 }
563 }
564
565 }
566
567 /*
568 * Probe for a VIA Rhine chip. Check the PCI vendor and device
569 * IDs against our list and return a match or NULL
570 */
571 static struct vr_type *
572 vr_match(device_t dev)
573 {
574 struct vr_type *t = vr_devs;
575
576 for (t = vr_devs; t->vr_name != NULL; t++)
577 if ((pci_get_vendor(dev) == t->vr_vid) &&
578 (pci_get_device(dev) == t->vr_did))
579 return (t);
580 return (NULL);
581 }
582
583 /*
584 * Probe for a VIA Rhine chip. Check the PCI vendor and device
585 * IDs against our list and return a device name if we find a match.
586 */
587 static int
588 vr_probe(device_t dev)
589 {
590 struct vr_type *t;
591
592 t = vr_match(dev);
593 if (t != NULL) {
594 device_set_desc(dev, t->vr_name);
595 return (BUS_PROBE_DEFAULT);
596 }
597 return (ENXIO);
598 }
599
600 /*
601 * Attach the interface. Allocate softc structures, do ifmedia
602 * setup and ethernet/BPF attach.
603 */
604 static int
605 vr_attach(device_t dev)
606 {
607 struct vr_softc *sc;
608 struct ifnet *ifp;
609 struct vr_type *t;
610 uint8_t eaddr[ETHER_ADDR_LEN];
611 int error, rid;
612 int i, phy, pmc;
613
614 sc = device_get_softc(dev);
615 sc->vr_dev = dev;
616 t = vr_match(dev);
617 KASSERT(t != NULL, ("Lost if_vr device match"));
618 sc->vr_quirks = t->vr_quirks;
619 device_printf(dev, "Quirks: 0x%x\n", sc->vr_quirks);
620
621 mtx_init(&sc->vr_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
622 MTX_DEF);
623 callout_init_mtx(&sc->vr_stat_callout, &sc->vr_mtx, 0);
624 TASK_INIT(&sc->vr_link_task, 0, vr_link_task, sc);
625 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
626 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
627 OID_AUTO, "stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
628 vr_sysctl_stats, "I", "Statistics");
629
630 error = 0;
631
632 /*
633 * Map control/status registers.
634 */
635 pci_enable_busmaster(dev);
636 sc->vr_revid = pci_get_revid(dev);
637 device_printf(dev, "Revision: 0x%x\n", sc->vr_revid);
638
639 sc->vr_res_id = PCIR_BAR(0);
640 sc->vr_res_type = SYS_RES_IOPORT;
641 sc->vr_res = bus_alloc_resource_any(dev, sc->vr_res_type,
642 &sc->vr_res_id, RF_ACTIVE);
643 if (sc->vr_res == NULL) {
644 device_printf(dev, "couldn't map ports\n");
645 error = ENXIO;
646 goto fail;
647 }
648
649 /* Allocate interrupt. */
650 rid = 0;
651 sc->vr_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
652 RF_SHAREABLE | RF_ACTIVE);
653
654 if (sc->vr_irq == NULL) {
655 device_printf(dev, "couldn't map interrupt\n");
656 error = ENXIO;
657 goto fail;
658 }
659
660 /* Allocate ifnet structure. */
661 ifp = sc->vr_ifp = if_alloc(IFT_ETHER);
662 if (ifp == NULL) {
663 device_printf(dev, "couldn't allocate ifnet structure\n");
664 error = ENOSPC;
665 goto fail;
666 }
667 ifp->if_softc = sc;
668 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
669 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
670 ifp->if_ioctl = vr_ioctl;
671 ifp->if_start = vr_start;
672 ifp->if_init = vr_init;
673 IFQ_SET_MAXLEN(&ifp->if_snd, VR_TX_RING_CNT - 1);
674 ifp->if_snd.ifq_maxlen = VR_TX_RING_CNT - 1;
675 IFQ_SET_READY(&ifp->if_snd);
676
677 /* Configure Tx FIFO threshold. */
678 sc->vr_txthresh = VR_TXTHRESH_MIN;
679 if (sc->vr_revid < REV_ID_VT6105_A0) {
680 /*
681 * Use store and forward mode for Rhine I/II.
682 * Otherwise they produce a lot of Tx underruns and
683 * it would take a while to get working FIFO threshold
684 * value.
685 */
686 sc->vr_txthresh = VR_TXTHRESH_MAX;
687 }
688 if ((sc->vr_quirks & VR_Q_CSUM) != 0) {
689 ifp->if_hwassist = VR_CSUM_FEATURES;
690 ifp->if_capabilities |= IFCAP_HWCSUM;
691 /*
692 * To update checksum field the hardware may need to
693 * store entire frames into FIFO before transmitting.
694 */
695 sc->vr_txthresh = VR_TXTHRESH_MAX;
696 }
697
698 if (sc->vr_revid >= REV_ID_VT6102_A &&
699 pci_find_extcap(dev, PCIY_PMG, &pmc) == 0)
700 ifp->if_capabilities |= IFCAP_WOL_UCAST | IFCAP_WOL_MAGIC;
701
702 /* Rhine supports oversized VLAN frame. */
703 ifp->if_capabilities |= IFCAP_VLAN_MTU;
704 ifp->if_capenable = ifp->if_capabilities;
705 #ifdef DEVICE_POLLING
706 ifp->if_capabilities |= IFCAP_POLLING;
707 #endif
708
709 /*
710 * Windows may put the chip in suspend mode when it
711 * shuts down. Be sure to kick it in the head to wake it
712 * up again.
713 */
714 if (pci_find_extcap(dev, PCIY_PMG, &pmc) == 0)
715 VR_CLRBIT(sc, VR_STICKHW, (VR_STICKHW_DS0|VR_STICKHW_DS1));
716
717 /*
718 * Get station address. The way the Rhine chips work,
719 * you're not allowed to directly access the EEPROM once
720 * they've been programmed a special way. Consequently,
721 * we need to read the node address from the PAR0 and PAR1
722 * registers.
723 * Reloading EEPROM also overwrites VR_CFGA, VR_CFGB,
724 * VR_CFGC and VR_CFGD such that memory mapped IO configured
725 * by driver is reset to default state.
726 */
727 VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
728 for (i = VR_TIMEOUT; i > 0; i--) {
729 DELAY(1);
730 if ((CSR_READ_1(sc, VR_EECSR) & VR_EECSR_LOAD) == 0)
731 break;
732 }
733 if (i == 0)
734 device_printf(dev, "Reloading EEPROM timeout!\n");
735 for (i = 0; i < ETHER_ADDR_LEN; i++)
736 eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
737
738 /* Reset the adapter. */
739 vr_reset(sc);
740 /* Ack intr & disable further interrupts. */
741 CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
742 CSR_WRITE_2(sc, VR_IMR, 0);
743 if (sc->vr_revid >= REV_ID_VT6102_A)
744 CSR_WRITE_2(sc, VR_MII_IMR, 0);
745
746 if (sc->vr_revid < REV_ID_VT6102_A) {
747 pci_write_config(dev, VR_PCI_MODE2,
748 pci_read_config(dev, VR_PCI_MODE2, 1) |
749 VR_MODE2_MODE10T, 1);
750 } else {
751 /* Report error instead of retrying forever. */
752 pci_write_config(dev, VR_PCI_MODE2,
753 pci_read_config(dev, VR_PCI_MODE2, 1) |
754 VR_MODE2_PCEROPT, 1);
755 /* Detect MII coding error. */
756 pci_write_config(dev, VR_PCI_MODE3,
757 pci_read_config(dev, VR_PCI_MODE3, 1) |
758 VR_MODE3_MIION, 1);
759 if (sc->vr_revid >= REV_ID_VT6105_LOM &&
760 sc->vr_revid < REV_ID_VT6105M_A0)
761 pci_write_config(dev, VR_PCI_MODE2,
762 pci_read_config(dev, VR_PCI_MODE2, 1) |
763 VR_MODE2_MODE10T, 1);
764 /* Enable Memory-Read-Multiple. */
765 if (sc->vr_revid >= REV_ID_VT6107_A1 &&
766 sc->vr_revid < REV_ID_VT6105M_A0)
767 pci_write_config(dev, VR_PCI_MODE2,
768 pci_read_config(dev, VR_PCI_MODE2, 1) |
769 VR_MODE2_MRDPL, 1);
770 }
771 /* Disable MII AUTOPOLL. */
772 VR_CLRBIT(sc, VR_MIICMD, VR_MIICMD_AUTOPOLL);
773
774 if (vr_dma_alloc(sc) != 0) {
775 error = ENXIO;
776 goto fail;
777 }
778
779 /* Do MII setup. */
780 if (sc->vr_revid >= REV_ID_VT6105_A0)
781 phy = 1;
782 else
783 phy = CSR_READ_1(sc, VR_PHYADDR) & VR_PHYADDR_MASK;
784 error = mii_attach(dev, &sc->vr_miibus, ifp, vr_ifmedia_upd,
785 vr_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
786 if (error != 0) {
787 device_printf(dev, "attaching PHYs failed\n");
788 goto fail;
789 }
790
791 /* Call MI attach routine. */
792 ether_ifattach(ifp, eaddr);
793 /*
794 * Tell the upper layer(s) we support long frames.
795 * Must appear after the call to ether_ifattach() because
796 * ether_ifattach() sets ifi_hdrlen to the default value.
797 */
798 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
799
800 /* Hook interrupt last to avoid having to lock softc. */
801 error = bus_setup_intr(dev, sc->vr_irq, INTR_TYPE_NET | INTR_MPSAFE,
802 NULL, vr_intr, sc, &sc->vr_intrhand);
803
804 if (error) {
805 device_printf(dev, "couldn't set up irq\n");
806 ether_ifdetach(ifp);
807 goto fail;
808 }
809
810 fail:
811 if (error)
812 vr_detach(dev);
813
814 return (error);
815 }
816
817 /*
818 * Shutdown hardware and free up resources. This can be called any
819 * time after the mutex has been initialized. It is called in both
820 * the error case in attach and the normal detach case so it needs
821 * to be careful about only freeing resources that have actually been
822 * allocated.
823 */
824 static int
825 vr_detach(device_t dev)
826 {
827 struct vr_softc *sc = device_get_softc(dev);
828 struct ifnet *ifp = sc->vr_ifp;
829
830 KASSERT(mtx_initialized(&sc->vr_mtx), ("vr mutex not initialized"));
831
832 #ifdef DEVICE_POLLING
833 if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
834 ether_poll_deregister(ifp);
835 #endif
836
837 /* These should only be active if attach succeeded. */
838 if (device_is_attached(dev)) {
839 VR_LOCK(sc);
840 sc->vr_detach = 1;
841 vr_stop(sc);
842 VR_UNLOCK(sc);
843 callout_drain(&sc->vr_stat_callout);
844 taskqueue_drain(taskqueue_swi, &sc->vr_link_task);
845 ether_ifdetach(ifp);
846 }
847 if (sc->vr_miibus)
848 device_delete_child(dev, sc->vr_miibus);
849 bus_generic_detach(dev);
850
851 if (sc->vr_intrhand)
852 bus_teardown_intr(dev, sc->vr_irq, sc->vr_intrhand);
853 if (sc->vr_irq)
854 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vr_irq);
855 if (sc->vr_res)
856 bus_release_resource(dev, sc->vr_res_type, sc->vr_res_id,
857 sc->vr_res);
858
859 if (ifp)
860 if_free(ifp);
861
862 vr_dma_free(sc);
863
864 mtx_destroy(&sc->vr_mtx);
865
866 return (0);
867 }
868
869 struct vr_dmamap_arg {
870 bus_addr_t vr_busaddr;
871 };
872
873 static void
874 vr_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
875 {
876 struct vr_dmamap_arg *ctx;
877
878 if (error != 0)
879 return;
880 ctx = arg;
881 ctx->vr_busaddr = segs[0].ds_addr;
882 }
883
884 static int
885 vr_dma_alloc(struct vr_softc *sc)
886 {
887 struct vr_dmamap_arg ctx;
888 struct vr_txdesc *txd;
889 struct vr_rxdesc *rxd;
890 bus_size_t tx_alignment;
891 int error, i;
892
893 /* Create parent DMA tag. */
894 error = bus_dma_tag_create(
895 bus_get_dma_tag(sc->vr_dev), /* parent */
896 1, 0, /* alignment, boundary */
897 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
898 BUS_SPACE_MAXADDR, /* highaddr */
899 NULL, NULL, /* filter, filterarg */
900 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
901 0, /* nsegments */
902 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
903 0, /* flags */
904 NULL, NULL, /* lockfunc, lockarg */
905 &sc->vr_cdata.vr_parent_tag);
906 if (error != 0) {
907 device_printf(sc->vr_dev, "failed to create parent DMA tag\n");
908 goto fail;
909 }
910 /* Create tag for Tx ring. */
911 error = bus_dma_tag_create(
912 sc->vr_cdata.vr_parent_tag, /* parent */
913 VR_RING_ALIGN, 0, /* alignment, boundary */
914 BUS_SPACE_MAXADDR, /* lowaddr */
915 BUS_SPACE_MAXADDR, /* highaddr */
916 NULL, NULL, /* filter, filterarg */
917 VR_TX_RING_SIZE, /* maxsize */
918 1, /* nsegments */
919 VR_TX_RING_SIZE, /* maxsegsize */
920 0, /* flags */
921 NULL, NULL, /* lockfunc, lockarg */
922 &sc->vr_cdata.vr_tx_ring_tag);
923 if (error != 0) {
924 device_printf(sc->vr_dev, "failed to create Tx ring DMA tag\n");
925 goto fail;
926 }
927
928 /* Create tag for Rx ring. */
929 error = bus_dma_tag_create(
930 sc->vr_cdata.vr_parent_tag, /* parent */
931 VR_RING_ALIGN, 0, /* alignment, boundary */
932 BUS_SPACE_MAXADDR, /* lowaddr */
933 BUS_SPACE_MAXADDR, /* highaddr */
934 NULL, NULL, /* filter, filterarg */
935 VR_RX_RING_SIZE, /* maxsize */
936 1, /* nsegments */
937 VR_RX_RING_SIZE, /* maxsegsize */
938 0, /* flags */
939 NULL, NULL, /* lockfunc, lockarg */
940 &sc->vr_cdata.vr_rx_ring_tag);
941 if (error != 0) {
942 device_printf(sc->vr_dev, "failed to create Rx ring DMA tag\n");
943 goto fail;
944 }
945
946 if ((sc->vr_quirks & VR_Q_NEEDALIGN) != 0)
947 tx_alignment = sizeof(uint32_t);
948 else
949 tx_alignment = 1;
950 /* Create tag for Tx buffers. */
951 error = bus_dma_tag_create(
952 sc->vr_cdata.vr_parent_tag, /* parent */
953 tx_alignment, 0, /* alignment, boundary */
954 BUS_SPACE_MAXADDR, /* lowaddr */
955 BUS_SPACE_MAXADDR, /* highaddr */
956 NULL, NULL, /* filter, filterarg */
957 MCLBYTES * VR_MAXFRAGS, /* maxsize */
958 VR_MAXFRAGS, /* nsegments */
959 MCLBYTES, /* maxsegsize */
960 0, /* flags */
961 NULL, NULL, /* lockfunc, lockarg */
962 &sc->vr_cdata.vr_tx_tag);
963 if (error != 0) {
964 device_printf(sc->vr_dev, "failed to create Tx DMA tag\n");
965 goto fail;
966 }
967
968 /* Create tag for Rx buffers. */
969 error = bus_dma_tag_create(
970 sc->vr_cdata.vr_parent_tag, /* parent */
971 VR_RX_ALIGN, 0, /* alignment, boundary */
972 BUS_SPACE_MAXADDR, /* lowaddr */
973 BUS_SPACE_MAXADDR, /* highaddr */
974 NULL, NULL, /* filter, filterarg */
975 MCLBYTES, /* maxsize */
976 1, /* nsegments */
977 MCLBYTES, /* maxsegsize */
978 0, /* flags */
979 NULL, NULL, /* lockfunc, lockarg */
980 &sc->vr_cdata.vr_rx_tag);
981 if (error != 0) {
982 device_printf(sc->vr_dev, "failed to create Rx DMA tag\n");
983 goto fail;
984 }
985
986 /* Allocate DMA'able memory and load the DMA map for Tx ring. */
987 error = bus_dmamem_alloc(sc->vr_cdata.vr_tx_ring_tag,
988 (void **)&sc->vr_rdata.vr_tx_ring, BUS_DMA_WAITOK |
989 BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->vr_cdata.vr_tx_ring_map);
990 if (error != 0) {
991 device_printf(sc->vr_dev,
992 "failed to allocate DMA'able memory for Tx ring\n");
993 goto fail;
994 }
995
996 ctx.vr_busaddr = 0;
997 error = bus_dmamap_load(sc->vr_cdata.vr_tx_ring_tag,
998 sc->vr_cdata.vr_tx_ring_map, sc->vr_rdata.vr_tx_ring,
999 VR_TX_RING_SIZE, vr_dmamap_cb, &ctx, 0);
1000 if (error != 0 || ctx.vr_busaddr == 0) {
1001 device_printf(sc->vr_dev,
1002 "failed to load DMA'able memory for Tx ring\n");
1003 goto fail;
1004 }
1005 sc->vr_rdata.vr_tx_ring_paddr = ctx.vr_busaddr;
1006
1007 /* Allocate DMA'able memory and load the DMA map for Rx ring. */
1008 error = bus_dmamem_alloc(sc->vr_cdata.vr_rx_ring_tag,
1009 (void **)&sc->vr_rdata.vr_rx_ring, BUS_DMA_WAITOK |
1010 BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->vr_cdata.vr_rx_ring_map);
1011 if (error != 0) {
1012 device_printf(sc->vr_dev,
1013 "failed to allocate DMA'able memory for Rx ring\n");
1014 goto fail;
1015 }
1016
1017 ctx.vr_busaddr = 0;
1018 error = bus_dmamap_load(sc->vr_cdata.vr_rx_ring_tag,
1019 sc->vr_cdata.vr_rx_ring_map, sc->vr_rdata.vr_rx_ring,
1020 VR_RX_RING_SIZE, vr_dmamap_cb, &ctx, 0);
1021 if (error != 0 || ctx.vr_busaddr == 0) {
1022 device_printf(sc->vr_dev,
1023 "failed to load DMA'able memory for Rx ring\n");
1024 goto fail;
1025 }
1026 sc->vr_rdata.vr_rx_ring_paddr = ctx.vr_busaddr;
1027
1028 /* Create DMA maps for Tx buffers. */
1029 for (i = 0; i < VR_TX_RING_CNT; i++) {
1030 txd = &sc->vr_cdata.vr_txdesc[i];
1031 txd->tx_m = NULL;
1032 txd->tx_dmamap = NULL;
1033 error = bus_dmamap_create(sc->vr_cdata.vr_tx_tag, 0,
1034 &txd->tx_dmamap);
1035 if (error != 0) {
1036 device_printf(sc->vr_dev,
1037 "failed to create Tx dmamap\n");
1038 goto fail;
1039 }
1040 }
1041 /* Create DMA maps for Rx buffers. */
1042 if ((error = bus_dmamap_create(sc->vr_cdata.vr_rx_tag, 0,
1043 &sc->vr_cdata.vr_rx_sparemap)) != 0) {
1044 device_printf(sc->vr_dev,
1045 "failed to create spare Rx dmamap\n");
1046 goto fail;
1047 }
1048 for (i = 0; i < VR_RX_RING_CNT; i++) {
1049 rxd = &sc->vr_cdata.vr_rxdesc[i];
1050 rxd->rx_m = NULL;
1051 rxd->rx_dmamap = NULL;
1052 error = bus_dmamap_create(sc->vr_cdata.vr_rx_tag, 0,
1053 &rxd->rx_dmamap);
1054 if (error != 0) {
1055 device_printf(sc->vr_dev,
1056 "failed to create Rx dmamap\n");
1057 goto fail;
1058 }
1059 }
1060
1061 fail:
1062 return (error);
1063 }
1064
1065 static void
1066 vr_dma_free(struct vr_softc *sc)
1067 {
1068 struct vr_txdesc *txd;
1069 struct vr_rxdesc *rxd;
1070 int i;
1071
1072 /* Tx ring. */
1073 if (sc->vr_cdata.vr_tx_ring_tag) {
1074 if (sc->vr_cdata.vr_tx_ring_map)
1075 bus_dmamap_unload(sc->vr_cdata.vr_tx_ring_tag,
1076 sc->vr_cdata.vr_tx_ring_map);
1077 if (sc->vr_cdata.vr_tx_ring_map &&
1078 sc->vr_rdata.vr_tx_ring)
1079 bus_dmamem_free(sc->vr_cdata.vr_tx_ring_tag,
1080 sc->vr_rdata.vr_tx_ring,
1081 sc->vr_cdata.vr_tx_ring_map);
1082 sc->vr_rdata.vr_tx_ring = NULL;
1083 sc->vr_cdata.vr_tx_ring_map = NULL;
1084 bus_dma_tag_destroy(sc->vr_cdata.vr_tx_ring_tag);
1085 sc->vr_cdata.vr_tx_ring_tag = NULL;
1086 }
1087 /* Rx ring. */
1088 if (sc->vr_cdata.vr_rx_ring_tag) {
1089 if (sc->vr_cdata.vr_rx_ring_map)
1090 bus_dmamap_unload(sc->vr_cdata.vr_rx_ring_tag,
1091 sc->vr_cdata.vr_rx_ring_map);
1092 if (sc->vr_cdata.vr_rx_ring_map &&
1093 sc->vr_rdata.vr_rx_ring)
1094 bus_dmamem_free(sc->vr_cdata.vr_rx_ring_tag,
1095 sc->vr_rdata.vr_rx_ring,
1096 sc->vr_cdata.vr_rx_ring_map);
1097 sc->vr_rdata.vr_rx_ring = NULL;
1098 sc->vr_cdata.vr_rx_ring_map = NULL;
1099 bus_dma_tag_destroy(sc->vr_cdata.vr_rx_ring_tag);
1100 sc->vr_cdata.vr_rx_ring_tag = NULL;
1101 }
1102 /* Tx buffers. */
1103 if (sc->vr_cdata.vr_tx_tag) {
1104 for (i = 0; i < VR_TX_RING_CNT; i++) {
1105 txd = &sc->vr_cdata.vr_txdesc[i];
1106 if (txd->tx_dmamap) {
1107 bus_dmamap_destroy(sc->vr_cdata.vr_tx_tag,
1108 txd->tx_dmamap);
1109 txd->tx_dmamap = NULL;
1110 }
1111 }
1112 bus_dma_tag_destroy(sc->vr_cdata.vr_tx_tag);
1113 sc->vr_cdata.vr_tx_tag = NULL;
1114 }
1115 /* Rx buffers. */
1116 if (sc->vr_cdata.vr_rx_tag) {
1117 for (i = 0; i < VR_RX_RING_CNT; i++) {
1118 rxd = &sc->vr_cdata.vr_rxdesc[i];
1119 if (rxd->rx_dmamap) {
1120 bus_dmamap_destroy(sc->vr_cdata.vr_rx_tag,
1121 rxd->rx_dmamap);
1122 rxd->rx_dmamap = NULL;
1123 }
1124 }
1125 if (sc->vr_cdata.vr_rx_sparemap) {
1126 bus_dmamap_destroy(sc->vr_cdata.vr_rx_tag,
1127 sc->vr_cdata.vr_rx_sparemap);
1128 sc->vr_cdata.vr_rx_sparemap = 0;
1129 }
1130 bus_dma_tag_destroy(sc->vr_cdata.vr_rx_tag);
1131 sc->vr_cdata.vr_rx_tag = NULL;
1132 }
1133
1134 if (sc->vr_cdata.vr_parent_tag) {
1135 bus_dma_tag_destroy(sc->vr_cdata.vr_parent_tag);
1136 sc->vr_cdata.vr_parent_tag = NULL;
1137 }
1138 }
1139
1140 /*
1141 * Initialize the transmit descriptors.
1142 */
1143 static int
1144 vr_tx_ring_init(struct vr_softc *sc)
1145 {
1146 struct vr_ring_data *rd;
1147 struct vr_txdesc *txd;
1148 bus_addr_t addr;
1149 int i;
1150
1151 sc->vr_cdata.vr_tx_prod = 0;
1152 sc->vr_cdata.vr_tx_cons = 0;
1153 sc->vr_cdata.vr_tx_cnt = 0;
1154 sc->vr_cdata.vr_tx_pkts = 0;
1155
1156 rd = &sc->vr_rdata;
1157 bzero(rd->vr_tx_ring, VR_TX_RING_SIZE);
1158 for (i = 0; i < VR_TX_RING_CNT; i++) {
1159 if (i == VR_TX_RING_CNT - 1)
1160 addr = VR_TX_RING_ADDR(sc, 0);
1161 else
1162 addr = VR_TX_RING_ADDR(sc, i + 1);
1163 rd->vr_tx_ring[i].vr_nextphys = htole32(VR_ADDR_LO(addr));
1164 txd = &sc->vr_cdata.vr_txdesc[i];
1165 txd->tx_m = NULL;
1166 }
1167
1168 bus_dmamap_sync(sc->vr_cdata.vr_tx_ring_tag,
1169 sc->vr_cdata.vr_tx_ring_map,
1170 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1171
1172 return (0);
1173 }
1174
1175 /*
1176 * Initialize the RX descriptors and allocate mbufs for them. Note that
1177 * we arrange the descriptors in a closed ring, so that the last descriptor
1178 * points back to the first.
1179 */
1180 static int
1181 vr_rx_ring_init(struct vr_softc *sc)
1182 {
1183 struct vr_ring_data *rd;
1184 struct vr_rxdesc *rxd;
1185 bus_addr_t addr;
1186 int i;
1187
1188 sc->vr_cdata.vr_rx_cons = 0;
1189
1190 rd = &sc->vr_rdata;
1191 bzero(rd->vr_rx_ring, VR_RX_RING_SIZE);
1192 for (i = 0; i < VR_RX_RING_CNT; i++) {
1193 rxd = &sc->vr_cdata.vr_rxdesc[i];
1194 rxd->rx_m = NULL;
1195 rxd->desc = &rd->vr_rx_ring[i];
1196 if (i == VR_RX_RING_CNT - 1)
1197 addr = VR_RX_RING_ADDR(sc, 0);
1198 else
1199 addr = VR_RX_RING_ADDR(sc, i + 1);
1200 rd->vr_rx_ring[i].vr_nextphys = htole32(VR_ADDR_LO(addr));
1201 if (vr_newbuf(sc, i) != 0)
1202 return (ENOBUFS);
1203 }
1204
1205 bus_dmamap_sync(sc->vr_cdata.vr_rx_ring_tag,
1206 sc->vr_cdata.vr_rx_ring_map,
1207 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1208
1209 return (0);
1210 }
1211
1212 static __inline void
1213 vr_discard_rxbuf(struct vr_rxdesc *rxd)
1214 {
1215 struct vr_desc *desc;
1216
1217 desc = rxd->desc;
1218 desc->vr_ctl = htole32(VR_RXCTL | (MCLBYTES - sizeof(uint64_t)));
1219 desc->vr_status = htole32(VR_RXSTAT_OWN);
1220 }
1221
1222 /*
1223 * Initialize an RX descriptor and attach an MBUF cluster.
1224 * Note: the length fields are only 11 bits wide, which means the
1225 * largest size we can specify is 2047. This is important because
1226 * MCLBYTES is 2048, so we have to subtract one otherwise we'll
1227 * overflow the field and make a mess.
1228 */
1229 static int
1230 vr_newbuf(struct vr_softc *sc, int idx)
1231 {
1232 struct vr_desc *desc;
1233 struct vr_rxdesc *rxd;
1234 struct mbuf *m;
1235 bus_dma_segment_t segs[1];
1236 bus_dmamap_t map;
1237 int nsegs;
1238
1239 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1240 if (m == NULL)
1241 return (ENOBUFS);
1242 m->m_len = m->m_pkthdr.len = MCLBYTES;
1243 m_adj(m, sizeof(uint64_t));
1244
1245 if (bus_dmamap_load_mbuf_sg(sc->vr_cdata.vr_rx_tag,
1246 sc->vr_cdata.vr_rx_sparemap, m, segs, &nsegs, 0) != 0) {
1247 m_freem(m);
1248 return (ENOBUFS);
1249 }
1250 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1251
1252 rxd = &sc->vr_cdata.vr_rxdesc[idx];
1253 if (rxd->rx_m != NULL) {
1254 bus_dmamap_sync(sc->vr_cdata.vr_rx_tag, rxd->rx_dmamap,
1255 BUS_DMASYNC_POSTREAD);
1256 bus_dmamap_unload(sc->vr_cdata.vr_rx_tag, rxd->rx_dmamap);
1257 }
1258 map = rxd->rx_dmamap;
1259 rxd->rx_dmamap = sc->vr_cdata.vr_rx_sparemap;
1260 sc->vr_cdata.vr_rx_sparemap = map;
1261 bus_dmamap_sync(sc->vr_cdata.vr_rx_tag, rxd->rx_dmamap,
1262 BUS_DMASYNC_PREREAD);
1263 rxd->rx_m = m;
1264 desc = rxd->desc;
1265 desc->vr_data = htole32(VR_ADDR_LO(segs[0].ds_addr));
1266 desc->vr_ctl = htole32(VR_RXCTL | segs[0].ds_len);
1267 desc->vr_status = htole32(VR_RXSTAT_OWN);
1268
1269 return (0);
1270 }
1271
1272 #ifndef __NO_STRICT_ALIGNMENT
1273 static __inline void
1274 vr_fixup_rx(struct mbuf *m)
1275 {
1276 uint16_t *src, *dst;
1277 int i;
1278
1279 src = mtod(m, uint16_t *);
1280 dst = src - 1;
1281
1282 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1283 *dst++ = *src++;
1284
1285 m->m_data -= ETHER_ALIGN;
1286 }
1287 #endif
1288
1289 /*
1290 * A frame has been uploaded: pass the resulting mbuf chain up to
1291 * the higher level protocols.
1292 */
1293 static void
1294 vr_rxeof(struct vr_softc *sc)
1295 {
1296 struct vr_rxdesc *rxd;
1297 struct mbuf *m;
1298 struct ifnet *ifp;
1299 struct vr_desc *cur_rx;
1300 int cons, prog, total_len;
1301 uint32_t rxstat, rxctl;
1302
1303 VR_LOCK_ASSERT(sc);
1304 ifp = sc->vr_ifp;
1305 cons = sc->vr_cdata.vr_rx_cons;
1306
1307 bus_dmamap_sync(sc->vr_cdata.vr_rx_ring_tag,
1308 sc->vr_cdata.vr_rx_ring_map,
1309 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1310
1311 for (prog = 0; prog < VR_RX_RING_CNT; VR_INC(cons, VR_RX_RING_CNT)) {
1312 #ifdef DEVICE_POLLING
1313 if (ifp->if_capenable & IFCAP_POLLING) {
1314 if (sc->rxcycles <= 0)
1315 break;
1316 sc->rxcycles--;
1317 }
1318 #endif
1319 cur_rx = &sc->vr_rdata.vr_rx_ring[cons];
1320 rxstat = le32toh(cur_rx->vr_status);
1321 rxctl = le32toh(cur_rx->vr_ctl);
1322 if ((rxstat & VR_RXSTAT_OWN) == VR_RXSTAT_OWN)
1323 break;
1324
1325 prog++;
1326 rxd = &sc->vr_cdata.vr_rxdesc[cons];
1327 m = rxd->rx_m;
1328
1329 /*
1330 * If an error occurs, update stats, clear the
1331 * status word and leave the mbuf cluster in place:
1332 * it should simply get re-used next time this descriptor
1333 * comes up in the ring.
1334 * We don't support SG in Rx path yet, so discard
1335 * partial frame.
1336 */
1337 if ((rxstat & VR_RXSTAT_RX_OK) == 0 ||
1338 (rxstat & (VR_RXSTAT_FIRSTFRAG | VR_RXSTAT_LASTFRAG)) !=
1339 (VR_RXSTAT_FIRSTFRAG | VR_RXSTAT_LASTFRAG)) {
1340 ifp->if_ierrors++;
1341 sc->vr_stat.rx_errors++;
1342 if (rxstat & VR_RXSTAT_CRCERR)
1343 sc->vr_stat.rx_crc_errors++;
1344 if (rxstat & VR_RXSTAT_FRAMEALIGNERR)
1345 sc->vr_stat.rx_alignment++;
1346 if (rxstat & VR_RXSTAT_FIFOOFLOW)
1347 sc->vr_stat.rx_fifo_overflows++;
1348 if (rxstat & VR_RXSTAT_GIANT)
1349 sc->vr_stat.rx_giants++;
1350 if (rxstat & VR_RXSTAT_RUNT)
1351 sc->vr_stat.rx_runts++;
1352 if (rxstat & VR_RXSTAT_BUFFERR)
1353 sc->vr_stat.rx_no_buffers++;
1354 #ifdef VR_SHOW_ERRORS
1355 device_printf(sc->vr_dev, "%s: receive error = 0x%b\n",
1356 __func__, rxstat & 0xff, VR_RXSTAT_ERR_BITS);
1357 #endif
1358 vr_discard_rxbuf(rxd);
1359 continue;
1360 }
1361
1362 if (vr_newbuf(sc, cons) != 0) {
1363 ifp->if_iqdrops++;
1364 sc->vr_stat.rx_errors++;
1365 sc->vr_stat.rx_no_mbufs++;
1366 vr_discard_rxbuf(rxd);
1367 continue;
1368 }
1369
1370 /*
1371 * XXX The VIA Rhine chip includes the CRC with every
1372 * received frame, and there's no way to turn this
1373 * behavior off (at least, I can't find anything in
1374 * the manual that explains how to do it) so we have
1375 * to trim off the CRC manually.
1376 */
1377 total_len = VR_RXBYTES(rxstat);
1378 total_len -= ETHER_CRC_LEN;
1379 m->m_pkthdr.len = m->m_len = total_len;
1380 #ifndef __NO_STRICT_ALIGNMENT
1381 /*
1382 * RX buffers must be 32-bit aligned.
1383 * Ignore the alignment problems on the non-strict alignment
1384 * platform. The performance hit incurred due to unaligned
1385 * accesses is much smaller than the hit produced by forcing
1386 * buffer copies all the time.
1387 */
1388 vr_fixup_rx(m);
1389 #endif
1390 m->m_pkthdr.rcvif = ifp;
1391 ifp->if_ipackets++;
1392 sc->vr_stat.rx_ok++;
1393 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0 &&
1394 (rxstat & VR_RXSTAT_FRAG) == 0 &&
1395 (rxctl & VR_RXCTL_IP) != 0) {
1396 /* Checksum is valid for non-fragmented IP packets. */
1397 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1398 if ((rxctl & VR_RXCTL_IPOK) == VR_RXCTL_IPOK) {
1399 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1400 if (rxctl & (VR_RXCTL_TCP | VR_RXCTL_UDP)) {
1401 m->m_pkthdr.csum_flags |=
1402 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1403 if ((rxctl & VR_RXCTL_TCPUDPOK) != 0)
1404 m->m_pkthdr.csum_data = 0xffff;
1405 }
1406 }
1407 }
1408 VR_UNLOCK(sc);
1409 (*ifp->if_input)(ifp, m);
1410 VR_LOCK(sc);
1411 }
1412
1413 if (prog > 0) {
1414 sc->vr_cdata.vr_rx_cons = cons;
1415 bus_dmamap_sync(sc->vr_cdata.vr_rx_ring_tag,
1416 sc->vr_cdata.vr_rx_ring_map,
1417 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1418 }
1419 }
1420
1421 /*
1422 * A frame was downloaded to the chip. It's safe for us to clean up
1423 * the list buffers.
1424 */
1425 static void
1426 vr_txeof(struct vr_softc *sc)
1427 {
1428 struct vr_txdesc *txd;
1429 struct vr_desc *cur_tx;
1430 struct ifnet *ifp;
1431 uint32_t txctl, txstat;
1432 int cons, prod;
1433
1434 VR_LOCK_ASSERT(sc);
1435
1436 cons = sc->vr_cdata.vr_tx_cons;
1437 prod = sc->vr_cdata.vr_tx_prod;
1438 if (cons == prod)
1439 return;
1440
1441 bus_dmamap_sync(sc->vr_cdata.vr_tx_ring_tag,
1442 sc->vr_cdata.vr_tx_ring_map,
1443 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1444
1445 ifp = sc->vr_ifp;
1446 /*
1447 * Go through our tx list and free mbufs for those
1448 * frames that have been transmitted.
1449 */
1450 for (; cons != prod; VR_INC(cons, VR_TX_RING_CNT)) {
1451 cur_tx = &sc->vr_rdata.vr_tx_ring[cons];
1452 txctl = le32toh(cur_tx->vr_ctl);
1453 txstat = le32toh(cur_tx->vr_status);
1454 if ((txstat & VR_TXSTAT_OWN) == VR_TXSTAT_OWN)
1455 break;
1456
1457 sc->vr_cdata.vr_tx_cnt--;
1458 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1459 /* Only the first descriptor in the chain is valid. */
1460 if ((txctl & VR_TXCTL_FIRSTFRAG) == 0)
1461 continue;
1462
1463 txd = &sc->vr_cdata.vr_txdesc[cons];
1464 KASSERT(txd->tx_m != NULL, ("%s: accessing NULL mbuf!\n",
1465 __func__));
1466
1467 if ((txstat & VR_TXSTAT_ERRSUM) != 0) {
1468 ifp->if_oerrors++;
1469 sc->vr_stat.tx_errors++;
1470 if ((txstat & VR_TXSTAT_ABRT) != 0) {
1471 /* Give up and restart Tx. */
1472 sc->vr_stat.tx_abort++;
1473 bus_dmamap_sync(sc->vr_cdata.vr_tx_tag,
1474 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
1475 bus_dmamap_unload(sc->vr_cdata.vr_tx_tag,
1476 txd->tx_dmamap);
1477 m_freem(txd->tx_m);
1478 txd->tx_m = NULL;
1479 VR_INC(cons, VR_TX_RING_CNT);
1480 sc->vr_cdata.vr_tx_cons = cons;
1481 if (vr_tx_stop(sc) != 0) {
1482 device_printf(sc->vr_dev,
1483 "%s: Tx shutdown error -- "
1484 "resetting\n", __func__);
1485 sc->vr_flags |= VR_F_RESTART;
1486 return;
1487 }
1488 vr_tx_start(sc);
1489 break;
1490 }
1491 if ((sc->vr_revid < REV_ID_VT3071_A &&
1492 (txstat & VR_TXSTAT_UNDERRUN)) ||
1493 (txstat & (VR_TXSTAT_UDF | VR_TXSTAT_TBUFF))) {
1494 sc->vr_stat.tx_underrun++;
1495 /* Retry and restart Tx. */
1496 sc->vr_cdata.vr_tx_cnt++;
1497 sc->vr_cdata.vr_tx_cons = cons;
1498 cur_tx->vr_status = htole32(VR_TXSTAT_OWN);
1499 bus_dmamap_sync(sc->vr_cdata.vr_tx_ring_tag,
1500 sc->vr_cdata.vr_tx_ring_map,
1501 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1502 vr_tx_underrun(sc);
1503 return;
1504 }
1505 if ((txstat & VR_TXSTAT_DEFER) != 0) {
1506 ifp->if_collisions++;
1507 sc->vr_stat.tx_collisions++;
1508 }
1509 if ((txstat & VR_TXSTAT_LATECOLL) != 0) {
1510 ifp->if_collisions++;
1511 sc->vr_stat.tx_late_collisions++;
1512 }
1513 } else {
1514 sc->vr_stat.tx_ok++;
1515 ifp->if_opackets++;
1516 }
1517
1518 bus_dmamap_sync(sc->vr_cdata.vr_tx_tag, txd->tx_dmamap,
1519 BUS_DMASYNC_POSTWRITE);
1520 bus_dmamap_unload(sc->vr_cdata.vr_tx_tag, txd->tx_dmamap);
1521 if (sc->vr_revid < REV_ID_VT3071_A) {
1522 ifp->if_collisions +=
1523 (txstat & VR_TXSTAT_COLLCNT) >> 3;
1524 sc->vr_stat.tx_collisions +=
1525 (txstat & VR_TXSTAT_COLLCNT) >> 3;
1526 } else {
1527 ifp->if_collisions += (txstat & 0x0f);
1528 sc->vr_stat.tx_collisions += (txstat & 0x0f);
1529 }
1530 m_freem(txd->tx_m);
1531 txd->tx_m = NULL;
1532 }
1533
1534 sc->vr_cdata.vr_tx_cons = cons;
1535 if (sc->vr_cdata.vr_tx_cnt == 0)
1536 sc->vr_watchdog_timer = 0;
1537 }
1538
1539 static void
1540 vr_tick(void *xsc)
1541 {
1542 struct vr_softc *sc;
1543 struct mii_data *mii;
1544
1545 sc = (struct vr_softc *)xsc;
1546
1547 VR_LOCK_ASSERT(sc);
1548
1549 if ((sc->vr_flags & VR_F_RESTART) != 0) {
1550 device_printf(sc->vr_dev, "restarting\n");
1551 sc->vr_stat.num_restart++;
1552 sc->vr_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1553 vr_init_locked(sc);
1554 sc->vr_flags &= ~VR_F_RESTART;
1555 }
1556
1557 mii = device_get_softc(sc->vr_miibus);
1558 mii_tick(mii);
1559 vr_watchdog(sc);
1560 callout_reset(&sc->vr_stat_callout, hz, vr_tick, sc);
1561 }
1562
1563 #ifdef DEVICE_POLLING
1564 static poll_handler_t vr_poll;
1565 static poll_handler_t vr_poll_locked;
1566
1567 static void
1568 vr_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1569 {
1570 struct vr_softc *sc;
1571
1572 sc = ifp->if_softc;
1573
1574 VR_LOCK(sc);
1575 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1576 vr_poll_locked(ifp, cmd, count);
1577 VR_UNLOCK(sc);
1578 }
1579
1580 static void
1581 vr_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
1582 {
1583 struct vr_softc *sc;
1584
1585 sc = ifp->if_softc;
1586
1587 VR_LOCK_ASSERT(sc);
1588
1589 sc->rxcycles = count;
1590 vr_rxeof(sc);
1591 vr_txeof(sc);
1592 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1593 vr_start_locked(ifp);
1594
1595 if (cmd == POLL_AND_CHECK_STATUS) {
1596 uint16_t status;
1597
1598 /* Also check status register. */
1599 status = CSR_READ_2(sc, VR_ISR);
1600 if (status)
1601 CSR_WRITE_2(sc, VR_ISR, status);
1602
1603 if ((status & VR_INTRS) == 0)
1604 return;
1605
1606 if ((status & (VR_ISR_BUSERR | VR_ISR_LINKSTAT2 |
1607 VR_ISR_STATSOFLOW)) != 0) {
1608 if (vr_error(sc, status) != 0)
1609 return;
1610 }
1611 if ((status & (VR_ISR_RX_NOBUF | VR_ISR_RX_OFLOW)) != 0) {
1612 #ifdef VR_SHOW_ERRORS
1613 device_printf(sc->vr_dev, "%s: receive error : 0x%b\n",
1614 __func__, status, VR_ISR_ERR_BITS);
1615 #endif
1616 vr_rx_start(sc);
1617 }
1618 }
1619 }
1620 #endif /* DEVICE_POLLING */
1621
1622 /* Back off the transmit threshold. */
1623 static void
1624 vr_tx_underrun(struct vr_softc *sc)
1625 {
1626 int thresh;
1627
1628 device_printf(sc->vr_dev, "Tx underrun -- ");
1629 if (sc->vr_txthresh < VR_TXTHRESH_MAX) {
1630 thresh = sc->vr_txthresh;
1631 sc->vr_txthresh++;
1632 if (sc->vr_txthresh >= VR_TXTHRESH_MAX) {
1633 sc->vr_txthresh = VR_TXTHRESH_MAX;
1634 printf("using store and forward mode\n");
1635 } else
1636 printf("increasing Tx threshold(%d -> %d)\n",
1637 vr_tx_threshold_tables[thresh].value,
1638 vr_tx_threshold_tables[thresh + 1].value);
1639 } else
1640 printf("\n");
1641 sc->vr_stat.tx_underrun++;
1642 if (vr_tx_stop(sc) != 0) {
1643 device_printf(sc->vr_dev, "%s: Tx shutdown error -- "
1644 "resetting\n", __func__);
1645 sc->vr_flags |= VR_F_RESTART;
1646 return;
1647 }
1648 vr_tx_start(sc);
1649 }
1650
1651 static void
1652 vr_intr(void *arg)
1653 {
1654 struct vr_softc *sc;
1655 struct ifnet *ifp;
1656 uint16_t status;
1657
1658 sc = (struct vr_softc *)arg;
1659
1660 VR_LOCK(sc);
1661
1662 if (sc->vr_suspended != 0)
1663 goto done_locked;
1664
1665 status = CSR_READ_2(sc, VR_ISR);
1666 if (status == 0 || status == 0xffff || (status & VR_INTRS) == 0)
1667 goto done_locked;
1668
1669 ifp = sc->vr_ifp;
1670 #ifdef DEVICE_POLLING
1671 if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1672 goto done_locked;
1673 #endif
1674
1675 /* Suppress unwanted interrupts. */
1676 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1677 (sc->vr_flags & VR_F_RESTART) != 0) {
1678 CSR_WRITE_2(sc, VR_IMR, 0);
1679 CSR_WRITE_2(sc, VR_ISR, status);
1680 goto done_locked;
1681 }
1682
1683 /* Disable interrupts. */
1684 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1685
1686 for (; (status & VR_INTRS) != 0;) {
1687 CSR_WRITE_2(sc, VR_ISR, status);
1688 if ((status & (VR_ISR_BUSERR | VR_ISR_LINKSTAT2 |
1689 VR_ISR_STATSOFLOW)) != 0) {
1690 if (vr_error(sc, status) != 0) {
1691 VR_UNLOCK(sc);
1692 return;
1693 }
1694 }
1695 vr_rxeof(sc);
1696 if ((status & (VR_ISR_RX_NOBUF | VR_ISR_RX_OFLOW)) != 0) {
1697 #ifdef VR_SHOW_ERRORS
1698 device_printf(sc->vr_dev, "%s: receive error = 0x%b\n",
1699 __func__, status, VR_ISR_ERR_BITS);
1700 #endif
1701 /* Restart Rx if RxDMA SM was stopped. */
1702 vr_rx_start(sc);
1703 }
1704 vr_txeof(sc);
1705 status = CSR_READ_2(sc, VR_ISR);
1706 }
1707
1708 /* Re-enable interrupts. */
1709 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1710
1711 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1712 vr_start_locked(ifp);
1713
1714 done_locked:
1715 VR_UNLOCK(sc);
1716 }
1717
1718 static int
1719 vr_error(struct vr_softc *sc, uint16_t status)
1720 {
1721 uint16_t pcis;
1722
1723 status &= VR_ISR_BUSERR | VR_ISR_LINKSTAT2 | VR_ISR_STATSOFLOW;
1724 if ((status & VR_ISR_BUSERR) != 0) {
1725 status &= ~VR_ISR_BUSERR;
1726 sc->vr_stat.bus_errors++;
1727 /* Disable further interrupts. */
1728 CSR_WRITE_2(sc, VR_IMR, 0);
1729 pcis = pci_read_config(sc->vr_dev, PCIR_STATUS, 2);
1730 device_printf(sc->vr_dev, "PCI bus error(0x%04x) -- "
1731 "resetting\n", pcis);
1732 pci_write_config(sc->vr_dev, PCIR_STATUS, pcis, 2);
1733 sc->vr_flags |= VR_F_RESTART;
1734 return (EAGAIN);
1735 }
1736 if ((status & VR_ISR_LINKSTAT2) != 0) {
1737 /* Link state change, duplex changes etc. */
1738 status &= ~VR_ISR_LINKSTAT2;
1739 }
1740 if ((status & VR_ISR_STATSOFLOW) != 0) {
1741 status &= ~VR_ISR_STATSOFLOW;
1742 if (sc->vr_revid >= REV_ID_VT6105M_A0) {
1743 /* Update MIB counters. */
1744 }
1745 }
1746
1747 if (status != 0)
1748 device_printf(sc->vr_dev,
1749 "unhandled interrupt, status = 0x%04x\n", status);
1750 return (0);
1751 }
1752
1753 /*
1754 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1755 * pointers to the fragment pointers.
1756 */
1757 static int
1758 vr_encap(struct vr_softc *sc, struct mbuf **m_head)
1759 {
1760 struct vr_txdesc *txd;
1761 struct vr_desc *desc;
1762 struct mbuf *m;
1763 bus_dma_segment_t txsegs[VR_MAXFRAGS];
1764 uint32_t csum_flags, txctl;
1765 int error, i, nsegs, prod, si;
1766 int padlen;
1767
1768 VR_LOCK_ASSERT(sc);
1769
1770 M_ASSERTPKTHDR((*m_head));
1771
1772 /*
1773 * Some VIA Rhine wants packet buffers to be longword
1774 * aligned, but very often our mbufs aren't. Rather than
1775 * waste time trying to decide when to copy and when not
1776 * to copy, just do it all the time.
1777 */
1778 if ((sc->vr_quirks & VR_Q_NEEDALIGN) != 0) {
1779 m = m_defrag(*m_head, M_DONTWAIT);
1780 if (m == NULL) {
1781 m_freem(*m_head);
1782 *m_head = NULL;
1783 return (ENOBUFS);
1784 }
1785 *m_head = m;
1786 }
1787
1788 /*
1789 * The Rhine chip doesn't auto-pad, so we have to make
1790 * sure to pad short frames out to the minimum frame length
1791 * ourselves.
1792 */
1793 if ((*m_head)->m_pkthdr.len < VR_MIN_FRAMELEN) {
1794 m = *m_head;
1795 padlen = VR_MIN_FRAMELEN - m->m_pkthdr.len;
1796 if (M_WRITABLE(m) == 0) {
1797 /* Get a writable copy. */
1798 m = m_dup(*m_head, M_DONTWAIT);
1799 m_freem(*m_head);
1800 if (m == NULL) {
1801 *m_head = NULL;
1802 return (ENOBUFS);
1803 }
1804 *m_head = m;
1805 }
1806 if (m->m_next != NULL || M_TRAILINGSPACE(m) < padlen) {
1807 m = m_defrag(m, M_DONTWAIT);
1808 if (m == NULL) {
1809 m_freem(*m_head);
1810 *m_head = NULL;
1811 return (ENOBUFS);
1812 }
1813 }
1814 /*
1815 * Manually pad short frames, and zero the pad space
1816 * to avoid leaking data.
1817 */
1818 bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
1819 m->m_pkthdr.len += padlen;
1820 m->m_len = m->m_pkthdr.len;
1821 *m_head = m;
1822 }
1823
1824 prod = sc->vr_cdata.vr_tx_prod;
1825 txd = &sc->vr_cdata.vr_txdesc[prod];
1826 error = bus_dmamap_load_mbuf_sg(sc->vr_cdata.vr_tx_tag, txd->tx_dmamap,
1827 *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1828 if (error == EFBIG) {
1829 m = m_collapse(*m_head, M_DONTWAIT, VR_MAXFRAGS);
1830 if (m == NULL) {
1831 m_freem(*m_head);
1832 *m_head = NULL;
1833 return (ENOBUFS);
1834 }
1835 *m_head = m;
1836 error = bus_dmamap_load_mbuf_sg(sc->vr_cdata.vr_tx_tag,
1837 txd->tx_dmamap, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1838 if (error != 0) {
1839 m_freem(*m_head);
1840 *m_head = NULL;
1841 return (error);
1842 }
1843 } else if (error != 0)
1844 return (error);
1845 if (nsegs == 0) {
1846 m_freem(*m_head);
1847 *m_head = NULL;
1848 return (EIO);
1849 }
1850
1851 /* Check number of available descriptors. */
1852 if (sc->vr_cdata.vr_tx_cnt + nsegs >= (VR_TX_RING_CNT - 1)) {
1853 bus_dmamap_unload(sc->vr_cdata.vr_tx_tag, txd->tx_dmamap);
1854 return (ENOBUFS);
1855 }
1856
1857 txd->tx_m = *m_head;
1858 bus_dmamap_sync(sc->vr_cdata.vr_tx_tag, txd->tx_dmamap,
1859 BUS_DMASYNC_PREWRITE);
1860
1861 /* Set checksum offload. */
1862 csum_flags = 0;
1863 if (((*m_head)->m_pkthdr.csum_flags & VR_CSUM_FEATURES) != 0) {
1864 if ((*m_head)->m_pkthdr.csum_flags & CSUM_IP)
1865 csum_flags |= VR_TXCTL_IPCSUM;
1866 if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP)
1867 csum_flags |= VR_TXCTL_TCPCSUM;
1868 if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP)
1869 csum_flags |= VR_TXCTL_UDPCSUM;
1870 }
1871
1872 /*
1873 * Quite contrary to datasheet for VIA Rhine, VR_TXCTL_TLINK bit
1874 * is required for all descriptors regardless of single or
1875 * multiple buffers. Also VR_TXSTAT_OWN bit is valid only for
1876 * the first descriptor for a multi-fragmented frames. Without
1877 * that VIA Rhine chip generates Tx underrun interrupts and can't
1878 * send any frames.
1879 */
1880 si = prod;
1881 for (i = 0; i < nsegs; i++) {
1882 desc = &sc->vr_rdata.vr_tx_ring[prod];
1883 desc->vr_status = 0;
1884 txctl = txsegs[i].ds_len | VR_TXCTL_TLINK | csum_flags;
1885 if (i == 0)
1886 txctl |= VR_TXCTL_FIRSTFRAG;
1887 desc->vr_ctl = htole32(txctl);
1888 desc->vr_data = htole32(VR_ADDR_LO(txsegs[i].ds_addr));
1889 sc->vr_cdata.vr_tx_cnt++;
1890 VR_INC(prod, VR_TX_RING_CNT);
1891 }
1892 /* Update producer index. */
1893 sc->vr_cdata.vr_tx_prod = prod;
1894
1895 prod = (prod + VR_TX_RING_CNT - 1) % VR_TX_RING_CNT;
1896 desc = &sc->vr_rdata.vr_tx_ring[prod];
1897
1898 /*
1899 * Set EOP on the last desciptor and reuqest Tx completion
1900 * interrupt for every VR_TX_INTR_THRESH-th frames.
1901 */
1902 VR_INC(sc->vr_cdata.vr_tx_pkts, VR_TX_INTR_THRESH);
1903 if (sc->vr_cdata.vr_tx_pkts == 0)
1904 desc->vr_ctl |= htole32(VR_TXCTL_LASTFRAG | VR_TXCTL_FINT);
1905 else
1906 desc->vr_ctl |= htole32(VR_TXCTL_LASTFRAG);
1907
1908 /* Lastly turn the first descriptor ownership to hardware. */
1909 desc = &sc->vr_rdata.vr_tx_ring[si];
1910 desc->vr_status |= htole32(VR_TXSTAT_OWN);
1911
1912 /* Sync descriptors. */
1913 bus_dmamap_sync(sc->vr_cdata.vr_tx_ring_tag,
1914 sc->vr_cdata.vr_tx_ring_map,
1915 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1916
1917 return (0);
1918 }
1919
1920 static void
1921 vr_start(struct ifnet *ifp)
1922 {
1923 struct vr_softc *sc;
1924
1925 sc = ifp->if_softc;
1926 VR_LOCK(sc);
1927 vr_start_locked(ifp);
1928 VR_UNLOCK(sc);
1929 }
1930
1931 static void
1932 vr_start_locked(struct ifnet *ifp)
1933 {
1934 struct vr_softc *sc;
1935 struct mbuf *m_head;
1936 int enq;
1937
1938 sc = ifp->if_softc;
1939
1940 VR_LOCK_ASSERT(sc);
1941
1942 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1943 IFF_DRV_RUNNING || sc->vr_link == 0)
1944 return;
1945
1946 for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
1947 sc->vr_cdata.vr_tx_cnt < VR_TX_RING_CNT - 2; ) {
1948 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1949 if (m_head == NULL)
1950 break;
1951 /*
1952 * Pack the data into the transmit ring. If we
1953 * don't have room, set the OACTIVE flag and wait
1954 * for the NIC to drain the ring.
1955 */
1956 if (vr_encap(sc, &m_head)) {
1957 if (m_head == NULL)
1958 break;
1959 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1960 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1961 break;
1962 }
1963
1964 enq++;
1965 /*
1966 * If there's a BPF listener, bounce a copy of this frame
1967 * to him.
1968 */
1969 ETHER_BPF_MTAP(ifp, m_head);
1970 }
1971
1972 if (enq > 0) {
1973 /* Tell the chip to start transmitting. */
1974 VR_SETBIT(sc, VR_CR0, VR_CR0_TX_GO);
1975 /* Set a timeout in case the chip goes out to lunch. */
1976 sc->vr_watchdog_timer = 5;
1977 }
1978 }
1979
1980 static void
1981 vr_init(void *xsc)
1982 {
1983 struct vr_softc *sc;
1984
1985 sc = (struct vr_softc *)xsc;
1986 VR_LOCK(sc);
1987 vr_init_locked(sc);
1988 VR_UNLOCK(sc);
1989 }
1990
1991 static void
1992 vr_init_locked(struct vr_softc *sc)
1993 {
1994 struct ifnet *ifp;
1995 struct mii_data *mii;
1996 bus_addr_t addr;
1997 int i;
1998
1999 VR_LOCK_ASSERT(sc);
2000
2001 ifp = sc->vr_ifp;
2002 mii = device_get_softc(sc->vr_miibus);
2003
2004 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2005 return;
2006
2007 /* Cancel pending I/O and free all RX/TX buffers. */
2008 vr_stop(sc);
2009 vr_reset(sc);
2010
2011 /* Set our station address. */
2012 for (i = 0; i < ETHER_ADDR_LEN; i++)
2013 CSR_WRITE_1(sc, VR_PAR0 + i, IF_LLADDR(sc->vr_ifp)[i]);
2014
2015 /* Set DMA size. */
2016 VR_CLRBIT(sc, VR_BCR0, VR_BCR0_DMA_LENGTH);
2017 VR_SETBIT(sc, VR_BCR0, VR_BCR0_DMA_STORENFWD);
2018
2019 /*
2020 * BCR0 and BCR1 can override the RXCFG and TXCFG registers,
2021 * so we must set both.
2022 */
2023 VR_CLRBIT(sc, VR_BCR0, VR_BCR0_RX_THRESH);
2024 VR_SETBIT(sc, VR_BCR0, VR_BCR0_RXTHRESH128BYTES);
2025
2026 VR_CLRBIT(sc, VR_BCR1, VR_BCR1_TX_THRESH);
2027 VR_SETBIT(sc, VR_BCR1, vr_tx_threshold_tables[sc->vr_txthresh].bcr_cfg);
2028
2029 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
2030 VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_128BYTES);
2031
2032 VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
2033 VR_SETBIT(sc, VR_TXCFG, vr_tx_threshold_tables[sc->vr_txthresh].tx_cfg);
2034
2035 /* Init circular RX list. */
2036 if (vr_rx_ring_init(sc) != 0) {
2037 device_printf(sc->vr_dev,
2038 "initialization failed: no memory for rx buffers\n");
2039 vr_stop(sc);
2040 return;
2041 }
2042
2043 /* Init tx descriptors. */
2044 vr_tx_ring_init(sc);
2045
2046 if ((sc->vr_quirks & VR_Q_CAM) != 0) {
2047 uint8_t vcam[2] = { 0, 0 };
2048
2049 /* Disable VLAN hardware tag insertion/stripping. */
2050 VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TXTAGEN | VR_TXCFG_RXTAGCTL);
2051 /* Disable VLAN hardware filtering. */
2052 VR_CLRBIT(sc, VR_BCR1, VR_BCR1_VLANFILT_ENB);
2053 /* Disable all CAM entries. */
2054 vr_cam_mask(sc, VR_MCAST_CAM, 0);
2055 vr_cam_mask(sc, VR_VLAN_CAM, 0);
2056 /* Enable the first VLAN CAM. */
2057 vr_cam_data(sc, VR_VLAN_CAM, 0, vcam);
2058 vr_cam_mask(sc, VR_VLAN_CAM, 1);
2059 }
2060
2061 /*
2062 * Set up receive filter.
2063 */
2064 vr_set_filter(sc);
2065
2066 /*
2067 * Load the address of the RX ring.
2068 */
2069 addr = VR_RX_RING_ADDR(sc, 0);
2070 CSR_WRITE_4(sc, VR_RXADDR, VR_ADDR_LO(addr));
2071 /*
2072 * Load the address of the TX ring.
2073 */
2074 addr = VR_TX_RING_ADDR(sc, 0);
2075 CSR_WRITE_4(sc, VR_TXADDR, VR_ADDR_LO(addr));
2076 /* Default : full-duplex, no Tx poll. */
2077 CSR_WRITE_1(sc, VR_CR1, VR_CR1_FULLDUPLEX | VR_CR1_TX_NOPOLL);
2078
2079 /* Set flow-control parameters for Rhine III. */
2080 if (sc->vr_revid >= REV_ID_VT6105_A0) {
2081 /* Rx buffer count available for incoming packet. */
2082 CSR_WRITE_1(sc, VR_FLOWCR0, VR_RX_RING_CNT);
2083 /*
2084 * Tx pause low threshold : 16 free receive buffers
2085 * Tx pause XON high threshold : 48 free receive buffers
2086 */
2087 CSR_WRITE_1(sc, VR_FLOWCR1,
2088 VR_FLOWCR1_TXLO16 | VR_FLOWCR1_TXHI48 | VR_FLOWCR1_XONXOFF);
2089 /* Set Tx pause timer. */
2090 CSR_WRITE_2(sc, VR_PAUSETIMER, 0xffff);
2091 }
2092
2093 /* Enable receiver and transmitter. */
2094 CSR_WRITE_1(sc, VR_CR0,
2095 VR_CR0_START | VR_CR0_TX_ON | VR_CR0_RX_ON | VR_CR0_RX_GO);
2096
2097 CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
2098 #ifdef DEVICE_POLLING
2099 /*
2100 * Disable interrupts if we are polling.
2101 */
2102 if (ifp->if_capenable & IFCAP_POLLING)
2103 CSR_WRITE_2(sc, VR_IMR, 0);
2104 else
2105 #endif
2106 /*
2107 * Enable interrupts and disable MII intrs.
2108 */
2109 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
2110 if (sc->vr_revid > REV_ID_VT6102_A)
2111 CSR_WRITE_2(sc, VR_MII_IMR, 0);
2112
2113 sc->vr_link = 0;
2114 mii_mediachg(mii);
2115
2116 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2117 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2118
2119 callout_reset(&sc->vr_stat_callout, hz, vr_tick, sc);
2120 }
2121
2122 /*
2123 * Set media options.
2124 */
2125 static int
2126 vr_ifmedia_upd(struct ifnet *ifp)
2127 {
2128 struct vr_softc *sc;
2129 struct mii_data *mii;
2130 struct mii_softc *miisc;
2131 int error;
2132
2133 sc = ifp->if_softc;
2134 VR_LOCK(sc);
2135 mii = device_get_softc(sc->vr_miibus);
2136 if (mii->mii_instance) {
2137 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
2138 mii_phy_reset(miisc);
2139 }
2140 error = mii_mediachg(mii);
2141 VR_UNLOCK(sc);
2142
2143 return (error);
2144 }
2145
2146 /*
2147 * Report current media status.
2148 */
2149 static void
2150 vr_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2151 {
2152 struct vr_softc *sc;
2153 struct mii_data *mii;
2154
2155 sc = ifp->if_softc;
2156 mii = device_get_softc(sc->vr_miibus);
2157 VR_LOCK(sc);
2158 mii_pollstat(mii);
2159 VR_UNLOCK(sc);
2160 ifmr->ifm_active = mii->mii_media_active;
2161 ifmr->ifm_status = mii->mii_media_status;
2162 }
2163
2164 static int
2165 vr_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2166 {
2167 struct vr_softc *sc;
2168 struct ifreq *ifr;
2169 struct mii_data *mii;
2170 int error, mask;
2171
2172 sc = ifp->if_softc;
2173 ifr = (struct ifreq *)data;
2174 error = 0;
2175
2176 switch (command) {
2177 case SIOCSIFFLAGS:
2178 VR_LOCK(sc);
2179 if (ifp->if_flags & IFF_UP) {
2180 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2181 if ((ifp->if_flags ^ sc->vr_if_flags) &
2182 (IFF_PROMISC | IFF_ALLMULTI))
2183 vr_set_filter(sc);
2184 } else {
2185 if (sc->vr_detach == 0)
2186 vr_init_locked(sc);
2187 }
2188 } else {
2189 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2190 vr_stop(sc);
2191 }
2192 sc->vr_if_flags = ifp->if_flags;
2193 VR_UNLOCK(sc);
2194 break;
2195 case SIOCADDMULTI:
2196 case SIOCDELMULTI:
2197 VR_LOCK(sc);
2198 vr_set_filter(sc);
2199 VR_UNLOCK(sc);
2200 break;
2201 case SIOCGIFMEDIA:
2202 case SIOCSIFMEDIA:
2203 mii = device_get_softc(sc->vr_miibus);
2204 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2205 break;
2206 case SIOCSIFCAP:
2207 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2208 #ifdef DEVICE_POLLING
2209 if (mask & IFCAP_POLLING) {
2210 if (ifr->ifr_reqcap & IFCAP_POLLING) {
2211 error = ether_poll_register(vr_poll, ifp);
2212 if (error != 0)
2213 break;
2214 VR_LOCK(sc);
2215 /* Disable interrupts. */
2216 CSR_WRITE_2(sc, VR_IMR, 0x0000);
2217 ifp->if_capenable |= IFCAP_POLLING;
2218 VR_UNLOCK(sc);
2219 } else {
2220 error = ether_poll_deregister(ifp);
2221 /* Enable interrupts. */
2222 VR_LOCK(sc);
2223 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
2224 ifp->if_capenable &= ~IFCAP_POLLING;
2225 VR_UNLOCK(sc);
2226 }
2227 }
2228 #endif /* DEVICE_POLLING */
2229 if ((mask & IFCAP_TXCSUM) != 0 &&
2230 (IFCAP_TXCSUM & ifp->if_capabilities) != 0) {
2231 ifp->if_capenable ^= IFCAP_TXCSUM;
2232 if ((IFCAP_TXCSUM & ifp->if_capenable) != 0)
2233 ifp->if_hwassist |= VR_CSUM_FEATURES;
2234 else
2235 ifp->if_hwassist &= ~VR_CSUM_FEATURES;
2236 }
2237 if ((mask & IFCAP_RXCSUM) != 0 &&
2238 (IFCAP_RXCSUM & ifp->if_capabilities) != 0)
2239 ifp->if_capenable ^= IFCAP_RXCSUM;
2240 if ((mask & IFCAP_WOL_UCAST) != 0 &&
2241 (ifp->if_capabilities & IFCAP_WOL_UCAST) != 0)
2242 ifp->if_capenable ^= IFCAP_WOL_UCAST;
2243 if ((mask & IFCAP_WOL_MAGIC) != 0 &&
2244 (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
2245 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2246 break;
2247 default:
2248 error = ether_ioctl(ifp, command, data);
2249 break;
2250 }
2251
2252 return (error);
2253 }
2254
2255 static void
2256 vr_watchdog(struct vr_softc *sc)
2257 {
2258 struct ifnet *ifp;
2259
2260 VR_LOCK_ASSERT(sc);
2261
2262 if (sc->vr_watchdog_timer == 0 || --sc->vr_watchdog_timer)
2263 return;
2264
2265 ifp = sc->vr_ifp;
2266 /*
2267 * Reclaim first as we don't request interrupt for every packets.
2268 */
2269 vr_txeof(sc);
2270 if (sc->vr_cdata.vr_tx_cnt == 0)
2271 return;
2272
2273 if (sc->vr_link == 0) {
2274 if (bootverbose)
2275 if_printf(sc->vr_ifp, "watchdog timeout "
2276 "(missed link)\n");
2277 ifp->if_oerrors++;
2278 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2279 vr_init_locked(sc);
2280 return;
2281 }
2282
2283 ifp->if_oerrors++;
2284 if_printf(ifp, "watchdog timeout\n");
2285
2286 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2287 vr_init_locked(sc);
2288
2289 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2290 vr_start_locked(ifp);
2291 }
2292
2293 static void
2294 vr_tx_start(struct vr_softc *sc)
2295 {
2296 bus_addr_t addr;
2297 uint8_t cmd;
2298
2299 cmd = CSR_READ_1(sc, VR_CR0);
2300 if ((cmd & VR_CR0_TX_ON) == 0) {
2301 addr = VR_TX_RING_ADDR(sc, sc->vr_cdata.vr_tx_cons);
2302 CSR_WRITE_4(sc, VR_TXADDR, VR_ADDR_LO(addr));
2303 cmd |= VR_CR0_TX_ON;
2304 CSR_WRITE_1(sc, VR_CR0, cmd);
2305 }
2306 if (sc->vr_cdata.vr_tx_cnt != 0) {
2307 sc->vr_watchdog_timer = 5;
2308 VR_SETBIT(sc, VR_CR0, VR_CR0_TX_GO);
2309 }
2310 }
2311
2312 static void
2313 vr_rx_start(struct vr_softc *sc)
2314 {
2315 bus_addr_t addr;
2316 uint8_t cmd;
2317
2318 cmd = CSR_READ_1(sc, VR_CR0);
2319 if ((cmd & VR_CR0_RX_ON) == 0) {
2320 addr = VR_RX_RING_ADDR(sc, sc->vr_cdata.vr_rx_cons);
2321 CSR_WRITE_4(sc, VR_RXADDR, VR_ADDR_LO(addr));
2322 cmd |= VR_CR0_RX_ON;
2323 CSR_WRITE_1(sc, VR_CR0, cmd);
2324 }
2325 CSR_WRITE_1(sc, VR_CR0, cmd | VR_CR0_RX_GO);
2326 }
2327
2328 static int
2329 vr_tx_stop(struct vr_softc *sc)
2330 {
2331 int i;
2332 uint8_t cmd;
2333
2334 cmd = CSR_READ_1(sc, VR_CR0);
2335 if ((cmd & VR_CR0_TX_ON) != 0) {
2336 cmd &= ~VR_CR0_TX_ON;
2337 CSR_WRITE_1(sc, VR_CR0, cmd);
2338 for (i = VR_TIMEOUT; i > 0; i--) {
2339 DELAY(5);
2340 cmd = CSR_READ_1(sc, VR_CR0);
2341 if ((cmd & VR_CR0_TX_ON) == 0)
2342 break;
2343 }
2344 if (i == 0)
2345 return (ETIMEDOUT);
2346 }
2347 return (0);
2348 }
2349
2350 static int
2351 vr_rx_stop(struct vr_softc *sc)
2352 {
2353 int i;
2354 uint8_t cmd;
2355
2356 cmd = CSR_READ_1(sc, VR_CR0);
2357 if ((cmd & VR_CR0_RX_ON) != 0) {
2358 cmd &= ~VR_CR0_RX_ON;
2359 CSR_WRITE_1(sc, VR_CR0, cmd);
2360 for (i = VR_TIMEOUT; i > 0; i--) {
2361 DELAY(5);
2362 cmd = CSR_READ_1(sc, VR_CR0);
2363 if ((cmd & VR_CR0_RX_ON) == 0)
2364 break;
2365 }
2366 if (i == 0)
2367 return (ETIMEDOUT);
2368 }
2369 return (0);
2370 }
2371
2372 /*
2373 * Stop the adapter and free any mbufs allocated to the
2374 * RX and TX lists.
2375 */
2376 static void
2377 vr_stop(struct vr_softc *sc)
2378 {
2379 struct vr_txdesc *txd;
2380 struct vr_rxdesc *rxd;
2381 struct ifnet *ifp;
2382 int i;
2383
2384 VR_LOCK_ASSERT(sc);
2385
2386 ifp = sc->vr_ifp;
2387 sc->vr_watchdog_timer = 0;
2388
2389 callout_stop(&sc->vr_stat_callout);
2390 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2391
2392 CSR_WRITE_1(sc, VR_CR0, VR_CR0_STOP);
2393 if (vr_rx_stop(sc) != 0)
2394 device_printf(sc->vr_dev, "%s: Rx shutdown error\n", __func__);
2395 if (vr_tx_stop(sc) != 0)
2396 device_printf(sc->vr_dev, "%s: Tx shutdown error\n", __func__);
2397 /* Clear pending interrupts. */
2398 CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
2399 CSR_WRITE_2(sc, VR_IMR, 0x0000);
2400 CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
2401 CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
2402
2403 /*
2404 * Free RX and TX mbufs still in the queues.
2405 */
2406 for (i = 0; i < VR_RX_RING_CNT; i++) {
2407 rxd = &sc->vr_cdata.vr_rxdesc[i];
2408 if (rxd->rx_m != NULL) {
2409 bus_dmamap_sync(sc->vr_cdata.vr_rx_tag,
2410 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2411 bus_dmamap_unload(sc->vr_cdata.vr_rx_tag,
2412 rxd->rx_dmamap);
2413 m_freem(rxd->rx_m);
2414 rxd->rx_m = NULL;
2415 }
2416 }
2417 for (i = 0; i < VR_TX_RING_CNT; i++) {
2418 txd = &sc->vr_cdata.vr_txdesc[i];
2419 if (txd->tx_m != NULL) {
2420 bus_dmamap_sync(sc->vr_cdata.vr_tx_tag,
2421 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2422 bus_dmamap_unload(sc->vr_cdata.vr_tx_tag,
2423 txd->tx_dmamap);
2424 m_freem(txd->tx_m);
2425 txd->tx_m = NULL;
2426 }
2427 }
2428 }
2429
2430 /*
2431 * Stop all chip I/O so that the kernel's probe routines don't
2432 * get confused by errant DMAs when rebooting.
2433 */
2434 static int
2435 vr_shutdown(device_t dev)
2436 {
2437
2438 return (vr_suspend(dev));
2439 }
2440
2441 static int
2442 vr_suspend(device_t dev)
2443 {
2444 struct vr_softc *sc;
2445
2446 sc = device_get_softc(dev);
2447
2448 VR_LOCK(sc);
2449 vr_stop(sc);
2450 vr_setwol(sc);
2451 sc->vr_suspended = 1;
2452 VR_UNLOCK(sc);
2453
2454 return (0);
2455 }
2456
2457 static int
2458 vr_resume(device_t dev)
2459 {
2460 struct vr_softc *sc;
2461 struct ifnet *ifp;
2462
2463 sc = device_get_softc(dev);
2464
2465 VR_LOCK(sc);
2466 ifp = sc->vr_ifp;
2467 vr_clrwol(sc);
2468 vr_reset(sc);
2469 if (ifp->if_flags & IFF_UP)
2470 vr_init_locked(sc);
2471
2472 sc->vr_suspended = 0;
2473 VR_UNLOCK(sc);
2474
2475 return (0);
2476 }
2477
2478 static void
2479 vr_setwol(struct vr_softc *sc)
2480 {
2481 struct ifnet *ifp;
2482 int pmc;
2483 uint16_t pmstat;
2484 uint8_t v;
2485
2486 VR_LOCK_ASSERT(sc);
2487
2488 if (sc->vr_revid < REV_ID_VT6102_A ||
2489 pci_find_extcap(sc->vr_dev, PCIY_PMG, &pmc) != 0)
2490 return;
2491
2492 ifp = sc->vr_ifp;
2493
2494 /* Clear WOL configuration. */
2495 CSR_WRITE_1(sc, VR_WOLCR_CLR, 0xFF);
2496 CSR_WRITE_1(sc, VR_WOLCFG_CLR, VR_WOLCFG_SAB | VR_WOLCFG_SAM);
2497 CSR_WRITE_1(sc, VR_PWRCSR_CLR, 0xFF);
2498 CSR_WRITE_1(sc, VR_PWRCFG_CLR, VR_PWRCFG_WOLEN);
2499 if (sc->vr_revid > REV_ID_VT6105_B0) {
2500 /* Newer Rhine III supports two additional patterns. */
2501 CSR_WRITE_1(sc, VR_WOLCFG_CLR, VR_WOLCFG_PATTERN_PAGE);
2502 CSR_WRITE_1(sc, VR_TESTREG_CLR, 3);
2503 CSR_WRITE_1(sc, VR_PWRCSR1_CLR, 3);
2504 }
2505 if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
2506 CSR_WRITE_1(sc, VR_WOLCR_SET, VR_WOLCR_UCAST);
2507 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2508 CSR_WRITE_1(sc, VR_WOLCR_SET, VR_WOLCR_MAGIC);
2509 /*
2510 * It seems that multicast wakeup frames require programming pattern
2511 * registers and valid CRC as well as pattern mask for each pattern.
2512 * While it's possible to setup such a pattern it would complicate
2513 * WOL configuration so ignore multicast wakeup frames.
2514 */
2515 if ((ifp->if_capenable & IFCAP_WOL) != 0) {
2516 CSR_WRITE_1(sc, VR_WOLCFG_SET, VR_WOLCFG_SAB | VR_WOLCFG_SAM);
2517 v = CSR_READ_1(sc, VR_STICKHW);
2518 CSR_WRITE_1(sc, VR_STICKHW, v | VR_STICKHW_WOL_ENB);
2519 CSR_WRITE_1(sc, VR_PWRCFG_SET, VR_PWRCFG_WOLEN);
2520 }
2521
2522 /* Put hardware into sleep. */
2523 v = CSR_READ_1(sc, VR_STICKHW);
2524 v |= VR_STICKHW_DS0 | VR_STICKHW_DS1;
2525 CSR_WRITE_1(sc, VR_STICKHW, v);
2526
2527 /* Request PME if WOL is requested. */
2528 pmstat = pci_read_config(sc->vr_dev, pmc + PCIR_POWER_STATUS, 2);
2529 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2530 if ((ifp->if_capenable & IFCAP_WOL) != 0)
2531 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2532 pci_write_config(sc->vr_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
2533 }
2534
2535 static void
2536 vr_clrwol(struct vr_softc *sc)
2537 {
2538 uint8_t v;
2539
2540 VR_LOCK_ASSERT(sc);
2541
2542 if (sc->vr_revid < REV_ID_VT6102_A)
2543 return;
2544
2545 /* Take hardware out of sleep. */
2546 v = CSR_READ_1(sc, VR_STICKHW);
2547 v &= ~(VR_STICKHW_DS0 | VR_STICKHW_DS1 | VR_STICKHW_WOL_ENB);
2548 CSR_WRITE_1(sc, VR_STICKHW, v);
2549
2550 /* Clear WOL configuration as WOL may interfere normal operation. */
2551 CSR_WRITE_1(sc, VR_WOLCR_CLR, 0xFF);
2552 CSR_WRITE_1(sc, VR_WOLCFG_CLR,
2553 VR_WOLCFG_SAB | VR_WOLCFG_SAM | VR_WOLCFG_PMEOVR);
2554 CSR_WRITE_1(sc, VR_PWRCSR_CLR, 0xFF);
2555 CSR_WRITE_1(sc, VR_PWRCFG_CLR, VR_PWRCFG_WOLEN);
2556 if (sc->vr_revid > REV_ID_VT6105_B0) {
2557 /* Newer Rhine III supports two additional patterns. */
2558 CSR_WRITE_1(sc, VR_WOLCFG_CLR, VR_WOLCFG_PATTERN_PAGE);
2559 CSR_WRITE_1(sc, VR_TESTREG_CLR, 3);
2560 CSR_WRITE_1(sc, VR_PWRCSR1_CLR, 3);
2561 }
2562 }
2563
2564 static int
2565 vr_sysctl_stats(SYSCTL_HANDLER_ARGS)
2566 {
2567 struct vr_softc *sc;
2568 struct vr_statistics *stat;
2569 int error;
2570 int result;
2571
2572 result = -1;
2573 error = sysctl_handle_int(oidp, &result, 0, req);
2574
2575 if (error != 0 || req->newptr == NULL)
2576 return (error);
2577
2578 if (result == 1) {
2579 sc = (struct vr_softc *)arg1;
2580 stat = &sc->vr_stat;
2581
2582 printf("%s statistics:\n", device_get_nameunit(sc->vr_dev));
2583 printf("Outbound good frames : %ju\n",
2584 (uintmax_t)stat->tx_ok);
2585 printf("Inbound good frames : %ju\n",
2586 (uintmax_t)stat->rx_ok);
2587 printf("Outbound errors : %u\n", stat->tx_errors);
2588 printf("Inbound errors : %u\n", stat->rx_errors);
2589 printf("Inbound no buffers : %u\n", stat->rx_no_buffers);
2590 printf("Inbound no mbuf clusters: %d\n", stat->rx_no_mbufs);
2591 printf("Inbound FIFO overflows : %d\n",
2592 stat->rx_fifo_overflows);
2593 printf("Inbound CRC errors : %u\n", stat->rx_crc_errors);
2594 printf("Inbound frame alignment errors : %u\n",
2595 stat->rx_alignment);
2596 printf("Inbound giant frames : %u\n", stat->rx_giants);
2597 printf("Inbound runt frames : %u\n", stat->rx_runts);
2598 printf("Outbound aborted with excessive collisions : %u\n",
2599 stat->tx_abort);
2600 printf("Outbound collisions : %u\n", stat->tx_collisions);
2601 printf("Outbound late collisions : %u\n",
2602 stat->tx_late_collisions);
2603 printf("Outbound underrun : %u\n", stat->tx_underrun);
2604 printf("PCI bus errors : %u\n", stat->bus_errors);
2605 printf("driver restarted due to Rx/Tx shutdown failure : %u\n",
2606 stat->num_restart);
2607 }
2608
2609 return (error);
2610 }
Cache object: 4b6308c7ce16febba6212541fe37d0f1
|