xref: /freebsd/sys/dev/mfi/mfivar.h (revision 94942af266ac119ede0ca836f9aa5a5ac0582938)
1 /*-
2  * Copyright (c) 2006 IronPort Systems
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _MFIVAR_H
28 #define _MFIVAR_H
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * SCSI structures and definitions are used from here, but no linking
35  * requirements are made to CAM.
36  */
37 #include <cam/scsi/scsi_all.h>
38 
39 struct mfi_hwcomms {
40 	uint32_t		hw_pi;
41 	uint32_t		hw_ci;
42 	uint32_t		hw_reply_q[1];
43 };
44 
45 struct mfi_softc;
46 struct disk;
47 struct ccb_hdr;
48 
49 struct mfi_command {
50 	TAILQ_ENTRY(mfi_command) cm_link;
51 	time_t			cm_timestamp;
52 	struct mfi_softc	*cm_sc;
53 	union mfi_frame		*cm_frame;
54 	uint32_t		cm_frame_busaddr;
55 	struct mfi_sense	*cm_sense;
56 	uint32_t		cm_sense_busaddr;
57 	bus_dmamap_t		cm_dmamap;
58 	union mfi_sgl		*cm_sg;
59 	void			*cm_data;
60 	int			cm_len;
61 	int			cm_total_frame_size;
62 	int			cm_extra_frames;
63 	int			cm_flags;
64 #define MFI_CMD_MAPPED		(1<<0)
65 #define MFI_CMD_DATAIN		(1<<1)
66 #define MFI_CMD_DATAOUT		(1<<2)
67 #define MFI_CMD_COMPLETED	(1<<3)
68 #define MFI_CMD_POLLED		(1<<4)
69 #define MFI_ON_MFIQ_FREE	(1<<5)
70 #define MFI_ON_MFIQ_READY	(1<<6)
71 #define MFI_ON_MFIQ_BUSY	(1<<7)
72 #define MFI_ON_MFIQ_MASK	((1<<5)|(1<<6)|(1<<7))
73 	int			cm_aen_abort;
74 	void			(* cm_complete)(struct mfi_command *cm);
75 	void			*cm_private;
76 	int			cm_index;
77 };
78 
79 struct mfi_disk {
80 	TAILQ_ENTRY(mfi_disk)	ld_link;
81 	device_t	ld_dev;
82 	int		ld_id;
83 	int		ld_unit;
84 	struct mfi_softc *ld_controller;
85 	struct mfi_ld_info	*ld_info;
86 	struct disk	*ld_disk;
87 	int		ld_flags;
88 #define MFI_DISK_FLAGS_OPEN	0x01
89 };
90 
91 struct mfi_aen {
92 	TAILQ_ENTRY(mfi_aen) aen_link;
93 	struct proc			*p;
94 };
95 
96 struct mfi_softc {
97 	device_t			mfi_dev;
98 	int				mfi_flags;
99 #define MFI_FLAGS_SG64		(1<<0)
100 #define MFI_FLAGS_QFRZN		(1<<1)
101 #define MFI_FLAGS_OPEN		(1<<2)
102 #define MFI_FLAGS_STOP		(1<<3)
103 
104 	struct mfi_hwcomms		*mfi_comms;
105 	TAILQ_HEAD(,mfi_command)	mfi_free;
106 	TAILQ_HEAD(,mfi_command)	mfi_ready;
107 	TAILQ_HEAD(,mfi_command)	mfi_busy;
108 	struct bio_queue_head		mfi_bioq;
109 	struct mfi_qstat		mfi_qstat[MFIQ_COUNT];
110 
111 	struct resource			*mfi_regs_resource;
112 	bus_space_handle_t		mfi_bhandle;
113 	bus_space_tag_t			mfi_btag;
114 	int				mfi_regs_rid;
115 
116 	bus_dma_tag_t			mfi_parent_dmat;
117 	bus_dma_tag_t			mfi_buffer_dmat;
118 
119 	bus_dma_tag_t			mfi_comms_dmat;
120 	bus_dmamap_t			mfi_comms_dmamap;
121 	uint32_t			mfi_comms_busaddr;
122 
123 	bus_dma_tag_t			mfi_frames_dmat;
124 	bus_dmamap_t			mfi_frames_dmamap;
125 	uint32_t			mfi_frames_busaddr;
126 	union mfi_frame			*mfi_frames;
127 
128 	TAILQ_HEAD(,mfi_aen)		mfi_aen_pids;
129 	struct mfi_command		*mfi_aen_cm;
130 	uint32_t			mfi_aen_triggered;
131 	uint32_t			mfi_poll_waiting;
132 	struct selinfo			mfi_select;
133 
134 	bus_dma_tag_t			mfi_sense_dmat;
135 	bus_dmamap_t			mfi_sense_dmamap;
136 	uint32_t			mfi_sense_busaddr;
137 	struct mfi_sense		*mfi_sense;
138 
139 	struct resource			*mfi_irq;
140 	void				*mfi_intr;
141 	int				mfi_irq_rid;
142 
143 	struct intr_config_hook		mfi_ich;
144 	eventhandler_tag		eh;
145 
146 	/*
147 	 * Allocation for the command array.  Used as an indexable array to
148 	 * recover completed commands.
149 	 */
150 	struct mfi_command		*mfi_commands;
151 	/*
152 	 * How many commands were actually allocated
153 	 */
154 	int				mfi_total_cmds;
155 	/*
156 	 * How many commands the firmware can handle.  Also how big the reply
157 	 * queue is, minus 1.
158 	 */
159 	int				mfi_max_fw_cmds;
160 	/*
161 	 * How many S/G elements we'll ever actually use
162 	 */
163 	int				mfi_max_sge;
164 	/*
165 	 * How many bytes a compound frame is, including all of the extra frames
166 	 * that are used for S/G elements.
167 	 */
168 	int				mfi_cmd_size;
169 	/*
170 	 * How large an S/G element is.  Used to calculate the number of single
171 	 * frames in a command.
172 	 */
173 	int				mfi_sge_size;
174 	/*
175 	 * Max number of sectors that the firmware allows
176 	 */
177 	uint32_t			mfi_max_io;
178 
179 	TAILQ_HEAD(,mfi_disk)		mfi_ld_tqh;
180 	eventhandler_tag		mfi_eh;
181 	struct cdev			*mfi_cdev;
182 
183 	TAILQ_HEAD(, ccb_hdr)		mfi_cam_ccbq;
184 	struct mfi_command *		(* mfi_cam_start)(void *);
185 	struct callout			mfi_watchdog_callout;
186 	struct mtx			mfi_io_lock;
187 };
188 
189 extern int mfi_attach(struct mfi_softc *);
190 extern void mfi_free(struct mfi_softc *);
191 extern int mfi_shutdown(struct mfi_softc *);
192 extern void mfi_startio(struct mfi_softc *);
193 extern void mfi_disk_complete(struct bio *);
194 extern int mfi_dump_blocks(struct mfi_softc *, int id, uint64_t, void *, int);
195 
196 #define MFIQ_ADD(sc, qname)					\
197 	do {							\
198 		struct mfi_qstat *qs;				\
199 								\
200 		qs = &(sc)->mfi_qstat[qname];			\
201 		qs->q_length++;					\
202 		if (qs->q_length > qs->q_max)			\
203 			qs->q_max = qs->q_length;		\
204 	} while (0)
205 
206 #define MFIQ_REMOVE(sc, qname)	(sc)->mfi_qstat[qname].q_length--
207 
208 #define MFIQ_INIT(sc, qname)					\
209 	do {							\
210 		sc->mfi_qstat[qname].q_length = 0;		\
211 		sc->mfi_qstat[qname].q_max = 0;			\
212 	} while (0)
213 
214 #define MFIQ_COMMAND_QUEUE(name, index)					\
215 	static __inline void						\
216 	mfi_initq_ ## name (struct mfi_softc *sc)			\
217 	{								\
218 		TAILQ_INIT(&sc->mfi_ ## name);				\
219 		MFIQ_INIT(sc, index);					\
220 	}								\
221 	static __inline void						\
222 	mfi_enqueue_ ## name (struct mfi_command *cm)			\
223 	{								\
224 		if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) {		\
225 			printf("command %p is on another queue, "	\
226 			    "flags = %#x\n", cm, cm->cm_flags);		\
227 			panic("command is on another queue");		\
228 		}							\
229 		TAILQ_INSERT_TAIL(&cm->cm_sc->mfi_ ## name, cm, cm_link); \
230 		cm->cm_flags |= MFI_ON_ ## index;			\
231 		MFIQ_ADD(cm->cm_sc, index);				\
232 	}								\
233 	static __inline void						\
234 	mfi_requeue_ ## name (struct mfi_command *cm)			\
235 	{								\
236 		if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) {		\
237 			printf("command %p is on another queue, "	\
238 			    "flags = %#x\n", cm, cm->cm_flags);		\
239 			panic("command is on another queue");		\
240 		}							\
241 		TAILQ_INSERT_HEAD(&cm->cm_sc->mfi_ ## name, cm, cm_link); \
242 		cm->cm_flags |= MFI_ON_ ## index;			\
243 		MFIQ_ADD(cm->cm_sc, index);				\
244 	}								\
245 	static __inline struct mfi_command *				\
246 	mfi_dequeue_ ## name (struct mfi_softc *sc)			\
247 	{								\
248 		struct mfi_command *cm;					\
249 									\
250 		if ((cm = TAILQ_FIRST(&sc->mfi_ ## name)) != NULL) {	\
251 			if ((cm->cm_flags & MFI_ON_ ## index) == 0) {	\
252 				printf("command %p not in queue, "	\
253 				    "flags = %#x, bit = %#x\n", cm,	\
254 				    cm->cm_flags, MFI_ON_ ## index);	\
255 				panic("command not in queue");		\
256 			}						\
257 			TAILQ_REMOVE(&sc->mfi_ ## name, cm, cm_link);	\
258 			cm->cm_flags &= ~MFI_ON_ ## index;		\
259 			MFIQ_REMOVE(sc, index);				\
260 		}							\
261 		return (cm);						\
262 	}								\
263 	static __inline void						\
264 	mfi_remove_ ## name (struct mfi_command *cm)			\
265 	{								\
266 		if ((cm->cm_flags & MFI_ON_ ## index) == 0) {		\
267 			printf("command %p not in queue, flags = %#x, " \
268 			    "bit = %#x\n", cm, cm->cm_flags,		\
269 			    MFI_ON_ ## index);				\
270 			panic("command not in queue");			\
271 		}							\
272 		TAILQ_REMOVE(&cm->cm_sc->mfi_ ## name, cm, cm_link);	\
273 		cm->cm_flags &= ~MFI_ON_ ## index;			\
274 		MFIQ_REMOVE(cm->cm_sc, index);				\
275 	}								\
276 struct hack
277 
278 MFIQ_COMMAND_QUEUE(free, MFIQ_FREE);
279 MFIQ_COMMAND_QUEUE(ready, MFIQ_READY);
280 MFIQ_COMMAND_QUEUE(busy, MFIQ_BUSY);
281 
282 static __inline void
283 mfi_initq_bio(struct mfi_softc *sc)
284 {
285 	bioq_init(&sc->mfi_bioq);
286 	MFIQ_INIT(sc, MFIQ_BIO);
287 }
288 
289 static __inline void
290 mfi_enqueue_bio(struct mfi_softc *sc, struct bio *bp)
291 {
292 	bioq_insert_tail(&sc->mfi_bioq, bp);
293 	MFIQ_ADD(sc, MFIQ_BIO);
294 }
295 
296 static __inline struct bio *
297 mfi_dequeue_bio(struct mfi_softc *sc)
298 {
299 	struct bio *bp;
300 
301 	if ((bp = bioq_first(&sc->mfi_bioq)) != NULL) {
302 		bioq_remove(&sc->mfi_bioq, bp);
303 		MFIQ_REMOVE(sc, MFIQ_BIO);
304 	}
305 	return (bp);
306 }
307 
308 static __inline void
309 mfi_print_sense(struct mfi_softc *sc, void *sense)
310 {
311 	int error, key, asc, ascq;
312 
313 	scsi_extract_sense((struct scsi_sense_data *)sense,
314 	    &error, &key, &asc, &ascq);
315 	device_printf(sc->mfi_dev, "sense error %d, sense_key %d, "
316 	    "asc %d, ascq %d\n", error, key, asc, ascq);
317 }
318 
319 
320 #define MFI_WRITE4(sc, reg, val)	bus_space_write_4((sc)->mfi_btag, \
321 	sc->mfi_bhandle, (reg), (val))
322 #define MFI_READ4(sc, reg)		bus_space_read_4((sc)->mfi_btag, \
323 	(sc)->mfi_bhandle, (reg))
324 #define MFI_WRITE2(sc, reg, val)	bus_space_write_2((sc)->mfi_btag, \
325 	sc->mfi_bhandle, (reg), (val))
326 #define MFI_READ2(sc, reg)		bus_space_read_2((sc)->mfi_btag, \
327 	(sc)->mfi_bhandle, (reg))
328 #define MFI_WRITE1(sc, reg, val)	bus_space_write_1((sc)->mfi_btag, \
329 	sc->mfi_bhandle, (reg), (val))
330 #define MFI_READ1(sc, reg)		bus_space_read_1((sc)->mfi_btag, \
331 	(sc)->mfi_bhandle, (reg))
332 
333 MALLOC_DECLARE(M_MFIBUF);
334 
335 #define MFI_CMD_TIMEOUT 30
336 
337 #ifdef MFI_DEBUG
338 extern void mfi_print_cmd(struct mfi_command *cm);
339 extern void mfi_dump_cmds(struct mfi_softc *sc);
340 extern void mfi_validate_sg(struct mfi_softc *, struct mfi_command *, const char *, int );
341 #define MFI_PRINT_CMD(cm)	mfi_print_cmd(cm)
342 #define MFI_DUMP_CMDS(sc)	mfi_dump_cmds(sc)
343 #define MFI_VALIDATE_CMD(sc, cm) mfi_validate_sg(sc, cm, __FUNCTION__, __LINE__)
344 #else
345 #define MFI_PRINT_CMD(cm)
346 #define MFI_DUMP_CMDS(sc)
347 #define MFI_VALIDATE_CMD(sc, cm)
348 #endif
349 
350 extern void mfi_release_command(struct mfi_command *cm);
351 
352 #endif /* _MFIVAR_H */
353