1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2021 Beijing WangXun Technology Co., Ltd.
3 * Copyright(c) 2010-2017 Intel Corporation
11 * ngbe_read_mbx - Reads a message from the mailbox
12 * @hw: pointer to the HW structure
13 * @msg: The message buffer
14 * @size: Length of buffer
15 * @mbx_id: id of mailbox to read
17 * returns 0 if it successfully read message from buffer
19 s32 ngbe_read_mbx(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
21 struct ngbe_mbx_info *mbx = &hw->mbx;
22 s32 ret_val = NGBE_ERR_MBX;
24 DEBUGFUNC("ngbe_read_mbx");
26 /* limit read to size of mailbox */
31 ret_val = mbx->read(hw, msg, size, mbx_id);
37 * ngbe_write_mbx - Write a message to the mailbox
38 * @hw: pointer to the HW structure
39 * @msg: The message buffer
40 * @size: Length of buffer
41 * @mbx_id: id of mailbox to write
43 * returns 0 if it successfully copied message into the buffer
45 s32 ngbe_write_mbx(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
47 struct ngbe_mbx_info *mbx = &hw->mbx;
50 DEBUGFUNC("ngbe_write_mbx");
52 if (size > mbx->size) {
53 ret_val = NGBE_ERR_MBX;
54 DEBUGOUT("Invalid mailbox message size %d", size);
55 } else if (mbx->write) {
56 ret_val = mbx->write(hw, msg, size, mbx_id);
63 * ngbe_check_for_msg - checks to see if someone sent us mail
64 * @hw: pointer to the HW structure
65 * @mbx_id: id of mailbox to check
67 * returns 0 if the Status bit was found or else ERR_MBX
69 s32 ngbe_check_for_msg(struct ngbe_hw *hw, u16 mbx_id)
71 struct ngbe_mbx_info *mbx = &hw->mbx;
72 s32 ret_val = NGBE_ERR_MBX;
74 DEBUGFUNC("ngbe_check_for_msg");
76 if (mbx->check_for_msg)
77 ret_val = mbx->check_for_msg(hw, mbx_id);
83 * ngbe_check_for_ack - checks to see if someone sent us ACK
84 * @hw: pointer to the HW structure
85 * @mbx_id: id of mailbox to check
87 * returns 0 if the Status bit was found or else ERR_MBX
89 s32 ngbe_check_for_ack(struct ngbe_hw *hw, u16 mbx_id)
91 struct ngbe_mbx_info *mbx = &hw->mbx;
92 s32 ret_val = NGBE_ERR_MBX;
94 DEBUGFUNC("ngbe_check_for_ack");
96 if (mbx->check_for_ack)
97 ret_val = mbx->check_for_ack(hw, mbx_id);
103 * ngbe_check_for_rst - checks to see if other side has reset
104 * @hw: pointer to the HW structure
105 * @mbx_id: id of mailbox to check
107 * returns 0 if the Status bit was found or else ERR_MBX
109 s32 ngbe_check_for_rst(struct ngbe_hw *hw, u16 mbx_id)
111 struct ngbe_mbx_info *mbx = &hw->mbx;
112 s32 ret_val = NGBE_ERR_MBX;
114 DEBUGFUNC("ngbe_check_for_rst");
116 if (mbx->check_for_rst)
117 ret_val = mbx->check_for_rst(hw, mbx_id);
122 STATIC s32 ngbe_check_for_bit_pf(struct ngbe_hw *hw, u32 mask)
124 u32 mbvficr = rd32(hw, NGBE_MBVFICR);
125 s32 ret_val = NGBE_ERR_MBX;
127 if (mbvficr & mask) {
129 wr32(hw, NGBE_MBVFICR, mask);
136 * ngbe_check_for_msg_pf - checks to see if the VF has sent mail
137 * @hw: pointer to the HW structure
138 * @vf_number: the VF index
140 * returns 0 if the VF has set the Status bit or else ERR_MBX
142 s32 ngbe_check_for_msg_pf(struct ngbe_hw *hw, u16 vf_number)
144 s32 ret_val = NGBE_ERR_MBX;
145 u32 vf_bit = vf_number;
147 DEBUGFUNC("ngbe_check_for_msg_pf");
149 if (!ngbe_check_for_bit_pf(hw, NGBE_MBVFICR_VFREQ_VF1 << vf_bit)) {
151 hw->mbx.stats.reqs++;
158 * ngbe_check_for_ack_pf - checks to see if the VF has ACKed
159 * @hw: pointer to the HW structure
160 * @vf_number: the VF index
162 * returns 0 if the VF has set the Status bit or else ERR_MBX
164 s32 ngbe_check_for_ack_pf(struct ngbe_hw *hw, u16 vf_number)
166 s32 ret_val = NGBE_ERR_MBX;
167 u32 vf_bit = vf_number;
169 DEBUGFUNC("ngbe_check_for_ack_pf");
171 if (!ngbe_check_for_bit_pf(hw, NGBE_MBVFICR_VFACK_VF1 << vf_bit)) {
173 hw->mbx.stats.acks++;
180 * ngbe_check_for_rst_pf - checks to see if the VF has reset
181 * @hw: pointer to the HW structure
182 * @vf_number: the VF index
184 * returns 0 if the VF has set the Status bit or else ERR_MBX
186 s32 ngbe_check_for_rst_pf(struct ngbe_hw *hw, u16 vf_number)
189 s32 ret_val = NGBE_ERR_MBX;
191 DEBUGFUNC("ngbe_check_for_rst_pf");
193 vflre = rd32(hw, NGBE_FLRVFE);
194 if (vflre & (1 << vf_number)) {
196 wr32(hw, NGBE_FLRVFEC, (1 << vf_number));
197 hw->mbx.stats.rsts++;
204 * ngbe_obtain_mbx_lock_pf - obtain mailbox lock
205 * @hw: pointer to the HW structure
206 * @vf_number: the VF index
208 * return 0 if we obtained the mailbox lock
210 STATIC s32 ngbe_obtain_mbx_lock_pf(struct ngbe_hw *hw, u16 vf_number)
212 s32 ret_val = NGBE_ERR_MBX;
215 DEBUGFUNC("ngbe_obtain_mbx_lock_pf");
217 /* Take ownership of the buffer */
218 wr32(hw, NGBE_MBCTL(vf_number), NGBE_MBCTL_PFU);
220 /* reserve mailbox for vf use */
221 p2v_mailbox = rd32(hw, NGBE_MBCTL(vf_number));
222 if (p2v_mailbox & NGBE_MBCTL_PFU)
225 DEBUGOUT("Failed to obtain mailbox lock for VF%d", vf_number);
232 * ngbe_write_mbx_pf - Places a message in the mailbox
233 * @hw: pointer to the HW structure
234 * @msg: The message buffer
235 * @size: Length of buffer
236 * @vf_number: the VF index
238 * returns 0 if it successfully copied message into the buffer
240 s32 ngbe_write_mbx_pf(struct ngbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
245 DEBUGFUNC("ngbe_write_mbx_pf");
247 /* lock the mailbox to prevent pf/vf race condition */
248 ret_val = ngbe_obtain_mbx_lock_pf(hw, vf_number);
252 /* flush msg and acks as we are overwriting the message buffer */
253 ngbe_check_for_msg_pf(hw, vf_number);
254 ngbe_check_for_ack_pf(hw, vf_number);
256 /* copy the caller specified message to the mailbox memory buffer */
257 for (i = 0; i < size; i++)
258 wr32a(hw, NGBE_MBMEM(vf_number), i, msg[i]);
260 /* Interrupt VF to tell it a message has been sent and release buffer*/
261 wr32(hw, NGBE_MBCTL(vf_number), NGBE_MBCTL_STS);
264 hw->mbx.stats.msgs_tx++;
271 * ngbe_read_mbx_pf - Read a message from the mailbox
272 * @hw: pointer to the HW structure
273 * @msg: The message buffer
274 * @size: Length of buffer
275 * @vf_number: the VF index
277 * This function copies a message from the mailbox buffer to the caller's
278 * memory buffer. The presumption is that the caller knows that there was
279 * a message due to a VF request so no polling for message is needed.
281 s32 ngbe_read_mbx_pf(struct ngbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
286 DEBUGFUNC("ngbe_read_mbx_pf");
288 /* lock the mailbox to prevent pf/vf race condition */
289 ret_val = ngbe_obtain_mbx_lock_pf(hw, vf_number);
293 /* copy the message to the mailbox memory buffer */
294 for (i = 0; i < size; i++)
295 msg[i] = rd32a(hw, NGBE_MBMEM(vf_number), i);
297 /* Acknowledge the message and release buffer */
298 wr32(hw, NGBE_MBCTL(vf_number), NGBE_MBCTL_ACK);
301 hw->mbx.stats.msgs_rx++;
308 * ngbe_init_mbx_params_pf - set initial values for pf mailbox
309 * @hw: pointer to the HW structure
311 * Initializes the hw->mbx struct to correct values for pf mailbox
313 void ngbe_init_mbx_params_pf(struct ngbe_hw *hw)
315 struct ngbe_mbx_info *mbx = &hw->mbx;
320 mbx->size = NGBE_P2VMBX_SIZE;
322 mbx->stats.msgs_tx = 0;
323 mbx->stats.msgs_rx = 0;