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 { 14374d1ed23SMaxime Henrion uint16_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)" }, 17529a8929dSMaxime Henrion { 0x1068, -1, "Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" }, 176f19fc5d8SJohn Polstra { 0x1209, -1, "Intel 82559ER Embedded 10/100 Ethernet" }, 177f19fc5d8SJohn Polstra { 0x1229, 0x01, "Intel 82557 Pro/100 Ethernet" }, 178f19fc5d8SJohn Polstra { 0x1229, 0x02, "Intel 82557 Pro/100 Ethernet" }, 179f19fc5d8SJohn Polstra { 0x1229, 0x03, "Intel 82557 Pro/100 Ethernet" }, 180f19fc5d8SJohn Polstra { 0x1229, 0x04, "Intel 82558 Pro/100 Ethernet" }, 181f19fc5d8SJohn Polstra { 0x1229, 0x05, "Intel 82558 Pro/100 Ethernet" }, 182f19fc5d8SJohn Polstra { 0x1229, 0x06, "Intel 82559 Pro/100 Ethernet" }, 183f19fc5d8SJohn Polstra { 0x1229, 0x07, "Intel 82559 Pro/100 Ethernet" }, 184f19fc5d8SJohn Polstra { 0x1229, 0x08, "Intel 82559 Pro/100 Ethernet" }, 185f19fc5d8SJohn Polstra { 0x1229, 0x09, "Intel 82559ER Pro/100 Ethernet" }, 186f19fc5d8SJohn Polstra { 0x1229, 0x0c, "Intel 82550 Pro/100 Ethernet" }, 187f19fc5d8SJohn Polstra { 0x1229, 0x0d, "Intel 82550 Pro/100 Ethernet" }, 188f19fc5d8SJohn Polstra { 0x1229, 0x0e, "Intel 82550 Pro/100 Ethernet" }, 189f19fc5d8SJohn Polstra { 0x1229, 0x0f, "Intel 82551 Pro/100 Ethernet" }, 190f19fc5d8SJohn Polstra { 0x1229, 0x10, "Intel 82551 Pro/100 Ethernet" }, 191f19fc5d8SJohn Polstra { 0x1229, -1, "Intel 82557/8/9 Pro/100 Ethernet" }, 192f19fc5d8SJohn Polstra { 0x2449, -1, "Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" }, 193f19fc5d8SJohn Polstra { 0, -1, NULL }, 194f7788e8eSJonathan Lemon }; 195f7788e8eSJonathan Lemon 196c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR 197c8bca6dcSBill Paul #define FXP_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 198c8bca6dcSBill Paul #else 199c8bca6dcSBill Paul #define FXP_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 200c8bca6dcSBill Paul #endif 201c8bca6dcSBill Paul 202f7788e8eSJonathan Lemon static int fxp_probe(device_t dev); 203f7788e8eSJonathan Lemon static int fxp_attach(device_t dev); 204f7788e8eSJonathan Lemon static int fxp_detach(device_t dev); 205f7788e8eSJonathan Lemon static int fxp_shutdown(device_t dev); 206f7788e8eSJonathan Lemon static int fxp_suspend(device_t dev); 207f7788e8eSJonathan Lemon static int fxp_resume(device_t dev); 208f7788e8eSJonathan Lemon 209f7788e8eSJonathan Lemon static void fxp_intr(void *xsc); 2104953bccaSNate Lawson static void fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, 21174d1ed23SMaxime Henrion uint8_t statack, int count); 212f7788e8eSJonathan Lemon static void fxp_init(void *xsc); 2134953bccaSNate Lawson static void fxp_init_body(struct fxp_softc *sc); 214f7788e8eSJonathan Lemon static void fxp_tick(void *xsc); 215f7788e8eSJonathan Lemon static void fxp_start(struct ifnet *ifp); 2164953bccaSNate Lawson static void fxp_start_body(struct ifnet *ifp); 217f7788e8eSJonathan Lemon static void fxp_stop(struct fxp_softc *sc); 218f7788e8eSJonathan Lemon static void fxp_release(struct fxp_softc *sc); 219f7788e8eSJonathan Lemon static int fxp_ioctl(struct ifnet *ifp, u_long command, 220f7788e8eSJonathan Lemon caddr_t data); 221f7788e8eSJonathan Lemon static void fxp_watchdog(struct ifnet *ifp); 222b2badf02SMaxime Henrion static int fxp_add_rfabuf(struct fxp_softc *sc, 223b2badf02SMaxime Henrion struct fxp_rx *rxp); 22409882363SJonathan Lemon static int fxp_mc_addrs(struct fxp_softc *sc); 225f7788e8eSJonathan Lemon static void fxp_mc_setup(struct fxp_softc *sc); 22674d1ed23SMaxime Henrion static uint16_t fxp_eeprom_getword(struct fxp_softc *sc, int offset, 227f7788e8eSJonathan Lemon int autosize); 22800c4116bSJonathan Lemon static void fxp_eeprom_putword(struct fxp_softc *sc, int offset, 22974d1ed23SMaxime Henrion uint16_t data); 230f7788e8eSJonathan Lemon static void fxp_autosize_eeprom(struct fxp_softc *sc); 231f7788e8eSJonathan Lemon static void fxp_read_eeprom(struct fxp_softc *sc, u_short *data, 232f7788e8eSJonathan Lemon int offset, int words); 23300c4116bSJonathan Lemon static void fxp_write_eeprom(struct fxp_softc *sc, u_short *data, 23400c4116bSJonathan Lemon int offset, int words); 235f7788e8eSJonathan Lemon static int fxp_ifmedia_upd(struct ifnet *ifp); 236f7788e8eSJonathan Lemon static void fxp_ifmedia_sts(struct ifnet *ifp, 237f7788e8eSJonathan Lemon struct ifmediareq *ifmr); 238f7788e8eSJonathan Lemon static int fxp_serial_ifmedia_upd(struct ifnet *ifp); 239f7788e8eSJonathan Lemon static void fxp_serial_ifmedia_sts(struct ifnet *ifp, 240f7788e8eSJonathan Lemon struct ifmediareq *ifmr); 241f7788e8eSJonathan Lemon static volatile int fxp_miibus_readreg(device_t dev, int phy, int reg); 242f7788e8eSJonathan Lemon static void fxp_miibus_writereg(device_t dev, int phy, int reg, 243f7788e8eSJonathan Lemon int value); 24472a32a26SJonathan Lemon static void fxp_load_ucode(struct fxp_softc *sc); 24572a32a26SJonathan Lemon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, 24672a32a26SJonathan Lemon int low, int high); 24772a32a26SJonathan Lemon static int sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS); 24872a32a26SJonathan Lemon static int sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS); 24928935f27SMaxime Henrion static void fxp_scb_wait(struct fxp_softc *sc); 25028935f27SMaxime Henrion static void fxp_scb_cmd(struct fxp_softc *sc, int cmd); 25128935f27SMaxime Henrion static void fxp_dma_wait(struct fxp_softc *sc, 25274d1ed23SMaxime Henrion volatile uint16_t *status, bus_dma_tag_t dmat, 253209b07bcSMaxime Henrion bus_dmamap_t map); 254f7788e8eSJonathan Lemon 255f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = { 256f7788e8eSJonathan Lemon /* Device interface */ 257f7788e8eSJonathan Lemon DEVMETHOD(device_probe, fxp_probe), 258f7788e8eSJonathan Lemon DEVMETHOD(device_attach, fxp_attach), 259f7788e8eSJonathan Lemon DEVMETHOD(device_detach, fxp_detach), 260f7788e8eSJonathan Lemon DEVMETHOD(device_shutdown, fxp_shutdown), 261f7788e8eSJonathan Lemon DEVMETHOD(device_suspend, fxp_suspend), 262f7788e8eSJonathan Lemon DEVMETHOD(device_resume, fxp_resume), 263f7788e8eSJonathan Lemon 264f7788e8eSJonathan Lemon /* MII interface */ 265f7788e8eSJonathan Lemon DEVMETHOD(miibus_readreg, fxp_miibus_readreg), 266f7788e8eSJonathan Lemon DEVMETHOD(miibus_writereg, fxp_miibus_writereg), 267f7788e8eSJonathan Lemon 268f7788e8eSJonathan Lemon { 0, 0 } 269f7788e8eSJonathan Lemon }; 270f7788e8eSJonathan Lemon 271f7788e8eSJonathan Lemon static driver_t fxp_driver = { 272f7788e8eSJonathan Lemon "fxp", 273f7788e8eSJonathan Lemon fxp_methods, 274f7788e8eSJonathan Lemon sizeof(struct fxp_softc), 275f7788e8eSJonathan Lemon }; 276f7788e8eSJonathan Lemon 277f7788e8eSJonathan Lemon static devclass_t fxp_devclass; 278f7788e8eSJonathan Lemon 279f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0); 280347934faSWarner Losh DRIVER_MODULE(fxp, cardbus, fxp_driver, fxp_devclass, 0, 0); 281f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0); 282f7788e8eSJonathan Lemon 283f7788e8eSJonathan Lemon /* 284dfe61cf1SDavid Greenman * Wait for the previous command to be accepted (but not necessarily 285dfe61cf1SDavid Greenman * completed). 286dfe61cf1SDavid Greenman */ 28728935f27SMaxime Henrion static void 288f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc) 289a17c678eSDavid Greenman { 290a17c678eSDavid Greenman int i = 10000; 291a17c678eSDavid Greenman 2927dced78aSDavid Greenman while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i) 2937dced78aSDavid Greenman DELAY(2); 2947dced78aSDavid Greenman if (i == 0) 29500c4116bSJonathan Lemon device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n", 296e8c8b728SJonathan Lemon CSR_READ_1(sc, FXP_CSR_SCB_COMMAND), 297e8c8b728SJonathan Lemon CSR_READ_1(sc, FXP_CSR_SCB_STATACK), 298e8c8b728SJonathan Lemon CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), 299e8c8b728SJonathan Lemon CSR_READ_2(sc, FXP_CSR_FLOWCONTROL)); 3007dced78aSDavid Greenman } 3017dced78aSDavid Greenman 30228935f27SMaxime Henrion static void 3032e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd) 3042e2b8238SJonathan Lemon { 3052e2b8238SJonathan Lemon 3062e2b8238SJonathan Lemon if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) { 3072e2b8238SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP); 3082e2b8238SJonathan Lemon fxp_scb_wait(sc); 3092e2b8238SJonathan Lemon } 3102e2b8238SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd); 3112e2b8238SJonathan Lemon } 3122e2b8238SJonathan Lemon 31328935f27SMaxime Henrion static void 31474d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status, 315209b07bcSMaxime Henrion bus_dma_tag_t dmat, bus_dmamap_t map) 3167dced78aSDavid Greenman { 3177dced78aSDavid Greenman int i = 10000; 3187dced78aSDavid Greenman 319209b07bcSMaxime Henrion bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD); 320209b07bcSMaxime Henrion while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i) { 3217dced78aSDavid Greenman DELAY(2); 322209b07bcSMaxime Henrion bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD); 323209b07bcSMaxime Henrion } 3247dced78aSDavid Greenman if (i == 0) 325f7788e8eSJonathan Lemon device_printf(sc->dev, "DMA timeout\n"); 326a17c678eSDavid Greenman } 327a17c678eSDavid Greenman 328dfe61cf1SDavid Greenman /* 32928935f27SMaxime Henrion * Return identification string if this device is ours. 330dfe61cf1SDavid Greenman */ 3316182fdbdSPeter Wemm static int 3326182fdbdSPeter Wemm fxp_probe(device_t dev) 333a17c678eSDavid Greenman { 33474d1ed23SMaxime Henrion uint16_t devid; 33574d1ed23SMaxime Henrion uint8_t revid; 336f7788e8eSJonathan Lemon struct fxp_ident *ident; 337f7788e8eSJonathan Lemon 33855ce7b51SDavid Greenman if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) { 339f7788e8eSJonathan Lemon devid = pci_get_device(dev); 340f19fc5d8SJohn Polstra revid = pci_get_revid(dev); 341f7788e8eSJonathan Lemon for (ident = fxp_ident_table; ident->name != NULL; ident++) { 342f19fc5d8SJohn Polstra if (ident->devid == devid && 343f19fc5d8SJohn Polstra (ident->revid == revid || ident->revid == -1)) { 344f7788e8eSJonathan Lemon device_set_desc(dev, ident->name); 345538565c4SWarner Losh return (BUS_PROBE_DEFAULT); 34655ce7b51SDavid Greenman } 347dd68ef16SPeter Wemm } 348f7788e8eSJonathan Lemon } 349f7788e8eSJonathan Lemon return (ENXIO); 3506182fdbdSPeter Wemm } 3516182fdbdSPeter Wemm 352b2badf02SMaxime Henrion static void 353b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 354b2badf02SMaxime Henrion { 35574d1ed23SMaxime Henrion uint32_t *addr; 356b2badf02SMaxime Henrion 357b2badf02SMaxime Henrion if (error) 358b2badf02SMaxime Henrion return; 359b2badf02SMaxime Henrion 360b2badf02SMaxime Henrion KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 361b2badf02SMaxime Henrion addr = arg; 362b2badf02SMaxime Henrion *addr = segs->ds_addr; 363b2badf02SMaxime Henrion } 364b2badf02SMaxime Henrion 3656182fdbdSPeter Wemm static int 3666182fdbdSPeter Wemm fxp_attach(device_t dev) 367a17c678eSDavid Greenman { 3686720ebccSMaxime Henrion struct fxp_softc *sc; 3696720ebccSMaxime Henrion struct fxp_cb_tx *tcbp; 3706720ebccSMaxime Henrion struct fxp_tx *txp; 371b2badf02SMaxime Henrion struct fxp_rx *rxp; 3726720ebccSMaxime Henrion struct ifnet *ifp; 37374d1ed23SMaxime Henrion uint32_t val; 37474d1ed23SMaxime Henrion uint16_t data, myea[ETHER_ADDR_LEN / 2]; 375d73e2e55SMaxime Henrion int i, rid, m1, m2, prefer_iomap, maxtxseg; 3766720ebccSMaxime Henrion int error, s; 377a17c678eSDavid Greenman 3786720ebccSMaxime Henrion error = 0; 3796720ebccSMaxime Henrion sc = device_get_softc(dev); 380f7788e8eSJonathan Lemon sc->dev = dev; 38145276e4aSSam Leffler callout_init(&sc->stat_ch, CALLOUT_MPSAFE); 3826008862bSJohn Baldwin mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 3834953bccaSNate Lawson MTX_DEF); 3844953bccaSNate Lawson ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd, 3854953bccaSNate Lawson fxp_serial_ifmedia_sts); 386a17c678eSDavid Greenman 387f7788e8eSJonathan Lemon s = splimp(); 388a17c678eSDavid Greenman 389dfe61cf1SDavid Greenman /* 3902bce79a2SMaxim Sobolev * Enable bus mastering. 391df373873SWes Peters */ 392cf0d8a1eSMaxim Sobolev pci_enable_busmaster(dev); 3939fa6ccfbSMatt Jacob val = pci_read_config(dev, PCIR_COMMAND, 2); 39479495006SWarner Losh 395df373873SWes Peters /* 3969fa6ccfbSMatt Jacob * Figure out which we should try first - memory mapping or i/o mapping? 3979fa6ccfbSMatt Jacob * We default to memory mapping. Then we accept an override from the 3989fa6ccfbSMatt Jacob * command line. Then we check to see which one is enabled. 399dfe61cf1SDavid Greenman */ 4009fa6ccfbSMatt Jacob m1 = PCIM_CMD_MEMEN; 4019fa6ccfbSMatt Jacob m2 = PCIM_CMD_PORTEN; 4022a05a4ebSMatt Jacob prefer_iomap = 0; 4032a05a4ebSMatt Jacob if (resource_int_value(device_get_name(dev), device_get_unit(dev), 4042a05a4ebSMatt Jacob "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) { 4059fa6ccfbSMatt Jacob m1 = PCIM_CMD_PORTEN; 4069fa6ccfbSMatt Jacob m2 = PCIM_CMD_MEMEN; 4079fa6ccfbSMatt Jacob } 4089fa6ccfbSMatt Jacob 409533294b9SMatthew N. Dodd sc->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT; 4109fa6ccfbSMatt Jacob sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA; 4115f96beb9SNate Lawson sc->mem = bus_alloc_resource_any(dev, sc->rtp, &sc->rgd, RF_ACTIVE); 412533294b9SMatthew N. Dodd if (sc->mem == NULL) { 4139fa6ccfbSMatt Jacob sc->rtp = 4149fa6ccfbSMatt Jacob (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT; 4159fa6ccfbSMatt Jacob sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA; 4165f96beb9SNate Lawson sc->mem = bus_alloc_resource_any(dev, sc->rtp, &sc->rgd, 4175f96beb9SNate Lawson RF_ACTIVE); 4189fa6ccfbSMatt Jacob } 4199fa6ccfbSMatt Jacob 4206182fdbdSPeter Wemm if (!sc->mem) { 4216182fdbdSPeter Wemm error = ENXIO; 422a17c678eSDavid Greenman goto fail; 423a17c678eSDavid Greenman } 4249fa6ccfbSMatt Jacob if (bootverbose) { 4259fa6ccfbSMatt Jacob device_printf(dev, "using %s space register mapping\n", 4269fa6ccfbSMatt Jacob sc->rtp == SYS_RES_MEMORY? "memory" : "I/O"); 4279fa6ccfbSMatt Jacob } 4284fc1dda9SAndrew Gallatin 4294fc1dda9SAndrew Gallatin sc->sc_st = rman_get_bustag(sc->mem); 4304fc1dda9SAndrew Gallatin sc->sc_sh = rman_get_bushandle(sc->mem); 431a17c678eSDavid Greenman 432a17c678eSDavid Greenman /* 433dfe61cf1SDavid Greenman * Allocate our interrupt. 434dfe61cf1SDavid Greenman */ 4356182fdbdSPeter Wemm rid = 0; 4365f96beb9SNate Lawson sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 4376182fdbdSPeter Wemm RF_SHAREABLE | RF_ACTIVE); 4386182fdbdSPeter Wemm if (sc->irq == NULL) { 4396182fdbdSPeter Wemm device_printf(dev, "could not map interrupt\n"); 4406182fdbdSPeter Wemm error = ENXIO; 4416182fdbdSPeter Wemm goto fail; 4426182fdbdSPeter Wemm } 4436182fdbdSPeter Wemm 444f7788e8eSJonathan Lemon /* 445f7788e8eSJonathan Lemon * Reset to a stable state. 446f7788e8eSJonathan Lemon */ 447f7788e8eSJonathan Lemon CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET); 448f7788e8eSJonathan Lemon DELAY(10); 449f7788e8eSJonathan Lemon 450f7788e8eSJonathan Lemon /* 451f7788e8eSJonathan Lemon * Find out how large of an SEEPROM we have. 452f7788e8eSJonathan Lemon */ 453f7788e8eSJonathan Lemon fxp_autosize_eeprom(sc); 454f7788e8eSJonathan Lemon 455f7788e8eSJonathan Lemon /* 45693b6e2e6SMaxime Henrion * Find out the chip revision; lump all 82557 revs together. 45793b6e2e6SMaxime Henrion */ 45893b6e2e6SMaxime Henrion fxp_read_eeprom(sc, &data, 5, 1); 45993b6e2e6SMaxime Henrion if ((data >> 8) == 1) 46093b6e2e6SMaxime Henrion sc->revision = FXP_REV_82557; 46193b6e2e6SMaxime Henrion else 46293b6e2e6SMaxime Henrion sc->revision = pci_get_revid(dev); 46393b6e2e6SMaxime Henrion 46493b6e2e6SMaxime Henrion /* 4653bd07cfdSJonathan Lemon * Determine whether we must use the 503 serial interface. 466f7788e8eSJonathan Lemon */ 467f7788e8eSJonathan Lemon fxp_read_eeprom(sc, &data, 6, 1); 46893b6e2e6SMaxime Henrion if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0 4694ed53076SMaxime Henrion && (data & FXP_PHY_SERIAL_ONLY)) 470dedabebfSJonathan Lemon sc->flags |= FXP_FLAG_SERIAL_MEDIA; 471f7788e8eSJonathan Lemon 4720f1db1d6SMaxime Henrion SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 4730f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 47450a33b6aSPawel Jakub Dawidek OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW, 475858b84f5SPoul-Henning Kamp &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I", 47672a32a26SJonathan Lemon "FXP driver receive interrupt microcode bundling delay"); 4770f1db1d6SMaxime Henrion SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 4780f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 47950a33b6aSPawel Jakub Dawidek OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW, 480858b84f5SPoul-Henning Kamp &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I", 48172a32a26SJonathan Lemon "FXP driver receive interrupt microcode bundle size limit"); 4820f1db1d6SMaxime Henrion SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 4830f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 4840f1db1d6SMaxime Henrion OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0, 4850f1db1d6SMaxime Henrion "FXP RNR events"); 4860f1db1d6SMaxime Henrion SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 4870f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 4880f1db1d6SMaxime Henrion OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0, 4890f1db1d6SMaxime Henrion "FXP flow control disabled"); 49072a32a26SJonathan Lemon 49172a32a26SJonathan Lemon /* 49272a32a26SJonathan Lemon * Pull in device tunables. 49372a32a26SJonathan Lemon */ 49472a32a26SJonathan Lemon sc->tunable_int_delay = TUNABLE_INT_DELAY; 49572a32a26SJonathan Lemon sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX; 49603edfff3SRobert Watson sc->tunable_noflow = 1; 49772a32a26SJonathan Lemon (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 49872a32a26SJonathan Lemon "int_delay", &sc->tunable_int_delay); 49972a32a26SJonathan Lemon (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 50072a32a26SJonathan Lemon "bundle_max", &sc->tunable_bundle_max); 5010f1db1d6SMaxime Henrion (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 5020f1db1d6SMaxime Henrion "noflow", &sc->tunable_noflow); 5030f1db1d6SMaxime Henrion sc->rnr = 0; 50472a32a26SJonathan Lemon 50572a32a26SJonathan Lemon /* 5062e2b8238SJonathan Lemon * Enable workarounds for certain chip revision deficiencies. 50700c4116bSJonathan Lemon * 50872a32a26SJonathan Lemon * Systems based on the ICH2/ICH2-M chip from Intel, and possibly 50972a32a26SJonathan Lemon * some systems based a normal 82559 design, have a defect where 51072a32a26SJonathan Lemon * the chip can cause a PCI protocol violation if it receives 51100c4116bSJonathan Lemon * a CU_RESUME command when it is entering the IDLE state. The 51200c4116bSJonathan Lemon * workaround is to disable Dynamic Standby Mode, so the chip never 51300c4116bSJonathan Lemon * deasserts CLKRUN#, and always remains in an active state. 51400c4116bSJonathan Lemon * 51500c4116bSJonathan Lemon * See Intel 82801BA/82801BAM Specification Update, Errata #30. 5162e2b8238SJonathan Lemon */ 5172e2b8238SJonathan Lemon i = pci_get_device(dev); 51872a32a26SJonathan Lemon if (i == 0x2449 || (i > 0x1030 && i < 0x1039) || 51972a32a26SJonathan Lemon sc->revision >= FXP_REV_82559_A0) { 52000c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, 10, 1); 52100c4116bSJonathan Lemon if (data & 0x02) { /* STB enable */ 52274d1ed23SMaxime Henrion uint16_t cksum; 52300c4116bSJonathan Lemon int i; 52400c4116bSJonathan Lemon 52500c4116bSJonathan Lemon device_printf(dev, 526001cfa92SJonathan Lemon "Disabling dynamic standby mode in EEPROM\n"); 52700c4116bSJonathan Lemon data &= ~0x02; 52800c4116bSJonathan Lemon fxp_write_eeprom(sc, &data, 10, 1); 52900c4116bSJonathan Lemon device_printf(dev, "New EEPROM ID: 0x%x\n", data); 53000c4116bSJonathan Lemon cksum = 0; 53100c4116bSJonathan Lemon for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) { 53200c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, i, 1); 53300c4116bSJonathan Lemon cksum += data; 53400c4116bSJonathan Lemon } 53500c4116bSJonathan Lemon i = (1 << sc->eeprom_size) - 1; 53600c4116bSJonathan Lemon cksum = 0xBABA - cksum; 53700c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, i, 1); 53800c4116bSJonathan Lemon fxp_write_eeprom(sc, &cksum, i, 1); 53900c4116bSJonathan Lemon device_printf(dev, 54000c4116bSJonathan Lemon "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n", 54100c4116bSJonathan Lemon i, data, cksum); 54200c4116bSJonathan Lemon #if 1 54300c4116bSJonathan Lemon /* 54400c4116bSJonathan Lemon * If the user elects to continue, try the software 54500c4116bSJonathan Lemon * workaround, as it is better than nothing. 54600c4116bSJonathan Lemon */ 5472e2b8238SJonathan Lemon sc->flags |= FXP_FLAG_CU_RESUME_BUG; 54800c4116bSJonathan Lemon #endif 54900c4116bSJonathan Lemon } 55000c4116bSJonathan Lemon } 5512e2b8238SJonathan Lemon 5522e2b8238SJonathan Lemon /* 5533bd07cfdSJonathan Lemon * If we are not a 82557 chip, we can enable extended features. 5543bd07cfdSJonathan Lemon */ 55572a32a26SJonathan Lemon if (sc->revision != FXP_REV_82557) { 5563bd07cfdSJonathan Lemon /* 55774396a0aSJonathan Lemon * If MWI is enabled in the PCI configuration, and there 55874396a0aSJonathan Lemon * is a valid cacheline size (8 or 16 dwords), then tell 55974396a0aSJonathan Lemon * the board to turn on MWI. 5603bd07cfdSJonathan Lemon */ 56174396a0aSJonathan Lemon if (val & PCIM_CMD_MWRICEN && 56274396a0aSJonathan Lemon pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0) 5633bd07cfdSJonathan Lemon sc->flags |= FXP_FLAG_MWI_ENABLE; 5643bd07cfdSJonathan Lemon 5653bd07cfdSJonathan Lemon /* turn on the extended TxCB feature */ 5663bd07cfdSJonathan Lemon sc->flags |= FXP_FLAG_EXT_TXCB; 56744e0bc11SYaroslav Tykhiy 56844e0bc11SYaroslav Tykhiy /* enable reception of long frames for VLAN */ 56944e0bc11SYaroslav Tykhiy sc->flags |= FXP_FLAG_LONG_PKT_EN; 57044e0bc11SYaroslav Tykhiy } else { 57144e0bc11SYaroslav Tykhiy /* a hack to get long VLAN frames on a 82557 */ 57244e0bc11SYaroslav Tykhiy sc->flags |= FXP_FLAG_SAVE_BAD; 5733bd07cfdSJonathan Lemon } 5743bd07cfdSJonathan Lemon 5753bd07cfdSJonathan Lemon /* 576c8bca6dcSBill Paul * Enable use of extended RFDs and TCBs for 82550 577c8bca6dcSBill Paul * and later chips. Note: we need extended TXCB support 578c8bca6dcSBill Paul * too, but that's already enabled by the code above. 579c8bca6dcSBill Paul * Be careful to do this only on the right devices. 580c8bca6dcSBill Paul */ 581c8bca6dcSBill Paul 582414ce15cSDon Lewis if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C) { 583c8bca6dcSBill Paul sc->rfa_size = sizeof (struct fxp_rfa); 584c8bca6dcSBill Paul sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT; 585c8bca6dcSBill Paul sc->flags |= FXP_FLAG_EXT_RFA; 586c8bca6dcSBill Paul } else { 587c8bca6dcSBill Paul sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN; 588c8bca6dcSBill Paul sc->tx_cmd = FXP_CB_COMMAND_XMIT; 589c8bca6dcSBill Paul } 590c8bca6dcSBill Paul 591c8bca6dcSBill Paul /* 592b2badf02SMaxime Henrion * Allocate DMA tags and DMA safe memory. 593b2badf02SMaxime Henrion */ 594d73e2e55SMaxime Henrion maxtxseg = sc->flags & FXP_FLAG_EXT_RFA ? FXP_NTXSEG - 1 : FXP_NTXSEG; 595b2badf02SMaxime Henrion error = bus_dma_tag_create(NULL, 2, 0, BUS_SPACE_MAXADDR_32BIT, 596d73e2e55SMaxime Henrion BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * maxtxseg, 597f6b1c44dSScott Long maxtxseg, MCLBYTES, 0, busdma_lock_mutex, &Giant, &sc->fxp_mtag); 598b2badf02SMaxime Henrion if (error) { 599b2badf02SMaxime Henrion device_printf(dev, "could not allocate dma tag\n"); 600b2badf02SMaxime Henrion goto fail; 601b2badf02SMaxime Henrion } 602b2badf02SMaxime Henrion 603b2badf02SMaxime Henrion error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 604b2badf02SMaxime Henrion BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_stats), 1, 605f6b1c44dSScott Long sizeof(struct fxp_stats), 0, busdma_lock_mutex, &Giant, 606f6b1c44dSScott Long &sc->fxp_stag); 607b2badf02SMaxime Henrion if (error) { 608b2badf02SMaxime Henrion device_printf(dev, "could not allocate dma tag\n"); 609b2badf02SMaxime Henrion goto fail; 610b2badf02SMaxime Henrion } 611b2badf02SMaxime Henrion 612b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats, 613aafb3ebbSMaxime Henrion BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap); 614b2badf02SMaxime Henrion if (error) 6154953bccaSNate Lawson goto fail; 616b2badf02SMaxime Henrion error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats, 617b2badf02SMaxime Henrion sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0); 618b2badf02SMaxime Henrion if (error) { 619b2badf02SMaxime Henrion device_printf(dev, "could not map the stats buffer\n"); 620b2badf02SMaxime Henrion goto fail; 621b2badf02SMaxime Henrion } 622b2badf02SMaxime Henrion 623b2badf02SMaxime Henrion error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 624b2badf02SMaxime Henrion BUS_SPACE_MAXADDR, NULL, NULL, FXP_TXCB_SZ, 1, 625f6b1c44dSScott Long FXP_TXCB_SZ, 0, busdma_lock_mutex, &Giant, &sc->cbl_tag); 626b2badf02SMaxime Henrion if (error) { 627b2badf02SMaxime Henrion device_printf(dev, "could not allocate dma tag\n"); 628b2badf02SMaxime Henrion goto fail; 629b2badf02SMaxime Henrion } 630b2badf02SMaxime Henrion 631b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list, 632aafb3ebbSMaxime Henrion BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map); 633b2badf02SMaxime Henrion if (error) 6344953bccaSNate Lawson goto fail; 635b2badf02SMaxime Henrion 636b2badf02SMaxime Henrion error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map, 637b2badf02SMaxime Henrion sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr, 638b2badf02SMaxime Henrion &sc->fxp_desc.cbl_addr, 0); 639b2badf02SMaxime Henrion if (error) { 640b2badf02SMaxime Henrion device_printf(dev, "could not map DMA memory\n"); 641b2badf02SMaxime Henrion goto fail; 642b2badf02SMaxime Henrion } 643b2badf02SMaxime Henrion 644b2badf02SMaxime Henrion error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 645b2badf02SMaxime Henrion BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_cb_mcs), 1, 646f6b1c44dSScott Long sizeof(struct fxp_cb_mcs), 0, busdma_lock_mutex, &Giant, 647f6b1c44dSScott Long &sc->mcs_tag); 648b2badf02SMaxime Henrion if (error) { 649b2badf02SMaxime Henrion device_printf(dev, "could not allocate dma tag\n"); 650b2badf02SMaxime Henrion goto fail; 651b2badf02SMaxime Henrion } 652b2badf02SMaxime Henrion 653b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp, 654b2badf02SMaxime Henrion BUS_DMA_NOWAIT, &sc->mcs_map); 655b2badf02SMaxime Henrion if (error) 6564953bccaSNate Lawson goto fail; 657b2badf02SMaxime Henrion error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp, 658b2badf02SMaxime Henrion sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0); 659b2badf02SMaxime Henrion if (error) { 660b2badf02SMaxime Henrion device_printf(dev, "can't map the multicast setup command\n"); 661b2badf02SMaxime Henrion goto fail; 662b2badf02SMaxime Henrion } 663b2badf02SMaxime Henrion 664b2badf02SMaxime Henrion /* 6656720ebccSMaxime Henrion * Pre-allocate the TX DMA maps and setup the pointers to 6666720ebccSMaxime Henrion * the TX command blocks. 667b2badf02SMaxime Henrion */ 6686720ebccSMaxime Henrion txp = sc->fxp_desc.tx_list; 6696720ebccSMaxime Henrion tcbp = sc->fxp_desc.cbl_list; 6704cec1653SMaxime Henrion for (i = 0; i < FXP_NTXCB; i++) { 6716720ebccSMaxime Henrion txp[i].tx_cb = tcbp + i; 6726720ebccSMaxime Henrion error = bus_dmamap_create(sc->fxp_mtag, 0, &txp[i].tx_map); 673b2badf02SMaxime Henrion if (error) { 674b2badf02SMaxime Henrion device_printf(dev, "can't create DMA map for TX\n"); 675b2badf02SMaxime Henrion goto fail; 676b2badf02SMaxime Henrion } 677b2badf02SMaxime Henrion } 678b2badf02SMaxime Henrion error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map); 679b2badf02SMaxime Henrion if (error) { 680b2badf02SMaxime Henrion device_printf(dev, "can't create spare DMA map\n"); 681b2badf02SMaxime Henrion goto fail; 682b2badf02SMaxime Henrion } 683b2badf02SMaxime Henrion 684b2badf02SMaxime Henrion /* 685b2badf02SMaxime Henrion * Pre-allocate our receive buffers. 686b2badf02SMaxime Henrion */ 687b2badf02SMaxime Henrion sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL; 688b2badf02SMaxime Henrion for (i = 0; i < FXP_NRFABUFS; i++) { 689b2badf02SMaxime Henrion rxp = &sc->fxp_desc.rx_list[i]; 690b2badf02SMaxime Henrion error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map); 691b2badf02SMaxime Henrion if (error) { 692b2badf02SMaxime Henrion device_printf(dev, "can't create DMA map for RX\n"); 693b2badf02SMaxime Henrion goto fail; 694b2badf02SMaxime Henrion } 6954953bccaSNate Lawson if (fxp_add_rfabuf(sc, rxp) != 0) { 6964953bccaSNate Lawson error = ENOMEM; 6974953bccaSNate Lawson goto fail; 6984953bccaSNate Lawson } 699b2badf02SMaxime Henrion } 700b2badf02SMaxime Henrion 701b2badf02SMaxime Henrion /* 702f7788e8eSJonathan Lemon * Read MAC address. 703f7788e8eSJonathan Lemon */ 70483e6547dSMaxime Henrion fxp_read_eeprom(sc, myea, 0, 3); 70583e6547dSMaxime Henrion sc->arpcom.ac_enaddr[0] = myea[0] & 0xff; 70683e6547dSMaxime Henrion sc->arpcom.ac_enaddr[1] = myea[0] >> 8; 70783e6547dSMaxime Henrion sc->arpcom.ac_enaddr[2] = myea[1] & 0xff; 70883e6547dSMaxime Henrion sc->arpcom.ac_enaddr[3] = myea[1] >> 8; 70983e6547dSMaxime Henrion sc->arpcom.ac_enaddr[4] = myea[2] & 0xff; 71083e6547dSMaxime Henrion sc->arpcom.ac_enaddr[5] = myea[2] >> 8; 711f7788e8eSJonathan Lemon if (bootverbose) { 7122e2b8238SJonathan Lemon device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n", 713f7788e8eSJonathan Lemon pci_get_vendor(dev), pci_get_device(dev), 7142e2b8238SJonathan Lemon pci_get_subvendor(dev), pci_get_subdevice(dev), 7152e2b8238SJonathan Lemon pci_get_revid(dev)); 71672a32a26SJonathan Lemon fxp_read_eeprom(sc, &data, 10, 1); 71772a32a26SJonathan Lemon device_printf(dev, "Dynamic Standby mode is %s\n", 71872a32a26SJonathan Lemon data & 0x02 ? "enabled" : "disabled"); 719f7788e8eSJonathan Lemon } 720f7788e8eSJonathan Lemon 721f7788e8eSJonathan Lemon /* 722f7788e8eSJonathan Lemon * If this is only a 10Mbps device, then there is no MII, and 723f7788e8eSJonathan Lemon * the PHY will use a serial interface instead. 724f7788e8eSJonathan Lemon * 725f7788e8eSJonathan Lemon * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter 726f7788e8eSJonathan Lemon * doesn't have a programming interface of any sort. The 727f7788e8eSJonathan Lemon * media is sensed automatically based on how the link partner 728f7788e8eSJonathan Lemon * is configured. This is, in essence, manual configuration. 729f7788e8eSJonathan Lemon */ 730f7788e8eSJonathan Lemon if (sc->flags & FXP_FLAG_SERIAL_MEDIA) { 731f7788e8eSJonathan Lemon ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 732f7788e8eSJonathan Lemon ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL); 733f7788e8eSJonathan Lemon } else { 734f7788e8eSJonathan Lemon if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd, 735f7788e8eSJonathan Lemon fxp_ifmedia_sts)) { 736f7788e8eSJonathan Lemon device_printf(dev, "MII without any PHY!\n"); 7376182fdbdSPeter Wemm error = ENXIO; 738ba8c6fd5SDavid Greenman goto fail; 739a17c678eSDavid Greenman } 740f7788e8eSJonathan Lemon } 741dccee1a1SDavid Greenman 742a17c678eSDavid Greenman ifp = &sc->arpcom.ac_if; 7439bf40edeSBrooks Davis if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 744a330e1f1SGary Palmer ifp->if_baudrate = 100000000; 745fb583156SDavid Greenman ifp->if_init = fxp_init; 746ba8c6fd5SDavid Greenman ifp->if_softc = sc; 747ba8c6fd5SDavid Greenman ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 748ba8c6fd5SDavid Greenman ifp->if_ioctl = fxp_ioctl; 749ba8c6fd5SDavid Greenman ifp->if_start = fxp_start; 750ba8c6fd5SDavid Greenman ifp->if_watchdog = fxp_watchdog; 751a17c678eSDavid Greenman 7525fe9116bSYaroslav Tykhiy ifp->if_capabilities = ifp->if_capenable = 0; 7535fe9116bSYaroslav Tykhiy 754c8bca6dcSBill Paul /* Enable checksum offload for 82550 or better chips */ 755c8bca6dcSBill Paul if (sc->flags & FXP_FLAG_EXT_RFA) { 756c8bca6dcSBill Paul ifp->if_hwassist = FXP_CSUM_FEATURES; 7575fe9116bSYaroslav Tykhiy ifp->if_capabilities |= IFCAP_HWCSUM; 7585fe9116bSYaroslav Tykhiy ifp->if_capenable |= IFCAP_HWCSUM; 759c8bca6dcSBill Paul } 760c8bca6dcSBill Paul 761fb917226SRuslan Ermilov #ifdef DEVICE_POLLING 762fb917226SRuslan Ermilov /* Inform the world we support polling. */ 763fb917226SRuslan Ermilov ifp->if_capabilities |= IFCAP_POLLING; 764fb917226SRuslan Ermilov ifp->if_capenable |= IFCAP_POLLING; 765fb917226SRuslan Ermilov #endif 766fb917226SRuslan Ermilov 767dfe61cf1SDavid Greenman /* 7684953bccaSNate Lawson * Attach the interface. 7694953bccaSNate Lawson */ 7704953bccaSNate Lawson ether_ifattach(ifp, sc->arpcom.ac_enaddr); 7714953bccaSNate Lawson 7724953bccaSNate Lawson /* 773e8c8b728SJonathan Lemon * Tell the upper layer(s) we support long frames. 7745fe9116bSYaroslav Tykhiy * Must appear after the call to ether_ifattach() because 7755fe9116bSYaroslav Tykhiy * ether_ifattach() sets ifi_hdrlen to the default value. 776e8c8b728SJonathan Lemon */ 777e8c8b728SJonathan Lemon ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 778673d9191SSam Leffler ifp->if_capabilities |= IFCAP_VLAN_MTU; 77944e0bc11SYaroslav Tykhiy ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */ 780e8c8b728SJonathan Lemon 781483b9871SDavid Greenman /* 7823114fdb4SDavid Greenman * Let the system queue as many packets as we have available 7833114fdb4SDavid Greenman * TX descriptors. 784483b9871SDavid Greenman */ 7857929aa03SMax Laier IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1); 7867929aa03SMax Laier ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1; 7877929aa03SMax Laier IFQ_SET_READY(&ifp->if_snd); 7884a684684SDavid Greenman 789201afb0eSMaxime Henrion /* 7904953bccaSNate Lawson * Hook our interrupt after all initialization is complete. 791201afb0eSMaxime Henrion */ 792b237430cSSam Leffler error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 793201afb0eSMaxime Henrion fxp_intr, sc, &sc->ih); 794201afb0eSMaxime Henrion if (error) { 795201afb0eSMaxime Henrion device_printf(dev, "could not setup irq\n"); 7964953bccaSNate Lawson ether_ifdetach(&sc->arpcom.ac_if); 797201afb0eSMaxime Henrion goto fail; 798201afb0eSMaxime Henrion } 799201afb0eSMaxime Henrion 800a17c678eSDavid Greenman fail: 801f7788e8eSJonathan Lemon splx(s); 8024953bccaSNate Lawson if (error) 803f7788e8eSJonathan Lemon fxp_release(sc); 804f7788e8eSJonathan Lemon return (error); 805f7788e8eSJonathan Lemon } 806f7788e8eSJonathan Lemon 807f7788e8eSJonathan Lemon /* 8084953bccaSNate Lawson * Release all resources. The softc lock should not be held and the 8094953bccaSNate Lawson * interrupt should already be torn down. 810f7788e8eSJonathan Lemon */ 811f7788e8eSJonathan Lemon static void 812f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc) 813f7788e8eSJonathan Lemon { 814b2badf02SMaxime Henrion struct fxp_rx *rxp; 815b2badf02SMaxime Henrion struct fxp_tx *txp; 816b2badf02SMaxime Henrion int i; 817b2badf02SMaxime Henrion 81867fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_NOTOWNED); 819670f5d73SMaxime Henrion KASSERT(sc->ih == NULL, 820670f5d73SMaxime Henrion ("fxp_release() called with intr handle still active")); 8214953bccaSNate Lawson if (sc->miibus) 8224953bccaSNate Lawson device_delete_child(sc->dev, sc->miibus); 8234953bccaSNate Lawson bus_generic_detach(sc->dev); 8244953bccaSNate Lawson ifmedia_removeall(&sc->sc_media); 825b2badf02SMaxime Henrion if (sc->fxp_desc.cbl_list) { 826b2badf02SMaxime Henrion bus_dmamap_unload(sc->cbl_tag, sc->cbl_map); 827b2badf02SMaxime Henrion bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list, 828b2badf02SMaxime Henrion sc->cbl_map); 829b2badf02SMaxime Henrion } 830b2badf02SMaxime Henrion if (sc->fxp_stats) { 831b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap); 832b2badf02SMaxime Henrion bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap); 833b2badf02SMaxime Henrion } 834b2badf02SMaxime Henrion if (sc->mcsp) { 835b2badf02SMaxime Henrion bus_dmamap_unload(sc->mcs_tag, sc->mcs_map); 836b2badf02SMaxime Henrion bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map); 837b2badf02SMaxime Henrion } 838f7788e8eSJonathan Lemon if (sc->irq) 839f7788e8eSJonathan Lemon bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq); 840f7788e8eSJonathan Lemon if (sc->mem) 841f7788e8eSJonathan Lemon bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem); 842b983c7b3SMaxime Henrion if (sc->fxp_mtag) { 843b983c7b3SMaxime Henrion for (i = 0; i < FXP_NRFABUFS; i++) { 844b983c7b3SMaxime Henrion rxp = &sc->fxp_desc.rx_list[i]; 845b983c7b3SMaxime Henrion if (rxp->rx_mbuf != NULL) { 846b983c7b3SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map, 847b983c7b3SMaxime Henrion BUS_DMASYNC_POSTREAD); 848b983c7b3SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map); 849b983c7b3SMaxime Henrion m_freem(rxp->rx_mbuf); 850b983c7b3SMaxime Henrion } 851b983c7b3SMaxime Henrion bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map); 852b983c7b3SMaxime Henrion } 853b983c7b3SMaxime Henrion bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map); 854b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->fxp_mtag); 855b983c7b3SMaxime Henrion } 856b983c7b3SMaxime Henrion if (sc->fxp_stag) { 857b983c7b3SMaxime Henrion for (i = 0; i < FXP_NTXCB; i++) { 858b983c7b3SMaxime Henrion txp = &sc->fxp_desc.tx_list[i]; 859b983c7b3SMaxime Henrion if (txp->tx_mbuf != NULL) { 860b983c7b3SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, 861b983c7b3SMaxime Henrion BUS_DMASYNC_POSTWRITE); 862b983c7b3SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, txp->tx_map); 863b983c7b3SMaxime Henrion m_freem(txp->tx_mbuf); 864b983c7b3SMaxime Henrion } 865b983c7b3SMaxime Henrion bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map); 866b983c7b3SMaxime Henrion } 867b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->fxp_stag); 868b983c7b3SMaxime Henrion } 869b2badf02SMaxime Henrion if (sc->cbl_tag) 870b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->cbl_tag); 871b2badf02SMaxime Henrion if (sc->mcs_tag) 872b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->mcs_tag); 87372a32a26SJonathan Lemon 8740f4dc94cSChuck Paterson mtx_destroy(&sc->sc_mtx); 8756182fdbdSPeter Wemm } 8766182fdbdSPeter Wemm 8776182fdbdSPeter Wemm /* 8786182fdbdSPeter Wemm * Detach interface. 8796182fdbdSPeter Wemm */ 8806182fdbdSPeter Wemm static int 8816182fdbdSPeter Wemm fxp_detach(device_t dev) 8826182fdbdSPeter Wemm { 8836182fdbdSPeter Wemm struct fxp_softc *sc = device_get_softc(dev); 884f7788e8eSJonathan Lemon int s; 8856182fdbdSPeter Wemm 8864953bccaSNate Lawson FXP_LOCK(sc); 887f7788e8eSJonathan Lemon s = splimp(); 88832cd7a9cSWarner Losh 8891d2945d5SWarner Losh sc->suspended = 1; /* Do same thing as we do for suspend */ 8906182fdbdSPeter Wemm /* 891f7788e8eSJonathan Lemon * Close down routes etc. 8926182fdbdSPeter Wemm */ 893673d9191SSam Leffler ether_ifdetach(&sc->arpcom.ac_if); 89420f0c80fSMaxime Henrion 89520f0c80fSMaxime Henrion /* 89632cd7a9cSWarner Losh * Stop DMA and drop transmit queue, but disable interrupts first. 89720f0c80fSMaxime Henrion */ 89820f0c80fSMaxime Henrion CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 89920f0c80fSMaxime Henrion fxp_stop(sc); 90032cd7a9cSWarner Losh FXP_UNLOCK(sc); 90120f0c80fSMaxime Henrion 9026182fdbdSPeter Wemm /* 9034953bccaSNate Lawson * Unhook interrupt before dropping lock. This is to prevent 9044953bccaSNate Lawson * races with fxp_intr(). 9056182fdbdSPeter Wemm */ 9064953bccaSNate Lawson bus_teardown_intr(sc->dev, sc->irq, sc->ih); 9074953bccaSNate Lawson sc->ih = NULL; 9086182fdbdSPeter Wemm 909f7788e8eSJonathan Lemon splx(s); 9106182fdbdSPeter Wemm 911f7788e8eSJonathan Lemon /* Release our allocated resources. */ 912f7788e8eSJonathan Lemon fxp_release(sc); 913f7788e8eSJonathan Lemon return (0); 914a17c678eSDavid Greenman } 915a17c678eSDavid Greenman 916a17c678eSDavid Greenman /* 9174a684684SDavid Greenman * Device shutdown routine. Called at system shutdown after sync. The 918a17c678eSDavid Greenman * main purpose of this routine is to shut off receiver DMA so that 919a17c678eSDavid Greenman * kernel memory doesn't get clobbered during warmboot. 920a17c678eSDavid Greenman */ 9216182fdbdSPeter Wemm static int 9226182fdbdSPeter Wemm fxp_shutdown(device_t dev) 923a17c678eSDavid Greenman { 9246182fdbdSPeter Wemm /* 9256182fdbdSPeter Wemm * Make sure that DMA is disabled prior to reboot. Not doing 9266182fdbdSPeter Wemm * do could allow DMA to corrupt kernel memory during the 9276182fdbdSPeter Wemm * reboot before the driver initializes. 9286182fdbdSPeter Wemm */ 9296182fdbdSPeter Wemm fxp_stop((struct fxp_softc *) device_get_softc(dev)); 930f7788e8eSJonathan Lemon return (0); 931a17c678eSDavid Greenman } 932a17c678eSDavid Greenman 9337dced78aSDavid Greenman /* 9347dced78aSDavid Greenman * Device suspend routine. Stop the interface and save some PCI 9357dced78aSDavid Greenman * settings in case the BIOS doesn't restore them properly on 9367dced78aSDavid Greenman * resume. 9377dced78aSDavid Greenman */ 9387dced78aSDavid Greenman static int 9397dced78aSDavid Greenman fxp_suspend(device_t dev) 9407dced78aSDavid Greenman { 9417dced78aSDavid Greenman struct fxp_softc *sc = device_get_softc(dev); 942f7788e8eSJonathan Lemon int i, s; 9437dced78aSDavid Greenman 9444953bccaSNate Lawson FXP_LOCK(sc); 945f7788e8eSJonathan Lemon s = splimp(); 9467dced78aSDavid Greenman 9477dced78aSDavid Greenman fxp_stop(sc); 9487dced78aSDavid Greenman 9497dced78aSDavid Greenman for (i = 0; i < 5; i++) 950e27951b2SJohn Baldwin sc->saved_maps[i] = pci_read_config(dev, PCIR_BAR(i), 4); 9517dced78aSDavid Greenman sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4); 9527dced78aSDavid Greenman sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1); 9537dced78aSDavid Greenman sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1); 9547dced78aSDavid Greenman sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1); 9557dced78aSDavid Greenman 9567dced78aSDavid Greenman sc->suspended = 1; 9577dced78aSDavid Greenman 9584953bccaSNate Lawson FXP_UNLOCK(sc); 959f7788e8eSJonathan Lemon splx(s); 960f7788e8eSJonathan Lemon return (0); 9617dced78aSDavid Greenman } 9627dced78aSDavid Greenman 9637dced78aSDavid Greenman /* 9647dced78aSDavid Greenman * Device resume routine. Restore some PCI settings in case the BIOS 9657dced78aSDavid Greenman * doesn't, re-enable busmastering, and restart the interface if 9667dced78aSDavid Greenman * appropriate. 9677dced78aSDavid Greenman */ 9687dced78aSDavid Greenman static int 9697dced78aSDavid Greenman fxp_resume(device_t dev) 9707dced78aSDavid Greenman { 9717dced78aSDavid Greenman struct fxp_softc *sc = device_get_softc(dev); 9727dced78aSDavid Greenman struct ifnet *ifp = &sc->sc_if; 97374d1ed23SMaxime Henrion uint16_t pci_command; 974f7788e8eSJonathan Lemon int i, s; 9757dced78aSDavid Greenman 9764953bccaSNate Lawson FXP_LOCK(sc); 977f7788e8eSJonathan Lemon s = splimp(); 97879495006SWarner Losh 9797dced78aSDavid Greenman /* better way to do this? */ 9807dced78aSDavid Greenman for (i = 0; i < 5; i++) 981e27951b2SJohn Baldwin pci_write_config(dev, PCIR_BAR(i), sc->saved_maps[i], 4); 9827dced78aSDavid Greenman pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4); 9837dced78aSDavid Greenman pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1); 9847dced78aSDavid Greenman pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1); 9857dced78aSDavid Greenman pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1); 9867dced78aSDavid Greenman 9877dced78aSDavid Greenman /* reenable busmastering */ 9887dced78aSDavid Greenman pci_command = pci_read_config(dev, PCIR_COMMAND, 2); 9897dced78aSDavid Greenman pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 9907dced78aSDavid Greenman pci_write_config(dev, PCIR_COMMAND, pci_command, 2); 9917dced78aSDavid Greenman 9927dced78aSDavid Greenman CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET); 9937dced78aSDavid Greenman DELAY(10); 9947dced78aSDavid Greenman 9957dced78aSDavid Greenman /* reinitialize interface if necessary */ 9967dced78aSDavid Greenman if (ifp->if_flags & IFF_UP) 9974953bccaSNate Lawson fxp_init_body(sc); 9987dced78aSDavid Greenman 9997dced78aSDavid Greenman sc->suspended = 0; 10007dced78aSDavid Greenman 10014953bccaSNate Lawson FXP_UNLOCK(sc); 1002f7788e8eSJonathan Lemon splx(s); 1003ba8c6fd5SDavid Greenman return (0); 1004f7788e8eSJonathan Lemon } 1005ba8c6fd5SDavid Greenman 100600c4116bSJonathan Lemon static void 100700c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length) 100800c4116bSJonathan Lemon { 100974d1ed23SMaxime Henrion uint16_t reg; 101000c4116bSJonathan Lemon int x; 101100c4116bSJonathan Lemon 101200c4116bSJonathan Lemon /* 101300c4116bSJonathan Lemon * Shift in data. 101400c4116bSJonathan Lemon */ 101500c4116bSJonathan Lemon for (x = 1 << (length - 1); x; x >>= 1) { 101600c4116bSJonathan Lemon if (data & x) 101700c4116bSJonathan Lemon reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI; 101800c4116bSJonathan Lemon else 101900c4116bSJonathan Lemon reg = FXP_EEPROM_EECS; 102000c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 102100c4116bSJonathan Lemon DELAY(1); 102200c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 102300c4116bSJonathan Lemon DELAY(1); 102400c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 102500c4116bSJonathan Lemon DELAY(1); 102600c4116bSJonathan Lemon } 102700c4116bSJonathan Lemon } 102800c4116bSJonathan Lemon 1029f7788e8eSJonathan Lemon /* 1030f7788e8eSJonathan Lemon * Read from the serial EEPROM. Basically, you manually shift in 1031f7788e8eSJonathan Lemon * the read opcode (one bit at a time) and then shift in the address, 1032f7788e8eSJonathan Lemon * and then you shift out the data (all of this one bit at a time). 1033f7788e8eSJonathan Lemon * The word size is 16 bits, so you have to provide the address for 1034f7788e8eSJonathan Lemon * every 16 bits of data. 1035f7788e8eSJonathan Lemon */ 103674d1ed23SMaxime Henrion static uint16_t 1037f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize) 1038f7788e8eSJonathan Lemon { 103974d1ed23SMaxime Henrion uint16_t reg, data; 1040f7788e8eSJonathan Lemon int x; 1041ba8c6fd5SDavid Greenman 1042f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 1043f7788e8eSJonathan Lemon /* 1044f7788e8eSJonathan Lemon * Shift in read opcode. 1045f7788e8eSJonathan Lemon */ 104600c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3); 1047f7788e8eSJonathan Lemon /* 1048f7788e8eSJonathan Lemon * Shift in address. 1049f7788e8eSJonathan Lemon */ 1050f7788e8eSJonathan Lemon data = 0; 1051f7788e8eSJonathan Lemon for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) { 1052f7788e8eSJonathan Lemon if (offset & x) 1053f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI; 1054f7788e8eSJonathan Lemon else 1055f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS; 1056f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1057f7788e8eSJonathan Lemon DELAY(1); 1058f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 1059f7788e8eSJonathan Lemon DELAY(1); 1060f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1061f7788e8eSJonathan Lemon DELAY(1); 1062f7788e8eSJonathan Lemon reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO; 1063f7788e8eSJonathan Lemon data++; 1064f7788e8eSJonathan Lemon if (autosize && reg == 0) { 1065f7788e8eSJonathan Lemon sc->eeprom_size = data; 1066f7788e8eSJonathan Lemon break; 1067f7788e8eSJonathan Lemon } 1068f7788e8eSJonathan Lemon } 1069f7788e8eSJonathan Lemon /* 1070f7788e8eSJonathan Lemon * Shift out data. 1071f7788e8eSJonathan Lemon */ 1072f7788e8eSJonathan Lemon data = 0; 1073f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS; 1074f7788e8eSJonathan Lemon for (x = 1 << 15; x; x >>= 1) { 1075f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 1076f7788e8eSJonathan Lemon DELAY(1); 1077f7788e8eSJonathan Lemon if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) 1078f7788e8eSJonathan Lemon data |= x; 1079f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1080f7788e8eSJonathan Lemon DELAY(1); 1081f7788e8eSJonathan Lemon } 1082f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 1083f7788e8eSJonathan Lemon DELAY(1); 1084f7788e8eSJonathan Lemon 1085f7788e8eSJonathan Lemon return (data); 1086ba8c6fd5SDavid Greenman } 1087ba8c6fd5SDavid Greenman 108800c4116bSJonathan Lemon static void 108974d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data) 109000c4116bSJonathan Lemon { 109100c4116bSJonathan Lemon int i; 109200c4116bSJonathan Lemon 109300c4116bSJonathan Lemon /* 109400c4116bSJonathan Lemon * Erase/write enable. 109500c4116bSJonathan Lemon */ 109600c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 109700c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x4, 3); 109800c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size); 109900c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 110000c4116bSJonathan Lemon DELAY(1); 110100c4116bSJonathan Lemon /* 110200c4116bSJonathan Lemon * Shift in write opcode, address, data. 110300c4116bSJonathan Lemon */ 110400c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 110500c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3); 110600c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, offset, sc->eeprom_size); 110700c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, data, 16); 110800c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 110900c4116bSJonathan Lemon DELAY(1); 111000c4116bSJonathan Lemon /* 111100c4116bSJonathan Lemon * Wait for EEPROM to finish up. 111200c4116bSJonathan Lemon */ 111300c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 111400c4116bSJonathan Lemon DELAY(1); 111500c4116bSJonathan Lemon for (i = 0; i < 1000; i++) { 111600c4116bSJonathan Lemon if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) 111700c4116bSJonathan Lemon break; 111800c4116bSJonathan Lemon DELAY(50); 111900c4116bSJonathan Lemon } 112000c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 112100c4116bSJonathan Lemon DELAY(1); 112200c4116bSJonathan Lemon /* 112300c4116bSJonathan Lemon * Erase/write disable. 112400c4116bSJonathan Lemon */ 112500c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 112600c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x4, 3); 112700c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0, sc->eeprom_size); 112800c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 112900c4116bSJonathan Lemon DELAY(1); 113000c4116bSJonathan Lemon } 113100c4116bSJonathan Lemon 1132ba8c6fd5SDavid Greenman /* 1133e9bf2fa7SDavid Greenman * From NetBSD: 1134e9bf2fa7SDavid Greenman * 1135e9bf2fa7SDavid Greenman * Figure out EEPROM size. 1136e9bf2fa7SDavid Greenman * 1137e9bf2fa7SDavid Greenman * 559's can have either 64-word or 256-word EEPROMs, the 558 1138e9bf2fa7SDavid Greenman * datasheet only talks about 64-word EEPROMs, and the 557 datasheet 1139e9bf2fa7SDavid Greenman * talks about the existance of 16 to 256 word EEPROMs. 1140e9bf2fa7SDavid Greenman * 1141e9bf2fa7SDavid Greenman * The only known sizes are 64 and 256, where the 256 version is used 1142e9bf2fa7SDavid Greenman * by CardBus cards to store CIS information. 1143e9bf2fa7SDavid Greenman * 1144e9bf2fa7SDavid Greenman * The address is shifted in msb-to-lsb, and after the last 1145e9bf2fa7SDavid Greenman * address-bit the EEPROM is supposed to output a `dummy zero' bit, 1146e9bf2fa7SDavid Greenman * after which follows the actual data. We try to detect this zero, by 1147e9bf2fa7SDavid Greenman * probing the data-out bit in the EEPROM control register just after 1148e9bf2fa7SDavid Greenman * having shifted in a bit. If the bit is zero, we assume we've 1149e9bf2fa7SDavid Greenman * shifted enough address bits. The data-out should be tri-state, 1150e9bf2fa7SDavid Greenman * before this, which should translate to a logical one. 1151e9bf2fa7SDavid Greenman */ 1152e9bf2fa7SDavid Greenman static void 1153f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc) 1154e9bf2fa7SDavid Greenman { 1155e9bf2fa7SDavid Greenman 1156f7788e8eSJonathan Lemon /* guess maximum size of 256 words */ 1157f7788e8eSJonathan Lemon sc->eeprom_size = 8; 1158f7788e8eSJonathan Lemon 1159f7788e8eSJonathan Lemon /* autosize */ 1160f7788e8eSJonathan Lemon (void) fxp_eeprom_getword(sc, 0, 1); 1161e9bf2fa7SDavid Greenman } 1162f7788e8eSJonathan Lemon 1163ba8c6fd5SDavid Greenman static void 1164f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words) 1165ba8c6fd5SDavid Greenman { 1166f7788e8eSJonathan Lemon int i; 1167ba8c6fd5SDavid Greenman 1168f7788e8eSJonathan Lemon for (i = 0; i < words; i++) 1169f7788e8eSJonathan Lemon data[i] = fxp_eeprom_getword(sc, offset + i, 0); 1170ba8c6fd5SDavid Greenman } 1171ba8c6fd5SDavid Greenman 117200c4116bSJonathan Lemon static void 117300c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words) 117400c4116bSJonathan Lemon { 117500c4116bSJonathan Lemon int i; 117600c4116bSJonathan Lemon 117700c4116bSJonathan Lemon for (i = 0; i < words; i++) 117800c4116bSJonathan Lemon fxp_eeprom_putword(sc, offset + i, data[i]); 117900c4116bSJonathan Lemon } 118000c4116bSJonathan Lemon 1181b2badf02SMaxime Henrion static void 1182b2badf02SMaxime Henrion fxp_dma_map_txbuf(void *arg, bus_dma_segment_t *segs, int nseg, 1183b2badf02SMaxime Henrion bus_size_t mapsize, int error) 1184b2badf02SMaxime Henrion { 1185b2badf02SMaxime Henrion struct fxp_softc *sc; 1186b2badf02SMaxime Henrion struct fxp_cb_tx *txp; 1187b2badf02SMaxime Henrion int i; 1188b2badf02SMaxime Henrion 1189b2badf02SMaxime Henrion if (error) 1190b2badf02SMaxime Henrion return; 1191b2badf02SMaxime Henrion 1192b2badf02SMaxime Henrion KASSERT(nseg <= FXP_NTXSEG, ("too many DMA segments")); 1193b2badf02SMaxime Henrion 1194b2badf02SMaxime Henrion sc = arg; 1195b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_last->tx_next->tx_cb; 1196b2badf02SMaxime Henrion for (i = 0; i < nseg; i++) { 1197b2badf02SMaxime Henrion KASSERT(segs[i].ds_len <= MCLBYTES, ("segment size too large")); 1198b2badf02SMaxime Henrion /* 1199b2badf02SMaxime Henrion * If this is an 82550/82551, then we're using extended 1200b2badf02SMaxime Henrion * TxCBs _and_ we're using checksum offload. This means 1201b2badf02SMaxime Henrion * that the TxCB is really an IPCB. One major difference 1202b2badf02SMaxime Henrion * between the two is that with plain extended TxCBs, 1203b2badf02SMaxime Henrion * the bottom half of the TxCB contains two entries from 1204b2badf02SMaxime Henrion * the TBD array, whereas IPCBs contain just one entry: 1205b2badf02SMaxime Henrion * one entry (8 bytes) has been sacrificed for the TCP/IP 1206b2badf02SMaxime Henrion * checksum offload control bits. So to make things work 1207b2badf02SMaxime Henrion * right, we have to start filling in the TBD array 1208b2badf02SMaxime Henrion * starting from a different place depending on whether 1209b2badf02SMaxime Henrion * the chip is an 82550/82551 or not. 1210b2badf02SMaxime Henrion */ 1211b2badf02SMaxime Henrion if (sc->flags & FXP_FLAG_EXT_RFA) { 121283e6547dSMaxime Henrion txp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr); 121383e6547dSMaxime Henrion txp->tbd[i + 1].tb_size = htole32(segs[i].ds_len); 1214b2badf02SMaxime Henrion } else { 121583e6547dSMaxime Henrion txp->tbd[i].tb_addr = htole32(segs[i].ds_addr); 121683e6547dSMaxime Henrion txp->tbd[i].tb_size = htole32(segs[i].ds_len); 1217b2badf02SMaxime Henrion } 1218b2badf02SMaxime Henrion } 1219b2badf02SMaxime Henrion txp->tbd_number = nseg; 1220b2badf02SMaxime Henrion } 1221b2badf02SMaxime Henrion 1222a17c678eSDavid Greenman /* 12234953bccaSNate Lawson * Grab the softc lock and call the real fxp_start_body() routine 1224a17c678eSDavid Greenman */ 1225a17c678eSDavid Greenman static void 1226f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp) 1227a17c678eSDavid Greenman { 12289b44ff22SGarrett Wollman struct fxp_softc *sc = ifp->if_softc; 12294953bccaSNate Lawson 12304953bccaSNate Lawson FXP_LOCK(sc); 12314953bccaSNate Lawson fxp_start_body(ifp); 12324953bccaSNate Lawson FXP_UNLOCK(sc); 12334953bccaSNate Lawson } 12344953bccaSNate Lawson 12354953bccaSNate Lawson /* 12364953bccaSNate Lawson * Start packet transmission on the interface. 12374953bccaSNate Lawson * This routine must be called with the softc lock held, and is an 12384953bccaSNate Lawson * internal entry point only. 12394953bccaSNate Lawson */ 12404953bccaSNate Lawson static void 12414953bccaSNate Lawson fxp_start_body(struct ifnet *ifp) 12424953bccaSNate Lawson { 12434953bccaSNate Lawson struct fxp_softc *sc = ifp->if_softc; 124450d81222SMaxime Henrion struct fxp_tx *txp; 1245b2badf02SMaxime Henrion struct mbuf *mb_head; 1246b2badf02SMaxime Henrion int error; 1247a17c678eSDavid Greenman 124867fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 1249a17c678eSDavid Greenman /* 1250483b9871SDavid Greenman * See if we need to suspend xmit until the multicast filter 1251483b9871SDavid Greenman * has been reprogrammed (which can only be done at the head 1252483b9871SDavid Greenman * of the command chain). 1253a17c678eSDavid Greenman */ 12540f4dc94cSChuck Paterson if (sc->need_mcsetup) { 1255a17c678eSDavid Greenman return; 12560f4dc94cSChuck Paterson } 12571cd443acSDavid Greenman 1258483b9871SDavid Greenman txp = NULL; 1259483b9871SDavid Greenman 1260483b9871SDavid Greenman /* 1261483b9871SDavid Greenman * We're finished if there is nothing more to add to the list or if 1262483b9871SDavid Greenman * we're all filled up with buffers to transmit. 12633114fdb4SDavid Greenman * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add 12643114fdb4SDavid Greenman * a NOP command when needed. 1265483b9871SDavid Greenman */ 12667929aa03SMax Laier while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 12677929aa03SMax Laier sc->tx_queued < FXP_NTXCB - 1) { 1268483b9871SDavid Greenman 1269dfe61cf1SDavid Greenman /* 1270dfe61cf1SDavid Greenman * Grab a packet to transmit. 1271dfe61cf1SDavid Greenman */ 12727929aa03SMax Laier IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head); 12737929aa03SMax Laier if (mb_head == NULL) 12747929aa03SMax Laier break; 1275a17c678eSDavid Greenman 1276dfe61cf1SDavid Greenman /* 1277483b9871SDavid Greenman * Get pointer to next available tx desc. 1278dfe61cf1SDavid Greenman */ 1279b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_last->tx_next; 1280c8bca6dcSBill Paul 1281c8bca6dcSBill Paul /* 1282a35e7eaaSDon Lewis * A note in Appendix B of the Intel 8255x 10/100 Mbps 1283a35e7eaaSDon Lewis * Ethernet Controller Family Open Source Software 1284a35e7eaaSDon Lewis * Developer Manual says: 1285a35e7eaaSDon Lewis * Using software parsing is only allowed with legal 1286a35e7eaaSDon Lewis * TCP/IP or UDP/IP packets. 1287a35e7eaaSDon Lewis * ... 1288a35e7eaaSDon Lewis * For all other datagrams, hardware parsing must 1289a35e7eaaSDon Lewis * be used. 1290a35e7eaaSDon Lewis * Software parsing appears to truncate ICMP and 1291a35e7eaaSDon Lewis * fragmented UDP packets that contain one to three 1292a35e7eaaSDon Lewis * bytes in the second (and final) mbuf of the packet. 1293a35e7eaaSDon Lewis */ 1294a35e7eaaSDon Lewis if (sc->flags & FXP_FLAG_EXT_RFA) 1295a35e7eaaSDon Lewis txp->tx_cb->ipcb_ip_activation_high = 1296a35e7eaaSDon Lewis FXP_IPCB_HARDWAREPARSING_ENABLE; 1297a35e7eaaSDon Lewis 1298a35e7eaaSDon Lewis /* 1299c8bca6dcSBill Paul * Deal with TCP/IP checksum offload. Note that 1300c8bca6dcSBill Paul * in order for TCP checksum offload to work, 1301c8bca6dcSBill Paul * the pseudo header checksum must have already 1302c8bca6dcSBill Paul * been computed and stored in the checksum field 1303c8bca6dcSBill Paul * in the TCP header. The stack should have 1304c8bca6dcSBill Paul * already done this for us. 1305c8bca6dcSBill Paul */ 1306c8bca6dcSBill Paul 1307c8bca6dcSBill Paul if (mb_head->m_pkthdr.csum_flags) { 1308c8bca6dcSBill Paul if (mb_head->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1309b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_schedule = 1310c8bca6dcSBill Paul FXP_IPCB_TCPUDP_CHECKSUM_ENABLE; 1311c8bca6dcSBill Paul if (mb_head->m_pkthdr.csum_flags & CSUM_TCP) 1312b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_schedule |= 1313c8bca6dcSBill Paul FXP_IPCB_TCP_PACKET; 1314c8bca6dcSBill Paul } 1315c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR 1316c8bca6dcSBill Paul /* 1317c8bca6dcSBill Paul * XXX The 82550 chip appears to have trouble 1318c8bca6dcSBill Paul * dealing with IP header checksums in very small 1319c8bca6dcSBill Paul * datagrams, namely fragments from 1 to 3 bytes 1320c8bca6dcSBill Paul * in size. For example, say you want to transmit 1321c8bca6dcSBill Paul * a UDP packet of 1473 bytes. The packet will be 1322c8bca6dcSBill Paul * fragmented over two IP datagrams, the latter 1323c8bca6dcSBill Paul * containing only one byte of data. The 82550 will 1324c8bca6dcSBill Paul * botch the header checksum on the 1-byte fragment. 1325c8bca6dcSBill Paul * As long as the datagram contains 4 or more bytes 1326c8bca6dcSBill Paul * of data, you're ok. 1327c8bca6dcSBill Paul * 1328c8bca6dcSBill Paul * The following code attempts to work around this 1329c8bca6dcSBill Paul * problem: if the datagram is less than 38 bytes 1330c8bca6dcSBill Paul * in size (14 bytes ether header, 20 bytes IP header, 1331c8bca6dcSBill Paul * plus 4 bytes of data), we punt and compute the IP 1332c8bca6dcSBill Paul * header checksum by hand. This workaround doesn't 1333c8bca6dcSBill Paul * work very well, however, since it can be fooled 1334c8bca6dcSBill Paul * by things like VLAN tags and IP options that make 1335c8bca6dcSBill Paul * the header sizes/offsets vary. 1336c8bca6dcSBill Paul */ 1337c8bca6dcSBill Paul 1338c8bca6dcSBill Paul if (mb_head->m_pkthdr.csum_flags & CSUM_IP) { 1339c8bca6dcSBill Paul if (mb_head->m_pkthdr.len < 38) { 1340c8bca6dcSBill Paul struct ip *ip; 1341c8bca6dcSBill Paul mb_head->m_data += ETHER_HDR_LEN; 1342c8bca6dcSBill Paul ip = mtod(mb_head, struct ip *); 1343c8bca6dcSBill Paul ip->ip_sum = in_cksum(mb_head, 1344c8bca6dcSBill Paul ip->ip_hl << 2); 1345c8bca6dcSBill Paul mb_head->m_data -= ETHER_HDR_LEN; 1346c8bca6dcSBill Paul } else { 1347b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_activation_high = 1348c8bca6dcSBill Paul FXP_IPCB_HARDWAREPARSING_ENABLE; 1349b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_schedule |= 1350c8bca6dcSBill Paul FXP_IPCB_IP_CHECKSUM_ENABLE; 1351c8bca6dcSBill Paul } 1352c8bca6dcSBill Paul } 1353c8bca6dcSBill Paul #endif 1354c8bca6dcSBill Paul } 1355c8bca6dcSBill Paul 1356c8bca6dcSBill Paul /* 1357a17c678eSDavid Greenman * Go through each of the mbufs in the chain and initialize 1358483b9871SDavid Greenman * the transmit buffer descriptors with the physical address 1359a17c678eSDavid Greenman * and size of the mbuf. 1360a17c678eSDavid Greenman */ 1361b2badf02SMaxime Henrion error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map, 1362b2badf02SMaxime Henrion mb_head, fxp_dma_map_txbuf, sc, 0); 1363b2badf02SMaxime Henrion 1364b2badf02SMaxime Henrion if (error && error != EFBIG) { 1365b2badf02SMaxime Henrion device_printf(sc->dev, "can't map mbuf (error %d)\n", 1366b2badf02SMaxime Henrion error); 1367b2badf02SMaxime Henrion m_freem(mb_head); 1368a17c678eSDavid Greenman break; 1369a17c678eSDavid Greenman } 1370b2badf02SMaxime Henrion 1371b2badf02SMaxime Henrion if (error) { 137223a0ed7cSDavid Greenman struct mbuf *mn; 137323a0ed7cSDavid Greenman 1374a17c678eSDavid Greenman /* 13753bd07cfdSJonathan Lemon * We ran out of segments. We have to recopy this 13763bd07cfdSJonathan Lemon * mbuf chain first. Bail out if we can't get the 13773bd07cfdSJonathan Lemon * new buffers. 1378a17c678eSDavid Greenman */ 13791104779bSMike Silbersack mn = m_defrag(mb_head, M_DONTWAIT); 138023a0ed7cSDavid Greenman if (mn == NULL) { 138123a0ed7cSDavid Greenman m_freem(mb_head); 1382483b9871SDavid Greenman break; 13831104779bSMike Silbersack } else { 138423a0ed7cSDavid Greenman mb_head = mn; 13851104779bSMike Silbersack } 1386b2badf02SMaxime Henrion error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map, 1387b2badf02SMaxime Henrion mb_head, fxp_dma_map_txbuf, sc, 0); 1388b2badf02SMaxime Henrion if (error) { 1389b2badf02SMaxime Henrion device_printf(sc->dev, 1390b2badf02SMaxime Henrion "can't map mbuf (error %d)\n", error); 1391b2badf02SMaxime Henrion m_freem(mb_head); 1392b2badf02SMaxime Henrion break; 1393b2badf02SMaxime Henrion } 139423a0ed7cSDavid Greenman } 139523a0ed7cSDavid Greenman 1396b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, 1397b2badf02SMaxime Henrion BUS_DMASYNC_PREWRITE); 1398b2badf02SMaxime Henrion 1399b2badf02SMaxime Henrion txp->tx_mbuf = mb_head; 1400b2badf02SMaxime Henrion txp->tx_cb->cb_status = 0; 1401b2badf02SMaxime Henrion txp->tx_cb->byte_count = 0; 14023114fdb4SDavid Greenman if (sc->tx_queued != FXP_CXINT_THRESH - 1) { 1403b2badf02SMaxime Henrion txp->tx_cb->cb_command = 140483e6547dSMaxime Henrion htole16(sc->tx_cmd | FXP_CB_COMMAND_SF | 140583e6547dSMaxime Henrion FXP_CB_COMMAND_S); 14063114fdb4SDavid Greenman } else { 1407b2badf02SMaxime Henrion txp->tx_cb->cb_command = 140883e6547dSMaxime Henrion htole16(sc->tx_cmd | FXP_CB_COMMAND_SF | 140983e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 14103114fdb4SDavid Greenman /* 14113bd07cfdSJonathan Lemon * Set a 5 second timer just in case we don't hear 14123bd07cfdSJonathan Lemon * from the card again. 14133114fdb4SDavid Greenman */ 14143114fdb4SDavid Greenman ifp->if_timer = 5; 14153114fdb4SDavid Greenman } 1416b2badf02SMaxime Henrion txp->tx_cb->tx_threshold = tx_threshold; 1417a17c678eSDavid Greenman 1418a17c678eSDavid Greenman /* 1419483b9871SDavid Greenman * Advance the end of list forward. 1420a17c678eSDavid Greenman */ 142106175228SAndrew Gallatin 142250d81222SMaxime Henrion #ifdef __alpha__ 142306175228SAndrew Gallatin /* 142406175228SAndrew Gallatin * On platforms which can't access memory in 16-bit 142506175228SAndrew Gallatin * granularities, we must prevent the card from DMA'ing 142606175228SAndrew Gallatin * up the status while we update the command field. 142706175228SAndrew Gallatin * This could cause us to overwrite the completion status. 142814fd1071SMaxime Henrion * XXX This is probably bogus and we're _not_ looking 142914fd1071SMaxime Henrion * for atomicity here. 143006175228SAndrew Gallatin */ 143114fd1071SMaxime Henrion atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command, 1432bafb64afSMaxime Henrion htole16(FXP_CB_COMMAND_S)); 143350d81222SMaxime Henrion #else 1434bafb64afSMaxime Henrion sc->fxp_desc.tx_last->tx_cb->cb_command &= 1435bafb64afSMaxime Henrion htole16(~FXP_CB_COMMAND_S); 143650d81222SMaxime Henrion #endif /*__alpha__*/ 1437b2badf02SMaxime Henrion sc->fxp_desc.tx_last = txp; 1438a17c678eSDavid Greenman 1439a17c678eSDavid Greenman /* 14401cd443acSDavid Greenman * Advance the beginning of the list forward if there are 1441b2badf02SMaxime Henrion * no other packets queued (when nothing is queued, tx_first 1442483b9871SDavid Greenman * sits on the last TxCB that was sent out). 1443a17c678eSDavid Greenman */ 14441cd443acSDavid Greenman if (sc->tx_queued == 0) 1445b2badf02SMaxime Henrion sc->fxp_desc.tx_first = txp; 1446a17c678eSDavid Greenman 14471cd443acSDavid Greenman sc->tx_queued++; 14481cd443acSDavid Greenman 1449a17c678eSDavid Greenman /* 1450a17c678eSDavid Greenman * Pass packet to bpf if there is a listener. 1451a17c678eSDavid Greenman */ 1452673d9191SSam Leffler BPF_MTAP(ifp, mb_head); 1453483b9871SDavid Greenman } 1454b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 1455483b9871SDavid Greenman 1456483b9871SDavid Greenman /* 1457483b9871SDavid Greenman * We're finished. If we added to the list, issue a RESUME to get DMA 1458483b9871SDavid Greenman * going again if suspended. 1459483b9871SDavid Greenman */ 1460483b9871SDavid Greenman if (txp != NULL) { 1461483b9871SDavid Greenman fxp_scb_wait(sc); 14622e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME); 1463483b9871SDavid Greenman } 1464a17c678eSDavid Greenman } 1465a17c678eSDavid Greenman 1466e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 1467e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll; 1468e4fc250cSLuigi Rizzo 1469e4fc250cSLuigi Rizzo static void 1470e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1471e4fc250cSLuigi Rizzo { 1472e4fc250cSLuigi Rizzo struct fxp_softc *sc = ifp->if_softc; 147374d1ed23SMaxime Henrion uint8_t statack; 1474e4fc250cSLuigi Rizzo 14754953bccaSNate Lawson FXP_LOCK(sc); 1476fb917226SRuslan Ermilov if (!(ifp->if_capenable & IFCAP_POLLING)) { 1477fb917226SRuslan Ermilov ether_poll_deregister(ifp); 1478fb917226SRuslan Ermilov cmd = POLL_DEREGISTER; 1479fb917226SRuslan Ermilov } 1480e4fc250cSLuigi Rizzo if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ 1481e4fc250cSLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0); 14824953bccaSNate Lawson FXP_UNLOCK(sc); 1483e4fc250cSLuigi Rizzo return; 1484e4fc250cSLuigi Rizzo } 1485e4fc250cSLuigi Rizzo statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA | 1486e4fc250cSLuigi Rizzo FXP_SCB_STATACK_FR; 1487e4fc250cSLuigi Rizzo if (cmd == POLL_AND_CHECK_STATUS) { 148874d1ed23SMaxime Henrion uint8_t tmp; 14896481f301SPeter Wemm 1490e4fc250cSLuigi Rizzo tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK); 14914953bccaSNate Lawson if (tmp == 0xff || tmp == 0) { 14924953bccaSNate Lawson FXP_UNLOCK(sc); 1493e4fc250cSLuigi Rizzo return; /* nothing to do */ 14944953bccaSNate Lawson } 1495e4fc250cSLuigi Rizzo tmp &= ~statack; 1496e4fc250cSLuigi Rizzo /* ack what we can */ 1497e4fc250cSLuigi Rizzo if (tmp != 0) 1498e4fc250cSLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp); 1499e4fc250cSLuigi Rizzo statack |= tmp; 1500e4fc250cSLuigi Rizzo } 15014953bccaSNate Lawson fxp_intr_body(sc, ifp, statack, count); 15024953bccaSNate Lawson FXP_UNLOCK(sc); 1503e4fc250cSLuigi Rizzo } 1504e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 1505e4fc250cSLuigi Rizzo 1506a17c678eSDavid Greenman /* 15079c7d2607SDavid Greenman * Process interface interrupts. 1508a17c678eSDavid Greenman */ 150994927790SDavid Greenman static void 1510f7788e8eSJonathan Lemon fxp_intr(void *xsc) 1511a17c678eSDavid Greenman { 1512f7788e8eSJonathan Lemon struct fxp_softc *sc = xsc; 15134953bccaSNate Lawson struct ifnet *ifp = &sc->sc_if; 151474d1ed23SMaxime Henrion uint8_t statack; 15150f4dc94cSChuck Paterson 15164953bccaSNate Lawson FXP_LOCK(sc); 1517704d1965SWarner Losh if (sc->suspended) { 1518704d1965SWarner Losh FXP_UNLOCK(sc); 1519704d1965SWarner Losh return; 1520704d1965SWarner Losh } 1521704d1965SWarner Losh 1522e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 15234953bccaSNate Lawson if (ifp->if_flags & IFF_POLLING) { 15244953bccaSNate Lawson FXP_UNLOCK(sc); 1525e4fc250cSLuigi Rizzo return; 15264953bccaSNate Lawson } 1527fb917226SRuslan Ermilov if ((ifp->if_capenable & IFCAP_POLLING) && 1528fb917226SRuslan Ermilov ether_poll_register(fxp_poll, ifp)) { 1529e4fc250cSLuigi Rizzo /* disable interrupts */ 1530e4fc250cSLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 15314953bccaSNate Lawson FXP_UNLOCK(sc); 1532c660bdfaSJohn Baldwin fxp_poll(ifp, 0, 1); 1533e4fc250cSLuigi Rizzo return; 1534e4fc250cSLuigi Rizzo } 1535e4fc250cSLuigi Rizzo #endif 1536b184b38eSDavid Greenman while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) { 1537a17c678eSDavid Greenman /* 153811457bbfSJonathan Lemon * It should not be possible to have all bits set; the 153911457bbfSJonathan Lemon * FXP_SCB_INTR_SWI bit always returns 0 on a read. If 154011457bbfSJonathan Lemon * all bits are set, this may indicate that the card has 154111457bbfSJonathan Lemon * been physically ejected, so ignore it. 154211457bbfSJonathan Lemon */ 15434953bccaSNate Lawson if (statack == 0xff) { 15444953bccaSNate Lawson FXP_UNLOCK(sc); 154511457bbfSJonathan Lemon return; 15464953bccaSNate Lawson } 154711457bbfSJonathan Lemon 154811457bbfSJonathan Lemon /* 1549a17c678eSDavid Greenman * First ACK all the interrupts in this pass. 1550a17c678eSDavid Greenman */ 1551ba8c6fd5SDavid Greenman CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack); 15524953bccaSNate Lawson fxp_intr_body(sc, ifp, statack, -1); 1553e4fc250cSLuigi Rizzo } 15544953bccaSNate Lawson FXP_UNLOCK(sc); 1555e4fc250cSLuigi Rizzo } 1556e4fc250cSLuigi Rizzo 1557e4fc250cSLuigi Rizzo static void 1558b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc) 1559b2badf02SMaxime Henrion { 1560b2badf02SMaxime Henrion struct fxp_tx *txp; 1561b2badf02SMaxime Henrion 1562b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD); 1563b2badf02SMaxime Henrion for (txp = sc->fxp_desc.tx_first; sc->tx_queued && 156483e6547dSMaxime Henrion (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0; 1565b2badf02SMaxime Henrion txp = txp->tx_next) { 1566b2badf02SMaxime Henrion if (txp->tx_mbuf != NULL) { 1567b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, 1568b2badf02SMaxime Henrion BUS_DMASYNC_POSTWRITE); 1569b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, txp->tx_map); 1570b2badf02SMaxime Henrion m_freem(txp->tx_mbuf); 1571b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 1572b2badf02SMaxime Henrion /* clear this to reset csum offload bits */ 1573b2badf02SMaxime Henrion txp->tx_cb->tbd[0].tb_addr = 0; 1574b2badf02SMaxime Henrion } 1575b2badf02SMaxime Henrion sc->tx_queued--; 1576b2badf02SMaxime Henrion } 1577b2badf02SMaxime Henrion sc->fxp_desc.tx_first = txp; 1578b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 1579b2badf02SMaxime Henrion } 1580b2badf02SMaxime Henrion 1581b2badf02SMaxime Henrion static void 158274d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack, 15834953bccaSNate Lawson int count) 1584e4fc250cSLuigi Rizzo { 15852b5989e9SLuigi Rizzo struct mbuf *m; 1586b2badf02SMaxime Henrion struct fxp_rx *rxp; 15872b5989e9SLuigi Rizzo struct fxp_rfa *rfa; 15882b5989e9SLuigi Rizzo int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0; 15892b5989e9SLuigi Rizzo 159067fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 15912b5989e9SLuigi Rizzo if (rnr) 15920f1db1d6SMaxime Henrion sc->rnr++; 1593947e3815SIan Dowse #ifdef DEVICE_POLLING 1594947e3815SIan Dowse /* Pick up a deferred RNR condition if `count' ran out last time. */ 1595947e3815SIan Dowse if (sc->flags & FXP_FLAG_DEFERRED_RNR) { 1596947e3815SIan Dowse sc->flags &= ~FXP_FLAG_DEFERRED_RNR; 1597947e3815SIan Dowse rnr = 1; 1598947e3815SIan Dowse } 1599947e3815SIan Dowse #endif 1600a17c678eSDavid Greenman 1601a17c678eSDavid Greenman /* 16023114fdb4SDavid Greenman * Free any finished transmit mbuf chains. 160306936301SBill Paul * 160406936301SBill Paul * Handle the CNA event likt a CXTNO event. It used to 160506936301SBill Paul * be that this event (control unit not ready) was not 160606936301SBill Paul * encountered, but it is now with the SMPng modifications. 160706936301SBill Paul * The exact sequence of events that occur when the interface 160806936301SBill Paul * is brought up are different now, and if this event 160906936301SBill Paul * goes unhandled, the configuration/rxfilter setup sequence 161006936301SBill Paul * can stall for several seconds. The result is that no 161106936301SBill Paul * packets go out onto the wire for about 5 to 10 seconds 161206936301SBill Paul * after the interface is ifconfig'ed for the first time. 16133114fdb4SDavid Greenman */ 161406936301SBill Paul if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) { 1615b2badf02SMaxime Henrion fxp_txeof(sc); 16163114fdb4SDavid Greenman 161741aa0ba2SLuigi Rizzo ifp->if_timer = 0; 1618e2102ae4SMike Silbersack if (sc->tx_queued == 0) { 16193114fdb4SDavid Greenman if (sc->need_mcsetup) 16203114fdb4SDavid Greenman fxp_mc_setup(sc); 1621e2102ae4SMike Silbersack } 16223114fdb4SDavid Greenman /* 16233114fdb4SDavid Greenman * Try to start more packets transmitting. 16243114fdb4SDavid Greenman */ 16257929aa03SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 16264953bccaSNate Lawson fxp_start_body(ifp); 16273114fdb4SDavid Greenman } 16282b5989e9SLuigi Rizzo 16292b5989e9SLuigi Rizzo /* 16302b5989e9SLuigi Rizzo * Just return if nothing happened on the receive side. 16312b5989e9SLuigi Rizzo */ 1632947e3815SIan Dowse if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0) 16332b5989e9SLuigi Rizzo return; 16342b5989e9SLuigi Rizzo 16353114fdb4SDavid Greenman /* 1636a17c678eSDavid Greenman * Process receiver interrupts. If a no-resource (RNR) 1637a17c678eSDavid Greenman * condition exists, get whatever packets we can and 1638a17c678eSDavid Greenman * re-start the receiver. 1639947e3815SIan Dowse * 16402b5989e9SLuigi Rizzo * When using polling, we do not process the list to completion, 16412b5989e9SLuigi Rizzo * so when we get an RNR interrupt we must defer the restart 16422b5989e9SLuigi Rizzo * until we hit the last buffer with the C bit set. 16432b5989e9SLuigi Rizzo * If we run out of cycles and rfa_headm has the C bit set, 1644947e3815SIan Dowse * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so 1645947e3815SIan Dowse * that the info will be used in the subsequent polling cycle. 1646a17c678eSDavid Greenman */ 16472b5989e9SLuigi Rizzo for (;;) { 1648b2badf02SMaxime Henrion rxp = sc->fxp_desc.rx_head; 1649b2badf02SMaxime Henrion m = rxp->rx_mbuf; 1650ba8c6fd5SDavid Greenman rfa = (struct fxp_rfa *)(m->m_ext.ext_buf + 1651ba8c6fd5SDavid Greenman RFA_ALIGNMENT_FUDGE); 1652b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map, 1653b2badf02SMaxime Henrion BUS_DMASYNC_POSTREAD); 1654a17c678eSDavid Greenman 1655e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */ 1656947e3815SIan Dowse if (count >= 0 && count-- == 0) { 1657947e3815SIan Dowse if (rnr) { 1658947e3815SIan Dowse /* Defer RNR processing until the next time. */ 1659947e3815SIan Dowse sc->flags |= FXP_FLAG_DEFERRED_RNR; 1660947e3815SIan Dowse rnr = 0; 1661947e3815SIan Dowse } 16622b5989e9SLuigi Rizzo break; 1663947e3815SIan Dowse } 16642b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */ 16652b5989e9SLuigi Rizzo 166683e6547dSMaxime Henrion if ((le16toh(rfa->rfa_status) & FXP_RFA_STATUS_C) == 0) 16672b5989e9SLuigi Rizzo break; 16682b5989e9SLuigi Rizzo 1669dfe61cf1SDavid Greenman /* 1670b2badf02SMaxime Henrion * Advance head forward. 1671dfe61cf1SDavid Greenman */ 1672b2badf02SMaxime Henrion sc->fxp_desc.rx_head = rxp->rx_next; 1673a17c678eSDavid Greenman 1674dfe61cf1SDavid Greenman /* 1675ba8c6fd5SDavid Greenman * Add a new buffer to the receive chain. 1676ba8c6fd5SDavid Greenman * If this fails, the old buffer is recycled 1677ba8c6fd5SDavid Greenman * instead. 1678dfe61cf1SDavid Greenman */ 1679b2badf02SMaxime Henrion if (fxp_add_rfabuf(sc, rxp) == 0) { 1680aed53495SDavid Greenman int total_len; 1681a17c678eSDavid Greenman 1682e8c8b728SJonathan Lemon /* 16832b5989e9SLuigi Rizzo * Fetch packet length (the top 2 bits of 16842b5989e9SLuigi Rizzo * actual_size are flags set by the controller 16852b5989e9SLuigi Rizzo * upon completion), and drop the packet in case 16862b5989e9SLuigi Rizzo * of bogus length or CRC errors. 1687e8c8b728SJonathan Lemon */ 1688bafb64afSMaxime Henrion total_len = le16toh(rfa->actual_size) & 0x3fff; 16892b5989e9SLuigi Rizzo if (total_len < sizeof(struct ether_header) || 16902b5989e9SLuigi Rizzo total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE - 1691b2badf02SMaxime Henrion sc->rfa_size || 169283e6547dSMaxime Henrion le16toh(rfa->rfa_status) & FXP_RFA_STATUS_CRC) { 1693e8c8b728SJonathan Lemon m_freem(m); 16942b5989e9SLuigi Rizzo continue; 1695e8c8b728SJonathan Lemon } 1696920b58e8SBrooks Davis 1697c8bca6dcSBill Paul /* Do IP checksum checking. */ 169883e6547dSMaxime Henrion if (le16toh(rfa->rfa_status) & FXP_RFA_STATUS_PARSE) { 1699c8bca6dcSBill Paul if (rfa->rfax_csum_sts & 1700c8bca6dcSBill Paul FXP_RFDX_CS_IP_CSUM_BIT_VALID) 1701c8bca6dcSBill Paul m->m_pkthdr.csum_flags |= 1702c8bca6dcSBill Paul CSUM_IP_CHECKED; 1703c8bca6dcSBill Paul if (rfa->rfax_csum_sts & 1704c8bca6dcSBill Paul FXP_RFDX_CS_IP_CSUM_VALID) 1705c8bca6dcSBill Paul m->m_pkthdr.csum_flags |= 1706c8bca6dcSBill Paul CSUM_IP_VALID; 1707c8bca6dcSBill Paul if ((rfa->rfax_csum_sts & 1708c8bca6dcSBill Paul FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) && 1709c8bca6dcSBill Paul (rfa->rfax_csum_sts & 1710c8bca6dcSBill Paul FXP_RFDX_CS_TCPUDP_CSUM_VALID)) { 1711c8bca6dcSBill Paul m->m_pkthdr.csum_flags |= 1712c8bca6dcSBill Paul CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 1713c8bca6dcSBill Paul m->m_pkthdr.csum_data = 0xffff; 1714c8bca6dcSBill Paul } 1715c8bca6dcSBill Paul } 1716c8bca6dcSBill Paul 17172e2de7f2SArchie Cobbs m->m_pkthdr.len = m->m_len = total_len; 1718673d9191SSam Leffler m->m_pkthdr.rcvif = ifp; 1719673d9191SSam Leffler 172005fb8c3fSNate Lawson /* 172105fb8c3fSNate Lawson * Drop locks before calling if_input() since it 172205fb8c3fSNate Lawson * may re-enter fxp_start() in the netisr case. 172305fb8c3fSNate Lawson * This would result in a lock reversal. Better 172405fb8c3fSNate Lawson * performance might be obtained by chaining all 172505fb8c3fSNate Lawson * packets received, dropping the lock, and then 172605fb8c3fSNate Lawson * calling if_input() on each one. 172705fb8c3fSNate Lawson */ 172805fb8c3fSNate Lawson FXP_UNLOCK(sc); 1729673d9191SSam Leffler (*ifp->if_input)(ifp, m); 173005fb8c3fSNate Lawson FXP_LOCK(sc); 1731a17c678eSDavid Greenman } 1732a17c678eSDavid Greenman } 17332b5989e9SLuigi Rizzo if (rnr) { 1734ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 1735ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 1736b2badf02SMaxime Henrion sc->fxp_desc.rx_head->rx_addr); 17372e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START); 1738a17c678eSDavid Greenman } 1739a17c678eSDavid Greenman } 1740a17c678eSDavid Greenman 1741dfe61cf1SDavid Greenman /* 1742dfe61cf1SDavid Greenman * Update packet in/out/collision statistics. The i82557 doesn't 1743dfe61cf1SDavid Greenman * allow you to access these counters without doing a fairly 1744dfe61cf1SDavid Greenman * expensive DMA to get _all_ of the statistics it maintains, so 1745dfe61cf1SDavid Greenman * we do this operation here only once per second. The statistics 1746dfe61cf1SDavid Greenman * counters in the kernel are updated from the previous dump-stats 1747dfe61cf1SDavid Greenman * DMA and then a new dump-stats DMA is started. The on-chip 1748dfe61cf1SDavid Greenman * counters are zeroed when the DMA completes. If we can't start 1749dfe61cf1SDavid Greenman * the DMA immediately, we don't wait - we just prepare to read 1750dfe61cf1SDavid Greenman * them again next time. 1751dfe61cf1SDavid Greenman */ 1752303b270bSEivind Eklund static void 1753f7788e8eSJonathan Lemon fxp_tick(void *xsc) 1754a17c678eSDavid Greenman { 1755f7788e8eSJonathan Lemon struct fxp_softc *sc = xsc; 1756ba8c6fd5SDavid Greenman struct ifnet *ifp = &sc->sc_if; 1757a17c678eSDavid Greenman struct fxp_stats *sp = sc->fxp_stats; 1758f7788e8eSJonathan Lemon int s; 1759a17c678eSDavid Greenman 17604953bccaSNate Lawson FXP_LOCK(sc); 17614953bccaSNate Lawson s = splimp(); 1762b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD); 176383e6547dSMaxime Henrion ifp->if_opackets += le32toh(sp->tx_good); 176483e6547dSMaxime Henrion ifp->if_collisions += le32toh(sp->tx_total_collisions); 1765397f9dfeSDavid Greenman if (sp->rx_good) { 176683e6547dSMaxime Henrion ifp->if_ipackets += le32toh(sp->rx_good); 1767397f9dfeSDavid Greenman sc->rx_idle_secs = 0; 1768397f9dfeSDavid Greenman } else { 1769c8cc6fcaSDavid Greenman /* 1770c8cc6fcaSDavid Greenman * Receiver's been idle for another second. 1771c8cc6fcaSDavid Greenman */ 1772397f9dfeSDavid Greenman sc->rx_idle_secs++; 1773397f9dfeSDavid Greenman } 17743ba65732SDavid Greenman ifp->if_ierrors += 177583e6547dSMaxime Henrion le32toh(sp->rx_crc_errors) + 177683e6547dSMaxime Henrion le32toh(sp->rx_alignment_errors) + 177783e6547dSMaxime Henrion le32toh(sp->rx_rnr_errors) + 177883e6547dSMaxime Henrion le32toh(sp->rx_overrun_errors); 1779a17c678eSDavid Greenman /* 1780f9be9005SDavid Greenman * If any transmit underruns occured, bump up the transmit 1781f9be9005SDavid Greenman * threshold by another 512 bytes (64 * 8). 1782f9be9005SDavid Greenman */ 1783f9be9005SDavid Greenman if (sp->tx_underruns) { 178483e6547dSMaxime Henrion ifp->if_oerrors += le32toh(sp->tx_underruns); 1785f9be9005SDavid Greenman if (tx_threshold < 192) 1786f9be9005SDavid Greenman tx_threshold += 64; 1787f9be9005SDavid Greenman } 17884953bccaSNate Lawson 1789397f9dfeSDavid Greenman /* 1790c8cc6fcaSDavid Greenman * Release any xmit buffers that have completed DMA. This isn't 1791c8cc6fcaSDavid Greenman * strictly necessary to do here, but it's advantagous for mbufs 1792c8cc6fcaSDavid Greenman * with external storage to be released in a timely manner rather 1793c8cc6fcaSDavid Greenman * than being defered for a potentially long time. This limits 1794c8cc6fcaSDavid Greenman * the delay to a maximum of one second. 1795c8cc6fcaSDavid Greenman */ 1796b2badf02SMaxime Henrion fxp_txeof(sc); 1797b2badf02SMaxime Henrion 1798c8cc6fcaSDavid Greenman /* 1799397f9dfeSDavid Greenman * If we haven't received any packets in FXP_MAC_RX_IDLE seconds, 1800397f9dfeSDavid Greenman * then assume the receiver has locked up and attempt to clear 1801397f9dfeSDavid Greenman * the condition by reprogramming the multicast filter. This is 1802397f9dfeSDavid Greenman * a work-around for a bug in the 82557 where the receiver locks 1803397f9dfeSDavid Greenman * up if it gets certain types of garbage in the syncronization 1804397f9dfeSDavid Greenman * bits prior to the packet header. This bug is supposed to only 1805397f9dfeSDavid Greenman * occur in 10Mbps mode, but has been seen to occur in 100Mbps 1806397f9dfeSDavid Greenman * mode as well (perhaps due to a 10/100 speed transition). 1807397f9dfeSDavid Greenman */ 1808397f9dfeSDavid Greenman if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) { 1809397f9dfeSDavid Greenman sc->rx_idle_secs = 0; 1810397f9dfeSDavid Greenman fxp_mc_setup(sc); 1811397f9dfeSDavid Greenman } 1812f9be9005SDavid Greenman /* 18133ba65732SDavid Greenman * If there is no pending command, start another stats 18143ba65732SDavid Greenman * dump. Otherwise punt for now. 1815a17c678eSDavid Greenman */ 1816397f9dfeSDavid Greenman if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) { 1817a17c678eSDavid Greenman /* 1818397f9dfeSDavid Greenman * Start another stats dump. 1819a17c678eSDavid Greenman */ 1820b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, 1821b2badf02SMaxime Henrion BUS_DMASYNC_PREREAD); 18222e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET); 1823dfe61cf1SDavid Greenman } else { 1824dfe61cf1SDavid Greenman /* 1825dfe61cf1SDavid Greenman * A previous command is still waiting to be accepted. 1826dfe61cf1SDavid Greenman * Just zero our copy of the stats and wait for the 18273ba65732SDavid Greenman * next timer event to update them. 1828dfe61cf1SDavid Greenman */ 1829dfe61cf1SDavid Greenman sp->tx_good = 0; 1830f9be9005SDavid Greenman sp->tx_underruns = 0; 1831dfe61cf1SDavid Greenman sp->tx_total_collisions = 0; 18323ba65732SDavid Greenman 1833dfe61cf1SDavid Greenman sp->rx_good = 0; 18343ba65732SDavid Greenman sp->rx_crc_errors = 0; 18353ba65732SDavid Greenman sp->rx_alignment_errors = 0; 18363ba65732SDavid Greenman sp->rx_rnr_errors = 0; 18373ba65732SDavid Greenman sp->rx_overrun_errors = 0; 1838dfe61cf1SDavid Greenman } 1839f7788e8eSJonathan Lemon if (sc->miibus != NULL) 1840f7788e8eSJonathan Lemon mii_tick(device_get_softc(sc->miibus)); 18414953bccaSNate Lawson 1842a17c678eSDavid Greenman /* 1843a17c678eSDavid Greenman * Schedule another timeout one second from now. 1844a17c678eSDavid Greenman */ 184545276e4aSSam Leffler callout_reset(&sc->stat_ch, hz, fxp_tick, sc); 18464953bccaSNate Lawson FXP_UNLOCK(sc); 18474953bccaSNate Lawson splx(s); 1848a17c678eSDavid Greenman } 1849a17c678eSDavid Greenman 1850a17c678eSDavid Greenman /* 1851a17c678eSDavid Greenman * Stop the interface. Cancels the statistics updater and resets 1852a17c678eSDavid Greenman * the interface. 1853a17c678eSDavid Greenman */ 1854a17c678eSDavid Greenman static void 1855f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc) 1856a17c678eSDavid Greenman { 1857ba8c6fd5SDavid Greenman struct ifnet *ifp = &sc->sc_if; 1858b2badf02SMaxime Henrion struct fxp_tx *txp; 18593ba65732SDavid Greenman int i; 1860a17c678eSDavid Greenman 18617dced78aSDavid Greenman ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 18627dced78aSDavid Greenman ifp->if_timer = 0; 18637dced78aSDavid Greenman 1864e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 1865e4fc250cSLuigi Rizzo ether_poll_deregister(ifp); 1866e4fc250cSLuigi Rizzo #endif 1867a17c678eSDavid Greenman /* 1868a17c678eSDavid Greenman * Cancel stats updater. 1869a17c678eSDavid Greenman */ 187045276e4aSSam Leffler callout_stop(&sc->stat_ch); 18713ba65732SDavid Greenman 18723ba65732SDavid Greenman /* 187372a32a26SJonathan Lemon * Issue software reset, which also unloads the microcode. 18743ba65732SDavid Greenman */ 187572a32a26SJonathan Lemon sc->flags &= ~FXP_FLAG_UCODE; 187609882363SJonathan Lemon CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET); 187772a32a26SJonathan Lemon DELAY(50); 1878a17c678eSDavid Greenman 18793ba65732SDavid Greenman /* 18803ba65732SDavid Greenman * Release any xmit buffers. 18813ba65732SDavid Greenman */ 1882b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_list; 1883da91462dSDavid Greenman if (txp != NULL) { 1884da91462dSDavid Greenman for (i = 0; i < FXP_NTXCB; i++) { 1885b2badf02SMaxime Henrion if (txp[i].tx_mbuf != NULL) { 1886b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map, 1887b2badf02SMaxime Henrion BUS_DMASYNC_POSTWRITE); 1888b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map); 1889b2badf02SMaxime Henrion m_freem(txp[i].tx_mbuf); 1890b2badf02SMaxime Henrion txp[i].tx_mbuf = NULL; 1891c8bca6dcSBill Paul /* clear this to reset csum offload bits */ 1892b2badf02SMaxime Henrion txp[i].tx_cb->tbd[0].tb_addr = 0; 1893da91462dSDavid Greenman } 1894da91462dSDavid Greenman } 18953ba65732SDavid Greenman } 1896b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 18973ba65732SDavid Greenman sc->tx_queued = 0; 1898a17c678eSDavid Greenman } 1899a17c678eSDavid Greenman 1900a17c678eSDavid Greenman /* 1901a17c678eSDavid Greenman * Watchdog/transmission transmit timeout handler. Called when a 1902a17c678eSDavid Greenman * transmission is started on the interface, but no interrupt is 1903a17c678eSDavid Greenman * received before the timeout. This usually indicates that the 1904a17c678eSDavid Greenman * card has wedged for some reason. 1905a17c678eSDavid Greenman */ 1906a17c678eSDavid Greenman static void 1907f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp) 1908a17c678eSDavid Greenman { 1909ba8c6fd5SDavid Greenman struct fxp_softc *sc = ifp->if_softc; 1910ba8c6fd5SDavid Greenman 19114953bccaSNate Lawson FXP_LOCK(sc); 1912f7788e8eSJonathan Lemon device_printf(sc->dev, "device timeout\n"); 19134a5f1499SDavid Greenman ifp->if_oerrors++; 1914a17c678eSDavid Greenman 19154953bccaSNate Lawson fxp_init_body(sc); 19164953bccaSNate Lawson FXP_UNLOCK(sc); 1917a17c678eSDavid Greenman } 1918a17c678eSDavid Greenman 19194953bccaSNate Lawson /* 19204953bccaSNate Lawson * Acquire locks and then call the real initialization function. This 19214953bccaSNate Lawson * is necessary because ether_ioctl() calls if_init() and this would 19224953bccaSNate Lawson * result in mutex recursion if the mutex was held. 19234953bccaSNate Lawson */ 1924a17c678eSDavid Greenman static void 1925f7788e8eSJonathan Lemon fxp_init(void *xsc) 1926a17c678eSDavid Greenman { 1927fb583156SDavid Greenman struct fxp_softc *sc = xsc; 19284953bccaSNate Lawson 19294953bccaSNate Lawson FXP_LOCK(sc); 19304953bccaSNate Lawson fxp_init_body(sc); 19314953bccaSNate Lawson FXP_UNLOCK(sc); 19324953bccaSNate Lawson } 19334953bccaSNate Lawson 19344953bccaSNate Lawson /* 19354953bccaSNate Lawson * Perform device initialization. This routine must be called with the 19364953bccaSNate Lawson * softc lock held. 19374953bccaSNate Lawson */ 19384953bccaSNate Lawson static void 19394953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc) 19404953bccaSNate Lawson { 1941ba8c6fd5SDavid Greenman struct ifnet *ifp = &sc->sc_if; 1942a17c678eSDavid Greenman struct fxp_cb_config *cbp; 1943a17c678eSDavid Greenman struct fxp_cb_ias *cb_ias; 1944b2badf02SMaxime Henrion struct fxp_cb_tx *tcbp; 1945b2badf02SMaxime Henrion struct fxp_tx *txp; 194609882363SJonathan Lemon struct fxp_cb_mcs *mcsp; 1947f7788e8eSJonathan Lemon int i, prm, s; 1948a17c678eSDavid Greenman 194967fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 1950f7788e8eSJonathan Lemon s = splimp(); 1951a17c678eSDavid Greenman /* 19523ba65732SDavid Greenman * Cancel any pending I/O 1953a17c678eSDavid Greenman */ 19543ba65732SDavid Greenman fxp_stop(sc); 1955a17c678eSDavid Greenman 1956a17c678eSDavid Greenman prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0; 1957a17c678eSDavid Greenman 1958a17c678eSDavid Greenman /* 1959a17c678eSDavid Greenman * Initialize base of CBL and RFA memory. Loading with zero 1960a17c678eSDavid Greenman * sets it up for regular linear addressing. 1961a17c678eSDavid Greenman */ 1962ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0); 19632e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE); 1964a17c678eSDavid Greenman 1965ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 19662e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE); 1967a17c678eSDavid Greenman 1968a17c678eSDavid Greenman /* 1969a17c678eSDavid Greenman * Initialize base of dump-stats buffer. 1970a17c678eSDavid Greenman */ 1971ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 1972b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD); 1973b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr); 19742e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR); 1975a17c678eSDavid Greenman 1976a17c678eSDavid Greenman /* 197772a32a26SJonathan Lemon * Attempt to load microcode if requested. 197872a32a26SJonathan Lemon */ 197972a32a26SJonathan Lemon if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0) 198072a32a26SJonathan Lemon fxp_load_ucode(sc); 198172a32a26SJonathan Lemon 198272a32a26SJonathan Lemon /* 198309882363SJonathan Lemon * Initialize the multicast address list. 198409882363SJonathan Lemon */ 198509882363SJonathan Lemon if (fxp_mc_addrs(sc)) { 198609882363SJonathan Lemon mcsp = sc->mcsp; 198709882363SJonathan Lemon mcsp->cb_status = 0; 198883e6547dSMaxime Henrion mcsp->cb_command = 198983e6547dSMaxime Henrion htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL); 199083e6547dSMaxime Henrion mcsp->link_addr = 0xffffffff; 199109882363SJonathan Lemon /* 199209882363SJonathan Lemon * Start the multicast setup command. 199309882363SJonathan Lemon */ 199409882363SJonathan Lemon fxp_scb_wait(sc); 1995b2badf02SMaxime Henrion bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE); 1996b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr); 199709882363SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 199809882363SJonathan Lemon /* ...and wait for it to complete. */ 1999209b07bcSMaxime Henrion fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map); 2000b2badf02SMaxime Henrion bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, 2001b2badf02SMaxime Henrion BUS_DMASYNC_POSTWRITE); 200209882363SJonathan Lemon } 200309882363SJonathan Lemon 200409882363SJonathan Lemon /* 2005a17c678eSDavid Greenman * We temporarily use memory that contains the TxCB list to 2006a17c678eSDavid Greenman * construct the config CB. The TxCB list memory is rebuilt 2007a17c678eSDavid Greenman * later. 2008a17c678eSDavid Greenman */ 2009b2badf02SMaxime Henrion cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list; 2010a17c678eSDavid Greenman 2011a17c678eSDavid Greenman /* 2012a17c678eSDavid Greenman * This bcopy is kind of disgusting, but there are a bunch of must be 2013a17c678eSDavid Greenman * zero and must be one bits in this structure and this is the easiest 2014a17c678eSDavid Greenman * way to initialize them all to proper values. 2015a17c678eSDavid Greenman */ 2016b2badf02SMaxime Henrion bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template)); 2017a17c678eSDavid Greenman 2018a17c678eSDavid Greenman cbp->cb_status = 0; 201983e6547dSMaxime Henrion cbp->cb_command = htole16(FXP_CB_COMMAND_CONFIG | 202083e6547dSMaxime Henrion FXP_CB_COMMAND_EL); 202183e6547dSMaxime Henrion cbp->link_addr = 0xffffffff; /* (no) next command */ 20222c6c0947SMaxime Henrion cbp->byte_count = sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22; 2023001696daSDavid Greenman cbp->rx_fifo_limit = 8; /* rx fifo threshold (32 bytes) */ 2024001696daSDavid Greenman cbp->tx_fifo_limit = 0; /* tx fifo threshold (0 bytes) */ 2025a17c678eSDavid Greenman cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */ 2026f7788e8eSJonathan Lemon cbp->mwi_enable = sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0; 2027f7788e8eSJonathan Lemon cbp->type_enable = 0; /* actually reserved */ 2028f7788e8eSJonathan Lemon cbp->read_align_en = sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0; 2029f7788e8eSJonathan Lemon cbp->end_wr_on_cl = sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0; 2030001696daSDavid Greenman cbp->rx_dma_bytecount = 0; /* (no) rx DMA max */ 2031001696daSDavid Greenman cbp->tx_dma_bytecount = 0; /* (no) tx DMA max */ 2032f7788e8eSJonathan Lemon cbp->dma_mbce = 0; /* (disable) dma max counters */ 2033a17c678eSDavid Greenman cbp->late_scb = 0; /* (don't) defer SCB update */ 2034f7788e8eSJonathan Lemon cbp->direct_dma_dis = 1; /* disable direct rcv dma mode */ 2035f7788e8eSJonathan Lemon cbp->tno_int_or_tco_en =0; /* (disable) tx not okay interrupt */ 20363114fdb4SDavid Greenman cbp->ci_int = 1; /* interrupt on CU idle */ 2037f7788e8eSJonathan Lemon cbp->ext_txcb_dis = sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1; 2038f7788e8eSJonathan Lemon cbp->ext_stats_dis = 1; /* disable extended counters */ 2039f7788e8eSJonathan Lemon cbp->keep_overrun_rx = 0; /* don't pass overrun frames to host */ 20408ef1f631SYaroslav Tykhiy cbp->save_bf = sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm; 2041a17c678eSDavid Greenman cbp->disc_short_rx = !prm; /* discard short packets */ 2042f7788e8eSJonathan Lemon cbp->underrun_retry = 1; /* retry mode (once) on DMA underrun */ 2043f7788e8eSJonathan Lemon cbp->two_frames = 0; /* do not limit FIFO to 2 frames */ 2044f7788e8eSJonathan Lemon cbp->dyn_tbd = 0; /* (no) dynamic TBD mode */ 2045c8bca6dcSBill Paul cbp->ext_rfa = sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0; 2046f7788e8eSJonathan Lemon cbp->mediatype = sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1; 2047f7788e8eSJonathan Lemon cbp->csma_dis = 0; /* (don't) disable link */ 2048f7788e8eSJonathan Lemon cbp->tcp_udp_cksum = 0; /* (don't) enable checksum */ 2049f7788e8eSJonathan Lemon cbp->vlan_tco = 0; /* (don't) enable vlan wakeup */ 2050f7788e8eSJonathan Lemon cbp->link_wake_en = 0; /* (don't) assert PME# on link change */ 2051f7788e8eSJonathan Lemon cbp->arp_wake_en = 0; /* (don't) assert PME# on arp */ 2052f7788e8eSJonathan Lemon cbp->mc_wake_en = 0; /* (don't) enable PME# on mcmatch */ 2053a17c678eSDavid Greenman cbp->nsai = 1; /* (don't) disable source addr insert */ 2054a17c678eSDavid Greenman cbp->preamble_length = 2; /* (7 byte) preamble */ 2055a17c678eSDavid Greenman cbp->loopback = 0; /* (don't) loopback */ 2056a17c678eSDavid Greenman cbp->linear_priority = 0; /* (normal CSMA/CD operation) */ 2057a17c678eSDavid Greenman cbp->linear_pri_mode = 0; /* (wait after xmit only) */ 2058a17c678eSDavid Greenman cbp->interfrm_spacing = 6; /* (96 bits of) interframe spacing */ 2059a17c678eSDavid Greenman cbp->promiscuous = prm; /* promiscuous mode */ 2060a17c678eSDavid Greenman cbp->bcast_disable = 0; /* (don't) disable broadcasts */ 2061f7788e8eSJonathan Lemon cbp->wait_after_win = 0; /* (don't) enable modified backoff alg*/ 2062f7788e8eSJonathan Lemon cbp->ignore_ul = 0; /* consider U/L bit in IA matching */ 2063f7788e8eSJonathan Lemon cbp->crc16_en = 0; /* (don't) enable crc-16 algorithm */ 2064f7788e8eSJonathan Lemon cbp->crscdt = sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0; 2065f7788e8eSJonathan Lemon 2066a17c678eSDavid Greenman cbp->stripping = !prm; /* truncate rx packet to byte count */ 2067a17c678eSDavid Greenman cbp->padding = 1; /* (do) pad short tx packets */ 2068a17c678eSDavid Greenman cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */ 2069f7788e8eSJonathan Lemon cbp->long_rx_en = sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0; 2070f7788e8eSJonathan Lemon cbp->ia_wake_en = 0; /* (don't) wake up on address match */ 2071f7788e8eSJonathan Lemon cbp->magic_pkt_dis = 0; /* (don't) disable magic packet */ 2072f7788e8eSJonathan Lemon /* must set wake_en in PMCSR also */ 2073a17c678eSDavid Greenman cbp->force_fdx = 0; /* (don't) force full duplex */ 20743ba65732SDavid Greenman cbp->fdx_pin_en = 1; /* (enable) FDX# pin */ 2075a17c678eSDavid Greenman cbp->multi_ia = 0; /* (don't) accept multiple IAs */ 2076f7788e8eSJonathan Lemon cbp->mc_all = sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0; 2077c8bca6dcSBill Paul cbp->gamla_rx = sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0; 2078a17c678eSDavid Greenman 20790f1db1d6SMaxime Henrion if (sc->tunable_noflow || sc->revision == FXP_REV_82557) { 20803bd07cfdSJonathan Lemon /* 20813bd07cfdSJonathan Lemon * The 82557 has no hardware flow control, the values 20823bd07cfdSJonathan Lemon * below are the defaults for the chip. 20833bd07cfdSJonathan Lemon */ 20843bd07cfdSJonathan Lemon cbp->fc_delay_lsb = 0; 20853bd07cfdSJonathan Lemon cbp->fc_delay_msb = 0x40; 20863bd07cfdSJonathan Lemon cbp->pri_fc_thresh = 3; 20873bd07cfdSJonathan Lemon cbp->tx_fc_dis = 0; 20883bd07cfdSJonathan Lemon cbp->rx_fc_restop = 0; 20893bd07cfdSJonathan Lemon cbp->rx_fc_restart = 0; 20903bd07cfdSJonathan Lemon cbp->fc_filter = 0; 20913bd07cfdSJonathan Lemon cbp->pri_fc_loc = 1; 20923bd07cfdSJonathan Lemon } else { 20933bd07cfdSJonathan Lemon cbp->fc_delay_lsb = 0x1f; 20943bd07cfdSJonathan Lemon cbp->fc_delay_msb = 0x01; 20953bd07cfdSJonathan Lemon cbp->pri_fc_thresh = 3; 20963bd07cfdSJonathan Lemon cbp->tx_fc_dis = 0; /* enable transmit FC */ 20973bd07cfdSJonathan Lemon cbp->rx_fc_restop = 1; /* enable FC restop frames */ 20983bd07cfdSJonathan Lemon cbp->rx_fc_restart = 1; /* enable FC restart frames */ 20993bd07cfdSJonathan Lemon cbp->fc_filter = !prm; /* drop FC frames to host */ 21003bd07cfdSJonathan Lemon cbp->pri_fc_loc = 1; /* FC pri location (byte31) */ 21013bd07cfdSJonathan Lemon } 21023bd07cfdSJonathan Lemon 2103a17c678eSDavid Greenman /* 2104a17c678eSDavid Greenman * Start the config command/DMA. 2105a17c678eSDavid Greenman */ 2106ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 2107b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2108b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr); 21092e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2110a17c678eSDavid Greenman /* ...and wait for it to complete. */ 2111209b07bcSMaxime Henrion fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map); 2112b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE); 2113a17c678eSDavid Greenman 2114a17c678eSDavid Greenman /* 2115a17c678eSDavid Greenman * Now initialize the station address. Temporarily use the TxCB 2116a17c678eSDavid Greenman * memory area like we did above for the config CB. 2117a17c678eSDavid Greenman */ 2118b2badf02SMaxime Henrion cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list; 2119a17c678eSDavid Greenman cb_ias->cb_status = 0; 212083e6547dSMaxime Henrion cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL); 212183e6547dSMaxime Henrion cb_ias->link_addr = 0xffffffff; 2122e609b4d7SMaxime Henrion bcopy(sc->arpcom.ac_enaddr, cb_ias->macaddr, 2123a17c678eSDavid Greenman sizeof(sc->arpcom.ac_enaddr)); 2124a17c678eSDavid Greenman 2125a17c678eSDavid Greenman /* 2126a17c678eSDavid Greenman * Start the IAS (Individual Address Setup) command/DMA. 2127a17c678eSDavid Greenman */ 2128ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 2129b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 21302e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2131a17c678eSDavid Greenman /* ...and wait for it to complete. */ 2132209b07bcSMaxime Henrion fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map); 2133b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE); 2134a17c678eSDavid Greenman 2135a17c678eSDavid Greenman /* 2136a17c678eSDavid Greenman * Initialize transmit control block (TxCB) list. 2137a17c678eSDavid Greenman */ 2138b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_list; 2139b2badf02SMaxime Henrion tcbp = sc->fxp_desc.cbl_list; 2140b2badf02SMaxime Henrion bzero(tcbp, FXP_TXCB_SZ); 2141a17c678eSDavid Greenman for (i = 0; i < FXP_NTXCB; i++) { 2142b2badf02SMaxime Henrion txp[i].tx_mbuf = NULL; 214383e6547dSMaxime Henrion tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK); 214483e6547dSMaxime Henrion tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP); 214583e6547dSMaxime Henrion tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr + 214683e6547dSMaxime Henrion (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx))); 21473bd07cfdSJonathan Lemon if (sc->flags & FXP_FLAG_EXT_TXCB) 2148b2badf02SMaxime Henrion tcbp[i].tbd_array_addr = 214983e6547dSMaxime Henrion htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2])); 21503bd07cfdSJonathan Lemon else 2151b2badf02SMaxime Henrion tcbp[i].tbd_array_addr = 215283e6547dSMaxime Henrion htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0])); 2153b2badf02SMaxime Henrion txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK]; 2154a17c678eSDavid Greenman } 2155a17c678eSDavid Greenman /* 2156397f9dfeSDavid Greenman * Set the suspend flag on the first TxCB and start the control 2157a17c678eSDavid Greenman * unit. It will execute the NOP and then suspend. 2158a17c678eSDavid Greenman */ 215983e6547dSMaxime Henrion tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S); 2160b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2161b2badf02SMaxime Henrion sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp; 2162397f9dfeSDavid Greenman sc->tx_queued = 1; 2163a17c678eSDavid Greenman 2164ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 21652e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2166a17c678eSDavid Greenman 2167a17c678eSDavid Greenman /* 2168a17c678eSDavid Greenman * Initialize receiver buffer area - RFA. 2169a17c678eSDavid Greenman */ 2170ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 2171b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr); 21722e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START); 2173a17c678eSDavid Greenman 2174dccee1a1SDavid Greenman /* 2175ba8c6fd5SDavid Greenman * Set current media. 2176dccee1a1SDavid Greenman */ 2177f7788e8eSJonathan Lemon if (sc->miibus != NULL) 2178f7788e8eSJonathan Lemon mii_mediachg(device_get_softc(sc->miibus)); 2179dccee1a1SDavid Greenman 2180a17c678eSDavid Greenman ifp->if_flags |= IFF_RUNNING; 2181a17c678eSDavid Greenman ifp->if_flags &= ~IFF_OACTIVE; 2182e8c8b728SJonathan Lemon 2183e8c8b728SJonathan Lemon /* 2184e8c8b728SJonathan Lemon * Enable interrupts. 2185e8c8b728SJonathan Lemon */ 21862b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING 21872b5989e9SLuigi Rizzo /* 21882b5989e9SLuigi Rizzo * ... but only do that if we are not polling. And because (presumably) 21892b5989e9SLuigi Rizzo * the default is interrupts on, we need to disable them explicitly! 21902b5989e9SLuigi Rizzo */ 219162f76486SMaxim Sobolev if ( ifp->if_flags & IFF_POLLING ) 21922b5989e9SLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 21932b5989e9SLuigi Rizzo else 21942b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */ 2195e8c8b728SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0); 2196a17c678eSDavid Greenman 2197a17c678eSDavid Greenman /* 2198a17c678eSDavid Greenman * Start stats updater. 2199a17c678eSDavid Greenman */ 220045276e4aSSam Leffler callout_reset(&sc->stat_ch, hz, fxp_tick, sc); 22014953bccaSNate Lawson splx(s); 2202f7788e8eSJonathan Lemon } 2203f7788e8eSJonathan Lemon 2204f7788e8eSJonathan Lemon static int 2205f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp) 2206f7788e8eSJonathan Lemon { 2207f7788e8eSJonathan Lemon 2208f7788e8eSJonathan Lemon return (0); 2209a17c678eSDavid Greenman } 2210a17c678eSDavid Greenman 2211303b270bSEivind Eklund static void 2212f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2213ba8c6fd5SDavid Greenman { 2214ba8c6fd5SDavid Greenman 2215f7788e8eSJonathan Lemon ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2216ba8c6fd5SDavid Greenman } 2217ba8c6fd5SDavid Greenman 2218ba8c6fd5SDavid Greenman /* 2219ba8c6fd5SDavid Greenman * Change media according to request. 2220ba8c6fd5SDavid Greenman */ 2221f7788e8eSJonathan Lemon static int 2222f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp) 2223ba8c6fd5SDavid Greenman { 2224ba8c6fd5SDavid Greenman struct fxp_softc *sc = ifp->if_softc; 2225f7788e8eSJonathan Lemon struct mii_data *mii; 2226ba8c6fd5SDavid Greenman 2227f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 2228f7788e8eSJonathan Lemon mii_mediachg(mii); 2229ba8c6fd5SDavid Greenman return (0); 2230ba8c6fd5SDavid Greenman } 2231ba8c6fd5SDavid Greenman 2232ba8c6fd5SDavid Greenman /* 2233ba8c6fd5SDavid Greenman * Notify the world which media we're using. 2234ba8c6fd5SDavid Greenman */ 2235f7788e8eSJonathan Lemon static void 2236f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2237ba8c6fd5SDavid Greenman { 2238ba8c6fd5SDavid Greenman struct fxp_softc *sc = ifp->if_softc; 2239f7788e8eSJonathan Lemon struct mii_data *mii; 2240ba8c6fd5SDavid Greenman 2241f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 2242f7788e8eSJonathan Lemon mii_pollstat(mii); 2243f7788e8eSJonathan Lemon ifmr->ifm_active = mii->mii_media_active; 2244f7788e8eSJonathan Lemon ifmr->ifm_status = mii->mii_media_status; 22452e2b8238SJonathan Lemon 22462e2b8238SJonathan Lemon if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG) 22472e2b8238SJonathan Lemon sc->cu_resume_bug = 1; 22482e2b8238SJonathan Lemon else 22492e2b8238SJonathan Lemon sc->cu_resume_bug = 0; 2250ba8c6fd5SDavid Greenman } 2251ba8c6fd5SDavid Greenman 2252a17c678eSDavid Greenman /* 2253a17c678eSDavid Greenman * Add a buffer to the end of the RFA buffer list. 2254a17c678eSDavid Greenman * Return 0 if successful, 1 for failure. A failure results in 2255a17c678eSDavid Greenman * adding the 'oldm' (if non-NULL) on to the end of the list - 2256dc733423SDag-Erling Smørgrav * tossing out its old contents and recycling it. 2257a17c678eSDavid Greenman * The RFA struct is stuck at the beginning of mbuf cluster and the 2258a17c678eSDavid Greenman * data pointer is fixed up to point just past it. 2259a17c678eSDavid Greenman */ 2260a17c678eSDavid Greenman static int 2261b2badf02SMaxime Henrion fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp) 2262a17c678eSDavid Greenman { 2263a17c678eSDavid Greenman struct mbuf *m; 2264a17c678eSDavid Greenman struct fxp_rfa *rfa, *p_rfa; 2265b2badf02SMaxime Henrion struct fxp_rx *p_rx; 2266b2badf02SMaxime Henrion bus_dmamap_t tmp_map; 2267b2badf02SMaxime Henrion int error; 2268a17c678eSDavid Greenman 2269a163d034SWarner Losh m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2270b2badf02SMaxime Henrion if (m == NULL) 2271b2badf02SMaxime Henrion return (ENOBUFS); 2272ba8c6fd5SDavid Greenman 2273ba8c6fd5SDavid Greenman /* 2274ba8c6fd5SDavid Greenman * Move the data pointer up so that the incoming data packet 2275ba8c6fd5SDavid Greenman * will be 32-bit aligned. 2276ba8c6fd5SDavid Greenman */ 2277ba8c6fd5SDavid Greenman m->m_data += RFA_ALIGNMENT_FUDGE; 2278ba8c6fd5SDavid Greenman 2279eadd5e3aSDavid Greenman /* 2280eadd5e3aSDavid Greenman * Get a pointer to the base of the mbuf cluster and move 2281eadd5e3aSDavid Greenman * data start past it. 2282eadd5e3aSDavid Greenman */ 2283a17c678eSDavid Greenman rfa = mtod(m, struct fxp_rfa *); 2284c8bca6dcSBill Paul m->m_data += sc->rfa_size; 228583e6547dSMaxime Henrion rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE); 2286eadd5e3aSDavid Greenman 2287a17c678eSDavid Greenman rfa->rfa_status = 0; 228883e6547dSMaxime Henrion rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL); 2289a17c678eSDavid Greenman rfa->actual_size = 0; 2290ba8c6fd5SDavid Greenman 229128935f27SMaxime Henrion /* 229228935f27SMaxime Henrion * Initialize the rest of the RFA. Note that since the RFA 229328935f27SMaxime Henrion * is misaligned, we cannot store values directly. We're thus 229428935f27SMaxime Henrion * using the le32enc() function which handles endianness and 229528935f27SMaxime Henrion * is also alignment-safe. 229628935f27SMaxime Henrion */ 229783e6547dSMaxime Henrion le32enc(&rfa->link_addr, 0xffffffff); 229883e6547dSMaxime Henrion le32enc(&rfa->rbd_addr, 0xffffffff); 2299ba8c6fd5SDavid Greenman 2300b2badf02SMaxime Henrion /* Map the RFA into DMA memory. */ 2301b2badf02SMaxime Henrion error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa, 2302b2badf02SMaxime Henrion MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr, 2303b2badf02SMaxime Henrion &rxp->rx_addr, 0); 2304b2badf02SMaxime Henrion if (error) { 2305b2badf02SMaxime Henrion m_freem(m); 2306b2badf02SMaxime Henrion return (error); 2307b2badf02SMaxime Henrion } 2308b2badf02SMaxime Henrion 2309b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map); 2310b2badf02SMaxime Henrion tmp_map = sc->spare_map; 2311b2badf02SMaxime Henrion sc->spare_map = rxp->rx_map; 2312b2badf02SMaxime Henrion rxp->rx_map = tmp_map; 2313b2badf02SMaxime Henrion rxp->rx_mbuf = m; 2314b2badf02SMaxime Henrion 2315b983c7b3SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map, 2316b983c7b3SMaxime Henrion BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2317b2badf02SMaxime Henrion 2318dfe61cf1SDavid Greenman /* 2319dfe61cf1SDavid Greenman * If there are other buffers already on the list, attach this 2320dfe61cf1SDavid Greenman * one to the end by fixing up the tail to point to this one. 2321dfe61cf1SDavid Greenman */ 2322b2badf02SMaxime Henrion if (sc->fxp_desc.rx_head != NULL) { 2323b2badf02SMaxime Henrion p_rx = sc->fxp_desc.rx_tail; 2324b2badf02SMaxime Henrion p_rfa = (struct fxp_rfa *) 2325b2badf02SMaxime Henrion (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE); 2326b2badf02SMaxime Henrion p_rx->rx_next = rxp; 232783e6547dSMaxime Henrion le32enc(&p_rfa->link_addr, rxp->rx_addr); 2328aed53495SDavid Greenman p_rfa->rfa_control = 0; 2329b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map, 23304cec1653SMaxime Henrion BUS_DMASYNC_PREWRITE); 2331a17c678eSDavid Greenman } else { 2332b2badf02SMaxime Henrion rxp->rx_next = NULL; 2333b2badf02SMaxime Henrion sc->fxp_desc.rx_head = rxp; 2334a17c678eSDavid Greenman } 2335b2badf02SMaxime Henrion sc->fxp_desc.rx_tail = rxp; 2336b2badf02SMaxime Henrion return (0); 2337a17c678eSDavid Greenman } 2338a17c678eSDavid Greenman 23396ebc3153SDavid Greenman static volatile int 2340f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg) 2341dccee1a1SDavid Greenman { 2342f7788e8eSJonathan Lemon struct fxp_softc *sc = device_get_softc(dev); 2343dccee1a1SDavid Greenman int count = 10000; 23446ebc3153SDavid Greenman int value; 2345dccee1a1SDavid Greenman 2346ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_MDICONTROL, 2347ba8c6fd5SDavid Greenman (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21)); 2348dccee1a1SDavid Greenman 2349ba8c6fd5SDavid Greenman while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0 2350ba8c6fd5SDavid Greenman && count--) 23516ebc3153SDavid Greenman DELAY(10); 2352dccee1a1SDavid Greenman 2353dccee1a1SDavid Greenman if (count <= 0) 2354f7788e8eSJonathan Lemon device_printf(dev, "fxp_miibus_readreg: timed out\n"); 2355dccee1a1SDavid Greenman 23566ebc3153SDavid Greenman return (value & 0xffff); 2357dccee1a1SDavid Greenman } 2358dccee1a1SDavid Greenman 2359dccee1a1SDavid Greenman static void 2360f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value) 2361dccee1a1SDavid Greenman { 2362f7788e8eSJonathan Lemon struct fxp_softc *sc = device_get_softc(dev); 2363dccee1a1SDavid Greenman int count = 10000; 2364dccee1a1SDavid Greenman 2365ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_MDICONTROL, 2366ba8c6fd5SDavid Greenman (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) | 2367ba8c6fd5SDavid Greenman (value & 0xffff)); 2368dccee1a1SDavid Greenman 2369ba8c6fd5SDavid Greenman while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 && 2370ba8c6fd5SDavid Greenman count--) 23716ebc3153SDavid Greenman DELAY(10); 2372dccee1a1SDavid Greenman 2373dccee1a1SDavid Greenman if (count <= 0) 2374f7788e8eSJonathan Lemon device_printf(dev, "fxp_miibus_writereg: timed out\n"); 2375dccee1a1SDavid Greenman } 2376dccee1a1SDavid Greenman 2377dccee1a1SDavid Greenman static int 2378f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 2379a17c678eSDavid Greenman { 23809b44ff22SGarrett Wollman struct fxp_softc *sc = ifp->if_softc; 2381a17c678eSDavid Greenman struct ifreq *ifr = (struct ifreq *)data; 2382f7788e8eSJonathan Lemon struct mii_data *mii; 23838ef1f631SYaroslav Tykhiy int flag, mask, s, error = 0; 2384a17c678eSDavid Greenman 2385704d1965SWarner Losh /* 2386704d1965SWarner Losh * Detaching causes us to call ioctl with the mutex owned. Preclude 2387704d1965SWarner Losh * that by saying we're busy if the lock is already held. 2388704d1965SWarner Losh */ 238967fc050fSMaxime Henrion if (FXP_LOCKED(sc)) 2390704d1965SWarner Losh return (EBUSY); 239132cd7a9cSWarner Losh 23924953bccaSNate Lawson FXP_LOCK(sc); 2393f7788e8eSJonathan Lemon s = splimp(); 2394a17c678eSDavid Greenman 2395a17c678eSDavid Greenman switch (command) { 2396a17c678eSDavid Greenman case SIOCSIFFLAGS: 2397f7788e8eSJonathan Lemon if (ifp->if_flags & IFF_ALLMULTI) 2398f7788e8eSJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 2399f7788e8eSJonathan Lemon else 2400f7788e8eSJonathan Lemon sc->flags &= ~FXP_FLAG_ALL_MCAST; 2401a17c678eSDavid Greenman 2402a17c678eSDavid Greenman /* 2403a17c678eSDavid Greenman * If interface is marked up and not running, then start it. 2404a17c678eSDavid Greenman * If it is marked down and running, stop it. 2405a17c678eSDavid Greenman * XXX If it's up then re-initialize it. This is so flags 2406a17c678eSDavid Greenman * such as IFF_PROMISC are handled. 2407a17c678eSDavid Greenman */ 2408a17c678eSDavid Greenman if (ifp->if_flags & IFF_UP) { 24094953bccaSNate Lawson fxp_init_body(sc); 2410a17c678eSDavid Greenman } else { 2411a17c678eSDavid Greenman if (ifp->if_flags & IFF_RUNNING) 24124a5f1499SDavid Greenman fxp_stop(sc); 2413a17c678eSDavid Greenman } 2414a17c678eSDavid Greenman break; 2415a17c678eSDavid Greenman 2416a17c678eSDavid Greenman case SIOCADDMULTI: 2417a17c678eSDavid Greenman case SIOCDELMULTI: 2418f7788e8eSJonathan Lemon if (ifp->if_flags & IFF_ALLMULTI) 2419f7788e8eSJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 2420f7788e8eSJonathan Lemon else 2421f7788e8eSJonathan Lemon sc->flags &= ~FXP_FLAG_ALL_MCAST; 2422a17c678eSDavid Greenman /* 2423a17c678eSDavid Greenman * Multicast list has changed; set the hardware filter 2424a17c678eSDavid Greenman * accordingly. 2425a17c678eSDavid Greenman */ 2426f7788e8eSJonathan Lemon if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) 2427397f9dfeSDavid Greenman fxp_mc_setup(sc); 2428397f9dfeSDavid Greenman /* 2429f7788e8eSJonathan Lemon * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it 2430397f9dfeSDavid Greenman * again rather than else {}. 2431397f9dfeSDavid Greenman */ 2432f7788e8eSJonathan Lemon if (sc->flags & FXP_FLAG_ALL_MCAST) 24334953bccaSNate Lawson fxp_init_body(sc); 2434a17c678eSDavid Greenman error = 0; 2435ba8c6fd5SDavid Greenman break; 2436ba8c6fd5SDavid Greenman 2437ba8c6fd5SDavid Greenman case SIOCSIFMEDIA: 2438ba8c6fd5SDavid Greenman case SIOCGIFMEDIA: 2439f7788e8eSJonathan Lemon if (sc->miibus != NULL) { 2440f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 2441f7788e8eSJonathan Lemon error = ifmedia_ioctl(ifp, ifr, 2442f7788e8eSJonathan Lemon &mii->mii_media, command); 2443f7788e8eSJonathan Lemon } else { 2444ba8c6fd5SDavid Greenman error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command); 2445f7788e8eSJonathan Lemon } 2446a17c678eSDavid Greenman break; 2447a17c678eSDavid Greenman 2448fb917226SRuslan Ermilov case SIOCSIFCAP: 24498ef1f631SYaroslav Tykhiy mask = ifp->if_capenable ^ ifr->ifr_reqcap; 24508ef1f631SYaroslav Tykhiy if (mask & IFCAP_POLLING) 24518ef1f631SYaroslav Tykhiy ifp->if_capenable ^= IFCAP_POLLING; 24528ef1f631SYaroslav Tykhiy if (mask & IFCAP_VLAN_MTU) { 24538ef1f631SYaroslav Tykhiy ifp->if_capenable ^= IFCAP_VLAN_MTU; 24548ef1f631SYaroslav Tykhiy if (sc->revision != FXP_REV_82557) 24558ef1f631SYaroslav Tykhiy flag = FXP_FLAG_LONG_PKT_EN; 24568ef1f631SYaroslav Tykhiy else /* a hack to get long frames on the old chip */ 24578ef1f631SYaroslav Tykhiy flag = FXP_FLAG_SAVE_BAD; 24588ef1f631SYaroslav Tykhiy sc->flags ^= flag; 24598ef1f631SYaroslav Tykhiy if (ifp->if_flags & IFF_UP) 24608ef1f631SYaroslav Tykhiy fxp_init_body(sc); 24618ef1f631SYaroslav Tykhiy } 2462fb917226SRuslan Ermilov break; 2463fb917226SRuslan Ermilov 2464a17c678eSDavid Greenman default: 24654953bccaSNate Lawson /* 24664953bccaSNate Lawson * ether_ioctl() will eventually call fxp_start() which 24674953bccaSNate Lawson * will result in mutex recursion so drop it first. 24684953bccaSNate Lawson */ 24694953bccaSNate Lawson FXP_UNLOCK(sc); 2470673d9191SSam Leffler error = ether_ioctl(ifp, command, data); 2471a17c678eSDavid Greenman } 247267fc050fSMaxime Henrion if (FXP_LOCKED(sc)) 24734953bccaSNate Lawson FXP_UNLOCK(sc); 2474f7788e8eSJonathan Lemon splx(s); 2475a17c678eSDavid Greenman return (error); 2476a17c678eSDavid Greenman } 2477397f9dfeSDavid Greenman 2478397f9dfeSDavid Greenman /* 247909882363SJonathan Lemon * Fill in the multicast address list and return number of entries. 248009882363SJonathan Lemon */ 248109882363SJonathan Lemon static int 248209882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc) 248309882363SJonathan Lemon { 248409882363SJonathan Lemon struct fxp_cb_mcs *mcsp = sc->mcsp; 248509882363SJonathan Lemon struct ifnet *ifp = &sc->sc_if; 248609882363SJonathan Lemon struct ifmultiaddr *ifma; 248709882363SJonathan Lemon int nmcasts; 248809882363SJonathan Lemon 248909882363SJonathan Lemon nmcasts = 0; 249009882363SJonathan Lemon if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) { 249109882363SJonathan Lemon #if __FreeBSD_version < 500000 249209882363SJonathan Lemon LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 249309882363SJonathan Lemon #else 249409882363SJonathan Lemon TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 249509882363SJonathan Lemon #endif 249609882363SJonathan Lemon if (ifma->ifma_addr->sa_family != AF_LINK) 249709882363SJonathan Lemon continue; 249809882363SJonathan Lemon if (nmcasts >= MAXMCADDR) { 249909882363SJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 250009882363SJonathan Lemon nmcasts = 0; 250109882363SJonathan Lemon break; 250209882363SJonathan Lemon } 250309882363SJonathan Lemon bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 2504bafb64afSMaxime Henrion &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN); 250509882363SJonathan Lemon nmcasts++; 250609882363SJonathan Lemon } 250709882363SJonathan Lemon } 2508bafb64afSMaxime Henrion mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN); 250909882363SJonathan Lemon return (nmcasts); 251009882363SJonathan Lemon } 251109882363SJonathan Lemon 251209882363SJonathan Lemon /* 2513397f9dfeSDavid Greenman * Program the multicast filter. 2514397f9dfeSDavid Greenman * 2515397f9dfeSDavid Greenman * We have an artificial restriction that the multicast setup command 2516397f9dfeSDavid Greenman * must be the first command in the chain, so we take steps to ensure 25173114fdb4SDavid Greenman * this. By requiring this, it allows us to keep up the performance of 2518397f9dfeSDavid Greenman * the pre-initialized command ring (esp. link pointers) by not actually 2519dc733423SDag-Erling Smørgrav * inserting the mcsetup command in the ring - i.e. its link pointer 2520397f9dfeSDavid Greenman * points to the TxCB ring, but the mcsetup descriptor itself is not part 2521397f9dfeSDavid Greenman * of it. We then can do 'CU_START' on the mcsetup descriptor and have it 2522397f9dfeSDavid Greenman * lead into the regular TxCB ring when it completes. 2523397f9dfeSDavid Greenman * 2524397f9dfeSDavid Greenman * This function must be called at splimp. 2525397f9dfeSDavid Greenman */ 2526397f9dfeSDavid Greenman static void 2527f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc) 2528397f9dfeSDavid Greenman { 2529397f9dfeSDavid Greenman struct fxp_cb_mcs *mcsp = sc->mcsp; 2530397f9dfeSDavid Greenman struct ifnet *ifp = &sc->sc_if; 2531b2badf02SMaxime Henrion struct fxp_tx *txp; 25327dced78aSDavid Greenman int count; 2533397f9dfeSDavid Greenman 253467fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 25353114fdb4SDavid Greenman /* 25363114fdb4SDavid Greenman * If there are queued commands, we must wait until they are all 25373114fdb4SDavid Greenman * completed. If we are already waiting, then add a NOP command 25383114fdb4SDavid Greenman * with interrupt option so that we're notified when all commands 25393114fdb4SDavid Greenman * have been completed - fxp_start() ensures that no additional 25403114fdb4SDavid Greenman * TX commands will be added when need_mcsetup is true. 25413114fdb4SDavid Greenman */ 2542397f9dfeSDavid Greenman if (sc->tx_queued) { 25433114fdb4SDavid Greenman /* 25443114fdb4SDavid Greenman * need_mcsetup will be true if we are already waiting for the 25453114fdb4SDavid Greenman * NOP command to be completed (see below). In this case, bail. 25463114fdb4SDavid Greenman */ 25473114fdb4SDavid Greenman if (sc->need_mcsetup) 25483114fdb4SDavid Greenman return; 2549397f9dfeSDavid Greenman sc->need_mcsetup = 1; 25503114fdb4SDavid Greenman 25513114fdb4SDavid Greenman /* 255272a32a26SJonathan Lemon * Add a NOP command with interrupt so that we are notified 255372a32a26SJonathan Lemon * when all TX commands have been processed. 25543114fdb4SDavid Greenman */ 2555b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_last->tx_next; 2556b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 2557b2badf02SMaxime Henrion txp->tx_cb->cb_status = 0; 255883e6547dSMaxime Henrion txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP | 255983e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 25603114fdb4SDavid Greenman /* 25613114fdb4SDavid Greenman * Advance the end of list forward. 25623114fdb4SDavid Greenman */ 256383e6547dSMaxime Henrion sc->fxp_desc.tx_last->tx_cb->cb_command &= 256483e6547dSMaxime Henrion htole16(~FXP_CB_COMMAND_S); 25655f361cbeSMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2566b2badf02SMaxime Henrion sc->fxp_desc.tx_last = txp; 25673114fdb4SDavid Greenman sc->tx_queued++; 25683114fdb4SDavid Greenman /* 25693114fdb4SDavid Greenman * Issue a resume in case the CU has just suspended. 25703114fdb4SDavid Greenman */ 25713114fdb4SDavid Greenman fxp_scb_wait(sc); 25722e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME); 25733114fdb4SDavid Greenman /* 25743114fdb4SDavid Greenman * Set a 5 second timer just in case we don't hear from the 25753114fdb4SDavid Greenman * card again. 25763114fdb4SDavid Greenman */ 25773114fdb4SDavid Greenman ifp->if_timer = 5; 25783114fdb4SDavid Greenman 2579397f9dfeSDavid Greenman return; 2580397f9dfeSDavid Greenman } 2581397f9dfeSDavid Greenman sc->need_mcsetup = 0; 2582397f9dfeSDavid Greenman 2583397f9dfeSDavid Greenman /* 2584397f9dfeSDavid Greenman * Initialize multicast setup descriptor. 2585397f9dfeSDavid Greenman */ 2586397f9dfeSDavid Greenman mcsp->cb_status = 0; 258783e6547dSMaxime Henrion mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | 258883e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 258983e6547dSMaxime Henrion mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr); 2590b2badf02SMaxime Henrion txp = &sc->fxp_desc.mcs_tx; 2591b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 2592b2badf02SMaxime Henrion txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp; 2593b2badf02SMaxime Henrion txp->tx_next = sc->fxp_desc.tx_list; 259409882363SJonathan Lemon (void) fxp_mc_addrs(sc); 2595b2badf02SMaxime Henrion sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp; 2596397f9dfeSDavid Greenman sc->tx_queued = 1; 2597397f9dfeSDavid Greenman 2598397f9dfeSDavid Greenman /* 2599397f9dfeSDavid Greenman * Wait until command unit is not active. This should never 2600397f9dfeSDavid Greenman * be the case when nothing is queued, but make sure anyway. 2601397f9dfeSDavid Greenman */ 26027dced78aSDavid Greenman count = 100; 2603397f9dfeSDavid Greenman while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) == 26047dced78aSDavid Greenman FXP_SCB_CUS_ACTIVE && --count) 26057dced78aSDavid Greenman DELAY(10); 26067dced78aSDavid Greenman if (count == 0) { 2607f7788e8eSJonathan Lemon device_printf(sc->dev, "command queue timeout\n"); 26087dced78aSDavid Greenman return; 26097dced78aSDavid Greenman } 2610397f9dfeSDavid Greenman 2611397f9dfeSDavid Greenman /* 2612397f9dfeSDavid Greenman * Start the multicast setup command. 2613397f9dfeSDavid Greenman */ 2614397f9dfeSDavid Greenman fxp_scb_wait(sc); 2615b2badf02SMaxime Henrion bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE); 2616b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr); 26172e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2618397f9dfeSDavid Greenman 26193114fdb4SDavid Greenman ifp->if_timer = 2; 2620397f9dfeSDavid Greenman return; 2621397f9dfeSDavid Greenman } 262272a32a26SJonathan Lemon 262374d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE; 262474d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE; 262574d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE; 262674d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE; 262774d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE; 262874d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE; 262972a32a26SJonathan Lemon 263074d1ed23SMaxime Henrion #define UCODE(x) x, sizeof(x)/sizeof(uint32_t) 263172a32a26SJonathan Lemon 263272a32a26SJonathan Lemon struct ucode { 263374d1ed23SMaxime Henrion uint32_t revision; 263474d1ed23SMaxime Henrion uint32_t *ucode; 263572a32a26SJonathan Lemon int length; 263672a32a26SJonathan Lemon u_short int_delay_offset; 263772a32a26SJonathan Lemon u_short bundle_max_offset; 263872a32a26SJonathan Lemon } ucode_table[] = { 263972a32a26SJonathan Lemon { FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 }, 264072a32a26SJonathan Lemon { FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 }, 264172a32a26SJonathan Lemon { FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma), 264272a32a26SJonathan Lemon D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD }, 264372a32a26SJonathan Lemon { FXP_REV_82559S_A, UCODE(fxp_ucode_d101s), 264472a32a26SJonathan Lemon D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD }, 264572a32a26SJonathan Lemon { FXP_REV_82550, UCODE(fxp_ucode_d102), 264672a32a26SJonathan Lemon D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD }, 264772a32a26SJonathan Lemon { FXP_REV_82550_C, UCODE(fxp_ucode_d102c), 264872a32a26SJonathan Lemon D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD }, 264972a32a26SJonathan Lemon { 0, NULL, 0, 0, 0 } 265072a32a26SJonathan Lemon }; 265172a32a26SJonathan Lemon 265272a32a26SJonathan Lemon static void 265372a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc) 265472a32a26SJonathan Lemon { 265572a32a26SJonathan Lemon struct ucode *uc; 265672a32a26SJonathan Lemon struct fxp_cb_ucode *cbp; 265794a4f968SPyun YongHyeon int i; 265872a32a26SJonathan Lemon 265972a32a26SJonathan Lemon for (uc = ucode_table; uc->ucode != NULL; uc++) 266072a32a26SJonathan Lemon if (sc->revision == uc->revision) 266172a32a26SJonathan Lemon break; 266272a32a26SJonathan Lemon if (uc->ucode == NULL) 266372a32a26SJonathan Lemon return; 2664b2badf02SMaxime Henrion cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list; 266572a32a26SJonathan Lemon cbp->cb_status = 0; 266683e6547dSMaxime Henrion cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL); 266783e6547dSMaxime Henrion cbp->link_addr = 0xffffffff; /* (no) next command */ 266894a4f968SPyun YongHyeon for (i = 0; i < uc->length; i++) 266994a4f968SPyun YongHyeon cbp->ucode[i] = htole32(uc->ucode[i]); 267072a32a26SJonathan Lemon if (uc->int_delay_offset) 267174d1ed23SMaxime Henrion *(uint16_t *)&cbp->ucode[uc->int_delay_offset] = 267283e6547dSMaxime Henrion htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2); 267372a32a26SJonathan Lemon if (uc->bundle_max_offset) 267474d1ed23SMaxime Henrion *(uint16_t *)&cbp->ucode[uc->bundle_max_offset] = 267583e6547dSMaxime Henrion htole16(sc->tunable_bundle_max); 267672a32a26SJonathan Lemon /* 267772a32a26SJonathan Lemon * Download the ucode to the chip. 267872a32a26SJonathan Lemon */ 267972a32a26SJonathan Lemon fxp_scb_wait(sc); 2680b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2681b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr); 268272a32a26SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 268372a32a26SJonathan Lemon /* ...and wait for it to complete. */ 2684209b07bcSMaxime Henrion fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map); 2685b2badf02SMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE); 268672a32a26SJonathan Lemon device_printf(sc->dev, 268772a32a26SJonathan Lemon "Microcode loaded, int_delay: %d usec bundle_max: %d\n", 268872a32a26SJonathan Lemon sc->tunable_int_delay, 268972a32a26SJonathan Lemon uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max); 269072a32a26SJonathan Lemon sc->flags |= FXP_FLAG_UCODE; 269172a32a26SJonathan Lemon } 269272a32a26SJonathan Lemon 269372a32a26SJonathan Lemon static int 269472a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 269572a32a26SJonathan Lemon { 269672a32a26SJonathan Lemon int error, value; 269772a32a26SJonathan Lemon 269872a32a26SJonathan Lemon value = *(int *)arg1; 269972a32a26SJonathan Lemon error = sysctl_handle_int(oidp, &value, 0, req); 270072a32a26SJonathan Lemon if (error || !req->newptr) 270172a32a26SJonathan Lemon return (error); 270272a32a26SJonathan Lemon if (value < low || value > high) 270372a32a26SJonathan Lemon return (EINVAL); 270472a32a26SJonathan Lemon *(int *)arg1 = value; 270572a32a26SJonathan Lemon return (0); 270672a32a26SJonathan Lemon } 270772a32a26SJonathan Lemon 270872a32a26SJonathan Lemon /* 270972a32a26SJonathan Lemon * Interrupt delay is expressed in microseconds, a multiplier is used 271072a32a26SJonathan Lemon * to convert this to the appropriate clock ticks before using. 271172a32a26SJonathan Lemon */ 271272a32a26SJonathan Lemon static int 271372a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS) 271472a32a26SJonathan Lemon { 271572a32a26SJonathan Lemon return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000)); 271672a32a26SJonathan Lemon } 271772a32a26SJonathan Lemon 271872a32a26SJonathan Lemon static int 271972a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS) 272072a32a26SJonathan Lemon { 272172a32a26SJonathan Lemon return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff)); 272272a32a26SJonathan Lemon } 2723