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