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