xref: /freebsd/sys/dev/nvme/nvme_sim.c (revision f08746a7e3195a6e144e6f58003dc5c221d15d02)
13a31c31cSWarner Losh /*-
252467047SWarner Losh  * Copyright (c) 2016 Netflix, Inc.
33a31c31cSWarner Losh  *
43a31c31cSWarner Losh  * Redistribution and use in source and binary forms, with or without
53a31c31cSWarner Losh  * modification, are permitted provided that the following conditions
63a31c31cSWarner Losh  * are met:
73a31c31cSWarner Losh  * 1. Redistributions of source code must retain the above copyright
83a31c31cSWarner Losh  *    notice, this list of conditions and the following disclaimer,
93a31c31cSWarner Losh  *    without modification, immediately at the beginning of the file.
103a31c31cSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
113a31c31cSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
123a31c31cSWarner Losh  *    documentation and/or other materials provided with the distribution.
133a31c31cSWarner Losh  *
143a31c31cSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153a31c31cSWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163a31c31cSWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
173a31c31cSWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
183a31c31cSWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
193a31c31cSWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203a31c31cSWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213a31c31cSWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223a31c31cSWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
233a31c31cSWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
243a31c31cSWarner Losh  */
253a31c31cSWarner Losh 
263a31c31cSWarner Losh #include <sys/param.h>
273a31c31cSWarner Losh #include <sys/systm.h>
283a31c31cSWarner Losh #include <sys/buf.h>
293a31c31cSWarner Losh #include <sys/bus.h>
303a31c31cSWarner Losh #include <sys/conf.h>
313a31c31cSWarner Losh #include <sys/ioccom.h>
323a31c31cSWarner Losh #include <sys/malloc.h>
333a31c31cSWarner Losh #include <sys/proc.h>
343a31c31cSWarner Losh #include <sys/smp.h>
353a31c31cSWarner Losh 
363a31c31cSWarner Losh #include <cam/cam.h>
373a31c31cSWarner Losh #include <cam/cam_ccb.h>
383a31c31cSWarner Losh #include <cam/cam_sim.h>
393a31c31cSWarner Losh #include <cam/cam_xpt_sim.h>
403a31c31cSWarner Losh #include <cam/cam_debug.h>
413a31c31cSWarner Losh 
424e3b2744SWarner Losh #include <dev/pci/pcivar.h>
434e3b2744SWarner Losh #include <dev/pci/pcireg.h>
444e3b2744SWarner Losh 
453a31c31cSWarner Losh #include "nvme_private.h"
463a31c31cSWarner Losh 
473a31c31cSWarner Losh #define ccb_accb_ptr spriv_ptr0
483a31c31cSWarner Losh #define ccb_ctrlr_ptr spriv_ptr1
493a31c31cSWarner Losh static void	nvme_sim_action(struct cam_sim *sim, union ccb *ccb);
503a31c31cSWarner Losh static void	nvme_sim_poll(struct cam_sim *sim);
513a31c31cSWarner Losh 
523a31c31cSWarner Losh #define sim2softc(sim)	((struct nvme_sim_softc *)cam_sim_softc(sim))
533a31c31cSWarner Losh #define sim2ctrlr(sim)	(sim2softc(sim)->s_ctrlr)
543a31c31cSWarner Losh 
553a31c31cSWarner Losh struct nvme_sim_softc
563a31c31cSWarner Losh {
573a31c31cSWarner Losh 	struct nvme_controller	*s_ctrlr;
583a31c31cSWarner Losh 	struct cam_sim		*s_sim;
593a31c31cSWarner Losh 	struct cam_path		*s_path;
603a31c31cSWarner Losh };
613a31c31cSWarner Losh 
623a31c31cSWarner Losh static void
nvme_sim_nvmeio_done(void * ccb_arg,const struct nvme_completion * cpl)633a31c31cSWarner Losh nvme_sim_nvmeio_done(void *ccb_arg, const struct nvme_completion *cpl)
643a31c31cSWarner Losh {
653a31c31cSWarner Losh 	union ccb *ccb = (union ccb *)ccb_arg;
663a31c31cSWarner Losh 
673a31c31cSWarner Losh 	/*
683a31c31cSWarner Losh 	 * Let the periph know the completion, and let it sort out what
6963b0c00eSWarner Losh 	 * it means. Report an error or success based on SC and SCT.
7063b0c00eSWarner Losh 	 * We do not try to fetch additional data from the error log,
7163b0c00eSWarner Losh 	 * though maybe we should in the future.
723a31c31cSWarner Losh 	 */
733a31c31cSWarner Losh 	memcpy(&ccb->nvmeio.cpl, cpl, sizeof(*cpl));
747e5f6f25SWarner Losh 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
75cfb43eb1SWarner Losh 	if (nvme_completion_is_error(cpl)) {
76774ab87cSWarner Losh 		ccb->ccb_h.status = CAM_NVME_STATUS_ERROR;
773a31c31cSWarner Losh 		xpt_done(ccb);
78cfb43eb1SWarner Losh 	} else {
79cfb43eb1SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
80cfb43eb1SWarner Losh 		xpt_done_direct(ccb);
81cfb43eb1SWarner Losh 	}
823a31c31cSWarner Losh }
833a31c31cSWarner Losh 
843a31c31cSWarner Losh static void
nvme_sim_nvmeio(struct cam_sim * sim,union ccb * ccb)853a31c31cSWarner Losh nvme_sim_nvmeio(struct cam_sim *sim, union ccb *ccb)
863a31c31cSWarner Losh {
873a31c31cSWarner Losh 	struct ccb_nvmeio	*nvmeio = &ccb->nvmeio;
883a31c31cSWarner Losh 	struct nvme_request	*req;
893a31c31cSWarner Losh 	void			*payload;
903a31c31cSWarner Losh 	uint32_t		size;
913a31c31cSWarner Losh 	struct nvme_controller *ctrlr;
923a31c31cSWarner Losh 
933a31c31cSWarner Losh 	ctrlr = sim2ctrlr(sim);
943a31c31cSWarner Losh 	payload = nvmeio->data_ptr;
953a31c31cSWarner Losh 	size = nvmeio->dxfer_len;
963a31c31cSWarner Losh 	/* SG LIST ??? */
973a31c31cSWarner Losh 	if ((nvmeio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
983a31c31cSWarner Losh 		req = nvme_allocate_request_bio((struct bio *)payload,
99*f08746a7SMark Johnston 		    M_NOWAIT, nvme_sim_nvmeio_done, ccb);
10051977281SWarner Losh 	else if ((nvmeio->ccb_h.flags & CAM_DATA_SG) == CAM_DATA_SG)
101*f08746a7SMark Johnston 		req = nvme_allocate_request_ccb(ccb, M_NOWAIT,
1023a31c31cSWarner Losh 		    nvme_sim_nvmeio_done, ccb);
103*f08746a7SMark Johnston 	else if (payload == NULL)
104*f08746a7SMark Johnston 		req = nvme_allocate_request_null(M_NOWAIT, nvme_sim_nvmeio_done,
105*f08746a7SMark Johnston 		    ccb);
106*f08746a7SMark Johnston 	else
107*f08746a7SMark Johnston 		req = nvme_allocate_request_vaddr(payload, size, M_NOWAIT,
108*f08746a7SMark Johnston 		    nvme_sim_nvmeio_done, ccb);
1093a31c31cSWarner Losh 	if (req == NULL) {
1103a31c31cSWarner Losh 		nvmeio->ccb_h.status = CAM_RESRC_UNAVAIL;
1113a31c31cSWarner Losh 		xpt_done(ccb);
1123a31c31cSWarner Losh 		return;
1133a31c31cSWarner Losh 	}
1147e5f6f25SWarner Losh 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1153a31c31cSWarner Losh 
1163a31c31cSWarner Losh 	memcpy(&req->cmd, &ccb->nvmeio.cmd, sizeof(ccb->nvmeio.cmd));
1173a31c31cSWarner Losh 
118df424515SWarner Losh 	if (ccb->ccb_h.func_code == XPT_NVME_IO)
1193a31c31cSWarner Losh 		nvme_ctrlr_submit_io_request(ctrlr, req);
120df424515SWarner Losh 	else
121df424515SWarner Losh 		nvme_ctrlr_submit_admin_request(ctrlr, req);
1223a31c31cSWarner Losh }
1233a31c31cSWarner Losh 
1244e3b2744SWarner Losh static uint32_t
nvme_link_kBps(struct nvme_controller * ctrlr)1254e3b2744SWarner Losh nvme_link_kBps(struct nvme_controller *ctrlr)
1264e3b2744SWarner Losh {
1274e3b2744SWarner Losh 	uint32_t speed, lanes, link[] = { 1, 250000, 500000, 985000, 1970000 };
128eab9d0a8SWarner Losh 	uint32_t status;
1294e3b2744SWarner Losh 
130eab9d0a8SWarner Losh 	status = pcie_read_config(ctrlr->dev, PCIER_LINK_STA, 2);
131eab9d0a8SWarner Losh 	speed = status & PCIEM_LINK_STA_SPEED;
132eab9d0a8SWarner Losh 	lanes = (status & PCIEM_LINK_STA_WIDTH) >> 4;
1334e3b2744SWarner Losh 	/*
1344e3b2744SWarner Losh 	 * Failsafe on link speed indicator. If it is insane report the number of
1354e3b2744SWarner Losh 	 * lanes as the speed. Not 100% accurate, but may be diagnostic.
1364e3b2744SWarner Losh 	 */
1374e3b2744SWarner Losh 	if (speed >= nitems(link))
1384e3b2744SWarner Losh 		speed = 0;
1394e3b2744SWarner Losh 	return link[speed] * lanes;
1404e3b2744SWarner Losh }
1414e3b2744SWarner Losh 
1423a31c31cSWarner Losh static void
nvme_sim_action(struct cam_sim * sim,union ccb * ccb)1433a31c31cSWarner Losh nvme_sim_action(struct cam_sim *sim, union ccb *ccb)
1443a31c31cSWarner Losh {
1453a31c31cSWarner Losh 	struct nvme_controller *ctrlr;
1463a31c31cSWarner Losh 
1473a31c31cSWarner Losh 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
1483a31c31cSWarner Losh 	    ("nvme_sim_action: func= %#x\n",
1493a31c31cSWarner Losh 		ccb->ccb_h.func_code));
1503a31c31cSWarner Losh 
1513a31c31cSWarner Losh 	ctrlr = sim2ctrlr(sim);
1523a31c31cSWarner Losh 
1533a31c31cSWarner Losh 	switch (ccb->ccb_h.func_code) {
1543a31c31cSWarner Losh 	case XPT_CALC_GEOMETRY:		/* Calculate Geometry Totally nuts ? XXX */
1553a31c31cSWarner Losh 		/*
1563a31c31cSWarner Losh 		 * Only meaningful for old-school SCSI disks since only the SCSI
1573a31c31cSWarner Losh 		 * da driver generates them. Reject all these that slip through.
1583a31c31cSWarner Losh 		 */
1593a31c31cSWarner Losh 		/*FALLTHROUGH*/
1603a31c31cSWarner Losh 	case XPT_ABORT:			/* Abort the specified CCB */
1613a31c31cSWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
1623a31c31cSWarner Losh 		break;
1633a31c31cSWarner Losh 	case XPT_SET_TRAN_SETTINGS:
1643a31c31cSWarner Losh 		/*
1653a31c31cSWarner Losh 		 * NVMe doesn't really have different transfer settings, but
1663a31c31cSWarner Losh 		 * other parts of CAM think failure here is a big deal.
1673a31c31cSWarner Losh 		 */
1683a31c31cSWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
1693a31c31cSWarner Losh 		break;
1703a31c31cSWarner Losh 	case XPT_PATH_INQ:		/* Path routing inquiry */
1713a31c31cSWarner Losh 	{
1723a31c31cSWarner Losh 		struct ccb_pathinq	*cpi = &ccb->cpi;
1734484c8f5SWarner Losh 		device_t		dev = ctrlr->dev;
1743a31c31cSWarner Losh 
1758f079322SWarner Losh 		/*
1768f079322SWarner Losh 		 * For devices that are reported as children of the AHCI
1778f079322SWarner Losh 		 * controller, which has no access to the config space for this
1788f079322SWarner Losh 		 * controller, report the AHCI controller's data.
1798f079322SWarner Losh 		 */
1808f079322SWarner Losh 		if (ctrlr->quirks & QUIRK_AHCI)
1818f079322SWarner Losh 			dev = device_get_parent(dev);
1823a31c31cSWarner Losh 		cpi->version_num = 1;
1833a31c31cSWarner Losh 		cpi->hba_inquiry = 0;
1843a31c31cSWarner Losh 		cpi->target_sprt = 0;
185f439e3a4SAlexander Motin 		cpi->hba_misc =  PIM_UNMAPPED | PIM_NOSCAN;
1863a31c31cSWarner Losh 		cpi->hba_eng_cnt = 0;
1873a31c31cSWarner Losh 		cpi->max_target = 0;
1883a31c31cSWarner Losh 		cpi->max_lun = ctrlr->cdata.nn;
189f439e3a4SAlexander Motin 		cpi->maxio = ctrlr->max_xfer_size;
1903a31c31cSWarner Losh 		cpi->initiator_id = 0;
1913a31c31cSWarner Losh 		cpi->bus_id = cam_sim_bus(sim);
1924e3b2744SWarner Losh 		cpi->base_transfer_speed = nvme_link_kBps(ctrlr);
1934195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1944195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "NVMe", HBA_IDLEN);
1954195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1963a31c31cSWarner Losh 		cpi->unit_number = cam_sim_unit(sim);
1973a31c31cSWarner Losh 		cpi->transport = XPORT_NVME;		/* XXX XPORT_PCIE ? */
1984e3b2744SWarner Losh 		cpi->transport_version = nvme_mmio_read_4(ctrlr, vs);
1993a31c31cSWarner Losh 		cpi->protocol = PROTO_NVME;
2004e3b2744SWarner Losh 		cpi->protocol_version = nvme_mmio_read_4(ctrlr, vs);
201f439e3a4SAlexander Motin 		cpi->xport_specific.nvme.nsid = xpt_path_lun_id(ccb->ccb_h.path);
2024484c8f5SWarner Losh 		cpi->xport_specific.nvme.domain = pci_get_domain(dev);
2034484c8f5SWarner Losh 		cpi->xport_specific.nvme.bus = pci_get_bus(dev);
2044484c8f5SWarner Losh 		cpi->xport_specific.nvme.slot = pci_get_slot(dev);
2054484c8f5SWarner Losh 		cpi->xport_specific.nvme.function = pci_get_function(dev);
2064484c8f5SWarner Losh 		cpi->xport_specific.nvme.extra = 0;
20701fc4883SJohn Baldwin 		strlcpy(cpi->xport_specific.nvme.dev_name, device_get_nameunit(dev),
208027d0612SWarner Losh 		    sizeof(cpi->xport_specific.nvme.dev_name));
20997dc595dSAlexander Motin 		cpi->hba_vendor = pci_get_vendor(dev);
21097dc595dSAlexander Motin 		cpi->hba_device = pci_get_device(dev);
21197dc595dSAlexander Motin 		cpi->hba_subvendor = pci_get_subvendor(dev);
21297dc595dSAlexander Motin 		cpi->hba_subdevice = pci_get_subdevice(dev);
2133a31c31cSWarner Losh 		cpi->ccb_h.status = CAM_REQ_CMP;
2143a31c31cSWarner Losh 		break;
2153a31c31cSWarner Losh 	}
2163a31c31cSWarner Losh 	case XPT_GET_TRAN_SETTINGS:	/* Get transport settings */
2173a31c31cSWarner Losh 	{
2183a31c31cSWarner Losh 		struct ccb_trans_settings	*cts;
2193a31c31cSWarner Losh 		struct ccb_trans_settings_nvme	*nvmep;
2203a31c31cSWarner Losh 		struct ccb_trans_settings_nvme	*nvmex;
2214e3b2744SWarner Losh 		device_t dev;
222b1f14710SChuck Tuffli 		uint32_t status, caps, flags;
2233a31c31cSWarner Losh 
2244e3b2744SWarner Losh 		dev = ctrlr->dev;
2253a31c31cSWarner Losh 		cts = &ccb->cts;
2263a31c31cSWarner Losh 		nvmex = &cts->xport_specific.nvme;
2273a31c31cSWarner Losh 		nvmep = &cts->proto_specific.nvme;
2283a31c31cSWarner Losh 
2298f079322SWarner Losh 		nvmex->spec = nvme_mmio_read_4(ctrlr, vs);
2308f079322SWarner Losh 		nvmex->valid = CTS_NVME_VALID_SPEC;
2318f079322SWarner Losh 		if ((ctrlr->quirks & QUIRK_AHCI) == 0) {
2328f079322SWarner Losh 			/* AHCI redirect makes it impossible to query */
233eab9d0a8SWarner Losh 			status = pcie_read_config(dev, PCIER_LINK_STA, 2);
234eab9d0a8SWarner Losh 			caps = pcie_read_config(dev, PCIER_LINK_CAP, 2);
235b1f14710SChuck Tuffli 			flags = pcie_read_config(dev, PCIER_FLAGS, 2);
236b1f14710SChuck Tuffli 			if ((flags & PCIEM_FLAGS_TYPE) == PCIEM_TYPE_ENDPOINT) {
237b1f14710SChuck Tuffli 				nvmex->valid |= CTS_NVME_VALID_LINK;
238eab9d0a8SWarner Losh 				nvmex->speed = status & PCIEM_LINK_STA_SPEED;
239eab9d0a8SWarner Losh 				nvmex->lanes = (status & PCIEM_LINK_STA_WIDTH) >> 4;
240eab9d0a8SWarner Losh 				nvmex->max_speed = caps & PCIEM_LINK_CAP_MAX_SPEED;
241eab9d0a8SWarner Losh 				nvmex->max_lanes = (caps & PCIEM_LINK_CAP_MAX_WIDTH) >> 4;
242b1f14710SChuck Tuffli 			}
2438f079322SWarner Losh 		}
2443a31c31cSWarner Losh 
2454e3b2744SWarner Losh 		/* XXX these should be something else maybe ? */
2469c2203a6SJohn Baldwin 		nvmep->valid = CTS_NVME_VALID_SPEC;
2474e3b2744SWarner Losh 		nvmep->spec = nvmex->spec;
2484e3b2744SWarner Losh 
2493a31c31cSWarner Losh 		cts->transport = XPORT_NVME;
2509c2203a6SJohn Baldwin 		cts->transport_version = nvmex->spec;
2513a31c31cSWarner Losh 		cts->protocol = PROTO_NVME;
2529c2203a6SJohn Baldwin 		cts->protocol_version = nvmex->spec;
2533a31c31cSWarner Losh 		cts->ccb_h.status = CAM_REQ_CMP;
2543a31c31cSWarner Losh 		break;
2553a31c31cSWarner Losh 	}
2563a31c31cSWarner Losh 	case XPT_TERM_IO:		/* Terminate the I/O process */
2573a31c31cSWarner Losh 		/*
2583a31c31cSWarner Losh 		 * every driver handles this, but nothing generates it. Assume
2593a31c31cSWarner Losh 		 * it's OK to just say 'that worked'.
2603a31c31cSWarner Losh 		 */
2613a31c31cSWarner Losh 		/*FALLTHROUGH*/
2623a31c31cSWarner Losh 	case XPT_RESET_DEV:		/* Bus Device Reset the specified device */
2633a31c31cSWarner Losh 	case XPT_RESET_BUS:		/* Reset the specified bus */
2643a31c31cSWarner Losh 		/*
2653a31c31cSWarner Losh 		 * NVMe doesn't really support physically resetting the bus. It's part
2663a31c31cSWarner Losh 		 * of the bus scanning dance, so return sucess to tell the process to
2673a31c31cSWarner Losh 		 * proceed.
2683a31c31cSWarner Losh 		 */
2693a31c31cSWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2703a31c31cSWarner Losh 		break;
2713a31c31cSWarner Losh 	case XPT_NVME_IO:		/* Execute the requested I/O operation */
2724b977e6dSWarner Losh 		if (ctrlr->is_failed) {
273fe52c338SWarner Losh 			/*
274fe52c338SWarner Losh 			 * I/O came in while we were failing the drive, so drop
275fe52c338SWarner Losh 			 * it. Once falure is complete, we'll be destroyed.
276fe52c338SWarner Losh 			 */
2774b977e6dSWarner Losh 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2784b977e6dSWarner Losh 			break;
2794b977e6dSWarner Losh 		}
2803a31c31cSWarner Losh 		nvme_sim_nvmeio(sim, ccb);
2813a31c31cSWarner Losh 		return;			/* no done */
2823d89acf5SWarner Losh 	case XPT_NVME_ADMIN:		/* or Admin operation */
2833d89acf5SWarner Losh 		if (ctrlr->is_failed_admin) {
2843d89acf5SWarner Losh 			/*
2853d89acf5SWarner Losh 			 * Admin request came in when we can't send admin
2863d89acf5SWarner Losh 			 * commands, so drop it. Once falure is complete, we'll
2873d89acf5SWarner Losh 			 * be destroyed.
2883d89acf5SWarner Losh 			 */
2893d89acf5SWarner Losh 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2903d89acf5SWarner Losh 			break;
2913d89acf5SWarner Losh 		}
2923d89acf5SWarner Losh 		nvme_sim_nvmeio(sim, ccb);
2933d89acf5SWarner Losh 		return;			/* no done */
2943a31c31cSWarner Losh 	default:
2953a31c31cSWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
2963a31c31cSWarner Losh 		break;
2973a31c31cSWarner Losh 	}
2983a31c31cSWarner Losh 	xpt_done(ccb);
2993a31c31cSWarner Losh }
3003a31c31cSWarner Losh 
3013a31c31cSWarner Losh static void
nvme_sim_poll(struct cam_sim * sim)3023a31c31cSWarner Losh nvme_sim_poll(struct cam_sim *sim)
3033a31c31cSWarner Losh {
3043a31c31cSWarner Losh 
30529431e54SWarner Losh 	nvme_ctrlr_poll(sim2ctrlr(sim));
3063a31c31cSWarner Losh }
3073a31c31cSWarner Losh 
3083a31c31cSWarner Losh static void *
nvme_sim_new_controller(struct nvme_controller * ctrlr)3093a31c31cSWarner Losh nvme_sim_new_controller(struct nvme_controller *ctrlr)
3103a31c31cSWarner Losh {
311f439e3a4SAlexander Motin 	struct nvme_sim_softc *sc;
3123a31c31cSWarner Losh 	struct cam_devq *devq;
3133a31c31cSWarner Losh 	int max_trans;
3143a31c31cSWarner Losh 
315c02565f9SWarner Losh 	max_trans = ctrlr->max_hw_pend_io;
3163a31c31cSWarner Losh 	devq = cam_simq_alloc(max_trans);
3173a31c31cSWarner Losh 	if (devq == NULL)
318f439e3a4SAlexander Motin 		return (NULL);
3193a31c31cSWarner Losh 
3203a31c31cSWarner Losh 	sc = malloc(sizeof(*sc), M_NVME, M_ZERO | M_WAITOK);
3213a31c31cSWarner Losh 	sc->s_ctrlr = ctrlr;
3223a31c31cSWarner Losh 
3233a31c31cSWarner Losh 	sc->s_sim = cam_sim_alloc(nvme_sim_action, nvme_sim_poll,
324f439e3a4SAlexander Motin 	    "nvme", sc, device_get_unit(ctrlr->dev),
325511662d0SAlexander Motin 	    NULL, max_trans, max_trans, devq);
3263a31c31cSWarner Losh 	if (sc->s_sim == NULL) {
3273a31c31cSWarner Losh 		printf("Failed to allocate a sim\n");
3283a31c31cSWarner Losh 		cam_simq_free(devq);
329f439e3a4SAlexander Motin 		goto err1;
330f439e3a4SAlexander Motin 	}
331f439e3a4SAlexander Motin 	if (xpt_bus_register(sc->s_sim, ctrlr->dev, 0) != CAM_SUCCESS) {
332f439e3a4SAlexander Motin 		printf("Failed to create a bus\n");
333f439e3a4SAlexander Motin 		goto err2;
334f439e3a4SAlexander Motin 	}
335f439e3a4SAlexander Motin 	if (xpt_create_path(&sc->s_path, /*periph*/NULL, cam_sim_path(sc->s_sim),
336f439e3a4SAlexander Motin 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
337f439e3a4SAlexander Motin 		printf("Failed to create a path\n");
338f439e3a4SAlexander Motin 		goto err3;
339f439e3a4SAlexander Motin 	}
340f439e3a4SAlexander Motin 
341f439e3a4SAlexander Motin 	return (sc);
342f439e3a4SAlexander Motin 
343f439e3a4SAlexander Motin err3:
344f439e3a4SAlexander Motin 	xpt_bus_deregister(cam_sim_path(sc->s_sim));
345f439e3a4SAlexander Motin err2:
346f439e3a4SAlexander Motin 	cam_sim_free(sc->s_sim, /*free_devq*/TRUE);
347f439e3a4SAlexander Motin err1:
3483a31c31cSWarner Losh 	free(sc, M_NVME);
349f439e3a4SAlexander Motin 	return (NULL);
3503a31c31cSWarner Losh }
3513a31c31cSWarner Losh 
3523a31c31cSWarner Losh static void *
nvme_sim_ns_change(struct nvme_namespace * ns,void * sc_arg)353950475caSWarner Losh nvme_sim_ns_change(struct nvme_namespace *ns, void *sc_arg)
3543a31c31cSWarner Losh {
3553a31c31cSWarner Losh 	struct nvme_sim_softc *sc = sc_arg;
356f439e3a4SAlexander Motin 	union ccb *ccb;
3573a31c31cSWarner Losh 
358f439e3a4SAlexander Motin 	ccb = xpt_alloc_ccb_nowait();
359f439e3a4SAlexander Motin 	if (ccb == NULL) {
360f439e3a4SAlexander Motin 		printf("unable to alloc CCB for rescan\n");
361f439e3a4SAlexander Motin 		return (NULL);
3623a31c31cSWarner Losh 	}
363f439e3a4SAlexander Motin 
3649cde7894SWarner Losh 	/*
3659cde7894SWarner Losh 	 * We map the NVMe namespace idea onto the CAM unit LUN. For
3669cde7894SWarner Losh 	 * each new namespace, we create a new CAM path for it. We then
3679cde7894SWarner Losh 	 * rescan the path to get it to enumerate.
3689cde7894SWarner Losh 	 */
369f439e3a4SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, /*periph*/NULL,
370f439e3a4SAlexander Motin 	    cam_sim_path(sc->s_sim), 0, ns->id) != CAM_REQ_CMP) {
371f439e3a4SAlexander Motin 		printf("unable to create path for rescan\n");
372f439e3a4SAlexander Motin 		xpt_free_ccb(ccb);
373f439e3a4SAlexander Motin 		return (NULL);
374f439e3a4SAlexander Motin 	}
375f439e3a4SAlexander Motin 	xpt_rescan(ccb);
376f439e3a4SAlexander Motin 
377950475caSWarner Losh 	return (sc_arg);
3783a31c31cSWarner Losh }
3793a31c31cSWarner Losh 
3803a31c31cSWarner Losh static void
nvme_sim_controller_fail(void * ctrlr_arg)3813a31c31cSWarner Losh nvme_sim_controller_fail(void *ctrlr_arg)
3823a31c31cSWarner Losh {
383f439e3a4SAlexander Motin 	struct nvme_sim_softc *sc = ctrlr_arg;
384f439e3a4SAlexander Motin 
385f439e3a4SAlexander Motin 	xpt_async(AC_LOST_DEVICE, sc->s_path, NULL);
386f439e3a4SAlexander Motin 	xpt_free_path(sc->s_path);
387f439e3a4SAlexander Motin 	xpt_bus_deregister(cam_sim_path(sc->s_sim));
388f439e3a4SAlexander Motin 	cam_sim_free(sc->s_sim, /*free_devq*/TRUE);
389f439e3a4SAlexander Motin 	free(sc, M_NVME);
3903a31c31cSWarner Losh }
3913a31c31cSWarner Losh 
3923a31c31cSWarner Losh struct nvme_consumer *consumer_cookie;
3933a31c31cSWarner Losh 
3943a31c31cSWarner Losh static void
nvme_sim_init(void)3953a31c31cSWarner Losh nvme_sim_init(void)
3963a31c31cSWarner Losh {
3978a5d94f9SWarner Losh 	if (nvme_use_nvd)
3988a5d94f9SWarner Losh 		return;
3993a31c31cSWarner Losh 
400950475caSWarner Losh 	consumer_cookie = nvme_register_consumer(nvme_sim_ns_change,
4013a31c31cSWarner Losh 	    nvme_sim_new_controller, NULL, nvme_sim_controller_fail);
4023a31c31cSWarner Losh }
4033a31c31cSWarner Losh 
4043a31c31cSWarner Losh SYSINIT(nvme_sim_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
4053a31c31cSWarner Losh     nvme_sim_init, NULL);
4063a31c31cSWarner Losh 
4073a31c31cSWarner Losh static void
nvme_sim_uninit(void)4083a31c31cSWarner Losh nvme_sim_uninit(void)
4093a31c31cSWarner Losh {
4108a5d94f9SWarner Losh 	if (nvme_use_nvd)
4118a5d94f9SWarner Losh 		return;
4123a31c31cSWarner Losh 	/* XXX Cleanup */
4133a31c31cSWarner Losh 
4143a31c31cSWarner Losh 	nvme_unregister_consumer(consumer_cookie);
4153a31c31cSWarner Losh }
4163a31c31cSWarner Losh 
4173a31c31cSWarner Losh SYSUNINIT(nvme_sim_unregister, SI_SUB_DRIVERS, SI_ORDER_ANY,
4183a31c31cSWarner Losh     nvme_sim_uninit, NULL);
419