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