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