1 /*
2    Copyright (C) 2015 by Ronnie Sahlberg <sahlberg@gmail.com>
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <CUnit/CUnit.h>
23 
24 #include "iscsi.h"
25 #include "scsi-lowlevel.h"
26 #include "iscsi-test-cu.h"
27 
28 void
test_writeatomic16_dpofua(void)29 test_writeatomic16_dpofua(void)
30 {
31         int gran, dpofua, usage_data_dpofua;
32         struct scsi_task *ms_task = NULL;
33         struct scsi_mode_sense *ms;
34         struct scsi_task *rso_task = NULL;
35         struct scsi_report_supported_op_codes_one_command *rsoc;
36 
37         logging(LOG_VERBOSE, LOG_BLANK_LINE);
38         logging(LOG_VERBOSE, "Test WRITEATOMIC16 DPO/FUA flags");
39 
40         CHECK_FOR_SBC;
41         CHECK_FOR_DATALOSS;
42 
43         if (!inq_bl) {
44                 CU_PASS("BlockLimits VPD is not available. Skipping test.\n");
45                 return;
46         }
47 
48         gran = inq_bl->atomic_gran ? inq_bl->atomic_gran : 1;
49 
50         logging(LOG_VERBOSE, "Read the DPOFUA flag from mode sense data");
51         MODESENSE6(sd, &ms_task, 0, SCSI_MODESENSE_PC_CURRENT,
52                    SCSI_MODEPAGE_RETURN_ALL_PAGES, 0, 255,
53                    EXPECT_STATUS_GOOD);
54 
55         logging(LOG_VERBOSE, "[SUCCESS] Mode sense returned status GOOD");
56         ms = scsi_datain_unmarshall(ms_task);
57         dpofua = ms && (ms->device_specific_parameter & 0x10);
58         scsi_free_scsi_task(ms_task);
59 
60         if (dpofua) {
61                 logging(LOG_VERBOSE, "DPOFUA flag is set. Device should allow "
62                         "DPO/FUA flags in CDBs");
63         } else {
64                 logging(LOG_VERBOSE, "DPOFUA flag is clear. Device should fail "
65                         "CDBs with DPO/FUA set");
66         }
67 
68         logging(LOG_VERBOSE, "Test WRITEATOMIC16 with DPO==1");
69         memset(scratch, 0xa6, block_size);
70         if (dpofua) {
71                 WRITEATOMIC16(sd, 0, gran * block_size,
72                               block_size, 0, 1, 0, 0, scratch,
73                               EXPECT_STATUS_GOOD);
74         } else {
75                 WRITEATOMIC16(sd, 0, gran * block_size,
76                               block_size, 0, 1, 0, 0, scratch,
77                               EXPECT_INVALID_FIELD_IN_CDB);
78         }
79 
80         logging(LOG_VERBOSE, "Test WRITEATOMIC16 with FUA==1");
81         if (dpofua) {
82                 WRITEATOMIC16(sd, 0, gran * block_size,
83                               block_size, 0, 0, 1, 0, scratch,
84                               EXPECT_STATUS_GOOD);
85         } else {
86                 WRITEATOMIC16(sd, 0, gran * block_size,
87                               block_size, 0, 0, 1, 0, scratch,
88                               EXPECT_INVALID_FIELD_IN_CDB);
89         }
90 
91         logging(LOG_VERBOSE, "Test WRITEATOMIC16 with DPO==1 FUA==1");
92         if (dpofua) {
93                 WRITEATOMIC16(sd, 0, gran * block_size,
94                               block_size, 0, 1, 1, 0, scratch,
95                               EXPECT_STATUS_GOOD);
96         } else {
97                 WRITEATOMIC16(sd, 0, gran * block_size,
98                               block_size, 0, 1, 1, 0, scratch,
99                               EXPECT_INVALID_FIELD_IN_CDB);
100         }
101 
102 
103         logging(LOG_VERBOSE, "Try fetching REPORT_SUPPORTED_OPCODES "
104                 "for WRITEATOMIC16");
105         REPORT_SUPPORTED_OPCODES(sd, &rso_task,
106                                  0, SCSI_REPORT_SUPPORTING_OPCODE,
107                                  SCSI_OPCODE_WRITE_ATOMIC16,
108                                  0,
109                                  65535,
110                                  EXPECT_STATUS_GOOD);
111 
112         logging(LOG_VERBOSE, "Unmarshall the DATA-IN buffer");
113         rsoc = scsi_datain_unmarshall(rso_task);
114         CU_ASSERT_PTR_NOT_NULL_FATAL(rsoc);
115 
116         usage_data_dpofua = rsoc->cdb_usage_data[1] & 0x18;
117         if (dpofua) {
118                 logging(LOG_VERBOSE, "DPOFUA is set. Verify the "
119                         "DPO/FUA flags are set in the CDB_USAGE_DATA");
120                 if (!usage_data_dpofua) {
121                         logging(LOG_NORMAL, "[FAILED] DpoFua not set "
122                                 "in CDB_USAGE_DATE");
123                         CU_FAIL("DpoFua not set in CDB_USAGE_DATE");
124                 }
125         } else {
126                 logging(LOG_VERBOSE, "DPOFUA is clear. Verify the "
127                         "DPO/FUA flags are clear in the CDB_USAGE_DATA");
128                 if (usage_data_dpofua) {
129                         logging(LOG_NORMAL, "[FAILED] DpoFua not clear "
130                                 "in CDB_USAGE_DATE");
131                         CU_FAIL("DpoFua not clear in CDB_USAGE_DATE");
132                 }
133         }
134 
135         scsi_free_scsi_task(rso_task);
136 }
137