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