1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Channel Subsystem tests 4 * 5 * Copyright (c) 2020 IBM Corp 6 * 7 * Authors: 8 * Pierre Morel <pmorel@linux.ibm.com> 9 */ 10 11 #include <libcflat.h> 12 #include <alloc_phys.h> 13 #include <asm/page.h> 14 #include <string.h> 15 #include <interrupt.h> 16 #include <asm/arch_def.h> 17 #include <alloc_page.h> 18 19 #include <malloc_io.h> 20 #include <css.h> 21 #include <asm/barrier.h> 22 23 #define DEFAULT_CU_TYPE 0x3832 /* virtio-ccw */ 24 static unsigned long cu_type = DEFAULT_CU_TYPE; 25 26 static int test_device_sid; 27 static struct senseid *senseid; 28 struct ccw1 *ccw; 29 30 static void test_enumerate(void) 31 { 32 test_device_sid = css_enumerate(); 33 if (test_device_sid & SCHID_ONE) { 34 report_pass("Schid of first I/O device: 0x%08x", test_device_sid); 35 return; 36 } 37 report_fail("No I/O device found"); 38 } 39 40 static void test_enable(void) 41 { 42 int cc; 43 44 if (!test_device_sid) { 45 report_skip("No device"); 46 return; 47 } 48 49 cc = css_enable(test_device_sid, IO_SCH_ISC); 50 51 report(cc == 0, "Enable subchannel %08x", test_device_sid); 52 } 53 54 /* 55 * test_sense 56 * Pre-requisites: 57 * - We need the test device as the first recognized 58 * device by the enumeration. 59 */ 60 static void test_sense(void) 61 { 62 int ret; 63 int len; 64 65 if (!test_device_sid) { 66 report_skip("No device"); 67 return; 68 } 69 70 ret = css_enable(test_device_sid, IO_SCH_ISC); 71 if (ret) { 72 report_fail("Could not enable the subchannel: %08x", 73 test_device_sid); 74 return; 75 } 76 77 lowcore_ptr->io_int_param = 0; 78 79 senseid = alloc_io_mem(sizeof(*senseid), 0); 80 if (!senseid) { 81 report_fail("Allocation of senseid"); 82 return; 83 } 84 85 ccw = ccw_alloc(CCW_CMD_SENSE_ID, senseid, sizeof(*senseid), CCW_F_SLI); 86 if (!ccw) { 87 report_fail("Allocation of CCW"); 88 goto error_ccw; 89 } 90 91 ret = start_ccw1_chain(test_device_sid, ccw); 92 if (ret) { 93 report_fail("Starting CCW chain"); 94 goto error; 95 } 96 97 if (wait_and_check_io_completion(test_device_sid) < 0) 98 goto error; 99 100 /* Test transfer completion */ 101 report_prefix_push("ssch transfer completion"); 102 103 ret = css_residual_count(test_device_sid); 104 105 if (ret < 0) { 106 report_info("no valid residual count"); 107 } else if (ret != 0) { 108 len = sizeof(*senseid) - ret; 109 if (ret && len < CSS_SENSEID_COMMON_LEN) { 110 report_fail("transferred a too short length: %d", ret); 111 goto error; 112 } else if (ret && len) 113 report_info("transferred a shorter length: %d", len); 114 } 115 116 if (senseid->reserved != 0xff) { 117 report_fail("transferred garbage: 0x%02x", senseid->reserved); 118 goto error; 119 } 120 121 report_prefix_pop(); 122 123 report_info("reserved 0x%02x cu_type 0x%04x cu_model 0x%02x dev_type 0x%04x dev_model 0x%02x", 124 senseid->reserved, senseid->cu_type, senseid->cu_model, 125 senseid->dev_type, senseid->dev_model); 126 127 report(senseid->cu_type == cu_type, "cu_type expected 0x%04x got 0x%04x", 128 (uint16_t)cu_type, senseid->cu_type); 129 130 error: 131 free_io_mem(ccw, sizeof(*ccw)); 132 error_ccw: 133 free_io_mem(senseid, sizeof(*senseid)); 134 } 135 136 static void sense_id(void) 137 { 138 assert(!start_ccw1_chain(test_device_sid, ccw)); 139 140 assert(wait_and_check_io_completion(test_device_sid) >= 0); 141 } 142 143 static void css_init(void) 144 { 145 assert(register_io_int_func(css_irq_io) == 0); 146 lowcore_ptr->io_int_param = 0; 147 148 report(get_chsc_scsc(), "Store Channel Characteristics"); 149 } 150 151 static void test_schm(void) 152 { 153 if (css_test_general_feature(CSSC_EXTENDED_MEASUREMENT_BLOCK)) 154 report_info("Extended measurement block available"); 155 156 /* bits 59-63 of MB address must be 0 if MBU is defined */ 157 report_prefix_push("Unaligned operand"); 158 expect_pgm_int(); 159 schm((void *)0x01, SCHM_MBU); 160 check_pgm_int_code(PGM_INT_CODE_OPERAND); 161 report_prefix_pop(); 162 163 /* bits 36-61 of register 1 (flags) must be 0 */ 164 report_prefix_push("Bad flags"); 165 expect_pgm_int(); 166 schm(NULL, 0xfffffffc); 167 check_pgm_int_code(PGM_INT_CODE_OPERAND); 168 report_prefix_pop(); 169 170 /* SCHM is a privilege operation */ 171 report_prefix_push("Privilege"); 172 enter_pstate(); 173 expect_pgm_int(); 174 schm(NULL, SCHM_MBU); 175 check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION); 176 report_prefix_pop(); 177 178 /* Normal operation */ 179 report_prefix_push("Normal operation"); 180 schm(NULL, SCHM_MBU); 181 report_pass("SCHM call without address"); 182 report_prefix_pop(); 183 } 184 185 #define SCHM_UPDATE_CNT 10 186 static bool start_measuring(uint64_t mbo, uint16_t mbi, bool fmt1) 187 { 188 int i; 189 190 senseid = alloc_io_mem(sizeof(*senseid), 0); 191 assert(senseid); 192 193 ccw = ccw_alloc(CCW_CMD_SENSE_ID, senseid, sizeof(*senseid), CCW_F_SLI); 194 assert(ccw); 195 196 if (!css_enable_mb(test_device_sid, mbo, mbi, PMCW_MBUE, fmt1)) { 197 report_abort("Enabling measurement block failed"); 198 return false; 199 } 200 201 for (i = 0; i < SCHM_UPDATE_CNT; i++) 202 sense_id(); 203 204 free_io_mem(ccw, sizeof(*ccw)); 205 free_io_mem(senseid, sizeof(*senseid)); 206 207 return true; 208 } 209 210 /* 211 * test_schm_fmt0: 212 * With measurement block format 0 a memory space is shared 213 * by all subchannels, each subchannel can provide an index 214 * for the measurement block facility to store the measurements. 215 */ 216 static void test_schm_fmt0(void) 217 { 218 struct measurement_block_format0 *mb0; 219 int shared_mb_size = 2 * sizeof(struct measurement_block_format0); 220 221 if (!test_device_sid) { 222 report_skip("No device"); 223 return; 224 } 225 226 /* Allocate zeroed Measurement block */ 227 mb0 = alloc_io_mem(shared_mb_size, 0); 228 if (!mb0) { 229 report_abort("measurement_block_format0 allocation failed"); 230 return; 231 } 232 233 schm(NULL, 0); /* Stop any previous measurement */ 234 schm(mb0, SCHM_MBU); 235 236 /* Expect success */ 237 report_prefix_push("Valid MB address and index 0"); 238 report(start_measuring(0, 0, false) && 239 mb0->ssch_rsch_count == SCHM_UPDATE_CNT, 240 "SSCH measured %d", mb0->ssch_rsch_count); 241 report_prefix_pop(); 242 243 /* Clear the measurement block for the next test */ 244 memset(mb0, 0, shared_mb_size); 245 246 /* Expect success */ 247 report_prefix_push("Valid MB address and index 1"); 248 if (start_measuring(0, 1, false)) 249 report(mb0[1].ssch_rsch_count == SCHM_UPDATE_CNT, 250 "SSCH measured %d", mb0[1].ssch_rsch_count); 251 report_prefix_pop(); 252 253 /* Stop the measurement */ 254 css_disable_mb(test_device_sid); 255 schm(NULL, 0); 256 257 free_io_mem(mb0, shared_mb_size); 258 } 259 260 static void msch_with_wrong_fmt1_mbo(unsigned int schid, uint64_t mb) 261 { 262 struct pmcw *pmcw = &schib.pmcw; 263 int cc; 264 265 /* Read the SCHIB for this subchannel */ 266 cc = stsch(schid, &schib); 267 if (cc) { 268 report_fail("stsch: sch %08x failed with cc=%d", schid, cc); 269 return; 270 } 271 272 /* Update the SCHIB to enable the measurement block */ 273 pmcw->flags |= PMCW_MBUE; 274 pmcw->flags2 |= PMCW_MBF1; 275 schib.mbo = mb; 276 277 /* Tell the CSS we want to modify the subchannel */ 278 expect_pgm_int(); 279 cc = msch(schid, &schib); 280 check_pgm_int_code(PGM_INT_CODE_OPERAND); 281 } 282 283 /* 284 * test_schm_fmt1: 285 * With measurement block format 1 the measurement block is 286 * dedicated to a subchannel. 287 */ 288 static void test_schm_fmt1(void) 289 { 290 struct measurement_block_format1 *mb1; 291 292 if (!test_device_sid) { 293 report_skip("No device"); 294 return; 295 } 296 297 if (!css_test_general_feature(CSSC_EXTENDED_MEASUREMENT_BLOCK)) { 298 report_skip("Extended measurement block not available"); 299 return; 300 } 301 302 /* Allocate zeroed Measurement block */ 303 mb1 = alloc_io_mem(sizeof(struct measurement_block_format1), 0); 304 if (!mb1) { 305 report_abort("measurement_block_format1 allocation failed"); 306 return; 307 } 308 309 schm(NULL, 0); /* Stop any previous measurement */ 310 schm(0, SCHM_MBU); 311 312 /* Expect error for non aligned MB */ 313 report_prefix_push("Unaligned MB origin"); 314 msch_with_wrong_fmt1_mbo(test_device_sid, (uint64_t)mb1 + 1); 315 report_prefix_pop(); 316 317 /* Clear the measurement block for the next test */ 318 memset(mb1, 0, sizeof(*mb1)); 319 320 /* Expect success */ 321 report_prefix_push("Valid MB origin"); 322 if (start_measuring((u64)mb1, 0, true)) 323 report(mb1->ssch_rsch_count == SCHM_UPDATE_CNT, 324 "SSCH measured %d", mb1->ssch_rsch_count); 325 report_prefix_pop(); 326 327 /* Stop the measurement */ 328 css_disable_mb(test_device_sid); 329 schm(NULL, 0); 330 331 free_io_mem(mb1, sizeof(struct measurement_block_format1)); 332 } 333 334 static struct { 335 const char *name; 336 void (*func)(void); 337 } tests[] = { 338 /* The css_init test is needed to initialize the CSS Characteristics */ 339 { "initialize CSS (chsc)", css_init }, 340 { "enumerate (stsch)", test_enumerate }, 341 { "enable (msch)", test_enable }, 342 { "sense (ssch/tsch)", test_sense }, 343 { "measurement block (schm)", test_schm }, 344 { "measurement block format0", test_schm_fmt0 }, 345 { "measurement block format1", test_schm_fmt1 }, 346 { NULL, NULL } 347 }; 348 349 int main(int argc, char *argv[]) 350 { 351 int i; 352 353 report_prefix_push("Channel Subsystem"); 354 enable_io_isc(0x80 >> IO_SCH_ISC); 355 for (i = 0; tests[i].name; i++) { 356 report_prefix_push(tests[i].name); 357 tests[i].func(); 358 report_prefix_pop(); 359 } 360 report_prefix_pop(); 361 362 return report_summary(); 363 } 364