xref: /freebsd/sys/dev/nvme/nvme_pci.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1f182f928SWarner Losh /*-
2f182f928SWarner Losh  * Copyright (C) 2012-2016 Intel Corporation
3f182f928SWarner Losh  * All rights reserved.
4f182f928SWarner Losh  *
5f182f928SWarner Losh  * Redistribution and use in source and binary forms, with or without
6f182f928SWarner Losh  * modification, are permitted provided that the following conditions
7f182f928SWarner Losh  * are met:
8f182f928SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9f182f928SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10f182f928SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11f182f928SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12f182f928SWarner Losh  *    documentation and/or other materials provided with the distribution.
13f182f928SWarner Losh  *
14f182f928SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15f182f928SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16f182f928SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17f182f928SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18f182f928SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19f182f928SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20f182f928SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f182f928SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f182f928SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f182f928SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24f182f928SWarner Losh  * SUCH DAMAGE.
25f182f928SWarner Losh  */
26f182f928SWarner Losh 
27f182f928SWarner Losh #include <sys/param.h>
28f182f928SWarner Losh #include <sys/systm.h>
29f182f928SWarner Losh #include <sys/buf.h>
30f182f928SWarner Losh #include <sys/bus.h>
31f182f928SWarner Losh #include <sys/conf.h>
32f182f928SWarner Losh #include <sys/proc.h>
33f182f928SWarner Losh #include <sys/smp.h>
341eab19cbSAlexander Motin #include <vm/vm.h>
35f182f928SWarner Losh 
36f182f928SWarner Losh #include <dev/pci/pcireg.h>
37f182f928SWarner Losh #include <dev/pci/pcivar.h>
38f182f928SWarner Losh 
39f182f928SWarner Losh #include "nvme_private.h"
40f182f928SWarner Losh 
41f182f928SWarner Losh static int    nvme_pci_probe(device_t);
42f182f928SWarner Losh static int    nvme_pci_attach(device_t);
43f182f928SWarner Losh static int    nvme_pci_detach(device_t);
444d547561SWarner Losh static int    nvme_pci_suspend(device_t);
454d547561SWarner Losh static int    nvme_pci_resume(device_t);
46f182f928SWarner Losh 
47e3bdf3daSAlexander Motin static int nvme_ctrlr_setup_interrupts(struct nvme_controller *ctrlr);
48f182f928SWarner Losh 
49f182f928SWarner Losh static device_method_t nvme_pci_methods[] = {
50f182f928SWarner Losh 	/* Device interface */
51f182f928SWarner Losh 	DEVMETHOD(device_probe,     nvme_pci_probe),
52f182f928SWarner Losh 	DEVMETHOD(device_attach,    nvme_pci_attach),
53f182f928SWarner Losh 	DEVMETHOD(device_detach,    nvme_pci_detach),
544d547561SWarner Losh 	DEVMETHOD(device_suspend,   nvme_pci_suspend),
554d547561SWarner Losh 	DEVMETHOD(device_resume,    nvme_pci_resume),
56f182f928SWarner Losh 	DEVMETHOD(device_shutdown,  nvme_shutdown),
57f182f928SWarner Losh 	{ 0, 0 }
58f182f928SWarner Losh };
59f182f928SWarner Losh 
60f182f928SWarner Losh static driver_t nvme_pci_driver = {
61f182f928SWarner Losh 	"nvme",
62f182f928SWarner Losh 	nvme_pci_methods,
63f182f928SWarner Losh 	sizeof(struct nvme_controller),
64f182f928SWarner Losh };
65f182f928SWarner Losh 
661093caa1SJohn Baldwin DRIVER_MODULE(nvme, pci, nvme_pci_driver, NULL, NULL);
67f182f928SWarner Losh 
68f182f928SWarner Losh static struct _pcsid
69f182f928SWarner Losh {
70f182f928SWarner Losh 	uint32_t	devid;
71f182f928SWarner Losh 	int		match_subdevice;
72f182f928SWarner Losh 	uint16_t	subdevice;
73f182f928SWarner Losh 	const char	*desc;
74f182f928SWarner Losh 	uint32_t	quirks;
75f182f928SWarner Losh } pci_ids[] = {
76f182f928SWarner Losh 	{ 0x01118086,		0, 0, "NVMe Controller"  },
77f182f928SWarner Losh 	{ IDT32_PCI_ID,		0, 0, "IDT NVMe Controller (32 channel)"  },
78f182f928SWarner Losh 	{ IDT8_PCI_ID,		0, 0, "IDT NVMe Controller (8 channel)" },
79f182f928SWarner Losh 	{ 0x09538086,		1, 0x3702, "DC P3700 SSD" },
80f182f928SWarner Losh 	{ 0x09538086,		1, 0x3703, "DC P3700 SSD [2.5\" SFF]" },
81f182f928SWarner Losh 	{ 0x09538086,		1, 0x3704, "DC P3500 SSD [Add-in Card]" },
82f182f928SWarner Losh 	{ 0x09538086,		1, 0x3705, "DC P3500 SSD [2.5\" SFF]" },
83f182f928SWarner Losh 	{ 0x09538086,		1, 0x3709, "DC P3600 SSD [Add-in Card]" },
84f182f928SWarner Losh 	{ 0x09538086,		1, 0x370a, "DC P3600 SSD [2.5\" SFF]" },
85053f8ed6SWarner Losh 	{ 0x09538086,		0, 0, "Intel DC PC3500", QUIRK_INTEL_ALIGNMENT },
86053f8ed6SWarner Losh 	{ 0x0a538086,		0, 0, "Intel DC PC3520", QUIRK_INTEL_ALIGNMENT },
87053f8ed6SWarner Losh 	{ 0x0a548086,		0, 0, "Intel DC PC4500", QUIRK_INTEL_ALIGNMENT },
88053f8ed6SWarner Losh 	{ 0x0a558086,		0, 0, "Dell Intel P4600", QUIRK_INTEL_ALIGNMENT },
89f182f928SWarner Losh 	{ 0x00031c58,		0, 0, "HGST SN100",	QUIRK_DELAY_B4_CHK_RDY },
90f182f928SWarner Losh 	{ 0x00231c58,		0, 0, "WDC SN200",	QUIRK_DELAY_B4_CHK_RDY },
91f182f928SWarner Losh 	{ 0x05401c5f,		0, 0, "Memblaze Pblaze4", QUIRK_DELAY_B4_CHK_RDY },
92f182f928SWarner Losh 	{ 0xa821144d,		0, 0, "Samsung PM1725", QUIRK_DELAY_B4_CHK_RDY },
93f182f928SWarner Losh 	{ 0xa822144d,		0, 0, "Samsung PM1725a", QUIRK_DELAY_B4_CHK_RDY },
942a31a06bSAlexander Motin 	{ 0x07f015ad,		0, 0, "VMware NVMe Controller" },
95*fc145250SVladimir Kondratyev 	{ 0x2003106b,		0, 0, "Apple S3X NVMe Controller" },
96f182f928SWarner Losh 	{ 0x00000000,		0, 0, NULL  }
97f182f928SWarner Losh };
98f182f928SWarner Losh 
99f182f928SWarner Losh static int
nvme_match(uint32_t devid,uint16_t subdevice,struct _pcsid * ep)100f182f928SWarner Losh nvme_match(uint32_t devid, uint16_t subdevice, struct _pcsid *ep)
101f182f928SWarner Losh {
102f182f928SWarner Losh 	if (devid != ep->devid)
103f182f928SWarner Losh 		return 0;
104f182f928SWarner Losh 
105f182f928SWarner Losh 	if (!ep->match_subdevice)
106f182f928SWarner Losh 		return 1;
107f182f928SWarner Losh 
108f182f928SWarner Losh 	if (subdevice == ep->subdevice)
109f182f928SWarner Losh 		return 1;
110f182f928SWarner Losh 	else
111f182f928SWarner Losh 		return 0;
112f182f928SWarner Losh }
113f182f928SWarner Losh 
114f182f928SWarner Losh static int
nvme_pci_probe(device_t device)115f182f928SWarner Losh nvme_pci_probe (device_t device)
116f182f928SWarner Losh {
117f182f928SWarner Losh 	struct nvme_controller *ctrlr = DEVICE2SOFTC(device);
118f182f928SWarner Losh 	struct _pcsid	*ep;
119f182f928SWarner Losh 	uint32_t	devid;
120f182f928SWarner Losh 	uint16_t	subdevice;
121f182f928SWarner Losh 
122f182f928SWarner Losh 	devid = pci_get_devid(device);
123f182f928SWarner Losh 	subdevice = pci_get_subdevice(device);
124f182f928SWarner Losh 	ep = pci_ids;
125f182f928SWarner Losh 
126f182f928SWarner Losh 	while (ep->devid) {
127f182f928SWarner Losh 		if (nvme_match(devid, subdevice, ep))
128f182f928SWarner Losh 			break;
129f182f928SWarner Losh 		++ep;
130f182f928SWarner Losh 	}
131f182f928SWarner Losh 	if (ep->devid)
132f182f928SWarner Losh 		ctrlr->quirks = ep->quirks;
133f182f928SWarner Losh 
134f182f928SWarner Losh 	if (ep->desc) {
135f182f928SWarner Losh 		device_set_desc(device, ep->desc);
136f182f928SWarner Losh 		return (BUS_PROBE_DEFAULT);
137f182f928SWarner Losh 	}
138f182f928SWarner Losh 
139f182f928SWarner Losh #if defined(PCIS_STORAGE_NVM)
140f182f928SWarner Losh 	if (pci_get_class(device)    == PCIC_STORAGE &&
141f182f928SWarner Losh 	    pci_get_subclass(device) == PCIS_STORAGE_NVM &&
142f182f928SWarner Losh 	    pci_get_progif(device)   == PCIP_STORAGE_NVM_ENTERPRISE_NVMHCI_1_0) {
143f182f928SWarner Losh 		device_set_desc(device, "Generic NVMe Device");
144f182f928SWarner Losh 		return (BUS_PROBE_GENERIC);
145f182f928SWarner Losh 	}
146f182f928SWarner Losh #endif
147f182f928SWarner Losh 
148f182f928SWarner Losh 	return (ENXIO);
149f182f928SWarner Losh }
150f182f928SWarner Losh 
151f182f928SWarner Losh static int
nvme_ctrlr_allocate_bar(struct nvme_controller * ctrlr)152f182f928SWarner Losh nvme_ctrlr_allocate_bar(struct nvme_controller *ctrlr)
153f182f928SWarner Losh {
154f182f928SWarner Losh 
155f182f928SWarner Losh 	ctrlr->resource_id = PCIR_BAR(0);
156f182f928SWarner Losh 
157f182f928SWarner Losh 	ctrlr->resource = bus_alloc_resource_any(ctrlr->dev, SYS_RES_MEMORY,
158f182f928SWarner Losh 	    &ctrlr->resource_id, RF_ACTIVE);
159f182f928SWarner Losh 
160f182f928SWarner Losh 	if(ctrlr->resource == NULL) {
161f182f928SWarner Losh 		nvme_printf(ctrlr, "unable to allocate pci resource\n");
162f182f928SWarner Losh 		return (ENOMEM);
163f182f928SWarner Losh 	}
164f182f928SWarner Losh 
165f182f928SWarner Losh 	ctrlr->bus_tag = rman_get_bustag(ctrlr->resource);
166f182f928SWarner Losh 	ctrlr->bus_handle = rman_get_bushandle(ctrlr->resource);
167f182f928SWarner Losh 	ctrlr->regs = (struct nvme_registers *)ctrlr->bus_handle;
168f182f928SWarner Losh 
169f182f928SWarner Losh 	/*
170f182f928SWarner Losh 	 * The NVMe spec allows for the MSI-X table to be placed behind
171f182f928SWarner Losh 	 *  BAR 4/5, separate from the control/doorbell registers.  Always
172f182f928SWarner Losh 	 *  try to map this bar, because it must be mapped prior to calling
173f182f928SWarner Losh 	 *  pci_alloc_msix().  If the table isn't behind BAR 4/5,
174f182f928SWarner Losh 	 *  bus_alloc_resource() will just return NULL which is OK.
175f182f928SWarner Losh 	 */
176f182f928SWarner Losh 	ctrlr->bar4_resource_id = PCIR_BAR(4);
177f182f928SWarner Losh 	ctrlr->bar4_resource = bus_alloc_resource_any(ctrlr->dev, SYS_RES_MEMORY,
178f182f928SWarner Losh 	    &ctrlr->bar4_resource_id, RF_ACTIVE);
179f182f928SWarner Losh 
180f182f928SWarner Losh 	return (0);
181f182f928SWarner Losh }
182f182f928SWarner Losh 
183f182f928SWarner Losh static int
nvme_pci_attach(device_t dev)184f182f928SWarner Losh nvme_pci_attach(device_t dev)
185f182f928SWarner Losh {
186f182f928SWarner Losh 	struct nvme_controller*ctrlr = DEVICE2SOFTC(dev);
187f182f928SWarner Losh 	int status;
188f182f928SWarner Losh 
189f182f928SWarner Losh 	ctrlr->dev = dev;
190f182f928SWarner Losh 	status = nvme_ctrlr_allocate_bar(ctrlr);
191f182f928SWarner Losh 	if (status != 0)
192f182f928SWarner Losh 		goto bad;
193f182f928SWarner Losh 	pci_enable_busmaster(dev);
194e3bdf3daSAlexander Motin 	status = nvme_ctrlr_setup_interrupts(ctrlr);
195e3bdf3daSAlexander Motin 	if (status != 0)
196e3bdf3daSAlexander Motin 		goto bad;
197f182f928SWarner Losh 	return nvme_attach(dev);
198f182f928SWarner Losh bad:
199f182f928SWarner Losh 	if (ctrlr->resource != NULL) {
200f182f928SWarner Losh 		bus_release_resource(dev, SYS_RES_MEMORY,
201f182f928SWarner Losh 		    ctrlr->resource_id, ctrlr->resource);
202f182f928SWarner Losh 	}
203f182f928SWarner Losh 
204f182f928SWarner Losh 	if (ctrlr->bar4_resource != NULL) {
205f182f928SWarner Losh 		bus_release_resource(dev, SYS_RES_MEMORY,
206f182f928SWarner Losh 		    ctrlr->bar4_resource_id, ctrlr->bar4_resource);
207f182f928SWarner Losh 	}
208f182f928SWarner Losh 
209f182f928SWarner Losh 	if (ctrlr->tag)
210f182f928SWarner Losh 		bus_teardown_intr(dev, ctrlr->res, ctrlr->tag);
211f182f928SWarner Losh 
212f182f928SWarner Losh 	if (ctrlr->res)
213f182f928SWarner Losh 		bus_release_resource(dev, SYS_RES_IRQ,
214f182f928SWarner Losh 		    rman_get_rid(ctrlr->res), ctrlr->res);
215f182f928SWarner Losh 
216e3bdf3daSAlexander Motin 	if (ctrlr->msi_count > 0)
217f182f928SWarner Losh 		pci_release_msi(dev);
218f182f928SWarner Losh 
219f182f928SWarner Losh 	return status;
220f182f928SWarner Losh }
221f182f928SWarner Losh 
222f182f928SWarner Losh static int
nvme_pci_detach(device_t dev)223f182f928SWarner Losh nvme_pci_detach(device_t dev)
224f182f928SWarner Losh {
225f182f928SWarner Losh 	struct nvme_controller*ctrlr = DEVICE2SOFTC(dev);
226ec743e0cSWarner Losh 	int rv;
227f182f928SWarner Losh 
228ec743e0cSWarner Losh 	rv = nvme_detach(dev);
229e3bdf3daSAlexander Motin 	if (ctrlr->msi_count > 0)
230f182f928SWarner Losh 		pci_release_msi(dev);
231f182f928SWarner Losh 	pci_disable_busmaster(dev);
232ec743e0cSWarner Losh 	return (rv);
233f182f928SWarner Losh }
234f182f928SWarner Losh 
235f182f928SWarner Losh static int
nvme_ctrlr_setup_shared(struct nvme_controller * ctrlr,int rid)236e3bdf3daSAlexander Motin nvme_ctrlr_setup_shared(struct nvme_controller *ctrlr, int rid)
237f182f928SWarner Losh {
238e3bdf3daSAlexander Motin 	int error;
239f182f928SWarner Losh 
240f182f928SWarner Losh 	ctrlr->num_io_queues = 1;
241e3bdf3daSAlexander Motin 	ctrlr->rid = rid;
242f182f928SWarner Losh 	ctrlr->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ,
243f182f928SWarner Losh 	    &ctrlr->rid, RF_SHAREABLE | RF_ACTIVE);
244f182f928SWarner Losh 	if (ctrlr->res == NULL) {
245e3bdf3daSAlexander Motin 		nvme_printf(ctrlr, "unable to allocate shared interrupt\n");
246f182f928SWarner Losh 		return (ENOMEM);
247f182f928SWarner Losh 	}
248f182f928SWarner Losh 
249e3bdf3daSAlexander Motin 	error = bus_setup_intr(ctrlr->dev, ctrlr->res,
250e3bdf3daSAlexander Motin 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, nvme_ctrlr_shared_handler,
251e3bdf3daSAlexander Motin 	    ctrlr, &ctrlr->tag);
252e3bdf3daSAlexander Motin 	if (error) {
253e3bdf3daSAlexander Motin 		nvme_printf(ctrlr, "unable to setup shared interrupt\n");
254e3bdf3daSAlexander Motin 		return (error);
255f182f928SWarner Losh 	}
256f182f928SWarner Losh 
257f182f928SWarner Losh 	return (0);
258f182f928SWarner Losh }
259f182f928SWarner Losh 
260e3bdf3daSAlexander Motin static int
nvme_ctrlr_setup_interrupts(struct nvme_controller * ctrlr)261f182f928SWarner Losh nvme_ctrlr_setup_interrupts(struct nvme_controller *ctrlr)
262f182f928SWarner Losh {
263f182f928SWarner Losh 	device_t	dev;
2641eab19cbSAlexander Motin 	int		force_intx, num_io_queues, per_cpu_io_queues;
265f182f928SWarner Losh 	int		min_cpus_per_ioq;
266e3bdf3daSAlexander Motin 	int		num_vectors_requested;
267f182f928SWarner Losh 
268f182f928SWarner Losh 	dev = ctrlr->dev;
269f182f928SWarner Losh 
2701eab19cbSAlexander Motin 	force_intx = 0;
2711eab19cbSAlexander Motin 	TUNABLE_INT_FETCH("hw.nvme.force_intx", &force_intx);
272e3bdf3daSAlexander Motin 	if (force_intx)
273e3bdf3daSAlexander Motin 		return (nvme_ctrlr_setup_shared(ctrlr, 0));
274f182f928SWarner Losh 
275e3bdf3daSAlexander Motin 	if (pci_msix_count(dev) == 0)
276e3bdf3daSAlexander Motin 		goto msi;
277e3bdf3daSAlexander Motin 
278e3bdf3daSAlexander Motin 	/*
279e3bdf3daSAlexander Motin 	 * Try to allocate one MSI-X per core for I/O queues, plus one
280e3bdf3daSAlexander Motin 	 * for admin queue, but accept single shared MSI-X if have to.
281e3bdf3daSAlexander Motin 	 * Fall back to MSI if can't get any MSI-X.
282e3bdf3daSAlexander Motin 	 */
2831eab19cbSAlexander Motin 	num_io_queues = mp_ncpus;
2841eab19cbSAlexander Motin 	TUNABLE_INT_FETCH("hw.nvme.num_io_queues", &num_io_queues);
2851eab19cbSAlexander Motin 	if (num_io_queues < 1 || num_io_queues > mp_ncpus)
2861eab19cbSAlexander Motin 		num_io_queues = mp_ncpus;
287f182f928SWarner Losh 
2881eab19cbSAlexander Motin 	per_cpu_io_queues = 1;
2891eab19cbSAlexander Motin 	TUNABLE_INT_FETCH("hw.nvme.per_cpu_io_queues", &per_cpu_io_queues);
2901eab19cbSAlexander Motin 	if (per_cpu_io_queues == 0)
2911eab19cbSAlexander Motin 		num_io_queues = 1;
2921eab19cbSAlexander Motin 
2931eab19cbSAlexander Motin 	min_cpus_per_ioq = smp_threads_per_core;
2941eab19cbSAlexander Motin 	TUNABLE_INT_FETCH("hw.nvme.min_cpus_per_ioq", &min_cpus_per_ioq);
2951eab19cbSAlexander Motin 	if (min_cpus_per_ioq > 1) {
2961eab19cbSAlexander Motin 		num_io_queues = min(num_io_queues,
2971eab19cbSAlexander Motin 		    max(1, mp_ncpus / min_cpus_per_ioq));
2981eab19cbSAlexander Motin 	}
2991eab19cbSAlexander Motin 
300e3bdf3daSAlexander Motin 	num_io_queues = min(num_io_queues, max(1, pci_msix_count(dev) - 1));
3011eab19cbSAlexander Motin 
3021eab19cbSAlexander Motin again:
3031eab19cbSAlexander Motin 	if (num_io_queues > vm_ndomains)
3041eab19cbSAlexander Motin 		num_io_queues -= num_io_queues % vm_ndomains;
305e3bdf3daSAlexander Motin 	num_vectors_requested = min(num_io_queues + 1, pci_msix_count(dev));
306e3bdf3daSAlexander Motin 	ctrlr->msi_count = num_vectors_requested;
307e3bdf3daSAlexander Motin 	if (pci_alloc_msix(dev, &ctrlr->msi_count) != 0) {
308e3bdf3daSAlexander Motin 		nvme_printf(ctrlr, "unable to allocate MSI-X\n");
309e3bdf3daSAlexander Motin 		ctrlr->msi_count = 0;
310e3bdf3daSAlexander Motin 		goto msi;
311f182f928SWarner Losh 	}
312e3bdf3daSAlexander Motin 	if (ctrlr->msi_count == 1)
313e3bdf3daSAlexander Motin 		return (nvme_ctrlr_setup_shared(ctrlr, 1));
314e3bdf3daSAlexander Motin 	if (ctrlr->msi_count != num_vectors_requested) {
315f182f928SWarner Losh 		pci_release_msi(dev);
316e3bdf3daSAlexander Motin 		num_io_queues = ctrlr->msi_count - 1;
3171eab19cbSAlexander Motin 		goto again;
3181eab19cbSAlexander Motin 	}
319f182f928SWarner Losh 
3201eab19cbSAlexander Motin 	ctrlr->num_io_queues = num_io_queues;
321e3bdf3daSAlexander Motin 	return (0);
322e3bdf3daSAlexander Motin 
323e3bdf3daSAlexander Motin msi:
324e3bdf3daSAlexander Motin 	/*
325e3bdf3daSAlexander Motin 	 * Try to allocate 2 MSIs (admin and I/O queues), but accept single
326e3bdf3daSAlexander Motin 	 * shared if have to.  Fall back to INTx if can't get any MSI.
327e3bdf3daSAlexander Motin 	 */
328e3bdf3daSAlexander Motin 	ctrlr->msi_count = min(pci_msi_count(dev), 2);
329e3bdf3daSAlexander Motin 	if (ctrlr->msi_count > 0) {
330e3bdf3daSAlexander Motin 		if (pci_alloc_msi(dev, &ctrlr->msi_count) != 0) {
331e3bdf3daSAlexander Motin 			nvme_printf(ctrlr, "unable to allocate MSI\n");
332e3bdf3daSAlexander Motin 			ctrlr->msi_count = 0;
333e3bdf3daSAlexander Motin 		} else if (ctrlr->msi_count == 2) {
334e3bdf3daSAlexander Motin 			ctrlr->num_io_queues = 1;
335e3bdf3daSAlexander Motin 			return (0);
336e3bdf3daSAlexander Motin 		}
337e3bdf3daSAlexander Motin 	}
338e3bdf3daSAlexander Motin 	return (nvme_ctrlr_setup_shared(ctrlr, ctrlr->msi_count > 0 ? 1 : 0));
339f182f928SWarner Losh }
3404d547561SWarner Losh 
3414d547561SWarner Losh static int
nvme_pci_suspend(device_t dev)3424d547561SWarner Losh nvme_pci_suspend(device_t dev)
3434d547561SWarner Losh {
3444d547561SWarner Losh 	struct nvme_controller	*ctrlr;
3454d547561SWarner Losh 
3464d547561SWarner Losh 	ctrlr = DEVICE2SOFTC(dev);
3474d547561SWarner Losh 	return (nvme_ctrlr_suspend(ctrlr));
3484d547561SWarner Losh }
3494d547561SWarner Losh 
3504d547561SWarner Losh static int
nvme_pci_resume(device_t dev)3514d547561SWarner Losh nvme_pci_resume(device_t dev)
3524d547561SWarner Losh {
3534d547561SWarner Losh 	struct nvme_controller	*ctrlr;
3544d547561SWarner Losh 
3554d547561SWarner Losh 	ctrlr = DEVICE2SOFTC(dev);
3564d547561SWarner Losh 	return (nvme_ctrlr_resume(ctrlr));
3574d547561SWarner Losh }
358