4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2003-8 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2003-8 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.100 2008/03/04 22:09:47 ballen4705 Exp $";
84 const char *os_XXXX_c_cvsid
="$Id: os_linux.cpp,v 1.100 2008/03/04 22:09:47 ballen4705 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 // NOTE: According to ATAPI 4 and UP, this command is obsolete
488 // select == 241 for enable but no data transfer. Use TASK ioctl.
489 buff
[1]=ATA_SMART_AUTO_OFFLINE
;
493 // select == 248 for enable but no data transfer. Use TASK ioctl.
494 buff
[1]=ATA_SMART_AUTOSAVE
;
497 case IMMEDIATE_OFFLINE
:
498 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
502 // This command uses HDIO_DRIVE_TASK and has different syntax than
503 // the other commands.
504 buff
[1]=ATA_SMART_STATUS
;
507 pout("Unrecognized command %d in linux_ata_command_interface()\n"
508 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
513 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
514 // only ioctl() that can be used to WRITE data to the disk.
515 if (command
==WRITE_LOG
) {
516 unsigned char task
[sizeof(ide_task_request_t
)+512];
517 ide_task_request_t
*reqtask
=(ide_task_request_t
*) task
;
518 task_struct_t
*taskfile
=(task_struct_t
*) reqtask
->io_ports
;
521 memset(task
, 0, sizeof(task
));
524 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
525 taskfile
->sector_count
= 1;
526 taskfile
->sector_number
= select
;
527 taskfile
->low_cylinder
= 0x4f;
528 taskfile
->high_cylinder
= 0xc2;
529 taskfile
->device_head
= 0;
530 taskfile
->command
= ATA_SMART_CMD
;
532 reqtask
->data_phase
= TASKFILE_OUT
;
533 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
534 reqtask
->out_size
= 512;
535 reqtask
->in_size
= 0;
537 // copy user data into the task request structure
538 memcpy(task
+sizeof(ide_task_request_t
), data
, 512);
540 if ((retval
=ioctl(device
, HDIO_DRIVE_TASKFILE
, task
))) {
542 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
548 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
550 if (command
==STATUS_CHECK
|| command
==AUTOSAVE
|| command
==AUTO_OFFLINE
){
553 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
554 // have to read the IDE driver source code. Sigh.
555 // buff[0]: ATA COMMAND CODE REGISTER
556 // buff[1]: ATA FEATURES REGISTER
557 // buff[2]: ATA SECTOR_COUNT
558 // buff[3]: ATA SECTOR NUMBER
559 // buff[4]: ATA CYL LO REGISTER
560 // buff[5]: ATA CYL HI REGISTER
561 // buff[6]: ATA DEVICE HEAD
563 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
564 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
568 if ((retval
=ioctl(device
, HDIO_DRIVE_TASK
, buff
))) {
569 if (retval
==-EINVAL
) {
570 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
571 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
574 syserror("Error SMART Status command failed");
578 // Cyl low and Cyl high unchanged means "Good SMART status"
579 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
582 // These values mean "Bad SMART status"
583 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
586 // We haven't gotten output that makes sense; print out some debugging info
587 syserror("Error SMART Status command failed");
588 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
589 pout("Register values returned from SMART Status command are:\n");
590 pout("ST =0x%02x\n",(int)buff
[0]);
591 pout("ERR=0x%02x\n",(int)buff
[1]);
592 pout("NS =0x%02x\n",(int)buff
[2]);
593 pout("SC =0x%02x\n",(int)buff
[3]);
594 pout("CL =0x%02x\n",(int)buff
[4]);
595 pout("CH =0x%02x\n",(int)buff
[5]);
596 pout("SEL=0x%02x\n",(int)buff
[6]);
601 // Note to people doing ports to other OSes -- don't worry about
602 // this block -- you can safely ignore it. I have put it here
603 // because under linux when you do IDENTIFY DEVICE to a packet
604 // device, it generates an ugly kernel syslog error message. This
605 // is harmless but frightens users. So this block detects packet
606 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
609 // If you read only the ATA specs, it appears as if a packet device
610 // *might* respond to the IDENTIFY DEVICE command. This is
611 // misleading - it's because around the time that SFF-8020 was
612 // incorporated into the ATA-3/4 standard, the ATA authors were
613 // sloppy. See SFF-8020 and you will see that ATAPI devices have
614 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
615 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
616 if (command
==IDENTIFY
|| command
==PIDENTIFY
){
617 unsigned short deviceid
[256];
618 // check the device identity, as seen when the system was booted
619 // or the device was FIRST registered. This will not be current
620 // if the user has subsequently changed some of the parameters. If
621 // device is a packet device, swap the command interpretations.
622 if (!ioctl(device
, HDIO_GET_IDENTITY
, deviceid
) && (deviceid
[0] & 0x8000))
623 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
627 // We are now doing the HDIO_DRIVE_CMD type ioctl.
628 if ((ioctl(device
, HDIO_DRIVE_CMD
, buff
)))
631 // CHECK POWER MODE command returns information in the Sector Count
632 // register (buff[3]). Copy to return data buffer.
633 if (command
==CHECK_POWER_MODE
)
634 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
636 // if the command returns data then copy it back
638 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
643 // >>>>>> Start of general SCSI specific linux code
645 /* Linux specific code.
646 * Historically smartmontools (and smartsuite before it) used the
647 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
648 * nodes that use the SCSI subsystem. A better interface has been available
649 * via the SCSI generic (sg) driver but this involves the extra step of
650 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
651 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
652 * the sg driver have become available via the SG_IO ioctl which is available
653 * on all SCSI devices (on SCSI tape devices from lk 2.6.6).
654 * So the strategy below is to find out if the SG_IO ioctl is available and
655 * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl.
656 * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */
658 #define MAX_DXFER_LEN 1024 /* can be increased if necessary */
659 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
660 #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
661 #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
662 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
663 #define LSCSI_DRIVER_TIMEOUT 0x6
664 #define LSCSI_DID_TIME_OUT 0x3
665 #define LSCSI_DID_BUS_BUSY 0x2
666 #define LSCSI_DID_NO_CONNECT 0x1
668 #ifndef SCSI_IOCTL_SEND_COMMAND
669 #define SCSI_IOCTL_SEND_COMMAND 1
672 #define SG_IO_PRESENT_UNKNOWN 0
673 #define SG_IO_PRESENT_YES 1
674 #define SG_IO_PRESENT_NO 2
676 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
678 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
);
680 static int sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
682 /* Preferred implementation for issuing SCSI commands in linux. This
683 * function uses the SG_IO ioctl. Return 0 if command issued successfully
684 * (various status values should still be checked). If the SCSI command
685 * cannot be issued then a negative errno value is returned. */
686 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
690 ARGUSED(dev_fd
); ARGUSED(iop
); ARGUSED(report
);
693 struct sg_io_hdr io_hdr
;
697 const unsigned char * ucp
= iop
->cmnd
;
700 const int sz
= (int)sizeof(buff
);
702 np
= scsi_get_opcode_name(ucp
[0]);
703 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
704 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
705 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
707 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
708 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
710 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
711 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
712 (trunc
? " [only first 256 bytes shown]" : ""));
713 dStrHex((const char *)iop
->dxferp
,
714 (trunc
? 256 : iop
->dxfer_len
) , 1);
717 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
720 memset(&io_hdr
, 0, sizeof(struct sg_io_hdr
));
721 io_hdr
.interface_id
= 'S';
722 io_hdr
.cmd_len
= iop
->cmnd_len
;
723 io_hdr
.mx_sb_len
= iop
->max_sense_len
;
724 io_hdr
.dxfer_len
= iop
->dxfer_len
;
725 io_hdr
.dxferp
= iop
->dxferp
;
726 io_hdr
.cmdp
= iop
->cmnd
;
727 io_hdr
.sbp
= iop
->sensep
;
728 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
729 defaults to 60 seconds. */
730 io_hdr
.timeout
= ((0 == iop
->timeout
) ? 60 : iop
->timeout
) * 1000;
731 switch (iop
->dxfer_dir
) {
733 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
735 case DXFER_FROM_DEVICE
:
736 io_hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
738 case DXFER_TO_DEVICE
:
739 io_hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
742 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
745 iop
->resp_sense_len
= 0;
746 iop
->scsi_status
= 0;
748 if (ioctl(dev_fd
, SG_IO
, &io_hdr
) < 0) {
749 if (report
&& (! unknown
))
750 pout(" SG_IO ioctl failed, errno=%d [%s]\n", errno
,
754 iop
->resid
= io_hdr
.resid
;
755 iop
->scsi_status
= io_hdr
.status
;
757 pout(" scsi_status=0x%x, host_status=0x%x, driver_status=0x%x\n"
758 " info=0x%x duration=%d milliseconds resid=%d\n", io_hdr
.status
,
759 io_hdr
.host_status
, io_hdr
.driver_status
, io_hdr
.info
,
760 io_hdr
.duration
, io_hdr
.resid
);
762 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
765 len
= iop
->dxfer_len
- iop
->resid
;
766 trunc
= (len
> 256) ? 1 : 0;
768 pout(" Incoming data, len=%d%s:\n", len
,
769 (trunc
? " [only first 256 bytes shown]" : ""));
770 dStrHex((const char*)iop
->dxferp
, (trunc
? 256 : len
),
773 pout(" Incoming data trimmed to nothing by resid\n");
778 if (io_hdr
.info
| SG_INFO_CHECK
) { /* error or warning */
779 int masked_driver_status
= (LSCSI_DRIVER_MASK
& io_hdr
.driver_status
);
781 if (0 != io_hdr
.host_status
) {
782 if ((LSCSI_DID_NO_CONNECT
== io_hdr
.host_status
) ||
783 (LSCSI_DID_BUS_BUSY
== io_hdr
.host_status
) ||
784 (LSCSI_DID_TIME_OUT
== io_hdr
.host_status
))
787 return -EIO
; /* catch all */
789 if (0 != masked_driver_status
) {
790 if (LSCSI_DRIVER_TIMEOUT
== masked_driver_status
)
792 else if (LSCSI_DRIVER_SENSE
!= masked_driver_status
)
795 if (LSCSI_DRIVER_SENSE
== masked_driver_status
)
796 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
797 iop
->resp_sense_len
= io_hdr
.sb_len_wr
;
798 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
799 iop
->sensep
&& (iop
->resp_sense_len
> 0)) {
801 pout(" >>> Sense buffer, len=%d:\n",
802 (int)iop
->resp_sense_len
);
803 dStrHex((const char *)iop
->sensep
, iop
->resp_sense_len
, 1);
807 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
808 if ((iop
->sensep
[0] & 0x7f) > 0x71)
809 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
810 iop
->scsi_status
, iop
->sensep
[1] & 0xf,
811 iop
->sensep
[2], iop
->sensep
[3]);
813 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
814 iop
->scsi_status
, iop
->sensep
[2] & 0xf,
815 iop
->sensep
[12], iop
->sensep
[13]);
818 pout(" status=0x%x\n", iop
->scsi_status
);
825 struct linux_ioctl_send_command
829 UINT8 buff
[MAX_DXFER_LEN
+ 16];
832 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
833 * support: CDB length (guesses it from opcode), resid and timeout.
834 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
835 * to 2 hours in order to allow long foreground extended self tests. */
836 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
838 struct linux_ioctl_send_command wrk
;
839 int status
, buff_offset
;
842 memcpy(wrk
.buff
, iop
->cmnd
, iop
->cmnd_len
);
843 buff_offset
= iop
->cmnd_len
;
846 const unsigned char * ucp
= iop
->cmnd
;
849 const int sz
= (int)sizeof(buff
);
851 np
= scsi_get_opcode_name(ucp
[0]);
852 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
853 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
854 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
856 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
857 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
859 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
860 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
861 (trunc
? " [only first 256 bytes shown]" : ""));
862 dStrHex((const char *)iop
->dxferp
,
863 (trunc
? 256 : iop
->dxfer_len
) , 1);
866 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
869 switch (iop
->dxfer_dir
) {
874 case DXFER_FROM_DEVICE
:
876 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
878 wrk
.outbufsize
= iop
->dxfer_len
;
880 case DXFER_TO_DEVICE
:
881 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
883 memcpy(wrk
.buff
+ buff_offset
, iop
->dxferp
, iop
->dxfer_len
);
884 wrk
.inbufsize
= iop
->dxfer_len
;
888 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
891 iop
->resp_sense_len
= 0;
892 iop
->scsi_status
= 0;
894 status
= ioctl(dev_fd
, SCSI_IOCTL_SEND_COMMAND
, &wrk
);
897 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
898 errno
, strerror(errno
));
904 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
905 memcpy(iop
->dxferp
, wrk
.buff
, iop
->dxfer_len
);
907 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
909 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
910 (trunc
? " [only first 256 bytes shown]" : ""));
911 dStrHex((const char*)iop
->dxferp
,
912 (trunc
? 256 : iop
->dxfer_len
) , 1);
917 iop
->scsi_status
= status
& 0x7e; /* bits 0 and 7 used to be for vendors */
918 if (LSCSI_DRIVER_SENSE
== ((status
>> 24) & 0xf))
919 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
920 len
= (SEND_IOCTL_RESP_SENSE_LEN
< iop
->max_sense_len
) ?
921 SEND_IOCTL_RESP_SENSE_LEN
: iop
->max_sense_len
;
922 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
923 iop
->sensep
&& (len
> 0)) {
924 memcpy(iop
->sensep
, wrk
.buff
, len
);
925 iop
->resp_sense_len
= len
;
927 pout(" >>> Sense buffer, len=%d:\n", (int)len
);
928 dStrHex((const char *)wrk
.buff
, len
, 1);
932 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
933 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status
& 0xff,
934 wrk
.buff
[2] & 0xf, wrk
.buff
[12], wrk
.buff
[13]);
937 pout(" status=0x%x\n", status
);
939 if (iop
->scsi_status
> 0)
943 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
945 return -EIO
; /* give up, assume no device there */
949 /* SCSI command transmission interface function, linux version.
950 * Returns 0 if SCSI command successfully launched and response
951 * received. Even when 0 is returned the caller should check
952 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
953 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
954 * (e.g. device not present or timeout) or some other problem
955 * (e.g. timeout) then returns a negative errno value */
956 static int do_normal_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
,
961 /* implementation relies on static sg_io_state variable. If not
962 * previously set tries the SG_IO ioctl. If that succeeds assume
963 * that SG_IO ioctl functional. If it fails with an errno value
964 * other than ENODEV (no device) or permission then assume
965 * SCSI_IOCTL_SEND_COMMAND is the only option. */
966 switch (sg_io_state
) {
967 case SG_IO_PRESENT_UNKNOWN
:
968 /* ignore report argument */
969 if (0 == (res
= sg_io_cmnd_io(dev_fd
, iop
, report
, 1))) {
970 sg_io_state
= SG_IO_PRESENT_YES
;
972 } else if ((-ENODEV
== res
) || (-EACCES
== res
) || (-EPERM
== res
))
973 return res
; /* wait until we see a device */
974 sg_io_state
= SG_IO_PRESENT_NO
;
975 /* drop through by design */
976 case SG_IO_PRESENT_NO
:
977 return sisc_cmnd_io(dev_fd
, iop
, report
);
978 case SG_IO_PRESENT_YES
:
979 return sg_io_cmnd_io(dev_fd
, iop
, report
, 0);
981 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state
);
982 sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
983 return -EIO
; /* report error and reset state */
987 /* Check and call the right interface. Maybe when the do_generic_scsi_cmd_io interface is better
988 we can take off this crude way of calling the right interface */
989 int do_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
991 switch(con
->controller_type
)
993 case CONTROLLER_CCISS
:
994 #ifdef HAVE_LINUX_CCISS_IOCTL_H
995 return cciss_io_interface(dev_fd
, con
->controller_port
-1, iop
, report
);
998 static int warned
= 0;
1000 pout("CCISS support is not available in this build of smartmontools,\n"
1001 "<linux/cciss_ioctl.h> was not available at build time.\n\n");
1011 return do_normal_scsi_cmnd_io(dev_fd
, iop
, report
);
1017 // >>>>>> End of general SCSI specific linux code
1020 void printwarning(smart_command_set command
);
1023 // This is an interface routine meant to isolate the OS dependent
1024 // parts of the code, and to provide a debugging interface. Each
1025 // different port and OS needs to provide it's own interface. This
1026 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
1027 // commands to be passed through the SCSI driver.
1028 // DETAILED DESCRIPTION OF ARGUMENTS
1029 // fd: is the file descriptor provided by open()
1030 // disknum is the disk number (0 to 15) in the RAID array
1031 // escalade_type indicates the type of controller type, and if scsi or char interface is used
1032 // command: defines the different operations.
1033 // select: additional input data if needed (which log, which type of
1035 // data: location to write output data, if needed (512 bytes).
1036 // Note: not all commands use all arguments.
1038 // -1 if the command failed
1039 // 0 if the command succeeded,
1040 // STATUS_CHECK routine:
1041 // -1 if the command failed
1042 // 0 if the command succeeded and disk SMART status is "OK"
1043 // 1 if the command succeeded and disk SMART status is "FAILING"
1046 /* 512 is the max payload size: increase if needed */
1047 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
1048 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
1049 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
1050 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
1052 int escalade_command_interface(int fd
, int disknum
, int escalade_type
, smart_command_set command
, int select
, char *data
){
1054 // return value and buffer for ioctl()
1055 int ioctlreturn
, readdata
=0;
1057 // Used by both the SCSI and char interfaces
1058 TW_Passthru
*passthru
=NULL
;
1059 char ioctl_buffer
[TW_IOCTL_BUFFER_SIZE
];
1061 // only used for SCSI device interface
1062 TW_Ioctl
*tw_ioctl
=NULL
;
1063 TW_Output
*tw_output
=NULL
;
1065 // only used for 6000/7000/8000 char device interface
1066 TW_New_Ioctl
*tw_ioctl_char
=NULL
;
1068 // only used for 9000 character device interface
1069 TW_Ioctl_Buf_Apache
*tw_ioctl_apache
=NULL
;
1071 memset(ioctl_buffer
, 0, TW_IOCTL_BUFFER_SIZE
);
1073 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
) {
1074 tw_ioctl_apache
= (TW_Ioctl_Buf_Apache
*)ioctl_buffer
;
1075 tw_ioctl_apache
->driver_command
.control_code
= TW_IOCTL_FIRMWARE_PASS_THROUGH
;
1076 tw_ioctl_apache
->driver_command
.buffer_length
= 512; /* payload size */
1077 passthru
= (TW_Passthru
*)&(tw_ioctl_apache
->firmware_command
.command
.oldcommand
);
1079 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
) {
1080 tw_ioctl_char
= (TW_New_Ioctl
*)ioctl_buffer
;
1081 tw_ioctl_char
->data_buffer_length
= 512;
1082 passthru
= (TW_Passthru
*)&(tw_ioctl_char
->firmware_command
);
1084 else if (escalade_type
==CONTROLLER_3WARE_678K
) {
1085 tw_ioctl
= (TW_Ioctl
*)ioctl_buffer
;
1086 tw_ioctl
->cdb
[0] = TW_IOCTL
;
1087 tw_ioctl
->opcode
= TW_ATA_PASSTHRU
;
1088 tw_ioctl
->input_length
= 512; // correct even for non-data commands
1089 tw_ioctl
->output_length
= 512; // correct even for non-data commands
1090 tw_output
= (TW_Output
*)tw_ioctl
;
1091 passthru
= (TW_Passthru
*)&(tw_ioctl
->input_data
);
1094 pout("Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1095 "Please contact " PACKAGE_BUGREPORT
"\n", escalade_type
, disknum
);
1100 // Same for (almost) all commands - but some reset below
1101 passthru
->byte0
.opcode
= TW_OP_ATA_PASSTHRU
;
1102 passthru
->request_id
= 0xFF;
1103 passthru
->unit
= disknum
;
1104 passthru
->status
= 0;
1105 passthru
->flags
= 0x1;
1106 passthru
->drive_head
= 0x0;
1107 passthru
->sector_num
= 0;
1109 // All SMART commands use this CL/CH signature. These are magic
1110 // values from the ATA specifications.
1111 passthru
->cylinder_lo
= 0x4F;
1112 passthru
->cylinder_hi
= 0xC2;
1114 // SMART ATA COMMAND REGISTER value
1115 passthru
->command
= ATA_SMART_CMD
;
1117 // Is this a command that reads or returns 512 bytes?
1118 // passthru->param values are:
1119 // 0x0 - non data command without TFR write check,
1120 // 0x8 - non data command with TFR write check,
1121 // 0xD - data command that returns data to host from device
1122 // 0xF - data command that writes data from host to device
1123 // passthru->size values are 0x5 for non-data and 0x07 for data
1124 if (command
== READ_VALUES
||
1125 command
== READ_THRESHOLDS
||
1126 command
== READ_LOG
||
1127 command
== IDENTIFY
||
1128 command
== WRITE_LOG
) {
1130 passthru
->byte0
.sgloff
= 0x5;
1131 passthru
->size
= 0x7;
1132 passthru
->param
= 0xD;
1133 passthru
->sector_count
= 0x1;
1134 // For 64-bit to work correctly, up the size of the command packet
1135 // in dwords by 1 to account for the 64-bit single sgl 'address'
1136 // field. Note that this doesn't agree with the typedefs but it's
1137 // right (agree with kernel driver behavior/typedefs).
1138 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
&& sizeof(long)==8)
1142 // Non data command -- but doesn't use large sector
1143 // count register values.
1144 passthru
->byte0
.sgloff
= 0x0;
1145 passthru
->size
= 0x5;
1146 passthru
->param
= 0x8;
1147 passthru
->sector_count
= 0x0;
1150 // Now set ATA registers depending upon command
1152 case CHECK_POWER_MODE
:
1153 passthru
->command
= ATA_CHECK_POWER_MODE
;
1154 passthru
->features
= 0;
1155 passthru
->cylinder_lo
= 0;
1156 passthru
->cylinder_hi
= 0;
1159 passthru
->features
= ATA_SMART_READ_VALUES
;
1161 case READ_THRESHOLDS
:
1162 passthru
->features
= ATA_SMART_READ_THRESHOLDS
;
1165 passthru
->features
= ATA_SMART_READ_LOG_SECTOR
;
1166 // log number to return
1167 passthru
->sector_num
= select
;
1170 if (escalade_type
== CONTROLLER_3WARE_9000_CHAR
)
1171 memcpy((unsigned char *)tw_ioctl_apache
->data_buffer
, data
, 512);
1172 else if (escalade_type
== CONTROLLER_3WARE_678K_CHAR
)
1173 memcpy((unsigned char *)tw_ioctl_char
->data_buffer
, data
, 512);
1175 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1176 // memcpy(tw_output->output_data, data, 512);
1177 printwarning(command
);
1182 passthru
->features
= ATA_SMART_WRITE_LOG_SECTOR
;
1183 passthru
->sector_count
= 1;
1184 passthru
->sector_num
= select
;
1185 passthru
->param
= 0xF; // PIO data write
1188 // ATA IDENTIFY DEVICE
1189 passthru
->command
= ATA_IDENTIFY_DEVICE
;
1190 passthru
->features
= 0;
1191 passthru
->cylinder_lo
= 0;
1192 passthru
->cylinder_hi
= 0;
1195 // 3WARE controller can NOT have packet device internally
1196 pout("WARNING - NO DEVICE FOUND ON 3WARE CONTROLLER (disk %d)\n", disknum
);
1197 pout("Note: /dev/sdX many need to be replaced with /dev/tweN or /dev/twaN\n");
1201 passthru
->features
= ATA_SMART_ENABLE
;
1204 passthru
->features
= ATA_SMART_DISABLE
;
1207 passthru
->features
= ATA_SMART_AUTO_OFFLINE
;
1208 // Enable or disable?
1209 passthru
->sector_count
= select
;
1212 passthru
->features
= ATA_SMART_AUTOSAVE
;
1213 // Enable or disable?
1214 passthru
->sector_count
= select
;
1216 case IMMEDIATE_OFFLINE
:
1217 passthru
->features
= ATA_SMART_IMMEDIATE_OFFLINE
;
1218 // What test type to run?
1219 passthru
->sector_num
= select
;
1222 passthru
->features
= ATA_SMART_STATUS
;
1225 // This is JUST to see if SMART is enabled, by giving SMART status
1226 // command. But it doesn't say if status was good, or failing.
1227 // See below for the difference.
1228 passthru
->features
= ATA_SMART_STATUS
;
1231 pout("Unrecognized command %d in linux_3ware_command_interface(disk %d)\n"
1232 "Please contact " PACKAGE_BUGREPORT
"\n", command
, disknum
);
1237 // Now send the command down through an ioctl()
1238 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1239 ioctlreturn
=ioctl(fd
, TW_IOCTL_FIRMWARE_PASS_THROUGH
, tw_ioctl_apache
);
1240 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1241 ioctlreturn
=ioctl(fd
, TW_CMD_PACKET_WITH_DATA
, tw_ioctl_char
);
1243 ioctlreturn
=ioctl(fd
, SCSI_IOCTL_SEND_COMMAND
, tw_ioctl
);
1245 // Deal with the different error cases
1247 if (CONTROLLER_3WARE_678K
==escalade_type
&& ((command
==AUTO_OFFLINE
|| command
==AUTOSAVE
) && select
)){
1248 // error here is probably a kernel driver whose version is too old
1249 printwarning(command
);
1257 // The passthru structure is valid after return from an ioctl if:
1258 // - we are using the character interface OR
1259 // - we are using the SCSI interface and this is a NON-READ-DATA command
1260 // For SCSI interface, note that we set passthru to a different
1261 // value after ioctl().
1262 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1266 passthru
=(TW_Passthru
*)&(tw_output
->output_data
);
1269 // See if the ATA command failed. Now that we have returned from
1270 // the ioctl() call, if passthru is valid, then:
1271 // - passthru->status contains the 3ware controller STATUS
1272 // - passthru->command contains the ATA STATUS register
1273 // - passthru->features contains the ATA ERROR register
1275 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1276 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1277 // While we *might* decode the ATA ERROR register, at the moment it
1278 // doesn't make much sense: we don't care in detail why the error
1281 if (passthru
&& (passthru
->status
|| (passthru
->command
& 0x21))) {
1286 // If this is a read data command, copy data to output buffer
1288 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1289 memcpy(data
, (unsigned char *)tw_ioctl_apache
->data_buffer
, 512);
1290 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1291 memcpy(data
, (unsigned char *)tw_ioctl_char
->data_buffer
, 512);
1293 memcpy(data
, tw_output
->output_data
, 512);
1296 // For STATUS_CHECK, we need to check register values
1297 if (command
==STATUS_CHECK
) {
1299 // To find out if the SMART RETURN STATUS is good or failing, we
1300 // need to examine the values of the Cylinder Low and Cylinder
1303 unsigned short cyl_lo
=passthru
->cylinder_lo
;
1304 unsigned short cyl_hi
=passthru
->cylinder_hi
;
1306 // If values in Cyl-LO and Cyl-HI are unchanged, SMART status is good.
1307 if (cyl_lo
==0x4F && cyl_hi
==0xC2)
1310 // If values in Cyl-LO and Cyl-HI are as follows, SMART status is FAIL
1311 if (cyl_lo
==0xF4 && cyl_hi
==0x2C)
1314 // Any other values mean that something has gone wrong with the command
1315 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1316 printwarning(command
);
1326 // copy sector count register (one byte!) to return data
1327 if (command
==CHECK_POWER_MODE
)
1328 *data
=*(char *)&(passthru
->sector_count
);
1330 // look for nonexistent devices/ports
1331 if (command
==IDENTIFY
&& !nonempty((unsigned char *)data
, 512)) {
1341 int marvell_command_interface(int device
,
1342 smart_command_set command
,
1352 mvsata_scsi_cmd smart_command
;
1353 unsigned char *buff
= (unsigned char *)&smart_command
.cmd
[6];
1354 // See struct hd_drive_cmd_hdr in hdreg.h
1355 // buff[0]: ATA COMMAND CODE REGISTER
1356 // buff[1]: ATA SECTOR NUMBER REGISTER
1357 // buff[2]: ATA FEATURES REGISTER
1358 // buff[3]: ATA SECTOR COUNT REGISTER
1360 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
1361 memset(&smart_command
, 0, sizeof(smart_command
));
1362 smart_command
.inlen
= 540;
1363 smart_command
.outlen
= 540;
1364 smart_command
.cmd
[0] = 0xC; //Vendor-specific code
1365 smart_command
.cmd
[4] = 6; //command length
1367 buff
[0] = ATA_SMART_CMD
;
1369 case CHECK_POWER_MODE
:
1370 buff
[0]=ATA_CHECK_POWER_MODE
;
1373 buff
[2]=ATA_SMART_READ_VALUES
;
1376 case READ_THRESHOLDS
:
1377 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1378 copydata
=buff
[1]=buff
[3]=1;
1381 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1386 buff
[0]=ATA_IDENTIFY_DEVICE
;
1390 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1394 buff
[2]=ATA_SMART_ENABLE
;
1398 buff
[2]=ATA_SMART_DISABLE
;
1403 // this command only says if SMART is working. It could be
1404 // replaced with STATUS_CHECK below.
1405 buff
[2] = ATA_SMART_STATUS
;
1408 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1409 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1412 buff
[2]=ATA_SMART_AUTOSAVE
;
1413 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1415 case IMMEDIATE_OFFLINE
:
1416 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1420 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command
);
1424 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
1426 // We are now doing the HDIO_DRIVE_CMD type ioctl.
1427 if (ioctl(device
, SCSI_IOCTL_SEND_COMMAND
, (void *)&smart_command
))
1430 if (command
==CHECK_POWER_MODE
) {
1431 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
1432 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
1437 // Always succeed on a SMART status, as a disk that failed returned
1438 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
1439 if (command
== STATUS
)
1441 //Data returned is starting from 0 offset
1442 if (command
== STATUS_CHECK
)
1444 // Cyl low and Cyl high unchanged means "Good SMART status"
1445 if (buff
[4] == 0x4F && buff
[5] == 0xC2)
1447 // These values mean "Bad SMART status"
1448 if (buff
[4] == 0xF4 && buff
[5] == 0x2C)
1450 // We haven't gotten output that makes sense; print out some debugging info
1451 syserror("Error SMART Status command failed");
1452 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT
);
1453 pout("Register values returned from SMART Status command are:\n");
1454 pout("CMD =0x%02x\n",(int)buff
[0]);
1455 pout("FR =0x%02x\n",(int)buff
[1]);
1456 pout("NS =0x%02x\n",(int)buff
[2]);
1457 pout("SC =0x%02x\n",(int)buff
[3]);
1458 pout("CL =0x%02x\n",(int)buff
[4]);
1459 pout("CH =0x%02x\n",(int)buff
[5]);
1460 pout("SEL=0x%02x\n",(int)buff
[6]);
1465 memcpy(data
, buff
, 512);
1469 // this implementation is derived from ata_command_interface with a header
1470 // packing for highpoint linux driver ioctl interface
1472 // ioctl(fd,HPTIO_CTL,buff)
1475 // structure of hpt_buff
1476 // +----+----+----+----+--------------------.....---------------------+
1477 // | 1 | 2 | 3 | 4 | 5 |
1478 // +----+----+----+----+--------------------.....---------------------+
1480 // 1: The target controller [ int ( 4 Bytes ) ]
1481 // 2: The channel of the target controllee [ int ( 4 Bytes ) ]
1482 // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
1483 // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
1484 // 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
1485 // if no pmport device, set to 1 or leave blank
1486 // 5: data [ void * ( var leangth ) ]
1488 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
1490 int highpoint_command_interface(int device
, smart_command_set command
,
1491 int select
, char *data
)
1495 unsigned char hpt_buff
[4*sizeof(int) + STRANGE_BUFFER_LENGTH
];
1496 unsigned int *hpt
= (unsigned int *)hpt_buff
;
1497 unsigned char *buff
= &hpt_buff
[4*sizeof(int)];
1499 const int HDIO_DRIVE_CMD_OFFSET
= 4;
1501 memset(hpt_buff
, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH
);
1502 hpt
[0] = con
->hpt_data
[0]; // controller id
1503 hpt
[1] = con
->hpt_data
[1]; // channel number
1504 hpt
[3] = con
->hpt_data
[2]; // pmport number
1506 buff
[0]=ATA_SMART_CMD
;
1508 case CHECK_POWER_MODE
:
1509 buff
[0]=ATA_CHECK_POWER_MODE
;
1513 buff
[2]=ATA_SMART_READ_VALUES
;
1517 case READ_THRESHOLDS
:
1518 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1523 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1531 buff
[0]=ATA_IDENTIFY_DEVICE
;
1536 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1541 buff
[2]=ATA_SMART_ENABLE
;
1545 buff
[2]=ATA_SMART_DISABLE
;
1549 buff
[2]=ATA_SMART_STATUS
;
1552 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1556 buff
[2]=ATA_SMART_AUTOSAVE
;
1559 case IMMEDIATE_OFFLINE
:
1560 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1564 buff
[1]=ATA_SMART_STATUS
;
1567 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
1568 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
1573 if (command
==WRITE_LOG
) {
1574 unsigned char task
[4*sizeof(int)+sizeof(ide_task_request_t
)+512];
1575 unsigned int *hpt
= (unsigned int *)task
;
1576 ide_task_request_t
*reqtask
= (ide_task_request_t
*)(&task
[4*sizeof(int)]);
1577 task_struct_t
*taskfile
= (task_struct_t
*)reqtask
->io_ports
;
1580 memset(task
, 0, sizeof(task
));
1582 hpt
[0] = con
->hpt_data
[0]; // controller id
1583 hpt
[1] = con
->hpt_data
[1]; // channel number
1584 hpt
[3] = con
->hpt_data
[2]; // pmport number
1585 hpt
[2] = HDIO_DRIVE_TASKFILE
; // real hd ioctl
1588 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
1589 taskfile
->sector_count
= 1;
1590 taskfile
->sector_number
= select
;
1591 taskfile
->low_cylinder
= 0x4f;
1592 taskfile
->high_cylinder
= 0xc2;
1593 taskfile
->device_head
= 0;
1594 taskfile
->command
= ATA_SMART_CMD
;
1596 reqtask
->data_phase
= TASKFILE_OUT
;
1597 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
1598 reqtask
->out_size
= 512;
1599 reqtask
->in_size
= 0;
1601 memcpy(task
+sizeof(ide_task_request_t
)+4*sizeof(int), data
, 512);
1603 if ((retval
=ioctl(device
, HPTIO_CTL
, task
))) {
1604 if (retval
==-EINVAL
)
1605 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
1611 if (command
==STATUS_CHECK
){
1613 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
1614 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
1618 hpt
[2] = HDIO_DRIVE_TASK
;
1620 if ((retval
=ioctl(device
, HPTIO_CTL
, hpt_buff
))) {
1621 if (retval
==-EINVAL
) {
1622 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
1623 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
1626 syserror("Error SMART Status command failed");
1630 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
1633 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
1636 syserror("Error SMART Status command failed");
1637 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
1638 pout("Register values returned from SMART Status command are:\n");
1639 pout("CMD=0x%02x\n",(int)buff
[0]);
1640 pout("FR =0x%02x\n",(int)buff
[1]);
1641 pout("NS =0x%02x\n",(int)buff
[2]);
1642 pout("SC =0x%02x\n",(int)buff
[3]);
1643 pout("CL =0x%02x\n",(int)buff
[4]);
1644 pout("CH =0x%02x\n",(int)buff
[5]);
1645 pout("SEL=0x%02x\n",(int)buff
[6]);
1650 if (command
==IDENTIFY
|| command
==PIDENTIFY
) {
1651 unsigned char deviceid
[4*sizeof(int)+512*sizeof(char)];
1652 unsigned int *hpt
= (unsigned int *)deviceid
;
1654 hpt
[0] = con
->hpt_data
[0]; // controller id
1655 hpt
[1] = con
->hpt_data
[1]; // channel number
1656 hpt
[3] = con
->hpt_data
[2]; // pmport number
1658 hpt
[2] = HDIO_GET_IDENTITY
;
1659 if (!ioctl(device
, HPTIO_CTL
, deviceid
) && (deviceid
[4*sizeof(int)] & 0x8000))
1660 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
1664 hpt
[2] = HDIO_DRIVE_CMD
;
1665 if ((ioctl(device
, HPTIO_CTL
, hpt_buff
)))
1668 if (command
==CHECK_POWER_MODE
)
1669 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
1672 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
1678 // Utility function for printing warnings
1679 void printwarning(smart_command_set command
){
1680 static int printed
[4]={0,0,0,0};
1681 const char* message
=
1682 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
1683 "applying a simple 3w-xxxx driver patch that can be found here:\n"
1684 PACKAGE_HOMEPAGE
"\n"
1685 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
1687 if (command
==AUTO_OFFLINE
&& !printed
[0]) {
1689 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message
);
1691 else if (command
==AUTOSAVE
&& !printed
[1]) {
1693 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message
);
1695 else if (command
==STATUS_CHECK
&& !printed
[2]) {
1697 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message
);
1699 else if (command
==WRITE_LOG
&& !printed
[3]) {
1701 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
1707 // Guess device type (ata or scsi) based on device name (Linux
1708 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
1709 // osst, nosst and sg.
1710 static const char * lin_dev_prefix
= "/dev/";
1711 static const char * lin_dev_ata_disk_plus
= "h";
1712 static const char * lin_dev_ata_devfs_disk_plus
= "ide/";
1713 static const char * lin_dev_scsi_devfs_disk_plus
= "scsi/";
1714 static const char * lin_dev_scsi_disk_plus
= "s";
1715 static const char * lin_dev_scsi_tape1
= "ns";
1716 static const char * lin_dev_scsi_tape2
= "os";
1717 static const char * lin_dev_scsi_tape3
= "nos";
1718 static const char * lin_dev_3ware_9000_char
= "twa";
1719 static const char * lin_dev_3ware_678k_char
= "twe";
1720 static const char * lin_dev_cciss_dir
= "cciss/";
1722 int guess_device_type(const char * dev_name
) {
1724 int dev_prefix_len
= strlen(lin_dev_prefix
);
1726 // if dev_name null, or string length zero
1727 if (!dev_name
|| !(len
= strlen(dev_name
)))
1728 return CONTROLLER_UNKNOWN
;
1730 // Remove the leading /dev/... if it's there
1731 if (!strncmp(lin_dev_prefix
, dev_name
, dev_prefix_len
)) {
1732 if (len
<= dev_prefix_len
)
1733 // if nothing else in the string, unrecognized
1734 return CONTROLLER_UNKNOWN
;
1735 // else advance pointer to following characters
1736 dev_name
+= dev_prefix_len
;
1739 // form /dev/h* or h*
1740 if (!strncmp(lin_dev_ata_disk_plus
, dev_name
,
1741 strlen(lin_dev_ata_disk_plus
)))
1742 return CONTROLLER_ATA
;
1744 // form /dev/ide/* or ide/*
1745 if (!strncmp(lin_dev_ata_devfs_disk_plus
, dev_name
,
1746 strlen(lin_dev_ata_devfs_disk_plus
)))
1747 return CONTROLLER_ATA
;
1749 // form /dev/s* or s*
1750 if (!strncmp(lin_dev_scsi_disk_plus
, dev_name
,
1751 strlen(lin_dev_scsi_disk_plus
)))
1752 return CONTROLLER_SCSI
;
1754 // form /dev/scsi/* or scsi/*
1755 if (!strncmp(lin_dev_scsi_devfs_disk_plus
, dev_name
,
1756 strlen(lin_dev_scsi_devfs_disk_plus
)))
1757 return CONTROLLER_SCSI
;
1759 // form /dev/ns* or ns*
1760 if (!strncmp(lin_dev_scsi_tape1
, dev_name
,
1761 strlen(lin_dev_scsi_tape1
)))
1762 return CONTROLLER_SCSI
;
1764 // form /dev/os* or os*
1765 if (!strncmp(lin_dev_scsi_tape2
, dev_name
,
1766 strlen(lin_dev_scsi_tape2
)))
1767 return CONTROLLER_SCSI
;
1769 // form /dev/nos* or nos*
1770 if (!strncmp(lin_dev_scsi_tape3
, dev_name
,
1771 strlen(lin_dev_scsi_tape3
)))
1772 return CONTROLLER_SCSI
;
1775 if (!strncmp(lin_dev_3ware_9000_char
, dev_name
,
1776 strlen(lin_dev_3ware_9000_char
)))
1777 return CONTROLLER_3WARE_9000_CHAR
;
1780 if (!strncmp(lin_dev_3ware_678k_char
, dev_name
,
1781 strlen(lin_dev_3ware_678k_char
)))
1782 return CONTROLLER_3WARE_678K_CHAR
;
1784 if (!strncmp(lin_dev_cciss_dir
, dev_name
,
1785 strlen(lin_dev_cciss_dir
)))
1786 return CONTROLLER_CCISS
;
1788 // we failed to recognize any of the forms
1789 return CONTROLLER_UNKNOWN
;
1795 [ed@firestorm ed
]$ ls
-l
/dev
/discs
1797 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc0
-> ../ide
/host2
/bus0
/target0
/lun0
/
1798 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc1
-> ../ide
/host2
/bus1
/target0
/lun0
/
1799 [ed@firestorm ed
]$ ls
-l dev
/ide
/host*/bus*/target*/lun*/disc
1800 ls
: dev
/ide
/host*/bus*/target*/lun*/disc
: No such file
or directory
1801 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/host*/bus*/target*/lun*/disc
1802 brw
------- 1 root root
33, 0 Dec
31 1969 /dev
/ide
/host2
/bus0
/target0
/lun0
/disc
1803 brw
------- 1 root root
34, 0 Dec
31 1969 /dev
/ide
/host2
/bus1
/target0
/lun0
/disc
1804 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/c
*b
*t
*u
*
1805 ls
: /dev
/ide
/c
*b
*t
*u
*: No such file
or directory
1807 Script done on Fri Nov
7 13:46:28 2003