Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
11 KiB

  1. .\" $OpenBSD: scsi.3,v 1.8 2003/03/06 20:13:15 jmc Exp $
  2. .\" Copyright (c) 1994 HD Associates (hd@world.std.com)
  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. All advertising materials mentioning features or use of this software
  14. .\" must display the following acknowledgement:
  15. .\" This product includes software developed by HD Associates
  16. .\" 4. Neither the name of the HD Associates nor the names of its contributors
  17. .\" may be used to endorse or promote products derived from this software
  18. .\" without specific prior written permission.
  19. .\"
  20. .\" THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES``AS IS'' AND
  21. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. .\" ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
  24. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. .\" SUCH DAMAGE.
  31. .\"
  32. .\"
  33. .Dd November 20, 1994
  34. .Dt SCSI 3
  35. .Os
  36. .Sh NAME
  37. .Nm scsireq_buff_decode ,
  38. .Nm scsireq_build ,
  39. .Nm scsireq_decode ,
  40. .Nm scsireq_encode ,
  41. .Nm scsireq_enter ,
  42. .Nm scsireq_new ,
  43. .Nm scsireq_reset ,
  44. .Nm SCSIREQ_ERROR ,
  45. .Nm scsi_open ,
  46. .Nm scsi_debug ,
  47. .Nm scsi_debug_output
  48. .Nd SCSI User library
  49. .Sh SYNOPSIS
  50. .Fd #include <sys/types.h>
  51. .Fd #include <sys/scsiio.h>
  52. .Fd #include <scsi.h>
  53. .Ft int
  54. .Fn scsireq_buff_decode "u_char *ptr, size_t len, char *fmt, ..."
  55. .Ft struct scsireq *
  56. .Fn scsireq_build "struct scsireq *, u_long len, caddr_t buf, u_long flags, char *fmt, ..."
  57. .Ft int
  58. .Fn scsireq_decode "struct scsireq *, char *fmt, ..."
  59. .Ft int
  60. .Fn scsireq_encode "struct scsireq *, char *fmt, ..."
  61. .Ft int
  62. .Fn scsireq_enter "int fid, struct scsireq *s"
  63. .Ft struct scsireq *
  64. .Fn scsireq_new void
  65. .Ft struct scsireq *
  66. .Fn scsireq_reset "struct scsireq *"
  67. .Ft int
  68. .Fn SCSIREQ_ERROR "struct scsireq *"
  69. .Ft int
  70. .Fn scsi_open "const char *path, int flags"
  71. .Ft void
  72. .Fn scsi_debug "FILE *f, int ret, struct scsireq *s"
  73. .Ft FILE *
  74. .Fn scsi_debug_output "char *s"
  75. .Sh DESCRIPTION
  76. These functions
  77. use the SCIOCCOMMAND
  78. .Xr ioctl 2
  79. of the SCSI subsystem
  80. to provide user level access to SCSI commands.
  81. The programmer must know the SCSI CDB (Command Descriptor
  82. Block) to perform the desired command.
  83. These functions assist in
  84. building up the CDB, submitting it to the SCSI subsystem, and decoding
  85. the result.
  86. .Pp
  87. Look at the
  88. .Xr scsi 8
  89. command before using the library directly - simple programs are
  90. best implemented as scripts using that facility.
  91. .Pp
  92. To provide for security,
  93. not all devices accept the SCIOCCOMAND ioctl.
  94. It is accepted by the
  95. control device for tape drives, partition D for disk drives, partition C
  96. for CD ROM drives, and any "unknown" device.
  97. .\" The "super scsi"
  98. .\" .Xr ssc 4
  99. .\" device also accepts the ioctl.
  100. .Pp
  101. Most of the SCSI library functions build up and manipulate the
  102. .Ar scsireq
  103. structure found in the include file
  104. .Aq Pa sys/scsiio.h :
  105. .Bd -literal -offset indent
  106. #define SENSEBUFLEN 48
  107. .Pp
  108. typedef struct scsireq {
  109. u_long flags; /* info about the request status and type */
  110. u_long timeout;
  111. u_char cmd[16]; /* 12 is actually the max */
  112. u_char cmdlen;
  113. caddr_t databuf; /* address in user space of buffer */
  114. u_long datalen; /* size of user buffer (request) */
  115. u_long datalen_used; /* size of user buffer (used)*/
  116. u_char sense[SENSEBUFLEN]; /* returned sense will be in here */
  117. u_char senselen; /* sensedata request size (MAX of SENSEBUFLEN)*/
  118. u_char senselen_used; /* return value only */
  119. u_char status; /* what the scsi status was from the adapter */
  120. u_char retsts; /* the return status for the command */
  121. int error; /* error bits */
  122. } scsireq_t;
  123. .Ed
  124. .Pp
  125. .Fn scsireq_new
  126. allocates a new
  127. .Ar scsireq
  128. structure and returns a pointer to it or NULL if it can't allocate
  129. memory.
  130. .Pp
  131. .Fn scsireq_reset
  132. resets the structure to reasonable values and returns the same pointer passed
  133. in to it.
  134. It gracefully handles the NULL pointer passed in to it so that you can
  135. unconditionally use
  136. .Ar scsireq_new .
  137. .Pp
  138. .Fn scsireq_build
  139. builds up a scsireq structure based on the information provided in
  140. the variable argument list.
  141. It gracefully handles a NULL pointer passed to it.
  142. .Pp
  143. .Fr len
  144. is the length of the data phase; the data transfer direction is
  145. determined by the
  146. .Ar flags
  147. argument.
  148. .Pp
  149. .Fr buf
  150. is the data buffer used during the SCSI data phase.
  151. If it is NULL it is allocated via malloc and
  152. .Ar scsireq->databuf
  153. is set to point to the newly allocated memory.
  154. .Pp
  155. .Fr flags
  156. are the flags defined in
  157. .Aq Pa sys/scsiio.h :
  158. .Bd -literal -offset indent
  159. /* bit definitions for flags */
  160. #define SCCMD_READ 0x00000001
  161. #define SCCMD_WRITE 0x00000002
  162. #define SCCMD_IOV 0x00000004
  163. #define SCCMD_ESCAPE 0x00000010
  164. #define SCCMD_TARGET 0x00000020
  165. .Ed
  166. Only two of these flags are supported in this release of the software:
  167. .Fr SCCMD_READ
  168. indicates a data in phase (a transfer into the user buffer at
  169. .Ar scsireg->databuf ) ,
  170. and
  171. .Fr SCCMD_WRITE
  172. indicates a data out phase (a transfer out of the user buffer).
  173. .Pp
  174. .Fr fmt
  175. is a CDB format specifier used to build up the SCSI CDB.
  176. This text string is made up of a list of field specifiers.
  177. Field specifiers specify the value for each CDB field (including indicating
  178. that the value be taken from the next argument in the
  179. variable argument list), the width
  180. of the field in bits or bytes, and an optional name.
  181. White space is ignored, and the pound sign ('#') introduces a comment that
  182. ends at the end of the current line.
  183. .Pp
  184. The optional name is the first part of a field specifier and
  185. is in curly braces.
  186. The text in curly braces in this example are the names:
  187. .Bd -literal -offset indent
  188. .Fr "{PS} v:b1 {Reserved} 0:b1 {Page Code} v:b6 # Mode select page"
  189. .Ed
  190. .Pp
  191. This field specifier has two one bit fields and one six bit field.
  192. The second one bit field is the constant value 0 and the first
  193. one bit field and the six bit field are taken from the variable
  194. argument list.
  195. Multi byte fields are swapped into the SCSI byte order in the
  196. CDB and whitespace is ignored.
  197. .Pp
  198. When the field is a hex value or the letter v, (e.g.,
  199. .Fr "1A"
  200. or
  201. .Fr "v" )
  202. then a single byte value
  203. is copied to the next unused byte of the CDB.
  204. When the letter
  205. .Fr v
  206. is used the next integer argument is taken from the variable argument list
  207. and that value used.
  208. .Pp
  209. A constant hex value followed by a field width specifier or the letter
  210. .Fr v
  211. followed by a field width specifier (e.g.,
  212. .Fr 3:4 ,
  213. .Fr 3:b4 ,
  214. .Fr 3:i3 ,
  215. .FR v:i3 )
  216. specifies a field of a given bit or byte width.
  217. Either the constant value or (for the V specifier) the next integer value from
  218. the variable argument list is copied to the next unused
  219. bits or bytes of the CDB.
  220. .Pp
  221. A decimal number or the letter
  222. .Fr b
  223. followed by a decimal number field width indicates a bit field of that width.
  224. The bit fields are packed as tightly as possible beginning with the
  225. high bit (so that it reads the same as the SCSI spec), and a new byte of
  226. the CDB is started whenever a byte fills completely or when an
  227. .Fr i
  228. field is encountered.
  229. .Pp
  230. A field width specifier consisting of the letter
  231. .Fr i
  232. followed by either
  233. 1, 2, 3 or 4 indicates a 1, 2, 3 or 4 byte integral value that must
  234. be swapped into SCSI byte order (MSB first).
  235. .Pp
  236. For the
  237. .Fr v
  238. field specifier the next integer argument is taken from the variable argument
  239. list and that value is used swapped into SCSI byte order.
  240. .Pp
  241. .Fn scsireq_decode
  242. is used to decode information from the data in phase of the SCSI
  243. transfer.
  244. .Pp
  245. The decoding is similar to
  246. the command specifier processing of
  247. .Fn scsireq_build
  248. except that the data is extracted from the data pointed to by
  249. .Fr scsireq->databuf .
  250. The stdarg list should be pointers to integers instead of integer
  251. values.
  252. A seek field type and a suppression modifier are added.
  253. The
  254. .Fr *
  255. suppression modifier (e.g.,
  256. .Fr *i3
  257. or
  258. .Fr *b4 )
  259. suppresses assignment from the field and can be used to skip
  260. over bytes or bits in the data, without having to copy
  261. them to a dummy variable in the arg list.
  262. .Pp
  263. The seek field type
  264. .Fr s
  265. permits you to skip over data.
  266. This seeks to an absolute position (
  267. .Fr s3 )
  268. or a relative position (
  269. .Fr s+3 )
  270. in the data, based on whether or not the presence of the '+' sign.
  271. The seek value can be specified as
  272. .Fr v
  273. and the next integer value from the argument list will be
  274. used as the seek value.
  275. .Pp
  276. .Fn scsireq_buff_decode
  277. decodes an arbitrary data buffer using the method
  278. described above in
  279. .Fn scsireq_decode .
  280. .Pp
  281. .Fn scsireq_encode
  282. encodes the data phase section of the structure.
  283. The encoding is handled identically as the encoding of the CDB structure by
  284. .Fn scsireq_build
  285. .Pp
  286. .Fn scsireq_enter
  287. submits the built up structure for processing using
  288. the SCIOCCOMMAND ioctl.
  289. .Pp
  290. .Fn SCSIREQ_ERROR
  291. is a macro that determines if the result of the SCIOCCOMMAND ioctl may
  292. have been
  293. in error by examining the host adapter return code, whether sense was sent
  294. or not, and so on.
  295. .Pp
  296. .Fn scsi_open
  297. checks environment variables and initializes the library for
  298. consistent library use and then calls the regular open system call.
  299. .Pp
  300. .Fn scsi_debug
  301. prints the results of a scsireq_enter function to the specified stdio
  302. stream.
  303. .Pp
  304. .Fn scsi_debug_output
  305. requests that the results of all transactions be debugged to the
  306. supplied file using
  307. .Fn scsi_debug .
  308. .Sh RETURN VALUES
  309. The function
  310. .Fn scsireq_new
  311. returns a pointer to storage allocated from malloc, and therefore
  312. potentially a NULL.
  313. .Pp
  314. The functions
  315. .Fn scsireq_build
  316. and
  317. .Fn scsireq_reset
  318. return the same pointer as the one passed in.
  319. .Pp
  320. The functions
  321. .Fn scsireq_buff_decode and
  322. .Fn scsireq_decode
  323. return the number of assignments performed.
  324. .Pp
  325. .Fn scsireq_encode
  326. returns the number of fields processed.
  327. .Pp
  328. The function
  329. .Fn scsireq_enter
  330. returns the result of the ioctl call.
  331. .Sh SEE ALSO
  332. .Xr scsi 4 ,
  333. .Xr scsi 8
  334. .Sh BUGS
  335. This only works completely for the 1542C.
  336. The host adapter code
  337. that sets up the residual amount of data transfer has to be added
  338. to each individual adapter.
  339. This library is usable on the other
  340. host adapters; however, the SCSI driver pretends that the proper
  341. amount of data is always transferred.
  342. If you have an Adaptec 174x
  343. and can hack contact dufault@hda.com and you can have the code to
  344. calculate residual data for the 174x series to integrate and test.
  345. .Sh HISTORY
  346. Many systems have comparable interfaces to permit a user to construct a
  347. SCSI command in user space.
  348. .Pp
  349. The data structure is almost identical to the SGI /dev/scsi data
  350. structure.
  351. If anyone knows the name of the authors it should
  352. go here; Peter Dufault first read about it in a 1989 Sun Expert magazine.
  353. .Pp
  354. Peter Dufault implemented a clone of SGI's interface in 386bsd that
  355. led to this library and the related kernel ioctl.
  356. If anyone needs that for compatibility contact dufault@hda.com.