mfi_pci.c revision 1.1.2.3 1 1.1.2.3 yamt /* $NetBSD: mfi_pci.c,v 1.1.2.3 2006/12/21 15:07:59 yamt Exp $ */
2 1.1.2.2 yamt /* $OpenBSD: mfi_pci.c,v 1.11 2006/08/06 04:40:08 brad Exp $ */
3 1.1.2.2 yamt /*
4 1.1.2.2 yamt * Copyright (c) 2006 Marco Peereboom <marco (at) peereboom.us>
5 1.1.2.2 yamt *
6 1.1.2.2 yamt * Permission to use, copy, modify, and distribute this software for any
7 1.1.2.2 yamt * purpose with or without fee is hereby granted, provided that the above
8 1.1.2.2 yamt * copyright notice and this permission notice appear in all copies.
9 1.1.2.2 yamt *
10 1.1.2.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1.2.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1.2.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1.2.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1.2.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1.2.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1.2.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1.2.2 yamt */
18 1.1.2.2 yamt
19 1.1.2.3 yamt #include <sys/cdefs.h>
20 1.1.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: mfi_pci.c,v 1.1.2.3 2006/12/21 15:07:59 yamt Exp $");
21 1.1.2.3 yamt
22 1.1.2.2 yamt #include <sys/param.h>
23 1.1.2.2 yamt #include <sys/systm.h>
24 1.1.2.2 yamt #include <sys/kernel.h>
25 1.1.2.2 yamt #include <sys/malloc.h>
26 1.1.2.2 yamt #include <sys/device.h>
27 1.1.2.2 yamt
28 1.1.2.2 yamt #include <dev/pci/pcidevs.h>
29 1.1.2.2 yamt #include <dev/pci/pcivar.h>
30 1.1.2.2 yamt
31 1.1.2.2 yamt #include <machine/bus.h>
32 1.1.2.2 yamt
33 1.1.2.2 yamt #include <dev/scsipi/scsipi_all.h>
34 1.1.2.2 yamt #include <dev/scsipi/scsipi_disk.h>
35 1.1.2.2 yamt #include <dev/scsipi/scsiconf.h>
36 1.1.2.2 yamt
37 1.1.2.2 yamt #include <dev/ic/mfireg.h>
38 1.1.2.2 yamt #include <dev/ic/mfivar.h>
39 1.1.2.2 yamt
40 1.1.2.2 yamt #define MFI_BAR 0x10
41 1.1.2.2 yamt #define MFI_PCI_MEMSIZE 0x2000 /* 8k */
42 1.1.2.2 yamt
43 1.1.2.2 yamt int mfi_pci_find_device(void *);
44 1.1.2.2 yamt int mfi_pci_match(struct device *, struct cfdata *, void *);
45 1.1.2.2 yamt void mfi_pci_attach(struct device *, struct device *, void *);
46 1.1.2.2 yamt
47 1.1.2.2 yamt CFATTACH_DECL(mfi_pci, sizeof(struct mfi_softc),
48 1.1.2.2 yamt mfi_pci_match, mfi_pci_attach, NULL, NULL);
49 1.1.2.2 yamt
50 1.1.2.2 yamt struct mfi_pci_device {
51 1.1.2.2 yamt pcireg_t mpd_vendor;
52 1.1.2.2 yamt pcireg_t mpd_product;
53 1.1.2.2 yamt pcireg_t mpd_subvendor;
54 1.1.2.2 yamt pcireg_t mpd_subproduct;
55 1.1.2.2 yamt const char *mpd_model;
56 1.1.2.2 yamt uint32_t mpd_flags;
57 1.1.2.2 yamt };
58 1.1.2.2 yamt
59 1.1.2.2 yamt static const
60 1.1.2.2 yamt struct mfi_pci_device mfi_pci_devices[] = {
61 1.1.2.2 yamt { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
62 1.1.2.2 yamt 0, 0, "", 0 },
63 1.1.2.2 yamt { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
64 1.1.2.2 yamt 0, 0, "", 0 },
65 1.1.2.2 yamt { PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_5,
66 1.1.2.2 yamt PCI_VENDOR_DELL, 0x1f01, "Dell PERC 5/e", 0 },
67 1.1.2.2 yamt { PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_5,
68 1.1.2.2 yamt PCI_VENDOR_DELL, 0x1f02, "Dell PERC 5/i", 0 },
69 1.1.2.2 yamt { 0, 0,
70 1.1.2.2 yamt 0, 0, NULL, 0}
71 1.1.2.2 yamt };
72 1.1.2.2 yamt
73 1.1.2.2 yamt int
74 1.1.2.2 yamt mfi_pci_find_device(void *aux) {
75 1.1.2.2 yamt struct pci_attach_args *pa = aux;
76 1.1.2.2 yamt int i;
77 1.1.2.2 yamt
78 1.1.2.2 yamt for (i = 0; mfi_pci_devices[i].mpd_vendor; i++) {
79 1.1.2.2 yamt if (mfi_pci_devices[i].mpd_vendor == PCI_VENDOR(pa->pa_id) &&
80 1.1.2.2 yamt mfi_pci_devices[i].mpd_product == PCI_PRODUCT(pa->pa_id)) {
81 1.1.2.2 yamt DNPRINTF(MFI_D_MISC, "mfi_pci_find_device: %i\n", i);
82 1.1.2.2 yamt return (i);
83 1.1.2.2 yamt }
84 1.1.2.2 yamt }
85 1.1.2.2 yamt
86 1.1.2.2 yamt return (-1);
87 1.1.2.2 yamt }
88 1.1.2.2 yamt
89 1.1.2.2 yamt int
90 1.1.2.2 yamt mfi_pci_match(struct device *parent, struct cfdata *match, void *aux)
91 1.1.2.2 yamt {
92 1.1.2.2 yamt int i;
93 1.1.2.2 yamt
94 1.1.2.2 yamt if ((i = mfi_pci_find_device(aux)) != -1) {
95 1.1.2.2 yamt DNPRINTF(MFI_D_MISC,
96 1.1.2.2 yamt "mfi_pci_match: vendor: %04x product: %04x\n",
97 1.1.2.2 yamt mfi_pci_devices[i].mpd_vendor,
98 1.1.2.2 yamt mfi_pci_devices[i].mpd_product);
99 1.1.2.2 yamt
100 1.1.2.2 yamt return (1);
101 1.1.2.2 yamt }
102 1.1.2.2 yamt
103 1.1.2.2 yamt return (0);
104 1.1.2.2 yamt }
105 1.1.2.2 yamt
106 1.1.2.2 yamt void
107 1.1.2.2 yamt mfi_pci_attach(struct device *parent, struct device *self, void *aux)
108 1.1.2.2 yamt {
109 1.1.2.2 yamt struct mfi_softc *sc = (struct mfi_softc *)self;
110 1.1.2.2 yamt struct pci_attach_args *pa = aux;
111 1.1.2.2 yamt const char *intrstr;
112 1.1.2.2 yamt pci_intr_handle_t ih;
113 1.1.2.2 yamt bus_size_t size;
114 1.1.2.2 yamt pcireg_t csr;
115 1.1.2.2 yamt uint32_t subsysid, i;
116 1.1.2.2 yamt
117 1.1.2.2 yamt subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
118 1.1.2.2 yamt for (i = 0; mfi_pci_devices[i].mpd_vendor; i++)
119 1.1.2.2 yamt if (mfi_pci_devices[i].mpd_subvendor == PCI_VENDOR(subsysid) &&
120 1.1.2.2 yamt mfi_pci_devices[i].mpd_subproduct == PCI_PRODUCT(subsysid)){
121 1.1.2.2 yamt aprint_normal(", %s",
122 1.1.2.2 yamt mfi_pci_devices[i].mpd_model);
123 1.1.2.2 yamt break;
124 1.1.2.2 yamt }
125 1.1.2.2 yamt
126 1.1.2.2 yamt csr = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MFI_BAR);
127 1.1.2.2 yamt csr |= PCI_MAPREG_MEM_TYPE_32BIT;
128 1.1.2.2 yamt if (pci_mapreg_map(pa, MFI_BAR, csr, 0,
129 1.1.2.2 yamt &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
130 1.1.2.2 yamt aprint_error(": can't map controller pci space\n");
131 1.1.2.2 yamt return;
132 1.1.2.2 yamt }
133 1.1.2.2 yamt
134 1.1.2.2 yamt sc->sc_dmat = pa->pa_dmat;
135 1.1.2.2 yamt
136 1.1.2.2 yamt if (pci_intr_map(pa, &ih)) {
137 1.1.2.2 yamt aprint_error(": can't map interrupt\n");
138 1.1.2.2 yamt bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
139 1.1.2.2 yamt return;
140 1.1.2.2 yamt }
141 1.1.2.2 yamt intrstr = pci_intr_string(pa->pa_pc, ih);
142 1.1.2.2 yamt sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mfi_intr, sc);
143 1.1.2.2 yamt if (!sc->sc_ih) {
144 1.1.2.2 yamt aprint_error(": can't establish interrupt");
145 1.1.2.2 yamt if (intrstr)
146 1.1.2.2 yamt aprint_error(" at %s", intrstr);
147 1.1.2.2 yamt aprint_error("\n");
148 1.1.2.2 yamt bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
149 1.1.2.2 yamt return;
150 1.1.2.2 yamt }
151 1.1.2.2 yamt
152 1.1.2.2 yamt aprint_normal(": %s\n", intrstr);
153 1.1.2.2 yamt
154 1.1.2.2 yamt if (mfi_attach(sc)) {
155 1.1.2.2 yamt aprint_error("%s: can't attach", DEVNAME(sc));
156 1.1.2.2 yamt pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
157 1.1.2.2 yamt sc->sc_ih = NULL;
158 1.1.2.2 yamt bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
159 1.1.2.2 yamt }
160 1.1.2.2 yamt }
161