From nobody Wed Dec 10 22:53:53 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dRWH61j4tz6JqRv for ; Wed, 10 Dec 2025 22:53:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dRWH56NX1z3PXj for ; Wed, 10 Dec 2025 22:53:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765407233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zUjwliOOQbVl/R22tRpdVNbjJMK2RGVjz5XAJG/ZR/g=; b=oIqJAMOyNyZf6ICIKjMvNv5fbHi/PK6dNT0TLckQqD6ORtUyGLsTVWt9gb5pYAI3Nh4llO lHvCXoNtU5vPVMViCoSThkGNU7hjHSKJQkzY7BOcMRnFDEkil/jtyY3M1kcvsXTiaeu9dZ V1rJiz0DoL0C2x3OiwepE+LyvNTKoSZc8QDfo2El6oILerla/a9vsSUD9MPBgWLoIIfZw1 w11K37TQ4jkkoTBbgP2CilQnese3d59H+ebW7F8CAumVJxWGHAcMp0Z4gjRH6zJUbh8Is2 vfYiOyalGN/BB4nOCp+WncQ5Kx9KvmPRr4VUybn+DUB0avYWO7gbuMYU0xBLZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765407233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zUjwliOOQbVl/R22tRpdVNbjJMK2RGVjz5XAJG/ZR/g=; b=uMYuGuQ+40+vT3tNT54qhUjKEjiMo+R5zmdKnC2BF2+YH3394XlEw4aKjhxsKYvKgPiMxg 0LCX4IddeJRlAI0lRnKLbFEErNVsHa6YZXqOBR+39OX/+MnEr+7XfpIsIAxymtCWABzq0b nr2d9MyLnrmbcVFUayS0jZMoOLSKF/ocxFhWyey7yuic9PlW9g8Nj/tCOKXGU3d+T+gwpa OTVYpVLx7+J5zC2KJw3XuOKllpT2GD72Ar2EyEeG2t5wXnW2doalC0i9Fgg5M4IN6DR/bw +mdnSE7tg7JT3cONCudczQR5slaZYZToeHg/mLn72HGsNUxsy7anUlyXt1E68A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1765407233; a=rsa-sha256; cv=none; b=kkXizOiPGycMZGQFg5x8AFHg/Isqfb5geKstz1pGO7aD2XsdCfXbV12/zPLUM4C5n7KVm7 Y6aUvbhL2PxjoMJqtSgYl6yYUWJFyAPOU5zouvKUrg/ZWOBUyGtcAsqtY6DwDBYY/uS+5w W4i9wRcpUUm6k920JUX5kBhd5DpLwcxmkiC+cpcXO+u83TiTq8JeZXob0S/loYi0DA97a8 VOfI6DxaeFZb8hc0Pe8yo78sVSZMdMITvJEwpUlkR+fgsTvUNpi+B0ekyPEOY6/QhZNw6c ++2VhRDfRS8t/KNROwLPUDWWtoiuTej0Zg0Vmlq5RhSR6ME7WXZ+ImAdms1qsQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dRWH55SKfz14j0 for ; Wed, 10 Dec 2025 22:53:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id b117 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 10 Dec 2025 22:53:53 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: a21f12840f5f - main - nvme_sim: Connect to events broadcast with nvme_if List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a21f12840f5f25d777920957ed8ba7362052eaa7 Auto-Submitted: auto-generated Date: Wed, 10 Dec 2025 22:53:53 +0000 Message-Id: <6939fa01.b117.36a8aca3@gitrepo.freebsd.org> The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=a21f12840f5f25d777920957ed8ba7362052eaa7 commit a21f12840f5f25d777920957ed8ba7362052eaa7 Author: Warner Losh AuthorDate: 2025-12-10 22:52:33 +0000 Commit: Warner Losh CommitDate: 2025-12-10 22:52:33 +0000 nvme_sim: Connect to events broadcast with nvme_if Connect up the nvme_ns_* events. Copy code from old ways, as needed, and refactor a little. Sponsored by: Netflix Reviewed by: dab Differential Revision: https://reviews.freebsd.org/D51387 --- sys/dev/nvme/nvme_sim.c | 145 +++++++++++++++++++++++++++------------------- sys/modules/nvme/Makefile | 1 + 2 files changed, 85 insertions(+), 61 deletions(-) diff --git a/sys/dev/nvme/nvme_sim.c b/sys/dev/nvme/nvme_sim.c index 88d777f556ed..d134d1daafc3 100644 --- a/sys/dev/nvme/nvme_sim.c +++ b/sys/dev/nvme/nvme_sim.c @@ -44,10 +44,15 @@ #include "nvme_private.h" +#include "nvme_if.h" + #define ccb_accb_ptr spriv_ptr0 #define ccb_ctrlr_ptr spriv_ptr1 static void nvme_sim_action(struct cam_sim *sim, union ccb *ccb); static void nvme_sim_poll(struct cam_sim *sim); +static int nvme_sim_ns_added(device_t dev, struct nvme_namespace *ns); +static int nvme_sim_ns_changed(device_t dev, uint32_t nsid); +static int nvme_sim_ns_removed(device_t dev, struct nvme_namespace *ns); #define sim2softc(sim) ((struct nvme_sim_softc *)cam_sim_softc(sim)) #define sim2ctrlr(sim) (sim2softc(sim)->s_ctrlr) @@ -304,26 +309,6 @@ nvme_sim_poll(struct cam_sim *sim) nvme_ctrlr_poll(sim2ctrlr(sim)); } -static void -nvme_sim_ns_change(struct nvme_namespace *ns, struct nvme_sim_softc *sc) -{ - union ccb *ccb; - - /* - * We map the NVMe namespace idea onto the CAM unit LUN. For each new - * namespace, we create a new CAM path for it. We then rescan the path - * to get it to enumerate. - */ - ccb = xpt_alloc_ccb(); - if (xpt_create_path(&ccb->ccb_h.path, /*periph*/NULL, - cam_sim_path(sc->s_sim), 0, ns->id) != CAM_REQ_CMP) { - printf("unable to create path for rescan\n"); - xpt_free_ccb(ccb); - return; - } - xpt_rescan(ccb); -} - static int nvme_sim_probe(device_t dev) { @@ -339,7 +324,7 @@ nvme_sim_attach(device_t dev) { struct cam_devq *devq; int max_trans; - int err = ENOMEM; + int err = ENXIO; struct nvme_sim_softc *sc = device_get_softc(dev); struct nvme_controller *ctrlr = device_get_ivars(dev); @@ -356,16 +341,14 @@ nvme_sim_attach(device_t dev) if (sc->s_sim == NULL) { device_printf(dev, "Failed to allocate a sim\n"); cam_simq_free(devq); - goto err1; + return (ENOMEM); } if (xpt_bus_register(sc->s_sim, dev, 0) != CAM_SUCCESS) { - err = ENXIO; device_printf(dev, "Failed to create a bus\n"); goto err2; } if (xpt_create_path(&sc->s_path, /*periph*/NULL, cam_sim_path(sc->s_sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { - err = ENXIO; device_printf(dev, "Failed to create a path\n"); goto err3; } @@ -375,7 +358,7 @@ nvme_sim_attach(device_t dev) if (ns->data.nsze == 0) continue; - nvme_sim_ns_change(ns, sc); + nvme_sim_ns_added(dev, ns); } return (0); @@ -383,69 +366,109 @@ err3: xpt_bus_deregister(cam_sim_path(sc->s_sim)); err2: cam_sim_free(sc->s_sim, /*free_devq*/TRUE); -err1: - free(sc, M_NVME); return (err); } + static int -nvme_sim_fail(device_t dev, struct nvme_namespace *ns) +nvme_sim_fail_all_ns(device_t dev) { struct nvme_sim_softc *sc = device_get_softc(dev); - struct cam_path *tmppath; - union ccb *ccb; + struct nvme_controller *ctrlr = sc->s_ctrlr; - if (xpt_create_path(&tmppath, /*periph*/NULL, - cam_sim_path(sc->s_sim), 0, ns->id) != CAM_REQ_CMP) { - device_printf(dev, "unable to create path for rescan\n"); - return (ENOMEM); - } - /* - * If it's gone, then signal that and leave. - */ - if (ns->flags & NVME_NS_GONE) { - xpt_async(AC_LOST_DEVICE, tmppath, NULL); - xpt_free_path(tmppath); - return (0); - } + for (int i = 0; i < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); i++) { + struct nvme_namespace *ns = &ctrlr->ns[i]; - ccb = xpt_alloc_ccb_nowait(); - if (ccb == NULL) { - device_printf(dev, "unable to alloc CCB for rescan\n"); - return (ENOMEM); + if (ns->data.nsze == 0) + continue; + nvme_sim_ns_removed(dev, ns); } - ccb->ccb_h.path = tmppath; + return (0); +} + +static int +nvme_sim_detach(device_t dev) +{ + return (nvme_sim_fail_all_ns(dev)); +} + +static int +nvme_sim_ns_added(device_t dev, struct nvme_namespace *ns) +{ + struct nvme_sim_softc *sc = device_get_softc(dev); + union ccb *ccb; /* * We map the NVMe namespace idea onto the CAM unit LUN. For each new - * namespace, scan or rescan the path to enumerate it. tmppath freed at - * end of scan. + * namespace, scan or rescan the path to enumerate it. */ + ccb = xpt_alloc_ccb(); + if (xpt_create_path(&ccb->ccb_h.path, /*periph*/NULL, + cam_sim_path(sc->s_sim), 0, ns->id) != CAM_REQ_CMP) { + printf("unable to create path for rescan\n"); + return (ENOMEM); + } xpt_rescan(ccb); return (0); } static int -nvme_sim_detach(device_t dev) +nvme_sim_ns_removed(device_t dev, struct nvme_namespace *ns) { - struct nvme_controller *ctrlr = device_get_ivars(dev); - - for (int i = 0; i < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); i++) { - struct nvme_namespace *ns = &ctrlr->ns[i]; + struct nvme_sim_softc *sc = device_get_softc(dev); + struct cam_path *tmppath; - if (ns->data.nsze == 0) - continue; - nvme_sim_fail(dev, ns); + if (xpt_create_path(&tmppath, /*periph*/NULL, + cam_sim_path(sc->s_sim), 0, ns->id) != CAM_REQ_CMP) { + printf("unable to create path for rescan\n"); + return (ENOMEM); } + xpt_async(AC_LOST_DEVICE, tmppath, NULL); + xpt_free_path(tmppath); + + return (0); +} + +static int +nvme_sim_ns_changed(device_t dev, uint32_t nsid) +{ + struct nvme_sim_softc *sc = device_get_softc(dev); + struct nvme_namespace *ns = &sc->s_ctrlr->ns[nsid - 1]; + + /* + * These wind up being the same. For a configured cam_ed, we generate + * AC_GETDEV_CHANGED, but for new one we do AC_FOUND_DEVICE, but the + * scan is the same. + */ + return (nvme_sim_ns_added(dev, ns)); +} + +static int +nvme_sim_controller_failed(device_t dev) +{ + return (nvme_sim_fail_all_ns(dev)); +} + +static int +nvme_sim_handle_aen(device_t dev, const struct nvme_completion *cpl, + uint32_t pg_nr, void *page, uint32_t page_len) +{ + /* Do nothing */ return (0); } static device_method_t nvme_sim_methods[] = { /* Device interface */ - DEVMETHOD(device_probe, nvme_sim_probe), - DEVMETHOD(device_attach, nvme_sim_attach), - DEVMETHOD(device_detach, nvme_sim_detach), + DEVMETHOD(device_probe, nvme_sim_probe), + DEVMETHOD(device_attach, nvme_sim_attach), + DEVMETHOD(device_detach, nvme_sim_detach), + /* Nvme controller messages */ + DEVMETHOD(nvme_ns_added, nvme_sim_ns_added), + DEVMETHOD(nvme_ns_removed, nvme_sim_ns_removed), + DEVMETHOD(nvme_ns_changed, nvme_sim_ns_changed), + DEVMETHOD(nvme_controller_failed, nvme_sim_controller_failed), + DEVMETHOD(nvme_handle_aen, nvme_sim_handle_aen), { 0, 0 } }; diff --git a/sys/modules/nvme/Makefile b/sys/modules/nvme/Makefile index 936a28fa5de9..b10f5dd190bf 100644 --- a/sys/modules/nvme/Makefile +++ b/sys/modules/nvme/Makefile @@ -16,6 +16,7 @@ SRCS = nvme.c \ \ bus_if.h \ device_if.h \ + nvme_if.h \ opt_cam.h \ opt_nvme.h \ pci_if.h