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