]>
Commit | Line | Data |
---|---|---|
b390f610 DW |
1 | /* |
2 | * Intel(R) Matrix Storage Manager hardware and firmware support routines | |
3 | * | |
4 | * Copyright (C) 2008 Intel Corporation | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | #include "mdadm.h" | |
20 | #include "platform-intel.h" | |
21 | #include "probe_roms.h" | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <unistd.h> | |
26 | #include <dirent.h> | |
27 | #include <fcntl.h> | |
28 | #include <sys/mman.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/stat.h> | |
31 | ||
32 | void free_sys_dev(struct sys_dev **list) | |
33 | { | |
34 | while (*list) { | |
35 | struct sys_dev *next = (*list)->next; | |
36 | ||
37 | if ((*list)->path) | |
38 | free((*list)->path); | |
39 | free(*list); | |
40 | *list = next; | |
41 | } | |
42 | } | |
43 | ||
44 | struct sys_dev *find_driver_devices(const char *bus, const char *driver) | |
45 | { | |
46 | /* search sysfs for devices driven by 'driver' */ | |
5dbb8c8d | 47 | char path[292]; |
b390f610 DW |
48 | char link[256]; |
49 | char *c; | |
50 | DIR *driver_dir; | |
51 | struct dirent *de; | |
52 | struct sys_dev *head = NULL; | |
53 | struct sys_dev *list = NULL; | |
a8e5382a LM |
54 | enum sys_dev_type type; |
55 | ||
56 | if (strcmp(driver, "isci") == 0) | |
57 | type = SYS_DEV_SAS; | |
58 | else if (strcmp(driver, "ahci") == 0) | |
59 | type = SYS_DEV_SATA; | |
60 | else | |
61 | type = SYS_DEV_UNKNOWN; | |
b390f610 DW |
62 | |
63 | sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver); | |
64 | driver_dir = opendir(path); | |
65 | if (!driver_dir) | |
66 | return NULL; | |
67 | for (de = readdir(driver_dir); de; de = readdir(driver_dir)) { | |
5a1920f2 AW |
68 | int n; |
69 | ||
b390f610 DW |
70 | /* is 'de' a device? check that the 'subsystem' link exists and |
71 | * that its target matches 'bus' | |
72 | */ | |
73 | sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem", | |
74 | bus, driver, de->d_name); | |
5a1920f2 | 75 | n = readlink(path, link, sizeof(link)); |
f21e18ca | 76 | if (n < 0 || n >= (int)sizeof(link)) |
b390f610 | 77 | continue; |
5a1920f2 | 78 | link[n] = '\0'; |
b390f610 DW |
79 | c = strrchr(link, '/'); |
80 | if (!c) | |
81 | continue; | |
82 | if (strncmp(bus, c+1, strlen(bus)) != 0) | |
83 | continue; | |
84 | ||
a8e5382a LM |
85 | sprintf(path, "/sys/bus/%s/drivers/%s/%s", |
86 | bus, driver, de->d_name); | |
87 | ||
88 | /* if it's not Intel device skip it. */ | |
89 | if (devpath_to_vendor(path) != 0x8086) | |
90 | continue; | |
91 | ||
b390f610 DW |
92 | /* start / add list entry */ |
93 | if (!head) { | |
94 | head = malloc(sizeof(*head)); | |
95 | list = head; | |
96 | } else { | |
97 | list->next = malloc(sizeof(*head)); | |
98 | list = list->next; | |
99 | } | |
100 | ||
101 | if (!list) { | |
102 | free_sys_dev(&head); | |
103 | break; | |
104 | } | |
105 | ||
a8e5382a | 106 | list->type = type; |
b390f610 DW |
107 | list->path = canonicalize_file_name(path); |
108 | list->next = NULL; | |
a8e5382a LM |
109 | if ((list->pci_id = strrchr(list->path, '/')) != NULL) |
110 | list->pci_id++; | |
b390f610 | 111 | } |
2a17c77b | 112 | closedir(driver_dir); |
b390f610 DW |
113 | return head; |
114 | } | |
115 | ||
116 | __u16 devpath_to_vendor(const char *dev_path) | |
117 | { | |
118 | char path[strlen(dev_path) + strlen("/vendor") + 1]; | |
119 | char vendor[7]; | |
120 | int fd; | |
121 | __u16 id = 0xffff; | |
122 | int n; | |
123 | ||
124 | sprintf(path, "%s/vendor", dev_path); | |
125 | ||
126 | fd = open(path, O_RDONLY); | |
127 | if (fd < 0) | |
128 | return 0xffff; | |
129 | ||
130 | n = read(fd, vendor, sizeof(vendor)); | |
131 | if (n == sizeof(vendor)) { | |
132 | vendor[n - 1] = '\0'; | |
133 | id = strtoul(vendor, NULL, 16); | |
134 | } | |
135 | close(fd); | |
136 | ||
137 | return id; | |
138 | } | |
139 | ||
a8e5382a | 140 | struct sys_dev *find_intel_devices(void) |
b390f610 | 141 | { |
a8e5382a LM |
142 | struct sys_dev *ahci, *isci; |
143 | ||
144 | isci = find_driver_devices("pci", "isci"); | |
145 | ahci = find_driver_devices("pci", "ahci"); | |
146 | ||
147 | if (!ahci) { | |
148 | ahci = isci; | |
149 | } else { | |
150 | struct sys_dev *elem = ahci; | |
151 | while (elem->next) | |
152 | elem = elem->next; | |
153 | elem->next = isci; | |
154 | } | |
155 | return ahci; | |
b390f610 DW |
156 | } |
157 | ||
a8e5382a LM |
158 | static int platform_has_intel_devices(void) |
159 | { | |
160 | struct sys_dev *devices; | |
161 | devices = find_intel_devices(); | |
162 | if (devices) { | |
163 | free_sys_dev(&devices); | |
164 | return 1; | |
165 | } | |
166 | return 0; | |
167 | } | |
b390f610 DW |
168 | |
169 | static struct imsm_orom imsm_orom; | |
170 | static int scan(const void *start, const void *end) | |
171 | { | |
172 | int offset; | |
173 | const struct imsm_orom *imsm_mem; | |
174 | int len = (end - start); | |
175 | ||
176 | for (offset = 0; offset < len; offset += 4) { | |
177 | imsm_mem = start + offset; | |
178 | if (memcmp(imsm_mem->signature, "$VER", 4) == 0) { | |
179 | imsm_orom = *imsm_mem; | |
180 | return 1; | |
181 | } | |
182 | } | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
187 | const struct imsm_orom *find_imsm_orom(void) | |
188 | { | |
189 | static int populated = 0; | |
969c2555 | 190 | unsigned long align; |
b390f610 DW |
191 | |
192 | /* it's static data so we only need to read it once */ | |
193 | if (populated) | |
194 | return &imsm_orom; | |
195 | ||
cceebc67 DW |
196 | if (check_env("IMSM_TEST_OROM")) { |
197 | memset(&imsm_orom, 0, sizeof(imsm_orom)); | |
198 | imsm_orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
199 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5; | |
200 | imsm_orom.sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
201 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
202 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB | | |
203 | IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB | | |
204 | IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB; | |
205 | imsm_orom.dpa = 6; | |
206 | imsm_orom.tds = 6; | |
207 | imsm_orom.vpa = 2; | |
208 | imsm_orom.vphba = 4; | |
209 | imsm_orom.attr = imsm_orom.rlc | IMSM_OROM_ATTR_ChecksumVerify; | |
210 | populated = 1; | |
211 | return &imsm_orom; | |
212 | } | |
213 | ||
a8e5382a | 214 | if (!platform_has_intel_devices()) |
b390f610 DW |
215 | return NULL; |
216 | ||
217 | /* scan option-rom memory looking for an imsm signature */ | |
969c2555 DW |
218 | if (check_env("IMSM_SAFE_OROM_SCAN")) |
219 | align = 2048; | |
220 | else | |
221 | align = 512; | |
222 | if (probe_roms_init(align) != 0) | |
b390f610 DW |
223 | return NULL; |
224 | probe_roms(); | |
225 | populated = scan_adapter_roms(scan); | |
226 | probe_roms_exit(); | |
227 | ||
228 | if (populated) | |
229 | return &imsm_orom; | |
230 | return NULL; | |
231 | } | |
25921536 DW |
232 | |
233 | char *devt_to_devpath(dev_t dev) | |
234 | { | |
37f0e1e0 | 235 | char device[46]; |
25921536 DW |
236 | |
237 | sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev)); | |
238 | return canonicalize_file_name(device); | |
239 | } | |
240 | ||
a8e5382a | 241 | char *diskfd_to_devpath(int fd) |
25921536 DW |
242 | { |
243 | /* return the device path for a disk, return NULL on error or fd | |
244 | * refers to a partition | |
245 | */ | |
246 | struct stat st; | |
247 | ||
248 | if (fstat(fd, &st) != 0) | |
249 | return NULL; | |
250 | if (!S_ISBLK(st.st_mode)) | |
251 | return NULL; | |
252 | ||
253 | return devt_to_devpath(st.st_rdev); | |
254 | } | |
255 | ||
256 | int path_attached_to_hba(const char *disk_path, const char *hba_path) | |
257 | { | |
258 | int rc; | |
259 | ||
260 | if (!disk_path || !hba_path) | |
261 | return 0; | |
a8e5382a | 262 | dprintf("hba: %s - disk: %s\n", hba_path, disk_path); |
25921536 DW |
263 | if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0) |
264 | rc = 1; | |
265 | else | |
266 | rc = 0; | |
267 | ||
268 | return rc; | |
269 | } | |
270 | ||
271 | int devt_attached_to_hba(dev_t dev, const char *hba_path) | |
272 | { | |
273 | char *disk_path = devt_to_devpath(dev); | |
274 | int rc = path_attached_to_hba(disk_path, hba_path); | |
275 | ||
276 | if (disk_path) | |
277 | free(disk_path); | |
278 | ||
279 | return rc; | |
280 | } | |
281 | ||
282 | int disk_attached_to_hba(int fd, const char *hba_path) | |
283 | { | |
284 | char *disk_path = diskfd_to_devpath(fd); | |
285 | int rc = path_attached_to_hba(disk_path, hba_path); | |
286 | ||
287 | if (disk_path) | |
288 | free(disk_path); | |
289 | ||
290 | return rc; | |
291 | } |