xref: /netbsd-src/sys/dev/ic/isp_netbsd.h (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /* $NetBSD: isp_netbsd.h,v 1.47 2001/12/14 00:13:45 mjacob Exp $ */
2 /*
3  * This driver, which is contained in NetBSD in the files:
4  *
5  *	sys/dev/ic/isp.c
6  *	sys/dev/ic/isp_inline.h
7  *	sys/dev/ic/isp_netbsd.c
8  *	sys/dev/ic/isp_netbsd.h
9  *	sys/dev/ic/isp_target.c
10  *	sys/dev/ic/isp_target.h
11  *	sys/dev/ic/isp_tpublic.h
12  *	sys/dev/ic/ispmbox.h
13  *	sys/dev/ic/ispreg.h
14  *	sys/dev/ic/ispvar.h
15  *	sys/microcode/isp/asm_sbus.h
16  *	sys/microcode/isp/asm_1040.h
17  *	sys/microcode/isp/asm_1080.h
18  *	sys/microcode/isp/asm_12160.h
19  *	sys/microcode/isp/asm_2100.h
20  *	sys/microcode/isp/asm_2200.h
21  *	sys/pci/isp_pci.c
22  *	sys/sbus/isp_sbus.c
23  *
24  * Is being actively maintained by Matthew Jacob (mjacob@netbsd.org).
25  * This driver also is shared source with FreeBSD, OpenBSD, Linux, Solaris,
26  * Linux versions. This tends to be an interesting maintenance problem.
27  *
28  * Please coordinate with Matthew Jacob on changes you wish to make here.
29  */
30 /*
31  * NetBSD Specific definitions for the Qlogic ISP Host Adapter
32  * Matthew Jacob <mjacob@nas.nasa.gov>
33  */
34 /*
35  * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. The name of the author may not be used to endorse or promote products
47  *    derived from this software without specific prior written permission
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 #ifndef	_ISP_NETBSD_H
61 #define	_ISP_NETBSD_H
62 
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/errno.h>
68 #include <sys/ioctl.h>
69 #include <sys/device.h>
70 #include <sys/malloc.h>
71 #include <sys/buf.h>
72 #include <sys/proc.h>
73 #include <sys/user.h>
74 #include <sys/kthread.h>
75 
76 #include <machine/bus.h>
77 
78 #include <dev/scsipi/scsi_all.h>
79 #include <dev/scsipi/scsipi_all.h>
80 #include <dev/scsipi/scsiconf.h>
81 
82 #include <dev/scsipi/scsi_message.h>
83 #include <dev/scsipi/scsipi_debug.h>
84 
85 #include "opt_isp.h"
86 
87 /*
88  * Efficiency- get rid of SBus code && tests unless we need them.
89  */
90 #if	defined(__sparcv9__ ) || defined(__sparc__)
91 #define	ISP_SBUS_SUPPORTED	1
92 #else
93 #define	ISP_SBUS_SUPPORTED	0
94 #endif
95 
96 #define	ISP_PLATFORM_VERSION_MAJOR	2
97 #define	ISP_PLATFORM_VERSION_MINOR	1
98 
99 struct isposinfo {
100 	struct device		_dev;
101 	struct scsipi_channel	_chan;
102 	struct scsipi_channel	_chan_b;
103 	struct scsipi_adapter   _adapter;
104 	bus_dma_tag_t		dmatag;
105 	bus_dmamap_t		rqdmap;
106 	bus_dmamap_t		rsdmap;
107 	bus_dmamap_t		scdmap;	/* FC only */
108 	int			splsaved;
109 	int			mboxwaiting;
110 	u_int32_t		islocked;
111 	u_int32_t		onintstack;
112 	unsigned int		: 26,
113 		loop_checked	: 1,
114 		mbox_wanted	: 1,
115 		mbox_locked	: 1,
116 		no_mbox_ints	: 1,
117 		paused		: 1,
118 		blocked		: 1;
119 	union {
120 		u_int64_t	_wwn;
121 		u_int16_t	_discovered[2];
122 #define	wwn_seed	un._wwn
123 #define	discovered	un._discovered
124 	} un;
125 	u_int32_t		threadwork;
126 	struct proc *		thread;
127 };
128 #define	isp_dmatag		isp_osinfo.dmatag
129 #define	isp_rqdmap		isp_osinfo.rqdmap
130 #define	isp_rsdmap		isp_osinfo.rsdmap
131 #define	isp_scdmap		isp_osinfo.scdmap
132 
133 #define	ISP_MUSTPOLL(isp)	\
134 	(isp->isp_osinfo.onintstack || isp->isp_osinfo.no_mbox_ints)
135 
136 #define	HANDLE_LOOPSTATE_IN_OUTER_LAYERS	1
137 
138 /*
139  * Required Macros/Defines
140  */
141 
142 #define	INLINE			__inline
143 
144 #define	ISP2100_SCRLEN		0x400
145 
146 #define	MEMZERO(dst, amt)	memset((dst), 0, (amt))
147 #define	MEMCPY(dst, src, amt)	memcpy((dst), (src), (amt))
148 #define	SNPRINTF		snprintf
149 #define	STRNCAT			strncat
150 #define	USEC_DELAY		DELAY
151 #define	USEC_SLEEP(isp, x)		\
152 	if (!ISP_MUSTPOLL(isp))		\
153 		ISP_UNLOCK(isp);	\
154 	DELAY(x);			\
155 	if (!ISP_MUSTPOLL(isp))		\
156 		ISP_LOCK(isp)
157 
158 #define	NANOTIME_T		struct timeval
159 #define	GET_NANOTIME		microtime
160 #define	GET_NANOSEC(x)		(((x)->tv_sec * 1000000 + (x)->tv_usec) * 1000)
161 #define	NANOTIME_SUB		isp_microtime_sub
162 
163 #define	MAXISPREQUEST(isp)	256
164 
165 
166 #define	MEMORYBARRIER(isp, type, offset, size)			\
167 switch (type) {							\
168 case SYNC_REQUEST:						\
169 {								\
170 	off_t off = (off_t) offset * QENTRY_LEN;		\
171 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_rqdmap,	\
172 	    off, size, BUS_DMASYNC_PREWRITE);			\
173 	break;							\
174 }								\
175 case SYNC_RESULT:						\
176 {								\
177 	off_t off = (off_t) offset * QENTRY_LEN;		\
178 	off += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));		\
179 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_rsdmap,	\
180 	    off, size, BUS_DMASYNC_POSTREAD);			\
181 	break;							\
182 }								\
183 case SYNC_SFORDEV:						\
184 {								\
185 	off_t off =						\
186 	    ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) +		\
187 	    ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp)) + offset;	\
188 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_scdmap,	\
189 	    off, size, BUS_DMASYNC_PREWRITE);			\
190 	break;							\
191 }								\
192 case SYNC_SFORCPU:						\
193 {								\
194 	off_t off =						\
195 	    ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) +		\
196 	    ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp)) + offset;	\
197 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_scdmap,	\
198 	    off, size, BUS_DMASYNC_POSTREAD);			\
199 	break;							\
200 }								\
201 case SYNC_REG:							\
202 default:							\
203 	break;							\
204 }
205 
206 #define	MBOX_ACQUIRE(isp)						\
207 	if (isp->isp_osinfo.onintstack) {				\
208 		if (isp->isp_osinfo.mbox_locked) {			\
209 			mbp->param[0] = MBOX_HOST_INTERFACE_ERROR;	\
210 			isp_prt(isp, ISP_LOGERR, "cannot acquire MBOX lock"); \
211 			return;						\
212 		}							\
213 	} else {							\
214 		while (isp->isp_osinfo.mbox_locked) {			\
215 			isp->isp_osinfo.mbox_wanted = 1;		\
216 			(void) tsleep(&isp->isp_osinfo.mboxwaiting+1,	\
217 			    PRIBIO, "mbox_acquire", 0);			\
218 		}							\
219 	}								\
220 	isp->isp_osinfo.mbox_locked = 1
221 
222 #define	MBOX_WAIT_COMPLETE	isp_wait_complete
223 
224 #define	MBOX_NOTIFY_COMPLETE(isp)					\
225 	if (isp->isp_osinfo.mboxwaiting) {				\
226                 isp->isp_osinfo.mboxwaiting = 0;			\
227                 wakeup(&isp->isp_osinfo.mboxwaiting);			\
228         }								\
229 	isp->isp_mboxbsy = 0
230 
231 #define	MBOX_RELEASE(isp)						\
232 	if (isp->isp_osinfo.mbox_wanted) {				\
233 		isp->isp_osinfo.mbox_wanted = 0;			\
234 		wakeup(&isp->isp_osinfo.mboxwaiting+1);			\
235 	}								\
236 	isp->isp_osinfo.mbox_locked = 0
237 
238 #ifndef	SCSI_GOOD
239 #define	SCSI_GOOD	0x0
240 #endif
241 #ifndef	SCSI_CHECK
242 #define	SCSI_CHECK	0x2
243 #endif
244 #ifndef	SCSI_BUSY
245 #define	SCSI_BUSY	0x8
246 #endif
247 #ifndef	SCSI_QFULL
248 #define	SCSI_QFULL	0x28
249 #endif
250 
251 #define	XS_T			struct scsipi_xfer
252 #define	XS_CHANNEL(xs)		((int) (xs)->xs_periph->periph_channel->chan_channel)
253 #define	XS_ISP(xs)		\
254 	((void *)(xs)->xs_periph->periph_channel->chan_adapter->adapt_dev)
255 #define	XS_LUN(xs)		((int) (xs)->xs_periph->periph_lun)
256 #define	XS_TGT(xs)		((int) (xs)->xs_periph->periph_target)
257 #define	XS_CDBP(xs)		((caddr_t) (xs)->cmd)
258 #define	XS_CDBLEN(xs)		(xs)->cmdlen
259 #define	XS_XFRLEN(xs)		(xs)->datalen
260 #define	XS_TIME(xs)		(xs)->timeout
261 #define	XS_RESID(xs)		(xs)->resid
262 #define	XS_STSP(xs)		(&(xs)->status)
263 #define	XS_SNSP(xs)		(&(xs)->sense.scsi_sense)
264 #define	XS_SNSLEN(xs)		(sizeof (xs)->sense)
265 #define	XS_SNSKEY(xs)		((xs)->sense.scsi_sense.flags)
266 #define	XS_TAG_P(ccb)		(((xs)->xs_control & XS_CTL_POLL) != 0)
267 #define	XS_TAG_TYPE(xs)	\
268 	(((xs)->xs_control & XS_CTL_URGENT) ? REQFLAG_HTAG : \
269 	((xs)->bp && xs->bp->b_flags & B_ASYNC) ? REQFLAG_STAG : REQFLAG_OTAG)
270 
271 #define	XS_SETERR(xs, v)	(xs)->error = v
272 
273 #	define	HBA_NOERROR		XS_NOERROR
274 #	define	HBA_BOTCH		XS_DRIVER_STUFFUP
275 #	define	HBA_CMDTIMEOUT		XS_TIMEOUT
276 #	define	HBA_SELTIMEOUT		XS_SELTIMEOUT
277 #	define	HBA_TGTBSY		XS_BUSY
278 #	define	HBA_BUSRESET		XS_RESET
279 #	define	HBA_ABORTED		XS_DRIVER_STUFFUP
280 #	define	HBA_DATAOVR		XS_DRIVER_STUFFUP
281 #	define	HBA_ARQFAIL		XS_DRIVER_STUFFUP
282 
283 #define	XS_ERR(xs)		(xs)->error
284 
285 #define	XS_NOERR(xs)		(xs)->error == XS_NOERROR
286 
287 #define	XS_INITERR(xs)		(xs)->error = 0, XS_CMD_S_CLEAR(xs)
288 
289 #define	XS_SAVE_SENSE(xs, sp)				\
290 	if (xs->error == XS_NOERROR) {			\
291 		xs->error = XS_SENSE;			\
292 	}						\
293 	memcpy(&(xs)->sense, sp->req_sense_data,	\
294 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
295 
296 #define	XS_SET_STATE_STAT(a, b, c)
297 
298 #define	DEFAULT_IID(x)		7
299 #define	DEFAULT_LOOPID(x)	108
300 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.un._wwn
301 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.un._wwn
302 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
303 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
304 
305 #if	_BYTE_ORDER == _BIG_ENDIAN
306 #ifdef	ISP_SBUS_SUPPORTED
307 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
308 #define	ISP_IOXPUT_16(isp, s, d)				\
309 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
310 #define	ISP_IOXPUT_32(isp, s, d)				\
311 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
312 
313 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
314 #define	ISP_IOXGET_16(isp, s, d)				\
315 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
316 	*((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
317 #define	ISP_IOXGET_32(isp, s, d)				\
318 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
319 	*((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
320 #else
321 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
322 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
323 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
324 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
325 #define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((u_int16_t *)s))
326 #define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((u_int32_t *)s))
327 #endif
328 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
329 #else
330 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
331 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
332 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
333 #define	ISP_IOXGET_8(isp, s, d)		d = *(s)
334 #define	ISP_IOXGET_16(isp, s, d)	d = *(s)
335 #define	ISP_IOXGET_32(isp, s, d)	d = *(s)
336 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
337 #endif
338 
339 /*
340  * Includes of common header files
341  */
342 
343 #include <dev/ic/ispreg.h>
344 #include <dev/ic/ispvar.h>
345 #include <dev/ic/ispmbox.h>
346 #include <dev/ic/isp_ioctl.h>
347 
348 /*
349  * isp_osinfo definitions, extensions and shorthand.
350  */
351 #define	isp_name	isp_osinfo._dev.dv_xname
352 #define	isp_unit	isp_osinfo._dev.dv_unit
353 #define	isp_chanA	isp_osinfo._chan
354 #define	isp_chanB	isp_osinfo._chan_b
355 
356 
357 /*
358  * Driver prototypes..
359  */
360 void isp_attach(struct ispsoftc *);
361 void isp_uninit(struct ispsoftc *);
362 
363 static INLINE void isp_lock(struct ispsoftc *);
364 static INLINE void isp_unlock(struct ispsoftc *);
365 static INLINE char *strncat(char *, const char *, size_t);
366 static INLINE u_int64_t
367 isp_microtime_sub(struct timeval *, struct timeval *);
368 static void isp_wait_complete(struct ispsoftc *);
369 
370 /*
371  * Driver wide data...
372  */
373 
374 /*
375  * Locking macros...
376  */
377 #define	ISP_LOCK		isp_lock
378 #define	ISP_UNLOCK		isp_unlock
379 #define	ISP_ILOCK(x)		isp_lock(x); isp->isp_osinfo.onintstack++
380 #define	ISP_IUNLOCK(x)		isp->isp_osinfo.onintstack--; isp_unlock(x)
381 
382 /*
383  * Platform private flags
384  */
385 
386 #define	XS_PSTS_INWDOG		0x10000000
387 #define	XS_PSTS_GRACE		0x20000000
388 #define	XS_PSTS_ALL		0x30000000
389 
390 #define	XS_CMD_S_WDOG(xs)	(xs)->xs_status |= XS_PSTS_INWDOG
391 #define	XS_CMD_C_WDOG(xs)	(xs)->xs_status &= ~XS_PSTS_INWDOG
392 #define	XS_CMD_WDOG_P(xs)	(((xs)->xs_status & XS_PSTS_INWDOG) != 0)
393 
394 #define	XS_CMD_S_GRACE(xs)	(xs)->xs_status |= XS_PSTS_GRACE
395 #define	XS_CMD_C_GRACE(xs)	(xs)->xs_status &= ~XS_PSTS_GRACE
396 #define	XS_CMD_GRACE_P(xs)	(((xs)->xs_status & XS_PSTS_GRACE) != 0)
397 
398 #define	XS_CMD_S_DONE(xs)	(xs)->xs_status |= XS_STS_DONE
399 #define	XS_CMD_C_DONE(xs)	(xs)->xs_status &= ~XS_STS_DONE
400 #define	XS_CMD_DONE_P(xs)	(((xs)->xs_status & XS_STS_DONE) != 0)
401 
402 #define	XS_CMD_S_CLEAR(xs)	(xs)->xs_status &= ~XS_PSTS_ALL
403 
404 /*
405  * Platform specific 'inline' or support functions
406  */
407 static INLINE void
408 isp_lock(struct ispsoftc *isp)
409 {
410 	int s = splbio();
411 	if (isp->isp_osinfo.islocked++ == 0) {
412 		isp->isp_osinfo.splsaved = s;
413 	} else {
414 		splx(s);
415 	}
416 }
417 
418 static INLINE void
419 isp_unlock(struct ispsoftc *isp)
420 {
421 	if (isp->isp_osinfo.islocked-- <= 1) {
422 		isp->isp_osinfo.islocked = 0;
423 		splx(isp->isp_osinfo.splsaved);
424 	}
425 }
426 
427 static INLINE char *
428 strncat(char *d, const char *s, size_t c)
429 {
430         char *t = d;
431 
432         if (c) {
433                 while (*d)
434                         d++;
435                 while ((*d++ = *s++)) {
436                         if (--c == 0) {
437                                 *d = '\0';
438                                 break;
439                         }
440                 }
441         }
442         return (t);
443 }
444 
445 static INLINE u_int64_t
446 isp_microtime_sub(struct timeval *b, struct timeval *a)
447 {
448 	struct timeval x;
449 	u_int64_t elapsed;
450 	timersub(b, a, &x);
451 	elapsed = GET_NANOSEC(&x);
452 	if (elapsed == 0)
453 		elapsed++;
454 	return (elapsed);
455 }
456 
457 static INLINE void
458 isp_wait_complete(struct ispsoftc *isp)
459 {
460 	if (isp->isp_osinfo.onintstack || isp->isp_osinfo.no_mbox_ints) {
461 		int usecs = 0;
462 		/*
463 		 * For sanity's sake, we don't delay longer
464 		 * than 5 seconds for polled commands.
465 		 */
466 		while (usecs < 5 * 1000000) {
467 			u_int16_t isr, sema, mbox;
468 			if (isp->isp_mboxbsy == 0) {
469 				break;
470 			}
471 			if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
472 				isp_intr(isp, isr, sema, mbox);
473 				if (isp->isp_mboxbsy == 0) {
474 					break;
475 				}
476 			}
477 			USEC_DELAY(500);
478 			usecs += 500;
479 		}
480 		if (isp->isp_mboxbsy != 0) {
481 			isp_prt(isp, ISP_LOGWARN,
482 			    "Polled Mailbox Command (0x%x) Timeout",
483 			    isp->isp_lastmbxcmd);
484 		}
485 	} else {
486 		int rv = 0;
487                 isp->isp_osinfo.mboxwaiting = 1;
488                 while (isp->isp_osinfo.mboxwaiting && rv == 0) {
489 			static const struct timeval dtime = { 5, 0 };
490 			int timo;
491 			struct timeval tv;
492 			microtime(&tv);
493 			timeradd(&tv, &dtime, &tv);
494 			if ((timo = hzto(&tv)) == 0) {
495 				timo = 1;
496 			}
497 			rv = tsleep(&isp->isp_osinfo.mboxwaiting,
498 			    PRIBIO, "isp_mboxcmd", timo);
499 		}
500 		if (rv == EWOULDBLOCK) {
501 			isp->isp_mboxbsy = 0;
502 			isp->isp_osinfo.mboxwaiting = 0;
503 			isp_prt(isp, ISP_LOGWARN,
504 			    "Interrupting Mailbox Command (0x%x) Timeout",
505 			    isp->isp_lastmbxcmd);
506 		}
507 	}
508 }
509 
510 
511 /*
512  * Common inline functions
513  */
514 #include <dev/ic/isp_inline.h>
515 
516 #if	!defined(ISP_DISABLE_FW) && !defined(ISP_COMPILE_FW)
517 #define	ISP_COMPILE_FW	1
518 #endif
519 #endif	/* _ISP_NETBSD_H */
520