]> git.ipfire.org Git - thirdparty/mdadm.git/blame - platform-intel.c
imsm: enforce "all member disks must be members of all arrays"
[thirdparty/mdadm.git] / platform-intel.c
CommitLineData
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
32void 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
44struct 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
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
121static 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
139static struct imsm_orom imsm_orom;
140static 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
157const struct imsm_orom *find_imsm_orom(void)
158{
159 static int populated = 0;
160
161 /* it's static data so we only need to read it once */
162 if (populated)
163 return &imsm_orom;
164
165 if (!platform_has_intel_ahci())
166 return NULL;
167
168 /* scan option-rom memory looking for an imsm signature */
169 if (probe_roms_init() != 0)
170 return NULL;
171 probe_roms();
172 populated = scan_adapter_roms(scan);
173 probe_roms_exit();
174
175 if (populated)
176 return &imsm_orom;
177 return NULL;
178}
25921536
DW
179
180char *devt_to_devpath(dev_t dev)
181{
182 char device[40];
183
184 sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev));
185 return canonicalize_file_name(device);
186}
187
188static char *diskfd_to_devpath(int fd)
189{
190 /* return the device path for a disk, return NULL on error or fd
191 * refers to a partition
192 */
193 struct stat st;
194
195 if (fstat(fd, &st) != 0)
196 return NULL;
197 if (!S_ISBLK(st.st_mode))
198 return NULL;
199
200 return devt_to_devpath(st.st_rdev);
201}
202
203int path_attached_to_hba(const char *disk_path, const char *hba_path)
204{
205 int rc;
206
207 if (!disk_path || !hba_path)
208 return 0;
209
210 if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
211 rc = 1;
212 else
213 rc = 0;
214
215 return rc;
216}
217
218int devt_attached_to_hba(dev_t dev, const char *hba_path)
219{
220 char *disk_path = devt_to_devpath(dev);
221 int rc = path_attached_to_hba(disk_path, hba_path);
222
223 if (disk_path)
224 free(disk_path);
225
226 return rc;
227}
228
229int disk_attached_to_hba(int fd, const char *hba_path)
230{
231 char *disk_path = diskfd_to_devpath(fd);
232 int rc = path_attached_to_hba(disk_path, hba_path);
233
234 if (disk_path)
235 free(disk_path);
236
237 return rc;
238}
239