xref: /freebsd/sys/dev/dpaa2/dpaa2_con.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1ba7319e9SDmitry Salychev /*-
2ba7319e9SDmitry Salychev  * SPDX-License-Identifier: BSD-2-Clause
3ba7319e9SDmitry Salychev  *
4ba7319e9SDmitry Salychev  * Copyright © 2021-2022 Dmitry Salychev
5ba7319e9SDmitry Salychev  *
6ba7319e9SDmitry Salychev  * Redistribution and use in source and binary forms, with or without
7ba7319e9SDmitry Salychev  * modification, are permitted provided that the following conditions
8ba7319e9SDmitry Salychev  * are met:
9ba7319e9SDmitry Salychev  * 1. Redistributions of source code must retain the above copyright
10ba7319e9SDmitry Salychev  *    notice, this list of conditions and the following disclaimer.
11ba7319e9SDmitry Salychev  * 2. Redistributions in binary form must reproduce the above copyright
12ba7319e9SDmitry Salychev  *    notice, this list of conditions and the following disclaimer in the
13ba7319e9SDmitry Salychev  *    documentation and/or other materials provided with the distribution.
14ba7319e9SDmitry Salychev  *
15ba7319e9SDmitry Salychev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16ba7319e9SDmitry Salychev  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ba7319e9SDmitry Salychev  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ba7319e9SDmitry Salychev  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ba7319e9SDmitry Salychev  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ba7319e9SDmitry Salychev  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ba7319e9SDmitry Salychev  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ba7319e9SDmitry Salychev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ba7319e9SDmitry Salychev  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ba7319e9SDmitry Salychev  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ba7319e9SDmitry Salychev  * SUCH DAMAGE.
26ba7319e9SDmitry Salychev  */
27ba7319e9SDmitry Salychev 
28ba7319e9SDmitry Salychev #include <sys/cdefs.h>
29ba7319e9SDmitry Salychev /*
30ba7319e9SDmitry Salychev  * The DPAA2 Concentrator (DPCON) driver.
31ba7319e9SDmitry Salychev  *
32ba7319e9SDmitry Salychev  * Supports configuration of QBMan channels for advanced scheduling of ingress
33ba7319e9SDmitry Salychev  * packets from one or more network interfaces.
34ba7319e9SDmitry Salychev  *
35ba7319e9SDmitry Salychev  * DPCONs are used to distribute Rx or Tx Confirmation traffic to different
36ba7319e9SDmitry Salychev  * cores, via affine DPIO objects. The implication is that one DPCON must be
37ba7319e9SDmitry Salychev  * available for each core where Rx or Tx Confirmation traffic should be
38ba7319e9SDmitry Salychev  * distributed to.
39ba7319e9SDmitry Salychev  *
40ba7319e9SDmitry Salychev  * QBMan channel contains several work queues. The WQs within a channel have a
41ba7319e9SDmitry Salychev  * priority relative to each other. Each channel consists of either eight or two
42ba7319e9SDmitry Salychev  * WQs, and thus, there are either eight or two possible priorities in a channel.
43ba7319e9SDmitry Salychev  */
44ba7319e9SDmitry Salychev 
45ba7319e9SDmitry Salychev #include <sys/param.h>
46ba7319e9SDmitry Salychev #include <sys/kernel.h>
47ba7319e9SDmitry Salychev #include <sys/bus.h>
48ba7319e9SDmitry Salychev #include <sys/rman.h>
49ba7319e9SDmitry Salychev #include <sys/module.h>
50ba7319e9SDmitry Salychev #include <sys/malloc.h>
51ba7319e9SDmitry Salychev #include <sys/mutex.h>
52ba7319e9SDmitry Salychev 
53ba7319e9SDmitry Salychev #include <vm/vm.h>
54ba7319e9SDmitry Salychev 
55ba7319e9SDmitry Salychev #include <machine/bus.h>
56ba7319e9SDmitry Salychev #include <machine/resource.h>
57ba7319e9SDmitry Salychev 
58ba7319e9SDmitry Salychev #include <dev/pci/pcivar.h>
59ba7319e9SDmitry Salychev 
60ba7319e9SDmitry Salychev #include "pcib_if.h"
61ba7319e9SDmitry Salychev #include "pci_if.h"
62ba7319e9SDmitry Salychev 
63ba7319e9SDmitry Salychev #include "dpaa2_mcp.h"
64ba7319e9SDmitry Salychev #include "dpaa2_swp.h"
65ba7319e9SDmitry Salychev #include "dpaa2_mc.h"
66ba7319e9SDmitry Salychev #include "dpaa2_cmd_if.h"
67ba7319e9SDmitry Salychev 
68ba7319e9SDmitry Salychev /* DPAA2 Concentrator resource specification. */
69ba7319e9SDmitry Salychev struct resource_spec dpaa2_con_spec[] = {
70ba7319e9SDmitry Salychev 	/*
71ba7319e9SDmitry Salychev 	 * DPMCP resources.
72ba7319e9SDmitry Salychev 	 *
73ba7319e9SDmitry Salychev 	 * NOTE: MC command portals (MCPs) are used to send commands to, and
74ba7319e9SDmitry Salychev 	 *	 receive responses from, the MC firmware. One portal per DPCON.
75ba7319e9SDmitry Salychev 	 */
76ba7319e9SDmitry Salychev #define MCP_RES_NUM	(1u)
77ba7319e9SDmitry Salychev #define MCP_RID_OFF	(0u)
78ba7319e9SDmitry Salychev #define MCP_RID(rid)	((rid) + MCP_RID_OFF)
79ba7319e9SDmitry Salychev 	/* --- */
80ba7319e9SDmitry Salychev 	{ DPAA2_DEV_MCP, MCP_RID(0), RF_ACTIVE | RF_SHAREABLE | RF_OPTIONAL },
81ba7319e9SDmitry Salychev 	/* --- */
82ba7319e9SDmitry Salychev 	RESOURCE_SPEC_END
83ba7319e9SDmitry Salychev };
84ba7319e9SDmitry Salychev 
85ba7319e9SDmitry Salychev static int dpaa2_con_detach(device_t dev);
86ba7319e9SDmitry Salychev 
87ba7319e9SDmitry Salychev /*
88ba7319e9SDmitry Salychev  * Device interface.
89ba7319e9SDmitry Salychev  */
90ba7319e9SDmitry Salychev 
91ba7319e9SDmitry Salychev static int
dpaa2_con_probe(device_t dev)92ba7319e9SDmitry Salychev dpaa2_con_probe(device_t dev)
93ba7319e9SDmitry Salychev {
94ba7319e9SDmitry Salychev 	/* DPCON device will be added by a parent resource container itself. */
95ba7319e9SDmitry Salychev 	device_set_desc(dev, "DPAA2 Concentrator");
96ba7319e9SDmitry Salychev 	return (BUS_PROBE_DEFAULT);
97ba7319e9SDmitry Salychev }
98ba7319e9SDmitry Salychev 
99ba7319e9SDmitry Salychev static int
dpaa2_con_detach(device_t dev)100ba7319e9SDmitry Salychev dpaa2_con_detach(device_t dev)
101ba7319e9SDmitry Salychev {
102*4cd96614SDmitry Salychev 	/* TBD */
103ba7319e9SDmitry Salychev 	return (0);
104ba7319e9SDmitry Salychev }
105ba7319e9SDmitry Salychev 
106ba7319e9SDmitry Salychev static int
dpaa2_con_attach(device_t dev)107ba7319e9SDmitry Salychev dpaa2_con_attach(device_t dev)
108ba7319e9SDmitry Salychev {
109ba7319e9SDmitry Salychev 	device_t pdev = device_get_parent(dev);
110ba7319e9SDmitry Salychev 	device_t child = dev;
111ba7319e9SDmitry Salychev 	device_t mcp_dev;
112ba7319e9SDmitry Salychev 	struct dpaa2_con_softc *sc = device_get_softc(dev);
113ba7319e9SDmitry Salychev 	struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
114ba7319e9SDmitry Salychev 	struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
115ba7319e9SDmitry Salychev 	struct dpaa2_devinfo *mcp_dinfo;
116*4cd96614SDmitry Salychev 	struct dpaa2_cmd cmd;
117*4cd96614SDmitry Salychev 	uint16_t rc_token, con_token;
118ba7319e9SDmitry Salychev 	int error;
119ba7319e9SDmitry Salychev 
120ba7319e9SDmitry Salychev 	sc->dev = dev;
121ba7319e9SDmitry Salychev 
122ba7319e9SDmitry Salychev 	error = bus_alloc_resources(sc->dev, dpaa2_con_spec, sc->res);
123ba7319e9SDmitry Salychev 	if (error) {
124ba7319e9SDmitry Salychev 		device_printf(dev, "%s: failed to allocate resources: "
125ba7319e9SDmitry Salychev 		    "error=%d\n", __func__, error);
126*4cd96614SDmitry Salychev 		goto err_exit;
127ba7319e9SDmitry Salychev 	}
128ba7319e9SDmitry Salychev 
129ba7319e9SDmitry Salychev 	/* Obtain MC portal. */
130ba7319e9SDmitry Salychev 	mcp_dev = (device_t) rman_get_start(sc->res[MCP_RID(0)]);
131ba7319e9SDmitry Salychev 	mcp_dinfo = device_get_ivars(mcp_dev);
132ba7319e9SDmitry Salychev 	dinfo->portal = mcp_dinfo->portal;
133ba7319e9SDmitry Salychev 
134*4cd96614SDmitry Salychev 	DPAA2_CMD_INIT(&cmd);
135*4cd96614SDmitry Salychev 
136*4cd96614SDmitry Salychev 	error = DPAA2_CMD_RC_OPEN(dev, child, &cmd, rcinfo->id, &rc_token);
137ba7319e9SDmitry Salychev 	if (error) {
138*4cd96614SDmitry Salychev 		device_printf(dev, "%s: failed to open DPRC: error=%d\n",
139*4cd96614SDmitry Salychev 		    __func__, error);
140ba7319e9SDmitry Salychev 		goto err_exit;
141ba7319e9SDmitry Salychev 	}
142*4cd96614SDmitry Salychev 	error = DPAA2_CMD_CON_OPEN(dev, child, &cmd, dinfo->id, &con_token);
143ba7319e9SDmitry Salychev 	if (error) {
144*4cd96614SDmitry Salychev 		device_printf(dev, "%s: failed to open DPCON: id=%d, error=%d\n",
145*4cd96614SDmitry Salychev 		    __func__, dinfo->id, error);
146*4cd96614SDmitry Salychev 		goto close_rc;
147ba7319e9SDmitry Salychev 	}
148ba7319e9SDmitry Salychev 
149*4cd96614SDmitry Salychev 	error = DPAA2_CMD_CON_RESET(dev, child, &cmd);
150ba7319e9SDmitry Salychev 	if (error) {
151*4cd96614SDmitry Salychev 		device_printf(dev, "%s: failed to reset DPCON: id=%d, "
152*4cd96614SDmitry Salychev 		    "error=%d\n", __func__, dinfo->id, error);
153*4cd96614SDmitry Salychev 		goto close_con;
154ba7319e9SDmitry Salychev 	}
155*4cd96614SDmitry Salychev 	error = DPAA2_CMD_CON_GET_ATTRIBUTES(dev, child, &cmd, &sc->attr);
156ba7319e9SDmitry Salychev 	if (error) {
157*4cd96614SDmitry Salychev 		device_printf(dev, "%s: failed to get DPCON attributes: id=%d, "
158*4cd96614SDmitry Salychev 		    "error=%d\n", __func__, dinfo->id, error);
159*4cd96614SDmitry Salychev 		goto close_con;
160ba7319e9SDmitry Salychev 	}
161ba7319e9SDmitry Salychev 
162*4cd96614SDmitry Salychev 	if (bootverbose) {
163ba7319e9SDmitry Salychev 		device_printf(dev, "chan_id=%d, priorities=%d\n",
164ba7319e9SDmitry Salychev 		    sc->attr.chan_id, sc->attr.prior_num);
165*4cd96614SDmitry Salychev 	}
166ba7319e9SDmitry Salychev 
167*4cd96614SDmitry Salychev 	(void)DPAA2_CMD_CON_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, con_token));
168*4cd96614SDmitry Salychev 	(void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
169ba7319e9SDmitry Salychev 	return (0);
170ba7319e9SDmitry Salychev 
171*4cd96614SDmitry Salychev close_con:
172*4cd96614SDmitry Salychev 	DPAA2_CMD_CON_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, con_token));
173*4cd96614SDmitry Salychev close_rc:
174*4cd96614SDmitry Salychev 	DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
175ba7319e9SDmitry Salychev err_exit:
176ba7319e9SDmitry Salychev 	return (ENXIO);
177ba7319e9SDmitry Salychev }
178ba7319e9SDmitry Salychev 
179ba7319e9SDmitry Salychev static device_method_t dpaa2_con_methods[] = {
180ba7319e9SDmitry Salychev 	/* Device interface */
181ba7319e9SDmitry Salychev 	DEVMETHOD(device_probe,		dpaa2_con_probe),
182ba7319e9SDmitry Salychev 	DEVMETHOD(device_attach,	dpaa2_con_attach),
183ba7319e9SDmitry Salychev 	DEVMETHOD(device_detach,	dpaa2_con_detach),
184ba7319e9SDmitry Salychev 
185ba7319e9SDmitry Salychev 	DEVMETHOD_END
186ba7319e9SDmitry Salychev };
187ba7319e9SDmitry Salychev 
188ba7319e9SDmitry Salychev static driver_t dpaa2_con_driver = {
189ba7319e9SDmitry Salychev 	"dpaa2_con",
190ba7319e9SDmitry Salychev 	dpaa2_con_methods,
191ba7319e9SDmitry Salychev 	sizeof(struct dpaa2_con_softc),
192ba7319e9SDmitry Salychev };
193ba7319e9SDmitry Salychev 
194ba7319e9SDmitry Salychev DRIVER_MODULE(dpaa2_con, dpaa2_rc, dpaa2_con_driver, 0, 0);
195