2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2013 Chelsio Communications, Inc.
6 * Written by: Navdeep Parhar <np@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/eventhandler.h>
37 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
43 #include <net/ethernet.h>
45 #include <net/if_clone.h>
46 #include <net/if_types.h>
48 #include "common/common.h"
49 #include "common/t4_msg.h"
50 #include "common/t4_regs.h"
57 * An interface cloner is registered during mod_load and it can be used to
58 * create or destroy the tracing ifnet for an adapter at any time. It is
59 * possible for the cloned interface to outlive the adapter (adapter disappears
60 * in t4_detach but the tracing ifnet may live till mod_unload when removal of
61 * the cloner finally destroys any remaining cloned interfaces). When tracing
62 * filters are active, this ifnet is also receiving data. There are potential
63 * bad races between ifnet create, ifnet destroy, ifnet rx, ifnet ioctl,
64 * cxgbe_detach/t4_detach, mod_unload.
66 * a) The driver selects an iq for tracing (sc->traceq) inside a synch op. The
67 * iq is destroyed inside a synch op too (and sc->traceq updated).
68 * b) The cloner looks for an adapter that matches the name of the ifnet it's
69 * been asked to create, starts a synch op on that adapter, and proceeds only
70 * if the adapter has a tracing iq.
71 * c) The cloned ifnet and the adapter are coupled to each other via
72 * ifp->if_softc and sc->ifp. These can be modified only with the global
73 * t4_trace_lock sx as well as the sc->ifp_lock mutex held. Holding either
74 * of these will prevent any change.
76 * The order in which all the locks involved should be acquired are:
79 * (begin synch op and let go of the above two)
84 static struct sx t4_trace_lock;
85 static const char *t4_cloner_name = "tXnex";
86 static struct if_clone *t4_cloner;
88 /* tracer ifnet routines. mostly no-ops. */
89 static void tracer_init(void *);
90 static int tracer_ioctl(if_t, unsigned long, caddr_t);
91 static int tracer_transmit(if_t, struct mbuf *);
92 static void tracer_qflush(if_t);
93 static int tracer_media_change(if_t);
94 static void tracer_media_status(if_t, struct ifmediareq *);
96 /* match name (request/response) */
99 int lock; /* set to 1 to returned sc locked. */
105 match_name(struct adapter *sc, void *arg)
107 struct match_rr *mrr = arg;
109 if (strcmp(device_get_nameunit(sc->dev), mrr->name) != 0)
112 KASSERT(mrr->sc == NULL, ("%s: multiple matches (%p, %p) for %s",
113 __func__, mrr->sc, sc, mrr->name));
117 mrr->rc = begin_synchronized_op(mrr->sc, NULL, 0, "t4clon");
123 t4_cloner_match(struct if_clone *ifc, const char *name)
126 if (strncmp(name, "t4nex", 5) != 0 &&
127 strncmp(name, "t5nex", 5) != 0 &&
128 strncmp(name, "t6nex", 5) != 0)
130 if (name[5] < '0' || name[5] > '9')
136 t4_cloner_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
142 const uint8_t lla[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
148 t4_iterate(match_name, &mrr);
154 KASSERT(sc != NULL, ("%s: name (%s) matched but softc is NULL",
156 ASSERT_SYNCHRONIZED_OP(sc);
158 sx_xlock(&t4_trace_lock);
160 if (sc->ifp != NULL) {
164 if (sc->traceq < 0) {
169 ifp = if_alloc(IFT_ETHER);
175 /* Note that if_xname is identical to the nexus nameunit */
176 if_initname(ifp, name, -1);
177 if_setdname(ifp, t4_cloner_name);
178 if_setinitfn(ifp, tracer_init);
179 if_setflags(ifp, IFF_SIMPLEX | IFF_DRV_RUNNING);
180 if_setioctlfn(ifp, tracer_ioctl);
181 if_settransmitfn(ifp, tracer_transmit);
182 if_setqflushfn(ifp, tracer_qflush);
183 if_setcapabilities(ifp, IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU);
184 ifmedia_init(&sc->media, IFM_IMASK, tracer_media_change,
185 tracer_media_status);
186 ifmedia_add(&sc->media, IFM_ETHER | IFM_FDX | IFM_NONE, 0, NULL);
187 ifmedia_set(&sc->media, IFM_ETHER | IFM_FDX | IFM_NONE);
188 sx_xunlock(&t4_trace_lock);
189 ether_ifattach(ifp, lla);
190 sx_xlock(&t4_trace_lock);
191 mtx_lock(&sc->ifp_lock);
192 if_setsoftc(ifp, sc);
194 mtx_unlock(&sc->ifp_lock);
197 sx_xunlock(&t4_trace_lock);
198 end_synchronized_op(sc, 0);
203 t4_cloner_destroy(struct if_clone *ifc, if_t ifp)
207 sx_xlock(&t4_trace_lock);
208 sc = if_getsoftc(ifp);
210 mtx_lock(&sc->ifp_lock);
212 if_setsoftc(ifp, NULL);
213 mtx_unlock(&sc->ifp_lock);
214 ifmedia_removeall(&sc->media);
216 sx_xunlock(&t4_trace_lock);
224 t4_tracer_modload(void)
227 sx_init(&t4_trace_lock, "T4/T5 tracer lock");
228 t4_cloner = if_clone_advanced(t4_cloner_name, 0, t4_cloner_match,
229 t4_cloner_create, t4_cloner_destroy);
233 t4_tracer_modunload(void)
236 if (t4_cloner != NULL) {
238 * The module is being unloaded so the nexus drivers have
239 * detached. The tracing interfaces can not outlive the nexus
240 * (ifp->if_softc is the nexus) and must have been destroyed
241 * already. XXX: but if_clone is opaque to us and we can't
242 * assert LIST_EMPTY(&t4_cloner->ifc_iflist) at this time.
244 if_clone_detach(t4_cloner);
246 sx_destroy(&t4_trace_lock);
250 t4_tracer_port_detach(struct adapter *sc)
253 sx_xlock(&t4_trace_lock);
254 if (sc->ifp != NULL) {
255 mtx_lock(&sc->ifp_lock);
256 if_setsoftc(sc->ifp, NULL);
258 mtx_unlock(&sc->ifp_lock);
260 ifmedia_removeall(&sc->media);
261 sx_xunlock(&t4_trace_lock);
265 t4_get_tracer(struct adapter *sc, struct t4_tracer *t)
268 struct trace_params tp;
270 if (t->idx >= NTRACE) {
277 rc = begin_synchronized_op(sc, NULL, HOLD_LOCK | SLEEP_OK | INTR_OK,
282 if (hw_off_limits(sc)) {
287 for (i = t->idx; i < NTRACE; i++) {
288 if (isset(&sc->tracer_valid, t->idx)) {
289 t4_get_trace_filter(sc, &tp, i, &enabled);
291 t->enabled = enabled;
293 memcpy(&t->tp.data[0], &tp.data[0], sizeof(t->tp.data));
294 memcpy(&t->tp.mask[0], &tp.mask[0], sizeof(t->tp.mask));
295 t->tp.snap_len = tp.snap_len;
296 t->tp.min_len = tp.min_len;
297 t->tp.skip_ofst = tp.skip_ofst;
298 t->tp.skip_len = tp.skip_len;
299 t->tp.invert = tp.invert;
301 /* convert channel to port iff 0 <= port < 8. */
303 t->tp.port = sc->chan_map[tp.port];
304 else if (tp.port < 8)
305 t->tp.port = sc->chan_map[tp.port - 4] + 4;
307 t->tp.port = tp.port;
317 end_synchronized_op(sc, LOCK_HELD);
323 t4_set_tracer(struct adapter *sc, struct t4_tracer *t)
326 struct trace_params tp, *tpp;
328 if (t->idx >= NTRACE)
331 rc = begin_synchronized_op(sc, NULL, HOLD_LOCK | SLEEP_OK | INTR_OK,
336 if (hw_off_limits(sc)) {
342 * If no tracing filter is specified this time then check if the filter
343 * at the index is valid anyway because it was set previously. If so
344 * then this is a legitimate enable/disable operation.
347 if (isset(&sc->tracer_valid, t->idx))
354 if (t->tp.port > 19 || t->tp.snap_len > 9600 ||
355 t->tp.min_len > M_TFMINPKTSIZE || t->tp.skip_len > M_TFLENGTH ||
356 t->tp.skip_ofst > M_TFOFFSET) {
361 memcpy(&tp.data[0], &t->tp.data[0], sizeof(tp.data));
362 memcpy(&tp.mask[0], &t->tp.mask[0], sizeof(tp.mask));
363 tp.snap_len = t->tp.snap_len;
364 tp.min_len = t->tp.min_len;
365 tp.skip_ofst = t->tp.skip_ofst;
366 tp.skip_len = t->tp.skip_len;
367 tp.invert = !!t->tp.invert;
369 /* convert port to channel iff 0 <= port < 8. */
370 if (t->tp.port < 4) {
371 if (sc->port[t->tp.port] == NULL) {
375 tp.port = sc->port[t->tp.port]->tx_chan;
376 } else if (t->tp.port < 8) {
377 if (sc->port[t->tp.port - 4] == NULL) {
381 tp.port = sc->port[t->tp.port - 4]->tx_chan + 4;
383 tp.port = t->tp.port;
387 rc = -t4_set_trace_filter(sc, tpp, t->idx, t->enabled);
390 setbit(&sc->tracer_valid, t->idx);
391 if (sc->tracer_enabled == 0) {
392 t4_set_reg_field(sc, A_MPS_TRC_CFG,
395 setbit(&sc->tracer_enabled, t->idx);
397 clrbit(&sc->tracer_enabled, t->idx);
398 if (sc->tracer_enabled == 0) {
399 t4_set_reg_field(sc, A_MPS_TRC_CFG,
405 end_synchronized_op(sc, LOCK_HELD);
411 t4_trace_pkt(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
413 struct adapter *sc = iq->adapter;
416 KASSERT(m != NULL, ("%s: no payload with opcode %02x", __func__,
419 mtx_lock(&sc->ifp_lock);
422 m_adj(m, sizeof(struct cpl_trace_pkt));
423 m->m_pkthdr.rcvif = ifp;
424 ETHER_BPF_MTAP(ifp, m);
426 mtx_unlock(&sc->ifp_lock);
433 t5_trace_pkt(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
435 struct adapter *sc = iq->adapter;
438 KASSERT(m != NULL, ("%s: no payload with opcode %02x", __func__,
441 mtx_lock(&sc->ifp_lock);
444 m_adj(m, sizeof(struct cpl_t5_trace_pkt));
445 m->m_pkthdr.rcvif = ifp;
446 ETHER_BPF_MTAP(ifp, m);
448 mtx_unlock(&sc->ifp_lock);
456 tracer_init(void *arg)
463 tracer_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
467 struct ifreq *ifr = (struct ifreq *)data;
479 sx_xlock(&t4_trace_lock);
480 sc = if_getsoftc(ifp);
484 rc = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
485 sx_xunlock(&t4_trace_lock);
488 rc = ether_ioctl(ifp, cmd, data);
495 tracer_transmit(if_t ifp, struct mbuf *m)
503 tracer_qflush(if_t ifp)
510 tracer_media_change(if_t ifp)
517 tracer_media_status(if_t ifp, struct ifmediareq *ifmr)
520 ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;