1 // SPDX-License-Identifier: GPL-2.0
2 /*******************************************************************************
4 Intel(R) Gigabit Ethernet Linux driver
5 Copyright(c) 2007-2013 Intel Corporation.
8 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
9 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
11 *******************************************************************************/
13 #include "e1000_mbx.h"
16 * e1000_null_mbx_check_for_flag - No-op function, return 0
17 * @hw: pointer to the HW structure
19 static s32 e1000_null_mbx_check_for_flag(struct e1000_hw E1000_UNUSEDARG *hw,
20 u16 E1000_UNUSEDARG mbx_id)
22 DEBUGFUNC("e1000_null_mbx_check_flag");
28 * e1000_null_mbx_transact - No-op function, return 0
29 * @hw: pointer to the HW structure
31 static s32 e1000_null_mbx_transact(struct e1000_hw E1000_UNUSEDARG *hw,
32 u32 E1000_UNUSEDARG *msg,
33 u16 E1000_UNUSEDARG size,
34 u16 E1000_UNUSEDARG mbx_id)
36 DEBUGFUNC("e1000_null_mbx_rw_msg");
42 * e1000_read_mbx - Reads a message from the mailbox
43 * @hw: pointer to the HW structure
44 * @msg: The message buffer
45 * @size: Length of buffer
46 * @mbx_id: id of mailbox to read
48 * returns SUCCESS if it successfully read message from buffer
50 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
52 struct e1000_mbx_info *mbx = &hw->mbx;
53 s32 ret_val = -E1000_ERR_MBX;
55 DEBUGFUNC("e1000_read_mbx");
57 /* limit read to size of mailbox */
62 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
68 * e1000_write_mbx - Write a message to the mailbox
69 * @hw: pointer to the HW structure
70 * @msg: The message buffer
71 * @size: Length of buffer
72 * @mbx_id: id of mailbox to write
74 * returns SUCCESS if it successfully copied message into the buffer
76 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
78 struct e1000_mbx_info *mbx = &hw->mbx;
79 s32 ret_val = E1000_SUCCESS;
81 DEBUGFUNC("e1000_write_mbx");
84 ret_val = -E1000_ERR_MBX;
86 else if (mbx->ops.write)
87 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
93 * e1000_check_for_msg - checks to see if someone sent us mail
94 * @hw: pointer to the HW structure
95 * @mbx_id: id of mailbox to check
97 * returns SUCCESS if the Status bit was found or else ERR_MBX
99 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
101 struct e1000_mbx_info *mbx = &hw->mbx;
102 s32 ret_val = -E1000_ERR_MBX;
104 DEBUGFUNC("e1000_check_for_msg");
106 if (mbx->ops.check_for_msg)
107 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
113 * e1000_check_for_ack - checks to see if someone sent us ACK
114 * @hw: pointer to the HW structure
115 * @mbx_id: id of mailbox to check
117 * returns SUCCESS if the Status bit was found or else ERR_MBX
119 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
121 struct e1000_mbx_info *mbx = &hw->mbx;
122 s32 ret_val = -E1000_ERR_MBX;
124 DEBUGFUNC("e1000_check_for_ack");
126 if (mbx->ops.check_for_ack)
127 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
133 * e1000_check_for_rst - checks to see if other side has reset
134 * @hw: pointer to the HW structure
135 * @mbx_id: id of mailbox to check
137 * returns SUCCESS if the Status bit was found or else ERR_MBX
139 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
141 struct e1000_mbx_info *mbx = &hw->mbx;
142 s32 ret_val = -E1000_ERR_MBX;
144 DEBUGFUNC("e1000_check_for_rst");
146 if (mbx->ops.check_for_rst)
147 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
153 * e1000_poll_for_msg - Wait for message notification
154 * @hw: pointer to the HW structure
155 * @mbx_id: id of mailbox to write
157 * returns SUCCESS if it successfully received a message notification
159 static s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
161 struct e1000_mbx_info *mbx = &hw->mbx;
162 int countdown = mbx->timeout;
164 DEBUGFUNC("e1000_poll_for_msg");
166 if (!countdown || !mbx->ops.check_for_msg)
169 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
173 usec_delay(mbx->usec_delay);
176 /* if we failed, all future posted messages fail until reset */
180 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
184 * e1000_poll_for_ack - Wait for message acknowledgement
185 * @hw: pointer to the HW structure
186 * @mbx_id: id of mailbox to write
188 * returns SUCCESS if it successfully received a message acknowledgement
190 static s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
192 struct e1000_mbx_info *mbx = &hw->mbx;
193 int countdown = mbx->timeout;
195 DEBUGFUNC("e1000_poll_for_ack");
197 if (!countdown || !mbx->ops.check_for_ack)
200 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
204 usec_delay(mbx->usec_delay);
207 /* if we failed, all future posted messages fail until reset */
211 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
215 * e1000_read_posted_mbx - Wait for message notification and receive message
216 * @hw: pointer to the HW structure
217 * @msg: The message buffer
218 * @size: Length of buffer
219 * @mbx_id: id of mailbox to write
221 * returns SUCCESS if it successfully received a message notification and
222 * copied it into the receive buffer.
224 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
226 struct e1000_mbx_info *mbx = &hw->mbx;
227 s32 ret_val = -E1000_ERR_MBX;
229 DEBUGFUNC("e1000_read_posted_mbx");
234 ret_val = e1000_poll_for_msg(hw, mbx_id);
236 /* if ack received read message, otherwise we timed out */
238 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
244 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
245 * @hw: pointer to the HW structure
246 * @msg: The message buffer
247 * @size: Length of buffer
248 * @mbx_id: id of mailbox to write
250 * returns SUCCESS if it successfully copied message into the buffer and
251 * received an ack to that message within delay * timeout period
253 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
255 struct e1000_mbx_info *mbx = &hw->mbx;
256 s32 ret_val = -E1000_ERR_MBX;
258 DEBUGFUNC("e1000_write_posted_mbx");
260 /* exit if either we can't write or there isn't a defined timeout */
261 if (!mbx->ops.write || !mbx->timeout)
265 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
267 /* if msg sent wait until we receive an ack */
269 ret_val = e1000_poll_for_ack(hw, mbx_id);
275 * e1000_init_mbx_ops_generic - Initialize mbx function pointers
276 * @hw: pointer to the HW structure
278 * Sets the function pointers to no-op functions
280 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
282 struct e1000_mbx_info *mbx = &hw->mbx;
283 mbx->ops.init_params = e1000_null_ops_generic;
284 mbx->ops.read = e1000_null_mbx_transact;
285 mbx->ops.write = e1000_null_mbx_transact;
286 mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
287 mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
288 mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
289 mbx->ops.read_posted = e1000_read_posted_mbx;
290 mbx->ops.write_posted = e1000_write_posted_mbx;
293 static s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
295 u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
296 s32 ret_val = -E1000_ERR_MBX;
298 if (mbvficr & mask) {
299 ret_val = E1000_SUCCESS;
300 E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
307 * e1000_check_for_msg_pf - checks to see if the VF has sent mail
308 * @hw: pointer to the HW structure
309 * @vf_number: the VF index
311 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
313 static s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
315 s32 ret_val = -E1000_ERR_MBX;
317 DEBUGFUNC("e1000_check_for_msg_pf");
319 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
320 ret_val = E1000_SUCCESS;
321 hw->mbx.stats.reqs++;
328 * e1000_check_for_ack_pf - checks to see if the VF has ACKed
329 * @hw: pointer to the HW structure
330 * @vf_number: the VF index
332 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
334 static s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
336 s32 ret_val = -E1000_ERR_MBX;
338 DEBUGFUNC("e1000_check_for_ack_pf");
340 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
341 ret_val = E1000_SUCCESS;
342 hw->mbx.stats.acks++;
349 * e1000_check_for_rst_pf - checks to see if the VF has reset
350 * @hw: pointer to the HW structure
351 * @vf_number: the VF index
353 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
355 static s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
357 u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
358 s32 ret_val = -E1000_ERR_MBX;
360 DEBUGFUNC("e1000_check_for_rst_pf");
362 if (vflre & (1 << vf_number)) {
363 ret_val = E1000_SUCCESS;
364 E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
365 hw->mbx.stats.rsts++;
372 * e1000_obtain_mbx_lock_pf - obtain mailbox lock
373 * @hw: pointer to the HW structure
374 * @vf_number: the VF index
376 * return SUCCESS if we obtained the mailbox lock
378 static s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
380 s32 ret_val = -E1000_ERR_MBX;
383 DEBUGFUNC("e1000_obtain_mbx_lock_pf");
385 /* Take ownership of the buffer */
386 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
388 /* reserve mailbox for vf use */
389 p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
390 if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
391 ret_val = E1000_SUCCESS;
397 * e1000_write_mbx_pf - Places a message in the mailbox
398 * @hw: pointer to the HW structure
399 * @msg: The message buffer
400 * @size: Length of buffer
401 * @vf_number: the VF index
403 * returns SUCCESS if it successfully copied message into the buffer
405 static s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
411 DEBUGFUNC("e1000_write_mbx_pf");
413 /* lock the mailbox to prevent pf/vf race condition */
414 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
418 /* flush msg and acks as we are overwriting the message buffer */
419 e1000_check_for_msg_pf(hw, vf_number);
420 e1000_check_for_ack_pf(hw, vf_number);
422 /* copy the caller specified message to the mailbox memory buffer */
423 for (i = 0; i < size; i++)
424 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
426 /* Interrupt VF to tell it a message has been sent and release buffer*/
427 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
430 hw->mbx.stats.msgs_tx++;
438 * e1000_read_mbx_pf - Read a message from the mailbox
439 * @hw: pointer to the HW structure
440 * @msg: The message buffer
441 * @size: Length of buffer
442 * @vf_number: the VF index
444 * This function copies a message from the mailbox buffer to the caller's
445 * memory buffer. The presumption is that the caller knows that there was
446 * a message due to a VF request so no polling for message is needed.
448 static s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
454 DEBUGFUNC("e1000_read_mbx_pf");
456 /* lock the mailbox to prevent pf/vf race condition */
457 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
461 /* copy the message to the mailbox memory buffer */
462 for (i = 0; i < size; i++)
463 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
465 /* Acknowledge the message and release buffer */
466 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
469 hw->mbx.stats.msgs_rx++;
476 * e1000_init_mbx_params_pf - set initial values for pf mailbox
477 * @hw: pointer to the HW structure
479 * Initializes the hw->mbx struct to correct values for pf mailbox
481 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
483 struct e1000_mbx_info *mbx = &hw->mbx;
485 switch (hw->mac.type) {
492 mbx->size = E1000_VFMAILBOX_SIZE;
494 mbx->ops.read = e1000_read_mbx_pf;
495 mbx->ops.write = e1000_write_mbx_pf;
496 mbx->ops.read_posted = e1000_read_posted_mbx;
497 mbx->ops.write_posted = e1000_write_posted_mbx;
498 mbx->ops.check_for_msg = e1000_check_for_msg_pf;
499 mbx->ops.check_for_ack = e1000_check_for_ack_pf;
500 mbx->ops.check_for_rst = e1000_check_for_rst_pf;
502 mbx->stats.msgs_tx = 0;
503 mbx->stats.msgs_rx = 0;
508 return E1000_SUCCESS;