ahcisata_pci.c revision 1.27.2.1 1 /* $NetBSD: ahcisata_pci.c,v 1.27.2.1 2012/11/18 18:06:00 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 2006 Manuel Bouyer.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ahcisata_pci.c,v 1.27.2.1 2012/11/18 18:06:00 msaitoh Exp $");
30
31 #include <sys/types.h>
32 #include <sys/malloc.h>
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/disklabel.h>
37 #include <sys/pmf.h>
38
39 #include <dev/pci/pcivar.h>
40 #include <dev/pci/pcidevs.h>
41 #include <dev/pci/pciidereg.h>
42 #include <dev/pci/pciidevar.h>
43 #include <dev/ic/ahcisatavar.h>
44
45 struct ahci_pci_quirk {
46 pci_vendor_id_t vendor; /* Vendor ID */
47 pci_product_id_t product; /* Product ID */
48 int quirks; /* quirks; see below */
49 };
50
51 #define AHCI_PCI_QUIRK_FORCE __BIT(0) /* force attach */
52 #define AHCI_PCI_QUIRK_BAD64 __BIT(1) /* broken 64-bit DMA */
53
54 static const struct ahci_pci_quirk ahci_pci_quirks[] = {
55 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA,
56 AHCI_PCI_QUIRK_FORCE },
57 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA2,
58 AHCI_PCI_QUIRK_FORCE },
59 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA3,
60 AHCI_PCI_QUIRK_FORCE },
61 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA4,
62 AHCI_PCI_QUIRK_FORCE },
63 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_SATA,
64 AHCI_PCI_QUIRK_FORCE },
65 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_AHCI_1,
66 AHCI_PCI_QUIRK_FORCE },
67 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_1,
68 AHCI_PCI_QUIRK_FORCE },
69 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_2,
70 AHCI_PCI_QUIRK_FORCE },
71 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_3,
72 AHCI_PCI_QUIRK_FORCE },
73 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_4,
74 AHCI_PCI_QUIRK_FORCE },
75 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_5,
76 AHCI_PCI_QUIRK_FORCE },
77 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_6,
78 AHCI_PCI_QUIRK_FORCE },
79 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_7,
80 AHCI_PCI_QUIRK_FORCE },
81 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_8,
82 AHCI_PCI_QUIRK_FORCE },
83 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_9,
84 AHCI_PCI_QUIRK_FORCE },
85 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_10,
86 AHCI_PCI_QUIRK_FORCE },
87 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_11,
88 AHCI_PCI_QUIRK_FORCE },
89 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_12,
90 AHCI_PCI_QUIRK_FORCE },
91 { PCI_VENDOR_ALI, PCI_PRODUCT_ALI_M5288,
92 AHCI_PCI_QUIRK_FORCE },
93 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88SE6121,
94 AHCI_PCI_QUIRK_FORCE },
95 { PCI_VENDOR_MARVELL2, PCI_PRODUCT_MARVELL2_88SE9128,
96 AHCI_PCI_QUIRK_FORCE },
97 /* ATI SB600 AHCI 64-bit DMA only works on some boards/BIOSes */
98 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB600_SATA_1,
99 AHCI_PCI_QUIRK_BAD64 },
100 { PCI_VENDOR_ASMEDIA, PCI_PRODUCT_ASMEDIA_ASM1061_01,
101 AHCI_PCI_QUIRK_FORCE },
102 { PCI_VENDOR_ASMEDIA, PCI_PRODUCT_ASMEDIA_ASM1061_02,
103 AHCI_PCI_QUIRK_FORCE },
104 { PCI_VENDOR_ASMEDIA, PCI_PRODUCT_ASMEDIA_ASM1061_11,
105 AHCI_PCI_QUIRK_FORCE },
106 { PCI_VENDOR_ASMEDIA, PCI_PRODUCT_ASMEDIA_ASM1061_12,
107 AHCI_PCI_QUIRK_FORCE },
108 };
109
110 struct ahci_pci_softc {
111 struct ahci_softc ah_sc;
112 pci_chipset_tag_t sc_pc;
113 pcitag_t sc_pcitag;
114 void * sc_ih;
115 };
116
117 static bool ahci_pci_has_quirk(pci_vendor_id_t, pci_product_id_t, int);
118 static int ahci_pci_match(device_t, cfdata_t, void *);
119 static void ahci_pci_attach(device_t, device_t, void *);
120 static int ahci_pci_detach(device_t, int);
121 static bool ahci_pci_resume(device_t, const pmf_qual_t *);
122
123
124 CFATTACH_DECL_NEW(ahcisata_pci, sizeof(struct ahci_pci_softc),
125 ahci_pci_match, ahci_pci_attach, ahci_pci_detach, NULL);
126
127 static bool
128 ahci_pci_has_quirk(pci_vendor_id_t vendor, pci_product_id_t product, int quirk)
129 {
130 int i;
131
132 for (i = 0; i < __arraycount(ahci_pci_quirks); i++)
133 if (vendor == ahci_pci_quirks[i].vendor &&
134 product == ahci_pci_quirks[i].product)
135 return (ahci_pci_quirks[i].quirks & quirk) != 0;
136 return false;
137 }
138
139 static int
140 ahci_pci_match(device_t parent, cfdata_t match, void *aux)
141 {
142 struct pci_attach_args *pa = aux;
143 bus_space_tag_t regt;
144 bus_space_handle_t regh;
145 bus_size_t size;
146 int ret = 0;
147 bool force;
148
149 force = ahci_pci_has_quirk(PCI_VENDOR(pa->pa_id),
150 PCI_PRODUCT(pa->pa_id),
151 AHCI_PCI_QUIRK_FORCE);
152
153 /* if wrong class and not forced by quirks, don't match */
154 if ((PCI_CLASS(pa->pa_class) != PCI_CLASS_MASS_STORAGE ||
155 ((PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_SATA ||
156 PCI_INTERFACE(pa->pa_class) != PCI_INTERFACE_SATA_AHCI) &&
157 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_RAID)) &&
158 (force == false))
159 return 0;
160
161 if (pci_mapreg_map(pa, AHCI_PCI_ABAR,
162 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
163 ®t, ®h, NULL, &size) != 0)
164 return 0;
165
166 if ((PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_SATA &&
167 PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_SATA_AHCI) ||
168 (bus_space_read_4(regt, regh, AHCI_GHC) & AHCI_GHC_AE) ||
169 (force == true))
170 ret = 3;
171
172 bus_space_unmap(regt, regh, size);
173 return ret;
174 }
175
176 static void
177 ahci_pci_attach(device_t parent, device_t self, void *aux)
178 {
179 struct pci_attach_args *pa = aux;
180 struct ahci_pci_softc *psc = device_private(self);
181 struct ahci_softc *sc = &psc->ah_sc;
182 const char *intrstr;
183 bool ahci_cap_64bit;
184 bool ahci_bad_64bit;
185 pci_intr_handle_t intrhandle;
186
187 sc->sc_atac.atac_dev = self;
188
189 if (pci_mapreg_map(pa, AHCI_PCI_ABAR,
190 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
191 &sc->sc_ahcit, &sc->sc_ahcih, NULL, &sc->sc_ahcis) != 0) {
192 aprint_error_dev(self, "can't map ahci registers\n");
193 return;
194 }
195 psc->sc_pc = pa->pa_pc;
196 psc->sc_pcitag = pa->pa_tag;
197
198 pci_aprint_devinfo(pa, "AHCI disk controller");
199
200 if (pci_intr_map(pa, &intrhandle) != 0) {
201 aprint_error_dev(self, "couldn't map interrupt\n");
202 return;
203 }
204 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
205 psc->sc_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO, ahci_intr, sc);
206 if (psc->sc_ih == NULL) {
207 aprint_error_dev(self, "couldn't establish interrupt\n");
208 return;
209 }
210 aprint_normal_dev(self, "interrupting at %s\n",
211 intrstr ? intrstr : "unknown interrupt");
212
213 sc->sc_dmat = pa->pa_dmat;
214
215 ahci_cap_64bit = (AHCI_READ(sc, AHCI_CAP) & AHCI_CAP_64BIT) != 0;
216 ahci_bad_64bit = ahci_pci_has_quirk(PCI_VENDOR(pa->pa_id),
217 PCI_PRODUCT(pa->pa_id),
218 AHCI_PCI_QUIRK_BAD64);
219
220 if (pci_dma64_available(pa) && ahci_cap_64bit) {
221 if (!ahci_bad_64bit)
222 sc->sc_dmat = pa->pa_dmat64;
223 aprint_verbose_dev(self, "64-bit DMA%s\n",
224 (sc->sc_dmat == pa->pa_dmat) ? " unavailable" : "");
225 }
226
227 if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID) {
228 AHCIDEBUG_PRINT(("%s: RAID mode\n", AHCINAME(sc)), DEBUG_PROBE);
229 sc->sc_atac_capflags = ATAC_CAP_RAID;
230 } else {
231 AHCIDEBUG_PRINT(("%s: SATA mode\n", AHCINAME(sc)), DEBUG_PROBE);
232 }
233
234 ahci_attach(sc);
235
236 if (!pmf_device_register(self, NULL, ahci_pci_resume))
237 aprint_error_dev(self, "couldn't establish power handler\n");
238 }
239
240 static int
241 ahci_pci_detach(device_t dv, int flags)
242 {
243 struct ahci_pci_softc *psc;
244 struct ahci_softc *sc;
245 int rv;
246
247 psc = device_private(dv);
248 sc = &psc->ah_sc;
249
250 if ((rv = ahci_detach(sc, flags)))
251 return rv;
252
253 pmf_device_deregister(dv);
254
255 if (psc->sc_ih != NULL)
256 pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
257
258 bus_space_unmap(sc->sc_ahcit, sc->sc_ahcih, sc->sc_ahcis);
259
260 return 0;
261 }
262
263 static bool
264 ahci_pci_resume(device_t dv, const pmf_qual_t *qual)
265 {
266 struct ahci_pci_softc *psc = device_private(dv);
267 struct ahci_softc *sc = &psc->ah_sc;
268 int s;
269
270 s = splbio();
271 ahci_resume(sc);
272 splx(s);
273
274 return true;
275 }
276