1 .\" $NetBSD: scsipi.9,v 1.23 2009/05/04 20:10:19 wiz Exp $
4 .\" Copyright (c) 2001 Manuel Bouyer.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 .Nd SCSI/ATAPI middle-layer interface
34 .In dev/scsipi/atapiconf.h
35 .In dev/scsipi/scsiconf.h
37 .Fn scsipi_async_event "struct scsipi_channel *chan" "scsipi_async_event_t event" "void *arg"
39 .Fn scsipi_channel_freeze "struct scsipi_channel *chan" "int count"
41 .Fn scsipi_channel_thaw "struct scsipi_channel *chan" "int count"
43 .Fn scsipi_channel_timed_thaw "void *arg"
45 .Fn scsipi_periph_freeze "struct scsipi_periph *periph" "int count"
47 .Fn scsipi_periph_thaw "struct scsipi_periph *periph" "int count"
49 .Fn scsipi_periph_timed_thaw "void *arg"
51 .Fn scsipi_done "struct scsipi_xfer *xs"
53 .Fn scsipi_printaddr "struct scsipi_periph *periph"
55 .Fn scsipi_target_detach "struct scsipi_channel *chan" "int target" "int lun" "int flags"
57 .Fn scsipi_thread_call_callback "struct scsipi_channel *chan" "void (*callback)(struct scsipi_channel *, void *)" "void *arg"
61 system is the middle layer interface between SCSI/ATAPI host bus
62 adapters (HBA) and high-level SCSI/ATAPI drivers.
63 This document describes the interfaces provided by the
65 layer towards the HBA layer.
66 An HBA has to provide a pointer to a
67 .Va struct scsipi_adapter
68 and one pointer per channel to a
69 .Va struct scsipi_channel .
70 Once the SCSI or ATAPI bus is attached, the
72 system will scan the bus and allocate a
73 .Va struct scsipi_periph
74 for each device found on the bus.
75 A high-level command (command sent from the high-level SCSI/ATAPI
76 layer to the low-level HBA layer) is described by a
77 .Va struct scsipi_xfer .
79 A request is sent to the HBA driver through the
82 The HBA driver signals completion (with or without errors) of the
86 knows the resource's limits of the HBA (max number of concurrent requests per
87 adapter of channel, and per periph), and will make sure the HBA won't receive
88 more requests than it can handle.
90 The mid-layer can also handle
96 An HBA driver has to allocate and initialize to 0 a
97 .Va struct scsipi_adapter
98 and fill in the following members:
99 .Bl -tag -width "struct_device *adapt_dev" -compact -offset indent
100 .It Va struct device *adapt_dev
103 .It Va int adapt_nchannels
104 number of channels (or busses) of the adapter
105 .It Va int adapt_openings
106 total number of commands the adapter can handle (may be replaced by
109 .It Va int adapt_max_periph
110 number of commands the adapter can handle per device
113 The following callbacks should be provided through the
114 .Va struct scsipi_adapter :
115 .Bl -tag -width someverylongword -compact -offset indent
116 .It void Fn (*adapt_request) "struct scsipi_channel *" "scsipi_adapter_req_t" "void *"
118 .It void Fn (*adapt_minphys) "struct buf *"
120 .It int Fn (*adapt_ioctl) "struct scsipi_channel *" "u_long" "void *" "int" "struct lwp *"
122 .It int Fn (*adapt_enable) "struct device *" "int"
126 .It int Fn (*adapt_getgeom) "struct scsipi_periph *" "struct disk_parms *" "u_long"
130 .It int Fn (*adapt_accesschk) "struct scsipi_periph *" "struct scsipi_inquiry_pattern *"
136 The HBA driver has to allocate and initialize to 0 one
137 .Va struct scsipi_channel
138 per channel and fill in the following members:
139 .Bl -tag -width struct_scsipi_adapter -compact -offset indent
140 .It Va struct scsipi_adapter *chan_adapter
142 .Fa struct scsipi_adapter
143 .It Va struct scsipi_bustype *chan_bustype
144 should be initialized to either
151 .It Va int chan_channel
152 channel number (starting at 0)
153 .It Va int chan_flags
155 .Bl -tag -width SCSIPI_CHAN_OPENINGS -compact
156 .It Dv SCSIPI_CHAN_OPENINGS
157 Use per-channel max number of commands
159 instead of per-adapter
161 .It Dv SCSIPI_CHAN_CANGROW
162 This channel can grow its
169 .It Dv SCSIPI_CHAN_NOSETTLE
170 Do not wait SCSI_DELAY seconds for devices to settle before probing (usually
171 used by adapters that provide an \*qabstracted\*q view of the bus).
173 .It Va int chan_openings
174 total number of commands the adapter can handle for this channel (used only
176 .Dv SCSIPI_CHAN_OPENINGS
178 .It Va chan_max_periph
179 number of commands per device the adapter can handle on this
180 channel (used only if the
181 .Va SCSIPI_CHAN_OPENINGS
183 .It Va int chan_ntargets
185 .It Va int chan_nluns
186 number of LUNs per target
188 adapter's ID on this channel
189 .It Va int chan_defquirks
190 default device quirks.
191 Quirks are defined in
192 .Aq Pa dev/scsipi/scsipiconf.h
193 and are usually set in the middle layer based on the device's inquiry
195 For some kinds of adapters it may be convenient to have a set of
196 quirks applied to all devices, regardless of the inquiry data.
199 The HBA driver attaches the SCSI or ATAPI bus (depending on the setting of
201 by passing a pointer to the
202 .Va struct scsipi_channel
206 The print function shall be either
210 .Ss OTHER DATA STRUCTURES
211 When scanning the bus, the
214 .Va struct scsipi_periph
215 for each device probed.
216 The interesting fields are:
217 .Bl -tag -width int_periph_openings -compact -offset indent
218 .It Va struct device *periph_dev
219 pointer to the device's
221 .It Va struct scsipi_channel *periph_channel
222 pointer to the channel the device is connected to
223 .It Va int periph_quirks
224 device quirks, defined in
225 .Aq Pa dev/scsipi/scsipiconf.h
226 .It Va int periph_target
227 target ID, or drive number on ATAPI
228 .It Va int periph_lun
229 LUN (currently not used on ATAPI)
232 A SCSI or ATAPI request is passed to the HBA through a
233 .Va struct scsipi_xfer .
234 The HBA driver has access to the following data:
235 .Bl -tag -width "int xs_callout" -compact -offset indent
236 .It Va struct callout xs_callout
237 callout for adapter use, usually for command timeout
238 .It Va int xs_control
239 control flags (only flags of interest for HBA drivers are described):
240 .Bl -tag -width XS_CTL_DISCOVERY -compact
242 poll in the HBA driver for request completion (most likely because interrupts
246 .It Dv XS_CTL_DATA_UIO
250 .It Dv XS_CTL_DATA_IN
251 data is transferred from HBA to memory
252 .It Dv XS_CTL_DATA_OUT
253 data is transferred from memory to HBA
254 .It Dv XS_CTL_DISCOVERY
255 this xfer is part of a device discovery done by the middle layer
256 .It Dv XS_CTL_REQSENSE
257 xfer is a request sense
262 .Bl -tag -width XS_STS_PRIVATE -compact
266 .It Va XS_STS_PRIVATE
267 mask of flags reserved for HBA's use (0xf0000000)
270 .It Va struct scsipi_periph *xs_periph
271 periph doing the xfer
273 command timeout, in milliseconds.
274 The HBA should start the timeout at the time the command is accepted
276 If the timeout happens, the HBA shall terminate the command through
278 with a XS_TIMEOUT error
279 .It Va struct scsipi_generic *cmd
280 scsipi command to execute
282 len (in bytes) of the cmd buffer
284 data buffer (this is either a DMA or uio address)
286 data length (in bytes, zero if uio)
290 and how much data was really transferred
291 .It Va scsipi_xfer_result_t error
292 error value returned by the HBA driver to mid-layer.
296 .It Va union {struct scsipi_sense_data scsi_sense; uint32_t atapi_sense;} sense
297 where to store sense info if
303 .It Va uint8_t status
304 SCSI status; checked by middle layer when
307 (the middle layer handles
310 .Dv SCSI_QUEUE_FULL )
311 .It Va uint8_t xs_tag_type
312 SCSI tag type, set to 0 if untagged command
313 .It Va uint8_t xs_tag_id
314 tag ID, used for tagged commands
316 .Ss FUNCTIONS AND CALLBACKS
317 .Bl -tag -width xxxxxxxx -compact
318 .It Fn (*adapt_request) "struct scsipi_channel *chan" "scsipi_adapter_req_t req" "void *arg"
319 Used by the mid-layer to transmit a request to the adapter.
322 .Bl -tag -width xxxxxxxx -compact
323 .It Dv ADAPTER_REQ_RUN_XFER
324 request the adapter to send a command to the device.
327 .Va struct scsipi_xfer .
328 Once the xfer is complete the HBA driver shall call
330 with updated status and error information.
331 .It Dv ADAPTER_REQ_GROW_RESOURCES
332 ask the adapter to increase resources of the channel (grow
337 Support of this feature is optional.
338 This request is called from the kernel completion thread.
341 .It Dv ADAPTER_REQ_SET_XFER_MODE
342 set the xfer mode for a for I_T Nexus.
343 This will be called once all LUNs of a target have been probed.
346 .Va struct scsipi_xfer_mode
348 .Bl -tag -width int_xm_target -compact
352 bitmask of device capabilities
363 .Dv ADAPTER_REQ_SET_XFER_MODE .
365 holds the following bits:
366 .Bl -tag -width xxxxxxxx -compact
367 .It Dv PERIPH_CAP_SYNC
368 ST synchronous transfers
369 .It Dv PERIPH_CAP_WIDE16
370 ST 16 bit wide transfers
371 .It Dv PERIPH_CAP_WIDE32
372 ST 32 bit wide transfers
375 .It Dv PERIPH_CAP_TQING
378 Whenever the xfer mode changes, the driver should call
379 .Fn scsipi_async_event
380 to notify the mid-layer.
384 may be called from interrupt context.
386 pointer to the driver's minphys function.
387 If the driver can handle transfers of size
392 ioctl function for the channel.
393 The only ioctl supported at this level is
395 for which the HBA driver shall issue a SCSI reset on the channel.
396 .It int Fn adapt_enable "struct device *dev" "int enable"
397 Disable the adapter if
399 is zero, or enable it if non-zero.
400 Returns 0 if operation is successful, or error from
401 .Pa \*[Lt]sys/errno.h\*[Gt] .
402 This callback is optional, and is useful mostly for hot-plug devices.
403 For example, this callback would power on or off
404 the relevant PCMCIA socket for a PCMCIA controller.
405 .It int Fn adapt_getgeom "struct scsipi_periph *periph" "struct disk_parms *params" "u_long sectors"
406 Optional callback, used by high-level drivers to get the fictitious geometry
407 used by the controller's firmware for the specified periph.
408 Returns 0 if successful.
409 See Adaptec drivers for details.
410 .It int Fn adapt_accesschk "struct scsipi_periph *periph" "struct scsipi_inquiry_pattern *inqbuf"
411 Optional callback; if present the mid-layer uses it to check if it can
412 attach a driver to the specified periph.
413 If the callback returns a non-zero value, the periph is ignored by the
416 This callback is used by adapters which want to drive some devices
417 themselves, for example hardware RAID controllers.
418 .It Fn scsipi_async_event "struct scsipi_channel *chan" "scsipi_async_event_t event" "void *arg"
419 Asynchronous event notification for the mid-layer.
422 .Bl -tag -width xxxxxxxx -compact
423 .It Dv ASYNC_EVENT_MAX_OPENINGS
424 set max openings for a periph.
426 .Va struct scsipi_max_openings
427 with at least the following members:
428 .Bl -tag -width xxxxxxxx -compact
431 .It Va int mo_openings
434 Not all periphs may allow openings to increase; if not allowed the request is
436 .It Dv ASYNC_EVENT_XFER_MODE
437 update the xfer mode for an I_T nexus.
439 .Va struct scsipi_xfer_mode
442 .Dv ASYNC_EVENT_XFER_MODE
447 is mandatory to activate tagged queuing.
448 .It Dv ASYNC_EVENT_RESET
449 channel has been reset.
451 HBA drivers have to issue
452 .Dv ASYNC_EVENT_RESET events if they rely on the
453 mid-layer for SCSI CHECK CONDITION handling.
456 .It Fn scsipi_done "struct scsipi_xfer *xs"
457 shall be called by the HBA when the xfer is complete, or when it needs to
458 be requeued by the mid-layer.
460 in the scsipi_xfer shall be set to one of the following:
461 .Bl -tag -width xxxxxxxx -compact
463 xfer completed without error.
465 Check the returned SCSI sense for the error.
467 Check the ATAPI sense for the error.
468 .It Dv XS_DRIVER_STUFFUP
469 Driver failed to perform operation.
470 .It Dv XS_RESOURCE_SHORTAGE
471 Adapter resource shortage.
472 The mid-layer will retry the command after some delay.
474 The device timed out while trying to send the command
476 The command was accepted by the device, but it didn't complete in allowed time.
478 The mid-layer will check
480 for additional details:
481 .Bl -tag -width SCSI_QUEUE_FULL -compact
483 SCSI check condition.
484 The mid-layer will freeze the periph queue and issue a REQUEST SENSE command.
485 If the HBA supports tagged queuing, it shall remove and requeue any command
486 not yet accepted by the HBA (or at last make sure no more commands will
487 be sent to the device before the REQUEST SENSE is complete).
488 .It Dv SCSI_QUEUE_FULL
489 The mid layer will adjust the periph's openings and requeue the command.
491 The mid-layer will requeue the xfer after delay.
494 xfer destroyed by a reset; the mid-layer will requeue it.
496 Ask the mid-layer to requeue this command immediately.
499 The adapter should not reference an
502 .Fn scsipi_done "xfer"
503 has been called, unless the
512 callback again only if called with
521 All other error conditions are handled by a kernel thread
522 (once the HBA's interrupt handler has returned).
523 .It Fn scsipi_printaddr "struct scsipi_periph *periph"
524 print a kernel message with the periph's name, in the form
525 device(controller:channel:target:lun).
526 .It Fn scsipi_channel_freeze "struct scsipi_channel *chan" "int count"
527 Freeze the specified channel (requests are queued but not sent to HBA).
528 The channel's freeze counter is increased by
530 .It Fn scsipi_channel_thaw "struct scsipi_channel *chan" "int count"
531 Decrement the channel's freeze counter by
533 and process the queue if the counter goes to 0.
534 In order to preserve command ordering, HBA drivers should not call
535 .Fn scsipi_channel_thaw
538 for all commands in the HBA's queue which need to be requeued.
539 .It Fn scsipi_periph_timed_thaw "void *arg"
541 .Fn scsipi_channel_thaw "arg" "1" .
542 Intended to be used as
545 .It Fn scsipi_periph_freeze "struct scsipi_periph *periph" "int count"
546 .It Fn scsipi_periph_thaw "struct scsipi_periph *periph"
547 .It Fn scsipi_periph_timed_thaw "void *arg"
548 Same as the channel counterparts, but only for one specific peripheral.
549 .It Fn scsipi_target_detach "struct scsipi_channel *chan" "int target" "int lun" "int flags"
550 detach the periph associated with this I_T_L nexus.
555 may be wildcarded using the magic value -1.
560 Returns 0 if successful, or error code if a device couldn't be removed.
561 .It Fn scsipi_thread_call_callback "struct scsipi_channel *chan" "void (*callback)(struct scsipi_channel *, void *)" "void *arg"
567 as arguments, from the channel completion thread.
568 The callback is run at splbio.
569 .Fn scsipi_thread_call_callback
570 will freeze the channel by one, it's up to the caller to thaw it when
572 Returns 0 if the callback was properly recorded, or EBUSY if the
573 channel has already a callback pending.
576 .Bl -tag -width sys/dev/atapiconf.h
577 .It Pa sys/dev/scsiconf.h
578 header file for use by SCSI HBA drivers
579 .It Pa sys/dev/atapiconf.h
580 header file for use by ATAPI HBA drivers
583 Both header files include
584 .Pa sys/dev/scsipiconf.h
585 which contains most structure definitions, function prototypes and macros.
587 The best examples are existing HBA drivers.
588 Most of them sit in the
594 interface appeared in
599 interface was designed and implemented by Jason R. Thorpe.
600 Manuel Bouyer converted most drivers to the new interface.