xref: /freebsd/sys/dev/isp/isp_freebsd.h (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
1 /* $FreeBSD$ */
2 /*
3  * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
4  * Copyright (c) 1997, 1998, 1999, 2000, 2001 by Matthew Jacob
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #ifndef	_ISP_FREEBSD_H
28 #define	_ISP_FREEBSD_H
29 
30 #define	ISP_PLATFORM_VERSION_MAJOR	5
31 #define	ISP_PLATFORM_VERSION_MINOR	9
32 
33 #include <sys/param.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/queue.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/proc.h>
43 #include <sys/bus.h>
44 
45 #include <machine/bus_memio.h>
46 #include <machine/bus_pio.h>
47 #include <machine/bus.h>
48 #include <machine/clock.h>
49 #include <machine/cpu.h>
50 
51 #include <cam/cam.h>
52 #include <cam/cam_debug.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_debug.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 
61 #include "opt_ddb.h"
62 #include "opt_isp.h"
63 /*
64  * Efficiency- get rid of SBus code && tests unless we need them.
65  */
66 #if	defined(__sparcv9__ ) || defined(__sparc__)
67 #define	ISP_SBUS_SUPPORTED	1
68 #else
69 #define	ISP_SBUS_SUPPORTED	0
70 #endif
71 
72 #define	HANDLE_LOOPSTATE_IN_OUTER_LAYERS	1
73 
74 typedef void ispfwfunc __P((int, int, int, u_int16_t **));
75 
76 #ifdef	ISP_TARGET_MODE
77 #define	ISP_TARGET_FUNCTIONS	1
78 #define	ATPDPSIZE	256
79 typedef struct {
80 	u_int32_t	orig_datalen;
81 	u_int32_t	bytes_xfered;
82 	u_int32_t	last_xframt;
83 	u_int32_t	tag;
84 } atio_private_data_t;
85 
86 typedef struct tstate {
87 	struct tstate *next;
88 	struct cam_path *owner;
89 	struct ccb_hdr_slist atios;
90 	struct ccb_hdr_slist inots;
91 	lun_id_t lun;
92 	int bus;
93 	u_int32_t hold;
94 } tstate_t;
95 
96 #define	LUN_HASH_SIZE			32
97 #define	LUN_HASH_FUNC(isp, port, lun)					\
98 	((IS_DUALBUS(isp)) ?						\
99 		(((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :	\
100 		((lun) & (LUN_HASH_SIZE - 1)))
101 #endif
102 
103 struct isposinfo {
104 	struct ispsoftc *	next;
105 	u_int64_t		default_port_wwn;
106 	u_int64_t		default_node_wwn;
107 	device_t		dev;
108 	struct cam_sim		*sim;
109 	struct cam_path		*path;
110 	struct cam_sim		*sim2;
111 	struct cam_path		*path2;
112 	struct intr_config_hook	ehook;
113 	u_int8_t		mboxwaiting;
114 	u_int8_t		simqfrozen;
115 	u_int8_t		drain;
116 	u_int8_t		intsok;
117 	struct mtx		lock;
118 	struct cv		kthread_cv;
119 	struct proc		*kproc;
120 #ifdef	ISP_TARGET_MODE
121 #define	TM_WANTED		0x80
122 #define	TM_BUSY			0x40
123 #define	TM_WILDCARD_ENABLED	0x02
124 #define	TM_TMODE_ENABLED	0x01
125 	struct cv		tgtcv0[2];	/* two busses */
126 	struct cv		tgtcv1[2];	/* two busses */
127 	u_int8_t		tmflags[2];	/* two busses */
128 	u_int8_t		rstatus[2];	/* two bussed */
129 	u_int16_t		rollinfo;
130 	tstate_t		tsdflt[2];	/* two busses */
131 	tstate_t		*lun_hash[LUN_HASH_SIZE];
132 	atio_private_data_t	atpdp[ATPDPSIZE];
133 #endif
134 };
135 
136 #define	isp_lock	isp_osinfo.lock
137 
138 /*
139  * Locking macros...
140  */
141 
142 #define	ISP_LOCK(x)		mtx_lock(&(x)->isp_lock)
143 #define	ISP_UNLOCK(x)		mtx_unlock(&(x)->isp_lock)
144 #define	ISPLOCK_2_CAMLOCK(isp)	\
145 	mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant)
146 #define	CAMLOCK_2_ISPLOCK(isp)	\
147 	mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock)
148 
149 /*
150  * Required Macros/Defines
151  */
152 
153 #define	INLINE			__inline
154 
155 #define	ISP2100_SCRLEN		0x400
156 
157 #define	MEMZERO			bzero
158 #define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
159 #define	SNPRINTF		snprintf
160 #define	STRNCAT			strncat
161 #define	USEC_DELAY		DELAY
162 #define	USEC_SLEEP(isp, x)		\
163 	if (isp->isp_osinfo.intsok)	\
164 		ISP_UNLOCK(isp);	\
165 	DELAY(x);			\
166 	if (isp->isp_osinfo.intsok)	\
167 		ISP_LOCK(isp)
168 
169 #define	NANOTIME_T		struct timespec
170 #define	GET_NANOTIME		nanotime
171 #define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
172 #define	NANOTIME_SUB		nanotime_sub
173 
174 #define	MAXISPREQUEST(isp)	256
175 
176 #if	defined(__alpha__)
177 #define	MEMORYBARRIER(isp, type, offset, size)	alpha_mb()
178 #elif	defined(__ia64__)
179 #define	MEMORYBARRIER(isp, type, offset, size)	\
180 	do { ia64_mf(); ia64_mf_a(); } while (0)
181 #else
182 #define	MEMORYBARRIER(isp, type, offset, size)
183 #endif
184 
185 #define	MBOX_ACQUIRE(isp)
186 #define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
187 #define	MBOX_NOTIFY_COMPLETE(isp)	\
188 	if (isp->isp_osinfo.mboxwaiting) { \
189 		isp->isp_osinfo.mboxwaiting = 0; \
190 		wakeup(&isp->isp_osinfo.mboxwaiting); \
191 	} \
192 	isp->isp_mboxbsy = 0
193 #define	MBOX_RELEASE(isp)
194 
195 #define	FC_SCRATCH_ACQUIRE(isp)
196 #define	FC_SCRATCH_RELEASE(isp)
197 
198 #ifndef	SCSI_GOOD
199 #define	SCSI_GOOD	SCSI_STATUS_OK
200 #endif
201 #ifndef	SCSI_CHECK
202 #define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
203 #endif
204 #ifndef	SCSI_BUSY
205 #define	SCSI_BUSY	SCSI_STATUS_BUSY
206 #endif
207 #ifndef	SCSI_QFULL
208 #define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
209 #endif
210 
211 #define	XS_T			struct ccb_scsiio
212 #define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
213 #define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
214 #define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
215 #define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
216 
217 #define	XS_CDBP(ccb)	\
218 	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
219 	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
220 
221 #define	XS_CDBLEN(ccb)		(ccb)->cdb_len
222 #define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
223 #define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
224 #define	XS_RESID(ccb)		(ccb)->resid
225 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
226 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
227 
228 #define	XS_SNSLEN(ccb)		\
229 	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
230 
231 #define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
232 #define	XS_TAG_P(ccb)	\
233 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
234 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
235 
236 #define	XS_TAG_TYPE(ccb)	\
237 	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
238 	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
239 
240 
241 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
242 				(ccb)->ccb_h.status |= v, \
243 				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
244 
245 #	define	HBA_NOERROR		CAM_REQ_INPROG
246 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
247 #	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
248 #	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
249 #	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
250 #	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
251 #	define	HBA_ABORTED		CAM_REQ_ABORTED
252 #	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
253 #	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
254 
255 
256 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
257 
258 #define	XS_NOERR(ccb)		\
259 	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
260 	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
261 
262 #define	XS_INITERR(ccb)		\
263 	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
264 
265 #define	XS_SAVE_SENSE(xs, sp)				\
266 	(xs)->ccb_h.status |= CAM_AUTOSNS_VALID,	\
267 	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
268 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
269 
270 #define	XS_SET_STATE_STAT(a, b, c)
271 
272 #define	DEFAULT_IID(x)		7
273 #define	DEFAULT_LOOPID(x)	109
274 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_node_wwn
275 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_port_wwn
276 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
277 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
278 
279 #if	BYTE_ORDER == BIG_ENDIAN
280 #ifdef	ISP_SBUS_SUPPORTED
281 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
282 #define	ISP_IOXPUT_16(isp, s, d)				\
283 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
284 #define	ISP_IOXPUT_32(isp, s, d)				\
285 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
286 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
287 #define	ISP_IOXGET_16(isp, s, d)				\
288 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
289 	*((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
290 #define	ISP_IOXGET_32(isp, s, d)				\
291 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
292 	*((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
293 #else
294 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
295 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
296 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
297 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
298 #define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((u_int16_t *)s))
299 #define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((u_int32_t *)s))
300 #endif
301 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
302 #else
303 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
304 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
305 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
306 #define	ISP_IOXGET_8(isp, s, d)		d = *(s)
307 #define	ISP_IOXGET_16(isp, s, d)	d = *(s)
308 #define	ISP_IOXGET_32(isp, s, d)	d = *(s)
309 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
310 #endif
311 
312 /*
313  * Includes of common header files
314  */
315 
316 #include <dev/isp/ispreg.h>
317 #include <dev/isp/ispvar.h>
318 #include <dev/isp/ispmbox.h>
319 
320 /*
321  * isp_osinfo definiitions && shorthand
322  */
323 #define	SIMQFRZ_RESOURCE	0x1
324 #define	SIMQFRZ_LOOPDOWN	0x2
325 #define	SIMQFRZ_TIMED		0x4
326 
327 #define	isp_sim		isp_osinfo.sim
328 #define	isp_path	isp_osinfo.path
329 #define	isp_sim2	isp_osinfo.sim2
330 #define	isp_path2	isp_osinfo.path2
331 #define	isp_dev		isp_osinfo.dev
332 
333 /*
334  * prototypes for isp_pci && isp_freebsd to share
335  */
336 extern void isp_attach(struct ispsoftc *);
337 extern void isp_uninit(struct ispsoftc *);
338 
339 /*
340  * Platform private flags
341  */
342 #define	ISP_SPRIV_ERRSET	0x1
343 #define	ISP_SPRIV_INWDOG	0x2
344 #define	ISP_SPRIV_GRACE		0x4
345 #define	ISP_SPRIV_DONE		0x8
346 
347 #define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
348 #define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
349 #define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
350 
351 #define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
352 #define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
353 #define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
354 
355 #define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
356 #define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
357 #define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
358 
359 #define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
360 /*
361  * Platform specific inline functions
362  */
363 
364 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
365 static INLINE void
366 isp_mbox_wait_complete(struct ispsoftc *isp)
367 {
368 	if (isp->isp_osinfo.intsok) {
369 		int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz;
370 		isp->isp_osinfo.mboxwaiting = 1;
371 		(void) msleep(&isp->isp_osinfo.mboxwaiting,
372 		    &isp->isp_lock, PRIBIO, "isp_mboxwaiting", lim);
373 		if (isp->isp_mboxbsy != 0) {
374 			isp_prt(isp, ISP_LOGWARN,
375 			    "Interrupting Mailbox Command (0x%x) Timeout",
376 			    isp->isp_lastmbxcmd);
377 			isp->isp_mboxbsy = 0;
378 		}
379 		isp->isp_osinfo.mboxwaiting = 0;
380 	} else {
381 		int lim = ((isp->isp_mbxwrk0)? 240 : 60) * 10000;
382 		int j;
383 		for (j = 0; j < lim; j++) {
384 			u_int16_t isr, sema, mbox;
385 			if (isp->isp_mboxbsy == 0) {
386 				break;
387 			}
388 			if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
389 				isp_intr(isp, isr, sema, mbox);
390 				if (isp->isp_mboxbsy == 0) {
391 					break;
392 				}
393 			}
394 			USEC_DELAY(500);
395 		}
396 		if (isp->isp_mboxbsy != 0) {
397 			isp_prt(isp, ISP_LOGWARN,
398 			    "Polled Mailbox Command (0x%x) Timeout",
399 			    isp->isp_lastmbxcmd);
400 		}
401 	}
402 }
403 
404 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
405 static INLINE u_int64_t
406 nanotime_sub(struct timespec *b, struct timespec *a)
407 {
408 	u_int64_t elapsed;
409 	struct timespec x = *b;
410 	timespecsub(&x, a);
411 	elapsed = GET_NANOSEC(&x);
412 	if (elapsed == 0)
413 		elapsed++;
414 	return (elapsed);
415 }
416 
417 static INLINE char *strncat(char *, const char *, size_t);
418 static INLINE char *
419 strncat(char *d, const char *s, size_t c)
420 {
421         char *t = d;
422 
423         if (c) {
424                 while (*d)
425                         d++;
426                 while ((*d++ = *s++)) {
427                         if (--c == 0) {
428                                 *d = '\0';
429                                 break;
430                         }
431                 }
432         }
433         return (t);
434 }
435 
436 /*
437  * Common inline functions
438  */
439 
440 #include <dev/isp/isp_inline.h>
441 #endif	/* _ISP_FREEBSD_H */
442