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