1 /* Intel(R) Gigabit Ethernet Linux driver
2 * Copyright(c) 2007-2014 Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, see <http://www.gnu.org/licenses/>.
16 * The full GNU General Public License is included in this distribution in
17 * the file called "COPYING".
19 * Contact Information:
20 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
21 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 #include "e1000_mbx.h"
27 * igb_read_mbx - Reads a message from the mailbox
28 * @hw: pointer to the HW structure
29 * @msg: The message buffer
30 * @size: Length of buffer
31 * @mbx_id: id of mailbox to read
33 * returns SUCCESS if it successfully read message from buffer
35 s32
igb_read_mbx(struct e1000_hw
*hw
, u32
*msg
, u16 size
, u16 mbx_id
,
38 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
39 s32 ret_val
= -E1000_ERR_MBX
;
41 /* limit read to size of mailbox */
46 ret_val
= mbx
->ops
.read(hw
, msg
, size
, mbx_id
, unlock
);
52 * igb_write_mbx - Write a message to the mailbox
53 * @hw: pointer to the HW structure
54 * @msg: The message buffer
55 * @size: Length of buffer
56 * @mbx_id: id of mailbox to write
58 * returns SUCCESS if it successfully copied message into the buffer
60 s32
igb_write_mbx(struct e1000_hw
*hw
, u32
*msg
, u16 size
, u16 mbx_id
)
62 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
66 ret_val
= -E1000_ERR_MBX
;
68 else if (mbx
->ops
.write
)
69 ret_val
= mbx
->ops
.write(hw
, msg
, size
, mbx_id
);
75 * igb_check_for_msg - checks to see if someone sent us mail
76 * @hw: pointer to the HW structure
77 * @mbx_id: id of mailbox to check
79 * returns SUCCESS if the Status bit was found or else ERR_MBX
81 s32
igb_check_for_msg(struct e1000_hw
*hw
, u16 mbx_id
)
83 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
84 s32 ret_val
= -E1000_ERR_MBX
;
86 if (mbx
->ops
.check_for_msg
)
87 ret_val
= mbx
->ops
.check_for_msg(hw
, mbx_id
);
93 * igb_check_for_ack - checks to see if someone sent us ACK
94 * @hw: pointer to the HW structure
95 * @mbx_id: id of mailbox to check
97 * returns SUCCESS if the Status bit was found or else ERR_MBX
99 s32
igb_check_for_ack(struct e1000_hw
*hw
, u16 mbx_id
)
101 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
102 s32 ret_val
= -E1000_ERR_MBX
;
104 if (mbx
->ops
.check_for_ack
)
105 ret_val
= mbx
->ops
.check_for_ack(hw
, mbx_id
);
111 * igb_check_for_rst - checks to see if other side has reset
112 * @hw: pointer to the HW structure
113 * @mbx_id: id of mailbox to check
115 * returns SUCCESS if the Status bit was found or else ERR_MBX
117 s32
igb_check_for_rst(struct e1000_hw
*hw
, u16 mbx_id
)
119 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
120 s32 ret_val
= -E1000_ERR_MBX
;
122 if (mbx
->ops
.check_for_rst
)
123 ret_val
= mbx
->ops
.check_for_rst(hw
, mbx_id
);
129 * igb_unlock_mbx - unlock the mailbox
130 * @hw: pointer to the HW structure
131 * @mbx_id: id of mailbox to check
133 * returns SUCCESS if the mailbox was unlocked or else ERR_MBX
135 s32
igb_unlock_mbx(struct e1000_hw
*hw
, u16 mbx_id
)
137 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
138 s32 ret_val
= -E1000_ERR_MBX
;
141 ret_val
= mbx
->ops
.unlock(hw
, mbx_id
);
147 * igb_poll_for_msg - Wait for message notification
148 * @hw: pointer to the HW structure
149 * @mbx_id: id of mailbox to write
151 * returns SUCCESS if it successfully received a message notification
153 static s32
igb_poll_for_msg(struct e1000_hw
*hw
, u16 mbx_id
)
155 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
156 int countdown
= mbx
->timeout
;
158 if (!countdown
|| !mbx
->ops
.check_for_msg
)
161 while (countdown
&& mbx
->ops
.check_for_msg(hw
, mbx_id
)) {
165 udelay(mbx
->usec_delay
);
168 /* if we failed, all future posted messages fail until reset */
172 return countdown
? 0 : -E1000_ERR_MBX
;
176 * igb_poll_for_ack - Wait for message acknowledgement
177 * @hw: pointer to the HW structure
178 * @mbx_id: id of mailbox to write
180 * returns SUCCESS if it successfully received a message acknowledgement
182 static s32
igb_poll_for_ack(struct e1000_hw
*hw
, u16 mbx_id
)
184 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
185 int countdown
= mbx
->timeout
;
187 if (!countdown
|| !mbx
->ops
.check_for_ack
)
190 while (countdown
&& mbx
->ops
.check_for_ack(hw
, mbx_id
)) {
194 udelay(mbx
->usec_delay
);
197 /* if we failed, all future posted messages fail until reset */
201 return countdown
? 0 : -E1000_ERR_MBX
;
205 * igb_read_posted_mbx - Wait for message notification and receive message
206 * @hw: pointer to the HW structure
207 * @msg: The message buffer
208 * @size: Length of buffer
209 * @mbx_id: id of mailbox to write
211 * returns SUCCESS if it successfully received a message notification and
212 * copied it into the receive buffer.
214 static s32
igb_read_posted_mbx(struct e1000_hw
*hw
, u32
*msg
, u16 size
,
217 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
218 s32 ret_val
= -E1000_ERR_MBX
;
223 ret_val
= igb_poll_for_msg(hw
, mbx_id
);
226 ret_val
= mbx
->ops
.read(hw
, msg
, size
, mbx_id
, true);
232 * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
233 * @hw: pointer to the HW structure
234 * @msg: The message buffer
235 * @size: Length of buffer
236 * @mbx_id: id of mailbox to write
238 * returns SUCCESS if it successfully copied message into the buffer and
239 * received an ack to that message within delay * timeout period
241 static s32
igb_write_posted_mbx(struct e1000_hw
*hw
, u32
*msg
, u16 size
,
244 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
245 s32 ret_val
= -E1000_ERR_MBX
;
247 /* exit if either we can't write or there isn't a defined timeout */
248 if (!mbx
->ops
.write
|| !mbx
->timeout
)
252 ret_val
= mbx
->ops
.write(hw
, msg
, size
, mbx_id
);
254 /* if msg sent wait until we receive an ack */
256 ret_val
= igb_poll_for_ack(hw
, mbx_id
);
261 static s32
igb_check_for_bit_pf(struct e1000_hw
*hw
, u32 mask
)
263 u32 mbvficr
= rd32(E1000_MBVFICR
);
264 s32 ret_val
= -E1000_ERR_MBX
;
266 if (mbvficr
& mask
) {
268 wr32(E1000_MBVFICR
, mask
);
275 * igb_check_for_msg_pf - checks to see if the VF has sent mail
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_msg_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_VFREQ_VF1
<< vf_number
)) {
287 hw
->mbx
.stats
.reqs
++;
294 * igb_check_for_ack_pf - checks to see if the VF has ACKed
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_ack_pf(struct e1000_hw
*hw
, u16 vf_number
)
302 s32 ret_val
= -E1000_ERR_MBX
;
304 if (!igb_check_for_bit_pf(hw
, E1000_MBVFICR_VFACK_VF1
<< vf_number
)) {
306 hw
->mbx
.stats
.acks
++;
313 * igb_check_for_rst_pf - checks to see if the VF has reset
314 * @hw: pointer to the HW structure
315 * @vf_number: the VF index
317 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
319 static s32
igb_check_for_rst_pf(struct e1000_hw
*hw
, u16 vf_number
)
321 u32 vflre
= rd32(E1000_VFLRE
);
322 s32 ret_val
= -E1000_ERR_MBX
;
324 if (vflre
& BIT(vf_number
)) {
326 wr32(E1000_VFLRE
, BIT(vf_number
));
327 hw
->mbx
.stats
.rsts
++;
334 * igb_obtain_mbx_lock_pf - obtain mailbox lock
335 * @hw: pointer to the HW structure
336 * @vf_number: the VF index
338 * return SUCCESS if we obtained the mailbox lock
340 static s32
igb_obtain_mbx_lock_pf(struct e1000_hw
*hw
, u16 vf_number
)
342 s32 ret_val
= -E1000_ERR_MBX
;
347 /* Take ownership of the buffer */
348 wr32(E1000_P2VMAILBOX(vf_number
), E1000_P2VMAILBOX_PFU
);
350 /* reserve mailbox for vf use */
351 p2v_mailbox
= rd32(E1000_P2VMAILBOX(vf_number
));
352 if (p2v_mailbox
& E1000_P2VMAILBOX_PFU
) {
357 } while (count
-- > 0);
363 * igb_release_mbx_lock_pf - release mailbox lock
364 * @hw: pointer to the HW structure
365 * @vf_number: the VF index
367 * return SUCCESS if we released the mailbox lock
369 static s32
igb_release_mbx_lock_pf(struct e1000_hw
*hw
, u16 vf_number
)
373 /* drop PF lock of mailbox, if set */
374 p2v_mailbox
= rd32(E1000_P2VMAILBOX(vf_number
));
375 if (p2v_mailbox
& E1000_P2VMAILBOX_PFU
)
376 wr32(E1000_P2VMAILBOX(vf_number
),
377 p2v_mailbox
& ~E1000_P2VMAILBOX_PFU
);
383 * igb_write_mbx_pf - Places a message in the mailbox
384 * @hw: pointer to the HW structure
385 * @msg: The message buffer
386 * @size: Length of buffer
387 * @vf_number: the VF index
389 * returns SUCCESS if it successfully copied message into the buffer
391 static s32
igb_write_mbx_pf(struct e1000_hw
*hw
, u32
*msg
, u16 size
,
397 /* lock the mailbox to prevent pf/vf race condition */
398 ret_val
= igb_obtain_mbx_lock_pf(hw
, vf_number
);
402 /* flush msg and acks as we are overwriting the message buffer */
403 igb_check_for_msg_pf(hw
, vf_number
);
404 igb_check_for_ack_pf(hw
, vf_number
);
406 /* copy the caller specified message to the mailbox memory buffer */
407 for (i
= 0; i
< size
; i
++)
408 array_wr32(E1000_VMBMEM(vf_number
), i
, msg
[i
]);
410 /* Interrupt VF to tell it a message has been sent and release buffer*/
411 wr32(E1000_P2VMAILBOX(vf_number
), E1000_P2VMAILBOX_STS
);
414 hw
->mbx
.stats
.msgs_tx
++;
422 * igb_read_mbx_pf - Read a message from the mailbox
423 * @hw: pointer to the HW structure
424 * @msg: The message buffer
425 * @size: Length of buffer
426 * @vf_number: the VF index
427 * @unlock: unlock the mailbox when done?
429 * This function copies a message from the mailbox buffer to the caller's
430 * memory buffer. The presumption is that the caller knows that there was
431 * a message due to a VF request so no polling for message is needed.
433 static s32
igb_read_mbx_pf(struct e1000_hw
*hw
, u32
*msg
, u16 size
,
434 u16 vf_number
, bool unlock
)
439 /* lock the mailbox to prevent pf/vf race condition */
440 ret_val
= igb_obtain_mbx_lock_pf(hw
, vf_number
);
444 /* copy the message to the mailbox memory buffer */
445 for (i
= 0; i
< size
; i
++)
446 msg
[i
] = array_rd32(E1000_VMBMEM(vf_number
), i
);
448 /* Acknowledge the message and release mailbox lock (or not) */
450 wr32(E1000_P2VMAILBOX(vf_number
), E1000_P2VMAILBOX_ACK
);
452 wr32(E1000_P2VMAILBOX(vf_number
),
453 E1000_P2VMAILBOX_ACK
| E1000_P2VMAILBOX_PFU
);
456 hw
->mbx
.stats
.msgs_rx
++;
463 * e1000_init_mbx_params_pf - set initial values for pf mailbox
464 * @hw: pointer to the HW structure
466 * Initializes the hw->mbx struct to correct values for pf mailbox
468 s32
igb_init_mbx_params_pf(struct e1000_hw
*hw
)
470 struct e1000_mbx_info
*mbx
= &hw
->mbx
;
475 mbx
->size
= E1000_VFMAILBOX_SIZE
;
477 mbx
->ops
.read
= igb_read_mbx_pf
;
478 mbx
->ops
.write
= igb_write_mbx_pf
;
479 mbx
->ops
.read_posted
= igb_read_posted_mbx
;
480 mbx
->ops
.write_posted
= igb_write_posted_mbx
;
481 mbx
->ops
.check_for_msg
= igb_check_for_msg_pf
;
482 mbx
->ops
.check_for_ack
= igb_check_for_ack_pf
;
483 mbx
->ops
.check_for_rst
= igb_check_for_rst_pf
;
484 mbx
->ops
.unlock
= igb_release_mbx_lock_pf
;
486 mbx
->stats
.msgs_tx
= 0;
487 mbx
->stats
.msgs_rx
= 0;