Home | History | Annotate | Line # | Download | only in ata
      1  1.109       rin /*	$NetBSD: atavar.h,v 1.109 2021/10/05 08:01:05 rin Exp $	*/
      2    1.2    bouyer 
      3    1.2    bouyer /*
      4   1.23    bouyer  * Copyright (c) 1998, 2001 Manuel Bouyer.
      5    1.2    bouyer  *
      6    1.2    bouyer  * Redistribution and use in source and binary forms, with or without
      7    1.2    bouyer  * modification, are permitted provided that the following conditions
      8    1.2    bouyer  * are met:
      9    1.2    bouyer  * 1. Redistributions of source code must retain the above copyright
     10    1.2    bouyer  *    notice, this list of conditions and the following disclaimer.
     11    1.2    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12    1.2    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13    1.2    bouyer  *    documentation and/or other materials provided with the distribution.
     14    1.2    bouyer  *
     15   1.18    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.18    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.18    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.67     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.18    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20   1.18    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21   1.18    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22   1.18    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23   1.18    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24   1.18    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25    1.2    bouyer  */
     26    1.2    bouyer 
     27   1.31   thorpej #ifndef _DEV_ATA_ATAVAR_H_
     28   1.31   thorpej #define	_DEV_ATA_ATAVAR_H_
     29   1.31   thorpej 
     30   1.37   thorpej #include <sys/lock.h>
     31   1.37   thorpej #include <sys/queue.h>
     32   1.37   thorpej 
     33   1.73     itohy #include <dev/ata/ataconf.h>
     34   1.73     itohy 
     35   1.59   thorpej /* XXX For scsipi_adapter and scsipi_channel. */
     36   1.59   thorpej #include <dev/scsipi/scsipi_all.h>
     37   1.59   thorpej #include <dev/scsipi/atapiconf.h>
     38   1.59   thorpej 
     39   1.59   thorpej /*
     40   1.93  jdolecek  * Parameters/state needed by the controller to perform an ATA bio.
     41   1.93  jdolecek  */
     42   1.93  jdolecek struct ata_bio {
     43   1.93  jdolecek 	volatile uint16_t flags;/* cmd flags */
     44   1.93  jdolecek /* 			0x0001	free, was ATA_NOSLEEP */
     45   1.93  jdolecek #define	ATA_POLL	0x0002	/* poll for completion */
     46   1.93  jdolecek #define	ATA_ITSDONE	0x0004	/* the transfer is as done as it gets */
     47   1.93  jdolecek #define	ATA_SINGLE	0x0008	/* transfer must be done in singlesector mode */
     48   1.93  jdolecek #define	ATA_LBA		0x0010	/* transfer uses LBA addressing */
     49   1.93  jdolecek #define	ATA_READ	0x0020	/* transfer is a read (otherwise a write) */
     50   1.93  jdolecek #define	ATA_CORR	0x0040	/* transfer had a corrected error */
     51   1.93  jdolecek #define	ATA_LBA48	0x0080	/* transfer uses 48-bit LBA addressing */
     52   1.93  jdolecek #define	ATA_FUA		0x0100	/* transfer uses FUA */
     53   1.93  jdolecek #define	ATA_PRIO_HIGH	0x0200	/* transfer has high priority */
     54   1.93  jdolecek 	daddr_t		blkno;	/* block addr */
     55   1.93  jdolecek 	daddr_t		blkdone;/* number of blks transferred */
     56   1.93  jdolecek 	daddr_t		nblks;	/* number of block currently transferring */
     57   1.93  jdolecek 	int		nbytes;	/* number of bytes currently transferring */
     58   1.93  jdolecek 	long		bcount;	/* total number of bytes */
     59   1.93  jdolecek 	char		*databuf;/* data buffer address */
     60   1.93  jdolecek 	volatile int	error;
     61   1.93  jdolecek #define	NOERROR 	0	/* There was no error (r_error invalid) */
     62   1.93  jdolecek #define	ERROR		1	/* check r_error */
     63   1.93  jdolecek #define	ERR_DF		2	/* Drive fault */
     64   1.93  jdolecek #define	ERR_DMA		3	/* DMA error */
     65   1.93  jdolecek #define	TIMEOUT		4	/* device timed out */
     66   1.93  jdolecek #define	ERR_NODEV	5	/* device has been gone */
     67   1.93  jdolecek #define ERR_RESET	6	/* command was terminated by channel reset */
     68   1.93  jdolecek #define REQUEUE		7	/* different xfer failed, requeue command */
     69   1.93  jdolecek 	uint8_t		r_error;/* copy of error register */
     70   1.93  jdolecek 	struct buf	*bp;
     71   1.93  jdolecek };
     72   1.93  jdolecek 
     73   1.93  jdolecek /*
     74   1.93  jdolecek  * ATA/ATAPI commands description
     75   1.93  jdolecek  *
     76   1.93  jdolecek  * This structure defines the interface between the ATA/ATAPI device driver
     77   1.93  jdolecek  * and the controller for short commands. It contains the command's parameter,
     78   1.93  jdolecek  * the length of data to read/write (if any), and a function to call upon
     79   1.93  jdolecek  * completion.
     80   1.93  jdolecek  * If no sleep is allowed, the driver can poll for command completion.
     81   1.93  jdolecek  * Once the command completed, if the error registered is valid, the flag
     82   1.93  jdolecek  * AT_ERROR is set and the error register value is copied to r_error .
     83   1.93  jdolecek  * A separate interface is needed for read/write or ATAPI packet commands
     84   1.93  jdolecek  * (which need multiple interrupts per commands).
     85   1.93  jdolecek  */
     86   1.93  jdolecek struct ata_command {
     87   1.93  jdolecek 	/* ATA parameters */
     88   1.93  jdolecek 	uint64_t r_lba;		/* before & after */
     89   1.93  jdolecek 	uint16_t r_count;	/* before & after */
     90   1.93  jdolecek 	union {
     91   1.93  jdolecek 		uint16_t r_features; /* before */
     92   1.93  jdolecek 		uint8_t r_error; /* after */
     93   1.93  jdolecek 	};
     94   1.93  jdolecek 	union {
     95   1.93  jdolecek 		uint8_t r_command; /* before */
     96   1.93  jdolecek 		uint8_t r_status; /* after */
     97   1.93  jdolecek 	};
     98   1.93  jdolecek 	uint8_t r_device;	/* before & after */
     99   1.93  jdolecek 
    100   1.93  jdolecek 	uint8_t r_st_bmask;	/* status register mask to wait for before
    101   1.93  jdolecek 				   command */
    102   1.93  jdolecek 	uint8_t r_st_pmask;	/* status register mask to wait for after
    103   1.93  jdolecek 				   command */
    104   1.93  jdolecek 	volatile uint16_t flags;
    105   1.93  jdolecek 
    106  1.102  christos #define AT_READ     	0x0001 /* There is data to read */
    107  1.102  christos #define AT_WRITE    	0x0002 /* There is data to write (excl. with AT_READ) */
    108  1.102  christos #define AT_WAIT     	0x0008 /* wait in controller for command completion */
    109  1.102  christos #define AT_POLL     	0x0010 /* poll for command completion (no interrupts) */
    110  1.102  christos #define AT_DONE     	0x0020 /* command is done */
    111  1.102  christos #define AT_XFDONE   	0x0040 /* data xfer is done */
    112  1.102  christos #define AT_ERROR    	0x0080 /* command is done with error */
    113  1.102  christos #define AT_TIMEOU   	0x0100 /* command timed out */
    114  1.102  christos #define AT_DF       	0x0200 /* Drive fault */
    115  1.102  christos #define AT_RESET    	0x0400 /* command terminated by channel reset */
    116  1.102  christos #define AT_GONE     	0x0800 /* command terminated because device is gone */
    117  1.102  christos #define AT_READREG  	0x1000 /* Read registers on completion */
    118  1.102  christos #define AT_LBA      	0x2000 /* LBA28 */
    119  1.102  christos #define AT_LBA48   	0x4000 /* LBA48 */
    120   1.93  jdolecek 
    121   1.93  jdolecek 	int timeout;		/* timeout (in ms) */
    122   1.93  jdolecek 	void *data;		/* Data buffer address */
    123   1.93  jdolecek 	int bcount;		/* number of bytes to transfer */
    124   1.93  jdolecek };
    125   1.93  jdolecek 
    126   1.93  jdolecek /* Forward declaration for ata_xfer */
    127   1.93  jdolecek struct scsipi_xfer;
    128  1.100  jdolecek struct ata_xfer_ops;
    129   1.93  jdolecek 
    130   1.93  jdolecek /*
    131   1.39   thorpej  * Description of a command to be handled by an ATA controller.  These
    132   1.39   thorpej  * commands are queued in a list.
    133   1.39   thorpej  */
    134   1.39   thorpej struct ata_xfer {
    135   1.93  jdolecek 	int8_t c_slot;			/* queue slot # */
    136   1.67     perry 
    137   1.39   thorpej 	/* Channel and drive that are to process the request. */
    138   1.59   thorpej 	struct ata_channel *c_chp;
    139   1.93  jdolecek 	uint16_t	c_drive;
    140   1.93  jdolecek 	uint16_t	c_retries;	/* number of xfer retry */
    141   1.39   thorpej 
    142   1.93  jdolecek 	volatile u_int c_flags;		/* command state flags */
    143   1.39   thorpej 	void	*c_databuf;		/* pointer to data buffer */
    144   1.39   thorpej 	int	c_bcount;		/* byte count left */
    145   1.39   thorpej 	int	c_skip;			/* bytes already transferred */
    146   1.93  jdolecek #define ATACH_ERR_ST(error, status)	((error) << 8 | (status))
    147   1.93  jdolecek #define ATACH_ERR(val)			(((val) >> 8) & 0xff)
    148   1.93  jdolecek #define ATACH_ST(val)			(((val) >> 0) & 0xff)
    149   1.93  jdolecek 
    150   1.93  jdolecek 	union {
    151   1.93  jdolecek 		struct ata_bio	c_bio;		/* ATA transfer */
    152   1.93  jdolecek 		struct ata_command c_ata_c;	/* ATA command */
    153  1.100  jdolecek 		struct {
    154  1.100  jdolecek 			struct scsipi_xfer *c_scsipi;	/* SCSI transfer */
    155  1.100  jdolecek 			int	c_dscpoll; /* counter for dsc polling (ATAPI) */
    156  1.100  jdolecek 			int	c_lenoff;  /* offset to c_bcount (ATAPI) */
    157  1.100  jdolecek 		} atapi;
    158   1.93  jdolecek 	} u;
    159   1.93  jdolecek #define c_bio	u.c_bio
    160   1.93  jdolecek #define c_ata_c	u.c_ata_c
    161  1.100  jdolecek #define c_atapi u.atapi
    162  1.100  jdolecek #define c_scsipi c_atapi.c_scsipi
    163   1.39   thorpej 
    164   1.39   thorpej 	/* Link on the command queue. */
    165  1.100  jdolecek 	SIMPLEQ_ENTRY(ata_xfer) c_xferchain;
    166   1.93  jdolecek 	TAILQ_ENTRY(ata_xfer) c_activechain;
    167   1.39   thorpej 
    168  1.100  jdolecek 	/* Links for error handling */
    169  1.100  jdolecek 	SLIST_ENTRY(ata_xfer) c_retrychain;
    170  1.100  jdolecek 
    171   1.39   thorpej 	/* Low-level protocol handlers. */
    172  1.100  jdolecek 	const struct ata_xfer_ops *ops;
    173  1.100  jdolecek };
    174  1.100  jdolecek 
    175  1.100  jdolecek struct ata_xfer_ops {
    176   1.93  jdolecek 	int	(*c_start)(struct ata_channel *, struct ata_xfer *);
    177   1.93  jdolecek #define ATASTART_STARTED	0	/* xfer started, waiting for intr */
    178   1.93  jdolecek #define ATASTART_TH		1	/* xfer needs to be run in thread */
    179   1.93  jdolecek #define ATASTART_POLL		2	/* xfer needs to be polled */
    180   1.93  jdolecek #define ATASTART_ABORT		3	/* error occurred, abort xfer */
    181  1.109       rin 	int	(*c_poll)(struct ata_channel *, struct ata_xfer *);
    182  1.109       rin #define	ATAPOLL_DONE		0
    183  1.109       rin #define	ATAPOLL_AGAIN		1
    184   1.93  jdolecek 	void	(*c_abort)(struct ata_channel *, struct ata_xfer *);
    185   1.59   thorpej 	int	(*c_intr)(struct ata_channel *, struct ata_xfer *, int);
    186   1.59   thorpej 	void	(*c_kill_xfer)(struct ata_channel *, struct ata_xfer *, int);
    187   1.39   thorpej };
    188   1.39   thorpej 
    189   1.65    bouyer /* flags in c_flags */
    190   1.39   thorpej #define	C_ATAPI		0x0001		/* xfer is ATAPI request */
    191   1.39   thorpej #define	C_TIMEOU	0x0002		/* xfer processing timed out */
    192   1.39   thorpej #define	C_POLL		0x0004		/* command is polled */
    193   1.39   thorpej #define	C_DMA		0x0008		/* command uses DMA */
    194   1.93  jdolecek #define C_WAIT		0x0010		/* can use kpause */
    195   1.65    bouyer #define C_WAITACT	0x0020		/* wakeup when active */
    196   1.65    bouyer #define C_FREE		0x0040		/* call ata_free_xfer() asap */
    197   1.72     itohy #define C_PIOBM		0x0080		/* command uses busmastering PIO */
    198   1.93  jdolecek #define	C_NCQ		0x0100		/* command is queued  */
    199  1.100  jdolecek #define C_SKIP_QUEUE	0x0200		/* skip xfer queue */
    200   1.93  jdolecek #define C_WAITTIMO	0x0400		/* race vs. timeout */
    201   1.93  jdolecek #define C_CHAOS		0x0800		/* forced error xfer */
    202   1.93  jdolecek #define C_RECOVERED	0x1000		/* error recovered, no need for reset */
    203  1.100  jdolecek #define C_PRIVATE_ALLOC	0x2000		/* private alloc, skip pool_put() */
    204   1.39   thorpej 
    205   1.44    bouyer /* reasons for c_kill_xfer() */
    206   1.93  jdolecek #define KILL_GONE 1		/* device is gone while xfer was active */
    207   1.93  jdolecek #define KILL_RESET 2		/* xfer was reset */
    208   1.93  jdolecek #define KILL_GONE_INACTIVE 3	/* device is gone while xfer was pending */
    209   1.93  jdolecek #define KILL_REQUEUE	4	/* xfer must be reissued to device, no err */
    210   1.93  jdolecek 
    211   1.93  jdolecek /*
    212   1.93  jdolecek  * While hw supports up to 32 tags, in practice we must never
    213   1.93  jdolecek  * allow 32 active commands, since that would signal same as
    214   1.93  jdolecek  * channel error. We use slot 32 only for error recovery if available.
    215   1.93  jdolecek  */
    216   1.93  jdolecek #define ATA_MAX_OPENINGS	32
    217   1.93  jdolecek #define ATA_REAL_OPENINGS(op)	((op) > 1 ? (op) - 1 : 1)
    218   1.44    bouyer 
    219   1.93  jdolecek #define ATA_BSIZE		512	/* Standard ATA block size (bytes) */
    220   1.93  jdolecek 
    221   1.93  jdolecek /* Per-channel queue of ata_xfers */
    222   1.93  jdolecek #ifndef ATABUS_PRIVATE
    223   1.93  jdolecek struct ata_queue;
    224   1.93  jdolecek #else
    225   1.39   thorpej struct ata_queue {
    226   1.93  jdolecek 	int8_t queue_flags;		/* flags for this queue */
    227   1.93  jdolecek #define QF_IDLE_WAIT	0x01    	/* someone wants the controller idle */
    228   1.93  jdolecek #define QF_NEED_XFER	0x02    	/* someone wants xfer */
    229   1.93  jdolecek 	int8_t queue_active; 		/* number of active transfers */
    230   1.93  jdolecek 	uint8_t queue_openings;			/* max number of active xfers */
    231  1.100  jdolecek 	SIMPLEQ_HEAD(, ata_xfer) queue_xfer; 	/* queue of pending commands */
    232   1.93  jdolecek 	int queue_freeze; 			/* freeze count for the queue */
    233   1.93  jdolecek 	kcondvar_t queue_drain;			/* c: waiting of queue drain */
    234   1.93  jdolecek 	kcondvar_t queue_idle;			/* c: waiting of queue idle */
    235   1.93  jdolecek 	TAILQ_HEAD(, ata_xfer) active_xfers; 	/* active commands */
    236   1.93  jdolecek 	uint32_t active_xfers_used;		/* mask of active commands */
    237   1.93  jdolecek 	uint32_t queue_xfers_avail;		/* available xfers mask */
    238  1.100  jdolecek 	uint32_t queue_hold;			/* slots held during recovery */
    239  1.100  jdolecek 	kcondvar_t c_active;		/* somebody actively waiting for xfer */
    240  1.100  jdolecek 	kcondvar_t c_cmd_finish;	/* somebody waiting for cmd finish */
    241   1.39   thorpej };
    242   1.93  jdolecek #endif
    243   1.39   thorpej 
    244   1.37   thorpej /* ATA bus instance state information. */
    245   1.37   thorpej struct atabus_softc {
    246   1.76      cube 	device_t sc_dev;
    247   1.59   thorpej 	struct ata_channel *sc_chan;
    248   1.45    bouyer 	int sc_flags;
    249   1.45    bouyer #define ATABUSCF_OPEN	0x01
    250   1.37   thorpej };
    251   1.37   thorpej 
    252   1.37   thorpej /*
    253   1.37   thorpej  * A queue of atabus instances, used to ensure the same bus probe order
    254   1.37   thorpej  * for a given hardware configuration at each boot.
    255   1.37   thorpej  */
    256   1.37   thorpej struct atabus_initq {
    257   1.37   thorpej 	TAILQ_ENTRY(atabus_initq) atabus_initq;
    258   1.37   thorpej 	struct atabus_softc *atabus_sc;
    259   1.37   thorpej };
    260   1.37   thorpej 
    261   1.31   thorpej /* High-level functions and structures used by both ATA and ATAPI devices */
    262   1.33   thorpej struct ataparams;
    263   1.33   thorpej 
    264    1.2    bouyer /* Datas common to drives and controller drivers */
    265    1.2    bouyer struct ata_drive_datas {
    266   1.92      matt 	uint8_t drive;		/* drive number */
    267   1.31   thorpej 	int8_t ata_vers;	/* ATA version supported */
    268   1.92      matt 	uint16_t drive_flags;	/* bitmask for drives present/absent and cap */
    269   1.93  jdolecek #define	ATA_DRIVE_CAP32		0x0001	/* 32-bit transfer capable */
    270   1.90    bouyer #define	ATA_DRIVE_DMA		0x0002
    271   1.90    bouyer #define	ATA_DRIVE_UDMA		0x0004
    272   1.90    bouyer #define	ATA_DRIVE_MODE		0x0008	/* the drive reported its mode */
    273   1.90    bouyer #define	ATA_DRIVE_RESET		0x0010	/* reset the drive state at next xfer */
    274   1.90    bouyer #define	ATA_DRIVE_WAITDRAIN	0x0020	/* device is waiting for the queue to drain */
    275   1.90    bouyer #define	ATA_DRIVE_NOSTREAM	0x0040	/* no stream methods on this drive */
    276   1.90    bouyer #define ATA_DRIVE_ATAPIDSCW	0x0080	/* needs to wait for DSC in phase_complete */
    277   1.93  jdolecek #define ATA_DRIVE_WFUA		0x0100	/* drive supports WRITE DMA FUA EXT */
    278   1.93  jdolecek #define ATA_DRIVE_NCQ		0x0200	/* drive supports NCQ feature set */
    279   1.93  jdolecek #define ATA_DRIVE_NCQ_PRIO	0x0400	/* drive supports NCQ PRIO field */
    280  1.100  jdolecek #define ATA_DRIVE_TH_RESET	0x0800	/* drive waits for thread drive reset */
    281   1.90    bouyer 
    282   1.90    bouyer 	uint8_t drive_type;
    283   1.90    bouyer #define	ATA_DRIVET_NONE		0
    284   1.90    bouyer #define	ATA_DRIVET_ATA		1
    285   1.90    bouyer #define	ATA_DRIVET_ATAPI	2
    286   1.90    bouyer #define	ATA_DRIVET_OLD		3
    287   1.90    bouyer #define	ATA_DRIVET_PM		4
    288   1.31   thorpej 
    289   1.31   thorpej 	/*
    290   1.31   thorpej 	 * Current setting of drive's PIO, DMA and UDMA modes.
    291   1.31   thorpej 	 * Is initialised by the disks drivers at attach time, and may be
    292   1.31   thorpej 	 * changed later by the controller's code if needed
    293   1.31   thorpej 	 */
    294   1.92      matt 	uint8_t PIO_mode;	/* Current setting of drive's PIO mode */
    295   1.73     itohy #if NATA_DMA
    296   1.92      matt 	uint8_t DMA_mode;	/* Current setting of drive's DMA mode */
    297   1.73     itohy #if NATA_UDMA
    298   1.92      matt 	uint8_t UDMA_mode;	/* Current setting of drive's UDMA mode */
    299   1.73     itohy #endif
    300   1.73     itohy #endif
    301   1.31   thorpej 
    302   1.31   thorpej 	/* Supported modes for this drive */
    303   1.92      matt 	uint8_t PIO_cap;	/* supported drive's PIO mode */
    304   1.73     itohy #if NATA_DMA
    305   1.92      matt 	uint8_t DMA_cap;	/* supported drive's DMA mode */
    306   1.73     itohy #if NATA_UDMA
    307   1.92      matt 	uint8_t UDMA_cap;	/* supported drive's UDMA mode */
    308   1.73     itohy #endif
    309   1.73     itohy #endif
    310   1.31   thorpej 
    311   1.31   thorpej 	/*
    312   1.31   thorpej 	 * Drive state.
    313   1.31   thorpej 	 * This is reset to 0 after a channel reset.
    314   1.31   thorpej 	 */
    315   1.92      matt 	uint8_t state;
    316   1.31   thorpej 
    317   1.16    bouyer #define RESET          0
    318   1.29    bouyer #define READY          1
    319    1.2    bouyer 
    320   1.93  jdolecek 	uint8_t drv_openings;		/* # of command tags */
    321   1.93  jdolecek 
    322   1.73     itohy #if NATA_DMA
    323   1.31   thorpej 	/* numbers of xfers and DMA errs. Used by ata_dmaerr() */
    324   1.92      matt 	uint8_t n_dmaerrs;
    325   1.92      matt 	uint32_t n_xfers;
    326   1.31   thorpej 
    327   1.31   thorpej 	/* Downgrade after NERRS_MAX errors in at most NXFER xfers */
    328   1.15    bouyer #define NERRS_MAX 4
    329   1.15    bouyer #define NXFER 4000
    330   1.73     itohy #endif
    331   1.35   thorpej 
    332   1.35   thorpej 	/* Callbacks into the drive's driver. */
    333   1.93  jdolecek 	void	(*drv_done)(device_t, struct ata_xfer *); /* xfer is done */
    334   1.93  jdolecek 
    335   1.93  jdolecek 	device_t drv_softc;		/* ATA drives softc, if any */
    336   1.93  jdolecek 	struct ata_channel *chnl_softc;	/* channel softc */
    337   1.93  jdolecek 
    338   1.93  jdolecek 	/* Context used for I/O */
    339   1.93  jdolecek 	struct disklabel *lp;	/* pointer to drive's label info */
    340   1.93  jdolecek 	uint8_t		multi;	/* # of blocks to transfer in multi-mode */
    341   1.93  jdolecek 	daddr_t	badsect[127];	/* 126 plus trailing -1 marker */
    342    1.2    bouyer };
    343    1.2    bouyer 
    344    1.7    bouyer /* User config flags that force (or disable) the use of a mode */
    345    1.7    bouyer #define ATA_CONFIG_PIO_MODES	0x0007
    346    1.7    bouyer #define ATA_CONFIG_PIO_SET	0x0008
    347    1.7    bouyer #define ATA_CONFIG_PIO_OFF	0
    348    1.7    bouyer #define ATA_CONFIG_DMA_MODES	0x0070
    349    1.7    bouyer #define ATA_CONFIG_DMA_SET	0x0080
    350    1.7    bouyer #define ATA_CONFIG_DMA_DISABLE	0x0070
    351    1.7    bouyer #define ATA_CONFIG_DMA_OFF	4
    352    1.7    bouyer #define ATA_CONFIG_UDMA_MODES	0x0700
    353    1.7    bouyer #define ATA_CONFIG_UDMA_SET	0x0800
    354    1.7    bouyer #define ATA_CONFIG_UDMA_DISABLE	0x0700
    355    1.7    bouyer #define ATA_CONFIG_UDMA_OFF	8
    356   1.32   thorpej 
    357   1.32   thorpej /*
    358   1.33   thorpej  * ata_bustype.  The first field must be compatible with scsipi_bustype,
    359   1.33   thorpej  * as it's used for autoconfig by both ata and atapi drivers.
    360   1.33   thorpej  */
    361   1.33   thorpej struct ata_bustype {
    362   1.33   thorpej 	int	bustype_type;	/* symbolic name of type */
    363  1.105  jdolecek 	void	(*ata_bio)(struct ata_drive_datas *, struct ata_xfer *);
    364   1.90    bouyer 	void	(*ata_reset_drive)(struct ata_drive_datas *, int, uint32_t *);
    365   1.64   thorpej 	void	(*ata_reset_channel)(struct ata_channel *, int);
    366  1.105  jdolecek 	void	(*ata_exec_command)(struct ata_drive_datas *,
    367   1.93  jdolecek 				    struct ata_xfer *);
    368   1.33   thorpej 
    369   1.50   thorpej #define	ATACMD_COMPLETE		0x01
    370   1.50   thorpej #define	ATACMD_QUEUED		0x02
    371   1.50   thorpej #define	ATACMD_TRY_AGAIN	0x03
    372   1.33   thorpej 
    373   1.92      matt 	int	(*ata_get_params)(struct ata_drive_datas *, uint8_t,
    374   1.33   thorpej 				  struct ataparams *);
    375   1.33   thorpej 	int	(*ata_addref)(struct ata_drive_datas *);
    376   1.33   thorpej 	void	(*ata_delref)(struct ata_drive_datas *);
    377   1.33   thorpej 	void	(*ata_killpending)(struct ata_drive_datas *);
    378  1.100  jdolecek 	void	(*ata_recovery)(struct ata_channel *, int, uint32_t);
    379   1.33   thorpej };
    380   1.33   thorpej 
    381   1.33   thorpej /* bustype_type */	/* XXX XXX XXX */
    382   1.33   thorpej /* #define SCSIPI_BUSTYPE_SCSI	0 */
    383   1.33   thorpej /* #define SCSIPI_BUSTYPE_ATAPI	1 */
    384   1.33   thorpej #define	SCSIPI_BUSTYPE_ATA	2
    385   1.34   thorpej 
    386   1.34   thorpej /*
    387   1.34   thorpej  * Describe an ATA device.  Has to be compatible with scsipi_channel, so
    388   1.34   thorpej  * start with a pointer to ata_bustype.
    389   1.34   thorpej  */
    390   1.34   thorpej struct ata_device {
    391   1.34   thorpej 	const struct ata_bustype *adev_bustype;
    392   1.34   thorpej 	int adev_channel;
    393   1.34   thorpej 	struct ata_drive_datas *adev_drv_data;
    394   1.34   thorpej };
    395   1.26     soren 
    396   1.59   thorpej /*
    397   1.59   thorpej  * Per-channel data
    398   1.59   thorpej  */
    399   1.59   thorpej struct ata_channel {
    400   1.59   thorpej 	int ch_channel;			/* location */
    401   1.60   thorpej 	struct atac_softc *ch_atac;	/* ATA controller softc */
    402   1.93  jdolecek 	kmutex_t ch_lock;		/* channel lock - queue */
    403   1.59   thorpej 
    404   1.59   thorpej 	/* Our state */
    405   1.59   thorpej 	volatile int ch_flags;
    406   1.59   thorpej #define ATACH_SHUTDOWN 0x02	/* channel is shutting down */
    407   1.95  jdolecek #define ATACH_IRQ_WAIT 0x10	/* controller is waiting for irq */
    408   1.59   thorpej #define ATACH_DMA_WAIT 0x20	/* controller is waiting for DMA */
    409   1.72     itohy #define ATACH_PIOBM_WAIT 0x40	/* controller is waiting for busmastering PIO */
    410   1.59   thorpej #define	ATACH_DISABLED 0x80	/* channel is disabled */
    411   1.59   thorpej #define ATACH_TH_RESET 0x200	/* someone ask the thread to reset */
    412   1.82  jakllsch #define ATACH_TH_RESCAN 0x400	/* rescan requested */
    413   1.93  jdolecek #define ATACH_NCQ	0x800	/* channel executing NCQ commands */
    414  1.101  jdolecek #define ATACH_DMA_BEFORE_CMD	0x01000	/* start DMA first */
    415  1.101  jdolecek #define ATACH_TH_DRIVE_RESET	0x02000	/* asked thread to drive(s) reset */
    416  1.101  jdolecek #define ATACH_RECOVERING	0x04000	/* channel is recovering */
    417  1.101  jdolecek #define ATACH_TH_RECOVERY	0x08000	/* asked thread to run recovery */
    418  1.101  jdolecek #define ATACH_DETACHED		0x10000 /* channel was destroyed */
    419   1.59   thorpej 
    420  1.100  jdolecek #define ATACH_NODRIVE	0xff	/* no drive selected for reset */
    421  1.100  jdolecek 
    422  1.100  jdolecek 	/* for the timeout callout */
    423  1.100  jdolecek 	struct callout c_timo_callout;	/* timeout callout handle */
    424   1.59   thorpej 
    425   1.59   thorpej 	/* per-drive info */
    426   1.90    bouyer 	int ch_ndrives; /* number of entries in ch_drive[] */
    427   1.90    bouyer 	struct ata_drive_datas *ch_drive; /* array of ata_drive_datas */
    428   1.59   thorpej 
    429   1.79    cegger 	device_t atabus;	/* self */
    430   1.59   thorpej 
    431   1.59   thorpej 	/* ATAPI children */
    432   1.79    cegger 	device_t atapibus;
    433   1.59   thorpej 	struct scsipi_channel ch_atapi_channel;
    434   1.59   thorpej 
    435   1.59   thorpej 	/*
    436   1.59   thorpej 	 * Channel queues.  May be the same for all channels, if hw
    437   1.59   thorpej 	 * channels are not independent.
    438   1.59   thorpej 	 */
    439   1.59   thorpej 	struct ata_queue *ch_queue;
    440   1.59   thorpej 
    441  1.107   thorpej 	/* The channel kernel thread */
    442  1.107   thorpej 	struct lwp *ch_thread;
    443  1.107   thorpej 	kcondvar_t ch_thr_idle;		/* thread waiting for work */
    444   1.90    bouyer 
    445   1.90    bouyer 	/* Number of sata PMP ports, if any */
    446   1.90    bouyer 	int ch_satapmp_nports;
    447  1.100  jdolecek 
    448  1.100  jdolecek 	/* Recovery buffer */
    449  1.100  jdolecek 	struct ata_xfer recovery_xfer;
    450  1.100  jdolecek 	uint8_t recovery_blk[ATA_BSIZE];
    451  1.100  jdolecek 	uint32_t recovery_tfd;		/* status/err encoded ATACH_ERR_ST() */
    452   1.59   thorpej };
    453   1.59   thorpej 
    454   1.60   thorpej /*
    455   1.60   thorpej  * ATA controller softc.
    456   1.60   thorpej  *
    457   1.60   thorpej  * This contains a bunch of generic info that all ATA controllers need
    458   1.60   thorpej  * to have.
    459   1.60   thorpej  *
    460   1.60   thorpej  * XXX There is still some lingering wdc-centricity here.
    461   1.60   thorpej  */
    462   1.60   thorpej struct atac_softc {
    463   1.76      cube 	device_t atac_dev;		/* generic device info */
    464   1.60   thorpej 
    465   1.60   thorpej 	int	atac_cap;		/* controller capabilities */
    466   1.60   thorpej 
    467   1.60   thorpej #define	ATAC_CAP_DATA16	0x0001		/* can do 16-bit data access */
    468   1.60   thorpej #define	ATAC_CAP_DATA32	0x0002		/* can do 32-bit data access */
    469   1.60   thorpej #define	ATAC_CAP_DMA	0x0008		/* can do ATA DMA modes */
    470   1.60   thorpej #define	ATAC_CAP_UDMA	0x0010		/* can do ATA Ultra DMA modes */
    471   1.72     itohy #define	ATAC_CAP_PIOBM	0x0020		/* can do busmastering PIO transfer */
    472   1.60   thorpej #define	ATAC_CAP_ATA_NOSTREAM 0x0040	/* don't use stream funcs on ATA */
    473   1.60   thorpej #define	ATAC_CAP_ATAPI_NOSTREAM 0x0080	/* don't use stream funcs on ATAPI */
    474   1.60   thorpej #define	ATAC_CAP_NOIRQ	0x1000		/* controller never interrupts */
    475   1.60   thorpej #define	ATAC_CAP_RAID	0x4000		/* controller "supports" RAID */
    476   1.93  jdolecek #define ATAC_CAP_NCQ	0x8000		/* controller supports NCQ */
    477   1.60   thorpej 
    478   1.60   thorpej 	uint8_t	atac_pio_cap;		/* highest PIO mode supported */
    479   1.73     itohy #if NATA_DMA
    480   1.60   thorpej 	uint8_t	atac_dma_cap;		/* highest DMA mode supported */
    481   1.73     itohy #if NATA_UDMA
    482   1.60   thorpej 	uint8_t	atac_udma_cap;		/* highest UDMA mode supported */
    483   1.73     itohy #endif
    484   1.73     itohy #endif
    485   1.60   thorpej 
    486   1.60   thorpej 	/* Array of pointers to channel-specific data. */
    487   1.60   thorpej 	struct ata_channel **atac_channels;
    488   1.60   thorpej 	int		     atac_nchannels;
    489   1.60   thorpej 
    490   1.62   thorpej 	const struct ata_bustype *atac_bustype_ata;
    491   1.62   thorpej 
    492   1.60   thorpej 	/*
    493   1.60   thorpej 	 * Glue between ATA and SCSIPI for the benefit of ATAPI.
    494   1.60   thorpej 	 *
    495   1.60   thorpej 	 * Note: The reference count here is used for both ATA and ATAPI
    496   1.60   thorpej 	 * devices.
    497   1.60   thorpej 	 */
    498   1.60   thorpej 	struct atapi_adapter atac_atapi_adapter;
    499   1.62   thorpej 	void (*atac_atapibus_attach)(struct atabus_softc *);
    500   1.60   thorpej 
    501   1.60   thorpej 	/* Driver callback to probe for drives. */
    502   1.60   thorpej 	void (*atac_probe)(struct ata_channel *);
    503   1.60   thorpej 
    504   1.93  jdolecek 	/*
    505   1.93  jdolecek 	 * Optional callbacks to lock/unlock hardware.
    506   1.93  jdolecek 	 * Called with channel mutex held.
    507   1.93  jdolecek 	 */
    508   1.60   thorpej 	int  (*atac_claim_hw)(struct ata_channel *, int);
    509   1.60   thorpej 	void (*atac_free_hw)(struct ata_channel *);
    510   1.60   thorpej 
    511   1.60   thorpej 	/*
    512   1.60   thorpej 	 * Optional callbacks to set drive mode.  Required for anything
    513   1.60   thorpej 	 * but basic PIO operation.
    514   1.60   thorpej 	 */
    515   1.60   thorpej 	void (*atac_set_modes)(struct ata_channel *);
    516   1.60   thorpej };
    517   1.60   thorpej 
    518   1.36       lha #ifdef _KERNEL
    519   1.63   thorpej void	ata_channel_attach(struct ata_channel *);
    520   1.93  jdolecek void	ata_channel_init(struct ata_channel *);
    521   1.93  jdolecek void	ata_channel_detach(struct ata_channel *);
    522   1.93  jdolecek void	ata_channel_destroy(struct ata_channel *);
    523   1.37   thorpej int	atabusprint(void *aux, const char *);
    524   1.37   thorpej int	ataprint(void *aux, const char *);
    525   1.36       lha 
    526   1.90    bouyer int	atabus_alloc_drives(struct ata_channel *, int);
    527   1.90    bouyer void	atabus_free_drives(struct ata_channel *);
    528   1.90    bouyer 
    529    1.2    bouyer struct ataparams;
    530   1.92      matt int	ata_get_params(struct ata_drive_datas *, uint8_t, struct ataparams *);
    531   1.92      matt int	ata_set_mode(struct ata_drive_datas *, uint8_t, uint8_t);
    532   1.93  jdolecek int	ata_read_log_ext_ncq(struct ata_drive_datas *, uint8_t, uint8_t *,
    533   1.93  jdolecek     uint8_t *, uint8_t *);
    534  1.100  jdolecek void	ata_recovery_resume(struct ata_channel *, int, int, int);
    535   1.93  jdolecek 
    536    1.2    bouyer /* return code for these cmds */
    537    1.2    bouyer #define CMD_OK    0
    538    1.2    bouyer #define CMD_ERR   1
    539    1.2    bouyer #define CMD_AGAIN 2
    540   1.10    bouyer 
    541  1.100  jdolecek struct ata_xfer *ata_get_xfer(struct ata_channel *, bool);
    542   1.59   thorpej void	ata_free_xfer(struct ata_channel *, struct ata_xfer *);
    543   1.93  jdolecek void	ata_deactivate_xfer(struct ata_channel *, struct ata_xfer *);
    544   1.93  jdolecek void	ata_exec_xfer(struct ata_channel *, struct ata_xfer *);
    545   1.93  jdolecek int	ata_xfer_start(struct ata_xfer *xfer);
    546  1.100  jdolecek void	ata_wait_cmd(struct ata_channel *, struct ata_xfer *xfer);
    547   1.54   thorpej 
    548   1.93  jdolecek void	ata_timeout(void *);
    549   1.93  jdolecek bool	ata_timo_xfer_check(struct ata_xfer *);
    550   1.55   thorpej void	ata_kill_pending(struct ata_drive_datas *);
    551   1.93  jdolecek void	ata_kill_active(struct ata_channel *, int, int);
    552  1.100  jdolecek void	ata_thread_run(struct ata_channel *, int, int, int);
    553  1.104  jdolecek bool	ata_is_thread_run(struct ata_channel *);
    554   1.93  jdolecek void	ata_channel_freeze(struct ata_channel *);
    555  1.100  jdolecek void	ata_channel_thaw_locked(struct ata_channel *);
    556   1.93  jdolecek void	ata_channel_lock(struct ata_channel *);
    557   1.93  jdolecek void	ata_channel_unlock(struct ata_channel *);
    558   1.93  jdolecek void	ata_channel_lock_owned(struct ata_channel *);
    559   1.55   thorpej 
    560   1.59   thorpej int	ata_addref(struct ata_channel *);
    561   1.59   thorpej void	ata_delref(struct ata_channel *);
    562   1.59   thorpej void	atastart(struct ata_channel *);
    563   1.59   thorpej void	ata_print_modes(struct ata_channel *);
    564   1.53   thorpej void	ata_probe_caps(struct ata_drive_datas *);
    565   1.51   thorpej 
    566   1.73     itohy #if NATA_DMA
    567   1.30   thorpej void	ata_dmaerr(struct ata_drive_datas *, int);
    568   1.73     itohy #endif
    569   1.93  jdolecek struct ata_queue *
    570   1.93  jdolecek 	ata_queue_alloc(uint8_t openings);
    571   1.93  jdolecek void	ata_queue_free(struct ata_queue *);
    572   1.93  jdolecek struct ata_xfer *
    573   1.93  jdolecek 	ata_queue_hwslot_to_xfer(struct ata_channel *, int);
    574   1.93  jdolecek struct ata_xfer *
    575   1.93  jdolecek 	ata_queue_get_active_xfer(struct ata_channel *);
    576   1.93  jdolecek struct ata_xfer *
    577   1.94  jdolecek 	ata_queue_get_active_xfer_locked(struct ata_channel *);
    578   1.94  jdolecek struct ata_xfer *
    579   1.93  jdolecek 	ata_queue_drive_active_xfer(struct ata_channel *, int);
    580  1.100  jdolecek bool	ata_queue_alloc_slot(struct ata_channel *, uint8_t *, uint8_t);
    581  1.100  jdolecek void	ata_queue_free_slot(struct ata_channel *, uint8_t);
    582  1.100  jdolecek uint32_t	ata_queue_active(struct ata_channel *);
    583  1.100  jdolecek uint8_t	ata_queue_openings(struct ata_channel *);
    584  1.100  jdolecek void	ata_queue_hold(struct ata_channel *);
    585  1.100  jdolecek void	ata_queue_unhold(struct ata_channel *);
    586   1.93  jdolecek 
    587   1.93  jdolecek void	ata_delay(struct ata_channel *, int, const char *, int);
    588   1.93  jdolecek 
    589   1.93  jdolecek bool	ata_waitdrain_xfer_check(struct ata_channel *, struct ata_xfer *);
    590   1.93  jdolecek 
    591   1.93  jdolecek void	atacmd_toncq(struct ata_xfer *, uint8_t *, uint16_t *, uint16_t *,
    592   1.93  jdolecek 	    uint8_t *);
    593   1.93  jdolecek 
    594   1.98  jdolecek #ifdef ATADEBUG
    595   1.98  jdolecek void	atachannel_debug(struct ata_channel *);
    596   1.98  jdolecek #endif
    597   1.98  jdolecek 
    598   1.37   thorpej #endif /* _KERNEL */
    599   1.31   thorpej 
    600   1.31   thorpej #endif /* _DEV_ATA_ATAVAR_H_ */
    601