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