1f7788e8eSJonathan Lemon /*- 2a17c678eSDavid Greenman * Copyright (c) 1995, David Greenman 33bd07cfdSJonathan Lemon * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org> 4a17c678eSDavid Greenman * All rights reserved. 5a17c678eSDavid Greenman * 6a17c678eSDavid Greenman * Redistribution and use in source and binary forms, with or without 7a17c678eSDavid Greenman * modification, are permitted provided that the following conditions 8a17c678eSDavid Greenman * are met: 9a17c678eSDavid Greenman * 1. Redistributions of source code must retain the above copyright 10a17c678eSDavid Greenman * notice unmodified, this list of conditions, and the following 11a17c678eSDavid Greenman * disclaimer. 12a17c678eSDavid Greenman * 2. Redistributions in binary form must reproduce the above copyright 13a17c678eSDavid Greenman * notice, this list of conditions and the following disclaimer in the 14a17c678eSDavid Greenman * documentation and/or other materials provided with the distribution. 15a17c678eSDavid Greenman * 16a17c678eSDavid Greenman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17a17c678eSDavid Greenman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18a17c678eSDavid Greenman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19a17c678eSDavid Greenman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20a17c678eSDavid Greenman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21a17c678eSDavid Greenman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22a17c678eSDavid Greenman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23a17c678eSDavid Greenman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24a17c678eSDavid Greenman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25a17c678eSDavid Greenman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26a17c678eSDavid Greenman * SUCH DAMAGE. 27a17c678eSDavid Greenman * 28a17c678eSDavid Greenman */ 29a17c678eSDavid Greenman 30aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 32aad970f1SDavid E. O'Brien 33a17c678eSDavid Greenman /* 34ae12cddaSDavid Greenman * Intel EtherExpress Pro/100B PCI Fast Ethernet driver 35a17c678eSDavid Greenman */ 36a17c678eSDavid Greenman 37a17c678eSDavid Greenman #include <sys/param.h> 38a17c678eSDavid Greenman #include <sys/systm.h> 3983e6547dSMaxime Henrion #include <sys/endian.h> 40a17c678eSDavid Greenman #include <sys/mbuf.h> 41f7788e8eSJonathan Lemon /* #include <sys/mutex.h> */ 42a17c678eSDavid Greenman #include <sys/kernel.h> 43fe12f24bSPoul-Henning Kamp #include <sys/module.h> 444458ac71SBruce Evans #include <sys/socket.h> 4572a32a26SJonathan Lemon #include <sys/sysctl.h> 46a17c678eSDavid Greenman 47a17c678eSDavid Greenman #include <net/if.h> 48397f9dfeSDavid Greenman #include <net/if_dl.h> 49ba8c6fd5SDavid Greenman #include <net/if_media.h> 50a17c678eSDavid Greenman 51a17c678eSDavid Greenman #include <net/bpf.h> 52ba8c6fd5SDavid Greenman #include <sys/sockio.h> 536182fdbdSPeter Wemm #include <sys/bus.h> 546182fdbdSPeter Wemm #include <machine/bus.h> 556182fdbdSPeter Wemm #include <sys/rman.h> 566182fdbdSPeter Wemm #include <machine/resource.h> 57ba8c6fd5SDavid Greenman 581d5e9e22SEivind Eklund #include <net/ethernet.h> 591d5e9e22SEivind Eklund #include <net/if_arp.h> 60ba8c6fd5SDavid Greenman 61f7788e8eSJonathan Lemon #include <machine/clock.h> /* for DELAY */ 62a17c678eSDavid Greenman 63e8c8b728SJonathan Lemon #include <net/if_types.h> 64e8c8b728SJonathan Lemon #include <net/if_vlan_var.h> 65e8c8b728SJonathan Lemon 66c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR 67c8bca6dcSBill Paul #include <netinet/in.h> 68c8bca6dcSBill Paul #include <netinet/in_systm.h> 69c8bca6dcSBill Paul #include <netinet/ip.h> 70c8bca6dcSBill Paul #include <machine/in_cksum.h> 71c8bca6dcSBill Paul #endif 72c8bca6dcSBill Paul 734fbd232cSWarner Losh #include <dev/pci/pcivar.h> 744fbd232cSWarner Losh #include <dev/pci/pcireg.h> /* for PCIM_CMD_xxx */ 75a17c678eSDavid Greenman 76f7788e8eSJonathan Lemon #include <dev/mii/mii.h> 77f7788e8eSJonathan Lemon #include <dev/mii/miivar.h> 78f7788e8eSJonathan Lemon 79f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h> 80f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h> 8172a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h> 82f7788e8eSJonathan Lemon 83f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1); 84f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1); 85f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1); 86f7788e8eSJonathan Lemon #include "miibus_if.h" 874fc1dda9SAndrew Gallatin 88ba8c6fd5SDavid Greenman /* 89ba8c6fd5SDavid Greenman * NOTE! On the Alpha, we have an alignment constraint. The 90ba8c6fd5SDavid Greenman * card DMAs the packet immediately following the RFA. However, 91ba8c6fd5SDavid Greenman * the first thing in the packet is a 14-byte Ethernet header. 92ba8c6fd5SDavid Greenman * This means that the packet is misaligned. To compensate, 93ba8c6fd5SDavid Greenman * we actually offset the RFA 2 bytes into the cluster. This 94ba8c6fd5SDavid Greenman * alignes the packet after the Ethernet header at a 32-bit 95ba8c6fd5SDavid Greenman * boundary. HOWEVER! This means that the RFA is misaligned! 96ba8c6fd5SDavid Greenman */ 97ba8c6fd5SDavid Greenman #define RFA_ALIGNMENT_FUDGE 2 98ba8c6fd5SDavid Greenman 99ba8c6fd5SDavid Greenman /* 100f7788e8eSJonathan Lemon * Set initial transmit threshold at 64 (512 bytes). This is 101f7788e8eSJonathan Lemon * increased by 64 (512 bytes) at a time, to maximum of 192 102f7788e8eSJonathan Lemon * (1536 bytes), if an underrun occurs. 103f7788e8eSJonathan Lemon */ 104f7788e8eSJonathan Lemon static int tx_threshold = 64; 105f7788e8eSJonathan Lemon 106f7788e8eSJonathan Lemon /* 107f7788e8eSJonathan Lemon * The configuration byte map has several undefined fields which 108f7788e8eSJonathan Lemon * must be one or must be zero. Set up a template for these bits 109f7788e8eSJonathan Lemon * only, (assuming a 82557 chip) leaving the actual configuration 110f7788e8eSJonathan Lemon * to fxp_init. 111f7788e8eSJonathan Lemon * 112f7788e8eSJonathan Lemon * See struct fxp_cb_config for the bit definitions. 113f7788e8eSJonathan Lemon */ 114f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = { 115f7788e8eSJonathan Lemon 0x0, 0x0, /* cb_status */ 116f7788e8eSJonathan Lemon 0x0, 0x0, /* cb_command */ 117f7788e8eSJonathan Lemon 0x0, 0x0, 0x0, 0x0, /* link_addr */ 118f7788e8eSJonathan Lemon 0x0, /* 0 */ 119f7788e8eSJonathan Lemon 0x0, /* 1 */ 120f7788e8eSJonathan Lemon 0x0, /* 2 */ 121f7788e8eSJonathan Lemon 0x0, /* 3 */ 122f7788e8eSJonathan Lemon 0x0, /* 4 */ 123f7788e8eSJonathan Lemon 0x0, /* 5 */ 124f7788e8eSJonathan Lemon 0x32, /* 6 */ 125f7788e8eSJonathan Lemon 0x0, /* 7 */ 126f7788e8eSJonathan Lemon 0x0, /* 8 */ 127f7788e8eSJonathan Lemon 0x0, /* 9 */ 128f7788e8eSJonathan Lemon 0x6, /* 10 */ 129f7788e8eSJonathan Lemon 0x0, /* 11 */ 130f7788e8eSJonathan Lemon 0x0, /* 12 */ 131f7788e8eSJonathan Lemon 0x0, /* 13 */ 132f7788e8eSJonathan Lemon 0xf2, /* 14 */ 133f7788e8eSJonathan Lemon 0x48, /* 15 */ 134f7788e8eSJonathan Lemon 0x0, /* 16 */ 135f7788e8eSJonathan Lemon 0x40, /* 17 */ 136f7788e8eSJonathan Lemon 0xf0, /* 18 */ 137f7788e8eSJonathan Lemon 0x0, /* 19 */ 138f7788e8eSJonathan Lemon 0x3f, /* 20 */ 139f7788e8eSJonathan Lemon 0x5 /* 21 */ 140f7788e8eSJonathan Lemon }; 141f7788e8eSJonathan Lemon 142f7788e8eSJonathan Lemon struct fxp_ident { 143f7788e8eSJonathan Lemon u_int16_t devid; 144f19fc5d8SJohn Polstra int16_t revid; /* -1 matches anything */ 145f7788e8eSJonathan Lemon char *name; 146f7788e8eSJonathan Lemon }; 147f7788e8eSJonathan Lemon 148f7788e8eSJonathan Lemon /* 149f7788e8eSJonathan Lemon * Claim various Intel PCI device identifiers for this driver. The 150f7788e8eSJonathan Lemon * sub-vendor and sub-device field are extensively used to identify 151f7788e8eSJonathan Lemon * particular variants, but we don't currently differentiate between 152f7788e8eSJonathan Lemon * them. 153f7788e8eSJonathan Lemon */ 154f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = { 155f19fc5d8SJohn Polstra { 0x1029, -1, "Intel 82559 PCI/CardBus Pro/100" }, 156f19fc5d8SJohn Polstra { 0x1030, -1, "Intel 82559 Pro/100 Ethernet" }, 157f19fc5d8SJohn Polstra { 0x1031, -1, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" }, 158f19fc5d8SJohn Polstra { 0x1032, -1, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" }, 159f19fc5d8SJohn Polstra { 0x1033, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" }, 160f19fc5d8SJohn Polstra { 0x1034, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" }, 161f19fc5d8SJohn Polstra { 0x1035, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" }, 162f19fc5d8SJohn Polstra { 0x1036, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" }, 163f19fc5d8SJohn Polstra { 0x1037, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" }, 164f19fc5d8SJohn Polstra { 0x1038, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" }, 165f19fc5d8SJohn Polstra { 0x1039, -1, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" }, 166f19fc5d8SJohn Polstra { 0x103A, -1, "Intel 82801DB (ICH4) Pro/100 Ethernet" }, 167f19fc5d8SJohn Polstra { 0x103B, -1, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" }, 168f19fc5d8SJohn Polstra { 0x103C, -1, "Intel 82801DB (ICH4) Pro/100 Ethernet" }, 169f19fc5d8SJohn Polstra { 0x103D, -1, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" }, 170f19fc5d8SJohn Polstra { 0x103E, -1, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" }, 171f19fc5d8SJohn Polstra { 0x1050, -1, "Intel 82801BA (D865) Pro/100 VE Ethernet" }, 172c2b37819SWarner Losh { 0x1051, -1, "Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" }, 173f19fc5d8SJohn Polstra { 0x1059, -1, "Intel 82551QM Pro/100 M Mobile Connection" }, 174048ca166SMaxime Henrion { 0x1064, -1, "Intel 82562EZ (ICH6)" }, 175f19fc5d8SJohn Polstra { 0x1209, -1, "Intel 82559ER Embedded 10/100 Ethernet" }, 176f19fc5d8SJohn Polstra { 0x1229, 0x01, "Intel 82557 Pro/100 Ethernet" }, 177f19fc5d8SJohn Polstra { 0x1229, 0x02, "Intel 82557 Pro/100 Ethernet" }, 178f19fc5d8SJohn Polstra { 0x1229, 0x03, "Intel 82557 Pro/100 Ethernet" }, 179f19fc5d8SJohn Polstra { 0x1229, 0x04, "Intel 82558 Pro/100 Ethernet" }, 180f19fc5d8SJohn Polstra { 0x1229, 0x05, "Intel 82558 Pro/100 Ethernet" }, 181f19fc5d8SJohn Polstra { 0x1229, 0x06, "Intel 82559 Pro/100 Ethernet" }, 182f19fc5d8SJohn Polstra { 0x1229, 0x07, "Intel 82559 Pro/100 Ethernet" }, 183f19fc5d8SJohn Polstra { 0x1229, 0x08, "Intel 82559 Pro/100 Ethernet" }, 184f19fc5d8SJohn Polstra { 0x1229, 0x09, "Intel 82559ER Pro/100 Ethernet" }, 185f19fc5d8SJohn Polstra { 0x1229, 0x0c, "Intel 82550 Pro/100 Ethernet" }, 186f19fc5d8SJohn Polstra { 0x1229, 0x0d, "Intel 82550 Pro/100 Ethernet" }, 187f19fc5d8SJohn Polstra { 0x1229, 0x0e, "Intel 82550 Pro/100 Ethernet" }, 188f19fc5d8SJohn Polstra { 0x1229, 0x0f, "Intel 82551 Pro/100 Ethernet" }, 189f19fc5d8SJohn Polstra { 0x1229, 0x10, "Intel 82551 Pro/100 Ethernet" }, 190f19fc5d8SJohn Polstra { 0x1229, -1, "Intel 82557/8/9 Pro/100 Ethernet" }, 191f19fc5d8SJohn Polstra { 0x2449, -1, "Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" }, 192f19fc5d8SJohn Polstra { 0, -1, NULL }, 193f7788e8eSJonathan Lemon }; 194f7788e8eSJonathan Lemon 195c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR 196c8bca6dcSBill Paul #define FXP_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 197c8bca6dcSBill Paul #else 198c8bca6dcSBill Paul #define FXP_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 199c8bca6dcSBill Paul #endif 200c8bca6dcSBill Paul 201f7788e8eSJonathan Lemon static int fxp_probe(device_t dev); 202f7788e8eSJonathan Lemon static int fxp_attach(device_t dev); 203f7788e8eSJonathan Lemon static int fxp_detach(device_t dev); 204f7788e8eSJonathan Lemon static int fxp_shutdown(device_t dev); 205f7788e8eSJonathan Lemon static int fxp_suspend(device_t dev); 206f7788e8eSJonathan Lemon static int fxp_resume(device_t dev); 207f7788e8eSJonathan Lemon 208f7788e8eSJonathan Lemon static void fxp_intr(void *xsc); 2094953bccaSNate Lawson static void fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, 2104953bccaSNate Lawson u_int8_t statack, int count); 211f7788e8eSJonathan Lemon static void fxp_init(void *xsc); 2124953bccaSNate Lawson static void fxp_init_body(struct fxp_softc *sc); 213f7788e8eSJonathan Lemon static void fxp_tick(void *xsc); 214f7788e8eSJonathan Lemon static void fxp_start(struct ifnet *ifp); 2154953bccaSNate Lawson static void fxp_start_body(struct ifnet *ifp); 216f7788e8eSJonathan Lemon static void fxp_stop(struct fxp_softc *sc); 217f7788e8eSJonathan Lemon static void fxp_release(struct fxp_softc *sc); 218f7788e8eSJonathan Lemon static int fxp_ioctl(struct ifnet *ifp, u_long command, 219f7788e8eSJonathan Lemon caddr_t data); 220f7788e8eSJonathan Lemon static void fxp_watchdog(struct ifnet *ifp); 221b2badf02SMaxime Henrion static int fxp_add_rfabuf(struct fxp_softc *sc, 222b2badf02SMaxime Henrion struct fxp_rx *rxp); 22309882363SJonathan Lemon static int fxp_mc_addrs(struct fxp_softc *sc); 224f7788e8eSJonathan Lemon static void fxp_mc_setup(struct fxp_softc *sc); 225f7788e8eSJonathan Lemon static u_int16_t fxp_eeprom_getword(struct fxp_softc *sc, int offset, 226f7788e8eSJonathan Lemon int autosize); 22700c4116bSJonathan Lemon static void fxp_eeprom_putword(struct fxp_softc *sc, int offset, 22800c4116bSJonathan Lemon u_int16_t data); 229f7788e8eSJonathan Lemon static void fxp_autosize_eeprom(struct fxp_softc *sc); 230f7788e8eSJonathan Lemon static void fxp_read_eeprom(struct fxp_softc *sc, u_short *data, 231f7788e8eSJonathan Lemon int offset, int words); 23200c4116bSJonathan Lemon static void fxp_write_eeprom(struct fxp_softc *sc, u_short *data, 23300c4116bSJonathan Lemon int offset, int words); 234f7788e8eSJonathan Lemon static int fxp_ifmedia_upd(struct ifnet *ifp); 235f7788e8eSJonathan Lemon static void fxp_ifmedia_sts(struct ifnet *ifp, 236f7788e8eSJonathan Lemon struct ifmediareq *ifmr); 237f7788e8eSJonathan Lemon static int fxp_serial_ifmedia_upd(struct ifnet *ifp); 238f7788e8eSJonathan Lemon static void fxp_serial_ifmedia_sts(struct ifnet *ifp, 239f7788e8eSJonathan Lemon struct ifmediareq *ifmr); 240f7788e8eSJonathan Lemon static volatile int fxp_miibus_readreg(device_t dev, int phy, int reg); 241f7788e8eSJonathan Lemon static void fxp_miibus_writereg(device_t dev, int phy, int reg, 242f7788e8eSJonathan Lemon int value); 24372a32a26SJonathan Lemon static void fxp_load_ucode(struct fxp_softc *sc); 24472a32a26SJonathan Lemon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, 24572a32a26SJonathan Lemon int low, int high); 24672a32a26SJonathan Lemon static int sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS); 24772a32a26SJonathan Lemon static int sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS); 24828935f27SMaxime Henrion static void fxp_scb_wait(struct fxp_softc *sc); 24928935f27SMaxime Henrion static void fxp_scb_cmd(struct fxp_softc *sc, int cmd); 25028935f27SMaxime Henrion static void fxp_dma_wait(struct fxp_softc *sc, 251209b07bcSMaxime Henrion volatile u_int16_t *status, bus_dma_tag_t dmat, 252209b07bcSMaxime Henrion bus_dmamap_t map); 253f7788e8eSJonathan Lemon 254f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = { 255f7788e8eSJonathan Lemon /* Device interface */ 256f7788e8eSJonathan Lemon DEVMETHOD(device_probe, fxp_probe), 257f7788e8eSJonathan Lemon DEVMETHOD(device_attach, fxp_attach), 258f7788e8eSJonathan Lemon DEVMETHOD(device_detach, fxp_detach), 259f7788e8eSJonathan Lemon DEVMETHOD(device_shutdown, fxp_shutdown), 260f7788e8eSJonathan Lemon DEVMETHOD(device_suspend, fxp_suspend), 261f7788e8eSJonathan Lemon DEVMETHOD(device_resume, fxp_resume), 262f7788e8eSJonathan Lemon 263f7788e8eSJonathan Lemon /* MII interface */ 264f7788e8eSJonathan Lemon DEVMETHOD(miibus_readreg, fxp_miibus_readreg), 265f7788e8eSJonathan Lemon DEVMETHOD(miibus_writereg, fxp_miibus_writereg), 266f7788e8eSJonathan Lemon 267f7788e8eSJonathan Lemon { 0, 0 } 268f7788e8eSJonathan Lemon }; 269f7788e8eSJonathan Lemon 270f7788e8eSJonathan Lemon static driver_t fxp_driver = { 271f7788e8eSJonathan Lemon "fxp", 272f7788e8eSJonathan Lemon fxp_methods, 273f7788e8eSJonathan Lemon sizeof(struct fxp_softc), 274f7788e8eSJonathan Lemon }; 275f7788e8eSJonathan Lemon 276f7788e8eSJonathan Lemon static devclass_t fxp_devclass; 277f7788e8eSJonathan Lemon 278f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0); 279347934faSWarner Losh DRIVER_MODULE(fxp, cardbus, fxp_driver, fxp_devclass, 0, 0); 280f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0); 281f7788e8eSJonathan Lemon 282f7788e8eSJonathan Lemon /* 283dfe61cf1SDavid Greenman * Wait for the previous command to be accepted (but not necessarily 284dfe61cf1SDavid Greenman * completed). 285dfe61cf1SDavid Greenman */ 28628935f27SMaxime Henrion static void 287f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc) 288a17c678eSDavid Greenman { 289a17c678eSDavid Greenman int i = 10000; 290a17c678eSDavid Greenman 2917dced78aSDavid Greenman while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i) 2927dced78aSDavid Greenman DELAY(2); 2937dced78aSDavid Greenman if (i == 0) 29400c4116bSJonathan Lemon device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n", 295e8c8b728SJonathan Lemon CSR_READ_1(sc, FXP_CSR_SCB_COMMAND), 296e8c8b728SJonathan Lemon CSR_READ_1(sc, FXP_CSR_SCB_STATACK), 297e8c8b728SJonathan Lemon CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), 298e8c8b728SJonathan Lemon CSR_READ_2(sc, FXP_CSR_FLOWCONTROL)); 2997dced78aSDavid Greenman } 3007dced78aSDavid Greenman 30128935f27SMaxime Henrion static void 3022e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd) 3032e2b8238SJonathan Lemon { 3042e2b8238SJonathan Lemon 3052e2b8238SJonathan Lemon if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) { 3062e2b8238SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP); 3072e2b8238SJonathan Lemon fxp_scb_wait(sc); 3082e2b8238SJonathan Lemon } 3092e2b8238SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd); 3102e2b8238SJonathan Lemon } 3112e2b8238SJonathan Lemon 31228935f27SMaxime Henrion static void 313209b07bcSMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile u_int16_t *status, 314209b07bcSMaxime Henrion bus_dma_tag_t dmat, bus_dmamap_t map) 3157dced78aSDavid Greenman { 3167dced78aSDavid Greenman int i = 10000; 3177dced78aSDavid Greenman 318209b07bcSMaxime Henrion bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD); 319209b07bcSMaxime Henrion while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i) { 3207dced78aSDavid Greenman DELAY(2); 321209b07bcSMaxime Henrion bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD); 322209b07bcSMaxime Henrion } 3237dced78aSDavid Greenman if (i == 0) 324f7788e8eSJonathan Lemon device_printf(sc->dev, "DMA timeout\n"); 325a17c678eSDavid Greenman } 326a17c678eSDavid Greenman 327dfe61cf1SDavid Greenman /* 32828935f27SMaxime Henrion * Return identification string if this device is ours. 329dfe61cf1SDavid Greenman */ 3306182fdbdSPeter Wemm static int 3316182fdbdSPeter Wemm fxp_probe(device_t dev) 332a17c678eSDavid Greenman { 333f7788e8eSJonathan Lemon u_int16_t devid; 334f19fc5d8SJohn Polstra u_int8_t revid; 335f7788e8eSJonathan Lemon struct fxp_ident *ident; 336f7788e8eSJonathan Lemon 33755ce7b51SDavid Greenman if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) { 338f7788e8eSJonathan Lemon devid = pci_get_device(dev); 339f19fc5d8SJohn Polstra revid = pci_get_revid(dev); 340f7788e8eSJonathan Lemon for (ident = fxp_ident_table; ident->name != NULL; ident++) { 341f19fc5d8SJohn Polstra if (ident->devid == devid && 342f19fc5d8SJohn Polstra (ident->revid == revid || ident->revid == -1)) { 343f7788e8eSJonathan Lemon device_set_desc(dev, ident->name); 344f7788e8eSJonathan Lemon return (0); 34555ce7b51SDavid Greenman } 346dd68ef16SPeter Wemm } 347f7788e8eSJonathan Lemon } 348f7788e8eSJonathan Lemon return (ENXIO); 3496182fdbdSPeter Wemm } 3506182fdbdSPeter Wemm 351b2badf02SMaxime Henrion static void 352b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 353b2badf02SMaxime Henrion { 354b2badf02SMaxime Henrion u_int32_t *addr; 355b2badf02SMaxime Henrion 356b2badf02SMaxime Henrion if (error) 357b2badf02SMaxime Henrion return; 358b2badf02SMaxime Henrion 359b2badf02SMaxime Henrion KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 360b2badf02SMaxime Henrion addr = arg; 361b2badf02SMaxime Henrion *addr = segs->ds_addr; 362b2badf02SMaxime Henrion } 363b2badf02SMaxime Henrion 3646182fdbdSPeter Wemm static int 3656182fdbdSPeter Wemm fxp_attach(device_t dev) 366a17c678eSDavid Greenman { 3676720ebccSMaxime Henrion struct fxp_softc *sc; 3686720ebccSMaxime Henrion struct fxp_cb_tx *tcbp; 3696720ebccSMaxime Henrion struct fxp_tx *txp; 370b2badf02SMaxime Henrion struct fxp_rx *rxp; 3716720ebccSMaxime Henrion struct ifnet *ifp; 3729fa6ccfbSMatt Jacob u_int32_t val; 37383e6547dSMaxime Henrion u_int16_t data, myea[ETHER_ADDR_LEN / 2]; 374d73e2e55SMaxime Henrion int i, rid, m1, m2, prefer_iomap, maxtxseg; 3756720ebccSMaxime Henrion int error, s; 376a17c678eSDavid Greenman 3776720ebccSMaxime Henrion error = 0; 3786720ebccSMaxime Henrion sc = device_get_softc(dev); 379f7788e8eSJonathan Lemon sc->dev = dev; 38045276e4aSSam Leffler callout_init(&sc->stat_ch, CALLOUT_MPSAFE); 3816008862bSJohn Baldwin mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 3824953bccaSNate Lawson MTX_DEF); 3834953bccaSNate Lawson ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd, 3844953bccaSNate Lawson fxp_serial_ifmedia_sts); 385a17c678eSDavid Greenman 386f7788e8eSJonathan Lemon s = splimp(); 387a17c678eSDavid Greenman 388dfe61cf1SDavid Greenman /* 3892bce79a2SMaxim Sobolev * Enable bus mastering. 390df373873SWes Peters */ 391cf0d8a1eSMaxim Sobolev pci_enable_busmaster(dev); 3929fa6ccfbSMatt Jacob val = pci_read_config(dev, PCIR_COMMAND, 2); 39379495006SWarner Losh 394df373873SWes Peters /* 3959fa6ccfbSMatt Jacob * Figure out which we should try first - memory mapping or i/o mapping? 3969fa6ccfbSMatt Jacob * We default to memory mapping. Then we accept an override from the 3979fa6ccfbSMatt Jacob * command line. Then we check to see which one is enabled. 398dfe61cf1SDavid Greenman */ 3999fa6ccfbSMatt Jacob m1 = PCIM_CMD_MEMEN; 4009fa6ccfbSMatt Jacob m2 = PCIM_CMD_PORTEN; 4012a05a4ebSMatt Jacob prefer_iomap = 0; 4022a05a4ebSMatt Jacob if (resource_int_value(device_get_name(dev), device_get_unit(dev), 4032a05a4ebSMatt Jacob "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) { 4049fa6ccfbSMatt Jacob m1 = PCIM_CMD_PORTEN; 4059fa6ccfbSMatt Jacob m2 = PCIM_CMD_MEMEN; 4069fa6ccfbSMatt Jacob } 4079fa6ccfbSMatt Jacob 408533294b9SMatthew N. Dodd sc->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT; 4099fa6ccfbSMatt Jacob sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA; 4105f96beb9SNate Lawson sc->mem = bus_alloc_resource_any(dev, sc->rtp, &sc->rgd, RF_ACTIVE); 411533294b9SMatthew N. Dodd if (sc->mem == NULL) { 4129fa6ccfbSMatt Jacob sc->rtp = 4139fa6ccfbSMatt Jacob (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT; 4149fa6ccfbSMatt Jacob sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA; 4155f96beb9SNate Lawson sc->mem = bus_alloc_resource_any(dev, sc->rtp, &sc->rgd, 4165f96beb9SNate Lawson RF_ACTIVE); 4179fa6ccfbSMatt Jacob } 4189fa6ccfbSMatt Jacob 4196182fdbdSPeter Wemm if (!sc->mem) { 4206182fdbdSPeter Wemm error = ENXIO; 421a17c678eSDavid Greenman goto fail; 422a17c678eSDavid Greenman } 4239fa6ccfbSMatt Jacob if (bootverbose) { 4249fa6ccfbSMatt Jacob device_printf(dev, "using %s space register mapping\n", 4259fa6ccfbSMatt Jacob sc->rtp == SYS_RES_MEMORY? "memory" : "I/O"); 4269fa6ccfbSMatt Jacob } 4274fc1dda9SAndrew Gallatin 4284fc1dda9SAndrew Gallatin sc->sc_st = rman_get_bustag(sc->mem); 4294fc1dda9SAndrew Gallatin sc->sc_sh = rman_get_bushandle(sc->mem); 430a17c678eSDavid Greenman 431a17c678eSDavid Greenman /* 432dfe61cf1SDavid Greenman * Allocate our interrupt. 433dfe61cf1SDavid Greenman */ 4346182fdbdSPeter Wemm rid = 0; 4355f96beb9SNate Lawson sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 4366182fdbdSPeter Wemm RF_SHAREABLE | RF_ACTIVE); 4376182fdbdSPeter Wemm if (sc->irq == NULL) { 4386182fdbdSPeter Wemm device_printf(dev, "could not map interrupt\n"); 4396182fdbdSPeter Wemm error = ENXIO; 4406182fdbdSPeter Wemm goto fail; 4416182fdbdSPeter Wemm } 4426182fdbdSPeter Wemm 443f7788e8eSJonathan Lemon /* 444f7788e8eSJonathan Lemon * Reset to a stable state. 445f7788e8eSJonathan Lemon */ 446f7788e8eSJonathan Lemon CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET); 447f7788e8eSJonathan Lemon DELAY(10); 448f7788e8eSJonathan Lemon 449f7788e8eSJonathan Lemon /* 450f7788e8eSJonathan Lemon * Find out how large of an SEEPROM we have. 451f7788e8eSJonathan Lemon */ 452f7788e8eSJonathan Lemon fxp_autosize_eeprom(sc); 453f7788e8eSJonathan Lemon 454f7788e8eSJonathan Lemon /* 45593b6e2e6SMaxime Henrion * Find out the chip revision; lump all 82557 revs together. 45693b6e2e6SMaxime Henrion */ 45793b6e2e6SMaxime Henrion fxp_read_eeprom(sc, &data, 5, 1); 45893b6e2e6SMaxime Henrion if ((data >> 8) == 1) 45993b6e2e6SMaxime Henrion sc->revision = FXP_REV_82557; 46093b6e2e6SMaxime Henrion else 46193b6e2e6SMaxime Henrion sc->revision = pci_get_revid(dev); 46293b6e2e6SMaxime Henrion 46393b6e2e6SMaxime Henrion /* 4643bd07cfdSJonathan Lemon * Determine whether we must use the 503 serial interface. 465f7788e8eSJonathan Lemon */ 466f7788e8eSJonathan Lemon fxp_read_eeprom(sc, &data, 6, 1); 46793b6e2e6SMaxime Henrion if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0 4684ed53076SMaxime Henrion && (data & FXP_PHY_SERIAL_ONLY)) 469dedabebfSJonathan Lemon sc->flags |= FXP_FLAG_SERIAL_MEDIA; 470f7788e8eSJonathan Lemon 4710f1db1d6SMaxime Henrion SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 4720f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 47350a33b6aSPawel Jakub Dawidek OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW, 474858b84f5SPoul-Henning Kamp &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I", 47572a32a26SJonathan Lemon "FXP driver receive interrupt microcode bundling delay"); 4760f1db1d6SMaxime Henrion SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 4770f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 47850a33b6aSPawel Jakub Dawidek OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW, 479858b84f5SPoul-Henning Kamp &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I", 48072a32a26SJonathan Lemon "FXP driver receive interrupt microcode bundle size limit"); 4810f1db1d6SMaxime Henrion SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 4820f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 4830f1db1d6SMaxime Henrion OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0, 4840f1db1d6SMaxime Henrion "FXP RNR events"); 4850f1db1d6SMaxime Henrion SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 4860f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 4870f1db1d6SMaxime Henrion OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0, 4880f1db1d6SMaxime Henrion "FXP flow control disabled"); 48972a32a26SJonathan Lemon 49072a32a26SJonathan Lemon /* 49172a32a26SJonathan Lemon * Pull in device tunables. 49272a32a26SJonathan Lemon */ 49372a32a26SJonathan Lemon sc->tunable_int_delay = TUNABLE_INT_DELAY; 49472a32a26SJonathan Lemon sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX; 49503edfff3SRobert Watson sc->tunable_noflow = 1; 49672a32a26SJonathan Lemon (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 49772a32a26SJonathan Lemon "int_delay", &sc->tunable_int_delay); 49872a32a26SJonathan Lemon (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 49972a32a26SJonathan Lemon "bundle_max", &sc->tunable_bundle_max); 5000f1db1d6SMaxime Henrion (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 5010f1db1d6SMaxime Henrion "noflow", &sc->tunable_noflow); 5020f1db1d6SMaxime Henrion sc->rnr = 0; 50372a32a26SJonathan Lemon 50472a32a26SJonathan Lemon /* 5052e2b8238SJonathan Lemon * Enable workarounds for certain chip revision deficiencies. 50600c4116bSJonathan Lemon * 50772a32a26SJonathan Lemon * Systems based on the ICH2/ICH2-M chip from Intel, and possibly 50872a32a26SJonathan Lemon * some systems based a normal 82559 design, have a defect where 50972a32a26SJonathan Lemon * the chip can cause a PCI protocol violation if it receives 51000c4116bSJonathan Lemon * a CU_RESUME command when it is entering the IDLE state. The 51100c4116bSJonathan Lemon * workaround is to disable Dynamic Standby Mode, so the chip never 51200c4116bSJonathan Lemon * deasserts CLKRUN#, and always remains in an active state. 51300c4116bSJonathan Lemon * 51400c4116bSJonathan Lemon * See Intel 82801BA/82801BAM Specification Update, Errata #30. 5152e2b8238SJonathan Lemon */ 5162e2b8238SJonathan Lemon i = pci_get_device(dev); 51772a32a26SJonathan Lemon if (i == 0x2449 || (i > 0x1030 && i < 0x1039) || 51872a32a26SJonathan Lemon sc->revision >= FXP_REV_82559_A0) { 51900c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, 10, 1); 52000c4116bSJonathan Lemon if (data & 0x02) { /* STB enable */ 52100c4116bSJonathan Lemon u_int16_t cksum; 52200c4116bSJonathan Lemon int i; 52300c4116bSJonathan Lemon 52400c4116bSJonathan Lemon device_printf(dev, 525001cfa92SJonathan Lemon "Disabling dynamic standby mode in EEPROM\n"); 52600c4116bSJonathan Lemon data &= ~0x02; 52700c4116bSJonathan Lemon fxp_write_eeprom(sc, &data, 10, 1); 52800c4116bSJonathan Lemon device_printf(dev, "New EEPROM ID: 0x%x\n", data); 52900c4116bSJonathan Lemon cksum = 0; 53000c4116bSJonathan Lemon for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) { 53100c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, i, 1); 53200c4116bSJonathan Lemon cksum += data; 53300c4116bSJonathan Lemon } 53400c4116bSJonathan Lemon i = (1 << sc->eeprom_size) - 1; 53500c4116bSJonathan Lemon cksum = 0xBABA - cksum; 53600c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, i, 1); 53700c4116bSJonathan Lemon fxp_write_eeprom(sc, &cksum, i, 1); 53800c4116bSJonathan Lemon device_printf(dev, 53900c4116bSJonathan Lemon "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n", 54000c4116bSJonathan Lemon i, data, cksum); 54100c4116bSJonathan Lemon #if 1 54200c4116bSJonathan Lemon /* 54300c4116bSJonathan Lemon * If the user elects to continue, try the software 54400c4116bSJonathan Lemon * workaround, as it is better than nothing. 54500c4116bSJonathan Lemon */ 5462e2b8238SJonathan Lemon sc->flags |= FXP_FLAG_CU_RESUME_BUG; 54700c4116bSJonathan Lemon #endif 54800c4116bSJonathan Lemon } 54900c4116bSJonathan Lemon } 5502e2b8238SJonathan Lemon 5512e2b8238SJonathan Lemon /* 5523bd07cfdSJonathan Lemon * If we are not a 82557 chip, we can enable extended features. 5533bd07cfdSJonathan Lemon */ 55472a32a26SJonathan Lemon if (sc->revision != FXP_REV_82557) { 5553bd07cfdSJonathan Lemon /* 55674396a0aSJonathan Lemon * If MWI is enabled in the PCI configuration, and there 55774396a0aSJonathan Lemon * is a valid cacheline size (8 or 16 dwords), then tell 55874396a0aSJonathan Lemon * the board to turn on MWI. 5593bd07cfdSJonathan Lemon */ 56074396a0aSJonathan Lemon if (val & PCIM_CMD_MWRICEN && 56174396a0aSJonathan Lemon pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0) 5623bd07cfdSJonathan Lemon sc->flags |= FXP_FLAG_MWI_ENABLE; 5633bd07cfdSJonathan Lemon 5643bd07cfdSJonathan Lemon /* turn on the extended TxCB feature */ 5653bd07cfdSJonathan Lemon sc->flags |= FXP_FLAG_EXT_TXCB; 56644e0bc11SYaroslav Tykhiy 56744e0bc11SYaroslav Tykhiy /* enable reception of long frames for VLAN */ 56844e0bc11SYaroslav Tykhiy sc->flags |= FXP_FLAG_LONG_PKT_EN; 56944e0bc11SYaroslav Tykhiy } else { 57044e0bc11SYaroslav Tykhiy /* a hack to get long VLAN frames on a 82557 */ 57144e0bc11SYaroslav Tykhiy sc->flags |= FXP_FLAG_SAVE_BAD; 5723bd07cfdSJonathan Lemon } 5733bd07cfdSJonathan Lemon 5743bd07cfdSJonathan Lemon /* 575c8bca6dcSBill Paul * Enable use of extended RFDs and TCBs for 82550 576c8bca6dcSBill Paul * and later chips. Note: we need extended TXCB support 577c8bca6dcSBill Paul * too, but that's already enabled by the code above. 578c8bca6dcSBill Paul * Be careful to do this only on the right devices. 579c8bca6dcSBill Paul */ 580c8bca6dcSBill Paul 581414ce15cSDon Lewis if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C) { 582c8bca6dcSBill Paul sc->rfa_size = sizeof (struct fxp_rfa); 583c8bca6dcSBill Paul sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT; 584c8bca6dcSBill Paul sc->flags |= FXP_FLAG_EXT_RFA; 585c8bca6dcSBill Paul } else { 586c8bca6dcSBill Paul sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN; 587c8bca6dcSBill Paul sc->tx_cmd = FXP_CB_COMMAND_XMIT; 588c8bca6dcSBill Paul } 589c8bca6dcSBill Paul 590c8bca6dcSBill Paul /* 591b2badf02SMaxime Henrion * Allocate DMA tags and DMA safe memory. 592b2badf02SMaxime Henrion */ 593d73e2e55SMaxime Henrion maxtxseg = sc->flags & FXP_FLAG_EXT_RFA ? FXP_NTXSEG - 1 : FXP_NTXSEG; 594b2badf02SMaxime Henrion error = bus_dma_tag_create(NULL, 2, 0, BUS_SPACE_MAXADDR_32BIT, 595d73e2e55SMaxime Henrion BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * maxtxseg, 596f6b1c44dSScott Long maxtxseg, MCLBYTES, 0, busdma_lock_mutex, &Giant, &sc->fxp_mtag); 597b2badf02SMaxime Henrion if (error) { 598b2badf02SMaxime Henrion device_printf(dev, "could not allocate dma tag\n"); 599b2badf02SMaxime Henrion goto fail; 600b2badf02SMaxime Henrion } 601b2badf02SMaxime Henrion 602b2badf02SMaxime Henrion error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 603b2badf02SMaxime Henrion BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_stats), 1, 604f6b1c44dSScott Long sizeof(struct fxp_stats), 0, busdma_lock_mutex, &Giant, 605f6b1c44dSScott Long &sc->fxp_stag); 606b2badf02SMaxime Henrion if (error) { 607b2badf02SMaxime Henrion device_printf(dev, "could not allocate dma tag\n"); 608b2badf02SMaxime Henrion goto fail; 609b2badf02SMaxime Henrion } 610b2badf02SMaxime Henrion 611b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats, 612aafb3ebbSMaxime Henrion BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap); 613b2badf02SMaxime Henrion if (error) 6144953bccaSNate Lawson goto fail; 615b2badf02SMaxime Henrion error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats, 616b2badf02SMaxime Henrion sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0); 617b2badf02SMaxime Henrion if (error) { 618b2badf02SMaxime Henrion device_printf(dev, "could not map the stats buffer\n"); 619b2badf02SMaxime Henrion goto fail; 620b2badf02SMaxime Henrion } 621b2badf02SMaxime Henrion 622b2badf02SMaxime Henrion error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 623b2badf02SMaxime Henrion BUS_SPACE_MAXADDR, NULL, NULL, FXP_TXCB_SZ, 1, 624f6b1c44dSScott Long FXP_TXCB_SZ, 0, busdma_lock_mutex, &Giant, &sc->cbl_tag); 625b2badf02SMaxime Henrion if (error) { 626b2badf02SMaxime Henrion device_printf(dev, "could not allocate dma tag\n"); 627b2badf02SMaxime Henrion goto fail; 628b2badf02SMaxime Henrion } 629b2badf02SMaxime Henrion 630b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list, 631aafb3ebbSMaxime Henrion BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map); 632b2badf02SMaxime Henrion if (error) 6334953bccaSNate Lawson goto fail; 634b2badf02SMaxime Henrion 635b2badf02SMaxime Henrion error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map, 636b2badf02SMaxime Henrion sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr, 637b2badf02SMaxime Henrion &sc->fxp_desc.cbl_addr, 0); 638b2badf02SMaxime Henrion if (error) { 639b2badf02SMaxime Henrion device_printf(dev, "could not map DMA memory\n"); 640b2badf02SMaxime Henrion goto fail; 641b2badf02SMaxime Henrion } 642b2badf02SMaxime Henrion 643b2badf02SMaxime Henrion error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 644b2badf02SMaxime Henrion BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_cb_mcs), 1, 645f6b1c44dSScott Long sizeof(struct fxp_cb_mcs), 0, busdma_lock_mutex, &Giant, 646f6b1c44dSScott Long &sc->mcs_tag); 647b2badf02SMaxime Henrion if (error) { 648b2badf02SMaxime Henrion device_printf(dev, "could not allocate dma tag\n"); 649b2badf02SMaxime Henrion goto fail; 650b2badf02SMaxime Henrion } 651b2badf02SMaxime Henrion 652b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp, 653b2badf02SMaxime Henrion BUS_DMA_NOWAIT, &sc->mcs_map); 654b2badf02SMaxime Henrion if (error) 6554953bccaSNate Lawson goto fail; 656b2badf02SMaxime Henrion error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp, 657b2badf02SMaxime Henrion sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0); 658b2badf02SMaxime Henrion if (error) { 659b2badf02SMaxime Henrion device_printf(dev, "can't map the multicast setup command\n"); 660b2badf02SMaxime Henrion goto fail; 661b2badf02SMaxime Henrion } 662b2badf02SMaxime Henrion 663b2badf02SMaxime Henrion /* 6646720ebccSMaxime Henrion * Pre-allocate the TX DMA maps and setup the pointers to 6656720ebccSMaxime Henrion * the TX command blocks. 666b2badf02SMaxime Henrion */ 6676720ebccSMaxime Henrion txp = sc->fxp_desc.tx_list; 6686720ebccSMaxime Henrion tcbp = sc->fxp_desc.cbl_list; 6694cec1653SMaxime Henrion for (i = 0; i < FXP_NTXCB; i++) { 6706720ebccSMaxime Henrion txp[i].tx_cb = tcbp + i; 6716720ebccSMaxime Henrion error = bus_dmamap_create(sc->fxp_mtag, 0, &txp[i].tx_map); 672b2badf02SMaxime Henrion if (error) { 673b2badf02SMaxime Henrion device_printf(dev, "can't create DMA map for TX\n"); 674b2badf02SMaxime Henrion goto fail; 675b2badf02SMaxime Henrion } 676b2badf02SMaxime Henrion } 677b2badf02SMaxime Henrion error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map); 678b2badf02SMaxime Henrion if (error) { 679b2badf02SMaxime Henrion device_printf(dev, "can't create spare DMA map\n"); 680b2badf02SMaxime Henrion goto fail; 681b2badf02SMaxime Henrion } 682b2badf02SMaxime Henrion 683b2badf02SMaxime Henrion /* 684b2badf02SMaxime Henrion * Pre-allocate our receive buffers. 685b2badf02SMaxime Henrion */ 686b2badf02SMaxime Henrion sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL; 687b2badf02SMaxime Henrion for (i = 0; i < FXP_NRFABUFS; i++) { 688b2badf02SMaxime Henrion rxp = &sc->fxp_desc.rx_list[i]; 689b2badf02SMaxime Henrion error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map); 690b2badf02SMaxime Henrion if (error) { 691b2badf02SMaxime Henrion device_printf(dev, "can't create DMA map for RX\n"); 692b2badf02SMaxime Henrion goto fail; 693b2badf02SMaxime Henrion } 6944953bccaSNate Lawson if (fxp_add_rfabuf(sc, rxp) != 0) { 6954953bccaSNate Lawson error = ENOMEM; 6964953bccaSNate Lawson goto fail; 6974953bccaSNate Lawson } 698b2badf02SMaxime Henrion } 699b2badf02SMaxime Henrion 700b2badf02SMaxime Henrion /* 701f7788e8eSJonathan Lemon * Read MAC address. 702f7788e8eSJonathan Lemon */ 70383e6547dSMaxime Henrion fxp_read_eeprom(sc, myea, 0, 3); 70483e6547dSMaxime Henrion sc->arpcom.ac_enaddr[0] = myea[0] & 0xff; 70583e6547dSMaxime Henrion sc->arpcom.ac_enaddr[1] = myea[0] >> 8; 70683e6547dSMaxime Henrion sc->arpcom.ac_enaddr[2] = myea[1] & 0xff; 70783e6547dSMaxime Henrion sc->arpcom.ac_enaddr[3] = myea[1] >> 8; 70883e6547dSMaxime Henrion sc->arpcom.ac_enaddr[4] = myea[2] & 0xff; 70983e6547dSMaxime Henrion sc->arpcom.ac_enaddr[5] = myea[2] >> 8; 710f7788e8eSJonathan Lemon if (bootverbose) { 7112e2b8238SJonathan Lemon device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n", 712f7788e8eSJonathan Lemon pci_get_vendor(dev), pci_get_device(dev), 7132e2b8238SJonathan Lemon pci_get_subvendor(dev), pci_get_subdevice(dev), 7142e2b8238SJonathan Lemon pci_get_revid(dev)); 71572a32a26SJonathan Lemon fxp_read_eeprom(sc, &data, 10, 1); 71672a32a26SJonathan Lemon device_printf(dev, "Dynamic Standby mode is %s\n", 71772a32a26SJonathan Lemon data & 0x02 ? "enabled" : "disabled"); 718f7788e8eSJonathan Lemon } 719f7788e8eSJonathan Lemon 720f7788e8eSJonathan Lemon /* 721f7788e8eSJonathan Lemon * If this is only a 10Mbps device, then there is no MII, and 722f7788e8eSJonathan Lemon * the PHY will use a serial interface instead. 723f7788e8eSJonathan Lemon * 724f7788e8eSJonathan Lemon * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter 725f7788e8eSJonathan Lemon * doesn't have a programming interface of any sort. The 726f7788e8eSJonathan Lemon * media is sensed automatically based on how the link partner 727f7788e8eSJonathan Lemon * is configured. This is, in essence, manual configuration. 728f7788e8eSJonathan Lemon */ 729f7788e8eSJonathan Lemon if (sc->flags & FXP_FLAG_SERIAL_MEDIA) { 730f7788e8eSJonathan Lemon ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 731f7788e8eSJonathan Lemon ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL); 732f7788e8eSJonathan Lemon } else { 733f7788e8eSJonathan Lemon if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd, 734f7788e8eSJonathan Lemon fxp_ifmedia_sts)) { 735f7788e8eSJonathan Lemon device_printf(dev, "MII without any PHY!\n"); 7366182fdbdSPeter Wemm error = ENXIO; 737ba8c6fd5SDavid Greenman goto fail; 738a17c678eSDavid Greenman } 739f7788e8eSJonathan Lemon } 740dccee1a1SDavid Greenman 741a17c678eSDavid Greenman ifp = &sc->arpcom.ac_if; 7429bf40edeSBrooks Davis if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 743a330e1f1SGary Palmer ifp->if_baudrate = 100000000; 744fb583156SDavid Greenman ifp->if_init = fxp_init; 745ba8c6fd5SDavid Greenman ifp->if_softc = sc; 746ba8c6fd5SDavid Greenman ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 747ba8c6fd5SDavid Greenman ifp->if_ioctl = fxp_ioctl; 748ba8c6fd5SDavid Greenman ifp->if_start = fxp_start; 749ba8c6fd5SDavid Greenman ifp->if_watchdog = fxp_watchdog; 750a17c678eSDavid Greenman 7515fe9116bSYaroslav Tykhiy ifp->if_capabilities = ifp->if_capenable = 0; 7525fe9116bSYaroslav Tykhiy 753c8bca6dcSBill Paul /* Enable checksum offload for 82550 or better chips */ 754c8bca6dcSBill Paul if (sc->flags & FXP_FLAG_EXT_RFA) { 755c8bca6dcSBill Paul ifp->if_hwassist = FXP_CSUM_FEATURES; 7565fe9116bSYaroslav Tykhiy ifp->if_capabilities |= IFCAP_HWCSUM; 7575fe9116bSYaroslav Tykhiy ifp->if_capenable |= IFCAP_HWCSUM; 758c8bca6dcSBill Paul } 759c8bca6dcSBill Paul 760fb917226SRuslan Ermilov #ifdef DEVICE_POLLING 761fb917226SRuslan Ermilov /* Inform the world we support polling. */ 762fb917226SRuslan Ermilov ifp->if_capabilities |= IFCAP_POLLING; 763fb917226SRuslan Ermilov ifp->if_capenable |= IFCAP_POLLING; 764fb917226SRuslan Ermilov #endif 765fb917226SRuslan Ermilov 766dfe61cf1SDavid Greenman /* 7674953bccaSNate Lawson * Attach the interface. 7684953bccaSNate Lawson */ 7694953bccaSNate Lawson ether_ifattach(ifp, sc->arpcom.ac_enaddr); 7704953bccaSNate Lawson 7714953bccaSNate Lawson /* 772e8c8b728SJonathan Lemon * Tell the upper layer(s) we support long frames. 7735fe9116bSYaroslav Tykhiy * Must appear after the call to ether_ifattach() because 7745fe9116bSYaroslav Tykhiy * ether_ifattach() sets ifi_hdrlen to the default value. 775e8c8b728SJonathan Lemon */ 776e8c8b728SJonathan Lemon ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 777673d9191SSam Leffler ifp->if_capabilities |= IFCAP_VLAN_MTU; 77844e0bc11SYaroslav Tykhiy ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */ 779e8c8b728SJonathan Lemon 780483b9871SDavid Greenman /* 7813114fdb4SDavid Greenman * Let the system queue as many packets as we have available 7823114fdb4SDavid Greenman * TX descriptors. 783483b9871SDavid Greenman */ 7847929aa03SMax Laier IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1); 7857929aa03SMax Laier ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1; 7867929aa03SMax Laier IFQ_SET_READY(&ifp->if_snd); 7874a684684SDavid Greenman 788201afb0eSMaxime Henrion /* 7894953bccaSNate Lawson * Hook our interrupt after all initialization is complete. 790201afb0eSMaxime Henrion */ 791b237430cSSam Leffler error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 792201afb0eSMaxime Henrion fxp_intr, sc, &sc->ih); 793201afb0eSMaxime Henrion if (error) { 794201afb0eSMaxime Henrion device_printf(dev, "could not setup irq\n"); 7954953bccaSNate Lawson ether_ifdetach(&sc->arpcom.ac_if); 796201afb0eSMaxime Henrion goto fail; 797201afb0eSMaxime Henrion } 798201afb0eSMaxime Henrion 799a17c678eSDavid Greenman fail: 800f7788e8eSJonathan Lemon splx(s); 8014953bccaSNate Lawson if (error) 802f7788e8eSJonathan Lemon fxp_release(sc); 803f7788e8eSJonathan Lemon return (error); 804f7788e8eSJonathan Lemon } 805f7788e8eSJonathan Lemon 806f7788e8eSJonathan Lemon /* 8074953bccaSNate Lawson * Release all resources. The softc lock should not be held and the 8084953bccaSNate Lawson * interrupt should already be torn down. 809f7788e8eSJonathan Lemon */ 810f7788e8eSJonathan Lemon static void 811f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc) 812f7788e8eSJonathan Lemon { 813b2badf02SMaxime Henrion struct fxp_rx *rxp; 814b2badf02SMaxime Henrion struct fxp_tx *txp; 815b2badf02SMaxime Henrion int i; 816b2badf02SMaxime Henrion 81767fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_NOTOWNED); 818670f5d73SMaxime Henrion KASSERT(sc->ih == NULL, 819670f5d73SMaxime Henrion ("fxp_release() called with intr handle still active")); 8204953bccaSNate Lawson if (sc->miibus) 8214953bccaSNate Lawson device_delete_child(sc->dev, sc->miibus); 8224953bccaSNate Lawson bus_generic_detach(sc->dev); 8234953bccaSNate Lawson ifmedia_removeall(&sc->sc_media); 824b2badf02SMaxime Henrion if (sc->fxp_desc.cbl_list) { 825b2badf02SMaxime Henrion bus_dmamap_unload(sc->cbl_tag, sc->cbl_map); 826b2badf02SMaxime Henrion bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list, 827b2badf02SMaxime Henrion sc->cbl_map); 828b2badf02SMaxime Henrion } 829b2badf02SMaxime Henrion if (sc->fxp_stats) { 830b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap); 831b2badf02SMaxime Henrion bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap); 832b2badf02SMaxime Henrion } 833b2badf02SMaxime Henrion if (sc->mcsp) { 834b2badf02SMaxime Henrion bus_dmamap_unload(sc->mcs_tag, sc->mcs_map); 835b2badf02SMaxime Henrion bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map); 836b2badf02SMaxime Henrion } 837f7788e8eSJonathan Lemon if (sc->irq) 838f7788e8eSJonathan Lemon bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq); 839f7788e8eSJonathan Lemon if (sc->mem) 840f7788e8eSJonathan Lemon bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem); 841b983c7b3SMaxime Henrion if (sc->fxp_mtag) { 842b983c7b3SMaxime Henrion for (i = 0; i < FXP_NRFABUFS; i++) { 843b983c7b3SMaxime Henrion rxp = &sc->fxp_desc.rx_list[i]; 844b983c7b3SMaxime Henrion if (rxp->rx_mbuf != NULL) { 845b983c7b3SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map, 846b983c7b3SMaxime Henrion BUS_DMASYNC_POSTREAD); 847b983c7b3SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map); 848b983c7b3SMaxime Henrion m_freem(rxp->rx_mbuf); 849b983c7b3SMaxime Henrion } 850b983c7b3SMaxime Henrion bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map); 851b983c7b3SMaxime Henrion } 852b983c7b3SMaxime Henrion bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map); 853b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->fxp_mtag); 854b983c7b3SMaxime Henrion } 855b983c7b3SMaxime Henrion if (sc->fxp_stag) { 856b983c7b3SMaxime Henrion for (i = 0; i < FXP_NTXCB; i++) { 857b983c7b3SMaxime Henrion txp = &sc->fxp_desc.tx_list[i]; 858b983c7b3SMaxime Henrion if (txp->tx_mbuf != NULL) { 859b983c7b3SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, 860b983c7b3SMaxime Henrion BUS_DMASYNC_POSTWRITE); 861b983c7b3SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, txp->tx_map); 862b983c7b3SMaxime Henrion m_freem(txp->tx_mbuf); 863b983c7b3SMaxime Henrion } 864b983c7b3SMaxime Henrion bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map); 865b983c7b3SMaxime Henrion } 866b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->fxp_stag); 867b983c7b3SMaxime Henrion } 868b2badf02SMaxime Henrion if (sc->cbl_tag) 869b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->cbl_tag); 870b2badf02SMaxime Henrion if (sc->mcs_tag) 871b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->mcs_tag); 87272a32a26SJonathan Lemon 8730f4dc94cSChuck Paterson mtx_destroy(&sc->sc_mtx); 8746182fdbdSPeter Wemm } 8756182fdbdSPeter Wemm 8766182fdbdSPeter Wemm /* 8776182fdbdSPeter Wemm * Detach interface. 8786182fdbdSPeter Wemm */ 8796182fdbdSPeter Wemm static int 8806182fdbdSPeter Wemm fxp_detach(device_t dev) 8816182fdbdSPeter Wemm { 8826182fdbdSPeter Wemm struct fxp_softc *sc = device_get_softc(dev); 883f7788e8eSJonathan Lemon int s; 8846182fdbdSPeter Wemm 8854953bccaSNate Lawson FXP_LOCK(sc); 886f7788e8eSJonathan Lemon s = splimp(); 88732cd7a9cSWarner Losh 8881d2945d5SWarner Losh sc->suspended = 1; /* Do same thing as we do for suspend */ 8896182fdbdSPeter Wemm /* 890f7788e8eSJonathan Lemon * Close down routes etc. 8916182fdbdSPeter Wemm */ 892673d9191SSam Leffler ether_ifdetach(&sc->arpcom.ac_if); 89320f0c80fSMaxime Henrion 89420f0c80fSMaxime Henrion /* 89532cd7a9cSWarner Losh * Stop DMA and drop transmit queue, but disable interrupts first. 89620f0c80fSMaxime Henrion */ 89720f0c80fSMaxime Henrion CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 89820f0c80fSMaxime Henrion fxp_stop(sc); 89932cd7a9cSWarner Losh FXP_UNLOCK(sc); 90020f0c80fSMaxime Henrion 9016182fdbdSPeter Wemm /* 9024953bccaSNate Lawson * Unhook interrupt before dropping lock. This is to prevent 9034953bccaSNate Lawson * races with fxp_intr(). 9046182fdbdSPeter Wemm */ 9054953bccaSNate Lawson bus_teardown_intr(sc->dev, sc->irq, sc->ih); 9064953bccaSNate Lawson sc->ih = NULL; 9076182fdbdSPeter Wemm 908f7788e8eSJonathan Lemon splx(s); 9096182fdbdSPeter Wemm 910f7788e8eSJonathan Lemon /* Release our allocated resources. */ 911f7788e8eSJonathan Lemon fxp_release(sc); 912f7788e8eSJonathan Lemon return (0); 913a17c678eSDavid Greenman } 914a17c678eSDavid Greenman 915a17c678eSDavid Greenman /* 9164a684684SDavid Greenman * Device shutdown routine. Called at system shutdown after sync. The 917a17c678eSDavid Greenman * main purpose of this routine is to shut off receiver DMA so that 918a17c678eSDavid Greenman * kernel memory doesn't get clobbered during warmboot. 919a17c678eSDavid Greenman */ 9206182fdbdSPeter Wemm static int 9216182fdbdSPeter Wemm fxp_shutdown(device_t dev) 922a17c678eSDavid Greenman { 9236182fdbdSPeter Wemm /* 9246182fdbdSPeter Wemm * Make sure that DMA is disabled prior to reboot. Not doing 9256182fdbdSPeter Wemm * do could allow DMA to corrupt kernel memory during the 9266182fdbdSPeter Wemm * reboot before the driver initializes. 9276182fdbdSPeter Wemm */ 9286182fdbdSPeter Wemm fxp_stop((struct fxp_softc *) device_get_softc(dev)); 929f7788e8eSJonathan Lemon return (0); 930a17c678eSDavid Greenman } 931a17c678eSDavid Greenman 9327dced78aSDavid Greenman /* 9337dced78aSDavid Greenman * Device suspend routine. Stop the interface and save some PCI 9347dced78aSDavid Greenman * settings in case the BIOS doesn't restore them properly on 9357dced78aSDavid Greenman * resume. 9367dced78aSDavid Greenman */ 9377dced78aSDavid Greenman static int 9387dced78aSDavid Greenman fxp_suspend(device_t dev) 9397dced78aSDavid Greenman { 9407dced78aSDavid Greenman struct fxp_softc *sc = device_get_softc(dev); 941f7788e8eSJonathan Lemon int i, s; 9427dced78aSDavid Greenman 9434953bccaSNate Lawson FXP_LOCK(sc); 944f7788e8eSJonathan Lemon s = splimp(); 9457dced78aSDavid Greenman 9467dced78aSDavid Greenman fxp_stop(sc); 9477dced78aSDavid Greenman 9487dced78aSDavid Greenman for (i = 0; i < 5; i++) 949e27951b2SJohn Baldwin sc->saved_maps[i] = pci_read_config(dev, PCIR_BAR(i), 4); 9507dced78aSDavid Greenman sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4); 9517dced78aSDavid Greenman sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1); 9527dced78aSDavid Greenman sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1); 9537dced78aSDavid Greenman sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1); 9547dced78aSDavid Greenman 9557dced78aSDavid Greenman sc->suspended = 1; 9567dced78aSDavid Greenman 9574953bccaSNate Lawson FXP_UNLOCK(sc); 958f7788e8eSJonathan Lemon splx(s); 959f7788e8eSJonathan Lemon return (0); 9607dced78aSDavid Greenman } 9617dced78aSDavid Greenman 9627dced78aSDavid Greenman /* 9637dced78aSDavid Greenman * Device resume routine. Restore some PCI settings in case the BIOS 9647dced78aSDavid Greenman * doesn't, re-enable busmastering, and restart the interface if 9657dced78aSDavid Greenman * appropriate. 9667dced78aSDavid Greenman */ 9677dced78aSDavid Greenman static int 9687dced78aSDavid Greenman fxp_resume(device_t dev) 9697dced78aSDavid Greenman { 9707dced78aSDavid Greenman struct fxp_softc *sc = device_get_softc(dev); 9717dced78aSDavid Greenman struct ifnet *ifp = &sc->sc_if; 9727dced78aSDavid Greenman u_int16_t pci_command; 973f7788e8eSJonathan Lemon int i, s; 9747dced78aSDavid Greenman 9754953bccaSNate Lawson FXP_LOCK(sc); 976f7788e8eSJonathan Lemon s = splimp(); 97779495006SWarner Losh 9787dced78aSDavid Greenman /* better way to do this? */ 9797dced78aSDavid Greenman for (i = 0; i < 5; i++) 980e27951b2SJohn Baldwin pci_write_config(dev, PCIR_BAR(i), sc->saved_maps[i], 4); 9817dced78aSDavid Greenman pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4); 9827dced78aSDavid Greenman pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1); 9837dced78aSDavid Greenman pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1); 9847dced78aSDavid Greenman pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1); 9857dced78aSDavid Greenman 9867dced78aSDavid Greenman /* reenable busmastering */ 9877dced78aSDavid Greenman pci_command = pci_read_config(dev, PCIR_COMMAND, 2); 9887dced78aSDavid Greenman pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 9897dced78aSDavid Greenman pci_write_config(dev, PCIR_COMMAND, pci_command, 2); 9907dced78aSDavid Greenman 9917dced78aSDavid Greenman CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET); 9927dced78aSDavid Greenman DELAY(10); 9937dced78aSDavid Greenman 9947dced78aSDavid Greenman /* reinitialize interface if necessary */ 9957dced78aSDavid Greenman if (ifp->if_flags & IFF_UP) 9964953bccaSNate Lawson fxp_init_body(sc); 9977dced78aSDavid Greenman 9987dced78aSDavid Greenman sc->suspended = 0; 9997dced78aSDavid Greenman 10004953bccaSNate Lawson FXP_UNLOCK(sc); 1001f7788e8eSJonathan Lemon splx(s); 1002ba8c6fd5SDavid Greenman return (0); 1003f7788e8eSJonathan Lemon } 1004ba8c6fd5SDavid Greenman 100500c4116bSJonathan Lemon static void 100600c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length) 100700c4116bSJonathan Lemon { 100800c4116bSJonathan Lemon u_int16_t reg; 100900c4116bSJonathan Lemon int x; 101000c4116bSJonathan Lemon 101100c4116bSJonathan Lemon /* 101200c4116bSJonathan Lemon * Shift in data. 101300c4116bSJonathan Lemon */ 101400c4116bSJonathan Lemon for (x = 1 << (length - 1); x; x >>= 1) { 101500c4116bSJonathan Lemon if (data & x) 101600c4116bSJonathan Lemon reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI; 101700c4116bSJonathan Lemon else 101800c4116bSJonathan Lemon reg = FXP_EEPROM_EECS; 101900c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 102000c4116bSJonathan Lemon DELAY(1); 102100c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 102200c4116bSJonathan Lemon DELAY(1); 102300c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 102400c4116bSJonathan Lemon DELAY(1); 102500c4116bSJonathan Lemon } 102600c4116bSJonathan Lemon } 102700c4116bSJonathan Lemon 1028f7788e8eSJonathan Lemon /* 1029f7788e8eSJonathan Lemon * Read from the serial EEPROM. Basically, you manually shift in 1030f7788e8eSJonathan Lemon * the read opcode (one bit at a time) and then shift in the address, 1031f7788e8eSJonathan Lemon * and then you shift out the data (all of this one bit at a time). 1032f7788e8eSJonathan Lemon * The word size is 16 bits, so you have to provide the address for 1033f7788e8eSJonathan Lemon * every 16 bits of data. 1034f7788e8eSJonathan Lemon */ 1035f7788e8eSJonathan Lemon static u_int16_t 1036f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize) 1037f7788e8eSJonathan Lemon { 1038f7788e8eSJonathan Lemon u_int16_t reg, data; 1039f7788e8eSJonathan Lemon int x; 1040ba8c6fd5SDavid Greenman 1041f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 1042f7788e8eSJonathan Lemon /* 1043f7788e8eSJonathan Lemon * Shift in read opcode. 1044f7788e8eSJonathan Lemon */ 104500c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3); 1046f7788e8eSJonathan Lemon /* 1047f7788e8eSJonathan Lemon * Shift in address. 1048f7788e8eSJonathan Lemon */ 1049f7788e8eSJonathan Lemon data = 0; 1050f7788e8eSJonathan Lemon for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) { 1051f7788e8eSJonathan Lemon if (offset & x) 1052f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI; 1053f7788e8eSJonathan Lemon else 1054f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS; 1055f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1056f7788e8eSJonathan Lemon DELAY(1); 1057f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 1058f7788e8eSJonathan Lemon DELAY(1); 1059f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1060f7788e8eSJonathan Lemon DELAY(1); 1061f7788e8eSJonathan Lemon reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO; 1062f7788e8eSJonathan Lemon data++; 1063f7788e8eSJonathan Lemon if (autosize && reg == 0) { 1064f7788e8eSJonathan Lemon sc->eeprom_size = data; 1065f7788e8eSJonathan Lemon break; 1066f7788e8eSJonathan Lemon } 1067f7788e8eSJonathan Lemon } 1068f7788e8eSJonathan Lemon /* 1069f7788e8eSJonathan Lemon * Shift out data. 1070f7788e8eSJonathan Lemon */ 1071f7788e8eSJonathan Lemon data = 0; 1072f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS; 1073f7788e8eSJonathan Lemon for (x = 1 << 15; x; x >>= 1) { 1074f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 1075f7788e8eSJonathan Lemon DELAY(1); 1076f7788e8eSJonathan Lemon if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) 1077f7788e8eSJonathan Lemon data |= x; 1078f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1079f7788e8eSJonathan Lemon DELAY(1); 1080f7788e8eSJonathan Lemon } 1081f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 1082f7788e8eSJonathan Lemon DELAY(1); 1083f7788e8eSJonathan Lemon 1084f7788e8eSJonathan Lemon return (data); 1085ba8c6fd5SDavid Greenman } 1086ba8c6fd5SDavid Greenman 108700c4116bSJonathan Lemon static void 108800c4116bSJonathan Lemon fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data) 108900c4116bSJonathan Lemon { 109000c4116bSJonathan Lemon int i; 109100c4116bSJonathan Lemon 109200c4116bSJonathan Lemon /* 109300c4116bSJonathan Lemon * Erase/write enable. 109400c4116bSJonathan Lemon */ 109500c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 109600c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x4, 3); 109700c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size); 109800c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 109900c4116bSJonathan Lemon DELAY(1); 110000c4116bSJonathan Lemon /* 110100c4116bSJonathan Lemon * Shift in write opcode, address, data. 110200c4116bSJonathan Lemon */ 110300c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 110400c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3); 110500c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, offset, sc->eeprom_size); 110600c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, data, 16); 110700c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 110800c4116bSJonathan Lemon DELAY(1); 110900c4116bSJonathan Lemon /* 111000c4116bSJonathan Lemon * Wait for EEPROM to finish up. 111100c4116bSJonathan Lemon */ 111200c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 111300c4116bSJonathan Lemon DELAY(1); 111400c4116bSJonathan Lemon for (i = 0; i < 1000; i++) { 111500c4116bSJonathan Lemon if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) 111600c4116bSJonathan Lemon break; 111700c4116bSJonathan Lemon DELAY(50); 111800c4116bSJonathan Lemon } 111900c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 112000c4116bSJonathan Lemon DELAY(1); 112100c4116bSJonathan Lemon /* 112200c4116bSJonathan Lemon * Erase/write disable. 112300c4116bSJonathan Lemon */ 112400c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 112500c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x4, 3); 112600c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0, sc->eeprom_size); 112700c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 112800c4116bSJonathan Lemon DELAY(1); 112900c4116bSJonathan Lemon } 113000c4116bSJonathan Lemon 1131ba8c6fd5SDavid Greenman /* 1132e9bf2fa7SDavid Greenman * From NetBSD: 1133e9bf2fa7SDavid Greenman * 1134e9bf2fa7SDavid Greenman * Figure out EEPROM size. 1135e9bf2fa7SDavid Greenman * 1136e9bf2fa7SDavid Greenman * 559's can have either 64-word or 256-word EEPROMs, the 558 1137e9bf2fa7SDavid Greenman * datasheet only talks about 64-word EEPROMs, and the 557 datasheet 1138e9bf2fa7SDavid Greenman * talks about the existance of 16 to 256 word EEPROMs. 1139e9bf2fa7SDavid Greenman * 1140e9bf2fa7SDavid Greenman * The only known sizes are 64 and 256, where the 256 version is used 1141e9bf2fa7SDavid Greenman * by CardBus cards to store CIS information. 1142e9bf2fa7SDavid Greenman * 1143e9bf2fa7SDavid Greenman * The address is shifted in msb-to-lsb, and after the last 1144e9bf2fa7SDavid Greenman * address-bit the EEPROM is supposed to output a `dummy zero' bit, 1145e9bf2fa7SDavid Greenman * after which follows the actual data. We try to detect this zero, by 1146e9bf2fa7SDavid Greenman * probing the data-out bit in the EEPROM control register just after 1147e9bf2fa7SDavid Greenman * having shifted in a bit. If the bit is zero, we assume we've 1148e9bf2fa7SDavid Greenman * shifted enough address bits. The data-out should be tri-state, 1149e9bf2fa7SDavid Greenman * before this, which should translate to a logical one. 1150e9bf2fa7SDavid Greenman */ 1151e9bf2fa7SDavid Greenman static void 1152f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc) 1153e9bf2fa7SDavid Greenman { 1154e9bf2fa7SDavid Greenman 1155f7788e8eSJonathan Lemon /* guess maximum size of 256 words */ 1156f7788e8eSJonathan Lemon sc->eeprom_size = 8; 1157f7788e8eSJonathan Lemon 1158f7788e8eSJonathan Lemon /* autosize */ 1159f7788e8eSJonathan Lemon (void) fxp_eeprom_getword(sc, 0, 1); 1160e9bf2fa7SDavid Greenman } 1161f7788e8eSJonathan Lemon 1162ba8c6fd5SDavid Greenman static void 1163f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words) 1164ba8c6fd5SDavid Greenman { 1165f7788e8eSJonathan Lemon int i; 1166ba8c6fd5SDavid Greenman 1167f7788e8eSJonathan Lemon for (i = 0; i < words; i++) 1168f7788e8eSJonathan Lemon data[i] = fxp_eeprom_getword(sc, offset + i, 0); 1169ba8c6fd5SDavid Greenman } 1170ba8c6fd5SDavid Greenman 117100c4116bSJonathan Lemon static void 117200c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words) 117300c4116bSJonathan Lemon { 117400c4116bSJonathan Lemon int i; 117500c4116bSJonathan Lemon 117600c4116bSJonathan Lemon for (i = 0; i < words; i++) 117700c4116bSJonathan Lemon fxp_eeprom_putword(sc, offset + i, data[i]); 117800c4116bSJonathan Lemon } 117900c4116bSJonathan Lemon 1180b2badf02SMaxime Henrion static void 1181b2badf02SMaxime Henrion fxp_dma_map_txbuf(void *arg, bus_dma_segment_t *segs, int nseg, 1182b2badf02SMaxime Henrion bus_size_t mapsize, int error) 1183b2badf02SMaxime Henrion { 1184b2badf02SMaxime Henrion struct fxp_softc *sc; 1185b2badf02SMaxime Henrion struct fxp_cb_tx *txp; 1186b2badf02SMaxime Henrion int i; 1187b2badf02SMaxime Henrion 1188b2badf02SMaxime Henrion if (error) 1189b2badf02SMaxime Henrion return; 1190b2badf02SMaxime Henrion 1191b2badf02SMaxime Henrion KASSERT(nseg <= FXP_NTXSEG, ("too many DMA segments")); 1192b2badf02SMaxime Henrion 1193b2badf02SMaxime Henrion sc = arg; 1194b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_last->tx_next->tx_cb; 1195b2badf02SMaxime Henrion for (i = 0; i < nseg; i++) { 1196b2badf02SMaxime Henrion KASSERT(segs[i].ds_len <= MCLBYTES, ("segment size too large")); 1197b2badf02SMaxime Henrion /* 1198b2badf02SMaxime Henrion * If this is an 82550/82551, then we're using extended 1199b2badf02SMaxime Henrion * TxCBs _and_ we're using checksum offload. This means 1200b2badf02SMaxime Henrion * that the TxCB is really an IPCB. One major difference 1201b2badf02SMaxime Henrion * between the two is that with plain extended TxCBs, 1202b2badf02SMaxime Henrion * the bottom half of the TxCB contains two entries from 1203b2badf02SMaxime Henrion * the TBD array, whereas IPCBs contain just one entry: 1204b2badf02SMaxime Henrion * one entry (8 bytes) has been sacrificed for the TCP/IP 1205b2badf02SMaxime Henrion * checksum offload control bits. So to make things work 1206b2badf02SMaxime Henrion * right, we have to start filling in the TBD array 1207b2badf02SMaxime Henrion * starting from a different place depending on whether 1208b2badf02SMaxime Henrion * the chip is an 82550/82551 or not. 1209b2badf02SMaxime Henrion */ 1210b2badf02SMaxime Henrion if (sc->flags & FXP_FLAG_EXT_RFA) { 121183e6547dSMaxime Henrion txp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr); 121283e6547dSMaxime Henrion txp->tbd[i + 1].tb_size = htole32(segs[i].ds_len); 1213b2badf02SMaxime Henrion } else { 121483e6547dSMaxime Henrion txp->tbd[i].tb_addr = htole32(segs[i].ds_addr); 121583e6547dSMaxime Henrion txp->tbd[i].tb_size = htole32(segs[i].ds_len); 1216b2badf02SMaxime Henrion } 1217b2badf02SMaxime Henrion } 1218b2badf02SMaxime Henrion txp->tbd_number = nseg; 1219b2badf02SMaxime Henrion } 1220b2badf02SMaxime Henrion 1221a17c678eSDavid Greenman /* 12224953bccaSNate Lawson * Grab the softc lock and call the real fxp_start_body() routine 1223a17c678eSDavid Greenman */ 1224a17c678eSDavid Greenman static void 1225f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp) 1226a17c678eSDavid Greenman { 12279b44ff22SGarrett Wollman struct fxp_softc *sc = ifp->if_softc; 12284953bccaSNate Lawson 12294953bccaSNate Lawson FXP_LOCK(sc); 12304953bccaSNate Lawson fxp_start_body(ifp); 12314953bccaSNate Lawson FXP_UNLOCK(sc); 12324953bccaSNate Lawson } 12334953bccaSNate Lawson 12344953bccaSNate Lawson /* 12354953bccaSNate Lawson * Start packet transmission on the interface. 12364953bccaSNate Lawson * This routine must be called with the softc lock held, and is an 12374953bccaSNate Lawson * internal entry point only. 12384953bccaSNate Lawson */ 12394953bccaSNate Lawson static void 12404953bccaSNate Lawson fxp_start_body(struct ifnet *ifp) 12414953bccaSNate Lawson { 12424953bccaSNate Lawson struct fxp_softc *sc = ifp->if_softc; 124350d81222SMaxime Henrion struct fxp_tx *txp; 1244b2badf02SMaxime Henrion struct mbuf *mb_head; 1245b2badf02SMaxime Henrion int error; 1246a17c678eSDavid Greenman 124767fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 1248a17c678eSDavid Greenman /* 1249483b9871SDavid Greenman * See if we need to suspend xmit until the multicast filter 1250483b9871SDavid Greenman * has been reprogrammed (which can only be done at the head 1251483b9871SDavid Greenman * of the command chain). 1252a17c678eSDavid Greenman */ 12530f4dc94cSChuck Paterson if (sc->need_mcsetup) { 1254a17c678eSDavid Greenman return; 12550f4dc94cSChuck Paterson } 12561cd443acSDavid Greenman 1257483b9871SDavid Greenman txp = NULL; 1258483b9871SDavid Greenman 1259483b9871SDavid Greenman /* 1260483b9871SDavid Greenman * We're finished if there is nothing more to add to the list or if 1261483b9871SDavid Greenman * we're all filled up with buffers to transmit. 12623114fdb4SDavid Greenman * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add 12633114fdb4SDavid Greenman * a NOP command when needed. 1264483b9871SDavid Greenman */ 12657929aa03SMax Laier while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 12667929aa03SMax Laier sc->tx_queued < FXP_NTXCB - 1) { 1267483b9871SDavid Greenman 1268dfe61cf1SDavid Greenman /* 1269dfe61cf1SDavid Greenman * Grab a packet to transmit. 1270dfe61cf1SDavid Greenman */ 12717929aa03SMax Laier IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head); 12727929aa03SMax Laier if (mb_head == NULL) 12737929aa03SMax Laier break; 1274a17c678eSDavid Greenman 1275dfe61cf1SDavid Greenman /* 1276483b9871SDavid Greenman * Get pointer to next available tx desc. 1277dfe61cf1SDavid Greenman */ 1278b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_last->tx_next; 1279c8bca6dcSBill Paul 1280c8bca6dcSBill Paul /* 1281a35e7eaaSDon Lewis * A note in Appendix B of the Intel 8255x 10/100 Mbps 1282a35e7eaaSDon Lewis * Ethernet Controller Family Open Source Software 1283a35e7eaaSDon Lewis * Developer Manual says: 1284a35e7eaaSDon Lewis * Using software parsing is only allowed with legal 1285a35e7eaaSDon Lewis * TCP/IP or UDP/IP packets. 1286a35e7eaaSDon Lewis * ... 1287a35e7eaaSDon Lewis * For all other datagrams, hardware parsing must 1288a35e7eaaSDon Lewis * be used. 1289a35e7eaaSDon Lewis * Software parsing appears to truncate ICMP and 1290a35e7eaaSDon Lewis * fragmented UDP packets that contain one to three 1291a35e7eaaSDon Lewis * bytes in the second (and final) mbuf of the packet. 1292a35e7eaaSDon Lewis */ 1293a35e7eaaSDon Lewis if (sc->flags & FXP_FLAG_EXT_RFA) 1294a35e7eaaSDon Lewis txp->tx_cb->ipcb_ip_activation_high = 1295a35e7eaaSDon Lewis FXP_IPCB_HARDWAREPARSING_ENABLE; 1296a35e7eaaSDon Lewis 1297a35e7eaaSDon Lewis /* 1298c8bca6dcSBill Paul * Deal with TCP/IP checksum offload. Note that 1299c8bca6dcSBill Paul * in order for TCP checksum offload to work, 1300c8bca6dcSBill Paul * the pseudo header checksum must have already 1301c8bca6dcSBill Paul * been computed and stored in the checksum field 1302c8bca6dcSBill Paul * in the TCP header. The stack should have 1303c8bca6dcSBill Paul * already done this for us. 1304c8bca6dcSBill Paul */ 1305c8bca6dcSBill Paul 1306c8bca6dcSBill Paul if (mb_head->m_pkthdr.csum_flags) { 1307c8bca6dcSBill Paul if (mb_head->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1308b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_schedule = 1309c8bca6dcSBill Paul FXP_IPCB_TCPUDP_CHECKSUM_ENABLE; 1310c8bca6dcSBill Paul if (mb_head->m_pkthdr.csum_flags & CSUM_TCP) 1311b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_schedule |= 1312c8bca6dcSBill Paul FXP_IPCB_TCP_PACKET; 1313c8bca6dcSBill Paul } 1314c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR 1315c8bca6dcSBill Paul /* 1316c8bca6dcSBill Paul * XXX The 82550 chip appears to have trouble 1317c8bca6dcSBill Paul * dealing with IP header checksums in very small 1318c8bca6dcSBill Paul * datagrams, namely fragments from 1 to 3 bytes 1319c8bca6dcSBill Paul * in size. For example, say you want to transmit 1320c8bca6dcSBill Paul * a UDP packet of 1473 bytes. The packet will be 1321c8bca6dcSBill Paul * fragmented over two IP datagrams, the latter 1322c8bca6dcSBill Paul * containing only one byte of data. The 82550 will 1323c8bca6dcSBill Paul * botch the header checksum on the 1-byte fragment. 1324c8bca6dcSBill Paul * As long as the datagram contains 4 or more bytes 1325c8bca6dcSBill Paul * of data, you're ok. 1326c8bca6dcSBill Paul * 1327c8bca6dcSBill Paul * The following code attempts to work around this 1328c8bca6dcSBill Paul * problem: if the datagram is less than 38 bytes 1329c8bca6dcSBill Paul * in size (14 bytes ether header, 20 bytes IP header, 1330c8bca6dcSBill Paul * plus 4 bytes of data), we punt and compute the IP 1331c8bca6dcSBill Paul * header checksum by hand. This workaround doesn't 1332c8bca6dcSBill Paul * work very well, however, since it can be fooled 1333c8bca6dcSBill Paul * by things like VLAN tags and IP options that make 1334c8bca6dcSBill Paul * the header sizes/offsets vary. 1335c8bca6dcSBill Paul */ 1336c8bca6dcSBill Paul 1337c8bca6dcSBill Paul if (mb_head->m_pkthdr.csum_flags & CSUM_IP) { 1338c8bca6dcSBill Paul if (mb_head->m_pkthdr.len < 38) { 1339c8bca6dcSBill Paul struct ip *ip; 1340c8bca6dcSBill Paul mb_head->m_data += ETHER_HDR_LEN; 1341c8bca6dcSBill Paul ip = mtod(mb_head, struct ip *); 1342c8bca6dcSBill Paul ip->ip_sum = in_cksum(mb_head, 1343c8bca6dcSBill Paul ip->ip_hl << 2); 1344c8bca6dcSBill Paul mb_head->m_data -= ETHER_HDR_LEN; 1345c8bca6dcSBill Paul } else { 1346b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_activation_high = 1347c8bca6dcSBill Paul FXP_IPCB_HARDWAREPARSING_ENABLE; 1348b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_schedule |= 1349c8bca6dcSBill Paul FXP_IPCB_IP_CHECKSUM_ENABLE; 1350c8bca6dcSBill Paul } 1351c8bca6dcSBill Paul } 1352c8bca6dcSBill Paul #endif 1353c8bca6dcSBill Paul } 1354c8bca6dcSBill Paul 1355c8bca6dcSBill Paul /* 1356a17c678eSDavid Greenman * Go through each of the mbufs in the chain and initialize 1357483b9871SDavid Greenman * the transmit buffer descriptors with the physical address 1358a17c678eSDavid Greenman * and size of the mbuf. 1359a17c678eSDavid Greenman */ 1360b2badf02SMaxime Henrion error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map, 1361b2badf02SMaxime Henrion mb_head, fxp_dma_map_txbuf, sc, 0); 1362b2badf02SMaxime Henrion 1363b2badf02SMaxime Henrion if (error && error != EFBIG) { 1364b2badf02SMaxime Henrion device_printf(sc->dev, "can't map mbuf (error %d)\n", 1365b2badf02SMaxime Henrion error); 1366b2badf02SMaxime Henrion m_freem(mb_head); 1367a17c678eSDavid Greenman break; 1368a17c678eSDavid Greenman } 1369b2badf02SMaxime Henrion 1370b2badf02SMaxime Henrion if (error) { 137123a0ed7cSDavid Greenman struct mbuf *mn; 137223a0ed7cSDavid Greenman 1373a17c678eSDavid Greenman /* 13743bd07cfdSJonathan Lemon * We ran out of segments. We have to recopy this 13753bd07cfdSJonathan Lemon * mbuf chain first. Bail out if we can't get the 13763bd07cfdSJonathan Lemon * new buffers. 1377a17c678eSDavid Greenman */ 13781104779bSMike Silbersack mn = m_defrag(mb_head, M_DONTWAIT); 137923a0ed7cSDavid Greenman if (mn == NULL) { 138023a0ed7cSDavid Greenman m_freem(mb_head); 1381483b9871SDavid Greenman break; 13821104779bSMike Silbersack } else { 138323a0ed7cSDavid Greenman mb_head = mn; 13841104779bSMike Silbersack } 1385b2badf02SMaxime Henrion error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map, 1386b2badf02SMaxime Henrion mb_head, fxp_dma_map_txbuf, sc, 0); 1387b2badf02SMaxime Henrion if (error) { 1388b2badf02SMaxime Henrion device_printf(sc->dev, 1389b2badf02SMaxime Henrion "can't map mbuf (error %d)\n", error); 1390b2badf02SMaxime Henrion m_freem(mb_head); 1391b2badf02SMaxime Henrion break; 1392b2badf02SMaxime Henrion } 139323a0ed7cSDavid Greenman } 139423a0ed7cSDavid Greenman 1395b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, 1396b2badf02SMaxime Henrion BUS_DMASYNC_PREWRITE); 1397b2badf02SMaxime Henrion 1398b2badf02SMaxime Henrion txp->tx_mbuf = mb_head; 1399b2badf02SMaxime Henrion txp->tx_cb->cb_status = 0; 1400b2badf02SMaxime Henrion txp->tx_cb->byte_count = 0; 14013114fdb4SDavid Greenman if (sc->tx_queued != FXP_CXINT_THRESH - 1) { 1402b2badf02SMaxime Henrion txp->tx_cb->cb_command = 140383e6547dSMaxime Henrion htole16(sc->tx_cmd | FXP_CB_COMMAND_SF | 140483e6547dSMaxime Henrion FXP_CB_COMMAND_S); 14053114fdb4SDavid Greenman } else { 1406b2badf02SMaxime Henrion txp->tx_cb->cb_command = 140783e6547dSMaxime Henrion htole16(sc->tx_cmd | FXP_CB_COMMAND_SF | 140883e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 14093114fdb4SDavid Greenman /* 14103bd07cfdSJonathan Lemon * Set a 5 second timer just in case we don't hear 14113bd07cfdSJonathan Lemon * from the card again. 14123114fdb4SDavid Greenman */ 14133114fdb4SDavid Greenman ifp->if_timer = 5; 14143114fdb4SDavid Greenman } 1415b2badf02SMaxime Henrion txp->tx_cb->tx_threshold = tx_threshold; 1416a17c678eSDavid Greenman 1417a17c678eSDavid Greenman /* 1418483b9871SDavid Greenman * Advance the end of list forward. 1419a17c678eSDavid Greenman */ 142006175228SAndrew Gallatin 142150d81222SMaxime Henrion #ifdef __alpha__ 142206175228SAndrew Gallatin /* 142306175228SAndrew Gallatin * On platforms which can't access memory in 16-bit 142406175228SAndrew Gallatin * granularities, we must prevent the card from DMA'ing 142506175228SAndrew Gallatin * up the status while we update the command field. 142606175228SAndrew Gallatin * This could cause us to overwrite the completion status. 142714fd1071SMaxime Henrion * XXX This is probably bogus and we're _not_ looking 142814fd1071SMaxime Henrion * for atomicity here. 142906175228SAndrew Gallatin */ 143014fd1071SMaxime Henrion atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command, 1431bafb64afSMaxime Henrion htole16(FXP_CB_COMMAND_S)); 143250d81222SMaxime Henrion #else 1433bafb64afSMaxime Henrion sc->fxp_desc.tx_last->tx_cb->cb_command &= 1434bafb64afSMaxime Henrion htole16(~FXP_CB_COMMAND_S); 143550d81222SMaxime Henrion #endif /*__alpha__*/ 1436b2badf02SMaxime Henrion sc->fxp_desc.tx_last = txp; 1437a17c678eSDavid Greenman 1438a17c678eSDavid Greenman /* 14391cd443acSDavid Greenman * Advance the beginning of the list forward if there are 1440b2badf02SMaxime Henrion * no other packets queued (when nothing is queued, tx_first 1441483b9871SDavid Greenman * sits on the last TxCB that was sent out). 1442a17c678eSDavid Greenman */ 14431cd443acSDavid Greenman if (sc->tx_queued == 0) 1444b2badf02SMaxime Henrion sc->fxp_desc.tx_first = txp; 1445a17c678eSDavid Greenman 14461cd443acSDavid Greenman sc->tx_queued++; 14471cd443acSDavid Greenman 1448a17c678eSDavid Greenman /* 1449a17c678eSDavid Greenman * Pass packet to bpf if there is a listener. 1450a17c678eSDavid Greenman */ 1451673d9191SSam Leffler BPF_MTAP(ifp, mb_head); 1452483b9871SDavid Greenman } 1453b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 1454483b9871SDavid Greenman 1455483b9871SDavid Greenman /* 1456483b9871SDavid Greenman * We're finished. If we added to the list, issue a RESUME to get DMA 1457483b9871SDavid Greenman * going again if suspended. 1458483b9871SDavid Greenman */ 1459483b9871SDavid Greenman if (txp != NULL) { 1460483b9871SDavid Greenman fxp_scb_wait(sc); 14612e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME); 1462483b9871SDavid Greenman } 1463a17c678eSDavid Greenman } 1464a17c678eSDavid Greenman 1465e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 1466e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll; 1467e4fc250cSLuigi Rizzo 1468e4fc250cSLuigi Rizzo static void 1469e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1470e4fc250cSLuigi Rizzo { 1471e4fc250cSLuigi Rizzo struct fxp_softc *sc = ifp->if_softc; 1472e4fc250cSLuigi Rizzo u_int8_t statack; 1473e4fc250cSLuigi Rizzo 14744953bccaSNate Lawson FXP_LOCK(sc); 1475fb917226SRuslan Ermilov if (!(ifp->if_capenable & IFCAP_POLLING)) { 1476fb917226SRuslan Ermilov ether_poll_deregister(ifp); 1477fb917226SRuslan Ermilov cmd = POLL_DEREGISTER; 1478fb917226SRuslan Ermilov } 1479e4fc250cSLuigi Rizzo if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ 1480e4fc250cSLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0); 14814953bccaSNate Lawson FXP_UNLOCK(sc); 1482e4fc250cSLuigi Rizzo return; 1483e4fc250cSLuigi Rizzo } 1484e4fc250cSLuigi Rizzo statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA | 1485e4fc250cSLuigi Rizzo FXP_SCB_STATACK_FR; 1486e4fc250cSLuigi Rizzo if (cmd == POLL_AND_CHECK_STATUS) { 1487e4fc250cSLuigi Rizzo u_int8_t tmp; 14886481f301SPeter Wemm 1489e4fc250cSLuigi Rizzo tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK); 14904953bccaSNate Lawson if (tmp == 0xff || tmp == 0) { 14914953bccaSNate Lawson FXP_UNLOCK(sc); 1492e4fc250cSLuigi Rizzo return; /* nothing to do */ 14934953bccaSNate Lawson } 1494e4fc250cSLuigi Rizzo tmp &= ~statack; 1495e4fc250cSLuigi Rizzo /* ack what we can */ 1496e4fc250cSLuigi Rizzo if (tmp != 0) 1497e4fc250cSLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp); 1498e4fc250cSLuigi Rizzo statack |= tmp; 1499e4fc250cSLuigi Rizzo } 15004953bccaSNate Lawson fxp_intr_body(sc, ifp, statack, count); 15014953bccaSNate Lawson FXP_UNLOCK(sc); 1502e4fc250cSLuigi Rizzo } 1503e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 1504e4fc250cSLuigi Rizzo 1505a17c678eSDavid Greenman /* 15069c7d2607SDavid Greenman * Process interface interrupts. 1507a17c678eSDavid Greenman */ 150894927790SDavid Greenman static void 1509f7788e8eSJonathan Lemon fxp_intr(void *xsc) 1510a17c678eSDavid Greenman { 1511f7788e8eSJonathan Lemon struct fxp_softc *sc = xsc; 15124953bccaSNate Lawson struct ifnet *ifp = &sc->sc_if; 15131cd443acSDavid Greenman u_int8_t statack; 15140f4dc94cSChuck Paterson 15154953bccaSNate Lawson FXP_LOCK(sc); 1516704d1965SWarner Losh if (sc->suspended) { 1517704d1965SWarner Losh FXP_UNLOCK(sc); 1518704d1965SWarner Losh return; 1519704d1965SWarner Losh } 1520704d1965SWarner Losh 1521e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 15224953bccaSNate Lawson if (ifp->if_flags & IFF_POLLING) { 15234953bccaSNate Lawson FXP_UNLOCK(sc); 1524e4fc250cSLuigi Rizzo return; 15254953bccaSNate Lawson } 1526fb917226SRuslan Ermilov if ((ifp->if_capenable & IFCAP_POLLING) && 1527fb917226SRuslan Ermilov ether_poll_register(fxp_poll, ifp)) { 1528e4fc250cSLuigi Rizzo /* disable interrupts */ 1529e4fc250cSLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 15304953bccaSNate Lawson FXP_UNLOCK(sc); 1531c660bdfaSJohn Baldwin fxp_poll(ifp, 0, 1); 1532e4fc250cSLuigi Rizzo return; 1533e4fc250cSLuigi Rizzo } 1534e4fc250cSLuigi Rizzo #endif 1535b184b38eSDavid Greenman while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) { 1536a17c678eSDavid Greenman /* 153711457bbfSJonathan Lemon * It should not be possible to have all bits set; the 153811457bbfSJonathan Lemon * FXP_SCB_INTR_SWI bit always returns 0 on a read. If 153911457bbfSJonathan Lemon * all bits are set, this may indicate that the card has 154011457bbfSJonathan Lemon * been physically ejected, so ignore it. 154111457bbfSJonathan Lemon */ 15424953bccaSNate Lawson if (statack == 0xff) { 15434953bccaSNate Lawson FXP_UNLOCK(sc); 154411457bbfSJonathan Lemon return; 15454953bccaSNate Lawson } 154611457bbfSJonathan Lemon 154711457bbfSJonathan Lemon /* 1548a17c678eSDavid Greenman * First ACK all the interrupts in this pass. 1549a17c678eSDavid Greenman */ 1550ba8c6fd5SDavid Greenman CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack); 15514953bccaSNate Lawson fxp_intr_body(sc, ifp, statack, -1); 1552e4fc250cSLuigi Rizzo } 15534953bccaSNate Lawson FXP_UNLOCK(sc); 1554e4fc250cSLuigi Rizzo } 1555e4fc250cSLuigi Rizzo 1556e4fc250cSLuigi Rizzo static void 1557b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc) 1558b2badf02SMaxime Henrion { 1559b2badf02SMaxime Henrion struct fxp_tx *txp; 1560b2badf02SMaxime Henrion 1561b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD); 1562b2badf02SMaxime Henrion for (txp = sc->fxp_desc.tx_first; sc->tx_queued && 156383e6547dSMaxime Henrion (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0; 1564b2badf02SMaxime Henrion txp = txp->tx_next) { 1565b2badf02SMaxime Henrion if (txp->tx_mbuf != NULL) { 1566b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, 1567b2badf02SMaxime Henrion BUS_DMASYNC_POSTWRITE); 1568b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, txp->tx_map); 1569b2badf02SMaxime Henrion m_freem(txp->tx_mbuf); 1570b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 1571b2badf02SMaxime Henrion /* clear this to reset csum offload bits */ 1572b2badf02SMaxime Henrion txp->tx_cb->tbd[0].tb_addr = 0; 1573b2badf02SMaxime Henrion } 1574b2badf02SMaxime Henrion sc->tx_queued--; 1575b2badf02SMaxime Henrion } 1576b2badf02SMaxime Henrion sc->fxp_desc.tx_first = txp; 1577b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 1578b2badf02SMaxime Henrion } 1579b2badf02SMaxime Henrion 1580b2badf02SMaxime Henrion static void 15814953bccaSNate Lawson fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, u_int8_t statack, 15824953bccaSNate Lawson int count) 1583e4fc250cSLuigi Rizzo { 15842b5989e9SLuigi Rizzo struct mbuf *m; 1585b2badf02SMaxime Henrion struct fxp_rx *rxp; 15862b5989e9SLuigi Rizzo struct fxp_rfa *rfa; 15872b5989e9SLuigi Rizzo int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0; 15882b5989e9SLuigi Rizzo 158967fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 15902b5989e9SLuigi Rizzo if (rnr) 15910f1db1d6SMaxime Henrion sc->rnr++; 1592947e3815SIan Dowse #ifdef DEVICE_POLLING 1593947e3815SIan Dowse /* Pick up a deferred RNR condition if `count' ran out last time. */ 1594947e3815SIan Dowse if (sc->flags & FXP_FLAG_DEFERRED_RNR) { 1595947e3815SIan Dowse sc->flags &= ~FXP_FLAG_DEFERRED_RNR; 1596947e3815SIan Dowse rnr = 1; 1597947e3815SIan Dowse } 1598947e3815SIan Dowse #endif 1599a17c678eSDavid Greenman 1600a17c678eSDavid Greenman /* 16013114fdb4SDavid Greenman * Free any finished transmit mbuf chains. 160206936301SBill Paul * 160306936301SBill Paul * Handle the CNA event likt a CXTNO event. It used to 160406936301SBill Paul * be that this event (control unit not ready) was not 160506936301SBill Paul * encountered, but it is now with the SMPng modifications. 160606936301SBill Paul * The exact sequence of events that occur when the interface 160706936301SBill Paul * is brought up are different now, and if this event 160806936301SBill Paul * goes unhandled, the configuration/rxfilter setup sequence 160906936301SBill Paul * can stall for several seconds. The result is that no 161006936301SBill Paul * packets go out onto the wire for about 5 to 10 seconds 161106936301SBill Paul * after the interface is ifconfig'ed for the first time. 16123114fdb4SDavid Greenman */ 161306936301SBill Paul if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) { 1614b2badf02SMaxime Henrion fxp_txeof(sc); 16153114fdb4SDavid Greenman 161641aa0ba2SLuigi Rizzo ifp->if_timer = 0; 1617e2102ae4SMike Silbersack if (sc->tx_queued == 0) { 16183114fdb4SDavid Greenman if (sc->need_mcsetup) 16193114fdb4SDavid Greenman fxp_mc_setup(sc); 1620e2102ae4SMike Silbersack } 16213114fdb4SDavid Greenman /* 16223114fdb4SDavid Greenman * Try to start more packets transmitting. 16233114fdb4SDavid Greenman */ 16247929aa03SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 16254953bccaSNate Lawson fxp_start_body(ifp); 16263114fdb4SDavid Greenman } 16272b5989e9SLuigi Rizzo 16282b5989e9SLuigi Rizzo /* 16292b5989e9SLuigi Rizzo * Just return if nothing happened on the receive side. 16302b5989e9SLuigi Rizzo */ 1631947e3815SIan Dowse if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0) 16322b5989e9SLuigi Rizzo return; 16332b5989e9SLuigi Rizzo 16343114fdb4SDavid Greenman /* 1635a17c678eSDavid Greenman * Process receiver interrupts. If a no-resource (RNR) 1636a17c678eSDavid Greenman * condition exists, get whatever packets we can and 1637a17c678eSDavid Greenman * re-start the receiver. 1638947e3815SIan Dowse * 16392b5989e9SLuigi Rizzo * When using polling, we do not process the list to completion, 16402b5989e9SLuigi Rizzo * so when we get an RNR interrupt we must defer the restart 16412b5989e9SLuigi Rizzo * until we hit the last buffer with the C bit set. 16422b5989e9SLuigi Rizzo * If we run out of cycles and rfa_headm has the C bit set, 1643947e3815SIan Dowse * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so 1644947e3815SIan Dowse * that the info will be used in the subsequent polling cycle. 1645a17c678eSDavid Greenman */ 16462b5989e9SLuigi Rizzo for (;;) { 1647b2badf02SMaxime Henrion rxp = sc->fxp_desc.rx_head; 1648b2badf02SMaxime Henrion m = rxp->rx_mbuf; 1649ba8c6fd5SDavid Greenman rfa = (struct fxp_rfa *)(m->m_ext.ext_buf + 1650ba8c6fd5SDavid Greenman RFA_ALIGNMENT_FUDGE); 1651b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map, 1652b2badf02SMaxime Henrion BUS_DMASYNC_POSTREAD); 1653a17c678eSDavid Greenman 1654e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */ 1655947e3815SIan Dowse if (count >= 0 && count-- == 0) { 1656947e3815SIan Dowse if (rnr) { 1657947e3815SIan Dowse /* Defer RNR processing until the next time. */ 1658947e3815SIan Dowse sc->flags |= FXP_FLAG_DEFERRED_RNR; 1659947e3815SIan Dowse rnr = 0; 1660947e3815SIan Dowse } 16612b5989e9SLuigi Rizzo break; 1662947e3815SIan Dowse } 16632b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */ 16642b5989e9SLuigi Rizzo 166583e6547dSMaxime Henrion if ((le16toh(rfa->rfa_status) & FXP_RFA_STATUS_C) == 0) 16662b5989e9SLuigi Rizzo break; 16672b5989e9SLuigi Rizzo 1668dfe61cf1SDavid Greenman /* 1669b2badf02SMaxime Henrion * Advance head forward. 1670dfe61cf1SDavid Greenman */ 1671b2badf02SMaxime Henrion sc->fxp_desc.rx_head = rxp->rx_next; 1672a17c678eSDavid Greenman 1673dfe61cf1SDavid Greenman /* 1674ba8c6fd5SDavid Greenman * Add a new buffer to the receive chain. 1675ba8c6fd5SDavid Greenman * If this fails, the old buffer is recycled 1676ba8c6fd5SDavid Greenman * instead. 1677dfe61cf1SDavid Greenman */ 1678b2badf02SMaxime Henrion if (fxp_add_rfabuf(sc, rxp) == 0) { 1679aed53495SDavid Greenman int total_len; 1680a17c678eSDavid Greenman 1681e8c8b728SJonathan Lemon /* 16822b5989e9SLuigi Rizzo * Fetch packet length (the top 2 bits of 16832b5989e9SLuigi Rizzo * actual_size are flags set by the controller 16842b5989e9SLuigi Rizzo * upon completion), and drop the packet in case 16852b5989e9SLuigi Rizzo * of bogus length or CRC errors. 1686e8c8b728SJonathan Lemon */ 1687bafb64afSMaxime Henrion total_len = le16toh(rfa->actual_size) & 0x3fff; 16882b5989e9SLuigi Rizzo if (total_len < sizeof(struct ether_header) || 16892b5989e9SLuigi Rizzo total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE - 1690b2badf02SMaxime Henrion sc->rfa_size || 169183e6547dSMaxime Henrion le16toh(rfa->rfa_status) & FXP_RFA_STATUS_CRC) { 1692e8c8b728SJonathan Lemon m_freem(m); 16932b5989e9SLuigi Rizzo continue; 1694e8c8b728SJonathan Lemon } 1695920b58e8SBrooks Davis 1696c8bca6dcSBill Paul /* Do IP checksum checking. */ 169783e6547dSMaxime Henrion if (le16toh(rfa->rfa_status) & FXP_RFA_STATUS_PARSE) { 1698c8bca6dcSBill Paul if (rfa->rfax_csum_sts & 1699c8bca6dcSBill Paul FXP_RFDX_CS_IP_CSUM_BIT_VALID) 1700c8bca6dcSBill Paul m->m_pkthdr.csum_flags |= 1701c8bca6dcSBill Paul CSUM_IP_CHECKED; 1702c8bca6dcSBill Paul if (rfa->rfax_csum_sts & 1703c8bca6dcSBill Paul FXP_RFDX_CS_IP_CSUM_VALID) 1704c8bca6dcSBill Paul m->m_pkthdr.csum_flags |= 1705c8bca6dcSBill Paul CSUM_IP_VALID; 1706c8bca6dcSBill Paul if ((rfa->rfax_csum_sts & 1707c8bca6dcSBill Paul FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) && 1708c8bca6dcSBill Paul (rfa->rfax_csum_sts & 1709c8bca6dcSBill Paul FXP_RFDX_CS_TCPUDP_CSUM_VALID)) { 1710c8bca6dcSBill Paul m->m_pkthdr.csum_flags |= 1711c8bca6dcSBill Paul CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 1712c8bca6dcSBill Paul m->m_pkthdr.csum_data = 0xffff; 1713c8bca6dcSBill Paul } 1714c8bca6dcSBill Paul } 1715c8bca6dcSBill Paul 17162e2de7f2SArchie Cobbs m->m_pkthdr.len = m->m_len = total_len; 1717673d9191SSam Leffler m->m_pkthdr.rcvif = ifp; 1718673d9191SSam Leffler 171905fb8c3fSNate Lawson /* 172005fb8c3fSNate Lawson * Drop locks before calling if_input() since it 172105fb8c3fSNate Lawson * may re-enter fxp_start() in the netisr case. 172205fb8c3fSNate Lawson * This would result in a lock reversal. Better 172305fb8c3fSNate Lawson * performance might be obtained by chaining all 172405fb8c3fSNate Lawson * packets received, dropping the lock, and then 172505fb8c3fSNate Lawson * calling if_input() on each one. 172605fb8c3fSNate Lawson */ 172705fb8c3fSNate Lawson FXP_UNLOCK(sc); 1728673d9191SSam Leffler (*ifp->if_input)(ifp, m); 172905fb8c3fSNate Lawson FXP_LOCK(sc); 1730a17c678eSDavid Greenman } 1731a17c678eSDavid Greenman } 17322b5989e9SLuigi Rizzo if (rnr) { 1733ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 1734ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 1735b2badf02SMaxime Henrion sc->fxp_desc.rx_head->rx_addr); 17362e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START); 1737a17c678eSDavid Greenman } 1738a17c678eSDavid Greenman } 1739a17c678eSDavid Greenman 1740dfe61cf1SDavid Greenman /* 1741dfe61cf1SDavid Greenman * Update packet in/out/collision statistics. The i82557 doesn't 1742dfe61cf1SDavid Greenman * allow you to access these counters without doing a fairly 1743dfe61cf1SDavid Greenman * expensive DMA to get _all_ of the statistics it maintains, so 1744dfe61cf1SDavid Greenman * we do this operation here only once per second. The statistics 1745dfe61cf1SDavid Greenman * counters in the kernel are updated from the previous dump-stats 1746dfe61cf1SDavid Greenman * DMA and then a new dump-stats DMA is started. The on-chip 1747dfe61cf1SDavid Greenman * counters are zeroed when the DMA completes. If we can't start 1748dfe61cf1SDavid Greenman * the DMA immediately, we don't wait - we just prepare to read 1749dfe61cf1SDavid Greenman * them again next time. 1750dfe61cf1SDavid Greenman */ 1751303b270bSEivind Eklund static void 1752f7788e8eSJonathan Lemon fxp_tick(void *xsc) 1753a17c678eSDavid Greenman { 1754f7788e8eSJonathan Lemon struct fxp_softc *sc = xsc; 1755ba8c6fd5SDavid Greenman struct ifnet *ifp = &sc->sc_if; 1756a17c678eSDavid Greenman struct fxp_stats *sp = sc->fxp_stats; 1757f7788e8eSJonathan Lemon int s; 1758a17c678eSDavid Greenman 17594953bccaSNate Lawson FXP_LOCK(sc); 17604953bccaSNate Lawson s = splimp(); 1761b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD); 176283e6547dSMaxime Henrion ifp->if_opackets += le32toh(sp->tx_good); 176383e6547dSMaxime Henrion ifp->if_collisions += le32toh(sp->tx_total_collisions); 1764397f9dfeSDavid Greenman if (sp->rx_good) { 176583e6547dSMaxime Henrion ifp->if_ipackets += le32toh(sp->rx_good); 1766397f9dfeSDavid Greenman sc->rx_idle_secs = 0; 1767397f9dfeSDavid Greenman } else { 1768c8cc6fcaSDavid Greenman /* 1769c8cc6fcaSDavid Greenman * Receiver's been idle for another second. 1770c8cc6fcaSDavid Greenman */ 1771397f9dfeSDavid Greenman sc->rx_idle_secs++; 1772397f9dfeSDavid Greenman } 17733ba65732SDavid Greenman ifp->if_ierrors += 177483e6547dSMaxime Henrion le32toh(sp->rx_crc_errors) + 177583e6547dSMaxime Henrion le32toh(sp->rx_alignment_errors) + 177683e6547dSMaxime Henrion le32toh(sp->rx_rnr_errors) + 177783e6547dSMaxime Henrion le32toh(sp->rx_overrun_errors); 1778a17c678eSDavid Greenman /* 1779f9be9005SDavid Greenman * If any transmit underruns occured, bump up the transmit 1780f9be9005SDavid Greenman * threshold by another 512 bytes (64 * 8). 1781f9be9005SDavid Greenman */ 1782f9be9005SDavid Greenman if (sp->tx_underruns) { 178383e6547dSMaxime Henrion ifp->if_oerrors += le32toh(sp->tx_underruns); 1784f9be9005SDavid Greenman if (tx_threshold < 192) 1785f9be9005SDavid Greenman tx_threshold += 64; 1786f9be9005SDavid Greenman } 17874953bccaSNate Lawson 1788397f9dfeSDavid Greenman /* 1789c8cc6fcaSDavid Greenman * Release any xmit buffers that have completed DMA. This isn't 1790c8cc6fcaSDavid Greenman * strictly necessary to do here, but it's advantagous for mbufs 1791c8cc6fcaSDavid Greenman * with external storage to be released in a timely manner rather 1792c8cc6fcaSDavid Greenman * than being defered for a potentially long time. This limits 1793c8cc6fcaSDavid Greenman * the delay to a maximum of one second. 1794c8cc6fcaSDavid Greenman */ 1795b2badf02SMaxime Henrion fxp_txeof(sc); 1796b2badf02SMaxime Henrion 1797c8cc6fcaSDavid Greenman /* 1798397f9dfeSDavid Greenman * If we haven't received any packets in FXP_MAC_RX_IDLE seconds, 1799397f9dfeSDavid Greenman * then assume the receiver has locked up and attempt to clear 1800397f9dfeSDavid Greenman * the condition by reprogramming the multicast filter. This is 1801397f9dfeSDavid Greenman * a work-around for a bug in the 82557 where the receiver locks 1802397f9dfeSDavid Greenman * up if it gets certain types of garbage in the syncronization 1803397f9dfeSDavid Greenman * bits prior to the packet header. This bug is supposed to only 1804397f9dfeSDavid Greenman * occur in 10Mbps mode, but has been seen to occur in 100Mbps 1805397f9dfeSDavid Greenman * mode as well (perhaps due to a 10/100 speed transition). 1806397f9dfeSDavid Greenman */ 1807397f9dfeSDavid Greenman if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) { 1808397f9dfeSDavid Greenman sc->rx_idle_secs = 0; 1809397f9dfeSDavid Greenman fxp_mc_setup(sc); 1810397f9dfeSDavid Greenman } 1811f9be9005SDavid Greenman /* 18123ba65732SDavid Greenman * If there is no pending command, start another stats 18133ba65732SDavid Greenman * dump. Otherwise punt for now. 1814a17c678eSDavid Greenman */ 1815397f9dfeSDavid Greenman if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) { 1816a17c678eSDavid Greenman /* 1817397f9dfeSDavid Greenman * Start another stats dump. 1818a17c678eSDavid Greenman */ 1819b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, 1820b2badf02SMaxime Henrion BUS_DMASYNC_PREREAD); 18212e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET); 1822dfe61cf1SDavid Greenman } else { 1823dfe61cf1SDavid Greenman /* 1824dfe61cf1SDavid Greenman * A previous command is still waiting to be accepted. 1825dfe61cf1SDavid Greenman * Just zero our copy of the stats and wait for the 18263ba65732SDavid Greenman * next timer event to update them. 1827dfe61cf1SDavid Greenman */ 1828dfe61cf1SDavid Greenman sp->tx_good = 0; 1829f9be9005SDavid Greenman sp->tx_underruns = 0; 1830dfe61cf1SDavid Greenman sp->tx_total_collisions = 0; 18313ba65732SDavid Greenman 1832dfe61cf1SDavid Greenman sp->rx_good = 0; 18333ba65732SDavid Greenman sp->rx_crc_errors = 0; 18343ba65732SDavid Greenman sp->rx_alignment_errors = 0; 18353ba65732SDavid Greenman sp->rx_rnr_errors = 0; 18363ba65732SDavid Greenman sp->rx_overrun_errors = 0; 1837dfe61cf1SDavid Greenman } 1838f7788e8eSJonathan Lemon if (sc->miibus != NULL) 1839f7788e8eSJonathan Lemon mii_tick(device_get_softc(sc->miibus)); 18404953bccaSNate Lawson 1841a17c678eSDavid Greenman /* 1842a17c678eSDavid Greenman * Schedule another timeout one second from now. 1843a17c678eSDavid Greenman */ 184445276e4aSSam Leffler callout_reset(&sc->stat_ch, hz, fxp_tick, sc); 18454953bccaSNate Lawson FXP_UNLOCK(sc); 18464953bccaSNate Lawson splx(s); 1847a17c678eSDavid Greenman } 1848a17c678eSDavid Greenman 1849a17c678eSDavid Greenman /* 1850a17c678eSDavid Greenman * Stop the interface. Cancels the statistics updater and resets 1851a17c678eSDavid Greenman * the interface. 1852a17c678eSDavid Greenman */ 1853a17c678eSDavid Greenman static void 1854f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc) 1855a17c678eSDavid Greenman { 1856ba8c6fd5SDavid Greenman struct ifnet *ifp = &sc->sc_if; 1857b2badf02SMaxime Henrion struct fxp_tx *txp; 18583ba65732SDavid Greenman int i; 1859a17c678eSDavid Greenman 18607dced78aSDavid Greenman ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 18617dced78aSDavid Greenman ifp->if_timer = 0; 18627dced78aSDavid Greenman 1863e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 1864e4fc250cSLuigi Rizzo ether_poll_deregister(ifp); 1865e4fc250cSLuigi Rizzo #endif 1866a17c678eSDavid Greenman /* 1867a17c678eSDavid Greenman * Cancel stats updater. 1868a17c678eSDavid Greenman */ 186945276e4aSSam Leffler callout_stop(&sc->stat_ch); 18703ba65732SDavid Greenman 18713ba65732SDavid Greenman /* 187272a32a26SJonathan Lemon * Issue software reset, which also unloads the microcode. 18733ba65732SDavid Greenman */ 187472a32a26SJonathan Lemon sc->flags &= ~FXP_FLAG_UCODE; 187509882363SJonathan Lemon CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET); 187672a32a26SJonathan Lemon DELAY(50); 1877a17c678eSDavid Greenman 18783ba65732SDavid Greenman /* 18793ba65732SDavid Greenman * Release any xmit buffers. 18803ba65732SDavid Greenman */ 1881b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_list; 1882da91462dSDavid Greenman if (txp != NULL) { 1883da91462dSDavid Greenman for (i = 0; i < FXP_NTXCB; i++) { 1884b2badf02SMaxime Henrion if (txp[i].tx_mbuf != NULL) { 1885b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map, 1886b2badf02SMaxime Henrion BUS_DMASYNC_POSTWRITE); 1887b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map); 1888b2badf02SMaxime Henrion m_freem(txp[i].tx_mbuf); 1889b2badf02SMaxime Henrion txp[i].tx_mbuf = NULL; 1890c8bca6dcSBill Paul /* clear this to reset csum offload bits */ 1891b2badf02SMaxime Henrion txp[i].tx_cb->tbd[0].tb_addr = 0; 1892da91462dSDavid Greenman } 1893da91462dSDavid Greenman } 18943ba65732SDavid Greenman } 1895b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 18963ba65732SDavid Greenman sc->tx_queued = 0; 1897a17c678eSDavid Greenman } 1898a17c678eSDavid Greenman 1899a17c678eSDavid Greenman /* 1900a17c678eSDavid Greenman * Watchdog/transmission transmit timeout handler. Called when a 1901a17c678eSDavid Greenman * transmission is started on the interface, but no interrupt is 1902a17c678eSDavid Greenman * received before the timeout. This usually indicates that the 1903a17c678eSDavid Greenman * card has wedged for some reason. 1904a17c678eSDavid Greenman */ 1905a17c678eSDavid Greenman static void 1906f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp) 1907a17c678eSDavid Greenman { 1908ba8c6fd5SDavid Greenman struct fxp_softc *sc = ifp->if_softc; 1909ba8c6fd5SDavid Greenman 19104953bccaSNate Lawson FXP_LOCK(sc); 1911f7788e8eSJonathan Lemon device_printf(sc->dev, "device timeout\n"); 19124a5f1499SDavid Greenman ifp->if_oerrors++; 1913a17c678eSDavid Greenman 19144953bccaSNate Lawson fxp_init_body(sc); 19154953bccaSNate Lawson FXP_UNLOCK(sc); 1916a17c678eSDavid Greenman } 1917a17c678eSDavid Greenman 19184953bccaSNate Lawson /* 19194953bccaSNate Lawson * Acquire locks and then call the real initialization function. This 19204953bccaSNate Lawson * is necessary because ether_ioctl() calls if_init() and this would 19214953bccaSNate Lawson * result in mutex recursion if the mutex was held. 19224953bccaSNate Lawson */ 1923a17c678eSDavid Greenman static void 1924f7788e8eSJonathan Lemon fxp_init(void *xsc) 1925a17c678eSDavid Greenman { 1926fb583156SDavid Greenman struct fxp_softc *sc = xsc; 19274953bccaSNate Lawson 19284953bccaSNate Lawson FXP_LOCK(sc); 19294953bccaSNate Lawson fxp_init_body(sc); 19304953bccaSNate Lawson FXP_UNLOCK(sc); 19314953bccaSNate Lawson } 19324953bccaSNate Lawson 19334953bccaSNate Lawson /* 19344953bccaSNate Lawson * Perform device initialization. This routine must be called with the 19354953bccaSNate Lawson * softc lock held. 19364953bccaSNate Lawson */ 19374953bccaSNate Lawson static void 19384953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc) 19394953bccaSNate Lawson { 1940ba8c6fd5SDavid Greenman struct ifnet *ifp = &sc->sc_if; 1941a17c678eSDavid Greenman struct fxp_cb_config *cbp; 1942a17c678eSDavid Greenman struct fxp_cb_ias *cb_ias; 1943b2badf02SMaxime Henrion struct fxp_cb_tx *tcbp; 1944b2badf02SMaxime Henrion struct fxp_tx *txp; 194509882363SJonathan Lemon struct fxp_cb_mcs *mcsp; 1946f7788e8eSJonathan Lemon int i, prm, s; 1947a17c678eSDavid Greenman 194867fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 1949f7788e8eSJonathan Lemon s = splimp(); 1950a17c678eSDavid Greenman /* 19513ba65732SDavid Greenman * Cancel any pending I/O 1952a17c678eSDavid Greenman */ 19533ba65732SDavid Greenman fxp_stop(sc); 1954a17c678eSDavid Greenman 1955a17c678eSDavid Greenman prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0; 1956a17c678eSDavid Greenman 1957a17c678eSDavid Greenman /* 1958a17c678eSDavid Greenman * Initialize base of CBL and RFA memory. Loading with zero 1959a17c678eSDavid Greenman * sets it up for regular linear addressing. 1960a17c678eSDavid Greenman */ 1961ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0); 19622e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE); 1963a17c678eSDavid Greenman 1964ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 19652e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE); 1966a17c678eSDavid Greenman 1967a17c678eSDavid Greenman /* 1968a17c678eSDavid Greenman * Initialize base of dump-stats buffer. 1969a17c678eSDavid Greenman */ 1970ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 1971b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD); 1972b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr); 19732e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR); 1974a17c678eSDavid Greenman 1975a17c678eSDavid Greenman /* 197672a32a26SJonathan Lemon * Attempt to load microcode if requested. 197772a32a26SJonathan Lemon */ 197872a32a26SJonathan Lemon if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0) 197972a32a26SJonathan Lemon fxp_load_ucode(sc); 198072a32a26SJonathan Lemon 198172a32a26SJonathan Lemon /* 198209882363SJonathan Lemon * Initialize the multicast address list. 198309882363SJonathan Lemon */ 198409882363SJonathan Lemon if (fxp_mc_addrs(sc)) { 198509882363SJonathan Lemon mcsp = sc->mcsp; 198609882363SJonathan Lemon mcsp->cb_status = 0; 198783e6547dSMaxime Henrion mcsp->cb_command = 198883e6547dSMaxime Henrion htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL); 198983e6547dSMaxime Henrion mcsp->link_addr = 0xffffffff; 199009882363SJonathan Lemon /* 199109882363SJonathan Lemon * Start the multicast setup command. 199209882363SJonathan Lemon */ 199309882363SJonathan Lemon fxp_scb_wait(sc); 1994b2badf02SMaxime Henrion bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE); 1995b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr); 199609882363SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 199709882363SJonathan Lemon /* ...and wait for it to complete. */ 1998209b07bcSMaxime Henrion fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map); 1999b2badf02SMaxime Henrion bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, 2000b2badf02SMaxime Henrion BUS_DMASYNC_POSTWRITE); 200109882363SJonathan Lemon } 200209882363SJonathan Lemon 200309882363SJonathan Lemon /* 2004a17c678eSDavid Greenman * We temporarily use memory that contains the TxCB list to 2005a17c678eSDavid Greenman * construct the config CB. The TxCB list memory is rebuilt 2006a17c678eSDavid Greenman * later. 2007a17c678eSDavid Greenman */ 2008b2badf02SMaxime Henrion cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list; 2009a17c678eSDavid Greenman 2010a17c678eSDavid Greenman /* 2011a17c678eSDavid Greenman * This bcopy is kind of disgusting, but there are a bunch of must be 2012a17c678eSDavid Greenman * zero and must be one bits in this structure and this is the easiest 2013a17c678eSDavid Greenman * way to initialize them all to proper values. 2014a17c678eSDavid Greenman */ 2015b2badf02SMaxime Henrion bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template)); 2016a17c678eSDavid Greenman 2017a17c678eSDavid Greenman cbp->cb_status = 0; 201883e6547dSMaxime Henrion cbp->cb_command = htole16(FXP_CB_COMMAND_CONFIG | 201983e6547dSMaxime Henrion FXP_CB_COMMAND_EL); 202083e6547dSMaxime Henrion cbp->link_addr = 0xffffffff; /* (no) next command */ 20212c6c0947SMaxime Henrion cbp->byte_count = sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22; 2022001696daSDavid Greenman cbp->rx_fifo_limit = 8; /* rx fifo threshold (32 bytes) */ 2023001696daSDavid Greenman cbp->tx_fifo_limit = 0; /* tx fifo threshold (0 bytes) */ 2024a17c678eSDavid Greenman cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */ 2025f7788e8eSJonathan Lemon cbp->mwi_enable = sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0; 2026f7788e8eSJonathan Lemon cbp->type_enable = 0; /* actually reserved */ 2027f7788e8eSJonathan Lemon cbp->read_align_en = sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0; 2028f7788e8eSJonathan Lemon cbp->end_wr_on_cl = sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0; 2029001696daSDavid Greenman cbp->rx_dma_bytecount = 0; /* (no) rx DMA max */ 2030001696daSDavid Greenman cbp->tx_dma_bytecount = 0; /* (no) tx DMA max */ 2031f7788e8eSJonathan Lemon cbp->dma_mbce = 0; /* (disable) dma max counters */ 2032a17c678eSDavid Greenman cbp->late_scb = 0; /* (don't) defer SCB update */ 2033f7788e8eSJonathan Lemon cbp->direct_dma_dis = 1; /* disable direct rcv dma mode */ 2034f7788e8eSJonathan Lemon cbp->tno_int_or_tco_en =0; /* (disable) tx not okay interrupt */ 20353114fdb4SDavid Greenman cbp->ci_int = 1; /* interrupt on CU idle */ 2036f7788e8eSJonathan Lemon cbp->ext_txcb_dis = sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1; 2037f7788e8eSJonathan Lemon cbp->ext_stats_dis = 1; /* disable extended counters */ 2038f7788e8eSJonathan Lemon cbp->keep_overrun_rx = 0; /* don't pass overrun frames to host */ 20398ef1f631SYaroslav Tykhiy cbp->save_bf = sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm; 2040a17c678eSDavid Greenman cbp->disc_short_rx = !prm; /* discard short packets */ 2041f7788e8eSJonathan Lemon cbp->underrun_retry = 1; /* retry mode (once) on DMA underrun */ 2042f7788e8eSJonathan Lemon cbp->two_frames = 0; /* do not limit FIFO to 2 frames */ 2043f7788e8eSJonathan Lemon cbp->dyn_tbd = 0; /* (no) dynamic TBD mode */ 2044c8bca6dcSBill Paul cbp->ext_rfa = sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0; 2045f7788e8eSJonathan Lemon cbp->mediatype = sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1; 2046f7788e8eSJonathan Lemon cbp->csma_dis = 0; /* (don't) disable link */ 2047f7788e8eSJonathan Lemon cbp->tcp_udp_cksum = 0; /* (don't) enable checksum */ 2048f7788e8eSJonathan Lemon cbp->vlan_tco = 0; /* (don't) enable vlan wakeup */ 2049f7788e8eSJonathan Lemon cbp->link_wake_en = 0; /* (don't) assert PME# on link change */ 2050f7788e8eSJonathan Lemon cbp->arp_wake_en = 0; /* (don't) assert PME# on arp */ 2051f7788e8eSJonathan Lemon cbp->mc_wake_en = 0; /* (don't) enable PME# on mcmatch */ 2052a17c678eSDavid Greenman cbp->nsai = 1; /* (don't) disable source addr insert */ 2053a17c678eSDavid Greenman cbp->preamble_length = 2; /* (7 byte) preamble */ 2054a17c678eSDavid Greenman cbp->loopback = 0; /* (don't) loopback */ 2055a17c678eSDavid Greenman cbp->linear_priority = 0; /* (normal CSMA/CD operation) */ 2056a17c678eSDavid Greenman cbp->linear_pri_mode = 0; /* (wait after xmit only) */ 2057a17c678eSDavid Greenman cbp->interfrm_spacing = 6; /* (96 bits of) interframe spacing */ 2058a17c678eSDavid Greenman cbp->promiscuous = prm; /* promiscuous mode */ 2059a17c678eSDavid Greenman cbp->bcast_disable = 0; /* (don't) disable broadcasts */ 2060f7788e8eSJonathan Lemon cbp->wait_after_win = 0; /* (don't) enable modified backoff alg*/ 2061f7788e8eSJonathan Lemon cbp->ignore_ul = 0; /* consider U/L bit in IA matching */ 2062f7788e8eSJonathan Lemon cbp->crc16_en = 0; /* (don't) enable crc-16 algorithm */ 2063f7788e8eSJonathan Lemon cbp->crscdt = sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0; 2064f7788e8eSJonathan Lemon 2065a17c678eSDavid Greenman cbp->stripping = !prm; /* truncate rx packet to byte count */ 2066a17c678eSDavid Greenman cbp->padding = 1; /* (do) pad short tx packets */ 2067a17c678eSDavid Greenman cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */ 2068f7788e8eSJonathan Lemon cbp->long_rx_en = sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0; 2069f7788e8eSJonathan Lemon cbp->ia_wake_en = 0; /* (don't) wake up on address match */ 2070f7788e8eSJonathan Lemon cbp->magic_pkt_dis = 0; /* (don't) disable magic packet */ 2071f7788e8eSJonathan Lemon /* must set wake_en in PMCSR also */ 2072a17c678eSDavid Greenman cbp->force_fdx = 0; /* (don't) force full duplex */ 20733ba65732SDavid Greenman cbp->fdx_pin_en = 1; /* (enable) FDX# pin */ 2074a17c678eSDavid Greenman cbp->multi_ia = 0; /* (don't) accept multiple IAs */ 2075f7788e8eSJonathan Lemon cbp->mc_all = sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0; 2076c8bca6dcSBill Paul cbp->gamla_rx = sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0; 2077a17c678eSDavid Greenman 20780f1db1d6SMaxime Henrion if (sc->tunable_noflow || sc->revision == FXP_REV_82557) { 20793bd07cfdSJonathan Lemon /* 20803bd07cfdSJonathan Lemon * The 82557 has no hardware flow control, the values 20813bd07cfdSJonathan Lemon * below are the defaults for the chip. 20823bd07cfdSJonathan Lemon */ 20833bd07cfdSJonathan Lemon cbp->fc_delay_lsb = 0; 20843bd07cfdSJonathan Lemon cbp->fc_delay_msb = 0x40; 20853bd07cfdSJonathan Lemon cbp->pri_fc_thresh = 3; 20863bd07cfdSJonathan Lemon cbp->tx_fc_dis = 0; 20873bd07cfdSJonathan Lemon cbp->rx_fc_restop = 0; 20883bd07cfdSJonathan Lemon cbp->rx_fc_restart = 0; 20893bd07cfdSJonathan Lemon cbp->fc_filter = 0; 20903bd07cfdSJonathan Lemon cbp->pri_fc_loc = 1; 20913bd07cfdSJonathan Lemon } else { 20923bd07cfdSJonathan Lemon cbp->fc_delay_lsb = 0x1f; 20933bd07cfdSJonathan Lemon cbp->fc_delay_msb = 0x01; 20943bd07cfdSJonathan Lemon cbp->pri_fc_thresh = 3; 20953bd07cfdSJonathan Lemon cbp->tx_fc_dis = 0; /* enable transmit FC */ 20963bd07cfdSJonathan Lemon cbp->rx_fc_restop = 1; /* enable FC restop frames */ 20973bd07cfdSJonathan Lemon cbp->rx_fc_restart = 1; /* enable FC restart frames */ 20983bd07cfdSJonathan Lemon cbp->fc_filter = !prm; /* drop FC frames to host */ 20993bd07cfdSJonathan Lemon cbp->pri_fc_loc = 1; /* FC pri location (byte31) */ 21003bd07cfdSJonathan Lemon } 21013bd07cfdSJonathan Lemon 2102a17c678eSDavid Greenman /* 2103a17c678eSDavid Greenman * Start the config command/DMA. 2104a17c678eSDavid Greenman */ 2105ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 2106b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2107b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr); 21082e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2109a17c678eSDavid Greenman /* ...and wait for it to complete. */ 2110209b07bcSMaxime Henrion fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map); 2111b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE); 2112a17c678eSDavid Greenman 2113a17c678eSDavid Greenman /* 2114a17c678eSDavid Greenman * Now initialize the station address. Temporarily use the TxCB 2115a17c678eSDavid Greenman * memory area like we did above for the config CB. 2116a17c678eSDavid Greenman */ 2117b2badf02SMaxime Henrion cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list; 2118a17c678eSDavid Greenman cb_ias->cb_status = 0; 211983e6547dSMaxime Henrion cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL); 212083e6547dSMaxime Henrion cb_ias->link_addr = 0xffffffff; 2121e609b4d7SMaxime Henrion bcopy(sc->arpcom.ac_enaddr, cb_ias->macaddr, 2122a17c678eSDavid Greenman sizeof(sc->arpcom.ac_enaddr)); 2123a17c678eSDavid Greenman 2124a17c678eSDavid Greenman /* 2125a17c678eSDavid Greenman * Start the IAS (Individual Address Setup) command/DMA. 2126a17c678eSDavid Greenman */ 2127ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 2128b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 21292e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2130a17c678eSDavid Greenman /* ...and wait for it to complete. */ 2131209b07bcSMaxime Henrion fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map); 2132b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE); 2133a17c678eSDavid Greenman 2134a17c678eSDavid Greenman /* 2135a17c678eSDavid Greenman * Initialize transmit control block (TxCB) list. 2136a17c678eSDavid Greenman */ 2137b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_list; 2138b2badf02SMaxime Henrion tcbp = sc->fxp_desc.cbl_list; 2139b2badf02SMaxime Henrion bzero(tcbp, FXP_TXCB_SZ); 2140a17c678eSDavid Greenman for (i = 0; i < FXP_NTXCB; i++) { 2141b2badf02SMaxime Henrion txp[i].tx_mbuf = NULL; 214283e6547dSMaxime Henrion tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK); 214383e6547dSMaxime Henrion tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP); 214483e6547dSMaxime Henrion tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr + 214583e6547dSMaxime Henrion (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx))); 21463bd07cfdSJonathan Lemon if (sc->flags & FXP_FLAG_EXT_TXCB) 2147b2badf02SMaxime Henrion tcbp[i].tbd_array_addr = 214883e6547dSMaxime Henrion htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2])); 21493bd07cfdSJonathan Lemon else 2150b2badf02SMaxime Henrion tcbp[i].tbd_array_addr = 215183e6547dSMaxime Henrion htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0])); 2152b2badf02SMaxime Henrion txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK]; 2153a17c678eSDavid Greenman } 2154a17c678eSDavid Greenman /* 2155397f9dfeSDavid Greenman * Set the suspend flag on the first TxCB and start the control 2156a17c678eSDavid Greenman * unit. It will execute the NOP and then suspend. 2157a17c678eSDavid Greenman */ 215883e6547dSMaxime Henrion tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S); 2159b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2160b2badf02SMaxime Henrion sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp; 2161397f9dfeSDavid Greenman sc->tx_queued = 1; 2162a17c678eSDavid Greenman 2163ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 21642e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2165a17c678eSDavid Greenman 2166a17c678eSDavid Greenman /* 2167a17c678eSDavid Greenman * Initialize receiver buffer area - RFA. 2168a17c678eSDavid Greenman */ 2169ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 2170b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr); 21712e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START); 2172a17c678eSDavid Greenman 2173dccee1a1SDavid Greenman /* 2174ba8c6fd5SDavid Greenman * Set current media. 2175dccee1a1SDavid Greenman */ 2176f7788e8eSJonathan Lemon if (sc->miibus != NULL) 2177f7788e8eSJonathan Lemon mii_mediachg(device_get_softc(sc->miibus)); 2178dccee1a1SDavid Greenman 2179a17c678eSDavid Greenman ifp->if_flags |= IFF_RUNNING; 2180a17c678eSDavid Greenman ifp->if_flags &= ~IFF_OACTIVE; 2181e8c8b728SJonathan Lemon 2182e8c8b728SJonathan Lemon /* 2183e8c8b728SJonathan Lemon * Enable interrupts. 2184e8c8b728SJonathan Lemon */ 21852b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING 21862b5989e9SLuigi Rizzo /* 21872b5989e9SLuigi Rizzo * ... but only do that if we are not polling. And because (presumably) 21882b5989e9SLuigi Rizzo * the default is interrupts on, we need to disable them explicitly! 21892b5989e9SLuigi Rizzo */ 219062f76486SMaxim Sobolev if ( ifp->if_flags & IFF_POLLING ) 21912b5989e9SLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 21922b5989e9SLuigi Rizzo else 21932b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */ 2194e8c8b728SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0); 2195a17c678eSDavid Greenman 2196a17c678eSDavid Greenman /* 2197a17c678eSDavid Greenman * Start stats updater. 2198a17c678eSDavid Greenman */ 219945276e4aSSam Leffler callout_reset(&sc->stat_ch, hz, fxp_tick, sc); 22004953bccaSNate Lawson splx(s); 2201f7788e8eSJonathan Lemon } 2202f7788e8eSJonathan Lemon 2203f7788e8eSJonathan Lemon static int 2204f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp) 2205f7788e8eSJonathan Lemon { 2206f7788e8eSJonathan Lemon 2207f7788e8eSJonathan Lemon return (0); 2208a17c678eSDavid Greenman } 2209a17c678eSDavid Greenman 2210303b270bSEivind Eklund static void 2211f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2212ba8c6fd5SDavid Greenman { 2213ba8c6fd5SDavid Greenman 2214f7788e8eSJonathan Lemon ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2215ba8c6fd5SDavid Greenman } 2216ba8c6fd5SDavid Greenman 2217ba8c6fd5SDavid Greenman /* 2218ba8c6fd5SDavid Greenman * Change media according to request. 2219ba8c6fd5SDavid Greenman */ 2220f7788e8eSJonathan Lemon static int 2221f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp) 2222ba8c6fd5SDavid Greenman { 2223ba8c6fd5SDavid Greenman struct fxp_softc *sc = ifp->if_softc; 2224f7788e8eSJonathan Lemon struct mii_data *mii; 2225ba8c6fd5SDavid Greenman 2226f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 2227f7788e8eSJonathan Lemon mii_mediachg(mii); 2228ba8c6fd5SDavid Greenman return (0); 2229ba8c6fd5SDavid Greenman } 2230ba8c6fd5SDavid Greenman 2231ba8c6fd5SDavid Greenman /* 2232ba8c6fd5SDavid Greenman * Notify the world which media we're using. 2233ba8c6fd5SDavid Greenman */ 2234f7788e8eSJonathan Lemon static void 2235f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2236ba8c6fd5SDavid Greenman { 2237ba8c6fd5SDavid Greenman struct fxp_softc *sc = ifp->if_softc; 2238f7788e8eSJonathan Lemon struct mii_data *mii; 2239ba8c6fd5SDavid Greenman 2240f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 2241f7788e8eSJonathan Lemon mii_pollstat(mii); 2242f7788e8eSJonathan Lemon ifmr->ifm_active = mii->mii_media_active; 2243f7788e8eSJonathan Lemon ifmr->ifm_status = mii->mii_media_status; 22442e2b8238SJonathan Lemon 22452e2b8238SJonathan Lemon if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG) 22462e2b8238SJonathan Lemon sc->cu_resume_bug = 1; 22472e2b8238SJonathan Lemon else 22482e2b8238SJonathan Lemon sc->cu_resume_bug = 0; 2249ba8c6fd5SDavid Greenman } 2250ba8c6fd5SDavid Greenman 2251a17c678eSDavid Greenman /* 2252a17c678eSDavid Greenman * Add a buffer to the end of the RFA buffer list. 2253a17c678eSDavid Greenman * Return 0 if successful, 1 for failure. A failure results in 2254a17c678eSDavid Greenman * adding the 'oldm' (if non-NULL) on to the end of the list - 2255dc733423SDag-Erling Smørgrav * tossing out its old contents and recycling it. 2256a17c678eSDavid Greenman * The RFA struct is stuck at the beginning of mbuf cluster and the 2257a17c678eSDavid Greenman * data pointer is fixed up to point just past it. 2258a17c678eSDavid Greenman */ 2259a17c678eSDavid Greenman static int 2260b2badf02SMaxime Henrion fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp) 2261a17c678eSDavid Greenman { 2262a17c678eSDavid Greenman struct mbuf *m; 2263a17c678eSDavid Greenman struct fxp_rfa *rfa, *p_rfa; 2264b2badf02SMaxime Henrion struct fxp_rx *p_rx; 2265b2badf02SMaxime Henrion bus_dmamap_t tmp_map; 2266b2badf02SMaxime Henrion int error; 2267a17c678eSDavid Greenman 2268a163d034SWarner Losh m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2269b2badf02SMaxime Henrion if (m == NULL) 2270b2badf02SMaxime Henrion return (ENOBUFS); 2271ba8c6fd5SDavid Greenman 2272ba8c6fd5SDavid Greenman /* 2273ba8c6fd5SDavid Greenman * Move the data pointer up so that the incoming data packet 2274ba8c6fd5SDavid Greenman * will be 32-bit aligned. 2275ba8c6fd5SDavid Greenman */ 2276ba8c6fd5SDavid Greenman m->m_data += RFA_ALIGNMENT_FUDGE; 2277ba8c6fd5SDavid Greenman 2278eadd5e3aSDavid Greenman /* 2279eadd5e3aSDavid Greenman * Get a pointer to the base of the mbuf cluster and move 2280eadd5e3aSDavid Greenman * data start past it. 2281eadd5e3aSDavid Greenman */ 2282a17c678eSDavid Greenman rfa = mtod(m, struct fxp_rfa *); 2283c8bca6dcSBill Paul m->m_data += sc->rfa_size; 228483e6547dSMaxime Henrion rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE); 2285eadd5e3aSDavid Greenman 2286a17c678eSDavid Greenman rfa->rfa_status = 0; 228783e6547dSMaxime Henrion rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL); 2288a17c678eSDavid Greenman rfa->actual_size = 0; 2289ba8c6fd5SDavid Greenman 229028935f27SMaxime Henrion /* 229128935f27SMaxime Henrion * Initialize the rest of the RFA. Note that since the RFA 229228935f27SMaxime Henrion * is misaligned, we cannot store values directly. We're thus 229328935f27SMaxime Henrion * using the le32enc() function which handles endianness and 229428935f27SMaxime Henrion * is also alignment-safe. 229528935f27SMaxime Henrion */ 229683e6547dSMaxime Henrion le32enc(&rfa->link_addr, 0xffffffff); 229783e6547dSMaxime Henrion le32enc(&rfa->rbd_addr, 0xffffffff); 2298ba8c6fd5SDavid Greenman 2299b2badf02SMaxime Henrion /* Map the RFA into DMA memory. */ 2300b2badf02SMaxime Henrion error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa, 2301b2badf02SMaxime Henrion MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr, 2302b2badf02SMaxime Henrion &rxp->rx_addr, 0); 2303b2badf02SMaxime Henrion if (error) { 2304b2badf02SMaxime Henrion m_freem(m); 2305b2badf02SMaxime Henrion return (error); 2306b2badf02SMaxime Henrion } 2307b2badf02SMaxime Henrion 2308b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map); 2309b2badf02SMaxime Henrion tmp_map = sc->spare_map; 2310b2badf02SMaxime Henrion sc->spare_map = rxp->rx_map; 2311b2badf02SMaxime Henrion rxp->rx_map = tmp_map; 2312b2badf02SMaxime Henrion rxp->rx_mbuf = m; 2313b2badf02SMaxime Henrion 2314b983c7b3SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map, 2315b983c7b3SMaxime Henrion BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2316b2badf02SMaxime Henrion 2317dfe61cf1SDavid Greenman /* 2318dfe61cf1SDavid Greenman * If there are other buffers already on the list, attach this 2319dfe61cf1SDavid Greenman * one to the end by fixing up the tail to point to this one. 2320dfe61cf1SDavid Greenman */ 2321b2badf02SMaxime Henrion if (sc->fxp_desc.rx_head != NULL) { 2322b2badf02SMaxime Henrion p_rx = sc->fxp_desc.rx_tail; 2323b2badf02SMaxime Henrion p_rfa = (struct fxp_rfa *) 2324b2badf02SMaxime Henrion (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE); 2325b2badf02SMaxime Henrion p_rx->rx_next = rxp; 232683e6547dSMaxime Henrion le32enc(&p_rfa->link_addr, rxp->rx_addr); 2327aed53495SDavid Greenman p_rfa->rfa_control = 0; 2328b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map, 23294cec1653SMaxime Henrion BUS_DMASYNC_PREWRITE); 2330a17c678eSDavid Greenman } else { 2331b2badf02SMaxime Henrion rxp->rx_next = NULL; 2332b2badf02SMaxime Henrion sc->fxp_desc.rx_head = rxp; 2333a17c678eSDavid Greenman } 2334b2badf02SMaxime Henrion sc->fxp_desc.rx_tail = rxp; 2335b2badf02SMaxime Henrion return (0); 2336a17c678eSDavid Greenman } 2337a17c678eSDavid Greenman 23386ebc3153SDavid Greenman static volatile int 2339f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg) 2340dccee1a1SDavid Greenman { 2341f7788e8eSJonathan Lemon struct fxp_softc *sc = device_get_softc(dev); 2342dccee1a1SDavid Greenman int count = 10000; 23436ebc3153SDavid Greenman int value; 2344dccee1a1SDavid Greenman 2345ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_MDICONTROL, 2346ba8c6fd5SDavid Greenman (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21)); 2347dccee1a1SDavid Greenman 2348ba8c6fd5SDavid Greenman while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0 2349ba8c6fd5SDavid Greenman && count--) 23506ebc3153SDavid Greenman DELAY(10); 2351dccee1a1SDavid Greenman 2352dccee1a1SDavid Greenman if (count <= 0) 2353f7788e8eSJonathan Lemon device_printf(dev, "fxp_miibus_readreg: timed out\n"); 2354dccee1a1SDavid Greenman 23556ebc3153SDavid Greenman return (value & 0xffff); 2356dccee1a1SDavid Greenman } 2357dccee1a1SDavid Greenman 2358dccee1a1SDavid Greenman static void 2359f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value) 2360dccee1a1SDavid Greenman { 2361f7788e8eSJonathan Lemon struct fxp_softc *sc = device_get_softc(dev); 2362dccee1a1SDavid Greenman int count = 10000; 2363dccee1a1SDavid Greenman 2364ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_MDICONTROL, 2365ba8c6fd5SDavid Greenman (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) | 2366ba8c6fd5SDavid Greenman (value & 0xffff)); 2367dccee1a1SDavid Greenman 2368ba8c6fd5SDavid Greenman while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 && 2369ba8c6fd5SDavid Greenman count--) 23706ebc3153SDavid Greenman DELAY(10); 2371dccee1a1SDavid Greenman 2372dccee1a1SDavid Greenman if (count <= 0) 2373f7788e8eSJonathan Lemon device_printf(dev, "fxp_miibus_writereg: timed out\n"); 2374dccee1a1SDavid Greenman } 2375dccee1a1SDavid Greenman 2376dccee1a1SDavid Greenman static int 2377f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 2378a17c678eSDavid Greenman { 23799b44ff22SGarrett Wollman struct fxp_softc *sc = ifp->if_softc; 2380a17c678eSDavid Greenman struct ifreq *ifr = (struct ifreq *)data; 2381f7788e8eSJonathan Lemon struct mii_data *mii; 23828ef1f631SYaroslav Tykhiy int flag, mask, s, error = 0; 2383a17c678eSDavid Greenman 2384704d1965SWarner Losh /* 2385704d1965SWarner Losh * Detaching causes us to call ioctl with the mutex owned. Preclude 2386704d1965SWarner Losh * that by saying we're busy if the lock is already held. 2387704d1965SWarner Losh */ 238867fc050fSMaxime Henrion if (FXP_LOCKED(sc)) 2389704d1965SWarner Losh return (EBUSY); 239032cd7a9cSWarner Losh 23914953bccaSNate Lawson FXP_LOCK(sc); 2392f7788e8eSJonathan Lemon s = splimp(); 2393a17c678eSDavid Greenman 2394a17c678eSDavid Greenman switch (command) { 2395a17c678eSDavid Greenman case SIOCSIFFLAGS: 2396f7788e8eSJonathan Lemon if (ifp->if_flags & IFF_ALLMULTI) 2397f7788e8eSJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 2398f7788e8eSJonathan Lemon else 2399f7788e8eSJonathan Lemon sc->flags &= ~FXP_FLAG_ALL_MCAST; 2400a17c678eSDavid Greenman 2401a17c678eSDavid Greenman /* 2402a17c678eSDavid Greenman * If interface is marked up and not running, then start it. 2403a17c678eSDavid Greenman * If it is marked down and running, stop it. 2404a17c678eSDavid Greenman * XXX If it's up then re-initialize it. This is so flags 2405a17c678eSDavid Greenman * such as IFF_PROMISC are handled. 2406a17c678eSDavid Greenman */ 2407a17c678eSDavid Greenman if (ifp->if_flags & IFF_UP) { 24084953bccaSNate Lawson fxp_init_body(sc); 2409a17c678eSDavid Greenman } else { 2410a17c678eSDavid Greenman if (ifp->if_flags & IFF_RUNNING) 24114a5f1499SDavid Greenman fxp_stop(sc); 2412a17c678eSDavid Greenman } 2413a17c678eSDavid Greenman break; 2414a17c678eSDavid Greenman 2415a17c678eSDavid Greenman case SIOCADDMULTI: 2416a17c678eSDavid Greenman case SIOCDELMULTI: 2417f7788e8eSJonathan Lemon if (ifp->if_flags & IFF_ALLMULTI) 2418f7788e8eSJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 2419f7788e8eSJonathan Lemon else 2420f7788e8eSJonathan Lemon sc->flags &= ~FXP_FLAG_ALL_MCAST; 2421a17c678eSDavid Greenman /* 2422a17c678eSDavid Greenman * Multicast list has changed; set the hardware filter 2423a17c678eSDavid Greenman * accordingly. 2424a17c678eSDavid Greenman */ 2425f7788e8eSJonathan Lemon if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) 2426397f9dfeSDavid Greenman fxp_mc_setup(sc); 2427397f9dfeSDavid Greenman /* 2428f7788e8eSJonathan Lemon * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it 2429397f9dfeSDavid Greenman * again rather than else {}. 2430397f9dfeSDavid Greenman */ 2431f7788e8eSJonathan Lemon if (sc->flags & FXP_FLAG_ALL_MCAST) 24324953bccaSNate Lawson fxp_init_body(sc); 2433a17c678eSDavid Greenman error = 0; 2434ba8c6fd5SDavid Greenman break; 2435ba8c6fd5SDavid Greenman 2436ba8c6fd5SDavid Greenman case SIOCSIFMEDIA: 2437ba8c6fd5SDavid Greenman case SIOCGIFMEDIA: 2438f7788e8eSJonathan Lemon if (sc->miibus != NULL) { 2439f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 2440f7788e8eSJonathan Lemon error = ifmedia_ioctl(ifp, ifr, 2441f7788e8eSJonathan Lemon &mii->mii_media, command); 2442f7788e8eSJonathan Lemon } else { 2443ba8c6fd5SDavid Greenman error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command); 2444f7788e8eSJonathan Lemon } 2445a17c678eSDavid Greenman break; 2446a17c678eSDavid Greenman 2447fb917226SRuslan Ermilov case SIOCSIFCAP: 24488ef1f631SYaroslav Tykhiy mask = ifp->if_capenable ^ ifr->ifr_reqcap; 24498ef1f631SYaroslav Tykhiy if (mask & IFCAP_POLLING) 24508ef1f631SYaroslav Tykhiy ifp->if_capenable ^= IFCAP_POLLING; 24518ef1f631SYaroslav Tykhiy if (mask & IFCAP_VLAN_MTU) { 24528ef1f631SYaroslav Tykhiy ifp->if_capenable ^= IFCAP_VLAN_MTU; 24538ef1f631SYaroslav Tykhiy if (sc->revision != FXP_REV_82557) 24548ef1f631SYaroslav Tykhiy flag = FXP_FLAG_LONG_PKT_EN; 24558ef1f631SYaroslav Tykhiy else /* a hack to get long frames on the old chip */ 24568ef1f631SYaroslav Tykhiy flag = FXP_FLAG_SAVE_BAD; 24578ef1f631SYaroslav Tykhiy sc->flags ^= flag; 24588ef1f631SYaroslav Tykhiy if (ifp->if_flags & IFF_UP) 24598ef1f631SYaroslav Tykhiy fxp_init_body(sc); 24608ef1f631SYaroslav Tykhiy } 2461fb917226SRuslan Ermilov break; 2462fb917226SRuslan Ermilov 2463a17c678eSDavid Greenman default: 24644953bccaSNate Lawson /* 24654953bccaSNate Lawson * ether_ioctl() will eventually call fxp_start() which 24664953bccaSNate Lawson * will result in mutex recursion so drop it first. 24674953bccaSNate Lawson */ 24684953bccaSNate Lawson FXP_UNLOCK(sc); 2469673d9191SSam Leffler error = ether_ioctl(ifp, command, data); 2470a17c678eSDavid Greenman } 247167fc050fSMaxime Henrion if (FXP_LOCKED(sc)) 24724953bccaSNate Lawson FXP_UNLOCK(sc); 2473f7788e8eSJonathan Lemon splx(s); 2474a17c678eSDavid Greenman return (error); 2475a17c678eSDavid Greenman } 2476397f9dfeSDavid Greenman 2477397f9dfeSDavid Greenman /* 247809882363SJonathan Lemon * Fill in the multicast address list and return number of entries. 247909882363SJonathan Lemon */ 248009882363SJonathan Lemon static int 248109882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc) 248209882363SJonathan Lemon { 248309882363SJonathan Lemon struct fxp_cb_mcs *mcsp = sc->mcsp; 248409882363SJonathan Lemon struct ifnet *ifp = &sc->sc_if; 248509882363SJonathan Lemon struct ifmultiaddr *ifma; 248609882363SJonathan Lemon int nmcasts; 248709882363SJonathan Lemon 248809882363SJonathan Lemon nmcasts = 0; 248909882363SJonathan Lemon if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) { 249009882363SJonathan Lemon #if __FreeBSD_version < 500000 249109882363SJonathan Lemon LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 249209882363SJonathan Lemon #else 249309882363SJonathan Lemon TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 249409882363SJonathan Lemon #endif 249509882363SJonathan Lemon if (ifma->ifma_addr->sa_family != AF_LINK) 249609882363SJonathan Lemon continue; 249709882363SJonathan Lemon if (nmcasts >= MAXMCADDR) { 249809882363SJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 249909882363SJonathan Lemon nmcasts = 0; 250009882363SJonathan Lemon break; 250109882363SJonathan Lemon } 250209882363SJonathan Lemon bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 2503bafb64afSMaxime Henrion &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN); 250409882363SJonathan Lemon nmcasts++; 250509882363SJonathan Lemon } 250609882363SJonathan Lemon } 2507bafb64afSMaxime Henrion mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN); 250809882363SJonathan Lemon return (nmcasts); 250909882363SJonathan Lemon } 251009882363SJonathan Lemon 251109882363SJonathan Lemon /* 2512397f9dfeSDavid Greenman * Program the multicast filter. 2513397f9dfeSDavid Greenman * 2514397f9dfeSDavid Greenman * We have an artificial restriction that the multicast setup command 2515397f9dfeSDavid Greenman * must be the first command in the chain, so we take steps to ensure 25163114fdb4SDavid Greenman * this. By requiring this, it allows us to keep up the performance of 2517397f9dfeSDavid Greenman * the pre-initialized command ring (esp. link pointers) by not actually 2518dc733423SDag-Erling Smørgrav * inserting the mcsetup command in the ring - i.e. its link pointer 2519397f9dfeSDavid Greenman * points to the TxCB ring, but the mcsetup descriptor itself is not part 2520397f9dfeSDavid Greenman * of it. We then can do 'CU_START' on the mcsetup descriptor and have it 2521397f9dfeSDavid Greenman * lead into the regular TxCB ring when it completes. 2522397f9dfeSDavid Greenman * 2523397f9dfeSDavid Greenman * This function must be called at splimp. 2524397f9dfeSDavid Greenman */ 2525397f9dfeSDavid Greenman static void 2526f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc) 2527397f9dfeSDavid Greenman { 2528397f9dfeSDavid Greenman struct fxp_cb_mcs *mcsp = sc->mcsp; 2529397f9dfeSDavid Greenman struct ifnet *ifp = &sc->sc_if; 2530b2badf02SMaxime Henrion struct fxp_tx *txp; 25317dced78aSDavid Greenman int count; 2532397f9dfeSDavid Greenman 253367fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 25343114fdb4SDavid Greenman /* 25353114fdb4SDavid Greenman * If there are queued commands, we must wait until they are all 25363114fdb4SDavid Greenman * completed. If we are already waiting, then add a NOP command 25373114fdb4SDavid Greenman * with interrupt option so that we're notified when all commands 25383114fdb4SDavid Greenman * have been completed - fxp_start() ensures that no additional 25393114fdb4SDavid Greenman * TX commands will be added when need_mcsetup is true. 25403114fdb4SDavid Greenman */ 2541397f9dfeSDavid Greenman if (sc->tx_queued) { 25423114fdb4SDavid Greenman /* 25433114fdb4SDavid Greenman * need_mcsetup will be true if we are already waiting for the 25443114fdb4SDavid Greenman * NOP command to be completed (see below). In this case, bail. 25453114fdb4SDavid Greenman */ 25463114fdb4SDavid Greenman if (sc->need_mcsetup) 25473114fdb4SDavid Greenman return; 2548397f9dfeSDavid Greenman sc->need_mcsetup = 1; 25493114fdb4SDavid Greenman 25503114fdb4SDavid Greenman /* 255172a32a26SJonathan Lemon * Add a NOP command with interrupt so that we are notified 255272a32a26SJonathan Lemon * when all TX commands have been processed. 25533114fdb4SDavid Greenman */ 2554b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_last->tx_next; 2555b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 2556b2badf02SMaxime Henrion txp->tx_cb->cb_status = 0; 255783e6547dSMaxime Henrion txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP | 255883e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 25593114fdb4SDavid Greenman /* 25603114fdb4SDavid Greenman * Advance the end of list forward. 25613114fdb4SDavid Greenman */ 256283e6547dSMaxime Henrion sc->fxp_desc.tx_last->tx_cb->cb_command &= 256383e6547dSMaxime Henrion htole16(~FXP_CB_COMMAND_S); 25645f361cbeSMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2565b2badf02SMaxime Henrion sc->fxp_desc.tx_last = txp; 25663114fdb4SDavid Greenman sc->tx_queued++; 25673114fdb4SDavid Greenman /* 25683114fdb4SDavid Greenman * Issue a resume in case the CU has just suspended. 25693114fdb4SDavid Greenman */ 25703114fdb4SDavid Greenman fxp_scb_wait(sc); 25712e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME); 25723114fdb4SDavid Greenman /* 25733114fdb4SDavid Greenman * Set a 5 second timer just in case we don't hear from the 25743114fdb4SDavid Greenman * card again. 25753114fdb4SDavid Greenman */ 25763114fdb4SDavid Greenman ifp->if_timer = 5; 25773114fdb4SDavid Greenman 2578397f9dfeSDavid Greenman return; 2579397f9dfeSDavid Greenman } 2580397f9dfeSDavid Greenman sc->need_mcsetup = 0; 2581397f9dfeSDavid Greenman 2582397f9dfeSDavid Greenman /* 2583397f9dfeSDavid Greenman * Initialize multicast setup descriptor. 2584397f9dfeSDavid Greenman */ 2585397f9dfeSDavid Greenman mcsp->cb_status = 0; 258683e6547dSMaxime Henrion mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | 258783e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 258883e6547dSMaxime Henrion mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr); 2589b2badf02SMaxime Henrion txp = &sc->fxp_desc.mcs_tx; 2590b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 2591b2badf02SMaxime Henrion txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp; 2592b2badf02SMaxime Henrion txp->tx_next = sc->fxp_desc.tx_list; 259309882363SJonathan Lemon (void) fxp_mc_addrs(sc); 2594b2badf02SMaxime Henrion sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp; 2595397f9dfeSDavid Greenman sc->tx_queued = 1; 2596397f9dfeSDavid Greenman 2597397f9dfeSDavid Greenman /* 2598397f9dfeSDavid Greenman * Wait until command unit is not active. This should never 2599397f9dfeSDavid Greenman * be the case when nothing is queued, but make sure anyway. 2600397f9dfeSDavid Greenman */ 26017dced78aSDavid Greenman count = 100; 2602397f9dfeSDavid Greenman while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) == 26037dced78aSDavid Greenman FXP_SCB_CUS_ACTIVE && --count) 26047dced78aSDavid Greenman DELAY(10); 26057dced78aSDavid Greenman if (count == 0) { 2606f7788e8eSJonathan Lemon device_printf(sc->dev, "command queue timeout\n"); 26077dced78aSDavid Greenman return; 26087dced78aSDavid Greenman } 2609397f9dfeSDavid Greenman 2610397f9dfeSDavid Greenman /* 2611397f9dfeSDavid Greenman * Start the multicast setup command. 2612397f9dfeSDavid Greenman */ 2613397f9dfeSDavid Greenman fxp_scb_wait(sc); 2614b2badf02SMaxime Henrion bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE); 2615b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr); 26162e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2617397f9dfeSDavid Greenman 26183114fdb4SDavid Greenman ifp->if_timer = 2; 2619397f9dfeSDavid Greenman return; 2620397f9dfeSDavid Greenman } 262172a32a26SJonathan Lemon 262272a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE; 262372a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE; 262472a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE; 262572a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE; 262672a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE; 262772a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE; 262872a32a26SJonathan Lemon 262994a4f968SPyun YongHyeon #define UCODE(x) x, sizeof(x)/sizeof(u_int32_t) 263072a32a26SJonathan Lemon 263172a32a26SJonathan Lemon struct ucode { 263272a32a26SJonathan Lemon u_int32_t revision; 263372a32a26SJonathan Lemon u_int32_t *ucode; 263472a32a26SJonathan Lemon int length; 263572a32a26SJonathan Lemon u_short int_delay_offset; 263672a32a26SJonathan Lemon u_short bundle_max_offset; 263772a32a26SJonathan Lemon } ucode_table[] = { 263872a32a26SJonathan Lemon { FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 }, 263972a32a26SJonathan Lemon { FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 }, 264072a32a26SJonathan Lemon { FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma), 264172a32a26SJonathan Lemon D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD }, 264272a32a26SJonathan Lemon { FXP_REV_82559S_A, UCODE(fxp_ucode_d101s), 264372a32a26SJonathan Lemon D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD }, 264472a32a26SJonathan Lemon { FXP_REV_82550, UCODE(fxp_ucode_d102), 264572a32a26SJonathan Lemon D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD }, 264672a32a26SJonathan Lemon { FXP_REV_82550_C, UCODE(fxp_ucode_d102c), 264772a32a26SJonathan Lemon D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD }, 264872a32a26SJonathan Lemon { 0, NULL, 0, 0, 0 } 264972a32a26SJonathan Lemon }; 265072a32a26SJonathan Lemon 265172a32a26SJonathan Lemon static void 265272a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc) 265372a32a26SJonathan Lemon { 265472a32a26SJonathan Lemon struct ucode *uc; 265572a32a26SJonathan Lemon struct fxp_cb_ucode *cbp; 265694a4f968SPyun YongHyeon int i; 265772a32a26SJonathan Lemon 265872a32a26SJonathan Lemon for (uc = ucode_table; uc->ucode != NULL; uc++) 265972a32a26SJonathan Lemon if (sc->revision == uc->revision) 266072a32a26SJonathan Lemon break; 266172a32a26SJonathan Lemon if (uc->ucode == NULL) 266272a32a26SJonathan Lemon return; 2663b2badf02SMaxime Henrion cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list; 266472a32a26SJonathan Lemon cbp->cb_status = 0; 266583e6547dSMaxime Henrion cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL); 266683e6547dSMaxime Henrion cbp->link_addr = 0xffffffff; /* (no) next command */ 266794a4f968SPyun YongHyeon for (i = 0; i < uc->length; i++) 266894a4f968SPyun YongHyeon cbp->ucode[i] = htole32(uc->ucode[i]); 266972a32a26SJonathan Lemon if (uc->int_delay_offset) 267083e6547dSMaxime Henrion *(u_int16_t *)&cbp->ucode[uc->int_delay_offset] = 267183e6547dSMaxime Henrion htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2); 267272a32a26SJonathan Lemon if (uc->bundle_max_offset) 267383e6547dSMaxime Henrion *(u_int16_t *)&cbp->ucode[uc->bundle_max_offset] = 267483e6547dSMaxime Henrion htole16(sc->tunable_bundle_max); 267572a32a26SJonathan Lemon /* 267672a32a26SJonathan Lemon * Download the ucode to the chip. 267772a32a26SJonathan Lemon */ 267872a32a26SJonathan Lemon fxp_scb_wait(sc); 2679b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2680b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr); 268172a32a26SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 268272a32a26SJonathan Lemon /* ...and wait for it to complete. */ 2683209b07bcSMaxime Henrion fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map); 2684b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE); 268572a32a26SJonathan Lemon device_printf(sc->dev, 268672a32a26SJonathan Lemon "Microcode loaded, int_delay: %d usec bundle_max: %d\n", 268772a32a26SJonathan Lemon sc->tunable_int_delay, 268872a32a26SJonathan Lemon uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max); 268972a32a26SJonathan Lemon sc->flags |= FXP_FLAG_UCODE; 269072a32a26SJonathan Lemon } 269172a32a26SJonathan Lemon 269272a32a26SJonathan Lemon static int 269372a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 269472a32a26SJonathan Lemon { 269572a32a26SJonathan Lemon int error, value; 269672a32a26SJonathan Lemon 269772a32a26SJonathan Lemon value = *(int *)arg1; 269872a32a26SJonathan Lemon error = sysctl_handle_int(oidp, &value, 0, req); 269972a32a26SJonathan Lemon if (error || !req->newptr) 270072a32a26SJonathan Lemon return (error); 270172a32a26SJonathan Lemon if (value < low || value > high) 270272a32a26SJonathan Lemon return (EINVAL); 270372a32a26SJonathan Lemon *(int *)arg1 = value; 270472a32a26SJonathan Lemon return (0); 270572a32a26SJonathan Lemon } 270672a32a26SJonathan Lemon 270772a32a26SJonathan Lemon /* 270872a32a26SJonathan Lemon * Interrupt delay is expressed in microseconds, a multiplier is used 270972a32a26SJonathan Lemon * to convert this to the appropriate clock ticks before using. 271072a32a26SJonathan Lemon */ 271172a32a26SJonathan Lemon static int 271272a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS) 271372a32a26SJonathan Lemon { 271472a32a26SJonathan Lemon return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000)); 271572a32a26SJonathan Lemon } 271672a32a26SJonathan Lemon 271772a32a26SJonathan Lemon static int 271872a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS) 271972a32a26SJonathan Lemon { 272072a32a26SJonathan Lemon return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff)); 272172a32a26SJonathan Lemon } 2722