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