760 cam_periph_lock(periph); 761 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n")); 762 763 softc = (struct ch_softc *)periph->softc; 764 765 error = 0; 766 767 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 768 ("trying to do ioctl %#lx\n", cmd)); 769 770 /* 771 * If this command can change the device's state, we must 772 * have the device open for writing. 773 */ 774 switch (cmd) { 775 case CHIOGPICKER: 776 case CHIOGPARAMS: 777 case OCHIOGSTATUS: 778 case CHIOGSTATUS: 779 break; 780 781 default: 782 if ((flag & FWRITE) == 0) { 783 cam_periph_unlock(periph); 784 return (EBADF); 785 } 786 } 787 788 switch (cmd) { 789 case CHIOMOVE: 790 error = chmove(periph, (struct changer_move *)addr); 791 break; 792 793 case CHIOEXCHANGE: 794 error = chexchange(periph, (struct changer_exchange *)addr); 795 break; 796 797 case CHIOPOSITION: 798 error = chposition(periph, (struct changer_position *)addr); 799 break; 800 801 case CHIOGPICKER: 802 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT]; 803 break; 804 805 case CHIOSPICKER: 806 { 807 int new_picker = *(int *)addr; 808 809 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) { 810 error = EINVAL; 811 break; 812 } 813 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker; 814 break; 815 } 816 case CHIOGPARAMS: 817 { 818 struct changer_params *cp = (struct changer_params *)addr; 819 820 cp->cp_npickers = softc->sc_counts[CHET_MT]; 821 cp->cp_nslots = softc->sc_counts[CHET_ST]; 822 cp->cp_nportals = softc->sc_counts[CHET_IE]; 823 cp->cp_ndrives = softc->sc_counts[CHET_DT]; 824 break; 825 } 826 case CHIOIELEM: 827 error = chielem(periph, *(unsigned int *)addr); 828 break; 829 830 case OCHIOGSTATUS: 831 { 832 error = chgetelemstatus(periph, SCSI_REV_2, cmd, 833 (struct changer_element_status_request *)addr); 834 break; 835 } 836 837 case CHIOGSTATUS: 838 { 839 int scsi_version; 840 841 scsi_version = chscsiversion(periph); 842 if (scsi_version >= SCSI_REV_0) { 843 error = chgetelemstatus(periph, scsi_version, cmd, 844 (struct changer_element_status_request *)addr); 845 } 846 else { /* unable to determine the SCSI version */ 847 cam_periph_unlock(periph); 848 return (ENXIO); 849 } 850 break; 851 } 852 853 case CHIOSETVOLTAG: 854 { 855 error = chsetvoltag(periph, 856 (struct changer_set_voltag_request *) addr); 857 break; 858 } 859 860 /* Implement prevent/allow? */ 861 862 default: 863 error = cam_periph_ioctl(periph, cmd, addr, cherror); 864 break; 865 } 866 867 cam_periph_unlock(periph); 868 return (error); 869} 870 871static int 872chmove(struct cam_periph *periph, struct changer_move *cm) 873{ 874 struct ch_softc *softc; 875 u_int16_t fromelem, toelem; 876 union ccb *ccb; 877 int error; 878 879 error = 0; 880 softc = (struct ch_softc *)periph->softc; 881 882 /* 883 * Check arguments. 884 */ 885 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT)) 886 return (EINVAL); 887 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) || 888 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1))) 889 return (ENODEV); 890 891 /* 892 * Check the request against the changer's capabilities. 893 */ 894 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0) 895 return (ENODEV); 896 897 /* 898 * Calculate the source and destination elements. 899 */ 900 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit; 901 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit; 902 903 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 904 905 scsi_move_medium(&ccb->csio, 906 /* retries */ 1, 907 /* cbfcnp */ chdone, 908 /* tag_action */ MSG_SIMPLE_Q_TAG, 909 /* tea */ softc->sc_picker, 910 /* src */ fromelem, 911 /* dst */ toelem, 912 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE, 913 /* sense_len */ SSD_FULL_SIZE, 914 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM); 915 916 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO, 917 /*sense_flags*/ SF_RETRY_UA, 918 softc->device_stats); 919 920 xpt_release_ccb(ccb); 921 922 return(error); 923} 924 925static int 926chexchange(struct cam_periph *periph, struct changer_exchange *ce) 927{ 928 struct ch_softc *softc; 929 u_int16_t src, dst1, dst2; 930 union ccb *ccb; 931 int error; 932 933 error = 0; 934 softc = (struct ch_softc *)periph->softc; 935 /* 936 * Check arguments. 937 */ 938 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) || 939 (ce->ce_sdsttype > CHET_DT)) 940 return (EINVAL); 941 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) || 942 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) || 943 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1))) 944 return (ENODEV); 945 946 /* 947 * Check the request against the changer's capabilities. 948 */ 949 if (((softc->sc_exchangemask[ce->ce_srctype] & 950 (1 << ce->ce_fdsttype)) == 0) || 951 ((softc->sc_exchangemask[ce->ce_fdsttype] & 952 (1 << ce->ce_sdsttype)) == 0)) 953 return (ENODEV); 954 955 /* 956 * Calculate the source and destination elements. 957 */ 958 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit; 959 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit; 960 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit; 961 962 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 963 964 scsi_exchange_medium(&ccb->csio, 965 /* retries */ 1, 966 /* cbfcnp */ chdone, 967 /* tag_action */ MSG_SIMPLE_Q_TAG, 968 /* tea */ softc->sc_picker, 969 /* src */ src, 970 /* dst1 */ dst1, 971 /* dst2 */ dst2, 972 /* invert1 */ (ce->ce_flags & CE_INVERT1) ? 973 TRUE : FALSE, 974 /* invert2 */ (ce->ce_flags & CE_INVERT2) ? 975 TRUE : FALSE, 976 /* sense_len */ SSD_FULL_SIZE, 977 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM); 978 979 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO, 980 /*sense_flags*/ SF_RETRY_UA, 981 softc->device_stats); 982 983 xpt_release_ccb(ccb); 984 985 return(error); 986} 987 988static int 989chposition(struct cam_periph *periph, struct changer_position *cp) 990{ 991 struct ch_softc *softc; 992 u_int16_t dst; 993 union ccb *ccb; 994 int error; 995 996 error = 0; 997 softc = (struct ch_softc *)periph->softc; 998 999 /* 1000 * Check arguments. 1001 */ 1002 if (cp->cp_type > CHET_DT) 1003 return (EINVAL); 1004 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1)) 1005 return (ENODEV); 1006 1007 /* 1008 * Calculate the destination element. 1009 */ 1010 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit; 1011 1012 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1013 1014 scsi_position_to_element(&ccb->csio, 1015 /* retries */ 1, 1016 /* cbfcnp */ chdone, 1017 /* tag_action */ MSG_SIMPLE_Q_TAG, 1018 /* tea */ softc->sc_picker, 1019 /* dst */ dst, 1020 /* invert */ (cp->cp_flags & CP_INVERT) ? 1021 TRUE : FALSE, 1022 /* sense_len */ SSD_FULL_SIZE, 1023 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT); 1024 1025 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1026 /*sense_flags*/ SF_RETRY_UA, 1027 softc->device_stats); 1028 1029 xpt_release_ccb(ccb); 1030 1031 return(error); 1032} 1033 1034/* 1035 * Copy a volume tag to a volume_tag struct, converting SCSI byte order 1036 * to host native byte order in the volume serial number. The volume 1037 * label as returned by the changer is transferred to user mode as 1038 * nul-terminated string. Volume labels are truncated at the first 1039 * space, as suggested by SCSI-2. 1040 */ 1041static void 1042copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag) 1043{ 1044 int i; 1045 for (i=0; i<CH_VOLTAG_MAXLEN; i++) { 1046 char c = voltag->vif[i]; 1047 if (c && c != ' ') 1048 uvoltag->cv_volid[i] = c; 1049 else 1050 break; 1051 } 1052 uvoltag->cv_serial = scsi_2btoul(voltag->vsn); 1053} 1054 1055/* 1056 * Copy an element status descriptor to a user-mode 1057 * changer_element_status structure. 1058 */ 1059static void 1060copy_element_status(struct ch_softc *softc, 1061 u_int16_t flags, 1062 struct read_element_status_descriptor *desc, 1063 struct changer_element_status *ces, 1064 int scsi_version) 1065{ 1066 u_int16_t eaddr = scsi_2btoul(desc->eaddr); 1067 u_int16_t et; 1068 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL; 1069 struct read_element_status_device_id *devid = NULL; 1070 1071 ces->ces_int_addr = eaddr; 1072 /* set up logical address in element status */ 1073 for (et = CHET_MT; et <= CHET_DT; et++) { 1074 if ((softc->sc_firsts[et] <= eaddr) 1075 && ((softc->sc_firsts[et] + softc->sc_counts[et]) 1076 > eaddr)) { 1077 ces->ces_addr = eaddr - softc->sc_firsts[et]; 1078 ces->ces_type = et; 1079 break; 1080 } 1081 } 1082 1083 ces->ces_flags = desc->flags1; 1084 1085 ces->ces_sensecode = desc->sense_code; 1086 ces->ces_sensequal = desc->sense_qual; 1087 1088 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT) 1089 ces->ces_flags |= CES_INVERT; 1090 1091 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) { 1092 1093 eaddr = scsi_2btoul(desc->ssea); 1094 1095 /* convert source address to logical format */ 1096 for (et = CHET_MT; et <= CHET_DT; et++) { 1097 if ((softc->sc_firsts[et] <= eaddr) 1098 && ((softc->sc_firsts[et] + softc->sc_counts[et]) 1099 > eaddr)) { 1100 ces->ces_source_addr = 1101 eaddr - softc->sc_firsts[et]; 1102 ces->ces_source_type = et; 1103 ces->ces_flags |= CES_SOURCE_VALID; 1104 break; 1105 } 1106 } 1107 1108 if (!(ces->ces_flags & CES_SOURCE_VALID)) 1109 printf("ch: warning: could not map element source " 1110 "address %ud to a valid element type\n", 1111 eaddr); 1112 } 1113 1114 /* 1115 * pvoltag and avoltag are common between SCSI-2 and later versions 1116 */ 1117 if (flags & READ_ELEMENT_STATUS_PVOLTAG) 1118 pvol_tag = &desc->voltag_devid.pvoltag; 1119 if (flags & READ_ELEMENT_STATUS_AVOLTAG) 1120 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ? 1121 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag; 1122 /* 1123 * For SCSI-3 and later, element status can carry designator and 1124 * other information. 1125 */ 1126 if (scsi_version >= SCSI_REV_SPC) { 1127 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^ 1128 (flags & READ_ELEMENT_STATUS_AVOLTAG)) 1129 devid = &desc->voltag_devid.pvol_and_devid.devid; 1130 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) && 1131 !(flags & READ_ELEMENT_STATUS_AVOLTAG)) 1132 devid = &desc->voltag_devid.devid; 1133 else /* Have both PVOLTAG and AVOLTAG */ 1134 devid = &desc->voltag_devid.vol_tags_and_devid.devid; 1135 } 1136 1137 if (pvol_tag) 1138 copy_voltag(&(ces->ces_pvoltag), pvol_tag); 1139 if (avol_tag) 1140 copy_voltag(&(ces->ces_pvoltag), avol_tag); 1141 if (devid != NULL) { 1142 if (devid->designator_length > 0) { 1143 bcopy((void *)devid->designator, 1144 (void *)ces->ces_designator, 1145 devid->designator_length); 1146 ces->ces_designator_length = devid->designator_length; 1147 /* 1148 * Make sure we are always NUL terminated. The 1149 * This won't matter for the binary code set, 1150 * since the user will only pay attention to the 1151 * length field. 1152 */ 1153 ces->ces_designator[devid->designator_length]= '\0'; 1154 } 1155 if (devid->piv_assoc_designator_type & 1156 READ_ELEMENT_STATUS_PIV_SET) { 1157 ces->ces_flags |= CES_PIV; 1158 ces->ces_protocol_id = 1159 READ_ELEMENT_STATUS_PROTOCOL_ID( 1160 devid->prot_code_set); 1161 } 1162 ces->ces_code_set = 1163 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set); 1164 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION( 1165 devid->piv_assoc_designator_type); 1166 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE( 1167 devid->piv_assoc_designator_type); 1168 } else if (scsi_version > SCSI_REV_2) { 1169 /* SCSI-SPC and No devid, no designator */ 1170 ces->ces_designator_length = 0; 1171 ces->ces_designator[0] = '\0'; 1172 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4; 1173 } 1174 1175 if (scsi_version <= SCSI_REV_2) { 1176 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags & 1177 READ_ELEMENT_STATUS_DT_IDVALID) { 1178 ces->ces_flags |= CES_SCSIID_VALID; 1179 ces->ces_scsi_id = 1180 desc->dt_or_obsolete.scsi_2.dt_scsi_addr; 1181 } 1182 1183 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr & 1184 READ_ELEMENT_STATUS_DT_LUVALID) { 1185 ces->ces_flags |= CES_LUN_VALID; 1186 ces->ces_scsi_lun = 1187 desc->dt_or_obsolete.scsi_2.dt_scsi_flags & 1188 READ_ELEMENT_STATUS_DT_LUNMASK; 1189 } 1190 } 1191} 1192 1193static int 1194chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd, 1195 struct changer_element_status_request *cesr) 1196{ 1197 struct read_element_status_header *st_hdr; 1198 struct read_element_status_page_header *pg_hdr; 1199 struct read_element_status_descriptor *desc; 1200 caddr_t data = NULL; 1201 size_t size, desclen; 1202 int avail, i, error = 0; 1203 int curdata, dvcid, sense_flags; 1204 int try_no_dvcid = 0; 1205 struct changer_element_status *user_data = NULL; 1206 struct ch_softc *softc; 1207 union ccb *ccb; 1208 int chet = cesr->cesr_element_type; 1209 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0; 1210 1211 softc = (struct ch_softc *)periph->softc; 1212 1213 /* perform argument checking */ 1214 1215 /* 1216 * Perform a range check on the cesr_element_{base,count} 1217 * request argument fields. 1218 */ 1219 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0 1220 || (cesr->cesr_element_base + cesr->cesr_element_count) 1221 > softc->sc_counts[chet]) 1222 return (EINVAL); 1223 1224 /* 1225 * Request one descriptor for the given element type. This 1226 * is used to determine the size of the descriptor so that 1227 * we can allocate enough storage for all of them. We assume 1228 * that the first one can fit into 1k. 1229 */ 1230 cam_periph_unlock(periph); 1231 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK); 1232 1233 cam_periph_lock(periph); 1234 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1235 1236 sense_flags = SF_RETRY_UA; 1237 if (softc->quirks & CH_Q_NO_DVCID) { 1238 dvcid = 0; 1239 curdata = 0; 1240 } else { 1241 dvcid = 1; 1242 curdata = 1; 1243 /* 1244 * Don't print anything for an Illegal Request, because 1245 * these flags can cause some changers to complain. We'll 1246 * retry without them if we get an error. 1247 */ 1248 sense_flags |= SF_QUIET_IR; 1249 } 1250 1251retry_einval: 1252 1253 scsi_read_element_status(&ccb->csio, 1254 /* retries */ 1, 1255 /* cbfcnp */ chdone, 1256 /* tag_action */ MSG_SIMPLE_Q_TAG, 1257 /* voltag */ want_voltags, 1258 /* sea */ softc->sc_firsts[chet], 1259 /* curdata */ curdata, 1260 /* dvcid */ dvcid, 1261 /* count */ 1, 1262 /* data_ptr */ data, 1263 /* dxfer_len */ 1024, 1264 /* sense_len */ SSD_FULL_SIZE, 1265 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1266 1267 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1268 /*sense_flags*/ sense_flags, 1269 softc->device_stats); 1270 1271 /* 1272 * An Illegal Request sense key (only used if there is no asc/ascq) 1273 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or 1274 * curdata are set (we set both or neither), try turning them off 1275 * and see if the command is successful. 1276 */ 1277 if ((error == EINVAL) 1278 && (dvcid || curdata)) { 1279 dvcid = 0; 1280 curdata = 0; 1281 error = 0; 1282 /* At this point we want to report any Illegal Request */ 1283 sense_flags &= ~SF_QUIET_IR; 1284 try_no_dvcid = 1; 1285 goto retry_einval; 1286 } 1287 1288 /* 1289 * In this case, we tried a read element status with dvcid and 1290 * curdata set, and it failed. We retried without those bits, and 1291 * it succeeded. Suggest to the user that he set a quirk, so we 1292 * don't go through the retry process the first time in the future. 1293 * This should only happen on changers that claim SCSI-3 or higher, 1294 * but don't support these bits. 1295 */ 1296 if ((try_no_dvcid != 0) 1297 && (error == 0)) 1298 softc->quirks |= CH_Q_NO_DVCID; 1299 1300 if (error) 1301 goto done; 1302 cam_periph_unlock(periph); 1303 1304 st_hdr = (struct read_element_status_header *)data; 1305 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr + 1306 sizeof(struct read_element_status_header)); 1307 desclen = scsi_2btoul(pg_hdr->edl); 1308 1309 size = sizeof(struct read_element_status_header) + 1310 sizeof(struct read_element_status_page_header) + 1311 (desclen * cesr->cesr_element_count); 1312 /* 1313 * Reallocate storage for descriptors and get them from the 1314 * device. 1315 */ 1316 free(data, M_DEVBUF); 1317 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK); 1318 1319 cam_periph_lock(periph); 1320 scsi_read_element_status(&ccb->csio, 1321 /* retries */ 1, 1322 /* cbfcnp */ chdone, 1323 /* tag_action */ MSG_SIMPLE_Q_TAG, 1324 /* voltag */ want_voltags, 1325 /* sea */ softc->sc_firsts[chet] 1326 + cesr->cesr_element_base, 1327 /* curdata */ curdata, 1328 /* dvcid */ dvcid, 1329 /* count */ cesr->cesr_element_count, 1330 /* data_ptr */ data, 1331 /* dxfer_len */ size, 1332 /* sense_len */ SSD_FULL_SIZE, 1333 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1334 1335 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1336 /*sense_flags*/ SF_RETRY_UA, 1337 softc->device_stats); 1338 1339 if (error) 1340 goto done; 1341 cam_periph_unlock(periph); 1342 1343 /* 1344 * Fill in the user status array. 1345 */ 1346 st_hdr = (struct read_element_status_header *)data; 1347 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr + 1348 sizeof(struct read_element_status_header)); 1349 avail = scsi_2btoul(st_hdr->count); 1350 1351 if (avail != cesr->cesr_element_count) { 1352 xpt_print(periph->path, 1353 "warning, READ ELEMENT STATUS avail != count\n"); 1354 } 1355 1356 user_data = (struct changer_element_status *) 1357 malloc(avail * sizeof(struct changer_element_status), 1358 M_DEVBUF, M_WAITOK | M_ZERO); 1359 1360 desc = (struct read_element_status_descriptor *)((uintptr_t)data + 1361 sizeof(struct read_element_status_header) + 1362 sizeof(struct read_element_status_page_header)); 1363 /* 1364 * Set up the individual element status structures 1365 */ 1366 for (i = 0; i < avail; ++i) { 1367 struct changer_element_status *ces; 1368 1369 /* 1370 * In the changer_element_status structure, fields from 1371 * the beginning to the field of ces_scsi_lun are common 1372 * between SCSI-2 and SCSI-3, while all the rest are new 1373 * from SCSI-3. In order to maintain backward compatibility 1374 * of the chio command, the ces pointer, below, is computed 1375 * such that it lines up with the structure boundary 1376 * corresponding to the SCSI version. 1377 */ 1378 ces = cmd == OCHIOGSTATUS ? 1379 (struct changer_element_status *) 1380 ((unsigned char *)user_data + i * 1381 (offsetof(struct changer_element_status,ces_scsi_lun)+1)): 1382 &user_data[i]; 1383 1384 copy_element_status(softc, pg_hdr->flags, desc, 1385 ces, scsi_version); 1386 1387 desc = (struct read_element_status_descriptor *) 1388 ((unsigned char *)desc + desclen); 1389 } 1390 1391 /* Copy element status structures out to userspace. */ 1392 if (cmd == OCHIOGSTATUS) 1393 error = copyout(user_data, 1394 cesr->cesr_element_status, 1395 avail* (offsetof(struct changer_element_status, 1396 ces_scsi_lun) + 1)); 1397 else 1398 error = copyout(user_data, 1399 cesr->cesr_element_status, 1400 avail * sizeof(struct changer_element_status)); 1401 1402 cam_periph_lock(periph); 1403 1404 done: 1405 xpt_release_ccb(ccb); 1406 1407 if (data != NULL) 1408 free(data, M_DEVBUF); 1409 if (user_data != NULL) 1410 free(user_data, M_DEVBUF); 1411 1412 return (error); 1413} 1414 1415static int 1416chielem(struct cam_periph *periph, 1417 unsigned int timeout) 1418{ 1419 union ccb *ccb; 1420 struct ch_softc *softc; 1421 int error; 1422 1423 if (!timeout) { 1424 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS; 1425 } else { 1426 timeout *= 1000; 1427 } 1428 1429 error = 0; 1430 softc = (struct ch_softc *)periph->softc; 1431 1432 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1433 1434 scsi_initialize_element_status(&ccb->csio, 1435 /* retries */ 1, 1436 /* cbfcnp */ chdone, 1437 /* tag_action */ MSG_SIMPLE_Q_TAG, 1438 /* sense_len */ SSD_FULL_SIZE, 1439 /* timeout */ timeout); 1440 1441 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1442 /*sense_flags*/ SF_RETRY_UA, 1443 softc->device_stats); 1444 1445 xpt_release_ccb(ccb); 1446 1447 return(error); 1448} 1449 1450static int 1451chsetvoltag(struct cam_periph *periph, 1452 struct changer_set_voltag_request *csvr) 1453{ 1454 union ccb *ccb; 1455 struct ch_softc *softc; 1456 u_int16_t ea; 1457 u_int8_t sac; 1458 struct scsi_send_volume_tag_parameters ssvtp; 1459 int error; 1460 int i; 1461 1462 error = 0; 1463 softc = (struct ch_softc *)periph->softc; 1464 1465 bzero(&ssvtp, sizeof(ssvtp)); 1466 for (i=0; i<sizeof(ssvtp.vitf); i++) { 1467 ssvtp.vitf[i] = ' '; 1468 } 1469 1470 /* 1471 * Check arguments. 1472 */ 1473 if (csvr->csvr_type > CHET_DT) 1474 return EINVAL; 1475 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1)) 1476 return ENODEV; 1477 1478 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr; 1479 1480 if (csvr->csvr_flags & CSVR_ALTERNATE) { 1481 switch (csvr->csvr_flags & CSVR_MODE_MASK) { 1482 case CSVR_MODE_SET: 1483 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE; 1484 break; 1485 case CSVR_MODE_REPLACE: 1486 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE; 1487 break; 1488 case CSVR_MODE_CLEAR: 1489 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE; 1490 break; 1491 default: 1492 error = EINVAL; 1493 goto out; 1494 } 1495 } else { 1496 switch (csvr->csvr_flags & CSVR_MODE_MASK) { 1497 case CSVR_MODE_SET: 1498 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY; 1499 break; 1500 case CSVR_MODE_REPLACE: 1501 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY; 1502 break; 1503 case CSVR_MODE_CLEAR: 1504 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY; 1505 break; 1506 default: 1507 error = EINVAL; 1508 goto out; 1509 } 1510 } 1511 1512 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid, 1513 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf))); 1514 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn); 1515 1516 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1517 1518 scsi_send_volume_tag(&ccb->csio, 1519 /* retries */ 1, 1520 /* cbfcnp */ chdone, 1521 /* tag_action */ MSG_SIMPLE_Q_TAG, 1522 /* element_address */ ea, 1523 /* send_action_code */ sac, 1524 /* parameters */ &ssvtp, 1525 /* sense_len */ SSD_FULL_SIZE, 1526 /* timeout */ CH_TIMEOUT_SEND_VOLTAG); 1527 1528 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1529 /*sense_flags*/ SF_RETRY_UA, 1530 softc->device_stats); 1531 1532 xpt_release_ccb(ccb); 1533 1534 out: 1535 return error; 1536} 1537 1538static int 1539chgetparams(struct cam_periph *periph) 1540{ 1541 union ccb *ccb; 1542 struct ch_softc *softc; 1543 void *mode_buffer; 1544 int mode_buffer_len; 1545 struct page_element_address_assignment *ea; 1546 struct page_device_capabilities *cap; 1547 int error, from, dbd; 1548 u_int8_t *moves, *exchanges; 1549 1550 error = 0; 1551 1552 softc = (struct ch_softc *)periph->softc; 1553 1554 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1555 1556 /* 1557 * The scsi_mode_sense_data structure is just a convenience 1558 * structure that allows us to easily calculate the worst-case 1559 * storage size of the mode sense buffer. 1560 */ 1561 mode_buffer_len = sizeof(struct scsi_mode_sense_data); 1562 1563 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT); 1564 1565 if (mode_buffer == NULL) { 1566 printf("chgetparams: couldn't malloc mode sense data\n"); 1567 return(ENOSPC); 1568 } 1569 1570 bzero(mode_buffer, mode_buffer_len); 1571 1572 if (softc->quirks & CH_Q_NO_DBD) 1573 dbd = FALSE; 1574 else 1575 dbd = TRUE; 1576 1577 /* 1578 * Get the element address assignment page. 1579 */ 1580 scsi_mode_sense(&ccb->csio, 1581 /* retries */ 1, 1582 /* cbfcnp */ chdone, 1583 /* tag_action */ MSG_SIMPLE_Q_TAG, 1584 /* dbd */ dbd, 1585 /* page_code */ SMS_PAGE_CTRL_CURRENT, 1586 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE, 1587 /* param_buf */ (u_int8_t *)mode_buffer, 1588 /* param_len */ mode_buffer_len, 1589 /* sense_len */ SSD_FULL_SIZE, 1590 /* timeout */ CH_TIMEOUT_MODE_SENSE); 1591 1592 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1593 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT, 1594 softc->device_stats); 1595 1596 if (error) { 1597 if (dbd) { 1598 struct scsi_mode_sense_6 *sms; 1599 1600 sms = (struct scsi_mode_sense_6 *) 1601 ccb->csio.cdb_io.cdb_bytes; 1602 1603 sms->byte2 &= ~SMS_DBD; 1604 error = cam_periph_runccb(ccb, cherror, 1605 /*cam_flags*/ CAM_RETRY_SELTO, 1606 /*sense_flags*/ SF_RETRY_UA, 1607 softc->device_stats); 1608 } else { 1609 /* 1610 * Since we disabled sense printing above, print 1611 * out the sense here since we got an error. 1612 */ 1613 scsi_sense_print(&ccb->csio); 1614 } 1615 1616 if (error) { 1617 xpt_print(periph->path, 1618 "chgetparams: error getting element " 1619 "address page\n"); 1620 xpt_release_ccb(ccb); 1621 free(mode_buffer, M_SCSICH); 1622 return(error); 1623 } 1624 } 1625 1626 ea = (struct page_element_address_assignment *) 1627 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer); 1628 1629 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea); 1630 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte); 1631 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea); 1632 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse); 1633 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea); 1634 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee); 1635 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea); 1636 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte); 1637 1638 bzero(mode_buffer, mode_buffer_len); 1639 1640 /* 1641 * Now get the device capabilities page. 1642 */ 1643 scsi_mode_sense(&ccb->csio, 1644 /* retries */ 1, 1645 /* cbfcnp */ chdone, 1646 /* tag_action */ MSG_SIMPLE_Q_TAG, 1647 /* dbd */ dbd, 1648 /* page_code */ SMS_PAGE_CTRL_CURRENT, 1649 /* page */ CH_DEVICE_CAP_PAGE, 1650 /* param_buf */ (u_int8_t *)mode_buffer, 1651 /* param_len */ mode_buffer_len, 1652 /* sense_len */ SSD_FULL_SIZE, 1653 /* timeout */ CH_TIMEOUT_MODE_SENSE); 1654 1655 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1656 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT, 1657 softc->device_stats); 1658 1659 if (error) { 1660 if (dbd) { 1661 struct scsi_mode_sense_6 *sms; 1662 1663 sms = (struct scsi_mode_sense_6 *) 1664 ccb->csio.cdb_io.cdb_bytes; 1665 1666 sms->byte2 &= ~SMS_DBD; 1667 error = cam_periph_runccb(ccb, cherror, 1668 /*cam_flags*/ CAM_RETRY_SELTO, 1669 /*sense_flags*/ SF_RETRY_UA, 1670 softc->device_stats); 1671 } else { 1672 /* 1673 * Since we disabled sense printing above, print 1674 * out the sense here since we got an error. 1675 */ 1676 scsi_sense_print(&ccb->csio); 1677 } 1678 1679 if (error) { 1680 xpt_print(periph->path, 1681 "chgetparams: error getting device " 1682 "capabilities page\n"); 1683 xpt_release_ccb(ccb); 1684 free(mode_buffer, M_SCSICH); 1685 return(error); 1686 } 1687 } 1688 1689 xpt_release_ccb(ccb); 1690 1691 cap = (struct page_device_capabilities *) 1692 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer); 1693 1694 bzero(softc->sc_movemask, sizeof(softc->sc_movemask)); 1695 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask)); 1696 moves = cap->move_from; 1697 exchanges = cap->exchange_with; 1698 for (from = CHET_MT; from <= CHET_MAX; ++from) { 1699 softc->sc_movemask[from] = moves[from]; 1700 softc->sc_exchangemask[from] = exchanges[from]; 1701 } 1702 1703 free(mode_buffer, M_SCSICH); 1704 1705 return(error); 1706} 1707 1708static int 1709chscsiversion(struct cam_periph *periph) 1710{ 1711 struct scsi_inquiry_data *inq_data; 1712 struct ccb_getdev *cgd; 1713 int dev_scsi_version; 1714 1715 cam_periph_assert(periph, MA_OWNED); 1716 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL) 1717 return (-1); 1718 /* 1719 * Get the device information. 1720 */ 1721 xpt_setup_ccb(&cgd->ccb_h, 1722 periph->path, 1723 CAM_PRIORITY_NORMAL); 1724 cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1725 xpt_action((union ccb *)cgd); 1726 1727 if (cgd->ccb_h.status != CAM_REQ_CMP) { 1728 xpt_free_ccb((union ccb *)cgd); 1729 return -1; 1730 } 1731 1732 inq_data = &cgd->inq_data; 1733 dev_scsi_version = inq_data->version; 1734 xpt_free_ccb((union ccb *)cgd); 1735 1736 return dev_scsi_version; 1737} 1738 1739void 1740scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries, 1741 void (*cbfcnp)(struct cam_periph *, union ccb *), 1742 u_int8_t tag_action, u_int32_t tea, u_int32_t src, 1743 u_int32_t dst, int invert, u_int8_t sense_len, 1744 u_int32_t timeout) 1745{ 1746 struct scsi_move_medium *scsi_cmd; 1747 1748 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes; 1749 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1750 1751 scsi_cmd->opcode = MOVE_MEDIUM; 1752 1753 scsi_ulto2b(tea, scsi_cmd->tea); 1754 scsi_ulto2b(src, scsi_cmd->src); 1755 scsi_ulto2b(dst, scsi_cmd->dst); 1756 1757 if (invert) 1758 scsi_cmd->invert |= MOVE_MEDIUM_INVERT; 1759 1760 cam_fill_csio(csio, 1761 retries, 1762 cbfcnp, 1763 /*flags*/ CAM_DIR_NONE, 1764 tag_action, 1765 /*data_ptr*/ NULL, 1766 /*dxfer_len*/ 0, 1767 sense_len, 1768 sizeof(*scsi_cmd), 1769 timeout); 1770} 1771 1772void 1773scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries, 1774 void (*cbfcnp)(struct cam_periph *, union ccb *), 1775 u_int8_t tag_action, u_int32_t tea, u_int32_t src, 1776 u_int32_t dst1, u_int32_t dst2, int invert1, 1777 int invert2, u_int8_t sense_len, u_int32_t timeout) 1778{ 1779 struct scsi_exchange_medium *scsi_cmd; 1780 1781 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes; 1782 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1783 1784 scsi_cmd->opcode = EXCHANGE_MEDIUM; 1785 1786 scsi_ulto2b(tea, scsi_cmd->tea); 1787 scsi_ulto2b(src, scsi_cmd->src); 1788 scsi_ulto2b(dst1, scsi_cmd->fdst); 1789 scsi_ulto2b(dst2, scsi_cmd->sdst); 1790 1791 if (invert1) 1792 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1; 1793 1794 if (invert2) 1795 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2; 1796 1797 cam_fill_csio(csio, 1798 retries, 1799 cbfcnp, 1800 /*flags*/ CAM_DIR_NONE, 1801 tag_action, 1802 /*data_ptr*/ NULL, 1803 /*dxfer_len*/ 0, 1804 sense_len, 1805 sizeof(*scsi_cmd), 1806 timeout); 1807} 1808 1809void 1810scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries, 1811 void (*cbfcnp)(struct cam_periph *, union ccb *), 1812 u_int8_t tag_action, u_int32_t tea, u_int32_t dst, 1813 int invert, u_int8_t sense_len, u_int32_t timeout) 1814{ 1815 struct scsi_position_to_element *scsi_cmd; 1816 1817 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes; 1818 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1819 1820 scsi_cmd->opcode = POSITION_TO_ELEMENT; 1821 1822 scsi_ulto2b(tea, scsi_cmd->tea); 1823 scsi_ulto2b(dst, scsi_cmd->dst); 1824 1825 if (invert) 1826 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT; 1827 1828 cam_fill_csio(csio, 1829 retries, 1830 cbfcnp, 1831 /*flags*/ CAM_DIR_NONE, 1832 tag_action, 1833 /*data_ptr*/ NULL, 1834 /*dxfer_len*/ 0, 1835 sense_len, 1836 sizeof(*scsi_cmd), 1837 timeout); 1838} 1839 1840void 1841scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries, 1842 void (*cbfcnp)(struct cam_periph *, union ccb *), 1843 u_int8_t tag_action, int voltag, u_int32_t sea, 1844 int curdata, int dvcid, 1845 u_int32_t count, u_int8_t *data_ptr, 1846 u_int32_t dxfer_len, u_int8_t sense_len, 1847 u_int32_t timeout) 1848{ 1849 struct scsi_read_element_status *scsi_cmd; 1850 1851 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes; 1852 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1853 1854 scsi_cmd->opcode = READ_ELEMENT_STATUS; 1855 1856 scsi_ulto2b(sea, scsi_cmd->sea); 1857 scsi_ulto2b(count, scsi_cmd->count); 1858 scsi_ulto3b(dxfer_len, scsi_cmd->len); 1859 if (dvcid) 1860 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID; 1861 if (curdata) 1862 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA; 1863 1864 if (voltag) 1865 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG; 1866 1867 cam_fill_csio(csio, 1868 retries, 1869 cbfcnp, 1870 /*flags*/ CAM_DIR_IN, 1871 tag_action, 1872 data_ptr, 1873 dxfer_len, 1874 sense_len, 1875 sizeof(*scsi_cmd), 1876 timeout); 1877} 1878 1879void 1880scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries, 1881 void (*cbfcnp)(struct cam_periph *, union ccb *), 1882 u_int8_t tag_action, u_int8_t sense_len, 1883 u_int32_t timeout) 1884{ 1885 struct scsi_initialize_element_status *scsi_cmd; 1886 1887 scsi_cmd = (struct scsi_initialize_element_status *) 1888 &csio->cdb_io.cdb_bytes; 1889 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1890 1891 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS; 1892 1893 cam_fill_csio(csio, 1894 retries, 1895 cbfcnp, 1896 /*flags*/ CAM_DIR_NONE, 1897 tag_action, 1898 /* data_ptr */ NULL, 1899 /* dxfer_len */ 0, 1900 sense_len, 1901 sizeof(*scsi_cmd), 1902 timeout); 1903} 1904 1905void 1906scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries, 1907 void (*cbfcnp)(struct cam_periph *, union ccb *), 1908 u_int8_t tag_action, 1909 u_int16_t element_address, 1910 u_int8_t send_action_code, 1911 struct scsi_send_volume_tag_parameters *parameters, 1912 u_int8_t sense_len, u_int32_t timeout) 1913{ 1914 struct scsi_send_volume_tag *scsi_cmd; 1915 1916 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes; 1917 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1918 1919 scsi_cmd->opcode = SEND_VOLUME_TAG; 1920 scsi_ulto2b(element_address, scsi_cmd->ea); 1921 scsi_cmd->sac = send_action_code; 1922 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll); 1923 1924 cam_fill_csio(csio, 1925 retries, 1926 cbfcnp, 1927 /*flags*/ CAM_DIR_OUT, 1928 tag_action, 1929 /* data_ptr */ (u_int8_t *) parameters, 1930 sizeof(*parameters), 1931 sense_len, 1932 sizeof(*scsi_cmd), 1933 timeout); 1934}
| 766 cam_periph_lock(periph); 767 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n")); 768 769 softc = (struct ch_softc *)periph->softc; 770 771 error = 0; 772 773 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 774 ("trying to do ioctl %#lx\n", cmd)); 775 776 /* 777 * If this command can change the device's state, we must 778 * have the device open for writing. 779 */ 780 switch (cmd) { 781 case CHIOGPICKER: 782 case CHIOGPARAMS: 783 case OCHIOGSTATUS: 784 case CHIOGSTATUS: 785 break; 786 787 default: 788 if ((flag & FWRITE) == 0) { 789 cam_periph_unlock(periph); 790 return (EBADF); 791 } 792 } 793 794 switch (cmd) { 795 case CHIOMOVE: 796 error = chmove(periph, (struct changer_move *)addr); 797 break; 798 799 case CHIOEXCHANGE: 800 error = chexchange(periph, (struct changer_exchange *)addr); 801 break; 802 803 case CHIOPOSITION: 804 error = chposition(periph, (struct changer_position *)addr); 805 break; 806 807 case CHIOGPICKER: 808 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT]; 809 break; 810 811 case CHIOSPICKER: 812 { 813 int new_picker = *(int *)addr; 814 815 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) { 816 error = EINVAL; 817 break; 818 } 819 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker; 820 break; 821 } 822 case CHIOGPARAMS: 823 { 824 struct changer_params *cp = (struct changer_params *)addr; 825 826 cp->cp_npickers = softc->sc_counts[CHET_MT]; 827 cp->cp_nslots = softc->sc_counts[CHET_ST]; 828 cp->cp_nportals = softc->sc_counts[CHET_IE]; 829 cp->cp_ndrives = softc->sc_counts[CHET_DT]; 830 break; 831 } 832 case CHIOIELEM: 833 error = chielem(periph, *(unsigned int *)addr); 834 break; 835 836 case OCHIOGSTATUS: 837 { 838 error = chgetelemstatus(periph, SCSI_REV_2, cmd, 839 (struct changer_element_status_request *)addr); 840 break; 841 } 842 843 case CHIOGSTATUS: 844 { 845 int scsi_version; 846 847 scsi_version = chscsiversion(periph); 848 if (scsi_version >= SCSI_REV_0) { 849 error = chgetelemstatus(periph, scsi_version, cmd, 850 (struct changer_element_status_request *)addr); 851 } 852 else { /* unable to determine the SCSI version */ 853 cam_periph_unlock(periph); 854 return (ENXIO); 855 } 856 break; 857 } 858 859 case CHIOSETVOLTAG: 860 { 861 error = chsetvoltag(periph, 862 (struct changer_set_voltag_request *) addr); 863 break; 864 } 865 866 /* Implement prevent/allow? */ 867 868 default: 869 error = cam_periph_ioctl(periph, cmd, addr, cherror); 870 break; 871 } 872 873 cam_periph_unlock(periph); 874 return (error); 875} 876 877static int 878chmove(struct cam_periph *periph, struct changer_move *cm) 879{ 880 struct ch_softc *softc; 881 u_int16_t fromelem, toelem; 882 union ccb *ccb; 883 int error; 884 885 error = 0; 886 softc = (struct ch_softc *)periph->softc; 887 888 /* 889 * Check arguments. 890 */ 891 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT)) 892 return (EINVAL); 893 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) || 894 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1))) 895 return (ENODEV); 896 897 /* 898 * Check the request against the changer's capabilities. 899 */ 900 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0) 901 return (ENODEV); 902 903 /* 904 * Calculate the source and destination elements. 905 */ 906 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit; 907 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit; 908 909 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 910 911 scsi_move_medium(&ccb->csio, 912 /* retries */ 1, 913 /* cbfcnp */ chdone, 914 /* tag_action */ MSG_SIMPLE_Q_TAG, 915 /* tea */ softc->sc_picker, 916 /* src */ fromelem, 917 /* dst */ toelem, 918 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE, 919 /* sense_len */ SSD_FULL_SIZE, 920 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM); 921 922 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO, 923 /*sense_flags*/ SF_RETRY_UA, 924 softc->device_stats); 925 926 xpt_release_ccb(ccb); 927 928 return(error); 929} 930 931static int 932chexchange(struct cam_periph *periph, struct changer_exchange *ce) 933{ 934 struct ch_softc *softc; 935 u_int16_t src, dst1, dst2; 936 union ccb *ccb; 937 int error; 938 939 error = 0; 940 softc = (struct ch_softc *)periph->softc; 941 /* 942 * Check arguments. 943 */ 944 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) || 945 (ce->ce_sdsttype > CHET_DT)) 946 return (EINVAL); 947 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) || 948 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) || 949 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1))) 950 return (ENODEV); 951 952 /* 953 * Check the request against the changer's capabilities. 954 */ 955 if (((softc->sc_exchangemask[ce->ce_srctype] & 956 (1 << ce->ce_fdsttype)) == 0) || 957 ((softc->sc_exchangemask[ce->ce_fdsttype] & 958 (1 << ce->ce_sdsttype)) == 0)) 959 return (ENODEV); 960 961 /* 962 * Calculate the source and destination elements. 963 */ 964 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit; 965 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit; 966 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit; 967 968 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 969 970 scsi_exchange_medium(&ccb->csio, 971 /* retries */ 1, 972 /* cbfcnp */ chdone, 973 /* tag_action */ MSG_SIMPLE_Q_TAG, 974 /* tea */ softc->sc_picker, 975 /* src */ src, 976 /* dst1 */ dst1, 977 /* dst2 */ dst2, 978 /* invert1 */ (ce->ce_flags & CE_INVERT1) ? 979 TRUE : FALSE, 980 /* invert2 */ (ce->ce_flags & CE_INVERT2) ? 981 TRUE : FALSE, 982 /* sense_len */ SSD_FULL_SIZE, 983 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM); 984 985 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO, 986 /*sense_flags*/ SF_RETRY_UA, 987 softc->device_stats); 988 989 xpt_release_ccb(ccb); 990 991 return(error); 992} 993 994static int 995chposition(struct cam_periph *periph, struct changer_position *cp) 996{ 997 struct ch_softc *softc; 998 u_int16_t dst; 999 union ccb *ccb; 1000 int error; 1001 1002 error = 0; 1003 softc = (struct ch_softc *)periph->softc; 1004 1005 /* 1006 * Check arguments. 1007 */ 1008 if (cp->cp_type > CHET_DT) 1009 return (EINVAL); 1010 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1)) 1011 return (ENODEV); 1012 1013 /* 1014 * Calculate the destination element. 1015 */ 1016 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit; 1017 1018 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1019 1020 scsi_position_to_element(&ccb->csio, 1021 /* retries */ 1, 1022 /* cbfcnp */ chdone, 1023 /* tag_action */ MSG_SIMPLE_Q_TAG, 1024 /* tea */ softc->sc_picker, 1025 /* dst */ dst, 1026 /* invert */ (cp->cp_flags & CP_INVERT) ? 1027 TRUE : FALSE, 1028 /* sense_len */ SSD_FULL_SIZE, 1029 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT); 1030 1031 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1032 /*sense_flags*/ SF_RETRY_UA, 1033 softc->device_stats); 1034 1035 xpt_release_ccb(ccb); 1036 1037 return(error); 1038} 1039 1040/* 1041 * Copy a volume tag to a volume_tag struct, converting SCSI byte order 1042 * to host native byte order in the volume serial number. The volume 1043 * label as returned by the changer is transferred to user mode as 1044 * nul-terminated string. Volume labels are truncated at the first 1045 * space, as suggested by SCSI-2. 1046 */ 1047static void 1048copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag) 1049{ 1050 int i; 1051 for (i=0; i<CH_VOLTAG_MAXLEN; i++) { 1052 char c = voltag->vif[i]; 1053 if (c && c != ' ') 1054 uvoltag->cv_volid[i] = c; 1055 else 1056 break; 1057 } 1058 uvoltag->cv_serial = scsi_2btoul(voltag->vsn); 1059} 1060 1061/* 1062 * Copy an element status descriptor to a user-mode 1063 * changer_element_status structure. 1064 */ 1065static void 1066copy_element_status(struct ch_softc *softc, 1067 u_int16_t flags, 1068 struct read_element_status_descriptor *desc, 1069 struct changer_element_status *ces, 1070 int scsi_version) 1071{ 1072 u_int16_t eaddr = scsi_2btoul(desc->eaddr); 1073 u_int16_t et; 1074 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL; 1075 struct read_element_status_device_id *devid = NULL; 1076 1077 ces->ces_int_addr = eaddr; 1078 /* set up logical address in element status */ 1079 for (et = CHET_MT; et <= CHET_DT; et++) { 1080 if ((softc->sc_firsts[et] <= eaddr) 1081 && ((softc->sc_firsts[et] + softc->sc_counts[et]) 1082 > eaddr)) { 1083 ces->ces_addr = eaddr - softc->sc_firsts[et]; 1084 ces->ces_type = et; 1085 break; 1086 } 1087 } 1088 1089 ces->ces_flags = desc->flags1; 1090 1091 ces->ces_sensecode = desc->sense_code; 1092 ces->ces_sensequal = desc->sense_qual; 1093 1094 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT) 1095 ces->ces_flags |= CES_INVERT; 1096 1097 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) { 1098 1099 eaddr = scsi_2btoul(desc->ssea); 1100 1101 /* convert source address to logical format */ 1102 for (et = CHET_MT; et <= CHET_DT; et++) { 1103 if ((softc->sc_firsts[et] <= eaddr) 1104 && ((softc->sc_firsts[et] + softc->sc_counts[et]) 1105 > eaddr)) { 1106 ces->ces_source_addr = 1107 eaddr - softc->sc_firsts[et]; 1108 ces->ces_source_type = et; 1109 ces->ces_flags |= CES_SOURCE_VALID; 1110 break; 1111 } 1112 } 1113 1114 if (!(ces->ces_flags & CES_SOURCE_VALID)) 1115 printf("ch: warning: could not map element source " 1116 "address %ud to a valid element type\n", 1117 eaddr); 1118 } 1119 1120 /* 1121 * pvoltag and avoltag are common between SCSI-2 and later versions 1122 */ 1123 if (flags & READ_ELEMENT_STATUS_PVOLTAG) 1124 pvol_tag = &desc->voltag_devid.pvoltag; 1125 if (flags & READ_ELEMENT_STATUS_AVOLTAG) 1126 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ? 1127 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag; 1128 /* 1129 * For SCSI-3 and later, element status can carry designator and 1130 * other information. 1131 */ 1132 if (scsi_version >= SCSI_REV_SPC) { 1133 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^ 1134 (flags & READ_ELEMENT_STATUS_AVOLTAG)) 1135 devid = &desc->voltag_devid.pvol_and_devid.devid; 1136 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) && 1137 !(flags & READ_ELEMENT_STATUS_AVOLTAG)) 1138 devid = &desc->voltag_devid.devid; 1139 else /* Have both PVOLTAG and AVOLTAG */ 1140 devid = &desc->voltag_devid.vol_tags_and_devid.devid; 1141 } 1142 1143 if (pvol_tag) 1144 copy_voltag(&(ces->ces_pvoltag), pvol_tag); 1145 if (avol_tag) 1146 copy_voltag(&(ces->ces_pvoltag), avol_tag); 1147 if (devid != NULL) { 1148 if (devid->designator_length > 0) { 1149 bcopy((void *)devid->designator, 1150 (void *)ces->ces_designator, 1151 devid->designator_length); 1152 ces->ces_designator_length = devid->designator_length; 1153 /* 1154 * Make sure we are always NUL terminated. The 1155 * This won't matter for the binary code set, 1156 * since the user will only pay attention to the 1157 * length field. 1158 */ 1159 ces->ces_designator[devid->designator_length]= '\0'; 1160 } 1161 if (devid->piv_assoc_designator_type & 1162 READ_ELEMENT_STATUS_PIV_SET) { 1163 ces->ces_flags |= CES_PIV; 1164 ces->ces_protocol_id = 1165 READ_ELEMENT_STATUS_PROTOCOL_ID( 1166 devid->prot_code_set); 1167 } 1168 ces->ces_code_set = 1169 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set); 1170 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION( 1171 devid->piv_assoc_designator_type); 1172 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE( 1173 devid->piv_assoc_designator_type); 1174 } else if (scsi_version > SCSI_REV_2) { 1175 /* SCSI-SPC and No devid, no designator */ 1176 ces->ces_designator_length = 0; 1177 ces->ces_designator[0] = '\0'; 1178 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4; 1179 } 1180 1181 if (scsi_version <= SCSI_REV_2) { 1182 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags & 1183 READ_ELEMENT_STATUS_DT_IDVALID) { 1184 ces->ces_flags |= CES_SCSIID_VALID; 1185 ces->ces_scsi_id = 1186 desc->dt_or_obsolete.scsi_2.dt_scsi_addr; 1187 } 1188 1189 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr & 1190 READ_ELEMENT_STATUS_DT_LUVALID) { 1191 ces->ces_flags |= CES_LUN_VALID; 1192 ces->ces_scsi_lun = 1193 desc->dt_or_obsolete.scsi_2.dt_scsi_flags & 1194 READ_ELEMENT_STATUS_DT_LUNMASK; 1195 } 1196 } 1197} 1198 1199static int 1200chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd, 1201 struct changer_element_status_request *cesr) 1202{ 1203 struct read_element_status_header *st_hdr; 1204 struct read_element_status_page_header *pg_hdr; 1205 struct read_element_status_descriptor *desc; 1206 caddr_t data = NULL; 1207 size_t size, desclen; 1208 int avail, i, error = 0; 1209 int curdata, dvcid, sense_flags; 1210 int try_no_dvcid = 0; 1211 struct changer_element_status *user_data = NULL; 1212 struct ch_softc *softc; 1213 union ccb *ccb; 1214 int chet = cesr->cesr_element_type; 1215 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0; 1216 1217 softc = (struct ch_softc *)periph->softc; 1218 1219 /* perform argument checking */ 1220 1221 /* 1222 * Perform a range check on the cesr_element_{base,count} 1223 * request argument fields. 1224 */ 1225 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0 1226 || (cesr->cesr_element_base + cesr->cesr_element_count) 1227 > softc->sc_counts[chet]) 1228 return (EINVAL); 1229 1230 /* 1231 * Request one descriptor for the given element type. This 1232 * is used to determine the size of the descriptor so that 1233 * we can allocate enough storage for all of them. We assume 1234 * that the first one can fit into 1k. 1235 */ 1236 cam_periph_unlock(periph); 1237 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK); 1238 1239 cam_periph_lock(periph); 1240 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1241 1242 sense_flags = SF_RETRY_UA; 1243 if (softc->quirks & CH_Q_NO_DVCID) { 1244 dvcid = 0; 1245 curdata = 0; 1246 } else { 1247 dvcid = 1; 1248 curdata = 1; 1249 /* 1250 * Don't print anything for an Illegal Request, because 1251 * these flags can cause some changers to complain. We'll 1252 * retry without them if we get an error. 1253 */ 1254 sense_flags |= SF_QUIET_IR; 1255 } 1256 1257retry_einval: 1258 1259 scsi_read_element_status(&ccb->csio, 1260 /* retries */ 1, 1261 /* cbfcnp */ chdone, 1262 /* tag_action */ MSG_SIMPLE_Q_TAG, 1263 /* voltag */ want_voltags, 1264 /* sea */ softc->sc_firsts[chet], 1265 /* curdata */ curdata, 1266 /* dvcid */ dvcid, 1267 /* count */ 1, 1268 /* data_ptr */ data, 1269 /* dxfer_len */ 1024, 1270 /* sense_len */ SSD_FULL_SIZE, 1271 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1272 1273 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1274 /*sense_flags*/ sense_flags, 1275 softc->device_stats); 1276 1277 /* 1278 * An Illegal Request sense key (only used if there is no asc/ascq) 1279 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or 1280 * curdata are set (we set both or neither), try turning them off 1281 * and see if the command is successful. 1282 */ 1283 if ((error == EINVAL) 1284 && (dvcid || curdata)) { 1285 dvcid = 0; 1286 curdata = 0; 1287 error = 0; 1288 /* At this point we want to report any Illegal Request */ 1289 sense_flags &= ~SF_QUIET_IR; 1290 try_no_dvcid = 1; 1291 goto retry_einval; 1292 } 1293 1294 /* 1295 * In this case, we tried a read element status with dvcid and 1296 * curdata set, and it failed. We retried without those bits, and 1297 * it succeeded. Suggest to the user that he set a quirk, so we 1298 * don't go through the retry process the first time in the future. 1299 * This should only happen on changers that claim SCSI-3 or higher, 1300 * but don't support these bits. 1301 */ 1302 if ((try_no_dvcid != 0) 1303 && (error == 0)) 1304 softc->quirks |= CH_Q_NO_DVCID; 1305 1306 if (error) 1307 goto done; 1308 cam_periph_unlock(periph); 1309 1310 st_hdr = (struct read_element_status_header *)data; 1311 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr + 1312 sizeof(struct read_element_status_header)); 1313 desclen = scsi_2btoul(pg_hdr->edl); 1314 1315 size = sizeof(struct read_element_status_header) + 1316 sizeof(struct read_element_status_page_header) + 1317 (desclen * cesr->cesr_element_count); 1318 /* 1319 * Reallocate storage for descriptors and get them from the 1320 * device. 1321 */ 1322 free(data, M_DEVBUF); 1323 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK); 1324 1325 cam_periph_lock(periph); 1326 scsi_read_element_status(&ccb->csio, 1327 /* retries */ 1, 1328 /* cbfcnp */ chdone, 1329 /* tag_action */ MSG_SIMPLE_Q_TAG, 1330 /* voltag */ want_voltags, 1331 /* sea */ softc->sc_firsts[chet] 1332 + cesr->cesr_element_base, 1333 /* curdata */ curdata, 1334 /* dvcid */ dvcid, 1335 /* count */ cesr->cesr_element_count, 1336 /* data_ptr */ data, 1337 /* dxfer_len */ size, 1338 /* sense_len */ SSD_FULL_SIZE, 1339 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1340 1341 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1342 /*sense_flags*/ SF_RETRY_UA, 1343 softc->device_stats); 1344 1345 if (error) 1346 goto done; 1347 cam_periph_unlock(periph); 1348 1349 /* 1350 * Fill in the user status array. 1351 */ 1352 st_hdr = (struct read_element_status_header *)data; 1353 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr + 1354 sizeof(struct read_element_status_header)); 1355 avail = scsi_2btoul(st_hdr->count); 1356 1357 if (avail != cesr->cesr_element_count) { 1358 xpt_print(periph->path, 1359 "warning, READ ELEMENT STATUS avail != count\n"); 1360 } 1361 1362 user_data = (struct changer_element_status *) 1363 malloc(avail * sizeof(struct changer_element_status), 1364 M_DEVBUF, M_WAITOK | M_ZERO); 1365 1366 desc = (struct read_element_status_descriptor *)((uintptr_t)data + 1367 sizeof(struct read_element_status_header) + 1368 sizeof(struct read_element_status_page_header)); 1369 /* 1370 * Set up the individual element status structures 1371 */ 1372 for (i = 0; i < avail; ++i) { 1373 struct changer_element_status *ces; 1374 1375 /* 1376 * In the changer_element_status structure, fields from 1377 * the beginning to the field of ces_scsi_lun are common 1378 * between SCSI-2 and SCSI-3, while all the rest are new 1379 * from SCSI-3. In order to maintain backward compatibility 1380 * of the chio command, the ces pointer, below, is computed 1381 * such that it lines up with the structure boundary 1382 * corresponding to the SCSI version. 1383 */ 1384 ces = cmd == OCHIOGSTATUS ? 1385 (struct changer_element_status *) 1386 ((unsigned char *)user_data + i * 1387 (offsetof(struct changer_element_status,ces_scsi_lun)+1)): 1388 &user_data[i]; 1389 1390 copy_element_status(softc, pg_hdr->flags, desc, 1391 ces, scsi_version); 1392 1393 desc = (struct read_element_status_descriptor *) 1394 ((unsigned char *)desc + desclen); 1395 } 1396 1397 /* Copy element status structures out to userspace. */ 1398 if (cmd == OCHIOGSTATUS) 1399 error = copyout(user_data, 1400 cesr->cesr_element_status, 1401 avail* (offsetof(struct changer_element_status, 1402 ces_scsi_lun) + 1)); 1403 else 1404 error = copyout(user_data, 1405 cesr->cesr_element_status, 1406 avail * sizeof(struct changer_element_status)); 1407 1408 cam_periph_lock(periph); 1409 1410 done: 1411 xpt_release_ccb(ccb); 1412 1413 if (data != NULL) 1414 free(data, M_DEVBUF); 1415 if (user_data != NULL) 1416 free(user_data, M_DEVBUF); 1417 1418 return (error); 1419} 1420 1421static int 1422chielem(struct cam_periph *periph, 1423 unsigned int timeout) 1424{ 1425 union ccb *ccb; 1426 struct ch_softc *softc; 1427 int error; 1428 1429 if (!timeout) { 1430 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS; 1431 } else { 1432 timeout *= 1000; 1433 } 1434 1435 error = 0; 1436 softc = (struct ch_softc *)periph->softc; 1437 1438 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1439 1440 scsi_initialize_element_status(&ccb->csio, 1441 /* retries */ 1, 1442 /* cbfcnp */ chdone, 1443 /* tag_action */ MSG_SIMPLE_Q_TAG, 1444 /* sense_len */ SSD_FULL_SIZE, 1445 /* timeout */ timeout); 1446 1447 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1448 /*sense_flags*/ SF_RETRY_UA, 1449 softc->device_stats); 1450 1451 xpt_release_ccb(ccb); 1452 1453 return(error); 1454} 1455 1456static int 1457chsetvoltag(struct cam_periph *periph, 1458 struct changer_set_voltag_request *csvr) 1459{ 1460 union ccb *ccb; 1461 struct ch_softc *softc; 1462 u_int16_t ea; 1463 u_int8_t sac; 1464 struct scsi_send_volume_tag_parameters ssvtp; 1465 int error; 1466 int i; 1467 1468 error = 0; 1469 softc = (struct ch_softc *)periph->softc; 1470 1471 bzero(&ssvtp, sizeof(ssvtp)); 1472 for (i=0; i<sizeof(ssvtp.vitf); i++) { 1473 ssvtp.vitf[i] = ' '; 1474 } 1475 1476 /* 1477 * Check arguments. 1478 */ 1479 if (csvr->csvr_type > CHET_DT) 1480 return EINVAL; 1481 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1)) 1482 return ENODEV; 1483 1484 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr; 1485 1486 if (csvr->csvr_flags & CSVR_ALTERNATE) { 1487 switch (csvr->csvr_flags & CSVR_MODE_MASK) { 1488 case CSVR_MODE_SET: 1489 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE; 1490 break; 1491 case CSVR_MODE_REPLACE: 1492 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE; 1493 break; 1494 case CSVR_MODE_CLEAR: 1495 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE; 1496 break; 1497 default: 1498 error = EINVAL; 1499 goto out; 1500 } 1501 } else { 1502 switch (csvr->csvr_flags & CSVR_MODE_MASK) { 1503 case CSVR_MODE_SET: 1504 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY; 1505 break; 1506 case CSVR_MODE_REPLACE: 1507 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY; 1508 break; 1509 case CSVR_MODE_CLEAR: 1510 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY; 1511 break; 1512 default: 1513 error = EINVAL; 1514 goto out; 1515 } 1516 } 1517 1518 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid, 1519 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf))); 1520 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn); 1521 1522 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1523 1524 scsi_send_volume_tag(&ccb->csio, 1525 /* retries */ 1, 1526 /* cbfcnp */ chdone, 1527 /* tag_action */ MSG_SIMPLE_Q_TAG, 1528 /* element_address */ ea, 1529 /* send_action_code */ sac, 1530 /* parameters */ &ssvtp, 1531 /* sense_len */ SSD_FULL_SIZE, 1532 /* timeout */ CH_TIMEOUT_SEND_VOLTAG); 1533 1534 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1535 /*sense_flags*/ SF_RETRY_UA, 1536 softc->device_stats); 1537 1538 xpt_release_ccb(ccb); 1539 1540 out: 1541 return error; 1542} 1543 1544static int 1545chgetparams(struct cam_periph *periph) 1546{ 1547 union ccb *ccb; 1548 struct ch_softc *softc; 1549 void *mode_buffer; 1550 int mode_buffer_len; 1551 struct page_element_address_assignment *ea; 1552 struct page_device_capabilities *cap; 1553 int error, from, dbd; 1554 u_int8_t *moves, *exchanges; 1555 1556 error = 0; 1557 1558 softc = (struct ch_softc *)periph->softc; 1559 1560 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1561 1562 /* 1563 * The scsi_mode_sense_data structure is just a convenience 1564 * structure that allows us to easily calculate the worst-case 1565 * storage size of the mode sense buffer. 1566 */ 1567 mode_buffer_len = sizeof(struct scsi_mode_sense_data); 1568 1569 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT); 1570 1571 if (mode_buffer == NULL) { 1572 printf("chgetparams: couldn't malloc mode sense data\n"); 1573 return(ENOSPC); 1574 } 1575 1576 bzero(mode_buffer, mode_buffer_len); 1577 1578 if (softc->quirks & CH_Q_NO_DBD) 1579 dbd = FALSE; 1580 else 1581 dbd = TRUE; 1582 1583 /* 1584 * Get the element address assignment page. 1585 */ 1586 scsi_mode_sense(&ccb->csio, 1587 /* retries */ 1, 1588 /* cbfcnp */ chdone, 1589 /* tag_action */ MSG_SIMPLE_Q_TAG, 1590 /* dbd */ dbd, 1591 /* page_code */ SMS_PAGE_CTRL_CURRENT, 1592 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE, 1593 /* param_buf */ (u_int8_t *)mode_buffer, 1594 /* param_len */ mode_buffer_len, 1595 /* sense_len */ SSD_FULL_SIZE, 1596 /* timeout */ CH_TIMEOUT_MODE_SENSE); 1597 1598 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1599 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT, 1600 softc->device_stats); 1601 1602 if (error) { 1603 if (dbd) { 1604 struct scsi_mode_sense_6 *sms; 1605 1606 sms = (struct scsi_mode_sense_6 *) 1607 ccb->csio.cdb_io.cdb_bytes; 1608 1609 sms->byte2 &= ~SMS_DBD; 1610 error = cam_periph_runccb(ccb, cherror, 1611 /*cam_flags*/ CAM_RETRY_SELTO, 1612 /*sense_flags*/ SF_RETRY_UA, 1613 softc->device_stats); 1614 } else { 1615 /* 1616 * Since we disabled sense printing above, print 1617 * out the sense here since we got an error. 1618 */ 1619 scsi_sense_print(&ccb->csio); 1620 } 1621 1622 if (error) { 1623 xpt_print(periph->path, 1624 "chgetparams: error getting element " 1625 "address page\n"); 1626 xpt_release_ccb(ccb); 1627 free(mode_buffer, M_SCSICH); 1628 return(error); 1629 } 1630 } 1631 1632 ea = (struct page_element_address_assignment *) 1633 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer); 1634 1635 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea); 1636 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte); 1637 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea); 1638 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse); 1639 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea); 1640 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee); 1641 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea); 1642 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte); 1643 1644 bzero(mode_buffer, mode_buffer_len); 1645 1646 /* 1647 * Now get the device capabilities page. 1648 */ 1649 scsi_mode_sense(&ccb->csio, 1650 /* retries */ 1, 1651 /* cbfcnp */ chdone, 1652 /* tag_action */ MSG_SIMPLE_Q_TAG, 1653 /* dbd */ dbd, 1654 /* page_code */ SMS_PAGE_CTRL_CURRENT, 1655 /* page */ CH_DEVICE_CAP_PAGE, 1656 /* param_buf */ (u_int8_t *)mode_buffer, 1657 /* param_len */ mode_buffer_len, 1658 /* sense_len */ SSD_FULL_SIZE, 1659 /* timeout */ CH_TIMEOUT_MODE_SENSE); 1660 1661 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1662 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT, 1663 softc->device_stats); 1664 1665 if (error) { 1666 if (dbd) { 1667 struct scsi_mode_sense_6 *sms; 1668 1669 sms = (struct scsi_mode_sense_6 *) 1670 ccb->csio.cdb_io.cdb_bytes; 1671 1672 sms->byte2 &= ~SMS_DBD; 1673 error = cam_periph_runccb(ccb, cherror, 1674 /*cam_flags*/ CAM_RETRY_SELTO, 1675 /*sense_flags*/ SF_RETRY_UA, 1676 softc->device_stats); 1677 } else { 1678 /* 1679 * Since we disabled sense printing above, print 1680 * out the sense here since we got an error. 1681 */ 1682 scsi_sense_print(&ccb->csio); 1683 } 1684 1685 if (error) { 1686 xpt_print(periph->path, 1687 "chgetparams: error getting device " 1688 "capabilities page\n"); 1689 xpt_release_ccb(ccb); 1690 free(mode_buffer, M_SCSICH); 1691 return(error); 1692 } 1693 } 1694 1695 xpt_release_ccb(ccb); 1696 1697 cap = (struct page_device_capabilities *) 1698 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer); 1699 1700 bzero(softc->sc_movemask, sizeof(softc->sc_movemask)); 1701 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask)); 1702 moves = cap->move_from; 1703 exchanges = cap->exchange_with; 1704 for (from = CHET_MT; from <= CHET_MAX; ++from) { 1705 softc->sc_movemask[from] = moves[from]; 1706 softc->sc_exchangemask[from] = exchanges[from]; 1707 } 1708 1709 free(mode_buffer, M_SCSICH); 1710 1711 return(error); 1712} 1713 1714static int 1715chscsiversion(struct cam_periph *periph) 1716{ 1717 struct scsi_inquiry_data *inq_data; 1718 struct ccb_getdev *cgd; 1719 int dev_scsi_version; 1720 1721 cam_periph_assert(periph, MA_OWNED); 1722 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL) 1723 return (-1); 1724 /* 1725 * Get the device information. 1726 */ 1727 xpt_setup_ccb(&cgd->ccb_h, 1728 periph->path, 1729 CAM_PRIORITY_NORMAL); 1730 cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1731 xpt_action((union ccb *)cgd); 1732 1733 if (cgd->ccb_h.status != CAM_REQ_CMP) { 1734 xpt_free_ccb((union ccb *)cgd); 1735 return -1; 1736 } 1737 1738 inq_data = &cgd->inq_data; 1739 dev_scsi_version = inq_data->version; 1740 xpt_free_ccb((union ccb *)cgd); 1741 1742 return dev_scsi_version; 1743} 1744 1745void 1746scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries, 1747 void (*cbfcnp)(struct cam_periph *, union ccb *), 1748 u_int8_t tag_action, u_int32_t tea, u_int32_t src, 1749 u_int32_t dst, int invert, u_int8_t sense_len, 1750 u_int32_t timeout) 1751{ 1752 struct scsi_move_medium *scsi_cmd; 1753 1754 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes; 1755 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1756 1757 scsi_cmd->opcode = MOVE_MEDIUM; 1758 1759 scsi_ulto2b(tea, scsi_cmd->tea); 1760 scsi_ulto2b(src, scsi_cmd->src); 1761 scsi_ulto2b(dst, scsi_cmd->dst); 1762 1763 if (invert) 1764 scsi_cmd->invert |= MOVE_MEDIUM_INVERT; 1765 1766 cam_fill_csio(csio, 1767 retries, 1768 cbfcnp, 1769 /*flags*/ CAM_DIR_NONE, 1770 tag_action, 1771 /*data_ptr*/ NULL, 1772 /*dxfer_len*/ 0, 1773 sense_len, 1774 sizeof(*scsi_cmd), 1775 timeout); 1776} 1777 1778void 1779scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries, 1780 void (*cbfcnp)(struct cam_periph *, union ccb *), 1781 u_int8_t tag_action, u_int32_t tea, u_int32_t src, 1782 u_int32_t dst1, u_int32_t dst2, int invert1, 1783 int invert2, u_int8_t sense_len, u_int32_t timeout) 1784{ 1785 struct scsi_exchange_medium *scsi_cmd; 1786 1787 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes; 1788 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1789 1790 scsi_cmd->opcode = EXCHANGE_MEDIUM; 1791 1792 scsi_ulto2b(tea, scsi_cmd->tea); 1793 scsi_ulto2b(src, scsi_cmd->src); 1794 scsi_ulto2b(dst1, scsi_cmd->fdst); 1795 scsi_ulto2b(dst2, scsi_cmd->sdst); 1796 1797 if (invert1) 1798 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1; 1799 1800 if (invert2) 1801 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2; 1802 1803 cam_fill_csio(csio, 1804 retries, 1805 cbfcnp, 1806 /*flags*/ CAM_DIR_NONE, 1807 tag_action, 1808 /*data_ptr*/ NULL, 1809 /*dxfer_len*/ 0, 1810 sense_len, 1811 sizeof(*scsi_cmd), 1812 timeout); 1813} 1814 1815void 1816scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries, 1817 void (*cbfcnp)(struct cam_periph *, union ccb *), 1818 u_int8_t tag_action, u_int32_t tea, u_int32_t dst, 1819 int invert, u_int8_t sense_len, u_int32_t timeout) 1820{ 1821 struct scsi_position_to_element *scsi_cmd; 1822 1823 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes; 1824 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1825 1826 scsi_cmd->opcode = POSITION_TO_ELEMENT; 1827 1828 scsi_ulto2b(tea, scsi_cmd->tea); 1829 scsi_ulto2b(dst, scsi_cmd->dst); 1830 1831 if (invert) 1832 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT; 1833 1834 cam_fill_csio(csio, 1835 retries, 1836 cbfcnp, 1837 /*flags*/ CAM_DIR_NONE, 1838 tag_action, 1839 /*data_ptr*/ NULL, 1840 /*dxfer_len*/ 0, 1841 sense_len, 1842 sizeof(*scsi_cmd), 1843 timeout); 1844} 1845 1846void 1847scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries, 1848 void (*cbfcnp)(struct cam_periph *, union ccb *), 1849 u_int8_t tag_action, int voltag, u_int32_t sea, 1850 int curdata, int dvcid, 1851 u_int32_t count, u_int8_t *data_ptr, 1852 u_int32_t dxfer_len, u_int8_t sense_len, 1853 u_int32_t timeout) 1854{ 1855 struct scsi_read_element_status *scsi_cmd; 1856 1857 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes; 1858 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1859 1860 scsi_cmd->opcode = READ_ELEMENT_STATUS; 1861 1862 scsi_ulto2b(sea, scsi_cmd->sea); 1863 scsi_ulto2b(count, scsi_cmd->count); 1864 scsi_ulto3b(dxfer_len, scsi_cmd->len); 1865 if (dvcid) 1866 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID; 1867 if (curdata) 1868 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA; 1869 1870 if (voltag) 1871 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG; 1872 1873 cam_fill_csio(csio, 1874 retries, 1875 cbfcnp, 1876 /*flags*/ CAM_DIR_IN, 1877 tag_action, 1878 data_ptr, 1879 dxfer_len, 1880 sense_len, 1881 sizeof(*scsi_cmd), 1882 timeout); 1883} 1884 1885void 1886scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries, 1887 void (*cbfcnp)(struct cam_periph *, union ccb *), 1888 u_int8_t tag_action, u_int8_t sense_len, 1889 u_int32_t timeout) 1890{ 1891 struct scsi_initialize_element_status *scsi_cmd; 1892 1893 scsi_cmd = (struct scsi_initialize_element_status *) 1894 &csio->cdb_io.cdb_bytes; 1895 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1896 1897 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS; 1898 1899 cam_fill_csio(csio, 1900 retries, 1901 cbfcnp, 1902 /*flags*/ CAM_DIR_NONE, 1903 tag_action, 1904 /* data_ptr */ NULL, 1905 /* dxfer_len */ 0, 1906 sense_len, 1907 sizeof(*scsi_cmd), 1908 timeout); 1909} 1910 1911void 1912scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries, 1913 void (*cbfcnp)(struct cam_periph *, union ccb *), 1914 u_int8_t tag_action, 1915 u_int16_t element_address, 1916 u_int8_t send_action_code, 1917 struct scsi_send_volume_tag_parameters *parameters, 1918 u_int8_t sense_len, u_int32_t timeout) 1919{ 1920 struct scsi_send_volume_tag *scsi_cmd; 1921 1922 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes; 1923 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1924 1925 scsi_cmd->opcode = SEND_VOLUME_TAG; 1926 scsi_ulto2b(element_address, scsi_cmd->ea); 1927 scsi_cmd->sac = send_action_code; 1928 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll); 1929 1930 cam_fill_csio(csio, 1931 retries, 1932 cbfcnp, 1933 /*flags*/ CAM_DIR_OUT, 1934 tag_action, 1935 /* data_ptr */ (u_int8_t *) parameters, 1936 sizeof(*parameters), 1937 sense_len, 1938 sizeof(*scsi_cmd), 1939 timeout); 1940}
|