Specifications
Multithreading a Device Drive
r
11-3
of some code depends upon the current state being constant, then the lock that protects
updates to the state must be held during execution of this code.
Note that words that contain more than one state are a problem. Different bits in the same
word used for distinctly different purposes must be protected by a single lock. This is
because an update of a bit is not necessarily an interlocked operation. If different synchro-
nization locks are used for the same word, there is nothing to prevent two processors from
modifying the same word but different bits at the same time. This causes one of the
updates to this word to be lost. Note that this is also a problem when two character ele-
ments that lie in the same longword are protected by different locks.
Next check the global variables in the driver. These variables must be protected because an
LWP is no longer guaranteed to have exclusive access to a global variable. Making a global
variable into a local variable corrects the problem. If this cannot be done, then a spin lock
or sleep lock must be locked whenever the variable is expected to contain a value that was
placed there.
For some device drivers, access to the hardware registers of the device must be treated as a
critical section. This is often true for registers that can be read only once.
The driver for a device such as the HSA (HVME SCSI adapter) does not need to protect its
registers. When the HSA receives an interrupt for command complete, a register points to
the request that has just been completed. This register cannot be overwritten because fur-
ther interrupts cannot be received until another command is sent to the HSA. The program
level code of this driver cannot issue another command to the HSA as long as there is cur-
rently a command executing on the HSA. The important thing to protect here is the state of
the HSA. Is there a command currently executing on the HSA? As long as this state is
maintained correctly, this hardware register cannot be overwritten.
One of the important decisions in multithreading a driver is whether to use spin locks or
sleep locks to protect structures. Spin locks are used when the holding time of the locks is
small or when the lock must be locked at interrupt level. Sleep locks must be used when
the holding times for the locks are longer. The routines associated with spin locks and
sleep locks are presented in the sections that follow.
NOTE
For performance reasons, it is strongly recommended that you
multithread your device driver. However, for compatibility pur-
poses, the kernel allows you to configure a single-threaded device
driver so that it can be used in a multiprocessor system. This is
done by setting the cpu_bind field of the Master file for the
device driver to the processor ID of the processor to which the
base level of the device driver must be bound. Also be sure that if
the devflag(D1) global variable is declared in your driver, it is
not initialized with the value D_MP.