mfi_pci.c revision 1.20
1/* $OpenBSD: mfi_pci.c,v 1.20 2009/02/10 23:21:44 marco 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
27#include <dev/pci/pcidevs.h>
28#include <dev/pci/pcivar.h>
29
30#include <machine/bus.h>
31
32#include <scsi/scsi_all.h>
33#include <scsi/scsi_disk.h>
34#include <scsi/scsiconf.h>
35
36#include <dev/ic/mfireg.h>
37#include <dev/ic/mfivar.h>
38
39#define	MFI_BAR		0x10
40#define	MFI_BAR_GEN2	0x14
41#define	MFI_PCI_MEMSIZE	0x2000 /* 8k */
42
43int	mfi_pci_match(struct device *, void *, void *);
44void	mfi_pci_attach(struct device *, struct device *, void *);
45
46struct cfattach mfi_pci_ca = {
47	sizeof(struct mfi_softc), mfi_pci_match, mfi_pci_attach
48};
49
50struct mfi_pci_subtype {
51	pcireg_t			st_id;
52	const char			*st_string;
53};
54
55static const struct mfi_pci_subtype mfi_1078_subtypes[] = {
56	{ 0x10061000,	"SAS 8888ELP" },
57	{ 0x100a1000,	"SAS 8708ELP" },
58	{ 0x100f1000,	"SAS 8708E" },
59	{ 0x10121000,	"SAS 8704ELP" },
60	{ 0x10131000,	"SAS 8708EM2" },
61	{ 0x10161000,	"SAS 8880EM2" },
62	{ 0x1f0a1028,	"Dell PERC 6/e" },
63	{ 0x1f0b1028,	"Dell PERC 6/i" },
64	{ 0x1f0d1028,	"Dell CERC 6/i" },
65	{ 0x1f0c1028,	"Dell PERC 6/i integrated" },
66	{ 0x1f111028,	"Dell CERC 6/i integrated" },
67	{ 0x0,		"" }
68};
69
70static const struct mfi_pci_subtype mfi_perc5_subtypes[] = {
71	{ 0x1f011028,	"Dell PERC 5/e" },
72	{ 0x1f021028,	"Dell PERC 5/i" },
73	{ 0x0,		"" }
74};
75
76static const struct mfi_pci_subtype mfi_gen2_subtypes[] = {
77	{ 0x0,		"" } /* XXX add entries when known */
78};
79
80static const
81struct	mfi_pci_device {
82	pcireg_t			mpd_vendor;
83	pcireg_t			mpd_product;
84	enum mfi_iop			mpd_iop;
85	const struct mfi_pci_subtype	*mpd_subtype;
86} mfi_pci_devices[] = {
87	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
88	  MFI_IOP_XSCALE,	NULL },
89	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
90	  MFI_IOP_XSCALE,	NULL },
91	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1078,
92	  MFI_IOP_PPC,		mfi_1078_subtypes },
93	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC5,
94	  MFI_IOP_XSCALE,	mfi_perc5_subtypes },
95	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_DELL_PERC6,
96	  MFI_IOP_PPC,		mfi_1078_subtypes },
97	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_1,
98	  MFI_IOP_GEN2,		mfi_gen2_subtypes },
99	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_2,
100	  MFI_IOP_GEN2,		mfi_gen2_subtypes },
101};
102
103const struct mfi_pci_device *mfi_pci_find_device(struct pci_attach_args *);
104
105const struct mfi_pci_device *
106mfi_pci_find_device(struct pci_attach_args *pa)
107{
108	const struct mfi_pci_device *mpd;
109	int i;
110
111	for (i = 0; i < nitems(mfi_pci_devices); i++) {
112		mpd = &mfi_pci_devices[i];
113
114		if (mpd->mpd_vendor == PCI_VENDOR(pa->pa_id) &&
115		    mpd->mpd_product == PCI_PRODUCT(pa->pa_id))
116			return (mpd);
117	}
118
119	return (NULL);
120}
121
122int
123mfi_pci_match(struct device *parent, void *match, void *aux)
124{
125	return ((mfi_pci_find_device(aux) != NULL) ? 1 : 0);
126}
127
128void
129mfi_pci_attach(struct device *parent, struct device *self, void *aux)
130{
131	struct mfi_softc	*sc = (struct mfi_softc *)self;
132	struct pci_attach_args	*pa = aux;
133	const struct mfi_pci_device *mpd;
134	const struct mfi_pci_subtype *st;
135	const char		*intrstr;
136	pci_intr_handle_t	ih;
137	bus_size_t		size;
138	pcireg_t		reg;
139	int			regbar;
140	const char		*subtype = NULL;
141	char			subid[32];
142
143	mpd = mfi_pci_find_device(pa);
144	if (mpd == NULL) {
145		printf(": can't find matching pci device\n");
146		return;
147	}
148
149	if (mpd->mpd_iop == MFI_IOP_GEN2)
150		regbar = MFI_BAR_GEN2;
151	else
152		regbar = MFI_BAR;
153
154	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, regbar);
155	if (pci_mapreg_map(pa, regbar, reg, 0,
156	    &sc->sc_iot, &sc->sc_ioh, NULL, &size, MFI_PCI_MEMSIZE)) {
157		printf(": can't map controller pci space\n");
158		return;
159	}
160
161	sc->sc_dmat = pa->pa_dmat;
162
163	if (pci_intr_map(pa, &ih)) {
164		printf(": can't map interrupt\n");
165		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
166		return;
167	}
168	intrstr = pci_intr_string(pa->pa_pc, ih);
169	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mfi_intr, sc,
170	    sc->sc_dev.dv_xname);
171	if (!sc->sc_ih) {
172		printf(": can't establish interrupt");
173		if (intrstr)
174			printf(" at %s", intrstr);
175		printf("\n");
176		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
177		return;
178	}
179
180	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
181
182	if (mpd->mpd_subtype != NULL) {
183		st = mpd->mpd_subtype;
184		while (st->st_id != 0x0) {
185			if (st->st_id == reg) {
186				subtype = st->st_string;
187				break;
188			}
189			st++;
190		}
191	}
192	if (subtype == NULL) {
193		snprintf(subid, sizeof(subid), "0x%08x", reg);
194		subtype = subid;
195	}
196
197	printf(": %s, %s\n", intrstr, subtype);
198
199	if (mfi_attach(sc, mpd->mpd_iop)) {
200		printf("%s: can't attach\n", DEVNAME(sc));
201		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
202		sc->sc_ih = NULL;
203		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
204	}
205}
206