]>
Commit | Line | Data |
---|---|---|
832b75ed | 1 | /* |
4d59bff9 | 2 | * os_netbsd.cpp |
832b75ed GG |
3 | * |
4 | * Home page of code is: http://smartmontools.sourceforge.net | |
5 | * | |
34ad0c5f | 6 | * Copyright (C) 2003-8 Sergey Svishchev <smartmontools-support@lists.sourceforge.net> |
832b75ed GG |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * (for example COPYING); if not, write to the Free | |
15 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | */ | |
18 | ||
19 | #include "config.h" | |
20 | #include "int64.h" | |
21 | #include "atacmds.h" | |
22 | #include "scsicmds.h" | |
23 | #include "utility.h" | |
24 | #include "os_netbsd.h" | |
cfbba5b9 GI |
25 | |
26 | #include <errno.h> | |
832b75ed GG |
27 | #include <unistd.h> |
28 | ||
cfbba5b9 GI |
29 | const char * os_netbsd_cpp_cvsid = "$Id: os_netbsd.cpp 3265 2011-02-21 16:21:14Z chrfranke $" |
30 | OS_NETBSD_H_CVSID; | |
832b75ed GG |
31 | |
32 | /* global variable holding byte count of allocated memory */ | |
33 | extern long long bytes; | |
34 | ||
35 | enum warnings { | |
2127e193 | 36 | BAD_SMART, NO_3WARE, NO_ARECA, MAX_MSG |
832b75ed GG |
37 | }; |
38 | ||
39 | /* Utility function for printing warnings */ | |
40 | void | |
41 | printwarning(int msgNo, const char *extra) | |
42 | { | |
43 | static int printed[] = {0, 0}; | |
44 | static const char *message[] = { | |
45 | "Error: SMART Status command failed.\nPlease get assistance from \n" PACKAGE_HOMEPAGE "\nRegister values returned from SMART Status command are:\n", | |
34ad0c5f | 46 | PACKAGE_STRING " does not currently support twe(4) and twa(4) devices (3ware Escalade, Apache)\n", |
832b75ed GG |
47 | }; |
48 | ||
49 | if (msgNo >= 0 && msgNo <= MAX_MSG) { | |
50 | if (!printed[msgNo]) { | |
51 | printed[msgNo] = 1; | |
52 | pout("%s", message[msgNo]); | |
53 | if (extra) | |
54 | pout("%s", extra); | |
55 | } | |
56 | } | |
57 | return; | |
58 | } | |
59 | ||
60 | static const char *net_dev_prefix = "/dev/"; | |
61 | static const char *net_dev_ata_disk = "wd"; | |
62 | static const char *net_dev_scsi_disk = "sd"; | |
63 | static const char *net_dev_scsi_tape = "enrst"; | |
64 | ||
34ad0c5f | 65 | /* Guess device type (ATA or SCSI) based on device name */ |
832b75ed GG |
66 | int |
67 | guess_device_type(const char *dev_name) | |
68 | { | |
69 | int len; | |
70 | int dev_prefix_len = strlen(net_dev_prefix); | |
71 | ||
72 | if (!dev_name || !(len = strlen(dev_name))) | |
73 | return CONTROLLER_UNKNOWN; | |
74 | ||
75 | if (!strncmp(net_dev_prefix, dev_name, dev_prefix_len)) { | |
76 | if (len <= dev_prefix_len) | |
77 | return CONTROLLER_UNKNOWN; | |
78 | else | |
79 | dev_name += dev_prefix_len; | |
80 | } | |
81 | if (!strncmp(net_dev_ata_disk, dev_name, strlen(net_dev_ata_disk))) | |
82 | return CONTROLLER_ATA; | |
83 | ||
84 | if (!strncmp(net_dev_scsi_disk, dev_name, strlen(net_dev_scsi_disk))) | |
85 | return CONTROLLER_SCSI; | |
86 | ||
87 | if (!strncmp(net_dev_scsi_tape, dev_name, strlen(net_dev_scsi_tape))) | |
88 | return CONTROLLER_SCSI; | |
89 | ||
90 | return CONTROLLER_UNKNOWN; | |
91 | } | |
92 | ||
93 | int | |
94 | get_dev_names(char ***names, const char *prefix) | |
95 | { | |
96 | char *disknames, *p, **mp; | |
97 | int n = 0; | |
98 | int sysctl_mib[2]; | |
99 | size_t sysctl_len; | |
100 | ||
101 | *names = NULL; | |
102 | ||
103 | sysctl_mib[0] = CTL_HW; | |
104 | sysctl_mib[1] = HW_DISKNAMES; | |
105 | if (-1 == sysctl(sysctl_mib, 2, NULL, &sysctl_len, NULL, 0)) { | |
106 | pout("Failed to get value of sysctl `hw.disknames'\n"); | |
107 | return -1; | |
108 | } | |
4d59bff9 | 109 | if (!(disknames = (char *)malloc(sysctl_len))) { |
832b75ed GG |
110 | pout("Out of memory constructing scan device list\n"); |
111 | return -1; | |
112 | } | |
113 | if (-1 == sysctl(sysctl_mib, 2, disknames, &sysctl_len, NULL, 0)) { | |
114 | pout("Failed to get value of sysctl `hw.disknames'\n"); | |
115 | return -1; | |
116 | } | |
117 | if (!(mp = (char **) calloc(strlen(disknames) / 2, sizeof(char *)))) { | |
118 | pout("Out of memory constructing scan device list\n"); | |
119 | return -1; | |
120 | } | |
121 | for (p = strtok(disknames, " "); p; p = strtok(NULL, " ")) { | |
122 | if (strncmp(p, prefix, strlen(prefix))) { | |
123 | continue; | |
124 | } | |
4d59bff9 | 125 | mp[n] = (char *)malloc(strlen(net_dev_prefix) + strlen(p) + 2); |
832b75ed GG |
126 | if (!mp[n]) { |
127 | pout("Out of memory constructing scan device list\n"); | |
128 | return -1; | |
129 | } | |
130 | sprintf(mp[n], "%s%s%c", net_dev_prefix, p, 'a' + getrawpartition()); | |
131 | bytes += strlen(mp[n]) + 1; | |
132 | n++; | |
133 | } | |
134 | ||
4d59bff9 | 135 | mp = (char **)realloc(mp, n * (sizeof(char *))); |
832b75ed GG |
136 | bytes += (n) * (sizeof(char *)); |
137 | *names = mp; | |
138 | return n; | |
139 | } | |
140 | ||
141 | int | |
142 | make_device_names(char ***devlist, const char *name) | |
143 | { | |
144 | if (!strcmp(name, "SCSI")) | |
145 | return get_dev_names(devlist, net_dev_scsi_disk); | |
146 | else if (!strcmp(name, "ATA")) | |
147 | return get_dev_names(devlist, net_dev_ata_disk); | |
148 | else | |
149 | return 0; | |
150 | } | |
151 | ||
152 | int | |
153 | deviceopen(const char *pathname, char *type) | |
154 | { | |
155 | if (!strcmp(type, "SCSI")) { | |
156 | int fd = open(pathname, O_RDWR | O_NONBLOCK); | |
157 | if (fd < 0 && errno == EROFS) | |
158 | fd = open(pathname, O_RDONLY | O_NONBLOCK); | |
159 | return fd; | |
160 | } else if (!strcmp(type, "ATA")) | |
161 | return open(pathname, O_RDWR | O_NONBLOCK); | |
162 | else | |
163 | return -1; | |
164 | } | |
165 | ||
166 | int | |
167 | deviceclose(int fd) | |
168 | { | |
169 | return close(fd); | |
170 | } | |
171 | ||
172 | int | |
173 | marvell_command_interface(int fd, smart_command_set command, int select, char *data) | |
174 | { return -1; } | |
175 | ||
34ad0c5f GG |
176 | int |
177 | highpoint_command_interface(int fd, smart_command_set command, int select, char *data) | |
178 | { return -1; } | |
4d59bff9 | 179 | |
832b75ed GG |
180 | int |
181 | ata_command_interface(int fd, smart_command_set command, int select, char *data) | |
182 | { | |
183 | struct atareq req; | |
184 | unsigned char inbuf[DEV_BSIZE]; | |
185 | int retval, copydata = 0; | |
186 | ||
187 | memset(&req, 0, sizeof(req)); | |
34ad0c5f GG |
188 | req.timeout = 1000; |
189 | ||
832b75ed GG |
190 | memset(&inbuf, 0, sizeof(inbuf)); |
191 | ||
192 | switch (command) { | |
193 | case READ_VALUES: | |
194 | req.flags = ATACMD_READ; | |
195 | req.features = WDSM_RD_DATA; | |
196 | req.command = WDCC_SMART; | |
4d59bff9 | 197 | req.databuf = (char *)inbuf; |
832b75ed GG |
198 | req.datalen = sizeof(inbuf); |
199 | req.cylinder = WDSMART_CYL; | |
832b75ed GG |
200 | copydata = 1; |
201 | break; | |
202 | case READ_THRESHOLDS: | |
203 | req.flags = ATACMD_READ; | |
204 | req.features = WDSM_RD_THRESHOLDS; | |
205 | req.command = WDCC_SMART; | |
4d59bff9 | 206 | req.databuf = (char *)inbuf; |
832b75ed GG |
207 | req.datalen = sizeof(inbuf); |
208 | req.cylinder = WDSMART_CYL; | |
832b75ed GG |
209 | copydata = 1; |
210 | break; | |
211 | case READ_LOG: | |
212 | req.flags = ATACMD_READ; | |
213 | req.features = ATA_SMART_READ_LOG_SECTOR; /* XXX missing from wdcreg.h */ | |
214 | req.command = WDCC_SMART; | |
4d59bff9 | 215 | req.databuf = (char *)inbuf; |
832b75ed GG |
216 | req.datalen = sizeof(inbuf); |
217 | req.cylinder = WDSMART_CYL; | |
218 | req.sec_num = select; | |
219 | req.sec_count = 1; | |
832b75ed GG |
220 | copydata = 1; |
221 | break; | |
222 | case WRITE_LOG: | |
223 | memcpy(inbuf, data, 512); | |
224 | req.flags = ATACMD_WRITE; | |
225 | req.features = ATA_SMART_WRITE_LOG_SECTOR; /* XXX missing from wdcreg.h */ | |
226 | req.command = WDCC_SMART; | |
4d59bff9 | 227 | req.databuf = (char *)inbuf; |
832b75ed GG |
228 | req.datalen = sizeof(inbuf); |
229 | req.cylinder = WDSMART_CYL; | |
230 | req.sec_num = select; | |
231 | req.sec_count = 1; | |
832b75ed GG |
232 | break; |
233 | case IDENTIFY: | |
234 | req.flags = ATACMD_READ; | |
235 | req.command = WDCC_IDENTIFY; | |
4d59bff9 | 236 | req.databuf = (char *)inbuf; |
832b75ed | 237 | req.datalen = sizeof(inbuf); |
832b75ed GG |
238 | copydata = 1; |
239 | break; | |
240 | case PIDENTIFY: | |
241 | req.flags = ATACMD_READ; | |
242 | req.command = ATAPI_IDENTIFY_DEVICE; | |
4d59bff9 | 243 | req.databuf = (char *)inbuf; |
832b75ed | 244 | req.datalen = sizeof(inbuf); |
832b75ed GG |
245 | copydata = 1; |
246 | break; | |
247 | case ENABLE: | |
34ad0c5f | 248 | req.flags = ATACMD_READREG; |
832b75ed GG |
249 | req.features = WDSM_ENABLE_OPS; |
250 | req.command = WDCC_SMART; | |
251 | req.cylinder = WDSMART_CYL; | |
832b75ed GG |
252 | break; |
253 | case DISABLE: | |
34ad0c5f | 254 | req.flags = ATACMD_READREG; |
832b75ed GG |
255 | req.features = WDSM_DISABLE_OPS; |
256 | req.command = WDCC_SMART; | |
257 | req.cylinder = WDSMART_CYL; | |
832b75ed GG |
258 | break; |
259 | case AUTO_OFFLINE: | |
260 | /* NOTE: According to ATAPI 4 and UP, this command is obsolete */ | |
34ad0c5f | 261 | req.flags = ATACMD_READREG; |
832b75ed GG |
262 | req.features = ATA_SMART_AUTO_OFFLINE; /* XXX missing from wdcreg.h */ |
263 | req.command = WDCC_SMART; | |
832b75ed | 264 | req.cylinder = WDSMART_CYL; |
34ad0c5f | 265 | req.sec_count = select; |
832b75ed GG |
266 | break; |
267 | case AUTOSAVE: | |
34ad0c5f | 268 | req.flags = ATACMD_READREG; |
832b75ed GG |
269 | req.features = ATA_SMART_AUTOSAVE; /* XXX missing from wdcreg.h */ |
270 | req.command = WDCC_SMART; | |
271 | req.cylinder = WDSMART_CYL; | |
34ad0c5f | 272 | req.sec_count = select; |
832b75ed GG |
273 | break; |
274 | case IMMEDIATE_OFFLINE: | |
275 | /* NOTE: According to ATAPI 4 and UP, this command is obsolete */ | |
34ad0c5f | 276 | req.flags = ATACMD_READREG; |
832b75ed GG |
277 | req.features = ATA_SMART_IMMEDIATE_OFFLINE; /* XXX missing from wdcreg.h */ |
278 | req.command = WDCC_SMART; | |
832b75ed GG |
279 | req.cylinder = WDSMART_CYL; |
280 | req.sec_num = select; | |
281 | req.sec_count = 1; | |
832b75ed | 282 | break; |
34ad0c5f GG |
283 | case STATUS: /* should return 0 if SMART is enabled at all */ |
284 | case STATUS_CHECK: /* should return 0 if disk's health is ok */ | |
285 | req.flags = ATACMD_READREG; | |
832b75ed GG |
286 | req.features = WDSM_STATUS; |
287 | req.command = WDCC_SMART; | |
288 | req.cylinder = WDSMART_CYL; | |
832b75ed GG |
289 | break; |
290 | case CHECK_POWER_MODE: | |
291 | req.flags = ATACMD_READREG; | |
292 | req.command = WDCC_CHECK_PWR; | |
832b75ed GG |
293 | break; |
294 | default: | |
295 | pout("Unrecognized command %d in ata_command_interface()\n", command); | |
296 | errno = ENOSYS; | |
297 | return -1; | |
298 | } | |
299 | ||
34ad0c5f | 300 | if (command == STATUS_CHECK || command == AUTOSAVE || command == AUTO_OFFLINE) { |
832b75ed GG |
301 | char buf[512]; |
302 | ||
303 | unsigned const short normal = WDSMART_CYL, failed = 0x2cf4; | |
304 | ||
305 | if ((retval = ioctl(fd, ATAIOCCOMMAND, &req))) { | |
306 | perror("Failed command"); | |
307 | return -1; | |
308 | } | |
34ad0c5f GG |
309 | if (req.retsts != ATACMD_OK) { |
310 | return -1; | |
311 | } | |
832b75ed GG |
312 | /* Cyl low and Cyl high unchanged means "Good SMART status" */ |
313 | if (req.cylinder == normal) | |
314 | return 0; | |
315 | ||
316 | /* These values mean "Bad SMART status" */ | |
317 | if (req.cylinder == failed) | |
318 | return 1; | |
319 | ||
320 | /* We haven't gotten output that makes sense; | |
321 | * print out some debugging info */ | |
322 | snprintf(buf, sizeof(buf), | |
323 | "CMD=0x%02x\nFR =0x%02x\nNS =0x%02x\nSC =0x%02x\nCL =0x%02x\nCH =0x%02x\nRETURN =0x%04x\n", | |
324 | (int) req.command, (int) req.features, (int) req.sec_count, (int) req.sec_num, | |
325 | (int) (le16toh(req.cylinder) & 0xff), (int) ((le16toh(req.cylinder) >> 8) & 0xff), | |
326 | (int) req.error); | |
327 | printwarning(BAD_SMART, buf); | |
328 | return 0; | |
329 | } | |
34ad0c5f | 330 | |
832b75ed GG |
331 | if ((retval = ioctl(fd, ATAIOCCOMMAND, &req))) { |
332 | perror("Failed command"); | |
333 | return -1; | |
334 | } | |
34ad0c5f GG |
335 | if (req.retsts != ATACMD_OK) { |
336 | return -1; | |
337 | } | |
338 | ||
832b75ed GG |
339 | if (command == CHECK_POWER_MODE) |
340 | data[0] = req.sec_count; | |
341 | ||
342 | if (copydata) | |
343 | memcpy(data, inbuf, 512); | |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | int | |
349 | escalade_command_interface(int fd, int disknum, int escalade_type, smart_command_set command, int select, char *data) | |
350 | { | |
351 | printwarning(NO_3WARE, NULL); | |
352 | return -1; | |
353 | } | |
354 | ||
2127e193 GI |
355 | int |
356 | areca_command_interface(int fd, int disknum, smart_command_set command, int select, char *data) | |
357 | { | |
358 | printwarning(NO_ARECA, NULL); | |
359 | return -1; | |
360 | } | |
361 | ||
832b75ed GG |
362 | int |
363 | do_scsi_cmnd_io(int fd, struct scsi_cmnd_io * iop, int report) | |
364 | { | |
365 | struct scsireq sc; | |
366 | ||
367 | if (report > 0) { | |
368 | size_t k; | |
369 | ||
370 | const unsigned char *ucp = iop->cmnd; | |
371 | const char *np; | |
372 | ||
373 | np = scsi_get_opcode_name(ucp[0]); | |
374 | pout(" [%s: ", np ? np : "<unknown opcode>"); | |
375 | for (k = 0; k < iop->cmnd_len; ++k) | |
376 | pout("%02x ", ucp[k]); | |
377 | if ((report > 1) && | |
378 | (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) { | |
379 | int trunc = (iop->dxfer_len > 256) ? 1 : 0; | |
380 | ||
381 | pout("]\n Outgoing data, len=%d%s:\n", (int) iop->dxfer_len, | |
382 | (trunc ? " [only first 256 bytes shown]" : "")); | |
383 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len), 1); | |
384 | } else | |
385 | pout("]"); | |
386 | } | |
387 | memset(&sc, 0, sizeof(sc)); | |
388 | memcpy(sc.cmd, iop->cmnd, iop->cmnd_len); | |
389 | sc.cmdlen = iop->cmnd_len; | |
4d59bff9 | 390 | sc.databuf = (char *)iop->dxferp; |
832b75ed GG |
391 | sc.datalen = iop->dxfer_len; |
392 | sc.senselen = iop->max_sense_len; | |
393 | sc.timeout = iop->timeout == 0 ? 60000 : (1000 * iop->timeout); | |
394 | sc.flags = | |
4d59bff9 | 395 | (iop->dxfer_dir == DXFER_NONE ? SCCMD_READ : |
832b75ed GG |
396 | (iop->dxfer_dir == DXFER_FROM_DEVICE ? SCCMD_READ : SCCMD_WRITE)); |
397 | ||
398 | if (ioctl(fd, SCIOCCOMMAND, &sc) < 0) { | |
399 | warn("error sending SCSI ccb"); | |
400 | return -1; | |
401 | } | |
402 | iop->resid = sc.datalen - sc.datalen_used; | |
403 | iop->scsi_status = sc.status; | |
404 | if (iop->sensep) { | |
405 | memcpy(iop->sensep, sc.sense, sc.senselen_used); | |
406 | iop->resp_sense_len = sc.senselen_used; | |
407 | } | |
408 | if (report > 0) { | |
409 | int trunc; | |
410 | ||
411 | pout(" status=0\n"); | |
412 | trunc = (iop->dxfer_len > 256) ? 1 : 0; | |
413 | ||
414 | pout(" Incoming data, len=%d%s:\n", (int) iop->dxfer_len, | |
415 | (trunc ? " [only first 256 bytes shown]" : "")); | |
416 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len), 1); | |
417 | } | |
34ad0c5f GG |
418 | switch (sc.retsts) { |
419 | case SCCMD_OK: | |
420 | return 0; | |
421 | case SCCMD_TIMEOUT: | |
422 | return -ETIMEDOUT; | |
423 | case SCCMD_BUSY: | |
424 | return -EBUSY; | |
425 | default: | |
426 | return -EIO; | |
427 | } | |
832b75ed GG |
428 | } |
429 | ||
430 | /* print examples for smartctl */ | |
431 | void | |
432 | print_smartctl_examples() | |
433 | { | |
434 | char p; | |
435 | ||
436 | p = 'a' + getrawpartition(); | |
437 | printf("=================================================== SMARTCTL EXAMPLES =====\n\n"); | |
438 | #ifdef HAVE_GETOPT_LONG | |
439 | printf( | |
440 | " smartctl -a /dev/wd0%c (Prints all SMART information)\n\n" | |
441 | " smartctl --smart=on --offlineauto=on --saveauto=on /dev/wd0%c\n" | |
442 | " (Enables SMART on first disk)\n\n" | |
443 | " smartctl -t long /dev/wd0%c (Executes extended disk self-test)\n\n" | |
444 | " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/wd0%c\n" | |
445 | " (Prints Self-Test & Attribute errors)\n", | |
446 | p, p, p, p | |
447 | ); | |
448 | #else | |
449 | printf( | |
450 | " smartctl -a /dev/wd0%c (Prints all SMART information)\n" | |
451 | " smartctl -s on -o on -S on /dev/wd0%c (Enables SMART on first disk)\n" | |
452 | " smartctl -t long /dev/wd0%c (Executes extended disk self-test)\n" | |
453 | " smartctl -A -l selftest -q errorsonly /dev/wd0%c" | |
454 | " (Prints Self-Test & Attribute errors)\n", | |
455 | p, p, p, p | |
456 | ); | |
457 | #endif | |
458 | return; | |
459 | } |