update Intel copyright years to 2014
[dpdk.git] / lib / librte_pmd_e1000 / igb_pf.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <stdarg.h>
40 #include <inttypes.h>
41
42 #include <rte_interrupts.h>
43 #include <rte_log.h>
44 #include <rte_debug.h>
45 #include <rte_eal.h>
46 #include <rte_ether.h>
47 #include <rte_ethdev.h>
48 #include <rte_memcpy.h>
49 #include <rte_malloc.h>
50 #include <rte_random.h>
51
52 #include "e1000/e1000_defines.h"
53 #include "e1000/e1000_regs.h"
54 #include "e1000/e1000_hw.h"
55 #include "e1000_ethdev.h"
56
57 static inline uint16_t
58 dev_num_vf(struct rte_eth_dev *eth_dev)
59 {
60         return eth_dev->pci_dev->max_vfs;
61 }
62
63 static inline 
64 int igb_vf_perm_addr_gen(struct rte_eth_dev *dev, uint16_t vf_num)
65 {
66         unsigned char vf_mac_addr[ETHER_ADDR_LEN];
67         struct e1000_vf_info *vfinfo = 
68                 *E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
69         uint16_t vfn;
70
71         for (vfn = 0; vfn < vf_num; vfn++) {
72                 eth_random_addr(vf_mac_addr);
73                 /* keep the random address as default */
74                 memcpy(vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 
75                                 ETHER_ADDR_LEN);
76         }
77
78         return 0;
79 }
80
81 static inline int
82 igb_mb_intr_setup(struct rte_eth_dev *dev)
83 {
84         struct e1000_interrupt *intr =
85                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
86
87         intr->mask |= E1000_ICR_VMMB;
88
89         return 0;
90 }
91
92 void igb_pf_host_init(struct rte_eth_dev *eth_dev)
93 {
94         struct e1000_vf_info **vfinfo = 
95                 E1000_DEV_PRIVATE_TO_P_VFDATA(eth_dev->data->dev_private);
96         struct e1000_hw *hw = 
97                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
98         uint16_t vf_num;
99         uint8_t nb_queue;
100
101         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
102         if (0 == (vf_num = dev_num_vf(eth_dev)))
103                 return;
104
105         if (hw->mac.type == e1000_i350)
106                 nb_queue = 1;
107         else if(hw->mac.type == e1000_82576)
108                 /* per datasheet, it should be 2, but 1 seems correct */
109                 nb_queue = 1;
110         else
111                 return;
112
113         *vfinfo = rte_zmalloc("vf_info", sizeof(struct e1000_vf_info) * vf_num, 0);
114         if (*vfinfo == NULL)
115                 rte_panic("Cannot allocate memory for private VF data\n");
116
117         RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_8_POOLS;
118         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = nb_queue;
119         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = vf_num;
120         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx = (uint16_t)(vf_num * nb_queue);
121
122         igb_vf_perm_addr_gen(eth_dev, vf_num);
123
124         /* set mb interrupt mask */
125         igb_mb_intr_setup(eth_dev);
126
127         return;
128 }
129
130 #define E1000_RAH_POOLSEL_SHIFT    (18)
131 int igb_pf_host_configure(struct rte_eth_dev *eth_dev)
132 {
133         uint32_t vtctl;
134         uint16_t vf_num;
135         struct e1000_hw *hw = 
136                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
137         uint32_t vlanctrl;
138         int i;
139         uint32_t rah;
140
141         if (0 == (vf_num = dev_num_vf(eth_dev)))
142                 return -1;
143
144         /* enable VMDq and set the default pool for PF */
145         vtctl = E1000_READ_REG(hw, E1000_VT_CTL);
146         vtctl &= ~E1000_VT_CTL_DEFAULT_POOL_MASK;
147         vtctl |= RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx 
148                 << E1000_VT_CTL_DEFAULT_POOL_SHIFT;
149         vtctl |= E1000_VT_CTL_VM_REPL_EN;
150         E1000_WRITE_REG(hw, E1000_VT_CTL, vtctl);
151
152         /* Enable pools reserved to PF only */
153         E1000_WRITE_REG(hw, E1000_VFRE, (~0) << vf_num);
154         E1000_WRITE_REG(hw, E1000_VFTE, (~0) << vf_num);
155
156         /* PFDMA Tx General Switch Control Enables VMDQ loopback */
157         if (hw->mac.type == e1000_i350)
158                 E1000_WRITE_REG(hw, E1000_TXSWC, E1000_DTXSWC_VMDQ_LOOPBACK_EN);
159         else
160                 E1000_WRITE_REG(hw, E1000_DTXSWC, E1000_DTXSWC_VMDQ_LOOPBACK_EN);
161
162         /* clear VMDq map to perment rar 0 */
163         rah = E1000_READ_REG(hw, E1000_RAH(0));
164         rah &= ~ (0xFF << E1000_RAH_POOLSEL_SHIFT);
165         E1000_WRITE_REG(hw, E1000_RAH(0), rah);
166
167         /* clear VMDq map to scan rar 32 */
168         rah = E1000_READ_REG(hw, E1000_RAH(hw->mac.rar_entry_count));
169         rah &= ~ (0xFF << E1000_RAH_POOLSEL_SHIFT);
170         E1000_WRITE_REG(hw, E1000_RAH(hw->mac.rar_entry_count), rah);
171
172         /* set VMDq map to default PF pool */
173         rah = E1000_READ_REG(hw, E1000_RAH(0));
174         rah |= (0x1 << (RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx +
175                         E1000_RAH_POOLSEL_SHIFT));
176         E1000_WRITE_REG(hw, E1000_RAH(0), rah);
177
178         /*
179          * enable vlan filtering and allow all vlan tags through 
180          */
181         vlanctrl = E1000_READ_REG(hw, E1000_RCTL);
182         vlanctrl |= E1000_RCTL_VFE ; /* enable vlan filters */
183         E1000_WRITE_REG(hw, E1000_RCTL, vlanctrl);
184
185         /* VFTA - enable all vlan filters */
186         for (i = 0; i < IGB_VFTA_SIZE; i++) {
187                 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, i, 0xFFFFFFFF);
188         }
189         
190         /* Enable/Disable MAC Anti-Spoofing */
191         e1000_vmdq_set_anti_spoofing_pf(hw, FALSE, vf_num);
192
193         return 0;
194 }
195
196 static void 
197 set_rx_mode(struct rte_eth_dev *dev)
198 {
199         struct rte_eth_dev_data *dev_data = 
200                 (struct rte_eth_dev_data*)dev->data->dev_private;
201         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
202         uint32_t fctrl, vmolr = E1000_VMOLR_BAM | E1000_VMOLR_AUPE;
203         uint16_t vfn = dev_num_vf(dev);
204
205         /* Check for Promiscuous and All Multicast modes */
206         fctrl = E1000_READ_REG(hw, E1000_RCTL);
207
208         /* set all bits that we expect to always be set */
209         fctrl &= ~E1000_RCTL_SBP; /* disable store-bad-packets */
210         fctrl |= E1000_RCTL_BAM;;
211
212         /* clear the bits we are changing the status of */
213         fctrl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
214
215         if (dev_data->promiscuous) {
216                 fctrl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
217                 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
218         } else {
219                 if (dev_data->all_multicast) {
220                         fctrl |= E1000_RCTL_MPE;
221                         vmolr |= E1000_VMOLR_MPME;
222                 } else {
223                         vmolr |= E1000_VMOLR_ROMPE;
224                 }
225         }
226
227         if ((hw->mac.type == e1000_82576) ||
228                 (hw->mac.type == e1000_i350)) {
229                 vmolr |= E1000_READ_REG(hw, E1000_VMOLR(vfn)) &
230                          ~(E1000_VMOLR_MPME | E1000_VMOLR_ROMPE |
231                            E1000_VMOLR_ROPE);
232                 E1000_WRITE_REG(hw, E1000_VMOLR(vfn), vmolr);
233         }
234
235         E1000_WRITE_REG(hw, E1000_RCTL, fctrl);
236 }
237
238 static inline void 
239 igb_vf_reset_event(struct rte_eth_dev *dev, uint16_t vf)
240 {
241         struct e1000_hw *hw = 
242                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
243         struct e1000_vf_info *vfinfo = 
244                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
245         uint32_t vmolr = E1000_READ_REG(hw, E1000_VMOLR(vf));
246
247         vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | 
248                         E1000_VMOLR_BAM | E1000_VMOLR_AUPE);
249         E1000_WRITE_REG(hw, E1000_VMOLR(vf), vmolr);
250
251         E1000_WRITE_REG(hw, E1000_VMVIR(vf), 0);
252         
253         /* reset multicast table array for vf */
254         vfinfo[vf].num_vf_mc_hashes = 0;
255
256         /* reset rx mode */
257         set_rx_mode(dev);
258 }
259
260 static inline void 
261 igb_vf_reset_msg(struct rte_eth_dev *dev, uint16_t vf)
262 {
263         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
264         uint32_t reg;
265
266         /* enable transmit and receive for vf */
267         reg = E1000_READ_REG(hw, E1000_VFTE);
268         reg |= (reg | (1 << vf));
269         E1000_WRITE_REG(hw, E1000_VFTE, reg);
270
271         reg = E1000_READ_REG(hw, E1000_VFRE);
272         reg |= (reg | (1 << vf));
273         E1000_WRITE_REG(hw, E1000_VFRE, reg);
274
275         igb_vf_reset_event(dev, vf);
276 }
277
278 static int
279 igb_vf_reset(struct rte_eth_dev *dev, uint16_t vf, uint32_t *msgbuf)
280 {
281         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
282         struct e1000_vf_info *vfinfo = 
283                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
284         unsigned char *vf_mac = vfinfo[vf].vf_mac_addresses;
285         int rar_entry = hw->mac.rar_entry_count - (vf + 1);
286         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
287         uint32_t rah;
288
289         igb_vf_reset_msg(dev, vf);
290
291         hw->mac.ops.rar_set(hw, vf_mac, rar_entry);
292         rah = E1000_READ_REG(hw, E1000_RAH(rar_entry));
293         rah |= (0x1 << (vf + E1000_RAH_POOLSEL_SHIFT));
294         E1000_WRITE_REG(hw, E1000_RAH(rar_entry), rah);
295
296         /* reply to reset with ack and vf mac address */
297         msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
298         rte_memcpy(new_mac, vf_mac, ETHER_ADDR_LEN);
299         e1000_write_mbx(hw, msgbuf, 3, vf);
300
301         return 0;
302 }
303
304 static int
305 igb_vf_set_mac_addr(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
306 {
307         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
308         struct e1000_vf_info *vfinfo = 
309                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
310         int rar_entry = hw->mac.rar_entry_count - (vf + 1);
311         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
312
313         if (is_valid_assigned_ether_addr((struct ether_addr*)new_mac)) {
314                 rte_memcpy(vfinfo[vf].vf_mac_addresses, new_mac, 6);
315                 hw->mac.ops.rar_set(hw, new_mac, rar_entry);
316                 return 0;
317         }
318         return -1;
319 }
320
321 static int
322 igb_vf_set_multicast(struct rte_eth_dev *dev, __rte_unused uint32_t vf, uint32_t *msgbuf)
323 {
324         int i;
325         uint32_t vector_bit;
326         uint32_t vector_reg;
327         uint32_t mta_reg;
328         int entries = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> 
329                 E1000_VT_MSGINFO_SHIFT;
330         uint16_t *hash_list = (uint16_t *)&msgbuf[1];
331         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
332         struct e1000_vf_info *vfinfo = 
333                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
334
335         /* only so many hash values supported */
336         entries = RTE_MIN(entries, E1000_MAX_VF_MC_ENTRIES);
337
338         /*
339          * salt away the number of multi cast addresses assigned
340          * to this VF for later use to restore when the PF multi cast
341          * list changes
342          */
343         vfinfo->num_vf_mc_hashes = (uint16_t)entries;
344
345         /*
346          * VFs are limited to using the MTA hash table for their multicast
347          * addresses
348          */
349         for (i = 0; i < entries; i++) {
350                 vfinfo->vf_mc_hashes[i] = hash_list[i];
351         }
352
353         for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
354                 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
355                 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
356                 mta_reg = E1000_READ_REG_ARRAY(hw, E1000_MTA, vector_reg);
357                 mta_reg |= (1 << vector_bit);
358                 E1000_WRITE_REG_ARRAY(hw, E1000_MTA, vector_reg, mta_reg);
359         }
360
361         return 0;
362 }
363
364 static int
365 igb_vf_set_vlan(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
366 {
367         int add, vid;
368         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
369         struct e1000_vf_info *vfinfo = 
370                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
371         uint32_t vid_idx, vid_bit, vfta;
372
373         add = (msgbuf[0] & E1000_VT_MSGINFO_MASK)
374                 >> E1000_VT_MSGINFO_SHIFT;
375         vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
376
377         if (add)
378                 vfinfo[vf].vlan_count++;
379         else if (vfinfo[vf].vlan_count)
380                 vfinfo[vf].vlan_count--;
381
382         vid_idx = (uint32_t)((vid >> E1000_VFTA_ENTRY_SHIFT) & 
383                              E1000_VFTA_ENTRY_MASK);
384         vid_bit = (uint32_t)(1 << (vid & E1000_VFTA_ENTRY_BIT_SHIFT_MASK));
385         vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, vid_idx);
386         if (add)
387                 vfta |= vid_bit;
388         else
389                 vfta &= ~vid_bit;
390         
391         E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, vid_idx, vfta);
392         E1000_WRITE_FLUSH(hw);
393
394         return 0;
395 }
396
397 static int 
398 igb_rcv_msg_from_vf(struct rte_eth_dev *dev, uint16_t vf)
399 {
400         uint16_t mbx_size = E1000_VFMAILBOX_SIZE;
401         uint32_t msgbuf[E1000_VFMAILBOX_SIZE];
402         int32_t retval;
403         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
404
405         retval = e1000_read_mbx(hw, msgbuf, mbx_size, vf);
406         if (retval) {
407                 RTE_LOG(ERR, PMD, "Error mbx recv msg from VF %d\n", vf);
408                 return retval;
409         }
410
411         /* do nothing with the message already processed */
412         if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
413                 return retval;
414
415         /* flush the ack before we write any messages back */
416         E1000_WRITE_FLUSH(hw);
417
418         /* perform VF reset */
419         if (msgbuf[0] == E1000_VF_RESET) {
420                 return igb_vf_reset(dev, vf, msgbuf);
421         }
422
423         /* check & process VF to PF mailbox message */
424         switch ((msgbuf[0] & 0xFFFF)) {
425         case E1000_VF_SET_MAC_ADDR:
426                 retval = igb_vf_set_mac_addr(dev, vf, msgbuf);
427                 break;
428         case E1000_VF_SET_MULTICAST:
429                 retval = igb_vf_set_multicast(dev, vf, msgbuf);
430                 break;
431         case E1000_VF_SET_VLAN:
432                 retval = igb_vf_set_vlan(dev, vf, msgbuf);
433                 break;
434         default:
435                 RTE_LOG(DEBUG, PMD, "Unhandled Msg %8.8x\n", (unsigned) msgbuf[0]);
436                 retval = E1000_ERR_MBX;
437                 break;
438         }
439
440         /* response the VF according to the message process result */
441         if (retval)
442                 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
443         else
444                 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
445
446         msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
447
448         e1000_write_mbx(hw, msgbuf, 1, vf);
449
450         return retval;
451 }
452
453 static inline void 
454 igb_rcv_ack_from_vf(struct rte_eth_dev *dev, uint16_t vf)
455 {
456         uint32_t msg = E1000_VT_MSGTYPE_NACK;
457         struct e1000_hw *hw = 
458                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
459
460         e1000_write_mbx(hw, &msg, 1, vf);
461 }
462
463 void igb_pf_mbx_process(struct rte_eth_dev *eth_dev)
464 {
465         uint16_t vf;
466         struct e1000_hw *hw = 
467                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
468
469         for (vf = 0; vf < dev_num_vf(eth_dev); vf++) {
470                 /* check & process vf function level reset */
471                 if (!e1000_check_for_rst(hw, vf))
472                         igb_vf_reset_event(eth_dev, vf);
473
474                 /* check & process vf mailbox messages */
475                 if (!e1000_check_for_msg(hw, vf))
476                         igb_rcv_msg_from_vf(eth_dev, vf);
477
478                 /* check & process acks from vf */
479                 if (!e1000_check_for_ack(hw, vf))
480                         igb_rcv_ack_from_vf(eth_dev, vf);
481         }
482 }