xref: /openbsd-src/sys/dev/pci/nvme_pci.c (revision ef4d1ee358f26ef117f25eba5737d7d2be6eaa9a)
1 /*	$OpenBSD: nvme_pci.c,v 1.13 2024/11/19 02:31:35 jcs Exp $ */
2 
3 /*
4  * Copyright (c) 2014 David Gwynne <dlg@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/device.h>
22 #include <sys/queue.h>
23 #include <sys/mutex.h>
24 
25 #include <machine/bus.h>
26 
27 #include <dev/pci/pcireg.h>
28 #include <dev/pci/pcivar.h>
29 #include <dev/pci/pcidevs.h>
30 
31 #include <scsi/scsi_all.h>
32 #include <scsi/scsiconf.h>
33 
34 #include <dev/ic/nvmereg.h>
35 #include <dev/ic/nvmevar.h>
36 
37 #define NVME_PCI_BAR		0x10
38 #define NVME_PCI_INTERFACE	0x02
39 
40 struct nvme_pci_softc {
41 	struct nvme_softc	psc_nvme;
42 	pci_chipset_tag_t	psc_pc;
43 };
44 
45 int	nvme_pci_match(struct device *, void *, void *);
46 void	nvme_pci_attach(struct device *, struct device *, void *);
47 int	nvme_pci_detach(struct device *, int);
48 int	nvme_pci_activate(struct device *, int);
49 
50 const struct cfattach nvme_pci_ca = {
51 	sizeof(struct nvme_pci_softc),
52 	nvme_pci_match,
53 	nvme_pci_attach,
54 	nvme_pci_detach,
55 	nvme_pci_activate
56 };
57 
58 int
59 nvme_pci_match(struct device *parent, void *match, void *aux)
60 {
61 	struct pci_attach_args *pa = aux;
62 
63 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
64 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_NVM &&
65 	    PCI_INTERFACE(pa->pa_class) == NVME_PCI_INTERFACE)
66 		return (1);
67 
68 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
69 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_NVME1 ||
70 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_NVME2))
71 	    	return (1);
72 
73 	return (0);
74 }
75 
76 void
77 nvme_pci_attach(struct device *parent, struct device *self, void *aux)
78 {
79 	struct nvme_pci_softc *psc = (struct nvme_pci_softc *)self;
80 	struct nvme_softc *sc = &psc->psc_nvme;
81 	struct pci_attach_args *pa = aux;
82 	pcireg_t maptype;
83 	pci_intr_handle_t ih;
84 	int msi = 1;
85 
86 	psc->psc_pc = pa->pa_pc;
87 	sc->sc_dmat = pa->pa_dmat;
88 
89 	printf(": ");
90 
91 	maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, NVME_PCI_BAR);
92 	if (pci_mapreg_map(pa, NVME_PCI_BAR, maptype, 0,
93 	    &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_ios, 0) != 0) {
94 		printf("unable to map registers\n");
95 		return;
96 	}
97 
98 	if (pci_intr_map_msix(pa, 0, &ih) != 0 &&
99 	    pci_intr_map_msi(pa, &ih) != 0) {
100 		if (pci_intr_map(pa, &ih) != 0) {
101 			printf("unable to map interrupt\n");
102 			goto unmap;
103 		}
104 		msi = 0;
105 	}
106 
107 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
108 	    msi ? nvme_intr : nvme_intr_intx, sc, DEVNAME(sc));
109 	if (sc->sc_ih == NULL) {
110 		printf("unable to establish interrupt\n");
111 		goto unmap;
112 	}
113 
114 	printf("%s, ", pci_intr_string(pa->pa_pc, ih));
115 	if (nvme_attach(sc) != 0) {
116 		/* error printed by nvme_attach() */
117 		goto disestablish;
118 	}
119 
120 	return;
121 
122 disestablish:
123 	pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
124 	sc->sc_ih = NULL;
125 
126 unmap:
127 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
128 	sc->sc_ios = 0;
129 }
130 
131 int
132 nvme_pci_detach(struct device *self, int flags)
133 {
134 	return config_detach_children(self, flags);
135 }
136 
137 int
138 nvme_pci_activate(struct device *self, int act)
139 {
140 	struct nvme_pci_softc *psc = (struct nvme_pci_softc *)self;
141 
142 	return (nvme_activate(&psc->psc_nvme, act));
143 }
144