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