4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2003-7 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2003-7 Doug Gilbert <dougg@torque.net>
9 * Parts of this file are derived from code that was
11 * Written By: Adam Radford <linux@3ware.com>
12 * Modifications By: Joel Jacobson <linux@3ware.com>
13 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
14 * Brad Strand <linux@3ware.com>
16 * Copyright (C) 1999-2003 3ware Inc.
18 * Kernel compatablity By: Andre Hedrick <andre@suse.com>
19 * Non-Copyright (C) 2000 Andre Hedrick <andre@suse.com>
21 * Other ars of this file are derived from code that was
23 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
24 * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org>
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2, or (at your option)
31 * You should have received a copy of the GNU General Public License
32 * (for example COPYING); if not, write to the Free
33 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 * This code was originally developed as a Senior Thesis by Michael Cornwell
36 * at the Concurrent Systems Laboratory (now part of the Storage Systems
37 * Research Center), Jack Baskin School of Engineering, University of
38 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
42 // This file contains the linux-specific IOCTL parts of
43 // smartmontools. It includes one interface routine for ATA devices,
44 // one for SCSI devices, and one for ATA devices behind escalade
53 #include <scsi/scsi_ioctl.h>
57 #include <sys/ioctl.h>
60 #ifndef makedev // old versions of types.h do not include sysmacros.h
61 #include <sys/sysmacros.h>
67 extern smartmonctrl
* con
;
75 #define ENOTSUP ENOSYS
77 typedef unsigned long long u8
;
80 #define ARGUSED(x) ((void)(x))
82 static const char *filenameandversion
="$Id: os_linux.cpp,v 1.98 2008/02/17 00:30:47 dpgilbert Exp $";
84 const char *os_XXXX_c_cvsid
="$Id: os_linux.cpp,v 1.98 2008/02/17 00:30:47 dpgilbert Exp $" \
85 ATACMDS_H_CVSID CONFIG_H_CVSID INT64_H_CVSID OS_LINUX_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID
;
87 // to hold onto exit code for atexit routine
88 extern int exitstatus
;
90 // global variable holding byte count of allocated memory
91 extern long long bytes
;
93 /* for passing global control variables */
94 extern smartmonctrl
*con
;
96 /* This function will setup and fix device nodes for a 3ware controller. */
97 #define MAJOR_STRING_LENGTH 3
98 #define DEVICE_STRING_LENGTH 32
99 #define NODE_STRING_LENGTH 16
100 int setup_3ware_nodes(char *nodename
, char *driver_name
) {
103 char majorstring
[MAJOR_STRING_LENGTH
+1];
104 char device_name
[DEVICE_STRING_LENGTH
+1];
105 char nodestring
[NODE_STRING_LENGTH
];
106 struct stat stat_buf
;
109 /* First try to open up /proc/devices */
110 if (!(file
= fopen("/proc/devices", "r"))) {
111 pout("Error opening /proc/devices to check/create 3ware device nodes\n");
113 return 0; // don't fail here: user might not have /proc !
116 /* Attempt to get device major number */
117 while (EOF
!= fscanf(file
, "%3s %32s", majorstring
, device_name
)) {
118 majorstring
[MAJOR_STRING_LENGTH
]='\0';
119 device_name
[DEVICE_STRING_LENGTH
]='\0';
120 if (!strncmp(device_name
, nodename
, DEVICE_STRING_LENGTH
)) {
121 tw_major
= atoi(majorstring
);
127 /* See if we found a major device number */
129 pout("No major number for /dev/%s listed in /proc/devices. Is the %s driver loaded?\n", nodename
, driver_name
);
133 /* Now check if nodes are correct */
134 for (index
=0; index
<16; index
++) {
135 sprintf(nodestring
, "/dev/%s%d", nodename
, index
);
137 /* Try to stat the node */
138 if ((stat(nodestring
, &stat_buf
))) {
139 /* Create a new node if it doesn't exist */
140 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
141 pout("problem creating 3ware device nodes %s", nodestring
);
147 /* See if nodes major and minor numbers are correct */
148 if ((tw_major
!= (int)(major(stat_buf
.st_rdev
))) ||
149 (index
!= (int)(minor(stat_buf
.st_rdev
))) ||
150 (!S_ISCHR(stat_buf
.st_mode
))) {
152 /* Delete the old node */
153 if (unlink(nodestring
)) {
154 pout("problem unlinking stale 3ware device node %s", nodestring
);
159 /* Make a new node */
160 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
161 pout("problem creating 3ware device nodes %s", nodestring
);
170 static char prev_scsi_dev
[128];
172 // equivalent to open(path, flags)
173 int deviceopen(const char *pathname
, char *type
){
176 if (0 == strcmp(type
,"SCSI")) {
177 strncpy(prev_scsi_dev
, pathname
, sizeof(prev_scsi_dev
) - 1);
178 fd
= open(pathname
, O_RDWR
| O_NONBLOCK
);
179 if (fd
< 0 && errno
== EROFS
)
180 fd
= open(pathname
, O_RDONLY
| O_NONBLOCK
);
182 } else if (0 == strcmp(type
,"ATA")) {
183 // smartd re-opens SCSI devices with "type"==ATA for some reason.
184 // If that was a SCSI generic device (e.g. /dev/sg0) then the
185 // sg driver wants O_RDWR to allow through ATA PASS-THROUGH commands.
186 // The purpose of the next code line is to limit the scope of
187 // this change as a release is pending (and smartd needs a rewrite).
188 if (0 == strncmp(pathname
, prev_scsi_dev
, sizeof(prev_scsi_dev
)))
189 return open(pathname
, O_RDWR
| O_NONBLOCK
);
191 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
192 } else if (0 == strcmp(type
,"ATA_3WARE_9000")) {
193 // the device nodes for this controller are dynamically assigned,
194 // so we need to check that they exist with the correct major
195 // numbers and if not, create them
196 if (setup_3ware_nodes("twa", "3w-9xxx")) {
201 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
203 else if (0 == strcmp(type
,"ATA_3WARE_678K")) {
204 // the device nodes for this controller are dynamically assigned,
205 // so we need to check that they exist with the correct major
206 // numbers and if not, create them
207 if (setup_3ware_nodes("twe", "3w-xxxx")) {
212 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
214 else if(0 == strcmp(type
, "CCISS")) {
215 // the device is a cciss smart array device.
216 return open(pathname
, O_RDWR
| O_NONBLOCK
);
223 // equivalent to close(file descriptor)
224 int deviceclose(int fd
){
228 // print examples for smartctl
229 void print_smartctl_examples(){
230 printf("=================================================== SMARTCTL EXAMPLES =====\n\n");
231 #ifdef HAVE_GETOPT_LONG
233 " smartctl --all /dev/hda (Prints all SMART information)\n\n"
234 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n"
235 " (Enables SMART on first disk)\n\n"
236 " smartctl --test=long /dev/hda (Executes extended disk self-test)\n\n"
237 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/hda\n"
238 " (Prints Self-Test & Attribute errors)\n"
239 " smartctl --all --device=3ware,2 /dev/sda\n"
240 " smartctl --all --device=3ware,2 /dev/twe0\n"
241 " smartctl --all --device=3ware,2 /dev/twa0\n"
242 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
243 " smartctl --all --device=hpt,1/1/3 /dev/sda\n"
244 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
245 " of the 1st channel on the 1st HighPoint RAID controller)\n"
249 " smartctl -a /dev/hda (Prints all SMART information)\n"
250 " smartctl -s on -o on -S on /dev/hda (Enables SMART on first disk)\n"
251 " smartctl -t long /dev/hda (Executes extended disk self-test)\n"
252 " smartctl -A -l selftest -q errorsonly /dev/hda\n"
253 " (Prints Self-Test & Attribute errors)\n"
254 " smartctl -a -d 3ware,2 /dev/sda\n"
255 " smartctl -a -d 3ware,2 /dev/twa0\n"
256 " smartctl -a -d 3ware,2 /dev/twe0\n"
257 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
258 " smartctl -a -d hpt,1/1/3 /dev/sda\n"
259 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
260 " of the 1st channel on the 1st HighPoint RAID controller)\n"
267 // we are going to take advantage of the fact that Linux's devfs will only
268 // have device entries for devices that exist. So if we get the equivalent of
269 // ls /dev/hd[a-t], we have all the ATA devices on the system
271 // If any errors occur, leave errno set as it was returned by the
272 // system call, and return <0.
273 int get_dev_names(char*** names
, const char* pattern
, const char* name
, int max
) {
274 int n
= 0, retglob
, i
, lim
;
278 memset(&globbuf
, 0, sizeof(globbuf
));
280 // in case of non-clean exit
283 // Use glob to look for any directory entries matching the pattern
284 if ((retglob
=glob(pattern
, GLOB_ERR
, NULL
, &globbuf
))) {
286 // glob failed: free memory and return
289 if (retglob
==GLOB_NOMATCH
){
290 pout("glob(3) found no matches for pattern %s\n", pattern
);
294 if (retglob
==GLOB_NOSPACE
)
295 pout("glob(3) ran out of memory matching pattern %s\n", pattern
);
296 #ifdef GLOB_ABORTED // missing in old versions of glob.h
297 else if (retglob
==GLOB_ABORTED
)
298 pout("glob(3) aborted matching pattern %s\n", pattern
);
301 pout("Unexplained error in glob(3) of pattern %s\n", pattern
);
306 // did we find too many paths?
307 lim
= ((int)globbuf
.gl_pathc
< max
) ? (int)globbuf
.gl_pathc
: max
;
308 if (lim
< (int)globbuf
.gl_pathc
)
309 pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n",
310 (int)globbuf
.gl_pathc
, max
, pattern
, (int)(globbuf
.gl_pathc
-max
));
312 // allocate space for up to lim number of ATA devices
313 if (!(mp
= (char **)calloc(lim
, sizeof(char*)))){
314 pout("Out of memory constructing scan device list\n");
318 // now step through the list returned by glob. If not a link, copy
319 // to list. If it is a link, evaluate it and see if the path ends
321 for (i
=0; i
<lim
; i
++){
324 // prepare a buffer for storing the link
327 // see if path is a link
328 retlink
=readlink(globbuf
.gl_pathv
[i
], linkbuf
, 1023);
330 // if not a link (or a strange link), keep it
331 if (retlink
<=0 || retlink
>1023)
332 mp
[n
++] = CustomStrDup(globbuf
.gl_pathv
[i
], 1, __LINE__
, filenameandversion
);
334 // or if it's a link that points to a disc, follow it
336 linkbuf
[retlink
]='\0';
337 if ((p
=strrchr(linkbuf
,'/')) && !strcmp(p
+1, "disc"))
338 // This is the branch of the code that gets followed if we are
339 // using devfs WITH traditional compatibility links. In this
340 // case, we add the traditional device name to the list that
342 mp
[n
++] = CustomStrDup(globbuf
.gl_pathv
[i
], 1, __LINE__
, filenameandversion
);
344 // This is the branch of the code that gets followed if we are
345 // using devfs WITHOUT traditional compatibility links. In
346 // this case, we check that the link to the directory is of
347 // the correct type, and then append "disc" to it.
348 char tmpname
[1024]={0};
349 const char * type
= (strcmp(name
,"ATA") ? "scsi" : "ide");
350 if (strstr(linkbuf
, type
)){
351 snprintf(tmpname
, 1024, "%s/disc", globbuf
.gl_pathv
[i
]);
352 mp
[n
++] = CustomStrDup(tmpname
, 1, __LINE__
, filenameandversion
);
358 // free memory, track memory usage
360 mp
= static_cast<char **>(realloc(mp
,n
*(sizeof(char*))));
361 bytes
+= n
*(sizeof(char*));
363 // and set up return values
368 // makes a list of device names to scan, for either ATA or SCSI
369 // devices. Return -1 if no memory remaining, else the number of
370 // devices on the list, which can be >=0.
371 int make_device_names (char*** devlist
, const char* name
) {
375 // for testing case where no device names are found
379 if (!strcmp(name
,"SCSI"))
380 retval
=get_dev_names(devlist
,"/dev/sd[a-z]", name
, maxdev
=26);
381 else if (!strcmp(name
,"ATA"))
382 retval
=get_dev_names(devlist
,"/dev/hd[a-t]", name
, maxdev
=20);
384 // don't recognize disk type!
387 // if we found traditional links, we are done
391 // else look for devfs entries without traditional links
392 return get_dev_names(devlist
,"/dev/discs/disc*", name
, maxdev
);
397 // This is an interface routine meant to isolate the OS dependent
398 // parts of the code, and to provide a debugging interface. Each
399 // different port and OS needs to provide it's own interface. This
401 // DETAILED DESCRIPTION OF ARGUMENTS
402 // device: is the file descriptor provided by open()
403 // command: defines the different operations.
404 // select: additional input data if needed (which log, which type of
406 // data: location to write output data, if needed (512 bytes).
407 // Note: not all commands use all arguments.
409 // -1 if the command failed
410 // 0 if the command succeeded,
411 // STATUS_CHECK routine:
412 // -1 if the command failed
413 // 0 if the command succeeded and disk SMART status is "OK"
414 // 1 if the command succeeded and disk SMART status is "FAILING"
417 #define BUFFER_LENGTH (4+512)
419 int ata_command_interface(int device
, smart_command_set command
, int select
, char *data
){
420 unsigned char buff
[BUFFER_LENGTH
];
421 // positive: bytes to write to caller. negative: bytes to READ from
422 // caller. zero: non-data command
425 const int HDIO_DRIVE_CMD_OFFSET
= 4;
427 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
428 // buff[0]: ATA COMMAND CODE REGISTER
429 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
430 // buff[2]: ATA FEATURES REGISTER
431 // buff[3]: ATA SECTOR COUNT REGISTER
433 // Note that on return:
434 // buff[2] contains the ATA SECTOR COUNT REGISTER
436 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
437 memset(buff
, 0, BUFFER_LENGTH
);
439 buff
[0]=ATA_SMART_CMD
;
441 case CHECK_POWER_MODE
:
442 buff
[0]=ATA_CHECK_POWER_MODE
;
446 buff
[2]=ATA_SMART_READ_VALUES
;
450 case READ_THRESHOLDS
:
451 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
456 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
464 buff
[0]=ATA_IDENTIFY_DEVICE
;
469 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
474 buff
[2]=ATA_SMART_ENABLE
;
478 buff
[2]=ATA_SMART_DISABLE
;
482 // this command only says if SMART is working. It could be
483 // replaced with STATUS_CHECK below.
484 buff
[2]=ATA_SMART_STATUS
;
487 // NSECT is 241 for enable but no data transfer. Use TASK ioctl.
488 buff
[1]=ATA_SMART_AUTO_OFFLINE
;
492 // NSECT is 248 for enable but no data transfer. Use TASK ioctl.
493 buff
[1]=ATA_SMART_AUTOSAVE
;
496 case IMMEDIATE_OFFLINE
:
497 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
501 // This command uses HDIO_DRIVE_TASK and has different syntax than
502 // the other commands.
503 buff
[1]=ATA_SMART_STATUS
;
506 pout("Unrecognized command %d in linux_ata_command_interface()\n"
507 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
512 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
513 // only ioctl() that can be used to WRITE data to the disk.
514 if (command
==WRITE_LOG
) {
515 unsigned char task
[sizeof(ide_task_request_t
)+512];
516 ide_task_request_t
*reqtask
=(ide_task_request_t
*) task
;
517 task_struct_t
*taskfile
=(task_struct_t
*) reqtask
->io_ports
;
520 memset(task
, 0, sizeof(task
));
523 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
524 taskfile
->sector_count
= 1;
525 taskfile
->sector_number
= select
;
526 taskfile
->low_cylinder
= 0x4f;
527 taskfile
->high_cylinder
= 0xc2;
528 taskfile
->device_head
= 0;
529 taskfile
->command
= ATA_SMART_CMD
;
531 reqtask
->data_phase
= TASKFILE_OUT
;
532 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
533 reqtask
->out_size
= 512;
534 reqtask
->in_size
= 0;
536 // copy user data into the task request structure
537 memcpy(task
+sizeof(ide_task_request_t
), data
, 512);
539 if ((retval
=ioctl(device
, HDIO_DRIVE_TASKFILE
, task
))) {
541 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
547 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
549 if (command
==STATUS_CHECK
|| command
==AUTOSAVE
|| command
==AUTO_OFFLINE
){
552 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
553 // have to read the IDE driver source code. Sigh.
554 // buff[0]: ATA COMMAND CODE REGISTER
555 // buff[1]: ATA FEATURES REGISTER
556 // buff[2]: ATA SECTOR_COUNT
557 // buff[3]: ATA SECTOR NUMBER
558 // buff[4]: ATA CYL LO REGISTER
559 // buff[5]: ATA CYL HI REGISTER
560 // buff[6]: ATA DEVICE HEAD
562 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
563 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
567 if ((retval
=ioctl(device
, HDIO_DRIVE_TASK
, buff
))) {
568 if (retval
==-EINVAL
) {
569 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
570 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
573 syserror("Error SMART Status command failed");
577 // Cyl low and Cyl high unchanged means "Good SMART status"
578 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
581 // These values mean "Bad SMART status"
582 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
585 // We haven't gotten output that makes sense; print out some debugging info
586 syserror("Error SMART Status command failed");
587 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
588 pout("Register values returned from SMART Status command are:\n");
589 pout("ST =0x%02x\n",(int)buff
[0]);
590 pout("ERR=0x%02x\n",(int)buff
[1]);
591 pout("NS =0x%02x\n",(int)buff
[2]);
592 pout("SC =0x%02x\n",(int)buff
[3]);
593 pout("CL =0x%02x\n",(int)buff
[4]);
594 pout("CH =0x%02x\n",(int)buff
[5]);
595 pout("SEL=0x%02x\n",(int)buff
[6]);
600 // Note to people doing ports to other OSes -- don't worry about
601 // this block -- you can safely ignore it. I have put it here
602 // because under linux when you do IDENTIFY DEVICE to a packet
603 // device, it generates an ugly kernel syslog error message. This
604 // is harmless but frightens users. So this block detects packet
605 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
608 // If you read only the ATA specs, it appears as if a packet device
609 // *might* respond to the IDENTIFY DEVICE command. This is
610 // misleading - it's because around the time that SFF-8020 was
611 // incorporated into the ATA-3/4 standard, the ATA authors were
612 // sloppy. See SFF-8020 and you will see that ATAPI devices have
613 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
614 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
615 if (command
==IDENTIFY
|| command
==PIDENTIFY
){
616 unsigned short deviceid
[256];
617 // check the device identity, as seen when the system was booted
618 // or the device was FIRST registered. This will not be current
619 // if the user has subsequently changed some of the parameters. If
620 // device is a packet device, swap the command interpretations.
621 if (!ioctl(device
, HDIO_GET_IDENTITY
, deviceid
) && (deviceid
[0] & 0x8000))
622 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
626 // We are now doing the HDIO_DRIVE_CMD type ioctl.
627 if ((ioctl(device
, HDIO_DRIVE_CMD
, buff
)))
630 // CHECK POWER MODE command returns information in the Sector Count
631 // register (buff[3]). Copy to return data buffer.
632 if (command
==CHECK_POWER_MODE
)
633 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
635 // if the command returns data then copy it back
637 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
642 // >>>>>> Start of general SCSI specific linux code
644 /* Linux specific code.
645 * Historically smartmontools (and smartsuite before it) used the
646 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
647 * nodes that use the SCSI subsystem. A better interface has been available
648 * via the SCSI generic (sg) driver but this involves the extra step of
649 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
650 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
651 * the sg driver have become available via the SG_IO ioctl which is available
652 * on all SCSI devices (on SCSI tape devices from lk 2.6.6).
653 * So the strategy below is to find out if the SG_IO ioctl is available and
654 * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl.
655 * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */
657 #define MAX_DXFER_LEN 1024 /* can be increased if necessary */
658 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
659 #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
660 #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
661 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
662 #define LSCSI_DRIVER_TIMEOUT 0x6
663 #define LSCSI_DID_TIME_OUT 0x3
664 #define LSCSI_DID_BUS_BUSY 0x2
665 #define LSCSI_DID_NO_CONNECT 0x1
667 #ifndef SCSI_IOCTL_SEND_COMMAND
668 #define SCSI_IOCTL_SEND_COMMAND 1
671 #define SG_IO_PRESENT_UNKNOWN 0
672 #define SG_IO_PRESENT_YES 1
673 #define SG_IO_PRESENT_NO 2
675 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
677 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
);
679 static int sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
681 /* Preferred implementation for issuing SCSI commands in linux. This
682 * function uses the SG_IO ioctl. Return 0 if command issued successfully
683 * (various status values should still be checked). If the SCSI command
684 * cannot be issued then a negative errno value is returned. */
685 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
689 ARGUSED(dev_fd
); ARGUSED(iop
); ARGUSED(report
);
692 struct sg_io_hdr io_hdr
;
696 const unsigned char * ucp
= iop
->cmnd
;
699 const int sz
= (int)sizeof(buff
);
701 np
= scsi_get_opcode_name(ucp
[0]);
702 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
703 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
704 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
706 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
707 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
709 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
710 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
711 (trunc
? " [only first 256 bytes shown]" : ""));
712 dStrHex((const char *)iop
->dxferp
,
713 (trunc
? 256 : iop
->dxfer_len
) , 1);
716 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
719 memset(&io_hdr
, 0, sizeof(struct sg_io_hdr
));
720 io_hdr
.interface_id
= 'S';
721 io_hdr
.cmd_len
= iop
->cmnd_len
;
722 io_hdr
.mx_sb_len
= iop
->max_sense_len
;
723 io_hdr
.dxfer_len
= iop
->dxfer_len
;
724 io_hdr
.dxferp
= iop
->dxferp
;
725 io_hdr
.cmdp
= iop
->cmnd
;
726 io_hdr
.sbp
= iop
->sensep
;
727 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
728 defaults to 60 seconds. */
729 io_hdr
.timeout
= ((0 == iop
->timeout
) ? 60 : iop
->timeout
) * 1000;
730 switch (iop
->dxfer_dir
) {
732 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
734 case DXFER_FROM_DEVICE
:
735 io_hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
737 case DXFER_TO_DEVICE
:
738 io_hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
741 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
744 iop
->resp_sense_len
= 0;
745 iop
->scsi_status
= 0;
747 if (ioctl(dev_fd
, SG_IO
, &io_hdr
) < 0) {
748 if (report
&& (! unknown
))
749 pout(" SG_IO ioctl failed, errno=%d [%s]\n", errno
,
753 iop
->resid
= io_hdr
.resid
;
754 iop
->scsi_status
= io_hdr
.status
;
756 pout(" scsi_status=0x%x, host_status=0x%x, driver_status=0x%x\n"
757 " info=0x%x duration=%d milliseconds resid=%d\n", io_hdr
.status
,
758 io_hdr
.host_status
, io_hdr
.driver_status
, io_hdr
.info
,
759 io_hdr
.duration
, io_hdr
.resid
);
761 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
764 len
= iop
->dxfer_len
- iop
->resid
;
765 trunc
= (len
> 256) ? 1 : 0;
767 pout(" Incoming data, len=%d%s:\n", len
,
768 (trunc
? " [only first 256 bytes shown]" : ""));
769 dStrHex((const char*)iop
->dxferp
, (trunc
? 256 : len
),
772 pout(" Incoming data trimmed to nothing by resid\n");
777 if (io_hdr
.info
| SG_INFO_CHECK
) { /* error or warning */
778 int masked_driver_status
= (LSCSI_DRIVER_MASK
& io_hdr
.driver_status
);
780 if (0 != io_hdr
.host_status
) {
781 if ((LSCSI_DID_NO_CONNECT
== io_hdr
.host_status
) ||
782 (LSCSI_DID_BUS_BUSY
== io_hdr
.host_status
) ||
783 (LSCSI_DID_TIME_OUT
== io_hdr
.host_status
))
786 return -EIO
; /* catch all */
788 if (0 != masked_driver_status
) {
789 if (LSCSI_DRIVER_TIMEOUT
== masked_driver_status
)
791 else if (LSCSI_DRIVER_SENSE
!= masked_driver_status
)
794 if (LSCSI_DRIVER_SENSE
== masked_driver_status
)
795 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
796 iop
->resp_sense_len
= io_hdr
.sb_len_wr
;
797 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
798 iop
->sensep
&& (iop
->resp_sense_len
> 0)) {
800 pout(" >>> Sense buffer, len=%d:\n",
801 (int)iop
->resp_sense_len
);
802 dStrHex((const char *)iop
->sensep
, iop
->resp_sense_len
, 1);
806 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
807 if ((iop
->sensep
[0] & 0x7f) > 0x71)
808 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
809 iop
->scsi_status
, iop
->sensep
[1] & 0xf,
810 iop
->sensep
[2], iop
->sensep
[3]);
812 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
813 iop
->scsi_status
, iop
->sensep
[2] & 0xf,
814 iop
->sensep
[12], iop
->sensep
[13]);
817 pout(" status=0x%x\n", iop
->scsi_status
);
824 struct linux_ioctl_send_command
828 UINT8 buff
[MAX_DXFER_LEN
+ 16];
831 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
832 * support: CDB length (guesses it from opcode), resid and timeout.
833 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
834 * to 2 hours in order to allow long foreground extended self tests. */
835 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
837 struct linux_ioctl_send_command wrk
;
838 int status
, buff_offset
;
841 memcpy(wrk
.buff
, iop
->cmnd
, iop
->cmnd_len
);
842 buff_offset
= iop
->cmnd_len
;
845 const unsigned char * ucp
= iop
->cmnd
;
848 const int sz
= (int)sizeof(buff
);
850 np
= scsi_get_opcode_name(ucp
[0]);
851 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
852 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
853 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
855 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
856 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
858 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
859 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
860 (trunc
? " [only first 256 bytes shown]" : ""));
861 dStrHex((const char *)iop
->dxferp
,
862 (trunc
? 256 : iop
->dxfer_len
) , 1);
865 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
868 switch (iop
->dxfer_dir
) {
873 case DXFER_FROM_DEVICE
:
875 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
877 wrk
.outbufsize
= iop
->dxfer_len
;
879 case DXFER_TO_DEVICE
:
880 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
882 memcpy(wrk
.buff
+ buff_offset
, iop
->dxferp
, iop
->dxfer_len
);
883 wrk
.inbufsize
= iop
->dxfer_len
;
887 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
890 iop
->resp_sense_len
= 0;
891 iop
->scsi_status
= 0;
893 status
= ioctl(dev_fd
, SCSI_IOCTL_SEND_COMMAND
, &wrk
);
896 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
897 errno
, strerror(errno
));
903 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
904 memcpy(iop
->dxferp
, wrk
.buff
, iop
->dxfer_len
);
906 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
908 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
909 (trunc
? " [only first 256 bytes shown]" : ""));
910 dStrHex((const char*)iop
->dxferp
,
911 (trunc
? 256 : iop
->dxfer_len
) , 1);
916 iop
->scsi_status
= status
& 0x7e; /* bits 0 and 7 used to be for vendors */
917 if (LSCSI_DRIVER_SENSE
== ((status
>> 24) & 0xf))
918 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
919 len
= (SEND_IOCTL_RESP_SENSE_LEN
< iop
->max_sense_len
) ?
920 SEND_IOCTL_RESP_SENSE_LEN
: iop
->max_sense_len
;
921 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
922 iop
->sensep
&& (len
> 0)) {
923 memcpy(iop
->sensep
, wrk
.buff
, len
);
924 iop
->resp_sense_len
= len
;
926 pout(" >>> Sense buffer, len=%d:\n", (int)len
);
927 dStrHex((const char *)wrk
.buff
, len
, 1);
931 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
932 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status
& 0xff,
933 wrk
.buff
[2] & 0xf, wrk
.buff
[12], wrk
.buff
[13]);
936 pout(" status=0x%x\n", status
);
938 if (iop
->scsi_status
> 0)
942 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
944 return -EIO
; /* give up, assume no device there */
948 /* SCSI command transmission interface function, linux version.
949 * Returns 0 if SCSI command successfully launched and response
950 * received. Even when 0 is returned the caller should check
951 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
952 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
953 * (e.g. device not present or timeout) or some other problem
954 * (e.g. timeout) then returns a negative errno value */
955 static int do_normal_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
,
960 /* implementation relies on static sg_io_state variable. If not
961 * previously set tries the SG_IO ioctl. If that succeeds assume
962 * that SG_IO ioctl functional. If it fails with an errno value
963 * other than ENODEV (no device) or permission then assume
964 * SCSI_IOCTL_SEND_COMMAND is the only option. */
965 switch (sg_io_state
) {
966 case SG_IO_PRESENT_UNKNOWN
:
967 /* ignore report argument */
968 if (0 == (res
= sg_io_cmnd_io(dev_fd
, iop
, report
, 1))) {
969 sg_io_state
= SG_IO_PRESENT_YES
;
971 } else if ((-ENODEV
== res
) || (-EACCES
== res
) || (-EPERM
== res
))
972 return res
; /* wait until we see a device */
973 sg_io_state
= SG_IO_PRESENT_NO
;
974 /* drop through by design */
975 case SG_IO_PRESENT_NO
:
976 return sisc_cmnd_io(dev_fd
, iop
, report
);
977 case SG_IO_PRESENT_YES
:
978 return sg_io_cmnd_io(dev_fd
, iop
, report
, 0);
980 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state
);
981 sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
982 return -EIO
; /* report error and reset state */
986 /* Check and call the right interface. Maybe when the do_generic_scsi_cmd_io interface is better
987 we can take off this crude way of calling the right interface */
988 int do_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
990 switch(con
->controller_type
)
992 case CONTROLLER_CCISS
:
993 #ifdef HAVE_LINUX_CCISS_IOCTL_H
994 return cciss_io_interface(dev_fd
, con
->controller_port
-1, iop
, report
);
997 static int warned
= 0;
999 pout("CCISS support is not available in this build of smartmontools,\n"
1000 "<linux/cciss_ioctl.h> was not available at build time.\n\n");
1010 return do_normal_scsi_cmnd_io(dev_fd
, iop
, report
);
1016 // >>>>>> End of general SCSI specific linux code
1019 void printwarning(smart_command_set command
);
1022 // This is an interface routine meant to isolate the OS dependent
1023 // parts of the code, and to provide a debugging interface. Each
1024 // different port and OS needs to provide it's own interface. This
1025 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
1026 // commands to be passed through the SCSI driver.
1027 // DETAILED DESCRIPTION OF ARGUMENTS
1028 // fd: is the file descriptor provided by open()
1029 // disknum is the disk number (0 to 15) in the RAID array
1030 // escalade_type indicates the type of controller type, and if scsi or char interface is used
1031 // command: defines the different operations.
1032 // select: additional input data if needed (which log, which type of
1034 // data: location to write output data, if needed (512 bytes).
1035 // Note: not all commands use all arguments.
1037 // -1 if the command failed
1038 // 0 if the command succeeded,
1039 // STATUS_CHECK routine:
1040 // -1 if the command failed
1041 // 0 if the command succeeded and disk SMART status is "OK"
1042 // 1 if the command succeeded and disk SMART status is "FAILING"
1045 /* 512 is the max payload size: increase if needed */
1046 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
1047 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
1048 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
1049 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
1051 int escalade_command_interface(int fd
, int disknum
, int escalade_type
, smart_command_set command
, int select
, char *data
){
1053 // return value and buffer for ioctl()
1054 int ioctlreturn
, readdata
=0;
1056 // Used by both the SCSI and char interfaces
1057 TW_Passthru
*passthru
=NULL
;
1058 char ioctl_buffer
[TW_IOCTL_BUFFER_SIZE
];
1060 // only used for SCSI device interface
1061 TW_Ioctl
*tw_ioctl
=NULL
;
1062 TW_Output
*tw_output
=NULL
;
1064 // only used for 6000/7000/8000 char device interface
1065 TW_New_Ioctl
*tw_ioctl_char
=NULL
;
1067 // only used for 9000 character device interface
1068 TW_Ioctl_Buf_Apache
*tw_ioctl_apache
=NULL
;
1070 memset(ioctl_buffer
, 0, TW_IOCTL_BUFFER_SIZE
);
1072 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
) {
1073 tw_ioctl_apache
= (TW_Ioctl_Buf_Apache
*)ioctl_buffer
;
1074 tw_ioctl_apache
->driver_command
.control_code
= TW_IOCTL_FIRMWARE_PASS_THROUGH
;
1075 tw_ioctl_apache
->driver_command
.buffer_length
= 512; /* payload size */
1076 passthru
= (TW_Passthru
*)&(tw_ioctl_apache
->firmware_command
.command
.oldcommand
);
1078 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
) {
1079 tw_ioctl_char
= (TW_New_Ioctl
*)ioctl_buffer
;
1080 tw_ioctl_char
->data_buffer_length
= 512;
1081 passthru
= (TW_Passthru
*)&(tw_ioctl_char
->firmware_command
);
1083 else if (escalade_type
==CONTROLLER_3WARE_678K
) {
1084 tw_ioctl
= (TW_Ioctl
*)ioctl_buffer
;
1085 tw_ioctl
->cdb
[0] = TW_IOCTL
;
1086 tw_ioctl
->opcode
= TW_ATA_PASSTHRU
;
1087 tw_ioctl
->input_length
= 512; // correct even for non-data commands
1088 tw_ioctl
->output_length
= 512; // correct even for non-data commands
1089 tw_output
= (TW_Output
*)tw_ioctl
;
1090 passthru
= (TW_Passthru
*)&(tw_ioctl
->input_data
);
1093 pout("Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1094 "Please contact " PACKAGE_BUGREPORT
"\n", escalade_type
, disknum
);
1099 // Same for (almost) all commands - but some reset below
1100 passthru
->byte0
.opcode
= TW_OP_ATA_PASSTHRU
;
1101 passthru
->request_id
= 0xFF;
1102 passthru
->unit
= disknum
;
1103 passthru
->status
= 0;
1104 passthru
->flags
= 0x1;
1105 passthru
->drive_head
= 0x0;
1106 passthru
->sector_num
= 0;
1108 // All SMART commands use this CL/CH signature. These are magic
1109 // values from the ATA specifications.
1110 passthru
->cylinder_lo
= 0x4F;
1111 passthru
->cylinder_hi
= 0xC2;
1113 // SMART ATA COMMAND REGISTER value
1114 passthru
->command
= ATA_SMART_CMD
;
1116 // Is this a command that reads or returns 512 bytes?
1117 // passthru->param values are:
1118 // 0x0 - non data command without TFR write check,
1119 // 0x8 - non data command with TFR write check,
1120 // 0xD - data command that returns data to host from device
1121 // 0xF - data command that writes data from host to device
1122 // passthru->size values are 0x5 for non-data and 0x07 for data
1123 if (command
== READ_VALUES
||
1124 command
== READ_THRESHOLDS
||
1125 command
== READ_LOG
||
1126 command
== IDENTIFY
||
1127 command
== WRITE_LOG
) {
1129 passthru
->byte0
.sgloff
= 0x5;
1130 passthru
->size
= 0x7;
1131 passthru
->param
= 0xD;
1132 passthru
->sector_count
= 0x1;
1133 // For 64-bit to work correctly, up the size of the command packet
1134 // in dwords by 1 to account for the 64-bit single sgl 'address'
1135 // field. Note that this doesn't agree with the typedefs but it's
1136 // right (agree with kernel driver behavior/typedefs).
1137 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
&& sizeof(long)==8)
1141 // Non data command -- but doesn't use large sector
1142 // count register values.
1143 passthru
->byte0
.sgloff
= 0x0;
1144 passthru
->size
= 0x5;
1145 passthru
->param
= 0x8;
1146 passthru
->sector_count
= 0x0;
1149 // Now set ATA registers depending upon command
1151 case CHECK_POWER_MODE
:
1152 passthru
->command
= ATA_CHECK_POWER_MODE
;
1153 passthru
->features
= 0;
1154 passthru
->cylinder_lo
= 0;
1155 passthru
->cylinder_hi
= 0;
1158 passthru
->features
= ATA_SMART_READ_VALUES
;
1160 case READ_THRESHOLDS
:
1161 passthru
->features
= ATA_SMART_READ_THRESHOLDS
;
1164 passthru
->features
= ATA_SMART_READ_LOG_SECTOR
;
1165 // log number to return
1166 passthru
->sector_num
= select
;
1169 if (escalade_type
== CONTROLLER_3WARE_9000_CHAR
)
1170 memcpy((unsigned char *)tw_ioctl_apache
->data_buffer
, data
, 512);
1171 else if (escalade_type
== CONTROLLER_3WARE_678K_CHAR
)
1172 memcpy((unsigned char *)tw_ioctl_char
->data_buffer
, data
, 512);
1174 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1175 // memcpy(tw_output->output_data, data, 512);
1176 printwarning(command
);
1181 passthru
->features
= ATA_SMART_WRITE_LOG_SECTOR
;
1182 passthru
->sector_count
= 1;
1183 passthru
->sector_num
= select
;
1184 passthru
->param
= 0xF; // PIO data write
1187 // ATA IDENTIFY DEVICE
1188 passthru
->command
= ATA_IDENTIFY_DEVICE
;
1189 passthru
->features
= 0;
1190 passthru
->cylinder_lo
= 0;
1191 passthru
->cylinder_hi
= 0;
1194 // 3WARE controller can NOT have packet device internally
1195 pout("WARNING - NO DEVICE FOUND ON 3WARE CONTROLLER (disk %d)\n", disknum
);
1196 pout("Note: /dev/sdX many need to be replaced with /dev/tweN or /dev/twaN\n");
1200 passthru
->features
= ATA_SMART_ENABLE
;
1203 passthru
->features
= ATA_SMART_DISABLE
;
1206 passthru
->features
= ATA_SMART_AUTO_OFFLINE
;
1207 // Enable or disable?
1208 passthru
->sector_count
= select
;
1211 passthru
->features
= ATA_SMART_AUTOSAVE
;
1212 // Enable or disable?
1213 passthru
->sector_count
= select
;
1215 case IMMEDIATE_OFFLINE
:
1216 passthru
->features
= ATA_SMART_IMMEDIATE_OFFLINE
;
1217 // What test type to run?
1218 passthru
->sector_num
= select
;
1221 passthru
->features
= ATA_SMART_STATUS
;
1224 // This is JUST to see if SMART is enabled, by giving SMART status
1225 // command. But it doesn't say if status was good, or failing.
1226 // See below for the difference.
1227 passthru
->features
= ATA_SMART_STATUS
;
1230 pout("Unrecognized command %d in linux_3ware_command_interface(disk %d)\n"
1231 "Please contact " PACKAGE_BUGREPORT
"\n", command
, disknum
);
1236 // Now send the command down through an ioctl()
1237 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1238 ioctlreturn
=ioctl(fd
, TW_IOCTL_FIRMWARE_PASS_THROUGH
, tw_ioctl_apache
);
1239 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1240 ioctlreturn
=ioctl(fd
, TW_CMD_PACKET_WITH_DATA
, tw_ioctl_char
);
1242 ioctlreturn
=ioctl(fd
, SCSI_IOCTL_SEND_COMMAND
, tw_ioctl
);
1244 // Deal with the different error cases
1246 if (CONTROLLER_3WARE_678K
==escalade_type
&& ((command
==AUTO_OFFLINE
|| command
==AUTOSAVE
) && select
)){
1247 // error here is probably a kernel driver whose version is too old
1248 printwarning(command
);
1256 // The passthru structure is valid after return from an ioctl if:
1257 // - we are using the character interface OR
1258 // - we are using the SCSI interface and this is a NON-READ-DATA command
1259 // For SCSI interface, note that we set passthru to a different
1260 // value after ioctl().
1261 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1265 passthru
=(TW_Passthru
*)&(tw_output
->output_data
);
1268 // See if the ATA command failed. Now that we have returned from
1269 // the ioctl() call, if passthru is valid, then:
1270 // - passthru->status contains the 3ware controller STATUS
1271 // - passthru->command contains the ATA STATUS register
1272 // - passthru->features contains the ATA ERROR register
1274 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1275 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1276 // While we *might* decode the ATA ERROR register, at the moment it
1277 // doesn't make much sense: we don't care in detail why the error
1280 if (passthru
&& (passthru
->status
|| (passthru
->command
& 0x21))) {
1285 // If this is a read data command, copy data to output buffer
1287 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1288 memcpy(data
, (unsigned char *)tw_ioctl_apache
->data_buffer
, 512);
1289 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1290 memcpy(data
, (unsigned char *)tw_ioctl_char
->data_buffer
, 512);
1292 memcpy(data
, tw_output
->output_data
, 512);
1295 // For STATUS_CHECK, we need to check register values
1296 if (command
==STATUS_CHECK
) {
1298 // To find out if the SMART RETURN STATUS is good or failing, we
1299 // need to examine the values of the Cylinder Low and Cylinder
1302 unsigned short cyl_lo
=passthru
->cylinder_lo
;
1303 unsigned short cyl_hi
=passthru
->cylinder_hi
;
1305 // If values in Cyl-LO and Cyl-HI are unchanged, SMART status is good.
1306 if (cyl_lo
==0x4F && cyl_hi
==0xC2)
1309 // If values in Cyl-LO and Cyl-HI are as follows, SMART status is FAIL
1310 if (cyl_lo
==0xF4 && cyl_hi
==0x2C)
1313 // Any other values mean that something has gone wrong with the command
1314 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1315 printwarning(command
);
1325 // copy sector count register (one byte!) to return data
1326 if (command
==CHECK_POWER_MODE
)
1327 *data
=*(char *)&(passthru
->sector_count
);
1329 // look for nonexistent devices/ports
1330 if (command
==IDENTIFY
&& !nonempty((unsigned char *)data
, 512)) {
1340 int marvell_command_interface(int device
,
1341 smart_command_set command
,
1351 mvsata_scsi_cmd smart_command
;
1352 unsigned char *buff
= (unsigned char *)&smart_command
.cmd
[6];
1353 // See struct hd_drive_cmd_hdr in hdreg.h
1354 // buff[0]: ATA COMMAND CODE REGISTER
1355 // buff[1]: ATA SECTOR NUMBER REGISTER
1356 // buff[2]: ATA FEATURES REGISTER
1357 // buff[3]: ATA SECTOR COUNT REGISTER
1359 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
1360 memset(&smart_command
, 0, sizeof(smart_command
));
1361 smart_command
.inlen
= 540;
1362 smart_command
.outlen
= 540;
1363 smart_command
.cmd
[0] = 0xC; //Vendor-specific code
1364 smart_command
.cmd
[4] = 6; //command length
1366 buff
[0] = ATA_SMART_CMD
;
1368 case CHECK_POWER_MODE
:
1369 buff
[0]=ATA_CHECK_POWER_MODE
;
1372 buff
[2]=ATA_SMART_READ_VALUES
;
1375 case READ_THRESHOLDS
:
1376 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1377 copydata
=buff
[1]=buff
[3]=1;
1380 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1385 buff
[0]=ATA_IDENTIFY_DEVICE
;
1389 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1393 buff
[2]=ATA_SMART_ENABLE
;
1397 buff
[2]=ATA_SMART_DISABLE
;
1402 // this command only says if SMART is working. It could be
1403 // replaced with STATUS_CHECK below.
1404 buff
[2] = ATA_SMART_STATUS
;
1407 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1408 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1411 buff
[2]=ATA_SMART_AUTOSAVE
;
1412 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1414 case IMMEDIATE_OFFLINE
:
1415 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1419 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command
);
1423 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
1425 // We are now doing the HDIO_DRIVE_CMD type ioctl.
1426 if (ioctl(device
, SCSI_IOCTL_SEND_COMMAND
, (void *)&smart_command
))
1429 if (command
==CHECK_POWER_MODE
) {
1430 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
1431 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
1436 // Always succeed on a SMART status, as a disk that failed returned
1437 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
1438 if (command
== STATUS
)
1440 //Data returned is starting from 0 offset
1441 if (command
== STATUS_CHECK
)
1443 // Cyl low and Cyl high unchanged means "Good SMART status"
1444 if (buff
[4] == 0x4F && buff
[5] == 0xC2)
1446 // These values mean "Bad SMART status"
1447 if (buff
[4] == 0xF4 && buff
[5] == 0x2C)
1449 // We haven't gotten output that makes sense; print out some debugging info
1450 syserror("Error SMART Status command failed");
1451 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT
);
1452 pout("Register values returned from SMART Status command are:\n");
1453 pout("CMD =0x%02x\n",(int)buff
[0]);
1454 pout("FR =0x%02x\n",(int)buff
[1]);
1455 pout("NS =0x%02x\n",(int)buff
[2]);
1456 pout("SC =0x%02x\n",(int)buff
[3]);
1457 pout("CL =0x%02x\n",(int)buff
[4]);
1458 pout("CH =0x%02x\n",(int)buff
[5]);
1459 pout("SEL=0x%02x\n",(int)buff
[6]);
1464 memcpy(data
, buff
, 512);
1468 // this implementation is derived from ata_command_interface with a header
1469 // packing for highpoint linux driver ioctl interface
1471 // ioctl(fd,HPTIO_CTL,buff)
1474 // structure of hpt_buff
1475 // +----+----+----+----+--------------------.....---------------------+
1476 // | 1 | 2 | 3 | 4 | 5 |
1477 // +----+----+----+----+--------------------.....---------------------+
1479 // 1: The target controller [ int ( 4 Bytes ) ]
1480 // 2: The channel of the target controllee [ int ( 4 Bytes ) ]
1481 // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
1482 // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
1483 // 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
1484 // if no pmport device, set to 1 or leave blank
1485 // 5: data [ void * ( var leangth ) ]
1487 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
1489 int highpoint_command_interface(int device
, smart_command_set command
,
1490 int select
, char *data
)
1494 unsigned char hpt_buff
[4*sizeof(int) + STRANGE_BUFFER_LENGTH
];
1495 unsigned int *hpt
= (unsigned int *)hpt_buff
;
1496 unsigned char *buff
= &hpt_buff
[4*sizeof(int)];
1498 const int HDIO_DRIVE_CMD_OFFSET
= 4;
1500 memset(hpt_buff
, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH
);
1501 hpt
[0] = con
->hpt_data
[0]; // controller id
1502 hpt
[1] = con
->hpt_data
[1]; // channel number
1503 hpt
[3] = con
->hpt_data
[2]; // pmport number
1505 buff
[0]=ATA_SMART_CMD
;
1507 case CHECK_POWER_MODE
:
1508 buff
[0]=ATA_CHECK_POWER_MODE
;
1512 buff
[2]=ATA_SMART_READ_VALUES
;
1516 case READ_THRESHOLDS
:
1517 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1522 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1530 buff
[0]=ATA_IDENTIFY_DEVICE
;
1535 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1540 buff
[2]=ATA_SMART_ENABLE
;
1544 buff
[2]=ATA_SMART_DISABLE
;
1548 buff
[2]=ATA_SMART_STATUS
;
1551 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1555 buff
[2]=ATA_SMART_AUTOSAVE
;
1558 case IMMEDIATE_OFFLINE
:
1559 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1563 buff
[1]=ATA_SMART_STATUS
;
1566 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
1567 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
1572 if (command
==WRITE_LOG
) {
1573 unsigned char task
[4*sizeof(int)+sizeof(ide_task_request_t
)+512];
1574 unsigned int *hpt
= (unsigned int *)task
;
1575 ide_task_request_t
*reqtask
= (ide_task_request_t
*)(&task
[4*sizeof(int)]);
1576 task_struct_t
*taskfile
= (task_struct_t
*)reqtask
->io_ports
;
1579 memset(task
, 0, sizeof(task
));
1581 hpt
[0] = con
->hpt_data
[0]; // controller id
1582 hpt
[1] = con
->hpt_data
[1]; // channel number
1583 hpt
[3] = con
->hpt_data
[2]; // pmport number
1584 hpt
[2] = HDIO_DRIVE_TASKFILE
; // real hd ioctl
1587 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
1588 taskfile
->sector_count
= 1;
1589 taskfile
->sector_number
= select
;
1590 taskfile
->low_cylinder
= 0x4f;
1591 taskfile
->high_cylinder
= 0xc2;
1592 taskfile
->device_head
= 0;
1593 taskfile
->command
= ATA_SMART_CMD
;
1595 reqtask
->data_phase
= TASKFILE_OUT
;
1596 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
1597 reqtask
->out_size
= 512;
1598 reqtask
->in_size
= 0;
1600 memcpy(task
+sizeof(ide_task_request_t
)+4*sizeof(int), data
, 512);
1602 if ((retval
=ioctl(device
, HPTIO_CTL
, task
))) {
1603 if (retval
==-EINVAL
)
1604 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
1610 if (command
==STATUS_CHECK
){
1612 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
1613 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
1617 hpt
[2] = HDIO_DRIVE_TASK
;
1619 if ((retval
=ioctl(device
, HPTIO_CTL
, hpt_buff
))) {
1620 if (retval
==-EINVAL
) {
1621 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
1622 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
1625 syserror("Error SMART Status command failed");
1629 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
1632 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
1635 syserror("Error SMART Status command failed");
1636 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
1637 pout("Register values returned from SMART Status command are:\n");
1638 pout("CMD=0x%02x\n",(int)buff
[0]);
1639 pout("FR =0x%02x\n",(int)buff
[1]);
1640 pout("NS =0x%02x\n",(int)buff
[2]);
1641 pout("SC =0x%02x\n",(int)buff
[3]);
1642 pout("CL =0x%02x\n",(int)buff
[4]);
1643 pout("CH =0x%02x\n",(int)buff
[5]);
1644 pout("SEL=0x%02x\n",(int)buff
[6]);
1649 if (command
==IDENTIFY
|| command
==PIDENTIFY
) {
1650 unsigned char deviceid
[4*sizeof(int)+512*sizeof(char)];
1651 unsigned int *hpt
= (unsigned int *)deviceid
;
1653 hpt
[0] = con
->hpt_data
[0]; // controller id
1654 hpt
[1] = con
->hpt_data
[1]; // channel number
1655 hpt
[3] = con
->hpt_data
[2]; // pmport number
1657 hpt
[2] = HDIO_GET_IDENTITY
;
1658 if (!ioctl(device
, HPTIO_CTL
, deviceid
) && (deviceid
[4*sizeof(int)] & 0x8000))
1659 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
1663 hpt
[2] = HDIO_DRIVE_CMD
;
1664 if ((ioctl(device
, HPTIO_CTL
, hpt_buff
)))
1667 if (command
==CHECK_POWER_MODE
)
1668 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
1671 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
1677 // Utility function for printing warnings
1678 void printwarning(smart_command_set command
){
1679 static int printed
[4]={0,0,0,0};
1680 const char* message
=
1681 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
1682 "applying a simple 3w-xxxx driver patch that can be found here:\n"
1683 PACKAGE_HOMEPAGE
"\n"
1684 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
1686 if (command
==AUTO_OFFLINE
&& !printed
[0]) {
1688 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message
);
1690 else if (command
==AUTOSAVE
&& !printed
[1]) {
1692 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message
);
1694 else if (command
==STATUS_CHECK
&& !printed
[2]) {
1696 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message
);
1698 else if (command
==WRITE_LOG
&& !printed
[3]) {
1700 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
1706 // Guess device type (ata or scsi) based on device name (Linux
1707 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
1708 // osst, nosst and sg.
1709 static const char * lin_dev_prefix
= "/dev/";
1710 static const char * lin_dev_ata_disk_plus
= "h";
1711 static const char * lin_dev_ata_devfs_disk_plus
= "ide/";
1712 static const char * lin_dev_scsi_devfs_disk_plus
= "scsi/";
1713 static const char * lin_dev_scsi_disk_plus
= "s";
1714 static const char * lin_dev_scsi_tape1
= "ns";
1715 static const char * lin_dev_scsi_tape2
= "os";
1716 static const char * lin_dev_scsi_tape3
= "nos";
1717 static const char * lin_dev_3ware_9000_char
= "twa";
1718 static const char * lin_dev_3ware_678k_char
= "twe";
1719 static const char * lin_dev_cciss_dir
= "cciss/";
1721 int guess_device_type(const char * dev_name
) {
1723 int dev_prefix_len
= strlen(lin_dev_prefix
);
1725 // if dev_name null, or string length zero
1726 if (!dev_name
|| !(len
= strlen(dev_name
)))
1727 return CONTROLLER_UNKNOWN
;
1729 // Remove the leading /dev/... if it's there
1730 if (!strncmp(lin_dev_prefix
, dev_name
, dev_prefix_len
)) {
1731 if (len
<= dev_prefix_len
)
1732 // if nothing else in the string, unrecognized
1733 return CONTROLLER_UNKNOWN
;
1734 // else advance pointer to following characters
1735 dev_name
+= dev_prefix_len
;
1738 // form /dev/h* or h*
1739 if (!strncmp(lin_dev_ata_disk_plus
, dev_name
,
1740 strlen(lin_dev_ata_disk_plus
)))
1741 return CONTROLLER_ATA
;
1743 // form /dev/ide/* or ide/*
1744 if (!strncmp(lin_dev_ata_devfs_disk_plus
, dev_name
,
1745 strlen(lin_dev_ata_devfs_disk_plus
)))
1746 return CONTROLLER_ATA
;
1748 // form /dev/s* or s*
1749 if (!strncmp(lin_dev_scsi_disk_plus
, dev_name
,
1750 strlen(lin_dev_scsi_disk_plus
)))
1751 return CONTROLLER_SCSI
;
1753 // form /dev/scsi/* or scsi/*
1754 if (!strncmp(lin_dev_scsi_devfs_disk_plus
, dev_name
,
1755 strlen(lin_dev_scsi_devfs_disk_plus
)))
1756 return CONTROLLER_SCSI
;
1758 // form /dev/ns* or ns*
1759 if (!strncmp(lin_dev_scsi_tape1
, dev_name
,
1760 strlen(lin_dev_scsi_tape1
)))
1761 return CONTROLLER_SCSI
;
1763 // form /dev/os* or os*
1764 if (!strncmp(lin_dev_scsi_tape2
, dev_name
,
1765 strlen(lin_dev_scsi_tape2
)))
1766 return CONTROLLER_SCSI
;
1768 // form /dev/nos* or nos*
1769 if (!strncmp(lin_dev_scsi_tape3
, dev_name
,
1770 strlen(lin_dev_scsi_tape3
)))
1771 return CONTROLLER_SCSI
;
1774 if (!strncmp(lin_dev_3ware_9000_char
, dev_name
,
1775 strlen(lin_dev_3ware_9000_char
)))
1776 return CONTROLLER_3WARE_9000_CHAR
;
1779 if (!strncmp(lin_dev_3ware_678k_char
, dev_name
,
1780 strlen(lin_dev_3ware_678k_char
)))
1781 return CONTROLLER_3WARE_678K_CHAR
;
1783 if (!strncmp(lin_dev_cciss_dir
, dev_name
,
1784 strlen(lin_dev_cciss_dir
)))
1785 return CONTROLLER_CCISS
;
1787 // we failed to recognize any of the forms
1788 return CONTROLLER_UNKNOWN
;
1794 [ed@firestorm ed
]$ ls
-l
/dev
/discs
1796 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc0
-> ../ide
/host2
/bus0
/target0
/lun0
/
1797 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc1
-> ../ide
/host2
/bus1
/target0
/lun0
/
1798 [ed@firestorm ed
]$ ls
-l dev
/ide
/host*/bus*/target*/lun*/disc
1799 ls
: dev
/ide
/host*/bus*/target*/lun*/disc
: No such file
or directory
1800 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/host*/bus*/target*/lun*/disc
1801 brw
------- 1 root root
33, 0 Dec
31 1969 /dev
/ide
/host2
/bus0
/target0
/lun0
/disc
1802 brw
------- 1 root root
34, 0 Dec
31 1969 /dev
/ide
/host2
/bus1
/target0
/lun0
/disc
1803 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/c
*b
*t
*u
*
1804 ls
: /dev
/ide
/c
*b
*t
*u
*: No such file
or directory
1806 Script done on Fri Nov
7 13:46:28 2003