1 /* 2 * Copyright (c) 1986 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)ik.c 7.3 (Berkeley) 05/05/89 18 */ 19 20 #include "ik.h" 21 #if NIK > 0 22 /* 23 * PS300/IKON DR-11W Device Driver. 24 */ 25 #include "param.h" 26 #include "buf.h" 27 #include "cmap.h" 28 #include "conf.h" 29 #include "dkstat.h" 30 #include "map.h" 31 #include "systm.h" 32 #include "user.h" 33 #include "vmmac.h" 34 #include "proc.h" 35 #include "kernel.h" 36 #include "syslog.h" 37 38 #include "../tahoe/mtpr.h" 39 #include "../tahoe/pte.h" 40 41 #include "../tahoevba/vbavar.h" 42 #include "../tahoevba/ikreg.h" 43 #include "../tahoevba/psreg.h" 44 #include "../tahoevba/psproto.h" 45 46 int ikprobe(), ikattach(), iktimer(); 47 struct vba_device *ikinfo[NIK]; 48 long ikstd[] = { 0 }; 49 struct vba_driver ikdriver = { ikprobe, 0, ikattach, 0, ikstd, "ik", ikinfo }; 50 51 #define splik() spl4() 52 /* 53 * Devices are organized in pairs with the odd valued 54 * device being used for ``diagnostic'' purposes. That 55 * is diagnostic devices don't get auto-attach'd and 56 * detach'd on open-close. 57 */ 58 #define IKUNIT(dev) (minor(dev) >> 1) 59 #define IKDIAG(dev) (minor(dev) & 01) /* is a diagnostic unit */ 60 61 struct ik_softc { 62 uid_t is_uid; /* uid of open processes */ 63 u_short is_timeout; /* current timeout (seconds) */ 64 u_short is_error; /* internal error codes */ 65 u_short is_flags; 66 #define IKF_ATTACHED 0x1 /* unit is attached (not used yet) */ 67 union { 68 u_short w[2]; 69 u_long l; 70 } is_nameaddr; /* address of last symbol lookup */ 71 caddr_t is_buf[PS_MAXDMA];/* i/o buffer XXX */ 72 } ik_softc[NIK]; 73 74 struct buf iktab[NIK]; /* unit command queue headers */ 75 struct buf rikbuf[NIK]; /* buffers for read/write operations */ 76 struct buf cikbuf[NIK]; /* buffers for control operations */ 77 78 /* buf overlay definitions */ 79 #define b_command b_resid 80 81 int ikdiotimo = PS_DIOTIMO; /* dio polling timeout */ 82 int iktimeout = PS_TIMEOUT; /* attention/dma timeout (in hz) */ 83 84 ikprobe(reg, vi) 85 caddr_t reg; 86 struct vba_device *vi; 87 { 88 register int br, cvec; /* r12, r11 */ 89 register struct ikdevice *ik; 90 91 #ifdef lint 92 br = 0; cvec = br; br = cvec; 93 ikintr(0); 94 #endif 95 if (badaddr(reg, 2)) 96 return (0); 97 ik = (struct ikdevice *)reg; 98 ik->ik_vec = --vi->ui_hd->vh_lastiv; 99 /* 100 * Use extended non-privileged address modifier 101 * to avoid address overlap with 24-bit devices. 102 */ 103 ik->ik_mod = 0xf1; /* address modifier */ 104 /* 105 * Try and reset the PS300. Since this 106 * won't work if it's powered off, we 107 * can't use sucess/failure to decide 108 * if the device is present. 109 */ 110 br = 0; 111 (void) psreset(ik, IKCSR_IENA); 112 if (br == 0) /* XXX */ 113 br = 0x18, cvec = ik->ik_vec; /* XXX */ 114 return (sizeof (struct ikdevice)); 115 } 116 117 /* 118 * Perform a ``hard'' reset. 119 */ 120 psreset(ik, iena) 121 register struct ikdevice *ik; 122 { 123 124 ik->ik_csr = IKCSR_MCLR|iena; 125 DELAY(10000); 126 ik->ik_csr = IKCSR_FNC3|iena; 127 if (!iena) 128 return (dioread(ik) == PS_RESET); 129 return (1); 130 } 131 132 ikattach(vi) 133 struct vba_device *vi; 134 { 135 136 ik_softc[vi->ui_unit].is_uid = -1; 137 } 138 139 /* 140 * Open a PS300 and attach. We allow multiple 141 * processes with the same uid to share a unit. 142 */ 143 /*ARGSUSED*/ 144 ikopen(dev, flag) 145 dev_t dev; 146 int flag; 147 { 148 register int unit = IKUNIT(dev); 149 register struct ik_softc *sc; 150 struct vba_device *vi; 151 struct ikdevice *ik; 152 int reset; 153 154 if (unit >= NIK || (vi = ikinfo[unit]) == 0 || vi->ui_alive == 0) 155 return (ENXIO); 156 sc = &ik_softc[unit]; 157 if (sc->is_uid != (uid_t)-1 && sc->is_uid != u.u_uid) 158 return (EBUSY); 159 if (sc->is_uid == (uid_t)-1) { 160 sc->is_timeout = 0; 161 timeout(iktimer, (caddr_t)unit, hz); 162 /* 163 * Perform PS300 attach for first process. 164 */ 165 if (!IKDIAG(dev)) { 166 reset = 0; 167 again: 168 if (ikcommand(dev, PS_ATTACH, 1)) { 169 /* 170 * If attach fails, perform a hard 171 * reset once, then retry the command. 172 */ 173 ik = (struct ikdevice *)ikinfo[unit]->ui_addr; 174 if (!reset++ && psreset(ik, 0)) 175 goto again; 176 untimeout(iktimer, (caddr_t)unit); 177 return (EIO); 178 } 179 } 180 sc->is_uid = u.u_uid; 181 } 182 return (0); 183 } 184 185 /*ARGSUSED*/ 186 ikclose(dev, flag) 187 dev_t dev; 188 int flag; 189 { 190 int unit = IKUNIT(dev); 191 register struct ik_softc *sc = &ik_softc[unit]; 192 193 if (!IKDIAG(dev)) 194 (void) ikcommand(dev, PS_DETACH, 1); /* auto detach */ 195 sc->is_uid = -1; 196 untimeout(iktimer, (caddr_t)unit); 197 } 198 199 ikread(dev, uio) 200 dev_t dev; 201 struct uio *uio; 202 { 203 204 return (ikrw(dev, uio, B_READ)); 205 } 206 207 ikwrite(dev, uio) 208 dev_t dev; 209 struct uio *uio; 210 { 211 212 return (ikrw(dev, uio, B_WRITE)); 213 } 214 215 /* 216 * Take read/write request and perform physical i/o 217 * transaction with PS300. This involves constructing 218 * a physical i/o request vector based on the uio 219 * vector, performing the dma, and, finally, moving 220 * the data to it's final destination (because of CCI 221 * VERSAbus bogosities). 222 */ 223 ikrw(dev, uio, rw) 224 dev_t dev; 225 register struct uio *uio; 226 int rw; 227 { 228 int error, unit = IKUNIT(dev), s, wrcmd; 229 register struct buf *bp; 230 register struct iovec *iov; 231 register struct psalist *ap; 232 struct ik_softc *sc = &ik_softc[unit]; 233 234 if (unit >= NIK) 235 return (ENXIO); 236 bp = &rikbuf[unit]; 237 error = 0, iov = uio->uio_iov, wrcmd = PS_WRPHY; 238 for (; !error && uio->uio_iovcnt; iov++, uio->uio_iovcnt--) { 239 /* 240 * Hack way to set PS300 address w/o doing an lseek 241 * and specify write physical w/ refresh synchronization. 242 */ 243 if (iov->iov_len == 0) { 244 if ((int)iov->iov_base&PSIO_SYNC) 245 wrcmd = PS_WRPHY_SYNC; 246 uio->uio_offset = (int)iov->iov_base & ~PSIO_SYNC; 247 continue; 248 } 249 if (iov->iov_len > PS_MAXDMA) { 250 sc->is_error = PSERROR_INVALBC, error = EINVAL; 251 continue; 252 } 253 if ((int)uio->uio_offset&01) { 254 sc->is_error = PSERROR_BADADDR, error = EINVAL; 255 continue; 256 } 257 s = splbio(); 258 while (bp->b_flags&B_BUSY) { 259 bp->b_flags |= B_WANTED; 260 sleep((caddr_t)bp, PRIBIO+1); 261 } 262 splx(s); 263 bp->b_flags = B_BUSY | rw; 264 /* 265 * Construct address descriptor in buffer. 266 */ 267 ap = (struct psalist *)sc->is_buf; 268 ap->nblocks = 1; 269 /* work-around dr300 word swapping */ 270 ap->addr[0] = uio->uio_offset & 0xffff; 271 ap->addr[1] = uio->uio_offset >> 16; 272 ap->wc = (iov->iov_len + 1) >> 1; 273 if (rw == B_WRITE) { 274 error = copyin(iov->iov_base, (caddr_t)&ap[1], 275 (unsigned)iov->iov_len); 276 if (!error) 277 error = ikcommand(dev, wrcmd, 278 iov->iov_len + sizeof (*ap)); 279 } else { 280 caddr_t cp; 281 int len; 282 283 error = ikcommand(dev, PS_RDPHY, sizeof (*ap)); 284 cp = (caddr_t)&ap[1], len = iov->iov_len; 285 for (; len > 0; len -= NBPG, cp += NBPG) 286 mtpr(P1DC, cp); 287 if (!error) 288 error = copyout((caddr_t)&ap[1], iov->iov_base, 289 (unsigned)iov->iov_len); 290 } 291 (void) splbio(); 292 if (bp->b_flags&B_WANTED) 293 wakeup((caddr_t)bp); 294 splx(s); 295 uio->uio_resid -= iov->iov_len; 296 uio->uio_offset += iov->iov_len; 297 bp->b_flags &= ~(B_BUSY|B_WANTED); 298 } 299 return (error); 300 } 301 302 /* 303 * Perform a PS300 command. 304 */ 305 ikcommand(dev, com, count) 306 dev_t dev; 307 int com, count; 308 { 309 register struct buf *bp; 310 register int s; 311 312 bp = &cikbuf[IKUNIT(dev)]; 313 s = splik(); 314 while (bp->b_flags&B_BUSY) { 315 if (bp->b_flags&B_DONE) 316 break; 317 bp->b_flags |= B_WANTED; 318 sleep((caddr_t)bp, PRIBIO); 319 } 320 bp->b_flags = B_BUSY|B_READ; 321 splx(s); 322 bp->b_dev = dev; 323 bp->b_command = com; 324 bp->b_bcount = count; 325 ikstrategy(bp); 326 biowait(bp); 327 if (bp->b_flags&B_WANTED) 328 wakeup((caddr_t)bp); 329 bp->b_flags &= B_ERROR; 330 return (geterror(bp)); 331 } 332 333 /* 334 * Physio strategy routine 335 */ 336 ikstrategy(bp) 337 register struct buf *bp; 338 { 339 register struct buf *dp; 340 341 /* 342 * Put request at end of controller queue. 343 */ 344 dp = &iktab[IKUNIT(bp->b_dev)]; 345 bp->av_forw = NULL; 346 (void) splik(); 347 if (dp->b_actf != NULL) { 348 dp->b_actl->av_forw = bp; 349 dp->b_actl = bp; 350 } else 351 dp->b_actf = dp->b_actl = bp; 352 if (!dp->b_active) 353 ikstart(dp); 354 (void) spl0(); 355 } 356 357 /* 358 * Start the next command on the controller's queue. 359 */ 360 ikstart(dp) 361 register struct buf *dp; 362 { 363 register struct buf *bp; 364 register struct ikdevice *ik; 365 register struct ik_softc *sc; 366 u_short bc, csr; 367 u_int addr; 368 int unit; 369 370 loop: 371 /* 372 * Pull a request off the controller queue 373 */ 374 if ((bp = dp->b_actf) == NULL) { 375 dp->b_active = 0; 376 return; 377 } 378 /* 379 * Mark controller busy and process this request. 380 */ 381 dp->b_active = 1; 382 unit = IKUNIT(bp->b_dev); 383 sc = &ik_softc[unit]; 384 ik = (struct ikdevice *)ikinfo[unit]->ui_addr; 385 switch ((int)bp->b_command) { 386 387 case PS_ATTACH: /* logical unit attach */ 388 case PS_DETACH: /* logical unit detach */ 389 case PS_LOOKUP: /* name lookup */ 390 case PS_RDPHY: /* physical i/o read */ 391 case PS_WRPHY: /* physical i/o write */ 392 case PS_WRPHY_SYNC: /* physical i/o write w/ sync */ 393 /* 394 * Handshake command and, optionally, 395 * byte count and byte swap flag. 396 */ 397 if (sc->is_error = diowrite(ik, (u_short)bp->b_command)) 398 goto bad; 399 if (bp->b_command < PS_DETACH) { 400 if (sc->is_error = diowrite(ik, (u_short)bp->b_bcount)) 401 goto bad; 402 if (sc->is_error = diowrite(ik, (u_short)0 /* !swab */)) 403 goto bad; 404 } 405 /* 406 * Set timeout and wait for an attention interrupt. 407 */ 408 sc->is_timeout = iktimeout; 409 return; 410 411 case PS_DMAOUT: /* dma data host->PS300 */ 412 bc = bp->b_bcount; 413 csr = IKCSR_CYCLE; 414 break; 415 416 case PS_DMAIN: /* dma data PS300->host */ 417 bc = bp->b_bcount; 418 csr = IKCSR_CYCLE|IKCSR_FNC1; 419 break; 420 421 default: 422 log(LOG_ERR, "ik%d: bad cmd %x\n", unit, bp->b_command); 423 sc->is_error = PSERROR_BADCMD; 424 goto bad; 425 } 426 /* initiate dma transfer */ 427 addr = vtoph((struct proc *)0, (unsigned)sc->is_buf); 428 ik->ik_bahi = addr >> 17; 429 ik->ik_balo = (addr >> 1) & 0xffff; 430 ik->ik_wc = ((bc + 1) >> 1) - 1; /* round & convert */ 431 ik->ik_pulse = IKPULSE_RATTF|IKPULSE_RDMAF; 432 sc->is_timeout = iktimeout; 433 ik->ik_csr = IKCSR_IENA|IKCSR_GO|csr; 434 return; 435 bad: 436 bp->b_flags |= B_ERROR; 437 dp->b_actf = bp->av_forw; /* remove from queue */ 438 biodone(bp); 439 goto loop; 440 } 441 442 #define FETCHWORD(i) { \ 443 v = dioread(ik); \ 444 if (v == -1) { \ 445 sc->is_error = PSERROR_NAMETIMO; \ 446 goto bad; \ 447 } \ 448 sc->is_nameaddr.w[i] = v; \ 449 } 450 451 /* 452 * Process a device interrupt. 453 */ 454 ikintr(ikon) 455 int ikon; 456 { 457 register struct ikdevice *ik; 458 register struct buf *bp, *dp; 459 struct ik_softc *sc; 460 register u_short data; 461 int v; 462 463 /* should go by controller, but for now... */ 464 if (ikinfo[ikon] == 0) 465 return; 466 ik = (struct ikdevice *)ikinfo[ikon]->ui_addr; 467 /* 468 * Discard all non-attention interrupts. The 469 * interrupts we're throwing away should all be 470 * associated with DMA completion. 471 */ 472 data = ik->ik_data; 473 if ((ik->ik_csr&(IKCSR_ATTF|IKCSR_STATC)) != IKCSR_ATTF) { 474 ik->ik_pulse = IKPULSE_RATTF|IKPULSE_RDMAF|IKPULSE_SIENA; 475 return; 476 } 477 /* 478 * Fetch attention code immediately. 479 */ 480 ik->ik_csr = IKCSR_RATTF|IKCSR_RDMAF|IKCSR_FNC1; 481 ik->ik_pulse = IKPULSE_FNC2; 482 /* 483 * Get device and block structures, and a pointer 484 * to the vba_device for the device. We receive an 485 * unsolicited interrupt whenever the PS300 is power 486 * cycled (so ignore it in that case). 487 */ 488 dp = &iktab[ikon]; 489 if ((bp = dp->b_actf) == NULL) { 490 if (PS_CODE(data) != PS_RESET) /* power failure */ 491 log(LOG_WARNING, "ik%d: spurious interrupt, code %x\n", 492 ikon, data); 493 goto enable; 494 } 495 sc = &ik_softc[IKUNIT(bp->b_dev)]; 496 sc->is_timeout = 0; /* disable timer */ 497 switch (PS_CODE(data)) { 498 499 case PS_LOOKUP: /* name lookup */ 500 if (data == PS_LOOKUP) { /* dma name */ 501 bp->b_command = PS_DMAOUT; 502 goto opcont; 503 } 504 if (data == PS_DMAOK(PS_LOOKUP)) { 505 /* reenable interrupt and wait for address */ 506 sc->is_timeout = iktimeout; 507 goto enable; 508 } 509 /* 510 * Address should be present, extract it one 511 * word at a time from the PS300 (yech). 512 */ 513 if (data != PS_ADROK(PS_LOOKUP)) 514 goto bad; 515 FETCHWORD(0); 516 FETCHWORD(1); 517 goto opdone; 518 519 case PS_WRPHY_SYNC: /* physical i/o write w/ sync */ 520 if (data == PS_WRPHY_SYNC) { /* start dma transfer */ 521 bp->b_command = PS_DMAOUT; 522 goto opcont; 523 } 524 if (data != PS_DMAOK(PS_WRPHY_SYNC)) 525 goto bad; 526 goto opdone; 527 528 case PS_WRPHY: /* physical i/o write */ 529 if (data == PS_WRPHY) { /* start dma transfer */ 530 bp->b_command = PS_DMAOUT; 531 goto opcont; 532 } 533 if (data != PS_DMAOK(PS_WRPHY)) 534 goto bad; 535 goto opdone; 536 537 case PS_ATTACH: /* attach unit */ 538 case PS_DETACH: /* detach unit */ 539 case PS_ABORT: /* abort code from ps300 */ 540 if (data != bp->b_command) 541 goto bad; 542 goto opdone; 543 544 case PS_RDPHY: /* physical i/o read */ 545 if (data == PS_RDPHY) { /* dma address list */ 546 bp->b_command = PS_DMAOUT; 547 goto opcont; 548 } 549 if (data == PS_ADROK(PS_RDPHY)) { 550 /* collect read byte count and start dma */ 551 bp->b_bcount = dioread(ik); 552 if (bp->b_bcount == -1) 553 goto bad; 554 bp->b_command = PS_DMAIN; 555 goto opcont; 556 } 557 if (data == PS_DMAOK(PS_RDPHY)) 558 goto opdone; 559 goto bad; 560 } 561 bad: 562 sc->is_error = data; 563 bp->b_flags |= B_ERROR; 564 opdone: 565 dp->b_actf = bp->av_forw; /* remove from queue */ 566 biodone(bp); 567 opcont: 568 ikstart(dp); 569 enable: 570 ik->ik_pulse = IKPULSE_SIENA; /* explicitly reenable */ 571 } 572 573 /* 574 * Watchdog timer. 575 */ 576 iktimer(unit) 577 int unit; 578 { 579 register struct ik_softc *sc = &ik_softc[unit]; 580 581 if (sc->is_timeout && --sc->is_timeout == 0) { 582 register struct buf *dp, *bp; 583 int s; 584 585 log(LOG_ERR, "ik%d: timeout\n", unit); 586 s = splik(); 587 /* should abort current command */ 588 dp = &iktab[unit]; 589 if (bp = dp->b_actf) { 590 sc->is_error = PSERROR_CMDTIMO; 591 bp->b_flags |= B_ERROR; 592 dp->b_actf = bp->av_forw; /* remove from queue */ 593 biodone(bp); 594 ikstart(dp); 595 } 596 splx(s); 597 } 598 timeout(iktimer, (caddr_t)unit, hz); 599 } 600 601 /* 602 * Handshake read from DR300. 603 */ 604 dioread(ik) 605 register struct ikdevice *ik; 606 { 607 register int t; 608 u_short data; 609 610 for (t = ikdiotimo; t > 0; t--) 611 if ((ik->ik_csr&(IKCSR_ATTF|IKCSR_STATC)) == IKCSR_ATTF) { 612 data = ik->ik_data; 613 ik->ik_csr = IKCSR_RATTF|IKCSR_RDMAF|IKCSR_FNC1; 614 ik->ik_pulse = IKPULSE_FNC2; 615 return (data); 616 } 617 return (-1); 618 } 619 620 /* 621 * Handshake write to DR300. 622 * 623 * Interrupts are enabled before completing the work 624 * so the caller should either be at splik or be 625 * prepared to take the interrupt immediately. 626 */ 627 diowrite(ik, v) 628 register struct ikdevice *ik; 629 u_short v; 630 { 631 register int t; 632 register u_short csr; 633 634 top: 635 /* 636 * Deposit data and generate dr300 attention 637 */ 638 ik->ik_data = v; 639 ik->ik_csr = IKCSR_RDMAF|IKCSR_RATTF; 640 ik->ik_pulse = IKPULSE_FNC2; 641 for (t = ikdiotimo; t > 0; t--) { 642 csr = ik->ik_csr; 643 #define IKCSR_DONE (IKCSR_STATA|IKCSR_STATC) 644 if ((csr&IKCSR_DONE) == IKCSR_DONE) { 645 /* 646 * Done, complete handshake by notifying dr300. 647 */ 648 ik->ik_csr = IKCSR_IENA; /* ~IKCSR_FNC1 */ 649 ik->ik_pulse = IKPULSE_FNC2; 650 return (0); 651 } 652 /* beware of potential deadlock with dioread */ 653 if ((csr&(IKCSR_ATTF|IKCSR_STATC)) == IKCSR_ATTF) 654 goto top; 655 } 656 ik->ik_csr = IKCSR_IENA; 657 return (PSERROR_DIOTIMO); 658 } 659 660 /*ARGSUSED*/ 661 ikioctl(dev, cmd, data, flag) 662 dev_t dev; 663 int cmd; 664 caddr_t data; 665 int flag; 666 { 667 int error = 0, unit = IKUNIT(dev), s; 668 register struct ik_softc *sc = &ik_softc[unit]; 669 670 switch (cmd) { 671 672 case PSIOGETERROR: /* get error code for last operation */ 673 *(int *)data = sc->is_error; 674 break; 675 676 case PSIOLOOKUP: { /* PS300 name lookup */ 677 register struct pslookup *lp = (struct pslookup *)data; 678 register struct buf *bp; 679 680 if (lp->pl_len > PS_MAXNAMELEN) 681 return (EINVAL); 682 bp = &rikbuf[unit]; 683 s = splbio(); 684 while (bp->b_flags&B_BUSY) { 685 bp->b_flags |= B_WANTED; 686 sleep((caddr_t)bp, PRIBIO+1); 687 } 688 splx(s); 689 bp->b_flags = B_BUSY | B_WRITE; 690 error = copyin(lp->pl_name, (caddr_t)sc->is_buf, 691 (unsigned)lp->pl_len); 692 if (error == 0) { 693 if (lp->pl_len&1) 694 sc->is_buf[lp->pl_len] = '\0'; 695 error = ikcommand(dev, PS_LOOKUP, lp->pl_len); 696 } 697 s = splbio(); 698 if (bp->b_flags&B_WANTED) 699 wakeup((caddr_t)bp); 700 splx(s); 701 bp->b_flags &= ~(B_BUSY|B_WANTED); 702 lp->pl_addr = sc->is_nameaddr.l; 703 break; 704 } 705 default: 706 return (ENOTTY); 707 } 708 return (error); 709 } 710 #endif 711