net/ice/base: fix build with GCC 12
[dpdk.git] / drivers / net / ice / ice_ddp_package.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2022 Intel Corporation
3  */
4
5 #include <rte_string_fns.h>
6 #include <rte_malloc.h>
7 #include <rte_tailq.h>
8
9 #include "ice_ethdev.h"
10 #include "rte_pmd_ice.h"
11
12 #define ICE_BUFF_SEG_HEADER_FLAG   0x1
13 #define ICE_PKG_HDR_HEADR_PART1    1
14 #define ICE_PKG_HDR_HEADR_PART2    2
15 #define ICE_PKG_HDR_GM_SEG_OFFSET  16
16 #define ICE_PKG_HDR_ICE_SEG_OFFSET 100
17 #define ICE_PKG_GM_SEG_TYPE        1
18 #define ICE_PKG_MAJOR_VERSION      1
19 #define ICE_PKG_GM_SEG_SIZE        84
20 #define ICE_PKG_ICE_SEG_TYPE       0x10
21 #define ICE_PKG_ICE_SEG_SIZE_BASE  56
22 #define SPACE_CHAR                 0x20
23
24 #define ICE_PKG_COPY_STRING(dst, src)   \
25         do {\
26                 char *_dst = (dst); \
27                 const char *_src = (src); \
28                 memset(_dst, SPACE_CHAR, ICE_PKG_NAME_SIZE); \
29                 strlcpy(_dst, _src, strlen(_dst)); \
30         } while (0)
31
32 /* Package header */
33 struct ice_package_header {
34         struct __hdr {
35                 uint32_t h1; /* header part 1 */
36                 uint32_t h2; /* header part 2 */
37         } header;
38         uint32_t gm_seg_offset;  /* Global Metadata segment: 16 */
39         uint32_t ice_seg_offset; /* ICE segment: 100 */
40         struct ice_global_metadata_seg gm_seg;
41         struct __ice_seg {
42                 struct ice_generic_seg_hdr hdr;
43                 uint32_t devid_count;
44                 struct ice_pkg_ver nvm_ver;
45         } ice_seg;
46
47         uint32_t buff_count;
48 };
49
50 struct ice_buff_seg_header {
51         __le16 flag;
52         __le16 length;
53         __le16 type;
54         __le16 reserve;         /* 0 */
55         __le16 header_len;      /* 0x0C */
56         __le16 data_size;       /* length - header_len */
57 };
58
59 struct ice_buff_seg_simple {
60         struct ice_buff_seg_header header;
61         __le16 seg_end;
62 };
63
64 struct ice_buff_seg_simple_data {
65         __le16 type;
66         __le32 addr;
67         __le16 len;
68         __le16 seg_end;
69 };
70
71 struct ice_buff_seg_series {
72         struct ice_buff_seg_header header;
73         uint16_t offset_delta;
74         uint16_t offset[2];
75 };
76
77 struct ice_buff_seg_series_data {
78         __le16 type;
79         __le32 begin_addr;
80         __le16 len;
81         __le32 end_addr;
82         __le16 last_len;
83         __le16 offset_delta;
84         __le16 seg_end;
85         uint8_t padding;
86 };
87
88 struct ice_buff_seg_series_with_sub {
89         struct ice_buff_seg_header header;
90         uint16_t sub_block_num;
91 };
92
93 struct ice_buff_seg_series_with_sub_data {
94         __le16 type;
95         __le32 begin_addr;
96         __le16 len;
97         __le32 end_addr;
98         __le16 last_len;
99         __le16 sblk_size;
100 };
101
102
103 static const
104 uint16_t ice_buff_seg_header_size = sizeof(struct ice_buff_seg_header);
105
106 static void
107 write_buffer_simple(uint8_t **buff)
108 {
109         uint16_t i;
110         /* ICE ddp package simple segment template */
111         const struct ice_buff_seg_simple_data buff_data[] = {
112             {0x0001, 0x00000, 0x0030, 0x0000},
113             {0x000a, 0x01000, 0x0810, 0x0800},
114             {0x000b, 0x02000, 0x00d8, 0x0000},
115             {0x000d, 0x06000, 0x0810, 0x0400},
116             {0x000f, 0x09000, 0x0110, 0x0100},
117             {0x0011, 0x17000, 0x001d, 0x0000},
118             {0x0012, 0x18000, 0x0014, 0x0000},
119             {0x0014, 0x19000, 0x0810, 0x0800},
120             {0x0015, 0x1a000, 0x00d8, 0x0000},
121             {0x0017, 0x1e000, 0x0810, 0x0400},
122             {0x0019, 0x21000, 0x0090, 0x0080},
123             {0x001b, 0x27000, 0x001d, 0x0000},
124             {0x001c, 0x28000, 0x0014, 0x0000},
125             {0x001e, 0x29000, 0x0810, 0x0800},
126             {0x001f, 0x2a000, 0x00d8, 0x0000},
127             {0x0021, 0x2e000, 0x0810, 0x0400},
128             {0x0023, 0x31000, 0x0090, 0x0080},
129             {0x0025, 0x36000, 0x001d, 0x0000},
130             {0x0026, 0x37000, 0x0014, 0x0000},
131             {0x0028, 0x38000, 0x0810, 0x0800},
132             {0x0029, 0x39000, 0x00d8, 0x0000},
133             {0x002b, 0x3d000, 0x0810, 0x0400},
134             {0x002d, 0x40000, 0x0090, 0x0080},
135             {0x002f, 0x45000, 0x001d, 0x0000},
136             {0x0030, 0x46000, 0x0014, 0x0000},
137             {0x0035, 0x57000, 0x0010, 0x0000},
138             {0x003a, 0x67000, 0x0190, 0x0010},
139             {0x003b, 0x68000, 0x0810, 0x0800},
140             {0x003f, 0x79000, 0x0010, 0x0000},
141             {0x0044, 0x89000, 0x0190, 0x0010},
142             {0x0045, 0x8a000, 0x0810, 0x0800},
143             {0x0046, 0x8b000, 0x001c, 0x0000},
144             {0x0047, 0x8c000, 0x001c, 0x0000},
145             {0x0048, 0x8d000, 0x0410, 0x0080},
146             {0x0049, 0x8e000, 0x0410, 0x0080},
147             {0x004a, 0x8f000, 0x0028, 0x0006},
148             {0x004b, 0x90000, 0x0028, 0x0006},
149             {0x004c, 0x91000, 0x0890, 0x0080},
150             {0x004d, 0x92000, 0x0890, 0x0080},
151             {0x004e, 0x93000, 0x0350, 0x0040},
152             {0x004f, 0x94000, 0x0350, 0x0040},
153             {0x0050, 0x95000, 0x0810, 0x0800},
154             {0x0051, 0x96000, 0x00d8, 0x0000},
155             {0x0053, 0x9a000, 0x0810, 0x0400},
156             {0x0055, 0x9c000, 0x0030, 0x0020},
157             {0x0057, 0x9f000, 0x001d, 0x0000},
158             {0x0058, 0xa0000, 0x0014, 0x0000},
159             {0x005a, 0xa1000, 0x0024, 0x0000},
160             {0x005b, 0xa2000, 0x0024, 0x0000},
161             {0x005d, 0xa4000, 0x0810, 0x0100},
162             {0x020d, 0xa8000, 0x0414, 0x0400},
163             {0x020e, 0xa9000, 0x0214, 0x0200},
164             {0x020f, 0xaa000, 0x0114, 0x0100},
165             {0x0210, 0xab000, 0x0114, 0x0100},
166             {0x0217, 0xaf000, 0x0414, 0x0400},
167             {0x0218, 0xb0000, 0x0214, 0x0200},
168             {0x0219, 0xb1000, 0x0094, 0x0080},
169             {0x021a, 0xb2000, 0x0094, 0x0080},
170             {0x0221, 0xb6000, 0x0414, 0x0400},
171             {0x0222, 0xb7000, 0x0214, 0x0200},
172             {0x0223, 0xb8000, 0x0094, 0x0080},
173             {0x0224, 0xb9000, 0x0094, 0x0080},
174             {0x022b, 0xbd000, 0x0414, 0x0400},
175             {0x022c, 0xbe000, 0x0214, 0x0200},
176             {0x022d, 0xbf000, 0x0094, 0x0080},
177             {0x022e, 0xc0000, 0x0094, 0x0080},
178             {0x0238, 0xc1000, 0x0114, 0x0100},
179             {0x0253, 0xc5000, 0x0414, 0x0400},
180             {0x0254, 0xc6000, 0x0054, 0x0040},
181             {0x0255, 0xc7000, 0x0034, 0x0020},
182             {0x0256, 0xc8000, 0x0034, 0x0020},
183         };
184
185         for (i = 0; i < ARRAY_SIZE(buff_data); i++) {
186                 const struct ice_buff_seg_simple_data *seg = &buff_data[i];
187                 struct ice_buff_seg_simple buff_seg;
188                 uint8_t *buffer = &(*buff)[seg->addr];
189
190                 memset(buffer, 0xFF, ICE_PKG_BUF_SIZE);
191                 buff_seg.header.flag = ICE_BUFF_SEG_HEADER_FLAG;
192                 buff_seg.header.length = seg->len;
193                 buff_seg.header.type = seg->type;
194                 buff_seg.header.reserve = 0x0;
195                 buff_seg.header.header_len =
196                         sizeof(struct ice_buff_seg_header);
197                 buff_seg.header.data_size =
198                         buff_seg.header.length - buff_seg.header.header_len;
199                 buff_seg.seg_end = seg->seg_end;
200
201                 memset(buffer, 0x00, buff_seg.header.length);
202                 memcpy(buffer, &buff_seg, sizeof(struct ice_buff_seg_simple));
203         }
204 }
205
206 static void
207 write_buffer_block(uint8_t **buff)
208 {
209         uint16_t i;
210         /* ICE ddp package multiple segments template 1 */
211         const struct ice_buff_seg_series_data buff_data[] = {
212                 {0x000c, 0x03000, 0x1000, 0x05000, 0x0030, 0x0ff0, 0x0020, 0},
213                 {0x0010, 0x0a000, 0x0fd0, 0x16000, 0x0310, 0x0015, 0x0004, 0},
214                 {0x0016, 0x1b000, 0x1000, 0x1d000, 0x0030, 0x0ff0, 0x0020, 0},
215                 {0x001a, 0x22000, 0x0f90, 0x26000, 0x0210, 0x001f, 0x0004, 0},
216                 {0x0020, 0x2b000, 0x1000, 0x2d000, 0x0030, 0x0ff0, 0x0020, 0},
217                 {0x0024, 0x32000, 0x0fd0, 0x35000, 0x00d0, 0x002a, 0x0002, 0},
218                 {0x002a, 0x3a000, 0x1000, 0x3c000, 0x0030, 0x0ff0, 0x0020, 0},
219                 {0x002e, 0x41000, 0x0fd0, 0x44000, 0x00d0, 0x002a, 0x0002, 0},
220                 {0x0032, 0x47000, 0x1000, 0x4f000, 0x0090, 0x00ff, 0x0008, 0},
221                 {0x0033, 0x50000, 0x1000, 0x53000, 0x0040, 0x0154, 0x0004, 0},
222                 {0x0034, 0x54000, 0x1000, 0x56000, 0x0430, 0x0055, 0x0016, 0},
223                 {0x0039, 0x65000, 0x1000, 0x66000, 0x0220, 0x00aa, 0x0016, 0},
224                 {0x003c, 0x69000, 0x1000, 0x71000, 0x0090, 0x00ff, 0x0008, 0},
225                 {0x003d, 0x72000, 0x1000, 0x75000, 0x0040, 0x0154, 0x0004, 0},
226                 {0x003e, 0x76000, 0x1000, 0x78000, 0x0430, 0x0055, 0x0016, 0},
227                 {0x0043, 0x87000, 0x1000, 0x88000, 0x0220, 0x00aa, 0x0016, 0},
228                 {0x0052, 0x97000, 0x1000, 0x99000, 0x0030, 0x0ff0, 0x0020, 0},
229                 {0x0056, 0x9d000, 0x0f90, 0x9e000, 0x0090, 0x001f, 0x0001, 0},
230                 {0x020c, 0xa5000, 0x1000, 0xa7000, 0x003c, 0x0fec, 0x0028, 1},
231                 {0x0216, 0xac000, 0x1000, 0xae000, 0x003c, 0x0fec, 0x0028, 1},
232                 {0x0220, 0xb3000, 0x1000, 0xb5000, 0x003c, 0x0fec, 0x0028, 1},
233                 {0x022a, 0xba000, 0x1000, 0xbc000, 0x003c, 0x0fec, 0x0028, 1},
234                 {0x0252, 0xc2000, 0x1000, 0xc4000, 0x003c, 0x0fec, 0x0028, 1},
235         };
236
237         for (i = 0; i < ARRAY_SIZE(buff_data); i++) {
238                 const struct ice_buff_seg_series_data *seg = &buff_data[i];
239                 struct ice_buff_seg_series buff_seg;
240                 const uint16_t buff_seg_size =
241                         sizeof(struct ice_buff_seg_series);
242                 uint32_t addr = seg->begin_addr;
243                 __le16 last_offset = 0;
244
245                 for (; addr <= seg->end_addr; addr += ICE_PKG_BUF_SIZE) {
246                         uint8_t *buffer = &(*buff)[addr];
247
248                         memset(buffer, 0xFF, ICE_PKG_BUF_SIZE);
249                         buff_seg.header.flag = ICE_BUFF_SEG_HEADER_FLAG;
250                         buff_seg.header.length = addr == seg->end_addr ?
251                                                 seg->last_len : seg->len;
252                         buff_seg.header.type = seg->type;
253                         buff_seg.header.reserve = 0x0;
254                         buff_seg.header.header_len = ice_buff_seg_header_size;
255                         buff_seg.header.data_size = buff_seg.header.length -
256                                                 buff_seg.header.header_len;
257                         buff_seg.offset_delta =  addr < seg->end_addr ?
258                                 seg->offset_delta : seg->seg_end;
259                         buff_seg.offset[!seg->padding] = 0x0;
260                         buff_seg.offset[seg->padding] = last_offset;
261
262                         memset(buffer, 0x00, buff_seg.header.length);
263                         memcpy(buffer, &buff_seg, buff_seg_size);
264
265                         last_offset += seg->offset_delta;
266                 }
267         }
268 }
269
270 static void
271 write_buffer_block2(uint8_t **buff)
272 {
273         uint16_t i;
274         /* ICE ddp package multiple segments template 2 */
275         struct ice_buff_seg_series_with_sub_data buff_data[] = {
276                 {0x000e, 0x07000, 0x1000, 0x08000, 0x0a1c, 13},
277                 {0x0018, 0x1f000, 0x1000, 0x20000, 0x0a1c, 13},
278                 {0x0022, 0x2f000, 0x1000, 0x30000, 0x0a1c, 13},
279                 {0x002c, 0x3e000, 0x1000, 0x3f000, 0x0a1c, 13},
280                 {0x0037, 0x58000, 0x1000, 0x5e000, 0x0070, 24},
281                 {0x0038, 0x5f000, 0x0fe0, 0x64000, 0x0900, 88},
282                 {0x0041, 0x7a000, 0x1000, 0x80000, 0x0070, 24},
283                 {0x0042, 0x81000, 0x0fe0, 0x86000, 0x0900, 88},
284                 {0x0054, 0x9b000, 0x034e, 0x9b000, 0x034e, 13},
285                 {0x005c, 0xa3000, 0x0a10, 0xa3000, 0x0a10, 40},
286         };
287
288         for (i = 0; i < ARRAY_SIZE(buff_data); i++) {
289                 struct ice_buff_seg_series_with_sub_data *seg = &buff_data[i];
290                 struct ice_buff_seg_series_with_sub buff_seg;
291                 const uint16_t buff_seg_size =
292                         sizeof(struct ice_buff_seg_series_with_sub);
293                 uint32_t addr;
294                 uint16_t last_idx = 0;
295
296                 for (addr = seg->begin_addr;
297                      addr <= seg->end_addr; addr += ICE_PKG_BUF_SIZE) {
298                         uint8_t *buffer = &(*buff)[addr];
299                         uint16_t total_sblk_size;
300                         uint16_t idx = 0;
301                         uint32_t pos = buff_seg_size;
302
303                         memset(buffer, 0xFF, ICE_PKG_BUF_SIZE);
304                         buff_seg.header.flag = ICE_BUFF_SEG_HEADER_FLAG;
305                         buff_seg.header.length =
306                                 addr == seg->end_addr ?
307                                         seg->last_len : seg->len;
308                         buff_seg.header.type = seg->type;
309                         buff_seg.header.reserve = 0x0;
310                         buff_seg.header.header_len = ice_buff_seg_header_size;
311                         buff_seg.header.data_size = buff_seg.header.length -
312                                         buff_seg.header.header_len;
313
314                         total_sblk_size = buff_seg.header.data_size
315                                           - sizeof(buff_seg.sub_block_num);
316                         buff_seg.sub_block_num =
317                                         total_sblk_size / seg->sblk_size;
318
319                         memset(buffer, 0x00, buff_seg.header.length);
320                         memcpy(buffer, &buff_seg, buff_seg_size);
321
322                         /* padding if needed */
323                         if (total_sblk_size % seg->sblk_size)
324                                 pos += sizeof(uint16_t);
325
326                         for (idx = last_idx;
327                              idx < last_idx + buff_seg.sub_block_num; idx++) {
328                                 memcpy(buffer + pos, &idx, sizeof(uint16_t));
329                                 pos += seg->sblk_size;
330                         }
331
332                         last_idx = idx;
333                 }
334         }
335 }
336
337 static int
338 ice_dump_pkg(struct rte_eth_dev *dev, uint8_t **buff, uint32_t *size)
339 {
340         struct ice_hw *hw;
341         struct ice_buf pkg_buff;
342         uint8_t *next_buff;
343         uint16_t i = 0;
344         uint16_t count;
345         struct ice_package_header *cache;
346         uint32_t cache_size;
347
348         write_buffer_simple(buff);
349         write_buffer_block(buff);
350         write_buffer_block2(buff);
351
352         hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
353
354         if (*size % ICE_PKG_BUF_SIZE)
355                 return -EINVAL;
356
357         count = *size / ICE_PKG_BUF_SIZE;
358         for (i = 0; i < count; i++) {
359                 next_buff = (uint8_t *)(*buff) + i * ICE_PKG_BUF_SIZE;
360                 rte_memcpy(pkg_buff.buf, next_buff, ICE_PKG_BUF_SIZE);
361                 if (ice_aq_upload_section(hw,
362                                           (struct ice_buf_hdr *)&pkg_buff.buf[0],
363                                           ICE_PKG_BUF_SIZE,
364                                           NULL))
365                         return -EINVAL;
366                 rte_memcpy(next_buff, pkg_buff.buf, ICE_PKG_BUF_SIZE);
367         }
368
369         cache_size = sizeof(struct ice_package_header) + *size;
370         cache = (struct ice_package_header *)malloc(cache_size);
371         if (!cache)
372                 return -ENOSPC;
373
374         cache->header.h1 = ICE_PKG_HDR_HEADR_PART1;
375         cache->header.h2 = ICE_PKG_HDR_HEADR_PART2;
376         cache->gm_seg_offset = ICE_PKG_HDR_GM_SEG_OFFSET;
377         cache->ice_seg_offset = ICE_PKG_HDR_ICE_SEG_OFFSET;
378         cache->gm_seg.hdr.seg_type = ICE_PKG_GM_SEG_TYPE;
379         cache->gm_seg.hdr.seg_format_ver.major = ICE_PKG_MAJOR_VERSION;
380         cache->gm_seg.hdr.seg_size = ICE_PKG_GM_SEG_SIZE;
381         ICE_PKG_COPY_STRING(cache->gm_seg.hdr.seg_id, "Global Metadata");
382
383         cache->gm_seg.pkg_ver.major = ICE_PKG_MAJOR_VERSION;
384         cache->gm_seg.rsvd = 1;
385         ICE_PKG_COPY_STRING(cache->gm_seg.pkg_name, "DEFAULT");
386
387         cache->ice_seg.hdr.seg_type = ICE_PKG_ICE_SEG_TYPE;
388         cache->ice_seg.hdr.seg_format_ver.major = ICE_PKG_MAJOR_VERSION;
389         cache->ice_seg.hdr.seg_size = ICE_PKG_ICE_SEG_SIZE_BASE + *size;
390         cache->ice_seg.devid_count = 0;
391         cache->ice_seg.nvm_ver.major = 0;
392         ICE_PKG_COPY_STRING(cache->ice_seg.hdr.seg_id, "CPK Configuration Data");
393
394         cache->buff_count = count;
395
396         next_buff = (uint8_t *)cache;
397         next_buff += sizeof(struct ice_package_header);
398         memcpy(next_buff, *buff, *size);
399
400         free(*buff);
401         *buff = (uint8_t *)cache;
402         *size = cache_size;
403
404         return 0;
405 }
406
407 int rte_pmd_ice_dump_package(uint16_t port, uint8_t **buff, uint32_t *size)
408 {
409         struct rte_eth_dev *dev;
410
411         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
412
413         dev = &rte_eth_devices[port];
414         if (!is_ice_supported(dev))
415                 return -ENOTSUP;
416
417         return ice_dump_pkg(dev, buff, size);
418 }