Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / igbvf / igbvf_vf.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_vf.h"
32
33
34 static s32       igbvf_init_mac_params_vf(struct e1000_hw *hw);
35 static s32       igbvf_check_for_link_vf(struct e1000_hw *hw);
36 static s32       igbvf_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
37                                               u16 *duplex);
38 static s32       igbvf_init_hw_vf(struct e1000_hw *hw);
39 static s32       igbvf_reset_hw_vf(struct e1000_hw *hw);
40 static void      igbvf_update_mc_addr_list_vf(struct e1000_hw *hw, u8 *, u32);
41 static void      igbvf_rar_set_vf(struct e1000_hw *, u8 *, u32);
42 static s32       igbvf_read_mac_addr_vf(struct e1000_hw *);
43
44 /**
45  *  igbvf_init_mac_params_vf - Inits MAC params
46  *  @hw: pointer to the HW structure
47  **/
48 static s32 igbvf_init_mac_params_vf(struct e1000_hw *hw)
49 {
50         struct e1000_mac_info *mac = &hw->mac;
51
52         DEBUGFUNC("igbvf_init_mac_params_vf");
53
54         /* VF's have no MTA Registers - PF feature only */
55         mac->mta_reg_count = 128;
56         /* VF's have no access to RAR entries  */
57         mac->rar_entry_count = 1;
58
59         /* Function pointers */
60         /* reset */
61         mac->ops.reset_hw = igbvf_reset_hw_vf;
62         /* hw initialization */
63         mac->ops.init_hw = igbvf_init_hw_vf;
64         /* check for link */
65         mac->ops.check_for_link = igbvf_check_for_link_vf;
66         /* link info */
67         mac->ops.get_link_up_info = igbvf_get_link_up_info_vf;
68         /* multicast address update */
69         mac->ops.update_mc_addr_list = igbvf_update_mc_addr_list_vf;
70         /* set mac address */
71         mac->ops.rar_set = igbvf_rar_set_vf;
72         /* read mac address */
73         mac->ops.read_mac_addr = igbvf_read_mac_addr_vf;
74
75
76         return E1000_SUCCESS;
77 }
78
79 /**
80  *  igbvf_init_function_pointers_vf - Inits function pointers
81  *  @hw: pointer to the HW structure
82  **/
83 void igbvf_init_function_pointers_vf(struct e1000_hw *hw)
84 {
85         DEBUGFUNC("igbvf_init_function_pointers_vf");
86
87         hw->mac.ops.init_params = igbvf_init_mac_params_vf;
88         hw->mbx.ops.init_params = igbvf_init_mbx_params_vf;
89 }
90
91 /**
92  *  igbvf_get_link_up_info_vf - Gets link info.
93  *  @hw: pointer to the HW structure
94  *  @speed: pointer to 16 bit value to store link speed.
95  *  @duplex: pointer to 16 bit value to store duplex.
96  *
97  *  Since we cannot read the PHY and get accurate link info, we must rely upon
98  *  the status register's data which is often stale and inaccurate.
99  **/
100 static s32 igbvf_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
101                                      u16 *duplex)
102 {
103         s32 status;
104
105         DEBUGFUNC("igbvf_get_link_up_info_vf");
106
107         status = E1000_READ_REG(hw, E1000_STATUS);
108         if (status & E1000_STATUS_SPEED_1000) {
109                 *speed = SPEED_1000;
110                 DEBUGOUT("1000 Mbs, ");
111         } else if (status & E1000_STATUS_SPEED_100) {
112                 *speed = SPEED_100;
113                 DEBUGOUT("100 Mbs, ");
114         } else {
115                 *speed = SPEED_10;
116                 DEBUGOUT("10 Mbs, ");
117         }
118
119         if (status & E1000_STATUS_FD) {
120                 *duplex = FULL_DUPLEX;
121                 DEBUGOUT("Full Duplex\n");
122         } else {
123                 *duplex = HALF_DUPLEX;
124                 DEBUGOUT("Half Duplex\n");
125         }
126
127         return E1000_SUCCESS;
128 }
129
130 /**
131  *  igbvf_reset_hw_vf - Resets the HW
132  *  @hw: pointer to the HW structure
133  *
134  *  VF's provide a function level reset. This is done using bit 26 of ctrl_reg.
135  *  This is all the reset we can perform on a VF.
136  **/
137 static s32 igbvf_reset_hw_vf(struct e1000_hw *hw)
138 {
139         struct e1000_mbx_info *mbx = &hw->mbx;
140         u32 timeout = E1000_VF_INIT_TIMEOUT;
141         s32 ret_val = -E1000_ERR_MAC_INIT;
142         u32 ctrl, msgbuf[3];
143         u8 *addr = (u8 *)(&msgbuf[1]);
144
145         DEBUGFUNC("igbvf_reset_hw_vf");
146
147         DEBUGOUT("Issuing a function level reset to MAC\n");
148         ctrl = E1000_READ_REG(hw, E1000_CTRL);
149         E1000_WRITE_REG(hw, E1000_CTRL, ctrl | E1000_CTRL_RST);
150
151         /* we cannot reset while the RSTI / RSTD bits are asserted */
152         while (!mbx->ops.check_for_rst(hw, 0) && timeout) {
153                 timeout--;
154                 usec_delay(5);
155         }
156
157         if (timeout) {
158                 /* mailbox timeout can now become active */
159                 mbx->timeout = E1000_VF_MBX_INIT_TIMEOUT;
160
161                 msgbuf[0] = E1000_VF_RESET;
162                 mbx->ops.write_posted(hw, msgbuf, 1, 0);
163
164                 msec_delay(10);
165
166                 /* set our "perm_addr" based on info provided by PF */
167                 ret_val = mbx->ops.read_posted(hw, msgbuf, 3, 0);
168                 if (!ret_val) {
169                         if (msgbuf[0] == (E1000_VF_RESET |
170                                                 E1000_VT_MSGTYPE_ACK))
171                                 memcpy(hw->mac.perm_addr, addr, 6);
172                         else
173                                 ret_val = -E1000_ERR_MAC_INIT;
174                 }
175         }
176
177         return ret_val;
178 }
179
180 /**
181  *  igbvf_init_hw_vf - Inits the HW
182  *  @hw: pointer to the HW structure
183  *
184  *  Not much to do here except clear the PF Reset indication if there is one.
185  **/
186 static s32 igbvf_init_hw_vf(struct e1000_hw *hw)
187 {
188         DEBUGFUNC("igbvf_init_hw_vf");
189
190         /* attempt to set and restore our mac address */
191         igbvf_rar_set_vf(hw, hw->mac.addr, 0);
192
193         return E1000_SUCCESS;
194 }
195
196 /**
197  *  igbvf_rar_set_vf - set device MAC address
198  *  @hw: pointer to the HW structure
199  *  @addr: pointer to the receive address
200  *  @index receive address array register
201  **/
202 static void igbvf_rar_set_vf(struct e1000_hw *hw, u8 * addr, u32 index __unused)
203 {
204         struct e1000_mbx_info *mbx = &hw->mbx;
205         u32 msgbuf[3];
206         u8 *msg_addr = (u8 *)(&msgbuf[1]);
207         s32 ret_val;
208
209         memset(msgbuf, 0, 12);
210         msgbuf[0] = E1000_VF_SET_MAC_ADDR;
211         memcpy(msg_addr, addr, 6);
212         ret_val = mbx->ops.write_posted(hw, msgbuf, 3, 0);
213
214         if (!ret_val)
215                 ret_val = mbx->ops.read_posted(hw, msgbuf, 3, 0);
216
217         msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
218
219         /* if nacked the address was rejected, use "perm_addr" */
220         if (!ret_val &&
221             (msgbuf[0] == (E1000_VF_SET_MAC_ADDR | E1000_VT_MSGTYPE_NACK)))
222                 igbvf_read_mac_addr_vf(hw);
223 }
224
225 /**
226  *  igbvf_hash_mc_addr_vf - Generate a multicast hash value
227  *  @hw: pointer to the HW structure
228  *  @mc_addr: pointer to a multicast address
229  *
230  *  Generates a multicast address hash value which is used to determine
231  *  the multicast filter table array address and new table value.  See
232  *  igbvf_mta_set_generic()
233  **/
234 static u32 igbvf_hash_mc_addr_vf(struct e1000_hw *hw, u8 *mc_addr)
235 {
236         u32 hash_value, hash_mask;
237         u8 bit_shift = 0;
238
239         DEBUGFUNC("igbvf_hash_mc_addr_generic");
240
241         /* Register count multiplied by bits per register */
242         hash_mask = (hw->mac.mta_reg_count * 32) - 1;
243
244         /*
245          * The bit_shift is the number of left-shifts
246          * where 0xFF would still fall within the hash mask.
247          */
248         while (hash_mask >> bit_shift != 0xFF)
249                 bit_shift++;
250
251         hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
252                                   (((u16) mc_addr[5]) << bit_shift)));
253
254         return hash_value;
255 }
256
257 /**
258  *  igbvf_update_mc_addr_list_vf - Update Multicast addresses
259  *  @hw: pointer to the HW structure
260  *  @mc_addr_list: array of multicast addresses to program
261  *  @mc_addr_count: number of multicast addresses to program
262  *
263  *  Updates the Multicast Table Array.
264  *  The caller must have a packed mc_addr_list of multicast addresses.
265  **/
266 void igbvf_update_mc_addr_list_vf(struct e1000_hw *hw,
267                                   u8 *mc_addr_list, u32 mc_addr_count)
268 {
269         struct e1000_mbx_info *mbx = &hw->mbx;
270         u32 msgbuf[E1000_VFMAILBOX_SIZE];
271         u16 *hash_list = (u16 *)&msgbuf[1];
272         u32 hash_value;
273         u32 i;
274
275         DEBUGFUNC("igbvf_update_mc_addr_list_vf");
276
277         /* Each entry in the list uses 1 16 bit word.  We have 30
278          * 16 bit words available in our HW msg buffer (minus 1 for the
279          * msg type).  That's 30 hash values if we pack 'em right.  If
280          * there are more than 30 MC addresses to add then punt the
281          * extras for now and then add code to handle more than 30 later.
282          * It would be unusual for a server to request that many multi-cast
283          * addresses except for in large enterprise network environments.
284          */
285
286         DEBUGOUT1("MC Addr Count = %d\n", mc_addr_count);
287
288         msgbuf[0] = E1000_VF_SET_MULTICAST;
289
290         if (mc_addr_count > 30) {
291                 msgbuf[0] |= E1000_VF_SET_MULTICAST_OVERFLOW;
292                 mc_addr_count = 30;
293         }
294
295         msgbuf[0] |= mc_addr_count << E1000_VT_MSGINFO_SHIFT;
296
297         for (i = 0; i < mc_addr_count; i++) {
298                 hash_value = igbvf_hash_mc_addr_vf(hw, mc_addr_list);
299                 DEBUGOUT1("Hash value = 0x%03X\n", hash_value);
300                 hash_list[i] = hash_value & 0x0FFF;
301                 mc_addr_list += ETH_ADDR_LEN;
302         }
303
304         mbx->ops.write_posted(hw, msgbuf, E1000_VFMAILBOX_SIZE, 0);
305 }
306
307 /**
308  *  igbvf_vfta_set_vf - Set/Unset vlan filter table address
309  *  @hw: pointer to the HW structure
310  *  @vid: determines the vfta register and bit to set/unset
311  *  @set: if true then set bit, else clear bit
312  **/
313 void igbvf_vfta_set_vf(struct e1000_hw *hw, u16 vid, bool set)
314 {
315         struct e1000_mbx_info *mbx = &hw->mbx;
316         u32 msgbuf[2];
317
318         msgbuf[0] = E1000_VF_SET_VLAN;
319         msgbuf[1] = vid;
320         /* Setting the 8 bit field MSG INFO to TRUE indicates "add" */
321         if (set)
322                 msgbuf[0] |= E1000_VF_SET_VLAN_ADD;
323
324         mbx->ops.write_posted(hw, msgbuf, 2, 0);
325 }
326
327 /** igbvf_rlpml_set_vf - Set the maximum receive packet length
328  *  @hw: pointer to the HW structure
329  *  @max_size: value to assign to max frame size
330  **/
331 void igbvf_rlpml_set_vf(struct e1000_hw *hw, u16 max_size)
332 {
333         struct e1000_mbx_info *mbx = &hw->mbx;
334         u32 msgbuf[2];
335
336         msgbuf[0] = E1000_VF_SET_LPE;
337         msgbuf[1] = max_size;
338
339         mbx->ops.write_posted(hw, msgbuf, 2, 0);
340 }
341
342 /**
343  *  igbvf_promisc_set_vf - Set flags for Unicast or Multicast promisc
344  *  @hw: pointer to the HW structure
345  *  @uni: boolean indicating unicast promisc status
346  *  @multi: boolean indicating multicast promisc status
347  **/
348 s32 igbvf_promisc_set_vf(struct e1000_hw *hw, enum e1000_promisc_type type)
349 {
350         struct e1000_mbx_info *mbx = &hw->mbx;
351         u32 msgbuf = E1000_VF_SET_PROMISC;
352         s32 ret_val;
353
354         switch (type) {
355         case e1000_promisc_multicast:
356                 msgbuf |= E1000_VF_SET_PROMISC_MULTICAST;
357                 break;
358         case e1000_promisc_enabled:
359                 msgbuf |= E1000_VF_SET_PROMISC_MULTICAST;
360         case e1000_promisc_unicast:
361                 msgbuf |= E1000_VF_SET_PROMISC_UNICAST;
362         case e1000_promisc_disabled:
363                 break;
364         default:
365                 return -E1000_ERR_MAC_INIT;
366         }
367
368          ret_val = mbx->ops.write_posted(hw, &msgbuf, 1, 0);
369
370         if (!ret_val)
371                 ret_val = mbx->ops.read_posted(hw, &msgbuf, 1, 0);
372
373         if (!ret_val && !(msgbuf & E1000_VT_MSGTYPE_ACK))
374                 ret_val = -E1000_ERR_MAC_INIT;
375
376         return ret_val;
377 }
378
379 /**
380  *  igbvf_read_mac_addr_vf - Read device MAC address
381  *  @hw: pointer to the HW structure
382  **/
383 static s32 igbvf_read_mac_addr_vf(struct e1000_hw *hw)
384 {
385         int i;
386
387         for (i = 0; i < ETH_ADDR_LEN; i++)
388                 hw->mac.addr[i] = hw->mac.perm_addr[i];
389
390         return E1000_SUCCESS;
391 }
392
393 /**
394  *  igbvf_check_for_link_vf - Check for link for a virtual interface
395  *  @hw: pointer to the HW structure
396  *
397  *  Checks to see if the underlying PF is still talking to the VF and
398  *  if it is then it reports the link state to the hardware, otherwise
399  *  it reports link down and returns an error.
400  **/
401 static s32 igbvf_check_for_link_vf(struct e1000_hw *hw)
402 {
403         struct e1000_mbx_info *mbx = &hw->mbx;
404         struct e1000_mac_info *mac = &hw->mac;
405         s32 ret_val = E1000_SUCCESS;
406         u32 in_msg = 0;
407
408         DEBUGFUNC("igbvf_check_for_link_vf");
409
410         /*
411          * We only want to run this if there has been a rst asserted.
412          * in this case that could mean a link change, device reset,
413          * or a virtual function reset
414          */
415
416         /* If we were hit with a reset drop the link */
417         if (!mbx->ops.check_for_rst(hw, 0))
418                 mac->get_link_status = true;
419
420         if (!mac->get_link_status)
421                 goto out;
422
423         /* if link status is down no point in checking to see if pf is up */
424         if (!(E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU))
425                 goto out;
426
427         /* if the read failed it could just be a mailbox collision, best wait
428          * until we are called again and don't report an error */
429         if (mbx->ops.read(hw, &in_msg, 1, 0))
430                 goto out;
431
432         /* if incoming message isn't clear to send we are waiting on response */
433         if (!(in_msg & E1000_VT_MSGTYPE_CTS)) {
434                 /* message is not CTS and is NACK we have lost CTS status */
435                 if (in_msg & E1000_VT_MSGTYPE_NACK)
436                         ret_val = -E1000_ERR_MAC_INIT;
437                 goto out;
438         }
439
440         /* at this point we know the PF is talking to us, check and see if
441          * we are still accepting timeout or if we had a timeout failure.
442          * if we failed then we will need to reinit */
443         if (!mbx->timeout) {
444                 ret_val = -E1000_ERR_MAC_INIT;
445                 goto out;
446         }
447
448         /* if we passed all the tests above then the link is up and we no
449          * longer need to check for link */
450         mac->get_link_status = false;
451
452 out:
453         return ret_val;
454 }
455