Previous 15994 Revisions Next

r15755 Sunday 5th August, 2012 at 09:06:52 UTC by Olivier Galibert
nscsi: documentation [O. Galibert]
[docs]nscsi.txt*

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

Previous 15994 Revisions Next


© 1998-2018 The MESS Team