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