summaryrefslogtreecommitdiff
path: root/sys/arch/newsmips/dev/sc_wrap.c
blob: 46965029d826734a977eb4e68d034a6d75aedd8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*	$NetBSD: sc_wrap.c,v 1.33 2021/08/07 16:19:01 thorpej Exp $	*/

/*
 * This driver is slow!  Need to rewrite.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sc_wrap.c,v 1.33 2021/08/07 16:19:01 thorpej Exp $");

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/malloc.h>

#include <uvm/uvm_extern.h>

#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/scsipi/scsi_message.h>

#include <newsmips/dev/hbvar.h>
#include <newsmips/dev/scsireg.h>
#include <newsmips/dev/dmac_0448.h>
#include <newsmips/dev/screg_1185.h>

#include <machine/adrsmap.h>
#include <machine/autoconf.h>
#include <machine/machConst.h>

#include <mips/cache.h>

static int cxd1185_match(device_t, cfdata_t, void *);
static void cxd1185_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(sc, sizeof(struct sc_softc),
    cxd1185_match, cxd1185_attach, NULL, NULL);

void cxd1185_init(struct sc_softc *);
static void free_scb(struct sc_softc *, struct sc_scb *);
static struct sc_scb *get_scb(struct sc_softc *, int);
static void sc_scsipi_request(struct scsipi_channel *,
    scsipi_adapter_req_t, void *);
static int sc_poll(struct sc_softc *, int, int);
static void sc_sched(struct sc_softc *);
void sc_done(struct sc_scb *);
int sc_intr(void *);
static void cxd1185_timeout(void *);

extern void sc_send(struct sc_scb *, int, int);
extern int scintr(void);
extern void scsi_hardreset(void);
extern int sc_busy(struct sc_softc *, int);
extern paddr_t kvtophys(vaddr_t);

static int sc_disconnect = IDT_DISCON;

int
cxd1185_match(device_t parent, cfdata_t cf, void *aux)
{
	struct hb_attach_args *ha = aux;

	if (strcmp(ha->ha_name, "sc"))
		return 0;

	return 1;
}

void
cxd1185_attach(device_t parent, device_t self, void *aux)
{
	struct sc_softc *sc = device_private(self);
	struct hb_attach_args *ha = aux;
	struct sc_scb *scb;
	int i, intlevel;

	sc->sc_dev = self;

	intlevel = ha->ha_level;
	if (intlevel == -1) {
#if 0
		aprint_error(": interrupt level not configured\n");
		return;
#else
		aprint_normal(": interrupt level not configured; using");
		intlevel = 0;
#endif
	}
	aprint_normal(" level %d\n", intlevel);

	if (sc_idenr & 0x08)
		sc->scsi_1185AQ = 1;
	else
		sc->scsi_1185AQ = 0;

	sc->sc_adapter.adapt_dev = self;
	sc->sc_adapter.adapt_nchannels = 1;
	sc->sc_adapter.adapt_openings = 7;
	sc->sc_adapter.adapt_max_periph = 1;
	sc->sc_adapter.adapt_ioctl = NULL;
	sc->sc_adapter.adapt_minphys = minphys;
	sc->sc_adapter.adapt_request = sc_scsipi_request;

	memset(&sc->sc_channel, 0, sizeof(sc->sc_channel));
	sc->sc_channel.chan_adapter = &sc->sc_adapter;
	sc->sc_channel.chan_bustype = &scsi_bustype;
	sc->sc_channel.chan_channel = 0;
	sc->sc_channel.chan_ntargets = 8;
	sc->sc_channel.chan_nluns = 8;
	sc->sc_channel.chan_id = 7;

	TAILQ_INIT(&sc->ready_list);
	TAILQ_INIT(&sc->free_list);

	scb = sc->sc_scb;
	for (i = 0; i < 24; i++) {	/* XXX 24 */
		TAILQ_INSERT_TAIL(&sc->free_list, scb, chain);
		scb++;
	}

	cxd1185_init(sc);
	DELAY(100000);

	hb_intr_establish(intlevel, INTEN1_DMA, IPL_BIO, sc_intr, sc);

	config_found(self, &sc->sc_channel, scsiprint, CFARGS_NONE);
}

void
cxd1185_init(struct sc_softc *sc)
{
	int i;

	for (i = 0; i < 8; i++)
		sc->inuse[i] = 0;

	scsi_hardreset();
}

void
free_scb(struct sc_softc *sc, struct sc_scb *scb)
{
	int s;

	s = splbio();

	TAILQ_INSERT_HEAD(&sc->free_list, scb, chain);

	/*
	 * If there were none, wake anybody waiting for one to come free,
	 * starting with queued entries.
	 */
	if (scb->chain.tqe_next == 0)
		wakeup(&sc->free_list);

	splx(s);
}

struct sc_scb *
get_scb(struct sc_softc *sc, int flags)
{
	int s;
	struct sc_scb *scb;

	s = splbio();

	while ((scb = sc->free_list.tqh_first) == NULL &&
		(flags & XS_CTL_NOSLEEP) == 0)
		tsleep(&sc->free_list, PRIBIO, "sc_scb", 0);
	if (scb) {
		TAILQ_REMOVE(&sc->free_list, scb, chain);
	}

	splx(s);
	return scb;
}

void
sc_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    void *arg)
{
	struct scsipi_xfer *xs;
	struct scsipi_periph *periph;
	struct sc_softc *sc = device_private(chan->chan_adapter->adapt_dev);
	struct sc_scb *scb;
	int flags, s;
	int target;

	switch (req) {
	case ADAPTER_REQ_RUN_XFER:
		xs = arg;
		periph = xs->xs_periph;

		flags = xs->xs_control;
		if ((scb = get_scb(sc, flags)) == NULL)
			panic("%s: no scb", __func__);

		scb->xs = xs;
		scb->flags = 0;
		scb->sc_ctag = 0;
		scb->sc_coffset = 0;
		scb->istatus = 0;
		scb->tstatus = 0;
		scb->message = 0;
		memset(scb->msgbuf, 0, sizeof(scb->msgbuf));

		s = splbio();

		TAILQ_INSERT_TAIL(&sc->ready_list, scb, chain);
		sc_sched(sc);
		splx(s);

		if (flags & XS_CTL_POLL) {
			target = periph->periph_target;
			if (sc_poll(sc, target, xs->timeout)) {
				printf("sc: timeout (retry)\n");
				if (sc_poll(sc, target, xs->timeout)) {
					printf("sc: timeout\n");
				}
			}
			/* called during autoconfig only... */
			mips_dcache_wbinv_all();	/* Flush DCache */
		}
		return;
	case ADAPTER_REQ_GROW_RESOURCES:
		/* XXX Not supported. */
		return;
	case ADAPTER_REQ_SET_XFER_MODE:
		/* XXX Not supported. */
		return;
	}
}

/*
 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
 */
int
sc_poll(struct sc_softc *sc, int chan, int count)
{
	volatile uint8_t *int_stat = (void *)INTST1;
	volatile uint8_t *int_clear = (void *)INTCLR1;

	while (sc_busy(sc, chan)) {
		if (*int_stat & INTST1_DMA) {
		    *int_clear = INTST1_DMA;
		    if (dmac_gstat & CH_INT(CH_SCSI)) {
			if (dmac_gstat & CH_MRQ(CH_SCSI)) {
			    DELAY(50);
			    if (dmac_gstat & CH_MRQ(CH_SCSI))
				printf("dma_poll\n");
			}
			DELAY(10);
			scintr();
		    }
		}
		DELAY(1000);
		count--;
		if (count <= 0)
			return 1;
	}
	return 0;
}

void
sc_sched(struct sc_softc *sc)
{
	struct scsipi_xfer *xs;
	struct scsipi_periph *periph;
	int ie = 0;
	int flags;
	int chan, lun;
	struct sc_scb *scb, *nextscb;

	scb = sc->ready_list.tqh_first;
start:
	if (scb == NULL)
		return;

	xs = scb->xs;
	periph = xs->xs_periph;
	chan = periph->periph_target;
	flags = xs->xs_control;

	if (sc->inuse[chan]) {
		scb = scb->chain.tqe_next;
		goto start;
	}
	sc->inuse[chan] = 1;

	if (flags & XS_CTL_RESET)
		printf("SCSI RESET\n");

	lun = periph->periph_lun;

	scb->identify = MSG_IDENT | sc_disconnect | (lun & IDT_DRMASK);
	scb->sc_ctrnscnt = xs->datalen;

	/* make va->pa mapping table for DMA */
	if (xs->datalen > 0) {
		uint32_t pn, pages, offset;
		int i;
		vaddr_t va;

#if 0
		memset(&sc->sc_map[chan], 0, sizeof(struct sc_map));
#endif

		va = (vaddr_t)xs->data;

		offset = va & PGOFSET;
		pages = (offset + xs->datalen + PAGE_SIZE -1 ) >> PGSHIFT;
		if (pages >= NSCMAP)
			panic("sc_map: Too many pages");

		for (i = 0; i < pages; i++) {
			pn = kvtophys(va) >> PGSHIFT;
			sc->sc_map[chan].mp_addr[i] = pn;
			va += PAGE_SIZE;
		}

		sc->sc_map[chan].mp_offset = offset;
		sc->sc_map[chan].mp_pages = pages;
		scb->sc_map = &sc->sc_map[chan];
	}

	if ((flags & XS_CTL_POLL) == 0)
		ie = SCSI_INTEN;

	if (xs->data)
		scb->sc_cpoint = (void *)xs->data;
	else
		scb->sc_cpoint = scb->msgbuf;
	scb->scb_softc = sc;

	callout_reset(&scb->xs->xs_callout, hz * 10, cxd1185_timeout, scb);
	sc_send(scb, chan, ie);
	callout_stop(&scb->xs->xs_callout);

	nextscb = scb->chain.tqe_next;

	TAILQ_REMOVE(&sc->ready_list, scb, chain);

	scb = nextscb;

	goto start;
}

void
sc_done(struct sc_scb *scb)
{
	struct scsipi_xfer *xs = scb->xs;
	struct scsipi_periph *periph = xs->xs_periph;
	struct sc_softc *sc;

	sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
	xs->resid = 0;
	xs->status = 0;

	if (scb->istatus != INST_EP) {
		if (scb->istatus == (INST_EP|INST_TO))
			xs->error = XS_SELTIMEOUT;
		else {
			printf("SC(i): [istatus=0x%x, tstatus=0x%x]\n",
				scb->istatus, scb->tstatus);
			xs->error = XS_DRIVER_STUFFUP;
		}
	}

	switch (scb->tstatus) {

	case TGST_GOOD:
		break;

	case TGST_CC:
		xs->status = SCSI_CHECK;
		if (xs->error == 0)
			xs->error = XS_BUSY;
		break;

	default:
		printf("SC(t): [istatus=0x%x, tstatus=0x%x]\n",
			scb->istatus, scb->tstatus);
		break;
	}

	scsipi_done(xs);
	free_scb(sc, scb);
	sc->inuse[periph->periph_target] = 0;
	sc_sched(sc);
}

int
sc_intr(void *v)
{
	/* struct sc_softc *sc = v; */
	volatile uint8_t *gsp = (uint8_t *)DMAC_GSTAT;
	u_int gstat = *gsp;
	int mrqb, i;

	if ((gstat & CH_INT(CH_SCSI)) == 0)
		return 0;

	/*
	 * when DMA interrupt occurs there remain some untransferred data.
	 * wait data transfer completion.
	 */
	mrqb = (gstat & CH_INT(CH_SCSI)) << 1;
	if (gstat & mrqb) {
		/*
		 * XXX SHOULD USE DELAY()
		 */
		for (i = 0; i < 50; i++)
			;
		if (*gsp & mrqb)
			printf("%s: MRQ\n", __func__);
	}
	scintr();

	return 1;
}


#if 0
/*
 * SCOP_RSENSE request
 */
void
scop_rsense(int intr, struct scsi *sc_param, int lun, int ie, int count,
    void *param)
{

	memset(sc_param, 0, sizeof(struct scsi));
	sc_param->identify = MSG_IDENT | sc_disconnect | (lun & IDT_DRMASK);
	sc_param->sc_lun = lun;

	sc_param->sc_cpoint = (uint8_t *)param;
	sc_param->sc_ctrnscnt = count;

	/* sc_cdb */
	sc_param->sc_opcode = SCOP_RSENSE;
	sc_param->sc_count = count;

	sc_go(intr, sc_param, ie, sc_param);
}
#endif

void
cxd1185_timeout(void *arg)
{
	struct sc_scb *scb = arg;
	struct scsipi_xfer *xs = scb->xs;
	struct scsipi_periph *periph = xs->xs_periph;
	int chan;

	chan = periph->periph_target;

	printf("sc: timeout ch=%d\n", chan);

	/* XXX abort transfer and ... */
}