ahb.c revision 1.63 1 /* $NetBSD: ahb.c,v 1.63 2016/07/11 11:31:50 msaitoh Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Originally written by Julian Elischer (julian (at) tfs.com)
35 * for TRW Financial Systems for use under the MACH(2.5) operating system.
36 *
37 * TRW Financial Systems, in accordance with their agreement with Carnegie
38 * Mellon University, makes this software available to CMU to distribute
39 * or use in any manner that they see fit as long as this message is kept with
40 * the software. For this reason TFS also grants any other persons or
41 * organisations permission to use or modify this software.
42 *
43 * TFS supplies this software to be publicly redistributed
44 * on the understanding that TFS is not responsible for the correct
45 * functioning of this software in any circumstances.
46 */
47
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: ahb.c,v 1.63 2016/07/11 11:31:50 msaitoh Exp $");
50
51 #include "opt_ddb.h"
52
53 #undef AHBDEBUG
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/errno.h>
59 #include <sys/ioctl.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62 #include <sys/buf.h>
63 #include <sys/proc.h>
64
65 #include <sys/bus.h>
66 #include <sys/intr.h>
67
68 #include <dev/scsipi/scsi_all.h>
69 #include <dev/scsipi/scsipi_all.h>
70 #include <dev/scsipi/scsiconf.h>
71
72 #include <dev/eisa/eisareg.h>
73 #include <dev/eisa/eisavar.h>
74 #include <dev/eisa/eisadevs.h>
75 #include <dev/eisa/ahbreg.h>
76
77 #ifndef DDB
78 #define Debugger() panic("should call debugger here (aha1742.c)")
79 #endif /* ! DDB */
80
81 #define AHB_ECB_MAX 32 /* store up to 32 ECBs at one time */
82 #define ECB_HASH_SIZE 32 /* hash table size for phystokv */
83 #define ECB_HASH_SHIFT 9
84 #define ECB_HASH(x) ((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
85
86 #define AHB_MAXXFER ((AHB_NSEG - 1) << PGSHIFT)
87
88 struct ahb_softc {
89 device_t sc_dev;
90
91 bus_space_tag_t sc_iot;
92 bus_space_handle_t sc_ioh;
93 bus_dma_tag_t sc_dmat;
94 void *sc_ih;
95
96 bus_dmamap_t sc_dmamap_ecb; /* maps the ecbs */
97 struct ahb_ecb *sc_ecbs; /* all our ecbs */
98
99 struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
100 TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
101 struct ahb_ecb *sc_immed_ecb; /* an outstanding immediete command */
102 int sc_numecbs;
103
104 struct scsipi_adapter sc_adapter;
105 struct scsipi_channel sc_channel;
106 };
107
108 /*
109 * Offset of an ECB from the beginning of the ECB DMA mapping.
110 */
111 #define AHB_ECB_OFF(e) (((u_long)(e)) - ((u_long)&sc->sc_ecbs[0]))
112
113 struct ahb_probe_data {
114 int sc_irq;
115 int sc_scsi_dev;
116 };
117
118 static void ahb_send_mbox(struct ahb_softc *, int, struct ahb_ecb *);
119 static void ahb_send_immed(struct ahb_softc *, u_int32_t, struct ahb_ecb *);
120 static int ahbintr(void *);
121 static void ahb_free_ecb(struct ahb_softc *, struct ahb_ecb *);
122 static struct ahb_ecb *ahb_get_ecb(struct ahb_softc *);
123 static struct ahb_ecb *ahb_ecb_phys_kv(struct ahb_softc *, physaddr);
124 static void ahb_done(struct ahb_softc *, struct ahb_ecb *);
125 static int ahb_find(bus_space_tag_t, bus_space_handle_t,
126 struct ahb_probe_data *);
127 static int ahb_init(struct ahb_softc *);
128 static void ahbminphys(struct buf *);
129 static void ahb_scsipi_request(struct scsipi_channel *,
130 scsipi_adapter_req_t, void *);
131 static int ahb_poll(struct ahb_softc *, struct scsipi_xfer *, int);
132 static void ahb_timeout(void *);
133 static int ahb_create_ecbs(struct ahb_softc *, struct ahb_ecb *, int);
134
135 static int ahb_init_ecb(struct ahb_softc *, struct ahb_ecb *);
136
137 static int ahbmatch(device_t, cfdata_t, void *);
138 static void ahbattach(device_t, device_t, void *);
139
140 CFATTACH_DECL_NEW(ahb, sizeof(struct ahb_softc),
141 ahbmatch, ahbattach, NULL, NULL);
142
143 #define AHB_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
144
145 /*
146 * Check the slots looking for a board we recognise
147 * If we find one, note its address (slot) and call
148 * the actual probe routine to check it out.
149 */
150 static int
151 ahbmatch(device_t parent, cfdata_t match,
152 void *aux)
153 {
154 struct eisa_attach_args *ea = aux;
155 bus_space_tag_t iot = ea->ea_iot;
156 bus_space_handle_t ioh;
157 int rv;
158
159 /* must match one of our known ID strings */
160 if (strcmp(ea->ea_idstring, "ADP0000") &&
161 strcmp(ea->ea_idstring, "ADP0001") &&
162 strcmp(ea->ea_idstring, "ADP0002") &&
163 strcmp(ea->ea_idstring, "ADP0400"))
164 return (0);
165
166 if (bus_space_map(iot,
167 EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET,
168 AHB_EISA_IOSIZE, 0, &ioh))
169 return (0);
170
171 rv = !ahb_find(iot, ioh, NULL);
172
173 bus_space_unmap(iot, ioh, AHB_EISA_IOSIZE);
174
175 return (rv);
176 }
177
178 /*
179 * Attach all the sub-devices we can find
180 */
181 static void
182 ahbattach(device_t parent, device_t self, void *aux)
183 {
184 struct eisa_attach_args *ea = aux;
185 struct ahb_softc *sc = device_private(self);
186 bus_space_tag_t iot = ea->ea_iot;
187 bus_space_handle_t ioh;
188 eisa_chipset_tag_t ec = ea->ea_ec;
189 eisa_intr_handle_t ih;
190 const char *model, *intrstr;
191 struct ahb_probe_data apd;
192 struct scsipi_adapter *adapt = &sc->sc_adapter;
193 struct scsipi_channel *chan = &sc->sc_channel;
194 char intrbuf[EISA_INTRSTR_LEN];
195
196 sc->sc_dev = self;
197
198 if (!strcmp(ea->ea_idstring, "ADP0000"))
199 model = EISA_PRODUCT_ADP0000;
200 else if (!strcmp(ea->ea_idstring, "ADP0001"))
201 model = EISA_PRODUCT_ADP0001;
202 else if (!strcmp(ea->ea_idstring, "ADP0002"))
203 model = EISA_PRODUCT_ADP0002;
204 else if (!strcmp(ea->ea_idstring, "ADP0400"))
205 model = EISA_PRODUCT_ADP0400;
206 else
207 model = "unknown model!";
208 printf(": %s\n", model);
209
210 if (bus_space_map(iot,
211 EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET,
212 AHB_EISA_IOSIZE, 0, &ioh))
213 panic("ahbattach: could not map I/O addresses");
214
215 sc->sc_iot = iot;
216 sc->sc_ioh = ioh;
217 sc->sc_dmat = ea->ea_dmat;
218 if (ahb_find(iot, ioh, &apd))
219 panic("ahbattach: ahb_find failed!");
220
221 TAILQ_INIT(&sc->sc_free_ecb);
222
223 /*
224 * Fill in the scsipi_adapter.
225 */
226 memset(adapt, 0, sizeof(*adapt));
227 adapt->adapt_dev = sc->sc_dev;
228 adapt->adapt_nchannels = 1;
229 /* adapt_openings initialized below */
230 adapt->adapt_max_periph = 4; /* XXX arbitrary? */
231 adapt->adapt_request = ahb_scsipi_request;
232 adapt->adapt_minphys = ahbminphys;
233
234 /*
235 * Fill in the scsipi_channel.
236 */
237 memset(chan, 0, sizeof(*chan));
238 chan->chan_adapter = adapt;
239 chan->chan_bustype = &scsi_bustype;
240 chan->chan_channel = 0;
241 chan->chan_ntargets = 8;
242 chan->chan_nluns = 8;
243 chan->chan_id = apd.sc_scsi_dev;
244
245 if (ahb_init(sc) != 0) {
246 /* Error during initialization! */
247 return;
248 }
249
250 if (eisa_intr_map(ec, apd.sc_irq, &ih)) {
251 aprint_error_dev(sc->sc_dev, "couldn't map interrupt (%d)\n",
252 apd.sc_irq);
253 return;
254 }
255 intrstr = eisa_intr_string(ec, ih, intrbuf, sizeof(intrbuf));
256 sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
257 ahbintr, sc);
258 if (sc->sc_ih == NULL) {
259 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
260 if (intrstr != NULL)
261 aprint_error(" at %s", intrstr);
262 aprint_error("\n");
263 return;
264 }
265 if (intrstr != NULL)
266 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
267
268 /*
269 * ask the adapter what subunits are present
270 */
271 config_found(self, &sc->sc_channel, scsiprint);
272 }
273
274 /*
275 * Function to send a command out through a mailbox
276 */
277 static void
278 ahb_send_mbox(struct ahb_softc *sc, int opcode, struct ahb_ecb *ecb)
279 {
280 bus_space_tag_t iot = sc->sc_iot;
281 bus_space_handle_t ioh = sc->sc_ioh;
282 int wait = 300; /* 1ms should be enough */
283
284 while (--wait) {
285 if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
286 == (G2STAT_MBOX_EMPTY))
287 break;
288 delay(10);
289 }
290 if (!wait) {
291 printf("%s: board not responding\n", device_xname(sc->sc_dev));
292 Debugger();
293 }
294
295 /*
296 * don't know if this will work.
297 * XXX WHAT DOES THIS COMMENT MEAN?! --thorpej
298 */
299 bus_space_write_4(iot, ioh, MBOXOUT0,
300 sc->sc_dmamap_ecb->dm_segs[0].ds_addr + AHB_ECB_OFF(ecb));
301 bus_space_write_1(iot, ioh, ATTN, opcode |
302 ecb->xs->xs_periph->periph_target);
303
304 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
305 callout_reset(&ecb->xs->xs_callout,
306 mstohz(ecb->timeout), ahb_timeout, ecb);
307 }
308
309 /*
310 * Function to send an immediate type command to the adapter
311 */
312 static void
313 ahb_send_immed(struct ahb_softc *sc, u_int32_t cmd, struct ahb_ecb *ecb)
314 {
315 bus_space_tag_t iot = sc->sc_iot;
316 bus_space_handle_t ioh = sc->sc_ioh;
317 int wait = 100; /* 1 ms enough? */
318
319 while (--wait) {
320 if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
321 == (G2STAT_MBOX_EMPTY))
322 break;
323 delay(10);
324 }
325 if (!wait) {
326 printf("%s: board not responding\n", device_xname(sc->sc_dev));
327 Debugger();
328 }
329
330 bus_space_write_4(iot, ioh, MBOXOUT0, cmd); /* don't know this will work */
331 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
332 bus_space_write_1(iot, ioh, ATTN, OP_IMMED |
333 ecb->xs->xs_periph->periph_target);
334
335 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
336 callout_reset(&ecb->xs->xs_callout,
337 mstohz(ecb->timeout), ahb_timeout, ecb);
338 }
339
340 /*
341 * Catch an interrupt from the adaptor
342 */
343 static int
344 ahbintr(void *arg)
345 {
346 struct ahb_softc *sc = arg;
347 bus_space_tag_t iot = sc->sc_iot;
348 bus_space_handle_t ioh = sc->sc_ioh;
349 struct ahb_ecb *ecb;
350 u_char ahbstat;
351 u_int32_t mboxval;
352
353 #ifdef AHBDEBUG
354 printf("%s: ahbintr ", device_xname(sc->sc_dev));
355 #endif /* AHBDEBUG */
356
357 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
358 return 0;
359
360 for (;;) {
361 /*
362 * First get all the information and then
363 * acknowledge the interrupt
364 */
365 ahbstat = bus_space_read_1(iot, ioh, G2INTST);
366 mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
367 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
368
369 #ifdef AHBDEBUG
370 printf("status = 0x%x ", ahbstat);
371 #endif /* AHBDEBUG */
372
373 /*
374 * Process the completed operation
375 */
376 switch (ahbstat & G2INTST_INT_STAT) {
377 case AHB_ECB_OK:
378 case AHB_ECB_RECOVERED:
379 case AHB_ECB_ERR:
380 ecb = ahb_ecb_phys_kv(sc, mboxval);
381 if (!ecb) {
382 aprint_error_dev(sc->sc_dev,
383 "BAD ECB RETURNED!\n");
384 goto next; /* whatever it was, it'll timeout */
385 }
386 break;
387
388 case AHB_IMMED_ERR:
389 ecb = sc->sc_immed_ecb;
390 sc->sc_immed_ecb = 0;
391 ecb->flags |= ECB_IMMED_FAIL;
392 break;
393
394 case AHB_IMMED_OK:
395 ecb = sc->sc_immed_ecb;
396 sc->sc_immed_ecb = 0;
397 break;
398
399 default:
400 aprint_error_dev(sc->sc_dev,
401 "unexpected interrupt %x\n", ahbstat);
402 goto next;
403 }
404
405 callout_stop(&ecb->xs->xs_callout);
406 ahb_done(sc, ecb);
407
408 next:
409 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
410 return 1;
411 }
412 }
413
414 static inline void
415 ahb_reset_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
416 {
417
418 ecb->flags = 0;
419 }
420
421 /*
422 * A ecb (and hence a mbx-out is put onto the
423 * free list.
424 */
425 static void
426 ahb_free_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
427 {
428 int s;
429
430 s = splbio();
431 ahb_reset_ecb(sc, ecb);
432 TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
433 splx(s);
434 }
435
436 /*
437 * Create a set of ecbs and add them to the free list.
438 */
439 static int
440 ahb_init_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
441 {
442 bus_dma_tag_t dmat = sc->sc_dmat;
443 int hashnum, error;
444
445 /*
446 * Create the DMA map for this ECB.
447 */
448 error = bus_dmamap_create(dmat, AHB_MAXXFER, AHB_NSEG, AHB_MAXXFER,
449 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &ecb->dmamap_xfer);
450 if (error) {
451 aprint_error_dev(sc->sc_dev, "can't create ecb dmamap_xfer\n");
452 return (error);
453 }
454
455 /*
456 * put in the phystokv hash table
457 * Never gets taken out.
458 */
459 ecb->hashkey = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
460 AHB_ECB_OFF(ecb);
461 hashnum = ECB_HASH(ecb->hashkey);
462 ecb->nexthash = sc->sc_ecbhash[hashnum];
463 sc->sc_ecbhash[hashnum] = ecb;
464 ahb_reset_ecb(sc, ecb);
465 return (0);
466 }
467
468 static int
469 ahb_create_ecbs(struct ahb_softc *sc, struct ahb_ecb *ecbstore, int count)
470 {
471 struct ahb_ecb *ecb;
472 int i, error;
473
474 memset(ecbstore, 0, sizeof(struct ahb_ecb) * count);
475 for (i = 0; i < count; i++) {
476 ecb = &ecbstore[i];
477 if ((error = ahb_init_ecb(sc, ecb)) != 0) {
478 aprint_error_dev(sc->sc_dev,
479 "unable to initialize ecb, error = %d\n", error);
480 goto out;
481 }
482 TAILQ_INSERT_TAIL(&sc->sc_free_ecb, ecb, chain);
483 }
484 out:
485 return (i);
486 }
487
488 /*
489 * Get a free ecb
490 *
491 * If there are none, see if we can allocate a new one. If so, put it in the
492 * hash table too otherwise either return an error or sleep.
493 */
494 static struct ahb_ecb *
495 ahb_get_ecb(struct ahb_softc *sc)
496 {
497 struct ahb_ecb *ecb;
498 int s;
499
500 s = splbio();
501 ecb = TAILQ_FIRST(&sc->sc_free_ecb);
502 if (ecb != NULL) {
503 TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
504 ecb->flags |= ECB_ALLOC;
505 }
506 splx(s);
507 return (ecb);
508 }
509
510 /*
511 * given a physical address, find the ecb that it corresponds to.
512 */
513 static struct ahb_ecb *
514 ahb_ecb_phys_kv(struct ahb_softc *sc, physaddr ecb_phys)
515 {
516 int hashnum = ECB_HASH(ecb_phys);
517 struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
518
519 while (ecb) {
520 if (ecb->hashkey == ecb_phys)
521 break;
522 ecb = ecb->nexthash;
523 }
524 return ecb;
525 }
526
527 /*
528 * We have a ecb which has been processed by the adaptor, now we look to see
529 * how the operation went.
530 */
531 static void
532 ahb_done(struct ahb_softc *sc, struct ahb_ecb *ecb)
533 {
534 bus_dma_tag_t dmat = sc->sc_dmat;
535 struct scsi_sense_data *s1, *s2;
536 struct scsipi_xfer *xs = ecb->xs;
537
538 SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("ahb_done\n"));
539
540 bus_dmamap_sync(dmat, sc->sc_dmamap_ecb,
541 AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
542 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
543
544 /*
545 * If we were a data transfer, unload the map that described
546 * the data buffer.
547 */
548 if (xs->datalen) {
549 bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
550 ecb->dmamap_xfer->dm_mapsize,
551 (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
552 BUS_DMASYNC_POSTWRITE);
553 bus_dmamap_unload(dmat, ecb->dmamap_xfer);
554 }
555
556 /*
557 * Otherwise, put the results of the operation
558 * into the xfer and call whoever started it
559 */
560 if ((ecb->flags & ECB_ALLOC) == 0) {
561 aprint_error_dev(sc->sc_dev, "exiting ecb not allocated!\n");
562 Debugger();
563 }
564 if (ecb->flags & ECB_IMMED) {
565 if (ecb->flags & ECB_IMMED_FAIL)
566 xs->error = XS_DRIVER_STUFFUP;
567 goto done;
568 }
569 if (xs->error == XS_NOERROR) {
570 if (ecb->ecb_status.host_stat != HS_OK) {
571 switch (ecb->ecb_status.host_stat) {
572 case HS_TIMED_OUT: /* No response */
573 xs->error = XS_SELTIMEOUT;
574 break;
575 default: /* Other scsi protocol messes */
576 printf("%s: host_stat %x\n",
577 device_xname(sc->sc_dev), ecb->ecb_status.host_stat);
578 xs->error = XS_DRIVER_STUFFUP;
579 }
580 } else if (ecb->ecb_status.target_stat != SCSI_OK) {
581 switch (ecb->ecb_status.target_stat) {
582 case SCSI_CHECK:
583 s1 = &ecb->ecb_sense;
584 s2 = &xs->sense.scsi_sense;
585 *s2 = *s1;
586 xs->error = XS_SENSE;
587 break;
588 case SCSI_BUSY:
589 xs->error = XS_BUSY;
590 break;
591 default:
592 printf("%s: target_stat %x\n",
593 device_xname(sc->sc_dev), ecb->ecb_status.target_stat);
594 xs->error = XS_DRIVER_STUFFUP;
595 }
596 } else
597 xs->resid = 0;
598 }
599 done:
600 ahb_free_ecb(sc, ecb);
601 scsipi_done(xs);
602 }
603
604 /*
605 * Start the board, ready for normal operation
606 */
607 static int
608 ahb_find(bus_space_tag_t iot, bus_space_handle_t ioh,
609 struct ahb_probe_data *sc)
610 {
611 u_char intdef;
612 int i, irq, busid;
613 int wait = 1000; /* 1 sec enough? */
614
615 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
616
617 #define NO_NO 1
618 #ifdef NO_NO
619 /*
620 * reset board, If it doesn't respond, assume
621 * that it's not there.. good for the probe
622 */
623 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
624 delay(1000);
625 bus_space_write_1(iot, ioh, G2CNTRL, 0);
626 delay(10000);
627 while (--wait) {
628 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
629 break;
630 delay(1000);
631 }
632 if (!wait) {
633 #ifdef AHBDEBUG
634 printf("ahb_find: No answer from aha1742 board\n");
635 #endif /* AHBDEBUG */
636 return ENXIO;
637 }
638 i = bus_space_read_1(iot, ioh, MBOXIN0);
639 if (i) {
640 printf("self test failed, val = 0x%x\n", i);
641 return EIO;
642 }
643
644 /* Set it again, just to be sure. */
645 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
646 #endif
647
648 while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
649 printf(".");
650 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
651 delay(10000);
652 }
653
654 intdef = bus_space_read_1(iot, ioh, INTDEF);
655 switch (intdef & 0x07) {
656 case INT9:
657 irq = 9;
658 break;
659 case INT10:
660 irq = 10;
661 break;
662 case INT11:
663 irq = 11;
664 break;
665 case INT12:
666 irq = 12;
667 break;
668 case INT14:
669 irq = 14;
670 break;
671 case INT15:
672 irq = 15;
673 break;
674 default:
675 printf("illegal int setting %x\n", intdef);
676 return EIO;
677 }
678
679 bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN)); /* make sure we can interrupt */
680
681 /* who are we on the scsi bus? */
682 busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
683
684 /* if we want to return data, do so now */
685 if (sc) {
686 sc->sc_irq = irq;
687 sc->sc_scsi_dev = busid;
688 }
689
690 /*
691 * Note that we are going and return (to probe)
692 */
693 return 0;
694 }
695
696 static int
697 ahb_init(struct ahb_softc *sc)
698 {
699 bus_dma_segment_t seg;
700 int i, error, rseg;
701
702 #define ECBSIZE (AHB_ECB_MAX * sizeof(struct ahb_ecb))
703
704 /*
705 * Allocate the ECBs.
706 */
707 if ((error = bus_dmamem_alloc(sc->sc_dmat, ECBSIZE,
708 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
709 aprint_error_dev(sc->sc_dev, "unable to allocate ecbs, error = %d\n",
710 error);
711 return (error);
712 }
713 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
714 ECBSIZE, (void **)&sc->sc_ecbs,
715 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
716 aprint_error_dev(sc->sc_dev,
717 "unable to map ecbs, error = %d\n", error);
718 return (error);
719 }
720
721 /*
722 * Create and load the DMA map used for the ecbs.
723 */
724 if ((error = bus_dmamap_create(sc->sc_dmat, ECBSIZE,
725 1, ECBSIZE, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_ecb)) != 0) {
726 aprint_error_dev(sc->sc_dev,
727 "unable to create ecb DMA map, error = %d\n", error);
728 return (error);
729 }
730 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ecb,
731 sc->sc_ecbs, ECBSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
732 aprint_error_dev(sc->sc_dev,
733 "unable to load ecb DMA map, error = %d\n", error);
734 return (error);
735 }
736
737 #undef ECBSIZE
738
739 /*
740 * Initialize the ecbs.
741 */
742 i = ahb_create_ecbs(sc, sc->sc_ecbs, AHB_ECB_MAX);
743 if (i == 0) {
744 aprint_error_dev(sc->sc_dev, "unable to create ecbs\n");
745 return (ENOMEM);
746 } else if (i != AHB_ECB_MAX) {
747 printf("%s: WARNING: only %d of %d ecbs created\n",
748 device_xname(sc->sc_dev), i, AHB_ECB_MAX);
749 }
750
751 sc->sc_adapter.adapt_openings = i;
752
753 return (0);
754 }
755
756 static void
757 ahbminphys(struct buf *bp)
758 {
759
760 if (bp->b_bcount > AHB_MAXXFER)
761 bp->b_bcount = AHB_MAXXFER;
762 minphys(bp);
763 }
764
765 /*
766 * start a scsi operation given the command and the data address. Also needs
767 * the unit, target and lu.
768 */
769 static void
770 ahb_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
771 void *arg)
772 {
773 struct scsipi_xfer *xs;
774 struct scsipi_periph *periph;
775 struct ahb_softc *sc = device_private(chan->chan_adapter->adapt_dev);
776 bus_dma_tag_t dmat = sc->sc_dmat;
777 struct ahb_ecb *ecb;
778 int error, seg, flags, s;
779
780 switch (req) {
781 case ADAPTER_REQ_RUN_XFER:
782 xs = arg;
783 periph = xs->xs_periph;
784 flags = xs->xs_control;
785
786 SC_DEBUG(periph, SCSIPI_DB2, ("ahb_scsipi_request\n"));
787
788 /* Get an ECB to use. */
789 ecb = ahb_get_ecb(sc);
790 #ifdef DIAGNOSTIC
791 /*
792 * This should never happen as we track the resources
793 * in the mid-layer.
794 */
795 if (ecb == NULL) {
796 scsipi_printaddr(periph);
797 printf("unable to allocate ecb\n");
798 panic("ahb_scsipi_request");
799 }
800 #endif
801
802 ecb->xs = xs;
803 ecb->timeout = xs->timeout;
804
805 /*
806 * If it's a reset, we need to do an 'immediate'
807 * command, and store its ecb for later
808 * if there is already an immediate waiting,
809 * then WE must wait
810 */
811 if (flags & XS_CTL_RESET) {
812 ecb->flags |= ECB_IMMED;
813 if (sc->sc_immed_ecb) {
814 ahb_free_ecb(sc, ecb);
815 xs->error = XS_BUSY;
816 scsipi_done(xs);
817 return;
818 }
819 sc->sc_immed_ecb = ecb;
820
821 s = splbio();
822 ahb_send_immed(sc, AHB_TARG_RESET, ecb);
823 splx(s);
824
825 if ((flags & XS_CTL_POLL) == 0)
826 return;
827
828 /*
829 * If we can't use interrupts, poll on completion
830 */
831 if (ahb_poll(sc, xs, ecb->timeout))
832 ahb_timeout(ecb);
833 return;
834 }
835
836 /*
837 * Put all the arguments for the xfer in the ecb
838 */
839 if (xs->cmdlen > sizeof(ecb->scsi_cmd)) {
840 aprint_error_dev(sc->sc_dev,
841 "cmdlen %d too large for ECB\n", xs->cmdlen);
842 xs->error = XS_DRIVER_STUFFUP;
843 goto out_bad;
844 }
845 ecb->opcode = ECB_SCSI_OP;
846 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
847 ecb->opt2 = periph->periph_lun | ECB_NRB;
848 memcpy(&ecb->scsi_cmd, xs->cmd,
849 ecb->scsi_cmd_length = xs->cmdlen);
850 ecb->sense_ptr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
851 AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_sense);
852 ecb->req_sense_length = sizeof(ecb->ecb_sense);
853 ecb->status = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
854 AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_status);
855 ecb->ecb_status.host_stat = 0x00;
856 ecb->ecb_status.target_stat = 0x00;
857
858 if (xs->datalen) {
859 /*
860 * Map the DMA transfer.
861 */
862 #ifdef TFS
863 if (flags & XS_CTL_DATA_UIO) {
864 error = bus_dmamap_load_uio(sc->sc_dmat,
865 ecb->dmamap_xfer, (struct uio *)xs->data,
866 BUS_DMA_NOWAIT);
867 } else
868 #endif /* TFS */
869 {
870 error = bus_dmamap_load(sc->sc_dmat,
871 ecb->dmamap_xfer, xs->data, xs->datalen,
872 NULL, BUS_DMA_NOWAIT);
873 }
874
875 switch (error) {
876 case 0:
877 break;
878
879 case ENOMEM:
880 case EAGAIN:
881 xs->error = XS_RESOURCE_SHORTAGE;
882 goto out_bad;
883
884 default:
885 xs->error = XS_DRIVER_STUFFUP;
886 aprint_error_dev(sc->sc_dev,
887 "error %d loading DMA map\n", error);
888 out_bad:
889 ahb_free_ecb(sc, ecb);
890 scsipi_done(xs);
891 return;
892 }
893
894 bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
895 ecb->dmamap_xfer->dm_mapsize,
896 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
897 BUS_DMASYNC_PREWRITE);
898
899 /*
900 * Load the hardware scatter/gather map with the
901 * contents of the DMA map.
902 */
903 for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
904 ecb->ahb_dma[seg].seg_addr =
905 ecb->dmamap_xfer->dm_segs[seg].ds_addr;
906 ecb->ahb_dma[seg].seg_len =
907 ecb->dmamap_xfer->dm_segs[seg].ds_len;
908 }
909
910 ecb->data_addr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
911 AHB_ECB_OFF(ecb) +
912 offsetof(struct ahb_ecb, ahb_dma);
913 ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
914 sizeof(struct ahb_dma_seg);
915 ecb->opt1 |= ECB_S_G;
916 } else { /* No data xfer, use non S/G values */
917 ecb->data_addr = (physaddr)0;
918 ecb->data_length = 0;
919 }
920 ecb->link_addr = (physaddr)0;
921
922 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ecb,
923 AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
924 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
925
926 s = splbio();
927 ahb_send_mbox(sc, OP_START_ECB, ecb);
928 splx(s);
929
930 if ((flags & XS_CTL_POLL) == 0)
931 return;
932
933 /*
934 * If we can't use interrupts, poll on completion
935 */
936 if (ahb_poll(sc, xs, ecb->timeout)) {
937 ahb_timeout(ecb);
938 if (ahb_poll(sc, xs, ecb->timeout))
939 ahb_timeout(ecb);
940 }
941 return;
942
943 case ADAPTER_REQ_GROW_RESOURCES:
944 /* XXX Not supported. */
945 return;
946
947 case ADAPTER_REQ_SET_XFER_MODE:
948 /* XXX How do we do this? */
949 return;
950 }
951 }
952
953 /*
954 * Function to poll for command completion when in poll mode
955 */
956 static int
957 ahb_poll(struct ahb_softc *sc, struct scsipi_xfer *xs, int count)
958 { /* in msec */
959 bus_space_tag_t iot = sc->sc_iot;
960 bus_space_handle_t ioh = sc->sc_ioh;
961
962 while (count) {
963 /*
964 * If we had interrupts enabled, would we
965 * have got an interrupt?
966 */
967 if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
968 ahbintr(sc);
969 if (xs->xs_status & XS_STS_DONE)
970 return 0;
971 delay(1000);
972 count--;
973 }
974 return 1;
975 }
976
977 static void
978 ahb_timeout(void *arg)
979 {
980 struct ahb_ecb *ecb = arg;
981 struct scsipi_xfer *xs = ecb->xs;
982 struct scsipi_periph *periph = xs->xs_periph;
983 struct ahb_softc *sc =
984 device_private(periph->periph_channel->chan_adapter->adapt_dev);
985 int s;
986
987 scsipi_printaddr(periph);
988 printf("timed out");
989
990 s = splbio();
991
992 if (ecb->flags & ECB_IMMED) {
993 printf("\n");
994 ecb->flags |= ECB_IMMED_FAIL;
995 /* XXX Must reset! */
996 } else
997
998 /*
999 * If it has been through before, then
1000 * a previous abort has failed, don't
1001 * try abort again
1002 */
1003 if (ecb->flags & ECB_ABORT) {
1004 /* abort timed out */
1005 printf(" AGAIN\n");
1006 /* XXX Must reset! */
1007 } else {
1008 /* abort the operation that has timed out */
1009 printf("\n");
1010 ecb->xs->error = XS_TIMEOUT;
1011 ecb->timeout = AHB_ABORT_TIMEOUT;
1012 ecb->flags |= ECB_ABORT;
1013 ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
1014 }
1015
1016 splx(s);
1017 }
1018