1 /* $NetBSD: if_jme.c,v 1.13 2010/01/19 22:07:01 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Manuel Bouyer. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /*- 28 * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org> 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions 33 * are met: 34 * 1. Redistributions of source code must retain the above copyright 35 * notice unmodified, this list of conditions, and the following 36 * disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 */ 53 54 55 /* 56 * Driver for JMicron Technologies JMC250 (Giganbit) and JMC260 (Fast) 57 * Ethernet Controllers. 58 */ 59 60 #include <sys/cdefs.h> 61 __KERNEL_RCSID(0, "$NetBSD: if_jme.c,v 1.13 2010/01/19 22:07:01 pooka Exp $"); 62 63 64 #include <sys/param.h> 65 #include <sys/systm.h> 66 #include <sys/mbuf.h> 67 #include <sys/protosw.h> 68 #include <sys/socket.h> 69 #include <sys/ioctl.h> 70 #include <sys/errno.h> 71 #include <sys/malloc.h> 72 #include <sys/kernel.h> 73 #include <sys/proc.h> /* only for declaration of wakeup() used by vm.h */ 74 #include <sys/device.h> 75 #include <sys/syslog.h> 76 #include <sys/sysctl.h> 77 78 #include <net/if.h> 79 #if defined(SIOCSIFMEDIA) 80 #include <net/if_media.h> 81 #endif 82 #include <net/if_types.h> 83 #include <net/if_dl.h> 84 #include <net/route.h> 85 #include <net/netisr.h> 86 87 #include <net/bpf.h> 88 #include <net/bpfdesc.h> 89 90 #include "rnd.h" 91 #if NRND > 0 92 #include <sys/rnd.h> 93 #endif 94 95 #include <netinet/in.h> 96 #include <netinet/in_systm.h> 97 #include <netinet/ip.h> 98 99 #ifdef INET 100 #include <netinet/in_var.h> 101 #endif 102 103 #include <netinet/tcp.h> 104 105 #include <net/if_ether.h> 106 #include <uvm/uvm_extern.h> 107 #if defined(INET) 108 #include <netinet/if_inarp.h> 109 #endif 110 111 #include <sys/bus.h> 112 #include <sys/intr.h> 113 114 #include <dev/pci/pcireg.h> 115 #include <dev/pci/pcivar.h> 116 #include <dev/pci/pcidevs.h> 117 #include <dev/pci/if_jmereg.h> 118 119 #include <dev/mii/mii.h> 120 #include <dev/mii/miivar.h> 121 122 struct jme_product_desc { 123 u_int32_t jme_product; 124 const char *jme_desc; 125 }; 126 127 /* number of entries in transmit and receive rings */ 128 #define JME_NBUFS (PAGE_SIZE / sizeof(struct jme_desc)) 129 130 #define JME_DESC_INC(x, y) ((x) = ((x) + 1) % (y)) 131 132 /* Water mark to kick reclaiming Tx buffers. */ 133 #define JME_TX_DESC_HIWAT (JME_NBUFS - (((JME_NBUFS) * 3) / 10)) 134 135 136 struct jme_softc { 137 device_t jme_dev; /* base device */ 138 bus_space_tag_t jme_bt_mac; 139 bus_space_handle_t jme_bh_mac; /* Mac registers */ 140 bus_space_tag_t jme_bt_phy; 141 bus_space_handle_t jme_bh_phy; /* PHY registers */ 142 bus_space_tag_t jme_bt_misc; 143 bus_space_handle_t jme_bh_misc; /* Misc registers */ 144 bus_dma_tag_t jme_dmatag; 145 bus_dma_segment_t jme_txseg; /* transmit ring seg */ 146 bus_dmamap_t jme_txmap; /* transmit ring DMA map */ 147 struct jme_desc* jme_txring; /* transmit ring */ 148 bus_dmamap_t jme_txmbufm[JME_NBUFS]; /* transmit mbufs DMA map */ 149 struct mbuf *jme_txmbuf[JME_NBUFS]; /* mbufs being transmitted */ 150 int jme_tx_cons; /* transmit ring consumer */ 151 int jme_tx_prod; /* transmit ring producer */ 152 int jme_tx_cnt; /* transmit ring active count */ 153 bus_dma_segment_t jme_rxseg; /* receive ring seg */ 154 bus_dmamap_t jme_rxmap; /* receive ring DMA map */ 155 struct jme_desc* jme_rxring; /* receive ring */ 156 bus_dmamap_t jme_rxmbufm[JME_NBUFS]; /* receive mbufs DMA map */ 157 struct mbuf *jme_rxmbuf[JME_NBUFS]; /* mbufs being received */ 158 int jme_rx_cons; /* receive ring consumer */ 159 int jme_rx_prod; /* receive ring producer */ 160 void* jme_ih; /* our interrupt */ 161 struct ethercom jme_ec; 162 struct callout jme_tick_ch; /* tick callout */ 163 u_int8_t jme_enaddr[ETHER_ADDR_LEN];/* hardware address */ 164 u_int8_t jme_phyaddr; /* address of integrated phy */ 165 u_int8_t jme_chip_rev; /* chip revision */ 166 u_int8_t jme_rev; /* PCI revision */ 167 mii_data_t jme_mii; /* mii bus */ 168 u_int32_t jme_flags; /* device features, see below */ 169 uint32_t jme_txcsr; /* TX config register */ 170 uint32_t jme_rxcsr; /* RX config register */ 171 #if NRND > 0 172 rndsource_element_t rnd_source; 173 #endif 174 /* interrupt coalition parameters */ 175 struct sysctllog *jme_clog; 176 int jme_intrxto; /* interrupt RX timeout */ 177 int jme_intrxct; /* interrupt RX packets counter */ 178 int jme_inttxto; /* interrupt TX timeout */ 179 int jme_inttxct; /* interrupt TX packets counter */ 180 }; 181 182 #define JME_FLAG_FPGA 0x0001 /* FPGA version */ 183 #define JME_FLAG_GIGA 0x0002 /* giga Ethernet capable */ 184 185 186 #define jme_if jme_ec.ec_if 187 #define jme_bpf jme_if.if_bpf 188 189 typedef struct jme_softc jme_softc_t; 190 typedef u_long ioctl_cmd_t; 191 192 static int jme_pci_match(device_t, cfdata_t, void *); 193 static void jme_pci_attach(device_t, device_t, void *); 194 static void jme_intr_rx(jme_softc_t *); 195 static int jme_intr(void *); 196 197 static int jme_ifioctl(struct ifnet *, ioctl_cmd_t, void *); 198 static int jme_mediachange(struct ifnet *); 199 static void jme_ifwatchdog(struct ifnet *); 200 static bool jme_shutdown(device_t, int); 201 202 static void jme_txeof(struct jme_softc *); 203 static void jme_ifstart(struct ifnet *); 204 static void jme_reset(jme_softc_t *); 205 static int jme_ifinit(struct ifnet *); 206 static int jme_init(struct ifnet *, int); 207 static void jme_stop(struct ifnet *, int); 208 // static void jme_restart(void *); 209 static void jme_ticks(void *); 210 static void jme_mac_config(jme_softc_t *); 211 static void jme_set_filter(jme_softc_t *); 212 213 int jme_mii_read(device_t, int, int); 214 void jme_mii_write(device_t, int, int, int); 215 void jme_statchg(device_t); 216 217 static int jme_eeprom_read_byte(struct jme_softc *, uint8_t, uint8_t *); 218 static int jme_eeprom_macaddr(struct jme_softc *); 219 220 #define JME_TIMEOUT 1000 221 #define JME_PHY_TIMEOUT 1000 222 #define JME_EEPROM_TIMEOUT 1000 223 224 static int jme_sysctl_intrxto(SYSCTLFN_PROTO); 225 static int jme_sysctl_intrxct(SYSCTLFN_PROTO); 226 static int jme_sysctl_inttxto(SYSCTLFN_PROTO); 227 static int jme_sysctl_inttxct(SYSCTLFN_PROTO); 228 static int jme_root_num; 229 230 231 CFATTACH_DECL_NEW(jme, sizeof(jme_softc_t), 232 jme_pci_match, jme_pci_attach, NULL, NULL); 233 234 static const struct jme_product_desc jme_products[] = { 235 { PCI_PRODUCT_JMICRON_JMC250, 236 "JMicron JMC250 Gigabit Ethernet Controller" }, 237 { PCI_PRODUCT_JMICRON_JMC260, 238 "JMicron JMC260 Gigabit Ethernet Controller" }, 239 { 0, NULL }, 240 }; 241 242 static const struct jme_product_desc *jme_lookup_product(uint32_t); 243 244 static const struct jme_product_desc * 245 jme_lookup_product(uint32_t id) 246 { 247 const struct jme_product_desc *jp; 248 249 for (jp = jme_products ; jp->jme_desc != NULL; jp++) 250 if (PCI_PRODUCT(id) == jp->jme_product) 251 return jp; 252 253 return NULL; 254 } 255 256 static int 257 jme_pci_match(device_t parent, cfdata_t cf, void *aux) 258 { 259 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 260 261 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_JMICRON) 262 return 0; 263 264 if (jme_lookup_product(pa->pa_id) != NULL) 265 return 1; 266 267 return 0; 268 } 269 270 static void 271 jme_pci_attach(device_t parent, device_t self, void *aux) 272 { 273 jme_softc_t *sc = device_private(self); 274 struct pci_attach_args * const pa = (struct pci_attach_args *)aux; 275 const struct jme_product_desc *jp; 276 struct ifnet * const ifp = &sc->jme_if; 277 bus_space_tag_t iot1, iot2, memt; 278 bus_space_handle_t ioh1, ioh2, memh; 279 bus_size_t size, size2; 280 pci_intr_handle_t intrhandle; 281 const char *intrstr; 282 pcireg_t csr; 283 int nsegs, i; 284 const struct sysctlnode *node; 285 int jme_nodenum; 286 287 sc->jme_dev = self; 288 aprint_normal("\n"); 289 callout_init(&sc->jme_tick_ch, 0); 290 291 jp = jme_lookup_product(pa->pa_id); 292 if (jp == NULL) 293 panic("jme_pci_attach: impossible"); 294 295 if (jp->jme_product == PCI_PRODUCT_JMICRON_JMC250) 296 sc->jme_flags = JME_FLAG_GIGA; 297 298 /* 299 * Map the card space. Try Mem first. 300 */ 301 if (pci_mapreg_map(pa, JME_PCI_BAR0, 302 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 303 0, &memt, &memh, NULL, &size) == 0) { 304 sc->jme_bt_mac = memt; 305 sc->jme_bh_mac = memh; 306 sc->jme_bt_phy = memt; 307 if (bus_space_subregion(memt, memh, JME_PHY_EEPROM_BASE_MEMOFF, 308 JME_PHY_EEPROM_SIZE, &sc->jme_bh_phy) != 0) { 309 aprint_error_dev(self, "can't subregion PHY space\n"); 310 bus_space_unmap(memt, memh, size); 311 return; 312 } 313 sc->jme_bt_misc = memt; 314 if (bus_space_subregion(memt, memh, JME_MISC_BASE_MEMOFF, 315 JME_MISC_SIZE, &sc->jme_bh_misc) != 0) { 316 aprint_error_dev(self, "can't subregion misc space\n"); 317 bus_space_unmap(memt, memh, size); 318 return; 319 } 320 } else { 321 if (pci_mapreg_map(pa, JME_PCI_BAR1, PCI_MAPREG_TYPE_IO, 322 0, &iot1, &ioh1, NULL, &size) != 0) { 323 aprint_error_dev(self, "can't map I/O space 1\n"); 324 return; 325 } 326 sc->jme_bt_mac = iot1; 327 sc->jme_bh_mac = ioh1; 328 if (pci_mapreg_map(pa, JME_PCI_BAR2, PCI_MAPREG_TYPE_IO, 329 0, &iot2, &ioh2, NULL, &size2) != 0) { 330 aprint_error_dev(self, "can't map I/O space 2\n"); 331 bus_space_unmap(iot1, ioh1, size); 332 return; 333 } 334 sc->jme_bt_phy = iot2; 335 sc->jme_bh_phy = ioh2; 336 sc->jme_bt_misc = iot2; 337 if (bus_space_subregion(iot2, ioh2, JME_MISC_BASE_IOOFF, 338 JME_MISC_SIZE, &sc->jme_bh_misc) != 0) { 339 aprint_error_dev(self, "can't subregion misc space\n"); 340 bus_space_unmap(iot1, ioh1, size); 341 bus_space_unmap(iot2, ioh2, size2); 342 return; 343 } 344 } 345 346 if (pci_dma64_available(pa)) 347 sc->jme_dmatag = pa->pa_dmat64; 348 else 349 sc->jme_dmatag = pa->pa_dmat; 350 351 /* Enable the device. */ 352 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 353 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 354 csr | PCI_COMMAND_MASTER_ENABLE); 355 356 aprint_normal_dev(self, "%s\n", jp->jme_desc); 357 358 sc->jme_rev = PCI_REVISION(pa->pa_class); 359 360 csr = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_CHIPMODE); 361 if (((csr & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT) != 362 CHIPMODE_NOT_FPGA) 363 sc->jme_flags |= JME_FLAG_FPGA; 364 sc->jme_chip_rev = (csr & CHIPMODE_REV_MASK) >> CHIPMODE_REV_SHIFT; 365 aprint_verbose_dev(self, "PCI device revision : 0x%x, Chip revision: " 366 "0x%x", sc->jme_rev, sc->jme_chip_rev); 367 if (sc->jme_flags & JME_FLAG_FPGA) 368 aprint_verbose(" FPGA revision: 0x%x", 369 (csr & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT); 370 aprint_verbose("\n"); 371 372 /* 373 * Save PHY address. 374 * Integrated JR0211 has fixed PHY address whereas FPGA version 375 * requires PHY probing to get correct PHY address. 376 */ 377 if ((sc->jme_flags & JME_FLAG_FPGA) == 0) { 378 sc->jme_phyaddr = 379 bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, 380 JME_GPREG0) & GPREG0_PHY_ADDR_MASK; 381 } else 382 sc->jme_phyaddr = 0; 383 384 385 jme_reset(sc); 386 387 /* read mac addr */ 388 if (jme_eeprom_macaddr(sc)) { 389 aprint_error_dev(self, "error reading Ethernet address\n"); 390 /* return; */ 391 } 392 aprint_normal_dev(self, "Ethernet address %s\n", 393 ether_sprintf(sc->jme_enaddr)); 394 395 /* Map and establish interrupts */ 396 if (pci_intr_map(pa, &intrhandle)) { 397 aprint_error_dev(self, "couldn't map interrupt\n"); 398 return; 399 } 400 intrstr = pci_intr_string(pa->pa_pc, intrhandle); 401 sc->jme_if.if_softc = sc; 402 sc->jme_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET, 403 jme_intr, sc); 404 if (sc->jme_ih == NULL) { 405 aprint_error_dev(self, "couldn't establish interrupt"); 406 if (intrstr != NULL) 407 aprint_error(" at %s", intrstr); 408 aprint_error("\n"); 409 return; 410 } 411 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 412 413 /* allocate and map DMA-safe memory for transmit ring */ 414 if (bus_dmamem_alloc(sc->jme_dmatag, PAGE_SIZE, 0, PAGE_SIZE, 415 &sc->jme_txseg, 1, &nsegs, BUS_DMA_NOWAIT) != 0 || 416 bus_dmamem_map(sc->jme_dmatag, &sc->jme_txseg, 417 nsegs, PAGE_SIZE, (void **)&sc->jme_txring, 418 BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0 || 419 bus_dmamap_create(sc->jme_dmatag, PAGE_SIZE, 1, PAGE_SIZE, 0, 420 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->jme_txmap) != 0 || 421 bus_dmamap_load(sc->jme_dmatag, sc->jme_txmap, sc->jme_txring, 422 PAGE_SIZE, NULL, BUS_DMA_NOWAIT) != 0) { 423 aprint_error_dev(self, "can't allocate DMA memory TX ring\n"); 424 return; 425 } 426 /* allocate and map DMA-safe memory for receive ring */ 427 if (bus_dmamem_alloc(sc->jme_dmatag, PAGE_SIZE, 0, PAGE_SIZE, 428 &sc->jme_rxseg, 1, &nsegs, BUS_DMA_NOWAIT) != 0 || 429 bus_dmamem_map(sc->jme_dmatag, &sc->jme_rxseg, 430 nsegs, PAGE_SIZE, (void **)&sc->jme_rxring, 431 BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0 || 432 bus_dmamap_create(sc->jme_dmatag, PAGE_SIZE, 1, PAGE_SIZE, 0, 433 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->jme_rxmap) != 0 || 434 bus_dmamap_load(sc->jme_dmatag, sc->jme_rxmap, sc->jme_rxring, 435 PAGE_SIZE, NULL, BUS_DMA_NOWAIT) != 0) { 436 aprint_error_dev(self, "can't allocate DMA memory RX ring\n"); 437 return; 438 } 439 for (i = 0; i < JME_NBUFS; i++) { 440 sc->jme_txmbuf[i] = sc->jme_rxmbuf[i] = NULL; 441 if (bus_dmamap_create(sc->jme_dmatag, JME_MAX_TX_LEN, 442 JME_NBUFS, JME_MAX_TX_LEN, 0, BUS_DMA_NOWAIT, 443 &sc->jme_txmbufm[i]) != 0) { 444 aprint_error_dev(self, "can't allocate DMA TX map\n"); 445 return; 446 } 447 if (bus_dmamap_create(sc->jme_dmatag, JME_MAX_RX_LEN, 448 1, JME_MAX_RX_LEN, 0, BUS_DMA_NOWAIT, 449 &sc->jme_rxmbufm[i]) != 0) { 450 aprint_error_dev(self, "can't allocate DMA RX map\n"); 451 return; 452 } 453 } 454 /* 455 * Initialize our media structures and probe the MII. 456 * 457 * Note that we don't care about the media instance. We 458 * are expecting to have multiple PHYs on the 10/100 cards, 459 * and on those cards we exclude the internal PHY from providing 460 * 10baseT. By ignoring the instance, it allows us to not have 461 * to specify it on the command line when switching media. 462 */ 463 sc->jme_mii.mii_ifp = ifp; 464 sc->jme_mii.mii_readreg = jme_mii_read; 465 sc->jme_mii.mii_writereg = jme_mii_write; 466 sc->jme_mii.mii_statchg = jme_statchg; 467 sc->jme_ec.ec_mii = &sc->jme_mii; 468 ifmedia_init(&sc->jme_mii.mii_media, IFM_IMASK, jme_mediachange, 469 ether_mediastatus); 470 mii_attach(self, &sc->jme_mii, 0xffffffff, MII_PHY_ANY, 471 MII_OFFSET_ANY, 0); 472 if (LIST_FIRST(&sc->jme_mii.mii_phys) == NULL) { 473 ifmedia_add(&sc->jme_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 474 ifmedia_set(&sc->jme_mii.mii_media, IFM_ETHER|IFM_NONE); 475 } else 476 ifmedia_set(&sc->jme_mii.mii_media, IFM_ETHER|IFM_AUTO); 477 478 /* 479 * We can support 802.1Q VLAN-sized frames. 480 */ 481 sc->jme_ec.ec_capabilities |= 482 ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING; 483 484 if (sc->jme_flags & JME_FLAG_GIGA) 485 sc->jme_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU; 486 487 488 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); 489 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST; 490 ifp->if_ioctl = jme_ifioctl; 491 ifp->if_start = jme_ifstart; 492 ifp->if_watchdog = jme_ifwatchdog; 493 ifp->if_init = jme_ifinit; 494 ifp->if_stop = jme_stop; 495 ifp->if_timer = 0; 496 ifp->if_capabilities |= 497 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx | 498 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx | 499 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx | 500 IFCAP_CSUM_TCPv6_Tx | /* IFCAP_CSUM_TCPv6_Rx | hardware bug */ 501 IFCAP_CSUM_UDPv6_Tx | /* IFCAP_CSUM_UDPv6_Rx | hardware bug */ 502 IFCAP_TSOv4 | IFCAP_TSOv6; 503 IFQ_SET_READY(&ifp->if_snd); 504 if_attach(ifp); 505 ether_ifattach(&(sc)->jme_if, (sc)->jme_enaddr); 506 507 /* 508 * Add shutdown hook so that DMA is disabled prior to reboot. 509 */ 510 if (pmf_device_register1(self, NULL, NULL, jme_shutdown)) 511 pmf_class_network_register(self, ifp); 512 else 513 aprint_error_dev(self, "couldn't establish power handler\n"); 514 515 #if NRND > 0 516 rnd_attach_source(&sc->rnd_source, device_xname(self), 517 RND_TYPE_NET, 0); 518 #endif 519 sc->jme_intrxto = PCCRX_COAL_TO_DEFAULT; 520 sc->jme_intrxct = PCCRX_COAL_PKT_DEFAULT; 521 sc->jme_inttxto = PCCTX_COAL_TO_DEFAULT; 522 sc->jme_inttxct = PCCTX_COAL_PKT_DEFAULT; 523 if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, 524 0, CTLTYPE_NODE, device_xname(sc->jme_dev), 525 SYSCTL_DESCR("jme per-controller controls"), 526 NULL, 0, NULL, 0, CTL_HW, jme_root_num, CTL_CREATE, 527 CTL_EOL) != 0) { 528 aprint_normal_dev(sc->jme_dev, "couldn't create sysctl node\n"); 529 return; 530 } 531 jme_nodenum = node->sysctl_num; 532 533 /* interrupt moderation sysctls */ 534 if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, 535 CTLFLAG_READWRITE, 536 CTLTYPE_INT, "int_rxto", 537 SYSCTL_DESCR("jme RX interrupt moderation timer"), 538 jme_sysctl_intrxto, 0, sc, 539 0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE, 540 CTL_EOL) != 0) { 541 aprint_normal_dev(sc->jme_dev, 542 "couldn't create int_rxto sysctl node\n"); 543 } 544 if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, 545 CTLFLAG_READWRITE, 546 CTLTYPE_INT, "int_rxct", 547 SYSCTL_DESCR("jme RX interrupt moderation packet counter"), 548 jme_sysctl_intrxct, 0, sc, 549 0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE, 550 CTL_EOL) != 0) { 551 aprint_normal_dev(sc->jme_dev, 552 "couldn't create int_rxct sysctl node\n"); 553 } 554 if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, 555 CTLFLAG_READWRITE, 556 CTLTYPE_INT, "int_txto", 557 SYSCTL_DESCR("jme TX interrupt moderation timer"), 558 jme_sysctl_inttxto, 0, sc, 559 0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE, 560 CTL_EOL) != 0) { 561 aprint_normal_dev(sc->jme_dev, 562 "couldn't create int_txto sysctl node\n"); 563 } 564 if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, 565 CTLFLAG_READWRITE, 566 CTLTYPE_INT, "int_txct", 567 SYSCTL_DESCR("jme TX interrupt moderation packet counter"), 568 jme_sysctl_inttxct, 0, sc, 569 0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE, 570 CTL_EOL) != 0) { 571 aprint_normal_dev(sc->jme_dev, 572 "couldn't create int_txct sysctl node\n"); 573 } 574 } 575 576 static void 577 jme_stop_rx(jme_softc_t *sc) 578 { 579 uint32_t reg; 580 int i; 581 582 reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR); 583 if ((reg & RXCSR_RX_ENB) == 0) 584 return; 585 reg &= ~RXCSR_RX_ENB; 586 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR, reg); 587 for (i = JME_TIMEOUT / 10; i > 0; i--) { 588 DELAY(10); 589 if ((bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, 590 JME_RXCSR) & RXCSR_RX_ENB) == 0) 591 break; 592 } 593 if (i == 0) 594 aprint_error_dev(sc->jme_dev, "stopping recevier timeout!\n"); 595 596 } 597 598 static void 599 jme_stop_tx(jme_softc_t *sc) 600 { 601 uint32_t reg; 602 int i; 603 604 reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR); 605 if ((reg & TXCSR_TX_ENB) == 0) 606 return; 607 reg &= ~TXCSR_TX_ENB; 608 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR, reg); 609 for (i = JME_TIMEOUT / 10; i > 0; i--) { 610 DELAY(10); 611 if ((bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, 612 JME_TXCSR) & TXCSR_TX_ENB) == 0) 613 break; 614 } 615 if (i == 0) 616 aprint_error_dev(sc->jme_dev, 617 "stopping transmitter timeout!\n"); 618 } 619 620 static void 621 jme_reset(jme_softc_t *sc) 622 { 623 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, GHC_RESET); 624 DELAY(10); 625 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, 0); 626 } 627 628 static bool 629 jme_shutdown(device_t self, int howto) 630 { 631 jme_softc_t *sc; 632 struct ifnet *ifp; 633 634 sc = device_private(self); 635 ifp = &sc->jme_if; 636 jme_stop(ifp, 1); 637 638 return true; 639 } 640 641 static void 642 jme_stop(struct ifnet *ifp, int disable) 643 { 644 jme_softc_t *sc = ifp->if_softc; 645 int i; 646 /* Stop receiver, transmitter. */ 647 jme_stop_rx(sc); 648 jme_stop_tx(sc); 649 /* free receive mbufs */ 650 for (i = 0; i < JME_NBUFS; i++) { 651 if (sc->jme_rxmbuf[i]) { 652 bus_dmamap_unload(sc->jme_dmatag, sc->jme_rxmbufm[i]); 653 m_freem(sc->jme_rxmbuf[i]); 654 } 655 sc->jme_rxmbuf[i] = NULL; 656 } 657 /* process completed transmits */ 658 jme_txeof(sc); 659 /* free abort pending transmits */ 660 for (i = 0; i < JME_NBUFS; i++) { 661 if (sc->jme_txmbuf[i]) { 662 bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[i]); 663 m_freem(sc->jme_txmbuf[i]); 664 sc->jme_txmbuf[i] = NULL; 665 } 666 } 667 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 668 ifp->if_timer = 0; 669 } 670 671 #if 0 672 static void 673 jme_restart(void *v) 674 { 675 676 jme_init(v); 677 } 678 #endif 679 680 static int 681 jme_add_rxbuf(jme_softc_t *sc, struct mbuf *m) 682 { 683 int error; 684 bus_dmamap_t map; 685 int i = sc->jme_rx_prod; 686 687 if (sc->jme_rxmbuf[i] != NULL) { 688 aprint_error_dev(sc->jme_dev, 689 "mbuf already here: rxprod %d rxcons %d\n", 690 sc->jme_rx_prod, sc->jme_rx_cons); 691 if (m) 692 m_freem(m); 693 return EINVAL; 694 } 695 696 if (m == NULL) { 697 sc->jme_rxmbuf[i] = NULL; 698 MGETHDR(m, M_DONTWAIT, MT_DATA); 699 if (m == NULL) 700 return (ENOBUFS); 701 MCLGET(m, M_DONTWAIT); 702 if ((m->m_flags & M_EXT) == 0) { 703 m_freem(m); 704 return (ENOBUFS); 705 } 706 } 707 map = sc->jme_rxmbufm[i]; 708 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 709 error = bus_dmamap_load_mbuf(sc->jme_dmatag, map, m, 710 BUS_DMA_READ|BUS_DMA_NOWAIT); 711 if (error) { 712 sc->jme_rxmbuf[i] = NULL; 713 aprint_error_dev(sc->jme_dev, 714 "unable to load rx DMA map %d, error = %d\n", 715 i, error); 716 m_freem(m); 717 return (error); 718 } 719 bus_dmamap_sync(sc->jme_dmatag, map, 0, map->dm_mapsize, 720 BUS_DMASYNC_PREREAD); 721 722 sc->jme_rxmbuf[i] = m; 723 724 sc->jme_rxring[i].buflen = htole32(map->dm_segs[0].ds_len); 725 sc->jme_rxring[i].addr_lo = 726 htole32(JME_ADDR_LO(map->dm_segs[0].ds_addr)); 727 sc->jme_rxring[i].addr_hi = 728 htole32(JME_ADDR_HI(map->dm_segs[0].ds_addr)); 729 sc->jme_rxring[i].flags = 730 htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT); 731 bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmap, 732 i * sizeof(struct jme_desc), sizeof(struct jme_desc), 733 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 734 JME_DESC_INC(sc->jme_rx_prod, JME_NBUFS); 735 return (0); 736 } 737 738 static int 739 jme_ifinit(struct ifnet *ifp) 740 { 741 return jme_init(ifp, 1); 742 } 743 744 static int 745 jme_init(struct ifnet *ifp, int do_ifinit) 746 { 747 jme_softc_t *sc = ifp->if_softc; 748 int i, s; 749 uint8_t eaddr[ETHER_ADDR_LEN]; 750 uint32_t reg; 751 752 s = splnet(); 753 /* cancel any pending IO */ 754 jme_stop(ifp, 1); 755 jme_reset(sc); 756 if ((sc->jme_if.if_flags & IFF_UP) == 0) { 757 splx(s); 758 return 0; 759 } 760 /* allocate receive ring */ 761 sc->jme_rx_prod = 0; 762 for (i = 0; i < JME_NBUFS; i++) { 763 if (jme_add_rxbuf(sc, NULL) < 0) { 764 aprint_error_dev(sc->jme_dev, 765 "can't allocate rx mbuf\n"); 766 for (i--; i >= 0; i--) { 767 bus_dmamap_unload(sc->jme_dmatag, 768 sc->jme_rxmbufm[i]); 769 m_freem(sc->jme_rxmbuf[i]); 770 sc->jme_rxmbuf[i] = NULL; 771 } 772 splx(s); 773 return ENOMEM; 774 } 775 } 776 /* init TX ring */ 777 memset(sc->jme_txring, 0, JME_NBUFS * sizeof(struct jme_desc)); 778 bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, 779 0, JME_NBUFS * sizeof(struct jme_desc), 780 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 781 for (i = 0; i < JME_NBUFS; i++) 782 sc->jme_txmbuf[i] = NULL; 783 sc->jme_tx_cons = sc->jme_tx_prod = sc->jme_tx_cnt = 0; 784 785 /* Reprogram the station address. */ 786 memcpy(eaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN); 787 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR0, 788 eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]); 789 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 790 JME_PAR1, eaddr[5] << 8 | eaddr[4]); 791 792 /* 793 * Configure Tx queue. 794 * Tx priority queue weight value : 0 795 * Tx FIFO threshold for processing next packet : 16QW 796 * Maximum Tx DMA length : 512 797 * Allow Tx DMA burst. 798 */ 799 sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0); 800 sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN); 801 sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW; 802 sc->jme_txcsr |= TXCSR_DMA_SIZE_512; 803 sc->jme_txcsr |= TXCSR_DMA_BURST; 804 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 805 JME_TXCSR, sc->jme_txcsr); 806 807 /* Set Tx descriptor counter. */ 808 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 809 JME_TXQDC, JME_NBUFS); 810 811 /* Set Tx ring address to the hardware. */ 812 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI, 813 JME_ADDR_HI(sc->jme_txmap->dm_segs[0].ds_addr)); 814 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO, 815 JME_ADDR_LO(sc->jme_txmap->dm_segs[0].ds_addr)); 816 817 /* Configure TxMAC parameters. */ 818 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC, 819 TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB | 820 TXMAC_THRESH_1_PKT | TXMAC_CRC_ENB | TXMAC_PAD_ENB); 821 822 /* 823 * Configure Rx queue. 824 * FIFO full threshold for transmitting Tx pause packet : 128T 825 * FIFO threshold for processing next packet : 128QW 826 * Rx queue 0 select 827 * Max Rx DMA length : 128 828 * Rx descriptor retry : 32 829 * Rx descriptor retry time gap : 256ns 830 * Don't receive runt/bad frame. 831 */ 832 sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T; 833 /* 834 * Since Rx FIFO size is 4K bytes, receiving frames larger 835 * than 4K bytes will suffer from Rx FIFO overruns. So 836 * decrease FIFO threshold to reduce the FIFO overruns for 837 * frames larger than 4000 bytes. 838 * For best performance of standard MTU sized frames use 839 * maximum allowable FIFO threshold, 128QW. 840 */ 841 if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + 842 ETHER_CRC_LEN) > JME_RX_FIFO_SIZE) 843 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW; 844 else 845 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW; 846 sc->jme_rxcsr |= RXCSR_DMA_SIZE_128 | RXCSR_RXQ_N_SEL(RXCSR_RXQ0); 847 sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT); 848 sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK; 849 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 850 JME_RXCSR, sc->jme_rxcsr); 851 852 /* Set Rx descriptor counter. */ 853 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 854 JME_RXQDC, JME_NBUFS); 855 856 /* Set Rx ring address to the hardware. */ 857 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_HI, 858 JME_ADDR_HI(sc->jme_rxmap->dm_segs[0].ds_addr)); 859 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_LO, 860 JME_ADDR_LO(sc->jme_rxmap->dm_segs[0].ds_addr)); 861 862 /* Clear receive filter. */ 863 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, 0); 864 /* Set up the receive filter. */ 865 jme_set_filter(sc); 866 867 /* 868 * Disable all WOL bits as WOL can interfere normal Rx 869 * operation. Also clear WOL detection status bits. 870 */ 871 reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PMCS); 872 reg &= ~PMCS_WOL_ENB_MASK; 873 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PMCS, reg); 874 875 reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC); 876 /* 877 * Pad 10bytes right before received frame. This will greatly 878 * help Rx performance on strict-alignment architectures as 879 * it does not need to copy the frame to align the payload. 880 */ 881 reg |= RXMAC_PAD_10BYTES; 882 if ((ifp->if_capenable & 883 (IFCAP_CSUM_IPv4_Rx|IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx| 884 IFCAP_CSUM_TCPv6_Rx|IFCAP_CSUM_UDPv6_Rx)) != 0) 885 reg |= RXMAC_CSUM_ENB; 886 reg |= RXMAC_VLAN_ENB; /* enable hardware vlan */ 887 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, reg); 888 889 /* Configure general purpose reg0 */ 890 reg = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_GPREG0); 891 reg &= ~GPREG0_PCC_UNIT_MASK; 892 /* Set PCC timer resolution to micro-seconds unit. */ 893 reg |= GPREG0_PCC_UNIT_US; 894 /* 895 * Disable all shadow register posting as we have to read 896 * JME_INTR_STATUS register in jme_int_task. Also it seems 897 * that it's hard to synchronize interrupt status between 898 * hardware and software with shadow posting due to 899 * requirements of bus_dmamap_sync(9). 900 */ 901 reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS | 902 GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS | 903 GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS | 904 GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS; 905 /* Disable posting of DW0. */ 906 reg &= ~GPREG0_POST_DW0_ENB; 907 /* Clear PME message. */ 908 reg &= ~GPREG0_PME_ENB; 909 /* Set PHY address. */ 910 reg &= ~GPREG0_PHY_ADDR_MASK; 911 reg |= sc->jme_phyaddr; 912 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_GPREG0, reg); 913 914 /* Configure Tx queue 0 packet completion coalescing. */ 915 reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK; 916 reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK; 917 reg |= PCCTX_COAL_TXQ0; 918 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg); 919 920 /* Configure Rx queue 0 packet completion coalescing. */ 921 reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK; 922 reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK; 923 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg); 924 925 /* Disable Timers */ 926 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_TMCSR, 0); 927 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_TIMER1, 0); 928 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_TIMER2, 0); 929 930 /* Configure retry transmit period, retry limit value. */ 931 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD, 932 ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) & 933 TXTRHD_RT_PERIOD_MASK) | 934 ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) & 935 TXTRHD_RT_LIMIT_SHIFT)); 936 937 /* Disable RSS. */ 938 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, 939 JME_RSSC, RSSC_DIS_RSS); 940 941 /* Initialize the interrupt mask. */ 942 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, 943 JME_INTR_MASK_SET, JME_INTRS_ENABLE); 944 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, 945 JME_INTR_STATUS, 0xFFFFFFFF); 946 947 /* set media, if not already handling a media change */ 948 if (do_ifinit) { 949 int error; 950 if ((error = mii_mediachg(&sc->jme_mii)) == ENXIO) 951 error = 0; 952 else if (error != 0) { 953 aprint_error_dev(sc->jme_dev, "could not set media\n"); 954 return error; 955 } 956 } 957 958 /* Program MAC with resolved speed/duplex/flow-control. */ 959 jme_mac_config(sc); 960 961 /* Start receiver/transmitter. */ 962 sc->jme_rx_cons = 0; 963 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR, 964 sc->jme_rxcsr | RXCSR_RX_ENB | RXCSR_RXQ_START); 965 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR, 966 sc->jme_txcsr | TXCSR_TX_ENB); 967 968 /* start ticks calls */ 969 callout_reset(&sc->jme_tick_ch, hz, jme_ticks, sc); 970 sc->jme_if.if_flags |= IFF_RUNNING; 971 sc->jme_if.if_flags &= ~IFF_OACTIVE; 972 splx(s); 973 return 0; 974 } 975 976 977 int 978 jme_mii_read(device_t self, int phy, int reg) 979 { 980 struct jme_softc *sc = device_private(self); 981 int val, i; 982 983 /* For FPGA version, PHY address 0 should be ignored. */ 984 if ((sc->jme_flags & JME_FLAG_FPGA) != 0) { 985 if (phy == 0) 986 return (0); 987 } else { 988 if (sc->jme_phyaddr != phy) 989 return (0); 990 } 991 992 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_SMI, 993 SMI_OP_READ | SMI_OP_EXECUTE | 994 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg)); 995 for (i = JME_PHY_TIMEOUT / 10; i > 0; i--) { 996 delay(10); 997 if (((val = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, 998 JME_SMI)) & SMI_OP_EXECUTE) == 0) 999 break; 1000 } 1001 1002 if (i == 0) { 1003 aprint_error_dev(sc->jme_dev, "phy read timeout : %d\n", reg); 1004 return (0); 1005 } 1006 1007 return ((val & SMI_DATA_MASK) >> SMI_DATA_SHIFT); 1008 } 1009 1010 void 1011 jme_mii_write(device_t self, int phy, int reg, int val) 1012 { 1013 struct jme_softc *sc = device_private(self); 1014 int i; 1015 1016 /* For FPGA version, PHY address 0 should be ignored. */ 1017 if ((sc->jme_flags & JME_FLAG_FPGA) != 0) { 1018 if (phy == 0) 1019 return; 1020 } else { 1021 if (sc->jme_phyaddr != phy) 1022 return; 1023 } 1024 1025 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_SMI, 1026 SMI_OP_WRITE | SMI_OP_EXECUTE | 1027 ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) | 1028 SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg)); 1029 for (i = JME_PHY_TIMEOUT / 10; i > 0; i--) { 1030 delay(10); 1031 if (((val = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, 1032 JME_SMI)) & SMI_OP_EXECUTE) == 0) 1033 break; 1034 } 1035 1036 if (i == 0) 1037 aprint_error_dev(sc->jme_dev, "phy write timeout : %d\n", reg); 1038 1039 return; 1040 } 1041 1042 void 1043 jme_statchg(device_t self) 1044 { 1045 jme_softc_t *sc = device_private(self); 1046 struct ifnet *ifp = &sc->jme_if; 1047 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING)) 1048 jme_init(ifp, 0); 1049 } 1050 1051 static void 1052 jme_intr_rx(jme_softc_t *sc) { 1053 struct mbuf *m, *mhead; 1054 struct ifnet *ifp = &sc->jme_if; 1055 uint32_t flags, buflen; 1056 int i, ipackets, nsegs, seg, error; 1057 struct jme_desc *desc; 1058 1059 bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmap, 0, 1060 sizeof(struct jme_desc) * JME_NBUFS, 1061 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1062 #ifdef JMEDEBUG_RX 1063 printf("rxintr sc->jme_rx_cons %d flags 0x%x\n", 1064 sc->jme_rx_cons, le32toh(sc->jme_rxring[sc->jme_rx_cons].flags)); 1065 #endif 1066 ipackets = 0; 1067 while((le32toh(sc->jme_rxring[ sc->jme_rx_cons].flags) & JME_RD_OWN) 1068 == 0) { 1069 i = sc->jme_rx_cons; 1070 desc = &sc->jme_rxring[i]; 1071 #ifdef JMEDEBUG_RX 1072 printf("rxintr i %d flags 0x%x buflen 0x%x\n", 1073 i, le32toh(desc->flags), le32toh(desc->buflen)); 1074 #endif 1075 if ((le32toh(desc->buflen) & JME_RD_VALID) == 0) 1076 break; 1077 bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmbufm[i], 0, 1078 sc->jme_rxmbufm[i]->dm_mapsize, BUS_DMASYNC_POSTREAD); 1079 bus_dmamap_unload(sc->jme_dmatag, sc->jme_rxmbufm[i]); 1080 1081 buflen = le32toh(desc->buflen); 1082 nsegs = JME_RX_NSEGS(buflen); 1083 flags = le32toh(desc->flags); 1084 if ((buflen & JME_RX_ERR_STAT) != 0 || 1085 JME_RX_BYTES(buflen) < sizeof(struct ether_header) || 1086 JME_RX_BYTES(buflen) > 1087 (ifp->if_mtu + ETHER_HDR_LEN + JME_RX_PAD_BYTES)) { 1088 #ifdef JMEDEBUG_RX 1089 printf("rx error flags 0x%x buflen 0x%x\n", 1090 flags, buflen); 1091 #endif 1092 ifp->if_ierrors++; 1093 /* reuse the mbufs */ 1094 for (seg = 0; seg < nsegs; seg++) { 1095 m = sc->jme_rxmbuf[i]; 1096 sc->jme_rxmbuf[i] = NULL; 1097 if ((error = jme_add_rxbuf(sc, m)) != 0) 1098 aprint_error_dev(sc->jme_dev, 1099 "can't reuse mbuf: %d\n", error); 1100 JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS); 1101 i = sc->jme_rx_cons; 1102 } 1103 continue; 1104 } 1105 /* receive this packet */ 1106 mhead = m = sc->jme_rxmbuf[i]; 1107 sc->jme_rxmbuf[i] = NULL; 1108 /* add a new buffer to chain */ 1109 if (jme_add_rxbuf(sc, NULL) == ENOBUFS) { 1110 for (seg = 0; seg < nsegs; seg++) { 1111 m = sc->jme_rxmbuf[i]; 1112 sc->jme_rxmbuf[i] = NULL; 1113 if ((error = jme_add_rxbuf(sc, m)) != 0) 1114 aprint_error_dev(sc->jme_dev, 1115 "can't reuse mbuf: %d\n", error); 1116 JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS); 1117 i = sc->jme_rx_cons; 1118 } 1119 ifp->if_ierrors++; 1120 continue; 1121 } 1122 1123 /* build mbuf chain: head, then remaining segments */ 1124 m->m_pkthdr.rcvif = ifp; 1125 m->m_pkthdr.len = JME_RX_BYTES(buflen) - JME_RX_PAD_BYTES; 1126 m->m_len = (nsegs > 1) ? (MCLBYTES - JME_RX_PAD_BYTES) : 1127 m->m_pkthdr.len; 1128 m->m_data = m->m_ext.ext_buf + JME_RX_PAD_BYTES; 1129 JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS); 1130 for (seg = 1; seg < nsegs; seg++) { 1131 i = sc->jme_rx_cons; 1132 m = sc->jme_rxmbuf[i]; 1133 sc->jme_rxmbuf[i] = NULL; 1134 (void)jme_add_rxbuf(sc, NULL); 1135 m->m_flags &= ~M_PKTHDR; 1136 m_cat(mhead, m); 1137 JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS); 1138 } 1139 /* and adjust last mbuf's size */ 1140 if (nsegs > 1) { 1141 m->m_len = 1142 JME_RX_BYTES(buflen) - (MCLBYTES * (nsegs - 1)); 1143 } 1144 ifp->if_ipackets++; 1145 ipackets++; 1146 if (ifp->if_bpf) 1147 bpf_ops->bpf_mtap(ifp->if_bpf, mhead); 1148 1149 if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) && 1150 (flags & JME_RD_IPV4)) { 1151 mhead->m_pkthdr.csum_flags |= M_CSUM_IPv4; 1152 if (!(flags & JME_RD_IPCSUM)) 1153 mhead->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD; 1154 } 1155 if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) && 1156 (flags & JME_RD_TCPV4) == JME_RD_TCPV4) { 1157 mhead->m_pkthdr.csum_flags |= M_CSUM_TCPv4; 1158 if (!(flags & JME_RD_TCPCSUM)) 1159 mhead->m_pkthdr.csum_flags |= 1160 M_CSUM_TCP_UDP_BAD; 1161 } 1162 if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) && 1163 (flags & JME_RD_UDPV4) == JME_RD_UDPV4) { 1164 mhead->m_pkthdr.csum_flags |= M_CSUM_UDPv4; 1165 if (!(flags & JME_RD_UDPCSUM)) 1166 mhead->m_pkthdr.csum_flags |= 1167 M_CSUM_TCP_UDP_BAD; 1168 } 1169 if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) && 1170 (flags & JME_RD_TCPV6) == JME_RD_TCPV6) { 1171 mhead->m_pkthdr.csum_flags |= M_CSUM_TCPv6; 1172 if (!(flags & JME_RD_TCPCSUM)) 1173 mhead->m_pkthdr.csum_flags |= 1174 M_CSUM_TCP_UDP_BAD; 1175 } 1176 if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) && 1177 (flags & JME_RD_UDPV6) == JME_RD_UDPV6) { 1178 m->m_pkthdr.csum_flags |= M_CSUM_UDPv6; 1179 if (!(flags & JME_RD_UDPCSUM)) 1180 mhead->m_pkthdr.csum_flags |= 1181 M_CSUM_TCP_UDP_BAD; 1182 } 1183 if (flags & JME_RD_VLAN_TAG) { 1184 /* pass to vlan_input() */ 1185 VLAN_INPUT_TAG(ifp, mhead, 1186 (flags & JME_RD_VLAN_MASK), continue); 1187 } 1188 (*ifp->if_input)(ifp, mhead); 1189 } 1190 #if NRND > 0 1191 if (ipackets && RND_ENABLED(&sc->rnd_source)) 1192 rnd_add_uint32(&sc->rnd_source, ipackets); 1193 #endif /* NRND > 0 */ 1194 1195 } 1196 1197 static int 1198 jme_intr(void *v) 1199 { 1200 jme_softc_t *sc = v; 1201 uint32_t istatus; 1202 1203 istatus = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, 1204 JME_INTR_STATUS); 1205 if (istatus == 0 || istatus == 0xFFFFFFFF) 1206 return 0; 1207 /* Disable interrupts. */ 1208 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, 1209 JME_INTR_MASK_CLR, 0xFFFFFFFF); 1210 again: 1211 /* and update istatus */ 1212 istatus = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, 1213 JME_INTR_STATUS); 1214 if ((istatus & JME_INTRS_CHECK) == 0) 1215 goto done; 1216 /* Reset PCC counter/timer and Ack interrupts. */ 1217 if ((istatus & (INTR_TXQ_COMP | INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) != 0) 1218 istatus |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP; 1219 if ((istatus & (INTR_RXQ_COMP | INTR_RXQ_COAL | INTR_RXQ_COAL_TO)) != 0) 1220 istatus |= INTR_RXQ_COAL | INTR_RXQ_COAL_TO | INTR_RXQ_COMP; 1221 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, 1222 JME_INTR_STATUS, istatus); 1223 1224 if ((sc->jme_if.if_flags & IFF_RUNNING) == 0) 1225 goto done; 1226 #ifdef JMEDEBUG_RX 1227 printf("jme_intr 0x%x RXCS 0x%x RXDBA 0x%x 0x%x RXQDC 0x%x RXNDA 0x%x RXMCS 0x%x\n", istatus, 1228 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR), 1229 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_LO), 1230 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_HI), 1231 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXQDC), 1232 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXNDA), 1233 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC)); 1234 printf("jme_intr RXUMA 0x%x 0x%x RXMCHT 0x%x 0x%x GHC 0x%x\n", 1235 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR0), 1236 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR1), 1237 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR0), 1238 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR1), 1239 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC)); 1240 #endif 1241 if ((istatus & (INTR_RXQ_COMP | INTR_RXQ_COAL | INTR_RXQ_COAL_TO)) != 0) 1242 jme_intr_rx(sc); 1243 if ((istatus & INTR_RXQ_DESC_EMPTY) != 0) { 1244 /* 1245 * Notify hardware availability of new Rx 1246 * buffers. 1247 * Reading RXCSR takes very long time under 1248 * heavy load so cache RXCSR value and writes 1249 * the ORed value with the kick command to 1250 * the RXCSR. This saves one register access 1251 * cycle. 1252 */ 1253 sc->jme_rx_cons = 0; 1254 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 1255 JME_RXCSR, 1256 sc->jme_rxcsr | RXCSR_RX_ENB | RXCSR_RXQ_START); 1257 } 1258 if ((istatus & (INTR_TXQ_COMP | INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) != 0) 1259 jme_ifstart(&sc->jme_if); 1260 1261 goto again; 1262 1263 done: 1264 /* enable interrupts. */ 1265 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, 1266 JME_INTR_MASK_SET, JME_INTRS_ENABLE); 1267 return 1; 1268 } 1269 1270 1271 static int 1272 jme_ifioctl(struct ifnet *ifp, unsigned long cmd, void *data) 1273 { 1274 struct jme_softc *sc = ifp->if_softc; 1275 int s, error; 1276 struct ifreq *ifr; 1277 struct ifcapreq *ifcr; 1278 1279 s = splnet(); 1280 /* 1281 * we can't support at the same time jumbo frames and 1282 * TX checksums offload/TSO 1283 */ 1284 switch(cmd) { 1285 case SIOCSIFMTU: 1286 ifr = data; 1287 if (ifr->ifr_mtu > JME_TX_FIFO_SIZE && 1288 (ifp->if_capenable & ( 1289 IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx| 1290 IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_UDPv6_Tx| 1291 IFCAP_TSOv4|IFCAP_TSOv6)) != 0) { 1292 splx(s); 1293 return EINVAL; 1294 } 1295 break; 1296 case SIOCSIFCAP: 1297 ifcr = data; 1298 if (ifp->if_mtu > JME_TX_FIFO_SIZE && 1299 (ifcr->ifcr_capenable & ( 1300 IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx| 1301 IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_UDPv6_Tx| 1302 IFCAP_TSOv4|IFCAP_TSOv6)) != 0) { 1303 splx(s); 1304 return EINVAL; 1305 } 1306 break; 1307 } 1308 1309 error = ether_ioctl(ifp, cmd, data); 1310 if (error == ENETRESET && (ifp->if_flags & IFF_RUNNING)) { 1311 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) { 1312 jme_set_filter(sc); 1313 error = 0; 1314 } else { 1315 error = jme_init(ifp, 0); 1316 } 1317 } 1318 splx(s); 1319 return error; 1320 } 1321 1322 static int 1323 jme_encap(struct jme_softc *sc, struct mbuf **m_head) 1324 { 1325 struct jme_desc *txd; 1326 struct jme_desc *desc; 1327 struct mbuf *m; 1328 struct m_tag *mtag; 1329 int error, i, prod, headdsc, nsegs; 1330 uint32_t cflags, tso_segsz; 1331 1332 if (((*m_head)->m_pkthdr.csum_flags & (M_CSUM_TSOv4|M_CSUM_TSOv6)) != 0){ 1333 /* 1334 * Due to the adherence to NDIS specification JMC250 1335 * assumes upper stack computed TCP pseudo checksum 1336 * without including payload length. This breaks 1337 * checksum offload for TSO case so recompute TCP 1338 * pseudo checksum for JMC250. Hopefully this wouldn't 1339 * be much burden on modern CPUs. 1340 */ 1341 bool v4 = ((*m_head)->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0; 1342 int iphl = v4 ? 1343 M_CSUM_DATA_IPv4_IPHL((*m_head)->m_pkthdr.csum_data) : 1344 M_CSUM_DATA_IPv6_HL((*m_head)->m_pkthdr.csum_data); 1345 /* 1346 * note: we support vlan offloading, so we should never have 1347 * a ETHERTYPE_VLAN packet here - so ETHER_HDR_LEN is always 1348 * right. 1349 */ 1350 int hlen = ETHER_HDR_LEN + iphl; 1351 1352 if (__predict_false((*m_head)->m_len < 1353 (hlen + sizeof(struct tcphdr)))) { 1354 /* 1355 * TCP/IP headers are not in the first mbuf; we need 1356 * to do this the slow and painful way. Let's just 1357 * hope this doesn't happen very often. 1358 */ 1359 struct tcphdr th; 1360 1361 m_copydata((*m_head), hlen, sizeof(th), &th); 1362 if (v4) { 1363 struct ip ip; 1364 1365 m_copydata((*m_head), ETHER_HDR_LEN, 1366 sizeof(ip), &ip); 1367 ip.ip_len = 0; 1368 m_copyback((*m_head), 1369 ETHER_HDR_LEN + offsetof(struct ip, ip_len), 1370 sizeof(ip.ip_len), &ip.ip_len); 1371 th.th_sum = in_cksum_phdr(ip.ip_src.s_addr, 1372 ip.ip_dst.s_addr, htons(IPPROTO_TCP)); 1373 } else { 1374 #if INET6 1375 struct ip6_hdr ip6; 1376 1377 m_copydata((*m_head), ETHER_HDR_LEN, 1378 sizeof(ip6), &ip6); 1379 ip6.ip6_plen = 0; 1380 m_copyback((*m_head), ETHER_HDR_LEN + 1381 offsetof(struct ip6_hdr, ip6_plen), 1382 sizeof(ip6.ip6_plen), &ip6.ip6_plen); 1383 th.th_sum = in6_cksum_phdr(&ip6.ip6_src, 1384 &ip6.ip6_dst, 0, htonl(IPPROTO_TCP)); 1385 #endif /* INET6 */ 1386 } 1387 m_copyback((*m_head), 1388 hlen + offsetof(struct tcphdr, th_sum), 1389 sizeof(th.th_sum), &th.th_sum); 1390 1391 hlen += th.th_off << 2; 1392 } else { 1393 /* 1394 * TCP/IP headers are in the first mbuf; we can do 1395 * this the easy way. 1396 */ 1397 struct tcphdr *th; 1398 1399 if (v4) { 1400 struct ip *ip = 1401 (void *)(mtod((*m_head), char *) + 1402 ETHER_HDR_LEN); 1403 th = (void *)(mtod((*m_head), char *) + hlen); 1404 1405 ip->ip_len = 0; 1406 th->th_sum = in_cksum_phdr(ip->ip_src.s_addr, 1407 ip->ip_dst.s_addr, htons(IPPROTO_TCP)); 1408 } else { 1409 #if INET6 1410 struct ip6_hdr *ip6 = 1411 (void *)(mtod((*m_head), char *) + 1412 ETHER_HDR_LEN); 1413 th = (void *)(mtod((*m_head), char *) + hlen); 1414 1415 ip6->ip6_plen = 0; 1416 th->th_sum = in6_cksum_phdr(&ip6->ip6_src, 1417 &ip6->ip6_dst, 0, htonl(IPPROTO_TCP)); 1418 #endif /* INET6 */ 1419 } 1420 hlen += th->th_off << 2; 1421 } 1422 1423 } 1424 1425 prod = sc->jme_tx_prod; 1426 txd = &sc->jme_txring[prod]; 1427 1428 error = bus_dmamap_load_mbuf(sc->jme_dmatag, sc->jme_txmbufm[prod], 1429 *m_head, BUS_DMA_WRITE); 1430 if (error) { 1431 if (error == EFBIG) { 1432 log(LOG_ERR, "%s: Tx packet consumes too many " 1433 "DMA segments, dropping...\n", 1434 device_xname(sc->jme_dev)); 1435 m_freem(*m_head); 1436 m_head = NULL; 1437 } 1438 return (error); 1439 } 1440 /* 1441 * Check descriptor overrun. Leave one free descriptor. 1442 * Since we always use 64bit address mode for transmitting, 1443 * each Tx request requires one more dummy descriptor. 1444 */ 1445 nsegs = sc->jme_txmbufm[prod]->dm_nsegs; 1446 #ifdef JMEDEBUG_TX 1447 printf("jme_encap prod %d nsegs %d jme_tx_cnt %d\n", prod, nsegs, sc->jme_tx_cnt); 1448 #endif 1449 if (sc->jme_tx_cnt + nsegs + 1 > JME_NBUFS - 1) { 1450 bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[prod]); 1451 return (ENOBUFS); 1452 } 1453 bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmbufm[prod], 1454 0, sc->jme_txmbufm[prod]->dm_mapsize, BUS_DMASYNC_PREWRITE); 1455 1456 m = *m_head; 1457 cflags = 0; 1458 tso_segsz = 0; 1459 /* Configure checksum offload and TSO. */ 1460 if ((m->m_pkthdr.csum_flags & (M_CSUM_TSOv4|M_CSUM_TSOv6)) != 0) { 1461 tso_segsz = (uint32_t)m->m_pkthdr.segsz << JME_TD_MSS_SHIFT; 1462 cflags |= JME_TD_TSO; 1463 } else { 1464 if ((m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) 1465 cflags |= JME_TD_IPCSUM; 1466 if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_TCPv6)) != 0) 1467 cflags |= JME_TD_TCPCSUM; 1468 if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv4|M_CSUM_UDPv6)) != 0) 1469 cflags |= JME_TD_UDPCSUM; 1470 } 1471 /* Configure VLAN. */ 1472 if ((mtag = VLAN_OUTPUT_TAG(&sc->jme_ec, m)) != NULL) { 1473 cflags |= (VLAN_TAG_VALUE(mtag) & JME_TD_VLAN_MASK); 1474 cflags |= JME_TD_VLAN_TAG; 1475 } 1476 1477 desc = &sc->jme_txring[prod]; 1478 desc->flags = htole32(cflags); 1479 desc->buflen = htole32(tso_segsz); 1480 desc->addr_hi = htole32(m->m_pkthdr.len); 1481 desc->addr_lo = 0; 1482 headdsc = prod; 1483 sc->jme_tx_cnt++; 1484 JME_DESC_INC(prod, JME_NBUFS); 1485 for (i = 0; i < nsegs; i++) { 1486 desc = &sc->jme_txring[prod]; 1487 desc->flags = htole32(JME_TD_OWN | JME_TD_64BIT); 1488 desc->buflen = 1489 htole32(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_len); 1490 desc->addr_hi = htole32( 1491 JME_ADDR_HI(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_addr)); 1492 desc->addr_lo = htole32( 1493 JME_ADDR_LO(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_addr)); 1494 bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, 1495 prod * sizeof(struct jme_desc), sizeof(struct jme_desc), 1496 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1497 sc->jme_txmbuf[prod] = NULL; 1498 sc->jme_tx_cnt++; 1499 JME_DESC_INC(prod, JME_NBUFS); 1500 } 1501 1502 /* Update producer index. */ 1503 sc->jme_tx_prod = prod; 1504 #ifdef JMEDEBUG_TX 1505 printf("jme_encap prod now %d\n", sc->jme_tx_prod); 1506 #endif 1507 /* 1508 * Finally request interrupt and give the first descriptor 1509 * owenership to hardware. 1510 */ 1511 desc = &sc->jme_txring[headdsc]; 1512 desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR); 1513 bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, 1514 headdsc * sizeof(struct jme_desc), sizeof(struct jme_desc), 1515 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1516 1517 sc->jme_txmbuf[headdsc] = m; 1518 return (0); 1519 } 1520 1521 static void 1522 jme_txeof(struct jme_softc *sc) 1523 { 1524 struct ifnet *ifp; 1525 struct jme_desc *desc; 1526 uint32_t status; 1527 int cons, cons0, nsegs, seg; 1528 1529 ifp = &sc->jme_if; 1530 1531 #ifdef JMEDEBUG_TX 1532 printf("jme_txeof cons %d prod %d\n", 1533 sc->jme_tx_cons, sc->jme_tx_prod); 1534 printf("jme_txeof JME_TXCSR 0x%x JME_TXDBA_LO 0x%x JME_TXDBA_HI 0x%x " 1535 "JME_TXQDC 0x%x JME_TXNDA 0x%x JME_TXMAC 0x%x JME_TXPFC 0x%x " 1536 "JME_TXTRHD 0x%x\n", 1537 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR), 1538 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO), 1539 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI), 1540 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXQDC), 1541 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXNDA), 1542 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC), 1543 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC), 1544 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD)); 1545 for (cons = sc->jme_tx_cons; cons != sc->jme_tx_prod; ) { 1546 desc = &sc->jme_txring[cons]; 1547 printf("ring[%d] 0x%x 0x%x 0x%x 0x%x\n", cons, 1548 desc->flags, desc->buflen, desc->addr_hi, desc->addr_lo); 1549 JME_DESC_INC(cons, JME_NBUFS); 1550 } 1551 #endif 1552 1553 cons = sc->jme_tx_cons; 1554 if (cons == sc->jme_tx_prod) 1555 return; 1556 1557 /* 1558 * Go through our Tx list and free mbufs for those 1559 * frames which have been transmitted. 1560 */ 1561 for (; cons != sc->jme_tx_prod;) { 1562 bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, 1563 cons * sizeof(struct jme_desc), sizeof(struct jme_desc), 1564 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1565 1566 desc = &sc->jme_txring[cons]; 1567 status = le32toh(desc->flags); 1568 #ifdef JMEDEBUG_TX 1569 printf("jme_txeof %i status 0x%x nsegs %d\n", cons, status, 1570 sc->jme_txmbufm[cons]->dm_nsegs); 1571 #endif 1572 if (status & JME_TD_OWN) 1573 break; 1574 1575 if ((status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) != 0) 1576 ifp->if_oerrors++; 1577 else { 1578 ifp->if_opackets++; 1579 if ((status & JME_TD_COLLISION) != 0) 1580 ifp->if_collisions += 1581 le32toh(desc->buflen) & 1582 JME_TD_BUF_LEN_MASK; 1583 } 1584 /* 1585 * Only the first descriptor of multi-descriptor 1586 * transmission is updated so driver have to skip entire 1587 * chained buffers for the transmiited frame. In other 1588 * words, JME_TD_OWN bit is valid only at the first 1589 * descriptor of a multi-descriptor transmission. 1590 */ 1591 nsegs = sc->jme_txmbufm[cons]->dm_nsegs; 1592 cons0 = cons; 1593 JME_DESC_INC(cons, JME_NBUFS); 1594 for (seg = 1; seg < nsegs + 1; seg++) { 1595 bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, 1596 cons * sizeof(struct jme_desc), 1597 sizeof(struct jme_desc), 1598 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1599 sc->jme_txring[cons].flags = 0; 1600 JME_DESC_INC(cons, JME_NBUFS); 1601 } 1602 /* Reclaim transferred mbufs. */ 1603 bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmbufm[cons0], 1604 0, sc->jme_txmbufm[cons0]->dm_mapsize, 1605 BUS_DMASYNC_POSTWRITE); 1606 bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[cons0]); 1607 1608 KASSERT(sc->jme_txmbuf[cons0] != NULL); 1609 m_freem(sc->jme_txmbuf[cons0]); 1610 sc->jme_txmbuf[cons0] = NULL; 1611 sc->jme_tx_cnt -= nsegs + 1; 1612 KASSERT(sc->jme_tx_cnt >= 0); 1613 sc->jme_if.if_flags &= ~IFF_OACTIVE; 1614 } 1615 sc->jme_tx_cons = cons; 1616 /* Unarm watchog timer when there is no pending descriptors in queue. */ 1617 if (sc->jme_tx_cnt == 0) 1618 ifp->if_timer = 0; 1619 #ifdef JMEDEBUG_TX 1620 printf("jme_txeof jme_tx_cnt %d\n", sc->jme_tx_cnt); 1621 #endif 1622 } 1623 1624 static void 1625 jme_ifstart(struct ifnet *ifp) 1626 { 1627 jme_softc_t *sc = ifp->if_softc; 1628 struct mbuf *mb_head; 1629 int enq; 1630 1631 /* 1632 * check if we can free some desc. 1633 * Clear TX interrupt status to reset TX coalescing counters. 1634 */ 1635 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, 1636 JME_INTR_STATUS, INTR_TXQ_COMP); 1637 jme_txeof(sc); 1638 1639 if ((sc->jme_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 1640 return; 1641 for (enq = 0;; enq++) { 1642 nexttx: 1643 /* Grab a paquet for output */ 1644 IFQ_DEQUEUE(&ifp->if_snd, mb_head); 1645 if (mb_head == NULL) { 1646 #ifdef JMEDEBUG_TX 1647 printf("%s: nothing to send\n", __func__); 1648 #endif 1649 break; 1650 } 1651 /* try to add this mbuf to the TX ring */ 1652 if (jme_encap(sc, &mb_head)) { 1653 if (mb_head == NULL) { 1654 ifp->if_oerrors++; 1655 /* packet dropped, try next one */ 1656 goto nexttx; 1657 } 1658 /* resource shortage, try again later */ 1659 IF_PREPEND(&ifp->if_snd, mb_head); 1660 ifp->if_flags |= IFF_OACTIVE; 1661 break; 1662 } 1663 /* Pass packet to bpf if there is a listener */ 1664 if (ifp->if_bpf) 1665 bpf_ops->bpf_mtap(ifp->if_bpf, mb_head); 1666 } 1667 #ifdef JMEDEBUG_TX 1668 printf("jme_ifstart enq %d\n", enq); 1669 #endif 1670 if (enq) { 1671 /* 1672 * Set a 5 second timer just in case we don't hear from 1673 * the card again. 1674 */ 1675 ifp->if_timer = 5; 1676 /* 1677 * Reading TXCSR takes very long time under heavy load 1678 * so cache TXCSR value and writes the ORed value with 1679 * the kick command to the TXCSR. This saves one register 1680 * access cycle. 1681 */ 1682 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR, 1683 sc->jme_txcsr | TXCSR_TX_ENB | TXCSR_TXQ_N_START(TXCSR_TXQ0)); 1684 #ifdef JMEDEBUG_TX 1685 printf("jme_ifstart JME_TXCSR 0x%x JME_TXDBA_LO 0x%x JME_TXDBA_HI 0x%x " 1686 "JME_TXQDC 0x%x JME_TXNDA 0x%x JME_TXMAC 0x%x JME_TXPFC 0x%x " 1687 "JME_TXTRHD 0x%x\n", 1688 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR), 1689 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO), 1690 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI), 1691 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXQDC), 1692 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXNDA), 1693 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC), 1694 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC), 1695 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD)); 1696 #endif 1697 } 1698 } 1699 1700 static void 1701 jme_ifwatchdog(struct ifnet *ifp) 1702 { 1703 jme_softc_t *sc = ifp->if_softc; 1704 1705 if ((ifp->if_flags & IFF_RUNNING) == 0) 1706 return; 1707 printf("%s: device timeout\n", device_xname(sc->jme_dev)); 1708 ifp->if_oerrors++; 1709 jme_init(ifp, 0); 1710 } 1711 1712 static int 1713 jme_mediachange(struct ifnet *ifp) 1714 { 1715 int error; 1716 jme_softc_t *sc = ifp->if_softc; 1717 1718 if ((error = mii_mediachg(&sc->jme_mii)) == ENXIO) 1719 error = 0; 1720 else if (error != 0) { 1721 aprint_error_dev(sc->jme_dev, "could not set media\n"); 1722 return error; 1723 } 1724 return 0; 1725 } 1726 1727 static void 1728 jme_ticks(void *v) 1729 { 1730 jme_softc_t *sc = v; 1731 int s = splnet(); 1732 1733 /* Tick the MII. */ 1734 mii_tick(&sc->jme_mii); 1735 1736 /* every seconds */ 1737 callout_reset(&sc->jme_tick_ch, hz, jme_ticks, sc); 1738 splx(s); 1739 } 1740 1741 static void 1742 jme_mac_config(jme_softc_t *sc) 1743 { 1744 uint32_t ghc, gpreg, rxmac, txmac, txpause; 1745 struct mii_data *mii = &sc->jme_mii; 1746 1747 ghc = 0; 1748 rxmac = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC); 1749 rxmac &= ~RXMAC_FC_ENB; 1750 txmac = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC); 1751 txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST); 1752 txpause = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC); 1753 txpause &= ~TXPFC_PAUSE_ENB; 1754 1755 if (mii->mii_media_active & IFM_FDX) { 1756 ghc |= GHC_FULL_DUPLEX; 1757 rxmac &= ~RXMAC_COLL_DET_ENB; 1758 txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | 1759 TXMAC_BACKOFF | TXMAC_CARRIER_EXT | 1760 TXMAC_FRAME_BURST); 1761 /* Disable retry transmit timer/retry limit. */ 1762 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD, 1763 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD) 1764 & ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB)); 1765 } else { 1766 rxmac |= RXMAC_COLL_DET_ENB; 1767 txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF; 1768 /* Enable retry transmit timer/retry limit. */ 1769 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD, 1770 bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD) | TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB); 1771 } 1772 /* Reprogram Tx/Rx MACs with resolved speed/duplex. */ 1773 switch (IFM_SUBTYPE(mii->mii_media_active)) { 1774 case IFM_10_T: 1775 ghc |= GHC_SPEED_10 | GHC_CLKSRC_10_100; 1776 break; 1777 case IFM_100_TX: 1778 ghc |= GHC_SPEED_100 | GHC_CLKSRC_10_100; 1779 break; 1780 case IFM_1000_T: 1781 ghc |= GHC_SPEED_1000 | GHC_CLKSRC_1000; 1782 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0) 1783 txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST; 1784 break; 1785 default: 1786 break; 1787 } 1788 if ((sc->jme_flags & JME_FLAG_GIGA) && 1789 sc->jme_chip_rev == DEVICEREVID_JMC250_A2) { 1790 /* 1791 * Workaround occasional packet loss issue of JMC250 A2 1792 * when it runs on half-duplex media. 1793 */ 1794 #ifdef JMEDEBUG 1795 printf("JME250 A2 workaround\n"); 1796 #endif 1797 gpreg = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, 1798 JME_GPREG1); 1799 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) 1800 gpreg &= ~GPREG1_HDPX_FIX; 1801 else 1802 gpreg |= GPREG1_HDPX_FIX; 1803 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, 1804 JME_GPREG1, gpreg); 1805 /* Workaround CRC errors at 100Mbps on JMC250 A2. */ 1806 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { 1807 /* Extend interface FIFO depth. */ 1808 jme_mii_write(sc->jme_dev, sc->jme_phyaddr, 1809 0x1B, 0x0000); 1810 } else { 1811 /* Select default interface FIFO depth. */ 1812 jme_mii_write(sc->jme_dev, sc->jme_phyaddr, 1813 0x1B, 0x0004); 1814 } 1815 } 1816 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, ghc); 1817 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, rxmac); 1818 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC, txmac); 1819 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC, txpause); 1820 } 1821 1822 static void 1823 jme_set_filter(jme_softc_t *sc) 1824 { 1825 struct ifnet *ifp = &sc->jme_if; 1826 struct ether_multistep step; 1827 struct ether_multi *enm; 1828 uint32_t hash[2] = {0, 0}; 1829 int i; 1830 uint32_t rxcfg; 1831 1832 rxcfg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC); 1833 rxcfg &= ~ (RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST | 1834 RXMAC_ALLMULTI); 1835 /* Always accept frames destined to our station address. */ 1836 rxcfg |= RXMAC_UNICAST; 1837 if ((ifp->if_flags & IFF_BROADCAST) != 0) 1838 rxcfg |= RXMAC_BROADCAST; 1839 if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) { 1840 if ((ifp->if_flags & IFF_PROMISC) != 0) 1841 rxcfg |= RXMAC_PROMISC; 1842 if ((ifp->if_flags & IFF_ALLMULTI) != 0) 1843 rxcfg |= RXMAC_ALLMULTI; 1844 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 1845 JME_MAR0, 0xFFFFFFFF); 1846 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 1847 JME_MAR1, 0xFFFFFFFF); 1848 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, 1849 JME_RXMAC, rxcfg); 1850 return; 1851 } 1852 /* 1853 * Set up the multicast address filter by passing all multicast 1854 * addresses through a CRC generator, and then using the low-order 1855 * 6 bits as an index into the 64 bit multicast hash table. The 1856 * high order bits select the register, while the rest of the bits 1857 * select the bit within the register. 1858 */ 1859 rxcfg |= RXMAC_MULTICAST; 1860 memset(hash, 0, sizeof(hash)); 1861 1862 ETHER_FIRST_MULTI(step, &sc->jme_ec, enm); 1863 while (enm != NULL) { 1864 #ifdef JEMDBUG 1865 printf("%s: addrs %s %s\n", __func__, 1866 ether_sprintf(enm->enm_addrlo), 1867 ether_sprintf(enm->enm_addrhi)); 1868 #endif 1869 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) { 1870 i = ether_crc32_be(enm->enm_addrlo, 6); 1871 /* Just want the 6 least significant bits. */ 1872 i &= 0x3f; 1873 hash[i / 32] |= 1 << (i%32); 1874 } else { 1875 hash[0] = hash[1] = 0xffffffff; 1876 sc->jme_if.if_flags |= IFF_ALLMULTI; 1877 break; 1878 } 1879 ETHER_NEXT_MULTI(step, enm); 1880 } 1881 #ifdef JMEDEBUG 1882 printf("%s: hash1 %x has2 %x\n", __func__, hash[0], hash[1]); 1883 #endif 1884 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR0, hash[0]); 1885 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR1, hash[1]); 1886 bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, rxcfg); 1887 } 1888 1889 #if 0 1890 static int 1891 jme_multicast_hash(uint8_t *a) 1892 { 1893 int hash; 1894 1895 #define DA(addr,bit) (addr[5 - (bit / 8)] & (1 << (bit % 8))) 1896 #define xor8(a,b,c,d,e,f,g,h) \ 1897 (((a != 0) + (b != 0) + (c != 0) + (d != 0) + \ 1898 (e != 0) + (f != 0) + (g != 0) + (h != 0)) & 1) 1899 1900 hash = xor8(DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30), 1901 DA(a,36), DA(a,42)); 1902 hash |= xor8(DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31), 1903 DA(a,37), DA(a,43)) << 1; 1904 hash |= xor8(DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32), 1905 DA(a,38), DA(a,44)) << 2; 1906 hash |= xor8(DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33), 1907 DA(a,39), DA(a,45)) << 3; 1908 hash |= xor8(DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34), 1909 DA(a,40), DA(a,46)) << 4; 1910 hash |= xor8(DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35), 1911 DA(a,41), DA(a,47)) << 5; 1912 1913 return hash; 1914 } 1915 #endif 1916 1917 static int 1918 jme_eeprom_read_byte(struct jme_softc *sc, uint8_t addr, uint8_t *val) 1919 { 1920 uint32_t reg; 1921 int i; 1922 1923 *val = 0; 1924 for (i = JME_EEPROM_TIMEOUT / 10; i > 0; i--) { 1925 reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy, 1926 JME_SMBCSR); 1927 if ((reg & SMBCSR_HW_BUSY_MASK) == SMBCSR_HW_IDLE) 1928 break; 1929 delay(10); 1930 } 1931 1932 if (i == 0) { 1933 aprint_error_dev(sc->jme_dev, "EEPROM idle timeout!\n"); 1934 return (ETIMEDOUT); 1935 } 1936 1937 reg = ((uint32_t)addr << SMBINTF_ADDR_SHIFT) & SMBINTF_ADDR_MASK; 1938 bus_space_write_4(sc->jme_bt_phy, sc->jme_bh_phy, 1939 JME_SMBINTF, reg | SMBINTF_RD | SMBINTF_CMD_TRIGGER); 1940 for (i = JME_EEPROM_TIMEOUT / 10; i > 0; i--) { 1941 delay(10); 1942 reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy, 1943 JME_SMBINTF); 1944 if ((reg & SMBINTF_CMD_TRIGGER) == 0) 1945 break; 1946 } 1947 1948 if (i == 0) { 1949 aprint_error_dev(sc->jme_dev, "EEPROM read timeout!\n"); 1950 return (ETIMEDOUT); 1951 } 1952 1953 reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy, JME_SMBINTF); 1954 *val = (reg & SMBINTF_RD_DATA_MASK) >> SMBINTF_RD_DATA_SHIFT; 1955 return (0); 1956 } 1957 1958 1959 static int 1960 jme_eeprom_macaddr(struct jme_softc *sc) 1961 { 1962 uint8_t eaddr[ETHER_ADDR_LEN]; 1963 uint8_t fup, reg, val; 1964 uint32_t offset; 1965 int match; 1966 1967 offset = 0; 1968 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 || 1969 fup != JME_EEPROM_SIG0) 1970 return (ENOENT); 1971 if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 || 1972 fup != JME_EEPROM_SIG1) 1973 return (ENOENT); 1974 match = 0; 1975 do { 1976 if (jme_eeprom_read_byte(sc, offset, &fup) != 0) 1977 break; 1978 if (JME_EEPROM_MKDESC(JME_EEPROM_FUNC0, JME_EEPROM_PAGE_BAR1) 1979 == (fup & (JME_EEPROM_FUNC_MASK|JME_EEPROM_PAGE_MASK))) { 1980 if (jme_eeprom_read_byte(sc, offset + 1, ®) != 0) 1981 break; 1982 if (reg >= JME_PAR0 && 1983 reg < JME_PAR0 + ETHER_ADDR_LEN) { 1984 if (jme_eeprom_read_byte(sc, offset + 2, 1985 &val) != 0) 1986 break; 1987 eaddr[reg - JME_PAR0] = val; 1988 match++; 1989 } 1990 } 1991 if (fup & JME_EEPROM_DESC_END) 1992 break; 1993 1994 /* Try next eeprom descriptor. */ 1995 offset += JME_EEPROM_DESC_BYTES; 1996 } while (match != ETHER_ADDR_LEN && offset < JME_EEPROM_END); 1997 1998 if (match == ETHER_ADDR_LEN) { 1999 memcpy(sc->jme_enaddr, eaddr, ETHER_ADDR_LEN); 2000 return (0); 2001 } 2002 2003 return (ENOENT); 2004 } 2005 2006 /* 2007 * Set up sysctl(3) MIB, hw.jme.* - Individual controllers will be 2008 * set up in jme_pci_attach() 2009 */ 2010 SYSCTL_SETUP(sysctl_jme, "sysctl jme subtree setup") 2011 { 2012 int rc; 2013 const struct sysctlnode *node; 2014 2015 if ((rc = sysctl_createv(clog, 0, NULL, NULL, 2016 0, CTLTYPE_NODE, "hw", NULL, 2017 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) { 2018 goto err; 2019 } 2020 2021 if ((rc = sysctl_createv(clog, 0, NULL, &node, 2022 0, CTLTYPE_NODE, "jme", 2023 SYSCTL_DESCR("jme interface controls"), 2024 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) { 2025 goto err; 2026 } 2027 2028 jme_root_num = node->sysctl_num; 2029 return; 2030 2031 err: 2032 aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc); 2033 } 2034 2035 static int 2036 jme_sysctl_intrxto(SYSCTLFN_ARGS) 2037 { 2038 int error, t; 2039 struct sysctlnode node; 2040 struct jme_softc *sc; 2041 uint32_t reg; 2042 2043 node = *rnode; 2044 sc = node.sysctl_data; 2045 t = sc->jme_intrxto; 2046 node.sysctl_data = &t; 2047 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2048 if (error || newp == NULL) 2049 return error; 2050 2051 if (t < PCCRX_COAL_TO_MIN || t > PCCRX_COAL_TO_MAX) 2052 return EINVAL; 2053 2054 /* 2055 * update the softc with sysctl-changed value, and mark 2056 * for hardware update 2057 */ 2058 sc->jme_intrxto = t; 2059 /* Configure Rx queue 0 packet completion coalescing. */ 2060 reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK; 2061 reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK; 2062 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg); 2063 return 0; 2064 } 2065 2066 static int 2067 jme_sysctl_intrxct(SYSCTLFN_ARGS) 2068 { 2069 int error, t; 2070 struct sysctlnode node; 2071 struct jme_softc *sc; 2072 uint32_t reg; 2073 2074 node = *rnode; 2075 sc = node.sysctl_data; 2076 t = sc->jme_intrxct; 2077 node.sysctl_data = &t; 2078 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2079 if (error || newp == NULL) 2080 return error; 2081 2082 if (t < PCCRX_COAL_PKT_MIN || t > PCCRX_COAL_PKT_MAX) 2083 return EINVAL; 2084 2085 /* 2086 * update the softc with sysctl-changed value, and mark 2087 * for hardware update 2088 */ 2089 sc->jme_intrxct = t; 2090 /* Configure Rx queue 0 packet completion coalescing. */ 2091 reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK; 2092 reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK; 2093 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg); 2094 return 0; 2095 } 2096 2097 static int 2098 jme_sysctl_inttxto(SYSCTLFN_ARGS) 2099 { 2100 int error, t; 2101 struct sysctlnode node; 2102 struct jme_softc *sc; 2103 uint32_t reg; 2104 2105 node = *rnode; 2106 sc = node.sysctl_data; 2107 t = sc->jme_inttxto; 2108 node.sysctl_data = &t; 2109 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2110 if (error || newp == NULL) 2111 return error; 2112 2113 if (t < PCCTX_COAL_TO_MIN || t > PCCTX_COAL_TO_MAX) 2114 return EINVAL; 2115 2116 /* 2117 * update the softc with sysctl-changed value, and mark 2118 * for hardware update 2119 */ 2120 sc->jme_inttxto = t; 2121 /* Configure Tx queue 0 packet completion coalescing. */ 2122 reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK; 2123 reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK; 2124 reg |= PCCTX_COAL_TXQ0; 2125 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg); 2126 return 0; 2127 } 2128 2129 static int 2130 jme_sysctl_inttxct(SYSCTLFN_ARGS) 2131 { 2132 int error, t; 2133 struct sysctlnode node; 2134 struct jme_softc *sc; 2135 uint32_t reg; 2136 2137 node = *rnode; 2138 sc = node.sysctl_data; 2139 t = sc->jme_inttxct; 2140 node.sysctl_data = &t; 2141 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2142 if (error || newp == NULL) 2143 return error; 2144 2145 if (t < PCCTX_COAL_PKT_MIN || t > PCCTX_COAL_PKT_MAX) 2146 return EINVAL; 2147 2148 /* 2149 * update the softc with sysctl-changed value, and mark 2150 * for hardware update 2151 */ 2152 sc->jme_inttxct = t; 2153 /* Configure Tx queue 0 packet completion coalescing. */ 2154 reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK; 2155 reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK; 2156 reg |= PCCTX_COAL_TXQ0; 2157 bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg); 2158 return 0; 2159 } 2160