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