xref: /freebsd/lib/libcam/cam_cdbparse.3 (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1.\"
2.\" Copyright (c) 1998 Kenneth D. Merry.
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\" 3. The name of the author may not be used to endorse or promote products
14.\"    derived from this software without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" $FreeBSD$
29.\"
30.\" This man page borrows heavily from the old scsi(3) man page, which had
31.\" the following copyright:
32.\"
33.\" Copyright (c) 1994 HD Associates (hd@world.std.com)
34.\" All rights reserved.
35.\"
36.\" Redistribution and use in source and binary forms, with or without
37.\" modification, are permitted provided that the following conditions
38.\" are met:
39.\" 1. Redistributions of source code must retain the above copyright
40.\"    notice, this list of conditions and the following disclaimer.
41.\" 2. Redistributions in binary form must reproduce the above copyright
42.\"    notice, this list of conditions and the following disclaimer in the
43.\"    documentation and/or other materials provided with the distribution.
44.\" 3. All advertising materials mentioning features or use of this software
45.\"    must display the following acknowledgement:
46.\"	This product includes software developed by HD Associates
47.\" 4. Neither the name of the HD Associates nor the names of its contributors
48.\"    may be used to endorse or promote products derived from this software
49.\"    without specific prior written permission.
50.\"
51.\" THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES``AS IS'' AND
52.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54.\" ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
55.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61.\" SUCH DAMAGE.
62.\"
63.\"
64.Dd March 13, 2017
65.Dt CAM_CDBPARSE 3
66.Os
67.Sh NAME
68.Nm csio_build ,
69.Nm csio_build_visit ,
70.Nm csio_decode ,
71.Nm csio_decode_visit ,
72.Nm buff_decode ,
73.Nm buff_decode_visit ,
74.Nm csio_encode ,
75.Nm csio_encode_visit ,
76.Nm buff_encode_visit
77.Nd CAM user library SCSI buffer parsing routines
78.Sh LIBRARY
79.Lb libcam
80.Sh SYNOPSIS
81.In stdio.h
82.In camlib.h
83.Ft int
84.Fo csio_build
85.Fa "struct ccb_scsiio *csio"
86.Fa "uint8_t *data_ptr"
87.Fa "uint32_t dxfer_len"
88.Fa "uint32_t flags"
89.Fa "int retry_count"
90.Fa "int timeout"
91.Fa "const char *cmd_spec"
92.Fa "..."
93.Fc
94.Ft int
95.Fo csio_build_visit
96.Fa "struct ccb_scsiio *csio"
97.Fa "uint8_t *data_ptr"
98.Fa "uint32_t dxfer_len"
99.Fa "uint32_t flags"
100.Fa "int retry_count"
101.Fa "int timeout"
102.Fa "const char *cmd_spec"
103.Fa "int (*arg_get)(void *hook, char *field_name)"
104.Fa "void *gethook"
105.Fc
106.Ft int
107.Fo csio_decode
108.Fa "struct ccb_scsiio *csio"
109.Fa "const char *fmt"
110.Fa "..."
111.Fc
112.Ft int
113.Fo csio_decode_visit
114.Fa "struct ccb_scsiio *csio"
115.Fa "const char *fmt"
116.Fa "void (*arg_put)(void *hook"
117.Fa "int letter"
118.Fa "void *val"
119.Fa "int count"
120.Fa "char *name)"
121.Fa "void *puthook"
122.Fc
123.Ft int
124.Fo buff_decode
125.Fa "uint8_t *buff"
126.Fa "size_t len"
127.Fa "const char *fmt"
128.Fa "..."
129.Fc
130.Ft int
131.Fo buff_decode_visit
132.Fa "uint8_t *buff"
133.Fa "size_t len"
134.Fa "const char *fmt"
135.Fa "void (*arg_put)(void *, int, void *, int, char *)"
136.Fa "void *puthook"
137.Fc
138.Ft int
139.Fo csio_encode
140.Fa "struct ccb_scsiio *csio"
141.Fa "const char *fmt"
142.Fa "..."
143.Fc
144.Ft int
145.Fo csio_encode_visit
146.Fa "struct ccb_scsiio *csio"
147.Fa "const char *fmt"
148.Fa "int (*arg_get)(void *hook, char *field_name)"
149.Fa "void *gethook"
150.Fc
151.Ft int
152.Fo buff_encode_visit
153.Fa "uint8_t *buff"
154.Fa "size_t len"
155.Fa "const char *fmt"
156.Fa "int (*arg_get)(void *hook, char *field_name)"
157.Fa "void *gethook"
158.Fc
159.Sh DESCRIPTION
160The CAM buffer/CDB encoding and decoding routines provide a relatively easy
161migration path for userland
162.Tn SCSI
163applications written with the similarly-named
164.Va scsireq_ Ns *
165functions from the old
166.Fx
167.Tn SCSI
168layer.
169.Pp
170These functions may be used in new applications, but users may find it
171easier to use the various SCSI CCB building functions included with the
172.Xr cam 3
173library, e.g., \&
174.Fn cam_fill_csio ,
175.Fn scsi_start_stop ,
176and
177.Fn scsi_read_write .
178.Pp
179.Fn csio_build
180builds up a
181.Va ccb_scsiio
182structure based on the information provided in
183the variable argument list.
184It gracefully handles a NULL
185.Fa data_ptr
186argument passed to it.
187.Pp
188.Fa dxfer_len
189is the length of the data phase; the data transfer direction is
190determined by the
191.Fa flags
192argument.
193.Pp
194.Fa data_ptr
195is the data buffer used during the
196.Tn SCSI
197data phase.
198If no data is to be
199transferred for the
200.Tn SCSI
201command in question, this should be set to NULL.
202If there is data to
203transfer for the command, this buffer must be at least
204.Fa dxfer_len
205long.
206.Pp
207.Fa flags
208are the flags defined in
209.In cam/cam_ccb.h :
210.Bd -literal
211/* Common CCB header */
212/* CAM CCB flags */
213typedef enum {
214     CAM_CDB_POINTER       = 0x00000001,/* The CDB field is a pointer    */
215     CAM_SCATTER_VALID     = 0x00000010,/* Scatter/gather list is valid  */
216     CAM_DIS_AUTOSENSE     = 0x00000020,/* Disable autosense feature     */
217     CAM_DIR_RESV          = 0x00000000,/* Data direction (00:reserved)  */
218     CAM_DIR_IN            = 0x00000040,/* Data direction (01:DATA IN)   */
219     CAM_DIR_OUT           = 0x00000080,/* Data direction (10:DATA OUT)  */
220     CAM_DIR_NONE          = 0x000000C0,/* Data direction (11:no data)   */
221     CAM_DIR_MASK          = 0x000000C0,/* Data direction Mask		 */
222     CAM_DEV_QFRZDIS       = 0x00000400,/* Disable DEV Q freezing	 */
223     CAM_DEV_QFREEZE       = 0x00000800,/* Freeze DEV Q on execution     */
224     CAM_HIGH_POWER        = 0x00001000,/* Command takes a lot of power  */
225     CAM_SENSE_PTR         = 0x00002000,/* Sense data is a pointer	 */
226     CAM_SENSE_PHYS        = 0x00004000,/* Sense pointer is physical addr*/
227     CAM_TAG_ACTION_VALID  = 0x00008000,/* Use the tag action in this ccb*/
228     CAM_PASS_ERR_RECOVER  = 0x00010000,/* Pass driver does err. recovery*/
229     CAM_DIS_DISCONNECT    = 0x00020000,/* Disable disconnect		 */
230     CAM_SG_LIST_PHYS      = 0x00040000,/* SG list has physical addrs.   */
231     CAM_DATA_PHYS         = 0x00200000,/* SG/Buffer data ptrs are phys. */
232     CAM_CDB_PHYS          = 0x00400000,/* CDB pointer is physical	 */
233
234/* Host target Mode flags */
235     CAM_SEND_SENSE        = 0x08000000,/* Send sense data with status   */
236     CAM_SEND_STATUS       = 0x80000000,/* Send status after data phase  */
237} ccb_flags;
238.Ed
239.Pp
240Multiple flags should be ORed together.
241Any of the CCB flags may be used,
242although it is worth noting several important ones here:
243.Bl -tag -width CAM_PASS_ERR_RECOVER
244.It Dv CAM_DIR_IN
245This indicates that the operation in question is a read operation.
246i.e.,
247data is being read from the
248.Tn SCSI
249device to the user-supplied buffer.
250.It Dv CAM_DIR_OUT
251This indicates that the operation is a write operation.
252i.e., data is being
253written from the user-supplied buffer to the device.
254.It Dv CAM_DIR_NONE
255This indicates that there is no data to be transferred for this command.
256.It Dv CAM_DEV_QFRZDIS
257This flag disables device queue freezing as an error recovery mechanism.
258.It Dv CAM_PASS_ERR_RECOVER
259This flag tells the
260.Xr pass 4
261driver to enable error recovery.
262The default is to not perform error
263recovery, which means that the retry count will not be honored without this
264flag, among other things.
265.It Dv CAM_DATA_PHYS
266This indicates that the address contained in
267.Fa data_ptr
268is a physical address, not a virtual address.
269.El
270.Pp
271The
272.Fa retry_count
273tells the kernel how many times to retry the command in question.
274The
275retry count is ignored unless the
276.Xr pass 4
277driver is told to enable error recovery via the
278.Dv CAM_PASS_ERR_RECOVER
279flag.
280.Pp
281The
282.Fa timeout
283tells the kernel how long to wait for the given command to complete.
284If
285the timeout expires and the command has not completed, the CCB will be
286returned from the kernel with an appropriate error status.
287.Pp
288.Fa cmd_spec
289is a CDB format specifier used to build up the SCSI CDB.
290This text string is made up of a list of field specifiers.
291Field
292specifiers specify the value for each CDB field (including indicating
293that the value be taken from the next argument in the
294variable argument list), the width
295of the field in bits or bytes, and an optional name.
296White space is
297ignored, and the pound sign ('#') introduces a comment that ends at the
298end of the current line.
299.Pp
300The optional name is the first part of a field specifier and
301is in curly braces.
302The text in curly braces in this example are
303the names:
304.Dl "{PS} v:b1 {Reserved} 0:b1 {Page Code} v:b6 # Mode select page"
305.Pp
306This field specifier has two one bit fields and one six bit field.
307The second one bit field is the constant value 0 and the first
308one bit field and the six bit field are taken from the variable
309argument list.
310Multi byte fields are swapped into the SCSI byte order in the
311CDB and white space is ignored.
312.Pp
313When the field is a hex value or the letter v, (e.g.,
314.Fa "1A"
315or
316.Fa "v" )
317then a single byte value
318is copied to the next unused byte of the CDB.
319When the letter
320.Fa v
321is used the next integer argument is taken from the variable argument list
322and that value used.
323.Pp
324A constant hex value followed by a field width specifier or the letter
325.Fa v
326followed by a field width specifier (e.g.,
327.Fa 3:4 ,
328.Fa 3:b4 ,
329.Fa 3:i3 ,
330.Fa v:i3 )
331specifies a field of a given bit or byte width.
332Either the constant value or (for the V specifier) the next integer value from
333the variable argument list is copied to the next unused
334bits or bytes of the CDB.
335.Pp
336A decimal number or the letter
337.Fa b
338followed by a decimal number field width indicates a bit field of that width.
339The bit fields are packed as tightly as possible beginning with the
340high bit (so that it reads the same as the SCSI spec), and a new byte of
341the CDB is started whenever a byte fills completely or when an
342.Fa i
343field is encountered.
344.Pp
345A field width specifier consisting of the letter
346.Fa i
347followed by either
3481, 2, 3 or 4 indicates a 1, 2, 3 or 4 byte integral value that must
349be swapped into SCSI byte order (MSB first).
350.Pp
351For the
352.Fa v
353field specifier the next integer argument is taken from the variable argument
354list and that value is used swapped into SCSI byte order.
355.Pp
356.Fn csio_build_visit
357operates similarly to
358.Fn csio_build ,
359except that the values to substitute for variable arguments in
360.Fa cmd_spec
361are retrieved via the
362.Fn arg_get
363function passed in to
364.Fn csio_build_visit
365instead of via
366.Xr stdarg 3 .
367The
368.Fn arg_get
369function takes two arguments:
370.Bl -tag -width field_name
371.It Fa gethook
372is passed into the
373.Fn arg_get
374function at each invocation.
375This enables the
376.Fn arg_get
377function to keep some state in between calls without using global or static
378variables.
379.It Fa field_name
380is the field name supplied in
381.Fa fmt ,
382if any.
383.El
384.Pp
385.Fn csio_decode
386is used to decode information from the data in phase of the SCSI
387transfer.
388.Pp
389The decoding is similar to
390the command specifier processing of
391.Fn csio_build
392except that the data is extracted from the data pointed to by
393.Fa csio->data_ptr .
394The stdarg list should be pointers to integers instead of integer
395values.
396A seek field type and a suppression modifier are added.
397The
398.Fa *
399suppression modifier (e.g.,
400.Fa *i3
401or
402.Fa *b4 )
403suppresses assignment from the field and can be used to skip
404over bytes or bits in the data, without having to copy
405them to a dummy variable in the arg list.
406.Pp
407The seek field type
408.Fa s
409permits you to skip over data.
410This seeks to an absolute position
411.Pq Fa s3
412or a relative position
413.Pq Fa s+3
414in the data, based on whether or not the presence of the '+' sign.
415The seek value can be specified as
416.Fa v
417and the next integer value from the argument list will be
418used as the seek value.
419.Pp
420.Fn csio_decode_visit
421operates like
422.Fn csio_decode
423except that instead of placing the decoded contents of the buffer in
424variadic arguments, the decoded buffer contents are returned to the user
425via the
426.Fn arg_put
427function that is passed in.
428The
429.Fn arg_put
430function takes several arguments:
431.Bl -tag -width letter
432.It Fa hook
433The "hook" is a mechanism to allow the
434.Fn arg_put
435function to save state in between calls.
436.It Fa letter
437is the letter describing the format of the argument being passed into the
438function.
439.It Fa val
440is a void pointer to the value being passed into the function.
441.It Fa count
442is the size of the value being passed into the
443.Fn arg_put
444function.
445The argument format determines the unit of measure.
446.It Fa name
447This is a text description of the field, if one was provided in the
448.Fa fmt .
449.El
450.Pp
451.Fn buff_decode
452decodes an arbitrary data buffer using the method
453described above for
454.Fn csio_decode .
455.Pp
456.Fn buff_decode_visit
457decodes an arbitrary data buffer using the method described above for
458.Fn csio_decode_visit .
459.Pp
460.Fn csio_encode
461encodes the
462.Fa data_ptr
463portion (not the CDB!) of a
464.Va ccb_scsiio
465structure, using the method described above for
466.Fn csio_build .
467.Pp
468.Fn csio_encode_visit
469encodes the
470.Fa data_ptr
471portion (not the CDB!) of a
472.Va ccb_scsiio
473structure, using the method described above for
474.Fn csio_build_visit .
475.Pp
476.Fn buff_encode_visit
477encodes an arbitrary data pointer, using the method described
478above for
479.Fn csio_build_visit .
480.Sh RETURN VALUES
481.Fn csio_build ,
482.Fn csio_build_visit ,
483.Fn csio_encode ,
484.Fn csio_encode_visit ,
485and
486.Fn buff_encode_visit
487return the number of fields processed.
488.Pp
489.Fn csio_decode ,
490.Fn csio_decode_visit ,
491.Fn buff_decode ,
492and
493.Fn buff_decode_visit
494return the number of assignments performed.
495.Sh SEE ALSO
496.Xr cam 3 ,
497.Xr pass 4 ,
498.Xr camcontrol 8
499.Sh HISTORY
500The CAM versions of these functions are based upon similar functions
501implemented for the old
502.Fx
503.Tn SCSI
504layer.
505The encoding/decoding functions in the old
506.Tn SCSI
507code were written by
508.An Peter Dufault Aq Mt dufault@hda.com .
509.Pp
510Many systems have comparable interfaces to permit a user to construct a
511SCSI command in user space.
512.Pp
513The old
514.Va scsireq
515data structure was almost identical to the SGI /dev/scsi data structure.
516If anyone knows the name of the authors it should go here;
517Peter Dufault
518first read about it in a 1989 Sun Expert magazine.
519.Pp
520The new CCB data structures are derived from the CAM-2 and CAM-3
521specifications.
522.Pp
523.An Peter Dufault
524implemented a clone of SGI's interface in
525.Bx 386
526that
527led to the original
528.Fx
529.Tn SCSI
530library and the related kernel ioctl.
531If anyone needs that for compatibility, contact
532.Mt dufault@hda.com .
533.Sh AUTHORS
534.An -nosplit
535.An Kenneth Merry Aq Mt ken@FreeBSD.org
536implemented the CAM versions of these encoding and decoding functions.
537This current work is based upon earlier work by
538.An Peter Dufault Aq Mt dufault@hda.com .
539.Sh BUGS
540There should probably be a function that encodes both the CDB and the data
541buffer portions of a
542.Tn SCSI
543CCB.
544I discovered this while implementing the arbitrary command execution
545code in
546.Xr camcontrol 8 ,
547but I have not yet had time to implement such a function.
548.Pp
549Some of the CCB flag descriptions really do not belong here.
550Rather they
551belong in a generic CCB man page.
552Since that man page has not yet been
553written, the shorter descriptions here will have to suffice.
554