1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2018 Intel Corporation. */
7 * igb_read_mbx - Reads a message from the mailbox
8 * @hw: pointer to the HW structure
9 * @msg: The message buffer
10 * @size: Length of buffer
11 * @mbx_id: id of mailbox to read
13 * returns SUCCESS if it successfully read message from buffer
15 s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id,
18 struct e1000_mbx_info *mbx = &hw->mbx;
19 s32 ret_val = -E1000_ERR_MBX;
21 /* limit read to size of mailbox */
26 ret_val = mbx->ops.read(hw, msg, size, mbx_id, unlock);
32 * igb_write_mbx - Write a message to the mailbox
33 * @hw: pointer to the HW structure
34 * @msg: The message buffer
35 * @size: Length of buffer
36 * @mbx_id: id of mailbox to write
38 * returns SUCCESS if it successfully copied message into the buffer
40 s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
42 struct e1000_mbx_info *mbx = &hw->mbx;
46 ret_val = -E1000_ERR_MBX;
48 else if (mbx->ops.write)
49 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
55 * igb_check_for_msg - checks to see if someone sent us mail
56 * @hw: pointer to the HW structure
57 * @mbx_id: id of mailbox to check
59 * returns SUCCESS if the Status bit was found or else ERR_MBX
61 s32 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
63 struct e1000_mbx_info *mbx = &hw->mbx;
64 s32 ret_val = -E1000_ERR_MBX;
66 if (mbx->ops.check_for_msg)
67 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
73 * igb_check_for_ack - checks to see if someone sent us ACK
74 * @hw: pointer to the HW structure
75 * @mbx_id: id of mailbox to check
77 * returns SUCCESS if the Status bit was found or else ERR_MBX
79 s32 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
81 struct e1000_mbx_info *mbx = &hw->mbx;
82 s32 ret_val = -E1000_ERR_MBX;
84 if (mbx->ops.check_for_ack)
85 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
91 * igb_check_for_rst - checks to see if other side has reset
92 * @hw: pointer to the HW structure
93 * @mbx_id: id of mailbox to check
95 * returns SUCCESS if the Status bit was found or else ERR_MBX
97 s32 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
99 struct e1000_mbx_info *mbx = &hw->mbx;
100 s32 ret_val = -E1000_ERR_MBX;
102 if (mbx->ops.check_for_rst)
103 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
109 * igb_unlock_mbx - unlock the mailbox
110 * @hw: pointer to the HW structure
111 * @mbx_id: id of mailbox to check
113 * returns SUCCESS if the mailbox was unlocked or else ERR_MBX
115 s32 igb_unlock_mbx(struct e1000_hw *hw, u16 mbx_id)
117 struct e1000_mbx_info *mbx = &hw->mbx;
118 s32 ret_val = -E1000_ERR_MBX;
121 ret_val = mbx->ops.unlock(hw, mbx_id);
127 * igb_poll_for_msg - Wait for message notification
128 * @hw: pointer to the HW structure
129 * @mbx_id: id of mailbox to write
131 * returns SUCCESS if it successfully received a message notification
133 static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
135 struct e1000_mbx_info *mbx = &hw->mbx;
136 int countdown = mbx->timeout;
138 if (!countdown || !mbx->ops.check_for_msg)
141 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
145 udelay(mbx->usec_delay);
148 /* if we failed, all future posted messages fail until reset */
152 return countdown ? 0 : -E1000_ERR_MBX;
156 * igb_poll_for_ack - Wait for message acknowledgement
157 * @hw: pointer to the HW structure
158 * @mbx_id: id of mailbox to write
160 * returns SUCCESS if it successfully received a message acknowledgement
162 static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
164 struct e1000_mbx_info *mbx = &hw->mbx;
165 int countdown = mbx->timeout;
167 if (!countdown || !mbx->ops.check_for_ack)
170 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
174 udelay(mbx->usec_delay);
177 /* if we failed, all future posted messages fail until reset */
181 return countdown ? 0 : -E1000_ERR_MBX;
185 * igb_read_posted_mbx - Wait for message notification and receive message
186 * @hw: pointer to the HW structure
187 * @msg: The message buffer
188 * @size: Length of buffer
189 * @mbx_id: id of mailbox to write
191 * returns SUCCESS if it successfully received a message notification and
192 * copied it into the receive buffer.
194 static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
197 struct e1000_mbx_info *mbx = &hw->mbx;
198 s32 ret_val = -E1000_ERR_MBX;
203 ret_val = igb_poll_for_msg(hw, mbx_id);
206 ret_val = mbx->ops.read(hw, msg, size, mbx_id, true);
212 * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
213 * @hw: pointer to the HW structure
214 * @msg: The message buffer
215 * @size: Length of buffer
216 * @mbx_id: id of mailbox to write
218 * returns SUCCESS if it successfully copied message into the buffer and
219 * received an ack to that message within delay * timeout period
221 static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
224 struct e1000_mbx_info *mbx = &hw->mbx;
225 s32 ret_val = -E1000_ERR_MBX;
227 /* exit if either we can't write or there isn't a defined timeout */
228 if (!mbx->ops.write || !mbx->timeout)
232 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
234 /* if msg sent wait until we receive an ack */
236 ret_val = igb_poll_for_ack(hw, mbx_id);
241 static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
243 u32 mbvficr = rd32(E1000_MBVFICR);
244 s32 ret_val = -E1000_ERR_MBX;
246 if (mbvficr & mask) {
248 wr32(E1000_MBVFICR, mask);
255 * igb_check_for_msg_pf - checks to see if the VF has sent mail
256 * @hw: pointer to the HW structure
257 * @vf_number: the VF index
259 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
261 static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
263 s32 ret_val = -E1000_ERR_MBX;
265 if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
267 hw->mbx.stats.reqs++;
274 * igb_check_for_ack_pf - checks to see if the VF has ACKed
275 * @hw: pointer to the HW structure
276 * @vf_number: the VF index
278 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
280 static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
282 s32 ret_val = -E1000_ERR_MBX;
284 if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
286 hw->mbx.stats.acks++;
293 * igb_check_for_rst_pf - checks to see if the VF has reset
294 * @hw: pointer to the HW structure
295 * @vf_number: the VF index
297 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
299 static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
301 u32 vflre = rd32(E1000_VFLRE);
302 s32 ret_val = -E1000_ERR_MBX;
304 if (vflre & BIT(vf_number)) {
306 wr32(E1000_VFLRE, BIT(vf_number));
307 hw->mbx.stats.rsts++;
314 * igb_obtain_mbx_lock_pf - obtain mailbox lock
315 * @hw: pointer to the HW structure
316 * @vf_number: the VF index
318 * return SUCCESS if we obtained the mailbox lock
320 static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
322 s32 ret_val = -E1000_ERR_MBX;
327 /* Take ownership of the buffer */
328 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
330 /* reserve mailbox for vf use */
331 p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
332 if (p2v_mailbox & E1000_P2VMAILBOX_PFU) {
337 } while (count-- > 0);
343 * igb_release_mbx_lock_pf - release mailbox lock
344 * @hw: pointer to the HW structure
345 * @vf_number: the VF index
347 * return SUCCESS if we released the mailbox lock
349 static s32 igb_release_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
353 /* drop PF lock of mailbox, if set */
354 p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
355 if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
356 wr32(E1000_P2VMAILBOX(vf_number),
357 p2v_mailbox & ~E1000_P2VMAILBOX_PFU);
363 * igb_write_mbx_pf - Places a message in the mailbox
364 * @hw: pointer to the HW structure
365 * @msg: The message buffer
366 * @size: Length of buffer
367 * @vf_number: the VF index
369 * returns SUCCESS if it successfully copied message into the buffer
371 static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
377 /* lock the mailbox to prevent pf/vf race condition */
378 ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
382 /* flush msg and acks as we are overwriting the message buffer */
383 igb_check_for_msg_pf(hw, vf_number);
384 igb_check_for_ack_pf(hw, vf_number);
386 /* copy the caller specified message to the mailbox memory buffer */
387 for (i = 0; i < size; i++)
388 array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
390 /* Interrupt VF to tell it a message has been sent and release buffer*/
391 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
394 hw->mbx.stats.msgs_tx++;
402 * igb_read_mbx_pf - Read a message from the mailbox
403 * @hw: pointer to the HW structure
404 * @msg: The message buffer
405 * @size: Length of buffer
406 * @vf_number: the VF index
407 * @unlock: unlock the mailbox when done?
409 * This function copies a message from the mailbox buffer to the caller's
410 * memory buffer. The presumption is that the caller knows that there was
411 * a message due to a VF request so no polling for message is needed.
413 static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
414 u16 vf_number, bool unlock)
419 /* lock the mailbox to prevent pf/vf race condition */
420 ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
424 /* copy the message to the mailbox memory buffer */
425 for (i = 0; i < size; i++)
426 msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
428 /* Acknowledge the message and release mailbox lock (or not) */
430 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
432 wr32(E1000_P2VMAILBOX(vf_number),
433 E1000_P2VMAILBOX_ACK | E1000_P2VMAILBOX_PFU);
436 hw->mbx.stats.msgs_rx++;
443 * e1000_init_mbx_params_pf - set initial values for pf mailbox
444 * @hw: pointer to the HW structure
446 * Initializes the hw->mbx struct to correct values for pf mailbox
448 s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
450 struct e1000_mbx_info *mbx = &hw->mbx;
455 mbx->size = E1000_VFMAILBOX_SIZE;
457 mbx->ops.read = igb_read_mbx_pf;
458 mbx->ops.write = igb_write_mbx_pf;
459 mbx->ops.read_posted = igb_read_posted_mbx;
460 mbx->ops.write_posted = igb_write_posted_mbx;
461 mbx->ops.check_for_msg = igb_check_for_msg_pf;
462 mbx->ops.check_for_ack = igb_check_for_ack_pf;
463 mbx->ops.check_for_rst = igb_check_for_rst_pf;
464 mbx->ops.unlock = igb_release_mbx_lock_pf;
466 mbx->stats.msgs_tx = 0;
467 mbx->stats.msgs_rx = 0;