]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - os_os2.cpp
Merge branch 'upstream'
[mirror_smartmontools-debian.git] / os_os2.cpp
1 /*
2 * os_os2.c
3 *
4 * Home page of code is: http://smartmontools.sourceforge.net
5 *
6 * Copyright (C) 2004-6 Yuri Dario <smartmontools-support@lists.sourceforge.net>
7 *
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)
11 * any later version.
12 *
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.
16 */
17
18 /*
19 *
20 * Thanks to Daniela Engert for providing sample code for SMART ioctl access.
21 *
22 */
23
24 // These are needed to define prototypes for the functions defined below
25 #include <errno.h>
26 #include "atacmds.h"
27 #include "scsicmds.h"
28 #include "utility.h"
29
30 // This is to include whatever prototypes you define in os_generic.h
31 #include "os_os2.h"
32
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;
36
37 // global handle to device driver
38 static HFILE hDevice;
39
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)
45 // implement.
46
47 #include "config.h"
48
49 typedef struct _IDEREGS {
50 UCHAR bFeaturesReg;
51 UCHAR bSectorCountReg;
52 UCHAR bSectorNumberReg;
53 UCHAR bCylLowReg;
54 UCHAR bCylHighReg;
55 UCHAR bDriveHeadReg;
56 UCHAR bCommandReg;
57 UCHAR bReserved;
58 } IDEREGS, *PIDEREGS, *LPIDEREGS;
59
60 static void unsupported(int which){
61 static int warninggiven[4];
62
63 if (which<0 || which>3)
64 return;
65
66 if (!warninggiven[which]) {
67 char msg;
68 debugmode=1;
69 warninggiven[which]=1;
70
71 switch (which) {
72 case 0:
73 msg="generate a list of devices";
74 break;
75 case 1:
76 msg="interface to Marvell-based SATA controllers";
77 break;
78 case 2:
79 msg="interface to 3ware-based RAID controllers";
80 break;
81 case 3:
82 msg="interface to SCSI devices";
83 break;
84 }
85 pout("Under OS/2, smartmontools can not %s\n");
86 }
87 return;
88 }
89
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
96 printf(
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"
105 );
106 #else
107 printf(
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"
115 );
116 #endif
117 return;
118 }
119
120 static const char * skipdev(const char * s)
121 {
122 return (!strncmp(s, "/dev/", 5) ? s + 5 : s);
123 }
124
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) {
128
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;
136 }
137
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) {
145 unsupported(0);
146 return 0;
147 }
148
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){
156
157 int fd;
158 APIRET rc;
159 ULONG ActionTaken;
160
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);
165 if (rc) {
166 char errmsg[256];
167 snprintf(errmsg,256,"Smartctl open driver IBMS506$ failed (%d)", rc);
168 errmsg[255]='\0';
169 syserror(errmsg);
170 return -1;
171 }
172
173 pathname = skipdev(pathname);
174 fd = tolower(pathname[2]) - 'a';
175
176 return fd;
177 }
178
179 // Like close(). Acts only on integer handles returned by
180 // deviceopen() above.
181 int deviceclose(int fd){
182
183 DosClose( hDevice);
184 hDevice = NULL;
185
186 return 0;
187 }
188
189 static void print_ide_regs(const IDEREGS * r, int out)
190 {
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);
194 }
195
196 //
197 // OS/2 direct ioctl interface to IBMS506$
198 //
199 int dani_ioctl( int device, int request, void* arg)
200 {
201 unsigned char* buff = (unsigned char*) arg;
202 APIRET rc;
203 DSKSP_CommandParameters Parms;
204 ULONG PLen = 1;
205 ULONG DLen = 512; //sizeof (*buf);
206 UCHAR temp;
207 ULONG value = 0;
208 IDEREGS regs;
209
210 //printf( "device %d, request 0x%x, arg[0] 0x%x, arg[2] 0x%x\n", device, request, buff[0], buff[2]);
211
212 Parms.byPhysicalUnit = device;
213 switch( buff[0]) {
214 case WIN_IDENTIFY:
215 rc = DosDevIOCtl (hDevice, DSKSP_CAT_GENERIC, DSKSP_GET_INQUIRY_DATA,
216 (PVOID)&Parms, PLen, &PLen, (PVOID)arg+4, DLen, &DLen);
217 if (rc != 0)
218 {
219 printf ("DANIS506 ATA GET HD Failed (%d,0x%x)\n", rc, rc);
220 return -1;
221 }
222 break;
223 case WIN_SMART:
224 switch( buff[2]) {
225 case SMART_STATUS:
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);
231 if (rc)
232 {
233 printf ("DANIS506 ATA GET SMART_STATUS failed (%d,0x%x)\n", rc, rc);
234 return -1;
235 }
236 buff[4] = (unsigned char)value;
237 break;
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);
241 if (rc)
242 {
243 printf ("DANIS506 ATA GET DSKSP_SMART_GET_ATTRIBUTES failed (%d,0x%x)\n", rc, rc);
244 return -1;
245 }
246 break;
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);
250 if (rc)
251 {
252 printf ("DANIS506 ATA GET DSKSP_SMART_GET_THRESHOLDS failed (%d,0x%x)\n", rc, rc);
253 return -1;
254 }
255 break;
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);
260 if (rc)
261 {
262 printf ("DANIS506 ATA GET DSKSP_SMART_READ_LOG failed (%d,0x%x)\n", rc, rc);
263 return -1;
264 }
265 break;
266 case SMART_ENABLE:
267 buff[0] = 1; // enable
268 DLen = 1;
269 rc = DosDevIOCtl (hDevice, DSKSP_CAT_SMART, DSKSP_SMART_ONOFF,
270 (PVOID)&Parms, PLen, &PLen, (PVOID)buff, DLen, &DLen);
271 if (rc) {
272 printf ("DANIS506 ATA GET DSKSP_SMART_ONOFF failed (%d,0x%x)\n", rc, rc);
273 return -1;
274 }
275 break;
276 case SMART_DISABLE:
277 buff[0] = 0; // disable
278 DLen = 1;
279 rc = DosDevIOCtl (hDevice, DSKSP_CAT_SMART, DSKSP_SMART_ONOFF,
280 (PVOID)&Parms, PLen, &PLen, (PVOID)buff, DLen, &DLen);
281 if (rc) {
282 printf ("DANIS506 ATA GET DSKSP_SMART_ONOFF failed (%d,0x%x)\n", rc, rc);
283 return -1;
284 }
285 break;
286 #if 0
287 case SMART_AUTO_OFFLINE:
288 buff[0] = buff[3]; // select field
289 DLen = 1;
290 rc = DosDevIOCtl (hDevice, DSKSP_CAT_SMART, DSKSP_SMART_AUTO_OFFLINE,
291 (PVOID)&Parms, PLen, &PLen, (PVOID)buff, DLen, &DLen);
292 if (rc) {
293 printf ("DANIS506 ATA GET DSKSP_SMART_ONOFF failed (%d,0x%x)\n", rc, rc);
294 return -1;
295 }
296 break;
297 #endif
298 case SMART_AUTOSAVE:
299 buff[0] = buff[3]; // select field
300 DLen = 1;
301 rc = DosDevIOCtl (hDevice, DSKSP_CAT_SMART, DSKSP_SMART_AUTOSAVE_ONOFF,
302 (PVOID)&Parms, PLen, &PLen, (PVOID)buff, DLen, &DLen);
303 if (rc) {
304 printf ("DANIS506 ATA DSKSP_SMART_AUTOSAVE_ONOFF failed (%d,0x%x)\n", rc, rc);
305 return -1;
306 }
307 break;
308 case SMART_IMMEDIATE_OFFLINE:
309 buff[0] = buff[1]; // select field
310 DLen = 1;
311 rc = DosDevIOCtl (hDevice, DSKSP_CAT_SMART, DSKSP_SMART_EOLI,
312 (PVOID)&Parms, PLen, &PLen, (PVOID)buff, DLen, &DLen);
313 if (rc) {
314 printf ("DANIS506 ATA GET DSKSP_SMART_EXEC_OFFLINE failed (%d,0x%x)\n", rc, rc);
315 return -1;
316 }
317 break;
318
319 default:
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");
322 return -1;
323 break;
324 }
325 break;
326 //case WIN_PIDENTIFY:
327 // break;
328 default:
329 fprintf( stderr, "unknown ioctl\n");
330 return -1;
331 break;
332 }
333
334 // ok
335 return 0;
336 }
337
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
343 // self-test).
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"
353
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)
359
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
364 int copydata=0;
365
366 const int HDIO_DRIVE_CMD_OFFSET = 4;
367
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
373
374 // Note that on return:
375 // buff[2] contains the ATA SECTOR COUNT REGISTER
376
377 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
378 memset(buff, 0, STRANGE_BUFFER_LENGTH);
379
380 //printf( "command, select %d,%d\n", command, select);
381 buff[0]=ATA_SMART_CMD;
382 switch (command){
383 case CHECK_POWER_MODE:
384 buff[0]=ATA_CHECK_POWER_MODE;
385 copydata=1;
386 break;
387 case READ_VALUES:
388 buff[2]=ATA_SMART_READ_VALUES;
389 buff[3]=1;
390 copydata=512;
391 break;
392 case READ_THRESHOLDS:
393 buff[2]=ATA_SMART_READ_THRESHOLDS;
394 buff[1]=buff[3]=1;
395 copydata=512;
396 break;
397 case READ_LOG:
398 buff[2]=ATA_SMART_READ_LOG_SECTOR;
399 buff[1]=select;
400 buff[3]=1;
401 copydata=512;
402 break;
403 case WRITE_LOG:
404 break;
405 case IDENTIFY:
406 buff[0]=ATA_IDENTIFY_DEVICE;
407 buff[3]=1;
408 copydata=512;
409 break;
410 case PIDENTIFY:
411 buff[0]=ATA_IDENTIFY_PACKET_DEVICE;
412 buff[3]=1;
413 copydata=512;
414 break;
415 case ENABLE:
416 buff[2]=ATA_SMART_ENABLE;
417 buff[1]=1;
418 break;
419 case DISABLE:
420 buff[2]=ATA_SMART_DISABLE;
421 buff[1]=1;
422 break;
423 case STATUS:
424 case STATUS_CHECK:
425 // this command only says if SMART is working. It could be
426 // replaced with STATUS_CHECK below.
427 buff[2]=ATA_SMART_STATUS;
428 buff[4]=0;
429 break;
430 case AUTO_OFFLINE:
431 buff[2]=ATA_SMART_AUTO_OFFLINE;
432 buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!!
433 break;
434 case AUTOSAVE:
435 buff[2]=ATA_SMART_AUTOSAVE;
436 buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!!
437 break;
438 case IMMEDIATE_OFFLINE:
439 buff[2]=ATA_SMART_IMMEDIATE_OFFLINE;
440 buff[1]=select;
441 break;
442 //case STATUS_CHECK:
443 // // This command uses HDIO_DRIVE_TASK and has different syntax than
444 // // the other commands.
445 // buff[1]=ATA_SMART_STATUS;
446 // break;
447 default:
448 pout("Unrecognized command %d in linux_ata_command_interface()\n"
449 "Please contact " PACKAGE_BUGREPORT "\n", command);
450 errno=ENOSYS;
451 return -1;
452 }
453
454 #if 0
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;
461 int retval;
462
463 memset(task, 0, sizeof(task));
464
465 taskfile->data = 0;
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;
473
474 reqtask->data_phase = TASKFILE_OUT;
475 reqtask->req_cmd = IDE_DRIVE_TASK_OUT;
476 reqtask->out_size = 512;
477 reqtask->in_size = 0;
478
479 // copy user data into the task request structure
480 memcpy(task+sizeof(ide_task_request_t), data, 512);
481
482 if ((retval=dani_ioctl(device, HDIO_DRIVE_TASKFILE, task))) {
483 if (retval==-EINVAL)
484 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
485 return -1;
486 }
487 return 0;
488 }
489 #endif // 0
490
491 // We are now doing the HDIO_DRIVE_CMD type ioctl.
492 if ((dani_ioctl(device, HDIO_DRIVE_CMD, buff)))
493 return -1;
494
495 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
496 // one is this:
497 if (command==STATUS_CHECK){
498 int retval;
499
500 // Cyl low and Cyl high unchanged means "Good SMART status"
501 if (buff[4]==0)
502 return 0;
503
504 // These values mean "Bad SMART status"
505 if (buff[4]==1)
506 return 1;
507
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");
511 return -1;
512 }
513
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];
518
519 // if the command returns data then copy it back
520 if (copydata)
521 memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata);
522
523 return 0;
524 }
525
526 int marvell_command_interface(int fd, smart_command_set command, int select, char *data){
527 unsupported(1);
528 return -1;
529 }
530
531 int highpoint_command_interface(int fd, smart_command_set command, int select, char *data)
532 {
533 unsupported(1);
534 return -1;
535 }
536
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
540 // controller.
541 int escalade_command_interface(int fd, int disknum, int escalade_type, smart_command_set command, int select, char *data){
542 unsupported(2);
543 return -1;
544 }
545
546 // Interface to SCSI devices. See os_linux.c
547 int do_scsi_cmnd_io(int fd, struct scsi_cmnd_io * iop, int report) {
548 unsupported(3);
549 return -ENOSYS;
550 }