Writing Device Drivers

Declarations and Structures

HBA drivers must include the following header files:

#include <sys/scsi/scsi.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>

To inform the system that the module depends on SCSA routines (see "SCSA HBA Interfaces" for more information), the driver binary must be generated with the following command:

% ld -r xx.o -o xx -N"misc/scsi"

The code samples are derived from a simplified isp driver for the QLogic Intelligent SCSI Peripheral device. The isp driver supports WIDE SCSI, with up to 15 target devices and 8 logical units (LUNs) per target.

Per-Command Structure

An HBA driver will usually need to define a structure to maintain state for each command submitted by a target driver. The layout of this per-command structure is entirely up to the device driver writer and needs to reflect the capabilities and features of the hardware and the software algorithms used in the driver.

The following structure is an example of a per-command structure. The remaining code fragments of this chapter use this structure to illustrate the HBA interfaces.

struct isp_cmd {
     struct isp_request            cmd_isp_request;
     struct isp_response           cmd_isp_response;
     struct scsi_pkt               *cmd_pkt;
     struct isp_cmd                *cmd_forw;
     uint32_t                      cmd_dmacount;
     ddi_dma_handle_t              cmd_dmahandle;
     uint_t                        cmd_cookie;
     uint_t                        cmd_ncookies;
     uint_t                        cmd_cookiecnt;
     uint_t                        cmd_nwin;
     uint_t                        cmd_curwin;
     off_t                         cmd_dma_offset;
     uint_t                        cmd_dma_len;
     ddi_dma_cookie_t              cmd_dmacookies[ISP_NDATASEGS];
     u_int                         cmd_flags;
     u_short                       cmd_slot;
     u_int                         cmd_cdblen;
     u_int                         cmd_scblen;
 };