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