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