1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
5 #include <linux/delay.h>
10 * ixgbe_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 SUCCESS if it successfully read message from buffer
18 s32
ixgbe_read_mbx(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
, u16 mbx_id
)
20 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
22 /* limit read to size of mailbox */
29 return mbx
->ops
->read(hw
, msg
, size
, mbx_id
);
33 * ixgbe_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
ixgbe_write_mbx(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
, u16 mbx_id
)
43 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
51 return mbx
->ops
->write(hw
, msg
, size
, mbx_id
);
55 * ixgbe_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
ixgbe_check_for_msg(struct ixgbe_hw
*hw
, u16 mbx_id
)
63 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
68 return mbx
->ops
->check_for_msg(hw
, mbx_id
);
72 * ixgbe_check_for_ack - checks to see if someone sent us ACK
73 * @hw: pointer to the HW structure
74 * @mbx_id: id of mailbox to check
76 * returns SUCCESS if the Status bit was found or else ERR_MBX
78 s32
ixgbe_check_for_ack(struct ixgbe_hw
*hw
, u16 mbx_id
)
80 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
85 return mbx
->ops
->check_for_ack(hw
, mbx_id
);
89 * ixgbe_check_for_rst - checks to see if other side has reset
90 * @hw: pointer to the HW structure
91 * @mbx_id: id of mailbox to check
93 * returns SUCCESS if the Status bit was found or else ERR_MBX
95 s32
ixgbe_check_for_rst(struct ixgbe_hw
*hw
, u16 mbx_id
)
97 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
100 return IXGBE_ERR_MBX
;
102 return mbx
->ops
->check_for_rst(hw
, mbx_id
);
106 * ixgbe_poll_for_msg - Wait for message notification
107 * @hw: pointer to the HW structure
108 * @mbx_id: id of mailbox to write
110 * returns SUCCESS if it successfully received a message notification
112 static s32
ixgbe_poll_for_msg(struct ixgbe_hw
*hw
, u16 mbx_id
)
114 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
115 int countdown
= mbx
->timeout
;
117 if (!countdown
|| !mbx
->ops
)
118 return IXGBE_ERR_MBX
;
120 while (mbx
->ops
->check_for_msg(hw
, mbx_id
)) {
123 return IXGBE_ERR_MBX
;
124 udelay(mbx
->usec_delay
);
131 * ixgbe_poll_for_ack - Wait for message acknowledgement
132 * @hw: pointer to the HW structure
133 * @mbx_id: id of mailbox to write
135 * returns SUCCESS if it successfully received a message acknowledgement
137 static s32
ixgbe_poll_for_ack(struct ixgbe_hw
*hw
, u16 mbx_id
)
139 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
140 int countdown
= mbx
->timeout
;
142 if (!countdown
|| !mbx
->ops
)
143 return IXGBE_ERR_MBX
;
145 while (mbx
->ops
->check_for_ack(hw
, mbx_id
)) {
148 return IXGBE_ERR_MBX
;
149 udelay(mbx
->usec_delay
);
156 * ixgbe_read_posted_mbx - Wait for message notification and receive message
157 * @hw: pointer to the HW structure
158 * @msg: The message buffer
159 * @size: Length of buffer
160 * @mbx_id: id of mailbox to write
162 * returns SUCCESS if it successfully received a message notification and
163 * copied it into the receive buffer.
165 static s32
ixgbe_read_posted_mbx(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
,
168 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
172 return IXGBE_ERR_MBX
;
174 ret_val
= ixgbe_poll_for_msg(hw
, mbx_id
);
178 /* if ack received read message */
179 return mbx
->ops
->read(hw
, msg
, size
, mbx_id
);
183 * ixgbe_write_posted_mbx - Write a message to the mailbox, wait for ack
184 * @hw: pointer to the HW structure
185 * @msg: The message buffer
186 * @size: Length of buffer
187 * @mbx_id: id of mailbox to write
189 * returns SUCCESS if it successfully copied message into the buffer and
190 * received an ack to that message within delay * timeout period
192 static s32
ixgbe_write_posted_mbx(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
,
195 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
198 /* exit if either we can't write or there isn't a defined timeout */
199 if (!mbx
->ops
|| !mbx
->timeout
)
200 return IXGBE_ERR_MBX
;
203 ret_val
= mbx
->ops
->write(hw
, msg
, size
, mbx_id
);
207 /* if msg sent wait until we receive an ack */
208 return ixgbe_poll_for_ack(hw
, mbx_id
);
211 static s32
ixgbe_check_for_bit_pf(struct ixgbe_hw
*hw
, u32 mask
, s32 index
)
213 u32 mbvficr
= IXGBE_READ_REG(hw
, IXGBE_MBVFICR(index
));
215 if (mbvficr
& mask
) {
216 IXGBE_WRITE_REG(hw
, IXGBE_MBVFICR(index
), mask
);
220 return IXGBE_ERR_MBX
;
224 * ixgbe_check_for_msg_pf - checks to see if the VF has sent mail
225 * @hw: pointer to the HW structure
226 * @vf_number: the VF index
228 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
230 static s32
ixgbe_check_for_msg_pf(struct ixgbe_hw
*hw
, u16 vf_number
)
232 s32 index
= IXGBE_MBVFICR_INDEX(vf_number
);
233 u32 vf_bit
= vf_number
% 16;
235 if (!ixgbe_check_for_bit_pf(hw
, IXGBE_MBVFICR_VFREQ_VF1
<< vf_bit
,
237 hw
->mbx
.stats
.reqs
++;
241 return IXGBE_ERR_MBX
;
245 * ixgbe_check_for_ack_pf - checks to see if the VF has ACKed
246 * @hw: pointer to the HW structure
247 * @vf_number: the VF index
249 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
251 static s32
ixgbe_check_for_ack_pf(struct ixgbe_hw
*hw
, u16 vf_number
)
253 s32 index
= IXGBE_MBVFICR_INDEX(vf_number
);
254 u32 vf_bit
= vf_number
% 16;
256 if (!ixgbe_check_for_bit_pf(hw
, IXGBE_MBVFICR_VFACK_VF1
<< vf_bit
,
258 hw
->mbx
.stats
.acks
++;
262 return IXGBE_ERR_MBX
;
266 * ixgbe_check_for_rst_pf - checks to see if the VF has reset
267 * @hw: pointer to the HW structure
268 * @vf_number: the VF index
270 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
272 static s32
ixgbe_check_for_rst_pf(struct ixgbe_hw
*hw
, u16 vf_number
)
274 u32 reg_offset
= (vf_number
< 32) ? 0 : 1;
275 u32 vf_shift
= vf_number
% 32;
278 switch (hw
->mac
.type
) {
279 case ixgbe_mac_82599EB
:
280 vflre
= IXGBE_READ_REG(hw
, IXGBE_VFLRE(reg_offset
));
284 case ixgbe_mac_X550EM_x
:
285 case ixgbe_mac_x550em_a
:
286 vflre
= IXGBE_READ_REG(hw
, IXGBE_VFLREC(reg_offset
));
292 if (vflre
& BIT(vf_shift
)) {
293 IXGBE_WRITE_REG(hw
, IXGBE_VFLREC(reg_offset
), BIT(vf_shift
));
294 hw
->mbx
.stats
.rsts
++;
298 return IXGBE_ERR_MBX
;
302 * ixgbe_obtain_mbx_lock_pf - obtain mailbox lock
303 * @hw: pointer to the HW structure
304 * @vf_number: the VF index
306 * return SUCCESS if we obtained the mailbox lock
308 static s32
ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw
*hw
, u16 vf_number
)
312 /* Take ownership of the buffer */
313 IXGBE_WRITE_REG(hw
, IXGBE_PFMAILBOX(vf_number
), IXGBE_PFMAILBOX_PFU
);
315 /* reserve mailbox for vf use */
316 p2v_mailbox
= IXGBE_READ_REG(hw
, IXGBE_PFMAILBOX(vf_number
));
317 if (p2v_mailbox
& IXGBE_PFMAILBOX_PFU
)
320 return IXGBE_ERR_MBX
;
324 * ixgbe_write_mbx_pf - Places a message in the mailbox
325 * @hw: pointer to the HW structure
326 * @msg: The message buffer
327 * @size: Length of buffer
328 * @vf_number: the VF index
330 * returns SUCCESS if it successfully copied message into the buffer
332 static s32
ixgbe_write_mbx_pf(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
,
338 /* lock the mailbox to prevent pf/vf race condition */
339 ret_val
= ixgbe_obtain_mbx_lock_pf(hw
, vf_number
);
343 /* flush msg and acks as we are overwriting the message buffer */
344 ixgbe_check_for_msg_pf(hw
, vf_number
);
345 ixgbe_check_for_ack_pf(hw
, vf_number
);
347 /* copy the caller specified message to the mailbox memory buffer */
348 for (i
= 0; i
< size
; i
++)
349 IXGBE_WRITE_REG_ARRAY(hw
, IXGBE_PFMBMEM(vf_number
), i
, msg
[i
]);
351 /* Interrupt VF to tell it a message has been sent and release buffer*/
352 IXGBE_WRITE_REG(hw
, IXGBE_PFMAILBOX(vf_number
), IXGBE_PFMAILBOX_STS
);
355 hw
->mbx
.stats
.msgs_tx
++;
361 * ixgbe_read_mbx_pf - Read a message from the mailbox
362 * @hw: pointer to the HW structure
363 * @msg: The message buffer
364 * @size: Length of buffer
365 * @vf_number: the VF index
367 * This function copies a message from the mailbox buffer to the caller's
368 * memory buffer. The presumption is that the caller knows that there was
369 * a message due to a VF request so no polling for message is needed.
371 static s32
ixgbe_read_mbx_pf(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
,
377 /* lock the mailbox to prevent pf/vf race condition */
378 ret_val
= ixgbe_obtain_mbx_lock_pf(hw
, vf_number
);
382 /* copy the message to the mailbox memory buffer */
383 for (i
= 0; i
< size
; i
++)
384 msg
[i
] = IXGBE_READ_REG_ARRAY(hw
, IXGBE_PFMBMEM(vf_number
), i
);
386 /* Acknowledge the message and release buffer */
387 IXGBE_WRITE_REG(hw
, IXGBE_PFMAILBOX(vf_number
), IXGBE_PFMAILBOX_ACK
);
390 hw
->mbx
.stats
.msgs_rx
++;
395 #ifdef CONFIG_PCI_IOV
397 * ixgbe_init_mbx_params_pf - set initial values for pf mailbox
398 * @hw: pointer to the HW structure
400 * Initializes the hw->mbx struct to correct values for pf mailbox
402 void ixgbe_init_mbx_params_pf(struct ixgbe_hw
*hw
)
404 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
406 if (hw
->mac
.type
!= ixgbe_mac_82599EB
&&
407 hw
->mac
.type
!= ixgbe_mac_X550
&&
408 hw
->mac
.type
!= ixgbe_mac_X550EM_x
&&
409 hw
->mac
.type
!= ixgbe_mac_x550em_a
&&
410 hw
->mac
.type
!= ixgbe_mac_X540
)
416 mbx
->stats
.msgs_tx
= 0;
417 mbx
->stats
.msgs_rx
= 0;
422 mbx
->size
= IXGBE_VFMAILBOX_SIZE
;
424 #endif /* CONFIG_PCI_IOV */
426 const struct ixgbe_mbx_operations mbx_ops_generic
= {
427 .read
= ixgbe_read_mbx_pf
,
428 .write
= ixgbe_write_mbx_pf
,
429 .read_posted
= ixgbe_read_posted_mbx
,
430 .write_posted
= ixgbe_write_posted_mbx
,
431 .check_for_msg
= ixgbe_check_for_msg_pf
,
432 .check_for_ack
= ixgbe_check_for_ack_pf
,
433 .check_for_rst
= ixgbe_check_for_rst_pf
,