1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020
5 #include "txgbe_type.h"
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
16 * returns 0 if it successfully read message from buffer
18 s32 txgbe_read_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
20 struct txgbe_mbx_info *mbx = &hw->mbx;
21 s32 ret_val = TXGBE_ERR_MBX;
23 DEBUGFUNC("txgbe_read_mbx");
25 /* limit read to size of mailbox */
30 ret_val = mbx->read(hw, msg, size, mbx_id);
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
42 * returns 0 if it successfully copied message into the buffer
44 s32 txgbe_write_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
46 struct txgbe_mbx_info *mbx = &hw->mbx;
49 DEBUGFUNC("txgbe_write_mbx");
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);
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
66 * returns 0 if the Status bit was found or else ERR_MBX
68 s32 txgbe_check_for_msg(struct txgbe_hw *hw, u16 mbx_id)
70 struct txgbe_mbx_info *mbx = &hw->mbx;
71 s32 ret_val = TXGBE_ERR_MBX;
73 DEBUGFUNC("txgbe_check_for_msg");
75 if (mbx->check_for_msg)
76 ret_val = mbx->check_for_msg(hw, mbx_id);
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
86 * returns 0 if the Status bit was found or else ERR_MBX
88 s32 txgbe_check_for_ack(struct txgbe_hw *hw, u16 mbx_id)
90 struct txgbe_mbx_info *mbx = &hw->mbx;
91 s32 ret_val = TXGBE_ERR_MBX;
93 DEBUGFUNC("txgbe_check_for_ack");
95 if (mbx->check_for_ack)
96 ret_val = mbx->check_for_ack(hw, mbx_id);
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
106 * returns 0 if the Status bit was found or else ERR_MBX
108 s32 txgbe_check_for_rst(struct txgbe_hw *hw, u16 mbx_id)
110 struct txgbe_mbx_info *mbx = &hw->mbx;
111 s32 ret_val = TXGBE_ERR_MBX;
113 DEBUGFUNC("txgbe_check_for_rst");
115 if (mbx->check_for_rst)
116 ret_val = mbx->check_for_rst(hw, mbx_id);
121 STATIC s32 txgbe_check_for_bit_pf(struct txgbe_hw *hw, u32 mask, s32 index)
123 u32 mbvficr = rd32(hw, TXGBE_MBVFICR(index));
124 s32 ret_val = TXGBE_ERR_MBX;
126 if (mbvficr & mask) {
128 wr32(hw, TXGBE_MBVFICR(index), mask);
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
139 * returns 0 if the VF has set the Status bit or else ERR_MBX
141 s32 txgbe_check_for_msg_pf(struct txgbe_hw *hw, u16 vf_number)
143 s32 ret_val = TXGBE_ERR_MBX;
144 s32 index = TXGBE_MBVFICR_INDEX(vf_number);
145 u32 vf_bit = vf_number % 16;
147 DEBUGFUNC("txgbe_check_for_msg_pf");
149 if (!txgbe_check_for_bit_pf(hw, TXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
152 hw->mbx.stats.reqs++;
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
163 * returns 0 if the VF has set the Status bit or else ERR_MBX
165 s32 txgbe_check_for_ack_pf(struct txgbe_hw *hw, u16 vf_number)
167 s32 ret_val = TXGBE_ERR_MBX;
168 s32 index = TXGBE_MBVFICR_INDEX(vf_number);
169 u32 vf_bit = vf_number % 16;
171 DEBUGFUNC("txgbe_check_for_ack_pf");
173 if (!txgbe_check_for_bit_pf(hw, TXGBE_MBVFICR_VFACK_VF1 << vf_bit,
176 hw->mbx.stats.acks++;
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
187 * returns 0 if the VF has set the Status bit or else ERR_MBX
189 s32 txgbe_check_for_rst_pf(struct txgbe_hw *hw, u16 vf_number)
191 u32 reg_offset = (vf_number < 32) ? 0 : 1;
192 u32 vf_shift = vf_number % 32;
194 s32 ret_val = TXGBE_ERR_MBX;
196 DEBUGFUNC("txgbe_check_for_rst_pf");
198 vflre = rd32(hw, TXGBE_FLRVFE(reg_offset));
199 if (vflre & (1 << vf_shift)) {
201 wr32(hw, TXGBE_FLRVFEC(reg_offset), (1 << vf_shift));
202 hw->mbx.stats.rsts++;
209 * txgbe_obtain_mbx_lock_pf - obtain mailbox lock
210 * @hw: pointer to the HW structure
211 * @vf_number: the VF index
213 * return 0 if we obtained the mailbox lock
215 STATIC s32 txgbe_obtain_mbx_lock_pf(struct txgbe_hw *hw, u16 vf_number)
217 s32 ret_val = TXGBE_ERR_MBX;
220 DEBUGFUNC("txgbe_obtain_mbx_lock_pf");
222 /* Take ownership of the buffer */
223 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_PFU);
225 /* reserve mailbox for vf use */
226 p2v_mailbox = rd32(hw, TXGBE_MBCTL(vf_number));
227 if (p2v_mailbox & TXGBE_MBCTL_PFU)
230 DEBUGOUT("Failed to obtain mailbox lock for VF%d", vf_number);
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
243 * returns 0 if it successfully copied message into the buffer
245 s32 txgbe_write_mbx_pf(struct txgbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
250 DEBUGFUNC("txgbe_write_mbx_pf");
252 /* lock the mailbox to prevent pf/vf race condition */
253 ret_val = txgbe_obtain_mbx_lock_pf(hw, vf_number);
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);
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]);
265 /* Interrupt VF to tell it a message has been sent and release buffer*/
266 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_STS);
269 hw->mbx.stats.msgs_tx++;
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
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.
286 s32 txgbe_read_mbx_pf(struct txgbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
291 DEBUGFUNC("txgbe_read_mbx_pf");
293 /* lock the mailbox to prevent pf/vf race condition */
294 ret_val = txgbe_obtain_mbx_lock_pf(hw, vf_number);
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);
302 /* Acknowledge the message and release buffer */
303 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_ACK);
306 hw->mbx.stats.msgs_rx++;
313 * txgbe_init_mbx_params_pf - set initial values for pf mailbox
314 * @hw: pointer to the HW structure
316 * Initializes the hw->mbx struct to correct values for pf mailbox
318 void txgbe_init_mbx_params_pf(struct txgbe_hw *hw)
320 struct txgbe_mbx_info *mbx = &hw->mbx;
325 mbx->size = TXGBE_P2VMBX_SIZE;
327 mbx->stats.msgs_tx = 0;
328 mbx->stats.msgs_rx = 0;