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