]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - dev_interface.h
Merge tag 'upstream/6.6'
[mirror_smartmontools-debian.git] / dev_interface.h
index 5f54fd8742c2f092af0ad561c21d64b784501bb3..44368507135b5ae28d438bd56262783c0fc8c504 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * dev_interface.h
  *
- * Home page of code is: http://smartmontools.sourceforge.net
+ * Home page of code is: http://www.smartmontools.org
  *
- * Copyright (C) 2008-9 Christian Franke <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2008-16 Christian Franke
  *
  * 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
 #ifndef DEV_INTERFACE_H
 #define DEV_INTERFACE_H
 
-#define DEV_INTERFACE_H_CVSID "$Id: dev_interface.h 2973 2009-10-26 22:38:19Z chrfranke $\n"
+#define DEV_INTERFACE_H_CVSID "$Id: dev_interface.h 4283 2016-04-10 12:55:59Z chrfranke $\n"
+
+#include "utility.h"
 
-#include <stdarg.h>
 #include <stdexcept>
 #include <string>
 #include <vector>
 
-#if !defined(__GNUC__) && !defined(__attribute__)
-#define __attribute__(x)  /**/
-#endif
-
-#ifdef _MSC_VER // Disable MSVC warning
-#pragma warning(disable:4250) // 'class1' : inherits 'class2::member' via dominance
-#endif
-
 /////////////////////////////////////////////////////////////////////////////
 // Common functionality for all device types
 
@@ -40,6 +33,7 @@
 class smart_interface;
 class ata_device;
 class scsi_device;
+class nvme_device;
 
 /// Base class for all devices
 class smart_device
@@ -85,7 +79,7 @@ protected:
   enum do_not_use_in_implementation_classes { never_called };
   /// Dummy constructor for abstract classes.
   /// Must never be called in implementation classes.
-  smart_device(do_not_use_in_implementation_classes);
+  explicit smart_device(do_not_use_in_implementation_classes);
 
 public:
   virtual ~smart_device() throw();
@@ -101,6 +95,9 @@ public:
   /// Return true if SCSI device
   bool is_scsi() const
     { return !!m_scsi_ptr; }
+  /// Return true if NVMe device
+  bool is_nvme() const
+    { return !!m_nvme_ptr; }
 
   /// Downcast to ATA device.
   ata_device * to_ata()
@@ -111,9 +108,15 @@ public:
   /// Downcast to SCSI device.
   scsi_device * to_scsi()
     { return m_scsi_ptr; }
-  /// Downcast to ATA device (const).
+  /// Downcast to SCSI device (const).
   const scsi_device * to_scsi() const
     { return m_scsi_ptr; }
+  /// Downcast to NVMe device.
+  nvme_device * to_nvme()
+    { return m_nvme_ptr; }
+  /// Downcast to NVMe device (const).
+  const nvme_device * to_nvme() const
+    { return m_nvme_ptr; }
 
   ///////////////////////////////////////////////
   // Device information
@@ -154,11 +157,15 @@ public:
   const char * get_errmsg() const
     { return m_err.msg.c_str(); }
 
+  /// Return true if last error indicates an unsupported system call.
+  /// Default implementation returns true on ENOSYS and ENOTSUP.
+  virtual bool is_syscall_unsup() const;
+
   /// Set last error number and message.
   /// Printf()-like formatting is supported.
   /// Returns false always to allow use as a return expression.
   bool set_err(int no, const char * msg, ...)
-    __attribute__ ((format (printf, 3, 4)));
+    __attribute_format_printf(3, 4);
 
   /// Set last error info struct.
   bool set_err(const error_info & err)
@@ -172,6 +179,10 @@ public:
   /// Message is retrieved from interface's get_msg_for_errno(no).
   bool set_err(int no);
 
+  /// Get current number of allocated 'smart_device' objects.
+  static int get_num_objects()
+    { return s_num_objects; }
+
 // Operations
 public:
   ///////////////////////////////////////////////
@@ -190,9 +201,20 @@ public:
   /// Open device with autodetection support.
   /// May return another device for further access.
   /// In this case, the original pointer is no longer valid.
-  /// Default Implementation calls 'open()' and returns 'this'.
+  /// Default implementation calls 'open()' and returns 'this'.
   virtual smart_device * autodetect_open();
 
+  ///////////////////////////////////////////////
+  // Support for checking power mode reported by operating system
+
+  /// Early test if device is powered up or down.
+  /// Can be used without calling 'open()' first!
+  /// Return true when device is powered down, false when
+  /// powered up. If this function is not implemented or
+  /// the mode cannot be determined, return false.
+  /// Default implementation returns false.
+  virtual bool is_powered_down();
+
   ///////////////////////////////////////////////
   // Support for tunnelled devices
 
@@ -205,14 +227,6 @@ public:
   virtual void release(const smart_device * dev);
 
 protected:
-  /// Set dynamic downcast for ATA
-  void this_is_ata(ata_device * ata);
-    // {see below;}
-
-  /// Set dynamic downcast for SCSI
-  void this_is_scsi(scsi_device * scsi);
-    // {see below;}
-
   /// Get interface which produced this object.
   smart_interface * smi()
     { return m_intf; }
@@ -224,9 +238,19 @@ protected:
 private:
   smart_interface * m_intf;
   device_info m_info;
+  error_info m_err;
+
+  // Pointers for to_ata(), to_scsi(), to_nvme()
+  // set by ATA/SCSI/NVMe interface classes.
+  friend class ata_device;
   ata_device * m_ata_ptr;
+  friend class scsi_device;
   scsi_device * m_scsi_ptr;
-  error_info m_err;
+  friend class nvme_device;
+  nvme_device * m_nvme_ptr;
+
+  // Number of objects.
+  static int s_num_objects;
 
   // Prevent copy/assigment
   smart_device(const smart_device &);
@@ -237,7 +261,7 @@ private:
 /////////////////////////////////////////////////////////////////////////////
 // ATA specific interface
 
-/// ATA register value and info whether is has been ever set
+/// ATA register value and info whether it has ever been set
 // (Automatically set by first assignment)
 class ata_register
 {
@@ -245,8 +269,8 @@ public:
   ata_register()
     : m_val(0x00), m_is_set(false) { }
 
-  ata_register & operator=(unsigned char val)
-    { m_val = val; m_is_set = true; return * this; }
+  ata_register & operator=(unsigned char x)
+    { m_val = x; m_is_set = true; return * this; }
 
   unsigned char val() const
     { return m_val; }
@@ -306,9 +330,9 @@ public:
   ata_reg_alias_16(ata_register & lo, ata_register & hi)
     : m_lo(lo), m_hi(hi) { }
 
-  ata_reg_alias_16 & operator=(unsigned short val)
-    { m_lo = (unsigned char) val;
-      m_hi = (unsigned char)(val >> 8);
+  ata_reg_alias_16 & operator=(unsigned short x)
+    { m_lo = (unsigned char) x;
+      m_hi = (unsigned char)(x >> 8);
       return * this;                   }
 
   unsigned short val() const
@@ -325,6 +349,50 @@ private:
 };
 
 
+/// 48-bit alias to six 8-bit ATA registers (for LBA).
+class ata_reg_alias_48
+{
+public:
+  ata_reg_alias_48(ata_register & ll, ata_register & lm, ata_register & lh,
+                   ata_register & hl, ata_register & hm, ata_register & hh)
+    : m_ll(ll), m_lm(lm), m_lh(lh),
+      m_hl(hl), m_hm(hm), m_hh(hh)
+    { }
+
+  ata_reg_alias_48 & operator=(uint64_t x)
+    {
+      m_ll = (unsigned char) x;
+      m_lm = (unsigned char)(x >>  8);
+      m_lh = (unsigned char)(x >> 16);
+      m_hl = (unsigned char)(x >> 24);
+      m_hm = (unsigned char)(x >> 32);
+      m_hh = (unsigned char)(x >> 40);
+      return * this;
+    }
+
+  uint64_t val() const
+    {
+      return (   (unsigned)m_ll
+              | ((unsigned)m_lm <<  8)
+              | ((unsigned)m_lh << 16)
+              | ((unsigned)m_hl << 24)
+              | ((uint64_t)m_hm << 32)
+              | ((uint64_t)m_hh << 40));
+    }
+
+  operator uint64_t() const
+    { return val(); }
+
+private:
+  ata_register & m_ll, & m_lm, & m_lh,
+               & m_hl, & m_hm, & m_hh;
+
+  // References must not be copied.
+  ata_reg_alias_48(const ata_reg_alias_48 &);
+  void operator=(const ata_reg_alias_48 &);
+};
+
+
 /// ATA Input registers for 48-bit commands
 // See section 4.14 of T13/1532D Volume 1 Revision 4b
 //
@@ -348,6 +416,9 @@ struct ata_in_regs_48bit
   ata_reg_alias_16 lba_mid_16;
   ata_reg_alias_16 lba_high_16;
 
+  // 48-bit alias to all 8-bit LBA registers.
+  ata_reg_alias_48 lba_48;
+
   /// Return true if 48-bit command
   bool is_48bit_cmd() const
     { return prev.is_set(); }
@@ -373,6 +444,9 @@ struct ata_out_regs_48bit
   ata_reg_alias_16 lba_mid_16;
   ata_reg_alias_16 lba_high_16;
 
+  // 48-bit alias to all 8-bit LBA registers.
+  ata_reg_alias_48 lba_48;
+
   ata_out_regs_48bit();
 };
 
@@ -462,17 +536,44 @@ public:
   virtual bool ata_identify_is_cached() const;
 
 protected:
+  /// Flags for ata_cmd_is_supported().
+  enum {
+    supports_data_out = 0x01, // PIO DATA OUT
+    supports_smart_status = 0x02, // read output registers for SMART STATUS only
+    supports_output_regs = 0x04, // read output registers for all commands
+    supports_multi_sector = 0x08, // more than one sector (1 DRQ/sector variant)
+    supports_48bit_hi_null = 0x10, // 48-bit commands with null high bytes only
+    supports_48bit = 0x20, // all 48-bit commands
+  };
+
   /// Check command input parameters.
+  /// Return false if required features are not implemented.
   /// Calls set_err(...) accordingly.
+  bool ata_cmd_is_supported(const ata_cmd_in & in, unsigned flags,
+    const char * type = 0);
+
+  /// Check command input parameters (old version).
+  // TODO: Remove if no longer used.
   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);
+    bool ata_48bit_support = false)
+    {
+      return ata_cmd_is_supported(in,
+        (data_out_support ? supports_data_out : 0) |
+        supports_output_regs |
+        (multi_sector_support ? supports_multi_sector : 0) |
+        (ata_48bit_support ? supports_48bit : 0));
+    }
+
+  /// Hide/unhide ATA interface.
+  void hide_ata(bool hide = true)
+    { m_ata_ptr = (!hide ? this : 0); }
 
   /// Default constructor, registers device as ATA.
   ata_device()
     : smart_device(never_called)
-    { this_is_ata(this); }
+    { hide_ata(false); }
 };
 
 
@@ -491,28 +592,103 @@ public:
   virtual bool scsi_pass_through(scsi_cmnd_io * iop) = 0;
 
 protected:
+  /// Hide/unhide SCSI interface.
+  void hide_scsi(bool hide = true)
+    { m_scsi_ptr = (!hide ? this : 0); }
+
   /// Default constructor, registers device as SCSI.
   scsi_device()
     : smart_device(never_called)
-    { this_is_scsi(this); }
+    { hide_scsi(false); }
 };
 
 
 /////////////////////////////////////////////////////////////////////////////
+// NVMe specific interface
+
+/// NVMe pass through input parameters
+struct nvme_cmd_in
+{
+  unsigned char opcode; ///< Opcode (CDW0 07:00)
+  unsigned nsid; ///< Namespace ID
+  unsigned cdw10, cdw11, cdw12, cdw13, cdw14, cdw15; ///< Cmd specific
 
-// Set dynamic downcasts
-// Note that due to virtual inheritance,
-// (ata == this) does not imply ((void*)ata == (void*)this))
+  void * buffer; ///< Pointer to data buffer
+  unsigned size; ///< Size of buffer
+
+  enum {
+    no_data = 0x0, data_out = 0x1, data_in = 0x2, data_io = 0x3
+  };
+
+  /// Get I/O direction from opcode
+  unsigned char direction() const
+    { return (opcode & 0x3); }
+
+  // Prepare for DATA IN command
+  void set_data_in(unsigned char op, void * buf, unsigned sz)
+    {
+      opcode = op;
+      if (direction() != data_in)
+        throw std::logic_error("invalid opcode for DATA IN");
+      buffer = buf;
+      size = sz;
+    }
+
+  nvme_cmd_in()
+    : opcode(0), nsid(0),
+      cdw10(0), cdw11(0), cdw12(0), cdw13(0), cdw14(0), cdw15(0),
+      buffer(0), size(0)
+    { }
+};
 
-inline void smart_device::this_is_ata(ata_device * ata)
+/// NVMe pass through output parameters
+struct nvme_cmd_out
 {
-  m_ata_ptr = (ata == this ? ata : 0);
-}
+   unsigned result; ///< Command specific result (DW0)
+   unsigned short status; ///< Status Field (DW3 31:17)
+   bool status_valid; ///< true if status is valid
+
+   nvme_cmd_out()
+     : result(0), status(0), status_valid(false)
+     { }
+};
 
-inline void smart_device::this_is_scsi(scsi_device * scsi)
+/// NVMe device access
+class nvme_device
+: virtual public /*extends*/ smart_device
 {
-  m_scsi_ptr = (scsi == this ? scsi : 0);
-}
+public:
+  /// NVMe pass through.
+  /// Return false on error.
+  virtual bool nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out) = 0;
+
+  /// Get namespace id.
+  unsigned get_nsid() const
+    { return m_nsid; }
+
+protected:
+  /// Hide/unhide NVMe interface.
+  void hide_nvme(bool hide = true)
+    { m_nvme_ptr = (!hide ? this : 0); }
+
+  /// Constructor requires namespace ID, registers device as NVMe.
+  explicit nvme_device(unsigned nsid)
+    : smart_device(never_called),
+      m_nsid(nsid)
+    { hide_nvme(false); }
+
+  /// Set namespace id.
+  /// Should be called in open() function if get_nsid() returns 0.
+  void set_nsid(unsigned nsid)
+    { m_nsid = nsid; }
+
+  /// Set last error number and message if pass-through returns NVMe error status.
+  /// Returns false always to allow use as a return expression.
+  bool set_nvme_err(nvme_cmd_out & out, unsigned status, const char * msg = 0);
+
+private:
+  unsigned m_nsid;
+};
 
 
 /////////////////////////////////////////////////////////////////////////////
@@ -550,8 +726,8 @@ public:
         if (m_base_dev && m_dev->owns(m_base_dev))
           m_dev->release(m_base_dev);
         delete m_dev;
+        m_dev = 0;
       }
-      m_dev = 0;
     }
 
   /// Return the pointer and release ownership.
@@ -602,6 +778,7 @@ private:
 typedef any_device_auto_ptr<smart_device> smart_device_auto_ptr;
 typedef any_device_auto_ptr<ata_device>   ata_device_auto_ptr;
 typedef any_device_auto_ptr<scsi_device>  scsi_device_auto_ptr;
+typedef any_device_auto_ptr<nvme_device>  nvme_device_auto_ptr;
 
 
 /////////////////////////////////////////////////////////////////////////////
@@ -656,6 +833,17 @@ public:
       return dev;
     }
 
+  void append(smart_device_list & devlist)
+    {
+      for (unsigned i = 0; i < devlist.size(); i++) {
+        smart_device * dev = devlist.at(i);
+        if (!dev)
+          continue;
+        push_back(dev);
+        devlist.m_list.at(i) = 0;
+      }
+    }
+
 // Implementation
 private:
   std::vector<smart_device *> m_list;
@@ -666,6 +854,10 @@ private:
 };
 
 
+/// List of types for DEVICESCAN
+typedef std::vector<std::string> smart_devtype_list;
+
+
 /////////////////////////////////////////////////////////////////////////////
 // smart_interface
 
@@ -699,6 +891,19 @@ public:
   /// TODO: Remove this hack.
   virtual std::string get_app_examples(const char * appname);
 
+  /// Get microseconds since some unspecified starting point.
+  /// Used only for command duration measurements in debug outputs.
+  /// Returns -1 if unsupported.
+  /// Default implementation uses clock_gettime(), gettimeofday() or ftime().
+  virtual int64_t get_timer_usec();
+
+  /// Disable/Enable system auto standby/sleep mode.
+  /// Return false if unsupported or if system is running
+  /// on battery.
+  /// Default implementation returns false.
+  virtual bool disable_system_auto_standby(bool disable);
+
+
   ///////////////////////////////////////////////
   // Last error information
 
@@ -714,12 +919,13 @@ public:
 
   /// Set last error number and message.
   /// Printf()-like formatting is supported.
-  void set_err(int no, const char * msg, ...)
-    __attribute__ ((format (printf, 3, 4)));
+  /// Returns false always to allow use as a return expression.
+  bool set_err(int no, const char * msg, ...)
+    __attribute_format_printf(3, 4);
 
   /// Set last error info struct.
-  void set_err(const smart_device::error_info & err)
-    { m_err = err; }
+  bool set_err(const smart_device::error_info & err)
+    { m_err = err; return false; }
 
   /// Clear last error info.
   void clear_err()
@@ -727,11 +933,11 @@ public:
 
   /// Set last error number and default message.
   /// Message is retrieved from get_msg_for_errno(no).
-  void set_err(int no);
+  bool set_err(int no);
 
   /// Set last error number and default message to any error_info.
   /// Used by set_err(no).
-  void set_err_var(smart_device::error_info * err, int no);
+  bool set_err_var(smart_device::error_info * err, int no);
 
   /// Convert error number into message, used by set_err(no).
   /// Default implementation returns strerror(no).
@@ -746,12 +952,22 @@ public:
   /// Default implementation selects between ata, scsi and custom device.
   virtual smart_device * get_smart_device(const char * name, const char * type);
 
-  /// Fill 'devlist' with devices of some 'type' with devices names.
+  /// Fill 'devlist' with devices of some 'type' with device names
   /// specified by some optional 'pattern'.
+  /// Use platform specific default if 'type' is empty or 0.
   /// Return false on error.
   virtual bool scan_smart_devices(smart_device_list & devlist, const char * type,
     const char * pattern = 0) = 0;
 
+  /// Fill 'devlist' with devices of all 'types' with device names
+  /// specified by some optional 'pattern'.
+  /// Use platform specific default if 'types' is empty.
+  /// Return false on error.
+  /// Default implementation calls above function for all types
+  /// and concatenates the results.
+  virtual bool scan_smart_devices(smart_device_list & devlist,
+    const smart_devtype_list & types, const char * pattern = 0);
+
 protected:
   /// Return standard ATA device.
   virtual ata_device * get_ata_device(const char * name, const char * type) = 0;
@@ -759,6 +975,11 @@ protected:
   /// Return standard SCSI device.
   virtual scsi_device * get_scsi_device(const char * name, const char * type) = 0;
 
+  /// Return standard NVMe device.
+  /// Default implementation returns 0.
+  virtual nvme_device * get_nvme_device(const char * name, const char * type,
+    unsigned nsid);
+
   /// Autodetect device if no device type specified.
   virtual smart_device * autodetect_smart_device(const char * name) = 0;
 
@@ -771,7 +992,9 @@ protected:
   /// Default implementation returns empty string.
   virtual std::string get_valid_custom_dev_types_str();
 
-  /// Return ATA->SCSI filter for SAT or USB.
+  /// Return ATA->SCSI filter for a SAT or USB 'type'.
+  /// Device 'scsidev' is used for SCSI access.
+  /// Return 0 and delete 'scsidev' on error.
   /// Override only if platform needs special handling.
   virtual ata_device * get_sat_device(const char * type, scsi_device * scsidev);
   //{ implemented in scsiata.cpp }