2 * Copyright (c) 2000 - 2006 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
46 case ATA_PIO: return "BIOSPIO";
47 case ATA_PIO0: return "PIO0";
48 case ATA_PIO1: return "PIO1";
49 case ATA_PIO2: return "PIO2";
50 case ATA_PIO3: return "PIO3";
51 case ATA_PIO4: return "PIO4";
52 case ATA_WDMA2: return "WDMA2";
53 case ATA_UDMA2: return "UDMA33";
54 case ATA_UDMA4: return "UDMA66";
55 case ATA_UDMA5: return "UDMA100";
56 case ATA_UDMA6: return "UDMA133";
57 case ATA_SA150: return "SATA150";
58 case ATA_SA300: return "SATA300";
59 case ATA_USB: return "USB";
60 case ATA_USB1: return "USB1";
61 case ATA_USB2: return "USB2";
62 case ATA_DMA: return "BIOSDMA";
63 default: return "???";
70 if (!strcasecmp(str, "BIOSPIO")) return ATA_PIO;
71 if (!strcasecmp(str, "PIO0")) return ATA_PIO0;
72 if (!strcasecmp(str, "PIO1")) return ATA_PIO1;
73 if (!strcasecmp(str, "PIO2")) return ATA_PIO2;
74 if (!strcasecmp(str, "PIO3")) return ATA_PIO3;
75 if (!strcasecmp(str, "PIO4")) return ATA_PIO4;
76 if (!strcasecmp(str, "WDMA2")) return ATA_WDMA2;
77 if (!strcasecmp(str, "UDMA2")) return ATA_UDMA2;
78 if (!strcasecmp(str, "UDMA33")) return ATA_UDMA2;
79 if (!strcasecmp(str, "UDMA4")) return ATA_UDMA4;
80 if (!strcasecmp(str, "UDMA66")) return ATA_UDMA4;
81 if (!strcasecmp(str, "UDMA5")) return ATA_UDMA5;
82 if (!strcasecmp(str, "UDMA100")) return ATA_UDMA5;
83 if (!strcasecmp(str, "UDMA6")) return ATA_UDMA6;
84 if (!strcasecmp(str, "UDMA133")) return ATA_UDMA6;
85 if (!strcasecmp(str, "SATA150")) return ATA_SA150;
86 if (!strcasecmp(str, "SATA300")) return ATA_SA300;
87 if (!strcasecmp(str, "USB")) return ATA_USB;
88 if (!strcasecmp(str, "USB1")) return ATA_USB1;
89 if (!strcasecmp(str, "USB2")) return ATA_USB2;
90 if (!strcasecmp(str, "BIOSDMA")) return ATA_DMA;
98 "usage: atacontrol <command> args:\n"
100 " atacontrol info channel\n"
101 " atacontrol attach channel\n"
102 " atacontrol detach channel\n"
103 " atacontrol reinit channel\n"
104 " atacontrol create type [interleave] disk0 ... diskN\n"
105 " atacontrol delete array\n"
106 " atacontrol addspare array disk\n"
107 " atacontrol rebuild array\n"
108 " atacontrol status array\n"
109 " atacontrol mode device [mode]\n"
110 " atacontrol cap device\n"
111 " atacontrol spindown device [seconds]\n"
123 for (bit = 15; bit >= 0; bit--)
130 param_print(struct ata_params *parm)
132 printf("<%.40s/%.8s> ", parm->model, parm->revision);
133 if (parm->satacapabilities && parm->satacapabilities != 0xffff) {
134 if (parm->satacapabilities & ATA_SATA_GEN2)
135 printf("SATA revision 2.x\n");
136 else if (parm->satacapabilities & ATA_SATA_GEN1)
137 printf("SATA revision 1.x\n");
139 printf("Unknown SATA revision\n");
142 printf("ATA/ATAPI revision %d\n", version(parm->version_major));
146 cap_print(struct ata_params *parm)
148 u_int32_t lbasize = (u_int32_t)parm->lba_size_1 |
149 ((u_int32_t)parm->lba_size_2 << 16);
151 u_int64_t lbasize48 = ((u_int64_t)parm->lba_size48_1) |
152 ((u_int64_t)parm->lba_size48_2 << 16) |
153 ((u_int64_t)parm->lba_size48_3 << 32) |
154 ((u_int64_t)parm->lba_size48_4 << 48);
158 if (parm->satacapabilities && parm->satacapabilities != 0xffff) {
159 if (parm->satacapabilities & ATA_SATA_GEN2)
160 printf("SATA revision 2.x\n");
161 else if (parm->satacapabilities & ATA_SATA_GEN1)
162 printf("SATA revision 1.x\n");
164 printf("Unknown SATA revision\n");
167 printf("ATA/ATAPI revision %d\n", version(parm->version_major));
168 printf("device model %.40s\n", parm->model);
169 printf("serial number %.20s\n", parm->serial);
170 printf("firmware revision %.8s\n", parm->revision);
172 printf("cylinders %d\n", parm->cylinders);
173 printf("heads %d\n", parm->heads);
174 printf("sectors/track %d\n", parm->sectors);
176 if (parm->config == ATA_PROTO_CFA ||
177 (parm->support.command2 & ATA_SUPPORT_CFA))
178 printf("CFA supported\n");
180 printf("lba%ssupported ",
181 parm->capabilities1 & ATA_SUPPORT_LBA ? " " : " not ");
183 printf("%d sectors\n", lbasize);
187 printf("lba48%ssupported ",
188 parm->support.command2 & ATA_SUPPORT_ADDRESS48 ? " " : " not ");
190 printf("%ju sectors\n", (uintmax_t)lbasize48);
194 printf("dma%ssupported\n",
195 parm->capabilities1 & ATA_SUPPORT_DMA ? " " : " not ");
197 printf("overlap%ssupported\n",
198 parm->capabilities1 & ATA_SUPPORT_OVERLAP ? " " : " not ");
201 "Support Enable Value Vendor\n");
203 printf("write cache %s %s\n",
204 parm->support.command1 & ATA_SUPPORT_WRITECACHE ? "yes" : "no",
205 parm->enabled.command1 & ATA_SUPPORT_WRITECACHE ? "yes" : "no");
207 printf("read ahead %s %s\n",
208 parm->support.command1 & ATA_SUPPORT_LOOKAHEAD ? "yes" : "no",
209 parm->enabled.command1 & ATA_SUPPORT_LOOKAHEAD ? "yes" : "no");
211 if (parm->satacapabilities && parm->satacapabilities != 0xffff) {
212 printf("Native Command Queuing (NCQ) %s %s"
214 parm->satacapabilities & ATA_SUPPORT_NCQ ?
216 (parm->satacapabilities & ATA_SUPPORT_NCQ) ?
217 ATA_QUEUE_LEN(parm->queue) : 0,
218 (parm->satacapabilities & ATA_SUPPORT_NCQ) ?
219 ATA_QUEUE_LEN(parm->queue) : 0);
221 printf("Tagged Command Queuing (TCQ) %s %s %d/0x%02X\n",
222 parm->support.command2 & ATA_SUPPORT_QUEUED ? "yes" : "no",
223 parm->enabled.command2 & ATA_SUPPORT_QUEUED ? "yes" : "no",
224 ATA_QUEUE_LEN(parm->queue), ATA_QUEUE_LEN(parm->queue));
226 printf("SMART %s %s\n",
227 parm->support.command1 & ATA_SUPPORT_SMART ? "yes" : "no",
228 parm->enabled.command1 & ATA_SUPPORT_SMART ? "yes" : "no");
230 printf("microcode download %s %s\n",
231 parm->support.command2 & ATA_SUPPORT_MICROCODE ? "yes" : "no",
232 parm->enabled.command2 & ATA_SUPPORT_MICROCODE ? "yes" : "no");
234 printf("security %s %s\n",
235 parm->support.command1 & ATA_SUPPORT_SECURITY ? "yes" : "no",
236 parm->enabled.command1 & ATA_SUPPORT_SECURITY ? "yes" : "no");
238 printf("power management %s %s\n",
239 parm->support.command1 & ATA_SUPPORT_POWERMGT ? "yes" : "no",
240 parm->enabled.command1 & ATA_SUPPORT_POWERMGT ? "yes" : "no");
242 printf("advanced power management %s %s %d/0x%02X\n",
243 parm->support.command2 & ATA_SUPPORT_APM ? "yes" : "no",
244 parm->enabled.command2 & ATA_SUPPORT_APM ? "yes" : "no",
245 parm->apm_value, parm->apm_value);
247 printf("automatic acoustic management %s %s "
248 "%d/0x%02X %d/0x%02X\n",
249 parm->support.command2 & ATA_SUPPORT_AUTOACOUSTIC ? "yes" :"no",
250 parm->enabled.command2 & ATA_SUPPORT_AUTOACOUSTIC ? "yes" :"no",
251 ATA_ACOUSTIC_CURRENT(parm->acoustic),
252 ATA_ACOUSTIC_CURRENT(parm->acoustic),
253 ATA_ACOUSTIC_VENDOR(parm->acoustic),
254 ATA_ACOUSTIC_VENDOR(parm->acoustic));
258 ata_cap_print(int fd)
260 struct ata_params params;
262 if (ioctl(fd, IOCATAGPARM, ¶ms) < 0)
263 err(1, "ioctl(IOCATAGPARM)");
268 info_print(int fd, int channel, int prchan)
270 struct ata_ioc_devices devices;
272 devices.channel = channel;
274 if (ioctl(fd, IOCATADEVICES, &devices) < 0) {
276 err(1, "ioctl(IOCATADEVICES)");
280 printf("ATA channel %d:\n", channel);
281 printf("%sMaster: ", prchan ? " " : "");
282 if (*devices.name[0]) {
283 printf("%4.4s ", devices.name[0]);
284 param_print(&devices.params[0]);
287 printf(" no device present\n");
288 printf("%sSlave: ", prchan ? " " : "");
289 if (*devices.name[1]) {
290 printf("%4.4s ", devices.name[1]);
291 param_print(&devices.params[1]);
294 printf(" no device present\n");
298 ata_spindown(int fd, const char *dev, const char *arg)
303 tmo = strtoul(arg, NULL, 0);
304 if (ioctl(fd, IOCATASSPINDOWN, &tmo) < 0)
305 err(1, "ioctl(IOCATASSPINDOWN)");
307 if (ioctl(fd, IOCATAGSPINDOWN, &tmo) < 0)
308 err(1, "ioctl(IOCATAGSPINDOWN)");
310 printf("%s: idle spin down disabled\n", dev);
312 printf("%s: spin down after %d seconds idle\n",
318 open_dev(const char *arg, int mode)
323 if (!(sscanf(arg, "ad%d", &disk) == 1 ||
324 sscanf(arg, "acd%d", &disk) == 1 ||
325 sscanf(arg, "afd%d", &disk) == 1 ||
326 sscanf(arg, "ast%d", &disk) == 1)) {
327 fprintf(stderr, "atacontrol: Invalid device %s\n", arg);
330 sprintf(device, "/dev/%s", arg);
331 if ((fd = open(device, mode)) < 0)
332 err(1, "device not found");
337 ar_arg(const char *arg)
341 if (!(sscanf(arg, "ar%d", &array) == 1)) {
342 fprintf(stderr, "atacontrol: Invalid array %s\n", arg);
349 ata_arg(const char *arg)
353 if (!(sscanf(arg, "ata%d", &channel) == 1)) {
354 fprintf(stderr, "atacontrol: Invalid channel %s\n", arg);
361 main(int argc, char **argv)
363 int fd, mode, channel, array;
368 if (!strcmp(argv[1], "mode") && (argc == 3 || argc == 4)) {
369 fd = open_dev(argv[2], O_RDONLY);
371 mode = str2mode(argv[3]);
373 errx(1, "unknown mode");
374 if (ioctl(fd, IOCATASMODE, &mode) < 0)
375 warn("ioctl(IOCATASMODE)");
377 if (argc == 3 || argc == 4) {
378 if (ioctl(fd, IOCATAGMODE, &mode) < 0)
379 err(1, "ioctl(IOCATAGMODE)");
380 printf("current mode = %s\n", mode2str(mode));
384 if (!strcmp(argv[1], "cap") && argc == 3) {
385 fd = open_dev(argv[2], O_RDONLY);
390 if (!strcmp(argv[1], "spindown") && (argc == 3 || argc == 4)) {
391 fd = open_dev(argv[2], O_RDONLY);
392 ata_spindown(fd, argv[2], argv[3]);
396 if ((fd = open("/dev/ata", O_RDWR)) < 0)
397 err(1, "control device not found");
399 if (!strcmp(argv[1], "list") && argc == 2) {
402 if (ioctl(fd, IOCATAGMAXCHANNEL, &maxchannel) < 0)
403 err(1, "ioctl(IOCATAGMAXCHANNEL)");
404 for (channel = 0; channel < maxchannel; channel++)
405 info_print(fd, channel, 1);
408 if (!strcmp(argv[1], "info") && argc == 3) {
409 channel = ata_arg(argv[2]);
410 info_print(fd, channel, 0);
413 if (!strcmp(argv[1], "detach") && argc == 3) {
414 channel = ata_arg(argv[2]);
415 if (ioctl(fd, IOCATADETACH, &channel) < 0)
416 err(1, "ioctl(IOCATADETACH)");
419 if (!strcmp(argv[1], "attach") && argc == 3) {
420 channel = ata_arg(argv[2]);
421 if (ioctl(fd, IOCATAATTACH, &channel) < 0)
422 err(1, "ioctl(IOCATAATTACH)");
423 info_print(fd, channel, 0);
426 if (!strcmp(argv[1], "reinit") && argc == 3) {
427 channel = ata_arg(argv[2]);
428 if (ioctl(fd, IOCATAREINIT, &channel) < 0)
429 warn("ioctl(IOCATAREINIT)");
430 info_print(fd, channel, 0);
433 if (!strcmp(argv[1], "create")) {
434 int disk, dev, offset;
435 struct ata_ioc_raid_config config;
437 bzero(&config, sizeof(config));
439 if (!strcasecmp(argv[2], "RAID0") ||
440 !strcasecmp(argv[2], "stripe"))
441 config.type = AR_RAID0;
442 if (!strcasecmp(argv[2], "RAID1") ||
443 !strcasecmp(argv[2],"mirror"))
444 config.type = AR_RAID1;
445 if (!strcasecmp(argv[2], "RAID0+1") ||
446 !strcasecmp(argv[2],"RAID10"))
447 config.type = AR_RAID01;
448 if (!strcasecmp(argv[2], "RAID5"))
449 config.type = AR_RAID5;
450 if (!strcasecmp(argv[2], "SPAN"))
451 config.type = AR_SPAN;
452 if (!strcasecmp(argv[2], "JBOD"))
453 config.type = AR_JBOD;
456 fprintf(stderr, "atacontrol: Invalid RAID type %s\n",
458 fprintf(stderr, "atacontrol: Valid RAID types: \n");
459 fprintf(stderr, " stripe | mirror | "
460 "RAID0 | RAID1 | RAID0+1 | RAID5 | "
465 if (config.type == AR_RAID0 ||
466 config.type == AR_RAID01 ||
467 config.type == AR_RAID5) {
469 !sscanf(argv[3], "%d", &config.interleave) == 1) {
471 "atacontrol: Invalid interleave %s\n",
480 for (disk = 0; disk < 16 && (offset + disk) < argc; disk++) {
481 if (!(sscanf(argv[offset + disk], "ad%d", &dev) == 1)) {
483 "atacontrol: Invalid disk %s\n",
484 argv[offset + disk]);
487 config.disks[disk] = dev;
490 if ((config.type == AR_RAID1 || config.type == AR_RAID01) &&
492 fprintf(stderr, "atacontrol: At least 2 disks must be "
497 config.total_disks = disk;
498 if (ioctl(fd, IOCATARAIDCREATE, &config) < 0)
499 err(1, "ioctl(IOCATARAIDCREATE)");
501 printf("ar%d created\n", config.lun);
504 if (!strcmp(argv[1], "delete") && argc == 3) {
505 array = ar_arg(argv[2]);
506 if (ioctl(fd, IOCATARAIDDELETE, &array) < 0)
507 warn("ioctl(IOCATARAIDDELETE)");
510 if (!strcmp(argv[1], "addspare") && argc == 4) {
511 struct ata_ioc_raid_config config;
513 config.lun = ar_arg(argv[2]);
514 if (!(sscanf(argv[3], "ad%d", &config.disks[0]) == 1)) {
516 "atacontrol: Invalid disk %s\n", argv[3]);
519 if (ioctl(fd, IOCATARAIDADDSPARE, &config) < 0)
520 warn("ioctl(IOCATARAIDADDSPARE)");
523 if (!strcmp(argv[1], "rebuild") && argc == 3) {
524 array = ar_arg(argv[2]);
525 if (ioctl(fd, IOCATARAIDREBUILD, &array) < 0)
526 warn("ioctl(IOCATARAIDREBUILD)");
533 if (daemon(0, 1) == -1)
536 snprintf(device, sizeof(device), "/dev/ar%d",
538 if ((arfd = open(device, O_RDONLY)) == -1)
539 err(1, "open %s", device);
540 if ((buffer = malloc(1024 * 1024)) == NULL)
542 while ((len = read(arfd, buffer, 1024 * 1024)) > 0)
548 "atacontrol: ar%d rebuild completed\n",
555 if (!strcmp(argv[1], "status") && argc == 3) {
556 struct ata_ioc_raid_status status;
559 status.lun = ar_arg(argv[2]);
560 if (ioctl(fd, IOCATARAIDSTATUS, &status) < 0)
561 err(1, "ioctl(IOCATARAIDSTATUS)");
563 printf("ar%d: ATA ", status.lun);
564 switch (status.type) {
566 printf("RAID0 stripesize=%d", status.interleave);
572 printf("RAID0+1 stripesize=%d", status.interleave);
575 printf("RAID5 stripesize=%d", status.interleave);
585 switch (status.status) {
589 case AR_READY | AR_DEGRADED:
590 printf("DEGRADED\n");
592 case AR_READY | AR_DEGRADED | AR_REBUILDING:
593 printf("REBUILDING %d%% completed\n",
599 printf(" subdisks:\n");
600 for (i = 0; i < status.total_disks; i++) {
602 lun = status.disks[i].lun;
603 state = status.disks[i].state;
607 printf("ad%-2d ", lun);
608 if (state & AR_DISK_ONLINE)
610 else if (state & AR_DISK_SPARE)
612 else if (state & AR_DISK_PRESENT)