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
12 * @unlock: skip locking or not
14 * returns SUCCESS if it successfully read message from buffer
16 s32
igb_read_mbx(struct e1000_hw
*hw
, u32
*msg
, u16 size
, u16 mbx_id
,
19 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
20 s32 ret_val
= -E1000_ERR_MBX
;
22 /* limit read to size of mailbox */
27 ret_val
= mbx
->ops
.read(hw
, msg
, size
, mbx_id
, unlock
);
33 * igb_write_mbx - Write a message to the mailbox
34 * @hw: pointer to the HW structure
35 * @msg: The message buffer
36 * @size: Length of buffer
37 * @mbx_id: id of mailbox to write
39 * returns SUCCESS if it successfully copied message into the buffer
41 s32
igb_write_mbx(struct e1000_hw
*hw
, u32
*msg
, u16 size
, u16 mbx_id
)
43 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
47 ret_val
= -E1000_ERR_MBX
;
49 else if (mbx
->ops
.write
)
50 ret_val
= mbx
->ops
.write(hw
, msg
, size
, mbx_id
);
56 * igb_check_for_msg - checks to see if someone sent us mail
57 * @hw: pointer to the HW structure
58 * @mbx_id: id of mailbox to check
60 * returns SUCCESS if the Status bit was found or else ERR_MBX
62 s32
igb_check_for_msg(struct e1000_hw
*hw
, u16 mbx_id
)
64 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
65 s32 ret_val
= -E1000_ERR_MBX
;
67 if (mbx
->ops
.check_for_msg
)
68 ret_val
= mbx
->ops
.check_for_msg(hw
, mbx_id
);
74 * igb_check_for_ack - checks to see if someone sent us ACK
75 * @hw: pointer to the HW structure
76 * @mbx_id: id of mailbox to check
78 * returns SUCCESS if the Status bit was found or else ERR_MBX
80 s32
igb_check_for_ack(struct e1000_hw
*hw
, u16 mbx_id
)
82 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
83 s32 ret_val
= -E1000_ERR_MBX
;
85 if (mbx
->ops
.check_for_ack
)
86 ret_val
= mbx
->ops
.check_for_ack(hw
, mbx_id
);
92 * igb_check_for_rst - checks to see if other side has reset
93 * @hw: pointer to the HW structure
94 * @mbx_id: id of mailbox to check
96 * returns SUCCESS if the Status bit was found or else ERR_MBX
98 s32
igb_check_for_rst(struct e1000_hw
*hw
, u16 mbx_id
)
100 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
101 s32 ret_val
= -E1000_ERR_MBX
;
103 if (mbx
->ops
.check_for_rst
)
104 ret_val
= mbx
->ops
.check_for_rst(hw
, mbx_id
);
110 * igb_unlock_mbx - unlock the mailbox
111 * @hw: pointer to the HW structure
112 * @mbx_id: id of mailbox to check
114 * returns SUCCESS if the mailbox was unlocked or else ERR_MBX
116 s32
igb_unlock_mbx(struct e1000_hw
*hw
, u16 mbx_id
)
118 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
119 s32 ret_val
= -E1000_ERR_MBX
;
122 ret_val
= mbx
->ops
.unlock(hw
, mbx_id
);
128 * igb_poll_for_msg - Wait for message notification
129 * @hw: pointer to the HW structure
130 * @mbx_id: id of mailbox to write
132 * returns SUCCESS if it successfully received a message notification
134 static s32
igb_poll_for_msg(struct e1000_hw
*hw
, u16 mbx_id
)
136 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
137 int countdown
= mbx
->timeout
;
139 if (!countdown
|| !mbx
->ops
.check_for_msg
)
142 while (countdown
&& mbx
->ops
.check_for_msg(hw
, mbx_id
)) {
146 udelay(mbx
->usec_delay
);
149 /* if we failed, all future posted messages fail until reset */
153 return countdown
? 0 : -E1000_ERR_MBX
;
157 * igb_poll_for_ack - Wait for message acknowledgement
158 * @hw: pointer to the HW structure
159 * @mbx_id: id of mailbox to write
161 * returns SUCCESS if it successfully received a message acknowledgement
163 static s32
igb_poll_for_ack(struct e1000_hw
*hw
, u16 mbx_id
)
165 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
166 int countdown
= mbx
->timeout
;
168 if (!countdown
|| !mbx
->ops
.check_for_ack
)
171 while (countdown
&& mbx
->ops
.check_for_ack(hw
, mbx_id
)) {
175 udelay(mbx
->usec_delay
);
178 /* if we failed, all future posted messages fail until reset */
182 return countdown
? 0 : -E1000_ERR_MBX
;
186 * igb_read_posted_mbx - Wait for message notification and receive message
187 * @hw: pointer to the HW structure
188 * @msg: The message buffer
189 * @size: Length of buffer
190 * @mbx_id: id of mailbox to write
192 * returns SUCCESS if it successfully received a message notification and
193 * copied it into the receive buffer.
195 static s32
igb_read_posted_mbx(struct e1000_hw
*hw
, u32
*msg
, u16 size
,
198 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
199 s32 ret_val
= -E1000_ERR_MBX
;
204 ret_val
= igb_poll_for_msg(hw
, mbx_id
);
207 ret_val
= mbx
->ops
.read(hw
, msg
, size
, mbx_id
, true);
213 * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
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
219 * returns SUCCESS if it successfully copied message into the buffer and
220 * received an ack to that message within delay * timeout period
222 static s32
igb_write_posted_mbx(struct e1000_hw
*hw
, u32
*msg
, u16 size
,
225 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
226 s32 ret_val
= -E1000_ERR_MBX
;
228 /* exit if either we can't write or there isn't a defined timeout */
229 if (!mbx
->ops
.write
|| !mbx
->timeout
)
233 ret_val
= mbx
->ops
.write(hw
, msg
, size
, mbx_id
);
235 /* if msg sent wait until we receive an ack */
237 ret_val
= igb_poll_for_ack(hw
, mbx_id
);
242 static s32
igb_check_for_bit_pf(struct e1000_hw
*hw
, u32 mask
)
244 u32 mbvficr
= rd32(E1000_MBVFICR
);
245 s32 ret_val
= -E1000_ERR_MBX
;
247 if (mbvficr
& mask
) {
249 wr32(E1000_MBVFICR
, mask
);
256 * igb_check_for_msg_pf - checks to see if the VF has sent mail
257 * @hw: pointer to the HW structure
258 * @vf_number: the VF index
260 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
262 static s32
igb_check_for_msg_pf(struct e1000_hw
*hw
, u16 vf_number
)
264 s32 ret_val
= -E1000_ERR_MBX
;
266 if (!igb_check_for_bit_pf(hw
, E1000_MBVFICR_VFREQ_VF1
<< vf_number
)) {
268 hw
->mbx
.stats
.reqs
++;
275 * igb_check_for_ack_pf - checks to see if the VF has ACKed
276 * @hw: pointer to the HW structure
277 * @vf_number: the VF index
279 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
281 static s32
igb_check_for_ack_pf(struct e1000_hw
*hw
, u16 vf_number
)
283 s32 ret_val
= -E1000_ERR_MBX
;
285 if (!igb_check_for_bit_pf(hw
, E1000_MBVFICR_VFACK_VF1
<< vf_number
)) {
287 hw
->mbx
.stats
.acks
++;
294 * igb_check_for_rst_pf - checks to see if the VF has reset
295 * @hw: pointer to the HW structure
296 * @vf_number: the VF index
298 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
300 static s32
igb_check_for_rst_pf(struct e1000_hw
*hw
, u16 vf_number
)
302 u32 vflre
= rd32(E1000_VFLRE
);
303 s32 ret_val
= -E1000_ERR_MBX
;
305 if (vflre
& BIT(vf_number
)) {
307 wr32(E1000_VFLRE
, BIT(vf_number
));
308 hw
->mbx
.stats
.rsts
++;
315 * igb_obtain_mbx_lock_pf - obtain mailbox lock
316 * @hw: pointer to the HW structure
317 * @vf_number: the VF index
319 * return SUCCESS if we obtained the mailbox lock
321 static s32
igb_obtain_mbx_lock_pf(struct e1000_hw
*hw
, u16 vf_number
)
323 s32 ret_val
= -E1000_ERR_MBX
;
328 /* Take ownership of the buffer */
329 wr32(E1000_P2VMAILBOX(vf_number
), E1000_P2VMAILBOX_PFU
);
331 /* reserve mailbox for vf use */
332 p2v_mailbox
= rd32(E1000_P2VMAILBOX(vf_number
));
333 if (p2v_mailbox
& E1000_P2VMAILBOX_PFU
) {
338 } while (count
-- > 0);
344 * igb_release_mbx_lock_pf - release mailbox lock
345 * @hw: pointer to the HW structure
346 * @vf_number: the VF index
348 * return SUCCESS if we released the mailbox lock
350 static s32
igb_release_mbx_lock_pf(struct e1000_hw
*hw
, u16 vf_number
)
354 /* drop PF lock of mailbox, if set */
355 p2v_mailbox
= rd32(E1000_P2VMAILBOX(vf_number
));
356 if (p2v_mailbox
& E1000_P2VMAILBOX_PFU
)
357 wr32(E1000_P2VMAILBOX(vf_number
),
358 p2v_mailbox
& ~E1000_P2VMAILBOX_PFU
);
364 * igb_write_mbx_pf - Places a message in the mailbox
365 * @hw: pointer to the HW structure
366 * @msg: The message buffer
367 * @size: Length of buffer
368 * @vf_number: the VF index
370 * returns SUCCESS if it successfully copied message into the buffer
372 static s32
igb_write_mbx_pf(struct e1000_hw
*hw
, u32
*msg
, u16 size
,
378 /* lock the mailbox to prevent pf/vf race condition */
379 ret_val
= igb_obtain_mbx_lock_pf(hw
, vf_number
);
383 /* flush msg and acks as we are overwriting the message buffer */
384 igb_check_for_msg_pf(hw
, vf_number
);
385 igb_check_for_ack_pf(hw
, vf_number
);
387 /* copy the caller specified message to the mailbox memory buffer */
388 for (i
= 0; i
< size
; i
++)
389 array_wr32(E1000_VMBMEM(vf_number
), i
, msg
[i
]);
391 /* Interrupt VF to tell it a message has been sent and release buffer*/
392 wr32(E1000_P2VMAILBOX(vf_number
), E1000_P2VMAILBOX_STS
);
395 hw
->mbx
.stats
.msgs_tx
++;
403 * igb_read_mbx_pf - Read a message from the mailbox
404 * @hw: pointer to the HW structure
405 * @msg: The message buffer
406 * @size: Length of buffer
407 * @vf_number: the VF index
408 * @unlock: unlock the mailbox when done?
410 * This function copies a message from the mailbox buffer to the caller's
411 * memory buffer. The presumption is that the caller knows that there was
412 * a message due to a VF request so no polling for message is needed.
414 static s32
igb_read_mbx_pf(struct e1000_hw
*hw
, u32
*msg
, u16 size
,
415 u16 vf_number
, bool unlock
)
420 /* lock the mailbox to prevent pf/vf race condition */
421 ret_val
= igb_obtain_mbx_lock_pf(hw
, vf_number
);
425 /* copy the message to the mailbox memory buffer */
426 for (i
= 0; i
< size
; i
++)
427 msg
[i
] = array_rd32(E1000_VMBMEM(vf_number
), i
);
429 /* Acknowledge the message and release mailbox lock (or not) */
431 wr32(E1000_P2VMAILBOX(vf_number
), E1000_P2VMAILBOX_ACK
);
433 wr32(E1000_P2VMAILBOX(vf_number
),
434 E1000_P2VMAILBOX_ACK
| E1000_P2VMAILBOX_PFU
);
437 hw
->mbx
.stats
.msgs_rx
++;
444 * e1000_init_mbx_params_pf - set initial values for pf mailbox
445 * @hw: pointer to the HW structure
447 * Initializes the hw->mbx struct to correct values for pf mailbox
449 s32
igb_init_mbx_params_pf(struct e1000_hw
*hw
)
451 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
456 mbx
->size
= E1000_VFMAILBOX_SIZE
;
458 mbx
->ops
.read
= igb_read_mbx_pf
;
459 mbx
->ops
.write
= igb_write_mbx_pf
;
460 mbx
->ops
.read_posted
= igb_read_posted_mbx
;
461 mbx
->ops
.write_posted
= igb_write_posted_mbx
;
462 mbx
->ops
.check_for_msg
= igb_check_for_msg_pf
;
463 mbx
->ops
.check_for_ack
= igb_check_for_ack_pf
;
464 mbx
->ops
.check_for_rst
= igb_check_for_rst_pf
;
465 mbx
->ops
.unlock
= igb_release_mbx_lock_pf
;
467 mbx
->stats
.msgs_tx
= 0;
468 mbx
->stats
.msgs_rx
= 0;