xref: /freebsd/sys/dev/mfi/mfivar.h (revision 51550f824d632544e5b47c09fc890ab259c918d7)
12e21a3efSScott Long /*-
22e21a3efSScott Long  * Copyright (c) 2006 IronPort Systems
32e21a3efSScott Long  * All rights reserved.
42e21a3efSScott Long  *
52e21a3efSScott Long  * Redistribution and use in source and binary forms, with or without
62e21a3efSScott Long  * modification, are permitted provided that the following conditions
72e21a3efSScott Long  * are met:
82e21a3efSScott Long  * 1. Redistributions of source code must retain the above copyright
92e21a3efSScott Long  *    notice, this list of conditions and the following disclaimer.
102e21a3efSScott Long  * 2. Redistributions in binary form must reproduce the above copyright
112e21a3efSScott Long  *    notice, this list of conditions and the following disclaimer in the
122e21a3efSScott Long  *    documentation and/or other materials provided with the distribution.
132e21a3efSScott Long  *
142e21a3efSScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152e21a3efSScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162e21a3efSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172e21a3efSScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182e21a3efSScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192e21a3efSScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202e21a3efSScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212e21a3efSScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222e21a3efSScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232e21a3efSScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242e21a3efSScott Long  * SUCH DAMAGE.
252e21a3efSScott Long  */
26610f2ef3SScott Long /*-
27610f2ef3SScott Long  * Copyright (c) 2007 LSI Corp.
28610f2ef3SScott Long  * Copyright (c) 2007 Rajesh Prabhakaran.
29610f2ef3SScott Long  * All rights reserved.
30610f2ef3SScott Long  *
31610f2ef3SScott Long  * Redistribution and use in source and binary forms, with or without
32610f2ef3SScott Long  * modification, are permitted provided that the following conditions
33610f2ef3SScott Long  * are met:
34610f2ef3SScott Long  * 1. Redistributions of source code must retain the above copyright
35610f2ef3SScott Long  *    notice, this list of conditions and the following disclaimer.
36610f2ef3SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
37610f2ef3SScott Long  *    notice, this list of conditions and the following disclaimer in the
38610f2ef3SScott Long  *    documentation and/or other materials provided with the distribution.
39610f2ef3SScott Long  *
40610f2ef3SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
41610f2ef3SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42610f2ef3SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43610f2ef3SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44610f2ef3SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45610f2ef3SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46610f2ef3SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47610f2ef3SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48610f2ef3SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49610f2ef3SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50610f2ef3SScott Long  * SUCH DAMAGE.
51610f2ef3SScott Long  */
522e21a3efSScott Long 
532e21a3efSScott Long #ifndef _MFIVAR_H
542e21a3efSScott Long #define _MFIVAR_H
552e21a3efSScott Long 
562e21a3efSScott Long #include <sys/cdefs.h>
572e21a3efSScott Long __FBSDID("$FreeBSD$");
582e21a3efSScott Long 
598ec5c98bSJohn Baldwin #include <sys/lock.h>
608ec5c98bSJohn Baldwin #include <sys/sx.h>
618ec5c98bSJohn Baldwin 
622e21a3efSScott Long /*
632e21a3efSScott Long  * SCSI structures and definitions are used from here, but no linking
642e21a3efSScott Long  * requirements are made to CAM.
652e21a3efSScott Long  */
662e21a3efSScott Long #include <cam/scsi/scsi_all.h>
672e21a3efSScott Long 
682e21a3efSScott Long struct mfi_hwcomms {
692e21a3efSScott Long 	uint32_t		hw_pi;
702e21a3efSScott Long 	uint32_t		hw_ci;
712e21a3efSScott Long 	uint32_t		hw_reply_q[1];
722e21a3efSScott Long };
732e21a3efSScott Long 
742e21a3efSScott Long struct mfi_softc;
75ddfae47bSScott Long struct disk;
7635ef86f2SScott Long struct ccb_hdr;
772e21a3efSScott Long 
782e21a3efSScott Long struct mfi_command {
792e21a3efSScott Long 	TAILQ_ENTRY(mfi_command) cm_link;
805ba21ff1SScott Long 	time_t			cm_timestamp;
812e21a3efSScott Long 	struct mfi_softc	*cm_sc;
822e21a3efSScott Long 	union mfi_frame		*cm_frame;
832e21a3efSScott Long 	uint32_t		cm_frame_busaddr;
842e21a3efSScott Long 	struct mfi_sense	*cm_sense;
852e21a3efSScott Long 	uint32_t		cm_sense_busaddr;
862e21a3efSScott Long 	bus_dmamap_t		cm_dmamap;
872e21a3efSScott Long 	union mfi_sgl		*cm_sg;
882e21a3efSScott Long 	void			*cm_data;
892e21a3efSScott Long 	int			cm_len;
905f8ad41cSAlexander Motin 	int			cm_stp_len;
912e21a3efSScott Long 	int			cm_total_frame_size;
922e21a3efSScott Long 	int			cm_extra_frames;
932e21a3efSScott Long 	int			cm_flags;
942e21a3efSScott Long #define MFI_CMD_MAPPED		(1<<0)
952e21a3efSScott Long #define MFI_CMD_DATAIN		(1<<1)
962e21a3efSScott Long #define MFI_CMD_DATAOUT		(1<<2)
972e21a3efSScott Long #define MFI_CMD_COMPLETED	(1<<3)
982e21a3efSScott Long #define MFI_CMD_POLLED		(1<<4)
992e21a3efSScott Long #define MFI_ON_MFIQ_FREE	(1<<5)
1002e21a3efSScott Long #define MFI_ON_MFIQ_READY	(1<<6)
1012e21a3efSScott Long #define MFI_ON_MFIQ_BUSY	(1<<7)
1022e21a3efSScott Long #define MFI_ON_MFIQ_MASK	((1<<5)|(1<<6)|(1<<7))
103741367d5SDoug Ambrisko 	int			cm_aen_abort;
1042e21a3efSScott Long 	void			(* cm_complete)(struct mfi_command *cm);
1052e21a3efSScott Long 	void			*cm_private;
1065ba21ff1SScott Long 	int			cm_index;
1075be25877SDoug Ambrisko 	int			cm_error;
1082e21a3efSScott Long };
1092e21a3efSScott Long 
110ddfae47bSScott Long struct mfi_disk {
111ddfae47bSScott Long 	TAILQ_ENTRY(mfi_disk)	ld_link;
112ddfae47bSScott Long 	device_t	ld_dev;
1132e21a3efSScott Long 	int		ld_id;
114ddfae47bSScott Long 	int		ld_unit;
115ddfae47bSScott Long 	struct mfi_softc *ld_controller;
116ddfae47bSScott Long 	struct mfi_ld_info	*ld_info;
117ddfae47bSScott Long 	struct disk	*ld_disk;
118ddfae47bSScott Long 	int		ld_flags;
119ddfae47bSScott Long #define MFI_DISK_FLAGS_OPEN	0x01
1208ec5c98bSJohn Baldwin #define	MFI_DISK_FLAGS_DISABLED	0x02
1212e21a3efSScott Long };
1222e21a3efSScott Long 
123741367d5SDoug Ambrisko struct mfi_aen {
124741367d5SDoug Ambrisko 	TAILQ_ENTRY(mfi_aen) aen_link;
125741367d5SDoug Ambrisko 	struct proc			*p;
126741367d5SDoug Ambrisko };
127741367d5SDoug Ambrisko 
1282e21a3efSScott Long struct mfi_softc {
1292e21a3efSScott Long 	device_t			mfi_dev;
1302e21a3efSScott Long 	int				mfi_flags;
1312e21a3efSScott Long #define MFI_FLAGS_SG64		(1<<0)
1322e21a3efSScott Long #define MFI_FLAGS_QFRZN		(1<<1)
1332e21a3efSScott Long #define MFI_FLAGS_OPEN		(1<<2)
134441f6d5dSScott Long #define MFI_FLAGS_STOP		(1<<3)
135610f2ef3SScott Long #define MFI_FLAGS_1064R		(1<<4)
136610f2ef3SScott Long #define MFI_FLAGS_1078		(1<<5)
137fa1e6ef4SDoug Ambrisko #define MFI_FLAGS_GEN2		(1<<6)
1382e21a3efSScott Long 
1392e21a3efSScott Long 	struct mfi_hwcomms		*mfi_comms;
1402e21a3efSScott Long 	TAILQ_HEAD(,mfi_command)	mfi_free;
1412e21a3efSScott Long 	TAILQ_HEAD(,mfi_command)	mfi_ready;
1422e21a3efSScott Long 	TAILQ_HEAD(,mfi_command)	mfi_busy;
1432e21a3efSScott Long 	struct bio_queue_head		mfi_bioq;
1442e21a3efSScott Long 	struct mfi_qstat		mfi_qstat[MFIQ_COUNT];
1452e21a3efSScott Long 
1462e21a3efSScott Long 	struct resource			*mfi_regs_resource;
1472e21a3efSScott Long 	bus_space_handle_t		mfi_bhandle;
1482e21a3efSScott Long 	bus_space_tag_t			mfi_btag;
1492e21a3efSScott Long 	int				mfi_regs_rid;
1502e21a3efSScott Long 
1512e21a3efSScott Long 	bus_dma_tag_t			mfi_parent_dmat;
1522e21a3efSScott Long 	bus_dma_tag_t			mfi_buffer_dmat;
1532e21a3efSScott Long 
1542e21a3efSScott Long 	bus_dma_tag_t			mfi_comms_dmat;
1552e21a3efSScott Long 	bus_dmamap_t			mfi_comms_dmamap;
1562e21a3efSScott Long 	uint32_t			mfi_comms_busaddr;
1572e21a3efSScott Long 
1582e21a3efSScott Long 	bus_dma_tag_t			mfi_frames_dmat;
1592e21a3efSScott Long 	bus_dmamap_t			mfi_frames_dmamap;
1602e21a3efSScott Long 	uint32_t			mfi_frames_busaddr;
1612e21a3efSScott Long 	union mfi_frame			*mfi_frames;
1622e21a3efSScott Long 
163741367d5SDoug Ambrisko 	TAILQ_HEAD(,mfi_aen)		mfi_aen_pids;
164741367d5SDoug Ambrisko 	struct mfi_command		*mfi_aen_cm;
165741367d5SDoug Ambrisko 	uint32_t			mfi_aen_triggered;
166741367d5SDoug Ambrisko 	uint32_t			mfi_poll_waiting;
167741367d5SDoug Ambrisko 	struct selinfo			mfi_select;
1688ec5c98bSJohn Baldwin 	int				mfi_delete_busy_volumes;
1698ec5c98bSJohn Baldwin 	int				mfi_keep_deleted_volumes;
1708ec5c98bSJohn Baldwin 	int				mfi_detaching;
171741367d5SDoug Ambrisko 
1722e21a3efSScott Long 	bus_dma_tag_t			mfi_sense_dmat;
1732e21a3efSScott Long 	bus_dmamap_t			mfi_sense_dmamap;
1742e21a3efSScott Long 	uint32_t			mfi_sense_busaddr;
1752e21a3efSScott Long 	struct mfi_sense		*mfi_sense;
1762e21a3efSScott Long 
1772e21a3efSScott Long 	struct resource			*mfi_irq;
1782e21a3efSScott Long 	void				*mfi_intr;
1792e21a3efSScott Long 	int				mfi_irq_rid;
1802e21a3efSScott Long 
1812e21a3efSScott Long 	struct intr_config_hook		mfi_ich;
1822e21a3efSScott Long 	eventhandler_tag		eh;
1832e21a3efSScott Long 
1842e21a3efSScott Long 	/*
1852e21a3efSScott Long 	 * Allocation for the command array.  Used as an indexable array to
1862e21a3efSScott Long 	 * recover completed commands.
1872e21a3efSScott Long 	 */
1882e21a3efSScott Long 	struct mfi_command		*mfi_commands;
1892e21a3efSScott Long 	/*
1902e21a3efSScott Long 	 * How many commands were actually allocated
1912e21a3efSScott Long 	 */
1922e21a3efSScott Long 	int				mfi_total_cmds;
1932e21a3efSScott Long 	/*
1942e21a3efSScott Long 	 * How many commands the firmware can handle.  Also how big the reply
1952e21a3efSScott Long 	 * queue is, minus 1.
1962e21a3efSScott Long 	 */
1972e21a3efSScott Long 	int				mfi_max_fw_cmds;
1982e21a3efSScott Long 	/*
1992e21a3efSScott Long 	 * How many S/G elements we'll ever actually use
2002e21a3efSScott Long 	 */
20178e36c27SScott Long 	int				mfi_max_sge;
2022e21a3efSScott Long 	/*
2032e21a3efSScott Long 	 * How many bytes a compound frame is, including all of the extra frames
2042e21a3efSScott Long 	 * that are used for S/G elements.
2052e21a3efSScott Long 	 */
20678e36c27SScott Long 	int				mfi_cmd_size;
2072e21a3efSScott Long 	/*
2082e21a3efSScott Long 	 * How large an S/G element is.  Used to calculate the number of single
2092e21a3efSScott Long 	 * frames in a command.
2102e21a3efSScott Long 	 */
21178e36c27SScott Long 	int				mfi_sge_size;
2122e21a3efSScott Long 	/*
2132e21a3efSScott Long 	 * Max number of sectors that the firmware allows
2142e21a3efSScott Long 	 */
2152e21a3efSScott Long 	uint32_t			mfi_max_io;
2162e21a3efSScott Long 
217ddfae47bSScott Long 	TAILQ_HEAD(,mfi_disk)		mfi_ld_tqh;
2182e21a3efSScott Long 	eventhandler_tag		mfi_eh;
2192e21a3efSScott Long 	struct cdev			*mfi_cdev;
2202e21a3efSScott Long 
22135ef86f2SScott Long 	TAILQ_HEAD(, ccb_hdr)		mfi_cam_ccbq;
22235ef86f2SScott Long 	struct mfi_command *		(* mfi_cam_start)(void *);
2235ba21ff1SScott Long 	struct callout			mfi_watchdog_callout;
2242e21a3efSScott Long 	struct mtx			mfi_io_lock;
2258ec5c98bSJohn Baldwin 	struct sx			mfi_config_lock;
226610f2ef3SScott Long 
227610f2ef3SScott Long 	/* Controller type specific interfaces */
228610f2ef3SScott Long 	void	(*mfi_enable_intr)(struct mfi_softc *sc);
229610f2ef3SScott Long 	int32_t	(*mfi_read_fw_status)(struct mfi_softc *sc);
230610f2ef3SScott Long 	int	(*mfi_check_clear_intr)(struct mfi_softc *sc);
231610f2ef3SScott Long  	void	(*mfi_issue_cmd)(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt);
2322e21a3efSScott Long };
2332e21a3efSScott Long 
2342e21a3efSScott Long extern int mfi_attach(struct mfi_softc *);
2352e21a3efSScott Long extern void mfi_free(struct mfi_softc *);
2362e21a3efSScott Long extern int mfi_shutdown(struct mfi_softc *);
2372e21a3efSScott Long extern void mfi_startio(struct mfi_softc *);
2382e21a3efSScott Long extern void mfi_disk_complete(struct bio *);
2398ec5c98bSJohn Baldwin extern int mfi_disk_disable(struct mfi_disk *);
2408ec5c98bSJohn Baldwin extern void mfi_disk_enable(struct mfi_disk *);
2412e21a3efSScott Long extern int mfi_dump_blocks(struct mfi_softc *, int id, uint64_t, void *, int);
2422e21a3efSScott Long 
2432e21a3efSScott Long #define MFIQ_ADD(sc, qname)					\
2442e21a3efSScott Long 	do {							\
2452e21a3efSScott Long 		struct mfi_qstat *qs;				\
2462e21a3efSScott Long 								\
2472e21a3efSScott Long 		qs = &(sc)->mfi_qstat[qname];			\
2482e21a3efSScott Long 		qs->q_length++;					\
2492e21a3efSScott Long 		if (qs->q_length > qs->q_max)			\
2502e21a3efSScott Long 			qs->q_max = qs->q_length;		\
2512e21a3efSScott Long 	} while (0)
2522e21a3efSScott Long 
2532e21a3efSScott Long #define MFIQ_REMOVE(sc, qname)	(sc)->mfi_qstat[qname].q_length--
2542e21a3efSScott Long 
2552e21a3efSScott Long #define MFIQ_INIT(sc, qname)					\
2562e21a3efSScott Long 	do {							\
2572e21a3efSScott Long 		sc->mfi_qstat[qname].q_length = 0;		\
2582e21a3efSScott Long 		sc->mfi_qstat[qname].q_max = 0;			\
2592e21a3efSScott Long 	} while (0)
2602e21a3efSScott Long 
2612e21a3efSScott Long #define MFIQ_COMMAND_QUEUE(name, index)					\
2622e21a3efSScott Long 	static __inline void						\
2632e21a3efSScott Long 	mfi_initq_ ## name (struct mfi_softc *sc)			\
2642e21a3efSScott Long 	{								\
2652e21a3efSScott Long 		TAILQ_INIT(&sc->mfi_ ## name);				\
2662e21a3efSScott Long 		MFIQ_INIT(sc, index);					\
2672e21a3efSScott Long 	}								\
2682e21a3efSScott Long 	static __inline void						\
2692e21a3efSScott Long 	mfi_enqueue_ ## name (struct mfi_command *cm)			\
2702e21a3efSScott Long 	{								\
2712e21a3efSScott Long 		if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) {		\
2722e21a3efSScott Long 			printf("command %p is on another queue, "	\
2732e21a3efSScott Long 			    "flags = %#x\n", cm, cm->cm_flags);		\
2742e21a3efSScott Long 			panic("command is on another queue");		\
2752e21a3efSScott Long 		}							\
2762e21a3efSScott Long 		TAILQ_INSERT_TAIL(&cm->cm_sc->mfi_ ## name, cm, cm_link); \
2772e21a3efSScott Long 		cm->cm_flags |= MFI_ON_ ## index;			\
2782e21a3efSScott Long 		MFIQ_ADD(cm->cm_sc, index);				\
2792e21a3efSScott Long 	}								\
2802e21a3efSScott Long 	static __inline void						\
2812e21a3efSScott Long 	mfi_requeue_ ## name (struct mfi_command *cm)			\
2822e21a3efSScott Long 	{								\
2832e21a3efSScott Long 		if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) {		\
2842e21a3efSScott Long 			printf("command %p is on another queue, "	\
2852e21a3efSScott Long 			    "flags = %#x\n", cm, cm->cm_flags);		\
2862e21a3efSScott Long 			panic("command is on another queue");		\
2872e21a3efSScott Long 		}							\
2882e21a3efSScott Long 		TAILQ_INSERT_HEAD(&cm->cm_sc->mfi_ ## name, cm, cm_link); \
2892e21a3efSScott Long 		cm->cm_flags |= MFI_ON_ ## index;			\
2902e21a3efSScott Long 		MFIQ_ADD(cm->cm_sc, index);				\
2912e21a3efSScott Long 	}								\
2922e21a3efSScott Long 	static __inline struct mfi_command *				\
2932e21a3efSScott Long 	mfi_dequeue_ ## name (struct mfi_softc *sc)			\
2942e21a3efSScott Long 	{								\
2952e21a3efSScott Long 		struct mfi_command *cm;					\
2962e21a3efSScott Long 									\
2972e21a3efSScott Long 		if ((cm = TAILQ_FIRST(&sc->mfi_ ## name)) != NULL) {	\
2982e21a3efSScott Long 			if ((cm->cm_flags & MFI_ON_ ## index) == 0) {	\
2992e21a3efSScott Long 				printf("command %p not in queue, "	\
3002e21a3efSScott Long 				    "flags = %#x, bit = %#x\n", cm,	\
3012e21a3efSScott Long 				    cm->cm_flags, MFI_ON_ ## index);	\
3022e21a3efSScott Long 				panic("command not in queue");		\
3032e21a3efSScott Long 			}						\
3042e21a3efSScott Long 			TAILQ_REMOVE(&sc->mfi_ ## name, cm, cm_link);	\
3052e21a3efSScott Long 			cm->cm_flags &= ~MFI_ON_ ## index;		\
3062e21a3efSScott Long 			MFIQ_REMOVE(sc, index);				\
3072e21a3efSScott Long 		}							\
3082e21a3efSScott Long 		return (cm);						\
3092e21a3efSScott Long 	}								\
3102e21a3efSScott Long 	static __inline void						\
3112e21a3efSScott Long 	mfi_remove_ ## name (struct mfi_command *cm)			\
3122e21a3efSScott Long 	{								\
3132e21a3efSScott Long 		if ((cm->cm_flags & MFI_ON_ ## index) == 0) {		\
3142e21a3efSScott Long 			printf("command %p not in queue, flags = %#x, " \
3152e21a3efSScott Long 			    "bit = %#x\n", cm, cm->cm_flags,		\
3162e21a3efSScott Long 			    MFI_ON_ ## index);				\
3172e21a3efSScott Long 			panic("command not in queue");			\
3182e21a3efSScott Long 		}							\
3192e21a3efSScott Long 		TAILQ_REMOVE(&cm->cm_sc->mfi_ ## name, cm, cm_link);	\
3202e21a3efSScott Long 		cm->cm_flags &= ~MFI_ON_ ## index;			\
3212e21a3efSScott Long 		MFIQ_REMOVE(cm->cm_sc, index);				\
3222e21a3efSScott Long 	}								\
3232e21a3efSScott Long struct hack
3242e21a3efSScott Long 
3252e21a3efSScott Long MFIQ_COMMAND_QUEUE(free, MFIQ_FREE);
3262e21a3efSScott Long MFIQ_COMMAND_QUEUE(ready, MFIQ_READY);
3272e21a3efSScott Long MFIQ_COMMAND_QUEUE(busy, MFIQ_BUSY);
3282e21a3efSScott Long 
3292e21a3efSScott Long static __inline void
3302e21a3efSScott Long mfi_initq_bio(struct mfi_softc *sc)
3312e21a3efSScott Long {
3322e21a3efSScott Long 	bioq_init(&sc->mfi_bioq);
3332e21a3efSScott Long 	MFIQ_INIT(sc, MFIQ_BIO);
3342e21a3efSScott Long }
3352e21a3efSScott Long 
3362e21a3efSScott Long static __inline void
3372e21a3efSScott Long mfi_enqueue_bio(struct mfi_softc *sc, struct bio *bp)
3382e21a3efSScott Long {
3392e21a3efSScott Long 	bioq_insert_tail(&sc->mfi_bioq, bp);
3402e21a3efSScott Long 	MFIQ_ADD(sc, MFIQ_BIO);
3412e21a3efSScott Long }
3422e21a3efSScott Long 
3432e21a3efSScott Long static __inline struct bio *
3442e21a3efSScott Long mfi_dequeue_bio(struct mfi_softc *sc)
3452e21a3efSScott Long {
3462e21a3efSScott Long 	struct bio *bp;
3472e21a3efSScott Long 
3482e21a3efSScott Long 	if ((bp = bioq_first(&sc->mfi_bioq)) != NULL) {
3492e21a3efSScott Long 		bioq_remove(&sc->mfi_bioq, bp);
3502e21a3efSScott Long 		MFIQ_REMOVE(sc, MFIQ_BIO);
3512e21a3efSScott Long 	}
3522e21a3efSScott Long 	return (bp);
3532e21a3efSScott Long }
3542e21a3efSScott Long 
355*51550f82SScott Long /*
356*51550f82SScott Long  * This is from the original scsi_extract_sense() in CAM.  It's copied
357*51550f82SScott Long  * here because CAM now uses a non-inline version that follows more complex
358*51550f82SScott Long  * additions to the SPC spec, and we don't want to force a dependency on
359*51550f82SScott Long  * the CAM module for such a trivial action.
360*51550f82SScott Long  */
361*51550f82SScott Long static __inline void
362*51550f82SScott Long mfi_extract_sense(struct scsi_sense_data_fixed *sense,
363*51550f82SScott Long     int *error_code, int *sense_key, int *asc, int *ascq)
364*51550f82SScott Long {
365*51550f82SScott Long 
366*51550f82SScott Long 	*error_code = sense->error_code & SSD_ERRCODE;
367*51550f82SScott Long 	*sense_key = sense->flags & SSD_KEY;
368*51550f82SScott Long 	*asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
369*51550f82SScott Long 	*ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
370*51550f82SScott Long }
371*51550f82SScott Long 
3722e21a3efSScott Long static __inline void
3732e21a3efSScott Long mfi_print_sense(struct mfi_softc *sc, void *sense)
3742e21a3efSScott Long {
3752e21a3efSScott Long 	int error, key, asc, ascq;
3762e21a3efSScott Long 
377*51550f82SScott Long 	mfi_extract_sense((struct scsi_sense_data_fixed *)sense,
3782e21a3efSScott Long 	    &error, &key, &asc, &ascq);
3792e21a3efSScott Long 	device_printf(sc->mfi_dev, "sense error %d, sense_key %d, "
3802e21a3efSScott Long 	    "asc %d, ascq %d\n", error, key, asc, ascq);
3812e21a3efSScott Long }
3822e21a3efSScott Long 
3832e21a3efSScott Long 
3842e21a3efSScott Long #define MFI_WRITE4(sc, reg, val)	bus_space_write_4((sc)->mfi_btag, \
3852e21a3efSScott Long 	sc->mfi_bhandle, (reg), (val))
3862e21a3efSScott Long #define MFI_READ4(sc, reg)		bus_space_read_4((sc)->mfi_btag, \
3872e21a3efSScott Long 	(sc)->mfi_bhandle, (reg))
3882e21a3efSScott Long #define MFI_WRITE2(sc, reg, val)	bus_space_write_2((sc)->mfi_btag, \
3892e21a3efSScott Long 	sc->mfi_bhandle, (reg), (val))
3902e21a3efSScott Long #define MFI_READ2(sc, reg)		bus_space_read_2((sc)->mfi_btag, \
3912e21a3efSScott Long 	(sc)->mfi_bhandle, (reg))
3922e21a3efSScott Long #define MFI_WRITE1(sc, reg, val)	bus_space_write_1((sc)->mfi_btag, \
3932e21a3efSScott Long 	sc->mfi_bhandle, (reg), (val))
3942e21a3efSScott Long #define MFI_READ1(sc, reg)		bus_space_read_1((sc)->mfi_btag, \
3952e21a3efSScott Long 	(sc)->mfi_bhandle, (reg))
3962e21a3efSScott Long 
3972e21a3efSScott Long MALLOC_DECLARE(M_MFIBUF);
3982e21a3efSScott Long 
3995ba21ff1SScott Long #define MFI_CMD_TIMEOUT 30
40052c9ce25SScott Long #define MFI_MAXPHYS (128 * 1024)
4015ba21ff1SScott Long 
4025ba21ff1SScott Long #ifdef MFI_DEBUG
4035ba21ff1SScott Long extern void mfi_print_cmd(struct mfi_command *cm);
4045ba21ff1SScott Long extern void mfi_dump_cmds(struct mfi_softc *sc);
405441f6d5dSScott Long extern void mfi_validate_sg(struct mfi_softc *, struct mfi_command *, const char *, int );
4065ba21ff1SScott Long #define MFI_PRINT_CMD(cm)	mfi_print_cmd(cm)
407441f6d5dSScott Long #define MFI_DUMP_CMDS(sc)	mfi_dump_cmds(sc)
408441f6d5dSScott Long #define MFI_VALIDATE_CMD(sc, cm) mfi_validate_sg(sc, cm, __FUNCTION__, __LINE__)
4095ba21ff1SScott Long #else
4105ba21ff1SScott Long #define MFI_PRINT_CMD(cm)
4115ba21ff1SScott Long #define MFI_DUMP_CMDS(sc)
412441f6d5dSScott Long #define MFI_VALIDATE_CMD(sc, cm)
4135ba21ff1SScott Long #endif
4145ba21ff1SScott Long 
41535ef86f2SScott Long extern void mfi_release_command(struct mfi_command *cm);
41635ef86f2SScott Long 
4172e21a3efSScott Long #endif /* _MFIVAR_H */
418