1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2000 Matthew Jacob 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification, immediately at the beginning of the file. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 34 #include <sys/conf.h> 35 #include <sys/errno.h> 36 #include <sys/fcntl.h> 37 #include <sys/kernel.h> 38 #include <sys/kthread.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/queue.h> 44 #include <sys/sbuf.h> 45 #include <sys/sx.h> 46 #include <sys/sysent.h> 47 #include <sys/systm.h> 48 #include <sys/sysctl.h> 49 #include <sys/types.h> 50 51 #include <machine/stdarg.h> 52 53 #include <cam/cam.h> 54 #include <cam/cam_ccb.h> 55 #include <cam/cam_debug.h> 56 #include <cam/cam_periph.h> 57 #include <cam/cam_xpt_periph.h> 58 59 #include <cam/scsi/scsi_all.h> 60 #include <cam/scsi/scsi_message.h> 61 #include <cam/scsi/scsi_enc.h> 62 #include <cam/scsi/scsi_enc_internal.h> 63 64 #include "opt_ses.h" 65 66 MALLOC_DEFINE(M_SCSIENC, "SCSI ENC", "SCSI ENC buffers"); 67 68 /* Enclosure type independent driver */ 69 70 static d_open_t enc_open; 71 static d_close_t enc_close; 72 static d_ioctl_t enc_ioctl; 73 static periph_init_t enc_init; 74 static periph_ctor_t enc_ctor; 75 static periph_oninv_t enc_oninvalidate; 76 static periph_dtor_t enc_dtor; 77 78 static void enc_async(void *, uint32_t, struct cam_path *, void *); 79 static enctyp enc_type(struct ccb_getdev *); 80 81 SYSCTL_NODE(_kern_cam, OID_AUTO, enc, CTLFLAG_RD, 0, 82 "CAM Enclosure Services driver"); 83 84 static struct periph_driver encdriver = { 85 enc_init, "ses", 86 TAILQ_HEAD_INITIALIZER(encdriver.units), /* generation */ 0 87 }; 88 89 PERIPHDRIVER_DECLARE(enc, encdriver); 90 91 static struct cdevsw enc_cdevsw = { 92 .d_version = D_VERSION, 93 .d_open = enc_open, 94 .d_close = enc_close, 95 .d_ioctl = enc_ioctl, 96 .d_name = "ses", 97 .d_flags = D_TRACKCLOSE, 98 }; 99 100 static void 101 enc_init(void) 102 { 103 cam_status status; 104 105 /* 106 * Install a global async callback. This callback will 107 * receive async callbacks like "new device found". 108 */ 109 status = xpt_register_async(AC_FOUND_DEVICE, enc_async, NULL, NULL); 110 111 if (status != CAM_REQ_CMP) { 112 printf("enc: Failed to attach master async callback " 113 "due to status 0x%x!\n", status); 114 } 115 } 116 117 static void 118 enc_devgonecb(void *arg) 119 { 120 struct cam_periph *periph; 121 struct enc_softc *enc; 122 struct mtx *mtx; 123 int i; 124 125 periph = (struct cam_periph *)arg; 126 mtx = cam_periph_mtx(periph); 127 mtx_lock(mtx); 128 enc = (struct enc_softc *)periph->softc; 129 130 /* 131 * When we get this callback, we will get no more close calls from 132 * devfs. So if we have any dangling opens, we need to release the 133 * reference held for that particular context. 134 */ 135 for (i = 0; i < enc->open_count; i++) 136 cam_periph_release_locked(periph); 137 138 enc->open_count = 0; 139 140 /* 141 * Release the reference held for the device node, it is gone now. 142 */ 143 cam_periph_release_locked(periph); 144 145 /* 146 * We reference the lock directly here, instead of using 147 * cam_periph_unlock(). The reason is that the final call to 148 * cam_periph_release_locked() above could result in the periph 149 * getting freed. If that is the case, dereferencing the periph 150 * with a cam_periph_unlock() call would cause a page fault. 151 */ 152 mtx_unlock(mtx); 153 } 154 155 static void 156 enc_oninvalidate(struct cam_periph *periph) 157 { 158 struct enc_softc *enc; 159 160 enc = periph->softc; 161 162 enc->enc_flags |= ENC_FLAG_INVALID; 163 164 /* If the sub-driver has an invalidate routine, call it */ 165 if (enc->enc_vec.softc_invalidate != NULL) 166 enc->enc_vec.softc_invalidate(enc); 167 168 /* 169 * Unregister any async callbacks. 170 */ 171 xpt_register_async(0, enc_async, periph, periph->path); 172 173 /* 174 * Shutdown our daemon. 175 */ 176 enc->enc_flags |= ENC_FLAG_SHUTDOWN; 177 if (enc->enc_daemon != NULL) { 178 /* Signal the ses daemon to terminate. */ 179 wakeup(enc->enc_daemon); 180 } 181 callout_drain(&enc->status_updater); 182 183 destroy_dev_sched_cb(enc->enc_dev, enc_devgonecb, periph); 184 } 185 186 static void 187 enc_dtor(struct cam_periph *periph) 188 { 189 struct enc_softc *enc; 190 191 enc = periph->softc; 192 193 /* If the sub-driver has a cleanup routine, call it */ 194 if (enc->enc_vec.softc_cleanup != NULL) 195 enc->enc_vec.softc_cleanup(enc); 196 197 if (enc->enc_boot_hold_ch.ich_func != NULL) { 198 config_intrhook_disestablish(&enc->enc_boot_hold_ch); 199 enc->enc_boot_hold_ch.ich_func = NULL; 200 } 201 202 ENC_FREE(enc); 203 } 204 205 static void 206 enc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg) 207 { 208 struct cam_periph *periph; 209 210 periph = (struct cam_periph *)callback_arg; 211 212 switch(code) { 213 case AC_FOUND_DEVICE: 214 { 215 struct ccb_getdev *cgd; 216 cam_status status; 217 path_id_t path_id; 218 219 cgd = (struct ccb_getdev *)arg; 220 if (arg == NULL) { 221 break; 222 } 223 224 if (enc_type(cgd) == ENC_NONE) { 225 /* 226 * Schedule announcement of the ENC bindings for 227 * this device if it is managed by a SEP. 228 */ 229 path_id = xpt_path_path_id(path); 230 xpt_lock_buses(); 231 TAILQ_FOREACH(periph, &encdriver.units, unit_links) { 232 struct enc_softc *softc; 233 234 softc = (struct enc_softc *)periph->softc; 235 if (xpt_path_path_id(periph->path) != path_id 236 || softc == NULL 237 || (softc->enc_flags & ENC_FLAG_INITIALIZED) 238 == 0 239 || softc->enc_vec.device_found == NULL) 240 continue; 241 242 softc->enc_vec.device_found(softc); 243 } 244 xpt_unlock_buses(); 245 return; 246 } 247 248 status = cam_periph_alloc(enc_ctor, enc_oninvalidate, 249 enc_dtor, NULL, "ses", CAM_PERIPH_BIO, 250 path, enc_async, AC_FOUND_DEVICE, cgd); 251 252 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) { 253 printf("enc_async: Unable to probe new device due to " 254 "status 0x%x\n", status); 255 } 256 break; 257 } 258 default: 259 cam_periph_async(periph, code, path, arg); 260 break; 261 } 262 } 263 264 static int 265 enc_open(struct cdev *dev, int flags, int fmt, struct thread *td) 266 { 267 struct cam_periph *periph; 268 struct enc_softc *softc; 269 int error = 0; 270 271 periph = (struct cam_periph *)dev->si_drv1; 272 if (cam_periph_acquire(periph) != 0) 273 return (ENXIO); 274 275 cam_periph_lock(periph); 276 277 softc = (struct enc_softc *)periph->softc; 278 279 if ((softc->enc_flags & ENC_FLAG_INITIALIZED) == 0) { 280 error = ENXIO; 281 goto out; 282 } 283 if (softc->enc_flags & ENC_FLAG_INVALID) { 284 error = ENXIO; 285 goto out; 286 } 287 out: 288 if (error != 0) 289 cam_periph_release_locked(periph); 290 else 291 softc->open_count++; 292 293 cam_periph_unlock(periph); 294 295 return (error); 296 } 297 298 static int 299 enc_close(struct cdev *dev, int flag, int fmt, struct thread *td) 300 { 301 struct cam_periph *periph; 302 struct enc_softc *enc; 303 struct mtx *mtx; 304 305 periph = (struct cam_periph *)dev->si_drv1; 306 mtx = cam_periph_mtx(periph); 307 mtx_lock(mtx); 308 309 enc = periph->softc; 310 enc->open_count--; 311 312 cam_periph_release_locked(periph); 313 314 /* 315 * We reference the lock directly here, instead of using 316 * cam_periph_unlock(). The reason is that the call to 317 * cam_periph_release_locked() above could result in the periph 318 * getting freed. If that is the case, dereferencing the periph 319 * with a cam_periph_unlock() call would cause a page fault. 320 * 321 * cam_periph_release() avoids this problem using the same method, 322 * but we're manually acquiring and dropping the lock here to 323 * protect the open count and avoid another lock acquisition and 324 * release. 325 */ 326 mtx_unlock(mtx); 327 328 return (0); 329 } 330 331 int 332 enc_error(union ccb *ccb, uint32_t cflags, uint32_t sflags) 333 { 334 struct enc_softc *softc; 335 struct cam_periph *periph; 336 337 periph = xpt_path_periph(ccb->ccb_h.path); 338 softc = (struct enc_softc *)periph->softc; 339 340 return (cam_periph_error(ccb, cflags, sflags)); 341 } 342 343 static int 344 enc_ioctl(struct cdev *dev, u_long cmd, caddr_t arg_addr, int flag, 345 struct thread *td) 346 { 347 struct cam_periph *periph; 348 encioc_enc_status_t tmp; 349 encioc_string_t sstr; 350 encioc_elm_status_t elms; 351 encioc_elm_desc_t elmd; 352 encioc_elm_devnames_t elmdn; 353 encioc_element_t *uelm; 354 enc_softc_t *enc; 355 enc_cache_t *cache; 356 void *addr; 357 int error, i; 358 359 #ifdef COMPAT_FREEBSD32 360 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) 361 return (ENOTTY); 362 #endif 363 364 if (arg_addr) 365 addr = *((caddr_t *) arg_addr); 366 else 367 addr = NULL; 368 369 periph = (struct cam_periph *)dev->si_drv1; 370 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering encioctl\n")); 371 372 cam_periph_lock(periph); 373 enc = (struct enc_softc *)periph->softc; 374 cache = &enc->enc_cache; 375 376 /* 377 * Now check to see whether we're initialized or not. 378 * This actually should never fail as we're not supposed 379 * to get past enc_open w/o successfully initializing 380 * things. 381 */ 382 if ((enc->enc_flags & ENC_FLAG_INITIALIZED) == 0) { 383 cam_periph_unlock(periph); 384 return (ENXIO); 385 } 386 cam_periph_unlock(periph); 387 388 error = 0; 389 390 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 391 ("trying to do ioctl %#lx\n", cmd)); 392 393 /* 394 * If this command can change the device's state, 395 * we must have the device open for writing. 396 * 397 * For commands that get information about the 398 * device- we don't need to lock the peripheral 399 * if we aren't running a command. The periph 400 * also can't go away while a user process has 401 * it open. 402 */ 403 switch (cmd) { 404 case ENCIOC_GETNELM: 405 case ENCIOC_GETELMMAP: 406 case ENCIOC_GETENCSTAT: 407 case ENCIOC_GETELMSTAT: 408 case ENCIOC_GETELMDESC: 409 case ENCIOC_GETELMDEVNAMES: 410 case ENCIOC_GETENCNAME: 411 case ENCIOC_GETENCID: 412 break; 413 default: 414 if ((flag & FWRITE) == 0) { 415 return (EBADF); 416 } 417 } 418 419 /* 420 * XXX The values read here are only valid for the current 421 * configuration generation. We need these ioctls 422 * to also pass in/out a generation number. 423 */ 424 sx_slock(&enc->enc_cache_lock); 425 switch (cmd) { 426 case ENCIOC_GETNELM: 427 error = copyout(&cache->nelms, addr, sizeof (cache->nelms)); 428 break; 429 430 case ENCIOC_GETELMMAP: 431 for (uelm = addr, i = 0; i != cache->nelms; i++) { 432 encioc_element_t kelm; 433 kelm.elm_idx = i; 434 kelm.elm_subenc_id = cache->elm_map[i].subenclosure; 435 kelm.elm_type = cache->elm_map[i].enctype; 436 error = copyout(&kelm, &uelm[i], sizeof(kelm)); 437 if (error) 438 break; 439 } 440 break; 441 442 case ENCIOC_GETENCSTAT: 443 cam_periph_lock(periph); 444 error = enc->enc_vec.get_enc_status(enc, 1); 445 if (error) { 446 cam_periph_unlock(periph); 447 break; 448 } 449 tmp = cache->enc_status; 450 cam_periph_unlock(periph); 451 error = copyout(&tmp, addr, sizeof(tmp)); 452 cache->enc_status = tmp; 453 break; 454 455 case ENCIOC_SETENCSTAT: 456 error = copyin(addr, &tmp, sizeof(tmp)); 457 if (error) 458 break; 459 cam_periph_lock(periph); 460 error = enc->enc_vec.set_enc_status(enc, tmp, 1); 461 cam_periph_unlock(periph); 462 break; 463 464 case ENCIOC_GETSTRING: 465 case ENCIOC_SETSTRING: 466 case ENCIOC_GETENCNAME: 467 case ENCIOC_GETENCID: 468 if (enc->enc_vec.handle_string == NULL) { 469 error = EINVAL; 470 break; 471 } 472 error = copyin(addr, &sstr, sizeof(sstr)); 473 if (error) 474 break; 475 cam_periph_lock(periph); 476 error = enc->enc_vec.handle_string(enc, &sstr, cmd); 477 cam_periph_unlock(periph); 478 break; 479 480 case ENCIOC_GETELMSTAT: 481 error = copyin(addr, &elms, sizeof(elms)); 482 if (error) 483 break; 484 if (elms.elm_idx >= cache->nelms) { 485 error = EINVAL; 486 break; 487 } 488 cam_periph_lock(periph); 489 error = enc->enc_vec.get_elm_status(enc, &elms, 1); 490 cam_periph_unlock(periph); 491 if (error) 492 break; 493 error = copyout(&elms, addr, sizeof(elms)); 494 break; 495 496 case ENCIOC_GETELMDESC: 497 error = copyin(addr, &elmd, sizeof(elmd)); 498 if (error) 499 break; 500 if (elmd.elm_idx >= cache->nelms) { 501 error = EINVAL; 502 break; 503 } 504 if (enc->enc_vec.get_elm_desc != NULL) { 505 error = enc->enc_vec.get_elm_desc(enc, &elmd); 506 if (error) 507 break; 508 } else 509 elmd.elm_desc_len = 0; 510 error = copyout(&elmd, addr, sizeof(elmd)); 511 break; 512 513 case ENCIOC_GETELMDEVNAMES: 514 if (enc->enc_vec.get_elm_devnames == NULL) { 515 error = EINVAL; 516 break; 517 } 518 error = copyin(addr, &elmdn, sizeof(elmdn)); 519 if (error) 520 break; 521 if (elmdn.elm_idx >= cache->nelms) { 522 error = EINVAL; 523 break; 524 } 525 cam_periph_lock(periph); 526 error = (*enc->enc_vec.get_elm_devnames)(enc, &elmdn); 527 cam_periph_unlock(periph); 528 if (error) 529 break; 530 error = copyout(&elmdn, addr, sizeof(elmdn)); 531 break; 532 533 case ENCIOC_SETELMSTAT: 534 error = copyin(addr, &elms, sizeof(elms)); 535 if (error) 536 break; 537 538 if (elms.elm_idx >= cache->nelms) { 539 error = EINVAL; 540 break; 541 } 542 cam_periph_lock(periph); 543 error = enc->enc_vec.set_elm_status(enc, &elms, 1); 544 cam_periph_unlock(periph); 545 546 break; 547 548 case ENCIOC_INIT: 549 550 cam_periph_lock(periph); 551 error = enc->enc_vec.init_enc(enc); 552 cam_periph_unlock(periph); 553 break; 554 555 default: 556 cam_periph_lock(periph); 557 error = cam_periph_ioctl(periph, cmd, arg_addr, enc_error); 558 cam_periph_unlock(periph); 559 break; 560 } 561 sx_sunlock(&enc->enc_cache_lock); 562 return (error); 563 } 564 565 int 566 enc_runcmd(struct enc_softc *enc, char *cdb, int cdbl, char *dptr, int *dlenp) 567 { 568 int error, dlen, tdlen; 569 ccb_flags ddf; 570 union ccb *ccb; 571 572 CAM_DEBUG(enc->periph->path, CAM_DEBUG_TRACE, 573 ("entering enc_runcmd\n")); 574 if (dptr) { 575 if ((dlen = *dlenp) < 0) { 576 dlen = -dlen; 577 ddf = CAM_DIR_OUT; 578 } else { 579 ddf = CAM_DIR_IN; 580 } 581 } else { 582 dlen = 0; 583 ddf = CAM_DIR_NONE; 584 } 585 586 if (cdbl > IOCDBLEN) { 587 cdbl = IOCDBLEN; 588 } 589 590 ccb = cam_periph_getccb(enc->periph, CAM_PRIORITY_NORMAL); 591 if (enc->enc_type == ENC_SEMB_SES || enc->enc_type == ENC_SEMB_SAFT) { 592 tdlen = min(dlen, 1020); 593 tdlen = (tdlen + 3) & ~3; 594 cam_fill_ataio(&ccb->ataio, 0, NULL, ddf, 0, dptr, tdlen, 595 30 * 1000); 596 if (cdb[0] == RECEIVE_DIAGNOSTIC) 597 ata_28bit_cmd(&ccb->ataio, 598 ATA_SEP_ATTN, cdb[2], 0x02, tdlen / 4); 599 else if (cdb[0] == SEND_DIAGNOSTIC) 600 ata_28bit_cmd(&ccb->ataio, 601 ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0, 602 0x82, tdlen / 4); 603 else if (cdb[0] == READ_BUFFER) 604 ata_28bit_cmd(&ccb->ataio, 605 ATA_SEP_ATTN, cdb[2], 0x00, tdlen / 4); 606 else 607 ata_28bit_cmd(&ccb->ataio, 608 ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0, 609 0x80, tdlen / 4); 610 } else { 611 tdlen = dlen; 612 cam_fill_csio(&ccb->csio, 0, NULL, ddf, MSG_SIMPLE_Q_TAG, 613 dptr, dlen, sizeof (struct scsi_sense_data), cdbl, 614 60 * 1000); 615 bcopy(cdb, ccb->csio.cdb_io.cdb_bytes, cdbl); 616 } 617 618 error = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL); 619 if (error) { 620 if (dptr) { 621 *dlenp = dlen; 622 } 623 } else { 624 if (dptr) { 625 if (ccb->ccb_h.func_code == XPT_ATA_IO) 626 *dlenp = ccb->ataio.resid; 627 else 628 *dlenp = ccb->csio.resid; 629 *dlenp += tdlen - dlen; 630 } 631 } 632 xpt_release_ccb(ccb); 633 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE, 634 ("exiting enc_runcmd: *dlenp = %d\n", *dlenp)); 635 return (error); 636 } 637 638 void 639 enc_log(struct enc_softc *enc, const char *fmt, ...) 640 { 641 va_list ap; 642 643 printf("%s%d: ", enc->periph->periph_name, enc->periph->unit_number); 644 va_start(ap, fmt); 645 vprintf(fmt, ap); 646 va_end(ap); 647 } 648 649 /* 650 * The code after this point runs on many platforms, 651 * so forgive the slightly awkward and nonconforming 652 * appearance. 653 */ 654 655 /* 656 * Is this a device that supports enclosure services? 657 * 658 * It's a pretty simple ruleset- if it is device type 659 * 0x0D (13), it's an ENCLOSURE device. 660 */ 661 662 #define SAFTE_START 44 663 #define SAFTE_END 50 664 #define SAFTE_LEN SAFTE_END-SAFTE_START 665 666 static enctyp 667 enc_type(struct ccb_getdev *cgd) 668 { 669 int buflen; 670 unsigned char *iqd; 671 672 if (cgd->protocol == PROTO_SEMB) { 673 iqd = (unsigned char *)&cgd->ident_data; 674 if (STRNCMP(iqd + 43, "S-E-S", 5) == 0) 675 return (ENC_SEMB_SES); 676 else if (STRNCMP(iqd + 43, "SAF-TE", 6) == 0) 677 return (ENC_SEMB_SAFT); 678 return (ENC_NONE); 679 680 } else if (cgd->protocol != PROTO_SCSI) 681 return (ENC_NONE); 682 683 iqd = (unsigned char *)&cgd->inq_data; 684 buflen = min(sizeof(cgd->inq_data), 685 SID_ADDITIONAL_LENGTH(&cgd->inq_data)); 686 687 if ((iqd[0] & 0x1f) == T_ENCLOSURE) { 688 if ((iqd[2] & 0x7) > 2) { 689 return (ENC_SES); 690 } else { 691 return (ENC_SES_SCSI2); 692 } 693 return (ENC_NONE); 694 } 695 696 #ifdef SES_ENABLE_PASSTHROUGH 697 if ((iqd[6] & 0x40) && (iqd[2] & 0x7) >= 2) { 698 /* 699 * PassThrough Device. 700 */ 701 return (ENC_SES_PASSTHROUGH); 702 } 703 #endif 704 705 /* 706 * The comparison is short for a reason- 707 * some vendors were chopping it short. 708 */ 709 710 if (buflen < SAFTE_END - 2) { 711 return (ENC_NONE); 712 } 713 714 if (STRNCMP((char *)&iqd[SAFTE_START], "SAF-TE", SAFTE_LEN - 2) == 0) { 715 return (ENC_SAFT); 716 } 717 return (ENC_NONE); 718 } 719 720 /*================== Enclosure Monitoring/Processing Daemon ==================*/ 721 /** 722 * \brief Queue an update request for a given action, if needed. 723 * 724 * \param enc SES softc to queue the request for. 725 * \param action Action requested. 726 */ 727 void 728 enc_update_request(enc_softc_t *enc, uint32_t action) 729 { 730 if ((enc->pending_actions & (0x1 << action)) == 0) { 731 enc->pending_actions |= (0x1 << action); 732 ENC_DLOG(enc, "%s: queing requested action %d\n", 733 __func__, action); 734 if (enc->current_action == ENC_UPDATE_NONE) 735 wakeup(enc->enc_daemon); 736 } else { 737 ENC_DLOG(enc, "%s: ignoring requested action %d - " 738 "Already queued\n", __func__, action); 739 } 740 } 741 742 /** 743 * \brief Invoke the handler of the highest priority pending 744 * state in the SES state machine. 745 * 746 * \param enc The SES instance invoking the state machine. 747 */ 748 static void 749 enc_fsm_step(enc_softc_t *enc) 750 { 751 union ccb *ccb; 752 uint8_t *buf; 753 struct enc_fsm_state *cur_state; 754 int error; 755 uint32_t xfer_len; 756 757 ENC_DLOG(enc, "%s enter %p\n", __func__, enc); 758 759 enc->current_action = ffs(enc->pending_actions) - 1; 760 enc->pending_actions &= ~(0x1 << enc->current_action); 761 762 cur_state = &enc->enc_fsm_states[enc->current_action]; 763 764 buf = NULL; 765 if (cur_state->buf_size != 0) { 766 cam_periph_unlock(enc->periph); 767 buf = malloc(cur_state->buf_size, M_SCSIENC, M_WAITOK|M_ZERO); 768 cam_periph_lock(enc->periph); 769 } 770 771 error = 0; 772 ccb = NULL; 773 if (cur_state->fill != NULL) { 774 ccb = cam_periph_getccb(enc->periph, CAM_PRIORITY_NORMAL); 775 776 error = cur_state->fill(enc, cur_state, ccb, buf); 777 if (error != 0) 778 goto done; 779 780 error = cam_periph_runccb(ccb, cur_state->error, 781 ENC_CFLAGS, 782 ENC_FLAGS|SF_QUIET_IR, NULL); 783 } 784 785 if (ccb != NULL) { 786 if (ccb->ccb_h.func_code == XPT_ATA_IO) 787 xfer_len = ccb->ataio.dxfer_len - ccb->ataio.resid; 788 else 789 xfer_len = ccb->csio.dxfer_len - ccb->csio.resid; 790 } else 791 xfer_len = 0; 792 793 cam_periph_unlock(enc->periph); 794 cur_state->done(enc, cur_state, ccb, &buf, error, xfer_len); 795 cam_periph_lock(enc->periph); 796 797 done: 798 ENC_DLOG(enc, "%s exit - result %d\n", __func__, error); 799 ENC_FREE_AND_NULL(buf); 800 if (ccb != NULL) 801 xpt_release_ccb(ccb); 802 } 803 804 /** 805 * \invariant Called with cam_periph mutex held. 806 */ 807 static void 808 enc_status_updater(void *arg) 809 { 810 enc_softc_t *enc; 811 812 enc = arg; 813 if (enc->enc_vec.poll_status != NULL) 814 enc->enc_vec.poll_status(enc); 815 } 816 817 static void 818 enc_daemon(void *arg) 819 { 820 enc_softc_t *enc; 821 822 enc = arg; 823 824 cam_periph_lock(enc->periph); 825 while ((enc->enc_flags & ENC_FLAG_SHUTDOWN) == 0) { 826 if (enc->pending_actions == 0) { 827 struct intr_config_hook *hook; 828 829 /* 830 * Reset callout and msleep, or 831 * issue timed task completion 832 * status command. 833 */ 834 enc->current_action = ENC_UPDATE_NONE; 835 836 /* 837 * We've been through our state machine at least 838 * once. Allow the transition to userland. 839 */ 840 hook = &enc->enc_boot_hold_ch; 841 if (hook->ich_func != NULL) { 842 config_intrhook_disestablish(hook); 843 hook->ich_func = NULL; 844 } 845 846 callout_reset(&enc->status_updater, 60*hz, 847 enc_status_updater, enc); 848 849 cam_periph_sleep(enc->periph, enc->enc_daemon, 850 PUSER, "idle", 0); 851 } else { 852 enc_fsm_step(enc); 853 } 854 } 855 enc->enc_daemon = NULL; 856 cam_periph_unlock(enc->periph); 857 cam_periph_release(enc->periph); 858 kproc_exit(0); 859 } 860 861 static int 862 enc_kproc_init(enc_softc_t *enc) 863 { 864 int result; 865 866 callout_init_mtx(&enc->status_updater, cam_periph_mtx(enc->periph), 0); 867 868 if (cam_periph_acquire(enc->periph) != 0) 869 return (ENXIO); 870 871 result = kproc_create(enc_daemon, enc, &enc->enc_daemon, /*flags*/0, 872 /*stackpgs*/0, "enc_daemon%d", 873 enc->periph->unit_number); 874 if (result == 0) { 875 /* Do an initial load of all page data. */ 876 cam_periph_lock(enc->periph); 877 enc->enc_vec.poll_status(enc); 878 cam_periph_unlock(enc->periph); 879 } else 880 cam_periph_release(enc->periph); 881 return (result); 882 } 883 884 /** 885 * \brief Interrupt configuration hook callback associated with 886 * enc_boot_hold_ch. 887 * 888 * Since interrupts are always functional at the time of enclosure 889 * configuration, there is nothing to be done when the callback occurs. 890 * This hook is only registered to hold up boot processing while initial 891 * eclosure processing occurs. 892 * 893 * \param arg The enclosure softc, but currently unused in this callback. 894 */ 895 static void 896 enc_nop_confighook_cb(void *arg __unused) 897 { 898 } 899 900 static cam_status 901 enc_ctor(struct cam_periph *periph, void *arg) 902 { 903 cam_status status = CAM_REQ_CMP_ERR; 904 int err; 905 enc_softc_t *enc; 906 struct ccb_getdev *cgd; 907 char *tname; 908 struct make_dev_args args; 909 struct sbuf sb; 910 911 cgd = (struct ccb_getdev *)arg; 912 if (cgd == NULL) { 913 printf("enc_ctor: no getdev CCB, can't register device\n"); 914 goto out; 915 } 916 917 enc = ENC_MALLOCZ(sizeof(*enc)); 918 if (enc == NULL) { 919 printf("enc_ctor: Unable to probe new device. " 920 "Unable to allocate enc\n"); 921 goto out; 922 } 923 enc->periph = periph; 924 enc->current_action = ENC_UPDATE_INVALID; 925 926 enc->enc_type = enc_type(cgd); 927 sx_init(&enc->enc_cache_lock, "enccache"); 928 929 switch (enc->enc_type) { 930 case ENC_SES: 931 case ENC_SES_SCSI2: 932 case ENC_SES_PASSTHROUGH: 933 case ENC_SEMB_SES: 934 err = ses_softc_init(enc); 935 break; 936 case ENC_SAFT: 937 case ENC_SEMB_SAFT: 938 err = safte_softc_init(enc); 939 break; 940 case ENC_NONE: 941 default: 942 ENC_FREE(enc); 943 return (CAM_REQ_CMP_ERR); 944 } 945 946 if (err) { 947 xpt_print(periph->path, "error %d initializing\n", err); 948 goto out; 949 } 950 951 /* 952 * Hold off userland until we have made at least one pass 953 * through our state machine so that physical path data is 954 * present. 955 */ 956 if (enc->enc_vec.poll_status != NULL) { 957 enc->enc_boot_hold_ch.ich_func = enc_nop_confighook_cb; 958 enc->enc_boot_hold_ch.ich_arg = enc; 959 config_intrhook_establish(&enc->enc_boot_hold_ch); 960 } 961 962 /* 963 * The softc field is set only once the enc is fully initialized 964 * so that we can rely on this field to detect partially 965 * initialized periph objects in the AC_FOUND_DEVICE handler. 966 */ 967 periph->softc = enc; 968 969 cam_periph_unlock(periph); 970 if (enc->enc_vec.poll_status != NULL) { 971 err = enc_kproc_init(enc); 972 if (err) { 973 xpt_print(periph->path, 974 "error %d starting enc_daemon\n", err); 975 goto out; 976 } 977 } 978 979 /* 980 * Acquire a reference to the periph before we create the devfs 981 * instance for it. We'll release this reference once the devfs 982 * instance has been freed. 983 */ 984 if (cam_periph_acquire(periph) != 0) { 985 xpt_print(periph->path, "%s: lost periph during " 986 "registration!\n", __func__); 987 cam_periph_lock(periph); 988 989 return (CAM_REQ_CMP_ERR); 990 } 991 992 make_dev_args_init(&args); 993 args.mda_devsw = &enc_cdevsw; 994 args.mda_unit = periph->unit_number; 995 args.mda_uid = UID_ROOT; 996 args.mda_gid = GID_OPERATOR; 997 args.mda_mode = 0600; 998 args.mda_si_drv1 = periph; 999 err = make_dev_s(&args, &enc->enc_dev, "%s%d", periph->periph_name, 1000 periph->unit_number); 1001 cam_periph_lock(periph); 1002 if (err != 0) { 1003 cam_periph_release_locked(periph); 1004 return (CAM_REQ_CMP_ERR); 1005 } 1006 1007 enc->enc_flags |= ENC_FLAG_INITIALIZED; 1008 1009 /* 1010 * Add an async callback so that we get notified if this 1011 * device goes away. 1012 */ 1013 xpt_register_async(AC_LOST_DEVICE, enc_async, periph, periph->path); 1014 1015 switch (enc->enc_type) { 1016 default: 1017 case ENC_NONE: 1018 tname = "No ENC device"; 1019 break; 1020 case ENC_SES_SCSI2: 1021 tname = "SCSI-2 ENC Device"; 1022 break; 1023 case ENC_SES: 1024 tname = "SCSI-3 ENC Device"; 1025 break; 1026 case ENC_SES_PASSTHROUGH: 1027 tname = "ENC Passthrough Device"; 1028 break; 1029 case ENC_SAFT: 1030 tname = "SAF-TE Compliant Device"; 1031 break; 1032 case ENC_SEMB_SES: 1033 tname = "SEMB SES Device"; 1034 break; 1035 case ENC_SEMB_SAFT: 1036 tname = "SEMB SAF-TE Device"; 1037 break; 1038 } 1039 1040 sbuf_new(&sb, enc->announce_buf, ENC_ANNOUNCE_SZ, SBUF_FIXEDLEN); 1041 xpt_announce_periph_sbuf(periph, &sb, tname); 1042 sbuf_finish(&sb); 1043 sbuf_putbuf(&sb); 1044 1045 status = CAM_REQ_CMP; 1046 1047 out: 1048 if (status != CAM_REQ_CMP) 1049 enc_dtor(periph); 1050 return (status); 1051 } 1052 1053