1/*- 2 * Copyright (c) 1997 Justin T. Gibbs. 3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 54 unchanged lines hidden (view full) --- 63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 64 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 65 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * SUCH DAMAGE. 68 */ 69 70#include <sys/cdefs.h> |
71__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_ch.c 168752 2007-04-15 08:49:19Z scottl $"); |
72 73#include <sys/param.h> 74#include <sys/queue.h> 75#include <sys/systm.h> 76#include <sys/kernel.h> 77#include <sys/types.h> 78#include <sys/malloc.h> 79#include <sys/fcntl.h> --- 88 unchanged lines hidden (view full) --- 168 u_int8_t sc_exchangemask[CHET_MAX + 1]; 169 170 /* 171 * Quirks; see below. XXX KDM not implemented yet 172 */ 173 int sc_settledelay; /* delay for settle */ 174}; 175 |
176static d_open_t chopen; 177static d_close_t chclose; 178static d_ioctl_t chioctl; 179static periph_init_t chinit; 180static periph_ctor_t chregister; 181static periph_oninv_t choninvalidate; 182static periph_dtor_t chcleanup; 183static periph_start_t chstart; --- 22 unchanged lines hidden (view full) --- 206 chinit, "ch", 207 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0 208}; 209 210PERIPHDRIVER_DECLARE(ch, chdriver); 211 212static struct cdevsw ch_cdevsw = { 213 .d_version = D_VERSION, |
214 .d_flags = 0, |
215 .d_open = chopen, 216 .d_close = chclose, 217 .d_ioctl = chioctl, 218 .d_name = "ch", 219}; 220 221static void 222chinit(void) --- 146 unchanged lines hidden (view full) --- 369 */ 370 softc->device_stats = devstat_new_entry("ch", 371 periph->unit_number, 0, 372 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS, 373 SID_TYPE(&cgd->inq_data)| DEVSTAT_TYPE_IF_SCSI, 374 DEVSTAT_PRIORITY_OTHER); 375 376 /* Register the device */ |
377 cam_periph_unlock(periph); |
378 softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT, 379 GID_OPERATOR, 0600, "%s%d", periph->periph_name, 380 periph->unit_number); |
381 cam_periph_lock(periph); |
382 softc->dev->si_drv1 = periph; 383 384 /* 385 * Add an async callback so that we get 386 * notified if this device goes away. 387 */ 388 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); 389 csa.ccb_h.func_code = XPT_SASYNC_CB; 390 csa.event_enable = AC_LOST_DEVICE; 391 csa.callback = chasync; 392 csa.callback_arg = periph; 393 xpt_action((union ccb *)&csa); 394 395 /* |
396 * Lock this periph until we are setup. |
397 * This first call can't block 398 */ |
399 (void)cam_periph_hold(periph, PRIBIO); |
400 xpt_schedule(periph, /*priority*/5); 401 402 return(CAM_REQ_CMP); 403} 404 405static int 406chopen(struct cdev *dev, int flags, int fmt, struct thread *td) 407{ 408 struct cam_periph *periph; 409 struct ch_softc *softc; 410 int error; |
411 412 periph = (struct cam_periph *)dev->si_drv1; |
413 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 414 return (ENXIO); |
415 416 softc = (struct ch_softc *)periph->softc; 417 |
418 cam_periph_lock(periph); 419 |
420 if (softc->flags & CH_FLAG_INVALID) { |
421 cam_periph_unlock(periph); 422 cam_periph_release(periph); |
423 return(ENXIO); 424 } 425 |
426 if ((softc->flags & CH_FLAG_OPEN) == 0) 427 softc->flags |= CH_FLAG_OPEN; 428 else 429 cam_periph_release(periph); 430 431 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 432 cam_periph_unlock(periph); 433 cam_periph_release(periph); |
434 return (error); 435 } |
436 |
437 /* 438 * Load information about this changer device into the softc. 439 */ 440 if ((error = chgetparams(periph)) != 0) { 441 softc->flags &= ~CH_FLAG_OPEN; 442 cam_periph_unlock(periph); 443 cam_periph_release(periph); 444 return(error); 445 } 446 |
447 cam_periph_unhold(periph); |
448 cam_periph_unlock(periph); 449 450 return(error); 451} 452 453static int 454chclose(struct cdev *dev, int flag, int fmt, struct thread *td) 455{ --- 4 unchanged lines hidden (view full) --- 460 error = 0; 461 462 periph = (struct cam_periph *)dev->si_drv1; 463 if (periph == NULL) 464 return(ENXIO); 465 466 softc = (struct ch_softc *)periph->softc; 467 |
468 cam_periph_lock(periph); |
469 470 softc->flags &= ~CH_FLAG_OPEN; 471 472 cam_periph_unlock(periph); 473 cam_periph_release(periph); 474 475 return(0); 476} 477 478static void 479chstart(struct cam_periph *periph, union ccb *start_ccb) 480{ 481 struct ch_softc *softc; |
482 483 softc = (struct ch_softc *)periph->softc; 484 485 switch (softc->state) { 486 case CH_STATE_NORMAL: 487 { |
488 if (periph->immediate_priority <= periph->pinfo.priority){ 489 start_ccb->ccb_h.ccb_state = CH_CCB_WAITING; 490 491 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 492 periph_links.sle); 493 periph->immediate_priority = CAM_PRIORITY_NONE; |
494 wakeup(&periph->ccb_list); |
495 } |
496 break; 497 } 498 case CH_STATE_PROBE: 499 { 500 int mode_buffer_len; 501 void *mode_buffer; 502 503 /* --- 156 unchanged lines hidden (view full) --- 660 * Since our peripheral may be invalidated by an error 661 * above or an external event, we must release our CCB 662 * before releasing the probe lock on the peripheral. 663 * The peripheral will only go away once the last lock 664 * is removed, and we need it around for the CCB release 665 * operation. 666 */ 667 xpt_release_ccb(done_ccb); |
668 cam_periph_unhold(periph); |
669 return; 670 } 671 case CH_CCB_WAITING: 672 { 673 /* Caller will release the CCB */ 674 wakeup(&done_ccb->ccb_h.cbfcnp); 675 return; 676 } --- 22 unchanged lines hidden (view full) --- 699 struct cam_periph *periph; 700 struct ch_softc *softc; 701 int error; 702 703 periph = (struct cam_periph *)dev->si_drv1; 704 if (periph == NULL) 705 return(ENXIO); 706 |
707 cam_periph_lock(periph); |
708 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n")); 709 710 softc = (struct ch_softc *)periph->softc; 711 712 error = 0; 713 714 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 715 ("trying to do ioctl %#lx\n", cmd)); --- 4 unchanged lines hidden (view full) --- 720 */ 721 switch (cmd) { 722 case CHIOGPICKER: 723 case CHIOGPARAMS: 724 case CHIOGSTATUS: 725 break; 726 727 default: |
728 if ((flag & FWRITE) == 0) { 729 cam_periph_unlock(periph); |
730 return (EBADF); |
731 } |
732 } 733 734 switch (cmd) { 735 case CHIOMOVE: 736 error = chmove(periph, (struct changer_move *)addr); 737 break; 738 739 case CHIOEXCHANGE: --- 7 unchanged lines hidden (view full) --- 747 case CHIOGPICKER: 748 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT]; 749 break; 750 751 case CHIOSPICKER: 752 { 753 int new_picker = *(int *)addr; 754 |
755 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) { 756 error = EINVAL; 757 break; 758 } |
759 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker; 760 break; 761 } 762 case CHIOGPARAMS: 763 { 764 struct changer_params *cp = (struct changer_params *)addr; 765 766 cp->cp_npickers = softc->sc_counts[CHET_MT]; --- 22 unchanged lines hidden (view full) --- 789 790 /* Implement prevent/allow? */ 791 792 default: 793 error = cam_periph_ioctl(periph, cmd, addr, cherror); 794 break; 795 } 796 |
797 cam_periph_unlock(periph); |
798 return (error); 799} 800 801static int 802chmove(struct cam_periph *periph, struct changer_move *cm) 803{ 804 struct ch_softc *softc; 805 u_int16_t fromelem, toelem; --- 281 unchanged lines hidden (view full) --- 1087 return (EINVAL); 1088 1089 /* 1090 * Request one descriptor for the given element type. This 1091 * is used to determine the size of the descriptor so that 1092 * we can allocate enough storage for all of them. We assume 1093 * that the first one can fit into 1k. 1094 */ |
1095 cam_periph_unlock(periph); |
1096 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK); 1097 |
1098 cam_periph_lock(periph); |
1099 ccb = cam_periph_getccb(periph, /*priority*/ 1); 1100 1101 scsi_read_element_status(&ccb->csio, 1102 /* retries */ 1, 1103 /* cbfcnp */ chdone, 1104 /* tag_action */ MSG_SIMPLE_Q_TAG, 1105 /* voltag */ want_voltags, 1106 /* sea */ softc->sc_firsts[chet], --- 4 unchanged lines hidden (view full) --- 1111 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1112 1113 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1114 /*sense_flags*/ SF_RETRY_UA, 1115 softc->device_stats); 1116 1117 if (error) 1118 goto done; |
1119 cam_periph_unlock(periph); |
1120 1121 st_hdr = (struct read_element_status_header *)data; 1122 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr + 1123 sizeof(struct read_element_status_header)); 1124 desclen = scsi_2btoul(pg_hdr->edl); 1125 1126 size = sizeof(struct read_element_status_header) + 1127 sizeof(struct read_element_status_page_header) + 1128 (desclen * cesr->cesr_element_count); 1129 1130 /* 1131 * Reallocate storage for descriptors and get them from the 1132 * device. 1133 */ 1134 free(data, M_DEVBUF); 1135 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK); 1136 |
1137 cam_periph_lock(periph); |
1138 scsi_read_element_status(&ccb->csio, 1139 /* retries */ 1, 1140 /* cbfcnp */ chdone, 1141 /* tag_action */ MSG_SIMPLE_Q_TAG, 1142 /* voltag */ want_voltags, 1143 /* sea */ softc->sc_firsts[chet] 1144 + cesr->cesr_element_base, 1145 /* count */ cesr->cesr_element_count, 1146 /* data_ptr */ data, 1147 /* dxfer_len */ size, 1148 /* sense_len */ SSD_FULL_SIZE, 1149 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1150 1151 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1152 /*sense_flags*/ SF_RETRY_UA, 1153 softc->device_stats); 1154 1155 if (error) 1156 goto done; |
1157 cam_periph_unlock(periph); |
1158 1159 /* 1160 * Fill in the user status array. 1161 */ 1162 st_hdr = (struct read_element_status_header *)data; 1163 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr + 1164 sizeof(struct read_element_status_header)); 1165 avail = scsi_2btoul(st_hdr->count); --- 21 unchanged lines hidden (view full) --- 1187 desc = (struct read_element_status_descriptor *) 1188 ((uintptr_t)desc + desclen); 1189 } 1190 1191 /* Copy element status structures out to userspace. */ 1192 error = copyout(user_data, 1193 cesr->cesr_element_status, 1194 avail * sizeof(struct changer_element_status)); |
1195 cam_periph_lock(periph); |
1196 1197 done: 1198 xpt_release_ccb(ccb); 1199 1200 if (data != NULL) 1201 free(data, M_DEVBUF); 1202 if (user_data != NULL) 1203 free(user_data, M_DEVBUF); --- 488 unchanged lines hidden --- |