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 37f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS 38f0796cd2SGleb Smirnoff #include "opt_device_polling.h" 39f0796cd2SGleb Smirnoff #endif 40f0796cd2SGleb Smirnoff 41a17c678eSDavid Greenman #include <sys/param.h> 42a17c678eSDavid Greenman #include <sys/systm.h> 438fae3bd4SPyun YongHyeon #include <sys/bus.h> 4483e6547dSMaxime Henrion #include <sys/endian.h> 45a17c678eSDavid Greenman #include <sys/kernel.h> 468fae3bd4SPyun YongHyeon #include <sys/mbuf.h> 476d7e1582SPyun YongHyeon #include <sys/lock.h> 48fe12f24bSPoul-Henning Kamp #include <sys/module.h> 496d7e1582SPyun YongHyeon #include <sys/mutex.h> 508fae3bd4SPyun YongHyeon #include <sys/rman.h> 514458ac71SBruce Evans #include <sys/socket.h> 528fae3bd4SPyun YongHyeon #include <sys/sockio.h> 5372a32a26SJonathan Lemon #include <sys/sysctl.h> 54a17c678eSDavid Greenman 558fae3bd4SPyun YongHyeon #include <net/bpf.h> 568fae3bd4SPyun YongHyeon #include <net/ethernet.h> 57a17c678eSDavid Greenman #include <net/if.h> 588fae3bd4SPyun YongHyeon #include <net/if_arp.h> 59397f9dfeSDavid Greenman #include <net/if_dl.h> 60ba8c6fd5SDavid Greenman #include <net/if_media.h> 61e8c8b728SJonathan Lemon #include <net/if_types.h> 62e8c8b728SJonathan Lemon #include <net/if_vlan_var.h> 63e8c8b728SJonathan Lemon 64c8bca6dcSBill Paul #include <netinet/in.h> 65c8bca6dcSBill Paul #include <netinet/in_systm.h> 66c8bca6dcSBill Paul #include <netinet/ip.h> 67f13075afSPyun YongHyeon #include <netinet/tcp.h> 68f13075afSPyun YongHyeon #include <netinet/udp.h> 69f13075afSPyun YongHyeon 70f13075afSPyun YongHyeon #include <machine/bus.h> 71c8bca6dcSBill Paul #include <machine/in_cksum.h> 72f13075afSPyun YongHyeon #include <machine/resource.h> 73c8bca6dcSBill Paul 744fbd232cSWarner Losh #include <dev/pci/pcivar.h> 754fbd232cSWarner Losh #include <dev/pci/pcireg.h> /* for PCIM_CMD_xxx */ 76a17c678eSDavid Greenman 77f7788e8eSJonathan Lemon #include <dev/mii/mii.h> 78f7788e8eSJonathan Lemon #include <dev/mii/miivar.h> 79f7788e8eSJonathan Lemon 80f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h> 81f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h> 8272a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h> 83f7788e8eSJonathan Lemon 84f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1); 85f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1); 86f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1); 87f7788e8eSJonathan Lemon #include "miibus_if.h" 884fc1dda9SAndrew Gallatin 89ba8c6fd5SDavid Greenman /* 90ba8c6fd5SDavid Greenman * NOTE! On the Alpha, we have an alignment constraint. The 91ba8c6fd5SDavid Greenman * card DMAs the packet immediately following the RFA. However, 92ba8c6fd5SDavid Greenman * the first thing in the packet is a 14-byte Ethernet header. 93ba8c6fd5SDavid Greenman * This means that the packet is misaligned. To compensate, 94ba8c6fd5SDavid Greenman * we actually offset the RFA 2 bytes into the cluster. This 95ba8c6fd5SDavid Greenman * alignes the packet after the Ethernet header at a 32-bit 96ba8c6fd5SDavid Greenman * boundary. HOWEVER! This means that the RFA is misaligned! 97ba8c6fd5SDavid Greenman */ 98ba8c6fd5SDavid Greenman #define RFA_ALIGNMENT_FUDGE 2 99ba8c6fd5SDavid Greenman 100ba8c6fd5SDavid Greenman /* 101f7788e8eSJonathan Lemon * Set initial transmit threshold at 64 (512 bytes). This is 102f7788e8eSJonathan Lemon * increased by 64 (512 bytes) at a time, to maximum of 192 103f7788e8eSJonathan Lemon * (1536 bytes), if an underrun occurs. 104f7788e8eSJonathan Lemon */ 105f7788e8eSJonathan Lemon static int tx_threshold = 64; 106f7788e8eSJonathan Lemon 107f7788e8eSJonathan Lemon /* 108f7788e8eSJonathan Lemon * The configuration byte map has several undefined fields which 109f7788e8eSJonathan Lemon * must be one or must be zero. Set up a template for these bits 110f7788e8eSJonathan Lemon * only, (assuming a 82557 chip) leaving the actual configuration 111f7788e8eSJonathan Lemon * to fxp_init. 112f7788e8eSJonathan Lemon * 113f7788e8eSJonathan Lemon * See struct fxp_cb_config for the bit definitions. 114f7788e8eSJonathan Lemon */ 115f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = { 116f7788e8eSJonathan Lemon 0x0, 0x0, /* cb_status */ 117f7788e8eSJonathan Lemon 0x0, 0x0, /* cb_command */ 118f7788e8eSJonathan Lemon 0x0, 0x0, 0x0, 0x0, /* link_addr */ 119f7788e8eSJonathan Lemon 0x0, /* 0 */ 120f7788e8eSJonathan Lemon 0x0, /* 1 */ 121f7788e8eSJonathan Lemon 0x0, /* 2 */ 122f7788e8eSJonathan Lemon 0x0, /* 3 */ 123f7788e8eSJonathan Lemon 0x0, /* 4 */ 124f7788e8eSJonathan Lemon 0x0, /* 5 */ 125f7788e8eSJonathan Lemon 0x32, /* 6 */ 126f7788e8eSJonathan Lemon 0x0, /* 7 */ 127f7788e8eSJonathan Lemon 0x0, /* 8 */ 128f7788e8eSJonathan Lemon 0x0, /* 9 */ 129f7788e8eSJonathan Lemon 0x6, /* 10 */ 130f7788e8eSJonathan Lemon 0x0, /* 11 */ 131f7788e8eSJonathan Lemon 0x0, /* 12 */ 132f7788e8eSJonathan Lemon 0x0, /* 13 */ 133f7788e8eSJonathan Lemon 0xf2, /* 14 */ 134f7788e8eSJonathan Lemon 0x48, /* 15 */ 135f7788e8eSJonathan Lemon 0x0, /* 16 */ 136f7788e8eSJonathan Lemon 0x40, /* 17 */ 137f7788e8eSJonathan Lemon 0xf0, /* 18 */ 138f7788e8eSJonathan Lemon 0x0, /* 19 */ 139f7788e8eSJonathan Lemon 0x3f, /* 20 */ 140f7788e8eSJonathan Lemon 0x5 /* 21 */ 141f7788e8eSJonathan Lemon }; 142f7788e8eSJonathan Lemon 143f7788e8eSJonathan Lemon struct fxp_ident { 14474d1ed23SMaxime Henrion uint16_t devid; 145f19fc5d8SJohn Polstra int16_t revid; /* -1 matches anything */ 146f7788e8eSJonathan Lemon char *name; 147f7788e8eSJonathan Lemon }; 148f7788e8eSJonathan Lemon 149f7788e8eSJonathan Lemon /* 150f7788e8eSJonathan Lemon * Claim various Intel PCI device identifiers for this driver. The 151f7788e8eSJonathan Lemon * sub-vendor and sub-device field are extensively used to identify 152f7788e8eSJonathan Lemon * particular variants, but we don't currently differentiate between 153f7788e8eSJonathan Lemon * them. 154f7788e8eSJonathan Lemon */ 155f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = { 156f19fc5d8SJohn Polstra { 0x1029, -1, "Intel 82559 PCI/CardBus Pro/100" }, 157f19fc5d8SJohn Polstra { 0x1030, -1, "Intel 82559 Pro/100 Ethernet" }, 158f19fc5d8SJohn Polstra { 0x1031, -1, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" }, 159f19fc5d8SJohn Polstra { 0x1032, -1, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" }, 160f19fc5d8SJohn Polstra { 0x1033, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" }, 161f19fc5d8SJohn Polstra { 0x1034, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" }, 162f19fc5d8SJohn Polstra { 0x1035, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" }, 163f19fc5d8SJohn Polstra { 0x1036, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" }, 164f19fc5d8SJohn Polstra { 0x1037, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" }, 165f19fc5d8SJohn Polstra { 0x1038, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" }, 166f19fc5d8SJohn Polstra { 0x1039, -1, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" }, 167f19fc5d8SJohn Polstra { 0x103A, -1, "Intel 82801DB (ICH4) Pro/100 Ethernet" }, 168f19fc5d8SJohn Polstra { 0x103B, -1, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" }, 169f19fc5d8SJohn Polstra { 0x103C, -1, "Intel 82801DB (ICH4) Pro/100 Ethernet" }, 170f19fc5d8SJohn Polstra { 0x103D, -1, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" }, 171f19fc5d8SJohn Polstra { 0x103E, -1, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" }, 172f19fc5d8SJohn Polstra { 0x1050, -1, "Intel 82801BA (D865) Pro/100 VE Ethernet" }, 173c2b37819SWarner Losh { 0x1051, -1, "Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" }, 174f19fc5d8SJohn Polstra { 0x1059, -1, "Intel 82551QM Pro/100 M Mobile Connection" }, 175048ca166SMaxime Henrion { 0x1064, -1, "Intel 82562EZ (ICH6)" }, 17642a4336aSRink Springer { 0x1065, -1, "Intel 82562ET/EZ/GT/GZ PRO/100 VE Ethernet" }, 17729a8929dSMaxime Henrion { 0x1068, -1, "Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" }, 17852dfd9cdSMaxime Henrion { 0x1069, -1, "Intel 82562EM/EX/GX Pro/100 Ethernet" }, 179847f5310SRemko Lodder { 0x1091, -1, "Intel 82562GX Pro/100 Ethernet" }, 180c943ffccSMatteo Riondato { 0x1092, -1, "Intel Pro/100 VE Network Connection" }, 181597d4fe4SRink Springer { 0x1093, -1, "Intel Pro/100 VM Network Connection" }, 18242a4336aSRink Springer { 0x1094, -1, "Intel Pro/100 946GZ (ICH7) Network Connection" }, 183f19fc5d8SJohn Polstra { 0x1209, -1, "Intel 82559ER Embedded 10/100 Ethernet" }, 184f19fc5d8SJohn Polstra { 0x1229, 0x01, "Intel 82557 Pro/100 Ethernet" }, 185f19fc5d8SJohn Polstra { 0x1229, 0x02, "Intel 82557 Pro/100 Ethernet" }, 186f19fc5d8SJohn Polstra { 0x1229, 0x03, "Intel 82557 Pro/100 Ethernet" }, 187f19fc5d8SJohn Polstra { 0x1229, 0x04, "Intel 82558 Pro/100 Ethernet" }, 188f19fc5d8SJohn Polstra { 0x1229, 0x05, "Intel 82558 Pro/100 Ethernet" }, 189f19fc5d8SJohn Polstra { 0x1229, 0x06, "Intel 82559 Pro/100 Ethernet" }, 190f19fc5d8SJohn Polstra { 0x1229, 0x07, "Intel 82559 Pro/100 Ethernet" }, 191f19fc5d8SJohn Polstra { 0x1229, 0x08, "Intel 82559 Pro/100 Ethernet" }, 192f19fc5d8SJohn Polstra { 0x1229, 0x09, "Intel 82559ER Pro/100 Ethernet" }, 193f19fc5d8SJohn Polstra { 0x1229, 0x0c, "Intel 82550 Pro/100 Ethernet" }, 194f19fc5d8SJohn Polstra { 0x1229, 0x0d, "Intel 82550 Pro/100 Ethernet" }, 195f19fc5d8SJohn Polstra { 0x1229, 0x0e, "Intel 82550 Pro/100 Ethernet" }, 196f19fc5d8SJohn Polstra { 0x1229, 0x0f, "Intel 82551 Pro/100 Ethernet" }, 197f19fc5d8SJohn Polstra { 0x1229, 0x10, "Intel 82551 Pro/100 Ethernet" }, 198f19fc5d8SJohn Polstra { 0x1229, -1, "Intel 82557/8/9 Pro/100 Ethernet" }, 199f19fc5d8SJohn Polstra { 0x2449, -1, "Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" }, 20086c8aacbSMaxime Henrion { 0x27dc, -1, "Intel 82801GB (ICH7) 10/100 Ethernet" }, 201f19fc5d8SJohn Polstra { 0, -1, NULL }, 202f7788e8eSJonathan Lemon }; 203f7788e8eSJonathan Lemon 204c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR 205c8bca6dcSBill Paul #define FXP_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 206c8bca6dcSBill Paul #else 207c8bca6dcSBill Paul #define FXP_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 208c8bca6dcSBill Paul #endif 209c8bca6dcSBill Paul 210f7788e8eSJonathan Lemon static int fxp_probe(device_t dev); 211f7788e8eSJonathan Lemon static int fxp_attach(device_t dev); 212f7788e8eSJonathan Lemon static int fxp_detach(device_t dev); 213f7788e8eSJonathan Lemon static int fxp_shutdown(device_t dev); 214f7788e8eSJonathan Lemon static int fxp_suspend(device_t dev); 215f7788e8eSJonathan Lemon static int fxp_resume(device_t dev); 216f7788e8eSJonathan Lemon 217f7788e8eSJonathan Lemon static void fxp_intr(void *xsc); 218f13075afSPyun YongHyeon static void fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp, 219f13075afSPyun YongHyeon struct mbuf *m, uint16_t status, int pos); 2201abcdbd1SAttilio Rao static int fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, 22174d1ed23SMaxime Henrion uint8_t statack, int count); 222f7788e8eSJonathan Lemon static void fxp_init(void *xsc); 2234953bccaSNate Lawson static void fxp_init_body(struct fxp_softc *sc); 224f7788e8eSJonathan Lemon static void fxp_tick(void *xsc); 225f7788e8eSJonathan Lemon static void fxp_start(struct ifnet *ifp); 2264953bccaSNate Lawson static void fxp_start_body(struct ifnet *ifp); 2274e53f837SPyun YongHyeon static int fxp_encap(struct fxp_softc *sc, struct mbuf **m_head); 2284e53f837SPyun YongHyeon static void fxp_txeof(struct fxp_softc *sc); 229f7788e8eSJonathan Lemon static void fxp_stop(struct fxp_softc *sc); 230f7788e8eSJonathan Lemon static void fxp_release(struct fxp_softc *sc); 231f7788e8eSJonathan Lemon static int fxp_ioctl(struct ifnet *ifp, u_long command, 232f7788e8eSJonathan Lemon caddr_t data); 233df79d527SGleb Smirnoff static void fxp_watchdog(struct fxp_softc *sc); 23485050421SPyun YongHyeon static void fxp_add_rfabuf(struct fxp_softc *sc, 23585050421SPyun YongHyeon struct fxp_rx *rxp); 23685050421SPyun YongHyeon static void fxp_discard_rfabuf(struct fxp_softc *sc, 23785050421SPyun YongHyeon struct fxp_rx *rxp); 23885050421SPyun YongHyeon static int fxp_new_rfabuf(struct fxp_softc *sc, 23985050421SPyun YongHyeon struct fxp_rx *rxp); 24009882363SJonathan Lemon static int fxp_mc_addrs(struct fxp_softc *sc); 241f7788e8eSJonathan Lemon static void fxp_mc_setup(struct fxp_softc *sc); 24274d1ed23SMaxime Henrion static uint16_t fxp_eeprom_getword(struct fxp_softc *sc, int offset, 243f7788e8eSJonathan Lemon int autosize); 24400c4116bSJonathan Lemon static void fxp_eeprom_putword(struct fxp_softc *sc, int offset, 24574d1ed23SMaxime Henrion uint16_t data); 246f7788e8eSJonathan Lemon static void fxp_autosize_eeprom(struct fxp_softc *sc); 247f7788e8eSJonathan Lemon static void fxp_read_eeprom(struct fxp_softc *sc, u_short *data, 248f7788e8eSJonathan Lemon int offset, int words); 24900c4116bSJonathan Lemon static void fxp_write_eeprom(struct fxp_softc *sc, u_short *data, 25000c4116bSJonathan Lemon int offset, int words); 251f7788e8eSJonathan Lemon static int fxp_ifmedia_upd(struct ifnet *ifp); 252f7788e8eSJonathan Lemon static void fxp_ifmedia_sts(struct ifnet *ifp, 253f7788e8eSJonathan Lemon struct ifmediareq *ifmr); 254f7788e8eSJonathan Lemon static int fxp_serial_ifmedia_upd(struct ifnet *ifp); 255f7788e8eSJonathan Lemon static void fxp_serial_ifmedia_sts(struct ifnet *ifp, 256f7788e8eSJonathan Lemon struct ifmediareq *ifmr); 257f1928b0cSKevin Lo static int fxp_miibus_readreg(device_t dev, int phy, int reg); 25816ec4b00SWarner Losh static int fxp_miibus_writereg(device_t dev, int phy, int reg, 259f7788e8eSJonathan Lemon int value); 26072a32a26SJonathan Lemon static void fxp_load_ucode(struct fxp_softc *sc); 26172a32a26SJonathan Lemon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, 26272a32a26SJonathan Lemon int low, int high); 26372a32a26SJonathan Lemon static int sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS); 26472a32a26SJonathan Lemon static int sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS); 26528935f27SMaxime Henrion static void fxp_scb_wait(struct fxp_softc *sc); 26628935f27SMaxime Henrion static void fxp_scb_cmd(struct fxp_softc *sc, int cmd); 26728935f27SMaxime Henrion static void fxp_dma_wait(struct fxp_softc *sc, 26874d1ed23SMaxime Henrion volatile uint16_t *status, bus_dma_tag_t dmat, 269209b07bcSMaxime Henrion bus_dmamap_t map); 270f7788e8eSJonathan Lemon 271f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = { 272f7788e8eSJonathan Lemon /* Device interface */ 273f7788e8eSJonathan Lemon DEVMETHOD(device_probe, fxp_probe), 274f7788e8eSJonathan Lemon DEVMETHOD(device_attach, fxp_attach), 275f7788e8eSJonathan Lemon DEVMETHOD(device_detach, fxp_detach), 276f7788e8eSJonathan Lemon DEVMETHOD(device_shutdown, fxp_shutdown), 277f7788e8eSJonathan Lemon DEVMETHOD(device_suspend, fxp_suspend), 278f7788e8eSJonathan Lemon DEVMETHOD(device_resume, fxp_resume), 279f7788e8eSJonathan Lemon 280f7788e8eSJonathan Lemon /* MII interface */ 281f7788e8eSJonathan Lemon DEVMETHOD(miibus_readreg, fxp_miibus_readreg), 282f7788e8eSJonathan Lemon DEVMETHOD(miibus_writereg, fxp_miibus_writereg), 283f7788e8eSJonathan Lemon 284f7788e8eSJonathan Lemon { 0, 0 } 285f7788e8eSJonathan Lemon }; 286f7788e8eSJonathan Lemon 287f7788e8eSJonathan Lemon static driver_t fxp_driver = { 288f7788e8eSJonathan Lemon "fxp", 289f7788e8eSJonathan Lemon fxp_methods, 290f7788e8eSJonathan Lemon sizeof(struct fxp_softc), 291f7788e8eSJonathan Lemon }; 292f7788e8eSJonathan Lemon 293f7788e8eSJonathan Lemon static devclass_t fxp_devclass; 294f7788e8eSJonathan Lemon 295f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0); 296f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0); 297f7788e8eSJonathan Lemon 29805bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = { 29905bd8c22SMaxime Henrion { SYS_RES_MEMORY, FXP_PCI_MMBA, RF_ACTIVE }, 30005bd8c22SMaxime Henrion { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 30105bd8c22SMaxime Henrion { -1, 0 } 30205bd8c22SMaxime Henrion }; 30305bd8c22SMaxime Henrion 30405bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = { 30505bd8c22SMaxime Henrion { SYS_RES_IOPORT, FXP_PCI_IOBA, RF_ACTIVE }, 30605bd8c22SMaxime Henrion { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 30705bd8c22SMaxime Henrion { -1, 0 } 30805bd8c22SMaxime Henrion }; 30905bd8c22SMaxime Henrion 310f7788e8eSJonathan Lemon /* 311dfe61cf1SDavid Greenman * Wait for the previous command to be accepted (but not necessarily 312dfe61cf1SDavid Greenman * completed). 313dfe61cf1SDavid Greenman */ 31428935f27SMaxime Henrion static void 315f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc) 316a17c678eSDavid Greenman { 3173cf09dd1SMarcel Moolenaar union { 3183cf09dd1SMarcel Moolenaar uint16_t w; 3193cf09dd1SMarcel Moolenaar uint8_t b[2]; 3203cf09dd1SMarcel Moolenaar } flowctl; 321a17c678eSDavid Greenman int i = 10000; 322a17c678eSDavid Greenman 3237dced78aSDavid Greenman while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i) 3247dced78aSDavid Greenman DELAY(2); 3253cf09dd1SMarcel Moolenaar if (i == 0) { 3263cf09dd1SMarcel Moolenaar flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL); 3273cf09dd1SMarcel Moolenaar flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL + 1); 32800c4116bSJonathan Lemon device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n", 329e8c8b728SJonathan Lemon CSR_READ_1(sc, FXP_CSR_SCB_COMMAND), 330e8c8b728SJonathan Lemon CSR_READ_1(sc, FXP_CSR_SCB_STATACK), 3313cf09dd1SMarcel Moolenaar CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w); 3323cf09dd1SMarcel Moolenaar } 3337dced78aSDavid Greenman } 3347dced78aSDavid Greenman 33528935f27SMaxime Henrion static void 3362e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd) 3372e2b8238SJonathan Lemon { 3382e2b8238SJonathan Lemon 3392e2b8238SJonathan Lemon if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) { 3402e2b8238SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP); 3412e2b8238SJonathan Lemon fxp_scb_wait(sc); 3422e2b8238SJonathan Lemon } 3432e2b8238SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd); 3442e2b8238SJonathan Lemon } 3452e2b8238SJonathan Lemon 34628935f27SMaxime Henrion static void 34774d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status, 348209b07bcSMaxime Henrion bus_dma_tag_t dmat, bus_dmamap_t map) 3497dced78aSDavid Greenman { 3505986d0d2SPyun YongHyeon int i; 3517dced78aSDavid Greenman 3525986d0d2SPyun YongHyeon for (i = 10000; i > 0; i--) { 3537dced78aSDavid Greenman DELAY(2); 3545986d0d2SPyun YongHyeon bus_dmamap_sync(dmat, map, 3555986d0d2SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 3565986d0d2SPyun YongHyeon if ((le16toh(*status) & FXP_CB_STATUS_C) != 0) 3575986d0d2SPyun YongHyeon break; 358209b07bcSMaxime Henrion } 3597dced78aSDavid Greenman if (i == 0) 360f7788e8eSJonathan Lemon device_printf(sc->dev, "DMA timeout\n"); 361a17c678eSDavid Greenman } 362a17c678eSDavid Greenman 363dfe61cf1SDavid Greenman /* 36428935f27SMaxime Henrion * Return identification string if this device is ours. 365dfe61cf1SDavid Greenman */ 3666182fdbdSPeter Wemm static int 3676182fdbdSPeter Wemm fxp_probe(device_t dev) 368a17c678eSDavid Greenman { 36974d1ed23SMaxime Henrion uint16_t devid; 37074d1ed23SMaxime Henrion uint8_t revid; 371f7788e8eSJonathan Lemon struct fxp_ident *ident; 372f7788e8eSJonathan Lemon 37355ce7b51SDavid Greenman if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) { 374f7788e8eSJonathan Lemon devid = pci_get_device(dev); 375f19fc5d8SJohn Polstra revid = pci_get_revid(dev); 376f7788e8eSJonathan Lemon for (ident = fxp_ident_table; ident->name != NULL; ident++) { 377f19fc5d8SJohn Polstra if (ident->devid == devid && 378f19fc5d8SJohn Polstra (ident->revid == revid || ident->revid == -1)) { 379f7788e8eSJonathan Lemon device_set_desc(dev, ident->name); 380538565c4SWarner Losh return (BUS_PROBE_DEFAULT); 38155ce7b51SDavid Greenman } 382dd68ef16SPeter Wemm } 383f7788e8eSJonathan Lemon } 384f7788e8eSJonathan Lemon return (ENXIO); 3856182fdbdSPeter Wemm } 3866182fdbdSPeter Wemm 387b2badf02SMaxime Henrion static void 388b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 389b2badf02SMaxime Henrion { 39074d1ed23SMaxime Henrion uint32_t *addr; 391b2badf02SMaxime Henrion 392b2badf02SMaxime Henrion if (error) 393b2badf02SMaxime Henrion return; 394b2badf02SMaxime Henrion 395b2badf02SMaxime Henrion KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 396b2badf02SMaxime Henrion addr = arg; 397b2badf02SMaxime Henrion *addr = segs->ds_addr; 398b2badf02SMaxime Henrion } 399b2badf02SMaxime Henrion 4006182fdbdSPeter Wemm static int 4016182fdbdSPeter Wemm fxp_attach(device_t dev) 402a17c678eSDavid Greenman { 4036720ebccSMaxime Henrion struct fxp_softc *sc; 4046720ebccSMaxime Henrion struct fxp_cb_tx *tcbp; 4056720ebccSMaxime Henrion struct fxp_tx *txp; 406b2badf02SMaxime Henrion struct fxp_rx *rxp; 4076720ebccSMaxime Henrion struct ifnet *ifp; 40874d1ed23SMaxime Henrion uint32_t val; 40974d1ed23SMaxime Henrion uint16_t data, myea[ETHER_ADDR_LEN / 2]; 410fc74a9f9SBrooks Davis u_char eaddr[ETHER_ADDR_LEN]; 4117137cea0SPyun YongHyeon int i, pmc, prefer_iomap; 4123212724cSJohn Baldwin int error; 413a17c678eSDavid Greenman 4146720ebccSMaxime Henrion error = 0; 4156720ebccSMaxime Henrion sc = device_get_softc(dev); 416f7788e8eSJonathan Lemon sc->dev = dev; 4176008862bSJohn Baldwin mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 4184953bccaSNate Lawson MTX_DEF); 4193212724cSJohn Baldwin callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0); 4204953bccaSNate Lawson ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd, 4214953bccaSNate Lawson fxp_serial_ifmedia_sts); 422a17c678eSDavid Greenman 4237ba33d82SBrooks Davis ifp = sc->ifp = if_alloc(IFT_ETHER); 4247ba33d82SBrooks Davis if (ifp == NULL) { 4257ba33d82SBrooks Davis device_printf(dev, "can not if_alloc()\n"); 4267ba33d82SBrooks Davis error = ENOSPC; 4277ba33d82SBrooks Davis goto fail; 4287ba33d82SBrooks Davis } 4297ba33d82SBrooks Davis 430dfe61cf1SDavid Greenman /* 4312bce79a2SMaxim Sobolev * Enable bus mastering. 432df373873SWes Peters */ 433cf0d8a1eSMaxim Sobolev pci_enable_busmaster(dev); 4349fa6ccfbSMatt Jacob val = pci_read_config(dev, PCIR_COMMAND, 2); 43579495006SWarner Losh 436df373873SWes Peters /* 4379fa6ccfbSMatt Jacob * Figure out which we should try first - memory mapping or i/o mapping? 4389fa6ccfbSMatt Jacob * We default to memory mapping. Then we accept an override from the 4399fa6ccfbSMatt Jacob * command line. Then we check to see which one is enabled. 440dfe61cf1SDavid Greenman */ 4412a05a4ebSMatt Jacob prefer_iomap = 0; 44205bd8c22SMaxime Henrion resource_int_value(device_get_name(dev), device_get_unit(dev), 44305bd8c22SMaxime Henrion "prefer_iomap", &prefer_iomap); 44405bd8c22SMaxime Henrion if (prefer_iomap) 44505bd8c22SMaxime Henrion sc->fxp_spec = fxp_res_spec_io; 44605bd8c22SMaxime Henrion else 44705bd8c22SMaxime Henrion sc->fxp_spec = fxp_res_spec_mem; 4489fa6ccfbSMatt Jacob 44905bd8c22SMaxime Henrion error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res); 45005bd8c22SMaxime Henrion if (error) { 45105bd8c22SMaxime Henrion if (sc->fxp_spec == fxp_res_spec_mem) 45205bd8c22SMaxime Henrion sc->fxp_spec = fxp_res_spec_io; 45305bd8c22SMaxime Henrion else 45405bd8c22SMaxime Henrion sc->fxp_spec = fxp_res_spec_mem; 45505bd8c22SMaxime Henrion error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res); 4569fa6ccfbSMatt Jacob } 45705bd8c22SMaxime Henrion if (error) { 45805bd8c22SMaxime Henrion device_printf(dev, "could not allocate resources\n"); 4596182fdbdSPeter Wemm error = ENXIO; 460a17c678eSDavid Greenman goto fail; 461a17c678eSDavid Greenman } 46205bd8c22SMaxime Henrion 4639fa6ccfbSMatt Jacob if (bootverbose) { 4649fa6ccfbSMatt Jacob device_printf(dev, "using %s space register mapping\n", 46505bd8c22SMaxime Henrion sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O"); 4666182fdbdSPeter Wemm } 4676182fdbdSPeter Wemm 468f7788e8eSJonathan Lemon /* 469f7788e8eSJonathan Lemon * Reset to a stable state. 470f7788e8eSJonathan Lemon */ 471f7788e8eSJonathan Lemon CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET); 472f7788e8eSJonathan Lemon DELAY(10); 473f7788e8eSJonathan Lemon 474f7788e8eSJonathan Lemon /* 475f7788e8eSJonathan Lemon * Find out how large of an SEEPROM we have. 476f7788e8eSJonathan Lemon */ 477f7788e8eSJonathan Lemon fxp_autosize_eeprom(sc); 478f7788e8eSJonathan Lemon 479f7788e8eSJonathan Lemon /* 48093b6e2e6SMaxime Henrion * Find out the chip revision; lump all 82557 revs together. 48193b6e2e6SMaxime Henrion */ 48293b6e2e6SMaxime Henrion fxp_read_eeprom(sc, &data, 5, 1); 48393b6e2e6SMaxime Henrion if ((data >> 8) == 1) 48493b6e2e6SMaxime Henrion sc->revision = FXP_REV_82557; 48593b6e2e6SMaxime Henrion else 48693b6e2e6SMaxime Henrion sc->revision = pci_get_revid(dev); 48793b6e2e6SMaxime Henrion 48893b6e2e6SMaxime Henrion /* 4897137cea0SPyun YongHyeon * Check availability of WOL. 82559ER does not support WOL. 4907137cea0SPyun YongHyeon */ 4917137cea0SPyun YongHyeon if (sc->revision >= FXP_REV_82558_A4 && 4927137cea0SPyun YongHyeon sc->revision != FXP_REV_82559S_A) { 4937137cea0SPyun YongHyeon fxp_read_eeprom(sc, &data, 10, 1); 4947137cea0SPyun YongHyeon if ((data & 0x20) != 0 && 4957137cea0SPyun YongHyeon pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) 4967137cea0SPyun YongHyeon sc->flags |= FXP_FLAG_WOLCAP; 4977137cea0SPyun YongHyeon } 4987137cea0SPyun YongHyeon 4997137cea0SPyun YongHyeon /* 5003bd07cfdSJonathan Lemon * Determine whether we must use the 503 serial interface. 501f7788e8eSJonathan Lemon */ 502f7788e8eSJonathan Lemon fxp_read_eeprom(sc, &data, 6, 1); 50393b6e2e6SMaxime Henrion if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0 5044ed53076SMaxime Henrion && (data & FXP_PHY_SERIAL_ONLY)) 505dedabebfSJonathan Lemon sc->flags |= FXP_FLAG_SERIAL_MEDIA; 506f7788e8eSJonathan Lemon 5070f1db1d6SMaxime Henrion SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 5080f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 50950a33b6aSPawel Jakub Dawidek OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW, 510858b84f5SPoul-Henning Kamp &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I", 51172a32a26SJonathan Lemon "FXP driver receive interrupt microcode bundling delay"); 5120f1db1d6SMaxime Henrion SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 5130f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 51450a33b6aSPawel Jakub Dawidek OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW, 515858b84f5SPoul-Henning Kamp &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I", 51672a32a26SJonathan Lemon "FXP driver receive interrupt microcode bundle size limit"); 5170f1db1d6SMaxime Henrion SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 5180f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 5190f1db1d6SMaxime Henrion OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0, 5200f1db1d6SMaxime Henrion "FXP RNR events"); 5210f1db1d6SMaxime Henrion SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 5220f1db1d6SMaxime Henrion SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 5230f1db1d6SMaxime Henrion OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0, 5240f1db1d6SMaxime Henrion "FXP flow control disabled"); 52572a32a26SJonathan Lemon 52672a32a26SJonathan Lemon /* 52772a32a26SJonathan Lemon * Pull in device tunables. 52872a32a26SJonathan Lemon */ 52972a32a26SJonathan Lemon sc->tunable_int_delay = TUNABLE_INT_DELAY; 53072a32a26SJonathan Lemon sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX; 53103edfff3SRobert Watson sc->tunable_noflow = 1; 53272a32a26SJonathan Lemon (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 53372a32a26SJonathan Lemon "int_delay", &sc->tunable_int_delay); 53472a32a26SJonathan Lemon (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 53572a32a26SJonathan Lemon "bundle_max", &sc->tunable_bundle_max); 5360f1db1d6SMaxime Henrion (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 5370f1db1d6SMaxime Henrion "noflow", &sc->tunable_noflow); 5380f1db1d6SMaxime Henrion sc->rnr = 0; 53972a32a26SJonathan Lemon 54072a32a26SJonathan Lemon /* 5412e2b8238SJonathan Lemon * Enable workarounds for certain chip revision deficiencies. 54200c4116bSJonathan Lemon * 54372a32a26SJonathan Lemon * Systems based on the ICH2/ICH2-M chip from Intel, and possibly 54472a32a26SJonathan Lemon * some systems based a normal 82559 design, have a defect where 54572a32a26SJonathan Lemon * the chip can cause a PCI protocol violation if it receives 54600c4116bSJonathan Lemon * a CU_RESUME command when it is entering the IDLE state. The 54700c4116bSJonathan Lemon * workaround is to disable Dynamic Standby Mode, so the chip never 54800c4116bSJonathan Lemon * deasserts CLKRUN#, and always remains in an active state. 54900c4116bSJonathan Lemon * 55000c4116bSJonathan Lemon * See Intel 82801BA/82801BAM Specification Update, Errata #30. 5512e2b8238SJonathan Lemon */ 5522e2b8238SJonathan Lemon i = pci_get_device(dev); 55372a32a26SJonathan Lemon if (i == 0x2449 || (i > 0x1030 && i < 0x1039) || 55472a32a26SJonathan Lemon sc->revision >= FXP_REV_82559_A0) { 55500c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, 10, 1); 55600c4116bSJonathan Lemon if (data & 0x02) { /* STB enable */ 55774d1ed23SMaxime Henrion uint16_t cksum; 55800c4116bSJonathan Lemon int i; 55900c4116bSJonathan Lemon 56000c4116bSJonathan Lemon device_printf(dev, 561001cfa92SJonathan Lemon "Disabling dynamic standby mode in EEPROM\n"); 56200c4116bSJonathan Lemon data &= ~0x02; 56300c4116bSJonathan Lemon fxp_write_eeprom(sc, &data, 10, 1); 56400c4116bSJonathan Lemon device_printf(dev, "New EEPROM ID: 0x%x\n", data); 56500c4116bSJonathan Lemon cksum = 0; 56600c4116bSJonathan Lemon for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) { 56700c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, i, 1); 56800c4116bSJonathan Lemon cksum += data; 56900c4116bSJonathan Lemon } 57000c4116bSJonathan Lemon i = (1 << sc->eeprom_size) - 1; 57100c4116bSJonathan Lemon cksum = 0xBABA - cksum; 57200c4116bSJonathan Lemon fxp_read_eeprom(sc, &data, i, 1); 57300c4116bSJonathan Lemon fxp_write_eeprom(sc, &cksum, i, 1); 57400c4116bSJonathan Lemon device_printf(dev, 57500c4116bSJonathan Lemon "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n", 57600c4116bSJonathan Lemon i, data, cksum); 57700c4116bSJonathan Lemon #if 1 57800c4116bSJonathan Lemon /* 57900c4116bSJonathan Lemon * If the user elects to continue, try the software 58000c4116bSJonathan Lemon * workaround, as it is better than nothing. 58100c4116bSJonathan Lemon */ 5822e2b8238SJonathan Lemon sc->flags |= FXP_FLAG_CU_RESUME_BUG; 58300c4116bSJonathan Lemon #endif 58400c4116bSJonathan Lemon } 58500c4116bSJonathan Lemon } 5862e2b8238SJonathan Lemon 5872e2b8238SJonathan Lemon /* 5883bd07cfdSJonathan Lemon * If we are not a 82557 chip, we can enable extended features. 5893bd07cfdSJonathan Lemon */ 59072a32a26SJonathan Lemon if (sc->revision != FXP_REV_82557) { 5913bd07cfdSJonathan Lemon /* 59274396a0aSJonathan Lemon * If MWI is enabled in the PCI configuration, and there 59374396a0aSJonathan Lemon * is a valid cacheline size (8 or 16 dwords), then tell 59474396a0aSJonathan Lemon * the board to turn on MWI. 5953bd07cfdSJonathan Lemon */ 59674396a0aSJonathan Lemon if (val & PCIM_CMD_MWRICEN && 59774396a0aSJonathan Lemon pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0) 5983bd07cfdSJonathan Lemon sc->flags |= FXP_FLAG_MWI_ENABLE; 5993bd07cfdSJonathan Lemon 6003bd07cfdSJonathan Lemon /* turn on the extended TxCB feature */ 6013bd07cfdSJonathan Lemon sc->flags |= FXP_FLAG_EXT_TXCB; 60244e0bc11SYaroslav Tykhiy 60344e0bc11SYaroslav Tykhiy /* enable reception of long frames for VLAN */ 60444e0bc11SYaroslav Tykhiy sc->flags |= FXP_FLAG_LONG_PKT_EN; 60544e0bc11SYaroslav Tykhiy } else { 60644e0bc11SYaroslav Tykhiy /* a hack to get long VLAN frames on a 82557 */ 60744e0bc11SYaroslav Tykhiy sc->flags |= FXP_FLAG_SAVE_BAD; 6083bd07cfdSJonathan Lemon } 6093bd07cfdSJonathan Lemon 610f13075afSPyun YongHyeon /* For 82559 or later chips, Rx checksum offload is supported. */ 611f13075afSPyun YongHyeon if (sc->revision >= FXP_REV_82559_A0) 612f13075afSPyun YongHyeon sc->flags |= FXP_FLAG_82559_RXCSUM; 6133bd07cfdSJonathan Lemon /* 614c8bca6dcSBill Paul * Enable use of extended RFDs and TCBs for 82550 615c8bca6dcSBill Paul * and later chips. Note: we need extended TXCB support 616c8bca6dcSBill Paul * too, but that's already enabled by the code above. 617c8bca6dcSBill Paul * Be careful to do this only on the right devices. 618c8bca6dcSBill Paul */ 619507feeafSMaxime Henrion if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C || 620507feeafSMaxime Henrion sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F 621507feeafSMaxime Henrion || sc->revision == FXP_REV_82551_10) { 622c8bca6dcSBill Paul sc->rfa_size = sizeof (struct fxp_rfa); 623c8bca6dcSBill Paul sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT; 624c8bca6dcSBill Paul sc->flags |= FXP_FLAG_EXT_RFA; 625f13075afSPyun YongHyeon /* Use extended RFA instead of 82559 checksum mode. */ 626f13075afSPyun YongHyeon sc->flags &= ~FXP_FLAG_82559_RXCSUM; 627c8bca6dcSBill Paul } else { 628c8bca6dcSBill Paul sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN; 629c8bca6dcSBill Paul sc->tx_cmd = FXP_CB_COMMAND_XMIT; 630c8bca6dcSBill Paul } 631c8bca6dcSBill Paul 632c8bca6dcSBill Paul /* 633b2badf02SMaxime Henrion * Allocate DMA tags and DMA safe memory. 634b2badf02SMaxime Henrion */ 63540c20505SMaxime Henrion sc->maxtxseg = FXP_NTXSEG; 636c21e84e4SPyun YongHyeon sc->maxsegsize = MCLBYTES; 637c21e84e4SPyun YongHyeon if (sc->flags & FXP_FLAG_EXT_RFA) { 63840c20505SMaxime Henrion sc->maxtxseg--; 639c21e84e4SPyun YongHyeon sc->maxsegsize = FXP_TSO_SEGSIZE; 640c21e84e4SPyun YongHyeon } 641c2175ff5SMarius Strobl error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0, 642c2175ff5SMarius Strobl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 643c21e84e4SPyun YongHyeon sc->maxsegsize * sc->maxtxseg + sizeof(struct ether_vlan_header), 644c21e84e4SPyun YongHyeon sc->maxtxseg, sc->maxsegsize, 0, 645a2057a72SPyun YongHyeon busdma_lock_mutex, &Giant, &sc->fxp_txmtag); 646b2badf02SMaxime Henrion if (error) { 647a2057a72SPyun YongHyeon device_printf(dev, "could not create TX DMA tag\n"); 648a2057a72SPyun YongHyeon goto fail; 649a2057a72SPyun YongHyeon } 650a2057a72SPyun YongHyeon 651a2057a72SPyun YongHyeon error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0, 652a2057a72SPyun YongHyeon BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 653a2057a72SPyun YongHyeon MCLBYTES, 1, MCLBYTES, 0, 654a2057a72SPyun YongHyeon busdma_lock_mutex, &Giant, &sc->fxp_rxmtag); 655a2057a72SPyun YongHyeon if (error) { 656a2057a72SPyun YongHyeon device_printf(dev, "could not create RX DMA tag\n"); 657b2badf02SMaxime Henrion goto fail; 658b2badf02SMaxime Henrion } 659b2badf02SMaxime Henrion 660c2175ff5SMarius Strobl error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0, 661c2175ff5SMarius Strobl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 662c2175ff5SMarius Strobl sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0, 663c2175ff5SMarius Strobl busdma_lock_mutex, &Giant, &sc->fxp_stag); 664b2badf02SMaxime Henrion if (error) { 665a2057a72SPyun YongHyeon device_printf(dev, "could not create stats DMA tag\n"); 666b2badf02SMaxime Henrion goto fail; 667b2badf02SMaxime Henrion } 668b2badf02SMaxime Henrion 669b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats, 670aafb3ebbSMaxime Henrion BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap); 671a2057a72SPyun YongHyeon if (error) { 672a2057a72SPyun YongHyeon device_printf(dev, "could not allocate stats DMA memory\n"); 6734953bccaSNate Lawson goto fail; 674a2057a72SPyun YongHyeon } 675b2badf02SMaxime Henrion error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats, 676b2badf02SMaxime Henrion sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0); 677b2badf02SMaxime Henrion if (error) { 678a2057a72SPyun YongHyeon device_printf(dev, "could not load the stats DMA buffer\n"); 679b2badf02SMaxime Henrion goto fail; 680b2badf02SMaxime Henrion } 681b2badf02SMaxime Henrion 682c2175ff5SMarius Strobl error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0, 683c2175ff5SMarius Strobl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 684c2175ff5SMarius Strobl FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0, 685c2175ff5SMarius Strobl busdma_lock_mutex, &Giant, &sc->cbl_tag); 686b2badf02SMaxime Henrion if (error) { 687a2057a72SPyun YongHyeon device_printf(dev, "could not create TxCB DMA tag\n"); 688b2badf02SMaxime Henrion goto fail; 689b2badf02SMaxime Henrion } 690b2badf02SMaxime Henrion 691b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list, 692aafb3ebbSMaxime Henrion BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map); 693a2057a72SPyun YongHyeon if (error) { 694a2057a72SPyun YongHyeon device_printf(dev, "could not allocate TxCB DMA memory\n"); 6954953bccaSNate Lawson goto fail; 696a2057a72SPyun YongHyeon } 697b2badf02SMaxime Henrion 698b2badf02SMaxime Henrion error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map, 699b2badf02SMaxime Henrion sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr, 700b2badf02SMaxime Henrion &sc->fxp_desc.cbl_addr, 0); 701b2badf02SMaxime Henrion if (error) { 702a2057a72SPyun YongHyeon device_printf(dev, "could not load TxCB DMA buffer\n"); 703b2badf02SMaxime Henrion goto fail; 704b2badf02SMaxime Henrion } 705b2badf02SMaxime Henrion 706c2175ff5SMarius Strobl error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0, 707c2175ff5SMarius Strobl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 708c2175ff5SMarius Strobl sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0, 709c2175ff5SMarius Strobl busdma_lock_mutex, &Giant, &sc->mcs_tag); 710b2badf02SMaxime Henrion if (error) { 711a2057a72SPyun YongHyeon device_printf(dev, 712a2057a72SPyun YongHyeon "could not create multicast setup DMA tag\n"); 713b2badf02SMaxime Henrion goto fail; 714b2badf02SMaxime Henrion } 715b2badf02SMaxime Henrion 716b2badf02SMaxime Henrion error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp, 717a2057a72SPyun YongHyeon BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->mcs_map); 718a2057a72SPyun YongHyeon if (error) { 719a2057a72SPyun YongHyeon device_printf(dev, 720a2057a72SPyun YongHyeon "could not allocate multicast setup DMA memory\n"); 7214953bccaSNate Lawson goto fail; 722a2057a72SPyun YongHyeon } 723b2badf02SMaxime Henrion error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp, 724b2badf02SMaxime Henrion sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0); 725b2badf02SMaxime Henrion if (error) { 726a2057a72SPyun YongHyeon device_printf(dev, 727a2057a72SPyun YongHyeon "can't load the multicast setup DMA buffer\n"); 728b2badf02SMaxime Henrion goto fail; 729b2badf02SMaxime Henrion } 730b2badf02SMaxime Henrion 731b2badf02SMaxime Henrion /* 7326720ebccSMaxime Henrion * Pre-allocate the TX DMA maps and setup the pointers to 7336720ebccSMaxime Henrion * the TX command blocks. 734b2badf02SMaxime Henrion */ 7356720ebccSMaxime Henrion txp = sc->fxp_desc.tx_list; 7366720ebccSMaxime Henrion tcbp = sc->fxp_desc.cbl_list; 7374cec1653SMaxime Henrion for (i = 0; i < FXP_NTXCB; i++) { 7386720ebccSMaxime Henrion txp[i].tx_cb = tcbp + i; 739a2057a72SPyun YongHyeon error = bus_dmamap_create(sc->fxp_txmtag, 0, &txp[i].tx_map); 740b2badf02SMaxime Henrion if (error) { 741b2badf02SMaxime Henrion device_printf(dev, "can't create DMA map for TX\n"); 742b2badf02SMaxime Henrion goto fail; 743b2badf02SMaxime Henrion } 744b2badf02SMaxime Henrion } 745a2057a72SPyun YongHyeon error = bus_dmamap_create(sc->fxp_rxmtag, 0, &sc->spare_map); 746b2badf02SMaxime Henrion if (error) { 747b2badf02SMaxime Henrion device_printf(dev, "can't create spare DMA map\n"); 748b2badf02SMaxime Henrion goto fail; 749b2badf02SMaxime Henrion } 750b2badf02SMaxime Henrion 751b2badf02SMaxime Henrion /* 752b2badf02SMaxime Henrion * Pre-allocate our receive buffers. 753b2badf02SMaxime Henrion */ 754b2badf02SMaxime Henrion sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL; 755b2badf02SMaxime Henrion for (i = 0; i < FXP_NRFABUFS; i++) { 756b2badf02SMaxime Henrion rxp = &sc->fxp_desc.rx_list[i]; 757a2057a72SPyun YongHyeon error = bus_dmamap_create(sc->fxp_rxmtag, 0, &rxp->rx_map); 758b2badf02SMaxime Henrion if (error) { 759b2badf02SMaxime Henrion device_printf(dev, "can't create DMA map for RX\n"); 760b2badf02SMaxime Henrion goto fail; 761b2badf02SMaxime Henrion } 76285050421SPyun YongHyeon if (fxp_new_rfabuf(sc, rxp) != 0) { 7634953bccaSNate Lawson error = ENOMEM; 7644953bccaSNate Lawson goto fail; 7654953bccaSNate Lawson } 76685050421SPyun YongHyeon fxp_add_rfabuf(sc, rxp); 767b2badf02SMaxime Henrion } 768b2badf02SMaxime Henrion 769b2badf02SMaxime Henrion /* 770f7788e8eSJonathan Lemon * Read MAC address. 771f7788e8eSJonathan Lemon */ 77283e6547dSMaxime Henrion fxp_read_eeprom(sc, myea, 0, 3); 773fc74a9f9SBrooks Davis eaddr[0] = myea[0] & 0xff; 774fc74a9f9SBrooks Davis eaddr[1] = myea[0] >> 8; 775fc74a9f9SBrooks Davis eaddr[2] = myea[1] & 0xff; 776fc74a9f9SBrooks Davis eaddr[3] = myea[1] >> 8; 777fc74a9f9SBrooks Davis eaddr[4] = myea[2] & 0xff; 778fc74a9f9SBrooks Davis eaddr[5] = myea[2] >> 8; 779f7788e8eSJonathan Lemon if (bootverbose) { 7802e2b8238SJonathan Lemon device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n", 781f7788e8eSJonathan Lemon pci_get_vendor(dev), pci_get_device(dev), 7822e2b8238SJonathan Lemon pci_get_subvendor(dev), pci_get_subdevice(dev), 7832e2b8238SJonathan Lemon pci_get_revid(dev)); 78472a32a26SJonathan Lemon fxp_read_eeprom(sc, &data, 10, 1); 78572a32a26SJonathan Lemon device_printf(dev, "Dynamic Standby mode is %s\n", 78672a32a26SJonathan Lemon data & 0x02 ? "enabled" : "disabled"); 787f7788e8eSJonathan Lemon } 788f7788e8eSJonathan Lemon 789f7788e8eSJonathan Lemon /* 790f7788e8eSJonathan Lemon * If this is only a 10Mbps device, then there is no MII, and 791f7788e8eSJonathan Lemon * the PHY will use a serial interface instead. 792f7788e8eSJonathan Lemon * 793f7788e8eSJonathan Lemon * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter 794f7788e8eSJonathan Lemon * doesn't have a programming interface of any sort. The 795f7788e8eSJonathan Lemon * media is sensed automatically based on how the link partner 796f7788e8eSJonathan Lemon * is configured. This is, in essence, manual configuration. 797f7788e8eSJonathan Lemon */ 798f7788e8eSJonathan Lemon if (sc->flags & FXP_FLAG_SERIAL_MEDIA) { 799f7788e8eSJonathan Lemon ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 800f7788e8eSJonathan Lemon ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL); 801f7788e8eSJonathan Lemon } else { 802f7788e8eSJonathan Lemon if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd, 803f7788e8eSJonathan Lemon fxp_ifmedia_sts)) { 804f7788e8eSJonathan Lemon device_printf(dev, "MII without any PHY!\n"); 8056182fdbdSPeter Wemm error = ENXIO; 806ba8c6fd5SDavid Greenman goto fail; 807a17c678eSDavid Greenman } 808f7788e8eSJonathan Lemon } 809dccee1a1SDavid Greenman 8109bf40edeSBrooks Davis if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 811fb583156SDavid Greenman ifp->if_init = fxp_init; 812ba8c6fd5SDavid Greenman ifp->if_softc = sc; 813ba8c6fd5SDavid Greenman ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 814ba8c6fd5SDavid Greenman ifp->if_ioctl = fxp_ioctl; 815ba8c6fd5SDavid Greenman ifp->if_start = fxp_start; 816a17c678eSDavid Greenman 8175fe9116bSYaroslav Tykhiy ifp->if_capabilities = ifp->if_capenable = 0; 8185fe9116bSYaroslav Tykhiy 819c21e84e4SPyun YongHyeon /* Enable checksum offload/TSO for 82550 or better chips */ 820c8bca6dcSBill Paul if (sc->flags & FXP_FLAG_EXT_RFA) { 821c21e84e4SPyun YongHyeon ifp->if_hwassist = FXP_CSUM_FEATURES | CSUM_TSO; 822c21e84e4SPyun YongHyeon ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSO4; 823c21e84e4SPyun YongHyeon ifp->if_capenable |= IFCAP_HWCSUM | IFCAP_TSO4; 824c8bca6dcSBill Paul } 825c8bca6dcSBill Paul 826f13075afSPyun YongHyeon if (sc->flags & FXP_FLAG_82559_RXCSUM) { 827f13075afSPyun YongHyeon ifp->if_capabilities |= IFCAP_RXCSUM; 828f13075afSPyun YongHyeon ifp->if_capenable |= IFCAP_RXCSUM; 829f13075afSPyun YongHyeon } 830f13075afSPyun YongHyeon 8317137cea0SPyun YongHyeon if (sc->flags & FXP_FLAG_WOLCAP) { 8327137cea0SPyun YongHyeon ifp->if_capabilities |= IFCAP_WOL_MAGIC; 8337137cea0SPyun YongHyeon ifp->if_capenable |= IFCAP_WOL_MAGIC; 8347137cea0SPyun YongHyeon } 8357137cea0SPyun YongHyeon 836fb917226SRuslan Ermilov #ifdef DEVICE_POLLING 837fb917226SRuslan Ermilov /* Inform the world we support polling. */ 838fb917226SRuslan Ermilov ifp->if_capabilities |= IFCAP_POLLING; 839fb917226SRuslan Ermilov #endif 840fb917226SRuslan Ermilov 841dfe61cf1SDavid Greenman /* 8424953bccaSNate Lawson * Attach the interface. 8434953bccaSNate Lawson */ 844fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 8454953bccaSNate Lawson 8464953bccaSNate Lawson /* 847e8c8b728SJonathan Lemon * Tell the upper layer(s) we support long frames. 8485fe9116bSYaroslav Tykhiy * Must appear after the call to ether_ifattach() because 8495fe9116bSYaroslav Tykhiy * ether_ifattach() sets ifi_hdrlen to the default value. 850e8c8b728SJonathan Lemon */ 851e8c8b728SJonathan Lemon ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 852673d9191SSam Leffler ifp->if_capabilities |= IFCAP_VLAN_MTU; 85344e0bc11SYaroslav Tykhiy ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */ 854bd4fa9d9SPyun YongHyeon if ((sc->flags & FXP_FLAG_EXT_RFA) != 0) { 855bd4fa9d9SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | 856bd4fa9d9SPyun YongHyeon IFCAP_VLAN_HWCSUM; 857bd4fa9d9SPyun YongHyeon ifp->if_capenable |= IFCAP_VLAN_HWTAGGING | 858bd4fa9d9SPyun YongHyeon IFCAP_VLAN_HWCSUM; 859bd4fa9d9SPyun YongHyeon } 860e8c8b728SJonathan Lemon 861483b9871SDavid Greenman /* 8623114fdb4SDavid Greenman * Let the system queue as many packets as we have available 8633114fdb4SDavid Greenman * TX descriptors. 864483b9871SDavid Greenman */ 8657929aa03SMax Laier IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1); 8667929aa03SMax Laier ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1; 8677929aa03SMax Laier IFQ_SET_READY(&ifp->if_snd); 8684a684684SDavid Greenman 869201afb0eSMaxime Henrion /* 8704953bccaSNate Lawson * Hook our interrupt after all initialization is complete. 871201afb0eSMaxime Henrion */ 87205bd8c22SMaxime Henrion error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE, 873ef544f63SPaolo Pisati NULL, fxp_intr, sc, &sc->ih); 874201afb0eSMaxime Henrion if (error) { 875201afb0eSMaxime Henrion device_printf(dev, "could not setup irq\n"); 876fc74a9f9SBrooks Davis ether_ifdetach(sc->ifp); 877201afb0eSMaxime Henrion goto fail; 878201afb0eSMaxime Henrion } 879201afb0eSMaxime Henrion 8807137cea0SPyun YongHyeon /* 8817137cea0SPyun YongHyeon * Configure hardware to reject magic frames otherwise 8827137cea0SPyun YongHyeon * system will hang on recipt of magic frames. 8837137cea0SPyun YongHyeon */ 8847137cea0SPyun YongHyeon if ((sc->flags & FXP_FLAG_WOLCAP) != 0) { 8857137cea0SPyun YongHyeon FXP_LOCK(sc); 8867137cea0SPyun YongHyeon /* Clear wakeup events. */ 887af75b654SPyun YongHyeon CSR_WRITE_1(sc, FXP_CSR_PMDR, CSR_READ_1(sc, FXP_CSR_PMDR)); 8887137cea0SPyun YongHyeon fxp_init_body(sc); 8897137cea0SPyun YongHyeon fxp_stop(sc); 8907137cea0SPyun YongHyeon FXP_UNLOCK(sc); 8917137cea0SPyun YongHyeon } 8927137cea0SPyun YongHyeon 893a17c678eSDavid Greenman fail: 8941b5a39d3SBrooks Davis if (error) 895f7788e8eSJonathan Lemon fxp_release(sc); 896f7788e8eSJonathan Lemon return (error); 897f7788e8eSJonathan Lemon } 898f7788e8eSJonathan Lemon 899f7788e8eSJonathan Lemon /* 9004953bccaSNate Lawson * Release all resources. The softc lock should not be held and the 9014953bccaSNate Lawson * interrupt should already be torn down. 902f7788e8eSJonathan Lemon */ 903f7788e8eSJonathan Lemon static void 904f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc) 905f7788e8eSJonathan Lemon { 906b2badf02SMaxime Henrion struct fxp_rx *rxp; 907b2badf02SMaxime Henrion struct fxp_tx *txp; 908b2badf02SMaxime Henrion int i; 909b2badf02SMaxime Henrion 91067fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_NOTOWNED); 911670f5d73SMaxime Henrion KASSERT(sc->ih == NULL, 912670f5d73SMaxime Henrion ("fxp_release() called with intr handle still active")); 9134953bccaSNate Lawson if (sc->miibus) 9144953bccaSNate Lawson device_delete_child(sc->dev, sc->miibus); 9154953bccaSNate Lawson bus_generic_detach(sc->dev); 9164953bccaSNate Lawson ifmedia_removeall(&sc->sc_media); 917b2badf02SMaxime Henrion if (sc->fxp_desc.cbl_list) { 918b2badf02SMaxime Henrion bus_dmamap_unload(sc->cbl_tag, sc->cbl_map); 919b2badf02SMaxime Henrion bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list, 920b2badf02SMaxime Henrion sc->cbl_map); 921b2badf02SMaxime Henrion } 922b2badf02SMaxime Henrion if (sc->fxp_stats) { 923b2badf02SMaxime Henrion bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap); 924b2badf02SMaxime Henrion bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap); 925b2badf02SMaxime Henrion } 926b2badf02SMaxime Henrion if (sc->mcsp) { 927b2badf02SMaxime Henrion bus_dmamap_unload(sc->mcs_tag, sc->mcs_map); 928b2badf02SMaxime Henrion bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map); 929b2badf02SMaxime Henrion } 93005bd8c22SMaxime Henrion bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res); 931a2057a72SPyun YongHyeon if (sc->fxp_rxmtag) { 932b983c7b3SMaxime Henrion for (i = 0; i < FXP_NRFABUFS; i++) { 933b983c7b3SMaxime Henrion rxp = &sc->fxp_desc.rx_list[i]; 934b983c7b3SMaxime Henrion if (rxp->rx_mbuf != NULL) { 935a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map, 936b983c7b3SMaxime Henrion BUS_DMASYNC_POSTREAD); 937a2057a72SPyun YongHyeon bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map); 938b983c7b3SMaxime Henrion m_freem(rxp->rx_mbuf); 939b983c7b3SMaxime Henrion } 940a2057a72SPyun YongHyeon bus_dmamap_destroy(sc->fxp_rxmtag, rxp->rx_map); 941b983c7b3SMaxime Henrion } 942a2057a72SPyun YongHyeon bus_dmamap_destroy(sc->fxp_rxmtag, sc->spare_map); 943a2057a72SPyun YongHyeon bus_dma_tag_destroy(sc->fxp_rxmtag); 944a2057a72SPyun YongHyeon } 945a2057a72SPyun YongHyeon if (sc->fxp_txmtag) { 946b983c7b3SMaxime Henrion for (i = 0; i < FXP_NTXCB; i++) { 947b983c7b3SMaxime Henrion txp = &sc->fxp_desc.tx_list[i]; 948b983c7b3SMaxime Henrion if (txp->tx_mbuf != NULL) { 949a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map, 950b983c7b3SMaxime Henrion BUS_DMASYNC_POSTWRITE); 951a2057a72SPyun YongHyeon bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map); 952b983c7b3SMaxime Henrion m_freem(txp->tx_mbuf); 953b983c7b3SMaxime Henrion } 954a2057a72SPyun YongHyeon bus_dmamap_destroy(sc->fxp_txmtag, txp->tx_map); 955b983c7b3SMaxime Henrion } 956a2057a72SPyun YongHyeon bus_dma_tag_destroy(sc->fxp_txmtag); 957b983c7b3SMaxime Henrion } 958c4bf1e90SMaxime Henrion if (sc->fxp_stag) 959c4bf1e90SMaxime Henrion bus_dma_tag_destroy(sc->fxp_stag); 960b2badf02SMaxime Henrion if (sc->cbl_tag) 961b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->cbl_tag); 962b2badf02SMaxime Henrion if (sc->mcs_tag) 963b2badf02SMaxime Henrion bus_dma_tag_destroy(sc->mcs_tag); 964fc74a9f9SBrooks Davis if (sc->ifp) 965fc74a9f9SBrooks Davis if_free(sc->ifp); 96672a32a26SJonathan Lemon 9670f4dc94cSChuck Paterson mtx_destroy(&sc->sc_mtx); 9686182fdbdSPeter Wemm } 9696182fdbdSPeter Wemm 9706182fdbdSPeter Wemm /* 9716182fdbdSPeter Wemm * Detach interface. 9726182fdbdSPeter Wemm */ 9736182fdbdSPeter Wemm static int 9746182fdbdSPeter Wemm fxp_detach(device_t dev) 9756182fdbdSPeter Wemm { 9766182fdbdSPeter Wemm struct fxp_softc *sc = device_get_softc(dev); 9776182fdbdSPeter Wemm 97840929967SGleb Smirnoff #ifdef DEVICE_POLLING 97940929967SGleb Smirnoff if (sc->ifp->if_capenable & IFCAP_POLLING) 98040929967SGleb Smirnoff ether_poll_deregister(sc->ifp); 98140929967SGleb Smirnoff #endif 98240929967SGleb Smirnoff 9834953bccaSNate Lawson FXP_LOCK(sc); 9841d2945d5SWarner Losh sc->suspended = 1; /* Do same thing as we do for suspend */ 9856182fdbdSPeter Wemm /* 98632cd7a9cSWarner Losh * Stop DMA and drop transmit queue, but disable interrupts first. 98720f0c80fSMaxime Henrion */ 98820f0c80fSMaxime Henrion CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 98920f0c80fSMaxime Henrion fxp_stop(sc); 99032cd7a9cSWarner Losh FXP_UNLOCK(sc); 9919eda9d7aSJohn Baldwin callout_drain(&sc->stat_ch); 99220f0c80fSMaxime Henrion 9936182fdbdSPeter Wemm /* 9943212724cSJohn Baldwin * Close down routes etc. 9953212724cSJohn Baldwin */ 9963212724cSJohn Baldwin ether_ifdetach(sc->ifp); 9973212724cSJohn Baldwin 9983212724cSJohn Baldwin /* 9994953bccaSNate Lawson * Unhook interrupt before dropping lock. This is to prevent 10004953bccaSNate Lawson * races with fxp_intr(). 10016182fdbdSPeter Wemm */ 100205bd8c22SMaxime Henrion bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih); 10034953bccaSNate Lawson sc->ih = NULL; 10046182fdbdSPeter Wemm 1005f7788e8eSJonathan Lemon /* Release our allocated resources. */ 1006f7788e8eSJonathan Lemon fxp_release(sc); 1007f7788e8eSJonathan Lemon return (0); 1008a17c678eSDavid Greenman } 1009a17c678eSDavid Greenman 1010a17c678eSDavid Greenman /* 10114a684684SDavid Greenman * Device shutdown routine. Called at system shutdown after sync. The 1012a17c678eSDavid Greenman * main purpose of this routine is to shut off receiver DMA so that 1013a17c678eSDavid Greenman * kernel memory doesn't get clobbered during warmboot. 1014a17c678eSDavid Greenman */ 10156182fdbdSPeter Wemm static int 10166182fdbdSPeter Wemm fxp_shutdown(device_t dev) 1017a17c678eSDavid Greenman { 10183212724cSJohn Baldwin 10196182fdbdSPeter Wemm /* 10206182fdbdSPeter Wemm * Make sure that DMA is disabled prior to reboot. Not doing 10216182fdbdSPeter Wemm * do could allow DMA to corrupt kernel memory during the 10226182fdbdSPeter Wemm * reboot before the driver initializes. 10236182fdbdSPeter Wemm */ 10247137cea0SPyun YongHyeon return (fxp_suspend(dev)); 1025a17c678eSDavid Greenman } 1026a17c678eSDavid Greenman 10277dced78aSDavid Greenman /* 10287dced78aSDavid Greenman * Device suspend routine. Stop the interface and save some PCI 10297dced78aSDavid Greenman * settings in case the BIOS doesn't restore them properly on 10307dced78aSDavid Greenman * resume. 10317dced78aSDavid Greenman */ 10327dced78aSDavid Greenman static int 10337dced78aSDavid Greenman fxp_suspend(device_t dev) 10347dced78aSDavid Greenman { 10357dced78aSDavid Greenman struct fxp_softc *sc = device_get_softc(dev); 10367137cea0SPyun YongHyeon struct ifnet *ifp; 10377137cea0SPyun YongHyeon int pmc; 10387137cea0SPyun YongHyeon uint16_t pmstat; 10397dced78aSDavid Greenman 10404953bccaSNate Lawson FXP_LOCK(sc); 10417dced78aSDavid Greenman 10427137cea0SPyun YongHyeon ifp = sc->ifp; 10437137cea0SPyun YongHyeon if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) { 10447137cea0SPyun YongHyeon pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2); 10457137cea0SPyun YongHyeon pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE); 10467137cea0SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) { 10477137cea0SPyun YongHyeon /* Request PME. */ 10487137cea0SPyun YongHyeon pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE; 10497137cea0SPyun YongHyeon sc->flags |= FXP_FLAG_WOL; 10507137cea0SPyun YongHyeon /* Reconfigure hardware to accept magic frames. */ 10517137cea0SPyun YongHyeon fxp_init_body(sc); 10527137cea0SPyun YongHyeon } 10537137cea0SPyun YongHyeon pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2); 10547137cea0SPyun YongHyeon } 10557dced78aSDavid Greenman fxp_stop(sc); 10567dced78aSDavid Greenman 10577dced78aSDavid Greenman sc->suspended = 1; 10587dced78aSDavid Greenman 10594953bccaSNate Lawson FXP_UNLOCK(sc); 1060f7788e8eSJonathan Lemon return (0); 10617dced78aSDavid Greenman } 10627dced78aSDavid Greenman 10637dced78aSDavid Greenman /* 106467ba6566SWarner Losh * Device resume routine. re-enable busmastering, and restart the interface if 10657dced78aSDavid Greenman * appropriate. 10667dced78aSDavid Greenman */ 10677dced78aSDavid Greenman static int 10687dced78aSDavid Greenman fxp_resume(device_t dev) 10697dced78aSDavid Greenman { 10707dced78aSDavid Greenman struct fxp_softc *sc = device_get_softc(dev); 1071fc74a9f9SBrooks Davis struct ifnet *ifp = sc->ifp; 10727137cea0SPyun YongHyeon int pmc; 10737137cea0SPyun YongHyeon uint16_t pmstat; 10747dced78aSDavid Greenman 10754953bccaSNate Lawson FXP_LOCK(sc); 10767dced78aSDavid Greenman 10777137cea0SPyun YongHyeon if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) { 10787137cea0SPyun YongHyeon sc->flags &= ~FXP_FLAG_WOL; 10797137cea0SPyun YongHyeon pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2); 10807137cea0SPyun YongHyeon /* Disable PME and clear PME status. */ 10817137cea0SPyun YongHyeon pmstat &= ~PCIM_PSTAT_PMEENABLE; 10827137cea0SPyun YongHyeon pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2); 1083af75b654SPyun YongHyeon if ((sc->flags & FXP_FLAG_WOLCAP) != 0) 1084af75b654SPyun YongHyeon CSR_WRITE_1(sc, FXP_CSR_PMDR, 1085af75b654SPyun YongHyeon CSR_READ_1(sc, FXP_CSR_PMDR)); 10867137cea0SPyun YongHyeon } 10877137cea0SPyun YongHyeon 10887dced78aSDavid Greenman CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET); 10897dced78aSDavid Greenman DELAY(10); 10907dced78aSDavid Greenman 10917dced78aSDavid Greenman /* reinitialize interface if necessary */ 10927dced78aSDavid Greenman if (ifp->if_flags & IFF_UP) 10934953bccaSNate Lawson fxp_init_body(sc); 10947dced78aSDavid Greenman 10957dced78aSDavid Greenman sc->suspended = 0; 10967dced78aSDavid Greenman 10974953bccaSNate Lawson FXP_UNLOCK(sc); 1098ba8c6fd5SDavid Greenman return (0); 1099f7788e8eSJonathan Lemon } 1100ba8c6fd5SDavid Greenman 110100c4116bSJonathan Lemon static void 110200c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length) 110300c4116bSJonathan Lemon { 110474d1ed23SMaxime Henrion uint16_t reg; 110500c4116bSJonathan Lemon int x; 110600c4116bSJonathan Lemon 110700c4116bSJonathan Lemon /* 110800c4116bSJonathan Lemon * Shift in data. 110900c4116bSJonathan Lemon */ 111000c4116bSJonathan Lemon for (x = 1 << (length - 1); x; x >>= 1) { 111100c4116bSJonathan Lemon if (data & x) 111200c4116bSJonathan Lemon reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI; 111300c4116bSJonathan Lemon else 111400c4116bSJonathan Lemon reg = FXP_EEPROM_EECS; 111500c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 111600c4116bSJonathan Lemon DELAY(1); 111700c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 111800c4116bSJonathan Lemon DELAY(1); 111900c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 112000c4116bSJonathan Lemon DELAY(1); 112100c4116bSJonathan Lemon } 112200c4116bSJonathan Lemon } 112300c4116bSJonathan Lemon 1124f7788e8eSJonathan Lemon /* 1125f7788e8eSJonathan Lemon * Read from the serial EEPROM. Basically, you manually shift in 1126f7788e8eSJonathan Lemon * the read opcode (one bit at a time) and then shift in the address, 1127f7788e8eSJonathan Lemon * and then you shift out the data (all of this one bit at a time). 1128f7788e8eSJonathan Lemon * The word size is 16 bits, so you have to provide the address for 1129f7788e8eSJonathan Lemon * every 16 bits of data. 1130f7788e8eSJonathan Lemon */ 113174d1ed23SMaxime Henrion static uint16_t 1132f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize) 1133f7788e8eSJonathan Lemon { 113474d1ed23SMaxime Henrion uint16_t reg, data; 1135f7788e8eSJonathan Lemon int x; 1136ba8c6fd5SDavid Greenman 1137f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 1138f7788e8eSJonathan Lemon /* 1139f7788e8eSJonathan Lemon * Shift in read opcode. 1140f7788e8eSJonathan Lemon */ 114100c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3); 1142f7788e8eSJonathan Lemon /* 1143f7788e8eSJonathan Lemon * Shift in address. 1144f7788e8eSJonathan Lemon */ 1145f7788e8eSJonathan Lemon data = 0; 1146f7788e8eSJonathan Lemon for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) { 1147f7788e8eSJonathan Lemon if (offset & x) 1148f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI; 1149f7788e8eSJonathan Lemon else 1150f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS; 1151f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1152f7788e8eSJonathan Lemon DELAY(1); 1153f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 1154f7788e8eSJonathan Lemon DELAY(1); 1155f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1156f7788e8eSJonathan Lemon DELAY(1); 1157f7788e8eSJonathan Lemon reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO; 1158f7788e8eSJonathan Lemon data++; 1159f7788e8eSJonathan Lemon if (autosize && reg == 0) { 1160f7788e8eSJonathan Lemon sc->eeprom_size = data; 1161f7788e8eSJonathan Lemon break; 1162f7788e8eSJonathan Lemon } 1163f7788e8eSJonathan Lemon } 1164f7788e8eSJonathan Lemon /* 1165f7788e8eSJonathan Lemon * Shift out data. 1166f7788e8eSJonathan Lemon */ 1167f7788e8eSJonathan Lemon data = 0; 1168f7788e8eSJonathan Lemon reg = FXP_EEPROM_EECS; 1169f7788e8eSJonathan Lemon for (x = 1 << 15; x; x >>= 1) { 1170f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK); 1171f7788e8eSJonathan Lemon DELAY(1); 1172f7788e8eSJonathan Lemon if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) 1173f7788e8eSJonathan Lemon data |= x; 1174f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg); 1175f7788e8eSJonathan Lemon DELAY(1); 1176f7788e8eSJonathan Lemon } 1177f7788e8eSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 1178f7788e8eSJonathan Lemon DELAY(1); 1179f7788e8eSJonathan Lemon 1180f7788e8eSJonathan Lemon return (data); 1181ba8c6fd5SDavid Greenman } 1182ba8c6fd5SDavid Greenman 118300c4116bSJonathan Lemon static void 118474d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data) 118500c4116bSJonathan Lemon { 118600c4116bSJonathan Lemon int i; 118700c4116bSJonathan Lemon 118800c4116bSJonathan Lemon /* 118900c4116bSJonathan Lemon * Erase/write enable. 119000c4116bSJonathan Lemon */ 119100c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 119200c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x4, 3); 119300c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size); 119400c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 119500c4116bSJonathan Lemon DELAY(1); 119600c4116bSJonathan Lemon /* 119700c4116bSJonathan Lemon * Shift in write opcode, address, data. 119800c4116bSJonathan Lemon */ 119900c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 120000c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3); 120100c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, offset, sc->eeprom_size); 120200c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, data, 16); 120300c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 120400c4116bSJonathan Lemon DELAY(1); 120500c4116bSJonathan Lemon /* 120600c4116bSJonathan Lemon * Wait for EEPROM to finish up. 120700c4116bSJonathan Lemon */ 120800c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 120900c4116bSJonathan Lemon DELAY(1); 121000c4116bSJonathan Lemon for (i = 0; i < 1000; i++) { 121100c4116bSJonathan Lemon if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) 121200c4116bSJonathan Lemon break; 121300c4116bSJonathan Lemon DELAY(50); 121400c4116bSJonathan Lemon } 121500c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 121600c4116bSJonathan Lemon DELAY(1); 121700c4116bSJonathan Lemon /* 121800c4116bSJonathan Lemon * Erase/write disable. 121900c4116bSJonathan Lemon */ 122000c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS); 122100c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0x4, 3); 122200c4116bSJonathan Lemon fxp_eeprom_shiftin(sc, 0, sc->eeprom_size); 122300c4116bSJonathan Lemon CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0); 122400c4116bSJonathan Lemon DELAY(1); 122500c4116bSJonathan Lemon } 122600c4116bSJonathan Lemon 1227ba8c6fd5SDavid Greenman /* 1228e9bf2fa7SDavid Greenman * From NetBSD: 1229e9bf2fa7SDavid Greenman * 1230e9bf2fa7SDavid Greenman * Figure out EEPROM size. 1231e9bf2fa7SDavid Greenman * 1232e9bf2fa7SDavid Greenman * 559's can have either 64-word or 256-word EEPROMs, the 558 1233e9bf2fa7SDavid Greenman * datasheet only talks about 64-word EEPROMs, and the 557 datasheet 1234e9bf2fa7SDavid Greenman * talks about the existance of 16 to 256 word EEPROMs. 1235e9bf2fa7SDavid Greenman * 1236e9bf2fa7SDavid Greenman * The only known sizes are 64 and 256, where the 256 version is used 1237e9bf2fa7SDavid Greenman * by CardBus cards to store CIS information. 1238e9bf2fa7SDavid Greenman * 1239e9bf2fa7SDavid Greenman * The address is shifted in msb-to-lsb, and after the last 1240e9bf2fa7SDavid Greenman * address-bit the EEPROM is supposed to output a `dummy zero' bit, 1241e9bf2fa7SDavid Greenman * after which follows the actual data. We try to detect this zero, by 1242e9bf2fa7SDavid Greenman * probing the data-out bit in the EEPROM control register just after 1243e9bf2fa7SDavid Greenman * having shifted in a bit. If the bit is zero, we assume we've 1244e9bf2fa7SDavid Greenman * shifted enough address bits. The data-out should be tri-state, 1245e9bf2fa7SDavid Greenman * before this, which should translate to a logical one. 1246e9bf2fa7SDavid Greenman */ 1247e9bf2fa7SDavid Greenman static void 1248f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc) 1249e9bf2fa7SDavid Greenman { 1250e9bf2fa7SDavid Greenman 1251f7788e8eSJonathan Lemon /* guess maximum size of 256 words */ 1252f7788e8eSJonathan Lemon sc->eeprom_size = 8; 1253f7788e8eSJonathan Lemon 1254f7788e8eSJonathan Lemon /* autosize */ 1255f7788e8eSJonathan Lemon (void) fxp_eeprom_getword(sc, 0, 1); 1256e9bf2fa7SDavid Greenman } 1257f7788e8eSJonathan Lemon 1258ba8c6fd5SDavid Greenman static void 1259f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words) 1260ba8c6fd5SDavid Greenman { 1261f7788e8eSJonathan Lemon int i; 1262ba8c6fd5SDavid Greenman 1263f7788e8eSJonathan Lemon for (i = 0; i < words; i++) 1264f7788e8eSJonathan Lemon data[i] = fxp_eeprom_getword(sc, offset + i, 0); 1265ba8c6fd5SDavid Greenman } 1266ba8c6fd5SDavid Greenman 126700c4116bSJonathan Lemon static void 126800c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words) 126900c4116bSJonathan Lemon { 127000c4116bSJonathan Lemon int i; 127100c4116bSJonathan Lemon 127200c4116bSJonathan Lemon for (i = 0; i < words; i++) 127300c4116bSJonathan Lemon fxp_eeprom_putword(sc, offset + i, data[i]); 127400c4116bSJonathan Lemon } 127500c4116bSJonathan Lemon 1276a17c678eSDavid Greenman /* 12774953bccaSNate Lawson * Grab the softc lock and call the real fxp_start_body() routine 1278a17c678eSDavid Greenman */ 1279a17c678eSDavid Greenman static void 1280f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp) 1281a17c678eSDavid Greenman { 12829b44ff22SGarrett Wollman struct fxp_softc *sc = ifp->if_softc; 12834953bccaSNate Lawson 12844953bccaSNate Lawson FXP_LOCK(sc); 12854953bccaSNate Lawson fxp_start_body(ifp); 12864953bccaSNate Lawson FXP_UNLOCK(sc); 12874953bccaSNate Lawson } 12884953bccaSNate Lawson 12894953bccaSNate Lawson /* 12904953bccaSNate Lawson * Start packet transmission on the interface. 12914953bccaSNate Lawson * This routine must be called with the softc lock held, and is an 12924953bccaSNate Lawson * internal entry point only. 12934953bccaSNate Lawson */ 12944953bccaSNate Lawson static void 12954953bccaSNate Lawson fxp_start_body(struct ifnet *ifp) 12964953bccaSNate Lawson { 12974953bccaSNate Lawson struct fxp_softc *sc = ifp->if_softc; 1298b2badf02SMaxime Henrion struct mbuf *mb_head; 12994e53f837SPyun YongHyeon int txqueued; 1300a17c678eSDavid Greenman 130167fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 130240c20505SMaxime Henrion 1303a17c678eSDavid Greenman /* 1304483b9871SDavid Greenman * See if we need to suspend xmit until the multicast filter 1305483b9871SDavid Greenman * has been reprogrammed (which can only be done at the head 1306483b9871SDavid Greenman * of the command chain). 1307a17c678eSDavid Greenman */ 130840c20505SMaxime Henrion if (sc->need_mcsetup) 1309a17c678eSDavid Greenman return; 1310483b9871SDavid Greenman 13114e53f837SPyun YongHyeon if (sc->tx_queued > FXP_NTXCB_HIWAT) 13124e53f837SPyun YongHyeon fxp_txeof(sc); 1313483b9871SDavid Greenman /* 1314483b9871SDavid Greenman * We're finished if there is nothing more to add to the list or if 1315483b9871SDavid Greenman * we're all filled up with buffers to transmit. 13163114fdb4SDavid Greenman * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add 13173114fdb4SDavid Greenman * a NOP command when needed. 1318483b9871SDavid Greenman */ 131940c20505SMaxime Henrion txqueued = 0; 13207929aa03SMax Laier while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 13217929aa03SMax Laier sc->tx_queued < FXP_NTXCB - 1) { 1322483b9871SDavid Greenman 1323dfe61cf1SDavid Greenman /* 1324dfe61cf1SDavid Greenman * Grab a packet to transmit. 1325dfe61cf1SDavid Greenman */ 13267929aa03SMax Laier IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head); 13277929aa03SMax Laier if (mb_head == NULL) 13287929aa03SMax Laier break; 1329a17c678eSDavid Greenman 13304e53f837SPyun YongHyeon if (fxp_encap(sc, &mb_head)) { 13314e53f837SPyun YongHyeon if (mb_head == NULL) 133240c20505SMaxime Henrion break; 13334e53f837SPyun YongHyeon IFQ_DRV_PREPEND(&ifp->if_snd, mb_head); 13344e53f837SPyun YongHyeon ifp->if_drv_flags |= IFF_DRV_OACTIVE; 133540c20505SMaxime Henrion } 13364e53f837SPyun YongHyeon txqueued++; 13374e53f837SPyun YongHyeon /* 13384e53f837SPyun YongHyeon * Pass packet to bpf if there is a listener. 13394e53f837SPyun YongHyeon */ 13404e53f837SPyun YongHyeon BPF_MTAP(ifp, mb_head); 13414e53f837SPyun YongHyeon } 134240c20505SMaxime Henrion 134340c20505SMaxime Henrion /* 134440c20505SMaxime Henrion * We're finished. If we added to the list, issue a RESUME to get DMA 134540c20505SMaxime Henrion * going again if suspended. 134640c20505SMaxime Henrion */ 13474e53f837SPyun YongHyeon if (txqueued > 0) { 1348a2057a72SPyun YongHyeon bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, 1349a2057a72SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 135040c20505SMaxime Henrion fxp_scb_wait(sc); 135140c20505SMaxime Henrion fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME); 13524e53f837SPyun YongHyeon /* 13534e53f837SPyun YongHyeon * Set a 5 second timer just in case we don't hear 13544e53f837SPyun YongHyeon * from the card again. 13554e53f837SPyun YongHyeon */ 13564e53f837SPyun YongHyeon sc->watchdog_timer = 5; 135740c20505SMaxime Henrion } 135840c20505SMaxime Henrion } 135940c20505SMaxime Henrion 136040c20505SMaxime Henrion static int 13614e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head) 136240c20505SMaxime Henrion { 136340c20505SMaxime Henrion struct ifnet *ifp; 136440c20505SMaxime Henrion struct mbuf *m; 136540c20505SMaxime Henrion struct fxp_tx *txp; 136640c20505SMaxime Henrion struct fxp_cb_tx *cbp; 1367c21e84e4SPyun YongHyeon struct tcphdr *tcp; 136840c20505SMaxime Henrion bus_dma_segment_t segs[FXP_NTXSEG]; 1369c21e84e4SPyun YongHyeon int error, i, nseg, tcp_payload; 137040c20505SMaxime Henrion 137140c20505SMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 1372fc74a9f9SBrooks Davis ifp = sc->ifp; 137340c20505SMaxime Henrion 1374c21e84e4SPyun YongHyeon tcp_payload = 0; 1375c21e84e4SPyun YongHyeon tcp = NULL; 1376dfe61cf1SDavid Greenman /* 1377483b9871SDavid Greenman * Get pointer to next available tx desc. 1378dfe61cf1SDavid Greenman */ 1379b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_last->tx_next; 1380c8bca6dcSBill Paul 1381c8bca6dcSBill Paul /* 1382a35e7eaaSDon Lewis * A note in Appendix B of the Intel 8255x 10/100 Mbps 1383a35e7eaaSDon Lewis * Ethernet Controller Family Open Source Software 1384a35e7eaaSDon Lewis * Developer Manual says: 1385a35e7eaaSDon Lewis * Using software parsing is only allowed with legal 1386a35e7eaaSDon Lewis * TCP/IP or UDP/IP packets. 1387a35e7eaaSDon Lewis * ... 1388a35e7eaaSDon Lewis * For all other datagrams, hardware parsing must 1389a35e7eaaSDon Lewis * be used. 1390a35e7eaaSDon Lewis * Software parsing appears to truncate ICMP and 1391a35e7eaaSDon Lewis * fragmented UDP packets that contain one to three 1392a35e7eaaSDon Lewis * bytes in the second (and final) mbuf of the packet. 1393a35e7eaaSDon Lewis */ 1394a35e7eaaSDon Lewis if (sc->flags & FXP_FLAG_EXT_RFA) 1395a35e7eaaSDon Lewis txp->tx_cb->ipcb_ip_activation_high = 1396a35e7eaaSDon Lewis FXP_IPCB_HARDWAREPARSING_ENABLE; 1397a35e7eaaSDon Lewis 13984e53f837SPyun YongHyeon m = *m_head; 1399a35e7eaaSDon Lewis /* 1400c8bca6dcSBill Paul * Deal with TCP/IP checksum offload. Note that 1401c8bca6dcSBill Paul * in order for TCP checksum offload to work, 1402c8bca6dcSBill Paul * the pseudo header checksum must have already 1403c8bca6dcSBill Paul * been computed and stored in the checksum field 1404c8bca6dcSBill Paul * in the TCP header. The stack should have 1405c8bca6dcSBill Paul * already done this for us. 1406c8bca6dcSBill Paul */ 1407c583cc04SPyun YongHyeon if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) { 1408c583cc04SPyun YongHyeon txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE; 14094e53f837SPyun YongHyeon if (m->m_pkthdr.csum_flags & CSUM_TCP) 1410c583cc04SPyun YongHyeon txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET; 141140c20505SMaxime Henrion 1412c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR 1413c8bca6dcSBill Paul /* 1414c8bca6dcSBill Paul * XXX The 82550 chip appears to have trouble 1415c8bca6dcSBill Paul * dealing with IP header checksums in very small 1416c8bca6dcSBill Paul * datagrams, namely fragments from 1 to 3 bytes 1417c8bca6dcSBill Paul * in size. For example, say you want to transmit 1418c8bca6dcSBill Paul * a UDP packet of 1473 bytes. The packet will be 1419c8bca6dcSBill Paul * fragmented over two IP datagrams, the latter 1420c8bca6dcSBill Paul * containing only one byte of data. The 82550 will 1421c8bca6dcSBill Paul * botch the header checksum on the 1-byte fragment. 1422c8bca6dcSBill Paul * As long as the datagram contains 4 or more bytes 1423c8bca6dcSBill Paul * of data, you're ok. 1424c8bca6dcSBill Paul * 1425c8bca6dcSBill Paul * The following code attempts to work around this 1426c8bca6dcSBill Paul * problem: if the datagram is less than 38 bytes 1427c8bca6dcSBill Paul * in size (14 bytes ether header, 20 bytes IP header, 1428c8bca6dcSBill Paul * plus 4 bytes of data), we punt and compute the IP 1429c8bca6dcSBill Paul * header checksum by hand. This workaround doesn't 1430c8bca6dcSBill Paul * work very well, however, since it can be fooled 1431c8bca6dcSBill Paul * by things like VLAN tags and IP options that make 1432c8bca6dcSBill Paul * the header sizes/offsets vary. 1433c8bca6dcSBill Paul */ 1434c8bca6dcSBill Paul 14354e53f837SPyun YongHyeon if (m->m_pkthdr.csum_flags & CSUM_IP) { 14364e53f837SPyun YongHyeon if (m->m_pkthdr.len < 38) { 1437c8bca6dcSBill Paul struct ip *ip; 14384e53f837SPyun YongHyeon m->m_data += ETHER_HDR_LEN; 14394e53f837SPyun YongHyeon ip = mtod(m, struct ip *); 14404e53f837SPyun YongHyeon ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 14414e53f837SPyun YongHyeon m->m_data -= ETHER_HDR_LEN; 1442c583cc04SPyun YongHyeon m->m_pkthdr.csum_flags &= ~CSUM_IP; 1443c8bca6dcSBill Paul } else { 1444b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_activation_high = 1445c8bca6dcSBill Paul FXP_IPCB_HARDWAREPARSING_ENABLE; 1446b2badf02SMaxime Henrion txp->tx_cb->ipcb_ip_schedule |= 1447c8bca6dcSBill Paul FXP_IPCB_IP_CHECKSUM_ENABLE; 1448c8bca6dcSBill Paul } 1449c8bca6dcSBill Paul } 1450c8bca6dcSBill Paul #endif 1451c8bca6dcSBill Paul } 1452c8bca6dcSBill Paul 1453c21e84e4SPyun YongHyeon if (m->m_pkthdr.csum_flags & CSUM_TSO) { 1454c21e84e4SPyun YongHyeon /* 1455c21e84e4SPyun YongHyeon * 82550/82551 requires ethernet/IP/TCP headers must be 1456c21e84e4SPyun YongHyeon * contained in the first active transmit buffer. 1457c21e84e4SPyun YongHyeon */ 1458c21e84e4SPyun YongHyeon struct ether_header *eh; 1459c21e84e4SPyun YongHyeon struct ip *ip; 1460c21e84e4SPyun YongHyeon uint32_t ip_off, poff; 1461c21e84e4SPyun YongHyeon 1462c21e84e4SPyun YongHyeon if (M_WRITABLE(*m_head) == 0) { 1463c21e84e4SPyun YongHyeon /* Get a writable copy. */ 1464c21e84e4SPyun YongHyeon m = m_dup(*m_head, M_DONTWAIT); 1465c21e84e4SPyun YongHyeon m_freem(*m_head); 1466c21e84e4SPyun YongHyeon if (m == NULL) { 1467c21e84e4SPyun YongHyeon *m_head = NULL; 1468c21e84e4SPyun YongHyeon return (ENOBUFS); 1469c21e84e4SPyun YongHyeon } 1470c21e84e4SPyun YongHyeon *m_head = m; 1471c21e84e4SPyun YongHyeon } 1472c21e84e4SPyun YongHyeon ip_off = sizeof(struct ether_header); 1473c21e84e4SPyun YongHyeon m = m_pullup(*m_head, ip_off); 1474c21e84e4SPyun YongHyeon if (m == NULL) { 1475c21e84e4SPyun YongHyeon *m_head = NULL; 1476c21e84e4SPyun YongHyeon return (ENOBUFS); 1477c21e84e4SPyun YongHyeon } 1478c21e84e4SPyun YongHyeon eh = mtod(m, struct ether_header *); 1479c21e84e4SPyun YongHyeon /* Check the existence of VLAN tag. */ 1480c21e84e4SPyun YongHyeon if (eh->ether_type == htons(ETHERTYPE_VLAN)) { 1481c21e84e4SPyun YongHyeon ip_off = sizeof(struct ether_vlan_header); 1482c21e84e4SPyun YongHyeon m = m_pullup(m, ip_off); 1483c21e84e4SPyun YongHyeon if (m == NULL) { 1484c21e84e4SPyun YongHyeon *m_head = NULL; 1485c21e84e4SPyun YongHyeon return (ENOBUFS); 1486c21e84e4SPyun YongHyeon } 1487c21e84e4SPyun YongHyeon } 1488c21e84e4SPyun YongHyeon m = m_pullup(m, ip_off + sizeof(struct ip)); 1489c21e84e4SPyun YongHyeon if (m == NULL) { 1490c21e84e4SPyun YongHyeon *m_head = NULL; 1491c21e84e4SPyun YongHyeon return (ENOBUFS); 1492c21e84e4SPyun YongHyeon } 1493c21e84e4SPyun YongHyeon ip = (struct ip *)(mtod(m, char *) + ip_off); 1494c21e84e4SPyun YongHyeon poff = ip_off + (ip->ip_hl << 2); 1495c21e84e4SPyun YongHyeon m = m_pullup(m, poff + sizeof(struct tcphdr)); 1496c21e84e4SPyun YongHyeon if (m == NULL) { 1497c21e84e4SPyun YongHyeon *m_head = NULL; 1498c21e84e4SPyun YongHyeon return (ENOBUFS); 1499c21e84e4SPyun YongHyeon } 1500c21e84e4SPyun YongHyeon tcp = (struct tcphdr *)(mtod(m, char *) + poff); 1501c21e84e4SPyun YongHyeon m = m_pullup(m, poff + sizeof(struct tcphdr) + tcp->th_off); 1502c21e84e4SPyun YongHyeon if (m == NULL) { 1503c21e84e4SPyun YongHyeon *m_head = NULL; 1504c21e84e4SPyun YongHyeon return (ENOBUFS); 1505c21e84e4SPyun YongHyeon } 1506c21e84e4SPyun YongHyeon 1507c21e84e4SPyun YongHyeon /* 1508c21e84e4SPyun YongHyeon * Since 82550/82551 doesn't modify IP length and pseudo 1509c21e84e4SPyun YongHyeon * checksum in the first frame driver should compute it. 1510c21e84e4SPyun YongHyeon */ 1511c21e84e4SPyun YongHyeon ip->ip_sum = 0; 15120685c824SPyun YongHyeon ip->ip_len = htons(m->m_pkthdr.tso_segsz + (ip->ip_hl << 2) + 15130685c824SPyun YongHyeon (tcp->th_off << 2)); 1514c21e84e4SPyun YongHyeon tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 1515c21e84e4SPyun YongHyeon htons(IPPROTO_TCP + (tcp->th_off << 2) + 1516c21e84e4SPyun YongHyeon m->m_pkthdr.tso_segsz)); 1517c21e84e4SPyun YongHyeon /* Compute total TCP payload. */ 1518c21e84e4SPyun YongHyeon tcp_payload = m->m_pkthdr.len - ip_off - (ip->ip_hl << 2); 1519c21e84e4SPyun YongHyeon tcp_payload -= tcp->th_off << 2; 1520c21e84e4SPyun YongHyeon *m_head = m; 1521c21e84e4SPyun YongHyeon } 1522c21e84e4SPyun YongHyeon 1523a2057a72SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map, *m_head, 15244e53f837SPyun YongHyeon segs, &nseg, 0); 15254e53f837SPyun YongHyeon if (error == EFBIG) { 15264e53f837SPyun YongHyeon m = m_collapse(*m_head, M_DONTWAIT, sc->maxtxseg); 15274e53f837SPyun YongHyeon if (m == NULL) { 15284e53f837SPyun YongHyeon m_freem(*m_head); 15294e53f837SPyun YongHyeon *m_head = NULL; 15304e53f837SPyun YongHyeon return (ENOMEM); 15311104779bSMike Silbersack } 15324e53f837SPyun YongHyeon *m_head = m; 1533a2057a72SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map, 15344e53f837SPyun YongHyeon *m_head, segs, &nseg, 0); 15354e53f837SPyun YongHyeon if (error != 0) { 15364e53f837SPyun YongHyeon m_freem(*m_head); 15374e53f837SPyun YongHyeon *m_head = NULL; 15384e53f837SPyun YongHyeon return (ENOMEM); 15394e53f837SPyun YongHyeon } 15404e53f837SPyun YongHyeon } else if (error != 0) 15414e53f837SPyun YongHyeon return (error); 15424e53f837SPyun YongHyeon if (nseg == 0) { 15434e53f837SPyun YongHyeon m_freem(*m_head); 15444e53f837SPyun YongHyeon *m_head = NULL; 15454e53f837SPyun YongHyeon return (EIO); 154623a0ed7cSDavid Greenman } 154723a0ed7cSDavid Greenman 154840c20505SMaxime Henrion KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments")); 1549a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map, BUS_DMASYNC_PREWRITE); 1550b2badf02SMaxime Henrion 155140c20505SMaxime Henrion cbp = txp->tx_cb; 155240c20505SMaxime Henrion for (i = 0; i < nseg; i++) { 155340c20505SMaxime Henrion /* 155440c20505SMaxime Henrion * If this is an 82550/82551, then we're using extended 155540c20505SMaxime Henrion * TxCBs _and_ we're using checksum offload. This means 155640c20505SMaxime Henrion * that the TxCB is really an IPCB. One major difference 155740c20505SMaxime Henrion * between the two is that with plain extended TxCBs, 155840c20505SMaxime Henrion * the bottom half of the TxCB contains two entries from 155940c20505SMaxime Henrion * the TBD array, whereas IPCBs contain just one entry: 156040c20505SMaxime Henrion * one entry (8 bytes) has been sacrificed for the TCP/IP 156140c20505SMaxime Henrion * checksum offload control bits. So to make things work 156240c20505SMaxime Henrion * right, we have to start filling in the TBD array 156340c20505SMaxime Henrion * starting from a different place depending on whether 156440c20505SMaxime Henrion * the chip is an 82550/82551 or not. 156540c20505SMaxime Henrion */ 156640c20505SMaxime Henrion if (sc->flags & FXP_FLAG_EXT_RFA) { 156768f4ab9aSPyun YongHyeon cbp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr); 156868f4ab9aSPyun YongHyeon cbp->tbd[i + 1].tb_size = htole32(segs[i].ds_len); 156940c20505SMaxime Henrion } else { 157040c20505SMaxime Henrion cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr); 157140c20505SMaxime Henrion cbp->tbd[i].tb_size = htole32(segs[i].ds_len); 157240c20505SMaxime Henrion } 157340c20505SMaxime Henrion } 1574c21e84e4SPyun YongHyeon if (sc->flags & FXP_FLAG_EXT_RFA) { 1575c21e84e4SPyun YongHyeon /* Configure dynamic TBD for 82550/82551. */ 1576c21e84e4SPyun YongHyeon cbp->tbd_number = 0xFF; 157768f4ab9aSPyun YongHyeon cbp->tbd[nseg].tb_size |= htole32(0x8000); 1578c21e84e4SPyun YongHyeon } else 157940c20505SMaxime Henrion cbp->tbd_number = nseg; 1580c21e84e4SPyun YongHyeon /* Configure TSO. */ 1581c21e84e4SPyun YongHyeon if (m->m_pkthdr.csum_flags & CSUM_TSO) { 1582c21e84e4SPyun YongHyeon cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16); 158368f4ab9aSPyun YongHyeon cbp->tbd[1].tb_size |= htole32(tcp_payload << 16); 1584c21e84e4SPyun YongHyeon cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE | 1585c21e84e4SPyun YongHyeon FXP_IPCB_IP_CHECKSUM_ENABLE | 1586c21e84e4SPyun YongHyeon FXP_IPCB_TCP_PACKET | 1587c21e84e4SPyun YongHyeon FXP_IPCB_TCPUDP_CHECKSUM_ENABLE; 1588c21e84e4SPyun YongHyeon } 1589bd4fa9d9SPyun YongHyeon /* Configure VLAN hardware tag insertion. */ 1590bd4fa9d9SPyun YongHyeon if ((m->m_flags & M_VLANTAG) != 0) { 1591bd4fa9d9SPyun YongHyeon cbp->ipcb_vlan_id = htons(m->m_pkthdr.ether_vtag); 1592bd4fa9d9SPyun YongHyeon txp->tx_cb->ipcb_ip_activation_high |= 1593bd4fa9d9SPyun YongHyeon FXP_IPCB_INSERTVLAN_ENABLE; 1594bd4fa9d9SPyun YongHyeon } 159540c20505SMaxime Henrion 15964e53f837SPyun YongHyeon txp->tx_mbuf = m; 1597b2badf02SMaxime Henrion txp->tx_cb->cb_status = 0; 1598b2badf02SMaxime Henrion txp->tx_cb->byte_count = 0; 15994e53f837SPyun YongHyeon if (sc->tx_queued != FXP_CXINT_THRESH - 1) 1600b2badf02SMaxime Henrion txp->tx_cb->cb_command = 160183e6547dSMaxime Henrion htole16(sc->tx_cmd | FXP_CB_COMMAND_SF | 160283e6547dSMaxime Henrion FXP_CB_COMMAND_S); 16034e53f837SPyun YongHyeon else 1604b2badf02SMaxime Henrion txp->tx_cb->cb_command = 160583e6547dSMaxime Henrion htole16(sc->tx_cmd | FXP_CB_COMMAND_SF | 160683e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 1607c21e84e4SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) 1608b2badf02SMaxime Henrion txp->tx_cb->tx_threshold = tx_threshold; 1609a17c678eSDavid Greenman 1610a17c678eSDavid Greenman /* 1611483b9871SDavid Greenman * Advance the end of list forward. 1612a17c678eSDavid Greenman */ 161306175228SAndrew Gallatin 161450d81222SMaxime Henrion #ifdef __alpha__ 161506175228SAndrew Gallatin /* 161606175228SAndrew Gallatin * On platforms which can't access memory in 16-bit 161706175228SAndrew Gallatin * granularities, we must prevent the card from DMA'ing 161806175228SAndrew Gallatin * up the status while we update the command field. 161906175228SAndrew Gallatin * This could cause us to overwrite the completion status. 162014fd1071SMaxime Henrion * XXX This is probably bogus and we're _not_ looking 162114fd1071SMaxime Henrion * for atomicity here. 162206175228SAndrew Gallatin */ 162314fd1071SMaxime Henrion atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command, 1624bafb64afSMaxime Henrion htole16(FXP_CB_COMMAND_S)); 162550d81222SMaxime Henrion #else 162640c20505SMaxime Henrion sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S); 162750d81222SMaxime Henrion #endif /*__alpha__*/ 1628b2badf02SMaxime Henrion sc->fxp_desc.tx_last = txp; 1629a17c678eSDavid Greenman 1630a17c678eSDavid Greenman /* 16311cd443acSDavid Greenman * Advance the beginning of the list forward if there are 1632b2badf02SMaxime Henrion * no other packets queued (when nothing is queued, tx_first 1633483b9871SDavid Greenman * sits on the last TxCB that was sent out). 1634a17c678eSDavid Greenman */ 16351cd443acSDavid Greenman if (sc->tx_queued == 0) 1636b2badf02SMaxime Henrion sc->fxp_desc.tx_first = txp; 1637a17c678eSDavid Greenman 16381cd443acSDavid Greenman sc->tx_queued++; 16391cd443acSDavid Greenman 164040c20505SMaxime Henrion return (0); 1641a17c678eSDavid Greenman } 1642a17c678eSDavid Greenman 1643e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 1644e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll; 1645e4fc250cSLuigi Rizzo 16461abcdbd1SAttilio Rao static int 1647e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1648e4fc250cSLuigi Rizzo { 1649e4fc250cSLuigi Rizzo struct fxp_softc *sc = ifp->if_softc; 165074d1ed23SMaxime Henrion uint8_t statack; 16511abcdbd1SAttilio Rao int rx_npkts = 0; 1652e4fc250cSLuigi Rizzo 16534953bccaSNate Lawson FXP_LOCK(sc); 165440929967SGleb Smirnoff if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 16554953bccaSNate Lawson FXP_UNLOCK(sc); 16561abcdbd1SAttilio Rao return (rx_npkts); 1657e4fc250cSLuigi Rizzo } 165840929967SGleb Smirnoff 1659e4fc250cSLuigi Rizzo statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA | 1660e4fc250cSLuigi Rizzo FXP_SCB_STATACK_FR; 1661e4fc250cSLuigi Rizzo if (cmd == POLL_AND_CHECK_STATUS) { 166274d1ed23SMaxime Henrion uint8_t tmp; 16636481f301SPeter Wemm 1664e4fc250cSLuigi Rizzo tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK); 16654953bccaSNate Lawson if (tmp == 0xff || tmp == 0) { 16664953bccaSNate Lawson FXP_UNLOCK(sc); 16671abcdbd1SAttilio Rao return (rx_npkts); /* nothing to do */ 16684953bccaSNate Lawson } 1669e4fc250cSLuigi Rizzo tmp &= ~statack; 1670e4fc250cSLuigi Rizzo /* ack what we can */ 1671e4fc250cSLuigi Rizzo if (tmp != 0) 1672e4fc250cSLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp); 1673e4fc250cSLuigi Rizzo statack |= tmp; 1674e4fc250cSLuigi Rizzo } 16751abcdbd1SAttilio Rao rx_npkts = fxp_intr_body(sc, ifp, statack, count); 16764953bccaSNate Lawson FXP_UNLOCK(sc); 16771abcdbd1SAttilio Rao return (rx_npkts); 1678e4fc250cSLuigi Rizzo } 1679e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 1680e4fc250cSLuigi Rizzo 1681a17c678eSDavid Greenman /* 16829c7d2607SDavid Greenman * Process interface interrupts. 1683a17c678eSDavid Greenman */ 168494927790SDavid Greenman static void 1685f7788e8eSJonathan Lemon fxp_intr(void *xsc) 1686a17c678eSDavid Greenman { 1687f7788e8eSJonathan Lemon struct fxp_softc *sc = xsc; 1688fc74a9f9SBrooks Davis struct ifnet *ifp = sc->ifp; 168974d1ed23SMaxime Henrion uint8_t statack; 16900f4dc94cSChuck Paterson 16914953bccaSNate Lawson FXP_LOCK(sc); 1692704d1965SWarner Losh if (sc->suspended) { 1693704d1965SWarner Losh FXP_UNLOCK(sc); 1694704d1965SWarner Losh return; 1695704d1965SWarner Losh } 1696704d1965SWarner Losh 1697e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 169840929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) { 16994953bccaSNate Lawson FXP_UNLOCK(sc); 1700e4fc250cSLuigi Rizzo return; 17014953bccaSNate Lawson } 1702e4fc250cSLuigi Rizzo #endif 1703b184b38eSDavid Greenman while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) { 1704a17c678eSDavid Greenman /* 170511457bbfSJonathan Lemon * It should not be possible to have all bits set; the 170611457bbfSJonathan Lemon * FXP_SCB_INTR_SWI bit always returns 0 on a read. If 170711457bbfSJonathan Lemon * all bits are set, this may indicate that the card has 170811457bbfSJonathan Lemon * been physically ejected, so ignore it. 170911457bbfSJonathan Lemon */ 17104953bccaSNate Lawson if (statack == 0xff) { 17114953bccaSNate Lawson FXP_UNLOCK(sc); 171211457bbfSJonathan Lemon return; 17134953bccaSNate Lawson } 171411457bbfSJonathan Lemon 171511457bbfSJonathan Lemon /* 1716a17c678eSDavid Greenman * First ACK all the interrupts in this pass. 1717a17c678eSDavid Greenman */ 1718ba8c6fd5SDavid Greenman CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack); 17194953bccaSNate Lawson fxp_intr_body(sc, ifp, statack, -1); 1720e4fc250cSLuigi Rizzo } 17214953bccaSNate Lawson FXP_UNLOCK(sc); 1722e4fc250cSLuigi Rizzo } 1723e4fc250cSLuigi Rizzo 1724e4fc250cSLuigi Rizzo static void 1725b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc) 1726b2badf02SMaxime Henrion { 17274e53f837SPyun YongHyeon struct ifnet *ifp; 1728b2badf02SMaxime Henrion struct fxp_tx *txp; 1729b2badf02SMaxime Henrion 17304e53f837SPyun YongHyeon ifp = sc->ifp; 1731a2057a72SPyun YongHyeon bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, 1732a2057a72SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1733b2badf02SMaxime Henrion for (txp = sc->fxp_desc.tx_first; sc->tx_queued && 173483e6547dSMaxime Henrion (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0; 1735b2badf02SMaxime Henrion txp = txp->tx_next) { 1736b2badf02SMaxime Henrion if (txp->tx_mbuf != NULL) { 1737a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map, 1738b2badf02SMaxime Henrion BUS_DMASYNC_POSTWRITE); 1739a2057a72SPyun YongHyeon bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map); 1740b2badf02SMaxime Henrion m_freem(txp->tx_mbuf); 1741b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 1742b2badf02SMaxime Henrion /* clear this to reset csum offload bits */ 1743b2badf02SMaxime Henrion txp->tx_cb->tbd[0].tb_addr = 0; 1744b2badf02SMaxime Henrion } 1745b2badf02SMaxime Henrion sc->tx_queued--; 17464e53f837SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1747b2badf02SMaxime Henrion } 1748b2badf02SMaxime Henrion sc->fxp_desc.tx_first = txp; 1749a2057a72SPyun YongHyeon bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, 1750a2057a72SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 175125935344SPyun YongHyeon if (sc->tx_queued == 0) { 175225935344SPyun YongHyeon sc->watchdog_timer = 0; 175325935344SPyun YongHyeon if (sc->need_mcsetup) 175425935344SPyun YongHyeon fxp_mc_setup(sc); 175525935344SPyun YongHyeon } 1756b2badf02SMaxime Henrion } 1757b2badf02SMaxime Henrion 1758b2badf02SMaxime Henrion static void 1759f13075afSPyun YongHyeon fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp, struct mbuf *m, 1760f13075afSPyun YongHyeon uint16_t status, int pos) 1761f13075afSPyun YongHyeon { 1762f13075afSPyun YongHyeon struct ether_header *eh; 1763f13075afSPyun YongHyeon struct ip *ip; 1764f13075afSPyun YongHyeon struct udphdr *uh; 1765f13075afSPyun YongHyeon int32_t hlen, len, pktlen, temp32; 1766f13075afSPyun YongHyeon uint16_t csum, *opts; 1767f13075afSPyun YongHyeon 1768f13075afSPyun YongHyeon if ((sc->flags & FXP_FLAG_82559_RXCSUM) == 0) { 1769f13075afSPyun YongHyeon if ((status & FXP_RFA_STATUS_PARSE) != 0) { 1770f13075afSPyun YongHyeon if (status & FXP_RFDX_CS_IP_CSUM_BIT_VALID) 1771f13075afSPyun YongHyeon m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1772f13075afSPyun YongHyeon if (status & FXP_RFDX_CS_IP_CSUM_VALID) 1773f13075afSPyun YongHyeon m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1774f13075afSPyun YongHyeon if ((status & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) && 1775f13075afSPyun YongHyeon (status & FXP_RFDX_CS_TCPUDP_CSUM_VALID)) { 1776f13075afSPyun YongHyeon m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | 1777f13075afSPyun YongHyeon CSUM_PSEUDO_HDR; 1778f13075afSPyun YongHyeon m->m_pkthdr.csum_data = 0xffff; 1779f13075afSPyun YongHyeon } 1780f13075afSPyun YongHyeon } 1781f13075afSPyun YongHyeon return; 1782f13075afSPyun YongHyeon } 1783f13075afSPyun YongHyeon 1784f13075afSPyun YongHyeon pktlen = m->m_pkthdr.len; 1785f13075afSPyun YongHyeon if (pktlen < sizeof(struct ether_header) + sizeof(struct ip)) 1786f13075afSPyun YongHyeon return; 1787f13075afSPyun YongHyeon eh = mtod(m, struct ether_header *); 1788f13075afSPyun YongHyeon if (eh->ether_type != htons(ETHERTYPE_IP)) 1789f13075afSPyun YongHyeon return; 1790f13075afSPyun YongHyeon ip = (struct ip *)(eh + 1); 1791f13075afSPyun YongHyeon if (ip->ip_v != IPVERSION) 1792f13075afSPyun YongHyeon return; 1793f13075afSPyun YongHyeon 1794f13075afSPyun YongHyeon hlen = ip->ip_hl << 2; 1795f13075afSPyun YongHyeon pktlen -= sizeof(struct ether_header); 1796f13075afSPyun YongHyeon if (hlen < sizeof(struct ip)) 1797f13075afSPyun YongHyeon return; 1798f13075afSPyun YongHyeon if (ntohs(ip->ip_len) < hlen) 1799f13075afSPyun YongHyeon return; 1800f13075afSPyun YongHyeon if (ntohs(ip->ip_len) != pktlen) 1801f13075afSPyun YongHyeon return; 1802f13075afSPyun YongHyeon if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 1803f13075afSPyun YongHyeon return; /* can't handle fragmented packet */ 1804f13075afSPyun YongHyeon 1805f13075afSPyun YongHyeon switch (ip->ip_p) { 1806f13075afSPyun YongHyeon case IPPROTO_TCP: 1807f13075afSPyun YongHyeon if (pktlen < (hlen + sizeof(struct tcphdr))) 1808f13075afSPyun YongHyeon return; 1809f13075afSPyun YongHyeon break; 1810f13075afSPyun YongHyeon case IPPROTO_UDP: 1811f13075afSPyun YongHyeon if (pktlen < (hlen + sizeof(struct udphdr))) 1812f13075afSPyun YongHyeon return; 1813f13075afSPyun YongHyeon uh = (struct udphdr *)((caddr_t)ip + hlen); 1814f13075afSPyun YongHyeon if (uh->uh_sum == 0) 1815f13075afSPyun YongHyeon return; /* no checksum */ 1816f13075afSPyun YongHyeon break; 1817f13075afSPyun YongHyeon default: 1818f13075afSPyun YongHyeon return; 1819f13075afSPyun YongHyeon } 1820f13075afSPyun YongHyeon /* Extract computed checksum. */ 1821f13075afSPyun YongHyeon csum = be16dec(mtod(m, char *) + pos); 1822f13075afSPyun YongHyeon /* checksum fixup for IP options */ 1823f13075afSPyun YongHyeon len = hlen - sizeof(struct ip); 1824f13075afSPyun YongHyeon if (len > 0) { 1825f13075afSPyun YongHyeon opts = (uint16_t *)(ip + 1); 1826f13075afSPyun YongHyeon for (; len > 0; len -= sizeof(uint16_t), opts++) { 1827f13075afSPyun YongHyeon temp32 = csum - *opts; 1828f13075afSPyun YongHyeon temp32 = (temp32 >> 16) + (temp32 & 65535); 1829f13075afSPyun YongHyeon csum = temp32 & 65535; 1830f13075afSPyun YongHyeon } 1831f13075afSPyun YongHyeon } 1832f13075afSPyun YongHyeon m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 1833f13075afSPyun YongHyeon m->m_pkthdr.csum_data = csum; 1834f13075afSPyun YongHyeon } 1835f13075afSPyun YongHyeon 18361abcdbd1SAttilio Rao static int 183774d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack, 18384953bccaSNate Lawson int count) 1839e4fc250cSLuigi Rizzo { 18402b5989e9SLuigi Rizzo struct mbuf *m; 1841b2badf02SMaxime Henrion struct fxp_rx *rxp; 18422b5989e9SLuigi Rizzo struct fxp_rfa *rfa; 18432b5989e9SLuigi Rizzo int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0; 18441abcdbd1SAttilio Rao int rx_npkts; 184560bb79ebSPyun YongHyeon uint16_t status; 18462b5989e9SLuigi Rizzo 18471abcdbd1SAttilio Rao rx_npkts = 0; 184867fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 18491abcdbd1SAttilio Rao 18502b5989e9SLuigi Rizzo if (rnr) 18510f1db1d6SMaxime Henrion sc->rnr++; 1852947e3815SIan Dowse #ifdef DEVICE_POLLING 1853947e3815SIan Dowse /* Pick up a deferred RNR condition if `count' ran out last time. */ 1854947e3815SIan Dowse if (sc->flags & FXP_FLAG_DEFERRED_RNR) { 1855947e3815SIan Dowse sc->flags &= ~FXP_FLAG_DEFERRED_RNR; 1856947e3815SIan Dowse rnr = 1; 1857947e3815SIan Dowse } 1858947e3815SIan Dowse #endif 1859a17c678eSDavid Greenman 1860a17c678eSDavid Greenman /* 18613114fdb4SDavid Greenman * Free any finished transmit mbuf chains. 186206936301SBill Paul * 186306936301SBill Paul * Handle the CNA event likt a CXTNO event. It used to 186406936301SBill Paul * be that this event (control unit not ready) was not 186506936301SBill Paul * encountered, but it is now with the SMPng modifications. 186606936301SBill Paul * The exact sequence of events that occur when the interface 186706936301SBill Paul * is brought up are different now, and if this event 186806936301SBill Paul * goes unhandled, the configuration/rxfilter setup sequence 186906936301SBill Paul * can stall for several seconds. The result is that no 187006936301SBill Paul * packets go out onto the wire for about 5 to 10 seconds 187106936301SBill Paul * after the interface is ifconfig'ed for the first time. 18723114fdb4SDavid Greenman */ 18734e53f837SPyun YongHyeon if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) 1874b2badf02SMaxime Henrion fxp_txeof(sc); 18753114fdb4SDavid Greenman 18763114fdb4SDavid Greenman /* 18773114fdb4SDavid Greenman * Try to start more packets transmitting. 18783114fdb4SDavid Greenman */ 18797929aa03SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 18804953bccaSNate Lawson fxp_start_body(ifp); 18812b5989e9SLuigi Rizzo 18822b5989e9SLuigi Rizzo /* 18832b5989e9SLuigi Rizzo * Just return if nothing happened on the receive side. 18842b5989e9SLuigi Rizzo */ 1885947e3815SIan Dowse if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0) 18861abcdbd1SAttilio Rao return (rx_npkts); 18872b5989e9SLuigi Rizzo 18883114fdb4SDavid Greenman /* 1889a17c678eSDavid Greenman * Process receiver interrupts. If a no-resource (RNR) 1890a17c678eSDavid Greenman * condition exists, get whatever packets we can and 1891a17c678eSDavid Greenman * re-start the receiver. 1892947e3815SIan Dowse * 18932b5989e9SLuigi Rizzo * When using polling, we do not process the list to completion, 18942b5989e9SLuigi Rizzo * so when we get an RNR interrupt we must defer the restart 18952b5989e9SLuigi Rizzo * until we hit the last buffer with the C bit set. 18962b5989e9SLuigi Rizzo * If we run out of cycles and rfa_headm has the C bit set, 1897947e3815SIan Dowse * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so 1898947e3815SIan Dowse * that the info will be used in the subsequent polling cycle. 1899a17c678eSDavid Greenman */ 19002b5989e9SLuigi Rizzo for (;;) { 1901b2badf02SMaxime Henrion rxp = sc->fxp_desc.rx_head; 1902b2badf02SMaxime Henrion m = rxp->rx_mbuf; 1903ba8c6fd5SDavid Greenman rfa = (struct fxp_rfa *)(m->m_ext.ext_buf + 1904ba8c6fd5SDavid Greenman RFA_ALIGNMENT_FUDGE); 1905a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map, 1906b2badf02SMaxime Henrion BUS_DMASYNC_POSTREAD); 1907a17c678eSDavid Greenman 1908e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */ 1909947e3815SIan Dowse if (count >= 0 && count-- == 0) { 1910947e3815SIan Dowse if (rnr) { 1911947e3815SIan Dowse /* Defer RNR processing until the next time. */ 1912947e3815SIan Dowse sc->flags |= FXP_FLAG_DEFERRED_RNR; 1913947e3815SIan Dowse rnr = 0; 1914947e3815SIan Dowse } 19152b5989e9SLuigi Rizzo break; 1916947e3815SIan Dowse } 19172b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */ 19182b5989e9SLuigi Rizzo 191960bb79ebSPyun YongHyeon status = le16toh(rfa->rfa_status); 192060bb79ebSPyun YongHyeon if ((status & FXP_RFA_STATUS_C) == 0) 19212b5989e9SLuigi Rizzo break; 19222b5989e9SLuigi Rizzo 1923dfe61cf1SDavid Greenman /* 1924b2badf02SMaxime Henrion * Advance head forward. 1925dfe61cf1SDavid Greenman */ 1926b2badf02SMaxime Henrion sc->fxp_desc.rx_head = rxp->rx_next; 1927a17c678eSDavid Greenman 1928dfe61cf1SDavid Greenman /* 1929ba8c6fd5SDavid Greenman * Add a new buffer to the receive chain. 1930ba8c6fd5SDavid Greenman * If this fails, the old buffer is recycled 1931ba8c6fd5SDavid Greenman * instead. 1932dfe61cf1SDavid Greenman */ 193385050421SPyun YongHyeon if (fxp_new_rfabuf(sc, rxp) == 0) { 1934aed53495SDavid Greenman int total_len; 1935a17c678eSDavid Greenman 1936e8c8b728SJonathan Lemon /* 19372b5989e9SLuigi Rizzo * Fetch packet length (the top 2 bits of 19382b5989e9SLuigi Rizzo * actual_size are flags set by the controller 19392b5989e9SLuigi Rizzo * upon completion), and drop the packet in case 19402b5989e9SLuigi Rizzo * of bogus length or CRC errors. 1941e8c8b728SJonathan Lemon */ 1942bafb64afSMaxime Henrion total_len = le16toh(rfa->actual_size) & 0x3fff; 1943f13075afSPyun YongHyeon if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 && 1944f13075afSPyun YongHyeon (ifp->if_capenable & IFCAP_RXCSUM) != 0) { 1945f13075afSPyun YongHyeon /* Adjust for appended checksum bytes. */ 1946f13075afSPyun YongHyeon total_len -= 2; 1947f13075afSPyun YongHyeon } 19482b5989e9SLuigi Rizzo if (total_len < sizeof(struct ether_header) || 19492b5989e9SLuigi Rizzo total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE - 195060bb79ebSPyun YongHyeon sc->rfa_size || status & FXP_RFA_STATUS_CRC) { 1951e8c8b728SJonathan Lemon m_freem(m); 19522b5989e9SLuigi Rizzo continue; 1953e8c8b728SJonathan Lemon } 1954920b58e8SBrooks Davis 19552e2de7f2SArchie Cobbs m->m_pkthdr.len = m->m_len = total_len; 1956673d9191SSam Leffler m->m_pkthdr.rcvif = ifp; 1957673d9191SSam Leffler 1958f13075afSPyun YongHyeon /* Do IP checksum checking. */ 1959f13075afSPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 1960f13075afSPyun YongHyeon fxp_rxcsum(sc, ifp, m, status, total_len); 1961bd4fa9d9SPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 && 1962bd4fa9d9SPyun YongHyeon (status & FXP_RFA_STATUS_VLAN) != 0) { 1963bd4fa9d9SPyun YongHyeon m->m_pkthdr.ether_vtag = 1964bd4fa9d9SPyun YongHyeon ntohs(rfa->rfax_vlan_id); 1965bd4fa9d9SPyun YongHyeon m->m_flags |= M_VLANTAG; 1966bd4fa9d9SPyun YongHyeon } 196705fb8c3fSNate Lawson /* 196805fb8c3fSNate Lawson * Drop locks before calling if_input() since it 196905fb8c3fSNate Lawson * may re-enter fxp_start() in the netisr case. 197005fb8c3fSNate Lawson * This would result in a lock reversal. Better 197105fb8c3fSNate Lawson * performance might be obtained by chaining all 197205fb8c3fSNate Lawson * packets received, dropping the lock, and then 197305fb8c3fSNate Lawson * calling if_input() on each one. 197405fb8c3fSNate Lawson */ 197505fb8c3fSNate Lawson FXP_UNLOCK(sc); 1976673d9191SSam Leffler (*ifp->if_input)(ifp, m); 197705fb8c3fSNate Lawson FXP_LOCK(sc); 19781abcdbd1SAttilio Rao rx_npkts++; 197985050421SPyun YongHyeon } else { 198085050421SPyun YongHyeon /* Reuse RFA and loaded DMA map. */ 198185050421SPyun YongHyeon ifp->if_iqdrops++; 198285050421SPyun YongHyeon fxp_discard_rfabuf(sc, rxp); 1983a17c678eSDavid Greenman } 198485050421SPyun YongHyeon fxp_add_rfabuf(sc, rxp); 1985a17c678eSDavid Greenman } 19862b5989e9SLuigi Rizzo if (rnr) { 1987ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 1988ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 1989b2badf02SMaxime Henrion sc->fxp_desc.rx_head->rx_addr); 19902e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START); 1991a17c678eSDavid Greenman } 19921abcdbd1SAttilio Rao return (rx_npkts); 1993a17c678eSDavid Greenman } 1994a17c678eSDavid Greenman 1995dfe61cf1SDavid Greenman /* 1996dfe61cf1SDavid Greenman * Update packet in/out/collision statistics. The i82557 doesn't 1997dfe61cf1SDavid Greenman * allow you to access these counters without doing a fairly 1998dfe61cf1SDavid Greenman * expensive DMA to get _all_ of the statistics it maintains, so 1999dfe61cf1SDavid Greenman * we do this operation here only once per second. The statistics 2000dfe61cf1SDavid Greenman * counters in the kernel are updated from the previous dump-stats 2001dfe61cf1SDavid Greenman * DMA and then a new dump-stats DMA is started. The on-chip 2002dfe61cf1SDavid Greenman * counters are zeroed when the DMA completes. If we can't start 2003dfe61cf1SDavid Greenman * the DMA immediately, we don't wait - we just prepare to read 2004dfe61cf1SDavid Greenman * them again next time. 2005dfe61cf1SDavid Greenman */ 2006303b270bSEivind Eklund static void 2007f7788e8eSJonathan Lemon fxp_tick(void *xsc) 2008a17c678eSDavid Greenman { 2009f7788e8eSJonathan Lemon struct fxp_softc *sc = xsc; 2010fc74a9f9SBrooks Davis struct ifnet *ifp = sc->ifp; 2011a17c678eSDavid Greenman struct fxp_stats *sp = sc->fxp_stats; 2012a17c678eSDavid Greenman 20133212724cSJohn Baldwin FXP_LOCK_ASSERT(sc, MA_OWNED); 2014b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD); 201583e6547dSMaxime Henrion ifp->if_opackets += le32toh(sp->tx_good); 201683e6547dSMaxime Henrion ifp->if_collisions += le32toh(sp->tx_total_collisions); 2017397f9dfeSDavid Greenman if (sp->rx_good) { 201883e6547dSMaxime Henrion ifp->if_ipackets += le32toh(sp->rx_good); 2019397f9dfeSDavid Greenman sc->rx_idle_secs = 0; 2020397f9dfeSDavid Greenman } else { 2021c8cc6fcaSDavid Greenman /* 2022c8cc6fcaSDavid Greenman * Receiver's been idle for another second. 2023c8cc6fcaSDavid Greenman */ 2024397f9dfeSDavid Greenman sc->rx_idle_secs++; 2025397f9dfeSDavid Greenman } 20263ba65732SDavid Greenman ifp->if_ierrors += 202783e6547dSMaxime Henrion le32toh(sp->rx_crc_errors) + 202883e6547dSMaxime Henrion le32toh(sp->rx_alignment_errors) + 202983e6547dSMaxime Henrion le32toh(sp->rx_rnr_errors) + 203083e6547dSMaxime Henrion le32toh(sp->rx_overrun_errors); 2031a17c678eSDavid Greenman /* 2032f9be9005SDavid Greenman * If any transmit underruns occured, bump up the transmit 2033f9be9005SDavid Greenman * threshold by another 512 bytes (64 * 8). 2034f9be9005SDavid Greenman */ 2035f9be9005SDavid Greenman if (sp->tx_underruns) { 203683e6547dSMaxime Henrion ifp->if_oerrors += le32toh(sp->tx_underruns); 2037f9be9005SDavid Greenman if (tx_threshold < 192) 2038f9be9005SDavid Greenman tx_threshold += 64; 2039f9be9005SDavid Greenman } 20404953bccaSNate Lawson 2041397f9dfeSDavid Greenman /* 2042c8cc6fcaSDavid Greenman * Release any xmit buffers that have completed DMA. This isn't 2043c8cc6fcaSDavid Greenman * strictly necessary to do here, but it's advantagous for mbufs 2044c8cc6fcaSDavid Greenman * with external storage to be released in a timely manner rather 2045c8cc6fcaSDavid Greenman * than being defered for a potentially long time. This limits 2046c8cc6fcaSDavid Greenman * the delay to a maximum of one second. 2047c8cc6fcaSDavid Greenman */ 2048b2badf02SMaxime Henrion fxp_txeof(sc); 2049b2badf02SMaxime Henrion 2050c8cc6fcaSDavid Greenman /* 2051397f9dfeSDavid Greenman * If we haven't received any packets in FXP_MAC_RX_IDLE seconds, 2052397f9dfeSDavid Greenman * then assume the receiver has locked up and attempt to clear 2053397f9dfeSDavid Greenman * the condition by reprogramming the multicast filter. This is 2054397f9dfeSDavid Greenman * a work-around for a bug in the 82557 where the receiver locks 2055397f9dfeSDavid Greenman * up if it gets certain types of garbage in the syncronization 2056397f9dfeSDavid Greenman * bits prior to the packet header. This bug is supposed to only 2057397f9dfeSDavid Greenman * occur in 10Mbps mode, but has been seen to occur in 100Mbps 2058397f9dfeSDavid Greenman * mode as well (perhaps due to a 10/100 speed transition). 2059397f9dfeSDavid Greenman */ 2060397f9dfeSDavid Greenman if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) { 2061397f9dfeSDavid Greenman sc->rx_idle_secs = 0; 2062397f9dfeSDavid Greenman fxp_mc_setup(sc); 2063397f9dfeSDavid Greenman } 2064f9be9005SDavid Greenman /* 20653ba65732SDavid Greenman * If there is no pending command, start another stats 20663ba65732SDavid Greenman * dump. Otherwise punt for now. 2067a17c678eSDavid Greenman */ 2068397f9dfeSDavid Greenman if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) { 2069a17c678eSDavid Greenman /* 2070397f9dfeSDavid Greenman * Start another stats dump. 2071a17c678eSDavid Greenman */ 2072b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, 2073b2badf02SMaxime Henrion BUS_DMASYNC_PREREAD); 20742e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET); 2075dfe61cf1SDavid Greenman } else { 2076dfe61cf1SDavid Greenman /* 2077dfe61cf1SDavid Greenman * A previous command is still waiting to be accepted. 2078dfe61cf1SDavid Greenman * Just zero our copy of the stats and wait for the 20793ba65732SDavid Greenman * next timer event to update them. 2080dfe61cf1SDavid Greenman */ 2081dfe61cf1SDavid Greenman sp->tx_good = 0; 2082f9be9005SDavid Greenman sp->tx_underruns = 0; 2083dfe61cf1SDavid Greenman sp->tx_total_collisions = 0; 20843ba65732SDavid Greenman 2085dfe61cf1SDavid Greenman sp->rx_good = 0; 20863ba65732SDavid Greenman sp->rx_crc_errors = 0; 20873ba65732SDavid Greenman sp->rx_alignment_errors = 0; 20883ba65732SDavid Greenman sp->rx_rnr_errors = 0; 20893ba65732SDavid Greenman sp->rx_overrun_errors = 0; 2090dfe61cf1SDavid Greenman } 2091f7788e8eSJonathan Lemon if (sc->miibus != NULL) 2092f7788e8eSJonathan Lemon mii_tick(device_get_softc(sc->miibus)); 20934953bccaSNate Lawson 2094a17c678eSDavid Greenman /* 209516f1e614SRuslan Ermilov * Check that chip hasn't hung. 2096df79d527SGleb Smirnoff */ 2097df79d527SGleb Smirnoff fxp_watchdog(sc); 2098df79d527SGleb Smirnoff 2099df79d527SGleb Smirnoff /* 2100a17c678eSDavid Greenman * Schedule another timeout one second from now. 2101a17c678eSDavid Greenman */ 210245276e4aSSam Leffler callout_reset(&sc->stat_ch, hz, fxp_tick, sc); 2103a17c678eSDavid Greenman } 2104a17c678eSDavid Greenman 2105a17c678eSDavid Greenman /* 2106a17c678eSDavid Greenman * Stop the interface. Cancels the statistics updater and resets 2107a17c678eSDavid Greenman * the interface. 2108a17c678eSDavid Greenman */ 2109a17c678eSDavid Greenman static void 2110f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc) 2111a17c678eSDavid Greenman { 2112fc74a9f9SBrooks Davis struct ifnet *ifp = sc->ifp; 2113b2badf02SMaxime Henrion struct fxp_tx *txp; 21143ba65732SDavid Greenman int i; 2115a17c678eSDavid Greenman 211613f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2117df79d527SGleb Smirnoff sc->watchdog_timer = 0; 21187dced78aSDavid Greenman 2119a17c678eSDavid Greenman /* 2120a17c678eSDavid Greenman * Cancel stats updater. 2121a17c678eSDavid Greenman */ 212245276e4aSSam Leffler callout_stop(&sc->stat_ch); 21233ba65732SDavid Greenman 21243ba65732SDavid Greenman /* 21257137cea0SPyun YongHyeon * Preserve PCI configuration, configure, IA/multicast 21267137cea0SPyun YongHyeon * setup and put RU and CU into idle state. 21273ba65732SDavid Greenman */ 21287137cea0SPyun YongHyeon CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET); 212972a32a26SJonathan Lemon DELAY(50); 21307137cea0SPyun YongHyeon /* Disable interrupts. */ 21317137cea0SPyun YongHyeon CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 2132a17c678eSDavid Greenman 21333ba65732SDavid Greenman /* 21343ba65732SDavid Greenman * Release any xmit buffers. 21353ba65732SDavid Greenman */ 2136b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_list; 2137da91462dSDavid Greenman if (txp != NULL) { 2138da91462dSDavid Greenman for (i = 0; i < FXP_NTXCB; i++) { 2139b2badf02SMaxime Henrion if (txp[i].tx_mbuf != NULL) { 2140a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_txmtag, txp[i].tx_map, 2141b2badf02SMaxime Henrion BUS_DMASYNC_POSTWRITE); 2142a2057a72SPyun YongHyeon bus_dmamap_unload(sc->fxp_txmtag, 2143a2057a72SPyun YongHyeon txp[i].tx_map); 2144b2badf02SMaxime Henrion m_freem(txp[i].tx_mbuf); 2145b2badf02SMaxime Henrion txp[i].tx_mbuf = NULL; 2146c8bca6dcSBill Paul /* clear this to reset csum offload bits */ 2147b2badf02SMaxime Henrion txp[i].tx_cb->tbd[0].tb_addr = 0; 2148da91462dSDavid Greenman } 2149da91462dSDavid Greenman } 21503ba65732SDavid Greenman } 2151a2057a72SPyun YongHyeon bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, 2152a2057a72SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 21533ba65732SDavid Greenman sc->tx_queued = 0; 2154a17c678eSDavid Greenman } 2155a17c678eSDavid Greenman 2156a17c678eSDavid Greenman /* 2157a17c678eSDavid Greenman * Watchdog/transmission transmit timeout handler. Called when a 2158a17c678eSDavid Greenman * transmission is started on the interface, but no interrupt is 2159a17c678eSDavid Greenman * received before the timeout. This usually indicates that the 2160a17c678eSDavid Greenman * card has wedged for some reason. 2161a17c678eSDavid Greenman */ 2162a17c678eSDavid Greenman static void 2163df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc) 2164a17c678eSDavid Greenman { 2165ba8c6fd5SDavid Greenman 2166df79d527SGleb Smirnoff FXP_LOCK_ASSERT(sc, MA_OWNED); 2167df79d527SGleb Smirnoff 2168df79d527SGleb Smirnoff if (sc->watchdog_timer == 0 || --sc->watchdog_timer) 2169df79d527SGleb Smirnoff return; 2170df79d527SGleb Smirnoff 2171f7788e8eSJonathan Lemon device_printf(sc->dev, "device timeout\n"); 2172df79d527SGleb Smirnoff sc->ifp->if_oerrors++; 2173a17c678eSDavid Greenman 21744953bccaSNate Lawson fxp_init_body(sc); 2175a17c678eSDavid Greenman } 2176a17c678eSDavid Greenman 21774953bccaSNate Lawson /* 21784953bccaSNate Lawson * Acquire locks and then call the real initialization function. This 21794953bccaSNate Lawson * is necessary because ether_ioctl() calls if_init() and this would 21804953bccaSNate Lawson * result in mutex recursion if the mutex was held. 21814953bccaSNate Lawson */ 2182a17c678eSDavid Greenman static void 2183f7788e8eSJonathan Lemon fxp_init(void *xsc) 2184a17c678eSDavid Greenman { 2185fb583156SDavid Greenman struct fxp_softc *sc = xsc; 21864953bccaSNate Lawson 21874953bccaSNate Lawson FXP_LOCK(sc); 21884953bccaSNate Lawson fxp_init_body(sc); 21894953bccaSNate Lawson FXP_UNLOCK(sc); 21904953bccaSNate Lawson } 21914953bccaSNate Lawson 21924953bccaSNate Lawson /* 21934953bccaSNate Lawson * Perform device initialization. This routine must be called with the 21944953bccaSNate Lawson * softc lock held. 21954953bccaSNate Lawson */ 21964953bccaSNate Lawson static void 21974953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc) 21984953bccaSNate Lawson { 2199fc74a9f9SBrooks Davis struct ifnet *ifp = sc->ifp; 2200a17c678eSDavid Greenman struct fxp_cb_config *cbp; 2201a17c678eSDavid Greenman struct fxp_cb_ias *cb_ias; 2202b2badf02SMaxime Henrion struct fxp_cb_tx *tcbp; 2203b2badf02SMaxime Henrion struct fxp_tx *txp; 220409882363SJonathan Lemon struct fxp_cb_mcs *mcsp; 22053212724cSJohn Baldwin int i, prm; 2206a17c678eSDavid Greenman 220767fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 2208a17c678eSDavid Greenman /* 22093ba65732SDavid Greenman * Cancel any pending I/O 2210a17c678eSDavid Greenman */ 22113ba65732SDavid Greenman fxp_stop(sc); 2212a17c678eSDavid Greenman 22137137cea0SPyun YongHyeon /* 22147137cea0SPyun YongHyeon * Issue software reset, which also unloads the microcode. 22157137cea0SPyun YongHyeon */ 22167137cea0SPyun YongHyeon sc->flags &= ~FXP_FLAG_UCODE; 22177137cea0SPyun YongHyeon CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET); 22187137cea0SPyun YongHyeon DELAY(50); 22197137cea0SPyun YongHyeon 2220a17c678eSDavid Greenman prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0; 2221a17c678eSDavid Greenman 2222a17c678eSDavid Greenman /* 2223a17c678eSDavid Greenman * Initialize base of CBL and RFA memory. Loading with zero 2224a17c678eSDavid Greenman * sets it up for regular linear addressing. 2225a17c678eSDavid Greenman */ 2226ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0); 22272e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE); 2228a17c678eSDavid Greenman 2229ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 22302e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE); 2231a17c678eSDavid Greenman 2232a17c678eSDavid Greenman /* 2233a17c678eSDavid Greenman * Initialize base of dump-stats buffer. 2234a17c678eSDavid Greenman */ 2235ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 2236b2badf02SMaxime Henrion bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD); 2237b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr); 22382e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR); 2239a17c678eSDavid Greenman 2240a17c678eSDavid Greenman /* 224172a32a26SJonathan Lemon * Attempt to load microcode if requested. 224272a32a26SJonathan Lemon */ 224372a32a26SJonathan Lemon if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0) 224472a32a26SJonathan Lemon fxp_load_ucode(sc); 224572a32a26SJonathan Lemon 224672a32a26SJonathan Lemon /* 224709882363SJonathan Lemon * Initialize the multicast address list. 224809882363SJonathan Lemon */ 224909882363SJonathan Lemon if (fxp_mc_addrs(sc)) { 225009882363SJonathan Lemon mcsp = sc->mcsp; 225109882363SJonathan Lemon mcsp->cb_status = 0; 225283e6547dSMaxime Henrion mcsp->cb_command = 225383e6547dSMaxime Henrion htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL); 225483e6547dSMaxime Henrion mcsp->link_addr = 0xffffffff; 225509882363SJonathan Lemon /* 225609882363SJonathan Lemon * Start the multicast setup command. 225709882363SJonathan Lemon */ 225809882363SJonathan Lemon fxp_scb_wait(sc); 22595986d0d2SPyun YongHyeon bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, 22605986d0d2SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2261b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr); 226209882363SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 226309882363SJonathan Lemon /* ...and wait for it to complete. */ 2264209b07bcSMaxime Henrion fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map); 226509882363SJonathan Lemon } 226609882363SJonathan Lemon 226709882363SJonathan Lemon /* 2268a17c678eSDavid Greenman * We temporarily use memory that contains the TxCB list to 2269a17c678eSDavid Greenman * construct the config CB. The TxCB list memory is rebuilt 2270a17c678eSDavid Greenman * later. 2271a17c678eSDavid Greenman */ 2272b2badf02SMaxime Henrion cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list; 2273a17c678eSDavid Greenman 2274a17c678eSDavid Greenman /* 2275a17c678eSDavid Greenman * This bcopy is kind of disgusting, but there are a bunch of must be 2276a17c678eSDavid Greenman * zero and must be one bits in this structure and this is the easiest 2277a17c678eSDavid Greenman * way to initialize them all to proper values. 2278a17c678eSDavid Greenman */ 2279b2badf02SMaxime Henrion bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template)); 2280a17c678eSDavid Greenman 2281a17c678eSDavid Greenman cbp->cb_status = 0; 228283e6547dSMaxime Henrion cbp->cb_command = htole16(FXP_CB_COMMAND_CONFIG | 228383e6547dSMaxime Henrion FXP_CB_COMMAND_EL); 228483e6547dSMaxime Henrion cbp->link_addr = 0xffffffff; /* (no) next command */ 22852c6c0947SMaxime Henrion cbp->byte_count = sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22; 2286001696daSDavid Greenman cbp->rx_fifo_limit = 8; /* rx fifo threshold (32 bytes) */ 2287001696daSDavid Greenman cbp->tx_fifo_limit = 0; /* tx fifo threshold (0 bytes) */ 2288a17c678eSDavid Greenman cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */ 2289f7788e8eSJonathan Lemon cbp->mwi_enable = sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0; 2290f7788e8eSJonathan Lemon cbp->type_enable = 0; /* actually reserved */ 2291f7788e8eSJonathan Lemon cbp->read_align_en = sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0; 2292f7788e8eSJonathan Lemon cbp->end_wr_on_cl = sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0; 2293001696daSDavid Greenman cbp->rx_dma_bytecount = 0; /* (no) rx DMA max */ 2294001696daSDavid Greenman cbp->tx_dma_bytecount = 0; /* (no) tx DMA max */ 2295f7788e8eSJonathan Lemon cbp->dma_mbce = 0; /* (disable) dma max counters */ 2296a17c678eSDavid Greenman cbp->late_scb = 0; /* (don't) defer SCB update */ 2297f7788e8eSJonathan Lemon cbp->direct_dma_dis = 1; /* disable direct rcv dma mode */ 2298f7788e8eSJonathan Lemon cbp->tno_int_or_tco_en =0; /* (disable) tx not okay interrupt */ 22993114fdb4SDavid Greenman cbp->ci_int = 1; /* interrupt on CU idle */ 2300f7788e8eSJonathan Lemon cbp->ext_txcb_dis = sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1; 2301f7788e8eSJonathan Lemon cbp->ext_stats_dis = 1; /* disable extended counters */ 2302f7788e8eSJonathan Lemon cbp->keep_overrun_rx = 0; /* don't pass overrun frames to host */ 23038ef1f631SYaroslav Tykhiy cbp->save_bf = sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm; 2304a17c678eSDavid Greenman cbp->disc_short_rx = !prm; /* discard short packets */ 2305f7788e8eSJonathan Lemon cbp->underrun_retry = 1; /* retry mode (once) on DMA underrun */ 2306f7788e8eSJonathan Lemon cbp->two_frames = 0; /* do not limit FIFO to 2 frames */ 2307c21e84e4SPyun YongHyeon cbp->dyn_tbd = sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0; 2308c8bca6dcSBill Paul cbp->ext_rfa = sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0; 2309f7788e8eSJonathan Lemon cbp->mediatype = sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1; 2310f7788e8eSJonathan Lemon cbp->csma_dis = 0; /* (don't) disable link */ 2311f13075afSPyun YongHyeon cbp->tcp_udp_cksum = ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 && 2312f13075afSPyun YongHyeon (ifp->if_capenable & IFCAP_RXCSUM) != 0) ? 1 : 0; 2313f7788e8eSJonathan Lemon cbp->vlan_tco = 0; /* (don't) enable vlan wakeup */ 2314f7788e8eSJonathan Lemon cbp->link_wake_en = 0; /* (don't) assert PME# on link change */ 2315f7788e8eSJonathan Lemon cbp->arp_wake_en = 0; /* (don't) assert PME# on arp */ 2316f7788e8eSJonathan Lemon cbp->mc_wake_en = 0; /* (don't) enable PME# on mcmatch */ 2317a17c678eSDavid Greenman cbp->nsai = 1; /* (don't) disable source addr insert */ 2318a17c678eSDavid Greenman cbp->preamble_length = 2; /* (7 byte) preamble */ 2319a17c678eSDavid Greenman cbp->loopback = 0; /* (don't) loopback */ 2320a17c678eSDavid Greenman cbp->linear_priority = 0; /* (normal CSMA/CD operation) */ 2321a17c678eSDavid Greenman cbp->linear_pri_mode = 0; /* (wait after xmit only) */ 2322a17c678eSDavid Greenman cbp->interfrm_spacing = 6; /* (96 bits of) interframe spacing */ 2323a17c678eSDavid Greenman cbp->promiscuous = prm; /* promiscuous mode */ 2324a17c678eSDavid Greenman cbp->bcast_disable = 0; /* (don't) disable broadcasts */ 2325f7788e8eSJonathan Lemon cbp->wait_after_win = 0; /* (don't) enable modified backoff alg*/ 2326f7788e8eSJonathan Lemon cbp->ignore_ul = 0; /* consider U/L bit in IA matching */ 2327f7788e8eSJonathan Lemon cbp->crc16_en = 0; /* (don't) enable crc-16 algorithm */ 2328f7788e8eSJonathan Lemon cbp->crscdt = sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0; 2329f7788e8eSJonathan Lemon 2330a17c678eSDavid Greenman cbp->stripping = !prm; /* truncate rx packet to byte count */ 2331a17c678eSDavid Greenman cbp->padding = 1; /* (do) pad short tx packets */ 2332a17c678eSDavid Greenman cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */ 2333f7788e8eSJonathan Lemon cbp->long_rx_en = sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0; 2334f7788e8eSJonathan Lemon cbp->ia_wake_en = 0; /* (don't) wake up on address match */ 23357137cea0SPyun YongHyeon cbp->magic_pkt_dis = sc->flags & FXP_FLAG_WOL ? 0 : 1; 2336a17c678eSDavid Greenman cbp->force_fdx = 0; /* (don't) force full duplex */ 23373ba65732SDavid Greenman cbp->fdx_pin_en = 1; /* (enable) FDX# pin */ 2338a17c678eSDavid Greenman cbp->multi_ia = 0; /* (don't) accept multiple IAs */ 2339f7788e8eSJonathan Lemon cbp->mc_all = sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0; 2340c8bca6dcSBill Paul cbp->gamla_rx = sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0; 2341bd4fa9d9SPyun YongHyeon cbp->vlan_strip_en = ((sc->flags & FXP_FLAG_EXT_RFA) != 0 && 2342bd4fa9d9SPyun YongHyeon (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) ? 1 : 0; 2343a17c678eSDavid Greenman 23440f1db1d6SMaxime Henrion if (sc->tunable_noflow || sc->revision == FXP_REV_82557) { 23453bd07cfdSJonathan Lemon /* 23463bd07cfdSJonathan Lemon * The 82557 has no hardware flow control, the values 23473bd07cfdSJonathan Lemon * below are the defaults for the chip. 23483bd07cfdSJonathan Lemon */ 23493bd07cfdSJonathan Lemon cbp->fc_delay_lsb = 0; 23503bd07cfdSJonathan Lemon cbp->fc_delay_msb = 0x40; 23513bd07cfdSJonathan Lemon cbp->pri_fc_thresh = 3; 23523bd07cfdSJonathan Lemon cbp->tx_fc_dis = 0; 23533bd07cfdSJonathan Lemon cbp->rx_fc_restop = 0; 23543bd07cfdSJonathan Lemon cbp->rx_fc_restart = 0; 23553bd07cfdSJonathan Lemon cbp->fc_filter = 0; 23563bd07cfdSJonathan Lemon cbp->pri_fc_loc = 1; 23573bd07cfdSJonathan Lemon } else { 23583bd07cfdSJonathan Lemon cbp->fc_delay_lsb = 0x1f; 23593bd07cfdSJonathan Lemon cbp->fc_delay_msb = 0x01; 23603bd07cfdSJonathan Lemon cbp->pri_fc_thresh = 3; 23613bd07cfdSJonathan Lemon cbp->tx_fc_dis = 0; /* enable transmit FC */ 23623bd07cfdSJonathan Lemon cbp->rx_fc_restop = 1; /* enable FC restop frames */ 23633bd07cfdSJonathan Lemon cbp->rx_fc_restart = 1; /* enable FC restart frames */ 23643bd07cfdSJonathan Lemon cbp->fc_filter = !prm; /* drop FC frames to host */ 23653bd07cfdSJonathan Lemon cbp->pri_fc_loc = 1; /* FC pri location (byte31) */ 23663bd07cfdSJonathan Lemon } 23673bd07cfdSJonathan Lemon 2368a17c678eSDavid Greenman /* 2369a17c678eSDavid Greenman * Start the config command/DMA. 2370a17c678eSDavid Greenman */ 2371ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 23725986d0d2SPyun YongHyeon bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, 23735986d0d2SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2374b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr); 23752e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2376a17c678eSDavid Greenman /* ...and wait for it to complete. */ 2377209b07bcSMaxime Henrion fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map); 2378a17c678eSDavid Greenman 2379a17c678eSDavid Greenman /* 2380a17c678eSDavid Greenman * Now initialize the station address. Temporarily use the TxCB 2381a17c678eSDavid Greenman * memory area like we did above for the config CB. 2382a17c678eSDavid Greenman */ 2383b2badf02SMaxime Henrion cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list; 2384a17c678eSDavid Greenman cb_ias->cb_status = 0; 238583e6547dSMaxime Henrion cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL); 238683e6547dSMaxime Henrion cb_ias->link_addr = 0xffffffff; 23874a0d6638SRuslan Ermilov bcopy(IF_LLADDR(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN); 2388a17c678eSDavid Greenman 2389a17c678eSDavid Greenman /* 2390a17c678eSDavid Greenman * Start the IAS (Individual Address Setup) command/DMA. 2391a17c678eSDavid Greenman */ 2392ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 23935986d0d2SPyun YongHyeon bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, 23945986d0d2SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 23952e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2396a17c678eSDavid Greenman /* ...and wait for it to complete. */ 2397209b07bcSMaxime Henrion fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map); 2398a17c678eSDavid Greenman 2399a17c678eSDavid Greenman /* 2400a17c678eSDavid Greenman * Initialize transmit control block (TxCB) list. 2401a17c678eSDavid Greenman */ 2402b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_list; 2403b2badf02SMaxime Henrion tcbp = sc->fxp_desc.cbl_list; 2404b2badf02SMaxime Henrion bzero(tcbp, FXP_TXCB_SZ); 2405a17c678eSDavid Greenman for (i = 0; i < FXP_NTXCB; i++) { 2406b2badf02SMaxime Henrion txp[i].tx_mbuf = NULL; 240783e6547dSMaxime Henrion tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK); 240883e6547dSMaxime Henrion tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP); 240983e6547dSMaxime Henrion tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr + 241083e6547dSMaxime Henrion (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx))); 24113bd07cfdSJonathan Lemon if (sc->flags & FXP_FLAG_EXT_TXCB) 2412b2badf02SMaxime Henrion tcbp[i].tbd_array_addr = 241383e6547dSMaxime Henrion htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2])); 24143bd07cfdSJonathan Lemon else 2415b2badf02SMaxime Henrion tcbp[i].tbd_array_addr = 241683e6547dSMaxime Henrion htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0])); 2417b2badf02SMaxime Henrion txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK]; 2418a17c678eSDavid Greenman } 2419a17c678eSDavid Greenman /* 2420397f9dfeSDavid Greenman * Set the suspend flag on the first TxCB and start the control 2421a17c678eSDavid Greenman * unit. It will execute the NOP and then suspend. 2422a17c678eSDavid Greenman */ 242383e6547dSMaxime Henrion tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S); 2424a2057a72SPyun YongHyeon bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, 2425a2057a72SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2426b2badf02SMaxime Henrion sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp; 2427397f9dfeSDavid Greenman sc->tx_queued = 1; 2428a17c678eSDavid Greenman 2429ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 24302e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2431a17c678eSDavid Greenman 2432a17c678eSDavid Greenman /* 2433a17c678eSDavid Greenman * Initialize receiver buffer area - RFA. 2434a17c678eSDavid Greenman */ 2435ba8c6fd5SDavid Greenman fxp_scb_wait(sc); 2436b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr); 24372e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START); 2438a17c678eSDavid Greenman 2439dccee1a1SDavid Greenman /* 2440ba8c6fd5SDavid Greenman * Set current media. 2441dccee1a1SDavid Greenman */ 2442f7788e8eSJonathan Lemon if (sc->miibus != NULL) 2443f7788e8eSJonathan Lemon mii_mediachg(device_get_softc(sc->miibus)); 2444dccee1a1SDavid Greenman 244513f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 244613f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2447e8c8b728SJonathan Lemon 2448e8c8b728SJonathan Lemon /* 2449e8c8b728SJonathan Lemon * Enable interrupts. 2450e8c8b728SJonathan Lemon */ 24512b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING 24522b5989e9SLuigi Rizzo /* 24532b5989e9SLuigi Rizzo * ... but only do that if we are not polling. And because (presumably) 24542b5989e9SLuigi Rizzo * the default is interrupts on, we need to disable them explicitly! 24552b5989e9SLuigi Rizzo */ 245640929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING ) 24572b5989e9SLuigi Rizzo CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); 24582b5989e9SLuigi Rizzo else 24592b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */ 2460e8c8b728SJonathan Lemon CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0); 2461a17c678eSDavid Greenman 2462a17c678eSDavid Greenman /* 2463a17c678eSDavid Greenman * Start stats updater. 2464a17c678eSDavid Greenman */ 246545276e4aSSam Leffler callout_reset(&sc->stat_ch, hz, fxp_tick, sc); 2466f7788e8eSJonathan Lemon } 2467f7788e8eSJonathan Lemon 2468f7788e8eSJonathan Lemon static int 2469f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp) 2470f7788e8eSJonathan Lemon { 2471f7788e8eSJonathan Lemon 2472f7788e8eSJonathan Lemon return (0); 2473a17c678eSDavid Greenman } 2474a17c678eSDavid Greenman 2475303b270bSEivind Eklund static void 2476f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2477ba8c6fd5SDavid Greenman { 2478ba8c6fd5SDavid Greenman 2479f7788e8eSJonathan Lemon ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2480ba8c6fd5SDavid Greenman } 2481ba8c6fd5SDavid Greenman 2482ba8c6fd5SDavid Greenman /* 2483ba8c6fd5SDavid Greenman * Change media according to request. 2484ba8c6fd5SDavid Greenman */ 2485f7788e8eSJonathan Lemon static int 2486f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp) 2487ba8c6fd5SDavid Greenman { 2488ba8c6fd5SDavid Greenman struct fxp_softc *sc = ifp->if_softc; 2489f7788e8eSJonathan Lemon struct mii_data *mii; 2490ba8c6fd5SDavid Greenman 2491f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 24923212724cSJohn Baldwin FXP_LOCK(sc); 24935aa0cdf4SJohn-Mark Gurney if (mii->mii_instance) { 24945aa0cdf4SJohn-Mark Gurney struct mii_softc *miisc; 24955aa0cdf4SJohn-Mark Gurney LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 24965aa0cdf4SJohn-Mark Gurney mii_phy_reset(miisc); 24975aa0cdf4SJohn-Mark Gurney } 2498f7788e8eSJonathan Lemon mii_mediachg(mii); 24993212724cSJohn Baldwin FXP_UNLOCK(sc); 2500ba8c6fd5SDavid Greenman return (0); 2501ba8c6fd5SDavid Greenman } 2502ba8c6fd5SDavid Greenman 2503ba8c6fd5SDavid Greenman /* 2504ba8c6fd5SDavid Greenman * Notify the world which media we're using. 2505ba8c6fd5SDavid Greenman */ 2506f7788e8eSJonathan Lemon static void 2507f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2508ba8c6fd5SDavid Greenman { 2509ba8c6fd5SDavid Greenman struct fxp_softc *sc = ifp->if_softc; 2510f7788e8eSJonathan Lemon struct mii_data *mii; 2511ba8c6fd5SDavid Greenman 2512f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 25133212724cSJohn Baldwin FXP_LOCK(sc); 2514f7788e8eSJonathan Lemon mii_pollstat(mii); 2515f7788e8eSJonathan Lemon ifmr->ifm_active = mii->mii_media_active; 2516f7788e8eSJonathan Lemon ifmr->ifm_status = mii->mii_media_status; 25172e2b8238SJonathan Lemon 25182b6fb51fSWarner Losh if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T && 25192b6fb51fSWarner Losh sc->flags & FXP_FLAG_CU_RESUME_BUG) 25202e2b8238SJonathan Lemon sc->cu_resume_bug = 1; 25212e2b8238SJonathan Lemon else 25222e2b8238SJonathan Lemon sc->cu_resume_bug = 0; 25233212724cSJohn Baldwin FXP_UNLOCK(sc); 2524ba8c6fd5SDavid Greenman } 2525ba8c6fd5SDavid Greenman 2526a17c678eSDavid Greenman /* 2527a17c678eSDavid Greenman * Add a buffer to the end of the RFA buffer list. 2528a17c678eSDavid Greenman * Return 0 if successful, 1 for failure. A failure results in 252985050421SPyun YongHyeon * reusing the RFA buffer. 2530a17c678eSDavid Greenman * The RFA struct is stuck at the beginning of mbuf cluster and the 2531a17c678eSDavid Greenman * data pointer is fixed up to point just past it. 2532a17c678eSDavid Greenman */ 2533a17c678eSDavid Greenman static int 253485050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp) 2535a17c678eSDavid Greenman { 2536a17c678eSDavid Greenman struct mbuf *m; 253785050421SPyun YongHyeon struct fxp_rfa *rfa; 2538b2badf02SMaxime Henrion bus_dmamap_t tmp_map; 253985050421SPyun YongHyeon int error; 2540a17c678eSDavid Greenman 2541a163d034SWarner Losh m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 254285050421SPyun YongHyeon if (m == NULL) 254385050421SPyun YongHyeon return (ENOBUFS); 2544ba8c6fd5SDavid Greenman 2545ba8c6fd5SDavid Greenman /* 2546ba8c6fd5SDavid Greenman * Move the data pointer up so that the incoming data packet 2547ba8c6fd5SDavid Greenman * will be 32-bit aligned. 2548ba8c6fd5SDavid Greenman */ 2549ba8c6fd5SDavid Greenman m->m_data += RFA_ALIGNMENT_FUDGE; 2550ba8c6fd5SDavid Greenman 2551eadd5e3aSDavid Greenman /* 2552eadd5e3aSDavid Greenman * Get a pointer to the base of the mbuf cluster and move 2553eadd5e3aSDavid Greenman * data start past it. 2554eadd5e3aSDavid Greenman */ 2555a17c678eSDavid Greenman rfa = mtod(m, struct fxp_rfa *); 2556c8bca6dcSBill Paul m->m_data += sc->rfa_size; 255783e6547dSMaxime Henrion rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE); 2558eadd5e3aSDavid Greenman 2559a17c678eSDavid Greenman rfa->rfa_status = 0; 256083e6547dSMaxime Henrion rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL); 2561a17c678eSDavid Greenman rfa->actual_size = 0; 256285050421SPyun YongHyeon m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE - 256385050421SPyun YongHyeon sc->rfa_size; 2564ba8c6fd5SDavid Greenman 256528935f27SMaxime Henrion /* 256628935f27SMaxime Henrion * Initialize the rest of the RFA. Note that since the RFA 256728935f27SMaxime Henrion * is misaligned, we cannot store values directly. We're thus 256828935f27SMaxime Henrion * using the le32enc() function which handles endianness and 256928935f27SMaxime Henrion * is also alignment-safe. 257028935f27SMaxime Henrion */ 257183e6547dSMaxime Henrion le32enc(&rfa->link_addr, 0xffffffff); 257283e6547dSMaxime Henrion le32enc(&rfa->rbd_addr, 0xffffffff); 2573ba8c6fd5SDavid Greenman 2574b2badf02SMaxime Henrion /* Map the RFA into DMA memory. */ 2575a2057a72SPyun YongHyeon error = bus_dmamap_load(sc->fxp_rxmtag, sc->spare_map, rfa, 2576b2badf02SMaxime Henrion MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr, 2577b2badf02SMaxime Henrion &rxp->rx_addr, 0); 2578b2badf02SMaxime Henrion if (error) { 2579b2badf02SMaxime Henrion m_freem(m); 2580b2badf02SMaxime Henrion return (error); 2581b2badf02SMaxime Henrion } 2582b2badf02SMaxime Henrion 2583e2157cf7SPyun YongHyeon if (rxp->rx_mbuf != NULL) 2584a2057a72SPyun YongHyeon bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map); 2585b2badf02SMaxime Henrion tmp_map = sc->spare_map; 2586b2badf02SMaxime Henrion sc->spare_map = rxp->rx_map; 2587b2badf02SMaxime Henrion rxp->rx_map = tmp_map; 2588b2badf02SMaxime Henrion rxp->rx_mbuf = m; 2589b2badf02SMaxime Henrion 2590a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map, 2591b983c7b3SMaxime Henrion BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 259285050421SPyun YongHyeon return (0); 259385050421SPyun YongHyeon } 259485050421SPyun YongHyeon 259585050421SPyun YongHyeon static void 259685050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp) 259785050421SPyun YongHyeon { 259885050421SPyun YongHyeon struct fxp_rfa *p_rfa; 259985050421SPyun YongHyeon struct fxp_rx *p_rx; 2600b2badf02SMaxime Henrion 2601dfe61cf1SDavid Greenman /* 2602dfe61cf1SDavid Greenman * If there are other buffers already on the list, attach this 2603dfe61cf1SDavid Greenman * one to the end by fixing up the tail to point to this one. 2604dfe61cf1SDavid Greenman */ 2605b2badf02SMaxime Henrion if (sc->fxp_desc.rx_head != NULL) { 2606b2badf02SMaxime Henrion p_rx = sc->fxp_desc.rx_tail; 2607b2badf02SMaxime Henrion p_rfa = (struct fxp_rfa *) 2608b2badf02SMaxime Henrion (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE); 2609b2badf02SMaxime Henrion p_rx->rx_next = rxp; 261083e6547dSMaxime Henrion le32enc(&p_rfa->link_addr, rxp->rx_addr); 2611aed53495SDavid Greenman p_rfa->rfa_control = 0; 2612a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_rxmtag, p_rx->rx_map, 26134cec1653SMaxime Henrion BUS_DMASYNC_PREWRITE); 2614a17c678eSDavid Greenman } else { 2615b2badf02SMaxime Henrion rxp->rx_next = NULL; 2616b2badf02SMaxime Henrion sc->fxp_desc.rx_head = rxp; 2617a17c678eSDavid Greenman } 2618b2badf02SMaxime Henrion sc->fxp_desc.rx_tail = rxp; 261985050421SPyun YongHyeon } 262085050421SPyun YongHyeon 262185050421SPyun YongHyeon static void 262285050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp) 262385050421SPyun YongHyeon { 262485050421SPyun YongHyeon struct mbuf *m; 262585050421SPyun YongHyeon struct fxp_rfa *rfa; 262685050421SPyun YongHyeon 262785050421SPyun YongHyeon m = rxp->rx_mbuf; 262885050421SPyun YongHyeon m->m_data = m->m_ext.ext_buf; 262985050421SPyun YongHyeon /* 263085050421SPyun YongHyeon * Move the data pointer up so that the incoming data packet 263185050421SPyun YongHyeon * will be 32-bit aligned. 263285050421SPyun YongHyeon */ 263385050421SPyun YongHyeon m->m_data += RFA_ALIGNMENT_FUDGE; 263485050421SPyun YongHyeon 263585050421SPyun YongHyeon /* 263685050421SPyun YongHyeon * Get a pointer to the base of the mbuf cluster and move 263785050421SPyun YongHyeon * data start past it. 263885050421SPyun YongHyeon */ 263985050421SPyun YongHyeon rfa = mtod(m, struct fxp_rfa *); 264085050421SPyun YongHyeon m->m_data += sc->rfa_size; 264185050421SPyun YongHyeon rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE); 264285050421SPyun YongHyeon 264385050421SPyun YongHyeon rfa->rfa_status = 0; 264485050421SPyun YongHyeon rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL); 264585050421SPyun YongHyeon rfa->actual_size = 0; 264685050421SPyun YongHyeon 264785050421SPyun YongHyeon /* 264885050421SPyun YongHyeon * Initialize the rest of the RFA. Note that since the RFA 264985050421SPyun YongHyeon * is misaligned, we cannot store values directly. We're thus 265085050421SPyun YongHyeon * using the le32enc() function which handles endianness and 265185050421SPyun YongHyeon * is also alignment-safe. 265285050421SPyun YongHyeon */ 265385050421SPyun YongHyeon le32enc(&rfa->link_addr, 0xffffffff); 265485050421SPyun YongHyeon le32enc(&rfa->rbd_addr, 0xffffffff); 265585050421SPyun YongHyeon 2656a2057a72SPyun YongHyeon bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map, 265785050421SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2658a17c678eSDavid Greenman } 2659a17c678eSDavid Greenman 2660f1928b0cSKevin Lo static int 2661f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg) 2662dccee1a1SDavid Greenman { 2663f7788e8eSJonathan Lemon struct fxp_softc *sc = device_get_softc(dev); 2664dccee1a1SDavid Greenman int count = 10000; 26656ebc3153SDavid Greenman int value; 2666dccee1a1SDavid Greenman 2667ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_MDICONTROL, 2668ba8c6fd5SDavid Greenman (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21)); 2669dccee1a1SDavid Greenman 2670ba8c6fd5SDavid Greenman while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0 2671ba8c6fd5SDavid Greenman && count--) 26726ebc3153SDavid Greenman DELAY(10); 2673dccee1a1SDavid Greenman 2674dccee1a1SDavid Greenman if (count <= 0) 2675f7788e8eSJonathan Lemon device_printf(dev, "fxp_miibus_readreg: timed out\n"); 2676dccee1a1SDavid Greenman 26776ebc3153SDavid Greenman return (value & 0xffff); 2678dccee1a1SDavid Greenman } 2679dccee1a1SDavid Greenman 268016ec4b00SWarner Losh static int 2681f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value) 2682dccee1a1SDavid Greenman { 2683f7788e8eSJonathan Lemon struct fxp_softc *sc = device_get_softc(dev); 2684dccee1a1SDavid Greenman int count = 10000; 2685dccee1a1SDavid Greenman 2686ba8c6fd5SDavid Greenman CSR_WRITE_4(sc, FXP_CSR_MDICONTROL, 2687ba8c6fd5SDavid Greenman (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) | 2688ba8c6fd5SDavid Greenman (value & 0xffff)); 2689dccee1a1SDavid Greenman 2690ba8c6fd5SDavid Greenman while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 && 2691ba8c6fd5SDavid Greenman count--) 26926ebc3153SDavid Greenman DELAY(10); 2693dccee1a1SDavid Greenman 2694dccee1a1SDavid Greenman if (count <= 0) 2695f7788e8eSJonathan Lemon device_printf(dev, "fxp_miibus_writereg: timed out\n"); 269616ec4b00SWarner Losh return (0); 2697dccee1a1SDavid Greenman } 2698dccee1a1SDavid Greenman 2699dccee1a1SDavid Greenman static int 2700f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 2701a17c678eSDavid Greenman { 27029b44ff22SGarrett Wollman struct fxp_softc *sc = ifp->if_softc; 2703a17c678eSDavid Greenman struct ifreq *ifr = (struct ifreq *)data; 2704f7788e8eSJonathan Lemon struct mii_data *mii; 270560bb79ebSPyun YongHyeon int flag, mask, error = 0, reinit; 2706a17c678eSDavid Greenman 2707a17c678eSDavid Greenman switch (command) { 2708a17c678eSDavid Greenman case SIOCSIFFLAGS: 27093212724cSJohn Baldwin FXP_LOCK(sc); 2710f7788e8eSJonathan Lemon if (ifp->if_flags & IFF_ALLMULTI) 2711f7788e8eSJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 2712f7788e8eSJonathan Lemon else 2713f7788e8eSJonathan Lemon sc->flags &= ~FXP_FLAG_ALL_MCAST; 2714a17c678eSDavid Greenman 2715a17c678eSDavid Greenman /* 2716a17c678eSDavid Greenman * If interface is marked up and not running, then start it. 2717a17c678eSDavid Greenman * If it is marked down and running, stop it. 2718a17c678eSDavid Greenman * XXX If it's up then re-initialize it. This is so flags 2719a17c678eSDavid Greenman * such as IFF_PROMISC are handled. 2720a17c678eSDavid Greenman */ 2721a17c678eSDavid Greenman if (ifp->if_flags & IFF_UP) { 27224953bccaSNate Lawson fxp_init_body(sc); 2723a17c678eSDavid Greenman } else { 272413f4c340SRobert Watson if (ifp->if_drv_flags & IFF_DRV_RUNNING) 27254a5f1499SDavid Greenman fxp_stop(sc); 2726a17c678eSDavid Greenman } 27273212724cSJohn Baldwin FXP_UNLOCK(sc); 2728a17c678eSDavid Greenman break; 2729a17c678eSDavid Greenman 2730a17c678eSDavid Greenman case SIOCADDMULTI: 2731a17c678eSDavid Greenman case SIOCDELMULTI: 27323212724cSJohn Baldwin FXP_LOCK(sc); 2733f7788e8eSJonathan Lemon if (ifp->if_flags & IFF_ALLMULTI) 2734f7788e8eSJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 2735f7788e8eSJonathan Lemon else 2736f7788e8eSJonathan Lemon sc->flags &= ~FXP_FLAG_ALL_MCAST; 2737a17c678eSDavid Greenman /* 2738a17c678eSDavid Greenman * Multicast list has changed; set the hardware filter 2739a17c678eSDavid Greenman * accordingly. 2740a17c678eSDavid Greenman */ 2741f7788e8eSJonathan Lemon if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) 2742397f9dfeSDavid Greenman fxp_mc_setup(sc); 2743397f9dfeSDavid Greenman /* 2744f7788e8eSJonathan Lemon * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it 2745397f9dfeSDavid Greenman * again rather than else {}. 2746397f9dfeSDavid Greenman */ 2747f7788e8eSJonathan Lemon if (sc->flags & FXP_FLAG_ALL_MCAST) 27484953bccaSNate Lawson fxp_init_body(sc); 27493212724cSJohn Baldwin FXP_UNLOCK(sc); 2750a17c678eSDavid Greenman error = 0; 2751ba8c6fd5SDavid Greenman break; 2752ba8c6fd5SDavid Greenman 2753ba8c6fd5SDavid Greenman case SIOCSIFMEDIA: 2754ba8c6fd5SDavid Greenman case SIOCGIFMEDIA: 2755f7788e8eSJonathan Lemon if (sc->miibus != NULL) { 2756f7788e8eSJonathan Lemon mii = device_get_softc(sc->miibus); 2757f7788e8eSJonathan Lemon error = ifmedia_ioctl(ifp, ifr, 2758f7788e8eSJonathan Lemon &mii->mii_media, command); 2759f7788e8eSJonathan Lemon } else { 2760ba8c6fd5SDavid Greenman error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command); 2761f7788e8eSJonathan Lemon } 2762a17c678eSDavid Greenman break; 2763a17c678eSDavid Greenman 2764fb917226SRuslan Ermilov case SIOCSIFCAP: 276560bb79ebSPyun YongHyeon reinit = 0; 27668ef1f631SYaroslav Tykhiy mask = ifp->if_capenable ^ ifr->ifr_reqcap; 276740929967SGleb Smirnoff #ifdef DEVICE_POLLING 276840929967SGleb Smirnoff if (mask & IFCAP_POLLING) { 276940929967SGleb Smirnoff if (ifr->ifr_reqcap & IFCAP_POLLING) { 277040929967SGleb Smirnoff error = ether_poll_register(fxp_poll, ifp); 277140929967SGleb Smirnoff if (error) 277240929967SGleb Smirnoff return(error); 277340929967SGleb Smirnoff FXP_LOCK(sc); 277440929967SGleb Smirnoff CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 277540929967SGleb Smirnoff FXP_SCB_INTR_DISABLE); 277640929967SGleb Smirnoff ifp->if_capenable |= IFCAP_POLLING; 277740929967SGleb Smirnoff FXP_UNLOCK(sc); 277840929967SGleb Smirnoff } else { 277940929967SGleb Smirnoff error = ether_poll_deregister(ifp); 278040929967SGleb Smirnoff /* Enable interrupts in any case */ 278140929967SGleb Smirnoff FXP_LOCK(sc); 278240929967SGleb Smirnoff CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0); 278340929967SGleb Smirnoff ifp->if_capenable &= ~IFCAP_POLLING; 278440929967SGleb Smirnoff FXP_UNLOCK(sc); 278540929967SGleb Smirnoff } 278640929967SGleb Smirnoff } 278740929967SGleb Smirnoff #endif 278840929967SGleb Smirnoff FXP_LOCK(sc); 278960bb79ebSPyun YongHyeon if ((mask & IFCAP_TXCSUM) != 0 && 279060bb79ebSPyun YongHyeon (ifp->if_capabilities & IFCAP_TXCSUM) != 0) { 279160bb79ebSPyun YongHyeon ifp->if_capenable ^= IFCAP_TXCSUM; 279260bb79ebSPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 279360bb79ebSPyun YongHyeon ifp->if_hwassist |= FXP_CSUM_FEATURES; 279460bb79ebSPyun YongHyeon else 279560bb79ebSPyun YongHyeon ifp->if_hwassist &= ~FXP_CSUM_FEATURES; 279660bb79ebSPyun YongHyeon } 279760bb79ebSPyun YongHyeon if ((mask & IFCAP_RXCSUM) != 0 && 2798f13075afSPyun YongHyeon (ifp->if_capabilities & IFCAP_RXCSUM) != 0) { 279960bb79ebSPyun YongHyeon ifp->if_capenable ^= IFCAP_RXCSUM; 2800f13075afSPyun YongHyeon if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0) 2801f13075afSPyun YongHyeon reinit++; 2802f13075afSPyun YongHyeon } 2803c21e84e4SPyun YongHyeon if ((mask & IFCAP_TSO4) != 0 && 2804c21e84e4SPyun YongHyeon (ifp->if_capabilities & IFCAP_TSO4) != 0) { 2805c21e84e4SPyun YongHyeon ifp->if_capenable ^= IFCAP_TSO4; 2806c21e84e4SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TSO4) != 0) 2807c21e84e4SPyun YongHyeon ifp->if_hwassist |= CSUM_TSO; 2808c21e84e4SPyun YongHyeon else 2809c21e84e4SPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 2810c21e84e4SPyun YongHyeon } 28117137cea0SPyun YongHyeon if ((mask & IFCAP_WOL_MAGIC) != 0 && 28127137cea0SPyun YongHyeon (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0) 28137137cea0SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MAGIC; 281460bb79ebSPyun YongHyeon if ((mask & IFCAP_VLAN_MTU) != 0 && 281560bb79ebSPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0) { 28168ef1f631SYaroslav Tykhiy ifp->if_capenable ^= IFCAP_VLAN_MTU; 28178ef1f631SYaroslav Tykhiy if (sc->revision != FXP_REV_82557) 28188ef1f631SYaroslav Tykhiy flag = FXP_FLAG_LONG_PKT_EN; 28198ef1f631SYaroslav Tykhiy else /* a hack to get long frames on the old chip */ 28208ef1f631SYaroslav Tykhiy flag = FXP_FLAG_SAVE_BAD; 28218ef1f631SYaroslav Tykhiy sc->flags ^= flag; 28228ef1f631SYaroslav Tykhiy if (ifp->if_flags & IFF_UP) 282360bb79ebSPyun YongHyeon reinit++; 282460bb79ebSPyun YongHyeon } 2825bd4fa9d9SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTAGGING) != 0 && 2826bd4fa9d9SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) { 2827bd4fa9d9SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 2828bd4fa9d9SPyun YongHyeon reinit++; 2829bd4fa9d9SPyun YongHyeon } 2830bd4fa9d9SPyun YongHyeon if (reinit > 0 && ifp->if_flags & IFF_UP) 28318ef1f631SYaroslav Tykhiy fxp_init_body(sc); 28323212724cSJohn Baldwin FXP_UNLOCK(sc); 2833bd4fa9d9SPyun YongHyeon VLAN_CAPABILITIES(ifp); 2834fb917226SRuslan Ermilov break; 2835fb917226SRuslan Ermilov 2836a17c678eSDavid Greenman default: 2837673d9191SSam Leffler error = ether_ioctl(ifp, command, data); 2838a17c678eSDavid Greenman } 2839a17c678eSDavid Greenman return (error); 2840a17c678eSDavid Greenman } 2841397f9dfeSDavid Greenman 2842397f9dfeSDavid Greenman /* 284309882363SJonathan Lemon * Fill in the multicast address list and return number of entries. 284409882363SJonathan Lemon */ 284509882363SJonathan Lemon static int 284609882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc) 284709882363SJonathan Lemon { 284809882363SJonathan Lemon struct fxp_cb_mcs *mcsp = sc->mcsp; 2849fc74a9f9SBrooks Davis struct ifnet *ifp = sc->ifp; 285009882363SJonathan Lemon struct ifmultiaddr *ifma; 285109882363SJonathan Lemon int nmcasts; 285209882363SJonathan Lemon 285309882363SJonathan Lemon nmcasts = 0; 285409882363SJonathan Lemon if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) { 285513b203d0SRobert Watson IF_ADDR_LOCK(ifp); 285609882363SJonathan Lemon TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 285709882363SJonathan Lemon if (ifma->ifma_addr->sa_family != AF_LINK) 285809882363SJonathan Lemon continue; 285909882363SJonathan Lemon if (nmcasts >= MAXMCADDR) { 286009882363SJonathan Lemon sc->flags |= FXP_FLAG_ALL_MCAST; 286109882363SJonathan Lemon nmcasts = 0; 286209882363SJonathan Lemon break; 286309882363SJonathan Lemon } 286409882363SJonathan Lemon bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 2865bafb64afSMaxime Henrion &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN); 286609882363SJonathan Lemon nmcasts++; 286709882363SJonathan Lemon } 286813b203d0SRobert Watson IF_ADDR_UNLOCK(ifp); 286909882363SJonathan Lemon } 2870bafb64afSMaxime Henrion mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN); 287109882363SJonathan Lemon return (nmcasts); 287209882363SJonathan Lemon } 287309882363SJonathan Lemon 287409882363SJonathan Lemon /* 2875397f9dfeSDavid Greenman * Program the multicast filter. 2876397f9dfeSDavid Greenman * 2877397f9dfeSDavid Greenman * We have an artificial restriction that the multicast setup command 2878397f9dfeSDavid Greenman * must be the first command in the chain, so we take steps to ensure 28793114fdb4SDavid Greenman * this. By requiring this, it allows us to keep up the performance of 2880397f9dfeSDavid Greenman * the pre-initialized command ring (esp. link pointers) by not actually 2881dc733423SDag-Erling Smørgrav * inserting the mcsetup command in the ring - i.e. its link pointer 2882397f9dfeSDavid Greenman * points to the TxCB ring, but the mcsetup descriptor itself is not part 2883397f9dfeSDavid Greenman * of it. We then can do 'CU_START' on the mcsetup descriptor and have it 2884397f9dfeSDavid Greenman * lead into the regular TxCB ring when it completes. 2885397f9dfeSDavid Greenman * 2886397f9dfeSDavid Greenman * This function must be called at splimp. 2887397f9dfeSDavid Greenman */ 2888397f9dfeSDavid Greenman static void 2889f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc) 2890397f9dfeSDavid Greenman { 2891397f9dfeSDavid Greenman struct fxp_cb_mcs *mcsp = sc->mcsp; 2892b2badf02SMaxime Henrion struct fxp_tx *txp; 28937dced78aSDavid Greenman int count; 2894397f9dfeSDavid Greenman 289567fc050fSMaxime Henrion FXP_LOCK_ASSERT(sc, MA_OWNED); 28963114fdb4SDavid Greenman /* 28973114fdb4SDavid Greenman * If there are queued commands, we must wait until they are all 28983114fdb4SDavid Greenman * completed. If we are already waiting, then add a NOP command 28993114fdb4SDavid Greenman * with interrupt option so that we're notified when all commands 29003114fdb4SDavid Greenman * have been completed - fxp_start() ensures that no additional 29013114fdb4SDavid Greenman * TX commands will be added when need_mcsetup is true. 29023114fdb4SDavid Greenman */ 2903397f9dfeSDavid Greenman if (sc->tx_queued) { 29043114fdb4SDavid Greenman /* 29053114fdb4SDavid Greenman * need_mcsetup will be true if we are already waiting for the 29063114fdb4SDavid Greenman * NOP command to be completed (see below). In this case, bail. 29073114fdb4SDavid Greenman */ 29083114fdb4SDavid Greenman if (sc->need_mcsetup) 29093114fdb4SDavid Greenman return; 2910397f9dfeSDavid Greenman sc->need_mcsetup = 1; 29113114fdb4SDavid Greenman 29123114fdb4SDavid Greenman /* 291372a32a26SJonathan Lemon * Add a NOP command with interrupt so that we are notified 291472a32a26SJonathan Lemon * when all TX commands have been processed. 29153114fdb4SDavid Greenman */ 2916b2badf02SMaxime Henrion txp = sc->fxp_desc.tx_last->tx_next; 2917b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 2918b2badf02SMaxime Henrion txp->tx_cb->cb_status = 0; 291983e6547dSMaxime Henrion txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP | 292083e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 29213114fdb4SDavid Greenman /* 29223114fdb4SDavid Greenman * Advance the end of list forward. 29233114fdb4SDavid Greenman */ 292483e6547dSMaxime Henrion sc->fxp_desc.tx_last->tx_cb->cb_command &= 292583e6547dSMaxime Henrion htole16(~FXP_CB_COMMAND_S); 29265f361cbeSMaxime Henrion bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE); 2927b2badf02SMaxime Henrion sc->fxp_desc.tx_last = txp; 29283114fdb4SDavid Greenman sc->tx_queued++; 29293114fdb4SDavid Greenman /* 29303114fdb4SDavid Greenman * Issue a resume in case the CU has just suspended. 29313114fdb4SDavid Greenman */ 29323114fdb4SDavid Greenman fxp_scb_wait(sc); 29332e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME); 29343114fdb4SDavid Greenman /* 29353114fdb4SDavid Greenman * Set a 5 second timer just in case we don't hear from the 29363114fdb4SDavid Greenman * card again. 29373114fdb4SDavid Greenman */ 2938df79d527SGleb Smirnoff sc->watchdog_timer = 5; 29393114fdb4SDavid Greenman 2940397f9dfeSDavid Greenman return; 2941397f9dfeSDavid Greenman } 2942397f9dfeSDavid Greenman sc->need_mcsetup = 0; 2943397f9dfeSDavid Greenman 2944397f9dfeSDavid Greenman /* 2945397f9dfeSDavid Greenman * Initialize multicast setup descriptor. 2946397f9dfeSDavid Greenman */ 2947397f9dfeSDavid Greenman mcsp->cb_status = 0; 294883e6547dSMaxime Henrion mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | 294983e6547dSMaxime Henrion FXP_CB_COMMAND_S | FXP_CB_COMMAND_I); 295083e6547dSMaxime Henrion mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr); 2951b2badf02SMaxime Henrion txp = &sc->fxp_desc.mcs_tx; 2952b2badf02SMaxime Henrion txp->tx_mbuf = NULL; 2953b2badf02SMaxime Henrion txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp; 2954b2badf02SMaxime Henrion txp->tx_next = sc->fxp_desc.tx_list; 295509882363SJonathan Lemon (void) fxp_mc_addrs(sc); 2956b2badf02SMaxime Henrion sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp; 2957397f9dfeSDavid Greenman sc->tx_queued = 1; 2958397f9dfeSDavid Greenman 2959397f9dfeSDavid Greenman /* 2960397f9dfeSDavid Greenman * Wait until command unit is not active. This should never 2961397f9dfeSDavid Greenman * be the case when nothing is queued, but make sure anyway. 2962397f9dfeSDavid Greenman */ 29637dced78aSDavid Greenman count = 100; 2964397f9dfeSDavid Greenman while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) == 29657dced78aSDavid Greenman FXP_SCB_CUS_ACTIVE && --count) 29667dced78aSDavid Greenman DELAY(10); 29677dced78aSDavid Greenman if (count == 0) { 2968f7788e8eSJonathan Lemon device_printf(sc->dev, "command queue timeout\n"); 29697dced78aSDavid Greenman return; 29707dced78aSDavid Greenman } 2971397f9dfeSDavid Greenman 2972397f9dfeSDavid Greenman /* 2973397f9dfeSDavid Greenman * Start the multicast setup command. 2974397f9dfeSDavid Greenman */ 2975397f9dfeSDavid Greenman fxp_scb_wait(sc); 2976a2057a72SPyun YongHyeon bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, 2977a2057a72SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2978b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr); 29792e2b8238SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 2980397f9dfeSDavid Greenman 2981df79d527SGleb Smirnoff sc->watchdog_timer = 2; 2982397f9dfeSDavid Greenman return; 2983397f9dfeSDavid Greenman } 298472a32a26SJonathan Lemon 298574d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE; 298674d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE; 298774d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE; 298874d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE; 298974d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE; 299074d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE; 2991de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE; 299272a32a26SJonathan Lemon 299374d1ed23SMaxime Henrion #define UCODE(x) x, sizeof(x)/sizeof(uint32_t) 299472a32a26SJonathan Lemon 299572a32a26SJonathan Lemon struct ucode { 299674d1ed23SMaxime Henrion uint32_t revision; 299774d1ed23SMaxime Henrion uint32_t *ucode; 299872a32a26SJonathan Lemon int length; 299972a32a26SJonathan Lemon u_short int_delay_offset; 300072a32a26SJonathan Lemon u_short bundle_max_offset; 300172a32a26SJonathan Lemon } ucode_table[] = { 300272a32a26SJonathan Lemon { FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 }, 300372a32a26SJonathan Lemon { FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 }, 300472a32a26SJonathan Lemon { FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma), 300572a32a26SJonathan Lemon D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD }, 300672a32a26SJonathan Lemon { FXP_REV_82559S_A, UCODE(fxp_ucode_d101s), 300772a32a26SJonathan Lemon D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD }, 300872a32a26SJonathan Lemon { FXP_REV_82550, UCODE(fxp_ucode_d102), 300972a32a26SJonathan Lemon D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD }, 301072a32a26SJonathan Lemon { FXP_REV_82550_C, UCODE(fxp_ucode_d102c), 301172a32a26SJonathan Lemon D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD }, 3012507feeafSMaxime Henrion { FXP_REV_82551_F, UCODE(fxp_ucode_d102e), 3013de571603SMaxime Henrion D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD }, 301472a32a26SJonathan Lemon { 0, NULL, 0, 0, 0 } 301572a32a26SJonathan Lemon }; 301672a32a26SJonathan Lemon 301772a32a26SJonathan Lemon static void 301872a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc) 301972a32a26SJonathan Lemon { 302072a32a26SJonathan Lemon struct ucode *uc; 302172a32a26SJonathan Lemon struct fxp_cb_ucode *cbp; 302294a4f968SPyun YongHyeon int i; 302372a32a26SJonathan Lemon 302472a32a26SJonathan Lemon for (uc = ucode_table; uc->ucode != NULL; uc++) 302572a32a26SJonathan Lemon if (sc->revision == uc->revision) 302672a32a26SJonathan Lemon break; 302772a32a26SJonathan Lemon if (uc->ucode == NULL) 302872a32a26SJonathan Lemon return; 3029b2badf02SMaxime Henrion cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list; 303072a32a26SJonathan Lemon cbp->cb_status = 0; 303183e6547dSMaxime Henrion cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL); 303283e6547dSMaxime Henrion cbp->link_addr = 0xffffffff; /* (no) next command */ 303394a4f968SPyun YongHyeon for (i = 0; i < uc->length; i++) 303494a4f968SPyun YongHyeon cbp->ucode[i] = htole32(uc->ucode[i]); 303572a32a26SJonathan Lemon if (uc->int_delay_offset) 303674d1ed23SMaxime Henrion *(uint16_t *)&cbp->ucode[uc->int_delay_offset] = 303783e6547dSMaxime Henrion htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2); 303872a32a26SJonathan Lemon if (uc->bundle_max_offset) 303974d1ed23SMaxime Henrion *(uint16_t *)&cbp->ucode[uc->bundle_max_offset] = 304083e6547dSMaxime Henrion htole16(sc->tunable_bundle_max); 304172a32a26SJonathan Lemon /* 304272a32a26SJonathan Lemon * Download the ucode to the chip. 304372a32a26SJonathan Lemon */ 304472a32a26SJonathan Lemon fxp_scb_wait(sc); 30455986d0d2SPyun YongHyeon bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, 30465986d0d2SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 3047b2badf02SMaxime Henrion CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr); 304872a32a26SJonathan Lemon fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START); 304972a32a26SJonathan Lemon /* ...and wait for it to complete. */ 3050209b07bcSMaxime Henrion fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map); 305172a32a26SJonathan Lemon device_printf(sc->dev, 305272a32a26SJonathan Lemon "Microcode loaded, int_delay: %d usec bundle_max: %d\n", 305372a32a26SJonathan Lemon sc->tunable_int_delay, 305472a32a26SJonathan Lemon uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max); 305572a32a26SJonathan Lemon sc->flags |= FXP_FLAG_UCODE; 305672a32a26SJonathan Lemon } 305772a32a26SJonathan Lemon 305872a32a26SJonathan Lemon static int 305972a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 306072a32a26SJonathan Lemon { 306172a32a26SJonathan Lemon int error, value; 306272a32a26SJonathan Lemon 306372a32a26SJonathan Lemon value = *(int *)arg1; 306472a32a26SJonathan Lemon error = sysctl_handle_int(oidp, &value, 0, req); 306572a32a26SJonathan Lemon if (error || !req->newptr) 306672a32a26SJonathan Lemon return (error); 306772a32a26SJonathan Lemon if (value < low || value > high) 306872a32a26SJonathan Lemon return (EINVAL); 306972a32a26SJonathan Lemon *(int *)arg1 = value; 307072a32a26SJonathan Lemon return (0); 307172a32a26SJonathan Lemon } 307272a32a26SJonathan Lemon 307372a32a26SJonathan Lemon /* 307472a32a26SJonathan Lemon * Interrupt delay is expressed in microseconds, a multiplier is used 307572a32a26SJonathan Lemon * to convert this to the appropriate clock ticks before using. 307672a32a26SJonathan Lemon */ 307772a32a26SJonathan Lemon static int 307872a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS) 307972a32a26SJonathan Lemon { 308072a32a26SJonathan Lemon return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000)); 308172a32a26SJonathan Lemon } 308272a32a26SJonathan Lemon 308372a32a26SJonathan Lemon static int 308472a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS) 308572a32a26SJonathan Lemon { 308672a32a26SJonathan Lemon return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff)); 308772a32a26SJonathan Lemon } 3088