1 /*******************************************************************************
3 Intel(R) Gigabit Ethernet Linux driver
4 Copyright(c) 2007-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 "e1000_mbx.h"
31 * e1000_null_mbx_check_for_flag - No-op function, return 0
32 * @hw: pointer to the HW structure
34 static s32 e1000_null_mbx_check_for_flag(struct e1000_hw E1000_UNUSEDARG *hw,
35 u16 E1000_UNUSEDARG mbx_id)
37 DEBUGFUNC("e1000_null_mbx_check_flag");
43 * e1000_null_mbx_transact - No-op function, return 0
44 * @hw: pointer to the HW structure
46 static s32 e1000_null_mbx_transact(struct e1000_hw E1000_UNUSEDARG *hw,
47 u32 E1000_UNUSEDARG *msg,
48 u16 E1000_UNUSEDARG size,
49 u16 E1000_UNUSEDARG mbx_id)
51 DEBUGFUNC("e1000_null_mbx_rw_msg");
57 * e1000_read_mbx - Reads a message from the mailbox
58 * @hw: pointer to the HW structure
59 * @msg: The message buffer
60 * @size: Length of buffer
61 * @mbx_id: id of mailbox to read
63 * returns SUCCESS if it successfully read message from buffer
65 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
67 struct e1000_mbx_info *mbx = &hw->mbx;
68 s32 ret_val = -E1000_ERR_MBX;
70 DEBUGFUNC("e1000_read_mbx");
72 /* limit read to size of mailbox */
77 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
83 * e1000_write_mbx - Write a message to the mailbox
84 * @hw: pointer to the HW structure
85 * @msg: The message buffer
86 * @size: Length of buffer
87 * @mbx_id: id of mailbox to write
89 * returns SUCCESS if it successfully copied message into the buffer
91 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
93 struct e1000_mbx_info *mbx = &hw->mbx;
94 s32 ret_val = E1000_SUCCESS;
96 DEBUGFUNC("e1000_write_mbx");
99 ret_val = -E1000_ERR_MBX;
101 else if (mbx->ops.write)
102 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
108 * e1000_check_for_msg - checks to see if someone sent us mail
109 * @hw: pointer to the HW structure
110 * @mbx_id: id of mailbox to check
112 * returns SUCCESS if the Status bit was found or else ERR_MBX
114 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
116 struct e1000_mbx_info *mbx = &hw->mbx;
117 s32 ret_val = -E1000_ERR_MBX;
119 DEBUGFUNC("e1000_check_for_msg");
121 if (mbx->ops.check_for_msg)
122 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
128 * e1000_check_for_ack - checks to see if someone sent us ACK
129 * @hw: pointer to the HW structure
130 * @mbx_id: id of mailbox to check
132 * returns SUCCESS if the Status bit was found or else ERR_MBX
134 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
136 struct e1000_mbx_info *mbx = &hw->mbx;
137 s32 ret_val = -E1000_ERR_MBX;
139 DEBUGFUNC("e1000_check_for_ack");
141 if (mbx->ops.check_for_ack)
142 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
148 * e1000_check_for_rst - checks to see if other side has reset
149 * @hw: pointer to the HW structure
150 * @mbx_id: id of mailbox to check
152 * returns SUCCESS if the Status bit was found or else ERR_MBX
154 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
156 struct e1000_mbx_info *mbx = &hw->mbx;
157 s32 ret_val = -E1000_ERR_MBX;
159 DEBUGFUNC("e1000_check_for_rst");
161 if (mbx->ops.check_for_rst)
162 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
168 * e1000_poll_for_msg - Wait for message notification
169 * @hw: pointer to the HW structure
170 * @mbx_id: id of mailbox to write
172 * returns SUCCESS if it successfully received a message notification
174 static s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
176 struct e1000_mbx_info *mbx = &hw->mbx;
177 int countdown = mbx->timeout;
179 DEBUGFUNC("e1000_poll_for_msg");
181 if (!countdown || !mbx->ops.check_for_msg)
184 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
188 usec_delay(mbx->usec_delay);
191 /* if we failed, all future posted messages fail until reset */
195 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
199 * e1000_poll_for_ack - Wait for message acknowledgement
200 * @hw: pointer to the HW structure
201 * @mbx_id: id of mailbox to write
203 * returns SUCCESS if it successfully received a message acknowledgement
205 static s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
207 struct e1000_mbx_info *mbx = &hw->mbx;
208 int countdown = mbx->timeout;
210 DEBUGFUNC("e1000_poll_for_ack");
212 if (!countdown || !mbx->ops.check_for_ack)
215 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
219 usec_delay(mbx->usec_delay);
222 /* if we failed, all future posted messages fail until reset */
226 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
230 * e1000_read_posted_mbx - Wait for message notification and receive message
231 * @hw: pointer to the HW structure
232 * @msg: The message buffer
233 * @size: Length of buffer
234 * @mbx_id: id of mailbox to write
236 * returns SUCCESS if it successfully received a message notification and
237 * copied it into the receive buffer.
239 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
241 struct e1000_mbx_info *mbx = &hw->mbx;
242 s32 ret_val = -E1000_ERR_MBX;
244 DEBUGFUNC("e1000_read_posted_mbx");
249 ret_val = e1000_poll_for_msg(hw, mbx_id);
251 /* if ack received read message, otherwise we timed out */
253 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
259 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
260 * @hw: pointer to the HW structure
261 * @msg: The message buffer
262 * @size: Length of buffer
263 * @mbx_id: id of mailbox to write
265 * returns SUCCESS if it successfully copied message into the buffer and
266 * received an ack to that message within delay * timeout period
268 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
270 struct e1000_mbx_info *mbx = &hw->mbx;
271 s32 ret_val = -E1000_ERR_MBX;
273 DEBUGFUNC("e1000_write_posted_mbx");
275 /* exit if either we can't write or there isn't a defined timeout */
276 if (!mbx->ops.write || !mbx->timeout)
280 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
282 /* if msg sent wait until we receive an ack */
284 ret_val = e1000_poll_for_ack(hw, mbx_id);
290 * e1000_init_mbx_ops_generic - Initialize mbx function pointers
291 * @hw: pointer to the HW structure
293 * Sets the function pointers to no-op functions
295 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
297 struct e1000_mbx_info *mbx = &hw->mbx;
298 mbx->ops.init_params = e1000_null_ops_generic;
299 mbx->ops.read = e1000_null_mbx_transact;
300 mbx->ops.write = e1000_null_mbx_transact;
301 mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
302 mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
303 mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
304 mbx->ops.read_posted = e1000_read_posted_mbx;
305 mbx->ops.write_posted = e1000_write_posted_mbx;
308 static s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
310 u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
311 s32 ret_val = -E1000_ERR_MBX;
313 if (mbvficr & mask) {
314 ret_val = E1000_SUCCESS;
315 E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
322 * e1000_check_for_msg_pf - checks to see if the VF has sent mail
323 * @hw: pointer to the HW structure
324 * @vf_number: the VF index
326 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
328 static s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
330 s32 ret_val = -E1000_ERR_MBX;
332 DEBUGFUNC("e1000_check_for_msg_pf");
334 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
335 ret_val = E1000_SUCCESS;
336 hw->mbx.stats.reqs++;
343 * e1000_check_for_ack_pf - checks to see if the VF has ACKed
344 * @hw: pointer to the HW structure
345 * @vf_number: the VF index
347 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
349 static s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
351 s32 ret_val = -E1000_ERR_MBX;
353 DEBUGFUNC("e1000_check_for_ack_pf");
355 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
356 ret_val = E1000_SUCCESS;
357 hw->mbx.stats.acks++;
364 * e1000_check_for_rst_pf - checks to see if the VF has reset
365 * @hw: pointer to the HW structure
366 * @vf_number: the VF index
368 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
370 static s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
372 u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
373 s32 ret_val = -E1000_ERR_MBX;
375 DEBUGFUNC("e1000_check_for_rst_pf");
377 if (vflre & (1 << vf_number)) {
378 ret_val = E1000_SUCCESS;
379 E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
380 hw->mbx.stats.rsts++;
387 * e1000_obtain_mbx_lock_pf - obtain mailbox lock
388 * @hw: pointer to the HW structure
389 * @vf_number: the VF index
391 * return SUCCESS if we obtained the mailbox lock
393 static s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
395 s32 ret_val = -E1000_ERR_MBX;
398 DEBUGFUNC("e1000_obtain_mbx_lock_pf");
400 /* Take ownership of the buffer */
401 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
403 /* reserve mailbox for vf use */
404 p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
405 if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
406 ret_val = E1000_SUCCESS;
412 * e1000_write_mbx_pf - Places a message in the mailbox
413 * @hw: pointer to the HW structure
414 * @msg: The message buffer
415 * @size: Length of buffer
416 * @vf_number: the VF index
418 * returns SUCCESS if it successfully copied message into the buffer
420 static s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
426 DEBUGFUNC("e1000_write_mbx_pf");
428 /* lock the mailbox to prevent pf/vf race condition */
429 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
433 /* flush msg and acks as we are overwriting the message buffer */
434 e1000_check_for_msg_pf(hw, vf_number);
435 e1000_check_for_ack_pf(hw, vf_number);
437 /* copy the caller specified message to the mailbox memory buffer */
438 for (i = 0; i < size; i++)
439 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
441 /* Interrupt VF to tell it a message has been sent and release buffer*/
442 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
445 hw->mbx.stats.msgs_tx++;
453 * e1000_read_mbx_pf - Read a message from the mailbox
454 * @hw: pointer to the HW structure
455 * @msg: The message buffer
456 * @size: Length of buffer
457 * @vf_number: the VF index
459 * This function copies a message from the mailbox buffer to the caller's
460 * memory buffer. The presumption is that the caller knows that there was
461 * a message due to a VF request so no polling for message is needed.
463 static s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
469 DEBUGFUNC("e1000_read_mbx_pf");
471 /* lock the mailbox to prevent pf/vf race condition */
472 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
476 /* copy the message to the mailbox memory buffer */
477 for (i = 0; i < size; i++)
478 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
480 /* Acknowledge the message and release buffer */
481 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
484 hw->mbx.stats.msgs_rx++;
491 * e1000_init_mbx_params_pf - set initial values for pf mailbox
492 * @hw: pointer to the HW structure
494 * Initializes the hw->mbx struct to correct values for pf mailbox
496 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
498 struct e1000_mbx_info *mbx = &hw->mbx;
500 switch (hw->mac.type) {
507 mbx->size = E1000_VFMAILBOX_SIZE;
509 mbx->ops.read = e1000_read_mbx_pf;
510 mbx->ops.write = e1000_write_mbx_pf;
511 mbx->ops.read_posted = e1000_read_posted_mbx;
512 mbx->ops.write_posted = e1000_write_posted_mbx;
513 mbx->ops.check_for_msg = e1000_check_for_msg_pf;
514 mbx->ops.check_for_ack = e1000_check_for_ack_pf;
515 mbx->ops.check_for_rst = e1000_check_for_rst_pf;
517 mbx->stats.msgs_tx = 0;
518 mbx->stats.msgs_rx = 0;
523 return E1000_SUCCESS;