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