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