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