1 /*	$OpenBSD: mpt_openbsd.h,v 1.6 2004/03/20 03:54:16 krw Exp $	*/
2 /*	$NetBSD: mpt_netbsd.h,v 1.2 2003/04/16 23:02:14 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 2004 Milos Urbanek
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  */
24 /*
25  * Copyright (c) 2003 Wasabi Systems, Inc.
26  * All rights reserved.
27  *
28  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
33  * 1. Redistributions of source code must retain the above copyright
34  *    notice, this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright
36  *    notice, this list of conditions and the following disclaimer in the
37  *    documentation and/or other materials provided with the distribution.
38  * 3. All advertising materials mentioning features or use of this software
39  *    must display the following acknowledgement:
40  *	This product includes software developed for the NetBSD Project by
41  *	Wasabi Systems, Inc.
42  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
43  *    or promote products derived from this software without specific prior
44  *    written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
50  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56  * POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 /*
60  * Copyright (c) 2000, 2001 by Greg Ansley, Adam Prewett
61  *
62  * Partially derived from Matt Jacobs ISP driver.
63  *
64  * Redistribution and use in source and binary forms, with or without
65  * modification, are permitted provided that the following conditions
66  * are met:
67  * 1. Redistributions of source code must retain the above copyright
68  *    notice immediately at the beginning of the file, without modification,
69  *    this list of conditions, and the following disclaimer.
70  * 2. The name of the author may not be used to endorse or promote products
71  *    derived from this software without specific prior written permission.
72  *
73  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
74  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
77  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82  */
83 /*
84  * Additional Copyright (c) 2002 by Matthew Jacob under same license.
85  */
86 
87 /*
88  * mpt_openbsd.h:
89  *
90  * OpenBSD-specific definitions for LSI Fusion adapters.
91  *
92  * Adapted from the NetBSD "mpt" driver by Milos Urbanek for
93  * ZOOM International, s.r.o.
94  */
95 
96 #ifndef _DEV_IC_MPT_NETBSD_H_
97 #define	_DEV_IC_MPT_NETBSD_H_
98 
99 #include <sys/param.h>
100 #include <sys/systm.h>
101 #include <sys/malloc.h>
102 #include <sys/device.h>
103 #include <sys/kernel.h>
104 #include <sys/timeout.h>
105 #include <sys/errno.h>
106 #include <sys/buf.h>
107 #include <sys/queue.h>
108 
109 #include <uvm/uvm_extern.h>
110 
111 #include <machine/bus.h>
112 #include <machine/intr.h>
113 
114 #include <scsi/scsi_all.h>
115 #include <scsi/scsiconf.h>
116 
117 #include <dev/ic/mpt_mpilib.h>
118 
119 /*
120  * macro to convert from milliseconds to hz without integer overflow
121  * Default version using only 32bits arithmetics.
122  * 64bit port can define 64bit version in their <machine/param.h>
123  * 0x20000 is safe for hz < 20000
124  */
125 #ifndef mstohz
126 #define mstohz(ms) \
127 	    (((ms +0u) / 1000u) * hz)
128 #endif
129 
130 /* Max MPT Reply we are willing to accept (must be a power of 2). */
131 #define	MPT_REPLY_SIZE		128
132 
133 #define	MPT_MAX_REQUESTS(mpt)	((mpt)->is_fc ? 1024 : 256)
134 #define	MPT_REQUEST_AREA	512
135 #define	MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
136 #define	MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
137 
138 /*
139  * We cannot tell prior to getting IOC facts how big the IOC's request
140  * area is. Because of this we cannot tell at compile time how many
141  * simple SG elements we can fit within an IOC request prior to having
142  * to put in a chain element.
143  *
144  * Experimentally we know that the Ultra4 parts have a 96 byte request
145  * element size and the Fibre Channel units have a 144 byte request
146  * element size. Therefore, if we have 512-32 (== 480) bytes of request
147  * area to play with, we have room for between 3 and 5 request sized
148  * regions- the first of which is the command  plus a simple SG list,
149  * the rest of which are chained continuation SG lists. Given that the
150  * normal request we use is 48 bytes w/o the first SG element, we can
151  * assume we have 480-48 == 432 bytes to have simple SG elements and/or
152  * chain elements. If we assume 32 bit addressing, this works out to
153  * 54 SG or chain elements. If we assume 5 chain elements, then we have
154  * a maximum of 49 seperate actual SG segments.
155  */
156 #define	MPT_SGL_MAX		49
157 
158 #define	MPT_RQSL(mpt)		((mpt)->request_frame_size << 2)
159 #define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof(SGE_SIMPLE32))
160 
161 #define	MPT_NSGL_FIRST(mpt)					\
162 	((((mpt)->request_frame_size << 2) -			\
163 	  sizeof(MSG_SCSI_IO_REQUEST) -				\
164 	  sizeof(SGE_IO_UNION)) / sizeof(SGE_SIMPLE32))
165 
166 /*
167  * Convert a physical address returned from IOC to a virtual address
168  * needed to access the data.
169  */
170 #define	MPT_REPLY_PTOV(m, x)					\
171 	((void *)(&(m)->reply[(((x) << 1) - (m)->reply_phys)]))
172 
173 enum mpt_req_state {
174 	REQ_FREE,
175 	REQ_IN_PROGRESS,
176 	REQ_TIMEOUT,
177 	REQ_ON_CHIP,
178 	REQ_DONE
179 };
180 typedef struct req_entry {
181 	uint16_t	index;		/* index of this entry */
182 	struct scsi_xfer *xfer;		/* scsipi xfer request */
183 	void		*req_vbuf;	/* virtual address of entry */
184 	void		*sense_vbuf;	/* virtual address of sense data */
185 	bus_addr_t	req_pbuf;	/* physical address of entry */
186 	bus_addr_t	sense_pbuf;	/* physical address of sense data */
187 	bus_dmamap_t	dmap;		/* DMA map for data buffer */
188 	SLIST_ENTRY(req_entry) link;	/* pointer to next in list */
189 	enum mpt_req_state debug;	/* debugging */
190 	uint32_t	sequence;	/* sequence number */
191 } request_t;
192 
193 typedef struct mpt_softc {
194 	struct device	mpt_dev;		/* base device glue */
195 
196 	int		verbose;
197 	int		is_fc;
198 	int		bus;
199 
200 	/* IOC facts */
201 	uint16_t	mpt_global_credits;
202 	uint16_t	request_frame_size;
203 	uint8_t		mpt_max_devices;
204 	uint8_t		mpt_max_buses;
205 	uint8_t         fw_download_boot;
206 	uint32_t        fw_image_size;
207 
208 	/* Port facts */
209 	uint16_t	mpt_ini_id;
210 
211 	/* Device configuration information */
212 	union {
213 		struct mpt_spi_cfg {
214 			fCONFIG_PAGE_SCSI_PORT_0	_port_page0;
215 			fCONFIG_PAGE_SCSI_PORT_1	_port_page1;
216 			fCONFIG_PAGE_SCSI_PORT_2	_port_page2;
217 			fCONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
218 			fCONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
219 			uint16_t			_negotiated_speed[16];
220 			uint16_t			_tag_enable;
221 			uint16_t			_disc_enable;
222 			uint16_t			_update_params0;
223 			uint16_t			_update_params1;
224 			uint16_t			_report_xfer_mode;
225 		} spi;
226 #define	mpt_port_page0		cfg.spi._port_page0
227 #define	mpt_port_page1		cfg.spi._port_page1
228 #define	mpt_port_page2		cfg.spi._port_page2
229 #define	mpt_dev_page0		cfg.spi._dev_page0
230 #define	mpt_dev_page1		cfg.spi._dev_page1
231 #define	mpt_negotiated_speed	cfg.spi._negotiated_speed
232 #define	mpt_tag_enable		cfg.spi._tag_enable
233 #define	mpt_disc_enable		cfg.spi._disc_enable
234 #define	mpt_update_params0	cfg.spi._update_params0
235 #define	mpt_update_params1	cfg.spi._update_params1
236 #define	mpt_report_xfer_mode	cfg.spi._report_xfer_mode
237 
238 		struct mpt_fc_cfg {
239 			uint8_t		nada;
240 		} fc;
241 	} cfg;
242 
243 	bus_space_tag_t		sc_st;
244 	bus_space_handle_t	sc_sh;
245 	bus_dma_tag_t		sc_dmat;
246 
247 	/* Reply memory */
248 	bus_dmamap_t		reply_dmap;
249 	char			*reply;
250 	bus_addr_t		reply_phys;
251 
252 	/* Request memory */
253 	bus_dmamap_t		request_dmap;
254 	char			*request;
255 	bus_addr_t		request_phys;
256 
257 	/* scsi linkage */
258 	request_t		*request_pool;
259 	SLIST_HEAD(req_queue, req_entry) request_free_list;
260 
261 	struct scsi_link	sc_link;
262 	struct scsi_adapter	sc_adapter;
263 
264 	uint32_t		sequence;	/* sequence number */
265 	uint32_t		timeouts;	/* timeout count */
266 	uint32_t		success;	/* success after timeout */
267 
268 	uint8_t                 upload_fw;      /* If set, do a fw upload */
269 	/* Firmware memory */
270 	bus_dmamap_t            fw_dmap;
271 	int                     fw_rseg;
272 	bus_dma_segment_t       fw_seg;
273 	char                    *fw;
274 
275 	/* Companion part in a 929 or 1030, or NULL. */
276 	struct mpt_softc	*mpt2;
277 
278 	/* To restore configuration after hard reset. */
279 	void			(*sc_set_config_regs)(struct mpt_softc *);
280 } mpt_softc_t;
281 
282 #define	MPT_SYNC_REQ(mpt, req, ops)				\
283 	bus_dmamap_sync((mpt)->sc_dmat, (mpt)->request_dmap,	\
284 	    (req)->req_pbuf - (mpt)->request_phys,		\
285 	    MPT_REQUEST_AREA, (ops))
286 
287 #define	mpt_read(mpt, reg)					\
288 	bus_space_read_4((mpt)->sc_st, (mpt)->sc_sh, (reg))
289 #define	mpt_write(mpt, reg, val)				\
290 	bus_space_write_4((mpt)->sc_st, (mpt)->sc_sh, (reg), (val))
291 
292 void	mpt_attach(mpt_softc_t *);
293 int	mpt_dma_mem_alloc(mpt_softc_t *);
294 int	mpt_intr(void *);
295 void	mpt_prt(mpt_softc_t *, const char *, ...);
296 
297 
298 #define	mpt_set_config_regs(mpt)				\
299 do {								\
300 	if ((mpt)->sc_set_config_regs != NULL)			\
301 		(*(mpt)->sc_set_config_regs)((mpt));		\
302 } while (/*CONSTCOND*/0)
303 
304 #endif /* _DEV_IC_MPT_NETBSD_H_ */
305