xref: /freebsd/sys/dev/dpaa2/dpaa2_con.c (revision 535af610)
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 	/* TBD */
105 	return (0);
106 }
107 
108 static int
109 dpaa2_con_attach(device_t dev)
110 {
111 	device_t pdev = device_get_parent(dev);
112 	device_t child = dev;
113 	device_t mcp_dev;
114 	struct dpaa2_con_softc *sc = device_get_softc(dev);
115 	struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
116 	struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
117 	struct dpaa2_devinfo *mcp_dinfo;
118 	struct dpaa2_cmd cmd;
119 	uint16_t rc_token, con_token;
120 	int error;
121 
122 	sc->dev = dev;
123 
124 	error = bus_alloc_resources(sc->dev, dpaa2_con_spec, sc->res);
125 	if (error) {
126 		device_printf(dev, "%s: failed to allocate resources: "
127 		    "error=%d\n", __func__, error);
128 		goto err_exit;
129 	}
130 
131 	/* Obtain MC portal. */
132 	mcp_dev = (device_t) rman_get_start(sc->res[MCP_RID(0)]);
133 	mcp_dinfo = device_get_ivars(mcp_dev);
134 	dinfo->portal = mcp_dinfo->portal;
135 
136 	DPAA2_CMD_INIT(&cmd);
137 
138 	error = DPAA2_CMD_RC_OPEN(dev, child, &cmd, rcinfo->id, &rc_token);
139 	if (error) {
140 		device_printf(dev, "%s: failed to open DPRC: error=%d\n",
141 		    __func__, error);
142 		goto err_exit;
143 	}
144 	error = DPAA2_CMD_CON_OPEN(dev, child, &cmd, dinfo->id, &con_token);
145 	if (error) {
146 		device_printf(dev, "%s: failed to open DPCON: id=%d, error=%d\n",
147 		    __func__, dinfo->id, error);
148 		goto close_rc;
149 	}
150 
151 	error = DPAA2_CMD_CON_RESET(dev, child, &cmd);
152 	if (error) {
153 		device_printf(dev, "%s: failed to reset DPCON: id=%d, "
154 		    "error=%d\n", __func__, dinfo->id, error);
155 		goto close_con;
156 	}
157 	error = DPAA2_CMD_CON_GET_ATTRIBUTES(dev, child, &cmd, &sc->attr);
158 	if (error) {
159 		device_printf(dev, "%s: failed to get DPCON attributes: id=%d, "
160 		    "error=%d\n", __func__, dinfo->id, error);
161 		goto close_con;
162 	}
163 
164 	if (bootverbose) {
165 		device_printf(dev, "chan_id=%d, priorities=%d\n",
166 		    sc->attr.chan_id, sc->attr.prior_num);
167 	}
168 
169 	(void)DPAA2_CMD_CON_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, con_token));
170 	(void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
171 	return (0);
172 
173 close_con:
174 	DPAA2_CMD_CON_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, con_token));
175 close_rc:
176 	DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
177 err_exit:
178 	return (ENXIO);
179 }
180 
181 static device_method_t dpaa2_con_methods[] = {
182 	/* Device interface */
183 	DEVMETHOD(device_probe,		dpaa2_con_probe),
184 	DEVMETHOD(device_attach,	dpaa2_con_attach),
185 	DEVMETHOD(device_detach,	dpaa2_con_detach),
186 
187 	DEVMETHOD_END
188 };
189 
190 static driver_t dpaa2_con_driver = {
191 	"dpaa2_con",
192 	dpaa2_con_methods,
193 	sizeof(struct dpaa2_con_softc),
194 };
195 
196 DRIVER_MODULE(dpaa2_con, dpaa2_rc, dpaa2_con_driver, 0, 0);
197