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>
69 #define ENOTSUP ENOSYS
71 typedef unsigned long long u8
;
73 #define ARGUSED(x) ((void)(x))
75 static const char *filenameandversion
="$Id: os_linux.c,v 1.82 2006/04/12 16:28:56 ballen4705 Exp $";
77 const char *os_XXXX_c_cvsid
="$Id: os_linux.c,v 1.82 2006/04/12 16:28:56 ballen4705 Exp $" \
78 ATACMDS_H_CVSID CONFIG_H_CVSID INT64_H_CVSID OS_LINUX_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID
;
80 // to hold onto exit code for atexit routine
81 extern int exitstatus
;
83 // global variable holding byte count of allocated memory
84 extern long long bytes
;
88 /* This function will setup and fix device nodes for a 3ware controller. */
89 #define MAJOR_STRING_LENGTH 3
90 #define DEVICE_STRING_LENGTH 32
91 #define NODE_STRING_LENGTH 16
92 int setup_3ware_nodes(char *nodename
, char *driver_name
) {
95 char majorstring
[MAJOR_STRING_LENGTH
+1];
96 char device_name
[DEVICE_STRING_LENGTH
+1];
97 char nodestring
[NODE_STRING_LENGTH
];
101 /* First try to open up /proc/devices */
102 if (!(file
= fopen("/proc/devices", "r"))) {
103 pout("Error opening /proc/devices to check/create 3ware device nodes\n");
105 return 0; // don't fail here: user might not have /proc !
108 /* Attempt to get device major number */
109 while (EOF
!= fscanf(file
, "%3s %32s", majorstring
, device_name
)) {
110 majorstring
[MAJOR_STRING_LENGTH
]='\0';
111 device_name
[DEVICE_STRING_LENGTH
]='\0';
112 if (!strncmp(device_name
, nodename
, DEVICE_STRING_LENGTH
)) {
113 tw_major
= atoi(majorstring
);
119 /* See if we found a major device number */
121 pout("No major number for /dev/%s listed in /proc/devices. Is the %s driver loaded?\n", nodename
, driver_name
);
125 /* Now check if nodes are correct */
126 for (index
=0; index
<16; index
++) {
127 sprintf(nodestring
, "/dev/%s%d", nodename
, index
);
129 /* Try to stat the node */
130 if ((stat(nodestring
, &stat_buf
))) {
131 /* Create a new node if it doesn't exist */
132 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
133 pout("problem creating 3ware device nodes %s", nodestring
);
139 /* See if nodes major and minor numbers are correct */
140 if ((tw_major
!= (int)(major(stat_buf
.st_rdev
))) ||
141 (index
!= (int)(minor(stat_buf
.st_rdev
))) ||
142 (!S_ISCHR(stat_buf
.st_mode
))) {
144 /* Delete the old node */
145 if (unlink(nodestring
)) {
146 pout("problem unlinking stale 3ware device node %s", nodestring
);
151 /* Make a new node */
152 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
153 pout("problem creating 3ware device nodes %s", nodestring
);
162 // equivalent to open(path, flags)
163 int deviceopen(const char *pathname
, char *type
){
164 if (!strcmp(type
,"SCSI")) {
165 int fd
= open(pathname
, O_RDWR
| O_NONBLOCK
);
166 if (fd
< 0 && errno
== EROFS
)
167 fd
= open(pathname
, O_RDONLY
| O_NONBLOCK
);
170 else if (!strcmp(type
,"ATA"))
171 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
172 else if (!strcmp(type
,"ATA_3WARE_9000")) {
173 // the device nodes for this controller are dynamically assigned,
174 // so we need to check that they exist with the correct major
175 // numbers and if not, create them
176 if (setup_3ware_nodes("twa", "3w-9xxx")) {
181 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
183 else if (!strcmp(type
,"ATA_3WARE_678K")) {
184 // the device nodes for this controller are dynamically assigned,
185 // so we need to check that they exist with the correct major
186 // numbers and if not, create them
187 if (setup_3ware_nodes("twe", "3w-xxxx")) {
192 return open(pathname
, O_RDONLY
| O_NONBLOCK
);
198 // equivalent to close(file descriptor)
199 int deviceclose(int fd
){
203 // print examples for smartctl
204 void print_smartctl_examples(){
205 printf("=================================================== SMARTCTL EXAMPLES =====\n\n");
206 #ifdef HAVE_GETOPT_LONG
208 " smartctl --all /dev/hda (Prints all SMART information)\n\n"
209 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n"
210 " (Enables SMART on first disk)\n\n"
211 " smartctl --test=long /dev/hda (Executes extended disk self-test)\n\n"
212 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/hda\n"
213 " (Prints Self-Test & Attribute errors)\n"
214 " smartctl --all --device=3ware,2 /dev/sda\n"
215 " smartctl --all --device=3ware,2 /dev/twe0\n"
216 " smartctl --all --device=3ware,2 /dev/twa0\n"
217 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
221 " smartctl -a /dev/hda (Prints all SMART information)\n"
222 " smartctl -s on -o on -S on /dev/hda (Enables SMART on first disk)\n"
223 " smartctl -t long /dev/hda (Executes extended disk self-test)\n"
224 " smartctl -A -l selftest -q errorsonly /dev/hda\n"
225 " (Prints Self-Test & Attribute errors)\n"
226 " smartctl -a -d 3ware,2 /dev/sda\n"
227 " smartctl -a -d 3ware,2 /dev/twa0\n"
228 " smartctl -a -d 3ware,2 /dev/twe0\n"
229 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
236 // we are going to take advantage of the fact that Linux's devfs will only
237 // have device entries for devices that exist. So if we get the equivalent of
238 // ls /dev/hd[a-t], we have all the ATA devices on the system
240 // If any errors occur, leave errno set as it was returned by the
241 // system call, and return <0.
242 int get_dev_names(char*** names
, const char* pattern
, const char* name
, int max
) {
243 int n
= 0, retglob
, i
, lim
;
247 memset(&globbuf
, 0, sizeof(globbuf
));
249 // in case of non-clean exit
252 // Use glob to look for any directory entries matching the pattern
253 if ((retglob
=glob(pattern
, GLOB_ERR
, NULL
, &globbuf
))) {
255 // glob failed: free memory and return
258 if (retglob
==GLOB_NOMATCH
){
259 pout("glob(3) found no matches for pattern %s\n", pattern
);
263 if (retglob
==GLOB_NOSPACE
)
264 pout("glob(3) ran out of memory matching pattern %s\n", pattern
);
265 #ifdef GLOB_ABORTED // missing in old versions of glob.h
266 else if (retglob
==GLOB_ABORTED
)
267 pout("glob(3) aborted matching pattern %s\n", pattern
);
270 pout("Unexplained error in glob(3) of pattern %s\n", pattern
);
275 // did we find too many paths?
276 lim
= ((int)globbuf
.gl_pathc
< max
) ? (int)globbuf
.gl_pathc
: max
;
277 if (lim
< (int)globbuf
.gl_pathc
)
278 pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n",
279 (int)globbuf
.gl_pathc
, max
, pattern
, (int)(globbuf
.gl_pathc
-max
));
281 // allocate space for up to lim number of ATA devices
282 if (!(mp
= (char **)calloc(lim
, sizeof(char*)))){
283 pout("Out of memory constructing scan device list\n");
287 // now step through the list returned by glob. If not a link, copy
288 // to list. If it is a link, evaluate it and see if the path ends
290 for (i
=0; i
<lim
; i
++){
293 // prepare a buffer for storing the link
296 // see if path is a link
297 retlink
=readlink(globbuf
.gl_pathv
[i
], linkbuf
, 1023);
299 // if not a link (or a strange link), keep it
300 if (retlink
<=0 || retlink
>1023)
301 mp
[n
++] = CustomStrDup(globbuf
.gl_pathv
[i
], 1, __LINE__
, filenameandversion
);
303 // or if it's a link that points to a disc, follow it
305 linkbuf
[retlink
]='\0';
306 if ((p
=strrchr(linkbuf
,'/')) && !strcmp(p
+1, "disc"))
307 // This is the branch of the code that gets followed if we are
308 // using devfs WITH traditional compatibility links. In this
309 // case, we add the traditional device name to the list that
311 mp
[n
++] = CustomStrDup(globbuf
.gl_pathv
[i
], 1, __LINE__
, filenameandversion
);
313 // This is the branch of the code that gets followed if we are
314 // using devfs WITHOUT traditional compatibility links. In
315 // this case, we check that the link to the directory is of
316 // the correct type, and then append "disc" to it.
317 char tmpname
[1024]={0};
318 char *type
=strcmp(name
,"ATA")?"scsi":"ide";
319 if (strstr(linkbuf
, type
)){
320 snprintf(tmpname
, 1024, "%s/disc", globbuf
.gl_pathv
[i
]);
321 mp
[n
++] = CustomStrDup(tmpname
, 1, __LINE__
, filenameandversion
);
327 // free memory, track memory usage
329 mp
= realloc(mp
,n
*(sizeof(char*)));
330 bytes
+= n
*(sizeof(char*));
332 // and set up return values
337 // makes a list of device names to scan, for either ATA or SCSI
338 // devices. Return -1 if no memory remaining, else the number of
339 // devices on the list, which can be >=0.
340 int make_device_names (char*** devlist
, const char* name
) {
344 // for testing case where no device names are found
348 if (!strcmp(name
,"SCSI"))
349 retval
=get_dev_names(devlist
,"/dev/sd[a-z]", name
, maxdev
=26);
350 else if (!strcmp(name
,"ATA"))
351 retval
=get_dev_names(devlist
,"/dev/hd[a-t]", name
, maxdev
=20);
353 // don't recognize disk type!
356 // if we found traditional links, we are done
360 // else look for devfs entries without traditional links
361 return get_dev_names(devlist
,"/dev/discs/disc*", name
, maxdev
);
366 // This is an interface routine meant to isolate the OS dependent
367 // parts of the code, and to provide a debugging interface. Each
368 // different port and OS needs to provide it's own interface. This
370 // DETAILED DESCRIPTION OF ARGUMENTS
371 // device: is the file descriptor provided by open()
372 // command: defines the different operations.
373 // select: additional input data if needed (which log, which type of
375 // data: location to write output data, if needed (512 bytes).
376 // Note: not all commands use all arguments.
378 // -1 if the command failed
379 // 0 if the command succeeded,
380 // STATUS_CHECK routine:
381 // -1 if the command failed
382 // 0 if the command succeeded and disk SMART status is "OK"
383 // 1 if the command succeeded and disk SMART status is "FAILING"
386 // huge value of buffer size needed because HDIO_DRIVE_CMD assumes
387 // that buff[3] is the data size. Since the ATA_SMART_AUTOSAVE and
388 // ATA_SMART_AUTO_OFFLINE use values of 0xf1 and 0xf8 we need the space.
389 // Otherwise a 4+512 byte buffer would be enough.
390 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
392 int ata_command_interface(int device
, smart_command_set command
, int select
, char *data
){
393 unsigned char buff
[STRANGE_BUFFER_LENGTH
];
394 // positive: bytes to write to caller. negative: bytes to READ from
395 // caller. zero: non-data command
398 const int HDIO_DRIVE_CMD_OFFSET
= 4;
400 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
401 // buff[0]: ATA COMMAND CODE REGISTER
402 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
403 // buff[2]: ATA FEATURES REGISTER
404 // buff[3]: ATA SECTOR COUNT REGISTER
406 // Note that on return:
407 // buff[2] contains the ATA SECTOR COUNT REGISTER
409 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
410 memset(buff
, 0, STRANGE_BUFFER_LENGTH
);
412 buff
[0]=ATA_SMART_CMD
;
414 case CHECK_POWER_MODE
:
415 buff
[0]=ATA_CHECK_POWER_MODE
;
419 buff
[2]=ATA_SMART_READ_VALUES
;
423 case READ_THRESHOLDS
:
424 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
429 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
437 buff
[0]=ATA_IDENTIFY_DEVICE
;
442 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
447 buff
[2]=ATA_SMART_ENABLE
;
451 buff
[2]=ATA_SMART_DISABLE
;
455 // this command only says if SMART is working. It could be
456 // replaced with STATUS_CHECK below.
457 buff
[2]=ATA_SMART_STATUS
;
460 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
461 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
464 buff
[2]=ATA_SMART_AUTOSAVE
;
465 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
467 case IMMEDIATE_OFFLINE
:
468 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
472 // This command uses HDIO_DRIVE_TASK and has different syntax than
473 // the other commands.
474 buff
[1]=ATA_SMART_STATUS
;
477 pout("Unrecognized command %d in linux_ata_command_interface()\n"
478 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
483 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
484 // only ioctl() that can be used to WRITE data to the disk.
485 if (command
==WRITE_LOG
) {
486 unsigned char task
[sizeof(ide_task_request_t
)+512];
487 ide_task_request_t
*reqtask
=(ide_task_request_t
*) task
;
488 task_struct_t
*taskfile
=(task_struct_t
*) reqtask
->io_ports
;
491 memset(task
, 0, sizeof(task
));
494 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
495 taskfile
->sector_count
= 1;
496 taskfile
->sector_number
= select
;
497 taskfile
->low_cylinder
= 0x4f;
498 taskfile
->high_cylinder
= 0xc2;
499 taskfile
->device_head
= 0;
500 taskfile
->command
= ATA_SMART_CMD
;
502 reqtask
->data_phase
= TASKFILE_OUT
;
503 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
504 reqtask
->out_size
= 512;
505 reqtask
->in_size
= 0;
507 // copy user data into the task request structure
508 memcpy(task
+sizeof(ide_task_request_t
), data
, 512);
510 if ((retval
=ioctl(device
, HDIO_DRIVE_TASKFILE
, task
))) {
512 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
518 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
520 if (command
==STATUS_CHECK
){
523 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
524 // have to read the IDE driver source code. Sigh.
525 // buff[0]: ATA COMMAND CODE REGISTER
526 // buff[1]: ATA FEATURES REGISTER
527 // buff[2]: ATA SECTOR_COUNT
528 // buff[3]: ATA SECTOR NUMBER
529 // buff[4]: ATA CYL LO REGISTER
530 // buff[5]: ATA CYL HI REGISTER
531 // buff[6]: ATA DEVICE HEAD
533 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
534 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
538 if ((retval
=ioctl(device
, HDIO_DRIVE_TASK
, buff
))) {
539 if (retval
==-EINVAL
) {
540 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
541 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
544 syserror("Error SMART Status command failed");
548 // Cyl low and Cyl high unchanged means "Good SMART status"
549 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
552 // These values mean "Bad SMART status"
553 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
556 // We haven't gotten output that makes sense; print out some debugging info
557 syserror("Error SMART Status command failed");
558 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
559 pout("Register values returned from SMART Status command are:\n");
560 pout("CMD=0x%02x\n",(int)buff
[0]);
561 pout("FR =0x%02x\n",(int)buff
[1]);
562 pout("NS =0x%02x\n",(int)buff
[2]);
563 pout("SC =0x%02x\n",(int)buff
[3]);
564 pout("CL =0x%02x\n",(int)buff
[4]);
565 pout("CH =0x%02x\n",(int)buff
[5]);
566 pout("SEL=0x%02x\n",(int)buff
[6]);
571 // Note to people doing ports to other OSes -- don't worry about
572 // this block -- you can safely ignore it. I have put it here
573 // because under linux when you do IDENTIFY DEVICE to a packet
574 // device, it generates an ugly kernel syslog error message. This
575 // is harmless but frightens users. So this block detects packet
576 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
579 // If you read only the ATA specs, it appears as if a packet device
580 // *might* respond to the IDENTIFY DEVICE command. This is
581 // misleading - it's because around the time that SFF-8020 was
582 // incorporated into the ATA-3/4 standard, the ATA authors were
583 // sloppy. See SFF-8020 and you will see that ATAPI devices have
584 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
585 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
586 if (command
==IDENTIFY
|| command
==PIDENTIFY
){
587 unsigned short deviceid
[256];
588 // check the device identity, as seen when the system was booted
589 // or the device was FIRST registered. This will not be current
590 // if the user has subsequently changed some of the parameters. If
591 // device is a packet device, swap the command interpretations.
592 if (!ioctl(device
, HDIO_GET_IDENTITY
, deviceid
) && (deviceid
[0] & 0x8000))
593 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
597 // We are now doing the HDIO_DRIVE_CMD type ioctl.
598 if ((ioctl(device
, HDIO_DRIVE_CMD
, buff
)))
601 // CHECK POWER MODE command returns information in the Sector Count
602 // register (buff[3]). Copy to return data buffer.
603 if (command
==CHECK_POWER_MODE
)
604 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
606 // if the command returns data then copy it back
608 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
613 // >>>>>> Start of general SCSI specific linux code
615 /* Linux specific code.
616 * Historically smartmontools (and smartsuite before it) used the
617 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
618 * nodes that use the SCSI subsystem. A better interface has been available
619 * via the SCSI generic (sg) driver but this involves the extra step of
620 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
621 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
622 * the sg driver have become available via the SG_IO ioctl which is available
623 * on all SCSI devices (on SCSI tape devices from lk 2.6.6).
624 * So the strategy below is to find out if the SG_IO ioctl is available and
625 * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl.
626 * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */
628 #define MAX_DXFER_LEN 1024 /* can be increased if necessary */
629 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
630 #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
631 #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
632 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
633 #define LSCSI_DRIVER_TIMEOUT 0x6
634 #define LSCSI_DID_TIME_OUT 0x3
635 #define LSCSI_DID_BUS_BUSY 0x2
636 #define LSCSI_DID_NO_CONNECT 0x1
638 #ifndef SCSI_IOCTL_SEND_COMMAND
639 #define SCSI_IOCTL_SEND_COMMAND 1
642 #define SG_IO_PRESENT_UNKNOWN 0
643 #define SG_IO_PRESENT_YES 1
644 #define SG_IO_PRESENT_NO 2
646 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
648 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
);
650 static int sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
652 /* Preferred implementation for issuing SCSI commands in linux. This
653 * function uses the SG_IO ioctl. Return 0 if command issued successfully
654 * (various status values should still be checked). If the SCSI command
655 * cannot be issued then a negative errno value is returned. */
656 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
660 ARGUSED(dev_fd
); ARGUSED(iop
); ARGUSED(report
);
663 struct sg_io_hdr io_hdr
;
667 const unsigned char * ucp
= iop
->cmnd
;
670 const int sz
= (int)sizeof(buff
);
672 np
= scsi_get_opcode_name(ucp
[0]);
673 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
674 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
675 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
677 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
678 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
680 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
681 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
682 (trunc
? " [only first 256 bytes shown]" : ""));
683 dStrHex((const char *)iop
->dxferp
,
684 (trunc
? 256 : iop
->dxfer_len
) , 1);
687 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
690 memset(&io_hdr
, 0, sizeof(struct sg_io_hdr
));
691 io_hdr
.interface_id
= 'S';
692 io_hdr
.cmd_len
= iop
->cmnd_len
;
693 io_hdr
.mx_sb_len
= iop
->max_sense_len
;
694 io_hdr
.dxfer_len
= iop
->dxfer_len
;
695 io_hdr
.dxferp
= iop
->dxferp
;
696 io_hdr
.cmdp
= iop
->cmnd
;
697 io_hdr
.sbp
= iop
->sensep
;
698 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
699 defaults to 60 seconds. */
700 io_hdr
.timeout
= ((0 == iop
->timeout
) ? 60 : iop
->timeout
) * 1000;
701 switch (iop
->dxfer_dir
) {
703 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
705 case DXFER_FROM_DEVICE
:
706 io_hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
708 case DXFER_TO_DEVICE
:
709 io_hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
712 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
715 iop
->resp_sense_len
= 0;
716 iop
->scsi_status
= 0;
718 if (ioctl(dev_fd
, SG_IO
, &io_hdr
) < 0) {
719 if (report
&& (! unknown
))
720 pout(" SG_IO ioctl failed, errno=%d [%s]\n", errno
,
725 pout(" scsi_status=0x%x, host_status=0x%x, driver_status=0x%x\n"
726 " info=0x%x duration=%d milliseconds\n", io_hdr
.status
,
727 io_hdr
.host_status
, io_hdr
.driver_status
, io_hdr
.info
,
730 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
731 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
733 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
734 (trunc
? " [only first 256 bytes shown]" : ""));
735 dStrHex((const char*)iop
->dxferp
,
736 (trunc
? 256 : iop
->dxfer_len
) , 1);
740 iop
->resid
= io_hdr
.resid
;
741 iop
->scsi_status
= io_hdr
.status
;
743 if (io_hdr
.info
| SG_INFO_CHECK
) { /* error or warning */
744 int masked_driver_status
= (LSCSI_DRIVER_MASK
& io_hdr
.driver_status
);
746 if (0 != io_hdr
.host_status
) {
747 if ((LSCSI_DID_NO_CONNECT
== io_hdr
.host_status
) ||
748 (LSCSI_DID_BUS_BUSY
== io_hdr
.host_status
) ||
749 (LSCSI_DID_TIME_OUT
== io_hdr
.host_status
))
752 return -EIO
; /* catch all */
754 if (0 != masked_driver_status
) {
755 if (LSCSI_DRIVER_TIMEOUT
== masked_driver_status
)
757 else if (LSCSI_DRIVER_SENSE
!= masked_driver_status
)
760 if (LSCSI_DRIVER_SENSE
== masked_driver_status
)
761 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
762 iop
->resp_sense_len
= io_hdr
.sb_len_wr
;
763 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
764 iop
->sensep
&& (iop
->resp_sense_len
> 0)) {
766 pout(" >>> Sense buffer, len=%d:\n",
767 (int)iop
->resp_sense_len
);
768 dStrHex((const char *)iop
->sensep
, iop
->resp_sense_len
, 1);
772 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
773 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
774 iop
->scsi_status
, iop
->sensep
[2] & 0xf, iop
->sensep
[12],
778 pout(" status=0x%x\n", iop
->scsi_status
);
785 struct linux_ioctl_send_command
789 UINT8 buff
[MAX_DXFER_LEN
+ 16];
792 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
793 * support: CDB length (guesses it from opcode), resid and timeout.
794 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
795 * to 2 hours in order to allow long foreground extended self tests. */
796 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
798 struct linux_ioctl_send_command wrk
;
799 int status
, buff_offset
;
802 memcpy(wrk
.buff
, iop
->cmnd
, iop
->cmnd_len
);
803 buff_offset
= iop
->cmnd_len
;
806 const unsigned char * ucp
= iop
->cmnd
;
809 const int sz
= (int)sizeof(buff
);
811 np
= scsi_get_opcode_name(ucp
[0]);
812 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
813 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
814 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
816 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
817 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
819 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
820 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
821 (trunc
? " [only first 256 bytes shown]" : ""));
822 dStrHex((const char *)iop
->dxferp
,
823 (trunc
? 256 : iop
->dxfer_len
) , 1);
826 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
829 switch (iop
->dxfer_dir
) {
834 case DXFER_FROM_DEVICE
:
836 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
838 wrk
.outbufsize
= iop
->dxfer_len
;
840 case DXFER_TO_DEVICE
:
841 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
843 memcpy(wrk
.buff
+ buff_offset
, iop
->dxferp
, iop
->dxfer_len
);
844 wrk
.inbufsize
= iop
->dxfer_len
;
848 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
851 iop
->resp_sense_len
= 0;
852 iop
->scsi_status
= 0;
854 status
= ioctl(dev_fd
, SCSI_IOCTL_SEND_COMMAND
, &wrk
);
857 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
858 errno
, strerror(errno
));
864 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
865 memcpy(iop
->dxferp
, wrk
.buff
, iop
->dxfer_len
);
867 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
869 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
870 (trunc
? " [only first 256 bytes shown]" : ""));
871 dStrHex((const char*)iop
->dxferp
,
872 (trunc
? 256 : iop
->dxfer_len
) , 1);
877 iop
->scsi_status
= status
& 0x7e; /* bits 0 and 7 used to be for vendors */
878 if (LSCSI_DRIVER_SENSE
== ((status
>> 24) & 0xf))
879 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
880 len
= (SEND_IOCTL_RESP_SENSE_LEN
< iop
->max_sense_len
) ?
881 SEND_IOCTL_RESP_SENSE_LEN
: iop
->max_sense_len
;
882 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
883 iop
->sensep
&& (len
> 0)) {
884 memcpy(iop
->sensep
, wrk
.buff
, len
);
885 iop
->resp_sense_len
= len
;
887 pout(" >>> Sense buffer, len=%d:\n", (int)len
);
888 dStrHex((const char *)wrk
.buff
, len
, 1);
892 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
893 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status
& 0xff,
894 wrk
.buff
[2] & 0xf, wrk
.buff
[12], wrk
.buff
[13]);
897 pout(" status=0x%x\n", status
);
899 if (iop
->scsi_status
> 0)
903 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
905 return -EIO
; /* give up, assume no device there */
909 /* SCSI command transmission interface function, linux version.
910 * Returns 0 if SCSI command successfully launched and response
911 * received. Even when 0 is returned the caller should check
912 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
913 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
914 * (e.g. device not present or timeout) or some other problem
915 * (e.g. timeout) then returns a negative errno value */
916 int do_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
920 /* implementation relies on static sg_io_state variable. If not
921 * previously set tries the SG_IO ioctl. If that succeeds assume
922 * that SG_IO ioctl functional. If it fails with an errno value
923 * other than ENODEV (no device) or permission then assume
924 * SCSI_IOCTL_SEND_COMMAND is the only option. */
925 switch (sg_io_state
) {
926 case SG_IO_PRESENT_UNKNOWN
:
927 /* ignore report argument */
928 if (0 == (res
= sg_io_cmnd_io(dev_fd
, iop
, report
, 1))) {
929 sg_io_state
= SG_IO_PRESENT_YES
;
931 } else if ((-ENODEV
== res
) || (-EACCES
== res
) || (-EPERM
== res
))
932 return res
; /* wait until we see a device */
933 sg_io_state
= SG_IO_PRESENT_NO
;
934 /* drop through by design */
935 case SG_IO_PRESENT_NO
:
936 return sisc_cmnd_io(dev_fd
, iop
, report
);
937 case SG_IO_PRESENT_YES
:
938 return sg_io_cmnd_io(dev_fd
, iop
, report
, 0);
940 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state
);
941 sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
942 return -EIO
; /* report error and reset state */
946 // >>>>>> End of general SCSI specific linux code
950 void printwarning(smart_command_set command
);
953 // This is an interface routine meant to isolate the OS dependent
954 // parts of the code, and to provide a debugging interface. Each
955 // different port and OS needs to provide it's own interface. This
956 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
957 // commands to be passed through the SCSI driver.
958 // DETAILED DESCRIPTION OF ARGUMENTS
959 // fd: is the file descriptor provided by open()
960 // disknum is the disk number (0 to 15) in the RAID array
961 // escalade_type indicates the type of controller type, and if scsi or char interface is used
962 // command: defines the different operations.
963 // select: additional input data if needed (which log, which type of
965 // data: location to write output data, if needed (512 bytes).
966 // Note: not all commands use all arguments.
968 // -1 if the command failed
969 // 0 if the command succeeded,
970 // STATUS_CHECK routine:
971 // -1 if the command failed
972 // 0 if the command succeeded and disk SMART status is "OK"
973 // 1 if the command succeeded and disk SMART status is "FAILING"
976 /* 512 is the max payload size: increase if needed */
977 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
978 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
979 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
980 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
982 int escalade_command_interface(int fd
, int disknum
, int escalade_type
, smart_command_set command
, int select
, char *data
){
984 // return value and buffer for ioctl()
985 int ioctlreturn
, readdata
=0;
987 // Used by both the SCSI and char interfaces
988 TW_Passthru
*passthru
=NULL
;
989 char ioctl_buffer
[TW_IOCTL_BUFFER_SIZE
];
991 // only used for SCSI device interface
992 TW_Ioctl
*tw_ioctl
=NULL
;
993 TW_Output
*tw_output
=NULL
;
995 // only used for 6000/7000/8000 char device interface
996 TW_New_Ioctl
*tw_ioctl_char
=NULL
;
998 // only used for 9000 character device interface
999 TW_Ioctl_Buf_Apache
*tw_ioctl_apache
=NULL
;
1001 memset(ioctl_buffer
, 0, TW_IOCTL_BUFFER_SIZE
);
1003 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
) {
1004 tw_ioctl_apache
= (TW_Ioctl_Buf_Apache
*)ioctl_buffer
;
1005 tw_ioctl_apache
->driver_command
.control_code
= TW_IOCTL_FIRMWARE_PASS_THROUGH
;
1006 tw_ioctl_apache
->driver_command
.buffer_length
= 512; /* payload size */
1007 passthru
= (TW_Passthru
*)&(tw_ioctl_apache
->firmware_command
.command
.oldcommand
);
1009 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
) {
1010 tw_ioctl_char
= (TW_New_Ioctl
*)ioctl_buffer
;
1011 tw_ioctl_char
->data_buffer_length
= 512;
1012 passthru
= (TW_Passthru
*)&(tw_ioctl_char
->firmware_command
);
1014 else if (escalade_type
==CONTROLLER_3WARE_678K
) {
1015 tw_ioctl
= (TW_Ioctl
*)ioctl_buffer
;
1016 tw_ioctl
->cdb
[0] = TW_IOCTL
;
1017 tw_ioctl
->opcode
= TW_ATA_PASSTHRU
;
1018 tw_ioctl
->input_length
= 512; // correct even for non-data commands
1019 tw_ioctl
->output_length
= 512; // correct even for non-data commands
1020 tw_output
= (TW_Output
*)tw_ioctl
;
1021 passthru
= (TW_Passthru
*)&(tw_ioctl
->input_data
);
1024 pout("Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1025 "Please contact " PACKAGE_BUGREPORT
"\n", escalade_type
, disknum
);
1030 // Same for (almost) all commands - but some reset below
1031 passthru
->byte0
.opcode
= TW_OP_ATA_PASSTHRU
;
1032 passthru
->request_id
= 0xFF;
1033 passthru
->byte3
.aport
= disknum
;
1034 passthru
->byte3
.host_id
= 0;
1035 passthru
->status
= 0;
1036 passthru
->flags
= 0x1;
1037 passthru
->drive_head
= 0x0;
1038 passthru
->sector_num
= 0;
1040 // All SMART commands use this CL/CH signature. These are magic
1041 // values from the ATA specifications.
1042 passthru
->cylinder_lo
= 0x4F;
1043 passthru
->cylinder_hi
= 0xC2;
1045 // SMART ATA COMMAND REGISTER value
1046 passthru
->command
= ATA_SMART_CMD
;
1048 // Is this a command that reads or returns 512 bytes?
1049 // passthru->param values are:
1050 // 0x0 - non data command without TFR write check,
1051 // 0x8 - non data command with TFR write check,
1052 // 0xD - data command that returns data to host from device
1053 // 0xF - data command that writes data from host to device
1054 // passthru->size values are 0x5 for non-data and 0x07 for data
1055 if (command
== READ_VALUES
||
1056 command
== READ_THRESHOLDS
||
1057 command
== READ_LOG
||
1058 command
== IDENTIFY
||
1059 command
== WRITE_LOG
) {
1061 passthru
->byte0
.sgloff
= 0x5;
1062 passthru
->size
= 0x7;
1063 passthru
->param
= 0xD;
1064 passthru
->sector_count
= 0x1;
1065 // For 64-bit to work correctly, up the size of the command packet
1066 // in dwords by 1 to account for the 64-bit single sgl 'address'
1067 // field. Note that this doesn't agree with the typedefs but it's
1068 // right (agree with kernel driver behavior/typedefs).
1069 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
&& sizeof(long)==8)
1073 // Non data command -- but doesn't use large sector
1074 // count register values.
1075 passthru
->byte0
.sgloff
= 0x0;
1076 passthru
->size
= 0x5;
1077 passthru
->param
= 0x8;
1078 passthru
->sector_count
= 0x0;
1081 // Now set ATA registers depending upon command
1083 case CHECK_POWER_MODE
:
1084 passthru
->command
= ATA_CHECK_POWER_MODE
;
1085 passthru
->features
= 0;
1086 passthru
->cylinder_lo
= 0;
1087 passthru
->cylinder_hi
= 0;
1090 passthru
->features
= ATA_SMART_READ_VALUES
;
1092 case READ_THRESHOLDS
:
1093 passthru
->features
= ATA_SMART_READ_THRESHOLDS
;
1096 passthru
->features
= ATA_SMART_READ_LOG_SECTOR
;
1097 // log number to return
1098 passthru
->sector_num
= select
;
1101 if (escalade_type
== CONTROLLER_3WARE_9000_CHAR
)
1102 memcpy((unsigned char *)tw_ioctl_apache
->data_buffer
, data
, 512);
1103 else if (escalade_type
== CONTROLLER_3WARE_678K_CHAR
)
1104 memcpy((unsigned char *)tw_ioctl_char
->data_buffer
, data
, 512);
1106 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1107 // memcpy(tw_output->output_data, data, 512);
1108 printwarning(command
);
1113 passthru
->features
= ATA_SMART_WRITE_LOG_SECTOR
;
1114 passthru
->sector_count
= 1;
1115 passthru
->sector_num
= select
;
1116 passthru
->param
= 0xF; // PIO data write
1119 // ATA IDENTIFY DEVICE
1120 passthru
->command
= ATA_IDENTIFY_DEVICE
;
1121 passthru
->features
= 0;
1122 passthru
->cylinder_lo
= 0;
1123 passthru
->cylinder_hi
= 0;
1126 // 3WARE controller can NOT have packet device internally
1127 pout("WARNING - NO DEVICE FOUND ON 3WARE CONTROLLER (disk %d)\n", disknum
);
1128 pout("Note: /dev/sdX many need to be replaced with /dev/tweN or /dev/twaN\n");
1132 passthru
->features
= ATA_SMART_ENABLE
;
1135 passthru
->features
= ATA_SMART_DISABLE
;
1138 passthru
->features
= ATA_SMART_AUTO_OFFLINE
;
1139 // Enable or disable?
1140 passthru
->sector_count
= select
;
1143 passthru
->features
= ATA_SMART_AUTOSAVE
;
1144 // Enable or disable?
1145 passthru
->sector_count
= select
;
1147 case IMMEDIATE_OFFLINE
:
1148 passthru
->features
= ATA_SMART_IMMEDIATE_OFFLINE
;
1149 // What test type to run?
1150 passthru
->sector_num
= select
;
1153 passthru
->features
= ATA_SMART_STATUS
;
1156 // This is JUST to see if SMART is enabled, by giving SMART status
1157 // command. But it doesn't say if status was good, or failing.
1158 // See below for the difference.
1159 passthru
->features
= ATA_SMART_STATUS
;
1162 pout("Unrecognized command %d in linux_3ware_command_interface(disk %d)\n"
1163 "Please contact " PACKAGE_BUGREPORT
"\n", command
, disknum
);
1168 // Now send the command down through an ioctl()
1169 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1170 ioctlreturn
=ioctl(fd
, TW_IOCTL_FIRMWARE_PASS_THROUGH
, tw_ioctl_apache
);
1171 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1172 ioctlreturn
=ioctl(fd
, TW_CMD_PACKET_WITH_DATA
, tw_ioctl_char
);
1174 ioctlreturn
=ioctl(fd
, SCSI_IOCTL_SEND_COMMAND
, tw_ioctl
);
1176 // Deal with the different error cases
1178 if (CONTROLLER_3WARE_678K
==escalade_type
&& ((command
==AUTO_OFFLINE
|| command
==AUTOSAVE
) && select
)){
1179 // error here is probably a kernel driver whose version is too old
1180 printwarning(command
);
1188 // The passthru structure is valid after return from an ioctl if:
1189 // - we are using the character interface OR
1190 // - we are using the SCSI interface and this is a NON-READ-DATA command
1191 // For SCSI interface, note that we set passthru to a different
1192 // value after ioctl().
1193 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1197 passthru
=(TW_Passthru
*)&(tw_output
->output_data
);
1200 // See if the ATA command failed. Now that we have returned from
1201 // the ioctl() call, if passthru is valid, then:
1202 // - passthru->status contains the 3ware controller STATUS
1203 // - passthru->command contains the ATA STATUS register
1204 // - passthru->features contains the ATA ERROR register
1206 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1207 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1208 // While we *might* decode the ATA ERROR register, at the moment it
1209 // doesn't make much sense: we don't care in detail why the error
1212 if (passthru
&& (passthru
->status
|| (passthru
->command
& 0x21))) {
1217 // If this is a read data command, copy data to output buffer
1219 if (escalade_type
==CONTROLLER_3WARE_9000_CHAR
)
1220 memcpy(data
, (unsigned char *)tw_ioctl_apache
->data_buffer
, 512);
1221 else if (escalade_type
==CONTROLLER_3WARE_678K_CHAR
)
1222 memcpy(data
, (unsigned char *)tw_ioctl_char
->data_buffer
, 512);
1224 memcpy(data
, tw_output
->output_data
, 512);
1227 // For STATUS_CHECK, we need to check register values
1228 if (command
==STATUS_CHECK
) {
1230 // To find out if the SMART RETURN STATUS is good or failing, we
1231 // need to examine the values of the Cylinder Low and Cylinder
1234 unsigned short cyl_lo
=passthru
->cylinder_lo
;
1235 unsigned short cyl_hi
=passthru
->cylinder_hi
;
1237 // If values in Cyl-LO and Cyl-HI are unchanged, SMART status is good.
1238 if (cyl_lo
==0x4F && cyl_hi
==0xC2)
1241 // If values in Cyl-LO and Cyl-HI are as follows, SMART status is FAIL
1242 if (cyl_lo
==0xF4 && cyl_hi
==0x2C)
1245 // Any other values mean that something has gone wrong with the command
1246 if (CONTROLLER_3WARE_678K
==escalade_type
) {
1247 printwarning(command
);
1257 // copy sector count register (one byte!) to return data
1258 if (command
==CHECK_POWER_MODE
)
1259 *data
=*(char *)&(passthru
->sector_count
);
1261 // look for nonexistent devices/ports
1262 if (command
==IDENTIFY
&& !nonempty((unsigned char *)data
, 512)) {
1272 int marvell_command_interface(int device
,
1273 smart_command_set command
,
1283 mvsata_scsi_cmd smart_command
;
1284 unsigned char *buff
= (unsigned char *)&smart_command
.cmd
[6];
1285 // See struct hd_drive_cmd_hdr in hdreg.h
1286 // buff[0]: ATA COMMAND CODE REGISTER
1287 // buff[1]: ATA SECTOR NUMBER REGISTER
1288 // buff[2]: ATA FEATURES REGISTER
1289 // buff[3]: ATA SECTOR COUNT REGISTER
1291 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
1292 memset(&smart_command
, 0, sizeof(smart_command
));
1293 smart_command
.inlen
= 540;
1294 smart_command
.outlen
= 540;
1295 smart_command
.cmd
[0] = 0xC; //Vendor-specific code
1296 smart_command
.cmd
[4] = 6; //command length
1298 buff
[0] = ATA_SMART_CMD
;
1300 case CHECK_POWER_MODE
:
1301 buff
[0]=ATA_CHECK_POWER_MODE
;
1304 buff
[2]=ATA_SMART_READ_VALUES
;
1307 case READ_THRESHOLDS
:
1308 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1309 copydata
=buff
[1]=buff
[3]=1;
1312 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1317 buff
[0]=ATA_IDENTIFY_DEVICE
;
1321 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1325 buff
[2]=ATA_SMART_ENABLE
;
1329 buff
[2]=ATA_SMART_DISABLE
;
1334 // this command only says if SMART is working. It could be
1335 // replaced with STATUS_CHECK below.
1336 buff
[2] = ATA_SMART_STATUS
;
1339 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1340 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1343 buff
[2]=ATA_SMART_AUTOSAVE
;
1344 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1346 case IMMEDIATE_OFFLINE
:
1347 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1351 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command
);
1355 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
1357 // We are now doing the HDIO_DRIVE_CMD type ioctl.
1358 if (ioctl(device
, SCSI_IOCTL_SEND_COMMAND
, (void *)&smart_command
))
1361 if (command
==CHECK_POWER_MODE
) {
1362 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
1363 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
1368 // Always succeed on a SMART status, as a disk that failed returned
1369 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
1370 if (command
== STATUS
)
1372 //Data returned is starting from 0 offset
1373 if (command
== STATUS_CHECK
)
1375 // Cyl low and Cyl high unchanged means "Good SMART status"
1376 if (buff
[4] == 0x4F && buff
[5] == 0xC2)
1378 // These values mean "Bad SMART status"
1379 if (buff
[4] == 0xF4 && buff
[5] == 0x2C)
1381 // We haven't gotten output that makes sense; print out some debugging info
1382 syserror("Error SMART Status command failed");
1383 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT
);
1384 pout("Register values returned from SMART Status command are:\n");
1385 pout("CMD =0x%02x\n",(int)buff
[0]);
1386 pout("FR =0x%02x\n",(int)buff
[1]);
1387 pout("NS =0x%02x\n",(int)buff
[2]);
1388 pout("SC =0x%02x\n",(int)buff
[3]);
1389 pout("CL =0x%02x\n",(int)buff
[4]);
1390 pout("CH =0x%02x\n",(int)buff
[5]);
1391 pout("SEL=0x%02x\n",(int)buff
[6]);
1396 memcpy(data
, buff
, 512);
1401 // Utility function for printing warnings
1402 void printwarning(smart_command_set command
){
1403 static int printed
[4]={0,0,0,0};
1404 const char* message
=
1405 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
1406 "applying a simple 3w-xxxx driver patch that can be found here:\n"
1407 PACKAGE_HOMEPAGE
"\n"
1408 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
1410 if (command
==AUTO_OFFLINE
&& !printed
[0]) {
1412 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message
);
1414 else if (command
==AUTOSAVE
&& !printed
[1]) {
1416 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message
);
1418 else if (command
==STATUS_CHECK
&& !printed
[2]) {
1420 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message
);
1422 else if (command
==WRITE_LOG
&& !printed
[3]) {
1424 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
1430 // Guess device type (ata or scsi) based on device name (Linux
1431 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
1432 // osst, nosst and sg.
1433 static const char * lin_dev_prefix
= "/dev/";
1434 static const char * lin_dev_ata_disk_plus
= "h";
1435 static const char * lin_dev_ata_devfs_disk_plus
= "ide/";
1436 static const char * lin_dev_scsi_devfs_disk_plus
= "scsi/";
1437 static const char * lin_dev_scsi_disk_plus
= "s";
1438 static const char * lin_dev_scsi_tape1
= "ns";
1439 static const char * lin_dev_scsi_tape2
= "os";
1440 static const char * lin_dev_scsi_tape3
= "nos";
1441 static const char * lin_dev_3ware_9000_char
= "twa";
1442 static const char * lin_dev_3ware_678k_char
= "twe";
1444 int guess_device_type(const char * dev_name
) {
1446 int dev_prefix_len
= strlen(lin_dev_prefix
);
1448 // if dev_name null, or string length zero
1449 if (!dev_name
|| !(len
= strlen(dev_name
)))
1450 return CONTROLLER_UNKNOWN
;
1452 // Remove the leading /dev/... if it's there
1453 if (!strncmp(lin_dev_prefix
, dev_name
, dev_prefix_len
)) {
1454 if (len
<= dev_prefix_len
)
1455 // if nothing else in the string, unrecognized
1456 return CONTROLLER_UNKNOWN
;
1457 // else advance pointer to following characters
1458 dev_name
+= dev_prefix_len
;
1461 // form /dev/h* or h*
1462 if (!strncmp(lin_dev_ata_disk_plus
, dev_name
,
1463 strlen(lin_dev_ata_disk_plus
)))
1464 return CONTROLLER_ATA
;
1466 // form /dev/ide/* or ide/*
1467 if (!strncmp(lin_dev_ata_devfs_disk_plus
, dev_name
,
1468 strlen(lin_dev_ata_devfs_disk_plus
)))
1469 return CONTROLLER_ATA
;
1471 // form /dev/s* or s*
1472 if (!strncmp(lin_dev_scsi_disk_plus
, dev_name
,
1473 strlen(lin_dev_scsi_disk_plus
)))
1474 return CONTROLLER_SCSI
;
1476 // form /dev/scsi/* or scsi/*
1477 if (!strncmp(lin_dev_scsi_devfs_disk_plus
, dev_name
,
1478 strlen(lin_dev_scsi_devfs_disk_plus
)))
1479 return CONTROLLER_SCSI
;
1481 // form /dev/ns* or ns*
1482 if (!strncmp(lin_dev_scsi_tape1
, dev_name
,
1483 strlen(lin_dev_scsi_tape1
)))
1484 return CONTROLLER_SCSI
;
1486 // form /dev/os* or os*
1487 if (!strncmp(lin_dev_scsi_tape2
, dev_name
,
1488 strlen(lin_dev_scsi_tape2
)))
1489 return CONTROLLER_SCSI
;
1491 // form /dev/nos* or nos*
1492 if (!strncmp(lin_dev_scsi_tape3
, dev_name
,
1493 strlen(lin_dev_scsi_tape3
)))
1494 return CONTROLLER_SCSI
;
1497 if (!strncmp(lin_dev_3ware_9000_char
, dev_name
,
1498 strlen(lin_dev_3ware_9000_char
)))
1499 return CONTROLLER_3WARE_9000_CHAR
;
1502 if (!strncmp(lin_dev_3ware_678k_char
, dev_name
,
1503 strlen(lin_dev_3ware_678k_char
)))
1504 return CONTROLLER_3WARE_678K_CHAR
;
1506 // we failed to recognize any of the forms
1507 return CONTROLLER_UNKNOWN
;
1513 [ed@firestorm ed
]$ ls
-l
/dev
/discs
1515 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc0
-> ../ide
/host2
/bus0
/target0
/lun0
/
1516 lr
-xr
-xr
-x
1 root root
30 Dec
31 1969 disc1
-> ../ide
/host2
/bus1
/target0
/lun0
/
1517 [ed@firestorm ed
]$ ls
-l dev
/ide
/host*/bus*/target*/lun*/disc
1518 ls
: dev
/ide
/host*/bus*/target*/lun*/disc
: No such file
or directory
1519 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/host*/bus*/target*/lun*/disc
1520 brw
------- 1 root root
33, 0 Dec
31 1969 /dev
/ide
/host2
/bus0
/target0
/lun0
/disc
1521 brw
------- 1 root root
34, 0 Dec
31 1969 /dev
/ide
/host2
/bus1
/target0
/lun0
/disc
1522 [ed@firestorm ed
]$ ls
-l
/dev
/ide
/c
*b
*t
*u
*
1523 ls
: /dev
/ide
/c
*b
*t
*u
*: No such file
or directory
1525 Script done on Fri Nov
7 13:46:28 2003