1 /* $NetBSD: uha.c,v 1.48 2021/04/24 23:36:55 thorpej 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.48 2021/04/24 23:36:55 thorpej 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, 146 "unable to allocate mscps, error = %d\n", 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, 153 "unable to map mscps, error = %d\n", 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, 164 "unable to create mscp DMA map, error = %d\n", 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, 170 "unable to load mscp DMA map, error = %d\n", 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, 185 "WARNING: only %d of %d mscps created\n", 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, CFARG_EOL); 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, 232 "can't create mscp DMA map, error = %d\n", 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, 263 "unable to initialize mscp, error = %d\n", 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, 373 "target_stat %x\n", 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, 399 void *arg) 400 { 401 struct scsipi_xfer *xs; 402 struct scsipi_periph *periph; 403 struct uha_softc *sc = device_private(chan->chan_adapter->adapt_dev); 404 bus_dma_tag_t dmat = sc->sc_dmat; 405 struct uha_mscp *mscp; 406 int error, seg, flags, s; 407 408 409 switch (req) { 410 case ADAPTER_REQ_RUN_XFER: 411 xs = arg; 412 periph = xs->xs_periph; 413 flags = xs->xs_control; 414 415 SC_DEBUG(periph, SCSIPI_DB2, ("uha_scsipi_request\n")); 416 417 /* Get an MSCP to use. */ 418 mscp = uha_get_mscp(sc); 419 #ifdef DIAGNOSTIC 420 /* 421 * This should never happen as we track the resources 422 * in the mid-layer. 423 */ 424 if (mscp == NULL) { 425 scsipi_printaddr(periph); 426 printf("unable to allocate mscp\n"); 427 panic("uha_scsipi_request"); 428 } 429 #endif 430 431 mscp->xs = xs; 432 mscp->timeout = xs->timeout; 433 434 /* 435 * Put all the arguments for the xfer in the mscp 436 */ 437 if (flags & XS_CTL_RESET) { 438 mscp->opcode = UHA_SDR; 439 mscp->ca = 0x01; 440 } else { 441 if (xs->cmdlen > sizeof(mscp->scsi_cmd)) { 442 aprint_error_dev(sc->sc_dev, 443 "cmdlen %d too large for MSCP\n", 444 xs->cmdlen); 445 xs->error = XS_DRIVER_STUFFUP; 446 goto out_bad; 447 } 448 mscp->opcode = UHA_TSP; 449 /* XXX Not for tapes. */ 450 mscp->ca = 0x01; 451 memcpy(&mscp->scsi_cmd, xs->cmd, mscp->scsi_cmd_length); 452 } 453 mscp->xdir = UHA_SDET; 454 mscp->dcn = 0x00; 455 mscp->chan = 0x00; 456 mscp->target = periph->periph_target; 457 mscp->lun = periph->periph_lun; 458 mscp->scsi_cmd_length = xs->cmdlen; 459 mscp->sense_ptr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr + 460 UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, mscp_sense); 461 mscp->req_sense_length = sizeof(mscp->mscp_sense); 462 mscp->host_stat = 0x00; 463 mscp->target_stat = 0x00; 464 465 if (xs->datalen) { 466 seg = 0; 467 #ifdef TFS 468 if (flags & SCSI_DATA_UIO) { 469 error = bus_dmamap_load_uio(dmat, 470 mscp->dmamap_xfer, (struct uio *)xs->data, 471 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : 472 BUS_DMA_WAITOK) | BUS_DMA_STREAMING | 473 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ : 474 BUS_DMA_WRITE)); 475 } else 476 #endif /*TFS */ 477 { 478 error = bus_dmamap_load(dmat, 479 mscp->dmamap_xfer, xs->data, xs->datalen, 480 NULL, 481 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : 482 BUS_DMA_WAITOK) | BUS_DMA_STREAMING | 483 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ : 484 BUS_DMA_WRITE)); 485 } 486 487 switch (error) { 488 case 0: 489 break; 490 491 case ENOMEM: 492 case EAGAIN: 493 xs->error = XS_RESOURCE_SHORTAGE; 494 goto out_bad; 495 496 default: 497 xs->error = XS_DRIVER_STUFFUP; 498 aprint_error_dev(sc->sc_dev, 499 "error %d loading DMA map\n", error); 500 out_bad: 501 uha_free_mscp(sc, mscp); 502 scsipi_done(xs); 503 return; 504 } 505 506 bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0, 507 mscp->dmamap_xfer->dm_mapsize, 508 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD : 509 BUS_DMASYNC_PREWRITE); 510 511 /* 512 * Load the hardware scatter/gather map with the 513 * contents of the DMA map. 514 */ 515 for (seg = 0; 516 seg < mscp->dmamap_xfer->dm_nsegs; seg++) { 517 mscp->uha_dma[seg].seg_addr = 518 mscp->dmamap_xfer->dm_segs[seg].ds_addr; 519 mscp->uha_dma[seg].seg_len = 520 mscp->dmamap_xfer->dm_segs[seg].ds_len; 521 } 522 523 mscp->data_addr = 524 sc->sc_dmamap_mscp->dm_segs[0].ds_addr + 525 UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, 526 uha_dma); 527 mscp->data_length = xs->datalen; 528 mscp->sgth = 0x01; 529 mscp->sg_num = seg; 530 } else { /* No data xfer, use non S/G values */ 531 mscp->data_addr = (physaddr)0; 532 mscp->data_length = 0; 533 mscp->sgth = 0x00; 534 mscp->sg_num = 0; 535 } 536 mscp->link_id = 0; 537 mscp->link_addr = (physaddr)0; 538 539 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_mscp, 540 UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp), 541 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 542 543 s = splbio(); 544 (sc->start_mbox)(sc, mscp); 545 splx(s); 546 547 if ((flags & XS_CTL_POLL) == 0) 548 return; 549 550 /* 551 * If we can't use interrupts, poll on completion 552 */ 553 if ((sc->poll)(sc, xs, mscp->timeout)) { 554 uha_timeout(mscp); 555 if ((sc->poll)(sc, xs, mscp->timeout)) 556 uha_timeout(mscp); 557 } 558 return; 559 560 case ADAPTER_REQ_GROW_RESOURCES: 561 /* XXX Not supported. */ 562 return; 563 564 case ADAPTER_REQ_SET_XFER_MODE: 565 /* 566 * We can't really do this (the UltraStor controllers 567 * have their own config). 568 * 569 * XXX How do we query the config? 570 */ 571 return; 572 } 573 } 574 void 575 uha_timeout(void *arg) 576 { 577 struct uha_mscp *mscp = arg; 578 struct scsipi_xfer *xs = mscp->xs; 579 struct scsipi_periph *periph = xs->xs_periph; 580 struct uha_softc *sc = 581 device_private(periph->periph_channel->chan_adapter->adapt_dev); 582 int s; 583 584 scsipi_printaddr(periph); 585 printf("timed out"); 586 587 s = splbio(); 588 589 if (mscp->flags & MSCP_ABORT) { 590 /* abort timed out */ 591 printf(" AGAIN\n"); 592 /* XXX Must reset! */ 593 } else { 594 /* abort the operation that has timed out */ 595 printf("\n"); 596 mscp->xs->error = XS_TIMEOUT; 597 mscp->timeout = UHA_ABORT_TIMEOUT; 598 mscp->flags |= MSCP_ABORT; 599 (sc->start_mbox)(sc, mscp); 600 } 601 602 splx(s); 603 } 604