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