docs/nscsi.txt
| r0 | r15755 | |
| 1 | The new SCSI subsystem |
| 2 | ---------------------- |
| 3 | |
| 4 | 1. Introduction |
| 5 | |
| 6 | The nscsi subsystem was created to allow an implementation to be |
| 7 | closer to the physical reality, making it easier (hopefully) to |
| 8 | implement new controller chips from the documentations. |
| 9 | |
| 10 | |
| 11 | 2. Global structure |
| 12 | |
| 13 | Parallel SCSI is built around a symmetric bus to which a number of |
| 14 | devices are connected. The bus is composed of 9 control lines (for |
| 15 | now, later scsi versions may have more) and up to 32 data lines (but |
| 16 | currently implemented chips only handle 8). All the lines are open |
| 17 | collector, which means that either one or multiple chip connect the |
| 18 | line to ground and the line, of course, goes to ground, or no chip |
| 19 | drives anything and the line stays at Vcc. Also, the bus uses |
| 20 | inverted logic, where ground means 1. SCSI chips traditionally work |
| 21 | in logical and not physical levels, so the nscsi subsystem also works |
| 22 | in logical levels and does a logical-or of all the outputs of the |
| 23 | devices. |
| 24 | |
| 25 | Structurally, the implementation is done around two main classes: |
| 26 | nscsi_bus_devices represents the bus, and nscsi_device represents an |
| 27 | individual device. A device only communicate with the bus, and the |
| 28 | bus takes care of transparently handling the device discovery and |
| 29 | communication. In addition the nscsi_full_device class proposes a |
| 30 | scsi device with the scsi protocol implemented making building generic |
| 31 | scsi devices like harddrives or cdrom readers easier. |
| 32 | |
| 33 | |
| 34 | 3. Plugging in a scsi bus in a driver |
| 35 | |
| 36 | The nscsi subsystem leverages the slot interfaces and the device |
| 37 | naming to allow for a configurable yet simple bus implementation. |
| 38 | |
| 39 | First you need to create a list of acceptable devices to plug on the |
| 40 | bus. This usually comprises of cdrom, harddisk and the controller |
| 41 | chip. For instance: |
| 42 | |
| 43 | static SLOT_INTERFACE_START( next_scsi_devices ) |
| 44 | SLOT_INTERFACE("cdrom", NSCSI_CDROM) |
| 45 | SLOT_INTERFACE("harddisk", NSCSI_HARDDISK) |
| 46 | SLOT_INTERFACE_INTERNAL("ncr5390", NCR5390) |
| 47 | SLOT_INTERFACE_END |
| 48 | |
| 49 | The _INTERNAL interface indicates a device that is not |
| 50 | user-selectable, which is useful for the controller. |
| 51 | |
| 52 | Then in the machine config (or in a fragment config) you need to first |
| 53 | add the bus, and then the (potential) devices as subdevices of the bus |
| 54 | with the scsi id as the name. For instance you can use: |
| 55 | |
| 56 | MCFG_NSCSI_BUS_ADD("scsibus") |
| 57 | MCFG_NSCSI_ADD("scsibus:0", next_scsi_devices, "cdrom", 0, 0, 0, false) |
| 58 | MCFG_NSCSI_ADD("scsibus:1", next_scsi_devices, "harddisk", 0, 0, 0, false) |
| 59 | MCFG_NSCSI_ADD("scsibus:2", next_scsi_devices, 0, 0, 0, 0, false) |
| 60 | MCFG_NSCSI_ADD("scsibus:3", next_scsi_devices, 0, 0, 0, 0, false) |
| 61 | MCFG_NSCSI_ADD("scsibus:4", next_scsi_devices, 0, 0, 0, 0, false) |
| 62 | MCFG_NSCSI_ADD("scsibus:5", next_scsi_devices, 0, 0, 0, 0, false) |
| 63 | MCFG_NSCSI_ADD("scsibus:6", next_scsi_devices, 0, 0, 0, 0, false) |
| 64 | MCFG_NSCSI_ADD("scsibus:7", next_scsi_devices, "ncr5390", 0, &next_ncr5390_interface, 10000000, true) |
| 65 | |
| 66 | That configuration puts as default a cdrom reader on scsi id 0 and a |
| 67 | hard drive on scsi id 1, and forces the controller on id 7. The |
| 68 | parameters of add are: |
| 69 | - device tag, comprised of bus-tag:scsi-id |
| 70 | - the list of acceptable devices |
| 71 | - the device name as per the list, if one is to be there by default |
| 72 | - the device input config, if any (and there usually isn't one) |
| 73 | - the device configuration structure, usually for the controller only |
| 74 | - the frequency, usually for the controller only |
| 75 | - "false" for a user-modifyable slot, "true" for a fixed slot |
| 76 | |
| 77 | The full device name, for mapping purposes, will be |
| 78 | bus-tag:scsi-id:device-type, i.e. "scsibus:7:ncr5390" for our |
| 79 | controller here. |
| 80 | |
| 81 | |
| 82 | 4. Creating a new scsi device using nscsi_device |
| 83 | |
| 84 | The base class "nscsi_device" is to be used for down-to-the-metal |
| 85 | devices, i.e. scsi controller chips. The class provides three |
| 86 | variables and one method. The first variable, scsi_bus, is a pointer |
| 87 | to the nscsi_bus_device. The second, scsi_refid, is an opaque |
| 88 | reference to pass to the bus on some operations. Finally, scsi_id |
| 89 | gives the scsi id as per the device tag. It's written once at startup |
| 90 | and never written or read afterwards, the device can do whatever it |
| 91 | wants with the value or the variable. |
| 92 | |
| 93 | The virtual method scsi_ctrl_changed is called when watched-for |
| 94 | control lines change. Which lines are watched is defined through the |
| 95 | bus. |
| 96 | |
| 97 | The bus proposes five methods to access the lines. The read methods |
| 98 | are ctrl_r() and data_r(). The meaning of the control bits are |
| 99 | defined in the S_* enum of nscsi_device. The bottom three bits (INP, |
| 100 | CTL and MSG) are setup so that masking with 7 (S_PHASE_MASK) gives the |
| 101 | traditional numbers for the phases, which are also available with the |
| 102 | S_PHASE_* enum. |
| 103 | |
| 104 | Writing the data lines is done with data_w(scsi_refid, value). |
| 105 | Writing the control lines is done with ctrl_w(scsi_refid, value, |
| 106 | mask-of-lines-to-change). To change all control lines in one call use |
| 107 | S_ALL as the mask. |
| 108 | |
| 109 | Of course, what is read is the logical-or of all of what is driven by |
| 110 | all devices. |
| 111 | |
| 112 | Finally, the method ctrl_wait_w(scsi_id, value, |
| 113 | mask-of-wait-lines-to-change) allows to select which control lines are |
| 114 | watched. The watch mask is per-device, and the device method |
| 115 | scsi_ctrl_changed is called whenever a control line in the mask |
| 116 | changes due to an action of another device (not itself, to avoid an |
| 117 | annoying and somewhat useless recursion). |
| 118 | |
| 119 | Implementing the controller is then just a matter of following the |
| 120 | state machines descriptions, at least if they're available. The only |
| 121 | part often not described is the arbitration/selection, which is |
| 122 | documented in the scsi standard though. For an initiator (which is |
| 123 | what the controller essentially always is), it goes like this: |
| 124 | - wait for the bus to be idle |
| 125 | - assert the data line which number is your scsi_id (1 << scsi_id) |
| 126 | - assert the busy line |
| 127 | - wait the arbitration time |
| 128 | - check that the of the active data lines the one with the highest number is yours |
| 129 | - if no, the arbitration was lost, stop driving anything and restart at the beginning |
| 130 | - assert the select line (at that point, the bus is yours) |
| 131 | - wait a short while |
| 132 | - keep your data line asserted, assert the data line which number is the scsi id of the target |
| 133 | - wait a short while |
| 134 | - assert the atn line if needed, deassert busy |
| 135 | - wait for busy to be asserted or timeout |
| 136 | - timeout means nobody is answering at that id, deassert everything and stop |
| 137 | - wait a short while for deskewing |
| 138 | - deassert the data bus and the select line |
| 139 | - wait a short while |
| 140 | |
| 141 | and then you're done, you're connected with the target until the |
| 142 | target deasserts the busy line, either because you asked it to or just |
| 143 | to annoy you. The deassert is called a disconnect. |
| 144 | |
| 145 | The ncr5390 is an example of how to use a two-level state machine to |
| 146 | handle all the events. |
| 147 | |
| 148 | |
| 149 | 5. Creating a new scsi device using nscsi_full_device |
| 150 | |
| 151 | The base class "nscsi_full_device" is used to create HLE-d scsi |
| 152 | devices intended for generic uses, like hard drives, cdroms, perhaps |
| 153 | scanners, etc. The class provides the scsi protocol handling, leaving |
| 154 | only the command handling and (optionally) the message handling to the |
| 155 | implementation. |
| 156 | |
| 157 | The class currently only support target devices. |
| 158 | |
| 159 | The first method to implement is scsi_command(). That method is |
| 160 | called when a command has fully arrived. The command is available in |
| 161 | scsi_cmdbuf[], and its length is in scsi_cmdsize (but the length is |
| 162 | generally useless, the command first byte giving it). The 4096-bytes |
| 163 | scsi_cmdbuf array is then freely modifiable. |
| 164 | |
| 165 | In scsi_command(), the device can either handle the command or pass it |
| 166 | up with nscsi_full_device::scsi_command(). |
| 167 | |
| 168 | To handle the command, a number of methods are available: |
| 169 | |
| 170 | - get_lun(lua-set-in-command) will give you the lun to work on (the |
| 171 | in-command one can be overriden by a message-level one). |
| 172 | |
| 173 | - bad_lun() replies to the host that the specific lun is unsupported. |
| 174 | |
| 175 | - scsi_data_in(buffer-id, size) sends size bytes from buffer buffer-id |
| 176 | |
| 177 | - scsi_data_out(buffer-id, size) recieves size bytes into buffer buffer-id |
| 178 | |
| 179 | - scsi_status_complete(status) ends the command with a given status. |
| 180 | |
| 181 | - sense(deferred, key) prepares the sense buffer for a subsequent |
| 182 | request-sense command, which is useful when returning a |
| 183 | check-condition status. |
| 184 | |
| 185 | The scsi_data_* and scsi_status_complete commands are queued, the |
| 186 | command handler should call them all without waiting. |
| 187 | |
| 188 | buffer-id identifies a buffer. 0, aka SBUF_MAIN, targets the |
| 189 | scsi_cmdbuf buffer. Other acceptable values are 2 or more. 2+ ids |
| 190 | are handled through the scsi_get_data method for read and |
| 191 | scsi_put_data for write. |
| 192 | |
| 193 | UINT8 device::scsi_get_data(int id, int pos) must return byte pos of |
| 194 | buffer id, upcalling in nscsi_full_device for id < 2. |
| 195 | |
| 196 | void device::scsi_put_data(int id, int pos, UINT8 data) must write |
| 197 | byte pos in buffer id, upcalling in nscsi_full_device for id < 2. |
| 198 | |
| 199 | scsi_get_data and scsi_put_data should do the external reads/writes |
| 200 | when needed. |
| 201 | |
| 202 | The device can also override scsi_message to handle scsi messages |
| 203 | other than the ones generically handled, and it can also override some |
| 204 | of the timings (but a lot of them aren't used, beware). |
| 205 | |
| 206 | A number of enums are defined to make things easier. The SS_* enum |
| 207 | gives status returns (with SS_GOOD for all's well). The SC_* enum |
| 208 | gives the scsi commands. The SM_* enum gives the scsi messages, with |
| 209 | the exception of identify (which is 80-ff, doesn't really fit in an |
| 210 | enum). |
| 211 | |
| 212 | |
| 213 | 6. What's missing |
| 214 | 6.1. What's missing in scsi_full_device |
| 215 | |
| 216 | Initiator support - we have no initiator device to HLE at that point. |
| 217 | |
| 218 | Delays - a scsi_delay command would help giving more realistic timings |
| 219 | to the cdrom reader in particular. |
| 220 | |
| 221 | Disconnected operation - would first require delays and in addition an |
| 222 | emulated OS that can handle it. |
| 223 | |
| 224 | 16-bits wide operation - needs an OS and an initiator that can handle |
| 225 | it. |
| 226 | |
| 227 | |
| 228 | 6.2. What's missing in the ncr5390 (and probably future other controllers) |
| 229 | |
| 230 | Bus free detection. Right now the bus is considered free if the |
| 231 | controllers isn't using it, which is true. This may change once |
| 232 | disconnected operation is in. |
| 233 | |
| 234 | Target commands, we don't emulate (vs. HLE) any target yet. |