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