82
120
- using the SG_IO ioctl [for synchronous access]
83
121
- using a write()/read() sequence that convey instances of "sg_io_hdr"
85
All utilities in the main directory use the SG_IO ioctl except for sgp_dd.
86
This is due to the asynchronous nature of sgp_dd. Note that sgp_dd does _not_
87
support the "blk_sgio" switch found in sg_dd. This is important since block
88
devices often identify themselves (programmatically) as sg devices in lk 2.6
89
and it would cause serious damage to do a write() of the sg driver's "sg_io_hdr"
90
metadata (i.e. disk corruption).
123
All utilities in the main directory that issue SCSI commands use the SG_IO
124
ioctl except for sgp_dd. This is due to the asynchronous nature of sgp_dd.
125
Note that sgp_dd does _not_ support the "blk_sgio" switch found in sg_dd.
126
This is important since block devices often identify themselves
127
(programmatically) as sg devices in lk 2.6 and it would cause serious damage
128
to do a write() of the sg driver's "sg_io_hdr" meta data (i.e. disk
133
Many devices use SCSI command sets over transport protocols not normally
134
associated with SCSI (as defined at http://www.t10.org ). Some of these
135
devices react poorly (e.g. lock up) when sent SCSI commands that they
136
don't support. Even sending a supported SCSI command with a field set
137
to an unexpected value can cause problems.
139
For example, all "SCSI" devices must support the INQUIRY command which
140
the SCSI-2 standard says should request a 36 byte response. However later
141
SCSI standards (e.g. SPC-2) have increased that length but some SCSI
142
devices lock up when they receive a request for anything other than
145
Any well implemented "SCSI" device should react sensibly when a utility in
146
sg3_utils sends a SCSI command that it doesn't support. Unfortunately this
147
cannot be guaranteed.
95
Here is a (somewhat arbitrary) categorization of the utilities included
97
1) dd variants: sg_dd, sgp_dd, sgm_dd and sgq_dd
98
2) scanning and mapping: sg_scan, sg_map and scsi_devfs_scan
99
3) SCSI support: sg_inq, scsi_inquiry, sginfo, sg_readcap, sg_start,
100
sg_modes, sg_logs, sg_senddiag, sg_reset, sg_opcodes, sg_persist,
101
sg_write_long and sg_read_long
102
4) timing and testing: sg_rbuf, sg_test_rwbuf, sg_read, sg_turs,
104
5) example programs: sg_simple1, sg_simple2, sg_simple3, sg_simple4
106
6) miscellaneous programs: isosize, hxascdmp
108
There is a brief description of each utility in the following sections.
109
All utilities in the main directory have a "man" page in section 8
110
which is a category for administration and privileged commands. Some
111
utilities in the other (sub-)directories have man pages.
116
The main utility is a variant of the standard Unix command "dd" that
117
is called "sg_dd". This program takes a useful subset of the command
118
line arguments that "dd" takes. Furthermore "sg_dd" will only work if
119
one or both of the given files (ie "if" or "of") is an "sg" or a raw
120
device. If "bs" (block size) is not given it is assumed to be 512 bytes.
121
Available dd options:
122
bs=<n> typically 512 or 2048
123
ibs=<n> if given must be the same as "bs"
124
obs=<n> if given must be the same as "bs"
125
if=<name> like dd plus sg device or "-" (read from stdin)
126
of=<name> like dd plus sg device or "-" (write to stdout)
127
skip=<n> block offset to start reading "if"
128
seek=<n> block offset to start writing "of"
130
bpt=<n> blocks per transfer (default 128)
131
dio=<n> 0 or 1, request direct IO (default 0)
132
cdbsz=6|10|12|16 allow the command size of SCSI READ and WRITE commands
133
to be specified (default is 10)
134
sync=0|1 when 1, do a SYNCHRONIZE CACHE on "of" after the transfer
136
fua=0|1|2|3 when 0, do not set 'force unit access' bit; when 1, set it
137
on "of"; when 2, set it on "if"; when 3, set it on "if"+"of"
138
time=0|1 times the transfer and calculates a throughput figure when
139
1, output sent to stderr. Default is 0
140
coe=0|1 continue on error (only on sg devices) when 1. Default is 0.
142
Numeric arguments can be given in decimal or hexadecimal. Hex arguments should
143
be prefixed with "0x" or "0X". Decimal arguments can take multiplier suffixes:
148
"m" * 1048576 [2 ^ 20]
149
"M" * 1000000 [10 ^ 6]
150
"g" * 1073741824 [2 ^ 30]
151
"G" * 1000000000 [10 ^ 9]
152
"t" * 1099511627776 [2 ^ 40]
153
"T" * 1000000000000 [10 ^ 12]
154
The 'skip' and 'seek' options lead to the use of the system command
155
lseek() to a byte offset when used on raw devices and normal files.
156
[For sg devices 32 bit block addresses are used thus limiting accesses
157
on disks with 512 byte blocks to 1 TB.] On 32 bit architectures the
158
normal lseek() is limited to a signed 32 bit byte offset (i.e. 2 GB).
159
"sg_dd" bypasses this limit by using Linux's _llseek() [while modern
160
"dd" commands use read loops to "walk" around the limit].
161
If 'count' is not given then the SCSI READ CAPACITY command will be
162
used (on sg devices) if appropriate. Block devices are queried for their
163
length if 'count' is not given. [Note that READ CAPACITY often
164
gives a 2 block overestimate for iso9660 file systems on CD-ROMs.
165
See the "isosize" command below.] Disk partition information can
166
be found with a command like "fdisk -ul /dev/sda". The 'dio' argument
167
requests direct IO (only functions in 2.4 kernels). A warning is issued
168
if direct IO is requested and /proc/scsi/sg/allow_dio == 0 .
169
"of=/dev/null" causes writes to be skipped, and "of=." is accepted as
170
an alias for "of=/dev/null" (convenient shorthand).
172
"sgp_dd" uses POSIX threads and attempts to run multiple IO operations
173
in parallel. The user can control the amount of parallelism from
174
1 worker (i.e. single threaded) through to 16 worker threads. This is
175
done via the "thr=<n>" option (default 4). Copies from one sg device to
176
another can be considerably faster due to this parallelism. There is
177
also some speed benefit when raw devices are used. To obtain fine level
178
SCSI command level control, sg devices should be used rather than block
179
devices which will be interpreted as normal Unix files.
181
"sgm_dd" is very similar to sg_dd but it uses mmap-ed IO on one of its
182
given sg device names. If both "if" and "of" are sg devices then mmap-ed
183
IO is selected on "if" while normal IO is selected on "of". Direct IO
184
cannot be selected with this command. To obtain fine level SCSI command
185
level control, sg devices should be used rather than block devices which
186
will be interpreted as normal Unix files.
188
"sgq_dd" is yet another implementation found in the archive directory.
189
From the user point of view it is very similar to sgp_dd but uses a
190
non-blocking state machine rather then POSIX threads for parallelism.
193
2) Scanning and mapping
194
-----------------------
195
"sg_scan" does a scan of all sg device and prints the results to
196
standard output. Alternatively one or more device names can be
197
given (e.g. 'sg_scan -i /dev/scd[01]') and these will be scanned instead
198
of the sg devices. Additional information from the INQUIRY command can
199
be obtained by using the "-i" option.
201
"sg_map" shows the mapping between sg device names and those of the
202
sd, sr and st device names. Some devices such as scanners have no
203
corresponding sd, sr nor st device names.
205
"scsi_devfs_scan" is a utility for doing a directory scan on a system
206
running devfs to identify SCSI (and optionally IDE) devices. Various
207
information (including an INQUIRY) can be listed for each found device.
212
"sg_inq" is a utility for examining the INQUIRY command which
213
contains much interesting information. It is based on SCSI 3's SPC-2
214
document. Has switches to output "vital product data" and "command support"
215
(now obsolete) pages. It can output in formatted ASCII, hex or binary.
216
Has '-i' to decode the now mandatory device identification VPD page (0x83).
217
This command is applicable to SCSI 2 (and perhaps SCSI 1) devices as well.
152
Here is list in alphabetical order of utilities found in the main directory
153
of the sg3_utils directory:
154
- sginfo, sgm_dd, sgp_dd, sg_dd, sg_emc_trespass, sg_get_config,
155
sg_format, sg_ident, sg_inq, sg_logs, sg_luns, sg_map, sg_map26,
156
sg_modes, sg_opcodes, sg_persist, sg_prevent, sg_rbuf, sg_rdac, sg_read,
157
sg_readcap, sg_read_long, sg_reassign, sg_request, sg_reset, sg_rmsn,
158
sg_rtpg, sg_scan, sg_senddiag, sg_ses, sg_start, sg_sync,
159
sg_test_rwbuff, sg_turs, sg_verify, sg_vpd, sg_write_long, sg_wr_mode
161
These utilities and the libsgutils.so library which they depend on are built
162
by the Makefile in the main directory. This Makefile does not invoke the
163
Makefile's in the subdirectories. Each utility in the main directory has a
164
"man" page in section 8 (system administration commands). Binary
165
distributions of the sg3_utils package (e.g. "rpm" and debian packages)
166
typically contains the shared library, the utilities in the main directory,
167
their associated man pages and some documentation files (e.g. README, INSTALL,
168
CREDITS, COPYING and COVERAGE). See the INSTALL file for instructions about
169
building and installing from a "tarball".
171
Man pages can be read (without building and installing the package) by
172
going to the main directory and executing something like this:
175
To see which SCSI commands (and ATA commands) are used by these
176
utilities see the COVERAGE file.
178
Here is a list in alphabetical order of utilities found in the archive
180
- isosize, scsi_devfs_scan, sg_debug, sgq_dd
181
Some of these utilities have man pages.
183
Here is a list in alphabetical order of utilities found in the examples
185
- scsi_inquiry, sg_simple1, sg_simple2, sg_simple3, sg_simple4,
186
sg_simple5 and sg_simple16.c
188
Also in that subdirectory is a script to test sg_persist, an example
189
data file for sg_persist (called "transport_ids.txt") and an example
190
data file for sg_reassign (called "reassign_addr.txt"). There are also
191
two scripts for 'sg_senddiag -pf -raw=-' that will put some SAS disk
192
phys into a compliant jitter tolerance pattern (CJTPAT).
194
The "utils" subdirectory contains source and a Makefile to build
195
"hxascdmp" which accepts binary data from stdin (or a file on the
196
command line) and outputs an ASCII-HEX and ASCII representation of
197
it. It is similar to the Unix od command. There is also code to
198
sg_chk_asc.c which checks a given text file (typically a copy of
199
http://www.t10.org/lists/asc-num.txt ) and checks it against the
200
asc/ascq test strings held in sg_lib.c .
202
The "doc" subdirectory contains copies of web pages relevant to
203
the sg3_utils package. Currently it contains:
204
- sg_dd.html : describes the sg_dd utility and compares it with the
206
- sg_io.html : describes the SG_IO ioctl used by sg3_utils. Looks
207
at differences between the lk 2.4 and 2.6 series;
208
including its use with block devices
209
- sg3_utils.html : a description of the sg3_utils package contents
210
- tools.html : a list of SCSI and storage tools with a summary and url
213
Notes for utilities without man pages
214
=====================================
219
215
The "scsi_inquiry" program shows the use of the SCSI_IOCTL_SEND_COMMAND
220
216
ioctl to send a SCSI INQUIRY command. That ioctl() is supported by the
221
217
SCSI sub system mid level and so is common to all sd, sr, st and sg devices.
222
218
This program has been placed in the "examples" subdirectory.
224
"sginfo" is a port of the "scsiinfo" program by Eric Youngdale to use the sg
225
devices. It can still take other SCSI device nodes (e.g. /dev/sda, /dev/st0
226
or /dev/hdc (if "hdc" is an ATAPI device)). This program outputs mode
227
page (and subpage) information and allows it to be modified. Additionally
228
defect lists (for disks) can be output and information from the SCSI INQUIRY
229
command is available.
231
"sg_readcap" call a READ CAPACITY command on the given device. Now also
232
supports "partial medium indicator" (PMI) flag that indicates where the
233
next significant delay will be on the media (after a given address).
235
"sg_start" has been provided by Kurt Garloff <garloff at suse dot de>
236
for spinning up (or down) disks.
237
see the README.sg_start file.
239
"sg_modes" and "sg_logs" print out mode sense pages and log sense pages
242
"sg_senddiag" permits various device self tests to be run. It can also
243
list the diagnostic pages support by a device.
245
"sg_reset" exercises the SCSI device/bus/host reset capability. It is
246
supported by the sg driver in lk 2.2.16 and beyond but associated
247
SCSI middle level driver changes have not been accepted into the
248
standard kernel at this time. Many distributions contain the patch to
249
the mid-level that activates this feature. This patch is in later
250
lk 2.4 versions and in the lk 2.6 series. This utility will only
251
work on sg device names.
253
"sg_opcodes" lists available opcode names. It uses the REPORT SUPPORTED
254
OPERATION CODES SCSI command which was introduced in February 2002
255
(and replaced the Cmd modes (CmdDt bit) in the INQUIRY command). Opcode
256
names include various SCSI commands with service actions (e.g.
257
the Maintenance In command) and variable length commands. The command
258
list is either unsorted (as returned by the device), numerically sorted
259
(default) or alphabetically sorted. An individual opcode (and optionally
260
a service action) can be given in which case the "changeable" bits of
261
that command are output. With the '-t' command line option this utility
262
will list supported task management functions.
264
"sg_persist" accesses the Persistent Reservation In and Out commands.
265
These are relatively complex commands and potential users are referred
266
to the SPC-3 document at http://www.t10.org . That said, it is
267
relatively safe to query the state of existing registrations and
268
reservations with the PR In command (use the switch "--in" with
269
sg_persist to enforce this).
271
"sg_write_long" can be used to write a "long" block to the given device
272
(typically a disk). A "long" block includes data, ECC information and
273
and other vendor specific information.
275
"sg_read_long" can be used to read a "long" block from the given device
279
4) Timing and testing
280
---------------------
281
"sg_rbuf" does repeated SCSI READ BUFFER commands which allows SCSI
282
bus bandwidth and the SCSI sub-system throughput to be measured.
283
This can be done in 4 modes: normal transfer to user space, no
284
transfer to user space, direct IO or mmap-ed IO. The latter one wins
287
"sg_test_rwbuf" is a program by Kurt Garloff <garloff at suse dot de> that
288
has the following description: Program to test the SCSI host adapter by
289
issuing write and read operations on a device's buffer and calculating
292
"sg_read" reads multiple blocks of data starting at the same logical
293
address. It can time the transfers (potentially ignoring the first
294
issued command) and calculate a MB/sec figure. [In keeping with most
295
disk manufacturers, "MB" is 1,000,000 bytes in this context.] Its
296
command line syntax is modelled on "sg_dd". It allows SCSI device,
297
SCSI bus bandwidth and the SCSI sub-system throughput to be measured.
298
This can be done in 3 modes: normal transfer to user space, direct
301
"sg_turs" executes a user specified number of TEST UNIT READY commands on
302
the given device. This can be used to time SCSI command overhead.
220
"sgq_dd" is yet another variant of dd found in the archive directory.
221
From the user's point of view it is very similar to sgp_dd but uses a
222
non-blocking state machine rather then POSIX threads for parallelism.
304
224
"sg_debug" is effectively defunct now. The user can instead do:
305
225
$ cat /proc/scsi/sg/debug . This command has been placed in the
306
226
archive directory.
311
228
"sg_simple1" and "sg_simple2" are simple example programs demonstrating
312
229
calls to the SCSI INQUIRY and TEST UNIT READY commands. They only differ
313
in their error processing: sg_simple1 uses sg_err.[hc] for error
230
in their error processing: sg_simple1 uses sg_lib.[hc] for error
314
231
processing while sg_simple2 does its own more primitive checks.
316
233
"sg_simple3" tests out user space scatter gather added to the version 3
319
236
"sg_simple4" shows the INQUIRY command using mmap-ed IO to obtain its
239
"sg_simple5" also sends and INQUIRY and TEST UNIT READY commands. It
240
uses the generic pass through mechanism based on sg_pt.h . It will
241
currently build in linux and FreeBSD (with "make -f Makefile.freebsd").
242
It has extensive error checking code.
322
244
"sg_simple16" attempts to send a 16 byte SCSI command, READ_16, to the
323
245
scsi device. This is only supported for lk >= 2.4.15 and for adapter
324
246
drivers that indicate that they have 16 byte CDB capability (otherwise
325
247
DID_ABORT will appear in the host_status).
327
All these example programs (plus scsi_inquiry) have been placed in the
328
"examples" subdirectory with their own Makefile.
333
"isosize" is a utility that gives the number of bytes in an iso9660
334
file system. It is a rewrite by Andries Brouwer<Andries.Brouwer at cwi dot nl>
335
of a utility that first appeared in the cdwrite package but is now
336
difficult to obtain. Note that the value given by isosize is usually
337
2 or more blocks less than the READ CAPACITY SCSI command yields on
338
a CD-ROM (due to run out sectors). This command has a "man" page
339
[section 8]. This utility has been moved to the archive directory
340
as isosize is now available in the util-linux-2.10s package (and later).
344
A Makefile is provided that builds the above utilities and test programs
345
'make' and 'make all' will cause everything (that is stale) to be built.
347
A complete rebuild can be forced by executing 'make clean' prior to
348
any of the above make commands. Individual commands can be built be
349
giving the executable name to make, for example: 'make sg_dd'.
351
There is also a 'make dep' but that shouldn't be needed very often.
352
A 'make install' will build if necessary and then install the
353
executables into /usr/local/bin by default (controlled by variable
354
INSTDIR). The "man" pages are loaded int /usr/local/man/man8 by default.
358
These utilities include 2 special Linux header files:
249
"sg_sat_identify" attempts to push an ATA IDENTIFY DEVICE or
250
IDENTIFY pACKET DEVICE command through the SAT-defined ATA PASS
251
THROUGH (16) SCSI command. If successful, the 256 word (512 byte)
254
"sg_sat_chk_power" attempts to push an ATA CHECK POWER MODE command
255
through the SAT-defined ATA PASS THROUGH (16) SCSI command. That
256
ATA command needs to read the "FIS" registers after the command is
257
completed which involves using the ATA Status Return (sense data)
258
descriptor (as defined in SAT).
260
"sg_sat_smart_rd_data" attempts to push an ATA SMART/READ DATA command
261
through the SAT-defined ATA PASS THROUGH (16) SCSI command. If
262
successful, the 256 word (512 byte) response is output.
265
Command line processing
266
=======================
267
These utilities can be divided into 3 groups when their handling of
268
command line arguments is considered:
269
- ad hoc, typically in a short form only, sometimes longer (e.g.
270
"sg_logs -pcb /dev/sdc")
271
- inspired by the dd Unix command (e.g. sg_dd, sgm_dd, sgp_dd, sg_read)
272
- recent utilities use "getopt_long" (see "man getopt_long")
273
type command lines. These have short form (starting with "-")
274
and corresponding longer form (starting with "--") options.
276
The recent utilities that use "getopt_long" are, in alphabetical
278
- sg_format sg_get_config sg_ident sg_luns sg_map26 sg_persist
279
sg_prevent sg_read_long sg_reassign sg_requests sg_rmsn sg_rtpg
280
sg_ses sg_sync sg_test_rwbuf sg_verify sg_vpd sg_write_long sg_wr_mode
283
Linux header file problems
284
==========================
285
In linux some utilities include 2 important header files:
359
286
#include <scsi/sg.h>
360
287
#include <scsi/scsi.h>
361
288
These files are typically found in the directory /usr/include/scsi which