xref: /linux/drivers/firewire/packet-serdes-test.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // packet-serdes-test.c - An application of Kunit to check serialization/deserialization of packets
4 //			  defined by IEEE 1394.
5 //
6 // Copyright (c) 2024 Takashi Sakamoto
7 
8 #include <kunit/test.h>
9 
10 #include <linux/firewire-constants.h>
11 
12 #include "packet-header-definitions.h"
13 #include "phy-packet-definitions.h"
14 
15 static void serialize_async_header_common(u32 header[ASYNC_HEADER_QUADLET_COUNT],
16 					  unsigned int dst_id, unsigned int tlabel,
17 					  unsigned int retry, unsigned int tcode,
18 					  unsigned int priority, unsigned int src_id)
19 {
20 	async_header_set_destination(header, dst_id);
21 	async_header_set_tlabel(header, tlabel);
22 	async_header_set_retry(header, retry);
23 	async_header_set_tcode(header, tcode);
24 	async_header_set_priority(header, priority);
25 	async_header_set_source(header, src_id);
26 }
27 
28 static void serialize_async_header_request(u32 header[ASYNC_HEADER_QUADLET_COUNT],
29 					   unsigned int dst_id, unsigned int tlabel,
30 					   unsigned int retry, unsigned int tcode,
31 					   unsigned int priority, unsigned int src_id, u64 offset)
32 {
33 	serialize_async_header_common(header, dst_id, tlabel, retry, tcode, priority, src_id);
34 	async_header_set_offset(header, offset);
35 }
36 
37 static void serialize_async_header_quadlet_request(u32 header[ASYNC_HEADER_QUADLET_COUNT],
38 						   unsigned int dst_id, unsigned int tlabel,
39 						   unsigned int retry, unsigned int tcode,
40 						   unsigned int priority, unsigned int src_id,
41 						   u64 offset)
42 {
43 	serialize_async_header_request(header, dst_id, tlabel, retry, tcode, priority, src_id,
44 				       offset);
45 }
46 
47 static void serialize_async_header_block_request(u32 header[ASYNC_HEADER_QUADLET_COUNT],
48 						 unsigned int dst_id, unsigned int tlabel,
49 						 unsigned int retry, unsigned int tcode,
50 						 unsigned int priority, unsigned int src_id,
51 						 u64 offset, unsigned int data_length,
52 						 unsigned int extended_tcode)
53 {
54 	serialize_async_header_request(header, dst_id, tlabel, retry, tcode, priority, src_id,
55 				       offset);
56 	async_header_set_data_length(header, data_length);
57 	async_header_set_extended_tcode(header, extended_tcode);
58 }
59 
60 static void serialize_async_header_response(u32 header[ASYNC_HEADER_QUADLET_COUNT],
61 					    unsigned int dst_id, unsigned int tlabel,
62 					    unsigned int retry, unsigned int tcode,
63 					    unsigned int priority, unsigned int src_id,
64 					    unsigned int rcode)
65 {
66 	serialize_async_header_common(header, dst_id, tlabel, retry, tcode, priority, src_id);
67 	async_header_set_rcode(header, rcode);
68 }
69 
70 static void serialize_async_header_quadlet_response(u32 header[ASYNC_HEADER_QUADLET_COUNT],
71 						    unsigned int dst_id, unsigned int tlabel,
72 						    unsigned int retry, unsigned int tcode,
73 						    unsigned int priority, unsigned int src_id,
74 						    unsigned int rcode)
75 {
76 	serialize_async_header_response(header, dst_id, tlabel, retry, tcode, priority, src_id,
77 					rcode);
78 }
79 
80 static void serialize_async_header_block_response(u32 header[ASYNC_HEADER_QUADLET_COUNT],
81 						  unsigned int dst_id, unsigned int tlabel,
82 						  unsigned int retry, unsigned int tcode,
83 						  unsigned int priority, unsigned int src_id,
84 						  unsigned int rcode, unsigned int data_length,
85 						  unsigned int extended_tcode)
86 {
87 	serialize_async_header_response(header, dst_id, tlabel, retry, tcode, priority, src_id,
88 					rcode);
89 	async_header_set_data_length(header, data_length);
90 	async_header_set_extended_tcode(header, extended_tcode);
91 }
92 
93 static void deserialize_async_header_common(const u32 header[ASYNC_HEADER_QUADLET_COUNT],
94 					    unsigned int *dst_id, unsigned int *tlabel,
95 					    unsigned int *retry, unsigned int *tcode,
96 					    unsigned int *priority, unsigned int *src_id)
97 {
98 	*dst_id = async_header_get_destination(header);
99 	*tlabel = async_header_get_tlabel(header);
100 	*retry = async_header_get_retry(header);
101 	*tcode = async_header_get_tcode(header);
102 	*priority = async_header_get_priority(header);
103 	*src_id = async_header_get_source(header);
104 }
105 
106 static void deserialize_async_header_request(const u32 header[ASYNC_HEADER_QUADLET_COUNT],
107 					     unsigned int *dst_id, unsigned int *tlabel,
108 					     unsigned int *retry, unsigned int *tcode,
109 					     unsigned int *priority, unsigned int *src_id,
110 					     u64 *offset)
111 {
112 	deserialize_async_header_common(header, dst_id, tlabel, retry, tcode, priority, src_id);
113 	*offset = async_header_get_offset(header);
114 }
115 
116 static void deserialize_async_header_quadlet_request(const u32 header[ASYNC_HEADER_QUADLET_COUNT],
117 						     unsigned int *dst_id, unsigned int *tlabel,
118 						     unsigned int *retry, unsigned int *tcode,
119 						     unsigned int *priority, unsigned int *src_id,
120 						     u64 *offset)
121 {
122 	deserialize_async_header_request(header, dst_id, tlabel, retry, tcode, priority, src_id,
123 					 offset);
124 }
125 
126 static void deserialize_async_header_block_request(const u32 header[ASYNC_HEADER_QUADLET_COUNT],
127 						   unsigned int *dst_id, unsigned int *tlabel,
128 						   unsigned int *retry, unsigned int *tcode,
129 						   unsigned int *priority, unsigned int *src_id,
130 						   u64 *offset,
131 						   unsigned int *data_length,
132 						   unsigned int *extended_tcode)
133 {
134 	deserialize_async_header_request(header, dst_id, tlabel, retry, tcode, priority, src_id,
135 					 offset);
136 	*data_length = async_header_get_data_length(header);
137 	*extended_tcode = async_header_get_extended_tcode(header);
138 }
139 
140 static void deserialize_async_header_response(const u32 header[ASYNC_HEADER_QUADLET_COUNT],
141 					      unsigned int *dst_id, unsigned int *tlabel,
142 					      unsigned int *retry, unsigned int *tcode,
143 					      unsigned int *priority, unsigned int *src_id,
144 					      unsigned int *rcode)
145 {
146 	deserialize_async_header_common(header, dst_id, tlabel, retry, tcode, priority, src_id);
147 	*rcode = async_header_get_rcode(header);
148 }
149 
150 static void deserialize_async_header_quadlet_response(const u32 header[ASYNC_HEADER_QUADLET_COUNT],
151 						      unsigned int *dst_id, unsigned int *tlabel,
152 						      unsigned int *retry, unsigned int *tcode,
153 						      unsigned int *priority, unsigned int *src_id,
154 						      unsigned int *rcode)
155 {
156 	deserialize_async_header_response(header, dst_id, tlabel, retry, tcode, priority, src_id, rcode);
157 }
158 
159 static void deserialize_async_header_block_response(const u32 header[ASYNC_HEADER_QUADLET_COUNT],
160 						    unsigned int *dst_id, unsigned int *tlabel,
161 						    unsigned int *retry, unsigned int *tcode,
162 						    unsigned int *priority, unsigned int *src_id,
163 						    unsigned int *rcode, unsigned int *data_length,
164 						    unsigned int *extended_tcode)
165 {
166 	deserialize_async_header_response(header, dst_id, tlabel, retry, tcode, priority, src_id, rcode);
167 	*data_length = async_header_get_data_length(header);
168 	*extended_tcode = async_header_get_extended_tcode(header);
169 }
170 
171 static void serialize_isoc_header(u32 *header, unsigned int data_length, unsigned int tag,
172 				  unsigned int channel, unsigned int tcode, unsigned int sy)
173 {
174 	isoc_header_set_data_length(header, data_length);
175 	isoc_header_set_tag(header, tag);
176 	isoc_header_set_channel(header, channel);
177 	isoc_header_set_tcode(header, tcode);
178 	isoc_header_set_sy(header, sy);
179 }
180 
181 static void deserialize_isoc_header(u32 header, unsigned int *data_length, unsigned int *tag,
182 				    unsigned int *channel, unsigned int *tcode, unsigned int *sy)
183 {
184 	*data_length = isoc_header_get_data_length(header);
185 	*tag = isoc_header_get_tag(header);
186 	*channel = isoc_header_get_channel(header);
187 	*tcode = isoc_header_get_tcode(header);
188 	*sy = isoc_header_get_sy(header);
189 }
190 
191 static void serialize_phy_packet_self_id_zero(u32 *quadlet, unsigned int packet_identifier,
192 					      unsigned int phy_id, bool extended,
193 					      bool link_is_active, unsigned int gap_count,
194 					      unsigned int scode, bool is_contender,
195 					      unsigned int power_class, bool is_initiated_reset,
196 					      bool has_more_packets)
197 {
198 	phy_packet_set_packet_identifier(quadlet, packet_identifier);
199 	phy_packet_self_id_set_phy_id(quadlet, phy_id);
200 	phy_packet_self_id_set_extended(quadlet, extended);
201 	phy_packet_self_id_zero_set_link_active(quadlet, link_is_active);
202 	phy_packet_self_id_zero_set_gap_count(quadlet, gap_count);
203 	phy_packet_self_id_zero_set_scode(quadlet, scode);
204 	phy_packet_self_id_zero_set_contender(quadlet, is_contender);
205 	phy_packet_self_id_zero_set_power_class(quadlet, power_class);
206 	phy_packet_self_id_zero_set_initiated_reset(quadlet, is_initiated_reset);
207 	phy_packet_self_id_set_more_packets(quadlet, has_more_packets);
208 }
209 
210 static void deserialize_phy_packet_self_id_zero(u32 quadlet, unsigned int *packet_identifier,
211 						unsigned int *phy_id, bool *extended,
212 						bool *link_is_active, unsigned int *gap_count,
213 						unsigned int *scode, bool *is_contender,
214 						unsigned int *power_class,
215 						bool *is_initiated_reset, bool *has_more_packets)
216 {
217 	*packet_identifier = phy_packet_get_packet_identifier(quadlet);
218 	*phy_id = phy_packet_self_id_get_phy_id(quadlet);
219 	*extended = phy_packet_self_id_get_extended(quadlet);
220 	*link_is_active = phy_packet_self_id_zero_get_link_active(quadlet);
221 	*gap_count = phy_packet_self_id_zero_get_gap_count(quadlet);
222 	*scode = phy_packet_self_id_zero_get_scode(quadlet);
223 	*is_contender = phy_packet_self_id_zero_get_contender(quadlet);
224 	*power_class = phy_packet_self_id_zero_get_power_class(quadlet);
225 	*is_initiated_reset = phy_packet_self_id_zero_get_initiated_reset(quadlet);
226 	*has_more_packets = phy_packet_self_id_get_more_packets(quadlet);
227 }
228 
229 static void serialize_phy_packet_self_id_extended(u32 *quadlet, unsigned int packet_identifier,
230 						  unsigned int phy_id, bool extended,
231 						  unsigned int sequence, bool has_more_packets)
232 {
233 	phy_packet_set_packet_identifier(quadlet, packet_identifier);
234 	phy_packet_self_id_set_phy_id(quadlet, phy_id);
235 	phy_packet_self_id_set_extended(quadlet, extended);
236 	phy_packet_self_id_extended_set_sequence(quadlet, sequence);
237 	phy_packet_self_id_set_more_packets(quadlet, has_more_packets);
238 }
239 
240 static void deserialize_phy_packet_self_id_extended(u32 quadlet, unsigned int *packet_identifier,
241 						    unsigned int *phy_id, bool *extended,
242 						    unsigned int *sequence, bool *has_more_packets)
243 {
244 	*packet_identifier = phy_packet_get_packet_identifier(quadlet);
245 	*phy_id = phy_packet_self_id_get_phy_id(quadlet);
246 	*extended = phy_packet_self_id_get_extended(quadlet);
247 	*sequence = phy_packet_self_id_extended_get_sequence(quadlet);
248 	*has_more_packets = phy_packet_self_id_get_more_packets(quadlet);
249 }
250 
251 static void serialize_phy_packet_phy_config(u32 *quadlet, unsigned int packet_identifier,
252 					    unsigned int root_id, bool has_force_root_node,
253 					    bool has_gap_count_optimization, unsigned int gap_count)
254 {
255 	phy_packet_set_packet_identifier(quadlet, packet_identifier);
256 	phy_packet_phy_config_set_root_id(quadlet, root_id);
257 	phy_packet_phy_config_set_force_root_node(quadlet, has_force_root_node);
258 	phy_packet_phy_config_set_gap_count_optimization(quadlet, has_gap_count_optimization);
259 	phy_packet_phy_config_set_gap_count(quadlet, gap_count);
260 }
261 
262 static void deserialize_phy_packet_phy_config(u32 quadlet, unsigned int *packet_identifier,
263 					      unsigned int *root_id, bool *has_force_root_node,
264 					      bool *has_gap_count_optimization,
265 					      unsigned int *gap_count)
266 {
267 	*packet_identifier = phy_packet_get_packet_identifier(quadlet);
268 	*root_id = phy_packet_phy_config_get_root_id(quadlet);
269 	*has_force_root_node = phy_packet_phy_config_get_force_root_node(quadlet);
270 	*has_gap_count_optimization = phy_packet_phy_config_get_gap_count_optimization(quadlet);
271 	*gap_count = phy_packet_phy_config_get_gap_count(quadlet);
272 }
273 
274 static void test_async_header_write_quadlet_request(struct kunit *test)
275 {
276 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
277 		0xffc05100,
278 		0xffc1ffff,
279 		0xf0000234,
280 		0x1f0000c0,
281 	};
282 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
283 
284 	unsigned int dst_id;
285 	unsigned int tlabel;
286 	unsigned int retry;
287 	unsigned int tcode;
288 	unsigned int priority;
289 	unsigned int src_id;
290 	u64 offset;
291 	u32 quadlet_data;
292 
293 	deserialize_async_header_quadlet_request(expected, &dst_id, &tlabel, &retry, &tcode,
294 						 &priority, &src_id, &offset);
295 	quadlet_data = async_header_get_quadlet_data(expected);
296 
297 	KUNIT_EXPECT_EQ(test, 0xffc0, dst_id);
298 	KUNIT_EXPECT_EQ(test, 0x14, tlabel);
299 	KUNIT_EXPECT_EQ(test, 0x01, retry);
300 	KUNIT_EXPECT_EQ(test, TCODE_WRITE_QUADLET_REQUEST, tcode);
301 	KUNIT_EXPECT_EQ(test, 0x00, priority);
302 	KUNIT_EXPECT_EQ(test, 0xffc1, src_id);
303 	KUNIT_EXPECT_EQ(test, 0xfffff0000234, offset);
304 	KUNIT_EXPECT_EQ(test, 0x1f0000c0, quadlet_data);
305 
306 	serialize_async_header_quadlet_request(header, dst_id, tlabel, retry, tcode, priority,
307 					       src_id, offset);
308 	async_header_set_quadlet_data(header, quadlet_data);
309 
310 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected));
311 }
312 
313 static void test_async_header_write_block_request(struct kunit *test)
314 {
315 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
316 		0xffc06510,
317 		0xffc1ecc0,
318 		0x00000000,
319 		0x00180000,
320 	};
321 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
322 
323 	unsigned int dst_id;
324 	unsigned int tlabel;
325 	unsigned int retry;
326 	unsigned int tcode;
327 	unsigned int priority;
328 	unsigned int src_id;
329 	u64 offset;
330 	unsigned int data_length;
331 	unsigned int extended_tcode;
332 
333 	deserialize_async_header_block_request(expected, &dst_id, &tlabel, &retry, &tcode,
334 					       &priority, &src_id, &offset, &data_length,
335 					       &extended_tcode);
336 
337 	KUNIT_EXPECT_EQ(test, 0xffc0, dst_id);
338 	KUNIT_EXPECT_EQ(test, 0x19, tlabel);
339 	KUNIT_EXPECT_EQ(test, 0x01, retry);
340 	KUNIT_EXPECT_EQ(test, TCODE_WRITE_BLOCK_REQUEST, tcode);
341 	KUNIT_EXPECT_EQ(test, 0x00, priority);
342 	KUNIT_EXPECT_EQ(test, 0xffc1, src_id);
343 	KUNIT_EXPECT_EQ(test, 0xecc000000000, offset);
344 	KUNIT_EXPECT_EQ(test, 0x0018, data_length);
345 	KUNIT_EXPECT_EQ(test, 0x0000, extended_tcode);
346 
347 	serialize_async_header_block_request(header, dst_id, tlabel, retry, tcode, priority, src_id,
348 					     offset, data_length, extended_tcode);
349 
350 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected));
351 }
352 
353 static void test_async_header_write_response(struct kunit *test)
354 {
355 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
356 		0xffc15120,
357 		0xffc00000,
358 		0x00000000,
359 		0x00000000,
360 	};
361 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
362 
363 	unsigned int dst_id;
364 	unsigned int tlabel;
365 	unsigned int retry;
366 	unsigned int tcode;
367 	unsigned int priority;
368 	unsigned int src_id;
369 	unsigned int rcode;
370 
371 	deserialize_async_header_quadlet_response(expected, &dst_id, &tlabel, &retry, &tcode,
372 						  &priority, &src_id, &rcode);
373 
374 	KUNIT_EXPECT_EQ(test, 0xffc1, dst_id);
375 	KUNIT_EXPECT_EQ(test, 0x14, tlabel);
376 	KUNIT_EXPECT_EQ(test, 0x01, retry);
377 	KUNIT_EXPECT_EQ(test, TCODE_WRITE_RESPONSE, tcode);
378 	KUNIT_EXPECT_EQ(test, 0x00, priority);
379 	KUNIT_EXPECT_EQ(test, 0xffc0, src_id);
380 	KUNIT_EXPECT_EQ(test, RCODE_COMPLETE, rcode);
381 
382 	serialize_async_header_quadlet_response(header, dst_id, tlabel, retry, tcode, priority,
383 						src_id, rcode);
384 
385 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected) - sizeof(expected[0]));
386 }
387 
388 static void test_async_header_read_quadlet_request(struct kunit *test)
389 {
390 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
391 		0xffc0f140,
392 		0xffc1ffff,
393 		0xf0000984,
394 		0x00000000,
395 	};
396 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
397 
398 	unsigned int dst_id;
399 	unsigned int tlabel;
400 	unsigned int retry;
401 	unsigned int tcode;
402 	unsigned int priority;
403 	unsigned int src_id;
404 	u64 offset;
405 
406 	deserialize_async_header_quadlet_request(expected, &dst_id, &tlabel, &retry, &tcode,
407 						 &priority, &src_id, &offset);
408 
409 	KUNIT_EXPECT_EQ(test, 0xffc0, dst_id);
410 	KUNIT_EXPECT_EQ(test, 0x3c, tlabel);
411 	KUNIT_EXPECT_EQ(test, 0x01, retry);
412 	KUNIT_EXPECT_EQ(test, TCODE_READ_QUADLET_REQUEST, tcode);
413 	KUNIT_EXPECT_EQ(test, 0x00, priority);
414 	KUNIT_EXPECT_EQ(test, 0xffc1, src_id);
415 	KUNIT_EXPECT_EQ(test, 0xfffff0000984, offset);
416 
417 	serialize_async_header_quadlet_request(header, dst_id, tlabel, retry, tcode, priority,
418 					       src_id, offset);
419 
420 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected));
421 }
422 
423 static void test_async_header_read_quadlet_response(struct kunit *test)
424 {
425 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
426 		0xffc1f160,
427 		0xffc00000,
428 		0x00000000,
429 		0x00000180,
430 	};
431 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
432 
433 	unsigned int dst_id;
434 	unsigned int tlabel;
435 	unsigned int retry;
436 	unsigned int tcode;
437 	unsigned int priority;
438 	unsigned int src_id;
439 	unsigned int rcode;
440 	u32 quadlet_data;
441 
442 	deserialize_async_header_quadlet_response(expected, &dst_id, &tlabel, &retry, &tcode,
443 						  &priority, &src_id, &rcode);
444 	quadlet_data = async_header_get_quadlet_data(expected);
445 
446 	KUNIT_EXPECT_EQ(test, 0xffc1, dst_id);
447 	KUNIT_EXPECT_EQ(test, 0x3c, tlabel);
448 	KUNIT_EXPECT_EQ(test, 0x01, retry);
449 	KUNIT_EXPECT_EQ(test, TCODE_READ_QUADLET_RESPONSE, tcode);
450 	KUNIT_EXPECT_EQ(test, 0x00, priority);
451 	KUNIT_EXPECT_EQ(test, 0xffc0, src_id);
452 	KUNIT_EXPECT_EQ(test, RCODE_COMPLETE, rcode);
453 	KUNIT_EXPECT_EQ(test, 0x00000180, quadlet_data);
454 
455 	serialize_async_header_quadlet_response(header, dst_id, tlabel, retry, tcode, priority,
456 						src_id, rcode);
457 	async_header_set_quadlet_data(header, quadlet_data);
458 
459 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected));
460 }
461 
462 static void test_async_header_read_block_request(struct kunit *test)
463 {
464 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
465 		0xffc0e150,
466 		0xffc1ffff,
467 		0xf0000400,
468 		0x00200000,
469 	};
470 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
471 
472 	unsigned int dst_id;
473 	unsigned int tlabel;
474 	unsigned int retry;
475 	unsigned int tcode;
476 	unsigned int priority;
477 	unsigned int src_id;
478 	u64 offset;
479 	unsigned int data_length;
480 	unsigned int extended_tcode;
481 
482 	deserialize_async_header_block_request(expected, &dst_id, &tlabel, &retry, &tcode,
483 					       &priority, &src_id, &offset, &data_length,
484 					       &extended_tcode);
485 
486 	KUNIT_EXPECT_EQ(test, 0xffc0, dst_id);
487 	KUNIT_EXPECT_EQ(test, 0x38, tlabel);
488 	KUNIT_EXPECT_EQ(test, 0x01, retry);
489 	KUNIT_EXPECT_EQ(test, TCODE_READ_BLOCK_REQUEST, tcode);
490 	KUNIT_EXPECT_EQ(test, 0x00, priority);
491 	KUNIT_EXPECT_EQ(test, 0xffc1, src_id);
492 	KUNIT_EXPECT_EQ(test, 0xfffff0000400, offset);
493 	KUNIT_EXPECT_EQ(test, 0x0020, data_length);
494 	KUNIT_EXPECT_EQ(test, 0x0000, extended_tcode);
495 
496 	serialize_async_header_block_request(header, dst_id, tlabel, retry, tcode, priority, src_id,
497 					     offset, data_length, extended_tcode);
498 
499 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected));
500 }
501 
502 static void test_async_header_read_block_response(struct kunit *test)
503 {
504 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
505 		0xffc1e170,
506 		0xffc00000,
507 		0x00000000,
508 		0x00200000,
509 	};
510 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
511 
512 	unsigned int dst_id;
513 	unsigned int tlabel;
514 	unsigned int retry;
515 	unsigned int tcode;
516 	unsigned int priority;
517 	unsigned int src_id;
518 	unsigned int rcode;
519 	unsigned int data_length;
520 	unsigned int extended_tcode;
521 
522 	deserialize_async_header_block_response(expected, &dst_id, &tlabel, &retry, &tcode,
523 						&priority, &src_id, &rcode, &data_length,
524 						&extended_tcode);
525 
526 	KUNIT_EXPECT_EQ(test, 0xffc1, dst_id);
527 	KUNIT_EXPECT_EQ(test, 0x38, tlabel);
528 	KUNIT_EXPECT_EQ(test, 0x01, retry);
529 	KUNIT_EXPECT_EQ(test, TCODE_READ_BLOCK_RESPONSE, tcode);
530 	KUNIT_EXPECT_EQ(test, 0x00, priority);
531 	KUNIT_EXPECT_EQ(test, 0xffc0, src_id);
532 	KUNIT_EXPECT_EQ(test, RCODE_COMPLETE, rcode);
533 	KUNIT_EXPECT_EQ(test, 0x0020, data_length);
534 	KUNIT_EXPECT_EQ(test, 0x0000, extended_tcode);
535 
536 	serialize_async_header_block_response(header, dst_id, tlabel, retry, tcode, priority,
537 					      src_id, rcode, data_length, extended_tcode);
538 
539 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected));
540 }
541 
542 static void test_async_header_lock_request(struct kunit *test)
543 {
544 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
545 		0xffc02d90,
546 		0xffc1ffff,
547 		0xf0000984,
548 		0x00080002,
549 	};
550 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
551 
552 	unsigned int dst_id;
553 	unsigned int tlabel;
554 	unsigned int retry;
555 	unsigned int tcode;
556 	unsigned int priority;
557 	unsigned int src_id;
558 	u64 offset;
559 	unsigned int data_length;
560 	unsigned int extended_tcode;
561 
562 	deserialize_async_header_block_request(expected, &dst_id, &tlabel, &retry, &tcode,
563 					       &priority, &src_id, &offset, &data_length,
564 					       &extended_tcode);
565 
566 	KUNIT_EXPECT_EQ(test, 0xffc0, dst_id);
567 	KUNIT_EXPECT_EQ(test, 0x0b, tlabel);
568 	KUNIT_EXPECT_EQ(test, 0x01, retry);
569 	KUNIT_EXPECT_EQ(test, TCODE_LOCK_REQUEST, tcode);
570 	KUNIT_EXPECT_EQ(test, 0x00, priority);
571 	KUNIT_EXPECT_EQ(test, 0xffc1, src_id);
572 	KUNIT_EXPECT_EQ(test, 0xfffff0000984, offset);
573 	KUNIT_EXPECT_EQ(test, 0x0008, data_length);
574 	KUNIT_EXPECT_EQ(test, EXTCODE_COMPARE_SWAP, extended_tcode);
575 
576 	serialize_async_header_block_request(header, dst_id, tlabel, retry, tcode, priority, src_id,
577 					     offset, data_length, extended_tcode);
578 
579 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected));
580 }
581 
582 static void test_async_header_lock_response(struct kunit *test)
583 {
584 	static const u32 expected[ASYNC_HEADER_QUADLET_COUNT] = {
585 		0xffc12db0,
586 		0xffc00000,
587 		0x00000000,
588 		0x00040002,
589 	};
590 	u32 header[ASYNC_HEADER_QUADLET_COUNT] = {0, 0, 0, 0};
591 
592 	unsigned int dst_id;
593 	unsigned int tlabel;
594 	unsigned int retry;
595 	unsigned int tcode;
596 	unsigned int priority;
597 	unsigned int src_id;
598 	unsigned int rcode;
599 	unsigned int data_length;
600 	unsigned int extended_tcode;
601 
602 	deserialize_async_header_block_response(expected, &dst_id, &tlabel, &retry, &tcode,
603 						&priority, &src_id, &rcode, &data_length,
604 						&extended_tcode);
605 
606 	KUNIT_EXPECT_EQ(test, 0xffc1, dst_id);
607 	KUNIT_EXPECT_EQ(test, 0x0b, tlabel);
608 	KUNIT_EXPECT_EQ(test, 0x01, retry);
609 	KUNIT_EXPECT_EQ(test, TCODE_LOCK_RESPONSE, tcode);
610 	KUNIT_EXPECT_EQ(test, 0x00, priority);
611 	KUNIT_EXPECT_EQ(test, 0xffc0, src_id);
612 	KUNIT_EXPECT_EQ(test, RCODE_COMPLETE, rcode);
613 	KUNIT_EXPECT_EQ(test, 0x0004, data_length);
614 	KUNIT_EXPECT_EQ(test, EXTCODE_COMPARE_SWAP, extended_tcode);
615 
616 	serialize_async_header_block_response(header, dst_id, tlabel, retry, tcode, priority,
617 					      src_id, rcode, data_length, extended_tcode);
618 
619 	KUNIT_EXPECT_MEMEQ(test, header, expected, sizeof(expected));
620 }
621 
622 static void test_isoc_header(struct kunit *test)
623 {
624 	const u32 expected = 0x00d08dec;
625 	u32 header = 0;
626 
627 	unsigned int data_length;
628 	unsigned int tag;
629 	unsigned int channel;
630 	unsigned int tcode;
631 	unsigned int sy;
632 
633 	deserialize_isoc_header(expected, &data_length, &tag, &channel, &tcode, &sy);
634 
635 	KUNIT_EXPECT_EQ(test, 0xd0, data_length);
636 	KUNIT_EXPECT_EQ(test, 0x02, tag);
637 	KUNIT_EXPECT_EQ(test, 0x0d, channel);
638 	KUNIT_EXPECT_EQ(test, 0x0e, tcode);
639 	KUNIT_EXPECT_EQ(test, 0x0c, sy);
640 
641 	serialize_isoc_header(&header, data_length, tag, channel, tcode, sy);
642 
643 	KUNIT_EXPECT_EQ(test, header, expected);
644 }
645 
646 static void test_phy_packet_self_id_zero_case0(struct kunit *test)
647 {
648 	// TSB41AB1/2 with 1 port.
649 	const u32 expected[] = {0x80458c80};
650 	u32 quadlets[] = {0};
651 
652 	unsigned int packet_identifier;
653 	unsigned int phy_id;
654 	bool extended;
655 	bool link_is_active;
656 	unsigned int gap_count;
657 	unsigned int scode;
658 	bool is_contender;
659 	unsigned int power_class;
660 	enum phy_packet_self_id_port_status port_status[3];
661 	bool is_initiated_reset;
662 	bool has_more_packets;
663 	unsigned int port_index;
664 
665 	deserialize_phy_packet_self_id_zero(expected[0], &packet_identifier, &phy_id, &extended,
666 					    &link_is_active, &gap_count, &scode, &is_contender,
667 					    &power_class, &is_initiated_reset, &has_more_packets);
668 
669 	KUNIT_EXPECT_EQ(test, PHY_PACKET_PACKET_IDENTIFIER_SELF_ID, packet_identifier);
670 	KUNIT_EXPECT_EQ(test, 0, phy_id);
671 	KUNIT_EXPECT_FALSE(test, extended);
672 	KUNIT_EXPECT_TRUE(test, link_is_active);
673 	KUNIT_EXPECT_EQ(test, 0x05, gap_count);
674 	KUNIT_EXPECT_EQ(test, SCODE_400, scode);
675 	KUNIT_EXPECT_TRUE(test, is_contender);
676 	KUNIT_EXPECT_EQ(test, 0x4, power_class);
677 	KUNIT_EXPECT_FALSE(test, is_initiated_reset);
678 	KUNIT_EXPECT_FALSE(test, has_more_packets);
679 
680 	serialize_phy_packet_self_id_zero(quadlets, packet_identifier, phy_id, extended,
681 					  link_is_active, gap_count, scode, is_contender,
682 					  power_class, is_initiated_reset, has_more_packets);
683 
684 	for (port_index = 0; port_index < ARRAY_SIZE(port_status); ++port_index) {
685 		port_status[port_index] =
686 			self_id_sequence_get_port_status(expected, ARRAY_SIZE(expected), port_index);
687 	}
688 
689 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[0]);
690 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NONE, port_status[1]);
691 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NONE, port_status[2]);
692 
693 	for (port_index = 0; port_index < ARRAY_SIZE(port_status); ++port_index) {
694 		self_id_sequence_set_port_status(quadlets, ARRAY_SIZE(quadlets), port_index,
695 						 port_status[port_index]);
696 	}
697 
698 	KUNIT_EXPECT_MEMEQ(test, quadlets, expected, sizeof(expected));
699 }
700 
701 static void test_phy_packet_self_id_zero_case1(struct kunit *test)
702 {
703 	// XIO2213 and TSB81BA3E with 3 ports.
704 	const u32 expected[] = {0x817fcc5e};
705 	u32 quadlets[] = {0};
706 
707 	unsigned int packet_identifier;
708 	unsigned int phy_id;
709 	bool extended;
710 	bool link_is_active;
711 	unsigned int gap_count;
712 	unsigned int scode;
713 	bool is_contender;
714 	unsigned int power_class;
715 	enum phy_packet_self_id_port_status port_status[3];
716 	bool is_initiated_reset;
717 	bool has_more_packets;
718 	unsigned int port_index;
719 
720 	deserialize_phy_packet_self_id_zero(expected[0], &packet_identifier, &phy_id, &extended,
721 					    &link_is_active, &gap_count, &scode, &is_contender,
722 					    &power_class, &is_initiated_reset, &has_more_packets);
723 
724 	KUNIT_EXPECT_EQ(test, PHY_PACKET_PACKET_IDENTIFIER_SELF_ID, packet_identifier);
725 	KUNIT_EXPECT_EQ(test, 1, phy_id);
726 	KUNIT_EXPECT_FALSE(test, extended);
727 	KUNIT_EXPECT_TRUE(test, link_is_active);
728 	KUNIT_EXPECT_EQ(test, 0x3f, gap_count);
729 	KUNIT_EXPECT_EQ(test, SCODE_800, scode);
730 	KUNIT_EXPECT_TRUE(test, is_contender);
731 	KUNIT_EXPECT_EQ(test, 0x4, power_class);
732 	KUNIT_EXPECT_TRUE(test, is_initiated_reset);
733 	KUNIT_EXPECT_FALSE(test, has_more_packets);
734 
735 	serialize_phy_packet_self_id_zero(quadlets, packet_identifier, phy_id, extended,
736 					  link_is_active, gap_count, scode, is_contender,
737 					  power_class, is_initiated_reset, has_more_packets);
738 
739 	for (port_index = 0; port_index < ARRAY_SIZE(port_status); ++port_index) {
740 		port_status[port_index] =
741 			self_id_sequence_get_port_status(expected, ARRAY_SIZE(expected), port_index);
742 	}
743 
744 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[0]);
745 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[1]);
746 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[2]);
747 
748 	for (port_index = 0; port_index < ARRAY_SIZE(port_status); ++port_index) {
749 		self_id_sequence_set_port_status(quadlets, ARRAY_SIZE(quadlets), port_index,
750 						 port_status[port_index]);
751 	}
752 
753 	KUNIT_EXPECT_MEMEQ(test, quadlets, expected, sizeof(expected));
754 }
755 
756 static void test_phy_packet_self_id_zero_and_one(struct kunit *test)
757 {
758 	// TSB41LV06A with 6 ports.
759 	const u32 expected[] = {
760 		0x803f8459,
761 		0x80815000,
762 	};
763 	u32 quadlets[] = {0, 0};
764 
765 	unsigned int packet_identifier;
766 	unsigned int phy_id;
767 	bool extended;
768 	bool link_is_active;
769 	unsigned int gap_count;
770 	unsigned int scode;
771 	bool is_contender;
772 	unsigned int power_class;
773 	enum phy_packet_self_id_port_status port_status[11];
774 	bool is_initiated_reset;
775 	bool has_more_packets;
776 
777 	unsigned int sequence;
778 	unsigned int port_index;
779 
780 	deserialize_phy_packet_self_id_zero(expected[0], &packet_identifier, &phy_id, &extended,
781 					    &link_is_active, &gap_count, &scode, &is_contender,
782 					    &power_class, &is_initiated_reset, &has_more_packets);
783 
784 	KUNIT_EXPECT_EQ(test, PHY_PACKET_PACKET_IDENTIFIER_SELF_ID, packet_identifier);
785 	KUNIT_EXPECT_EQ(test, 0, phy_id);
786 	KUNIT_EXPECT_FALSE(test, extended);
787 	KUNIT_EXPECT_FALSE(test, link_is_active);
788 	KUNIT_EXPECT_EQ(test, 0x3f, gap_count);
789 	KUNIT_EXPECT_EQ(test, SCODE_400, scode);
790 	KUNIT_EXPECT_FALSE(test, is_contender);
791 	KUNIT_EXPECT_EQ(test, 0x4, power_class);
792 	KUNIT_EXPECT_FALSE(test, is_initiated_reset);
793 	KUNIT_EXPECT_TRUE(test, has_more_packets);
794 
795 	serialize_phy_packet_self_id_zero(quadlets, packet_identifier, phy_id, extended,
796 					  link_is_active, gap_count, scode, is_contender,
797 					  power_class, is_initiated_reset, has_more_packets);
798 
799 	deserialize_phy_packet_self_id_extended(expected[1], &packet_identifier, &phy_id, &extended,
800 						&sequence, &has_more_packets);
801 
802 	KUNIT_EXPECT_EQ(test, PHY_PACKET_PACKET_IDENTIFIER_SELF_ID, packet_identifier);
803 	KUNIT_EXPECT_EQ(test, 0, phy_id);
804 	KUNIT_EXPECT_TRUE(test, extended);
805 	KUNIT_EXPECT_EQ(test, 0, sequence);
806 	KUNIT_EXPECT_FALSE(test, has_more_packets);
807 
808 	serialize_phy_packet_self_id_extended(&quadlets[1], packet_identifier, phy_id, extended,
809 					      sequence, has_more_packets);
810 
811 
812 	for (port_index = 0; port_index < ARRAY_SIZE(port_status); ++port_index) {
813 		port_status[port_index] =
814 			self_id_sequence_get_port_status(expected, ARRAY_SIZE(expected), port_index);
815 	}
816 
817 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[0]);
818 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[1]);
819 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[2]);
820 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[3]);
821 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[4]);
822 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[5]);
823 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NONE, port_status[6]);
824 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NONE, port_status[7]);
825 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NONE, port_status[8]);
826 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NONE, port_status[9]);
827 	KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NONE, port_status[10]);
828 
829 	for (port_index = 0; port_index < ARRAY_SIZE(port_status); ++port_index) {
830 		self_id_sequence_set_port_status(quadlets, ARRAY_SIZE(quadlets), port_index,
831 						 port_status[port_index]);
832 	}
833 
834 	KUNIT_EXPECT_MEMEQ(test, quadlets, expected, sizeof(expected));
835 }
836 
837 static void test_phy_packet_phy_config_force_root_node(struct kunit *test)
838 {
839 	const u32 expected = 0x02800000;
840 	u32 quadlet = 0;
841 
842 	unsigned int packet_identifier;
843 	unsigned int root_id;
844 	bool has_force_root_node;
845 	bool has_gap_count_optimization;
846 	unsigned int gap_count;
847 
848 	deserialize_phy_packet_phy_config(expected, &packet_identifier, &root_id,
849 					  &has_force_root_node, &has_gap_count_optimization,
850 					  &gap_count);
851 
852 	KUNIT_EXPECT_EQ(test, PHY_PACKET_PACKET_IDENTIFIER_PHY_CONFIG, packet_identifier);
853 	KUNIT_EXPECT_EQ(test, 0x02, root_id);
854 	KUNIT_EXPECT_TRUE(test, has_force_root_node);
855 	KUNIT_EXPECT_FALSE(test, has_gap_count_optimization);
856 	KUNIT_EXPECT_EQ(test, 0, gap_count);
857 
858 	serialize_phy_packet_phy_config(&quadlet, packet_identifier, root_id, has_force_root_node,
859 					has_gap_count_optimization, gap_count);
860 
861 	KUNIT_EXPECT_EQ(test, quadlet, expected);
862 }
863 
864 static void test_phy_packet_phy_config_gap_count_optimization(struct kunit *test)
865 {
866 	const u32 expected = 0x034f0000;
867 	u32 quadlet = 0;
868 
869 	unsigned int packet_identifier;
870 	unsigned int root_id;
871 	bool has_force_root_node;
872 	bool has_gap_count_optimization;
873 	unsigned int gap_count;
874 
875 	deserialize_phy_packet_phy_config(expected, &packet_identifier, &root_id,
876 					  &has_force_root_node, &has_gap_count_optimization,
877 					  &gap_count);
878 
879 	KUNIT_EXPECT_EQ(test, PHY_PACKET_PACKET_IDENTIFIER_PHY_CONFIG, packet_identifier);
880 	KUNIT_EXPECT_EQ(test, 0x03, root_id);
881 	KUNIT_EXPECT_FALSE(test, has_force_root_node);
882 	KUNIT_EXPECT_TRUE(test, has_gap_count_optimization);
883 	KUNIT_EXPECT_EQ(test, 0x0f, gap_count);
884 
885 	serialize_phy_packet_phy_config(&quadlet, packet_identifier, root_id, has_force_root_node,
886 					has_gap_count_optimization, gap_count);
887 
888 	KUNIT_EXPECT_EQ(test, quadlet, expected);
889 }
890 
891 static struct kunit_case packet_serdes_test_cases[] = {
892 	KUNIT_CASE(test_async_header_write_quadlet_request),
893 	KUNIT_CASE(test_async_header_write_block_request),
894 	KUNIT_CASE(test_async_header_write_response),
895 	KUNIT_CASE(test_async_header_read_quadlet_request),
896 	KUNIT_CASE(test_async_header_read_quadlet_response),
897 	KUNIT_CASE(test_async_header_read_block_request),
898 	KUNIT_CASE(test_async_header_read_block_response),
899 	KUNIT_CASE(test_async_header_lock_request),
900 	KUNIT_CASE(test_async_header_lock_response),
901 	KUNIT_CASE(test_isoc_header),
902 	KUNIT_CASE(test_phy_packet_self_id_zero_case0),
903 	KUNIT_CASE(test_phy_packet_self_id_zero_case1),
904 	KUNIT_CASE(test_phy_packet_self_id_zero_and_one),
905 	KUNIT_CASE(test_phy_packet_phy_config_force_root_node),
906 	KUNIT_CASE(test_phy_packet_phy_config_gap_count_optimization),
907 	{}
908 };
909 
910 static struct kunit_suite packet_serdes_test_suite = {
911 	.name = "firewire-packet-serdes",
912 	.test_cases = packet_serdes_test_cases,
913 };
914 kunit_test_suite(packet_serdes_test_suite);
915 
916 MODULE_DESCRIPTION("FireWire packet serialization/deserialization unit test suite");
917 MODULE_LICENSE("GPL");
918