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