mscp.c revision 1.18 1 1.18 simonb /* $NetBSD: mscp.c,v 1.18 2002/03/04 02:19:10 simonb Exp $ */
2 1.1 ragge
3 1.1 ragge /*
4 1.1 ragge * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
5 1.1 ragge * Copyright (c) 1988 Regents of the University of California.
6 1.1 ragge * All rights reserved.
7 1.1 ragge *
8 1.1 ragge * This code is derived from software contributed to Berkeley by
9 1.1 ragge * Chris Torek.
10 1.1 ragge *
11 1.1 ragge * Redistribution and use in source and binary forms, with or without
12 1.1 ragge * modification, are permitted provided that the following conditions
13 1.1 ragge * are met:
14 1.1 ragge * 1. Redistributions of source code must retain the above copyright
15 1.1 ragge * notice, this list of conditions and the following disclaimer.
16 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 ragge * notice, this list of conditions and the following disclaimer in the
18 1.1 ragge * documentation and/or other materials provided with the distribution.
19 1.1 ragge * 3. All advertising materials mentioning features or use of this software
20 1.1 ragge * must display the following acknowledgement:
21 1.1 ragge * This product includes software developed by the University of
22 1.1 ragge * California, Berkeley and its contributors.
23 1.1 ragge * 4. Neither the name of the University nor the names of its contributors
24 1.1 ragge * may be used to endorse or promote products derived from this software
25 1.1 ragge * without specific prior written permission.
26 1.1 ragge *
27 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 ragge * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 ragge * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 ragge * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 ragge * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 ragge * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 ragge * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 ragge * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 ragge * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 ragge * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 ragge * SUCH DAMAGE.
38 1.1 ragge *
39 1.1 ragge * @(#)mscp.c 7.5 (Berkeley) 12/16/90
40 1.1 ragge */
41 1.1 ragge
42 1.1 ragge /*
43 1.1 ragge * MSCP generic driver routines
44 1.1 ragge */
45 1.16 lukem
46 1.16 lukem #include <sys/cdefs.h>
47 1.18 simonb __KERNEL_RCSID(0, "$NetBSD: mscp.c,v 1.18 2002/03/04 02:19:10 simonb Exp $");
48 1.1 ragge
49 1.1 ragge #include <sys/param.h>
50 1.1 ragge #include <sys/buf.h>
51 1.18 simonb #include <sys/kernel.h>
52 1.1 ragge #include <sys/malloc.h>
53 1.1 ragge #include <sys/device.h>
54 1.8 ragge #include <sys/proc.h>
55 1.8 ragge #include <sys/systm.h>
56 1.1 ragge
57 1.11 ragge #include <machine/bus.h>
58 1.11 ragge
59 1.11 ragge #include <dev/mscp/mscp.h>
60 1.11 ragge #include <dev/mscp/mscpreg.h>
61 1.11 ragge #include <dev/mscp/mscpvar.h>
62 1.1 ragge
63 1.7 ragge #define PCMD PSWP /* priority for command packet waits */
64 1.1 ragge
65 1.1 ragge /*
66 1.1 ragge * Get a command packet. Second argument is true iff we are
67 1.1 ragge * to wait if necessary. Return NULL if none are available and
68 1.1 ragge * we cannot wait.
69 1.1 ragge */
70 1.1 ragge struct mscp *
71 1.1 ragge mscp_getcp(mi, canwait)
72 1.12 augustss struct mscp_softc *mi;
73 1.1 ragge int canwait;
74 1.1 ragge {
75 1.7 ragge #define mri (&mi->mi_cmd)
76 1.12 augustss struct mscp *mp;
77 1.12 augustss int i;
78 1.15 thorpej int s = spluba();
79 1.1 ragge
80 1.1 ragge again:
81 1.1 ragge /*
82 1.1 ragge * Ensure that we have some command credits, and
83 1.1 ragge * that the next command packet is free.
84 1.1 ragge */
85 1.1 ragge if (mi->mi_credits <= MSCP_MINCREDITS) {
86 1.1 ragge if (!canwait) {
87 1.1 ragge splx(s);
88 1.1 ragge return (NULL);
89 1.1 ragge }
90 1.1 ragge mi->mi_wantcredits = 1;
91 1.13 thorpej (void) tsleep(&mi->mi_wantcredits, PCMD, "mscpwcrd", 0);
92 1.1 ragge goto again;
93 1.1 ragge }
94 1.1 ragge i = mri->mri_next;
95 1.1 ragge if (mri->mri_desc[i] & MSCP_OWN) {
96 1.1 ragge if (!canwait) {
97 1.1 ragge splx(s);
98 1.1 ragge return (NULL);
99 1.1 ragge }
100 1.1 ragge mi->mi_wantcmd = 1;
101 1.13 thorpej (void) tsleep(&mi->mi_wantcmd, PCMD, "mscpwcmd", 0);
102 1.1 ragge goto again;
103 1.1 ragge }
104 1.1 ragge mi->mi_credits--;
105 1.1 ragge mri->mri_desc[i] &= ~MSCP_INT;
106 1.1 ragge mri->mri_next = (mri->mri_next + 1) % mri->mri_size;
107 1.1 ragge splx(s);
108 1.1 ragge mp = &mri->mri_ring[i];
109 1.1 ragge
110 1.1 ragge /*
111 1.1 ragge * Initialise some often-zero fields.
112 1.1 ragge * ARE THE LAST TWO NECESSARY IN GENERAL? IT SURE WOULD BE
113 1.1 ragge * NICE IF DEC SOLD DOCUMENTATION FOR THEIR OWN CONTROLLERS.
114 1.1 ragge */
115 1.1 ragge mp->mscp_msglen = MSCP_MSGLEN;
116 1.1 ragge mp->mscp_flags = 0;
117 1.1 ragge mp->mscp_modifier = 0;
118 1.1 ragge mp->mscp_seq.seq_bytecount = 0;
119 1.1 ragge mp->mscp_seq.seq_buffer = 0;
120 1.1 ragge mp->mscp_seq.seq_mapbase = 0;
121 1.7 ragge /*???*/ mp->mscp_sccc.sccc_errlgfl = 0;
122 1.7 ragge /*???*/ mp->mscp_sccc.sccc_copyspd = 0;
123 1.1 ragge return (mp);
124 1.1 ragge #undef mri
125 1.1 ragge }
126 1.1 ragge
127 1.1 ragge #ifdef AVOID_EMULEX_BUG
128 1.1 ragge int mscp_aeb_xor = 0x8000bb80;
129 1.1 ragge #endif
130 1.1 ragge
131 1.1 ragge /*
132 1.1 ragge * Handle a response ring transition.
133 1.1 ragge */
134 1.1 ragge void
135 1.1 ragge mscp_dorsp(mi)
136 1.12 augustss struct mscp_softc *mi;
137 1.1 ragge {
138 1.1 ragge struct device *drive;
139 1.1 ragge struct mscp_device *me = mi->mi_me;
140 1.1 ragge struct mscp_ctlr *mc = mi->mi_mc;
141 1.11 ragge struct buf *bp;
142 1.11 ragge struct mscp *mp;
143 1.11 ragge struct mscp_xi *mxi;
144 1.11 ragge int nextrsp;
145 1.11 ragge int st, error;
146 1.1 ragge extern struct mscp slavereply;
147 1.1 ragge
148 1.1 ragge nextrsp = mi->mi_rsp.mri_next;
149 1.1 ragge loop:
150 1.1 ragge if (mi->mi_rsp.mri_desc[nextrsp] & MSCP_OWN) {
151 1.1 ragge /*
152 1.1 ragge * No more responses. Remember the next expected
153 1.1 ragge * response index. Check to see if we have some
154 1.1 ragge * credits back, and wake up sleepers if so.
155 1.1 ragge */
156 1.1 ragge mi->mi_rsp.mri_next = nextrsp;
157 1.1 ragge if (mi->mi_wantcredits && mi->mi_credits > MSCP_MINCREDITS) {
158 1.1 ragge mi->mi_wantcredits = 0;
159 1.1 ragge wakeup((caddr_t) &mi->mi_wantcredits);
160 1.1 ragge }
161 1.1 ragge return;
162 1.1 ragge }
163 1.1 ragge
164 1.1 ragge mp = &mi->mi_rsp.mri_ring[nextrsp];
165 1.1 ragge mi->mi_credits += MSCP_CREDITS(mp->mscp_msgtc);
166 1.1 ragge /*
167 1.1 ragge * Controllers are allowed to interrupt as any drive, so we
168 1.1 ragge * must check the command before checking for a drive.
169 1.1 ragge */
170 1.1 ragge if (mp->mscp_opcode == (M_OP_SETCTLRC | M_OP_END)) {
171 1.1 ragge if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS) {
172 1.1 ragge mi->mi_flags |= MSC_READY;
173 1.1 ragge } else {
174 1.4 christos printf("%s: SETCTLRC failed: %d ",
175 1.1 ragge mi->mi_dev.dv_xname, mp->mscp_status);
176 1.1 ragge mscp_printevent(mp);
177 1.1 ragge }
178 1.1 ragge goto done;
179 1.1 ragge }
180 1.1 ragge
181 1.1 ragge /*
182 1.1 ragge * Found a response. Update credit information. If there is
183 1.1 ragge * nothing else to do, jump to `done' to get the next response.
184 1.1 ragge */
185 1.2 ragge if (mp->mscp_unit >= mi->mi_driveno) { /* Must expand drive table */
186 1.2 ragge int tmpno = ((mp->mscp_unit + 32) & 0xffe0) * sizeof(void *);
187 1.2 ragge struct device **tmp = (struct device **)
188 1.17 tsutsui malloc(tmpno, M_DEVBUF, M_NOWAIT|M_ZERO);
189 1.2 ragge if (mi->mi_driveno) {
190 1.2 ragge bcopy(mi->mi_dp, tmp, mi->mi_driveno);
191 1.2 ragge free(mi->mi_dp, mi->mi_driveno);
192 1.2 ragge }
193 1.2 ragge mi->mi_driveno = tmpno;
194 1.2 ragge mi->mi_dp = tmp;
195 1.2 ragge }
196 1.2 ragge
197 1.1 ragge drive = mi->mi_dp[mp->mscp_unit];
198 1.1 ragge
199 1.1 ragge switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
200 1.1 ragge
201 1.1 ragge case MSCPT_SEQ:
202 1.1 ragge break;
203 1.1 ragge
204 1.1 ragge case MSCPT_DATAGRAM:
205 1.2 ragge (*me->me_dgram)(drive, mp, mi);
206 1.1 ragge goto done;
207 1.1 ragge
208 1.1 ragge case MSCPT_CREDITS:
209 1.1 ragge goto done;
210 1.1 ragge
211 1.1 ragge case MSCPT_MAINTENANCE:
212 1.1 ragge default:
213 1.4 christos printf("%s: unit %d: unknown message type 0x%x ignored\n",
214 1.1 ragge mi->mi_dev.dv_xname, mp->mscp_unit,
215 1.1 ragge MSCP_MSGTYPE(mp->mscp_msgtc));
216 1.1 ragge goto done;
217 1.1 ragge }
218 1.1 ragge
219 1.1 ragge /*
220 1.1 ragge * Handle individual responses.
221 1.1 ragge */
222 1.1 ragge st = mp->mscp_status & M_ST_MASK;
223 1.1 ragge error = 0;
224 1.1 ragge switch (mp->mscp_opcode) {
225 1.1 ragge
226 1.1 ragge case M_OP_END:
227 1.1 ragge /*
228 1.1 ragge * The controller presents a bogus END packet when
229 1.1 ragge * a read/write command is given with an illegal
230 1.1 ragge * block number. This is contrary to the MSCP
231 1.1 ragge * specification (ENDs are to be given only for
232 1.1 ragge * invalid commands), but that is the way of it.
233 1.1 ragge */
234 1.1 ragge if (st == M_ST_INVALCMD && mp->mscp_cmdref != 0) {
235 1.4 christos printf("%s: bad lbn (%d)?\n", drive->dv_xname,
236 1.1 ragge (int)mp->mscp_seq.seq_lbn);
237 1.1 ragge error = EIO;
238 1.1 ragge goto rwend;
239 1.1 ragge }
240 1.1 ragge goto unknown;
241 1.1 ragge
242 1.1 ragge case M_OP_ONLINE | M_OP_END:
243 1.1 ragge /*
244 1.7 ragge * Finished an ON LINE request. Call the driver to
245 1.1 ragge * find out whether it succeeded. If so, mark it on
246 1.1 ragge * line.
247 1.1 ragge */
248 1.1 ragge (*me->me_online)(drive, mp);
249 1.1 ragge break;
250 1.1 ragge
251 1.1 ragge case M_OP_GETUNITST | M_OP_END:
252 1.1 ragge /*
253 1.1 ragge * Got unit status. If we are autoconfiguring, save
254 1.1 ragge * the mscp struct so that mscp_attach know what to do.
255 1.1 ragge * If the drive isn't configured, call config_found()
256 1.1 ragge * to set it up, otherwise it's just a "normal" unit
257 1.1 ragge * status.
258 1.1 ragge */
259 1.1 ragge if (cold)
260 1.1 ragge bcopy(mp, &slavereply, sizeof(struct mscp));
261 1.1 ragge
262 1.2 ragge if (mp->mscp_status == (M_ST_OFFLINE|M_OFFLINE_UNKNOWN))
263 1.2 ragge break;
264 1.2 ragge
265 1.1 ragge if (drive == 0) {
266 1.1 ragge struct drive_attach_args da;
267 1.1 ragge
268 1.1 ragge da.da_mp = (struct mscp *)mp;
269 1.2 ragge da.da_typ = mi->mi_type;
270 1.1 ragge config_found(&mi->mi_dev, (void *)&da, mscp_print);
271 1.1 ragge } else
272 1.2 ragge /* Hack to avoid complaints */
273 1.2 ragge if (!(((mp->mscp_event & M_ST_MASK) == M_ST_AVAILABLE)
274 1.2 ragge && cold))
275 1.2 ragge (*me->me_gotstatus)(drive, mp);
276 1.1 ragge break;
277 1.1 ragge
278 1.1 ragge case M_OP_AVAILATTN:
279 1.1 ragge /*
280 1.1 ragge * The drive went offline and we did not notice.
281 1.1 ragge * Mark it off line now, to force an on line request
282 1.1 ragge * next, so we can make sure it is still the same
283 1.1 ragge * drive.
284 1.1 ragge *
285 1.1 ragge * IF THE UDA DRIVER HAS A COMMAND AWAITING UNIBUS
286 1.1 ragge * RESOURCES, THAT COMMAND MAY GO OUT BEFORE THE ON
287 1.1 ragge * LINE. IS IT WORTH FIXING??
288 1.1 ragge */
289 1.1 ragge #ifdef notyet
290 1.1 ragge (*md->md_offline)(ui, mp);
291 1.1 ragge #endif
292 1.1 ragge break;
293 1.1 ragge
294 1.2 ragge case M_OP_POS | M_OP_END:
295 1.2 ragge case M_OP_WRITM | M_OP_END:
296 1.9 ragge case M_OP_AVAILABLE | M_OP_END:
297 1.2 ragge /*
298 1.2 ragge * A non-data transfer operation completed.
299 1.2 ragge */
300 1.2 ragge (*me->me_cmddone)(drive, mp);
301 1.2 ragge break;
302 1.2 ragge
303 1.1 ragge case M_OP_READ | M_OP_END:
304 1.1 ragge case M_OP_WRITE | M_OP_END:
305 1.1 ragge /*
306 1.7 ragge * A transfer finished. Get the buffer, and release its
307 1.7 ragge * map registers via ubadone(). If the command finished
308 1.1 ragge * with an off line or available status, the drive went
309 1.1 ragge * off line (the idiot controller does not tell us until
310 1.1 ragge * it comes back *on* line, or until we try to use it).
311 1.1 ragge */
312 1.6 ragge rwend:
313 1.5 ragge #ifdef DIAGNOSTIC
314 1.11 ragge if (mp->mscp_cmdref >= NCMD) {
315 1.1 ragge /*
316 1.1 ragge * No buffer means there is a bug somewhere!
317 1.1 ragge */
318 1.11 ragge printf("%s: io done, but bad xfer number?\n",
319 1.1 ragge drive->dv_xname);
320 1.1 ragge mscp_hexdump(mp);
321 1.1 ragge break;
322 1.1 ragge }
323 1.5 ragge #endif
324 1.1 ragge
325 1.6 ragge if (mp->mscp_cmdref == -1) {
326 1.6 ragge (*me->me_cmddone)(drive, mp);
327 1.6 ragge break;
328 1.6 ragge }
329 1.11 ragge mxi = &mi->mi_xi[mp->mscp_cmdref];
330 1.11 ragge if (mxi->mxi_inuse == 0)
331 1.11 ragge panic("mxi not inuse");
332 1.11 ragge bp = mxi->mxi_bp;
333 1.1 ragge /*
334 1.1 ragge * Mark any error-due-to-bad-LBN (via `goto rwend').
335 1.7 ragge * WHAT STATUS WILL THESE HAVE? IT SURE WOULD BE NICE
336 1.1 ragge * IF DEC SOLD DOCUMENTATION FOR THEIR OWN CONTROLLERS.
337 1.1 ragge */
338 1.1 ragge if (error) {
339 1.1 ragge bp->b_flags |= B_ERROR;
340 1.1 ragge bp->b_error = error;
341 1.1 ragge }
342 1.1 ragge if (st == M_ST_OFFLINE || st == M_ST_AVAILABLE) {
343 1.1 ragge #ifdef notyet
344 1.1 ragge (*md->md_offline)(ui, mp);
345 1.1 ragge #endif
346 1.1 ragge }
347 1.1 ragge
348 1.1 ragge /*
349 1.1 ragge * If the transfer has something to do with bad
350 1.1 ragge * block forwarding, let the driver handle the
351 1.1 ragge * rest.
352 1.1 ragge */
353 1.1 ragge if ((bp->b_flags & B_BAD) != 0 && me->me_bb != NULL) {
354 1.1 ragge (*me->me_bb)(drive, mp, bp);
355 1.1 ragge goto out;
356 1.1 ragge }
357 1.1 ragge
358 1.1 ragge /*
359 1.1 ragge * If the transfer failed, give the driver a crack
360 1.1 ragge * at fixing things up.
361 1.1 ragge */
362 1.1 ragge if (st != M_ST_SUCCESS) {
363 1.1 ragge switch ((*me->me_ioerr)(drive, mp, bp)) {
364 1.1 ragge
365 1.1 ragge case MSCP_DONE: /* fixed */
366 1.1 ragge break;
367 1.1 ragge
368 1.1 ragge case MSCP_RESTARTED: /* still working on it */
369 1.1 ragge goto out;
370 1.1 ragge
371 1.1 ragge case MSCP_FAILED: /* no luck */
372 1.1 ragge /* XXX must move to ra.c */
373 1.1 ragge mscp_printevent(mp);
374 1.1 ragge break;
375 1.1 ragge }
376 1.1 ragge }
377 1.1 ragge
378 1.1 ragge /*
379 1.1 ragge * Set the residual count and mark the transfer as
380 1.1 ragge * done. If the I/O wait queue is now empty, release
381 1.1 ragge * the shared BDP, if any.
382 1.1 ragge */
383 1.1 ragge bp->b_resid = bp->b_bcount - mp->mscp_seq.seq_bytecount;
384 1.11 ragge bus_dmamap_unload(mi->mi_dmat, mxi->mxi_dmam);
385 1.1 ragge
386 1.11 ragge (*mc->mc_ctlrdone)(mi->mi_dev.dv_parent);
387 1.1 ragge (*me->me_iodone)(drive, bp);
388 1.1 ragge out:
389 1.11 ragge mxi->mxi_inuse = 0;
390 1.11 ragge mi->mi_mxiuse |= (1 << mp->mscp_cmdref);
391 1.1 ragge break;
392 1.1 ragge
393 1.1 ragge case M_OP_REPLACE | M_OP_END:
394 1.1 ragge /*
395 1.1 ragge * A replace operation finished. Just let the driver
396 1.1 ragge * handle it (if it does replaces).
397 1.1 ragge */
398 1.1 ragge if (me->me_replace == NULL)
399 1.4 christos printf("%s: bogus REPLACE end\n", drive->dv_xname);
400 1.1 ragge else
401 1.1 ragge (*me->me_replace)(drive, mp);
402 1.1 ragge break;
403 1.1 ragge
404 1.1 ragge default:
405 1.1 ragge /*
406 1.1 ragge * If it is not one of the above, we cannot handle it.
407 1.1 ragge * (And we should not have received it, for that matter.)
408 1.1 ragge */
409 1.1 ragge unknown:
410 1.4 christos printf("%s: unknown opcode 0x%x status 0x%x ignored\n",
411 1.1 ragge drive->dv_xname, mp->mscp_opcode, mp->mscp_status);
412 1.5 ragge #ifdef DIAGNOSTIC
413 1.1 ragge mscp_hexdump(mp);
414 1.5 ragge #endif
415 1.1 ragge break;
416 1.1 ragge }
417 1.1 ragge
418 1.1 ragge /*
419 1.1 ragge * If the drive needs to be put back in the controller queue,
420 1.7 ragge * do that now. (`bp' below ought to be `dp', but they are all
421 1.1 ragge * struct buf *.) Note that b_active was cleared in the driver;
422 1.1 ragge * we presume that there is something to be done, hence reassert it.
423 1.1 ragge */
424 1.1 ragge #ifdef notyet /* XXX */
425 1.1 ragge if (ui->ui_flags & UNIT_REQUEUE) {
426 1.11 ragge ...
427 1.1 ragge }
428 1.1 ragge #endif
429 1.1 ragge done:
430 1.1 ragge /*
431 1.1 ragge * Give back the response packet, and take a look at the next.
432 1.1 ragge */
433 1.1 ragge mp->mscp_msglen = MSCP_MSGLEN;
434 1.1 ragge mi->mi_rsp.mri_desc[nextrsp] |= MSCP_OWN;
435 1.1 ragge nextrsp = (nextrsp + 1) % mi->mi_rsp.mri_size;
436 1.1 ragge goto loop;
437 1.1 ragge }
438 1.1 ragge
439 1.1 ragge /*
440 1.1 ragge * Requeue outstanding transfers, e.g., after bus reset.
441 1.1 ragge * Also requeue any drives that have on line or unit status
442 1.1 ragge * info pending.
443 1.1 ragge */
444 1.1 ragge void
445 1.1 ragge mscp_requeue(mi)
446 1.1 ragge struct mscp_softc *mi;
447 1.1 ragge {
448 1.11 ragge panic("mscp_requeue");
449 1.1 ragge }
450 1.1 ragge
451