Home | History | Annotate | Line # | Download | only in dev
sc_wrap.c revision 1.16
      1 /*	$NetBSD: sc_wrap.c,v 1.16 2000/12/03 01:42:30 matt Exp $	*/
      2 
      3 /*
      4  * This driver is slow!  Need to rewrite.
      5  */
      6 
      7 #include <sys/types.h>
      8 #include <sys/param.h>
      9 #include <sys/systm.h>
     10 #include <sys/kernel.h>
     11 #include <sys/device.h>
     12 #include <sys/proc.h>
     13 #include <sys/buf.h>
     14 #include <sys/malloc.h>
     15 
     16 #include <dev/scsipi/scsi_all.h>
     17 #include <dev/scsipi/scsipi_all.h>
     18 #include <dev/scsipi/scsiconf.h>
     19 #include <dev/scsipi/scsi_message.h>
     20 
     21 #include <newsmips/dev/scsireg.h>
     22 #include <newsmips/dev/dmac_0448.h>
     23 #include <newsmips/dev/screg_1185.h>
     24 
     25 #include <machine/locore.h>
     26 #include <machine/adrsmap.h>
     27 #include <machine/autoconf.h>
     28 #include <machine/machConst.h>
     29 
     30 static int cxd1185_match __P((struct device *, struct cfdata *, void *));
     31 static void cxd1185_attach __P((struct device *, struct device *, void *));
     32 
     33 struct cfattach sc_ca = {
     34 	sizeof(struct sc_softc), cxd1185_match, cxd1185_attach
     35 };
     36 
     37 void cxd1185_init __P((struct sc_softc *));
     38 static void free_scb __P((struct sc_softc *, struct sc_scb *));
     39 static struct sc_scb *get_scb __P((struct sc_softc *, int));
     40 static int sc_scsi_cmd __P((struct scsipi_xfer *));
     41 static int sc_poll __P((struct sc_softc *, int, int));
     42 static void sc_sched __P((struct sc_softc *));
     43 void sc_done __P((struct sc_scb *));
     44 int sc_intr __P((void *));
     45 static void cxd1185_timeout __P((void *));
     46 
     47 extern void sc_send __P((struct sc_scb *, int, int));
     48 extern int scintr __P((void));
     49 extern void scsi_hardreset __P((void));
     50 extern int sc_busy __P((struct sc_softc *, int));
     51 extern paddr_t kvtophys __P((vaddr_t));
     52 
     53 static int sc_disconnect = IDT_DISCON;
     54 
     55 struct scsipi_device cxd1185_dev = {
     56 	NULL,
     57 	NULL,
     58 	NULL,
     59 	NULL
     60 };
     61 
     62 int
     63 cxd1185_match(parent, cf, aux)
     64 	struct device *parent;
     65 	struct cfdata *cf;
     66 	void *aux;
     67 {
     68 	struct confargs *ca = aux;
     69 
     70 	if (strcmp(ca->ca_name, "sc"))
     71 		return 0;
     72 
     73 	return 1;
     74 }
     75 
     76 void
     77 cxd1185_attach(parent, self, aux)
     78 	struct device *parent, *self;
     79 	void *aux;
     80 {
     81 	struct sc_softc *sc = (void *)self;
     82 	struct sc_scb *scb;
     83 	int i, intlevel;
     84 
     85 	intlevel = sc->sc_dev.dv_cfdata->cf_level;
     86 	if (intlevel == -1) {
     87 #if 0
     88 		printf(": interrupt level not configured\n");
     89 		return;
     90 #else
     91 		printf(": interrupt level not configured; using");
     92 		intlevel = 0;
     93 #endif
     94 	}
     95 	printf(" level %d\n", intlevel);
     96 
     97 	if (sc_idenr & 0x08)
     98 		sc->scsi_1185AQ = 1;
     99 	else
    100 		sc->scsi_1185AQ = 0;
    101 
    102 	sc->sc_adapter.scsipi_cmd = sc_scsi_cmd;
    103 	sc->sc_adapter.scsipi_minphys = minphys;
    104 
    105 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    106 	sc->sc_link.adapter_softc = sc;
    107 	sc->sc_link.scsipi_scsi.adapter_target = 7;
    108 	sc->sc_link.adapter = &sc->sc_adapter;
    109 	sc->sc_link.device = &cxd1185_dev;
    110 	sc->sc_link.openings = 2;
    111 	sc->sc_link.scsipi_scsi.max_target = 7;
    112 	sc->sc_link.scsipi_scsi.max_lun = 7;
    113 	sc->sc_link.type = BUS_SCSI;
    114 
    115 	TAILQ_INIT(&sc->ready_list);
    116 	TAILQ_INIT(&sc->free_list);
    117 
    118 	scb = sc->sc_scb;
    119 	for (i = 0; i < 24; i++) {	/* XXX 24 */
    120 		TAILQ_INSERT_TAIL(&sc->free_list, scb, chain);
    121 		scb++;
    122 	}
    123 
    124 	cxd1185_init(sc);
    125 	DELAY(100000);
    126 
    127 	hb_intr_establish(intlevel, IPL_BIO, sc_intr, sc);
    128 
    129 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    130 }
    131 
    132 void
    133 cxd1185_init(sc)
    134 	struct sc_softc *sc;
    135 {
    136 	int i;
    137 
    138 	for (i = 0; i < 8; i++)
    139 		sc->inuse[i] = 0;
    140 
    141 	scsi_hardreset();
    142 }
    143 
    144 void
    145 free_scb(sc, scb)
    146 	struct sc_softc *sc;
    147 	struct sc_scb *scb;
    148 {
    149 	int s;
    150 
    151 	s = splbio();
    152 
    153 	TAILQ_INSERT_HEAD(&sc->free_list, scb, chain);
    154 
    155 	/*
    156 	 * If there were none, wake anybody waiting for one to come free,
    157 	 * starting with queued entries.
    158 	 */
    159 	if (scb->chain.tqe_next == 0)
    160 		wakeup(&sc->free_list);
    161 
    162 	splx(s);
    163 }
    164 
    165 struct sc_scb *
    166 get_scb(sc, flags)
    167 	struct sc_softc *sc;
    168 	int flags;
    169 {
    170 	int s;
    171 	struct sc_scb *scb;
    172 
    173 	s = splbio();
    174 
    175 	while ((scb = sc->free_list.tqh_first) == NULL &&
    176 		(flags & XS_CTL_NOSLEEP) == 0)
    177 		tsleep(&sc->free_list, PRIBIO, "sc_scb", 0);
    178 	if (scb) {
    179 		TAILQ_REMOVE(&sc->free_list, scb, chain);
    180 	}
    181 
    182 	splx(s);
    183 	return scb;
    184 }
    185 
    186 int
    187 sc_scsi_cmd(xs)
    188 	struct scsipi_xfer *xs;
    189 {
    190 	struct scsipi_link *sc_link = xs->sc_link;
    191 	struct sc_softc *sc = sc_link->adapter_softc;
    192 	struct sc_scb *scb;
    193 	int flags, s;
    194 	int chan;
    195 
    196 	flags = xs->xs_control;
    197 	if ((scb = get_scb(sc, flags)) == NULL)
    198 		return TRY_AGAIN_LATER;
    199 
    200 	scb->xs = xs;
    201 	scb->flags = 0;
    202 	scb->sc_ctag = 0;
    203 	scb->sc_coffset = 0;
    204 	scb->istatus = 0;
    205 	scb->tstatus = 0;
    206 	scb->message = 0;
    207 	bzero(scb->msgbuf, sizeof(scb->msgbuf));
    208 
    209 	s = splbio();
    210 
    211 	TAILQ_INSERT_TAIL(&sc->ready_list, scb, chain);
    212 	sc_sched(sc);
    213 	splx(s);
    214 
    215 	if ((flags & XS_CTL_POLL) == 0)
    216 		return SUCCESSFULLY_QUEUED;
    217 
    218 	chan = sc_link->scsipi_scsi.target;
    219 
    220 	if (sc_poll(sc, chan, xs->timeout)) {
    221 		printf("sc: timeout (retry)\n");
    222 		if (sc_poll(sc, chan, xs->timeout)) {
    223 			printf("sc: timeout\n");
    224 			return COMPLETE;
    225 		}
    226 	}
    227 
    228 	/* called during autoconfig only... */
    229 
    230 	MachFlushCache(); /* Flush all caches */
    231 	return COMPLETE;
    232 }
    233 
    234 /*
    235  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
    236  */
    237 int
    238 sc_poll(sc, chan, count)
    239 	struct sc_softc *sc;
    240 	int chan, count;
    241 {
    242 	volatile u_char *int_stat = (void *)INTST1;
    243 	volatile u_char *int_clear = (void *)INTCLR1;
    244 
    245 	while (sc_busy(sc, chan)) {
    246 		if (*int_stat & INTST1_DMA) {
    247 		    *int_clear = INTST1_DMA;
    248 		    if (dmac_gstat & CH_INT(CH_SCSI)) {
    249 			if (dmac_gstat & CH_MRQ(CH_SCSI)) {
    250 			    DELAY(50);
    251 			    if (dmac_gstat & CH_MRQ(CH_SCSI))
    252 				printf("dma_poll\n");
    253 			}
    254 			DELAY(10);
    255 			scintr();
    256 		    }
    257 		}
    258 		DELAY(1000);
    259 		count--;
    260 		if (count <= 0)
    261 			return 1;
    262 	}
    263 	return 0;
    264 }
    265 
    266 void
    267 sc_sched(sc)
    268 	struct sc_softc *sc;
    269 {
    270 	struct scsipi_xfer *xs;
    271 	struct scsipi_link *sc_link;
    272 	int ie = 0;
    273 	int flags;
    274 	int chan, lun;
    275 	struct sc_scb *scb, *nextscb;
    276 
    277 	scb = sc->ready_list.tqh_first;
    278 start:
    279 	if (scb == NULL)
    280 		return;
    281 
    282 	xs = scb->xs;
    283 	sc_link = xs->sc_link;
    284 	chan = sc_link->scsipi_scsi.target;
    285 	flags = xs->xs_control;
    286 
    287 	if (cold)
    288 		flags |= XS_CTL_POLL;
    289 
    290 	if (sc->inuse[chan]) {
    291 		scb = scb->chain.tqe_next;
    292 		goto start;
    293 	}
    294 	sc->inuse[chan] = 1;
    295 
    296 	if (flags & XS_CTL_RESET)
    297 		printf("SCSI RESET\n");
    298 
    299 	lun = sc_link->scsipi_scsi.lun;
    300 
    301 	scb->identify = MSG_IDENT | sc_disconnect | (lun & IDT_DRMASK);
    302 	scb->sc_ctrnscnt = xs->datalen;
    303 
    304 	/* make va->pa mapping table for dma */
    305 	if (xs->datalen > 0) {
    306 		int pages, offset;
    307 		int i, pn;
    308 		vaddr_t va;
    309 
    310 		/* bzero(&sc->sc_map[chan], sizeof(struct sc_map)); */
    311 
    312 		va = (vaddr_t)xs->data;
    313 
    314 		offset = va & PGOFSET;
    315 		pages = (offset + xs->datalen + NBPG -1 ) >> PGSHIFT;
    316 		if (pages >= NSCMAP)
    317 			panic("sc_map: Too many pages");
    318 
    319 		for (i = 0; i < pages; i++) {
    320 			pn = kvtophys(va) >> PGSHIFT;
    321 			sc->sc_map[chan].mp_addr[i] = pn;
    322 			va += NBPG;
    323 		}
    324 
    325 		sc->sc_map[chan].mp_offset = offset;
    326 		sc->sc_map[chan].mp_pages = pages;
    327 		scb->sc_map = &sc->sc_map[chan];
    328 	}
    329 
    330 	if ((flags & XS_CTL_POLL) == 0)
    331 		ie = SCSI_INTEN;
    332 
    333 	if (xs->data)
    334 		scb->sc_cpoint = (void *)xs->data;
    335 	else
    336 		scb->sc_cpoint = scb->msgbuf;
    337 	scb->scb_softc = sc;
    338 
    339 	callout_reset(&scb->xs->xs_callout, hz * 10, cxd1185_timeout, scb);
    340 	sc_send(scb, chan, ie);
    341 	callout_stop(&scb->xs->xs_callout);
    342 
    343 	nextscb = scb->chain.tqe_next;
    344 
    345 	TAILQ_REMOVE(&sc->ready_list, scb, chain);
    346 
    347 	scb = nextscb;
    348 
    349 	goto start;
    350 }
    351 
    352 void
    353 sc_done(scb)
    354 	struct sc_scb *scb;
    355 {
    356 	struct scsipi_xfer *xs = scb->xs;
    357 	struct scsipi_link *sc_link = xs->sc_link;
    358 	struct sc_softc *sc = sc_link->adapter_softc;
    359 
    360 	xs->xs_status |= XS_STS_DONE;
    361 	xs->resid = 0;
    362 	xs->status = 0;
    363 
    364 	if (scb->istatus != INST_EP) {
    365 		if (scb->istatus == (INST_EP|INST_TO))
    366 			xs->error = XS_SELTIMEOUT;
    367 		else {
    368 			printf("SC(i): [istatus=0x%x, tstatus=0x%x]\n",
    369 				scb->istatus, scb->tstatus);
    370 			xs->error = XS_DRIVER_STUFFUP;
    371 		}
    372 	}
    373 
    374 	switch (scb->tstatus) {
    375 
    376 	case TGST_GOOD:
    377 		break;
    378 
    379 	case TGST_CC:
    380 		break;		/* XXX */
    381 #if 0
    382 		chan = sc_link->scsipi_scsi.target;
    383 		lun = sc_link->scsipi_scsi.lun;
    384 		scop_rsense(chan, scb, lun, SCSI_INTDIS, 18, 0);
    385 		if (scb->tstatus != TGST_GOOD) {
    386 			printf("SC(t2): [istatus=0x%x, tstatus=0x%x]\n",
    387 				scb->istatus, scb->tstatus);
    388 		}
    389 #endif
    390 
    391 	default:
    392 		printf("SC(t): [istatus=0x%x, tstatus=0x%x]\n",
    393 			scb->istatus, scb->tstatus);
    394 		break;
    395 	}
    396 
    397 	scsipi_done(xs);
    398 	free_scb(sc, scb);
    399 	sc->inuse[sc_link->scsipi_scsi.target] = 0;
    400 	sc_sched(sc);
    401 }
    402 
    403 int
    404 sc_intr(v)
    405 	void *v;
    406 {
    407 	/* struct sc_softc *sc = v; */
    408 	volatile u_char *gsp = (u_char *)DMAC_GSTAT;
    409 	u_int gstat = *gsp;
    410 	int mrqb, i;
    411 
    412 	if ((gstat & CH_INT(CH_SCSI)) == 0)
    413 		return 0;
    414 
    415 	/*
    416 	 * when DMA interrupt occurs there remain some untransferred data.
    417 	 * wait data transfer completion.
    418 	 */
    419 	mrqb = (gstat & CH_INT(CH_SCSI)) << 1;
    420 	if (gstat & mrqb) {
    421 		/*
    422 		 * XXX SHOULD USE DELAY()
    423 		 */
    424 		for (i = 0; i < 50; i++)
    425 			;
    426 		if (*gsp & mrqb)
    427 			printf("sc_intr: MRQ\n");
    428 	}
    429 	scintr();
    430 
    431 	return 1;
    432 }
    433 
    434 
    435 #if 0
    436 /*
    437  * SCOP_RSENSE request
    438  */
    439 void
    440 scop_rsense(intr, sc_param, lun, ie, count, param)
    441 	register int intr;
    442 	register struct scsi *sc_param;
    443 	register int lun;
    444 	register int ie;
    445 	register int count;
    446 	register caddr_t param;
    447 {
    448 	bzero(sc_param, sizeof(struct scsi));
    449 	sc_param->identify = MSG_IDENT | sc_disconnect | (lun & IDT_DRMASK);
    450 	sc_param->sc_lun = lun;
    451 
    452 	sc_param->sc_cpoint = (u_char *)param;
    453 	sc_param->sc_ctrnscnt = count;
    454 
    455 	/* sc_cdb */
    456 	sc_param->sc_opcode = SCOP_RSENSE;
    457 	sc_param->sc_count = count;
    458 
    459 	sc_go(intr, sc_param, ie, sc_param);
    460 }
    461 #endif
    462 
    463 void
    464 cxd1185_timeout(arg)
    465 	void *arg;
    466 {
    467 	struct sc_scb *scb = arg;
    468 	struct scsipi_xfer *xs = scb->xs;
    469 	struct scsipi_link *sc_link = xs->sc_link;
    470 	int chan;
    471 
    472 	chan = sc_link->scsipi_scsi.target;
    473 
    474 	printf("sc: timeout ch=%d\n", chan);
    475 
    476 	/* XXX abort transfer and ... */
    477 }
    478