4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2004-6 Yuri Dario <smartmontools-support@lists.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * You should have received a copy of the GNU General Public License
14 * (for example COPYING); if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Thanks to Daniela Engert for providing sample code for SMART ioctl access.
24 // These are needed to define prototypes for the functions defined below
30 // This is to include whatever prototypes you define in os_generic.h
33 // Needed by '-V' option (CVS versioning) of smartd/smartctl
34 const char *os_XXXX_c_cvsid
="$Id: os_os2.cpp,v 1.7 2006/09/20 16:17:31 shattered Exp $" \
35 ATACMDS_H_CVSID OS_XXXX_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID
;
37 // global handle to device driver
40 // Please eliminate the following block: both the two #includes and
41 // the 'unsupported()' function. They are only here to warn
42 // unsuspecting users that their Operating System is not supported! If
43 // you wish, you can use a similar warning mechanism for any of the
44 // functions in this file that you can not (or choose not to)
49 typedef struct _IDEREGS
{
51 UCHAR bSectorCountReg
;
52 UCHAR bSectorNumberReg
;
58 } IDEREGS
, *PIDEREGS
, *LPIDEREGS
;
60 static void unsupported(int which
){
61 static int warninggiven
[4];
63 if (which
<0 || which
>3)
66 if (!warninggiven
[which
]) {
69 warninggiven
[which
]=1;
73 msg
="generate a list of devices";
76 msg
="interface to Marvell-based SATA controllers";
79 msg
="interface to 3ware-based RAID controllers";
82 msg
="interface to SCSI devices";
85 pout("Under OS/2, smartmontools can not %s\n");
90 // print examples for smartctl. You should modify this function so
91 // that the device paths are sensible for your OS, and to eliminate
92 // unsupported commands (eg, 3ware controllers).
93 void print_smartctl_examples(){
94 printf("=================================================== SMARTCTL EXAMPLES =====\n\n");
95 #ifdef HAVE_GETOPT_LONG
97 " smartctl -a /dev/hda (Prints all SMART information)\n\n"
98 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n"
99 " (Enables SMART on first disk)\n\n"
100 " smartctl -t long /dev/hda (Executes extended disk self-test)\n\n"
101 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/hda\n"
102 " (Prints Self-Test & Attribute errors)\n"
103 " smartctl -a --device=3ware,2 /dev/sda\n"
104 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
108 " smartctl -a /dev/hda (Prints all SMART information)\n"
109 " smartctl -s on -o on -S on /dev/hda (Enables SMART on first disk)\n"
110 " smartctl -t long /dev/hda (Executes extended disk self-test)\n"
111 " smartctl -A -l selftest -q errorsonly /dev/hda\n"
112 " (Prints Self-Test & Attribute errors)\n"
113 " smartctl -a -d 3ware,2 /dev/sda\n"
114 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
120 static const char * skipdev(const char * s
)
122 return (!strncmp(s
, "/dev/", 5) ? s
+ 5 : s
);
125 // tries to guess device type given the name (a path). See utility.h
126 // for return values.
127 int guess_device_type (const char* dev_name
) {
129 //printf( "dev_name %s\n", dev_name);
130 dev_name
= skipdev(dev_name
);
131 if (!strncmp(dev_name
, "hd", 2))
132 return CONTROLLER_ATA
;
133 if (!strncmp(dev_name
, "scsi", 4))
134 return CONTROLLER_SCSI
;
135 return CONTROLLER_UNKNOWN
;
138 // makes a list of ATA or SCSI devices for the DEVICESCAN directive of
139 // smartd. Returns number N of devices, or -1 if out of
140 // memory. Allocates N+1 arrays: one of N pointers (devlist); the
141 // other N arrays each contain null-terminated character strings. In
142 // the case N==0, no arrays are allocated because the array of 0
143 // pointers has zero length, equivalent to calling malloc(0).
144 int make_device_names (char*** devlist
, const char* name
) {
149 // Like open(). Return non-negative integer handle, only used by the
150 // functions below. type=="ATA" or "SCSI". If you need to store
151 // extra information about your devices, create a private internal
152 // array within this file (see os_freebsd.cpp for an example). If you
153 // can not open the device (permission denied, does not exist, etc)
154 // set errno as open() does and return <0.
155 int deviceopen(const char *pathname
, char *type
){
161 //printf( "deviceopen pathname %s\n", pathname);
162 rc
= DosOpen ("\\DEV\\IBMS506$", &hDevice
, &ActionTaken
, 0, FILE_SYSTEM
,
163 OPEN_ACTION_OPEN_IF_EXISTS
, OPEN_SHARE_DENYNONE
|
164 OPEN_FLAGS_NOINHERIT
| OPEN_ACCESS_READONLY
, NULL
);
167 snprintf(errmsg
,256,"Smartctl open driver IBMS506$ failed (%d)", rc
);
173 pathname
= skipdev(pathname
);
174 fd
= tolower(pathname
[2]) - 'a';
179 // Like close(). Acts only on integer handles returned by
180 // deviceopen() above.
181 int deviceclose(int fd
){
189 static void print_ide_regs(const IDEREGS
* r
, int out
)
191 pout("%s=0x%02x,%s=0x%02x, SC=0x%02x, NS=0x%02x, CL=0x%02x, CH=0x%02x, SEL=0x%02x\n",
192 (out
?"STS":"CMD"), r
->bCommandReg
, (out
?"ERR":" FR"), r
->bFeaturesReg
,
193 r
->bSectorCountReg
, r
->bSectorNumberReg
, r
->bCylLowReg
, r
->bCylHighReg
, r
->bDriveHeadReg
);
197 // OS/2 direct ioctl interface to IBMS506$
199 int dani_ioctl( int device
, int request
, void* arg
)
201 unsigned char* buff
= (unsigned char*) arg
;
203 DSKSP_CommandParameters Parms
;
205 ULONG DLen
= 512; //sizeof (*buf);
210 //printf( "device %d, request 0x%x, arg[0] 0x%x, arg[2] 0x%x\n", device, request, buff[0], buff[2]);
212 Parms
.byPhysicalUnit
= device
;
215 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_GENERIC
, DSKSP_GET_INQUIRY_DATA
,
216 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)arg
+4, DLen
, &DLen
);
219 printf ("DANIS506 ATA GET HD Failed (%d,0x%x)\n", rc
, rc
);
226 DLen
= sizeof(value
);
227 // OS/2 already checks CL/CH in IBM1S506 code!! see s506rte.c (ddk)
228 // value: -1=not supported, 0=ok, 1=failing
229 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_GETSTATUS
,
230 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)&value
, DLen
, &DLen
);
233 printf ("DANIS506 ATA GET SMART_STATUS failed (%d,0x%x)\n", rc
, rc
);
236 buff
[4] = (unsigned char)value
;
238 case SMART_READ_VALUES
:
239 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_GET_ATTRIBUTES
,
240 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)arg
+4, DLen
, &DLen
);
243 printf ("DANIS506 ATA GET DSKSP_SMART_GET_ATTRIBUTES failed (%d,0x%x)\n", rc
, rc
);
247 case SMART_READ_THRESHOLDS
:
248 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_GET_THRESHOLDS
,
249 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)arg
+4, DLen
, &DLen
);
252 printf ("DANIS506 ATA GET DSKSP_SMART_GET_THRESHOLDS failed (%d,0x%x)\n", rc
, rc
);
256 case SMART_READ_LOG_SECTOR
:
257 buff
[4] = buff
[1]; // copy select field
258 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_READ_LOG
,
259 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)arg
+4, DLen
, &DLen
);
262 printf ("DANIS506 ATA GET DSKSP_SMART_READ_LOG failed (%d,0x%x)\n", rc
, rc
);
267 buff
[0] = 1; // enable
269 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_ONOFF
,
270 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)buff
, DLen
, &DLen
);
272 printf ("DANIS506 ATA GET DSKSP_SMART_ONOFF failed (%d,0x%x)\n", rc
, rc
);
277 buff
[0] = 0; // disable
279 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_ONOFF
,
280 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)buff
, DLen
, &DLen
);
282 printf ("DANIS506 ATA GET DSKSP_SMART_ONOFF failed (%d,0x%x)\n", rc
, rc
);
287 case SMART_AUTO_OFFLINE
:
288 buff
[0] = buff
[3]; // select field
290 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_AUTO_OFFLINE
,
291 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)buff
, DLen
, &DLen
);
293 printf ("DANIS506 ATA GET DSKSP_SMART_ONOFF failed (%d,0x%x)\n", rc
, rc
);
299 buff
[0] = buff
[3]; // select field
301 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_AUTOSAVE_ONOFF
,
302 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)buff
, DLen
, &DLen
);
304 printf ("DANIS506 ATA DSKSP_SMART_AUTOSAVE_ONOFF failed (%d,0x%x)\n", rc
, rc
);
308 case SMART_IMMEDIATE_OFFLINE
:
309 buff
[0] = buff
[1]; // select field
311 rc
= DosDevIOCtl (hDevice
, DSKSP_CAT_SMART
, DSKSP_SMART_EOLI
,
312 (PVOID
)&Parms
, PLen
, &PLen
, (PVOID
)buff
, DLen
, &DLen
);
314 printf ("DANIS506 ATA GET DSKSP_SMART_EXEC_OFFLINE failed (%d,0x%x)\n", rc
, rc
);
320 fprintf( stderr
, "device %d, request 0x%x, arg[0] 0x%x, arg[2] 0x%x\n", device
, request
, buff
[0], buff
[2]);
321 fprintf( stderr
, "unknown ioctl\n");
326 //case WIN_PIDENTIFY:
329 fprintf( stderr
, "unknown ioctl\n");
338 // Interface to ATA devices. See os_linux.cpp for the cannonical example.
339 // DETAILED DESCRIPTION OF ARGUMENTS
340 // device: is the integer handle provided by deviceopen()
341 // command: defines the different operations, see atacmds.h
342 // select: additional input data IF NEEDED (which log, which type of
344 // data: location to write output data, IF NEEDED (1 or 512 bytes).
345 // Note: not all commands use all arguments.
346 // RETURN VALUES (for all commands BUT command==STATUS_CHECK)
347 // -1 if the command failed
348 // 0 if the command succeeded,
349 // RETURN VALUES if command==STATUS_CHECK
350 // -1 if the command failed OR the disk SMART status can't be determined
351 // 0 if the command succeeded and disk SMART status is "OK"
352 // 1 if the command succeeded and disk SMART status is "FAILING"
354 // huge value of buffer size needed because HDIO_DRIVE_CMD assumes
355 // that buff[3] is the data size. Since the ATA_SMART_AUTOSAVE and
356 // ATA_SMART_AUTO_OFFLINE use values of 0xf1 and 0xf8 we need the space.
357 // Otherwise a 4+512 byte buffer would be enough.
358 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
360 int ata_command_interface(int device
, smart_command_set command
, int select
, char *data
){
361 unsigned char buff
[STRANGE_BUFFER_LENGTH
];
362 // positive: bytes to write to caller. negative: bytes to READ from
363 // caller. zero: non-data command
366 const int HDIO_DRIVE_CMD_OFFSET
= 4;
368 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
369 // buff[0]: ATA COMMAND CODE REGISTER
370 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
371 // buff[2]: ATA FEATURES REGISTER
372 // buff[3]: ATA SECTOR COUNT REGISTER
374 // Note that on return:
375 // buff[2] contains the ATA SECTOR COUNT REGISTER
377 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
378 memset(buff
, 0, STRANGE_BUFFER_LENGTH
);
380 //printf( "command, select %d,%d\n", command, select);
381 buff
[0]=ATA_SMART_CMD
;
383 case CHECK_POWER_MODE
:
384 buff
[0]=ATA_CHECK_POWER_MODE
;
388 buff
[2]=ATA_SMART_READ_VALUES
;
392 case READ_THRESHOLDS
:
393 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
398 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
406 buff
[0]=ATA_IDENTIFY_DEVICE
;
411 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
416 buff
[2]=ATA_SMART_ENABLE
;
420 buff
[2]=ATA_SMART_DISABLE
;
425 // this command only says if SMART is working. It could be
426 // replaced with STATUS_CHECK below.
427 buff
[2]=ATA_SMART_STATUS
;
431 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
432 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
435 buff
[2]=ATA_SMART_AUTOSAVE
;
436 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
438 case IMMEDIATE_OFFLINE
:
439 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
443 // // This command uses HDIO_DRIVE_TASK and has different syntax than
444 // // the other commands.
445 // buff[1]=ATA_SMART_STATUS;
448 pout("Unrecognized command %d in linux_ata_command_interface()\n"
449 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
455 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
456 // only ioctl() that can be used to WRITE data to the disk.
457 if (command
==WRITE_LOG
) {
458 unsigned char task
[sizeof(ide_task_request_t
)+512];
459 ide_task_request_t
*reqtask
=(ide_task_request_t
*) task
;
460 task_struct_t
*taskfile
=(task_struct_t
*) reqtask
->io_ports
;
463 memset(task
, 0, sizeof(task
));
466 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
467 taskfile
->sector_count
= 1;
468 taskfile
->sector_number
= select
;
469 taskfile
->low_cylinder
= 0x4f;
470 taskfile
->high_cylinder
= 0xc2;
471 taskfile
->device_head
= 0;
472 taskfile
->command
= ATA_SMART_CMD
;
474 reqtask
->data_phase
= TASKFILE_OUT
;
475 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
476 reqtask
->out_size
= 512;
477 reqtask
->in_size
= 0;
479 // copy user data into the task request structure
480 memcpy(task
+sizeof(ide_task_request_t
), data
, 512);
482 if ((retval
=dani_ioctl(device
, HDIO_DRIVE_TASKFILE
, task
))) {
484 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
491 // We are now doing the HDIO_DRIVE_CMD type ioctl.
492 if ((dani_ioctl(device
, HDIO_DRIVE_CMD
, buff
)))
495 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
497 if (command
==STATUS_CHECK
){
500 // Cyl low and Cyl high unchanged means "Good SMART status"
504 // These values mean "Bad SMART status"
508 // We haven't gotten output that makes sense; print out some debugging info
509 syserror("Error SMART Status command failed");
510 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
514 // CHECK POWER MODE command returns information in the Sector Count
515 // register (buff[3]). Copy to return data buffer.
516 if (command
==CHECK_POWER_MODE
)
517 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
519 // if the command returns data then copy it back
521 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
526 int marvell_command_interface(int fd
, smart_command_set command
, int select
, char *data
){
531 int highpoint_command_interface(int fd
, smart_command_set command
, int select
, char *data
)
537 // Interface to ATA devices behind 3ware escalade RAID controller
538 // cards. Same description as ata_command_interface() above except
539 // that 0 <= disknum <= 15 specifies the ATA disk attached to the
541 int escalade_command_interface(int fd
, int disknum
, int escalade_type
, smart_command_set command
, int select
, char *data
){
546 // Interface to SCSI devices. See os_linux.c
547 int do_scsi_cmnd_io(int fd
, struct scsi_cmnd_io
* iop
, int report
) {