xref: /linux/drivers/firewire/ohci-serdes-test.c (revision 3ea5eb68b9d624935108b5e696859304edfac202)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // ohci-serdes-test.c - An application of Kunit to check serialization/deserialization of data in
4 //			buffers and registers defined in 1394 OHCI specification.
5 //
6 // Copyright (c) 2024 Takashi Sakamoto
7 
8 #include <kunit/test.h>
9 
10 #include "ohci.h"
11 
12 
13 static void test_self_id_count_register_deserialization(struct kunit *test)
14 {
15 	const u32 expected = 0x803d0594;
16 
17 	bool is_error = ohci1394_self_id_count_is_error(expected);
18 	u8 generation = ohci1394_self_id_count_get_generation(expected);
19 	u32 size = ohci1394_self_id_count_get_size(expected);
20 
21 	KUNIT_EXPECT_TRUE(test, is_error);
22 	KUNIT_EXPECT_EQ(test, 0x3d, generation);
23 	KUNIT_EXPECT_EQ(test, 0x165, size);
24 }
25 
26 static void test_self_id_receive_buffer_deserialization(struct kunit *test)
27 {
28 	const u32 buffer[] = {
29 		0x0006f38b,
30 		0x807fcc56,
31 		0x7f8033a9,
32 		0x8145cc5e,
33 		0x7eba33a1,
34 	};
35 
36 	u8 generation = ohci1394_self_id_receive_q0_get_generation(buffer[0]);
37 	u16 timestamp = ohci1394_self_id_receive_q0_get_timestamp(buffer[0]);
38 
39 	KUNIT_EXPECT_EQ(test, 0x6, generation);
40 	KUNIT_EXPECT_EQ(test, 0xf38b, timestamp);
41 }
42 
43 static void test_at_data_serdes(struct kunit *test)
44 {
45 	static const __le32 expected[] = {
46 		cpu_to_le32(0x00020e80),
47 		cpu_to_le32(0xffc2ffff),
48 		cpu_to_le32(0xe0000000),
49 	};
50 	__le32 quadlets[] = {0, 0, 0};
51 	bool has_src_bus_id = ohci1394_at_data_get_src_bus_id(expected);
52 	unsigned int speed = ohci1394_at_data_get_speed(expected);
53 	unsigned int tlabel = ohci1394_at_data_get_tlabel(expected);
54 	unsigned int retry = ohci1394_at_data_get_retry(expected);
55 	unsigned int tcode = ohci1394_at_data_get_tcode(expected);
56 	unsigned int destination_id = ohci1394_at_data_get_destination_id(expected);
57 	u64 destination_offset = ohci1394_at_data_get_destination_offset(expected);
58 
59 	KUNIT_EXPECT_FALSE(test, has_src_bus_id);
60 	KUNIT_EXPECT_EQ(test, 0x02, speed);
61 	KUNIT_EXPECT_EQ(test, 0x03, tlabel);
62 	KUNIT_EXPECT_EQ(test, 0x02, retry);
63 	KUNIT_EXPECT_EQ(test, 0x08, tcode);
64 
65 	ohci1394_at_data_set_src_bus_id(quadlets, has_src_bus_id);
66 	ohci1394_at_data_set_speed(quadlets, speed);
67 	ohci1394_at_data_set_tlabel(quadlets, tlabel);
68 	ohci1394_at_data_set_retry(quadlets, retry);
69 	ohci1394_at_data_set_tcode(quadlets, tcode);
70 	ohci1394_at_data_set_destination_id(quadlets, destination_id);
71 	ohci1394_at_data_set_destination_offset(quadlets, destination_offset);
72 
73 	KUNIT_EXPECT_MEMEQ(test, quadlets, expected, sizeof(expected));
74 }
75 
76 static void test_it_data_serdes(struct kunit *test)
77 {
78 	static const __le32 expected[] = {
79 		cpu_to_le32(0x000349a7),
80 		cpu_to_le32(0x02300000),
81 	};
82 	__le32 quadlets[] = {0, 0};
83 	unsigned int scode = ohci1394_it_data_get_speed(expected);
84 	unsigned int tag = ohci1394_it_data_get_tag(expected);
85 	unsigned int channel = ohci1394_it_data_get_channel(expected);
86 	unsigned int tcode = ohci1394_it_data_get_tcode(expected);
87 	unsigned int sync = ohci1394_it_data_get_sync(expected);
88 	unsigned int data_length = ohci1394_it_data_get_data_length(expected);
89 
90 	KUNIT_EXPECT_EQ(test, 0x03, scode);
91 	KUNIT_EXPECT_EQ(test, 0x01, tag);
92 	KUNIT_EXPECT_EQ(test, 0x09, channel);
93 	KUNIT_EXPECT_EQ(test, 0x0a, tcode);
94 	KUNIT_EXPECT_EQ(test, 0x7, sync);
95 	KUNIT_EXPECT_EQ(test, 0x0230, data_length);
96 
97 	ohci1394_it_data_set_speed(quadlets, scode);
98 	ohci1394_it_data_set_tag(quadlets, tag);
99 	ohci1394_it_data_set_channel(quadlets, channel);
100 	ohci1394_it_data_set_tcode(quadlets, tcode);
101 	ohci1394_it_data_set_sync(quadlets, sync);
102 	ohci1394_it_data_set_data_length(quadlets, data_length);
103 
104 	KUNIT_EXPECT_MEMEQ(test, quadlets, expected, sizeof(expected));
105 }
106 
107 static struct kunit_case ohci_serdes_test_cases[] = {
108 	KUNIT_CASE(test_self_id_count_register_deserialization),
109 	KUNIT_CASE(test_self_id_receive_buffer_deserialization),
110 	KUNIT_CASE(test_at_data_serdes),
111 	KUNIT_CASE(test_it_data_serdes),
112 	{}
113 };
114 
115 static struct kunit_suite ohci_serdes_test_suite = {
116 	.name = "firewire-ohci-serdes",
117 	.test_cases = ohci_serdes_test_cases,
118 };
119 kunit_test_suite(ohci_serdes_test_suite);
120 
121 MODULE_DESCRIPTION("FireWire buffers and registers serialization/deserialization unit test suite");
122 MODULE_LICENSE("GPL");
123