xref: /netbsd-src/share/man/man9/scsipi.9 (revision 04a38fa8a3ca8f295ef0fe5aaa4122a5b12e66c7)
1.\"	$NetBSD: scsipi.9,v 1.33 2024/10/29 15:45:58 nat Exp $
2.\"
3.\"
4.\" Copyright (c) 2001 Manuel Bouyer.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
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.
14.\"
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.
25.\"
26.\"
27.Dd November 20, 2016
28.Dt SCSIPI 9
29.Os
30.Sh NAME
31.Nm scsipi
32.Nd SCSI/ATAPI middle-layer interface
33.Sh SYNOPSIS
34.In dev/scsipi/atapiconf.h
35.In dev/scsipi/scsiconf.h
36.Ft void
37.Fn scsipi_async_event "struct scsipi_channel *chan" "scsipi_async_event_t event" "void *arg"
38.Ft void
39.Fn scsipi_channel_freeze "struct scsipi_channel *chan" "int count"
40.Ft void
41.Fn scsipi_channel_thaw "struct scsipi_channel *chan" "int count"
42.Ft void
43.Fn scsipi_channel_timed_thaw "void *arg"
44.Ft void
45.Fn scsipi_periph_freeze "struct scsipi_periph *periph" "int count"
46.Ft void
47.Fn scsipi_periph_thaw "struct scsipi_periph *periph" "int count"
48.Ft void
49.Fn scsipi_periph_timed_thaw "void *arg"
50.Ft void
51.Fn scsipi_done "struct scsipi_xfer *xs"
52.Ft void
53.Fn scsipi_printaddr "struct scsipi_periph *periph"
54.Ft int
55.Fn scsipi_target_detach "struct scsipi_channel *chan" "int target" "int lun" "int flags"
56.Ft int
57.Fn scsipi_thread_call_callback "struct scsipi_channel *chan" "void (*callback)(struct scsipi_channel *, void *)" "void *arg"
58.Ft int
59.Fn scsipi_adapter_addref "struct scsipi_adapter *adapt"
60.Ft void
61.Fn scsipi_adapter_delref "struct scsipi_adapter *adapt"
62.Sh DESCRIPTION
63The
64.Nm
65system is the middle layer interface between SCSI/ATAPI host bus
66adapters (HBA) and high-level SCSI/ATAPI drivers.
67This document describes the interfaces provided by the
68.Nm
69layer towards the HBA layer.
70An HBA has to provide a pointer to a
71.Va struct scsipi_adapter
72and one pointer per channel to a
73.Va struct scsipi_channel .
74Once the SCSI or ATAPI bus is attached, the
75.Nm
76system will scan the bus and allocate a
77.Va struct scsipi_periph
78for each device found on the bus.
79A high-level command (command sent from the high-level SCSI/ATAPI
80layer to the low-level HBA layer) is described by a
81.Va struct scsipi_xfer .
82.Pp
83A request is sent to the HBA driver through the
84.Fn adapt_request
85callback.
86The HBA driver signals completion (with or without errors) of the
87request through
88.Fn scsipi_done .
89.Nm
90knows the resource's limits of the HBA (max number of concurrent requests per
91adapter of channel, and per periph), and will make sure the HBA won't receive
92more requests than it can handle.
93.Pp
94The mid-layer can also handle
95.Dv QUEUE FULL
96and
97.Dv CHECK CONDITION
98events.
99.Ss INITIALISATION
100An HBA driver has to allocate and initialize to 0 a
101.Va struct scsipi_adapter
102and fill in the following members:
103.Bl -tag -width "struct_device *adapt_dev" -compact -offset indent
104.It Va struct device *adapt_dev
105pointer to the HBA's
106.Va struct device
107.It Va int adapt_nchannels
108number of channels (or busses) of the adapter
109.It Va int adapt_openings
110total number of commands the adapter can handle (may be replaced by
111.Va chan_openings ,
112see below)
113.It Va int adapt_max_periph
114number of commands the adapter can handle per device
115.It Va int adapt_flags
116adapter properties
117.Bl -tag -width SCSIPI_ADAPT_POLL_ONLY -compact
118.It Dv SCSIPI_ADAPT_POLL_ONLY
119the HBA can't do interrupts
120.It Dv SCSIPI_ADAPT_MPSAFE
121don't acquire the kernel lock when doing HBA callbacks
122.El
123.El
124.Pp
125The following callbacks should be provided through the
126.Va struct scsipi_adapter :
127.Bl -tag -width someverylongword -compact -offset indent
128.It void Fn (*adapt_request) "struct scsipi_channel *" "scsipi_adapter_req_t" "void *"
129mandatory
130.It void Fn (*adapt_minphys) "struct buf *"
131mandatory
132.It int Fn (*adapt_ioctl) "struct scsipi_channel *" "u_long" "void *" "int" "struct lwp *"
133optional
134.It int Fn (*adapt_enable) "struct device *" "int"
135optional, set to
136.Dv NULL
137if not used
138.It int Fn (*adapt_getgeom) "struct scsipi_periph *" "struct disk_parms *" "u_long"
139optional, set to
140.Dv NULL
141if not used
142.It int Fn (*adapt_accesschk) "struct scsipi_periph *" "struct scsipi_inquiry_pattern *"
143optional, set to
144.Dv NULL
145if not used
146.El
147.Pp
148The HBA driver has to allocate and initialize to 0 one
149.Va struct scsipi_channel
150per channel and fill in the following members:
151.Bl -tag -width struct_scsipi_adapter -compact -offset indent
152.It Va struct scsipi_adapter *chan_adapter
153Pointer to the HBA's
154.Fa struct scsipi_adapter
155.It Va struct scsipi_bustype *chan_bustype
156should be initialized to either
157.Va bus_atapi
158or
159.Va bus_scsi ,
160both defined in the
161.Nm
162code.
163.It Va int chan_channel
164channel number (starting at 0)
165.It Va int chan_flags
166channel flags:
167.Bl -tag -width SCSIPI_CHAN_OPENINGS -compact
168.It Dv SCSIPI_CHAN_OPENINGS
169Use per-channel max number of commands
170.Va chan_openings
171instead of per-adapter
172.Va adapt_openings
173.It Dv SCSIPI_CHAN_CANGROW
174This channel can grow its
175.Va chan_openings
176or
177.Va adapt_openings
178on request (via the
179.Fn adapt_request
180callback)
181.It Dv SCSIPI_CHAN_NOSETTLE
182Do not wait SCSI_DELAY seconds for devices to settle before probing (usually
183used by adapters that provide an \*qabstracted\*q view of the bus).
184.El
185.It Va int chan_openings
186total number of commands the adapter can handle for this channel (used only
187if the
188.Dv SCSIPI_CHAN_OPENINGS
189flag is set)
190.It Va chan_max_periph
191number of commands per device the adapter can handle on this
192channel (used only if the
193.Va SCSIPI_CHAN_OPENINGS
194flag is set)
195.It Va int chan_ntargets
196number of targets
197.It Va int chan_nluns
198number of LUNs per target
199.It Va int chan_id
200adapter's ID on this channel
201.It Va int chan_defquirks
202default device quirks.
203Quirks are defined in
204.In dev/scsipi/scsipiconf.h
205and are usually set in the middle layer based on the device's inquiry
206data.
207For some kinds of adapters it may be convenient to have a set of
208quirks applied to all devices, regardless of the inquiry data.
209.El
210.Pp
211The HBA driver attaches the SCSI or ATAPI bus (depending on the setting of
212.Va chan_bustype )
213by passing a pointer to the
214.Va struct scsipi_channel
215to the
216.Xr autoconf 9
217machinery.
218The print function shall be either
219.Fn scsiprint
220or
221.Fn atapiprint .
222.Ss OTHER DATA STRUCTURES
223When scanning the bus, the
224.Nm
225system allocates a
226.Va struct scsipi_periph
227for each device probed.
228The interesting fields are:
229.Bl -tag -width int_periph_openings -compact -offset indent
230.It Va struct device *periph_dev
231pointer to the device's
232.Va struct device
233.It Va struct scsipi_channel *periph_channel
234pointer to the channel the device is connected to
235.It Va int periph_quirks
236device quirks, defined in
237.In dev/scsipi/scsipiconf.h
238.It Va int periph_target
239target ID, or drive number on ATAPI
240.It Va int periph_lun
241LUN (currently not used on ATAPI)
242.El
243.Pp
244A SCSI or ATAPI request is passed to the HBA through a
245.Va struct scsipi_xfer .
246The HBA driver has access to the following data:
247.Bl -tag -width "int xs_callout" -compact -offset indent
248.It Va struct callout xs_callout
249callout for adapter use, usually for command timeout
250.It Va int xs_control
251control flags (only flags of interest for HBA drivers are described):
252.Bl -tag -width XS_CTL_DISCOVERY -compact
253.It Dv XS_CTL_POLL
254poll in the HBA driver for request completion (most likely because interrupts
255are disabled)
256.It Dv XS_CTL_RESET
257reset the device
258.It Dv XS_CTL_DATA_UIO
259xs_data points to a
260.Fa struct uio
261buffer
262.It Dv XS_CTL_DATA_IN
263data is transferred from HBA to memory
264.It Dv XS_CTL_DATA_OUT
265data is transferred from memory to HBA
266.It Dv XS_CTL_DISCOVERY
267this xfer is part of a device discovery done by the middle layer
268.It Dv XS_CTL_REQSENSE
269xfer is a request sense
270.El
271.Pp
272.It Va int xs_status
273status flags:
274.Bl -tag -width XS_STS_PRIVATE -compact
275.It Va XS_STS_DONE
276xfer is done (set by
277.Fn scsipi_done )
278.It Va XS_STS_PRIVATE
279mask of flags reserved for HBA's use (0xf0000000)
280.El
281.Pp
282.It Va struct scsipi_periph *xs_periph
283periph doing the xfer
284.It Va int timeout
285command timeout, in milliseconds.
286The HBA should start the timeout at the time the command is accepted
287by the device.
288If the timeout happens, the HBA shall terminate the command through
289.Fn scsipi_done
290with a XS_TIMEOUT error
291.It Va struct scsipi_generic *cmd
292scsipi command to execute
293.It Va int cmdlen
294len (in bytes) of the cmd buffer
295.It Va u_char *data
296data buffer (this is either a DMA or uio address)
297.It Va int datalen
298data length (in bytes, zero if uio)
299.It Va int resid
300difference between
301.Fa datalen
302and how much data was really transferred
303.It Va scsipi_xfer_result_t error
304error value returned by the HBA driver to mid-layer.
305See description of
306.Fn scsipi_done
307for valid values
308.It Va union {struct scsipi_sense_data scsi_sense; uint32_t atapi_sense;} sense
309where to store sense info if
310.Fa error
311is
312.Dv XS_SENSE
313or
314.Dv XS_SHORTSENSE
315.It Va uint8_t status
316SCSI status; checked by middle layer when
317.Fa error is
318.Dv XS_BUSY
319(the middle layer handles
320.Dv SCSI_CHECK
321and
322.Dv SCSI_QUEUE_FULL )
323.It Va uint8_t xs_tag_type
324SCSI tag type, set to 0 if untagged command
325.It Va uint8_t xs_tag_id
326tag ID, used for tagged commands
327.El
328.Ss FUNCTIONS AND CALLBACKS
329.Bl -tag -width xxxxxxxx -compact
330.It Fn (*adapt_request) "struct scsipi_channel *chan" "scsipi_adapter_req_t req" "void *arg"
331Used by the mid-layer to transmit a request to the adapter.
332.Va req
333can be one of:
334.Bl -tag -width xxxxxxxx -compact
335.It Dv ADAPTER_REQ_RUN_XFER
336request the adapter to send a command to the device.
337.Fa arg
338is a pointer to the
339.Va struct scsipi_xfer .
340Once the xfer is complete the HBA driver shall call
341.Fn scsipi_done
342with updated status and error information.
343.It Dv ADAPTER_REQ_GROW_RESOURCES
344ask the adapter to increase resources of the channel (grow
345.Va adapt_openings
346or
347.Va chan_openings )
348if possible.
349Support of this feature is optional.
350This request is called from the kernel completion thread.
351.Fa arg
352must be ignored.
353.It Dv ADAPTER_REQ_SET_XFER_MODE
354set the xfer mode for a I_T Nexus.
355This will be called once all LUNs of a target have been probed.
356.Fa arg
357points to a
358.Va struct scsipi_xfer_mode
359defined as follows:
360.Bl -tag -width int_xm_target -compact
361.It Va int xm_target
362target for I_T Nexus
363.It Va int xm_mode
364bitmask of device capabilities
365.It Va int xm_period
366sync period
367.It Va int xm_offset
368sync offset
369.El
370.Pp
371.Va xm_period
372and
373.Va xm_offset
374shall be ignored for
375.Dv ADAPTER_REQ_SET_XFER_MODE .
376.Va xm_mode
377holds the following bits:
378.Bl -tag -width xxxxxxxx -compact
379.It Dv PERIPH_CAP_SYNC
380ST synchronous transfers
381.It Dv PERIPH_CAP_WIDE16
382ST 16 bit wide transfers
383.It Dv PERIPH_CAP_WIDE32
384ST 32 bit wide transfers
385.It Dv PERIPH_CAP_DT
386DT transfers
387.It Dv PERIPH_CAP_TQING
388tagged queuing
389.El
390Whenever the xfer mode changes, the driver should call
391.Fn scsipi_async_event
392to notify the mid-layer.
393.El
394.Pp
395.Fn adapt_request
396may be called from interrupt context.
397.It Fn adapt_minphys
398pointer to the driver's minphys function.
399If the driver can handle transfers of size
400.Dv MAXPHYS ,
401this can point to
402.Fn minphys .
403.It Fn adapt_ioctl
404ioctl function for the channel.
405The only ioctl supported at this level is
406.Dv SCBUSIORESET
407for which the HBA driver shall issue a SCSI reset on the channel.
408.It int Fn adapt_enable "struct device *dev" "int enable"
409Disable the adapter if
410.Va enable
411is zero, or enable it if non-zero.
412Returns 0 if operation is successful, or error from
413.Pa <sys/errno.h> .
414This callback is optional, and is useful mostly for hot-plug devices.
415For example, this callback would power on or off
416the relevant PCMCIA socket for a PCMCIA controller.
417.Fn scsipi_adapter_addref
418and
419.Fn scsipi_adapter_delref
420maintain a reference count, the enable callback is called appropriately
421for the first reference and the last reference.
422.It int Fn adapt_getgeom "struct scsipi_periph *periph" "struct disk_parms *params" "u_long sectors"
423Optional callback, used by high-level drivers to get the fictitious geometry
424used by the controller's firmware for the specified periph.
425Returns 0 if successful.
426See Adaptec drivers for details.
427.It int Fn adapt_accesschk "struct scsipi_periph *periph" "struct scsipi_inquiry_pattern *inqbuf"
428Optional callback; if present the mid-layer uses it to check if it can
429attach a driver to the specified periph.
430If the callback returns a non-zero value, the periph is ignored by the
431.Nm
432code.
433This callback is used by adapters which want to drive some devices
434themselves, for example hardware RAID controllers.
435.It Fn scsipi_async_event "struct scsipi_channel *chan" "scsipi_async_event_t event" "void *arg"
436Asynchronous event notification for the mid-layer.
437.Fa event
438can be one of:
439.Bl -tag -width xxxxxxxx -compact
440.It Dv ASYNC_EVENT_MAX_OPENINGS
441set max openings for a periph.
442Argument is a
443.Va struct scsipi_max_openings
444with at least the following members:
445.Bl -tag -width xxxxxxxx -compact
446.It Va int mo_target
447.It Va int mo_lun
448.It Va int mo_openings
449.El
450.Pp
451Not all periphs may allow openings to increase; if not allowed the request is
452silently ignored.
453.It Dv ASYNC_EVENT_XFER_MODE
454update the xfer mode for an I_T nexus.
455Argument is a
456.Va struct scsipi_xfer_mode
457properly filled in.
458An
459.Dv ASYNC_EVENT_XFER_MODE
460call with
461.Dv PERIPH_CAP_TQING
462set in
463.Va xm_mode
464is mandatory to activate tagged queuing.
465.It Dv ASYNC_EVENT_RESET
466channel has been reset.
467No argument.
468HBA drivers have to issue
469.Dv ASYNC_EVENT_RESET events if they rely on the
470mid-layer for SCSI CHECK CONDITION handling.
471.El
472.Pp
473.It Fn scsipi_done "struct scsipi_xfer *xs"
474shall be called by the HBA when the xfer is complete, or when it needs to
475be requeued by the mid-layer.
476.Va error
477in the scsipi_xfer shall be set to one of the following:
478.Bl -tag -width xxxxxxxx -compact
479.It Dv XS_NOERROR
480xfer completed without error.
481.It Dv XS_SENSE
482Check the returned SCSI sense for the error.
483.It Dv XS_SHORTSENSE
484Check the ATAPI sense for the error.
485.It Dv XS_DRIVER_STUFFUP
486Driver failed to perform operation.
487.It Dv XS_RESOURCE_SHORTAGE
488Adapter resource shortage.
489The mid-layer will retry the command after some delay.
490.It Dv XS_SELTIMEOUT
491The device timed out while trying to send the command
492.It Dv XS_TIMEOUT
493The command was accepted by the device, but it didn't complete in allowed time.
494.It Dv XS_BUSY
495The mid-layer will check
496.Va status
497for additional details:
498.Bl -tag -width SCSI_QUEUE_FULL -compact
499.It Dv SCSI_CHECK
500SCSI check condition.
501The mid-layer will freeze the periph queue and issue a REQUEST SENSE command.
502If the HBA supports tagged queuing, it shall remove and requeue any command
503not yet accepted by the HBA (or at last make sure no more commands will
504be sent to the device before the REQUEST SENSE is complete).
505.It Dv SCSI_QUEUE_FULL
506The mid layer will adjust the periph's openings and requeue the command.
507.It Dv SCSI_BUSY
508The mid-layer will requeue the xfer after delay.
509.El
510.It Dv XS_RESET
511xfer destroyed by a reset; the mid-layer will requeue it.
512.It Dv XS_REQUEUE
513Ask the mid-layer to requeue this command immediately.
514.El
515.Pp
516The adapter should not reference an
517.Fa xfer
518once
519.Fn scsipi_done "xfer"
520has been called, unless the
521.Fa xfer
522had
523.Dv XS_CTL_POLL
524set.
525.Pp
526.Fn scsipi_done
527will call the
528.Fn adapt_request
529callback again only if called with
530.Fa xs->error
531set to
532.Dv XS_NOERROR ,
533and
534.Fa xfer
535doesn't have
536.Dv XS_CTL_POLL
537set.
538All other error conditions are handled by a kernel thread
539(once the HBA's interrupt handler has returned).
540.It Fn scsipi_printaddr "struct scsipi_periph *periph"
541print a kernel message with the periph's name, in the form
542device(controller:channel:target:lun).
543.It Fn scsipi_channel_freeze "struct scsipi_channel *chan" "int count"
544Freeze the specified channel (requests are queued but not sent to HBA).
545The channel's freeze counter is increased by
546.Fa count .
547.It Fn scsipi_channel_thaw "struct scsipi_channel *chan" "int count"
548Decrement the channel's freeze counter by
549.Fa count
550and process the queue if the counter goes to 0.
551In order to preserve command ordering, HBA drivers should not call
552.Fn scsipi_channel_thaw
553before calling
554.Fn scsipi_done
555for all commands in the HBA's queue which need to be requeued.
556.It Fn scsipi_periph_timed_thaw "void *arg"
557Call
558.Fn scsipi_channel_thaw "arg" "1" .
559Intended to be used as
560.Xr callout 9
561callback.
562.It Fn scsipi_periph_freeze "struct scsipi_periph *periph" "int count"
563.It Fn scsipi_periph_thaw "struct scsipi_periph *periph"
564.It Fn scsipi_periph_timed_thaw "void *arg"
565Same as the channel counterparts, but only for one specific peripheral.
566.It Fn scsipi_target_detach "struct scsipi_channel *chan" "int target" "int lun" "int flags"
567detach the periph associated with this I_T_L nexus.
568Both
569.Fa target
570and
571.Fa lun
572may be wildcarded using the magic value -1.
573.Fa flags
574is passed to
575.Fn config_detach ""
576\&.
577Returns 0 if successful, or error code if a device couldn't be removed.
578.It Fn scsipi_thread_call_callback "struct scsipi_channel *chan" "void (*callback)(struct scsipi_channel *, void *)" "void *arg"
579.Fn callback
580will be called with
581.Fa chan
582and
583.Fa arg
584as arguments, from the channel completion thread.
585The callback is run at
586.Dv IPL_BIO
587with the channel lock held.
588.Fn scsipi_thread_call_callback
589will freeze the channel by one, it's up to the caller to thaw it when
590appropriate.
591Returns 0 if the callback was properly recorded, or EBUSY if the
592channel has already a callback pending.
593.El
594.Sh FILES
595.Bl -tag -width sys/dev/atapiconf.h
596.It Pa sys/dev/scsiconf.h
597header file for use by SCSI HBA drivers
598.It Pa sys/dev/atapiconf.h
599header file for use by ATAPI HBA drivers
600.El
601.Pp
602Both header files include
603.Pa sys/dev/scsipiconf.h
604which contains most structure definitions, function prototypes and macros.
605.Sh EXAMPLES
606The best examples are existing HBA drivers.
607Most of them sit in the
608.Pa sys/dev/ic
609directory.
610.Sh HISTORY
611The
612.Nm
613interface appeared in
614.Nx 1.6 .
615.Sh AUTHORS
616.An -nosplit
617The
618.Nm
619interface was designed and implemented by
620.An Jason R. Thorpe .
621.An Manuel Bouyer
622converted most drivers to the new interface.
623