xref: /f-stack/dpdk/drivers/net/txgbe/base/txgbe_mbx.c (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4 
5 #include "txgbe_type.h"
6 
7 #include "txgbe_mbx.h"
8 
9 /**
10  *  txgbe_read_mbx - Reads a message from the mailbox
11  *  @hw: pointer to the HW structure
12  *  @msg: The message buffer
13  *  @size: Length of buffer
14  *  @mbx_id: id of mailbox to read
15  *
16  *  returns 0 if it successfully read message from buffer
17  **/
txgbe_read_mbx(struct txgbe_hw * hw,u32 * msg,u16 size,u16 mbx_id)18 s32 txgbe_read_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
19 {
20 	struct txgbe_mbx_info *mbx = &hw->mbx;
21 	s32 ret_val = TXGBE_ERR_MBX;
22 
23 	DEBUGFUNC("txgbe_read_mbx");
24 
25 	/* limit read to size of mailbox */
26 	if (size > mbx->size)
27 		size = mbx->size;
28 
29 	if (mbx->read)
30 		ret_val = mbx->read(hw, msg, size, mbx_id);
31 
32 	return ret_val;
33 }
34 
35 /**
36  *  txgbe_write_mbx - Write a message to the mailbox
37  *  @hw: pointer to the HW structure
38  *  @msg: The message buffer
39  *  @size: Length of buffer
40  *  @mbx_id: id of mailbox to write
41  *
42  *  returns 0 if it successfully copied message into the buffer
43  **/
txgbe_write_mbx(struct txgbe_hw * hw,u32 * msg,u16 size,u16 mbx_id)44 s32 txgbe_write_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
45 {
46 	struct txgbe_mbx_info *mbx = &hw->mbx;
47 	s32 ret_val = 0;
48 
49 	DEBUGFUNC("txgbe_write_mbx");
50 
51 	if (size > mbx->size) {
52 		ret_val = TXGBE_ERR_MBX;
53 		DEBUGOUT("Invalid mailbox message size %d", size);
54 	} else if (mbx->write) {
55 		ret_val = mbx->write(hw, msg, size, mbx_id);
56 	}
57 
58 	return ret_val;
59 }
60 
61 /**
62  *  txgbe_check_for_msg - checks to see if someone sent us mail
63  *  @hw: pointer to the HW structure
64  *  @mbx_id: id of mailbox to check
65  *
66  *  returns 0 if the Status bit was found or else ERR_MBX
67  **/
txgbe_check_for_msg(struct txgbe_hw * hw,u16 mbx_id)68 s32 txgbe_check_for_msg(struct txgbe_hw *hw, u16 mbx_id)
69 {
70 	struct txgbe_mbx_info *mbx = &hw->mbx;
71 	s32 ret_val = TXGBE_ERR_MBX;
72 
73 	DEBUGFUNC("txgbe_check_for_msg");
74 
75 	if (mbx->check_for_msg)
76 		ret_val = mbx->check_for_msg(hw, mbx_id);
77 
78 	return ret_val;
79 }
80 
81 /**
82  *  txgbe_check_for_ack - checks to see if someone sent us ACK
83  *  @hw: pointer to the HW structure
84  *  @mbx_id: id of mailbox to check
85  *
86  *  returns 0 if the Status bit was found or else ERR_MBX
87  **/
txgbe_check_for_ack(struct txgbe_hw * hw,u16 mbx_id)88 s32 txgbe_check_for_ack(struct txgbe_hw *hw, u16 mbx_id)
89 {
90 	struct txgbe_mbx_info *mbx = &hw->mbx;
91 	s32 ret_val = TXGBE_ERR_MBX;
92 
93 	DEBUGFUNC("txgbe_check_for_ack");
94 
95 	if (mbx->check_for_ack)
96 		ret_val = mbx->check_for_ack(hw, mbx_id);
97 
98 	return ret_val;
99 }
100 
101 /**
102  *  txgbe_check_for_rst - checks to see if other side has reset
103  *  @hw: pointer to the HW structure
104  *  @mbx_id: id of mailbox to check
105  *
106  *  returns 0 if the Status bit was found or else ERR_MBX
107  **/
txgbe_check_for_rst(struct txgbe_hw * hw,u16 mbx_id)108 s32 txgbe_check_for_rst(struct txgbe_hw *hw, u16 mbx_id)
109 {
110 	struct txgbe_mbx_info *mbx = &hw->mbx;
111 	s32 ret_val = TXGBE_ERR_MBX;
112 
113 	DEBUGFUNC("txgbe_check_for_rst");
114 
115 	if (mbx->check_for_rst)
116 		ret_val = mbx->check_for_rst(hw, mbx_id);
117 
118 	return ret_val;
119 }
120 
txgbe_check_for_bit_pf(struct txgbe_hw * hw,u32 mask,s32 index)121 STATIC s32 txgbe_check_for_bit_pf(struct txgbe_hw *hw, u32 mask, s32 index)
122 {
123 	u32 mbvficr = rd32(hw, TXGBE_MBVFICR(index));
124 	s32 ret_val = TXGBE_ERR_MBX;
125 
126 	if (mbvficr & mask) {
127 		ret_val = 0;
128 		wr32(hw, TXGBE_MBVFICR(index), mask);
129 	}
130 
131 	return ret_val;
132 }
133 
134 /**
135  *  txgbe_check_for_msg_pf - checks to see if the VF has sent mail
136  *  @hw: pointer to the HW structure
137  *  @vf_number: the VF index
138  *
139  *  returns 0 if the VF has set the Status bit or else ERR_MBX
140  **/
txgbe_check_for_msg_pf(struct txgbe_hw * hw,u16 vf_number)141 s32 txgbe_check_for_msg_pf(struct txgbe_hw *hw, u16 vf_number)
142 {
143 	s32 ret_val = TXGBE_ERR_MBX;
144 	s32 index = TXGBE_MBVFICR_INDEX(vf_number);
145 	u32 vf_bit = vf_number % 16;
146 
147 	DEBUGFUNC("txgbe_check_for_msg_pf");
148 
149 	if (!txgbe_check_for_bit_pf(hw, TXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
150 				    index)) {
151 		ret_val = 0;
152 		hw->mbx.stats.reqs++;
153 	}
154 
155 	return ret_val;
156 }
157 
158 /**
159  *  txgbe_check_for_ack_pf - checks to see if the VF has ACKed
160  *  @hw: pointer to the HW structure
161  *  @vf_number: the VF index
162  *
163  *  returns 0 if the VF has set the Status bit or else ERR_MBX
164  **/
txgbe_check_for_ack_pf(struct txgbe_hw * hw,u16 vf_number)165 s32 txgbe_check_for_ack_pf(struct txgbe_hw *hw, u16 vf_number)
166 {
167 	s32 ret_val = TXGBE_ERR_MBX;
168 	s32 index = TXGBE_MBVFICR_INDEX(vf_number);
169 	u32 vf_bit = vf_number % 16;
170 
171 	DEBUGFUNC("txgbe_check_for_ack_pf");
172 
173 	if (!txgbe_check_for_bit_pf(hw, TXGBE_MBVFICR_VFACK_VF1 << vf_bit,
174 				    index)) {
175 		ret_val = 0;
176 		hw->mbx.stats.acks++;
177 	}
178 
179 	return ret_val;
180 }
181 
182 /**
183  *  txgbe_check_for_rst_pf - checks to see if the VF has reset
184  *  @hw: pointer to the HW structure
185  *  @vf_number: the VF index
186  *
187  *  returns 0 if the VF has set the Status bit or else ERR_MBX
188  **/
txgbe_check_for_rst_pf(struct txgbe_hw * hw,u16 vf_number)189 s32 txgbe_check_for_rst_pf(struct txgbe_hw *hw, u16 vf_number)
190 {
191 	u32 reg_offset = (vf_number < 32) ? 0 : 1;
192 	u32 vf_shift = vf_number % 32;
193 	u32 vflre = 0;
194 	s32 ret_val = TXGBE_ERR_MBX;
195 
196 	DEBUGFUNC("txgbe_check_for_rst_pf");
197 
198 	vflre = rd32(hw, TXGBE_FLRVFE(reg_offset));
199 	if (vflre & (1 << vf_shift)) {
200 		ret_val = 0;
201 		wr32(hw, TXGBE_FLRVFEC(reg_offset), (1 << vf_shift));
202 		hw->mbx.stats.rsts++;
203 	}
204 
205 	return ret_val;
206 }
207 
208 /**
209  *  txgbe_obtain_mbx_lock_pf - obtain mailbox lock
210  *  @hw: pointer to the HW structure
211  *  @vf_number: the VF index
212  *
213  *  return 0 if we obtained the mailbox lock
214  **/
txgbe_obtain_mbx_lock_pf(struct txgbe_hw * hw,u16 vf_number)215 STATIC s32 txgbe_obtain_mbx_lock_pf(struct txgbe_hw *hw, u16 vf_number)
216 {
217 	s32 ret_val = TXGBE_ERR_MBX;
218 	u32 p2v_mailbox;
219 
220 	DEBUGFUNC("txgbe_obtain_mbx_lock_pf");
221 
222 	/* Take ownership of the buffer */
223 	wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_PFU);
224 
225 	/* reserve mailbox for vf use */
226 	p2v_mailbox = rd32(hw, TXGBE_MBCTL(vf_number));
227 	if (p2v_mailbox & TXGBE_MBCTL_PFU)
228 		ret_val = 0;
229 	else
230 		DEBUGOUT("Failed to obtain mailbox lock for VF%d", vf_number);
231 
232 
233 	return ret_val;
234 }
235 
236 /**
237  *  txgbe_write_mbx_pf - Places a message in the mailbox
238  *  @hw: pointer to the HW structure
239  *  @msg: The message buffer
240  *  @size: Length of buffer
241  *  @vf_number: the VF index
242  *
243  *  returns 0 if it successfully copied message into the buffer
244  **/
txgbe_write_mbx_pf(struct txgbe_hw * hw,u32 * msg,u16 size,u16 vf_number)245 s32 txgbe_write_mbx_pf(struct txgbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
246 {
247 	s32 ret_val;
248 	u16 i;
249 
250 	DEBUGFUNC("txgbe_write_mbx_pf");
251 
252 	/* lock the mailbox to prevent pf/vf race condition */
253 	ret_val = txgbe_obtain_mbx_lock_pf(hw, vf_number);
254 	if (ret_val)
255 		goto out_no_write;
256 
257 	/* flush msg and acks as we are overwriting the message buffer */
258 	txgbe_check_for_msg_pf(hw, vf_number);
259 	txgbe_check_for_ack_pf(hw, vf_number);
260 
261 	/* copy the caller specified message to the mailbox memory buffer */
262 	for (i = 0; i < size; i++)
263 		wr32a(hw, TXGBE_MBMEM(vf_number), i, msg[i]);
264 
265 	/* Interrupt VF to tell it a message has been sent and release buffer*/
266 	wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_STS);
267 
268 	/* update stats */
269 	hw->mbx.stats.msgs_tx++;
270 
271 out_no_write:
272 	return ret_val;
273 }
274 
275 /**
276  *  txgbe_read_mbx_pf - Read a message from the mailbox
277  *  @hw: pointer to the HW structure
278  *  @msg: The message buffer
279  *  @size: Length of buffer
280  *  @vf_number: the VF index
281  *
282  *  This function copies a message from the mailbox buffer to the caller's
283  *  memory buffer.  The presumption is that the caller knows that there was
284  *  a message due to a VF request so no polling for message is needed.
285  **/
txgbe_read_mbx_pf(struct txgbe_hw * hw,u32 * msg,u16 size,u16 vf_number)286 s32 txgbe_read_mbx_pf(struct txgbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
287 {
288 	s32 ret_val;
289 	u16 i;
290 
291 	DEBUGFUNC("txgbe_read_mbx_pf");
292 
293 	/* lock the mailbox to prevent pf/vf race condition */
294 	ret_val = txgbe_obtain_mbx_lock_pf(hw, vf_number);
295 	if (ret_val)
296 		goto out_no_read;
297 
298 	/* copy the message to the mailbox memory buffer */
299 	for (i = 0; i < size; i++)
300 		msg[i] = rd32a(hw, TXGBE_MBMEM(vf_number), i);
301 
302 	/* Acknowledge the message and release buffer */
303 	wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_ACK);
304 
305 	/* update stats */
306 	hw->mbx.stats.msgs_rx++;
307 
308 out_no_read:
309 	return ret_val;
310 }
311 
312 /**
313  *  txgbe_init_mbx_params_pf - set initial values for pf mailbox
314  *  @hw: pointer to the HW structure
315  *
316  *  Initializes the hw->mbx struct to correct values for pf mailbox
317  */
txgbe_init_mbx_params_pf(struct txgbe_hw * hw)318 void txgbe_init_mbx_params_pf(struct txgbe_hw *hw)
319 {
320 	struct txgbe_mbx_info *mbx = &hw->mbx;
321 
322 	mbx->timeout = 0;
323 	mbx->usec_delay = 0;
324 
325 	mbx->size = TXGBE_P2VMBX_SIZE;
326 
327 	mbx->stats.msgs_tx = 0;
328 	mbx->stats.msgs_rx = 0;
329 	mbx->stats.reqs = 0;
330 	mbx->stats.acks = 0;
331 	mbx->stats.rsts = 0;
332 }
333