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