xref: /freebsd-13-stable/sys/dev/dpaa2/dpaa2_con.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
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
dpaa2_con_probe(device_t dev)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
dpaa2_con_detach(device_t dev)100 dpaa2_con_detach(device_t dev)
101 {
102 	device_t child = dev;
103 	struct dpaa2_con_softc *sc = device_get_softc(dev);
104 
105 	DPAA2_CMD_CON_CLOSE(dev, child, dpaa2_mcp_tk(sc->cmd, sc->con_token));
106 	DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(sc->cmd, sc->rc_token));
107 	dpaa2_mcp_free_command(sc->cmd);
108 
109 	sc->cmd = NULL;
110 	sc->con_token = 0;
111 	sc->rc_token = 0;
112 
113 	return (0);
114 }
115 
116 static int
dpaa2_con_attach(device_t dev)117 dpaa2_con_attach(device_t dev)
118 {
119 	device_t pdev = device_get_parent(dev);
120 	device_t child = dev;
121 	device_t mcp_dev;
122 	struct dpaa2_con_softc *sc = device_get_softc(dev);
123 	struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
124 	struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
125 	struct dpaa2_devinfo *mcp_dinfo;
126 	int error;
127 
128 	sc->dev = dev;
129 
130 	error = bus_alloc_resources(sc->dev, dpaa2_con_spec, sc->res);
131 	if (error) {
132 		device_printf(dev, "%s: failed to allocate resources: "
133 		    "error=%d\n", __func__, error);
134 		return (ENXIO);
135 	}
136 
137 	/* Obtain MC portal. */
138 	mcp_dev = (device_t) rman_get_start(sc->res[MCP_RID(0)]);
139 	mcp_dinfo = device_get_ivars(mcp_dev);
140 	dinfo->portal = mcp_dinfo->portal;
141 
142 	/* Allocate a command to send to MC hardware. */
143 	error = dpaa2_mcp_init_command(&sc->cmd, DPAA2_CMD_DEF);
144 	if (error) {
145 		device_printf(dev, "Failed to allocate dpaa2_cmd: error=%d\n",
146 		    error);
147 		goto err_exit;
148 	}
149 
150 	/* Open resource container and DPCON object. */
151 	error = DPAA2_CMD_RC_OPEN(dev, child, sc->cmd, rcinfo->id,
152 	    &sc->rc_token);
153 	if (error) {
154 		device_printf(dev, "Failed to open DPRC: error=%d\n", error);
155 		goto err_free_cmd;
156 	}
157 	error = DPAA2_CMD_CON_OPEN(dev, child, sc->cmd, dinfo->id,
158 	    &sc->con_token);
159 	if (error) {
160 		device_printf(dev, "Failed to open DPCON: id=%d, error=%d\n",
161 		    dinfo->id, error);
162 		goto err_close_rc;
163 	}
164 
165 	/* Prepare DPCON object. */
166 	error = DPAA2_CMD_CON_RESET(dev, child, sc->cmd);
167 	if (error) {
168 		device_printf(dev, "Failed to reset DPCON: id=%d, error=%d\n",
169 		    dinfo->id, error);
170 		goto err_close_con;
171 	}
172 	error = DPAA2_CMD_CON_GET_ATTRIBUTES(dev, child, sc->cmd, &sc->attr);
173 	if (error) {
174 		device_printf(dev, "Failed to get DPCON attributes: id=%d, "
175 		    "error=%d\n", dinfo->id, error);
176 		goto err_close_con;
177 	}
178 
179 	/* TODO: Enable debug output via sysctl (to reduce output). */
180 	if (bootverbose)
181 		device_printf(dev, "chan_id=%d, priorities=%d\n",
182 		    sc->attr.chan_id, sc->attr.prior_num);
183 
184 	return (0);
185 
186  err_close_con:
187 	DPAA2_CMD_CON_CLOSE(dev, child, dpaa2_mcp_tk(sc->cmd, sc->con_token));
188  err_close_rc:
189 	DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(sc->cmd, sc->rc_token));
190  err_free_cmd:
191 	dpaa2_mcp_free_command(sc->cmd);
192  err_exit:
193 	return (ENXIO);
194 }
195 
196 static device_method_t dpaa2_con_methods[] = {
197 	/* Device interface */
198 	DEVMETHOD(device_probe,		dpaa2_con_probe),
199 	DEVMETHOD(device_attach,	dpaa2_con_attach),
200 	DEVMETHOD(device_detach,	dpaa2_con_detach),
201 
202 	DEVMETHOD_END
203 };
204 
205 static driver_t dpaa2_con_driver = {
206 	"dpaa2_con",
207 	dpaa2_con_methods,
208 	sizeof(struct dpaa2_con_softc),
209 };
210 
211 DRIVER_MODULE(dpaa2_con, dpaa2_rc, dpaa2_con_driver, 0, 0);
212