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