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