]> git.ipfire.org Git - thirdparty/mdadm.git/blame - platform-intel.c
Assemble/Incr : minor tidy up of setting 'trustworthy'.
[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
cceebc67
DW
165 if (check_env("IMSM_TEST_OROM")) {
166 memset(&imsm_orom, 0, sizeof(imsm_orom));
167 imsm_orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
168 IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5;
169 imsm_orom.sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
170 IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
171 IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB |
172 IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB |
173 IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB;
174 imsm_orom.dpa = 6;
175 imsm_orom.tds = 6;
176 imsm_orom.vpa = 2;
177 imsm_orom.vphba = 4;
178 imsm_orom.attr = imsm_orom.rlc | IMSM_OROM_ATTR_ChecksumVerify;
179 populated = 1;
180 return &imsm_orom;
181 }
182
b390f610
DW
183 if (!platform_has_intel_ahci())
184 return NULL;
185
186 /* scan option-rom memory looking for an imsm signature */
187 if (probe_roms_init() != 0)
188 return NULL;
189 probe_roms();
190 populated = scan_adapter_roms(scan);
191 probe_roms_exit();
192
193 if (populated)
194 return &imsm_orom;
195 return NULL;
196}
25921536
DW
197
198char *devt_to_devpath(dev_t dev)
199{
200 char device[40];
201
202 sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev));
203 return canonicalize_file_name(device);
204}
205
206static char *diskfd_to_devpath(int fd)
207{
208 /* return the device path for a disk, return NULL on error or fd
209 * refers to a partition
210 */
211 struct stat st;
212
213 if (fstat(fd, &st) != 0)
214 return NULL;
215 if (!S_ISBLK(st.st_mode))
216 return NULL;
217
218 return devt_to_devpath(st.st_rdev);
219}
220
221int path_attached_to_hba(const char *disk_path, const char *hba_path)
222{
223 int rc;
224
225 if (!disk_path || !hba_path)
226 return 0;
227
228 if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
229 rc = 1;
230 else
231 rc = 0;
232
233 return rc;
234}
235
236int devt_attached_to_hba(dev_t dev, const char *hba_path)
237{
238 char *disk_path = devt_to_devpath(dev);
239 int rc = path_attached_to_hba(disk_path, hba_path);
240
241 if (disk_path)
242 free(disk_path);
243
244 return rc;
245}
246
247int disk_attached_to_hba(int fd, const char *hba_path)
248{
249 char *disk_path = diskfd_to_devpath(fd);
250 int rc = path_attached_to_hba(disk_path, hba_path);
251
252 if (disk_path)
253 free(disk_path);
254
255 return rc;
256}
257