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.97 2007/09/06 08:48:55 ballen4705 Exp $";
84 const char *os_XXXX_c_cvsid
="$Id: os_linux.cpp,v 1.97 2007/09/06 08:48:55 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 // equivalent to open(path, flags)
171 int deviceopen(const char *pathname
, char *type
){
172 if (!strcmp(type
,"SCSI")) {
173 int fd
= open(pathname
, O_RDWR
| O_NONBLOCK
);
174 if (fd
< 0 && errno
== EROFS
)
175 fd
= open(pathname
, O_RDONLY
| O_NONBLOCK
);
178 else if (!strcmp(type
,"ATA"))
179 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
180 else if (!strcmp(type
,"ATA_3WARE_9000")) {
181 // the device nodes for this controller are dynamically assigned,
182 // so we need to check that they exist with the correct major
183 // numbers and if not, create them
184 if (setup_3ware_nodes("twa", "3w-9xxx")) {
189 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
191 else if (!strcmp(type
,"ATA_3WARE_678K")) {
192 // the device nodes for this controller are dynamically assigned,
193 // so we need to check that they exist with the correct major
194 // numbers and if not, create them
195 if (setup_3ware_nodes("twe", "3w-xxxx")) {
200 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
202 else if(!strcmp(type
, "CCISS")) {
203 // the device is a cciss smart array device.
204 return open(pathname
, O_RDWR
| O_NONBLOCK
);
211 // equivalent to close(file descriptor)
212 int deviceclose(int fd
){
216 // print examples for smartctl
217 void print_smartctl_examples(){
218 printf("=================================================== SMARTCTL EXAMPLES =====\n\n");
219 #ifdef HAVE_GETOPT_LONG
221 " smartctl --all /dev/hda (Prints all SMART information)\n\n"
222 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n"
223 " (Enables SMART on first disk)\n\n"
224 " smartctl --test=long /dev/hda (Executes extended disk self-test)\n\n"
225 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/hda\n"
226 " (Prints Self-Test & Attribute errors)\n"
227 " smartctl --all --device=3ware,2 /dev/sda\n"
228 " smartctl --all --device=3ware,2 /dev/twe0\n"
229 " smartctl --all --device=3ware,2 /dev/twa0\n"
230 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
231 " smartctl --all --device=hpt,1/1/3 /dev/sda\n"
232 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
233 " of the 1st channel on the 1st HighPoint RAID controller)\n"
237 " smartctl -a /dev/hda (Prints all SMART information)\n"
238 " smartctl -s on -o on -S on /dev/hda (Enables SMART on first disk)\n"
239 " smartctl -t long /dev/hda (Executes extended disk self-test)\n"
240 " smartctl -A -l selftest -q errorsonly /dev/hda\n"
241 " (Prints Self-Test & Attribute errors)\n"
242 " smartctl -a -d 3ware,2 /dev/sda\n"
243 " smartctl -a -d 3ware,2 /dev/twa0\n"
244 " smartctl -a -d 3ware,2 /dev/twe0\n"
245 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
246 " smartctl -a -d hpt,1/1/3 /dev/sda\n"
247 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
248 " of the 1st channel on the 1st HighPoint RAID controller)\n"
255 // we are going to take advantage of the fact that Linux's devfs will only
256 // have device entries for devices that exist. So if we get the equivalent of
257 // ls /dev/hd[a-t], we have all the ATA devices on the system
259 // If any errors occur, leave errno set as it was returned by the
260 // system call, and return <0.
261 int get_dev_names(char*** names
, const char* pattern
, const char* name
, int max
) {
262 int n
= 0, retglob
, i
, lim
;
266 memset(&globbuf
, 0, sizeof(globbuf
));
268 // in case of non-clean exit
271 // Use glob to look for any directory entries matching the pattern
272 if ((retglob
=glob(pattern
, GLOB_ERR
, NULL
, &globbuf
))) {
274 // glob failed: free memory and return
277 if (retglob
==GLOB_NOMATCH
){
278 pout("glob(3) found no matches for pattern %s\n", pattern
);
282 if (retglob
==GLOB_NOSPACE
)
283 pout("glob(3) ran out of memory matching pattern %s\n", pattern
);
284 #ifdef GLOB_ABORTED // missing in old versions of glob.h
285 else if (retglob
==GLOB_ABORTED
)
286 pout("glob(3) aborted matching pattern %s\n", pattern
);
289 pout("Unexplained error in glob(3) of pattern %s\n", pattern
);
294 // did we find too many paths?
295 lim
= ((int)globbuf
.gl_pathc
< max
) ? (int)globbuf
.gl_pathc
: max
;
296 if (lim
< (int)globbuf
.gl_pathc
)
297 pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n",
298 (int)globbuf
.gl_pathc
, max
, pattern
, (int)(globbuf
.gl_pathc
-max
));
300 // allocate space for up to lim number of ATA devices
301 if (!(mp
= (char **)calloc(lim
, sizeof(char*)))){
302 pout("Out of memory constructing scan device list\n");
306 // now step through the list returned by glob. If not a link, copy
307 // to list. If it is a link, evaluate it and see if the path ends
309 for (i
=0; i
<lim
; i
++){
312 // prepare a buffer for storing the link
315 // see if path is a link
316 retlink
=readlink(globbuf
.gl_pathv
[i
], linkbuf
, 1023);
318 // if not a link (or a strange link), keep it
319 if (retlink
<=0 || retlink
>1023)
320 mp
[n
++] = CustomStrDup(globbuf
.gl_pathv
[i
], 1, __LINE__
, filenameandversion
);
322 // or if it's a link that points to a disc, follow it
324 linkbuf
[retlink
]='\0';
325 if ((p
=strrchr(linkbuf
,'/')) && !strcmp(p
+1, "disc"))
326 // This is the branch of the code that gets followed if we are
327 // using devfs WITH traditional compatibility links. In this
328 // case, we add the traditional device name to the list that
330 mp
[n
++] = CustomStrDup(globbuf
.gl_pathv
[i
], 1, __LINE__
, filenameandversion
);
332 // This is the branch of the code that gets followed if we are
333 // using devfs WITHOUT traditional compatibility links. In
334 // this case, we check that the link to the directory is of
335 // the correct type, and then append "disc" to it.
336 char tmpname
[1024]={0};
337 const char * type
= (strcmp(name
,"ATA") ? "scsi" : "ide");
338 if (strstr(linkbuf
, type
)){
339 snprintf(tmpname
, 1024, "%s/disc", globbuf
.gl_pathv
[i
]);
340 mp
[n
++] = CustomStrDup(tmpname
, 1, __LINE__
, filenameandversion
);
346 // free memory, track memory usage
348 mp
= static_cast<char **>(realloc(mp
,n
*(sizeof(char*))));
349 bytes
+= n
*(sizeof(char*));
351 // and set up return values
356 // makes a list of device names to scan, for either ATA or SCSI
357 // devices. Return -1 if no memory remaining, else the number of
358 // devices on the list, which can be >=0.
359 int make_device_names (char*** devlist
, const char* name
) {
363 // for testing case where no device names are found
367 if (!strcmp(name
,"SCSI"))
368 retval
=get_dev_names(devlist
,"/dev/sd[a-z]", name
, maxdev
=26);
369 else if (!strcmp(name
,"ATA"))
370 retval
=get_dev_names(devlist
,"/dev/hd[a-t]", name
, maxdev
=20);
372 // don't recognize disk type!
375 // if we found traditional links, we are done
379 // else look for devfs entries without traditional links
380 return get_dev_names(devlist
,"/dev/discs/disc*", name
, maxdev
);
385 // This is an interface routine meant to isolate the OS dependent
386 // parts of the code, and to provide a debugging interface. Each
387 // different port and OS needs to provide it's own interface. This
389 // DETAILED DESCRIPTION OF ARGUMENTS
390 // device: is the file descriptor provided by open()
391 // command: defines the different operations.
392 // select: additional input data if needed (which log, which type of
394 // data: location to write output data, if needed (512 bytes).
395 // Note: not all commands use all arguments.
397 // -1 if the command failed
398 // 0 if the command succeeded,
399 // STATUS_CHECK routine:
400 // -1 if the command failed
401 // 0 if the command succeeded and disk SMART status is "OK"
402 // 1 if the command succeeded and disk SMART status is "FAILING"
405 #define BUFFER_LENGTH (4+512)
407 int ata_command_interface(int device
, smart_command_set command
, int select
, char *data
){
408 unsigned char buff
[BUFFER_LENGTH
];
409 // positive: bytes to write to caller. negative: bytes to READ from
410 // caller. zero: non-data command
413 const int HDIO_DRIVE_CMD_OFFSET
= 4;
415 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
416 // buff[0]: ATA COMMAND CODE REGISTER
417 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
418 // buff[2]: ATA FEATURES REGISTER
419 // buff[3]: ATA SECTOR COUNT REGISTER
421 // Note that on return:
422 // buff[2] contains the ATA SECTOR COUNT REGISTER
424 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
425 memset(buff
, 0, BUFFER_LENGTH
);
427 buff
[0]=ATA_SMART_CMD
;
429 case CHECK_POWER_MODE
:
430 buff
[0]=ATA_CHECK_POWER_MODE
;
434 buff
[2]=ATA_SMART_READ_VALUES
;
438 case READ_THRESHOLDS
:
439 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
444 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
452 buff
[0]=ATA_IDENTIFY_DEVICE
;
457 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
462 buff
[2]=ATA_SMART_ENABLE
;
466 buff
[2]=ATA_SMART_DISABLE
;
470 // this command only says if SMART is working. It could be
471 // replaced with STATUS_CHECK below.
472 buff
[2]=ATA_SMART_STATUS
;
475 // NSECT is 241 for enable but no data transfer. Use TASK ioctl.
476 buff
[1]=ATA_SMART_AUTO_OFFLINE
;
480 // NSECT is 248 for enable but no data transfer. Use TASK ioctl.
481 buff
[1]=ATA_SMART_AUTOSAVE
;
484 case IMMEDIATE_OFFLINE
:
485 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
489 // This command uses HDIO_DRIVE_TASK and has different syntax than
490 // the other commands.
491 buff
[1]=ATA_SMART_STATUS
;
494 pout("Unrecognized command %d in linux_ata_command_interface()\n"
495 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
500 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
501 // only ioctl() that can be used to WRITE data to the disk.
502 if (command
==WRITE_LOG
) {
503 unsigned char task
[sizeof(ide_task_request_t
)+512];
504 ide_task_request_t
*reqtask
=(ide_task_request_t
*) task
;
505 task_struct_t
*taskfile
=(task_struct_t
*) reqtask
->io_ports
;
508 memset(task
, 0, sizeof(task
));
511 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
512 taskfile
->sector_count
= 1;
513 taskfile
->sector_number
= select
;
514 taskfile
->low_cylinder
= 0x4f;
515 taskfile
->high_cylinder
= 0xc2;
516 taskfile
->device_head
= 0;
517 taskfile
->command
= ATA_SMART_CMD
;
519 reqtask
->data_phase
= TASKFILE_OUT
;
520 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
521 reqtask
->out_size
= 512;
522 reqtask
->in_size
= 0;
524 // copy user data into the task request structure
525 memcpy(task
+sizeof(ide_task_request_t
), data
, 512);
527 if ((retval
=ioctl(device
, HDIO_DRIVE_TASKFILE
, task
))) {
529 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
535 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
537 if (command
==STATUS_CHECK
|| command
==AUTOSAVE
|| command
==AUTO_OFFLINE
){
540 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
541 // have to read the IDE driver source code. Sigh.
542 // buff[0]: ATA COMMAND CODE REGISTER
543 // buff[1]: ATA FEATURES REGISTER
544 // buff[2]: ATA SECTOR_COUNT
545 // buff[3]: ATA SECTOR NUMBER
546 // buff[4]: ATA CYL LO REGISTER
547 // buff[5]: ATA CYL HI REGISTER
548 // buff[6]: ATA DEVICE HEAD
550 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
551 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
555 if ((retval
=ioctl(device
, HDIO_DRIVE_TASK
, buff
))) {
556 if (retval
==-EINVAL
) {
557 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
558 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
561 syserror("Error SMART Status command failed");
565 // Cyl low and Cyl high unchanged means "Good SMART status"
566 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
569 // These values mean "Bad SMART status"
570 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
573 // We haven't gotten output that makes sense; print out some debugging info
574 syserror("Error SMART Status command failed");
575 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
576 pout("Register values returned from SMART Status command are:\n");
577 pout("ST =0x%02x\n",(int)buff
[0]);
578 pout("ERR=0x%02x\n",(int)buff
[1]);
579 pout("NS =0x%02x\n",(int)buff
[2]);
580 pout("SC =0x%02x\n",(int)buff
[3]);
581 pout("CL =0x%02x\n",(int)buff
[4]);
582 pout("CH =0x%02x\n",(int)buff
[5]);
583 pout("SEL=0x%02x\n",(int)buff
[6]);
588 // Note to people doing ports to other OSes -- don't worry about
589 // this block -- you can safely ignore it. I have put it here
590 // because under linux when you do IDENTIFY DEVICE to a packet
591 // device, it generates an ugly kernel syslog error message. This
592 // is harmless but frightens users. So this block detects packet
593 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
596 // If you read only the ATA specs, it appears as if a packet device
597 // *might* respond to the IDENTIFY DEVICE command. This is
598 // misleading - it's because around the time that SFF-8020 was
599 // incorporated into the ATA-3/4 standard, the ATA authors were
600 // sloppy. See SFF-8020 and you will see that ATAPI devices have
601 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
602 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
603 if (command
==IDENTIFY
|| command
==PIDENTIFY
){
604 unsigned short deviceid
[256];
605 // check the device identity, as seen when the system was booted
606 // or the device was FIRST registered. This will not be current
607 // if the user has subsequently changed some of the parameters. If
608 // device is a packet device, swap the command interpretations.
609 if (!ioctl(device
, HDIO_GET_IDENTITY
, deviceid
) && (deviceid
[0] & 0x8000))
610 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
614 // We are now doing the HDIO_DRIVE_CMD type ioctl.
615 if ((ioctl(device
, HDIO_DRIVE_CMD
, buff
)))
618 // CHECK POWER MODE command returns information in the Sector Count
619 // register (buff[3]). Copy to return data buffer.
620 if (command
==CHECK_POWER_MODE
)
621 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
623 // if the command returns data then copy it back
625 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
630 // >>>>>> Start of general SCSI specific linux code
632 /* Linux specific code.
633 * Historically smartmontools (and smartsuite before it) used the
634 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
635 * nodes that use the SCSI subsystem. A better interface has been available
636 * via the SCSI generic (sg) driver but this involves the extra step of
637 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
638 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
639 * the sg driver have become available via the SG_IO ioctl which is available
640 * on all SCSI devices (on SCSI tape devices from lk 2.6.6).
641 * So the strategy below is to find out if the SG_IO ioctl is available and
642 * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl.
643 * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */
645 #define MAX_DXFER_LEN 1024 /* can be increased if necessary */
646 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
647 #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
648 #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
649 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
650 #define LSCSI_DRIVER_TIMEOUT 0x6
651 #define LSCSI_DID_TIME_OUT 0x3
652 #define LSCSI_DID_BUS_BUSY 0x2
653 #define LSCSI_DID_NO_CONNECT 0x1
655 #ifndef SCSI_IOCTL_SEND_COMMAND
656 #define SCSI_IOCTL_SEND_COMMAND 1
659 #define SG_IO_PRESENT_UNKNOWN 0
660 #define SG_IO_PRESENT_YES 1
661 #define SG_IO_PRESENT_NO 2
663 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
665 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
);
667 static int sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
669 /* Preferred implementation for issuing SCSI commands in linux. This
670 * function uses the SG_IO ioctl. Return 0 if command issued successfully
671 * (various status values should still be checked). If the SCSI command
672 * cannot be issued then a negative errno value is returned. */
673 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
677 ARGUSED(dev_fd
); ARGUSED(iop
); ARGUSED(report
);
680 struct sg_io_hdr io_hdr
;
684 const unsigned char * ucp
= iop
->cmnd
;
687 const int sz
= (int)sizeof(buff
);
689 np
= scsi_get_opcode_name(ucp
[0]);
690 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
691 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
692 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
694 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
695 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
697 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
698 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
699 (trunc
? " [only first 256 bytes shown]" : ""));
700 dStrHex((const char *)iop
->dxferp
,
701 (trunc
? 256 : iop
->dxfer_len
) , 1);
704 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
707 memset(&io_hdr
, 0, sizeof(struct sg_io_hdr
));
708 io_hdr
.interface_id
= 'S';
709 io_hdr
.cmd_len
= iop
->cmnd_len
;
710 io_hdr
.mx_sb_len
= iop
->max_sense_len
;
711 io_hdr
.dxfer_len
= iop
->dxfer_len
;
712 io_hdr
.dxferp
= iop
->dxferp
;
713 io_hdr
.cmdp
= iop
->cmnd
;
714 io_hdr
.sbp
= iop
->sensep
;
715 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
716 defaults to 60 seconds. */
717 io_hdr
.timeout
= ((0 == iop
->timeout
) ? 60 : iop
->timeout
) * 1000;
718 switch (iop
->dxfer_dir
) {
720 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
722 case DXFER_FROM_DEVICE
:
723 io_hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
725 case DXFER_TO_DEVICE
:
726 io_hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
729 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
732 iop
->resp_sense_len
= 0;
733 iop
->scsi_status
= 0;
735 if (ioctl(dev_fd
, SG_IO
, &io_hdr
) < 0) {
736 if (report
&& (! unknown
))
737 pout(" SG_IO ioctl failed, errno=%d [%s]\n", errno
,
741 iop
->resid
= io_hdr
.resid
;
742 iop
->scsi_status
= io_hdr
.status
;
744 pout(" scsi_status=0x%x, host_status=0x%x, driver_status=0x%x\n"
745 " info=0x%x duration=%d milliseconds resid=%d\n", io_hdr
.status
,
746 io_hdr
.host_status
, io_hdr
.driver_status
, io_hdr
.info
,
747 io_hdr
.duration
, io_hdr
.resid
);
749 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
752 len
= iop
->dxfer_len
- iop
->resid
;
753 trunc
= (len
> 256) ? 1 : 0;
755 pout(" Incoming data, len=%d%s:\n", len
,
756 (trunc
? " [only first 256 bytes shown]" : ""));
757 dStrHex((const char*)iop
->dxferp
, (trunc
? 256 : len
),
760 pout(" Incoming data trimmed to nothing by resid\n");
765 if (io_hdr
.info
| SG_INFO_CHECK
) { /* error or warning */
766 int masked_driver_status
= (LSCSI_DRIVER_MASK
& io_hdr
.driver_status
);
768 if (0 != io_hdr
.host_status
) {
769 if ((LSCSI_DID_NO_CONNECT
== io_hdr
.host_status
) ||
770 (LSCSI_DID_BUS_BUSY
== io_hdr
.host_status
) ||
771 (LSCSI_DID_TIME_OUT
== io_hdr
.host_status
))
774 return -EIO
; /* catch all */
776 if (0 != masked_driver_status
) {
777 if (LSCSI_DRIVER_TIMEOUT
== masked_driver_status
)
779 else if (LSCSI_DRIVER_SENSE
!= masked_driver_status
)
782 if (LSCSI_DRIVER_SENSE
== masked_driver_status
)
783 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
784 iop
->resp_sense_len
= io_hdr
.sb_len_wr
;
785 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
786 iop
->sensep
&& (iop
->resp_sense_len
> 0)) {
788 pout(" >>> Sense buffer, len=%d:\n",
789 (int)iop
->resp_sense_len
);
790 dStrHex((const char *)iop
->sensep
, iop
->resp_sense_len
, 1);
794 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
795 if ((iop
->sensep
[0] & 0x7f) > 0x71)
796 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
797 iop
->scsi_status
, iop
->sensep
[1] & 0xf,
798 iop
->sensep
[2], iop
->sensep
[3]);
800 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
801 iop
->scsi_status
, iop
->sensep
[2] & 0xf,
802 iop
->sensep
[12], iop
->sensep
[13]);
805 pout(" status=0x%x\n", iop
->scsi_status
);
812 struct linux_ioctl_send_command
816 UINT8 buff
[MAX_DXFER_LEN
+ 16];
819 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
820 * support: CDB length (guesses it from opcode), resid and timeout.
821 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
822 * to 2 hours in order to allow long foreground extended self tests. */
823 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
825 struct linux_ioctl_send_command wrk
;
826 int status
, buff_offset
;
829 memcpy(wrk
.buff
, iop
->cmnd
, iop
->cmnd_len
);
830 buff_offset
= iop
->cmnd_len
;
833 const unsigned char * ucp
= iop
->cmnd
;
836 const int sz
= (int)sizeof(buff
);
838 np
= scsi_get_opcode_name(ucp
[0]);
839 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
840 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
841 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
843 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
844 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
846 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
847 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
848 (trunc
? " [only first 256 bytes shown]" : ""));
849 dStrHex((const char *)iop
->dxferp
,
850 (trunc
? 256 : iop
->dxfer_len
) , 1);
853 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
856 switch (iop
->dxfer_dir
) {
861 case DXFER_FROM_DEVICE
:
863 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
865 wrk
.outbufsize
= iop
->dxfer_len
;
867 case DXFER_TO_DEVICE
:
868 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
870 memcpy(wrk
.buff
+ buff_offset
, iop
->dxferp
, iop
->dxfer_len
);
871 wrk
.inbufsize
= iop
->dxfer_len
;
875 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
878 iop
->resp_sense_len
= 0;
879 iop
->scsi_status
= 0;
881 status
= ioctl(dev_fd
, SCSI_IOCTL_SEND_COMMAND
, &wrk
);
884 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
885 errno
, strerror(errno
));
891 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
892 memcpy(iop
->dxferp
, wrk
.buff
, iop
->dxfer_len
);
894 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
896 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
897 (trunc
? " [only first 256 bytes shown]" : ""));
898 dStrHex((const char*)iop
->dxferp
,
899 (trunc
? 256 : iop
->dxfer_len
) , 1);
904 iop
->scsi_status
= status
& 0x7e; /* bits 0 and 7 used to be for vendors */
905 if (LSCSI_DRIVER_SENSE
== ((status
>> 24) & 0xf))
906 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
907 len
= (SEND_IOCTL_RESP_SENSE_LEN
< iop
->max_sense_len
) ?
908 SEND_IOCTL_RESP_SENSE_LEN
: iop
->max_sense_len
;
909 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
910 iop
->sensep
&& (len
> 0)) {
911 memcpy(iop
->sensep
, wrk
.buff
, len
);
912 iop
->resp_sense_len
= len
;
914 pout(" >>> Sense buffer, len=%d:\n", (int)len
);
915 dStrHex((const char *)wrk
.buff
, len
, 1);
919 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
920 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status
& 0xff,
921 wrk
.buff
[2] & 0xf, wrk
.buff
[12], wrk
.buff
[13]);
924 pout(" status=0x%x\n", status
);
926 if (iop
->scsi_status
> 0)
930 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
932 return -EIO
; /* give up, assume no device there */
936 /* SCSI command transmission interface function, linux version.
937 * Returns 0 if SCSI command successfully launched and response
938 * received. Even when 0 is returned the caller should check
939 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
940 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
941 * (e.g. device not present or timeout) or some other problem
942 * (e.g. timeout) then returns a negative errno value */
943 static int do_normal_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
,
948 /* implementation relies on static sg_io_state variable. If not
949 * previously set tries the SG_IO ioctl. If that succeeds assume
950 * that SG_IO ioctl functional. If it fails with an errno value
951 * other than ENODEV (no device) or permission then assume
952 * SCSI_IOCTL_SEND_COMMAND is the only option. */
953 switch (sg_io_state
) {
954 case SG_IO_PRESENT_UNKNOWN
:
955 /* ignore report argument */
956 if (0 == (res
= sg_io_cmnd_io(dev_fd
, iop
, report
, 1))) {
957 sg_io_state
= SG_IO_PRESENT_YES
;
959 } else if ((-ENODEV
== res
) || (-EACCES
== res
) || (-EPERM
== res
))
960 return res
; /* wait until we see a device */
961 sg_io_state
= SG_IO_PRESENT_NO
;
962 /* drop through by design */
963 case SG_IO_PRESENT_NO
:
964 return sisc_cmnd_io(dev_fd
, iop
, report
);
965 case SG_IO_PRESENT_YES
:
966 return sg_io_cmnd_io(dev_fd
, iop
, report
, 0);
968 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state
);
969 sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
970 return -EIO
; /* report error and reset state */
974 /* Check and call the right interface. Maybe when the do_generic_scsi_cmd_io interface is better
975 we can take off this crude way of calling the right interface */
976 int do_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
978 switch(con
->controller_type
)
980 case CONTROLLER_CCISS
:
981 #ifdef HAVE_LINUX_CCISS_IOCTL_H
982 return cciss_io_interface(dev_fd
, con
->controller_port
-1, iop
, report
);
985 static int warned
= 0;
987 pout("CCISS support is not available in this build of smartmontools,\n"
988 "<linux/cciss_ioctl.h> was not available at build time.\n\n");
998 return do_normal_scsi_cmnd_io(dev_fd
, iop
, report
);
1004 // >>>>>> End of general SCSI specific linux code
1007 void printwarning(smart_command_set command
);
1010 // This is an interface routine meant to isolate the OS dependent
1011 // parts of the code, and to provide a debugging interface. Each
1012 // different port and OS needs to provide it's own interface. This
1013 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
1014 // commands to be passed through the SCSI driver.
1015 // DETAILED DESCRIPTION OF ARGUMENTS
1016 // fd: is the file descriptor provided by open()
1017 // disknum is the disk number (0 to 15) in the RAID array
1018 // escalade_type indicates the type of controller type, and if scsi or char interface is used
1019 // command: defines the different operations.
1020 // select: additional input data if needed (which log, which type of
1022 // data: location to write output data, if needed (512 bytes).
1023 // Note: not all commands use all arguments.
1025 // -1 if the command failed
1026 // 0 if the command succeeded,
1027 // STATUS_CHECK routine:
1028 // -1 if the command failed
1029 // 0 if the command succeeded and disk SMART status is "OK"
1030 // 1 if the command succeeded and disk SMART status is "FAILING"
1033 /* 512 is the max payload size: increase if needed */
1034 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
1035 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
1036 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
1037 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
1039 int escalade_command_interface(int fd
, int disknum
, int escalade_type
, smart_command_set command
, int select
, char *data
){
1041 // return value and buffer for ioctl()
1042 int ioctlreturn
, readdata
=0;
1044 // Used by both the SCSI and char interfaces
1045 TW_Passthru
*passthru
=NULL
;
1046 char ioctl_buffer
[TW_IOCTL_BUFFER_SIZE
];
1048 // only used for SCSI device interface
1049 TW_Ioctl
*tw_ioctl
=NULL
;
1050 TW_Output
*tw_output
=NULL
;
1052 // only used for 6000/7000/8000 char device interface
1053 TW_New_Ioctl
*tw_ioctl_char
=NULL
;
1055 // only used for 9000 character device interface
1056 TW_Ioctl_Buf_Apache
*tw_ioctl_apache
=NULL
;
1058 memset(ioctl_buffer
, 0, TW_IOCTL_BUFFER_SIZE
);
1060 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
) {
1061 tw_ioctl_apache
= (TW_Ioctl_Buf_Apache
*)ioctl_buffer
;
1062 tw_ioctl_apache
->driver_command
.control_code
= TW_IOCTL_FIRMWARE_PASS_THROUGH
;
1063 tw_ioctl_apache
->driver_command
.buffer_length
= 512; /* payload size */
1064 passthru
= (TW_Passthru
*)&(tw_ioctl_apache
->firmware_command
.command
.oldcommand
);
1066 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
) {
1067 tw_ioctl_char
= (TW_New_Ioctl
*)ioctl_buffer
;
1068 tw_ioctl_char
->data_buffer_length
= 512;
1069 passthru
= (TW_Passthru
*)&(tw_ioctl_char
->firmware_command
);
1071 else if (escalade_type
==CONTROLLER_3WARE_678K
) {
1072 tw_ioctl
= (TW_Ioctl
*)ioctl_buffer
;
1073 tw_ioctl
->cdb
[0] = TW_IOCTL
;
1074 tw_ioctl
->opcode
= TW_ATA_PASSTHRU
;
1075 tw_ioctl
->input_length
= 512; // correct even for non-data commands
1076 tw_ioctl
->output_length
= 512; // correct even for non-data commands
1077 tw_output
= (TW_Output
*)tw_ioctl
;
1078 passthru
= (TW_Passthru
*)&(tw_ioctl
->input_data
);
1081 pout("Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1082 "Please contact " PACKAGE_BUGREPORT
"\n", escalade_type
, disknum
);
1087 // Same for (almost) all commands - but some reset below
1088 passthru
->byte0
.opcode
= TW_OP_ATA_PASSTHRU
;
1089 passthru
->request_id
= 0xFF;
1090 passthru
->unit
= disknum
;
1091 passthru
->status
= 0;
1092 passthru
->flags
= 0x1;
1093 passthru
->drive_head
= 0x0;
1094 passthru
->sector_num
= 0;
1096 // All SMART commands use this CL/CH signature. These are magic
1097 // values from the ATA specifications.
1098 passthru
->cylinder_lo
= 0x4F;
1099 passthru
->cylinder_hi
= 0xC2;
1101 // SMART ATA COMMAND REGISTER value
1102 passthru
->command
= ATA_SMART_CMD
;
1104 // Is this a command that reads or returns 512 bytes?
1105 // passthru->param values are:
1106 // 0x0 - non data command without TFR write check,
1107 // 0x8 - non data command with TFR write check,
1108 // 0xD - data command that returns data to host from device
1109 // 0xF - data command that writes data from host to device
1110 // passthru->size values are 0x5 for non-data and 0x07 for data
1111 if (command
== READ_VALUES
||
1112 command
== READ_THRESHOLDS
||
1113 command
== READ_LOG
||
1114 command
== IDENTIFY
||
1115 command
== WRITE_LOG
) {
1117 passthru
->byte0
.sgloff
= 0x5;
1118 passthru
->size
= 0x7;
1119 passthru
->param
= 0xD;
1120 passthru
->sector_count
= 0x1;
1121 // For 64-bit to work correctly, up the size of the command packet
1122 // in dwords by 1 to account for the 64-bit single sgl 'address'
1123 // field. Note that this doesn't agree with the typedefs but it's
1124 // right (agree with kernel driver behavior/typedefs).
1125 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
&& sizeof(long)==8)
1129 // Non data command -- but doesn't use large sector
1130 // count register values.
1131 passthru
->byte0
.sgloff
= 0x0;
1132 passthru
->size
= 0x5;
1133 passthru
->param
= 0x8;
1134 passthru
->sector_count
= 0x0;
1137 // Now set ATA registers depending upon command
1139 case CHECK_POWER_MODE
:
1140 passthru
->command
= ATA_CHECK_POWER_MODE
;
1141 passthru
->features
= 0;
1142 passthru
->cylinder_lo
= 0;
1143 passthru
->cylinder_hi
= 0;
1146 passthru
->features
= ATA_SMART_READ_VALUES
;
1148 case READ_THRESHOLDS
:
1149 passthru
->features
= ATA_SMART_READ_THRESHOLDS
;
1152 passthru
->features
= ATA_SMART_READ_LOG_SECTOR
;
1153 // log number to return
1154 passthru
->sector_num
= select
;
1157 if (escalade_type
== CONTROLLER_3WARE_9000_CHAR
)
1158 memcpy((unsigned char *)tw_ioctl_apache
->data_buffer
, data
, 512);
1159 else if (escalade_type
== CONTROLLER_3WARE_678K_CHAR
)
1160 memcpy((unsigned char *)tw_ioctl_char
->data_buffer
, data
, 512);
1162 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1163 // memcpy(tw_output->output_data, data, 512);
1164 printwarning(command
);
1169 passthru
->features
= ATA_SMART_WRITE_LOG_SECTOR
;
1170 passthru
->sector_count
= 1;
1171 passthru
->sector_num
= select
;
1172 passthru
->param
= 0xF; // PIO data write
1175 // ATA IDENTIFY DEVICE
1176 passthru
->command
= ATA_IDENTIFY_DEVICE
;
1177 passthru
->features
= 0;
1178 passthru
->cylinder_lo
= 0;
1179 passthru
->cylinder_hi
= 0;
1182 // 3WARE controller can NOT have packet device internally
1183 pout("WARNING - NO DEVICE FOUND ON 3WARE CONTROLLER (disk %d)\n", disknum
);
1184 pout("Note: /dev/sdX many need to be replaced with /dev/tweN or /dev/twaN\n");
1188 passthru
->features
= ATA_SMART_ENABLE
;
1191 passthru
->features
= ATA_SMART_DISABLE
;
1194 passthru
->features
= ATA_SMART_AUTO_OFFLINE
;
1195 // Enable or disable?
1196 passthru
->sector_count
= select
;
1199 passthru
->features
= ATA_SMART_AUTOSAVE
;
1200 // Enable or disable?
1201 passthru
->sector_count
= select
;
1203 case IMMEDIATE_OFFLINE
:
1204 passthru
->features
= ATA_SMART_IMMEDIATE_OFFLINE
;
1205 // What test type to run?
1206 passthru
->sector_num
= select
;
1209 passthru
->features
= ATA_SMART_STATUS
;
1212 // This is JUST to see if SMART is enabled, by giving SMART status
1213 // command. But it doesn't say if status was good, or failing.
1214 // See below for the difference.
1215 passthru
->features
= ATA_SMART_STATUS
;
1218 pout("Unrecognized command %d in linux_3ware_command_interface(disk %d)\n"
1219 "Please contact " PACKAGE_BUGREPORT
"\n", command
, disknum
);
1224 // Now send the command down through an ioctl()
1225 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1226 ioctlreturn
=ioctl(fd
, TW_IOCTL_FIRMWARE_PASS_THROUGH
, tw_ioctl_apache
);
1227 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1228 ioctlreturn
=ioctl(fd
, TW_CMD_PACKET_WITH_DATA
, tw_ioctl_char
);
1230 ioctlreturn
=ioctl(fd
, SCSI_IOCTL_SEND_COMMAND
, tw_ioctl
);
1232 // Deal with the different error cases
1234 if (CONTROLLER_3WARE_678K
==escalade_type
&& ((command
==AUTO_OFFLINE
|| command
==AUTOSAVE
) && select
)){
1235 // error here is probably a kernel driver whose version is too old
1236 printwarning(command
);
1244 // The passthru structure is valid after return from an ioctl if:
1245 // - we are using the character interface OR
1246 // - we are using the SCSI interface and this is a NON-READ-DATA command
1247 // For SCSI interface, note that we set passthru to a different
1248 // value after ioctl().
1249 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1253 passthru
=(TW_Passthru
*)&(tw_output
->output_data
);
1256 // See if the ATA command failed. Now that we have returned from
1257 // the ioctl() call, if passthru is valid, then:
1258 // - passthru->status contains the 3ware controller STATUS
1259 // - passthru->command contains the ATA STATUS register
1260 // - passthru->features contains the ATA ERROR register
1262 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1263 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1264 // While we *might* decode the ATA ERROR register, at the moment it
1265 // doesn't make much sense: we don't care in detail why the error
1268 if (passthru
&& (passthru
->status
|| (passthru
->command
& 0x21))) {
1273 // If this is a read data command, copy data to output buffer
1275 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1276 memcpy(data
, (unsigned char *)tw_ioctl_apache
->data_buffer
, 512);
1277 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1278 memcpy(data
, (unsigned char *)tw_ioctl_char
->data_buffer
, 512);
1280 memcpy(data
, tw_output
->output_data
, 512);
1283 // For STATUS_CHECK, we need to check register values
1284 if (command
==STATUS_CHECK
) {
1286 // To find out if the SMART RETURN STATUS is good or failing, we
1287 // need to examine the values of the Cylinder Low and Cylinder
1290 unsigned short cyl_lo
=passthru
->cylinder_lo
;
1291 unsigned short cyl_hi
=passthru
->cylinder_hi
;
1293 // If values in Cyl-LO and Cyl-HI are unchanged, SMART status is good.
1294 if (cyl_lo
==0x4F && cyl_hi
==0xC2)
1297 // If values in Cyl-LO and Cyl-HI are as follows, SMART status is FAIL
1298 if (cyl_lo
==0xF4 && cyl_hi
==0x2C)
1301 // Any other values mean that something has gone wrong with the command
1302 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1303 printwarning(command
);
1313 // copy sector count register (one byte!) to return data
1314 if (command
==CHECK_POWER_MODE
)
1315 *data
=*(char *)&(passthru
->sector_count
);
1317 // look for nonexistent devices/ports
1318 if (command
==IDENTIFY
&& !nonempty((unsigned char *)data
, 512)) {
1328 int marvell_command_interface(int device
,
1329 smart_command_set command
,
1339 mvsata_scsi_cmd smart_command
;
1340 unsigned char *buff
= (unsigned char *)&smart_command
.cmd
[6];
1341 // See struct hd_drive_cmd_hdr in hdreg.h
1342 // buff[0]: ATA COMMAND CODE REGISTER
1343 // buff[1]: ATA SECTOR NUMBER REGISTER
1344 // buff[2]: ATA FEATURES REGISTER
1345 // buff[3]: ATA SECTOR COUNT REGISTER
1347 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
1348 memset(&smart_command
, 0, sizeof(smart_command
));
1349 smart_command
.inlen
= 540;
1350 smart_command
.outlen
= 540;
1351 smart_command
.cmd
[0] = 0xC; //Vendor-specific code
1352 smart_command
.cmd
[4] = 6; //command length
1354 buff
[0] = ATA_SMART_CMD
;
1356 case CHECK_POWER_MODE
:
1357 buff
[0]=ATA_CHECK_POWER_MODE
;
1360 buff
[2]=ATA_SMART_READ_VALUES
;
1363 case READ_THRESHOLDS
:
1364 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1365 copydata
=buff
[1]=buff
[3]=1;
1368 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1373 buff
[0]=ATA_IDENTIFY_DEVICE
;
1377 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1381 buff
[2]=ATA_SMART_ENABLE
;
1385 buff
[2]=ATA_SMART_DISABLE
;
1390 // this command only says if SMART is working. It could be
1391 // replaced with STATUS_CHECK below.
1392 buff
[2] = ATA_SMART_STATUS
;
1395 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1396 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1399 buff
[2]=ATA_SMART_AUTOSAVE
;
1400 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1402 case IMMEDIATE_OFFLINE
:
1403 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1407 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command
);
1411 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
1413 // We are now doing the HDIO_DRIVE_CMD type ioctl.
1414 if (ioctl(device
, SCSI_IOCTL_SEND_COMMAND
, (void *)&smart_command
))
1417 if (command
==CHECK_POWER_MODE
) {
1418 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
1419 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
1424 // Always succeed on a SMART status, as a disk that failed returned
1425 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
1426 if (command
== STATUS
)
1428 //Data returned is starting from 0 offset
1429 if (command
== STATUS_CHECK
)
1431 // Cyl low and Cyl high unchanged means "Good SMART status"
1432 if (buff
[4] == 0x4F && buff
[5] == 0xC2)
1434 // These values mean "Bad SMART status"
1435 if (buff
[4] == 0xF4 && buff
[5] == 0x2C)
1437 // We haven't gotten output that makes sense; print out some debugging info
1438 syserror("Error SMART Status command failed");
1439 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT
);
1440 pout("Register values returned from SMART Status command are:\n");
1441 pout("CMD =0x%02x\n",(int)buff
[0]);
1442 pout("FR =0x%02x\n",(int)buff
[1]);
1443 pout("NS =0x%02x\n",(int)buff
[2]);
1444 pout("SC =0x%02x\n",(int)buff
[3]);
1445 pout("CL =0x%02x\n",(int)buff
[4]);
1446 pout("CH =0x%02x\n",(int)buff
[5]);
1447 pout("SEL=0x%02x\n",(int)buff
[6]);
1452 memcpy(data
, buff
, 512);
1456 // this implementation is derived from ata_command_interface with a header
1457 // packing for highpoint linux driver ioctl interface
1459 // ioctl(fd,HPTIO_CTL,buff)
1462 // structure of hpt_buff
1463 // +----+----+----+----+--------------------.....---------------------+
1464 // | 1 | 2 | 3 | 4 | 5 |
1465 // +----+----+----+----+--------------------.....---------------------+
1467 // 1: The target controller [ int ( 4 Bytes ) ]
1468 // 2: The channel of the target controllee [ int ( 4 Bytes ) ]
1469 // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
1470 // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
1471 // 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
1472 // if no pmport device, set to 1 or leave blank
1473 // 5: data [ void * ( var leangth ) ]
1475 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
1477 int highpoint_command_interface(int device
, smart_command_set command
,
1478 int select
, char *data
)
1482 unsigned char hpt_buff
[4*sizeof(int) + STRANGE_BUFFER_LENGTH
];
1483 unsigned int *hpt
= (unsigned int *)hpt_buff
;
1484 unsigned char *buff
= &hpt_buff
[4*sizeof(int)];
1486 const int HDIO_DRIVE_CMD_OFFSET
= 4;
1488 memset(hpt_buff
, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH
);
1489 hpt
[0] = con
->hpt_data
[0]; // controller id
1490 hpt
[1] = con
->hpt_data
[1]; // channel number
1491 hpt
[3] = con
->hpt_data
[2]; // pmport number
1493 buff
[0]=ATA_SMART_CMD
;
1495 case CHECK_POWER_MODE
:
1496 buff
[0]=ATA_CHECK_POWER_MODE
;
1500 buff
[2]=ATA_SMART_READ_VALUES
;
1504 case READ_THRESHOLDS
:
1505 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1510 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1518 buff
[0]=ATA_IDENTIFY_DEVICE
;
1523 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1528 buff
[2]=ATA_SMART_ENABLE
;
1532 buff
[2]=ATA_SMART_DISABLE
;
1536 buff
[2]=ATA_SMART_STATUS
;
1539 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1543 buff
[2]=ATA_SMART_AUTOSAVE
;
1546 case IMMEDIATE_OFFLINE
:
1547 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1551 buff
[1]=ATA_SMART_STATUS
;
1554 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
1555 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
1560 if (command
==WRITE_LOG
) {
1561 unsigned char task
[4*sizeof(int)+sizeof(ide_task_request_t
)+512];
1562 unsigned int *hpt
= (unsigned int *)task
;
1563 ide_task_request_t
*reqtask
= (ide_task_request_t
*)(&task
[4*sizeof(int)]);
1564 task_struct_t
*taskfile
= (task_struct_t
*)reqtask
->io_ports
;
1567 memset(task
, 0, sizeof(task
));
1569 hpt
[0] = con
->hpt_data
[0]; // controller id
1570 hpt
[1] = con
->hpt_data
[1]; // channel number
1571 hpt
[3] = con
->hpt_data
[2]; // pmport number
1572 hpt
[2] = HDIO_DRIVE_TASKFILE
; // real hd ioctl
1575 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
1576 taskfile
->sector_count
= 1;
1577 taskfile
->sector_number
= select
;
1578 taskfile
->low_cylinder
= 0x4f;
1579 taskfile
->high_cylinder
= 0xc2;
1580 taskfile
->device_head
= 0;
1581 taskfile
->command
= ATA_SMART_CMD
;
1583 reqtask
->data_phase
= TASKFILE_OUT
;
1584 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
1585 reqtask
->out_size
= 512;
1586 reqtask
->in_size
= 0;
1588 memcpy(task
+sizeof(ide_task_request_t
)+4*sizeof(int), data
, 512);
1590 if ((retval
=ioctl(device
, HPTIO_CTL
, task
))) {
1591 if (retval
==-EINVAL
)
1592 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
1598 if (command
==STATUS_CHECK
){
1600 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
1601 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
1605 hpt
[2] = HDIO_DRIVE_TASK
;
1607 if ((retval
=ioctl(device
, HPTIO_CTL
, hpt_buff
))) {
1608 if (retval
==-EINVAL
) {
1609 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
1610 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
1613 syserror("Error SMART Status command failed");
1617 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
1620 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
1623 syserror("Error SMART Status command failed");
1624 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
1625 pout("Register values returned from SMART Status command are:\n");
1626 pout("CMD=0x%02x\n",(int)buff
[0]);
1627 pout("FR =0x%02x\n",(int)buff
[1]);
1628 pout("NS =0x%02x\n",(int)buff
[2]);
1629 pout("SC =0x%02x\n",(int)buff
[3]);
1630 pout("CL =0x%02x\n",(int)buff
[4]);
1631 pout("CH =0x%02x\n",(int)buff
[5]);
1632 pout("SEL=0x%02x\n",(int)buff
[6]);
1637 if (command
==IDENTIFY
|| command
==PIDENTIFY
) {
1638 unsigned char deviceid
[4*sizeof(int)+512*sizeof(char)];
1639 unsigned int *hpt
= (unsigned int *)deviceid
;
1641 hpt
[0] = con
->hpt_data
[0]; // controller id
1642 hpt
[1] = con
->hpt_data
[1]; // channel number
1643 hpt
[3] = con
->hpt_data
[2]; // pmport number
1645 hpt
[2] = HDIO_GET_IDENTITY
;
1646 if (!ioctl(device
, HPTIO_CTL
, deviceid
) && (deviceid
[4*sizeof(int)] & 0x8000))
1647 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
1651 hpt
[2] = HDIO_DRIVE_CMD
;
1652 if ((ioctl(device
, HPTIO_CTL
, hpt_buff
)))
1655 if (command
==CHECK_POWER_MODE
)
1656 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
1659 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
1665 // Utility function for printing warnings
1666 void printwarning(smart_command_set command
){
1667 static int printed
[4]={0,0,0,0};
1668 const char* message
=
1669 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
1670 "applying a simple 3w-xxxx driver patch that can be found here:\n"
1671 PACKAGE_HOMEPAGE
"\n"
1672 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
1674 if (command
==AUTO_OFFLINE
&& !printed
[0]) {
1676 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message
);
1678 else if (command
==AUTOSAVE
&& !printed
[1]) {
1680 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message
);
1682 else if (command
==STATUS_CHECK
&& !printed
[2]) {
1684 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message
);
1686 else if (command
==WRITE_LOG
&& !printed
[3]) {
1688 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
1694 // Guess device type (ata or scsi) based on device name (Linux
1695 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
1696 // osst, nosst and sg.
1697 static const char * lin_dev_prefix
= "/dev/";
1698 static const char * lin_dev_ata_disk_plus
= "h";
1699 static const char * lin_dev_ata_devfs_disk_plus
= "ide/";
1700 static const char * lin_dev_scsi_devfs_disk_plus
= "scsi/";
1701 static const char * lin_dev_scsi_disk_plus
= "s";
1702 static const char * lin_dev_scsi_tape1
= "ns";
1703 static const char * lin_dev_scsi_tape2
= "os";
1704 static const char * lin_dev_scsi_tape3
= "nos";
1705 static const char * lin_dev_3ware_9000_char
= "twa";
1706 static const char * lin_dev_3ware_678k_char
= "twe";
1707 static const char * lin_dev_cciss_dir
= "cciss/";
1709 int guess_device_type(const char * dev_name
) {
1711 int dev_prefix_len
= strlen(lin_dev_prefix
);
1713 // if dev_name null, or string length zero
1714 if (!dev_name
|| !(len
= strlen(dev_name
)))
1715 return CONTROLLER_UNKNOWN
;
1717 // Remove the leading /dev/... if it's there
1718 if (!strncmp(lin_dev_prefix
, dev_name
, dev_prefix_len
)) {
1719 if (len
<= dev_prefix_len
)
1720 // if nothing else in the string, unrecognized
1721 return CONTROLLER_UNKNOWN
;
1722 // else advance pointer to following characters
1723 dev_name
+= dev_prefix_len
;
1726 // form /dev/h* or h*
1727 if (!strncmp(lin_dev_ata_disk_plus
, dev_name
,
1728 strlen(lin_dev_ata_disk_plus
)))
1729 return CONTROLLER_ATA
;
1731 // form /dev/ide/* or ide/*
1732 if (!strncmp(lin_dev_ata_devfs_disk_plus
, dev_name
,
1733 strlen(lin_dev_ata_devfs_disk_plus
)))
1734 return CONTROLLER_ATA
;
1736 // form /dev/s* or s*
1737 if (!strncmp(lin_dev_scsi_disk_plus
, dev_name
,
1738 strlen(lin_dev_scsi_disk_plus
)))
1739 return CONTROLLER_SCSI
;
1741 // form /dev/scsi/* or scsi/*
1742 if (!strncmp(lin_dev_scsi_devfs_disk_plus
, dev_name
,
1743 strlen(lin_dev_scsi_devfs_disk_plus
)))
1744 return CONTROLLER_SCSI
;
1746 // form /dev/ns* or ns*
1747 if (!strncmp(lin_dev_scsi_tape1
, dev_name
,
1748 strlen(lin_dev_scsi_tape1
)))
1749 return CONTROLLER_SCSI
;
1751 // form /dev/os* or os*
1752 if (!strncmp(lin_dev_scsi_tape2
, dev_name
,
1753 strlen(lin_dev_scsi_tape2
)))
1754 return CONTROLLER_SCSI
;
1756 // form /dev/nos* or nos*
1757 if (!strncmp(lin_dev_scsi_tape3
, dev_name
,
1758 strlen(lin_dev_scsi_tape3
)))
1759 return CONTROLLER_SCSI
;
1762 if (!strncmp(lin_dev_3ware_9000_char
, dev_name
,
1763 strlen(lin_dev_3ware_9000_char
)))
1764 return CONTROLLER_3WARE_9000_CHAR
;
1767 if (!strncmp(lin_dev_3ware_678k_char
, dev_name
,
1768 strlen(lin_dev_3ware_678k_char
)))
1769 return CONTROLLER_3WARE_678K_CHAR
;
1771 if (!strncmp(lin_dev_cciss_dir
, dev_name
,
1772 strlen(lin_dev_cciss_dir
)))
1773 return CONTROLLER_CCISS
;
1775 // we failed to recognize any of the forms
1776 return CONTROLLER_UNKNOWN
;
1782 [ed@firestorm ed
]$ ls
-l
/dev
/discs
1784 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc0
-> ../ide
/host2
/bus0
/target0
/lun0
/
1785 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc1
-> ../ide
/host2
/bus1
/target0
/lun0
/
1786 [ed@firestorm ed
]$ ls
-l dev
/ide
/host*/bus*/target*/lun*/disc
1787 ls
: dev
/ide
/host*/bus*/target*/lun*/disc
: No such file
or directory
1788 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/host*/bus*/target*/lun*/disc
1789 brw
------- 1 root root
33, 0 Dec
31 1969 /dev
/ide
/host2
/bus0
/target0
/lun0
/disc
1790 brw
------- 1 root root
34, 0 Dec
31 1969 /dev
/ide
/host2
/bus1
/target0
/lun0
/disc
1791 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/c
*b
*t
*u
*
1792 ls
: /dev
/ide
/c
*b
*t
*u
*: No such file
or directory
1794 Script done on Fri Nov
7 13:46:28 2003