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