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