blob: 04ce073dbc6a195d715b50190fd13375f394330c [file] [log] [blame]
Eric Andersen3443bd72003-07-22 07:30:36 +00001/* vi: set sw=4 ts=4: */
2/*
3 * hdparm implementation for busybox
4 *
5 *
Glenn L McGrath5be6a202003-11-28 22:55:03 +00006 * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
Glenn L McGrath07085852003-10-09 07:28:22 +00007 *
8 * Hacked by Tito <farmatito@tiscali.it> for size optimization.
Eric Andersen3443bd72003-07-22 07:30:36 +00009 *
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 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307 USA
24 *
25 *
26 * This program is based on the source code of hdparm: see below...
27 * hdparm.c - Command line interface to get/set hard disk parameters
28 * - by Mark Lord (C) 1994-2002 -- freely distributable
29 */
Glenn L McGrath07085852003-10-09 07:28:22 +000030
Eric Andersen3443bd72003-07-22 07:30:36 +000031#include <unistd.h>
32#include <string.h>
33#include <stdlib.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <ctype.h>
37#include <endian.h>
38#include <sys/ioctl.h>
39#include <sys/shm.h>
40#include <sys/sysmacros.h>
41#include <sys/time.h>
42#include <sys/times.h>
43#include <sys/mount.h>
44#include "busybox.h"
45#include <linux/types.h>
46#include <linux/hdreg.h>
47#include <linux/major.h>
48#include <asm/byteorder.h>
49
50
Glenn L McGratha55d72b2003-10-09 11:38:45 +000051#if (__BYTE_ORDER == __BIG_ENDIAN) && !defined(__USE_XOPEN)
Eric Andersen3443bd72003-07-22 07:30:36 +000052#define __USE_XOPEN
53#endif
54
55/* device types */
56/* ------------ */
57#define NO_DEV 0xffff
58#define ATA_DEV 0x0000
59#define ATAPI_DEV 0x0001
60
61/* word definitions */
62/* ---------------- */
63#define GEN_CONFIG 0 /* general configuration */
64#define LCYLS 1 /* number of logical cylinders */
65#define CONFIG 2 /* specific configuration */
66#define LHEADS 3 /* number of logical heads */
67#define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
68#define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
69#define LSECTS 6 /* number of logical sectors/track */
70#define START_SERIAL 10 /* ASCII serial number */
71#define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
72#define BUF_TYPE 20 /* buffer type (ATA-1) */
73#define BUFFER__SIZE 21 /* buffer size (ATA-1) */
74#define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
75#define START_FW_REV 23 /* ASCII firmware revision */
76#define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
77#define START_MODEL 27 /* ASCII model number */
78#define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
79#define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
80#define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
81#define CAPAB_0 49 /* capabilities */
82#define CAPAB_1 50
83#define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
84#define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
85#define WHATS_VALID 53 /* what fields are valid */
86#define LCYLS_CUR 54 /* current logical cylinders */
87#define LHEADS_CUR 55 /* current logical heads */
88#define LSECTS_CUR 56 /* current logical sectors/track */
89#define CAPACITY_LSB 57 /* current capacity in sectors */
90#define CAPACITY_MSB 58
91#define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
92#define LBA_SECTS_LSB 60 /* LBA: total number of user */
93#define LBA_SECTS_MSB 61 /* addressable sectors */
94#define SINGLE_DMA 62 /* singleword DMA modes */
95#define MULTI_DMA 63 /* multiword DMA modes */
96#define ADV_PIO_MODES 64 /* advanced PIO modes supported */
97 /* multiword DMA xfer cycle time: */
98#define DMA_TIME_MIN 65 /* - minimum */
99#define DMA_TIME_NORM 66 /* - manufacturer's recommended */
100 /* minimum PIO xfer cycle time: */
101#define PIO_NO_FLOW 67 /* - without flow control */
102#define PIO_FLOW 68 /* - with IORDY flow control */
103#define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
104#define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
105#define CDR_MAJOR 73 /* CD ROM: major version number */
106#define CDR_MINOR 74 /* CD ROM: minor version number */
107#define QUEUE_DEPTH 75 /* queue depth */
108#define MAJOR 80 /* major version number */
109#define MINOR 81 /* minor version number */
110#define CMDS_SUPP_0 82 /* command/feature set(s) supported */
111#define CMDS_SUPP_1 83
112#define CMDS_SUPP_2 84
113#define CMDS_EN_0 85 /* command/feature set(s) enabled */
114#define CMDS_EN_1 86
115#define CMDS_EN_2 87
116#define ULTRA_DMA 88 /* ultra DMA modes */
117 /* time to complete security erase */
118#define ERASE_TIME 89 /* - ordinary */
119#define ENH_ERASE_TIME 90 /* - enhanced */
120#define ADV_PWR 91 /* current advanced power management level
Glenn L McGrath07085852003-10-09 07:28:22 +0000121 in low byte, 0x40 in high byte. */
Eric Andersen3443bd72003-07-22 07:30:36 +0000122#define PSWD_CODE 92 /* master password revision code */
123#define HWRST_RSLT 93 /* hardware reset result */
124#define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
125#define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
126#define LBA_MID 101 /* bits are used, but addr 103 */
127#define LBA_48_MSB 102 /* has been reserved for LBA in */
128#define LBA_64_MSB 103 /* the future. */
129#define RM_STAT 127 /* removable media status notification feature set support */
130#define SECU_STATUS 128 /* security status */
131#define CFA_PWR_MODE 160 /* CFA power mode 1 */
132#define START_MEDIA 176 /* media serial number */
133#define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
134#define START_MANUF 196 /* media manufacturer I.D. */
135#define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
136#define INTEGRITY 255 /* integrity word */
137
138/* bit definitions within the words */
139/* -------------------------------- */
140
141/* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
142#define VALID 0xc000
143#define VALID_VAL 0x4000
144/* many words are considered invalid if they are either all-0 or all-1 */
145#define NOVAL_0 0x0000
146#define NOVAL_1 0xffff
147
148/* word 0: gen_config */
Glenn L McGrath07085852003-10-09 07:28:22 +0000149#define NOT_ATA 0x8000
Eric Andersen3443bd72003-07-22 07:30:36 +0000150#define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
151#define MEDIA_REMOVABLE 0x0080
152#define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
153#define INCOMPLETE 0x0004
154#define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
155#define DRQ_RESPONSE_TIME 0x0060
156#define DRQ_3MS_VAL 0x0000
157#define DRQ_INTR_VAL 0x0020
158#define DRQ_50US_VAL 0x0040
159#define PKT_SIZE_SUPPORTED 0x0003
160#define PKT_SIZE_12_VAL 0x0000
161#define PKT_SIZE_16_VAL 0x0001
162#define EQPT_TYPE 0x1f00
163#define SHIFT_EQPT 8
164
165#define CDROM 0x0005
166
167#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000168static const char * const pkt_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000169 "Direct-access device", /* word 0, bits 12-8 = 00 */
170 "Sequential-access device", /* word 0, bits 12-8 = 01 */
171 "Printer", /* word 0, bits 12-8 = 02 */
172 "Processor", /* word 0, bits 12-8 = 03 */
173 "Write-once device", /* word 0, bits 12-8 = 04 */
174 "CD-ROM", /* word 0, bits 12-8 = 05 */
175 "Scanner", /* word 0, bits 12-8 = 06 */
176 "Optical memory", /* word 0, bits 12-8 = 07 */
177 "Medium changer", /* word 0, bits 12-8 = 08 */
178 "Communications device", /* word 0, bits 12-8 = 09 */
179 "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
180 "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
181 "Array controller", /* word 0, bits 12-8 = 0c */
182 "Enclosure services", /* word 0, bits 12-8 = 0d */
183 "Reduced block command device", /* word 0, bits 12-8 = 0e */
184 "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
185 "", /* word 0, bits 12-8 = 10 */
186 "", /* word 0, bits 12-8 = 11 */
187 "", /* word 0, bits 12-8 = 12 */
188 "", /* word 0, bits 12-8 = 13 */
189 "", /* word 0, bits 12-8 = 14 */
190 "", /* word 0, bits 12-8 = 15 */
191 "", /* word 0, bits 12-8 = 16 */
192 "", /* word 0, bits 12-8 = 17 */
193 "", /* word 0, bits 12-8 = 18 */
194 "", /* word 0, bits 12-8 = 19 */
195 "", /* word 0, bits 12-8 = 1a */
196 "", /* word 0, bits 12-8 = 1b */
197 "", /* word 0, bits 12-8 = 1c */
198 "", /* word 0, bits 12-8 = 1d */
199 "", /* word 0, bits 12-8 = 1e */
200 "Unknown", /* word 0, bits 12-8 = 1f */
201};
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000202
203static const char * const ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
Eric Andersen3443bd72003-07-22 07:30:36 +0000204 "reserved", /* bit 0 */
205 "hard sectored", /* bit 1 */
206 "soft sectored", /* bit 2 */
207 "not MFM encoded ", /* bit 3 */
208 "head switch time > 15us", /* bit 4 */
209 "spindle motor control option", /* bit 5 */
210 "fixed drive", /* bit 6 */
211 "removable drive", /* bit 7 */
212 "disk xfer rate <= 5Mbs", /* bit 8 */
213 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
214 "disk xfer rate > 5Mbs", /* bit 10 */
215 "rotational speed tol.", /* bit 11 */
216 "data strobe offset option", /* bit 12 */
217 "track offset option", /* bit 13 */
218 "format speed tolerance gap reqd", /* bit 14 */
219 "ATAPI" /* bit 14 */
220};
221#endif
222
223/* word 1: number of logical cylinders */
224#define LCYLS_MAX 0x3fff /* maximum allowable value */
225
Eric Andersenaff114c2004-04-14 17:51:38 +0000226/* word 2: specific configuration
Eric Andersen3443bd72003-07-22 07:30:36 +0000227 * (a) require SET FEATURES to spin-up
228 * (b) require spin-up to fully reply to IDENTIFY DEVICE
229 */
230#define STBY_NID_VAL 0x37c8 /* (a) and (b) */
231#define STBY_ID_VAL 0x738c /* (a) and not (b) */
232#define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
233#define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
234
235/* words 47 & 59: sector_xfer_max & sector_xfer_cur */
236#define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
237#define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
238
239/* word 49: capabilities 0 */
240#define STD_STBY 0x2000 /* 1=standard values supported (ATA);
241 0=vendor specific values */
242#define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
243#define IORDY_OFF 0x0400 /* 1=may be disabled */
244#define LBA_SUP 0x0200 /* 1=Logical Block Address support */
245#define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
246#define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
247#define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
248#define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
249#define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
250
251/* word 50: capabilities 1 */
252#define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
253
254/* words 51 & 52: PIO & DMA cycle times */
255#define MODE 0xff00 /* the mode is in the MSBs */
256
257/* word 53: whats_valid */
258#define OK_W88 0x0004 /* the ultra_dma info is valid */
259#define OK_W64_70 0x0002 /* see above for word descriptions */
260#define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
261
262/*word 63,88: dma_mode, ultra_dma_mode*/
263#define MODE_MAX 7 /* bit definitions force udma <=7 (when
264 * udma >=8 comes out it'll have to be
265 * defined in a new dma_mode word!) */
266
267/* word 64: PIO transfer modes */
268#define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
269#define PIO_MODE_MAX 8 /* but all 8 bits are defined */
270
271/* word 75: queue_depth */
272#define DEPTH_BITS 0x001f /* bits used for queue depth */
273
274/* words 80-81: version numbers */
275/* NOVAL_0 or NOVAL_1 means device does not report version */
276
277/* word 81: minor version number */
278#define MINOR_MAX 0x1C
279#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000280static const char * const minor_str[] = { /* word 81 value: */
Eric Andersen3443bd72003-07-22 07:30:36 +0000281 "device does not report version", /* 0x0000 */
282 "ATA-1 X3T9.2 781D prior to revision 4", /* 0x0001 */
283 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
284 "ATA-1 X3T9.2 781D revision 4", /* 0x0003 */
285 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
286 "ATA-2 X3T10 948D prior to revision 2k", /* 0x0005 */
287 "ATA-3 X3T10 2008D revision 1", /* 0x0006 */
288 "ATA-2 X3T10 948D revision 2k", /* 0x0007 */
289 "ATA-3 X3T10 2008D revision 0", /* 0x0008 */
290 "ATA-2 X3T10 948D revision 3", /* 0x0009 */
291 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
292 "ATA-3 X3T10 2008D revision 6", /* 0x000b */
293 "ATA-3 X3T13 2008D revision 7 and 7a", /* 0x000c */
294 "ATA/ATAPI-4 X3T13 1153D revision 6", /* 0x000d */
295 "ATA/ATAPI-4 T13 1153D revision 13", /* 0x000e */
296 "ATA/ATAPI-4 X3T13 1153D revision 7", /* 0x000f */
297 "ATA/ATAPI-4 T13 1153D revision 18", /* 0x0010 */
298 "ATA/ATAPI-4 T13 1153D revision 15", /* 0x0011 */
299 "ATA/ATAPI-4 published, ANSI NCITS 317-1998", /* 0x0012 */
300 "ATA/ATAPI-5 T13 1321D revision 3",
301 "ATA/ATAPI-4 T13 1153D revision 14", /* 0x0014 */
302 "ATA/ATAPI-5 T13 1321D revision 1", /* 0x0015 */
303 "ATA/ATAPI-5 published, ANSI NCITS 340-2000", /* 0x0016 */
304 "ATA/ATAPI-4 T13 1153D revision 17", /* 0x0017 */
305 "ATA/ATAPI-6 T13 1410D revision 0", /* 0x0018 */
306 "ATA/ATAPI-6 T13 1410D revision 3a", /* 0x0019 */
307 "Reserved", /* 0x001a */
308 "ATA/ATAPI-6 T13 1410D revision 2", /* 0x001b */
309 "ATA/ATAPI-6 T13 1410D revision 1", /* 0x001c */
310 "reserved" /* 0x001d */
311 "reserved" /* 0x001e */
312 "reserved" /* 0x001f-0xfffe*/
313};
314#endif
Glenn L McGrath07085852003-10-09 07:28:22 +0000315static const char actual_ver[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000316 /* word 81 value: */
317 0, /* 0x0000 WARNING: */
318 1, /* 0x0001 WARNING: */
319 1, /* 0x0002 WARNING: */
320 1, /* 0x0003 WARNING: */
321 2, /* 0x0004 WARNING: This array */
322 2, /* 0x0005 WARNING: corresponds */
323 3, /* 0x0006 WARNING: *exactly* */
324 2, /* 0x0007 WARNING: to the ATA/ */
325 3, /* 0x0008 WARNING: ATAPI version */
326 2, /* 0x0009 WARNING: listed in */
327 3, /* 0x000a WARNING: the */
328 3, /* 0x000b WARNING: minor_str */
329 3, /* 0x000c WARNING: array */
330 4, /* 0x000d WARNING: above. */
331 4, /* 0x000e WARNING: */
332 4, /* 0x000f WARNING: if you change */
333 4, /* 0x0010 WARNING: that one, */
334 4, /* 0x0011 WARNING: change this one */
335 4, /* 0x0012 WARNING: too!!! */
336 5, /* 0x0013 WARNING: */
337 4, /* 0x0014 WARNING: */
338 5, /* 0x0015 WARNING: */
339 5, /* 0x0016 WARNING: */
340 4, /* 0x0017 WARNING: */
341 6, /* 0x0018 WARNING: */
342 6, /* 0x0019 WARNING: */
343 0, /* 0x001a WARNING: */
344 6, /* 0x001b WARNING: */
345 6, /* 0x001c WARNING: */
346 0 /* 0x001d-0xfffe */
347};
348
349/* words 82-84: cmds/feats supported */
350#define CMDS_W82 0x77ff /* word 82: defined command locations*/
351#define CMDS_W83 0x3fff /* word 83: defined command locations*/
352#define CMDS_W84 0x002f /* word 83: defined command locations*/
Glenn L McGrath07085852003-10-09 07:28:22 +0000353#define SUPPORT_48_BIT 0x0400
Eric Andersen3443bd72003-07-22 07:30:36 +0000354#define NUM_CMD_FEAT_STR 48
355
356#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000357static const char * const cmd_feat_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000358 "", /* word 82 bit 15: obsolete */
359 "NOP cmd", /* word 82 bit 14 */
360 "READ BUFFER cmd", /* word 82 bit 13 */
361 "WRITE BUFFER cmd", /* word 82 bit 12 */
362 "", /* word 82 bit 11: obsolete */
363 "Host Protected Area feature set", /* word 82 bit 10 */
364 "DEVICE RESET cmd", /* word 82 bit 9 */
365 "SERVICE interrupt", /* word 82 bit 8 */
366 "Release interrupt", /* word 82 bit 7 */
367 "Look-ahead", /* word 82 bit 6 */
368 "Write cache", /* word 82 bit 5 */
369 "PACKET command feature set", /* word 82 bit 4 */
370 "Power Management feature set", /* word 82 bit 3 */
371 "Removable Media feature set", /* word 82 bit 2 */
372 "Security Mode feature set", /* word 82 bit 1 */
373 "SMART feature set", /* word 82 bit 0 */
374 /* --------------*/
375 "", /* word 83 bit 15: !valid bit */
376 "", /* word 83 bit 14: valid bit */
377 "FLUSH CACHE EXT command", /* word 83 bit 13 */
378 "Mandatory FLUSH CACHE command ", /* word 83 bit 12 */
379 "Device Configuration Overlay feature set ",
380 "48-bit Address feature set ", /* word 83 bit 10 */
381 "",
382 "SET MAX security extension", /* word 83 bit 8 */
383 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
384 "SET FEATURES subcommand required to spinup after power up",
385 "Power-Up In Standby feature set", /* word 83 bit 5 */
386 "Removable Media Status Notification feature set",
Eric Andersen06d4ec22004-03-19 10:53:52 +0000387 "Adv. Power Management feature set",/* word 83 bit 3 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000388 "CFA feature set", /* word 83 bit 2 */
389 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
390 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
391 /* --------------*/
392 "", /* word 84 bit 15: !valid bit */
393 "", /* word 84 bit 14: valid bit */
394 "", /* word 84 bit 13: reserved */
395 "", /* word 84 bit 12: reserved */
396 "", /* word 84 bit 11: reserved */
397 "", /* word 84 bit 10: reserved */
398 "", /* word 84 bit 9: reserved */
399 "", /* word 84 bit 8: reserved */
400 "", /* word 84 bit 7: reserved */
401 "", /* word 84 bit 6: reserved */
402 "General Purpose Logging feature set", /* word 84 bit 5 */
403 "", /* word 84 bit 4: reserved */
404 "Media Card Pass Through Command feature set ",
405 "Media serial number ", /* word 84 bit 2 */
406 "SMART self-test ", /* word 84 bit 1 */
407 "SMART error logging " /* word 84 bit 0 */
408};
409#endif
410
411
412/* words 85-87: cmds/feats enabled */
413/* use cmd_feat_str[] to display what commands and features have
Glenn L McGrath07085852003-10-09 07:28:22 +0000414 * been enabled with words 85-87
Eric Andersen3443bd72003-07-22 07:30:36 +0000415 */
416
417/* words 89, 90, SECU ERASE TIME */
418#define ERASE_BITS 0x00ff
419
420/* word 92: master password revision */
421/* NOVAL_0 or NOVAL_1 means no support for master password revision */
422
423/* word 93: hw reset result */
424#define CBLID 0x2000 /* CBLID status */
425#define RST0 0x0001 /* 1=reset to device #0 */
426#define DEV_DET 0x0006 /* how device num determined */
427#define JUMPER_VAL 0x0002 /* device num determined by jumper */
428#define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
429
430/* word 127: removable media status notification feature set support */
431#define RM_STAT_BITS 0x0003
432#define RM_STAT_SUP 0x0001
Glenn L McGrath07085852003-10-09 07:28:22 +0000433
Eric Andersen3443bd72003-07-22 07:30:36 +0000434/* word 128: security */
435#define SECU_ENABLED 0x0002
436#define SECU_LEVEL 0x0010
437#define NUM_SECU_STR 6
438#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000439static const char * const secu_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000440 "supported", /* word 128, bit 0 */
441 "enabled", /* word 128, bit 1 */
442 "locked", /* word 128, bit 2 */
443 "frozen", /* word 128, bit 3 */
444 "expired: security count", /* word 128, bit 4 */
445 "supported: enhanced erase" /* word 128, bit 5 */
446};
447#endif
448
449/* word 160: CFA power mode */
450#define VALID_W160 0x8000 /* 1=word valid */
451#define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
452#define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
453#define MAX_AMPS 0x0fff /* value = max current in ma */
454
455/* word 255: integrity */
456#define SIG 0x00ff /* signature location */
457#define SIG_VAL 0x00A5 /* signature value */
458
Glenn L McGrath07085852003-10-09 07:28:22 +0000459#define VERSION "v5.4"
460
461#define TIMING_MB 64
462#define TIMING_BUF_MB 1
463#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
464#define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
465#define BUFCACHE_FACTOR 2
466
467#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
468
469/* Busybox messages and functions */
470
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000471static const char bb_msg_shared_mem[] = "could not %s sharedmem buf";
472static const char bb_msg_op_not_supp[] = " operation not supported on %s disks";
Glenn L McGrath07085852003-10-09 07:28:22 +0000473
474static void bb_ioctl(int fd, int request, void *argp, const char *string)
475{
476 if (ioctl (fd, request, argp) != 0)
Eric Andersen06d4ec22004-03-19 10:53:52 +0000477 bb_perror_msg(" %s", string);
Glenn L McGrath07085852003-10-09 07:28:22 +0000478}
479
480static void if_printf(unsigned long i, char *fmt, ... )
481{
482 va_list ap;
483 va_start(ap, fmt);
484 if (i)
485 vprintf(fmt, ap);
486 va_end(ap);
487}
488
489static void on_off (unsigned int value);
490
491static void if_printf_on_off(unsigned long get_arg,const char *fmt, unsigned long arg)
492{
493 if (get_arg)
494 {
495 printf(fmt, arg);
496 on_off(arg);
497 }
498}
499
500static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
501 const char * fmt)
502{
503 if (ioctl (fd, request, &argp) != 0)
Eric Andersen06d4ec22004-03-19 10:53:52 +0000504 bb_perror_msg(" %s", string);
Glenn L McGrath07085852003-10-09 07:28:22 +0000505 else
506 {
507 printf(fmt, (unsigned long) argp);
508 on_off((unsigned long) argp);
509 }
510}
Eric Andersen416c2422003-12-12 00:08:57 +0000511
512#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000513static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
514{
515 va_list ap;
516 va_start(ap, fmt2);
517 if (i)
518 vprintf(fmt1, ap);
519 else
520 vprintf(fmt2, ap);
521 va_end(ap);
522}
523
524static void print_ascii(uint16_t *p, uint8_t length);
525
526static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
527{
528 if(val[i])
529 {
530 printf("\t%-20s",string);
531 print_ascii(&val[i], n);
532 }
533}
534
Eric Andersen416c2422003-12-12 00:08:57 +0000535static void if_strcat(unsigned long test, char *modes, char *string)
536{
537 if (test)
538 strcat(modes,string);
539}
540#endif
541
Glenn L McGrath07085852003-10-09 07:28:22 +0000542static void sync_and_sleep(int i)
543{
544 sync();
545 sleep(i);
546}
547
Eric Andersenb2aa7762004-04-05 13:08:08 +0000548static uint16_t check_if_min_and_set_val(uint16_t a, uint16_t b)
Glenn L McGrath07085852003-10-09 07:28:22 +0000549{
550 if( a < b)
551 a = b;
Eric Andersenb2aa7762004-04-05 13:08:08 +0000552 return a;
Glenn L McGrath07085852003-10-09 07:28:22 +0000553}
554
Eric Andersenb2aa7762004-04-05 13:08:08 +0000555static uint16_t check_if_maj_and_set_val(uint16_t a, uint16_t b)
Glenn L McGrath07085852003-10-09 07:28:22 +0000556{
557 if( a > b)
558 a = b;
Eric Andersenb2aa7762004-04-05 13:08:08 +0000559 return a;
Glenn L McGrath07085852003-10-09 07:28:22 +0000560}
561
Eric Andersenb2aa7762004-04-05 13:08:08 +0000562static unsigned long int set_flag(char *p, char max)
Glenn L McGrath07085852003-10-09 07:28:22 +0000563{
564 if (*p >= '0' && *p <= max )
565 return 1;
566 return 0;
567}
568
Glenn L McGrath07085852003-10-09 07:28:22 +0000569/* end of busybox specific stuff */
570
Eric Andersen3443bd72003-07-22 07:30:36 +0000571#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000572static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
573{
Eric Andersen3443bd72003-07-22 07:30:36 +0000574 uint16_t ii;
575 uint8_t err_dma = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000576
577 for(ii = 0; ii <= MODE_MAX; ii++)
578 {
579 if(mode_sel & 0x0001)
580 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000581 printf("*%cdma%u ",cc,ii);
Glenn L McGrath07085852003-10-09 07:28:22 +0000582 if(*have_mode)
583 err_dma = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000584 *have_mode = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000585 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000586 else if(mode_sup & 0x0001)
587 printf("%cdma%u ",cc,ii);
588
589 mode_sup >>=1;
590 mode_sel >>=1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000591 }
592 return err_dma;
593}
Glenn L McGrath07085852003-10-09 07:28:22 +0000594
Eric Andersen3443bd72003-07-22 07:30:36 +0000595static void print_ascii(uint16_t *p, uint8_t length) {
596 uint8_t ii;
597 char cl;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000598
Eric Andersen3443bd72003-07-22 07:30:36 +0000599 /* find first non-space & print it */
Glenn L McGrath07085852003-10-09 07:28:22 +0000600 for(ii = 0; ii< length; ii++)
601 {
602 if(((char) 0x00ff&((*p)>>8)) != ' ')
603 break;
604 if((cl = (char) 0x00ff&(*p)) != ' ')
605 {
606 if_printf((cl != '\0'),"%c",cl);
607 p++;
608 ii++;
Eric Andersen3443bd72003-07-22 07:30:36 +0000609 break;
610 }
611 p++;
612 }
613 /* print the rest */
Glenn L McGrath07085852003-10-09 07:28:22 +0000614 for(; ii< length; ii++)
615 {
616 if(!(*p))
617 break; /* some older devices have NULLs */
Eric Andersen3443bd72003-07-22 07:30:36 +0000618 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
619 p++;
620 }
621 printf("\n");
622}
623
624/* identify() is the only extern function used across two source files. The
625 others, though, were declared in hdparm.c with global scope; since other
626 functions in that file have static (file) scope, I assume the difference is
627 intentional. */
628static void identify (uint16_t *id_supplied, const char *devname)
629{
630
Glenn L McGrath07085852003-10-09 07:28:22 +0000631 char *id_file = NULL;
Eric Andersen3443bd72003-07-22 07:30:36 +0000632 FILE *fl;
633 uint16_t val[256], ii, jj, kk;
634 uint16_t like_std = 1, std = 0, min_std = 0xffff;
635 uint16_t dev = NO_DEV, eqpt = NO_DEV;
636 uint8_t have_mode = 0, err_dma = 0;
637 uint8_t chksum = 0;
638 uint32_t ll, mm, nn, oo;
639 __u64 bbbig; /* (:) */
640
Glenn L McGrath07085852003-10-09 07:28:22 +0000641 if (id_supplied)
642 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000643#if __BYTE_ORDER == __BIG_ENDIAN
644 swab(id_supplied, val, sizeof(val));
645#else
646 memcpy(val, id_supplied, sizeof(val));
647#endif
Glenn L McGrath07085852003-10-09 07:28:22 +0000648 }
649 else
650 {
651 id_file = xcalloc(1, strlen(devname)+22);
652 sprintf(id_file, "/proc/ide/%s/identify", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +0000653 /* open the file, read in all the info and close it */
Glenn L McGrath07085852003-10-09 07:28:22 +0000654 if (id_file == NULL)
Eric Andersen3443bd72003-07-22 07:30:36 +0000655 fl = stdin;
Glenn L McGrath07085852003-10-09 07:28:22 +0000656 else
657 fl = bb_xfopen(id_file, "r");
658
Eric Andersen3443bd72003-07-22 07:30:36 +0000659 /* calculate checksum over all bytes */
Glenn L McGrath07085852003-10-09 07:28:22 +0000660 for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
661 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000662 unsigned int scratch;
Glenn L McGrath07085852003-10-09 07:28:22 +0000663 if(1 != fscanf(fl,"%04x",&scratch))
664 break;
Eric Andersen3443bd72003-07-22 07:30:36 +0000665 val[ii] = (uint16_t)scratch;
666 chksum += val[ii] + (val[ii] >> 8);
667 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000668 fclose(fl);
669 /*bb_fclose_nonstdin(fl);*/
Eric Andersen3443bd72003-07-22 07:30:36 +0000670 if(ii < (INTEGRITY+1))
671 bb_error_msg_and_die("Input file wrong format or length");
672 }
673 chksum &= 0xff;
674
675 /* check if we recognise the device type */
676 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000677 if(!(val[GEN_CONFIG] & NOT_ATA))
678 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000679 dev = ATA_DEV;
680 printf("ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000681 }
682 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
683 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000684 dev = ATA_DEV;
685 like_std = 4;
686 printf("CompactFlash ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000687 }
688 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
689 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000690 dev = ATAPI_DEV;
691 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
692 printf("ATAPI %s, with ", pkt_str[eqpt]);
693 like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000694 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000695 else
696 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
697 bb_error_msg_and_die("Unknown device type");
Eric Andersen3443bd72003-07-22 07:30:36 +0000698
Glenn L McGrath07085852003-10-09 07:28:22 +0000699 if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
700 printf("removable media\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000701
702 /* Info from the specific configuration word says whether or not the
703 * ID command completed correctly. It is only defined, however in
Glenn L McGrath07085852003-10-09 07:28:22 +0000704 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
Eric Andersen3443bd72003-07-22 07:30:36 +0000705 * standards. Since the values allowed for this word are extremely
706 * specific, it should be safe to check it now, even though we don't
707 * know yet what standard this device is using.
708 */
709 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000710 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
711 {
712 like_std = 5;
713 if_printf(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)),
714 "powers-up in standby; SET FEATURES subcmd spins-up.\n");
715 if_printf((((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)),
716 "\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000717 }
718
719 /* output the model and serial numbers and the fw revision */
Glenn L McGrath07085852003-10-09 07:28:22 +0000720 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
721 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
722 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
723 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
724 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
Eric Andersen3443bd72003-07-22 07:30:36 +0000725
726 /* major & minor standards version number (Note: these words were not
727 * defined until ATA-3 & the CDROM std uses different words.) */
728 printf("Standards:");
Glenn L McGrath07085852003-10-09 07:28:22 +0000729 if(eqpt != CDROM)
730 {
731 if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
732 {
Eric Andersenb2aa7762004-04-05 13:08:08 +0000733 like_std=check_if_min_and_set_val(like_std, 3);
Eric Andersen3443bd72003-07-22 07:30:36 +0000734 std = actual_ver[val[MINOR]];
Glenn L McGrath07085852003-10-09 07:28:22 +0000735 if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);
736
Eric Andersen3443bd72003-07-22 07:30:36 +0000737 }
738 /* looks like when they up-issue the std, they obsolete one;
Glenn L McGrath07085852003-10-09 07:28:22 +0000739 * thus, only the newest 4 issues need be supported. (That's
Eric Andersen3443bd72003-07-22 07:30:36 +0000740 * what "kk" and "min_std" are all about.) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000741 if(val[MAJOR] && (val[MAJOR] !=NOVAL_1))
742 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000743 printf("\n\tSupported: ");
744 jj = val[MAJOR] << 1;
745 kk = like_std >4 ? like_std-4: 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000746 for(ii = 14; (ii >0)&&(ii>kk); ii--)
747 {
748 if(jj & 0x8000)
749 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000750 printf("%u ", ii);
Glenn L McGrath07085852003-10-09 07:28:22 +0000751 if(like_std < ii)
752 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000753 like_std = ii;
754 kk = like_std >4 ? like_std-4: 0;
755 }
Eric Andersenb2aa7762004-04-05 13:08:08 +0000756 min_std=check_if_maj_and_set_val(min_std, ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000757 }
758 jj <<= 1;
759 }
Eric Andersenb2aa7762004-04-05 13:08:08 +0000760 like_std=check_if_min_and_set_val(like_std, 3);
Eric Andersen3443bd72003-07-22 07:30:36 +0000761 }
762 /* Figure out what standard the device is using if it hasn't told
763 * us. If we know the std, check if the device is using any of
764 * the words from the next level up. It happens.
765 */
Eric Andersenb2aa7762004-04-05 13:08:08 +0000766 like_std=check_if_min_and_set_val(like_std, std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000767
Eric Andersen3443bd72003-07-22 07:30:36 +0000768 if(((std == 5) || (!std && (like_std < 6))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000769 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
770 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
771 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
772 ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
773 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000774 like_std = 6;
Glenn L McGrath07085852003-10-09 07:28:22 +0000775 }
776 else if(((std == 4) || (!std && (like_std < 5))) &&
777 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
778 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
779 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
780 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
781 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000782 like_std = 5;
Glenn L McGrath07085852003-10-09 07:28:22 +0000783 }
784 else if(((std == 3) || (!std && (like_std < 4))) &&
785 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
786 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
787 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
788 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
789 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
790 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
791 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000792 like_std = 4;
Glenn L McGrath07085852003-10-09 07:28:22 +0000793 }
794 else if(((std == 2) || (!std && (like_std < 3))) &&
795 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
796 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000797 like_std = 3;
Glenn L McGrath07085852003-10-09 07:28:22 +0000798 }
799 else if(((std == 1) || (!std && (like_std < 2))) &&
800 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
801 (val[WHATS_VALID] & OK_W64_70)) )
802 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000803 like_std = 2;
804 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000805 if(!std)
Eric Andersen3443bd72003-07-22 07:30:36 +0000806 printf("\n\tLikely used: %u\n",like_std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000807 else if(like_std > std)
808 printf("& some of %u\n",like_std);
809 else
810 printf("\n");
811 }
812 else
813 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000814 /* TBD: do CDROM stuff more thoroughly. For now... */
815 kk = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000816 if(val[CDR_MINOR] == 9)
817 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000818 kk = 1;
819 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
820 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000821 if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
822 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000823 kk = 1;
824 printf("\n\tSupported: CD-ROM ATAPI");
825 jj = val[CDR_MAJOR] >> 1;
Glenn L McGrath07085852003-10-09 07:28:22 +0000826 for(ii = 1; ii <15; ii++)
827 {
828 if_printf((jj & 0x0001),"-%u ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000829 jj >>= 1;
830 }
831 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000832 if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000833 /* the cdrom stuff is more like ATA-2 than anything else, so: */
834 like_std = 2;
835 }
836
Glenn L McGrath07085852003-10-09 07:28:22 +0000837 if(min_std == 0xffff)
838 min_std = like_std > 4 ? like_std - 3 : 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000839
840 printf("Configuration:\n");
841 /* more info from the general configuration word */
Glenn L McGrath07085852003-10-09 07:28:22 +0000842 if((eqpt != CDROM) && (like_std == 1))
843 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000844 jj = val[GEN_CONFIG] >> 1;
Glenn L McGrath07085852003-10-09 07:28:22 +0000845 for(ii = 1; ii < 15; ii++)
846 {
847 if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000848 jj >>=1;
849 }
850 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000851 if(dev == ATAPI_DEV)
852 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000853 printf("\tDRQ response: "); /* Data Request (DRQ) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000854 switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
855 {
856 case DRQ_3MS_VAL :
857 printf("3ms.\n");
858 break;
859 case DRQ_INTR_VAL :
860 printf("<=10ms with INTRQ\n");
861 break;
862 case DRQ_50US_VAL :
863 printf("50us.\n");
864 break;
865 default :
866 printf("unknown.\n");
867 break;
Eric Andersen3443bd72003-07-22 07:30:36 +0000868 }
869 printf("\tPacket size: ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000870 switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
871 {
872 case PKT_SIZE_12_VAL :
873 printf("12 bytes\n");
874 break;
875 case PKT_SIZE_16_VAL :
876 printf("16 bytes\n");
877 break;
878 default :
879 printf("Unknown\n");
880 break;
Eric Andersen3443bd72003-07-22 07:30:36 +0000881 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000882 }
883 else
884 {
885 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000886 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
887 mm = 0; bbbig = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000888 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
Eric Andersen3443bd72003-07-22 07:30:36 +0000889 printf("\tCHS addressing not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000890 else
891 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000892 jj = val[WHATS_VALID] & OK_W54_58;
Glenn L McGrath07085852003-10-09 07:28:22 +0000893 printf("\tLogical\t\tmax\tcurrent\n\tcylinders\t%u\t%u\n\theads\t\t%u\t%u\n\tsectors/track\t%u\t%u\n\t--\n",
894 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
895
896 if_printf(((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])),
897 "\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
898
899 if(jj)
900 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000901 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
Glenn L McGrath07085852003-10-09 07:28:22 +0000902 if(like_std < 3)
903 {
904 /* check Endian of capacity bytes */
Eric Andersen3443bd72003-07-22 07:30:36 +0000905 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
906 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
907 if(abs(mm - nn) > abs(oo - nn))
908 mm = oo;
909 }
910 printf("\tCHS current addressable sectors:%11u\n",mm);
Glenn L McGrath07085852003-10-09 07:28:22 +0000911 }
Eric Andersen3443bd72003-07-22 07:30:36 +0000912 }
913 /* LBA addressing */
914 printf("\tLBA user addressable sectors:%11u\n",ll);
915 if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000916 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
917 {
918 bbbig = (__u64)val[LBA_64_MSB] << 48 |
919 (__u64)val[LBA_48_MSB] << 32 |
920 (__u64)val[LBA_MID] << 16 |
921 val[LBA_LSB] ;
Eric Andersen3443bd72003-07-22 07:30:36 +0000922 printf("\tLBA48 user addressable sectors:%11llu\n",bbbig);
923 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000924
925 if (!bbbig)
926 bbbig = (__u64)(ll>mm ? ll : mm); /* # 512 byte blocks */
Eric Andersen3443bd72003-07-22 07:30:36 +0000927 printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
928 bbbig = (bbbig<<9)/1000000;
929 printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);
Glenn L McGrath07085852003-10-09 07:28:22 +0000930
931 if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
Eric Andersen3443bd72003-07-22 07:30:36 +0000932
933 }
934
935 /* hw support of commands (capabilities) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000936 printf("Capabilities:\n\t");
937
938 if(dev == ATAPI_DEV)
939 {
940 if(eqpt != CDROM)
941 if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");
942
943 if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
Eric Andersen3443bd72003-07-22 07:30:36 +0000944 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000945 if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
946
947 if(like_std != 1)
948 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000949 printf("IORDY");
Glenn L McGrath07085852003-10-09 07:28:22 +0000950 if_printf((!(val[CAPAB_0] & IORDY_SUP)),"(may be)");
951 if_else_printf((val[CAPAB_0] & IORDY_OFF),"(can","(cannot");
952 printf(" be disabled)\n");
953 }
954 else
955 printf("no IORDY\n");
956
957 if((like_std == 1) && val[BUF_TYPE])
958 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000959 kk = val[BUF_TYPE];
960 printf("\tBuffer type: %04x: ",kk);
Glenn L McGrath07085852003-10-09 07:28:22 +0000961 if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
962 if_printf((kk > 2)," with read caching ability");
Eric Andersen3443bd72003-07-22 07:30:36 +0000963 printf("\n");
964 }
965 jj = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000966 if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
967 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000968 printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
969 jj = 1;
970 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000971 if((min_std < 4) && (val[RW_LONG]))
972 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000973 printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
974 jj = 1;
975 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000976 if((eqpt != CDROM) && (like_std > 3))
977 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000978 printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
979 jj = 1;
980 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000981 if_printf(jj,"\n");
982
983 if(dev == ATA_DEV)
984 {
985 if(like_std == 1)
986 printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
987 else
988 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000989 printf("\tStandby timer values: spec'd by ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000990 if_else_printf((val[CAPAB_0] & STD_STBY),"Standard","Vendor");
991 if((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
992 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
993 else
994 printf("\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000995 }
996 printf("\tR/W multiple sector transfer: ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000997 if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
Eric Andersen3443bd72003-07-22 07:30:36 +0000998 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000999 else
1000 {
1001 printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
1002 if_else_printf((val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID),
1003 "%u\n","?\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
Eric Andersen3443bd72003-07-22 07:30:36 +00001004 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001005 if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
1006 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001007 /* We print out elsewhere whether the APM feature is enabled or
1008 not. If it's not enabled, let's not repeat the info; just print
1009 nothing here. */
Glenn L McGrath07085852003-10-09 07:28:22 +00001010 printf("\tAdvancedPM level: ");
1011 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
1012 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001013 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
Eric Andersen3443bd72003-07-22 07:30:36 +00001014 printf("%u (0x%x)\n", apm_level, apm_level);
Glenn L McGrath07085852003-10-09 07:28:22 +00001015 }
1016 else
Eric Andersen3443bd72003-07-22 07:30:36 +00001017 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001018 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001019 if(like_std > 5)
1020 {
1021 if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
1022 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
Eric Andersen3443bd72003-07-22 07:30:36 +00001023 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001024 }
1025 else
1026 {
1027 /* ATAPI */
1028 if(eqpt != CDROM)
1029 if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");
1030
1031 if(val[PKT_REL] || val[SVC_NBSY])
1032 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001033 printf("\tOverlap support:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001034 if_printf(val[PKT_REL]," %uus to release bus.",val[PKT_REL]);
1035 if_printf(val[SVC_NBSY]," %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001036 printf("\n");
1037 }
1038 }
1039
1040 /* DMA stuff. Check that only one DMA mode is selected. */
1041 printf("\tDMA: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001042 if(!(val[CAPAB_0] & DMA_SUP))
Eric Andersen3443bd72003-07-22 07:30:36 +00001043 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001044 else
1045 {
1046 if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
1047 " sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
1048 if(val[SINGLE_DMA])
1049 {
1050 jj = val[SINGLE_DMA];
1051 kk = val[SINGLE_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +00001052 err_dma += mode_loop(jj,kk,'s',&have_mode);
1053 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001054 if(val[MULTI_DMA])
1055 {
1056 jj = val[MULTI_DMA];
1057 kk = val[MULTI_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +00001058 err_dma += mode_loop(jj,kk,'m',&have_mode);
1059 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001060 if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
1061 {
1062 jj = val[ULTRA_DMA];
1063 kk = val[ULTRA_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +00001064 err_dma += mode_loop(jj,kk,'u',&have_mode);
1065 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001066 if_printf((err_dma || !have_mode),"(?)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001067 printf("\n");
1068
Glenn L McGrath07085852003-10-09 07:28:22 +00001069 if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
1070 "\t Interleaved DMA support\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001071
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001072 if((val[WHATS_VALID] & OK_W64_70) &&
Glenn L McGrath07085852003-10-09 07:28:22 +00001073 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
1074 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001075 printf("\t Cycle time:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001076 if_printf(val[DMA_TIME_MIN]," min=%uns",val[DMA_TIME_MIN]);
1077 if_printf(val[DMA_TIME_NORM]," recommended=%uns",val[DMA_TIME_NORM]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001078 printf("\n");
1079 }
1080 }
1081
1082 /* Programmed IO stuff */
1083 printf("\tPIO: ");
1084 /* If a drive supports mode n (e.g. 3), it also supports all modes less
1085 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
Glenn L McGrath07085852003-10-09 07:28:22 +00001086 if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
1087 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001088 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
Glenn L McGrath07085852003-10-09 07:28:22 +00001089 for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
1090 {
1091 if_printf((jj & 0x0001),"pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +00001092 jj >>=1;
1093 }
1094 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001095 }
1096 else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
1097 {
1098 for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
Eric Andersen3443bd72003-07-22 07:30:36 +00001099 printf("pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +00001100 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001101 }
1102 else
1103 printf("unknown\n");
1104
1105 if(val[WHATS_VALID] & OK_W64_70)
1106 {
1107 if(val[PIO_NO_FLOW] || val[PIO_FLOW])
1108 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001109 printf("\t Cycle time:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001110 if_printf(val[PIO_NO_FLOW]," no flow control=%uns", val[PIO_NO_FLOW]);
1111 if_printf(val[PIO_FLOW]," IORDY flow control=%uns", val[PIO_FLOW]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001112 printf("\n");
1113 }
1114 }
1115
Glenn L McGrath07085852003-10-09 07:28:22 +00001116 if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1117 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001118 printf("Commands/features:\n\tEnabled\tSupported:\n");
1119 jj = val[CMDS_SUPP_0];
1120 kk = val[CMDS_EN_0];
Glenn L McGrath07085852003-10-09 07:28:22 +00001121 for(ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
1122 {
1123 if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
1124 {
1125 if_else_printf((kk & 0x8000),"\t *","\t");
Eric Andersen3443bd72003-07-22 07:30:36 +00001126 printf("\t%s\n",cmd_feat_str[ii]);
1127 }
1128 jj <<=1; kk<<=1;
Glenn L McGrath07085852003-10-09 07:28:22 +00001129 if(ii%16 == 15)
1130 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001131 jj = val[CMDS_SUPP_0+1+(ii/16)];
1132 kk = val[CMDS_EN_0+1+(ii/16)];
1133 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001134 if(ii == 31)
1135 {
1136 if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1137 ii +=16;
Eric Andersen3443bd72003-07-22 07:30:36 +00001138 }
1139 }
1140 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001141 if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
1142 "\tRemovable Media Status Notification feature set supported\n");
1143
1144
Eric Andersen3443bd72003-07-22 07:30:36 +00001145 /* security */
Glenn L McGrath07085852003-10-09 07:28:22 +00001146 if((eqpt != CDROM) && (like_std > 3) &&
1147 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1148 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001149 printf("Security: \n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001150 if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
1151 "\tMaster password revision code = %u\n",val[PSWD_CODE]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001152 jj = val[SECU_STATUS];
Glenn L McGrath07085852003-10-09 07:28:22 +00001153 if(jj)
1154 {
1155 for(ii = 0; ii < NUM_SECU_STR; ii++)
1156 {
1157 if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001158 jj >>=1;
1159 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001160 if(val[SECU_STATUS] & SECU_ENABLED)
1161 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001162 printf("\tSecurity level ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001163 if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001164 }
1165 }
1166 jj = val[ERASE_TIME] & ERASE_BITS;
1167 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
Glenn L McGrath07085852003-10-09 07:28:22 +00001168 if(jj || kk)
1169 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001170 printf("\t");
Glenn L McGrath07085852003-10-09 07:28:22 +00001171 if_printf(jj,"%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
1172 if_printf(kk,"%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
Eric Andersen3443bd72003-07-22 07:30:36 +00001173 printf("\n");
1174 }
1175 }
1176
1177 /* reset result */
Glenn L McGrath07085852003-10-09 07:28:22 +00001178 if((val[HWRST_RSLT] & VALID) == VALID_VAL)
1179 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001180 printf("HW reset results:\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001181 if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
1182
1183 if(val[HWRST_RSLT] & RST0)
1184 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001185 printf("\tDevice num = 0");
1186 jj = val[HWRST_RSLT];
Glenn L McGrath07085852003-10-09 07:28:22 +00001187 }
1188 else
1189 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001190 printf("\tDevice num = 1");
1191 jj = val[HWRST_RSLT] >> 8;
1192 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001193
1194 if((jj & DEV_DET) == JUMPER_VAL)
Eric Andersen3443bd72003-07-22 07:30:36 +00001195 printf(" determined by the jumper");
1196 else if((jj & DEV_DET) == CSEL_VAL)
1197 printf(" determined by CSEL");
1198 printf("\n");
1199 }
1200
1201 /* more stuff from std 5 */
Glenn L McGrath07085852003-10-09 07:28:22 +00001202 if((like_std > 4) && (eqpt != CDROM))
1203 {
1204 if(val[CFA_PWR_MODE] & VALID_W160)
1205 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001206 printf("CFA power mode 1:\n\t");
Glenn L McGrath07085852003-10-09 07:28:22 +00001207 if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");
1208
1209 if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
Eric Andersen3443bd72003-07-22 07:30:36 +00001210 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001211
1212 if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001213 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001214 if((val[INTEGRITY] & SIG) == SIG_VAL)
1215 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001216 printf("Checksum: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001217 if_printf(chksum,"in");
Eric Andersen3443bd72003-07-22 07:30:36 +00001218 printf("correct\n");
1219 }
1220 }
1221
1222 exit(0);
1223}
1224#endif
1225
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001226static int verbose, get_identity, get_geom, noisy = 1, quiet;
1227static int flagcount, do_flush, is_scsi_hd, is_xt_hd;
1228static int do_ctimings, do_timings;
Eric Andersen3443bd72003-07-22 07:30:36 +00001229
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001230static unsigned long set_readahead, get_readahead, Xreadahead;
1231static unsigned long set_readonly, get_readonly, readonly;
1232static unsigned long set_unmask, get_unmask, unmask;
1233static unsigned long set_mult, get_mult, mult;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001234#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001235static unsigned long set_dma, get_dma, dma;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001236#endif
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001237static unsigned long set_dma_q, get_dma_q, dma_q;
1238static unsigned long set_nowerr, get_nowerr, nowerr;
1239static unsigned long set_keep, get_keep, keep;
1240static unsigned long set_io32bit, get_io32bit, io32bit;
1241static unsigned long set_piomode, noisy_piomode;
1242static int piomode;
Eric Andersen3443bd72003-07-22 07:30:36 +00001243#ifdef HDIO_DRIVE_CMD
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001244static unsigned long set_dkeep, get_dkeep, dkeep;
1245static unsigned long set_standby, get_standby, standby_requested;
1246static unsigned long set_xfermode, get_xfermode;
1247static int xfermode_requested;
1248static unsigned long set_lookahead, get_lookahead, lookahead;
1249static unsigned long set_prefetch, get_prefetch, prefetch;
1250static unsigned long set_defects, get_defects, defects;
1251static unsigned long set_wcache, get_wcache, wcache;
1252static unsigned long set_doorlock, get_doorlock, doorlock;
1253static unsigned long set_seagate, get_seagate;
1254static unsigned long set_standbynow, get_standbynow;
1255static unsigned long set_sleepnow, get_sleepnow;
1256static unsigned long get_powermode;
1257static unsigned long set_apmmode, get_apmmode, apmmode;
Eric Andersen3443bd72003-07-22 07:30:36 +00001258#endif
1259#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001260static int get_IDentity;
Eric Andersen3443bd72003-07-22 07:30:36 +00001261#endif
1262#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001263static int unregister_hwif;
1264static int hwif;
Eric Andersen3443bd72003-07-22 07:30:36 +00001265#endif
1266#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001267static int scan_hwif;
1268static int hwif_data;
1269static int hwif_ctrl;
1270static int hwif_irq;
Eric Andersen3443bd72003-07-22 07:30:36 +00001271#endif
1272#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001273static int set_busstate, get_busstate, busstate;
Eric Andersen3443bd72003-07-22 07:30:36 +00001274#endif
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001275static int reread_partn;
Eric Andersen3443bd72003-07-22 07:30:36 +00001276
1277#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001278static int perform_reset;
Eric Andersen3443bd72003-07-22 07:30:36 +00001279#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1280#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001281static int perform_tristate, tristate;
Eric Andersen3443bd72003-07-22 07:30:36 +00001282#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1283
1284// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1285// then the HDIO_GET_IDENTITY only returned 142 bytes.
1286// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1287// and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1288// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1289// (which they should, but they should just return -EINVAL).
1290//
1291// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1292// On a really old system, it will not, and we will be confused.
1293// Too bad, really.
1294
1295#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001296static const char * const cfg_str[] =
Eric Andersen3443bd72003-07-22 07:30:36 +00001297{ "", " HardSect", " SoftSect", " NotMFM",
1298 " HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
1299 " DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
1300 " dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
1301};
1302
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001303static const char * const BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
Eric Andersen3443bd72003-07-22 07:30:36 +00001304
Eric Andersen3443bd72003-07-22 07:30:36 +00001305static void dump_identity (const struct hd_driveid *id)
1306{
1307 int i;
1308 char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
1309 const unsigned short int *id_regs= (const void*) id;
1310 unsigned long capacity;
1311
Glenn L McGrath07085852003-10-09 07:28:22 +00001312 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1313 id->model, id->fw_rev, id->serial_no);
1314 for (i=0; i<=15; i++)
1315 if_printf((id->config & (1<<i)),"%s", cfg_str[i]);
1316
1317 printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
1318 id->cyls, id->heads, id->sectors, id->track_bytes,
1319 id->sector_bytes, id->ecc_bytes);
1320
1321 if (id->buf_type > 3)
1322 printf("%s%u", " BuffType=", id->buf_type);
1323 else
1324 printf("%s%s", " BuffType=", BuffType[id->buf_type]);
1325
Eric Andersen3443bd72003-07-22 07:30:36 +00001326 printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
Glenn L McGrath07085852003-10-09 07:28:22 +00001327 if (id->max_multsect)
1328 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001329 printf(", MultSect=");
1330 if (!(id->multsect_valid&1))
1331 printf("?%u?", id->multsect);
1332 else if (id->multsect)
1333 printf("%u", id->multsect);
1334 else
1335 printf("off");
1336 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001337 printf("\n");
1338 if (id->tPIO <= 5)
1339 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001340 strcat(pmodes, "pio0 ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001341 if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
1342 if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
1343
Eric Andersen3443bd72003-07-22 07:30:36 +00001344 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001345 if_printf((!(id->field_valid&1))," (maybe):");
Eric Andersen3443bd72003-07-22 07:30:36 +00001346#if __BYTE_ORDER == __BIG_ENDIAN
1347 capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1348#else
1349 capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1350#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001351 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1352 id->cur_sectors, capacity ,
1353 ((id->capability&2)==0)?"no":"yes");
Eric Andersen3443bd72003-07-22 07:30:36 +00001354
Glenn L McGrath07085852003-10-09 07:28:22 +00001355 if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);
1356
1357 if (id->capability&1)
1358 {
1359 if (id->dma_1word | id->dma_mword)
1360 {
1361 if_strcat((id->dma_1word & 0x100), dmodes, "*");
1362 if_strcat((id->dma_1word & 1), dmodes, "sdma0 ");
1363 if_strcat((id->dma_1word & 0x200), dmodes, "*");
1364 if_strcat((id->dma_1word & 2), dmodes, "sdma1 ");
1365 if_strcat((id->dma_1word & 0x400), dmodes, "*");
1366 if_strcat((id->dma_1word & 4), dmodes, "sdma2 ");
1367 if_strcat((id->dma_1word & 0xf800), dmodes, "*");
1368 if_strcat((id->dma_1word & 0xf8), dmodes, "sdma? ");
1369 if_strcat((id->dma_mword & 0x100), dmodes, "*");
1370 if_strcat((id->dma_mword & 1), dmodes, "mdma0 ");
1371 if_strcat((id->dma_mword & 0x200), dmodes, "*");
1372 if_strcat((id->dma_mword & 2), dmodes, "mdma1 ");
1373 if_strcat((id->dma_mword & 0x400), dmodes, "*");
1374 if_strcat((id->dma_mword & 4), dmodes, "mdma2 ");
1375 if_strcat((id->dma_mword & 0xf800), dmodes, "*");
1376 if_strcat((id->dma_mword & 0xf8), dmodes, "mdma? ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001377 }
1378 }
1379 printf("\n IORDY=");
1380 if (id->capability&8)
1381 printf((id->capability&4) ? "on/off" : "yes");
1382 else
1383 printf("no");
Eric Andersen3443bd72003-07-22 07:30:36 +00001384
Glenn L McGrath07085852003-10-09 07:28:22 +00001385 if ((id->capability&8) || (id->field_valid&2))
1386 {
1387 if (id->field_valid&2)
1388 {
1389 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1390 if_strcat((id->eide_pio_modes & 1), pmodes, "pio3 ");
1391 if_strcat((id->eide_pio_modes & 2), pmodes, "pio4 ");
1392 if_strcat((id->eide_pio_modes &~3), pmodes, "pio? ");
1393 }
1394 if (id->field_valid&4)
1395 {
1396 if_strcat((id->dma_ultra & 0x100),umodes,"*");
1397 if_strcat((id->dma_ultra & 0x001),umodes,"udma0 ");
1398 if_strcat((id->dma_ultra & 0x200),umodes,"*");
1399 if_strcat((id->dma_ultra & 0x002),umodes,"udma1 ");
1400 if_strcat((id->dma_ultra & 0x400),umodes,"*");
1401 if_strcat((id->dma_ultra & 0x004),umodes,"udma2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001402#ifdef __NEW_HD_DRIVE_ID
Glenn L McGrath07085852003-10-09 07:28:22 +00001403 if (id->hw_config & 0x2000)
1404 {
1405#else /* !__NEW_HD_DRIVE_ID */
1406 if (id->word93 & 0x2000)
1407 {
1408#endif /* __NEW_HD_DRIVE_ID */
1409 if_strcat((id->dma_ultra & 0x0800),umodes,"*");
1410 if_strcat((id->dma_ultra & 0x0008),umodes,"udma3 ");
1411 if_strcat((id->dma_ultra & 0x1000),umodes,"*");
1412 if_strcat((id->dma_ultra & 0x0010),umodes,"udma4 ");
1413 if_strcat((id->dma_ultra & 0x2000),umodes,"*");
1414 if_strcat((id->dma_ultra & 0x0020),umodes,"udma5 ");
1415 if_strcat((id->dma_ultra & 0x4000),umodes,"*");
1416 if_strcat((id->dma_ultra & 0x0040),umodes,"udma6 ");
1417 if_strcat((id->dma_ultra & 0x8000),umodes,"*");
1418 if_strcat((id->dma_ultra & 0x0080),umodes,"udma7 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001419 }
1420 }
1421 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001422 if_printf(((id->capability&1) && (id->field_valid&2)),
1423 ", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1424 printf("\n PIO modes: %s", pmodes);
1425 if_printf((*dmodes),"\n DMA modes: %s", dmodes);
1426 if_printf((*umodes),"\n UDMA modes: %s", umodes);
1427
1428 printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1429 if (id_regs[83] & 8)
1430 {
1431 if (!(id_regs[86]&8))
1432 printf(": disabled (255)");
1433 else if ((id_regs[91]&0xFF00)!=0x4000)
1434 printf(": unknown setting");
1435 else
1436 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1437 }
1438 if_printf( (id_regs[82]&0x20)," WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1439#ifdef __NEW_HD_DRIVE_ID
1440 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1441 {
1442 printf("\n Drive conforms to: ");
1443 if_else_printf((id->minor_rev_num <= 31),"%s: ","unknown: ", minor_str[id->minor_rev_num]);
1444 if (id->major_rev_num < 31)
1445 {
1446 for (i=0; i <= 15; i++)
1447 if_printf((id->major_rev_num & (1<<i))," %u", i);
1448 }
1449 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001450#endif /* __NEW_HD_DRIVE_ID */
Glenn L McGrath07085852003-10-09 07:28:22 +00001451 printf("\n\n * signifies the current active mode\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001452}
1453#endif
1454
1455static void flush_buffer_cache (int fd)
1456{
1457 fsync (fd); /* flush buffers */
Glenn L McGrath07085852003-10-09 07:28:22 +00001458 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
Eric Andersen3443bd72003-07-22 07:30:36 +00001459#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001460 sleep(1);
1461 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
Eric Andersen06d4ec22004-03-19 10:53:52 +00001462 bb_perror_msg("HDIO_DRIVE_CMD");
Eric Andersen3443bd72003-07-22 07:30:36 +00001463#endif
1464}
1465
1466static int seek_to_zero (int fd)
1467{
Glenn L McGrath07085852003-10-09 07:28:22 +00001468 if (lseek(fd, (off_t) 0, SEEK_SET))
Eric Andersen3443bd72003-07-22 07:30:36 +00001469 return 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001470 return 0;
1471}
1472
1473static int read_big_block (int fd, char *buf)
1474{
Glenn L McGrath07085852003-10-09 07:28:22 +00001475
1476 const char *string;
Eric Andersen3443bd72003-07-22 07:30:36 +00001477 int i, rc;
Glenn L McGrath07085852003-10-09 07:28:22 +00001478 if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
1479 {
1480 switch(rc)
1481 {
1482 case -1:
1483 string = "read()";
1484 break;
1485 case 0:
1486 string = "read() hit EOF - device too small";
1487 break;
1488 default:
1489 string = "read(%u) returned %u bytes";
Eric Andersen3443bd72003-07-22 07:30:36 +00001490 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001491 bb_error_msg(string, TIMING_BUF_BYTES, rc);
Eric Andersen3443bd72003-07-22 07:30:36 +00001492 return 1;
1493 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001494
Eric Andersen3443bd72003-07-22 07:30:36 +00001495 /* access all sectors of buf to ensure the read fully completed */
1496 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1497 buf[i] &= 1;
1498 return 0;
1499}
1500
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001501static double correction;
Eric Andersen50af12d2003-08-06 08:47:59 +00001502
Eric Andersen06d4ec22004-03-19 10:53:52 +00001503static void do_time (int flag, int fd)
Glenn L McGrath07085852003-10-09 07:28:22 +00001504/*
1505 flag = 0 time_cache
1506 flag = 1 time_device
1507*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001508{
Eric Andersen50af12d2003-08-06 08:47:59 +00001509 int i;
Eric Andersen3443bd72003-07-22 07:30:36 +00001510 char *buf;
1511 double elapsed;
1512 struct itimerval e1, e2;
1513 int shmid;
Eric Andersen50af12d2003-08-06 08:47:59 +00001514 int timing_MB = TIMING_MB;
Eric Andersen3443bd72003-07-22 07:30:36 +00001515
Glenn L McGrath07085852003-10-09 07:28:22 +00001516 if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
1517 {
1518 bb_error_msg (bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001519 return;
1520 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001521 if (shmctl(shmid, SHM_LOCK, NULL) == -1)
1522 {
1523 bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001524 (void) shmctl(shmid, IPC_RMID, NULL);
1525 return;
1526 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001527 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
1528 {
1529 bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001530 (void) shmctl(shmid, IPC_RMID, NULL);
1531 return;
1532 }
1533 if (shmctl(shmid, IPC_RMID, NULL) == -1)
Glenn L McGrath07085852003-10-09 07:28:22 +00001534 bb_error_msg ("shmctl(,IPC_RMID,)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001535
1536 /* Clear out the device request queues & give them time to complete */
Glenn L McGrath07085852003-10-09 07:28:22 +00001537 sync_and_sleep(3);
Eric Andersen3443bd72003-07-22 07:30:36 +00001538
Glenn L McGrath07085852003-10-09 07:28:22 +00001539 if(flag == 0) /* Time cache */
1540 {
1541 /* Calculate a correction factor for the basic
1542 * overhead of doing a read() from the buffer cache.
1543 * To do this, we read the data once to "cache it" and
1544 * to force full preallocation of our timing buffer,
1545 * and then we re-read it 10 times while timing it.
1546 *
1547 * getitimer() is used rather than gettimeofday() because
1548 * it is much more consistent (on my machine, at least).
1549 */
1550 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1551 if (seek_to_zero (fd))
1552 return;
1553 if (read_big_block (fd, buf))
1554 return;
1555 printf(" Timing buffer-cache reads: ");
1556 fflush(stdout);
Eric Andersen3443bd72003-07-22 07:30:36 +00001557
Glenn L McGrath07085852003-10-09 07:28:22 +00001558 /* Clear out the device request queues & give them time to complete */
1559 sync_and_sleep(1);
Eric Andersen3443bd72003-07-22 07:30:36 +00001560
Glenn L McGrath07085852003-10-09 07:28:22 +00001561 /* Time re-reading from the buffer-cache */
1562 getitimer(ITIMER_REAL, &e1);
1563 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1564 {
1565 if (seek_to_zero (fd))
1566 goto quit;
1567 if (read_big_block (fd, buf))
1568 goto quit;
1569 }
1570 getitimer(ITIMER_REAL, &e2);
1571 correction = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
Eric Andersen3443bd72003-07-22 07:30:36 +00001572
Glenn L McGrath07085852003-10-09 07:28:22 +00001573 /* Now remove the lseek() from the correction factor */
1574 getitimer(ITIMER_REAL, &e1);
1575 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1576 {
1577 if (seek_to_zero (fd))
1578 goto quit;
1579 }
1580 getitimer(ITIMER_REAL, &e2);
1581 correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
1582 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
Eric Andersen50af12d2003-08-06 08:47:59 +00001583
Glenn L McGrath07085852003-10-09 07:28:22 +00001584 if ((BUFCACHE_FACTOR * timing_MB) >= correction) /* more than 1MB/s */
Eric Andersen50af12d2003-08-06 08:47:59 +00001585 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
Glenn L McGrath07085852003-10-09 07:28:22 +00001586 (BUFCACHE_FACTOR * timing_MB), correction,
1587 (BUFCACHE_FACTOR * timing_MB) / correction);
Eric Andersen50af12d2003-08-06 08:47:59 +00001588 else
1589 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
Glenn L McGrath07085852003-10-09 07:28:22 +00001590 (BUFCACHE_FACTOR * timing_MB), correction,
1591 (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
1592 correction /= BUFCACHE_FACTOR;
1593
1594 flush_buffer_cache(fd);
1595 sleep(1);
Eric Andersen50af12d2003-08-06 08:47:59 +00001596 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001597 else /* Time device */
1598 {
1599 printf(" Timing buffered disk reads: ");
1600 fflush(stdout);
1601
1602 /*
1603 * getitimer() is used rather than gettimeofday() because
1604 * it is much more consistent (on my machine, at least).
1605 */
1606 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1607
1608 /* Now do the timings for real */
1609 getitimer(ITIMER_REAL, &e1);
1610 for (i = TIMING_BUF_COUNT; i > 0; --i)
1611 {
1612 if (read_big_block (fd, buf))
1613 goto quit;
1614 }
1615 getitimer(ITIMER_REAL, &e2);
1616
1617 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1618
1619 if (timing_MB >= elapsed) /* more than 1MB/s */
1620 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",timing_MB, elapsed, timing_MB / elapsed);
1621 else
1622 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);
1623
1624 /*"Hmm.. suspicious results: probably not enough free memory for a proper test.");*/
1625 if (elapsed <= (correction * 2))
1626 bb_error_msg(bb_msg_memory_exhausted);
1627
1628#if 0 /* the "estimate" is just plain wrong for many systems.. */
1629 else if (correction != 0.0) {
1630 printf(" Estimating raw driver speed: ");
1631 elapsed -= correction;
1632 if (timing_MB >= elapsed) /* more than 1MB/s */
1633 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1634 timing_MB, elapsed, timing_MB / elapsed);
1635 else
1636 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1637 timing_MB, elapsed, timing_MB / elapsed * 1024);
1638 }
Eric Andersen50af12d2003-08-06 08:47:59 +00001639#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001640 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001641quit:
1642 if (-1 == shmdt(buf))
Glenn L McGrath07085852003-10-09 07:28:22 +00001643 bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001644}
1645
Glenn L McGrath07085852003-10-09 07:28:22 +00001646
Eric Andersen3443bd72003-07-22 07:30:36 +00001647static void no_scsi (void)
1648{
Glenn L McGrath07085852003-10-09 07:28:22 +00001649 /*" operation not supported on SCSI disks"*/
1650 if (is_scsi_hd)
1651 bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
Eric Andersen3443bd72003-07-22 07:30:36 +00001652}
1653
1654static void no_xt (void)
1655{
Glenn L McGrath07085852003-10-09 07:28:22 +00001656 if (is_xt_hd)
1657 bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001658}
1659
1660static void on_off (unsigned int value)
1661{
1662 printf(value ? " (on)\n" : " (off)\n");
1663}
1664
1665#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1666static void bus_state_value (unsigned int value)
1667{
1668 const char *string;
1669
Glenn L McGrath07085852003-10-09 07:28:22 +00001670 switch (value)
1671 {
1672 case BUSSTATE_ON:
1673 string = " (on)\n";
1674 break;
1675 case BUSSTATE_OFF:
1676 string = " (off)\n";
1677 break;
1678 case BUSSTATE_TRISTATE:
1679 string = " (tristate)\n";
1680 break;
1681 default:
1682 string = " (unknown: %d)\n";
1683 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001684 }
1685 printf(string, value);
1686}
1687#endif
1688
1689#ifdef HDIO_DRIVE_CMD
1690static void interpret_standby (unsigned int standby)
1691{
1692 printf(" (");
Glenn L McGrath07085852003-10-09 07:28:22 +00001693 switch(standby)
1694 {
1695 case 0:
1696 printf("off");
1697 break;
1698 case 252:
1699 printf("21 minutes");
1700 break;
1701 case 253:
1702 printf("vendor-specific");
1703 break;
1704 case 254:
1705 printf("?reserved");
1706 break;
1707 case 255:
1708 printf("21 minutes + 15 seconds");
1709 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001710 default:
Glenn L McGrath07085852003-10-09 07:28:22 +00001711 if (standby <= 240)
1712 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001713 unsigned int secs = standby * 5;
1714 unsigned int mins = secs / 60;
1715 secs %= 60;
Glenn L McGrath07085852003-10-09 07:28:22 +00001716 if_printf(mins,"%u minutes", mins);
1717 if_printf((mins && secs)," + ");
1718 if_printf(secs,"%u seconds", secs);
1719 }
1720 else if (standby <= 251)
1721 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001722 unsigned int mins = (standby - 240) * 30;
1723 unsigned int hrs = mins / 60;
1724 mins %= 60;
Glenn L McGrath07085852003-10-09 07:28:22 +00001725 if_printf(hrs,"%u hours", hrs);
1726 if_printf((hrs && mins)," + ");
1727 if_printf(mins,"%u minutes", mins);
1728 }
1729 else
Eric Andersen3443bd72003-07-22 07:30:36 +00001730 printf("illegal value");
1731 break;
1732 }
1733 printf(")\n");
1734}
1735
1736struct xfermode_entry {
1737 int val;
1738 const char *name;
1739};
1740
1741static const struct xfermode_entry xfermode_table[] = {
1742 { 8, "pio0" },
1743 { 9, "pio1" },
1744 { 10, "pio2" },
1745 { 11, "pio3" },
1746 { 12, "pio4" },
1747 { 13, "pio5" },
1748 { 14, "pio6" },
1749 { 15, "pio7" },
1750 { 16, "sdma0" },
1751 { 17, "sdma1" },
1752 { 18, "sdma2" },
1753 { 19, "sdma3" },
1754 { 20, "sdma4" },
1755 { 21, "sdma5" },
1756 { 22, "sdma6" },
1757 { 23, "sdma7" },
1758 { 32, "mdma0" },
1759 { 33, "mdma1" },
1760 { 34, "mdma2" },
1761 { 35, "mdma3" },
1762 { 36, "mdma4" },
1763 { 37, "mdma5" },
1764 { 38, "mdma6" },
1765 { 39, "mdma7" },
1766 { 64, "udma0" },
1767 { 65, "udma1" },
1768 { 66, "udma2" },
1769 { 67, "udma3" },
1770 { 68, "udma4" },
1771 { 69, "udma5" },
1772 { 70, "udma6" },
1773 { 71, "udma7" },
1774 { 0, NULL }
1775};
1776
1777static int translate_xfermode(char * name)
1778{
Glenn L McGrath07085852003-10-09 07:28:22 +00001779 const struct xfermode_entry *tmp;
1780 char *endptr;
1781 int val = -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001782
1783
Glenn L McGrath07085852003-10-09 07:28:22 +00001784 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1785 {
1786 if (!strcmp(name, tmp->name))
1787 return tmp->val;
Eric Andersen3443bd72003-07-22 07:30:36 +00001788
Glenn L McGrath07085852003-10-09 07:28:22 +00001789 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001790
Glenn L McGrath07085852003-10-09 07:28:22 +00001791 val = strtol(name, &endptr, 10);
1792 if (*endptr == '\0')
1793 return val;
1794
1795 return -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001796}
1797
1798static void interpret_xfermode (unsigned int xfermode)
1799{
1800 printf(" (");
1801 switch(xfermode) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001802 case 0:
1803 printf("default PIO mode");
1804 break;
1805 case 1:
1806 printf("default PIO mode, disable IORDY");
1807 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001808 case 8:
1809 case 9:
1810 case 10:
1811 case 11:
1812 case 12:
1813 case 13:
1814 case 14:
Glenn L McGrath07085852003-10-09 07:28:22 +00001815 case 15:
1816 printf("PIO flow control mode%u", xfermode-8);
1817 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001818 case 16:
1819 case 17:
1820 case 18:
1821 case 19:
1822 case 20:
1823 case 21:
1824 case 22:
Glenn L McGrath07085852003-10-09 07:28:22 +00001825 case 23:
1826 printf("singleword DMA mode%u", xfermode-16);
1827 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001828 case 32:
1829 case 33:
1830 case 34:
1831 case 35:
1832 case 36:
1833 case 37:
1834 case 38:
Glenn L McGrath07085852003-10-09 07:28:22 +00001835 case 39:
1836 printf("multiword DMA mode%u", xfermode-32);
1837 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001838 case 64:
1839 case 65:
1840 case 66:
1841 case 67:
1842 case 68:
1843 case 69:
1844 case 70:
Glenn L McGrath07085852003-10-09 07:28:22 +00001845 case 71:
1846 printf("UltraDMA mode%u", xfermode-64);
1847 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001848 default:
Glenn L McGrath07085852003-10-09 07:28:22 +00001849 printf("unknown, probably not valid");
1850 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001851 }
1852 printf(")\n");
1853}
1854#endif /* HDIO_DRIVE_CMD */
1855
1856#ifndef VXVM_MAJOR
1857#define VXVM_MAJOR 199
1858#endif
1859
1860#ifndef CCISS_MAJOR
1861#define CCISS_MAJOR 104
1862#endif
1863
1864static void process_dev (char *devname)
1865{
1866 int fd;
1867 static long parm, multcount;
1868 struct stat stat_buf;
1869#ifndef HDIO_DRIVE_CMD
1870 int force_operation = 0;
1871#endif
1872 if (stat(devname,&stat_buf))
1873 bb_perror_msg_and_die(devname);
1874
Glenn L McGrath07085852003-10-09 07:28:22 +00001875 switch(major(stat_buf.st_rdev))
1876 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001877#ifdef SCSI_DISK0_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001878 case (SCSI_DISK0_MAJOR):
1879 case (SCSI_DISK1_MAJOR):
1880 case (SCSI_DISK2_MAJOR):
1881 case (SCSI_DISK3_MAJOR):
1882 case (SCSI_DISK4_MAJOR):
1883 case (SCSI_DISK5_MAJOR):
1884 case (SCSI_DISK6_MAJOR):
1885 case (SCSI_DISK7_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001886#else
Glenn L McGrath07085852003-10-09 07:28:22 +00001887 case (SCSI_DISK_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001888#endif
1889#ifdef MD_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001890 case (MD_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001891#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001892 case (VXVM_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001893#ifdef LVM_BLK_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001894 case (LVM_BLK_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001895#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001896 case (CCISS_MAJOR):
1897 is_scsi_hd = 1;
1898 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001899#ifdef XT_DISK_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001900 case (XT_DISK_MAJOR):
1901 is_xt_hd = 1;
1902 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001903#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001904 case IDE0_MAJOR:
1905 case IDE1_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001906#ifdef IDE2_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001907 case IDE2_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001908#endif
1909#ifdef IDE3_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001910 case IDE3_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001911#endif
1912#ifdef IDE4_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001913 case IDE4_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001914#endif
1915#ifdef IDE5_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001916 case IDE5_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001917#endif
1918#ifdef IDE6_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001919 case IDE6_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001920#endif
1921#ifdef IDE7_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001922 case IDE7_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001923#endif
1924#ifdef IDE8_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001925 case IDE8_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001926#endif
1927#ifdef IDE9_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001928 case IDE9_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001929#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001930 break; /* do nothing */
1931 default:
1932 bb_error_msg_and_die("%s not supported",devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001933 }
1934
Glenn L McGrath07085852003-10-09 07:28:22 +00001935 fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
1936 if_printf( (!quiet),"\n%s:\n", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001937
Glenn L McGrath07085852003-10-09 07:28:22 +00001938 if (set_readahead)
1939 {
Eric Andersenf828da02004-07-20 22:53:59 +00001940 if_printf(get_readahead," setting fs readahead to %ld\n", Xreadahead);
1941 bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001942 }
1943#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001944 if (unregister_hwif)
1945 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001946 no_scsi();
1947 printf(" attempting to unregister hwif#%u\n", hwif);
Glenn L McGrath07085852003-10-09 07:28:22 +00001948 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)hwif,"HDIO_UNREGISTER_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001949 }
1950#endif
1951#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001952 if (scan_hwif)
1953 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001954 int args[3];
1955 no_scsi();
1956 printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
1957 args[0] = hwif_data;
1958 args[1] = hwif_ctrl;
1959 args[2] = hwif_irq;
Glenn L McGrath07085852003-10-09 07:28:22 +00001960 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001961 }
1962#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001963 if (set_piomode)
1964 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001965 no_scsi();
1966 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00001967
1968 if (noisy_piomode)
1969 {
1970 printf(" attempting to ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001971 if (piomode == 255)
Glenn L McGrath07085852003-10-09 07:28:22 +00001972 printf("auto-tune PIO mode\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001973 else if (piomode < 100)
Glenn L McGrath07085852003-10-09 07:28:22 +00001974 printf("set PIO mode to %d\n", piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001975 else if (piomode < 200)
Glenn L McGrath07085852003-10-09 07:28:22 +00001976 printf("set MDMA mode to %d\n", (piomode-100));
Eric Andersen3443bd72003-07-22 07:30:36 +00001977 else
Glenn L McGrath07085852003-10-09 07:28:22 +00001978 printf("set UDMA mode to %d\n", (piomode-200));
Eric Andersen3443bd72003-07-22 07:30:36 +00001979 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001980 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)piomode, "HDIO_SET_PIO_MODE");
Eric Andersen3443bd72003-07-22 07:30:36 +00001981 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001982 if (set_io32bit)
1983 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001984 no_scsi();
1985 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00001986 if_printf(get_io32bit," setting 32-bit IO_support flag to %ld\n", io32bit);
1987 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001988 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001989 if (set_mult)
1990 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001991 no_scsi();
1992 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00001993 if_printf(get_mult, " setting multcount to %ld\n", mult);
1994 if(ioctl(fd, HDIO_SET_MULTCOUNT, mult))
Eric Andersen06d4ec22004-03-19 10:53:52 +00001995 bb_perror_msg("HDIO_SET_MULTCOUNT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001996#ifndef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001997 else
1998 force_operation = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001999#endif
2000 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002001 if (set_readonly)
2002 {
2003 if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
2004 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
Eric Andersen3443bd72003-07-22 07:30:36 +00002005 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002006 if (set_unmask)
2007 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002008 no_scsi();
2009 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002010 if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
2011 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
Eric Andersen3443bd72003-07-22 07:30:36 +00002012 }
Eric Andersen0a57a792003-08-06 08:57:35 +00002013#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Glenn L McGrath07085852003-10-09 07:28:22 +00002014 if (set_dma)
2015 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002016 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002017 if_printf_on_off(get_dma," setting using_dma to %ld", dma);
2018 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00002019 }
Eric Andersen0a57a792003-08-06 08:57:35 +00002020#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
Glenn L McGrath07085852003-10-09 07:28:22 +00002021 if (set_dma_q)
2022 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002023 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002024 if_printf_on_off(get_dma_q," setting DMA queue_depth to %ld", dma_q);
2025 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00002026 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002027 if (set_nowerr)
2028 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002029 no_scsi();
2030 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002031 if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
2032 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
Eric Andersen3443bd72003-07-22 07:30:36 +00002033 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002034 if (set_keep)
2035 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002036 no_scsi();
2037 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002038 if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
2039 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
Eric Andersen3443bd72003-07-22 07:30:36 +00002040 }
2041#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00002042 if (set_doorlock)
2043 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002044 unsigned char args[4] = {0,0,0,0};
2045 no_scsi();
2046 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002047
Eric Andersen3443bd72003-07-22 07:30:36 +00002048 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
Glenn L McGrath07085852003-10-09 07:28:22 +00002049 if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
2050 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002051 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002052 if (set_dkeep)
2053 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002054 /* lock/unlock the drive's "feature" settings */
2055 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2056 no_scsi();
2057 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002058
2059 if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
Eric Andersen3443bd72003-07-22 07:30:36 +00002060 args[2] = dkeep ? 0x66 : 0xcc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002061 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002062 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002063 if (set_defects)
2064 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002065 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
2066 no_scsi();
2067 args[2] = defects ? 0x04 : 0x84;
Glenn L McGrath07085852003-10-09 07:28:22 +00002068 if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
2069 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002070 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002071 if (set_prefetch)
2072 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002073 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
2074 no_scsi();
2075 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002076
Eric Andersen3443bd72003-07-22 07:30:36 +00002077 args[1] = prefetch;
Glenn L McGrath07085852003-10-09 07:28:22 +00002078 if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
2079 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002080 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002081 if (set_xfermode)
2082 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002083 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
2084 no_scsi();
2085 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002086
Eric Andersen3443bd72003-07-22 07:30:36 +00002087 args[1] = xfermode_requested;
Glenn L McGrath07085852003-10-09 07:28:22 +00002088 if (get_xfermode)
2089 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002090 printf(" setting xfermode to %d", xfermode_requested);
2091 interpret_xfermode(xfermode_requested);
2092 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002093 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002094 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002095 if (set_lookahead)
2096 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002097 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2098 no_scsi();
2099 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002100
Eric Andersen3443bd72003-07-22 07:30:36 +00002101 args[2] = lookahead ? 0xaa : 0x55;
Glenn L McGrath07085852003-10-09 07:28:22 +00002102 if_printf_on_off(get_lookahead," setting drive read-lookahead to %ld", lookahead);
2103 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002104 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002105 if (set_apmmode)
2106 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002107 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2108 no_scsi();
Eric Andersenb2aa7762004-04-05 13:08:08 +00002109 apmmode=check_if_min_and_set_val(apmmode,1);
2110 apmmode=check_if_maj_and_set_val(apmmode,255);
Glenn L McGrath07085852003-10-09 07:28:22 +00002111 if_printf(get_apmmode," setting APM level to");
2112 if (apmmode==255)
2113 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002114 /* disable Advanced Power Management */
Glenn L McGrath07085852003-10-09 07:28:22 +00002115 args[2] = 0x85; /* feature register */
2116 if_printf(get_apmmode," disabled\n");
2117 }
2118 else
2119 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002120 /* set Advanced Power Management mode */
Glenn L McGrath07085852003-10-09 07:28:22 +00002121 args[2] = 0x05; /* feature register */
Eric Andersen3443bd72003-07-22 07:30:36 +00002122 args[1] = apmmode; /* sector count register */
Glenn L McGrath07085852003-10-09 07:28:22 +00002123 if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
2124 }
2125 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
Eric Andersen3443bd72003-07-22 07:30:36 +00002126 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002127 if (set_wcache)
2128 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002129#ifdef DO_FLUSHCACHE
2130#ifndef WIN_FLUSHCACHE
2131#define WIN_FLUSHCACHE 0xe7
2132#endif
2133 unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
2134#endif /* DO_FLUSHCACHE */
2135 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2136 no_scsi();
2137 no_xt();
2138 args[2] = wcache ? 0x02 : 0x82;
Glenn L McGrath07085852003-10-09 07:28:22 +00002139 if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00002140#ifdef DO_FLUSHCACHE
2141 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002142 bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002143#endif /* DO_FLUSHCACHE */
Glenn L McGrath07085852003-10-09 07:28:22 +00002144 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002145#ifdef DO_FLUSHCACHE
2146 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002147 bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002148#endif /* DO_FLUSHCACHE */
2149 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002150 if (set_standbynow)
2151 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002152#ifndef WIN_STANDBYNOW1
2153#define WIN_STANDBYNOW1 0xE0
2154#endif
2155#ifndef WIN_STANDBYNOW2
2156#define WIN_STANDBYNOW2 0x94
2157#endif
2158 unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
2159 unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
2160 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002161 if_printf(get_standbynow," issuing standby command\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002162 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2163 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002164 bb_perror_msg("HDIO_DRIVE_CMD(standby)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002165 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002166 if (set_sleepnow)
2167 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002168#ifndef WIN_SLEEPNOW1
2169#define WIN_SLEEPNOW1 0xE6
2170#endif
2171#ifndef WIN_SLEEPNOW2
2172#define WIN_SLEEPNOW2 0x99
2173#endif
2174 unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2175 unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2176 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002177 if_printf(get_sleepnow," issuing sleep command\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002178 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2179 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002180 bb_perror_msg("HDIO_DRIVE_CMD(sleep)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002181 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002182 if (set_seagate)
2183 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002184 unsigned char args[4] = {0xfb,0,0,0};
2185 no_scsi();
2186 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002187 if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
2188 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002189 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002190 if (set_standby)
2191 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002192 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2193 no_scsi();
2194 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002195 if (get_standby)
2196 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002197 printf(" setting standby to %lu", standby_requested);
2198 interpret_standby(standby_requested);
2199 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002200 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002201 }
2202#else /* HDIO_DRIVE_CMD */
Glenn L McGrath07085852003-10-09 07:28:22 +00002203 if (force_operation)
2204 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002205 char buf[512];
2206 flush_buffer_cache(fd);
2207 if (-1 == read(fd, buf, sizeof(buf)))
Glenn L McGrath07085852003-10-09 07:28:22 +00002208 bb_error_msg("access failed");
Eric Andersen3443bd72003-07-22 07:30:36 +00002209 }
2210#endif /* HDIO_DRIVE_CMD */
2211
2212 if (!flagcount)
2213 verbose = 1;
2214
Glenn L McGrath07085852003-10-09 07:28:22 +00002215 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
2216 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002217 no_scsi();
2218 multcount = -1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002219 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
2220 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002221 if ((verbose && !is_xt_hd) || get_mult)
Eric Andersen06d4ec22004-03-19 10:53:52 +00002222 bb_perror_msg("HDIO_GET_MULTCOUNT");
Glenn L McGrath07085852003-10-09 07:28:22 +00002223 }
2224 else if (verbose | get_mult)
2225 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002226 printf(" multcount = %2ld", multcount);
2227 on_off(multcount);
2228 }
2229 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002230 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
2231 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002232 no_scsi();
2233 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002234 if(ioctl(fd, HDIO_GET_32BIT, &parm))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002235 bb_perror_msg("HDIO_GET_32BIT");
Glenn L McGrath07085852003-10-09 07:28:22 +00002236 else
2237 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002238 printf(" IO_support =%3ld (", parm);
Glenn L McGrath07085852003-10-09 07:28:22 +00002239 switch (parm)
2240 {
2241 case 0:
2242 printf("default ");
2243 case 2:
2244 printf("16-bit)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002245 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002246 case 1:
2247 printf("32-bit)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002248 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002249 case 3:
2250 printf("32-bit w/sync)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002251 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002252 case 8:
2253 printf("Request-Queue-Bypass)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002254 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002255 default:
2256 printf("\?\?\?)\n");
2257 /*esac*/
Eric Andersen3443bd72003-07-22 07:30:36 +00002258 }
2259 }
2260 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002261 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
2262 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002263 no_scsi();
2264 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002265 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
2266 "HDIO_GET_UNMASKINTR"," unmaskirq = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002267 }
2268
Glenn L McGrath07085852003-10-09 07:28:22 +00002269
Eric Andersen0a57a792003-08-06 08:57:35 +00002270#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Eric Andersen3443bd72003-07-22 07:30:36 +00002271 if ((verbose && !is_scsi_hd) || get_dma) {
2272 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002273 if(ioctl(fd, HDIO_GET_DMA, &parm))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002274 bb_perror_msg("HDIO_GET_DMA");
Glenn L McGrath07085852003-10-09 07:28:22 +00002275 else
2276 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002277 printf(" using_dma = %2ld", parm);
2278 if (parm == 8)
2279 printf(" (DMA-Assisted-PIO)\n");
2280 else
2281 on_off(parm);
2282 }
2283 }
2284#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00002285 if (get_dma_q)
2286 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002287 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002288 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
2289 "HDIO_GET_QDMA"," queue_depth = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002290 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002291 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
2292 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002293 no_scsi();
2294 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002295 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
2296 "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002297 }
2298
Glenn L McGrath07085852003-10-09 07:28:22 +00002299 if (get_nowerr)
2300 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002301 no_scsi();
2302 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002303 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
2304 " HDIO_GET_NOWERR"," nowerr = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002305 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002306 if (verbose || get_readonly)
2307 {
2308 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
2309 " BLKROGET"," readonly = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002310 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002311 if ((verbose && !is_scsi_hd) || get_readahead)
2312 {
2313 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
2314 " BLKRAGET"," readahead = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002315 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002316 if (verbose || get_geom)
2317 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002318 static const char msg[] = " geometry = %u/%u/%u, sectors = %ld, start = %ld\n";
2319 static struct hd_geometry g;
2320#ifdef HDIO_GETGEO_BIG
2321 static struct hd_big_geometry bg;
2322#endif
2323
2324 if (ioctl(fd, BLKGETSIZE, &parm))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002325 bb_perror_msg("BLKGETSIZE");
Eric Andersen3443bd72003-07-22 07:30:36 +00002326#ifdef HDIO_GETGEO_BIG
2327 else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
2328 printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
2329#endif
2330 else if (ioctl(fd, HDIO_GETGEO, &g))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002331 bb_perror_msg("HDIO_GETGEO");
Glenn L McGrath07085852003-10-09 07:28:22 +00002332 else
2333 printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
Eric Andersen3443bd72003-07-22 07:30:36 +00002334 }
2335#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00002336 if (get_powermode)
2337 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002338#ifndef WIN_CHECKPOWERMODE1
2339#define WIN_CHECKPOWERMODE1 0xE5
2340#endif
2341#ifndef WIN_CHECKPOWERMODE2
2342#define WIN_CHECKPOWERMODE2 0x98
2343#endif
2344 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
2345 const char *state;
2346 no_scsi();
2347 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
Glenn L McGrath07085852003-10-09 07:28:22 +00002348 && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2349 && ioctl(fd, HDIO_DRIVE_CMD, &args))
2350 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002351 if (errno != EIO || args[0] != 0 || args[1] != 0)
2352 state = "unknown";
2353 else
2354 state = "sleeping";
Eric Andersen3443bd72003-07-22 07:30:36 +00002355 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002356 else
2357 state = (args[2] == 255) ? "active/idle" : "standby";
2358
Eric Andersen3443bd72003-07-22 07:30:36 +00002359 printf(" drive state is: %s\n", state);
2360 }
2361#endif
2362#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
Glenn L McGrath07085852003-10-09 07:28:22 +00002363 if (perform_reset)
2364 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002365 no_scsi();
2366 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002367 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
Eric Andersen3443bd72003-07-22 07:30:36 +00002368 }
2369#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2370#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002371 if (perform_tristate)
2372 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002373 unsigned char args[4] = {0,tristate,0,0};
2374 no_scsi();
2375 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002376 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00002377 }
2378#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2379#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +00002380 if (get_identity)
2381 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002382 static struct hd_driveid id;
2383
2384 no_scsi();
2385 no_xt();
2386
Glenn L McGrath07085852003-10-09 07:28:22 +00002387 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
2388 {
2389 if (multcount != -1)
2390 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002391 id.multsect = multcount;
2392 id.multsect_valid |= 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002393 }
2394 else
Eric Andersen3443bd72003-07-22 07:30:36 +00002395 id.multsect_valid &= ~1;
2396 dump_identity(&id);
Glenn L McGrath07085852003-10-09 07:28:22 +00002397 }
2398 else if (errno == -ENOMSG)
Eric Andersen3443bd72003-07-22 07:30:36 +00002399 printf(" no identification info available\n");
2400 else
Eric Andersen06d4ec22004-03-19 10:53:52 +00002401 bb_perror_msg("HDIO_GET_IDENTITY");
Eric Andersen3443bd72003-07-22 07:30:36 +00002402 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002403
2404 if (get_IDentity)
2405 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002406 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2407 unsigned i;
Glenn L McGrath07085852003-10-09 07:28:22 +00002408
Eric Andersen3443bd72003-07-22 07:30:36 +00002409 no_scsi();
2410 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002411
2412 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2413 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002414 args[0] = WIN_PIDENTIFY;
Glenn L McGrath07085852003-10-09 07:28:22 +00002415 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2416 {
Eric Andersen06d4ec22004-03-19 10:53:52 +00002417 bb_perror_msg("HDIO_DRIVE_CMD(identify)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002418 goto identify_abort;
2419 }
2420 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002421 for(i=0; i<(sizeof args)/2; i+=2)
Eric Andersen3443bd72003-07-22 07:30:36 +00002422 __le16_to_cpus((uint16_t *)(&args[i]));
Glenn L McGrath07085852003-10-09 07:28:22 +00002423
Eric Andersen3443bd72003-07-22 07:30:36 +00002424 identify((void *)&args[4], NULL);
Glenn L McGrath07085852003-10-09 07:28:22 +00002425identify_abort:
2426 /* VOID */;
Eric Andersen3443bd72003-07-22 07:30:36 +00002427 }
2428#endif
2429#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002430 if (set_busstate)
2431 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002432 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002433 if (get_busstate)
2434 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002435 printf(" setting bus state to %d", busstate);
2436 bus_state_value(busstate);
2437 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002438 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)busstate, "HDIO_SET_BUSSTATE");
Eric Andersen3443bd72003-07-22 07:30:36 +00002439 }
2440#endif
2441#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002442 if (get_busstate)
2443 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002444 no_scsi();
2445 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002446 bb_perror_msg("HDIO_GET_BUSSTATE");
Glenn L McGrath07085852003-10-09 07:28:22 +00002447 else
2448 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002449 printf(" busstate = %2ld", parm);
2450 bus_state_value(parm);
2451 }
2452 }
2453#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00002454 if (reread_partn)
2455 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2456
Eric Andersen3443bd72003-07-22 07:30:36 +00002457
2458 if (do_ctimings)
Glenn L McGrath07085852003-10-09 07:28:22 +00002459 do_time(0,fd); /*time cache */
Eric Andersen3443bd72003-07-22 07:30:36 +00002460 if (do_timings)
Glenn L McGrath07085852003-10-09 07:28:22 +00002461 do_time(1,fd); /*time device */
Eric Andersen3443bd72003-07-22 07:30:36 +00002462 if (do_flush)
2463 flush_buffer_cache (fd);
2464 close (fd);
2465}
2466
Eric Andersen06d4ec22004-03-19 10:53:52 +00002467static char * GET_NUMBER(char *p, unsigned long *flag, unsigned long *num)
2468{
2469 *num = 0;
2470 while (isdigit(*p)) {
2471 *flag = 1;
2472 *num = (*num * 10) + (*p++ - '0');
2473 }
2474 return p;
2475}
2476
2477static char * GET_STRING(char *p, unsigned long *flag, int *num)
2478{
2479 char *tmpstr;
2480 char name[32];
2481 tmpstr = name;
2482 tmpstr[0] = '\0';
2483 while (isalnum(*p) && (tmpstr - name) < 31) {
2484 tmpstr[0] = *p++;
2485 tmpstr[1] = '\0';
2486 ++tmpstr;
2487 }
2488 *num = translate_xfermode(name);
2489 if (*num == -1)
2490 *flag = 0;
2491 else
2492 *flag = 1;
2493 return p;
2494}
2495
Eric Andersen3443bd72003-07-22 07:30:36 +00002496#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2497static int fromhex (unsigned char c)
2498{
2499 if (c >= 'a' && c <= 'f')
2500 return 10 + (c - 'a');
2501 if (c >= '0' && c <= '9')
2502 return (c - '0');
2503 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2504}
2505
2506static int identify_from_stdin (void)
2507{
2508 unsigned short sbuf[800];
2509 unsigned char buf[1600], *b = (unsigned char *)buf;
2510 int i, count = read(0, buf, 1280);
2511
2512 if (count != 1280)
2513 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
Glenn L McGrath07085852003-10-09 07:28:22 +00002514 for (i = 0; count >= 4; ++i)
2515 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002516 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2517 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2518 b += 5;
2519 count -= 5;
2520 }
2521 identify(sbuf, NULL);
2522 return 0;
2523}
2524#endif
2525
Eric Andersenb2aa7762004-04-05 13:08:08 +00002526static void missing_arg(int arg, char c, char* add)
2527{
2528 if (!arg)
2529 bb_error_msg("-%c: missing value %s", c, (add!=NULL)? add :"");
2530}
2531
Eric Andersen3443bd72003-07-22 07:30:36 +00002532/* our main() routine: */
2533int hdparm_main(int argc, char **argv)
2534{
2535 char c, *p;
Eric Andersen3443bd72003-07-22 07:30:36 +00002536
2537 ++argv;
2538 if (!--argc)
2539 bb_show_usage();
Glenn L McGrath07085852003-10-09 07:28:22 +00002540
2541 while (argc--)
2542 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002543#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +00002544 if (!strcmp("-Istdin", *argv))
2545 {
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00002546 return identify_from_stdin();
Eric Andersen3443bd72003-07-22 07:30:36 +00002547 }
2548#endif
2549 p = *argv++;
Glenn L McGrath07085852003-10-09 07:28:22 +00002550 if (*p == '-')
2551 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002552 if (!*++p)
2553 bb_show_usage();
Glenn L McGrath07085852003-10-09 07:28:22 +00002554 while ((c = *p++))
2555 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002556 ++flagcount;
Glenn L McGrath07085852003-10-09 07:28:22 +00002557 switch (c)
2558 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002559 case 'V':
Glenn L McGrath07085852003-10-09 07:28:22 +00002560 /*bb_error_msg_and_die("%s", VERSION);*/
2561 /* We have to return 0 here and not 1 */
2562 printf("%s %s\n",bb_applet_name, VERSION);
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00002563 return EXIT_SUCCESS;
Eric Andersen3443bd72003-07-22 07:30:36 +00002564 case 'v':
2565 verbose = 1;
2566 break;
2567#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2568 case 'I':
2569 get_IDentity = 1;
2570 break;
2571 case 'i':
2572 get_identity = 1;
2573 break;
2574#endif
2575 case 'g':
2576 get_geom = 1;
2577 break;
2578 case 'f':
2579 do_flush = 1;
2580 break;
2581 case 'q':
2582 quiet = 1;
2583 noisy = 0;
2584 break;
2585 case 'u':
2586 get_unmask = noisy;
2587 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002588 if (!*p && argc && isdigit(**argv))
2589 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002590 if((set_unmask = set_flag(p,'1'))==1)
2591 unmask = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002592 break;
Eric Andersen0a57a792003-08-06 08:57:35 +00002593#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Eric Andersen3443bd72003-07-22 07:30:36 +00002594 case 'd':
2595 get_dma = noisy;
2596 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002597 if (!*p && argc && isdigit(**argv))
2598 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002599 if((set_dma = set_flag(p,'9'))==1)
Eric Andersen3443bd72003-07-22 07:30:36 +00002600 dma = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002601 break;
Eric Andersen0a57a792003-08-06 08:57:35 +00002602#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
Eric Andersen3443bd72003-07-22 07:30:36 +00002603 case 'n':
2604 get_nowerr = noisy;
2605 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002606 if (!*p && argc && isdigit(**argv))
2607 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002608 if((set_nowerr = set_flag(p,'1'))==1)
2609 nowerr = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002610 break;
2611 case 'p':
2612 noisy_piomode = noisy;
2613 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002614 if (!*p && argc && isalnum(**argv))
2615 p = *argv++, --argc;
2616 p=GET_STRING(p,&set_piomode,&piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00002617 break;
2618 case 'r':
2619 get_readonly = noisy;
2620 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002621 if (!*p && argc && isdigit(**argv))
2622 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002623 if((set_readonly = set_flag(p,'1'))==1)
2624 readonly = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002625 break;
2626 case 'm':
2627 get_mult = noisy;
2628 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002629 if (!*p && argc && isalnum(**argv))
2630 p = *argv++, --argc;
2631 p=GET_NUMBER(p,&set_mult,&mult);
Eric Andersen3443bd72003-07-22 07:30:36 +00002632 break;
2633 case 'c':
2634 get_io32bit = noisy;
2635 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002636 if (!*p && argc && isalnum(**argv))
2637 p = *argv++, --argc;
2638 p=GET_NUMBER(p,&set_io32bit,&io32bit);
Eric Andersen3443bd72003-07-22 07:30:36 +00002639 break;
2640#ifdef HDIO_DRIVE_CMD
2641 case 'S':
2642 get_standby = noisy;
2643 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002644 if (!*p && argc && isalnum(**argv))
2645 p = *argv++, --argc;
2646 p=GET_NUMBER(p,&set_standby,&standby_requested);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002647 missing_arg(set_standby, c, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00002648 break;
2649
2650 case 'D':
2651 get_defects = noisy;
2652 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002653 if (!*p && argc && isalnum(**argv))
2654 p = *argv++, --argc;
2655 p=GET_NUMBER(p,&set_defects,&defects);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002656 missing_arg(set_defects, c, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00002657 break;
2658 case 'P':
2659 get_prefetch = noisy;
2660 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002661 if (!*p && argc && isalnum(**argv))
2662 p = *argv++, --argc;
2663 p=GET_NUMBER(p,&set_prefetch,&prefetch);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002664 missing_arg(set_prefetch, c, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00002665 break;
2666
2667 case 'X':
2668 get_xfermode = noisy;
2669 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002670 if (!*p && argc && isalnum(**argv))
2671 p = *argv++, --argc;
2672 p=GET_STRING(p,&set_xfermode,&xfermode_requested);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002673 missing_arg(set_xfermode, c, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00002674 break;
2675
2676 case 'K':
2677 get_dkeep = noisy;
2678 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002679 if (!*p && argc && isdigit(**argv))
2680 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002681 if((set_dkeep = set_flag(p,'1'))==1)
2682 dkeep = *p++ - '0';
2683 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002684 goto missing_arg_error;
Eric Andersen3443bd72003-07-22 07:30:36 +00002685 break;
2686
2687 case 'A':
2688 get_lookahead = noisy;
2689 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002690 if (!*p && argc && isdigit(**argv))
2691 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002692 if((set_lookahead = set_flag(p,'1'))==1)
2693 lookahead = *p++ - '0';
2694 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002695 goto missing_arg_error;
Eric Andersen3443bd72003-07-22 07:30:36 +00002696 break;
2697
2698 case 'L':
2699 get_doorlock = noisy;
2700 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002701 if (!*p && argc && isdigit(**argv))
2702 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002703 if((set_doorlock = set_flag(p,'1'))==1)
2704 doorlock = *p++ - '0';
2705 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002706 goto missing_arg_error;
Eric Andersen3443bd72003-07-22 07:30:36 +00002707 break;
2708
2709 case 'W':
2710 get_wcache = noisy;
2711 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002712 if (!*p && argc && isdigit(**argv))
2713 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002714 if((set_wcache = set_flag(p,'1'))==1)
2715 wcache = *p++ - '0';
2716 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002717missing_arg_error:
2718 missing_arg(1, c, "(0/1)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002719 break;
2720
2721 case 'C':
2722 get_powermode = noisy;
2723 noisy = 1;
2724 break;
2725
2726 case 'y':
2727 get_standbynow = noisy;
2728 noisy = 1;
2729 set_standbynow = 1;
2730 break;
2731
2732 case 'Y':
2733 get_sleepnow = noisy;
2734 noisy = 1;
2735 set_sleepnow = 1;
2736 break;
2737
2738 case 'z':
2739 reread_partn = 1;
2740 break;
2741
2742 case 'Z':
2743 get_seagate = noisy;
2744 noisy = 1;
2745 set_seagate = 1;
2746 break;
2747#endif /* HDIO_DRIVE_CMD */
2748 case 'k':
2749 get_keep = noisy;
2750 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002751 if (!*p && argc && isdigit(**argv))
2752 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002753 if((set_keep = set_flag(p,'1'))==1)
2754 keep = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002755 break;
2756#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2757 case 'U':
Eric Andersen06d4ec22004-03-19 10:53:52 +00002758 if (!*p && argc && isdigit(**argv))
2759 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002760 if(! p)
Eric Andersenb2aa7762004-04-05 13:08:08 +00002761 goto expected_hwif_error; /* "expected hwif_nr" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002762
2763 sscanf(p++, "%i", &hwif);
2764
2765 unregister_hwif = 1;
2766 break;
2767#endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2768#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2769 case 'R':
Eric Andersen06d4ec22004-03-19 10:53:52 +00002770 if (!*p && argc && isdigit(**argv))
2771 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002772 if(! p)
Eric Andersenb2aa7762004-04-05 13:08:08 +00002773 goto expected_hwif_error; /* "expected hwif_data" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002774
2775 sscanf(p++, "%i", &hwif_data);
2776
2777 if (argc && isdigit(**argv))
2778 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002779 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002780 goto expected_hwif_error; /* "expected hwif_ctrl" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002781
2782 sscanf(p, "%i", &hwif_ctrl);
2783
2784 if (argc && isdigit(**argv))
2785 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002786 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002787expected_hwif_error:
Glenn L McGrath07085852003-10-09 07:28:22 +00002788 bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002789
2790 sscanf(p, "%i", &hwif_irq);
2791
2792 *p = '\0';
2793
2794 scan_hwif = 1;
2795 break;
2796#endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2797 case 'Q':
Eric Andersen06d4ec22004-03-19 10:53:52 +00002798#ifdef HDIO_GET_QDMA
Eric Andersen3443bd72003-07-22 07:30:36 +00002799 get_dma_q = noisy;
2800 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002801#ifdef HDIO_SET_QDMA
2802 if (!*p && argc && isalnum(**argv))
2803 p = *argv++, --argc;
2804 p=GET_NUMBER(p,&set_dma_q,&dma_q);
2805#ifdef HDIO_GET_QDMA
2806 dma_q = -dma_q;
2807#endif
2808#endif
2809#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00002810 break;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002811
Eric Andersen3443bd72003-07-22 07:30:36 +00002812#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2813 case 'w':
2814 perform_reset = 1;
2815 break;
2816#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2817#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2818 case 'x':
Eric Andersen06d4ec22004-03-19 10:53:52 +00002819 if (!*p && argc && isdigit(**argv))
2820 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002821 if((perform_tristate = set_flag(p,'1'))==1)
2822 tristate = *p++ - '0';
2823 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002824 missing_arg(1, c, "(0/1)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002825 break;
2826
2827#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2828 case 'a':
2829 get_readahead = noisy;
2830 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002831 if (!*p && argc && isalnum(**argv))
2832 p = *argv++, --argc;
Eric Andersenf828da02004-07-20 22:53:59 +00002833 p=GET_NUMBER(p,&set_readahead,&Xreadahead);
Eric Andersen3443bd72003-07-22 07:30:36 +00002834 break;
2835 case 'B':
2836 get_apmmode = noisy;
2837 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002838 if (!*p && argc && isalnum(**argv))
2839 p = *argv++, --argc;
2840 p=GET_NUMBER(p,&set_apmmode,&apmmode);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002841 missing_arg(set_apmmode, c, "(1-255)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002842 break;
2843 case 't':
2844 do_timings = 1;
2845 do_flush = 1;
2846 break;
2847 case 'T':
2848 do_ctimings = 1;
2849 do_flush = 1;
2850 break;
2851#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2852 case 'b':
2853 get_busstate = noisy;
2854 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002855 if (!*p && argc && isdigit(**argv))
2856 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002857 if((set_busstate = set_flag(p,'2'))==1)
2858 busstate = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002859 break;
2860#endif
2861 case 'h':
2862 default:
2863 bb_show_usage();
2864 }
2865 }
2866 if (!argc)
2867 bb_show_usage();
Eric Andersen06d4ec22004-03-19 10:53:52 +00002868 } else {
Glenn L McGrath07085852003-10-09 07:28:22 +00002869 process_dev (p);
Eric Andersen06d4ec22004-03-19 10:53:52 +00002870 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002871 }
2872 return 0 ;
2873}