xref: /openbsd/sys/dev/pci/mfi_pci.c (revision 905646f0)
1 /* $OpenBSD: mfi_pci.c,v 1.30 2019/12/31 00:00:09 deraadt Exp $ */
2 /*
3  * Copyright (c) 2006 Marco Peereboom <marco@peereboom.us>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "bio.h"
19 
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
23 #include <sys/malloc.h>
24 #include <sys/device.h>
25 #include <sys/rwlock.h>
26 #include <sys/sensors.h>
27 
28 #include <dev/pci/pcidevs.h>
29 #include <dev/pci/pcivar.h>
30 
31 #include <machine/bus.h>
32 
33 #include <scsi/scsi_all.h>
34 #include <scsi/scsi_disk.h>
35 #include <scsi/scsiconf.h>
36 
37 #include <dev/ic/mfireg.h>
38 #include <dev/ic/mfivar.h>
39 
40 #define	MFI_BAR		0x10
41 #define	MFI_BAR_GEN2	0x14
42 #define	MFI_PCI_MEMSIZE	0x2000 /* 8k */
43 
44 int	mfi_pci_match(struct device *, void *, void *);
45 void	mfi_pci_attach(struct device *, struct device *, void *);
46 
47 struct cfattach mfi_pci_ca = {
48 	sizeof(struct mfi_softc), mfi_pci_match, mfi_pci_attach
49 };
50 
51 static const
52 struct	mfi_pci_device {
53 	pcireg_t		mpd_vendor;
54 	pcireg_t		mpd_product;
55 	enum mfi_iop		mpd_iop;
56 } mfi_pci_devices[] = {
57 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
58 	  MFI_IOP_XSCALE },
59 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
60 	  MFI_IOP_XSCALE },
61 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1078,
62 	  MFI_IOP_PPC },
63 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1078DE,
64 	  MFI_IOP_PPC },
65 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC5,
66 	  MFI_IOP_XSCALE },
67 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_1,
68 	  MFI_IOP_GEN2 },
69 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_2,
70 	  MFI_IOP_GEN2 },
71 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2008_1,
72 	  MFI_IOP_SKINNY }
73 };
74 
75 const struct mfi_pci_device *mfi_pci_find_device(struct pci_attach_args *);
76 
77 const struct mfi_pci_device *
78 mfi_pci_find_device(struct pci_attach_args *pa)
79 {
80 	const struct mfi_pci_device *mpd;
81 	int i;
82 
83 	for (i = 0; i < nitems(mfi_pci_devices); i++) {
84 		mpd = &mfi_pci_devices[i];
85 
86 		if (mpd->mpd_vendor == PCI_VENDOR(pa->pa_id) &&
87 		    mpd->mpd_product == PCI_PRODUCT(pa->pa_id))
88 			return (mpd);
89 	}
90 
91 	return (NULL);
92 }
93 
94 int
95 mfi_pci_match(struct device *parent, void *match, void *aux)
96 {
97 	return ((mfi_pci_find_device(aux) != NULL) ? 1 : 0);
98 }
99 
100 void
101 mfi_pci_attach(struct device *parent, struct device *self, void *aux)
102 {
103 	struct mfi_softc	*sc = (struct mfi_softc *)self;
104 	struct pci_attach_args	*pa = aux;
105 	const struct mfi_pci_device *mpd;
106 	pci_intr_handle_t	ih;
107 	bus_size_t		size;
108 	pcireg_t		reg;
109 	int			regbar;
110 
111 	mpd = mfi_pci_find_device(pa);
112 	if (mpd == NULL) {
113 		printf(": can't find matching pci device\n");
114 		return;
115 	}
116 
117 	if (mpd->mpd_iop == MFI_IOP_GEN2 || mpd->mpd_iop == MFI_IOP_SKINNY)
118 		regbar = MFI_BAR_GEN2;
119 	else
120 		regbar = MFI_BAR;
121 
122 	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, regbar);
123 	if (pci_mapreg_map(pa, regbar, reg, 0,
124 	    &sc->sc_iot, &sc->sc_ioh, NULL, &size, MFI_PCI_MEMSIZE)) {
125 		printf(": can't map controller pci space\n");
126 		return;
127 	}
128 
129 	sc->sc_dmat = pa->pa_dmat;
130 
131 	if (pci_intr_map(pa, &ih) != 0) {
132 		printf(": can't map interrupt\n");
133 		goto unmap;
134 	}
135 	printf(": %s\n", pci_intr_string(pa->pa_pc, ih));
136 
137 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO | IPL_MPSAFE,
138 	    mfi_intr, sc, sc->sc_dev.dv_xname);
139 	if (!sc->sc_ih) {
140 		printf("%s: can't establish interrupt\n", DEVNAME(sc));
141 		goto unmap;
142 	}
143 
144 	if (mfi_attach(sc, mpd->mpd_iop)) {
145 		printf("%s: can't attach\n", DEVNAME(sc));
146 		goto unintr;
147 	}
148 
149 	return;
150 unintr:
151 	pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
152 	sc->sc_ih = NULL;
153 unmap:
154 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
155 }
156