smartmontools SVN Rev 5396
Utility to control and monitor storage systems with "S.M.A.R.T."
dev_interface.h
Go to the documentation of this file.
1/*
2 * dev_interface.h
3 *
4 * Home page of code is: https://www.smartmontools.org
5 *
6 * Copyright (C) 2008-21 Christian Franke
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11#ifndef DEV_INTERFACE_H
12#define DEV_INTERFACE_H
13
14#define DEV_INTERFACE_H_CVSID "$Id: dev_interface.h 5393 2022-05-29 05:08:10Z dpgilbert $\n"
15
16#include "utility.h"
17
18#include <stdexcept>
19#include <string>
20#include <vector>
21
22/////////////////////////////////////////////////////////////////////////////
23// Common functionality for all device types
24
25// Forward declarations
26class smart_interface;
27class ata_device;
28class scsi_device;
29class nvme_device;
30
31/// Base class for all devices
33{
34// Types
35public:
36 /// Device info strings
37 struct device_info {
39 { }
40 device_info(const char * d_name, const char * d_type, const char * r_type)
41 : dev_name(d_name), info_name(d_name),
42 dev_type(d_type), req_type(r_type)
43 { }
44
45 std::string dev_name; ///< Device (path)name
46 std::string info_name; ///< Informal name
47 std::string dev_type; ///< Actual device type
48 std::string req_type; ///< Device type requested by user, empty if none
49 };
50
51 /// Error (number,message) pair
52 struct error_info {
53 explicit error_info(int n = 0)
54 : no(n) { }
55 error_info(int n, const char * m)
56 : no(n), msg(m) { }
57 void clear()
58 { no = 0; msg.erase(); }
59
60 int no; ///< Error number
61 std::string msg; ///< Error message
62 };
63
64// Construction
65protected:
66 /// Constructor to init interface and device info.
67 /// Must be called in implementation classes.
68 smart_device(smart_interface * intf, const char * dev_name,
69 const char * dev_type, const char * req_type);
70
71 /// Dummy enum for dummy constructor.
73 /// Dummy constructor for abstract classes.
74 /// Must never be called in implementation classes.
76
77public:
78 virtual ~smart_device();
79
80// Attributes
81public:
82 ///////////////////////////////////////////////
83 // Dynamic downcasts to actual device flavor
84
85 /// Return true if ATA device
86 bool is_ata() const
87 { return !!m_ata_ptr; }
88 /// Return true if SCSI device
89 bool is_scsi() const
90 { return !!m_scsi_ptr; }
91 /// Return true if NVMe device
92 bool is_nvme() const
93 { return !!m_nvme_ptr; }
94
95 /// Downcast to ATA device.
97 { return m_ata_ptr; }
98 /// Downcast to ATA device (const).
99 const ata_device * to_ata() const
100 { return m_ata_ptr; }
101 /// Downcast to SCSI device.
103 { return m_scsi_ptr; }
104 /// Downcast to SCSI device (const).
105 const scsi_device * to_scsi() const
106 { return m_scsi_ptr; }
107 /// Downcast to NVMe device.
109 { return m_nvme_ptr; }
110 /// Downcast to NVMe device (const).
111 const nvme_device * to_nvme() const
112 { return m_nvme_ptr; }
113
114 ///////////////////////////////////////////////
115 // Device information
116
117 /// Get device info struct.
118 const device_info & get_info() const
119 { return m_info; }
120
121 /// Get device (path)name.
122 const char * get_dev_name() const
123 { return m_info.dev_name.c_str(); }
124 /// Get informal name.
125 const char * get_info_name() const
126 { return m_info.info_name.c_str(); }
127 /// Get device type.
128 const char * get_dev_type() const
129 { return m_info.dev_type.c_str(); }
130 /// Get type requested by user, empty if none.
131 const char * get_req_type() const
132 { return m_info.req_type.c_str(); }
133
134protected:
135 /// R/W access to device info struct.
137 { return m_info; }
138
139public:
140 ///////////////////////////////////////////////
141 // Last error information
142
143 /// Get last error info struct.
144 const error_info & get_err() const
145 { return m_err; }
146 /// Get last error number.
147 int get_errno() const
148 { return m_err.no; }
149 /// Get last error message.
150 const char * get_errmsg() const
151 { return m_err.msg.c_str(); }
152
153 /// Return true if last error indicates an unsupported system call.
154 /// Default implementation returns true on ENOSYS and ENOTSUP.
155 virtual bool is_syscall_unsup() const;
156
157 /// Set last error number and message.
158 /// Printf()-like formatting is supported.
159 /// Returns false always to allow use as a return expression.
160 bool set_err(int no, const char * msg, ...)
162
163 /// Set last error info struct.
164 bool set_err(const error_info & err)
165 { m_err = err; return false; }
166
167 /// Clear last error info.
169 { m_err.clear(); }
170
171 /// Set last error number and default message.
172 /// Message is retrieved from interface's get_msg_for_errno(no).
173 bool set_err(int no);
174
175 /// Get current number of allocated 'smart_device' objects.
176 static int get_num_objects()
177 { return s_num_objects; }
178
179// Operations
180public:
181 ///////////////////////////////////////////////
182 // Device open/close
183 // Must be implemented in derived class
184
185 /// Return true if device is open.
186 virtual bool is_open() const = 0;
187
188 /// Open device, return false on error.
189 virtual bool open() = 0;
190
191 /// Close device, return false on error.
192 virtual bool close() = 0;
193
194 /// Open device with autodetection support.
195 /// May return another device for further access.
196 /// In this case, the original pointer is no longer valid.
197 /// Default implementation calls 'open()' and returns 'this'.
198 virtual smart_device * autodetect_open();
199
200 ///////////////////////////////////////////////
201 // Support for checking power mode reported by operating system
202
203 /// Early test if device is powered up or down.
204 /// Can be used without calling 'open()' first!
205 /// Return true when device is powered down, false when
206 /// powered up. If this function is not implemented or
207 /// the mode cannot be determined, return false.
208 /// Default implementation returns false.
209 virtual bool is_powered_down();
210
211 ///////////////////////////////////////////////
212 // Support for tunnelled devices
213
214 /// Return true if other device is owned by this device.
215 /// Default implementation returns false.
216 virtual bool owns(const smart_device * dev) const;
217
218 /// Release ownership of other device.
219 /// Default implementation does nothing.
220 virtual void release(const smart_device * dev);
221
222protected:
223 /// Get interface which produced this object.
225 { return m_intf; }
226 /// Get interface which produced this object (const).
227 const smart_interface * smi() const
228 { return m_intf; }
229
230// Implementation
231private:
235
236 // Pointers for to_ata(), to_scsi(), to_nvme()
237 // set by ATA/SCSI/NVMe interface classes.
238 friend class ata_device;
240 friend class scsi_device;
242 friend class nvme_device;
244
245 // Number of objects.
246 static int s_num_objects;
247
248 // Prevent copy/assignment
250 void operator=(const smart_device &);
251};
252
253
254/////////////////////////////////////////////////////////////////////////////
255// ATA specific interface
256
257/// ATA register value and info whether it has ever been set
258// (Automatically set by first assignment)
260{
261public:
263 : m_val(0x00), m_is_set(false) { }
264
265 ata_register & operator=(unsigned char x)
266 { m_val = x; m_is_set = true; return * this; }
267
268 unsigned char val() const
269 { return m_val; }
270 operator unsigned char() const
271 { return m_val; }
272
273 bool is_set() const
274 { return m_is_set; }
275
276private:
277 unsigned char m_val; ///< Register value
278 bool m_is_set; ///< true if set
279};
280
281/// ATA Input registers (for 28-bit commands)
283{
284 // ATA-6/7 register names // ATA-3/4/5 // ATA-8
285 ata_register features; // features // features
286 ata_register sector_count; // sector count // count
287 ata_register lba_low; // sector number // ]
288 ata_register lba_mid; // cylinder low // ] lba
289 ata_register lba_high; // cylinder high // ]
290 ata_register device; // device/head // device
291 ata_register command; // command // command
292
293 /// Return true if any register is set
294 bool is_set() const
295 { return (features.is_set() || sector_count.is_set()
297 || device.is_set() || command.is_set()); }
298};
299
300/// ATA Output registers (for 28-bit commands)
302{
310
311 /// Return true if any register is set
312 bool is_set() const
313 { return (error.is_set() || sector_count.is_set()
315 || device.is_set() || status.is_set()); }
316};
317
318
319/// 16-bit alias to a 8-bit ATA register pair.
321{
322public:
324 : m_lo(lo), m_hi(hi) { }
325
326 ata_reg_alias_16 & operator=(unsigned short x)
327 { m_lo = (unsigned char) x;
328 m_hi = (unsigned char)(x >> 8);
329 return * this; }
330
331 unsigned short val() const
332 { return m_lo | (m_hi << 8); }
333 operator unsigned short() const
334 { return m_lo | (m_hi << 8); }
335
336private:
338
339 // References must not be copied.
342};
343
344
345/// 48-bit alias to six 8-bit ATA registers (for LBA).
347{
348public:
350 ata_register & hl, ata_register & hm, ata_register & hh)
351 : m_ll(ll), m_lm(lm), m_lh(lh),
352 m_hl(hl), m_hm(hm), m_hh(hh)
353 { }
354
356 {
357 m_ll = (unsigned char) x;
358 m_lm = (unsigned char)(x >> 8);
359 m_lh = (unsigned char)(x >> 16);
360 m_hl = (unsigned char)(x >> 24);
361 m_hm = (unsigned char)(x >> 32);
362 m_hh = (unsigned char)(x >> 40);
363 return * this;
364 }
365
366 uint64_t val() const
367 {
368 return ( (unsigned)m_ll
369 | ((unsigned)m_lm << 8)
370 | ((unsigned)m_lh << 16)
371 | ((unsigned)m_hl << 24)
372 | ((uint64_t)m_hm << 32)
373 | ((uint64_t)m_hh << 40));
374 }
375
376 operator uint64_t() const
377 { return val(); }
378
379private:
381 & m_hl, & m_hm, & m_hh;
382
383 // References must not be copied.
386};
387
388
389/// ATA Input registers for 48-bit commands
390// See section 4.14 of T13/1532D Volume 1 Revision 4b
391//
392// Uses ATA-6/7 method to specify 16-bit registers as
393// recent (low byte) and previous (high byte) content of
394// 8-bit registers.
395//
396// (ATA-8 ACS does not longer follow this scheme, it uses
397// abstract registers with sufficient size and leaves the
398// actual mapping to the transport layer.)
399//
401: public ata_in_regs // "most recently written" registers
402{
403 ata_in_regs prev; ///< "previous content"
404
405 // 16-bit aliases for above pair.
411
412 // 48-bit alias to all 8-bit LBA registers.
414
415 /// Return true if 48-bit command
416 bool is_48bit_cmd() const
417 { return prev.is_set(); }
418
419 /// Return true if 48-bit command with any nonzero high byte
420 bool is_real_48bit_cmd() const
421 { return ( prev.features || prev.sector_count
422 || prev.lba_low || prev.lba_mid || prev.lba_high); }
423
425};
426
427
428/// ATA Output registers for 48-bit commands
430: public ata_out_regs // read with HOB=0
431{
432 ata_out_regs prev; ///< read with HOB=1
433
434 // 16-bit aliases for above pair.
439
440 // 48-bit alias to all 8-bit LBA registers.
442
444};
445
446
447/// Flags for each ATA output register
449{
451
452 /// Return true if any flag is set.
453 bool is_set() const
454 { return ( error || sector_count || lba_low
455 || lba_mid || lba_high || device || status); }
456
457 /// Default constructor clears all flags.
459 : error(false), sector_count(false), lba_low(false), lba_mid(false),
460 lba_high(false), device(false), status(false) { }
461};
462
463
464/// ATA pass through input parameters
466{
467 ata_in_regs_48bit in_regs; ///< Input registers
468 ata_out_regs_flags out_needed; ///< True if output register value needed
469 enum { no_data = 0, data_in, data_out } direction; ///< I/O direction
470 void * buffer; ///< Pointer to data buffer
471 unsigned size; ///< Size of buffer
472
473 /// Prepare for 28-bit DATA IN command
474 void set_data_in(void * buf, unsigned nsectors)
475 {
476 buffer = buf;
477 in_regs.sector_count = nsectors;
479 size = nsectors * 512;
480 }
481
482 /// Prepare for 28-bit DATA OUT command
483 void set_data_out(const void * buf, unsigned nsectors)
484 {
485 buffer = const_cast<void *>(buf);
486 in_regs.sector_count = nsectors;
488 size = nsectors * 512;
489 }
490
491 /// Prepare for 48-bit DATA IN command
492 void set_data_in_48bit(void * buf, unsigned nsectors)
493 {
494 buffer = buf;
495 // Note: This also sets 'in_regs.is_48bit_cmd()'
496 in_regs.sector_count_16 = nsectors;
498 size = nsectors * 512;
499 }
500
501 ata_cmd_in();
502};
503
504/// ATA pass through output parameters
506{
507 ata_out_regs_48bit out_regs; ///< Output registers
508
509 ata_cmd_out();
510};
511
512/// ATA device access
514: virtual public /*extends*/ smart_device
515{
516public:
517 /// ATA pass through.
518 /// Return false on error.
519 /// Must be implemented in derived class.
520 virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) = 0;
521
522 /// ATA pass through without output registers.
523 /// Return false on error.
524 /// Calls ata_pass_through(in, dummy), cannot be reimplemented.
525 bool ata_pass_through(const ata_cmd_in & in);
526
527 /// Return true if OS caches ATA identify sector.
528 /// Default implementation returns false.
529 virtual bool ata_identify_is_cached() const;
530
531protected:
532 /// Flags for ata_cmd_is_supported().
533 enum {
534 supports_data_out = 0x01, // PIO DATA OUT
535 supports_smart_status = 0x02, // read output registers for SMART STATUS only
536 supports_output_regs = 0x04, // read output registers for all commands
537 supports_multi_sector = 0x08, // more than one sector (1 DRQ/sector variant)
538 supports_48bit_hi_null = 0x10, // 48-bit commands with null high bytes only
539 supports_48bit = 0x20, // all 48-bit commands
540 };
541
542 /// Check command input parameters.
543 /// Return false if required features are not implemented.
544 /// Calls set_err(...) accordingly.
545 bool ata_cmd_is_supported(const ata_cmd_in & in, unsigned flags,
546 const char * type = 0);
547
548 /// Check command input parameters (old version).
549 // TODO: Remove if no longer used.
550 bool ata_cmd_is_ok(const ata_cmd_in & in,
551 bool data_out_support = false,
552 bool multi_sector_support = false,
553 bool ata_48bit_support = false)
554 {
555 return ata_cmd_is_supported(in,
556 (data_out_support ? supports_data_out : 0) |
558 (multi_sector_support ? supports_multi_sector : 0) |
559 (ata_48bit_support ? supports_48bit : 0));
560 }
561
562 /// Hide/unhide ATA interface.
563 void hide_ata(bool hide = true)
564 { m_ata_ptr = (!hide ? this : 0); }
565
566 /// Default constructor, registers device as ATA.
569 { hide_ata(false); }
570};
571
572
573/////////////////////////////////////////////////////////////////////////////
574// SCSI specific interface
575
576struct scsi_cmnd_io;
577
579 uint8_t cdb0;
580 uint8_t sa_valid;
581 uint16_t sa;
582};
583
584/// SCSI device access
586: virtual public /*extends*/ smart_device
587{
588public:
589 /// SCSI pass through.
590 /// Returns false on error.
591 virtual bool scsi_pass_through(scsi_cmnd_io * iop) = 0;
592
593 // Call scsi_pass_through and check sense.
595 const char * msg = "");
596
597 /// Always try READ CAPACITY(10) (rcap10) first but once we know
598 /// rcap16 is needed, use it instead.
600 { rcap16_first = true; }
601
602 bool use_rcap16() const
603 { return rcap16_first; }
604
606
607 bool is_spc4_or_higher() const { return spc4_or_above; }
608
609protected:
610 /// Hide/unhide SCSI interface.
611 void hide_scsi(bool hide = true)
612 { m_scsi_ptr = (!hide ? this : 0); }
613
614 /// Default constructor, registers device as SCSI.
617 rcap16_first(false),
618 spc4_or_above(false)
619 { hide_scsi(false); }
620
621private:
624 /* rsoc: report supported operation codes (command) */
625 std::vector<scsi_rsoc_elem> rsoc_list;
626};
627
628
629/////////////////////////////////////////////////////////////////////////////
630// NVMe specific interface
631
632/// NVMe pass through input parameters
634{
635 unsigned char opcode; ///< Opcode (CDW0 07:00)
636 unsigned nsid; ///< Namespace ID
637 unsigned cdw10, cdw11, cdw12, cdw13, cdw14, cdw15; ///< Cmd specific
638
639 void * buffer; ///< Pointer to data buffer
640 unsigned size; ///< Size of buffer
641
642 enum {
643 no_data = 0x0, data_out = 0x1, data_in = 0x2, data_io = 0x3
644 };
645
646 /// Get I/O direction from opcode
647 unsigned char direction() const
648 { return (opcode & 0x3); }
649
650 // Prepare for DATA IN command
651 void set_data_in(unsigned char op, void * buf, unsigned sz)
652 {
653 opcode = op;
654 if (direction() != data_in)
655 throw std::logic_error("invalid opcode for DATA IN");
656 buffer = buf;
657 size = sz;
658 }
659
661 : opcode(0), nsid(0),
662 cdw10(0), cdw11(0), cdw12(0), cdw13(0), cdw14(0), cdw15(0),
663 buffer(0), size(0)
664 { }
665};
666
667/// NVMe pass through output parameters
669{
670 unsigned result; ///< Command specific result (DW0)
671 unsigned short status; ///< Status Field (DW3 31:17)
672 bool status_valid; ///< true if status is valid
673
675 : result(0), status(0), status_valid(false)
676 { }
677};
678
679/// NVMe device access
681: virtual public /*extends*/ smart_device
682{
683public:
684 /// NVMe pass through.
685 /// Return false on error.
686 virtual bool nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out) = 0;
687
688 /// Get namespace id.
689 unsigned get_nsid() const
690 { return m_nsid; }
691
692protected:
693 /// Hide/unhide NVMe interface.
694 void hide_nvme(bool hide = true)
695 { m_nvme_ptr = (!hide ? this : 0); }
696
697 /// Constructor requires namespace ID, registers device as NVMe.
698 explicit nvme_device(unsigned nsid)
700 m_nsid(nsid)
701 { hide_nvme(false); }
702
703 /// Set namespace id.
704 /// Should be called in open() function if get_nsid() returns 0.
705 void set_nsid(unsigned nsid)
706 { m_nsid = nsid; }
707
708 /// Set last error number and message if pass-through returns NVMe error status.
709 /// Returns false always to allow use as a return expression.
710 bool set_nvme_err(nvme_cmd_out & out, unsigned status, const char * msg = 0);
711
712private:
713 unsigned m_nsid;
714};
715
716
717/////////////////////////////////////////////////////////////////////////////
718/// Smart pointer class for device pointers
719
720template <class Dev>
722{
723public:
724 typedef Dev device_type;
725
726 /// Construct from optional pointer to device
727 /// and optional pointer to base device.
729 smart_device * base_dev = 0)
730 : m_dev(dev), m_base_dev(base_dev) { }
731
732 /// Destructor deletes device object.
734 { reset(); }
735
736 /// Assign a new pointer.
737 /// Throws if a pointer is already assigned.
739 {
740 if (m_dev)
741 fail();
742 m_dev = dev;
743 }
744
745 /// Delete device object and clear the pointer.
746 void reset()
747 {
748 if (m_dev) {
749 if (m_base_dev && m_dev->owns(m_base_dev))
750 m_dev->release(m_base_dev);
751 delete m_dev;
752 m_dev = 0;
753 }
754 }
755
756 /// Return the pointer and release ownership.
758 {
759 device_type * dev = m_dev;
760 m_dev = 0;
761 return dev;
762 }
763
764 /// Replace the pointer.
765 /// Used to call dev->autodetect_open().
767 { m_dev = dev; }
768
769 /// Return the pointer.
770 device_type * get() const
771 { return m_dev; }
772
773 /// Pointer dereferencing.
775 { return *m_dev; }
776
777 /// Pointer dereferencing.
779 { return m_dev; }
780
781 /// For (ptr != 0) check.
782 operator bool() const
783 { return !!m_dev; }
784
785 /// For (ptr == 0) check.
786 bool operator !() const
787 { return !m_dev; }
788
789private:
792
793 void fail() const
794 { throw std::logic_error("any_device_auto_ptr: wrong usage"); }
795
796 // Prevent copy/assignment
799};
800
805
806
807/////////////////////////////////////////////////////////////////////////////
808// smart_device_list
809
810/// List of devices for DEVICESCAN
812{
813// Construction
814public:
816 { }
817
819 {
820 for (unsigned i = 0; i < m_list.size(); i++)
821 delete m_list[i];
822 }
823
824// Attributes
825 unsigned size() const
826 { return m_list.size(); }
827
828// Operations
829 void clear()
830 {
831 for (unsigned i = 0; i < m_list.size(); i++)
832 delete m_list[i];
833 m_list.clear();
834 }
835
836
838 { m_list.push_back(dev); }
839
841 {
842 m_list.push_back(dev.get());
843 dev.release();
844 }
845
846 smart_device * at(unsigned i)
847 { return m_list.at(i); }
848
849 const smart_device * at(unsigned i) const
850 { return m_list.at(i); }
851
852 smart_device * release(unsigned i)
853 {
854 smart_device * dev = m_list.at(i);
855 m_list[i] = 0;
856 return dev;
857 }
858
859 void append(smart_device_list & devlist)
860 {
861 for (unsigned i = 0; i < devlist.size(); i++) {
862 smart_device * dev = devlist.at(i);
863 if (!dev)
864 continue;
865 push_back(dev);
866 devlist.m_list.at(i) = 0;
867 }
868 }
869
870// Implementation
871private:
872 std::vector<smart_device *> m_list;
873
874 // Prevent copy/assignment
877};
878
879
880/// List of types for DEVICESCAN
881typedef std::vector<std::string> smart_devtype_list;
882
883
884/////////////////////////////////////////////////////////////////////////////
885// smart_interface
886
887/// The platform interface abstraction
889{
890public:
891 /// Initialize platform interface and register with smi().
892 /// Must be implemented by platform module and register interface with set()
893 static void init();
894
896 { }
897
899 { }
900
901 /// Return info string about build host and/or OS version.
902 /// Default implementation returns SMARTMONTOOLS_BUILD_HOST.
903 virtual std::string get_os_version_str();
904
905 /// Return valid args for device type option/directive.
906 /// Default implementation returns "ata, scsi, sat, usb*..."
907 /// concatenated with result from get_valid_custom_dev_types_str().
908 virtual std::string get_valid_dev_types_str();
909
910 /// Return example string for program 'appname'.
911 /// Default implementation returns empty string.
912 /// For the migration of print_smartctl_examples(),
913 /// function is allowed to print examples to stdout.
914 /// TODO: Remove this hack.
915 virtual std::string get_app_examples(const char * appname);
916
917 /// Disable/Enable system auto standby/sleep mode.
918 /// Return false if unsupported or if system is running
919 /// on battery.
920 /// Default implementation returns false.
921 virtual bool disable_system_auto_standby(bool disable);
922
923
924 ///////////////////////////////////////////////
925 // Last error information
926
927 /// Get last error info struct.
929 { return m_err; }
930 /// Get last error number.
931 int get_errno() const
932 { return m_err.no; }
933 /// Get last error message.
934 const char * get_errmsg() const
935 { return m_err.msg.c_str(); }
936
937 /// Set last error number and message.
938 /// Printf()-like formatting is supported.
939 /// Returns false always to allow use as a return expression.
940 bool set_err(int no, const char * msg, ...)
942
943 /// Set last error info struct.
944 bool set_err(const smart_device::error_info & err)
945 { m_err = err; return false; }
946
947 /// Clear last error info.
949 { m_err.clear(); }
950
951 /// Set last error number and default message.
952 /// Message is retrieved from get_msg_for_errno(no).
953 bool set_err(int no);
954
955 /// Set last error number and default message to any error_info.
956 /// Used by set_err(no).
957 bool set_err_var(smart_device::error_info * err, int no);
958
959 /// Convert error number into message, used by set_err(no).
960 /// Default implementation returns strerror(no).
961 virtual const char * get_msg_for_errno(int no);
962
963 ///////////////////////////////////////////////////////////////////////////
964 // Device factory:
965
966 /// Return device object for device 'name' with some 'type'.
967 /// 'type' is 0 if not specified by user.
968 /// Return 0 on error.
969 /// Default implementation selects between ata, scsi and custom device.
970 virtual smart_device * get_smart_device(const char * name, const char * type);
971
972 /// Fill 'devlist' with devices of some 'type' with device names
973 /// specified by some optional 'pattern'.
974 /// Use platform specific default if 'type' is empty or 0.
975 /// Return false on error.
976 /// Default implementation returns false;
977 virtual bool scan_smart_devices(smart_device_list & devlist, const char * type,
978 const char * pattern = 0);
979
980 /// Fill 'devlist' with devices of all 'types' with device names
981 /// specified by some optional 'pattern'.
982 /// Use platform specific default if 'types' is empty.
983 /// Return false on error.
984 /// Default implementation calls above function for all types
985 /// and concatenates the results.
986 virtual bool scan_smart_devices(smart_device_list & devlist,
987 const smart_devtype_list & types, const char * pattern = 0);
988
989 /// Return unique device name which is (only) suitable for duplicate detection.
990 /// Default implementation resolves symlinks on POSIX systems and appends
991 /// " [type]" if is_raid_dev_type(type)' returns true.
992 virtual std::string get_unique_dev_name(const char * name, const char * type) const;
993
994 /// Return true if the 'type' string contains a RAID drive number.
995 /// Default implementation returns true if 'type' starts with '[^,]+,[0-9]'
996 /// but not with 'sat,'.
997 virtual bool is_raid_dev_type(const char * type) const;
998
999protected:
1000 /// Return standard ATA device.
1001 virtual ata_device * get_ata_device(const char * name, const char * type) = 0;
1002
1003 /// Return standard SCSI device.
1004 virtual scsi_device * get_scsi_device(const char * name, const char * type) = 0;
1005
1006 /// Return standard NVMe device.
1007 /// Default implementation returns 0.
1008 virtual nvme_device * get_nvme_device(const char * name, const char * type,
1009 unsigned nsid);
1010
1011 /// Autodetect device if no device type specified.
1012 virtual smart_device * autodetect_smart_device(const char * name) = 0;
1013
1014 /// Return device for platform specific 'type'.
1015 /// Default implementation returns 0.
1016 virtual smart_device * get_custom_smart_device(const char * name, const char * type);
1017
1018 /// Return valid 'type' args accepted by above.
1019 /// This is called in get_valid_dev_types_str().
1020 /// Default implementation returns empty string.
1021 virtual std::string get_valid_custom_dev_types_str();
1022
1023 /// Return ATA->SCSI of NVMe->SCSI filter for a SAT, SNT or USB 'type'.
1024 /// Uses get_sat_device and get_snt_device.
1025 /// Return 0 and delete 'scsidev' on error.
1026 virtual smart_device * get_scsi_passthrough_device(const char * type, scsi_device * scsidev);
1027
1028 /// Return ATA->SCSI filter for a SAT or USB 'type'.
1029 /// Device 'scsidev' is used for SCSI access.
1030 /// Return 0 and delete 'scsidev' on error.
1031 /// Override only if platform needs special handling.
1032 virtual ata_device * get_sat_device(const char * type, scsi_device * scsidev);
1033 //{ implemented in scsiata.cpp }
1034
1035 /// Return NVMe->SCSI filter for a SNT or USB 'type'.
1036 /// Device 'scsidev' is used for SCSI access.
1037 /// Return 0 and delete 'scsidev' on error.
1038 /// Override only if platform needs special handling.
1039 virtual nvme_device * get_snt_device(const char * type, scsi_device * scsidev);
1040 //{ implemented in scsinvme.cpp }
1041
1042 /// Return filter for Intelliprop controllers.
1043 virtual ata_device * get_intelliprop_device(const char * type, ata_device * atadev);
1044 //{ implemented in dev_intelliprop.cpp }
1045
1046 /// Return JMB93x->ATA filter.
1047 /// Device 'smartdev' is used for ATA or SCSI R/W access.
1048 /// Return 0 and delete 'scsidev' on error.
1049 /// Override only if platform needs special handling.
1050 virtual ata_device * get_jmb39x_device(const char * type, smart_device * smartdev);
1051 //{ implemented in dev_jmb39x_raid.cpp }
1052
1053public:
1054 /// Try to detect a SAT device behind a SCSI interface.
1055 /// Inquiry data can be passed if available.
1056 /// Return appropriate device if yes, otherwise 0.
1057 /// Override only if platform needs special handling.
1058 virtual ata_device * autodetect_sat_device(scsi_device * scsidev,
1059 const unsigned char * inqdata, unsigned inqsize);
1060 //{ implemented in scsiata.cpp }
1061
1062 /// Get type name for USB device with known VENDOR:PRODUCT ID.
1063 /// Return name if device known and supported, otherwise 0.
1064 virtual const char * get_usb_dev_type_by_id(int vendor_id, int product_id,
1065 int version = -1);
1066 //{ implemented in scsiata.cpp }
1067
1068protected:
1069 /// Set interface to use, must be called from init().
1070 static void set(smart_interface * intf)
1071 { s_instance = intf; }
1072
1073// Implementation
1074private:
1076
1077 friend smart_interface * smi(); // below
1078 static smart_interface * s_instance; ///< Pointer to the interface object.
1079
1080 // Prevent copy/assignment
1083};
1084
1085
1086/////////////////////////////////////////////////////////////////////////////
1087// smi()
1088
1089/// Global access to the (usually singleton) smart_interface
1091 { return smart_interface::s_instance; }
1092
1093/////////////////////////////////////////////////////////////////////////////
1094
1095#endif // DEV_INTERFACE_H
Smart pointer class for device pointers.
device_type * release()
Return the pointer and release ownership.
~any_device_auto_ptr()
Destructor deletes device object.
device_type * m_dev
void reset()
Delete device object and clear the pointer.
device_type * get() const
Return the pointer.
void replace(device_type *dev)
Replace the pointer.
void operator=(const any_device_auto_ptr< Dev > &)
void operator=(device_type *dev)
Assign a new pointer.
bool operator!() const
For (ptr == 0) check.
any_device_auto_ptr(device_type *dev=0, smart_device *base_dev=0)
Construct from optional pointer to device and optional pointer to base device.
device_type * operator->() const
Pointer dereferencing.
device_type & operator*() const
Pointer dereferencing.
smart_device * m_base_dev
any_device_auto_ptr(const any_device_auto_ptr< Dev > &)
ATA device access.
ata_device()
Default constructor, registers device as ATA.
@ supports_output_regs
@ supports_48bit_hi_null
@ supports_multi_sector
@ supports_smart_status
virtual bool ata_identify_is_cached() const
Return true if OS caches ATA identify sector.
bool ata_cmd_is_supported(const ata_cmd_in &in, unsigned flags, const char *type=0)
Check command input parameters.
void hide_ata(bool hide=true)
Hide/unhide ATA interface.
virtual bool ata_pass_through(const ata_cmd_in &in, ata_cmd_out &out)=0
ATA pass through.
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).
16-bit alias to a 8-bit ATA register pair.
ata_reg_alias_16(const ata_reg_alias_16 &)
ata_register & m_hi
ata_register & m_lo
unsigned short val() const
ata_reg_alias_16(ata_register &lo, ata_register &hi)
ata_reg_alias_16 & operator=(unsigned short x)
void operator=(const ata_reg_alias_16 &)
48-bit alias to six 8-bit ATA registers (for LBA).
ata_register & m_hh
ata_register & m_ll
ata_register & m_lm
uint64_t val() const
ata_reg_alias_48 & operator=(uint64_t x)
void operator=(const ata_reg_alias_48 &)
ata_reg_alias_48(ata_register &ll, ata_register &lm, ata_register &lh, ata_register &hl, ata_register &hm, ata_register &hh)
ata_reg_alias_48(const ata_reg_alias_48 &)
ata_register & m_hl
ata_register & m_lh
ata_register & m_hm
ATA register value and info whether it has ever been set.
unsigned char val() const
bool is_set() const
ata_register & operator=(unsigned char x)
bool m_is_set
true if set
unsigned char m_val
Register value.
NVMe device access.
unsigned get_nsid() const
Get namespace id.
unsigned m_nsid
nvme_device(unsigned nsid)
Constructor requires namespace ID, registers device as NVMe.
virtual bool nvme_pass_through(const nvme_cmd_in &in, nvme_cmd_out &out)=0
NVMe pass through.
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 hide_nvme(bool hide=true)
Hide/unhide NVMe interface.
void set_nsid(unsigned nsid)
Set namespace id.
SCSI device access.
scsi_device()
Default constructor, registers device as SCSI.
bool spc4_or_above
bool is_spc4_or_higher() const
std::vector< scsi_rsoc_elem > rsoc_list
void hide_scsi(bool hide=true)
Hide/unhide SCSI interface.
void set_rcap16_first()
Always try READ CAPACITY(10) (rcap10) first but once we know rcap16 is needed, use it instead.
bool scsi_pass_through_and_check(scsi_cmnd_io *iop, const char *msg="")
virtual bool scsi_pass_through(scsi_cmnd_io *iop)=0
SCSI pass through.
bool use_rcap16() const
void set_spc4_or_higher()
List of devices for DEVICESCAN.
const smart_device * at(unsigned i) const
unsigned size() const
smart_device * at(unsigned i)
void operator=(const smart_device_list &)
void push_back(smart_device *dev)
std::vector< smart_device * > m_list
void push_back(smart_device_auto_ptr &dev)
void append(smart_device_list &devlist)
smart_device_list(const smart_device_list &)
smart_device * release(unsigned i)
Base class for all devices.
Definition: dev_interface.h:33
bool is_scsi() const
Return true if SCSI device.
Definition: dev_interface.h:89
int get_errno() const
Get last error number.
const error_info & get_err() const
Get last error info struct.
error_info m_err
virtual void release(const smart_device *dev)
Release ownership of other device.
smart_device(const smart_device &)
scsi_device * m_scsi_ptr
virtual bool is_powered_down()
Early test if device is powered up or down.
bool is_nvme() const
Return true if NVMe device.
Definition: dev_interface.h:92
const device_info & get_info() const
Get device info struct.
const smart_interface * smi() const
Get interface which produced this object (const).
nvme_device * m_nvme_ptr
smart_interface * smi()
Get interface which produced this object.
const char * get_req_type() const
Get type requested by user, empty if none.
const ata_device * to_ata() const
Downcast to ATA device (const).
Definition: dev_interface.h:99
const scsi_device * to_scsi() const
Downcast to SCSI device (const).
void operator=(const smart_device &)
const nvme_device * to_nvme() const
Downcast to NVMe device (const).
const char * get_dev_type() const
Get device type.
const char * get_errmsg() const
Get last error message.
virtual bool close()=0
Close device, return false on error.
nvme_device * to_nvme()
Downcast to NVMe device.
bool set_err(int no, const char *msg,...) __attribute_format_printf(3
Set last error number and message.
device_info m_info
ata_device * to_ata()
Downcast to ATA device.
Definition: dev_interface.h:96
do_not_use_in_implementation_classes
Dummy enum for dummy constructor.
Definition: dev_interface.h:72
const char * get_info_name() const
Get informal name.
virtual smart_device * autodetect_open()
Open device with autodetection support.
scsi_device * to_scsi()
Downcast to SCSI device.
static int get_num_objects()
Get current number of allocated 'smart_device' objects.
virtual bool owns(const smart_device *dev) const
Return true if other device is owned by this device.
virtual bool is_syscall_unsup() const
Return true if last error indicates an unsupported system call.
virtual bool is_open() const =0
Return true if device is open.
bool is_ata() const
Return true if ATA device.
Definition: dev_interface.h:86
static int s_num_objects
ata_device * m_ata_ptr
device_info & set_info()
R/W access to device info struct.
const char * get_dev_name() const
Get device (path)name.
smart_device(smart_interface *intf, const char *dev_name, const char *dev_type, const char *req_type)
Constructor to init interface and device info.
virtual bool open()=0
Open device, return false on error.
virtual ~smart_device()
void clear_err()
Clear last error info.
smart_interface * m_intf
The platform interface abstraction.
virtual std::string get_app_examples(const char *appname)
Return example string for program 'appname'.
smart_interface(const smart_interface &)
static void set(smart_interface *intf)
Set interface to use, must be called from init().
virtual const char * get_msg_for_errno(int no)
Convert error number into message, used by set_err(no).
void clear_err()
Clear last error info.
virtual nvme_device * get_snt_device(const char *type, scsi_device *scsidev)
Return NVMe->SCSI filter for a SNT or USB 'type'.
Definition: scsinvme.cpp:396
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
static smart_interface * s_instance
Pointer to the interface object.
virtual ata_device * get_jmb39x_device(const char *type, smart_device *smartdev)
Return JMB93x->ATA filter.
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
virtual std::string get_unique_dev_name(const char *name, const char *type) const
Return unique device name which is (only) suitable for duplicate detection.
virtual smart_device * get_smart_device(const char *name, const char *type)
Return device object for device 'name' with some 'type'.
smart_device::error_info m_err
virtual ata_device * get_ata_device(const char *name, const char *type)=0
Return standard ATA device.
const char * get_errmsg() const
Get last error message.
virtual ata_device * get_intelliprop_device(const char *type, ata_device *atadev)
Return filter for Intelliprop controllers.
static void init()
Initialize platform interface and register with smi().
Definition: dev_legacy.cpp:334
int get_errno() const
Get last error number.
virtual smart_device * get_custom_smart_device(const char *name, const char *type)
Return device for platform specific 'type'.
virtual std::string get_os_version_str()
Return info string about build host and/or OS version.
virtual std::string get_valid_custom_dev_types_str()
Return valid 'type' args accepted by above.
const smart_device::error_info & get_err() const
Get last error info struct.
void operator=(const smart_interface &)
virtual bool disable_system_auto_standby(bool disable)
Disable/Enable system auto standby/sleep mode.
virtual bool scan_smart_devices(smart_device_list &devlist, const char *type, const char *pattern=0)
Fill 'devlist' with devices of some 'type' with device names specified by some optional 'pattern'.
virtual smart_device * autodetect_smart_device(const char *name)=0
Autodetect device if no device type specified.
virtual std::string get_valid_dev_types_str()
Return valid args for device type option/directive.
virtual scsi_device * get_scsi_device(const char *name, const char *type)=0
Return standard SCSI device.
friend smart_interface * smi()
Global access to the (usually singleton) smart_interface.
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 nvme_device * get_nvme_device(const char *name, const char *type, unsigned nsid)
Return standard NVMe device.
virtual ~smart_interface()
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_var(smart_device::error_info *err, int no)
Set last error number and default message to any error_info.
bool set_err(int no, const char *msg,...) __attribute_format_printf(3
Set last error number and message.
virtual bool is_raid_dev_type(const char *type) const
Return true if the 'type' string contains a RAID drive number.
any_device_auto_ptr< scsi_device > scsi_device_auto_ptr
std::vector< std::string > smart_devtype_list
List of types for DEVICESCAN.
any_device_auto_ptr< smart_device > smart_device_auto_ptr
any_device_auto_ptr< nvme_device > nvme_device_auto_ptr
smart_interface * smi()
Global access to the (usually singleton) smart_interface.
any_device_auto_ptr< ata_device > ata_device_auto_ptr
u16 flags
Definition: megaraid.h:14
uint32_t nsid
ATA pass through input parameters.
enum ata_cmd_in::@29 direction
I/O direction.
void set_data_out(const void *buf, unsigned nsectors)
Prepare for 28-bit DATA OUT command.
void * buffer
Pointer to data buffer.
void set_data_in_48bit(void *buf, unsigned nsectors)
Prepare for 48-bit DATA IN command.
ata_in_regs_48bit in_regs
Input registers.
unsigned size
Size of buffer.
ata_out_regs_flags out_needed
True if output register value needed.
void set_data_in(void *buf, unsigned nsectors)
Prepare for 28-bit DATA IN command.
ATA pass through output parameters.
ata_out_regs_48bit out_regs
Output registers.
ATA Input registers for 48-bit commands.
bool is_48bit_cmd() const
Return true if 48-bit command.
ata_reg_alias_16 lba_high_16
ata_reg_alias_16 sector_count_16
bool is_real_48bit_cmd() const
Return true if 48-bit command with any nonzero high byte.
ata_reg_alias_16 features_16
ata_in_regs prev
"previous content"
ata_reg_alias_16 lba_low_16
ata_reg_alias_48 lba_48
ata_reg_alias_16 lba_mid_16
ATA Input registers (for 28-bit commands)
bool is_set() const
Return true if any register is set.
ata_register device
ata_register lba_high
ata_register sector_count
ata_register lba_low
ata_register features
ata_register command
ata_register lba_mid
ATA Output registers for 48-bit commands.
ata_reg_alias_48 lba_48
ata_out_regs prev
read with HOB=1
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
Flags for each ATA output register.
ata_out_regs_flags()
Default constructor clears all flags.
bool is_set() const
Return true if any flag is set.
ATA Output registers (for 28-bit commands)
ata_register sector_count
ata_register lba_low
ata_register lba_mid
ata_register error
ata_register lba_high
bool is_set() const
Return true if any register is set.
ata_register device
ata_register status
NVMe pass through input parameters.
unsigned char direction() const
Get I/O direction from opcode.
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 set_data_in(unsigned char op, void *buf, unsigned sz)
void * buffer
Pointer to data buffer.
NVMe pass through output parameters.
bool status_valid
true if status is valid
unsigned short status
Status Field (DW3 31:17)
unsigned result
Command specific result (DW0)
Device info strings.
Definition: dev_interface.h:37
std::string req_type
Device type requested by user, empty if none.
Definition: dev_interface.h:48
std::string info_name
Informal name.
Definition: dev_interface.h:46
device_info(const char *d_name, const char *d_type, const char *r_type)
Definition: dev_interface.h:40
std::string dev_type
Actual device type.
Definition: dev_interface.h:47
std::string dev_name
Device (path)name.
Definition: dev_interface.h:45
Error (number,message) pair.
Definition: dev_interface.h:52
std::string msg
Error message.
Definition: dev_interface.h:61
error_info(int n, const char *m)
Definition: dev_interface.h:55
int no
Error number.
Definition: dev_interface.h:60
#define __attribute_format_printf(x, y)
Definition: utility.h:34