atactl.c revision 1.3 1 /* $NetBSD: atactl.c,v 1.3 1998/11/23 23:02:58 kenh Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ken Hornstein.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * wdctl(8) - a program to control wd (aka ATA) devices.
41 */
42
43 #include <sys/param.h>
44 #include <sys/ioctl.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <util.h>
53
54 #include <dev/ata/atareg.h>
55 #include <dev/ic/wdcreg.h>
56 #include <sys/ataio.h>
57
58 struct command {
59 const char *cmd_name;
60 void (*cmd_func) __P((int, char *[]));
61 };
62
63 struct bitinfo {
64 u_int bitmask;
65 const char *string;
66 };
67
68 int main __P((int, char *[]));
69 void usage __P((void));
70 void ata_command __P((struct atareq *));
71 void print_bitinfo __P((const char *, u_int, struct bitinfo *));
72
73 int fd; /* file descriptor for device */
74 const char *dvname; /* device name */
75 char dvname_store[MAXPATHLEN]; /* for opendisk(3) */
76 const char *cmdname; /* command user issued */
77
78 extern const char *__progname; /* from crt0.o */
79
80 void device_identify __P((int, char *[]));
81 void device_setidle __P((int, char *[]));
82 void device_idle __P((int, char *[]));
83 void device_checkpower __P((int, char *[]));
84
85 struct command commands[] = {
86 { "identify", device_identify },
87 { "setidle", device_setidle },
88 { "setstandby", device_setidle },
89 { "idle", device_idle },
90 { "standby", device_idle },
91 { "sleep", device_idle },
92 { "checkpower", device_checkpower },
93 { NULL, NULL },
94 };
95
96 /*
97 * Tables containing bitmasks used for error reporting and
98 * device identification.
99 */
100
101 struct bitinfo ata_caps[] = {
102 { ATA_CAP_STBY, "ATA standby timer values" },
103 { WDC_CAP_IORDY, "IORDY operation" },
104 { WDC_CAP_IORDY_DSBL, "IORDY disabling" },
105 { NULL, NULL },
106 };
107
108 struct bitinfo ata_vers[] = {
109 { WDC_VER_ATA1, "ATA-1" },
110 { WDC_VER_ATA2, "ATA-2" },
111 { WDC_VER_ATA3, "ATA-3" },
112 { WDC_VER_ATA4, "ATA-4" },
113 { NULL, NULL },
114 };
115
116 struct bitinfo ata_cmd_set1[] = {
117 { WDC_CMD1_NOP, "NOP command" },
118 { WDC_CMD1_RB, "READ BUFFER command" },
119 { WDC_CMD1_WB, "WRITE BUFFER command" },
120 { WDC_CMD1_HPA, "Host Protected Area feature set" },
121 { WDC_CMD1_DVRST, "DEVICE RESET command" },
122 { WDC_CMD1_SRV, "SERVICE interrupt" },
123 { WDC_CMD1_RLSE, "release interrupt" },
124 { WDC_CMD1_AHEAD, "look-ahead" },
125 { WDC_CMD1_CACHE, "write cache" },
126 { WDC_CMD1_PKT, "PACKET command feature set" },
127 { WDC_CMD1_PM, "Power Management feature set" },
128 { WDC_CMD1_REMOV, "Removable Media feature set" },
129 { WDC_CMD1_SEC, "Security Mode feature set" },
130 { WDC_CMD1_SMART, "SMART feature set" },
131 { NULL, NULL },
132 };
133
134 struct bitinfo ata_cmd_set2[] = {
135 { WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
136 { ATA_CMD2_APM, "Advanced Power Management feature set" },
137 { ATA_CMD2_CFA, "CFA feature set" },
138 { ATA_CMD2_RWQ, "READ/WRITE DMS QUEUED commands" },
139 { WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
140 { NULL, NULL },
141 };
142
143 int
144 main(argc, argv)
145 int argc;
146 char *argv[];
147 {
148 int i;
149
150 /* Must have at least: device command */
151 if (argc < 3)
152 usage();
153
154 /* Skip program name, get and skip device name and command. */
155 dvname = argv[1];
156 cmdname = argv[2];
157 argv += 3;
158 argc -= 3;
159
160 /*
161 * Open the device
162 */
163 fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
164 if (fd == -1) {
165 if (errno == ENOENT) {
166 /*
167 * Device doesn't exist. Probably trying to open
168 * a device which doesn't use disk semantics for
169 * device name. Try again, specifying "cooked",
170 * which leaves off the "r" in front of the device's
171 * name.
172 */
173 fd = opendisk(dvname, O_RDWR, dvname_store,
174 sizeof(dvname_store), 1);
175 if (fd == -1)
176 err(1, "%s", dvname);
177 }
178 err(1, "%s", dvname);
179 }
180
181 /*
182 * Point the dvname at the actual device name that opendisk() opened.
183 */
184 dvname = dvname_store;
185
186 /* Look up and call the command. */
187 for (i = 0; commands[i].cmd_name != NULL; i++)
188 if (strcmp(cmdname, commands[i].cmd_name) == 0)
189 break;
190 if (commands[i].cmd_name == NULL)
191 errx(1, "unknown command: %s\n", cmdname);
192
193 (*commands[i].cmd_func)(argc, argv);
194 exit(0);
195 }
196
197 void
198 usage()
199 {
200
201 fprintf(stderr, "usage: %s device command [arg [...]]\n",
202 __progname);
203 exit(1);
204 }
205
206 /*
207 * Wrapper that calls ATAIOCCOMMAND and checks for errors
208 */
209
210 void
211 ata_command(req)
212 struct atareq *req;
213 {
214 int error;
215
216 error = ioctl(fd, ATAIOCCOMMAND, req);
217
218 if (error == -1)
219 err(1, "ATAIOCCOMMAND failed");
220
221 switch (req->retsts) {
222
223 case ATACMD_OK:
224 return;
225 case ATACMD_TIMEOUT:
226 fprintf(stderr, "ATA command timed out\n");
227 exit(1);
228 case ATACMD_DF:
229 fprintf(stderr, "ATA device returned a Device Fault\n");
230 exit(1);
231 case ATACMD_ERROR:
232 if (req->error & WDCE_ABRT)
233 fprintf(stderr, "ATA device returned Aborted "
234 "Command\n");
235 else
236 fprintf(stderr, "ATA device returned error register "
237 "%0x\n", req->error);
238 exit(1);
239 default:
240 fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
241 "%d\n", req->retsts);
242 exit(1);
243 }
244 }
245
246 /*
247 * Print out strings associated with particular bitmasks
248 */
249
250 void
251 print_bitinfo(f, bits, binfo)
252 const char *f;
253 u_int bits;
254 struct bitinfo *binfo;
255 {
256
257 for (; binfo->bitmask != NULL; binfo++)
258 if (bits & binfo->bitmask)
259 printf(f, binfo->string);
260 }
261
262 /*
263 * DEVICE COMMANDS
264 */
265
266 /*
267 * device_identify:
268 *
269 * Display the identity of the device
270 */
271 void
272 device_identify(argc, argv)
273 int argc;
274 char *argv[];
275 {
276 struct ataparams *inqbuf;
277 struct atareq req;
278 unsigned char inbuf[DEV_BSIZE];
279 #if BYTE_ORDER == LITTLE_ENDIAN
280 int i;
281 u_int16_t *p;
282 #endif
283
284 /* No arguments. */
285 if (argc != 0)
286 goto usage;
287
288 memset(&inbuf, 0, sizeof(inbuf));
289 memset(&req, 0, sizeof(req));
290
291 inqbuf = (struct ataparams *) inbuf;
292
293 req.flags = ATACMD_READ;
294 req.command = WDCC_IDENTIFY;
295 req.databuf = (caddr_t) inbuf;
296 req.datalen = sizeof(inbuf);
297 req.timeout = 1000;
298
299 ata_command(&req);
300
301 #if BYTE_ORDER == LITTLE_ENDIAN
302 /*
303 * On little endian machines, we need to shuffle the string
304 * byte order. However, we don't have to do this for NEC or
305 * Mitsumi ATAPI devices
306 */
307
308 if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
309 ((inqbuf->atap_model[0] == 'N' &&
310 inqbuf->atap_model[1] == 'E') ||
311 (inqbuf->atap_model[0] == 'F' &&
312 inqbuf->atap_model[1] == 'X')))) {
313 for (i = 0 ; i < sizeof(inqbuf->atap_model); i += 2) {
314 p = (u_short *) (inqbuf->atap_model + i);
315 *p = ntohs(*p);
316 }
317 for (i = 0 ; i < sizeof(inqbuf->atap_serial); i += 2) {
318 p = (u_short *) (inqbuf->atap_serial + i);
319 *p = ntohs(*p);
320 }
321 for (i = 0 ; i < sizeof(inqbuf->atap_revision); i += 2) {
322 p = (u_short *) (inqbuf->atap_revision + i);
323 *p = ntohs(*p);
324 }
325 }
326 #endif
327
328 /*
329 * Strip blanks off of the info strings. Yuck, I wish this was
330 * cleaner.
331 */
332
333 if (inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] == ' ') {
334 inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] = '\0';
335 while (inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] == ' ')
336 inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] = '\0';
337 }
338
339 if (inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] == ' ') {
340 inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] = '\0';
341 while (inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] == ' ')
342 inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] = '\0';
343 }
344
345 if (inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] == ' ') {
346 inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] = '\0';
347 while (inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] == ' ')
348 inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] = '\0';
349 }
350
351 printf("Model: %.*s, Rev: %.*s, Serial #: %.*s\n",
352 (int) sizeof(inqbuf->atap_model), inqbuf->atap_model,
353 (int) sizeof(inqbuf->atap_revision), inqbuf->atap_revision,
354 (int) sizeof(inqbuf->atap_serial), inqbuf->atap_serial);
355
356 printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
357 "ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
358 "removable");
359
360 if ((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == 0)
361 printf("Cylinders: %d, heads: %d, sec/track: %d, total "
362 "sectors: %d\n", inqbuf->atap_cylinders,
363 inqbuf->atap_heads, inqbuf->atap_sectors,
364 (inqbuf->atap_capacity[1] << 16) |
365 inqbuf->atap_capacity[0]);
366
367 if (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK)
368 printf("Device supports command queue depth of %d\n",
369 inqbuf->atap_queuedepth & 0xf);
370
371 printf("Device capabilities:\n");
372 print_bitinfo("\t%s\n", inqbuf->atap_capabilities1, ata_caps);
373
374 if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
375 printf("Device supports following standards:\n");
376 print_bitinfo("%s ", inqbuf->atap_ata_major, ata_vers);
377 printf("\n");
378 }
379
380 if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
381 inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
382 printf("Command set support:\n");
383 print_bitinfo("\t%s\n", inqbuf->atap_cmd_set1, ata_cmd_set1);
384 print_bitinfo("\t%s\n", inqbuf->atap_cmd_set2, ata_cmd_set2);
385 }
386
387 if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
388 printf("Command sets/features enabled:\n");
389 print_bitinfo("\t%s\n", inqbuf->atap_cmd_set1 &
390 (WDC_CMD1_SRV | WDC_CMD1_RLSE | WDC_CMD1_AHEAD |
391 WDC_CMD1_CACHE | WDC_CMD1_SEC | WDC_CMD1_SMART),
392 ata_cmd_set1);
393 print_bitinfo("\t%s\n", inqbuf->atap_cmd_set2 &
394 (WDC_CMD2_RMSN | ATA_CMD2_APM), ata_cmd_set2);
395 }
396
397 return;
398
399 usage:
400 fprintf(stderr, "usage: %s device %s\n", __progname, cmdname);
401 exit(1);
402 }
403
404 /*
405 * device idle:
406 *
407 * issue the IDLE IMMEDIATE command to the drive
408 */
409
410 void
411 device_idle(argc, argv)
412 int argc;
413 char *argv[];
414 {
415 struct atareq req;
416
417 /* No arguments. */
418 if (argc != 0)
419 goto usage;
420
421 memset(&req, 0, sizeof(req));
422
423 if (strcmp(cmdname, "idle") == 0)
424 req.command = WDCC_IDLE_IMMED;
425 else if (strcmp(cmdname, "standby") == 0)
426 req.command = WDCC_STANDBY_IMMED;
427 else
428 req.command = WDCC_SLEEP;
429
430 req.timeout = 1000;
431
432 ata_command(&req);
433
434 return;
435 usage:
436 fprintf(stderr, "usage: %s device %s\n", __progname, cmdname);
437 exit(1);
438 }
439
440 /*
441 * Set the idle timer on the disk. Set it for either idle mode or
442 * standby mode, depending on how we were invoked.
443 */
444
445 void
446 device_setidle(argc, argv)
447 int argc;
448 char *argv[];
449 {
450 unsigned long idle;
451 struct atareq req;
452 char *end;
453
454 /* Only one argument */
455 if (argc != 1)
456 goto usage;
457
458 idle = strtoul(argv[0], &end, 0);
459
460 if (*end != '\0') {
461 fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
462 exit(1);
463 }
464
465 if (idle > 19800) {
466 fprintf(stderr, "Idle time has a maximum value of 5.5 "
467 "hours\n");
468 exit(1);
469 }
470
471 if (idle != 0 && idle < 5) {
472 fprintf(stderr, "Idle timer must be at least 5 seconds\n");
473 exit(1);
474 }
475
476 memset(&req, 0, sizeof(req));
477
478 if (idle <= 240*5)
479 req.sec_count = idle / 5;
480 else
481 req.sec_count = idle / (30*60) + 240;
482
483 req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
484 req.timeout = 1000;
485
486 ata_command(&req);
487
488 return;
489
490 usage:
491 fprintf(stderr, "usage; %s device %s idle-time\n", __progname,
492 cmdname);
493 exit(1);
494 }
495
496 /*
497 * Query the device for the current power mode
498 */
499
500 void
501 device_checkpower(argc, argv)
502 int argc;
503 char *argv[];
504 {
505 struct atareq req;
506
507 /* No arguments. */
508 if (argc != 0)
509 goto usage;
510
511 memset(&req, 0, sizeof(req));
512
513 req.command = WDCC_CHECK_PWR;
514 req.timeout = 1000;
515 req.flags = ATACMD_READREG;
516
517 ata_command(&req);
518
519 printf("Current power status: ");
520
521 switch (req.sec_count) {
522 case 0x00:
523 printf("Standby mode\n");
524 break;
525 case 0x80:
526 printf("Idle mode\n");
527 break;
528 case 0xff:
529 printf("Active mode\n");
530 break;
531 default:
532 printf("Unknown power code (%02x)\n", req.sec_count);
533 }
534
535 return;
536 usage:
537 fprintf(stderr, "usage: %s device %s\n", __progname, cmdname);
538 exit(1);
539 }
540