blob: 063ab9b5f256bcd391c16c69c7ef76f4b07fe5d3 [file] [log] [blame]
Lawrence Tangde9707f2022-07-19 10:54:31 +01001/**
Lawrence Tangefe17e22022-08-08 09:16:23 +01002 * Functions for generating pseudo-random CPER PCIe error sections.
Ed Tanousfedd4572024-07-12 13:56:00 -07003 *
Lawrence Tangde9707f2022-07-19 10:54:31 +01004 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdlib.h>
Thu Nguyene42fb482024-10-15 14:43:11 +00008#include <libcper/BaseTypes.h>
9#include <libcper/generator/gen-utils.h>
10#include <libcper/generator/sections/gen-section.h>
Lawrence Tangde9707f2022-07-19 10:54:31 +010011
Patrick Williams30cd66a2024-12-18 11:23:15 -050012#define PCIE_PORT_TYPES (int[]){ 0, 1, 4, 5, 6, 7, 8, 9, 10 }
Lawrence Tangde9707f2022-07-19 10:54:31 +010013
Lawrence Tangefe17e22022-08-08 09:16:23 +010014//Generates a single pseudo-random PCIe error section, saving the resulting address to the given
Lawrence Tangde9707f2022-07-19 10:54:31 +010015//location. Returns the size of the newly created section.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080016size_t generate_section_pcie(void **location,
17 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangde9707f2022-07-19 10:54:31 +010018{
John Chungf8fc7052024-05-03 20:05:29 +080019 //Create random bytes.
20 int size = 208;
21 UINT8 *bytes = generate_random_bytes(size);
Lawrence Tangde9707f2022-07-19 10:54:31 +010022
John Chungf8fc7052024-05-03 20:05:29 +080023 //Set reserved areas to zero.
24 UINT64 *validation = (UINT64 *)bytes;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080025 *validation &= 0xFF; //Validation 8-63
26 if (validBitsType == ALL_VALID) {
27 *validation = 0xFF;
28 } else if (validBitsType == SOME_VALID) {
Erwin Tsaur8870c072025-02-28 12:57:12 -080029 /*
30 * Valid Sections
31 * 0b00000001 : Port Type
32 * 0b00000100 : Command Status Valid
33 * 0b00010000 : Device Serial Number Valid
34 * 0b01000000 : Capability Structure Valid
35 * 0b10000000 : AER Info Valid
36 */
37 *validation = 0xD5;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080038 }
John Chungf8fc7052024-05-03 20:05:29 +080039 UINT32 *version = (UINT32 *)(bytes + 12);
40 *version &= 0xFFFF; //Version bytes 2-3
41 UINT32 *reserved = (UINT32 *)(bytes + 20);
42 *reserved = 0; //Reserved bytes 20-24
43 *(bytes + 37) &= ~0x7; //Device ID byte 13 bits 0-3
44 *(bytes + 39) = 0; //Device ID byte 15
Lawrence Tang3ab351f2022-07-20 16:09:34 +010045
John Chungf8fc7052024-05-03 20:05:29 +080046 //Set expected values.
Ed Tanous2d4d3b62025-03-11 10:34:29 -070047 int minor = cper_rand() % 128;
48 int major = cper_rand() % 128;
John Chungf8fc7052024-05-03 20:05:29 +080049 *version = int_to_bcd(minor);
50 *version |= int_to_bcd(major) << 8;
Lawrence Tangde9707f2022-07-19 10:54:31 +010051
John Chungf8fc7052024-05-03 20:05:29 +080052 //Fix values that could be above range.
53 UINT32 *port_type = (UINT32 *)(bytes + 8);
Ed Tanous2d4d3b62025-03-11 10:34:29 -070054 *port_type = PCIE_PORT_TYPES[cper_rand() %
John Chungf8fc7052024-05-03 20:05:29 +080055 (sizeof(PCIE_PORT_TYPES) / sizeof(int))];
56
57 //Set return values, exit.
58 *location = bytes;
59 return size;
60}