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