1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2020 Intel Corporation. 3 * Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. 4 * All rights reserved. 5 */ 6 7 #include "accel_internal.h" 8 9 #include "spdk/rpc.h" 10 #include "spdk/util.h" 11 #include "spdk/event.h" 12 #include "spdk/stdinc.h" 13 #include "spdk/env.h" 14 15 static void 16 rpc_accel_get_opc_assignments(struct spdk_jsonrpc_request *request, 17 const struct spdk_json_val *params) 18 { 19 struct spdk_json_write_ctx *w; 20 enum accel_opcode opcode; 21 const char *name, *module_name = NULL; 22 int rc; 23 24 if (params != NULL) { 25 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 26 "accel_get_opc_assignments requires no parameters"); 27 return; 28 } 29 30 w = spdk_jsonrpc_begin_result(request); 31 32 spdk_json_write_object_begin(w); 33 for (opcode = 0; opcode < ACCEL_OPC_LAST; opcode++) { 34 rc = _accel_get_opc_name(opcode, &name); 35 if (rc == 0) { 36 rc = spdk_accel_get_opc_module_name(opcode, &module_name); 37 if (rc != 0) { 38 /* This isn't fatal but throw an informational message if we 39 * cant get an module name right now */ 40 SPDK_NOTICELOG("FYI error (%d) getting module name.\n", rc); 41 } 42 spdk_json_write_named_string(w, name, module_name); 43 } else { 44 /* this should never happen */ 45 SPDK_ERRLOG("Invalid opcode (%d)).\n", opcode); 46 assert(0); 47 } 48 } 49 spdk_json_write_object_end(w); 50 51 spdk_jsonrpc_end_result(request, w); 52 } 53 SPDK_RPC_REGISTER("accel_get_opc_assignments", rpc_accel_get_opc_assignments, 54 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 55 56 static void 57 rpc_dump_module_info(struct module_info *info) 58 { 59 struct spdk_json_write_ctx *w = info->w; 60 const char *name; 61 uint32_t i; 62 int rc; 63 64 spdk_json_write_object_begin(w); 65 66 spdk_json_write_named_string(w, "module", info->name); 67 spdk_json_write_named_array_begin(w, "supported ops"); 68 69 for (i = 0; i < info->num_ops; i++) { 70 rc = _accel_get_opc_name(i, &name); 71 if (rc == 0) { 72 spdk_json_write_string(w, name); 73 } else { 74 /* this should never happen */ 75 SPDK_ERRLOG("Invalid opcode (%d)).\n", i); 76 assert(0); 77 } 78 } 79 80 spdk_json_write_array_end(w); 81 spdk_json_write_object_end(w); 82 } 83 84 static void 85 rpc_accel_get_module_info(struct spdk_jsonrpc_request *request, 86 const struct spdk_json_val *params) 87 { 88 struct module_info info; 89 90 if (params != NULL) { 91 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 92 "accel_get_module_info requires no parameters"); 93 return; 94 } 95 96 info.w = spdk_jsonrpc_begin_result(request); 97 spdk_json_write_array_begin(info.w); 98 99 _accel_for_each_module(&info, rpc_dump_module_info); 100 101 spdk_json_write_array_end(info.w); 102 spdk_jsonrpc_end_result(request, info.w); 103 } 104 SPDK_RPC_REGISTER("accel_get_module_info", rpc_accel_get_module_info, 105 SPDK_RPC_RUNTIME) 106 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(accel_get_module_info, accel_get_engine_info) 107 108 struct rpc_accel_assign_opc { 109 char *opname; 110 char *module; 111 }; 112 113 static const struct spdk_json_object_decoder rpc_accel_assign_opc_decoders[] = { 114 {"opname", offsetof(struct rpc_accel_assign_opc, opname), spdk_json_decode_string}, 115 {"module", offsetof(struct rpc_accel_assign_opc, module), spdk_json_decode_string}, 116 }; 117 118 static void 119 free_accel_assign_opc(struct rpc_accel_assign_opc *r) 120 { 121 free(r->opname); 122 free(r->module); 123 } 124 125 static void 126 rpc_accel_assign_opc(struct spdk_jsonrpc_request *request, 127 const struct spdk_json_val *params) 128 { 129 struct rpc_accel_assign_opc req = {}; 130 const char *opcode_str; 131 enum accel_opcode opcode; 132 bool found = false; 133 int rc; 134 135 if (spdk_json_decode_object(params, rpc_accel_assign_opc_decoders, 136 SPDK_COUNTOF(rpc_accel_assign_opc_decoders), 137 &req)) { 138 SPDK_DEBUGLOG(accel, "spdk_json_decode_object failed\n"); 139 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR, 140 "spdk_json_decode_object failed"); 141 goto cleanup; 142 } 143 144 for (opcode = 0; opcode < ACCEL_OPC_LAST; opcode++) { 145 rc = _accel_get_opc_name(opcode, &opcode_str); 146 assert(!rc); 147 if (strcmp(opcode_str, req.opname) == 0) { 148 found = true; 149 break; 150 } 151 } 152 153 if (found == false) { 154 SPDK_DEBUGLOG(accel, "Invalid operation name\n"); 155 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 156 "spdk_json_decode_object failed"); 157 goto cleanup; 158 } 159 160 rc = spdk_accel_assign_opc(opcode, req.module); 161 if (rc) { 162 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 163 "error assigning opcode"); 164 goto cleanup; 165 } 166 167 SPDK_NOTICELOG("Operation %s will be assigned to module %s\n", req.opname, req.module); 168 spdk_jsonrpc_send_bool_response(request, true); 169 170 cleanup: 171 free_accel_assign_opc(&req); 172 173 } 174 SPDK_RPC_REGISTER("accel_assign_opc", rpc_accel_assign_opc, SPDK_RPC_STARTUP) 175