ahb.c revision 1.3 1 /* $NetBSD: ahb.c,v 1.3 1996/09/19 23:02:18 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) 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 printf(": %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 printf("%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 printf("%s: couldn't establish interrupt",
240 sc->sc_dev.dv_xname);
241 if (intrstr != NULL)
242 printf(" at %s", intrstr);
243 printf("\n");
244 return;
245 }
246 if (intrstr != NULL)
247 printf("%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 printf("%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 printf("%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 printf("%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 printf("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 printf("%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 printf("%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 printf("%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 printf("%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 printf("%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 printf("%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 printf("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 printf("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 printf(".");
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 printf("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 struct iovec *iovp;
706 int s;
707
708 SC_DEBUG(sc_link, SDEV_DB2, ("ahb_scsi_cmd\n"));
709 /*
710 * get a ecb (mbox-out) to use. If the transfer
711 * is from a buf (possibly from interrupt time)
712 * then we can't allow it to sleep
713 */
714 flags = xs->flags;
715 if ((ecb = ahb_get_ecb(sc, flags)) == NULL) {
716 xs->error = XS_DRIVER_STUFFUP;
717 return TRY_AGAIN_LATER;
718 }
719 ecb->xs = xs;
720 ecb->timeout = xs->timeout;
721
722 /*
723 * If it's a reset, we need to do an 'immediate'
724 * command, and store its ecb for later
725 * if there is already an immediate waiting,
726 * then WE must wait
727 */
728 if (flags & SCSI_RESET) {
729 ecb->flags |= ECB_IMMED;
730 if (sc->sc_immed_ecb)
731 return TRY_AGAIN_LATER;
732 sc->sc_immed_ecb = ecb;
733
734 s = splbio();
735 ahb_send_immed(sc, AHB_TARG_RESET, ecb);
736 splx(s);
737
738 if ((flags & SCSI_POLL) == 0)
739 return SUCCESSFULLY_QUEUED;
740
741 /*
742 * If we can't use interrupts, poll on completion
743 */
744 if (ahb_poll(sc, xs, ecb->timeout))
745 ahb_timeout(ecb);
746 return COMPLETE;
747 }
748
749 /*
750 * Put all the arguments for the xfer in the ecb
751 */
752 ecb->opcode = ECB_SCSI_OP;
753 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
754 ecb->opt2 = sc_link->lun | ECB_NRB;
755 bcopy(xs->cmd, &ecb->scsi_cmd, ecb->scsi_cmd_length = xs->cmdlen);
756 ecb->sense_ptr = KVTOPHYS(&ecb->ecb_sense);
757 ecb->req_sense_length = sizeof(ecb->ecb_sense);
758 ecb->status = KVTOPHYS(&ecb->ecb_status);
759 ecb->ecb_status.host_stat = 0x00;
760 ecb->ecb_status.target_stat = 0x00;
761
762 if (xs->datalen) {
763 sg = ecb->ahb_dma;
764 seg = 0;
765 #ifdef TFS
766 if (flags & SCSI_DATA_UIO) {
767 iovp = ((struct uio *) xs->data)->uio_iov;
768 datalen = ((struct uio *) xs->data)->uio_iovcnt;
769 xs->datalen = 0;
770 while (datalen && seg < AHB_NSEG) {
771 sg->seg_addr = (physaddr)iovp->iov_base;
772 sg->seg_len = iovp->iov_len;
773 xs->datalen += iovp->iov_len;
774 SC_DEBUGN(sc_link, SDEV_DB4, ("(0x%x@0x%x)",
775 iovp->iov_len, iovp->iov_base));
776 sg++;
777 iovp++;
778 seg++;
779 datalen--;
780 }
781 }
782 else
783 #endif /*TFS */
784 {
785 /*
786 * Set up the scatter gather block
787 */
788 SC_DEBUG(sc_link, SDEV_DB4,
789 ("%d @0x%x:- ", xs->datalen, xs->data));
790 datalen = xs->datalen;
791 thiskv = (long) xs->data;
792 thisphys = KVTOPHYS(thiskv);
793
794 while (datalen && seg < AHB_NSEG) {
795 bytes_this_seg = 0;
796
797 /* put in the base address */
798 sg->seg_addr = thisphys;
799
800 SC_DEBUGN(sc_link, SDEV_DB4, ("0x%x", thisphys));
801
802 /* do it at least once */
803 nextphys = thisphys;
804 while (datalen && thisphys == nextphys) {
805 /*
806 * This page is contiguous (physically)
807 * with the the last, just extend the
808 * length
809 */
810 /* how far to the end of the page */
811 nextphys = (thisphys & ~PGOFSET) + NBPG;
812 bytes_this_page = nextphys - thisphys;
813 /**** or the data ****/
814 bytes_this_page = min(bytes_this_page,
815 datalen);
816 bytes_this_seg += bytes_this_page;
817 datalen -= bytes_this_page;
818
819 /* get more ready for the next page */
820 thiskv = (thiskv & ~PGOFSET) + NBPG;
821 if (datalen)
822 thisphys = KVTOPHYS(thiskv);
823 }
824 /*
825 * next page isn't contiguous, finish the seg
826 */
827 SC_DEBUGN(sc_link, SDEV_DB4,
828 ("(0x%x)", bytes_this_seg));
829 sg->seg_len = bytes_this_seg;
830 sg++;
831 seg++;
832 }
833 }
834 /*end of iov/kv decision */
835 SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
836 if (datalen) {
837 /*
838 * there's still data, must have run out of segs!
839 */
840 printf("%s: ahb_scsi_cmd, more than %d dma segs\n",
841 sc->sc_dev.dv_xname, AHB_NSEG);
842 goto bad;
843 }
844 ecb->data_addr = KVTOPHYS(ecb->ahb_dma);
845 ecb->data_length = seg * sizeof(struct ahb_dma_seg);
846 ecb->opt1 |= ECB_S_G;
847 } else { /* No data xfer, use non S/G values */
848 ecb->data_addr = (physaddr)0;
849 ecb->data_length = 0;
850 }
851 ecb->link_addr = (physaddr)0;
852
853 s = splbio();
854 ahb_send_mbox(sc, OP_START_ECB, ecb);
855 splx(s);
856
857 /*
858 * Usually return SUCCESSFULLY QUEUED
859 */
860 if ((flags & SCSI_POLL) == 0)
861 return SUCCESSFULLY_QUEUED;
862
863 /*
864 * If we can't use interrupts, poll on completion
865 */
866 if (ahb_poll(sc, xs, ecb->timeout)) {
867 ahb_timeout(ecb);
868 if (ahb_poll(sc, xs, ecb->timeout))
869 ahb_timeout(ecb);
870 }
871 return COMPLETE;
872
873 bad:
874 xs->error = XS_DRIVER_STUFFUP;
875 ahb_free_ecb(sc, ecb);
876 return COMPLETE;
877 }
878
879 /*
880 * Function to poll for command completion when in poll mode
881 */
882 int
883 ahb_poll(sc, xs, count)
884 struct ahb_softc *sc;
885 struct scsi_xfer *xs;
886 int count;
887 { /* in msec */
888 bus_chipset_tag_t bc = sc->sc_bc;
889 bus_io_handle_t ioh = sc->sc_ioh;
890
891 while (count) {
892 /*
893 * If we had interrupts enabled, would we
894 * have got an interrupt?
895 */
896 if (bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND)
897 ahbintr(sc);
898 if (xs->flags & ITSDONE)
899 return 0;
900 delay(1000);
901 count--;
902 }
903 return 1;
904 }
905
906 void
907 ahb_timeout(arg)
908 void *arg;
909 {
910 struct ahb_ecb *ecb = arg;
911 struct scsi_xfer *xs = ecb->xs;
912 struct scsi_link *sc_link = xs->sc_link;
913 struct ahb_softc *sc = sc_link->adapter_softc;
914 int s;
915
916 sc_print_addr(sc_link);
917 printf("timed out");
918
919 s = splbio();
920
921 if (ecb->flags & ECB_IMMED) {
922 printf("\n");
923 ecb->flags |= ECB_IMMED_FAIL;
924 /* XXX Must reset! */
925 } else
926
927 /*
928 * If it has been through before, then
929 * a previous abort has failed, don't
930 * try abort again
931 */
932 if (ecb->flags & ECB_ABORT) {
933 /* abort timed out */
934 printf(" AGAIN\n");
935 /* XXX Must reset! */
936 } else {
937 /* abort the operation that has timed out */
938 printf("\n");
939 ecb->xs->error = XS_TIMEOUT;
940 ecb->timeout = AHB_ABORT_TIMEOUT;
941 ecb->flags |= ECB_ABORT;
942 ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
943 }
944
945 splx(s);
946 }
947