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