4 * Home page of code is: http://www.smartmontools.org
6 * Copyright (C) 2003-11 Bruce Allen
7 * Copyright (C) 2003-11 Doug Gilbert <dgilbert@interlog.com>
8 * Copyright (C) 2008-18 Christian Franke
10 * Original AACRaid code:
11 * Copyright (C) 2014 Raghava Aditya <raghava.aditya@pmcs.com>
13 * Original Areca code:
14 * Copyright (C) 2008-12 Hank Wu <hank@areca.com.tw>
15 * Copyright (C) 2008 Oliver Bock <brevilo@users.sourceforge.net>
17 * Original MegaRAID code:
18 * Copyright (C) 2008 Jordan Hargrave <jordan_hargrave@dell.com>
20 * 3ware code was derived from code that was:
22 * Written By: Adam Radford <linux@3ware.com>
23 * Modifications By: Joel Jacobson <linux@3ware.com>
24 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
25 * Brad Strand <linux@3ware.com>
27 * Copyright (C) 1999-2003 3ware Inc.
29 * Kernel compatibility By: Andre Hedrick <andre@suse.com>
30 * Non-Copyright (C) 2000 Andre Hedrick <andre@suse.com>
32 * Other ars of this file are derived from code that was
34 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
35 * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org>
37 * SPDX-License-Identifier: GPL-2.0-or-later
40 // This file contains the linux-specific IOCTL parts of
41 // smartmontools. It includes one interface routine for ATA devices,
42 // one for SCSI devices, and one for ATA devices behind escalade
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_ioctl.h>
54 #include <linux/bsg.h>
57 #include <sys/ioctl.h>
59 #include <sys/utsname.h>
61 #include <stddef.h> // for offsetof()
63 #include <sys/types.h>
65 #ifdef HAVE_SYS_SYSMACROS_H
66 // glibc 2.25: The inclusion of <sys/sysmacros.h> by <sys/types.h> is
67 // deprecated. A warning is printed if major(), minor() or makedev()
68 // is used but <sys/sysmacros.h> is not included.
69 #include <sys/sysmacros.h>
71 #ifdef HAVE_LIBSELINUX
72 #include <selinux/selinux.h>
84 #include "dev_interface.h"
85 #include "dev_ata_cmd_set.h"
86 #include "dev_areca.h"
88 // "include/uapi/linux/nvme_ioctl.h" from Linux kernel sources
89 #include "linux_nvme_ioctl.h" // nvme_passthru_cmd, NVME_IOCTL_ADMIN_CMD
92 #define ENOTSUP ENOSYS
95 #define ARGUSED(x) ((void)(x))
97 const char * os_linux_cpp_cvsid
= "$Id: os_linux.cpp 4854 2018-12-11 20:32:29Z chrfranke $"
99 extern unsigned char failuretest_permissive
;
101 namespace os_linux
{ // No need to publish anything, name provided for Doxygen
103 /////////////////////////////////////////////////////////////////////////////
104 /// Shared open/close routines
106 class linux_smart_device
107 : virtual public /*implements*/ smart_device
110 explicit linux_smart_device(int flags
, int retry_flags
= -1)
111 : smart_device(never_called
),
113 m_flags(flags
), m_retry_flags(retry_flags
)
116 virtual ~linux_smart_device() throw();
118 virtual bool is_open() const;
122 virtual bool close();
125 /// Return filedesc for derived classes.
133 int m_fd
; ///< filedesc, -1 if not open.
134 int m_flags
; ///< Flags for ::open()
135 int m_retry_flags
; ///< Flags to retry ::open(), -1 if no retry
138 linux_smart_device::~linux_smart_device() throw()
144 bool linux_smart_device::is_open() const
149 bool linux_smart_device::open()
151 m_fd
= ::open(get_dev_name(), m_flags
);
153 if (m_fd
< 0 && errno
== EROFS
&& m_retry_flags
!= -1)
155 m_fd
= ::open(get_dev_name(), m_retry_flags
);
158 if (errno
== EBUSY
&& (m_flags
& O_EXCL
))
160 return set_err(EBUSY
,
161 "The requested controller is used exclusively by another process!\n"
162 "(e.g. smartctl or smartd)\n"
163 "Please quit the impeding process or try again later...");
164 return set_err((errno
==ENOENT
|| errno
==ENOTDIR
) ? ENODEV
: errno
);
168 // sets FD_CLOEXEC on the opened device file descriptor. The
169 // descriptor is otherwise leaked to other applications (mail
170 // sender) which may be considered a security risk and may result
171 // in AVC messages on SELinux-enabled systems.
172 if (-1 == fcntl(m_fd
, F_SETFD
, FD_CLOEXEC
))
173 // TODO: Provide an error printing routine in class smart_interface
174 pout("fcntl(set FD_CLOEXEC) failed, errno=%d [%s]\n", errno
, strerror(errno
));
180 // equivalent to close(file descriptor)
181 bool linux_smart_device::close()
183 int fd
= m_fd
; m_fd
= -1;
185 return set_err(errno
);
189 // examples for smartctl
190 static const char smartctl_examples
[] =
191 "=================================================== SMARTCTL EXAMPLES =====\n\n"
192 " smartctl --all /dev/sda (Prints all SMART information)\n\n"
193 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/sda\n"
194 " (Enables SMART on first disk)\n\n"
195 " smartctl --test=long /dev/sda (Executes extended disk self-test)\n\n"
196 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/sda\n"
197 " (Prints Self-Test & Attribute errors)\n"
198 " smartctl --all --device=3ware,2 /dev/sda\n"
199 " smartctl --all --device=3ware,2 /dev/twe0\n"
200 " smartctl --all --device=3ware,2 /dev/twa0\n"
201 " smartctl --all --device=3ware,2 /dev/twl0\n"
202 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
203 " smartctl --all --device=hpt,1/1/3 /dev/sda\n"
204 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
205 " of the 1st channel on the 1st HighPoint RAID controller)\n"
206 " smartctl --all --device=areca,3/1 /dev/sg2\n"
207 " (Prints all SMART info for 3rd ATA disk of the 1st enclosure\n"
208 " on Areca RAID controller)\n"
211 /////////////////////////////////////////////////////////////////////////////
212 /// Linux ATA support
214 class linux_ata_device
215 : public /*implements*/ ata_device_with_command_set
,
216 public /*extends*/ linux_smart_device
219 linux_ata_device(smart_interface
* intf
, const char * dev_name
, const char * req_type
);
222 virtual int ata_command_interface(smart_command_set command
, int select
, char * data
);
225 linux_ata_device::linux_ata_device(smart_interface
* intf
, const char * dev_name
, const char * req_type
)
226 : smart_device(intf
, dev_name
, "ata", req_type
),
227 linux_smart_device(O_RDONLY
| O_NONBLOCK
)
232 // This is an interface routine meant to isolate the OS dependent
233 // parts of the code, and to provide a debugging interface. Each
234 // different port and OS needs to provide it's own interface. This
236 // DETAILED DESCRIPTION OF ARGUMENTS
237 // device: is the file descriptor provided by open()
238 // command: defines the different operations.
239 // select: additional input data if needed (which log, which type of
241 // data: location to write output data, if needed (512 bytes).
242 // Note: not all commands use all arguments.
244 // -1 if the command failed
245 // 0 if the command succeeded,
246 // STATUS_CHECK routine:
247 // -1 if the command failed
248 // 0 if the command succeeded and disk SMART status is "OK"
249 // 1 if the command succeeded and disk SMART status is "FAILING"
251 #define BUFFER_LENGTH (4+512)
253 int linux_ata_device::ata_command_interface(smart_command_set command
, int select
, char * data
)
255 unsigned char buff
[BUFFER_LENGTH
];
256 // positive: bytes to write to caller. negative: bytes to READ from
257 // caller. zero: non-data command
260 const int HDIO_DRIVE_CMD_OFFSET
= 4;
262 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
263 // buff[0]: ATA COMMAND CODE REGISTER
264 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
265 // buff[2]: ATA FEATURES REGISTER
266 // buff[3]: ATA SECTOR COUNT REGISTER
268 // Note that on return:
269 // buff[2] contains the ATA SECTOR COUNT REGISTER
271 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
272 memset(buff
, 0, BUFFER_LENGTH
);
274 buff
[0]=ATA_SMART_CMD
;
276 case CHECK_POWER_MODE
:
277 buff
[0]=ATA_CHECK_POWER_MODE
;
281 buff
[2]=ATA_SMART_READ_VALUES
;
285 case READ_THRESHOLDS
:
286 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
291 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
299 buff
[0]=ATA_IDENTIFY_DEVICE
;
304 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
309 buff
[2]=ATA_SMART_ENABLE
;
313 buff
[2]=ATA_SMART_DISABLE
;
317 // this command only says if SMART is working. It could be
318 // replaced with STATUS_CHECK below.
319 buff
[2]=ATA_SMART_STATUS
;
322 // NOTE: According to ATAPI 4 and UP, this command is obsolete
323 // select == 241 for enable but no data transfer. Use TASK ioctl.
324 buff
[1]=ATA_SMART_AUTO_OFFLINE
;
328 // select == 248 for enable but no data transfer. Use TASK ioctl.
329 buff
[1]=ATA_SMART_AUTOSAVE
;
332 case IMMEDIATE_OFFLINE
:
333 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
337 // This command uses HDIO_DRIVE_TASK and has different syntax than
338 // the other commands.
339 buff
[1]=ATA_SMART_STATUS
;
342 pout("Unrecognized command %d in linux_ata_command_interface()\n"
343 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
348 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
349 // only ioctl() that can be used to WRITE data to the disk.
350 if (command
==WRITE_LOG
) {
351 unsigned char task
[sizeof(ide_task_request_t
)+512];
352 ide_task_request_t
*reqtask
=(ide_task_request_t
*) task
;
353 task_struct_t
*taskfile
=(task_struct_t
*) reqtask
->io_ports
;
355 memset(task
, 0, sizeof(task
));
358 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
359 taskfile
->sector_count
= 1;
360 taskfile
->sector_number
= select
;
361 taskfile
->low_cylinder
= 0x4f;
362 taskfile
->high_cylinder
= 0xc2;
363 taskfile
->device_head
= 0;
364 taskfile
->command
= ATA_SMART_CMD
;
366 reqtask
->data_phase
= TASKFILE_OUT
;
367 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
368 reqtask
->out_size
= 512;
369 reqtask
->in_size
= 0;
371 // copy user data into the task request structure
372 memcpy(task
+sizeof(ide_task_request_t
), data
, 512);
374 if (ioctl(get_fd(), HDIO_DRIVE_TASKFILE
, task
)) {
376 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASK_IOCTL set\n");
382 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
384 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 (ioctl(get_fd(), HDIO_DRIVE_TASK
, buff
)) {
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_USE_DETECT 0
506 #define SG_IO_UNSUPP 1
507 #define SG_IO_USE_V3 3
508 #define SG_IO_USE_V4 4
510 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
512 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
);
514 static int sg_io_state
= SG_IO_USE_DETECT
;
516 /* Preferred implementation for issuing SCSI commands in linux. This
517 * function uses the SG_IO ioctl. Return 0 if command issued successfully
518 * (various status values should still be checked). If the SCSI command
519 * cannot be issued then a negative errno value is returned. */
520 static int sg_io_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
,
524 ARGUSED(dev_fd
); ARGUSED(iop
); ARGUSED(report
);
528 /* we are filling structures for both versions, but using only one requested */
529 struct sg_io_hdr io_hdr_v3
;
530 struct sg_io_v4 io_hdr_v4
;
532 #ifdef SCSI_CDB_CHECK
533 bool ok
= is_scsi_cdb(iop
->cmnd
, iop
->cmnd_len
);
535 int n
= iop
->cmnd_len
;
536 const unsigned char * ucp
= iop
->cmnd
;
538 pout(">>>>>>>> %s: cdb seems invalid, opcode=0x%x, len=%d, cdb:\n",
539 __func__
, ((n
> 0) ? ucp
[0] : 0), n
);
542 pout(" <<truncating to first 16 bytes>>\n");
543 dStrHex((const uint8_t *)ucp
, ((n
> 16) ? 16 : n
), 1);
550 const unsigned char * ucp
= iop
->cmnd
;
553 const int sz
= (int)sizeof(buff
);
555 pout(">>>> do_scsi_cmnd_io: sg_io_ver=%d\n", sg_io_ver
);
556 np
= scsi_get_opcode_name(ucp
[0]);
557 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
558 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
559 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
561 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
562 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
564 snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
565 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
566 (trunc
? " [only first 256 bytes shown]" : ""));
567 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
570 snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
573 memset(&io_hdr_v3
, 0, sizeof(struct sg_io_hdr
));
574 memset(&io_hdr_v4
, 0, sizeof(struct sg_io_v4
));
576 io_hdr_v3
.interface_id
= 'S';
577 io_hdr_v3
.cmd_len
= iop
->cmnd_len
;
578 io_hdr_v3
.mx_sb_len
= iop
->max_sense_len
;
579 io_hdr_v3
.dxfer_len
= iop
->dxfer_len
;
580 io_hdr_v3
.dxferp
= iop
->dxferp
;
581 io_hdr_v3
.cmdp
= iop
->cmnd
;
582 io_hdr_v3
.sbp
= iop
->sensep
;
583 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
584 defaults to 60 seconds. */
585 io_hdr_v3
.timeout
= ((0 == iop
->timeout
) ? 60 : iop
->timeout
) * 1000;
587 io_hdr_v4
.guard
= 'Q';
588 io_hdr_v4
.request_len
= iop
->cmnd_len
;
589 io_hdr_v4
.request
= __u64(iop
->cmnd
);
590 io_hdr_v4
.max_response_len
= iop
->max_sense_len
;
591 io_hdr_v4
.response
= __u64(iop
->sensep
);
592 io_hdr_v4
.timeout
= ((0 == iop
->timeout
) ? 60 : iop
->timeout
) * 1000; // msec
594 switch (iop
->dxfer_dir
) {
596 io_hdr_v3
.dxfer_direction
= SG_DXFER_NONE
;
598 case DXFER_FROM_DEVICE
:
599 io_hdr_v3
.dxfer_direction
= SG_DXFER_FROM_DEV
;
600 io_hdr_v4
.din_xfer_len
= iop
->dxfer_len
;
601 io_hdr_v4
.din_xferp
= __u64(iop
->dxferp
);
603 case DXFER_TO_DEVICE
:
604 io_hdr_v3
.dxfer_direction
= SG_DXFER_TO_DEV
;
605 io_hdr_v4
.dout_xfer_len
= iop
->dxfer_len
;
606 io_hdr_v4
.dout_xferp
= __u64(iop
->dxferp
);
609 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
613 iop
->resp_sense_len
= 0;
614 iop
->scsi_status
= 0;
617 void * io_hdr
= NULL
;
627 // should never be reached
632 if (ioctl(dev_fd
, SG_IO
, io_hdr
) < 0) {
634 pout(" SG_IO ioctl failed, errno=%d [%s], SG_IO_V%d\n", errno
,
635 strerror(errno
), sg_io_ver
);
639 unsigned int sg_driver_status
= 0, sg_transport_status
= 0, sg_info
= 0,
642 if (sg_io_ver
== SG_IO_USE_V3
) {
643 iop
->resid
= io_hdr_v3
.resid
;
644 iop
->scsi_status
= io_hdr_v3
.status
;
645 sg_driver_status
= io_hdr_v3
.driver_status
;
646 sg_transport_status
= io_hdr_v3
.host_status
;
647 sg_info
= io_hdr_v3
.info
;
648 iop
->resp_sense_len
= io_hdr_v3
.sb_len_wr
;
649 sg_duration
= io_hdr_v3
.duration
;
652 if (sg_io_ver
== SG_IO_USE_V4
) {
653 switch (iop
->dxfer_dir
) {
657 case DXFER_FROM_DEVICE
:
658 iop
->resid
= io_hdr_v4
.din_resid
;
660 case DXFER_TO_DEVICE
:
661 iop
->resid
= io_hdr_v4
.dout_resid
;
664 iop
->scsi_status
= io_hdr_v4
.device_status
;
665 sg_driver_status
= io_hdr_v4
.driver_status
;
666 sg_transport_status
= io_hdr_v4
.transport_status
;
667 sg_info
= io_hdr_v4
.info
;
668 iop
->resp_sense_len
= io_hdr_v4
.response_len
;
669 sg_duration
= io_hdr_v4
.duration
;
673 pout(" scsi_status=0x%x, sg_transport_status=0x%x, sg_driver_status=0x%x\n"
674 " sg_info=0x%x sg_duration=%d milliseconds resid=%d\n", iop
->scsi_status
,
675 sg_transport_status
, sg_driver_status
, sg_info
,
676 sg_duration
, iop
->resid
);
679 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
682 len
= iop
->dxfer_len
- iop
->resid
;
683 trunc
= (len
> 256) ? 1 : 0;
685 pout(" Incoming data, len=%d%s:\n", len
,
686 (trunc
? " [only first 256 bytes shown]" : ""));
687 dStrHex(iop
->dxferp
, (trunc
? 256 : len
), 1);
689 pout(" Incoming data trimmed to nothing by resid\n");
694 if (sg_info
& SG_INFO_CHECK
) { /* error or warning */
695 int masked_driver_status
= (LSCSI_DRIVER_MASK
& sg_driver_status
);
697 if (0 != sg_transport_status
) {
698 if ((LSCSI_DID_NO_CONNECT
== sg_transport_status
) ||
699 (LSCSI_DID_BUS_BUSY
== sg_transport_status
) ||
700 (LSCSI_DID_TIME_OUT
== sg_transport_status
))
703 /* Check for DID_ERROR - workaround for aacraid driver quirk */
704 if (LSCSI_DID_ERROR
!= sg_transport_status
) {
705 return -EIO
; /* catch all if not DID_ERR */
708 if (0 != masked_driver_status
) {
709 if (LSCSI_DRIVER_TIMEOUT
== masked_driver_status
)
711 else if (LSCSI_DRIVER_SENSE
!= masked_driver_status
)
714 if (LSCSI_DRIVER_SENSE
== masked_driver_status
)
715 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
716 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
717 iop
->sensep
&& (iop
->resp_sense_len
> 0)) {
719 pout(" >>> Sense buffer, len=%d:\n",
720 (int)iop
->resp_sense_len
);
721 dStrHex(iop
->sensep
, iop
->resp_sense_len
, 1);
725 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
&& iop
->sensep
) {
726 if ((iop
->sensep
[0] & 0x7f) > 0x71)
727 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
728 iop
->scsi_status
, iop
->sensep
[1] & 0xf,
729 iop
->sensep
[2], iop
->sensep
[3]);
731 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
732 iop
->scsi_status
, iop
->sensep
[2] & 0xf,
733 iop
->sensep
[12], iop
->sensep
[13]);
736 pout(" status=0x%x\n", iop
->scsi_status
);
743 struct linux_ioctl_send_command
747 uint8_t buff
[MAX_DXFER_LEN
+ 16];
750 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
751 * support: CDB length (guesses it from opcode), resid and timeout.
752 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
753 * to 2 hours in order to allow long foreground extended self tests. */
754 static int sisc_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
)
756 struct linux_ioctl_send_command wrk
;
757 int status
, buff_offset
;
760 memcpy(wrk
.buff
, iop
->cmnd
, iop
->cmnd_len
);
761 buff_offset
= iop
->cmnd_len
;
764 const unsigned char * ucp
= iop
->cmnd
;
767 const int sz
= (int)sizeof(buff
);
769 np
= scsi_get_opcode_name(ucp
[0]);
770 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
771 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
772 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
773 if ((report
> 1) && (DXFER_TO_DEVICE
== iop
->dxfer_dir
)) {
774 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
776 snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
777 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
778 (trunc
? " [only first 256 bytes shown]" : ""));
779 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
782 snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
785 switch (iop
->dxfer_dir
) {
790 case DXFER_FROM_DEVICE
:
792 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
794 wrk
.outbufsize
= iop
->dxfer_len
;
796 case DXFER_TO_DEVICE
:
797 if (iop
->dxfer_len
> MAX_DXFER_LEN
)
799 memcpy(wrk
.buff
+ buff_offset
, iop
->dxferp
, iop
->dxfer_len
);
800 wrk
.inbufsize
= iop
->dxfer_len
;
804 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
807 iop
->resp_sense_len
= 0;
808 iop
->scsi_status
= 0;
810 status
= ioctl(dev_fd
, SCSI_IOCTL_SEND_COMMAND
, &wrk
);
813 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
814 errno
, strerror(errno
));
820 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
) {
821 memcpy(iop
->dxferp
, wrk
.buff
, iop
->dxfer_len
);
823 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
825 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
826 (trunc
? " [only first 256 bytes shown]" : ""));
827 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
832 iop
->scsi_status
= status
& 0x7e; /* bits 0 and 7 used to be for vendors */
833 if (LSCSI_DRIVER_SENSE
== ((status
>> 24) & 0xf))
834 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
835 len
= (SEND_IOCTL_RESP_SENSE_LEN
< iop
->max_sense_len
) ?
836 SEND_IOCTL_RESP_SENSE_LEN
: iop
->max_sense_len
;
837 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
838 iop
->sensep
&& (len
> 0)) {
839 memcpy(iop
->sensep
, wrk
.buff
, len
);
840 iop
->resp_sense_len
= len
;
842 pout(" >>> Sense buffer, len=%d:\n", (int)len
);
843 dStrHex(wrk
.buff
, len
, 1);
847 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
848 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status
& 0xff,
849 wrk
.buff
[2] & 0xf, wrk
.buff
[12], wrk
.buff
[13]);
852 pout(" status=0x%x\n", status
);
854 if (iop
->scsi_status
> 0)
858 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
860 return -EIO
; /* give up, assume no device there */
864 /* SCSI command transmission interface function, linux version.
865 * Returns 0 if SCSI command successfully launched and response
866 * received. Even when 0 is returned the caller should check
867 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
868 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
869 * (e.g. device not present or timeout) or some other problem
870 * (e.g. timeout) then returns a negative errno value */
871 static int do_normal_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
,
876 /* implementation relies on static sg_io_state variable. If not
877 * previously set tries the SG_IO ioctl. If that succeeds assume
878 * that SG_IO ioctl functional. If it fails with an errno value
879 * other than ENODEV (no device) or permission then assume
880 * SCSI_IOCTL_SEND_COMMAND is the only option. */
881 switch (sg_io_state
) {
882 case SG_IO_USE_DETECT
:
883 /* ignore report argument */
884 /* Try SG_IO V3 first */
885 if (0 == (res
= sg_io_cmnd_io(dev_fd
, iop
, report
, SG_IO_USE_V3
))) {
886 sg_io_state
= SG_IO_USE_V3
;
888 } else if ((-ENODEV
== res
) || (-EACCES
== res
) || (-EPERM
== res
))
889 return res
; /* wait until we see a device */
890 /* See if we can use SG_IO V4 * */
891 if (0 == (res
= sg_io_cmnd_io(dev_fd
, iop
, report
, SG_IO_USE_V4
))) {
892 sg_io_state
= SG_IO_USE_V4
;
894 } else if ((-ENODEV
== res
) || (-EACCES
== res
) || (-EPERM
== res
))
895 return res
; /* wait until we see a device */
896 /* fallback to the SCSI_IOCTL_SEND_COMMAND */
897 sg_io_state
= SG_IO_UNSUPP
;
900 /* deprecated SCSI_IOCTL_SEND_COMMAND ioctl */
901 return sisc_cmnd_io(dev_fd
, iop
, report
);
904 /* use SG_IO V3 or V4 ioctl, depending on availabiliy */
905 return sg_io_cmnd_io(dev_fd
, iop
, report
, sg_io_state
);
907 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state
);
908 sg_io_state
= SG_IO_USE_DETECT
;
909 return -EIO
; /* report error and reset state */
913 // >>>>>> End of general SCSI specific linux code
915 /////////////////////////////////////////////////////////////////////////////
916 /// Standard SCSI support
918 class linux_scsi_device
919 : public /*implements*/ scsi_device
,
920 public /*extends*/ linux_smart_device
923 linux_scsi_device(smart_interface
* intf
, const char * dev_name
,
924 const char * req_type
, bool scanning
= false);
926 virtual smart_device
* autodetect_open();
928 virtual bool scsi_pass_through(scsi_cmnd_io
* iop
);
931 bool m_scanning
; ///< true if created within scan_smart_devices
934 linux_scsi_device::linux_scsi_device(smart_interface
* intf
,
935 const char * dev_name
, const char * req_type
, bool scanning
/*= false*/)
936 : smart_device(intf
, dev_name
, "scsi", req_type
),
937 // If opened with O_RDWR, a SATA disk in standby mode
938 // may spin-up after device close().
939 linux_smart_device(O_RDONLY
| O_NONBLOCK
),
944 bool linux_scsi_device::scsi_pass_through(scsi_cmnd_io
* iop
)
946 int status
= do_normal_scsi_cmnd_io(get_fd(), iop
, scsi_debugmode
);
948 return set_err(-status
);
952 /////////////////////////////////////////////////////////////////////////////
953 /// PMC AacRAID support
955 class linux_aacraid_device
957 public /*extends */ linux_smart_device
960 linux_aacraid_device(smart_interface
*intf
, const char *dev_name
,
961 unsigned int host
, unsigned int channel
, unsigned int device
);
963 virtual ~linux_aacraid_device() throw();
967 virtual bool scsi_pass_through(scsi_cmnd_io
*iop
);
973 //Channel(Lun) of the device
981 linux_aacraid_device::linux_aacraid_device(smart_interface
*intf
,
982 const char *dev_name
, unsigned int host
, unsigned int channel
, unsigned int device
)
983 : smart_device(intf
,dev_name
,"aacraid","aacraid"),
984 linux_smart_device(O_RDWR
|O_NONBLOCK
),
985 aHost(host
), aLun(channel
), aId(device
)
987 set_info().info_name
= strprintf("%s [aacraid_disk_%02d_%02d_%d]",dev_name
,aHost
,aLun
,aId
);
988 set_info().dev_type
= strprintf("aacraid,%d,%d,%d",aHost
,aLun
,aId
);
991 linux_aacraid_device::~linux_aacraid_device() throw()
995 bool linux_aacraid_device::open()
997 //Create the character device name based on the host number
998 //Required for get stats from disks connected to different controllers
1000 snprintf(dev_name
, sizeof(dev_name
), "/dev/aac%d", aHost
);
1002 //Initial open of dev name to check if it exsists
1003 int afd
= ::open(dev_name
,O_RDWR
);
1005 if(afd
< 0 && errno
== ENOENT
) {
1007 FILE *fp
= fopen("/proc/devices","r");
1009 return set_err(errno
,"cannot open /proc/devices:%s",
1015 while(fgets(line
,sizeof(line
),fp
) !=NULL
) {
1017 if(sscanf(line
,"%d aac%n",&mjr
,&nc
) == 1
1018 && nc
> 0 && '\n' == line
[nc
])
1023 //work with /proc/devices is done
1027 return set_err(ENOENT
, "aac entry not found in /proc/devices");
1029 //Create misc device file in /dev/ used for communication with driver
1030 if(mknod(dev_name
,S_IFCHR
,makedev(mjr
,aHost
)))
1031 return set_err(errno
,"cannot create %s:%s",dev_name
,strerror(errno
));
1033 afd
= ::open(dev_name
,O_RDWR
);
1037 return set_err(errno
,"cannot open %s:%s",dev_name
,strerror(errno
));
1043 bool linux_aacraid_device::scsi_pass_through(scsi_cmnd_io
*iop
)
1045 int report
= scsi_debugmode
;
1049 const unsigned char * ucp
= iop
->cmnd
;
1052 const int sz
= (int)sizeof(buff
);
1054 np
= scsi_get_opcode_name(ucp
[0]);
1055 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
1056 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
1057 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
1059 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
1060 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
1062 snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
1063 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
1064 (trunc
? " [only first 256 bytes shown]" : ""));
1065 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
1068 snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
1074 //return test commands
1075 if (iop
->cmnd
[0] == 0x00)
1078 user_aac_reply
*pReply
;
1080 #ifdef ENVIRONMENT64
1081 // Create user 64 bit request
1082 user_aac_srb64
*pSrb
;
1083 uint8_t aBuff
[sizeof(user_aac_srb64
) + sizeof(user_aac_reply
)] = {0,};
1085 pSrb
= (user_aac_srb64
*)aBuff
;
1086 pSrb
->count
= sizeof(user_aac_srb64
) - sizeof(user_sgentry64
);
1088 #elif defined(ENVIRONMENT32)
1089 //Create user 32 bit request
1090 user_aac_srb32
*pSrb
;
1091 uint8_t aBuff
[sizeof(user_aac_srb32
) + sizeof(user_aac_reply
)] = {0,};
1093 pSrb
= (user_aac_srb32
*)aBuff
;
1094 pSrb
->count
= sizeof(user_aac_srb32
) - sizeof(user_sgentry32
);
1097 pSrb
->function
= SRB_FUNCTION_EXECUTE_SCSI
;
1098 //channel is 0 always
1104 pSrb
->retry_limit
= 0;
1105 pSrb
->cdb_size
= iop
->cmnd_len
;
1107 switch(iop
->dxfer_dir
) {
1109 pSrb
->flags
= SRB_NoDataXfer
;
1111 case DXFER_FROM_DEVICE
:
1112 pSrb
->flags
= SRB_DataIn
;
1114 case DXFER_TO_DEVICE
:
1115 pSrb
->flags
= SRB_DataOut
;
1118 pout("aacraid: bad dxfer_dir\n");
1119 return set_err(EINVAL
, "aacraid: bad dxfer_dir\n");
1122 if(iop
->dxfer_len
> 0) {
1124 #ifdef ENVIRONMENT64
1125 pSrb
->sg64
.count
= 1;
1126 pSrb
->sg64
.sg64
[0].addr64
.lo32
= ((intptr_t)iop
->dxferp
) &
1128 pSrb
->sg64
.sg64
[0].addr64
.hi32
= ((intptr_t)iop
->dxferp
) >> 32;
1130 pSrb
->sg64
.sg64
[0].length
= (uint32_t)iop
->dxfer_len
;
1131 pSrb
->count
+= pSrb
->sg64
.count
* sizeof(user_sgentry64
);
1132 #elif defined(ENVIRONMENT32)
1133 pSrb
->sg32
.count
= 1;
1134 pSrb
->sg32
.sg32
[0].addr32
= (intptr_t)iop
->dxferp
;
1136 pSrb
->sg32
.sg32
[0].length
= (uint32_t)iop
->dxfer_len
;
1137 pSrb
->count
+= pSrb
->sg32
.count
* sizeof(user_sgentry32
);
1142 pReply
= (user_aac_reply
*)(aBuff
+pSrb
->count
);
1144 memcpy(pSrb
->cdb
,iop
->cmnd
,iop
->cmnd_len
);
1148 rc
= ioctl(get_fd(),FSACTL_SEND_RAW_SRB
,pSrb
);
1151 return set_err(errno
, "aacraid send_raw_srb: %d.%d = %s",
1152 aLun
, aId
, strerror(errno
));
1154 /* see kernel aacraid.h and MSDN SCSI_REQUEST_BLOCK documentation */
1155 #define SRB_STATUS_SUCCESS 0x1
1156 #define SRB_STATUS_ERROR 0x4
1157 #define SRB_STATUS_NO_DEVICE 0x08
1158 #define SRB_STATUS_SELECTION_TIMEOUT 0x0a
1159 #define SRB_STATUS_AUTOSENSE_VALID 0x80
1161 iop
->scsi_status
= pReply
->scsi_status
;
1163 if (pReply
->srb_status
== (SRB_STATUS_AUTOSENSE_VALID
| SRB_STATUS_ERROR
)
1164 && iop
->scsi_status
== SCSI_STATUS_CHECK_CONDITION
) {
1165 memcpy(iop
->sensep
, pReply
->sense_data
, pReply
->sense_data_size
);
1166 iop
->resp_sense_len
= pReply
->sense_data_size
;
1167 return true; /* request completed with sense data */
1170 switch (pReply
->srb_status
& 0x3f) {
1172 case SRB_STATUS_SUCCESS
:
1173 return true; /* request completed successfully */
1175 case SRB_STATUS_NO_DEVICE
:
1176 return set_err(EIO
, "aacraid: Device %d %d does not exist", aLun
, aId
);
1178 case SRB_STATUS_SELECTION_TIMEOUT
:
1179 return set_err(EIO
, "aacraid: Device %d %d not responding", aLun
, aId
);
1182 return set_err(EIO
, "aacraid result: %d.%d = 0x%x",
1183 aLun
, aId
, pReply
->srb_status
);
1188 /////////////////////////////////////////////////////////////////////////////
1189 /// LSI MegaRAID support
1191 class linux_megaraid_device
1192 : public /* implements */ scsi_device
,
1193 public /* extends */ linux_smart_device
1196 linux_megaraid_device(smart_interface
*intf
, const char *name
,
1199 virtual ~linux_megaraid_device() throw();
1201 virtual smart_device
* autodetect_open();
1203 virtual bool open();
1204 virtual bool close();
1206 virtual bool scsi_pass_through(scsi_cmnd_io
*iop
);
1209 unsigned int m_disknum
;
1213 bool (linux_megaraid_device::*pt_cmd
)(int cdblen
, void *cdb
, int dataLen
, void *data
,
1214 int senseLen
, void *sense
, int report
, int direction
);
1215 bool megasas_cmd(int cdbLen
, void *cdb
, int dataLen
, void *data
,
1216 int senseLen
, void *sense
, int report
, int direction
);
1217 bool megadev_cmd(int cdbLen
, void *cdb
, int dataLen
, void *data
,
1218 int senseLen
, void *sense
, int report
, int direction
);
1221 linux_megaraid_device::linux_megaraid_device(smart_interface
*intf
,
1222 const char *dev_name
, unsigned int tgt
)
1223 : smart_device(intf
, dev_name
, "megaraid", "megaraid"),
1224 linux_smart_device(O_RDWR
| O_NONBLOCK
),
1225 m_disknum(tgt
), m_hba(0),
1228 set_info().info_name
= strprintf("%s [megaraid_disk_%02d]", dev_name
, m_disknum
);
1229 set_info().dev_type
= strprintf("megaraid,%d", tgt
);
1232 linux_megaraid_device::~linux_megaraid_device() throw()
1238 smart_device
* linux_megaraid_device::autodetect_open()
1240 int report
= scsi_debugmode
;
1246 // The code below is based on smartd.cpp:SCSIFilterKnown()
1247 if (strcmp(get_req_type(), "megaraid"))
1251 unsigned char req_buff
[64] = {0, };
1253 if (scsiStdInquiry(this, req_buff
, req_len
)) {
1255 set_err(EIO
, "INQUIRY failed");
1259 int avail_len
= req_buff
[4] + 5;
1260 int len
= (avail_len
< req_len
? avail_len
: req_len
);
1265 pout("Got MegaRAID inquiry.. %s\n", req_buff
+8);
1267 // Use INQUIRY to detect type
1270 ata_device
* newdev
= smi()->autodetect_sat_device(this, req_buff
, len
);
1271 if (newdev
) // NOTE: 'this' is now owned by '*newdev'
1275 // Nothing special found
1279 bool linux_megaraid_device::open()
1283 int report
= scsi_debugmode
;
1285 if (sscanf(get_dev_name(), "/dev/bus/%u", &m_hba
) == 0) {
1286 if (!linux_smart_device::open())
1288 /* Get device HBA */
1289 struct sg_scsi_id sgid
;
1290 if (ioctl(get_fd(), SG_GET_SCSI_ID
, &sgid
) == 0) {
1291 m_hba
= sgid
.host_no
;
1293 else if (ioctl(get_fd(), SCSI_IOCTL_GET_BUS_NUMBER
, &m_hba
) != 0) {
1295 linux_smart_device::close();
1296 return set_err(err
, "can't get bus number");
1297 } // we don't need this device anymore
1298 linux_smart_device::close();
1300 /* Perform mknod of device ioctl node */
1301 FILE * fp
= fopen("/proc/devices", "r");
1303 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1305 if (sscanf(line
, "%d megaraid_sas_ioctl%n", &mjr
, &n1
) == 1 && n1
== 22) {
1306 n1
=mknod("/dev/megaraid_sas_ioctl_node", S_IFCHR
, makedev(mjr
, 0));
1308 pout("Creating /dev/megaraid_sas_ioctl_node = %d\n", n1
>= 0 ? 0 : errno
);
1309 if (n1
>= 0 || errno
== EEXIST
)
1312 else if (sscanf(line
, "%d megadev%n", &mjr
, &n1
) == 1 && n1
== 11) {
1313 n1
=mknod("/dev/megadev0", S_IFCHR
, makedev(mjr
, 0));
1315 pout("Creating /dev/megadev0 = %d\n", n1
>= 0 ? 0 : errno
);
1316 if (n1
>= 0 || errno
== EEXIST
)
1323 /* Open Device IOCTL node */
1324 if ((m_fd
= ::open("/dev/megaraid_sas_ioctl_node", O_RDWR
)) >= 0) {
1325 pt_cmd
= &linux_megaraid_device::megasas_cmd
;
1327 else if ((m_fd
= ::open("/dev/megadev0", O_RDWR
)) >= 0) {
1328 pt_cmd
= &linux_megaraid_device::megadev_cmd
;
1332 linux_smart_device::close();
1333 return set_err(err
, "cannot open /dev/megaraid_sas_ioctl_node or /dev/megadev0");
1339 bool linux_megaraid_device::close()
1343 m_fd
= -1; m_hba
= 0; pt_cmd
= 0;
1348 bool linux_megaraid_device::scsi_pass_through(scsi_cmnd_io
*iop
)
1350 int report
= scsi_debugmode
;
1354 const unsigned char * ucp
= iop
->cmnd
;
1357 const int sz
= (int)sizeof(buff
);
1359 np
= scsi_get_opcode_name(ucp
[0]);
1360 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
1361 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
1362 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
1364 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
1365 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
1367 snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
1368 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
1369 (trunc
? " [only first 256 bytes shown]" : ""));
1370 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
1373 snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
1377 // Controller rejects Test Unit Ready
1378 if (iop
->cmnd
[0] == 0x00)
1381 if (iop
->cmnd
[0] == SAT_ATA_PASSTHROUGH_12
|| iop
->cmnd
[0] == SAT_ATA_PASSTHROUGH_16
) {
1382 // Controller does not return ATA output registers in SAT sense data
1383 if (iop
->cmnd
[2] & (1 << 5)) // chk_cond
1384 return set_err(ENOSYS
, "ATA return descriptor not supported by controller firmware");
1386 // SMART WRITE LOG SECTOR causing media errors
1387 if ((iop
->cmnd
[0] == SAT_ATA_PASSTHROUGH_16
// SAT16 WRITE LOG
1388 && iop
->cmnd
[14] == ATA_SMART_CMD
&& iop
->cmnd
[3]==0 && iop
->cmnd
[4] == ATA_SMART_WRITE_LOG_SECTOR
) ||
1389 (iop
->cmnd
[0] == SAT_ATA_PASSTHROUGH_12
// SAT12 WRITE LOG
1390 && iop
->cmnd
[9] == ATA_SMART_CMD
&& iop
->cmnd
[3] == ATA_SMART_WRITE_LOG_SECTOR
))
1392 if(!failuretest_permissive
)
1393 return set_err(ENOSYS
, "SMART WRITE LOG SECTOR may cause problems, try with -T permissive to force");
1397 return (this->*pt_cmd
)(iop
->cmnd_len
, iop
->cmnd
,
1398 iop
->dxfer_len
, iop
->dxferp
,
1399 iop
->max_sense_len
, iop
->sensep
, report
, iop
->dxfer_dir
);
1402 /* Issue passthrough scsi command to PERC5/6 controllers */
1403 bool linux_megaraid_device::megasas_cmd(int cdbLen
, void *cdb
,
1404 int dataLen
, void *data
,
1405 int /*senseLen*/, void * /*sense*/, int /*report*/, int dxfer_dir
)
1407 struct megasas_pthru_frame
*pthru
;
1408 struct megasas_iocpacket uio
;
1410 memset(&uio
, 0, sizeof(uio
));
1411 pthru
= &uio
.frame
.pthru
;
1412 pthru
->cmd
= MFI_CMD_PD_SCSI_IO
;
1413 pthru
->cmd_status
= 0xFF;
1414 pthru
->scsi_status
= 0x0;
1415 pthru
->target_id
= m_disknum
;
1417 pthru
->cdb_len
= cdbLen
;
1419 switch (dxfer_dir
) {
1421 pthru
->flags
= MFI_FRAME_DIR_NONE
;
1423 case DXFER_FROM_DEVICE
:
1424 pthru
->flags
= MFI_FRAME_DIR_READ
;
1426 case DXFER_TO_DEVICE
:
1427 pthru
->flags
= MFI_FRAME_DIR_WRITE
;
1430 pout("megasas_cmd: bad dxfer_dir\n");
1431 return set_err(EINVAL
, "megasas_cmd: bad dxfer_dir\n");
1435 pthru
->sge_count
= 1;
1436 pthru
->data_xfer_len
= dataLen
;
1437 pthru
->sgl
.sge32
[0].phys_addr
= (intptr_t)data
;
1438 pthru
->sgl
.sge32
[0].length
= (uint32_t)dataLen
;
1440 memcpy(pthru
->cdb
, cdb
, cdbLen
);
1442 uio
.host_no
= m_hba
;
1445 uio
.sgl_off
= offsetof(struct megasas_pthru_frame
, sgl
);
1446 uio
.sgl
[0].iov_base
= data
;
1447 uio
.sgl
[0].iov_len
= dataLen
;
1451 int rc
= ioctl(m_fd
, MEGASAS_IOC_FIRMWARE
, &uio
);
1452 if (pthru
->cmd_status
|| rc
!= 0) {
1453 if (pthru
->cmd_status
== 12) {
1454 return set_err(EIO
, "megasas_cmd: Device %d does not exist\n", m_disknum
);
1456 return set_err((errno
? errno
: EIO
), "megasas_cmd result: %d.%d = %d/%d",
1457 m_hba
, m_disknum
, errno
,
1463 /* Issue passthrough scsi commands to PERC2/3/4 controllers */
1464 bool linux_megaraid_device::megadev_cmd(int cdbLen
, void *cdb
,
1465 int dataLen
, void *data
,
1466 int /*senseLen*/, void * /*sense*/, int /*report*/, int /* dir */)
1468 struct uioctl_t uio
;
1471 /* Don't issue to the controller */
1475 memset(&uio
, 0, sizeof(uio
));
1476 uio
.inlen
= dataLen
;
1477 uio
.outlen
= dataLen
;
1479 memset(data
, 0, dataLen
);
1480 uio
.ui
.fcs
.opcode
= 0x80; // M_RD_IOCTL_CMD
1481 uio
.ui
.fcs
.adapno
= MKADAP(m_hba
);
1483 uio
.data
.pointer
= (uint8_t *)data
;
1485 uio
.mbox
.cmd
= MEGA_MBOXCMD_PASSTHRU
;
1486 uio
.mbox
.xferaddr
= (intptr_t)&uio
.pthru
;
1489 uio
.pthru
.timeout
= 2;
1490 uio
.pthru
.channel
= 0;
1491 uio
.pthru
.target
= m_disknum
;
1492 uio
.pthru
.cdblen
= cdbLen
;
1493 uio
.pthru
.reqsenselen
= MAX_REQ_SENSE_LEN
;
1494 uio
.pthru
.dataxferaddr
= (intptr_t)data
;
1495 uio
.pthru
.dataxferlen
= dataLen
;
1496 memcpy(uio
.pthru
.cdb
, cdb
, cdbLen
);
1498 rc
=ioctl(m_fd
, MEGAIOCCMD
, &uio
);
1499 if (uio
.pthru
.scsistatus
|| rc
!= 0) {
1500 return set_err((errno
? errno
: EIO
), "megadev_cmd result: %d.%d = %d/%d",
1501 m_hba
, m_disknum
, errno
,
1502 uio
.pthru
.scsistatus
);
1507 /////////////////////////////////////////////////////////////////////////////
1508 /// CCISS RAID support
1510 #ifdef HAVE_LINUX_CCISS_IOCTL_H
1512 class linux_cciss_device
1513 : public /*implements*/ scsi_device
,
1514 public /*extends*/ linux_smart_device
1517 linux_cciss_device(smart_interface
* intf
, const char * name
, unsigned char disknum
);
1519 virtual bool scsi_pass_through(scsi_cmnd_io
* iop
);
1522 unsigned char m_disknum
; ///< Disk number.
1525 linux_cciss_device::linux_cciss_device(smart_interface
* intf
,
1526 const char * dev_name
, unsigned char disknum
)
1527 : smart_device(intf
, dev_name
, "cciss", "cciss"),
1528 linux_smart_device(O_RDWR
| O_NONBLOCK
),
1531 set_info().info_name
= strprintf("%s [cciss_disk_%02d]", dev_name
, disknum
);
1534 bool linux_cciss_device::scsi_pass_through(scsi_cmnd_io
* iop
)
1536 int status
= cciss_io_interface(get_fd(), m_disknum
, iop
, scsi_debugmode
);
1538 return set_err(-status
);
1542 #endif // HAVE_LINUX_CCISS_IOCTL_H
1544 /////////////////////////////////////////////////////////////////////////////
1545 /// AMCC/3ware RAID support
1547 class linux_escalade_device
1548 : public /*implements*/ ata_device
,
1549 public /*extends*/ linux_smart_device
1552 enum escalade_type_t
{
1554 AMCC_3WARE_678K_CHAR
,
1555 AMCC_3WARE_9000_CHAR
,
1556 AMCC_3WARE_9700_CHAR
1559 linux_escalade_device(smart_interface
* intf
, const char * dev_name
,
1560 escalade_type_t escalade_type
, int disknum
);
1562 virtual bool open();
1564 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
);
1567 escalade_type_t m_escalade_type
; ///< Controller type
1568 int m_disknum
; ///< Disk number.
1571 linux_escalade_device::linux_escalade_device(smart_interface
* intf
, const char * dev_name
,
1572 escalade_type_t escalade_type
, int disknum
)
1573 : smart_device(intf
, dev_name
, "3ware", "3ware"),
1574 linux_smart_device(O_RDONLY
| O_NONBLOCK
),
1575 m_escalade_type(escalade_type
), m_disknum(disknum
)
1577 set_info().info_name
= strprintf("%s [3ware_disk_%02d]", dev_name
, disknum
);
1580 /* This function will setup and fix device nodes for a 3ware controller. */
1581 #define MAJOR_STRING_LENGTH 3
1582 #define DEVICE_STRING_LENGTH 32
1583 #define NODE_STRING_LENGTH 16
1584 static int setup_3ware_nodes(const char *nodename
, const char *driver_name
)
1588 char majorstring
[MAJOR_STRING_LENGTH
+1];
1589 char device_name
[DEVICE_STRING_LENGTH
+1];
1590 char nodestring
[NODE_STRING_LENGTH
];
1591 struct stat stat_buf
;
1594 #ifdef HAVE_LIBSELINUX
1595 security_context_t orig_context
= NULL
;
1596 security_context_t node_context
= NULL
;
1597 int selinux_enabled
= is_selinux_enabled();
1598 int selinux_enforced
= security_getenforce();
1601 /* First try to open up /proc/devices */
1602 if (!(file
= fopen("/proc/devices", "r"))) {
1603 pout("Error opening /proc/devices to check/create 3ware device nodes\n");
1605 return 0; // don't fail here: user might not have /proc !
1608 /* Attempt to get device major number */
1609 while (EOF
!= fscanf(file
, "%3s %32s", majorstring
, device_name
)) {
1610 majorstring
[MAJOR_STRING_LENGTH
]='\0';
1611 device_name
[DEVICE_STRING_LENGTH
]='\0';
1612 if (!strncmp(device_name
, nodename
, DEVICE_STRING_LENGTH
)) {
1613 tw_major
= atoi(majorstring
);
1619 /* See if we found a major device number */
1621 pout("No major number for /dev/%s listed in /proc/devices. Is the %s driver loaded?\n", nodename
, driver_name
);
1624 #ifdef HAVE_LIBSELINUX
1625 /* Prepare a database of contexts for files in /dev
1626 * and save the current context */
1627 if (selinux_enabled
) {
1628 if (matchpathcon_init_prefix(NULL
, "/dev") < 0)
1629 pout("Error initializing contexts database for /dev");
1630 if (getfscreatecon(&orig_context
) < 0) {
1631 pout("Error retrieving original SELinux fscreate context");
1632 if (selinux_enforced
) {
1633 matchpathcon_fini();
1639 /* Now check if nodes are correct */
1640 for (index
=0; index
<16; index
++) {
1641 snprintf(nodestring
, sizeof(nodestring
), "/dev/%s%d", nodename
, index
);
1642 #ifdef HAVE_LIBSELINUX
1643 /* Get context of the node and set it as the default */
1644 if (selinux_enabled
) {
1645 if (matchpathcon(nodestring
, S_IRUSR
| S_IWUSR
, &node_context
) < 0) {
1646 pout("Could not retrieve context for %s", nodestring
);
1647 if (selinux_enforced
) {
1652 if (setfscreatecon(node_context
) < 0) {
1653 pout ("Error setting default fscreate context");
1654 if (selinux_enforced
) {
1661 /* Try to stat the node */
1662 if ((stat(nodestring
, &stat_buf
))) {
1663 pout("Node %s does not exist and must be created. Check the udev rules.\n", nodestring
);
1664 /* Create a new node if it doesn't exist */
1665 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
1666 pout("problem creating 3ware device nodes %s", nodestring
);
1671 #ifdef HAVE_LIBSELINUX
1672 if (selinux_enabled
&& node_context
) {
1673 freecon(node_context
);
1674 node_context
= NULL
;
1681 /* See if nodes major and minor numbers are correct */
1682 if ((tw_major
!= (int)(major(stat_buf
.st_rdev
))) ||
1683 (index
!= (int)(minor(stat_buf
.st_rdev
))) ||
1684 (!S_ISCHR(stat_buf
.st_mode
))) {
1685 pout("Node %s has wrong major/minor number and must be created anew."
1686 " Check the udev rules.\n", nodestring
);
1687 /* Delete the old node */
1688 if (unlink(nodestring
)) {
1689 pout("problem unlinking stale 3ware device node %s", nodestring
);
1695 /* Make a new node */
1696 if (mknod(nodestring
, S_IFCHR
|0600, makedev(tw_major
, index
))) {
1697 pout("problem creating 3ware device nodes %s", nodestring
);
1703 #ifdef HAVE_LIBSELINUX
1704 if (selinux_enabled
&& node_context
) {
1705 freecon(node_context
);
1706 node_context
= NULL
;
1711 #ifdef HAVE_LIBSELINUX
1712 if (selinux_enabled
) {
1713 if(setfscreatecon(orig_context
) < 0) {
1714 pout("Error re-setting original fscreate context");
1715 if (selinux_enforced
)
1719 freecon(orig_context
);
1721 freecon(node_context
);
1722 matchpathcon_fini();
1728 bool linux_escalade_device::open()
1730 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
||
1731 m_escalade_type
== AMCC_3WARE_678K_CHAR
) {
1732 // the device nodes for these controllers are dynamically assigned,
1733 // so we need to check that they exist with the correct major
1734 // numbers and if not, create them
1735 const char * node
= (m_escalade_type
== AMCC_3WARE_9700_CHAR
? "twl" :
1736 m_escalade_type
== AMCC_3WARE_9000_CHAR
? "twa" :
1738 const char * driver
= (m_escalade_type
== AMCC_3WARE_9700_CHAR
? "3w-sas" :
1739 m_escalade_type
== AMCC_3WARE_9000_CHAR
? "3w-9xxx" :
1741 if (setup_3ware_nodes(node
, driver
))
1742 return set_err((errno
? errno
: ENXIO
), "setup_3ware_nodes(\"%s\", \"%s\") failed", node
, driver
);
1744 // Continue with default open
1745 return linux_smart_device::open();
1748 // TODO: Function no longer useful
1749 //void printwarning(smart_command_set command);
1752 // This is an interface routine meant to isolate the OS dependent
1753 // parts of the code, and to provide a debugging interface. Each
1754 // different port and OS needs to provide it's own interface. This
1755 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
1756 // commands to be passed through the SCSI driver.
1757 // DETAILED DESCRIPTION OF ARGUMENTS
1758 // fd: is the file descriptor provided by open()
1759 // disknum is the disk number (0 to 15) in the RAID array
1760 // escalade_type indicates the type of controller type, and if scsi or char interface is used
1761 // command: defines the different operations.
1762 // select: additional input data if needed (which log, which type of
1764 // data: location to write output data, if needed (512 bytes).
1765 // Note: not all commands use all arguments.
1767 // -1 if the command failed
1768 // 0 if the command succeeded,
1769 // STATUS_CHECK routine:
1770 // -1 if the command failed
1771 // 0 if the command succeeded and disk SMART status is "OK"
1772 // 1 if the command succeeded and disk SMART status is "FAILING"
1774 /* 512 is the max payload size: increase if needed */
1775 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
1776 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
1777 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
1778 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
1780 bool linux_escalade_device::ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
)
1782 if (!ata_cmd_is_ok(in
,
1783 true, // data_out_support
1784 false, // TODO: multi_sector_support
1785 true) // ata_48bit_support
1789 // Used by both the SCSI and char interfaces
1790 TW_Passthru
*passthru
=NULL
;
1791 char ioctl_buffer
[TW_IOCTL_BUFFER_SIZE
];
1793 // only used for SCSI device interface
1794 TW_Ioctl
*tw_ioctl
=NULL
;
1795 TW_Output
*tw_output
=NULL
;
1797 // only used for 6000/7000/8000 char device interface
1798 TW_New_Ioctl
*tw_ioctl_char
=NULL
;
1800 // only used for 9000 character device interface
1801 TW_Ioctl_Buf_Apache
*tw_ioctl_apache
=NULL
;
1803 memset(ioctl_buffer
, 0, TW_IOCTL_BUFFER_SIZE
);
1805 // TODO: Handle controller differences by different classes
1806 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
) {
1807 tw_ioctl_apache
= (TW_Ioctl_Buf_Apache
*)ioctl_buffer
;
1808 tw_ioctl_apache
->driver_command
.control_code
= TW_IOCTL_FIRMWARE_PASS_THROUGH
;
1809 tw_ioctl_apache
->driver_command
.buffer_length
= 512; /* payload size */
1810 passthru
= (TW_Passthru
*)&(tw_ioctl_apache
->firmware_command
.command
.oldcommand
);
1812 else if (m_escalade_type
==AMCC_3WARE_678K_CHAR
) {
1813 tw_ioctl_char
= (TW_New_Ioctl
*)ioctl_buffer
;
1814 tw_ioctl_char
->data_buffer_length
= 512;
1815 passthru
= (TW_Passthru
*)&(tw_ioctl_char
->firmware_command
);
1817 else if (m_escalade_type
==AMCC_3WARE_678K
) {
1818 tw_ioctl
= (TW_Ioctl
*)ioctl_buffer
;
1819 tw_ioctl
->cdb
[0] = TW_IOCTL
;
1820 tw_ioctl
->opcode
= TW_ATA_PASSTHRU
;
1821 tw_ioctl
->input_length
= 512; // correct even for non-data commands
1822 tw_ioctl
->output_length
= 512; // correct even for non-data commands
1823 tw_output
= (TW_Output
*)tw_ioctl
;
1824 passthru
= (TW_Passthru
*)&(tw_ioctl
->input_data
);
1827 return set_err(ENOSYS
,
1828 "Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1829 "Please contact " PACKAGE_BUGREPORT
"\n", (int)m_escalade_type
, m_disknum
);
1832 // Same for (almost) all commands - but some reset below
1833 passthru
->byte0
.opcode
= TW_OP_ATA_PASSTHRU
;
1834 passthru
->request_id
= 0xFF;
1835 passthru
->unit
= m_disknum
;
1836 passthru
->status
= 0;
1837 passthru
->flags
= 0x1;
1841 const ata_in_regs_48bit
& r
= in
.in_regs
;
1842 passthru
->features
= r
.features_16
;
1843 passthru
->sector_count
= r
.sector_count_16
;
1844 passthru
->sector_num
= r
.lba_low_16
;
1845 passthru
->cylinder_lo
= r
.lba_mid_16
;
1846 passthru
->cylinder_hi
= r
.lba_high_16
;
1847 passthru
->drive_head
= r
.device
;
1848 passthru
->command
= r
.command
;
1851 // Is this a command that reads or returns 512 bytes?
1852 // passthru->param values are:
1853 // 0x0 - non data command without TFR write check,
1854 // 0x8 - non data command with TFR write check,
1855 // 0xD - data command that returns data to host from device
1856 // 0xF - data command that writes data from host to device
1857 // passthru->size values are 0x5 for non-data and 0x07 for data
1858 bool readdata
= false;
1859 if (in
.direction
== ata_cmd_in::data_in
) {
1861 passthru
->byte0
.sgloff
= 0x5;
1862 passthru
->size
= 0x7; // TODO: Other value for multi-sector ?
1863 passthru
->param
= 0xD;
1864 // For 64-bit to work correctly, up the size of the command packet
1865 // in dwords by 1 to account for the 64-bit single sgl 'address'
1866 // field. Note that this doesn't agree with the typedefs but it's
1867 // right (agree with kernel driver behavior/typedefs).
1868 if ((m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1869 && sizeof(long) == 8)
1872 else if (in
.direction
== ata_cmd_in::no_data
) {
1873 // Non data command -- but doesn't use large sector
1874 // count register values.
1875 passthru
->byte0
.sgloff
= 0x0;
1876 passthru
->size
= 0x5;
1877 passthru
->param
= 0x8;
1878 passthru
->sector_count
= 0x0;
1880 else if (in
.direction
== ata_cmd_in::data_out
) {
1881 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1882 memcpy(tw_ioctl_apache
->data_buffer
, in
.buffer
, in
.size
);
1883 else if (m_escalade_type
== AMCC_3WARE_678K_CHAR
)
1884 memcpy(tw_ioctl_char
->data_buffer
, in
.buffer
, in
.size
);
1886 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1887 // memcpy(tw_output->output_data, data, 512);
1888 // printwarning(command); // TODO: Parameter no longer valid
1889 return set_err(ENOTSUP
, "DATA OUT not supported for this 3ware controller type");
1891 passthru
->byte0
.sgloff
= 0x5;
1892 passthru
->size
= 0x7; // TODO: Other value for multi-sector ?
1893 passthru
->param
= 0xF; // PIO data write
1894 if ((m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1895 && sizeof(long) == 8)
1899 return set_err(EINVAL
);
1901 // Now send the command down through an ioctl()
1903 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1904 ioctlreturn
=ioctl(get_fd(), TW_IOCTL_FIRMWARE_PASS_THROUGH
, tw_ioctl_apache
);
1905 else if (m_escalade_type
==AMCC_3WARE_678K_CHAR
)
1906 ioctlreturn
=ioctl(get_fd(), TW_CMD_PACKET_WITH_DATA
, tw_ioctl_char
);
1908 ioctlreturn
=ioctl(get_fd(), SCSI_IOCTL_SEND_COMMAND
, tw_ioctl
);
1910 // Deal with the different error cases
1912 if (AMCC_3WARE_678K
==m_escalade_type
1913 && in
.in_regs
.command
==ATA_SMART_CMD
1914 && ( in
.in_regs
.features
== ATA_SMART_AUTO_OFFLINE
1915 || in
.in_regs
.features
== ATA_SMART_AUTOSAVE
)
1916 && in
.in_regs
.lba_low
) {
1917 // error here is probably a kernel driver whose version is too old
1918 // printwarning(command); // TODO: Parameter no longer valid
1919 return set_err(ENOTSUP
, "Probably kernel driver too old");
1921 return set_err(EIO
);
1924 // The passthru structure is valid after return from an ioctl if:
1925 // - we are using the character interface OR
1926 // - we are using the SCSI interface and this is a NON-READ-DATA command
1927 // For SCSI interface, note that we set passthru to a different
1928 // value after ioctl().
1929 if (AMCC_3WARE_678K
==m_escalade_type
) {
1933 passthru
=(TW_Passthru
*)&(tw_output
->output_data
);
1936 // See if the ATA command failed. Now that we have returned from
1937 // the ioctl() call, if passthru is valid, then:
1938 // - passthru->status contains the 3ware controller STATUS
1939 // - passthru->command contains the ATA STATUS register
1940 // - passthru->features contains the ATA ERROR register
1942 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1943 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1944 // While we *might* decode the ATA ERROR register, at the moment it
1945 // doesn't make much sense: we don't care in detail why the error
1948 if (passthru
&& (passthru
->status
|| (passthru
->command
& 0x21))) {
1949 return set_err(EIO
);
1952 // If this is a read data command, copy data to output buffer
1954 if (m_escalade_type
== AMCC_3WARE_9700_CHAR
|| m_escalade_type
== AMCC_3WARE_9000_CHAR
)
1955 memcpy(in
.buffer
, tw_ioctl_apache
->data_buffer
, in
.size
);
1956 else if (m_escalade_type
==AMCC_3WARE_678K_CHAR
)
1957 memcpy(in
.buffer
, tw_ioctl_char
->data_buffer
, in
.size
);
1959 memcpy(in
.buffer
, tw_output
->output_data
, in
.size
);
1962 // Return register values
1964 ata_out_regs_48bit
& r
= out
.out_regs
;
1965 r
.error
= passthru
->features
;
1966 r
.sector_count_16
= passthru
->sector_count
;
1967 r
.lba_low_16
= passthru
->sector_num
;
1968 r
.lba_mid_16
= passthru
->cylinder_lo
;
1969 r
.lba_high_16
= passthru
->cylinder_hi
;
1970 r
.device
= passthru
->drive_head
;
1971 r
.status
= passthru
->command
;
1974 // look for nonexistent devices/ports
1975 if ( in
.in_regs
.command
== ATA_IDENTIFY_DEVICE
1976 && !nonempty(in
.buffer
, in
.size
)) {
1977 return set_err(ENODEV
, "No drive on port %d", m_disknum
);
1983 /////////////////////////////////////////////////////////////////////////////
1984 /// Areca RAID support
1986 ///////////////////////////////////////////////////////////////////
1987 // SATA(ATA) device behind Areca RAID Controller
1988 class linux_areca_ata_device
1989 : public /*implements*/ areca_ata_device
,
1990 public /*extends*/ linux_smart_device
1993 linux_areca_ata_device(smart_interface
* intf
, const char * dev_name
, int disknum
, int encnum
= 1);
1994 virtual smart_device
* autodetect_open();
1995 virtual bool arcmsr_lock();
1996 virtual bool arcmsr_unlock();
1997 virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io
* iop
);
2000 ///////////////////////////////////////////////////////////////////
2001 // SAS(SCSI) device behind Areca RAID Controller
2002 class linux_areca_scsi_device
2003 : public /*implements*/ areca_scsi_device
,
2004 public /*extends*/ linux_smart_device
2007 linux_areca_scsi_device(smart_interface
* intf
, const char * dev_name
, int disknum
, int encnum
= 1);
2008 virtual smart_device
* autodetect_open();
2009 virtual bool arcmsr_lock();
2010 virtual bool arcmsr_unlock();
2011 virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io
* iop
);
2014 // Looks in /proc/scsi to suggest correct areca devices
2015 static int find_areca_in_proc()
2017 const char* proc_format_string
="host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n";
2019 // check data formwat
2020 FILE *fp
=fopen("/proc/scsi/sg/device_hdr", "r");
2022 pout("Unable to open /proc/scsi/sg/device_hdr for reading\n");
2026 // get line, compare to format
2029 char *out
= fgets(linebuf
, 256, fp
);
2032 pout("Unable to read contents of /proc/scsi/sg/device_hdr\n");
2036 if (strcmp(linebuf
, proc_format_string
)) {
2038 // Fix this by comparing only tokens not white space!!
2039 pout("Unexpected format %s in /proc/scsi/sg/device_hdr\n", proc_format_string
);
2043 // Format is understood, now search for correct device
2044 fp
=fopen("/proc/scsi/sg/devices", "r");
2046 int host
, chan
, id
, lun
, type
, opens
, qdepth
, busy
, online
;
2049 // search all lines of /proc/scsi/sg/devices
2050 while (9 == fscanf(fp
, "%d %d %d %d %d %d %d %d %d", &host
, &chan
, &id
, &lun
, &type
, &opens
, &qdepth
, &busy
, &online
)) {
2052 if (id
== 16 && type
== 3) {
2053 // devices with id=16 and type=3 might be Areca controllers
2054 pout("Device /dev/sg%d appears to be an Areca controller.\n", dev
);
2062 // Areca RAID Controller(SATA Disk)
2063 linux_areca_ata_device::linux_areca_ata_device(smart_interface
* intf
, const char * dev_name
, int disknum
, int encnum
)
2064 : smart_device(intf
, dev_name
, "areca", "areca"),
2065 linux_smart_device(O_RDWR
| O_EXCL
| O_NONBLOCK
)
2067 set_disknum(disknum
);
2069 set_info().info_name
= strprintf("%s [areca_disk#%02d_enc#%02d]", dev_name
, disknum
, encnum
);
2072 smart_device
* linux_areca_ata_device::autodetect_open()
2074 // autodetect device type
2075 int is_ata
= arcmsr_get_dev_type();
2089 smart_device_auto_ptr
newdev(new linux_areca_scsi_device(smi(), get_dev_name(), get_disknum(), get_encnum()));
2092 newdev
->open(); // TODO: Can possibly pass open fd
2094 return newdev
.release();
2097 int linux_areca_ata_device::arcmsr_do_scsi_io(struct scsi_cmnd_io
* iop
)
2099 int ioctlreturn
= 0;
2103 find_areca_in_proc();
2107 ioctlreturn
= do_normal_scsi_cmnd_io(get_fd(), iop
, scsi_debugmode
);
2108 if ( ioctlreturn
|| iop
->scsi_status
)
2117 bool linux_areca_ata_device::arcmsr_lock()
2122 bool linux_areca_ata_device::arcmsr_unlock()
2127 // Areca RAID Controller(SAS Device)
2128 linux_areca_scsi_device::linux_areca_scsi_device(smart_interface
* intf
, const char * dev_name
, int disknum
, int encnum
)
2129 : smart_device(intf
, dev_name
, "areca", "areca"),
2130 linux_smart_device(O_RDWR
| O_EXCL
| O_NONBLOCK
)
2132 set_disknum(disknum
);
2134 set_info().info_name
= strprintf("%s [areca_disk#%02d_enc#%02d]", dev_name
, disknum
, encnum
);
2137 smart_device
* linux_areca_scsi_device::autodetect_open()
2142 int linux_areca_scsi_device::arcmsr_do_scsi_io(struct scsi_cmnd_io
* iop
)
2144 int ioctlreturn
= 0;
2148 find_areca_in_proc();
2152 ioctlreturn
= do_normal_scsi_cmnd_io(get_fd(), iop
, scsi_debugmode
);
2153 if ( ioctlreturn
|| iop
->scsi_status
)
2162 bool linux_areca_scsi_device::arcmsr_lock()
2167 bool linux_areca_scsi_device::arcmsr_unlock()
2172 /////////////////////////////////////////////////////////////////////////////
2175 class linux_marvell_device
2176 : public /*implements*/ ata_device_with_command_set
,
2177 public /*extends*/ linux_smart_device
2180 linux_marvell_device(smart_interface
* intf
, const char * dev_name
, const char * req_type
);
2183 virtual int ata_command_interface(smart_command_set command
, int select
, char * data
);
2186 linux_marvell_device::linux_marvell_device(smart_interface
* intf
,
2187 const char * dev_name
, const char * req_type
)
2188 : smart_device(intf
, dev_name
, "marvell", req_type
),
2189 linux_smart_device(O_RDONLY
| O_NONBLOCK
)
2193 int linux_marvell_device::ata_command_interface(smart_command_set command
, int select
, char * data
)
2202 mvsata_scsi_cmd smart_command
;
2203 unsigned char *buff
= (unsigned char *)&smart_command
.cmd
[6];
2204 // See struct hd_drive_cmd_hdr in hdreg.h
2205 // buff[0]: ATA COMMAND CODE REGISTER
2206 // buff[1]: ATA SECTOR NUMBER REGISTER
2207 // buff[2]: ATA FEATURES REGISTER
2208 // buff[3]: ATA SECTOR COUNT REGISTER
2210 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
2211 memset(&smart_command
, 0, sizeof(smart_command
));
2212 smart_command
.inlen
= 540;
2213 smart_command
.outlen
= 540;
2214 smart_command
.cmd
[0] = 0xC; //Vendor-specific code
2215 smart_command
.cmd
[4] = 6; //command length
2217 buff
[0] = ATA_SMART_CMD
;
2219 case CHECK_POWER_MODE
:
2220 buff
[0]=ATA_CHECK_POWER_MODE
;
2223 buff
[2]=ATA_SMART_READ_VALUES
;
2226 case READ_THRESHOLDS
:
2227 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
2228 copydata
=buff
[1]=buff
[3]=1;
2231 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
2236 buff
[0]=ATA_IDENTIFY_DEVICE
;
2240 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
2244 buff
[2]=ATA_SMART_ENABLE
;
2248 buff
[2]=ATA_SMART_DISABLE
;
2253 // this command only says if SMART is working. It could be
2254 // replaced with STATUS_CHECK below.
2255 buff
[2] = ATA_SMART_STATUS
;
2258 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
2259 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
2262 buff
[2]=ATA_SMART_AUTOSAVE
;
2263 buff
[3]=select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
2265 case IMMEDIATE_OFFLINE
:
2266 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
2270 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command
);
2274 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
2276 // We are now doing the HDIO_DRIVE_CMD type ioctl.
2277 if (ioctl(get_fd(), SCSI_IOCTL_SEND_COMMAND
, (void *)&smart_command
))
2280 if (command
==CHECK_POWER_MODE
) {
2281 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
2282 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
2287 // Always succeed on a SMART status, as a disk that failed returned
2288 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
2289 if (command
== STATUS
)
2291 //Data returned is starting from 0 offset
2292 if (command
== STATUS_CHECK
)
2294 // Cyl low and Cyl high unchanged means "Good SMART status"
2295 if (buff
[4] == 0x4F && buff
[5] == 0xC2)
2297 // These values mean "Bad SMART status"
2298 if (buff
[4] == 0xF4 && buff
[5] == 0x2C)
2300 // We haven't gotten output that makes sense; print out some debugging info
2301 syserror("Error SMART Status command failed");
2302 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT
);
2303 pout("Register values returned from SMART Status command are:\n");
2304 pout("CMD =0x%02x\n",(int)buff
[0]);
2305 pout("FR =0x%02x\n",(int)buff
[1]);
2306 pout("NS =0x%02x\n",(int)buff
[2]);
2307 pout("SC =0x%02x\n",(int)buff
[3]);
2308 pout("CL =0x%02x\n",(int)buff
[4]);
2309 pout("CH =0x%02x\n",(int)buff
[5]);
2310 pout("SEL=0x%02x\n",(int)buff
[6]);
2315 memcpy(data
, buff
, 512);
2319 /////////////////////////////////////////////////////////////////////////////
2320 /// Highpoint RAID support
2322 class linux_highpoint_device
2323 : public /*implements*/ ata_device_with_command_set
,
2324 public /*extends*/ linux_smart_device
2327 linux_highpoint_device(smart_interface
* intf
, const char * dev_name
,
2328 unsigned char controller
, unsigned char channel
, unsigned char port
);
2331 virtual int ata_command_interface(smart_command_set command
, int select
, char * data
);
2334 unsigned char m_hpt_data
[3]; ///< controller/channel/port
2337 linux_highpoint_device::linux_highpoint_device(smart_interface
* intf
, const char * dev_name
,
2338 unsigned char controller
, unsigned char channel
, unsigned char port
)
2339 : smart_device(intf
, dev_name
, "hpt", "hpt"),
2340 linux_smart_device(O_RDONLY
| O_NONBLOCK
)
2342 m_hpt_data
[0] = controller
; m_hpt_data
[1] = channel
; m_hpt_data
[2] = port
;
2343 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]);
2346 // this implementation is derived from ata_command_interface with a header
2347 // packing for highpoint linux driver ioctl interface
2349 // ioctl(fd,HPTIO_CTL,buff)
2352 // structure of hpt_buff
2353 // +----+----+----+----+--------------------.....---------------------+
2354 // | 1 | 2 | 3 | 4 | 5 |
2355 // +----+----+----+----+--------------------.....---------------------+
2357 // 1: The target controller [ int ( 4 Bytes ) ]
2358 // 2: The channel of the target controllee [ int ( 4 Bytes ) ]
2359 // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
2360 // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
2361 // 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
2362 // if no pmport device, set to 1 or leave blank
2363 // 5: data [ void * ( var leangth ) ]
2365 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
2367 int linux_highpoint_device::ata_command_interface(smart_command_set command
, int select
, char * data
)
2369 unsigned char hpt_buff
[4*sizeof(int) + STRANGE_BUFFER_LENGTH
];
2370 unsigned int *hpt
= (unsigned int *)hpt_buff
;
2371 unsigned char *buff
= &hpt_buff
[4*sizeof(int)];
2373 const int HDIO_DRIVE_CMD_OFFSET
= 4;
2375 memset(hpt_buff
, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH
);
2376 hpt
[0] = m_hpt_data
[0]; // controller id
2377 hpt
[1] = m_hpt_data
[1]; // channel number
2378 hpt
[3] = m_hpt_data
[2]; // pmport number
2380 buff
[0]=ATA_SMART_CMD
;
2382 case CHECK_POWER_MODE
:
2383 buff
[0]=ATA_CHECK_POWER_MODE
;
2387 buff
[2]=ATA_SMART_READ_VALUES
;
2391 case READ_THRESHOLDS
:
2392 buff
[2]=ATA_SMART_READ_THRESHOLDS
;
2397 buff
[2]=ATA_SMART_READ_LOG_SECTOR
;
2405 buff
[0]=ATA_IDENTIFY_DEVICE
;
2410 buff
[0]=ATA_IDENTIFY_PACKET_DEVICE
;
2415 buff
[2]=ATA_SMART_ENABLE
;
2419 buff
[2]=ATA_SMART_DISABLE
;
2423 buff
[2]=ATA_SMART_STATUS
;
2426 buff
[2]=ATA_SMART_AUTO_OFFLINE
;
2430 buff
[2]=ATA_SMART_AUTOSAVE
;
2433 case IMMEDIATE_OFFLINE
:
2434 buff
[2]=ATA_SMART_IMMEDIATE_OFFLINE
;
2438 buff
[1]=ATA_SMART_STATUS
;
2441 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
2442 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
2447 if (command
==WRITE_LOG
) {
2448 unsigned char task
[4*sizeof(int)+sizeof(ide_task_request_t
)+512];
2449 unsigned int *hpt_tf
= (unsigned int *)task
;
2450 ide_task_request_t
*reqtask
= (ide_task_request_t
*)(&task
[4*sizeof(int)]);
2451 task_struct_t
*taskfile
= (task_struct_t
*)reqtask
->io_ports
;
2453 memset(task
, 0, sizeof(task
));
2455 hpt_tf
[0] = m_hpt_data
[0]; // controller id
2456 hpt_tf
[1] = m_hpt_data
[1]; // channel number
2457 hpt_tf
[3] = m_hpt_data
[2]; // pmport number
2458 hpt_tf
[2] = HDIO_DRIVE_TASKFILE
; // real hd ioctl
2461 taskfile
->feature
= ATA_SMART_WRITE_LOG_SECTOR
;
2462 taskfile
->sector_count
= 1;
2463 taskfile
->sector_number
= select
;
2464 taskfile
->low_cylinder
= 0x4f;
2465 taskfile
->high_cylinder
= 0xc2;
2466 taskfile
->device_head
= 0;
2467 taskfile
->command
= ATA_SMART_CMD
;
2469 reqtask
->data_phase
= TASKFILE_OUT
;
2470 reqtask
->req_cmd
= IDE_DRIVE_TASK_OUT
;
2471 reqtask
->out_size
= 512;
2472 reqtask
->in_size
= 0;
2474 memcpy(task
+sizeof(ide_task_request_t
)+4*sizeof(int), data
, 512);
2476 if (ioctl(get_fd(), HPTIO_CTL
, task
))
2482 if (command
==STATUS_CHECK
){
2483 unsigned const char normal_lo
=0x4f, normal_hi
=0xc2;
2484 unsigned const char failed_lo
=0xf4, failed_hi
=0x2c;
2488 hpt
[2] = HDIO_DRIVE_TASK
;
2490 if (ioctl(get_fd(), HPTIO_CTL
, hpt_buff
))
2493 if (buff
[4]==normal_lo
&& buff
[5]==normal_hi
)
2496 if (buff
[4]==failed_lo
&& buff
[5]==failed_hi
)
2499 syserror("Error SMART Status command failed");
2500 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
2501 pout("Register values returned from SMART Status command are:\n");
2502 pout("CMD=0x%02x\n",(int)buff
[0]);
2503 pout("FR =0x%02x\n",(int)buff
[1]);
2504 pout("NS =0x%02x\n",(int)buff
[2]);
2505 pout("SC =0x%02x\n",(int)buff
[3]);
2506 pout("CL =0x%02x\n",(int)buff
[4]);
2507 pout("CH =0x%02x\n",(int)buff
[5]);
2508 pout("SEL=0x%02x\n",(int)buff
[6]);
2513 if (command
==IDENTIFY
|| command
==PIDENTIFY
) {
2514 unsigned char deviceid
[4*sizeof(int)+512*sizeof(char)];
2515 unsigned int *hpt_id
= (unsigned int *)deviceid
;
2517 hpt_id
[0] = m_hpt_data
[0]; // controller id
2518 hpt_id
[1] = m_hpt_data
[1]; // channel number
2519 hpt_id
[3] = m_hpt_data
[2]; // pmport number
2521 hpt_id
[2] = HDIO_GET_IDENTITY
;
2522 if (!ioctl(get_fd(), HPTIO_CTL
, deviceid
) && (deviceid
[4*sizeof(int)] & 0x8000))
2523 buff
[0]=(command
==IDENTIFY
)?ATA_IDENTIFY_PACKET_DEVICE
:ATA_IDENTIFY_DEVICE
;
2527 hpt
[2] = HDIO_DRIVE_CMD
;
2528 if ((ioctl(get_fd(), HPTIO_CTL
, hpt_buff
)))
2531 if (command
==CHECK_POWER_MODE
)
2532 buff
[HDIO_DRIVE_CMD_OFFSET
]=buff
[2];
2535 memcpy(data
, buff
+HDIO_DRIVE_CMD_OFFSET
, copydata
);
2540 #if 0 // TODO: Migrate from 'smart_command_set' to 'ata_in_regs' OR remove the function
2541 // Utility function for printing warnings
2542 void printwarning(smart_command_set command
){
2543 static int printed
[4]={0,0,0,0};
2544 const char* message
=
2545 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
2546 "applying a simple 3w-xxxx driver patch that can be found here:\n"
2547 PACKAGE_HOMEPAGE
"\n"
2548 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
2550 if (command
==AUTO_OFFLINE
&& !printed
[0]) {
2552 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message
);
2554 else if (command
==AUTOSAVE
&& !printed
[1]) {
2556 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message
);
2558 else if (command
==STATUS_CHECK
&& !printed
[2]) {
2560 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message
);
2562 else if (command
==WRITE_LOG
&& !printed
[3]) {
2564 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
2571 /////////////////////////////////////////////////////////////////////////////
2572 /// SCSI open with autodetection support
2574 smart_device
* linux_scsi_device::autodetect_open()
2580 // No Autodetection if device type was specified by user
2581 bool sat_only
= false;
2582 if (*get_req_type()) {
2583 // Detect SAT if device object was created by scan_smart_devices().
2584 if (!(m_scanning
&& !strcmp(get_req_type(), "sat")))
2589 // The code below is based on smartd.cpp:SCSIFilterKnown()
2592 unsigned char req_buff
[64] = {0, };
2594 if (scsiStdInquiry(this, req_buff
, req_len
)) {
2595 // Marvell controllers fail on a 36 bytes StdInquiry, but 64 suffices
2596 // watch this spot ... other devices could lock up here
2598 if (scsiStdInquiry(this, req_buff
, req_len
)) {
2599 // device doesn't like INQUIRY commands
2601 set_err(EIO
, "INQUIRY failed");
2606 int avail_len
= req_buff
[4] + 5;
2607 int len
= (avail_len
< req_len
? avail_len
: req_len
);
2611 set_err(EIO
, "INQUIRY too short for SAT");
2616 // Use INQUIRY to detect type
2620 if (!memcmp(req_buff
+ 8, "3ware", 5) || !memcmp(req_buff
+ 8, "AMCC", 4)) {
2622 set_err(EINVAL
, "AMCC/3ware controller, please try adding '-d 3ware,N',\n"
2623 "you may need to replace %s with /dev/twlN, /dev/twaN or /dev/tweN", get_dev_name());
2628 if (!memcmp(req_buff
+ 8, "DELL PERC", 12) || !memcmp(req_buff
+ 8, "MegaRAID", 8)
2629 || !memcmp(req_buff
+ 16, "PERC H700", 9) || !memcmp(req_buff
+ 8, "LSI\0",4)
2632 set_err(EINVAL
, "DELL or MegaRaid controller, please try adding '-d megaraid,N'");
2637 if (len
>= 42 && !memcmp(req_buff
+ 36, "MVSATA", 6)) {
2638 //pout("Device %s: using '-d marvell' for ATA disk with Marvell driver\n", get_dev_name());
2640 smart_device_auto_ptr
newdev(
2641 new linux_marvell_device(smi(), get_dev_name(), get_req_type())
2643 newdev
->open(); // TODO: Can possibly pass open fd
2645 return newdev
.release();
2651 smart_device
* newdev
= smi()->autodetect_sat_device(this, req_buff
, len
);
2653 // NOTE: 'this' is now owned by '*newdev'
2657 // Nothing special found
2661 set_err(EIO
, "Not a SAT device");
2666 /////////////////////////////////////////////////////////////////////////////
2669 class linux_nvme_device
2670 : public /*implements*/ nvme_device
,
2671 public /*extends*/ linux_smart_device
2674 linux_nvme_device(smart_interface
* intf
, const char * dev_name
,
2675 const char * req_type
, unsigned nsid
);
2677 virtual bool open();
2679 virtual bool nvme_pass_through(const nvme_cmd_in
& in
, nvme_cmd_out
& out
);
2682 linux_nvme_device::linux_nvme_device(smart_interface
* intf
, const char * dev_name
,
2683 const char * req_type
, unsigned nsid
)
2684 : smart_device(intf
, dev_name
, "nvme", req_type
),
2686 linux_smart_device(O_RDONLY
| O_NONBLOCK
)
2690 bool linux_nvme_device::open()
2692 if (!linux_smart_device::open())
2696 // Use actual NSID (/dev/nvmeXnN) if available,
2697 // else use broadcast namespace (/dev/nvmeX)
2698 int nsid
= ioctl(get_fd(), NVME_IOCTL_ID
, (void*)0);
2705 bool linux_nvme_device::nvme_pass_through(const nvme_cmd_in
& in
, nvme_cmd_out
& out
)
2707 nvme_passthru_cmd pt
;
2708 memset(&pt
, 0, sizeof(pt
));
2710 pt
.opcode
= in
.opcode
;
2712 pt
.addr
= (uint64_t)in
.buffer
;
2713 pt
.data_len
= in
.size
;
2714 pt
.cdw10
= in
.cdw10
;
2715 pt
.cdw11
= in
.cdw11
;
2716 pt
.cdw12
= in
.cdw12
;
2717 pt
.cdw13
= in
.cdw13
;
2718 pt
.cdw14
= in
.cdw14
;
2719 pt
.cdw15
= in
.cdw15
;
2720 // Kernel default for NVMe admin commands is 60 seconds
2721 // pt.timeout_ms = 60 * 1000;
2723 int status
= ioctl(get_fd(), NVME_IOCTL_ADMIN_CMD
, &pt
);
2726 return set_err(errno
, "NVME_IOCTL_ADMIN_CMD: %s", strerror(errno
));
2729 return set_nvme_err(out
, status
);
2731 out
.result
= pt
.result
;
2736 //////////////////////////////////////////////////////////////////////
2737 // USB bridge ID detection
2739 // Read USB ID from /sys file
2740 static bool read_id(const std::string
& path
, unsigned short & id
)
2742 FILE * f
= fopen(path
.c_str(), "r");
2746 bool ok
= (fscanf(f
, "%hx%n", &id
, &n
) == 1 && n
== 4);
2751 // Get USB bridge ID for "sdX" or "sgN"
2752 static bool get_usb_id(const char * name
, unsigned short & vendor_id
,
2753 unsigned short & product_id
, unsigned short & version
)
2755 // Only "sdX" or "sgN" supported
2756 if (!(name
[0] == 's' && (name
[1] == 'd' || name
[1] == 'g') && !strchr(name
, '/')))
2759 // Start search at dir referenced by symlink
2760 // "/sys/block/sdX/device" or
2761 // "/sys/class/scsi_generic/sgN"
2762 // -> "/sys/devices/.../usb*/.../host*/target*/..."
2763 std::string dir
= strprintf("/sys/%s/%s%s",
2764 (name
[1] == 'd' ? "block" : "class/scsi_generic"), name
,
2765 (name
[1] == 'd' ? "/device" : ""));
2767 // Stop search at "/sys/devices"
2769 if (stat("/sys/devices", &st
))
2771 ino_t stop_ino
= st
.st_ino
;
2773 // Search in parent directories until "idVendor" is found,
2774 // fail if "/sys/devices" reached or too many iterations
2778 if (!(++cnt
< 10 && !stat(dir
.c_str(), &st
) && st
.st_ino
!= stop_ino
))
2780 } while (access((dir
+ "/idVendor").c_str(), 0));
2783 if (!( read_id(dir
+ "/idVendor", vendor_id
)
2784 && read_id(dir
+ "/idProduct", product_id
)
2785 && read_id(dir
+ "/bcdDevice", version
) ))
2788 if (scsi_debugmode
> 1)
2789 pout("USB ID = 0x%04x:0x%04x (0x%03x)\n", vendor_id
, product_id
, version
);
2793 //////////////////////////////////////////////////////////////////////
2796 class linux_smart_interface
2797 : public /*implements*/ smart_interface
2800 virtual std::string
get_os_version_str();
2802 virtual std::string
get_app_examples(const char * appname
);
2804 virtual bool scan_smart_devices(smart_device_list
& devlist
,
2805 const smart_devtype_list
& types
, const char * pattern
= 0);
2808 virtual ata_device
* get_ata_device(const char * name
, const char * type
);
2810 virtual scsi_device
* get_scsi_device(const char * name
, const char * type
);
2812 virtual nvme_device
* get_nvme_device(const char * name
, const char * type
,
2815 virtual smart_device
* autodetect_smart_device(const char * name
);
2817 virtual smart_device
* get_custom_smart_device(const char * name
, const char * type
);
2819 virtual std::string
get_valid_custom_dev_types_str();
2822 static const int devxy_to_n_max
= 103; // Max value of devxy_to_n() below
2824 void get_dev_list(smart_device_list
& devlist
, const char * pattern
,
2825 bool scan_scsi
, bool (* p_dev_sdxy_seen
)[devxy_to_n_max
+1],
2826 bool scan_nvme
, const char * req_type
, bool autodetect
);
2828 bool get_dev_megasas(smart_device_list
& devlist
);
2829 smart_device
* missing_option(const char * opt
);
2830 int megasas_dcmd_cmd(int bus_no
, uint32_t opcode
, void *buf
,
2831 size_t bufsize
, uint8_t *mbox
, size_t mboxlen
, uint8_t *statusp
);
2832 int megasas_pd_add_list(int bus_no
, smart_device_list
& devlist
);
2835 std::string
linux_smart_interface::get_os_version_str()
2839 return strprintf("%s-linux-%s", u
.machine
, u
.release
);
2841 return SMARTMONTOOLS_BUILD_HOST
;
2844 std::string
linux_smart_interface::get_app_examples(const char * appname
)
2846 if (!strcmp(appname
, "smartctl"))
2847 return smartctl_examples
;
2851 // "/dev/sdXY" -> 0-103
2852 // "/dev/disk/by-id/NAME" -> "../../sdXY" -> 0-103
2854 static int devxy_to_n(const char * name
, bool debug
)
2858 if (str_starts_with(name
, "/dev/sd")) {
2859 // Assume "/dev/sdXY"
2860 xy
= name
+ sizeof("/dev/sd") - 1;
2863 // Assume "/dev/disk/by-id/NAME", check link target
2864 int sz
= readlink(name
, dest
, sizeof(dest
)-1);
2865 if (!(0 < sz
&& sz
< (int)sizeof(dest
)))
2868 if (!str_starts_with(dest
, "../../sd"))
2871 pout("%s -> %s\n", name
, dest
);
2872 xy
= dest
+ sizeof("../../sd") - 1;
2876 if (!('a' <= x
&& x
<= 'z'))
2883 if (!(x
<= 'c' && 'a' <= y
&& y
<= 'z' && !xy
[2]))
2885 // "[a-c][a-z]" -> 26-103
2886 return (x
- 'a' + 1) * ('z' - 'a' + 1) + (y
- 'a');
2889 void linux_smart_interface::get_dev_list(smart_device_list
& devlist
,
2890 const char * pattern
, bool scan_scsi
, bool (* p_dev_sdxy_seen
)[devxy_to_n_max
+1],
2891 bool scan_nvme
, const char * req_type
, bool autodetect
)
2893 bool debug
= (ata_debugmode
|| scsi_debugmode
|| nvme_debugmode
);
2895 // Use glob to look for any directory entries matching the pattern
2897 memset(&globbuf
, 0, sizeof(globbuf
));
2898 int retglob
= glob(pattern
, GLOB_ERR
, NULL
, &globbuf
);
2900 // glob failed: free memory and return
2904 pout("glob(3) error %d for pattern %s\n", retglob
, pattern
);
2906 if (retglob
== GLOB_NOSPACE
)
2907 throw std::bad_alloc();
2911 // did we find too many paths?
2912 const int max_pathc
= 1024;
2913 int n
= (int)globbuf
.gl_pathc
;
2914 if (n
> max_pathc
) {
2915 pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n",
2916 n
, max_pathc
, pattern
, n
- max_pathc
);
2920 // now step through the list returned by glob.
2921 for (int i
= 0; i
< n
; i
++) {
2922 const char * name
= globbuf
.gl_pathv
[i
];
2924 if (p_dev_sdxy_seen
) {
2925 // Follow "/dev/disk/by-id/*" symlink and check for duplicate "/dev/sdXY"
2926 int dev_n
= devxy_to_n(name
, debug
);
2927 if (!(0 <= dev_n
&& dev_n
<= devxy_to_n_max
))
2929 if ((*p_dev_sdxy_seen
)[dev_n
]) {
2931 pout("%s: duplicate, ignored\n", name
);
2934 (*p_dev_sdxy_seen
)[dev_n
] = true;
2939 dev
= autodetect_smart_device(name
);
2944 dev
= new linux_scsi_device(this, name
, req_type
, true /*scanning*/);
2946 dev
= new linux_nvme_device(this, name
, req_type
, 0 /* use default nsid */);
2948 dev
= new linux_ata_device(this, name
, req_type
);
2949 devlist
.push_back(dev
);
2956 // getting devices from LSI SAS MegaRaid, if available
2957 bool linux_smart_interface::get_dev_megasas(smart_device_list
& devlist
)
2959 /* Scanning of disks on MegaRaid device */
2960 /* Perform mknod of device ioctl node */
2963 bool scan_megasas
= false;
2964 FILE * fp
= fopen("/proc/devices", "r");
2967 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
2969 if (sscanf(line
, "%d megaraid_sas_ioctl%n", &mjr
, &n1
) == 1 && n1
== 22) {
2970 scan_megasas
= true;
2971 n1
=mknod("/dev/megaraid_sas_ioctl_node", S_IFCHR
, makedev(mjr
, 0));
2972 if(scsi_debugmode
> 0)
2973 pout("Creating /dev/megaraid_sas_ioctl_node = %d\n", n1
>= 0 ? 0 : errno
);
2974 if (n1
>= 0 || errno
== EEXIST
)
2983 // getting bus numbers with megasas devices
2984 // we are using sysfs to get list of all scsi hosts
2985 DIR * dp
= opendir ("/sys/class/scsi_host/");
2989 while ((ep
= readdir (dp
)) != NULL
) {
2990 unsigned int host_no
= 0;
2991 if (!sscanf(ep
->d_name
, "host%u", &host_no
))
2993 /* proc_name should be megaraid_sas */
2995 snprintf(sysfsdir
, sizeof(sysfsdir
) - 1,
2996 "/sys/class/scsi_host/host%u/proc_name", host_no
);
2997 if((fp
= fopen(sysfsdir
, "r")) == NULL
)
2999 if(fgets(line
, sizeof(line
), fp
) != NULL
&& !strncmp(line
,"megaraid_sas",12)) {
3000 megasas_pd_add_list(host_no
, devlist
);
3004 (void) closedir (dp
);
3005 } else { /* sysfs not mounted ? */
3006 for(unsigned i
= 0; i
<=16; i
++) // trying to add devices on first 16 buses
3007 megasas_pd_add_list(i
, devlist
);
3012 bool linux_smart_interface::scan_smart_devices(smart_device_list
& devlist
,
3013 const smart_devtype_list
& types
, const char * pattern
/*= 0*/)
3016 return set_err(EINVAL
, "DEVICESCAN with pattern not implemented yet");
3020 const char * type_ata
= 0, * type_scsi
= 0, * type_sat
= 0, * type_nvme
= 0;
3021 for (unsigned i
= 0; i
< types
.size(); i
++) {
3022 const char * type
= types
[i
].c_str();
3023 if (!strcmp(type
, "by-id"))
3025 else if (!strcmp(type
, "ata"))
3027 else if (!strcmp(type
, "scsi"))
3029 else if (!strcmp(type
, "sat"))
3031 else if (!strcmp(type
, "nvme"))
3034 return set_err(EINVAL
, "Invalid type '%s', valid arguments are: by-id, ata, scsi, sat, nvme",
3037 // Use default if no type specified
3038 if (!(type_ata
|| type_scsi
|| type_sat
|| type_nvme
)) {
3039 type_ata
= type_scsi
= type_sat
= "";
3040 #ifdef WITH_NVME_DEVICESCAN // TODO: Remove when NVMe support is no longer EXPERIMENTAL
3046 get_dev_list(devlist
, "/dev/hd[a-t]", false, 0, false, type_ata
, false);
3048 if (type_scsi
|| type_sat
) {
3049 // "sat" detection will be later handled in linux_scsi_device::autodetect_open()
3050 const char * type_scsi_sat
= ((type_scsi
&& type_sat
) ? "" // detect both
3051 : (type_scsi
? type_scsi
: type_sat
));
3052 bool autodetect
= !*type_scsi_sat
; // If no type specified, detect USB also
3054 bool dev_sdxy_seen
[devxy_to_n_max
+1] = {false, };
3055 bool (*p_dev_sdxy_seen
)[devxy_to_n_max
+1] = 0;
3057 // Scan unique symlinks first
3058 get_dev_list(devlist
, "/dev/disk/by-id/*", true, &dev_sdxy_seen
, false,
3059 type_scsi_sat
, autodetect
);
3060 p_dev_sdxy_seen
= &dev_sdxy_seen
; // Check for duplicates below
3063 get_dev_list(devlist
, "/dev/sd[a-z]", true, p_dev_sdxy_seen
, false, type_scsi_sat
, autodetect
);
3064 get_dev_list(devlist
, "/dev/sd[a-c][a-z]", true, p_dev_sdxy_seen
, false, type_scsi_sat
, autodetect
);
3066 // get device list from the megaraid device
3067 get_dev_megasas(devlist
);
3071 get_dev_list(devlist
, "/dev/nvme[0-9]", false, 0, true, type_nvme
, false);
3072 get_dev_list(devlist
, "/dev/nvme[1-9][0-9]", false, 0, true, type_nvme
, false);
3078 ata_device
* linux_smart_interface::get_ata_device(const char * name
, const char * type
)
3080 return new linux_ata_device(this, name
, type
);
3083 scsi_device
* linux_smart_interface::get_scsi_device(const char * name
, const char * type
)
3085 return new linux_scsi_device(this, name
, type
);
3088 nvme_device
* linux_smart_interface::get_nvme_device(const char * name
, const char * type
,
3091 return new linux_nvme_device(this, name
, type
, nsid
);
3094 smart_device
* linux_smart_interface::missing_option(const char * opt
)
3096 set_err(EINVAL
, "requires option '%s'", opt
);
3101 linux_smart_interface::megasas_dcmd_cmd(int bus_no
, uint32_t opcode
, void *buf
,
3102 size_t bufsize
, uint8_t *mbox
, size_t mboxlen
, uint8_t *statusp
)
3104 struct megasas_iocpacket ioc
;
3106 if ((mbox
!= NULL
&& (mboxlen
== 0 || mboxlen
> MFI_MBOX_SIZE
)) ||
3107 (mbox
== NULL
&& mboxlen
!= 0))
3113 bzero(&ioc
, sizeof(ioc
));
3114 struct megasas_dcmd_frame
* dcmd
= &ioc
.frame
.dcmd
;
3115 ioc
.host_no
= bus_no
;
3117 bcopy(mbox
, dcmd
->mbox
.w
, mboxlen
);
3118 dcmd
->cmd
= MFI_CMD_DCMD
;
3121 dcmd
->data_xfer_len
= bufsize
;
3122 dcmd
->opcode
= opcode
;
3125 dcmd
->sge_count
= 1;
3126 dcmd
->data_xfer_len
= bufsize
;
3127 dcmd
->sgl
.sge32
[0].phys_addr
= (intptr_t)buf
;
3128 dcmd
->sgl
.sge32
[0].length
= (uint32_t)bufsize
;
3130 ioc
.sgl_off
= offsetof(struct megasas_dcmd_frame
, sgl
);
3131 ioc
.sgl
[0].iov_base
= buf
;
3132 ioc
.sgl
[0].iov_len
= bufsize
;
3136 if ((fd
= ::open("/dev/megaraid_sas_ioctl_node", O_RDWR
)) <= 0) {
3140 int r
= ioctl(fd
, MEGASAS_IOC_FIRMWARE
, &ioc
);
3146 if (statusp
!= NULL
)
3147 *statusp
= dcmd
->cmd_status
;
3148 else if (dcmd
->cmd_status
!= MFI_STAT_OK
) {
3149 fprintf(stderr
, "command %x returned error status %x\n",
3150 opcode
, dcmd
->cmd_status
);
3158 linux_smart_interface::megasas_pd_add_list(int bus_no
, smart_device_list
& devlist
)
3161 * Keep fetching the list in a loop until we have a large enough
3162 * buffer to hold the entire list.
3164 megasas_pd_list
* list
= 0;
3165 for (unsigned list_size
= 1024; ; ) {
3166 list
= reinterpret_cast<megasas_pd_list
*>(realloc(list
, list_size
));
3168 throw std::bad_alloc();
3169 bzero(list
, list_size
);
3170 if (megasas_dcmd_cmd(bus_no
, MFI_DCMD_PD_GET_LIST
, list
, list_size
, NULL
, 0,
3176 if (list
->size
<= list_size
)
3178 list_size
= list
->size
;
3181 // adding all SCSI devices
3182 for (unsigned i
= 0; i
< list
->count
; i
++) {
3183 if(list
->addr
[i
].scsi_dev_type
)
3184 continue; /* non disk device found */
3186 snprintf(line
, sizeof(line
) - 1, "/dev/bus/%d", bus_no
);
3187 smart_device
* dev
= new linux_megaraid_device(this, line
, list
->addr
[i
].device_id
);
3188 devlist
.push_back(dev
);
3194 // Return kernel release as integer ("2.6.31" -> 206031)
3195 static unsigned get_kernel_release()
3200 unsigned x
= 0, y
= 0, z
= 0;
3201 if (!(sscanf(u
.release
, "%u.%u.%u", &x
, &y
, &z
) == 3
3202 && x
< 100 && y
< 100 && z
< 1000 ))
3204 return x
* 100000 + y
* 1000 + z
;
3207 // Check for SCSI host proc_name "hpsa"
3208 static bool is_hpsa(const char * name
)
3211 snprintf(path
, sizeof(path
), "/sys/block/%s/device", name
);
3212 char * syshostpath
= realpath(path
, (char *)0);
3216 char * syshost
= strrchr(syshostpath
, '/');
3222 char * hostsep
= strchr(++syshost
, ':');
3226 snprintf(path
, sizeof(path
), "/sys/class/scsi_host/host%s/proc_name", syshost
);
3228 int fd
= open(path
, O_RDONLY
);
3233 ssize_t n
= read(fd
, proc_name
, sizeof(proc_name
) - 1);
3239 if (proc_name
[n
- 1] == '\n')
3240 proc_name
[n
- 1] = 0;
3242 if (scsi_debugmode
> 1)
3243 pout("%s -> %s: \"%s\"\n", name
, path
, proc_name
);
3245 if (strcmp(proc_name
, "hpsa"))
3251 // Guess device type (ata or scsi) based on device name (Linux
3252 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
3253 // osst, nosst and sg.
3254 smart_device
* linux_smart_interface::autodetect_smart_device(const char * name
)
3256 const char * test_name
= name
;
3258 // Dereference symlinks
3260 std::string pathbuf
;
3261 if (!lstat(name
, &st
) && S_ISLNK(st
.st_mode
)) {
3262 char * p
= realpath(name
, (char *)0);
3266 test_name
= pathbuf
.c_str();
3270 // Remove the leading /dev/... if it's there
3271 static const char dev_prefix
[] = "/dev/";
3272 if (str_starts_with(test_name
, dev_prefix
))
3273 test_name
+= strlen(dev_prefix
);
3275 // form /dev/h* or h*
3276 if (str_starts_with(test_name
, "h"))
3277 return new linux_ata_device(this, name
, "");
3279 // form /dev/ide/* or ide/*
3280 if (str_starts_with(test_name
, "ide/"))
3281 return new linux_ata_device(this, name
, "");
3283 // form /dev/s* or s*
3284 if (str_starts_with(test_name
, "s")) {
3286 // Try to detect possible USB->(S)ATA bridge
3287 unsigned short vendor_id
= 0, product_id
= 0, version
= 0;
3288 if (get_usb_id(test_name
, vendor_id
, product_id
, version
)) {
3289 const char * usbtype
= get_usb_dev_type_by_id(vendor_id
, product_id
, version
);
3293 // Kernels before 2.6.29 do not support the sense data length
3294 // required for SAT ATA PASS-THROUGH(16)
3295 if (!strcmp(usbtype
, "sat") && get_kernel_release() < 206029)
3298 // Return SAT/USB device for this type
3299 // (Note: linux_scsi_device::autodetect_open() will not be called in this case)
3300 return get_scsi_passthrough_device(usbtype
, new linux_scsi_device(this, name
, ""));
3303 // Fail if hpsa driver
3304 if (is_hpsa(test_name
))
3305 return missing_option("-d cciss,N");
3307 // No USB bridge or hpsa driver found, assume regular SCSI device
3308 return new linux_scsi_device(this, name
, "");
3311 // form /dev/scsi/* or scsi/*
3312 if (str_starts_with(test_name
, "scsi/"))
3313 return new linux_scsi_device(this, name
, "");
3315 // form /dev/bsg/* or bsg/*
3316 if (str_starts_with(test_name
, "bsg/"))
3317 return new linux_scsi_device(this, name
, "");
3319 // form /dev/ns* or ns*
3320 if (str_starts_with(test_name
, "ns"))
3321 return new linux_scsi_device(this, name
, "");
3323 // form /dev/os* or os*
3324 if (str_starts_with(test_name
, "os"))
3325 return new linux_scsi_device(this, name
, "");
3327 // form /dev/nos* or nos*
3328 if (str_starts_with(test_name
, "nos"))
3329 return new linux_scsi_device(this, name
, "");
3331 // form /dev/nvme* or nvme*
3332 if (str_starts_with(test_name
, "nvme"))
3333 return new linux_nvme_device(this, name
, "", 0 /* use default nsid */);
3335 // form /dev/tw[ael]* or tw[ael]*
3336 if (str_starts_with(test_name
, "tw") && strchr("ael", test_name
[2]))
3337 return missing_option("-d 3ware,N");
3339 // form /dev/cciss/* or cciss/*
3340 if (str_starts_with(test_name
, "cciss/"))
3341 return missing_option("-d cciss,N");
3343 // we failed to recognize any of the forms
3347 smart_device
* linux_smart_interface::get_custom_smart_device(const char * name
, const char * type
)
3350 if (!strcmp(type
, "marvell"))
3351 return new linux_marvell_device(this, name
, type
);
3354 int disknum
= -1, n1
= -1, n2
= -1;
3355 if (sscanf(type
, "3ware,%n%d%n", &n1
, &disknum
, &n2
) == 1 || n1
== 6) {
3356 if (n2
!= (int)strlen(type
)) {
3357 set_err(EINVAL
, "Option -d 3ware,N requires N to be a non-negative integer");
3360 if (!(0 <= disknum
&& disknum
<= 127)) {
3361 set_err(EINVAL
, "Option -d 3ware,N (N=%d) must have 0 <= N <= 127", disknum
);
3365 if (!strncmp(name
, "/dev/twl", 8))
3366 return new linux_escalade_device(this, name
, linux_escalade_device::AMCC_3WARE_9700_CHAR
, disknum
);
3367 else if (!strncmp(name
, "/dev/twa", 8))
3368 return new linux_escalade_device(this, name
, linux_escalade_device::AMCC_3WARE_9000_CHAR
, disknum
);
3369 else if (!strncmp(name
, "/dev/twe", 8))
3370 return new linux_escalade_device(this, name
, linux_escalade_device::AMCC_3WARE_678K_CHAR
, disknum
);
3372 return new linux_escalade_device(this, name
, linux_escalade_device::AMCC_3WARE_678K
, disknum
);
3376 disknum
= n1
= n2
= -1;
3378 if (sscanf(type
, "areca,%n%d/%d%n", &n1
, &disknum
, &encnum
, &n2
) >= 1 || n1
== 6) {
3379 if (!(1 <= disknum
&& disknum
<= 128)) {
3380 set_err(EINVAL
, "Option -d areca,N/E (N=%d) must have 1 <= N <= 128", disknum
);
3383 if (!(1 <= encnum
&& encnum
<= 8)) {
3384 set_err(EINVAL
, "Option -d areca,N/E (E=%d) must have 1 <= E <= 8", encnum
);
3387 return new linux_areca_ata_device(this, name
, disknum
, encnum
);
3391 int controller
= -1, channel
= -1; disknum
= 1;
3392 n1
= n2
= -1; int n3
= -1;
3393 if (sscanf(type
, "hpt,%n%d/%d%n/%d%n", &n1
, &controller
, &channel
, &n2
, &disknum
, &n3
) >= 2 || n1
== 4) {
3394 int len
= strlen(type
);
3395 if (!(n2
== len
|| n3
== len
)) {
3396 set_err(EINVAL
, "Option '-d hpt,L/M/N' supports 2-3 items");
3399 if (!(1 <= controller
&& controller
<= 8)) {
3400 set_err(EINVAL
, "Option '-d hpt,L/M/N' invalid controller id L supplied");
3403 if (!(1 <= channel
&& channel
<= 128)) {
3404 set_err(EINVAL
, "Option '-d hpt,L/M/N' invalid channel number M supplied");
3407 if (!(1 <= disknum
&& disknum
<= 15)) {
3408 set_err(EINVAL
, "Option '-d hpt,L/M/N' invalid pmport number N supplied");
3411 return new linux_highpoint_device(this, name
, controller
, channel
, disknum
);
3414 #ifdef HAVE_LINUX_CCISS_IOCTL_H
3416 disknum
= n1
= n2
= -1;
3417 if (sscanf(type
, "cciss,%n%d%n", &n1
, &disknum
, &n2
) == 1 || n1
== 6) {
3418 if (n2
!= (int)strlen(type
)) {
3419 set_err(EINVAL
, "Option -d cciss,N requires N to be a non-negative integer");
3422 if (!(0 <= disknum
&& disknum
<= 127)) {
3423 set_err(EINVAL
, "Option -d cciss,N (N=%d) must have 0 <= N <= 127", disknum
);
3426 return get_sat_device("sat,auto", new linux_cciss_device(this, name
, disknum
));
3428 #endif // HAVE_LINUX_CCISS_IOCTL_H
3431 if (sscanf(type
, "megaraid,%d", &disknum
) == 1) {
3432 return new linux_megaraid_device(this, name
, disknum
);
3436 unsigned host
, chan
, device
;
3437 if (sscanf(type
, "aacraid,%u,%u,%u", &host
, &chan
, &device
) == 3) {
3438 //return new linux_aacraid_device(this,name,channel,device);
3439 return get_sat_device("sat,auto",
3440 new linux_aacraid_device(this, name
, host
, chan
, device
));
3447 std::string
linux_smart_interface::get_valid_custom_dev_types_str()
3449 return "marvell, areca,N/E, 3ware,N, hpt,L/M/N, megaraid,N, aacraid,H,L,ID"
3450 #ifdef HAVE_LINUX_CCISS_IOCTL_H
3458 /////////////////////////////////////////////////////////////////////////////
3459 /// Initialize platform interface and register with smi()
3461 void smart_interface::init()
3463 static os_linux::linux_smart_interface the_interface
;
3464 smart_interface::set(&the_interface
);