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