xref: /kvm-unit-tests/s390x/css.c (revision c604fa931a1cb70c3649ac1b7223178fc79eab6a)
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 #include <hardware.h>
19 
20 #include <malloc_io.h>
21 #include <css.h>
22 #include <asm/barrier.h>
23 
24 #define DEFAULT_CU_TYPE		0x3832 /* virtio-ccw */
25 static unsigned long cu_type = DEFAULT_CU_TYPE;
26 
27 static int test_device_sid;
28 static struct senseid *senseid;
29 struct ccw1 *ccw;
30 
31 char alignment_test_page[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
32 
33 static void test_enumerate(void)
34 {
35 	test_device_sid = css_enumerate();
36 	if (test_device_sid & SCHID_ONE) {
37 		report_pass("Schid of first I/O device: 0x%08x", test_device_sid);
38 		return;
39 	}
40 	report_fail("No I/O device found");
41 }
42 
43 static void test_enable(void)
44 {
45 	int cc;
46 
47 	if (!test_device_sid) {
48 		report_skip("No device");
49 		return;
50 	}
51 
52 	cc = css_enable(test_device_sid, IO_SCH_ISC);
53 
54 	report(cc == 0, "Enable subchannel %08x", test_device_sid);
55 }
56 
57 /*
58  * test_sense
59  * Pre-requisites:
60  * - We need the test device as the first recognized
61  *   device by the enumeration.
62  */
63 static void test_sense(void)
64 {
65 	int ret;
66 	int len;
67 
68 	if (!test_device_sid) {
69 		report_skip("No device");
70 		return;
71 	}
72 
73 	ret = css_enable(test_device_sid, IO_SCH_ISC);
74 	if (ret) {
75 		report_fail("Could not enable the subchannel: %08x",
76 			    test_device_sid);
77 		return;
78 	}
79 
80 	lowcore_ptr->io_int_param = 0;
81 
82 	senseid = alloc_io_mem(sizeof(*senseid), 0);
83 	if (!senseid) {
84 		report_fail("Allocation of senseid");
85 		return;
86 	}
87 
88 	ccw = ccw_alloc(CCW_CMD_SENSE_ID, senseid, sizeof(*senseid), CCW_F_SLI);
89 	if (!ccw) {
90 		report_fail("Allocation of CCW");
91 		goto error_ccw;
92 	}
93 
94 	ret = start_ccw1_chain(test_device_sid, ccw);
95 	if (ret) {
96 		report_fail("Starting CCW chain");
97 		goto error;
98 	}
99 
100 	if (wait_and_check_io_completion(test_device_sid) < 0)
101 		goto error;
102 
103 	/* Test transfer completion */
104 	report_prefix_push("ssch transfer completion");
105 
106 	ret = css_residual_count(test_device_sid);
107 
108 	if (ret < 0) {
109 		report_info("no valid residual count");
110 	} else if (ret != 0) {
111 		len = sizeof(*senseid) - ret;
112 		if (ret && len < CSS_SENSEID_COMMON_LEN) {
113 			report_fail("transferred a too short length: %d", ret);
114 			goto error;
115 		} else if (ret && len)
116 			report_info("transferred a shorter length: %d", len);
117 	}
118 
119 	if (senseid->reserved != 0xff) {
120 		report_fail("transferred garbage: 0x%02x", senseid->reserved);
121 		goto error;
122 	}
123 
124 	report_prefix_pop();
125 
126 	report_info("reserved 0x%02x cu_type 0x%04x cu_model 0x%02x dev_type 0x%04x dev_model 0x%02x",
127 		    senseid->reserved, senseid->cu_type, senseid->cu_model,
128 		    senseid->dev_type, senseid->dev_model);
129 
130 	report(senseid->cu_type == cu_type, "cu_type expected 0x%04x got 0x%04x",
131 	       (uint16_t)cu_type, senseid->cu_type);
132 
133 error:
134 	free_io_mem(ccw, sizeof(*ccw));
135 error_ccw:
136 	free_io_mem(senseid, sizeof(*senseid));
137 }
138 
139 static void sense_id(void)
140 {
141 	assert(!start_ccw1_chain(test_device_sid, ccw));
142 
143 	assert(wait_and_check_io_completion(test_device_sid) >= 0);
144 }
145 
146 static void css_init(void)
147 {
148 	assert(register_io_int_func(css_irq_io) == 0);
149 	lowcore_ptr->io_int_param = 0;
150 
151 	report(get_chsc_scsc(), "Store Channel Characteristics");
152 }
153 
154 static void test_schm(void)
155 {
156 	if (css_test_general_feature(CSSC_EXTENDED_MEASUREMENT_BLOCK))
157 		report_info("Extended measurement block available");
158 
159 	/* bits 59-63 of MB address must be 0  if MBU is defined */
160 	report_prefix_push("Unaligned operand");
161 	expect_pgm_int();
162 	schm((void *)0x01, SCHM_MBU);
163 	check_pgm_int_code(PGM_INT_CODE_OPERAND);
164 	report_prefix_pop();
165 
166 	/* bits 36-61 of register 1 (flags) must be 0 */
167 	report_prefix_push("Bad flags");
168 	expect_pgm_int();
169 	schm(NULL, 0xfffffffc);
170 	check_pgm_int_code(PGM_INT_CODE_OPERAND);
171 	report_prefix_pop();
172 
173 	/* SCHM is a privilege operation */
174 	report_prefix_push("Privilege");
175 	enter_pstate();
176 	expect_pgm_int();
177 	schm(NULL, SCHM_MBU);
178 	check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION);
179 	report_prefix_pop();
180 
181 	/* Normal operation */
182 	report_prefix_push("Normal operation");
183 	schm(NULL, SCHM_MBU);
184 	report_pass("SCHM call without address");
185 	report_prefix_pop();
186 }
187 
188 #define SCHM_UPDATE_CNT 10
189 static bool start_measuring(uint64_t mbo, uint16_t mbi, bool fmt1)
190 {
191 	int i;
192 
193 	senseid = alloc_io_mem(sizeof(*senseid), 0);
194 	assert(senseid);
195 
196 	ccw = ccw_alloc(CCW_CMD_SENSE_ID, senseid, sizeof(*senseid), CCW_F_SLI);
197 	assert(ccw);
198 
199 	if (!css_enable_mb(test_device_sid, mbo, mbi, PMCW_MBUE, fmt1)) {
200 		report_abort("Enabling measurement block failed");
201 		return false;
202 	}
203 
204 	for (i = 0; i < SCHM_UPDATE_CNT; i++)
205 		sense_id();
206 
207 	free_io_mem(ccw, sizeof(*ccw));
208 	free_io_mem(senseid, sizeof(*senseid));
209 
210 	return true;
211 }
212 
213 /*
214  * test_schm_fmt0:
215  * With measurement block format 0 a memory space is shared
216  * by all subchannels, each subchannel can provide an index
217  * for the measurement block facility to store the measurements.
218  */
219 static void test_schm_fmt0(void)
220 {
221 	struct measurement_block_format0 *mb0;
222 	int shared_mb_size = 2 * sizeof(struct measurement_block_format0);
223 
224 	if (!test_device_sid) {
225 		report_skip("No device");
226 		return;
227 	}
228 
229 	/* Allocate zeroed Measurement block */
230 	mb0 = alloc_io_mem(shared_mb_size, 0);
231 	if (!mb0) {
232 		report_abort("measurement_block_format0 allocation failed");
233 		return;
234 	}
235 
236 	schm(NULL, 0); /* Stop any previous measurement */
237 	schm(mb0, SCHM_MBU);
238 
239 	/* Expect success */
240 	report_prefix_push("Valid MB address and index 0");
241 	report(start_measuring(0, 0, false) &&
242 	       mb0->ssch_rsch_count == SCHM_UPDATE_CNT,
243 	       "SSCH measured %d", mb0->ssch_rsch_count);
244 	report_prefix_pop();
245 
246 	/* Clear the measurement block for the next test */
247 	memset(mb0, 0, shared_mb_size);
248 
249 	/* Expect success */
250 	report_prefix_push("Valid MB address and index 1");
251 	if (start_measuring(0, 1, false))
252 		report(mb0[1].ssch_rsch_count == SCHM_UPDATE_CNT,
253 		       "SSCH measured %d", mb0[1].ssch_rsch_count);
254 	report_prefix_pop();
255 
256 	/* Stop the measurement */
257 	css_disable_mb(test_device_sid);
258 	schm(NULL, 0);
259 
260 	free_io_mem(mb0, shared_mb_size);
261 }
262 
263 static void msch_with_wrong_fmt1_mbo(unsigned int schid, uint64_t mb)
264 {
265 	struct pmcw *pmcw = &schib.pmcw;
266 	int cc;
267 
268 	/* Read the SCHIB for this subchannel */
269 	cc = stsch(schid, &schib);
270 	if (cc) {
271 		report_fail("stsch: sch %08x failed with cc=%d", schid, cc);
272 		return;
273 	}
274 
275 	/* Update the SCHIB to enable the measurement block */
276 	pmcw->flags |= PMCW_MBUE;
277 	pmcw->flags2 |= PMCW_MBF1;
278 	schib.mbo = mb;
279 
280 	/* Tell the CSS we want to modify the subchannel */
281 	expect_pgm_int();
282 	cc = msch(schid, &schib);
283 	check_pgm_int_code(PGM_INT_CODE_OPERAND);
284 }
285 
286 /*
287  * test_schm_fmt1:
288  * With measurement block format 1 the measurement block is
289  * dedicated to a subchannel.
290  */
291 static void test_schm_fmt1(void)
292 {
293 	struct measurement_block_format1 *mb1;
294 
295 	if (!test_device_sid) {
296 		report_skip("No device");
297 		return;
298 	}
299 
300 	if (!css_test_general_feature(CSSC_EXTENDED_MEASUREMENT_BLOCK)) {
301 		report_skip("Extended measurement block not available");
302 		return;
303 	}
304 
305 	/* Allocate zeroed Measurement block */
306 	mb1 = alloc_io_mem(sizeof(struct measurement_block_format1), 0);
307 	if (!mb1) {
308 		report_abort("measurement_block_format1 allocation failed");
309 		return;
310 	}
311 
312 	schm(NULL, 0); /* Stop any previous measurement */
313 	schm(0, SCHM_MBU);
314 
315 	/* Expect error for non aligned MB */
316 	report_prefix_push("Unaligned MB origin");
317 	msch_with_wrong_fmt1_mbo(test_device_sid, (uint64_t)mb1 + 1);
318 	report_prefix_pop();
319 
320 	/* Clear the measurement block for the next test */
321 	memset(mb1, 0, sizeof(*mb1));
322 
323 	/* Expect success */
324 	report_prefix_push("Valid MB origin");
325 	if (start_measuring((u64)mb1, 0, true))
326 		report(mb1->ssch_rsch_count == SCHM_UPDATE_CNT,
327 		       "SSCH measured %d", mb1->ssch_rsch_count);
328 	report_prefix_pop();
329 
330 	/* Stop the measurement */
331 	css_disable_mb(test_device_sid);
332 	schm(NULL, 0);
333 
334 	free_io_mem(mb1, sizeof(struct measurement_block_format1));
335 }
336 
337 static void test_msch(void)
338 {
339 	int invalid_pmcw_flags[] = {0, 1, 6, 7};
340 	struct schib test_schib;
341 	uint16_t old_pmcw_flags;
342 	const int align_to = 4;
343 	int invalid_flag;
344 	int cc;
345 
346 	if (!test_device_sid) {
347 		report_skip("No device");
348 		return;
349 	}
350 
351 	cc = stsch(test_device_sid, &schib);
352 	if (cc) {
353 		report_fail("stsch: sch %08x failed with cc=%d", test_device_sid, cc);
354 		return;
355 	}
356 
357 	report_prefix_push("Unaligned");
358 	for (int i = 1; i < align_to; i *= 2) {
359 		report_prefix_pushf("%d", i);
360 
361 		expect_pgm_int();
362 		msch(test_device_sid, (struct schib *)(alignment_test_page + i));
363 		check_pgm_int_code(PGM_INT_CODE_SPECIFICATION);
364 
365 		report_prefix_pop();
366 	}
367 	report_prefix_pop();
368 
369 	report_prefix_push("Invalid SCHIB");
370 	old_pmcw_flags = schib.pmcw.flags;
371 	for (int i = 0; i < ARRAY_SIZE(invalid_pmcw_flags); i++) {
372 		invalid_flag = invalid_pmcw_flags[i];
373 
374 		report_prefix_pushf("PMCW flag bit %d set", invalid_flag);
375 
376 		schib.pmcw.flags = old_pmcw_flags | BIT(15 - invalid_flag);
377 		expect_pgm_int();
378 		msch(test_device_sid, &schib);
379 		check_pgm_int_code(PGM_INT_CODE_OPERAND);
380 
381 		cc = stsch(test_device_sid, &test_schib);
382 		report(!cc, "STSCH succeeded");
383 		report(!(test_schib.pmcw.flags & BIT(15 - invalid_flag)), "Clear on STSCH");
384 
385 		report_prefix_pop();
386 	}
387 	report_prefix_pop();
388 
389 	schib.pmcw.flags = old_pmcw_flags;
390 }
391 
392 static void check_stcrw_no_crw_available(void)
393 {
394 	uint32_t crw = 0xfeedc0fe;
395 	int cc;
396 
397 	report_prefix_push("No CRW available");
398 	cc = stcrw(&crw);
399 	report(cc == 1, "cc == 1");
400 	report(!crw, "stored zeroes in crw");
401 	report_prefix_pop();
402 }
403 
404 static int check_stcrw_crw_available(void)
405 {
406 	const uint32_t magic = 0xfeedc0fe;
407 	uint32_t crw = magic;
408 	int cc;
409 
410 	report_prefix_push("CRW available");
411 	cc = stcrw(&crw);
412 	report(!cc, "cc is zero");
413 	report(crw != magic, "stored crw");
414 	report_prefix_pop();
415 
416 	return crw;
417 }
418 
419 static uint32_t crw_get_rsc(uint32_t crw)
420 {
421 	const int rsc_begin = 4;
422 	const int rsc_end = 8;
423 
424 	return (crw & GENMASK(31 - rsc_begin, 31 - rsc_end)) >> 24;
425 }
426 
427 #define CRW_RSC_CHP 4
428 static void test_stcrw(void)
429 {
430 	const int align_to = 4;
431 	int res;
432 	uint32_t crw;
433 
434 	if (!test_device_sid) {
435 		report_skip("No device");
436 		return;
437 	}
438 
439 	report_prefix_push("Unaligned");
440 	for (int i = 1; i < align_to; i *= 2) {
441 		report_prefix_pushf("%d", i);
442 
443 		expect_pgm_int();
444 		stcrw((uint32_t *)(alignment_test_page + i));
445 		check_pgm_int_code(PGM_INT_CODE_SPECIFICATION);
446 
447 		report_prefix_pop();
448 	}
449 	report_prefix_pop();
450 
451 	report_prefix_push("No CRW available initally");
452 	check_stcrw_no_crw_available();
453 	report_prefix_pop();
454 
455 	res = css_generate_crw(test_device_sid);
456 	if (res) {
457 		report_skip("Couldn't generate CRW");
458 		report_prefix_pop();
459 		return;
460 	}
461 
462 	crw = check_stcrw_crw_available();
463 
464 	report_prefix_push("CRW available");
465 	report(crw_get_rsc(crw) == CRW_RSC_CHP, "CRW has Channel Path RSC");
466 	report_prefix_pop();
467 
468 	report_prefix_push("No more CRWs pending");
469 	check_stcrw_no_crw_available();
470 	report_prefix_pop();
471 }
472 
473 static void test_ssch(void)
474 {
475 	const int align_to = 4;
476 	struct orb orb;
477 
478 	if (!test_device_sid) {
479 		report_skip("No device");
480 		return;
481 	}
482 
483 	report_prefix_push("Unaligned");
484 	for (int i = 1; i < align_to; i *= 2) {
485 		report_prefix_pushf("%d", i);
486 
487 		expect_pgm_int();
488 		ssch(test_device_sid, (struct orb *)(alignment_test_page + i));
489 		check_pgm_int_code(PGM_INT_CODE_SPECIFICATION);
490 
491 		report_prefix_pop();
492 	}
493 	report_prefix_pop();
494 
495 	report_prefix_push("Invalid ORB");
496 
497 	memset(&orb, 0xff, sizeof(orb));
498 	expect_pgm_int();
499 	ssch(test_device_sid, &orb);
500 	check_pgm_int_code(PGM_INT_CODE_OPERAND);
501 
502 	report_prefix_pop();
503 }
504 
505 static void test_stsch(void)
506 {
507 	const int align_to = 4;
508 	struct schib schib;
509 	int cc;
510 
511 	if (!test_device_sid) {
512 		report_skip("No device");
513 		return;
514 	}
515 
516 	report_prefix_push("Unaligned");
517 	for (int i = 1; i < align_to; i *= 2) {
518 		report_prefix_pushf("%d", i);
519 
520 		expect_pgm_int();
521 		stsch(test_device_sid, (struct schib *)(alignment_test_page + i));
522 		check_pgm_int_code(PGM_INT_CODE_SPECIFICATION);
523 
524 		report_prefix_pop();
525 	}
526 	report_prefix_pop();
527 
528 	report_prefix_push("Invalid subchannel number");
529 	cc = stsch(0x0001ffff, &schib);
530 	report(cc == 3, "Channel not operational");
531 	report_prefix_pop();
532 
533 	/*
534 	 * No matter if multiple-subchannel-set facility is installed, bit 47
535 	 * always needs to be 1.
536 	 */
537 	report_prefix_push("Bit 47 in SID is zero");
538 	expect_pgm_int();
539 	stsch(0x0000ffff, &schib);
540 	check_pgm_int_code(PGM_INT_CODE_OPERAND);
541 	report_prefix_pop();
542 }
543 
544 /*
545  * According to architecture MSCH does ignore bit 5 of the second word
546  * but STSCH will store bit 5 as zero.
547  */
548 static void test_pmcw_bit5(void)
549 {
550 	int cc;
551 	uint16_t old_pmcw_flags;
552 
553 	cc = stsch(test_device_sid, &schib);
554 	if (cc) {
555 		report_fail("stsch: sch %08x failed with cc=%d", test_device_sid, cc);
556 		return;
557 	}
558 	old_pmcw_flags = schib.pmcw.flags;
559 
560 	report_prefix_push("Bit 5 set");
561 
562 	schib.pmcw.flags = old_pmcw_flags | BIT(15 - 5);
563 	cc = msch(test_device_sid, &schib);
564 	report(!cc, "MSCH cc == 0");
565 
566 	cc = stsch(test_device_sid, &schib);
567 	report(!cc, "STSCH cc == 0");
568 	report(!(schib.pmcw.flags & BIT(15 - 5)), "STSCH PMCW Bit 5 is clear");
569 
570 	report_prefix_pop();
571 
572 	report_prefix_push("Bit 5 clear");
573 
574 	schib.pmcw.flags = old_pmcw_flags & ~BIT(15 - 5);
575 	cc = msch(test_device_sid, &schib);
576 	report(!cc, "MSCH cc == 0");
577 
578 	cc = stsch(test_device_sid, &schib);
579 	report(!cc, "STSCH cc == 0");
580 	report(!(schib.pmcw.flags & BIT(15 - 5)), "STSCH PMCW Bit 5 is clear");
581 
582 	report_prefix_pop();
583 }
584 
585 static void test_tsch(void)
586 {
587 	const int align_to = 4;
588 	struct irb irb;
589 	int cc;
590 
591 	if (!test_device_sid) {
592 		report_skip("No device");
593 		return;
594 	}
595 
596 	report_prefix_push("Unaligned");
597 	for (int i = 1; i < align_to; i *= 2) {
598 		report_prefix_pushf("%d", i);
599 
600 		expect_pgm_int();
601 		tsch(test_device_sid, (struct irb *)(alignment_test_page + i));
602 		check_pgm_int_code(PGM_INT_CODE_SPECIFICATION);
603 
604 		report_prefix_pop();
605 	}
606 	report_prefix_pop();
607 
608 	report_prefix_push("Invalid subchannel number");
609 	cc = tsch(0x0001ffff, &irb);
610 	report(cc == 3, "Channel not operational");
611 	report_prefix_pop();
612 
613 	report_prefix_push("Bit 47 in SID is zero");
614 	expect_pgm_int();
615 	tsch(0x0000ffff, &irb);
616 	check_pgm_int_code(PGM_INT_CODE_OPERAND);
617 	report_prefix_pop();
618 }
619 
620 static struct {
621 	const char *name;
622 	void (*func)(void);
623 } tests[] = {
624 	/* The css_init test is needed to initialize the CSS Characteristics */
625 	{ "initialize CSS (chsc)", css_init },
626 	{ "enumerate (stsch)", test_enumerate },
627 	{ "enable (msch)", test_enable },
628 	{ "sense (ssch/tsch)", test_sense },
629 	{ "measurement block (schm)", test_schm },
630 	{ "measurement block format0", test_schm_fmt0 },
631 	{ "measurement block format1", test_schm_fmt1 },
632 	{ "msch", test_msch },
633 	{ "stcrw", test_stcrw },
634 	{ "ssch", test_ssch },
635 	{ "stsch", test_stsch },
636 	{ "pmcw bit 5 ignored", test_pmcw_bit5 },
637 	{ "tsch", test_tsch },
638 	{ NULL, NULL }
639 };
640 
641 int main(int argc, char *argv[])
642 {
643 	int i;
644 
645 	report_prefix_push("Channel Subsystem");
646 
647 	/* There's no guarantee where our devices are without qemu */
648 	if (!host_is_qemu()) {
649 		report_skip("Not running under QEMU");
650 		goto done;
651 	}
652 
653 	enable_io_isc(0x80 >> IO_SCH_ISC);
654 	for (i = 0; tests[i].name; i++) {
655 		report_prefix_push(tests[i].name);
656 		tests[i].func();
657 		report_prefix_pop();
658 	}
659 
660 done:
661 	report_prefix_pop();
662 	return report_summary();
663 }
664