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