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