sc_wrap.c revision 1.24.2.3 1 /* $NetBSD: sc_wrap.c,v 1.24.2.3 2004/09/21 13:19:32 skrll 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.24.2.3 2004/09/21 13:19:32 skrll 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 (sc->inuse[chan]) {
302 scb = scb->chain.tqe_next;
303 goto start;
304 }
305 sc->inuse[chan] = 1;
306
307 if (flags & XS_CTL_RESET)
308 printf("SCSI RESET\n");
309
310 lun = periph->periph_lun;
311
312 scb->identify = MSG_IDENT | sc_disconnect | (lun & IDT_DRMASK);
313 scb->sc_ctrnscnt = xs->datalen;
314
315 /* make va->pa mapping table for DMA */
316 if (xs->datalen > 0) {
317 int pages, offset;
318 int i, pn;
319 vaddr_t va;
320
321 /* bzero(&sc->sc_map[chan], sizeof(struct sc_map)); */
322
323 va = (vaddr_t)xs->data;
324
325 offset = va & PGOFSET;
326 pages = (offset + xs->datalen + PAGE_SIZE -1 ) >> PGSHIFT;
327 if (pages >= NSCMAP)
328 panic("sc_map: Too many pages");
329
330 for (i = 0; i < pages; i++) {
331 pn = kvtophys(va) >> PGSHIFT;
332 sc->sc_map[chan].mp_addr[i] = pn;
333 va += PAGE_SIZE;
334 }
335
336 sc->sc_map[chan].mp_offset = offset;
337 sc->sc_map[chan].mp_pages = pages;
338 scb->sc_map = &sc->sc_map[chan];
339 }
340
341 if ((flags & XS_CTL_POLL) == 0)
342 ie = SCSI_INTEN;
343
344 if (xs->data)
345 scb->sc_cpoint = (void *)xs->data;
346 else
347 scb->sc_cpoint = scb->msgbuf;
348 scb->scb_softc = sc;
349
350 callout_reset(&scb->xs->xs_callout, hz * 10, cxd1185_timeout, scb);
351 sc_send(scb, chan, ie);
352 callout_stop(&scb->xs->xs_callout);
353
354 nextscb = scb->chain.tqe_next;
355
356 TAILQ_REMOVE(&sc->ready_list, scb, chain);
357
358 scb = nextscb;
359
360 goto start;
361 }
362
363 void
364 sc_done(scb)
365 struct sc_scb *scb;
366 {
367 struct scsipi_xfer *xs = scb->xs;
368 struct scsipi_periph *periph = xs->xs_periph;
369 struct sc_softc *sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
370
371 xs->resid = 0;
372 xs->status = 0;
373
374 if (scb->istatus != INST_EP) {
375 if (scb->istatus == (INST_EP|INST_TO))
376 xs->error = XS_SELTIMEOUT;
377 else {
378 printf("SC(i): [istatus=0x%x, tstatus=0x%x]\n",
379 scb->istatus, scb->tstatus);
380 xs->error = XS_DRIVER_STUFFUP;
381 }
382 }
383
384 switch (scb->tstatus) {
385
386 case TGST_GOOD:
387 break;
388
389 case TGST_CC:
390 xs->status = SCSI_CHECK;
391 if (xs->error == 0)
392 xs->error = XS_BUSY;
393
394 default:
395 printf("SC(t): [istatus=0x%x, tstatus=0x%x]\n",
396 scb->istatus, scb->tstatus);
397 break;
398 }
399
400 scsipi_done(xs);
401 free_scb(sc, scb);
402 sc->inuse[periph->periph_target] = 0;
403 sc_sched(sc);
404 }
405
406 int
407 sc_intr(v)
408 void *v;
409 {
410 /* struct sc_softc *sc = v; */
411 volatile u_char *gsp = (u_char *)DMAC_GSTAT;
412 u_int gstat = *gsp;
413 int mrqb, i;
414
415 if ((gstat & CH_INT(CH_SCSI)) == 0)
416 return 0;
417
418 /*
419 * when DMA interrupt occurs there remain some untransferred data.
420 * wait data transfer completion.
421 */
422 mrqb = (gstat & CH_INT(CH_SCSI)) << 1;
423 if (gstat & mrqb) {
424 /*
425 * XXX SHOULD USE DELAY()
426 */
427 for (i = 0; i < 50; i++)
428 ;
429 if (*gsp & mrqb)
430 printf("sc_intr: MRQ\n");
431 }
432 scintr();
433
434 return 1;
435 }
436
437
438 #if 0
439 /*
440 * SCOP_RSENSE request
441 */
442 void
443 scop_rsense(intr, sc_param, lun, ie, count, param)
444 register int intr;
445 register struct scsi *sc_param;
446 register int lun;
447 register int ie;
448 register int count;
449 register caddr_t param;
450 {
451 bzero(sc_param, sizeof(struct scsi));
452 sc_param->identify = MSG_IDENT | sc_disconnect | (lun & IDT_DRMASK);
453 sc_param->sc_lun = lun;
454
455 sc_param->sc_cpoint = (u_char *)param;
456 sc_param->sc_ctrnscnt = count;
457
458 /* sc_cdb */
459 sc_param->sc_opcode = SCOP_RSENSE;
460 sc_param->sc_count = count;
461
462 sc_go(intr, sc_param, ie, sc_param);
463 }
464 #endif
465
466 void
467 cxd1185_timeout(arg)
468 void *arg;
469 {
470 struct sc_scb *scb = arg;
471 struct scsipi_xfer *xs = scb->xs;
472 struct scsipi_periph *periph = xs->xs_periph;
473 int chan;
474
475 chan = periph->periph_target;
476
477 printf("sc: timeout ch=%d\n", chan);
478
479 /* XXX abort transfer and ... */
480 }
481