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