Writing Device Drivers

Declarations and Structures

HBA drivers must include the following header files, along with a declaration of dependency upon the scsi module:

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

 	char _depends_on[] = "misc/scsi";

This declaration informs the system that the module depends on SCSA routines (see "SCSA HBA Interfaces" for more information). This construct is used only for SCSI drivers and should not be used elsewhere.

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;