4 * Home page of code is: http://www.smartmontools.org
6 * Copyright (C) 2008-16 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * You should have received a copy of the GNU General Public License
14 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
20 #include "dev_interface.h"
21 #include "dev_tunnelled.h"
22 #include "atacmds.h" // ATA_SMART_CMD/STATUS
29 #if defined(HAVE_GETTIMEOFDAY)
31 #elif defined(HAVE_FTIME)
32 #include <sys/timeb.h>
35 const char * dev_interface_cpp_cvsid
= "$Id: dev_interface.cpp 4283 2016-04-10 12:55:59Z chrfranke $"
36 DEV_INTERFACE_H_CVSID
;
38 /////////////////////////////////////////////////////////////////////////////
41 int smart_device::s_num_objects
= 0;
43 smart_device::smart_device(smart_interface
* intf
, const char * dev_name
,
44 const char * dev_type
, const char * req_type
)
45 : m_intf(intf
), m_info(dev_name
, dev_type
, req_type
),
46 m_ata_ptr(0), m_scsi_ptr(0), m_nvme_ptr(0)
51 smart_device::smart_device(do_not_use_in_implementation_classes
)
52 : m_intf(0), m_ata_ptr(0), m_scsi_ptr(0), m_nvme_ptr(0)
54 throw std::logic_error("smart_device: wrong constructor called in implementation class");
57 smart_device::~smart_device() throw()
62 bool smart_device::is_syscall_unsup() const
64 if (get_errno() == ENOSYS
)
67 if (get_errno() == ENOTSUP
)
73 bool smart_device::set_err(int no
, const char * msg
, ...)
78 va_list ap
; va_start(ap
, msg
);
79 m_err
.msg
= vstrprintf(msg
, ap
);
84 bool smart_device::set_err(int no
)
86 return smi()->set_err_var(&m_err
, no
);
89 smart_device
* smart_device::autodetect_open()
95 bool smart_device::is_powered_down()
100 bool smart_device::owns(const smart_device
* /*dev*/) const
105 void smart_device::release(const smart_device
* /*dev*/)
110 /////////////////////////////////////////////////////////////////////////////
113 ata_in_regs_48bit::ata_in_regs_48bit()
114 : features_16(features
, prev
.features
),
115 sector_count_16(sector_count
, prev
.sector_count
),
116 lba_low_16(lba_low
, prev
.lba_low
),
117 lba_mid_16(lba_mid
, prev
.lba_mid
),
118 lba_high_16(lba_high
, prev
.lba_high
),
119 lba_48( lba_low
, lba_mid
, lba_high
,
120 prev
.lba_low
, prev
.lba_mid
, prev
.lba_high
)
124 ata_out_regs_48bit::ata_out_regs_48bit()
125 : sector_count_16(sector_count
, prev
.sector_count
),
126 lba_low_16(lba_low
, prev
.lba_low
),
127 lba_mid_16(lba_mid
, prev
.lba_mid
),
128 lba_high_16(lba_high
, prev
.lba_high
),
129 lba_48( lba_low
, lba_mid
, lba_high
,
130 prev
.lba_low
, prev
.lba_mid
, prev
.lba_high
)
134 ata_cmd_in::ata_cmd_in()
135 : direction(no_data
),
141 ata_cmd_out::ata_cmd_out()
145 bool ata_device::ata_pass_through(const ata_cmd_in
& in
)
148 return ata_pass_through(in
, dummy
);
151 bool ata_device::ata_cmd_is_supported(const ata_cmd_in
& in
,
152 unsigned flags
, const char * type
/* = 0 */)
155 switch (in
.direction
) {
156 case ata_cmd_in::no_data
: break;
157 case ata_cmd_in::data_in
: break;
158 case ata_cmd_in::data_out
: break;
160 return set_err(EINVAL
, "Invalid data direction %d", (int)in
.direction
);
164 if (in
.direction
== ata_cmd_in::no_data
) {
166 return set_err(EINVAL
, "Buffer size %u > 0 for NO DATA command", in
.size
);
170 return set_err(EINVAL
, "Buffer not set for DATA IN/OUT command");
171 unsigned count
= (in
.in_regs
.prev
.sector_count
<<16)|in
.in_regs
.sector_count
;
172 // TODO: Add check for sector count == 0
173 if (count
* 512 != in
.size
)
174 return set_err(EINVAL
, "Sector count %u does not match buffer size %u", count
, in
.size
);
178 const char * errmsg
= 0;
179 if (in
.direction
== ata_cmd_in::data_out
&& !(flags
& supports_data_out
))
180 errmsg
= "DATA OUT ATA commands not implemented";
181 else if ( in
.out_needed
.is_set() && !(flags
& supports_output_regs
)
182 && !( in
.in_regs
.command
== ATA_SMART_CMD
183 && in
.in_regs
.features
== ATA_SMART_STATUS
184 && (flags
& supports_smart_status
)))
185 errmsg
= "Read of ATA output registers not implemented";
186 else if (!(in
.size
== 0 || in
.size
== 512) && !(flags
& supports_multi_sector
))
187 errmsg
= "Multi-sector ATA commands not implemented";
188 else if (in
.in_regs
.is_48bit_cmd() && !(flags
& (supports_48bit_hi_null
|supports_48bit
)))
189 errmsg
= "48-bit ATA commands not implemented";
190 else if (in
.in_regs
.is_real_48bit_cmd() && !(flags
& supports_48bit
))
191 errmsg
= "48-bit ATA commands not fully implemented";
194 return set_err(ENOSYS
, "%s%s%s%s", errmsg
,
195 (type
? " [" : ""), (type
? type
: ""), (type
? "]" : ""));
200 bool ata_device::ata_identify_is_cached() const
206 /////////////////////////////////////////////////////////////////////////////
209 bool nvme_device::set_nvme_err(nvme_cmd_out
& out
, unsigned status
, const char * msg
/* = 0 */)
212 throw std::logic_error("nvme_device: set_nvme_err() called with status=0");
215 out
.status_valid
= true;
216 return set_err(EIO
, "%sNVMe Status 0x%02x", (msg
? msg
: ""), status
);
220 /////////////////////////////////////////////////////////////////////////////
221 // tunnelled_device_base
223 tunnelled_device_base::tunnelled_device_base(smart_device
* tunnel_dev
)
224 : smart_device(never_called
),
225 m_tunnel_base_dev(tunnel_dev
)
229 tunnelled_device_base::~tunnelled_device_base() throw()
231 delete m_tunnel_base_dev
;
234 bool tunnelled_device_base::is_open() const
236 return (m_tunnel_base_dev
&& m_tunnel_base_dev
->is_open());
239 bool tunnelled_device_base::open()
241 if (!m_tunnel_base_dev
)
242 return set_err(ENOSYS
);
243 if (!m_tunnel_base_dev
->open())
244 return set_err(m_tunnel_base_dev
->get_err());
248 bool tunnelled_device_base::close()
250 if (!m_tunnel_base_dev
)
252 if (!m_tunnel_base_dev
->close())
253 return set_err(m_tunnel_base_dev
->get_err());
257 bool tunnelled_device_base::owns(const smart_device
* dev
) const
259 return (m_tunnel_base_dev
&& (m_tunnel_base_dev
== dev
));
262 void tunnelled_device_base::release(const smart_device
* dev
)
264 if (m_tunnel_base_dev
== dev
)
265 m_tunnel_base_dev
= 0;
269 /////////////////////////////////////////////////////////////////////////////
272 // Pointer to (usually singleton) interface object returned by ::smi()
273 smart_interface
* smart_interface::s_instance
;
275 std::string
smart_interface::get_os_version_str()
277 return SMARTMONTOOLS_BUILD_HOST
;
280 std::string
smart_interface::get_valid_dev_types_str()
284 "ata, scsi, nvme[,NSID], sat[,auto][,N][+TYPE], "
285 "usbcypress[,X], usbjmicron[,p][,x][,N], usbprolific, usbsunplus";
287 std::string s2
= get_valid_custom_dev_types_str();
294 std::string
smart_interface::get_app_examples(const char * /*appname*/)
299 int64_t smart_interface::get_timer_usec()
301 #if defined(HAVE_GETTIMEOFDAY)
302 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
304 static bool have_clock_monotonic
= true;
305 if (have_clock_monotonic
) {
307 if (!clock_gettime(CLOCK_MONOTONIC
, &ts
))
308 return ts
.tv_sec
* 1000000LL + ts
.tv_nsec
/1000;
309 have_clock_monotonic
= false;
315 gettimeofday(&tv
, 0);
316 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
318 #elif defined(HAVE_FTIME)
322 return tb
.time
* 1000000LL + tb
.millitm
* 1000;
329 bool smart_interface::disable_system_auto_standby(bool /*disable*/)
331 return set_err(ENOSYS
);
334 bool smart_interface::set_err(int no
, const char * msg
, ...)
339 va_list ap
; va_start(ap
, msg
);
340 m_err
.msg
= vstrprintf(msg
, ap
);
345 bool smart_interface::set_err(int no
)
347 return set_err_var(&m_err
, no
);
350 bool smart_interface::set_err_var(smart_device::error_info
* err
, int no
)
353 err
->msg
= get_msg_for_errno(no
);
354 if (err
->msg
.empty() && no
!= 0)
355 err
->msg
= strprintf("Unknown error %d", no
);
359 const char * smart_interface::get_msg_for_errno(int no
)
365 /////////////////////////////////////////////////////////////////////////////
366 // Default device factory
368 smart_device
* smart_interface::get_smart_device(const char * name
, const char * type
)
372 // Call platform specific autodetection if no device type specified
374 if (!type
|| !*type
) {
375 dev
= autodetect_smart_device(name
);
376 if (!dev
&& !get_errno())
377 set_err(EINVAL
, "Unable to detect device type");
381 // First check for platform specific device types
382 dev
= get_custom_smart_device(name
, type
);
383 if (dev
|| get_errno())
386 if (!strcmp(type
, "ata"))
387 dev
= get_ata_device(name
, type
);
388 else if (!strcmp(type
, "scsi"))
389 dev
= get_scsi_device(name
, type
);
391 else if (str_starts_with(type
, "nvme")) {
392 int n1
= -1, n2
= -1, len
= strlen(type
);
393 unsigned nsid
= 0; // invalid namespace id -> use default
394 sscanf(type
, "nvme%n,0x%x%n", &n1
, &nsid
, &n2
);
395 if (!(n1
== len
|| n2
== len
)) {
396 set_err(EINVAL
, "Invalid NVMe namespace id in '%s'", type
);
399 dev
= get_nvme_device(name
, type
, nsid
);
402 else if ( ((!strncmp(type
, "sat", 3) && (!type
[3] || strchr(",+", type
[3])))
403 || (!strncmp(type
, "usb", 3)))) {
404 // Split "sat...+base..." -> ("sat...", "base...")
405 unsigned satlen
= strcspn(type
, "+");
406 std::string
sattype(type
, satlen
);
407 const char * basetype
= (type
[satlen
] ? type
+satlen
+1 : "");
408 // Recurse to allocate base device, default is standard SCSI
411 smart_device_auto_ptr
basedev( get_smart_device(name
, basetype
) );
413 set_err(EINVAL
, "Type '%s+...': %s", sattype
.c_str(), get_errmsg());
416 // Result must be SCSI
417 if (!basedev
->is_scsi()) {
418 set_err(EINVAL
, "Type '%s+...': Device type '%s' is not SCSI", sattype
.c_str(), basetype
);
422 return get_sat_device(sattype
.c_str(), basedev
.release()->to_scsi());
426 set_err(EINVAL
, "Unknown device type '%s'", type
);
429 if (!dev
&& !get_errno())
430 set_err(EINVAL
, "Not a device of type '%s'", type
);
434 bool smart_interface::scan_smart_devices(smart_device_list
& devlist
,
435 const smart_devtype_list
& types
, const char * pattern
/* = 0 */)
437 unsigned n
= types
.size();
439 return scan_smart_devices(devlist
, (const char *)0, pattern
);
441 return scan_smart_devices(devlist
, types
.front().c_str(), pattern
);
443 for (unsigned i
= 0; i
< n
; i
++) {
444 smart_device_list tmplist
;
445 if (!scan_smart_devices(tmplist
, types
[i
].c_str(), pattern
))
447 devlist
.append(tmplist
);
453 nvme_device
* smart_interface::get_nvme_device(const char * /*name*/, const char * /*type*/, unsigned /*nsid*/)
455 set_err(ENOSYS
, "NVMe devices are not supported in this version of smartmontools");
459 smart_device
* smart_interface::get_custom_smart_device(const char * /*name*/, const char * /*type*/)
464 std::string
smart_interface::get_valid_custom_dev_types_str()