adw.c revision 1.29 1 /* $NetBSD: adw.c,v 1.29 2001/04/25 17:53:29 bouyer Exp $ */
2
3 /*
4 * Generic driver for the Advanced Systems Inc. SCSI controllers
5 *
6 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * Author: Baldassare Dante Profeta <dante (at) mclink.it>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/callout.h>
44 #include <sys/kernel.h>
45 #include <sys/errno.h>
46 #include <sys/ioctl.h>
47 #include <sys/device.h>
48 #include <sys/malloc.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/user.h>
52
53 #include <machine/bus.h>
54 #include <machine/intr.h>
55
56 #include <uvm/uvm_extern.h>
57
58 #include <dev/scsipi/scsi_all.h>
59 #include <dev/scsipi/scsipi_all.h>
60 #include <dev/scsipi/scsiconf.h>
61
62 #include <dev/ic/adwlib.h>
63 #include <dev/ic/adwmcode.h>
64 #include <dev/ic/adw.h>
65
66 #ifndef DDB
67 #define Debugger() panic("should call debugger here (adw.c)")
68 #endif /* ! DDB */
69
70 /******************************************************************************/
71
72
73 static int adw_alloc_controls __P((ADW_SOFTC *));
74 static int adw_alloc_carriers __P((ADW_SOFTC *));
75 static int adw_create_ccbs __P((ADW_SOFTC *, ADW_CCB *, int));
76 static void adw_free_ccb __P((ADW_SOFTC *, ADW_CCB *));
77 static void adw_reset_ccb __P((ADW_CCB *));
78 static int adw_init_ccb __P((ADW_SOFTC *, ADW_CCB *));
79 static ADW_CCB *adw_get_ccb __P((ADW_SOFTC *));
80 static int adw_queue_ccb __P((ADW_SOFTC *, ADW_CCB *));
81
82 static void adw_scsipi_request __P((struct scsipi_channel *,
83 scsipi_adapter_req_t, void *));
84 static int adw_build_req __P((ADW_SOFTC *, ADW_CCB *));
85 static void adw_build_sglist __P((ADW_CCB *, ADW_SCSI_REQ_Q *, ADW_SG_BLOCK *));
86 static void adwminphys __P((struct buf *));
87 static void adw_isr_callback __P((ADW_SOFTC *, ADW_SCSI_REQ_Q *));
88 static void adw_async_callback __P((ADW_SOFTC *, u_int8_t));
89
90 static void adw_print_info __P((ADW_SOFTC *, int));
91
92 static int adw_poll __P((ADW_SOFTC *, struct scsipi_xfer *, int));
93 static void adw_timeout __P((void *));
94 static void adw_reset_bus __P((ADW_SOFTC *));
95
96
97 /******************************************************************************/
98 /* DMA Mapping for Control Blocks */
99 /******************************************************************************/
100
101
102 static int
103 adw_alloc_controls(sc)
104 ADW_SOFTC *sc;
105 {
106 bus_dma_segment_t seg;
107 int error, rseg;
108
109 /*
110 * Allocate the control structure.
111 */
112 if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adw_control),
113 PAGE_SIZE, 0, &seg, 1, &rseg,
114 BUS_DMA_NOWAIT)) != 0) {
115 printf("%s: unable to allocate control structures,"
116 " error = %d\n", sc->sc_dev.dv_xname, error);
117 return (error);
118 }
119 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
120 sizeof(struct adw_control), (caddr_t *) & sc->sc_control,
121 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
122 printf("%s: unable to map control structures, error = %d\n",
123 sc->sc_dev.dv_xname, error);
124 return (error);
125 }
126
127 /*
128 * Create and load the DMA map used for the control blocks.
129 */
130 if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adw_control),
131 1, sizeof(struct adw_control), 0, BUS_DMA_NOWAIT,
132 &sc->sc_dmamap_control)) != 0) {
133 printf("%s: unable to create control DMA map, error = %d\n",
134 sc->sc_dev.dv_xname, error);
135 return (error);
136 }
137 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
138 sc->sc_control, sizeof(struct adw_control), NULL,
139 BUS_DMA_NOWAIT)) != 0) {
140 printf("%s: unable to load control DMA map, error = %d\n",
141 sc->sc_dev.dv_xname, error);
142 return (error);
143 }
144
145 return (0);
146 }
147
148
149 static int
150 adw_alloc_carriers(sc)
151 ADW_SOFTC *sc;
152 {
153 bus_dma_segment_t seg;
154 int error, rseg;
155
156 /*
157 * Allocate the control structure.
158 */
159 sc->sc_control->carriers = malloc(sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
160 M_DEVBUF, M_WAITOK);
161 if(!sc->sc_control->carriers) {
162 printf("%s: malloc() failed in allocating carrier structures\n",
163 sc->sc_dev.dv_xname);
164 return (ENOMEM);
165 }
166
167 if ((error = bus_dmamem_alloc(sc->sc_dmat,
168 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
169 0x10, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
170 printf("%s: unable to allocate carrier structures,"
171 " error = %d\n", sc->sc_dev.dv_xname, error);
172 return (error);
173 }
174 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
175 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
176 (caddr_t *) &sc->sc_control->carriers,
177 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
178 printf("%s: unable to map carrier structures,"
179 " error = %d\n", sc->sc_dev.dv_xname, error);
180 return (error);
181 }
182
183 /*
184 * Create and load the DMA map used for the control blocks.
185 */
186 if ((error = bus_dmamap_create(sc->sc_dmat,
187 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 1,
188 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 0,BUS_DMA_NOWAIT,
189 &sc->sc_dmamap_carrier)) != 0) {
190 printf("%s: unable to create carriers DMA map,"
191 " error = %d\n", sc->sc_dev.dv_xname, error);
192 return (error);
193 }
194 if ((error = bus_dmamap_load(sc->sc_dmat,
195 sc->sc_dmamap_carrier, sc->sc_control->carriers,
196 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, NULL,
197 BUS_DMA_NOWAIT)) != 0) {
198 printf("%s: unable to load carriers DMA map,"
199 " error = %d\n", sc->sc_dev.dv_xname, error);
200 return (error);
201 }
202
203 return (0);
204 }
205
206
207 /******************************************************************************/
208 /* Control Blocks routines */
209 /******************************************************************************/
210
211
212 /*
213 * Create a set of ccbs and add them to the free list. Called once
214 * by adw_init(). We return the number of CCBs successfully created.
215 */
216 static int
217 adw_create_ccbs(sc, ccbstore, count)
218 ADW_SOFTC *sc;
219 ADW_CCB *ccbstore;
220 int count;
221 {
222 ADW_CCB *ccb;
223 int i, error;
224
225 for (i = 0; i < count; i++) {
226 ccb = &ccbstore[i];
227 if ((error = adw_init_ccb(sc, ccb)) != 0) {
228 printf("%s: unable to initialize ccb, error = %d\n",
229 sc->sc_dev.dv_xname, error);
230 return (i);
231 }
232 TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
233 }
234
235 return (i);
236 }
237
238
239 /*
240 * A ccb is put onto the free list.
241 */
242 static void
243 adw_free_ccb(sc, ccb)
244 ADW_SOFTC *sc;
245 ADW_CCB *ccb;
246 {
247 int s;
248
249 s = splbio();
250
251 adw_reset_ccb(ccb);
252 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
253
254 splx(s);
255 }
256
257
258 static void
259 adw_reset_ccb(ccb)
260 ADW_CCB *ccb;
261 {
262
263 ccb->flags = 0;
264 }
265
266
267 static int
268 adw_init_ccb(sc, ccb)
269 ADW_SOFTC *sc;
270 ADW_CCB *ccb;
271 {
272 int hashnum, error;
273
274 /*
275 * Create the DMA map for this CCB.
276 */
277 error = bus_dmamap_create(sc->sc_dmat,
278 (ADW_MAX_SG_LIST - 1) * PAGE_SIZE,
279 ADW_MAX_SG_LIST, (ADW_MAX_SG_LIST - 1) * PAGE_SIZE,
280 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer);
281 if (error) {
282 printf("%s: unable to create CCB DMA map, error = %d\n",
283 sc->sc_dev.dv_xname, error);
284 return (error);
285 }
286
287 /*
288 * put in the phystokv hash table
289 * Never gets taken out.
290 */
291 ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
292 ADW_CCB_OFF(ccb);
293 hashnum = CCB_HASH(ccb->hashkey);
294 ccb->nexthash = sc->sc_ccbhash[hashnum];
295 sc->sc_ccbhash[hashnum] = ccb;
296 adw_reset_ccb(ccb);
297 return (0);
298 }
299
300
301 /*
302 * Get a free ccb
303 *
304 * If there are none, see if we can allocate a new one
305 */
306 static ADW_CCB *
307 adw_get_ccb(sc)
308 ADW_SOFTC *sc;
309 {
310 ADW_CCB *ccb = 0;
311 int s;
312
313 s = splbio();
314
315 ccb = sc->sc_free_ccb.tqh_first;
316 if (ccb != NULL) {
317 TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
318 ccb->flags |= CCB_ALLOC;
319 }
320 splx(s);
321 return (ccb);
322 }
323
324
325 /*
326 * Given a physical address, find the ccb that it corresponds to.
327 */
328 ADW_CCB *
329 adw_ccb_phys_kv(sc, ccb_phys)
330 ADW_SOFTC *sc;
331 u_int32_t ccb_phys;
332 {
333 int hashnum = CCB_HASH(ccb_phys);
334 ADW_CCB *ccb = sc->sc_ccbhash[hashnum];
335
336 while (ccb) {
337 if (ccb->hashkey == ccb_phys)
338 break;
339 ccb = ccb->nexthash;
340 }
341 return (ccb);
342 }
343
344
345 /*
346 * Queue a CCB to be sent to the controller, and send it if possible.
347 */
348 static int
349 adw_queue_ccb(sc, ccb)
350 ADW_SOFTC *sc;
351 ADW_CCB *ccb;
352 {
353 int errcode = ADW_SUCCESS;
354
355 TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
356
357 while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
358
359 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
360 errcode = AdwExeScsiQueue(sc, &ccb->scsiq);
361 switch(errcode) {
362 case ADW_SUCCESS:
363 break;
364
365 case ADW_BUSY:
366 printf("ADW_BUSY\n");
367 return(ADW_BUSY);
368
369 case ADW_ERROR:
370 printf("ADW_ERROR\n");
371 return(ADW_ERROR);
372 }
373
374 TAILQ_INSERT_TAIL(&sc->sc_pending_ccb, ccb, chain);
375
376 if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
377 callout_reset(&ccb->xs->xs_callout,
378 (ccb->timeout * hz) / 1000, adw_timeout, ccb);
379 }
380
381 return(errcode);
382 }
383
384
385 /******************************************************************************/
386 /* SCSI layer interfacing routines */
387 /******************************************************************************/
388
389
390 int
391 adw_init(sc)
392 ADW_SOFTC *sc;
393 {
394 u_int16_t warn_code;
395
396
397 sc->cfg.lib_version = (ADW_LIB_VERSION_MAJOR << 8) |
398 ADW_LIB_VERSION_MINOR;
399 sc->cfg.chip_version =
400 ADW_GET_CHIP_VERSION(sc->sc_iot, sc->sc_ioh, sc->bus_type);
401
402 /*
403 * Reset the chip to start and allow register writes.
404 */
405 if (ADW_FIND_SIGNATURE(sc->sc_iot, sc->sc_ioh) == 0) {
406 panic("adw_init: adw_find_signature failed");
407 } else {
408 AdwResetChip(sc->sc_iot, sc->sc_ioh);
409
410 warn_code = AdwInitFromEEPROM(sc);
411
412 if (warn_code & ADW_WARN_EEPROM_CHKSUM)
413 printf("%s: Bad checksum found. "
414 "Setting default values\n",
415 sc->sc_dev.dv_xname);
416 if (warn_code & ADW_WARN_EEPROM_TERMINATION)
417 printf("%s: Bad bus termination setting."
418 "Using automatic termination.\n",
419 sc->sc_dev.dv_xname);
420 }
421
422 sc->isr_callback = (ADW_CALLBACK) adw_isr_callback;
423 sc->async_callback = (ADW_CALLBACK) adw_async_callback;
424
425 return 0;
426 }
427
428
429 void
430 adw_attach(sc)
431 ADW_SOFTC *sc;
432 {
433 struct scsipi_adapter *adapt = &sc->sc_adapter;
434 struct scsipi_channel *chan = &sc->sc_channel;
435 int ncontrols, error;
436
437 TAILQ_INIT(&sc->sc_free_ccb);
438 TAILQ_INIT(&sc->sc_waiting_ccb);
439 TAILQ_INIT(&sc->sc_pending_ccb);
440
441 /*
442 * Allocate the Control Blocks.
443 */
444 error = adw_alloc_controls(sc);
445 if (error)
446 return; /* (error) */ ;
447
448 bzero(sc->sc_control, sizeof(struct adw_control));
449
450 /*
451 * Create and initialize the Control Blocks.
452 */
453 ncontrols = adw_create_ccbs(sc, sc->sc_control->ccbs, ADW_MAX_CCB);
454 if (ncontrols == 0) {
455 printf("%s: unable to create Control Blocks\n",
456 sc->sc_dev.dv_xname);
457 return; /* (ENOMEM) */ ;
458 } else if (ncontrols != ADW_MAX_CCB) {
459 printf("%s: WARNING: only %d of %d Control Blocks"
460 " created\n",
461 sc->sc_dev.dv_xname, ncontrols, ADW_MAX_CCB);
462 }
463
464 /*
465 * Create and initialize the Carriers.
466 */
467 error = adw_alloc_carriers(sc);
468 if (error)
469 return; /* (error) */ ;
470
471 /*
472 * Zero's the freeze_device status
473 */
474 bzero(sc->sc_freeze_dev, sizeof(sc->sc_freeze_dev));
475
476 /*
477 * Initialize the adapter
478 */
479 switch (AdwInitDriver(sc)) {
480 case ADW_IERR_BIST_PRE_TEST:
481 panic("%s: BIST pre-test error",
482 sc->sc_dev.dv_xname);
483 break;
484
485 case ADW_IERR_BIST_RAM_TEST:
486 panic("%s: BIST RAM test error",
487 sc->sc_dev.dv_xname);
488 break;
489
490 case ADW_IERR_MCODE_CHKSUM:
491 panic("%s: Microcode checksum error",
492 sc->sc_dev.dv_xname);
493 break;
494
495 case ADW_IERR_ILLEGAL_CONNECTION:
496 panic("%s: All three connectors are in use",
497 sc->sc_dev.dv_xname);
498 break;
499
500 case ADW_IERR_REVERSED_CABLE:
501 panic("%s: Cable is reversed",
502 sc->sc_dev.dv_xname);
503 break;
504
505 case ADW_IERR_HVD_DEVICE:
506 panic("%s: HVD attached to LVD connector",
507 sc->sc_dev.dv_xname);
508 break;
509
510 case ADW_IERR_SINGLE_END_DEVICE:
511 panic("%s: single-ended device is attached to"
512 " one of the connectors",
513 sc->sc_dev.dv_xname);
514 break;
515
516 case ADW_IERR_NO_CARRIER:
517 panic("%s: unable to create Carriers",
518 sc->sc_dev.dv_xname);
519 break;
520
521 case ADW_WARN_BUSRESET_ERROR:
522 printf("%s: WARNING: Bus Reset Error\n",
523 sc->sc_dev.dv_xname);
524 break;
525 }
526
527 /*
528 * Fill in the scsipi_adapter.
529 */
530 memset(adapt, 0, sizeof(*adapt));
531 adapt->adapt_dev = &sc->sc_dev;
532 adapt->adapt_nchannels = 1;
533 adapt->adapt_openings = ncontrols;
534 adapt->adapt_max_periph = adapt->adapt_openings;
535 adapt->adapt_request = adw_scsipi_request;
536 adapt->adapt_minphys = adwminphys;
537
538 /*
539 * Fill in the scsipi_channel.
540 */
541 memset(chan, 0, sizeof(*chan));
542 chan->chan_adapter = adapt;
543 chan->chan_bustype = &scsi_bustype;
544 chan->chan_channel = 0;
545 chan->chan_ntargets = ADW_MAX_TID + 1;
546 chan->chan_nluns = 7;
547 chan->chan_id = sc->chip_scsi_id;
548
549 config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
550 }
551
552
553 static void
554 adwminphys(bp)
555 struct buf *bp;
556 {
557
558 if (bp->b_bcount > ((ADW_MAX_SG_LIST - 1) * PAGE_SIZE))
559 bp->b_bcount = ((ADW_MAX_SG_LIST - 1) * PAGE_SIZE);
560 minphys(bp);
561 }
562
563
564 /*
565 * start a scsi operation given the command and the data address.
566 * Also needs the unit, target and lu.
567 */
568 static void
569 adw_scsipi_request(chan, req, arg)
570 struct scsipi_channel *chan;
571 scsipi_adapter_req_t req;
572 void *arg;
573 {
574 struct scsipi_xfer *xs;
575 ADW_SOFTC *sc = (void *)chan->chan_adapter->adapt_dev;
576 ADW_CCB *ccb;
577 int s, retry;
578
579 switch (req) {
580 case ADAPTER_REQ_RUN_XFER:
581 xs = arg;
582
583 /*
584 * get a ccb to use. If the transfer
585 * is from a buf (possibly from interrupt time)
586 * then we can't allow it to sleep
587 */
588
589 ccb = adw_get_ccb(sc);
590 #ifdef DIAGNOSTIC
591 /*
592 * This should never happen as we track the resources
593 * in the mid-layer.
594 */
595 if (ccb == NULL) {
596 scsipi_printaddr(xs->xs_periph);
597 printf("unable to allocate ccb\n");
598 panic("adw_scsipi_request");
599 }
600 #endif
601
602 ccb->xs = xs;
603 ccb->timeout = xs->timeout;
604
605 if (adw_build_req(sc, ccb)) {
606 s = splbio();
607 retry = adw_queue_ccb(sc, ccb);
608 splx(s);
609
610 switch(retry) {
611 case ADW_BUSY:
612 xs->error = XS_RESOURCE_SHORTAGE;
613 adw_free_ccb(sc, ccb);
614 scsipi_done(xs);
615 return;
616
617 case ADW_ERROR:
618 xs->error = XS_DRIVER_STUFFUP;
619 adw_free_ccb(sc, ccb);
620 scsipi_done(xs);
621 return;
622 }
623 if ((xs->xs_control & XS_CTL_POLL) == 0)
624 return;
625 /*
626 * Not allowed to use interrupts, poll for completion.
627 */
628 if (adw_poll(sc, xs, ccb->timeout)) {
629 adw_timeout(ccb);
630 if (adw_poll(sc, xs, ccb->timeout))
631 adw_timeout(ccb);
632 }
633 }
634 return;
635
636 case ADAPTER_REQ_GROW_RESOURCES:
637 /* XXX Not supported. */
638 return;
639
640 case ADAPTER_REQ_SET_XFER_MODE:
641 /* XXX XXX XXX */
642 return;
643 }
644 }
645
646
647 /*
648 * Build a request structure for the Wide Boards.
649 */
650 static int
651 adw_build_req(sc, ccb)
652 ADW_SOFTC *sc;
653 ADW_CCB *ccb;
654 {
655 struct scsipi_xfer *xs = ccb->xs;
656 struct scsipi_periph *periph = xs->xs_periph;
657 bus_dma_tag_t dmat = sc->sc_dmat;
658 ADW_SCSI_REQ_Q *scsiqp;
659 int error;
660
661 scsiqp = &ccb->scsiq;
662 bzero(scsiqp, sizeof(ADW_SCSI_REQ_Q));
663
664 /*
665 * Set the ADW_SCSI_REQ_Q 'ccb_ptr' to point to the
666 * physical CCB structure.
667 */
668 scsiqp->ccb_ptr = ccb->hashkey;
669
670 /*
671 * Build the ADW_SCSI_REQ_Q request.
672 */
673
674 /*
675 * Set CDB length and copy it to the request structure.
676 * For wide boards a CDB length maximum of 16 bytes
677 * is supported.
678 */
679 bcopy(xs->cmd, &scsiqp->cdb, ((scsiqp->cdb_len = xs->cmdlen) <= 12)?
680 xs->cmdlen : 12 );
681 if(xs->cmdlen > 12)
682 bcopy(&(xs->cmd[12]), &scsiqp->cdb16, xs->cmdlen - 12);
683
684 scsiqp->target_id = periph->periph_target;
685 scsiqp->target_lun = periph->periph_lun;
686
687 scsiqp->vsense_addr = &ccb->scsi_sense;
688 scsiqp->sense_addr = sc->sc_dmamap_control->dm_segs[0].ds_addr +
689 ADW_CCB_OFF(ccb) + offsetof(struct adw_ccb, scsi_sense);
690 scsiqp->sense_len = sizeof(struct scsipi_sense_data);
691
692 /*
693 * Build ADW_SCSI_REQ_Q for a scatter-gather buffer command.
694 */
695 if (xs->datalen) {
696 /*
697 * Map the DMA transfer.
698 */
699 #ifdef TFS
700 if (xs->xs_control & SCSI_DATA_UIO) {
701 error = bus_dmamap_load_uio(dmat,
702 ccb->dmamap_xfer, (struct uio *) xs->data,
703 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
704 BUS_DMA_WAITOK) | BUS_DMA_STREAMING);
705 } else
706 #endif /* TFS */
707 {
708 error = bus_dmamap_load(dmat,
709 ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
710 ((xs->xs_control & XS_CTL_NOSLEEP) ?
711 BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
712 BUS_DMA_STREAMING);
713 }
714
715 switch (error) {
716 case 0:
717 break;
718 case ENOMEM:
719 case EAGAIN:
720 xs->error = XS_RESOURCE_SHORTAGE;
721 goto out_bad;
722
723 default:
724 xs->error = XS_DRIVER_STUFFUP;
725 printf("%s: error %d loading DMA map\n",
726 sc->sc_dev.dv_xname, error);
727 out_bad:
728 adw_free_ccb(sc, ccb);
729 scsipi_done(xs);
730 return(0);
731 }
732
733 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
734 ccb->dmamap_xfer->dm_mapsize,
735 (xs->xs_control & XS_CTL_DATA_IN) ?
736 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
737
738 /*
739 * Build scatter-gather list.
740 */
741 scsiqp->data_cnt = xs->datalen;
742 scsiqp->vdata_addr = xs->data;
743 scsiqp->data_addr = ccb->dmamap_xfer->dm_segs[0].ds_addr;
744 bzero(ccb->sg_block, sizeof(ADW_SG_BLOCK) * ADW_NUM_SG_BLOCK);
745 adw_build_sglist(ccb, scsiqp, ccb->sg_block);
746 } else {
747 /*
748 * No data xfer, use non S/G values.
749 */
750 scsiqp->data_cnt = 0;
751 scsiqp->vdata_addr = 0;
752 scsiqp->data_addr = 0;
753 }
754
755 return (1);
756 }
757
758
759 /*
760 * Build scatter-gather list for Wide Boards.
761 */
762 static void
763 adw_build_sglist(ccb, scsiqp, sg_block)
764 ADW_CCB *ccb;
765 ADW_SCSI_REQ_Q *scsiqp;
766 ADW_SG_BLOCK *sg_block;
767 {
768 u_long sg_block_next_addr; /* block and its next */
769 u_int32_t sg_block_physical_addr;
770 int i; /* how many SG entries */
771 bus_dma_segment_t *sg_list = &ccb->dmamap_xfer->dm_segs[0];
772 int sg_elem_cnt = ccb->dmamap_xfer->dm_nsegs;
773
774
775 sg_block_next_addr = (u_long) sg_block; /* allow math operation */
776 sg_block_physical_addr = ccb->hashkey +
777 offsetof(struct adw_ccb, sg_block[0]);
778 scsiqp->sg_real_addr = sg_block_physical_addr;
779
780 /*
781 * If there are more than NO_OF_SG_PER_BLOCK dma segments (hw sg-list)
782 * then split the request into multiple sg-list blocks.
783 */
784
785 do {
786 for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) {
787 sg_block->sg_list[i].sg_addr = sg_list->ds_addr;
788 sg_block->sg_list[i].sg_count = sg_list->ds_len;
789
790 if (--sg_elem_cnt == 0) {
791 /* last entry, get out */
792 sg_block->sg_cnt = i + 1;
793 sg_block->sg_ptr = NULL; /* next link = NULL */
794 return;
795 }
796 sg_list++;
797 }
798 sg_block_next_addr += sizeof(ADW_SG_BLOCK);
799 sg_block_physical_addr += sizeof(ADW_SG_BLOCK);
800
801 sg_block->sg_cnt = NO_OF_SG_PER_BLOCK;
802 sg_block->sg_ptr = sg_block_physical_addr;
803 sg_block = (ADW_SG_BLOCK *) sg_block_next_addr; /* virt. addr */
804 } while (1);
805 }
806
807
808 /******************************************************************************/
809 /* Interrupts and TimeOut routines */
810 /******************************************************************************/
811
812
813 int
814 adw_intr(arg)
815 void *arg;
816 {
817 ADW_SOFTC *sc = arg;
818
819
820 if(AdwISR(sc) != ADW_FALSE) {
821 return (1);
822 }
823
824 return (0);
825 }
826
827
828 /*
829 * Poll a particular unit, looking for a particular xs
830 */
831 static int
832 adw_poll(sc, xs, count)
833 ADW_SOFTC *sc;
834 struct scsipi_xfer *xs;
835 int count;
836 {
837
838 /* timeouts are in msec, so we loop in 1000 usec cycles */
839 while (count) {
840 adw_intr(sc);
841 if (xs->xs_status & XS_STS_DONE)
842 return (0);
843 delay(1000); /* only happens in boot so ok */
844 count--;
845 }
846 return (1);
847 }
848
849
850 static void
851 adw_timeout(arg)
852 void *arg;
853 {
854 ADW_CCB *ccb = arg;
855 struct scsipi_xfer *xs = ccb->xs;
856 struct scsipi_periph *periph = xs->xs_periph;
857 ADW_SOFTC *sc =
858 (void *)periph->periph_channel->chan_adapter->adapt_dev;
859 int s;
860
861 scsipi_printaddr(periph);
862 printf("timed out");
863
864 s = splbio();
865
866 if (ccb->flags & CCB_ABORTED) {
867 /*
868 * Abort Timed Out
869 *
870 * No more opportunities. Lets try resetting the bus and
871 * reinitialize the host adapter.
872 */
873 callout_stop(&xs->xs_callout);
874 printf(" AGAIN. Resetting SCSI Bus\n");
875 adw_reset_bus(sc);
876 splx(s);
877 return;
878 } else if (ccb->flags & CCB_ABORTING) {
879 /*
880 * Abort the operation that has timed out.
881 *
882 * Second opportunity.
883 */
884 printf("\n");
885 xs->error = XS_TIMEOUT;
886 ccb->flags |= CCB_ABORTED;
887 #if 0
888 /*
889 * - XXX - 3.3a microcode is BROKEN!!!
890 *
891 * We cannot abort a CCB, so we can only hope the command
892 * get completed before the next timeout, otherwise a
893 * Bus Reset will arrive inexorably.
894 */
895 /*
896 * ADW_ABORT_CCB() makes the board to generate an interrupt
897 *
898 * - XXX - The above assertion MUST be verified (and this
899 * code changed as well [callout_*()]), when the
900 * ADW_ABORT_CCB will be working again
901 */
902 ADW_ABORT_CCB(sc, ccb);
903 #endif
904 /*
905 * waiting for multishot callout_reset() let's restart it
906 * by hand so the next time a timeout event will occour
907 * we will reset the bus.
908 */
909 callout_reset(&xs->xs_callout,
910 (ccb->timeout * hz) / 1000, adw_timeout, ccb);
911 } else {
912 /*
913 * Abort the operation that has timed out.
914 *
915 * First opportunity.
916 */
917 printf("\n");
918 xs->error = XS_TIMEOUT;
919 ccb->flags |= CCB_ABORTING;
920 #if 0
921 /*
922 * - XXX - 3.3a microcode is BROKEN!!!
923 *
924 * We cannot abort a CCB, so we can only hope the command
925 * get completed before the next 2 timeout, otherwise a
926 * Bus Reset will arrive inexorably.
927 */
928 /*
929 * ADW_ABORT_CCB() makes the board to generate an interrupt
930 *
931 * - XXX - The above assertion MUST be verified (and this
932 * code changed as well [callout_*()]), when the
933 * ADW_ABORT_CCB will be working again
934 */
935 ADW_ABORT_CCB(sc, ccb);
936 #endif
937 /*
938 * waiting for multishot callout_reset() let's restart it
939 * by hand so to give a second opportunity to the command
940 * which timed-out.
941 */
942 callout_reset(&xs->xs_callout,
943 (ccb->timeout * hz) / 1000, adw_timeout, ccb);
944 }
945
946 splx(s);
947 }
948
949
950 static void
951 adw_reset_bus(sc)
952 ADW_SOFTC *sc;
953 {
954 ADW_CCB *ccb;
955 int s;
956 struct scsipi_xfer *xs;
957
958 s = splbio();
959 AdwResetSCSIBus(sc);
960 while((ccb = TAILQ_LAST(&sc->sc_pending_ccb,
961 adw_pending_ccb)) != NULL) {
962 callout_stop(&ccb->xs->xs_callout);
963 TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
964 xs = ccb->xs;
965 adw_free_ccb(sc, ccb);
966 xs->error = XS_RESOURCE_SHORTAGE;
967 scsipi_done(xs);
968 }
969 splx(s);
970 }
971
972
973 /******************************************************************************/
974 /* Host Adapter and Peripherals Information Routines */
975 /******************************************************************************/
976
977
978 static void
979 adw_print_info(sc, tid)
980 ADW_SOFTC *sc;
981 int tid;
982 {
983 bus_space_tag_t iot = sc->sc_iot;
984 bus_space_handle_t ioh = sc->sc_ioh;
985 u_int16_t wdtr_able, wdtr_done, wdtr;
986 u_int16_t sdtr_able, sdtr_done, sdtr, period;
987 static int wdtr_reneg = 0, sdtr_reneg = 0;
988
989 if (tid == 0){
990 wdtr_reneg = sdtr_reneg = 0;
991 }
992
993 printf("%s: target %d ", sc->sc_dev.dv_xname, tid);
994
995 ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, wdtr_able);
996 if(wdtr_able & ADW_TID_TO_TIDMASK(tid)) {
997 ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE, wdtr_done);
998 ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_DEVICE_HSHK_CFG_TABLE +
999 (2 * tid), wdtr);
1000 printf("using %d-bits wide, ", (wdtr & 0x8000)? 16 : 8);
1001 if((wdtr_done & ADW_TID_TO_TIDMASK(tid)) == 0)
1002 wdtr_reneg = 1;
1003 } else {
1004 printf("wide transfers disabled, ");
1005 }
1006
1007 ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
1008 if(sdtr_able & ADW_TID_TO_TIDMASK(tid)) {
1009 ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE, sdtr_done);
1010 ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_DEVICE_HSHK_CFG_TABLE +
1011 (2 * tid), sdtr);
1012 sdtr &= ~0x8000;
1013 if((sdtr & 0x1F) != 0) {
1014 if((sdtr & 0x1F00) == 0x1100){
1015 printf("80.0 MHz");
1016 } else if((sdtr & 0x1F00) == 0x1000){
1017 printf("40.0 MHz");
1018 } else {
1019 /* <= 20.0 MHz */
1020 period = (((sdtr >> 8) * 25) + 50)/4;
1021 if(period == 0) {
1022 /* Should never happen. */
1023 printf("? MHz");
1024 } else {
1025 printf("%d.%d MHz", 250/period,
1026 ADW_TENTHS(250, period));
1027 }
1028 }
1029 printf(" synchronous transfers\n");
1030 } else {
1031 printf("asynchronous transfers\n");
1032 }
1033 if((sdtr_done & ADW_TID_TO_TIDMASK(tid)) == 0)
1034 sdtr_reneg = 1;
1035 } else {
1036 printf("synchronous transfers disabled\n");
1037 }
1038
1039 if(wdtr_reneg || sdtr_reneg) {
1040 printf("%s: target %d %s", sc->sc_dev.dv_xname, tid,
1041 (wdtr_reneg)? ((sdtr_reneg)? "wide/sync" : "wide") :
1042 ((sdtr_reneg)? "sync" : "") );
1043 printf(" renegotiation pending before next command.\n");
1044 }
1045 }
1046
1047
1048 /******************************************************************************/
1049 /* WIDE boards Interrupt callbacks */
1050 /******************************************************************************/
1051
1052
1053 /*
1054 * adw_isr_callback() - Second Level Interrupt Handler called by AdwISR()
1055 *
1056 * Interrupt callback function for the Wide SCSI Adv Library.
1057 *
1058 * Notice:
1059 * Interrupts are disabled by the caller (AdwISR() function), and will be
1060 * enabled at the end of the caller.
1061 */
1062 static void
1063 adw_isr_callback(sc, scsiq)
1064 ADW_SOFTC *sc;
1065 ADW_SCSI_REQ_Q *scsiq;
1066 {
1067 bus_dma_tag_t dmat = sc->sc_dmat;
1068 ADW_CCB *ccb;
1069 struct scsipi_xfer *xs;
1070 struct scsipi_sense_data *s1, *s2;
1071
1072
1073 ccb = adw_ccb_phys_kv(sc, scsiq->ccb_ptr);
1074
1075 callout_stop(&ccb->xs->xs_callout);
1076
1077 xs = ccb->xs;
1078
1079 /*
1080 * If we were a data transfer, unload the map that described
1081 * the data buffer.
1082 */
1083 if (xs->datalen) {
1084 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
1085 ccb->dmamap_xfer->dm_mapsize,
1086 (xs->xs_control & XS_CTL_DATA_IN) ?
1087 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1088 bus_dmamap_unload(dmat, ccb->dmamap_xfer);
1089 }
1090
1091 if ((ccb->flags & CCB_ALLOC) == 0) {
1092 printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
1093 Debugger();
1094 return;
1095 }
1096
1097 /*
1098 * 'done_status' contains the command's ending status.
1099 * 'host_status' conatins the host adapter status.
1100 * 'scsi_status' contains the scsi peripheral status.
1101 */
1102 if ((scsiq->host_status == QHSTA_NO_ERROR) &&
1103 ((scsiq->done_status == QD_NO_ERROR) ||
1104 (scsiq->done_status == QD_WITH_ERROR))) {
1105 switch (scsiq->host_status) {
1106 case SCSI_STATUS_GOOD:
1107 if ((scsiq->cdb[0] == INQUIRY) &&
1108 (scsiq->target_lun == 0)) {
1109 adw_print_info(sc, scsiq->target_id);
1110 }
1111 xs->error = XS_NOERROR;
1112 xs->resid = scsiq->data_cnt;
1113 sc->sc_freeze_dev[scsiq->target_id] = 0;
1114 break;
1115
1116 case SCSI_STATUS_CHECK_CONDITION:
1117 case SCSI_STATUS_CMD_TERMINATED:
1118 s1 = &ccb->scsi_sense;
1119 s2 = &xs->sense.scsi_sense;
1120 *s2 = *s1;
1121 xs->error = XS_SENSE;
1122 sc->sc_freeze_dev[scsiq->target_id] = 1;
1123 break;
1124
1125 default:
1126 xs->error = XS_BUSY;
1127 sc->sc_freeze_dev[scsiq->target_id] = 1;
1128 break;
1129 }
1130 } else if (scsiq->done_status == QD_ABORTED_BY_HOST) {
1131 xs->error = XS_DRIVER_STUFFUP;
1132 } else {
1133 switch (scsiq->host_status) {
1134 case QHSTA_M_SEL_TIMEOUT:
1135 xs->error = XS_SELTIMEOUT;
1136 break;
1137
1138 case QHSTA_M_SXFR_OFF_UFLW:
1139 case QHSTA_M_SXFR_OFF_OFLW:
1140 case QHSTA_M_DATA_OVER_RUN:
1141 printf("%s: Overrun/Overflow/Underflow condition\n",
1142 sc->sc_dev.dv_xname);
1143 xs->error = XS_DRIVER_STUFFUP;
1144 break;
1145
1146 case QHSTA_M_SXFR_DESELECTED:
1147 case QHSTA_M_UNEXPECTED_BUS_FREE:
1148 printf("%s: Unexpected BUS free\n",sc->sc_dev.dv_xname);
1149 xs->error = XS_DRIVER_STUFFUP;
1150 break;
1151
1152 case QHSTA_M_SCSI_BUS_RESET:
1153 case QHSTA_M_SCSI_BUS_RESET_UNSOL:
1154 printf("%s: BUS Reset\n", sc->sc_dev.dv_xname);
1155 xs->error = XS_DRIVER_STUFFUP;
1156 break;
1157
1158 case QHSTA_M_BUS_DEVICE_RESET:
1159 printf("%s: Device Reset\n", sc->sc_dev.dv_xname);
1160 xs->error = XS_DRIVER_STUFFUP;
1161 break;
1162
1163 case QHSTA_M_QUEUE_ABORTED:
1164 printf("%s: Queue Aborted\n", sc->sc_dev.dv_xname);
1165 xs->error = XS_DRIVER_STUFFUP;
1166 break;
1167
1168 case QHSTA_M_SXFR_SDMA_ERR:
1169 case QHSTA_M_SXFR_SXFR_PERR:
1170 case QHSTA_M_RDMA_PERR:
1171 /*
1172 * DMA Error. This should *NEVER* happen!
1173 *
1174 * Lets try resetting the bus and reinitialize
1175 * the host adapter.
1176 */
1177 printf("%s: DMA Error. Reseting bus\n",
1178 sc->sc_dev.dv_xname);
1179 TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
1180 adw_reset_bus(sc);
1181 xs->error = XS_BUSY;
1182 goto done;
1183
1184 case QHSTA_M_WTM_TIMEOUT:
1185 case QHSTA_M_SXFR_WD_TMO:
1186 /* The SCSI bus hung in a phase */
1187 printf("%s: Watch Dog timer expired. Reseting bus\n",
1188 sc->sc_dev.dv_xname);
1189 TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
1190 adw_reset_bus(sc);
1191 xs->error = XS_BUSY;
1192 goto done;
1193
1194 case QHSTA_M_SXFR_XFR_PH_ERR:
1195 printf("%s: Transfer Error\n", sc->sc_dev.dv_xname);
1196 xs->error = XS_DRIVER_STUFFUP;
1197 break;
1198
1199 case QHSTA_M_BAD_CMPL_STATUS_IN:
1200 /* No command complete after a status message */
1201 printf("%s: Bad Completion Status\n",
1202 sc->sc_dev.dv_xname);
1203 xs->error = XS_DRIVER_STUFFUP;
1204 break;
1205
1206 case QHSTA_M_AUTO_REQ_SENSE_FAIL:
1207 printf("%s: Auto Sense Failed\n", sc->sc_dev.dv_xname);
1208 xs->error = XS_DRIVER_STUFFUP;
1209 break;
1210
1211 case QHSTA_M_INVALID_DEVICE:
1212 printf("%s: Invalid Device\n", sc->sc_dev.dv_xname);
1213 xs->error = XS_DRIVER_STUFFUP;
1214 break;
1215
1216 case QHSTA_M_NO_AUTO_REQ_SENSE:
1217 /*
1218 * User didn't request sense, but we got a
1219 * check condition.
1220 */
1221 printf("%s: Unexpected Check Condition\n",
1222 sc->sc_dev.dv_xname);
1223 xs->error = XS_DRIVER_STUFFUP;
1224 break;
1225
1226 case QHSTA_M_SXFR_UNKNOWN_ERROR:
1227 printf("%s: Unknown Error\n", sc->sc_dev.dv_xname);
1228 xs->error = XS_DRIVER_STUFFUP;
1229 break;
1230
1231 default:
1232 panic("%s: Unhandled Host Status Error %x",
1233 sc->sc_dev.dv_xname, scsiq->host_status);
1234 }
1235 }
1236
1237 TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
1238 done: adw_free_ccb(sc, ccb);
1239 scsipi_done(xs);
1240 }
1241
1242
1243 /*
1244 * adw_async_callback() - Adv Library asynchronous event callback function.
1245 */
1246 static void
1247 adw_async_callback(sc, code)
1248 ADW_SOFTC *sc;
1249 u_int8_t code;
1250 {
1251 switch (code) {
1252 case ADV_ASYNC_SCSI_BUS_RESET_DET:
1253 /* The firmware detected a SCSI Bus reset. */
1254 printf("%s: SCSI Bus reset detected\n", sc->sc_dev.dv_xname);
1255 break;
1256
1257 case ADV_ASYNC_RDMA_FAILURE:
1258 /*
1259 * Handle RDMA failure by resetting the SCSI Bus and
1260 * possibly the chip if it is unresponsive.
1261 */
1262 printf("%s: RDMA failure. Resetting the SCSI Bus and"
1263 " the adapter\n", sc->sc_dev.dv_xname);
1264 AdwResetSCSIBus(sc);
1265 break;
1266
1267 case ADV_HOST_SCSI_BUS_RESET:
1268 /* Host generated SCSI bus reset occurred. */
1269 printf("%s: Host generated SCSI bus reset occurred\n",
1270 sc->sc_dev.dv_xname);
1271 break;
1272
1273 case ADV_ASYNC_CARRIER_READY_FAILURE:
1274 /* Carrier Ready failure. */
1275 printf("%s: Carrier Ready failure!\n", sc->sc_dev.dv_xname);
1276 break;
1277
1278 default:
1279 break;
1280 }
1281 }
1282