xref: /netbsd/sys/dev/ic/isp_netbsd.h (revision bf9ec67e)
1 /* $NetBSD: isp_netbsd.h,v 1.50 2002/04/04 23:38:48 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		0x800
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 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_rsdmap,	\
179 	    off, size, BUS_DMASYNC_POSTREAD);			\
180 	break;							\
181 }								\
182 case SYNC_SFORDEV:						\
183 {								\
184 	off_t off = (off_t) offset;				\
185 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_scdmap,	\
186 	    off, size, BUS_DMASYNC_PREWRITE);			\
187 	break;							\
188 }								\
189 case SYNC_SFORCPU:						\
190 {								\
191 	off_t off = (off_t) offset;				\
192 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_scdmap,	\
193 	    off, size, BUS_DMASYNC_POSTREAD);			\
194 	break;							\
195 }								\
196 case SYNC_REG:							\
197 default:							\
198 	break;							\
199 }
200 
201 #define	MBOX_ACQUIRE(isp)						\
202 	if (isp->isp_osinfo.onintstack) {				\
203 		if (isp->isp_osinfo.mbox_locked) {			\
204 			mbp->param[0] = MBOX_HOST_INTERFACE_ERROR;	\
205 			isp_prt(isp, ISP_LOGERR, "cannot acquire MBOX lock"); \
206 			return;						\
207 		}							\
208 	} else {							\
209 		while (isp->isp_osinfo.mbox_locked) {			\
210 			isp->isp_osinfo.mbox_wanted = 1;		\
211 			(void) tsleep(&isp->isp_osinfo.mboxwaiting+1,	\
212 			    PRIBIO, "mbox_acquire", 0);			\
213 		}							\
214 	}								\
215 	isp->isp_osinfo.mbox_locked = 1
216 
217 #define	MBOX_WAIT_COMPLETE	isp_wait_complete
218 
219 #define	MBOX_NOTIFY_COMPLETE(isp)					\
220 	if (isp->isp_osinfo.mboxwaiting) {				\
221                 isp->isp_osinfo.mboxwaiting = 0;			\
222                 wakeup(&isp->isp_osinfo.mboxwaiting);			\
223         }								\
224 	isp->isp_mboxbsy = 0
225 
226 #define	MBOX_RELEASE(isp)						\
227 	if (isp->isp_osinfo.mbox_wanted) {				\
228 		isp->isp_osinfo.mbox_wanted = 0;			\
229 		wakeup(&isp->isp_osinfo.mboxwaiting+1);			\
230 	}								\
231 	isp->isp_osinfo.mbox_locked = 0
232 
233 #define	FC_SCRATCH_ACQUIRE(isp)
234 #define	FC_SCRATCH_RELEASE(isp)
235 
236 #ifndef	SCSI_GOOD
237 #define	SCSI_GOOD	0x0
238 #endif
239 #ifndef	SCSI_CHECK
240 #define	SCSI_CHECK	0x2
241 #endif
242 #ifndef	SCSI_BUSY
243 #define	SCSI_BUSY	0x8
244 #endif
245 #ifndef	SCSI_QFULL
246 #define	SCSI_QFULL	0x28
247 #endif
248 
249 #define	XS_T			struct scsipi_xfer
250 #define	XS_CHANNEL(xs)		((int) (xs)->xs_periph->periph_channel->chan_channel)
251 #define	XS_ISP(xs)		\
252 	((void *)(xs)->xs_periph->periph_channel->chan_adapter->adapt_dev)
253 #define	XS_LUN(xs)		((int) (xs)->xs_periph->periph_lun)
254 #define	XS_TGT(xs)		((int) (xs)->xs_periph->periph_target)
255 #define	XS_CDBP(xs)		((caddr_t) (xs)->cmd)
256 #define	XS_CDBLEN(xs)		(xs)->cmdlen
257 #define	XS_XFRLEN(xs)		(xs)->datalen
258 #define	XS_TIME(xs)		(xs)->timeout
259 #define	XS_RESID(xs)		(xs)->resid
260 #define	XS_STSP(xs)		(&(xs)->status)
261 #define	XS_SNSP(xs)		(&(xs)->sense.scsi_sense)
262 #define	XS_SNSLEN(xs)		(sizeof (xs)->sense)
263 #define	XS_SNSKEY(xs)		((xs)->sense.scsi_sense.flags)
264 #define	XS_TAG_P(ccb)		(((xs)->xs_control & XS_CTL_POLL) != 0)
265 #define	XS_TAG_TYPE(xs)	\
266 	(((xs)->xs_control & XS_CTL_URGENT) ? REQFLAG_HTAG : \
267 	((xs)->bp && xs->bp->b_flags & B_ASYNC) ? REQFLAG_STAG : REQFLAG_OTAG)
268 
269 #define	XS_SETERR(xs, v)	(xs)->error = v
270 
271 #	define	HBA_NOERROR		XS_NOERROR
272 #	define	HBA_BOTCH		XS_DRIVER_STUFFUP
273 #	define	HBA_CMDTIMEOUT		XS_TIMEOUT
274 #	define	HBA_SELTIMEOUT		XS_SELTIMEOUT
275 #	define	HBA_TGTBSY		XS_BUSY
276 #	define	HBA_BUSRESET		XS_RESET
277 #	define	HBA_ABORTED		XS_DRIVER_STUFFUP
278 #	define	HBA_DATAOVR		XS_DRIVER_STUFFUP
279 #	define	HBA_ARQFAIL		XS_DRIVER_STUFFUP
280 
281 #define	XS_ERR(xs)		(xs)->error
282 
283 #define	XS_NOERR(xs)		(xs)->error == XS_NOERROR
284 
285 #define	XS_INITERR(xs)		(xs)->error = 0, XS_CMD_S_CLEAR(xs)
286 
287 #define	XS_SAVE_SENSE(xs, sp)				\
288 	if (xs->error == XS_NOERROR) {			\
289 		xs->error = XS_SENSE;			\
290 	}						\
291 	memcpy(&(xs)->sense, sp->req_sense_data,	\
292 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
293 
294 #define	XS_SET_STATE_STAT(a, b, c)
295 
296 #define	DEFAULT_IID(x)		7
297 #define	DEFAULT_LOOPID(x)	108
298 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.un._wwn
299 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.un._wwn
300 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
301 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
302 
303 #if	_BYTE_ORDER == _BIG_ENDIAN
304 #ifdef	ISP_SBUS_SUPPORTED
305 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
306 #define	ISP_IOXPUT_16(isp, s, d)				\
307 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
308 #define	ISP_IOXPUT_32(isp, s, d)				\
309 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
310 
311 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
312 #define	ISP_IOXGET_16(isp, s, d)				\
313 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
314 	*((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
315 #define	ISP_IOXGET_32(isp, s, d)				\
316 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
317 	*((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
318 #else
319 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
320 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
321 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
322 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
323 #define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((u_int16_t *)s))
324 #define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((u_int32_t *)s))
325 #endif
326 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
327 #else
328 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
329 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
330 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
331 #define	ISP_IOXGET_8(isp, s, d)		d = *(s)
332 #define	ISP_IOXGET_16(isp, s, d)	d = *(s)
333 #define	ISP_IOXGET_32(isp, s, d)	d = *(s)
334 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
335 #endif
336 
337 /*
338  * Includes of common header files
339  */
340 
341 #include <dev/ic/ispreg.h>
342 #include <dev/ic/ispvar.h>
343 #include <dev/ic/ispmbox.h>
344 #include <dev/ic/isp_ioctl.h>
345 
346 /*
347  * isp_osinfo definitions, extensions and shorthand.
348  */
349 #define	isp_name	isp_osinfo._dev.dv_xname
350 #define	isp_unit	isp_osinfo._dev.dv_unit
351 #define	isp_chanA	isp_osinfo._chan
352 #define	isp_chanB	isp_osinfo._chan_b
353 
354 
355 /*
356  * Driver prototypes..
357  */
358 void isp_attach(struct ispsoftc *);
359 void isp_uninit(struct ispsoftc *);
360 
361 static INLINE void isp_lock(struct ispsoftc *);
362 static INLINE void isp_unlock(struct ispsoftc *);
363 static INLINE char *strncat(char *, const char *, size_t);
364 static INLINE u_int64_t
365 isp_microtime_sub(struct timeval *, struct timeval *);
366 static void isp_wait_complete(struct ispsoftc *);
367 
368 /*
369  * Driver wide data...
370  */
371 
372 /*
373  * Locking macros...
374  */
375 #define	ISP_LOCK		isp_lock
376 #define	ISP_UNLOCK		isp_unlock
377 #define	ISP_ILOCK(x)		isp_lock(x); isp->isp_osinfo.onintstack++
378 #define	ISP_IUNLOCK(x)		isp->isp_osinfo.onintstack--; isp_unlock(x)
379 
380 /*
381  * Platform private flags
382  */
383 
384 #define	XS_PSTS_INWDOG		0x10000000
385 #define	XS_PSTS_GRACE		0x20000000
386 #define	XS_PSTS_ALL		0x30000000
387 
388 #define	XS_CMD_S_WDOG(xs)	(xs)->xs_status |= XS_PSTS_INWDOG
389 #define	XS_CMD_C_WDOG(xs)	(xs)->xs_status &= ~XS_PSTS_INWDOG
390 #define	XS_CMD_WDOG_P(xs)	(((xs)->xs_status & XS_PSTS_INWDOG) != 0)
391 
392 #define	XS_CMD_S_GRACE(xs)	(xs)->xs_status |= XS_PSTS_GRACE
393 #define	XS_CMD_C_GRACE(xs)	(xs)->xs_status &= ~XS_PSTS_GRACE
394 #define	XS_CMD_GRACE_P(xs)	(((xs)->xs_status & XS_PSTS_GRACE) != 0)
395 
396 #define	XS_CMD_S_DONE(xs)	(xs)->xs_status |= XS_STS_DONE
397 #define	XS_CMD_C_DONE(xs)	(xs)->xs_status &= ~XS_STS_DONE
398 #define	XS_CMD_DONE_P(xs)	(((xs)->xs_status & XS_STS_DONE) != 0)
399 
400 #define	XS_CMD_S_CLEAR(xs)	(xs)->xs_status &= ~XS_PSTS_ALL
401 
402 /*
403  * Platform specific 'inline' or support functions
404  */
405 static INLINE void
406 isp_lock(struct ispsoftc *isp)
407 {
408 	int s = splbio();
409 	if (isp->isp_osinfo.islocked++ == 0) {
410 		isp->isp_osinfo.splsaved = s;
411 	} else {
412 		splx(s);
413 	}
414 }
415 
416 static INLINE void
417 isp_unlock(struct ispsoftc *isp)
418 {
419 	if (isp->isp_osinfo.islocked-- <= 1) {
420 		isp->isp_osinfo.islocked = 0;
421 		splx(isp->isp_osinfo.splsaved);
422 	}
423 }
424 
425 static INLINE char *
426 strncat(char *d, const char *s, size_t c)
427 {
428         char *t = d;
429 
430         if (c) {
431                 while (*d)
432                         d++;
433                 while ((*d++ = *s++)) {
434                         if (--c == 0) {
435                                 *d = '\0';
436                                 break;
437                         }
438                 }
439         }
440         return (t);
441 }
442 
443 static INLINE u_int64_t
444 isp_microtime_sub(struct timeval *b, struct timeval *a)
445 {
446 	struct timeval x;
447 	u_int64_t elapsed;
448 	timersub(b, a, &x);
449 	elapsed = GET_NANOSEC(&x);
450 	if (elapsed == 0)
451 		elapsed++;
452 	return (elapsed);
453 }
454 
455 static INLINE void
456 isp_wait_complete(struct ispsoftc *isp)
457 {
458 	int lim;
459 	if (isp->isp_mbxwrk0)
460 		lim = 60;
461 	else
462 		lim = 5;
463 	if (isp->isp_osinfo.onintstack || isp->isp_osinfo.no_mbox_ints) {
464 		int useclim = 1000000 * lim, usecs = 0;
465 		/*
466 		 * For sanity's sake, we don't delay longer
467 		 * than 5 seconds for polled commands.
468 		 */
469 		while (usecs < useclim) {
470 			u_int16_t isr, sema, mbox;
471 			if (isp->isp_mboxbsy == 0) {
472 				break;
473 			}
474 			if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
475 				isp_intr(isp, isr, sema, mbox);
476 				if (isp->isp_mboxbsy == 0) {
477 					break;
478 				}
479 			}
480 			USEC_DELAY(500);
481 			usecs += 500;
482 		}
483 		if (isp->isp_mboxbsy != 0) {
484 			isp_prt(isp, ISP_LOGWARN,
485 			    "Polled Mailbox Command (0x%x) Timeout",
486 			    isp->isp_lastmbxcmd);
487 		}
488 	} else {
489 		int rv = 0;
490                 isp->isp_osinfo.mboxwaiting = 1;
491                 while (isp->isp_osinfo.mboxwaiting && rv == 0) {
492 			struct timeval dtime;
493 			int timo;
494 			struct timeval tv;
495 
496 			dtime.tv_sec = lim;
497 			dtime.tv_usec = 0;
498 			microtime(&tv);
499 			timeradd(&tv, &dtime, &tv);
500 			if ((timo = hzto(&tv)) == 0) {
501 				timo = 1;
502 			}
503 			rv = tsleep(&isp->isp_osinfo.mboxwaiting,
504 			    PRIBIO, "isp_mboxcmd", timo);
505 		}
506 		if (rv == EWOULDBLOCK) {
507 			isp->isp_mboxbsy = 0;
508 			isp->isp_osinfo.mboxwaiting = 0;
509 			isp_prt(isp, ISP_LOGWARN,
510 			    "Interrupting Mailbox Command (0x%x) Timeout",
511 			    isp->isp_lastmbxcmd);
512 		}
513 	}
514 }
515 
516 
517 /*
518  * Common inline functions
519  */
520 #include <dev/ic/isp_inline.h>
521 
522 #if	!defined(ISP_DISABLE_FW) && !defined(ISP_COMPILE_FW)
523 #define	ISP_COMPILE_FW	1
524 #endif
525 #endif	/* _ISP_NETBSD_H */
526