1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
|
/*
* Copyright (c) 1999-2023 Douglas Gilbert.
* All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the BSD_LICENSE file.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
/*
* CONTENTS
* Some SCSI commands are executed in many contexts and hence began
* to appear in several sg3_utils utilities. This files centralizes
* some of the low level command execution code. In most cases the
* interpretation of the command response is left to the each
* utility.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "sg_lib.h"
#include "sg_cmds_basic.h"
#include "sg_pt.h"
#include "sg_unaligned.h"
#include "sg_pr2serr.h"
#define SENSE_BUFF_LEN 64 /* Arbitrary, could be larger */
#define EBUFF_SZ 256
#define DEF_PT_TIMEOUT 60 /* 60 seconds */
#define START_PT_TIMEOUT 120 /* 120 seconds == 2 minutes */
#define LONG_PT_TIMEOUT 7200 /* 7,200 seconds == 120 minutes */
#define SYNCHRONIZE_CACHE_CMD 0x35
#define SYNCHRONIZE_CACHE_CMDLEN 10
#define SERVICE_ACTION_IN_16_CMD 0x9e
#define SERVICE_ACTION_IN_16_CMDLEN 16
#define READ_CAPACITY_16_SA 0x10
#define READ_CAPACITY_10_CMD 0x25
#define READ_CAPACITY_10_CMDLEN 10
#define MODE_SENSE6_CMD 0x1a
#define MODE_SENSE6_CMDLEN 6
#define MODE_SENSE10_CMD 0x5a
#define MODE_SENSE10_CMDLEN 10
#define MODE_SELECT6_CMD 0x15
#define MODE_SELECT6_CMDLEN 6
#define MODE_SELECT10_CMD 0x55
#define MODE_SELECT10_CMDLEN 10
#define LOG_SENSE_CMD 0x4d
#define LOG_SENSE_CMDLEN 10
#define LOG_SELECT_CMD 0x4c
#define LOG_SELECT_CMDLEN 10
#define START_STOP_CMD 0x1b
#define START_STOP_CMDLEN 6
#define PREVENT_ALLOW_CMD 0x1e
#define PREVENT_ALLOW_CMDLEN 6
#define MODE6_RESP_HDR_LEN 4
#define MODE10_RESP_HDR_LEN 8
#define MODE_RESP_ARB_LEN 8192
#define INQUIRY_RESP_INITIAL_LEN 36
static struct sg_pt_base *
create_pt_obj(const char * cname)
{
struct sg_pt_base * ptvp = construct_scsi_pt_obj();
if (NULL == ptvp)
pr2ws("%s: out of memory\n", cname);
return ptvp;
}
/* Invokes a SCSI SYNCHRONIZE CACHE (10) command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors */
int
sg_ll_sync_cache_10(int sg_fd, bool sync_nv, bool immed, int group,
unsigned int lba, unsigned int count, bool noisy,
int verbose)
{
static const char * const cdb_s = "synchronize cache(10)";
int res, ret, sense_cat;
uint8_t sc_cdb[SYNCHRONIZE_CACHE_CMDLEN] =
{SYNCHRONIZE_CACHE_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
if (sync_nv)
sc_cdb[1] |= 4;
if (immed)
sc_cdb[1] |= 2;
sg_put_unaligned_be32((uint32_t)lba, sc_cdb + 2);
sc_cdb[6] = group & GRPNUM_MASK;
if (count > 0xffff) {
pr2ws("count too big\n");
return -1;
}
sg_put_unaligned_be16((int16_t)count, sc_cdb + 7);
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(sc_cdb, SYNCHRONIZE_CACHE_CMDLEN, false,
sizeof(b), b));
}
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
return -1;
set_scsi_pt_cdb(ptvp, sc_cdb, sizeof(sc_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else
ret = 0;
destruct_scsi_pt_obj(ptvp);
return ret;
}
/* Invokes a SCSI READ CAPACITY (16) command. Returns 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors */
int
sg_ll_readcap_16(int sg_fd, bool pmi, uint64_t llba, void * resp,
int mx_resp_len, bool noisy, int verbose)
{
static const char * const cdb_s = "read capacity(16)";
int ret, res, sense_cat;
uint8_t rc_cdb[SERVICE_ACTION_IN_16_CMDLEN] =
{SERVICE_ACTION_IN_16_CMD, READ_CAPACITY_16_SA,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
if (pmi) { /* lbs only valid when pmi set */
rc_cdb[14] |= 1;
sg_put_unaligned_be64(llba, rc_cdb + 2);
}
/* Allocation length, no guidance in SBC-2 rev 15b */
sg_put_unaligned_be32((uint32_t)mx_resp_len, rc_cdb + 10);
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(rc_cdb, SERVICE_ACTION_IN_16_CMDLEN, false,
sizeof(b), b));
}
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
return -1;
set_scsi_pt_cdb(ptvp, rc_cdb, sizeof(rc_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
set_scsi_pt_data_in(ptvp, (uint8_t *)resp, mx_resp_len);
res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else
ret = 0;
destruct_scsi_pt_obj(ptvp);
return ret;
}
/* Invokes a SCSI READ CAPACITY (10) command. Returns 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors */
int
sg_ll_readcap_10(int sg_fd, bool pmi, unsigned int lba, void * resp,
int mx_resp_len, bool noisy, int verbose)
{
static const char * const cdb_s = "read capacity(10)";
int ret, res, sense_cat;
uint8_t rc_cdb[READ_CAPACITY_10_CMDLEN] =
{READ_CAPACITY_10_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
if (pmi) { /* lbs only valid when pmi set */
rc_cdb[8] |= 1;
sg_put_unaligned_be32((uint32_t)lba, rc_cdb + 2);
}
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(rc_cdb, READ_CAPACITY_10_CMDLEN, false,
sizeof(b), b));
}
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
return -1;
set_scsi_pt_cdb(ptvp, rc_cdb, sizeof(rc_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
set_scsi_pt_data_in(ptvp, (uint8_t *)resp, mx_resp_len);
res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else
ret = 0;
destruct_scsi_pt_obj(ptvp);
return ret;
}
/* Invokes a SCSI MODE SENSE (6) command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors */
int
sg_ll_mode_sense6(int sg_fd, bool dbd, int pc, int pg_code, int sub_pg_code,
void * resp, int mx_resp_len, bool noisy, int verbose)
{
static const char * const cdb_s = "mode sense(6)";
int res, ret, sense_cat, resid;
uint8_t modes_cdb[MODE_SENSE6_CMDLEN] =
{MODE_SENSE6_CMD, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
modes_cdb[1] = (uint8_t)(dbd ? 0x8 : 0);
modes_cdb[2] = (uint8_t)(((pc << 6) & 0xc0) | (pg_code & 0x3f));
modes_cdb[3] = (uint8_t)(sub_pg_code & 0xff);
modes_cdb[4] = (uint8_t)(mx_resp_len & 0xff);
if (mx_resp_len > 0xff) {
pr2ws("mx_resp_len too big\n");
return -1;
}
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(modes_cdb, MODE_SENSE6_CMDLEN, false,
sizeof(b), b));
}
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
return -1;
set_scsi_pt_cdb(ptvp, modes_cdb, sizeof(modes_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
set_scsi_pt_data_in(ptvp, (uint8_t *)resp, mx_resp_len);
res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
resid = get_scsi_pt_resid(ptvp);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else {
if ((verbose > 2) && (ret > 0)) {
pr2ws(" %s: response", cdb_s);
if (3 == verbose) {
pr2ws("%s:\n", (ret > 256 ? ", first 256 bytes" : ""));
hex2stderr((const uint8_t *)resp, (ret > 256 ? 256 : ret),
-1);
} else {
pr2ws(":\n");
hex2stderr((const uint8_t *)resp, ret, 0);
}
}
ret = 0;
}
destruct_scsi_pt_obj(ptvp);
if (resid > 0) {
if (resid > mx_resp_len) {
pr2ws("%s: resid (%d) should never exceed requested len=%d\n",
cdb_s, resid, mx_resp_len);
return ret ? ret : SG_LIB_CAT_MALFORMED;
}
/* zero unfilled section of response buffer */
memset((uint8_t *)resp + (mx_resp_len - resid), 0, resid);
}
return ret;
}
/* Invokes a SCSI MODE SENSE (10) command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors */
int
sg_ll_mode_sense10(int sg_fd, bool llbaa, bool dbd, int pc, int pg_code,
int sub_pg_code, void * resp, int mx_resp_len,
bool noisy, int verbose)
{
return sg_ll_mode_sense10_v2(sg_fd, llbaa, dbd, pc, pg_code, sub_pg_code,
resp, mx_resp_len, 0, NULL, noisy, verbose);
}
/* Invokes a SCSI MODE SENSE (10) command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors.
* Adds the ability to set the command abort timeout
* and the ability to report the residual count. If timeout_secs is zero
* or less the default command abort timeout (60 seconds) is used.
* If residp is non-NULL then the residual value is written where residp
* points. A residual value of 0 implies mx_resp_len bytes have be written
* where resp points. If the residual value equals mx_resp_len then no
* bytes have been written. */
int
sg_ll_mode_sense10_v2(int sg_fd, bool llbaa, bool dbd, int pc, int pg_code,
int sub_pg_code, void * resp, int mx_resp_len,
int timeout_secs, int * residp, bool noisy, int verbose)
{
int res, ret, sense_cat, resid;
static const char * const cdb_s = "mode sense(10)";
struct sg_pt_base * ptvp;
uint8_t modes_cdb[MODE_SENSE10_CMDLEN] =
{MODE_SENSE10_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
modes_cdb[1] = (uint8_t)((dbd ? 0x8 : 0) | (llbaa ? 0x10 : 0));
modes_cdb[2] = (uint8_t)(((pc << 6) & 0xc0) | (pg_code & 0x3f));
modes_cdb[3] = (uint8_t)(sub_pg_code & 0xff);
sg_put_unaligned_be16((int16_t)mx_resp_len, modes_cdb + 7);
if (mx_resp_len > 0xffff) {
pr2ws("mx_resp_len too big\n");
goto gen_err;
}
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(modes_cdb, MODE_SENSE10_CMDLEN, false,
sizeof(b), b));
}
if (timeout_secs <= 0)
timeout_secs = DEF_PT_TIMEOUT;
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
goto gen_err;
set_scsi_pt_cdb(ptvp, modes_cdb, sizeof(modes_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
set_scsi_pt_data_in(ptvp, (uint8_t *)resp, mx_resp_len);
res = do_scsi_pt(ptvp, sg_fd, timeout_secs, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
resid = get_scsi_pt_resid(ptvp);
if (residp)
*residp = resid;
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else {
if ((verbose > 2) && (ret > 0)) {
pr2ws(" %s: response", cdb_s);
if (3 == verbose) {
pr2ws("%s:\n", (ret > 256 ? ", first 256 bytes" : ""));
hex2stderr((const uint8_t *)resp, (ret > 256 ? 256 : ret),
-1);
} else {
pr2ws(":\n");
hex2stderr((const uint8_t *)resp, ret, 0);
}
}
ret = 0;
}
destruct_scsi_pt_obj(ptvp);
if (resid > 0) {
if (resid > mx_resp_len) {
pr2ws("%s: resid (%d) should never exceed requested len=%d\n",
cdb_s, resid, mx_resp_len);
return ret ? ret : SG_LIB_CAT_MALFORMED;
}
/* zero unfilled section of response buffer */
memset((uint8_t *)resp + (mx_resp_len - resid), 0, resid);
}
return ret;
gen_err:
if (residp)
*residp = 0;
return -1;
}
/* Invokes a SCSI MODE SELECT (6) command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors */
int
sg_ll_mode_select6_v2(int sg_fd, bool pf, bool rtd, bool sp, void * paramp,
int param_len, bool noisy, int verbose)
{
static const char * const cdb_s = "mode select(6)";
int res, ret, sense_cat;
uint8_t modes_cdb[MODE_SELECT6_CMDLEN] =
{MODE_SELECT6_CMD, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
modes_cdb[1] = (uint8_t)((pf ? 0x10 : 0x0) | (sp ? 0x1 : 0x0));
if (rtd)
modes_cdb[1] |= 0x2;
modes_cdb[4] = (uint8_t)(param_len & 0xff);
if (param_len > 0xff) {
pr2ws("%s: param_len too big\n", cdb_s);
return -1;
}
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(modes_cdb, MODE_SELECT6_CMDLEN, false,
sizeof(b), b));
}
if (verbose > 1) {
pr2ws(" %s parameter list\n", cdb_s);
hex2stderr((const uint8_t *)paramp, param_len, -1);
}
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
return -1;
set_scsi_pt_cdb(ptvp, modes_cdb, sizeof(modes_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
set_scsi_pt_data_out(ptvp, (uint8_t *)paramp, param_len);
res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else
ret = 0;
destruct_scsi_pt_obj(ptvp);
return ret;
}
int
sg_ll_mode_select6(int sg_fd, bool pf, bool sp, void * paramp, int param_len,
bool noisy, int verbose)
{
return sg_ll_mode_select6_v2(sg_fd, pf, false, sp, paramp, param_len,
noisy, verbose);
}
/* Invokes a SCSI MODE SELECT (10) command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors,
* v2 adds rtd (revert to defaults) bit (spc5r11). */
int
sg_ll_mode_select10_v2(int sg_fd, bool pf, bool rtd, bool sp, void * paramp,
int param_len, bool noisy, int verbose)
{
static const char * const cdb_s = "mode select(10)";
int res, ret, sense_cat;
uint8_t modes_cdb[MODE_SELECT10_CMDLEN] =
{MODE_SELECT10_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
modes_cdb[1] = (uint8_t)((pf ? 0x10 : 0x0) | (sp ? 0x1 : 0x0));
if (rtd)
modes_cdb[1] |= 0x2;
sg_put_unaligned_be16((int16_t)param_len, modes_cdb + 7);
if (param_len > 0xffff) {
pr2ws("%s: param_len too big\n", cdb_s);
return -1;
}
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(modes_cdb, MODE_SELECT10_CMDLEN, false,
sizeof(b), b));
}
if (verbose > 1) {
pr2ws(" %s parameter list\n", cdb_s);
hex2stderr((const uint8_t *)paramp, param_len, -1);
}
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
return -1;
set_scsi_pt_cdb(ptvp, modes_cdb, sizeof(modes_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
set_scsi_pt_data_out(ptvp, (uint8_t *)paramp, param_len);
res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else
ret = 0;
destruct_scsi_pt_obj(ptvp);
return ret;
}
int
sg_ll_mode_select10(int sg_fd, bool pf, bool sp, void * paramp,
int param_len, bool noisy, int verbose)
{
return sg_ll_mode_select10_v2(sg_fd, pf, false, sp, paramp, param_len,
noisy, verbose);
}
/* MODE SENSE commands yield a response that has header then zero or more
* block descriptors followed by mode pages. In most cases users are
* interested in the first mode page. This function returns the (byte)
* offset of the start of the first mode page. Set mode_sense_6 to true for
* MODE SENSE (6) and false for MODE SENSE (10). Returns >= 0 is successful
* or -1 if failure. If there is a failure a message is written to err_buff
* if it is non-NULL and err_buff_len > 0. */
int
sg_mode_page_offset(const uint8_t * resp, int resp_len,
bool mode_sense_6, char * err_buff, int err_buff_len)
{
int bd_len, calc_len, offset;
bool err_buff_ok = ((err_buff_len > 0) && err_buff);
if ((NULL == resp) || (resp_len < 4))
goto too_short;
if (mode_sense_6) {
calc_len = resp[0] + 1;
bd_len = resp[3];
offset = bd_len + MODE6_RESP_HDR_LEN;
} else { /* Mode sense(10) */
if (resp_len < 8)
goto too_short;
calc_len = sg_get_unaligned_be16(resp) + 2;
bd_len = sg_get_unaligned_be16(resp + 6);
/* LongLBA doesn't change this calculation */
offset = bd_len + MODE10_RESP_HDR_LEN;
}
if ((offset + 2) > calc_len) {
if (err_buff_ok)
snprintf(err_buff, err_buff_len, "calculated response "
"length too small, offset=%d calc_len=%d bd_len=%d\n",
offset, calc_len, bd_len);
offset = -1;
}
return offset;
too_short:
if (err_buff_ok)
snprintf(err_buff, err_buff_len, "given MS(%d) response length (%d) "
"too short\n", (mode_sense_6 ? 6 : 10), resp_len);
return -1;
}
/* MODE SENSE commands yield a response that has header then zero or more
* block descriptors followed by mode pages. This functions returns the
* length (in bytes) of those three components. Note that the return value
* can exceed resp_len in which case the MODE SENSE command should be
* re-issued with a larger response buffer. If bd_lenp is non-NULL and if
* successful the block descriptor length (in bytes) is written to *bd_lenp.
* Set mode_sense_6 to true for MODE SENSE (6) and false for MODE SENSE (10)
* responses. Returns -1 if there is an error (e.g. response too short). */
int
sg_msense_calc_length(const uint8_t * resp, int resp_len,
bool mode_sense_6, int * bd_lenp)
{
int calc_len;
if (NULL == resp)
goto an_err;
if (mode_sense_6) {
if (resp_len < 4)
goto an_err;
calc_len = resp[0] + 1;
} else {
if (resp_len < 8)
goto an_err;
calc_len = sg_get_unaligned_be16(resp + 0) + 2;
}
if (bd_lenp)
*bd_lenp = mode_sense_6 ? resp[3] : sg_get_unaligned_be16(resp + 6);
return calc_len;
an_err:
if (bd_lenp)
*bd_lenp = 0;
return -1;
}
/* Fetches current, changeable, default and/or saveable mode pages (note:
* _only_ mode pages) as indicated by pcontrol_arr for given pg_code and
* sub_pg_code. If mode6 is true then use MODE SENSE (6) else use MODE SENSE
* (10). If flexible true and mode data length seems wrong then try and fix
* (compensating hack for bad device or driver). pcontrol_arr should have 4
* elements for output of current, changeable, default and saved values
* respectively. Each element should be NULL or a pointer to memory that is
* at least mx_mpage_len bytes long. The length of the fetched mode page
* (or pages) is written to the reported_lenp pointer, if it is non-NULL.
* That should be the length written to each pointer within pcontrol_arr .
* If success_mask pointer is not NULL then first zeros it. Then set bits
* 0, 1, 2 and/or 3 if the current, changeable, default and saved values
* respectively have been fetched. Returns 0 if overall success, otherwise
* a SG_LIB_CAT type error is returned or -1 for an uncategorized error.
* If error on current page then stops and returns that error; otherwise
* continues if an error is detected, returning the first error encountered.
* So if the saved page control is not supported, for example, then 7 is
* written to *smask and SG_LIB_CAT_ILLEGAL_REQ is returned. */
int
sg_get_mode_page_controls(int sg_fd, bool mode6, int pg_code, int sub_pg_code,
bool dbd, bool flexible, int mx_mpage_len,
int * success_mask, void * pcontrol_arr[],
int * reported_lenp, int verbose)
{
bool resp_mode6;
int k, n, res, offset, calc_len, xfer_len;
int resid = 0;
const int msense10_hlen = MODE10_RESP_HDR_LEN;
uint8_t * buffp;
uint8_t * free_buffp;
char ebuff[EBUFF_SZ];
int first_err = 0;
if (success_mask)
*success_mask = 0;
if (reported_lenp)
*reported_lenp = 0;
if (mx_mpage_len < 4)
return 0;
buffp = sg_memalign(MODE_RESP_ARB_LEN, 0, &free_buffp, false);
if (NULL == buffp)
return sg_convert_errno(ENOMEM);
memset(ebuff, 0, sizeof(ebuff));
/* first try to find length of current page response */
memset(buffp, 0, msense10_hlen);
if (mode6) /* want first 8 bytes just in case */
res = sg_ll_mode_sense6(sg_fd, dbd, 0 /* pc */, pg_code,
sub_pg_code, buffp, msense10_hlen, true,
verbose);
else /* MODE SENSE(10) obviously */
res = sg_ll_mode_sense10_v2(sg_fd, false /* llbaa */, dbd,
0 /* pc */, pg_code, sub_pg_code, buffp,
msense10_hlen, 0, NULL, true, verbose);
if (0 != res) {
first_err = res;
goto fini;
}
n = buffp[0];
if (reported_lenp) {
int bd_len, non_mps;
int m = sg_msense_calc_length(buffp, msense10_hlen, mode6, &bd_len);
non_mps = bd_len + (mode6 ? 4 : 8);
*reported_lenp = (m > non_mps) ? m - non_mps : 0;
}
resp_mode6 = mode6;
if (flexible) {
if (mode6 && (n < 3)) {
resp_mode6 = false;
if (verbose)
pr2ws(">>> msense(6) but resp[0]=%d so try msense(10) "
"response processing\n", n);
}
if ((! mode6) && (n > 5)) {
if ((n > 11) && (0 == (n % 2)) && (0 == buffp[4]) &&
(0 == buffp[5]) && (0 == buffp[6])) {
buffp[1] = n;
buffp[0] = 0;
if (verbose)
pr2ws(">>> msense(10) but resp[0]=%d and not msense(6) "
"response so fix length\n", n);
} else
resp_mode6 = true;
}
}
if (verbose && (resp_mode6 != mode6))
pr2ws(">>> msense(%d) but resp[0]=%d so switch response "
"processing\n", (mode6 ? 6 : 10), buffp[0]);
calc_len = sg_msense_calc_length(buffp, msense10_hlen, resp_mode6, NULL);
if (calc_len > MODE_RESP_ARB_LEN)
calc_len = MODE_RESP_ARB_LEN;
offset = sg_mode_page_offset(buffp, calc_len, resp_mode6, ebuff, EBUFF_SZ);
if (offset < 0) {
if (('\0' != ebuff[0]) && (verbose > 0))
pr2ws("%s: %s\n", __func__, ebuff);
first_err = SG_LIB_CAT_MALFORMED;
goto fini;
}
xfer_len = calc_len - offset;
if (xfer_len > mx_mpage_len)
xfer_len = mx_mpage_len;
for (k = 0; k < 4; ++k) {
if (NULL == pcontrol_arr[k])
continue;
memset(pcontrol_arr[k], 0, mx_mpage_len);
resid = 0;
if (mode6)
res = sg_ll_mode_sense6(sg_fd, dbd, k /* pc */,
pg_code, sub_pg_code, buffp,
calc_len, true, verbose);
else
res = sg_ll_mode_sense10_v2(sg_fd, false /* llbaa */, dbd,
k /* pc */, pg_code, sub_pg_code,
buffp, calc_len, 0, &resid, true,
verbose);
if (res || resid) {
if (0 == first_err) {
if (res)
first_err = res;
else {
first_err = -49; /* unexpected resid != 0 */
if (verbose)
pr2ws("%s: unexpected resid=%d, page=0x%x, "
"pcontrol=%d\n", __func__, resid, pg_code, k);
}
}
if (0 == k)
break; /* if problem on current page, it won't improve */
else
continue;
}
if (xfer_len > 0)
memcpy(pcontrol_arr[k], buffp + offset, xfer_len);
if (success_mask)
*success_mask |= (1 << k);
}
fini:
if (free_buffp)
free(free_buffp);
return first_err;
}
/* Invokes a SCSI LOG SENSE command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors. */
int
sg_ll_log_sense(int sg_fd, bool ppc, bool sp, int pc, int pg_code,
int subpg_code, int paramp, uint8_t * resp,
int mx_resp_len, bool noisy, int verbose)
{
return sg_ll_log_sense_v2(sg_fd, ppc, sp, pc, pg_code, subpg_code,
paramp, resp, mx_resp_len, 0, NULL, noisy,
verbose);
}
/* Invokes a SCSI LOG SENSE command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors.
* Adds the ability to set the command abort timeout
* and the ability to report the residual count. If timeout_secs is zero
* or less the default command abort timeout (60 seconds) is used.
* If residp is non-NULL then the residual value is written where residp
* points. A residual value of 0 implies mx_resp_len bytes have be written
* where resp points. If the residual value equals mx_resp_len then no
* bytes have been written. */
int
sg_ll_log_sense_v2(int sg_fd, bool ppc, bool sp, int pc, int pg_code,
int subpg_code, int paramp, uint8_t * resp,
int mx_resp_len, int timeout_secs, int * residp,
bool noisy, int verbose)
{
static const char * const cdb_s = "log sense";
int res, ret, sense_cat, resid;
uint8_t logs_cdb[LOG_SENSE_CMDLEN] =
{LOG_SENSE_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
if (mx_resp_len > 0xffff) {
pr2ws("mx_resp_len too big\n");
goto gen_err;
}
logs_cdb[1] = (uint8_t)((ppc ? 2 : 0) | (sp ? 1 : 0));
logs_cdb[2] = (uint8_t)(((pc << 6) & 0xc0) | (pg_code & 0x3f));
logs_cdb[3] = (uint8_t)(subpg_code & 0xff);
sg_put_unaligned_be16((int16_t)paramp, logs_cdb + 5);
sg_put_unaligned_be16((int16_t)mx_resp_len, logs_cdb + 7);
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(logs_cdb, LOG_SENSE_CMDLEN, false,
sizeof(b), b));
}
if (timeout_secs <= 0)
timeout_secs = DEF_PT_TIMEOUT;
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
goto gen_err;
set_scsi_pt_cdb(ptvp, logs_cdb, sizeof(logs_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
set_scsi_pt_data_in(ptvp, resp, mx_resp_len);
res = do_scsi_pt(ptvp, sg_fd, timeout_secs, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
resid = get_scsi_pt_resid(ptvp);
if (residp)
*residp = resid;
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else {
if ((mx_resp_len > 3) && (ret < 4)) {
/* resid indicates LOG SENSE response length bad, so zero it */
resp[2] = 0;
resp[3] = 0;
}
ret = 0;
}
destruct_scsi_pt_obj(ptvp);
if (resid > 0) {
if (resid > mx_resp_len) {
pr2ws("%s: resid (%d) should never exceed requested len=%d\n",
cdb_s, resid, mx_resp_len);
return ret ? ret : SG_LIB_CAT_MALFORMED;
}
/* zero unfilled section of response buffer */
memset((uint8_t *)resp + (mx_resp_len - resid), 0, resid);
}
return ret;
gen_err:
if (residp)
*residp = 0;
return -1;
}
/* Invokes a SCSI LOG SELECT command. Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors */
int
sg_ll_log_select(int sg_fd, bool pcr, bool sp, int pc, int pg_code,
int subpg_code, uint8_t * paramp, int param_len,
bool noisy, int verbose)
{
static const char * const cdb_s = "log select";
int res, ret, sense_cat;
uint8_t logs_cdb[LOG_SELECT_CMDLEN] =
{LOG_SELECT_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
if (param_len > 0xffff) {
pr2ws("%s: param_len too big\n", cdb_s);
return -1;
}
logs_cdb[1] = (uint8_t)((pcr ? 2 : 0) | (sp ? 1 : 0));
logs_cdb[2] = (uint8_t)(((pc << 6) & 0xc0) | (pg_code & 0x3f));
logs_cdb[3] = (uint8_t)(subpg_code & 0xff);
sg_put_unaligned_be16((int16_t)param_len, logs_cdb + 7);
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(logs_cdb, LOG_SELECT_CMDLEN, false,
sizeof(b), b));
}
if ((verbose > 1) && (param_len > 0)) {
pr2ws(" %s parameter list\n", cdb_s);
hex2stderr(paramp, param_len, -1);
}
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
return -1;
set_scsi_pt_cdb(ptvp, logs_cdb, sizeof(logs_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
set_scsi_pt_data_out(ptvp, paramp, param_len);
res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else
ret = 0;
destruct_scsi_pt_obj(ptvp);
return ret;
}
/* Invokes a SCSI START STOP UNIT command (SBC + MMC).
* Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors.
* SBC-3 and MMC partially overlap on the power_condition_modifier(sbc) and
* format_layer_number(mmc) fields. They also overlap on the noflush(sbc)
* and fl(mmc) one bit field. This is the cause of the awkardly named
* pc_mod__fl_num and noflush__fl arguments to this function.
* */
static int
sg_ll_start_stop_unit_com(struct sg_pt_base * ptvp, int sg_fd, bool immed,
int pc_mod__fl_num, int power_cond, bool noflush__fl,
bool loej, bool start, bool noisy, int verbose)
{
static const char * const cdb_s = "start stop unit";
bool ptvp_given = false;
bool local_sense = true;
bool local_cdb = true;
int res, ret, sense_cat;
uint8_t ssuBlk[START_STOP_CMDLEN] = {START_STOP_CMD, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
if (immed)
ssuBlk[1] = 0x1;
ssuBlk[3] = pc_mod__fl_num & 0xf; /* bits 2 and 3 are reserved in MMC */
ssuBlk[4] = ((power_cond & 0xf) << 4);
if (noflush__fl)
ssuBlk[4] |= 0x4;
if (loej)
ssuBlk[4] |= 0x2;
if (start)
ssuBlk[4] |= 0x1;
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(ssuBlk, sizeof(ssuBlk), false,
sizeof(b), b));
}
if (ptvp) {
ptvp_given = true;
partial_clear_scsi_pt_obj(ptvp);
if (get_scsi_pt_cdb_buf(ptvp))
local_cdb = false; /* N.B. Ignores locally built cdb */
else
set_scsi_pt_cdb(ptvp, ssuBlk, sizeof(ssuBlk));
if (get_scsi_pt_sense_buf(ptvp))
local_sense = false;
else
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
} else {
ptvp = construct_scsi_pt_obj_with_fd(sg_fd, verbose);
if (NULL == ptvp)
return sg_convert_errno(ENOMEM);
set_scsi_pt_cdb(ptvp, ssuBlk, sizeof(ssuBlk));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
}
res = do_scsi_pt(ptvp, -1, START_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else
ret = 0;
if (ptvp_given) {
if (local_sense) /* stop caller trying to access local sense */
set_scsi_pt_sense(ptvp, NULL, 0);
if (local_cdb)
set_scsi_pt_cdb(ptvp, NULL, 0);
} else {
if (ptvp)
destruct_scsi_pt_obj(ptvp);
}
return ret;
}
int
sg_ll_start_stop_unit(int sg_fd, bool immed, int pc_mod__fl_num,
int power_cond, bool noflush__fl, bool loej, bool start,
bool noisy, int verbose)
{
return sg_ll_start_stop_unit_com(NULL, sg_fd, immed, pc_mod__fl_num,
power_cond, noflush__fl, loej, start,
noisy, verbose);
}
int
sg_ll_start_stop_unit_pt(struct sg_pt_base * ptvp, bool immed,
int pc_mod__fl_num, int power_cond, bool noflush__fl,
bool loej, bool start, bool noisy, int verbose)
{
return sg_ll_start_stop_unit_com(ptvp, -1, immed, pc_mod__fl_num,
power_cond, noflush__fl, loej, start,
noisy, verbose);
}
/* Invokes a SCSI PREVENT ALLOW MEDIUM REMOVAL command
* [was in SPC-3 but displaced from SPC-4 into SBC-3, MMC-5, SSC-3]
* prevent==0 allows removal, prevent==1 prevents removal ...
* Return of 0 -> success,
* various SG_LIB_CAT_* positive values or -1 -> other errors */
int
sg_ll_prevent_allow(int sg_fd, int prevent, bool noisy, int verbose)
{
static const char * const cdb_s = "prevent allow medium removal";
int res, ret, sense_cat;
uint8_t p_cdb[PREVENT_ALLOW_CMDLEN] =
{PREVENT_ALLOW_CMD, 0, 0, 0, 0, 0};
uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
struct sg_pt_base * ptvp;
if ((prevent < 0) || (prevent > 3)) {
pr2ws("prevent argument should be 0, 1, 2 or 3\n");
return -1;
}
p_cdb[4] |= (prevent & 0x3);
if (verbose) {
char b[128];
pr2ws(" %s cdb: %s\n", cdb_s,
sg_get_command_str(p_cdb, PREVENT_ALLOW_CMDLEN, false,
sizeof(b), b));
}
if (NULL == ((ptvp = create_pt_obj(cdb_s))))
return -1;
set_scsi_pt_cdb(ptvp, p_cdb, sizeof(p_cdb));
set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
ret = sg_cmds_process_resp(ptvp, cdb_s, res, noisy, verbose, &sense_cat);
if (-1 == ret) {
if (get_scsi_pt_transport_err(ptvp))
ret = SG_LIB_TRANSPORT_ERROR;
else
ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
} else if (-2 == ret) {
switch (sense_cat) {
case SG_LIB_CAT_RECOVERED:
case SG_LIB_CAT_NO_SENSE:
ret = 0;
break;
default:
ret = sense_cat;
break;
}
} else
ret = 0;
destruct_scsi_pt_obj(ptvp);
return ret;
}
|