]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - dev_interface.cpp
Closes #831504
[mirror_smartmontools-debian.git] / dev_interface.cpp
index 7651244aeb069e88d6bbf16775e3c50e53e7f02d..dd428413a393b8b7e308e6267a3986bee24d1603 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * dev_interface.cpp
  *
- * Home page of code is: http://smartmontools.sourceforge.net
+ * Home page of code is: http://www.smartmontools.org
  *
- * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2008-16 Christian Franke <smartmontools-support@lists.sourceforge.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 #include "int64.h"
 #include "dev_interface.h"
 #include "dev_tunnelled.h"
+#include "atacmds.h" // ATA_SMART_CMD/STATUS
 #include "utility.h"
 
 #include <errno.h>
 #include <sys/timeb.h>
 #endif
 
-const char * dev_interface_cpp_cvsid = "$Id: dev_interface.cpp 3554 2012-06-01 20:11:46Z chrfranke $"
+const char * dev_interface_cpp_cvsid = "$Id: dev_interface.cpp 4283 2016-04-10 12:55:59Z chrfranke $"
   DEV_INTERFACE_H_CVSID;
 
 /////////////////////////////////////////////////////////////////////////////
 // smart_device
 
+int smart_device::s_num_objects = 0;
+
 smart_device::smart_device(smart_interface * intf, const char * dev_name,
     const char * dev_type, const char * req_type)
 : m_intf(intf), m_info(dev_name, dev_type, req_type),
-  m_ata_ptr(0), m_scsi_ptr(0)
+  m_ata_ptr(0), m_scsi_ptr(0), m_nvme_ptr(0)
 {
+  s_num_objects++;
 }
 
 smart_device::smart_device(do_not_use_in_implementation_classes)
-: m_intf(0), m_ata_ptr(0), m_scsi_ptr(0)
+: m_intf(0), m_ata_ptr(0), m_scsi_ptr(0), m_nvme_ptr(0)
 {
   throw std::logic_error("smart_device: wrong constructor called in implementation class");
 }
 
 smart_device::~smart_device() throw()
 {
+  s_num_objects--;
 }
 
 bool smart_device::is_syscall_unsup() const
@@ -87,6 +92,11 @@ smart_device * smart_device::autodetect_open()
   return this;
 }
 
+bool smart_device::is_powered_down()
+{
+  return false;
+}
+
 bool smart_device::owns(const smart_device * /*dev*/) const
 {
   return false;
@@ -138,10 +148,8 @@ bool ata_device::ata_pass_through(const ata_cmd_in & in)
   return ata_pass_through(in, dummy);
 }
 
-bool ata_device::ata_cmd_is_ok(const ata_cmd_in & in,
-  bool data_out_support /*= false*/,
-  bool multi_sector_support /*= false*/,
-  bool ata_48bit_support /*= false*/)
+bool ata_device::ata_cmd_is_supported(const ata_cmd_in & in,
+  unsigned flags, const char * type /* = 0 */)
 {
   // Check DATA IN/OUT
   switch (in.direction) {
@@ -167,12 +175,25 @@ bool ata_device::ata_cmd_is_ok(const ata_cmd_in & in,
   }
 
   // Check features
-  if (in.direction == ata_cmd_in::data_out && !data_out_support)
-    return set_err(ENOSYS, "DATA OUT ATA commands not supported");
-  if (!(in.size == 0 || in.size == 512) && !multi_sector_support)
-    return set_err(ENOSYS, "Multi-sector ATA commands not supported");
-  if (in.in_regs.is_48bit_cmd() && !ata_48bit_support)
-    return set_err(ENOSYS, "48-bit ATA commands not supported");
+  const char * errmsg = 0;
+  if (in.direction == ata_cmd_in::data_out && !(flags & supports_data_out))
+    errmsg = "DATA OUT ATA commands not implemented";
+  else if (   in.out_needed.is_set() && !(flags & supports_output_regs)
+           && !(   in.in_regs.command == ATA_SMART_CMD
+                && in.in_regs.features == ATA_SMART_STATUS
+                && (flags & supports_smart_status)))
+    errmsg = "Read of ATA output registers not implemented";
+  else if (!(in.size == 0 || in.size == 512) && !(flags & supports_multi_sector))
+    errmsg = "Multi-sector ATA commands not implemented";
+  else if (in.in_regs.is_48bit_cmd() && !(flags & (supports_48bit_hi_null|supports_48bit)))
+    errmsg = "48-bit ATA commands not implemented";
+  else if (in.in_regs.is_real_48bit_cmd() && !(flags & supports_48bit))
+    errmsg = "48-bit ATA commands not fully implemented";
+
+  if (errmsg)
+    return set_err(ENOSYS, "%s%s%s%s", errmsg,
+                   (type ? " [" : ""), (type ? type : ""), (type ? "]" : ""));
+
   return true;
 }
 
@@ -182,6 +203,20 @@ bool ata_device::ata_identify_is_cached() const
 }
 
 
+/////////////////////////////////////////////////////////////////////////////
+// nvme_device
+
+bool nvme_device::set_nvme_err(nvme_cmd_out & out, unsigned status, const char * msg /* = 0 */)
+{
+  if (!status)
+    throw std::logic_error("nvme_device: set_nvme_err() called with status=0");
+
+  out.status = status;
+  out.status_valid = true;
+  return set_err(EIO, "%sNVMe Status 0x%02x", (msg ? msg : ""), status);
+}
+
+
 /////////////////////////////////////////////////////////////////////////////
 // tunnelled_device_base
 
@@ -246,7 +281,8 @@ std::string smart_interface::get_valid_dev_types_str()
 {
   // default
   std::string s =
-    "ata, scsi, sat[,auto][,N][+TYPE], usbcypress[,X], usbjmicron[,x][,N], usbsunplus";
+    "ata, scsi, nvme[,NSID], sat[,auto][,N][+TYPE], "
+    "usbcypress[,X], usbjmicron[,p][,x][,N], usbprolific, usbsunplus";
   // append custom
   std::string s2 = get_valid_custom_dev_types_str();
   if (!s2.empty()) {
@@ -352,6 +388,17 @@ smart_device * smart_interface::get_smart_device(const char * name, const char *
   else if (!strcmp(type, "scsi"))
     dev = get_scsi_device(name, type);
 
+  else if (str_starts_with(type, "nvme")) {
+    int n1 = -1, n2 = -1, len = strlen(type);
+    unsigned nsid = 0; // invalid namespace id -> use default
+    sscanf(type, "nvme%n,0x%x%n", &n1, &nsid, &n2);
+    if (!(n1 == len || n2 == len)) {
+      set_err(EINVAL, "Invalid NVMe namespace id in '%s'", type);
+      return 0;
+    }
+    dev = get_nvme_device(name, type, nsid);
+  }
+
   else if (  ((!strncmp(type, "sat", 3) && (!type[3] || strchr(",+", type[3])))
            || (!strncmp(type, "usb", 3)))) {
     // Split "sat...+base..." -> ("sat...", "base...")
@@ -372,11 +419,7 @@ smart_device * smart_interface::get_smart_device(const char * name, const char *
       return 0;
     }
     // Attach SAT tunnel
-    ata_device * satdev = get_sat_device(sattype.c_str(), basedev->to_scsi());
-    if (!satdev)
-      return 0;
-    basedev.release();
-    return satdev;
+    return get_sat_device(sattype.c_str(), basedev.release()->to_scsi());
   }
 
   else {
@@ -388,6 +431,31 @@ smart_device * smart_interface::get_smart_device(const char * name, const char *
   return dev;
 }
 
+bool smart_interface::scan_smart_devices(smart_device_list & devlist,
+  const smart_devtype_list & types, const char * pattern /* = 0 */)
+{
+  unsigned n = types.size();
+  if (n == 0)
+    return scan_smart_devices(devlist, (const char *)0, pattern);
+  if (n == 1)
+    return scan_smart_devices(devlist, types.front().c_str(), pattern);
+
+  for (unsigned i = 0; i < n; i++) {
+    smart_device_list tmplist;
+    if (!scan_smart_devices(tmplist, types[i].c_str(), pattern))
+      return false;
+    devlist.append(tmplist);
+  }
+
+  return true;
+}
+
+nvme_device * smart_interface::get_nvme_device(const char * /*name*/, const char * /*type*/, unsigned /*nsid*/)
+{
+  set_err(ENOSYS, "NVMe devices are not supported in this version of smartmontools");
+  return 0;
+}
+
 smart_device * smart_interface::get_custom_smart_device(const char * /*name*/, const char * /*type*/)
 {
   return 0;