xref: /f-stack/dpdk/drivers/net/e1000/base/e1000_mbx.c (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2020 Intel Corporation
3  */
4 
5 #include "e1000_mbx.h"
6 
7 /**
8  *  e1000_null_mbx_check_for_flag - No-op function, return 0
9  *  @hw: pointer to the HW structure
10  *  @mbx_id: id of mailbox to read
11  **/
e1000_null_mbx_check_for_flag(struct e1000_hw E1000_UNUSEDARG * hw,u16 E1000_UNUSEDARG mbx_id)12 STATIC s32 e1000_null_mbx_check_for_flag(struct e1000_hw E1000_UNUSEDARG *hw,
13 					 u16 E1000_UNUSEDARG mbx_id)
14 {
15 	DEBUGFUNC("e1000_null_mbx_check_flag");
16 	UNREFERENCED_2PARAMETER(hw, mbx_id);
17 
18 	return E1000_SUCCESS;
19 }
20 
21 /**
22  *  e1000_null_mbx_transact - No-op function, return 0
23  *  @hw: pointer to the HW structure
24  *  @msg: The message buffer
25  *  @size: Length of buffer
26  *  @mbx_id: id of mailbox to read
27  **/
e1000_null_mbx_transact(struct e1000_hw E1000_UNUSEDARG * hw,u32 E1000_UNUSEDARG * msg,u16 E1000_UNUSEDARG size,u16 E1000_UNUSEDARG mbx_id)28 STATIC s32 e1000_null_mbx_transact(struct e1000_hw E1000_UNUSEDARG *hw,
29 				   u32 E1000_UNUSEDARG *msg,
30 				   u16 E1000_UNUSEDARG size,
31 				   u16 E1000_UNUSEDARG mbx_id)
32 {
33 	DEBUGFUNC("e1000_null_mbx_rw_msg");
34 	UNREFERENCED_4PARAMETER(hw, msg, size, mbx_id);
35 
36 	return E1000_SUCCESS;
37 }
38 
39 /**
40  *  e1000_read_mbx - Reads a message from the mailbox
41  *  @hw: pointer to the HW structure
42  *  @msg: The message buffer
43  *  @size: Length of buffer
44  *  @mbx_id: id of mailbox to read
45  *
46  *  returns SUCCESS if it successfully read message from buffer
47  **/
e1000_read_mbx(struct e1000_hw * hw,u32 * msg,u16 size,u16 mbx_id)48 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
49 {
50 	struct e1000_mbx_info *mbx = &hw->mbx;
51 	s32 ret_val = -E1000_ERR_MBX;
52 
53 	DEBUGFUNC("e1000_read_mbx");
54 
55 	/* limit read to size of mailbox */
56 	if (size > mbx->size)
57 		size = mbx->size;
58 
59 	if (mbx->ops.read)
60 		ret_val = mbx->ops.read(hw, msg, size, mbx_id);
61 
62 	return ret_val;
63 }
64 
65 /**
66  *  e1000_write_mbx - Write a message to the mailbox
67  *  @hw: pointer to the HW structure
68  *  @msg: The message buffer
69  *  @size: Length of buffer
70  *  @mbx_id: id of mailbox to write
71  *
72  *  returns SUCCESS if it successfully copied message into the buffer
73  **/
e1000_write_mbx(struct e1000_hw * hw,u32 * msg,u16 size,u16 mbx_id)74 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
75 {
76 	struct e1000_mbx_info *mbx = &hw->mbx;
77 	s32 ret_val = E1000_SUCCESS;
78 
79 	DEBUGFUNC("e1000_write_mbx");
80 
81 	if (size > mbx->size)
82 		ret_val = -E1000_ERR_MBX;
83 
84 	else if (mbx->ops.write)
85 		ret_val = mbx->ops.write(hw, msg, size, mbx_id);
86 
87 	return ret_val;
88 }
89 
90 /**
91  *  e1000_check_for_msg - checks to see if someone sent us mail
92  *  @hw: pointer to the HW structure
93  *  @mbx_id: id of mailbox to check
94  *
95  *  returns SUCCESS if the Status bit was found or else ERR_MBX
96  **/
e1000_check_for_msg(struct e1000_hw * hw,u16 mbx_id)97 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
98 {
99 	struct e1000_mbx_info *mbx = &hw->mbx;
100 	s32 ret_val = -E1000_ERR_MBX;
101 
102 	DEBUGFUNC("e1000_check_for_msg");
103 
104 	if (mbx->ops.check_for_msg)
105 		ret_val = mbx->ops.check_for_msg(hw, mbx_id);
106 
107 	return ret_val;
108 }
109 
110 /**
111  *  e1000_check_for_ack - checks to see if someone sent us ACK
112  *  @hw: pointer to the HW structure
113  *  @mbx_id: id of mailbox to check
114  *
115  *  returns SUCCESS if the Status bit was found or else ERR_MBX
116  **/
e1000_check_for_ack(struct e1000_hw * hw,u16 mbx_id)117 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
118 {
119 	struct e1000_mbx_info *mbx = &hw->mbx;
120 	s32 ret_val = -E1000_ERR_MBX;
121 
122 	DEBUGFUNC("e1000_check_for_ack");
123 
124 	if (mbx->ops.check_for_ack)
125 		ret_val = mbx->ops.check_for_ack(hw, mbx_id);
126 
127 	return ret_val;
128 }
129 
130 /**
131  *  e1000_check_for_rst - checks to see if other side has reset
132  *  @hw: pointer to the HW structure
133  *  @mbx_id: id of mailbox to check
134  *
135  *  returns SUCCESS if the Status bit was found or else ERR_MBX
136  **/
e1000_check_for_rst(struct e1000_hw * hw,u16 mbx_id)137 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
138 {
139 	struct e1000_mbx_info *mbx = &hw->mbx;
140 	s32 ret_val = -E1000_ERR_MBX;
141 
142 	DEBUGFUNC("e1000_check_for_rst");
143 
144 	if (mbx->ops.check_for_rst)
145 		ret_val = mbx->ops.check_for_rst(hw, mbx_id);
146 
147 	return ret_val;
148 }
149 
150 /**
151  *  e1000_poll_for_msg - Wait for message notification
152  *  @hw: pointer to the HW structure
153  *  @mbx_id: id of mailbox to write
154  *
155  *  returns SUCCESS if it successfully received a message notification
156  **/
e1000_poll_for_msg(struct e1000_hw * hw,u16 mbx_id)157 STATIC s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
158 {
159 	struct e1000_mbx_info *mbx = &hw->mbx;
160 	int countdown = mbx->timeout;
161 
162 	DEBUGFUNC("e1000_poll_for_msg");
163 
164 	if (!countdown || !mbx->ops.check_for_msg)
165 		goto out;
166 
167 	while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
168 		countdown--;
169 		if (!countdown)
170 			break;
171 		usec_delay(mbx->usec_delay);
172 	}
173 
174 	/* if we failed, all future posted messages fail until reset */
175 	if (!countdown)
176 		mbx->timeout = 0;
177 out:
178 	return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
179 }
180 
181 /**
182  *  e1000_poll_for_ack - Wait for message acknowledgement
183  *  @hw: pointer to the HW structure
184  *  @mbx_id: id of mailbox to write
185  *
186  *  returns SUCCESS if it successfully received a message acknowledgement
187  **/
e1000_poll_for_ack(struct e1000_hw * hw,u16 mbx_id)188 STATIC s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
189 {
190 	struct e1000_mbx_info *mbx = &hw->mbx;
191 	int countdown = mbx->timeout;
192 
193 	DEBUGFUNC("e1000_poll_for_ack");
194 
195 	if (!countdown || !mbx->ops.check_for_ack)
196 		goto out;
197 
198 	while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
199 		countdown--;
200 		if (!countdown)
201 			break;
202 		usec_delay(mbx->usec_delay);
203 	}
204 
205 	/* if we failed, all future posted messages fail until reset */
206 	if (!countdown)
207 		mbx->timeout = 0;
208 out:
209 	return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
210 }
211 
212 /**
213  *  e1000_read_posted_mbx - Wait for message notification and receive message
214  *  @hw: pointer to the HW structure
215  *  @msg: The message buffer
216  *  @size: Length of buffer
217  *  @mbx_id: id of mailbox to write
218  *
219  *  returns SUCCESS if it successfully received a message notification and
220  *  copied it into the receive buffer.
221  **/
e1000_read_posted_mbx(struct e1000_hw * hw,u32 * msg,u16 size,u16 mbx_id)222 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
223 {
224 	struct e1000_mbx_info *mbx = &hw->mbx;
225 	s32 ret_val = -E1000_ERR_MBX;
226 
227 	DEBUGFUNC("e1000_read_posted_mbx");
228 
229 	if (!mbx->ops.read)
230 		goto out;
231 
232 	ret_val = e1000_poll_for_msg(hw, mbx_id);
233 
234 	/* if ack received read message, otherwise we timed out */
235 	if (!ret_val)
236 		ret_val = mbx->ops.read(hw, msg, size, mbx_id);
237 out:
238 	return ret_val;
239 }
240 
241 /**
242  *  e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
243  *  @hw: pointer to the HW structure
244  *  @msg: The message buffer
245  *  @size: Length of buffer
246  *  @mbx_id: id of mailbox to write
247  *
248  *  returns SUCCESS if it successfully copied message into the buffer and
249  *  received an ack to that message within delay * timeout period
250  **/
e1000_write_posted_mbx(struct e1000_hw * hw,u32 * msg,u16 size,u16 mbx_id)251 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
252 {
253 	struct e1000_mbx_info *mbx = &hw->mbx;
254 	s32 ret_val = -E1000_ERR_MBX;
255 
256 	DEBUGFUNC("e1000_write_posted_mbx");
257 
258 	/* exit if either we can't write or there isn't a defined timeout */
259 	if (!mbx->ops.write || !mbx->timeout)
260 		goto out;
261 
262 	/* send msg */
263 	ret_val = mbx->ops.write(hw, msg, size, mbx_id);
264 
265 	/* if msg sent wait until we receive an ack */
266 	if (!ret_val)
267 		ret_val = e1000_poll_for_ack(hw, mbx_id);
268 out:
269 	return ret_val;
270 }
271 
272 /**
273  *  e1000_init_mbx_ops_generic - Initialize mbx function pointers
274  *  @hw: pointer to the HW structure
275  *
276  *  Sets the function pointers to no-op functions
277  **/
e1000_init_mbx_ops_generic(struct e1000_hw * hw)278 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
279 {
280 	struct e1000_mbx_info *mbx = &hw->mbx;
281 	mbx->ops.init_params = e1000_null_ops_generic;
282 	mbx->ops.read = e1000_null_mbx_transact;
283 	mbx->ops.write = e1000_null_mbx_transact;
284 	mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
285 	mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
286 	mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
287 	mbx->ops.read_posted = e1000_read_posted_mbx;
288 	mbx->ops.write_posted = e1000_write_posted_mbx;
289 }
290 
291 /**
292  *  e1000_read_v2p_mailbox - read v2p mailbox
293  *  @hw: pointer to the HW structure
294  *
295  *  This function is used to read the v2p mailbox without losing the read to
296  *  clear status bits.
297  **/
e1000_read_v2p_mailbox(struct e1000_hw * hw)298 STATIC u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
299 {
300 	u32 v2p_mailbox = E1000_READ_REG(hw, E1000_V2PMAILBOX(0));
301 
302 	v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
303 	hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
304 
305 	return v2p_mailbox;
306 }
307 
308 /**
309  *  e1000_check_for_bit_vf - Determine if a status bit was set
310  *  @hw: pointer to the HW structure
311  *  @mask: bitmask for bits to be tested and cleared
312  *
313  *  This function is used to check for the read to clear bits within
314  *  the V2P mailbox.
315  **/
e1000_check_for_bit_vf(struct e1000_hw * hw,u32 mask)316 STATIC s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
317 {
318 	u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
319 	s32 ret_val = -E1000_ERR_MBX;
320 
321 	if (v2p_mailbox & mask)
322 		ret_val = E1000_SUCCESS;
323 
324 	hw->dev_spec.vf.v2p_mailbox &= ~mask;
325 
326 	return ret_val;
327 }
328 
329 /**
330  *  e1000_check_for_msg_vf - checks to see if the PF has sent mail
331  *  @hw: pointer to the HW structure
332  *  @mbx_id: id of mailbox to check
333  *
334  *  returns SUCCESS if the PF has set the Status bit or else ERR_MBX
335  **/
e1000_check_for_msg_vf(struct e1000_hw * hw,u16 E1000_UNUSEDARG mbx_id)336 STATIC s32 e1000_check_for_msg_vf(struct e1000_hw *hw,
337 				  u16 E1000_UNUSEDARG mbx_id)
338 {
339 	s32 ret_val = -E1000_ERR_MBX;
340 
341 	UNREFERENCED_1PARAMETER(mbx_id);
342 	DEBUGFUNC("e1000_check_for_msg_vf");
343 
344 	if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
345 		ret_val = E1000_SUCCESS;
346 		hw->mbx.stats.reqs++;
347 	}
348 
349 	return ret_val;
350 }
351 
352 /**
353  *  e1000_check_for_ack_vf - checks to see if the PF has ACK'd
354  *  @hw: pointer to the HW structure
355  *  @mbx_id: id of mailbox to check
356  *
357  *  returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
358  **/
e1000_check_for_ack_vf(struct e1000_hw * hw,u16 E1000_UNUSEDARG mbx_id)359 STATIC s32 e1000_check_for_ack_vf(struct e1000_hw *hw,
360 				  u16 E1000_UNUSEDARG mbx_id)
361 {
362 	s32 ret_val = -E1000_ERR_MBX;
363 
364 	UNREFERENCED_1PARAMETER(mbx_id);
365 	DEBUGFUNC("e1000_check_for_ack_vf");
366 
367 	if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
368 		ret_val = E1000_SUCCESS;
369 		hw->mbx.stats.acks++;
370 	}
371 
372 	return ret_val;
373 }
374 
375 /**
376  *  e1000_check_for_rst_vf - checks to see if the PF has reset
377  *  @hw: pointer to the HW structure
378  *  @mbx_id: id of mailbox to check
379  *
380  *  returns true if the PF has set the reset done bit or else false
381  **/
e1000_check_for_rst_vf(struct e1000_hw * hw,u16 E1000_UNUSEDARG mbx_id)382 STATIC s32 e1000_check_for_rst_vf(struct e1000_hw *hw,
383 				  u16 E1000_UNUSEDARG mbx_id)
384 {
385 	s32 ret_val = -E1000_ERR_MBX;
386 
387 	UNREFERENCED_1PARAMETER(mbx_id);
388 	DEBUGFUNC("e1000_check_for_rst_vf");
389 
390 	if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
391 					 E1000_V2PMAILBOX_RSTI))) {
392 		ret_val = E1000_SUCCESS;
393 		hw->mbx.stats.rsts++;
394 	}
395 
396 	return ret_val;
397 }
398 
399 /**
400  *  e1000_obtain_mbx_lock_vf - obtain mailbox lock
401  *  @hw: pointer to the HW structure
402  *
403  *  return SUCCESS if we obtained the mailbox lock
404  **/
e1000_obtain_mbx_lock_vf(struct e1000_hw * hw)405 STATIC s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
406 {
407 	s32 ret_val = -E1000_ERR_MBX;
408 	int count = 10;
409 
410 	DEBUGFUNC("e1000_obtain_mbx_lock_vf");
411 
412 	do {
413 		/* Take ownership of the buffer */
414 		E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
415 
416 		/* reserve mailbox for vf use */
417 		if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU) {
418 			ret_val = E1000_SUCCESS;
419 			break;
420 		}
421 		usec_delay(1000);
422 	} while (count-- > 0);
423 
424 	return ret_val;
425 }
426 
427 /**
428  *  e1000_write_mbx_vf - Write a message to the mailbox
429  *  @hw: pointer to the HW structure
430  *  @msg: The message buffer
431  *  @size: Length of buffer
432  *  @mbx_id: id of mailbox to write
433  *
434  *  returns SUCCESS if it successfully copied message into the buffer
435  **/
e1000_write_mbx_vf(struct e1000_hw * hw,u32 * msg,u16 size,u16 E1000_UNUSEDARG mbx_id)436 STATIC s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
437 			      u16 E1000_UNUSEDARG mbx_id)
438 {
439 	s32 ret_val;
440 	u16 i;
441 
442 	UNREFERENCED_1PARAMETER(mbx_id);
443 
444 	DEBUGFUNC("e1000_write_mbx_vf");
445 
446 	/* lock the mailbox to prevent pf/vf race condition */
447 	ret_val = e1000_obtain_mbx_lock_vf(hw);
448 	if (ret_val)
449 		goto out_no_write;
450 
451 	/* flush msg and acks as we are overwriting the message buffer */
452 	e1000_check_for_msg_vf(hw, 0);
453 	e1000_check_for_ack_vf(hw, 0);
454 
455 	/* copy the caller specified message to the mailbox memory buffer */
456 	for (i = 0; i < size; i++)
457 		E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(0), i, msg[i]);
458 
459 	/* update stats */
460 	hw->mbx.stats.msgs_tx++;
461 
462 	/* Drop VFU and interrupt the PF to tell it a message has been sent */
463 	E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
464 
465 out_no_write:
466 	return ret_val;
467 }
468 
469 /**
470  *  e1000_read_mbx_vf - Reads a message from the inbox intended for vf
471  *  @hw: pointer to the HW structure
472  *  @msg: The message buffer
473  *  @size: Length of buffer
474  *  @mbx_id: id of mailbox to read
475  *
476  *  returns SUCCESS if it successfully read message from buffer
477  **/
e1000_read_mbx_vf(struct e1000_hw * hw,u32 * msg,u16 size,u16 E1000_UNUSEDARG mbx_id)478 STATIC s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
479 			     u16 E1000_UNUSEDARG mbx_id)
480 {
481 	s32 ret_val = E1000_SUCCESS;
482 	u16 i;
483 
484 	DEBUGFUNC("e1000_read_mbx_vf");
485 	UNREFERENCED_1PARAMETER(mbx_id);
486 
487 	/* lock the mailbox to prevent pf/vf race condition */
488 	ret_val = e1000_obtain_mbx_lock_vf(hw);
489 	if (ret_val)
490 		goto out_no_read;
491 
492 	/* copy the message from the mailbox memory buffer */
493 	for (i = 0; i < size; i++)
494 		msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(0), i);
495 
496 	/* Acknowledge receipt and release mailbox, then we're done */
497 	E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
498 
499 	/* update stats */
500 	hw->mbx.stats.msgs_rx++;
501 
502 out_no_read:
503 	return ret_val;
504 }
505 
506 /**
507  *  e1000_init_mbx_params_vf - set initial values for vf mailbox
508  *  @hw: pointer to the HW structure
509  *
510  *  Initializes the hw->mbx struct to correct values for vf mailbox
511  */
e1000_init_mbx_params_vf(struct e1000_hw * hw)512 s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
513 {
514 	struct e1000_mbx_info *mbx = &hw->mbx;
515 
516 	/* start mailbox as timed out and let the reset_hw call set the timeout
517 	 * value to begin communications */
518 	mbx->timeout = 0;
519 	mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
520 
521 	mbx->size = E1000_VFMAILBOX_SIZE;
522 
523 	mbx->ops.read = e1000_read_mbx_vf;
524 	mbx->ops.write = e1000_write_mbx_vf;
525 	mbx->ops.read_posted = e1000_read_posted_mbx;
526 	mbx->ops.write_posted = e1000_write_posted_mbx;
527 	mbx->ops.check_for_msg = e1000_check_for_msg_vf;
528 	mbx->ops.check_for_ack = e1000_check_for_ack_vf;
529 	mbx->ops.check_for_rst = e1000_check_for_rst_vf;
530 
531 	mbx->stats.msgs_tx = 0;
532 	mbx->stats.msgs_rx = 0;
533 	mbx->stats.reqs = 0;
534 	mbx->stats.acks = 0;
535 	mbx->stats.rsts = 0;
536 
537 	return E1000_SUCCESS;
538 }
539 
e1000_check_for_bit_pf(struct e1000_hw * hw,u32 mask)540 STATIC s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
541 {
542 	u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
543 	s32 ret_val = -E1000_ERR_MBX;
544 
545 	if (mbvficr & mask) {
546 		ret_val = E1000_SUCCESS;
547 		E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
548 	}
549 
550 	return ret_val;
551 }
552 
553 /**
554  *  e1000_check_for_msg_pf - checks to see if the VF has sent mail
555  *  @hw: pointer to the HW structure
556  *  @vf_number: the VF index
557  *
558  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
559  **/
e1000_check_for_msg_pf(struct e1000_hw * hw,u16 vf_number)560 STATIC s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
561 {
562 	s32 ret_val = -E1000_ERR_MBX;
563 
564 	DEBUGFUNC("e1000_check_for_msg_pf");
565 
566 	if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
567 		ret_val = E1000_SUCCESS;
568 		hw->mbx.stats.reqs++;
569 	}
570 
571 	return ret_val;
572 }
573 
574 /**
575  *  e1000_check_for_ack_pf - checks to see if the VF has ACKed
576  *  @hw: pointer to the HW structure
577  *  @vf_number: the VF index
578  *
579  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
580  **/
e1000_check_for_ack_pf(struct e1000_hw * hw,u16 vf_number)581 STATIC s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
582 {
583 	s32 ret_val = -E1000_ERR_MBX;
584 
585 	DEBUGFUNC("e1000_check_for_ack_pf");
586 
587 	if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
588 		ret_val = E1000_SUCCESS;
589 		hw->mbx.stats.acks++;
590 	}
591 
592 	return ret_val;
593 }
594 
595 /**
596  *  e1000_check_for_rst_pf - checks to see if the VF has reset
597  *  @hw: pointer to the HW structure
598  *  @vf_number: the VF index
599  *
600  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
601  **/
e1000_check_for_rst_pf(struct e1000_hw * hw,u16 vf_number)602 STATIC s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
603 {
604 	u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
605 	s32 ret_val = -E1000_ERR_MBX;
606 
607 	DEBUGFUNC("e1000_check_for_rst_pf");
608 
609 	if (vflre & (1 << vf_number)) {
610 		ret_val = E1000_SUCCESS;
611 		E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
612 		hw->mbx.stats.rsts++;
613 	}
614 
615 	return ret_val;
616 }
617 
618 /**
619  *  e1000_obtain_mbx_lock_pf - obtain mailbox lock
620  *  @hw: pointer to the HW structure
621  *  @vf_number: the VF index
622  *
623  *  return SUCCESS if we obtained the mailbox lock
624  **/
e1000_obtain_mbx_lock_pf(struct e1000_hw * hw,u16 vf_number)625 STATIC s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
626 {
627 	s32 ret_val = -E1000_ERR_MBX;
628 	u32 p2v_mailbox;
629 	int count = 10;
630 
631 	DEBUGFUNC("e1000_obtain_mbx_lock_pf");
632 
633 	do {
634 		/* Take ownership of the buffer */
635 		E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number),
636 				E1000_P2VMAILBOX_PFU);
637 
638 		/* reserve mailbox for pf use */
639 		p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
640 		if (p2v_mailbox & E1000_P2VMAILBOX_PFU) {
641 			ret_val = E1000_SUCCESS;
642 			break;
643 		}
644 		usec_delay(1000);
645 	} while (count-- > 0);
646 
647 	return ret_val;
648 
649 }
650 
651 /**
652  *  e1000_write_mbx_pf - Places a message in the mailbox
653  *  @hw: pointer to the HW structure
654  *  @msg: The message buffer
655  *  @size: Length of buffer
656  *  @vf_number: the VF index
657  *
658  *  returns SUCCESS if it successfully copied message into the buffer
659  **/
e1000_write_mbx_pf(struct e1000_hw * hw,u32 * msg,u16 size,u16 vf_number)660 STATIC s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
661 			      u16 vf_number)
662 {
663 	s32 ret_val;
664 	u16 i;
665 
666 	DEBUGFUNC("e1000_write_mbx_pf");
667 
668 	/* lock the mailbox to prevent pf/vf race condition */
669 	ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
670 	if (ret_val)
671 		goto out_no_write;
672 
673 	/* flush msg and acks as we are overwriting the message buffer */
674 	e1000_check_for_msg_pf(hw, vf_number);
675 	e1000_check_for_ack_pf(hw, vf_number);
676 
677 	/* copy the caller specified message to the mailbox memory buffer */
678 	for (i = 0; i < size; i++)
679 		E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
680 
681 	/* Interrupt VF to tell it a message has been sent and release buffer*/
682 	E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
683 
684 	/* update stats */
685 	hw->mbx.stats.msgs_tx++;
686 
687 out_no_write:
688 	return ret_val;
689 
690 }
691 
692 /**
693  *  e1000_read_mbx_pf - Read a message from the mailbox
694  *  @hw: pointer to the HW structure
695  *  @msg: The message buffer
696  *  @size: Length of buffer
697  *  @vf_number: the VF index
698  *
699  *  This function copies a message from the mailbox buffer to the caller's
700  *  memory buffer.  The presumption is that the caller knows that there was
701  *  a message due to a VF request so no polling for message is needed.
702  **/
e1000_read_mbx_pf(struct e1000_hw * hw,u32 * msg,u16 size,u16 vf_number)703 STATIC s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
704 			     u16 vf_number)
705 {
706 	s32 ret_val;
707 	u16 i;
708 
709 	DEBUGFUNC("e1000_read_mbx_pf");
710 
711 	/* lock the mailbox to prevent pf/vf race condition */
712 	ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
713 	if (ret_val)
714 		goto out_no_read;
715 
716 	/* copy the message to the mailbox memory buffer */
717 	for (i = 0; i < size; i++)
718 		msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
719 
720 	/* Acknowledge the message and release buffer */
721 	E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
722 
723 	/* update stats */
724 	hw->mbx.stats.msgs_rx++;
725 
726 out_no_read:
727 	return ret_val;
728 }
729 
730 /**
731  *  e1000_init_mbx_params_pf - set initial values for pf mailbox
732  *  @hw: pointer to the HW structure
733  *
734  *  Initializes the hw->mbx struct to correct values for pf mailbox
735  */
e1000_init_mbx_params_pf(struct e1000_hw * hw)736 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
737 {
738 	struct e1000_mbx_info *mbx = &hw->mbx;
739 
740 	switch (hw->mac.type) {
741 	case e1000_82576:
742 	case e1000_i350:
743 	case e1000_i354:
744 		mbx->timeout = 0;
745 		mbx->usec_delay = 0;
746 
747 		mbx->size = E1000_VFMAILBOX_SIZE;
748 
749 		mbx->ops.read = e1000_read_mbx_pf;
750 		mbx->ops.write = e1000_write_mbx_pf;
751 		mbx->ops.read_posted = e1000_read_posted_mbx;
752 		mbx->ops.write_posted = e1000_write_posted_mbx;
753 		mbx->ops.check_for_msg = e1000_check_for_msg_pf;
754 		mbx->ops.check_for_ack = e1000_check_for_ack_pf;
755 		mbx->ops.check_for_rst = e1000_check_for_rst_pf;
756 
757 		mbx->stats.msgs_tx = 0;
758 		mbx->stats.msgs_rx = 0;
759 		mbx->stats.reqs = 0;
760 		mbx->stats.acks = 0;
761 		mbx->stats.rsts = 0;
762 		/* Fall through */
763 	default:
764 		return E1000_SUCCESS;
765 	}
766 }
767 
768