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