ahb.c revision 1.4 1 /* $NetBSD: ahb.c,v 1.4 1996/10/10 19:54:10 christos 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) 1994, 1996 Charles M. Hannum. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Charles M. Hannum.
24 * 4. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Originally written by Julian Elischer (julian (at) tfs.com)
41 * for TRW Financial Systems for use under the MACH(2.5) operating system.
42 *
43 * TRW Financial Systems, in accordance with their agreement with Carnegie
44 * Mellon University, makes this software available to CMU to distribute
45 * or use in any manner that they see fit as long as this message is kept with
46 * the software. For this reason TFS also grants any other persons or
47 * organisations permission to use or modify this software.
48 *
49 * TFS supplies this software to be publicly redistributed
50 * on the understanding that TFS is not responsible for the correct
51 * functioning of this software in any circumstances.
52 */
53
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/errno.h>
59 #include <sys/ioctl.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62 #include <sys/buf.h>
63 #include <sys/proc.h>
64 #include <sys/user.h>
65
66 #include <machine/bus.h>
67 #include <machine/intr.h>
68
69 #include <scsi/scsi_all.h>
70 #include <scsi/scsiconf.h>
71
72 #include <dev/eisa/eisareg.h>
73 #include <dev/eisa/eisavar.h>
74 #include <dev/eisa/eisadevs.h>
75 #include <dev/eisa/ahbreg.h>
76
77 #ifndef DDB
78 #define Debugger() panic("should call debugger here (aha1742.c)")
79 #endif /* ! DDB */
80
81 #define AHB_ECB_MAX 32 /* store up to 32 ECBs at one time */
82 #define ECB_HASH_SIZE 32 /* hash table size for phystokv */
83 #define ECB_HASH_SHIFT 9
84 #define ECB_HASH(x) ((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
85
86 #define KVTOPHYS(x) vtophys(x)
87
88 struct ahb_softc {
89 struct device sc_dev;
90 bus_chipset_tag_t sc_bc;
91
92 bus_io_handle_t sc_ioh;
93 int sc_irq;
94 void *sc_ih;
95
96 struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
97 TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
98 struct ahb_ecb *sc_immed_ecb; /* an outstanding immediete command */
99 int sc_numecbs;
100 int sc_scsi_dev; /* our scsi id */
101 struct scsi_link sc_link;
102 };
103
104 void ahb_send_mbox __P((struct ahb_softc *, int, struct ahb_ecb *));
105 void ahb_send_immed __P((struct ahb_softc *, u_long, struct ahb_ecb *));
106 int ahbintr __P((void *));
107 void ahb_free_ecb __P((struct ahb_softc *, struct ahb_ecb *));
108 struct ahb_ecb *ahb_get_ecb __P((struct ahb_softc *, int));
109 struct ahb_ecb *ahb_ecb_phys_kv __P((struct ahb_softc *, physaddr));
110 void ahb_done __P((struct ahb_softc *, struct ahb_ecb *));
111 int ahb_find __P((bus_chipset_tag_t, bus_io_handle_t, struct ahb_softc *));
112 void ahb_init __P((struct ahb_softc *));
113 void ahbminphys __P((struct buf *));
114 int ahb_scsi_cmd __P((struct scsi_xfer *));
115 int ahb_poll __P((struct ahb_softc *, struct scsi_xfer *, int));
116 void ahb_timeout __P((void *));
117
118 integrate void ahb_reset_ecb __P((struct ahb_softc *, struct ahb_ecb *));
119 integrate void ahb_init_ecb __P((struct ahb_softc *, struct ahb_ecb *));
120
121 struct scsi_adapter ahb_switch = {
122 ahb_scsi_cmd,
123 ahbminphys,
124 0,
125 0,
126 };
127
128 /* the below structure is so we have a default dev struct for our link struct */
129 struct scsi_device ahb_dev = {
130 NULL, /* Use default error handler */
131 NULL, /* have a queue, served by this */
132 NULL, /* have no async handler */
133 NULL, /* Use default 'done' routine */
134 };
135
136 int ahbmatch __P((struct device *, void *, void *));
137 void ahbattach __P((struct device *, struct device *, void *));
138
139 struct cfattach ahb_ca = {
140 sizeof(struct ahb_softc), ahbmatch, ahbattach
141 };
142
143 struct cfdriver ahb_cd = {
144 NULL, "ahb", DV_DULL
145 };
146
147 #define AHB_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
148
149 /*
150 * Check the slots looking for a board we recognise
151 * If we find one, note it's address (slot) and call
152 * the actual probe routine to check it out.
153 */
154 int
155 ahbmatch(parent, match, aux)
156 struct device *parent;
157 void *match, *aux;
158 {
159 struct eisa_attach_args *ea = aux;
160 bus_chipset_tag_t bc = ea->ea_bc;
161 bus_io_handle_t ioh;
162 int rv;
163
164 /* must match one of our known ID strings */
165 if (strcmp(ea->ea_idstring, "ADP0000") &&
166 strcmp(ea->ea_idstring, "ADP0001") &&
167 strcmp(ea->ea_idstring, "ADP0002") &&
168 strcmp(ea->ea_idstring, "ADP0400"))
169 return (0);
170
171 if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot), EISA_SLOT_SIZE, &ioh))
172 return (0);
173
174 rv = !ahb_find(bc, ioh, NULL);
175
176 bus_io_unmap(bc, ioh, EISA_SLOT_SIZE);
177
178 return (rv);
179 }
180
181 /*
182 * Attach all the sub-devices we can find
183 */
184 void
185 ahbattach(parent, self, aux)
186 struct device *parent, *self;
187 void *aux;
188 {
189 struct eisa_attach_args *ea = aux;
190 struct ahb_softc *sc = (void *)self;
191 bus_chipset_tag_t bc = ea->ea_bc;
192 bus_io_handle_t ioh;
193 eisa_chipset_tag_t ec = ea->ea_ec;
194 eisa_intr_handle_t ih;
195 const char *model, *intrstr;
196
197 if (!strcmp(ea->ea_idstring, "ADP0000"))
198 model = EISA_PRODUCT_ADP0000;
199 else if (!strcmp(ea->ea_idstring, "ADP0001"))
200 model = EISA_PRODUCT_ADP0001;
201 else if (!strcmp(ea->ea_idstring, "ADP0002"))
202 model = EISA_PRODUCT_ADP0002;
203 else if (!strcmp(ea->ea_idstring, "ADP0400"))
204 model = EISA_PRODUCT_ADP0400;
205 else
206 model = "unknown model!";
207 kprintf(": %s\n", model);
208
209 if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot), EISA_SLOT_SIZE, &ioh))
210 panic("ahbattach: could not map I/O addresses");
211
212 sc->sc_bc = bc;
213 sc->sc_ioh = ioh;
214 if (ahb_find(bc, ioh, sc))
215 panic("ahbattach: ahb_find failed!");
216
217 ahb_init(sc);
218 TAILQ_INIT(&sc->sc_free_ecb);
219
220 /*
221 * fill in the prototype scsi_link.
222 */
223 sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
224 sc->sc_link.adapter_softc = sc;
225 sc->sc_link.adapter_target = sc->sc_scsi_dev;
226 sc->sc_link.adapter = &ahb_switch;
227 sc->sc_link.device = &ahb_dev;
228 sc->sc_link.openings = 4;
229
230 if (eisa_intr_map(ec, sc->sc_irq, &ih)) {
231 kprintf("%s: couldn't map interrupt (%d)\n",
232 sc->sc_dev.dv_xname, sc->sc_irq);
233 return;
234 }
235 intrstr = eisa_intr_string(ec, ih);
236 sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
237 ahbintr, sc);
238 if (sc->sc_ih == NULL) {
239 kprintf("%s: couldn't establish interrupt",
240 sc->sc_dev.dv_xname);
241 if (intrstr != NULL)
242 kprintf(" at %s", intrstr);
243 kprintf("\n");
244 return;
245 }
246 if (intrstr != NULL)
247 kprintf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
248 intrstr);
249
250 /*
251 * ask the adapter what subunits are present
252 */
253 config_found(self, &sc->sc_link, scsiprint);
254 }
255
256 /*
257 * Function to send a command out through a mailbox
258 */
259 void
260 ahb_send_mbox(sc, opcode, ecb)
261 struct ahb_softc *sc;
262 int opcode;
263 struct ahb_ecb *ecb;
264 {
265 bus_chipset_tag_t bc = sc->sc_bc;
266 bus_io_handle_t ioh = sc->sc_ioh;
267 int wait = 300; /* 1ms should be enough */
268
269 while (--wait) {
270 if ((bus_io_read_1(bc, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
271 == (G2STAT_MBOX_EMPTY))
272 break;
273 delay(10);
274 }
275 if (!wait) {
276 kprintf("%s: board not responding\n", sc->sc_dev.dv_xname);
277 Debugger();
278 }
279
280 bus_io_write_4(bc, ioh, MBOXOUT0, KVTOPHYS(ecb)); /* don't know this will work */
281 bus_io_write_1(bc, ioh, ATTN, opcode | ecb->xs->sc_link->target);
282
283 if ((ecb->xs->flags & SCSI_POLL) == 0)
284 timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
285 }
286
287 /*
288 * Function to send an immediate type command to the adapter
289 */
290 void
291 ahb_send_immed(sc, cmd, ecb)
292 struct ahb_softc *sc;
293 u_long cmd;
294 struct ahb_ecb *ecb;
295 {
296 bus_chipset_tag_t bc = sc->sc_bc;
297 bus_io_handle_t ioh = sc->sc_ioh;
298 int wait = 100; /* 1 ms enough? */
299
300 while (--wait) {
301 if ((bus_io_read_1(bc, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
302 == (G2STAT_MBOX_EMPTY))
303 break;
304 delay(10);
305 }
306 if (!wait) {
307 kprintf("%s: board not responding\n", sc->sc_dev.dv_xname);
308 Debugger();
309 }
310
311 bus_io_write_4(bc, ioh, MBOXOUT0, cmd); /* don't know this will work */
312 bus_io_write_1(bc, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
313 bus_io_write_1(bc, ioh, ATTN, OP_IMMED | ecb->xs->sc_link->target);
314
315 if ((ecb->xs->flags & SCSI_POLL) == 0)
316 timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
317 }
318
319 /*
320 * Catch an interrupt from the adaptor
321 */
322 int
323 ahbintr(arg)
324 void *arg;
325 {
326 struct ahb_softc *sc = arg;
327 bus_chipset_tag_t bc = sc->sc_bc;
328 bus_io_handle_t ioh = sc->sc_ioh;
329 struct ahb_ecb *ecb;
330 u_char ahbstat;
331 u_long mboxval;
332
333 #ifdef AHBDEBUG
334 kprintf("%s: ahbintr ", sc->sc_dev.dv_xname);
335 #endif /* AHBDEBUG */
336
337 if ((bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
338 return 0;
339
340 for (;;) {
341 /*
342 * First get all the information and then
343 * acknowlege the interrupt
344 */
345 ahbstat = bus_io_read_1(bc, ioh, G2INTST);
346 mboxval = bus_io_read_4(bc, ioh, MBOXIN0);
347 bus_io_write_1(bc, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
348
349 #ifdef AHBDEBUG
350 kprintf("status = 0x%x ", ahbstat);
351 #endif /* AHBDEBUG */
352
353 /*
354 * Process the completed operation
355 */
356 switch (ahbstat & G2INTST_INT_STAT) {
357 case AHB_ECB_OK:
358 case AHB_ECB_RECOVERED:
359 case AHB_ECB_ERR:
360 ecb = ahb_ecb_phys_kv(sc, mboxval);
361 if (!ecb) {
362 kprintf("%s: BAD ECB RETURNED!\n",
363 sc->sc_dev.dv_xname);
364 goto next; /* whatever it was, it'll timeout */
365 }
366 break;
367
368 case AHB_IMMED_ERR:
369 ecb = sc->sc_immed_ecb;
370 sc->sc_immed_ecb = 0;
371 ecb->flags |= ECB_IMMED_FAIL;
372 break;
373
374 case AHB_IMMED_OK:
375 ecb = sc->sc_immed_ecb;
376 sc->sc_immed_ecb = 0;
377 break;
378
379 default:
380 kprintf("%s: unexpected interrupt %x\n",
381 sc->sc_dev.dv_xname, ahbstat);
382 goto next;
383 }
384
385 untimeout(ahb_timeout, ecb);
386 ahb_done(sc, ecb);
387
388 next:
389 if ((bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
390 return 1;
391 }
392 }
393
394 integrate void
395 ahb_reset_ecb(sc, ecb)
396 struct ahb_softc *sc;
397 struct ahb_ecb *ecb;
398 {
399
400 ecb->flags = 0;
401 }
402
403 /*
404 * A ecb (and hence a mbx-out is put onto the
405 * free list.
406 */
407 void
408 ahb_free_ecb(sc, ecb)
409 struct ahb_softc *sc;
410 struct ahb_ecb *ecb;
411 {
412 int s;
413
414 s = splbio();
415
416 ahb_reset_ecb(sc, ecb);
417 TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
418
419 /*
420 * If there were none, wake anybody waiting for one to come free,
421 * starting with queued entries.
422 */
423 if (ecb->chain.tqe_next == 0)
424 wakeup(&sc->sc_free_ecb);
425
426 splx(s);
427 }
428
429 integrate void
430 ahb_init_ecb(sc, ecb)
431 struct ahb_softc *sc;
432 struct ahb_ecb *ecb;
433 {
434 int hashnum;
435
436 bzero(ecb, sizeof(struct ahb_ecb));
437 /*
438 * put in the phystokv hash table
439 * Never gets taken out.
440 */
441 ecb->hashkey = KVTOPHYS(ecb);
442 hashnum = ECB_HASH(ecb->hashkey);
443 ecb->nexthash = sc->sc_ecbhash[hashnum];
444 sc->sc_ecbhash[hashnum] = ecb;
445 ahb_reset_ecb(sc, ecb);
446 }
447
448 /*
449 * Get a free ecb
450 *
451 * If there are none, see if we can allocate a new one. If so, put it in the
452 * hash table too otherwise either return an error or sleep.
453 */
454 struct ahb_ecb *
455 ahb_get_ecb(sc, flags)
456 struct ahb_softc *sc;
457 int flags;
458 {
459 struct ahb_ecb *ecb;
460 int s;
461
462 s = splbio();
463
464 /*
465 * If we can and have to, sleep waiting for one to come free
466 * but only if we can't allocate a new one.
467 */
468 for (;;) {
469 ecb = sc->sc_free_ecb.tqh_first;
470 if (ecb) {
471 TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
472 break;
473 }
474 if (sc->sc_numecbs < AHB_ECB_MAX) {
475 ecb = (struct ahb_ecb *) malloc(sizeof(struct ahb_ecb),
476 M_TEMP, M_NOWAIT);
477 if (!ecb) {
478 kprintf("%s: can't malloc ecb\n",
479 sc->sc_dev.dv_xname);
480 goto out;
481 }
482 ahb_init_ecb(sc, ecb);
483 sc->sc_numecbs++;
484 break;
485 }
486 if ((flags & SCSI_NOSLEEP) != 0)
487 goto out;
488 tsleep(&sc->sc_free_ecb, PRIBIO, "ahbecb", 0);
489 }
490
491 ecb->flags |= ECB_ALLOC;
492
493 out:
494 splx(s);
495 return ecb;
496 }
497
498 /*
499 * given a physical address, find the ecb that it corresponds to.
500 */
501 struct ahb_ecb *
502 ahb_ecb_phys_kv(sc, ecb_phys)
503 struct ahb_softc *sc;
504 physaddr ecb_phys;
505 {
506 int hashnum = ECB_HASH(ecb_phys);
507 struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
508
509 while (ecb) {
510 if (ecb->hashkey == ecb_phys)
511 break;
512 ecb = ecb->nexthash;
513 }
514 return ecb;
515 }
516
517 /*
518 * We have a ecb which has been processed by the adaptor, now we look to see
519 * how the operation went.
520 */
521 void
522 ahb_done(sc, ecb)
523 struct ahb_softc *sc;
524 struct ahb_ecb *ecb;
525 {
526 struct scsi_sense_data *s1, *s2;
527 struct scsi_xfer *xs = ecb->xs;
528
529 SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahb_done\n"));
530 /*
531 * Otherwise, put the results of the operation
532 * into the xfer and call whoever started it
533 */
534 if ((ecb->flags & ECB_ALLOC) == 0) {
535 kprintf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
536 Debugger();
537 }
538 if (ecb->flags & ECB_IMMED) {
539 if (ecb->flags & ECB_IMMED_FAIL)
540 xs->error = XS_DRIVER_STUFFUP;
541 goto done;
542 }
543 if (xs->error == XS_NOERROR) {
544 if (ecb->ecb_status.host_stat != HS_OK) {
545 switch (ecb->ecb_status.host_stat) {
546 case HS_TIMED_OUT: /* No response */
547 xs->error = XS_SELTIMEOUT;
548 break;
549 default: /* Other scsi protocol messes */
550 kprintf("%s: host_stat %x\n",
551 sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
552 xs->error = XS_DRIVER_STUFFUP;
553 }
554 } else if (ecb->ecb_status.target_stat != SCSI_OK) {
555 switch (ecb->ecb_status.target_stat) {
556 case SCSI_CHECK:
557 s1 = &ecb->ecb_sense;
558 s2 = &xs->sense;
559 *s2 = *s1;
560 xs->error = XS_SENSE;
561 break;
562 case SCSI_BUSY:
563 xs->error = XS_BUSY;
564 break;
565 default:
566 kprintf("%s: target_stat %x\n",
567 sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
568 xs->error = XS_DRIVER_STUFFUP;
569 }
570 } else
571 xs->resid = 0;
572 }
573 done:
574 ahb_free_ecb(sc, ecb);
575 xs->flags |= ITSDONE;
576 scsi_done(xs);
577 }
578
579 /*
580 * Start the board, ready for normal operation
581 */
582 int
583 ahb_find(bc, ioh, sc)
584 bus_chipset_tag_t bc;
585 bus_io_handle_t ioh;
586 struct ahb_softc *sc;
587 {
588 u_char intdef;
589 int i, irq, busid;
590 int wait = 1000; /* 1 sec enough? */
591
592 bus_io_write_1(bc, ioh, PORTADDR, PORTADDR_ENHANCED);
593
594 #define NO_NO 1
595 #ifdef NO_NO
596 /*
597 * reset board, If it doesn't respond, assume
598 * that it's not there.. good for the probe
599 */
600 bus_io_write_1(bc, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
601 delay(1000);
602 bus_io_write_1(bc, ioh, G2CNTRL, 0);
603 delay(10000);
604 while (--wait) {
605 if ((bus_io_read_1(bc, ioh, G2STAT) & G2STAT_BUSY) == 0)
606 break;
607 delay(1000);
608 }
609 if (!wait) {
610 #ifdef AHBDEBUG
611 kprintf("ahb_find: No answer from aha1742 board\n");
612 #endif /* AHBDEBUG */
613 return ENXIO;
614 }
615 i = bus_io_read_1(bc, ioh, MBOXIN0);
616 if (i) {
617 kprintf("self test failed, val = 0x%x\n", i);
618 return EIO;
619 }
620
621 /* Set it again, just to be sure. */
622 bus_io_write_1(bc, ioh, PORTADDR, PORTADDR_ENHANCED);
623 #endif
624
625 while (bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND) {
626 kprintf(".");
627 bus_io_write_1(bc, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
628 delay(10000);
629 }
630
631 intdef = bus_io_read_1(bc, ioh, INTDEF);
632 switch (intdef & 0x07) {
633 case INT9:
634 irq = 9;
635 break;
636 case INT10:
637 irq = 10;
638 break;
639 case INT11:
640 irq = 11;
641 break;
642 case INT12:
643 irq = 12;
644 break;
645 case INT14:
646 irq = 14;
647 break;
648 case INT15:
649 irq = 15;
650 break;
651 default:
652 kprintf("illegal int setting %x\n", intdef);
653 return EIO;
654 }
655
656 bus_io_write_1(bc, ioh, INTDEF, (intdef | INTEN)); /* make sure we can interrupt */
657
658 /* who are we on the scsi bus? */
659 busid = (bus_io_read_1(bc, ioh, SCSIDEF) & HSCSIID);
660
661 /* if we want to fill in softc, do so now */
662 if (sc != NULL) {
663 sc->sc_irq = irq;
664 sc->sc_scsi_dev = busid;
665 }
666
667 /*
668 * Note that we are going and return (to probe)
669 */
670 return 0;
671 }
672
673 void
674 ahb_init(sc)
675 struct ahb_softc *sc;
676 {
677
678 }
679
680 void
681 ahbminphys(bp)
682 struct buf *bp;
683 {
684
685 if (bp->b_bcount > ((AHB_NSEG - 1) << PGSHIFT))
686 bp->b_bcount = ((AHB_NSEG - 1) << PGSHIFT);
687 minphys(bp);
688 }
689
690 /*
691 * start a scsi operation given the command and the data address. Also needs
692 * the unit, target and lu.
693 */
694 int
695 ahb_scsi_cmd(xs)
696 struct scsi_xfer *xs;
697 {
698 struct scsi_link *sc_link = xs->sc_link;
699 struct ahb_softc *sc = sc_link->adapter_softc;
700 struct ahb_ecb *ecb;
701 struct ahb_dma_seg *sg;
702 int seg; /* scatter gather seg being worked on */
703 u_long thiskv, thisphys, nextphys;
704 int bytes_this_seg, bytes_this_page, datalen, flags;
705 int s;
706
707 SC_DEBUG(sc_link, SDEV_DB2, ("ahb_scsi_cmd\n"));
708 /*
709 * get a ecb (mbox-out) to use. If the transfer
710 * is from a buf (possibly from interrupt time)
711 * then we can't allow it to sleep
712 */
713 flags = xs->flags;
714 if ((ecb = ahb_get_ecb(sc, flags)) == NULL) {
715 xs->error = XS_DRIVER_STUFFUP;
716 return TRY_AGAIN_LATER;
717 }
718 ecb->xs = xs;
719 ecb->timeout = xs->timeout;
720
721 /*
722 * If it's a reset, we need to do an 'immediate'
723 * command, and store its ecb for later
724 * if there is already an immediate waiting,
725 * then WE must wait
726 */
727 if (flags & SCSI_RESET) {
728 ecb->flags |= ECB_IMMED;
729 if (sc->sc_immed_ecb)
730 return TRY_AGAIN_LATER;
731 sc->sc_immed_ecb = ecb;
732
733 s = splbio();
734 ahb_send_immed(sc, AHB_TARG_RESET, ecb);
735 splx(s);
736
737 if ((flags & SCSI_POLL) == 0)
738 return SUCCESSFULLY_QUEUED;
739
740 /*
741 * If we can't use interrupts, poll on completion
742 */
743 if (ahb_poll(sc, xs, ecb->timeout))
744 ahb_timeout(ecb);
745 return COMPLETE;
746 }
747
748 /*
749 * Put all the arguments for the xfer in the ecb
750 */
751 ecb->opcode = ECB_SCSI_OP;
752 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
753 ecb->opt2 = sc_link->lun | ECB_NRB;
754 bcopy(xs->cmd, &ecb->scsi_cmd, ecb->scsi_cmd_length = xs->cmdlen);
755 ecb->sense_ptr = KVTOPHYS(&ecb->ecb_sense);
756 ecb->req_sense_length = sizeof(ecb->ecb_sense);
757 ecb->status = KVTOPHYS(&ecb->ecb_status);
758 ecb->ecb_status.host_stat = 0x00;
759 ecb->ecb_status.target_stat = 0x00;
760
761 if (xs->datalen) {
762 sg = ecb->ahb_dma;
763 seg = 0;
764 #ifdef TFS
765 if (flags & SCSI_DATA_UIO) {
766 struct iovec *iovp = ((struct uio *) xs->data)->uio_iov;
767 datalen = ((struct uio *) xs->data)->uio_iovcnt;
768 xs->datalen = 0;
769 while (datalen && seg < AHB_NSEG) {
770 sg->seg_addr = (physaddr)iovp->iov_base;
771 sg->seg_len = iovp->iov_len;
772 xs->datalen += iovp->iov_len;
773 SC_DEBUGN(sc_link, SDEV_DB4, ("(0x%x@0x%x)",
774 iovp->iov_len, iovp->iov_base));
775 sg++;
776 iovp++;
777 seg++;
778 datalen--;
779 }
780 }
781 else
782 #endif /*TFS */
783 {
784 /*
785 * Set up the scatter gather block
786 */
787 SC_DEBUG(sc_link, SDEV_DB4,
788 ("%d @0x%x:- ", xs->datalen, xs->data));
789 datalen = xs->datalen;
790 thiskv = (long) xs->data;
791 thisphys = KVTOPHYS(thiskv);
792
793 while (datalen && seg < AHB_NSEG) {
794 bytes_this_seg = 0;
795
796 /* put in the base address */
797 sg->seg_addr = thisphys;
798
799 SC_DEBUGN(sc_link, SDEV_DB4, ("0x%x", thisphys));
800
801 /* do it at least once */
802 nextphys = thisphys;
803 while (datalen && thisphys == nextphys) {
804 /*
805 * This page is contiguous (physically)
806 * with the the last, just extend the
807 * length
808 */
809 /* how far to the end of the page */
810 nextphys = (thisphys & ~PGOFSET) + NBPG;
811 bytes_this_page = nextphys - thisphys;
812 /**** or the data ****/
813 bytes_this_page = min(bytes_this_page,
814 datalen);
815 bytes_this_seg += bytes_this_page;
816 datalen -= bytes_this_page;
817
818 /* get more ready for the next page */
819 thiskv = (thiskv & ~PGOFSET) + NBPG;
820 if (datalen)
821 thisphys = KVTOPHYS(thiskv);
822 }
823 /*
824 * next page isn't contiguous, finish the seg
825 */
826 SC_DEBUGN(sc_link, SDEV_DB4,
827 ("(0x%x)", bytes_this_seg));
828 sg->seg_len = bytes_this_seg;
829 sg++;
830 seg++;
831 }
832 }
833 /*end of iov/kv decision */
834 SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
835 if (datalen) {
836 /*
837 * there's still data, must have run out of segs!
838 */
839 kprintf("%s: ahb_scsi_cmd, more than %d dma segs\n",
840 sc->sc_dev.dv_xname, AHB_NSEG);
841 goto bad;
842 }
843 ecb->data_addr = KVTOPHYS(ecb->ahb_dma);
844 ecb->data_length = seg * sizeof(struct ahb_dma_seg);
845 ecb->opt1 |= ECB_S_G;
846 } else { /* No data xfer, use non S/G values */
847 ecb->data_addr = (physaddr)0;
848 ecb->data_length = 0;
849 }
850 ecb->link_addr = (physaddr)0;
851
852 s = splbio();
853 ahb_send_mbox(sc, OP_START_ECB, ecb);
854 splx(s);
855
856 /*
857 * Usually return SUCCESSFULLY QUEUED
858 */
859 if ((flags & SCSI_POLL) == 0)
860 return SUCCESSFULLY_QUEUED;
861
862 /*
863 * If we can't use interrupts, poll on completion
864 */
865 if (ahb_poll(sc, xs, ecb->timeout)) {
866 ahb_timeout(ecb);
867 if (ahb_poll(sc, xs, ecb->timeout))
868 ahb_timeout(ecb);
869 }
870 return COMPLETE;
871
872 bad:
873 xs->error = XS_DRIVER_STUFFUP;
874 ahb_free_ecb(sc, ecb);
875 return COMPLETE;
876 }
877
878 /*
879 * Function to poll for command completion when in poll mode
880 */
881 int
882 ahb_poll(sc, xs, count)
883 struct ahb_softc *sc;
884 struct scsi_xfer *xs;
885 int count;
886 { /* in msec */
887 bus_chipset_tag_t bc = sc->sc_bc;
888 bus_io_handle_t ioh = sc->sc_ioh;
889
890 while (count) {
891 /*
892 * If we had interrupts enabled, would we
893 * have got an interrupt?
894 */
895 if (bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND)
896 ahbintr(sc);
897 if (xs->flags & ITSDONE)
898 return 0;
899 delay(1000);
900 count--;
901 }
902 return 1;
903 }
904
905 void
906 ahb_timeout(arg)
907 void *arg;
908 {
909 struct ahb_ecb *ecb = arg;
910 struct scsi_xfer *xs = ecb->xs;
911 struct scsi_link *sc_link = xs->sc_link;
912 struct ahb_softc *sc = sc_link->adapter_softc;
913 int s;
914
915 sc_print_addr(sc_link);
916 kprintf("timed out");
917
918 s = splbio();
919
920 if (ecb->flags & ECB_IMMED) {
921 kprintf("\n");
922 ecb->flags |= ECB_IMMED_FAIL;
923 /* XXX Must reset! */
924 } else
925
926 /*
927 * If it has been through before, then
928 * a previous abort has failed, don't
929 * try abort again
930 */
931 if (ecb->flags & ECB_ABORT) {
932 /* abort timed out */
933 kprintf(" AGAIN\n");
934 /* XXX Must reset! */
935 } else {
936 /* abort the operation that has timed out */
937 kprintf("\n");
938 ecb->xs->error = XS_TIMEOUT;
939 ecb->timeout = AHB_ABORT_TIMEOUT;
940 ecb->flags |= ECB_ABORT;
941 ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
942 }
943
944 splx(s);
945 }
946