smartmontools SVN Rev 5419
Utility to control and monitor storage systems with "S.M.A.R.T."
os_linux.cpp
Go to the documentation of this file.
1/*
2 * os_linux.cpp
3 *
4 * Home page of code is: https://www.smartmontools.org
5 *
6 * Copyright (C) 2003-11 Bruce Allen
7 * Copyright (C) 2003-11 Doug Gilbert <dgilbert@interlog.com>
8 * Copyright (C) 2008-22 Christian Franke
9 *
10 * Original AACRaid code:
11 * Copyright (C) 2014 Raghava Aditya <raghava.aditya@pmcs.com>
12 *
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>
16 *
17 * Original MegaRAID code:
18 * Copyright (C) 2008 Jordan Hargrave <jordan_hargrave@dell.com>
19 *
20 * 3ware code was derived from code that was:
21 *
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>
26 *
27 * Copyright (C) 1999-2003 3ware Inc.
28 *
29 * Kernel compatibility By: Andre Hedrick <andre@suse.com>
30 * Non-Copyright (C) 2000 Andre Hedrick <andre@suse.com>
31 *
32 * Other ars of this file are derived from code that was
33 *
34 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
35 * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org>
36 *
37 * SPDX-License-Identifier: GPL-2.0-or-later
38 */
39
40#include "config.h"
41
42#include <errno.h>
43#include <fcntl.h>
44#include <glob.h>
45
46#include <scsi/scsi.h>
47#include <scsi/scsi_ioctl.h>
48#include <scsi/sg.h>
49#include <linux/bsg.h>
50#include <stdlib.h>
51#include <string.h>
52#include <sys/ioctl.h>
53#include <sys/stat.h>
54#include <sys/utsname.h>
55#include <unistd.h>
56#include <stddef.h> // for offsetof()
57#include <sys/uio.h>
58#include <sys/types.h>
59#include <dirent.h>
60#ifdef HAVE_SYS_SYSMACROS_H
61// glibc 2.25: The inclusion of <sys/sysmacros.h> by <sys/types.h> is
62// deprecated. A warning is printed if major(), minor() or makedev()
63// is used but <sys/sysmacros.h> is not included.
64#include <sys/sysmacros.h>
65#endif
66#ifdef HAVE_LIBSELINUX
67#include <selinux/selinux.h>
68#endif
69
70#include "atacmds.h"
71#include "os_linux.h"
72#include "scsicmds.h"
73#include "utility.h"
74#include "cciss.h"
75#include "megaraid.h"
76#include "aacraid.h"
77#include "nvmecmds.h"
78
79#include "dev_interface.h"
80#include "dev_ata_cmd_set.h"
81#include "dev_areca.h"
82
83// "include/uapi/linux/nvme_ioctl.h" from Linux kernel sources
84#include "linux_nvme_ioctl.h" // nvme_passthru_cmd, NVME_IOCTL_ADMIN_CMD
85
86#ifndef ENOTSUP
87#define ENOTSUP ENOSYS
88#endif
89
90#define ARGUSED(x) ((void)(x))
91
92const char * os_linux_cpp_cvsid = "$Id: os_linux.cpp 5397 2022-07-17 02:22:29Z dpgilbert $"
94extern unsigned char failuretest_permissive;
95
96namespace os_linux { // No need to publish anything, name provided for Doxygen
97
98/////////////////////////////////////////////////////////////////////////////
99/// Shared open/close routines
100
102: virtual public /*implements*/ smart_device
103{
104public:
105 explicit linux_smart_device(int flags, int retry_flags = -1)
107 m_fd(-1),
108 m_flags(flags), m_retry_flags(retry_flags)
109 { }
110
111 virtual ~linux_smart_device();
112
113 virtual bool is_open() const override;
114
115 virtual bool open() override;
116
117 virtual bool close() override;
118
119protected:
120 /// Return filedesc for derived classes.
121 int get_fd() const
122 { return m_fd; }
123
124 void set_fd(int fd)
125 { m_fd = fd; }
126
127private:
128 int m_fd; ///< filedesc, -1 if not open.
129 int m_flags; ///< Flags for ::open()
130 int m_retry_flags; ///< Flags to retry ::open(), -1 if no retry
131};
132
134{
135 if (m_fd >= 0)
136 ::close(m_fd);
137}
138
140{
141 return (m_fd >= 0);
142}
143
145{
147
148 if (m_fd < 0 && errno == EROFS && m_retry_flags != -1)
149 // Retry
151
152 if (m_fd < 0) {
153 if (errno == EBUSY && (m_flags & O_EXCL))
154 // device is locked
155 return set_err(EBUSY,
156 "The requested controller is used exclusively by another process!\n"
157 "(e.g. smartctl or smartd)\n"
158 "Please quit the impeding process or try again later...");
159 return set_err((errno==ENOENT || errno==ENOTDIR) ? ENODEV : errno);
160 }
161
162 if (m_fd >= 0) {
163 // sets FD_CLOEXEC on the opened device file descriptor. The
164 // descriptor is otherwise leaked to other applications (mail
165 // sender) which may be considered a security risk and may result
166 // in AVC messages on SELinux-enabled systems.
167 if (-1 == fcntl(m_fd, F_SETFD, FD_CLOEXEC))
168 // TODO: Provide an error printing routine in class smart_interface
169 pout("fcntl(set FD_CLOEXEC) failed, errno=%d [%s]\n", errno, strerror(errno));
170 }
171
172 return true;
173}
174
175// equivalent to close(file descriptor)
177{
178 int fd = m_fd; m_fd = -1;
179 if (::close(fd) < 0)
180 return set_err(errno);
181 return true;
182}
183
184// examples for smartctl
185static const char smartctl_examples[] =
186 "=================================================== SMARTCTL EXAMPLES =====\n\n"
187 " smartctl --all /dev/sda (Prints all SMART information)\n\n"
188 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/sda\n"
189 " (Enables SMART on first disk)\n\n"
190 " smartctl --test=long /dev/sda (Executes extended disk self-test)\n\n"
191 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/sda\n"
192 " (Prints Self-Test & Attribute errors)\n"
193 " smartctl --all --device=3ware,2 /dev/sda\n"
194 " smartctl --all --device=3ware,2 /dev/twe0\n"
195 " smartctl --all --device=3ware,2 /dev/twa0\n"
196 " smartctl --all --device=3ware,2 /dev/twl0\n"
197 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
198 " smartctl --all --device=hpt,1/1/3 /dev/sda\n"
199 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
200 " of the 1st channel on the 1st HighPoint RAID controller)\n"
201 " smartctl --all --device=areca,3/1 /dev/sg2\n"
202 " (Prints all SMART info for 3rd ATA disk of the 1st enclosure\n"
203 " on Areca RAID controller)\n"
204 ;
205
206/////////////////////////////////////////////////////////////////////////////
207/// Linux ATA support
208
210: public /*implements*/ ata_device_with_command_set,
211 public /*extends*/ linux_smart_device
212{
213public:
214 linux_ata_device(smart_interface * intf, const char * dev_name, const char * req_type);
215
216protected:
217 virtual int ata_command_interface(smart_command_set command, int select, char * data) override;
218};
219
220linux_ata_device::linux_ata_device(smart_interface * intf, const char * dev_name, const char * req_type)
221: smart_device(intf, dev_name, "ata", req_type),
222 linux_smart_device(O_RDONLY | O_NONBLOCK)
223{
224}
225
226// PURPOSE
227// This is an interface routine meant to isolate the OS dependent
228// parts of the code, and to provide a debugging interface. Each
229// different port and OS needs to provide it's own interface. This
230// is the linux one.
231// DETAILED DESCRIPTION OF ARGUMENTS
232// device: is the file descriptor provided by open()
233// command: defines the different operations.
234// select: additional input data if needed (which log, which type of
235// self-test).
236// data: location to write output data, if needed (512 bytes).
237// Note: not all commands use all arguments.
238// RETURN VALUES
239// -1 if the command failed
240// 0 if the command succeeded,
241// STATUS_CHECK routine:
242// -1 if the command failed
243// 0 if the command succeeded and disk SMART status is "OK"
244// 1 if the command succeeded and disk SMART status is "FAILING"
245
246#define BUFFER_LENGTH (4+512)
247
249{
250 unsigned char buff[BUFFER_LENGTH];
251 // positive: bytes to write to caller. negative: bytes to READ from
252 // caller. zero: non-data command
253 int copydata=0;
254
255 const int HDIO_DRIVE_CMD_OFFSET = 4;
256
257 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
258 // buff[0]: ATA COMMAND CODE REGISTER
259 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
260 // buff[2]: ATA FEATURES REGISTER
261 // buff[3]: ATA SECTOR COUNT REGISTER
262
263 // Note that on return:
264 // buff[2] contains the ATA SECTOR COUNT REGISTER
265
266 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
267 memset(buff, 0, BUFFER_LENGTH);
268
269 buff[0]=ATA_SMART_CMD;
270 switch (command){
271 case CHECK_POWER_MODE:
272 buff[0]=ATA_CHECK_POWER_MODE;
273 copydata=1;
274 break;
275 case READ_VALUES:
276 buff[2]=ATA_SMART_READ_VALUES;
277 buff[3]=1;
278 copydata=512;
279 break;
280 case READ_THRESHOLDS:
282 buff[1]=buff[3]=1;
283 copydata=512;
284 break;
285 case READ_LOG:
287 buff[1]=select;
288 buff[3]=1;
289 copydata=512;
290 break;
291 case WRITE_LOG:
292 break;
293 case IDENTIFY:
294 buff[0]=ATA_IDENTIFY_DEVICE;
295 buff[3]=1;
296 copydata=512;
297 break;
298 case PIDENTIFY:
300 buff[3]=1;
301 copydata=512;
302 break;
303 case ENABLE:
304 buff[2]=ATA_SMART_ENABLE;
305 buff[1]=1;
306 break;
307 case DISABLE:
308 buff[2]=ATA_SMART_DISABLE;
309 buff[1]=1;
310 break;
311 case STATUS:
312 // this command only says if SMART is working. It could be
313 // replaced with STATUS_CHECK below.
314 buff[2]=ATA_SMART_STATUS;
315 break;
316 case AUTO_OFFLINE:
317 // NOTE: According to ATAPI 4 and UP, this command is obsolete
318 // select == 241 for enable but no data transfer. Use TASK ioctl.
320 buff[2]=select;
321 break;
322 case AUTOSAVE:
323 // select == 248 for enable but no data transfer. Use TASK ioctl.
324 buff[1]=ATA_SMART_AUTOSAVE;
325 buff[2]=select;
326 break;
329 buff[1]=select;
330 break;
331 case STATUS_CHECK:
332 // This command uses HDIO_DRIVE_TASK and has different syntax than
333 // the other commands.
334 buff[1]=ATA_SMART_STATUS;
335 break;
336 default:
337 pout("Unrecognized command %d in linux_ata_command_interface()\n"
338 "Please contact " PACKAGE_BUGREPORT "\n", command);
339 errno=ENOSYS;
340 return -1;
341 }
342
343 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
344 // only ioctl() that can be used to WRITE data to the disk.
345 if (command==WRITE_LOG) {
346 unsigned char task[sizeof(ide_task_request_t)+512];
347 ide_task_request_t *reqtask=(ide_task_request_t *) task;
348 task_struct_t *taskfile=(task_struct_t *) reqtask->io_ports;
349
350 memset(task, 0, sizeof(task));
351
352 taskfile->data = 0;
354 taskfile->sector_count = 1;
355 taskfile->sector_number = select;
356 taskfile->low_cylinder = 0x4f;
357 taskfile->high_cylinder = 0xc2;
358 taskfile->device_head = 0;
359 taskfile->command = ATA_SMART_CMD;
360
361 reqtask->data_phase = TASKFILE_OUT;
362 reqtask->req_cmd = IDE_DRIVE_TASK_OUT;
363 reqtask->out_size = 512;
364 reqtask->in_size = 0;
365
366 // copy user data into the task request structure
367 memcpy(task+sizeof(ide_task_request_t), data, 512);
368
369 if (ioctl(get_fd(), HDIO_DRIVE_TASKFILE, task)) {
370 if (errno==EINVAL)
371 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASK_IOCTL set\n");
372 return -1;
373 }
374 return 0;
375 }
376
377 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
378 // one is this:
379 if (command==STATUS_CHECK || command==AUTOSAVE || command==AUTO_OFFLINE){
380 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
381 // have to read the IDE driver source code. Sigh.
382 // buff[0]: ATA COMMAND CODE REGISTER
383 // buff[1]: ATA FEATURES REGISTER
384 // buff[2]: ATA SECTOR_COUNT
385 // buff[3]: ATA SECTOR NUMBER
386 // buff[4]: ATA CYL LO REGISTER
387 // buff[5]: ATA CYL HI REGISTER
388 // buff[6]: ATA DEVICE HEAD
389
390 unsigned const char normal_lo=0x4f, normal_hi=0xc2;
391 unsigned const char failed_lo=0xf4, failed_hi=0x2c;
392 buff[4]=normal_lo;
393 buff[5]=normal_hi;
394
395 if (ioctl(get_fd(), HDIO_DRIVE_TASK, buff)) {
396 if (errno==EINVAL) {
397 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
398 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
399 }
400 else
401 syserror("Error SMART Status command failed");
402 return -1;
403 }
404
405 // Cyl low and Cyl high unchanged means "Good SMART status"
406 if (buff[4]==normal_lo && buff[5]==normal_hi)
407 return 0;
408
409 // These values mean "Bad SMART status"
410 if (buff[4]==failed_lo && buff[5]==failed_hi)
411 return 1;
412
413 // We haven't gotten output that makes sense; print out some debugging info
414 syserror("Error SMART Status command failed");
415 pout("Please get assistance from " PACKAGE_URL "\n");
416 pout("Register values returned from SMART Status command are:\n");
417 pout("ST =0x%02x\n",(int)buff[0]);
418 pout("ERR=0x%02x\n",(int)buff[1]);
419 pout("NS =0x%02x\n",(int)buff[2]);
420 pout("SC =0x%02x\n",(int)buff[3]);
421 pout("CL =0x%02x\n",(int)buff[4]);
422 pout("CH =0x%02x\n",(int)buff[5]);
423 pout("SEL=0x%02x\n",(int)buff[6]);
424 return -1;
425 }
426
427#if 1
428 // Note to people doing ports to other OSes -- don't worry about
429 // this block -- you can safely ignore it. I have put it here
430 // because under linux when you do IDENTIFY DEVICE to a packet
431 // device, it generates an ugly kernel syslog error message. This
432 // is harmless but frightens users. So this block detects packet
433 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
434 // error message.
435 //
436 // If you read only the ATA specs, it appears as if a packet device
437 // *might* respond to the IDENTIFY DEVICE command. This is
438 // misleading - it's because around the time that SFF-8020 was
439 // incorporated into the ATA-3/4 standard, the ATA authors were
440 // sloppy. See SFF-8020 and you will see that ATAPI devices have
441 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
442 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
443 if (command==IDENTIFY || command==PIDENTIFY){
444 unsigned short deviceid[256];
445 // check the device identity, as seen when the system was booted
446 // or the device was FIRST registered. This will not be current
447 // if the user has subsequently changed some of the parameters. If
448 // device is a packet device, swap the command interpretations.
449 if (!ioctl(get_fd(), HDIO_GET_IDENTITY, deviceid) && (deviceid[0] & 0x8000))
451 }
452#endif
453
454 // We are now doing the HDIO_DRIVE_CMD type ioctl.
455 if ((ioctl(get_fd(), HDIO_DRIVE_CMD, buff)))
456 return -1;
457
458 // CHECK POWER MODE command returns information in the Sector Count
459 // register (buff[3]). Copy to return data buffer.
460 if (command==CHECK_POWER_MODE)
461 buff[HDIO_DRIVE_CMD_OFFSET]=buff[2];
462
463 // if the command returns data then copy it back
464 if (copydata)
465 memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata);
466
467 return 0;
468}
469
470// >>>>>> Start of general SCSI specific linux code
471
472/* Linux specific code.
473 * Historically smartmontools (and smartsuite before it) used the
474 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
475 * nodes that use the SCSI subsystem. A better interface has been available
476 * via the SCSI generic (sg) driver but this involves the extra step of
477 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
478 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
479 * the sg driver have become available via the SG_IO ioctl which is available
480 * on all SCSI devices (on SCSI tape devices from lk 2.6.6). Now in lk 5.17
481 * the SCSI_IOCTL_SEND_COMMAND ioctl is still present but deprecated sending
482 * a warning to the log the first time (after power up) it is used. The SG_IO
483 * Version 3 interface is the most widely used (circa lk 5.17 in 2022) and is
484 * available on the primary block device name (e.g. /dev/sdc) for all SCSI
485 * disks (and tapes) including all USB attached storage and all ATA/SATA
486 * storage. */
487
488#define MAX_DXFER_LEN 1024 /* can be increased if necessary */
489#define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
490#define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
491#define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
492#define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
493#define LSCSI_DID_ERROR 0x7 /* Need to work around aacraid driver quirk */
494#define LSCSI_DRIVER_TIMEOUT 0x6
495#define LSCSI_DID_TIME_OUT 0x3
496#define LSCSI_DID_BUS_BUSY 0x2
497#define LSCSI_DID_NO_CONNECT 0x1
498
499
505};
506
508
509
510/* Preferred implementation for issuing SCSI commands in linux. This
511 * function uses the SG_IO ioctl. Return 0 if command issued successfully
512 * (various status values should still be checked). If the SCSI command
513 * cannot be issued then a negative errno value is returned. */
514static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report,
515 enum lk_sg_io_ifc_t sg_io_ifc)
516{
517 /* we are filling structures for both versions, but using only one requested */
518 struct sg_io_hdr io_hdr_v3;
519 struct sg_io_v4 io_hdr_v4;
520
521#ifdef SCSI_CDB_CHECK
522 bool ok = is_scsi_cdb(iop->cmnd, iop->cmnd_len);
523 if (! ok) {
524 int n = iop->cmnd_len;
525 const unsigned char * ucp = iop->cmnd;
526
527 pout(">>>>>>>> %s: cdb seems invalid, opcode=0x%x, len=%d, cdb:\n",
528 __func__, ((n > 0) ? ucp[0] : 0), n);
529 if (n > 0) {
530 if (n > 16)
531 pout(" <<truncating to first 16 bytes>>\n");
532 dStrHex((const uint8_t *)ucp, ((n > 16) ? 16 : n), 1);
533 }
534 }
535#endif
536
537 if (report > 0) {
538 int k, j;
539 const unsigned char * ucp = iop->cmnd;
540 const char * np;
541 char buff[256];
542 const int sz = (int)sizeof(buff);
543
544 pout(">>>> do_scsi_cmnd_io: sg_io_ifc=%d\n", (int)sg_io_ifc);
545 np = scsi_get_opcode_name(ucp);
546 j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
547 for (k = 0; k < (int)iop->cmnd_len; ++k)
548 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
549 if ((report > 1) &&
550 (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
551 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
552
553 snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
554 "data, len=%d%s:\n", (int)iop->dxfer_len,
555 (trunc ? " [only first 256 bytes shown]" : ""));
556 dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1);
557 }
558 else
559 snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
560 pout("%s", buff);
561 }
562 memset(&io_hdr_v3, 0, sizeof(struct sg_io_hdr));
563 memset(&io_hdr_v4, 0, sizeof(struct sg_io_v4));
564
565 io_hdr_v3.interface_id = 'S';
566 io_hdr_v3.cmd_len = iop->cmnd_len;
567 io_hdr_v3.mx_sb_len = iop->max_sense_len;
568 io_hdr_v3.dxfer_len = iop->dxfer_len;
569 io_hdr_v3.dxferp = iop->dxferp;
570 io_hdr_v3.cmdp = iop->cmnd;
571 io_hdr_v3.sbp = iop->sensep;
572 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
573 defaults to 60 seconds. */
574 io_hdr_v3.timeout = ((0 == iop->timeout) ? 60 : iop->timeout) * 1000;
575
576 io_hdr_v4.guard = 'Q';
577 io_hdr_v4.request_len = iop->cmnd_len;
578 io_hdr_v4.request = __u64(iop->cmnd);
579 io_hdr_v4.max_response_len = iop->max_sense_len;
580 io_hdr_v4.response = __u64(iop->sensep);
581 io_hdr_v4.timeout = ((0 == iop->timeout) ? 60 : iop->timeout) * 1000; // msec
582
583 switch (iop->dxfer_dir) {
584 case DXFER_NONE:
585 io_hdr_v3.dxfer_direction = SG_DXFER_NONE;
586 break;
588 io_hdr_v3.dxfer_direction = SG_DXFER_FROM_DEV;
589 io_hdr_v4.din_xfer_len = iop->dxfer_len;
590 io_hdr_v4.din_xferp = __u64(iop->dxferp);
591 break;
592 case DXFER_TO_DEVICE:
593 io_hdr_v3.dxfer_direction = SG_DXFER_TO_DEV;
594 io_hdr_v4.dout_xfer_len = iop->dxfer_len;
595 io_hdr_v4.dout_xferp = __u64(iop->dxferp);
596 break;
597 default:
598 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
599 return -EINVAL;
600 }
601
602 iop->resp_sense_len = 0;
603 iop->scsi_status = 0;
604 iop->resid = 0;
605
606 void * io_hdr = NULL;
607
608 switch (sg_io_ifc) {
609 case SG_IO_USE_V3:
610 io_hdr = &io_hdr_v3;
611 break;
612 case SG_IO_USE_V4:
613 io_hdr = &io_hdr_v4;
614 break;
615 default:
616 // should never be reached
617 errno = EOPNOTSUPP;
618 return -errno;
619 }
620
621 if (ioctl(dev_fd, SG_IO, io_hdr) < 0) {
622 if (report)
623 pout(" SG_IO ioctl failed, errno=%d [%s], SG_IO_V%d\n", errno,
624 strerror(errno), (int)sg_io_ifc);
625 return -errno;
626 }
627
628 unsigned int sg_driver_status = 0, sg_transport_status = 0, sg_info = 0,
629 sg_duration = 0;
630
631 if (sg_io_ifc == SG_IO_USE_V3) {
632 iop->resid = io_hdr_v3.resid;
633 iop->scsi_status = io_hdr_v3.status;
634 sg_driver_status = io_hdr_v3.driver_status;
635 sg_transport_status = io_hdr_v3.host_status;
636 sg_info = io_hdr_v3.info;
637 iop->resp_sense_len = io_hdr_v3.sb_len_wr;
638 sg_duration = io_hdr_v3.duration;
639 }
640
641 if (sg_io_ifc == SG_IO_USE_V4) {
642 switch (iop->dxfer_dir) {
643 case DXFER_NONE:
644 iop->resid = 0;
645 break;
647 iop->resid = io_hdr_v4.din_resid;
648 break;
649 case DXFER_TO_DEVICE:
650 iop->resid = io_hdr_v4.dout_resid;
651 break;
652 }
653 iop->scsi_status = io_hdr_v4.device_status;
654 sg_driver_status = io_hdr_v4.driver_status;
655 sg_transport_status = io_hdr_v4.transport_status;
656 sg_info = io_hdr_v4.info;
657 iop->resp_sense_len = io_hdr_v4.response_len;
658 sg_duration = io_hdr_v4.duration;
659 }
660
661 if (report > 0) {
662 pout(" scsi_status=0x%x, sg_transport_status=0x%x, sg_driver_status=0x%x\n"
663 " sg_info=0x%x sg_duration=%d milliseconds resid=%d\n", iop->scsi_status,
664 sg_transport_status, sg_driver_status, sg_info,
665 sg_duration, iop->resid);
666
667 if (report > 1) {
668 if (DXFER_FROM_DEVICE == iop->dxfer_dir) {
669 int trunc, len;
670
671 len = iop->dxfer_len - iop->resid;
672 trunc = (len > 256) ? 1 : 0;
673 if (len > 0) {
674 pout(" Incoming data, len=%d%s:\n", len,
675 (trunc ? " [only first 256 bytes shown]" : ""));
676 dStrHex(iop->dxferp, (trunc ? 256 : len), 1);
677 } else
678 pout(" Incoming data trimmed to nothing by resid\n");
679 }
680 }
681 }
682
683 if (sg_info & SG_INFO_CHECK) { /* error or warning */
684 int masked_driver_status = (LSCSI_DRIVER_MASK & sg_driver_status);
685
686 if (0 != sg_transport_status) {
687 if ((LSCSI_DID_NO_CONNECT == sg_transport_status) ||
688 (LSCSI_DID_BUS_BUSY == sg_transport_status) ||
689 (LSCSI_DID_TIME_OUT == sg_transport_status))
690 return -ETIMEDOUT;
691 else
692 /* Check for DID_ERROR - workaround for aacraid driver quirk */
693 if (LSCSI_DID_ERROR != sg_transport_status) {
694 return -EIO; /* catch all if not DID_ERR */
695 }
696 }
697 if (0 != masked_driver_status) {
698 if (LSCSI_DRIVER_TIMEOUT == masked_driver_status)
699 return -ETIMEDOUT;
700 else if (LSCSI_DRIVER_SENSE != masked_driver_status)
701 return -EIO;
702 }
703 if (LSCSI_DRIVER_SENSE == masked_driver_status)
706 iop->sensep && (iop->resp_sense_len > 0)) {
707 if (report > 1) {
708 pout(" >>> Sense buffer, len=%d:\n",
709 (int)iop->resp_sense_len);
710 dStrHex(iop->sensep, iop->resp_sense_len , 1);
711 }
712 }
713 if (report) {
714 if (SCSI_STATUS_CHECK_CONDITION == iop->scsi_status && iop->sensep) {
715 if ((iop->sensep[0] & 0x7f) > 0x71)
716 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
717 iop->scsi_status, iop->sensep[1] & 0xf,
718 iop->sensep[2], iop->sensep[3]);
719 else
720 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
721 iop->scsi_status, iop->sensep[2] & 0xf,
722 iop->sensep[12], iop->sensep[13]);
723 }
724 else
725 pout(" status=0x%x\n", iop->scsi_status);
726 }
727 }
728 return 0;
729}
730
731/* SCSI command transmission interface function, linux version.
732 * Returns 0 if SCSI command successfully launched and response
733 * received. Even when 0 is returned the caller should check
734 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
735 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
736 * (e.g. device not present or timeout) or some other problem
737 * (e.g. timeout) then returns a negative errno value */
738static int do_normal_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop,
739 int report)
740{
741 int res;
742
743 /* implementation relies on static sg_io_interface variable. If not
744 * previously set tries the SG_IO ioctl. If that succeeds assume
745 * that SG_IO ioctl functional. If it fails with an errno value
746 * other than ENODEV (no device) or a permissions problem then
747 * assume the SG_IO_USE_V3 interface. */
748 switch (sg_io_interface) {
749 case SG_IO_USE_DETECT:
750 /* ignore report argument */
751 /* Try SG_IO V3 first */
752 if (0 == (res = sg_io_cmnd_io(dev_fd, iop, report, SG_IO_USE_V3))) {
754 return 0;
755 } else if ((-ENODEV == res) || (-EACCES == res) || (-EPERM == res))
756 return res; /* wait until we see a device */
757 /* See if we can use SG_IO V4 * */
758 if (0 == (res = sg_io_cmnd_io(dev_fd, iop, report, SG_IO_USE_V4))) {
760 return 0;
761 } else if ((-ENODEV == res) || (-EACCES == res) || (-EPERM == res))
762 return res; /* wait until we see a device */
764 /* FALLTHRU */
765 case SG_IO_UNSUPP:
766 /* previously called SCSI_IOCTL_SEND_COMMAND ioctl which has now
767 * been removed. The SG_IO_USE_V3 is most widely used now in Linux
768 * (circa 2022), try it again. */
770 /* FALLTHRU */
771 case SG_IO_USE_V3:
772 case SG_IO_USE_V4:
773 /* use SG_IO V3 or V4 ioctl, depending on availabiliy */
774 return sg_io_cmnd_io(dev_fd, iop, report, sg_io_interface);
775 default:
776 pout(">>>> do_scsi_cmnd_io: bad sg_io_interface=%d\n",
777 (int)sg_io_interface);
779 return -EIO; /* report error and reset state */
780 }
781}
782
783// >>>>>> End of general SCSI specific linux code
784
785/////////////////////////////////////////////////////////////////////////////
786/// Standard SCSI support
787
789: public /*implements*/ scsi_device,
790 public /*extends*/ linux_smart_device
791{
792public:
793 linux_scsi_device(smart_interface * intf, const char * dev_name,
794 const char * req_type, bool scanning = false);
795
796 virtual smart_device * autodetect_open() override;
797
798 virtual bool scsi_pass_through(scsi_cmnd_io * iop) override;
799
800private:
801 bool m_scanning; ///< true if created within scan_smart_devices
802};
803
805 const char * dev_name, const char * req_type, bool scanning /*= false*/)
806: smart_device(intf, dev_name, "scsi", req_type),
807 // If opened with O_RDWR, a SATA disk in standby mode
808 // may spin-up after device close().
809 linux_smart_device(O_RDONLY | O_NONBLOCK),
810 m_scanning(scanning)
811{
812}
813
815{
816 int status = do_normal_scsi_cmnd_io(get_fd(), iop, scsi_debugmode);
817 if (status < 0)
818 return set_err(-status);
819 return true;
820}
821
822/////////////////////////////////////////////////////////////////////////////
823/// PMC AacRAID support
824
826:public scsi_device,
827 public /*extends */ linux_smart_device
828{
829public:
830 linux_aacraid_device(smart_interface *intf, const char *dev_name,
831 unsigned int host, unsigned int channel, unsigned int device);
832
833 virtual ~linux_aacraid_device();
834
835 virtual bool open() override;
836
837 virtual bool scsi_pass_through(scsi_cmnd_io *iop) override;
838
839private:
840 //Device Host number
841 int aHost;
842
843 //Channel(Lun) of the device
844 int aLun;
845
846 //Id of the device
847 int aId;
848
849};
850
852 const char *dev_name, unsigned int host, unsigned int channel, unsigned int device)
853 : smart_device(intf,dev_name,"aacraid","aacraid"),
854 linux_smart_device(O_RDWR|O_NONBLOCK),
855 aHost(host), aLun(channel), aId(device)
856{
857 set_info().info_name = strprintf("%s [aacraid_disk_%02d_%02d_%d]",dev_name,aHost,aLun,aId);
858 set_info().dev_type = strprintf("aacraid,%d,%d,%d",aHost,aLun,aId);
859}
860
862{
863}
864
866{
867 //Create the character device name based on the host number
868 //Required for get stats from disks connected to different controllers
869 char dev_name[128];
870 snprintf(dev_name, sizeof(dev_name), "/dev/aac%d", aHost);
871
872 //Initial open of dev name to check if it exists
873 int afd = ::open(dev_name,O_RDWR);
874
875 if(afd < 0 && errno == ENOENT) {
876
877 FILE *fp = fopen("/proc/devices","r");
878 if(NULL == fp)
879 return set_err(errno,"cannot open /proc/devices:%s",
880 strerror(errno));
881
882 char line[256];
883 int mjr = -1;
884
885 while(fgets(line,sizeof(line),fp) !=NULL) {
886 int nc = -1;
887 if(sscanf(line,"%d aac%n",&mjr,&nc) == 1
888 && nc > 0 && '\n' == line[nc])
889 break;
890 mjr = -1;
891 }
892
893 //work with /proc/devices is done
894 fclose(fp);
895
896 if (mjr < 0)
897 return set_err(ENOENT, "aac entry not found in /proc/devices");
898
899 //Create misc device file in /dev/ used for communication with driver
900 if(mknod(dev_name, S_IFCHR|0600, makedev(mjr,aHost)))
901 return set_err(errno,"cannot create %s:%s",dev_name,strerror(errno));
902
903 afd = ::open(dev_name,O_RDWR);
904 }
905
906 if(afd < 0)
907 return set_err(errno,"cannot open %s:%s",dev_name,strerror(errno));
908
909 set_fd(afd);
910 return true;
911}
912
914{
915 int report = scsi_debugmode;
916
917 if (report > 0) {
918 int k, j;
919 const unsigned char * ucp = iop->cmnd;
920 const char * np;
921 char buff[256];
922 const int sz = (int)sizeof(buff);
923
924 np = scsi_get_opcode_name(ucp);
925 j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
926 for (k = 0; k < (int)iop->cmnd_len; ++k)
927 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
928 if ((report > 1) &&
929 (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
930 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
931
932 snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
933 "data, len=%d%s:\n", (int)iop->dxfer_len,
934 (trunc ? " [only first 256 bytes shown]" : ""));
935 dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1);
936 }
937 else
938 snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
939
940 pout("%s", buff);
941 }
942
943
944 //return test commands
945 if (iop->cmnd[0] == 0x00)
946 return true;
947
948 user_aac_reply *pReply;
949
950 #ifdef ENVIRONMENT64
951 // Create user 64 bit request
952 user_aac_srb64 *pSrb;
953 uint8_t aBuff[sizeof(user_aac_srb64) + sizeof(user_aac_reply)] = {0,};
954
955 pSrb = (user_aac_srb64*)aBuff;
956 pSrb->count = sizeof(user_aac_srb64) - sizeof(user_sgentry64);
957
958 #elif defined(ENVIRONMENT32)
959 //Create user 32 bit request
960 user_aac_srb32 *pSrb;
961 uint8_t aBuff[sizeof(user_aac_srb32) + sizeof(user_aac_reply)] = {0,};
962
963 pSrb = (user_aac_srb32*)aBuff;
964 pSrb->count = sizeof(user_aac_srb32) - sizeof(user_sgentry32);
965 #endif
966
968 //channel is 0 always
969 pSrb->channel = 0;
970 pSrb->id = aId;
971 pSrb->lun = aLun;
972 pSrb->timeout = 0;
973
974 pSrb->retry_limit = 0;
975 pSrb->cdb_size = iop->cmnd_len;
976
977 switch(iop->dxfer_dir) {
978 case DXFER_NONE:
979 pSrb->flags = SRB_NoDataXfer;
980 break;
982 pSrb->flags = SRB_DataIn;
983 break;
984 case DXFER_TO_DEVICE:
985 pSrb->flags = SRB_DataOut;
986 break;
987 default:
988 pout("aacraid: bad dxfer_dir\n");
989 return set_err(EINVAL, "aacraid: bad dxfer_dir\n");
990 }
991
992 if(iop->dxfer_len > 0) {
993
994 #ifdef ENVIRONMENT64
995 pSrb->sg64.count = 1;
996 pSrb->sg64.sg64[0].addr64.lo32 = ((intptr_t)iop->dxferp) &
997 0x00000000ffffffff;
998 pSrb->sg64.sg64[0].addr64.hi32 = ((intptr_t)iop->dxferp) >> 32;
999
1000 pSrb->sg64.sg64[0].length = (uint32_t)iop->dxfer_len;
1001 pSrb->count += pSrb->sg64.count * sizeof(user_sgentry64);
1002 #elif defined(ENVIRONMENT32)
1003 pSrb->sg32.count = 1;
1004 pSrb->sg32.sg32[0].addr32 = (intptr_t)iop->dxferp;
1005
1006 pSrb->sg32.sg32[0].length = (uint32_t)iop->dxfer_len;
1007 pSrb->count += pSrb->sg32.count * sizeof(user_sgentry32);
1008 #endif
1009
1010 }
1011
1012 pReply = (user_aac_reply*)(aBuff+pSrb->count);
1013
1014 memcpy(pSrb->cdb,iop->cmnd,iop->cmnd_len);
1015
1016 int rc = 0;
1017 errno = 0;
1018 rc = ioctl(get_fd(),FSACTL_SEND_RAW_SRB,pSrb);
1019
1020 if (rc != 0)
1021 return set_err(errno, "aacraid send_raw_srb: %d.%d = %s",
1022 aLun, aId, strerror(errno));
1023
1024/* see kernel aacraid.h and MSDN SCSI_REQUEST_BLOCK documentation */
1025#define SRB_STATUS_SUCCESS 0x1
1026#define SRB_STATUS_ERROR 0x4
1027#define SRB_STATUS_NO_DEVICE 0x08
1028#define SRB_STATUS_SELECTION_TIMEOUT 0x0a
1029#define SRB_STATUS_AUTOSENSE_VALID 0x80
1030
1031 iop->scsi_status = pReply->scsi_status;
1032
1035 memcpy(iop->sensep, pReply->sense_data, pReply->sense_data_size);
1036 iop->resp_sense_len = pReply->sense_data_size;
1037 return true; /* request completed with sense data */
1038 }
1039
1040 switch (pReply->srb_status & 0x3f) {
1041
1042 case SRB_STATUS_SUCCESS:
1043 return true; /* request completed successfully */
1044
1046 return set_err(EIO, "aacraid: Device %d %d does not exist", aLun, aId);
1047
1049 return set_err(EIO, "aacraid: Device %d %d not responding", aLun, aId);
1050
1051 default:
1052 return set_err(EIO, "aacraid result: %d.%d = 0x%x",
1053 aLun, aId, pReply->srb_status);
1054 }
1055}
1056
1057
1058/////////////////////////////////////////////////////////////////////////////
1059/// LSI MegaRAID support
1060
1062: public /* implements */ scsi_device,
1063 public /* extends */ linux_smart_device
1064{
1065public:
1066 linux_megaraid_device(smart_interface *intf, const char *name,
1067 unsigned int tgt);
1068
1069 virtual ~linux_megaraid_device();
1070
1071 virtual smart_device * autodetect_open() override;
1072
1073 virtual bool open() override;
1074 virtual bool close() override;
1075
1076 virtual bool scsi_pass_through(scsi_cmnd_io *iop) override;
1077
1078private:
1079 unsigned int m_disknum;
1080 unsigned int m_hba;
1081 int m_fd;
1082
1083 bool (linux_megaraid_device::*pt_cmd)(int cdblen, void *cdb, int dataLen, void *data,
1084 int senseLen, void *sense, int report, int direction);
1085 bool megasas_cmd(int cdbLen, void *cdb, int dataLen, void *data,
1086 int senseLen, void *sense, int report, int direction);
1087 bool megadev_cmd(int cdbLen, void *cdb, int dataLen, void *data,
1088 int senseLen, void *sense, int report, int direction);
1089};
1090
1092 const char *dev_name, unsigned int tgt)
1093 : smart_device(intf, dev_name, "megaraid", "megaraid"),
1094 linux_smart_device(O_RDWR | O_NONBLOCK),
1095 m_disknum(tgt), m_hba(0),
1096 m_fd(-1), pt_cmd(0)
1097{
1098 set_info().info_name = strprintf("%s [megaraid_disk_%02d]", dev_name, m_disknum);
1099 set_info().dev_type = strprintf("megaraid,%d", tgt);
1100}
1101
1103{
1104 if (m_fd >= 0)
1105 ::close(m_fd);
1106}
1107
1109{
1110 int report = scsi_debugmode;
1111
1112 // Open device
1113 if (!open())
1114 return this;
1115
1116 // The code below is based on smartd.cpp:SCSIFilterKnown()
1117 if (strcmp(get_req_type(), "megaraid"))
1118 return this;
1119
1120 // Get INQUIRY
1121 unsigned char req_buff[64] = {0, };
1122 int req_len = 36;
1123 if (scsiStdInquiry(this, req_buff, req_len)) {
1124 close();
1125 set_err(EIO, "INQUIRY failed");
1126 return this;
1127 }
1128
1129 int avail_len = req_buff[4] + 5;
1130 int len = (avail_len < req_len ? avail_len : req_len);
1131 if (len < 36)
1132 return this;
1133
1134 if (report)
1135 pout("Got MegaRAID inquiry.. %s\n", req_buff+8);
1136
1137 // Use INQUIRY to detect type
1138 {
1139 // SAT?
1140 ata_device * newdev = smi()->autodetect_sat_device(this, req_buff, len);
1141 if (newdev) // NOTE: 'this' is now owned by '*newdev'
1142 return newdev;
1143 }
1144
1145 // Nothing special found
1146 return this;
1147}
1148
1150{
1151 int mjr;
1152 int report = scsi_debugmode;
1153
1154 if (sscanf(get_dev_name(), "/dev/bus/%u", &m_hba) == 0) {
1156 return false;
1157 /* Get device HBA */
1158 struct sg_scsi_id sgid;
1159 if (ioctl(get_fd(), SG_GET_SCSI_ID, &sgid) == 0) {
1160 m_hba = sgid.host_no;
1161 }
1162 else if (ioctl(get_fd(), SCSI_IOCTL_GET_BUS_NUMBER, &m_hba) != 0) {
1163 int err = errno;
1165 return set_err(err, "can't get bus number");
1166 } // we don't need this device anymore
1168 }
1169 /* Perform mknod of device ioctl node */
1170 FILE * fp = fopen("/proc/devices", "r");
1171 if (fp) {
1172 char line[128];
1173 while (fgets(line, sizeof(line), fp) != NULL) {
1174 int n1 = 0;
1175 if (sscanf(line, "%d megaraid_sas_ioctl%n", &mjr, &n1) == 1 && n1 == 22) {
1176 n1=mknod("/dev/megaraid_sas_ioctl_node", S_IFCHR|0600, makedev(mjr, 0));
1177 if(report > 0)
1178 pout("Creating /dev/megaraid_sas_ioctl_node = %d\n", n1 >= 0 ? 0 : errno);
1179 if (n1 >= 0 || errno == EEXIST)
1180 break;
1181 }
1182 else if (sscanf(line, "%d megadev%n", &mjr, &n1) == 1 && n1 == 11) {
1183 n1=mknod("/dev/megadev0", S_IFCHR|0600, makedev(mjr, 0));
1184 if(report > 0)
1185 pout("Creating /dev/megadev0 = %d\n", n1 >= 0 ? 0 : errno);
1186 if (n1 >= 0 || errno == EEXIST)
1187 break;
1188 }
1189 }
1190 fclose(fp);
1191 }
1192
1193 /* Open Device IOCTL node */
1194 if ((m_fd = ::open("/dev/megaraid_sas_ioctl_node", O_RDWR)) >= 0) {
1196 }
1197 else if ((m_fd = ::open("/dev/megadev0", O_RDWR)) >= 0) {
1199 }
1200 else {
1201 int err = errno;
1203 return set_err(err, "cannot open /dev/megaraid_sas_ioctl_node or /dev/megadev0");
1204 }
1205 set_fd(m_fd);
1206 return true;
1207}
1208
1210{
1211 if (m_fd >= 0)
1212 ::close(m_fd);
1213 m_fd = -1; m_hba = 0; pt_cmd = 0;
1214 set_fd(m_fd);
1215 return true;
1216}
1217
1219{
1220 int report = scsi_debugmode;
1221
1222 if (report > 0) {
1223 int k, j;
1224 const unsigned char * ucp = iop->cmnd;
1225 const char * np;
1226 char buff[256];
1227 const int sz = (int)sizeof(buff);
1228
1229 np = scsi_get_opcode_name(ucp);
1230 j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
1231 for (k = 0; k < (int)iop->cmnd_len; ++k)
1232 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
1233 if ((report > 1) &&
1234 (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
1235 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
1236
1237 snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
1238 "data, len=%d%s:\n", (int)iop->dxfer_len,
1239 (trunc ? " [only first 256 bytes shown]" : ""));
1240 dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1);
1241 }
1242 else
1243 snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
1244 pout("%s", buff);
1245 }
1246
1247 // Controller rejects Test Unit Ready
1248 if (iop->cmnd[0] == 0x00)
1249 return true;
1250
1251 if (iop->cmnd[0] == SAT_ATA_PASSTHROUGH_12 || iop->cmnd[0] == SAT_ATA_PASSTHROUGH_16) {
1252 // Controller does not return ATA output registers in SAT sense data
1253 if (iop->cmnd[2] & (1 << 5)) // chk_cond
1254 return set_err(ENOSYS, "ATA return descriptor not supported by controller firmware");
1255 }
1256 // SMART WRITE LOG SECTOR causing media errors
1257 if ((iop->cmnd[0] == SAT_ATA_PASSTHROUGH_16 // SAT16 WRITE LOG
1258 && iop->cmnd[14] == ATA_SMART_CMD && iop->cmnd[3]==0 && iop->cmnd[4] == ATA_SMART_WRITE_LOG_SECTOR) ||
1259 (iop->cmnd[0] == SAT_ATA_PASSTHROUGH_12 // SAT12 WRITE LOG
1260 && iop->cmnd[9] == ATA_SMART_CMD && iop->cmnd[3] == ATA_SMART_WRITE_LOG_SECTOR))
1261 {
1263 return set_err(ENOSYS, "SMART WRITE LOG SECTOR may cause problems, try with -T permissive to force");
1264 }
1265 if (pt_cmd == NULL)
1266 return false;
1267 return (this->*pt_cmd)(iop->cmnd_len, iop->cmnd,
1268 iop->dxfer_len, iop->dxferp,
1269 iop->max_sense_len, iop->sensep, report, iop->dxfer_dir);
1270}
1271
1272/* Issue passthrough scsi command to PERC5/6 controllers */
1274 int dataLen, void *data,
1275 int /*senseLen*/, void * /*sense*/, int /*report*/, int dxfer_dir)
1276{
1277 struct megasas_pthru_frame *pthru;
1278 struct megasas_iocpacket uio;
1279
1280 memset(&uio, 0, sizeof(uio));
1281 pthru = &uio.frame.pthru;
1283 pthru->cmd_status = 0xFF;
1284 pthru->scsi_status = 0x0;
1285 pthru->target_id = m_disknum;
1286 pthru->lun = 0;
1287 pthru->cdb_len = cdbLen;
1288 pthru->timeout = 0;
1289 switch (dxfer_dir) {
1290 case DXFER_NONE:
1291 pthru->flags = MFI_FRAME_DIR_NONE;
1292 break;
1293 case DXFER_FROM_DEVICE:
1294 pthru->flags = MFI_FRAME_DIR_READ;
1295 break;
1296 case DXFER_TO_DEVICE:
1297 pthru->flags = MFI_FRAME_DIR_WRITE;
1298 break;
1299 default:
1300 pout("megasas_cmd: bad dxfer_dir\n");
1301 return set_err(EINVAL, "megasas_cmd: bad dxfer_dir\n");
1302 }
1303
1304 if (dataLen > 0) {
1305 pthru->sge_count = 1;
1306 pthru->data_xfer_len = dataLen;
1307 pthru->sgl.sge32[0].phys_addr = (intptr_t)data;
1308 pthru->sgl.sge32[0].length = (uint32_t)dataLen;
1309 }
1310 memcpy(pthru->cdb, cdb, cdbLen);
1311
1312 uio.host_no = m_hba;
1313 if (dataLen > 0) {
1314 uio.sge_count = 1;
1315 uio.sgl_off = offsetof(struct megasas_pthru_frame, sgl);
1316 uio.sgl[0].iov_base = data;
1317 uio.sgl[0].iov_len = dataLen;
1318 }
1319
1320 errno = 0;
1321 int rc = ioctl(m_fd, MEGASAS_IOC_FIRMWARE, &uio);
1322 if (pthru->cmd_status || rc != 0) {
1323 if (pthru->cmd_status == 12) {
1324 return set_err(EIO, "megasas_cmd: Device %d does not exist\n", m_disknum);
1325 }
1326 return set_err((errno ? errno : EIO), "megasas_cmd result: %d.%d = %d/%d",
1327 m_hba, m_disknum, errno,
1328 pthru->cmd_status);
1329 }
1330 return true;
1331}
1332
1333/* Issue passthrough scsi commands to PERC2/3/4 controllers */
1335 int dataLen, void *data,
1336 int /*senseLen*/, void * /*sense*/, int /*report*/, int /* dir */)
1337{
1338 struct uioctl_t uio;
1339 int rc;
1340
1341 /* Don't issue to the controller */
1342 if (m_disknum == 7)
1343 return false;
1344
1345 memset(&uio, 0, sizeof(uio));
1346 uio.inlen = dataLen;
1347 uio.outlen = dataLen;
1348
1349 memset(data, 0, dataLen);
1350 uio.ui.fcs.opcode = 0x80; // M_RD_IOCTL_CMD
1351 uio.ui.fcs.adapno = MKADAP(m_hba);
1352
1353 uio.data.pointer = (uint8_t *)data;
1354
1355 uio.mbox.cmd = MEGA_MBOXCMD_PASSTHRU;
1356 uio.mbox.xferaddr = (intptr_t)&uio.pthru;
1357
1358 uio.pthru.ars = 1;
1359 uio.pthru.timeout = 2;
1360 uio.pthru.channel = 0;
1361 uio.pthru.target = m_disknum;
1362 uio.pthru.cdblen = cdbLen;
1363 uio.pthru.reqsenselen = MAX_REQ_SENSE_LEN;
1364 uio.pthru.dataxferaddr = (intptr_t)data;
1365 uio.pthru.dataxferlen = dataLen;
1366 memcpy(uio.pthru.cdb, cdb, cdbLen);
1367
1368 rc=ioctl(m_fd, MEGAIOCCMD, &uio);
1369 if (uio.pthru.scsistatus || rc != 0) {
1370 return set_err((errno ? errno : EIO), "megadev_cmd result: %d.%d = %d/%d",
1371 m_hba, m_disknum, errno,
1372 uio.pthru.scsistatus);
1373 }
1374 return true;
1375}
1376
1377/////////////////////////////////////////////////////////////////////////////
1378/// CCISS RAID support
1379
1380#ifdef HAVE_LINUX_CCISS_IOCTL_H
1381
1382class linux_cciss_device
1383: public /*implements*/ scsi_device,
1384 public /*extends*/ linux_smart_device
1385{
1386public:
1387 linux_cciss_device(smart_interface * intf, const char * name, unsigned char disknum);
1388
1389 virtual bool scsi_pass_through(scsi_cmnd_io * iop) override;
1390
1391private:
1392 unsigned char m_disknum; ///< Disk number.
1393};
1394
1395linux_cciss_device::linux_cciss_device(smart_interface * intf,
1396 const char * dev_name, unsigned char disknum)
1397: smart_device(intf, dev_name, "cciss", "cciss"),
1398 linux_smart_device(O_RDWR | O_NONBLOCK),
1399 m_disknum(disknum)
1400{
1401 set_info().info_name = strprintf("%s [cciss_disk_%02d]", dev_name, disknum);
1402}
1403
1404bool linux_cciss_device::scsi_pass_through(scsi_cmnd_io * iop)
1405{
1406 int status = cciss_io_interface(get_fd(), m_disknum, iop, scsi_debugmode);
1407 if (status < 0)
1408 return set_err(-status);
1409 return true;
1410}
1411
1412#endif // HAVE_LINUX_CCISS_IOCTL_H
1413
1414/////////////////////////////////////////////////////////////////////////////
1415/// AMCC/3ware RAID support
1416
1418: public /*implements*/ ata_device,
1419 public /*extends*/ linux_smart_device
1420{
1421public:
1428
1429 linux_escalade_device(smart_interface * intf, const char * dev_name,
1430 escalade_type_t escalade_type, int disknum);
1431
1432 virtual bool open() override;
1433
1434 virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) override;
1435
1436private:
1437 escalade_type_t m_escalade_type; ///< Controller type
1438 int m_disknum; ///< Disk number.
1439};
1440
1442 escalade_type_t escalade_type, int disknum)
1443: smart_device(intf, dev_name, "3ware", "3ware"),
1444 linux_smart_device(O_RDONLY | O_NONBLOCK),
1445 m_escalade_type(escalade_type), m_disknum(disknum)
1446{
1447 set_info().info_name = strprintf("%s [3ware_disk_%02d]", dev_name, disknum);
1448}
1449
1450/* This function will setup and fix device nodes for a 3ware controller. */
1451#define MAJOR_STRING_LENGTH 3
1452#define DEVICE_STRING_LENGTH 32
1453#define NODE_STRING_LENGTH 16
1454static int setup_3ware_nodes(const char *nodename, const char *driver_name)
1455{
1456 int tw_major = 0;
1457 int index = 0;
1458 char majorstring[MAJOR_STRING_LENGTH+1];
1459 char device_name[DEVICE_STRING_LENGTH+1];
1460 char nodestring[NODE_STRING_LENGTH];
1461 struct stat stat_buf;
1462 FILE *file;
1463 int retval = 0;
1464#ifdef HAVE_LIBSELINUX
1465 security_context_t orig_context = NULL;
1466 security_context_t node_context = NULL;
1467 int selinux_enabled = is_selinux_enabled();
1468 int selinux_enforced = security_getenforce();
1469#endif
1470
1471 /* First try to open up /proc/devices */
1472 if (!(file = fopen("/proc/devices", "r"))) {
1473 pout("Error opening /proc/devices to check/create 3ware device nodes\n");
1474 syserror("fopen");
1475 return 0; // don't fail here: user might not have /proc !
1476 }
1477
1478 /* Attempt to get device major number */
1479 while (EOF != fscanf(file, "%3s %32s", majorstring, device_name)) {
1480 majorstring[MAJOR_STRING_LENGTH]='\0';
1481 device_name[DEVICE_STRING_LENGTH]='\0';
1482 if (!strncmp(device_name, nodename, DEVICE_STRING_LENGTH)) {
1483 tw_major = atoi(majorstring);
1484 break;
1485 }
1486 }
1487 fclose(file);
1488
1489 /* See if we found a major device number */
1490 if (!tw_major) {
1491 pout("No major number for /dev/%s listed in /proc/devices. Is the %s driver loaded?\n", nodename, driver_name);
1492 return 2;
1493 }
1494#ifdef HAVE_LIBSELINUX
1495 /* Prepare a database of contexts for files in /dev
1496 * and save the current context */
1497 if (selinux_enabled) {
1498 if (matchpathcon_init_prefix(NULL, "/dev") < 0)
1499 pout("Error initializing contexts database for /dev");
1500 if (getfscreatecon(&orig_context) < 0) {
1501 pout("Error retrieving original SELinux fscreate context");
1502 if (selinux_enforced) {
1503 matchpathcon_fini();
1504 return 6;
1505 }
1506 }
1507 }
1508#endif
1509 /* Now check if nodes are correct */
1510 for (index=0; index<16; index++) {
1511 snprintf(nodestring, sizeof(nodestring), "/dev/%s%d", nodename, index);
1512#ifdef HAVE_LIBSELINUX
1513 /* Get context of the node and set it as the default */
1514 if (selinux_enabled) {
1515 if (matchpathcon(nodestring, S_IRUSR | S_IWUSR, &node_context) < 0) {
1516 pout("Could not retrieve context for %s", nodestring);
1517 if (selinux_enforced) {
1518 retval = 6;
1519 break;
1520 }
1521 }
1522 if (setfscreatecon(node_context) < 0) {
1523 pout ("Error setting default fscreate context");
1524 if (selinux_enforced) {
1525 retval = 6;
1526 break;
1527 }
1528 }
1529 }
1530#endif
1531 /* Try to stat the node */
1532 if ((stat(nodestring, &stat_buf))) {
1533 pout("Node %s does not exist and must be created. Check the udev rules.\n", nodestring);
1534 /* Create a new node if it doesn't exist */
1535 if (mknod(nodestring, S_IFCHR|0600, makedev(tw_major, index))) {
1536 pout("problem creating 3ware device nodes %s", nodestring);
1537 syserror("mknod");
1538 retval = 3;
1539 break;
1540 } else {
1541#ifdef HAVE_LIBSELINUX
1542 if (selinux_enabled && node_context) {
1543 freecon(node_context);
1544 node_context = NULL;
1545 }
1546#endif
1547 continue;
1548 }
1549 }
1550
1551 /* See if nodes major and minor numbers are correct */
1552 if ((tw_major != (int)(major(stat_buf.st_rdev))) ||
1553 (index != (int)(minor(stat_buf.st_rdev))) ||
1554 (!S_ISCHR(stat_buf.st_mode))) {
1555 pout("Node %s has wrong major/minor number and must be created anew."
1556 " Check the udev rules.\n", nodestring);
1557 /* Delete the old node */
1558 if (unlink(nodestring)) {
1559 pout("problem unlinking stale 3ware device node %s", nodestring);
1560 syserror("unlink");
1561 retval = 4;
1562 break;
1563 }
1564
1565 /* Make a new node */
1566 if (mknod(nodestring, S_IFCHR|0600, makedev(tw_major, index))) {
1567 pout("problem creating 3ware device nodes %s", nodestring);
1568 syserror("mknod");
1569 retval = 5;
1570 break;
1571 }
1572 }
1573#ifdef HAVE_LIBSELINUX
1574 if (selinux_enabled && node_context) {
1575 freecon(node_context);
1576 node_context = NULL;
1577 }
1578#endif
1579 }
1580
1581#ifdef HAVE_LIBSELINUX
1582 if (selinux_enabled) {
1583 if(setfscreatecon(orig_context) < 0) {
1584 pout("Error re-setting original fscreate context");
1585 if (selinux_enforced)
1586 retval = 6;
1587 }
1588 if(orig_context)
1589 freecon(orig_context);
1590 if(node_context)
1591 freecon(node_context);
1592 matchpathcon_fini();
1593 }
1594#endif
1595 return retval;
1596}
1597
1599{
1602 // the device nodes for these controllers are dynamically assigned,
1603 // so we need to check that they exist with the correct major
1604 // numbers and if not, create them
1605 const char * node = (m_escalade_type == AMCC_3WARE_9700_CHAR ? "twl" :
1607 "twe" );
1608 const char * driver = (m_escalade_type == AMCC_3WARE_9700_CHAR ? "3w-sas" :
1609 m_escalade_type == AMCC_3WARE_9000_CHAR ? "3w-9xxx" :
1610 "3w-xxxx" );
1611 if (setup_3ware_nodes(node, driver))
1612 return set_err((errno ? errno : ENXIO), "setup_3ware_nodes(\"%s\", \"%s\") failed", node, driver);
1613 }
1614 // Continue with default open
1615 return linux_smart_device::open();
1616}
1617
1618// TODO: Function no longer useful
1619//void printwarning(smart_command_set command);
1620
1621#ifndef SCSI_IOCTL_SEND_COMMAND
1622#define SCSI_IOCTL_SEND_COMMAND 1
1623#endif
1624
1625// PURPOSE
1626// This is an interface routine meant to isolate the OS dependent
1627// parts of the code, and to provide a debugging interface. Each
1628// different port and OS needs to provide it's own interface. This
1629// is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
1630// commands to be passed through the SCSI driver.
1631// DETAILED DESCRIPTION OF ARGUMENTS
1632// fd: is the file descriptor provided by open()
1633// disknum is the disk number (0 to 15) in the RAID array
1634// escalade_type indicates the type of controller type, and if scsi or char interface is used
1635// command: defines the different operations.
1636// select: additional input data if needed (which log, which type of
1637// self-test).
1638// data: location to write output data, if needed (512 bytes).
1639// Note: not all commands use all arguments.
1640// RETURN VALUES
1641// -1 if the command failed
1642// 0 if the command succeeded,
1643// STATUS_CHECK routine:
1644// -1 if the command failed
1645// 0 if the command succeeded and disk SMART status is "OK"
1646// 1 if the command succeeded and disk SMART status is "FAILING"
1647
1648/* 512 is the max payload size: increase if needed */
1649#define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
1650#define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
1651#define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
1652#define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
1653
1655{
1656 if (!ata_cmd_is_ok(in,
1657 true, // data_out_support
1658 false, // TODO: multi_sector_support
1659 true) // ata_48bit_support
1660 )
1661 return false;
1662
1663 // Used by both the SCSI and char interfaces
1664 TW_Passthru *passthru=NULL;
1665 char ioctl_buffer[TW_IOCTL_BUFFER_SIZE];
1666
1667 // only used for SCSI device interface
1668 TW_Ioctl *tw_ioctl=NULL;
1669 TW_Output *tw_output=NULL;
1670
1671 // only used for 6000/7000/8000 char device interface
1672 TW_New_Ioctl *tw_ioctl_char=NULL;
1673
1674 // only used for 9000 character device interface
1675 TW_Ioctl_Buf_Apache *tw_ioctl_apache=NULL;
1676
1677 memset(ioctl_buffer, 0, TW_IOCTL_BUFFER_SIZE);
1678
1679 // TODO: Handle controller differences by different classes
1681 tw_ioctl_apache = (TW_Ioctl_Buf_Apache *)ioctl_buffer;
1683 tw_ioctl_apache->driver_command.buffer_length = 512; /* payload size */
1684 passthru = (TW_Passthru *)&(tw_ioctl_apache->firmware_command.command.oldcommand);
1685 }
1687 tw_ioctl_char = (TW_New_Ioctl *)ioctl_buffer;
1688 tw_ioctl_char->data_buffer_length = 512;
1689 passthru = (TW_Passthru *)&(tw_ioctl_char->firmware_command);
1690 }
1691 else if (m_escalade_type==AMCC_3WARE_678K) {
1692 tw_ioctl = (TW_Ioctl *)ioctl_buffer;
1693 tw_ioctl->cdb[0] = TW_IOCTL;
1694 tw_ioctl->opcode = TW_ATA_PASSTHRU;
1695 tw_ioctl->input_length = 512; // correct even for non-data commands
1696 tw_ioctl->output_length = 512; // correct even for non-data commands
1697 tw_output = (TW_Output *)tw_ioctl;
1698 passthru = (TW_Passthru *)&(tw_ioctl->input_data);
1699 }
1700 else {
1701 return set_err(ENOSYS,
1702 "Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1703 "Please contact " PACKAGE_BUGREPORT "\n", (int)m_escalade_type, m_disknum);
1704 }
1705
1706 // Same for (almost) all commands - but some reset below
1707 passthru->byte0.opcode = TW_OP_ATA_PASSTHRU;
1708 passthru->request_id = 0xFF;
1709 passthru->unit = m_disknum;
1710 passthru->status = 0;
1711 passthru->flags = 0x1;
1712
1713 // Set registers
1714 {
1715 const ata_in_regs_48bit & r = in.in_regs;
1716 passthru->features = r.features_16;
1717 passthru->sector_count = r.sector_count_16;
1718 passthru->sector_num = r.lba_low_16;
1719 passthru->cylinder_lo = r.lba_mid_16;
1720 passthru->cylinder_hi = r.lba_high_16;
1721 passthru->drive_head = r.device;
1722 passthru->command = r.command;
1723 }
1724
1725 // Is this a command that reads or returns 512 bytes?
1726 // passthru->param values are:
1727 // 0x0 - non data command without TFR write check,
1728 // 0x8 - non data command with TFR write check,
1729 // 0xD - data command that returns data to host from device
1730 // 0xF - data command that writes data from host to device
1731 // passthru->size values are 0x5 for non-data and 0x07 for data
1732 bool readdata = false;
1733 if (in.direction == ata_cmd_in::data_in) {
1734 readdata=true;
1735 passthru->byte0.sgloff = 0x5;
1736 passthru->size = 0x7; // TODO: Other value for multi-sector ?
1737 passthru->param = 0xD;
1738 // For 64-bit to work correctly, up the size of the command packet
1739 // in dwords by 1 to account for the 64-bit single sgl 'address'
1740 // field. Note that this doesn't agree with the typedefs but it's
1741 // right (agree with kernel driver behavior/typedefs).
1743 && sizeof(long) == 8)
1744 passthru->size++;
1745 }
1746 else if (in.direction == ata_cmd_in::no_data) {
1747 // Non data command -- but doesn't use large sector
1748 // count register values.
1749 passthru->byte0.sgloff = 0x0;
1750 passthru->size = 0x5;
1751 passthru->param = 0x8;
1752 passthru->sector_count = 0x0;
1753 }
1754 else if (in.direction == ata_cmd_in::data_out) {
1756 memcpy(tw_ioctl_apache->data_buffer, in.buffer, in.size);
1758 memcpy(tw_ioctl_char->data_buffer, in.buffer, in.size);
1759 else {
1760 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1761 // memcpy(tw_output->output_data, data, 512);
1762 // printwarning(command); // TODO: Parameter no longer valid
1763 return set_err(ENOTSUP, "DATA OUT not supported for this 3ware controller type");
1764 }
1765 passthru->byte0.sgloff = 0x5;
1766 passthru->size = 0x7; // TODO: Other value for multi-sector ?
1767 passthru->param = 0xF; // PIO data write
1769 && sizeof(long) == 8)
1770 passthru->size++;
1771 }
1772 else
1773 return set_err(EINVAL);
1774
1775 // Now send the command down through an ioctl()
1776 int ioctlreturn;
1778 ioctlreturn=ioctl(get_fd(), TW_IOCTL_FIRMWARE_PASS_THROUGH, tw_ioctl_apache);
1780 ioctlreturn=ioctl(get_fd(), TW_CMD_PACKET_WITH_DATA, tw_ioctl_char);
1781 else
1782 ioctlreturn=ioctl(get_fd(), SCSI_IOCTL_SEND_COMMAND, tw_ioctl);
1783
1784 // Deal with the different error cases
1785 if (ioctlreturn) {
1790 && in.in_regs.lba_low) {
1791 // error here is probably a kernel driver whose version is too old
1792 // printwarning(command); // TODO: Parameter no longer valid
1793 return set_err(ENOTSUP, "Probably kernel driver too old");
1794 }
1795 return set_err(EIO);
1796 }
1797
1798 // The passthru structure is valid after return from an ioctl if:
1799 // - we are using the character interface OR
1800 // - we are using the SCSI interface and this is a NON-READ-DATA command
1801 // For SCSI interface, note that we set passthru to a different
1802 // value after ioctl().
1804 if (readdata)
1805 passthru=NULL;
1806 else
1807 passthru=(TW_Passthru *)&(tw_output->output_data);
1808 }
1809
1810 // See if the ATA command failed. Now that we have returned from
1811 // the ioctl() call, if passthru is valid, then:
1812 // - passthru->status contains the 3ware controller STATUS
1813 // - passthru->command contains the ATA STATUS register
1814 // - passthru->features contains the ATA ERROR register
1815 //
1816 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1817 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1818 // While we *might* decode the ATA ERROR register, at the moment it
1819 // doesn't make much sense: we don't care in detail why the error
1820 // happened.
1821
1822 if (passthru && (passthru->status || (passthru->command & 0x21))) {
1823 return set_err(EIO);
1824 }
1825
1826 // If this is a read data command, copy data to output buffer
1827 if (readdata) {
1829 memcpy(in.buffer, tw_ioctl_apache->data_buffer, in.size);
1831 memcpy(in.buffer, tw_ioctl_char->data_buffer, in.size);
1832 else
1833 memcpy(in.buffer, tw_output->output_data, in.size);
1834 }
1835
1836 // Return register values
1837 if (passthru) {
1838 ata_out_regs_48bit & r = out.out_regs;
1839 r.error = passthru->features;
1840 r.sector_count_16 = passthru->sector_count;
1841 r.lba_low_16 = passthru->sector_num;
1842 r.lba_mid_16 = passthru->cylinder_lo;
1843 r.lba_high_16 = passthru->cylinder_hi;
1844 r.device = passthru->drive_head;
1845 r.status = passthru->command;
1846 }
1847
1848 // look for nonexistent devices/ports
1850 && !nonempty(in.buffer, in.size)) {
1851 return set_err(ENODEV, "No drive on port %d", m_disknum);
1852 }
1853
1854 return true;
1855}
1856
1857/////////////////////////////////////////////////////////////////////////////
1858/// Areca RAID support
1859
1860///////////////////////////////////////////////////////////////////
1861// SATA(ATA) device behind Areca RAID Controller
1863: public /*implements*/ areca_ata_device,
1864 public /*extends*/ linux_smart_device
1865{
1866public:
1867 linux_areca_ata_device(smart_interface * intf, const char * dev_name, int disknum, int encnum = 1);
1868 virtual smart_device * autodetect_open() override;
1869 virtual bool arcmsr_lock() override;
1870 virtual bool arcmsr_unlock() override;
1871 virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io * iop) override;
1872};
1873
1874///////////////////////////////////////////////////////////////////
1875// SAS(SCSI) device behind Areca RAID Controller
1877: public /*implements*/ areca_scsi_device,
1878 public /*extends*/ linux_smart_device
1879{
1880public:
1881 linux_areca_scsi_device(smart_interface * intf, const char * dev_name, int disknum, int encnum = 1);
1882 virtual smart_device * autodetect_open() override;
1883 virtual bool arcmsr_lock() override;
1884 virtual bool arcmsr_unlock() override;
1885 virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io * iop) override;
1886};
1887
1888// Looks in /proc/scsi to suggest correct areca devices
1890{
1891 const char* proc_format_string="host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n";
1892
1893 // check data formwat
1894 FILE *fp=fopen("/proc/scsi/sg/device_hdr", "r");
1895 if (!fp) {
1896 pout("Unable to open /proc/scsi/sg/device_hdr for reading\n");
1897 return 1;
1898 }
1899
1900 // get line, compare to format
1901 char linebuf[256];
1902 linebuf[255]='\0';
1903 char *out = fgets(linebuf, 256, fp);
1904 fclose(fp);
1905 if (!out) {
1906 pout("Unable to read contents of /proc/scsi/sg/device_hdr\n");
1907 return 2;
1908 }
1909
1910 if (strcmp(linebuf, proc_format_string)) {
1911 // wrong format!
1912 // Fix this by comparing only tokens not white space!!
1913 pout("Unexpected format %s in /proc/scsi/sg/device_hdr\n", proc_format_string);
1914 return 3;
1915 }
1916
1917 // Format is understood, now search for correct device
1918 fp=fopen("/proc/scsi/sg/devices", "r");
1919 if (!fp) return 1;
1920 int host, chan, id, lun, type, opens, qdepth, busy, online;
1921 int dev=-1;
1922 // search all lines of /proc/scsi/sg/devices
1923 while (9 == fscanf(fp, "%d %d %d %d %d %d %d %d %d", &host, &chan, &id, &lun, &type, &opens, &qdepth, &busy, &online)) {
1924 dev++;
1925 if (id == 16 && type == 3) {
1926 // devices with id=16 and type=3 might be Areca controllers
1927 pout("Device /dev/sg%d appears to be an Areca controller.\n", dev);
1928 }
1929 }
1930 fclose(fp);
1931 return 0;
1932}
1933
1934// Areca RAID Controller(SATA Disk)
1935linux_areca_ata_device::linux_areca_ata_device(smart_interface * intf, const char * dev_name, int disknum, int encnum)
1936: smart_device(intf, dev_name, "areca", "areca"),
1937 linux_smart_device(O_RDWR | O_EXCL | O_NONBLOCK)
1938{
1939 set_disknum(disknum);
1940 set_encnum(encnum);
1941 set_info().info_name = strprintf("%s [areca_disk#%02d_enc#%02d]", dev_name, disknum, encnum);
1942}
1943
1945{
1946 // autodetect device type
1948 if(is_ata < 0)
1949 {
1950 set_err(EIO);
1951 return this;
1952 }
1953
1954 if(is_ata == 1)
1955 {
1956 // SATA device
1957 return this;
1958 }
1959
1960 // SAS device
1962 close();
1963 delete this;
1964 newdev->open(); // TODO: Can possibly pass open fd
1965
1966 return newdev.release();
1967}
1968
1970{
1971 int ioctlreturn = 0;
1972
1973 if(!is_open()) {
1974 if(!open()){
1976 }
1977 }
1978
1979 ioctlreturn = do_normal_scsi_cmnd_io(get_fd(), iop, scsi_debugmode);
1980 if ( ioctlreturn || iop->scsi_status )
1981 {
1982 // errors found
1983 return -1;
1984 }
1985
1986 return ioctlreturn;
1987}
1988
1990{
1991 return true;
1992}
1993
1995{
1996 return true;
1997}
1998
1999// Areca RAID Controller(SAS Device)
2000linux_areca_scsi_device::linux_areca_scsi_device(smart_interface * intf, const char * dev_name, int disknum, int encnum)
2001: smart_device(intf, dev_name, "areca", "areca"),
2002 linux_smart_device(O_RDWR | O_EXCL | O_NONBLOCK)
2003{
2004 set_disknum(disknum);
2005 set_encnum(encnum);
2006 set_info().info_name = strprintf("%s [areca_disk#%02d_enc#%02d]", dev_name, disknum, encnum);
2007}
2008
2010{
2011 return this;
2012}
2013
2015{
2016 int ioctlreturn = 0;
2017
2018 if(!is_open()) {
2019 if(!open()){
2021 }
2022 }
2023
2024 ioctlreturn = do_normal_scsi_cmnd_io(get_fd(), iop, scsi_debugmode);
2025 if ( ioctlreturn || iop->scsi_status )
2026 {
2027 // errors found
2028 return -1;
2029 }
2030
2031 return ioctlreturn;
2032}
2033
2035{
2036 return true;
2037}
2038
2040{
2041 return true;
2042}
2043
2044/////////////////////////////////////////////////////////////////////////////
2045/// Marvell support
2046
2048: public /*implements*/ ata_device_with_command_set,
2049 public /*extends*/ linux_smart_device
2050{
2051public:
2052 linux_marvell_device(smart_interface * intf, const char * dev_name, const char * req_type);
2053
2054protected:
2055 virtual int ata_command_interface(smart_command_set command, int select, char * data);
2056};
2057
2059 const char * dev_name, const char * req_type)
2060: smart_device(intf, dev_name, "marvell", req_type),
2061 linux_smart_device(O_RDONLY | O_NONBLOCK)
2062{
2063}
2064
2066{
2067 typedef struct {
2068 int inlen;
2069 int outlen;
2070 char cmd[540];
2071 } mvsata_scsi_cmd;
2072
2073 int copydata = 0;
2074 mvsata_scsi_cmd smart_command;
2075 unsigned char *buff = (unsigned char *)&smart_command.cmd[6];
2076 // See struct hd_drive_cmd_hdr in hdreg.h
2077 // buff[0]: ATA COMMAND CODE REGISTER
2078 // buff[1]: ATA SECTOR NUMBER REGISTER
2079 // buff[2]: ATA FEATURES REGISTER
2080 // buff[3]: ATA SECTOR COUNT REGISTER
2081
2082 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
2083 memset(&smart_command, 0, sizeof(smart_command));
2084 smart_command.inlen = 540;
2085 smart_command.outlen = 540;
2086 smart_command.cmd[0] = 0xC; //Vendor-specific code
2087 smart_command.cmd[4] = 6; //command length
2088
2089 buff[0] = ATA_SMART_CMD;
2090 switch (command){
2091 case CHECK_POWER_MODE:
2092 buff[0]=ATA_CHECK_POWER_MODE;
2093 break;
2094 case READ_VALUES:
2095 buff[2]=ATA_SMART_READ_VALUES;
2096 copydata=buff[3]=1;
2097 break;
2098 case READ_THRESHOLDS:
2100 copydata=buff[1]=buff[3]=1;
2101 break;
2102 case READ_LOG:
2104 buff[1]=select;
2105 copydata=buff[3]=1;
2106 break;
2107 case IDENTIFY:
2108 buff[0]=ATA_IDENTIFY_DEVICE;
2109 copydata=buff[3]=1;
2110 break;
2111 case PIDENTIFY:
2113 copydata=buff[3]=1;
2114 break;
2115 case ENABLE:
2116 buff[2]=ATA_SMART_ENABLE;
2117 buff[1]=1;
2118 break;
2119 case DISABLE:
2120 buff[2]=ATA_SMART_DISABLE;
2121 buff[1]=1;
2122 break;
2123 case STATUS:
2124 case STATUS_CHECK:
2125 // this command only says if SMART is working. It could be
2126 // replaced with STATUS_CHECK below.
2127 buff[2] = ATA_SMART_STATUS;
2128 break;
2129 case AUTO_OFFLINE:
2130 buff[2]=ATA_SMART_AUTO_OFFLINE;
2131 buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!!
2132 break;
2133 case AUTOSAVE:
2134 buff[2]=ATA_SMART_AUTOSAVE;
2135 buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!!
2136 break;
2137 case IMMEDIATE_OFFLINE:
2139 buff[1]=select;
2140 break;
2141 default:
2142 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command);
2143 errno = EINVAL;
2144 return -1;
2145 }
2146 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
2147 // one is this:
2148 // We are now doing the HDIO_DRIVE_CMD type ioctl.
2149 if (ioctl(get_fd(), SCSI_IOCTL_SEND_COMMAND, (void *)&smart_command))
2150 return -1;
2151
2152 if (command==CHECK_POWER_MODE) {
2153 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
2154 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
2155 data[0]=buff[3];
2156 return 0;
2157 }
2158
2159 // Always succeed on a SMART status, as a disk that failed returned
2160 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
2161 if (command == STATUS)
2162 return 0;
2163 //Data returned is starting from 0 offset
2164 if (command == STATUS_CHECK)
2165 {
2166 // Cyl low and Cyl high unchanged means "Good SMART status"
2167 if (buff[4] == 0x4F && buff[5] == 0xC2)
2168 return 0;
2169 // These values mean "Bad SMART status"
2170 if (buff[4] == 0xF4 && buff[5] == 0x2C)
2171 return 1;
2172 // We haven't gotten output that makes sense; print out some debugging info
2173 syserror("Error SMART Status command failed");
2174 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT);
2175 pout("Register values returned from SMART Status command are:\n");
2176 pout("CMD =0x%02x\n",(int)buff[0]);
2177 pout("FR =0x%02x\n",(int)buff[1]);
2178 pout("NS =0x%02x\n",(int)buff[2]);
2179 pout("SC =0x%02x\n",(int)buff[3]);
2180 pout("CL =0x%02x\n",(int)buff[4]);
2181 pout("CH =0x%02x\n",(int)buff[5]);
2182 pout("SEL=0x%02x\n",(int)buff[6]);
2183 return -1;
2184 }
2185
2186 if (copydata)
2187 memcpy(data, buff, 512);
2188 return 0;
2189}
2190
2191/////////////////////////////////////////////////////////////////////////////
2192/// Highpoint RAID support
2193
2195: public /*implements*/ ata_device_with_command_set,
2196 public /*extends*/ linux_smart_device
2197{
2198public:
2199 linux_highpoint_device(smart_interface * intf, const char * dev_name,
2200 unsigned char controller, unsigned char channel, unsigned char port);
2201
2202protected:
2203 virtual int ata_command_interface(smart_command_set command, int select, char * data);
2204
2205private:
2206 unsigned char m_hpt_data[3]; ///< controller/channel/port
2207};
2208
2210 unsigned char controller, unsigned char channel, unsigned char port)
2211: smart_device(intf, dev_name, "hpt", "hpt"),
2212 linux_smart_device(O_RDONLY | O_NONBLOCK)
2213{
2214 m_hpt_data[0] = controller; m_hpt_data[1] = channel; m_hpt_data[2] = port;
2215 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]);
2216}
2217
2218// this implementation is derived from ata_command_interface with a header
2219// packing for highpoint linux driver ioctl interface
2220//
2221// ioctl(fd,HPTIO_CTL,buff)
2222// ^^^^^^^^^
2223//
2224// structure of hpt_buff
2225// +----+----+----+----+--------------------.....---------------------+
2226// | 1 | 2 | 3 | 4 | 5 |
2227// +----+----+----+----+--------------------.....---------------------+
2228//
2229// 1: The target controller [ int ( 4 Bytes ) ]
2230// 2: The channel of the target controllee [ int ( 4 Bytes ) ]
2231// 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
2232// available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
2233// 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
2234// if no pmport device, set to 1 or leave blank
2235// 5: data [ void * ( var leangth ) ]
2236//
2237#define STRANGE_BUFFER_LENGTH (4+512*0xf8)
2238
2240{
2241 unsigned char hpt_buff[4*sizeof(int) + STRANGE_BUFFER_LENGTH];
2242 unsigned int *hpt = (unsigned int *)hpt_buff;
2243 unsigned char *buff = &hpt_buff[4*sizeof(int)];
2244 int copydata = 0;
2245 const int HDIO_DRIVE_CMD_OFFSET = 4;
2246
2247 memset(hpt_buff, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH);
2248 hpt[0] = m_hpt_data[0]; // controller id
2249 hpt[1] = m_hpt_data[1]; // channel number
2250 hpt[3] = m_hpt_data[2]; // pmport number
2251
2252 buff[0]=ATA_SMART_CMD;
2253 switch (command){
2254 case CHECK_POWER_MODE:
2255 buff[0]=ATA_CHECK_POWER_MODE;
2256 copydata=1;
2257 break;
2258 case READ_VALUES:
2259 buff[2]=ATA_SMART_READ_VALUES;
2260 buff[3]=1;
2261 copydata=512;
2262 break;
2263 case READ_THRESHOLDS:
2265 buff[1]=buff[3]=1;
2266 copydata=512;
2267 break;
2268 case READ_LOG:
2270 buff[1]=select;
2271 buff[3]=1;
2272 copydata=512;
2273 break;
2274 case WRITE_LOG:
2275 break;
2276 case IDENTIFY:
2277 buff[0]=ATA_IDENTIFY_DEVICE;
2278 buff[3]=1;
2279 copydata=512;
2280 break;
2281 case PIDENTIFY:
2283 buff[3]=1;
2284 copydata=512;
2285 break;
2286 case ENABLE:
2287 buff[2]=ATA_SMART_ENABLE;
2288 buff[1]=1;
2289 break;
2290 case DISABLE:
2291 buff[2]=ATA_SMART_DISABLE;
2292 buff[1]=1;
2293 break;
2294 case STATUS:
2295 buff[2]=ATA_SMART_STATUS;
2296 break;
2297 case AUTO_OFFLINE:
2298 buff[2]=ATA_SMART_AUTO_OFFLINE;
2299 buff[3]=select;
2300 break;
2301 case AUTOSAVE:
2302 buff[2]=ATA_SMART_AUTOSAVE;
2303 buff[3]=select;
2304 break;
2305 case IMMEDIATE_OFFLINE:
2307 buff[1]=select;
2308 break;
2309 case STATUS_CHECK:
2310 buff[1]=ATA_SMART_STATUS;
2311 break;
2312 default:
2313 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
2314 "Please contact " PACKAGE_BUGREPORT "\n", command);
2315 errno=ENOSYS;
2316 return -1;
2317 }
2318
2319 if (command==WRITE_LOG) {
2320 unsigned char task[4*sizeof(int)+sizeof(ide_task_request_t)+512];
2321 unsigned int *hpt_tf = (unsigned int *)task;
2322 ide_task_request_t *reqtask = (ide_task_request_t *)(&task[4*sizeof(int)]);
2323 task_struct_t *taskfile = (task_struct_t *)reqtask->io_ports;
2324
2325 memset(task, 0, sizeof(task));
2326
2327 hpt_tf[0] = m_hpt_data[0]; // controller id
2328 hpt_tf[1] = m_hpt_data[1]; // channel number
2329 hpt_tf[3] = m_hpt_data[2]; // pmport number
2330 hpt_tf[2] = HDIO_DRIVE_TASKFILE; // real hd ioctl
2331
2332 taskfile->data = 0;
2334 taskfile->sector_count = 1;
2335 taskfile->sector_number = select;
2336 taskfile->low_cylinder = 0x4f;
2337 taskfile->high_cylinder = 0xc2;
2338 taskfile->device_head = 0;
2339 taskfile->command = ATA_SMART_CMD;
2340
2341 reqtask->data_phase = TASKFILE_OUT;
2342 reqtask->req_cmd = IDE_DRIVE_TASK_OUT;
2343 reqtask->out_size = 512;
2344 reqtask->in_size = 0;
2345
2346 memcpy(task+sizeof(ide_task_request_t)+4*sizeof(int), data, 512);
2347
2348 if (ioctl(get_fd(), HPTIO_CTL, task))
2349 return -1;
2350
2351 return 0;
2352 }
2353
2354 if (command==STATUS_CHECK){
2355 unsigned const char normal_lo=0x4f, normal_hi=0xc2;
2356 unsigned const char failed_lo=0xf4, failed_hi=0x2c;
2357 buff[4]=normal_lo;
2358 buff[5]=normal_hi;
2359
2360 hpt[2] = HDIO_DRIVE_TASK;
2361
2362 if (ioctl(get_fd(), HPTIO_CTL, hpt_buff))
2363 return -1;
2364
2365 if (buff[4]==normal_lo && buff[5]==normal_hi)
2366 return 0;
2367
2368 if (buff[4]==failed_lo && buff[5]==failed_hi)
2369 return 1;
2370
2371 syserror("Error SMART Status command failed");
2372 pout("Please get assistance from " PACKAGE_URL "\n");
2373 pout("Register values returned from SMART Status command are:\n");
2374 pout("CMD=0x%02x\n",(int)buff[0]);
2375 pout("FR =0x%02x\n",(int)buff[1]);
2376 pout("NS =0x%02x\n",(int)buff[2]);
2377 pout("SC =0x%02x\n",(int)buff[3]);
2378 pout("CL =0x%02x\n",(int)buff[4]);
2379 pout("CH =0x%02x\n",(int)buff[5]);
2380 pout("SEL=0x%02x\n",(int)buff[6]);
2381 return -1;
2382 }
2383
2384#if 1
2385 if (command==IDENTIFY || command==PIDENTIFY) {
2386 unsigned char deviceid[4*sizeof(int)+512*sizeof(char)];
2387 unsigned int *hpt_id = (unsigned int *)deviceid;
2388
2389 hpt_id[0] = m_hpt_data[0]; // controller id
2390 hpt_id[1] = m_hpt_data[1]; // channel number
2391 hpt_id[3] = m_hpt_data[2]; // pmport number
2392
2393 hpt_id[2] = HDIO_GET_IDENTITY;
2394 if (!ioctl(get_fd(), HPTIO_CTL, deviceid) && (deviceid[4*sizeof(int)] & 0x8000))
2396 }
2397#endif
2398
2399 hpt[2] = HDIO_DRIVE_CMD;
2400 if ((ioctl(get_fd(), HPTIO_CTL, hpt_buff)))
2401 return -1;
2402
2403 if (command==CHECK_POWER_MODE)
2404 buff[HDIO_DRIVE_CMD_OFFSET]=buff[2];
2405
2406 if (copydata)
2407 memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata);
2408
2409 return 0;
2410}
2411
2412#if 0 // TODO: Migrate from 'smart_command_set' to 'ata_in_regs' OR remove the function
2413// Utility function for printing warnings
2414void printwarning(smart_command_set command){
2415 static int printed[4]={0,0,0,0};
2416 const char* message=
2417 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
2418 "applying a simple 3w-xxxx driver patch that can be found here:\n"
2419 PACKAGE_URL "\n"
2420 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
2421
2422 if (command==AUTO_OFFLINE && !printed[0]) {
2423 printed[0]=1;
2424 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message);
2425 }
2426 else if (command==AUTOSAVE && !printed[1]) {
2427 printed[1]=1;
2428 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message);
2429 }
2430 else if (command==STATUS_CHECK && !printed[2]) {
2431 printed[2]=1;
2432 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message);
2433 }
2434 else if (command==WRITE_LOG && !printed[3]) {
2435 printed[3]=1;
2436 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
2437 }
2438
2439 return;
2440}
2441#endif
2442
2443/////////////////////////////////////////////////////////////////////////////
2444/// SCSI open with autodetection support
2445
2447{
2448 // Open device
2449 if (!open())
2450 return this;
2451
2452 // No Autodetection if device type was specified by user
2453 bool sat_only = false;
2454 if (*get_req_type()) {
2455 // Detect SAT if device object was created by scan_smart_devices().
2456 if (!(m_scanning && !strcmp(get_req_type(), "sat")))
2457 return this;
2458 sat_only = true;
2459 }
2460
2461 // The code below is based on smartd.cpp:SCSIFilterKnown()
2462
2463 // Get INQUIRY
2464 unsigned char req_buff[64] = {0, };
2465 int req_len = 36;
2466 if (scsiStdInquiry(this, req_buff, req_len)) {
2467 // Marvell controllers fail on a 36 bytes StdInquiry, but 64 suffices
2468 // watch this spot ... other devices could lock up here
2469 req_len = 64;
2470 if (scsiStdInquiry(this, req_buff, req_len)) {
2471 // device doesn't like INQUIRY commands
2472 close();
2473 set_err(EIO, "INQUIRY failed");
2474 return this;
2475 }
2476 }
2477
2478 int avail_len = req_buff[4] + 5;
2479 int len = (avail_len < req_len ? avail_len : req_len);
2480 if (len < 36) {
2481 if (sat_only) {
2482 close();
2483 set_err(EIO, "INQUIRY too short for SAT");
2484 }
2485 return this;
2486 }
2487
2488 // Use INQUIRY to detect type
2489 if (!sat_only) {
2490
2491 // 3ware ?
2492 if (!memcmp(req_buff + 8, "3ware", 5) || !memcmp(req_buff + 8, "AMCC", 4)) {
2493 close();
2494 set_err(EINVAL, "AMCC/3ware controller, please try adding '-d 3ware,N',\n"
2495 "you may need to replace %s with /dev/twlN, /dev/twaN or /dev/tweN", get_dev_name());
2496 return this;
2497 }
2498
2499 // DELL?
2500 if (!memcmp(req_buff + 8, "DELL PERC", 12) || !memcmp(req_buff + 8, "MegaRAID", 8)
2501 || !memcmp(req_buff + 16, "PERC ", 5) || !memcmp(req_buff + 8, "LSI\0",4)
2502 ) {
2503 close();
2504 set_err(EINVAL, "DELL or MegaRaid controller, please try adding '-d megaraid,N'");
2505 return this;
2506 }
2507
2508 // Marvell ?
2509 if (len >= 42 && !memcmp(req_buff + 36, "MVSATA", 6)) {
2510 //pout("Device %s: using '-d marvell' for ATA disk with Marvell driver\n", get_dev_name());
2511 close();
2512 smart_device_auto_ptr newdev(
2514 );
2515 newdev->open(); // TODO: Can possibly pass open fd
2516 delete this;
2517 return newdev.release();
2518 }
2519 }
2520
2521 // SAT or USB ?
2522 {
2523 smart_device * newdev = smi()->autodetect_sat_device(this, req_buff, len);
2524 if (newdev)
2525 // NOTE: 'this' is now owned by '*newdev'
2526 return newdev;
2527 }
2528
2529 // Nothing special found
2530
2531 if (sat_only) {
2532 close();
2533 set_err(EIO, "Not a SAT device");
2534 }
2535 return this;
2536}
2537
2538/////////////////////////////////////////////////////////////////////////////
2539/// NVMe support
2540
2542: public /*implements*/ nvme_device,
2543 public /*extends*/ linux_smart_device
2544{
2545public:
2546 linux_nvme_device(smart_interface * intf, const char * dev_name,
2547 const char * req_type, unsigned nsid);
2548
2549 virtual bool open() override;
2550
2551 virtual bool nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out) override;
2552};
2553
2555 const char * req_type, unsigned nsid)
2556: smart_device(intf, dev_name, "nvme", req_type),
2558 linux_smart_device(O_RDONLY | O_NONBLOCK)
2559{
2560}
2561
2563{
2565 return false;
2566
2567 if (!get_nsid()) {
2568 // Use actual NSID (/dev/nvmeXnN) if available,
2569 // else use broadcast namespace (/dev/nvmeX)
2570 int nsid = ioctl(get_fd(), NVME_IOCTL_ID, (void*)0);
2571 set_nsid(nsid);
2572 }
2573
2574 return true;
2575}
2576
2578{
2580 memset(&pt, 0, sizeof(pt));
2581
2582 pt.opcode = in.opcode;
2583 pt.nsid = in.nsid;
2584 pt.addr = (uint64_t)in.buffer;
2585 pt.data_len = in.size;
2586 pt.cdw10 = in.cdw10;
2587 pt.cdw11 = in.cdw11;
2588 pt.cdw12 = in.cdw12;
2589 pt.cdw13 = in.cdw13;
2590 pt.cdw14 = in.cdw14;
2591 pt.cdw15 = in.cdw15;
2592 // Kernel default for NVMe admin commands is 60 seconds
2593 // pt.timeout_ms = 60 * 1000;
2594
2595 int status = ioctl(get_fd(), NVME_IOCTL_ADMIN_CMD, &pt);
2596
2597 if (status < 0)
2598 return set_err(errno, "NVME_IOCTL_ADMIN_CMD: %s", strerror(errno));
2599
2600 if (status > 0)
2601 return set_nvme_err(out, status);
2602
2603 out.result = pt.result;
2604 return true;
2605}
2606
2607
2608//////////////////////////////////////////////////////////////////////
2609// USB bridge ID detection
2610
2611// Read USB ID from /sys file
2612static bool read_id(const std::string & path, unsigned short & id)
2613{
2614 FILE * f = fopen(path.c_str(), "r");
2615 if (!f)
2616 return false;
2617 int n = -1;
2618 bool ok = (fscanf(f, "%hx%n", &id, &n) == 1 && n == 4);
2619 fclose(f);
2620 return ok;
2621}
2622
2623// Get USB bridge ID for "sdX" or "sgN"
2624static bool get_usb_id(const char * name, unsigned short & vendor_id,
2625 unsigned short & product_id, unsigned short & version)
2626{
2627 // Only "sdX" or "sgN" supported
2628 if (!(name[0] == 's' && (name[1] == 'd' || name[1] == 'g') && !strchr(name, '/')))
2629 return false;
2630
2631 // Start search at dir referenced by symlink
2632 // "/sys/block/sdX/device" or
2633 // "/sys/class/scsi_generic/sgN"
2634 // -> "/sys/devices/.../usb*/.../host*/target*/..."
2635 std::string dir = strprintf("/sys/%s/%s%s",
2636 (name[1] == 'd' ? "block" : "class/scsi_generic"), name,
2637 (name[1] == 'd' ? "/device" : ""));
2638
2639 // Stop search at "/sys/devices"
2640 struct stat st;
2641 if (stat("/sys/devices", &st))
2642 return false;
2643 ino_t stop_ino = st.st_ino;
2644
2645 // Search in parent directories until "idVendor" is found,
2646 // fail if "/sys/devices" reached or too many iterations
2647 int cnt = 0;
2648 do {
2649 dir += "/..";
2650 if (!(++cnt < 10 && !stat(dir.c_str(), &st) && st.st_ino != stop_ino))
2651 return false;
2652 } while (access((dir + "/idVendor").c_str(), 0));
2653
2654 if (scsi_debugmode > 1) {
2655 pout("Found idVendor in: %s\n", dir.c_str());
2656 char * p = realpath(dir.c_str(), (char *)0);
2657 if (p) {
2658 pout(" realpath: %s\n", p);
2659 free(p);
2660 }
2661 }
2662
2663 // Read IDs
2664 if (!( read_id(dir + "/idVendor", vendor_id)
2665 && read_id(dir + "/idProduct", product_id)
2666 && read_id(dir + "/bcdDevice", version) ))
2667 return false;
2668
2669 if (scsi_debugmode > 1)
2670 pout("USB ID = 0x%04x:0x%04x (0x%03x)\n", vendor_id, product_id, version);
2671 return true;
2672}
2673
2674//////////////////////////////////////////////////////////////////////
2675/// Linux interface
2676
2678: public /*implements*/ smart_interface
2679{
2680public:
2681 virtual std::string get_os_version_str() override;
2682
2683 virtual std::string get_app_examples(const char * appname) override;
2684
2685 virtual bool scan_smart_devices(smart_device_list & devlist,
2686 const smart_devtype_list & types, const char * pattern = 0) override;
2687
2688protected:
2689 virtual ata_device * get_ata_device(const char * name, const char * type) override;
2690
2691 virtual scsi_device * get_scsi_device(const char * name, const char * type) override;
2692
2693 virtual nvme_device * get_nvme_device(const char * name, const char * type,
2694 unsigned nsid) override;
2695
2696 virtual smart_device * autodetect_smart_device(const char * name) override;
2697
2698 virtual smart_device * get_custom_smart_device(const char * name, const char * type) override;
2699
2700 virtual std::string get_valid_custom_dev_types_str() override;
2701
2702private:
2703 static constexpr int devxy_to_n_max = 701; // "/dev/sdzz"
2704 static int devxy_to_n(const char * name, bool debug);
2705
2706 void get_dev_list(smart_device_list & devlist, const char * pattern,
2707 bool scan_scsi, bool (* p_dev_sdxy_seen)[devxy_to_n_max+1],
2708 bool scan_nvme, const char * req_type, bool autodetect);
2709
2710 bool get_dev_megasas(smart_device_list & devlist);
2711 smart_device * missing_option(const char * opt);
2712 int megasas_dcmd_cmd(int bus_no, uint32_t opcode, void *buf,
2713 size_t bufsize, uint8_t *mbox, size_t mboxlen, uint8_t *statusp);
2714 int megasas_pd_add_list(int bus_no, smart_device_list & devlist);
2715};
2716
2718{
2719 struct utsname u;
2720 if (!uname(&u))
2721 return strprintf("%s-linux-%s", u.machine, u.release);
2722 else
2723 return SMARTMONTOOLS_BUILD_HOST;
2724}
2725
2726std::string linux_smart_interface::get_app_examples(const char * appname)
2727{
2728 if (!strcmp(appname, "smartctl"))
2729 return smartctl_examples;
2730 return "";
2731}
2732
2733// "/dev/sdXY" -> 0-devxy_to_n_max
2734// "/dev/disk/by-id/NAME" -> "../../sdXY" -> 0-devxy_to_n_max
2735// Other -> -1
2736int linux_smart_interface::devxy_to_n(const char * name, bool debug)
2737{
2738 const char * xy;
2739 char dest[256];
2740 if (str_starts_with(name, "/dev/sd")) {
2741 // Assume "/dev/sdXY"
2742 xy = name + sizeof("/dev/sd") - 1;
2743 }
2744 else {
2745 // Assume "/dev/disk/by-id/NAME", check link target
2746 int sz = readlink(name, dest, sizeof(dest)-1);
2747 if (!(0 < sz && sz < (int)sizeof(dest)))
2748 return -1;
2749 dest[sz] = 0;
2750 if (!str_starts_with(dest, "../../sd"))
2751 return -1;
2752 if (debug)
2753 pout("%s -> %s\n", name, dest);
2754 xy = dest + sizeof("../../sd") - 1;
2755 }
2756
2757 char x = xy[0];
2758 if (!('a' <= x && x <= 'z'))
2759 return -1;
2760 char y = xy[1];
2761 if (!y)
2762 // "[a-z]" -> 0-25
2763 return x - 'a';
2764
2765 if (!('a' <= y && y <= 'z' && !xy[2]))
2766 return -1;
2767 // "[a-z][a-z]" -> 26-701
2768 STATIC_ASSERT((('z' - 'a' + 1) * ('z' - 'a' + 1) + ('z' - 'a')) == devxy_to_n_max);
2769 return (x - 'a' + 1) * ('z' - 'a' + 1) + (y - 'a');
2770}
2771
2773 const char * pattern, bool scan_scsi, bool (* p_dev_sdxy_seen)[devxy_to_n_max+1],
2774 bool scan_nvme, const char * req_type, bool autodetect)
2775{
2776 bool debug = (ata_debugmode || scsi_debugmode || nvme_debugmode);
2777
2778 // Use glob to look for any directory entries matching the pattern
2779 glob_t globbuf;
2780 memset(&globbuf, 0, sizeof(globbuf));
2781 int retglob = glob(pattern, GLOB_ERR, NULL, &globbuf);
2782 if (retglob) {
2783 // glob failed: free memory and return
2784 globfree(&globbuf);
2785
2786 if (debug)
2787 pout("glob(3) error %d for pattern %s\n", retglob, pattern);
2788
2789 if (retglob == GLOB_NOSPACE)
2790 throw std::bad_alloc();
2791 return;
2792 }
2793
2794 // did we find too many paths?
2795 const int max_pathc = 1024;
2796 int n = (int)globbuf.gl_pathc;
2797 if (n > max_pathc) {
2798 pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n",
2799 n, max_pathc, pattern, n - max_pathc);
2800 n = max_pathc;
2801 }
2802
2803 // now step through the list returned by glob.
2804 for (int i = 0; i < n; i++) {
2805 const char * name = globbuf.gl_pathv[i];
2806
2807 if (p_dev_sdxy_seen) {
2808 // Follow "/dev/disk/by-id/*" symlink and check for duplicate "/dev/sdXY"
2809 int dev_n = devxy_to_n(name, debug);
2810 if (!(0 <= dev_n && dev_n <= devxy_to_n_max))
2811 continue;
2812 if ((*p_dev_sdxy_seen)[dev_n]) {
2813 if (debug)
2814 pout("%s: duplicate, ignored\n", name);
2815 continue;
2816 }
2817 (*p_dev_sdxy_seen)[dev_n] = true;
2818 }
2819
2820 smart_device * dev;
2821 if (autodetect) {
2822 dev = autodetect_smart_device(name);
2823 if (!dev)
2824 continue;
2825 }
2826 else if (scan_scsi)
2827 dev = new linux_scsi_device(this, name, req_type, true /*scanning*/);
2828 else if (scan_nvme)
2829 dev = new linux_nvme_device(this, name, req_type, 0 /* use default nsid */);
2830 else
2831 dev = new linux_ata_device(this, name, req_type);
2832 devlist.push_back(dev);
2833 }
2834
2835 // free memory
2836 globfree(&globbuf);
2837}
2838
2839// getting devices from LSI SAS MegaRaid, if available
2841{
2842 /* Scanning of disks on MegaRaid device */
2843 /* Perform mknod of device ioctl node */
2844 int mjr, n1;
2845 char line[128];
2846 bool scan_megasas = false;
2847 FILE * fp = fopen("/proc/devices", "r");
2848 if (!fp)
2849 return false;
2850 while (fgets(line, sizeof(line), fp) != NULL) {
2851 n1=0;
2852 if (sscanf(line, "%d megaraid_sas_ioctl%n", &mjr, &n1) == 1 && n1 == 22) {
2853 scan_megasas = true;
2854 n1=mknod("/dev/megaraid_sas_ioctl_node", S_IFCHR|0600, makedev(mjr, 0));
2855 if(scsi_debugmode > 0)
2856 pout("Creating /dev/megaraid_sas_ioctl_node = %d\n", n1 >= 0 ? 0 : errno);
2857 if (n1 >= 0 || errno == EEXIST)
2858 break;
2859 }
2860 }
2861 fclose(fp);
2862
2863 if(!scan_megasas)
2864 return false;
2865
2866 // getting bus numbers with megasas devices
2867 // we are using sysfs to get list of all scsi hosts
2868 DIR * dp = opendir ("/sys/class/scsi_host/");
2869 if (dp != NULL)
2870 {
2871 struct dirent *ep;
2872 while ((ep = readdir (dp)) != NULL) {
2873 unsigned int host_no = 0;
2874 if (!sscanf(ep->d_name, "host%u", &host_no))
2875 continue;
2876 /* proc_name should be megaraid_sas */
2877 char sysfsdir[256];
2878 snprintf(sysfsdir, sizeof(sysfsdir) - 1,
2879 "/sys/class/scsi_host/host%u/proc_name", host_no);
2880 if((fp = fopen(sysfsdir, "r")) == NULL)
2881 continue;
2882 if(fgets(line, sizeof(line), fp) != NULL && !strncmp(line,"megaraid_sas",12)) {
2883 megasas_pd_add_list(host_no, devlist);
2884 }
2885 fclose(fp);
2886 }
2887 (void) closedir (dp);
2888 } else { /* sysfs not mounted ? */
2889 for(unsigned i = 0; i <=16; i++) // trying to add devices on first 16 buses
2890 megasas_pd_add_list(i, devlist);
2891 }
2892 return true;
2893}
2894
2896 const smart_devtype_list & types, const char * pattern /*= 0*/)
2897{
2898 if (pattern)
2899 return set_err(EINVAL, "DEVICESCAN with pattern not implemented yet");
2900
2901 // Scan type list
2902 bool by_id = false;
2903 const char * type_ata = 0, * type_scsi = 0, * type_sat = 0, * type_nvme = 0;
2904 for (unsigned i = 0; i < types.size(); i++) {
2905 const char * type = types[i].c_str();
2906 if (!strcmp(type, "by-id"))
2907 by_id = true;
2908 else if (!strcmp(type, "ata"))
2909 type_ata = "ata";
2910 else if (!strcmp(type, "scsi"))
2911 type_scsi = "scsi";
2912 else if (!strcmp(type, "sat"))
2913 type_sat = "sat";
2914 else if (!strcmp(type, "nvme"))
2915 type_nvme = "nvme";
2916 else
2917 return set_err(EINVAL, "Invalid type '%s', valid arguments are: by-id, ata, scsi, sat, nvme",
2918 type);
2919 }
2920 // Use default if no type specified
2921 if (!(type_ata || type_scsi || type_sat || type_nvme)) {
2922 type_ata = type_scsi = type_sat = "";
2923#ifdef WITH_NVME_DEVICESCAN // TODO: Remove when NVMe support is no longer EXPERIMENTAL
2924 type_nvme = "";
2925#endif
2926 }
2927
2928 if (type_ata)
2929 get_dev_list(devlist, "/dev/hd[a-t]", false, 0, false, type_ata, false);
2930
2931 if (type_scsi || type_sat) {
2932 // "sat" detection will be later handled in linux_scsi_device::autodetect_open()
2933 const char * type_scsi_sat = ((type_scsi && type_sat) ? "" // detect both
2934 : (type_scsi ? type_scsi : type_sat));
2935 bool autodetect = !*type_scsi_sat; // If no type specified, detect USB also
2936
2937 bool dev_sdxy_seen[devxy_to_n_max+1] = {false, };
2938 bool (*p_dev_sdxy_seen)[devxy_to_n_max+1] = 0;
2939 if (by_id) {
2940 // Scan unique symlinks first
2941 get_dev_list(devlist, "/dev/disk/by-id/*", true, &dev_sdxy_seen, false,
2942 type_scsi_sat, autodetect);
2943 p_dev_sdxy_seen = &dev_sdxy_seen; // Check for duplicates below
2944 }
2945
2946 get_dev_list(devlist, "/dev/sd[a-z]", true, p_dev_sdxy_seen, false, type_scsi_sat, autodetect);
2947 get_dev_list(devlist, "/dev/sd[a-z][a-z]", true, p_dev_sdxy_seen, false, type_scsi_sat, autodetect);
2948
2949 // get device list from the megaraid device
2950 get_dev_megasas(devlist);
2951 }
2952
2953 if (type_nvme) {
2954 get_dev_list(devlist, "/dev/nvme[0-9]", false, 0, true, type_nvme, false);
2955 get_dev_list(devlist, "/dev/nvme[1-9][0-9]", false, 0, true, type_nvme, false);
2956 }
2957
2958 return true;
2959}
2960
2961ata_device * linux_smart_interface::get_ata_device(const char * name, const char * type)
2962{
2963 return new linux_ata_device(this, name, type);
2964}
2965
2966scsi_device * linux_smart_interface::get_scsi_device(const char * name, const char * type)
2967{
2968 return new linux_scsi_device(this, name, type);
2969}
2970
2971nvme_device * linux_smart_interface::get_nvme_device(const char * name, const char * type,
2972 unsigned nsid)
2973{
2974 return new linux_nvme_device(this, name, type, nsid);
2975}
2976
2978{
2979 set_err(EINVAL, "requires option '%s'", opt);
2980 return 0;
2981}
2982
2983int
2984linux_smart_interface::megasas_dcmd_cmd(int bus_no, uint32_t opcode, void *buf,
2985 size_t bufsize, uint8_t *mbox, size_t mboxlen, uint8_t *statusp)
2986{
2987 struct megasas_iocpacket ioc;
2988
2989 if ((mbox != NULL && (mboxlen == 0 || mboxlen > MFI_MBOX_SIZE)) ||
2990 (mbox == NULL && mboxlen != 0))
2991 {
2992 errno = EINVAL;
2993 return (-1);
2994 }
2995
2996 memset(&ioc, 0, sizeof(ioc));
2997 struct megasas_dcmd_frame * dcmd = &ioc.frame.dcmd;
2998 ioc.host_no = bus_no;
2999 if (mbox)
3000 memcpy(dcmd->mbox.w, mbox, mboxlen);
3002 dcmd->timeout = 0;
3003 dcmd->flags = 0;
3004 dcmd->data_xfer_len = bufsize;
3005 dcmd->opcode = opcode;
3006
3007 if (bufsize > 0) {
3008 dcmd->sge_count = 1;
3009 dcmd->data_xfer_len = bufsize;
3010 dcmd->sgl.sge32[0].phys_addr = (intptr_t)buf;
3011 dcmd->sgl.sge32[0].length = (uint32_t)bufsize;
3012 ioc.sge_count = 1;
3013 ioc.sgl_off = offsetof(struct megasas_dcmd_frame, sgl);
3014 ioc.sgl[0].iov_base = buf;
3015 ioc.sgl[0].iov_len = bufsize;
3016 }
3017
3018 int fd;
3019 if ((fd = ::open("/dev/megaraid_sas_ioctl_node", O_RDWR)) < 0) {
3020 return (errno);
3021 }
3022
3023 int r = ioctl(fd, MEGASAS_IOC_FIRMWARE, &ioc);
3024 ::close(fd);
3025 if (r < 0) {
3026 return (r);
3027 }
3028
3029 if (statusp != NULL)
3030 *statusp = dcmd->cmd_status;
3031 else if (dcmd->cmd_status != MFI_STAT_OK) {
3032 fprintf(stderr, "command %x returned error status %x\n",
3034 errno = EIO;
3035 return (-1);
3036 }
3037 return (0);
3038}
3039
3040int
3042{
3043 /*
3044 * Keep fetching the list in a loop until we have a large enough
3045 * buffer to hold the entire list.
3046 */
3047 megasas_pd_list * list = 0;
3048 for (unsigned list_size = 1024; ; ) {
3049 list = reinterpret_cast<megasas_pd_list *>(realloc(list, list_size));
3050 if (!list)
3051 throw std::bad_alloc();
3052 memset(list, 0, list_size);
3053 if (megasas_dcmd_cmd(bus_no, MFI_DCMD_PD_GET_LIST, list, list_size, NULL, 0,
3054 NULL) < 0)
3055 {
3056 free(list);
3057 return (-1);
3058 }
3059 if (list->size <= list_size)
3060 break;
3061 list_size = list->size;
3062 }
3063
3064 // adding all SCSI devices
3065 for (unsigned i = 0; i < list->count; i++) {
3066 if(list->addr[i].scsi_dev_type)
3067 continue; /* non disk device found */
3068 char line[128];
3069 snprintf(line, sizeof(line) - 1, "/dev/bus/%d", bus_no);
3070 smart_device * dev = new linux_megaraid_device(this, line, list->addr[i].device_id);
3071 devlist.push_back(dev);
3072 }
3073 free(list);
3074 return (0);
3075}
3076
3077// Return kernel release as integer ("2.6.31" -> 206031)
3078static unsigned get_kernel_release()
3079{
3080 struct utsname u;
3081 if (uname(&u))
3082 return 0;
3083 unsigned x = 0, y = 0, z = 0;
3084 if (!(sscanf(u.release, "%u.%u.%u", &x, &y, &z) == 3
3085 && x < 100 && y < 100 && z < 1000 ))
3086 return 0;
3087 return x * 100000 + y * 1000 + z;
3088}
3089
3090// Check for SCSI host proc_name "hpsa" and HPSA raid_level
3091static bool is_hpsa_in_raid_mode(const char * name)
3092{
3093 char path[128];
3094 snprintf(path, sizeof(path), "/sys/block/%s/device", name);
3095 char * syshostpath = realpath(path, (char *)0);
3096 if (!syshostpath)
3097 return false;
3098
3099 char * syshost = strrchr(syshostpath, '/');
3100 if (!syshost) {
3101 free(syshostpath);
3102 return false;
3103 }
3104
3105 char * hostsep = strchr(++syshost, ':');
3106 if (hostsep)
3107 *hostsep = 0;
3108
3109 snprintf(path, sizeof(path), "/sys/class/scsi_host/host%s/proc_name", syshost);
3110 free(syshostpath);
3111 int fd = open(path, O_RDONLY);
3112 if (fd < 0)
3113 return false;
3114
3115 char proc_name[32];
3116 ssize_t n = read(fd, proc_name, sizeof(proc_name) - 1);
3117 close(fd);
3118 if (n < 4)
3119 return false;
3120
3121 proc_name[n] = 0;
3122 if (proc_name[n - 1] == '\n')
3123 proc_name[n - 1] = 0;
3124
3125 if (scsi_debugmode > 1)
3126 pout("%s -> %s: \"%s\"\n", name, path, proc_name);
3127
3128 if (strcmp(proc_name, "hpsa"))
3129 return false;
3130
3131 // See: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/scsi/hpsa.c?id=6417f03132a6952cd17ddd8eaddbac92b61b17e0#n693
3132 snprintf(path, sizeof(path), "/sys/block/%s/device/raid_level", name);
3133 fd = open(path, O_RDONLY);
3134 if (fd < 0)
3135 return false;
3136
3137 char raid_level[4];
3138 n = read(fd, raid_level, sizeof(raid_level) - 1);
3139 close(fd);
3140 if (n < 3)
3141 return false;
3142 raid_level[n] = 0;
3143
3144 if (strcmp(raid_level, "N/A"))
3145 return true;
3146
3147 return false;
3148}
3149
3150// Guess device type (ata or scsi) based on device name (Linux
3151// specific) SCSI device name in linux can be sd, sr, scd, st, nst,
3152// osst, nosst and sg.
3154{
3155 const char * test_name = name;
3156
3157 // Dereference symlinks
3158 struct stat st;
3159 std::string pathbuf;
3160 if (!lstat(name, &st) && S_ISLNK(st.st_mode)) {
3161 char * p = realpath(name, (char *)0);
3162 if (p) {
3163 pathbuf = p;
3164 free(p);
3165 test_name = pathbuf.c_str();
3166 }
3167 }
3168
3169 // Remove the leading /dev/... if it's there
3170 static const char dev_prefix[] = "/dev/";
3171 if (str_starts_with(test_name, dev_prefix))
3172 test_name += strlen(dev_prefix);
3173
3174 // form /dev/h* or h*
3175 if (str_starts_with(test_name, "h"))
3176 return new linux_ata_device(this, name, "");
3177
3178 // form /dev/ide/* or ide/*
3179 if (str_starts_with(test_name, "ide/"))
3180 return new linux_ata_device(this, name, "");
3181
3182 // form /dev/s* or s*
3183 if (str_starts_with(test_name, "s")) {
3184
3185 // Try to detect possible USB->(S)ATA bridge
3186 unsigned short vendor_id = 0, product_id = 0, version = 0;
3187 if (get_usb_id(test_name, vendor_id, product_id, version)) {
3188 const char * usbtype = get_usb_dev_type_by_id(vendor_id, product_id, version);
3189 if (!usbtype)
3190 return 0;
3191
3192 // Kernels before 2.6.29 do not support the sense data length
3193 // required for SAT ATA PASS-THROUGH(16)
3194 if (!strcmp(usbtype, "sat") && get_kernel_release() < 206029)
3195 usbtype = "sat,12";
3196
3197 // Return SAT/USB device for this type
3198 // (Note: linux_scsi_device::autodetect_open() will not be called in this case)
3199 return get_scsi_passthrough_device(usbtype, new linux_scsi_device(this, name, ""));
3200 }
3201
3202 // Fail if hpsa driver and device is using RAID
3203 if (is_hpsa_in_raid_mode(test_name))
3204 return missing_option("-d cciss,N");
3205
3206 // No USB bridge or hpsa driver found, assume regular SCSI device
3207 return new linux_scsi_device(this, name, "");
3208 }
3209
3210 // form /dev/scsi/* or scsi/*
3211 if (str_starts_with(test_name, "scsi/"))
3212 return new linux_scsi_device(this, name, "");
3213
3214 // form /dev/bsg/* or bsg/*
3215 if (str_starts_with(test_name, "bsg/"))
3216 return new linux_scsi_device(this, name, "");
3217
3218 // form /dev/ns* or ns*
3219 if (str_starts_with(test_name, "ns"))
3220 return new linux_scsi_device(this, name, "");
3221
3222 // form /dev/os* or os*
3223 if (str_starts_with(test_name, "os"))
3224 return new linux_scsi_device(this, name, "");
3225
3226 // form /dev/nos* or nos*
3227 if (str_starts_with(test_name, "nos"))
3228 return new linux_scsi_device(this, name, "");
3229
3230 // form /dev/nvme* or nvme*
3231 if (str_starts_with(test_name, "nvme"))
3232 return new linux_nvme_device(this, name, "", 0 /* use default nsid */);
3233
3234 // form /dev/tw[ael]* or tw[ael]*
3235 if (str_starts_with(test_name, "tw") && strchr("ael", test_name[2]))
3236 return missing_option("-d 3ware,N");
3237
3238 // form /dev/cciss/* or cciss/*
3239 if (str_starts_with(test_name, "cciss/"))
3240 return missing_option("-d cciss,N");
3241
3242 // we failed to recognize any of the forms
3243 return 0;
3244}
3245
3247{
3248 // Marvell ?
3249 if (!strcmp(type, "marvell"))
3250 return new linux_marvell_device(this, name, type);
3251
3252 // 3Ware ?
3253 int disknum = -1, n1 = -1, n2 = -1;
3254 if (sscanf(type, "3ware,%n%d%n", &n1, &disknum, &n2) == 1 || n1 == 6) {
3255 if (n2 != (int)strlen(type)) {
3256 set_err(EINVAL, "Option -d 3ware,N requires N to be a non-negative integer");
3257 return 0;
3258 }
3259 if (!(0 <= disknum && disknum <= 127)) {
3260 set_err(EINVAL, "Option -d 3ware,N (N=%d) must have 0 <= N <= 127", disknum);
3261 return 0;
3262 }
3263
3264 if (!strncmp(name, "/dev/twl", 8))
3266 else if (!strncmp(name, "/dev/twa", 8))
3268 else if (!strncmp(name, "/dev/twe", 8))
3270 else
3271 return new linux_escalade_device(this, name, linux_escalade_device::AMCC_3WARE_678K, disknum);
3272 }
3273
3274 // Areca?
3275 disknum = n1 = n2 = -1;
3276 int encnum = 1;
3277 if (sscanf(type, "areca,%n%d/%d%n", &n1, &disknum, &encnum, &n2) >= 1 || n1 == 6) {
3278 if (!(1 <= disknum && disknum <= 128)) {
3279 set_err(EINVAL, "Option -d areca,N/E (N=%d) must have 1 <= N <= 128", disknum);
3280 return 0;
3281 }
3282 if (!(1 <= encnum && encnum <= 8)) {
3283 set_err(EINVAL, "Option -d areca,N/E (E=%d) must have 1 <= E <= 8", encnum);
3284 return 0;
3285 }
3286 return new linux_areca_ata_device(this, name, disknum, encnum);
3287 }
3288
3289 // Highpoint ?
3290 int controller = -1, channel = -1; disknum = 1;
3291 n1 = n2 = -1; int n3 = -1;
3292 if (sscanf(type, "hpt,%n%d/%d%n/%d%n", &n1, &controller, &channel, &n2, &disknum, &n3) >= 2 || n1 == 4) {
3293 int len = strlen(type);
3294 if (!(n2 == len || n3 == len)) {
3295 set_err(EINVAL, "Option '-d hpt,L/M/N' supports 2-3 items");
3296 return 0;
3297 }
3298 if (!(1 <= controller && controller <= 8)) {
3299 set_err(EINVAL, "Option '-d hpt,L/M/N' invalid controller id L supplied");
3300 return 0;
3301 }
3302 if (!(1 <= channel && channel <= 128)) {
3303 set_err(EINVAL, "Option '-d hpt,L/M/N' invalid channel number M supplied");
3304 return 0;
3305 }
3306 if (!(1 <= disknum && disknum <= 15)) {
3307 set_err(EINVAL, "Option '-d hpt,L/M/N' invalid pmport number N supplied");
3308 return 0;
3309 }
3310 return new linux_highpoint_device(this, name, controller, channel, disknum);
3311 }
3312
3313#ifdef HAVE_LINUX_CCISS_IOCTL_H
3314 // CCISS ?
3315 disknum = n1 = n2 = -1;
3316 if (sscanf(type, "cciss,%n%d%n", &n1, &disknum, &n2) == 1 || n1 == 6) {
3317 if (n2 != (int)strlen(type)) {
3318 set_err(EINVAL, "Option -d cciss,N requires N to be a non-negative integer");
3319 return 0;
3320 }
3321 if (!(0 <= disknum && disknum <= 127)) {
3322 set_err(EINVAL, "Option -d cciss,N (N=%d) must have 0 <= N <= 127", disknum);
3323 return 0;
3324 }
3325 return get_sat_device("sat,auto", new linux_cciss_device(this, name, disknum));
3326 }
3327#endif // HAVE_LINUX_CCISS_IOCTL_H
3328
3329 // MegaRAID ?
3330 if (sscanf(type, "megaraid,%d", &disknum) == 1) {
3331 return new linux_megaraid_device(this, name, disknum);
3332 }
3333
3334 //aacraid?
3335 unsigned host, chan, device;
3336 if (sscanf(type, "aacraid,%u,%u,%u", &host, &chan, &device) == 3) {
3337 //return new linux_aacraid_device(this,name,channel,device);
3338 return get_sat_device("sat,auto",
3339 new linux_aacraid_device(this, name, host, chan, device));
3340
3341 }
3342
3343 return 0;
3344}
3345
3347{
3348 return "marvell, areca,N/E, 3ware,N, hpt,L/M/N, megaraid,N, aacraid,H,L,ID"
3349#ifdef HAVE_LINUX_CCISS_IOCTL_H
3350 ", cciss,N"
3351#endif
3352 ;
3353}
3354
3355} // namespace
3356
3357/////////////////////////////////////////////////////////////////////////////
3358/// Initialize platform interface and register with smi()
3359
3361{
3362 static os_linux::linux_smart_interface the_interface;
3363 smart_interface::set(&the_interface);
3364}
#define SRB_DataIn
Definition: aacraid.h:43
#define SRB_DataOut
Definition: aacraid.h:44
#define SRB_FUNCTION_EXECUTE_SCSI
Definition: aacraid.h:41
#define FSACTL_SEND_RAW_SRB
Definition: aacraid.h:39
#define SRB_NoDataXfer
Definition: aacraid.h:45
unsigned char ata_debugmode
Definition: atacmds.cpp:33
#define ATA_SMART_AUTO_OFFLINE
Definition: atacmds.h:94
#define ATA_IDENTIFY_DEVICE
Definition: atacmds.h:53
#define ATA_SMART_WRITE_LOG_SECTOR
Definition: atacmds.h:87
#define ATA_IDENTIFY_PACKET_DEVICE
Definition: atacmds.h:54
#define ATA_SMART_STATUS
Definition: atacmds.h:91
#define ATA_SMART_READ_VALUES
Definition: atacmds.h:81
#define ATA_SMART_READ_THRESHOLDS
Definition: atacmds.h:82
#define ATA_SMART_READ_LOG_SECTOR
Definition: atacmds.h:86
#define ATA_SMART_CMD
Definition: atacmds.h:56
#define ATA_SMART_IMMEDIATE_OFFLINE
Definition: atacmds.h:85
smart_command_set
Definition: atacmds.h:29
@ PIDENTIFY
Definition: atacmds.h:43
@ CHECK_POWER_MODE
Definition: atacmds.h:45
@ IDENTIFY
Definition: atacmds.h:42
@ STATUS_CHECK
Definition: atacmds.h:37
@ IMMEDIATE_OFFLINE
Definition: atacmds.h:34
@ AUTO_OFFLINE
Definition: atacmds.h:35
@ ENABLE
Definition: atacmds.h:31
@ WRITE_LOG
Definition: atacmds.h:47
@ READ_VALUES
Definition: atacmds.h:39
@ AUTOSAVE
Definition: atacmds.h:33
@ STATUS
Definition: atacmds.h:36
@ READ_THRESHOLDS
Definition: atacmds.h:40
@ DISABLE
Definition: atacmds.h:32
@ READ_LOG
Definition: atacmds.h:41
#define ATA_SMART_ENABLE
Definition: atacmds.h:89
#define ATA_SMART_AUTOSAVE
Definition: atacmds.h:83
#define ATA_SMART_DISABLE
Definition: atacmds.h:90
#define ATA_CHECK_POWER_MODE
Definition: atacmds.h:52
int cciss_io_interface(int device, int target, struct scsi_cmnd_io *iop, int report)
Smart pointer class for device pointers.
device_type * release()
Return the pointer and release ownership.
Adapter class to implement new ATA pass through old interface.
ATA device access.
bool ata_cmd_is_ok(const ata_cmd_in &in, bool data_out_support=false, bool multi_sector_support=false, bool ata_48bit_support=false)
Check command input parameters (old version).
virtual int arcmsr_get_dev_type()
Definition: dev_areca.cpp:342
void set_disknum(int disknum)
Definition: dev_areca.h:119
void set_encnum(int encnum)
Definition: dev_areca.h:122
NVMe device access.
unsigned get_nsid() const
Get namespace id.
bool set_nvme_err(nvme_cmd_out &out, unsigned status, const char *msg=0)
Set last error number and message if pass-through returns NVMe error status.
void set_nsid(unsigned nsid)
Set namespace id.
PMC AacRAID support.
Definition: os_linux.cpp:828
virtual bool scsi_pass_through(scsi_cmnd_io *iop) override
SCSI pass through.
Definition: os_linux.cpp:913
linux_aacraid_device(smart_interface *intf, const char *dev_name, unsigned int host, unsigned int channel, unsigned int device)
Definition: os_linux.cpp:851
virtual bool open() override
Open device, return false on error.
Definition: os_linux.cpp:865
linux_areca_ata_device(smart_interface *intf, const char *dev_name, int disknum, int encnum=1)
Definition: os_linux.cpp:1935
virtual smart_device * autodetect_open() override
Open device with autodetection support.
Definition: os_linux.cpp:1944
virtual bool arcmsr_lock() override
Definition: os_linux.cpp:1989
virtual bool arcmsr_unlock() override
Definition: os_linux.cpp:1994
virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io *iop) override
Definition: os_linux.cpp:1969
virtual smart_device * autodetect_open() override
Open device with autodetection support.
Definition: os_linux.cpp:2009
virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io *iop) override
Definition: os_linux.cpp:2014
virtual bool arcmsr_lock() override
Definition: os_linux.cpp:2034
virtual bool arcmsr_unlock() override
Definition: os_linux.cpp:2039
linux_areca_scsi_device(smart_interface *intf, const char *dev_name, int disknum, int encnum=1)
Definition: os_linux.cpp:2000
Linux ATA support.
Definition: os_linux.cpp:212
linux_ata_device(smart_interface *intf, const char *dev_name, const char *req_type)
Definition: os_linux.cpp:220
virtual int ata_command_interface(smart_command_set command, int select, char *data) override
Old ATA interface called by ata_pass_through()
Definition: os_linux.cpp:248
virtual bool ata_pass_through(const ata_cmd_in &in, ata_cmd_out &out) override
ATA pass through.
Definition: os_linux.cpp:1654
virtual bool open() override
Open device, return false on error.
Definition: os_linux.cpp:1598
linux_escalade_device(smart_interface *intf, const char *dev_name, escalade_type_t escalade_type, int disknum)
Definition: os_linux.cpp:1441
escalade_type_t m_escalade_type
Controller type.
Definition: os_linux.cpp:1437
Highpoint RAID support.
Definition: os_linux.cpp:2197
unsigned char m_hpt_data[3]
controller/channel/port
Definition: os_linux.cpp:2206
linux_highpoint_device(smart_interface *intf, const char *dev_name, unsigned char controller, unsigned char channel, unsigned char port)
Definition: os_linux.cpp:2209
virtual int ata_command_interface(smart_command_set command, int select, char *data)
Old ATA interface called by ata_pass_through()
Definition: os_linux.cpp:2239
virtual int ata_command_interface(smart_command_set command, int select, char *data)
Old ATA interface called by ata_pass_through()
Definition: os_linux.cpp:2065
linux_marvell_device(smart_interface *intf, const char *dev_name, const char *req_type)
Definition: os_linux.cpp:2058
LSI MegaRAID support.
Definition: os_linux.cpp:1064
bool megasas_cmd(int cdbLen, void *cdb, int dataLen, void *data, int senseLen, void *sense, int report, int direction)
Definition: os_linux.cpp:1273
bool(linux_megaraid_device::* pt_cmd)(int cdblen, void *cdb, int dataLen, void *data, int senseLen, void *sense, int report, int direction)
Definition: os_linux.cpp:1083
virtual bool close() override
Close device, return false on error.
Definition: os_linux.cpp:1209
virtual bool open() override
Open device, return false on error.
Definition: os_linux.cpp:1149
linux_megaraid_device(smart_interface *intf, const char *name, unsigned int tgt)
Definition: os_linux.cpp:1091
virtual smart_device * autodetect_open() override
Open device with autodetection support.
Definition: os_linux.cpp:1108
virtual bool scsi_pass_through(scsi_cmnd_io *iop) override
SCSI pass through.
Definition: os_linux.cpp:1218
bool megadev_cmd(int cdbLen, void *cdb, int dataLen, void *data, int senseLen, void *sense, int report, int direction)
Definition: os_linux.cpp:1334
linux_nvme_device(smart_interface *intf, const char *dev_name, const char *req_type, unsigned nsid)
Definition: os_linux.cpp:2554
virtual bool nvme_pass_through(const nvme_cmd_in &in, nvme_cmd_out &out) override
NVMe pass through.
Definition: os_linux.cpp:2577
virtual bool open() override
Open device, return false on error.
Definition: os_linux.cpp:2562
Standard SCSI support.
Definition: os_linux.cpp:791
bool m_scanning
true if created within scan_smart_devices
Definition: os_linux.cpp:801
virtual smart_device * autodetect_open() override
SCSI open with autodetection support.
Definition: os_linux.cpp:2446
linux_scsi_device(smart_interface *intf, const char *dev_name, const char *req_type, bool scanning=false)
Definition: os_linux.cpp:804
virtual bool scsi_pass_through(scsi_cmnd_io *iop) override
SCSI pass through.
Definition: os_linux.cpp:814
Shared open/close routines.
Definition: os_linux.cpp:103
virtual bool open() override
Open device, return false on error.
Definition: os_linux.cpp:144
int m_flags
Flags for open()
Definition: os_linux.cpp:129
virtual bool is_open() const override
Return true if device is open.
Definition: os_linux.cpp:139
int m_retry_flags
Flags to retry open(), -1 if no retry.
Definition: os_linux.cpp:130
virtual bool close() override
Close device, return false on error.
Definition: os_linux.cpp:176
linux_smart_device(int flags, int retry_flags=-1)
Definition: os_linux.cpp:105
int m_fd
filedesc, -1 if not open.
Definition: os_linux.cpp:128
int get_fd() const
Return filedesc for derived classes.
Definition: os_linux.cpp:121
virtual nvme_device * get_nvme_device(const char *name, const char *type, unsigned nsid) override
Return standard NVMe device.
Definition: os_linux.cpp:2971
virtual ata_device * get_ata_device(const char *name, const char *type) override
Return standard ATA device.
Definition: os_linux.cpp:2961
static int devxy_to_n(const char *name, bool debug)
Definition: os_linux.cpp:2736
virtual std::string get_valid_custom_dev_types_str() override
Return valid 'type' args accepted by above.
Definition: os_linux.cpp:3346
int megasas_pd_add_list(int bus_no, smart_device_list &devlist)
Definition: os_linux.cpp:3041
virtual bool scan_smart_devices(smart_device_list &devlist, const smart_devtype_list &types, const char *pattern=0) override
Fill 'devlist' with devices of all 'types' with device names specified by some optional 'pattern'.
Definition: os_linux.cpp:2895
virtual scsi_device * get_scsi_device(const char *name, const char *type) override
Return standard SCSI device.
Definition: os_linux.cpp:2966
virtual std::string get_app_examples(const char *appname) override
Return example string for program 'appname'.
Definition: os_linux.cpp:2726
virtual smart_device * autodetect_smart_device(const char *name) override
Autodetect device if no device type specified.
Definition: os_linux.cpp:3153
virtual std::string get_os_version_str() override
Return info string about build host and/or OS version.
Definition: os_linux.cpp:2717
bool get_dev_megasas(smart_device_list &devlist)
Definition: os_linux.cpp:2840
virtual smart_device * get_custom_smart_device(const char *name, const char *type) override
Return device for platform specific 'type'.
Definition: os_linux.cpp:3246
int megasas_dcmd_cmd(int bus_no, uint32_t opcode, void *buf, size_t bufsize, uint8_t *mbox, size_t mboxlen, uint8_t *statusp)
Definition: os_linux.cpp:2984
void get_dev_list(smart_device_list &devlist, const char *pattern, bool scan_scsi, bool(*p_dev_sdxy_seen)[devxy_to_n_max+1], bool scan_nvme, const char *req_type, bool autodetect)
Definition: os_linux.cpp:2772
smart_device * missing_option(const char *opt)
Definition: os_linux.cpp:2977
static constexpr int devxy_to_n_max
Definition: os_linux.cpp:2703
SCSI device access.
List of devices for DEVICESCAN.
void push_back(smart_device *dev)
Base class for all devices.
Definition: dev_interface.h:33
smart_interface * smi()
Get interface which produced this object.
const char * get_req_type() const
Get type requested by user, empty if none.
bool set_err(int no, const char *msg,...) __attribute_format_printf(3
Set last error number and message.
bool is_ata() const
Return true if ATA device.
Definition: dev_interface.h:86
device_info & set_info()
R/W access to device info struct.
const char * get_dev_name() const
Get device (path)name.
The platform interface abstraction.
static void set(smart_interface *intf)
Set interface to use, must be called from init().
virtual const char * get_usb_dev_type_by_id(int vendor_id, int product_id, int version=-1)
Get type name for USB device with known VENDOR:PRODUCT ID.
Definition: scsiata.cpp:1502
virtual ata_device * get_sat_device(const char *type, scsi_device *scsidev)
Return ATA->SCSI filter for a SAT or USB 'type'.
Definition: scsiata.cpp:1389
static void init()
Initialize platform interface and register with smi().
Definition: dev_legacy.cpp:334
virtual smart_device * get_scsi_passthrough_device(const char *type, scsi_device *scsidev)
Return ATA->SCSI of NVMe->SCSI filter for a SAT, SNT or USB 'type'.
virtual ata_device * autodetect_sat_device(scsi_device *scsidev, const unsigned char *inqdata, unsigned inqsize)
Try to detect a SAT device behind a SCSI interface.
Definition: scsiata.cpp:1470
bool set_err(int no, const char *msg,...) __attribute_format_printf(3
Set last error number and message.
std::vector< std::string > smart_devtype_list
List of types for DEVICESCAN.
#define NVME_IOCTL_ID
#define NVME_IOCTL_ADMIN_CMD
#define MFI_CMD_DCMD
Definition: megaraid.h:120
u8 cmd
Definition: megaraid.h:1
u16 flags
Definition: megaraid.h:14
#define MEGA_MBOXCMD_PASSTHRU
Definition: megaraid.h:36
u16 host_no
Definition: megaraid.h:0
mega_passthru pthru
Definition: megaraid.h:14
uint8_t opcode
Definition: megaraid.h:0
u8 lun
Definition: megaraid.h:7
#define MEGAIOCCMD
Definition: megaraid.h:28
#define MFI_FRAME_DIR_WRITE
Definition: megaraid.h:130
#define MEGASAS_IOC_FIRMWARE
Definition: megaraid.h:117
uint32_t inlen
Definition: megaraid.h:0
u8 cdb[16]
Definition: megaraid.h:21
#define MFI_STAT_OK
Definition: megaraid.h:122
#define MFI_FRAME_DIR_NONE
Definition: megaraid.h:129
#define MKADAP(adapno)
Definition: megaraid.h:31
#define MFI_DCMD_PD_GET_LIST
Definition: megaraid.h:123
union megasas_sgl sgl
Definition: megaraid.h:22
#define MAX_REQ_SENSE_LEN
Definition: megaraid.h:38
struct megasas_dcmd_frame dcmd
Definition: megaraid.h:10
ptr_t data
Definition: megaraid.h:15
megacmd_t mbox
Definition: megaraid.h:13
#define MFI_FRAME_DIR_READ
Definition: megaraid.h:131
uint32_t outlen
Definition: megaraid.h:1
#define MFI_CMD_PD_SCSI_IO
Definition: megaraid.h:119
#define MFI_MBOX_SIZE
Definition: megaraid.h:127
static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io *iop, int report, enum lk_sg_io_ifc_t sg_io_ifc)
Definition: os_linux.cpp:514
static int do_normal_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io *iop, int report)
Definition: os_linux.cpp:738
@ SG_IO_UNSUPP
Definition: os_linux.cpp:502
@ SG_IO_USE_DETECT
Definition: os_linux.cpp:501
@ SG_IO_USE_V4
Definition: os_linux.cpp:504
@ SG_IO_USE_V3
Definition: os_linux.cpp:503
static bool get_usb_id(const char *name, unsigned short &vendor_id, unsigned short &product_id, unsigned short &version)
Definition: os_linux.cpp:2624
static unsigned get_kernel_release()
Definition: os_linux.cpp:3078
static bool read_id(const std::string &path, unsigned short &id)
Definition: os_linux.cpp:2612
static int setup_3ware_nodes(const char *nodename, const char *driver_name)
Definition: os_linux.cpp:1454
static const char smartctl_examples[]
Definition: os_linux.cpp:185
static int find_areca_in_proc()
Definition: os_linux.cpp:1889
static bool is_hpsa_in_raid_mode(const char *name)
Definition: os_linux.cpp:3091
static enum lk_sg_io_ifc_t sg_io_interface
Definition: os_linux.cpp:507
uint8_t id
uint32_t nsid
unsigned char nvme_debugmode
Definition: nvmecmds.cpp:27
unsigned char failuretest_permissive
Definition: smartctl.cpp:1447
void printwarning(int msgNo, const char *extra)
Definition: os_freebsd.cpp:89
#define TW_IOCTL_BUFFER_SIZE
Definition: os_freebsd.cpp:118
const char * os_linux_cpp_cvsid
Definition: os_linux.cpp:92
#define LSCSI_DID_ERROR
Definition: os_linux.cpp:493
unsigned char failuretest_permissive
Definition: smartctl.cpp:1447
#define LSCSI_DRIVER_TIMEOUT
Definition: os_linux.cpp:494
#define LSCSI_DID_BUS_BUSY
Definition: os_linux.cpp:496
#define LSCSI_DRIVER_MASK
Definition: os_linux.cpp:491
#define LSCSI_DID_TIME_OUT
Definition: os_linux.cpp:495
#define MAJOR_STRING_LENGTH
Definition: os_linux.cpp:1451
#define LSCSI_DRIVER_SENSE
Definition: os_linux.cpp:492
#define LSCSI_DID_NO_CONNECT
Definition: os_linux.cpp:497
#define ENOTSUP
Definition: os_linux.cpp:87
#define SRB_STATUS_AUTOSENSE_VALID
#define SRB_STATUS_ERROR
#define DEVICE_STRING_LENGTH
Definition: os_linux.cpp:1452
#define SRB_STATUS_SELECTION_TIMEOUT
#define STRANGE_BUFFER_LENGTH
Definition: os_linux.cpp:2237
#define SRB_STATUS_NO_DEVICE
#define SRB_STATUS_SUCCESS
#define BUFFER_LENGTH
Definition: os_linux.cpp:246
#define SCSI_IOCTL_SEND_COMMAND
Definition: os_linux.cpp:1622
#define NODE_STRING_LENGTH
Definition: os_linux.cpp:1453
#define TW_OP_ATA_PASSTHRU
Definition: os_linux.h:40
struct ide_task_request_s ide_task_request_t
#define HPTIO_CTL
Definition: os_linux.h:373
#define OS_LINUX_H_CVSID
Definition: os_linux.h:27
#define TW_ATA_PASSTHRU
Definition: os_linux.h:86
#define TASKFILE_OUT
Definition: os_linux.h:363
#define HDIO_DRIVE_TASKFILE
Definition: os_linux.h:370
#define HDIO_GET_IDENTITY
Definition: os_linux.h:371
#define TW_IOCTL_FIRMWARE_PASS_THROUGH
Definition: os_linux.h:121
#define HDIO_DRIVE_TASK
Definition: os_linux.h:369
#define TW_CMD_PACKET_WITH_DATA
Definition: os_linux.h:228
#define IDE_DRIVE_TASK_OUT
Definition: os_linux.h:367
#define TW_IOCTL
Definition: os_linux.h:85
#define HDIO_DRIVE_CMD
Definition: os_linux.h:368
bool is_scsi_cdb(const uint8_t *cdbp, int clen)
Definition: scsicmds.cpp:151
int scsiStdInquiry(scsi_device *device, uint8_t *pBuf, int bufLen)
Definition: scsicmds.cpp:923
void dStrHex(const uint8_t *up, int len, int no_ascii)
Definition: scsicmds.cpp:78
unsigned char scsi_debugmode
Definition: scsicmds.cpp:45
const char * scsi_get_opcode_name(const uint8_t *cdbp)
Definition: scsicmds.cpp:271
#define SAT_ATA_PASSTHROUGH_12
Definition: scsicmds.h:98
#define DXFER_NONE
Definition: scsicmds.h:105
#define DXFER_FROM_DEVICE
Definition: scsicmds.h:106
#define SAT_ATA_PASSTHROUGH_16
Definition: scsicmds.h:101
#define DXFER_TO_DEVICE
Definition: scsicmds.h:107
#define SCSI_STATUS_CHECK_CONDITION
Definition: scsicmds.h:313
void pout(const char *fmt,...)
Definition: smartd.cpp:1328
#define STATIC_ASSERT(x)
Definition: static_assert.h:24
union TAG_TW_Command_Full_9000::@63 command
TW_Command_9000 oldcommand
Definition: os_linux.h:208
TW_Command_Full_9000 firmware_command
Definition: os_linux.h:217
char data_buffer[1]
Definition: os_linux.h:218
TW_Ioctl_Driver_Command_9000 driver_command
Definition: os_linux.h:215
unsigned char input_data[499]
Definition: os_linux.h:106
unsigned char opcode
Definition: os_linux.h:95
int input_length
Definition: os_linux.h:92
unsigned char cdb[16]
Definition: os_linux.h:94
int output_length
Definition: os_linux.h:93
unsigned int data_buffer_length
Definition: os_linux.h:272
TW_Command firmware_command
Definition: os_linux.h:274
char data_buffer[1]
Definition: os_linux.h:275
char output_data[512]
Definition: os_linux.h:116
unsigned char drive_head
Definition: os_linux.h:74
struct TAG_TW_Passthru::@51 byte0
unsigned char flags
Definition: os_linux.h:67
unsigned char sgloff
Definition: os_linux.h:61
unsigned short cylinder_hi
Definition: os_linux.h:73
unsigned char command
Definition: os_linux.h:75
unsigned short cylinder_lo
Definition: os_linux.h:72
unsigned short features
Definition: os_linux.h:69
unsigned char status
Definition: os_linux.h:66
unsigned char opcode
Definition: os_linux.h:60
unsigned char unit
Definition: os_linux.h:65
unsigned short sector_num
Definition: os_linux.h:71
unsigned short sector_count
Definition: os_linux.h:70
unsigned char size
Definition: os_linux.h:63
unsigned short param
Definition: os_linux.h:68
unsigned char request_id
Definition: os_linux.h:64
uint32_t hi32
Definition: aacraid.h:49
uint32_t lo32
Definition: aacraid.h:48
ATA pass through input parameters.
enum ata_cmd_in::@29 direction
I/O direction.
void * buffer
Pointer to data buffer.
ata_in_regs_48bit in_regs
Input registers.
unsigned size
Size of buffer.
ATA pass through output parameters.
ata_out_regs_48bit out_regs
Output registers.
ATA Input registers for 48-bit commands.
ata_reg_alias_16 lba_high_16
ata_reg_alias_16 sector_count_16
ata_reg_alias_16 features_16
ata_reg_alias_16 lba_low_16
ata_reg_alias_16 lba_mid_16
ata_register device
ata_register lba_low
ata_register features
ata_register command
ATA Output registers for 48-bit commands.
ata_reg_alias_16 lba_mid_16
ata_reg_alias_16 sector_count_16
ata_reg_alias_16 lba_low_16
ata_reg_alias_16 lba_high_16
ata_register error
ata_register device
ata_register status
task_ioreg_t sector_number
Definition: os_linux.h:321
task_ioreg_t low_cylinder
Definition: os_linux.h:322
task_ioreg_t feature
Definition: os_linux.h:319
task_ioreg_t high_cylinder
Definition: os_linux.h:323
task_ioreg_t data
Definition: os_linux.h:318
task_ioreg_t command
Definition: os_linux.h:325
task_ioreg_t device_head
Definition: os_linux.h:324
task_ioreg_t sector_count
Definition: os_linux.h:320
unsigned long in_size
Definition: os_linux.h:358
task_ioreg_t io_ports[8]
Definition: os_linux.h:351
unsigned long out_size
Definition: os_linux.h:357
union megasas_dcmd_frame::@38 mbox
union megasas_sgl sgl
Definition: megaraid.h:228
struct megasas_pthru_frame pthru
Definition: megaraid.h:242
struct iovec sgl[MAX_IOCTL_SGE]
Definition: megaraid.h:246
struct megasas_dcmd_frame dcmd
Definition: megaraid.h:243
union megasas_iocpacket::@40 frame
struct megasas_pd_address addr[MAX_SYS_PDS]
Definition: megaraid.h:262
NVMe pass through input parameters.
unsigned cdw10
unsigned cdw13
unsigned cdw11
unsigned char opcode
Opcode (CDW0 07:00)
unsigned size
Size of buffer.
unsigned cdw14
unsigned cdw15
Cmd specific.
unsigned nsid
Namespace ID.
unsigned cdw12
void * buffer
Pointer to data buffer.
NVMe pass through output parameters.
unsigned result
Command specific result (DW0)
uint8_t * sensep
Definition: scsicmds.h:120
uint8_t * dxferp
Definition: scsicmds.h:118
int dxfer_dir
Definition: scsicmds.h:116
size_t cmnd_len
Definition: scsicmds.h:115
size_t resp_sense_len
Definition: scsicmds.h:124
size_t dxfer_len
Definition: scsicmds.h:119
size_t max_sense_len
Definition: scsicmds.h:122
uint8_t scsi_status
Definition: scsicmds.h:125
uint8_t * cmnd
Definition: scsicmds.h:114
unsigned timeout
Definition: scsicmds.h:123
std::string info_name
Informal name.
Definition: dev_interface.h:46
std::string dev_type
Actual device type.
Definition: dev_interface.h:47
uint32_t inlen
Definition: megaraid.h:92
mega_passthru pthru
Definition: megaraid.h:106
uint8_t opcode
Definition: megaraid.h:97
uint32_t outlen
Definition: megaraid.h:93
megacmd_t mbox
Definition: megaraid.h:105
ptr_t data
Definition: megaraid.h:107
uint16_t adapno
Definition: megaraid.h:99
uint32_t srb_status
Definition: aacraid.h:141
uint32_t sense_data_size
Definition: aacraid.h:144
uint8_t sense_data[30]
Definition: aacraid.h:145
uint32_t scsi_status
Definition: aacraid.h:142
uint32_t id
Definition: aacraid.h:114
uint8_t cdb[16]
Definition: aacraid.h:121
uint32_t function
Definition: aacraid.h:112
uint32_t cdb_size
Definition: aacraid.h:120
uint32_t timeout
Definition: aacraid.h:116
user_sgmap64 sg64
Definition: aacraid.h:122
uint32_t retry_limit
Definition: aacraid.h:119
uint32_t count
Definition: aacraid.h:118
uint32_t flags
Definition: aacraid.h:117
uint32_t channel
Definition: aacraid.h:113
uint32_t lun
Definition: aacraid.h:115
uint32_t length
Definition: aacraid.h:54
address64 addr64
Definition: aacraid.h:53
user_sgentry64 sg64[1]
Definition: aacraid.h:64
uint32_t count
Definition: aacraid.h:63
struct megasas_sge32 sge32[1]
Definition: megaraid.h:152
uint8_t * pointer
Definition: megaraid.h:77
void syserror(const char *message)
Definition: utility.cpp:390
std::string strprintf(const char *fmt,...)
Definition: utility.cpp:780
bool nonempty(const void *data, int size)
Definition: utility.cpp:663
bool str_starts_with(const char *str, const char *prefix)
Definition: utility.h:51