Skip to content

Commit

Permalink
dpaa2: Allocate dpaa2_cmd on stack for DPIO
Browse files Browse the repository at this point in the history
This is a first patch of a series to fix a bug #14.
  • Loading branch information
Dmitry Salychev authored and Dmitry Salychev committed Feb 15, 2023
1 parent 28137bd commit bc093d5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 64 deletions.
86 changes: 47 additions & 39 deletions sys/dev/dpaa2/dpaa2_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,35 +127,48 @@ dpaa2_io_probe(device_t dev)
static int
dpaa2_io_detach(device_t dev)
{
device_t pdev = device_get_parent(dev);
device_t child = dev;
struct dpaa2_io_softc *sc = device_get_softc(dev);
struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
struct dpaa2_cmd cmd;
uint16_t rc_token;
uint16_t io_token __unused;
int error;

/* Tear down interrupt handler and release IRQ resources. */
dpaa2_io_release_irqs(dev);

/* Free software portal helper object. */
dpaa2_swp_free_portal(sc->swp);
error = dpaa2_mcp_init_command(&cmd, DPAA2_CMD_DEF);
if (error) {
device_printf(dev, "%s: failed to initialize dpaa2_cmd: "
"error=%d\n", __func__, error);
goto unmap_res;
}

/* Disable DPIO object. */
error = DPAA2_CMD_IO_DISABLE(dev, child, dpaa2_mcp_tk(sc->cmd,
sc->io_token));
if (error && bootverbose)
/* Disable DPIO object */

error = DPAA2_CMD_RC_OPEN(dev, child, &cmd, rcinfo->id, &rc_token);
if (error) {
device_printf(dev, "%s: failed to open DPRC: error=%d\n",
__func__, error);
goto unmap_res;
}
error = DPAA2_CMD_IO_OPEN(dev, child, &cmd, dinfo->id, &io_token);
if (error) {
device_printf(dev, "%s: failed to open DPIO: id=%d, error=%d\n",
__func__, dinfo->id, error);
goto unmap_res;
}
error = DPAA2_CMD_IO_DISABLE(dev, child, &cmd);
if (error && bootverbose) {
device_printf(dev, "%s: failed to disable DPIO: id=%d, "
"error=%d\n", __func__, dinfo->id, error);
}
DPAA2_CMD_IO_CLOSE(dev, child, &cmd);
DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(&cmd, rc_token));

/* Close control sessions with the DPAA2 objects. */
DPAA2_CMD_IO_CLOSE(dev, child, dpaa2_mcp_tk(sc->cmd, sc->io_token));
DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(sc->cmd, sc->rc_token));

/* Free pre-allocated MC command. */
dpaa2_mcp_free_command(sc->cmd);
sc->cmd = NULL;
sc->io_token = 0;
sc->rc_token = 0;

/* Unmap memory resources of the portal. */
unmap_res:
for (int i = 0; i < MEM_RES_NUM; i++) {
if (sc->res[MEM_RID(i)] == NULL)
continue;
Expand All @@ -166,8 +179,6 @@ dpaa2_io_detach(device_t dev)
"resource: rid=%d, error=%d\n", __func__, MEM_RID(i),
error);
}

/* Release allocated resources. */
bus_release_resources(dev, dpaa2_io_spec, sc->res);

return (0);
Expand All @@ -183,6 +194,7 @@ dpaa2_io_attach(device_t dev)
struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
struct dpaa2_devinfo *mcp_dinfo;
struct dpaa2_cmd cmd;
struct resource_map_request req;
struct {
vm_memattr_t memattr;
Expand All @@ -192,32 +204,30 @@ dpaa2_io_attach(device_t dev)
{ VM_MEMATTR_DEVICE, "cache-inhibited part" },
{ VM_MEMATTR_DEVICE, "control registers" }
};
uint16_t rc_token;
uint16_t io_token __unused;
int error;

sc->dev = dev;
sc->swp = NULL;
sc->cmd = NULL;
sc->intr = NULL;
sc->irq_resource = NULL;

/* Allocate resources. */
error = bus_alloc_resources(sc->dev, dpaa2_io_spec, sc->res);
if (error) {
device_printf(dev, "%s: failed to allocate resources: "
"error=%d\n", __func__, error);
return (ENXIO);
}

/* Set allocated MC portal up. */
/* Setup allocated MC portal */
mcp_dev = (device_t) rman_get_start(sc->res[MCP_RID(0)]);
mcp_dinfo = device_get_ivars(mcp_dev);
dinfo->portal = mcp_dinfo->portal;

/* Map memory resources of the portal. */
for (int i = 0; i < MEM_RES_NUM; i++) {
if (sc->res[MEM_RID(i)] == NULL)
continue;

resource_init_map_request(&req);
req.memattr = map_args[i].memattr;
error = bus_map_resource(sc->dev, SYS_RES_MEMORY,
Expand All @@ -229,46 +239,47 @@ dpaa2_io_attach(device_t dev)
}
}

/* Allocate a command to send to the MC hardware. */
error = dpaa2_mcp_init_command(&sc->cmd, DPAA2_CMD_DEF);
error = dpaa2_mcp_init_command(&cmd, DPAA2_CMD_DEF);
if (error) {
device_printf(dev, "%s: failed to allocate dpaa2_cmd: "
device_printf(dev, "%s: failed to initialize dpaa2_cmd: "
"error=%d\n", __func__, error);
goto err_exit;
}

/* Prepare DPIO object. */
error = DPAA2_CMD_RC_OPEN(dev, child, sc->cmd, rcinfo->id,
&sc->rc_token);
/* Enable DPIO object */

error = DPAA2_CMD_RC_OPEN(dev, child, &cmd, rcinfo->id, &rc_token);
if (error) {
device_printf(dev, "%s: failed to open DPRC: error=%d\n",
__func__, error);
goto err_exit;
}
error = DPAA2_CMD_IO_OPEN(dev, child, sc->cmd, dinfo->id, &sc->io_token);
error = DPAA2_CMD_IO_OPEN(dev, child, &cmd, dinfo->id, &io_token);
if (error) {
device_printf(dev, "%s: failed to open DPIO: id=%d, error=%d\n",
__func__, dinfo->id, error);
goto err_exit;
}
error = DPAA2_CMD_IO_RESET(dev, child, sc->cmd);
error = DPAA2_CMD_IO_RESET(dev, child, &cmd);
if (error) {
device_printf(dev, "%s: failed to reset DPIO: id=%d, error=%d\n",
__func__, dinfo->id, error);
goto err_exit;
}
error = DPAA2_CMD_IO_GET_ATTRIBUTES(dev, child, sc->cmd, &sc->attr);
error = DPAA2_CMD_IO_GET_ATTRIBUTES(dev, child, &cmd, &sc->attr);
if (error) {
device_printf(dev, "%s: failed to get DPIO attributes: id=%d, "
"error=%d\n", __func__, dinfo->id, error);
goto err_exit;
}
error = DPAA2_CMD_IO_ENABLE(dev, child, sc->cmd);
error = DPAA2_CMD_IO_ENABLE(dev, child, &cmd);
if (error) {
device_printf(dev, "%s: failed to enable DPIO: id=%d, "
"error=%d\n", __func__, dinfo->id, error);
goto err_exit;
}
DPAA2_CMD_IO_CLOSE(dev, child, &cmd);
DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(&cmd, rc_token));

/* Prepare descriptor of the QBMan software portal. */
sc->swp_desc.dpio_dev = dev;
Expand All @@ -291,7 +302,6 @@ dpaa2_io_attach(device_t dev)
sc->swp_desc.swp_cycles_ratio = 256000 /
(sc->swp_desc.swp_clk / 1000000);

/* Initialize QBMan software portal. */
error = dpaa2_swp_init_portal(&sc->swp, &sc->swp_desc, DPAA2_SWP_DEF);
if (error) {
device_printf(dev, "%s: failed to initialize dpaa2_swp: "
Expand All @@ -306,16 +316,14 @@ dpaa2_io_attach(device_t dev)
goto err_exit;
}

#if 0
/* TODO: Enable debug output via sysctl (to reduce output). */
if (bootverbose)
device_printf(dev, "dpio_id=%d, swp_id=%d, chan_mode=%s, "
"notif_priors=%d, swp_version=0x%x\n",
sc->attr.id, sc->attr.swp_id,
sc->attr.chan_mode == DPAA2_IO_LOCAL_CHANNEL
? "local_channel" : "no_channel", sc->attr.priors_num,
sc->attr.swp_version);
#endif

return (0);

err_exit:
Expand Down
5 changes: 0 additions & 5 deletions sys/dev/dpaa2/dpaa2_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ struct dpaa2_io_softc {
struct dpaa2_swp *swp;
struct dpaa2_io_attr attr;

/* Help to send commands to MC. */
struct dpaa2_cmd *cmd;
uint16_t rc_token;
uint16_t io_token;

struct resource *res[DPAA2_IO_MAX_RESOURCES];
struct resource_map map[DPAA2_IO_MAX_RESOURCES];

Expand Down
22 changes: 4 additions & 18 deletions sys/dev/dpaa2/dpaa2_mcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,14 @@ dpaa2_mcp_free_portal(struct dpaa2_mcp *mcp)
}

int
dpaa2_mcp_init_command(struct dpaa2_cmd **cmd, uint16_t flags)
dpaa2_mcp_init_command(struct dpaa2_cmd *cmd)
{
const int mflags = flags & DPAA2_CMD_NOWAIT_ALLOC
? (M_NOWAIT | M_ZERO) : (M_WAITOK | M_ZERO);
struct dpaa2_cmd *c;
struct dpaa2_cmd_header *hdr;

if (!cmd)
return (DPAA2_CMD_STAT_EINVAL);

c = malloc(sizeof(struct dpaa2_cmd), M_DPAA2_MCP, mflags);
if (!c)
return (DPAA2_CMD_STAT_NO_MEMORY);

hdr = (struct dpaa2_cmd_header *) &c->header;
hdr = (struct dpaa2_cmd_header *) cmd->header;
hdr->srcid = 0;
hdr->status = DPAA2_CMD_STAT_OK;
hdr->token = 0;
Expand All @@ -134,20 +127,13 @@ dpaa2_mcp_init_command(struct dpaa2_cmd **cmd, uint16_t flags)
hdr->flags_hw |= DPAA2_HW_FLAG_HIGH_PRIO;
if (flags & DPAA2_CMD_INTR_DIS)
hdr->flags_sw |= DPAA2_SW_FLAG_INTR_DIS;

for (uint32_t i = 0; i < DPAA2_CMD_PARAMS_N; i++)
c->params[i] = 0;
*cmd = c;
cmd->params[i] = 0;

return (0);
}

void
dpaa2_mcp_free_command(struct dpaa2_cmd *cmd)
{
if (cmd != NULL)
free(cmd, M_DPAA2_MCP);
}

struct dpaa2_cmd *
dpaa2_mcp_tk(struct dpaa2_cmd *cmd, uint16_t token)
{
Expand Down
3 changes: 1 addition & 2 deletions sys/dev/dpaa2/dpaa2_mcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,8 @@ struct dpaa2_mcp_softc {

int dpaa2_mcp_init_portal(struct dpaa2_mcp **mcp, struct resource *res,
struct resource_map *map, uint16_t flags);
int dpaa2_mcp_init_command(struct dpaa2_cmd **cmd, uint16_t flags);
int dpaa2_mcp_init_command(struct dpaa2_cmd *cmd);
void dpaa2_mcp_free_portal(struct dpaa2_mcp *mcp);
void dpaa2_mcp_free_command(struct dpaa2_cmd *cmd);

/* to quickly update command token */
struct dpaa2_cmd *dpaa2_mcp_tk(struct dpaa2_cmd *cmd, uint16_t token);
Expand Down

0 comments on commit bc093d5

Please sign in to comment.