xref: /freebsd/sys/dev/isp/isp_freebsd.h (revision 11f0b352e05306cf6f1f85e9087022c0a92624a3)
1 /* $FreeBSD$ */
2 /*
3  * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
4  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 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/systm.h>
35 #include <sys/endian.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	_MACHINE_ARCH == sparc64
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(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	: 16,
84 			lun	: 13,	/* not enough */
85 			state	: 3;
86 } atio_private_data_t;
87 #define	ATPD_STATE_FREE			0
88 #define	ATPD_STATE_ATIO			1
89 #define	ATPD_STATE_CAM			2
90 #define	ATPD_STATE_CTIO			3
91 #define	ATPD_STATE_LAST_CTIO		4
92 #define	ATPD_STATE_PDON			5
93 
94 typedef struct tstate {
95 	struct tstate *next;
96 	struct cam_path *owner;
97 	struct ccb_hdr_slist atios;
98 	struct ccb_hdr_slist inots;
99 	lun_id_t lun;
100 	int bus;
101 	u_int32_t hold;
102 	int atio_count;
103 } tstate_t;
104 
105 #define	LUN_HASH_SIZE			32
106 #define	LUN_HASH_FUNC(isp, port, lun)					\
107 	((IS_DUALBUS(isp)) ?						\
108 		(((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :	\
109 		((lun) & (LUN_HASH_SIZE - 1)))
110 #endif
111 
112 struct isposinfo {
113 	struct ispsoftc *	next;
114 	u_int64_t		default_port_wwn;
115 	u_int64_t		default_node_wwn;
116 	u_int32_t		default_id;
117 	device_t		dev;
118 	struct cam_sim		*sim;
119 	struct cam_path		*path;
120 	struct cam_sim		*sim2;
121 	struct cam_path		*path2;
122 	struct intr_config_hook	ehook;
123 	u_int8_t		: 1,
124 		fcbsy		: 1,
125 		ktmature	: 1,
126 		mboxwaiting	: 1,
127 		intsok		: 1,
128 		simqfrozen	: 3;
129 	struct mtx		lock;
130 	struct cv		kthread_cv;
131 	struct proc		*kproc;
132 	bus_dma_tag_t		cdmat;
133 	bus_dmamap_t		cdmap;
134 #define	isp_cdmat		isp_osinfo.cdmat
135 #define	isp_cdmap		isp_osinfo.cdmap
136 #ifdef	ISP_TARGET_MODE
137 #define	TM_WANTED		0x80
138 #define	TM_BUSY			0x40
139 #define	TM_WILDCARD_ENABLED	0x02
140 #define	TM_TMODE_ENABLED	0x01
141 	struct cv		tgtcv0[2];	/* two busses */
142 	struct cv		tgtcv1[2];	/* two busses */
143 	u_int8_t		tmflags[2];	/* two busses */
144 	u_int8_t		rstatus[2];	/* two bussed */
145 	u_int16_t		rollinfo;
146 	tstate_t		tsdflt[2];	/* two busses */
147 	tstate_t		*lun_hash[LUN_HASH_SIZE];
148 	atio_private_data_t	atpdp[ATPDPSIZE];
149 #endif
150 };
151 
152 #define	isp_lock	isp_osinfo.lock
153 
154 /*
155  * Locking macros...
156  */
157 
158 #define	ISP_LOCK(x)		mtx_lock(&(x)->isp_lock)
159 #define	ISP_UNLOCK(x)		mtx_unlock(&(x)->isp_lock)
160 #define	ISPLOCK_2_CAMLOCK(isp)	\
161 	mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant)
162 #define	CAMLOCK_2_ISPLOCK(isp)	\
163 	mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock)
164 
165 /*
166  * Required Macros/Defines
167  */
168 
169 #define	INLINE			__inline
170 
171 #define	ISP2100_SCRLEN		0x800
172 
173 #define	MEMZERO			bzero
174 #define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
175 #define	SNPRINTF		snprintf
176 #define	STRNCAT			strncat
177 #define	USEC_DELAY		DELAY
178 #define	USEC_SLEEP(isp, x)		\
179 	if (isp->isp_osinfo.intsok)	\
180 		ISP_UNLOCK(isp);	\
181 	DELAY(x);			\
182 	if (isp->isp_osinfo.intsok)	\
183 		ISP_LOCK(isp)
184 
185 #define	NANOTIME_T		struct timespec
186 #define	GET_NANOTIME		nanotime
187 #define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
188 #define	NANOTIME_SUB		nanotime_sub
189 
190 #define	MAXISPREQUEST(isp)	((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
191 
192 #define	MEMORYBARRIER(isp, type, offset, size)			\
193 switch (type) {							\
194 case SYNC_SFORDEV:						\
195 case SYNC_REQUEST:						\
196 	bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap, 	\
197 	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
198 	break;							\
199 case SYNC_SFORCPU:						\
200 case SYNC_RESULT:						\
201 	bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap,		\
202 	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
203 	break;							\
204 default:							\
205 	break;							\
206 }
207 
208 #define	MBOX_ACQUIRE(isp)
209 #define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
210 #define	MBOX_NOTIFY_COMPLETE(isp)	\
211 	if (isp->isp_osinfo.mboxwaiting) { \
212 		isp->isp_osinfo.mboxwaiting = 0; \
213 		wakeup(&isp->isp_mbxworkp); \
214 	} \
215 	isp->isp_mboxbsy = 0
216 #define	MBOX_RELEASE(isp)
217 
218 #define	FC_SCRATCH_ACQUIRE(isp)						\
219 	if (isp->isp_osinfo.fcbsy) {					\
220 		isp_prt(isp, ISP_LOGWARN,				\
221 		    "FC scratch area busy (line %d)!", __LINE__);	\
222 	} else								\
223 		isp->isp_osinfo.fcbsy = 1
224 #define	FC_SCRATCH_RELEASE(isp)		 isp->isp_osinfo.fcbsy = 0
225 
226 #ifndef	SCSI_GOOD
227 #define	SCSI_GOOD	SCSI_STATUS_OK
228 #endif
229 #ifndef	SCSI_CHECK
230 #define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
231 #endif
232 #ifndef	SCSI_BUSY
233 #define	SCSI_BUSY	SCSI_STATUS_BUSY
234 #endif
235 #ifndef	SCSI_QFULL
236 #define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
237 #endif
238 
239 #define	XS_T			struct ccb_scsiio
240 #define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
241 #define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
242 #define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
243 #define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
244 
245 #define	XS_CDBP(ccb)	\
246 	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
247 	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
248 
249 #define	XS_CDBLEN(ccb)		(ccb)->cdb_len
250 #define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
251 #define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
252 #define	XS_RESID(ccb)		(ccb)->resid
253 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
254 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
255 
256 #define	XS_SNSLEN(ccb)		\
257 	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
258 
259 #define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
260 #define	XS_TAG_P(ccb)	\
261 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
262 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
263 
264 #define	XS_TAG_TYPE(ccb)	\
265 	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
266 	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
267 
268 
269 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
270 				(ccb)->ccb_h.status |= v, \
271 				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
272 
273 #	define	HBA_NOERROR		CAM_REQ_INPROG
274 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
275 #	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
276 #	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
277 #	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
278 #	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
279 #	define	HBA_ABORTED		CAM_REQ_ABORTED
280 #	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
281 #	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
282 
283 
284 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
285 
286 #define	XS_NOERR(ccb)		\
287 	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
288 	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
289 
290 #define	XS_INITERR(ccb)		\
291 	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
292 
293 #define	XS_SAVE_SENSE(xs, sp)				\
294 	(xs)->ccb_h.status |= CAM_AUTOSNS_VALID,	\
295 	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
296 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
297 
298 #define	XS_SET_STATE_STAT(a, b, c)
299 
300 #define	DEFAULT_IID(x)		(isp)->isp_osinfo.default_id
301 #define	DEFAULT_LOOPID(x)	(isp)->isp_osinfo.default_id
302 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_node_wwn
303 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_port_wwn
304 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
305 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
306 
307 #if	BYTE_ORDER == BIG_ENDIAN
308 #ifdef	ISP_SBUS_SUPPORTED
309 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
310 #define	ISP_IOXPUT_16(isp, s, d)				\
311 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
312 #define	ISP_IOXPUT_32(isp, s, d)				\
313 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
314 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
315 #define	ISP_IOXGET_16(isp, s, d)				\
316 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
317 	*((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
318 #define	ISP_IOXGET_32(isp, s, d)				\
319 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
320 	*((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
321 #else
322 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
323 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
324 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
325 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
326 #define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((u_int16_t *)s))
327 #define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((u_int32_t *)s))
328 #endif
329 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
330 #else
331 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
332 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
333 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
334 #define	ISP_IOXGET_8(isp, s, d)		d = *(s)
335 #define	ISP_IOXGET_16(isp, s, d)	d = *(s)
336 #define	ISP_IOXGET_32(isp, s, d)	d = *(s)
337 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
338 #endif
339 
340 /*
341  * Includes of common header files
342  */
343 
344 #include <dev/isp/ispreg.h>
345 #include <dev/isp/ispvar.h>
346 #include <dev/isp/ispmbox.h>
347 
348 /*
349  * isp_osinfo definiitions && shorthand
350  */
351 #define	SIMQFRZ_RESOURCE	0x1
352 #define	SIMQFRZ_LOOPDOWN	0x2
353 #define	SIMQFRZ_TIMED		0x4
354 
355 #define	isp_sim		isp_osinfo.sim
356 #define	isp_path	isp_osinfo.path
357 #define	isp_sim2	isp_osinfo.sim2
358 #define	isp_path2	isp_osinfo.path2
359 #define	isp_dev		isp_osinfo.dev
360 
361 /*
362  * prototypes for isp_pci && isp_freebsd to share
363  */
364 extern void isp_attach(struct ispsoftc *);
365 extern void isp_uninit(struct ispsoftc *);
366 
367 /*
368  * driver global data
369  */
370 extern int isp_announced;
371 
372 /*
373  * Platform private flags
374  */
375 #define	ISP_SPRIV_ERRSET	0x1
376 #define	ISP_SPRIV_INWDOG	0x2
377 #define	ISP_SPRIV_GRACE		0x4
378 #define	ISP_SPRIV_DONE		0x8
379 
380 #define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
381 #define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
382 #define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
383 
384 #define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
385 #define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
386 #define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
387 
388 #define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
389 #define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
390 #define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
391 
392 #define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
393 /*
394  * Platform specific inline functions
395  */
396 
397 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
398 static INLINE void
399 isp_mbox_wait_complete(struct ispsoftc *isp)
400 {
401 	if (isp->isp_osinfo.intsok) {
402 		int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz;
403 		isp->isp_osinfo.mboxwaiting = 1;
404 		(void) msleep(&isp->isp_mbxworkp,
405 		    &isp->isp_lock, PRIBIO, "isp_mboxwaiting", lim);
406 		if (isp->isp_mboxbsy != 0) {
407 			isp_prt(isp, ISP_LOGWARN,
408 			    "Interrupting Mailbox Command (0x%x) Timeout",
409 			    isp->isp_lastmbxcmd);
410 			isp->isp_mboxbsy = 0;
411 		}
412 		isp->isp_osinfo.mboxwaiting = 0;
413 	} else {
414 		int lim = ((isp->isp_mbxwrk0)? 240 : 60) * 10000;
415 		int j;
416 		for (j = 0; j < lim; j++) {
417 			u_int16_t isr, sema, mbox;
418 			if (isp->isp_mboxbsy == 0) {
419 				break;
420 			}
421 			if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
422 				isp_intr(isp, isr, sema, mbox);
423 				if (isp->isp_mboxbsy == 0) {
424 					break;
425 				}
426 			}
427 			USEC_DELAY(500);
428 		}
429 		if (isp->isp_mboxbsy != 0) {
430 			isp_prt(isp, ISP_LOGWARN,
431 			    "Polled Mailbox Command (0x%x) Timeout",
432 			    isp->isp_lastmbxcmd);
433 		}
434 	}
435 }
436 
437 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
438 static INLINE u_int64_t
439 nanotime_sub(struct timespec *b, struct timespec *a)
440 {
441 	u_int64_t elapsed;
442 	struct timespec x = *b;
443 	timespecsub(&x, a);
444 	elapsed = GET_NANOSEC(&x);
445 	if (elapsed == 0)
446 		elapsed++;
447 	return (elapsed);
448 }
449 
450 static INLINE char *strncat(char *, const char *, size_t);
451 static INLINE char *
452 strncat(char *d, const char *s, size_t c)
453 {
454         char *t = d;
455 
456         if (c) {
457                 while (*d)
458                         d++;
459                 while ((*d++ = *s++)) {
460                         if (--c == 0) {
461                                 *d = '\0';
462                                 break;
463                         }
464                 }
465         }
466         return (t);
467 }
468 
469 /*
470  * Common inline functions
471  */
472 
473 #include <dev/isp/isp_inline.h>
474 #endif	/* _ISP_FREEBSD_H */
475