ahb.c revision 1.16 1 /* $NetBSD: ahb.c,v 1.16 1998/02/04 00:35:57 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 <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,
735 (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
736 BUS_DMASYNC_POSTWRITE);
737 bus_dmamap_unload(dmat, ecb->dmamap_xfer);
738 }
739
740 /*
741 * Otherwise, put the results of the operation
742 * into the xfer and call whoever started it
743 */
744 if ((ecb->flags & ECB_ALLOC) == 0) {
745 printf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
746 Debugger();
747 }
748 if (ecb->flags & ECB_IMMED) {
749 if (ecb->flags & ECB_IMMED_FAIL)
750 xs->error = XS_DRIVER_STUFFUP;
751 goto done;
752 }
753 if (xs->error == XS_NOERROR) {
754 if (ecb->ecb_status.host_stat != HS_OK) {
755 switch (ecb->ecb_status.host_stat) {
756 case HS_TIMED_OUT: /* No response */
757 xs->error = XS_SELTIMEOUT;
758 break;
759 default: /* Other scsi protocol messes */
760 printf("%s: host_stat %x\n",
761 sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
762 xs->error = XS_DRIVER_STUFFUP;
763 }
764 } else if (ecb->ecb_status.target_stat != SCSI_OK) {
765 switch (ecb->ecb_status.target_stat) {
766 case SCSI_CHECK:
767 s1 = &ecb->ecb_sense;
768 s2 = &xs->sense.scsi_sense;
769 *s2 = *s1;
770 xs->error = XS_SENSE;
771 break;
772 case SCSI_BUSY:
773 xs->error = XS_BUSY;
774 break;
775 default:
776 printf("%s: target_stat %x\n",
777 sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
778 xs->error = XS_DRIVER_STUFFUP;
779 }
780 } else
781 xs->resid = 0;
782 }
783 done:
784 ahb_free_ecb(sc, ecb);
785 xs->flags |= ITSDONE;
786 scsipi_done(xs);
787
788 /*
789 * If there are queue entries in the software queue, try to
790 * run the first one. We should be more or less guaranteed
791 * to succeed, since we just freed an ECB.
792 *
793 * NOTE: ahb_scsi_cmd() relies on our calling it with
794 * the first entry in the queue.
795 */
796 if ((xs = sc->sc_queue.lh_first) != NULL)
797 (void) ahb_scsi_cmd(xs);
798 }
799
800 /*
801 * Start the board, ready for normal operation
802 */
803 int
804 ahb_find(iot, ioh, sc)
805 bus_space_tag_t iot;
806 bus_space_handle_t ioh;
807 struct ahb_probe_data *sc;
808 {
809 u_char intdef;
810 int i, irq, busid;
811 int wait = 1000; /* 1 sec enough? */
812
813 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
814
815 #define NO_NO 1
816 #ifdef NO_NO
817 /*
818 * reset board, If it doesn't respond, assume
819 * that it's not there.. good for the probe
820 */
821 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
822 delay(1000);
823 bus_space_write_1(iot, ioh, G2CNTRL, 0);
824 delay(10000);
825 while (--wait) {
826 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
827 break;
828 delay(1000);
829 }
830 if (!wait) {
831 #ifdef AHBDEBUG
832 printf("ahb_find: No answer from aha1742 board\n");
833 #endif /* AHBDEBUG */
834 return ENXIO;
835 }
836 i = bus_space_read_1(iot, ioh, MBOXIN0);
837 if (i) {
838 printf("self test failed, val = 0x%x\n", i);
839 return EIO;
840 }
841
842 /* Set it again, just to be sure. */
843 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
844 #endif
845
846 while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
847 printf(".");
848 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
849 delay(10000);
850 }
851
852 intdef = bus_space_read_1(iot, ioh, INTDEF);
853 switch (intdef & 0x07) {
854 case INT9:
855 irq = 9;
856 break;
857 case INT10:
858 irq = 10;
859 break;
860 case INT11:
861 irq = 11;
862 break;
863 case INT12:
864 irq = 12;
865 break;
866 case INT14:
867 irq = 14;
868 break;
869 case INT15:
870 irq = 15;
871 break;
872 default:
873 printf("illegal int setting %x\n", intdef);
874 return EIO;
875 }
876
877 bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN)); /* make sure we can interrupt */
878
879 /* who are we on the scsi bus? */
880 busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
881
882 /* if we want to return data, do so now */
883 if (sc) {
884 sc->sc_irq = irq;
885 sc->sc_scsi_dev = busid;
886 }
887
888 /*
889 * Note that we are going and return (to probe)
890 */
891 return 0;
892 }
893
894 void
895 ahb_init(sc)
896 struct ahb_softc *sc;
897 {
898
899 }
900
901 void
902 ahbminphys(bp)
903 struct buf *bp;
904 {
905
906 if (bp->b_bcount > AHB_MAXXFER)
907 bp->b_bcount = AHB_MAXXFER;
908 minphys(bp);
909 }
910
911 /*
912 * start a scsi operation given the command and the data address. Also needs
913 * the unit, target and lu.
914 */
915 int
916 ahb_scsi_cmd(xs)
917 struct scsipi_xfer *xs;
918 {
919 struct scsipi_link *sc_link = xs->sc_link;
920 struct ahb_softc *sc = sc_link->adapter_softc;
921 bus_dma_tag_t dmat = sc->sc_dmat;
922 struct ahb_ecb *ecb;
923 int error, seg, flags, s;
924 int fromqueue = 0, dontqueue = 0;
925
926 SC_DEBUG(sc_link, SDEV_DB2, ("ahb_scsi_cmd\n"));
927
928 s = splbio(); /* protect the queue */
929
930 /*
931 * If we're running the queue from ahb_done(), we've been
932 * called with the first queue entry as our argument.
933 */
934 if (xs == sc->sc_queue.lh_first) {
935 xs = ahb_dequeue(sc);
936 fromqueue = 1;
937 goto get_ecb;
938 }
939
940 /* Polled requests can't be queued for later. */
941 dontqueue = xs->flags & SCSI_POLL;
942
943 /*
944 * If there are jobs in the queue, run them first.
945 */
946 if (sc->sc_queue.lh_first != NULL) {
947 /*
948 * If we can't queue, we have to abort, since
949 * we have to preserve order.
950 */
951 if (dontqueue) {
952 splx(s);
953 xs->error = XS_DRIVER_STUFFUP;
954 return (TRY_AGAIN_LATER);
955 }
956
957 /*
958 * Swap with the first queue entry.
959 */
960 ahb_enqueue(sc, xs, 0);
961 xs = ahb_dequeue(sc);
962 fromqueue = 1;
963 }
964
965 get_ecb:
966 /*
967 * get a ecb (mbox-out) to use. If the transfer
968 * is from a buf (possibly from interrupt time)
969 * then we can't allow it to sleep
970 */
971 flags = xs->flags;
972 if ((ecb = ahb_get_ecb(sc, flags)) == NULL) {
973 /*
974 * If we can't queue, we lose.
975 */
976 if (dontqueue) {
977 splx(s);
978 xs->error = XS_DRIVER_STUFFUP;
979 return (TRY_AGAIN_LATER);
980 }
981
982 /*
983 * Stuff ourselves into the queue, in front
984 * if we came off in the first place.
985 */
986 ahb_enqueue(sc, xs, fromqueue);
987 splx(s);
988 return (SUCCESSFULLY_QUEUED);
989 }
990
991 splx(s); /* done playing with the queue */
992
993 ecb->xs = xs;
994 ecb->timeout = xs->timeout;
995
996 /*
997 * If it's a reset, we need to do an 'immediate'
998 * command, and store its ecb for later
999 * if there is already an immediate waiting,
1000 * then WE must wait
1001 */
1002 if (flags & SCSI_RESET) {
1003 ecb->flags |= ECB_IMMED;
1004 if (sc->sc_immed_ecb)
1005 return TRY_AGAIN_LATER;
1006 sc->sc_immed_ecb = ecb;
1007
1008 s = splbio();
1009 ahb_send_immed(sc, AHB_TARG_RESET, ecb);
1010 splx(s);
1011
1012 if ((flags & SCSI_POLL) == 0)
1013 return SUCCESSFULLY_QUEUED;
1014
1015 /*
1016 * If we can't use interrupts, poll on completion
1017 */
1018 if (ahb_poll(sc, xs, ecb->timeout))
1019 ahb_timeout(ecb);
1020 return COMPLETE;
1021 }
1022
1023 /*
1024 * Put all the arguments for the xfer in the ecb
1025 */
1026 ecb->opcode = ECB_SCSI_OP;
1027 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
1028 ecb->opt2 = sc_link->scsipi_scsi.lun | ECB_NRB;
1029 bcopy(xs->cmd, &ecb->scsi_cmd, ecb->scsi_cmd_length = xs->cmdlen);
1030 ecb->sense_ptr = ecb->dmamap_self->dm_segs[0].ds_addr +
1031 offsetof(struct ahb_ecb, ecb_sense);
1032 ecb->req_sense_length = sizeof(ecb->ecb_sense);
1033 ecb->status = ecb->dmamap_self->dm_segs[0].ds_addr +
1034 offsetof(struct ahb_ecb, ecb_status);
1035 ecb->ecb_status.host_stat = 0x00;
1036 ecb->ecb_status.target_stat = 0x00;
1037
1038 if (xs->datalen) {
1039 /*
1040 * Map the DMA transfer.
1041 */
1042 #ifdef TFS
1043 if (flags & SCSI_DATA_UIO) {
1044 error = bus_dmamap_load_uio(sc->sc_dmat,
1045 ecb->dmamap_xfer, (struct uio *)xs->data,
1046 (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1047 BUS_DMA_WAITOK);
1048 } else
1049 #endif /* TFS */
1050 {
1051 error = bus_dmamap_load(sc->sc_dmat,
1052 ecb->dmamap_xfer, xs->data, xs->datalen, NULL,
1053 (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1054 BUS_DMA_WAITOK);
1055 }
1056
1057 if (error) {
1058 if (error == EFBIG) {
1059 printf("%s: ahb_scsi_cmd, more than %d"
1060 " dma segments\n",
1061 sc->sc_dev.dv_xname, AHB_NSEG);
1062 } else {
1063 printf("%s: ahb_scsi_cmd, error %d loading"
1064 " dma map\n",
1065 sc->sc_dev.dv_xname, error);
1066 }
1067 goto bad;
1068 }
1069
1070 bus_dmamap_sync(dmat, ecb->dmamap_xfer,
1071 (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
1072 BUS_DMASYNC_PREWRITE);
1073
1074 /*
1075 * Load the hardware scatter/gather map with the
1076 * contents of the DMA map.
1077 */
1078 for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
1079 ecb->ahb_dma[seg].seg_addr =
1080 ecb->dmamap_xfer->dm_segs[seg].ds_addr;
1081 ecb->ahb_dma[seg].seg_len =
1082 ecb->dmamap_xfer->dm_segs[seg].ds_len;
1083 }
1084
1085 ecb->data_addr = ecb->dmamap_self->dm_segs[0].ds_addr +
1086 offsetof(struct ahb_ecb, ahb_dma);
1087 ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
1088 sizeof(struct ahb_dma_seg);
1089 ecb->opt1 |= ECB_S_G;
1090 } else { /* No data xfer, use non S/G values */
1091 ecb->data_addr = (physaddr)0;
1092 ecb->data_length = 0;
1093 }
1094 ecb->link_addr = (physaddr)0;
1095
1096 s = splbio();
1097 ahb_send_mbox(sc, OP_START_ECB, ecb);
1098 splx(s);
1099
1100 /*
1101 * Usually return SUCCESSFULLY QUEUED
1102 */
1103 if ((flags & SCSI_POLL) == 0)
1104 return SUCCESSFULLY_QUEUED;
1105
1106 /*
1107 * If we can't use interrupts, poll on completion
1108 */
1109 if (ahb_poll(sc, xs, ecb->timeout)) {
1110 ahb_timeout(ecb);
1111 if (ahb_poll(sc, xs, ecb->timeout))
1112 ahb_timeout(ecb);
1113 }
1114 return COMPLETE;
1115
1116 bad:
1117 xs->error = XS_DRIVER_STUFFUP;
1118 ahb_free_ecb(sc, ecb);
1119 return COMPLETE;
1120 }
1121
1122 /*
1123 * Function to poll for command completion when in poll mode
1124 */
1125 int
1126 ahb_poll(sc, xs, count)
1127 struct ahb_softc *sc;
1128 struct scsipi_xfer *xs;
1129 int count;
1130 { /* in msec */
1131 bus_space_tag_t iot = sc->sc_iot;
1132 bus_space_handle_t ioh = sc->sc_ioh;
1133
1134 while (count) {
1135 /*
1136 * If we had interrupts enabled, would we
1137 * have got an interrupt?
1138 */
1139 if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
1140 ahbintr(sc);
1141 if (xs->flags & ITSDONE)
1142 return 0;
1143 delay(1000);
1144 count--;
1145 }
1146 return 1;
1147 }
1148
1149 void
1150 ahb_timeout(arg)
1151 void *arg;
1152 {
1153 struct ahb_ecb *ecb = arg;
1154 struct scsipi_xfer *xs = ecb->xs;
1155 struct scsipi_link *sc_link = xs->sc_link;
1156 struct ahb_softc *sc = sc_link->adapter_softc;
1157 int s;
1158
1159 scsi_print_addr(sc_link);
1160 printf("timed out");
1161
1162 s = splbio();
1163
1164 if (ecb->flags & ECB_IMMED) {
1165 printf("\n");
1166 ecb->flags |= ECB_IMMED_FAIL;
1167 /* XXX Must reset! */
1168 } else
1169
1170 /*
1171 * If it has been through before, then
1172 * a previous abort has failed, don't
1173 * try abort again
1174 */
1175 if (ecb->flags & ECB_ABORT) {
1176 /* abort timed out */
1177 printf(" AGAIN\n");
1178 /* XXX Must reset! */
1179 } else {
1180 /* abort the operation that has timed out */
1181 printf("\n");
1182 ecb->xs->error = XS_TIMEOUT;
1183 ecb->timeout = AHB_ABORT_TIMEOUT;
1184 ecb->flags |= ECB_ABORT;
1185 ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
1186 }
1187
1188 splx(s);
1189 }
1190