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