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