From 4cbd23e28bb0e2598d3c88f42d533870b21d98d5 Mon Sep 17 00:00:00 2001 From: Konrad Sztyber Date: Tue, 5 Jul 2022 15:33:35 +0200 Subject: [PATCH] vmd: method for forcing a rescan Added a new RPC, vmd_rescan, that forces the VMD driver to do a rescan of all devices behind the VMD. A device that was previously removed via spdk_vmd_remove_device() will be found again during vmd_rescan. Signed-off-by: Konrad Sztyber Change-Id: Ide87eb44c1d6d524234820dc07c78ba5b8bcd3ad Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/13958 Reviewed-by: Jim Harris Reviewed-by: Tom Nabarro Reviewed-by: Tomasz Zawadzki Tested-by: SPDK CI Jenkins --- doc/jsonrpc.md | 34 +++++++++++++++++++++++++++ include/spdk/vmd.h | 8 +++++++ lib/vmd/spdk_vmd.map | 1 + lib/vmd/vmd.c | 16 +++++++++++++ module/event/subsystems/vmd/vmd_rpc.c | 25 ++++++++++++++++++++ python/spdk/rpc/vmd.py | 5 ++++ scripts/rpc.py | 6 +++++ 7 files changed, 95 insertions(+) diff --git a/doc/jsonrpc.md b/doc/jsonrpc.md index ed61f203b..f416d32f9 100644 --- a/doc/jsonrpc.md +++ b/doc/jsonrpc.md @@ -10030,6 +10030,40 @@ Example response: } ~~~ +### vmd_rescan {#rpc_vmd_rescan} + +Force a rescan of the devices behind VMD. + +### Parameters + +This method has no parameters. + +#### Response + +The response is the number of new devices found. + +### Example + +~~~json +{ + "jsonrpc": "2.0", + "method": "vmd_rescan", + "id": 1 +} +~~~ + +Example response: + +~~~json +{ + "jsonrpc": "2.0", + "id": 1, + "result": { + "count": 1 + } +} +~~~ + ### spdk_get_version {#rpc_spdk_get_version} Get the version info of the running SPDK application. diff --git a/include/spdk/vmd.h b/include/spdk/vmd.h index ae073b14d..629fb4d8b 100644 --- a/include/spdk/vmd.h +++ b/include/spdk/vmd.h @@ -90,6 +90,14 @@ int spdk_vmd_hotplug_monitor(void); */ int spdk_vmd_remove_device(const struct spdk_pci_addr *addr); +/** + * Forces a rescan of the devices behind the VMD. If a device was previously removed through + * spdk_vmd_remove_device() this will cause it to be reattached. + * + * \return number of new devices found during scanning or negative errno on failure. + */ +int spdk_vmd_rescan(void); + #ifdef __cplusplus } #endif diff --git a/lib/vmd/spdk_vmd.map b/lib/vmd/spdk_vmd.map index 75eebc31e..5049e02e4 100644 --- a/lib/vmd/spdk_vmd.map +++ b/lib/vmd/spdk_vmd.map @@ -9,6 +9,7 @@ spdk_vmd_get_led_state; spdk_vmd_hotplug_monitor; spdk_vmd_remove_device; + spdk_vmd_rescan; local: *; }; diff --git a/lib/vmd/vmd.c b/lib/vmd/vmd.c index ee8c04a7d..92595f993 100644 --- a/lib/vmd/vmd.c +++ b/lib/vmd/vmd.c @@ -1435,6 +1435,22 @@ spdk_vmd_remove_device(const struct spdk_pci_addr *addr) return 0; } +int +spdk_vmd_rescan(void) +{ + struct vmd_pci_bus *bus; + uint32_t i; + int rc = 0; + + for (i = 0; i < g_vmd_container.count; ++i) { + TAILQ_FOREACH(bus, &g_vmd_container.vmd[i].bus_list, tailq) { + rc += vmd_scan_single_bus(bus, bus->self, true); + } + } + + return rc; +} + static int vmd_attach_device(const struct spdk_pci_addr *addr) { diff --git a/module/event/subsystems/vmd/vmd_rpc.c b/module/event/subsystems/vmd/vmd_rpc.c index 57a7903fe..844561eac 100644 --- a/module/event/subsystems/vmd/vmd_rpc.c +++ b/module/event/subsystems/vmd/vmd_rpc.c @@ -69,3 +69,28 @@ out: free(req.addr); } SPDK_RPC_REGISTER("vmd_remove_device", rpc_vmd_remove_device, SPDK_RPC_RUNTIME) + +static void +rpc_vmd_rescan(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) +{ + struct spdk_json_write_ctx *w; + int rc; + + if (!vmd_subsystem_is_enabled()) { + spdk_jsonrpc_send_error_response(request, -EPERM, "VMD subsystem is disabled"); + return; + } + + rc = spdk_vmd_rescan(); + if (rc < 0) { + spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); + return; + } + + w = spdk_jsonrpc_begin_result(request); + spdk_json_write_object_begin(w); + spdk_json_write_named_uint32(w, "count", (uint32_t)rc); + spdk_json_write_object_end(w); + spdk_jsonrpc_end_result(request, w); +} +SPDK_RPC_REGISTER("vmd_rescan", rpc_vmd_rescan, SPDK_RPC_RUNTIME) diff --git a/python/spdk/rpc/vmd.py b/python/spdk/rpc/vmd.py index 3b3452de2..b4ceb79c1 100644 --- a/python/spdk/rpc/vmd.py +++ b/python/spdk/rpc/vmd.py @@ -10,3 +10,8 @@ def vmd_enable(client): def vmd_remove_device(client, addr): """Remove a device behind VMD""" return client.call('vmd_remove_device', {'addr': addr}) + + +def vmd_rescan(client): + """Force a rescan of the devices behind VMD""" + return client.call('vmd_rescan') diff --git a/scripts/rpc.py b/scripts/rpc.py index 31c7092d5..98ecc91c3 100755 --- a/scripts/rpc.py +++ b/scripts/rpc.py @@ -2080,6 +2080,12 @@ Format: 'user:u1 secret:s1 muser:mu1 msecret:ms1,user:u2 secret:s2 muser:mu2 mse p.add_argument('addr', help='Address of the device to remove', type=str) p.set_defaults(func=vmd_remove_device) + def vmd_rescan(args): + print_dict(rpc.vmd.vmd_rescan(args.client)) + + p = subparsers.add_parser('vmd_rescan', help='Force a rescan of the devices behind VMD') + p.set_defaults(func=vmd_rescan) + # nbd def nbd_start_disk(args): print(rpc.nbd.nbd_start_disk(args.client,