doc: whitespace changes in licenses
[dpdk.git] / lib / librte_pmd_e1000 / igb_pf.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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 
58 void eth_random_addr(uint8_t *addr)
59 {
60         uint64_t rand = rte_rand();
61         uint8_t *p = (uint8_t*)&rand;
62
63         rte_memcpy(addr, p, ETHER_ADDR_LEN);
64         addr[0] &= 0xfe;        /* clear multicast bit */
65         addr[0] |= 0x02;        /* set local assignment bit (IEEE802) */
66 }
67
68 static inline uint16_t
69 dev_num_vf(struct rte_eth_dev *eth_dev)
70 {
71         return eth_dev->pci_dev->max_vfs;
72 }
73
74 static inline 
75 int igb_vf_perm_addr_gen(struct rte_eth_dev *dev, uint16_t vf_num)
76 {
77         unsigned char vf_mac_addr[ETHER_ADDR_LEN];
78         struct e1000_vf_info *vfinfo = 
79                 *E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
80         uint16_t vfn;
81
82         for (vfn = 0; vfn < vf_num; vfn++) {
83                 eth_random_addr(vf_mac_addr);
84                 /* keep the random address as default */
85                 memcpy(vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 
86                                 ETHER_ADDR_LEN);
87         }
88
89         return 0;
90 }
91
92 static inline int
93 igb_mb_intr_setup(struct rte_eth_dev *dev)
94 {
95         struct e1000_interrupt *intr =
96                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
97
98         intr->mask |= E1000_ICR_VMMB;
99
100         return 0;
101 }
102
103 void igb_pf_host_init(struct rte_eth_dev *eth_dev)
104 {
105         struct e1000_vf_info **vfinfo = 
106                 E1000_DEV_PRIVATE_TO_P_VFDATA(eth_dev->data->dev_private);
107         struct e1000_hw *hw = 
108                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
109         uint16_t vf_num;
110         uint8_t nb_queue;
111
112         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
113         if (0 == (vf_num = dev_num_vf(eth_dev)))
114                 return;
115
116         if (hw->mac.type == e1000_i350)
117                 nb_queue = 1;
118         else if(hw->mac.type == e1000_82576)
119                 /* per datasheet, it should be 2, but 1 seems correct */
120                 nb_queue = 1;
121         else
122                 return;
123
124         *vfinfo = rte_zmalloc("vf_info", sizeof(struct e1000_vf_info) * vf_num, 0);
125         if (*vfinfo == NULL)
126                 rte_panic("Cannot allocate memory for private VF data\n");
127
128         RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_8_POOLS;
129         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = nb_queue;
130         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = vf_num;
131         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx = (uint16_t)(vf_num * nb_queue);
132
133         igb_vf_perm_addr_gen(eth_dev, vf_num);
134
135         /* set mb interrupt mask */
136         igb_mb_intr_setup(eth_dev);
137
138         return;
139 }
140
141 #define E1000_RAH_POOLSEL_SHIFT    (18)
142 int igb_pf_host_configure(struct rte_eth_dev *eth_dev)
143 {
144         uint32_t vtctl;
145         uint16_t vf_num;
146         struct e1000_hw *hw = 
147                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
148         uint32_t vlanctrl;
149         int i;
150         uint32_t rah;
151
152         if (0 == (vf_num = dev_num_vf(eth_dev)))
153                 return -1;
154
155         /* enable VMDq and set the default pool for PF */
156         vtctl = E1000_READ_REG(hw, E1000_VT_CTL);
157         vtctl &= ~E1000_VT_CTL_DEFAULT_POOL_MASK;
158         vtctl |= RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx 
159                 << E1000_VT_CTL_DEFAULT_POOL_SHIFT;
160         vtctl |= E1000_VT_CTL_VM_REPL_EN;
161         E1000_WRITE_REG(hw, E1000_VT_CTL, vtctl);
162
163         /* Enable pools reserved to PF only */
164         E1000_WRITE_REG(hw, E1000_VFRE, (~0) << vf_num);
165         E1000_WRITE_REG(hw, E1000_VFTE, (~0) << vf_num);
166
167         /* PFDMA Tx General Switch Control Enables VMDQ loopback */
168         if (hw->mac.type == e1000_i350)
169                 E1000_WRITE_REG(hw, E1000_TXSWC, E1000_DTXSWC_VMDQ_LOOPBACK_EN);
170         else
171                 E1000_WRITE_REG(hw, E1000_DTXSWC, E1000_DTXSWC_VMDQ_LOOPBACK_EN);
172
173         /* clear VMDq map to perment rar 0 */
174         rah = E1000_READ_REG(hw, E1000_RAH(0));
175         rah &= ~ (0xFF << E1000_RAH_POOLSEL_SHIFT);
176         E1000_WRITE_REG(hw, E1000_RAH(0), rah);
177
178         /* clear VMDq map to scan rar 32 */
179         rah = E1000_READ_REG(hw, E1000_RAH(hw->mac.rar_entry_count));
180         rah &= ~ (0xFF << E1000_RAH_POOLSEL_SHIFT);
181         E1000_WRITE_REG(hw, E1000_RAH(hw->mac.rar_entry_count), rah);
182
183         /* set VMDq map to default PF pool */
184         rah = E1000_READ_REG(hw, E1000_RAH(0));
185         rah |= (0x1 << (RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx +
186                         E1000_RAH_POOLSEL_SHIFT));
187         E1000_WRITE_REG(hw, E1000_RAH(0), rah);
188
189         /*
190          * enable vlan filtering and allow all vlan tags through 
191          */
192         vlanctrl = E1000_READ_REG(hw, E1000_RCTL);
193         vlanctrl |= E1000_RCTL_VFE ; /* enable vlan filters */
194         E1000_WRITE_REG(hw, E1000_RCTL, vlanctrl);
195
196         /* VFTA - enable all vlan filters */
197         for (i = 0; i < IGB_VFTA_SIZE; i++) {
198                 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, i, 0xFFFFFFFF);
199         }
200         
201         /* Enable/Disable MAC Anti-Spoofing */
202         e1000_vmdq_set_anti_spoofing_pf(hw, FALSE, vf_num);
203
204         return 0;
205 }
206
207 static void 
208 set_rx_mode(struct rte_eth_dev *dev)
209 {
210         struct rte_eth_dev_data *dev_data = 
211                 (struct rte_eth_dev_data*)dev->data->dev_private;
212         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
213         uint32_t fctrl, vmolr = E1000_VMOLR_BAM | E1000_VMOLR_AUPE;
214         uint16_t vfn = dev_num_vf(dev);
215
216         /* Check for Promiscuous and All Multicast modes */
217         fctrl = E1000_READ_REG(hw, E1000_RCTL);
218
219         /* set all bits that we expect to always be set */
220         fctrl &= ~E1000_RCTL_SBP; /* disable store-bad-packets */
221         fctrl |= E1000_RCTL_BAM;;
222
223         /* clear the bits we are changing the status of */
224         fctrl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
225
226         if (dev_data->promiscuous) {
227                 fctrl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
228                 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
229         } else {
230                 if (dev_data->all_multicast) {
231                         fctrl |= E1000_RCTL_MPE;
232                         vmolr |= E1000_VMOLR_MPME;
233                 } else {
234                         vmolr |= E1000_VMOLR_ROMPE;
235                 }
236         }
237
238         if ((hw->mac.type == e1000_82576) ||
239                 (hw->mac.type == e1000_i350)) {
240                 vmolr |= E1000_READ_REG(hw, E1000_VMOLR(vfn)) &
241                          ~(E1000_VMOLR_MPME | E1000_VMOLR_ROMPE |
242                            E1000_VMOLR_ROPE);
243                 E1000_WRITE_REG(hw, E1000_VMOLR(vfn), vmolr);
244         }
245
246         E1000_WRITE_REG(hw, E1000_RCTL, fctrl);
247 }
248
249 static inline void 
250 igb_vf_reset_event(struct rte_eth_dev *dev, uint16_t vf)
251 {
252         struct e1000_hw *hw = 
253                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
254         struct e1000_vf_info *vfinfo = 
255                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
256         uint32_t vmolr = E1000_READ_REG(hw, E1000_VMOLR(vf));
257
258         vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | 
259                         E1000_VMOLR_BAM | E1000_VMOLR_AUPE);
260         E1000_WRITE_REG(hw, E1000_VMOLR(vf), vmolr);
261
262         E1000_WRITE_REG(hw, E1000_VMVIR(vf), 0);
263         
264         /* reset multicast table array for vf */
265         vfinfo[vf].num_vf_mc_hashes = 0;
266
267         /* reset rx mode */
268         set_rx_mode(dev);
269 }
270
271 static inline void 
272 igb_vf_reset_msg(struct rte_eth_dev *dev, uint16_t vf)
273 {
274         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
275         uint32_t reg;
276
277         /* enable transmit and receive for vf */
278         reg = E1000_READ_REG(hw, E1000_VFTE);
279         reg |= (reg | (1 << vf));
280         E1000_WRITE_REG(hw, E1000_VFTE, reg);
281
282         reg = E1000_READ_REG(hw, E1000_VFRE);
283         reg |= (reg | (1 << vf));
284         E1000_WRITE_REG(hw, E1000_VFRE, reg);
285
286         igb_vf_reset_event(dev, vf);
287 }
288
289 static int
290 igb_vf_reset(struct rte_eth_dev *dev, uint16_t vf, uint32_t *msgbuf)
291 {
292         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
293         struct e1000_vf_info *vfinfo = 
294                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
295         unsigned char *vf_mac = vfinfo[vf].vf_mac_addresses;
296         int rar_entry = hw->mac.rar_entry_count - (vf + 1);
297         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
298         uint32_t rah;
299
300         igb_vf_reset_msg(dev, vf);
301
302         hw->mac.ops.rar_set(hw, vf_mac, rar_entry);
303         rah = E1000_READ_REG(hw, E1000_RAH(rar_entry));
304         rah |= (0x1 << (vf + E1000_RAH_POOLSEL_SHIFT));
305         E1000_WRITE_REG(hw, E1000_RAH(rar_entry), rah);
306
307         /* reply to reset with ack and vf mac address */
308         msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
309         rte_memcpy(new_mac, vf_mac, ETHER_ADDR_LEN);
310         e1000_write_mbx(hw, msgbuf, 3, vf);
311
312         return 0;
313 }
314
315 static int
316 igb_vf_set_mac_addr(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
317 {
318         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
319         struct e1000_vf_info *vfinfo = 
320                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
321         int rar_entry = hw->mac.rar_entry_count - (vf + 1);
322         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
323
324         if (is_valid_assigned_ether_addr((struct ether_addr*)new_mac)) {
325                 rte_memcpy(vfinfo[vf].vf_mac_addresses, new_mac, 6);
326                 hw->mac.ops.rar_set(hw, new_mac, rar_entry);
327                 return 0;
328         }
329         return -1;
330 }
331
332 static int
333 igb_vf_set_multicast(struct rte_eth_dev *dev, __rte_unused uint32_t vf, uint32_t *msgbuf)
334 {
335         int i;
336         uint32_t vector_bit;
337         uint32_t vector_reg;
338         uint32_t mta_reg;
339         int entries = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> 
340                 E1000_VT_MSGINFO_SHIFT;
341         uint16_t *hash_list = (uint16_t *)&msgbuf[1];
342         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
343         struct e1000_vf_info *vfinfo = 
344                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
345
346         /* only so many hash values supported */
347         entries = RTE_MIN(entries, E1000_MAX_VF_MC_ENTRIES);
348
349         /*
350          * salt away the number of multi cast addresses assigned
351          * to this VF for later use to restore when the PF multi cast
352          * list changes
353          */
354         vfinfo->num_vf_mc_hashes = (uint16_t)entries;
355
356         /*
357          * VFs are limited to using the MTA hash table for their multicast
358          * addresses
359          */
360         for (i = 0; i < entries; i++) {
361                 vfinfo->vf_mc_hashes[i] = hash_list[i];
362         }
363
364         for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
365                 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
366                 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
367                 mta_reg = E1000_READ_REG_ARRAY(hw, E1000_MTA, vector_reg);
368                 mta_reg |= (1 << vector_bit);
369                 E1000_WRITE_REG_ARRAY(hw, E1000_MTA, vector_reg, mta_reg);
370         }
371
372         return 0;
373 }
374
375 static int
376 igb_vf_set_vlan(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
377 {
378         int add, vid;
379         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
380         struct e1000_vf_info *vfinfo = 
381                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
382         uint32_t vid_idx, vid_bit, vfta;
383
384         add = (msgbuf[0] & E1000_VT_MSGINFO_MASK)
385                 >> E1000_VT_MSGINFO_SHIFT;
386         vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
387
388         if (add)
389                 vfinfo[vf].vlan_count++;
390         else if (vfinfo[vf].vlan_count)
391                 vfinfo[vf].vlan_count--;
392
393         vid_idx = (uint32_t)((vid >> E1000_VFTA_ENTRY_SHIFT) & 
394                              E1000_VFTA_ENTRY_MASK);
395         vid_bit = (uint32_t)(1 << (vid & E1000_VFTA_ENTRY_BIT_SHIFT_MASK));
396         vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, vid_idx);
397         if (add)
398                 vfta |= vid_bit;
399         else
400                 vfta &= ~vid_bit;
401         
402         E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, vid_idx, vfta);
403         E1000_WRITE_FLUSH(hw);
404
405         return 0;
406 }
407
408 static int 
409 igb_rcv_msg_from_vf(struct rte_eth_dev *dev, uint16_t vf)
410 {
411         uint16_t mbx_size = E1000_VFMAILBOX_SIZE;
412         uint32_t msgbuf[E1000_VFMAILBOX_SIZE];
413         int32_t retval;
414         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
415
416         retval = e1000_read_mbx(hw, msgbuf, mbx_size, vf);
417         if (retval) {
418                 RTE_LOG(ERR, PMD, "Error mbx recv msg from VF %d\n", vf);
419                 return retval;
420         }
421
422         /* do nothing with the message already processed */
423         if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
424                 return retval;
425
426         /* flush the ack before we write any messages back */
427         E1000_WRITE_FLUSH(hw);
428
429         /* perform VF reset */
430         if (msgbuf[0] == E1000_VF_RESET) {
431                 return igb_vf_reset(dev, vf, msgbuf);
432         }
433
434         /* check & process VF to PF mailbox message */
435         switch ((msgbuf[0] & 0xFFFF)) {
436         case E1000_VF_SET_MAC_ADDR:
437                 retval = igb_vf_set_mac_addr(dev, vf, msgbuf);
438                 break;
439         case E1000_VF_SET_MULTICAST:
440                 retval = igb_vf_set_multicast(dev, vf, msgbuf);
441                 break;
442         case E1000_VF_SET_VLAN:
443                 retval = igb_vf_set_vlan(dev, vf, msgbuf);
444                 break;
445         default:
446                 RTE_LOG(DEBUG, PMD, "Unhandled Msg %8.8x\n", (unsigned) msgbuf[0]);
447                 retval = E1000_ERR_MBX;
448                 break;
449         }
450
451         /* response the VF according to the message process result */
452         if (retval)
453                 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
454         else
455                 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
456
457         msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
458
459         e1000_write_mbx(hw, msgbuf, 1, vf);
460
461         return retval;
462 }
463
464 static inline void 
465 igb_rcv_ack_from_vf(struct rte_eth_dev *dev, uint16_t vf)
466 {
467         uint32_t msg = E1000_VT_MSGTYPE_NACK;
468         struct e1000_hw *hw = 
469                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
470
471         e1000_write_mbx(hw, &msg, 1, vf);
472 }
473
474 void igb_pf_mbx_process(struct rte_eth_dev *eth_dev)
475 {
476         uint16_t vf;
477         struct e1000_hw *hw = 
478                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
479
480         for (vf = 0; vf < dev_num_vf(eth_dev); vf++) {
481                 /* check & process vf function level reset */
482                 if (!e1000_check_for_rst(hw, vf))
483                         igb_vf_reset_event(eth_dev, vf);
484
485                 /* check & process vf mailbox messages */
486                 if (!e1000_check_for_msg(hw, vf))
487                         igb_rcv_msg_from_vf(eth_dev, vf);
488
489                 /* check & process acks from vf */
490                 if (!e1000_check_for_ack(hw, vf))
491                         igb_rcv_ack_from_vf(eth_dev, vf);
492         }
493 }