SCSIPI(9)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸ’– NAME πŸ’–

scsipi β€” SCSI/ATAPI middle-layer interface. This is SCSIPI(9) from the Kernel Developer's Manual, bestie, and we’re folding that header in right here like a cute lil’ sticker. πŸŽ€


✨ SYNOPSIS ✨

#include <dev/scsipi/atapiconf.h>
#include <dev/scsipi/scsiconf.h>

void
scsipi_async_event(struct scsipi_channel *chan,
    scsipi_async_event_t event, void *arg);

void
scsipi_channel_freeze(struct scsipi_channel *chan, int count);

void
scsipi_channel_thaw(struct scsipi_channel *chan, int count);

void
scsipi_channel_timed_thaw(void *arg);

void
scsipi_periph_freeze(struct scsipi_periph *periph, int count);

void
scsipi_periph_thaw(struct scsipi_periph *periph, int count);

void
scsipi_periph_timed_thaw(void *arg);

void
scsipi_done(struct scsipi_xfer *xs);

void
scsipi_printaddr(struct scsipi_periph *periph);

int
scsipi_target_detach(struct scsipi_channel *chan, int target, int lun,
    int flags);

int
scsipi_thread_call_callback(struct scsipi_channel *chan,
    void (*callback)(struct scsipi_channel *, void *), void *arg);

int
scsipi_adapter_addref(struct scsipi_adapter *adapt);

void
scsipi_adapter_delref(struct scsipi_adapter *adapt);

🌸 DESCRIPTION 🌸

Okay so scsipi is the middle-layer interface between SCSI/ATAPI host bus adapters (HBA) and high-level SCSI/ATAPI drivers. This doc is specifically about the interfaces that the scsipi layer gives to the HBA layer. An HBA has to provide a pointer to a struct scsipi_adapter and one pointer per channel to a struct scsipi_channel. Once the SCSI or ATAPI bus is attached, the scsipi system will scan the bus and allocate a struct scsipi_periph for each device found on the bus. A high-level command β€” meaning a command sent from the high-level SCSI/ATAPI layer to the low-level HBA layer β€” is described by a struct scsipi_xfer. πŸ¦‹

A request is sent to the HBA driver through the adapt_request() callback. The HBA driver signals completion (with or without errors) of the request through scsipi_done(). scsipi knows the resource’s limits of the HBA β€” max number of concurrent requests per adapter of channel, and per periph β€” and will make sure the HBA won’t receive more requests than it can handle. πŸ’…

The mid-layer can also handle QUEUE FULL and CHECK CONDITION events. No cap. πŸ”₯

πŸ› οΈ INITALISATION πŸ› οΈ

An HBA driver has to allocate and initialize to 0 a struct scsipi_adapter and fill in the following members:

  • struct device *adapt_dev β€” pointer to the HBA’s struct device.
  • int adapt_nchannels β€” number of channels (or busses) of the adapter.
  • int adapt_openings β€” total number of commands the adapter can handle (may be replaced by chan_openings, see below).
  • int adapt_max_periph β€” number of commands the adapter can handle per device.
  • int adapt_flags β€” adapter properties:
    • SCSIPI_ADAPT_POLL_ONLY β€” the HBA can’t do interrupts.
    • SCSIPI_ADAPT_MPSAFE β€” don’t acquire the kernel lock when doing HBA callbacks.

The following callbacks should be provided through the struct scsipi_adapter:

  • void (*adapt_request)(struct scsipi_channel *, scsipi_adapter_req_t, void *) β€” mandatory.
  • void (*adapt_minphys)(struct buf *) β€” mandatory.
  • int (*adapt_ioctl)(struct scsipi_channel *, u_long, void *, int, struct lwp *) β€” optional.
  • int (*adapt_enable)(struct device *, int) β€” optional, set to NULL if not used.
  • int (*adapt_getgeom)(struct scsipi_periph *, struct disk_parms *, u_long) β€” optional, set to NULL if not used.
  • int (*adapt_accesschk)(struct scsipi_periph *, struct scsipi_inquiry_pattern *) β€” optional, set to NULL if not used.

The HBA driver has to allocate and initialize to 0 one struct scsipi_channel per channel and fill in the following members:

  • struct scsipi_adapter *chan_adapter β€” Pointer to the HBA’s struct scsipi_adapter.
  • struct scsipi_bustype *chan_bustype β€” should be initialized to either bus_atapi or bus_scsi, both defined in the scsipi code.
  • int chan_channel β€” channel number (starting at 0).
  • int chan_flags β€” channel flags:
    • SCSIPI_CHAN_OPENINGS β€” Use per-channel max number of commands chan_openings instead of per-adapter adapt_openings.
    • SCSIPI_CHAN_CANGROW β€” This channel can grow its chan_openings or adapt_openings on request (via the adapt_request() callback).
    • SCSIPI_CHAN_NOSETTLE β€” Do not wait SCSI_DELAY seconds for devices to settle before probing (usually used by adapters that provide an β€œabstracted” view of the bus).
  • int chan_openings β€” total number of commands the adapter can handle for this channel (used only if the SCSIPI_CHAN_OPENINGS flag is set).
  • chan_max_periph β€” number of commands per device the adapter can handle on this channel (used only if the SCSIPI_CHAN_OPENINGS flag is set).
  • int chan_ntargets β€” number of targets.
  • int chan_nluns β€” number of LUNs per target.
  • int chan_id β€” adapter’s ID on this channel.
  • int chan_defquirks β€” default device quirks. Quirks are defined in <dev/scsipi/scsipiconf.h> and are usually set in the middle layer based on the device’s inquiry data. For some kinds of adapters it may be convenient to have a set of quirks applied to all devices, regardless of the inquiry data.

The HBA driver attaches the SCSI or ATAPI bus (depending on the setting of chan_bustype) by passing a pointer to the struct scsipi_channel to the autoconf(9) machinery. The print function shall be either scsiprint() or atapiprint(). πŸ‘‘

πŸŽ€ OTHER DATA STRUCTURES πŸŽ€

When scanning the bus, the scsipi system allocates a struct scsipi_periph for each device probed. The interesting fields are:

  • struct device *periph_dev β€” pointer to the device’s struct device.
  • struct scsipi_channel *periph_channel β€” pointer to the channel the device is connected to.
  • int periph_quirks β€” device quirks, defined in <dev/scsipi/scsipiconf.h>.
  • int periph_target β€” target ID, or drive number on ATAPI.
  • int periph_lun β€” LUN (currently not used on ATAPI).

A SCSI or ATAPI request is passed to the HBA through a struct scsipi_xfer. The HBA driver has access to the following data:

  • struct callout xs_callout β€” callout for adapter use, usually for command timeout.
  • int xs_control β€” control flags (only flags of interest for HBA drivers are described):
    • XS_CTL_POLL β€” poll in the HBA driver for request completion (most likely because interrupts are disabled).
    • XS_CTL_RESET β€” reset the device.
    • XS_CTL_DATA_UIO β€” xs_data points to a struct uio buffer.
    • XS_CTL_DATA_IN β€” data is transferred from HBA to memory.
    • XS_CTL_DATA_OUT β€” data is transferred from memory to HBA.
    • XS_CTL_DISCOVERY β€” this xfer is part of a device discovery done by the middle layer.
    • XS_CTL_REQSENSE β€” xfer is a request sense.
  • int xs_status β€” status flags:
    • XS_STS_DONE β€” xfer is done (set by scsipi_done()).
    • XS_STS_PRIVATE β€” mask of flags reserved for HBA’s use (0xf0000000).

Okay so, picking right back up with the rest of the scsipi_xfer fields β€” stay with me, bestie, because every single one of these matters:

  • struct scsipi_periph *xs_periph β€” the periph doing the xfer πŸ’…
  • int timeout β€” command timeout, in milliseconds. The HBA should start the timeout at the time the command is accepted by the device. If the timeout happens, the HBA shall terminate the command through scsipi_done() with a XS_TIMEOUT error.
  • struct scsipi_generic *cmd β€” the scsipi command to execute.
  • int cmdlen β€” len (in bytes) of the cmd buffer.
  • u_char *data β€” data buffer (this is either a DMA or uio address).
  • int datalen β€” data length (in bytes, zero if uio).
  • int resid β€” difference between datalen and how much data was really transferred.
  • scsipi_xfer_result_t error β€” error value returned by the HBA driver to mid-layer. See description of scsipi_done() for valid values.
  • union {struct scsipi_sense_data scsi_sense; uint32_t atapi_sense;} sense β€” where to store sense info if error is XS_SENSE or XS_SHORTSENSE.
  • uint8_t status β€” SCSI status; checked by middle layer when error is XS_BUSY (the middle layer handles SCSI_CHECK and SCSI_QUEUE_FULL).
  • uint8_t xs_tag_type β€” SCSI tag type, set to 0 if untagged command.
  • uint8_t xs_tag_id β€” tag ID, used for tagged commands.

✨ FUNCTIONS AND CALLBACKS ✨

  • (*adapt_request)(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg) β€” used by the mid-layer to transmit a request to the adapter. req can be one of:
    • ADAPTER_REQ_RUN_XFER β€” request the adapter to send a command to the device. arg is a pointer to the struct scsipi_xfer. Once the xfer is complete the HBA driver shall call scsipi_done() with updated status and error information.
    • ADAPTER_REQ_GROW_RESOURCES β€” ask the adapter to increase resources of the channel (grow adapt_openings or chan_openings) if possible. Support of this feature is optional. This request is called from the kernel completion thread. arg must be ignored.
    • ADAPTER_REQ_SET_XFER_MODE β€” set the xfer mode for a I_T Nexus. This will be called once all LUNs of a target have been probed. arg points to a struct scsipi_xfer_mode defined as follows:
int xm_target  target for I_T Nexus
int xm_mode    bitmask of device capabilities
int xm_period  sync period
int xm_offset  sync offset

xm_period and xm_offset shall be ignored for ADAPTER_REQ_SET_XFER_MODE. xm_mode holds the following bits:

  • PERIPH_CAP_SYNC β€” ST synchronous transfers
  • PERIPH_CAP_WIDE16 β€” ST 16 bit wide transfers
  • PERIPH_CAP_WIDE32 β€” ST 32 bit wide transfers
  • PERIPH_CAP_DT β€” DT transfers
  • PERIPH_CAP_TQING β€” tagged queuing

Whenever the xfer mode changes, the driver should call scsipi_async_event() to notify the mid-layer.

adapt_request() may be called from interrupt context. No cap, be ready for it. 😭
  • adapt_minphys() β€” pointer to the driver's minphys function. If the driver can handle transfers of size MAXPHYS, this can point to minphys().
  • adapt_ioctl() β€” ioctl function for the channel. The only ioctl supported at this level is SCBUSIORESET for which the HBA driver shall issue a SCSI reset on the channel.
  • int adapt_enable(struct device *dev, int enable) β€” disable the adapter if enable is zero, or enable it if non-zero. Returns 0 if operation is successful, or error from <sys/errno.h>. This callback is optional, and is useful mostly for hot-plug devices. For example, this callback would power on or off the relevant PCMCIA socket for a PCMCIA controller. scsipi_adapter_addref() and scsipi_adapter_delref() maintain a reference count, the enable callback is called appropriately for the first reference and the last reference.
  • int adapt_getgeom(struct scsipi_periph *periph, struct disk_parms *params, u_long sectors) β€” optional callback, used by high-level drivers to get the fictitious geometry used by the controller's firmware for the specified periph. Returns 0 if successful. See Adaptec drivers for details.
  • int adapt_accesschk(struct scsipi_periph *periph, struct scsipi_inquiry_pattern *inqbuf) β€” optional callback; if present the mid-layer uses it to check if it can attach a driver to the specified periph. If the callback returns a non-zero value, the periph is ignored by the scsipi code. This callback is used by adapters which want to drive some devices themselves, for example hardware RAID controllers.
  • scsipi_async_event(struct scsipi_channel *chan, scsipi_async_event_t event, void *arg) β€” asynchronous event notification for the mid-layer. event can be one of:
    • ASYNC_EVENT_MAX_OPENINGS β€” set max openings for a periph. Argument is a struct scsipi_max_openings with at least the following members:
int mo_target
int mo_lun
int mo_openings

Not all periphs may allow openings to increase; if not allowed the request is silently ignored.

  • ASYNC_EVENT_XFER_MODE β€” update the xfer mode for an I_T nexus. Argument is a struct scsipi_xfer_mode properly filled in. An ASYNC_EVENT_XFER_MODE call with PERIPH_CAP_TQING set in xm_mode is mandatory to activate tagged queuing.
  • ASYNC_EVENT_RESET β€” channel has been reset. No argument. HBA drivers have to issue ASYNC_EVENT_RESET events if they rely on the mid-layer for SCSI CHECK CONDITION handling.
  • scsipi_done(struct scsipi_xfer *xs) β€” shall be called by the HBA when the xfer is complete, or when it needs to be requeued by the mid-layer. error in the scsipi_xfer shall be set to one of the following:
    • XS_NOERROR β€” xfer completed without error.
    • XS_SENSE β€” check the returned SCSI sense for the error.
    • XS_SHORTSENSE β€” check the ATAPI sense for the error.
    • XS_DRIVER_STUFFUP β€” driver failed to perform operation.
    • XS_RESOURCE_SHORTAGE β€” adapter resource shortage. The mid-layer will retry the command after some delay.
    • XS_SELTIMEOUT β€” the device timed out while trying to send the command.
    • XS_TIMEOUT β€” the command was accepted by the device, but it didn't complete in allowed time.
    • XS_BUSY β€” the mid-layer will check status for additional details:
      • SCSI_CHECK β€” SCSI check condition. The mid-layer will freeze the periph queue and issue a REQUEST SENSE command. If the HBA supports tagged queuing, it shall remove and requeue any command not yet accepted by the HBA (or at last make sure no more commands will be sent to the device before the REQUEST SENSE is complete).
      • SCSI_QUEUE_FULL β€” the mid layer will adjust the periph's openings and requeue the command.
      • SCSI_BUSY β€” the mid-layer will requeue the xfer after delay.
    • XS_RESET β€” xfer destroyed by a reset; the mid-layer will requeue it.
    • XS_REQUEUE β€” ask the mid-layer to requeue this command immediately.
The adapter should not reference an xfer once scsipi_done(xfer) has been called, unless the xfer had XS_CTL_POLL set. This is a hard line, bestie β€” do not cross it. πŸ’€
  • scsipi_done() β€” this one's the vibe check for the adapt layer: it will call the adapt_request() callback again only if it's called with xs->error set to XS_NOERROR, and only if xfer doesn't have XS_CTL_POLL set. Every other error condition gets handled by a kernel thread (once the HBA's interrupt handler has returned). No cap, that's just how it is πŸ’…
  • scsipi_printaddr(struct scsipi_periph *periph) β€” prints a kernel message with the periph's name, in the form device(controller:channel:target:lun). It's giving "hello, this is my whole address" energy ✨
  • scsipi_channel_freeze(struct scsipi_channel *chan, int count) β€” freezes the specified channel, meaning requests get queued but not sent to the HBA. The channel's freeze counter goes up by count. Think of it as putting the whole channel on Do Not Disturb 🧊
  • scsipi_channel_thaw(struct scsipi_channel *chan, int count) β€” decrements the channel's freeze counter by count and processes the queue if the counter hits 0. But hear me out: to preserve command ordering, HBA drivers should not call scsipi_channel_thaw() before calling scsipi_done() for all commands in the HBA's queue which need to be requeued. Order matters, bestie, fr fr πŸ”₯
  • scsipi_periph_timed_thaw(void *arg) β€” literally just calls scsipi_channel_thaw(arg, 1). It's intended to be used as a callout(9) callback. Scheduled thaw, iconic behavior ⏰
  • scsipi_periph_freeze(struct scsipi_periph *periph, int count)
  • scsipi_periph_thaw(struct scsipi_periph *periph)
  • scsipi_periph_timed_thaw(void *arg)

Same as the channel counterparts, but only for one specific peripheral. Same energy, smaller scope β€” main character periph only πŸ‘‘

  • scsipi_target_detach(struct scsipi_channel *chan, int target, int lun, int flags) β€” detaches the periph associated with this I_T_L nexus. Both target and lun may be wildcarded using the magic value -1. flags is passed to config_detach(). Returns 0 if successful, or an error code if a device couldn't be removed. Breaking up with a device is complicated sometimes 😭
  • scsipi_thread_call_callback(struct scsipi_channel *chan, void (*callback)(struct scsipi_channel *, void *), void *arg) β€” callback() will be called with chan and arg as arguments, from the channel completion thread. The callback is run at IPL_BIO with the channel lock held. scsipi_thread_call_callback() will freeze the channel by one, and it's up to the caller to thaw it when appropriate. Returns 0 if the callback was properly recorded, or EBUSY if the channel has already a callback pending. So like, it's a whole commitment, don't double-book 🌸

πŸ“ FILES

  • sys/dev/scsiconf.h β€” header file for use by SCSI HBA drivers
  • sys/dev/atapiconf.h β€” header file for use by ATAPI HBA drivers

Both header files include sys/dev/scsipiconf.h which contains most structure definitions, function prototypes and macros. So scsipiconf.h is the actual mothership πŸ›Έ


🌈 EXAMPLES

The best examples are existing HBA drivers. Most of them sit in the sys/dev/ic directory. Go read the source, bestie, that's the whole assignment πŸ“–βœ¨


πŸ•°οΈ HISTORY

The scsipi interface appeared in NetBSD 1.6. Vintage but make it fresh πŸ’œ


πŸ‘‘ AUTHORS

The scsipi interface was designed and implemented by Jason R. Thorpe. Manuel Bouyer converted most drivers to the new interface. Absolute legends, both of them 🫢


And that's the whole page, babe β€” brought to you by Debian, dated November 20, 2016. Slay, go write a driver πŸ’…πŸŽ€

πŸ“¦ where this page comes from