blob: 5635ffd69af2504731c50d90b382ff355d7258d5 [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
168static const char *pkt_str[] = {
169 "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};
202static const char *ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
203 "reserved", /* bit 0 */
204 "hard sectored", /* bit 1 */
205 "soft sectored", /* bit 2 */
206 "not MFM encoded ", /* bit 3 */
207 "head switch time > 15us", /* bit 4 */
208 "spindle motor control option", /* bit 5 */
209 "fixed drive", /* bit 6 */
210 "removable drive", /* bit 7 */
211 "disk xfer rate <= 5Mbs", /* bit 8 */
212 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
213 "disk xfer rate > 5Mbs", /* bit 10 */
214 "rotational speed tol.", /* bit 11 */
215 "data strobe offset option", /* bit 12 */
216 "track offset option", /* bit 13 */
217 "format speed tolerance gap reqd", /* bit 14 */
218 "ATAPI" /* bit 14 */
219};
220#endif
221
222/* word 1: number of logical cylinders */
223#define LCYLS_MAX 0x3fff /* maximum allowable value */
224
Eric Andersenaff114c2004-04-14 17:51:38 +0000225/* word 2: specific configuration
Eric Andersen3443bd72003-07-22 07:30:36 +0000226 * (a) require SET FEATURES to spin-up
227 * (b) require spin-up to fully reply to IDENTIFY DEVICE
228 */
229#define STBY_NID_VAL 0x37c8 /* (a) and (b) */
230#define STBY_ID_VAL 0x738c /* (a) and not (b) */
231#define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
232#define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
233
234/* words 47 & 59: sector_xfer_max & sector_xfer_cur */
235#define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
236#define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
237
238/* word 49: capabilities 0 */
239#define STD_STBY 0x2000 /* 1=standard values supported (ATA);
240 0=vendor specific values */
241#define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
242#define IORDY_OFF 0x0400 /* 1=may be disabled */
243#define LBA_SUP 0x0200 /* 1=Logical Block Address support */
244#define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
245#define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
246#define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
247#define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
248#define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
249
250/* word 50: capabilities 1 */
251#define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
252
253/* words 51 & 52: PIO & DMA cycle times */
254#define MODE 0xff00 /* the mode is in the MSBs */
255
256/* word 53: whats_valid */
257#define OK_W88 0x0004 /* the ultra_dma info is valid */
258#define OK_W64_70 0x0002 /* see above for word descriptions */
259#define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
260
261/*word 63,88: dma_mode, ultra_dma_mode*/
262#define MODE_MAX 7 /* bit definitions force udma <=7 (when
263 * udma >=8 comes out it'll have to be
264 * defined in a new dma_mode word!) */
265
266/* word 64: PIO transfer modes */
267#define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
268#define PIO_MODE_MAX 8 /* but all 8 bits are defined */
269
270/* word 75: queue_depth */
271#define DEPTH_BITS 0x001f /* bits used for queue depth */
272
273/* words 80-81: version numbers */
274/* NOVAL_0 or NOVAL_1 means device does not report version */
275
276/* word 81: minor version number */
277#define MINOR_MAX 0x1C
278#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
279static const char *minor_str[] = { /* word 81 value: */
280 "device does not report version", /* 0x0000 */
281 "ATA-1 X3T9.2 781D prior to revision 4", /* 0x0001 */
282 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
283 "ATA-1 X3T9.2 781D revision 4", /* 0x0003 */
284 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
285 "ATA-2 X3T10 948D prior to revision 2k", /* 0x0005 */
286 "ATA-3 X3T10 2008D revision 1", /* 0x0006 */
287 "ATA-2 X3T10 948D revision 2k", /* 0x0007 */
288 "ATA-3 X3T10 2008D revision 0", /* 0x0008 */
289 "ATA-2 X3T10 948D revision 3", /* 0x0009 */
290 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
291 "ATA-3 X3T10 2008D revision 6", /* 0x000b */
292 "ATA-3 X3T13 2008D revision 7 and 7a", /* 0x000c */
293 "ATA/ATAPI-4 X3T13 1153D revision 6", /* 0x000d */
294 "ATA/ATAPI-4 T13 1153D revision 13", /* 0x000e */
295 "ATA/ATAPI-4 X3T13 1153D revision 7", /* 0x000f */
296 "ATA/ATAPI-4 T13 1153D revision 18", /* 0x0010 */
297 "ATA/ATAPI-4 T13 1153D revision 15", /* 0x0011 */
298 "ATA/ATAPI-4 published, ANSI NCITS 317-1998", /* 0x0012 */
299 "ATA/ATAPI-5 T13 1321D revision 3",
300 "ATA/ATAPI-4 T13 1153D revision 14", /* 0x0014 */
301 "ATA/ATAPI-5 T13 1321D revision 1", /* 0x0015 */
302 "ATA/ATAPI-5 published, ANSI NCITS 340-2000", /* 0x0016 */
303 "ATA/ATAPI-4 T13 1153D revision 17", /* 0x0017 */
304 "ATA/ATAPI-6 T13 1410D revision 0", /* 0x0018 */
305 "ATA/ATAPI-6 T13 1410D revision 3a", /* 0x0019 */
306 "Reserved", /* 0x001a */
307 "ATA/ATAPI-6 T13 1410D revision 2", /* 0x001b */
308 "ATA/ATAPI-6 T13 1410D revision 1", /* 0x001c */
309 "reserved" /* 0x001d */
310 "reserved" /* 0x001e */
311 "reserved" /* 0x001f-0xfffe*/
312};
313#endif
Glenn L McGrath07085852003-10-09 07:28:22 +0000314static const char actual_ver[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000315 /* word 81 value: */
316 0, /* 0x0000 WARNING: */
317 1, /* 0x0001 WARNING: */
318 1, /* 0x0002 WARNING: */
319 1, /* 0x0003 WARNING: */
320 2, /* 0x0004 WARNING: This array */
321 2, /* 0x0005 WARNING: corresponds */
322 3, /* 0x0006 WARNING: *exactly* */
323 2, /* 0x0007 WARNING: to the ATA/ */
324 3, /* 0x0008 WARNING: ATAPI version */
325 2, /* 0x0009 WARNING: listed in */
326 3, /* 0x000a WARNING: the */
327 3, /* 0x000b WARNING: minor_str */
328 3, /* 0x000c WARNING: array */
329 4, /* 0x000d WARNING: above. */
330 4, /* 0x000e WARNING: */
331 4, /* 0x000f WARNING: if you change */
332 4, /* 0x0010 WARNING: that one, */
333 4, /* 0x0011 WARNING: change this one */
334 4, /* 0x0012 WARNING: too!!! */
335 5, /* 0x0013 WARNING: */
336 4, /* 0x0014 WARNING: */
337 5, /* 0x0015 WARNING: */
338 5, /* 0x0016 WARNING: */
339 4, /* 0x0017 WARNING: */
340 6, /* 0x0018 WARNING: */
341 6, /* 0x0019 WARNING: */
342 0, /* 0x001a WARNING: */
343 6, /* 0x001b WARNING: */
344 6, /* 0x001c WARNING: */
345 0 /* 0x001d-0xfffe */
346};
347
348/* words 82-84: cmds/feats supported */
349#define CMDS_W82 0x77ff /* word 82: defined command locations*/
350#define CMDS_W83 0x3fff /* word 83: defined command locations*/
351#define CMDS_W84 0x002f /* word 83: defined command locations*/
Glenn L McGrath07085852003-10-09 07:28:22 +0000352#define SUPPORT_48_BIT 0x0400
Eric Andersen3443bd72003-07-22 07:30:36 +0000353#define NUM_CMD_FEAT_STR 48
354
355#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
356static const char *cmd_feat_str[] = {
357 "", /* word 82 bit 15: obsolete */
358 "NOP cmd", /* word 82 bit 14 */
359 "READ BUFFER cmd", /* word 82 bit 13 */
360 "WRITE BUFFER cmd", /* word 82 bit 12 */
361 "", /* word 82 bit 11: obsolete */
362 "Host Protected Area feature set", /* word 82 bit 10 */
363 "DEVICE RESET cmd", /* word 82 bit 9 */
364 "SERVICE interrupt", /* word 82 bit 8 */
365 "Release interrupt", /* word 82 bit 7 */
366 "Look-ahead", /* word 82 bit 6 */
367 "Write cache", /* word 82 bit 5 */
368 "PACKET command feature set", /* word 82 bit 4 */
369 "Power Management feature set", /* word 82 bit 3 */
370 "Removable Media feature set", /* word 82 bit 2 */
371 "Security Mode feature set", /* word 82 bit 1 */
372 "SMART feature set", /* word 82 bit 0 */
373 /* --------------*/
374 "", /* word 83 bit 15: !valid bit */
375 "", /* word 83 bit 14: valid bit */
376 "FLUSH CACHE EXT command", /* word 83 bit 13 */
377 "Mandatory FLUSH CACHE command ", /* word 83 bit 12 */
378 "Device Configuration Overlay feature set ",
379 "48-bit Address feature set ", /* word 83 bit 10 */
380 "",
381 "SET MAX security extension", /* word 83 bit 8 */
382 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
383 "SET FEATURES subcommand required to spinup after power up",
384 "Power-Up In Standby feature set", /* word 83 bit 5 */
385 "Removable Media Status Notification feature set",
Eric Andersen06d4ec22004-03-19 10:53:52 +0000386 "Adv. Power Management feature set",/* word 83 bit 3 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000387 "CFA feature set", /* word 83 bit 2 */
388 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
389 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
390 /* --------------*/
391 "", /* word 84 bit 15: !valid bit */
392 "", /* word 84 bit 14: valid bit */
393 "", /* word 84 bit 13: reserved */
394 "", /* word 84 bit 12: reserved */
395 "", /* word 84 bit 11: reserved */
396 "", /* word 84 bit 10: reserved */
397 "", /* word 84 bit 9: reserved */
398 "", /* word 84 bit 8: reserved */
399 "", /* word 84 bit 7: reserved */
400 "", /* word 84 bit 6: reserved */
401 "General Purpose Logging feature set", /* word 84 bit 5 */
402 "", /* word 84 bit 4: reserved */
403 "Media Card Pass Through Command feature set ",
404 "Media serial number ", /* word 84 bit 2 */
405 "SMART self-test ", /* word 84 bit 1 */
406 "SMART error logging " /* word 84 bit 0 */
407};
408#endif
409
410
411/* words 85-87: cmds/feats enabled */
412/* use cmd_feat_str[] to display what commands and features have
Glenn L McGrath07085852003-10-09 07:28:22 +0000413 * been enabled with words 85-87
Eric Andersen3443bd72003-07-22 07:30:36 +0000414 */
415
416/* words 89, 90, SECU ERASE TIME */
417#define ERASE_BITS 0x00ff
418
419/* word 92: master password revision */
420/* NOVAL_0 or NOVAL_1 means no support for master password revision */
421
422/* word 93: hw reset result */
423#define CBLID 0x2000 /* CBLID status */
424#define RST0 0x0001 /* 1=reset to device #0 */
425#define DEV_DET 0x0006 /* how device num determined */
426#define JUMPER_VAL 0x0002 /* device num determined by jumper */
427#define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
428
429/* word 127: removable media status notification feature set support */
430#define RM_STAT_BITS 0x0003
431#define RM_STAT_SUP 0x0001
Glenn L McGrath07085852003-10-09 07:28:22 +0000432
Eric Andersen3443bd72003-07-22 07:30:36 +0000433/* word 128: security */
434#define SECU_ENABLED 0x0002
435#define SECU_LEVEL 0x0010
436#define NUM_SECU_STR 6
437#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
438static const char *secu_str[] = {
439 "supported", /* word 128, bit 0 */
440 "enabled", /* word 128, bit 1 */
441 "locked", /* word 128, bit 2 */
442 "frozen", /* word 128, bit 3 */
443 "expired: security count", /* word 128, bit 4 */
444 "supported: enhanced erase" /* word 128, bit 5 */
445};
446#endif
447
448/* word 160: CFA power mode */
449#define VALID_W160 0x8000 /* 1=word valid */
450#define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
451#define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
452#define MAX_AMPS 0x0fff /* value = max current in ma */
453
454/* word 255: integrity */
455#define SIG 0x00ff /* signature location */
456#define SIG_VAL 0x00A5 /* signature value */
457
Glenn L McGrath07085852003-10-09 07:28:22 +0000458#define VERSION "v5.4"
459
460#define TIMING_MB 64
461#define TIMING_BUF_MB 1
462#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
463#define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
464#define BUFCACHE_FACTOR 2
465
466#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
467
468/* Busybox messages and functions */
469
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000470static const char * const bb_msg_shared_mem ="could not %s sharedmem buf";
471static const char * const bb_msg_op_not_supp =" operation not supported on %s disks";
Glenn L McGrath07085852003-10-09 07:28:22 +0000472
473static void bb_ioctl(int fd, int request, void *argp, const char *string)
474{
475 if (ioctl (fd, request, argp) != 0)
Eric Andersen06d4ec22004-03-19 10:53:52 +0000476 bb_perror_msg(" %s", string);
Glenn L McGrath07085852003-10-09 07:28:22 +0000477}
478
479static void if_printf(unsigned long i, char *fmt, ... )
480{
481 va_list ap;
482 va_start(ap, fmt);
483 if (i)
484 vprintf(fmt, ap);
485 va_end(ap);
486}
487
488static void on_off (unsigned int value);
489
490static void if_printf_on_off(unsigned long get_arg,const char *fmt, unsigned long arg)
491{
492 if (get_arg)
493 {
494 printf(fmt, arg);
495 on_off(arg);
496 }
497}
498
499static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
500 const char * fmt)
501{
502 if (ioctl (fd, request, &argp) != 0)
Eric Andersen06d4ec22004-03-19 10:53:52 +0000503 bb_perror_msg(" %s", string);
Glenn L McGrath07085852003-10-09 07:28:22 +0000504 else
505 {
506 printf(fmt, (unsigned long) argp);
507 on_off((unsigned long) argp);
508 }
509}
Eric Andersen416c2422003-12-12 00:08:57 +0000510
511#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000512static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
513{
514 va_list ap;
515 va_start(ap, fmt2);
516 if (i)
517 vprintf(fmt1, ap);
518 else
519 vprintf(fmt2, ap);
520 va_end(ap);
521}
522
523static void print_ascii(uint16_t *p, uint8_t length);
524
525static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
526{
527 if(val[i])
528 {
529 printf("\t%-20s",string);
530 print_ascii(&val[i], n);
531 }
532}
533
Eric Andersen416c2422003-12-12 00:08:57 +0000534static void if_strcat(unsigned long test, char *modes, char *string)
535{
536 if (test)
537 strcat(modes,string);
538}
539#endif
540
Glenn L McGrath07085852003-10-09 07:28:22 +0000541static void sync_and_sleep(int i)
542{
543 sync();
544 sleep(i);
545}
546
Eric Andersenb2aa7762004-04-05 13:08:08 +0000547static uint16_t check_if_min_and_set_val(uint16_t a, uint16_t b)
Glenn L McGrath07085852003-10-09 07:28:22 +0000548{
549 if( a < b)
550 a = b;
Eric Andersenb2aa7762004-04-05 13:08:08 +0000551 return a;
Glenn L McGrath07085852003-10-09 07:28:22 +0000552}
553
Eric Andersenb2aa7762004-04-05 13:08:08 +0000554static uint16_t check_if_maj_and_set_val(uint16_t a, uint16_t b)
Glenn L McGrath07085852003-10-09 07:28:22 +0000555{
556 if( a > b)
557 a = b;
Eric Andersenb2aa7762004-04-05 13:08:08 +0000558 return a;
Glenn L McGrath07085852003-10-09 07:28:22 +0000559}
560
Eric Andersenb2aa7762004-04-05 13:08:08 +0000561static unsigned long int set_flag(char *p, char max)
Glenn L McGrath07085852003-10-09 07:28:22 +0000562{
563 if (*p >= '0' && *p <= max )
564 return 1;
565 return 0;
566}
567
Glenn L McGrath07085852003-10-09 07:28:22 +0000568/* end of busybox specific stuff */
569
Eric Andersen3443bd72003-07-22 07:30:36 +0000570#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000571static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
572{
Eric Andersen3443bd72003-07-22 07:30:36 +0000573 uint16_t ii;
574 uint8_t err_dma = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000575
576 for(ii = 0; ii <= MODE_MAX; ii++)
577 {
578 if(mode_sel & 0x0001)
579 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000580 printf("*%cdma%u ",cc,ii);
Glenn L McGrath07085852003-10-09 07:28:22 +0000581 if(*have_mode)
582 err_dma = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000583 *have_mode = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000584 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000585 else if(mode_sup & 0x0001)
586 printf("%cdma%u ",cc,ii);
587
588 mode_sup >>=1;
589 mode_sel >>=1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000590 }
591 return err_dma;
592}
Glenn L McGrath07085852003-10-09 07:28:22 +0000593
Eric Andersen3443bd72003-07-22 07:30:36 +0000594static void print_ascii(uint16_t *p, uint8_t length) {
595 uint8_t ii;
596 char cl;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000597
Eric Andersen3443bd72003-07-22 07:30:36 +0000598 /* find first non-space & print it */
Glenn L McGrath07085852003-10-09 07:28:22 +0000599 for(ii = 0; ii< length; ii++)
600 {
601 if(((char) 0x00ff&((*p)>>8)) != ' ')
602 break;
603 if((cl = (char) 0x00ff&(*p)) != ' ')
604 {
605 if_printf((cl != '\0'),"%c",cl);
606 p++;
607 ii++;
Eric Andersen3443bd72003-07-22 07:30:36 +0000608 break;
609 }
610 p++;
611 }
612 /* print the rest */
Glenn L McGrath07085852003-10-09 07:28:22 +0000613 for(; ii< length; ii++)
614 {
615 if(!(*p))
616 break; /* some older devices have NULLs */
Eric Andersen3443bd72003-07-22 07:30:36 +0000617 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
618 p++;
619 }
620 printf("\n");
621}
622
623/* identify() is the only extern function used across two source files. The
624 others, though, were declared in hdparm.c with global scope; since other
625 functions in that file have static (file) scope, I assume the difference is
626 intentional. */
627static void identify (uint16_t *id_supplied, const char *devname)
628{
629
Glenn L McGrath07085852003-10-09 07:28:22 +0000630 char *id_file = NULL;
Eric Andersen3443bd72003-07-22 07:30:36 +0000631 FILE *fl;
632 uint16_t val[256], ii, jj, kk;
633 uint16_t like_std = 1, std = 0, min_std = 0xffff;
634 uint16_t dev = NO_DEV, eqpt = NO_DEV;
635 uint8_t have_mode = 0, err_dma = 0;
636 uint8_t chksum = 0;
637 uint32_t ll, mm, nn, oo;
638 __u64 bbbig; /* (:) */
639
Glenn L McGrath07085852003-10-09 07:28:22 +0000640 if (id_supplied)
641 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000642#if __BYTE_ORDER == __BIG_ENDIAN
643 swab(id_supplied, val, sizeof(val));
644#else
645 memcpy(val, id_supplied, sizeof(val));
646#endif
Glenn L McGrath07085852003-10-09 07:28:22 +0000647 }
648 else
649 {
650 id_file = xcalloc(1, strlen(devname)+22);
651 sprintf(id_file, "/proc/ide/%s/identify", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +0000652 /* open the file, read in all the info and close it */
Glenn L McGrath07085852003-10-09 07:28:22 +0000653 if (id_file == NULL)
Eric Andersen3443bd72003-07-22 07:30:36 +0000654 fl = stdin;
Glenn L McGrath07085852003-10-09 07:28:22 +0000655 else
656 fl = bb_xfopen(id_file, "r");
657
Eric Andersen3443bd72003-07-22 07:30:36 +0000658 /* calculate checksum over all bytes */
Glenn L McGrath07085852003-10-09 07:28:22 +0000659 for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
660 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000661 unsigned int scratch;
Glenn L McGrath07085852003-10-09 07:28:22 +0000662 if(1 != fscanf(fl,"%04x",&scratch))
663 break;
Eric Andersen3443bd72003-07-22 07:30:36 +0000664 val[ii] = (uint16_t)scratch;
665 chksum += val[ii] + (val[ii] >> 8);
666 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000667 fclose(fl);
668 /*bb_fclose_nonstdin(fl);*/
Eric Andersen3443bd72003-07-22 07:30:36 +0000669 if(ii < (INTEGRITY+1))
670 bb_error_msg_and_die("Input file wrong format or length");
671 }
672 chksum &= 0xff;
673
674 /* check if we recognise the device type */
675 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000676 if(!(val[GEN_CONFIG] & NOT_ATA))
677 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000678 dev = ATA_DEV;
679 printf("ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000680 }
681 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
682 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000683 dev = ATA_DEV;
684 like_std = 4;
685 printf("CompactFlash ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000686 }
687 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
688 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000689 dev = ATAPI_DEV;
690 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
691 printf("ATAPI %s, with ", pkt_str[eqpt]);
692 like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000693 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000694 else
695 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
696 bb_error_msg_and_die("Unknown device type");
Eric Andersen3443bd72003-07-22 07:30:36 +0000697
Glenn L McGrath07085852003-10-09 07:28:22 +0000698 if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
699 printf("removable media\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000700
701 /* Info from the specific configuration word says whether or not the
702 * ID command completed correctly. It is only defined, however in
Glenn L McGrath07085852003-10-09 07:28:22 +0000703 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
Eric Andersen3443bd72003-07-22 07:30:36 +0000704 * standards. Since the values allowed for this word are extremely
705 * specific, it should be safe to check it now, even though we don't
706 * know yet what standard this device is using.
707 */
708 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000709 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
710 {
711 like_std = 5;
712 if_printf(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)),
713 "powers-up in standby; SET FEATURES subcmd spins-up.\n");
714 if_printf((((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)),
715 "\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000716 }
717
718 /* output the model and serial numbers and the fw revision */
Glenn L McGrath07085852003-10-09 07:28:22 +0000719 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
720 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
721 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
722 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
723 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
Eric Andersen3443bd72003-07-22 07:30:36 +0000724
725 /* major & minor standards version number (Note: these words were not
726 * defined until ATA-3 & the CDROM std uses different words.) */
727 printf("Standards:");
Glenn L McGrath07085852003-10-09 07:28:22 +0000728 if(eqpt != CDROM)
729 {
730 if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
731 {
Eric Andersenb2aa7762004-04-05 13:08:08 +0000732 like_std=check_if_min_and_set_val(like_std, 3);
Eric Andersen3443bd72003-07-22 07:30:36 +0000733 std = actual_ver[val[MINOR]];
Glenn L McGrath07085852003-10-09 07:28:22 +0000734 if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);
735
Eric Andersen3443bd72003-07-22 07:30:36 +0000736 }
737 /* looks like when they up-issue the std, they obsolete one;
Glenn L McGrath07085852003-10-09 07:28:22 +0000738 * thus, only the newest 4 issues need be supported. (That's
Eric Andersen3443bd72003-07-22 07:30:36 +0000739 * what "kk" and "min_std" are all about.) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000740 if(val[MAJOR] && (val[MAJOR] !=NOVAL_1))
741 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000742 printf("\n\tSupported: ");
743 jj = val[MAJOR] << 1;
744 kk = like_std >4 ? like_std-4: 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000745 for(ii = 14; (ii >0)&&(ii>kk); ii--)
746 {
747 if(jj & 0x8000)
748 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000749 printf("%u ", ii);
Glenn L McGrath07085852003-10-09 07:28:22 +0000750 if(like_std < ii)
751 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000752 like_std = ii;
753 kk = like_std >4 ? like_std-4: 0;
754 }
Eric Andersenb2aa7762004-04-05 13:08:08 +0000755 min_std=check_if_maj_and_set_val(min_std, ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000756 }
757 jj <<= 1;
758 }
Eric Andersenb2aa7762004-04-05 13:08:08 +0000759 like_std=check_if_min_and_set_val(like_std, 3);
Eric Andersen3443bd72003-07-22 07:30:36 +0000760 }
761 /* Figure out what standard the device is using if it hasn't told
762 * us. If we know the std, check if the device is using any of
763 * the words from the next level up. It happens.
764 */
Eric Andersenb2aa7762004-04-05 13:08:08 +0000765 like_std=check_if_min_and_set_val(like_std, std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000766
Eric Andersen3443bd72003-07-22 07:30:36 +0000767 if(((std == 5) || (!std && (like_std < 6))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000768 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
769 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
770 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
771 ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
772 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000773 like_std = 6;
Glenn L McGrath07085852003-10-09 07:28:22 +0000774 }
775 else if(((std == 4) || (!std && (like_std < 5))) &&
776 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
777 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
778 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
779 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
780 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000781 like_std = 5;
Glenn L McGrath07085852003-10-09 07:28:22 +0000782 }
783 else if(((std == 3) || (!std && (like_std < 4))) &&
784 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
785 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
786 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
787 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
788 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
789 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
790 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000791 like_std = 4;
Glenn L McGrath07085852003-10-09 07:28:22 +0000792 }
793 else if(((std == 2) || (!std && (like_std < 3))) &&
794 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
795 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000796 like_std = 3;
Glenn L McGrath07085852003-10-09 07:28:22 +0000797 }
798 else if(((std == 1) || (!std && (like_std < 2))) &&
799 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
800 (val[WHATS_VALID] & OK_W64_70)) )
801 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000802 like_std = 2;
803 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000804 if(!std)
Eric Andersen3443bd72003-07-22 07:30:36 +0000805 printf("\n\tLikely used: %u\n",like_std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000806 else if(like_std > std)
807 printf("& some of %u\n",like_std);
808 else
809 printf("\n");
810 }
811 else
812 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000813 /* TBD: do CDROM stuff more thoroughly. For now... */
814 kk = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000815 if(val[CDR_MINOR] == 9)
816 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000817 kk = 1;
818 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
819 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000820 if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
821 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000822 kk = 1;
823 printf("\n\tSupported: CD-ROM ATAPI");
824 jj = val[CDR_MAJOR] >> 1;
Glenn L McGrath07085852003-10-09 07:28:22 +0000825 for(ii = 1; ii <15; ii++)
826 {
827 if_printf((jj & 0x0001),"-%u ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000828 jj >>= 1;
829 }
830 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000831 if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000832 /* the cdrom stuff is more like ATA-2 than anything else, so: */
833 like_std = 2;
834 }
835
Glenn L McGrath07085852003-10-09 07:28:22 +0000836 if(min_std == 0xffff)
837 min_std = like_std > 4 ? like_std - 3 : 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000838
839 printf("Configuration:\n");
840 /* more info from the general configuration word */
Glenn L McGrath07085852003-10-09 07:28:22 +0000841 if((eqpt != CDROM) && (like_std == 1))
842 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000843 jj = val[GEN_CONFIG] >> 1;
Glenn L McGrath07085852003-10-09 07:28:22 +0000844 for(ii = 1; ii < 15; ii++)
845 {
846 if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000847 jj >>=1;
848 }
849 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000850 if(dev == ATAPI_DEV)
851 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000852 printf("\tDRQ response: "); /* Data Request (DRQ) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000853 switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
854 {
855 case DRQ_3MS_VAL :
856 printf("3ms.\n");
857 break;
858 case DRQ_INTR_VAL :
859 printf("<=10ms with INTRQ\n");
860 break;
861 case DRQ_50US_VAL :
862 printf("50us.\n");
863 break;
864 default :
865 printf("unknown.\n");
866 break;
Eric Andersen3443bd72003-07-22 07:30:36 +0000867 }
868 printf("\tPacket size: ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000869 switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
870 {
871 case PKT_SIZE_12_VAL :
872 printf("12 bytes\n");
873 break;
874 case PKT_SIZE_16_VAL :
875 printf("16 bytes\n");
876 break;
877 default :
878 printf("Unknown\n");
879 break;
Eric Andersen3443bd72003-07-22 07:30:36 +0000880 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000881 }
882 else
883 {
884 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000885 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
886 mm = 0; bbbig = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000887 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
Eric Andersen3443bd72003-07-22 07:30:36 +0000888 printf("\tCHS addressing not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000889 else
890 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000891 jj = val[WHATS_VALID] & OK_W54_58;
Glenn L McGrath07085852003-10-09 07:28:22 +0000892 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",
893 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
894
895 if_printf(((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])),
896 "\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
897
898 if(jj)
899 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000900 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
Glenn L McGrath07085852003-10-09 07:28:22 +0000901 if(like_std < 3)
902 {
903 /* check Endian of capacity bytes */
Eric Andersen3443bd72003-07-22 07:30:36 +0000904 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
905 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
906 if(abs(mm - nn) > abs(oo - nn))
907 mm = oo;
908 }
909 printf("\tCHS current addressable sectors:%11u\n",mm);
Glenn L McGrath07085852003-10-09 07:28:22 +0000910 }
Eric Andersen3443bd72003-07-22 07:30:36 +0000911 }
912 /* LBA addressing */
913 printf("\tLBA user addressable sectors:%11u\n",ll);
914 if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000915 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
916 {
917 bbbig = (__u64)val[LBA_64_MSB] << 48 |
918 (__u64)val[LBA_48_MSB] << 32 |
919 (__u64)val[LBA_MID] << 16 |
920 val[LBA_LSB] ;
Eric Andersen3443bd72003-07-22 07:30:36 +0000921 printf("\tLBA48 user addressable sectors:%11llu\n",bbbig);
922 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000923
924 if (!bbbig)
925 bbbig = (__u64)(ll>mm ? ll : mm); /* # 512 byte blocks */
Eric Andersen3443bd72003-07-22 07:30:36 +0000926 printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
927 bbbig = (bbbig<<9)/1000000;
928 printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);
Glenn L McGrath07085852003-10-09 07:28:22 +0000929
930 if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
Eric Andersen3443bd72003-07-22 07:30:36 +0000931
932 }
933
934 /* hw support of commands (capabilities) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000935 printf("Capabilities:\n\t");
936
937 if(dev == ATAPI_DEV)
938 {
939 if(eqpt != CDROM)
940 if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");
941
942 if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
Eric Andersen3443bd72003-07-22 07:30:36 +0000943 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000944 if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
945
946 if(like_std != 1)
947 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000948 printf("IORDY");
Glenn L McGrath07085852003-10-09 07:28:22 +0000949 if_printf((!(val[CAPAB_0] & IORDY_SUP)),"(may be)");
950 if_else_printf((val[CAPAB_0] & IORDY_OFF),"(can","(cannot");
951 printf(" be disabled)\n");
952 }
953 else
954 printf("no IORDY\n");
955
956 if((like_std == 1) && val[BUF_TYPE])
957 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000958 kk = val[BUF_TYPE];
959 printf("\tBuffer type: %04x: ",kk);
Glenn L McGrath07085852003-10-09 07:28:22 +0000960 if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
961 if_printf((kk > 2)," with read caching ability");
Eric Andersen3443bd72003-07-22 07:30:36 +0000962 printf("\n");
963 }
964 jj = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000965 if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
966 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000967 printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
968 jj = 1;
969 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000970 if((min_std < 4) && (val[RW_LONG]))
971 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000972 printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
973 jj = 1;
974 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000975 if((eqpt != CDROM) && (like_std > 3))
976 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000977 printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
978 jj = 1;
979 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000980 if_printf(jj,"\n");
981
982 if(dev == ATA_DEV)
983 {
984 if(like_std == 1)
985 printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
986 else
987 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000988 printf("\tStandby timer values: spec'd by ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000989 if_else_printf((val[CAPAB_0] & STD_STBY),"Standard","Vendor");
990 if((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
991 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
992 else
993 printf("\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000994 }
995 printf("\tR/W multiple sector transfer: ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000996 if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
Eric Andersen3443bd72003-07-22 07:30:36 +0000997 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000998 else
999 {
1000 printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
1001 if_else_printf((val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID),
1002 "%u\n","?\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
Eric Andersen3443bd72003-07-22 07:30:36 +00001003 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001004 if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
1005 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001006 /* We print out elsewhere whether the APM feature is enabled or
1007 not. If it's not enabled, let's not repeat the info; just print
1008 nothing here. */
Glenn L McGrath07085852003-10-09 07:28:22 +00001009 printf("\tAdvancedPM level: ");
1010 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
1011 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001012 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
Eric Andersen3443bd72003-07-22 07:30:36 +00001013 printf("%u (0x%x)\n", apm_level, apm_level);
Glenn L McGrath07085852003-10-09 07:28:22 +00001014 }
1015 else
Eric Andersen3443bd72003-07-22 07:30:36 +00001016 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001017 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001018 if(like_std > 5)
1019 {
1020 if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
1021 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
Eric Andersen3443bd72003-07-22 07:30:36 +00001022 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001023 }
1024 else
1025 {
1026 /* ATAPI */
1027 if(eqpt != CDROM)
1028 if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");
1029
1030 if(val[PKT_REL] || val[SVC_NBSY])
1031 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001032 printf("\tOverlap support:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001033 if_printf(val[PKT_REL]," %uus to release bus.",val[PKT_REL]);
1034 if_printf(val[SVC_NBSY]," %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001035 printf("\n");
1036 }
1037 }
1038
1039 /* DMA stuff. Check that only one DMA mode is selected. */
1040 printf("\tDMA: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001041 if(!(val[CAPAB_0] & DMA_SUP))
Eric Andersen3443bd72003-07-22 07:30:36 +00001042 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001043 else
1044 {
1045 if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
1046 " sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
1047 if(val[SINGLE_DMA])
1048 {
1049 jj = val[SINGLE_DMA];
1050 kk = val[SINGLE_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +00001051 err_dma += mode_loop(jj,kk,'s',&have_mode);
1052 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001053 if(val[MULTI_DMA])
1054 {
1055 jj = val[MULTI_DMA];
1056 kk = val[MULTI_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +00001057 err_dma += mode_loop(jj,kk,'m',&have_mode);
1058 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001059 if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
1060 {
1061 jj = val[ULTRA_DMA];
1062 kk = val[ULTRA_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +00001063 err_dma += mode_loop(jj,kk,'u',&have_mode);
1064 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001065 if_printf((err_dma || !have_mode),"(?)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001066 printf("\n");
1067
Glenn L McGrath07085852003-10-09 07:28:22 +00001068 if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
1069 "\t Interleaved DMA support\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001070
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001071 if((val[WHATS_VALID] & OK_W64_70) &&
Glenn L McGrath07085852003-10-09 07:28:22 +00001072 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
1073 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001074 printf("\t Cycle time:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001075 if_printf(val[DMA_TIME_MIN]," min=%uns",val[DMA_TIME_MIN]);
1076 if_printf(val[DMA_TIME_NORM]," recommended=%uns",val[DMA_TIME_NORM]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001077 printf("\n");
1078 }
1079 }
1080
1081 /* Programmed IO stuff */
1082 printf("\tPIO: ");
1083 /* If a drive supports mode n (e.g. 3), it also supports all modes less
1084 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
Glenn L McGrath07085852003-10-09 07:28:22 +00001085 if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
1086 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001087 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
Glenn L McGrath07085852003-10-09 07:28:22 +00001088 for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
1089 {
1090 if_printf((jj & 0x0001),"pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +00001091 jj >>=1;
1092 }
1093 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001094 }
1095 else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
1096 {
1097 for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
Eric Andersen3443bd72003-07-22 07:30:36 +00001098 printf("pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +00001099 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001100 }
1101 else
1102 printf("unknown\n");
1103
1104 if(val[WHATS_VALID] & OK_W64_70)
1105 {
1106 if(val[PIO_NO_FLOW] || val[PIO_FLOW])
1107 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001108 printf("\t Cycle time:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001109 if_printf(val[PIO_NO_FLOW]," no flow control=%uns", val[PIO_NO_FLOW]);
1110 if_printf(val[PIO_FLOW]," IORDY flow control=%uns", val[PIO_FLOW]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001111 printf("\n");
1112 }
1113 }
1114
Glenn L McGrath07085852003-10-09 07:28:22 +00001115 if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1116 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001117 printf("Commands/features:\n\tEnabled\tSupported:\n");
1118 jj = val[CMDS_SUPP_0];
1119 kk = val[CMDS_EN_0];
Glenn L McGrath07085852003-10-09 07:28:22 +00001120 for(ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
1121 {
1122 if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
1123 {
1124 if_else_printf((kk & 0x8000),"\t *","\t");
Eric Andersen3443bd72003-07-22 07:30:36 +00001125 printf("\t%s\n",cmd_feat_str[ii]);
1126 }
1127 jj <<=1; kk<<=1;
Glenn L McGrath07085852003-10-09 07:28:22 +00001128 if(ii%16 == 15)
1129 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001130 jj = val[CMDS_SUPP_0+1+(ii/16)];
1131 kk = val[CMDS_EN_0+1+(ii/16)];
1132 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001133 if(ii == 31)
1134 {
1135 if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1136 ii +=16;
Eric Andersen3443bd72003-07-22 07:30:36 +00001137 }
1138 }
1139 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001140 if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
1141 "\tRemovable Media Status Notification feature set supported\n");
1142
1143
Eric Andersen3443bd72003-07-22 07:30:36 +00001144 /* security */
Glenn L McGrath07085852003-10-09 07:28:22 +00001145 if((eqpt != CDROM) && (like_std > 3) &&
1146 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1147 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001148 printf("Security: \n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001149 if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
1150 "\tMaster password revision code = %u\n",val[PSWD_CODE]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001151 jj = val[SECU_STATUS];
Glenn L McGrath07085852003-10-09 07:28:22 +00001152 if(jj)
1153 {
1154 for(ii = 0; ii < NUM_SECU_STR; ii++)
1155 {
1156 if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001157 jj >>=1;
1158 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001159 if(val[SECU_STATUS] & SECU_ENABLED)
1160 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001161 printf("\tSecurity level ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001162 if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001163 }
1164 }
1165 jj = val[ERASE_TIME] & ERASE_BITS;
1166 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
Glenn L McGrath07085852003-10-09 07:28:22 +00001167 if(jj || kk)
1168 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001169 printf("\t");
Glenn L McGrath07085852003-10-09 07:28:22 +00001170 if_printf(jj,"%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
1171 if_printf(kk,"%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
Eric Andersen3443bd72003-07-22 07:30:36 +00001172 printf("\n");
1173 }
1174 }
1175
1176 /* reset result */
Glenn L McGrath07085852003-10-09 07:28:22 +00001177 if((val[HWRST_RSLT] & VALID) == VALID_VAL)
1178 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001179 printf("HW reset results:\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001180 if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
1181
1182 if(val[HWRST_RSLT] & RST0)
1183 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001184 printf("\tDevice num = 0");
1185 jj = val[HWRST_RSLT];
Glenn L McGrath07085852003-10-09 07:28:22 +00001186 }
1187 else
1188 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001189 printf("\tDevice num = 1");
1190 jj = val[HWRST_RSLT] >> 8;
1191 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001192
1193 if((jj & DEV_DET) == JUMPER_VAL)
Eric Andersen3443bd72003-07-22 07:30:36 +00001194 printf(" determined by the jumper");
1195 else if((jj & DEV_DET) == CSEL_VAL)
1196 printf(" determined by CSEL");
1197 printf("\n");
1198 }
1199
1200 /* more stuff from std 5 */
Glenn L McGrath07085852003-10-09 07:28:22 +00001201 if((like_std > 4) && (eqpt != CDROM))
1202 {
1203 if(val[CFA_PWR_MODE] & VALID_W160)
1204 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001205 printf("CFA power mode 1:\n\t");
Glenn L McGrath07085852003-10-09 07:28:22 +00001206 if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");
1207
1208 if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
Eric Andersen3443bd72003-07-22 07:30:36 +00001209 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001210
1211 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 +00001212 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001213 if((val[INTEGRITY] & SIG) == SIG_VAL)
1214 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001215 printf("Checksum: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001216 if_printf(chksum,"in");
Eric Andersen3443bd72003-07-22 07:30:36 +00001217 printf("correct\n");
1218 }
1219 }
1220
1221 exit(0);
1222}
1223#endif
1224
Eric Andersen3443bd72003-07-22 07:30:36 +00001225static int verbose = 0, get_identity = 0, get_geom = 0, noisy = 1, quiet = 0;
1226static int flagcount = 0, do_flush = 0, is_scsi_hd = 0, is_xt_hd = 0;
1227static int do_ctimings, do_timings = 0;
1228
Eric Andersenf828da02004-07-20 22:53:59 +00001229static unsigned long set_readahead= 0, get_readahead= 0, Xreadahead= 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001230static unsigned long set_readonly = 0, get_readonly = 0, readonly = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001231static unsigned long set_unmask = 0, get_unmask = 0, unmask = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001232static unsigned long set_mult = 0, get_mult = 0, mult = 0;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001233#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Eric Andersen3443bd72003-07-22 07:30:36 +00001234static unsigned long set_dma = 0, get_dma = 0, dma = 0;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001235#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001236static unsigned long set_dma_q = 0, get_dma_q = 0, dma_q = 0;
1237static unsigned long set_nowerr = 0, get_nowerr = 0, nowerr = 0;
1238static unsigned long set_keep = 0, get_keep = 0, keep = 0;
1239static unsigned long set_io32bit = 0, get_io32bit = 0, io32bit = 0;
1240static unsigned long set_piomode = 0, noisy_piomode= 0;
1241static int piomode = 0;
1242#ifdef HDIO_DRIVE_CMD
1243static unsigned long set_dkeep = 0, get_dkeep = 0, dkeep = 0;
1244static unsigned long set_standby = 0, get_standby = 0, standby_requested= 0;
1245static unsigned long set_xfermode = 0, get_xfermode = 0;
1246static int xfermode_requested= 0;
1247static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
1248static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
1249static unsigned long set_defects = 0, get_defects = 0, defects = 0;
1250static unsigned long set_wcache = 0, get_wcache = 0, wcache = 0;
1251static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
1252static unsigned long set_seagate = 0, get_seagate = 0;
1253static unsigned long set_standbynow = 0, get_standbynow = 0;
1254static unsigned long set_sleepnow = 0, get_sleepnow = 0;
1255static unsigned long get_powermode = 0;
1256static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
1257#endif
1258#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1259static int get_IDentity = 0;
1260#endif
1261#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1262static int unregister_hwif = 0;
1263static int hwif = 0;
1264#endif
1265#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1266static int scan_hwif = 0;
1267static int hwif_data = 0;
1268static int hwif_ctrl = 0;
1269static int hwif_irq = 0;
1270#endif
1271#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1272static int set_busstate = 0, get_busstate = 0, busstate = 0;
1273#endif
1274static int reread_partn = 0;
1275
1276#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1277static int perform_reset = 0;
1278#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1279#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1280static int perform_tristate = 0, tristate = 0;
1281#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1282
1283// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1284// then the HDIO_GET_IDENTITY only returned 142 bytes.
1285// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1286// and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1287// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1288// (which they should, but they should just return -EINVAL).
1289//
1290// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1291// On a really old system, it will not, and we will be confused.
1292// Too bad, really.
1293
1294#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1295static const char *cfg_str[] =
1296{ "", " HardSect", " SoftSect", " NotMFM",
1297 " HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
1298 " DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
1299 " dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
1300};
1301
1302static const char *BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1303
Eric Andersen3443bd72003-07-22 07:30:36 +00001304static void dump_identity (const struct hd_driveid *id)
1305{
1306 int i;
1307 char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
1308 const unsigned short int *id_regs= (const void*) id;
1309 unsigned long capacity;
1310
Glenn L McGrath07085852003-10-09 07:28:22 +00001311 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1312 id->model, id->fw_rev, id->serial_no);
1313 for (i=0; i<=15; i++)
1314 if_printf((id->config & (1<<i)),"%s", cfg_str[i]);
1315
1316 printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
1317 id->cyls, id->heads, id->sectors, id->track_bytes,
1318 id->sector_bytes, id->ecc_bytes);
1319
1320 if (id->buf_type > 3)
1321 printf("%s%u", " BuffType=", id->buf_type);
1322 else
1323 printf("%s%s", " BuffType=", BuffType[id->buf_type]);
1324
Eric Andersen3443bd72003-07-22 07:30:36 +00001325 printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
Glenn L McGrath07085852003-10-09 07:28:22 +00001326 if (id->max_multsect)
1327 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001328 printf(", MultSect=");
1329 if (!(id->multsect_valid&1))
1330 printf("?%u?", id->multsect);
1331 else if (id->multsect)
1332 printf("%u", id->multsect);
1333 else
1334 printf("off");
1335 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001336 printf("\n");
1337 if (id->tPIO <= 5)
1338 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001339 strcat(pmodes, "pio0 ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001340 if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
1341 if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
1342
Eric Andersen3443bd72003-07-22 07:30:36 +00001343 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001344 if_printf((!(id->field_valid&1))," (maybe):");
Eric Andersen3443bd72003-07-22 07:30:36 +00001345#if __BYTE_ORDER == __BIG_ENDIAN
1346 capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1347#else
1348 capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1349#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001350 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1351 id->cur_sectors, capacity ,
1352 ((id->capability&2)==0)?"no":"yes");
Eric Andersen3443bd72003-07-22 07:30:36 +00001353
Glenn L McGrath07085852003-10-09 07:28:22 +00001354 if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);
1355
1356 if (id->capability&1)
1357 {
1358 if (id->dma_1word | id->dma_mword)
1359 {
1360 if_strcat((id->dma_1word & 0x100), dmodes, "*");
1361 if_strcat((id->dma_1word & 1), dmodes, "sdma0 ");
1362 if_strcat((id->dma_1word & 0x200), dmodes, "*");
1363 if_strcat((id->dma_1word & 2), dmodes, "sdma1 ");
1364 if_strcat((id->dma_1word & 0x400), dmodes, "*");
1365 if_strcat((id->dma_1word & 4), dmodes, "sdma2 ");
1366 if_strcat((id->dma_1word & 0xf800), dmodes, "*");
1367 if_strcat((id->dma_1word & 0xf8), dmodes, "sdma? ");
1368 if_strcat((id->dma_mword & 0x100), dmodes, "*");
1369 if_strcat((id->dma_mword & 1), dmodes, "mdma0 ");
1370 if_strcat((id->dma_mword & 0x200), dmodes, "*");
1371 if_strcat((id->dma_mword & 2), dmodes, "mdma1 ");
1372 if_strcat((id->dma_mword & 0x400), dmodes, "*");
1373 if_strcat((id->dma_mword & 4), dmodes, "mdma2 ");
1374 if_strcat((id->dma_mword & 0xf800), dmodes, "*");
1375 if_strcat((id->dma_mword & 0xf8), dmodes, "mdma? ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001376 }
1377 }
1378 printf("\n IORDY=");
1379 if (id->capability&8)
1380 printf((id->capability&4) ? "on/off" : "yes");
1381 else
1382 printf("no");
Eric Andersen3443bd72003-07-22 07:30:36 +00001383
Glenn L McGrath07085852003-10-09 07:28:22 +00001384 if ((id->capability&8) || (id->field_valid&2))
1385 {
1386 if (id->field_valid&2)
1387 {
1388 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1389 if_strcat((id->eide_pio_modes & 1), pmodes, "pio3 ");
1390 if_strcat((id->eide_pio_modes & 2), pmodes, "pio4 ");
1391 if_strcat((id->eide_pio_modes &~3), pmodes, "pio? ");
1392 }
1393 if (id->field_valid&4)
1394 {
1395 if_strcat((id->dma_ultra & 0x100),umodes,"*");
1396 if_strcat((id->dma_ultra & 0x001),umodes,"udma0 ");
1397 if_strcat((id->dma_ultra & 0x200),umodes,"*");
1398 if_strcat((id->dma_ultra & 0x002),umodes,"udma1 ");
1399 if_strcat((id->dma_ultra & 0x400),umodes,"*");
1400 if_strcat((id->dma_ultra & 0x004),umodes,"udma2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001401#ifdef __NEW_HD_DRIVE_ID
Glenn L McGrath07085852003-10-09 07:28:22 +00001402 if (id->hw_config & 0x2000)
1403 {
1404#else /* !__NEW_HD_DRIVE_ID */
1405 if (id->word93 & 0x2000)
1406 {
1407#endif /* __NEW_HD_DRIVE_ID */
1408 if_strcat((id->dma_ultra & 0x0800),umodes,"*");
1409 if_strcat((id->dma_ultra & 0x0008),umodes,"udma3 ");
1410 if_strcat((id->dma_ultra & 0x1000),umodes,"*");
1411 if_strcat((id->dma_ultra & 0x0010),umodes,"udma4 ");
1412 if_strcat((id->dma_ultra & 0x2000),umodes,"*");
1413 if_strcat((id->dma_ultra & 0x0020),umodes,"udma5 ");
1414 if_strcat((id->dma_ultra & 0x4000),umodes,"*");
1415 if_strcat((id->dma_ultra & 0x0040),umodes,"udma6 ");
1416 if_strcat((id->dma_ultra & 0x8000),umodes,"*");
1417 if_strcat((id->dma_ultra & 0x0080),umodes,"udma7 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001418 }
1419 }
1420 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001421 if_printf(((id->capability&1) && (id->field_valid&2)),
1422 ", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1423 printf("\n PIO modes: %s", pmodes);
1424 if_printf((*dmodes),"\n DMA modes: %s", dmodes);
1425 if_printf((*umodes),"\n UDMA modes: %s", umodes);
1426
1427 printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1428 if (id_regs[83] & 8)
1429 {
1430 if (!(id_regs[86]&8))
1431 printf(": disabled (255)");
1432 else if ((id_regs[91]&0xFF00)!=0x4000)
1433 printf(": unknown setting");
1434 else
1435 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1436 }
1437 if_printf( (id_regs[82]&0x20)," WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1438#ifdef __NEW_HD_DRIVE_ID
1439 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1440 {
1441 printf("\n Drive conforms to: ");
1442 if_else_printf((id->minor_rev_num <= 31),"%s: ","unknown: ", minor_str[id->minor_rev_num]);
1443 if (id->major_rev_num < 31)
1444 {
1445 for (i=0; i <= 15; i++)
1446 if_printf((id->major_rev_num & (1<<i))," %u", i);
1447 }
1448 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001449#endif /* __NEW_HD_DRIVE_ID */
Glenn L McGrath07085852003-10-09 07:28:22 +00001450 printf("\n\n * signifies the current active mode\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001451}
1452#endif
1453
1454static void flush_buffer_cache (int fd)
1455{
1456 fsync (fd); /* flush buffers */
Glenn L McGrath07085852003-10-09 07:28:22 +00001457 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
Eric Andersen3443bd72003-07-22 07:30:36 +00001458#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001459 sleep(1);
1460 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
Eric Andersen06d4ec22004-03-19 10:53:52 +00001461 bb_perror_msg("HDIO_DRIVE_CMD");
Eric Andersen3443bd72003-07-22 07:30:36 +00001462#endif
1463}
1464
1465static int seek_to_zero (int fd)
1466{
Glenn L McGrath07085852003-10-09 07:28:22 +00001467 if (lseek(fd, (off_t) 0, SEEK_SET))
Eric Andersen3443bd72003-07-22 07:30:36 +00001468 return 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001469 return 0;
1470}
1471
1472static int read_big_block (int fd, char *buf)
1473{
Glenn L McGrath07085852003-10-09 07:28:22 +00001474
1475 const char *string;
Eric Andersen3443bd72003-07-22 07:30:36 +00001476 int i, rc;
Glenn L McGrath07085852003-10-09 07:28:22 +00001477 if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
1478 {
1479 switch(rc)
1480 {
1481 case -1:
1482 string = "read()";
1483 break;
1484 case 0:
1485 string = "read() hit EOF - device too small";
1486 break;
1487 default:
1488 string = "read(%u) returned %u bytes";
Eric Andersen3443bd72003-07-22 07:30:36 +00001489 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001490 bb_error_msg(string, TIMING_BUF_BYTES, rc);
Eric Andersen3443bd72003-07-22 07:30:36 +00001491 return 1;
1492 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001493
Eric Andersen3443bd72003-07-22 07:30:36 +00001494 /* access all sectors of buf to ensure the read fully completed */
1495 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1496 buf[i] &= 1;
1497 return 0;
1498}
1499
Eric Andersen50af12d2003-08-06 08:47:59 +00001500static double correction = 0.0;
1501
Eric Andersen06d4ec22004-03-19 10:53:52 +00001502static void do_time (int flag, int fd)
Glenn L McGrath07085852003-10-09 07:28:22 +00001503/*
1504 flag = 0 time_cache
1505 flag = 1 time_device
1506*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001507{
Eric Andersen50af12d2003-08-06 08:47:59 +00001508 int i;
Eric Andersen3443bd72003-07-22 07:30:36 +00001509 char *buf;
1510 double elapsed;
1511 struct itimerval e1, e2;
1512 int shmid;
Eric Andersen50af12d2003-08-06 08:47:59 +00001513 int timing_MB = TIMING_MB;
Eric Andersen3443bd72003-07-22 07:30:36 +00001514
Glenn L McGrath07085852003-10-09 07:28:22 +00001515 if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
1516 {
1517 bb_error_msg (bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001518 return;
1519 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001520 if (shmctl(shmid, SHM_LOCK, NULL) == -1)
1521 {
1522 bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001523 (void) shmctl(shmid, IPC_RMID, NULL);
1524 return;
1525 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001526 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
1527 {
1528 bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001529 (void) shmctl(shmid, IPC_RMID, NULL);
1530 return;
1531 }
1532 if (shmctl(shmid, IPC_RMID, NULL) == -1)
Glenn L McGrath07085852003-10-09 07:28:22 +00001533 bb_error_msg ("shmctl(,IPC_RMID,)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001534
1535 /* Clear out the device request queues & give them time to complete */
Glenn L McGrath07085852003-10-09 07:28:22 +00001536 sync_and_sleep(3);
Eric Andersen3443bd72003-07-22 07:30:36 +00001537
Glenn L McGrath07085852003-10-09 07:28:22 +00001538 if(flag == 0) /* Time cache */
1539 {
1540 /* Calculate a correction factor for the basic
1541 * overhead of doing a read() from the buffer cache.
1542 * To do this, we read the data once to "cache it" and
1543 * to force full preallocation of our timing buffer,
1544 * and then we re-read it 10 times while timing it.
1545 *
1546 * getitimer() is used rather than gettimeofday() because
1547 * it is much more consistent (on my machine, at least).
1548 */
1549 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1550 if (seek_to_zero (fd))
1551 return;
1552 if (read_big_block (fd, buf))
1553 return;
1554 printf(" Timing buffer-cache reads: ");
1555 fflush(stdout);
Eric Andersen3443bd72003-07-22 07:30:36 +00001556
Glenn L McGrath07085852003-10-09 07:28:22 +00001557 /* Clear out the device request queues & give them time to complete */
1558 sync_and_sleep(1);
Eric Andersen3443bd72003-07-22 07:30:36 +00001559
Glenn L McGrath07085852003-10-09 07:28:22 +00001560 /* Time re-reading from the buffer-cache */
1561 getitimer(ITIMER_REAL, &e1);
1562 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1563 {
1564 if (seek_to_zero (fd))
1565 goto quit;
1566 if (read_big_block (fd, buf))
1567 goto quit;
1568 }
1569 getitimer(ITIMER_REAL, &e2);
1570 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 +00001571
Glenn L McGrath07085852003-10-09 07:28:22 +00001572 /* Now remove the lseek() from the correction factor */
1573 getitimer(ITIMER_REAL, &e1);
1574 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1575 {
1576 if (seek_to_zero (fd))
1577 goto quit;
1578 }
1579 getitimer(ITIMER_REAL, &e2);
1580 correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
1581 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
Eric Andersen50af12d2003-08-06 08:47:59 +00001582
Glenn L McGrath07085852003-10-09 07:28:22 +00001583 if ((BUFCACHE_FACTOR * timing_MB) >= correction) /* more than 1MB/s */
Eric Andersen50af12d2003-08-06 08:47:59 +00001584 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
Glenn L McGrath07085852003-10-09 07:28:22 +00001585 (BUFCACHE_FACTOR * timing_MB), correction,
1586 (BUFCACHE_FACTOR * timing_MB) / correction);
Eric Andersen50af12d2003-08-06 08:47:59 +00001587 else
1588 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
Glenn L McGrath07085852003-10-09 07:28:22 +00001589 (BUFCACHE_FACTOR * timing_MB), correction,
1590 (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
1591 correction /= BUFCACHE_FACTOR;
1592
1593 flush_buffer_cache(fd);
1594 sleep(1);
Eric Andersen50af12d2003-08-06 08:47:59 +00001595 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001596 else /* Time device */
1597 {
1598 printf(" Timing buffered disk reads: ");
1599 fflush(stdout);
1600
1601 /*
1602 * getitimer() is used rather than gettimeofday() because
1603 * it is much more consistent (on my machine, at least).
1604 */
1605 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1606
1607 /* Now do the timings for real */
1608 getitimer(ITIMER_REAL, &e1);
1609 for (i = TIMING_BUF_COUNT; i > 0; --i)
1610 {
1611 if (read_big_block (fd, buf))
1612 goto quit;
1613 }
1614 getitimer(ITIMER_REAL, &e2);
1615
1616 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1617
1618 if (timing_MB >= elapsed) /* more than 1MB/s */
1619 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",timing_MB, elapsed, timing_MB / elapsed);
1620 else
1621 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);
1622
1623 /*"Hmm.. suspicious results: probably not enough free memory for a proper test.");*/
1624 if (elapsed <= (correction * 2))
1625 bb_error_msg(bb_msg_memory_exhausted);
1626
1627#if 0 /* the "estimate" is just plain wrong for many systems.. */
1628 else if (correction != 0.0) {
1629 printf(" Estimating raw driver speed: ");
1630 elapsed -= correction;
1631 if (timing_MB >= elapsed) /* more than 1MB/s */
1632 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1633 timing_MB, elapsed, timing_MB / elapsed);
1634 else
1635 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1636 timing_MB, elapsed, timing_MB / elapsed * 1024);
1637 }
Eric Andersen50af12d2003-08-06 08:47:59 +00001638#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001639 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001640quit:
1641 if (-1 == shmdt(buf))
Glenn L McGrath07085852003-10-09 07:28:22 +00001642 bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001643}
1644
Glenn L McGrath07085852003-10-09 07:28:22 +00001645
Eric Andersen3443bd72003-07-22 07:30:36 +00001646static void no_scsi (void)
1647{
Glenn L McGrath07085852003-10-09 07:28:22 +00001648 /*" operation not supported on SCSI disks"*/
1649 if (is_scsi_hd)
1650 bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
Eric Andersen3443bd72003-07-22 07:30:36 +00001651}
1652
1653static void no_xt (void)
1654{
Glenn L McGrath07085852003-10-09 07:28:22 +00001655 if (is_xt_hd)
1656 bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001657}
1658
1659static void on_off (unsigned int value)
1660{
1661 printf(value ? " (on)\n" : " (off)\n");
1662}
1663
1664#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1665static void bus_state_value (unsigned int value)
1666{
1667 const char *string;
1668
Glenn L McGrath07085852003-10-09 07:28:22 +00001669 switch (value)
1670 {
1671 case BUSSTATE_ON:
1672 string = " (on)\n";
1673 break;
1674 case BUSSTATE_OFF:
1675 string = " (off)\n";
1676 break;
1677 case BUSSTATE_TRISTATE:
1678 string = " (tristate)\n";
1679 break;
1680 default:
1681 string = " (unknown: %d)\n";
1682 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001683 }
1684 printf(string, value);
1685}
1686#endif
1687
1688#ifdef HDIO_DRIVE_CMD
1689static void interpret_standby (unsigned int standby)
1690{
1691 printf(" (");
Glenn L McGrath07085852003-10-09 07:28:22 +00001692 switch(standby)
1693 {
1694 case 0:
1695 printf("off");
1696 break;
1697 case 252:
1698 printf("21 minutes");
1699 break;
1700 case 253:
1701 printf("vendor-specific");
1702 break;
1703 case 254:
1704 printf("?reserved");
1705 break;
1706 case 255:
1707 printf("21 minutes + 15 seconds");
1708 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001709 default:
Glenn L McGrath07085852003-10-09 07:28:22 +00001710 if (standby <= 240)
1711 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001712 unsigned int secs = standby * 5;
1713 unsigned int mins = secs / 60;
1714 secs %= 60;
Glenn L McGrath07085852003-10-09 07:28:22 +00001715 if_printf(mins,"%u minutes", mins);
1716 if_printf((mins && secs)," + ");
1717 if_printf(secs,"%u seconds", secs);
1718 }
1719 else if (standby <= 251)
1720 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001721 unsigned int mins = (standby - 240) * 30;
1722 unsigned int hrs = mins / 60;
1723 mins %= 60;
Glenn L McGrath07085852003-10-09 07:28:22 +00001724 if_printf(hrs,"%u hours", hrs);
1725 if_printf((hrs && mins)," + ");
1726 if_printf(mins,"%u minutes", mins);
1727 }
1728 else
Eric Andersen3443bd72003-07-22 07:30:36 +00001729 printf("illegal value");
1730 break;
1731 }
1732 printf(")\n");
1733}
1734
1735struct xfermode_entry {
1736 int val;
1737 const char *name;
1738};
1739
1740static const struct xfermode_entry xfermode_table[] = {
1741 { 8, "pio0" },
1742 { 9, "pio1" },
1743 { 10, "pio2" },
1744 { 11, "pio3" },
1745 { 12, "pio4" },
1746 { 13, "pio5" },
1747 { 14, "pio6" },
1748 { 15, "pio7" },
1749 { 16, "sdma0" },
1750 { 17, "sdma1" },
1751 { 18, "sdma2" },
1752 { 19, "sdma3" },
1753 { 20, "sdma4" },
1754 { 21, "sdma5" },
1755 { 22, "sdma6" },
1756 { 23, "sdma7" },
1757 { 32, "mdma0" },
1758 { 33, "mdma1" },
1759 { 34, "mdma2" },
1760 { 35, "mdma3" },
1761 { 36, "mdma4" },
1762 { 37, "mdma5" },
1763 { 38, "mdma6" },
1764 { 39, "mdma7" },
1765 { 64, "udma0" },
1766 { 65, "udma1" },
1767 { 66, "udma2" },
1768 { 67, "udma3" },
1769 { 68, "udma4" },
1770 { 69, "udma5" },
1771 { 70, "udma6" },
1772 { 71, "udma7" },
1773 { 0, NULL }
1774};
1775
1776static int translate_xfermode(char * name)
1777{
Glenn L McGrath07085852003-10-09 07:28:22 +00001778 const struct xfermode_entry *tmp;
1779 char *endptr;
1780 int val = -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001781
1782
Glenn L McGrath07085852003-10-09 07:28:22 +00001783 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1784 {
1785 if (!strcmp(name, tmp->name))
1786 return tmp->val;
Eric Andersen3443bd72003-07-22 07:30:36 +00001787
Glenn L McGrath07085852003-10-09 07:28:22 +00001788 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001789
Glenn L McGrath07085852003-10-09 07:28:22 +00001790 val = strtol(name, &endptr, 10);
1791 if (*endptr == '\0')
1792 return val;
1793
1794 return -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001795}
1796
1797static void interpret_xfermode (unsigned int xfermode)
1798{
1799 printf(" (");
1800 switch(xfermode) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001801 case 0:
1802 printf("default PIO mode");
1803 break;
1804 case 1:
1805 printf("default PIO mode, disable IORDY");
1806 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001807 case 8:
1808 case 9:
1809 case 10:
1810 case 11:
1811 case 12:
1812 case 13:
1813 case 14:
Glenn L McGrath07085852003-10-09 07:28:22 +00001814 case 15:
1815 printf("PIO flow control mode%u", xfermode-8);
1816 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001817 case 16:
1818 case 17:
1819 case 18:
1820 case 19:
1821 case 20:
1822 case 21:
1823 case 22:
Glenn L McGrath07085852003-10-09 07:28:22 +00001824 case 23:
1825 printf("singleword DMA mode%u", xfermode-16);
1826 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001827 case 32:
1828 case 33:
1829 case 34:
1830 case 35:
1831 case 36:
1832 case 37:
1833 case 38:
Glenn L McGrath07085852003-10-09 07:28:22 +00001834 case 39:
1835 printf("multiword DMA mode%u", xfermode-32);
1836 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001837 case 64:
1838 case 65:
1839 case 66:
1840 case 67:
1841 case 68:
1842 case 69:
1843 case 70:
Glenn L McGrath07085852003-10-09 07:28:22 +00001844 case 71:
1845 printf("UltraDMA mode%u", xfermode-64);
1846 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001847 default:
Glenn L McGrath07085852003-10-09 07:28:22 +00001848 printf("unknown, probably not valid");
1849 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001850 }
1851 printf(")\n");
1852}
1853#endif /* HDIO_DRIVE_CMD */
1854
1855#ifndef VXVM_MAJOR
1856#define VXVM_MAJOR 199
1857#endif
1858
1859#ifndef CCISS_MAJOR
1860#define CCISS_MAJOR 104
1861#endif
1862
1863static void process_dev (char *devname)
1864{
1865 int fd;
1866 static long parm, multcount;
1867 struct stat stat_buf;
1868#ifndef HDIO_DRIVE_CMD
1869 int force_operation = 0;
1870#endif
1871 if (stat(devname,&stat_buf))
1872 bb_perror_msg_and_die(devname);
1873
Glenn L McGrath07085852003-10-09 07:28:22 +00001874 switch(major(stat_buf.st_rdev))
1875 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001876#ifdef SCSI_DISK0_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001877 case (SCSI_DISK0_MAJOR):
1878 case (SCSI_DISK1_MAJOR):
1879 case (SCSI_DISK2_MAJOR):
1880 case (SCSI_DISK3_MAJOR):
1881 case (SCSI_DISK4_MAJOR):
1882 case (SCSI_DISK5_MAJOR):
1883 case (SCSI_DISK6_MAJOR):
1884 case (SCSI_DISK7_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001885#else
Glenn L McGrath07085852003-10-09 07:28:22 +00001886 case (SCSI_DISK_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001887#endif
1888#ifdef MD_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001889 case (MD_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001890#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001891 case (VXVM_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001892#ifdef LVM_BLK_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001893 case (LVM_BLK_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001894#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001895 case (CCISS_MAJOR):
1896 is_scsi_hd = 1;
1897 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001898#ifdef XT_DISK_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001899 case (XT_DISK_MAJOR):
1900 is_xt_hd = 1;
1901 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001902#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001903 case IDE0_MAJOR:
1904 case IDE1_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001905#ifdef IDE2_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001906 case IDE2_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001907#endif
1908#ifdef IDE3_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001909 case IDE3_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001910#endif
1911#ifdef IDE4_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001912 case IDE4_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001913#endif
1914#ifdef IDE5_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001915 case IDE5_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001916#endif
1917#ifdef IDE6_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001918 case IDE6_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001919#endif
1920#ifdef IDE7_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001921 case IDE7_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001922#endif
1923#ifdef IDE8_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001924 case IDE8_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001925#endif
1926#ifdef IDE9_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001927 case IDE9_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001928#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001929 break; /* do nothing */
1930 default:
1931 bb_error_msg_and_die("%s not supported",devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001932 }
1933
Glenn L McGrath07085852003-10-09 07:28:22 +00001934 fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
1935 if_printf( (!quiet),"\n%s:\n", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001936
Glenn L McGrath07085852003-10-09 07:28:22 +00001937 if (set_readahead)
1938 {
Eric Andersenf828da02004-07-20 22:53:59 +00001939 if_printf(get_readahead," setting fs readahead to %ld\n", Xreadahead);
1940 bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001941 }
1942#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001943 if (unregister_hwif)
1944 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001945 no_scsi();
1946 printf(" attempting to unregister hwif#%u\n", hwif);
Glenn L McGrath07085852003-10-09 07:28:22 +00001947 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)hwif,"HDIO_UNREGISTER_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001948 }
1949#endif
1950#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001951 if (scan_hwif)
1952 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001953 int args[3];
1954 no_scsi();
1955 printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
1956 args[0] = hwif_data;
1957 args[1] = hwif_ctrl;
1958 args[2] = hwif_irq;
Glenn L McGrath07085852003-10-09 07:28:22 +00001959 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001960 }
1961#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001962 if (set_piomode)
1963 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001964 no_scsi();
1965 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00001966
1967 if (noisy_piomode)
1968 {
1969 printf(" attempting to ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001970 if (piomode == 255)
Glenn L McGrath07085852003-10-09 07:28:22 +00001971 printf("auto-tune PIO mode\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001972 else if (piomode < 100)
Glenn L McGrath07085852003-10-09 07:28:22 +00001973 printf("set PIO mode to %d\n", piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001974 else if (piomode < 200)
Glenn L McGrath07085852003-10-09 07:28:22 +00001975 printf("set MDMA mode to %d\n", (piomode-100));
Eric Andersen3443bd72003-07-22 07:30:36 +00001976 else
Glenn L McGrath07085852003-10-09 07:28:22 +00001977 printf("set UDMA mode to %d\n", (piomode-200));
Eric Andersen3443bd72003-07-22 07:30:36 +00001978 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001979 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)piomode, "HDIO_SET_PIO_MODE");
Eric Andersen3443bd72003-07-22 07:30:36 +00001980 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001981 if (set_io32bit)
1982 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001983 no_scsi();
1984 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00001985 if_printf(get_io32bit," setting 32-bit IO_support flag to %ld\n", io32bit);
1986 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001987 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001988 if (set_mult)
1989 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001990 no_scsi();
1991 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00001992 if_printf(get_mult, " setting multcount to %ld\n", mult);
1993 if(ioctl(fd, HDIO_SET_MULTCOUNT, mult))
Eric Andersen06d4ec22004-03-19 10:53:52 +00001994 bb_perror_msg("HDIO_SET_MULTCOUNT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001995#ifndef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001996 else
1997 force_operation = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001998#endif
1999 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002000 if (set_readonly)
2001 {
2002 if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
2003 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
Eric Andersen3443bd72003-07-22 07:30:36 +00002004 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002005 if (set_unmask)
2006 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002007 no_scsi();
2008 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002009 if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
2010 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
Eric Andersen3443bd72003-07-22 07:30:36 +00002011 }
Eric Andersen0a57a792003-08-06 08:57:35 +00002012#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Glenn L McGrath07085852003-10-09 07:28:22 +00002013 if (set_dma)
2014 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002015 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002016 if_printf_on_off(get_dma," setting using_dma to %ld", dma);
2017 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00002018 }
Eric Andersen0a57a792003-08-06 08:57:35 +00002019#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
Glenn L McGrath07085852003-10-09 07:28:22 +00002020 if (set_dma_q)
2021 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002022 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002023 if_printf_on_off(get_dma_q," setting DMA queue_depth to %ld", dma_q);
2024 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00002025 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002026 if (set_nowerr)
2027 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002028 no_scsi();
2029 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002030 if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
2031 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
Eric Andersen3443bd72003-07-22 07:30:36 +00002032 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002033 if (set_keep)
2034 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002035 no_scsi();
2036 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002037 if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
2038 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
Eric Andersen3443bd72003-07-22 07:30:36 +00002039 }
2040#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00002041 if (set_doorlock)
2042 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002043 unsigned char args[4] = {0,0,0,0};
2044 no_scsi();
2045 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002046
Eric Andersen3443bd72003-07-22 07:30:36 +00002047 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
Glenn L McGrath07085852003-10-09 07:28:22 +00002048 if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
2049 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002050 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002051 if (set_dkeep)
2052 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002053 /* lock/unlock the drive's "feature" settings */
2054 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2055 no_scsi();
2056 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002057
2058 if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
Eric Andersen3443bd72003-07-22 07:30:36 +00002059 args[2] = dkeep ? 0x66 : 0xcc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002060 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002061 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002062 if (set_defects)
2063 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002064 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
2065 no_scsi();
2066 args[2] = defects ? 0x04 : 0x84;
Glenn L McGrath07085852003-10-09 07:28:22 +00002067 if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
2068 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002069 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002070 if (set_prefetch)
2071 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002072 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
2073 no_scsi();
2074 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002075
Eric Andersen3443bd72003-07-22 07:30:36 +00002076 args[1] = prefetch;
Glenn L McGrath07085852003-10-09 07:28:22 +00002077 if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
2078 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002079 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002080 if (set_xfermode)
2081 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002082 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
2083 no_scsi();
2084 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002085
Eric Andersen3443bd72003-07-22 07:30:36 +00002086 args[1] = xfermode_requested;
Glenn L McGrath07085852003-10-09 07:28:22 +00002087 if (get_xfermode)
2088 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002089 printf(" setting xfermode to %d", xfermode_requested);
2090 interpret_xfermode(xfermode_requested);
2091 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002092 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002093 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002094 if (set_lookahead)
2095 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002096 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2097 no_scsi();
2098 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002099
Eric Andersen3443bd72003-07-22 07:30:36 +00002100 args[2] = lookahead ? 0xaa : 0x55;
Glenn L McGrath07085852003-10-09 07:28:22 +00002101 if_printf_on_off(get_lookahead," setting drive read-lookahead to %ld", lookahead);
2102 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002103 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002104 if (set_apmmode)
2105 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002106 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2107 no_scsi();
Eric Andersenb2aa7762004-04-05 13:08:08 +00002108 apmmode=check_if_min_and_set_val(apmmode,1);
2109 apmmode=check_if_maj_and_set_val(apmmode,255);
Glenn L McGrath07085852003-10-09 07:28:22 +00002110 if_printf(get_apmmode," setting APM level to");
2111 if (apmmode==255)
2112 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002113 /* disable Advanced Power Management */
Glenn L McGrath07085852003-10-09 07:28:22 +00002114 args[2] = 0x85; /* feature register */
2115 if_printf(get_apmmode," disabled\n");
2116 }
2117 else
2118 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002119 /* set Advanced Power Management mode */
Glenn L McGrath07085852003-10-09 07:28:22 +00002120 args[2] = 0x05; /* feature register */
Eric Andersen3443bd72003-07-22 07:30:36 +00002121 args[1] = apmmode; /* sector count register */
Glenn L McGrath07085852003-10-09 07:28:22 +00002122 if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
2123 }
2124 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
Eric Andersen3443bd72003-07-22 07:30:36 +00002125 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002126 if (set_wcache)
2127 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002128#ifdef DO_FLUSHCACHE
2129#ifndef WIN_FLUSHCACHE
2130#define WIN_FLUSHCACHE 0xe7
2131#endif
2132 unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
2133#endif /* DO_FLUSHCACHE */
2134 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2135 no_scsi();
2136 no_xt();
2137 args[2] = wcache ? 0x02 : 0x82;
Glenn L McGrath07085852003-10-09 07:28:22 +00002138 if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00002139#ifdef DO_FLUSHCACHE
2140 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002141 bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002142#endif /* DO_FLUSHCACHE */
Glenn L McGrath07085852003-10-09 07:28:22 +00002143 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002144#ifdef DO_FLUSHCACHE
2145 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002146 bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002147#endif /* DO_FLUSHCACHE */
2148 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002149 if (set_standbynow)
2150 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002151#ifndef WIN_STANDBYNOW1
2152#define WIN_STANDBYNOW1 0xE0
2153#endif
2154#ifndef WIN_STANDBYNOW2
2155#define WIN_STANDBYNOW2 0x94
2156#endif
2157 unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
2158 unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
2159 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002160 if_printf(get_standbynow," issuing standby command\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002161 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2162 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002163 bb_perror_msg("HDIO_DRIVE_CMD(standby)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002164 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002165 if (set_sleepnow)
2166 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002167#ifndef WIN_SLEEPNOW1
2168#define WIN_SLEEPNOW1 0xE6
2169#endif
2170#ifndef WIN_SLEEPNOW2
2171#define WIN_SLEEPNOW2 0x99
2172#endif
2173 unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2174 unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2175 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002176 if_printf(get_sleepnow," issuing sleep command\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002177 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2178 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002179 bb_perror_msg("HDIO_DRIVE_CMD(sleep)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002180 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002181 if (set_seagate)
2182 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002183 unsigned char args[4] = {0xfb,0,0,0};
2184 no_scsi();
2185 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002186 if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
2187 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002188 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002189 if (set_standby)
2190 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002191 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2192 no_scsi();
2193 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002194 if (get_standby)
2195 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002196 printf(" setting standby to %lu", standby_requested);
2197 interpret_standby(standby_requested);
2198 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002199 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002200 }
2201#else /* HDIO_DRIVE_CMD */
Glenn L McGrath07085852003-10-09 07:28:22 +00002202 if (force_operation)
2203 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002204 char buf[512];
2205 flush_buffer_cache(fd);
2206 if (-1 == read(fd, buf, sizeof(buf)))
Glenn L McGrath07085852003-10-09 07:28:22 +00002207 bb_error_msg("access failed");
Eric Andersen3443bd72003-07-22 07:30:36 +00002208 }
2209#endif /* HDIO_DRIVE_CMD */
2210
2211 if (!flagcount)
2212 verbose = 1;
2213
Glenn L McGrath07085852003-10-09 07:28:22 +00002214 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
2215 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002216 no_scsi();
2217 multcount = -1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002218 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
2219 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002220 if ((verbose && !is_xt_hd) || get_mult)
Eric Andersen06d4ec22004-03-19 10:53:52 +00002221 bb_perror_msg("HDIO_GET_MULTCOUNT");
Glenn L McGrath07085852003-10-09 07:28:22 +00002222 }
2223 else if (verbose | get_mult)
2224 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002225 printf(" multcount = %2ld", multcount);
2226 on_off(multcount);
2227 }
2228 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002229 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
2230 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002231 no_scsi();
2232 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002233 if(ioctl(fd, HDIO_GET_32BIT, &parm))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002234 bb_perror_msg("HDIO_GET_32BIT");
Glenn L McGrath07085852003-10-09 07:28:22 +00002235 else
2236 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002237 printf(" IO_support =%3ld (", parm);
Glenn L McGrath07085852003-10-09 07:28:22 +00002238 switch (parm)
2239 {
2240 case 0:
2241 printf("default ");
2242 case 2:
2243 printf("16-bit)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002244 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002245 case 1:
2246 printf("32-bit)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002247 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002248 case 3:
2249 printf("32-bit w/sync)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002250 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002251 case 8:
2252 printf("Request-Queue-Bypass)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002253 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002254 default:
2255 printf("\?\?\?)\n");
2256 /*esac*/
Eric Andersen3443bd72003-07-22 07:30:36 +00002257 }
2258 }
2259 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002260 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
2261 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002262 no_scsi();
2263 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002264 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
2265 "HDIO_GET_UNMASKINTR"," unmaskirq = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002266 }
2267
Glenn L McGrath07085852003-10-09 07:28:22 +00002268
Eric Andersen0a57a792003-08-06 08:57:35 +00002269#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Eric Andersen3443bd72003-07-22 07:30:36 +00002270 if ((verbose && !is_scsi_hd) || get_dma) {
2271 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002272 if(ioctl(fd, HDIO_GET_DMA, &parm))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002273 bb_perror_msg("HDIO_GET_DMA");
Glenn L McGrath07085852003-10-09 07:28:22 +00002274 else
2275 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002276 printf(" using_dma = %2ld", parm);
2277 if (parm == 8)
2278 printf(" (DMA-Assisted-PIO)\n");
2279 else
2280 on_off(parm);
2281 }
2282 }
2283#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00002284 if (get_dma_q)
2285 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002286 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002287 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
2288 "HDIO_GET_QDMA"," queue_depth = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002289 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002290 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
2291 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002292 no_scsi();
2293 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002294 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
2295 "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002296 }
2297
Glenn L McGrath07085852003-10-09 07:28:22 +00002298 if (get_nowerr)
2299 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002300 no_scsi();
2301 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002302 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
2303 " HDIO_GET_NOWERR"," nowerr = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002304 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002305 if (verbose || get_readonly)
2306 {
2307 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
2308 " BLKROGET"," readonly = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002309 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002310 if ((verbose && !is_scsi_hd) || get_readahead)
2311 {
2312 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
2313 " BLKRAGET"," readahead = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002314 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002315 if (verbose || get_geom)
2316 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002317 static const char msg[] = " geometry = %u/%u/%u, sectors = %ld, start = %ld\n";
2318 static struct hd_geometry g;
2319#ifdef HDIO_GETGEO_BIG
2320 static struct hd_big_geometry bg;
2321#endif
2322
2323 if (ioctl(fd, BLKGETSIZE, &parm))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002324 bb_perror_msg("BLKGETSIZE");
Eric Andersen3443bd72003-07-22 07:30:36 +00002325#ifdef HDIO_GETGEO_BIG
2326 else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
2327 printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
2328#endif
2329 else if (ioctl(fd, HDIO_GETGEO, &g))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002330 bb_perror_msg("HDIO_GETGEO");
Glenn L McGrath07085852003-10-09 07:28:22 +00002331 else
2332 printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
Eric Andersen3443bd72003-07-22 07:30:36 +00002333 }
2334#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00002335 if (get_powermode)
2336 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002337#ifndef WIN_CHECKPOWERMODE1
2338#define WIN_CHECKPOWERMODE1 0xE5
2339#endif
2340#ifndef WIN_CHECKPOWERMODE2
2341#define WIN_CHECKPOWERMODE2 0x98
2342#endif
2343 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
2344 const char *state;
2345 no_scsi();
2346 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
Glenn L McGrath07085852003-10-09 07:28:22 +00002347 && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2348 && ioctl(fd, HDIO_DRIVE_CMD, &args))
2349 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002350 if (errno != EIO || args[0] != 0 || args[1] != 0)
2351 state = "unknown";
2352 else
2353 state = "sleeping";
Eric Andersen3443bd72003-07-22 07:30:36 +00002354 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002355 else
2356 state = (args[2] == 255) ? "active/idle" : "standby";
2357
Eric Andersen3443bd72003-07-22 07:30:36 +00002358 printf(" drive state is: %s\n", state);
2359 }
2360#endif
2361#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
Glenn L McGrath07085852003-10-09 07:28:22 +00002362 if (perform_reset)
2363 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002364 no_scsi();
2365 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002366 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
Eric Andersen3443bd72003-07-22 07:30:36 +00002367 }
2368#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2369#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002370 if (perform_tristate)
2371 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002372 unsigned char args[4] = {0,tristate,0,0};
2373 no_scsi();
2374 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002375 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00002376 }
2377#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2378#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +00002379 if (get_identity)
2380 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002381 static struct hd_driveid id;
2382
2383 no_scsi();
2384 no_xt();
2385
Glenn L McGrath07085852003-10-09 07:28:22 +00002386 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
2387 {
2388 if (multcount != -1)
2389 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002390 id.multsect = multcount;
2391 id.multsect_valid |= 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002392 }
2393 else
Eric Andersen3443bd72003-07-22 07:30:36 +00002394 id.multsect_valid &= ~1;
2395 dump_identity(&id);
Glenn L McGrath07085852003-10-09 07:28:22 +00002396 }
2397 else if (errno == -ENOMSG)
Eric Andersen3443bd72003-07-22 07:30:36 +00002398 printf(" no identification info available\n");
2399 else
Eric Andersen06d4ec22004-03-19 10:53:52 +00002400 bb_perror_msg("HDIO_GET_IDENTITY");
Eric Andersen3443bd72003-07-22 07:30:36 +00002401 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002402
2403 if (get_IDentity)
2404 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002405 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2406 unsigned i;
Glenn L McGrath07085852003-10-09 07:28:22 +00002407
Eric Andersen3443bd72003-07-22 07:30:36 +00002408 no_scsi();
2409 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002410
2411 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2412 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002413 args[0] = WIN_PIDENTIFY;
Glenn L McGrath07085852003-10-09 07:28:22 +00002414 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2415 {
Eric Andersen06d4ec22004-03-19 10:53:52 +00002416 bb_perror_msg("HDIO_DRIVE_CMD(identify)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002417 goto identify_abort;
2418 }
2419 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002420 for(i=0; i<(sizeof args)/2; i+=2)
Eric Andersen3443bd72003-07-22 07:30:36 +00002421 __le16_to_cpus((uint16_t *)(&args[i]));
Glenn L McGrath07085852003-10-09 07:28:22 +00002422
Eric Andersen3443bd72003-07-22 07:30:36 +00002423 identify((void *)&args[4], NULL);
Glenn L McGrath07085852003-10-09 07:28:22 +00002424identify_abort:
2425 /* VOID */;
Eric Andersen3443bd72003-07-22 07:30:36 +00002426 }
2427#endif
2428#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002429 if (set_busstate)
2430 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002431 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002432 if (get_busstate)
2433 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002434 printf(" setting bus state to %d", busstate);
2435 bus_state_value(busstate);
2436 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002437 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)busstate, "HDIO_SET_BUSSTATE");
Eric Andersen3443bd72003-07-22 07:30:36 +00002438 }
2439#endif
2440#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002441 if (get_busstate)
2442 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002443 no_scsi();
2444 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
Eric Andersen06d4ec22004-03-19 10:53:52 +00002445 bb_perror_msg("HDIO_GET_BUSSTATE");
Glenn L McGrath07085852003-10-09 07:28:22 +00002446 else
2447 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002448 printf(" busstate = %2ld", parm);
2449 bus_state_value(parm);
2450 }
2451 }
2452#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00002453 if (reread_partn)
2454 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2455
Eric Andersen3443bd72003-07-22 07:30:36 +00002456
2457 if (do_ctimings)
Glenn L McGrath07085852003-10-09 07:28:22 +00002458 do_time(0,fd); /*time cache */
Eric Andersen3443bd72003-07-22 07:30:36 +00002459 if (do_timings)
Glenn L McGrath07085852003-10-09 07:28:22 +00002460 do_time(1,fd); /*time device */
Eric Andersen3443bd72003-07-22 07:30:36 +00002461 if (do_flush)
2462 flush_buffer_cache (fd);
2463 close (fd);
2464}
2465
Eric Andersen06d4ec22004-03-19 10:53:52 +00002466static char * GET_NUMBER(char *p, unsigned long *flag, unsigned long *num)
2467{
2468 *num = 0;
2469 while (isdigit(*p)) {
2470 *flag = 1;
2471 *num = (*num * 10) + (*p++ - '0');
2472 }
2473 return p;
2474}
2475
2476static char * GET_STRING(char *p, unsigned long *flag, int *num)
2477{
2478 char *tmpstr;
2479 char name[32];
2480 tmpstr = name;
2481 tmpstr[0] = '\0';
2482 while (isalnum(*p) && (tmpstr - name) < 31) {
2483 tmpstr[0] = *p++;
2484 tmpstr[1] = '\0';
2485 ++tmpstr;
2486 }
2487 *num = translate_xfermode(name);
2488 if (*num == -1)
2489 *flag = 0;
2490 else
2491 *flag = 1;
2492 return p;
2493}
2494
Eric Andersen3443bd72003-07-22 07:30:36 +00002495#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2496static int fromhex (unsigned char c)
2497{
2498 if (c >= 'a' && c <= 'f')
2499 return 10 + (c - 'a');
2500 if (c >= '0' && c <= '9')
2501 return (c - '0');
2502 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2503}
2504
2505static int identify_from_stdin (void)
2506{
2507 unsigned short sbuf[800];
2508 unsigned char buf[1600], *b = (unsigned char *)buf;
2509 int i, count = read(0, buf, 1280);
2510
2511 if (count != 1280)
2512 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
Glenn L McGrath07085852003-10-09 07:28:22 +00002513 for (i = 0; count >= 4; ++i)
2514 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002515 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2516 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2517 b += 5;
2518 count -= 5;
2519 }
2520 identify(sbuf, NULL);
2521 return 0;
2522}
2523#endif
2524
Eric Andersenb2aa7762004-04-05 13:08:08 +00002525static void missing_arg(int arg, char c, char* add)
2526{
2527 if (!arg)
2528 bb_error_msg("-%c: missing value %s", c, (add!=NULL)? add :"");
2529}
2530
Eric Andersen3443bd72003-07-22 07:30:36 +00002531/* our main() routine: */
2532int hdparm_main(int argc, char **argv)
2533{
2534 char c, *p;
Eric Andersen3443bd72003-07-22 07:30:36 +00002535
2536 ++argv;
2537 if (!--argc)
2538 bb_show_usage();
Glenn L McGrath07085852003-10-09 07:28:22 +00002539
2540 while (argc--)
2541 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002542#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +00002543 if (!strcmp("-Istdin", *argv))
2544 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002545 exit(identify_from_stdin());
2546 }
2547#endif
2548 p = *argv++;
Glenn L McGrath07085852003-10-09 07:28:22 +00002549 if (*p == '-')
2550 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002551 if (!*++p)
2552 bb_show_usage();
Glenn L McGrath07085852003-10-09 07:28:22 +00002553 while ((c = *p++))
2554 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002555 ++flagcount;
Glenn L McGrath07085852003-10-09 07:28:22 +00002556 switch (c)
2557 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002558 case 'V':
Glenn L McGrath07085852003-10-09 07:28:22 +00002559 /*bb_error_msg_and_die("%s", VERSION);*/
2560 /* We have to return 0 here and not 1 */
2561 printf("%s %s\n",bb_applet_name, VERSION);
2562 exit(EXIT_SUCCESS);
Eric Andersen3443bd72003-07-22 07:30:36 +00002563 case 'v':
2564 verbose = 1;
2565 break;
2566#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2567 case 'I':
2568 get_IDentity = 1;
2569 break;
2570 case 'i':
2571 get_identity = 1;
2572 break;
2573#endif
2574 case 'g':
2575 get_geom = 1;
2576 break;
2577 case 'f':
2578 do_flush = 1;
2579 break;
2580 case 'q':
2581 quiet = 1;
2582 noisy = 0;
2583 break;
2584 case 'u':
2585 get_unmask = noisy;
2586 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002587 if (!*p && argc && isdigit(**argv))
2588 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002589 if((set_unmask = set_flag(p,'1'))==1)
2590 unmask = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002591 break;
Eric Andersen0a57a792003-08-06 08:57:35 +00002592#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Eric Andersen3443bd72003-07-22 07:30:36 +00002593 case 'd':
2594 get_dma = noisy;
2595 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002596 if (!*p && argc && isdigit(**argv))
2597 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002598 if((set_dma = set_flag(p,'9'))==1)
Eric Andersen3443bd72003-07-22 07:30:36 +00002599 dma = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002600 break;
Eric Andersen0a57a792003-08-06 08:57:35 +00002601#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
Eric Andersen3443bd72003-07-22 07:30:36 +00002602 case 'n':
2603 get_nowerr = noisy;
2604 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002605 if (!*p && argc && isdigit(**argv))
2606 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002607 if((set_nowerr = set_flag(p,'1'))==1)
2608 nowerr = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002609 break;
2610 case 'p':
2611 noisy_piomode = noisy;
2612 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002613 if (!*p && argc && isalnum(**argv))
2614 p = *argv++, --argc;
2615 p=GET_STRING(p,&set_piomode,&piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00002616 break;
2617 case 'r':
2618 get_readonly = noisy;
2619 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002620 if (!*p && argc && isdigit(**argv))
2621 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002622 if((set_readonly = set_flag(p,'1'))==1)
2623 readonly = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002624 break;
2625 case 'm':
2626 get_mult = noisy;
2627 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002628 if (!*p && argc && isalnum(**argv))
2629 p = *argv++, --argc;
2630 p=GET_NUMBER(p,&set_mult,&mult);
Eric Andersen3443bd72003-07-22 07:30:36 +00002631 break;
2632 case 'c':
2633 get_io32bit = noisy;
2634 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002635 if (!*p && argc && isalnum(**argv))
2636 p = *argv++, --argc;
2637 p=GET_NUMBER(p,&set_io32bit,&io32bit);
Eric Andersen3443bd72003-07-22 07:30:36 +00002638 break;
2639#ifdef HDIO_DRIVE_CMD
2640 case 'S':
2641 get_standby = noisy;
2642 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002643 if (!*p && argc && isalnum(**argv))
2644 p = *argv++, --argc;
2645 p=GET_NUMBER(p,&set_standby,&standby_requested);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002646 missing_arg(set_standby, c, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00002647 break;
2648
2649 case 'D':
2650 get_defects = noisy;
2651 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002652 if (!*p && argc && isalnum(**argv))
2653 p = *argv++, --argc;
2654 p=GET_NUMBER(p,&set_defects,&defects);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002655 missing_arg(set_defects, c, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00002656 break;
2657 case 'P':
2658 get_prefetch = noisy;
2659 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002660 if (!*p && argc && isalnum(**argv))
2661 p = *argv++, --argc;
2662 p=GET_NUMBER(p,&set_prefetch,&prefetch);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002663 missing_arg(set_prefetch, c, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00002664 break;
2665
2666 case 'X':
2667 get_xfermode = noisy;
2668 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002669 if (!*p && argc && isalnum(**argv))
2670 p = *argv++, --argc;
2671 p=GET_STRING(p,&set_xfermode,&xfermode_requested);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002672 missing_arg(set_xfermode, c, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00002673 break;
2674
2675 case 'K':
2676 get_dkeep = noisy;
2677 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002678 if (!*p && argc && isdigit(**argv))
2679 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002680 if((set_dkeep = set_flag(p,'1'))==1)
2681 dkeep = *p++ - '0';
2682 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002683 goto missing_arg_error;
Eric Andersen3443bd72003-07-22 07:30:36 +00002684 break;
2685
2686 case 'A':
2687 get_lookahead = noisy;
2688 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002689 if (!*p && argc && isdigit(**argv))
2690 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002691 if((set_lookahead = set_flag(p,'1'))==1)
2692 lookahead = *p++ - '0';
2693 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002694 goto missing_arg_error;
Eric Andersen3443bd72003-07-22 07:30:36 +00002695 break;
2696
2697 case 'L':
2698 get_doorlock = noisy;
2699 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002700 if (!*p && argc && isdigit(**argv))
2701 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002702 if((set_doorlock = set_flag(p,'1'))==1)
2703 doorlock = *p++ - '0';
2704 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002705 goto missing_arg_error;
Eric Andersen3443bd72003-07-22 07:30:36 +00002706 break;
2707
2708 case 'W':
2709 get_wcache = noisy;
2710 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002711 if (!*p && argc && isdigit(**argv))
2712 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002713 if((set_wcache = set_flag(p,'1'))==1)
2714 wcache = *p++ - '0';
2715 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002716missing_arg_error:
2717 missing_arg(1, c, "(0/1)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002718 break;
2719
2720 case 'C':
2721 get_powermode = noisy;
2722 noisy = 1;
2723 break;
2724
2725 case 'y':
2726 get_standbynow = noisy;
2727 noisy = 1;
2728 set_standbynow = 1;
2729 break;
2730
2731 case 'Y':
2732 get_sleepnow = noisy;
2733 noisy = 1;
2734 set_sleepnow = 1;
2735 break;
2736
2737 case 'z':
2738 reread_partn = 1;
2739 break;
2740
2741 case 'Z':
2742 get_seagate = noisy;
2743 noisy = 1;
2744 set_seagate = 1;
2745 break;
2746#endif /* HDIO_DRIVE_CMD */
2747 case 'k':
2748 get_keep = noisy;
2749 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002750 if (!*p && argc && isdigit(**argv))
2751 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002752 if((set_keep = set_flag(p,'1'))==1)
2753 keep = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002754 break;
2755#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2756 case 'U':
Eric Andersen06d4ec22004-03-19 10:53:52 +00002757 if (!*p && argc && isdigit(**argv))
2758 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002759 if(! p)
Eric Andersenb2aa7762004-04-05 13:08:08 +00002760 goto expected_hwif_error; /* "expected hwif_nr" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002761
2762 sscanf(p++, "%i", &hwif);
2763
2764 unregister_hwif = 1;
2765 break;
2766#endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2767#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2768 case 'R':
Eric Andersen06d4ec22004-03-19 10:53:52 +00002769 if (!*p && argc && isdigit(**argv))
2770 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002771 if(! p)
Eric Andersenb2aa7762004-04-05 13:08:08 +00002772 goto expected_hwif_error; /* "expected hwif_data" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002773
2774 sscanf(p++, "%i", &hwif_data);
2775
2776 if (argc && isdigit(**argv))
2777 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002778 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002779 goto expected_hwif_error; /* "expected hwif_ctrl" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002780
2781 sscanf(p, "%i", &hwif_ctrl);
2782
2783 if (argc && isdigit(**argv))
2784 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002785 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002786expected_hwif_error:
Glenn L McGrath07085852003-10-09 07:28:22 +00002787 bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002788
2789 sscanf(p, "%i", &hwif_irq);
2790
2791 *p = '\0';
2792
2793 scan_hwif = 1;
2794 break;
2795#endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2796 case 'Q':
Eric Andersen06d4ec22004-03-19 10:53:52 +00002797#ifdef HDIO_GET_QDMA
Eric Andersen3443bd72003-07-22 07:30:36 +00002798 get_dma_q = noisy;
2799 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002800#ifdef HDIO_SET_QDMA
2801 if (!*p && argc && isalnum(**argv))
2802 p = *argv++, --argc;
2803 p=GET_NUMBER(p,&set_dma_q,&dma_q);
2804#ifdef HDIO_GET_QDMA
2805 dma_q = -dma_q;
2806#endif
2807#endif
2808#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00002809 break;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002810
Eric Andersen3443bd72003-07-22 07:30:36 +00002811#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2812 case 'w':
2813 perform_reset = 1;
2814 break;
2815#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2816#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2817 case 'x':
Eric Andersen06d4ec22004-03-19 10:53:52 +00002818 if (!*p && argc && isdigit(**argv))
2819 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002820 if((perform_tristate = set_flag(p,'1'))==1)
2821 tristate = *p++ - '0';
2822 else
Eric Andersenb2aa7762004-04-05 13:08:08 +00002823 missing_arg(1, c, "(0/1)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002824 break;
2825
2826#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2827 case 'a':
2828 get_readahead = noisy;
2829 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002830 if (!*p && argc && isalnum(**argv))
2831 p = *argv++, --argc;
Eric Andersenf828da02004-07-20 22:53:59 +00002832 p=GET_NUMBER(p,&set_readahead,&Xreadahead);
Eric Andersen3443bd72003-07-22 07:30:36 +00002833 break;
2834 case 'B':
2835 get_apmmode = noisy;
2836 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002837 if (!*p && argc && isalnum(**argv))
2838 p = *argv++, --argc;
2839 p=GET_NUMBER(p,&set_apmmode,&apmmode);
Eric Andersenb2aa7762004-04-05 13:08:08 +00002840 missing_arg(set_apmmode, c, "(1-255)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002841 break;
2842 case 't':
2843 do_timings = 1;
2844 do_flush = 1;
2845 break;
2846 case 'T':
2847 do_ctimings = 1;
2848 do_flush = 1;
2849 break;
2850#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2851 case 'b':
2852 get_busstate = noisy;
2853 noisy = 1;
Eric Andersen06d4ec22004-03-19 10:53:52 +00002854 if (!*p && argc && isdigit(**argv))
2855 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002856 if((set_busstate = set_flag(p,'2'))==1)
2857 busstate = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002858 break;
2859#endif
2860 case 'h':
2861 default:
2862 bb_show_usage();
2863 }
2864 }
2865 if (!argc)
2866 bb_show_usage();
Eric Andersen06d4ec22004-03-19 10:53:52 +00002867 } else {
Glenn L McGrath07085852003-10-09 07:28:22 +00002868 process_dev (p);
Eric Andersen06d4ec22004-03-19 10:53:52 +00002869 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002870 }
2871 return 0 ;
2872}