pvrusb2: reduce stack usage pvr2_eeprom_analyze()
[linux/fpc-iii.git] / drivers / net / ethernet / intel / igb / e1000_mbx.c
blob00e263f0c030f9d7ef86dbdaedc8f2f30473f94d
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)
37 struct e1000_mbx_info *mbx = &hw->mbx;
38 s32 ret_val = -E1000_ERR_MBX;
40 /* limit read to size of mailbox */
41 if (size > mbx->size)
42 size = mbx->size;
44 if (mbx->ops.read)
45 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
47 return ret_val;
50 /**
51 * igb_write_mbx - Write a message to the mailbox
52 * @hw: pointer to the HW structure
53 * @msg: The message buffer
54 * @size: Length of buffer
55 * @mbx_id: id of mailbox to write
57 * returns SUCCESS if it successfully copied message into the buffer
58 **/
59 s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
61 struct e1000_mbx_info *mbx = &hw->mbx;
62 s32 ret_val = 0;
64 if (size > mbx->size)
65 ret_val = -E1000_ERR_MBX;
67 else if (mbx->ops.write)
68 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
70 return ret_val;
73 /**
74 * igb_check_for_msg - checks to see if someone sent us mail
75 * @hw: pointer to the HW structure
76 * @mbx_id: id of mailbox to check
78 * returns SUCCESS if the Status bit was found or else ERR_MBX
79 **/
80 s32 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
82 struct e1000_mbx_info *mbx = &hw->mbx;
83 s32 ret_val = -E1000_ERR_MBX;
85 if (mbx->ops.check_for_msg)
86 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
88 return ret_val;
91 /**
92 * igb_check_for_ack - checks to see if someone sent us ACK
93 * @hw: pointer to the HW structure
94 * @mbx_id: id of mailbox to check
96 * returns SUCCESS if the Status bit was found or else ERR_MBX
97 **/
98 s32 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
100 struct e1000_mbx_info *mbx = &hw->mbx;
101 s32 ret_val = -E1000_ERR_MBX;
103 if (mbx->ops.check_for_ack)
104 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
106 return ret_val;
110 * igb_check_for_rst - checks to see if other side has reset
111 * @hw: pointer to the HW structure
112 * @mbx_id: id of mailbox to check
114 * returns SUCCESS if the Status bit was found or else ERR_MBX
116 s32 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
118 struct e1000_mbx_info *mbx = &hw->mbx;
119 s32 ret_val = -E1000_ERR_MBX;
121 if (mbx->ops.check_for_rst)
122 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
124 return ret_val;
128 * igb_poll_for_msg - Wait for message notification
129 * @hw: pointer to the HW structure
130 * @mbx_id: id of mailbox to write
132 * returns SUCCESS if it successfully received a message notification
134 static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
136 struct e1000_mbx_info *mbx = &hw->mbx;
137 int countdown = mbx->timeout;
139 if (!countdown || !mbx->ops.check_for_msg)
140 goto out;
142 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
143 countdown--;
144 if (!countdown)
145 break;
146 udelay(mbx->usec_delay);
149 /* if we failed, all future posted messages fail until reset */
150 if (!countdown)
151 mbx->timeout = 0;
152 out:
153 return countdown ? 0 : -E1000_ERR_MBX;
157 * igb_poll_for_ack - Wait for message acknowledgement
158 * @hw: pointer to the HW structure
159 * @mbx_id: id of mailbox to write
161 * returns SUCCESS if it successfully received a message acknowledgement
163 static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
165 struct e1000_mbx_info *mbx = &hw->mbx;
166 int countdown = mbx->timeout;
168 if (!countdown || !mbx->ops.check_for_ack)
169 goto out;
171 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
172 countdown--;
173 if (!countdown)
174 break;
175 udelay(mbx->usec_delay);
178 /* if we failed, all future posted messages fail until reset */
179 if (!countdown)
180 mbx->timeout = 0;
181 out:
182 return countdown ? 0 : -E1000_ERR_MBX;
186 * igb_read_posted_mbx - Wait for message notification and receive message
187 * @hw: pointer to the HW structure
188 * @msg: The message buffer
189 * @size: Length of buffer
190 * @mbx_id: id of mailbox to write
192 * returns SUCCESS if it successfully received a message notification and
193 * copied it into the receive buffer.
195 static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
196 u16 mbx_id)
198 struct e1000_mbx_info *mbx = &hw->mbx;
199 s32 ret_val = -E1000_ERR_MBX;
201 if (!mbx->ops.read)
202 goto out;
204 ret_val = igb_poll_for_msg(hw, mbx_id);
206 if (!ret_val)
207 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
208 out:
209 return ret_val;
213 * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
214 * @hw: pointer to the HW structure
215 * @msg: The message buffer
216 * @size: Length of buffer
217 * @mbx_id: id of mailbox to write
219 * returns SUCCESS if it successfully copied message into the buffer and
220 * received an ack to that message within delay * timeout period
222 static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
223 u16 mbx_id)
225 struct e1000_mbx_info *mbx = &hw->mbx;
226 s32 ret_val = -E1000_ERR_MBX;
228 /* exit if either we can't write or there isn't a defined timeout */
229 if (!mbx->ops.write || !mbx->timeout)
230 goto out;
232 /* send msg */
233 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
235 /* if msg sent wait until we receive an ack */
236 if (!ret_val)
237 ret_val = igb_poll_for_ack(hw, mbx_id);
238 out:
239 return ret_val;
242 static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
244 u32 mbvficr = rd32(E1000_MBVFICR);
245 s32 ret_val = -E1000_ERR_MBX;
247 if (mbvficr & mask) {
248 ret_val = 0;
249 wr32(E1000_MBVFICR, mask);
252 return ret_val;
256 * igb_check_for_msg_pf - checks to see if the VF has sent mail
257 * @hw: pointer to the HW structure
258 * @vf_number: the VF index
260 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
262 static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
264 s32 ret_val = -E1000_ERR_MBX;
266 if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
267 ret_val = 0;
268 hw->mbx.stats.reqs++;
271 return ret_val;
275 * igb_check_for_ack_pf - checks to see if the VF has ACKed
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_ack_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_VFACK_VF1 << vf_number)) {
286 ret_val = 0;
287 hw->mbx.stats.acks++;
290 return ret_val;
294 * igb_check_for_rst_pf - checks to see if the VF has reset
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_rst_pf(struct e1000_hw *hw, u16 vf_number)
302 u32 vflre = rd32(E1000_VFLRE);
303 s32 ret_val = -E1000_ERR_MBX;
305 if (vflre & BIT(vf_number)) {
306 ret_val = 0;
307 wr32(E1000_VFLRE, BIT(vf_number));
308 hw->mbx.stats.rsts++;
311 return ret_val;
315 * igb_obtain_mbx_lock_pf - obtain mailbox lock
316 * @hw: pointer to the HW structure
317 * @vf_number: the VF index
319 * return SUCCESS if we obtained the mailbox lock
321 static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
323 s32 ret_val = -E1000_ERR_MBX;
324 u32 p2v_mailbox;
325 int count = 10;
327 do {
328 /* Take ownership of the buffer */
329 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
331 /* reserve mailbox for vf use */
332 p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
333 if (p2v_mailbox & E1000_P2VMAILBOX_PFU) {
334 ret_val = 0;
335 break;
337 udelay(1000);
338 } while (count-- > 0);
340 return ret_val;
344 * igb_write_mbx_pf - Places a message in the mailbox
345 * @hw: pointer to the HW structure
346 * @msg: The message buffer
347 * @size: Length of buffer
348 * @vf_number: the VF index
350 * returns SUCCESS if it successfully copied message into the buffer
352 static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
353 u16 vf_number)
355 s32 ret_val;
356 u16 i;
358 /* lock the mailbox to prevent pf/vf race condition */
359 ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
360 if (ret_val)
361 goto out_no_write;
363 /* flush msg and acks as we are overwriting the message buffer */
364 igb_check_for_msg_pf(hw, vf_number);
365 igb_check_for_ack_pf(hw, vf_number);
367 /* copy the caller specified message to the mailbox memory buffer */
368 for (i = 0; i < size; i++)
369 array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
371 /* Interrupt VF to tell it a message has been sent and release buffer*/
372 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
374 /* update stats */
375 hw->mbx.stats.msgs_tx++;
377 out_no_write:
378 return ret_val;
383 * igb_read_mbx_pf - Read a message from 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 * This function copies a message from the mailbox buffer to the caller's
390 * memory buffer. The presumption is that the caller knows that there was
391 * a message due to a VF request so no polling for message is needed.
393 static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
394 u16 vf_number)
396 s32 ret_val;
397 u16 i;
399 /* lock the mailbox to prevent pf/vf race condition */
400 ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
401 if (ret_val)
402 goto out_no_read;
404 /* copy the message to the mailbox memory buffer */
405 for (i = 0; i < size; i++)
406 msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
408 /* Acknowledge the message and release buffer */
409 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
411 /* update stats */
412 hw->mbx.stats.msgs_rx++;
414 out_no_read:
415 return ret_val;
419 * e1000_init_mbx_params_pf - set initial values for pf mailbox
420 * @hw: pointer to the HW structure
422 * Initializes the hw->mbx struct to correct values for pf mailbox
424 s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
426 struct e1000_mbx_info *mbx = &hw->mbx;
428 mbx->timeout = 0;
429 mbx->usec_delay = 0;
431 mbx->size = E1000_VFMAILBOX_SIZE;
433 mbx->ops.read = igb_read_mbx_pf;
434 mbx->ops.write = igb_write_mbx_pf;
435 mbx->ops.read_posted = igb_read_posted_mbx;
436 mbx->ops.write_posted = igb_write_posted_mbx;
437 mbx->ops.check_for_msg = igb_check_for_msg_pf;
438 mbx->ops.check_for_ack = igb_check_for_ack_pf;
439 mbx->ops.check_for_rst = igb_check_for_rst_pf;
441 mbx->stats.msgs_tx = 0;
442 mbx->stats.msgs_rx = 0;
443 mbx->stats.reqs = 0;
444 mbx->stats.acks = 0;
445 mbx->stats.rsts = 0;
447 return 0;