4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2003-6 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2003-6 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
50 #include <scsi/scsi_ioctl.h>
54 #include <sys/ioctl.h>
57 #ifndef makedev // old versions of types.h do not include sysmacros.h
58 #include <sys/sysmacros.h>
65 extern smartmonctrl
* con
;
71 #define ENOTSUP ENOSYS
73 typedef unsigned long long u8
;
75 #define ARGUSED(x) ((void)(x))
77 static const char *filenameandversion
="$Id: os_linux.cpp,v 1.86 2006/09/12 01:16:54 sxzzsf Exp $";
79 const char *os_XXXX_c_cvsid
="$Id: os_linux.cpp,v 1.86 2006/09/12 01:16:54 sxzzsf Exp $" \
80 ATACMDS_H_CVSID CONFIG_H_CVSID INT64_H_CVSID OS_LINUX_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID
;
82 // to hold onto exit code for atexit routine
83 extern int exitstatus
;
85 // global variable holding byte count of allocated memory
86 extern long long bytes
;
90 /* This function will setup and fix device nodes for a 3ware controller. */
91 #define MAJOR_STRING_LENGTH 3
92 #define DEVICE_STRING_LENGTH 32
93 #define NODE_STRING_LENGTH 16
94 int setup_3ware_nodes(char *nodename
, char *driver_name
) {
97 char majorstring
[MAJOR_STRING_LENGTH
+1];
98 char device_name
[DEVICE_STRING_LENGTH
+1];
99 char nodestring
[NODE_STRING_LENGTH
];
100 struct stat stat_buf
;
103 /* First try to open up /proc/devices */
104 if (!(file
= fopen("/proc/devices", "r"))) {
105 pout("Error opening /proc/devices to check/create 3ware device nodes\n");
107 return 0; // don't fail here: user might not have /proc !
110 /* Attempt to get device major number */
111 while (EOF
!= fscanf(file
, "%3s %32s", majorstring
, device_name
)) {
112 majorstring
[MAJOR_STRING_LENGTH
]='\0';
113 device_name
[DEVICE_STRING_LENGTH
]='\0';
114 if (!strncmp(device_name
, nodename
, DEVICE_STRING_LENGTH
)) {
115 tw_major
= atoi(majorstring
);
121 /* See if we found a major device number */
123 pout("No major number for /dev/%s listed in /proc/devices. Is the %s driver loaded?\n", nodename
, driver_name
);
127 /* Now check if nodes are correct */
128 for (index
=0; index
<16; index
++) {
129 sprintf(nodestring
, "/dev/%s%d", nodename
, index
);
131 /* Try to stat the node */
132 if ((stat(nodestring
, &stat_buf
))) {
133 /* Create a new node if it doesn't exist */
134 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
135 pout("problem creating 3ware device nodes %s", nodestring
);
141 /* See if nodes major and minor numbers are correct */
142 if ((tw_major
!= (int)(major(stat_buf
.st_rdev
))) ||
143 (index
!= (int)(minor(stat_buf
.st_rdev
))) ||
144 (!S_ISCHR(stat_buf
.st_mode
))) {
146 /* Delete the old node */
147 if (unlink(nodestring
)) {
148 pout("problem unlinking stale 3ware device node %s", nodestring
);
153 /* Make a new node */
154 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
155 pout("problem creating 3ware device nodes %s", nodestring
);
164 // equivalent to open(path, flags)
165 int deviceopen(const char *pathname
, char *type
){
166 if (!strcmp(type
,"SCSI")) {
167 int fd
= open(pathname
, O_RDWR
| O_NONBLOCK
);
168 if (fd
< 0 && errno
== EROFS
)
169 fd
= open(pathname
, O_RDONLY
| O_NONBLOCK
);
172 else if (!strcmp(type
,"ATA"))
173 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
174 else if (!strcmp(type
,"ATA_3WARE_9000")) {
175 // the device nodes for this controller are dynamically assigned,
176 // so we need to check that they exist with the correct major
177 // numbers and if not, create them
178 if (setup_3ware_nodes("twa", "3w-9xxx")) {
183 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
185 else if (!strcmp(type
,"ATA_3WARE_678K")) {
186 // the device nodes for this controller are dynamically assigned,
187 // so we need to check that they exist with the correct major
188 // numbers and if not, create them
189 if (setup_3ware_nodes("twe", "3w-xxxx")) {
194 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
200 // equivalent to close(file descriptor)
201 int deviceclose(int fd
){
205 // print examples for smartctl
206 void print_smartctl_examples(){
207 printf("=================================================== SMARTCTL EXAMPLES =====\n\n");
208 #ifdef HAVE_GETOPT_LONG
210 " smartctl --all /dev/hda (Prints all SMART information)\n\n"
211 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n"
212 " (Enables SMART on first disk)\n\n"
213 " smartctl --test=long /dev/hda (Executes extended disk self-test)\n\n"
214 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/hda\n"
215 " (Prints Self-Test & Attribute errors)\n"
216 " smartctl --all --device=3ware,2 /dev/sda\n"
217 " smartctl --all --device=3ware,2 /dev/twe0\n"
218 " smartctl --all --device=3ware,2 /dev/twa0\n"
219 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
220 " smartctl --all --device=hpt,1/1/3 /dev/sda\n"
221 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
222 " of the 1st channel on the 1st HighPoint RAID controller)\n"
226 " smartctl -a /dev/hda (Prints all SMART information)\n"
227 " smartctl -s on -o on -S on /dev/hda (Enables SMART on first disk)\n"
228 " smartctl -t long /dev/hda (Executes extended disk self-test)\n"
229 " smartctl -A -l selftest -q errorsonly /dev/hda\n"
230 " (Prints Self-Test & Attribute errors)\n"
231 " smartctl -a -d 3ware,2 /dev/sda\n"
232 " smartctl -a -d 3ware,2 /dev/twa0\n"
233 " smartctl -a -d 3ware,2 /dev/twe0\n"
234 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
235 " smartctl -a -d hpt,1/1/3 /dev/sda\n"
236 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
237 " of the 1st channel on the 1st HighPoint RAID controller)\n"
244 // we are going to take advantage of the fact that Linux's devfs will only
245 // have device entries for devices that exist. So if we get the equivalent of
246 // ls /dev/hd[a-t], we have all the ATA devices on the system
248 // If any errors occur, leave errno set as it was returned by the
249 // system call, and return <0.
250 int get_dev_names(char*** names
, const char* pattern
, const char* name
, int max
) {
251 int n
= 0, retglob
, i
, lim
;
255 memset(&globbuf
, 0, sizeof(globbuf
));
257 // in case of non-clean exit
260 // Use glob to look for any directory entries matching the pattern
261 if ((retglob
=glob(pattern
, GLOB_ERR
, NULL
, &globbuf
))) {
263 // glob failed: free memory and return
266 if (retglob
==GLOB_NOMATCH
){
267 pout("glob(3) found no matches for pattern %s\n", pattern
);
271 if (retglob
==GLOB_NOSPACE
)
272 pout("glob(3) ran out of memory matching pattern %s\n", pattern
);
273 #ifdef GLOB_ABORTED // missing in old versions of glob.h
274 else if (retglob
==GLOB_ABORTED
)
275 pout("glob(3) aborted matching pattern %s\n", pattern
);
278 pout("Unexplained error in glob(3) of pattern %s\n", pattern
);
283 // did we find too many paths?
284 lim
= ((int)globbuf
.gl_pathc
< max
) ? (int)globbuf
.gl_pathc
: max
;
285 if (lim
< (int)globbuf
.gl_pathc
)
286 pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n",
287 (int)globbuf
.gl_pathc
, max
, pattern
, (int)(globbuf
.gl_pathc
-max
));
289 // allocate space for up to lim number of ATA devices
290 if (!(mp
= (char **)calloc(lim
, sizeof(char*)))){
291 pout("Out of memory constructing scan device list\n");
295 // now step through the list returned by glob. If not a link, copy
296 // to list. If it is a link, evaluate it and see if the path ends
298 for (i
=0; i
<lim
; i
++){
301 // prepare a buffer for storing the link
304 // see if path is a link
305 retlink
=readlink(globbuf
.gl_pathv
[i
], linkbuf
, 1023);
307 // if not a link (or a strange link), keep it
308 if (retlink
<=0 || retlink
>1023)
309 mp
[n
++] = CustomStrDup(globbuf
.gl_pathv
[i
], 1, __LINE__
, filenameandversion
);
311 // or if it's a link that points to a disc, follow it
313 linkbuf
[retlink
]='\0';
314 if ((p
=strrchr(linkbuf
,'/')) && !strcmp(p
+1, "disc"))
315 // This is the branch of the code that gets followed if we are
316 // using devfs WITH traditional compatibility links. In this
317 // case, we add the traditional device name to the list that
319 mp
[n
++] = CustomStrDup(globbuf
.gl_pathv
[i
], 1, __LINE__
, filenameandversion
);
321 // This is the branch of the code that gets followed if we are
322 // using devfs WITHOUT traditional compatibility links. In
323 // this case, we check that the link to the directory is of
324 // the correct type, and then append "disc" to it.
325 char tmpname
[1024]={0};
326 const char * type
= (strcmp(name
,"ATA") ? "scsi" : "ide");
327 if (strstr(linkbuf
, type
)){
328 snprintf(tmpname
, 1024, "%s/disc", globbuf
.gl_pathv
[i
]);
329 mp
[n
++] = CustomStrDup(tmpname
, 1, __LINE__
, filenameandversion
);
335 // free memory, track memory usage
337 mp
= static_cast<char **>(realloc(mp
,n
*(sizeof(char*))));
338 bytes
+= n
*(sizeof(char*));
340 // and set up return values
345 // makes a list of device names to scan, for either ATA or SCSI
346 // devices. Return -1 if no memory remaining, else the number of
347 // devices on the list, which can be >=0.
348 int make_device_names (char*** devlist
, const char* name
) {
352 // for testing case where no device names are found
356 if (!strcmp(name
,"SCSI"))
357 retval
=get_dev_names(devlist
,"/dev/sd[a-z]", name
, maxdev
=26);
358 else if (!strcmp(name
,"ATA"))
359 retval
=get_dev_names(devlist
,"/dev/hd[a-t]", name
, maxdev
=20);
361 // don't recognize disk type!
364 // if we found traditional links, we are done
368 // else look for devfs entries without traditional links
369 return get_dev_names(devlist
,"/dev/discs/disc*", name
, maxdev
);
374 // This is an interface routine meant to isolate the OS dependent
375 // parts of the code, and to provide a debugging interface. Each
376 // different port and OS needs to provide it's own interface. This
378 // DETAILED DESCRIPTION OF ARGUMENTS
379 // device: is the file descriptor provided by open()
380 // command: defines the different operations.
381 // select: additional input data if needed (which log, which type of
383 // data: location to write output data, if needed (512 bytes).
384 // Note: not all commands use all arguments.
386 // -1 if the command failed
387 // 0 if the command succeeded,
388 // STATUS_CHECK routine:
389 // -1 if the command failed
390 // 0 if the command succeeded and disk SMART status is "OK"
391 // 1 if the command succeeded and disk SMART status is "FAILING"
394 // huge value of buffer size needed because HDIO_DRIVE_CMD assumes
395 // that buff[3] is the data size. Since the ATA_SMART_AUTOSAVE and
396 // ATA_SMART_AUTO_OFFLINE use values of 0xf1 and 0xf8 we need the space.
397 // Otherwise a 4+512 byte buffer would be enough.
398 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
400 int ata_command_interface(int device
, smart_command_set command
, int select
, char *data
){
401 unsigned char buff
[STRANGE_BUFFER_LENGTH
];
402 // positive: bytes to write to caller. negative: bytes to READ from
403 // caller. zero: non-data command
406 const int HDIO_DRIVE_CMD_OFFSET
= 4;
408 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
409 // buff[0]: ATA COMMAND CODE REGISTER
410 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
411 // buff[2]: ATA FEATURES REGISTER
412 // buff[3]: ATA SECTOR COUNT REGISTER
414 // Note that on return:
415 // buff[2] contains the ATA SECTOR COUNT REGISTER
417 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
418 memset(buff
, 0, STRANGE_BUFFER_LENGTH
);
420 buff
[0]=ATA_SMART_CMD
;
422 case CHECK_POWER_MODE
:
423 buff
[0]=ATA_CHECK_POWER_MODE
;
427 buff
[2]=ATA_SMART_READ_VALUES
;
431 case READ_THRESHOLDS
:
432 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
437 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
445 buff
[0]=ATA_IDENTIFY_DEVICE
;
450 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
455 buff
[2]=ATA_SMART_ENABLE
;
459 buff
[2]=ATA_SMART_DISABLE
;
463 // this command only says if SMART is working. It could be
464 // replaced with STATUS_CHECK below.
465 buff
[2]=ATA_SMART_STATUS
;
468 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
469 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
472 buff
[2]=ATA_SMART_AUTOSAVE
;
473 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
475 case IMMEDIATE_OFFLINE
:
476 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
480 // This command uses HDIO_DRIVE_TASK and has different syntax than
481 // the other commands.
482 buff
[1]=ATA_SMART_STATUS
;
485 pout("Unrecognized command %d in linux_ata_command_interface()\n"
486 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
491 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
492 // only ioctl() that can be used to WRITE data to the disk.
493 if (command
==WRITE_LOG
) {
494 unsigned char task
[sizeof(ide_task_request_t
)+512];
495 ide_task_request_t
*reqtask
=(ide_task_request_t
*) task
;
496 task_struct_t
*taskfile
=(task_struct_t
*) reqtask
->io_ports
;
499 memset(task
, 0, sizeof(task
));
502 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
503 taskfile
->sector_count
= 1;
504 taskfile
->sector_number
= select
;
505 taskfile
->low_cylinder
= 0x4f;
506 taskfile
->high_cylinder
= 0xc2;
507 taskfile
->device_head
= 0;
508 taskfile
->command
= ATA_SMART_CMD
;
510 reqtask
->data_phase
= TASKFILE_OUT
;
511 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
512 reqtask
->out_size
= 512;
513 reqtask
->in_size
= 0;
515 // copy user data into the task request structure
516 memcpy(task
+sizeof(ide_task_request_t
), data
, 512);
518 if ((retval
=ioctl(device
, HDIO_DRIVE_TASKFILE
, task
))) {
520 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
526 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
528 if (command
==STATUS_CHECK
){
531 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
532 // have to read the IDE driver source code. Sigh.
533 // buff[0]: ATA COMMAND CODE REGISTER
534 // buff[1]: ATA FEATURES REGISTER
535 // buff[2]: ATA SECTOR_COUNT
536 // buff[3]: ATA SECTOR NUMBER
537 // buff[4]: ATA CYL LO REGISTER
538 // buff[5]: ATA CYL HI REGISTER
539 // buff[6]: ATA DEVICE HEAD
541 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
542 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
546 if ((retval
=ioctl(device
, HDIO_DRIVE_TASK
, buff
))) {
547 if (retval
==-EINVAL
) {
548 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
549 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
552 syserror("Error SMART Status command failed");
556 // Cyl low and Cyl high unchanged means "Good SMART status"
557 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
560 // These values mean "Bad SMART status"
561 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
564 // We haven't gotten output that makes sense; print out some debugging info
565 syserror("Error SMART Status command failed");
566 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
567 pout("Register values returned from SMART Status command are:\n");
568 pout("CMD=0x%02x\n",(int)buff
[0]);
569 pout("FR =0x%02x\n",(int)buff
[1]);
570 pout("NS =0x%02x\n",(int)buff
[2]);
571 pout("SC =0x%02x\n",(int)buff
[3]);
572 pout("CL =0x%02x\n",(int)buff
[4]);
573 pout("CH =0x%02x\n",(int)buff
[5]);
574 pout("SEL=0x%02x\n",(int)buff
[6]);
579 // Note to people doing ports to other OSes -- don't worry about
580 // this block -- you can safely ignore it. I have put it here
581 // because under linux when you do IDENTIFY DEVICE to a packet
582 // device, it generates an ugly kernel syslog error message. This
583 // is harmless but frightens users. So this block detects packet
584 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
587 // If you read only the ATA specs, it appears as if a packet device
588 // *might* respond to the IDENTIFY DEVICE command. This is
589 // misleading - it's because around the time that SFF-8020 was
590 // incorporated into the ATA-3/4 standard, the ATA authors were
591 // sloppy. See SFF-8020 and you will see that ATAPI devices have
592 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
593 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
594 if (command
==IDENTIFY
|| command
==PIDENTIFY
){
595 unsigned short deviceid
[256];
596 // check the device identity, as seen when the system was booted
597 // or the device was FIRST registered. This will not be current
598 // if the user has subsequently changed some of the parameters. If
599 // device is a packet device, swap the command interpretations.
600 if (!ioctl(device
, HDIO_GET_IDENTITY
, deviceid
) && (deviceid
[0] & 0x8000))
601 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
605 // We are now doing the HDIO_DRIVE_CMD type ioctl.
606 if ((ioctl(device
, HDIO_DRIVE_CMD
, buff
)))
609 // CHECK POWER MODE command returns information in the Sector Count
610 // register (buff[3]). Copy to return data buffer.
611 if (command
==CHECK_POWER_MODE
)
612 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
614 // if the command returns data then copy it back
616 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
621 // >>>>>> Start of general SCSI specific linux code
623 /* Linux specific code.
624 * Historically smartmontools (and smartsuite before it) used the
625 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
626 * nodes that use the SCSI subsystem. A better interface has been available
627 * via the SCSI generic (sg) driver but this involves the extra step of
628 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
629 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
630 * the sg driver have become available via the SG_IO ioctl which is available
631 * on all SCSI devices (on SCSI tape devices from lk 2.6.6).
632 * So the strategy below is to find out if the SG_IO ioctl is available and
633 * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl.
634 * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */
636 #define MAX_DXFER_LEN 1024 /* can be increased if necessary */
637 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
638 #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
639 #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
640 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
641 #define LSCSI_DRIVER_TIMEOUT 0x6
642 #define LSCSI_DID_TIME_OUT 0x3
643 #define LSCSI_DID_BUS_BUSY 0x2
644 #define LSCSI_DID_NO_CONNECT 0x1
646 #ifndef SCSI_IOCTL_SEND_COMMAND
647 #define SCSI_IOCTL_SEND_COMMAND 1
650 #define SG_IO_PRESENT_UNKNOWN 0
651 #define SG_IO_PRESENT_YES 1
652 #define SG_IO_PRESENT_NO 2
654 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
656 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
);
658 static int sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
660 /* Preferred implementation for issuing SCSI commands in linux. This
661 * function uses the SG_IO ioctl. Return 0 if command issued successfully
662 * (various status values should still be checked). If the SCSI command
663 * cannot be issued then a negative errno value is returned. */
664 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
668 ARGUSED(dev_fd
); ARGUSED(iop
); ARGUSED(report
);
671 struct sg_io_hdr io_hdr
;
675 const unsigned char * ucp
= iop
->cmnd
;
678 const int sz
= (int)sizeof(buff
);
680 np
= scsi_get_opcode_name(ucp
[0]);
681 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
682 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
683 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
685 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
686 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
688 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
689 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
690 (trunc
? " [only first 256 bytes shown]" : ""));
691 dStrHex((const char *)iop
->dxferp
,
692 (trunc
? 256 : iop
->dxfer_len
) , 1);
695 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
698 memset(&io_hdr
, 0, sizeof(struct sg_io_hdr
));
699 io_hdr
.interface_id
= 'S';
700 io_hdr
.cmd_len
= iop
->cmnd_len
;
701 io_hdr
.mx_sb_len
= iop
->max_sense_len
;
702 io_hdr
.dxfer_len
= iop
->dxfer_len
;
703 io_hdr
.dxferp
= iop
->dxferp
;
704 io_hdr
.cmdp
= iop
->cmnd
;
705 io_hdr
.sbp
= iop
->sensep
;
706 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
707 defaults to 60 seconds. */
708 io_hdr
.timeout
= ((0 == iop
->timeout
) ? 60 : iop
->timeout
) * 1000;
709 switch (iop
->dxfer_dir
) {
711 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
713 case DXFER_FROM_DEVICE
:
714 io_hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
716 case DXFER_TO_DEVICE
:
717 io_hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
720 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
723 iop
->resp_sense_len
= 0;
724 iop
->scsi_status
= 0;
726 if (ioctl(dev_fd
, SG_IO
, &io_hdr
) < 0) {
727 if (report
&& (! unknown
))
728 pout(" SG_IO ioctl failed, errno=%d [%s]\n", errno
,
733 pout(" scsi_status=0x%x, host_status=0x%x, driver_status=0x%x\n"
734 " info=0x%x duration=%d milliseconds\n", io_hdr
.status
,
735 io_hdr
.host_status
, io_hdr
.driver_status
, io_hdr
.info
,
738 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
739 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
741 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
742 (trunc
? " [only first 256 bytes shown]" : ""));
743 dStrHex((const char*)iop
->dxferp
,
744 (trunc
? 256 : iop
->dxfer_len
) , 1);
748 iop
->resid
= io_hdr
.resid
;
749 iop
->scsi_status
= io_hdr
.status
;
751 if (io_hdr
.info
| SG_INFO_CHECK
) { /* error or warning */
752 int masked_driver_status
= (LSCSI_DRIVER_MASK
& io_hdr
.driver_status
);
754 if (0 != io_hdr
.host_status
) {
755 if ((LSCSI_DID_NO_CONNECT
== io_hdr
.host_status
) ||
756 (LSCSI_DID_BUS_BUSY
== io_hdr
.host_status
) ||
757 (LSCSI_DID_TIME_OUT
== io_hdr
.host_status
))
760 return -EIO
; /* catch all */
762 if (0 != masked_driver_status
) {
763 if (LSCSI_DRIVER_TIMEOUT
== masked_driver_status
)
765 else if (LSCSI_DRIVER_SENSE
!= masked_driver_status
)
768 if (LSCSI_DRIVER_SENSE
== masked_driver_status
)
769 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
770 iop
->resp_sense_len
= io_hdr
.sb_len_wr
;
771 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
772 iop
->sensep
&& (iop
->resp_sense_len
> 0)) {
774 pout(" >>> Sense buffer, len=%d:\n",
775 (int)iop
->resp_sense_len
);
776 dStrHex((const char *)iop
->sensep
, iop
->resp_sense_len
, 1);
780 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
781 if ((iop
->sensep
[0] & 0x7f) > 0x71)
782 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
783 iop
->scsi_status
, iop
->sensep
[1] & 0xf,
784 iop
->sensep
[2], iop
->sensep
[3]);
786 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
787 iop
->scsi_status
, iop
->sensep
[2] & 0xf,
788 iop
->sensep
[12], iop
->sensep
[13]);
791 pout(" status=0x%x\n", iop
->scsi_status
);
798 struct linux_ioctl_send_command
802 UINT8 buff
[MAX_DXFER_LEN
+ 16];
805 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
806 * support: CDB length (guesses it from opcode), resid and timeout.
807 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
808 * to 2 hours in order to allow long foreground extended self tests. */
809 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
811 struct linux_ioctl_send_command wrk
;
812 int status
, buff_offset
;
815 memcpy(wrk
.buff
, iop
->cmnd
, iop
->cmnd_len
);
816 buff_offset
= iop
->cmnd_len
;
819 const unsigned char * ucp
= iop
->cmnd
;
822 const int sz
= (int)sizeof(buff
);
824 np
= scsi_get_opcode_name(ucp
[0]);
825 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
826 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
827 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
829 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
830 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
832 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
833 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
834 (trunc
? " [only first 256 bytes shown]" : ""));
835 dStrHex((const char *)iop
->dxferp
,
836 (trunc
? 256 : iop
->dxfer_len
) , 1);
839 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
842 switch (iop
->dxfer_dir
) {
847 case DXFER_FROM_DEVICE
:
849 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
851 wrk
.outbufsize
= iop
->dxfer_len
;
853 case DXFER_TO_DEVICE
:
854 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
856 memcpy(wrk
.buff
+ buff_offset
, iop
->dxferp
, iop
->dxfer_len
);
857 wrk
.inbufsize
= iop
->dxfer_len
;
861 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
864 iop
->resp_sense_len
= 0;
865 iop
->scsi_status
= 0;
867 status
= ioctl(dev_fd
, SCSI_IOCTL_SEND_COMMAND
, &wrk
);
870 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
871 errno
, strerror(errno
));
877 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
878 memcpy(iop
->dxferp
, wrk
.buff
, iop
->dxfer_len
);
880 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
882 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
883 (trunc
? " [only first 256 bytes shown]" : ""));
884 dStrHex((const char*)iop
->dxferp
,
885 (trunc
? 256 : iop
->dxfer_len
) , 1);
890 iop
->scsi_status
= status
& 0x7e; /* bits 0 and 7 used to be for vendors */
891 if (LSCSI_DRIVER_SENSE
== ((status
>> 24) & 0xf))
892 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
893 len
= (SEND_IOCTL_RESP_SENSE_LEN
< iop
->max_sense_len
) ?
894 SEND_IOCTL_RESP_SENSE_LEN
: iop
->max_sense_len
;
895 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
896 iop
->sensep
&& (len
> 0)) {
897 memcpy(iop
->sensep
, wrk
.buff
, len
);
898 iop
->resp_sense_len
= len
;
900 pout(" >>> Sense buffer, len=%d:\n", (int)len
);
901 dStrHex((const char *)wrk
.buff
, len
, 1);
905 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
906 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status
& 0xff,
907 wrk
.buff
[2] & 0xf, wrk
.buff
[12], wrk
.buff
[13]);
910 pout(" status=0x%x\n", status
);
912 if (iop
->scsi_status
> 0)
916 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
918 return -EIO
; /* give up, assume no device there */
922 /* SCSI command transmission interface function, linux version.
923 * Returns 0 if SCSI command successfully launched and response
924 * received. Even when 0 is returned the caller should check
925 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
926 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
927 * (e.g. device not present or timeout) or some other problem
928 * (e.g. timeout) then returns a negative errno value */
929 int do_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
933 /* implementation relies on static sg_io_state variable. If not
934 * previously set tries the SG_IO ioctl. If that succeeds assume
935 * that SG_IO ioctl functional. If it fails with an errno value
936 * other than ENODEV (no device) or permission then assume
937 * SCSI_IOCTL_SEND_COMMAND is the only option. */
938 switch (sg_io_state
) {
939 case SG_IO_PRESENT_UNKNOWN
:
940 /* ignore report argument */
941 if (0 == (res
= sg_io_cmnd_io(dev_fd
, iop
, report
, 1))) {
942 sg_io_state
= SG_IO_PRESENT_YES
;
944 } else if ((-ENODEV
== res
) || (-EACCES
== res
) || (-EPERM
== res
))
945 return res
; /* wait until we see a device */
946 sg_io_state
= SG_IO_PRESENT_NO
;
947 /* drop through by design */
948 case SG_IO_PRESENT_NO
:
949 return sisc_cmnd_io(dev_fd
, iop
, report
);
950 case SG_IO_PRESENT_YES
:
951 return sg_io_cmnd_io(dev_fd
, iop
, report
, 0);
953 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state
);
954 sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
955 return -EIO
; /* report error and reset state */
959 // >>>>>> End of general SCSI specific linux code
963 void printwarning(smart_command_set command
);
966 // This is an interface routine meant to isolate the OS dependent
967 // parts of the code, and to provide a debugging interface. Each
968 // different port and OS needs to provide it's own interface. This
969 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
970 // commands to be passed through the SCSI driver.
971 // DETAILED DESCRIPTION OF ARGUMENTS
972 // fd: is the file descriptor provided by open()
973 // disknum is the disk number (0 to 15) in the RAID array
974 // escalade_type indicates the type of controller type, and if scsi or char interface is used
975 // command: defines the different operations.
976 // select: additional input data if needed (which log, which type of
978 // data: location to write output data, if needed (512 bytes).
979 // Note: not all commands use all arguments.
981 // -1 if the command failed
982 // 0 if the command succeeded,
983 // STATUS_CHECK routine:
984 // -1 if the command failed
985 // 0 if the command succeeded and disk SMART status is "OK"
986 // 1 if the command succeeded and disk SMART status is "FAILING"
989 /* 512 is the max payload size: increase if needed */
990 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
991 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
992 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
993 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
995 int escalade_command_interface(int fd
, int disknum
, int escalade_type
, smart_command_set command
, int select
, char *data
){
997 // return value and buffer for ioctl()
998 int ioctlreturn
, readdata
=0;
1000 // Used by both the SCSI and char interfaces
1001 TW_Passthru
*passthru
=NULL
;
1002 char ioctl_buffer
[TW_IOCTL_BUFFER_SIZE
];
1004 // only used for SCSI device interface
1005 TW_Ioctl
*tw_ioctl
=NULL
;
1006 TW_Output
*tw_output
=NULL
;
1008 // only used for 6000/7000/8000 char device interface
1009 TW_New_Ioctl
*tw_ioctl_char
=NULL
;
1011 // only used for 9000 character device interface
1012 TW_Ioctl_Buf_Apache
*tw_ioctl_apache
=NULL
;
1014 memset(ioctl_buffer
, 0, TW_IOCTL_BUFFER_SIZE
);
1016 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
) {
1017 tw_ioctl_apache
= (TW_Ioctl_Buf_Apache
*)ioctl_buffer
;
1018 tw_ioctl_apache
->driver_command
.control_code
= TW_IOCTL_FIRMWARE_PASS_THROUGH
;
1019 tw_ioctl_apache
->driver_command
.buffer_length
= 512; /* payload size */
1020 passthru
= (TW_Passthru
*)&(tw_ioctl_apache
->firmware_command
.command
.oldcommand
);
1022 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
) {
1023 tw_ioctl_char
= (TW_New_Ioctl
*)ioctl_buffer
;
1024 tw_ioctl_char
->data_buffer_length
= 512;
1025 passthru
= (TW_Passthru
*)&(tw_ioctl_char
->firmware_command
);
1027 else if (escalade_type
==CONTROLLER_3WARE_678K
) {
1028 tw_ioctl
= (TW_Ioctl
*)ioctl_buffer
;
1029 tw_ioctl
->cdb
[0] = TW_IOCTL
;
1030 tw_ioctl
->opcode
= TW_ATA_PASSTHRU
;
1031 tw_ioctl
->input_length
= 512; // correct even for non-data commands
1032 tw_ioctl
->output_length
= 512; // correct even for non-data commands
1033 tw_output
= (TW_Output
*)tw_ioctl
;
1034 passthru
= (TW_Passthru
*)&(tw_ioctl
->input_data
);
1037 pout("Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1038 "Please contact " PACKAGE_BUGREPORT
"\n", escalade_type
, disknum
);
1043 // Same for (almost) all commands - but some reset below
1044 passthru
->byte0
.opcode
= TW_OP_ATA_PASSTHRU
;
1045 passthru
->request_id
= 0xFF;
1046 passthru
->byte3
.aport
= disknum
;
1047 passthru
->byte3
.host_id
= 0;
1048 passthru
->status
= 0;
1049 passthru
->flags
= 0x1;
1050 passthru
->drive_head
= 0x0;
1051 passthru
->sector_num
= 0;
1053 // All SMART commands use this CL/CH signature. These are magic
1054 // values from the ATA specifications.
1055 passthru
->cylinder_lo
= 0x4F;
1056 passthru
->cylinder_hi
= 0xC2;
1058 // SMART ATA COMMAND REGISTER value
1059 passthru
->command
= ATA_SMART_CMD
;
1061 // Is this a command that reads or returns 512 bytes?
1062 // passthru->param values are:
1063 // 0x0 - non data command without TFR write check,
1064 // 0x8 - non data command with TFR write check,
1065 // 0xD - data command that returns data to host from device
1066 // 0xF - data command that writes data from host to device
1067 // passthru->size values are 0x5 for non-data and 0x07 for data
1068 if (command
== READ_VALUES
||
1069 command
== READ_THRESHOLDS
||
1070 command
== READ_LOG
||
1071 command
== IDENTIFY
||
1072 command
== WRITE_LOG
) {
1074 passthru
->byte0
.sgloff
= 0x5;
1075 passthru
->size
= 0x7;
1076 passthru
->param
= 0xD;
1077 passthru
->sector_count
= 0x1;
1078 // For 64-bit to work correctly, up the size of the command packet
1079 // in dwords by 1 to account for the 64-bit single sgl 'address'
1080 // field. Note that this doesn't agree with the typedefs but it's
1081 // right (agree with kernel driver behavior/typedefs).
1082 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
&& sizeof(long)==8)
1086 // Non data command -- but doesn't use large sector
1087 // count register values.
1088 passthru
->byte0
.sgloff
= 0x0;
1089 passthru
->size
= 0x5;
1090 passthru
->param
= 0x8;
1091 passthru
->sector_count
= 0x0;
1094 // Now set ATA registers depending upon command
1096 case CHECK_POWER_MODE
:
1097 passthru
->command
= ATA_CHECK_POWER_MODE
;
1098 passthru
->features
= 0;
1099 passthru
->cylinder_lo
= 0;
1100 passthru
->cylinder_hi
= 0;
1103 passthru
->features
= ATA_SMART_READ_VALUES
;
1105 case READ_THRESHOLDS
:
1106 passthru
->features
= ATA_SMART_READ_THRESHOLDS
;
1109 passthru
->features
= ATA_SMART_READ_LOG_SECTOR
;
1110 // log number to return
1111 passthru
->sector_num
= select
;
1114 if (escalade_type
== CONTROLLER_3WARE_9000_CHAR
)
1115 memcpy((unsigned char *)tw_ioctl_apache
->data_buffer
, data
, 512);
1116 else if (escalade_type
== CONTROLLER_3WARE_678K_CHAR
)
1117 memcpy((unsigned char *)tw_ioctl_char
->data_buffer
, data
, 512);
1119 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1120 // memcpy(tw_output->output_data, data, 512);
1121 printwarning(command
);
1126 passthru
->features
= ATA_SMART_WRITE_LOG_SECTOR
;
1127 passthru
->sector_count
= 1;
1128 passthru
->sector_num
= select
;
1129 passthru
->param
= 0xF; // PIO data write
1132 // ATA IDENTIFY DEVICE
1133 passthru
->command
= ATA_IDENTIFY_DEVICE
;
1134 passthru
->features
= 0;
1135 passthru
->cylinder_lo
= 0;
1136 passthru
->cylinder_hi
= 0;
1139 // 3WARE controller can NOT have packet device internally
1140 pout("WARNING - NO DEVICE FOUND ON 3WARE CONTROLLER (disk %d)\n", disknum
);
1141 pout("Note: /dev/sdX many need to be replaced with /dev/tweN or /dev/twaN\n");
1145 passthru
->features
= ATA_SMART_ENABLE
;
1148 passthru
->features
= ATA_SMART_DISABLE
;
1151 passthru
->features
= ATA_SMART_AUTO_OFFLINE
;
1152 // Enable or disable?
1153 passthru
->sector_count
= select
;
1156 passthru
->features
= ATA_SMART_AUTOSAVE
;
1157 // Enable or disable?
1158 passthru
->sector_count
= select
;
1160 case IMMEDIATE_OFFLINE
:
1161 passthru
->features
= ATA_SMART_IMMEDIATE_OFFLINE
;
1162 // What test type to run?
1163 passthru
->sector_num
= select
;
1166 passthru
->features
= ATA_SMART_STATUS
;
1169 // This is JUST to see if SMART is enabled, by giving SMART status
1170 // command. But it doesn't say if status was good, or failing.
1171 // See below for the difference.
1172 passthru
->features
= ATA_SMART_STATUS
;
1175 pout("Unrecognized command %d in linux_3ware_command_interface(disk %d)\n"
1176 "Please contact " PACKAGE_BUGREPORT
"\n", command
, disknum
);
1181 // Now send the command down through an ioctl()
1182 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1183 ioctlreturn
=ioctl(fd
, TW_IOCTL_FIRMWARE_PASS_THROUGH
, tw_ioctl_apache
);
1184 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1185 ioctlreturn
=ioctl(fd
, TW_CMD_PACKET_WITH_DATA
, tw_ioctl_char
);
1187 ioctlreturn
=ioctl(fd
, SCSI_IOCTL_SEND_COMMAND
, tw_ioctl
);
1189 // Deal with the different error cases
1191 if (CONTROLLER_3WARE_678K
==escalade_type
&& ((command
==AUTO_OFFLINE
|| command
==AUTOSAVE
) && select
)){
1192 // error here is probably a kernel driver whose version is too old
1193 printwarning(command
);
1201 // The passthru structure is valid after return from an ioctl if:
1202 // - we are using the character interface OR
1203 // - we are using the SCSI interface and this is a NON-READ-DATA command
1204 // For SCSI interface, note that we set passthru to a different
1205 // value after ioctl().
1206 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1210 passthru
=(TW_Passthru
*)&(tw_output
->output_data
);
1213 // See if the ATA command failed. Now that we have returned from
1214 // the ioctl() call, if passthru is valid, then:
1215 // - passthru->status contains the 3ware controller STATUS
1216 // - passthru->command contains the ATA STATUS register
1217 // - passthru->features contains the ATA ERROR register
1219 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1220 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1221 // While we *might* decode the ATA ERROR register, at the moment it
1222 // doesn't make much sense: we don't care in detail why the error
1225 if (passthru
&& (passthru
->status
|| (passthru
->command
& 0x21))) {
1230 // If this is a read data command, copy data to output buffer
1232 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1233 memcpy(data
, (unsigned char *)tw_ioctl_apache
->data_buffer
, 512);
1234 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1235 memcpy(data
, (unsigned char *)tw_ioctl_char
->data_buffer
, 512);
1237 memcpy(data
, tw_output
->output_data
, 512);
1240 // For STATUS_CHECK, we need to check register values
1241 if (command
==STATUS_CHECK
) {
1243 // To find out if the SMART RETURN STATUS is good or failing, we
1244 // need to examine the values of the Cylinder Low and Cylinder
1247 unsigned short cyl_lo
=passthru
->cylinder_lo
;
1248 unsigned short cyl_hi
=passthru
->cylinder_hi
;
1250 // If values in Cyl-LO and Cyl-HI are unchanged, SMART status is good.
1251 if (cyl_lo
==0x4F && cyl_hi
==0xC2)
1254 // If values in Cyl-LO and Cyl-HI are as follows, SMART status is FAIL
1255 if (cyl_lo
==0xF4 && cyl_hi
==0x2C)
1258 // Any other values mean that something has gone wrong with the command
1259 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1260 printwarning(command
);
1270 // copy sector count register (one byte!) to return data
1271 if (command
==CHECK_POWER_MODE
)
1272 *data
=*(char *)&(passthru
->sector_count
);
1274 // look for nonexistent devices/ports
1275 if (command
==IDENTIFY
&& !nonempty((unsigned char *)data
, 512)) {
1285 int marvell_command_interface(int device
,
1286 smart_command_set command
,
1296 mvsata_scsi_cmd smart_command
;
1297 unsigned char *buff
= (unsigned char *)&smart_command
.cmd
[6];
1298 // See struct hd_drive_cmd_hdr in hdreg.h
1299 // buff[0]: ATA COMMAND CODE REGISTER
1300 // buff[1]: ATA SECTOR NUMBER REGISTER
1301 // buff[2]: ATA FEATURES REGISTER
1302 // buff[3]: ATA SECTOR COUNT REGISTER
1304 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
1305 memset(&smart_command
, 0, sizeof(smart_command
));
1306 smart_command
.inlen
= 540;
1307 smart_command
.outlen
= 540;
1308 smart_command
.cmd
[0] = 0xC; //Vendor-specific code
1309 smart_command
.cmd
[4] = 6; //command length
1311 buff
[0] = ATA_SMART_CMD
;
1313 case CHECK_POWER_MODE
:
1314 buff
[0]=ATA_CHECK_POWER_MODE
;
1317 buff
[2]=ATA_SMART_READ_VALUES
;
1320 case READ_THRESHOLDS
:
1321 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1322 copydata
=buff
[1]=buff
[3]=1;
1325 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1330 buff
[0]=ATA_IDENTIFY_DEVICE
;
1334 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1338 buff
[2]=ATA_SMART_ENABLE
;
1342 buff
[2]=ATA_SMART_DISABLE
;
1347 // this command only says if SMART is working. It could be
1348 // replaced with STATUS_CHECK below.
1349 buff
[2] = ATA_SMART_STATUS
;
1352 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1353 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1356 buff
[2]=ATA_SMART_AUTOSAVE
;
1357 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1359 case IMMEDIATE_OFFLINE
:
1360 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1364 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command
);
1368 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
1370 // We are now doing the HDIO_DRIVE_CMD type ioctl.
1371 if (ioctl(device
, SCSI_IOCTL_SEND_COMMAND
, (void *)&smart_command
))
1374 if (command
==CHECK_POWER_MODE
) {
1375 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
1376 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
1381 // Always succeed on a SMART status, as a disk that failed returned
1382 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
1383 if (command
== STATUS
)
1385 //Data returned is starting from 0 offset
1386 if (command
== STATUS_CHECK
)
1388 // Cyl low and Cyl high unchanged means "Good SMART status"
1389 if (buff
[4] == 0x4F && buff
[5] == 0xC2)
1391 // These values mean "Bad SMART status"
1392 if (buff
[4] == 0xF4 && buff
[5] == 0x2C)
1394 // We haven't gotten output that makes sense; print out some debugging info
1395 syserror("Error SMART Status command failed");
1396 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT
);
1397 pout("Register values returned from SMART Status command are:\n");
1398 pout("CMD =0x%02x\n",(int)buff
[0]);
1399 pout("FR =0x%02x\n",(int)buff
[1]);
1400 pout("NS =0x%02x\n",(int)buff
[2]);
1401 pout("SC =0x%02x\n",(int)buff
[3]);
1402 pout("CL =0x%02x\n",(int)buff
[4]);
1403 pout("CH =0x%02x\n",(int)buff
[5]);
1404 pout("SEL=0x%02x\n",(int)buff
[6]);
1409 memcpy(data
, buff
, 512);
1413 // this implementation is derived from ata_command_interface with a header
1414 // packing for highpoint linux driver ioctl interface
1416 // ioctl(fd,HPTIO_CTL,buff)
1419 // structure of hpt_buff
1420 // +----+----+----+----+--------------------.....---------------------+
1421 // | 1 | 2 | 3 | 4 | 5 |
1422 // +----+----+----+----+--------------------.....---------------------+
1424 // 1: The target controller [ int ( 4 Bytes ) ]
1425 // 2: The channel of the target controllee [ int ( 4 Bytes ) ]
1426 // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
1427 // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
1428 // 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
1429 // if no pmport device, set to 1 or leave blank
1430 // 5: data [ void * ( var leangth ) ]
1432 int highpoint_command_interface(int device
, smart_command_set command
,
1433 int select
, char *data
)
1435 unsigned char hpt_buff
[4*sizeof(int) + STRANGE_BUFFER_LENGTH
];
1436 unsigned int *hpt
= (unsigned int *)hpt_buff
;
1437 unsigned char *buff
= &hpt_buff
[4*sizeof(int)];
1439 const int HDIO_DRIVE_CMD_OFFSET
= 4;
1441 memset(hpt_buff
, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH
);
1442 hpt
[0] = con
->hpt_data
[0]; // controller id
1443 hpt
[1] = con
->hpt_data
[1]; // channel number
1444 hpt
[3] = con
->hpt_data
[2]; // pmport number
1446 buff
[0]=ATA_SMART_CMD
;
1448 case CHECK_POWER_MODE
:
1449 buff
[0]=ATA_CHECK_POWER_MODE
;
1453 buff
[2]=ATA_SMART_READ_VALUES
;
1457 case READ_THRESHOLDS
:
1458 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1463 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1471 buff
[0]=ATA_IDENTIFY_DEVICE
;
1476 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1481 buff
[2]=ATA_SMART_ENABLE
;
1485 buff
[2]=ATA_SMART_DISABLE
;
1489 buff
[2]=ATA_SMART_STATUS
;
1492 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1496 buff
[2]=ATA_SMART_AUTOSAVE
;
1499 case IMMEDIATE_OFFLINE
:
1500 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1504 buff
[1]=ATA_SMART_STATUS
;
1507 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
1508 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
1513 if (command
==WRITE_LOG
) {
1514 unsigned char task
[4*sizeof(int)+sizeof(ide_task_request_t
)+512];
1515 unsigned int *hpt
= (unsigned int *)task
;
1516 ide_task_request_t
*reqtask
= (ide_task_request_t
*)(&task
[4*sizeof(int)]);
1517 task_struct_t
*taskfile
= (task_struct_t
*)reqtask
->io_ports
;
1520 memset(task
, 0, sizeof(task
));
1522 hpt
[0] = con
->hpt_data
[0]; // controller id
1523 hpt
[1] = con
->hpt_data
[1]; // channel number
1524 hpt
[3] = con
->hpt_data
[2]; // pmport number
1525 hpt
[2] = HDIO_DRIVE_TASKFILE
; // real hd ioctl
1528 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
1529 taskfile
->sector_count
= 1;
1530 taskfile
->sector_number
= select
;
1531 taskfile
->low_cylinder
= 0x4f;
1532 taskfile
->high_cylinder
= 0xc2;
1533 taskfile
->device_head
= 0;
1534 taskfile
->command
= ATA_SMART_CMD
;
1536 reqtask
->data_phase
= TASKFILE_OUT
;
1537 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
1538 reqtask
->out_size
= 512;
1539 reqtask
->in_size
= 0;
1541 memcpy(task
+sizeof(ide_task_request_t
)+4*sizeof(int), data
, 512);
1543 if ((retval
=ioctl(device
, HPTIO_CTL
, task
))) {
1544 if (retval
==-EINVAL
)
1545 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
1551 if (command
==STATUS_CHECK
){
1553 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
1554 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
1558 hpt
[2] = HDIO_DRIVE_TASK
;
1560 if ((retval
=ioctl(device
, HPTIO_CTL
, hpt_buff
))) {
1561 if (retval
==-EINVAL
) {
1562 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
1563 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
1566 syserror("Error SMART Status command failed");
1570 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
1573 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
1576 syserror("Error SMART Status command failed");
1577 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
1578 pout("Register values returned from SMART Status command are:\n");
1579 pout("CMD=0x%02x\n",(int)buff
[0]);
1580 pout("FR =0x%02x\n",(int)buff
[1]);
1581 pout("NS =0x%02x\n",(int)buff
[2]);
1582 pout("SC =0x%02x\n",(int)buff
[3]);
1583 pout("CL =0x%02x\n",(int)buff
[4]);
1584 pout("CH =0x%02x\n",(int)buff
[5]);
1585 pout("SEL=0x%02x\n",(int)buff
[6]);
1590 if (command
==IDENTIFY
|| command
==PIDENTIFY
) {
1591 unsigned char deviceid
[4*sizeof(int)+512*sizeof(char)];
1592 unsigned int *hpt
= (unsigned int *)deviceid
;
1594 hpt
[0] = con
->hpt_data
[0]; // controller id
1595 hpt
[1] = con
->hpt_data
[1]; // channel number
1596 hpt
[3] = con
->hpt_data
[2]; // pmport number
1598 hpt
[2] = HDIO_GET_IDENTITY
;
1599 if (!ioctl(device
, HPTIO_CTL
, deviceid
) && (deviceid
[4*sizeof(int)] & 0x8000))
1600 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
1604 hpt
[2] = HDIO_DRIVE_CMD
;
1605 if ((ioctl(device
, HPTIO_CTL
, hpt_buff
)))
1608 if (command
==CHECK_POWER_MODE
)
1609 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
1612 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
1618 // Utility function for printing warnings
1619 void printwarning(smart_command_set command
){
1620 static int printed
[4]={0,0,0,0};
1621 const char* message
=
1622 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
1623 "applying a simple 3w-xxxx driver patch that can be found here:\n"
1624 PACKAGE_HOMEPAGE
"\n"
1625 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
1627 if (command
==AUTO_OFFLINE
&& !printed
[0]) {
1629 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message
);
1631 else if (command
==AUTOSAVE
&& !printed
[1]) {
1633 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message
);
1635 else if (command
==STATUS_CHECK
&& !printed
[2]) {
1637 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message
);
1639 else if (command
==WRITE_LOG
&& !printed
[3]) {
1641 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
1647 // Guess device type (ata or scsi) based on device name (Linux
1648 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
1649 // osst, nosst and sg.
1650 static const char * lin_dev_prefix
= "/dev/";
1651 static const char * lin_dev_ata_disk_plus
= "h";
1652 static const char * lin_dev_ata_devfs_disk_plus
= "ide/";
1653 static const char * lin_dev_scsi_devfs_disk_plus
= "scsi/";
1654 static const char * lin_dev_scsi_disk_plus
= "s";
1655 static const char * lin_dev_scsi_tape1
= "ns";
1656 static const char * lin_dev_scsi_tape2
= "os";
1657 static const char * lin_dev_scsi_tape3
= "nos";
1658 static const char * lin_dev_3ware_9000_char
= "twa";
1659 static const char * lin_dev_3ware_678k_char
= "twe";
1661 int guess_device_type(const char * dev_name
) {
1663 int dev_prefix_len
= strlen(lin_dev_prefix
);
1665 // if dev_name null, or string length zero
1666 if (!dev_name
|| !(len
= strlen(dev_name
)))
1667 return CONTROLLER_UNKNOWN
;
1669 // Remove the leading /dev/... if it's there
1670 if (!strncmp(lin_dev_prefix
, dev_name
, dev_prefix_len
)) {
1671 if (len
<= dev_prefix_len
)
1672 // if nothing else in the string, unrecognized
1673 return CONTROLLER_UNKNOWN
;
1674 // else advance pointer to following characters
1675 dev_name
+= dev_prefix_len
;
1678 // form /dev/h* or h*
1679 if (!strncmp(lin_dev_ata_disk_plus
, dev_name
,
1680 strlen(lin_dev_ata_disk_plus
)))
1681 return CONTROLLER_ATA
;
1683 // form /dev/ide/* or ide/*
1684 if (!strncmp(lin_dev_ata_devfs_disk_plus
, dev_name
,
1685 strlen(lin_dev_ata_devfs_disk_plus
)))
1686 return CONTROLLER_ATA
;
1688 // form /dev/s* or s*
1689 if (!strncmp(lin_dev_scsi_disk_plus
, dev_name
,
1690 strlen(lin_dev_scsi_disk_plus
)))
1691 return CONTROLLER_SCSI
;
1693 // form /dev/scsi/* or scsi/*
1694 if (!strncmp(lin_dev_scsi_devfs_disk_plus
, dev_name
,
1695 strlen(lin_dev_scsi_devfs_disk_plus
)))
1696 return CONTROLLER_SCSI
;
1698 // form /dev/ns* or ns*
1699 if (!strncmp(lin_dev_scsi_tape1
, dev_name
,
1700 strlen(lin_dev_scsi_tape1
)))
1701 return CONTROLLER_SCSI
;
1703 // form /dev/os* or os*
1704 if (!strncmp(lin_dev_scsi_tape2
, dev_name
,
1705 strlen(lin_dev_scsi_tape2
)))
1706 return CONTROLLER_SCSI
;
1708 // form /dev/nos* or nos*
1709 if (!strncmp(lin_dev_scsi_tape3
, dev_name
,
1710 strlen(lin_dev_scsi_tape3
)))
1711 return CONTROLLER_SCSI
;
1714 if (!strncmp(lin_dev_3ware_9000_char
, dev_name
,
1715 strlen(lin_dev_3ware_9000_char
)))
1716 return CONTROLLER_3WARE_9000_CHAR
;
1719 if (!strncmp(lin_dev_3ware_678k_char
, dev_name
,
1720 strlen(lin_dev_3ware_678k_char
)))
1721 return CONTROLLER_3WARE_678K_CHAR
;
1723 // we failed to recognize any of the forms
1724 return CONTROLLER_UNKNOWN
;
1730 [ed@firestorm ed
]$ ls
-l
/dev
/discs
1732 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc0
-> ../ide
/host2
/bus0
/target0
/lun0
/
1733 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc1
-> ../ide
/host2
/bus1
/target0
/lun0
/
1734 [ed@firestorm ed
]$ ls
-l dev
/ide
/host*/bus*/target*/lun*/disc
1735 ls
: dev
/ide
/host*/bus*/target*/lun*/disc
: No such file
or directory
1736 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/host*/bus*/target*/lun*/disc
1737 brw
------- 1 root root
33, 0 Dec
31 1969 /dev
/ide
/host2
/bus0
/target0
/lun0
/disc
1738 brw
------- 1 root root
34, 0 Dec
31 1969 /dev
/ide
/host2
/bus1
/target0
/lun0
/disc
1739 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/c
*b
*t
*u
*
1740 ls
: /dev
/ide
/c
*b
*t
*u
*: No such file
or directory
1742 Script done on Fri Nov
7 13:46:28 2003