Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / igbvf / igbvf_mbx.c
1 /*******************************************************************************
2
3   Intel(R) 82576 Virtual Function Linux driver
4   Copyright(c) 1999 - 2008 Intel Corporation.
5
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.
9
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
13   more details.
14
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.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Contact Information:
23   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 FILE_LICENCE ( GPL2_ONLY );
30
31 #include "igbvf_mbx.h"
32
33 /**
34  *  igbvf_poll_for_msg - Wait for message notification
35  *  @hw: pointer to the HW structure
36  *  @mbx_id: id of mailbox to write
37  *
38  *  returns SUCCESS if it successfully received a message notification
39  **/
40 static s32 igbvf_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
41 {
42         struct e1000_mbx_info *mbx = &hw->mbx;
43         int countdown = mbx->timeout;
44
45         DEBUGFUNC("igbvf_poll_for_msg");
46
47         if (!countdown || !mbx->ops.check_for_msg)
48                 goto out;
49
50         while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
51                 countdown--;
52                 if (!countdown)
53                         break;
54                 usec_delay(mbx->usec_delay);
55         }
56
57         /* if we failed, all future posted messages fail until reset */
58         if (!countdown)
59                 mbx->timeout = 0;
60 out:
61         return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
62 }
63
64 /**
65  *  igbvf_poll_for_ack - Wait for message acknowledgement
66  *  @hw: pointer to the HW structure
67  *  @mbx_id: id of mailbox to write
68  *
69  *  returns SUCCESS if it successfully received a message acknowledgement
70  **/
71 static s32 igbvf_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
72 {
73         struct e1000_mbx_info *mbx = &hw->mbx;
74         int countdown = mbx->timeout;
75
76         DEBUGFUNC("igbvf_poll_for_ack");
77
78         if (!countdown || !mbx->ops.check_for_ack)
79                 goto out;
80
81         while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
82                 countdown--;
83                 if (!countdown)
84                         break;
85                 usec_delay(mbx->usec_delay);
86         }
87
88         /* if we failed, all future posted messages fail until reset */
89         if (!countdown)
90                 mbx->timeout = 0;
91 out:
92         return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
93 }
94
95 /**
96  *  igbvf_read_posted_mbx - Wait for message notification and receive message
97  *  @hw: pointer to the HW structure
98  *  @msg: The message buffer
99  *  @size: Length of buffer
100  *  @mbx_id: id of mailbox to write
101  *
102  *  returns SUCCESS if it successfully received a message notification and
103  *  copied it into the receive buffer.
104  **/
105 static s32 igbvf_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
106                                  u16 mbx_id)
107 {
108         struct e1000_mbx_info *mbx = &hw->mbx;
109         s32 ret_val = -E1000_ERR_MBX;
110
111         DEBUGFUNC("igbvf_read_posted_mbx");
112
113         if (!mbx->ops.read)
114                 goto out;
115
116         ret_val = igbvf_poll_for_msg(hw, mbx_id);
117
118         /* if ack received read message, otherwise we timed out */
119         if (!ret_val)
120                 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
121 out:
122         return ret_val;
123 }
124
125 /**
126  *  igbvf_write_posted_mbx - Write a message to the mailbox, wait for ack
127  *  @hw: pointer to the HW structure
128  *  @msg: The message buffer
129  *  @size: Length of buffer
130  *  @mbx_id: id of mailbox to write
131  *
132  *  returns SUCCESS if it successfully copied message into the buffer and
133  *  received an ack to that message within delay * timeout period
134  **/
135 static s32 igbvf_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
136                                   u16 mbx_id)
137 {
138         struct e1000_mbx_info *mbx = &hw->mbx;
139         s32 ret_val = -E1000_ERR_MBX;
140
141         DEBUGFUNC("igbvf_write_posted_mbx");
142
143         /* exit if either we can't write or there isn't a defined timeout */
144         if (!mbx->ops.write || !mbx->timeout)
145                 goto out;
146
147         /* send msg */
148         ret_val = mbx->ops.write(hw, msg, size, mbx_id);
149
150         /* if msg sent wait until we receive an ack */
151         if (!ret_val)
152                 ret_val = igbvf_poll_for_ack(hw, mbx_id);
153 out:
154         return ret_val;
155 }
156
157 /**
158  *  igbvf_init_mbx_ops_generic - Initialize NVM function pointers
159  *  @hw: pointer to the HW structure
160  *
161  *  Setups up the function pointers to no-op functions
162  **/
163 void igbvf_init_mbx_ops_generic(struct e1000_hw *hw)
164 {
165         struct e1000_mbx_info *mbx = &hw->mbx;
166         mbx->ops.read_posted = igbvf_read_posted_mbx;
167         mbx->ops.write_posted = igbvf_write_posted_mbx;
168 }
169
170 /**
171  *  igbvf_read_v2p_mailbox - read v2p mailbox
172  *  @hw: pointer to the HW structure
173  *
174  *  This function is used to read the v2p mailbox without losing the read to
175  *  clear status bits.
176  **/
177 static u32 igbvf_read_v2p_mailbox(struct e1000_hw *hw)
178 {
179         u32 v2p_mailbox = E1000_READ_REG(hw, E1000_V2PMAILBOX(0));
180
181         v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
182         hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
183
184         return v2p_mailbox;
185 }
186
187 /**
188  *  igbvf_check_for_bit_vf - Determine if a status bit was set
189  *  @hw: pointer to the HW structure
190  *  @mask: bitmask for bits to be tested and cleared
191  *
192  *  This function is used to check for the read to clear bits within
193  *  the V2P mailbox.
194  **/
195 static s32 igbvf_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
196 {
197         u32 v2p_mailbox = igbvf_read_v2p_mailbox(hw);
198         s32 ret_val = -E1000_ERR_MBX;
199
200         if (v2p_mailbox & mask)
201                 ret_val = E1000_SUCCESS;
202
203         hw->dev_spec.vf.v2p_mailbox &= ~mask;
204
205         return ret_val;
206 }
207
208 /**
209  *  igbvf_check_for_msg_vf - checks to see if the PF has sent mail
210  *  @hw: pointer to the HW structure
211  *  @mbx_id: id of mailbox to check
212  *
213  *  returns SUCCESS if the PF has set the Status bit or else ERR_MBX
214  **/
215 static s32 igbvf_check_for_msg_vf(struct e1000_hw *hw, u16 mbx_id __unused)
216 {
217         s32 ret_val = -E1000_ERR_MBX;
218
219         DEBUGFUNC("igbvf_check_for_msg_vf");
220
221         if (!igbvf_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
222                 ret_val = E1000_SUCCESS;
223                 hw->mbx.stats.reqs++;
224         }
225
226         return ret_val;
227 }
228
229 /**
230  *  igbvf_check_for_ack_vf - checks to see if the PF has ACK'd
231  *  @hw: pointer to the HW structure
232  *  @mbx_id: id of mailbox to check
233  *
234  *  returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
235  **/
236 static s32 igbvf_check_for_ack_vf(struct e1000_hw *hw, u16 mbx_id __unused)
237 {
238         s32 ret_val = -E1000_ERR_MBX;
239
240         DEBUGFUNC("igbvf_check_for_ack_vf");
241
242         if (!igbvf_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
243                 ret_val = E1000_SUCCESS;
244                 hw->mbx.stats.acks++;
245         }
246
247         return ret_val;
248 }
249
250 /**
251  *  igbvf_check_for_rst_vf - checks to see if the PF has reset
252  *  @hw: pointer to the HW structure
253  *  @mbx_id: id of mailbox to check
254  *
255  *  returns true if the PF has set the reset done bit or else false
256  **/
257 static s32 igbvf_check_for_rst_vf(struct e1000_hw *hw, u16 mbx_id __unused)
258 {
259         s32 ret_val = -E1000_ERR_MBX;
260
261         DEBUGFUNC("igbvf_check_for_rst_vf");
262
263         if (!igbvf_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
264                                          E1000_V2PMAILBOX_RSTI))) {
265                 ret_val = E1000_SUCCESS;
266                 hw->mbx.stats.rsts++;
267         }
268
269         return ret_val;
270 }
271
272 /**
273  *  igbvf_obtain_mbx_lock_vf - obtain mailbox lock
274  *  @hw: pointer to the HW structure
275  *
276  *  return SUCCESS if we obtained the mailbox lock
277  **/
278 static s32 igbvf_obtain_mbx_lock_vf(struct e1000_hw *hw)
279 {
280         s32 ret_val = -E1000_ERR_MBX;
281
282         DEBUGFUNC("igbvf_obtain_mbx_lock_vf");
283
284         /* Take ownership of the buffer */
285         E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
286
287         /* reserve mailbox for vf use */
288         if (igbvf_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU)
289                 ret_val = E1000_SUCCESS;
290
291         return ret_val;
292 }
293
294 /**
295  *  igbvf_write_mbx_vf - Write a message to the mailbox
296  *  @hw: pointer to the HW structure
297  *  @msg: The message buffer
298  *  @size: Length of buffer
299  *  @mbx_id: id of mailbox to write
300  *
301  *  returns SUCCESS if it successfully copied message into the buffer
302  **/
303 static s32 igbvf_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
304                               u16 mbx_id __unused)
305 {
306         s32 ret_val;
307         u16 i;
308
309
310         DEBUGFUNC("igbvf_write_mbx_vf");
311
312         /* lock the mailbox to prevent pf/vf race condition */
313         ret_val = igbvf_obtain_mbx_lock_vf(hw);
314         if (ret_val)
315                 goto out_no_write;
316
317         /* flush msg and acks as we are overwriting the message buffer */
318         igbvf_check_for_msg_vf(hw, 0);
319         igbvf_check_for_ack_vf(hw, 0);
320
321         /* copy the caller specified message to the mailbox memory buffer */
322         for (i = 0; i < size; i++)
323                 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(0), i, msg[i]);
324
325         /* update stats */
326         hw->mbx.stats.msgs_tx++;
327
328         /* Drop VFU and interrupt the PF to tell it a message has been sent */
329         E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
330
331 out_no_write:
332         return ret_val;
333 }
334
335 /**
336  *  igbvf_read_mbx_vf - Reads a message from the inbox intended for vf
337  *  @hw: pointer to the HW structure
338  *  @msg: The message buffer
339  *  @size: Length of buffer
340  *  @mbx_id: id of mailbox to read
341  *
342  *  returns SUCCESS if it successfuly read message from buffer
343  **/
344 static s32 igbvf_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
345                              u16 mbx_id __unused)
346 {
347         s32 ret_val = E1000_SUCCESS;
348         u16 i;
349
350         DEBUGFUNC("igbvf_read_mbx_vf");
351
352         /* lock the mailbox to prevent pf/vf race condition */
353         ret_val = igbvf_obtain_mbx_lock_vf(hw);
354         if (ret_val)
355                 goto out_no_read;
356
357         /* copy the message from the mailbox memory buffer */
358         for (i = 0; i < size; i++)
359                 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(0), i);
360
361         /* Acknowledge receipt and release mailbox, then we're done */
362         E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
363
364         /* update stats */
365         hw->mbx.stats.msgs_rx++;
366
367 out_no_read:
368         return ret_val;
369 }
370
371 /**
372  *  igbvf_init_mbx_params_vf - set initial values for vf mailbox
373  *  @hw: pointer to the HW structure
374  *
375  *  Initializes the hw->mbx struct to correct values for vf mailbox
376  */
377 s32 igbvf_init_mbx_params_vf(struct e1000_hw *hw)
378 {
379         struct e1000_mbx_info *mbx = &hw->mbx;
380
381         /* start mailbox as timed out and let the reset_hw call set the timeout
382          * value to begin communications */
383         mbx->timeout = 0;
384         mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
385
386         mbx->size = E1000_VFMAILBOX_SIZE;
387
388         mbx->ops.read = igbvf_read_mbx_vf;
389         mbx->ops.write = igbvf_write_mbx_vf;
390         mbx->ops.read_posted = igbvf_read_posted_mbx;
391         mbx->ops.write_posted = igbvf_write_posted_mbx;
392         mbx->ops.check_for_msg = igbvf_check_for_msg_vf;
393         mbx->ops.check_for_ack = igbvf_check_for_ack_vf;
394         mbx->ops.check_for_rst = igbvf_check_for_rst_vf;
395
396         mbx->stats.msgs_tx = 0;
397         mbx->stats.msgs_rx = 0;
398         mbx->stats.reqs = 0;
399         mbx->stats.acks = 0;
400         mbx->stats.rsts = 0;
401
402         return E1000_SUCCESS;
403 }
404