xref: /aosp_15_r20/external/sg3_utils/README.details (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1*44704f69SBart Van Assche                      README.details for sg3_utils
2*44704f69SBart Van Assche                      ============================
3*44704f69SBart Van AsscheIntroduction
4*44704f69SBart Van Assche============
5*44704f69SBart Van AsscheThis package contains low level command line utilities for devices that use
6*44704f69SBart Van Asschethe SCSI command set. Originally the SCSI command set was associated
7*44704f69SBart Van Asscheexclusively with the SCSI Parallel Interface (SPI) transport. SPI has now
8*44704f69SBart Van Asschealmost been completely replaced by the Serial Attached SCSI (SAS) transport
9*44704f69SBart Van Asschewhich also accepts the SCSI command set. Additionally many other storage
10*44704f69SBart Van Asscherelated transports use the SCSI command set (amongst others); examples are
11*44704f69SBart Van AsscheATAPI devices (CD/DVDs and tapes), USB mass storage devices (including those
12*44704f69SBart Van Asscheusing the newer UAS[P]), Fibre Channel disks, IEEE 1394 storage devices (SBP
13*44704f69SBart Van Asscheprotocol), iSCSI, FCoE and SOP devices. Even NVMe which has its own command
14*44704f69SBart Van Asscheset accepts SCSI commands in some contexts; one example is for enclosure
15*44704f69SBart Van Asschemanagement where NVME-MI has SES Send and SES Receive commands. SES refers
16*44704f69SBart Van Asscheto the SCSI Enclosure Services command set.
17*44704f69SBart Van Assche
18*44704f69SBart Van AsscheThis package originally targeted the Linux SCSI subsystem. Since most
19*44704f69SBart Van Asscheoperating systems contain a SCSI command pass-through mechanism, many
20*44704f69SBart Van Asscheutilities within this package have been ported. This README mainly
21*44704f69SBart Van Asscheconcentrates on Linux: see the README.freebsd file for the FreeBSD port,
22*44704f69SBart Van AsscheREADME.solaris for the Solaris port, the README.tru64 file for the Tru64
23*44704f69SBart Van Assche(OSF) port and README.win32 for the Windows ports (of which there are two
24*44704f69SBart Van Asschevariants).
25*44704f69SBart Van Assche
26*44704f69SBart Van AsscheMost utilities within the sg3_utils package work at the SCSI command level.
27*44704f69SBart Van AsscheFor example the sg_inq utility issues a SCSI INQUIRY command and decodes the
28*44704f69SBart Van Asscheresponse. The COVERAGE file has a table containing a row for each SCSI
29*44704f69SBart Van Asschecommand issued by this package; to the right of each row is the utility
30*44704f69SBart Van Assche(sometimes more than one) that issue that SCSI command. The COVERAGE file
31*44704f69SBart Van Asschehas a second table for ATA commands usage.
32*44704f69SBart Van Assche
33*44704f69SBart Van AsscheSome utilities interface at a slightly higher level, for example: sg_dd,
34*44704f69SBart Van Asschesgm_dd and sgp_dd. These are closely related to the Unix dd command and
35*44704f69SBart Van Asschetypically issue a sequence of SCSI READ and WRITE commands to copy data.
36*44704f69SBart Van AsscheThese utilities are relatively tightly bound to Linux and are not ported to
37*44704f69SBart Van Asscheother Operating Systems. A new utility called ddpt (in a package of the same
38*44704f69SBart Van Asschename) is more generic while still allowing a copy to be done in terms of
39*44704f69SBart Van AsscheSCSI READ and WRITE commands. ddpt has been ported to other OSes.
40*44704f69SBart Van Assche
41*44704f69SBart Van AsscheLicense
42*44704f69SBart Van Assche=======
43*44704f69SBart Van AsscheAll utilities and libraries have either a "2 clause" BSD license or are
44*44704f69SBart Van Assche"GPL-2ed". The "2 clause" BSD license is taken from the FreeBSD project but
45*44704f69SBart Van Asschedrops the last paragraph that directly refers to the "FreeBSD project".
46*44704f69SBart Van AsscheThat BSD license was updated from the "3 clause" to the newer "2 clause"
47*44704f69SBart Van Asscheversion on 20180119. To save space various source code files refer to a
48*44704f69SBart Van Asschefile called "BSD_LICENSE" in the main, src and lib directories. The author's
49*44704f69SBart Van Asscheintention is that users may incorporate all or part of the code in their work
50*44704f69SBart Van Asscheas they please. Attribution is encouraged. Please check the code as other
51*44704f69SBart Van Asschecontributors (apart from the author) may also have copyright notices. For a
52*44704f69SBart Van Asschelist of contributors see the CREDITS file.
53*44704f69SBart Van Assche
54*44704f69SBart Van Assche
55*44704f69SBart Van AsscheDescription
56*44704f69SBart Van Assche===========
57*44704f69SBart Van AsscheA web site supporting the sg3_utils package can be found at
58*44704f69SBart Van Asschehttps://sg.danny.cz/sg/sg3_utils.html . That page has a table of released
59*44704f69SBart Van Asscheversions for download. The most recent release or beta of sg3_utils may
60*44704f69SBart Van Asschebe found on this page: https://sg.danny.cz/sg in the News section.
61*44704f69SBart Van Assche
62*44704f69SBart Van AsscheThe predecessor to this package was called sg_utils. It is described in
63*44704f69SBart Van Asschehttps://sg.danny.cz/sg/uu_index.html and old versions can be downloaded
64*44704f69SBart Van Asschefrom the Downloads section of https://sg.danny.cz/sg .
65*44704f69SBart Van Assche
66*44704f69SBart Van AsscheIn the Linux 2.4 kernel series these utilities need to use the SCSI generic
67*44704f69SBart Van Assche(sg) driver to access SCSI devices. The name of this package (i.e. sg3_utils)
68*44704f69SBart Van Asscherefers to version 3 of the SCSI generic (sg) driver which was introduced at
69*44704f69SBart Van Asschethe beginning of the 2.4 Linux kernel series. Significantly this added a new
70*44704f69SBart Van AsscheSCSI command interface structure (i.e. struct sg_io_hdr) that is more
71*44704f69SBart Van Asscheflexible than the older "sg_header" structure found in the sg driver in the
72*44704f69SBart Van Assche2.2 and earlier Linux kernel series. The sg_io_hdr structure is also more
73*44704f69SBart Van Asscheflexible than the awkward (and limiting) interface to the
74*44704f69SBart Van AsscheSCSI_IOCTL_SEND_COMMAND ioctl supported by the Linux SCSI mid level. The
75*44704f69SBart Van Asscheversion 3 sg driver also added the SG_IO ioctl that is synchronous (i.e. it
76*44704f69SBart Van Asscheissues the requested SCSI command and waits for the response (or a timeout)
77*44704f69SBart Van Asschebefore the ioctl returns to the user space program that invoked it). The
78*44704f69SBart Van AsscheSG_IO ioctl is now supported in other parts of the Linux kernel in the 2.6
79*44704f69SBart Van Asscheseries.
80*44704f69SBart Van Assche
81*44704f69SBart Van AsscheIn sg3_utils version 1.27 support has been added for the Linux bsg driver
82*44704f69SBart Van Asschewhich use the sg version 4 interface. There seems no point in renaming
83*44704f69SBart Van Asschethis package sg4_utils. The existing utilities just silently support either.
84*44704f69SBart Van AsscheCurrently the source build must be able to see the /usr/include/linux/bsg.h
85*44704f69SBart Van Asschefile. Then at run time the /proc/devices pseudo file needs to have an entry
86*44704f69SBart Van Asschefor the bsg driver (appeared around lk 2.6.28). With this in place each
87*44704f69SBart Van Asscheutility at run time checks the device it has been given and if it is a char
88*44704f69SBart Van Asschedevice whose major number matches the bsg entry in /proc/devices then the
89*44704f69SBart Van Asschesg v4 interface is used. Otherwise the sg v3 interface is used.
90*44704f69SBart Van Assche
91*44704f69SBart Van AsscheUtilities that wish to use the asynchronous SCSI command interface (i.e. via
92*44704f69SBart Van Asschea write() read() sequence) or issue special "commands" (e.g. bus and device
93*44704f69SBart Van Asscheresets) still need to use the Linux sg driver. Note that various
94*44704f69SBart Van Asschedrivers (e.g. cdrom/sr) have different open() flag and permissions policies
95*44704f69SBart Van Asschethat the user may need to take into account.
96*44704f69SBart Van Assche
97*44704f69SBart Van AsscheIf users have problems or questions about them please contact the author.
98*44704f69SBart Van AsscheDocumentation for the Linux sg device driver can be found at:
99*44704f69SBart Van Asschehttps://sg.danny.cz/sg/p/sg_v3_ho.html . This is written in DocBook and the
100*44704f69SBart Van Asscheoriginal xml can be found in the same directory with the ".xml" extension.
101*44704f69SBart Van AsschePostscript and pdf renderings are also in that directory. Older documentation
102*44704f69SBart Van Asschefor the sg version 3 driver can be found at:
103*44704f69SBart Van Asschehttps://sg.danny.cz/sg/p/scsi_generic_v3.txt .
104*44704f69SBart Van Assche
105*44704f69SBart Van AsscheTo save the repetition of common code (e.g. SCSI error processing) and
106*44704f69SBart Van Asschereduce the size of the executable files, a shared library called
107*44704f69SBart Van Asschelibsgutils<num>.so (its Linux name) is created during the build process.
108*44704f69SBart Van AsscheThat library is built from the contents of the include and lib
109*44704f69SBart Van Asschesubdirectories. The header files in the include subdirectory can be seen
110*44704f69SBart Van Asscheas the API of libsgutils and are commented with that in mind. The SCSI
111*44704f69SBart Van Asschepass-through code for the supported operating systems is found in the lib
112*44704f69SBart Van Asschesubdirectory with names like sg_pt_linux.c and sg_pt_win32.c .
113*44704f69SBart Van Assche
114*44704f69SBart Van AsscheVarious distributions (of Linux mainly) distribute sg3_utils as 3
115*44704f69SBart Van Asscheinstallable packages. One is a package containing the shared library
116*44704f69SBart Van Asschediscussed above (e.g. libsgutils2-2_1.33-0.1_i386.deb). A second package
117*44704f69SBart Van Asschecontains the utilities (e.g. sg3-utils_1.33-0.1_i386.deb) and depends on the
118*44704f69SBart Van Asschefirst package). Finally there is an optional package that contains header
119*44704f69SBart Van Asschefiles and a static library (e.g. libsgutils2-dev_1.33-0.1_i386.deb). This
120*44704f69SBart Van Asschefinal package is only needed to build other packages (e.g. sdparm) that
121*44704f69SBart Van Asschewish to use the sg3_utils shared library.
122*44704f69SBart Van Assche
123*44704f69SBart Van AsscheAll the utilities in the src subdirectory have "man" pages that are
124*44704f69SBart Van Asscheplaced in the doc subdirectory. There is also a sg3_utils (8) man page that
125*44704f69SBart Van Asschesummarizes common facilities including exit statuses. Additional
126*44704f69SBart Van Asscheinformation (including each utility's version number) can be found towards
127*44704f69SBart Van Asschethe top of each ".c" file corresponding to the utility name.
128*44704f69SBart Van Assche
129*44704f69SBart Van AsscheThe sg driver in Linux can be seen as having 3 distinct versions:
130*44704f69SBart Van Assche
131*44704f69SBart Van Assche   v1   lk < 2.2.6     sg_header based relatively unchanged since 1992
132*44704f69SBart Van Assche   v2   lk >= 2.2.6    enhanced sg_header interface structure [1999/4/16]
133*44704f69SBart Van Assche   v3   lk >= 2.4      additional sg_io_hdr interface structure [2001/1/4]
134*44704f69SBart Van Assche   v3   lk >= 2.6      same interface as found in lk 2.4 [2.6.0: 2003/12/18]
135*44704f69SBart Van Assche
136*44704f69SBart Van Asscheand the bsg driver supports the sg v4 interface and was added around
137*44704f69SBart Van Asschelk 2.6.28 . This package is targeted at "v3" and "v4". Another package called
138*44704f69SBart Van Assche"sg_utils" is targeted at "v2" and to a lesser extent "v1". The "sg_utils"
139*44704f69SBart Van Asschepackage has a subset of the utilities found in this package.
140*44704f69SBart Van Assche
141*44704f69SBart Van AsscheIn Linux some sg driver ioctls (notably SG_IO) are defined for many block
142*44704f69SBart Van Asschedevices in lk 2.6 series. In practice this means all SCSI block devices,
143*44704f69SBart Van AsscheATAPI block devices (mainly CD, DVD and BD optical devices) but _not_ ATA
144*44704f69SBart Van Asschedisks, depending on which kernel configuration options, can be accessed by
145*44704f69SBart Van Asschethe utilities in this package. SATA disks that use the libata kernel library
146*44704f69SBart Van Assche(or some other SCSI to ATA Translation (SAT) Layer (SATL)) accept SCSI
147*44704f69SBart Van Asschecommands and thus are supported. Support for the SG_IO as been added to the
148*44704f69SBart Van Asschescsi tape driver (st) in lk 2.6.6 .
149*44704f69SBart Van Assche
150*44704f69SBart Van AsscheIn the src directory the bulk of the utilities are written in relatively
151*44704f69SBart Van Asscheclean POSIX compliant C code with Linux specific system calls and structures
152*44704f69SBart Van Asscheremoved and placed in Linux specific files in the lib directory. A small
153*44704f69SBart Van Asschenumber of utilities in the src directory do contain Linux specific logic
154*44704f69SBart Van Asscheand are not ported to other OSes (e.g. sg_dd). One utility, sg_scan, has
155*44704f69SBart Van Asschetwo separate implementations, one for Linux (sg_scan_linux.c) and one for
156*44704f69SBart Van AsscheWindows (sg_scan_win32.c). The src-lib directory split approach allows
157*44704f69SBart Van AsscheFreeBSD, Solaris, Tru64 and Windows specific code to be isolated to a few
158*44704f69SBart Van Asschefiles in the lib directory whose interfaces match those of the Linux
159*44704f69SBart Van Asschespecific code.
160*44704f69SBart Van Assche
161*44704f69SBart Van AsscheDarwin is not supported because the Apple folks do not want to give their
162*44704f69SBart Van Asscheusers a pass-through SCSI interface. The author has read about creative
163*44704f69SBart Van Asschehackers using a VM containing a real OS to circumvent the Apple restriction.
164*44704f69SBart Van Assche
165*44704f69SBart Van AsscheC standard is C11
166*44704f69SBart Van Assche==================
167*44704f69SBart Van AsscheThe C code in this package is written for portability rather than speed.
168*44704f69SBart Van AsscheIt assumes a level of C99 compliance (the C standard prior to C11) and
169*44704f69SBart Van Asschefavours POSIX system and library calls over OS specific calls.
170*44704f69SBart Van Assche
171*44704f69SBart Van AsscheThe C code is written in a C++ friendly way and is checked from time to
172*44704f69SBart Van Asschetime that it compiles clean with C++. To accommodate C++ certain C99
173*44704f69SBart Van Asscheconstructs such as designated initializers cannot be used. To build
174*44704f69SBart Van Asschewith C++, C++11 (i.e. the C++ standard from 2011) or later is required.
175*44704f69SBart Van AsscheFinding a common C and C++ syntax for zeroing stack variables (including
176*44704f69SBart Van Asscheaggregates) may need to wait until C23 allows this syntax:
177*44704f69SBart Van Assche   struct example_t ex1 {};
178*44704f69SBart Van Asschewhich C++ introduced in C++11.  In the meantime the SG_C_CPP_ZERO_INIT
179*44704f69SBart Van Asschedefine (hack) does this.
180*44704f69SBart Van Assche
181*44704f69SBart Van AsscheThe author has not seriously attempted to build this code on MSVC (aka
182*44704f69SBart Van AsscheVisual Studio). There are a few roadblocks (that may be overcome in the
183*44704f69SBart Van Asschefuture) that include MSVC being basically a C++ compiler, not a C/C++
184*44704f69SBart Van Asschecompiler. For some reason MSVC only claims C89 compliance (i.e. the first
185*44704f69SBart Van AsscheC standard from 1989). MSVC 2013 and 2015 are moving closer to C99
186*44704f69SBart Van Asschecompliance and may be sufficient to compile this package. Another problem
187*44704f69SBart Van Asscheis the assumption of the availability of basic Unix system calls such as
188*44704f69SBart Van Asscheopen(). Nearly 20 years ago Microsoft indicated (promised ?) that it
189*44704f69SBart Van Asschewould move in the direction of POSIX compliance, but very little ever
190*44704f69SBart Van Asschehappened. "Talk is cheap, there should be a tax on it."
191*44704f69SBart Van Assche
192*44704f69SBart Van AsscheBuilding
193*44704f69SBart Van Assche========
194*44704f69SBart Van AsscheThis package is designed to be built with the usual:
195*44704f69SBart Van Assche    "./configure ; make ; make install"
196*44704f69SBart Van Asschesequence. In some situations that may need to be prefixed by a call to
197*44704f69SBart Van Asschethe "./autogen.sh" script which invokes autoconf and automake. That in turn
198*44704f69SBart Van Asschemay require packages containing those utilities to be installed. The
199*44704f69SBart Van Asschelibtool utility is also required. Naturally a C compiler is required
200*44704f69SBart Van Asscheand due to the vagaries of libtool a C++ compiler also.
201*44704f69SBart Van Assche
202*44704f69SBart Van AsscheThe "./configure" takes many command line options with the defaults
203*44704f69SBart Van Asschebeing usually sufficient to start with. One quirk is that the location
204*44704f69SBart Van Asscheof the installation is under the /usr/local directory. So the sg_inq
205*44704f69SBart Van Asscheutility will be installed at /usr/local/bin/sg_inq . This is controlled
206*44704f69SBart Van Asscheby the "--prefix=<directory>" option which defaults to
207*44704f69SBart Van Assche"--prefix=/usr/local". As an example to install the executables in /usr/bin
208*44704f69SBart Van Asscheand disable the creation of the shared library (libsgutils<num>.so) this
209*44704f69SBart Van Asscheinvocation could be used: "./configure --prefix=/usr --disable-shared".
210*44704f69SBart Van AsscheTo reduce the size of an executable as well try this:
211*44704f69SBart Van Assche"./configure --prefix=/usr --disable-shared --disable-scsistrings".
212*44704f69SBart Van AsscheAlso --disable-shared will produce (relatively) "static" executables in
213*44704f69SBart Van Asschethe src directory that are easier to debug. And
214*44704f69SBart Van Assche"./configure --enable-debug" will compile with more debug type options,
215*44704f69SBart Van Asscheincluding more compiler checks and defining "DEBUG" within the src and
216*44704f69SBart Van Asschelib source files. Most utilities in the src directory set '-vv' (i.e.
217*44704f69SBart Van Asscheequivalent to calling "--verbose" twice) when "DEBUG" is set.
218*44704f69SBart Van Assche
219*44704f69SBart Van AsscheIn Linux there are package build files for "rpm" based and for "deb" based
220*44704f69SBart Van Asschesystems. The 'sg3_utils.spec' file in the main directory can be used like
221*44704f69SBart Van Asschethis: 'rpmbuild -ba sg3_utils.spec' in a rpmbuild tree SPECS directory.
222*44704f69SBart Van AsscheTo cross build or make a more widely distributable package then the --target
223*44704f69SBart Van Asscheoption may be useful: 'rpmbuild --target=i386 -ba sg3_utils.spec' or
224*44704f69SBart Van Assche'rpmbuild --target=x86_64 -ba sg3_utils.spec' . The sg3_utils.spec file
225*44704f69SBart Van Asschein the main directory targets Red Hat systems, an alternative "spec" file
226*44704f69SBart Van Asschefor Suse systems has been placed under the 'suse' directory.
227*44704f69SBart Van Assche
228*44704f69SBart Van AsscheThe 'build_debian.sh' script should build several "deb" packages and place
229*44704f69SBart Van Asschethem in the parent directory. In debian based systems doing
230*44704f69SBart Van Asschea 'apt-get install build-essential' is one way to get most of build
231*44704f69SBart Van Asscheenvironment needed if it has not already been loaded. There are now some
232*44704f69SBart Van Asscheproblems with this script and the superseded Debian 4.0 ("etch"). See
233*44704f69SBart Van Asschedebian/README.debian4 for a workaround. Amongst other things debian
234*44704f69SBart Van Asschebuilds are sensitive to the value in the debian/compat file. If it
235*44704f69SBart Van Asschecontains "7" then it works on lenny and gives warning on squeeze (but
236*44704f69SBart Van Asschefails on the earlier etch).
237*44704f69SBart Van Assche
238*44704f69SBart Van AsscheWarning
239*44704f69SBart Van Assche=======
240*44704f69SBart Van AsscheMany devices use SCSI command sets over transport protocols not normally
241*44704f69SBart Van Asscheassociated with SCSI (as defined at https://www.t10.org ). Some of these
242*44704f69SBart Van Asschedevices react poorly (e.g. lock up) when sent SCSI commands that they don't
243*44704f69SBart Van Asschesupport. Even sending a supported SCSI command with a field set to an
244*44704f69SBart Van Asscheunexpected value can cause problems. [The author is talking about billions
245*44704f69SBart Van Asscheof USB devices with horrible SCSI implementations.]
246*44704f69SBart Van Assche
247*44704f69SBart Van AsscheFor example, all "SCSI" devices must support the INQUIRY command which the
248*44704f69SBart Van AsscheSCSI-2 standard says should request a 36 byte response. However later SCSI
249*44704f69SBart Van Asschestandards (e.g. SPC-2) have increased that length but some SCSI devices lock
250*44704f69SBart Van Asscheup when they receive a request for anything other than a 36 byte response.
251*44704f69SBart Van Assche
252*44704f69SBart Van AsscheAny well implemented "SCSI" device should react sensibly when a utility in
253*44704f69SBart Van Asschesg3_utils sends a SCSI command that it doesn't support. Unfortunately this
254*44704f69SBart Van Asschecannot be guaranteed.
255*44704f69SBart Van Assche
256*44704f69SBart Van AsschePrior to lk 2.6.29 USB mass storage limited sense data to 18 bytes which
257*44704f69SBart Van Asschecaused problems for certain types of descriptor based sense data. An
258*44704f69SBart Van Asscheexample of this is the SCSI ATA PASS-THROUGH command with the CK_COND bit
259*44704f69SBart Van Asscheset.
260*44704f69SBart Van Assche
261*44704f69SBart Van Assche
262*44704f69SBart Van AsscheUtilities
263*44704f69SBart Van Assche=========
264*44704f69SBart Van AsscheHere is list in alphabetical order of utilities found in the 'src'
265*44704f69SBart Van Asschesubdirectory of the sg3_utils package:
266*44704f69SBart Van Assche    sginfo, sg_bt_ctl, sg_compare_and_write, sg_copy_results, sgm_dd, sgp_dd,
267*44704f69SBart Van Assche    sg_dd, sg_decode_sense, sg_emc_trespass, sg_format, sg_get_config,
268*44704f69SBart Van Assche    sg_get_elem_status, sg_get_lba_status, sg_ident, sg_inq, sg_logs,
269*44704f69SBart Van Assche    sg_luns, sg_map, sg_map26, sg_modes, sg_opcodes, sg_persist, sg_prevent,
270*44704f69SBart Van Assche    sg_raw, sg_rbuf, sg_rdac, sg_read, sg_read_attr, sg_readcap,
271*44704f69SBart Van Assche    sg_read_block_limits, sg_read_buffer, sg_read_long, sg_reassign,
272*44704f69SBart Van Assche    sg_referrals, sg_rem_rest_elem, sg_rep_density, sg_rep_pip, sg_rep_zones,
273*44704f69SBart Van Assche    sg_request, sg_reset, sg_rmsn, sg_rtpg, sg_safte, sg_sanitize,
274*44704f69SBart Van Assche    sg_sat_identify, sg_sat_phy_event, sg_sat_read_gplog, sg_sat_set_features,
275*44704f69SBart Van Assche    sg_scan, sg_seek, sg_senddiag, sg_ses, sg_ses_microcode, sg_start,
276*44704f69SBart Van Assche    sg_stpg, sg_stream_ctl, sg_sync, sg_test_rwbuff, sg_timestamp, sg_turs,
277*44704f69SBart Van Assche    sg_unmap, sg_verify, sg_vpd, sg_write_buffer, sg_write_long,
278*44704f69SBart Van Assche    sg_write_same, sg_write_verify, sg_write_x, sg_wr_mode, sg_xcopy, sg_zone,
279*44704f69SBart Van Assche    sg_z_act_query
280*44704f69SBart Van Assche
281*44704f69SBart Van AsscheEach of the above utilities depends on header files found in the 'include'
282*44704f69SBart Van Asschesubdirectory and library code found in the 'lib' subdirectory. Associated
283*44704f69SBart Van Asscheman pages are found in the 'doc' subdirectory. Additional programs found
284*44704f69SBart Van Asschein the 'archive', 'examples' and 'utils' subdirectories in not build by the
285*44704f69SBart Van Asschetop level build infrastructure. Linux binary distributions of the sg3_utils
286*44704f69SBart Van Asschepackage (e.g. "rpm" and debian packages) typically contain the shared
287*44704f69SBart Van Asschelibrary, the utilities found in the 'src' subdirectory, their associated man
288*44704f69SBart Van Asschepages and some documentation files (e.g. README, INSTALL, CREDITS, COPYING
289*44704f69SBart Van Asscheand COVERAGE). See the INSTALL file for generic instructions about building
290*44704f69SBart Van Asschewith autotools (e.g. ./configure ).
291*44704f69SBart Van Assche
292*44704f69SBart Van AsscheMan pages can be read (without building and installing the package) by
293*44704f69SBart Van Asschegoing to the 'doc' subdirectory and executing something like this:
294*44704f69SBart Van Assche $ man ./sg_dd.8
295*44704f69SBart Van Assche
296*44704f69SBart Van AsscheTo see which SCSI commands (and ATA commands) are used by these utilities
297*44704f69SBart Van Asscherefer to the COVERAGE file.
298*44704f69SBart Van Assche
299*44704f69SBart Van AsscheHere is a list in alphabetical order of utilities found in the 'examples'
300*44704f69SBart Van Asschesubdirectory:
301*44704f69SBart Van Assche  - sg_excl, scsi_inquiry, sg_sat_chk_power, sg__sat_identify,
302*44704f69SBart Van Assche    sg__sat_phy_event, sg__sat_set_features, sg_sat_smart_rd_data,
303*44704f69SBart Van Assche    sg_simple1, sg_simple2, sg_simple3, sg_simple4, sg_simple5,
304*44704f69SBart Van Assche    sg_simple16
305*44704f69SBart Van Assche
306*44704f69SBart Van AsscheAlso in that subdirectory is a script to test sg_persist, an example data
307*44704f69SBart Van Asschefile for sg_persist (called "transport_ids.txt") and an example data file for
308*44704f69SBart Van Asschesg_reassign (called "reassign_addr.txt"). There are several scripts
309*44704f69SBart Van Asschefor 'sg_senddiag -pf -raw=-' that will put some SAS disk phys into
310*44704f69SBart Van Asschea "compliant jitter tolerance pattern" (CJTPAT).
311*44704f69SBart Van Assche
312*44704f69SBart Van AsscheThe 'testing' subdirectory contains source and a Makefiles to test
313*44704f69SBart Van Asschekernel pass-through and associated drivers, mainly for Linux. There is
314*44704f69SBart Van Asscheboth C code (with the extension ".c") and C++ code (with the extension
315*44704f69SBart Van Assche".cpp"). There is a "Makefile" to build the C + C++ code. The Makefile
316*44704f69SBart Van Asschedepends on some object files from the "lib" subdirectory. So a sequence
317*44704f69SBart Van Asschelike this may be required prior to invoking make: "cd <top_of_package> ;
318*44704f69SBart Van Assche./configure ; cd lib ; make ; cd ../testing".
319*44704f69SBart Van Assche
320*44704f69SBart Van AsscheHere is a list in alphabetical order of utilities found in the 'testing'
321*44704f69SBart Van Asschesubdirectory:
322*44704f69SBart Van Assche  - bsg_queue_tst, sgh_dd (C++), sg_iovec_tst, sg_queue_tst, sg_sense_tst,
323*44704f69SBart Van Assche    sg_tst_async (C++), sg_tst_context (C++), sg_tst_excl (C++),
324*44704f69SBart Van Assche    sg_tst_excl2 (C++), sg_tst_excl3 (C++)
325*44704f69SBart Van Assche
326*44704f69SBart Van AsscheThe 'utils' subdirectory contains source and a Makefile to build "hxascdmp"
327*44704f69SBart Van Asschewhich accepts binary data from stdin (or a file on the command line) and
328*44704f69SBart Van Asscheoutputs an ASCII-HEX and ASCII representation of it. It is similar to the
329*44704f69SBart Van AsscheUnix od command. There is also code to sg_chk_asc.c which checks a given
330*44704f69SBart Van Asschetext file (typically a copy of https://www.t10.org/lists/asc-num.txt ) and
331*44704f69SBart Van Asschechecks it against the asc/ascq text strings held in sg_lib_data.c .
332*44704f69SBart Van Assche
333*44704f69SBart Van AsscheThe 'doc' subdirectory contains a README file containing the urls of
334*44704f69SBart Van Asschevarious related documents.
335*44704f69SBart Van Assche
336*44704f69SBart Van AsscheThe 'scripts' subdirectory contains some Bourne (bash) shell scripts that
337*44704f69SBart Van Asscherely on utilities in the main directory. One script uses the sdparm utility.
338*44704f69SBart Van AsscheThese scripts are described in the scripts/README file and have usage
339*44704f69SBart Van Asschemessages.
340*44704f69SBart Van Assche
341*44704f69SBart Van Assche
342*44704f69SBart Van AsscheNotes for utilities without man pages
343*44704f69SBart Van Assche=====================================
344*44704f69SBart Van AsscheThese utils are found in the 'examples' subdirectory.
345*44704f69SBart Van Assche
346*44704f69SBart Van AsscheThe "scsi_inquiry" program shows the use of the SCSI_IOCTL_SEND_COMMAND
347*44704f69SBart Van Asscheioctl to send a SCSI INQUIRY command. That ioctl() is supported by the
348*44704f69SBart Van AsscheSCSI sub system mid level and so is common to all sd, sr, st and sg devices.
349*44704f69SBart Van AsscheThat ioctl is deprecated in the lk 2.6 series. This program has been placed
350*44704f69SBart Van Asschein the "examples" subdirectory.
351*44704f69SBart Van Assche
352*44704f69SBart Van Assche"sg_simple1" and "sg_simple2" are example programs demonstrating calls
353*44704f69SBart Van Asscheto the SCSI INQUIRY and TEST UNIT READY commands. They only differ in their
354*44704f69SBart Van Asscheerror processing: sg_simple1 uses sg_lib.[hc] for error processing while
355*44704f69SBart Van Asschesg_simple2 does its own more primitive checks.
356*44704f69SBart Van Assche
357*44704f69SBart Van Assche"sg_simple3" tests out user space scatter gather added to the version 3
358*44704f69SBart Van Asschesg driver.
359*44704f69SBart Van Assche
360*44704f69SBart Van Assche"sg_simple4" shows the INQUIRY command using mmap-ed IO to obtain its
361*44704f69SBart Van Asscheresponse buffer.
362*44704f69SBart Van Assche
363*44704f69SBart Van Assche"sg_simple5" also sends and INQUIRY and TEST UNIT READY commands. It
364*44704f69SBart Van Asscheuses the generic pass through mechanism based on sg_pt.h . It will
365*44704f69SBart Van Asschecurrently build in Linux and FreeBSD (with "make -f Makefile.freebsd").
366*44704f69SBart Van AsscheIt has extensive error checking code.
367*44704f69SBart Van Assche
368*44704f69SBart Van Assche"sg_simple16" attempts to send a 16 byte SCSI command, READ_16, to the
369*44704f69SBart Van Asschescsi device. This is only supported for lk >= 2.4.15 and for adapter
370*44704f69SBart Van Asschedrivers that indicate that they have 16 byte CDB capability (otherwise
371*44704f69SBart Van AsscheDID_ABORT will appear in the host_status).
372*44704f69SBart Van Assche
373*44704f69SBart Van Assche"sg_sat_chk_power" attempts to push an ATA CHECK POWER MODE command
374*44704f69SBart Van Asschethrough the SAT-defined ATA PASS_THROUGH (16) SCSI command. That
375*44704f69SBart Van AsscheATA command needs to read the "FIS" registers after the command is
376*44704f69SBart Van Asschecompleted which involves using the ATA Status Return (sense data)
377*44704f69SBart Van Asschedescriptor (as defined in SAT).
378*44704f69SBart Van Assche
379*44704f69SBart Van Assche"sg_sat_smart_rd_data" attempts to push an ATA SMART/READ DATA command
380*44704f69SBart Van Asschethrough the SAT-defined ATA PASS_THROUGH (16) SCSI command. If
381*44704f69SBart Van Asschesuccessful, the 256 word (512 byte) response is output.
382*44704f69SBart Van Assche
383*44704f69SBart Van Assche"sg_tst_excl" and "sg_tst_excl2" use multiple threads to bombard the
384*44704f69SBart Van Asschegiven device with O_EXCL open flags, so only one should succeed at a
385*44704f69SBart Van Asschetime. While holding O_EXCL control a thread attempts a double increment
386*44704f69SBart Van Asscheon an integer in the given LBA. If the integer starts even (after the
387*44704f69SBart Van Asschefirst read) then it should remain even if the O_EXCL flag is doing its job.
388*44704f69SBart Van AsscheThe "sg_tst_excl" variant uses the Linux SG_IO v3 interface while the
389*44704f69SBart Van Assche"sg_tst_excl2" uses the more generic sg_pt infrastructure.
390*44704f69SBart Van Assche
391*44704f69SBart Van Assche"sg_tst_excl3" is a variant of "sg_tst_excl2". "sg_tst_excl3" only does
392*44704f69SBart Van Asschethe double increment from the first thread, each time using O_EXCL on
393*44704f69SBart Van Asscheopen. The remaining threads check the value is even, each time doing
394*44704f69SBart Van Asschean open without the O_EXCL flag.
395*44704f69SBart Van Assche
396*44704f69SBart Van Assche"bsg_queue_tst" sends an INQUIRY command via the Linux SG_IO v4 interface
397*44704f69SBart Van Asschewhich is used by the bsg driver. So it will take device names like
398*44704f69SBart Van Assche"/dev/bsg/6:0:0:0". It tests if sending repeated INQUIRYs with
399*44704f69SBart Van Asschethe BSG_FLAG_Q_AT_HEAD or BSG_FLAG_Q_AT_TAIL flag makes any difference.
400*44704f69SBart Van Assche
401*44704f69SBart Van Assche"sg_tst_async" is a test harness for the Linux sg driver. It is multi
402*44704f69SBart Van Asschethreaded, submitting either TEST UNIT READY, READ(16) or WRITE(16) SCSI
403*44704f69SBart Van Asschecommands asynchronously. Each thread opens a file descriptor and submits
404*44704f69SBart Van Asschethose commands up to the queue limit (sg driver has a per file descriptor
405*44704f69SBart Van Asschequeue limit of 16). Multiple threads doing the same thing act as a
406*44704f69SBart Van Asschemultiplier to that queue limit.
407*44704f69SBart Van Assche
408*44704f69SBart Van Assche
409*44704f69SBart Van AsscheNVME Support
410*44704f69SBart Van Assche============
411*44704f69SBart Van AsscheFirstly the author has no intention of extending this package to contain
412*44704f69SBart Van Asschegeneral purpose NVMe utilities. That leaves the areas where SCSI overlaps
413*44704f69SBart Van Asschewith NVMe. There was a SCSI to NVMe Translation Layer (SNTL) driver in the
414*44704f69SBart Van AsscheLinux kernel based on a white paper from NVM Express. Intel has withdrawn
415*44704f69SBart Van Asschethat driver and T10 (SCSI) and NVM Express have made no further attempts
416*44704f69SBart Van Asscheto standardize a SNTL. Given the SCSI to ATA Translation Layer (SATL) which
417*44704f69SBart Van Asscheis standardized by T10, it is pretty clear what a SNTL should do.
418*44704f69SBart Van Assche
419*44704f69SBart Van AsscheThe NVMe Management Interface (NVME-MI) committee have decided to use SES-3
420*44704f69SBart Van Asschestandard from T10 via the newly added SES Send and SES Receive MI commands.
421*44704f69SBart Van AsscheSo the sg_ses utility and this package's library have been extended to use
422*44704f69SBart Van Asschethese commands when a NVMe device (typically a disk enclosure) is detected.
423*44704f69SBart Van AsscheThis has been tested by a disk vendor who is happy with the results. Other
424*44704f69SBart Van Asscheuser reports are welcome as the author does not have equipment to test
425*44704f69SBart Van Asschethis.
426*44704f69SBart Van Assche
427*44704f69SBart Van AsscheOther utilities in this package that use the SES Send and Receive commands,
428*44704f69SBart Van Asscheor the SNTL in the library are sg_senddiag, sg_inq, sg_raw and sg_readcap.
429*44704f69SBart Van Assche
430*44704f69SBart Van Assche
431*44704f69SBart Van AsscheCommand line processing
432*44704f69SBart Van Assche=======================
433*44704f69SBart Van AsscheThese utilities can be divided into 3 groups when their handling of command
434*44704f69SBart Van Asscheline arguments is considered:
435*44704f69SBart Van Assche  - ad hoc, typically in a short form only, sometimes longer (e.g.
436*44704f69SBart Van Assche    "sg_logs -pcb /dev/sdc")
437*44704f69SBart Van Assche  - inspired by the dd Unix command (e.g. sg_dd, sgm_dd, sgp_dd, sg_read)
438*44704f69SBart Van Assche  - recent utilities use "getopt_long" (see "man getopt_long")
439*44704f69SBart Van Assche    type command lines. These have short form (starting with "-")
440*44704f69SBart Van Assche    and corresponding longer form (starting with "--") options.
441*44704f69SBart Van Assche
442*44704f69SBart Van AsscheThe older utilities that use ad hoc options, in alphabetical order:
443*44704f69SBart Van Assche  - sg_emc_trespass, sginfo(1/2), sg_inq, sg_logs, sg_map, sg_modes,
444*44704f69SBart Van Assche    sg_opcodes, sg_rbuf, sg_rdac, sg_readcap, sg_reset, sg_scan (Linux),
445*44704f69SBart Van Assche    sg_senddiag, sg_start, sg_test_rwbuf, sg_turs
446*44704f69SBart Van AsscheIn sg3_utils version 1.23 the following utilities from this group were
447*44704f69SBart Van Asscheconverted to have a dual getopt_long/ad_hoc interface, defaulting to
448*44704f69SBart Van Asschethe getop_long interface:
449*44704f69SBart Van Assche  - sg_inq, sg_logs, sg_modes, sg_opcodes, sg_rbuf, sg_readcap,
450*44704f69SBart Van Assche    sg_senddiag, sg_start, sg_turs
451*44704f69SBart Van AsscheThese can be switched back to the older (backward compatible) ad hoc
452*44704f69SBart Van Asscheinterface by defining the SG3_UTILS_OLD_OPTS environment variable
453*44704f69SBart Van Asscheor using '-O' as the first command line option.
454*44704f69SBart Van Assche
455*44704f69SBart Van AsscheThe more recent utilities that use "getopt_long" only are:
456*44704f69SBart Van Assche  - sg_bt_ctl, sg_compare_and_write, sg_decode_sense, sg_format,
457*44704f69SBart Van Assche    sg_get_config, sg_get_lba_status, sg_ident, sg_luns, sg_map26,
458*44704f69SBart Van Assche    sg_persist, sg_prevent, sg_raw, sg_read_attr, sg_read_block_limits,
459*44704f69SBart Van Assche    sg_read_buffer, sg_read_long, sg_reassign, sg_referrals, sg_rep_pip,
460*44704f69SBart Van Assche    sg_rep_zones, sg_requests, sg_rmsn, sg_rtpg, sg_safte, sg_sanitize,
461*44704f69SBart Van Assche    sg_sat_identify, sg_sat_phy_event, sg_sat_read_gplog,
462*44704f69SBart Van Assche    sg_sat_set_features, sg_scan(w), sg_seek, sg_ses, sg_ses_microcode,
463*44704f69SBart Van Assche    sg_stpg, sg_stream_ctl, sg_sync, sg_test_rwbuf, sg_timestamp, sg_unmap,
464*44704f69SBart Van Assche    sg_verify, sg_vpd, sg_write_buffer, sg_write_long, sg_write_same,
465*44704f69SBart Van Assche    sg_write_verify, sg_write_x, sg_wr_mode, sg_zone, sg_z_act_query
466*44704f69SBart Van Assche
467*44704f69SBart Van Assche
468*44704f69SBart Van AsscheDangerous code
469*44704f69SBart Van Assche==============
470*44704f69SBart Van AsscheThis C code snippet:
471*44704f69SBart Van Assche    unsigned char uc = 0x80;
472*44704f69SBart Van Assche    uint64_t ull;
473*44704f69SBart Van Assche    ull = (uc << 24);
474*44704f69SBart Van AsscheSomewhat surprisingly sets ull to:
475*44704f69SBart Van Assche    ull: 0xffffffff80000000
476*44704f69SBart Van AsscheThis result is due to the 'unary conversion' of uc to a (32 bit signed)
477*44704f69SBart Van Assche'int' before the shift. The resultant type from the shift is also an int
478*44704f69SBart Van Asscheand it has its top bit set so there is sign extension when it is assigned
479*44704f69SBart Van Asscheinto a 64 bit unsigned integer. Making sure there is no conversion to 'int'
480*44704f69SBart Van Asschesolves the problem. In this case if uc is declared as unsigned int the
481*44704f69SBart Van Asscheresult will be as expected (i.e. 0x80000000).
482*44704f69SBart Van Assche
483*44704f69SBart Van Assche
484*44704f69SBart Van AsscheBypassing the somewhat dangerous shift operators
485*44704f69SBart Van Assche================================================
486*44704f69SBart Van AsscheThe shift operators in C are "<<" and ">>". They can be dangerous (as shown
487*44704f69SBart Van Asschein the above section) or tedious and hence error prone to use. However they
488*44704f69SBart Van Asscheare often needed to cope with the translation of integers on the host OS to
489*44704f69SBart Van Asschethe corresponding representation within a SCSI command or parameter data
490*44704f69SBart Van Asschemoved to or from a SCSI device. The Logical Block Address (LBA) is a good
491*44704f69SBart Van Asscheexample; it is either 32 or 64 bits long typically (i.e. 4 or 8 bytes
492*44704f69SBart Van Asscherespectively). The host machine representation may be big or little endian
493*44704f69SBart Van Asscheand may prefer or require alignment to a particular memory address boundary
494*44704f69SBart Van Assche(e.g. module 4 (or in 'C' code: "(lba % 4) == 0")). For SCSI commands and
495*44704f69SBart Van Asschethe parameter data moved to or from a SCSI device, the integer
496*44704f69SBart Van Asscherepresentation is big endian and it is unaligned.
497*44704f69SBart Van Assche
498*44704f69SBart Van AsscheRecent versions of this package have replaced the explicit use of the C
499*44704f69SBart Van Asscheshift operators with a group of functions modelled on those found in the
500*44704f69SBart Van AsscheLinux kernel. These functions contain either "get_unaligned" or
501*44704f69SBart Van Assche"put_unaligned" in their names and are found in the asm/unaligned.h
502*44704f69SBart Van Asscheheader. This package contains the sg_unaligned.h header that implements
503*44704f69SBart Van Asschea similar set of functions. The current implementation favours correctness
504*44704f69SBart Van Asscheover speed. The functions in the package use a "sg_" prefix but otherwise
505*44704f69SBart Van Asscheuse the same function name as the Linux kernel for the same action.
506*44704f69SBart Van Assche
507*44704f69SBart Van AsscheAn example of the change made to a snippet of sg_write_buffer.c may
508*44704f69SBart Van Asscheclarify this change. The old code was:
509*44704f69SBart Van Assche
510*44704f69SBart Van Assche   wbufCmdBlk[3] = (unsigned char)((buffer_offset >> 16) & 0xff);
511*44704f69SBart Van Assche   wbufCmdBlk[4] = (unsigned char)((buffer_offset >> 8) & 0xff);
512*44704f69SBart Van Assche   wbufCmdBlk[5] = (unsigned char)(buffer_offset & 0xff);
513*44704f69SBart Van Assche
514*44704f69SBart Van Asscheand it has been replaced by:
515*44704f69SBart Van Assche
516*44704f69SBart Van Assche   sg_put_unaligned_be24(buffer_offset, wbufCmdBlk + 3);
517*44704f69SBart Van Assche
518*44704f69SBart Van AsscheThe Linux kernel only supplies "unaligned" functions for 16, 32 and 64
519*44704f69SBart Van Asschebit quantities. SCSI commands also have cases of 24 and 48 bit numbers
520*44704f69SBart Van Asscheso sg_unaligned.h contains support for those plus a variant where the
521*44704f69SBart Van Asschebyte length is passed as an argument.
522*44704f69SBart Van Assche
523*44704f69SBart Van AsscheThe unaligned functions are inlined for speed (at the possible expense of
524*44704f69SBart Van Asschespace) and now have specializations depending whether the host is big or
525*44704f69SBart Van Assche(more likely) little endian. These functions can be broken down to a
526*44704f69SBart Van Asschememcpy() and optionally a byte-swap for 16, 32 and 64 bit operations.
527*44704f69SBart Van AsscheThe memcpy() takes care of alignment while the byte-swap (bswap_16(),
528*44704f69SBart Van Asschebswap_32() and bswap_64() ) addresses integer endianness. If the host is
529*44704f69SBart Van Asschelittle endian and a little endian variant of the unaligned functions is
530*44704f69SBart Van Asscherequested, then no byte-swap is required. These specializations can be
531*44704f69SBart Van Assche"compiled out" with this configure option: './configure
532*44704f69SBart Van Assche--disable-fast-lebe' in which case the classic "C shifting" technique is
533*44704f69SBart Van Asscheused to implement all the unaligned functions.
534*44704f69SBart Van Assche
535*44704f69SBart Van AsscheAssociated with the above change, fixed length integer types seem a better
536*44704f69SBart Van Asschefit for SCSI command and parameter integers than the traditional integer
537*44704f69SBart Van Asschetypes in the C language. Fixed length integer types were standardized in
538*44704f69SBart Van AsscheC99 and require the inclusion of <stdint.h>. For example this means for
539*44704f69SBart Van Asschean integer that will represent a 64 bit LBA, to favour using "uint64_t"
540*44704f69SBart Van Asscheover the "unsigned long long" type. Also "unsigned char" has mostly been
541*44704f69SBart Van Asschereplaced by "uint8_t" as the 8 bit (unsigned) byte type; "char" is still
542*44704f69SBart Van Asscheused for ASCII text.
543*44704f69SBart Van Assche
544*44704f69SBart Van Assche
545*44704f69SBart Van AsscheCoding Style
546*44704f69SBart Van Assche============
547*44704f69SBart Van AsscheEveryone has their own C/C++ coding style and the author is no different.
548*44704f69SBart Van AsscheIn terms of the GNU indent command:
549*44704f69SBart Van Assche     indent -i4 -il0 -nut -br -npcs -ncs -ce
550*44704f69SBart Van Asscheis pretty close. That is similar to the Linux kernel coding style but
551*44704f69SBart Van Asschewith 4 space indentations and no tabs.
552*44704f69SBart Van Assche
553*44704f69SBart Van Assche
554*44704f69SBart Van AsscheOther SCSI and storage tools
555*44704f69SBart Van Assche============================
556*44704f69SBart Van AsscheSee https://sg.danny.cz/sg/tools.html
557*44704f69SBart Van Assche
558*44704f69SBart Van Assche
559*44704f69SBart Van AsscheDouglas Gilbert		[email protected]
560*44704f69SBart Van Assche26th August 2022
561