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