xref: /netbsd-src/sys/dev/ic/isp_netbsd.c (revision 93f9db1b75d415b78f73ed629beeb86235153473)
1 /* $NetBSD: isp_netbsd.c,v 1.5 1998/10/10 00:28:34 thorpej Exp $ */
2 /*
3  * Platform (NetBSD) dependent common attachment code for Qlogic adapters.
4  *
5  *---------------------------------------
6  * Copyright (c) 1997, 1998 by Matthew Jacob
7  * NASA/Ames Research Center
8  * All rights reserved.
9  *---------------------------------------
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice immediately at the beginning of the file, without modification,
16  *    this list of conditions, and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * The author may be reached via electronic communications at
36  *
37  *  mjacob@nas.nasa.gov
38  *  mjacob@feral.com
39  *
40  * or, via United States Postal Address
41  *
42  *  Matthew Jacob
43  *  Feral Software
44  *  2339 3rd Street
45  *  Suite 24
46  *  San Francisco, CA, 94107
47  */
48 
49 #include <dev/ic/isp_netbsd.h>
50 
51 static void ispminphys __P((struct buf *));
52 static int32_t ispcmd __P((ISP_SCSI_XFER_T *));
53 
54 static struct scsipi_adapter isp_switch = {
55 	ispcmd,			/* scsipi_cmd */
56 	ispminphys,		/* scsipi_minphys */
57 	NULL,			/* scsipi_ioctl */
58 };
59 
60 static struct scsipi_device isp_dev = { NULL, NULL, NULL, NULL };
61 static int isp_poll __P((struct ispsoftc *, ISP_SCSI_XFER_T *, int));
62 
63 /*
64  * Complete attachment of hardware, include subdevices.
65  */
66 void
67 isp_attach(isp)
68 	struct ispsoftc *isp;
69 {
70 	isp->isp_state = ISP_RUNSTATE;
71 	isp->isp_osinfo._link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
72 	isp->isp_osinfo._link.adapter_softc = isp;
73 	isp->isp_osinfo._link.device = &isp_dev;
74 	isp->isp_osinfo._link.adapter = &isp_switch;
75 
76 	if (isp->isp_type & ISP_HA_FC) {
77 		isp->isp_osinfo._link.scsipi_scsi.max_target = MAX_FC_TARG-1;
78 		isp->isp_osinfo._link.openings =
79 			RQUEST_QUEUE_LEN / (MAX_FC_TARG-1);
80 		isp->isp_osinfo._link.scsipi_scsi.adapter_target = 0xff;
81 	} else {
82 		isp->isp_osinfo._link.openings =
83 			RQUEST_QUEUE_LEN / (MAX_TARGETS-1);
84 		isp->isp_osinfo._link.scsipi_scsi.max_target = MAX_TARGETS-1;
85 		isp->isp_osinfo._link.scsipi_scsi.adapter_target =
86 			((sdparam *)isp->isp_param)->isp_initiator_id;
87 	}
88 	if (isp->isp_osinfo._link.openings < 2)
89 		isp->isp_osinfo._link.openings = 2;
90 	isp->isp_osinfo._link.type = BUS_SCSI;
91 	config_found((void *)isp, &isp->isp_osinfo._link, scsiprint);
92 }
93 
94 /*
95  * minphys our xfers
96  *
97  * Unfortunately, the buffer pointer describes the target device- not the
98  * adapter device, so we can't use the pointer to find out what kind of
99  * adapter we are and adjust accordingly.
100  */
101 
102 static void
103 ispminphys(bp)
104 	struct buf *bp;
105 {
106 	/*
107 	 * XX: Only the 1020 has a 24 bit limit.
108 	 */
109 	if (bp->b_bcount >= (1 << 24)) {
110 		bp->b_bcount = (1 << 24);
111 	}
112 	minphys(bp);
113 }
114 
115 static int
116 ispcmd(xs)
117 	ISP_SCSI_XFER_T *xs;
118 {
119 	struct ispsoftc *isp;
120 	int result;
121 	ISP_LOCKVAL_DECL;
122 
123 	isp = XS_ISP(xs);
124 	ISP_LOCK(isp);
125 	/*
126 	 * This is less efficient than I would like in that the
127 	 * majority of cases will have to do some pointer deferences
128 	 * to find out that things don't need to be updated.
129 	 */
130 	if ((xs->flags & SCSI_AUTOCONF) == 0 && (isp->isp_type & ISP_HA_SCSI)) {
131 		sdparam *sdp = isp->isp_param;
132 		if (sdp->isp_devparam[XS_TGT(xs)].dev_flags !=
133 		    sdp->isp_devparam[XS_TGT(xs)].cur_dflags) {
134 			u_int16_t f = DPARM_WIDE|DPARM_SYNC|DPARM_TQING;
135 			if (xs->sc_link->quirks & SDEV_NOSYNC)
136 				f &= ~DPARM_SYNC;
137 			if (xs->sc_link->quirks & SDEV_NOWIDE)
138 				f &= ~DPARM_WIDE;
139 			if (xs->sc_link->quirks & SDEV_NOTAG)
140 				f &= ~DPARM_TQING;
141 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &=
142 				~(DPARM_WIDE|DPARM_SYNC|DPARM_TQING);
143 			sdp->isp_devparam[XS_TGT(xs)].dev_flags |= f;
144 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
145 			isp->isp_update = 1;
146 		}
147 	}
148 	result = ispscsicmd(xs);
149 	if (result != CMD_QUEUED || (xs->flags & SCSI_POLL) == 0) {
150 		ISP_UNLOCK(isp);
151 		return (result);
152 	}
153 
154 	/*
155 	 * If we can't use interrupts, poll on completion.
156 	 */
157 	if (isp_poll(isp, xs, XS_TIME(xs))) {
158 		/*
159 		 * If no other error occurred but we didn't finish,
160 		 * something bad happened.
161 		 */
162 		if (XS_IS_CMD_DONE(xs) == 0) {
163 			isp->isp_nactive--;
164 			if (isp->isp_nactive < 0)
165 				isp->isp_nactive = 0;
166 			if (XS_NOERR(xs)) {
167 				isp_lostcmd(isp, xs);
168 				XS_SETERR(xs, HBA_BOTCH);
169 			}
170 		}
171 	}
172 	ISP_UNLOCK(isp);
173 	return (CMD_COMPLETE);
174 }
175 
176 static int
177 isp_poll(isp, xs, mswait)
178 	struct ispsoftc *isp;
179 	ISP_SCSI_XFER_T *xs;
180 	int mswait;
181 {
182 
183 	while (mswait) {
184 		/* Try the interrupt handling routine */
185 		(void)isp_intr((void *)isp);
186 
187 		/* See if the xs is now done */
188 		if (XS_IS_CMD_DONE(xs)) {
189 			return (0);
190 		}
191 		SYS_DELAY(1000);	/* wait one millisecond */
192 		mswait--;
193 	}
194 	return (1);
195 }
196