1 /*******************************************************************************
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2013 Intel Corporation.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *******************************************************************************/
28 #include <linux/pci.h>
29 #include <linux/delay.h>
31 #include "ixgbe_mbx.h"
34 * ixgbe_read_mbx - Reads a message from the mailbox
35 * @hw: pointer to the HW structure
36 * @msg: The message buffer
37 * @size: Length of buffer
38 * @mbx_id: id of mailbox to read
40 * returns SUCCESS if it successfully read message from buffer
42 s32
ixgbe_read_mbx(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
, u16 mbx_id
)
44 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
45 s32 ret_val
= IXGBE_ERR_MBX
;
47 /* limit read to size of mailbox */
52 ret_val
= mbx
->ops
.read(hw
, msg
, size
, mbx_id
);
58 * ixgbe_write_mbx - Write a message to the mailbox
59 * @hw: pointer to the HW structure
60 * @msg: The message buffer
61 * @size: Length of buffer
62 * @mbx_id: id of mailbox to write
64 * returns SUCCESS if it successfully copied message into the buffer
66 s32
ixgbe_write_mbx(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
, u16 mbx_id
)
68 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
72 ret_val
= IXGBE_ERR_MBX
;
74 else if (mbx
->ops
.write
)
75 ret_val
= mbx
->ops
.write(hw
, msg
, size
, mbx_id
);
81 * ixgbe_check_for_msg - checks to see if someone sent us mail
82 * @hw: pointer to the HW structure
83 * @mbx_id: id of mailbox to check
85 * returns SUCCESS if the Status bit was found or else ERR_MBX
87 s32
ixgbe_check_for_msg(struct ixgbe_hw
*hw
, u16 mbx_id
)
89 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
90 s32 ret_val
= IXGBE_ERR_MBX
;
92 if (mbx
->ops
.check_for_msg
)
93 ret_val
= mbx
->ops
.check_for_msg(hw
, mbx_id
);
99 * ixgbe_check_for_ack - checks to see if someone sent us ACK
100 * @hw: pointer to the HW structure
101 * @mbx_id: id of mailbox to check
103 * returns SUCCESS if the Status bit was found or else ERR_MBX
105 s32
ixgbe_check_for_ack(struct ixgbe_hw
*hw
, u16 mbx_id
)
107 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
108 s32 ret_val
= IXGBE_ERR_MBX
;
110 if (mbx
->ops
.check_for_ack
)
111 ret_val
= mbx
->ops
.check_for_ack(hw
, mbx_id
);
117 * ixgbe_check_for_rst - checks to see if other side has reset
118 * @hw: pointer to the HW structure
119 * @mbx_id: id of mailbox to check
121 * returns SUCCESS if the Status bit was found or else ERR_MBX
123 s32
ixgbe_check_for_rst(struct ixgbe_hw
*hw
, u16 mbx_id
)
125 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
126 s32 ret_val
= IXGBE_ERR_MBX
;
128 if (mbx
->ops
.check_for_rst
)
129 ret_val
= mbx
->ops
.check_for_rst(hw
, mbx_id
);
135 * ixgbe_poll_for_msg - Wait for message notification
136 * @hw: pointer to the HW structure
137 * @mbx_id: id of mailbox to write
139 * returns SUCCESS if it successfully received a message notification
141 static s32
ixgbe_poll_for_msg(struct ixgbe_hw
*hw
, u16 mbx_id
)
143 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
144 int countdown
= mbx
->timeout
;
146 if (!countdown
|| !mbx
->ops
.check_for_msg
)
149 while (countdown
&& mbx
->ops
.check_for_msg(hw
, mbx_id
)) {
153 udelay(mbx
->usec_delay
);
157 return countdown
? 0 : IXGBE_ERR_MBX
;
161 * ixgbe_poll_for_ack - Wait for message acknowledgement
162 * @hw: pointer to the HW structure
163 * @mbx_id: id of mailbox to write
165 * returns SUCCESS if it successfully received a message acknowledgement
167 static s32
ixgbe_poll_for_ack(struct ixgbe_hw
*hw
, u16 mbx_id
)
169 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
170 int countdown
= mbx
->timeout
;
172 if (!countdown
|| !mbx
->ops
.check_for_ack
)
175 while (countdown
&& mbx
->ops
.check_for_ack(hw
, mbx_id
)) {
179 udelay(mbx
->usec_delay
);
183 return countdown
? 0 : IXGBE_ERR_MBX
;
187 * ixgbe_read_posted_mbx - Wait for message notification and receive message
188 * @hw: pointer to the HW structure
189 * @msg: The message buffer
190 * @size: Length of buffer
191 * @mbx_id: id of mailbox to write
193 * returns SUCCESS if it successfully received a message notification and
194 * copied it into the receive buffer.
196 static s32
ixgbe_read_posted_mbx(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
,
199 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
200 s32 ret_val
= IXGBE_ERR_MBX
;
205 ret_val
= ixgbe_poll_for_msg(hw
, mbx_id
);
207 /* if ack received read message, otherwise we timed out */
209 ret_val
= mbx
->ops
.read(hw
, msg
, size
, mbx_id
);
215 * ixgbe_write_posted_mbx - Write a message to the mailbox, wait for ack
216 * @hw: pointer to the HW structure
217 * @msg: The message buffer
218 * @size: Length of buffer
219 * @mbx_id: id of mailbox to write
221 * returns SUCCESS if it successfully copied message into the buffer and
222 * received an ack to that message within delay * timeout period
224 static s32
ixgbe_write_posted_mbx(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
,
227 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
228 s32 ret_val
= IXGBE_ERR_MBX
;
230 /* exit if either we can't write or there isn't a defined timeout */
231 if (!mbx
->ops
.write
|| !mbx
->timeout
)
235 ret_val
= mbx
->ops
.write(hw
, msg
, size
, mbx_id
);
237 /* if msg sent wait until we receive an ack */
239 ret_val
= ixgbe_poll_for_ack(hw
, mbx_id
);
244 static s32
ixgbe_check_for_bit_pf(struct ixgbe_hw
*hw
, u32 mask
, s32 index
)
246 u32 mbvficr
= IXGBE_READ_REG(hw
, IXGBE_MBVFICR(index
));
247 s32 ret_val
= IXGBE_ERR_MBX
;
249 if (mbvficr
& mask
) {
251 IXGBE_WRITE_REG(hw
, IXGBE_MBVFICR(index
), mask
);
258 * ixgbe_check_for_msg_pf - checks to see if the VF has sent mail
259 * @hw: pointer to the HW structure
260 * @vf_number: the VF index
262 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
264 static s32
ixgbe_check_for_msg_pf(struct ixgbe_hw
*hw
, u16 vf_number
)
266 s32 ret_val
= IXGBE_ERR_MBX
;
267 s32 index
= IXGBE_MBVFICR_INDEX(vf_number
);
268 u32 vf_bit
= vf_number
% 16;
270 if (!ixgbe_check_for_bit_pf(hw
, IXGBE_MBVFICR_VFREQ_VF1
<< vf_bit
,
273 hw
->mbx
.stats
.reqs
++;
280 * ixgbe_check_for_ack_pf - checks to see if the VF has ACKed
281 * @hw: pointer to the HW structure
282 * @vf_number: the VF index
284 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
286 static s32
ixgbe_check_for_ack_pf(struct ixgbe_hw
*hw
, u16 vf_number
)
288 s32 ret_val
= IXGBE_ERR_MBX
;
289 s32 index
= IXGBE_MBVFICR_INDEX(vf_number
);
290 u32 vf_bit
= vf_number
% 16;
292 if (!ixgbe_check_for_bit_pf(hw
, IXGBE_MBVFICR_VFACK_VF1
<< vf_bit
,
295 hw
->mbx
.stats
.acks
++;
302 * ixgbe_check_for_rst_pf - checks to see if the VF has reset
303 * @hw: pointer to the HW structure
304 * @vf_number: the VF index
306 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
308 static s32
ixgbe_check_for_rst_pf(struct ixgbe_hw
*hw
, u16 vf_number
)
310 u32 reg_offset
= (vf_number
< 32) ? 0 : 1;
311 u32 vf_shift
= vf_number
% 32;
313 s32 ret_val
= IXGBE_ERR_MBX
;
315 switch (hw
->mac
.type
) {
316 case ixgbe_mac_82599EB
:
317 vflre
= IXGBE_READ_REG(hw
, IXGBE_VFLRE(reg_offset
));
320 vflre
= IXGBE_READ_REG(hw
, IXGBE_VFLREC(reg_offset
));
326 if (vflre
& (1 << vf_shift
)) {
328 IXGBE_WRITE_REG(hw
, IXGBE_VFLREC(reg_offset
), (1 << vf_shift
));
329 hw
->mbx
.stats
.rsts
++;
336 * ixgbe_obtain_mbx_lock_pf - obtain mailbox lock
337 * @hw: pointer to the HW structure
338 * @vf_number: the VF index
340 * return SUCCESS if we obtained the mailbox lock
342 static s32
ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw
*hw
, u16 vf_number
)
344 s32 ret_val
= IXGBE_ERR_MBX
;
347 /* Take ownership of the buffer */
348 IXGBE_WRITE_REG(hw
, IXGBE_PFMAILBOX(vf_number
), IXGBE_PFMAILBOX_PFU
);
350 /* reserve mailbox for vf use */
351 p2v_mailbox
= IXGBE_READ_REG(hw
, IXGBE_PFMAILBOX(vf_number
));
352 if (p2v_mailbox
& IXGBE_PFMAILBOX_PFU
)
359 * ixgbe_write_mbx_pf - Places a message in the mailbox
360 * @hw: pointer to the HW structure
361 * @msg: The message buffer
362 * @size: Length of buffer
363 * @vf_number: the VF index
365 * returns SUCCESS if it successfully copied message into the buffer
367 static s32
ixgbe_write_mbx_pf(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
,
373 /* lock the mailbox to prevent pf/vf race condition */
374 ret_val
= ixgbe_obtain_mbx_lock_pf(hw
, vf_number
);
378 /* flush msg and acks as we are overwriting the message buffer */
379 ixgbe_check_for_msg_pf(hw
, vf_number
);
380 ixgbe_check_for_ack_pf(hw
, vf_number
);
382 /* copy the caller specified message to the mailbox memory buffer */
383 for (i
= 0; i
< size
; i
++)
384 IXGBE_WRITE_REG_ARRAY(hw
, IXGBE_PFMBMEM(vf_number
), i
, msg
[i
]);
386 /* Interrupt VF to tell it a message has been sent and release buffer*/
387 IXGBE_WRITE_REG(hw
, IXGBE_PFMAILBOX(vf_number
), IXGBE_PFMAILBOX_STS
);
390 hw
->mbx
.stats
.msgs_tx
++;
398 * ixgbe_read_mbx_pf - Read a message from the mailbox
399 * @hw: pointer to the HW structure
400 * @msg: The message buffer
401 * @size: Length of buffer
402 * @vf_number: the VF index
404 * This function copies a message from the mailbox buffer to the caller's
405 * memory buffer. The presumption is that the caller knows that there was
406 * a message due to a VF request so no polling for message is needed.
408 static s32
ixgbe_read_mbx_pf(struct ixgbe_hw
*hw
, u32
*msg
, u16 size
,
414 /* lock the mailbox to prevent pf/vf race condition */
415 ret_val
= ixgbe_obtain_mbx_lock_pf(hw
, vf_number
);
419 /* copy the message to the mailbox memory buffer */
420 for (i
= 0; i
< size
; i
++)
421 msg
[i
] = IXGBE_READ_REG_ARRAY(hw
, IXGBE_PFMBMEM(vf_number
), i
);
423 /* Acknowledge the message and release buffer */
424 IXGBE_WRITE_REG(hw
, IXGBE_PFMAILBOX(vf_number
), IXGBE_PFMAILBOX_ACK
);
427 hw
->mbx
.stats
.msgs_rx
++;
433 #ifdef CONFIG_PCI_IOV
435 * ixgbe_init_mbx_params_pf - set initial values for pf mailbox
436 * @hw: pointer to the HW structure
438 * Initializes the hw->mbx struct to correct values for pf mailbox
440 void ixgbe_init_mbx_params_pf(struct ixgbe_hw
*hw
)
442 struct ixgbe_mbx_info
*mbx
= &hw
->mbx
;
444 if (hw
->mac
.type
!= ixgbe_mac_82599EB
&&
445 hw
->mac
.type
!= ixgbe_mac_X540
)
451 mbx
->stats
.msgs_tx
= 0;
452 mbx
->stats
.msgs_rx
= 0;
457 mbx
->size
= IXGBE_VFMAILBOX_SIZE
;
459 #endif /* CONFIG_PCI_IOV */
461 struct ixgbe_mbx_operations mbx_ops_generic
= {
462 .read
= ixgbe_read_mbx_pf
,
463 .write
= ixgbe_write_mbx_pf
,
464 .read_posted
= ixgbe_read_posted_mbx
,
465 .write_posted
= ixgbe_write_posted_mbx
,
466 .check_for_msg
= ixgbe_check_for_msg_pf
,
467 .check_for_ack
= ixgbe_check_for_ack_pf
,
468 .check_for_rst
= ixgbe_check_for_rst_pf
,