7.      What happens on the SCSI bus ?


7.1.    Basics

        Principally, the SCSI bus is sort of a "state machine".
        This means, it has a number of "states" of which at any time
        exactly one is active.
        For maintaining this behaviour, several states (phases) are
        defined, along with a lot of timing parameters that are used
        in the state switching process.

        The basic bus phases are described in 7.2. and following, but
        for keeping it simple, i omit the exact timings and signal
        dependencies.
        Whoever wants to have the exact protocol specifications, should
        look in the SCSI 2 spec. Text files are on the SCSI BBS,
        paper copies are available via Global Engineering.
        Their Address is in appendix B.


7.2.    Bus phases

        The SCSI bus has eight possible states called phases:

        a)    BUS FREE phase
        b)    ARBITRATION phase
        c)    SELECTION phase
        d)    RESELECTION phase
        e)    COMMAND phase         
        f)    DATA phase               These phases are called
        g)    STATUS phase             "information transfer phases"
        h)    MESSAGE phase         

        The COMMAND, DATA, STATUS, and MESSAGE phases are commonly
        called the information transfer phases because they are all
        used to transfer data or control information over the data bus.

        Especially the SELECTION and RESELECTION phases have time-out
        procedures to ensure that the bus can't lock up, if a target
        doesn't answer.

7.2.1.  BUS FREE phase

        The BUS FREE phase indicates that actually no I/O process is
        running and the SCSI bus is available for a connection.
        It is the basic state of the bus for every transfer.

7.2.2.  ARBITRATION phase

        The ARBITRATION phase allows all attached SCSI devices to tell
        "i need the bus" and eventually gain control over the SCSI bus
        so that it can initiate or resume an I/O process.

        Please see also 7.3. Arbitration.


7.2.3.  SELECTION phase

        In the SELECTION phase, the initiator (the arbitration winner)
        selects a target for his pending operation. When this target
        selection has happened, the target asserts the REQ signal to
        enter an information transfer phase.

7.2.4.  RESELECTION phase

        The RESELECTION phase is an optional phase that is needed in
        case of an uncompleted operation. For example, if a target
        device disconnected (means allowing a BUS FREE phase by
        releasing the BSY and SEL signals), the RESELECTION process
        allows the target to reconnect to the initiator of the
        suspended operation.

        To avoid confusion, it is neccessary to keep in mind that in a
        RESELECTION phase, the _target_ of a former operation acts
        actively to get a connection to the initiator.

7.2.5.  Information transfer phases
        (COMMAND, DATA, STATUS and MESSAGE phase)

        In these phases the actual data exchange between the initiator
        and the target happens. Three bus signals (C/D, I/O, and MSG)
        are used to distinguish between the different information
        transfer phases and directions. These signals are controlled
        by the target device, so it has control over the changes between
        these information transfer phases.
        In these phases, REQ/ACK handshake procedures are used for each
        byte of information.
        REQ/ACK handshake basically means, that the target asserts the
        REQ signal to REQuest a byte of information, then the initiator
        sets the data bus and sets the ACK signal to ACKnowledge the
        transfer. The target reads the data bus, then releases the REQ
        signal to allow the initiator to also release the ACK signal.
        Then the next byte can be transferred with the same REQ/ACK
        procedure.

        The COMMAND phase allows the target to request command
        information from the initiator.

        DATA PHASE really means two phases, DATA IN and DATA OUT,
        in which the target requests to send data to or from the
        initiator.

        The STATUS phase allows the target to request that status
        information be sent from the target to the initiator.

        MESSAGE phase also can be a MESSAGE IN or a MESSAGE OUT phase.
        In the MESSAGE phase, the target can request a message to or
        from the initiator. A message can be either a single byte or a
        multiple byte message, but the whole message must be contained
        in one message phase, that means, without a change on the C/D,
        I/O, and MSG signals.


7.2.6.  SCSI bus conditions

        Additional to the standard phases, there are two SCSI bus
        conditions; the ATTENTION and the RESET condition.

        In the attention condition, the initiator can inform a target
        that he initiator has a message ready. The target then can get
        this message by performing a MESSAGE OUT phase.

        An attention condition is issued by asserting the ATN signal;
        this can happen in bus any state except during the ARBITRATION
        or BUS FREE phases.

        The RESET condition is used to immediately clear all SCSI
        devices from the bus. The RESET condition has absolute priority
        over all other phases and conditions. Any SCSI device can create
        the reset condition by asserting the RST signal.
        On RESET, all SCSI devices release all SCSI bus signals except
        RST, so that a BUS FREE phase follows the reset condition.

7.2.7.  Phase sequence

        SCSI bus phases normally follow a prescribed sequence, but this
        can change any time. Bus phases can happen in _all_ possible
        sequences, without any rules.
        Normally, the Phase sequence on the bus is:

          - BUS FREE phase
          - ARBITRATION
          - SELECTION or RESELECTION
          - one or more of the information transfer phases
            (COMMAND, DATA, STATUS, MESSAGE)
            The final information transfer phase is normally the
            MESSAGE IN phase where a DISCONNECT or COMMAND COMPLETE
            message is transferred, followed by the next
          - BUS FREE phase

        The reset condition can abort any phase and is always followed
        by the BUS FREE phase. Also any other phase can be followed by
        the BUS FREE phase, but in most cases, this happens only due to
        an error.


7.3.    Arbitration

        SCSI's arbitration scheme includes a priority depending on
        IDs. After each BUS FREE - state, every SCSI device that wants
        to use the SCSI bus can try to get it by setting BSY and its
        ID line. Then, after an arbitration delay, these devices check
        the data bus. If a higher priority (=ID) bit than the own one
        is set active, the device has "lost" the arbitration. So, on a
        SCSI bus with - say - six devices, all heavily used, the
        devices with low IDs - usually hard disks - could show bad
        performance from an effect called "starvation" - devices with
        higher priorities "block" the bus.

        This is the main reason why hard disk IDs on workstations are
        normally set "top down" - so the system and swap disk is
        guaranteed to have highest priority.

        In real life situations, you should not encounter a performance
        difference between the various IDs, and most PC SCSI adapters
        need the standard hard disks on ID's 0 and 1, so you haven't a
        choice anyway. But keep this in mind if you have strange
        performance "holes".


7.4.    Synchronous/Asynchronous Transfers

        Asynchronous transfer is the standard case, using the normal
        REQ/ACK handshake. Synchronous transfer is a specific sort of
        overlapping some REQ/ACK cycles. In a "Synchronous Negotiation"
        the initiator and the target agree on a maximal overlap count,
        called the "REQ/ACK offset". So, with an overlap of 16, the
        target can send up to 16 REQ pulses before it must wait for an
        ACK and the first data byte. So, the devices can establish a
        pacing mechanism to transfer following bytes of information much
        faster than in asynchronous mode, for various bus settle delays
        doesn't occur here or are overlapped, thus saving time and
        reducing overhead.


7.5.    FAST/WIDE Transfer Negotiation

        Usually, the transfer type negotiation mentioned in 2.7. and
        2.8. is done via a Synchronous Data Transfer Request (SDTR).
        An SDTR is a message containing the minimal transfer time the
        device can handle, and the REQ offset, the maximal number of
        REQ pulses (means data requests) the target can handle before
        the first request must be acknowledged. The offset count has
        two special cases: 00h means asynchronous mode only, FFh
        means not 255, but unlimited REQ/ACK offset.
        Let's call it "question" and "answer". Now, the device that
        starts the SDTR "raises the question" by sendind its values;
        the responding device checks them, and if it can handle the
        same or better values, "answers 'yes' " by reposting the same
        data in its own SDTR message. If it can't handle one of the
        requested values, it "answers" by posting its own timing
        capabilities, leaving unchanged the values it can handle.
        If one of the devices posts an offset of 00h, or if an agree-
        ment can't be negotiated, both devices use only asynchronous
        transfers between them.
        If this negotiation would occur on each selection, it would
        have catastrophic effects on performance. To avoid this
        problem, usually the Synchronous Transfer Negotiation
        happens on three major occasions:

        - after a power cycle (of the initiator)
        - after a hard reset condition, and
        - after a BUS DEVICE RESET.

        Additionally, it makes sense to re-negotiate on every INQUIRY
        and REQUEST SENSE command, for both usually are issued only to
        check devices for presence or error conditions - for example,
        after a power cycle on an external device the host adapter
        can't sense, for it is power cycled independently from the
        main system containing the host adapter.

        The same happens to negotiate Wide Data Transfer. A WDTR
        message contains the transfer width "exponent" - 00h for
        8 bit, 01h for 16 bit, and - you guess it <g> - 02h for
        32 bit data transfer width. The mechanism is absolutely
        identical to the SDTR, only the message is different.

        If a device is able to use both Wide and Synchronous Data
        Transfers, - likely with every actual device, and in fact
        true for _all_ Wide SCSI devices i know - it is recommended
        that Wide Transfer is negotiated before the Synchronous
        Transfer Negotiation. Also, if you're programming around the
        Wide Data Requests, expect that a device that re-negotiates
        Wide Data Transfers will reset the Synchronous Transfer
        agreement to Asynchronous Mode only, until you re-negotiate
        Synchronous Transfer Mode also.

