blob: 9d0d31da426b6e50053ad908c9d72b20950d745f [file] [log] [blame]
Jacob Keller86641092016-04-07 08:21:21 -07001/* Intel(R) Ethernet Switch Host Interface Driver
Jacob Keller363656e2018-01-16 11:20:51 -08002 * Copyright(c) 2013 - 2018 Intel Corporation.
Alexander Duyckae17db0ee2014-09-20 19:46:30 -04003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
15 *
16 * Contact Information:
17 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19 */
20
21#include "fm10k_tlv.h"
22
23/**
24 * fm10k_tlv_msg_init - Initialize message block for TLV data storage
25 * @msg: Pointer to message block
26 * @msg_id: Message ID indicating message type
27 *
28 * This function return success if provided with a valid message pointer
29 **/
30s32 fm10k_tlv_msg_init(u32 *msg, u16 msg_id)
31{
32 /* verify pointer is not NULL */
33 if (!msg)
34 return FM10K_ERR_PARAM;
35
36 *msg = (FM10K_TLV_FLAGS_MSG << FM10K_TLV_FLAGS_SHIFT) | msg_id;
37
38 return 0;
39}
40
41/**
42 * fm10k_tlv_attr_put_null_string - Place null terminated string on message
43 * @msg: Pointer to message block
44 * @attr_id: Attribute ID
45 * @string: Pointer to string to be stored in attribute
46 *
47 * This function will reorder a string to be CPU endian and store it in
48 * the attribute buffer. It will return success if provided with a valid
49 * pointers.
50 **/
Bruce Allanbb269e82015-10-28 17:19:51 -070051static s32 fm10k_tlv_attr_put_null_string(u32 *msg, u16 attr_id,
52 const unsigned char *string)
Alexander Duyckae17db0ee2014-09-20 19:46:30 -040053{
54 u32 attr_data = 0, len = 0;
55 u32 *attr;
56
57 /* verify pointers are not NULL */
58 if (!string || !msg)
59 return FM10K_ERR_PARAM;
60
61 attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
62
63 /* copy string into local variable and then write to msg */
64 do {
65 /* write data to message */
66 if (len && !(len % 4)) {
67 attr[len / 4] = attr_data;
68 attr_data = 0;
69 }
70
71 /* record character to offset location */
72 attr_data |= (u32)(*string) << (8 * (len % 4));
73 len++;
74
75 /* test for NULL and then increment */
76 } while (*(string++));
77
78 /* write last piece of data to message */
79 attr[(len + 3) / 4] = attr_data;
80
81 /* record attribute header, update message length */
82 len <<= FM10K_TLV_LEN_SHIFT;
83 attr[0] = len | attr_id;
84
85 /* add header length to length */
86 len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
87 *msg += FM10K_TLV_LEN_ALIGN(len);
88
89 return 0;
90}
91
92/**
93 * fm10k_tlv_attr_get_null_string - Get null terminated string from attribute
94 * @attr: Pointer to attribute
95 * @string: Pointer to location of destination string
96 *
97 * This function pulls the string back out of the attribute and will place
98 * it in the array pointed by by string. It will return success if provided
99 * with a valid pointers.
100 **/
Bruce Allanbb269e82015-10-28 17:19:51 -0700101static s32 fm10k_tlv_attr_get_null_string(u32 *attr, unsigned char *string)
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400102{
103 u32 len;
104
105 /* verify pointers are not NULL */
106 if (!string || !attr)
107 return FM10K_ERR_PARAM;
108
109 len = *attr >> FM10K_TLV_LEN_SHIFT;
110 attr++;
111
112 while (len--)
113 string[len] = (u8)(attr[len / 4] >> (8 * (len % 4)));
114
115 return 0;
116}
117
118/**
119 * fm10k_tlv_attr_put_mac_vlan - Store MAC/VLAN attribute in message
120 * @msg: Pointer to message block
121 * @attr_id: Attribute ID
122 * @mac_addr: MAC address to be stored
Jacob Keller363656e2018-01-16 11:20:51 -0800123 * @vlan: VLAN to be stored
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400124 *
125 * This function will reorder a MAC address to be CPU endian and store it
126 * in the attribute buffer. It will return success if provided with a
127 * valid pointers.
128 **/
129s32 fm10k_tlv_attr_put_mac_vlan(u32 *msg, u16 attr_id,
130 const u8 *mac_addr, u16 vlan)
131{
132 u32 len = ETH_ALEN << FM10K_TLV_LEN_SHIFT;
133 u32 *attr;
134
135 /* verify pointers are not NULL */
136 if (!msg || !mac_addr)
137 return FM10K_ERR_PARAM;
138
139 attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
140
141 /* record attribute header, update message length */
142 attr[0] = len | attr_id;
143
144 /* copy value into local variable and then write to msg */
145 attr[1] = le32_to_cpu(*(const __le32 *)&mac_addr[0]);
146 attr[2] = le16_to_cpu(*(const __le16 *)&mac_addr[4]);
147 attr[2] |= (u32)vlan << 16;
148
149 /* add header length to length */
150 len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
151 *msg += FM10K_TLV_LEN_ALIGN(len);
152
153 return 0;
154}
155
156/**
157 * fm10k_tlv_attr_get_mac_vlan - Get MAC/VLAN stored in attribute
158 * @attr: Pointer to attribute
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400159 * @mac_addr: location of buffer to store MAC address
Jacob Keller363656e2018-01-16 11:20:51 -0800160 * @vlan: location of buffer to store VLAN
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400161 *
162 * This function pulls the MAC address back out of the attribute and will
163 * place it in the array pointed by by mac_addr. It will return success
164 * if provided with a valid pointers.
165 **/
166s32 fm10k_tlv_attr_get_mac_vlan(u32 *attr, u8 *mac_addr, u16 *vlan)
167{
168 /* verify pointers are not NULL */
169 if (!mac_addr || !attr)
170 return FM10K_ERR_PARAM;
171
172 *(__le32 *)&mac_addr[0] = cpu_to_le32(attr[1]);
173 *(__le16 *)&mac_addr[4] = cpu_to_le16((u16)(attr[2]));
174 *vlan = (u16)(attr[2] >> 16);
175
176 return 0;
177}
178
179/**
180 * fm10k_tlv_attr_put_bool - Add header indicating value "true"
181 * @msg: Pointer to message block
182 * @attr_id: Attribute ID
183 *
184 * This function will simply add an attribute header, the fact
185 * that the header is here means the attribute value is true, else
186 * it is false. The function will return success if provided with a
187 * valid pointers.
188 **/
189s32 fm10k_tlv_attr_put_bool(u32 *msg, u16 attr_id)
190{
191 /* verify pointers are not NULL */
192 if (!msg)
193 return FM10K_ERR_PARAM;
194
195 /* record attribute header */
196 msg[FM10K_TLV_DWORD_LEN(*msg)] = attr_id;
197
198 /* add header length to length */
199 *msg += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
200
201 return 0;
202}
203
204/**
205 * fm10k_tlv_attr_put_value - Store integer value attribute in message
206 * @msg: Pointer to message block
207 * @attr_id: Attribute ID
208 * @value: Value to be written
209 * @len: Size of value
210 *
211 * This function will place an integer value of up to 8 bytes in size
212 * in a message attribute. The function will return success provided
213 * that msg is a valid pointer, and len is 1, 2, 4, or 8.
214 **/
215s32 fm10k_tlv_attr_put_value(u32 *msg, u16 attr_id, s64 value, u32 len)
216{
217 u32 *attr;
218
219 /* verify non-null msg and len is 1, 2, 4, or 8 */
220 if (!msg || !len || len > 8 || (len & (len - 1)))
221 return FM10K_ERR_PARAM;
222
223 attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
224
225 if (len < 4) {
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800226 attr[1] = (u32)value & (BIT(8 * len) - 1);
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400227 } else {
228 attr[1] = (u32)value;
229 if (len > 4)
230 attr[2] = (u32)(value >> 32);
231 }
232
233 /* record attribute header, update message length */
234 len <<= FM10K_TLV_LEN_SHIFT;
235 attr[0] = len | attr_id;
236
237 /* add header length to length */
238 len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
239 *msg += FM10K_TLV_LEN_ALIGN(len);
240
241 return 0;
242}
243
244/**
245 * fm10k_tlv_attr_get_value - Get integer value stored in attribute
246 * @attr: Pointer to attribute
247 * @value: Pointer to destination buffer
248 * @len: Size of value
249 *
250 * This function will place an integer value of up to 8 bytes in size
251 * in the offset pointed to by value. The function will return success
252 * provided that pointers are valid and the len value matches the
253 * attribute length.
254 **/
255s32 fm10k_tlv_attr_get_value(u32 *attr, void *value, u32 len)
256{
257 /* verify pointers are not NULL */
258 if (!attr || !value)
259 return FM10K_ERR_PARAM;
260
261 if ((*attr >> FM10K_TLV_LEN_SHIFT) != len)
262 return FM10K_ERR_PARAM;
263
264 if (len == 8)
265 *(u64 *)value = ((u64)attr[2] << 32) | attr[1];
266 else if (len == 4)
267 *(u32 *)value = attr[1];
268 else if (len == 2)
269 *(u16 *)value = (u16)attr[1];
270 else
271 *(u8 *)value = (u8)attr[1];
272
273 return 0;
274}
275
276/**
277 * fm10k_tlv_attr_put_le_struct - Store little endian structure in message
278 * @msg: Pointer to message block
279 * @attr_id: Attribute ID
280 * @le_struct: Pointer to structure to be written
281 * @len: Size of le_struct
282 *
283 * This function will place a little endian structure value in a message
284 * attribute. The function will return success provided that all pointers
285 * are valid and length is a non-zero multiple of 4.
286 **/
287s32 fm10k_tlv_attr_put_le_struct(u32 *msg, u16 attr_id,
288 const void *le_struct, u32 len)
289{
290 const __le32 *le32_ptr = (const __le32 *)le_struct;
291 u32 *attr;
292 u32 i;
293
294 /* verify non-null msg and len is in 32 bit words */
295 if (!msg || !len || (len % 4))
296 return FM10K_ERR_PARAM;
297
298 attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
299
300 /* copy le32 structure into host byte order at 32b boundaries */
301 for (i = 0; i < (len / 4); i++)
302 attr[i + 1] = le32_to_cpu(le32_ptr[i]);
303
304 /* record attribute header, update message length */
305 len <<= FM10K_TLV_LEN_SHIFT;
306 attr[0] = len | attr_id;
307
308 /* add header length to length */
309 len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
310 *msg += FM10K_TLV_LEN_ALIGN(len);
311
312 return 0;
313}
314
315/**
316 * fm10k_tlv_attr_get_le_struct - Get little endian struct form attribute
317 * @attr: Pointer to attribute
318 * @le_struct: Pointer to structure to be written
319 * @len: Size of structure
320 *
321 * This function will place a little endian structure in the buffer
322 * pointed to by le_struct. The function will return success
323 * provided that pointers are valid and the len value matches the
324 * attribute length.
325 **/
326s32 fm10k_tlv_attr_get_le_struct(u32 *attr, void *le_struct, u32 len)
327{
328 __le32 *le32_ptr = (__le32 *)le_struct;
329 u32 i;
330
331 /* verify pointers are not NULL */
332 if (!le_struct || !attr)
333 return FM10K_ERR_PARAM;
334
335 if ((*attr >> FM10K_TLV_LEN_SHIFT) != len)
336 return FM10K_ERR_PARAM;
337
338 attr++;
339
340 for (i = 0; len; i++, len -= 4)
341 le32_ptr[i] = cpu_to_le32(attr[i]);
342
343 return 0;
344}
345
346/**
347 * fm10k_tlv_attr_nest_start - Start a set of nested attributes
348 * @msg: Pointer to message block
349 * @attr_id: Attribute ID
350 *
351 * This function will mark off a new nested region for encapsulating
352 * a given set of attributes. The idea is if you wish to place a secondary
353 * structure within the message this mechanism allows for that. The
354 * function will return NULL on failure, and a pointer to the start
355 * of the nested attributes on success.
356 **/
Bruce Allanbb269e82015-10-28 17:19:51 -0700357static u32 *fm10k_tlv_attr_nest_start(u32 *msg, u16 attr_id)
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400358{
359 u32 *attr;
360
361 /* verify pointer is not NULL */
362 if (!msg)
363 return NULL;
364
365 attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
366
367 attr[0] = attr_id;
368
369 /* return pointer to nest header */
370 return attr;
371}
372
373/**
Bruce Allanbb269e82015-10-28 17:19:51 -0700374 * fm10k_tlv_attr_nest_stop - Stop a set of nested attributes
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400375 * @msg: Pointer to message block
376 *
377 * This function closes off an existing set of nested attributes. The
378 * message pointer should be pointing to the parent of the nest. So in
379 * the case of a nest within the nest this would be the outer nest pointer.
380 * This function will return success provided all pointers are valid.
381 **/
Bruce Allanbb269e82015-10-28 17:19:51 -0700382static s32 fm10k_tlv_attr_nest_stop(u32 *msg)
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400383{
384 u32 *attr;
385 u32 len;
386
387 /* verify pointer is not NULL */
388 if (!msg)
389 return FM10K_ERR_PARAM;
390
391 /* locate the nested header and retrieve its length */
392 attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
393 len = (attr[0] >> FM10K_TLV_LEN_SHIFT) << FM10K_TLV_LEN_SHIFT;
394
395 /* only include nest if data was added to it */
396 if (len) {
397 len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
398 *msg += len;
399 }
400
401 return 0;
402}
403
404/**
405 * fm10k_tlv_attr_validate - Validate attribute metadata
406 * @attr: Pointer to attribute
407 * @tlv_attr: Type and length info for attribute
408 *
409 * This function does some basic validation of the input TLV. It
410 * verifies the length, and in the case of null terminated strings
411 * it verifies that the last byte is null. The function will
412 * return FM10K_ERR_PARAM if any attribute is malformed, otherwise
413 * it returns 0.
414 **/
415static s32 fm10k_tlv_attr_validate(u32 *attr,
416 const struct fm10k_tlv_attr *tlv_attr)
417{
418 u32 attr_id = *attr & FM10K_TLV_ID_MASK;
419 u16 len = *attr >> FM10K_TLV_LEN_SHIFT;
420
421 /* verify this is an attribute and not a message */
422 if (*attr & (FM10K_TLV_FLAGS_MSG << FM10K_TLV_FLAGS_SHIFT))
423 return FM10K_ERR_PARAM;
424
425 /* search through the list of attributes to find a matching ID */
426 while (tlv_attr->id < attr_id)
427 tlv_attr++;
428
429 /* if didn't find a match then we should exit */
430 if (tlv_attr->id != attr_id)
431 return FM10K_NOT_IMPLEMENTED;
432
433 /* move to start of attribute data */
434 attr++;
435
436 switch (tlv_attr->type) {
437 case FM10K_TLV_NULL_STRING:
438 if (!len ||
439 (attr[(len - 1) / 4] & (0xFF << (8 * ((len - 1) % 4)))))
440 return FM10K_ERR_PARAM;
441 if (len > tlv_attr->len)
442 return FM10K_ERR_PARAM;
443 break;
444 case FM10K_TLV_MAC_ADDR:
445 if (len != ETH_ALEN)
446 return FM10K_ERR_PARAM;
447 break;
448 case FM10K_TLV_BOOL:
449 if (len)
450 return FM10K_ERR_PARAM;
451 break;
452 case FM10K_TLV_UNSIGNED:
453 case FM10K_TLV_SIGNED:
454 if (len != tlv_attr->len)
455 return FM10K_ERR_PARAM;
456 break;
457 case FM10K_TLV_LE_STRUCT:
458 /* struct must be 4 byte aligned */
459 if ((len % 4) || len != tlv_attr->len)
460 return FM10K_ERR_PARAM;
461 break;
462 case FM10K_TLV_NESTED:
463 /* nested attributes must be 4 byte aligned */
464 if (len % 4)
465 return FM10K_ERR_PARAM;
466 break;
467 default:
468 /* attribute id is mapped to bad value */
469 return FM10K_ERR_PARAM;
470 }
471
472 return 0;
473}
474
475/**
476 * fm10k_tlv_attr_parse - Parses stream of attribute data
477 * @attr: Pointer to attribute list
478 * @results: Pointer array to store pointers to attributes
479 * @tlv_attr: Type and length info for attributes
480 *
481 * This function validates a stream of attributes and parses them
482 * up into an array of pointers stored in results. The function will
483 * return FM10K_ERR_PARAM on any input or message error,
484 * FM10K_NOT_IMPLEMENTED for any attribute that is outside of the array
Jacob Keller4e160f22016-04-01 16:17:35 -0700485 * and 0 on success. Any attributes not found in tlv_attr will be silently
486 * ignored.
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400487 **/
Bruce Allanbb269e82015-10-28 17:19:51 -0700488static s32 fm10k_tlv_attr_parse(u32 *attr, u32 **results,
489 const struct fm10k_tlv_attr *tlv_attr)
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400490{
491 u32 i, attr_id, offset = 0;
492 s32 err = 0;
493 u16 len;
494
495 /* verify pointers are not NULL */
496 if (!attr || !results)
497 return FM10K_ERR_PARAM;
498
499 /* initialize results to NULL */
500 for (i = 0; i < FM10K_TLV_RESULTS_MAX; i++)
501 results[i] = NULL;
502
503 /* pull length from the message header */
504 len = *attr >> FM10K_TLV_LEN_SHIFT;
505
506 /* no attributes to parse if there is no length */
507 if (!len)
508 return 0;
509
510 /* no attributes to parse, just raw data, message becomes attribute */
511 if (!tlv_attr) {
512 results[0] = attr;
513 return 0;
514 }
515
516 /* move to start of attribute data */
517 attr++;
518
519 /* run through list parsing all attributes */
520 while (offset < len) {
521 attr_id = *attr & FM10K_TLV_ID_MASK;
522
Jacob Keller4e160f22016-04-01 16:17:35 -0700523 if (attr_id >= FM10K_TLV_RESULTS_MAX)
524 return FM10K_NOT_IMPLEMENTED;
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400525
Jacob Keller4e160f22016-04-01 16:17:35 -0700526 err = fm10k_tlv_attr_validate(attr, tlv_attr);
527 if (err == FM10K_NOT_IMPLEMENTED)
528 ; /* silently ignore non-implemented attributes */
529 else if (err)
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400530 return err;
Jacob Keller4e160f22016-04-01 16:17:35 -0700531 else
Alexander Duyckae17db0ee2014-09-20 19:46:30 -0400532 results[attr_id] = attr;
533
534 /* update offset */
535 offset += FM10K_TLV_DWORD_LEN(*attr) * 4;
536
537 /* move to next attribute */
538 attr = &attr[FM10K_TLV_DWORD_LEN(*attr)];
539 }
540
541 /* we should find ourselves at the end of the list */
542 if (offset != len)
543 return FM10K_ERR_PARAM;
544
545 return 0;
546}
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400547
548/**
549 * fm10k_tlv_msg_parse - Parses message header and calls function handler
550 * @hw: Pointer to hardware structure
551 * @msg: Pointer to message
552 * @mbx: Pointer to mailbox information structure
Jacob Keller363656e2018-01-16 11:20:51 -0800553 * @data: Pointer to message handler data structure
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400554 *
555 * This function should be the first function called upon receiving a
556 * message. The handler will identify the message type and call the correct
557 * handler for the given message. It will return the value from the function
558 * call on a recognized message type, otherwise it will return
559 * FM10K_NOT_IMPLEMENTED on an unrecognized type.
560 **/
561s32 fm10k_tlv_msg_parse(struct fm10k_hw *hw, u32 *msg,
562 struct fm10k_mbx_info *mbx,
563 const struct fm10k_msg_data *data)
564{
565 u32 *results[FM10K_TLV_RESULTS_MAX];
566 u32 msg_id;
567 s32 err;
568
569 /* verify pointer is not NULL */
570 if (!msg || !data)
571 return FM10K_ERR_PARAM;
572
573 /* verify this is a message and not an attribute */
574 if (!(*msg & (FM10K_TLV_FLAGS_MSG << FM10K_TLV_FLAGS_SHIFT)))
575 return FM10K_ERR_PARAM;
576
577 /* grab message ID */
578 msg_id = *msg & FM10K_TLV_ID_MASK;
579
580 while (data->id < msg_id)
581 data++;
582
583 /* if we didn't find it then pass it up as an error */
584 if (data->id != msg_id) {
585 while (data->id != FM10K_TLV_ERROR)
586 data++;
587 }
588
589 /* parse the attributes into the results list */
590 err = fm10k_tlv_attr_parse(msg, results, data->attr);
591 if (err < 0)
592 return err;
593
594 return data->func(hw, results, mbx);
595}
596
597/**
598 * fm10k_tlv_msg_error - Default handler for unrecognized TLV message IDs
599 * @hw: Pointer to hardware structure
600 * @results: Pointer array to message, results[0] is pointer to message
601 * @mbx: Unused mailbox pointer
602 *
603 * This function is a default handler for unrecognized messages. At a
604 * a minimum it just indicates that the message requested was
605 * unimplemented.
606 **/
607s32 fm10k_tlv_msg_error(struct fm10k_hw *hw, u32 **results,
608 struct fm10k_mbx_info *mbx)
609{
610 return FM10K_NOT_IMPLEMENTED;
611}
612
613static const unsigned char test_str[] = "fm10k";
614static const unsigned char test_mac[ETH_ALEN] = { 0x12, 0x34, 0x56,
615 0x78, 0x9a, 0xbc };
616static const u16 test_vlan = 0x0FED;
617static const u64 test_u64 = 0xfedcba9876543210ull;
618static const u32 test_u32 = 0x87654321;
619static const u16 test_u16 = 0x8765;
620static const u8 test_u8 = 0x87;
621static const s64 test_s64 = -0x123456789abcdef0ll;
622static const s32 test_s32 = -0x1235678;
623static const s16 test_s16 = -0x1234;
624static const s8 test_s8 = -0x12;
625static const __le32 test_le[2] = { cpu_to_le32(0x12345678),
626 cpu_to_le32(0x9abcdef0)};
627
628/* The message below is meant to be used as a test message to demonstrate
629 * how to use the TLV interface and to test the types. Normally this code
630 * be compiled out by stripping the code wrapped in FM10K_TLV_TEST_MSG
631 */
632const struct fm10k_tlv_attr fm10k_tlv_msg_test_attr[] = {
633 FM10K_TLV_ATTR_NULL_STRING(FM10K_TEST_MSG_STRING, 80),
634 FM10K_TLV_ATTR_MAC_ADDR(FM10K_TEST_MSG_MAC_ADDR),
635 FM10K_TLV_ATTR_U8(FM10K_TEST_MSG_U8),
636 FM10K_TLV_ATTR_U16(FM10K_TEST_MSG_U16),
637 FM10K_TLV_ATTR_U32(FM10K_TEST_MSG_U32),
638 FM10K_TLV_ATTR_U64(FM10K_TEST_MSG_U64),
639 FM10K_TLV_ATTR_S8(FM10K_TEST_MSG_S8),
640 FM10K_TLV_ATTR_S16(FM10K_TEST_MSG_S16),
641 FM10K_TLV_ATTR_S32(FM10K_TEST_MSG_S32),
642 FM10K_TLV_ATTR_S64(FM10K_TEST_MSG_S64),
643 FM10K_TLV_ATTR_LE_STRUCT(FM10K_TEST_MSG_LE_STRUCT, 8),
644 FM10K_TLV_ATTR_NESTED(FM10K_TEST_MSG_NESTED),
645 FM10K_TLV_ATTR_S32(FM10K_TEST_MSG_RESULT),
646 FM10K_TLV_ATTR_LAST
647};
648
649/**
650 * fm10k_tlv_msg_test_generate_data - Stuff message with data
651 * @msg: Pointer to message
652 * @attr_flags: List of flags indicating what attributes to add
653 *
654 * This function is meant to load a message buffer with attribute data
655 **/
656static void fm10k_tlv_msg_test_generate_data(u32 *msg, u32 attr_flags)
657{
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800658 if (attr_flags & BIT(FM10K_TEST_MSG_STRING))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400659 fm10k_tlv_attr_put_null_string(msg, FM10K_TEST_MSG_STRING,
660 test_str);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800661 if (attr_flags & BIT(FM10K_TEST_MSG_MAC_ADDR))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400662 fm10k_tlv_attr_put_mac_vlan(msg, FM10K_TEST_MSG_MAC_ADDR,
663 test_mac, test_vlan);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800664 if (attr_flags & BIT(FM10K_TEST_MSG_U8))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400665 fm10k_tlv_attr_put_u8(msg, FM10K_TEST_MSG_U8, test_u8);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800666 if (attr_flags & BIT(FM10K_TEST_MSG_U16))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400667 fm10k_tlv_attr_put_u16(msg, FM10K_TEST_MSG_U16, test_u16);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800668 if (attr_flags & BIT(FM10K_TEST_MSG_U32))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400669 fm10k_tlv_attr_put_u32(msg, FM10K_TEST_MSG_U32, test_u32);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800670 if (attr_flags & BIT(FM10K_TEST_MSG_U64))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400671 fm10k_tlv_attr_put_u64(msg, FM10K_TEST_MSG_U64, test_u64);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800672 if (attr_flags & BIT(FM10K_TEST_MSG_S8))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400673 fm10k_tlv_attr_put_s8(msg, FM10K_TEST_MSG_S8, test_s8);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800674 if (attr_flags & BIT(FM10K_TEST_MSG_S16))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400675 fm10k_tlv_attr_put_s16(msg, FM10K_TEST_MSG_S16, test_s16);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800676 if (attr_flags & BIT(FM10K_TEST_MSG_S32))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400677 fm10k_tlv_attr_put_s32(msg, FM10K_TEST_MSG_S32, test_s32);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800678 if (attr_flags & BIT(FM10K_TEST_MSG_S64))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400679 fm10k_tlv_attr_put_s64(msg, FM10K_TEST_MSG_S64, test_s64);
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800680 if (attr_flags & BIT(FM10K_TEST_MSG_LE_STRUCT))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400681 fm10k_tlv_attr_put_le_struct(msg, FM10K_TEST_MSG_LE_STRUCT,
682 test_le, 8);
683}
684
685/**
686 * fm10k_tlv_msg_test_create - Create a test message testing all attributes
687 * @msg: Pointer to message
688 * @attr_flags: List of flags indicating what attributes to add
689 *
690 * This function is meant to load a message buffer with all attribute types
691 * including a nested attribute.
692 **/
693void fm10k_tlv_msg_test_create(u32 *msg, u32 attr_flags)
694{
695 u32 *nest = NULL;
696
697 fm10k_tlv_msg_init(msg, FM10K_TLV_MSG_ID_TEST);
698
699 fm10k_tlv_msg_test_generate_data(msg, attr_flags);
700
701 /* check for nested attributes */
702 attr_flags >>= FM10K_TEST_MSG_NESTED;
703
704 if (attr_flags) {
705 nest = fm10k_tlv_attr_nest_start(msg, FM10K_TEST_MSG_NESTED);
706
707 fm10k_tlv_msg_test_generate_data(nest, attr_flags);
708
709 fm10k_tlv_attr_nest_stop(msg);
710 }
711}
712
713/**
714 * fm10k_tlv_msg_test - Validate all results on test message receive
715 * @hw: Pointer to hardware structure
Matthew Vickeca32042015-01-31 02:23:05 +0000716 * @results: Pointer array to attributes in the message
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400717 * @mbx: Pointer to mailbox information structure
718 *
719 * This function does a check to verify all attributes match what the test
720 * message placed in the message buffer. It is the default handler
721 * for TLV test messages.
722 **/
723s32 fm10k_tlv_msg_test(struct fm10k_hw *hw, u32 **results,
724 struct fm10k_mbx_info *mbx)
725{
726 u32 *nest_results[FM10K_TLV_RESULTS_MAX];
727 unsigned char result_str[80];
728 unsigned char result_mac[ETH_ALEN];
729 s32 err = 0;
730 __le32 result_le[2];
731 u16 result_vlan;
732 u64 result_u64;
733 u32 result_u32;
734 u16 result_u16;
735 u8 result_u8;
736 s64 result_s64;
737 s32 result_s32;
738 s16 result_s16;
739 s8 result_s8;
740 u32 reply[3];
741
742 /* retrieve results of a previous test */
743 if (!!results[FM10K_TEST_MSG_RESULT])
744 return fm10k_tlv_attr_get_s32(results[FM10K_TEST_MSG_RESULT],
745 &mbx->test_result);
746
747parse_nested:
748 if (!!results[FM10K_TEST_MSG_STRING]) {
749 err = fm10k_tlv_attr_get_null_string(
750 results[FM10K_TEST_MSG_STRING],
751 result_str);
752 if (!err && memcmp(test_str, result_str, sizeof(test_str)))
753 err = FM10K_ERR_INVALID_VALUE;
754 if (err)
755 goto report_result;
756 }
757 if (!!results[FM10K_TEST_MSG_MAC_ADDR]) {
758 err = fm10k_tlv_attr_get_mac_vlan(
759 results[FM10K_TEST_MSG_MAC_ADDR],
760 result_mac, &result_vlan);
Jacob Keller6186ddf2015-11-16 15:33:34 -0800761 if (!err && !ether_addr_equal(test_mac, result_mac))
Alexander Duyck6b1f2012014-09-20 19:47:13 -0400762 err = FM10K_ERR_INVALID_VALUE;
763 if (!err && test_vlan != result_vlan)
764 err = FM10K_ERR_INVALID_VALUE;
765 if (err)
766 goto report_result;
767 }
768 if (!!results[FM10K_TEST_MSG_U8]) {
769 err = fm10k_tlv_attr_get_u8(results[FM10K_TEST_MSG_U8],
770 &result_u8);
771 if (!err && test_u8 != result_u8)
772 err = FM10K_ERR_INVALID_VALUE;
773 if (err)
774 goto report_result;
775 }
776 if (!!results[FM10K_TEST_MSG_U16]) {
777 err = fm10k_tlv_attr_get_u16(results[FM10K_TEST_MSG_U16],
778 &result_u16);
779 if (!err && test_u16 != result_u16)
780 err = FM10K_ERR_INVALID_VALUE;
781 if (err)
782 goto report_result;
783 }
784 if (!!results[FM10K_TEST_MSG_U32]) {
785 err = fm10k_tlv_attr_get_u32(results[FM10K_TEST_MSG_U32],
786 &result_u32);
787 if (!err && test_u32 != result_u32)
788 err = FM10K_ERR_INVALID_VALUE;
789 if (err)
790 goto report_result;
791 }
792 if (!!results[FM10K_TEST_MSG_U64]) {
793 err = fm10k_tlv_attr_get_u64(results[FM10K_TEST_MSG_U64],
794 &result_u64);
795 if (!err && test_u64 != result_u64)
796 err = FM10K_ERR_INVALID_VALUE;
797 if (err)
798 goto report_result;
799 }
800 if (!!results[FM10K_TEST_MSG_S8]) {
801 err = fm10k_tlv_attr_get_s8(results[FM10K_TEST_MSG_S8],
802 &result_s8);
803 if (!err && test_s8 != result_s8)
804 err = FM10K_ERR_INVALID_VALUE;
805 if (err)
806 goto report_result;
807 }
808 if (!!results[FM10K_TEST_MSG_S16]) {
809 err = fm10k_tlv_attr_get_s16(results[FM10K_TEST_MSG_S16],
810 &result_s16);
811 if (!err && test_s16 != result_s16)
812 err = FM10K_ERR_INVALID_VALUE;
813 if (err)
814 goto report_result;
815 }
816 if (!!results[FM10K_TEST_MSG_S32]) {
817 err = fm10k_tlv_attr_get_s32(results[FM10K_TEST_MSG_S32],
818 &result_s32);
819 if (!err && test_s32 != result_s32)
820 err = FM10K_ERR_INVALID_VALUE;
821 if (err)
822 goto report_result;
823 }
824 if (!!results[FM10K_TEST_MSG_S64]) {
825 err = fm10k_tlv_attr_get_s64(results[FM10K_TEST_MSG_S64],
826 &result_s64);
827 if (!err && test_s64 != result_s64)
828 err = FM10K_ERR_INVALID_VALUE;
829 if (err)
830 goto report_result;
831 }
832 if (!!results[FM10K_TEST_MSG_LE_STRUCT]) {
833 err = fm10k_tlv_attr_get_le_struct(
834 results[FM10K_TEST_MSG_LE_STRUCT],
835 result_le,
836 sizeof(result_le));
837 if (!err && memcmp(test_le, result_le, sizeof(test_le)))
838 err = FM10K_ERR_INVALID_VALUE;
839 if (err)
840 goto report_result;
841 }
842
843 if (!!results[FM10K_TEST_MSG_NESTED]) {
844 /* clear any pointers */
845 memset(nest_results, 0, sizeof(nest_results));
846
847 /* parse the nested attributes into the nest results list */
848 err = fm10k_tlv_attr_parse(results[FM10K_TEST_MSG_NESTED],
849 nest_results,
850 fm10k_tlv_msg_test_attr);
851 if (err)
852 goto report_result;
853
854 /* loop back through to the start */
855 results = nest_results;
856 goto parse_nested;
857 }
858
859report_result:
860 /* generate reply with test result */
861 fm10k_tlv_msg_init(reply, FM10K_TLV_MSG_ID_TEST);
862 fm10k_tlv_attr_put_s32(reply, FM10K_TEST_MSG_RESULT, err);
863
864 /* load onto outgoing mailbox */
865 return mbx->ops.enqueue_tx(hw, mbx, reply);
866}