ahb.c revision 1.28.2.1 1 /* $NetBSD: ahb.c,v 1.28.2.1 1999/10/19 17:44:55 thorpej Exp $ */
2
3 #include "opt_ddb.h"
4
5 #undef AHBDEBUG
6 #ifdef DDB
7 #define integrate
8 #else
9 #define integrate static inline
10 #endif
11
12 /*-
13 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
14 * All rights reserved.
15 *
16 * This code is derived from software contributed to The NetBSD Foundation
17 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
18 * Simulation Facility, NASA Ames Research Center.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the NetBSD
31 * Foundation, Inc. and its contributors.
32 * 4. Neither the name of The NetBSD Foundation nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
37 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
38 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
40 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 * POSSIBILITY OF SUCH DAMAGE.
47 */
48
49 /*
50 * Originally written by Julian Elischer (julian (at) tfs.com)
51 * for TRW Financial Systems for use under the MACH(2.5) operating system.
52 *
53 * TRW Financial Systems, in accordance with their agreement with Carnegie
54 * Mellon University, makes this software available to CMU to distribute
55 * or use in any manner that they see fit as long as this message is kept with
56 * the software. For this reason TFS also grants any other persons or
57 * organisations permission to use or modify this software.
58 *
59 * TFS supplies this software to be publicly redistributed
60 * on the understanding that TFS is not responsible for the correct
61 * functioning of this software in any circumstances.
62 */
63
64 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/errno.h>
69 #include <sys/ioctl.h>
70 #include <sys/device.h>
71 #include <sys/malloc.h>
72 #include <sys/buf.h>
73 #include <sys/proc.h>
74 #include <sys/user.h>
75
76 #include <machine/bus.h>
77 #include <machine/intr.h>
78
79 #include <dev/scsipi/scsi_all.h>
80 #include <dev/scsipi/scsipi_all.h>
81 #include <dev/scsipi/scsiconf.h>
82
83 #include <dev/eisa/eisareg.h>
84 #include <dev/eisa/eisavar.h>
85 #include <dev/eisa/eisadevs.h>
86 #include <dev/eisa/ahbreg.h>
87
88 #ifndef DDB
89 #define Debugger() panic("should call debugger here (aha1742.c)")
90 #endif /* ! DDB */
91
92 #define AHB_ECB_MAX 32 /* store up to 32 ECBs at one time */
93 #define ECB_HASH_SIZE 32 /* hash table size for phystokv */
94 #define ECB_HASH_SHIFT 9
95 #define ECB_HASH(x) ((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
96
97 #define AHB_MAXXFER ((AHB_NSEG - 1) << PGSHIFT)
98
99 struct ahb_softc {
100 struct device sc_dev;
101
102 bus_space_tag_t sc_iot;
103 bus_space_handle_t sc_ioh;
104 bus_dma_tag_t sc_dmat;
105 void *sc_ih;
106
107 bus_dmamap_t sc_dmamap_ecb; /* maps the ecbs */
108 struct ahb_ecb *sc_ecbs; /* all our ecbs */
109
110 struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
111 TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
112 struct ahb_ecb *sc_immed_ecb; /* an outstanding immediete command */
113 int sc_numecbs;
114
115 struct scsipi_adapter sc_adapter;
116 struct scsipi_channel sc_channel;
117 };
118
119 /*
120 * Offset of an ECB from the beginning of the ECB DMA mapping.
121 */
122 #define AHB_ECB_OFF(e) (((u_long)(e)) - ((u_long)&sc->sc_ecbs[0]))
123
124 struct ahb_probe_data {
125 int sc_irq;
126 int sc_scsi_dev;
127 };
128
129 void ahb_send_mbox __P((struct ahb_softc *, int, struct ahb_ecb *));
130 void ahb_send_immed __P((struct ahb_softc *, u_long, struct ahb_ecb *));
131 int ahbintr __P((void *));
132 void ahb_free_ecb __P((struct ahb_softc *, struct ahb_ecb *));
133 struct ahb_ecb *ahb_get_ecb __P((struct ahb_softc *, int));
134 struct ahb_ecb *ahb_ecb_phys_kv __P((struct ahb_softc *, physaddr));
135 void ahb_done __P((struct ahb_softc *, struct ahb_ecb *));
136 int ahb_find __P((bus_space_tag_t, bus_space_handle_t, struct ahb_probe_data *));
137 int ahb_init __P((struct ahb_softc *));
138 void ahbminphys __P((struct buf *));
139 void ahb_scsipi_request __P((struct scsipi_channel *,
140 scsipi_adapter_req_t, void *));
141 int ahb_poll __P((struct ahb_softc *, struct scsipi_xfer *, int));
142 void ahb_timeout __P((void *));
143 int ahb_create_ecbs __P((struct ahb_softc *, struct ahb_ecb *, int));
144
145 integrate void ahb_reset_ecb __P((struct ahb_softc *, struct ahb_ecb *));
146 integrate int ahb_init_ecb __P((struct ahb_softc *, struct ahb_ecb *));
147
148 int ahbmatch __P((struct device *, struct cfdata *, void *));
149 void ahbattach __P((struct device *, struct device *, void *));
150
151 struct cfattach ahb_ca = {
152 sizeof(struct ahb_softc), ahbmatch, ahbattach
153 };
154
155 #define AHB_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
156
157 /*
158 * Check the slots looking for a board we recognise
159 * If we find one, note it's address (slot) and call
160 * the actual probe routine to check it out.
161 */
162 int
163 ahbmatch(parent, match, aux)
164 struct device *parent;
165 struct cfdata *match;
166 void *aux;
167 {
168 struct eisa_attach_args *ea = aux;
169 bus_space_tag_t iot = ea->ea_iot;
170 bus_space_handle_t ioh;
171 int rv;
172
173 /* must match one of our known ID strings */
174 if (strcmp(ea->ea_idstring, "ADP0000") &&
175 strcmp(ea->ea_idstring, "ADP0001") &&
176 strcmp(ea->ea_idstring, "ADP0002") &&
177 strcmp(ea->ea_idstring, "ADP0400"))
178 return (0);
179
180 if (bus_space_map(iot,
181 EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET, AHB_EISA_IOSIZE,
182 0, &ioh))
183 return (0);
184
185 rv = !ahb_find(iot, ioh, NULL);
186
187 bus_space_unmap(iot, ioh, AHB_EISA_IOSIZE);
188
189 return (rv);
190 }
191
192 /*
193 * Attach all the sub-devices we can find
194 */
195 void
196 ahbattach(parent, self, aux)
197 struct device *parent, *self;
198 void *aux;
199 {
200 struct eisa_attach_args *ea = aux;
201 struct ahb_softc *sc = (void *)self;
202 bus_space_tag_t iot = ea->ea_iot;
203 bus_space_handle_t ioh;
204 eisa_chipset_tag_t ec = ea->ea_ec;
205 eisa_intr_handle_t ih;
206 const char *model, *intrstr;
207 struct ahb_probe_data apd;
208 struct scsipi_adapter *adapt = &sc->sc_adapter;
209 struct scsipi_channel *chan = &sc->sc_channel;
210
211 if (!strcmp(ea->ea_idstring, "ADP0000"))
212 model = EISA_PRODUCT_ADP0000;
213 else if (!strcmp(ea->ea_idstring, "ADP0001"))
214 model = EISA_PRODUCT_ADP0001;
215 else if (!strcmp(ea->ea_idstring, "ADP0002"))
216 model = EISA_PRODUCT_ADP0002;
217 else if (!strcmp(ea->ea_idstring, "ADP0400"))
218 model = EISA_PRODUCT_ADP0400;
219 else
220 model = "unknown model!";
221 printf(": %s\n", model);
222
223 if (bus_space_map(iot,
224 EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET, AHB_EISA_IOSIZE,
225 0, &ioh))
226 panic("ahbattach: could not map I/O addresses");
227
228 sc->sc_iot = iot;
229 sc->sc_ioh = ioh;
230 sc->sc_dmat = ea->ea_dmat;
231 if (ahb_find(iot, ioh, &apd))
232 panic("ahbattach: ahb_find failed!");
233
234 TAILQ_INIT(&sc->sc_free_ecb);
235
236 /*
237 * Fill in the scsipi_adapter.
238 */
239 memset(adapt, 0, sizeof(*adapt));
240 adapt->adapt_dev = &sc->sc_dev;
241 adapt->adapt_nchannels = 1;
242 /* adapt_openings initialized below */
243 adapt->adapt_max_periph = 4; /* XXX arbitrary? */
244 adapt->adapt_request = ahb_scsipi_request;
245 adapt->adapt_minphys = ahbminphys;
246
247 /*
248 * Fill in the scsipi_channel.
249 */
250 memset(chan, 0, sizeof(*chan));
251 chan->chan_adapter = adapt;
252 chan->chan_bustype = &scsi_bustype;
253 chan->chan_channel = 0;
254 chan->chan_ntargets = 8;
255 chan->chan_nluns = 8;
256 chan->chan_id = apd.sc_scsi_dev;
257
258 if (ahb_init(sc) != 0) {
259 /* Error during initialization! */
260 return;
261 }
262
263 if (eisa_intr_map(ec, apd.sc_irq, &ih)) {
264 printf("%s: couldn't map interrupt (%d)\n",
265 sc->sc_dev.dv_xname, apd.sc_irq);
266 return;
267 }
268 intrstr = eisa_intr_string(ec, ih);
269 sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
270 ahbintr, sc);
271 if (sc->sc_ih == NULL) {
272 printf("%s: couldn't establish interrupt",
273 sc->sc_dev.dv_xname);
274 if (intrstr != NULL)
275 printf(" at %s", intrstr);
276 printf("\n");
277 return;
278 }
279 if (intrstr != NULL)
280 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
281 intrstr);
282
283 /*
284 * ask the adapter what subunits are present
285 */
286 config_found(self, &sc->sc_channel, scsiprint);
287 }
288
289 /*
290 * Function to send a command out through a mailbox
291 */
292 void
293 ahb_send_mbox(sc, opcode, ecb)
294 struct ahb_softc *sc;
295 int opcode;
296 struct ahb_ecb *ecb;
297 {
298 bus_space_tag_t iot = sc->sc_iot;
299 bus_space_handle_t ioh = sc->sc_ioh;
300 int wait = 300; /* 1ms should be enough */
301
302 while (--wait) {
303 if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
304 == (G2STAT_MBOX_EMPTY))
305 break;
306 delay(10);
307 }
308 if (!wait) {
309 printf("%s: board not responding\n", sc->sc_dev.dv_xname);
310 Debugger();
311 }
312
313 /*
314 * don't know if this will work.
315 * XXX WHAT DOES THIS COMMENT MEAN?! --thorpej
316 */
317 bus_space_write_4(iot, ioh, MBOXOUT0,
318 sc->sc_dmamap_ecb->dm_segs[0].ds_addr + AHB_ECB_OFF(ecb));
319 bus_space_write_1(iot, ioh, ATTN, opcode |
320 ecb->xs->xs_periph->periph_target);
321
322 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
323 timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
324 }
325
326 /*
327 * Function to send an immediate type command to the adapter
328 */
329 void
330 ahb_send_immed(sc, cmd, ecb)
331 struct ahb_softc *sc;
332 u_long cmd;
333 struct ahb_ecb *ecb;
334 {
335 bus_space_tag_t iot = sc->sc_iot;
336 bus_space_handle_t ioh = sc->sc_ioh;
337 int wait = 100; /* 1 ms enough? */
338
339 while (--wait) {
340 if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
341 == (G2STAT_MBOX_EMPTY))
342 break;
343 delay(10);
344 }
345 if (!wait) {
346 printf("%s: board not responding\n", sc->sc_dev.dv_xname);
347 Debugger();
348 }
349
350 bus_space_write_4(iot, ioh, MBOXOUT0, cmd); /* don't know this will work */
351 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
352 bus_space_write_1(iot, ioh, ATTN, OP_IMMED |
353 ecb->xs->xs_periph->periph_target);
354
355 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
356 timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
357 }
358
359 /*
360 * Catch an interrupt from the adaptor
361 */
362 int
363 ahbintr(arg)
364 void *arg;
365 {
366 struct ahb_softc *sc = arg;
367 bus_space_tag_t iot = sc->sc_iot;
368 bus_space_handle_t ioh = sc->sc_ioh;
369 struct ahb_ecb *ecb;
370 u_char ahbstat;
371 u_long mboxval;
372
373 #ifdef AHBDEBUG
374 printf("%s: ahbintr ", sc->sc_dev.dv_xname);
375 #endif /* AHBDEBUG */
376
377 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
378 return 0;
379
380 for (;;) {
381 /*
382 * First get all the information and then
383 * acknowlege the interrupt
384 */
385 ahbstat = bus_space_read_1(iot, ioh, G2INTST);
386 mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
387 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
388
389 #ifdef AHBDEBUG
390 printf("status = 0x%x ", ahbstat);
391 #endif /* AHBDEBUG */
392
393 /*
394 * Process the completed operation
395 */
396 switch (ahbstat & G2INTST_INT_STAT) {
397 case AHB_ECB_OK:
398 case AHB_ECB_RECOVERED:
399 case AHB_ECB_ERR:
400 ecb = ahb_ecb_phys_kv(sc, mboxval);
401 if (!ecb) {
402 printf("%s: BAD ECB RETURNED!\n",
403 sc->sc_dev.dv_xname);
404 goto next; /* whatever it was, it'll timeout */
405 }
406 break;
407
408 case AHB_IMMED_ERR:
409 ecb = sc->sc_immed_ecb;
410 sc->sc_immed_ecb = 0;
411 ecb->flags |= ECB_IMMED_FAIL;
412 break;
413
414 case AHB_IMMED_OK:
415 ecb = sc->sc_immed_ecb;
416 sc->sc_immed_ecb = 0;
417 break;
418
419 default:
420 printf("%s: unexpected interrupt %x\n",
421 sc->sc_dev.dv_xname, ahbstat);
422 goto next;
423 }
424
425 untimeout(ahb_timeout, ecb);
426 ahb_done(sc, ecb);
427
428 next:
429 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
430 return 1;
431 }
432 }
433
434 integrate void
435 ahb_reset_ecb(sc, ecb)
436 struct ahb_softc *sc;
437 struct ahb_ecb *ecb;
438 {
439
440 ecb->flags = 0;
441 }
442
443 /*
444 * A ecb (and hence a mbx-out is put onto the
445 * free list.
446 */
447 void
448 ahb_free_ecb(sc, ecb)
449 struct ahb_softc *sc;
450 struct ahb_ecb *ecb;
451 {
452 int s;
453
454 s = splbio();
455
456 ahb_reset_ecb(sc, ecb);
457 TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
458
459 /*
460 * If there were none, wake anybody waiting for one to come free,
461 * starting with queued entries.
462 */
463 if (ecb->chain.tqe_next == 0)
464 wakeup(&sc->sc_free_ecb);
465
466 splx(s);
467 }
468
469 /*
470 * Create a set of ecbs and add them to the free list.
471 */
472 integrate int
473 ahb_init_ecb(sc, ecb)
474 struct ahb_softc *sc;
475 struct ahb_ecb *ecb;
476 {
477 bus_dma_tag_t dmat = sc->sc_dmat;
478 int hashnum, error;
479
480 /*
481 * Create the DMA map for this ECB.
482 */
483 error = bus_dmamap_create(dmat, AHB_MAXXFER, AHB_NSEG, AHB_MAXXFER,
484 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &ecb->dmamap_xfer);
485 if (error) {
486 printf("%s: can't create ecb dmamap_xfer\n",
487 sc->sc_dev.dv_xname);
488 return (error);
489 }
490
491 /*
492 * put in the phystokv hash table
493 * Never gets taken out.
494 */
495 ecb->hashkey = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
496 AHB_ECB_OFF(ecb);
497 hashnum = ECB_HASH(ecb->hashkey);
498 ecb->nexthash = sc->sc_ecbhash[hashnum];
499 sc->sc_ecbhash[hashnum] = ecb;
500 ahb_reset_ecb(sc, ecb);
501 return (0);
502 }
503
504 int
505 ahb_create_ecbs(sc, ecbstore, count)
506 struct ahb_softc *sc;
507 struct ahb_ecb *ecbstore;
508 int count;
509 {
510 struct ahb_ecb *ecb;
511 int i, error;
512
513 bzero(ecbstore, sizeof(struct ahb_ecb) * count);
514 for (i = 0; i < count; i++) {
515 ecb = &ecbstore[i];
516 if ((error = ahb_init_ecb(sc, ecb)) != 0) {
517 printf("%s: unable to initialize ecb, error = %d\n",
518 sc->sc_dev.dv_xname, error);
519 goto out;
520 }
521 TAILQ_INSERT_TAIL(&sc->sc_free_ecb, ecb, chain);
522 }
523 out:
524 return (i);
525 }
526
527 /*
528 * Get a free ecb
529 *
530 * If there are none, see if we can allocate a new one. If so, put it in the
531 * hash table too otherwise either return an error or sleep.
532 */
533 struct ahb_ecb *
534 ahb_get_ecb(sc, flags)
535 struct ahb_softc *sc;
536 int flags;
537 {
538 struct ahb_ecb *ecb;
539 int s;
540
541 s = splbio();
542
543 /*
544 * If we can and have to, sleep waiting for one to come free
545 * but only if we can't allocate a new one.
546 */
547 for (;;) {
548 ecb = sc->sc_free_ecb.tqh_first;
549 if (ecb) {
550 TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
551 break;
552 }
553 if ((flags & XS_CTL_NOSLEEP) != 0)
554 goto out;
555 tsleep(&sc->sc_free_ecb, PRIBIO, "ahbecb", 0);
556 }
557
558 ecb->flags |= ECB_ALLOC;
559
560 out:
561 splx(s);
562 return ecb;
563 }
564
565 /*
566 * given a physical address, find the ecb that it corresponds to.
567 */
568 struct ahb_ecb *
569 ahb_ecb_phys_kv(sc, ecb_phys)
570 struct ahb_softc *sc;
571 physaddr ecb_phys;
572 {
573 int hashnum = ECB_HASH(ecb_phys);
574 struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
575
576 while (ecb) {
577 if (ecb->hashkey == ecb_phys)
578 break;
579 ecb = ecb->nexthash;
580 }
581 return ecb;
582 }
583
584 /*
585 * We have a ecb which has been processed by the adaptor, now we look to see
586 * how the operation went.
587 */
588 void
589 ahb_done(sc, ecb)
590 struct ahb_softc *sc;
591 struct ahb_ecb *ecb;
592 {
593 bus_dma_tag_t dmat = sc->sc_dmat;
594 struct scsipi_sense_data *s1, *s2;
595 struct scsipi_xfer *xs = ecb->xs;
596
597 SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahb_done\n"));
598
599 bus_dmamap_sync(dmat, sc->sc_dmamap_ecb,
600 AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
601 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
602
603 /*
604 * If we were a data transfer, unload the map that described
605 * the data buffer.
606 */
607 if (xs->datalen) {
608 bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
609 ecb->dmamap_xfer->dm_mapsize,
610 (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
611 BUS_DMASYNC_POSTWRITE);
612 bus_dmamap_unload(dmat, ecb->dmamap_xfer);
613 }
614
615 /*
616 * Otherwise, put the results of the operation
617 * into the xfer and call whoever started it
618 */
619 if ((ecb->flags & ECB_ALLOC) == 0) {
620 printf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
621 Debugger();
622 }
623 if (ecb->flags & ECB_IMMED) {
624 if (ecb->flags & ECB_IMMED_FAIL)
625 xs->error = XS_DRIVER_STUFFUP;
626 goto done;
627 }
628 if (xs->error == XS_NOERROR) {
629 if (ecb->ecb_status.host_stat != HS_OK) {
630 switch (ecb->ecb_status.host_stat) {
631 case HS_TIMED_OUT: /* No response */
632 xs->error = XS_SELTIMEOUT;
633 break;
634 default: /* Other scsi protocol messes */
635 printf("%s: host_stat %x\n",
636 sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
637 xs->error = XS_DRIVER_STUFFUP;
638 }
639 } else if (ecb->ecb_status.target_stat != SCSI_OK) {
640 switch (ecb->ecb_status.target_stat) {
641 case SCSI_CHECK:
642 s1 = &ecb->ecb_sense;
643 s2 = &xs->sense.scsi_sense;
644 *s2 = *s1;
645 xs->error = XS_SENSE;
646 break;
647 case SCSI_BUSY:
648 xs->error = XS_BUSY;
649 break;
650 default:
651 printf("%s: target_stat %x\n",
652 sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
653 xs->error = XS_DRIVER_STUFFUP;
654 }
655 } else
656 xs->resid = 0;
657 }
658 done:
659 ahb_free_ecb(sc, ecb);
660 scsipi_done(xs);
661 }
662
663 /*
664 * Start the board, ready for normal operation
665 */
666 int
667 ahb_find(iot, ioh, sc)
668 bus_space_tag_t iot;
669 bus_space_handle_t ioh;
670 struct ahb_probe_data *sc;
671 {
672 u_char intdef;
673 int i, irq, busid;
674 int wait = 1000; /* 1 sec enough? */
675
676 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
677
678 #define NO_NO 1
679 #ifdef NO_NO
680 /*
681 * reset board, If it doesn't respond, assume
682 * that it's not there.. good for the probe
683 */
684 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
685 delay(1000);
686 bus_space_write_1(iot, ioh, G2CNTRL, 0);
687 delay(10000);
688 while (--wait) {
689 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
690 break;
691 delay(1000);
692 }
693 if (!wait) {
694 #ifdef AHBDEBUG
695 printf("ahb_find: No answer from aha1742 board\n");
696 #endif /* AHBDEBUG */
697 return ENXIO;
698 }
699 i = bus_space_read_1(iot, ioh, MBOXIN0);
700 if (i) {
701 printf("self test failed, val = 0x%x\n", i);
702 return EIO;
703 }
704
705 /* Set it again, just to be sure. */
706 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
707 #endif
708
709 while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
710 printf(".");
711 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
712 delay(10000);
713 }
714
715 intdef = bus_space_read_1(iot, ioh, INTDEF);
716 switch (intdef & 0x07) {
717 case INT9:
718 irq = 9;
719 break;
720 case INT10:
721 irq = 10;
722 break;
723 case INT11:
724 irq = 11;
725 break;
726 case INT12:
727 irq = 12;
728 break;
729 case INT14:
730 irq = 14;
731 break;
732 case INT15:
733 irq = 15;
734 break;
735 default:
736 printf("illegal int setting %x\n", intdef);
737 return EIO;
738 }
739
740 bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN)); /* make sure we can interrupt */
741
742 /* who are we on the scsi bus? */
743 busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
744
745 /* if we want to return data, do so now */
746 if (sc) {
747 sc->sc_irq = irq;
748 sc->sc_scsi_dev = busid;
749 }
750
751 /*
752 * Note that we are going and return (to probe)
753 */
754 return 0;
755 }
756
757 int
758 ahb_init(sc)
759 struct ahb_softc *sc;
760 {
761 bus_dma_segment_t seg;
762 int i, error, rseg;
763
764 #define ECBSIZE (AHB_ECB_MAX * sizeof(struct ahb_ecb))
765
766 /*
767 * Allocate the ECBs.
768 */
769 if ((error = bus_dmamem_alloc(sc->sc_dmat, ECBSIZE,
770 NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
771 printf("%s: unable to allocate ecbs, error = %d\n",
772 sc->sc_dev.dv_xname, error);
773 return (error);
774 }
775 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
776 ECBSIZE, (caddr_t *)&sc->sc_ecbs,
777 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
778 printf("%s: unable to map ecbs, error = %d\n",
779 sc->sc_dev.dv_xname, error);
780 return (error);
781 }
782
783 /*
784 * Create and load the DMA map used for the ecbs.
785 */
786 if ((error = bus_dmamap_create(sc->sc_dmat, ECBSIZE,
787 1, ECBSIZE, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_ecb)) != 0) {
788 printf("%s: unable to create ecb DMA map, error = %d\n",
789 sc->sc_dev.dv_xname, error);
790 return (error);
791 }
792 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ecb,
793 sc->sc_ecbs, ECBSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
794 printf("%s: unable to load ecb DMA map, error = %d\n",
795 sc->sc_dev.dv_xname, error);
796 return (error);
797 }
798
799 #undef ECBSIZE
800
801 /*
802 * Initialize the ecbs.
803 */
804 i = ahb_create_ecbs(sc, sc->sc_ecbs, AHB_ECB_MAX);
805 if (i == 0) {
806 printf("%s: unable to create ecbs\n",
807 sc->sc_dev.dv_xname);
808 return (ENOMEM);
809 } else if (i != AHB_ECB_MAX) {
810 printf("%s: WARNING: only %d of %d ecbs created\n",
811 sc->sc_dev.dv_xname, i, AHB_ECB_MAX);
812 }
813
814 sc->sc_adapter.adapt_openings = i;
815
816 return (0);
817 }
818
819 void
820 ahbminphys(bp)
821 struct buf *bp;
822 {
823
824 if (bp->b_bcount > AHB_MAXXFER)
825 bp->b_bcount = AHB_MAXXFER;
826 minphys(bp);
827 }
828
829 /*
830 * start a scsi operation given the command and the data address. Also needs
831 * the unit, target and lu.
832 */
833 void
834 ahb_scsipi_request(chan, req, arg)
835 struct scsipi_channel *chan;
836 scsipi_adapter_req_t req;
837 void *arg;
838 {
839 struct scsipi_xfer *xs;
840 struct scsipi_periph *periph;
841 struct ahb_softc *sc = (void *)chan->chan_adapter->adapt_dev;
842 bus_dma_tag_t dmat = sc->sc_dmat;
843 struct ahb_ecb *ecb;
844 int error, seg, flags, s;
845
846 switch (req) {
847 case ADAPTER_REQ_RUN_XFER:
848 xs = arg;
849 periph = xs->xs_periph;
850 flags = xs->xs_control;
851
852 SC_DEBUG(sc_link, SDEV_DB2, ("ahb_scsipi_request\n"));
853
854 /* Get an ECB to use. */
855 ecb = ahb_get_ecb(sc, flags);
856 #ifdef DIAGNOSTIC
857 /*
858 * This should never happen as we track the resources
859 * in the mid-layer.
860 */
861 if (ecb == NULL) {
862 scsipi_printaddr(periph);
863 printf("unable to allocate ecb\n");
864 panic("ahb_scsipi_request");
865 }
866 #endif
867
868 ecb->xs = xs;
869 ecb->timeout = xs->timeout;
870
871 /*
872 * If it's a reset, we need to do an 'immediate'
873 * command, and store its ecb for later
874 * if there is already an immediate waiting,
875 * then WE must wait
876 */
877 if (flags & XS_CTL_RESET) {
878 ecb->flags |= ECB_IMMED;
879 if (sc->sc_immed_ecb) {
880 ahb_free_ecb(sc, ecb);
881 xs->error = XS_BUSY;
882 scsipi_done(xs);
883 return;
884 }
885 sc->sc_immed_ecb = ecb;
886
887 s = splbio();
888 ahb_send_immed(sc, AHB_TARG_RESET, ecb);
889 splx(s);
890
891 if ((flags & XS_CTL_POLL) == 0)
892 return;
893
894 /*
895 * If we can't use interrupts, poll on completion
896 */
897 if (ahb_poll(sc, xs, ecb->timeout))
898 ahb_timeout(ecb);
899 return;
900 }
901
902 /*
903 * Put all the arguments for the xfer in the ecb
904 */
905 ecb->opcode = ECB_SCSI_OP;
906 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
907 ecb->opt2 = periph->periph_lun | ECB_NRB;
908 bcopy(xs->cmd, &ecb->scsi_cmd,
909 ecb->scsi_cmd_length = xs->cmdlen);
910 ecb->sense_ptr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
911 AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_sense);
912 ecb->req_sense_length = sizeof(ecb->ecb_sense);
913 ecb->status = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
914 AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_status);
915 ecb->ecb_status.host_stat = 0x00;
916 ecb->ecb_status.target_stat = 0x00;
917
918 if (xs->datalen) {
919 /*
920 * Map the DMA transfer.
921 */
922 #ifdef TFS
923 if (flags & XS_CTL_DATA_UIO) {
924 error = bus_dmamap_load_uio(sc->sc_dmat,
925 ecb->dmamap_xfer, (struct uio *)xs->data,
926 (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
927 BUS_DMA_WAITOK);
928 } else
929 #endif /* TFS */
930 {
931 error = bus_dmamap_load(sc->sc_dmat,
932 ecb->dmamap_xfer, xs->data, xs->datalen,
933 NULL, (flags & XS_CTL_NOSLEEP) ?
934 BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
935 }
936
937 if (error) {
938 if (error == EFBIG) {
939 printf("%s: ahb_scsipi_request, more "
940 "than %d dma segments\n",
941 sc->sc_dev.dv_xname, AHB_NSEG);
942 } else {
943 printf("%s: ahb_scsipi_request, error "
944 "%d loading dma map\n",
945 sc->sc_dev.dv_xname, error);
946 }
947 ahb_free_ecb(sc, ecb);
948 xs->error = XS_DRIVER_STUFFUP;
949 scsipi_done(xs);
950 return;
951 }
952
953 bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
954 ecb->dmamap_xfer->dm_mapsize,
955 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
956 BUS_DMASYNC_PREWRITE);
957
958 /*
959 * Load the hardware scatter/gather map with the
960 * contents of the DMA map.
961 */
962 for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
963 ecb->ahb_dma[seg].seg_addr =
964 ecb->dmamap_xfer->dm_segs[seg].ds_addr;
965 ecb->ahb_dma[seg].seg_len =
966 ecb->dmamap_xfer->dm_segs[seg].ds_len;
967 }
968
969 ecb->data_addr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
970 AHB_ECB_OFF(ecb) +
971 offsetof(struct ahb_ecb, ahb_dma);
972 ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
973 sizeof(struct ahb_dma_seg);
974 ecb->opt1 |= ECB_S_G;
975 } else { /* No data xfer, use non S/G values */
976 ecb->data_addr = (physaddr)0;
977 ecb->data_length = 0;
978 }
979 ecb->link_addr = (physaddr)0;
980
981 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ecb,
982 AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
983 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
984
985 s = splbio();
986 ahb_send_mbox(sc, OP_START_ECB, ecb);
987 splx(s);
988
989 if ((flags & XS_CTL_POLL) == 0)
990 return;
991
992 /*
993 * If we can't use interrupts, poll on completion
994 */
995 if (ahb_poll(sc, xs, ecb->timeout)) {
996 ahb_timeout(ecb);
997 if (ahb_poll(sc, xs, ecb->timeout))
998 ahb_timeout(ecb);
999 }
1000 return;
1001
1002 case ADAPTER_REQ_GROW_RESOURCES:
1003 /* XXX Not supported. */
1004 return;
1005
1006 case ADAPTER_REQ_SET_XFER_MODE:
1007 /* XXX How do we do this? */
1008 return;
1009
1010 case ADAPTER_REQ_GET_XFER_MODE:
1011 /* XXX How do we do this? */
1012 return;
1013 }
1014 }
1015
1016 /*
1017 * Function to poll for command completion when in poll mode
1018 */
1019 int
1020 ahb_poll(sc, xs, count)
1021 struct ahb_softc *sc;
1022 struct scsipi_xfer *xs;
1023 int count;
1024 { /* in msec */
1025 bus_space_tag_t iot = sc->sc_iot;
1026 bus_space_handle_t ioh = sc->sc_ioh;
1027
1028 while (count) {
1029 /*
1030 * If we had interrupts enabled, would we
1031 * have got an interrupt?
1032 */
1033 if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
1034 ahbintr(sc);
1035 if (xs->xs_status & XS_STS_DONE)
1036 return 0;
1037 delay(1000);
1038 count--;
1039 }
1040 return 1;
1041 }
1042
1043 void
1044 ahb_timeout(arg)
1045 void *arg;
1046 {
1047 struct ahb_ecb *ecb = arg;
1048 struct scsipi_xfer *xs = ecb->xs;
1049 struct scsipi_periph *periph = xs->xs_periph;
1050 struct ahb_softc *sc =
1051 (void *)periph->periph_channel->chan_adapter->adapt_dev;
1052 int s;
1053
1054 scsipi_printaddr(periph);
1055 printf("timed out");
1056
1057 s = splbio();
1058
1059 if (ecb->flags & ECB_IMMED) {
1060 printf("\n");
1061 ecb->flags |= ECB_IMMED_FAIL;
1062 /* XXX Must reset! */
1063 } else
1064
1065 /*
1066 * If it has been through before, then
1067 * a previous abort has failed, don't
1068 * try abort again
1069 */
1070 if (ecb->flags & ECB_ABORT) {
1071 /* abort timed out */
1072 printf(" AGAIN\n");
1073 /* XXX Must reset! */
1074 } else {
1075 /* abort the operation that has timed out */
1076 printf("\n");
1077 ecb->xs->error = XS_TIMEOUT;
1078 ecb->timeout = AHB_ABORT_TIMEOUT;
1079 ecb->flags |= ECB_ABORT;
1080 ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
1081 }
1082
1083 splx(s);
1084 }
1085