1 /* $NetBSD: ahb.c,v 1.64 2016/07/14 04:00:45 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@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.64 2016/07/14 04:00:45 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, void *aux) 152 { 153 struct eisa_attach_args *ea = aux; 154 bus_space_tag_t iot = ea->ea_iot; 155 bus_space_handle_t ioh; 156 int rv; 157 158 /* must match one of our known ID strings */ 159 if (strcmp(ea->ea_idstring, "ADP0000") && 160 strcmp(ea->ea_idstring, "ADP0001") && 161 strcmp(ea->ea_idstring, "ADP0002") && 162 strcmp(ea->ea_idstring, "ADP0400")) 163 return (0); 164 165 if (bus_space_map(iot, 166 EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET, 167 AHB_EISA_IOSIZE, 0, &ioh)) 168 return (0); 169 170 rv = !ahb_find(iot, ioh, NULL); 171 172 bus_space_unmap(iot, ioh, AHB_EISA_IOSIZE); 173 174 return (rv); 175 } 176 177 /* 178 * Attach all the sub-devices we can find 179 */ 180 static void 181 ahbattach(device_t parent, device_t self, void *aux) 182 { 183 struct eisa_attach_args *ea = aux; 184 struct ahb_softc *sc = device_private(self); 185 bus_space_tag_t iot = ea->ea_iot; 186 bus_space_handle_t ioh; 187 eisa_chipset_tag_t ec = ea->ea_ec; 188 eisa_intr_handle_t ih; 189 const char *model, *intrstr; 190 struct ahb_probe_data apd; 191 struct scsipi_adapter *adapt = &sc->sc_adapter; 192 struct scsipi_channel *chan = &sc->sc_channel; 193 char intrbuf[EISA_INTRSTR_LEN]; 194 195 sc->sc_dev = self; 196 197 if (!strcmp(ea->ea_idstring, "ADP0000")) 198 model = EISA_PRODUCT_ADP0000; 199 else if (!strcmp(ea->ea_idstring, "ADP0001")) 200 model = EISA_PRODUCT_ADP0001; 201 else if (!strcmp(ea->ea_idstring, "ADP0002")) 202 model = EISA_PRODUCT_ADP0002; 203 else if (!strcmp(ea->ea_idstring, "ADP0400")) 204 model = EISA_PRODUCT_ADP0400; 205 else 206 model = "unknown model!"; 207 aprint_naive("\n"); 208 aprint_normal(": %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), 578 ecb->ecb_status.host_stat); 579 xs->error = XS_DRIVER_STUFFUP; 580 } 581 } else if (ecb->ecb_status.target_stat != SCSI_OK) { 582 switch (ecb->ecb_status.target_stat) { 583 case SCSI_CHECK: 584 s1 = &ecb->ecb_sense; 585 s2 = &xs->sense.scsi_sense; 586 *s2 = *s1; 587 xs->error = XS_SENSE; 588 break; 589 case SCSI_BUSY: 590 xs->error = XS_BUSY; 591 break; 592 default: 593 printf("%s: target_stat %x\n", 594 device_xname(sc->sc_dev), 595 ecb->ecb_status.target_stat); 596 xs->error = XS_DRIVER_STUFFUP; 597 } 598 } else 599 xs->resid = 0; 600 } 601 done: 602 ahb_free_ecb(sc, ecb); 603 scsipi_done(xs); 604 } 605 606 /* 607 * Start the board, ready for normal operation 608 */ 609 static int 610 ahb_find(bus_space_tag_t iot, bus_space_handle_t ioh, 611 struct ahb_probe_data *sc) 612 { 613 u_char intdef; 614 int i, irq, busid; 615 int wait = 1000; /* 1 sec enough? */ 616 617 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED); 618 619 #define NO_NO 1 620 #ifdef NO_NO 621 /* 622 * reset board, If it doesn't respond, assume 623 * that it's not there.. good for the probe 624 */ 625 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET); 626 delay(1000); 627 bus_space_write_1(iot, ioh, G2CNTRL, 0); 628 delay(10000); 629 while (--wait) { 630 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0) 631 break; 632 delay(1000); 633 } 634 if (!wait) { 635 #ifdef AHBDEBUG 636 printf("ahb_find: No answer from aha1742 board\n"); 637 #endif /* AHBDEBUG */ 638 return ENXIO; 639 } 640 i = bus_space_read_1(iot, ioh, MBOXIN0); 641 if (i) { 642 printf("self test failed, val = 0x%x\n", i); 643 return EIO; 644 } 645 646 /* Set it again, just to be sure. */ 647 bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED); 648 #endif 649 650 while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) { 651 printf("."); 652 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT); 653 delay(10000); 654 } 655 656 intdef = bus_space_read_1(iot, ioh, INTDEF); 657 switch (intdef & 0x07) { 658 case INT9: 659 irq = 9; 660 break; 661 case INT10: 662 irq = 10; 663 break; 664 case INT11: 665 irq = 11; 666 break; 667 case INT12: 668 irq = 12; 669 break; 670 case INT14: 671 irq = 14; 672 break; 673 case INT15: 674 irq = 15; 675 break; 676 default: 677 printf("illegal int setting %x\n", intdef); 678 return EIO; 679 } 680 681 bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN)); /* make sure we can interrupt */ 682 683 /* who are we on the scsi bus? */ 684 busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID); 685 686 /* if we want to return data, do so now */ 687 if (sc) { 688 sc->sc_irq = irq; 689 sc->sc_scsi_dev = busid; 690 } 691 692 /* 693 * Note that we are going and return (to probe) 694 */ 695 return 0; 696 } 697 698 static int 699 ahb_init(struct ahb_softc *sc) 700 { 701 bus_dma_segment_t seg; 702 int i, error, rseg; 703 704 #define ECBSIZE (AHB_ECB_MAX * sizeof(struct ahb_ecb)) 705 706 /* 707 * Allocate the ECBs. 708 */ 709 if ((error = bus_dmamem_alloc(sc->sc_dmat, ECBSIZE, 710 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) { 711 aprint_error_dev(sc->sc_dev, 712 "unable to allocate ecbs, error = %d\n", error); 713 return (error); 714 } 715 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, 716 ECBSIZE, (void **)&sc->sc_ecbs, 717 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) { 718 aprint_error_dev(sc->sc_dev, 719 "unable to map ecbs, error = %d\n", error); 720 return (error); 721 } 722 723 /* 724 * Create and load the DMA map used for the ecbs. 725 */ 726 if ((error = bus_dmamap_create(sc->sc_dmat, ECBSIZE, 727 1, ECBSIZE, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_ecb)) != 0) { 728 aprint_error_dev(sc->sc_dev, 729 "unable to create ecb DMA map, error = %d\n", error); 730 return (error); 731 } 732 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ecb, 733 sc->sc_ecbs, ECBSIZE, NULL, BUS_DMA_NOWAIT)) != 0) { 734 aprint_error_dev(sc->sc_dev, 735 "unable to load ecb DMA map, error = %d\n", error); 736 return (error); 737 } 738 739 #undef ECBSIZE 740 741 /* 742 * Initialize the ecbs. 743 */ 744 i = ahb_create_ecbs(sc, sc->sc_ecbs, AHB_ECB_MAX); 745 if (i == 0) { 746 aprint_error_dev(sc->sc_dev, "unable to create ecbs\n"); 747 return (ENOMEM); 748 } else if (i != AHB_ECB_MAX) { 749 printf("%s: WARNING: only %d of %d ecbs created\n", 750 device_xname(sc->sc_dev), i, AHB_ECB_MAX); 751 } 752 753 sc->sc_adapter.adapt_openings = i; 754 755 return (0); 756 } 757 758 static void 759 ahbminphys(struct buf *bp) 760 { 761 762 if (bp->b_bcount > AHB_MAXXFER) 763 bp->b_bcount = AHB_MAXXFER; 764 minphys(bp); 765 } 766 767 /* 768 * start a scsi operation given the command and the data address. Also needs 769 * the unit, target and lu. 770 */ 771 static void 772 ahb_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, 773 void *arg) 774 { 775 struct scsipi_xfer *xs; 776 struct scsipi_periph *periph; 777 struct ahb_softc *sc = device_private(chan->chan_adapter->adapt_dev); 778 bus_dma_tag_t dmat = sc->sc_dmat; 779 struct ahb_ecb *ecb; 780 int error, seg, flags, s; 781 782 switch (req) { 783 case ADAPTER_REQ_RUN_XFER: 784 xs = arg; 785 periph = xs->xs_periph; 786 flags = xs->xs_control; 787 788 SC_DEBUG(periph, SCSIPI_DB2, ("ahb_scsipi_request\n")); 789 790 /* Get an ECB to use. */ 791 ecb = ahb_get_ecb(sc); 792 #ifdef DIAGNOSTIC 793 /* 794 * This should never happen as we track the resources 795 * in the mid-layer. 796 */ 797 if (ecb == NULL) { 798 scsipi_printaddr(periph); 799 printf("unable to allocate ecb\n"); 800 panic("ahb_scsipi_request"); 801 } 802 #endif 803 804 ecb->xs = xs; 805 ecb->timeout = xs->timeout; 806 807 /* 808 * If it's a reset, we need to do an 'immediate' 809 * command, and store its ecb for later 810 * if there is already an immediate waiting, 811 * then WE must wait 812 */ 813 if (flags & XS_CTL_RESET) { 814 ecb->flags |= ECB_IMMED; 815 if (sc->sc_immed_ecb) { 816 ahb_free_ecb(sc, ecb); 817 xs->error = XS_BUSY; 818 scsipi_done(xs); 819 return; 820 } 821 sc->sc_immed_ecb = ecb; 822 823 s = splbio(); 824 ahb_send_immed(sc, AHB_TARG_RESET, ecb); 825 splx(s); 826 827 if ((flags & XS_CTL_POLL) == 0) 828 return; 829 830 /* 831 * If we can't use interrupts, poll on completion 832 */ 833 if (ahb_poll(sc, xs, ecb->timeout)) 834 ahb_timeout(ecb); 835 return; 836 } 837 838 /* 839 * Put all the arguments for the xfer in the ecb 840 */ 841 if (xs->cmdlen > sizeof(ecb->scsi_cmd)) { 842 aprint_error_dev(sc->sc_dev, 843 "cmdlen %d too large for ECB\n", xs->cmdlen); 844 xs->error = XS_DRIVER_STUFFUP; 845 goto out_bad; 846 } 847 ecb->opcode = ECB_SCSI_OP; 848 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS; 849 ecb->opt2 = periph->periph_lun | ECB_NRB; 850 memcpy(&ecb->scsi_cmd, xs->cmd, 851 ecb->scsi_cmd_length = xs->cmdlen); 852 ecb->sense_ptr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr + 853 AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_sense); 854 ecb->req_sense_length = sizeof(ecb->ecb_sense); 855 ecb->status = sc->sc_dmamap_ecb->dm_segs[0].ds_addr + 856 AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_status); 857 ecb->ecb_status.host_stat = 0x00; 858 ecb->ecb_status.target_stat = 0x00; 859 860 if (xs->datalen) { 861 /* 862 * Map the DMA transfer. 863 */ 864 #ifdef TFS 865 if (flags & XS_CTL_DATA_UIO) { 866 error = bus_dmamap_load_uio(sc->sc_dmat, 867 ecb->dmamap_xfer, (struct uio *)xs->data, 868 BUS_DMA_NOWAIT); 869 } else 870 #endif /* TFS */ 871 { 872 error = bus_dmamap_load(sc->sc_dmat, 873 ecb->dmamap_xfer, xs->data, xs->datalen, 874 NULL, BUS_DMA_NOWAIT); 875 } 876 877 switch (error) { 878 case 0: 879 break; 880 881 case ENOMEM: 882 case EAGAIN: 883 xs->error = XS_RESOURCE_SHORTAGE; 884 goto out_bad; 885 886 default: 887 xs->error = XS_DRIVER_STUFFUP; 888 aprint_error_dev(sc->sc_dev, 889 "error %d loading DMA map\n", error); 890 out_bad: 891 ahb_free_ecb(sc, ecb); 892 scsipi_done(xs); 893 return; 894 } 895 896 bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0, 897 ecb->dmamap_xfer->dm_mapsize, 898 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD : 899 BUS_DMASYNC_PREWRITE); 900 901 /* 902 * Load the hardware scatter/gather map with the 903 * contents of the DMA map. 904 */ 905 for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) { 906 ecb->ahb_dma[seg].seg_addr = 907 ecb->dmamap_xfer->dm_segs[seg].ds_addr; 908 ecb->ahb_dma[seg].seg_len = 909 ecb->dmamap_xfer->dm_segs[seg].ds_len; 910 } 911 912 ecb->data_addr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr + 913 AHB_ECB_OFF(ecb) + 914 offsetof(struct ahb_ecb, ahb_dma); 915 ecb->data_length = ecb->dmamap_xfer->dm_nsegs * 916 sizeof(struct ahb_dma_seg); 917 ecb->opt1 |= ECB_S_G; 918 } else { /* No data xfer, use non S/G values */ 919 ecb->data_addr = (physaddr)0; 920 ecb->data_length = 0; 921 } 922 ecb->link_addr = (physaddr)0; 923 924 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ecb, 925 AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb), 926 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 927 928 s = splbio(); 929 ahb_send_mbox(sc, OP_START_ECB, ecb); 930 splx(s); 931 932 if ((flags & XS_CTL_POLL) == 0) 933 return; 934 935 /* 936 * If we can't use interrupts, poll on completion 937 */ 938 if (ahb_poll(sc, xs, ecb->timeout)) { 939 ahb_timeout(ecb); 940 if (ahb_poll(sc, xs, ecb->timeout)) 941 ahb_timeout(ecb); 942 } 943 return; 944 945 case ADAPTER_REQ_GROW_RESOURCES: 946 /* XXX Not supported. */ 947 return; 948 949 case ADAPTER_REQ_SET_XFER_MODE: 950 /* XXX How do we do this? */ 951 return; 952 } 953 } 954 955 /* 956 * Function to poll for command completion when in poll mode 957 */ 958 static int 959 ahb_poll(struct ahb_softc *sc, struct scsipi_xfer *xs, int count) 960 { /* in msec */ 961 bus_space_tag_t iot = sc->sc_iot; 962 bus_space_handle_t ioh = sc->sc_ioh; 963 964 while (count) { 965 /* 966 * If we had interrupts enabled, would we 967 * have got an interrupt? 968 */ 969 if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) 970 ahbintr(sc); 971 if (xs->xs_status & XS_STS_DONE) 972 return 0; 973 delay(1000); 974 count--; 975 } 976 return 1; 977 } 978 979 static void 980 ahb_timeout(void *arg) 981 { 982 struct ahb_ecb *ecb = arg; 983 struct scsipi_xfer *xs = ecb->xs; 984 struct scsipi_periph *periph = xs->xs_periph; 985 struct ahb_softc *sc = 986 device_private(periph->periph_channel->chan_adapter->adapt_dev); 987 int s; 988 989 scsipi_printaddr(periph); 990 printf("timed out"); 991 992 s = splbio(); 993 994 if (ecb->flags & ECB_IMMED) { 995 printf("\n"); 996 ecb->flags |= ECB_IMMED_FAIL; 997 /* XXX Must reset! */ 998 } else 999 1000 /* 1001 * If it has been through before, then 1002 * a previous abort has failed, don't 1003 * try abort again 1004 */ 1005 if (ecb->flags & ECB_ABORT) { 1006 /* abort timed out */ 1007 printf(" AGAIN\n"); 1008 /* XXX Must reset! */ 1009 } else { 1010 /* abort the operation that has timed out */ 1011 printf("\n"); 1012 ecb->xs->error = XS_TIMEOUT; 1013 ecb->timeout = AHB_ABORT_TIMEOUT; 1014 ecb->flags |= ECB_ABORT; 1015 ahb_send_mbox(sc, OP_ABORT_ECB, ecb); 1016 } 1017 1018 splx(s); 1019 } 1020