bha.c revision 1.20 1 /* $NetBSD: bha.c,v 1.20 1998/02/04 00:36:52 thorpej Exp $ */
2
3 #undef BHADIAG
4 #ifdef DDB
5 #define integrate
6 #else
7 #define integrate static inline
8 #endif
9
10 /*-
11 * Copyright (c) 1997 The NetBSD Foundation, Inc.
12 * All rights reserved.
13 *
14 * This code is derived from software contributed to The NetBSD Foundation
15 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
16 * NASA Ames Research Center.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the NetBSD
29 * Foundation, Inc. and its contributors.
30 * 4. Neither the name of The NetBSD Foundation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 * POSSIBILITY OF SUCH DAMAGE.
45 */
46
47 /*
48 * Copyright (c) 1994, 1996, 1997 Charles M. Hannum. All rights reserved.
49 *
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. All advertising materials mentioning features or use of this software
59 * must display the following acknowledgement:
60 * This product includes software developed by Charles M. Hannum.
61 * 4. The name of the author may not be used to endorse or promote products
62 * derived from this software without specific prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
65 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
66 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
67 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
68 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
69 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
73 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 */
75
76 /*
77 * Originally written by Julian Elischer (julian (at) tfs.com)
78 * for TRW Financial Systems for use under the MACH(2.5) operating system.
79 *
80 * TRW Financial Systems, in accordance with their agreement with Carnegie
81 * Mellon University, makes this software available to CMU to distribute
82 * or use in any manner that they see fit as long as this message is kept with
83 * the software. For this reason TFS also grants any other persons or
84 * organisations permission to use or modify this software.
85 *
86 * TFS supplies this software to be publicly redistributed
87 * on the understanding that TFS is not responsible for the correct
88 * functioning of this software in any circumstances.
89 */
90
91 #include <sys/types.h>
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/kernel.h>
95 #include <sys/errno.h>
96 #include <sys/ioctl.h>
97 #include <sys/device.h>
98 #include <sys/malloc.h>
99 #include <sys/buf.h>
100 #include <sys/proc.h>
101 #include <sys/user.h>
102
103 #include <machine/bus.h>
104 #include <machine/intr.h>
105
106 #include <dev/scsipi/scsi_all.h>
107 #include <dev/scsipi/scsipi_all.h>
108 #include <dev/scsipi/scsiconf.h>
109
110 #include <dev/ic/bhareg.h>
111 #include <dev/ic/bhavar.h>
112
113 #ifndef DDB
114 #define Debugger() panic("should call debugger here (bha.c)")
115 #endif /* ! DDB */
116
117 #define BHA_MAXXFER ((BHA_NSEG - 1) << PGSHIFT)
118
119 #ifdef BHADEBUG
120 int bha_debug = 0;
121 #endif /* BHADEBUG */
122
123 int bha_cmd __P((bus_space_tag_t, bus_space_handle_t, struct bha_softc *,
124 int, u_char *, int, u_char *));
125 integrate void bha_finish_ccbs __P((struct bha_softc *));
126 integrate void bha_reset_ccb __P((struct bha_softc *, struct bha_ccb *));
127 void bha_free_ccb __P((struct bha_softc *, struct bha_ccb *));
128 integrate int bha_init_ccb __P((struct bha_softc *, struct bha_ccb *));
129 struct bha_ccb *bha_get_ccb __P((struct bha_softc *, int));
130 struct bha_ccb *bha_ccb_phys_kv __P((struct bha_softc *, u_long));
131 void bha_queue_ccb __P((struct bha_softc *, struct bha_ccb *));
132 void bha_collect_mbo __P((struct bha_softc *));
133 void bha_start_ccbs __P((struct bha_softc *));
134 void bha_done __P((struct bha_softc *, struct bha_ccb *));
135 void bha_init __P((struct bha_softc *));
136 void bhaminphys __P((struct buf *));
137 int bha_scsi_cmd __P((struct scsipi_xfer *));
138 int bha_poll __P((struct bha_softc *, struct scsipi_xfer *, int));
139 void bha_timeout __P((void *arg));
140 int bha_create_ccbs __P((struct bha_softc *, void *, size_t, int));
141 void bha_enqueue __P((struct bha_softc *, struct scsipi_xfer *, int));
142 struct scsipi_xfer *bha_dequeue __P((struct bha_softc *));
143
144 struct scsipi_adapter bha_switch = {
145 bha_scsi_cmd,
146 bhaminphys,
147 0,
148 0,
149 };
150
151 /* the below structure is so we have a default dev struct for out link struct */
152 struct scsipi_device bha_dev = {
153 NULL, /* Use default error handler */
154 NULL, /* have a queue, served by this */
155 NULL, /* have no async handler */
156 NULL, /* Use default 'done' routine */
157 };
158
159 #define BHA_RESET_TIMEOUT 2000 /* time to wait for reset (mSec) */
160 #define BHA_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
161
162 /*
163 * Insert a scsipi_xfer into the software queue. We overload xs->free_list
164 * to avoid having to allocate additional resources (since we're used
165 * only during resource shortages anyhow.
166 */
167 void
168 bha_enqueue(sc, xs, infront)
169 struct bha_softc *sc;
170 struct scsipi_xfer *xs;
171 int infront;
172 {
173
174 if (infront || sc->sc_queue.lh_first == NULL) {
175 if (sc->sc_queue.lh_first == NULL)
176 sc->sc_queuelast = xs;
177 LIST_INSERT_HEAD(&sc->sc_queue, xs, free_list);
178 return;
179 }
180
181 LIST_INSERT_AFTER(sc->sc_queuelast, xs, free_list);
182 sc->sc_queuelast = xs;
183 }
184
185 /*
186 * Pull a scsipi_xfer off the front of the software queue.
187 */
188 struct scsipi_xfer *
189 bha_dequeue(sc)
190 struct bha_softc *sc;
191 {
192 struct scsipi_xfer *xs;
193
194 xs = sc->sc_queue.lh_first;
195 LIST_REMOVE(xs, free_list);
196
197 if (sc->sc_queue.lh_first == NULL)
198 sc->sc_queuelast = NULL;
199
200 return (xs);
201 }
202
203 /*
204 * bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
205 *
206 * Activate Adapter command
207 * icnt: number of args (outbound bytes including opcode)
208 * ibuf: argument buffer
209 * ocnt: number of expected returned bytes
210 * obuf: result buffer
211 * wait: number of seconds to wait for response
212 *
213 * Performs an adapter command through the ports. Not to be confused with a
214 * scsi command, which is read in via the dma; one of the adapter commands
215 * tells it to read in a scsi command.
216 */
217 int
218 bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
219 bus_space_tag_t iot;
220 bus_space_handle_t ioh;
221 struct bha_softc *sc;
222 int icnt, ocnt;
223 u_char *ibuf, *obuf;
224 {
225 const char *name;
226 register int i;
227 int wait;
228 u_char sts;
229 u_char opcode = ibuf[0];
230
231 if (sc != NULL)
232 name = sc->sc_dev.dv_xname;
233 else
234 name = "(bha probe)";
235
236 /*
237 * Calculate a reasonable timeout for the command.
238 */
239 switch (opcode) {
240 case BHA_INQUIRE_DEVICES:
241 case BHA_INQUIRE_DEVICES_2:
242 wait = 90 * 20000;
243 break;
244 default:
245 wait = 1 * 20000;
246 break;
247 }
248
249 /*
250 * Wait for the adapter to go idle, unless it's one of
251 * the commands which don't need this
252 */
253 if (opcode != BHA_MBO_INTR_EN) {
254 for (i = 20000; i; i--) { /* 1 sec? */
255 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
256 if (sts & BHA_STAT_IDLE)
257 break;
258 delay(50);
259 }
260 if (!i) {
261 printf("%s: bha_cmd, host not idle(0x%x)\n",
262 name, sts);
263 return (1);
264 }
265 }
266 /*
267 * Now that it is idle, if we expect output, preflush the
268 * queue feeding to us.
269 */
270 if (ocnt) {
271 while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
272 BHA_STAT_DF)
273 bus_space_read_1(iot, ioh, BHA_DATA_PORT);
274 }
275 /*
276 * Output the command and the number of arguments given
277 * for each byte, first check the port is empty.
278 */
279 while (icnt--) {
280 for (i = wait; i; i--) {
281 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
282 if (!(sts & BHA_STAT_CDF))
283 break;
284 delay(50);
285 }
286 if (!i) {
287 if (opcode != BHA_INQUIRE_REVISION)
288 printf("%s: bha_cmd, cmd/data port full\n",
289 name);
290 goto bad;
291 }
292 bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
293 }
294 /*
295 * If we expect input, loop that many times, each time,
296 * looking for the data register to have valid data
297 */
298 while (ocnt--) {
299 for (i = wait; i; i--) {
300 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
301 if (sts & BHA_STAT_DF)
302 break;
303 delay(50);
304 }
305 if (!i) {
306 if (opcode != BHA_INQUIRE_REVISION)
307 printf("%s: bha_cmd, cmd/data port empty %d\n",
308 name, ocnt);
309 goto bad;
310 }
311 *obuf++ = bus_space_read_1(iot, ioh, BHA_DATA_PORT);
312 }
313 /*
314 * Wait for the board to report a finished instruction.
315 * We may get an extra interrupt for the HACC signal, but this is
316 * unimportant.
317 */
318 if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
319 for (i = 20000; i; i--) { /* 1 sec? */
320 sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
321 /* XXX Need to save this in the interrupt handler? */
322 if (sts & BHA_INTR_HACC)
323 break;
324 delay(50);
325 }
326 if (!i) {
327 printf("%s: bha_cmd, host not finished(0x%x)\n",
328 name, sts);
329 return (1);
330 }
331 }
332 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
333 return (0);
334
335 bad:
336 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
337 return (1);
338 }
339
340 /*
341 * Attach all the sub-devices we can find
342 */
343 void
344 bha_attach(sc, bpd)
345 struct bha_softc *sc;
346 struct bha_probe_data *bpd;
347 {
348
349 /*
350 * fill in the prototype scsipi_link.
351 */
352 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
353 sc->sc_link.adapter_softc = sc;
354 sc->sc_link.scsipi_scsi.adapter_target = bpd->sc_scsi_dev;
355 sc->sc_link.adapter = &bha_switch;
356 sc->sc_link.device = &bha_dev;
357 sc->sc_link.openings = 4;
358 sc->sc_link.scsipi_scsi.max_target = bpd->sc_iswide ? 15 : 7;
359 sc->sc_link.type = BUS_SCSI;
360
361 TAILQ_INIT(&sc->sc_free_ccb);
362 TAILQ_INIT(&sc->sc_waiting_ccb);
363 LIST_INIT(&sc->sc_queue);
364
365 bha_inquire_setup_information(sc);
366
367 printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
368 sc->sc_model, sc->sc_firmware);
369
370 bha_init(sc);
371
372 /*
373 * ask the adapter what subunits are present
374 */
375 config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
376 }
377
378 integrate void
379 bha_finish_ccbs(sc)
380 struct bha_softc *sc;
381 {
382 struct bha_mbx_in *wmbi;
383 struct bha_ccb *ccb;
384 int i;
385
386 wmbi = wmbx->tmbi;
387
388 if (wmbi->stat == BHA_MBI_FREE) {
389 for (i = 0; i < BHA_MBX_SIZE; i++) {
390 if (wmbi->stat != BHA_MBI_FREE) {
391 printf("%s: mbi not in round-robin order\n",
392 sc->sc_dev.dv_xname);
393 goto AGAIN;
394 }
395 bha_nextmbx(wmbi, wmbx, mbi);
396 }
397 #ifdef BHADIAGnot
398 printf("%s: mbi interrupt with no full mailboxes\n",
399 sc->sc_dev.dv_xname);
400 #endif
401 return;
402 }
403
404 AGAIN:
405 do {
406 ccb = bha_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
407 if (!ccb) {
408 printf("%s: bad mbi ccb pointer; skipping\n",
409 sc->sc_dev.dv_xname);
410 goto next;
411 }
412
413 #ifdef BHADEBUG
414 if (bha_debug) {
415 u_char *cp = &ccb->scsi_cmd;
416 printf("op=%x %x %x %x %x %x\n",
417 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
418 printf("stat %x for mbi addr = 0x%08x, ",
419 wmbi->stat, wmbi);
420 printf("ccb addr = 0x%x\n", ccb);
421 }
422 #endif /* BHADEBUG */
423
424 switch (wmbi->stat) {
425 case BHA_MBI_OK:
426 case BHA_MBI_ERROR:
427 if ((ccb->flags & CCB_ABORT) != 0) {
428 /*
429 * If we already started an abort, wait for it
430 * to complete before clearing the CCB. We
431 * could instead just clear CCB_SENDING, but
432 * what if the mailbox was already received?
433 * The worst that happens here is that we clear
434 * the CCB a bit later than we need to. BFD.
435 */
436 goto next;
437 }
438 break;
439
440 case BHA_MBI_ABORT:
441 case BHA_MBI_UNKNOWN:
442 /*
443 * Even if the CCB wasn't found, we clear it anyway.
444 * See preceeding comment.
445 */
446 break;
447
448 default:
449 printf("%s: bad mbi status %02x; skipping\n",
450 sc->sc_dev.dv_xname, wmbi->stat);
451 goto next;
452 }
453
454 untimeout(bha_timeout, ccb);
455 bha_done(sc, ccb);
456
457 next:
458 wmbi->stat = BHA_MBI_FREE;
459 bha_nextmbx(wmbi, wmbx, mbi);
460 } while (wmbi->stat != BHA_MBI_FREE);
461
462 wmbx->tmbi = wmbi;
463 }
464
465 /*
466 * Catch an interrupt from the adaptor
467 */
468 int
469 bha_intr(arg)
470 void *arg;
471 {
472 struct bha_softc *sc = arg;
473 bus_space_tag_t iot = sc->sc_iot;
474 bus_space_handle_t ioh = sc->sc_ioh;
475 u_char sts;
476
477 #ifdef BHADEBUG
478 printf("%s: bha_intr ", sc->sc_dev.dv_xname);
479 #endif /* BHADEBUG */
480
481 /*
482 * First acknowlege the interrupt, Then if it's not telling about
483 * a completed operation just return.
484 */
485 sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
486 if ((sts & BHA_INTR_ANYINTR) == 0)
487 return (0);
488 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
489
490 #ifdef BHADIAG
491 /* Make sure we clear CCB_SENDING before finishing a CCB. */
492 bha_collect_mbo(sc);
493 #endif
494
495 /* Mail box out empty? */
496 if (sts & BHA_INTR_MBOA) {
497 struct bha_toggle toggle;
498
499 toggle.cmd.opcode = BHA_MBO_INTR_EN;
500 toggle.cmd.enable = 0;
501 bha_cmd(iot, ioh, sc,
502 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
503 0, (u_char *)0);
504 bha_start_ccbs(sc);
505 }
506
507 /* Mail box in full? */
508 if (sts & BHA_INTR_MBIF)
509 bha_finish_ccbs(sc);
510
511 return (1);
512 }
513
514 integrate void
515 bha_reset_ccb(sc, ccb)
516 struct bha_softc *sc;
517 struct bha_ccb *ccb;
518 {
519
520 ccb->flags = 0;
521 }
522
523 /*
524 * A ccb is put onto the free list.
525 */
526 void
527 bha_free_ccb(sc, ccb)
528 struct bha_softc *sc;
529 struct bha_ccb *ccb;
530 {
531 int s;
532
533 s = splbio();
534
535 bha_reset_ccb(sc, ccb);
536 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
537
538 /*
539 * If there were none, wake anybody waiting for one to come free,
540 * starting with queued entries.
541 */
542 if (ccb->chain.tqe_next == 0)
543 wakeup(&sc->sc_free_ccb);
544
545 splx(s);
546 }
547
548 integrate int
549 bha_init_ccb(sc, ccb)
550 struct bha_softc *sc;
551 struct bha_ccb *ccb;
552 {
553 bus_dma_tag_t dmat = sc->sc_dmat;
554 int hashnum, error;
555
556 /*
557 * XXX Should we put a DIAGNOSTIC check for multiple
558 * XXX CCB inits here?
559 */
560
561 bzero(ccb, sizeof(struct bha_ccb));
562
563 /*
564 * Create DMA maps for this CCB.
565 */
566 error = bus_dmamap_create(dmat, sizeof(struct bha_ccb), 1,
567 sizeof(struct bha_ccb), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
568 &ccb->dmamap_self);
569 if (error) {
570 printf("%s: can't create ccb dmamap_self\n",
571 sc->sc_dev.dv_xname);
572 return (error);
573 }
574
575 error = bus_dmamap_create(dmat, BHA_MAXXFER, BHA_NSEG, BHA_MAXXFER,
576 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
577 &ccb->dmamap_xfer);
578 if (error) {
579 printf("%s: can't create ccb dmamap_xfer\n",
580 sc->sc_dev.dv_xname);
581 bus_dmamap_destroy(dmat, ccb->dmamap_self);
582 return (error);
583 }
584
585 /*
586 * Load the permanent DMA maps.
587 */
588 error = bus_dmamap_load(dmat, ccb->dmamap_self, ccb,
589 sizeof(struct bha_ccb), NULL, BUS_DMA_NOWAIT);
590 if (error) {
591 printf("%s: can't load ccb dmamap_self\n",
592 sc->sc_dev.dv_xname);
593 bus_dmamap_destroy(dmat, ccb->dmamap_self);
594 bus_dmamap_destroy(dmat, ccb->dmamap_xfer);
595 return (error);
596 }
597
598 /*
599 * put in the phystokv hash table
600 * Never gets taken out.
601 */
602 ccb->hashkey = ccb->dmamap_self->dm_segs[0].ds_addr;
603 hashnum = CCB_HASH(ccb->hashkey);
604 ccb->nexthash = sc->sc_ccbhash[hashnum];
605 sc->sc_ccbhash[hashnum] = ccb;
606 bha_reset_ccb(sc, ccb);
607 return (0);
608 }
609
610 /*
611 * Create a set of ccbs and add them to the free list.
612 */
613 int
614 bha_create_ccbs(sc, mem, size, max_ccbs)
615 struct bha_softc *sc;
616 void *mem;
617 size_t size;
618 int max_ccbs;
619 {
620 bus_dma_segment_t seg;
621 struct bha_ccb *ccb;
622 int rseg, error;
623
624 if (sc->sc_numccbs >= BHA_CCB_MAX)
625 return (0);
626
627 if (max_ccbs > BHA_CCB_MAX)
628 max_ccbs = BHA_CCB_MAX;
629
630 if ((ccb = mem) != NULL)
631 goto have_mem;
632
633 size = NBPG;
634 error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1, &rseg,
635 BUS_DMA_NOWAIT);
636 if (error) {
637 printf("%s: can't allocate memory for ccbs\n",
638 sc->sc_dev.dv_xname);
639 return (error);
640 }
641
642 error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
643 (caddr_t *)&ccb, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
644 if (error) {
645 printf("%s: can't map memory for ccbs\n",
646 sc->sc_dev.dv_xname);
647 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
648 return (error);
649 }
650
651 have_mem:
652 bzero(ccb, size);
653 while (size > sizeof(struct bha_ccb) && sc->sc_numccbs < max_ccbs) {
654 error = bha_init_ccb(sc, ccb);
655 if (error) {
656 printf("%s: can't initialize ccb\n",
657 sc->sc_dev.dv_xname);
658 return (error);
659 }
660 TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
661 (caddr_t)ccb += ALIGN(sizeof(struct bha_ccb));
662 size -= ALIGN(sizeof(struct bha_ccb));
663 sc->sc_numccbs++;
664 }
665
666 return (0);
667 }
668
669 /*
670 * Get a free ccb
671 *
672 * If there are none, see if we can allocate a new one. If so, put it in
673 * the hash table too otherwise either return an error or sleep.
674 */
675 struct bha_ccb *
676 bha_get_ccb(sc, flags)
677 struct bha_softc *sc;
678 int flags;
679 {
680 struct bha_ccb *ccb;
681 int s;
682
683 s = splbio();
684
685 /*
686 * If we can and have to, sleep waiting for one to come free
687 * but only if we can't allocate a new one.
688 */
689 for (;;) {
690 ccb = sc->sc_free_ccb.tqh_first;
691 if (ccb) {
692 TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
693 break;
694 }
695 if (sc->sc_numccbs < BHA_CCB_MAX) {
696 /*
697 * bha_create_ccbs() might have managed to create
698 * one before it failed. If so, don't abort,
699 * just grab it and continue to hobble along.
700 */
701 if (bha_create_ccbs(sc, NULL, 0, BHA_CCB_MAX) != 0 &&
702 sc->sc_free_ccb.tqh_first == NULL) {
703 printf("%s: can't allocate ccbs\n",
704 sc->sc_dev.dv_xname);
705 goto out;
706 }
707 continue;
708 }
709 if ((flags & SCSI_NOSLEEP) != 0)
710 goto out;
711 tsleep(&sc->sc_free_ccb, PRIBIO, "bhaccb", 0);
712 }
713
714 ccb->flags |= CCB_ALLOC;
715
716 out:
717 splx(s);
718 return (ccb);
719 }
720
721 /*
722 * Given a physical address, find the ccb that it corresponds to.
723 */
724 struct bha_ccb *
725 bha_ccb_phys_kv(sc, ccb_phys)
726 struct bha_softc *sc;
727 u_long ccb_phys;
728 {
729 int hashnum = CCB_HASH(ccb_phys);
730 struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
731
732 while (ccb) {
733 if (ccb->hashkey == ccb_phys)
734 break;
735 ccb = ccb->nexthash;
736 }
737 return (ccb);
738 }
739
740 /*
741 * Queue a CCB to be sent to the controller, and send it if possible.
742 */
743 void
744 bha_queue_ccb(sc, ccb)
745 struct bha_softc *sc;
746 struct bha_ccb *ccb;
747 {
748
749 TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
750 bha_start_ccbs(sc);
751 }
752
753 /*
754 * Garbage collect mailboxes that are no longer in use.
755 */
756 void
757 bha_collect_mbo(sc)
758 struct bha_softc *sc;
759 {
760 struct bha_mbx_out *wmbo; /* Mail Box Out pointer */
761 #ifdef BHADIAG
762 struct bha_ccb *ccb;
763 #endif
764
765 wmbo = wmbx->cmbo;
766
767 while (sc->sc_mbofull > 0) {
768 if (wmbo->cmd != BHA_MBO_FREE)
769 break;
770
771 #ifdef BHADIAG
772 ccb = bha_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
773 ccb->flags &= ~CCB_SENDING;
774 #endif
775
776 --sc->sc_mbofull;
777 bha_nextmbx(wmbo, wmbx, mbo);
778 }
779
780 wmbx->cmbo = wmbo;
781 }
782
783 /*
784 * Send as many CCBs as we have empty mailboxes for.
785 */
786 void
787 bha_start_ccbs(sc)
788 struct bha_softc *sc;
789 {
790 bus_space_tag_t iot = sc->sc_iot;
791 bus_space_handle_t ioh = sc->sc_ioh;
792 struct bha_mbx_out *wmbo; /* Mail Box Out pointer */
793 struct bha_ccb *ccb;
794
795 wmbo = wmbx->tmbo;
796
797 while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
798 if (sc->sc_mbofull >= BHA_MBX_SIZE) {
799 bha_collect_mbo(sc);
800 if (sc->sc_mbofull >= BHA_MBX_SIZE) {
801 struct bha_toggle toggle;
802
803 toggle.cmd.opcode = BHA_MBO_INTR_EN;
804 toggle.cmd.enable = 1;
805 bha_cmd(iot, ioh, sc,
806 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
807 0, (u_char *)0);
808 break;
809 }
810 }
811
812 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
813 #ifdef BHADIAG
814 ccb->flags |= CCB_SENDING;
815 #endif
816
817 /* Link ccb to mbo. */
818 ltophys(ccb->dmamap_self->dm_segs[0].ds_addr, wmbo->ccb_addr);
819 if (ccb->flags & CCB_ABORT)
820 wmbo->cmd = BHA_MBO_ABORT;
821 else
822 wmbo->cmd = BHA_MBO_START;
823
824 /* Tell the card to poll immediately. */
825 bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
826
827 if ((ccb->xs->flags & SCSI_POLL) == 0)
828 timeout(bha_timeout, ccb, (ccb->timeout * hz) / 1000);
829
830 ++sc->sc_mbofull;
831 bha_nextmbx(wmbo, wmbx, mbo);
832 }
833
834 wmbx->tmbo = wmbo;
835 }
836
837 /*
838 * We have a ccb which has been processed by the
839 * adaptor, now we look to see how the operation
840 * went. Wake up the owner if waiting
841 */
842 void
843 bha_done(sc, ccb)
844 struct bha_softc *sc;
845 struct bha_ccb *ccb;
846 {
847 bus_dma_tag_t dmat = sc->sc_dmat;
848 struct scsipi_sense_data *s1, *s2;
849 struct scsipi_xfer *xs = ccb->xs;
850
851 SC_DEBUG(xs->sc_link, SDEV_DB2, ("bha_done\n"));
852
853 /*
854 * If we were a data transfer, unload the map that described
855 * the data buffer.
856 */
857 if (xs->datalen) {
858 bus_dmamap_sync(dmat, ccb->dmamap_xfer,
859 (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
860 BUS_DMASYNC_POSTWRITE);
861 bus_dmamap_unload(dmat, ccb->dmamap_xfer);
862 }
863
864 /*
865 * Otherwise, put the results of the operation
866 * into the xfer and call whoever started it
867 */
868 #ifdef BHADIAG
869 if (ccb->flags & CCB_SENDING) {
870 printf("%s: exiting ccb still in transit!\n",
871 sc->sc_dev.dv_xname);
872 Debugger();
873 return;
874 }
875 #endif
876 if ((ccb->flags & CCB_ALLOC) == 0) {
877 printf("%s: exiting ccb not allocated!\n",
878 sc->sc_dev.dv_xname);
879 Debugger();
880 return;
881 }
882 if (xs->error == XS_NOERROR) {
883 if (ccb->host_stat != BHA_OK) {
884 switch (ccb->host_stat) {
885 case BHA_SEL_TIMEOUT: /* No response */
886 xs->error = XS_SELTIMEOUT;
887 break;
888 default: /* Other scsi protocol messes */
889 printf("%s: host_stat %x\n",
890 sc->sc_dev.dv_xname, ccb->host_stat);
891 xs->error = XS_DRIVER_STUFFUP;
892 break;
893 }
894 } else if (ccb->target_stat != SCSI_OK) {
895 switch (ccb->target_stat) {
896 case SCSI_CHECK:
897 s1 = &ccb->scsi_sense;
898 s2 = &xs->sense.scsi_sense;
899 *s2 = *s1;
900 xs->error = XS_SENSE;
901 break;
902 case SCSI_BUSY:
903 xs->error = XS_BUSY;
904 break;
905 default:
906 printf("%s: target_stat %x\n",
907 sc->sc_dev.dv_xname, ccb->target_stat);
908 xs->error = XS_DRIVER_STUFFUP;
909 break;
910 }
911 } else
912 xs->resid = 0;
913 }
914 bha_free_ccb(sc, ccb);
915 xs->flags |= ITSDONE;
916 scsipi_done(xs);
917
918 /*
919 * If there are queue entries in the software queue, try to
920 * run the first one. We should be more or less guaranteed
921 * to succeed, since we just freed a CCB.
922 *
923 * NOTE: bha_scsi_cmd() relies on our calling it with
924 * the first entry in the queue.
925 */
926 if ((xs = sc->sc_queue.lh_first) != NULL)
927 (void) bha_scsi_cmd(xs);
928 }
929
930 /*
931 * Find the board and find it's irq/drq
932 */
933 int
934 bha_find(iot, ioh, sc)
935 bus_space_tag_t iot;
936 bus_space_handle_t ioh;
937 struct bha_probe_data *sc;
938 {
939 int i, iswide;
940 u_char sts;
941 struct bha_extended_inquire inquire;
942 struct bha_config config;
943 int irq, drq;
944
945 /* Check something is at the ports we need to access */
946 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
947 if (sts == 0xFF)
948 return (0);
949
950 /*
951 * Reset board, If it doesn't respond, assume
952 * that it's not there.. good for the probe
953 */
954
955 bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
956 BHA_CTRL_HRST | BHA_CTRL_SRST);
957
958 delay(100);
959 for (i = BHA_RESET_TIMEOUT; i; i--) {
960 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
961 if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
962 break;
963 delay(1000);
964 }
965 if (!i) {
966 #ifdef BHADEBUG
967 if (bha_debug)
968 printf("bha_find: No answer from buslogic board\n");
969 #endif /* BHADEBUG */
970 return (0);
971 }
972
973 /*
974 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
975 * interface. The native bha interface is not compatible with
976 * an aha. 1542. We need to ensure that we never match an
977 * Adaptec 1542. We must also avoid sending Adaptec-compatible
978 * commands to a real bha, lest it go into 1542 emulation mode.
979 * (On an indirect bus like ISA, we should always probe for BusLogic
980 * interfaces before Adaptec interfaces).
981 */
982
983 /*
984 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
985 * for an extended-geometry register. The 1542[AB] don't have one.
986 */
987 sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
988 if (sts == 0xFF)
989 return (0);
990
991 /*
992 * Check that we actually know how to use this board.
993 */
994 delay(1000);
995 inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
996 inquire.cmd.len = sizeof(inquire.reply);
997 i = bha_cmd(iot, ioh, (struct bha_softc *)0,
998 sizeof(inquire.cmd), (u_char *)&inquire.cmd,
999 sizeof(inquire.reply), (u_char *)&inquire.reply);
1000
1001 /*
1002 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
1003 * have the extended-geometry register and also respond to
1004 * BHA_INQUIRE_EXTENDED. Make sure we never match such cards,
1005 * by checking the size of the reply is what a BusLogic card returns.
1006 */
1007 if (i) {
1008 #ifdef BHADEBUG
1009 printf("bha_find: board returned %d instead of %d to %s\n",
1010 i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
1011 #endif
1012 return (0);
1013 }
1014
1015 /* OK, we know we've found a buslogic adaptor. */
1016
1017 switch (inquire.reply.bus_type) {
1018 case BHA_BUS_TYPE_24BIT:
1019 case BHA_BUS_TYPE_32BIT:
1020 break;
1021 case BHA_BUS_TYPE_MCA:
1022 /* We don't grok MicroChannel (yet). */
1023 return (0);
1024 default:
1025 printf("bha_find: illegal bus type %c\n",
1026 inquire.reply.bus_type);
1027 return (0);
1028 }
1029
1030 /* Note if we have a wide bus. */
1031 iswide = inquire.reply.scsi_flags & BHA_SCSI_WIDE;
1032
1033 /*
1034 * Assume we have a board at this stage setup dma channel from
1035 * jumpers and save int level
1036 */
1037 delay(1000);
1038 config.cmd.opcode = BHA_INQUIRE_CONFIG;
1039 bha_cmd(iot, ioh, (struct bha_softc *)0,
1040 sizeof(config.cmd), (u_char *)&config.cmd,
1041 sizeof(config.reply), (u_char *)&config.reply);
1042 switch (config.reply.chan) {
1043 case EISADMA:
1044 drq = -1;
1045 break;
1046 case CHAN0:
1047 drq = 0;
1048 break;
1049 case CHAN5:
1050 drq = 5;
1051 break;
1052 case CHAN6:
1053 drq = 6;
1054 break;
1055 case CHAN7:
1056 drq = 7;
1057 break;
1058 default:
1059 printf("bha_find: illegal drq setting %x\n",
1060 config.reply.chan);
1061 return (0);
1062 }
1063
1064 switch (config.reply.intr) {
1065 case INT9:
1066 irq = 9;
1067 break;
1068 case INT10:
1069 irq = 10;
1070 break;
1071 case INT11:
1072 irq = 11;
1073 break;
1074 case INT12:
1075 irq = 12;
1076 break;
1077 case INT14:
1078 irq = 14;
1079 break;
1080 case INT15:
1081 irq = 15;
1082 break;
1083 default:
1084 printf("bha_find: illegal irq setting %x\n",
1085 config.reply.intr);
1086 return (0);
1087 }
1088
1089 /* if we want to fill in softc, do so now */
1090 if (sc != NULL) {
1091 sc->sc_irq = irq;
1092 sc->sc_drq = drq;
1093 sc->sc_scsi_dev = config.reply.scsi_dev;
1094 sc->sc_iswide = iswide;
1095 }
1096
1097 return (1);
1098 }
1099
1100
1101 /*
1102 * Disable the ISA-compatiblity ioports on PCI bha devices,
1103 * to ensure they're not autoconfigured a second time as an ISA bha.
1104 */
1105 int
1106 bha_disable_isacompat(sc)
1107 struct bha_softc *sc;
1108 {
1109 struct bha_isadisable isa_disable;
1110
1111 isa_disable.cmd.opcode = BHA_MODIFY_IOPORT;
1112 isa_disable.cmd.modifier = BHA_IOMODIFY_DISABLE1;
1113 bha_cmd(sc->sc_iot, sc->sc_ioh, sc,
1114 sizeof(isa_disable.cmd), (u_char*)&isa_disable.cmd,
1115 0, (u_char *)0);
1116 return (0);
1117 }
1118
1119
1120 /*
1121 * Start the board, ready for normal operation
1122 */
1123 void
1124 bha_init(sc)
1125 struct bha_softc *sc;
1126 {
1127 bus_space_tag_t iot = sc->sc_iot;
1128 bus_space_handle_t ioh = sc->sc_ioh;
1129 bus_dma_segment_t seg;
1130 struct bha_devices devices;
1131 struct bha_setup setup;
1132 struct bha_mailbox mailbox;
1133 struct bha_period period;
1134 int i, j, initial_ccbs, rlen, rseg;
1135
1136 /* Enable round-robin scheme - appeared at firmware rev. 3.31. */
1137 if (strcmp(sc->sc_firmware, "3.31") >= 0) {
1138 struct bha_toggle toggle;
1139
1140 toggle.cmd.opcode = BHA_ROUND_ROBIN;
1141 toggle.cmd.enable = 1;
1142 bha_cmd(iot, ioh, sc,
1143 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1144 0, (u_char *)0);
1145 }
1146
1147 /*
1148 * Inquire installed devices (to force synchronous negotiation).
1149 */
1150
1151 /*
1152 * Poll targets 0 - 7.
1153 */
1154 devices.cmd.opcode = BHA_INQUIRE_DEVICES;
1155 bha_cmd(iot, ioh, sc,
1156 sizeof(devices.cmd), (u_char *)&devices.cmd,
1157 sizeof(devices.reply), (u_char *)&devices.reply);
1158
1159 /* Count installed units. */
1160 initial_ccbs = 0;
1161 for (i = 0; i < 8; i++) {
1162 for (j = 0; j < 8; j++) {
1163 if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1164 initial_ccbs++;
1165 }
1166 }
1167
1168 /*
1169 * Poll targets 8 - 15 if we have a wide bus.
1170 */
1171 if (ISWIDE(sc)) {
1172 devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
1173 bha_cmd(iot, ioh, sc,
1174 sizeof(devices.cmd), (u_char *)&devices.cmd,
1175 sizeof(devices.reply), (u_char *)&devices.reply);
1176
1177 for (i = 0; i < 8; i++) {
1178 for (j = 0; j < 8; j++) {
1179 if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1180 initial_ccbs++;
1181 }
1182 }
1183 }
1184
1185 initial_ccbs *= sc->sc_link.openings;
1186
1187 /* Obtain setup information from. */
1188 rlen = sizeof(setup.reply) +
1189 (ISWIDE(sc) ? sizeof(setup.reply_w) : 0);
1190 setup.cmd.opcode = BHA_INQUIRE_SETUP;
1191 setup.cmd.len = rlen;
1192 bha_cmd(iot, ioh, sc,
1193 sizeof(setup.cmd), (u_char *)&setup.cmd,
1194 rlen, (u_char *)&setup.reply);
1195
1196 printf("%s: %s, %s\n",
1197 sc->sc_dev.dv_xname,
1198 setup.reply.sync_neg ? "sync" : "async",
1199 setup.reply.parity ? "parity" : "no parity");
1200
1201 for (i = 0; i < 8; i++)
1202 period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
1203 if (ISWIDE(sc)) {
1204 for (i = 0; i < 8; i++)
1205 period.reply_w.period[i] =
1206 setup.reply_w.sync[i].period * 5 + 20;
1207 }
1208
1209 if (sc->sc_firmware[0] >= '3') {
1210 rlen = sizeof(period.reply) +
1211 (ISWIDE(sc) ? sizeof(period.reply_w) : 0);
1212 period.cmd.opcode = BHA_INQUIRE_PERIOD;
1213 period.cmd.len = rlen;
1214 bha_cmd(iot, ioh, sc,
1215 sizeof(period.cmd), (u_char *)&period.cmd,
1216 rlen, (u_char *)&period.reply);
1217 }
1218
1219 for (i = 0; i < 8; i++) {
1220 if (!setup.reply.sync[i].valid ||
1221 (!setup.reply.sync[i].offset &&
1222 !setup.reply.sync[i].period))
1223 continue;
1224 printf("%s targ %d: sync, offset %d, period %dnsec\n",
1225 sc->sc_dev.dv_xname, i,
1226 setup.reply.sync[i].offset, period.reply.period[i] * 10);
1227 }
1228 if (ISWIDE(sc)) {
1229 for (i = 0; i < 8; i++) {
1230 if (!setup.reply_w.sync[i].valid ||
1231 (!setup.reply_w.sync[i].offset &&
1232 !setup.reply_w.sync[i].period))
1233 continue;
1234 printf("%s targ %d: sync, offset %d, period %dnsec\n",
1235 sc->sc_dev.dv_xname, i + 8,
1236 setup.reply_w.sync[i].offset,
1237 period.reply_w.period[i] * 10);
1238 }
1239 }
1240
1241 /*
1242 * Allocate the mailbox.
1243 */
1244 if (bus_dmamem_alloc(sc->sc_dmat, NBPG, NBPG, 0, &seg, 1,
1245 &rseg, BUS_DMA_NOWAIT) ||
1246 bus_dmamem_map(sc->sc_dmat, &seg, rseg, NBPG,
1247 (caddr_t *)&wmbx, BUS_DMA_NOWAIT|BUS_DMA_COHERENT))
1248 panic("bha_init: can't create or map mailbox");
1249
1250 /*
1251 * Since DMA memory allocation is always rounded up to a
1252 * page size, create some ccbs from the leftovers.
1253 */
1254 if (bha_create_ccbs(sc, ((caddr_t)wmbx) +
1255 ALIGN(sizeof(struct bha_mbx)),
1256 NBPG - ALIGN(sizeof(struct bha_mbx)), initial_ccbs))
1257 panic("bha_init: can't create ccbs");
1258
1259 /*
1260 * Create and load the mailbox DMA map.
1261 */
1262 if (bus_dmamap_create(sc->sc_dmat, sizeof(struct bha_mbx), 1,
1263 sizeof(struct bha_mbx), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
1264 &sc->sc_dmamap_mbox) ||
1265 bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox, wmbx,
1266 sizeof(struct bha_mbx), NULL, BUS_DMA_NOWAIT))
1267 panic("bha_init: can't create or load mailbox dma map");
1268
1269 /*
1270 * Set up initial mail box for round-robin operation.
1271 */
1272 for (i = 0; i < BHA_MBX_SIZE; i++) {
1273 wmbx->mbo[i].cmd = BHA_MBO_FREE;
1274 wmbx->mbi[i].stat = BHA_MBI_FREE;
1275 }
1276 wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1277 wmbx->tmbi = &wmbx->mbi[0];
1278 sc->sc_mbofull = 0;
1279
1280 /* Initialize mail box. */
1281 mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
1282 mailbox.cmd.nmbx = BHA_MBX_SIZE;
1283 ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, mailbox.cmd.addr);
1284 bha_cmd(iot, ioh, sc,
1285 sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
1286 0, (u_char *)0);
1287 }
1288
1289 void
1290 bha_inquire_setup_information(sc)
1291 struct bha_softc *sc;
1292 {
1293 bus_space_tag_t iot = sc->sc_iot;
1294 bus_space_handle_t ioh = sc->sc_ioh;
1295 struct bha_model model;
1296 struct bha_revision revision;
1297 struct bha_digit digit;
1298 char *p;
1299
1300 /*
1301 * Get the firmware revision.
1302 */
1303 p = sc->sc_firmware;
1304 revision.cmd.opcode = BHA_INQUIRE_REVISION;
1305 bha_cmd(iot, ioh, sc,
1306 sizeof(revision.cmd), (u_char *)&revision.cmd,
1307 sizeof(revision.reply), (u_char *)&revision.reply);
1308 *p++ = revision.reply.firm_revision;
1309 *p++ = '.';
1310 *p++ = revision.reply.firm_version;
1311 digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
1312 bha_cmd(iot, ioh, sc,
1313 sizeof(digit.cmd), (u_char *)&digit.cmd,
1314 sizeof(digit.reply), (u_char *)&digit.reply);
1315 *p++ = digit.reply.digit;
1316 if (revision.reply.firm_revision >= '3' ||
1317 (revision.reply.firm_revision == '3' &&
1318 revision.reply.firm_version >= '3')) {
1319 digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
1320 bha_cmd(iot, ioh, sc,
1321 sizeof(digit.cmd), (u_char *)&digit.cmd,
1322 sizeof(digit.reply), (u_char *)&digit.reply);
1323 *p++ = digit.reply.digit;
1324 }
1325 while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
1326 p--;
1327 *p = '\0';
1328
1329 /*
1330 * Get the model number.
1331 */
1332 if (revision.reply.firm_revision >= '3') {
1333 p = sc->sc_model;
1334 model.cmd.opcode = BHA_INQUIRE_MODEL;
1335 model.cmd.len = sizeof(model.reply);
1336 bha_cmd(iot, ioh, sc,
1337 sizeof(model.cmd), (u_char *)&model.cmd,
1338 sizeof(model.reply), (u_char *)&model.reply);
1339 *p++ = model.reply.id[0];
1340 *p++ = model.reply.id[1];
1341 *p++ = model.reply.id[2];
1342 *p++ = model.reply.id[3];
1343 while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1344 p--;
1345 *p++ = model.reply.version[0];
1346 *p++ = model.reply.version[1];
1347 while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1348 p--;
1349 *p = '\0';
1350 } else
1351 strcpy(sc->sc_model, "542B");
1352 }
1353
1354 void
1355 bhaminphys(bp)
1356 struct buf *bp;
1357 {
1358
1359 if (bp->b_bcount > BHA_MAXXFER)
1360 bp->b_bcount = BHA_MAXXFER;
1361 minphys(bp);
1362 }
1363
1364 /*
1365 * start a scsi operation given the command and the data address. Also needs
1366 * the unit, target and lu.
1367 */
1368 int
1369 bha_scsi_cmd(xs)
1370 struct scsipi_xfer *xs;
1371 {
1372 struct scsipi_link *sc_link = xs->sc_link;
1373 struct bha_softc *sc = sc_link->adapter_softc;
1374 bus_dma_tag_t dmat = sc->sc_dmat;
1375 struct bha_ccb *ccb;
1376 int error, seg, flags, s;
1377 int fromqueue = 0, dontqueue = 0;
1378
1379 SC_DEBUG(sc_link, SDEV_DB2, ("bha_scsi_cmd\n"));
1380
1381 s = splbio(); /* protect the queue */
1382
1383 /*
1384 * If we're running the queue from bha_done(), we've been
1385 * called with the first queue entry as our argument.
1386 */
1387 if (xs == sc->sc_queue.lh_first) {
1388 xs = bha_dequeue(sc);
1389 fromqueue = 1;
1390 goto get_ccb;
1391 }
1392
1393 /* Polled requests can't be queued for later. */
1394 dontqueue = xs->flags & SCSI_POLL;
1395
1396 /*
1397 * If there are jobs in the queue, run them first.
1398 */
1399 if (sc->sc_queue.lh_first != NULL) {
1400 /*
1401 * If we can't queue, we have to abort, since
1402 * we have to preserve order.
1403 */
1404 if (dontqueue) {
1405 splx(s);
1406 xs->error = XS_DRIVER_STUFFUP;
1407 return (TRY_AGAIN_LATER);
1408 }
1409
1410 /*
1411 * Swap with the first queue entry.
1412 */
1413 bha_enqueue(sc, xs, 0);
1414 xs = bha_dequeue(sc);
1415 fromqueue = 1;
1416 }
1417
1418 get_ccb:
1419 /*
1420 * get a ccb to use. If the transfer
1421 * is from a buf (possibly from interrupt time)
1422 * then we can't allow it to sleep
1423 */
1424 flags = xs->flags;
1425 if ((ccb = bha_get_ccb(sc, flags)) == NULL) {
1426 /*
1427 * If we can't queue, we lose.
1428 */
1429 if (dontqueue) {
1430 splx(s);
1431 xs->error = XS_DRIVER_STUFFUP;
1432 return (TRY_AGAIN_LATER);
1433 }
1434
1435 /*
1436 * Stuff ourselves into the queue, in front
1437 * if we came off in the first place.
1438 */
1439 bha_enqueue(sc, xs, fromqueue);
1440 splx(s);
1441 return (SUCCESSFULLY_QUEUED);
1442 }
1443
1444 splx(s); /* done playing with the queue */
1445
1446 ccb->xs = xs;
1447 ccb->timeout = xs->timeout;
1448
1449 /*
1450 * Put all the arguments for the xfer in the ccb
1451 */
1452 if (flags & SCSI_RESET) {
1453 ccb->opcode = BHA_RESET_CCB;
1454 ccb->scsi_cmd_length = 0;
1455 } else {
1456 /* can't use S/G if zero length */
1457 ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
1458 : BHA_INITIATOR_CCB);
1459 bcopy(xs->cmd, &ccb->scsi_cmd,
1460 ccb->scsi_cmd_length = xs->cmdlen);
1461 }
1462
1463 if (xs->datalen) {
1464 /*
1465 * Map the DMA transfer.
1466 */
1467 #ifdef TFS
1468 if (flags & SCSI_DATA_UIO) {
1469 error = bus_dmamap_load_uio(dmat,
1470 ccb->dmamap_xfer, (struct uio *)xs->data,
1471 (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1472 BUS_DMA_WAITOK);
1473 } else
1474 #endif /* TFS */
1475 {
1476 error = bus_dmamap_load(dmat,
1477 ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
1478 (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1479 BUS_DMA_WAITOK);
1480 }
1481
1482 if (error) {
1483 if (error == EFBIG) {
1484 printf("%s: bha_scsi_cmd, more than %d"
1485 " dma segments\n",
1486 sc->sc_dev.dv_xname, BHA_NSEG);
1487 } else {
1488 printf("%s: bha_scsi_cmd, error %d loading"
1489 " dma map\n",
1490 sc->sc_dev.dv_xname, error);
1491 }
1492 goto bad;
1493 }
1494
1495 bus_dmamap_sync(dmat, ccb->dmamap_xfer,
1496 (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
1497 BUS_DMASYNC_PREWRITE);
1498
1499 /*
1500 * Load the hardware scatter/gather map with the
1501 * contents of the DMA map.
1502 */
1503 for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
1504 ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
1505 ccb->scat_gath[seg].seg_addr);
1506 ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
1507 ccb->scat_gath[seg].seg_len);
1508 }
1509
1510 ltophys(ccb->dmamap_self->dm_segs[0].ds_addr +
1511 offsetof(struct bha_ccb, scat_gath), ccb->data_addr);
1512 ltophys(ccb->dmamap_xfer->dm_nsegs *
1513 sizeof(struct bha_scat_gath), ccb->data_length);
1514 } else {
1515 /*
1516 * No data xfer, use non S/G values.
1517 */
1518 ltophys(0, ccb->data_addr);
1519 ltophys(0, ccb->data_length);
1520 }
1521
1522 ccb->data_out = 0;
1523 ccb->data_in = 0;
1524 ccb->target = sc_link->scsipi_scsi.target;
1525 ccb->lun = sc_link->scsipi_scsi.lun;
1526 ltophys(ccb->dmamap_self->dm_segs[0].ds_addr +
1527 offsetof(struct bha_ccb, scsi_sense), ccb->sense_ptr);
1528 ccb->req_sense_length = sizeof(ccb->scsi_sense);
1529 ccb->host_stat = 0x00;
1530 ccb->target_stat = 0x00;
1531 ccb->link_id = 0;
1532 ltophys(0, ccb->link_addr);
1533
1534 s = splbio();
1535 bha_queue_ccb(sc, ccb);
1536 splx(s);
1537
1538 /*
1539 * Usually return SUCCESSFULLY QUEUED
1540 */
1541 SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
1542 if ((flags & SCSI_POLL) == 0)
1543 return (SUCCESSFULLY_QUEUED);
1544
1545 /*
1546 * If we can't use interrupts, poll on completion
1547 */
1548 if (bha_poll(sc, xs, ccb->timeout)) {
1549 bha_timeout(ccb);
1550 if (bha_poll(sc, xs, ccb->timeout))
1551 bha_timeout(ccb);
1552 }
1553 return (COMPLETE);
1554
1555 bad:
1556 xs->error = XS_DRIVER_STUFFUP;
1557 bha_free_ccb(sc, ccb);
1558 return (COMPLETE);
1559 }
1560
1561 /*
1562 * Poll a particular unit, looking for a particular xs
1563 */
1564 int
1565 bha_poll(sc, xs, count)
1566 struct bha_softc *sc;
1567 struct scsipi_xfer *xs;
1568 int count;
1569 {
1570 bus_space_tag_t iot = sc->sc_iot;
1571 bus_space_handle_t ioh = sc->sc_ioh;
1572
1573 /* timeouts are in msec, so we loop in 1000 usec cycles */
1574 while (count) {
1575 /*
1576 * If we had interrupts enabled, would we
1577 * have got an interrupt?
1578 */
1579 if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
1580 BHA_INTR_ANYINTR)
1581 bha_intr(sc);
1582 if (xs->flags & ITSDONE)
1583 return (0);
1584 delay(1000); /* only happens in boot so ok */
1585 count--;
1586 }
1587 return (1);
1588 }
1589
1590 void
1591 bha_timeout(arg)
1592 void *arg;
1593 {
1594 struct bha_ccb *ccb = arg;
1595 struct scsipi_xfer *xs = ccb->xs;
1596 struct scsipi_link *sc_link = xs->sc_link;
1597 struct bha_softc *sc = sc_link->adapter_softc;
1598 int s;
1599
1600 scsi_print_addr(sc_link);
1601 printf("timed out");
1602
1603 s = splbio();
1604
1605 #ifdef BHADIAG
1606 /*
1607 * If the ccb's mbx is not free, then the board has gone Far East?
1608 */
1609 bha_collect_mbo(sc);
1610 if (ccb->flags & CCB_SENDING) {
1611 printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1612 Debugger();
1613 }
1614 #endif
1615
1616 /*
1617 * If it has been through before, then
1618 * a previous abort has failed, don't
1619 * try abort again
1620 */
1621 if (ccb->flags & CCB_ABORT) {
1622 /* abort timed out */
1623 printf(" AGAIN\n");
1624 /* XXX Must reset! */
1625 } else {
1626 /* abort the operation that has timed out */
1627 printf("\n");
1628 ccb->xs->error = XS_TIMEOUT;
1629 ccb->timeout = BHA_ABORT_TIMEOUT;
1630 ccb->flags |= CCB_ABORT;
1631 bha_queue_ccb(sc, ccb);
1632 }
1633
1634 splx(s);
1635 }
1636