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