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