source: trunk/src/os2ahci/pci.c@ 188

Last change on this file since 188 was 188, checked in by David Azarewicz, 8 years ago

Fixed interrupt problem.

File size: 34.4 KB
Line 
1/******************************************************************************
2 * PCI.c - PCI constants and detection code for os2ahci driver
3 *
4 * Copyright (c) 2011 thi.guten Software Development
5 * Copyright (c) 2011 Mensys B.V.
6 * Copyright (c) 2013-2016 David Azarewicz
7 *
8 * Authors: Christian Mueller, Markus Thielen
9 *
10 * Parts copied from/inspired by the Linux AHCI driver;
11 * those parts are (c) Linux AHCI/ATA maintainers
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include "os2ahci.h"
29
30/* -------------------------- macros and constants ------------------------- */
31
32/* offset of PCI base address register (BAR) in the PCI config space */
33#define PCI_BAR(reg) (UCHAR) (0x10 + (reg) * sizeof(u32))
34
35/* ------------------------ typedefs and structures ------------------------ */
36
37/* -------------------------- function prototypes -------------------------- */
38
39static void add_pci_device(PCI_ID *pci_id, USHORT BusDevFunc);
40static long bar_resource(USHORT BusDevFunc, RESOURCESTRUCT *resource, int i);
41static char *rmerr(APIRET ret);
42
43/* ------------------------ global/static variables ------------------------ */
44
45/******************************************************************************
46 * chipset/controller name strings
47 */
48static char chip_esb2[] = "ESB2";
49static char chip_ich8[] = "ICH8";
50static char chip_ich8m[] = "ICH8M";
51static char chip_ich9[] = "ICH9";
52static char chip_ich9m[] = "ICH9M";
53static char chip_ich10[] = "ICH10";
54static char chip_pchahci[] = "PCH AHCI";
55static char chip_pchraid[] = "PCH RAID";
56static char chip_tolapai[] = "Tolapai";
57static char chip_sb600[] = "SB600";
58static char chip_sb700[] = "SB700/800";
59static char chip_vt8251[] = "VT8251";
60static char chip_mcp65[] = "MCP65";
61static char chip_mcp67[] = "MCP67";
62static char chip_mcp73[] = "MCP73";
63static char chip_mcp77[] = "MCP77";
64static char chip_mcp79[] = "MCP79";
65static char chip_mcp89[] = "MCP689";
66static char chip_sis968[] = "968";
67
68static char s_generic[] = "Generic";
69
70
71/******************************************************************************
72 * PCI vendor and device IDs for known AHCI adapters. Copied from the Linux
73 * AHCI driver.
74 */
75
76PCI_ID pci_ids[] =
77{
78 /* Intel
79 * NOTE: ICH5 controller does NOT support AHCI, so we do
80 * not add it here! */
81 { PCI_VDEVICE(INTEL, 0x2652), board_ahci, "ICH6" }, /* ICH6 */
82 { PCI_VDEVICE(INTEL, 0x2653), board_ahci, "ICH6M" }, /* ICH6M */
83 { PCI_VDEVICE(INTEL, 0x27c1), board_ahci, "ICH7" }, /* ICH7 */
84 { PCI_VDEVICE(INTEL, 0x27c5), board_ahci, "ICH7M" }, /* ICH7M */
85 { PCI_VDEVICE(INTEL, 0x27c3), board_ahci, "ICH7R" }, /* ICH7R */
86 { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr, "ULiM5288" }, /* ULi M5288 */
87 { PCI_VDEVICE(INTEL, 0x2681), board_ahci, chip_esb2 }, /* ESB2 */
88 { PCI_VDEVICE(INTEL, 0x2682), board_ahci, chip_esb2 }, /* ESB2 */
89 { PCI_VDEVICE(INTEL, 0x2683), board_ahci, chip_esb2 }, /* ESB2 */
90 { PCI_VDEVICE(INTEL, 0x27c6), board_ahci, "ICH7MDH" }, /* ICH7-M DH */
91 { PCI_VDEVICE(INTEL, 0x2821), board_ahci, chip_ich8 }, /* ICH8 */
92 { PCI_VDEVICE(INTEL, 0x2822), board_ahci_nosntf, chip_ich8 }, /* ICH8 */
93 { PCI_VDEVICE(INTEL, 0x2824), board_ahci, chip_ich8 }, /* ICH8 */
94 { PCI_VDEVICE(INTEL, 0x2829), board_ahci, chip_ich8m }, /* ICH8M */
95 { PCI_VDEVICE(INTEL, 0x282a), board_ahci, chip_ich8m }, /* ICH8M */
96 { PCI_VDEVICE(INTEL, 0x2922), board_ahci, chip_ich9 }, /* ICH9 */
97 { PCI_VDEVICE(INTEL, 0x2923), board_ahci, chip_ich9 }, /* ICH9 */
98 { PCI_VDEVICE(INTEL, 0x2924), board_ahci, chip_ich9 }, /* ICH9 */
99 { PCI_VDEVICE(INTEL, 0x2925), board_ahci, chip_ich9 }, /* ICH9 */
100 { PCI_VDEVICE(INTEL, 0x2927), board_ahci, chip_ich9 }, /* ICH9 */
101 { PCI_VDEVICE(INTEL, 0x2929), board_ahci, chip_ich9m }, /* ICH9M */
102 { PCI_VDEVICE(INTEL, 0x292a), board_ahci, chip_ich9m }, /* ICH9M */
103 { PCI_VDEVICE(INTEL, 0x292b), board_ahci, chip_ich9m }, /* ICH9M */
104 { PCI_VDEVICE(INTEL, 0x292c), board_ahci, chip_ich9m }, /* ICH9M */
105 { PCI_VDEVICE(INTEL, 0x292f), board_ahci, chip_ich9m }, /* ICH9M */
106 { PCI_VDEVICE(INTEL, 0x294d), board_ahci, chip_ich9 }, /* ICH9 */
107 { PCI_VDEVICE(INTEL, 0x294e), board_ahci, chip_ich9m }, /* ICH9M */
108 { PCI_VDEVICE(INTEL, 0x502a), board_ahci, chip_tolapai }, /* Tolapai */
109 { PCI_VDEVICE(INTEL, 0x502b), board_ahci, chip_tolapai }, /* Tolapai */
110 { PCI_VDEVICE(INTEL, 0x3a05), board_ahci, chip_ich10 }, /* ICH10 */
111 { PCI_VDEVICE(INTEL, 0x3a22), board_ahci, chip_ich10 }, /* ICH10 */
112 { PCI_VDEVICE(INTEL, 0x3a25), board_ahci, chip_ich10 }, /* ICH10 */
113 { PCI_VDEVICE(INTEL, 0x3b22), board_ahci, chip_pchahci }, /* PCH AHCI */
114 { PCI_VDEVICE(INTEL, 0x3b23), board_ahci, chip_pchahci }, /* PCH AHCI */
115 { PCI_VDEVICE(INTEL, 0x3b24), board_ahci, chip_pchraid }, /* PCH RAID */
116 { PCI_VDEVICE(INTEL, 0x3b25), board_ahci, chip_pchraid }, /* PCH RAID */
117 { PCI_VDEVICE(INTEL, 0x3b29), board_ahci, chip_pchahci }, /* PCH AHCI */
118 { PCI_VDEVICE(INTEL, 0x3b2b), board_ahci, chip_pchraid }, /* PCH RAID */
119 { PCI_VDEVICE(INTEL, 0x3b2c), board_ahci, chip_pchraid }, /* PCH RAID */
120 { PCI_VDEVICE(INTEL, 0x3b2f), board_ahci, chip_pchahci }, /* PCH AHCI */
121
122 /* JMicron 360/1/3/5/6, match class to avoid IDE function */
123 { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
124 PCI_CLASS_STORAGE_SATA_AHCI, 0xffffffL, board_ahci_ign_iferr, "360" },
125
126 /* ATI */
127 { PCI_VDEVICE(ATI, 0x4380), board_ahci_sb600, chip_sb600 }, /* ATI SB600 */
128 { PCI_VDEVICE(ATI, 0x4390), board_ahci_sb700, chip_sb700 }, /* ATI SB700/800 */
129 { PCI_VDEVICE(ATI, 0x4391), board_ahci_sb700, chip_sb700 }, /* ATI SB700/800 */
130 { PCI_VDEVICE(ATI, 0x4392), board_ahci_sb700, chip_sb700 }, /* ATI SB700/800 */
131 { PCI_VDEVICE(ATI, 0x4393), board_ahci_sb700, chip_sb700 }, /* ATI SB700/800 */
132 { PCI_VDEVICE(ATI, 0x4394), board_ahci_sb700, chip_sb700 }, /* ATI SB700/800 */
133 { PCI_VDEVICE(ATI, 0x4395), board_ahci_sb700, chip_sb700 }, /* ATI SB700/800 */
134
135 /* AMD */
136 { PCI_VDEVICE(AMD, 0x7800), board_ahci }, /* AMD Hudson-2 */
137 /* AMD is using RAID class only for ahci controllers */
138 { PCI_VENDOR_ID_AMD, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
139 PCI_CLASS_STORAGE_RAID << 8, 0xffffffL, board_ahci, "Hudson2" },
140
141 /* VIA */
142 { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251, chip_vt8251 }, /* VIA VT8251 */
143 { PCI_VDEVICE(VIA, 0x6287), board_ahci_vt8251, chip_vt8251 }, /* VIA VT8251 */
144
145 /* NVIDIA */
146 { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci_mcp65, chip_mcp65 }, /* MCP65 */
147 { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci_mcp65, chip_mcp65 }, /* MCP65 */
148 { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci_mcp65, chip_mcp65 }, /* MCP65 */
149 { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci_mcp65, chip_mcp65 }, /* MCP65 */
150 { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci_mcp65, chip_mcp65 }, /* MCP65 */
151 { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci_mcp65, chip_mcp65 }, /* MCP65 */
152 { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci_mcp65, chip_mcp65 }, /* MCP65 */
153 { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci_mcp65, chip_mcp65 }, /* MCP65 */
154 { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
155 { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
156 { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
157 { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
158 { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
159 { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
160 { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
161 { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
162 { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
163 { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
164 { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
165 { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci_yesncq, chip_mcp67 }, /* MCP67 */
166 { PCI_VDEVICE(NVIDIA, 0x0580), board_ahci_yesncq, chip_mcp67 }, /* Linux ID */
167 { PCI_VDEVICE(NVIDIA, 0x07f0), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
168 { PCI_VDEVICE(NVIDIA, 0x07f1), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
169 { PCI_VDEVICE(NVIDIA, 0x07f2), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
170 { PCI_VDEVICE(NVIDIA, 0x07f3), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
171 { PCI_VDEVICE(NVIDIA, 0x07f4), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
172 { PCI_VDEVICE(NVIDIA, 0x07f5), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
173 { PCI_VDEVICE(NVIDIA, 0x07f6), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
174 { PCI_VDEVICE(NVIDIA, 0x07f7), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
175 { PCI_VDEVICE(NVIDIA, 0x07f8), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
176 { PCI_VDEVICE(NVIDIA, 0x07f9), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
177 { PCI_VDEVICE(NVIDIA, 0x07fa), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
178 { PCI_VDEVICE(NVIDIA, 0x07fb), board_ahci_yesncq, chip_mcp73 }, /* MCP73 */
179 { PCI_VDEVICE(NVIDIA, 0x0ad0), board_ahci, chip_mcp77 }, /* MCP77 */
180 { PCI_VDEVICE(NVIDIA, 0x0ad1), board_ahci, chip_mcp77 }, /* MCP77 */
181 { PCI_VDEVICE(NVIDIA, 0x0ad2), board_ahci, chip_mcp77 }, /* MCP77 */
182 { PCI_VDEVICE(NVIDIA, 0x0ad3), board_ahci, chip_mcp77 }, /* MCP77 */
183 { PCI_VDEVICE(NVIDIA, 0x0ad4), board_ahci, chip_mcp77 }, /* MCP77 */
184 { PCI_VDEVICE(NVIDIA, 0x0ad5), board_ahci, chip_mcp77 }, /* MCP77 */
185 { PCI_VDEVICE(NVIDIA, 0x0ad6), board_ahci, chip_mcp77 }, /* MCP77 */
186 { PCI_VDEVICE(NVIDIA, 0x0ad7), board_ahci, chip_mcp77 }, /* MCP77 */
187 { PCI_VDEVICE(NVIDIA, 0x0ad8), board_ahci, chip_mcp77 }, /* MCP77 */
188 { PCI_VDEVICE(NVIDIA, 0x0ad9), board_ahci, chip_mcp77 }, /* MCP77 */
189 { PCI_VDEVICE(NVIDIA, 0x0ada), board_ahci, chip_mcp77 }, /* MCP77 */
190 { PCI_VDEVICE(NVIDIA, 0x0adb), board_ahci, chip_mcp77 }, /* MCP77 */
191 { PCI_VDEVICE(NVIDIA, 0x0ab4), board_ahci, chip_mcp79 }, /* MCP79 */
192 { PCI_VDEVICE(NVIDIA, 0x0ab5), board_ahci, chip_mcp79 }, /* MCP79 */
193 { PCI_VDEVICE(NVIDIA, 0x0ab6), board_ahci, chip_mcp79 }, /* MCP79 */
194 { PCI_VDEVICE(NVIDIA, 0x0ab7), board_ahci, chip_mcp79 }, /* MCP79 */
195 { PCI_VDEVICE(NVIDIA, 0x0ab8), board_ahci, chip_mcp79 }, /* MCP79 */
196 { PCI_VDEVICE(NVIDIA, 0x0ab9), board_ahci, chip_mcp79 }, /* MCP79 */
197 { PCI_VDEVICE(NVIDIA, 0x0aba), board_ahci, chip_mcp79 }, /* MCP79 */
198 { PCI_VDEVICE(NVIDIA, 0x0abb), board_ahci, chip_mcp79 }, /* MCP79 */
199 { PCI_VDEVICE(NVIDIA, 0x0abc), board_ahci, chip_mcp79 }, /* MCP79 */
200 { PCI_VDEVICE(NVIDIA, 0x0abd), board_ahci, chip_mcp79 }, /* MCP79 */
201 { PCI_VDEVICE(NVIDIA, 0x0abe), board_ahci, chip_mcp79 }, /* MCP79 */
202 { PCI_VDEVICE(NVIDIA, 0x0abf), board_ahci, chip_mcp79 }, /* MCP79 */
203 { PCI_VDEVICE(NVIDIA, 0x0d84), board_ahci, chip_mcp89 }, /* MCP89 */
204 { PCI_VDEVICE(NVIDIA, 0x0d85), board_ahci, chip_mcp89 }, /* MCP89 */
205 { PCI_VDEVICE(NVIDIA, 0x0d86), board_ahci, chip_mcp89 }, /* MCP89 */
206 { PCI_VDEVICE(NVIDIA, 0x0d87), board_ahci, chip_mcp89 }, /* MCP89 */
207 { PCI_VDEVICE(NVIDIA, 0x0d88), board_ahci, chip_mcp89 }, /* MCP89 */
208 { PCI_VDEVICE(NVIDIA, 0x0d89), board_ahci, chip_mcp89 }, /* MCP89 */
209 { PCI_VDEVICE(NVIDIA, 0x0d8a), board_ahci, chip_mcp89 }, /* MCP89 */
210 { PCI_VDEVICE(NVIDIA, 0x0d8b), board_ahci, chip_mcp89 }, /* MCP89 */
211 { PCI_VDEVICE(NVIDIA, 0x0d8c), board_ahci, chip_mcp89 }, /* MCP89 */
212 { PCI_VDEVICE(NVIDIA, 0x0d8d), board_ahci, chip_mcp89 }, /* MCP89 */
213 { PCI_VDEVICE(NVIDIA, 0x0d8e), board_ahci, chip_mcp89 }, /* MCP89 */
214 { PCI_VDEVICE(NVIDIA, 0x0d8f), board_ahci, chip_mcp89 }, /* MCP89 */
215
216 /* SiS */
217 { PCI_VDEVICE(SI, 0x1184), board_ahci, "966" }, /* SiS 966 */
218 { PCI_VDEVICE(SI, 0x1185), board_ahci, chip_sis968 }, /* SiS 968 */
219 { PCI_VDEVICE(SI, 0x0186), board_ahci, chip_sis968 }, /* SiS 968 */
220
221 /* Marvell */
222 { PCI_VDEVICE(MARVELL, 0x6145), board_ahci_mv, "6145" }, /* 6145 */
223 { PCI_VDEVICE(MARVELL, 0x6121), board_ahci_mv, "6121" }, /* 6121 */
224
225 /* Promise */
226 { PCI_VDEVICE(PROMISE, 0x3f20), board_ahci, "PDC42819" }, /* PDC42819 */
227
228 /* Generic, PCI class code for AHCI */
229 { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
230 PCI_CLASS_STORAGE_SATA_AHCI, 0xffffffL, board_ahci, s_generic },
231
232 /* end of list, including a few slots to define custom adapters (10) */
233 { 0, 0, 0, 0, 0, 0, 0, NULL },
234 { 0, 0, 0, 0, 0, 0, 0, NULL },
235 { 0, 0, 0, 0, 0, 0, 0, NULL },
236 { 0, 0, 0, 0, 0, 0, 0, NULL },
237 { 0, 0, 0, 0, 0, 0, 0, NULL },
238 { 0, 0, 0, 0, 0, 0, 0, NULL },
239 { 0, 0, 0, 0, 0, 0, 0, NULL },
240 { 0, 0, 0, 0, 0, 0, 0, NULL },
241 { 0, 0, 0, 0, 0, 0, 0, NULL },
242 { 0, 0, 0, 0, 0, 0, 0, NULL },
243
244 { 0, 0, 0, 0, 0, 0, 0, NULL }
245};
246
247/* ----------------------------- start of code ----------------------------- */
248
249/******************************************************************************
250 * Add specified PCI vendor and device ID to the list of supported AHCI
251 * controllers. Please note that the last slot in pci_ids needs to remain
252 * empty because it's used as end marker.
253 */
254int add_pci_id(u16 vendor, u16 device)
255{
256 int max_slot = sizeof(pci_ids) / sizeof(*pci_ids) - 2;
257 int i;
258
259 /* search for last used slot in 'pci_ids' */
260 for (i = max_slot; i >= 0 && pci_ids[i].vendor == 0; i--);
261 if (i >= max_slot) return(-1); /* all slots in use */
262
263 /* use slot after the last used slot */
264 i++;
265 pci_ids[i].vendor = vendor;
266 pci_ids[i].device = device;
267 pci_ids[i].board = board_ahci;
268 pci_ids[i].chipname = s_generic;
269 return(0);
270}
271
272/******************************************************************************
273 * Scan PCI bus using OEMHLP$ IOCTLs and build adapter list.
274 */
275void scan_pci_bus(void)
276{
277 UCHAR index;
278 int ad_indx = 0;
279 int i;
280 int n;
281 USHORT BusDevFunc;
282
283 DPRINTF(3,"scanning PCI bus...\n");
284
285 /* Go through the list of PCI IDs and search for each device
286 *
287 * NOTES:
288 *
289 * - When searching via class code, the OEMHLP$ interface doesn't allow
290 * setting a bitmask to look for individual portions of class code,
291 * subclass code and programming interface. However, all bitmasks in the
292 * PCI list currently use 0xffffff, thus this should not be a problem at
293 * this point in time.
294 *
295 * - Scanning via OEMHLP$ seems rather slow, at least in the virtual
296 * machine I'm currenly using to test this driver. Thus, class code
297 * scans are preferred unless the option "-t" (thorough_scan) has been
298 * specified. The assumption is that most, if not all, modern AHCI
299 * adapters have the correct class code (PCI_CLASS_STORAGE_SATA_AHCI).
300 */
301 for (i = 0; pci_ids[i].vendor != 0; i++)
302 {
303 index = 0;
304 do
305 {
306 if (pci_ids[i].device == PCI_ANY_ID || pci_ids[i].vendor == PCI_ANY_ID)
307 {
308 /* look for class code */
309 BusDevFunc = PciFindClass(pci_ids[i].class, index);
310 }
311 else if (thorough_scan)
312 {
313 /* look for this specific vendor and device ID */
314 BusDevFunc = PciFindDevice( pci_ids[i].vendor, pci_ids[i].device, index);
315
316 }
317 else
318 {
319 BusDevFunc = 0xffff;
320 }
321
322 if (BusDevFunc != 0xffff)
323 {
324 /* found a device */
325 int already_found = 0;
326
327 /* increment index for next loop */
328 if (++index > 180) return; /* something's wrong here... */
329
330 /* check whether we already found this device */
331 for (n = 0; n < ad_info_cnt; n++)
332 {
333 if (ad_infos[n].bus_dev_func == BusDevFunc)
334 {
335 /* this device has already been found (e.g. via thorough scan) */
336 already_found = 1;
337 break;
338 }
339 }
340
341 if (already_found || (ad_ignore & (1U << ad_indx++)))
342 {
343 /* ignore this device; it has either already been found via a
344 * thorough scan or has been specified to be ignored via command
345 * line option */
346 continue;
347 }
348
349 /* add this PCI device to ad_infos[] */
350 add_pci_device(pci_ids + i, BusDevFunc);
351 }
352
353 } while (BusDevFunc != 0xffff);
354 }
355}
356
357/******************************************************************************
358 * Enable interrupt generation. PCI 2.3 added a bit which allows disabling
359 * interrupt generation for a device. This function clears the corresponding
360 * bit in the configuration space command register.
361 */
362int pci_enable_int(USHORT BusDevFunc)
363{
364 ULONG tmp;
365
366 if (PciReadConfig(BusDevFunc, 4, sizeof(tmp), &tmp) ||
367 PciWriteConfig(BusDevFunc, 4, sizeof(tmp), tmp & ~(1UL << 10)))
368 {
369 return(-1);
370 }
371 return(0);
372}
373
374/******************************************************************************
375 * Hack to set up proper IRQ mappings in the emulated PIIX3 ISA bridge in
376 * VirtualBox (for some reason, the first mapped IRQ is 0x80 without this
377 * hack).
378 */
379void pci_hack_virtualbox(void)
380{
381 UCHAR irq = 0;
382
383 if (!PciReadConfig(0x0008, 0x60, sizeof(irq), &irq) && irq == 0x80)
384 {
385 /* set IRQ for first device/func to 11 */
386 DPRINTF(1,"hacking virtualbox PIIX3 PCI to ISA bridge IRQ mapping\n");
387 irq = ad_infos[0].irq;
388 PciWriteConfig(0x0008, 0x60, sizeof(irq), irq);
389 }
390}
391
392/******************************************************************************
393 * Add a single PCI device to the list of adapters.
394 */
395static void add_pci_device(PCI_ID *pci_id, USHORT BusDevFunc)
396{
397 char rc_list_buf[sizeof(AHRESOURCE) + sizeof(HRESOURCE) * 15];
398 AHRESOURCE *rc_list = (AHRESOURCE *) rc_list_buf;
399 RESOURCESTRUCT resource;
400 ADAPTERSTRUCT adapter;
401 ADJUNCT adj;
402 AD_INFO *ad_info;
403 APIRET ret;
404 ULONG val;
405 char tmp[40];
406 u16 device;
407 u16 vendor;
408 u32 class;
409 int irq;
410 int pin;
411 int i;
412
413 /*****************************************************************************
414 * Part 1: Get further information about the device to be added; PCI ID...
415 */
416 if (PciReadConfig(BusDevFunc, 0x00, sizeof(ULONG), &val)) return;
417 device = (val >> 16);
418 vendor = (val & 0xffff);
419
420 /* ... and class code */
421 if (PciReadConfig(BusDevFunc, 0x08, sizeof(ULONG), &val)) return;
422 class = (val >> 8);
423
424 if (pci_id->device == PCI_ANY_ID)
425 {
426 /* We found this device in a wildcard search. There are two possible
427 * reasons which require a different handling:
428 *
429 * 1) This device uses a non-standard PCI class and has been identified
430 * with the corresponding class in pci_ids[] (e.g. the entry
431 * PCI_VENDOR_ID_JMICRON), but there is a vendor ID in pci_ids[]. In
432 * this case, we need to verify that the vendor is correct (see
433 * comments regarding OEMHLP limitations in 'scan_pci_bus()')
434 *
435 * 2) This device was identified using a generic PCI class for AHCI
436 * adapters such as PCI_CLASS_STORAGE_SATA_AHCI and we need to map
437 * the device and vendor ID to the corresponding index in pci_ids[]
438 * if there is such an entry; the index passed to this function will
439 * be the generic class-based index which is fine as long as there's
440 * not special treatment required as indicated by the board_*
441 * constants in pci_ids[]...
442 *
443 * The main reason for this kludge is that it seems as if OEMHLP$
444 * is rather slow searching for PCI devices, adding around 30s
445 * to the boot time when scanning for individual AHCI PCI IDs. Thus,
446 * the OS2AHCI driver avoids this kind of scan in favor of a class-
447 * based scan (unless overridden with the "/T" option).
448 */
449 if (pci_id->vendor != PCI_ANY_ID)
450 {
451 /* case 1: the vendor is known but we found the PCI device using a class
452 * search; verify vendor matches the one in pci_ids[]
453 */
454 if (pci_id->vendor != vendor) return; /* vendor doesn't match */
455 }
456 else
457 {
458 /* case 2: we found this device using a generic class search; if the
459 * device/vendor is listed in pci_ids[], use this entry in favor of the
460 * one passed in 'pci_id'
461 */
462 for (i = 0; pci_ids[i].vendor != 0; i++)
463 {
464 if (pci_ids[i].device == device && pci_ids[i].vendor == vendor)
465 {
466 pci_id = pci_ids + i;
467 break;
468 }
469 }
470 }
471 }
472
473 /* found a supported AHCI device */
474
475 if (PciReadConfig(BusDevFunc, 0x3c, sizeof(u32), &val)) return;
476 irq = (int) (val & 0xff);
477 pin = (int) ((val >> 8) & 0xff);
478
479 i = 1;
480 if (irq==0 || irq==255) i = 0;
481
482 if (verbosity > i)
483 {
484 iprintf("%s AHCI device %s %s (%d:%d:%d %04x:%04x) class:0x%06x", i?"Found":"Ignoring",
485 vendor_from_id(vendor), device_from_id(device),
486 PCI_BUS_FROM_BDF(BusDevFunc), PCI_DEV_FROM_BDF(BusDevFunc), PCI_FUNC_FROM_BDF(BusDevFunc),
487 vendor, device, class);
488 if (i==0) iprintf("Invalid interrupt (IRQ=%d).", irq);
489 }
490 if (i==0) return;
491
492 /* make sure we got room in the adapter information array */
493 if (ad_info_cnt >= MAX_AD - 1)
494 {
495 iprintf("%s: too many AHCI devices", drv_name);
496 return;
497 }
498
499 /****************************************************************************
500 * Part 2: Determine resource requirements and allocate resources with the
501 * OS/2 resource manager. While doing so, some of the entries of the
502 * corresponding slot in the AD_INFO array, namely resource manager
503 * handles, are initialized so we need prepare the slot.
504 *
505 * NOTE: While registering resources with the resource manager, each new
506 * resource is added to the corresponding rc_list.hResource[] slot.
507 * rc_list is used further down to associate resources to adapters
508 * when the adapter itself is registered with the OS/2 resource
509 * manager.
510 */
511 ad_info = ad_infos + ad_info_cnt;
512 memset(ad_info, 0x00, sizeof(*ad_info));
513 rc_list->NumResource = 0;
514
515 /* Register IRQ with resource manager */
516 ret = RmAddIrq(rm_drvh, &ad_info->rm_irq, irq, pin);
517 if (ret)
518 {
519 if (ret == RMRC_RES_ALREADY_CLAIMED)
520 {
521 ciiprintf("Device already claimed.");
522 }
523 else
524 {
525 iprintf("%s: couldn't register IRQ %d (rc = %s)", drv_name, irq, rmerr(ret));
526 }
527 return;
528 }
529 rc_list->hResource[rc_list->NumResource++] = ad_info->rm_irq;
530
531 /* Allocate all I/O and MMIO addresses offered by this device. In theory,
532 * we need only BAR #5, the AHCI MMIO BAR, but in order to prevent any
533 * other driver from hijacking our device and accessing it via legacy
534 * registers we'll reserve anything we can find.
535 */
536
537 ciprintf("Adapter %d PCI=%d:%d:%d ID=%04x:%04x\n", ad_info_cnt, PCI_BUS_FROM_BDF(BusDevFunc),
538 PCI_DEV_FROM_BDF(BusDevFunc), PCI_FUNC_FROM_BDF(BusDevFunc), vendor, device);
539 DPRINTF(1,"Adapter %d PCI=%d:%d:%d ID=%04x:%04x\n", ad_info_cnt, PCI_BUS_FROM_BDF(BusDevFunc),
540 PCI_DEV_FROM_BDF(BusDevFunc), PCI_FUNC_FROM_BDF(BusDevFunc), vendor, device);
541
542 for (i = 0; i < sizeof(ad_info->rm_bars) / sizeof(*ad_info->rm_bars); i++)
543 {
544 long len = bar_resource(BusDevFunc, &resource, i);
545
546 if (len < 0)
547 {
548 /* something went wrong */
549 goto add_pci_fail;
550 }
551 if (len == 0)
552 {
553 /* this BAR is unused */
554 continue;
555 }
556
557 if (i == AHCI_PCI_BAR)
558 {
559 if (resource.ResourceType != RS_TYPE_MEM)
560 {
561 iprintf("%s: BAR #5 must be an MMIO region", drv_name);
562 goto add_pci_fail;
563 }
564 /* save this BAR's address as MMIO address */
565 ad_info->mmio_phys = resource.MEMResource.MemBase;
566 ad_info->mmio_size = resource.MEMResource.MemSize;
567 }
568
569 /* register [MM]IO region with resource manager */
570 ret = RMAllocResource(rm_drvh, ad_info->rm_bars + i, &resource);
571 if (ret != RMRC_SUCCESS)
572 {
573 if (ret == RMRC_RES_ALREADY_CLAIMED)
574 {
575 ciiprintf("Device already claimed.");
576 }
577 else
578 {
579 iprintf("%s: couldn't register [MM]IO region (rc = %s)", drv_name, rmerr(ret));
580 }
581 goto add_pci_fail;
582 }
583 rc_list->hResource[rc_list->NumResource++] = ad_info->rm_bars[i];
584 }
585
586 if (ad_info->mmio_phys == 0)
587 {
588 iprintf("%s: couldn't determine MMIO base address", drv_name);
589 goto add_pci_fail;
590 }
591
592 /****************************************************************************
593 * Part 3: Fill in the remaining fields in the AD_INFO slot and allocate
594 * memory and GDT selectors for the adapter. Finally, register the adapter
595 * itself with the OS/2 resource manager
596 */
597 ad_info->pci = pci_id;
598 ad_info->pci_vendor = vendor;
599 ad_info->pci_device = device;
600 ad_info->bus_dev_func = BusDevFunc;
601 ad_info->irq = irq;
602
603 ad_info->mmio = MapPhysToLin(ad_info->mmio_phys, ad_info->mmio_size);
604 if (!ad_info->mmio) goto add_pci_fail;
605
606 /* register adapter with resource manager */
607 memset(&adj, 0x00, sizeof(adj));
608 adj.pNextAdj = NULL;
609 adj.AdjLength = sizeof(adj);
610 adj.AdjType = ADJ_ADAPTER_NUMBER;
611 adj.Adapter_Number = ad_info_cnt;
612
613 memset(&adapter, 0x00, sizeof(adapter));
614 sprintf(tmp, "AHCI_%d Controller", ad_info_cnt);
615 adapter.AdaptDescriptName = tmp;
616 adapter.AdaptFlags = 0;
617 adapter.BaseType = AS_BASE_MSD;
618 adapter.SubType = AS_SUB_IDE;
619 adapter.InterfaceType = AS_INTF_GENERIC;
620 adapter.HostBusType = AS_HOSTBUS_PCI;
621 adapter.HostBusWidth = AS_BUSWIDTH_32BIT;
622 adapter.pAdjunctList = &adj;
623
624 ret = RMCreateAdapter(rm_drvh, &ad_info->rm_adh, &adapter, NULL, rc_list);
625 if (ret != RMRC_SUCCESS)
626 {
627 iprintf("%s: couldn't register adapter (rc = %s)", drv_name, rmerr(ret));
628 goto add_pci_fail;
629 }
630
631 if (ahci_config_caps(ad_info)) goto add_pci_fail;
632
633 #ifndef DAZ_NEW_CODE
634 /* fill in DMA scratch buffer addresses in adapter info */
635 for (i = 0; i < AHCI_MAX_PORTS; i++)
636 {
637 if (!(ad_info->port_map & (1UL << i))) continue;
638
639 ad_info->ports[i].dma_buf = MemAllocAlign(AHCI_PORT_PRIV_DMA_SZ, 1024);
640 ad_info->ports[i].dma_buf_phys = MemPhysAdr(ad_info->ports[i].dma_buf);
641 }
642 #endif
643
644 /* Successfully added the adapter and reserved its resources; the adapter
645 * is still under BIOS control so we're not going to do anything else at
646 * this point.
647 */
648
649 ad_info_cnt++;
650 return;
651
652
653add_pci_fail:
654 /* something went wrong; try to clean up as far as possible */
655 for (i = 0; i < sizeof(ad_info->rm_bars) / sizeof(*ad_info->rm_bars); i++)
656 {
657 if (ad_info->rm_bars[i] != 0) RMDeallocResource(rm_drvh, ad_info->rm_bars[i]);
658 }
659 if (ad_info->rm_irq != 0) RMDeallocResource(rm_drvh, ad_info->rm_irq);
660}
661
662/******************************************************************************
663 * Prepare a resource structure for a PCI Base Address Register (BAR). This
664 * basically means the type, address and range of the I/O address space. It
665 * returns the length of the address range as a signed long to allow the caller
666 * to differentiate between error conditions (< 0), unused BARs (0) or valid
667 * bars (> 0).
668 *
669 * NOTE: In order to do this, we need to temporarily write 0xffffffff to
670 * the MMIO base address register (BAR), read back the resulting value
671 * and check the 0 bits from the right end, masking the lower 2 (I/O) or
672 * 4 (MMIO) bits. After doing this, we must restore the original value
673 * set up by the BIOS.
674 *
675 * 31 4 3 2 1 0
676 * -------------------------------------------------------------------
677 * base address P T T I
678 * P = prefetchable
679 * T = type (0 = any 32 bit, 1 = <1M, 2 = 64 bit)
680 * I = I/O (1) or memory (0)
681 */
682static long bar_resource(USHORT BusDevFunc, RESOURCESTRUCT *resource, int i)
683{
684 u32 bar_addr = 0;
685 u32 bar_size = 0;
686
687 /* temporarily write 1s to this BAR to determine the address range */
688 if (PciReadConfig (BusDevFunc, PCI_BAR(i), sizeof(u32), &bar_addr) ||
689 PciWriteConfig(BusDevFunc, PCI_BAR(i), sizeof(u32), ~(0UL)) ||
690 PciReadConfig (BusDevFunc, PCI_BAR(i), sizeof(u32), &bar_size) ||
691 PciWriteConfig(BusDevFunc, PCI_BAR(i), sizeof(u32), bar_addr) )
692 {
693 iprintf("%s: couldn't determine [MM]IO size", drv_name);
694 if (bar_addr != 0)
695 {
696 PciWriteConfig(BusDevFunc, PCI_BAR(i), sizeof(u32), bar_addr);
697 }
698 return(-1);
699 }
700
701 /* bar not implemented or device not working properly */
702 if (bar_size == 0 || bar_size == 0xffffffffUL) return(0);
703
704 /* prepare resource allocation structure */
705 memset(resource, 0x00, sizeof(*resource));
706 if (bar_addr & 1)
707 {
708 bar_size = ~(bar_size & 0xfffffffcUL) + 1;
709 bar_size &= 0xffffUL; /* I/O address space is 16 bits on x86 */
710 bar_addr &= 0xfffcUL;
711
712 resource->ResourceType = RS_TYPE_IO;
713 resource->IOResource.BaseIOPort = bar_addr;
714 resource->IOResource.NumIOPorts = bar_size;
715 resource->IOResource.IOFlags = RS_IO_EXCLUSIVE;
716 resource->IOResource.IOAddressLines = 16;
717
718 }
719 else
720 {
721 bar_size = ~(bar_size & 0xfffffff0UL) + 1;
722 bar_addr &= 0xfffffff0UL;
723
724 resource->ResourceType = RS_TYPE_MEM;
725 resource->MEMResource.MemBase = bar_addr;
726 resource->MEMResource.MemSize = bar_size;
727 resource->MEMResource.MemFlags = RS_MEM_EXCLUSIVE;
728 }
729
730 DPRINTF(3,"BAR #%d: type = %s, addr = 0x%08lx, size = %d\n", i,
731 (resource->ResourceType == RS_TYPE_IO) ? "I/O" : "MEM",
732 bar_addr, bar_size);
733
734 return((long) bar_size);
735}
736
737/******************************************************************************
738 * return vendor name for PCI vendor ID
739 */
740char *vendor_from_id(u16 id)
741{
742
743 switch(id)
744 {
745 case PCI_VENDOR_ID_AL:
746 return "Ali";
747 case PCI_VENDOR_ID_AMD:
748 case PCI_VENDOR_ID_ATI:
749 return "AMD";
750 case PCI_VENDOR_ID_AT:
751 return "Allied Telesyn";
752 case PCI_VENDOR_ID_ATT:
753 return "ATT";
754 case PCI_VENDOR_ID_CMD:
755 return "CMD";
756 case PCI_VENDOR_ID_CT:
757 return "CT";
758 case PCI_VENDOR_ID_INTEL:
759 return "Intel";
760 case PCI_VENDOR_ID_INITIO:
761 return "Initio";
762 case PCI_VENDOR_ID_JMICRON:
763 return "JMicron";
764 case PCI_VENDOR_ID_MARVELL:
765 return "Marvell";
766 case PCI_VENDOR_ID_NVIDIA:
767 return "NVIDIA";
768 case PCI_VENDOR_ID_PROMISE:
769 return "PROMISE";
770 case PCI_VENDOR_ID_SI:
771 return "SiS";
772 case PCI_VENDOR_ID_VIA:
773 return "VIA";
774 default:
775 break;
776 }
777
778 return "Generic";
779}
780
781/******************************************************************************
782 * return a device name for a PCI device id
783 * NOTE: this is as simple as can be, so don't call it twice in one statement.
784 */
785char *device_from_id(u16 device)
786{
787 int i;
788
789 for (i = 0; pci_ids[i].vendor != 0; i++)
790 {
791 if (pci_ids[i].device == device)
792 {
793 return pci_ids[i].chipname;
794 }
795 }
796
797 return s_generic;
798}
799
800/******************************************************************************
801 * Return textual version of a resource manager error
802 */
803static char *rmerr(APIRET ret)
804{
805 switch (ret) {
806 case RMRC_SUCCESS:
807 return("RMRC_SUCCESS");
808 case RMRC_NOTINITIALIZED:
809 return("RMRC_NOTINITIALIZED");
810 case RMRC_BAD_DRIVERHANDLE:
811 return("RMRC_BAD_DRIVERHANDLE");
812 case RMRC_BAD_ADAPTERHANDLE:
813 return("RMRC_BAD_ADAPTERHANDLE");
814 case RMRC_BAD_DEVICEHANDLE:
815 return("RMRC_BAD_DEVICEHANDLE");
816 case RMRC_BAD_RESOURCEHANDLE:
817 return("RMRC_BAD_RESOURCEHANDLE");
818 case RMRC_BAD_LDEVHANDLE:
819 return("RMRC_BAD_LDEVHANDLE");
820 case RMRC_BAD_SYSNAMEHANDLE:
821 return("RMRC_BAD_SYSNAMEHANDLE");
822 case RMRC_BAD_DEVHELP:
823 return("RMRC_BAD_DEVHELP");
824 case RMRC_NULL_POINTER:
825 return("RMRC_NULL_POINTER");
826 case RMRC_NULL_STRINGS:
827 return("RMRC_NULL_STRINGS");
828 case RMRC_BAD_VERSION:
829 return("RMRC_BAD_VERSION");
830 case RMRC_RES_ALREADY_CLAIMED:
831 return("RMRC_RES_ALREADY_CLAIMED");
832 case RMRC_DEV_ALREADY_CLAIMED:
833 return("RMRC_DEV_ALREADY_CLAIMED");
834 case RMRC_INVALID_PARM_VALUE:
835 return("RMRC_INVALID_PARM_VALUE");
836 case RMRC_OUT_OF_MEMORY:
837 return("RMRC_OUT_OF_MEMORY");
838 case RMRC_SEARCH_FAILED:
839 return("RMRC_SEARCH_FAILED");
840 case RMRC_BUFFER_TOO_SMALL:
841 return("RMRC_BUFFER_TOO_SMALL");
842 case RMRC_GENERAL_FAILURE:
843 return("RMRC_GENERAL_FAILURE");
844 case RMRC_IRQ_ENTRY_ILLEGAL:
845 return("RMRC_IRQ_ENTRY_ILLEGAL");
846 case RMRC_NOT_IMPLEMENTED:
847 return("RMRC_NOT_IMPLEMENTED");
848 case RMRC_NOT_INSTALLED:
849 return("RMRC_NOT_INSTALLED");
850 case RMRC_BAD_DETECTHANDLE:
851 return("RMRC_BAD_DETECTHANDLE");
852 case RMRC_BAD_RMHANDLE:
853 return("RMRC_BAD_RMHANDLE");
854 case RMRC_BAD_FLAGS:
855 return("RMRC_BAD_FLAGS");
856 case RMRC_NO_DETECTED_DATA:
857 return("RMRC_NO_DETECTED_DATA");
858 default:
859 return("RMRC_UNKOWN");
860 }
861}
Note: See TracBrowser for help on using the repository browser.