isp_netbsd.c revision 1.18.2.1 1 1.18.2.1 thorpej /* $NetBSD: isp_netbsd.c,v 1.18.2.1 1999/10/19 17:47:37 thorpej Exp $ */
2 1.1 mjacob /*
3 1.1 mjacob * Platform (NetBSD) dependent common attachment code for Qlogic adapters.
4 1.15 mjacob * Matthew Jacob <mjacob (at) nas.nasa.gov>
5 1.15 mjacob */
6 1.15 mjacob /*
7 1.15 mjacob * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
8 1.1 mjacob * All rights reserved.
9 1.1 mjacob *
10 1.1 mjacob * Redistribution and use in source and binary forms, with or without
11 1.1 mjacob * modification, are permitted provided that the following conditions
12 1.1 mjacob * are met:
13 1.1 mjacob * 1. Redistributions of source code must retain the above copyright
14 1.15 mjacob * notice, this list of conditions and the following disclaimer.
15 1.1 mjacob * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mjacob * notice, this list of conditions and the following disclaimer in the
17 1.1 mjacob * documentation and/or other materials provided with the distribution.
18 1.1 mjacob * 3. The name of the author may not be used to endorse or promote products
19 1.15 mjacob * derived from this software without specific prior written permission
20 1.1 mjacob *
21 1.15 mjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.15 mjacob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.15 mjacob * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.15 mjacob * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.15 mjacob * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.15 mjacob * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.15 mjacob * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.15 mjacob * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.15 mjacob * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.15 mjacob * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 mjacob */
32 1.1 mjacob
33 1.1 mjacob #include <dev/ic/isp_netbsd.h>
34 1.17 mjacob #include <sys/scsiio.h>
35 1.1 mjacob
36 1.1 mjacob static void ispminphys __P((struct buf *));
37 1.18.2.1 thorpej static void isp_scsipi_request __P((struct scsipi_channel *,
38 1.18.2.1 thorpej scsipi_adapter_req_t, void *));
39 1.17 mjacob static int
40 1.18.2.1 thorpej ispioctl __P((struct scsipi_channel *, u_long, caddr_t, int, struct proc *));
41 1.1 mjacob
42 1.1 mjacob static int isp_poll __P((struct ispsoftc *, ISP_SCSI_XFER_T *, int));
43 1.10 mjacob static void isp_watch __P((void *));
44 1.17 mjacob static void isp_command_requeue __P((void *));
45 1.10 mjacob static void isp_internal_restart __P((void *));
46 1.10 mjacob
47 1.1 mjacob /*
48 1.1 mjacob * Complete attachment of hardware, include subdevices.
49 1.1 mjacob */
50 1.1 mjacob void
51 1.1 mjacob isp_attach(isp)
52 1.1 mjacob struct ispsoftc *isp;
53 1.1 mjacob {
54 1.18.2.1 thorpej struct scsipi_adapter *adapt = &isp->isp_osinfo._adapter;
55 1.18.2.1 thorpej struct scsipi_channel *chan;
56 1.18.2.1 thorpej int i;
57 1.6 thorpej
58 1.1 mjacob isp->isp_state = ISP_RUNSTATE;
59 1.18 mjacob TAILQ_INIT(&isp->isp_osinfo.waitq); /* XXX 2nd Bus? */
60 1.1 mjacob
61 1.18.2.1 thorpej /*
62 1.18.2.1 thorpej * Fill in the scsipi_adapter.
63 1.18.2.1 thorpej */
64 1.18.2.1 thorpej adapt->adapt_dev = &isp->isp_osinfo._dev;
65 1.18.2.1 thorpej if (IS_FC(isp) == 0 && IS_12X0(isp) != 0)
66 1.18.2.1 thorpej adapt->adapt_nchannels = 2;
67 1.18.2.1 thorpej else
68 1.18.2.1 thorpej adapt->adapt_nchannels = 1;
69 1.18.2.1 thorpej adapt->adapt_openings = isp->isp_maxcmds;
70 1.18.2.1 thorpej adapt->adapt_max_periph = adapt->adapt_openings;
71 1.18.2.1 thorpej adapt->adapt_request = isp_scsipi_request;
72 1.18.2.1 thorpej adapt->adapt_minphys = ispminphys;
73 1.18.2.1 thorpej adapt->adapt_ioctl = ispioctl;
74 1.18.2.1 thorpej
75 1.18.2.1 thorpej /*
76 1.18.2.1 thorpej * Fill in the scsipi_channel(s).
77 1.18.2.1 thorpej */
78 1.18.2.1 thorpej for (i = 0; i < adapt->adapt_nchannels; i++) {
79 1.18.2.1 thorpej chan = &isp->isp_osinfo._channels[i];
80 1.18.2.1 thorpej chan->chan_adapter = adapt;
81 1.18.2.1 thorpej chan->chan_bustype = &scsi_bustype;
82 1.18.2.1 thorpej chan->chan_channel = i;
83 1.18.2.1 thorpej
84 1.18.2.1 thorpej if (IS_FC(isp)) {
85 1.18.2.1 thorpej fcparam *fcp = isp->isp_param;
86 1.18.2.1 thorpej fcp = &fcp[i];
87 1.18.2.1 thorpej
88 1.18.2.1 thorpej /*
89 1.18.2.1 thorpej * Give it another chance here to come alive...
90 1.18.2.1 thorpej */
91 1.18.2.1 thorpej if (fcp->isp_fwstate != FW_READY) {
92 1.18.2.1 thorpej (void) isp_control(isp, ISPCTL_FCLINK_TEST,
93 1.18.2.1 thorpej NULL);
94 1.18.2.1 thorpej }
95 1.18.2.1 thorpej chan->chan_ntargets = MAX_FC_TARG;
96 1.11 mjacob #ifdef ISP2100_SCCLUN
97 1.18.2.1 thorpej /*
98 1.18.2.1 thorpej * 16 bits worth, but let's be reasonable..
99 1.18.2.1 thorpej */
100 1.18.2.1 thorpej chan->chan_nluns = 256;
101 1.7 mjacob #else
102 1.18.2.1 thorpej chan->chan_nluns = 16;
103 1.7 mjacob #endif
104 1.18.2.1 thorpej chan->chan_id = fcp->isp_loopid;
105 1.7 mjacob } else {
106 1.18.2.1 thorpej sdparam *sdp = isp->isp_param;
107 1.18.2.1 thorpej sdp = &sdp[i];
108 1.18.2.1 thorpej
109 1.18.2.1 thorpej chan->chan_ntargets = MAX_TARGETS;
110 1.18.2.1 thorpej if (isp->isp_bustype == ISP_BT_SBUS)
111 1.18.2.1 thorpej chan->chan_nluns = 8;
112 1.18.2.1 thorpej else {
113 1.18.2.1 thorpej #if 0
114 1.18.2.1 thorpej /* Too many broken targets... */
115 1.18.2.1 thorpej if (isp->isp_fwrev >= ISP_FW_REV(7,55,0))
116 1.18.2.1 thorpej chan->chan_nluns = 32;
117 1.18.2.1 thorpej else
118 1.7 mjacob #endif
119 1.18.2.1 thorpej chan->chan_nluns = 7;
120 1.18.2.1 thorpej }
121 1.18.2.1 thorpej chan->chan_id = sdp->isp_initiator_id;
122 1.14 mjacob }
123 1.1 mjacob }
124 1.8 mjacob
125 1.8 mjacob /*
126 1.10 mjacob * Send a SCSI Bus Reset (used to be done as part of attach,
127 1.10 mjacob * but now left to the OS outer layers).
128 1.10 mjacob */
129 1.14 mjacob if (IS_SCSI(isp)) {
130 1.18.2.1 thorpej for (i = 0; i < adapt->adapt_nchannels; i++)
131 1.18.2.1 thorpej (void) isp_control(isp, ISPCTL_RESET_BUS, &i);
132 1.11 mjacob SYS_DELAY(2*1000000);
133 1.11 mjacob }
134 1.10 mjacob
135 1.10 mjacob /*
136 1.8 mjacob * Start the watchdog.
137 1.8 mjacob */
138 1.8 mjacob isp->isp_dogactive = 1;
139 1.17 mjacob timeout(isp_watch, isp, WATCH_INTERVAL * hz);
140 1.8 mjacob
141 1.8 mjacob /*
142 1.8 mjacob * And attach children (if any).
143 1.8 mjacob */
144 1.18.2.1 thorpej for (i = 0; i < adapt->adapt_nchannels; i++)
145 1.18.2.1 thorpej (void) config_found((void *)isp, &isp->isp_osinfo._channels[i],
146 1.18.2.1 thorpej scsiprint);
147 1.1 mjacob }
148 1.1 mjacob
149 1.1 mjacob /*
150 1.1 mjacob * minphys our xfers
151 1.1 mjacob *
152 1.1 mjacob * Unfortunately, the buffer pointer describes the target device- not the
153 1.1 mjacob * adapter device, so we can't use the pointer to find out what kind of
154 1.1 mjacob * adapter we are and adjust accordingly.
155 1.1 mjacob */
156 1.1 mjacob
157 1.1 mjacob static void
158 1.1 mjacob ispminphys(bp)
159 1.1 mjacob struct buf *bp;
160 1.1 mjacob {
161 1.1 mjacob /*
162 1.1 mjacob * XX: Only the 1020 has a 24 bit limit.
163 1.1 mjacob */
164 1.1 mjacob if (bp->b_bcount >= (1 << 24)) {
165 1.1 mjacob bp->b_bcount = (1 << 24);
166 1.1 mjacob }
167 1.1 mjacob minphys(bp);
168 1.1 mjacob }
169 1.1 mjacob
170 1.17 mjacob static int
171 1.18.2.1 thorpej ispioctl(chan, cmd, addr, flag, p)
172 1.18.2.1 thorpej struct scsipi_channel *chan;
173 1.17 mjacob u_long cmd;
174 1.17 mjacob caddr_t addr;
175 1.17 mjacob int flag;
176 1.17 mjacob struct proc *p;
177 1.17 mjacob {
178 1.18.2.1 thorpej struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
179 1.18.2.1 thorpej int s, ch, retval = ENOTTY;
180 1.17 mjacob
181 1.17 mjacob switch (cmd) {
182 1.17 mjacob case SCBUSIORESET:
183 1.18.2.1 thorpej ch = chan->chan_channel;
184 1.17 mjacob s = splbio();
185 1.18.2.1 thorpej if (isp_control(isp, ISPCTL_RESET_BUS, &ch))
186 1.17 mjacob retval = EIO;
187 1.17 mjacob else
188 1.17 mjacob retval = 0;
189 1.17 mjacob (void) splx(s);
190 1.17 mjacob break;
191 1.17 mjacob default:
192 1.17 mjacob break;
193 1.3 mjacob }
194 1.17 mjacob return (retval);
195 1.17 mjacob }
196 1.17 mjacob
197 1.18.2.1 thorpej static void
198 1.18.2.1 thorpej isp_scsipi_request(chan, req, arg)
199 1.18.2.1 thorpej struct scsipi_channel *chan;
200 1.18.2.1 thorpej scsipi_adapter_req_t req;
201 1.18.2.1 thorpej void *arg;
202 1.17 mjacob {
203 1.18.2.1 thorpej struct scsipi_xfer *xs;
204 1.18.2.1 thorpej struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
205 1.17 mjacob int result, s;
206 1.11 mjacob
207 1.18.2.1 thorpej switch (req) {
208 1.18.2.1 thorpej case ADAPTER_REQ_RUN_XFER:
209 1.18.2.1 thorpej s = splbio();
210 1.18.2.1 thorpej if (isp->isp_state < ISP_RUNSTATE) {
211 1.18.2.1 thorpej DISABLE_INTS(isp);
212 1.18.2.1 thorpej isp_init(isp);
213 1.18.2.1 thorpej if (isp->isp_state != ISP_INITSTATE) {
214 1.18.2.1 thorpej ENABLE_INTS(isp);
215 1.18.2.1 thorpej (void) splx(s);
216 1.18.2.1 thorpej XS_SETERR(xs, HBA_BOTCH);
217 1.18.2.1 thorpej XS_CMD_DONE(xs);
218 1.18.2.1 thorpej return;
219 1.18.2.1 thorpej }
220 1.18.2.1 thorpej isp->isp_state = ISP_RUNSTATE;
221 1.11 mjacob ENABLE_INTS(isp);
222 1.18.2.1 thorpej }
223 1.11 mjacob
224 1.18.2.1 thorpej /*
225 1.18.2.1 thorpej * Check for queue blockage...
226 1.18.2.1 thorpej * XXX Should be done in the mid-layer.
227 1.18.2.1 thorpej */
228 1.18.2.1 thorpej if (isp->isp_osinfo.blocked) {
229 1.18.2.1 thorpej if (xs->xs_control & XS_CTL_POLL) {
230 1.18.2.1 thorpej xs->error = XS_BUSY;
231 1.18.2.1 thorpej scsipi_done(xs);
232 1.18.2.1 thorpej splx(s);
233 1.18.2.1 thorpej return;
234 1.18.2.1 thorpej }
235 1.18.2.1 thorpej TAILQ_INSERT_TAIL(&isp->isp_osinfo.waitq, xs,
236 1.18.2.1 thorpej channel_q);
237 1.10 mjacob splx(s);
238 1.18.2.1 thorpej return;
239 1.10 mjacob }
240 1.17 mjacob
241 1.18.2.1 thorpej DISABLE_INTS(isp);
242 1.18.2.1 thorpej result = ispscsicmd(xs);
243 1.18.2.1 thorpej ENABLE_INTS(isp);
244 1.18.2.1 thorpej
245 1.17 mjacob switch (result) {
246 1.17 mjacob case CMD_QUEUED:
247 1.18.2.1 thorpej /*
248 1.18.2.1 thorpej * If we're not polling for completion, just return.
249 1.18.2.1 thorpej */
250 1.18.2.1 thorpej if ((xs->xs_control & XS_CTL_POLL) == 0) {
251 1.18.2.1 thorpej (void) splx(s);
252 1.18.2.1 thorpej return;
253 1.18.2.1 thorpej }
254 1.17 mjacob break;
255 1.18.2.1 thorpej
256 1.17 mjacob case CMD_EAGAIN:
257 1.18.2.1 thorpej /*
258 1.18.2.1 thorpej * Adapter resource shortage of some sort. Should
259 1.18.2.1 thorpej * retry later.
260 1.18.2.1 thorpej */
261 1.18.2.1 thorpej XS_SETERR(xs, XS_RESOURCE_SHORTAGE);
262 1.18.2.1 thorpej XS_CMD_DONE(xs);
263 1.18.2.1 thorpej (void) splx(s);
264 1.18.2.1 thorpej return;
265 1.18.2.1 thorpej
266 1.17 mjacob case CMD_RQLATER:
267 1.18.2.1 thorpej /*
268 1.18.2.1 thorpej * XXX I think what we should do here is freeze
269 1.18.2.1 thorpej * XXX the channel queue, and do a timed thaw
270 1.18.2.1 thorpej * XXX of it. Need to add this to the mid-layer.
271 1.18.2.1 thorpej */
272 1.18.2.1 thorpej if ((xs->xs_control & XS_CTL_POLL) != 0) {
273 1.18.2.1 thorpej XS_SETERR(xs, XS_DRIVER_STUFFUP);
274 1.18.2.1 thorpej XS_CMD_DONE(xs);
275 1.18.2.1 thorpej } else
276 1.18.2.1 thorpej timeout(isp_command_requeue, xs, hz);
277 1.18.2.1 thorpej (void) splx(s);
278 1.18.2.1 thorpej return;
279 1.18.2.1 thorpej
280 1.17 mjacob case CMD_COMPLETE:
281 1.18.2.1 thorpej /*
282 1.18.2.1 thorpej * Something went horribly wrong, xs->error is set,
283 1.18.2.1 thorpej * and we just need to finish it off.
284 1.18.2.1 thorpej */
285 1.18.2.1 thorpej XS_CMD_DONE(xs);
286 1.18.2.1 thorpej (void) splx(s);
287 1.18.2.1 thorpej return;
288 1.17 mjacob }
289 1.1 mjacob
290 1.18.2.1 thorpej /*
291 1.18.2.1 thorpej * If we can't use interrupts, poll on completion.
292 1.18.2.1 thorpej */
293 1.17 mjacob if (isp_poll(isp, xs, XS_TIME(xs))) {
294 1.17 mjacob /*
295 1.17 mjacob * If no other error occurred but we didn't finish,
296 1.17 mjacob * something bad happened.
297 1.17 mjacob */
298 1.17 mjacob if (XS_IS_CMD_DONE(xs) == 0) {
299 1.17 mjacob if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
300 1.17 mjacob isp_restart(isp);
301 1.17 mjacob }
302 1.17 mjacob if (XS_NOERR(xs)) {
303 1.17 mjacob XS_SETERR(xs, HBA_BOTCH);
304 1.17 mjacob }
305 1.18.2.1 thorpej XS_CMD_DONE(xs);
306 1.1 mjacob }
307 1.1 mjacob }
308 1.18.2.1 thorpej (void) splx(s);
309 1.18.2.1 thorpej return;
310 1.18.2.1 thorpej
311 1.18.2.1 thorpej case ADAPTER_REQ_GROW_RESOURCES:
312 1.18.2.1 thorpej /* XXX Not supported. */
313 1.18.2.1 thorpej return;
314 1.18.2.1 thorpej
315 1.18.2.1 thorpej case ADAPTER_REQ_SET_XFER_MODE:
316 1.18.2.1 thorpej if (isp->isp_type & ISP_HA_SCSI) {
317 1.18.2.1 thorpej sdparam *sdp = isp->isp_param;
318 1.18.2.1 thorpej struct scsipi_periph *periph = arg;
319 1.18.2.1 thorpej u_int16_t flags;
320 1.18.2.1 thorpej
321 1.18.2.1 thorpej sdp = &sdp[periph->periph_channel->chan_channel];
322 1.18.2.1 thorpej
323 1.18.2.1 thorpej flags =
324 1.18.2.1 thorpej sdp->isp_devparam[periph->periph_target].dev_flags;
325 1.18.2.1 thorpej flags &= ~(DPARM_WIDE|DPARM_SYNC|DPARM_TQING);
326 1.18.2.1 thorpej
327 1.18.2.1 thorpej if (periph->periph_cap & PERIPH_CAP_SYNC)
328 1.18.2.1 thorpej flags |= DPARM_SYNC;
329 1.18.2.1 thorpej
330 1.18.2.1 thorpej if (periph->periph_cap & PERIPH_CAP_WIDE16)
331 1.18.2.1 thorpej flags |= DPARM_WIDE;
332 1.18.2.1 thorpej
333 1.18.2.1 thorpej if (periph->periph_cap & PERIPH_CAP_TQING)
334 1.18.2.1 thorpej flags |= DPARM_TQING;
335 1.18.2.1 thorpej
336 1.18.2.1 thorpej sdp->isp_devparam[periph->periph_target].dev_flags =
337 1.18.2.1 thorpej flags;
338 1.18.2.1 thorpej sdp->isp_devparam[periph->periph_target].dev_update = 1;
339 1.18.2.1 thorpej isp->isp_update |=
340 1.18.2.1 thorpej (1 << periph->periph_channel->chan_channel);
341 1.18.2.1 thorpej (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, NULL);
342 1.18.2.1 thorpej }
343 1.18.2.1 thorpej return;
344 1.18.2.1 thorpej
345 1.18.2.1 thorpej case ADAPTER_REQ_GET_XFER_MODE:
346 1.18.2.1 thorpej if (isp->isp_type & ISP_HA_SCSI) {
347 1.18.2.1 thorpej sdparam *sdp = isp->isp_param;
348 1.18.2.1 thorpej struct scsipi_periph *periph = arg;
349 1.18.2.1 thorpej u_int16_t flags;
350 1.18.2.1 thorpej
351 1.18.2.1 thorpej sdp = &sdp[periph->periph_channel->chan_channel];
352 1.18.2.1 thorpej
353 1.18.2.1 thorpej periph->periph_mode = 0;
354 1.18.2.1 thorpej periph->periph_period = 0;
355 1.18.2.1 thorpej periph->periph_offset = 0;
356 1.18.2.1 thorpej
357 1.18.2.1 thorpej flags =
358 1.18.2.1 thorpej sdp->isp_devparam[periph->periph_target].cur_dflags;
359 1.18.2.1 thorpej
360 1.18.2.1 thorpej if (flags & DPARM_SYNC) {
361 1.18.2.1 thorpej periph->periph_mode |= PERIPH_CAP_SYNC;
362 1.18.2.1 thorpej periph->periph_period =
363 1.18.2.1 thorpej sdp->isp_devparam[periph->periph_target].cur_period;
364 1.18.2.1 thorpej periph->periph_offset =
365 1.18.2.1 thorpej sdp->isp_devparam[periph->periph_target].cur_offset;
366 1.18.2.1 thorpej }
367 1.18.2.1 thorpej if (flags & DPARM_WIDE)
368 1.18.2.1 thorpej periph->periph_mode |= PERIPH_CAP_WIDE16;
369 1.18.2.1 thorpej if (flags & DPARM_TQING)
370 1.18.2.1 thorpej periph->periph_mode |= PERIPH_CAP_TQING;
371 1.18.2.1 thorpej
372 1.18.2.1 thorpej periph->periph_flags |= PERIPH_MODE_VALID;
373 1.18.2.1 thorpej }
374 1.18.2.1 thorpej return;
375 1.1 mjacob }
376 1.1 mjacob }
377 1.1 mjacob
378 1.1 mjacob static int
379 1.1 mjacob isp_poll(isp, xs, mswait)
380 1.1 mjacob struct ispsoftc *isp;
381 1.1 mjacob ISP_SCSI_XFER_T *xs;
382 1.1 mjacob int mswait;
383 1.1 mjacob {
384 1.1 mjacob
385 1.1 mjacob while (mswait) {
386 1.1 mjacob /* Try the interrupt handling routine */
387 1.1 mjacob (void)isp_intr((void *)isp);
388 1.1 mjacob
389 1.1 mjacob /* See if the xs is now done */
390 1.1 mjacob if (XS_IS_CMD_DONE(xs)) {
391 1.1 mjacob return (0);
392 1.1 mjacob }
393 1.1 mjacob SYS_DELAY(1000); /* wait one millisecond */
394 1.1 mjacob mswait--;
395 1.1 mjacob }
396 1.1 mjacob return (1);
397 1.8 mjacob }
398 1.8 mjacob
399 1.8 mjacob static void
400 1.8 mjacob isp_watch(arg)
401 1.8 mjacob void *arg;
402 1.8 mjacob {
403 1.8 mjacob int i;
404 1.8 mjacob struct ispsoftc *isp = arg;
405 1.17 mjacob struct scsipi_xfer *xs;
406 1.17 mjacob int s;
407 1.8 mjacob
408 1.8 mjacob /*
409 1.8 mjacob * Look for completely dead commands (but not polled ones).
410 1.8 mjacob */
411 1.17 mjacob s = splbio();
412 1.17 mjacob for (i = 0; i < isp->isp_maxcmds; i++) {
413 1.17 mjacob xs = isp->isp_xflist[i];
414 1.17 mjacob if (xs == NULL) {
415 1.8 mjacob continue;
416 1.8 mjacob }
417 1.17 mjacob if (xs->timeout == 0 || (xs->xs_control & XS_CTL_POLL)) {
418 1.8 mjacob continue;
419 1.8 mjacob }
420 1.17 mjacob xs->timeout -= (WATCH_INTERVAL * 1000);
421 1.8 mjacob /*
422 1.8 mjacob * Avoid later thinking that this
423 1.8 mjacob * transaction is not being timed.
424 1.8 mjacob * Then give ourselves to watchdog
425 1.8 mjacob * periods of grace.
426 1.8 mjacob */
427 1.17 mjacob if (xs->timeout == 0) {
428 1.17 mjacob xs->timeout = 1;
429 1.17 mjacob } else if (xs->timeout > -(2 * WATCH_INTERVAL * 1000)) {
430 1.8 mjacob continue;
431 1.8 mjacob }
432 1.8 mjacob if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
433 1.8 mjacob printf("%s: isp_watch failed to abort command\n",
434 1.8 mjacob isp->isp_name);
435 1.8 mjacob isp_restart(isp);
436 1.8 mjacob break;
437 1.8 mjacob }
438 1.8 mjacob }
439 1.8 mjacob timeout(isp_watch, isp, WATCH_INTERVAL * hz);
440 1.8 mjacob isp->isp_dogactive = 1;
441 1.17 mjacob (void) splx(s);
442 1.8 mjacob }
443 1.8 mjacob
444 1.8 mjacob /*
445 1.8 mjacob * Free any associated resources prior to decommissioning and
446 1.8 mjacob * set the card to a known state (so it doesn't wake up and kick
447 1.8 mjacob * us when we aren't expecting it to).
448 1.8 mjacob *
449 1.8 mjacob * Locks are held before coming here.
450 1.8 mjacob */
451 1.8 mjacob void
452 1.8 mjacob isp_uninit(isp)
453 1.8 mjacob struct ispsoftc *isp;
454 1.8 mjacob {
455 1.8 mjacob ISP_ILOCKVAL_DECL;
456 1.8 mjacob ISP_ILOCK(isp);
457 1.8 mjacob /*
458 1.8 mjacob * Leave with interrupts disabled.
459 1.8 mjacob */
460 1.8 mjacob DISABLE_INTS(isp);
461 1.8 mjacob
462 1.8 mjacob /*
463 1.8 mjacob * Turn off the watchdog (if active).
464 1.8 mjacob */
465 1.8 mjacob if (isp->isp_dogactive) {
466 1.8 mjacob untimeout(isp_watch, isp);
467 1.8 mjacob isp->isp_dogactive = 0;
468 1.8 mjacob }
469 1.8 mjacob
470 1.8 mjacob ISP_IUNLOCK(isp);
471 1.9 mjacob }
472 1.9 mjacob
473 1.10 mjacob /*
474 1.17 mjacob * Restart function for a command to be requeued later.
475 1.17 mjacob */
476 1.17 mjacob static void
477 1.17 mjacob isp_command_requeue(arg)
478 1.17 mjacob void *arg;
479 1.17 mjacob {
480 1.17 mjacob struct scsipi_xfer *xs = arg;
481 1.18.2.1 thorpej int s;
482 1.18.2.1 thorpej
483 1.18.2.1 thorpej s = splbio();
484 1.18.2.1 thorpej scsipi_adapter_request(xs->xs_periph->periph_channel,
485 1.18.2.1 thorpej ADAPTER_REQ_RUN_XFER, xs);
486 1.17 mjacob (void) splx(s);
487 1.17 mjacob }
488 1.18 mjacob
489 1.17 mjacob /*
490 1.10 mjacob * Restart function after a LOOP UP event (e.g.),
491 1.10 mjacob * done as a timeout for some hysteresis.
492 1.10 mjacob */
493 1.10 mjacob static void
494 1.10 mjacob isp_internal_restart(arg)
495 1.10 mjacob void *arg;
496 1.10 mjacob {
497 1.10 mjacob struct ispsoftc *isp = arg;
498 1.11 mjacob int result, nrestarted = 0, s;
499 1.10 mjacob
500 1.11 mjacob s = splbio();
501 1.10 mjacob if (isp->isp_osinfo.blocked == 0) {
502 1.10 mjacob struct scsipi_xfer *xs;
503 1.10 mjacob while ((xs = TAILQ_FIRST(&isp->isp_osinfo.waitq)) != NULL) {
504 1.18.2.1 thorpej TAILQ_REMOVE(&isp->isp_osinfo.waitq, xs, channel_q);
505 1.11 mjacob DISABLE_INTS(isp);
506 1.11 mjacob result = ispscsicmd(xs);
507 1.11 mjacob ENABLE_INTS(isp);
508 1.11 mjacob if (result != CMD_QUEUED) {
509 1.10 mjacob printf("%s: botched command restart (0x%x)\n",
510 1.10 mjacob isp->isp_name, result);
511 1.18.2.1 thorpej if (XS_ERR(xs) == XS_NOERROR)
512 1.18.2.1 thorpej XS_SETERR(xs, HBA_BOTCH);
513 1.18.2.1 thorpej XS_CMD_DONE(xs);
514 1.10 mjacob }
515 1.10 mjacob nrestarted++;
516 1.10 mjacob }
517 1.10 mjacob printf("%s: requeued %d commands\n", isp->isp_name, nrestarted);
518 1.10 mjacob }
519 1.10 mjacob (void) splx(s);
520 1.10 mjacob }
521 1.18 mjacob
522 1.9 mjacob int
523 1.9 mjacob isp_async(isp, cmd, arg)
524 1.9 mjacob struct ispsoftc *isp;
525 1.9 mjacob ispasync_t cmd;
526 1.9 mjacob void *arg;
527 1.9 mjacob {
528 1.18.2.1 thorpej int bus;
529 1.10 mjacob int s = splbio();
530 1.9 mjacob switch (cmd) {
531 1.9 mjacob case ISPASYNC_NEW_TGT_PARAMS:
532 1.18.2.1 thorpej /*
533 1.18.2.1 thorpej * XXX Should we really do anything here?
534 1.18.2.1 thorpej */
535 1.11 mjacob break;
536 1.11 mjacob case ISPASYNC_BUS_RESET:
537 1.14 mjacob if (arg)
538 1.14 mjacob bus = *((int *) arg);
539 1.14 mjacob else
540 1.14 mjacob bus = 0;
541 1.14 mjacob printf("%s: SCSI bus %d reset detected\n", isp->isp_name, bus);
542 1.11 mjacob break;
543 1.11 mjacob case ISPASYNC_LOOP_DOWN:
544 1.11 mjacob /*
545 1.11 mjacob * Hopefully we get here in time to minimize the number
546 1.11 mjacob * of commands we are firing off that are sure to die.
547 1.11 mjacob */
548 1.11 mjacob isp->isp_osinfo.blocked = 1;
549 1.11 mjacob printf("%s: Loop DOWN\n", isp->isp_name);
550 1.11 mjacob break;
551 1.11 mjacob case ISPASYNC_LOOP_UP:
552 1.11 mjacob isp->isp_osinfo.blocked = 0;
553 1.11 mjacob timeout(isp_internal_restart, isp, 1);
554 1.11 mjacob printf("%s: Loop UP\n", isp->isp_name);
555 1.11 mjacob break;
556 1.15 mjacob case ISPASYNC_PDB_CHANGED:
557 1.17 mjacob if (IS_FC(isp) && isp->isp_dblev) {
558 1.15 mjacob const char *fmt = "%s: Target %d (Loop 0x%x) Port ID 0x%x "
559 1.15 mjacob "role %s %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x\n";
560 1.15 mjacob const static char *roles[4] = {
561 1.11 mjacob "No", "Target", "Initiator", "Target/Initiator"
562 1.11 mjacob };
563 1.15 mjacob char *ptr;
564 1.15 mjacob fcparam *fcp = isp->isp_param;
565 1.15 mjacob int tgt = *((int *) arg);
566 1.15 mjacob struct lportdb *lp = &fcp->portdb[tgt];
567 1.15 mjacob
568 1.15 mjacob if (lp->valid) {
569 1.15 mjacob ptr = "arrived";
570 1.15 mjacob } else {
571 1.15 mjacob ptr = "disappeared";
572 1.11 mjacob }
573 1.15 mjacob printf(fmt, isp->isp_name, tgt, lp->loopid, lp->portid,
574 1.15 mjacob roles[lp->roles & 0x3], ptr,
575 1.15 mjacob (u_int32_t) (lp->port_wwn >> 32),
576 1.15 mjacob (u_int32_t) (lp->port_wwn & 0xffffffffLL),
577 1.15 mjacob (u_int32_t) (lp->node_wwn >> 32),
578 1.15 mjacob (u_int32_t) (lp->node_wwn & 0xffffffffLL));
579 1.11 mjacob break;
580 1.11 mjacob }
581 1.15 mjacob #ifdef ISP2100_FABRIC
582 1.11 mjacob case ISPASYNC_CHANGE_NOTIFY:
583 1.11 mjacob printf("%s: Name Server Database Changed\n", isp->isp_name);
584 1.9 mjacob break;
585 1.15 mjacob case ISPASYNC_FABRIC_DEV:
586 1.15 mjacob {
587 1.15 mjacob int target;
588 1.15 mjacob struct lportdb *lp;
589 1.15 mjacob sns_scrsp_t *resp = (sns_scrsp_t *) arg;
590 1.15 mjacob u_int32_t portid;
591 1.15 mjacob u_int64_t wwn;
592 1.15 mjacob fcparam *fcp = isp->isp_param;
593 1.15 mjacob
594 1.15 mjacob portid =
595 1.15 mjacob (((u_int32_t) resp->snscb_port_id[0]) << 16) |
596 1.15 mjacob (((u_int32_t) resp->snscb_port_id[1]) << 8) |
597 1.15 mjacob (((u_int32_t) resp->snscb_port_id[2]));
598 1.15 mjacob wwn =
599 1.15 mjacob (((u_int64_t)resp->snscb_portname[0]) << 56) |
600 1.15 mjacob (((u_int64_t)resp->snscb_portname[1]) << 48) |
601 1.15 mjacob (((u_int64_t)resp->snscb_portname[2]) << 40) |
602 1.15 mjacob (((u_int64_t)resp->snscb_portname[3]) << 32) |
603 1.15 mjacob (((u_int64_t)resp->snscb_portname[4]) << 24) |
604 1.15 mjacob (((u_int64_t)resp->snscb_portname[5]) << 16) |
605 1.15 mjacob (((u_int64_t)resp->snscb_portname[6]) << 8) |
606 1.15 mjacob (((u_int64_t)resp->snscb_portname[7]));
607 1.15 mjacob printf("%s: Fabric Device (Type 0x%x)@PortID 0x%x WWN "
608 1.15 mjacob "0x%08x%08x\n", isp->isp_name, resp->snscb_port_type,
609 1.15 mjacob portid, ((u_int32_t)(wwn >> 32)),
610 1.15 mjacob ((u_int32_t)(wwn & 0xffffffff)));
611 1.15 mjacob if (resp->snscb_port_type != 2)
612 1.15 mjacob break;
613 1.15 mjacob for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
614 1.15 mjacob lp = &fcp->portdb[target];
615 1.15 mjacob if (lp->port_wwn == wwn)
616 1.15 mjacob break;
617 1.15 mjacob }
618 1.15 mjacob if (target < MAX_FC_TARG) {
619 1.15 mjacob break;
620 1.15 mjacob }
621 1.15 mjacob for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
622 1.15 mjacob lp = &fcp->portdb[target];
623 1.15 mjacob if (lp->port_wwn == 0)
624 1.15 mjacob break;
625 1.15 mjacob }
626 1.15 mjacob if (target == MAX_FC_TARG) {
627 1.15 mjacob printf("%s: no more space for fabric devices\n",
628 1.15 mjacob isp->isp_name);
629 1.15 mjacob return (-1);
630 1.15 mjacob }
631 1.15 mjacob lp->port_wwn = lp->node_wwn = wwn;
632 1.15 mjacob lp->portid = portid;
633 1.15 mjacob break;
634 1.15 mjacob }
635 1.15 mjacob #endif
636 1.9 mjacob default:
637 1.9 mjacob break;
638 1.9 mjacob }
639 1.10 mjacob (void) splx(s);
640 1.9 mjacob return (0);
641 1.1 mjacob }
642