Home | History | Annotate | Line # | Download | only in pci
nvme_pci.c revision 1.4.2.1
      1  1.4.2.1  pgoyette /*	$NetBSD: nvme_pci.c,v 1.4.2.1 2016/11/04 14:49:10 pgoyette Exp $	*/
      2      1.1    nonaka /*	$OpenBSD: nvme_pci.c,v 1.3 2016/04/14 11:18:32 dlg Exp $ */
      3      1.1    nonaka 
      4      1.1    nonaka /*
      5      1.1    nonaka  * Copyright (c) 2014 David Gwynne <dlg (at) openbsd.org>
      6      1.1    nonaka  *
      7      1.1    nonaka  * Permission to use, copy, modify, and distribute this software for any
      8      1.1    nonaka  * purpose with or without fee is hereby granted, provided that the above
      9      1.1    nonaka  * copyright notice and this permission notice appear in all copies.
     10      1.1    nonaka  *
     11      1.1    nonaka  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12      1.1    nonaka  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13      1.1    nonaka  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14      1.1    nonaka  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15      1.1    nonaka  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16      1.1    nonaka  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17      1.1    nonaka  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18      1.1    nonaka  */
     19      1.1    nonaka 
     20      1.1    nonaka /*-
     21      1.1    nonaka  * Copyright (C) 2016 NONAKA Kimihiro <nonaka (at) netbsd.org>
     22      1.1    nonaka  * All rights reserved.
     23      1.1    nonaka  *
     24      1.1    nonaka  * Redistribution and use in source and binary forms, with or without
     25      1.1    nonaka  * modification, are permitted provided that the following conditions
     26      1.1    nonaka  * are met:
     27      1.1    nonaka  * 1. Redistributions of source code must retain the above copyright
     28      1.1    nonaka  *    notice, this list of conditions and the following disclaimer.
     29      1.1    nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     30      1.1    nonaka  *    notice, this list of conditions and the following disclaimer in the
     31      1.1    nonaka  *    documentation and/or other materials provided with the distribution.
     32      1.1    nonaka  *
     33      1.1    nonaka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     34      1.1    nonaka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     35      1.1    nonaka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     36      1.1    nonaka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     37      1.1    nonaka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38      1.1    nonaka  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     39      1.1    nonaka  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     40      1.1    nonaka  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     41      1.1    nonaka  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     42      1.1    nonaka  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     43      1.1    nonaka  */
     44      1.1    nonaka 
     45      1.1    nonaka #include <sys/cdefs.h>
     46  1.4.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: nvme_pci.c,v 1.4.2.1 2016/11/04 14:49:10 pgoyette Exp $");
     47      1.1    nonaka 
     48      1.1    nonaka #include <sys/param.h>
     49      1.1    nonaka #include <sys/systm.h>
     50      1.1    nonaka #include <sys/kernel.h>
     51      1.1    nonaka #include <sys/device.h>
     52      1.1    nonaka #include <sys/bitops.h>
     53      1.1    nonaka #include <sys/bus.h>
     54      1.1    nonaka #include <sys/cpu.h>
     55      1.1    nonaka #include <sys/interrupt.h>
     56      1.1    nonaka #include <sys/kmem.h>
     57      1.1    nonaka #include <sys/pmf.h>
     58  1.4.2.1  pgoyette #include <sys/module.h>
     59      1.1    nonaka 
     60      1.1    nonaka #include <dev/pci/pcireg.h>
     61      1.1    nonaka #include <dev/pci/pcivar.h>
     62      1.1    nonaka 
     63      1.1    nonaka #include <dev/ic/nvmereg.h>
     64      1.1    nonaka #include <dev/ic/nvmevar.h>
     65      1.1    nonaka 
     66      1.1    nonaka int nvme_pci_force_intx = 0;
     67  1.4.2.1  pgoyette int nvme_pci_mpsafe = 1;
     68      1.1    nonaka int nvme_pci_mq = 1;		/* INTx: ioq=1, MSI/MSI-X: ioq=ncpu */
     69      1.1    nonaka 
     70      1.1    nonaka #define NVME_PCI_BAR		0x10
     71      1.1    nonaka 
     72  1.4.2.1  pgoyette #ifndef __HAVE_PCI_MSI_MSIX
     73  1.4.2.1  pgoyette #define pci_intr_release(pc, intrs, nintrs) \
     74  1.4.2.1  pgoyette 	kmem_free(intrs, sizeof(*intrs) * nintrs)
     75  1.4.2.1  pgoyette #define pci_intr_establish_xname(pc, ih, level, intrhand, intrarg, xname) \
     76  1.4.2.1  pgoyette 	pci_intr_establish(pc, ih, level, intrhand, intrarg)
     77  1.4.2.1  pgoyette #endif
     78  1.4.2.1  pgoyette 
     79      1.1    nonaka struct nvme_pci_softc {
     80      1.1    nonaka 	struct nvme_softc	psc_nvme;
     81      1.1    nonaka 
     82      1.1    nonaka 	pci_chipset_tag_t	psc_pc;
     83      1.1    nonaka 	pci_intr_handle_t	*psc_intrs;
     84      1.1    nonaka 	int			psc_nintrs;
     85      1.1    nonaka };
     86      1.1    nonaka 
     87      1.1    nonaka static int	nvme_pci_match(device_t, cfdata_t, void *);
     88      1.1    nonaka static void	nvme_pci_attach(device_t, device_t, void *);
     89      1.1    nonaka static int	nvme_pci_detach(device_t, int);
     90  1.4.2.1  pgoyette static int	nvme_pci_rescan(device_t, const char *, const int *);
     91      1.1    nonaka 
     92      1.1    nonaka CFATTACH_DECL3_NEW(nvme_pci, sizeof(struct nvme_pci_softc),
     93  1.4.2.1  pgoyette     nvme_pci_match, nvme_pci_attach, nvme_pci_detach, NULL, nvme_pci_rescan,
     94      1.1    nonaka     nvme_childdet, DVF_DETACH_SHUTDOWN);
     95      1.1    nonaka 
     96      1.1    nonaka static int	nvme_pci_intr_establish(struct nvme_softc *,
     97      1.1    nonaka 		    uint16_t, struct nvme_queue *);
     98      1.1    nonaka static int	nvme_pci_intr_disestablish(struct nvme_softc *, uint16_t);
     99      1.1    nonaka static int	nvme_pci_setup_intr(struct pci_attach_args *,
    100      1.1    nonaka 		    struct nvme_pci_softc *);
    101      1.1    nonaka 
    102      1.1    nonaka static int
    103      1.1    nonaka nvme_pci_match(device_t parent, cfdata_t match, void *aux)
    104      1.1    nonaka {
    105      1.1    nonaka 	struct pci_attach_args *pa = aux;
    106      1.1    nonaka 
    107      1.1    nonaka 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    108      1.1    nonaka 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_NVM &&
    109      1.1    nonaka 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_NVM_NVME)
    110      1.1    nonaka 		return 1;
    111      1.1    nonaka 
    112      1.1    nonaka 	return 0;
    113      1.1    nonaka }
    114      1.1    nonaka 
    115      1.1    nonaka static void
    116      1.1    nonaka nvme_pci_attach(device_t parent, device_t self, void *aux)
    117      1.1    nonaka {
    118      1.1    nonaka 	struct nvme_pci_softc *psc = device_private(self);
    119      1.1    nonaka 	struct nvme_softc *sc = &psc->psc_nvme;
    120      1.1    nonaka 	struct pci_attach_args *pa = aux;
    121  1.4.2.1  pgoyette 	pcireg_t memtype, reg;
    122      1.1    nonaka 	bus_addr_t memaddr;
    123  1.4.2.1  pgoyette 	int flags, error;
    124  1.4.2.1  pgoyette #ifdef __HAVE_PCI_MSI_MSIX
    125  1.4.2.1  pgoyette 	int msixoff;
    126  1.4.2.1  pgoyette #endif
    127      1.1    nonaka 
    128      1.1    nonaka 	sc->sc_dev = self;
    129      1.1    nonaka 	psc->psc_pc = pa->pa_pc;
    130      1.1    nonaka 	if (pci_dma64_available(pa))
    131      1.1    nonaka 		sc->sc_dmat = pa->pa_dmat64;
    132      1.1    nonaka 	else
    133      1.1    nonaka 		sc->sc_dmat = pa->pa_dmat;
    134      1.1    nonaka 
    135      1.1    nonaka 	pci_aprint_devinfo(pa, NULL);
    136      1.1    nonaka 
    137  1.4.2.1  pgoyette 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    138  1.4.2.1  pgoyette 	if ((reg & PCI_COMMAND_MASTER_ENABLE) == 0) {
    139  1.4.2.1  pgoyette 		reg |= PCI_COMMAND_MASTER_ENABLE;
    140  1.4.2.1  pgoyette         	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    141  1.4.2.1  pgoyette 	}
    142  1.4.2.1  pgoyette 
    143      1.1    nonaka 	/* Map registers */
    144      1.1    nonaka 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, NVME_PCI_BAR);
    145      1.1    nonaka 	if (PCI_MAPREG_TYPE(memtype) != PCI_MAPREG_TYPE_MEM) {
    146      1.1    nonaka 		aprint_error_dev(self, "invalid type (type=0x%x)\n", memtype);
    147      1.1    nonaka 		return;
    148      1.1    nonaka 	}
    149      1.1    nonaka 	sc->sc_iot = pa->pa_memt;
    150      1.1    nonaka 	error = pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START,
    151      1.1    nonaka 	    memtype, &memaddr, &sc->sc_ios, &flags);
    152      1.1    nonaka 	if (error) {
    153      1.1    nonaka 		aprint_error_dev(self, "can't get map info\n");
    154      1.1    nonaka 		return;
    155      1.1    nonaka 	}
    156  1.4.2.1  pgoyette 
    157  1.4.2.1  pgoyette #ifdef __HAVE_PCI_MSI_MSIX
    158      1.1    nonaka 	if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &msixoff,
    159      1.1    nonaka 	    NULL)) {
    160      1.1    nonaka 		pcireg_t msixtbl;
    161      1.1    nonaka 		uint32_t table_offset;
    162      1.1    nonaka 		int bir;
    163      1.1    nonaka 
    164      1.1    nonaka 		msixtbl = pci_conf_read(pa->pa_pc, pa->pa_tag,
    165      1.1    nonaka 		    msixoff + PCI_MSIX_TBLOFFSET);
    166      1.1    nonaka 		table_offset = msixtbl & PCI_MSIX_TBLOFFSET_MASK;
    167      1.1    nonaka 		bir = msixtbl & PCI_MSIX_PBABIR_MASK;
    168      1.1    nonaka 		if (bir == 0) {
    169      1.1    nonaka 			sc->sc_ios = table_offset;
    170      1.1    nonaka 		}
    171      1.1    nonaka 	}
    172  1.4.2.1  pgoyette #endif /* __HAVE_PCI_MSI_MSIX */
    173  1.4.2.1  pgoyette 
    174      1.1    nonaka 	error = bus_space_map(sc->sc_iot, memaddr, sc->sc_ios, flags,
    175      1.1    nonaka 	    &sc->sc_ioh);
    176      1.1    nonaka 	if (error != 0) {
    177      1.1    nonaka 		aprint_error_dev(self, "can't map mem space (error=%d)\n",
    178      1.1    nonaka 		    error);
    179      1.1    nonaka 		return;
    180      1.1    nonaka 	}
    181      1.1    nonaka 
    182      1.1    nonaka 	/* Establish interrupts */
    183      1.1    nonaka 	if (nvme_pci_setup_intr(pa, psc) != 0) {
    184      1.1    nonaka 		aprint_error_dev(self, "unable to allocate interrupt\n");
    185      1.1    nonaka 		goto unmap;
    186      1.1    nonaka 	}
    187      1.1    nonaka 	sc->sc_intr_establish = nvme_pci_intr_establish;
    188      1.1    nonaka 	sc->sc_intr_disestablish = nvme_pci_intr_disestablish;
    189      1.1    nonaka 
    190  1.4.2.1  pgoyette 	sc->sc_ih = kmem_zalloc(sizeof(*sc->sc_ih) * psc->psc_nintrs, KM_SLEEP);
    191      1.1    nonaka 	if (sc->sc_ih == NULL) {
    192      1.1    nonaka 		aprint_error_dev(self, "unable to allocate ih memory\n");
    193      1.1    nonaka 		goto intr_release;
    194      1.1    nonaka 	}
    195      1.1    nonaka 
    196  1.4.2.1  pgoyette 	sc->sc_softih = kmem_zalloc(
    197  1.4.2.1  pgoyette 	    sizeof(*sc->sc_softih) * psc->psc_nintrs, KM_SLEEP);
    198  1.4.2.1  pgoyette 	if (sc->sc_softih == NULL) {
    199  1.4.2.1  pgoyette 		aprint_error_dev(self,
    200  1.4.2.1  pgoyette 		    "unable to allocate softih memory\n");
    201  1.4.2.1  pgoyette 		goto intr_free;
    202  1.4.2.1  pgoyette 	}
    203  1.4.2.1  pgoyette 
    204      1.1    nonaka 	if (nvme_attach(sc) != 0) {
    205      1.1    nonaka 		/* error printed by nvme_attach() */
    206  1.4.2.1  pgoyette 		goto softintr_free;
    207      1.1    nonaka 	}
    208      1.1    nonaka 
    209      1.1    nonaka 	if (!pmf_device_register(self, NULL, NULL))
    210      1.1    nonaka 		aprint_error_dev(self, "couldn't establish power handler\n");
    211      1.1    nonaka 
    212      1.1    nonaka 	SET(sc->sc_flags, NVME_F_ATTACHED);
    213      1.1    nonaka 	return;
    214      1.1    nonaka 
    215  1.4.2.1  pgoyette softintr_free:
    216  1.4.2.1  pgoyette 	kmem_free(sc->sc_softih, sizeof(*sc->sc_softih) * psc->psc_nintrs);
    217      1.2    nonaka intr_free:
    218  1.4.2.1  pgoyette 	kmem_free(sc->sc_ih, sizeof(*sc->sc_ih) * psc->psc_nintrs);
    219      1.1    nonaka 	sc->sc_nq = 0;
    220      1.1    nonaka intr_release:
    221      1.1    nonaka 	pci_intr_release(pa->pa_pc, psc->psc_intrs, psc->psc_nintrs);
    222      1.1    nonaka 	psc->psc_nintrs = 0;
    223      1.1    nonaka unmap:
    224      1.1    nonaka 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    225      1.1    nonaka 	sc->sc_ios = 0;
    226      1.1    nonaka }
    227      1.1    nonaka 
    228      1.1    nonaka static int
    229  1.4.2.1  pgoyette nvme_pci_rescan(device_t self, const char *attr, const int *flags)
    230  1.4.2.1  pgoyette {
    231  1.4.2.1  pgoyette 
    232  1.4.2.1  pgoyette 	return nvme_rescan(self, attr, flags);
    233  1.4.2.1  pgoyette }
    234  1.4.2.1  pgoyette 
    235  1.4.2.1  pgoyette static int
    236      1.1    nonaka nvme_pci_detach(device_t self, int flags)
    237      1.1    nonaka {
    238      1.1    nonaka 	struct nvme_pci_softc *psc = device_private(self);
    239      1.1    nonaka 	struct nvme_softc *sc = &psc->psc_nvme;
    240  1.4.2.1  pgoyette 	int error;
    241      1.1    nonaka 
    242      1.1    nonaka 	if (!ISSET(sc->sc_flags, NVME_F_ATTACHED))
    243      1.1    nonaka 		return 0;
    244      1.1    nonaka 
    245      1.1    nonaka 	error = nvme_detach(sc, flags);
    246      1.1    nonaka 	if (error)
    247      1.1    nonaka 		return error;
    248      1.1    nonaka 
    249  1.4.2.1  pgoyette 	kmem_free(sc->sc_softih, sizeof(*sc->sc_softih) * psc->psc_nintrs);
    250  1.4.2.1  pgoyette 	sc->sc_softih = NULL;
    251  1.4.2.1  pgoyette 
    252  1.4.2.1  pgoyette 	kmem_free(sc->sc_ih, sizeof(*sc->sc_ih) * psc->psc_nintrs);
    253      1.1    nonaka 	pci_intr_release(psc->psc_pc, psc->psc_intrs, psc->psc_nintrs);
    254      1.1    nonaka 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    255      1.1    nonaka 	return 0;
    256      1.1    nonaka }
    257      1.1    nonaka 
    258      1.1    nonaka static int
    259      1.1    nonaka nvme_pci_intr_establish(struct nvme_softc *sc, uint16_t qid,
    260      1.1    nonaka     struct nvme_queue *q)
    261      1.1    nonaka {
    262      1.1    nonaka 	struct nvme_pci_softc *psc = (struct nvme_pci_softc *)sc;
    263      1.1    nonaka 	char intr_xname[INTRDEVNAMEBUF];
    264      1.1    nonaka 	char intrbuf[PCI_INTRSTR_LEN];
    265      1.1    nonaka 	const char *intrstr = NULL;
    266      1.1    nonaka 	int (*ih_func)(void *);
    267  1.4.2.1  pgoyette 	void (*ih_func_soft)(void *);
    268      1.1    nonaka 	void *ih_arg;
    269  1.4.2.1  pgoyette #ifdef __HAVE_PCI_MSI_MSIX
    270      1.1    nonaka 	int error;
    271  1.4.2.1  pgoyette #endif
    272      1.1    nonaka 
    273  1.4.2.1  pgoyette 	KASSERT(sc->sc_use_mq || qid == NVME_ADMIN_Q);
    274      1.1    nonaka 	KASSERT(sc->sc_ih[qid] == NULL);
    275      1.1    nonaka 
    276      1.1    nonaka 	if (nvme_pci_mpsafe) {
    277      1.1    nonaka 		pci_intr_setattr(psc->psc_pc, &psc->psc_intrs[qid],
    278      1.1    nonaka 		    PCI_INTR_MPSAFE, true);
    279      1.1    nonaka 	}
    280  1.4.2.1  pgoyette 
    281  1.4.2.1  pgoyette #ifdef __HAVE_PCI_MSI_MSIX
    282      1.1    nonaka 	if (!sc->sc_use_mq) {
    283  1.4.2.1  pgoyette #endif
    284      1.1    nonaka 		snprintf(intr_xname, sizeof(intr_xname), "%s",
    285      1.1    nonaka 		    device_xname(sc->sc_dev));
    286      1.1    nonaka 		ih_arg = sc;
    287      1.1    nonaka 		ih_func = nvme_intr;
    288  1.4.2.1  pgoyette 		ih_func_soft = nvme_softintr_intx;
    289  1.4.2.1  pgoyette #ifdef __HAVE_PCI_MSI_MSIX
    290  1.4.2.1  pgoyette 	}
    291  1.4.2.1  pgoyette 	else {
    292  1.4.2.1  pgoyette 		if (qid == NVME_ADMIN_Q) {
    293      1.1    nonaka 			snprintf(intr_xname, sizeof(intr_xname), "%s adminq",
    294      1.1    nonaka 			    device_xname(sc->sc_dev));
    295      1.1    nonaka 		} else {
    296      1.1    nonaka 			snprintf(intr_xname, sizeof(intr_xname), "%s ioq%d",
    297      1.1    nonaka 			    device_xname(sc->sc_dev), qid);
    298      1.1    nonaka 		}
    299      1.1    nonaka 		ih_arg = q;
    300  1.4.2.1  pgoyette 		ih_func = nvme_intr_msi;
    301  1.4.2.1  pgoyette 		ih_func_soft = nvme_softintr_msi;
    302      1.1    nonaka 	}
    303  1.4.2.1  pgoyette #endif /* __HAVE_PCI_MSI_MSIX */
    304  1.4.2.1  pgoyette 
    305  1.4.2.1  pgoyette 	/* establish hardware interrupt */
    306      1.1    nonaka 	sc->sc_ih[qid] = pci_intr_establish_xname(psc->psc_pc,
    307      1.1    nonaka 	    psc->psc_intrs[qid], IPL_BIO, ih_func, ih_arg, intr_xname);
    308      1.1    nonaka 	if (sc->sc_ih[qid] == NULL) {
    309      1.1    nonaka 		aprint_error_dev(sc->sc_dev,
    310      1.1    nonaka 		    "unable to establish %s interrupt\n", intr_xname);
    311      1.1    nonaka 		return 1;
    312      1.1    nonaka 	}
    313  1.4.2.1  pgoyette 
    314  1.4.2.1  pgoyette 	/* establish also the software interrupt */
    315  1.4.2.1  pgoyette 	sc->sc_softih[qid] = softint_establish(
    316  1.4.2.1  pgoyette 	    SOFTINT_BIO|(nvme_pci_mpsafe ? SOFTINT_MPSAFE : 0),
    317  1.4.2.1  pgoyette 	    ih_func_soft, q);
    318  1.4.2.1  pgoyette 	if (sc->sc_softih[qid] == NULL) {
    319  1.4.2.1  pgoyette 		pci_intr_disestablish(psc->psc_pc, sc->sc_ih[qid]);
    320  1.4.2.1  pgoyette 		sc->sc_ih[qid] = NULL;
    321  1.4.2.1  pgoyette 
    322  1.4.2.1  pgoyette 		aprint_error_dev(sc->sc_dev,
    323  1.4.2.1  pgoyette 		    "unable to establish %s soft interrupt\n",
    324  1.4.2.1  pgoyette 		    intr_xname);
    325  1.4.2.1  pgoyette 		return 1;
    326  1.4.2.1  pgoyette 	}
    327  1.4.2.1  pgoyette 
    328      1.1    nonaka 	intrstr = pci_intr_string(psc->psc_pc, psc->psc_intrs[qid], intrbuf,
    329      1.1    nonaka 	    sizeof(intrbuf));
    330      1.1    nonaka 	if (!sc->sc_use_mq) {
    331      1.1    nonaka 		aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    332  1.4.2.1  pgoyette 	}
    333  1.4.2.1  pgoyette #ifdef __HAVE_PCI_MSI_MSIX
    334  1.4.2.1  pgoyette 	else if (qid == NVME_ADMIN_Q) {
    335      1.1    nonaka 		aprint_normal_dev(sc->sc_dev,
    336      1.1    nonaka 		    "for admin queue interrupting at %s\n", intrstr);
    337      1.1    nonaka 	} else if (!nvme_pci_mpsafe) {
    338      1.1    nonaka 		aprint_normal_dev(sc->sc_dev,
    339      1.1    nonaka 		    "for io queue %d interrupting at %s\n", qid, intrstr);
    340      1.1    nonaka 	} else {
    341  1.4.2.1  pgoyette 		kcpuset_t *affinity;
    342  1.4.2.1  pgoyette 		cpuid_t affinity_to;
    343  1.4.2.1  pgoyette 
    344      1.1    nonaka 		kcpuset_create(&affinity, true);
    345      1.1    nonaka 		affinity_to = (qid - 1) % ncpu;
    346      1.1    nonaka 		kcpuset_set(affinity, affinity_to);
    347      1.1    nonaka 		error = interrupt_distribute(sc->sc_ih[qid], affinity, NULL);
    348      1.1    nonaka 		kcpuset_destroy(affinity);
    349      1.1    nonaka 		aprint_normal_dev(sc->sc_dev,
    350      1.1    nonaka 		    "for io queue %d interrupting at %s", qid, intrstr);
    351      1.1    nonaka 		if (error == 0)
    352      1.1    nonaka 			aprint_normal(" affinity to cpu%lu", affinity_to);
    353      1.1    nonaka 		aprint_normal("\n");
    354      1.1    nonaka 	}
    355  1.4.2.1  pgoyette #endif
    356      1.1    nonaka 	return 0;
    357      1.1    nonaka }
    358      1.1    nonaka 
    359      1.1    nonaka static int
    360      1.1    nonaka nvme_pci_intr_disestablish(struct nvme_softc *sc, uint16_t qid)
    361      1.1    nonaka {
    362      1.1    nonaka 	struct nvme_pci_softc *psc = (struct nvme_pci_softc *)sc;
    363      1.1    nonaka 
    364  1.4.2.1  pgoyette 	KASSERT(sc->sc_use_mq || qid == NVME_ADMIN_Q);
    365      1.1    nonaka 	KASSERT(sc->sc_ih[qid] != NULL);
    366      1.1    nonaka 
    367  1.4.2.1  pgoyette 	if (sc->sc_softih) {
    368  1.4.2.1  pgoyette 		softint_disestablish(sc->sc_softih[qid]);
    369  1.4.2.1  pgoyette 		sc->sc_softih[qid] = NULL;
    370  1.4.2.1  pgoyette 	}
    371  1.4.2.1  pgoyette 
    372      1.1    nonaka 	pci_intr_disestablish(psc->psc_pc, sc->sc_ih[qid]);
    373      1.1    nonaka 	sc->sc_ih[qid] = NULL;
    374      1.1    nonaka 
    375      1.1    nonaka 	return 0;
    376      1.1    nonaka }
    377      1.1    nonaka 
    378      1.1    nonaka static int
    379      1.1    nonaka nvme_pci_setup_intr(struct pci_attach_args *pa, struct nvme_pci_softc *psc)
    380      1.1    nonaka {
    381      1.1    nonaka 	struct nvme_softc *sc = &psc->psc_nvme;
    382  1.4.2.1  pgoyette #ifdef __HAVE_PCI_MSI_MSIX
    383  1.4.2.1  pgoyette 	int error;
    384      1.1    nonaka 	int counts[PCI_INTR_TYPE_SIZE], alloced_counts[PCI_INTR_TYPE_SIZE];
    385  1.4.2.1  pgoyette 	pci_intr_handle_t *ihps;
    386      1.1    nonaka 	int max_type, intr_type;
    387  1.4.2.1  pgoyette #else
    388  1.4.2.1  pgoyette 	pci_intr_handle_t ih;
    389  1.4.2.1  pgoyette #endif /* __HAVE_PCI_MSI_MSIX */
    390      1.1    nonaka 
    391  1.4.2.1  pgoyette #ifdef __HAVE_PCI_MSI_MSIX
    392      1.1    nonaka 	if (nvme_pci_force_intx) {
    393      1.1    nonaka 		max_type = PCI_INTR_TYPE_INTX;
    394      1.1    nonaka 		goto force_intx;
    395      1.1    nonaka 	}
    396      1.1    nonaka 
    397      1.1    nonaka 	/* MSI-X */
    398      1.1    nonaka 	max_type = PCI_INTR_TYPE_MSIX;
    399      1.1    nonaka 	counts[PCI_INTR_TYPE_MSIX] = min(pci_msix_count(pa->pa_pc, pa->pa_tag),
    400      1.1    nonaka 	    ncpu + 1);
    401      1.1    nonaka 	if (counts[PCI_INTR_TYPE_MSIX] > 0) {
    402      1.1    nonaka 		memset(alloced_counts, 0, sizeof(alloced_counts));
    403      1.1    nonaka 		alloced_counts[PCI_INTR_TYPE_MSIX] = counts[PCI_INTR_TYPE_MSIX];
    404      1.1    nonaka 		if (pci_intr_alloc(pa, &ihps, alloced_counts,
    405      1.1    nonaka 		    PCI_INTR_TYPE_MSIX)) {
    406      1.1    nonaka 			counts[PCI_INTR_TYPE_MSIX] = 0;
    407      1.1    nonaka 		} else {
    408      1.1    nonaka 			counts[PCI_INTR_TYPE_MSIX] =
    409      1.1    nonaka 			    alloced_counts[PCI_INTR_TYPE_MSIX];
    410      1.1    nonaka 			pci_intr_release(pa->pa_pc, ihps,
    411      1.1    nonaka 			    alloced_counts[PCI_INTR_TYPE_MSIX]);
    412      1.1    nonaka 		}
    413      1.1    nonaka 	}
    414      1.1    nonaka 	if (counts[PCI_INTR_TYPE_MSIX] < 2) {
    415      1.1    nonaka 		counts[PCI_INTR_TYPE_MSIX] = 0;
    416      1.1    nonaka 		max_type = PCI_INTR_TYPE_MSI;
    417      1.1    nonaka 	} else if (!nvme_pci_mq || !nvme_pci_mpsafe) {
    418      1.1    nonaka 		counts[PCI_INTR_TYPE_MSIX] = 2;	/* adminq + 1 ioq */
    419      1.1    nonaka 	}
    420      1.1    nonaka 
    421      1.1    nonaka retry_msi:
    422      1.1    nonaka 	/* MSI */
    423      1.1    nonaka 	counts[PCI_INTR_TYPE_MSI] = pci_msi_count(pa->pa_pc, pa->pa_tag);
    424      1.1    nonaka 	if (counts[PCI_INTR_TYPE_MSI] > 0) {
    425      1.1    nonaka 		while (counts[PCI_INTR_TYPE_MSI] > ncpu + 1) {
    426      1.1    nonaka 			if (counts[PCI_INTR_TYPE_MSI] / 2 <= ncpu + 1)
    427      1.1    nonaka 				break;
    428      1.1    nonaka 			counts[PCI_INTR_TYPE_MSI] /= 2;
    429      1.1    nonaka 		}
    430      1.1    nonaka 		memset(alloced_counts, 0, sizeof(alloced_counts));
    431      1.1    nonaka 		alloced_counts[PCI_INTR_TYPE_MSI] = counts[PCI_INTR_TYPE_MSI];
    432      1.1    nonaka 		if (pci_intr_alloc(pa, &ihps, alloced_counts,
    433      1.1    nonaka 		    PCI_INTR_TYPE_MSI)) {
    434      1.1    nonaka 			counts[PCI_INTR_TYPE_MSI] = 0;
    435      1.1    nonaka 		} else {
    436      1.1    nonaka 			counts[PCI_INTR_TYPE_MSI] =
    437      1.1    nonaka 			    alloced_counts[PCI_INTR_TYPE_MSI];
    438      1.1    nonaka 			pci_intr_release(pa->pa_pc, ihps,
    439      1.1    nonaka 			    alloced_counts[PCI_INTR_TYPE_MSI]);
    440      1.1    nonaka 		}
    441      1.1    nonaka 	}
    442      1.1    nonaka 	if (counts[PCI_INTR_TYPE_MSI] < 1) {
    443      1.1    nonaka 		counts[PCI_INTR_TYPE_MSI] = 0;
    444      1.1    nonaka 		if (max_type == PCI_INTR_TYPE_MSI)
    445      1.1    nonaka 			max_type = PCI_INTR_TYPE_INTX;
    446      1.1    nonaka 	} else if (!nvme_pci_mq || !nvme_pci_mpsafe) {
    447      1.1    nonaka 		if (counts[PCI_INTR_TYPE_MSI] > 2)
    448      1.1    nonaka 			counts[PCI_INTR_TYPE_MSI] = 2;	/* adminq + 1 ioq */
    449      1.1    nonaka 	}
    450      1.1    nonaka 
    451      1.1    nonaka force_intx:
    452      1.1    nonaka 	/* INTx */
    453      1.1    nonaka 	counts[PCI_INTR_TYPE_INTX] = 1;
    454      1.1    nonaka 
    455      1.1    nonaka 	memcpy(alloced_counts, counts, sizeof(counts));
    456      1.1    nonaka 	error = pci_intr_alloc(pa, &ihps, alloced_counts, max_type);
    457      1.1    nonaka 	if (error) {
    458      1.1    nonaka 		if (max_type != PCI_INTR_TYPE_INTX) {
    459      1.1    nonaka retry:
    460      1.1    nonaka 			memset(counts, 0, sizeof(counts));
    461      1.1    nonaka 			if (max_type == PCI_INTR_TYPE_MSIX) {
    462      1.1    nonaka 				max_type = PCI_INTR_TYPE_MSI;
    463      1.1    nonaka 				goto retry_msi;
    464      1.1    nonaka 			} else {
    465      1.1    nonaka 				max_type = PCI_INTR_TYPE_INTX;
    466      1.1    nonaka 				goto force_intx;
    467      1.1    nonaka 			}
    468      1.1    nonaka 		}
    469      1.1    nonaka 		return error;
    470      1.1    nonaka 	}
    471      1.1    nonaka 
    472      1.4  knakahar 	intr_type = pci_intr_type(pa->pa_pc, ihps[0]);
    473      1.1    nonaka 	if (alloced_counts[intr_type] < counts[intr_type]) {
    474      1.1    nonaka 		if (intr_type != PCI_INTR_TYPE_INTX) {
    475      1.1    nonaka 			pci_intr_release(pa->pa_pc, ihps,
    476      1.1    nonaka 			    alloced_counts[intr_type]);
    477      1.1    nonaka 			max_type = intr_type;
    478      1.1    nonaka 			goto retry;
    479      1.1    nonaka 		}
    480      1.1    nonaka 		return EBUSY;
    481      1.1    nonaka 	}
    482      1.1    nonaka 
    483      1.1    nonaka 	psc->psc_intrs = ihps;
    484      1.1    nonaka 	psc->psc_nintrs = alloced_counts[intr_type];
    485      1.1    nonaka 	if (intr_type == PCI_INTR_TYPE_MSI) {
    486      1.1    nonaka 		if (alloced_counts[intr_type] > ncpu + 1)
    487      1.1    nonaka 			alloced_counts[intr_type] = ncpu + 1;
    488      1.1    nonaka 	}
    489      1.1    nonaka 	sc->sc_use_mq = alloced_counts[intr_type] > 1;
    490      1.1    nonaka 	sc->sc_nq = sc->sc_use_mq ? alloced_counts[intr_type] - 1 : 1;
    491  1.4.2.1  pgoyette 
    492  1.4.2.1  pgoyette #else /* !__HAVE_PCI_MSI_MSIX */
    493  1.4.2.1  pgoyette         if (pci_intr_map(pa, &ih)) {
    494  1.4.2.1  pgoyette                 aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
    495  1.4.2.1  pgoyette                 return EBUSY;
    496  1.4.2.1  pgoyette         }
    497  1.4.2.1  pgoyette 
    498  1.4.2.1  pgoyette 	psc->psc_intrs = kmem_zalloc(sizeof(ih), KM_SLEEP);
    499  1.4.2.1  pgoyette 	psc->psc_intrs[0] = ih;
    500  1.4.2.1  pgoyette 	psc->psc_nintrs = 1;
    501  1.4.2.1  pgoyette 	sc->sc_use_mq = 0;
    502  1.4.2.1  pgoyette 	sc->sc_nq = 1;
    503  1.4.2.1  pgoyette #endif /* __HAVE_PCI_MSI_MSIX */
    504  1.4.2.1  pgoyette 
    505      1.1    nonaka 	return 0;
    506      1.1    nonaka }
    507  1.4.2.1  pgoyette 
    508  1.4.2.1  pgoyette MODULE(MODULE_CLASS_DRIVER, nvme, "pci,dk_subr");
    509  1.4.2.1  pgoyette 
    510  1.4.2.1  pgoyette #ifdef _MODULE
    511  1.4.2.1  pgoyette #include "ioconf.c"
    512  1.4.2.1  pgoyette #endif
    513  1.4.2.1  pgoyette 
    514  1.4.2.1  pgoyette static int
    515  1.4.2.1  pgoyette nvme_modcmd(modcmd_t cmd, void *opaque)
    516  1.4.2.1  pgoyette {
    517  1.4.2.1  pgoyette #ifdef _MODULE
    518  1.4.2.1  pgoyette 	devmajor_t cmajor, bmajor;
    519  1.4.2.1  pgoyette 	extern const struct cdevsw nvme_cdevsw;
    520  1.4.2.1  pgoyette #endif
    521  1.4.2.1  pgoyette 	int error = 0;
    522  1.4.2.1  pgoyette 
    523  1.4.2.1  pgoyette #ifdef _MODULE
    524  1.4.2.1  pgoyette 	switch (cmd) {
    525  1.4.2.1  pgoyette 	case MODULE_CMD_INIT:
    526  1.4.2.1  pgoyette 		error = config_init_component(cfdriver_ioconf_nvme_pci,
    527  1.4.2.1  pgoyette 		    cfattach_ioconf_nvme_pci, cfdata_ioconf_nvme_pci);
    528  1.4.2.1  pgoyette 		if (error)
    529  1.4.2.1  pgoyette 			break;
    530  1.4.2.1  pgoyette 
    531  1.4.2.1  pgoyette 		bmajor = cmajor = NODEVMAJOR;
    532  1.4.2.1  pgoyette 		error = devsw_attach(nvme_cd.cd_name, NULL, &bmajor,
    533  1.4.2.1  pgoyette 		    &nvme_cdevsw, &cmajor);
    534  1.4.2.1  pgoyette 		if (error) {
    535  1.4.2.1  pgoyette 			aprint_error("%s: unable to register devsw\n",
    536  1.4.2.1  pgoyette 			    nvme_cd.cd_name);
    537  1.4.2.1  pgoyette 			/* do not abort, just /dev/nvme* will not work */
    538  1.4.2.1  pgoyette 		}
    539  1.4.2.1  pgoyette 		break;
    540  1.4.2.1  pgoyette 	case MODULE_CMD_FINI:
    541  1.4.2.1  pgoyette 		devsw_detach(NULL, &nvme_cdevsw);
    542  1.4.2.1  pgoyette 
    543  1.4.2.1  pgoyette 		error = config_fini_component(cfdriver_ioconf_nvme_pci,
    544  1.4.2.1  pgoyette 		    cfattach_ioconf_nvme_pci, cfdata_ioconf_nvme_pci);
    545  1.4.2.1  pgoyette 		break;
    546  1.4.2.1  pgoyette 	default:
    547  1.4.2.1  pgoyette 		break;
    548  1.4.2.1  pgoyette 	}
    549  1.4.2.1  pgoyette #endif
    550  1.4.2.1  pgoyette 	return error;
    551  1.4.2.1  pgoyette }
    552