4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2003-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2003-11 Doug Gilbert <dgilbert@interlog.com>
8 * Copyright (C) 2008-12 Hank Wu <hank@areca.com.tw>
9 * Copyright (C) 2008 Oliver Bock <brevilo@users.sourceforge.net>
10 * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
11 * Copyright (C) 2008 Jordan Hargrave <jordan_hargrave@dell.com>
13 * Parts of this file are derived from code that was
15 * Written By: Adam Radford <linux@3ware.com>
16 * Modifications By: Joel Jacobson <linux@3ware.com>
17 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
18 * Brad Strand <linux@3ware.com>
20 * Copyright (C) 1999-2003 3ware Inc.
22 * Kernel compatablity By: Andre Hedrick <andre@suse.com>
23 * Non-Copyright (C) 2000 Andre Hedrick <andre@suse.com>
25 * Other ars of this file are derived from code that was
27 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
28 * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org>
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2, or (at your option)
35 * You should have received a copy of the GNU General Public License
36 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
38 * This code was originally developed as a Senior Thesis by Michael Cornwell
39 * at the Concurrent Systems Laboratory (now part of the Storage Systems
40 * Research Center), Jack Baskin School of Engineering, University of
41 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
45 // This file contains the linux-specific IOCTL parts of
46 // smartmontools. It includes one interface routine for ATA devices,
47 // one for SCSI devices, and one for ATA devices behind escalade
56 #include <scsi/scsi.h>
57 #include <scsi/scsi_ioctl.h>
61 #include <sys/ioctl.h>
63 #include <sys/utsname.h>
65 #include <stddef.h> // for offsetof()
67 #include <sys/types.h>
69 #ifndef makedev // old versions of types.h do not include sysmacros.h
70 #include <sys/sysmacros.h>
73 #include <selinux/selinux.h>
84 #include "dev_interface.h"
85 #include "dev_ata_cmd_set.h"
86 #include "dev_areca.h"
89 #define ENOTSUP ENOSYS
92 #define ARGUSED(x) ((void)(x))
94 const char * os_linux_cpp_cvsid
= "$Id: os_linux.cpp 3824 2013-07-05 10:40:38Z samm2 $"
96 extern unsigned char failuretest_permissive
;
98 namespace os_linux
{ // No need to publish anything, name provided for Doxygen
100 /////////////////////////////////////////////////////////////////////////////
101 /// Shared open/close routines
103 class linux_smart_device
104 : virtual public /*implements*/ smart_device
107 explicit linux_smart_device(int flags
, int retry_flags
= -1)
108 : smart_device(never_called
),
110 m_flags(flags
), m_retry_flags(retry_flags
)
113 virtual ~linux_smart_device() throw();
115 virtual bool is_open() const;
119 virtual bool close();
122 /// Return filedesc for derived classes.
130 int m_fd
; ///< filedesc, -1 if not open.
131 int m_flags
; ///< Flags for ::open()
132 int m_retry_flags
; ///< Flags to retry ::open(), -1 if no retry
135 linux_smart_device::~linux_smart_device() throw()
141 bool linux_smart_device::is_open() const
146 bool linux_smart_device::open()
148 m_fd
= ::open(get_dev_name(), m_flags
);
150 if (m_fd
< 0 && errno
== EROFS
&& m_retry_flags
!= -1)
152 m_fd
= ::open(get_dev_name(), m_retry_flags
);
155 if (errno
== EBUSY
&& (m_flags
& O_EXCL
))
157 return set_err(EBUSY
,
158 "The requested controller is used exclusively by another process!\n"
159 "(e.g. smartctl or smartd)\n"
160 "Please quit the impeding process or try again later...");
161 return set_err((errno
==ENOENT
|| errno
==ENOTDIR
) ? ENODEV
: errno
);
165 // sets FD_CLOEXEC on the opened device file descriptor. The
166 // descriptor is otherwise leaked to other applications (mail
167 // sender) which may be considered a security risk and may result
168 // in AVC messages on SELinux-enabled systems.
169 if (-1 == fcntl(m_fd
, F_SETFD
, FD_CLOEXEC
))
170 // TODO: Provide an error printing routine in class smart_interface
171 pout("fcntl(set FD_CLOEXEC) failed, errno=%d [%s]\n", errno
, strerror(errno
));
177 // equivalent to close(file descriptor)
178 bool linux_smart_device::close()
180 int fd
= m_fd
; m_fd
= -1;
182 return set_err(errno
);
186 // examples for smartctl
187 static const char smartctl_examples
[] =
188 "=================================================== SMARTCTL EXAMPLES =====\n\n"
189 " smartctl --all /dev/hda (Prints all SMART information)\n\n"
190 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n"
191 " (Enables SMART on first disk)\n\n"
192 " smartctl --test=long /dev/hda (Executes extended disk self-test)\n\n"
193 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/hda\n"
194 " (Prints Self-Test & Attribute errors)\n"
195 " smartctl --all --device=3ware,2 /dev/sda\n"
196 " smartctl --all --device=3ware,2 /dev/twe0\n"
197 " smartctl --all --device=3ware,2 /dev/twa0\n"
198 " smartctl --all --device=3ware,2 /dev/twl0\n"
199 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
200 " smartctl --all --device=hpt,1/1/3 /dev/sda\n"
201 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
202 " of the 1st channel on the 1st HighPoint RAID controller)\n"
203 " smartctl --all --device=areca,3/1 /dev/sg2\n"
204 " (Prints all SMART info for 3rd ATA disk of the 1st enclosure\n"
205 " on Areca RAID controller)\n"
208 /////////////////////////////////////////////////////////////////////////////
209 /// Linux ATA support
211 class linux_ata_device
212 : public /*implements*/ ata_device_with_command_set
,
213 public /*extends*/ linux_smart_device
216 linux_ata_device(smart_interface
* intf
, const char * dev_name
, const char * req_type
);
219 virtual int ata_command_interface(smart_command_set command
, int select
, char * data
);
222 linux_ata_device::linux_ata_device(smart_interface
* intf
, const char * dev_name
, const char * req_type
)
223 : smart_device(intf
, dev_name
, "ata", req_type
),
224 linux_smart_device(O_RDONLY
| O_NONBLOCK
)
229 // This is an interface routine meant to isolate the OS dependent
230 // parts of the code, and to provide a debugging interface. Each
231 // different port and OS needs to provide it's own interface. This
233 // DETAILED DESCRIPTION OF ARGUMENTS
234 // device: is the file descriptor provided by open()
235 // command: defines the different operations.
236 // select: additional input data if needed (which log, which type of
238 // data: location to write output data, if needed (512 bytes).
239 // Note: not all commands use all arguments.
241 // -1 if the command failed
242 // 0 if the command succeeded,
243 // STATUS_CHECK routine:
244 // -1 if the command failed
245 // 0 if the command succeeded and disk SMART status is "OK"
246 // 1 if the command succeeded and disk SMART status is "FAILING"
248 #define BUFFER_LENGTH (4+512)
250 int linux_ata_device::ata_command_interface(smart_command_set command
, int select
, char * data
)
252 unsigned char buff
[BUFFER_LENGTH
];
253 // positive: bytes to write to caller. negative: bytes to READ from
254 // caller. zero: non-data command
257 const int HDIO_DRIVE_CMD_OFFSET
= 4;
259 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
260 // buff[0]: ATA COMMAND CODE REGISTER
261 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
262 // buff[2]: ATA FEATURES REGISTER
263 // buff[3]: ATA SECTOR COUNT REGISTER
265 // Note that on return:
266 // buff[2] contains the ATA SECTOR COUNT REGISTER
268 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
269 memset(buff
, 0, BUFFER_LENGTH
);
271 buff
[0]=ATA_SMART_CMD
;
273 case CHECK_POWER_MODE
:
274 buff
[0]=ATA_CHECK_POWER_MODE
;
278 buff
[2]=ATA_SMART_READ_VALUES
;
282 case READ_THRESHOLDS
:
283 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
288 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
296 buff
[0]=ATA_IDENTIFY_DEVICE
;
301 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
306 buff
[2]=ATA_SMART_ENABLE
;
310 buff
[2]=ATA_SMART_DISABLE
;
314 // this command only says if SMART is working. It could be
315 // replaced with STATUS_CHECK below.
316 buff
[2]=ATA_SMART_STATUS
;
319 // NOTE: According to ATAPI 4 and UP, this command is obsolete
320 // select == 241 for enable but no data transfer. Use TASK ioctl.
321 buff
[1]=ATA_SMART_AUTO_OFFLINE
;
325 // select == 248 for enable but no data transfer. Use TASK ioctl.
326 buff
[1]=ATA_SMART_AUTOSAVE
;
329 case IMMEDIATE_OFFLINE
:
330 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
334 // This command uses HDIO_DRIVE_TASK and has different syntax than
335 // the other commands.
336 buff
[1]=ATA_SMART_STATUS
;
339 pout("Unrecognized command %d in linux_ata_command_interface()\n"
340 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
345 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
346 // only ioctl() that can be used to WRITE data to the disk.
347 if (command
==WRITE_LOG
) {
348 unsigned char task
[sizeof(ide_task_request_t
)+512];
349 ide_task_request_t
*reqtask
=(ide_task_request_t
*) task
;
350 task_struct_t
*taskfile
=(task_struct_t
*) reqtask
->io_ports
;
353 memset(task
, 0, sizeof(task
));
356 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
357 taskfile
->sector_count
= 1;
358 taskfile
->sector_number
= select
;
359 taskfile
->low_cylinder
= 0x4f;
360 taskfile
->high_cylinder
= 0xc2;
361 taskfile
->device_head
= 0;
362 taskfile
->command
= ATA_SMART_CMD
;
364 reqtask
->data_phase
= TASKFILE_OUT
;
365 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
366 reqtask
->out_size
= 512;
367 reqtask
->in_size
= 0;
369 // copy user data into the task request structure
370 memcpy(task
+sizeof(ide_task_request_t
), data
, 512);
372 if ((retval
=ioctl(get_fd(), HDIO_DRIVE_TASKFILE
, task
))) {
374 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
380 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
382 if (command
==STATUS_CHECK
|| command
==AUTOSAVE
|| command
==AUTO_OFFLINE
){
385 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
386 // have to read the IDE driver source code. Sigh.
387 // buff[0]: ATA COMMAND CODE REGISTER
388 // buff[1]: ATA FEATURES REGISTER
389 // buff[2]: ATA SECTOR_COUNT
390 // buff[3]: ATA SECTOR NUMBER
391 // buff[4]: ATA CYL LO REGISTER
392 // buff[5]: ATA CYL HI REGISTER
393 // buff[6]: ATA DEVICE HEAD
395 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
396 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
400 if ((retval
=ioctl(get_fd(), HDIO_DRIVE_TASK
, buff
))) {
401 if (retval
==-EINVAL
) {
402 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
403 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
406 syserror("Error SMART Status command failed");
410 // Cyl low and Cyl high unchanged means "Good SMART status"
411 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
414 // These values mean "Bad SMART status"
415 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
418 // We haven't gotten output that makes sense; print out some debugging info
419 syserror("Error SMART Status command failed");
420 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
421 pout("Register values returned from SMART Status command are:\n");
422 pout("ST =0x%02x\n",(int)buff
[0]);
423 pout("ERR=0x%02x\n",(int)buff
[1]);
424 pout("NS =0x%02x\n",(int)buff
[2]);
425 pout("SC =0x%02x\n",(int)buff
[3]);
426 pout("CL =0x%02x\n",(int)buff
[4]);
427 pout("CH =0x%02x\n",(int)buff
[5]);
428 pout("SEL=0x%02x\n",(int)buff
[6]);
433 // Note to people doing ports to other OSes -- don't worry about
434 // this block -- you can safely ignore it. I have put it here
435 // because under linux when you do IDENTIFY DEVICE to a packet
436 // device, it generates an ugly kernel syslog error message. This
437 // is harmless but frightens users. So this block detects packet
438 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
441 // If you read only the ATA specs, it appears as if a packet device
442 // *might* respond to the IDENTIFY DEVICE command. This is
443 // misleading - it's because around the time that SFF-8020 was
444 // incorporated into the ATA-3/4 standard, the ATA authors were
445 // sloppy. See SFF-8020 and you will see that ATAPI devices have
446 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
447 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
448 if (command
==IDENTIFY
|| command
==PIDENTIFY
){
449 unsigned short deviceid
[256];
450 // check the device identity, as seen when the system was booted
451 // or the device was FIRST registered. This will not be current
452 // if the user has subsequently changed some of the parameters. If
453 // device is a packet device, swap the command interpretations.
454 if (!ioctl(get_fd(), HDIO_GET_IDENTITY
, deviceid
) && (deviceid
[0] & 0x8000))
455 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
459 // We are now doing the HDIO_DRIVE_CMD type ioctl.
460 if ((ioctl(get_fd(), HDIO_DRIVE_CMD
, buff
)))
463 // CHECK POWER MODE command returns information in the Sector Count
464 // register (buff[3]). Copy to return data buffer.
465 if (command
==CHECK_POWER_MODE
)
466 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
468 // if the command returns data then copy it back
470 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
475 // >>>>>> Start of general SCSI specific linux code
477 /* Linux specific code.
478 * Historically smartmontools (and smartsuite before it) used the
479 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
480 * nodes that use the SCSI subsystem. A better interface has been available
481 * via the SCSI generic (sg) driver but this involves the extra step of
482 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
483 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
484 * the sg driver have become available via the SG_IO ioctl which is available
485 * on all SCSI devices (on SCSI tape devices from lk 2.6.6).
486 * So the strategy below is to find out if the SG_IO ioctl is available and
487 * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl.
488 * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */
490 #define MAX_DXFER_LEN 1024 /* can be increased if necessary */
491 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
492 #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
493 #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
494 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
495 #define LSCSI_DID_ERROR 0x7 /* Need to work around aacraid driver quirk */
496 #define LSCSI_DRIVER_TIMEOUT 0x6
497 #define LSCSI_DID_TIME_OUT 0x3
498 #define LSCSI_DID_BUS_BUSY 0x2
499 #define LSCSI_DID_NO_CONNECT 0x1
501 #ifndef SCSI_IOCTL_SEND_COMMAND
502 #define SCSI_IOCTL_SEND_COMMAND 1
505 #define SG_IO_PRESENT_UNKNOWN 0
506 #define SG_IO_PRESENT_YES 1
507 #define SG_IO_PRESENT_NO 2
509 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
511 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
);
513 static int sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
515 /* Preferred implementation for issuing SCSI commands in linux. This
516 * function uses the SG_IO ioctl. Return 0 if command issued successfully
517 * (various status values should still be checked). If the SCSI command
518 * cannot be issued then a negative errno value is returned. */
519 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
523 ARGUSED(dev_fd
); ARGUSED(iop
); ARGUSED(report
);
526 struct sg_io_hdr io_hdr
;
530 const unsigned char * ucp
= iop
->cmnd
;
533 const int sz
= (int)sizeof(buff
);
535 np
= scsi_get_opcode_name(ucp
[0]);
536 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
537 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
538 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
540 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
541 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
543 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
544 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
545 (trunc
? " [only first 256 bytes shown]" : ""));
546 dStrHex((const char *)iop
->dxferp
,
547 (trunc
? 256 : iop
->dxfer_len
) , 1);
550 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
553 memset(&io_hdr
, 0, sizeof(struct sg_io_hdr
));
554 io_hdr
.interface_id
= 'S';
555 io_hdr
.cmd_len
= iop
->cmnd_len
;
556 io_hdr
.mx_sb_len
= iop
->max_sense_len
;
557 io_hdr
.dxfer_len
= iop
->dxfer_len
;
558 io_hdr
.dxferp
= iop
->dxferp
;
559 io_hdr
.cmdp
= iop
->cmnd
;
560 io_hdr
.sbp
= iop
->sensep
;
561 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
562 defaults to 60 seconds. */
563 io_hdr
.timeout
= ((0 == iop
->timeout
) ? 60 : iop
->timeout
) * 1000;
564 switch (iop
->dxfer_dir
) {
566 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
568 case DXFER_FROM_DEVICE
:
569 io_hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
571 case DXFER_TO_DEVICE
:
572 io_hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
575 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
578 iop
->resp_sense_len
= 0;
579 iop
->scsi_status
= 0;
581 if (ioctl(dev_fd
, SG_IO
, &io_hdr
) < 0) {
582 if (report
&& (! unknown
))
583 pout(" SG_IO ioctl failed, errno=%d [%s]\n", errno
,
587 iop
->resid
= io_hdr
.resid
;
588 iop
->scsi_status
= io_hdr
.status
;
590 pout(" scsi_status=0x%x, host_status=0x%x, driver_status=0x%x\n"
591 " info=0x%x duration=%d milliseconds resid=%d\n", io_hdr
.status
,
592 io_hdr
.host_status
, io_hdr
.driver_status
, io_hdr
.info
,
593 io_hdr
.duration
, io_hdr
.resid
);
595 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
598 len
= iop
->dxfer_len
- iop
->resid
;
599 trunc
= (len
> 256) ? 1 : 0;
601 pout(" Incoming data, len=%d%s:\n", len
,
602 (trunc
? " [only first 256 bytes shown]" : ""));
603 dStrHex((const char*)iop
->dxferp
, (trunc
? 256 : len
),
606 pout(" Incoming data trimmed to nothing by resid\n");
611 if (io_hdr
.info
& SG_INFO_CHECK
) { /* error or warning */
612 int masked_driver_status
= (LSCSI_DRIVER_MASK
& io_hdr
.driver_status
);
614 if (0 != io_hdr
.host_status
) {
615 if ((LSCSI_DID_NO_CONNECT
== io_hdr
.host_status
) ||
616 (LSCSI_DID_BUS_BUSY
== io_hdr
.host_status
) ||
617 (LSCSI_DID_TIME_OUT
== io_hdr
.host_status
))
620 /* Check for DID_ERROR - workaround for aacraid driver quirk */
621 if (LSCSI_DID_ERROR
!= io_hdr
.host_status
) {
622 return -EIO
; /* catch all if not DID_ERR */
625 if (0 != masked_driver_status
) {
626 if (LSCSI_DRIVER_TIMEOUT
== masked_driver_status
)
628 else if (LSCSI_DRIVER_SENSE
!= masked_driver_status
)
631 if (LSCSI_DRIVER_SENSE
== masked_driver_status
)
632 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
633 iop
->resp_sense_len
= io_hdr
.sb_len_wr
;
634 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
635 iop
->sensep
&& (iop
->resp_sense_len
> 0)) {
637 pout(" >>> Sense buffer, len=%d:\n",
638 (int)iop
->resp_sense_len
);
639 dStrHex((const char *)iop
->sensep
, iop
->resp_sense_len
, 1);
643 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
644 if ((iop
->sensep
[0] & 0x7f) > 0x71)
645 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
646 iop
->scsi_status
, iop
->sensep
[1] & 0xf,
647 iop
->sensep
[2], iop
->sensep
[3]);
649 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
650 iop
->scsi_status
, iop
->sensep
[2] & 0xf,
651 iop
->sensep
[12], iop
->sensep
[13]);
654 pout(" status=0x%x\n", iop
->scsi_status
);
661 struct linux_ioctl_send_command
665 UINT8 buff
[MAX_DXFER_LEN
+ 16];
668 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
669 * support: CDB length (guesses it from opcode), resid and timeout.
670 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
671 * to 2 hours in order to allow long foreground extended self tests. */
672 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
674 struct linux_ioctl_send_command wrk
;
675 int status
, buff_offset
;
678 memcpy(wrk
.buff
, iop
->cmnd
, iop
->cmnd_len
);
679 buff_offset
= iop
->cmnd_len
;
682 const unsigned char * ucp
= iop
->cmnd
;
685 const int sz
= (int)sizeof(buff
);
687 np
= scsi_get_opcode_name(ucp
[0]);
688 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
689 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
690 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
692 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
693 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
695 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
696 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
697 (trunc
? " [only first 256 bytes shown]" : ""));
698 dStrHex((const char *)iop
->dxferp
,
699 (trunc
? 256 : iop
->dxfer_len
) , 1);
702 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
705 switch (iop
->dxfer_dir
) {
710 case DXFER_FROM_DEVICE
:
712 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
714 wrk
.outbufsize
= iop
->dxfer_len
;
716 case DXFER_TO_DEVICE
:
717 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
719 memcpy(wrk
.buff
+ buff_offset
, iop
->dxferp
, iop
->dxfer_len
);
720 wrk
.inbufsize
= iop
->dxfer_len
;
724 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
727 iop
->resp_sense_len
= 0;
728 iop
->scsi_status
= 0;
730 status
= ioctl(dev_fd
, SCSI_IOCTL_SEND_COMMAND
, &wrk
);
733 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
734 errno
, strerror(errno
));
740 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
741 memcpy(iop
->dxferp
, wrk
.buff
, iop
->dxfer_len
);
743 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
745 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
746 (trunc
? " [only first 256 bytes shown]" : ""));
747 dStrHex((const char*)iop
->dxferp
,
748 (trunc
? 256 : iop
->dxfer_len
) , 1);
753 iop
->scsi_status
= status
& 0x7e; /* bits 0 and 7 used to be for vendors */
754 if (LSCSI_DRIVER_SENSE
== ((status
>> 24) & 0xf))
755 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
756 len
= (SEND_IOCTL_RESP_SENSE_LEN
< iop
->max_sense_len
) ?
757 SEND_IOCTL_RESP_SENSE_LEN
: iop
->max_sense_len
;
758 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
759 iop
->sensep
&& (len
> 0)) {
760 memcpy(iop
->sensep
, wrk
.buff
, len
);
761 iop
->resp_sense_len
= len
;
763 pout(" >>> Sense buffer, len=%d:\n", (int)len
);
764 dStrHex((const char *)wrk
.buff
, len
, 1);
768 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
769 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status
& 0xff,
770 wrk
.buff
[2] & 0xf, wrk
.buff
[12], wrk
.buff
[13]);
773 pout(" status=0x%x\n", status
);
775 if (iop
->scsi_status
> 0)
779 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
781 return -EIO
; /* give up, assume no device there */
785 /* SCSI command transmission interface function, linux version.
786 * Returns 0 if SCSI command successfully launched and response
787 * received. Even when 0 is returned the caller should check
788 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
789 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
790 * (e.g. device not present or timeout) or some other problem
791 * (e.g. timeout) then returns a negative errno value */
792 static int do_normal_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
,
797 /* implementation relies on static sg_io_state variable. If not
798 * previously set tries the SG_IO ioctl. If that succeeds assume
799 * that SG_IO ioctl functional. If it fails with an errno value
800 * other than ENODEV (no device) or permission then assume
801 * SCSI_IOCTL_SEND_COMMAND is the only option. */
802 switch (sg_io_state
) {
803 case SG_IO_PRESENT_UNKNOWN
:
804 /* ignore report argument */
805 if (0 == (res
= sg_io_cmnd_io(dev_fd
, iop
, report
, 1))) {
806 sg_io_state
= SG_IO_PRESENT_YES
;
808 } else if ((-ENODEV
== res
) || (-EACCES
== res
) || (-EPERM
== res
))
809 return res
; /* wait until we see a device */
810 sg_io_state
= SG_IO_PRESENT_NO
;
811 /* drop through by design */
812 case SG_IO_PRESENT_NO
:
813 return sisc_cmnd_io(dev_fd
, iop
, report
);
814 case SG_IO_PRESENT_YES
:
815 return sg_io_cmnd_io(dev_fd
, iop
, report
, 0);
817 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state
);
818 sg_io_state
= SG_IO_PRESENT_UNKNOWN
;
819 return -EIO
; /* report error and reset state */
823 // >>>>>> End of general SCSI specific linux code
825 /////////////////////////////////////////////////////////////////////////////
826 /// Standard SCSI support
828 class linux_scsi_device
829 : public /*implements*/ scsi_device
,
830 public /*extends*/ linux_smart_device
833 linux_scsi_device(smart_interface
* intf
, const char * dev_name
,
834 const char * req_type
, bool scanning
= false);
836 virtual smart_device
* autodetect_open();
838 virtual bool scsi_pass_through(scsi_cmnd_io
* iop
);
841 bool m_scanning
; ///< true if created within scan_smart_devices
844 linux_scsi_device::linux_scsi_device(smart_interface
* intf
,
845 const char * dev_name
, const char * req_type
, bool scanning
/*= false*/)
846 : smart_device(intf
, dev_name
, "scsi", req_type
),
847 // If opened with O_RDWR, a SATA disk in standby mode
848 // may spin-up after device close().
849 linux_smart_device(O_RDONLY
| O_NONBLOCK
),
854 bool linux_scsi_device::scsi_pass_through(scsi_cmnd_io
* iop
)
856 int status
= do_normal_scsi_cmnd_io(get_fd(), iop
, scsi_debugmode
);
858 return set_err(-status
);
862 /////////////////////////////////////////////////////////////////////////////
863 /// LSI MegaRAID support
865 class linux_megaraid_device
866 : public /* implements */ scsi_device
,
867 public /* extends */ linux_smart_device
870 linux_megaraid_device(smart_interface
*intf
, const char *name
,
871 unsigned int bus
, unsigned int tgt
);
873 virtual ~linux_megaraid_device() throw();
875 virtual smart_device
* autodetect_open();
878 virtual bool close();
880 virtual bool scsi_pass_through(scsi_cmnd_io
*iop
);
883 unsigned int m_disknum
;
884 unsigned int m_busnum
;
888 bool (linux_megaraid_device::*pt_cmd
)(int cdblen
, void *cdb
, int dataLen
, void *data
,
889 int senseLen
, void *sense
, int report
, int direction
);
890 bool megasas_cmd(int cdbLen
, void *cdb
, int dataLen
, void *data
,
891 int senseLen
, void *sense
, int report
, int direction
);
892 bool megadev_cmd(int cdbLen
, void *cdb
, int dataLen
, void *data
,
893 int senseLen
, void *sense
, int report
, int direction
);
896 linux_megaraid_device::linux_megaraid_device(smart_interface
*intf
,
897 const char *dev_name
, unsigned int bus
, unsigned int tgt
)
898 : smart_device(intf
, dev_name
, "megaraid", "megaraid"),
899 linux_smart_device(O_RDWR
| O_NONBLOCK
),
900 m_disknum(tgt
), m_busnum(bus
), m_hba(0),
903 set_info().info_name
= strprintf("%s [megaraid_disk_%02d]", dev_name
, m_disknum
);
904 set_info().dev_type
= strprintf("megaraid,%d", tgt
);
907 linux_megaraid_device::~linux_megaraid_device() throw()
913 smart_device
* linux_megaraid_device::autodetect_open()
915 int report
= scsi_debugmode
;
921 // The code below is based on smartd.cpp:SCSIFilterKnown()
922 if (strcmp(get_req_type(), "megaraid"))
926 unsigned char req_buff
[64] = {0, };
928 if (scsiStdInquiry(this, req_buff
, req_len
)) {
930 set_err(EIO
, "INQUIRY failed");
934 int avail_len
= req_buff
[4] + 5;
935 int len
= (avail_len
< req_len
? avail_len
: req_len
);
940 pout("Got MegaRAID inquiry.. %s\n", req_buff
+8);
942 // Use INQUIRY to detect type
945 ata_device
* newdev
= smi()->autodetect_sat_device(this, req_buff
, len
);
946 if (newdev
) // NOTE: 'this' is now owned by '*newdev'
950 // Nothing special found
954 bool linux_megaraid_device::open()
958 int report
= scsi_debugmode
;
960 if(sscanf(get_dev_name(),"/dev/bus/%d", &m_hba
) == 0) {
961 if (!linux_smart_device::open())
964 struct sg_scsi_id sgid
;
965 if (ioctl(get_fd(), SG_GET_SCSI_ID
, &sgid
) == 0) {
966 m_hba
= sgid
.host_no
;
968 else if (ioctl(get_fd(), SCSI_IOCTL_GET_BUS_NUMBER
, &m_hba
) != 0) {
970 linux_smart_device::close();
971 return set_err(err
, "can't get bus number");
972 } // we dont need this device anymore
973 linux_smart_device::close();
975 /* Perform mknod of device ioctl node */
976 FILE * fp
= fopen("/proc/devices", "r");
977 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
979 if (sscanf(line
, "%d megaraid_sas_ioctl%n", &mjr
, &n1
) == 1 && n1
== 22) {
980 n1
=mknod("/dev/megaraid_sas_ioctl_node", S_IFCHR
, makedev(mjr
, 0));
982 pout("Creating /dev/megaraid_sas_ioctl_node = %d\n", n1
>= 0 ? 0 : errno
);
983 if (n1
>= 0 || errno
== EEXIST
)
986 else if (sscanf(line
, "%d megadev%n", &mjr
, &n1
) == 1 && n1
== 11) {
987 n1
=mknod("/dev/megadev0", S_IFCHR
, makedev(mjr
, 0));
989 pout("Creating /dev/megadev0 = %d\n", n1
>= 0 ? 0 : errno
);
990 if (n1
>= 0 || errno
== EEXIST
)
996 /* Open Device IOCTL node */
997 if ((m_fd
= ::open("/dev/megaraid_sas_ioctl_node", O_RDWR
)) >= 0) {
998 pt_cmd
= &linux_megaraid_device::megasas_cmd
;
1000 else if ((m_fd
= ::open("/dev/megadev0", O_RDWR
)) >= 0) {
1001 pt_cmd
= &linux_megaraid_device::megadev_cmd
;
1005 linux_smart_device::close();
1006 return set_err(err
, "cannot open /dev/megaraid_sas_ioctl_node or /dev/megadev0");
1012 bool linux_megaraid_device::close()
1016 m_fd
= -1; m_hba
= 0; pt_cmd
= 0;
1021 bool linux_megaraid_device::scsi_pass_through(scsi_cmnd_io
*iop
)
1023 int report
= scsi_debugmode
;
1027 const unsigned char * ucp
= iop
->cmnd
;
1030 const int sz
= (int)sizeof(buff
);
1032 np
= scsi_get_opcode_name(ucp
[0]);
1033 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
1034 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
1035 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
1037 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
1038 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
1040 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
1041 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
1042 (trunc
? " [only first 256 bytes shown]" : ""));
1043 dStrHex((const char *)iop
->dxferp
,
1044 (trunc
? 256 : iop
->dxfer_len
) , 1);
1047 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
1051 // Controller rejects Test Unit Ready
1052 if (iop
->cmnd
[0] == 0x00)
1055 if (iop
->cmnd
[0] == SAT_ATA_PASSTHROUGH_12
|| iop
->cmnd
[0] == SAT_ATA_PASSTHROUGH_16
) {
1056 // Controller does not return ATA output registers in SAT sense data
1057 if (iop
->cmnd
[2] & (1 << 5)) // chk_cond
1058 return set_err(ENOSYS
, "ATA return descriptor not supported by controller firmware");
1060 // SMART WRITE LOG SECTOR causing media errors
1061 if ((iop
->cmnd
[0] == SAT_ATA_PASSTHROUGH_16
// SAT16 WRITE LOG
1062 && iop
->cmnd
[14] == ATA_SMART_CMD
&& iop
->cmnd
[3]==0 && iop
->cmnd
[4] == ATA_SMART_WRITE_LOG_SECTOR
) ||
1063 (iop
->cmnd
[0] == SAT_ATA_PASSTHROUGH_12
// SAT12 WRITE LOG
1064 && iop
->cmnd
[9] == ATA_SMART_CMD
&& iop
->cmnd
[3] == ATA_SMART_WRITE_LOG_SECTOR
))
1066 if(!failuretest_permissive
)
1067 return set_err(ENOSYS
, "SMART WRITE LOG SECTOR may cause problems, try with -T permissive to force");
1071 return (this->*pt_cmd
)(iop
->cmnd_len
, iop
->cmnd
,
1072 iop
->dxfer_len
, iop
->dxferp
,
1073 iop
->max_sense_len
, iop
->sensep
, report
, iop
->dxfer_dir
);
1076 /* Issue passthrough scsi command to PERC5/6 controllers */
1077 bool linux_megaraid_device::megasas_cmd(int cdbLen
, void *cdb
,
1078 int dataLen
, void *data
,
1079 int /*senseLen*/, void * /*sense*/, int /*report*/, int dxfer_dir
)
1081 struct megasas_pthru_frame
*pthru
;
1082 struct megasas_iocpacket uio
;
1085 memset(&uio
, 0, sizeof(uio
));
1086 pthru
= &uio
.frame
.pthru
;
1087 pthru
->cmd
= MFI_CMD_PD_SCSI_IO
;
1088 pthru
->cmd_status
= 0xFF;
1089 pthru
->scsi_status
= 0x0;
1090 pthru
->target_id
= m_disknum
;
1092 pthru
->cdb_len
= cdbLen
;
1094 switch (dxfer_dir
) {
1096 pthru
->flags
= MFI_FRAME_DIR_NONE
;
1098 case DXFER_FROM_DEVICE
:
1099 pthru
->flags
= MFI_FRAME_DIR_READ
;
1101 case DXFER_TO_DEVICE
:
1102 pthru
->flags
= MFI_FRAME_DIR_WRITE
;
1105 pout("megasas_cmd: bad dxfer_dir\n");
1106 return set_err(EINVAL
, "megasas_cmd: bad dxfer_dir\n");
1110 pthru
->sge_count
= 1;
1111 pthru
->data_xfer_len
= dataLen
;
1112 pthru
->sgl
.sge32
[0].phys_addr
= (intptr_t)data
;
1113 pthru
->sgl
.sge32
[0].length
= (uint32_t)dataLen
;
1115 memcpy(pthru
->cdb
, cdb
, cdbLen
);
1117 uio
.host_no
= m_hba
;
1120 uio
.sgl_off
= offsetof(struct megasas_pthru_frame
, sgl
);
1121 uio
.sgl
[0].iov_base
= data
;
1122 uio
.sgl
[0].iov_len
= dataLen
;
1127 rc
= ioctl(m_fd
, MEGASAS_IOC_FIRMWARE
, &uio
);
1128 if (pthru
->cmd_status
|| rc
!= 0) {
1129 if (pthru
->cmd_status
== 12) {
1130 return set_err(EIO
, "megasas_cmd: Device %d does not exist\n", m_disknum
);
1132 return set_err((errno
? errno
: EIO
), "megasas_cmd result: %d.%d = %d/%d",
1133 m_hba
, m_disknum
, errno
,
1139 /* Issue passthrough scsi commands to PERC2/3/4 controllers */
1140 bool linux_megaraid_device::megadev_cmd(int cdbLen
, void *cdb
,
1141 int dataLen
, void *data
,
1142 int /*senseLen*/, void * /*sense*/, int /*report*/, int /* dir */)
1144 struct uioctl_t uio
;
1147 /* Don't issue to the controller */
1151 memset(&uio
, 0, sizeof(uio
));
1152 uio
.inlen
= dataLen
;
1153 uio
.outlen
= dataLen
;
1155 memset(data
, 0, dataLen
);
1156 uio
.ui
.fcs
.opcode
= 0x80; // M_RD_IOCTL_CMD
1157 uio
.ui
.fcs
.adapno
= MKADAP(m_hba
);
1159 uio
.data
.pointer
= (uint8_t *)data
;
1161 uio
.mbox
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
1162 uio
.mbox
.xferaddr
= (intptr_t)&uio
.pthru
;
1165 uio
.pthru
.timeout
= 2;
1166 uio
.pthru
.channel
= 0;
1167 uio
.pthru
.target
= m_disknum
;
1168 uio
.pthru
.cdblen
= cdbLen
;
1169 uio
.pthru
.reqsenselen
= MAX_REQ_SENSE_LEN
;
1170 uio
.pthru
.dataxferaddr
= (intptr_t)data
;
1171 uio
.pthru
.dataxferlen
= dataLen
;
1172 memcpy(uio
.pthru
.cdb
, cdb
, cdbLen
);
1174 rc
=ioctl(m_fd
, MEGAIOCCMD
, &uio
);
1175 if (uio
.pthru
.scsistatus
|| rc
!= 0) {
1176 return set_err((errno
? errno
: EIO
), "megadev_cmd result: %d.%d = %d/%d",
1177 m_hba
, m_disknum
, errno
,
1178 uio
.pthru
.scsistatus
);
1183 /////////////////////////////////////////////////////////////////////////////
1184 /// CCISS RAID support
1186 #ifdef HAVE_LINUX_CCISS_IOCTL_H
1188 class linux_cciss_device
1189 : public /*implements*/ scsi_device
,
1190 public /*extends*/ linux_smart_device
1193 linux_cciss_device(smart_interface
* intf
, const char * name
, unsigned char disknum
);
1195 virtual bool scsi_pass_through(scsi_cmnd_io
* iop
);
1198 unsigned char m_disknum
; ///< Disk number.
1201 linux_cciss_device::linux_cciss_device(smart_interface
* intf
,
1202 const char * dev_name
, unsigned char disknum
)
1203 : smart_device(intf
, dev_name
, "cciss", "cciss"),
1204 linux_smart_device(O_RDWR
| O_NONBLOCK
),
1207 set_info().info_name
= strprintf("%s [cciss_disk_%02d]", dev_name
, disknum
);
1210 bool linux_cciss_device::scsi_pass_through(scsi_cmnd_io
* iop
)
1212 int status
= cciss_io_interface(get_fd(), m_disknum
, iop
, scsi_debugmode
);
1214 return set_err(-status
);
1218 #endif // HAVE_LINUX_CCISS_IOCTL_H
1220 /////////////////////////////////////////////////////////////////////////////
1221 /// AMCC/3ware RAID support
1223 class linux_escalade_device
1224 : public /*implements*/ ata_device
,
1225 public /*extends*/ linux_smart_device
1228 enum escalade_type_t
{
1230 AMCC_3WARE_678K_CHAR
,
1231 AMCC_3WARE_9000_CHAR
,
1232 AMCC_3WARE_9700_CHAR
1235 linux_escalade_device(smart_interface
* intf
, const char * dev_name
,
1236 escalade_type_t escalade_type
, int disknum
);
1238 virtual bool open();
1240 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
);
1243 escalade_type_t m_escalade_type
; ///< Controller type
1244 int m_disknum
; ///< Disk number.
1247 linux_escalade_device::linux_escalade_device(smart_interface
* intf
, const char * dev_name
,
1248 escalade_type_t escalade_type
, int disknum
)
1249 : smart_device(intf
, dev_name
, "3ware", "3ware"),
1250 linux_smart_device(O_RDONLY
| O_NONBLOCK
),
1251 m_escalade_type(escalade_type
), m_disknum(disknum
)
1253 set_info().info_name
= strprintf("%s [3ware_disk_%02d]", dev_name
, disknum
);
1256 /* This function will setup and fix device nodes for a 3ware controller. */
1257 #define MAJOR_STRING_LENGTH 3
1258 #define DEVICE_STRING_LENGTH 32
1259 #define NODE_STRING_LENGTH 16
1260 static int setup_3ware_nodes(const char *nodename
, const char *driver_name
)
1264 char majorstring
[MAJOR_STRING_LENGTH
+1];
1265 char device_name
[DEVICE_STRING_LENGTH
+1];
1266 char nodestring
[NODE_STRING_LENGTH
];
1267 struct stat stat_buf
;
1271 security_context_t orig_context
= NULL
;
1272 security_context_t node_context
= NULL
;
1273 int selinux_enabled
= is_selinux_enabled();
1274 int selinux_enforced
= security_getenforce();
1277 /* First try to open up /proc/devices */
1278 if (!(file
= fopen("/proc/devices", "r"))) {
1279 pout("Error opening /proc/devices to check/create 3ware device nodes\n");
1281 return 0; // don't fail here: user might not have /proc !
1284 /* Attempt to get device major number */
1285 while (EOF
!= fscanf(file
, "%3s %32s", majorstring
, device_name
)) {
1286 majorstring
[MAJOR_STRING_LENGTH
]='\0';
1287 device_name
[DEVICE_STRING_LENGTH
]='\0';
1288 if (!strncmp(device_name
, nodename
, DEVICE_STRING_LENGTH
)) {
1289 tw_major
= atoi(majorstring
);
1295 /* See if we found a major device number */
1297 pout("No major number for /dev/%s listed in /proc/devices. Is the %s driver loaded?\n", nodename
, driver_name
);
1301 /* Prepare a database of contexts for files in /dev
1302 * and save the current context */
1303 if (selinux_enabled
) {
1304 if (matchpathcon_init_prefix(NULL
, "/dev") < 0)
1305 pout("Error initializing contexts database for /dev");
1306 if (getfscreatecon(&orig_context
) < 0) {
1307 pout("Error retrieving original SELinux fscreate context");
1308 if (selinux_enforced
)
1309 matchpathcon_fini();
1314 /* Now check if nodes are correct */
1315 for (index
=0; index
<16; index
++) {
1316 snprintf(nodestring
, sizeof(nodestring
), "/dev/%s%d", nodename
, index
);
1318 /* Get context of the node and set it as the default */
1319 if (selinux_enabled
) {
1320 if (matchpathcon(nodestring
, S_IRUSR
| S_IWUSR
, &node_context
) < 0) {
1321 pout("Could not retrieve context for %s", nodestring
);
1322 if (selinux_enforced
) {
1327 if (setfscreatecon(node_context
) < 0) {
1328 pout ("Error setting default fscreate context");
1329 if (selinux_enforced
) {
1336 /* Try to stat the node */
1337 if ((stat(nodestring
, &stat_buf
))) {
1338 pout("Node %s does not exist and must be created. Check the udev rules.\n", nodestring
);
1339 /* Create a new node if it doesn't exist */
1340 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
1341 pout("problem creating 3ware device nodes %s", nodestring
);
1347 if (selinux_enabled
&& node_context
) {
1348 freecon(node_context
);
1349 node_context
= NULL
;
1356 /* See if nodes major and minor numbers are correct */
1357 if ((tw_major
!= (int)(major(stat_buf
.st_rdev
))) ||
1358 (index
!= (int)(minor(stat_buf
.st_rdev
))) ||
1359 (!S_ISCHR(stat_buf
.st_mode
))) {
1360 pout("Node %s has wrong major/minor number and must be created anew."
1361 " Check the udev rules.\n", nodestring
);
1362 /* Delete the old node */
1363 if (unlink(nodestring
)) {
1364 pout("problem unlinking stale 3ware device node %s", nodestring
);
1370 /* Make a new node */
1371 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
1372 pout("problem creating 3ware device nodes %s", nodestring
);
1379 if (selinux_enabled
&& node_context
) {
1380 freecon(node_context
);
1381 node_context
= NULL
;
1387 if (selinux_enabled
) {
1388 if(setfscreatecon(orig_context
) < 0) {
1389 pout("Error re-setting original fscreate context");
1390 if (selinux_enforced
)
1394 freecon(orig_context
);
1396 freecon(node_context
);
1397 matchpathcon_fini();
1403 bool linux_escalade_device::open()
1405 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
||
1406 m_escalade_type
== AMCC_3WARE_678K_CHAR
) {
1407 // the device nodes for these controllers are dynamically assigned,
1408 // so we need to check that they exist with the correct major
1409 // numbers and if not, create them
1410 const char * node
= (m_escalade_type
== AMCC_3WARE_9700_CHAR
? "twl" :
1411 m_escalade_type
== AMCC_3WARE_9000_CHAR
? "twa" :
1413 const char * driver
= (m_escalade_type
== AMCC_3WARE_9700_CHAR
? "3w-sas" :
1414 m_escalade_type
== AMCC_3WARE_9000_CHAR
? "3w-9xxx" :
1416 if (setup_3ware_nodes(node
, driver
))
1417 return set_err((errno
? errno
: ENXIO
), "setup_3ware_nodes(\"%s\", \"%s\") failed", node
, driver
);
1419 // Continue with default open
1420 return linux_smart_device::open();
1423 // TODO: Function no longer useful
1424 //void printwarning(smart_command_set command);
1427 // This is an interface routine meant to isolate the OS dependent
1428 // parts of the code, and to provide a debugging interface. Each
1429 // different port and OS needs to provide it's own interface. This
1430 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
1431 // commands to be passed through the SCSI driver.
1432 // DETAILED DESCRIPTION OF ARGUMENTS
1433 // fd: is the file descriptor provided by open()
1434 // disknum is the disk number (0 to 15) in the RAID array
1435 // escalade_type indicates the type of controller type, and if scsi or char interface is used
1436 // command: defines the different operations.
1437 // select: additional input data if needed (which log, which type of
1439 // data: location to write output data, if needed (512 bytes).
1440 // Note: not all commands use all arguments.
1442 // -1 if the command failed
1443 // 0 if the command succeeded,
1444 // STATUS_CHECK routine:
1445 // -1 if the command failed
1446 // 0 if the command succeeded and disk SMART status is "OK"
1447 // 1 if the command succeeded and disk SMART status is "FAILING"
1449 /* 512 is the max payload size: increase if needed */
1450 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
1451 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
1452 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
1453 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
1455 bool linux_escalade_device::ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
)
1457 if (!ata_cmd_is_ok(in
,
1458 true, // data_out_support
1459 false, // TODO: multi_sector_support
1460 true) // ata_48bit_support
1464 // Used by both the SCSI and char interfaces
1465 TW_Passthru
*passthru
=NULL
;
1466 char ioctl_buffer
[TW_IOCTL_BUFFER_SIZE
];
1468 // only used for SCSI device interface
1469 TW_Ioctl
*tw_ioctl
=NULL
;
1470 TW_Output
*tw_output
=NULL
;
1472 // only used for 6000/7000/8000 char device interface
1473 TW_New_Ioctl
*tw_ioctl_char
=NULL
;
1475 // only used for 9000 character device interface
1476 TW_Ioctl_Buf_Apache
*tw_ioctl_apache
=NULL
;
1478 memset(ioctl_buffer
, 0, TW_IOCTL_BUFFER_SIZE
);
1480 // TODO: Handle controller differences by different classes
1481 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
) {
1482 tw_ioctl_apache
= (TW_Ioctl_Buf_Apache
*)ioctl_buffer
;
1483 tw_ioctl_apache
->driver_command
.control_code
= TW_IOCTL_FIRMWARE_PASS_THROUGH
;
1484 tw_ioctl_apache
->driver_command
.buffer_length
= 512; /* payload size */
1485 passthru
= (TW_Passthru
*)&(tw_ioctl_apache
->firmware_command
.command
.oldcommand
);
1487 else if (m_escalade_type
==AMCC_3WARE_678K_CHAR
) {
1488 tw_ioctl_char
= (TW_New_Ioctl
*)ioctl_buffer
;
1489 tw_ioctl_char
->data_buffer_length
= 512;
1490 passthru
= (TW_Passthru
*)&(tw_ioctl_char
->firmware_command
);
1492 else if (m_escalade_type
==AMCC_3WARE_678K
) {
1493 tw_ioctl
= (TW_Ioctl
*)ioctl_buffer
;
1494 tw_ioctl
->cdb
[0] = TW_IOCTL
;
1495 tw_ioctl
->opcode
= TW_ATA_PASSTHRU
;
1496 tw_ioctl
->input_length
= 512; // correct even for non-data commands
1497 tw_ioctl
->output_length
= 512; // correct even for non-data commands
1498 tw_output
= (TW_Output
*)tw_ioctl
;
1499 passthru
= (TW_Passthru
*)&(tw_ioctl
->input_data
);
1502 return set_err(ENOSYS
,
1503 "Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1504 "Please contact " PACKAGE_BUGREPORT
"\n", (int)m_escalade_type
, m_disknum
);
1507 // Same for (almost) all commands - but some reset below
1508 passthru
->byte0
.opcode
= TW_OP_ATA_PASSTHRU
;
1509 passthru
->request_id
= 0xFF;
1510 passthru
->unit
= m_disknum
;
1511 passthru
->status
= 0;
1512 passthru
->flags
= 0x1;
1516 const ata_in_regs_48bit
& r
= in
.in_regs
;
1517 passthru
->features
= r
.features_16
;
1518 passthru
->sector_count
= r
.sector_count_16
;
1519 passthru
->sector_num
= r
.lba_low_16
;
1520 passthru
->cylinder_lo
= r
.lba_mid_16
;
1521 passthru
->cylinder_hi
= r
.lba_high_16
;
1522 passthru
->drive_head
= r
.device
;
1523 passthru
->command
= r
.command
;
1526 // Is this a command that reads or returns 512 bytes?
1527 // passthru->param values are:
1528 // 0x0 - non data command without TFR write check,
1529 // 0x8 - non data command with TFR write check,
1530 // 0xD - data command that returns data to host from device
1531 // 0xF - data command that writes data from host to device
1532 // passthru->size values are 0x5 for non-data and 0x07 for data
1533 bool readdata
= false;
1534 if (in
.direction
== ata_cmd_in::data_in
) {
1536 passthru
->byte0
.sgloff
= 0x5;
1537 passthru
->size
= 0x7; // TODO: Other value for multi-sector ?
1538 passthru
->param
= 0xD;
1539 // For 64-bit to work correctly, up the size of the command packet
1540 // in dwords by 1 to account for the 64-bit single sgl 'address'
1541 // field. Note that this doesn't agree with the typedefs but it's
1542 // right (agree with kernel driver behavior/typedefs).
1543 if ((m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1544 && sizeof(long) == 8)
1547 else if (in
.direction
== ata_cmd_in::no_data
) {
1548 // Non data command -- but doesn't use large sector
1549 // count register values.
1550 passthru
->byte0
.sgloff
= 0x0;
1551 passthru
->size
= 0x5;
1552 passthru
->param
= 0x8;
1553 passthru
->sector_count
= 0x0;
1555 else if (in
.direction
== ata_cmd_in::data_out
) {
1556 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1557 memcpy(tw_ioctl_apache
->data_buffer
, in
.buffer
, in
.size
);
1558 else if (m_escalade_type
== AMCC_3WARE_678K_CHAR
)
1559 memcpy(tw_ioctl_char
->data_buffer
, in
.buffer
, in
.size
);
1561 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1562 // memcpy(tw_output->output_data, data, 512);
1563 // printwarning(command); // TODO: Parameter no longer valid
1564 return set_err(ENOTSUP
, "DATA OUT not supported for this 3ware controller type");
1566 passthru
->byte0
.sgloff
= 0x5;
1567 passthru
->size
= 0x7; // TODO: Other value for multi-sector ?
1568 passthru
->param
= 0xF; // PIO data write
1569 if ((m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1570 && sizeof(long) == 8)
1574 return set_err(EINVAL
);
1576 // Now send the command down through an ioctl()
1578 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1579 ioctlreturn
=ioctl(get_fd(), TW_IOCTL_FIRMWARE_PASS_THROUGH
, tw_ioctl_apache
);
1580 else if (m_escalade_type
==AMCC_3WARE_678K_CHAR
)
1581 ioctlreturn
=ioctl(get_fd(), TW_CMD_PACKET_WITH_DATA
, tw_ioctl_char
);
1583 ioctlreturn
=ioctl(get_fd(), SCSI_IOCTL_SEND_COMMAND
, tw_ioctl
);
1585 // Deal with the different error cases
1587 if (AMCC_3WARE_678K
==m_escalade_type
1588 && in
.in_regs
.command
==ATA_SMART_CMD
1589 && ( in
.in_regs
.features
== ATA_SMART_AUTO_OFFLINE
1590 || in
.in_regs
.features
== ATA_SMART_AUTOSAVE
)
1591 && in
.in_regs
.lba_low
) {
1592 // error here is probably a kernel driver whose version is too old
1593 // printwarning(command); // TODO: Parameter no longer valid
1594 return set_err(ENOTSUP
, "Probably kernel driver too old");
1596 return set_err(EIO
);
1599 // The passthru structure is valid after return from an ioctl if:
1600 // - we are using the character interface OR
1601 // - we are using the SCSI interface and this is a NON-READ-DATA command
1602 // For SCSI interface, note that we set passthru to a different
1603 // value after ioctl().
1604 if (AMCC_3WARE_678K
==m_escalade_type
) {
1608 passthru
=(TW_Passthru
*)&(tw_output
->output_data
);
1611 // See if the ATA command failed. Now that we have returned from
1612 // the ioctl() call, if passthru is valid, then:
1613 // - passthru->status contains the 3ware controller STATUS
1614 // - passthru->command contains the ATA STATUS register
1615 // - passthru->features contains the ATA ERROR register
1617 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1618 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1619 // While we *might* decode the ATA ERROR register, at the moment it
1620 // doesn't make much sense: we don't care in detail why the error
1623 if (passthru
&& (passthru
->status
|| (passthru
->command
& 0x21))) {
1624 return set_err(EIO
);
1627 // If this is a read data command, copy data to output buffer
1629 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1630 memcpy(in
.buffer
, tw_ioctl_apache
->data_buffer
, in
.size
);
1631 else if (m_escalade_type
==AMCC_3WARE_678K_CHAR
)
1632 memcpy(in
.buffer
, tw_ioctl_char
->data_buffer
, in
.size
);
1634 memcpy(in
.buffer
, tw_output
->output_data
, in
.size
);
1637 // Return register values
1639 ata_out_regs_48bit
& r
= out
.out_regs
;
1640 r
.error
= passthru
->features
;
1641 r
.sector_count_16
= passthru
->sector_count
;
1642 r
.lba_low_16
= passthru
->sector_num
;
1643 r
.lba_mid_16
= passthru
->cylinder_lo
;
1644 r
.lba_high_16
= passthru
->cylinder_hi
;
1645 r
.device
= passthru
->drive_head
;
1646 r
.status
= passthru
->command
;
1649 // look for nonexistent devices/ports
1650 if ( in
.in_regs
.command
== ATA_IDENTIFY_DEVICE
1651 && !nonempty(in
.buffer
, in
.size
)) {
1652 return set_err(ENODEV
, "No drive on port %d", m_disknum
);
1658 /////////////////////////////////////////////////////////////////////////////
1659 /// Areca RAID support
1661 ///////////////////////////////////////////////////////////////////
1662 // SATA(ATA) device behind Areca RAID Controller
1663 class linux_areca_ata_device
1664 : public /*implements*/ areca_ata_device
,
1665 public /*extends*/ linux_smart_device
1668 linux_areca_ata_device(smart_interface
* intf
, const char * dev_name
, int disknum
, int encnum
= 1);
1669 virtual smart_device
* autodetect_open();
1670 virtual bool arcmsr_lock();
1671 virtual bool arcmsr_unlock();
1672 virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io
* iop
);
1675 ///////////////////////////////////////////////////////////////////
1676 // SAS(SCSI) device behind Areca RAID Controller
1677 class linux_areca_scsi_device
1678 : public /*implements*/ areca_scsi_device
,
1679 public /*extends*/ linux_smart_device
1682 linux_areca_scsi_device(smart_interface
* intf
, const char * dev_name
, int disknum
, int encnum
= 1);
1683 virtual smart_device
* autodetect_open();
1684 virtual bool arcmsr_lock();
1685 virtual bool arcmsr_unlock();
1686 virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io
* iop
);
1689 // Looks in /proc/scsi to suggest correct areca devices
1690 static int find_areca_in_proc()
1692 const char* proc_format_string
="host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n";
1694 // check data formwat
1695 FILE *fp
=fopen("/proc/scsi/sg/device_hdr", "r");
1697 pout("Unable to open /proc/scsi/sg/device_hdr for reading\n");
1701 // get line, compare to format
1704 char *out
= fgets(linebuf
, 256, fp
);
1707 pout("Unable to read contents of /proc/scsi/sg/device_hdr\n");
1711 if (strcmp(linebuf
, proc_format_string
)) {
1713 // Fix this by comparing only tokens not white space!!
1714 pout("Unexpected format %s in /proc/scsi/sg/device_hdr\n", proc_format_string
);
1718 // Format is understood, now search for correct device
1719 fp
=fopen("/proc/scsi/sg/devices", "r");
1721 int host
, chan
, id
, lun
, type
, opens
, qdepth
, busy
, online
;
1724 // search all lines of /proc/scsi/sg/devices
1725 while (9 == fscanf(fp
, "%d %d %d %d %d %d %d %d %d", &host
, &chan
, &id
, &lun
, &type
, &opens
, &qdepth
, &busy
, &online
)) {
1727 if (id
== 16 && type
== 3) {
1728 // devices with id=16 and type=3 might be Areca controllers
1729 pout("Device /dev/sg%d appears to be an Areca controller.\n", dev
);
1737 // Areca RAID Controller(SATA Disk)
1738 linux_areca_ata_device::linux_areca_ata_device(smart_interface
* intf
, const char * dev_name
, int disknum
, int encnum
)
1739 : smart_device(intf
, dev_name
, "areca", "areca"),
1740 linux_smart_device(O_RDWR
| O_EXCL
| O_NONBLOCK
)
1742 set_disknum(disknum
);
1744 set_info().info_name
= strprintf("%s [areca_disk#%02d_enc#%02d]", dev_name
, disknum
, encnum
);
1747 smart_device
* linux_areca_ata_device::autodetect_open()
1751 // autodetect device type
1752 is_ata
= arcmsr_get_dev_type();
1766 smart_device_auto_ptr
newdev(new linux_areca_scsi_device(smi(), get_dev_name(), get_disknum(), get_encnum()));
1769 newdev
->open(); // TODO: Can possibly pass open fd
1771 return newdev
.release();
1774 int linux_areca_ata_device::arcmsr_do_scsi_io(struct scsi_cmnd_io
* iop
)
1776 int ioctlreturn
= 0;
1780 find_areca_in_proc();
1784 ioctlreturn
= do_normal_scsi_cmnd_io(get_fd(), iop
, scsi_debugmode
);
1785 if ( ioctlreturn
|| iop
->scsi_status
)
1794 bool linux_areca_ata_device::arcmsr_lock()
1799 bool linux_areca_ata_device::arcmsr_unlock()
1804 // Areca RAID Controller(SAS Device)
1805 linux_areca_scsi_device::linux_areca_scsi_device(smart_interface
* intf
, const char * dev_name
, int disknum
, int encnum
)
1806 : smart_device(intf
, dev_name
, "areca", "areca"),
1807 linux_smart_device(O_RDWR
| O_EXCL
| O_NONBLOCK
)
1809 set_disknum(disknum
);
1811 set_info().info_name
= strprintf("%s [areca_disk#%02d_enc#%02d]", dev_name
, disknum
, encnum
);
1814 smart_device
* linux_areca_scsi_device::autodetect_open()
1819 int linux_areca_scsi_device::arcmsr_do_scsi_io(struct scsi_cmnd_io
* iop
)
1821 int ioctlreturn
= 0;
1825 find_areca_in_proc();
1829 ioctlreturn
= do_normal_scsi_cmnd_io(get_fd(), iop
, scsi_debugmode
);
1830 if ( ioctlreturn
|| iop
->scsi_status
)
1839 bool linux_areca_scsi_device::arcmsr_lock()
1844 bool linux_areca_scsi_device::arcmsr_unlock()
1849 /////////////////////////////////////////////////////////////////////////////
1852 class linux_marvell_device
1853 : public /*implements*/ ata_device_with_command_set
,
1854 public /*extends*/ linux_smart_device
1857 linux_marvell_device(smart_interface
* intf
, const char * dev_name
, const char * req_type
);
1860 virtual int ata_command_interface(smart_command_set command
, int select
, char * data
);
1863 linux_marvell_device::linux_marvell_device(smart_interface
* intf
,
1864 const char * dev_name
, const char * req_type
)
1865 : smart_device(intf
, dev_name
, "marvell", req_type
),
1866 linux_smart_device(O_RDONLY
| O_NONBLOCK
)
1870 int linux_marvell_device::ata_command_interface(smart_command_set command
, int select
, char * data
)
1879 mvsata_scsi_cmd smart_command
;
1880 unsigned char *buff
= (unsigned char *)&smart_command
.cmd
[6];
1881 // See struct hd_drive_cmd_hdr in hdreg.h
1882 // buff[0]: ATA COMMAND CODE REGISTER
1883 // buff[1]: ATA SECTOR NUMBER REGISTER
1884 // buff[2]: ATA FEATURES REGISTER
1885 // buff[3]: ATA SECTOR COUNT REGISTER
1887 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
1888 memset(&smart_command
, 0, sizeof(smart_command
));
1889 smart_command
.inlen
= 540;
1890 smart_command
.outlen
= 540;
1891 smart_command
.cmd
[0] = 0xC; //Vendor-specific code
1892 smart_command
.cmd
[4] = 6; //command length
1894 buff
[0] = ATA_SMART_CMD
;
1896 case CHECK_POWER_MODE
:
1897 buff
[0]=ATA_CHECK_POWER_MODE
;
1900 buff
[2]=ATA_SMART_READ_VALUES
;
1903 case READ_THRESHOLDS
:
1904 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
1905 copydata
=buff
[1]=buff
[3]=1;
1908 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
1913 buff
[0]=ATA_IDENTIFY_DEVICE
;
1917 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
1921 buff
[2]=ATA_SMART_ENABLE
;
1925 buff
[2]=ATA_SMART_DISABLE
;
1930 // this command only says if SMART is working. It could be
1931 // replaced with STATUS_CHECK below.
1932 buff
[2] = ATA_SMART_STATUS
;
1935 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
1936 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1939 buff
[2]=ATA_SMART_AUTOSAVE
;
1940 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1942 case IMMEDIATE_OFFLINE
:
1943 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
1947 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command
);
1951 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
1953 // We are now doing the HDIO_DRIVE_CMD type ioctl.
1954 if (ioctl(get_fd(), SCSI_IOCTL_SEND_COMMAND
, (void *)&smart_command
))
1957 if (command
==CHECK_POWER_MODE
) {
1958 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
1959 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
1964 // Always succeed on a SMART status, as a disk that failed returned
1965 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
1966 if (command
== STATUS
)
1968 //Data returned is starting from 0 offset
1969 if (command
== STATUS_CHECK
)
1971 // Cyl low and Cyl high unchanged means "Good SMART status"
1972 if (buff
[4] == 0x4F && buff
[5] == 0xC2)
1974 // These values mean "Bad SMART status"
1975 if (buff
[4] == 0xF4 && buff
[5] == 0x2C)
1977 // We haven't gotten output that makes sense; print out some debugging info
1978 syserror("Error SMART Status command failed");
1979 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT
);
1980 pout("Register values returned from SMART Status command are:\n");
1981 pout("CMD =0x%02x\n",(int)buff
[0]);
1982 pout("FR =0x%02x\n",(int)buff
[1]);
1983 pout("NS =0x%02x\n",(int)buff
[2]);
1984 pout("SC =0x%02x\n",(int)buff
[3]);
1985 pout("CL =0x%02x\n",(int)buff
[4]);
1986 pout("CH =0x%02x\n",(int)buff
[5]);
1987 pout("SEL=0x%02x\n",(int)buff
[6]);
1992 memcpy(data
, buff
, 512);
1996 /////////////////////////////////////////////////////////////////////////////
1997 /// Highpoint RAID support
1999 class linux_highpoint_device
2000 : public /*implements*/ ata_device_with_command_set
,
2001 public /*extends*/ linux_smart_device
2004 linux_highpoint_device(smart_interface
* intf
, const char * dev_name
,
2005 unsigned char controller
, unsigned char channel
, unsigned char port
);
2008 virtual int ata_command_interface(smart_command_set command
, int select
, char * data
);
2011 unsigned char m_hpt_data
[3]; ///< controller/channel/port
2014 linux_highpoint_device::linux_highpoint_device(smart_interface
* intf
, const char * dev_name
,
2015 unsigned char controller
, unsigned char channel
, unsigned char port
)
2016 : smart_device(intf
, dev_name
, "hpt", "hpt"),
2017 linux_smart_device(O_RDONLY
| O_NONBLOCK
)
2019 m_hpt_data
[0] = controller
; m_hpt_data
[1] = channel
; m_hpt_data
[2] = port
;
2020 set_info().info_name
= strprintf("%s [hpt_disk_%u/%u/%u]", dev_name
, m_hpt_data
[0], m_hpt_data
[1], m_hpt_data
[2]);
2023 // this implementation is derived from ata_command_interface with a header
2024 // packing for highpoint linux driver ioctl interface
2026 // ioctl(fd,HPTIO_CTL,buff)
2029 // structure of hpt_buff
2030 // +----+----+----+----+--------------------.....---------------------+
2031 // | 1 | 2 | 3 | 4 | 5 |
2032 // +----+----+----+----+--------------------.....---------------------+
2034 // 1: The target controller [ int ( 4 Bytes ) ]
2035 // 2: The channel of the target controllee [ int ( 4 Bytes ) ]
2036 // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
2037 // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
2038 // 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
2039 // if no pmport device, set to 1 or leave blank
2040 // 5: data [ void * ( var leangth ) ]
2042 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
2044 int linux_highpoint_device::ata_command_interface(smart_command_set command
, int select
, char * data
)
2046 unsigned char hpt_buff
[4*sizeof(int) + STRANGE_BUFFER_LENGTH
];
2047 unsigned int *hpt
= (unsigned int *)hpt_buff
;
2048 unsigned char *buff
= &hpt_buff
[4*sizeof(int)];
2050 const int HDIO_DRIVE_CMD_OFFSET
= 4;
2052 memset(hpt_buff
, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH
);
2053 hpt
[0] = m_hpt_data
[0]; // controller id
2054 hpt
[1] = m_hpt_data
[1]; // channel number
2055 hpt
[3] = m_hpt_data
[2]; // pmport number
2057 buff
[0]=ATA_SMART_CMD
;
2059 case CHECK_POWER_MODE
:
2060 buff
[0]=ATA_CHECK_POWER_MODE
;
2064 buff
[2]=ATA_SMART_READ_VALUES
;
2068 case READ_THRESHOLDS
:
2069 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
2074 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
2082 buff
[0]=ATA_IDENTIFY_DEVICE
;
2087 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
2092 buff
[2]=ATA_SMART_ENABLE
;
2096 buff
[2]=ATA_SMART_DISABLE
;
2100 buff
[2]=ATA_SMART_STATUS
;
2103 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
2107 buff
[2]=ATA_SMART_AUTOSAVE
;
2110 case IMMEDIATE_OFFLINE
:
2111 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
2115 buff
[1]=ATA_SMART_STATUS
;
2118 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
2119 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
2124 if (command
==WRITE_LOG
) {
2125 unsigned char task
[4*sizeof(int)+sizeof(ide_task_request_t
)+512];
2126 unsigned int *hpt_tf
= (unsigned int *)task
;
2127 ide_task_request_t
*reqtask
= (ide_task_request_t
*)(&task
[4*sizeof(int)]);
2128 task_struct_t
*taskfile
= (task_struct_t
*)reqtask
->io_ports
;
2131 memset(task
, 0, sizeof(task
));
2133 hpt_tf
[0] = m_hpt_data
[0]; // controller id
2134 hpt_tf
[1] = m_hpt_data
[1]; // channel number
2135 hpt_tf
[3] = m_hpt_data
[2]; // pmport number
2136 hpt_tf
[2] = HDIO_DRIVE_TASKFILE
; // real hd ioctl
2139 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
2140 taskfile
->sector_count
= 1;
2141 taskfile
->sector_number
= select
;
2142 taskfile
->low_cylinder
= 0x4f;
2143 taskfile
->high_cylinder
= 0xc2;
2144 taskfile
->device_head
= 0;
2145 taskfile
->command
= ATA_SMART_CMD
;
2147 reqtask
->data_phase
= TASKFILE_OUT
;
2148 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
2149 reqtask
->out_size
= 512;
2150 reqtask
->in_size
= 0;
2152 memcpy(task
+sizeof(ide_task_request_t
)+4*sizeof(int), data
, 512);
2154 if ((retval
=ioctl(get_fd(), HPTIO_CTL
, task
))) {
2155 if (retval
==-EINVAL
)
2156 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
2162 if (command
==STATUS_CHECK
){
2164 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
2165 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
2169 hpt
[2] = HDIO_DRIVE_TASK
;
2171 if ((retval
=ioctl(get_fd(), HPTIO_CTL
, hpt_buff
))) {
2172 if (retval
==-EINVAL
) {
2173 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
2174 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
2177 syserror("Error SMART Status command failed");
2181 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
2184 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
2187 syserror("Error SMART Status command failed");
2188 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
2189 pout("Register values returned from SMART Status command are:\n");
2190 pout("CMD=0x%02x\n",(int)buff
[0]);
2191 pout("FR =0x%02x\n",(int)buff
[1]);
2192 pout("NS =0x%02x\n",(int)buff
[2]);
2193 pout("SC =0x%02x\n",(int)buff
[3]);
2194 pout("CL =0x%02x\n",(int)buff
[4]);
2195 pout("CH =0x%02x\n",(int)buff
[5]);
2196 pout("SEL=0x%02x\n",(int)buff
[6]);
2201 if (command
==IDENTIFY
|| command
==PIDENTIFY
) {
2202 unsigned char deviceid
[4*sizeof(int)+512*sizeof(char)];
2203 unsigned int *hpt_id
= (unsigned int *)deviceid
;
2205 hpt_id
[0] = m_hpt_data
[0]; // controller id
2206 hpt_id
[1] = m_hpt_data
[1]; // channel number
2207 hpt_id
[3] = m_hpt_data
[2]; // pmport number
2209 hpt_id
[2] = HDIO_GET_IDENTITY
;
2210 if (!ioctl(get_fd(), HPTIO_CTL
, deviceid
) && (deviceid
[4*sizeof(int)] & 0x8000))
2211 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
2215 hpt
[2] = HDIO_DRIVE_CMD
;
2216 if ((ioctl(get_fd(), HPTIO_CTL
, hpt_buff
)))
2219 if (command
==CHECK_POWER_MODE
)
2220 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
2223 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
2228 #if 0 // TODO: Migrate from 'smart_command_set' to 'ata_in_regs' OR remove the function
2229 // Utility function for printing warnings
2230 void printwarning(smart_command_set command
){
2231 static int printed
[4]={0,0,0,0};
2232 const char* message
=
2233 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
2234 "applying a simple 3w-xxxx driver patch that can be found here:\n"
2235 PACKAGE_HOMEPAGE
"\n"
2236 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
2238 if (command
==AUTO_OFFLINE
&& !printed
[0]) {
2240 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message
);
2242 else if (command
==AUTOSAVE
&& !printed
[1]) {
2244 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message
);
2246 else if (command
==STATUS_CHECK
&& !printed
[2]) {
2248 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message
);
2250 else if (command
==WRITE_LOG
&& !printed
[3]) {
2252 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
2259 /////////////////////////////////////////////////////////////////////////////
2260 /// SCSI open with autodetection support
2262 smart_device
* linux_scsi_device::autodetect_open()
2268 // No Autodetection if device type was specified by user
2269 bool sat_only
= false;
2270 if (*get_req_type()) {
2271 // Detect SAT if device object was created by scan_smart_devices().
2272 if (!(m_scanning
&& !strcmp(get_req_type(), "sat")))
2277 // The code below is based on smartd.cpp:SCSIFilterKnown()
2280 unsigned char req_buff
[64] = {0, };
2282 if (scsiStdInquiry(this, req_buff
, req_len
)) {
2283 // Marvell controllers fail on a 36 bytes StdInquiry, but 64 suffices
2284 // watch this spot ... other devices could lock up here
2286 if (scsiStdInquiry(this, req_buff
, req_len
)) {
2287 // device doesn't like INQUIRY commands
2289 set_err(EIO
, "INQUIRY failed");
2294 int avail_len
= req_buff
[4] + 5;
2295 int len
= (avail_len
< req_len
? avail_len
: req_len
);
2299 set_err(EIO
, "INQUIRY too short for SAT");
2304 // Use INQUIRY to detect type
2308 if (!memcmp(req_buff
+ 8, "3ware", 5) || !memcmp(req_buff
+ 8, "AMCC", 4)) {
2310 set_err(EINVAL
, "AMCC/3ware controller, please try adding '-d 3ware,N',\n"
2311 "you may need to replace %s with /dev/twlN, /dev/twaN or /dev/tweN", get_dev_name());
2316 if (!memcmp(req_buff
+ 8, "DELL PERC", 12) || !memcmp(req_buff
+ 8, "MegaRAID", 8)
2317 || !memcmp(req_buff
+ 16, "PERC H700", 9) || !memcmp(req_buff
+ 8, "LSI\0",4)
2320 set_err(EINVAL
, "DELL or MegaRaid controller, please try adding '-d megaraid,N'");
2325 if (len
>= 42 && !memcmp(req_buff
+ 36, "MVSATA", 6)) {
2326 //pout("Device %s: using '-d marvell' for ATA disk with Marvell driver\n", get_dev_name());
2328 smart_device_auto_ptr
newdev(
2329 new linux_marvell_device(smi(), get_dev_name(), get_req_type())
2331 newdev
->open(); // TODO: Can possibly pass open fd
2333 return newdev
.release();
2339 smart_device
* newdev
= smi()->autodetect_sat_device(this, req_buff
, len
);
2341 // NOTE: 'this' is now owned by '*newdev'
2345 // Nothing special found
2349 set_err(EIO
, "Not a SAT device");
2354 //////////////////////////////////////////////////////////////////////
2355 // USB bridge ID detection
2357 // Read USB ID from /sys file
2358 static bool read_id(const std::string
& path
, unsigned short & id
)
2360 FILE * f
= fopen(path
.c_str(), "r");
2364 bool ok
= (fscanf(f
, "%hx%n", &id
, &n
) == 1 && n
== 4);
2369 // Get USB bridge ID for "sdX"
2370 static bool get_usb_id(const char * name
, unsigned short & vendor_id
,
2371 unsigned short & product_id
, unsigned short & version
)
2373 // Only "sdX" supported
2374 if (!(!strncmp(name
, "sd", 2) && !strchr(name
, '/')))
2377 // Start search at dir referenced by symlink "/sys/block/sdX/device"
2378 // -> "/sys/devices/.../usb*/.../host*/target*/..."
2379 std::string dir
= strprintf("/sys/block/%s/device", name
);
2381 // Stop search at "/sys/devices"
2383 if (stat("/sys/devices", &st
))
2385 ino_t stop_ino
= st
.st_ino
;
2387 // Search in parent directories until "idVendor" is found,
2388 // fail if "/sys/devices" reached or too many iterations
2392 if (!(++cnt
< 10 && !stat(dir
.c_str(), &st
) && st
.st_ino
!= stop_ino
))
2394 } while (access((dir
+ "/idVendor").c_str(), 0));
2397 if (!( read_id(dir
+ "/idVendor", vendor_id
)
2398 && read_id(dir
+ "/idProduct", product_id
)
2399 && read_id(dir
+ "/bcdDevice", version
) ))
2402 if (scsi_debugmode
> 1)
2403 pout("USB ID = 0x%04x:0x%04x (0x%03x)\n", vendor_id
, product_id
, version
);
2407 //////////////////////////////////////////////////////////////////////
2410 class linux_smart_interface
2411 : public /*implements*/ smart_interface
2414 virtual std::string
get_os_version_str();
2416 virtual std::string
get_app_examples(const char * appname
);
2418 virtual bool scan_smart_devices(smart_device_list
& devlist
, const char * type
,
2419 const char * pattern
= 0);
2422 virtual ata_device
* get_ata_device(const char * name
, const char * type
);
2424 virtual scsi_device
* get_scsi_device(const char * name
, const char * type
);
2426 virtual smart_device
* autodetect_smart_device(const char * name
);
2428 virtual smart_device
* get_custom_smart_device(const char * name
, const char * type
);
2430 virtual std::string
get_valid_custom_dev_types_str();
2433 bool get_dev_list(smart_device_list
& devlist
, const char * pattern
,
2434 bool scan_ata
, bool scan_scsi
, const char * req_type
, bool autodetect
);
2435 bool get_dev_megasas(smart_device_list
& devlist
);
2436 smart_device
* missing_option(const char * opt
);
2437 int megasas_dcmd_cmd(int bus_no
, uint32_t opcode
, void *buf
,
2438 size_t bufsize
, uint8_t *mbox
, size_t mboxlen
, uint8_t *statusp
);
2439 int megasas_pd_add_list(int bus_no
, smart_device_list
& devlist
);
2442 std::string
linux_smart_interface::get_os_version_str()
2446 return strprintf("%s-linux-%s", u
.machine
, u
.release
);
2448 return SMARTMONTOOLS_BUILD_HOST
;
2451 std::string
linux_smart_interface::get_app_examples(const char * appname
)
2453 if (!strcmp(appname
, "smartctl"))
2454 return smartctl_examples
;
2458 // we are going to take advantage of the fact that Linux's devfs will only
2459 // have device entries for devices that exist. So if we get the equivalent of
2460 // ls /dev/hd[a-t], we have all the ATA devices on the system
2461 bool linux_smart_interface::get_dev_list(smart_device_list
& devlist
,
2462 const char * pattern
, bool scan_ata
, bool scan_scsi
,
2463 const char * req_type
, bool autodetect
)
2465 // Use glob to look for any directory entries matching the pattern
2467 memset(&globbuf
, 0, sizeof(globbuf
));
2468 int retglob
= glob(pattern
, GLOB_ERR
, NULL
, &globbuf
);
2470 // glob failed: free memory and return
2473 if (retglob
==GLOB_NOMATCH
){
2474 pout("glob(3) found no matches for pattern %s\n", pattern
);
2478 if (retglob
==GLOB_NOSPACE
)
2479 set_err(ENOMEM
, "glob(3) ran out of memory matching pattern %s", pattern
);
2480 #ifdef GLOB_ABORTED // missing in old versions of glob.h
2481 else if (retglob
==GLOB_ABORTED
)
2482 set_err(EINVAL
, "glob(3) aborted matching pattern %s", pattern
);
2485 set_err(EINVAL
, "Unexplained error in glob(3) of pattern %s", pattern
);
2490 // did we find too many paths?
2491 const int max_pathc
= 32;
2492 int n
= (int)globbuf
.gl_pathc
;
2493 if (n
> max_pathc
) {
2494 pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n",
2495 n
, max_pathc
, pattern
, n
- max_pathc
);
2499 // now step through the list returned by glob. If not a link, copy
2500 // to list. If it is a link, evaluate it and see if the path ends
2502 for (int i
= 0; i
< n
; i
++){
2503 // see if path is a link
2505 int retlink
= readlink(globbuf
.gl_pathv
[i
], linkbuf
, sizeof(linkbuf
)-1);
2507 char tmpname
[1024]={0};
2508 const char * name
= 0;
2509 bool is_scsi
= scan_scsi
;
2510 // if not a link (or a strange link), keep it
2511 if (retlink
<=0 || retlink
>1023)
2512 name
= globbuf
.gl_pathv
[i
];
2514 // or if it's a link that points to a disc, follow it
2515 linkbuf
[retlink
] = 0;
2517 if ((p
=strrchr(linkbuf
, '/')) && !strcmp(p
+1, "disc"))
2518 // This is the branch of the code that gets followed if we are
2519 // using devfs WITH traditional compatibility links. In this
2520 // case, we add the traditional device name to the list that
2522 name
= globbuf
.gl_pathv
[i
];
2524 // This is the branch of the code that gets followed if we are
2525 // using devfs WITHOUT traditional compatibility links. In
2526 // this case, we check that the link to the directory is of
2527 // the correct type, and then append "disc" to it.
2528 bool match_ata
= strstr(linkbuf
, "ide");
2529 bool match_scsi
= strstr(linkbuf
, "scsi");
2530 if (((match_ata
&& scan_ata
) || (match_scsi
&& scan_scsi
)) && !(match_ata
&& match_scsi
)) {
2531 is_scsi
= match_scsi
;
2532 snprintf(tmpname
, sizeof(tmpname
), "%s/disc", globbuf
.gl_pathv
[i
]);
2539 // Found a name, add device to list.
2542 dev
= autodetect_smart_device(name
);
2544 dev
= new linux_scsi_device(this, name
, req_type
, true /*scanning*/);
2546 dev
= new linux_ata_device(this, name
, req_type
);
2547 if (dev
) // autodetect_smart_device() may return nullptr.
2548 devlist
.push_back(dev
);
2557 // getting devices from LSI SAS MegaRaid, if available
2558 bool linux_smart_interface::get_dev_megasas(smart_device_list
& devlist
)
2560 /* Scanning of disks on MegaRaid device */
2561 /* Perform mknod of device ioctl node */
2564 bool scan_megasas
= false;
2565 FILE * fp
= fopen("/proc/devices", "r");
2566 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2568 if (sscanf(line
, "%d megaraid_sas_ioctl%n", &mjr
, &n1
) == 1 && n1
== 22) {
2569 scan_megasas
= true;
2570 n1
=mknod("/dev/megaraid_sas_ioctl_node", S_IFCHR
, makedev(mjr
, 0));
2571 if(scsi_debugmode
> 0)
2572 pout("Creating /dev/megaraid_sas_ioctl_node = %d\n", n1
>= 0 ? 0 : errno
);
2573 if (n1
>= 0 || errno
== EEXIST
)
2582 // getting bus numbers with megasas devices
2584 unsigned int host_no
= 0;
2587 /* we are using sysfs to get list of all scsi hosts */
2588 DIR * dp
= opendir ("/sys/class/scsi_host/");
2591 while ((ep
= readdir (dp
)) != NULL
) {
2592 if (!sscanf(ep
->d_name
, "host%d", &host_no
))
2594 /* proc_name should be megaraid_sas */
2595 snprintf(sysfsdir
, sizeof(sysfsdir
) - 1,
2596 "/sys/class/scsi_host/host%d/proc_name", host_no
);
2597 if((fp
= fopen(sysfsdir
, "r")) == NULL
)
2599 if(fgets(line
, sizeof(line
), fp
) != NULL
&& !strncmp(line
,"megaraid_sas",12)) {
2600 megasas_pd_add_list(host_no
, devlist
);
2604 (void) closedir (dp
);
2605 } else { /* sysfs not mounted ? */
2606 for(unsigned i
= 0; i
<=16; i
++) // trying to add devices on first 16 buses
2607 megasas_pd_add_list(i
, devlist
);
2612 bool linux_smart_interface::scan_smart_devices(smart_device_list
& devlist
,
2613 const char * type
, const char * pattern
/*= 0*/)
2616 set_err(EINVAL
, "DEVICESCAN with pattern not implemented yet");
2623 bool scan_ata
= (!*type
|| !strcmp(type
, "ata" ));
2624 // "sat" detection will be later handled in linux_scsi_device::autodetect_open()
2625 bool scan_scsi
= (!*type
|| !strcmp(type
, "scsi") || !strcmp(type
, "sat"));
2626 if (!(scan_ata
|| scan_scsi
))
2630 get_dev_list(devlist
, "/dev/hd[a-t]", true, false, type
, false);
2632 bool autodetect
= !*type
; // Try USB autodetection if no type specifed
2633 get_dev_list(devlist
, "/dev/sd[a-z]", false, true, type
, autodetect
);
2634 // Support up to 104 devices
2635 get_dev_list(devlist
, "/dev/sd[a-c][a-z]", false, true, type
, autodetect
);
2636 // get device list from the megaraid device
2637 get_dev_megasas(devlist
);
2640 // if we found traditional links, we are done
2641 if (devlist
.size() > 0)
2644 // else look for devfs entries without traditional links
2645 // TODO: Add udev support
2646 return get_dev_list(devlist
, "/dev/discs/disc*", scan_ata
, scan_scsi
, type
, false);
2649 ata_device
* linux_smart_interface::get_ata_device(const char * name
, const char * type
)
2651 return new linux_ata_device(this, name
, type
);
2654 scsi_device
* linux_smart_interface::get_scsi_device(const char * name
, const char * type
)
2656 return new linux_scsi_device(this, name
, type
);
2659 smart_device
* linux_smart_interface::missing_option(const char * opt
)
2661 set_err(EINVAL
, "requires option '%s'", opt
);
2666 linux_smart_interface::megasas_dcmd_cmd(int bus_no
, uint32_t opcode
, void *buf
,
2667 size_t bufsize
, uint8_t *mbox
, size_t mboxlen
, uint8_t *statusp
)
2669 struct megasas_iocpacket ioc
;
2671 if ((mbox
!= NULL
&& (mboxlen
== 0 || mboxlen
> MFI_MBOX_SIZE
)) ||
2672 (mbox
== NULL
&& mboxlen
!= 0))
2678 bzero(&ioc
, sizeof(ioc
));
2679 struct megasas_dcmd_frame
* dcmd
= &ioc
.frame
.dcmd
;
2680 ioc
.host_no
= bus_no
;
2682 bcopy(mbox
, dcmd
->mbox
.w
, mboxlen
);
2683 dcmd
->cmd
= MFI_CMD_DCMD
;
2686 dcmd
->data_xfer_len
= bufsize
;
2687 dcmd
->opcode
= opcode
;
2690 dcmd
->sge_count
= 1;
2691 dcmd
->data_xfer_len
= bufsize
;
2692 dcmd
->sgl
.sge32
[0].phys_addr
= (intptr_t)buf
;
2693 dcmd
->sgl
.sge32
[0].length
= (uint32_t)bufsize
;
2695 ioc
.sgl_off
= offsetof(struct megasas_dcmd_frame
, sgl
);
2696 ioc
.sgl
[0].iov_base
= buf
;
2697 ioc
.sgl
[0].iov_len
= bufsize
;
2701 if ((fd
= ::open("/dev/megaraid_sas_ioctl_node", O_RDWR
)) <= 0) {
2705 int r
= ioctl(fd
, MEGASAS_IOC_FIRMWARE
, &ioc
);
2710 if (statusp
!= NULL
)
2711 *statusp
= dcmd
->cmd_status
;
2712 else if (dcmd
->cmd_status
!= MFI_STAT_OK
) {
2713 fprintf(stderr
, "command %x returned error status %x\n",
2714 opcode
, dcmd
->cmd_status
);
2722 linux_smart_interface::megasas_pd_add_list(int bus_no
, smart_device_list
& devlist
)
2725 * Keep fetching the list in a loop until we have a large enough
2726 * buffer to hold the entire list.
2728 megasas_pd_list
* list
= 0;
2729 for (unsigned list_size
= 1024; ; ) {
2730 list
= (megasas_pd_list
*)realloc(list
, list_size
);
2732 throw std::bad_alloc();
2733 bzero(list
, list_size
);
2734 if (megasas_dcmd_cmd(bus_no
, MFI_DCMD_PD_GET_LIST
, list
, list_size
, NULL
, 0,
2740 if (list
->size
<= list_size
)
2742 list_size
= list
->size
;
2745 // adding all SCSI devices
2746 for (unsigned i
= 0; i
< list
->count
; i
++) {
2747 if(list
->addr
[i
].scsi_dev_type
)
2748 continue; /* non disk device found */
2750 snprintf(line
, sizeof(line
) - 1, "/dev/bus/%d", bus_no
);
2751 smart_device
* dev
= new linux_megaraid_device(this, line
, 0, list
->addr
[i
].device_id
);
2752 devlist
.push_back(dev
);
2758 // Return kernel release as integer ("2.6.31" -> 206031)
2759 static unsigned get_kernel_release()
2764 unsigned x
= 0, y
= 0, z
= 0;
2765 if (!(sscanf(u
.release
, "%u.%u.%u", &x
, &y
, &z
) == 3
2766 && x
< 100 && y
< 100 && z
< 1000 ))
2768 return x
* 100000 + y
* 1000 + z
;
2771 // Guess device type (ata or scsi) based on device name (Linux
2772 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
2773 // osst, nosst and sg.
2774 smart_device
* linux_smart_interface::autodetect_smart_device(const char * name
)
2776 const char * test_name
= name
;
2778 // Dereference symlinks
2780 std::string pathbuf
;
2781 if (!lstat(name
, &st
) && S_ISLNK(st
.st_mode
)) {
2782 char * p
= realpath(name
, (char *)0);
2786 test_name
= pathbuf
.c_str();
2790 // Remove the leading /dev/... if it's there
2791 static const char dev_prefix
[] = "/dev/";
2792 if (str_starts_with(test_name
, dev_prefix
))
2793 test_name
+= strlen(dev_prefix
);
2795 // form /dev/h* or h*
2796 if (str_starts_with(test_name
, "h"))
2797 return new linux_ata_device(this, name
, "");
2799 // form /dev/ide/* or ide/*
2800 if (str_starts_with(test_name
, "ide/"))
2801 return new linux_ata_device(this, name
, "");
2803 // form /dev/s* or s*
2804 if (str_starts_with(test_name
, "s")) {
2806 // Try to detect possible USB->(S)ATA bridge
2807 unsigned short vendor_id
= 0, product_id
= 0, version
= 0;
2808 if (get_usb_id(test_name
, vendor_id
, product_id
, version
)) {
2809 const char * usbtype
= get_usb_dev_type_by_id(vendor_id
, product_id
, version
);
2813 // Kernels before 2.6.29 do not support the sense data length
2814 // required for SAT ATA PASS-THROUGH(16)
2815 if (!strcmp(usbtype
, "sat") && get_kernel_release() < 206029)
2818 // Return SAT/USB device for this type
2819 // (Note: linux_scsi_device::autodetect_open() will not be called in this case)
2820 return get_sat_device(usbtype
, new linux_scsi_device(this, name
, ""));
2823 // No USB bridge found, assume regular SCSI device
2824 return new linux_scsi_device(this, name
, "");
2827 // form /dev/scsi/* or scsi/*
2828 if (str_starts_with(test_name
, "scsi/"))
2829 return new linux_scsi_device(this, name
, "");
2831 // form /dev/ns* or ns*
2832 if (str_starts_with(test_name
, "ns"))
2833 return new linux_scsi_device(this, name
, "");
2835 // form /dev/os* or os*
2836 if (str_starts_with(test_name
, "os"))
2837 return new linux_scsi_device(this, name
, "");
2839 // form /dev/nos* or nos*
2840 if (str_starts_with(test_name
, "nos"))
2841 return new linux_scsi_device(this, name
, "");
2843 // form /dev/tw[ael]* or tw[ael]*
2844 if (str_starts_with(test_name
, "tw") && strchr("ael", test_name
[2]))
2845 return missing_option("-d 3ware,N");
2847 // form /dev/cciss/* or cciss/*
2848 if (str_starts_with(test_name
, "cciss/"))
2849 return missing_option("-d cciss,N");
2851 // we failed to recognize any of the forms
2855 smart_device
* linux_smart_interface::get_custom_smart_device(const char * name
, const char * type
)
2858 if (!strcmp(type
, "marvell"))
2859 return new linux_marvell_device(this, name
, type
);
2862 int disknum
= -1, n1
= -1, n2
= -1;
2863 if (sscanf(type
, "3ware,%n%d%n", &n1
, &disknum
, &n2
) == 1 || n1
== 6) {
2864 if (n2
!= (int)strlen(type
)) {
2865 set_err(EINVAL
, "Option -d 3ware,N requires N to be a non-negative integer");
2868 if (!(0 <= disknum
&& disknum
<= 127)) {
2869 set_err(EINVAL
, "Option -d 3ware,N (N=%d) must have 0 <= N <= 127", disknum
);
2873 if (!strncmp(name
, "/dev/twl", 8))
2874 return new linux_escalade_device(this, name
, linux_escalade_device::AMCC_3WARE_9700_CHAR
, disknum
);
2875 else if (!strncmp(name
, "/dev/twa", 8))
2876 return new linux_escalade_device(this, name
, linux_escalade_device::AMCC_3WARE_9000_CHAR
, disknum
);
2877 else if (!strncmp(name
, "/dev/twe", 8))
2878 return new linux_escalade_device(this, name
, linux_escalade_device::AMCC_3WARE_678K_CHAR
, disknum
);
2880 return new linux_escalade_device(this, name
, linux_escalade_device::AMCC_3WARE_678K
, disknum
);
2884 disknum
= n1
= n2
= -1;
2886 if (sscanf(type
, "areca,%n%d/%d%n", &n1
, &disknum
, &encnum
, &n2
) >= 1 || n1
== 6) {
2887 if (!(1 <= disknum
&& disknum
<= 128)) {
2888 set_err(EINVAL
, "Option -d areca,N/E (N=%d) must have 1 <= N <= 128", disknum
);
2891 if (!(1 <= encnum
&& encnum
<= 8)) {
2892 set_err(EINVAL
, "Option -d areca,N/E (E=%d) must have 1 <= E <= 8", encnum
);
2895 return new linux_areca_ata_device(this, name
, disknum
, encnum
);
2899 int controller
= -1, channel
= -1; disknum
= 1;
2900 n1
= n2
= -1; int n3
= -1;
2901 if (sscanf(type
, "hpt,%n%d/%d%n/%d%n", &n1
, &controller
, &channel
, &n2
, &disknum
, &n3
) >= 2 || n1
== 4) {
2902 int len
= strlen(type
);
2903 if (!(n2
== len
|| n3
== len
)) {
2904 set_err(EINVAL
, "Option '-d hpt,L/M/N' supports 2-3 items");
2907 if (!(1 <= controller
&& controller
<= 8)) {
2908 set_err(EINVAL
, "Option '-d hpt,L/M/N' invalid controller id L supplied");
2911 if (!(1 <= channel
&& channel
<= 128)) {
2912 set_err(EINVAL
, "Option '-d hpt,L/M/N' invalid channel number M supplied");
2915 if (!(1 <= disknum
&& disknum
<= 15)) {
2916 set_err(EINVAL
, "Option '-d hpt,L/M/N' invalid pmport number N supplied");
2919 return new linux_highpoint_device(this, name
, controller
, channel
, disknum
);
2922 #ifdef HAVE_LINUX_CCISS_IOCTL_H
2924 disknum
= n1
= n2
= -1;
2925 if (sscanf(type
, "cciss,%n%d%n", &n1
, &disknum
, &n2
) == 1 || n1
== 6) {
2926 if (n2
!= (int)strlen(type
)) {
2927 set_err(EINVAL
, "Option -d cciss,N requires N to be a non-negative integer");
2930 if (!(0 <= disknum
&& disknum
<= 127)) {
2931 set_err(EINVAL
, "Option -d cciss,N (N=%d) must have 0 <= N <= 127", disknum
);
2934 return get_sat_device("sat,auto", new linux_cciss_device(this, name
, disknum
));
2936 #endif // HAVE_LINUX_CCISS_IOCTL_H
2939 if (sscanf(type
, "megaraid,%d", &disknum
) == 1) {
2940 return new linux_megaraid_device(this, name
, 0, disknum
);
2945 std::string
linux_smart_interface::get_valid_custom_dev_types_str()
2947 return "marvell, areca,N/E, 3ware,N, hpt,L/M/N, megaraid,N"
2948 #ifdef HAVE_LINUX_CCISS_IOCTL_H
2956 /////////////////////////////////////////////////////////////////////////////
2957 /// Initialize platform interface and register with smi()
2959 void smart_interface::init()
2961 static os_linux::linux_smart_interface the_interface
;
2962 smart_interface::set(&the_interface
);