xref: /linux/drivers/firewire/config-rom-generator-test.c (revision b0ae21b88ae4fd68c197d5ee3b6676162bebb61e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // config-rom-generator-test.c - An application of Kunit to test configuration ROM generator.
4 //
5 // Copyright (c) 2026 Takashi Sakamoto
6 //
7 // This file can not be built independently since it is intentionally included in core-card.c.
8 
9 #include <kunit/test.h>
10 #include <kunit/device.h>
11 
12 static const __be32 config_rom_bare[] = {
13 	cpu_to_be32(0x0404921b), // bus info
14 	cpu_to_be32(0x31333934), // |
15 	cpu_to_be32(0xf000b223), // |
16 	cpu_to_be32(0x01234567), // |
17 	cpu_to_be32(0x89abcdef), // v
18 	cpu_to_be32(0x00051b70), // root directory
19 	cpu_to_be32(0x0c0083c0), // |
20 	cpu_to_be32(0x03001f11), // |
21 	cpu_to_be32(0x81000003), // |
22 	cpu_to_be32(0x17023901), // |
23 	cpu_to_be32(0x81000008), // v
24 	cpu_to_be32(0x00064cb7), // text descriptor leaf (from root)
25 	cpu_to_be32(0x00000000), // |
26 	cpu_to_be32(0x00000000), // |
27 	cpu_to_be32(0x4c696e75), // |
28 	cpu_to_be32(0x78204669), // |
29 	cpu_to_be32(0x72657769), // |
30 	cpu_to_be32(0x72650000), // v
31 	cpu_to_be32(0x0003ff1c), // text descriptor leaf (from root)
32 	cpu_to_be32(0x00000000), // |
33 	cpu_to_be32(0x00000000), // |
34 	cpu_to_be32(0x4a756a75), // v Juju is a code name at the development time of this stack.
35 };
36 
37 // Following to Configuration ROM for AV/C Devices 1.0 (Dec. 2000. 1394 Trading Association,
38 // Document 1999027).
39 #define UNIT_SPEC_ID_1394TA		0x0000a02d
40 #define UNIT_SW_VERSION_AVC		0x00010001
41 #define UNIT_SW_VERSION_IIDC_0104	0x00000100
42 
43 static const u32 avc_unit_directory_and_leaf[] = {
44 	0x00040000,		// Unit directory consists of below 4 quads.
45 	(CSR_SPECIFIER_ID << 24) | UNIT_SPEC_ID_1394TA,
46 	(CSR_VERSION << 24) | UNIT_SW_VERSION_AVC,
47 	(CSR_MODEL << 24) | 0x00260827,	// Today.
48 	((CSR_LEAF | CSR_DESCRIPTOR) << 24) | 0x00000001, // Point to next quadlet.
49 	0x00030000,	// Text leaf consists of below 3 quads.
50 	0x00000000,
51 	0x00000000,
52 	0x50756900,	// Pui is the name of a cat that the author takes care of.
53 };
54 
55 static const __be32 config_rom_with_avc_unit[] = {
56 	cpu_to_be32(0x0404c1e5), // bus info
57 	cpu_to_be32(0x31333934), // |
58 	cpu_to_be32(0xf000b233), // |
59 	cpu_to_be32(0x01234567), // |
60 	cpu_to_be32(0x89abcdef), // v
61 	cpu_to_be32(0x0006a2d2), // root directory
62 	cpu_to_be32(0x0c0083c0), // |
63 	cpu_to_be32(0x03001f11), // |
64 	cpu_to_be32(0x81000004), // |
65 	cpu_to_be32(0x17023901), // |
66 	cpu_to_be32(0x81000009), // |
67 	cpu_to_be32(0xd100000c), // v
68 	cpu_to_be32(0x00064cb7), // text descriptor leaf (from root)
69 	cpu_to_be32(0x00000000), // |
70 	cpu_to_be32(0x00000000), // |
71 	cpu_to_be32(0x4c696e75), // |
72 	cpu_to_be32(0x78204669), // |
73 	cpu_to_be32(0x72657769), // |
74 	cpu_to_be32(0x72650000), // v
75 	cpu_to_be32(0x0003ff1c), // text descriptor leaf (from root)
76 	cpu_to_be32(0x00000000), // |
77 	cpu_to_be32(0x00000000), // |
78 	cpu_to_be32(0x4a756a75), // v
79 	cpu_to_be32(0x0004227b), // unit directory (from root)
80 	cpu_to_be32(0x1200a02d), // |
81 	cpu_to_be32(0x13010001), // |
82 	cpu_to_be32(0x17260827), // |
83 	cpu_to_be32(0x81000001), // v
84 	cpu_to_be32(0x0003f771), // text descriptor leaf (from unit)
85 	cpu_to_be32(0x00000000), // |
86 	cpu_to_be32(0x00000000), // |
87 	cpu_to_be32(0x50756900), // v
88 };
89 
90 // Following to 1394-based Digital Camera Specification Version 1.04 (Aug. 1996. 1394 Trading
91 // Association)
92 #define IIDC_COMMAND_REGS_BASE	0x00
93 #define IIDC_VENDOR_NAME_LEAF	0x01
94 #define IIDC_MODEL_NAME_LEAF	0x02
95 
96 static const u32 iidc_unit_directories_and_leafs[] = {
97 	0x00030000,
98 	(CSR_SPECIFIER_ID << 24) | UNIT_SPEC_ID_1394TA,
99 	(CSR_VERSION << 24) | UNIT_SW_VERSION_IIDC_0104,
100 	((CSR_DIRECTORY | CSR_DEPENDENT_INFO) << 24) | 0x00000001,
101 	0x00030000,
102 	((CSR_OFFSET | IIDC_COMMAND_REGS_BASE) << 24) | 0x00012345,
103 	((CSR_LEAF | IIDC_VENDOR_NAME_LEAF) << 24) | 0x00000002,
104 	((CSR_LEAF | IIDC_MODEL_NAME_LEAF) << 24) | 0x00000008,
105 	0x00060000,	// Text leaf consists of below 6 quads.
106 	0x00000000,
107 	0x00000000,
108 	0x4c696e75,	// Use the same name in root directory.
109 	0x78204669,
110 	0x72657769,
111 	0x72650000,
112 	0x00040000,	// Text leaf consists of below 4 quads.
113 	0x00000000,
114 	0x00000000,
115 	0x43686566,	// Chef Cat is a nick name when inventing IEEE 1394 itself.
116 	0x20436174,
117 };
118 
119 static const __be32 config_rom_with_iidc_unit[] = {
120 	cpu_to_be32(0x04046a3e), // bus info
121 	cpu_to_be32(0x31333934), // |
122 	cpu_to_be32(0xf000b243), // |
123 	cpu_to_be32(0x01234567), // |
124 	cpu_to_be32(0x89abcdef), // v
125 	cpu_to_be32(0x0006a2d2), // root directory
126 	cpu_to_be32(0x0c0083c0), // |
127 	cpu_to_be32(0x03001f11), // |
128 	cpu_to_be32(0x81000004), // |
129 	cpu_to_be32(0x17023901), // |
130 	cpu_to_be32(0x81000009), // |
131 	cpu_to_be32(0xd100000c), // v
132 	cpu_to_be32(0x00064cb7), // text descriptor leaf (from root)
133 	cpu_to_be32(0x00000000), // |
134 	cpu_to_be32(0x00000000), // |
135 	cpu_to_be32(0x4c696e75), // |
136 	cpu_to_be32(0x78204669), // |
137 	cpu_to_be32(0x72657769), // |
138 	cpu_to_be32(0x72650000), // v
139 	cpu_to_be32(0x0003ff1c), // text descriptor leaf (from root)
140 	cpu_to_be32(0x00000000), // |
141 	cpu_to_be32(0x00000000), // |
142 	cpu_to_be32(0x4a756a75), // v
143 	cpu_to_be32(0x0003d7fe), // unit directory (from root)
144 	cpu_to_be32(0x1200a02d), // |
145 	cpu_to_be32(0x13000100), // |
146 	cpu_to_be32(0xd4000001), // v
147 	cpu_to_be32(0x0003ea57), // dependent directory (from unit directory)
148 	cpu_to_be32(0x40012345), // |
149 	cpu_to_be32(0x81000002), // |
150 	cpu_to_be32(0x82000008), // v
151 	cpu_to_be32(0x00064cb7), // text descriptor leaf (from dependent directory)
152 	cpu_to_be32(0x00000000), // |
153 	cpu_to_be32(0x00000000), // |
154 	cpu_to_be32(0x4c696e75), // |
155 	cpu_to_be32(0x78204669), // |
156 	cpu_to_be32(0x72657769), // |
157 	cpu_to_be32(0x72650000), // v
158 	cpu_to_be32(0x0004a3e9), // text descriptor leaf (from dependent directory)
159 	cpu_to_be32(0x00000000), // |
160 	cpu_to_be32(0x00000000), // |
161 	cpu_to_be32(0x43686566), // |
162 	cpu_to_be32(0x20436174), // v
163 };
164 
165 static const __be32 config_rom_with_avc_and_iidc_units[] = {
166 	cpu_to_be32(0x040439c0), // bus info
167 	cpu_to_be32(0x31333934), // |
168 	cpu_to_be32(0xf000b253), // |
169 	cpu_to_be32(0x01234567), // |
170 	cpu_to_be32(0x89abcdef), // v
171 	cpu_to_be32(0x0007073e), // root directory
172 	cpu_to_be32(0x0c0083c0), // |
173 	cpu_to_be32(0x03001f11), // |
174 	cpu_to_be32(0x81000005), // |
175 	cpu_to_be32(0x17023901), // |
176 	cpu_to_be32(0x8100000a), // |
177 	cpu_to_be32(0xd100000d), // |
178 	cpu_to_be32(0xd1000015), // v
179 	cpu_to_be32(0x00064cb7), // text descriptor leaf (from root)
180 	cpu_to_be32(0x00000000), // |
181 	cpu_to_be32(0x00000000), // |
182 	cpu_to_be32(0x4c696e75), // |
183 	cpu_to_be32(0x78204669), // |
184 	cpu_to_be32(0x72657769), // |
185 	cpu_to_be32(0x72650000), // v
186 	cpu_to_be32(0x0003ff1c), // text descriptor leaf (from root)
187 	cpu_to_be32(0x00000000), // |
188 	cpu_to_be32(0x00000000), // |
189 	cpu_to_be32(0x4a756a75), // v
190 	cpu_to_be32(0x0004227b), // unit directory (from root)
191 	cpu_to_be32(0x1200a02d), // |
192 	cpu_to_be32(0x13010001), // |
193 	cpu_to_be32(0x17260827), // |
194 	cpu_to_be32(0x81000001), // v
195 	cpu_to_be32(0x0003f771), // text descriptor leaf (from unit)
196 	cpu_to_be32(0x00000000), // |
197 	cpu_to_be32(0x00000000), // |
198 	cpu_to_be32(0x50756900), // v
199 	cpu_to_be32(0x0003d7fe), // unit directory (from root)
200 	cpu_to_be32(0x1200a02d), // |
201 	cpu_to_be32(0x13000100), // |
202 	cpu_to_be32(0xd4000001), // v
203 	cpu_to_be32(0x0003ea57), // dependent directory (from unit directory)
204 	cpu_to_be32(0x40012345), // |
205 	cpu_to_be32(0x81000002), // |
206 	cpu_to_be32(0x82000008), // v
207 	cpu_to_be32(0x00064cb7), // text descriptor leaf (from dependent directory)
208 	cpu_to_be32(0x00000000), // |
209 	cpu_to_be32(0x00000000), // |
210 	cpu_to_be32(0x4c696e75), // |
211 	cpu_to_be32(0x78204669), // |
212 	cpu_to_be32(0x72657769), // |
213 	cpu_to_be32(0x72650000), // v
214 	cpu_to_be32(0x0004a3e9), // text descriptor leaf (from dependent directory)
215 	cpu_to_be32(0x00000000), // |
216 	cpu_to_be32(0x00000000), // |
217 	cpu_to_be32(0x43686566), // |
218 	cpu_to_be32(0x20436174), // v
219 };
220 
221 static const struct generator_test_case {
222 	const char *const name;
223 	int config_rom_generation;
224 	const __be32 *const expected;
225 	unsigned int quadlet_length;
226 } generator_test_cases[] = {
227 	{
228 		.name = "bare",
229 		.config_rom_generation = 0,
230 		.expected = config_rom_bare,
231 		.quadlet_length = ARRAY_SIZE(config_rom_bare),
232 	},
233 	{
234 		.name = "with_avc_unit",
235 		.config_rom_generation = 1,
236 		.expected = config_rom_with_avc_unit,
237 		.quadlet_length = ARRAY_SIZE(config_rom_with_avc_unit),
238 	},
239 	{
240 		.name = "with_iidc_unit",
241 		.config_rom_generation = 2,
242 		.expected = config_rom_with_iidc_unit,
243 		.quadlet_length = ARRAY_SIZE(config_rom_with_iidc_unit),
244 	},
245 	{
246 		.name = "with_avc_and_iidc_unit",
247 		.config_rom_generation = 3,
248 		.expected = config_rom_with_avc_and_iidc_units,
249 		.quadlet_length = ARRAY_SIZE(config_rom_with_avc_and_iidc_units),
250 	},
251 };
252 
253 // Define generator_test_gen_params.
254 KUNIT_ARRAY_PARAM_DESC(generator_test, generator_test_cases, name);
255 
256 struct state_data {
257 	struct fw_card card;
258 	__be32 config_rom[(CSR_CONFIG_ROM_END - CSR_CONFIG_ROM) / sizeof(__be32)];
259 };
260 
261 static void test_config_rom_generator(struct kunit *test)
262 {
263 	// Use kernel stack since they should be mutable for doubly linked-list.
264 	struct fw_descriptor avc_unit_entry = {
265 		.length = ARRAY_SIZE(avc_unit_directory_and_leaf),
266 		.key = (CSR_DIRECTORY | CSR_UNIT) << 24,
267 		.data = avc_unit_directory_and_leaf,
268 	};
269 	struct fw_descriptor iidc_unit_entry = {
270 		.length = ARRAY_SIZE(iidc_unit_directories_and_leafs),
271 		.key = (CSR_DIRECTORY | CSR_UNIT) << 24,
272 		.data = iidc_unit_directories_and_leafs,
273 	};
274 	const struct generator_test_case *test_case = test->param_value;
275 	struct state_data *state = test->priv;
276 	struct fw_card *card = &state->card;
277 	__be32 *config_rom = state->config_rom;
278 
279 	card->config_rom_generation = test_case->config_rom_generation;
280 	card->link_speed = SCODE_800;
281 	card->max_receive = 11;
282 	card->guid = 0x0123456789abcdefULL;
283 
284 	if (test_case->expected == config_rom_with_avc_unit ||
285 	    test_case->expected == config_rom_with_avc_and_iidc_units)
286 		KUNIT_EXPECT_EQ(test, fw_core_add_descriptor(&avc_unit_entry), 0);
287 
288 	if (test_case->expected == config_rom_with_iidc_unit ||
289 	    test_case->expected == config_rom_with_avc_and_iidc_units)
290 		KUNIT_EXPECT_EQ(test, fw_core_add_descriptor(&iidc_unit_entry), 0);
291 
292 	scoped_guard(mutex, &card_mutex) {
293 		generate_config_rom(card, config_rom);
294 		KUNIT_EXPECT_EQ(test, config_rom_length, test_case->quadlet_length);
295 	}
296 
297 	KUNIT_EXPECT_MEMEQ(test, config_rom, test_case->expected,
298 			   sizeof(*test_case->expected) * test_case->quadlet_length);
299 
300 	if (test_case->expected == config_rom_with_iidc_unit ||
301 	    test_case->expected == config_rom_with_avc_and_iidc_units)
302 		fw_core_remove_descriptor(&iidc_unit_entry);
303 
304 	if (test_case->expected == config_rom_with_avc_unit ||
305 	    test_case->expected == config_rom_with_avc_and_iidc_units)
306 		fw_core_remove_descriptor(&avc_unit_entry);
307 }
308 
309 static void add_descriptor_with_invalid_length(struct kunit *test)
310 {
311 	// Use kernel stack since they should be mutable for doubly linked-list.
312 	struct fw_descriptor entry_with_invalid_length = {
313 		.length = 257,
314 	};
315 
316 	KUNIT_EXPECT_EQ(test, fw_core_add_descriptor(&entry_with_invalid_length), -EINVAL);
317 }
318 
319 static void add_descriptor_with_invalid_data(struct kunit *test)
320 {
321 	// Use vendor directory defined in Annex A of Configuration ROM for AV/C Devices 1.0 (Dec.
322 	// 2000, 1394 Trading Association, TA Document 1999027).
323 	static const u32 invalid_vendor_directory[] = {
324 		0x00020000,
325 		(CSR_MODEL << 24) | 0x00009402,
326 		((CSR_LEAF | CSR_DESCRIPTOR) << 24) | 0x00000001,
327 		0xffff0000,	// The length should be 6, invalid.
328 		0x00000000,
329 		0x00000000,
330 		0x436f6e63,
331 		0x6174204e,
332 		0x6f746174,
333 		0x696f6e00,
334 	};
335 	// Use kernel stack since they should be mutable for doubly linked-list.
336 	struct fw_descriptor entry_with_invalid_data = {
337 		.length = ARRAY_SIZE(invalid_vendor_directory),
338 		.key = (CSR_DIRECTORY | CSR_VENDOR) << 24,
339 		.data = invalid_vendor_directory,
340 	};
341 
342 	KUNIT_EXPECT_EQ(test, fw_core_add_descriptor(&entry_with_invalid_data), -EINVAL);
343 }
344 
345 static void add_descriptor_beyond_upper_limit(struct kunit *test)
346 {
347 	// Use kernel stack since they should be mutable for doubly linked-list.
348 	struct fw_descriptor entry_beyond_upper_limit = {
349 		.length = 255,
350 		.key = (CSR_DIRECTORY | CSR_UNIT) << 24,
351 		.data = NULL,
352 	};
353 	u32 *data;
354 
355 	data = kunit_kzalloc(test, entry_beyond_upper_limit.length, GFP_KERNEL);
356 	KUNIT_ASSERT_NOT_NULL(test, data);
357 
358 	data[0] = (entry_beyond_upper_limit.length - 1) << 16;
359 	entry_beyond_upper_limit.data = data;
360 	KUNIT_EXPECT_EQ(test, fw_core_add_descriptor(&entry_beyond_upper_limit), -EBUSY);
361 
362 	kunit_kfree(test, data);
363 }
364 
365 static const struct fw_card_driver dummy_card_driver;
366 
367 static int config_rom_generator_test_init(struct kunit *test)
368 {
369 	struct state_data *state = kunit_kzalloc(test, sizeof(*state), GFP_KERNEL);
370 
371 	KUNIT_ASSERT_NOT_NULL(test, state);
372 
373 	struct device *dev = kunit_device_register(test, "dummy-device");
374 
375 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
376 
377 	fw_card_initialize(&state->card, &dummy_card_driver, dev);
378 
379 	test->priv = state;
380 
381 	return 0;
382 }
383 
384 static void config_rom_generator_test_exit(struct kunit *test)
385 {
386 	struct state_data *state = test->priv;
387 
388 	kunit_device_unregister(test, state->card.device);
389 	kunit_kfree(test, state);
390 }
391 
392 
393 static struct kunit_case config_rom_generator_test_cases[] = {
394 	KUNIT_CASE_PARAM(test_config_rom_generator, generator_test_gen_params),
395 	KUNIT_CASE(add_descriptor_with_invalid_length),
396 	KUNIT_CASE(add_descriptor_with_invalid_data),
397 	KUNIT_CASE(add_descriptor_beyond_upper_limit),
398 	{}
399 };
400 
401 static struct kunit_suite config_rom_generator_test_suite = {
402 	.name = "firewire-config-rom-generator",
403 	.init = config_rom_generator_test_init,
404 	.exit = config_rom_generator_test_exit,
405 	.test_cases = config_rom_generator_test_cases,
406 };
407 kunit_test_suite(config_rom_generator_test_suite);
408