-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhdparm.c
2278 lines (2167 loc) · 59.6 KB
/
hdparm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* hdparm.c - Command line interface to get/set hard disk parameters */
/* - by Mark Lord (C) 1994-2005 -- freely distributable */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/time.h>
#endif
#if !defined(_WIN32) && !defined(__CYGWIN__)
#include <endian.h>
#include <sys/ioctl.h>
#include <sys/shm.h>
#include <sys/sysmacros.h>
#include <sys/times.h>
#include <sys/mount.h>
#include <linux/types.h>
#include <linux/hdreg.h>
#include <linux/major.h>
#include <asm/byteorder.h>
//#include <endian.h>
#else
#ifndef __CYGWIN__
#include "win32/timer.h"
#endif
#include "win32/fs.h"
#include "win32/hdreg.h"
#include "win32/shm.h"
#include "win32/rawio.h"
#include "win32/version.h"
#define __le16_to_cpus(x) ((void)(x))
#endif
#include "hdparm.h"
extern const char *minor_str[];
#ifndef VERSION
#define VERSION "v6.9"
#endif
#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
#ifndef O_DIRECT
#define O_DIRECT 040000 /* direct disk access, not easily obtained from headers */
#endif
#if !defined(_WIN32) && !defined(__CYGWIN__)
#ifndef CDROM_SELECT_SPEED /* already defined in 2.3.xx kernels and above */
#define CDROM_SELECT_SPEED 0x5322
#endif
#endif
#ifndef BLKGETSIZE64
#define BLKGETSIZE64 _IOR(0x12,114,size_t)
#endif
#define TIMING_BUF_MB 2
#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
char *progname;
static int verbose = 0, noisy = 1, quiet = 0;
static int flagcount = 0, do_flush = 0;
static int do_ctimings, do_timings = 0;
static double ctimings = 0.0, timings = 0.0;
#ifdef HDIO_GET_IDENTITY
static int get_identity = 0;
#endif
#ifdef HDIO_GETGEO
static int get_geom = 0;
#endif
#ifdef BLKRASET
static unsigned long set_fsreadahead= 0, get_fsreadahead= 0, fsreadahead= 0;
#endif
#ifdef BLKROSET
static unsigned long set_readonly = 0, get_readonly = 0, readonly = 0;
#endif
#ifdef HDIO_SET_UNMASKINTR
static unsigned long set_unmask = 0, get_unmask = 0, unmask = 0;
#endif
#ifdef HDIO_SET_MULTCOUNT
static unsigned long set_mult = 0, get_mult = 0, mult = 0;
#endif
#ifdef HDIO_SET_DMA
static unsigned long set_dma = 0, get_dma = 0, dma = 0;
#endif
#ifdef HDIO_GET_QDMA
static unsigned long set_dma_q = 0, get_dma_q = 0, dma_q = 0;
#endif
#ifdef HDIO_SET_NOWERR
static unsigned long set_nowerr = 0, get_nowerr = 0, nowerr = 0;
#endif
#ifdef HDIO_GET_KEEPSETTINGS
static unsigned long set_keep = 0, get_keep = 0, keep = 0;
#endif
#ifdef HDIO_SET_32BIT
static unsigned long set_io32bit = 0, get_io32bit = 0, io32bit = 0;
#endif
#ifdef HDIO_SET_PIO_MODE
static unsigned long set_piomode = 0, noisy_piomode= 0;
int piomode = 0;
#endif
#ifdef HDIO_DRIVE_CMD
static unsigned long set_dkeep = 0, get_dkeep = 0, dkeep = 0;
static unsigned long set_standby = 0, get_standby = 0, standby_requested= 0;
static unsigned long set_xfermode = 0, get_xfermode = 0;
static int xfermode_requested= 0;
static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
static unsigned long set_defects = 0, get_defects = 0, defects = 0;
static unsigned long set_wcache = 0, get_wcache = 0, wcache = 0;
static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
static unsigned long set_seagate = 0, get_seagate = 0;
static unsigned long set_standbynow = 0, get_standbynow = 0;
static unsigned long set_sleepnow = 0, get_sleepnow = 0;
static unsigned long set_powerup_in_standby = 0, get_powerup_in_standby = 0, powerup_in_standby = 0;
static unsigned long get_hitachi_temp = 0;
static unsigned long set_freeze = 0;
#ifndef WIN_SECURITY_FREEZE_LOCK
#define WIN_SECURITY_FREEZE_LOCK 0xF5
#endif
#ifdef IDE_DRIVE_TASK_NO_DATA
static unsigned long security_master = 1, security_mode = 0;
static unsigned long enhanced_erase = 0;
static unsigned long set_security = 0;
#ifndef WIN_SECURITY_SET_PASS
#define WIN_SECURITY_SET_PASS 0xF1
#define WIN_SECURITY_UNLOCK 0xF2
#define WIN_SECURITY_ERASE_PREPARE 0xF3
#define WIN_SECURITY_ERASE_UNIT 0xF4
#define WIN_SECURITY_DISABLE 0xF6
#endif
static unsigned int security_command = WIN_SECURITY_UNLOCK;
static char security_password[33];
#endif // IDE_DRIVE_TASK_NO_DATA
static unsigned long get_powermode = 0;
static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
#endif // HDIO_DRIVE_CMD
#ifdef CDROM_SELECT_SPEED
static unsigned long set_cdromspeed = 0, cdromspeed = 0;
#endif /* CDROM_SELECT_SPEED */
#ifdef HDIO_DRIVE_CMD
static int get_IDentity = 0;
#endif
#ifdef HDIO_UNREGISTER_HWIF
static int unregister_hwif = 0;
static int hwif = 0;
#endif
#ifdef HDIO_SCAN_HWIF
static int scan_hwif = 0;
static int hwif_data = 0;
static int hwif_ctrl = 0;
static int hwif_irq = 0;
#endif
#ifdef HDIO_GET_BUSSTATE
static int set_busstate = 0, get_busstate = 0, busstate = 0;
#endif
#ifdef BLKRRPART
static int reread_partn = 0;
#endif
#if defined(HDIO_GET_ACOUSTIC) || defined(HDIO_DRIVE_CMD)
static int set_acoustic = 0, get_acoustic = 0, acoustic = 0;
#endif
#ifdef HDIO_DRIVE_RESET
static int perform_reset = 0;
#endif /* HDIO_DRIVE_RESET */
#ifdef HDIO_TRISTATE_HWIF
static int perform_tristate = 0, tristate = 0;
#endif /* HDIO_TRISTATE_HWIF */
#ifdef O_NONBLOCK
static int open_flags = O_RDONLY|O_NONBLOCK;
#else
static int open_flags = O_RDONLY;
#endif
#ifdef HDIO_GET_IDENTITY
// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
// then the HDIO_GET_IDENTITY only returned 142 bytes.
// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
// and HDIO_GET_IDENTITY returns 512 bytes. But the latest
// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
// (which they should, but they should just return -EINVAL).
//
// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
// On a really old system, it will not, and we will be confused.
// Too bad, really.
const char *cfg_str[] =
{ "", " HardSect", " SoftSect", " NotMFM",
" HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
" DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
" dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
};
const char *SlowMedFast[] = {"slow", "medium", "fast", "eide", "ata"};
const char *BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
#define YN(b) (((b)==0)?"no":"yes")
static void dmpstr (const char *prefix, unsigned int i, const char *s[], unsigned int maxi)
{
if (i > maxi)
printf("%s%u", prefix, i);
else
printf("%s%s", prefix, s[i]);
}
static void dump_identity (const struct hd_driveid *id)
{
int i;
char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
const unsigned short int *id_regs= (const void*) id;
unsigned long capacity;
printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s",
id->model, id->fw_rev, id->serial_no);
printf("\n Config={");
for (i=0; i<=15; i++) {
if (id->config & (1<<i))
printf("%s", cfg_str[i]);
}
printf(" }\n");
printf(" RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
id->cyls, id->heads, id->sectors,
id->track_bytes, id->sector_bytes, id->ecc_bytes);
dmpstr(" BuffType=",id->buf_type,BuffType,3);
printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
if (id->max_multsect) {
printf(", MultSect=");
if (!(id->multsect_valid&1))
printf("?%u?", id->multsect);
else if (id->multsect)
printf("%u", id->multsect);
else
printf("off");
}
putchar('\n');
if (id->tPIO <= 5) {
strcat(pmodes, "pio0 ");
if (id->tPIO >= 1) strcat(pmodes, "pio1 ");
if (id->tPIO >= 2) strcat(pmodes, "pio2 ");
}
if (!(id->field_valid&1))
printf(" (maybe):");
capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
printf(" CurCHS=%u/%u/%u, CurSects=%lu", id->cur_cyls, id->cur_heads, id->cur_sectors, capacity);
printf(", LBA=%s", YN(id->capability&2));
if (id->capability&2)
printf(", LBAsects=%u", id->lba_capacity);
if (id->capability&1) {
if (id->dma_1word | id->dma_mword) {
if (id->dma_1word & 0x100) strcat(dmodes,"*");
if (id->dma_1word & 1) strcat(dmodes,"sdma0 ");
if (id->dma_1word & 0x200) strcat(dmodes,"*");
if (id->dma_1word & 2) strcat(dmodes,"sdma1 ");
if (id->dma_1word & 0x400) strcat(dmodes,"*");
if (id->dma_1word & 4) strcat(dmodes,"sdma2 ");
if (id->dma_1word & 0xf800) strcat(dmodes,"*");
if (id->dma_1word & 0xf8) strcat(dmodes,"sdma? ");
if (id->dma_mword & 0x100) strcat(dmodes,"*");
if (id->dma_mword & 1) strcat(dmodes,"mdma0 ");
if (id->dma_mword & 0x200) strcat(dmodes,"*");
if (id->dma_mword & 2) strcat(dmodes,"mdma1 ");
if (id->dma_mword & 0x400) strcat(dmodes,"*");
if (id->dma_mword & 4) strcat(dmodes,"mdma2 ");
if (id->dma_mword & 0xf800) strcat(dmodes,"*");
if (id->dma_mword & 0xf8) strcat(dmodes,"mdma? ");
}
}
printf("\n IORDY=");
if (id->capability&8)
printf((id->capability&4) ? "on/off" : "yes");
else
printf("no");
if ((id->capability&8) || (id->field_valid&2)) {
if (id->field_valid&2) {
printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
if (id->eide_pio_modes & 1) strcat(pmodes, "pio3 ");
if (id->eide_pio_modes & 2) strcat(pmodes, "pio4 ");
if (id->eide_pio_modes &~3) strcat(pmodes, "pio? ");
}
if (id->field_valid&4) {
if (id->dma_ultra & 0x100) strcat(umodes,"*");
if (id->dma_ultra & 0x001) strcat(umodes,"udma0 ");
if (id->dma_ultra & 0x200) strcat(umodes,"*");
if (id->dma_ultra & 0x002) strcat(umodes,"udma1 ");
if (id->dma_ultra & 0x400) strcat(umodes,"*");
if (id->dma_ultra & 0x004) strcat(umodes,"udma2 ");
if (id->dma_ultra & 0x800) strcat(umodes,"*");
if (id->dma_ultra & 0x008) strcat(umodes,"udma3 ");
if (id->dma_ultra & 0x1000) strcat(umodes,"*");
if (id->dma_ultra & 0x010) strcat(umodes,"udma4 ");
if (id->dma_ultra & 0x2000) strcat(umodes,"*");
if (id->dma_ultra & 0x020) strcat(umodes,"udma5 ");
if (id->dma_ultra & 0x4000) strcat(umodes,"*");
if (id->dma_ultra & 0x0040) strcat(umodes,"udma6 ");
if (id->dma_ultra & 0x8000) strcat(umodes,"*");
if (id->dma_ultra & 0x0080) strcat(umodes,"udma7 ");
}
}
if ((id->capability&1) && (id->field_valid&2))
printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
printf("\n PIO modes: %s", pmodes);
if (*dmodes)
printf("\n DMA modes: %s", dmodes);
if (*umodes)
printf("\n UDMA modes: %s", umodes);
printf("\n AdvancedPM=%s",YN(id_regs[83]&8));
if (id_regs[83] & 8) {
if (!(id_regs[86]&8))
printf(": disabled (255)");
else if ((id_regs[91]&0xFF00)!=0x4000)
printf(": unknown setting");
else
printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
}
if (id_regs[82]&0x20)
printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
#ifdef __NEW_HD_DRIVE_ID
if (id->minor_rev_num || id->major_rev_num) {
printf("\n Drive conforms to: ");
if (id->minor_rev_num <= 31)
printf("%s: ", minor_str[id->minor_rev_num]);
else
printf("unknown: ");
if (id->major_rev_num != 0x0000 && /* NOVAL_0 */
id->major_rev_num != 0xFFFF) { /* NOVAL_1 */
/* through ATA/ATAPI-7 is currently defined--
* increase this value as further specs are
* standardized (though we can guess safely to 15)
*/
for (i=0; i <= 7; i++) {
if (id->major_rev_num & (1<<i))
printf(" ATA/ATAPI-%u", i);
}
}
}
#endif /* __NEW_HD_DRIVE_ID */
printf("\n");
printf("\n * signifies the current active mode\n");
printf("\n");
}
#endif /* HDIO_GET_IDENTITY */
void flush_buffer_cache (int fd)
{
fsync (fd); /* flush buffers */
#ifdef BLKFLSBUF
if (ioctl(fd, BLKFLSBUF, NULL)) /* do it again, big time */
perror("BLKFLSBUF failed");
#endif
#ifdef HDIO_DRIVE_CMD
if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
perror("HDIO_DRIVE_CMD(null) (wait for flush complete) failed");
#endif
}
int seek_to_zero (int fd)
{
if (lseek(fd, (off_t) 0, SEEK_SET)) {
perror("lseek() failed");
return 1;
}
return 0;
}
int read_big_block (int fd, char *buf)
{
int i, rc;
if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
if (rc) {
if (rc == -1)
perror("read() failed");
else
fprintf(stderr, "read(%u) returned %u bytes\n", TIMING_BUF_BYTES, rc);
} else {
fputs ("read() hit EOF - device too small\n", stderr);
}
return 1;
}
/* access all sectors of buf to ensure the read fully completed */
for (i = 0; i < TIMING_BUF_BYTES; i += 512)
buf[i] &= 1;
return 0;
}
void time_cache (int fd)
{
char *buf;
struct itimerval e1, e2;
int shmid;
double elapsed, elapsed2;
unsigned int iterations, total_MB;
if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1) {
perror ("could not allocate sharedmem buf");
return;
}
if (shmctl(shmid, SHM_LOCK, NULL) == -1) {
perror ("could not lock sharedmem buf");
(void) shmctl(shmid, IPC_RMID, NULL);
return;
}
if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1) {
perror ("could not attach sharedmem buf");
(void) shmctl(shmid, IPC_RMID, NULL);
return;
}
if (shmctl(shmid, IPC_RMID, NULL) == -1)
perror ("shmctl(,IPC_RMID,) failed");
/* Clear out the device request queues & give them time to complete */
sync();
sleep(3);
/*
* getitimer() is used rather than gettimeofday() because
* it is much more consistent (on my machine, at least).
*/
{
const struct itimerval tv = {{1000,0},{1000,0}};
setitimer(ITIMER_REAL, &tv, NULL);
}
if (seek_to_zero (fd)) return;
if (read_big_block (fd, buf)) return;
printf(" Timing %scached reads: ", (open_flags & O_DIRECT) ? "O_DIRECT " : "");
fflush(stdout);
/* Clear out the device request queues & give them time to complete */
sync();
sleep(1);
/* Now do the timing */
iterations = 0;
getitimer(ITIMER_REAL, &e1);
do {
++iterations;
if (seek_to_zero (fd) || read_big_block (fd, buf))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (elapsed < ctimings);
total_MB = iterations * TIMING_BUF_MB;
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
/* Now remove the lseek() and getitimer() overheads from the elapsed time */
getitimer(ITIMER_REAL, &e1);
do {
if (seek_to_zero (fd))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (--iterations);
elapsed -= elapsed2;
if (total_MB >= elapsed) /* more than 1MB/s */
printf("%3u MB in %5.2f seconds = %6.2f MB/sec\n",
total_MB, elapsed,
total_MB / elapsed);
else
printf("%3u MB in %5.2f seconds = %6.2f kB/sec\n",
total_MB, elapsed,
total_MB / elapsed * 1024);
flush_buffer_cache(fd);
sleep(1);
quit:
if (-1 == shmdt(buf))
perror ("could not detach sharedmem buf");
}
static int do_blkgetsize (int fd, __ull *blksize64)
{
int rc;
unsigned int blksize32 = 0;
if (0 == ioctl(fd, BLKGETSIZE64, blksize64)) { // returns bytes
*blksize64 /= 512;
return 0;
}
rc = ioctl(fd, BLKGETSIZE, &blksize32); // returns sectors
if (rc)
perror(" BLKGETSIZE failed");
*blksize64 = blksize32;
return rc;
}
void time_device (int fd)
{
char *buf;
double elapsed;
struct itimerval e1, e2;
int shmid;
unsigned int max_iterations = 1024, total_MB, iterations;
//
// get device size
//
if (do_ctimings || do_timings) {
__ull blksize;
if (0 == do_blkgetsize(fd, &blksize))
max_iterations = (unsigned)(blksize / (2 * 1024) / TIMING_BUF_MB);
}
if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1) {
perror ("could not allocate sharedmem buf");
return;
}
if (shmctl(shmid, SHM_LOCK, NULL) == -1) {
perror ("could not lock sharedmem buf");
(void) shmctl(shmid, IPC_RMID, NULL);
return;
}
if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1) {
perror ("could not attach sharedmem buf");
(void) shmctl(shmid, IPC_RMID, NULL);
return;
}
if (shmctl(shmid, IPC_RMID, NULL) == -1)
perror ("shmctl(,IPC_RMID,) failed");
/* Clear out the device request queues & give them time to complete */
sync();
sleep(3);
printf(" Timing %s disk reads: ", (open_flags & O_DIRECT) ? "O_DIRECT" : "buffered");
fflush(stdout);
/*
* getitimer() is used rather than gettimeofday() because
* it is much more consistent (on my machine, at least).
*/
{
const struct itimerval tv = {{1000,0},{1000,0}};
setitimer(ITIMER_REAL, &tv, NULL);
}
/* Now do the timings for real */
iterations = 0;
getitimer(ITIMER_REAL, &e1);
do {
++iterations;
if (read_big_block (fd, buf))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (elapsed < timings && iterations < max_iterations);
total_MB = iterations * TIMING_BUF_MB;
if ((total_MB / elapsed) > 1.0) /* more than 1MB/s */
printf("%3u MB in %5.2f seconds = %6.2f MB/sec\n",
total_MB, elapsed, total_MB / elapsed);
else
printf("%3u MB in %5.2f seconds = %6.2f kB/sec\n",
total_MB, elapsed, total_MB / elapsed * 1024);
quit:
if (-1 == shmdt(buf))
perror ("could not detach sharedmem buf");
}
static void on_off (unsigned int value)
{
printf(value ? " (on)\n" : " (off)\n");
}
#ifdef HDIO_GET_BUSSTATE
static void bus_state_value (unsigned int value)
{
const char *string;
switch (value) {
case BUSSTATE_ON:
string = " (on)\n";
break;
case BUSSTATE_OFF:
string = " (off)\n";
break;
case BUSSTATE_TRISTATE:
string = " (tristate)\n";
break;
default:
string = " (unknown: %d)\n";
break;
}
printf(string, value);
}
#endif
#ifdef HDIO_DRIVE_CMD
static void interpret_standby (unsigned int standby)
{
printf(" (");
switch(standby) {
case 0: printf("off");
break;
case 252: printf("21 minutes");
break;
case 253: printf("vendor-specific");
break;
case 254: printf("?reserved");
break;
case 255: printf("21 minutes + 15 seconds");
break;
default:
if (standby <= 240) {
unsigned int secs = standby * 5;
unsigned int mins = secs / 60;
secs %= 60;
if (mins) printf("%u minutes", mins);
if (mins && secs) printf(" + ");
if (secs) printf("%u seconds", secs);
} else if (standby <= 251) {
unsigned int mins = (standby - 240) * 30;
unsigned int hrs = mins / 60;
mins %= 60;
if (hrs) printf("%u hours", hrs);
if (hrs && mins) printf(" + ");
if (mins) printf("%u minutes", mins);
} else
printf("illegal value");
break;
}
printf(")\n");
}
struct xfermode_entry {
int val;
const char *name;
};
static const struct xfermode_entry xfermode_table[] = {
{ 8, "pio0" },
{ 9, "pio1" },
{ 10, "pio2" },
{ 11, "pio3" },
{ 12, "pio4" },
{ 13, "pio5" },
{ 14, "pio6" },
{ 15, "pio7" },
{ 16, "sdma0" },
{ 17, "sdma1" },
{ 18, "sdma2" },
{ 19, "sdma3" },
{ 20, "sdma4" },
{ 21, "sdma5" },
{ 22, "sdma6" },
{ 23, "sdma7" },
{ 32, "mdma0" },
{ 33, "mdma1" },
{ 34, "mdma2" },
{ 35, "mdma3" },
{ 36, "mdma4" },
{ 37, "mdma5" },
{ 38, "mdma6" },
{ 39, "mdma7" },
{ 64, "udma0" },
{ 65, "udma1" },
{ 66, "udma2" },
{ 67, "udma3" },
{ 68, "udma4" },
{ 69, "udma5" },
{ 70, "udma6" },
{ 71, "udma7" },
{ 0, NULL }
};
static int translate_xfermode(char * name)
{
const struct xfermode_entry *tmp;
char *endptr;
int val = -1;
for (tmp = xfermode_table; tmp->name != NULL; ++tmp) {
if (!strcmp(name, tmp->name))
return tmp->val;
}
val = strtol(name, &endptr, 10);
if (*endptr == '\0')
return val;
return -1;
}
static void interpret_xfermode (unsigned int xfermode)
{
printf(" (");
switch(xfermode) {
case 0: printf("default PIO mode");
break;
case 1: printf("default PIO mode, disable IORDY");
break;
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15: printf("PIO flow control mode%u", xfermode-8);
break;
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23: printf("singleword DMA mode%u", xfermode-16);
break;
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39: printf("multiword DMA mode%u", xfermode-32);
break;
case 64:
case 65:
case 66:
case 67:
case 68:
case 69:
case 70:
case 71: printf("UltraDMA mode%u", xfermode-64);
break;
default:
printf("unknown, probably not valid");
break;
}
printf(")\n");
}
#endif /* HDIO_DRIVE_CMD */
#ifndef VXVM_MAJOR
#define VXVM_MAJOR 199
#endif
#ifndef CCISS_MAJOR
#define CCISS_MAJOR 104
#endif
void process_dev (char *devname)
{
int fd;
static long parm, multcount;
#ifndef HDIO_DRIVE_CMD
int force_operation = 0;
#endif
fd = open (devname, open_flags);
if (fd < 0) {
perror(devname);
exit(errno);
}
if (!quiet)
printf("\n%s:\n", devname);
#ifdef BLKRASET
if (set_fsreadahead) {
if (get_fsreadahead)
printf(" setting fs readahead to %ld\n", fsreadahead);
if (ioctl(fd, BLKRASET, fsreadahead))
perror(" BLKRASET failed");
}
#endif
#ifdef HDIO_UNREGISTER_HWIF
if (unregister_hwif) {
printf(" attempting to unregister hwif#%u\n", hwif);
if (ioctl(fd, HDIO_UNREGISTER_HWIF, hwif))
perror(" HDIO_UNREGISTER_HWIF failed");
}
#endif
#ifdef HDIO_SCAN_HWIF
if (scan_hwif) {
int args[3];
printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
args[0] = hwif_data;
args[1] = hwif_ctrl;
args[2] = hwif_irq;
if (ioctl(fd, HDIO_SCAN_HWIF, args))
perror(" HDIO_SCAN_HWIF failed");
}
#endif
#ifdef HDIO_SET_PIO_MODE
if (set_piomode) {
if (noisy_piomode) {
if (piomode == 255)
printf(" attempting to auto-tune PIO mode\n");
else if (piomode < 100)
printf(" attempting to set PIO mode to %d\n", piomode);
else if (piomode < 200)
printf(" attempting to set MDMA mode to %d\n", (piomode-100));
else
printf(" attempting to set UDMA mode to %d\n", (piomode-200));
}
if (ioctl(fd, HDIO_SET_PIO_MODE, piomode))
perror(" HDIO_SET_PIO_MODE failed");
}
#endif
#ifdef HDIO_SET_32BIT
if (set_io32bit) {
if (get_io32bit)
printf(" setting 32-bit IO_support flag to %ld\n", io32bit);
if (ioctl(fd, HDIO_SET_32BIT, io32bit))
perror(" HDIO_SET_32BIT failed");
}
#endif
#ifdef HDIO_SET_MULTCOUNT
if (set_mult) {
if (get_mult)
printf(" setting multcount to %ld\n", mult);
if (ioctl(fd, HDIO_SET_MULTCOUNT, mult))
perror(" HDIO_SET_MULTCOUNT failed");
#ifndef HDIO_DRIVE_CMD
else force_operation = 1;
#endif
}
#endif
#ifdef BLKROSET
if (set_readonly) {
if (get_readonly) {
printf(" setting readonly to %ld", readonly);
on_off(readonly);
}
if (ioctl(fd, BLKROSET, &readonly))
perror(" BLKROSET failed");
}
#endif
#ifdef HDIO_SET_UNMASKINTR
if (set_unmask) {
if (get_unmask) {
printf(" setting unmaskirq to %ld", unmask);
on_off(unmask);
}
if (ioctl(fd, HDIO_SET_UNMASKINTR, unmask))
perror(" HDIO_SET_UNMASKINTR failed");
}
#endif
#ifdef HDIO_SET_DMA
if (set_dma) {
if (get_dma) {
printf(" setting using_dma to %ld", dma);
on_off(dma);
}
if (ioctl(fd, HDIO_SET_DMA, dma))
perror(" HDIO_SET_DMA failed");
}
#endif
#ifdef HDIO_SET_QDMA
if (set_dma_q) {
if (get_dma_q) {
printf(" setting DMA queue_depth to %ld", dma_q);
on_off(dma_q);
}
if (ioctl(fd, HDIO_SET_QDMA, dma_q))
perror(" HDIO_SET_QDMA failed");
}
#endif
#ifdef HDIO_SET_NOWERR
if (set_nowerr) {
if (get_nowerr) {
printf(" setting nowerr to %ld", nowerr);
on_off(nowerr);
}
if (ioctl(fd, HDIO_SET_NOWERR, nowerr))
perror(" HDIO_SET_NOWERR failed");
}
#endif
#ifdef HDIO_GET_KEEPSETTINGS
if (set_keep) {
if (get_keep) {
printf(" setting keep_settings to %ld", keep);
on_off(keep);
}
if (ioctl(fd, HDIO_SET_KEEPSETTINGS, keep))
perror(" HDIO_SET_KEEPSETTINGS failed");
}
#endif /* HDIO_GET_KEEPSETTINGS */
#ifdef HDIO_DRIVE_CMD
if (set_doorlock) {
unsigned char args[4] = {0,0,0,0};
args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
if (get_doorlock) {
printf(" setting drive doorlock to %ld", doorlock);
on_off(doorlock);
}
if (ioctl(fd, HDIO_DRIVE_CMD, &args))
perror(" HDIO_DRIVE_CMD(doorlock) failed");
}
if (set_dkeep) {
/* lock/unlock the drive's "feature" settings */
unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
if (get_dkeep) {
printf(" setting drive keep features to %ld", dkeep);
on_off(dkeep);
}
args[2] = dkeep ? 0x66 : 0xcc;
if (ioctl(fd, HDIO_DRIVE_CMD, &args))
perror(" HDIO_DRIVE_CMD(keepsettings) failed");
}
if (set_defects) {
unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
args[2] = defects ? 0x04 : 0x84;
if (get_defects)
printf(" setting drive defect management to %ld\n", defects);
if (ioctl(fd, HDIO_DRIVE_CMD, &args))
perror(" HDIO_DRIVE_CMD(defectmgmt) failed");
}
if (set_prefetch) {
unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
args[1] = (char)prefetch;
if (get_prefetch)
printf(" setting drive prefetch to %ld\n", prefetch);
if (ioctl(fd, HDIO_DRIVE_CMD, &args))
perror(" HDIO_DRIVE_CMD(setprefetch) failed");
}
if (set_xfermode) {
unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
args[1] = xfermode_requested;
if (get_xfermode) {
printf(" setting xfermode to %d", xfermode_requested);
interpret_xfermode(xfermode_requested);
}
if (ioctl(fd, HDIO_DRIVE_CMD, &args))
perror(" HDIO_DRIVE_CMD(setxfermode) failed");
}
if (set_lookahead) {
unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
args[2] = lookahead ? 0xaa : 0x55;
if (get_lookahead) {
printf(" setting drive read-lookahead to %ld", lookahead);
on_off(lookahead);
}
if (ioctl(fd, HDIO_DRIVE_CMD, &args))
perror(" HDIO_DRIVE_CMD(setreadahead) failed");
}
if (set_powerup_in_standby) {
unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
args[2] = powerup_in_standby ? 0x06 : 0x86;
if (get_powerup_in_standby) {
printf(" setting power-up in standby to %ld", powerup_in_standby);
on_off(powerup_in_standby);
}
if (ioctl(fd, HDIO_DRIVE_CMD, &args))
perror(" HDIO_DRIVE_CMD(powerup_in_standby) failed");
}
if (set_apmmode) {
unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
if (apmmode<1) apmmode=1;
if (apmmode>255) apmmode=255;
if (get_apmmode)
printf(" setting Advanced Power Management level to");
if (apmmode==255) {
/* disable Advanced Power Management */
args[2] = 0x85; /* feature register */
if (get_apmmode) printf(" disabled\n");
} else {
/* set Advanced Power Management mode */
args[2] = 0x05; /* feature register */
args[1] = (unsigned char)apmmode; /* sector count register */
if (get_apmmode) printf(" 0x%02lX (%ld)\n",apmmode,apmmode);
}
if (ioctl(fd, HDIO_DRIVE_CMD, &args))
perror(" HDIO_DRIVE_CMD failed");
}
#endif
#ifdef CDROM_SELECT_SPEED
if (set_cdromspeed) {
printf ("setting cdrom speed to %ld\n", cdromspeed);
if (ioctl (fd, CDROM_SELECT_SPEED, cdromspeed))
perror(" CDROM_SELECT_SPEED failed");
}
#endif
#ifdef HDIO_DRIVE_CMD
if (set_wcache) {
#ifdef DO_FLUSHCACHE