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