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