1*4882a593Smuzhiyun===================== 2*4882a593SmuzhiyunSCSI Interfaces Guide 3*4882a593Smuzhiyun===================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: James Bottomley 6*4882a593Smuzhiyun:Author: Rob Landley 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunIntroduction 9*4882a593Smuzhiyun============ 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunProtocol vs bus 12*4882a593Smuzhiyun--------------- 13*4882a593Smuzhiyun 14*4882a593SmuzhiyunOnce upon a time, the Small Computer Systems Interface defined both a 15*4882a593Smuzhiyunparallel I/O bus and a data protocol to connect a wide variety of 16*4882a593Smuzhiyunperipherals (disk drives, tape drives, modems, printers, scanners, 17*4882a593Smuzhiyunoptical drives, test equipment, and medical devices) to a host computer. 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunAlthough the old parallel (fast/wide/ultra) SCSI bus has largely fallen 20*4882a593Smuzhiyunout of use, the SCSI command set is more widely used than ever to 21*4882a593Smuzhiyuncommunicate with devices over a number of different busses. 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunThe `SCSI protocol <http://www.t10.org/scsi-3.htm>`__ is a big-endian 24*4882a593Smuzhiyunpeer-to-peer packet based protocol. SCSI commands are 6, 10, 12, or 16 25*4882a593Smuzhiyunbytes long, often followed by an associated data payload. 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunSCSI commands can be transported over just about any kind of bus, and 28*4882a593Smuzhiyunare the default protocol for storage devices attached to USB, SATA, SAS, 29*4882a593SmuzhiyunFibre Channel, FireWire, and ATAPI devices. SCSI packets are also 30*4882a593Smuzhiyuncommonly exchanged over Infiniband, 31*4882a593Smuzhiyun`I2O <http://i2o.shadowconnect.com/faq.php>`__, TCP/IP 32*4882a593Smuzhiyun(`iSCSI <https://en.wikipedia.org/wiki/ISCSI>`__), even `Parallel 33*4882a593Smuzhiyunports <http://cyberelk.net/tim/parport/parscsi.html>`__. 34*4882a593Smuzhiyun 35*4882a593SmuzhiyunDesign of the Linux SCSI subsystem 36*4882a593Smuzhiyun---------------------------------- 37*4882a593Smuzhiyun 38*4882a593SmuzhiyunThe SCSI subsystem uses a three layer design, with upper, mid, and low 39*4882a593Smuzhiyunlayers. Every operation involving the SCSI subsystem (such as reading a 40*4882a593Smuzhiyunsector from a disk) uses one driver at each of the 3 levels: one upper 41*4882a593Smuzhiyunlayer driver, one lower layer driver, and the SCSI midlayer. 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunThe SCSI upper layer provides the interface between userspace and the 44*4882a593Smuzhiyunkernel, in the form of block and char device nodes for I/O and ioctl(). 45*4882a593SmuzhiyunThe SCSI lower layer contains drivers for specific hardware devices. 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunIn between is the SCSI mid-layer, analogous to a network routing layer 48*4882a593Smuzhiyunsuch as the IPv4 stack. The SCSI mid-layer routes a packet based data 49*4882a593Smuzhiyunprotocol between the upper layer's /dev nodes and the corresponding 50*4882a593Smuzhiyundevices in the lower layer. It manages command queues, provides error 51*4882a593Smuzhiyunhandling and power management functions, and responds to ioctl() 52*4882a593Smuzhiyunrequests. 53*4882a593Smuzhiyun 54*4882a593SmuzhiyunSCSI upper layer 55*4882a593Smuzhiyun================ 56*4882a593Smuzhiyun 57*4882a593SmuzhiyunThe upper layer supports the user-kernel interface by providing device 58*4882a593Smuzhiyunnodes. 59*4882a593Smuzhiyun 60*4882a593Smuzhiyunsd (SCSI Disk) 61*4882a593Smuzhiyun-------------- 62*4882a593Smuzhiyun 63*4882a593Smuzhiyunsd (sd_mod.o) 64*4882a593Smuzhiyun 65*4882a593Smuzhiyunsr (SCSI CD-ROM) 66*4882a593Smuzhiyun---------------- 67*4882a593Smuzhiyun 68*4882a593Smuzhiyunsr (sr_mod.o) 69*4882a593Smuzhiyun 70*4882a593Smuzhiyunst (SCSI Tape) 71*4882a593Smuzhiyun-------------- 72*4882a593Smuzhiyun 73*4882a593Smuzhiyunst (st.o) 74*4882a593Smuzhiyun 75*4882a593Smuzhiyunsg (SCSI Generic) 76*4882a593Smuzhiyun----------------- 77*4882a593Smuzhiyun 78*4882a593Smuzhiyunsg (sg.o) 79*4882a593Smuzhiyun 80*4882a593Smuzhiyunch (SCSI Media Changer) 81*4882a593Smuzhiyun----------------------- 82*4882a593Smuzhiyun 83*4882a593Smuzhiyunch (ch.c) 84*4882a593Smuzhiyun 85*4882a593SmuzhiyunSCSI mid layer 86*4882a593Smuzhiyun============== 87*4882a593Smuzhiyun 88*4882a593SmuzhiyunSCSI midlayer implementation 89*4882a593Smuzhiyun---------------------------- 90*4882a593Smuzhiyun 91*4882a593Smuzhiyuninclude/scsi/scsi_device.h 92*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~ 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun.. kernel-doc:: include/scsi/scsi_device.h 95*4882a593Smuzhiyun :internal: 96*4882a593Smuzhiyun 97*4882a593Smuzhiyundrivers/scsi/scsi.c 98*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 99*4882a593Smuzhiyun 100*4882a593SmuzhiyunMain file for the SCSI midlayer. 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi.c 103*4882a593Smuzhiyun :export: 104*4882a593Smuzhiyun 105*4882a593Smuzhiyundrivers/scsi/scsicam.c 106*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~ 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun`SCSI Common Access 109*4882a593SmuzhiyunMethod <http://www.t10.org/ftp/t10/drafts/cam/cam-r12b.pdf>`__ support 110*4882a593Smuzhiyunfunctions, for use with HDIO_GETGEO, etc. 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsicam.c 113*4882a593Smuzhiyun :export: 114*4882a593Smuzhiyun 115*4882a593Smuzhiyundrivers/scsi/scsi_error.c 116*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunCommon SCSI error/timeout handling routines. 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_error.c 121*4882a593Smuzhiyun :export: 122*4882a593Smuzhiyun 123*4882a593Smuzhiyundrivers/scsi/scsi_devinfo.c 124*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunManage scsi_dev_info_list, which tracks blacklisted and whitelisted 127*4882a593Smuzhiyundevices. 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_devinfo.c 130*4882a593Smuzhiyun :internal: 131*4882a593Smuzhiyun 132*4882a593Smuzhiyundrivers/scsi/scsi_ioctl.c 133*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunHandle ioctl() calls for SCSI devices. 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_ioctl.c 138*4882a593Smuzhiyun :export: 139*4882a593Smuzhiyun 140*4882a593Smuzhiyundrivers/scsi/scsi_lib.c 141*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 142*4882a593Smuzhiyun 143*4882a593SmuzhiyunSCSI queuing library. 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_lib.c 146*4882a593Smuzhiyun :export: 147*4882a593Smuzhiyun 148*4882a593Smuzhiyundrivers/scsi/scsi_lib_dma.c 149*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunSCSI library functions depending on DMA (map and unmap scatter-gather 152*4882a593Smuzhiyunlists). 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_lib_dma.c 155*4882a593Smuzhiyun :export: 156*4882a593Smuzhiyun 157*4882a593Smuzhiyundrivers/scsi/scsi_proc.c 158*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~ 159*4882a593Smuzhiyun 160*4882a593SmuzhiyunThe functions in this file provide an interface between the PROC file 161*4882a593Smuzhiyunsystem and the SCSI device drivers It is mainly used for debugging, 162*4882a593Smuzhiyunstatistics and to pass information directly to the lowlevel driver. I.E. 163*4882a593Smuzhiyunplumbing to manage /proc/scsi/\* 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_proc.c 166*4882a593Smuzhiyun :internal: 167*4882a593Smuzhiyun 168*4882a593Smuzhiyundrivers/scsi/scsi_netlink.c 169*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 170*4882a593Smuzhiyun 171*4882a593SmuzhiyunInfrastructure to provide async events from transports to userspace via 172*4882a593Smuzhiyunnetlink, using a single NETLINK_SCSITRANSPORT protocol for all 173*4882a593Smuzhiyuntransports. See `the original patch 174*4882a593Smuzhiyunsubmission <http://marc.info/?l=linux-scsi&m=115507374832500&w=2>`__ for 175*4882a593Smuzhiyunmore details. 176*4882a593Smuzhiyun 177*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_netlink.c 178*4882a593Smuzhiyun :internal: 179*4882a593Smuzhiyun 180*4882a593Smuzhiyundrivers/scsi/scsi_scan.c 181*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~ 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunScan a host to determine which (if any) devices are attached. The 184*4882a593Smuzhiyungeneral scanning/probing algorithm is as follows, exceptions are made to 185*4882a593Smuzhiyunit depending on device specific flags, compilation options, and global 186*4882a593Smuzhiyunvariable (boot or module load time) settings. A specific LUN is scanned 187*4882a593Smuzhiyunvia an INQUIRY command; if the LUN has a device attached, a scsi_device 188*4882a593Smuzhiyunis allocated and setup for it. For every id of every channel on the 189*4882a593Smuzhiyungiven host, start by scanning LUN 0. Skip hosts that don't respond at 190*4882a593Smuzhiyunall to a scan of LUN 0. Otherwise, if LUN 0 has a device attached, 191*4882a593Smuzhiyunallocate and setup a scsi_device for it. If target is SCSI-3 or up, 192*4882a593Smuzhiyunissue a REPORT LUN, and scan all of the LUNs returned by the REPORT LUN; 193*4882a593Smuzhiyunelse, sequentially scan LUNs up until some maximum is reached, or a LUN 194*4882a593Smuzhiyunis seen that cannot have a device attached to it. 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_scan.c 197*4882a593Smuzhiyun :internal: 198*4882a593Smuzhiyun 199*4882a593Smuzhiyundrivers/scsi/scsi_sysctl.c 200*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~ 201*4882a593Smuzhiyun 202*4882a593SmuzhiyunSet up the sysctl entry: "/dev/scsi/logging_level" 203*4882a593Smuzhiyun(DEV_SCSI_LOGGING_LEVEL) which sets/returns scsi_logging_level. 204*4882a593Smuzhiyun 205*4882a593Smuzhiyundrivers/scsi/scsi_sysfs.c 206*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 207*4882a593Smuzhiyun 208*4882a593SmuzhiyunSCSI sysfs interface routines. 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_sysfs.c 211*4882a593Smuzhiyun :export: 212*4882a593Smuzhiyun 213*4882a593Smuzhiyundrivers/scsi/hosts.c 214*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~ 215*4882a593Smuzhiyun 216*4882a593Smuzhiyunmid to lowlevel SCSI driver interface 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/hosts.c 219*4882a593Smuzhiyun :export: 220*4882a593Smuzhiyun 221*4882a593Smuzhiyundrivers/scsi/scsi_common.c 222*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 223*4882a593Smuzhiyun 224*4882a593Smuzhiyungeneral support functions 225*4882a593Smuzhiyun 226*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_common.c 227*4882a593Smuzhiyun :export: 228*4882a593Smuzhiyun 229*4882a593SmuzhiyunTransport classes 230*4882a593Smuzhiyun----------------- 231*4882a593Smuzhiyun 232*4882a593SmuzhiyunTransport classes are service libraries for drivers in the SCSI lower 233*4882a593Smuzhiyunlayer, which expose transport attributes in sysfs. 234*4882a593Smuzhiyun 235*4882a593SmuzhiyunFibre Channel transport 236*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~ 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunThe file drivers/scsi/scsi_transport_fc.c defines transport attributes 239*4882a593Smuzhiyunfor Fibre Channel. 240*4882a593Smuzhiyun 241*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_transport_fc.c 242*4882a593Smuzhiyun :export: 243*4882a593Smuzhiyun 244*4882a593SmuzhiyuniSCSI transport class 245*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~ 246*4882a593Smuzhiyun 247*4882a593SmuzhiyunThe file drivers/scsi/scsi_transport_iscsi.c defines transport 248*4882a593Smuzhiyunattributes for the iSCSI class, which sends SCSI packets over TCP/IP 249*4882a593Smuzhiyunconnections. 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_transport_iscsi.c 252*4882a593Smuzhiyun :export: 253*4882a593Smuzhiyun 254*4882a593SmuzhiyunSerial Attached SCSI (SAS) transport class 255*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 256*4882a593Smuzhiyun 257*4882a593SmuzhiyunThe file drivers/scsi/scsi_transport_sas.c defines transport 258*4882a593Smuzhiyunattributes for Serial Attached SCSI, a variant of SATA aimed at large 259*4882a593Smuzhiyunhigh-end systems. 260*4882a593Smuzhiyun 261*4882a593SmuzhiyunThe SAS transport class contains common code to deal with SAS HBAs, an 262*4882a593Smuzhiyunaproximated representation of SAS topologies in the driver model, and 263*4882a593Smuzhiyunvarious sysfs attributes to expose these topologies and management 264*4882a593Smuzhiyuninterfaces to userspace. 265*4882a593Smuzhiyun 266*4882a593SmuzhiyunIn addition to the basic SCSI core objects this transport class 267*4882a593Smuzhiyunintroduces two additional intermediate objects: The SAS PHY as 268*4882a593Smuzhiyunrepresented by struct sas_phy defines an "outgoing" PHY on a SAS HBA or 269*4882a593SmuzhiyunExpander, and the SAS remote PHY represented by struct sas_rphy defines 270*4882a593Smuzhiyunan "incoming" PHY on a SAS Expander or end device. Note that this is 271*4882a593Smuzhiyunpurely a software concept, the underlying hardware for a PHY and a 272*4882a593Smuzhiyunremote PHY is the exactly the same. 273*4882a593Smuzhiyun 274*4882a593SmuzhiyunThere is no concept of a SAS port in this code, users can see what PHYs 275*4882a593Smuzhiyunform a wide port based on the port_identifier attribute, which is the 276*4882a593Smuzhiyunsame for all PHYs in a port. 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_transport_sas.c 279*4882a593Smuzhiyun :export: 280*4882a593Smuzhiyun 281*4882a593SmuzhiyunSATA transport class 282*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~ 283*4882a593Smuzhiyun 284*4882a593SmuzhiyunThe SATA transport is handled by libata, which has its own book of 285*4882a593Smuzhiyundocumentation in this directory. 286*4882a593Smuzhiyun 287*4882a593SmuzhiyunParallel SCSI (SPI) transport class 288*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 289*4882a593Smuzhiyun 290*4882a593SmuzhiyunThe file drivers/scsi/scsi_transport_spi.c defines transport 291*4882a593Smuzhiyunattributes for traditional (fast/wide/ultra) SCSI busses. 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_transport_spi.c 294*4882a593Smuzhiyun :export: 295*4882a593Smuzhiyun 296*4882a593SmuzhiyunSCSI RDMA (SRP) transport class 297*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 298*4882a593Smuzhiyun 299*4882a593SmuzhiyunThe file drivers/scsi/scsi_transport_srp.c defines transport 300*4882a593Smuzhiyunattributes for SCSI over Remote Direct Memory Access. 301*4882a593Smuzhiyun 302*4882a593Smuzhiyun.. kernel-doc:: drivers/scsi/scsi_transport_srp.c 303*4882a593Smuzhiyun :export: 304*4882a593Smuzhiyun 305*4882a593SmuzhiyunSCSI lower layer 306*4882a593Smuzhiyun================ 307*4882a593Smuzhiyun 308*4882a593SmuzhiyunHost Bus Adapter transport types 309*4882a593Smuzhiyun-------------------------------- 310*4882a593Smuzhiyun 311*4882a593SmuzhiyunMany modern device controllers use the SCSI command set as a protocol to 312*4882a593Smuzhiyuncommunicate with their devices through many different types of physical 313*4882a593Smuzhiyunconnections. 314*4882a593Smuzhiyun 315*4882a593SmuzhiyunIn SCSI language a bus capable of carrying SCSI commands is called a 316*4882a593Smuzhiyun"transport", and a controller connecting to such a bus is called a "host 317*4882a593Smuzhiyunbus adapter" (HBA). 318*4882a593Smuzhiyun 319*4882a593SmuzhiyunDebug transport 320*4882a593Smuzhiyun~~~~~~~~~~~~~~~ 321*4882a593Smuzhiyun 322*4882a593SmuzhiyunThe file drivers/scsi/scsi_debug.c simulates a host adapter with a 323*4882a593Smuzhiyunvariable number of disks (or disk like devices) attached, sharing a 324*4882a593Smuzhiyuncommon amount of RAM. Does a lot of checking to make sure that we are 325*4882a593Smuzhiyunnot getting blocks mixed up, and panics the kernel if anything out of 326*4882a593Smuzhiyunthe ordinary is seen. 327*4882a593Smuzhiyun 328*4882a593SmuzhiyunTo be more realistic, the simulated devices have the transport 329*4882a593Smuzhiyunattributes of SAS disks. 330*4882a593Smuzhiyun 331*4882a593SmuzhiyunFor documentation see http://sg.danny.cz/sg/sdebug26.html 332*4882a593Smuzhiyun 333*4882a593Smuzhiyuntodo 334*4882a593Smuzhiyun~~~~ 335*4882a593Smuzhiyun 336*4882a593SmuzhiyunParallel (fast/wide/ultra) SCSI, USB, SATA, SAS, Fibre Channel, 337*4882a593SmuzhiyunFireWire, ATAPI devices, Infiniband, I2O, Parallel ports, 338*4882a593Smuzhiyunnetlink... 339