FreeBSD/Linux Kernel Cross Reference
sys/dev/eisa/ahb.c
1 /* $NetBSD: ahb.c,v 1.51 2008/04/28 20:23:48 martin 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@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.51 2008/04/28 20:23:48 martin 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 #include <sys/user.h>
65
66 #include <uvm/uvm_extern.h>
67
68 #include <sys/bus.h>
69 #include <sys/intr.h>
70
71 #include <dev/scsipi/scsi_all.h>
72 #include <dev/scsipi/scsipi_all.h>
73 #include <dev/scsipi/scsiconf.h>
74
75 #include <dev/eisa/eisareg.h>
76 #include <dev/eisa/eisavar.h>
77 #include <dev/eisa/eisadevs.h>
78 #include <dev/eisa/ahbreg.h>
79
80 #ifndef DDB
81 #define Debugger() panic("should call debugger here (aha1742.c)")
82 #endif /* ! DDB */
83
84 #define AHB_ECB_MAX 32 /* store up to 32 ECBs at one time */
85 #define ECB_HASH_SIZE 32 /* hash table size for phystokv */
86 #define ECB_HASH_SHIFT 9
87 #define ECB_HASH(x) ((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
88
89 #define AHB_MAXXFER ((AHB_NSEG - 1) << PGSHIFT)
90
91 struct ahb_softc {
92 struct device sc_dev;
93
94 bus_space_tag_t sc_iot;
95 bus_space_handle_t sc_ioh;
96 bus_dma_tag_t sc_dmat;
97 void *sc_ih;
98
99 bus_dmamap_t sc_dmamap_ecb; /* maps the ecbs */
100 struct ahb_ecb *sc_ecbs; /* all our ecbs */
101
102 struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
103 TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
104 struct ahb_ecb *sc_immed_ecb; /* an outstanding immediete command */
105 int sc_numecbs;
106
107 struct scsipi_adapter sc_adapter;
108 struct scsipi_channel sc_channel;
109 };
110
111 /*
112 * Offset of an ECB from the beginning of the ECB DMA mapping.
113 */
114 #define AHB_ECB_OFF(e) (((u_long)(e)) - ((u_long)&sc->sc_ecbs[0]))
115
116 struct ahb_probe_data {
117 int sc_irq;
118 int sc_scsi_dev;
119 };
120
121 static void ahb_send_mbox(struct ahb_softc *, int, struct ahb_ecb *);
122 static void ahb_send_immed(struct ahb_softc *, u_int32_t, struct ahb_ecb *);
123 static int ahbintr(void *);
124 static void ahb_free_ecb(struct ahb_softc *, struct ahb_ecb *);
125 static struct ahb_ecb *ahb_get_ecb(struct ahb_softc *);
126 static struct ahb_ecb *ahb_ecb_phys_kv(struct ahb_softc *, physaddr);
127 static void ahb_done(struct ahb_softc *, struct ahb_ecb *);
128 static int ahb_find(bus_space_tag_t, bus_space_handle_t,
129 struct ahb_probe_data *);
130 static int ahb_init(struct ahb_softc *);
131 static void ahbminphys(struct buf *);
132 static void ahb_scsipi_request(struct scsipi_channel *,
133 scsipi_adapter_req_t, void *);
134 static int ahb_poll(struct ahb_softc *, struct scsipi_xfer *, int);
135 static void ahb_timeout(void *);
136 static int ahb_create_ecbs(struct ahb_softc *, struct ahb_ecb *, int);
137
138 static int ahb_init_ecb(struct ahb_softc *, struct ahb_ecb *);
139
140 static int ahbmatch(struct device *, struct cfdata *, void *);
141 static void ahbattach(struct device *, struct device *, void *);
142
143 CFATTACH_DECL(ahb, sizeof(struct ahb_softc),
144 ahbmatch, ahbattach, NULL, NULL);
145
146 #define AHB_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
147
148 /*
149 * Check the slots looking for a board we recognise
150 * If we find one, note it's address (slot) and call
151 * the actual probe routine to check it out.
152 */
153 static int
154 ahbmatch(struct device *parent, struct cfdata *match,
155 void *aux)
156 {
157 struct eisa_attach_args *ea = aux;
158 bus_space_tag_t iot = ea->ea_iot;
159 bus_space_handle_t ioh;
160 int rv;
161
162 /* must match one of our known ID strings */
163 if (strcmp(ea->ea_idstring, "ADP0000") &&
164 strcmp(ea->ea_idstring, "ADP0001") &&
165 strcmp(ea->ea_idstring, "ADP0002") &&
166 strcmp(ea->ea_idstring, "ADP0400"))
167 return (0);
168
169 if (bus_space_map(iot,
170 EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET, AHB_EISA_IOSIZE,
171 0, &ioh))
172 return (0);
173
174 rv = !ahb_find(iot, ioh, NULL);
175
176 bus_space_unmap(iot, ioh, AHB_EISA_IOSIZE);
177
178 return (rv);
179 }
180
181 /*
182 * Attach all the sub-devices we can find
183 */
184 static void
185 ahbattach(struct device *parent, struct device *self, void *aux)
186 {
187 struct eisa_attach_args *ea = aux;
188 struct ahb_softc *sc = device_private(self);
189 bus_space_tag_t iot = ea->ea_iot;
190 bus_space_handle_t ioh;
191 eisa_chipset_tag_t ec = ea->ea_ec;
192 eisa_intr_handle_t ih;
193 const char *model, *intrstr;
194 struct ahb_probe_data apd;
195 struct scsipi_adapter *adapt = &sc->sc_adapter;
196 struct scsipi_channel *chan = &sc->sc_channel;
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, AHB_EISA_IOSIZE,
212 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);
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 printf(" at %s", intrstr);
262 printf("\n");
263 return;
264 }
265 if (intrstr != NULL)
266 printf("%s: interrupting at %s\n", device_xname(&sc->sc_dev),
267 intrstr);
268
269 /*
270 * ask the adapter what subunits are present
271 */
272 config_found(self, &sc->sc_channel, scsiprint);
273 }
274
275 /*
276 * Function to send a command out through a mailbox
277 */
278 static void
279 ahb_send_mbox(struct ahb_softc *sc, int opcode, struct ahb_ecb *ecb)
280 {
281 bus_space_tag_t iot = sc->sc_iot;
282 bus_space_handle_t ioh = sc->sc_ioh;
283 int wait = 300; /* 1ms should be enough */
284
285 while (--wait) {
286 if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
287 == (G2STAT_MBOX_EMPTY))
288 break;
289 delay(10);
290 }
291 if (!wait) {
292 printf("%s: board not responding\n", device_xname(&sc->sc_dev));
293 Debugger();
294 }
295
296 /*
297 * don't know if this will work.
298 * XXX WHAT DOES THIS COMMENT MEAN?! --thorpej
299 */
300 bus_space_write_4(iot, ioh, MBOXOUT0,
301 sc->sc_dmamap_ecb->dm_segs[0].ds_addr + AHB_ECB_OFF(ecb));
302 bus_space_write_1(iot, ioh, ATTN, opcode |
303 ecb->xs->xs_periph->periph_target);
304
305 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
306 callout_reset(&ecb->xs->xs_callout,
307 mstohz(ecb->timeout), ahb_timeout, ecb);
308 }
309
310 /*
311 * Function to send an immediate type command to the adapter
312 */
313 static void
314 ahb_send_immed(struct ahb_softc *sc, u_int32_t cmd, struct ahb_ecb *ecb)
315 {
316 bus_space_tag_t iot = sc->sc_iot;
317 bus_space_handle_t ioh = sc->sc_ioh;
318 int wait = 100; /* 1 ms enough? */
319
320 while (--wait) {
321 if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
322 == (G2STAT_MBOX_EMPTY))
323 break;
324 delay(10);
325 }
326 if (!wait) {
327 printf("%s: board not responding\n", device_xname(&sc->sc_dev));
328 Debugger();
329 }
330
331 bus_space_write_4(iot, ioh, MBOXOUT0, cmd); /* don't know this will work */
332 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
333 bus_space_write_1(iot, ioh, ATTN, OP_IMMED |
334 ecb->xs->xs_periph->periph_target);
335
336 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
337 callout_reset(&ecb->xs->xs_callout,
338 mstohz(ecb->timeout), ahb_timeout, ecb);
339 }
340
341 /*
342 * Catch an interrupt from the adaptor
343 */
344 static int
345 ahbintr(void *arg)
346 {
347 struct ahb_softc *sc = arg;
348 bus_space_tag_t iot = sc->sc_iot;
349 bus_space_handle_t ioh = sc->sc_ioh;
350 struct ahb_ecb *ecb;
351 u_char ahbstat;
352 u_int32_t mboxval;
353
354 #ifdef AHBDEBUG
355 printf("%s: ahbintr ", device_xname(&sc->sc_dev));
356 #endif /* AHBDEBUG */
357
358 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
359 return 0;
360
361 for (;;) {
362 /*
363 * First get all the information and then
364 * acknowledge the interrupt
365 */
366 ahbstat = bus_space_read_1(iot, ioh, G2INTST);
367 mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
368 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
369
370 #ifdef AHBDEBUG
371 printf("status = 0x%x ", ahbstat);
372 #endif /* AHBDEBUG */
373
374 /*
375 * Process the completed operation
376 */
377 switch (ahbstat & G2INTST_INT_STAT) {
378 case AHB_ECB_OK:
379 case AHB_ECB_RECOVERED:
380 case AHB_ECB_ERR:
381 ecb = ahb_ecb_phys_kv(sc, mboxval);
382 if (!ecb) {
383 aprint_error_dev(&sc->sc_dev, "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, "unexpected interrupt %x\n",
401 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 bzero(ecbstore, 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, "unable to initialize ecb, error = %d\n",
479 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, struct ahb_probe_data *sc)
609 {
610 u_char intdef;
611 int i, irq, busid;
612 int wait = 1000; /* 1 sec enough? */
613
614 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
615
616 #define NO_NO 1
617 #ifdef NO_NO
618 /*
619 * reset board, If it doesn't respond, assume
620 * that it's not there.. good for the probe
621 */
622 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
623 delay(1000);
624 bus_space_write_1(iot, ioh, G2CNTRL, 0);
625 delay(10000);
626 while (--wait) {
627 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
628 break;
629 delay(1000);
630 }
631 if (!wait) {
632 #ifdef AHBDEBUG
633 printf("ahb_find: No answer from aha1742 board\n");
634 #endif /* AHBDEBUG */
635 return ENXIO;
636 }
637 i = bus_space_read_1(iot, ioh, MBOXIN0);
638 if (i) {
639 printf("self test failed, val = 0x%x\n", i);
640 return EIO;
641 }
642
643 /* Set it again, just to be sure. */
644 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
645 #endif
646
647 while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
648 printf(".");
649 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
650 delay(10000);
651 }
652
653 intdef = bus_space_read_1(iot, ioh, INTDEF);
654 switch (intdef & 0x07) {
655 case INT9:
656 irq = 9;
657 break;
658 case INT10:
659 irq = 10;
660 break;
661 case INT11:
662 irq = 11;
663 break;
664 case INT12:
665 irq = 12;
666 break;
667 case INT14:
668 irq = 14;
669 break;
670 case INT15:
671 irq = 15;
672 break;
673 default:
674 printf("illegal int setting %x\n", intdef);
675 return EIO;
676 }
677
678 bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN)); /* make sure we can interrupt */
679
680 /* who are we on the scsi bus? */
681 busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
682
683 /* if we want to return data, do so now */
684 if (sc) {
685 sc->sc_irq = irq;
686 sc->sc_scsi_dev = busid;
687 }
688
689 /*
690 * Note that we are going and return (to probe)
691 */
692 return 0;
693 }
694
695 static int
696 ahb_init(struct ahb_softc *sc)
697 {
698 bus_dma_segment_t seg;
699 int i, error, rseg;
700
701 #define ECBSIZE (AHB_ECB_MAX * sizeof(struct ahb_ecb))
702
703 /*
704 * Allocate the ECBs.
705 */
706 if ((error = bus_dmamem_alloc(sc->sc_dmat, ECBSIZE,
707 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
708 aprint_error_dev(&sc->sc_dev, "unable to allocate ecbs, error = %d\n",
709 error);
710 return (error);
711 }
712 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
713 ECBSIZE, (void **)&sc->sc_ecbs,
714 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
715 aprint_error_dev(&sc->sc_dev, "unable to map ecbs, error = %d\n",
716 error);
717 return (error);
718 }
719
720 /*
721 * Create and load the DMA map used for the ecbs.
722 */
723 if ((error = bus_dmamap_create(sc->sc_dmat, ECBSIZE,
724 1, ECBSIZE, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_ecb)) != 0) {
725 aprint_error_dev(&sc->sc_dev, "unable to create ecb DMA map, error = %d\n",
726 error);
727 return (error);
728 }
729 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ecb,
730 sc->sc_ecbs, ECBSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
731 aprint_error_dev(&sc->sc_dev, "unable to load ecb DMA map, error = %d\n",
732 error);
733 return (error);
734 }
735
736 #undef ECBSIZE
737
738 /*
739 * Initialize the ecbs.
740 */
741 i = ahb_create_ecbs(sc, sc->sc_ecbs, AHB_ECB_MAX);
742 if (i == 0) {
743 aprint_error_dev(&sc->sc_dev, "unable to create ecbs\n");
744 return (ENOMEM);
745 } else if (i != AHB_ECB_MAX) {
746 printf("%s: WARNING: only %d of %d ecbs created\n",
747 device_xname(&sc->sc_dev), i, AHB_ECB_MAX);
748 }
749
750 sc->sc_adapter.adapt_openings = i;
751
752 return (0);
753 }
754
755 static void
756 ahbminphys(struct buf *bp)
757 {
758
759 if (bp->b_bcount > AHB_MAXXFER)
760 bp->b_bcount = AHB_MAXXFER;
761 minphys(bp);
762 }
763
764 /*
765 * start a scsi operation given the command and the data address. Also needs
766 * the unit, target and lu.
767 */
768 static void
769 ahb_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
770 void *arg)
771 {
772 struct scsipi_xfer *xs;
773 struct scsipi_periph *periph;
774 struct ahb_softc *sc = (void *)chan->chan_adapter->adapt_dev;
775 bus_dma_tag_t dmat = sc->sc_dmat;
776 struct ahb_ecb *ecb;
777 int error, seg, flags, s;
778
779 switch (req) {
780 case ADAPTER_REQ_RUN_XFER:
781 xs = arg;
782 periph = xs->xs_periph;
783 flags = xs->xs_control;
784
785 SC_DEBUG(periph, SCSIPI_DB2, ("ahb_scsipi_request\n"));
786
787 /* Get an ECB to use. */
788 ecb = ahb_get_ecb(sc);
789 #ifdef DIAGNOSTIC
790 /*
791 * This should never happen as we track the resources
792 * in the mid-layer.
793 */
794 if (ecb == NULL) {
795 scsipi_printaddr(periph);
796 printf("unable to allocate ecb\n");
797 panic("ahb_scsipi_request");
798 }
799 #endif
800
801 ecb->xs = xs;
802 ecb->timeout = xs->timeout;
803
804 /*
805 * If it's a reset, we need to do an 'immediate'
806 * command, and store its ecb for later
807 * if there is already an immediate waiting,
808 * then WE must wait
809 */
810 if (flags & XS_CTL_RESET) {
811 ecb->flags |= ECB_IMMED;
812 if (sc->sc_immed_ecb) {
813 ahb_free_ecb(sc, ecb);
814 xs->error = XS_BUSY;
815 scsipi_done(xs);
816 return;
817 }
818 sc->sc_immed_ecb = ecb;
819
820 s = splbio();
821 ahb_send_immed(sc, AHB_TARG_RESET, ecb);
822 splx(s);
823
824 if ((flags & XS_CTL_POLL) == 0)
825 return;
826
827 /*
828 * If we can't use interrupts, poll on completion
829 */
830 if (ahb_poll(sc, xs, ecb->timeout))
831 ahb_timeout(ecb);
832 return;
833 }
834
835 /*
836 * Put all the arguments for the xfer in the ecb
837 */
838 if (xs->cmdlen > sizeof(ecb->scsi_cmd)) {
839 aprint_error_dev(&sc->sc_dev, "cmdlen %d too large for ECB\n",
840 xs->cmdlen);
841 xs->error = XS_DRIVER_STUFFUP;
842 goto out_bad;
843 }
844 ecb->opcode = ECB_SCSI_OP;
845 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
846 ecb->opt2 = periph->periph_lun | ECB_NRB;
847 bcopy(xs->cmd, &ecb->scsi_cmd,
848 ecb->scsi_cmd_length = xs->cmdlen);
849 ecb->sense_ptr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
850 AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_sense);
851 ecb->req_sense_length = sizeof(ecb->ecb_sense);
852 ecb->status = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
853 AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_status);
854 ecb->ecb_status.host_stat = 0x00;
855 ecb->ecb_status.target_stat = 0x00;
856
857 if (xs->datalen) {
858 /*
859 * Map the DMA transfer.
860 */
861 #ifdef TFS
862 if (flags & XS_CTL_DATA_UIO) {
863 error = bus_dmamap_load_uio(sc->sc_dmat,
864 ecb->dmamap_xfer, (struct uio *)xs->data,
865 BUS_DMA_NOWAIT);
866 } else
867 #endif /* TFS */
868 {
869 error = bus_dmamap_load(sc->sc_dmat,
870 ecb->dmamap_xfer, xs->data, xs->datalen,
871 NULL, BUS_DMA_NOWAIT);
872 }
873
874 switch (error) {
875 case 0:
876 break;
877
878 case ENOMEM:
879 case EAGAIN:
880 xs->error = XS_RESOURCE_SHORTAGE;
881 goto out_bad;
882
883 default:
884 xs->error = XS_DRIVER_STUFFUP;
885 aprint_error_dev(&sc->sc_dev, "error %d loading DMA map\n",
886 error);
887 out_bad:
888 ahb_free_ecb(sc, ecb);
889 scsipi_done(xs);
890 return;
891 }
892
893 bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
894 ecb->dmamap_xfer->dm_mapsize,
895 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
896 BUS_DMASYNC_PREWRITE);
897
898 /*
899 * Load the hardware scatter/gather map with the
900 * contents of the DMA map.
901 */
902 for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
903 ecb->ahb_dma[seg].seg_addr =
904 ecb->dmamap_xfer->dm_segs[seg].ds_addr;
905 ecb->ahb_dma[seg].seg_len =
906 ecb->dmamap_xfer->dm_segs[seg].ds_len;
907 }
908
909 ecb->data_addr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
910 AHB_ECB_OFF(ecb) +
911 offsetof(struct ahb_ecb, ahb_dma);
912 ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
913 sizeof(struct ahb_dma_seg);
914 ecb->opt1 |= ECB_S_G;
915 } else { /* No data xfer, use non S/G values */
916 ecb->data_addr = (physaddr)0;
917 ecb->data_length = 0;
918 }
919 ecb->link_addr = (physaddr)0;
920
921 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ecb,
922 AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
923 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
924
925 s = splbio();
926 ahb_send_mbox(sc, OP_START_ECB, ecb);
927 splx(s);
928
929 if ((flags & XS_CTL_POLL) == 0)
930 return;
931
932 /*
933 * If we can't use interrupts, poll on completion
934 */
935 if (ahb_poll(sc, xs, ecb->timeout)) {
936 ahb_timeout(ecb);
937 if (ahb_poll(sc, xs, ecb->timeout))
938 ahb_timeout(ecb);
939 }
940 return;
941
942 case ADAPTER_REQ_GROW_RESOURCES:
943 /* XXX Not supported. */
944 return;
945
946 case ADAPTER_REQ_SET_XFER_MODE:
947 /* XXX How do we do this? */
948 return;
949 }
950 }
951
952 /*
953 * Function to poll for command completion when in poll mode
954 */
955 static int
956 ahb_poll(struct ahb_softc *sc, struct scsipi_xfer *xs, int count)
957 { /* in msec */
958 bus_space_tag_t iot = sc->sc_iot;
959 bus_space_handle_t ioh = sc->sc_ioh;
960
961 while (count) {
962 /*
963 * If we had interrupts enabled, would we
964 * have got an interrupt?
965 */
966 if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
967 ahbintr(sc);
968 if (xs->xs_status & XS_STS_DONE)
969 return 0;
970 delay(1000);
971 count--;
972 }
973 return 1;
974 }
975
976 static void
977 ahb_timeout(void *arg)
978 {
979 struct ahb_ecb *ecb = arg;
980 struct scsipi_xfer *xs = ecb->xs;
981 struct scsipi_periph *periph = xs->xs_periph;
982 struct ahb_softc *sc =
983 (void *)periph->periph_channel->chan_adapter->adapt_dev;
984 int s;
985
986 scsipi_printaddr(periph);
987 printf("timed out");
988
989 s = splbio();
990
991 if (ecb->flags & ECB_IMMED) {
992 printf("\n");
993 ecb->flags |= ECB_IMMED_FAIL;
994 /* XXX Must reset! */
995 } else
996
997 /*
998 * If it has been through before, then
999 * a previous abort has failed, don't
1000 * try abort again
1001 */
1002 if (ecb->flags & ECB_ABORT) {
1003 /* abort timed out */
1004 printf(" AGAIN\n");
1005 /* XXX Must reset! */
1006 } else {
1007 /* abort the operation that has timed out */
1008 printf("\n");
1009 ecb->xs->error = XS_TIMEOUT;
1010 ecb->timeout = AHB_ABORT_TIMEOUT;
1011 ecb->flags |= ECB_ABORT;
1012 ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
1013 }
1014
1015 splx(s);
1016 }
Cache object: 2809a22eb60fdc47dc5f8ea2dc833fa6
|