1/*- 2 * Copyright (c) 2010-2011 Juli Mallett <jmallett@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 *
|
26 * $FreeBSD: head/sys/mips/cavium/if_octm.c 231987 2012-02-22 01:30:25Z gonzo $
|
26 * $FreeBSD: head/sys/mips/cavium/if_octm.c 232289 2012-02-29 05:48:29Z gonzo $ |
27 */ 28 29/* 30 * Cavium Octeon management port Ethernet devices. 31 */ 32 33#include "opt_inet.h" 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/bus.h> 38#include <sys/endian.h> 39#include <sys/kernel.h> 40#include <sys/mbuf.h> 41#include <sys/lock.h> 42#include <sys/module.h> 43#include <sys/mutex.h> 44#include <sys/rman.h> 45#include <sys/socket.h> 46#include <sys/sockio.h> 47#include <sys/sysctl.h> 48 49#include <net/bpf.h> 50#include <net/ethernet.h> 51#include <net/if.h> 52#include <net/if_dl.h> 53#include <net/if_media.h> 54#include <net/if_types.h> 55#include <net/if_var.h> 56#include <net/if_vlan_var.h> 57 58#ifdef INET 59#include <netinet/in.h> 60#include <netinet/if_ether.h> 61#endif 62 63#include <contrib/octeon-sdk/cvmx.h> 64#include <contrib/octeon-sdk/cvmx-interrupt.h> 65#include <contrib/octeon-sdk/cvmx-mgmt-port.h>
|
66#include "octe/ethernet-common.h"
|
66 67struct octm_softc { 68 struct ifnet *sc_ifp; 69 device_t sc_dev; 70 unsigned sc_port; 71 int sc_flags; 72 struct ifmedia sc_ifmedia; 73 struct resource *sc_intr; 74 void *sc_intr_cookie; 75}; 76 77static void octm_identify(driver_t *, device_t); 78static int octm_probe(device_t); 79static int octm_attach(device_t); 80static int octm_detach(device_t); 81static int octm_shutdown(device_t); 82 83static void octm_init(void *); 84static int octm_transmit(struct ifnet *, struct mbuf *); 85 86static int octm_medchange(struct ifnet *); 87static void octm_medstat(struct ifnet *, struct ifmediareq *); 88 89static int octm_ioctl(struct ifnet *, u_long, caddr_t); 90 91static void octm_rx_intr(void *); 92 93static device_method_t octm_methods[] = { 94 /* Device interface */ 95 DEVMETHOD(device_identify, octm_identify), 96 DEVMETHOD(device_probe, octm_probe), 97 DEVMETHOD(device_attach, octm_attach), 98 DEVMETHOD(device_detach, octm_detach), 99 DEVMETHOD(device_shutdown, octm_shutdown), 100 101 { 0, 0 } 102}; 103 104static driver_t octm_driver = { 105 "octm", 106 octm_methods, 107 sizeof (struct octm_softc), 108}; 109 110static devclass_t octm_devclass; 111 112DRIVER_MODULE(octm, ciu, octm_driver, octm_devclass, 0, 0); 113 114static void 115octm_identify(driver_t *drv, device_t parent) 116{ 117 unsigned i; 118 119 if (!octeon_has_feature(OCTEON_FEATURE_MGMT_PORT)) 120 return; 121 122 for (i = 0; i < CVMX_MGMT_PORT_NUM_PORTS; i++) 123 BUS_ADD_CHILD(parent, 0, "octm", i); 124} 125 126static int 127octm_probe(device_t dev) 128{ 129 cvmx_mgmt_port_result_t result; 130 131 result = cvmx_mgmt_port_initialize(device_get_unit(dev)); 132 switch (result) { 133 case CVMX_MGMT_PORT_SUCCESS: 134 break; 135 case CVMX_MGMT_PORT_NO_MEMORY: 136 return (ENOBUFS); 137 case CVMX_MGMT_PORT_INVALID_PARAM: 138 return (ENXIO); 139 case CVMX_MGMT_PORT_INIT_ERROR: 140 return (EIO); 141 } 142 143 device_set_desc(dev, "Cavium Octeon Management Ethernet"); 144 145 return (0); 146} 147 148static int 149octm_attach(device_t dev) 150{ 151 struct ifnet *ifp; 152 struct octm_softc *sc; 153 cvmx_mixx_irhwm_t mixx_irhwm; 154 cvmx_mixx_intena_t mixx_intena; 155 uint64_t mac; 156 int error; 157 int irq; 158 int rid; 159 160 sc = device_get_softc(dev); 161 sc->sc_dev = dev; 162 sc->sc_port = device_get_unit(dev); 163 164 switch (sc->sc_port) { 165 case 0: 166 irq = CVMX_IRQ_MII; 167 break; 168 case 1: 169 irq = CVMX_IRQ_MII1; 170 break; 171 default: 172 device_printf(dev, "unsupported management port %u.\n", sc->sc_port); 173 return (ENXIO); 174 } 175 176 /* 177 * Set MAC address for this management port. 178 */
|
180 if (cvm_assign_mac_address(&mac, NULL) != 0) {
181 device_printf(dev, "unable to allocate MAC address.\n");
182 return (ENXIO);
183 }
|
179 mac = 0; 180 memcpy((u_int8_t *)&mac + 2, cvmx_sysinfo_get()->mac_addr_base, 6); 181 mac += sc->sc_port; 182 |
183 cvmx_mgmt_port_set_mac(sc->sc_port, mac); 184 185 /* No watermark for input ring. */ 186 mixx_irhwm.u64 = 0; 187 cvmx_write_csr(CVMX_MIXX_IRHWM(sc->sc_port), mixx_irhwm.u64); 188 189 /* Enable input ring interrupts. */ 190 mixx_intena.u64 = 0; 191 mixx_intena.s.ithena = 1; 192 cvmx_write_csr(CVMX_MIXX_INTENA(sc->sc_port), mixx_intena.u64); 193 194 /* Allocate and establish interrupt. */ 195 rid = 0; 196 sc->sc_intr = bus_alloc_resource(sc->sc_dev, SYS_RES_IRQ, &rid, 197 irq, irq, 1, RF_ACTIVE); 198 if (sc->sc_intr == NULL) { 199 device_printf(dev, "unable to allocate IRQ.\n"); 200 return (ENXIO); 201 } 202 203 error = bus_setup_intr(sc->sc_dev, sc->sc_intr, INTR_TYPE_NET, NULL, 204 octm_rx_intr, sc, &sc->sc_intr_cookie); 205 if (error != 0) { 206 device_printf(dev, "unable to setup interrupt.\n"); 207 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr); 208 return (ENXIO); 209 } 210 211 bus_describe_intr(sc->sc_dev, sc->sc_intr, sc->sc_intr_cookie, "rx"); 212 213 /* XXX Possibly should enable TX interrupts. */ 214 215 ifp = if_alloc(IFT_ETHER); 216 if (ifp == NULL) { 217 device_printf(dev, "cannot allocate ifnet.\n"); 218 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr); 219 return (ENOMEM); 220 } 221 222 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 223 ifp->if_mtu = ETHERMTU; 224 ifp->if_init = octm_init; 225 ifp->if_softc = sc; 226 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_ALLMULTI; 227 ifp->if_ioctl = octm_ioctl; 228 229 sc->sc_ifp = ifp; 230 sc->sc_flags = ifp->if_flags; 231 232 ifmedia_init(&sc->sc_ifmedia, 0, octm_medchange, octm_medstat); 233 234 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL); 235 ifmedia_set(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO); 236 237 ether_ifattach(ifp, (const u_int8_t *)&mac + 2); 238 239 ifp->if_transmit = octm_transmit; 240 241 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 242 ifp->if_capabilities = IFCAP_VLAN_MTU; 243 ifp->if_capenable = ifp->if_capabilities; 244 245 IFQ_SET_MAXLEN(&ifp->if_snd, CVMX_MGMT_PORT_NUM_TX_BUFFERS); 246 ifp->if_snd.ifq_drv_maxlen = CVMX_MGMT_PORT_NUM_TX_BUFFERS; 247 IFQ_SET_READY(&ifp->if_snd); 248 249 return (bus_generic_attach(dev)); 250} 251 252static int 253octm_detach(device_t dev) 254{ 255 struct octm_softc *sc; 256 cvmx_mgmt_port_result_t result; 257 258 sc = device_get_softc(dev); 259 260 result = cvmx_mgmt_port_initialize(sc->sc_port); 261 switch (result) { 262 case CVMX_MGMT_PORT_SUCCESS: 263 break; 264 case CVMX_MGMT_PORT_NO_MEMORY: 265 return (ENOBUFS); 266 case CVMX_MGMT_PORT_INVALID_PARAM: 267 return (ENXIO); 268 case CVMX_MGMT_PORT_INIT_ERROR: 269 return (EIO); 270 } 271 272 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr); 273 /* XXX Incomplete. */ 274 275 return (0); 276} 277 278static int 279octm_shutdown(device_t dev) 280{ 281 return (octm_detach(dev)); 282} 283 284static void 285octm_init(void *arg) 286{ 287 struct ifnet *ifp; 288 struct octm_softc *sc; 289 cvmx_mgmt_port_netdevice_flags_t flags; 290 uint64_t mac; 291 292 sc = arg; 293 ifp = sc->sc_ifp; 294 295 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 296 cvmx_mgmt_port_disable(sc->sc_port); 297 298 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 299 } 300 301 if (((ifp->if_flags ^ sc->sc_flags) & (IFF_ALLMULTI | IFF_MULTICAST | IFF_PROMISC)) != 0) { 302 flags = 0; 303 if ((ifp->if_flags & IFF_ALLMULTI) != 0) 304 flags |= CVMX_IFF_ALLMULTI; 305 if ((ifp->if_flags & IFF_PROMISC) != 0) 306 flags |= CVMX_IFF_PROMISC; 307 cvmx_mgmt_port_set_multicast_list(sc->sc_port, flags); 308 } 309 310 mac = 0; 311 memcpy((u_int8_t *)&mac + 2, IF_LLADDR(ifp), 6); 312 cvmx_mgmt_port_set_mac(sc->sc_port, mac); 313 314 /* XXX link state? */ 315 316 if ((ifp->if_flags & IFF_UP) != 0) 317 cvmx_mgmt_port_enable(sc->sc_port); 318 319 ifp->if_drv_flags |= IFF_DRV_RUNNING; 320 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 321} 322 323static int 324octm_transmit(struct ifnet *ifp, struct mbuf *m) 325{ 326 struct octm_softc *sc; 327 cvmx_mgmt_port_result_t result; 328 329 sc = ifp->if_softc; 330 331 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 332 IFF_DRV_RUNNING) { 333 m_freem(m); 334 return (0); 335 } 336 337 result = cvmx_mgmt_port_sendm(sc->sc_port, m); 338 339 if (result == CVMX_MGMT_PORT_SUCCESS) { 340 ETHER_BPF_MTAP(ifp, m); 341 342 ifp->if_opackets++; 343 ifp->if_obytes += m->m_pkthdr.len; 344 } else 345 ifp->if_oerrors++; 346 347 m_freem(m); 348 349 switch (result) { 350 case CVMX_MGMT_PORT_SUCCESS: 351 return (0); 352 case CVMX_MGMT_PORT_NO_MEMORY: 353 return (ENOBUFS); 354 case CVMX_MGMT_PORT_INVALID_PARAM: 355 return (ENXIO); 356 case CVMX_MGMT_PORT_INIT_ERROR: 357 return (EIO); 358 default: 359 return (EDOOFUS); 360 } 361} 362 363static int 364octm_medchange(struct ifnet *ifp) 365{ 366 return (ENOTSUP); 367} 368 369static void 370octm_medstat(struct ifnet *ifp, struct ifmediareq *ifm) 371{ 372 struct octm_softc *sc; 373 cvmx_helper_link_info_t link_info; 374 375 sc = ifp->if_softc; 376 377 ifm->ifm_status = IFM_AVALID; 378 ifm->ifm_active = IFT_ETHER; 379 380 link_info = cvmx_mgmt_port_link_get(sc->sc_port); 381 if (!link_info.s.link_up) 382 return; 383 384 ifm->ifm_status |= IFM_ACTIVE; 385 386 switch (link_info.s.speed) { 387 case 10: 388 ifm->ifm_active |= IFM_10_T; 389 break; 390 case 100: 391 ifm->ifm_active |= IFM_100_TX; 392 break; 393 case 1000: 394 ifm->ifm_active |= IFM_1000_T; 395 break; 396 case 10000: 397 ifm->ifm_active |= IFM_10G_T; 398 break; 399 } 400 401 if (link_info.s.full_duplex) 402 ifm->ifm_active |= IFM_FDX; 403 else 404 ifm->ifm_active |= IFM_HDX; 405} 406 407static int 408octm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 409{ 410 struct octm_softc *sc; 411 struct ifreq *ifr; 412#ifdef INET 413 struct ifaddr *ifa; 414#endif 415 int error; 416 417 sc = ifp->if_softc; 418 ifr = (struct ifreq *)data; 419#ifdef INET 420 ifa = (struct ifaddr *)data; 421#endif 422 423 switch (cmd) { 424 case SIOCSIFADDR: 425#ifdef INET 426 /* 427 * Avoid reinitialization unless it's necessary. 428 */ 429 if (ifa->ifa_addr->sa_family == AF_INET) { 430 ifp->if_flags |= IFF_UP; 431 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 432 octm_init(sc); 433 arp_ifinit(ifp, ifa); 434 435 return (0); 436 } 437#endif 438 error = ether_ioctl(ifp, cmd, data); 439 if (error != 0) 440 return (error); 441 return (0); 442 443 case SIOCSIFFLAGS: 444 if (ifp->if_flags == sc->sc_flags) 445 return (0); 446 if ((ifp->if_flags & IFF_UP) != 0) { 447 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 448 octm_init(sc); 449 } else { 450 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 451 cvmx_mgmt_port_disable(sc->sc_port); 452 453 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 454 } 455 } 456 sc->sc_flags = ifp->if_flags; 457 return (0); 458 459 case SIOCSIFCAP: 460 /* 461 * Just change the capabilities in software, currently none 462 * require reprogramming hardware, they just toggle whether we 463 * make use of already-present facilities in software. 464 */ 465 ifp->if_capenable = ifr->ifr_reqcap; 466 return (0); 467 468 case SIOCSIFMTU: 469 cvmx_mgmt_port_set_max_packet_size(sc->sc_port, ifr->ifr_mtu + ifp->if_data.ifi_hdrlen); 470 return (0); 471 472 case SIOCSIFMEDIA: 473 case SIOCGIFMEDIA: 474 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd); 475 if (error != 0) 476 return (error); 477 return (0); 478 479 default: 480 error = ether_ioctl(ifp, cmd, data); 481 if (error != 0) 482 return (error); 483 return (0); 484 } 485} 486 487static void 488octm_rx_intr(void *arg) 489{ 490 struct octm_softc *sc = arg; 491 cvmx_mixx_isr_t mixx_isr; 492 int len; 493 494 mixx_isr.u64 = cvmx_read_csr(CVMX_MIXX_ISR(sc->sc_port)); 495 if (!mixx_isr.s.irthresh) { 496 device_printf(sc->sc_dev, "stray interrupt.\n"); 497 return; 498 } 499 500 for (;;) { 501 struct mbuf *m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 502 if (m == NULL) { 503 device_printf(sc->sc_dev, "no memory for receive mbuf.\n"); 504 return; 505 } 506 507 508 len = cvmx_mgmt_port_receive(sc->sc_port, MCLBYTES, m->m_data); 509 if (len > 0) { 510 m->m_pkthdr.rcvif = sc->sc_ifp; 511 m->m_pkthdr.len = m->m_len = len; 512 513 sc->sc_ifp->if_ipackets++; 514 515 (*sc->sc_ifp->if_input)(sc->sc_ifp, m); 516 517 continue; 518 } 519 520 m_freem(m); 521 522 if (len == 0) 523 break; 524 525 sc->sc_ifp->if_ierrors++; 526 } 527 528 /* Acknowledge interrupts. */ 529 cvmx_write_csr(CVMX_MIXX_ISR(sc->sc_port), mixx_isr.u64); 530 cvmx_read_csr(CVMX_MIXX_ISR(sc->sc_port)); 531}
|