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