Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / ethernet / intel / igb / e1000_mbx.c
blobbffd58f7b2a1d8e1e49850f37dbf97916a876772
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
11 * more details.
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"
26 /**
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
34 **/
35 s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id,
36 bool unlock)
38 struct e1000_mbx_info *mbx = &hw->mbx;
39 s32 ret_val = -E1000_ERR_MBX;
41 /* limit read to size of mailbox */
42 if (size > mbx->size)
43 size = mbx->size;
45 if (mbx->ops.read)
46 ret_val = mbx->ops.read(hw, msg, size, mbx_id, unlock);
48 return ret_val;
51 /**
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
59 **/
60 s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
62 struct e1000_mbx_info *mbx = &hw->mbx;
63 s32 ret_val = 0;
65 if (size > mbx->size)
66 ret_val = -E1000_ERR_MBX;
68 else if (mbx->ops.write)
69 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
71 return ret_val;
74 /**
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
80 **/
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);
89 return ret_val;
92 /**
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
98 **/
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);
107 return ret_val;
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);
125 return ret_val;
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;
140 if (mbx->ops.unlock)
141 ret_val = mbx->ops.unlock(hw, mbx_id);
143 return ret_val;
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)
159 goto out;
161 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
162 countdown--;
163 if (!countdown)
164 break;
165 udelay(mbx->usec_delay);
168 /* if we failed, all future posted messages fail until reset */
169 if (!countdown)
170 mbx->timeout = 0;
171 out:
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)
188 goto out;
190 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
191 countdown--;
192 if (!countdown)
193 break;
194 udelay(mbx->usec_delay);
197 /* if we failed, all future posted messages fail until reset */
198 if (!countdown)
199 mbx->timeout = 0;
200 out:
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,
215 u16 mbx_id)
217 struct e1000_mbx_info *mbx = &hw->mbx;
218 s32 ret_val = -E1000_ERR_MBX;
220 if (!mbx->ops.read)
221 goto out;
223 ret_val = igb_poll_for_msg(hw, mbx_id);
225 if (!ret_val)
226 ret_val = mbx->ops.read(hw, msg, size, mbx_id, true);
227 out:
228 return ret_val;
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,
242 u16 mbx_id)
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)
249 goto out;
251 /* send msg */
252 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
254 /* if msg sent wait until we receive an ack */
255 if (!ret_val)
256 ret_val = igb_poll_for_ack(hw, mbx_id);
257 out:
258 return ret_val;
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) {
267 ret_val = 0;
268 wr32(E1000_MBVFICR, mask);
271 return ret_val;
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)) {
286 ret_val = 0;
287 hw->mbx.stats.reqs++;
290 return ret_val;
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)) {
305 ret_val = 0;
306 hw->mbx.stats.acks++;
309 return ret_val;
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)) {
325 ret_val = 0;
326 wr32(E1000_VFLRE, BIT(vf_number));
327 hw->mbx.stats.rsts++;
330 return ret_val;
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;
343 u32 p2v_mailbox;
344 int count = 10;
346 do {
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) {
353 ret_val = 0;
354 break;
356 udelay(1000);
357 } while (count-- > 0);
359 return ret_val;
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)
371 u32 p2v_mailbox;
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);
379 return 0;
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,
392 u16 vf_number)
394 s32 ret_val;
395 u16 i;
397 /* lock the mailbox to prevent pf/vf race condition */
398 ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
399 if (ret_val)
400 goto out_no_write;
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);
413 /* update stats */
414 hw->mbx.stats.msgs_tx++;
416 out_no_write:
417 return ret_val;
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)
436 s32 ret_val;
437 u16 i;
439 /* lock the mailbox to prevent pf/vf race condition */
440 ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
441 if (ret_val)
442 goto out_no_read;
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) */
449 if (unlock)
450 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
451 else
452 wr32(E1000_P2VMAILBOX(vf_number),
453 E1000_P2VMAILBOX_ACK | E1000_P2VMAILBOX_PFU);
455 /* update stats */
456 hw->mbx.stats.msgs_rx++;
458 out_no_read:
459 return ret_val;
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;
472 mbx->timeout = 0;
473 mbx->usec_delay = 0;
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;
488 mbx->stats.reqs = 0;
489 mbx->stats.acks = 0;
490 mbx->stats.rsts = 0;
492 return 0;