1 /*******************************************************************************
3 Intel(R) Gigabit Ethernet Linux driver
4 Copyright(c) 2007-2012 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 *hw, u16 mbx_id)
36 DEBUGFUNC("e1000_null_mbx_check_flag");
42 * e1000_null_mbx_transact - No-op function, return 0
43 * @hw: pointer to the HW structure
45 static s32 e1000_null_mbx_transact(struct e1000_hw *hw, u32 *msg, u16 size,
48 DEBUGFUNC("e1000_null_mbx_rw_msg");
54 * e1000_read_mbx - Reads a message from the mailbox
55 * @hw: pointer to the HW structure
56 * @msg: The message buffer
57 * @size: Length of buffer
58 * @mbx_id: id of mailbox to read
60 * returns SUCCESS if it successfuly read message from buffer
62 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
64 struct e1000_mbx_info *mbx = &hw->mbx;
65 s32 ret_val = -E1000_ERR_MBX;
67 DEBUGFUNC("e1000_read_mbx");
69 /* limit read to size of mailbox */
74 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
80 * e1000_write_mbx - Write a message to the mailbox
81 * @hw: pointer to the HW structure
82 * @msg: The message buffer
83 * @size: Length of buffer
84 * @mbx_id: id of mailbox to write
86 * returns SUCCESS if it successfully copied message into the buffer
88 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
90 struct e1000_mbx_info *mbx = &hw->mbx;
91 s32 ret_val = E1000_SUCCESS;
93 DEBUGFUNC("e1000_write_mbx");
96 ret_val = -E1000_ERR_MBX;
98 else if (mbx->ops.write)
99 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
105 * e1000_check_for_msg - checks to see if someone sent us mail
106 * @hw: pointer to the HW structure
107 * @mbx_id: id of mailbox to check
109 * returns SUCCESS if the Status bit was found or else ERR_MBX
111 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
113 struct e1000_mbx_info *mbx = &hw->mbx;
114 s32 ret_val = -E1000_ERR_MBX;
116 DEBUGFUNC("e1000_check_for_msg");
118 if (mbx->ops.check_for_msg)
119 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
125 * e1000_check_for_ack - checks to see if someone sent us ACK
126 * @hw: pointer to the HW structure
127 * @mbx_id: id of mailbox to check
129 * returns SUCCESS if the Status bit was found or else ERR_MBX
131 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
133 struct e1000_mbx_info *mbx = &hw->mbx;
134 s32 ret_val = -E1000_ERR_MBX;
136 DEBUGFUNC("e1000_check_for_ack");
138 if (mbx->ops.check_for_ack)
139 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
145 * e1000_check_for_rst - checks to see if other side has reset
146 * @hw: pointer to the HW structure
147 * @mbx_id: id of mailbox to check
149 * returns SUCCESS if the Status bit was found or else ERR_MBX
151 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
153 struct e1000_mbx_info *mbx = &hw->mbx;
154 s32 ret_val = -E1000_ERR_MBX;
156 DEBUGFUNC("e1000_check_for_rst");
158 if (mbx->ops.check_for_rst)
159 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
165 * e1000_poll_for_msg - Wait for message notification
166 * @hw: pointer to the HW structure
167 * @mbx_id: id of mailbox to write
169 * returns SUCCESS if it successfully received a message notification
171 static s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
173 struct e1000_mbx_info *mbx = &hw->mbx;
174 int countdown = mbx->timeout;
176 DEBUGFUNC("e1000_poll_for_msg");
178 if (!countdown || !mbx->ops.check_for_msg)
181 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
185 usec_delay(mbx->usec_delay);
188 /* if we failed, all future posted messages fail until reset */
192 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
196 * e1000_poll_for_ack - Wait for message acknowledgement
197 * @hw: pointer to the HW structure
198 * @mbx_id: id of mailbox to write
200 * returns SUCCESS if it successfully received a message acknowledgement
202 static s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
204 struct e1000_mbx_info *mbx = &hw->mbx;
205 int countdown = mbx->timeout;
207 DEBUGFUNC("e1000_poll_for_ack");
209 if (!countdown || !mbx->ops.check_for_ack)
212 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
216 usec_delay(mbx->usec_delay);
219 /* if we failed, all future posted messages fail until reset */
223 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
227 * e1000_read_posted_mbx - Wait for message notification and receive message
228 * @hw: pointer to the HW structure
229 * @msg: The message buffer
230 * @size: Length of buffer
231 * @mbx_id: id of mailbox to write
233 * returns SUCCESS if it successfully received a message notification and
234 * copied it into the receive buffer.
236 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
238 struct e1000_mbx_info *mbx = &hw->mbx;
239 s32 ret_val = -E1000_ERR_MBX;
241 DEBUGFUNC("e1000_read_posted_mbx");
246 ret_val = e1000_poll_for_msg(hw, mbx_id);
248 /* if ack received read message, otherwise we timed out */
250 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
256 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
257 * @hw: pointer to the HW structure
258 * @msg: The message buffer
259 * @size: Length of buffer
260 * @mbx_id: id of mailbox to write
262 * returns SUCCESS if it successfully copied message into the buffer and
263 * received an ack to that message within delay * timeout period
265 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
267 struct e1000_mbx_info *mbx = &hw->mbx;
268 s32 ret_val = -E1000_ERR_MBX;
270 DEBUGFUNC("e1000_write_posted_mbx");
272 /* exit if either we can't write or there isn't a defined timeout */
273 if (!mbx->ops.write || !mbx->timeout)
277 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
279 /* if msg sent wait until we receive an ack */
281 ret_val = e1000_poll_for_ack(hw, mbx_id);
287 * e1000_init_mbx_ops_generic - Initialize mbx function pointers
288 * @hw: pointer to the HW structure
290 * Sets the function pointers to no-op functions
292 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
294 struct e1000_mbx_info *mbx = &hw->mbx;
295 mbx->ops.init_params = e1000_null_ops_generic;
296 mbx->ops.read = e1000_null_mbx_transact;
297 mbx->ops.write = e1000_null_mbx_transact;
298 mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
299 mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
300 mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
301 mbx->ops.read_posted = e1000_read_posted_mbx;
302 mbx->ops.write_posted = e1000_write_posted_mbx;
305 static s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
307 u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
308 s32 ret_val = -E1000_ERR_MBX;
310 if (mbvficr & mask) {
311 ret_val = E1000_SUCCESS;
312 E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
319 * e1000_check_for_msg_pf - checks to see if the VF has sent mail
320 * @hw: pointer to the HW structure
321 * @vf_number: the VF index
323 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
325 static s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
327 s32 ret_val = -E1000_ERR_MBX;
329 DEBUGFUNC("e1000_check_for_msg_pf");
331 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
332 ret_val = E1000_SUCCESS;
333 hw->mbx.stats.reqs++;
340 * e1000_check_for_ack_pf - checks to see if the VF has ACKed
341 * @hw: pointer to the HW structure
342 * @vf_number: the VF index
344 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
346 static s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
348 s32 ret_val = -E1000_ERR_MBX;
350 DEBUGFUNC("e1000_check_for_ack_pf");
352 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
353 ret_val = E1000_SUCCESS;
354 hw->mbx.stats.acks++;
361 * e1000_check_for_rst_pf - checks to see if the VF has reset
362 * @hw: pointer to the HW structure
363 * @vf_number: the VF index
365 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
367 static s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
369 u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
370 s32 ret_val = -E1000_ERR_MBX;
372 DEBUGFUNC("e1000_check_for_rst_pf");
374 if (vflre & (1 << vf_number)) {
375 ret_val = E1000_SUCCESS;
376 E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
377 hw->mbx.stats.rsts++;
384 * e1000_obtain_mbx_lock_pf - obtain mailbox lock
385 * @hw: pointer to the HW structure
386 * @vf_number: the VF index
388 * return SUCCESS if we obtained the mailbox lock
390 static s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
392 s32 ret_val = -E1000_ERR_MBX;
395 DEBUGFUNC("e1000_obtain_mbx_lock_pf");
397 /* Take ownership of the buffer */
398 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
400 /* reserve mailbox for vf use */
401 p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
402 if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
403 ret_val = E1000_SUCCESS;
409 * e1000_write_mbx_pf - Places a message in the mailbox
410 * @hw: pointer to the HW structure
411 * @msg: The message buffer
412 * @size: Length of buffer
413 * @vf_number: the VF index
415 * returns SUCCESS if it successfully copied message into the buffer
417 static s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
423 DEBUGFUNC("e1000_write_mbx_pf");
425 /* lock the mailbox to prevent pf/vf race condition */
426 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
430 /* flush msg and acks as we are overwriting the message buffer */
431 e1000_check_for_msg_pf(hw, vf_number);
432 e1000_check_for_ack_pf(hw, vf_number);
434 /* copy the caller specified message to the mailbox memory buffer */
435 for (i = 0; i < size; i++)
436 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
438 /* Interrupt VF to tell it a message has been sent and release buffer*/
439 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
442 hw->mbx.stats.msgs_tx++;
450 * e1000_read_mbx_pf - Read a message from the mailbox
451 * @hw: pointer to the HW structure
452 * @msg: The message buffer
453 * @size: Length of buffer
454 * @vf_number: the VF index
456 * This function copies a message from the mailbox buffer to the caller's
457 * memory buffer. The presumption is that the caller knows that there was
458 * a message due to a VF request so no polling for message is needed.
460 static s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
466 DEBUGFUNC("e1000_read_mbx_pf");
468 /* lock the mailbox to prevent pf/vf race condition */
469 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
473 /* copy the message to the mailbox memory buffer */
474 for (i = 0; i < size; i++)
475 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
477 /* Acknowledge the message and release buffer */
478 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
481 hw->mbx.stats.msgs_rx++;
488 * e1000_init_mbx_params_pf - set initial values for pf mailbox
489 * @hw: pointer to the HW structure
491 * Initializes the hw->mbx struct to correct values for pf mailbox
493 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
495 struct e1000_mbx_info *mbx = &hw->mbx;
497 switch (hw->mac.type) {
503 mbx->size = E1000_VFMAILBOX_SIZE;
505 mbx->ops.read = e1000_read_mbx_pf;
506 mbx->ops.write = e1000_write_mbx_pf;
507 mbx->ops.read_posted = e1000_read_posted_mbx;
508 mbx->ops.write_posted = e1000_write_posted_mbx;
509 mbx->ops.check_for_msg = e1000_check_for_msg_pf;
510 mbx->ops.check_for_ack = e1000_check_for_ack_pf;
511 mbx->ops.check_for_rst = e1000_check_for_rst_pf;
513 mbx->stats.msgs_tx = 0;
514 mbx->stats.msgs_rx = 0;
519 return E1000_SUCCESS;