1 /* $NetBSD: uha.c,v 1.26 2001/04/25 17:53:34 bouyer Exp $ */ 2 3 #undef UHADEBUG 4 #ifdef DDB 5 #define integrate 6 #else 7 #define integrate static inline 8 #endif 9 10 /*- 11 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 12 * All rights reserved. 13 * 14 * This code is derived from software contributed to The NetBSD Foundation 15 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 16 * Simulation Facility, NASA Ames Research Center. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 3. All advertising materials mentioning features or use of this software 27 * must display the following acknowledgement: 28 * This product includes software developed by the NetBSD 29 * Foundation, Inc. and its contributors. 30 * 4. Neither the name of The NetBSD Foundation nor the names of its 31 * contributors may be used to endorse or promote products derived 32 * from this software without specific prior written permission. 33 * 34 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 35 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 36 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 38 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 39 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 40 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 44 * POSSIBILITY OF SUCH DAMAGE. 45 */ 46 47 /* 48 * Ported for use with the UltraStor 14f by Gary Close (gclose@wvnvms.wvnet.edu) 49 * Slight fixes to timeouts to run with the 34F 50 * Thanks to Julian Elischer for advice and help with this port. 51 * 52 * Originally written by Julian Elischer (julian@tfs.com) 53 * for TRW Financial Systems for use under the MACH(2.5) operating system. 54 * 55 * TRW Financial Systems, in accordance with their agreement with Carnegie 56 * Mellon University, makes this software available to CMU to distribute 57 * or use in any manner that they see fit as long as this message is kept with 58 * the software. For this reason TFS also grants any other persons or 59 * organisations permission to use or modify this software. 60 * 61 * TFS supplies this software to be publicly redistributed 62 * on the understanding that TFS is not responsible for the correct 63 * functioning of this software in any circumstances. 64 * 65 * commenced: Sun Sep 27 18:14:01 PDT 1992 66 * slight mod to make work with 34F as well: Wed Jun 2 18:05:48 WST 1993 67 */ 68 69 #include <sys/types.h> 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/kernel.h> 73 #include <sys/errno.h> 74 #include <sys/ioctl.h> 75 #include <sys/device.h> 76 #include <sys/malloc.h> 77 #include <sys/buf.h> 78 #include <sys/proc.h> 79 #include <sys/user.h> 80 81 #include <uvm/uvm_extern.h> 82 83 #include <machine/bus.h> 84 #include <machine/intr.h> 85 86 #include <dev/scsipi/scsi_all.h> 87 #include <dev/scsipi/scsipi_all.h> 88 #include <dev/scsipi/scsiconf.h> 89 90 #include <dev/ic/uhareg.h> 91 #include <dev/ic/uhavar.h> 92 93 #ifndef DDB 94 #define Debugger() panic("should call debugger here (uha.c)") 95 #endif /* ! DDB */ 96 97 #define UHA_MAXXFER ((UHA_NSEG - 1) << PGSHIFT) 98 99 integrate void uha_reset_mscp __P((struct uha_softc *, struct uha_mscp *)); 100 void uha_free_mscp __P((struct uha_softc *, struct uha_mscp *)); 101 integrate int uha_init_mscp __P((struct uha_softc *, struct uha_mscp *)); 102 struct uha_mscp *uha_get_mscp __P((struct uha_softc *)); 103 void uhaminphys __P((struct buf *)); 104 void uha_scsipi_request __P((struct scsipi_channel *, 105 scsipi_adapter_req_t, void *)); 106 int uha_create_mscps __P((struct uha_softc *, struct uha_mscp *, int)); 107 108 #define UHA_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */ 109 110 /* 111 * Attach all the sub-devices we can find 112 */ 113 void 114 uha_attach(sc, upd) 115 struct uha_softc *sc; 116 struct uha_probe_data *upd; 117 { 118 struct scsipi_adapter *adapt = &sc->sc_adapter; 119 struct scsipi_channel *chan = &sc->sc_channel; 120 bus_dma_segment_t seg; 121 int i, error, rseg; 122 123 TAILQ_INIT(&sc->sc_free_mscp); 124 125 (sc->init)(sc); 126 127 /* 128 * Fill in the scsipi_adapter. 129 */ 130 memset(adapt, 0, sizeof(*adapt)); 131 adapt->adapt_dev = &sc->sc_dev; 132 adapt->adapt_nchannels = 1; 133 /* adapt_openings initialized below */ 134 /* adapt_max_periph initialized below */ 135 adapt->adapt_request = uha_scsipi_request; 136 adapt->adapt_minphys = uhaminphys; 137 138 /* 139 * Fill in the scsipi_channel. 140 */ 141 memset(chan, 0, sizeof(*chan)); 142 chan->chan_adapter = adapt; 143 chan->chan_bustype = &scsi_bustype; 144 chan->chan_channel = 0; 145 chan->chan_ntargets = 8; 146 chan->chan_nluns = 8; 147 chan->chan_id = upd->sc_scsi_dev; 148 149 #define MSCPSIZE (UHA_MSCP_MAX * sizeof(struct uha_mscp)) 150 151 /* 152 * Allocate the MSCPs. 153 */ 154 if ((error = bus_dmamem_alloc(sc->sc_dmat, MSCPSIZE, 155 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) { 156 printf("%s: unable to allocate mscps, error = %d\n", 157 sc->sc_dev.dv_xname, error); 158 return; 159 } 160 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, 161 MSCPSIZE, (caddr_t *)&sc->sc_mscps, 162 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) { 163 printf("%s: unable to map mscps, error = %d\n", 164 sc->sc_dev.dv_xname, error); 165 return; 166 } 167 168 /* 169 * Create and load the DMA map used for the mscps. 170 */ 171 if ((error = bus_dmamap_create(sc->sc_dmat, MSCPSIZE, 172 1, MSCPSIZE, 0, BUS_DMA_NOWAIT | sc->sc_dmaflags, 173 &sc->sc_dmamap_mscp)) != 0) { 174 printf("%s: unable to create mscp DMA map, error = %d\n", 175 sc->sc_dev.dv_xname, error); 176 return; 177 } 178 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mscp, 179 sc->sc_mscps, MSCPSIZE, NULL, BUS_DMA_NOWAIT)) != 0) { 180 printf("%s: unable to load mscp DMA map, error = %d\n", 181 sc->sc_dev.dv_xname, error); 182 return; 183 } 184 185 #undef MSCPSIZE 186 187 /* 188 * Initialize the mscps. 189 */ 190 i = uha_create_mscps(sc, sc->sc_mscps, UHA_MSCP_MAX); 191 if (i == 0) { 192 printf("%s: unable to create mscps\n", 193 sc->sc_dev.dv_xname); 194 return; 195 } else if (i != UHA_MSCP_MAX) { 196 printf("%s: WARNING: only %d of %d mscps created\n", 197 sc->sc_dev.dv_xname, i, UHA_MSCP_MAX); 198 } 199 200 adapt->adapt_openings = i; 201 adapt->adapt_max_periph = adapt->adapt_openings; 202 203 /* 204 * ask the adapter what subunits are present 205 */ 206 config_found(&sc->sc_dev, &sc->sc_channel, scsiprint); 207 } 208 209 integrate void 210 uha_reset_mscp(sc, mscp) 211 struct uha_softc *sc; 212 struct uha_mscp *mscp; 213 { 214 215 mscp->flags = 0; 216 } 217 218 /* 219 * A mscp (and hence a mbx-out) is put onto the free list. 220 */ 221 void 222 uha_free_mscp(sc, mscp) 223 struct uha_softc *sc; 224 struct uha_mscp *mscp; 225 { 226 int s; 227 228 s = splbio(); 229 uha_reset_mscp(sc, mscp); 230 TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain); 231 splx(s); 232 } 233 234 integrate int 235 uha_init_mscp(sc, mscp) 236 struct uha_softc *sc; 237 struct uha_mscp *mscp; 238 { 239 bus_dma_tag_t dmat = sc->sc_dmat; 240 int hashnum, error; 241 242 /* 243 * Create the DMA map for this MSCP. 244 */ 245 error = bus_dmamap_create(dmat, UHA_MAXXFER, UHA_NSEG, UHA_MAXXFER, 246 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags, 247 &mscp->dmamap_xfer); 248 if (error) { 249 printf("%s: can't create mscp DMA map, error = %d\n", 250 sc->sc_dev.dv_xname, error); 251 return (error); 252 } 253 254 /* 255 * put in the phystokv hash table 256 * Never gets taken out. 257 */ 258 mscp->hashkey = sc->sc_dmamap_mscp->dm_segs[0].ds_addr + 259 UHA_MSCP_OFF(mscp); 260 hashnum = MSCP_HASH(mscp->hashkey); 261 mscp->nexthash = sc->sc_mscphash[hashnum]; 262 sc->sc_mscphash[hashnum] = mscp; 263 uha_reset_mscp(sc, mscp); 264 return (0); 265 } 266 267 /* 268 * Create a set of MSCPs and add them to the free list. 269 */ 270 int 271 uha_create_mscps(sc, mscpstore, count) 272 struct uha_softc *sc; 273 struct uha_mscp *mscpstore; 274 int count; 275 { 276 struct uha_mscp *mscp; 277 int i, error; 278 279 bzero(mscpstore, sizeof(struct uha_mscp) * count); 280 for (i = 0; i < count; i++) { 281 mscp = &mscpstore[i]; 282 if ((error = uha_init_mscp(sc, mscp)) != 0) { 283 printf("%s: unable to initialize mscp, error = %d\n", 284 sc->sc_dev.dv_xname, error); 285 goto out; 286 } 287 TAILQ_INSERT_TAIL(&sc->sc_free_mscp, mscp, chain); 288 } 289 out: 290 return (i); 291 } 292 293 /* 294 * Get a free mscp 295 * 296 * If there are none, see if we can allocate a new one. If so, put it in the 297 * hash table too otherwise either return an error or sleep. 298 */ 299 struct uha_mscp * 300 uha_get_mscp(sc) 301 struct uha_softc *sc; 302 { 303 struct uha_mscp *mscp; 304 int s; 305 306 s = splbio(); 307 mscp = TAILQ_FIRST(&sc->sc_free_mscp); 308 if (mscp != NULL) { 309 TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain); 310 mscp->flags |= MSCP_ALLOC; 311 } 312 splx(s); 313 return (mscp); 314 } 315 316 /* 317 * given a physical address, find the mscp that it corresponds to. 318 */ 319 struct uha_mscp * 320 uha_mscp_phys_kv(sc, mscp_phys) 321 struct uha_softc *sc; 322 u_long mscp_phys; 323 { 324 int hashnum = MSCP_HASH(mscp_phys); 325 struct uha_mscp *mscp = sc->sc_mscphash[hashnum]; 326 327 while (mscp) { 328 if (mscp->hashkey == mscp_phys) 329 break; 330 mscp = mscp->nexthash; 331 } 332 return (mscp); 333 } 334 335 /* 336 * We have a mscp which has been processed by the adaptor, now we look to see 337 * how the operation went. 338 */ 339 void 340 uha_done(sc, mscp) 341 struct uha_softc *sc; 342 struct uha_mscp *mscp; 343 { 344 bus_dma_tag_t dmat = sc->sc_dmat; 345 struct scsipi_sense_data *s1, *s2; 346 struct scsipi_xfer *xs = mscp->xs; 347 348 SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("uha_done\n")); 349 350 bus_dmamap_sync(dmat, sc->sc_dmamap_mscp, 351 UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp), 352 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 353 354 /* 355 * If we were a data transfer, unload the map that described 356 * the data buffer. 357 */ 358 if (xs->datalen) { 359 bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0, 360 mscp->dmamap_xfer->dm_mapsize, 361 (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD : 362 BUS_DMASYNC_POSTWRITE); 363 bus_dmamap_unload(dmat, mscp->dmamap_xfer); 364 } 365 366 /* 367 * Otherwise, put the results of the operation 368 * into the xfer and call whoever started it 369 */ 370 if ((mscp->flags & MSCP_ALLOC) == 0) { 371 printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname); 372 Debugger(); 373 return; 374 } 375 if (xs->error == XS_NOERROR) { 376 if (mscp->host_stat != UHA_NO_ERR) { 377 switch (mscp->host_stat) { 378 case UHA_SBUS_TIMEOUT: /* No response */ 379 xs->error = XS_SELTIMEOUT; 380 break; 381 default: /* Other scsi protocol messes */ 382 printf("%s: host_stat %x\n", 383 sc->sc_dev.dv_xname, mscp->host_stat); 384 xs->error = XS_DRIVER_STUFFUP; 385 } 386 } else if (mscp->target_stat != SCSI_OK) { 387 switch (mscp->target_stat) { 388 case SCSI_CHECK: 389 s1 = &mscp->mscp_sense; 390 s2 = &xs->sense.scsi_sense; 391 *s2 = *s1; 392 xs->error = XS_SENSE; 393 break; 394 case SCSI_BUSY: 395 xs->error = XS_BUSY; 396 break; 397 default: 398 printf("%s: target_stat %x\n", 399 sc->sc_dev.dv_xname, mscp->target_stat); 400 xs->error = XS_DRIVER_STUFFUP; 401 } 402 } else 403 xs->resid = 0; 404 } 405 uha_free_mscp(sc, mscp); 406 scsipi_done(xs); 407 } 408 409 void 410 uhaminphys(bp) 411 struct buf *bp; 412 { 413 414 if (bp->b_bcount > UHA_MAXXFER) 415 bp->b_bcount = UHA_MAXXFER; 416 minphys(bp); 417 } 418 419 /* 420 * start a scsi operation given the command and the data address. Also 421 * needs the unit, target and lu. 422 */ 423 424 void 425 uha_scsipi_request(chan, req, arg) 426 struct scsipi_channel *chan; 427 scsipi_adapter_req_t req; 428 void *arg; 429 { 430 struct scsipi_xfer *xs; 431 struct scsipi_periph *periph; 432 struct uha_softc *sc = (void *)chan->chan_adapter->adapt_dev; 433 bus_dma_tag_t dmat = sc->sc_dmat; 434 struct uha_mscp *mscp; 435 struct uha_dma_seg *sg; 436 int error, seg, flags, s; 437 438 439 switch (req) { 440 case ADAPTER_REQ_RUN_XFER: 441 xs = arg; 442 periph = xs->xs_periph; 443 flags = xs->xs_control; 444 445 SC_DEBUG(periph, SCSIPI_DB2, ("uha_scsipi_request\n")); 446 447 /* Get an MSCP to use. */ 448 mscp = uha_get_mscp(sc); 449 #ifdef DIAGNOSTIC 450 /* 451 * This should never happen as we track the resources 452 * in the mid-layer. 453 */ 454 if (mscp == NULL) { 455 scsipi_printaddr(periph); 456 printf("unable to allocate mscp\n"); 457 panic("uha_scsipi_request"); 458 } 459 #endif 460 461 mscp->xs = xs; 462 mscp->timeout = xs->timeout; 463 464 /* 465 * Put all the arguments for the xfer in the mscp 466 */ 467 if (flags & XS_CTL_RESET) { 468 mscp->opcode = UHA_SDR; 469 mscp->ca = 0x01; 470 } else { 471 mscp->opcode = UHA_TSP; 472 /* XXX Not for tapes. */ 473 mscp->ca = 0x01; 474 bcopy(xs->cmd, &mscp->scsi_cmd, mscp->scsi_cmd_length); 475 } 476 mscp->xdir = UHA_SDET; 477 mscp->dcn = 0x00; 478 mscp->chan = 0x00; 479 mscp->target = periph->periph_target; 480 mscp->lun = periph->periph_lun; 481 mscp->scsi_cmd_length = xs->cmdlen; 482 mscp->sense_ptr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr + 483 UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, mscp_sense); 484 mscp->req_sense_length = sizeof(mscp->mscp_sense); 485 mscp->host_stat = 0x00; 486 mscp->target_stat = 0x00; 487 488 if (xs->datalen) { 489 sg = mscp->uha_dma; 490 seg = 0; 491 #ifdef TFS 492 if (flags & SCSI_DATA_UIO) { 493 error = bus_dmamap_load_uio(dmat, 494 mscp->dmamap_xfer, (struct uio *)xs->data, 495 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : 496 BUS_DMA_WAITOK) | BUS_DMA_STREAMING); 497 } else 498 #endif /*TFS */ 499 { 500 error = bus_dmamap_load(dmat, 501 mscp->dmamap_xfer, xs->data, xs->datalen, 502 NULL, 503 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : 504 BUS_DMA_WAITOK) | BUS_DMA_STREAMING); 505 } 506 507 switch (error) { 508 case 0: 509 break; 510 511 case ENOMEM: 512 case EAGAIN: 513 xs->error = XS_RESOURCE_SHORTAGE; 514 goto out_bad; 515 516 default: 517 xs->error = XS_DRIVER_STUFFUP; 518 printf("%s: error %d loading DMA map\n", 519 sc->sc_dev.dv_xname, error); 520 out_bad: 521 uha_free_mscp(sc, mscp); 522 scsipi_done(xs); 523 return; 524 } 525 526 bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0, 527 mscp->dmamap_xfer->dm_mapsize, 528 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD : 529 BUS_DMASYNC_PREWRITE); 530 531 /* 532 * Load the hardware scatter/gather map with the 533 * contents of the DMA map. 534 */ 535 for (seg = 0; 536 seg < mscp->dmamap_xfer->dm_nsegs; seg++) { 537 mscp->uha_dma[seg].seg_addr = 538 mscp->dmamap_xfer->dm_segs[seg].ds_addr; 539 mscp->uha_dma[seg].seg_len = 540 mscp->dmamap_xfer->dm_segs[seg].ds_len; 541 } 542 543 mscp->data_addr = 544 sc->sc_dmamap_mscp->dm_segs[0].ds_addr + 545 UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, 546 uha_dma); 547 mscp->data_length = xs->datalen; 548 mscp->sgth = 0x01; 549 mscp->sg_num = seg; 550 } else { /* No data xfer, use non S/G values */ 551 mscp->data_addr = (physaddr)0; 552 mscp->data_length = 0; 553 mscp->sgth = 0x00; 554 mscp->sg_num = 0; 555 } 556 mscp->link_id = 0; 557 mscp->link_addr = (physaddr)0; 558 559 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_mscp, 560 UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp), 561 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 562 563 s = splbio(); 564 (sc->start_mbox)(sc, mscp); 565 splx(s); 566 567 if ((flags & XS_CTL_POLL) == 0) 568 return; 569 570 /* 571 * If we can't use interrupts, poll on completion 572 */ 573 if ((sc->poll)(sc, xs, mscp->timeout)) { 574 uha_timeout(mscp); 575 if ((sc->poll)(sc, xs, mscp->timeout)) 576 uha_timeout(mscp); 577 } 578 return; 579 580 case ADAPTER_REQ_GROW_RESOURCES: 581 /* XXX Not supported. */ 582 return; 583 584 case ADAPTER_REQ_SET_XFER_MODE: 585 /* 586 * We can't really do this (the UltraStor controllers 587 * have their own config). 588 * 589 * XXX How do we query the config? 590 */ 591 return; 592 } 593 } 594 void 595 uha_timeout(arg) 596 void *arg; 597 { 598 struct uha_mscp *mscp = arg; 599 struct scsipi_xfer *xs = mscp->xs; 600 struct scsipi_periph *periph = xs->xs_periph; 601 struct uha_softc *sc = 602 (void *)periph->periph_channel->chan_adapter->adapt_dev; 603 int s; 604 605 scsipi_printaddr(periph); 606 printf("timed out"); 607 608 s = splbio(); 609 610 if (mscp->flags & MSCP_ABORT) { 611 /* abort timed out */ 612 printf(" AGAIN\n"); 613 /* XXX Must reset! */ 614 } else { 615 /* abort the operation that has timed out */ 616 printf("\n"); 617 mscp->xs->error = XS_TIMEOUT; 618 mscp->timeout = UHA_ABORT_TIMEOUT; 619 mscp->flags |= MSCP_ABORT; 620 (sc->start_mbox)(sc, mscp); 621 } 622 623 splx(s); 624 } 625