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