Skip to content

Commit

Permalink
resource: ubridge: unify reserved state names and log message when ch…
Browse files Browse the repository at this point in the history
…anging reserved state
  • Loading branch information
prajnoha committed Jan 11, 2024
1 parent 6d70f5f commit 70e76f4
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions src/resource/ubridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ static const char * const dev_ready_str[] = {
};

static const char * const dev_reserved_str[] = {
[DEV_RES_UNDEFINED] = "undefined",
[DEV_RES_UNPROCESSED] = "unprocessed",
[DEV_RES_RESERVED] = "reserved",
[DEV_RES_USED] = "used",
[DEV_RES_FREE] = "free",
[DEV_RES_UNDEFINED] = "RES_UNDEFINED",
[DEV_RES_UNPROCESSED] = "RES_UNPROCESSED",
[DEV_RES_RESERVED] = "RES_RESERVED",
[DEV_RES_USED] = "RES_USED",
[DEV_RES_FREE] = "RES_FREE",
};

struct sid_ucmd_ctx {
Expand Down Expand Up @@ -2786,23 +2786,29 @@ dev_ready_t sid_ucmd_dev_get_ready(sid_resource_t *mod_res, struct sid_ucmd_ctx
return _do_sid_ucmd_dev_get_ready(mod_res, ucmd_ctx, archive);
}

static int _do_sid_ucmd_dev_set_reserved(sid_resource_t *mod_res, struct sid_ucmd_ctx *ucmd_ctx, dev_reserved_t reserved)
static int _do_sid_ucmd_dev_set_reserved(sid_resource_t *res, struct sid_ucmd_ctx *ucmd_ctx, dev_reserved_t reserved)
{
dev_reserved_t old_reserved;
dev_reserved_t old_reserved = ucmd_ctx->scan.dev_reserved;
int r;

if (!(_cmd_scan_phase_regs[ucmd_ctx->scan.phase].flags & CMD_SCAN_CAP_RES))
return -EPERM;
r = -EPERM;

if ((old_reserved = ucmd_ctx->scan.dev_reserved) == reserved)
return 0;
if (reserved == old_reserved) {
r = 0;
goto out;
}

switch (reserved) {
case DEV_RDY_UNDEFINED:
return -EBADRQC;
r = -EBADRQC;
goto out;

case DEV_RES_UNPROCESSED:
if (old_reserved != DEV_RES_UNDEFINED)
return -EBADRQC;
if (old_reserved != DEV_RES_UNDEFINED) {
r = -EBADRQC;
goto out;
}
break;

case DEV_RES_RESERVED:
Expand All @@ -2815,18 +2821,35 @@ static int _do_sid_ucmd_dev_set_reserved(sid_resource_t *mod_res, struct sid_ucm
break;
}

if (!_do_sid_ucmd_set_kv(mod_res,
if (!_do_sid_ucmd_set_kv(sid_resource_match(res, &sid_resource_type_ubridge_command, NULL) ? NULL : res,
ucmd_ctx,
NULL,
KV_NS_DEVICE,
KV_KEY_DEV_RESERVED,
KV_SYNC | KV_AR | KV_RD | KV_SUB_WR | KV_SUP_WR,
&reserved,
sizeof(reserved)))
return -1;
r = -1;
else
r = 0;
out:
if (r < 0) {
sid_resource_log_error_errno(res,
r,
"Reserved state change failed for device " CMD_DEV_PRINT_FMT ": %s --> %s.",
CMD_DEV_PRINT(ucmd_ctx),
dev_reserved_str[old_reserved],
dev_reserved_str[reserved]);
} else {
ucmd_ctx->scan.dev_reserved = reserved;
sid_resource_log_debug(res,
"Reserved state changed for device " CMD_DEV_PRINT_FMT ": %s --> %s.",
CMD_DEV_PRINT(ucmd_ctx),
dev_reserved_str[old_reserved],
dev_reserved_str[reserved]);
}

ucmd_ctx->scan.dev_reserved = reserved;
return 0;
return r;
}

int sid_ucmd_dev_set_reserved(sid_resource_t *mod_res, struct sid_ucmd_ctx *ucmd_ctx, dev_reserved_t reserved)
Expand Down Expand Up @@ -4334,7 +4357,7 @@ static int _set_device_kv_records(sid_resource_t *cmd_res)
_do_sid_ucmd_dev_set_ready(cmd_res, ucmd_ctx, DEV_RDY_UNPROCESSED);

if (_do_sid_ucmd_dev_get_reserved(NULL, ucmd_ctx, 0) == DEV_RES_UNDEFINED)
_do_sid_ucmd_dev_set_reserved(NULL, ucmd_ctx, DEV_RES_UNPROCESSED);
_do_sid_ucmd_dev_set_reserved(cmd_res, ucmd_ctx, DEV_RES_UNPROCESSED);

return _refresh_device_hierarchy_from_sysfs(cmd_res);
}
Expand Down Expand Up @@ -4490,7 +4513,7 @@ static int _cmd_exec_scan_exit(sid_resource_t *cmd_res)
return _do_sid_ucmd_dev_set_ready(cmd_res, ucmd_ctx, DEV_RDY_PUBLIC);

if (_do_sid_ucmd_dev_get_reserved(NULL, ucmd_ctx, 0) == DEV_RES_UNPROCESSED)
return _do_sid_ucmd_dev_set_reserved(NULL, ucmd_ctx, DEV_RES_FREE);
return _do_sid_ucmd_dev_set_reserved(cmd_res, ucmd_ctx, DEV_RES_FREE);

return 0;
}
Expand Down

0 comments on commit 70e76f4

Please sign in to comment.