xref: /freebsd/lib/libcam/cam_cdbparse.3 (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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 October 13, 1998
65.Os FreeBSD 3.0
66.Dt CAM_CDBPARSE 3
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 SYNOPSIS
79.Fd #include <stdio.h>
80.Fd #include <camlib.h>
81.Ft int
82.Fo csio_build
83.Fa "struct ccb_scsiio *csio"
84.Fa "u_int8_t *data_ptr"
85.Fa "u_int32_t dxfer_len"
86.Fa "u_int32_t flags"
87.Fa "int retry_count"
88.Fa "int timeout"
89.Fa "char *cmd_spec"
90.Fa "..."
91.Fc
92.Ft int
93.Fo csio_build_visit
94.Fa "struct ccb_scsiio *csio"
95.Fa "u_int8_t *data_ptr"
96.Fa "u_int32_t dxfer_len"
97.Fa "u_int32_t flags"
98.Fa "int retry_count"
99.Fa "int timeout"
100.Fa "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 "char *fmt"
108.Fa "..."
109.Fc
110.Ft int
111.Fo csio_decode_visit
112.Fa "struct ccb_scsiio *csio"
113.Fa "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 "u_int8_t *buff"
124.Fa "size_t len"
125.Fa "char *fmt"
126.Fa "..."
127.Fc
128.Ft int
129.Fo buff_decode_visit
130.Fa "u_int8_t *buff"
131.Fa "size_t len"
132.Fa "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 "char *fmt"
140.Fa "..."
141.Fc
142.Ft int
143.Fo csio_encode_visit
144.Fa "struct ccb_scsiio *csio"
145.Fa "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 "u_int8_t *buff"
152.Fa "size_t len"
153.Fa "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 FreeBSD
164.Tn SCSI
165layer.
166.Pp
167These functions may be used in new applications, but users may find it
168easier to use the various SCSI CCB building functions included with the
169.Xr cam 3
170library.  (e.g.
171.Fn cam_fill_csio ,
172.Fn scsi_start_stop ,
173and
174.Fn scsi_read_write )
175.Pp
176.Fn csio_build
177builds up a
178.Va ccb_scsiio
179structure based on the information provided in
180the variable argument list.
181It gracefully handles a NULL
182.Fa data_ptr
183argument passed to it.
184.Pp
185.Fa dxfer_len
186is the length of the data phase; the data transfer direction is
187determined by the
188.Fa flags
189argument.
190.Pp
191.Fa data_ptr
192is the data buffer used during the
193.Tn SCSI
194data phase.  If no data is to be
195transferred for the
196.Tn SCSI
197command in question, this should be set to NULL.  If there is data to
198transfer for the command, this buffer must be at least
199.Fa dxfer_len
200long.
201.Pp
202.Fa flags
203are the flags defined in
204.Aq Pa cam/cam_ccb.h :
205.Bd -literal
206/* Common CCB header */
207/* CAM CCB flags */
208typedef enum {
209     CAM_CDB_POINTER       = 0x00000001,/* The CDB field is a pointer    */
210     CAM_QUEUE_ENABLE      = 0x00000002,/* SIM queue actions are enabled */
211     CAM_CDB_LINKED        = 0x00000004,/* CCB contains a linked CDB     */
212     CAM_SCATTER_VALID     = 0x00000010,/* Scatter/gather list is valid  */
213     CAM_DIS_AUTOSENSE     = 0x00000020,/* Disable autosense feature     */
214     CAM_DIR_RESV          = 0x00000000,/* Data direction (00:reserved)  */
215     CAM_DIR_IN            = 0x00000040,/* Data direction (01:DATA IN)   */
216     CAM_DIR_OUT           = 0x00000080,/* Data direction (10:DATA OUT)  */
217     CAM_DIR_NONE          = 0x000000C0,/* Data direction (11:no data)   */
218     CAM_DIR_MASK          = 0x000000C0,/* Data direction Mask		 */
219     CAM_SOFT_RST_OP       = 0x00000100,/* Use Soft reset alternative    */
220     CAM_ENG_SYNC          = 0x00000200,/* Flush resid bytes on complete */
221     CAM_DEV_QFRZDIS       = 0x00000400,/* Disable DEV Q freezing	 */
222     CAM_DEV_QFREEZE       = 0x00000800,/* Freeze DEV Q on execution     */
223     CAM_HIGH_POWER        = 0x00001000,/* Command takes a lot of power  */
224     CAM_SENSE_PTR         = 0x00002000,/* Sense data is a pointer	 */
225     CAM_SENSE_PHYS        = 0x00004000,/* Sense pointer is physical addr*/
226     CAM_TAG_ACTION_VALID  = 0x00008000,/* Use the tag action in this ccb*/
227     CAM_PASS_ERR_RECOVER  = 0x00010000,/* Pass driver does err. recovery*/
228     CAM_DIS_DISCONNECT    = 0x00020000,/* Disable disconnect		 */
229     CAM_SG_LIST_PHYS      = 0x00040000,/* SG list has physical addrs.   */
230     CAM_MSG_BUF_PHYS      = 0x00080000,/* Message buffer ptr is physical*/
231     CAM_SNS_BUF_PHYS      = 0x00100000,/* Autosense data ptr is physical*/
232     CAM_DATA_PHYS         = 0x00200000,/* SG/Buffer data ptrs are phys. */
233     CAM_CDB_PHYS          = 0x00400000,/* CDB poiner is physical	 */
234     CAM_ENG_SGLIST        = 0x00800000,/* SG list is for the HBA engine */
235
236/* Phase cognizant mode flags */
237     CAM_DIS_AUTOSRP       = 0x01000000,/* Diable autosave/restore ptrs	*/
238     CAM_DIS_AUTODISC      = 0x02000000,/* Disable auto disconnect	*/
239     CAM_TGT_CCB_AVAIL     = 0x04000000,/* Target CCB available		*/
240     CAM_TGT_PHASE_MODE    = 0x08000000,/* The SIM runs in phase mode	*/
241     CAM_MSGB_VALID        = 0x20000000,/* Message buffer valid		*/
242     CAM_STATUS_VALID      = 0x40000000,/* Status buffer valid		*/
243     CAM_DATAB_VALID       = 0x80000000,/* Data buffer valid		*/
244
245/* Host target Mode flags */
246     CAM_TERM_IO           = 0x20000000,/* Terminate I/O Message sup.    */
247     CAM_DISCONNECT        = 0x40000000,/* Disconnects are mandatory     */
248     CAM_SEND_STATUS       = 0x80000000,/* Send status after data phase  */
249} ccb_flags;
250.Ed
251.Pp
252Multiple flags should be ORed together.  Any of the CCB flags may be used,
253although it is worth noting several important ones here:
254.Pp
255.Bl -tag -width CAM_PASS_ERR_RECOVER
256.It Dv CAM_DIR_IN
257This indicates that the operation in question is a read operation.  i.e.,
258data is being read from the
259.Tn SCSI
260device to the user-supplied buffer.
261.It Dv CAM_DIR_OUT
262This indicates that the operation is a write operation.  i.e. data is being
263written from the user-supplied buffer to the device.
264.It Dv CAM_DIR_NONE
265This indicates that there is no data to be transferred for this command.
266.It Dv CAM_DEV_QFRZDIS
267This flag disables device queue freezing as an error recovery mechanism.
268.It Dv CAM_PASS_ERR_RECOVER
269This flag tells the
270.Xr pass 4
271driver to enable error recovery.  The default is to not perform error
272recovery, which means that the retry count won't be honored without this
273flag, among other things.
274.It Dv CAM_DATA_PHYS
275This indicates that the address contained in
276.Fa data_ptr
277is a physical address, not a virtual address.
278.El
279.Pp
280The
281.Fa retry_count
282tells the kernel how many times to retry the command in question.  The
283retry count is ignored unless the
284.Xr pass 4
285driver is told to enable error recovery via the
286.Dv CAM_PASS_ERR_RECOVER
287flag.
288.Pp
289The
290.Fa timeout
291tells the kernel how long to wait for the given command to complete.  If
292the timeout expires and the command hasn't completed, the CCB will be
293returned from the kernel with an appropriate error status.
294.Pp
295.Fa cmd_spec
296is a CDB format specifier used to build up the SCSI CDB.
297This text string is made up of a list of field specifiers.  Field
298specifiers specify the value for each CDB field (including indicating
299that the value be taken from the next argument in the
300variable argument list), the width
301of the field in bits or bytes, and an optional name.  White space is
302ignored, and the pound sign ('#') introduces a comment that ends at the
303end of the current line.
304.Pp
305The optional name is the first part of a field specifier and
306is in curly braces.  The text in curly braces in this example are
307the names:
308.Bd -literal -offset indent
309.Fa "{PS} v:b1 {Reserved} 0:b1 {Page Code} v:b6 # Mode select page"
310.Ed
311.Pp
312This field specifier has two one bit fields and one six bit field.
313The second one bit field is the constant value 0 and the first
314one bit field and the six bit field are taken from the variable
315argument list.
316Multi byte fields are swapped into the SCSI byte order in the
317CDB and white space is ignored.
318.Pp
319When the field is a hex value or the letter v, (e.g.,
320.Fa "1A"
321or
322.Fa "v" )
323then a single byte value
324is copied to the next unused byte of the CDB.
325When the letter
326.Fa v
327is used the next integer argument is taken from the variable argument list
328and that value used.
329.Pp
330A constant hex value followed by a field width specifier or the letter
331.Fa v
332followed by a field width specifier (e.g.,
333.Fa 3:4 ,
334.Fa 3:b4 ,
335.Fa 3:i3 ,
336.FR v:i3 )
337specifies a field of a given bit or byte width.
338Either the constant value or (for the V specifier) the next integer value from
339the variable argument list is copied to the next unused
340bits or bytes of the CDB.
341.Pp
342A decimal number or the letter
343.Fa b
344followed by a decimal number field width indicates a bit field of that width.
345The bit fields are packed as tightly as possible beginning with the
346high bit (so that it reads the same as the SCSI spec), and a new byte of
347the CDB is started whenever a byte fills completely or when an
348.Fa i
349field is encountered.
350.Pp
351A field width specifier consisting of the letter
352.Fa i
353followed by either
3541, 2, 3 or 4 indicates a 1, 2, 3 or 4 byte integral value that must
355be swapped into SCSI byte order (MSB first).
356.Pp
357For the
358.Fa v
359field specifier the next integer argument is taken from the variable argument
360list and that value is used swapped into SCSI byte order.
361.Pp
362.Fn csio_build_visit
363operates similarly to
364.Fn csio_build ,
365except that the values to substitute for variable arguments in
366.Fa cmd_spec
367are retrieved via the
368.Fn arg_get
369function passed in to
370.Fn csio_build_visit
371instead of via
372.Xr stdarg 3 .
373The
374.Fn arg_get
375function takes two arguments:
376.Bl -tag -width field_name
377.It Fa gethook
378is passed into the
379.Fn arg_get
380function at each invocation.  This enables the
381.Fn arg_get
382function to keep some state in between calls without using global or static
383variables.
384.It Fa field_name
385is the field name supplied in
386.Fa fmt ,
387if any.
388.El
389.Pp
390.Fn csio_decode
391is used to decode information from the data in phase of the SCSI
392transfer.
393.Pp
394The decoding is similar to
395the command specifier processing of
396.Fn csio_build
397except that the data is extracted from the data pointed to by
398.Fa csio->data_ptr .
399The stdarg list should be pointers to integers instead of integer
400values.
401A seek field type and a suppression modifier are added.
402The
403.Fa *
404suppression modifier (e.g.,
405.Fa *i3
406or
407.Fa *b4 )
408suppresses assignment from the field and can be used to skip
409over bytes or bits in the data, without having to copy
410them to a dummy variable in the arg list.
411.Pp
412The seek field type
413.Fa s
414permits you to skip over data.
415This seeks to an absolute position (
416.Fa s3 )
417or a relative position (
418.Fa s+3 )
419in the data, based on whether or not the presence of the '+' sign.
420The seek value can be specified as
421.Fa v
422and the next integer value from the argument list will be
423used as the seek value.
424.Pp
425.Fn csio_decode_visit
426operates like
427.Fn csio_decode
428except that instead of placing the decoded contents of the buffer in
429varardic arguments, the decoded buffer contents are returned to the user
430via the
431.Fn arg_put
432function that is passed in.
433The
434.Fn arg_put
435function takes several arguments:
436.Bl -tag -width letter
437.It Fa hook
438The "hook" is a mechanism to allow the
439.Fn arg_put
440function to save state in between calls.
441.It Fa letter
442is the letter describing the format of the argument being passed into the
443function.
444.It Fa val
445is a void pointer to the value being passed into the function.
446.It Fa count
447is the number of arguments being passed into the
448.Fn arg_put
449function.  At present this will only be set to 1.
450.It Fa name
451This is a text description of the field, if one was provided in the
452.Fa fmt .
453.El
454.Pp
455.Fn buff_decode
456decodes an arbitrary data buffer using the method
457described above for
458.Fn csio_decode .
459.Pp
460.Fn buff_decode_visit
461decodes an arbitrary data buffer using the method described above for
462.Fn csio_decode_visit .
463.Pp
464.Fn csio_encode
465encodes the
466.Fa data_ptr
467portion (not the CDB!) of a
468.Va ccb_scsiio
469structure, using the method described above for
470.Fn csio_build .
471.Pp
472.Fn csio_encode_visit
473encodes the
474.Fa data_ptr
475portion (not the CDB!) of a
476.Va ccb_scsiio
477structure, using the method described above for
478.Fn csio_build_visit .
479.Pp
480.Fn buff_encode_visit
481encodes an arbitrary data pointer, using the method described
482above for
483.Fn csio_build_visit .
484.Sh RETURN VALUES
485.Fn csio_build ,
486.Fn csio_build_visit ,
487.Fn csio_encode ,
488.Fn csio_encode_visit ,
489and
490.Fn buff_encode_visit
491return the number of fields processed.
492.Pp
493.Fn csio_decode ,
494.Fn csio_decode_visit ,
495.Fn buff_decode ,
496and
497.Fn buff_decode_visit
498return the number of assignments performed.
499.Sh SEE ALSO
500.Xr cam 3 ,
501.Xr pass 4 ,
502.Xr camcontrol 8
503.Sh HISTORY
504.Pp
505The CAM versions of these functions are based upon similar functions
506implemented for the old FreeBSD
507.Tn SCSI
508layer.  The encoding/decoding functions in the old
509.Tn SCSI
510code were written by Peter Dufault.
511.Pp
512Many systems have comparable interfaces to permit a user to construct a
513SCSI command in user space.
514.Pp
515The old
516.Va scsireq
517data structure was almost identical to the SGI /dev/scsi data
518structure.  If anyone knows the name of the authors it should
519go here; Peter Dufault first read about it in a 1989 Sun Expert magazine.
520.Pp
521The new CCB data structures are derived from the CAM-2 and CAM-3
522specifications.
523.Pp
524Peter Dufault implemented a clone of SGI's interface in 386bsd that
525led to the original FreeBSD
526.Tn SCSI
527library and the related kernel ioctl.
528If anyone needs that for compatibility contact dufault@hda.com.
529.Sh AUTHORS
530Kenneth Merry implemented the CAM versions of these encoding and decoding
531functions.  This current work is based upon earlier work by Peter Dufault.
532.Sh BUGS
533There should probably be a function that encodes both the CDB and the data
534buffer portions of a
535.Tn SCSI
536CCB.  I discovered this while implementing the arbitrary command execution
537code in
538.Xr camcontrol 8 ,
539but I haven't yet had time to implement such a function.
540.Pp
541Some of the CCB flag descriptions really don't belong here.  Rather they
542belong in a generic CCB man page.  Since that man page hasn't yet been
543written, the shorter descriptions here will have to suffice.
544