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 Buffer Pool (DPBP) driver.
31ba7319e9SDmitry Salychev *
32ba7319e9SDmitry Salychev * The DPBP configures a buffer pool that can be associated with DPAA2 network
33ba7319e9SDmitry Salychev * and accelerator interfaces.
34ba7319e9SDmitry Salychev */
35ba7319e9SDmitry Salychev
36ba7319e9SDmitry Salychev #include <sys/param.h>
37ba7319e9SDmitry Salychev #include <sys/kernel.h>
38ba7319e9SDmitry Salychev #include <sys/bus.h>
39ba7319e9SDmitry Salychev #include <sys/rman.h>
40ba7319e9SDmitry Salychev #include <sys/module.h>
41ba7319e9SDmitry Salychev #include <sys/malloc.h>
42ba7319e9SDmitry Salychev #include <sys/mutex.h>
43ba7319e9SDmitry Salychev
44ba7319e9SDmitry Salychev #include <vm/vm.h>
45ba7319e9SDmitry Salychev
46ba7319e9SDmitry Salychev #include <machine/bus.h>
47ba7319e9SDmitry Salychev #include <machine/resource.h>
48ba7319e9SDmitry Salychev
49ba7319e9SDmitry Salychev #include <dev/pci/pcivar.h>
50ba7319e9SDmitry Salychev
51ba7319e9SDmitry Salychev #include "pcib_if.h"
52ba7319e9SDmitry Salychev #include "pci_if.h"
53ba7319e9SDmitry Salychev
54ba7319e9SDmitry Salychev #include "dpaa2_mc.h"
55ba7319e9SDmitry Salychev #include "dpaa2_mcp.h"
56ba7319e9SDmitry Salychev #include "dpaa2_swp.h"
57ba7319e9SDmitry Salychev #include "dpaa2_swp_if.h"
58ba7319e9SDmitry Salychev #include "dpaa2_cmd_if.h"
59ba7319e9SDmitry Salychev
60ba7319e9SDmitry Salychev /* DPAA2 Buffer Pool resource specification. */
61ba7319e9SDmitry Salychev struct resource_spec dpaa2_bp_spec[] = {
62ba7319e9SDmitry Salychev /*
63ba7319e9SDmitry Salychev * DPMCP resources.
64ba7319e9SDmitry Salychev *
65ba7319e9SDmitry Salychev * NOTE: MC command portals (MCPs) are used to send commands to, and
66ba7319e9SDmitry Salychev * receive responses from, the MC firmware. One portal per DPBP.
67ba7319e9SDmitry Salychev */
68ba7319e9SDmitry Salychev #define MCP_RES_NUM (1u)
69ba7319e9SDmitry Salychev #define MCP_RID_OFF (0u)
70ba7319e9SDmitry Salychev #define MCP_RID(rid) ((rid) + MCP_RID_OFF)
71ba7319e9SDmitry Salychev /* --- */
72ba7319e9SDmitry Salychev { DPAA2_DEV_MCP, MCP_RID(0), RF_ACTIVE | RF_SHAREABLE | RF_OPTIONAL },
73ba7319e9SDmitry Salychev /* --- */
74ba7319e9SDmitry Salychev RESOURCE_SPEC_END
75ba7319e9SDmitry Salychev };
76ba7319e9SDmitry Salychev
77ba7319e9SDmitry Salychev static int
dpaa2_bp_probe(device_t dev)78ba7319e9SDmitry Salychev dpaa2_bp_probe(device_t dev)
79ba7319e9SDmitry Salychev {
80ba7319e9SDmitry Salychev /* DPBP device will be added by the parent resource container. */
81ba7319e9SDmitry Salychev device_set_desc(dev, "DPAA2 Buffer Pool");
82ba7319e9SDmitry Salychev return (BUS_PROBE_DEFAULT);
83ba7319e9SDmitry Salychev }
84ba7319e9SDmitry Salychev
85ba7319e9SDmitry Salychev static int
dpaa2_bp_detach(device_t dev)86ba7319e9SDmitry Salychev dpaa2_bp_detach(device_t dev)
87ba7319e9SDmitry Salychev {
88*4cd96614SDmitry Salychev device_t pdev = device_get_parent(dev);
89ba7319e9SDmitry Salychev device_t child = dev;
90ba7319e9SDmitry Salychev struct dpaa2_bp_softc *sc = device_get_softc(dev);
91*4cd96614SDmitry Salychev struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
92ba7319e9SDmitry Salychev struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
93*4cd96614SDmitry Salychev struct dpaa2_cmd cmd;
94*4cd96614SDmitry Salychev uint16_t rc_token, bp_token;
95*4cd96614SDmitry Salychev int error;
96ba7319e9SDmitry Salychev
97*4cd96614SDmitry Salychev DPAA2_CMD_INIT(&cmd);
98ba7319e9SDmitry Salychev
99*4cd96614SDmitry Salychev error = DPAA2_CMD_RC_OPEN(dev, child, &cmd, rcinfo->id, &rc_token);
100*4cd96614SDmitry Salychev if (error) {
101*4cd96614SDmitry Salychev device_printf(dev, "%s: failed to open DPRC: error=%d\n",
102*4cd96614SDmitry Salychev __func__, error);
103*4cd96614SDmitry Salychev goto err_exit;
104ba7319e9SDmitry Salychev }
105*4cd96614SDmitry Salychev error = DPAA2_CMD_BP_OPEN(dev, child, &cmd, dinfo->id, &bp_token);
106*4cd96614SDmitry Salychev if (error) {
107*4cd96614SDmitry Salychev device_printf(dev, "%s: failed to open DPBP: id=%d, error=%d\n",
108*4cd96614SDmitry Salychev __func__, dinfo->id, error);
109*4cd96614SDmitry Salychev goto close_rc;
110*4cd96614SDmitry Salychev }
111*4cd96614SDmitry Salychev (void)DPAA2_CMD_BP_DISABLE(dev, child, &cmd);
112*4cd96614SDmitry Salychev (void)DPAA2_CMD_BP_CLOSE(dev, child, &cmd);
113*4cd96614SDmitry Salychev (void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
114ba7319e9SDmitry Salychev
115ba7319e9SDmitry Salychev dinfo->portal = NULL;
116ba7319e9SDmitry Salychev bus_release_resources(sc->dev, dpaa2_bp_spec, sc->res);
117ba7319e9SDmitry Salychev
118ba7319e9SDmitry Salychev return (0);
119*4cd96614SDmitry Salychev
120*4cd96614SDmitry Salychev close_rc:
121*4cd96614SDmitry Salychev (void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
122*4cd96614SDmitry Salychev err_exit:
123*4cd96614SDmitry Salychev return (ENXIO);
124ba7319e9SDmitry Salychev }
125ba7319e9SDmitry Salychev
126ba7319e9SDmitry Salychev static int
dpaa2_bp_attach(device_t dev)127ba7319e9SDmitry Salychev dpaa2_bp_attach(device_t dev)
128ba7319e9SDmitry Salychev {
129ba7319e9SDmitry Salychev device_t pdev = device_get_parent(dev);
130ba7319e9SDmitry Salychev device_t child = dev;
131ba7319e9SDmitry Salychev device_t mcp_dev;
132ba7319e9SDmitry Salychev struct dpaa2_bp_softc *sc = device_get_softc(dev);
133ba7319e9SDmitry Salychev struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
134ba7319e9SDmitry Salychev struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
135ba7319e9SDmitry Salychev struct dpaa2_devinfo *mcp_dinfo;
136*4cd96614SDmitry Salychev struct dpaa2_cmd cmd;
137*4cd96614SDmitry Salychev uint16_t rc_token, bp_token;
138ba7319e9SDmitry Salychev int error;
139ba7319e9SDmitry Salychev
140ba7319e9SDmitry Salychev sc->dev = dev;
141ba7319e9SDmitry Salychev
142ba7319e9SDmitry Salychev error = bus_alloc_resources(sc->dev, dpaa2_bp_spec, sc->res);
143ba7319e9SDmitry Salychev if (error) {
144ba7319e9SDmitry Salychev device_printf(dev, "%s: failed to allocate resources: "
145ba7319e9SDmitry Salychev "error=%d\n", __func__, error);
146*4cd96614SDmitry Salychev goto err_exit;
147ba7319e9SDmitry Salychev }
148ba7319e9SDmitry Salychev
149ba7319e9SDmitry Salychev /* Send commands to MC via allocated portal. */
150ba7319e9SDmitry Salychev mcp_dev = (device_t) rman_get_start(sc->res[MCP_RID(0)]);
151ba7319e9SDmitry Salychev mcp_dinfo = device_get_ivars(mcp_dev);
152ba7319e9SDmitry Salychev dinfo->portal = mcp_dinfo->portal;
153ba7319e9SDmitry Salychev
154*4cd96614SDmitry Salychev DPAA2_CMD_INIT(&cmd);
155ba7319e9SDmitry Salychev
156*4cd96614SDmitry Salychev error = DPAA2_CMD_RC_OPEN(dev, child, &cmd, rcinfo->id, &rc_token);
157ba7319e9SDmitry Salychev if (error) {
158ba7319e9SDmitry Salychev device_printf(dev, "%s: failed to open DPRC: error=%d\n",
159ba7319e9SDmitry Salychev __func__, error);
160*4cd96614SDmitry Salychev goto detach;
161ba7319e9SDmitry Salychev }
162*4cd96614SDmitry Salychev error = DPAA2_CMD_BP_OPEN(dev, child, &cmd, dinfo->id, &bp_token);
163ba7319e9SDmitry Salychev if (error) {
164ba7319e9SDmitry Salychev device_printf(dev, "%s: failed to open DPBP: id=%d, error=%d\n",
165ba7319e9SDmitry Salychev __func__, dinfo->id, error);
166*4cd96614SDmitry Salychev goto close_rc;
167ba7319e9SDmitry Salychev }
168ba7319e9SDmitry Salychev
169*4cd96614SDmitry Salychev error = DPAA2_CMD_BP_RESET(dev, child, &cmd);
170ba7319e9SDmitry Salychev if (error) {
171ba7319e9SDmitry Salychev device_printf(dev, "%s: failed to reset DPBP: id=%d, error=%d\n",
172ba7319e9SDmitry Salychev __func__, dinfo->id, error);
173*4cd96614SDmitry Salychev goto close_bp;
174ba7319e9SDmitry Salychev }
175*4cd96614SDmitry Salychev error = DPAA2_CMD_BP_ENABLE(dev, child, &cmd);
176ba7319e9SDmitry Salychev if (error) {
177ba7319e9SDmitry Salychev device_printf(dev, "%s: failed to enable DPBP: id=%d, "
178ba7319e9SDmitry Salychev "error=%d\n", __func__, dinfo->id, error);
179*4cd96614SDmitry Salychev goto close_bp;
180ba7319e9SDmitry Salychev }
181*4cd96614SDmitry Salychev error = DPAA2_CMD_BP_GET_ATTRIBUTES(dev, child, &cmd, &sc->attr);
182ba7319e9SDmitry Salychev if (error) {
183ba7319e9SDmitry Salychev device_printf(dev, "%s: failed to get DPBP attributes: id=%d, "
184ba7319e9SDmitry Salychev "error=%d\n", __func__, dinfo->id, error);
185*4cd96614SDmitry Salychev goto close_bp;
186ba7319e9SDmitry Salychev }
187ba7319e9SDmitry Salychev
188*4cd96614SDmitry Salychev (void)DPAA2_CMD_BP_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, bp_token));
189*4cd96614SDmitry Salychev (void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
190ba7319e9SDmitry Salychev return (0);
191*4cd96614SDmitry Salychev
192*4cd96614SDmitry Salychev close_bp:
193*4cd96614SDmitry Salychev (void)DPAA2_CMD_BP_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, bp_token));
194*4cd96614SDmitry Salychev close_rc:
195*4cd96614SDmitry Salychev (void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
196*4cd96614SDmitry Salychev detach:
197*4cd96614SDmitry Salychev dpaa2_bp_detach(dev);
198*4cd96614SDmitry Salychev err_exit:
199*4cd96614SDmitry Salychev return (ENXIO);
200ba7319e9SDmitry Salychev }
201ba7319e9SDmitry Salychev
202ba7319e9SDmitry Salychev static device_method_t dpaa2_bp_methods[] = {
203ba7319e9SDmitry Salychev /* Device interface */
204ba7319e9SDmitry Salychev DEVMETHOD(device_probe, dpaa2_bp_probe),
205ba7319e9SDmitry Salychev DEVMETHOD(device_attach, dpaa2_bp_attach),
206ba7319e9SDmitry Salychev DEVMETHOD(device_detach, dpaa2_bp_detach),
207ba7319e9SDmitry Salychev
208ba7319e9SDmitry Salychev DEVMETHOD_END
209ba7319e9SDmitry Salychev };
210ba7319e9SDmitry Salychev
211ba7319e9SDmitry Salychev static driver_t dpaa2_bp_driver = {
212ba7319e9SDmitry Salychev "dpaa2_bp",
213ba7319e9SDmitry Salychev dpaa2_bp_methods,
214ba7319e9SDmitry Salychev sizeof(struct dpaa2_bp_softc),
215ba7319e9SDmitry Salychev };
216ba7319e9SDmitry Salychev
217ba7319e9SDmitry Salychev DRIVER_MODULE(dpaa2_bp, dpaa2_rc, dpaa2_bp_driver, 0, 0);
218