]> git.ipfire.org Git - thirdparty/mdadm.git/blob - platform-intel.c
Support external metadata recovery-resume
[thirdparty/mdadm.git] / platform-intel.c
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' */
47 char path[256];
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;
54
55 sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver);
56 driver_dir = opendir(path);
57 if (!driver_dir)
58 return NULL;
59 for (de = readdir(driver_dir); de; de = readdir(driver_dir)) {
60 /* is 'de' a device? check that the 'subsystem' link exists and
61 * that its target matches 'bus'
62 */
63 sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem",
64 bus, driver, de->d_name);
65 if (readlink(path, link, sizeof(link)) < 0)
66 continue;
67 c = strrchr(link, '/');
68 if (!c)
69 continue;
70 if (strncmp(bus, c+1, strlen(bus)) != 0)
71 continue;
72
73 /* start / add list entry */
74 if (!head) {
75 head = malloc(sizeof(*head));
76 list = head;
77 } else {
78 list->next = malloc(sizeof(*head));
79 list = list->next;
80 }
81
82 if (!list) {
83 free_sys_dev(&head);
84 break;
85 }
86
87 /* generate canonical path name for the device */
88 sprintf(path, "/sys/bus/%s/drivers/%s/%s",
89 bus, driver, de->d_name);
90 list->path = canonicalize_file_name(path);
91 list->next = NULL;
92 }
93 closedir(driver_dir);
94 return head;
95 }
96
97 __u16 devpath_to_vendor(const char *dev_path)
98 {
99 char path[strlen(dev_path) + strlen("/vendor") + 1];
100 char vendor[7];
101 int fd;
102 __u16 id = 0xffff;
103 int n;
104
105 sprintf(path, "%s/vendor", dev_path);
106
107 fd = open(path, O_RDONLY);
108 if (fd < 0)
109 return 0xffff;
110
111 n = read(fd, vendor, sizeof(vendor));
112 if (n == sizeof(vendor)) {
113 vendor[n - 1] = '\0';
114 id = strtoul(vendor, NULL, 16);
115 }
116 close(fd);
117
118 return id;
119 }
120
121 static int platform_has_intel_ahci(void)
122 {
123 struct sys_dev *devices = find_driver_devices("pci", "ahci");
124 struct sys_dev *dev;
125 int ret = 0;
126
127 for (dev = devices; dev; dev = dev->next)
128 if (devpath_to_vendor(dev->path) == 0x8086) {
129 ret = 1;
130 break;
131 }
132
133 free_sys_dev(&devices);
134
135 return ret;
136 }
137
138
139 static struct imsm_orom imsm_orom;
140 static int scan(const void *start, const void *end)
141 {
142 int offset;
143 const struct imsm_orom *imsm_mem;
144 int len = (end - start);
145
146 for (offset = 0; offset < len; offset += 4) {
147 imsm_mem = start + offset;
148 if (memcmp(imsm_mem->signature, "$VER", 4) == 0) {
149 imsm_orom = *imsm_mem;
150 return 1;
151 }
152 }
153
154 return 0;
155 }
156
157 const struct imsm_orom *find_imsm_orom(void)
158 {
159 static int populated = 0;
160 unsigned long align;
161
162 /* it's static data so we only need to read it once */
163 if (populated)
164 return &imsm_orom;
165
166 if (check_env("IMSM_TEST_OROM")) {
167 memset(&imsm_orom, 0, sizeof(imsm_orom));
168 imsm_orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
169 IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5;
170 imsm_orom.sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
171 IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
172 IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB |
173 IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB |
174 IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB;
175 imsm_orom.dpa = 6;
176 imsm_orom.tds = 6;
177 imsm_orom.vpa = 2;
178 imsm_orom.vphba = 4;
179 imsm_orom.attr = imsm_orom.rlc | IMSM_OROM_ATTR_ChecksumVerify;
180 populated = 1;
181 return &imsm_orom;
182 }
183
184 if (!platform_has_intel_ahci())
185 return NULL;
186
187 /* scan option-rom memory looking for an imsm signature */
188 if (check_env("IMSM_SAFE_OROM_SCAN"))
189 align = 2048;
190 else
191 align = 512;
192 if (probe_roms_init(align) != 0)
193 return NULL;
194 probe_roms();
195 populated = scan_adapter_roms(scan);
196 probe_roms_exit();
197
198 if (populated)
199 return &imsm_orom;
200 return NULL;
201 }
202
203 char *devt_to_devpath(dev_t dev)
204 {
205 char device[40];
206
207 sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev));
208 return canonicalize_file_name(device);
209 }
210
211 static char *diskfd_to_devpath(int fd)
212 {
213 /* return the device path for a disk, return NULL on error or fd
214 * refers to a partition
215 */
216 struct stat st;
217
218 if (fstat(fd, &st) != 0)
219 return NULL;
220 if (!S_ISBLK(st.st_mode))
221 return NULL;
222
223 return devt_to_devpath(st.st_rdev);
224 }
225
226 int path_attached_to_hba(const char *disk_path, const char *hba_path)
227 {
228 int rc;
229
230 if (!disk_path || !hba_path)
231 return 0;
232
233 if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
234 rc = 1;
235 else
236 rc = 0;
237
238 return rc;
239 }
240
241 int devt_attached_to_hba(dev_t dev, const char *hba_path)
242 {
243 char *disk_path = devt_to_devpath(dev);
244 int rc = path_attached_to_hba(disk_path, hba_path);
245
246 if (disk_path)
247 free(disk_path);
248
249 return rc;
250 }
251
252 int disk_attached_to_hba(int fd, const char *hba_path)
253 {
254 char *disk_path = diskfd_to_devpath(fd);
255 int rc = path_attached_to_hba(disk_path, hba_path);
256
257 if (disk_path)
258 free(disk_path);
259
260 return rc;
261 }
262