]>
Commit | Line | Data |
---|---|---|
e0f83fbe | 1 | /* |
c521693b GKH |
2 | * Copyright (C) IBM Corp. 2003 |
3 | * | |
8b5651bb | 4 | * Author: Patrick Mansfield<patmans@us.ibm.com> |
c521693b | 5 | * |
8b5651bb KS |
6 | * This program is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
c521693b GKH |
18 | */ |
19 | ||
c521693b | 20 | #include <errno.h> |
c521693b | 21 | #include <fcntl.h> |
78d9ecfd | 22 | #include <inttypes.h> |
07630cea LP |
23 | #include <linux/bsg.h> |
24 | #include <linux/types.h> | |
78d9ecfd | 25 | #include <scsi/scsi.h> |
c521693b | 26 | #include <scsi/sg.h> |
07630cea LP |
27 | #include <stdio.h> |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <sys/stat.h> | |
32 | #include <sys/types.h> | |
33 | #include <time.h> | |
34 | #include <unistd.h> | |
1aa1e248 | 35 | |
cf0fbc49 | 36 | #include "libudev.h" |
b4bbcaa9 TA |
37 | |
38 | #include "libudev-private.h" | |
07630cea | 39 | #include "random-util.h" |
c521693b | 40 | #include "scsi.h" |
1aa1e248 | 41 | #include "scsi_id.h" |
07630cea | 42 | #include "string-util.h" |
c521693b GKH |
43 | |
44 | /* | |
45 | * A priority based list of id, naa, and binary/ascii for the identifier | |
46 | * descriptor in VPD page 0x83. | |
47 | * | |
48 | * Brute force search for a match starting with the first value in the | |
49 | * following id_search_list. This is not a performance issue, since there | |
50 | * is normally one or some small number of descriptors. | |
51 | */ | |
52 | static const struct scsi_id_search_values id_search_list[] = { | |
461577ee ZJS |
53 | { SCSI_ID_TGTGROUP, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, |
54 | { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG_EXTENDED, SCSI_ID_BINARY }, | |
55 | { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG_EXTENDED, SCSI_ID_ASCII }, | |
56 | { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG, SCSI_ID_BINARY }, | |
57 | { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG, SCSI_ID_ASCII }, | |
912541b0 KS |
58 | /* |
59 | * Devices already exist using NAA values that are now marked | |
60 | * reserved. These should not conflict with other values, or it is | |
61 | * a bug in the device. As long as we find the IEEE extended one | |
62 | * first, we really don't care what other ones are used. Using | |
63 | * don't care here means that a device that returns multiple | |
64 | * non-IEEE descriptors in a random order will get different | |
65 | * names. | |
66 | */ | |
461577ee ZJS |
67 | { SCSI_ID_NAA, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, |
68 | { SCSI_ID_NAA, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII }, | |
69 | { SCSI_ID_EUI_64, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, | |
70 | { SCSI_ID_EUI_64, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII }, | |
71 | { SCSI_ID_T10_VENDOR, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, | |
72 | { SCSI_ID_T10_VENDOR, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII }, | |
73 | { SCSI_ID_VENDOR_SPECIFIC, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, | |
74 | { SCSI_ID_VENDOR_SPECIFIC, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII }, | |
c521693b GKH |
75 | }; |
76 | ||
77 | static const char hex_str[]="0123456789abcdef"; | |
78 | ||
c521693b GKH |
79 | /* |
80 | * Values returned in the result/status, only the ones used by the code | |
81 | * are used here. | |
82 | */ | |
83 | ||
461577ee ZJS |
84 | #define DID_NO_CONNECT 0x01 /* Unable to connect before timeout */ |
85 | #define DID_BUS_BUSY 0x02 /* Bus remain busy until timeout */ | |
86 | #define DID_TIME_OUT 0x03 /* Timed out for some other reason */ | |
87 | #define DRIVER_TIMEOUT 0x06 | |
88 | #define DRIVER_SENSE 0x08 /* Sense_buffer has been set */ | |
c521693b GKH |
89 | |
90 | /* The following "category" function returns one of the following */ | |
912541b0 KS |
91 | #define SG_ERR_CAT_CLEAN 0 /* No errors or other information */ |
92 | #define SG_ERR_CAT_MEDIA_CHANGED 1 /* interpreted from sense buffer */ | |
93 | #define SG_ERR_CAT_RESET 2 /* interpreted from sense buffer */ | |
461577ee ZJS |
94 | #define SG_ERR_CAT_TIMEOUT 3 |
95 | #define SG_ERR_CAT_RECOVERED 4 /* Successful command after recovered err */ | |
96 | #define SG_ERR_CAT_NOTSUPPORTED 5 /* Illegal / unsupported command */ | |
97 | #define SG_ERR_CAT_SENSE 98 /* Something else in the sense buffer */ | |
98 | #define SG_ERR_CAT_OTHER 99 /* Some other error/warning */ | |
c521693b | 99 | |
4e9fdfcc | 100 | static int do_scsi_page80_inquiry(struct udev *udev, |
912541b0 KS |
101 | struct scsi_id_device *dev_scsi, int fd, |
102 | char *serial, char *serial_short, int max_len); | |
4e9fdfcc | 103 | |
7d563a17 | 104 | static int sg_err_category_new(struct udev *udev, |
912541b0 KS |
105 | int scsi_status, int msg_status, int |
106 | host_status, int driver_status, const | |
107 | unsigned char *sense_buffer, int sb_len) | |
c521693b | 108 | { |
912541b0 KS |
109 | scsi_status &= 0x7e; |
110 | ||
111 | /* | |
112 | * XXX change to return only two values - failed or OK. | |
113 | */ | |
114 | ||
115 | if (!scsi_status && !host_status && !driver_status) | |
116 | return SG_ERR_CAT_CLEAN; | |
117 | ||
4c701096 YW |
118 | if (IN_SET(scsi_status, SCSI_CHECK_CONDITION, SCSI_COMMAND_TERMINATED) || |
119 | (driver_status & 0xf) == DRIVER_SENSE) { | |
912541b0 KS |
120 | if (sense_buffer && (sb_len > 2)) { |
121 | int sense_key; | |
122 | unsigned char asc; | |
123 | ||
124 | if (sense_buffer[0] & 0x2) { | |
125 | sense_key = sense_buffer[1] & 0xf; | |
126 | asc = sense_buffer[2]; | |
127 | } else { | |
128 | sense_key = sense_buffer[2] & 0xf; | |
129 | asc = (sb_len > 12) ? sense_buffer[12] : 0; | |
130 | } | |
131 | ||
132 | if (sense_key == RECOVERED_ERROR) | |
133 | return SG_ERR_CAT_RECOVERED; | |
134 | else if (sense_key == UNIT_ATTENTION) { | |
135 | if (0x28 == asc) | |
136 | return SG_ERR_CAT_MEDIA_CHANGED; | |
137 | if (0x29 == asc) | |
138 | return SG_ERR_CAT_RESET; | |
1f6b4113 | 139 | } else if (sense_key == ILLEGAL_REQUEST) |
912541b0 | 140 | return SG_ERR_CAT_NOTSUPPORTED; |
912541b0 KS |
141 | } |
142 | return SG_ERR_CAT_SENSE; | |
143 | } | |
144 | if (host_status) { | |
4c701096 | 145 | if (IN_SET(host_status, DID_NO_CONNECT, DID_BUS_BUSY, DID_TIME_OUT)) |
912541b0 KS |
146 | return SG_ERR_CAT_TIMEOUT; |
147 | } | |
148 | if (driver_status) { | |
149 | if (driver_status == DRIVER_TIMEOUT) | |
150 | return SG_ERR_CAT_TIMEOUT; | |
151 | } | |
152 | return SG_ERR_CAT_OTHER; | |
c521693b GKH |
153 | } |
154 | ||
7d563a17 | 155 | static int sg_err_category3(struct udev *udev, struct sg_io_hdr *hp) |
c521693b | 156 | { |
912541b0 KS |
157 | return sg_err_category_new(udev, |
158 | hp->status, hp->msg_status, | |
159 | hp->host_status, hp->driver_status, | |
160 | hp->sbp, hp->sb_len_wr); | |
c521693b GKH |
161 | } |
162 | ||
7d563a17 | 163 | static int sg_err_category4(struct udev *udev, struct sg_io_v4 *hp) |
78d9ecfd | 164 | { |
912541b0 KS |
165 | return sg_err_category_new(udev, hp->device_status, 0, |
166 | hp->transport_status, hp->driver_status, | |
167 | (unsigned char *)(uintptr_t)hp->response, | |
168 | hp->response_len); | |
78d9ecfd HR |
169 | } |
170 | ||
7d563a17 | 171 | static int scsi_dump_sense(struct udev *udev, |
912541b0 KS |
172 | struct scsi_id_device *dev_scsi, |
173 | unsigned char *sense_buffer, int sb_len) | |
c521693b | 174 | { |
912541b0 KS |
175 | int s; |
176 | int code; | |
177 | int sense_class; | |
178 | int sense_key; | |
179 | int asc, ascq; | |
c521693b | 180 | #ifdef DUMP_SENSE |
912541b0 KS |
181 | char out_buffer[256]; |
182 | int i, j; | |
c521693b GKH |
183 | #endif |
184 | ||
912541b0 KS |
185 | /* |
186 | * Figure out and print the sense key, asc and ascq. | |
187 | * | |
188 | * If you want to suppress these for a particular drive model, add | |
189 | * a black list entry in the scsi_id config file. | |
190 | * | |
191 | * XXX We probably need to: lookup the sense/asc/ascq in a retry | |
192 | * table, and if found return 1 (after dumping the sense, asc, and | |
193 | * ascq). So, if/when we get something like a power on/reset, | |
194 | * we'll retry the command. | |
195 | */ | |
196 | ||
912541b0 | 197 | if (sb_len < 1) { |
9f6445e3 | 198 | log_debug("%s: sense buffer empty", dev_scsi->kernel); |
912541b0 KS |
199 | return -1; |
200 | } | |
201 | ||
202 | sense_class = (sense_buffer[0] >> 4) & 0x07; | |
203 | code = sense_buffer[0] & 0xf; | |
204 | ||
205 | if (sense_class == 7) { | |
206 | /* | |
207 | * extended sense data. | |
208 | */ | |
209 | s = sense_buffer[7] + 8; | |
210 | if (sb_len < s) { | |
9f6445e3 | 211 | log_debug("%s: sense buffer too small %d bytes, %d bytes too short", |
461577ee | 212 | dev_scsi->kernel, sb_len, s - sb_len); |
912541b0 KS |
213 | return -1; |
214 | } | |
4c701096 | 215 | if (IN_SET(code, 0x0, 0x1)) { |
912541b0 KS |
216 | sense_key = sense_buffer[2] & 0xf; |
217 | if (s < 14) { | |
218 | /* | |
219 | * Possible? | |
220 | */ | |
9f6445e3 | 221 | log_debug("%s: sense result too" " small %d bytes", |
461577ee | 222 | dev_scsi->kernel, s); |
912541b0 KS |
223 | return -1; |
224 | } | |
225 | asc = sense_buffer[12]; | |
226 | ascq = sense_buffer[13]; | |
4c701096 | 227 | } else if (IN_SET(code, 0x2, 0x3)) { |
912541b0 KS |
228 | sense_key = sense_buffer[1] & 0xf; |
229 | asc = sense_buffer[2]; | |
230 | ascq = sense_buffer[3]; | |
231 | } else { | |
9f6445e3 | 232 | log_debug("%s: invalid sense code 0x%x", |
461577ee | 233 | dev_scsi->kernel, code); |
912541b0 KS |
234 | return -1; |
235 | } | |
9f6445e3 | 236 | log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x", |
461577ee | 237 | dev_scsi->kernel, sense_key, asc, ascq); |
912541b0 KS |
238 | } else { |
239 | if (sb_len < 4) { | |
9f6445e3 | 240 | log_debug("%s: sense buffer too small %d bytes, %d bytes too short", |
461577ee | 241 | dev_scsi->kernel, sb_len, 4 - sb_len); |
912541b0 KS |
242 | return -1; |
243 | } | |
244 | ||
245 | if (sense_buffer[0] < 15) | |
9f6445e3 | 246 | log_debug("%s: old sense key: 0x%x", dev_scsi->kernel, sense_buffer[0] & 0x0f); |
912541b0 | 247 | else |
9f6445e3 | 248 | log_debug("%s: sense = %2x %2x", |
461577ee | 249 | dev_scsi->kernel, sense_buffer[0], sense_buffer[2]); |
9f6445e3 | 250 | log_debug("%s: non-extended sense class %d code 0x%0x", |
461577ee | 251 | dev_scsi->kernel, sense_class, code); |
912541b0 KS |
252 | |
253 | } | |
c521693b GKH |
254 | |
255 | #ifdef DUMP_SENSE | |
912541b0 | 256 | for (i = 0, j = 0; (i < s) && (j < 254); i++) { |
912541b0 KS |
257 | out_buffer[j++] = hex_str[(sense_buffer[i] & 0xf0) >> 4]; |
258 | out_buffer[j++] = hex_str[sense_buffer[i] & 0x0f]; | |
259 | out_buffer[j++] = ' '; | |
260 | } | |
261 | out_buffer[j] = '\0'; | |
9f6445e3 LP |
262 | log_debug("%s: sense dump:", dev_scsi->kernel); |
263 | log_debug("%s: %s", dev_scsi->kernel, out_buffer); | |
c521693b GKH |
264 | |
265 | #endif | |
912541b0 | 266 | return -1; |
c521693b GKH |
267 | } |
268 | ||
7d563a17 | 269 | static int scsi_dump(struct udev *udev, |
912541b0 | 270 | struct scsi_id_device *dev_scsi, struct sg_io_hdr *io) |
c521693b | 271 | { |
912541b0 KS |
272 | if (!io->status && !io->host_status && !io->msg_status && |
273 | !io->driver_status) { | |
274 | /* | |
275 | * Impossible, should not be called. | |
276 | */ | |
9f6445e3 | 277 | log_debug("%s: called with no error", __FUNCTION__); |
912541b0 KS |
278 | return -1; |
279 | } | |
280 | ||
9f6445e3 | 281 | log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x", |
461577ee | 282 | dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status); |
912541b0 KS |
283 | if (io->status == SCSI_CHECK_CONDITION) |
284 | return scsi_dump_sense(udev, dev_scsi, io->sbp, io->sb_len_wr); | |
285 | else | |
286 | return -1; | |
78d9ecfd HR |
287 | } |
288 | ||
7d563a17 | 289 | static int scsi_dump_v4(struct udev *udev, |
912541b0 | 290 | struct scsi_id_device *dev_scsi, struct sg_io_v4 *io) |
78d9ecfd | 291 | { |
912541b0 KS |
292 | if (!io->device_status && !io->transport_status && |
293 | !io->driver_status) { | |
294 | /* | |
295 | * Impossible, should not be called. | |
296 | */ | |
9f6445e3 | 297 | log_debug("%s: called with no error", __FUNCTION__); |
912541b0 KS |
298 | return -1; |
299 | } | |
300 | ||
9f6445e3 | 301 | log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x", |
461577ee | 302 | dev_scsi->kernel, io->driver_status, io->transport_status, io->device_status); |
912541b0 KS |
303 | if (io->device_status == SCSI_CHECK_CONDITION) |
304 | return scsi_dump_sense(udev, dev_scsi, (unsigned char *)(uintptr_t)io->response, | |
305 | io->response_len); | |
306 | else | |
307 | return -1; | |
c521693b GKH |
308 | } |
309 | ||
7d563a17 | 310 | static int scsi_inquiry(struct udev *udev, |
912541b0 KS |
311 | struct scsi_id_device *dev_scsi, int fd, |
312 | unsigned char evpd, unsigned char page, | |
313 | unsigned char *buf, unsigned int buflen) | |
c521693b | 314 | { |
912541b0 KS |
315 | unsigned char inq_cmd[INQUIRY_CMDLEN] = |
316 | { INQUIRY_CMD, evpd, page, 0, buflen, 0 }; | |
317 | unsigned char sense[SENSE_BUFF_LEN]; | |
318 | void *io_buf; | |
319 | struct sg_io_v4 io_v4; | |
320 | struct sg_io_hdr io_hdr; | |
321 | int retry = 3; /* rather random */ | |
322 | int retval; | |
323 | ||
324 | if (buflen > SCSI_INQ_BUFF_LEN) { | |
9f6445e3 | 325 | log_debug("buflen %d too long", buflen); |
912541b0 KS |
326 | return -1; |
327 | } | |
c521693b GKH |
328 | |
329 | resend: | |
912541b0 | 330 | if (dev_scsi->use_sg == 4) { |
29804cc1 | 331 | memzero(&io_v4, sizeof(struct sg_io_v4)); |
912541b0 KS |
332 | io_v4.guard = 'Q'; |
333 | io_v4.protocol = BSG_PROTOCOL_SCSI; | |
334 | io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD; | |
335 | io_v4.request_len = sizeof(inq_cmd); | |
336 | io_v4.request = (uintptr_t)inq_cmd; | |
337 | io_v4.max_response_len = sizeof(sense); | |
338 | io_v4.response = (uintptr_t)sense; | |
339 | io_v4.din_xfer_len = buflen; | |
340 | io_v4.din_xferp = (uintptr_t)buf; | |
341 | io_buf = (void *)&io_v4; | |
342 | } else { | |
29804cc1 | 343 | memzero(&io_hdr, sizeof(struct sg_io_hdr)); |
912541b0 KS |
344 | io_hdr.interface_id = 'S'; |
345 | io_hdr.cmd_len = sizeof(inq_cmd); | |
346 | io_hdr.mx_sb_len = sizeof(sense); | |
347 | io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
348 | io_hdr.dxfer_len = buflen; | |
349 | io_hdr.dxferp = buf; | |
350 | io_hdr.cmdp = inq_cmd; | |
351 | io_hdr.sbp = sense; | |
352 | io_hdr.timeout = DEF_TIMEOUT; | |
353 | io_buf = (void *)&io_hdr; | |
354 | } | |
355 | ||
356 | retval = ioctl(fd, SG_IO, io_buf); | |
357 | if (retval < 0) { | |
3742095b | 358 | if (IN_SET(errno, EINVAL, ENOSYS) && dev_scsi->use_sg == 4) { |
912541b0 KS |
359 | dev_scsi->use_sg = 3; |
360 | goto resend; | |
361 | } | |
56f64d95 | 362 | log_debug_errno(errno, "%s: ioctl failed: %m", dev_scsi->kernel); |
912541b0 KS |
363 | goto error; |
364 | } | |
365 | ||
366 | if (dev_scsi->use_sg == 4) | |
367 | retval = sg_err_category4(udev, io_buf); | |
368 | else | |
369 | retval = sg_err_category3(udev, io_buf); | |
370 | ||
371 | switch (retval) { | |
372 | case SG_ERR_CAT_NOTSUPPORTED: | |
373 | buf[1] = 0; | |
374 | /* Fallthrough */ | |
375 | case SG_ERR_CAT_CLEAN: | |
376 | case SG_ERR_CAT_RECOVERED: | |
377 | retval = 0; | |
378 | break; | |
379 | ||
380 | default: | |
381 | if (dev_scsi->use_sg == 4) | |
382 | retval = scsi_dump_v4(udev, dev_scsi, io_buf); | |
383 | else | |
384 | retval = scsi_dump(udev, dev_scsi, io_buf); | |
385 | } | |
386 | ||
387 | if (!retval) { | |
388 | retval = buflen; | |
389 | } else if (retval > 0) { | |
baa30fbc | 390 | if (--retry > 0) |
912541b0 | 391 | goto resend; |
912541b0 KS |
392 | retval = -1; |
393 | } | |
c521693b | 394 | |
01f950e2 | 395 | error: |
912541b0 | 396 | if (retval < 0) |
9f6445e3 | 397 | log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.", |
461577ee | 398 | dev_scsi->kernel, evpd, page); |
062db23d | 399 | |
912541b0 | 400 | return retval; |
c521693b GKH |
401 | } |
402 | ||
01f950e2 | 403 | /* Get list of supported EVPD pages */ |
7d563a17 | 404 | static int do_scsi_page0_inquiry(struct udev *udev, |
912541b0 KS |
405 | struct scsi_id_device *dev_scsi, int fd, |
406 | unsigned char *buffer, unsigned int len) | |
c521693b | 407 | { |
912541b0 KS |
408 | int retval; |
409 | ||
29804cc1 | 410 | memzero(buffer, len); |
912541b0 KS |
411 | retval = scsi_inquiry(udev, dev_scsi, fd, 1, 0x0, buffer, len); |
412 | if (retval < 0) | |
413 | return 1; | |
414 | ||
415 | if (buffer[1] != 0) { | |
9f6445e3 | 416 | log_debug("%s: page 0 not available.", dev_scsi->kernel); |
912541b0 KS |
417 | return 1; |
418 | } | |
419 | if (buffer[3] > len) { | |
461577ee | 420 | log_debug("%s: page 0 buffer too long %d", dev_scsi->kernel, buffer[3]); |
912541b0 KS |
421 | return 1; |
422 | } | |
423 | ||
424 | /* | |
425 | * Following check is based on code once included in the 2.5.x | |
426 | * kernel. | |
427 | * | |
428 | * Some ill behaved devices return the standard inquiry here | |
429 | * rather than the evpd data, snoop the data to verify. | |
430 | */ | |
431 | if (buffer[3] > MODEL_LENGTH) { | |
432 | /* | |
433 | * If the vendor id appears in the page assume the page is | |
434 | * invalid. | |
435 | */ | |
641906e9 | 436 | if (strneq((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) { |
9f6445e3 | 437 | log_debug("%s: invalid page0 data", dev_scsi->kernel); |
912541b0 KS |
438 | return 1; |
439 | } | |
440 | } | |
441 | return 0; | |
c521693b GKH |
442 | } |
443 | ||
444 | /* | |
445 | * The caller checks that serial is long enough to include the vendor + | |
446 | * model. | |
447 | */ | |
7d563a17 | 448 | static int prepend_vendor_model(struct udev *udev, |
912541b0 | 449 | struct scsi_id_device *dev_scsi, char *serial) |
c521693b | 450 | { |
912541b0 KS |
451 | int ind; |
452 | ||
453 | strncpy(serial, dev_scsi->vendor, VENDOR_LENGTH); | |
454 | strncat(serial, dev_scsi->model, MODEL_LENGTH); | |
455 | ind = strlen(serial); | |
456 | ||
457 | /* | |
458 | * This is not a complete check, since we are using strncat/cpy | |
459 | * above, ind will never be too large. | |
460 | */ | |
461 | if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) { | |
9f6445e3 | 462 | log_debug("%s: expected length %d, got length %d", |
461577ee | 463 | dev_scsi->kernel, (VENDOR_LENGTH + MODEL_LENGTH), ind); |
912541b0 KS |
464 | return -1; |
465 | } | |
466 | return ind; | |
c521693b GKH |
467 | } |
468 | ||
bb061708 | 469 | /* |
c521693b GKH |
470 | * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill |
471 | * serial number. | |
bb061708 | 472 | */ |
7d563a17 | 473 | static int check_fill_0x83_id(struct udev *udev, |
912541b0 KS |
474 | struct scsi_id_device *dev_scsi, |
475 | unsigned char *page_83, | |
476 | const struct scsi_id_search_values | |
477 | *id_search, char *serial, char *serial_short, | |
478 | int max_len, char *wwn, | |
479 | char *wwn_vendor_extension, char *tgpt_group) | |
c521693b | 480 | { |
912541b0 KS |
481 | int i, j, s, len; |
482 | ||
483 | /* | |
484 | * ASSOCIATION must be with the device (value 0) | |
485 | * or with the target port for SCSI_ID_TGTPORT | |
486 | */ | |
487 | if ((page_83[1] & 0x30) == 0x10) { | |
488 | if (id_search->id_type != SCSI_ID_TGTGROUP) | |
489 | return 1; | |
1f6b4113 | 490 | } else if ((page_83[1] & 0x30) != 0) |
912541b0 | 491 | return 1; |
912541b0 KS |
492 | |
493 | if ((page_83[1] & 0x0f) != id_search->id_type) | |
494 | return 1; | |
495 | ||
496 | /* | |
497 | * Possibly check NAA sub-type. | |
498 | */ | |
499 | if ((id_search->naa_type != SCSI_ID_NAA_DONT_CARE) && | |
500 | (id_search->naa_type != (page_83[4] & 0xf0) >> 4)) | |
501 | return 1; | |
502 | ||
503 | /* | |
504 | * Check for matching code set - ASCII or BINARY. | |
505 | */ | |
506 | if ((page_83[0] & 0x0f) != id_search->code_set) | |
507 | return 1; | |
508 | ||
509 | /* | |
510 | * page_83[3]: identifier length | |
511 | */ | |
512 | len = page_83[3]; | |
513 | if ((page_83[0] & 0x0f) != SCSI_ID_ASCII) | |
514 | /* | |
515 | * If not ASCII, use two bytes for each binary value. | |
516 | */ | |
517 | len *= 2; | |
518 | ||
519 | /* | |
520 | * Add one byte for the NUL termination, and one for the id_type. | |
521 | */ | |
522 | len += 2; | |
523 | if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC) | |
524 | len += VENDOR_LENGTH + MODEL_LENGTH; | |
525 | ||
526 | if (max_len < len) { | |
9f6445e3 | 527 | log_debug("%s: length %d too short - need %d", |
461577ee | 528 | dev_scsi->kernel, max_len, len); |
912541b0 KS |
529 | return 1; |
530 | } | |
531 | ||
532 | if (id_search->id_type == SCSI_ID_TGTGROUP && tgpt_group != NULL) { | |
533 | unsigned int group; | |
534 | ||
535 | group = ((unsigned int)page_83[6] << 8) | page_83[7]; | |
536 | sprintf(tgpt_group,"%x", group); | |
537 | return 1; | |
538 | } | |
539 | ||
540 | serial[0] = hex_str[id_search->id_type]; | |
541 | ||
542 | /* | |
543 | * For SCSI_ID_VENDOR_SPECIFIC prepend the vendor and model before | |
544 | * the id since it is not unique across all vendors and models, | |
545 | * this differs from SCSI_ID_T10_VENDOR, where the vendor is | |
546 | * included in the identifier. | |
547 | */ | |
548 | if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC) | |
baa30fbc | 549 | if (prepend_vendor_model(udev, dev_scsi, &serial[1]) < 0) |
912541b0 | 550 | return 1; |
912541b0 KS |
551 | |
552 | i = 4; /* offset to the start of the identifier */ | |
553 | s = j = strlen(serial); | |
554 | if ((page_83[0] & 0x0f) == SCSI_ID_ASCII) { | |
555 | /* | |
556 | * ASCII descriptor. | |
557 | */ | |
558 | while (i < (4 + page_83[3])) | |
559 | serial[j++] = page_83[i++]; | |
560 | } else { | |
561 | /* | |
562 | * Binary descriptor, convert to ASCII, using two bytes of | |
563 | * ASCII for each byte in the page_83. | |
564 | */ | |
565 | while (i < (4 + page_83[3])) { | |
566 | serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4]; | |
567 | serial[j++] = hex_str[page_83[i] & 0x0f]; | |
568 | i++; | |
569 | } | |
570 | } | |
571 | ||
572 | strcpy(serial_short, &serial[s]); | |
573 | ||
574 | if (id_search->id_type == SCSI_ID_NAA && wwn != NULL) { | |
575 | strncpy(wwn, &serial[s], 16); | |
ece174c5 | 576 | if (wwn_vendor_extension != NULL) |
912541b0 | 577 | strncpy(wwn_vendor_extension, &serial[s + 16], 16); |
912541b0 KS |
578 | } |
579 | ||
580 | return 0; | |
c521693b GKH |
581 | } |
582 | ||
cbba4a54 | 583 | /* Extract the raw binary from VPD 0x83 pre-SPC devices */ |
7d563a17 | 584 | static int check_fill_0x83_prespc3(struct udev *udev, |
912541b0 KS |
585 | struct scsi_id_device *dev_scsi, |
586 | unsigned char *page_83, | |
587 | const struct scsi_id_search_values | |
588 | *id_search, char *serial, char *serial_short, int max_len) | |
cbba4a54 | 589 | { |
912541b0 KS |
590 | int i, j; |
591 | ||
912541b0 KS |
592 | serial[0] = hex_str[id_search->id_type]; |
593 | /* serial has been memset to zero before */ | |
594 | j = strlen(serial); /* j = 1; */ | |
595 | ||
596 | for (i = 0; (i < page_83[3]) && (j < max_len-3); ++i) { | |
597 | serial[j++] = hex_str[(page_83[4+i] & 0xf0) >> 4]; | |
598 | serial[j++] = hex_str[ page_83[4+i] & 0x0f]; | |
599 | } | |
600 | serial[max_len-1] = 0; | |
601 | strncpy(serial_short, serial, max_len-1); | |
602 | return 0; | |
cbba4a54 KG |
603 | } |
604 | ||
605 | ||
01f950e2 | 606 | /* Get device identification VPD page */ |
7d563a17 | 607 | static int do_scsi_page83_inquiry(struct udev *udev, |
912541b0 KS |
608 | struct scsi_id_device *dev_scsi, int fd, |
609 | char *serial, char *serial_short, int len, | |
610 | char *unit_serial_number, char *wwn, | |
611 | char *wwn_vendor_extension, char *tgpt_group) | |
c521693b | 612 | { |
912541b0 KS |
613 | int retval; |
614 | unsigned int id_ind, j; | |
615 | unsigned char page_83[SCSI_INQ_BUFF_LEN]; | |
c521693b | 616 | |
912541b0 | 617 | /* also pick up the page 80 serial number */ |
4e9fdfcc DZ |
618 | do_scsi_page80_inquiry(udev, dev_scsi, fd, NULL, unit_serial_number, MAX_SERIAL_LEN); |
619 | ||
29804cc1 | 620 | memzero(page_83, SCSI_INQ_BUFF_LEN); |
912541b0 KS |
621 | retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_83, page_83, |
622 | SCSI_INQ_BUFF_LEN); | |
623 | if (retval < 0) | |
624 | return 1; | |
625 | ||
626 | if (page_83[1] != PAGE_83) { | |
9f6445e3 | 627 | log_debug("%s: Invalid page 0x83", dev_scsi->kernel); |
912541b0 KS |
628 | return 1; |
629 | } | |
630 | ||
631 | /* | |
632 | * XXX Some devices (IBM 3542) return all spaces for an identifier if | |
633 | * the LUN is not actually configured. This leads to identifiers of | |
634 | * the form: "1 ". | |
635 | */ | |
636 | ||
637 | /* | |
638 | * Model 4, 5, and (some) model 6 EMC Symmetrix devices return | |
639 | * a page 83 reply according to SCSI-2 format instead of SPC-2/3. | |
640 | * | |
641 | * The SCSI-2 page 83 format returns an IEEE WWN in binary | |
642 | * encoded hexi-decimal in the 16 bytes following the initial | |
643 | * 4-byte page 83 reply header. | |
644 | * | |
645 | * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part | |
646 | * of an Identification descriptor. The 3rd byte of the first | |
647 | * Identification descriptor is a reserved (BSZ) byte field. | |
648 | * | |
649 | * Reference the 7th byte of the page 83 reply to determine | |
650 | * whether the reply is compliant with SCSI-2 or SPC-2/3 | |
651 | * specifications. A zero value in the 7th byte indicates | |
652 | * an SPC-2/3 conformant reply, (i.e., the reserved field of the | |
653 | * first Identification descriptor). This byte will be non-zero | |
654 | * for a SCSI-2 conformant page 83 reply from these EMC | |
655 | * Symmetrix models since the 7th byte of the reply corresponds | |
656 | * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is, | |
657 | * 0x006048. | |
658 | */ | |
659 | ||
660 | if (page_83[6] != 0) | |
661 | return check_fill_0x83_prespc3(udev, | |
662 | dev_scsi, page_83, id_search_list, | |
663 | serial, serial_short, len); | |
664 | ||
665 | /* | |
666 | * Search for a match in the prioritized id_search_list - since WWN ids | |
4e9fdfcc | 667 | * come first we can pick up the WWN in check_fill_0x83_id(). |
912541b0 KS |
668 | */ |
669 | for (id_ind = 0; | |
670 | id_ind < sizeof(id_search_list)/sizeof(id_search_list[0]); | |
671 | id_ind++) { | |
672 | /* | |
673 | * Examine each descriptor returned. There is normally only | |
674 | * one or a small number of descriptors. | |
675 | */ | |
676 | for (j = 4; j <= (unsigned int)page_83[3] + 3; j += page_83[j + 3] + 4) { | |
677 | retval = check_fill_0x83_id(udev, | |
678 | dev_scsi, &page_83[j], | |
679 | &id_search_list[id_ind], | |
680 | serial, serial_short, len, | |
681 | wwn, wwn_vendor_extension, | |
682 | tgpt_group); | |
baa30fbc | 683 | if (!retval) |
912541b0 | 684 | return retval; |
baa30fbc | 685 | else if (retval < 0) |
912541b0 | 686 | return retval; |
912541b0 KS |
687 | } |
688 | } | |
689 | return 1; | |
c521693b GKH |
690 | } |
691 | ||
50be1401 EG |
692 | /* |
693 | * Get device identification VPD page for older SCSI-2 device which is not | |
694 | * compliant with either SPC-2 or SPC-3 format. | |
695 | * | |
696 | * Return the hard coded error code value 2 if the page 83 reply is not | |
697 | * conformant to the SCSI-2 format. | |
698 | */ | |
7d563a17 | 699 | static int do_scsi_page83_prespc3_inquiry(struct udev *udev, |
912541b0 KS |
700 | struct scsi_id_device *dev_scsi, int fd, |
701 | char *serial, char *serial_short, int len) | |
50be1401 | 702 | { |
912541b0 KS |
703 | int retval; |
704 | int i, j; | |
705 | unsigned char page_83[SCSI_INQ_BUFF_LEN]; | |
706 | ||
29804cc1 | 707 | memzero(page_83, SCSI_INQ_BUFF_LEN); |
912541b0 KS |
708 | retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN); |
709 | if (retval < 0) | |
710 | return 1; | |
711 | ||
712 | if (page_83[1] != PAGE_83) { | |
9f6445e3 | 713 | log_debug("%s: Invalid page 0x83", dev_scsi->kernel); |
912541b0 KS |
714 | return 1; |
715 | } | |
716 | /* | |
717 | * Model 4, 5, and (some) model 6 EMC Symmetrix devices return | |
718 | * a page 83 reply according to SCSI-2 format instead of SPC-2/3. | |
719 | * | |
720 | * The SCSI-2 page 83 format returns an IEEE WWN in binary | |
721 | * encoded hexi-decimal in the 16 bytes following the initial | |
722 | * 4-byte page 83 reply header. | |
723 | * | |
724 | * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part | |
725 | * of an Identification descriptor. The 3rd byte of the first | |
726 | * Identification descriptor is a reserved (BSZ) byte field. | |
727 | * | |
728 | * Reference the 7th byte of the page 83 reply to determine | |
729 | * whether the reply is compliant with SCSI-2 or SPC-2/3 | |
730 | * specifications. A zero value in the 7th byte indicates | |
731 | * an SPC-2/3 conformant reply, (i.e., the reserved field of the | |
732 | * first Identification descriptor). This byte will be non-zero | |
733 | * for a SCSI-2 conformant page 83 reply from these EMC | |
734 | * Symmetrix models since the 7th byte of the reply corresponds | |
735 | * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is, | |
736 | * 0x006048. | |
737 | */ | |
738 | if (page_83[6] == 0) | |
739 | return 2; | |
740 | ||
741 | serial[0] = hex_str[id_search_list[0].id_type]; | |
742 | /* | |
743 | * The first four bytes contain data, not a descriptor. | |
744 | */ | |
745 | i = 4; | |
746 | j = strlen(serial); | |
747 | /* | |
748 | * Binary descriptor, convert to ASCII, | |
749 | * using two bytes of ASCII for each byte | |
750 | * in the page_83. | |
751 | */ | |
752 | while (i < (page_83[3]+4)) { | |
753 | serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4]; | |
754 | serial[j++] = hex_str[page_83[i] & 0x0f]; | |
755 | i++; | |
756 | } | |
912541b0 | 757 | return 0; |
50be1401 EG |
758 | } |
759 | ||
01f950e2 | 760 | /* Get unit serial number VPD page */ |
7d563a17 | 761 | static int do_scsi_page80_inquiry(struct udev *udev, |
912541b0 KS |
762 | struct scsi_id_device *dev_scsi, int fd, |
763 | char *serial, char *serial_short, int max_len) | |
c521693b | 764 | { |
912541b0 KS |
765 | int retval; |
766 | int ser_ind; | |
767 | int i; | |
768 | int len; | |
769 | unsigned char buf[SCSI_INQ_BUFF_LEN]; | |
770 | ||
29804cc1 | 771 | memzero(buf, SCSI_INQ_BUFF_LEN); |
912541b0 KS |
772 | retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN); |
773 | if (retval < 0) | |
774 | return retval; | |
775 | ||
776 | if (buf[1] != PAGE_80) { | |
9f6445e3 | 777 | log_debug("%s: Invalid page 0x80", dev_scsi->kernel); |
912541b0 KS |
778 | return 1; |
779 | } | |
780 | ||
781 | len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3]; | |
782 | if (max_len < len) { | |
9f6445e3 | 783 | log_debug("%s: length %d too short - need %d", |
461577ee | 784 | dev_scsi->kernel, max_len, len); |
912541b0 KS |
785 | return 1; |
786 | } | |
787 | /* | |
788 | * Prepend 'S' to avoid unlikely collision with page 0x83 vendor | |
789 | * specific type where we prepend '0' + vendor + model. | |
790 | */ | |
4e9fdfcc DZ |
791 | len = buf[3]; |
792 | if (serial != NULL) { | |
793 | serial[0] = 'S'; | |
794 | ser_ind = prepend_vendor_model(udev, dev_scsi, &serial[1]); | |
795 | if (ser_ind < 0) | |
796 | return 1; | |
b929bf04 | 797 | ser_ind++; /* for the leading 'S' */ |
4e9fdfcc DZ |
798 | for (i = 4; i < len + 4; i++, ser_ind++) |
799 | serial[ser_ind] = buf[i]; | |
800 | } | |
801 | if (serial_short != NULL) { | |
802 | memcpy(serial_short, &buf[4], len); | |
803 | serial_short[len] = '\0'; | |
804 | } | |
912541b0 | 805 | return 0; |
c521693b GKH |
806 | } |
807 | ||
7d563a17 | 808 | int scsi_std_inquiry(struct udev *udev, |
912541b0 | 809 | struct scsi_id_device *dev_scsi, const char *devname) |
87cf9f5a | 810 | { |
912541b0 KS |
811 | int fd; |
812 | unsigned char buf[SCSI_INQ_BUFF_LEN]; | |
813 | struct stat statbuf; | |
814 | int err = 0; | |
815 | ||
c8a202b7 | 816 | fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC); |
912541b0 | 817 | if (fd < 0) { |
56f64d95 | 818 | log_debug_errno(errno, "scsi_id: cannot open %s: %m", devname); |
912541b0 KS |
819 | return 1; |
820 | } | |
821 | ||
822 | if (fstat(fd, &statbuf) < 0) { | |
56f64d95 | 823 | log_debug_errno(errno, "scsi_id: cannot stat %s: %m", devname); |
912541b0 KS |
824 | err = 2; |
825 | goto out; | |
826 | } | |
827 | sprintf(dev_scsi->kernel,"%d:%d", major(statbuf.st_rdev), | |
828 | minor(statbuf.st_rdev)); | |
829 | ||
29804cc1 | 830 | memzero(buf, SCSI_INQ_BUFF_LEN); |
912541b0 KS |
831 | err = scsi_inquiry(udev, dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN); |
832 | if (err < 0) | |
833 | goto out; | |
834 | ||
835 | err = 0; | |
836 | memcpy(dev_scsi->vendor, buf + 8, 8); | |
837 | dev_scsi->vendor[8] = '\0'; | |
838 | memcpy(dev_scsi->model, buf + 16, 16); | |
839 | dev_scsi->model[16] = '\0'; | |
840 | memcpy(dev_scsi->revision, buf + 32, 4); | |
841 | dev_scsi->revision[4] = '\0'; | |
842 | sprintf(dev_scsi->type,"%x", buf[0] & 0x1f); | |
87cf9f5a | 843 | |
24dabba5 | 844 | out: |
912541b0 KS |
845 | close(fd); |
846 | return err; | |
87cf9f5a HR |
847 | } |
848 | ||
7d563a17 | 849 | int scsi_get_serial(struct udev *udev, |
912541b0 KS |
850 | struct scsi_id_device *dev_scsi, const char *devname, |
851 | int page_code, int len) | |
c521693b | 852 | { |
912541b0 KS |
853 | unsigned char page0[SCSI_INQ_BUFF_LEN]; |
854 | int fd = -1; | |
855 | int cnt; | |
856 | int ind; | |
857 | int retval; | |
858 | ||
29804cc1 | 859 | memzero(dev_scsi->serial, len); |
ef309a68 | 860 | initialize_srand(); |
912541b0 KS |
861 | for (cnt = 20; cnt > 0; cnt--) { |
862 | struct timespec duration; | |
863 | ||
c8a202b7 | 864 | fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC); |
912541b0 KS |
865 | if (fd >= 0 || errno != EBUSY) |
866 | break; | |
867 | duration.tv_sec = 0; | |
868 | duration.tv_nsec = (200 * 1000 * 1000) + (rand() % 100 * 1000 * 1000); | |
869 | nanosleep(&duration, NULL); | |
870 | } | |
871 | if (fd < 0) | |
872 | return 1; | |
873 | ||
874 | if (page_code == PAGE_80) { | |
875 | if (do_scsi_page80_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) { | |
876 | retval = 1; | |
877 | goto completed; | |
878 | } else { | |
879 | retval = 0; | |
880 | goto completed; | |
881 | } | |
882 | } else if (page_code == PAGE_83) { | |
883 | if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) { | |
884 | retval = 1; | |
885 | goto completed; | |
886 | } else { | |
887 | retval = 0; | |
888 | goto completed; | |
889 | } | |
890 | } else if (page_code == PAGE_83_PRE_SPC3) { | |
891 | retval = do_scsi_page83_prespc3_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len); | |
892 | if (retval) { | |
893 | /* | |
894 | * Fallback to servicing a SPC-2/3 compliant page 83 | |
895 | * inquiry if the page 83 reply format does not | |
896 | * conform to pre-SPC3 expectations. | |
897 | */ | |
898 | if (retval == 2) { | |
899 | if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) { | |
900 | retval = 1; | |
901 | goto completed; | |
902 | } else { | |
903 | retval = 0; | |
904 | goto completed; | |
905 | } | |
906 | } | |
907 | else { | |
908 | retval = 1; | |
909 | goto completed; | |
910 | } | |
911 | } else { | |
912 | retval = 0; | |
913 | goto completed; | |
914 | } | |
915 | } else if (page_code != 0x00) { | |
9f6445e3 | 916 | log_debug("%s: unsupported page code 0x%d", dev_scsi->kernel, page_code); |
ab261e12 LN |
917 | retval = 1; |
918 | goto completed; | |
912541b0 KS |
919 | } |
920 | ||
921 | /* | |
922 | * Get page 0, the page of the pages. By default, try from best to | |
923 | * worst of supported pages: 0x83 then 0x80. | |
924 | */ | |
925 | if (do_scsi_page0_inquiry(udev, dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) { | |
926 | /* | |
927 | * Don't try anything else. Black list if a specific page | |
928 | * should be used for this vendor+model, or maybe have an | |
929 | * optional fall-back to page 0x80 or page 0x83. | |
930 | */ | |
931 | retval = 1; | |
932 | goto completed; | |
933 | } | |
934 | ||
912541b0 KS |
935 | for (ind = 4; ind <= page0[3] + 3; ind++) |
936 | if (page0[ind] == PAGE_83) | |
937 | if (!do_scsi_page83_inquiry(udev, dev_scsi, fd, | |
938 | dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) { | |
939 | /* | |
940 | * Success | |
941 | */ | |
942 | retval = 0; | |
943 | goto completed; | |
944 | } | |
945 | ||
946 | for (ind = 4; ind <= page0[3] + 3; ind++) | |
947 | if (page0[ind] == PAGE_80) | |
948 | if (!do_scsi_page80_inquiry(udev, dev_scsi, fd, | |
949 | dev_scsi->serial, dev_scsi->serial_short, len)) { | |
950 | /* | |
951 | * Success | |
952 | */ | |
953 | retval = 0; | |
954 | goto completed; | |
955 | } | |
956 | retval = 1; | |
24dabba5 | 957 | |
c521693b | 958 | completed: |
912541b0 KS |
959 | close(fd); |
960 | return retval; | |
c521693b | 961 | } |