2 * Copyright (c) 2013 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
34 #include <sys/param.h>
36 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
42 #include <net/ethernet.h>
44 #include <net/if_clone.h>
45 #include <net/if_types.h>
47 #include "common/common.h"
48 #include "common/t4_msg.h"
49 #include "common/t4_regs.h"
56 * An interface cloner is registered during mod_load and it can be used to
57 * create or destroy the tracing ifnet for an adapter at any time. It is
58 * possible for the cloned interface to outlive the adapter (adapter disappears
59 * in t4_detach but the tracing ifnet may live till mod_unload when removal of
60 * the cloner finally destroys any remaining cloned interfaces). When tracing
61 * filters are active, this ifnet is also receiving data. There are potential
62 * bad races between ifnet create, ifnet destroy, ifnet rx, ifnet ioctl,
63 * cxgbe_detach/t4_detach, mod_unload.
65 * a) The driver selects an iq for tracing (sc->traceq) inside a synch op. The
66 * iq is destroyed inside a synch op too (and sc->traceq updated).
67 * b) The cloner looks for an adapter that matches the name of the ifnet it's
68 * been asked to create, starts a synch op on that adapter, and proceeds only
69 * if the adapter has a tracing iq.
70 * c) The cloned ifnet and the adapter are coupled to each other via
71 * ifp->if_softc and sc->ifp. These can be modified only with the global
72 * t4_trace_lock sx as well as the sc->ifp_lock mutex held. Holding either
73 * of these will prevent any change.
75 * The order in which all the locks involved should be acquired are:
78 * (begin synch op and let go of the above two)
83 static struct sx t4_trace_lock;
84 static const char *t4_cloner_name = "tXnex";
85 static struct if_clone *t4_cloner;
87 /* tracer ifnet routines. mostly no-ops. */
88 static void tracer_init(void *);
89 static int tracer_ioctl(struct ifnet *, unsigned long, caddr_t);
90 static int tracer_transmit(struct ifnet *, struct mbuf *);
91 static void tracer_qflush(struct ifnet *);
92 static int tracer_media_change(struct ifnet *);
93 static void tracer_media_status(struct ifnet *, struct ifmediareq *);
95 /* match name (request/response) */
98 int lock; /* set to 1 to returned sc locked. */
104 match_name(struct adapter *sc, void *arg)
106 struct match_rr *mrr = arg;
108 if (strcmp(device_get_nameunit(sc->dev), mrr->name) != 0)
111 KASSERT(mrr->sc == NULL, ("%s: multiple matches (%p, %p) for %s",
112 __func__, mrr->sc, sc, mrr->name));
116 mrr->rc = begin_synchronized_op(mrr->sc, NULL, 0, "t4clon");
122 t4_cloner_match(struct if_clone *ifc, const char *name)
129 t4_iterate(match_name, &mrr);
131 return (mrr.sc != NULL);
135 t4_cloner_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
141 const uint8_t lla[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
147 t4_iterate(match_name, &mrr);
153 KASSERT(sc != NULL, ("%s: name (%s) matched but softc is NULL",
155 ASSERT_SYNCHRONIZED_OP(sc);
157 sx_xlock(&t4_trace_lock);
159 if (sc->ifp != NULL) {
163 if (sc->traceq < 0) {
170 rc = ifc_alloc_unit(ifc, &unit);
174 ifp = if_alloc(IFT_ETHER);
176 ifc_free_unit(ifc, unit);
181 /* Note that if_xname is not <if_dname><if_dunit>. */
182 strlcpy(ifp->if_xname, name, sizeof(ifp->if_xname));
183 ifp->if_dname = t4_cloner_name;
184 ifp->if_dunit = unit;
185 ifp->if_init = tracer_init;
186 ifp->if_flags = IFF_SIMPLEX | IFF_DRV_RUNNING;
187 ifp->if_ioctl = tracer_ioctl;
188 ifp->if_transmit = tracer_transmit;
189 ifp->if_qflush = tracer_qflush;
190 ifp->if_capabilities = IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
191 ifmedia_init(&sc->media, IFM_IMASK, tracer_media_change,
192 tracer_media_status);
193 ifmedia_add(&sc->media, IFM_ETHER | IFM_FDX | IFM_NONE, 0, NULL);
194 ifmedia_set(&sc->media, IFM_ETHER | IFM_FDX | IFM_NONE);
195 ether_ifattach(ifp, lla);
198 mtx_lock(&sc->ifp_lock);
201 mtx_unlock(&sc->ifp_lock);
203 sx_xunlock(&t4_trace_lock);
204 end_synchronized_op(sc, 0);
209 t4_cloner_destroy(struct if_clone *ifc, struct ifnet *ifp)
212 int unit = ifp->if_dunit;
214 sx_xlock(&t4_trace_lock);
217 mtx_lock(&sc->ifp_lock);
219 ifp->if_softc = NULL;
220 mtx_unlock(&sc->ifp_lock);
221 ifmedia_removeall(&sc->media);
225 ifc_free_unit(ifc, unit);
226 sx_xunlock(&t4_trace_lock);
235 sx_init(&t4_trace_lock, "T4/T5 tracer lock");
236 t4_cloner = if_clone_advanced(t4_cloner_name, 0, t4_cloner_match,
237 t4_cloner_create, t4_cloner_destroy);
241 t4_tracer_modunload()
244 if (t4_cloner != NULL) {
246 * The module is being unloaded so the nexus drivers have
247 * detached. The tracing interfaces can not outlive the nexus
248 * (ifp->if_softc is the nexus) and must have been destroyed
249 * already. XXX: but if_clone is opaque to us and we can't
250 * assert LIST_EMPTY(&t4_cloner->ifc_iflist) at this time.
252 if_clone_detach(t4_cloner);
254 sx_destroy(&t4_trace_lock);
258 t4_tracer_port_detach(struct adapter *sc)
261 sx_xlock(&t4_trace_lock);
262 if (sc->ifp != NULL) {
263 mtx_lock(&sc->ifp_lock);
264 sc->ifp->if_softc = NULL;
266 mtx_unlock(&sc->ifp_lock);
268 ifmedia_removeall(&sc->media);
269 sx_xunlock(&t4_trace_lock);
273 t4_get_tracer(struct adapter *sc, struct t4_tracer *t)
276 struct trace_params tp;
278 if (t->idx >= NTRACE) {
285 rc = begin_synchronized_op(sc, NULL, HOLD_LOCK | SLEEP_OK | INTR_OK,
290 for (i = t->idx; i < NTRACE; i++) {
291 if (isset(&sc->tracer_valid, t->idx)) {
292 t4_get_trace_filter(sc, &tp, i, &enabled);
294 t->enabled = enabled;
296 memcpy(&t->tp.data[0], &tp.data[0], sizeof(t->tp.data));
297 memcpy(&t->tp.mask[0], &tp.mask[0], sizeof(t->tp.mask));
298 t->tp.snap_len = tp.snap_len;
299 t->tp.min_len = tp.min_len;
300 t->tp.skip_ofst = tp.skip_ofst;
301 t->tp.skip_len = tp.skip_len;
302 t->tp.invert = tp.invert;
304 /* convert channel to port iff 0 <= port < 8. */
306 t->tp.port = sc->chan_map[tp.port];
307 else if (tp.port < 8)
308 t->tp.port = sc->chan_map[tp.port - 4] + 4;
310 t->tp.port = tp.port;
320 end_synchronized_op(sc, LOCK_HELD);
326 t4_set_tracer(struct adapter *sc, struct t4_tracer *t)
329 struct trace_params tp, *tpp;
331 if (t->idx >= NTRACE)
334 rc = begin_synchronized_op(sc, NULL, HOLD_LOCK | SLEEP_OK | INTR_OK,
340 * If no tracing filter is specified this time then check if the filter
341 * at the index is valid anyway because it was set previously. If so
342 * then this is a legitimate enable/disable operation.
345 if (isset(&sc->tracer_valid, t->idx))
352 if (t->tp.port > 19 || t->tp.snap_len > 9600 ||
353 t->tp.min_len > M_TFMINPKTSIZE || t->tp.skip_len > M_TFLENGTH ||
354 t->tp.skip_ofst > M_TFOFFSET) {
359 memcpy(&tp.data[0], &t->tp.data[0], sizeof(tp.data));
360 memcpy(&tp.mask[0], &t->tp.mask[0], sizeof(tp.mask));
361 tp.snap_len = t->tp.snap_len;
362 tp.min_len = t->tp.min_len;
363 tp.skip_ofst = t->tp.skip_ofst;
364 tp.skip_len = t->tp.skip_len;
365 tp.invert = !!t->tp.invert;
367 /* convert port to channel iff 0 <= port < 8. */
368 if (t->tp.port < 4) {
369 if (sc->port[t->tp.port] == NULL) {
373 tp.port = sc->port[t->tp.port]->tx_chan;
374 } else if (t->tp.port < 8) {
375 if (sc->port[t->tp.port - 4] == NULL) {
379 tp.port = sc->port[t->tp.port - 4]->tx_chan + 4;
384 rc = -t4_set_trace_filter(sc, tpp, t->idx, t->enabled);
387 setbit(&sc->tracer_valid, t->idx);
388 if (sc->tracer_enabled == 0) {
389 t4_set_reg_field(sc, A_MPS_TRC_CFG,
392 setbit(&sc->tracer_enabled, t->idx);
394 clrbit(&sc->tracer_enabled, t->idx);
395 if (sc->tracer_enabled == 0) {
396 t4_set_reg_field(sc, A_MPS_TRC_CFG,
402 end_synchronized_op(sc, LOCK_HELD);
408 t4_trace_pkt(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
410 struct adapter *sc = iq->adapter;
413 KASSERT(m != NULL, ("%s: no payload with opcode %02x", __func__,
416 mtx_lock(&sc->ifp_lock);
419 m_adj(m, sizeof(struct cpl_trace_pkt));
420 m->m_pkthdr.rcvif = ifp;
421 ETHER_BPF_MTAP(ifp, m);
423 mtx_unlock(&sc->ifp_lock);
430 t5_trace_pkt(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
432 struct adapter *sc = iq->adapter;
435 KASSERT(m != NULL, ("%s: no payload with opcode %02x", __func__,
438 mtx_lock(&sc->ifp_lock);
441 m_adj(m, sizeof(struct cpl_t5_trace_pkt));
442 m->m_pkthdr.rcvif = ifp;
443 ETHER_BPF_MTAP(ifp, m);
445 mtx_unlock(&sc->ifp_lock);
453 tracer_init(void *arg)
460 tracer_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
464 struct ifreq *ifr = (struct ifreq *)data;
475 sx_xlock(&t4_trace_lock);
480 rc = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
481 sx_xunlock(&t4_trace_lock);
484 rc = ether_ioctl(ifp, cmd, data);
491 tracer_transmit(struct ifnet *ifp, struct mbuf *m)
499 tracer_qflush(struct ifnet *ifp)
506 tracer_media_change(struct ifnet *ifp)
513 tracer_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
516 ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;