e1000: support port hotplug
[dpdk.git] / drivers / net / e1000 / igb_pf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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 "base/e1000_defines.h"
53 #include "base/e1000_regs.h"
54 #include "base/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 void igb_pf_host_uninit(struct rte_eth_dev *dev)
131 {
132         struct e1000_vf_info **vfinfo;
133         uint16_t vf_num;
134
135         PMD_INIT_FUNC_TRACE();
136
137         vfinfo = E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
138
139         RTE_ETH_DEV_SRIOV(dev).active = 0;
140         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool = 0;
141         RTE_ETH_DEV_SRIOV(dev).def_vmdq_idx = 0;
142         RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx = 0;
143
144         vf_num = dev_num_vf(dev);
145         if (vf_num == 0)
146                 return;
147
148         rte_free(*vfinfo);
149         *vfinfo = NULL;
150 }
151
152 #define E1000_RAH_POOLSEL_SHIFT    (18)
153 int igb_pf_host_configure(struct rte_eth_dev *eth_dev)
154 {
155         uint32_t vtctl;
156         uint16_t vf_num;
157         struct e1000_hw *hw =
158                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
159         uint32_t vlanctrl;
160         int i;
161         uint32_t rah;
162
163         if (0 == (vf_num = dev_num_vf(eth_dev)))
164                 return -1;
165
166         /* enable VMDq and set the default pool for PF */
167         vtctl = E1000_READ_REG(hw, E1000_VT_CTL);
168         vtctl &= ~E1000_VT_CTL_DEFAULT_POOL_MASK;
169         vtctl |= RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx
170                 << E1000_VT_CTL_DEFAULT_POOL_SHIFT;
171         vtctl |= E1000_VT_CTL_VM_REPL_EN;
172         E1000_WRITE_REG(hw, E1000_VT_CTL, vtctl);
173
174         /* Enable pools reserved to PF only */
175         E1000_WRITE_REG(hw, E1000_VFRE, (~0) << vf_num);
176         E1000_WRITE_REG(hw, E1000_VFTE, (~0) << vf_num);
177
178         /* PFDMA Tx General Switch Control Enables VMDQ loopback */
179         if (hw->mac.type == e1000_i350)
180                 E1000_WRITE_REG(hw, E1000_TXSWC, E1000_DTXSWC_VMDQ_LOOPBACK_EN);
181         else
182                 E1000_WRITE_REG(hw, E1000_DTXSWC, E1000_DTXSWC_VMDQ_LOOPBACK_EN);
183
184         /* clear VMDq map to perment rar 0 */
185         rah = E1000_READ_REG(hw, E1000_RAH(0));
186         rah &= ~ (0xFF << E1000_RAH_POOLSEL_SHIFT);
187         E1000_WRITE_REG(hw, E1000_RAH(0), rah);
188
189         /* clear VMDq map to scan rar 32 */
190         rah = E1000_READ_REG(hw, E1000_RAH(hw->mac.rar_entry_count));
191         rah &= ~ (0xFF << E1000_RAH_POOLSEL_SHIFT);
192         E1000_WRITE_REG(hw, E1000_RAH(hw->mac.rar_entry_count), rah);
193
194         /* set VMDq map to default PF pool */
195         rah = E1000_READ_REG(hw, E1000_RAH(0));
196         rah |= (0x1 << (RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx +
197                         E1000_RAH_POOLSEL_SHIFT));
198         E1000_WRITE_REG(hw, E1000_RAH(0), rah);
199
200         /*
201          * enable vlan filtering and allow all vlan tags through
202          */
203         vlanctrl = E1000_READ_REG(hw, E1000_RCTL);
204         vlanctrl |= E1000_RCTL_VFE ; /* enable vlan filters */
205         E1000_WRITE_REG(hw, E1000_RCTL, vlanctrl);
206
207         /* VFTA - enable all vlan filters */
208         for (i = 0; i < IGB_VFTA_SIZE; i++) {
209                 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, i, 0xFFFFFFFF);
210         }
211
212         /* Enable/Disable MAC Anti-Spoofing */
213         e1000_vmdq_set_anti_spoofing_pf(hw, FALSE, vf_num);
214
215         return 0;
216 }
217
218 static void
219 set_rx_mode(struct rte_eth_dev *dev)
220 {
221         struct rte_eth_dev_data *dev_data =
222                 (struct rte_eth_dev_data*)dev->data->dev_private;
223         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
224         uint32_t fctrl, vmolr = E1000_VMOLR_BAM | E1000_VMOLR_AUPE;
225         uint16_t vfn = dev_num_vf(dev);
226
227         /* Check for Promiscuous and All Multicast modes */
228         fctrl = E1000_READ_REG(hw, E1000_RCTL);
229
230         /* set all bits that we expect to always be set */
231         fctrl &= ~E1000_RCTL_SBP; /* disable store-bad-packets */
232         fctrl |= E1000_RCTL_BAM;;
233
234         /* clear the bits we are changing the status of */
235         fctrl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
236
237         if (dev_data->promiscuous) {
238                 fctrl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
239                 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
240         } else {
241                 if (dev_data->all_multicast) {
242                         fctrl |= E1000_RCTL_MPE;
243                         vmolr |= E1000_VMOLR_MPME;
244                 } else {
245                         vmolr |= E1000_VMOLR_ROMPE;
246                 }
247         }
248
249         if ((hw->mac.type == e1000_82576) ||
250                 (hw->mac.type == e1000_i350)) {
251                 vmolr |= E1000_READ_REG(hw, E1000_VMOLR(vfn)) &
252                          ~(E1000_VMOLR_MPME | E1000_VMOLR_ROMPE |
253                            E1000_VMOLR_ROPE);
254                 E1000_WRITE_REG(hw, E1000_VMOLR(vfn), vmolr);
255         }
256
257         E1000_WRITE_REG(hw, E1000_RCTL, fctrl);
258 }
259
260 static inline void
261 igb_vf_reset_event(struct rte_eth_dev *dev, uint16_t vf)
262 {
263         struct e1000_hw *hw =
264                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
265         struct e1000_vf_info *vfinfo =
266                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
267         uint32_t vmolr = E1000_READ_REG(hw, E1000_VMOLR(vf));
268
269         vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE |
270                         E1000_VMOLR_BAM | E1000_VMOLR_AUPE);
271         E1000_WRITE_REG(hw, E1000_VMOLR(vf), vmolr);
272
273         E1000_WRITE_REG(hw, E1000_VMVIR(vf), 0);
274
275         /* reset multicast table array for vf */
276         vfinfo[vf].num_vf_mc_hashes = 0;
277
278         /* reset rx mode */
279         set_rx_mode(dev);
280 }
281
282 static inline void
283 igb_vf_reset_msg(struct rte_eth_dev *dev, uint16_t vf)
284 {
285         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
286         uint32_t reg;
287
288         /* enable transmit and receive for vf */
289         reg = E1000_READ_REG(hw, E1000_VFTE);
290         reg |= (reg | (1 << vf));
291         E1000_WRITE_REG(hw, E1000_VFTE, reg);
292
293         reg = E1000_READ_REG(hw, E1000_VFRE);
294         reg |= (reg | (1 << vf));
295         E1000_WRITE_REG(hw, E1000_VFRE, reg);
296
297         igb_vf_reset_event(dev, vf);
298 }
299
300 static int
301 igb_vf_reset(struct rte_eth_dev *dev, uint16_t vf, uint32_t *msgbuf)
302 {
303         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
304         struct e1000_vf_info *vfinfo =
305                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
306         unsigned char *vf_mac = vfinfo[vf].vf_mac_addresses;
307         int rar_entry = hw->mac.rar_entry_count - (vf + 1);
308         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
309         uint32_t rah;
310
311         igb_vf_reset_msg(dev, vf);
312
313         hw->mac.ops.rar_set(hw, vf_mac, rar_entry);
314         rah = E1000_READ_REG(hw, E1000_RAH(rar_entry));
315         rah |= (0x1 << (vf + E1000_RAH_POOLSEL_SHIFT));
316         E1000_WRITE_REG(hw, E1000_RAH(rar_entry), rah);
317
318         /* reply to reset with ack and vf mac address */
319         msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
320         rte_memcpy(new_mac, vf_mac, ETHER_ADDR_LEN);
321         e1000_write_mbx(hw, msgbuf, 3, vf);
322
323         return 0;
324 }
325
326 static int
327 igb_vf_set_mac_addr(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
328 {
329         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
330         struct e1000_vf_info *vfinfo =
331                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
332         int rar_entry = hw->mac.rar_entry_count - (vf + 1);
333         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
334
335         if (is_valid_assigned_ether_addr((struct ether_addr*)new_mac)) {
336                 rte_memcpy(vfinfo[vf].vf_mac_addresses, new_mac, 6);
337                 hw->mac.ops.rar_set(hw, new_mac, rar_entry);
338                 return 0;
339         }
340         return -1;
341 }
342
343 static int
344 igb_vf_set_multicast(struct rte_eth_dev *dev, __rte_unused uint32_t vf, uint32_t *msgbuf)
345 {
346         int i;
347         uint32_t vector_bit;
348         uint32_t vector_reg;
349         uint32_t mta_reg;
350         int entries = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >>
351                 E1000_VT_MSGINFO_SHIFT;
352         uint16_t *hash_list = (uint16_t *)&msgbuf[1];
353         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
354         struct e1000_vf_info *vfinfo =
355                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
356
357         /* only so many hash values supported */
358         entries = RTE_MIN(entries, E1000_MAX_VF_MC_ENTRIES);
359
360         /*
361          * salt away the number of multi cast addresses assigned
362          * to this VF for later use to restore when the PF multi cast
363          * list changes
364          */
365         vfinfo->num_vf_mc_hashes = (uint16_t)entries;
366
367         /*
368          * VFs are limited to using the MTA hash table for their multicast
369          * addresses
370          */
371         for (i = 0; i < entries; i++) {
372                 vfinfo->vf_mc_hashes[i] = hash_list[i];
373         }
374
375         for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
376                 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
377                 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
378                 mta_reg = E1000_READ_REG_ARRAY(hw, E1000_MTA, vector_reg);
379                 mta_reg |= (1 << vector_bit);
380                 E1000_WRITE_REG_ARRAY(hw, E1000_MTA, vector_reg, mta_reg);
381         }
382
383         return 0;
384 }
385
386 static int
387 igb_vf_set_vlan(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
388 {
389         int add, vid;
390         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
391         struct e1000_vf_info *vfinfo =
392                 *(E1000_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
393         uint32_t vid_idx, vid_bit, vfta;
394
395         add = (msgbuf[0] & E1000_VT_MSGINFO_MASK)
396                 >> E1000_VT_MSGINFO_SHIFT;
397         vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
398
399         if (add)
400                 vfinfo[vf].vlan_count++;
401         else if (vfinfo[vf].vlan_count)
402                 vfinfo[vf].vlan_count--;
403
404         vid_idx = (uint32_t)((vid >> E1000_VFTA_ENTRY_SHIFT) &
405                              E1000_VFTA_ENTRY_MASK);
406         vid_bit = (uint32_t)(1 << (vid & E1000_VFTA_ENTRY_BIT_SHIFT_MASK));
407         vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, vid_idx);
408         if (add)
409                 vfta |= vid_bit;
410         else
411                 vfta &= ~vid_bit;
412
413         E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, vid_idx, vfta);
414         E1000_WRITE_FLUSH(hw);
415
416         return 0;
417 }
418
419 static int
420 igb_vf_set_rlpml(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
421 {
422         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
423         uint16_t rlpml = msgbuf[1] & E1000_VMOLR_RLPML_MASK;
424         uint32_t max_frame = rlpml + ETHER_HDR_LEN + ETHER_CRC_LEN;
425         uint32_t vmolr;
426
427         if ((max_frame < ETHER_MIN_LEN) || (max_frame > ETHER_MAX_JUMBO_FRAME_LEN))
428                 return -1;
429
430         vmolr = E1000_READ_REG(hw, E1000_VMOLR(vf));
431
432         vmolr &= ~E1000_VMOLR_RLPML_MASK;
433         vmolr |= rlpml;
434
435         /* Enable Long Packet support */
436         vmolr |= E1000_VMOLR_LPE;
437
438         E1000_WRITE_REG(hw, E1000_VMOLR(vf), vmolr);
439         E1000_WRITE_FLUSH(hw);
440
441         return 0;
442 }
443
444 static int
445 igb_rcv_msg_from_vf(struct rte_eth_dev *dev, uint16_t vf)
446 {
447         uint16_t mbx_size = E1000_VFMAILBOX_SIZE;
448         uint32_t msgbuf[E1000_VFMAILBOX_SIZE];
449         int32_t retval;
450         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
451
452         retval = e1000_read_mbx(hw, msgbuf, mbx_size, vf);
453         if (retval) {
454                 PMD_INIT_LOG(ERR, "Error mbx recv msg from VF %d", vf);
455                 return retval;
456         }
457
458         /* do nothing with the message already processed */
459         if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
460                 return retval;
461
462         /* flush the ack before we write any messages back */
463         E1000_WRITE_FLUSH(hw);
464
465         /* perform VF reset */
466         if (msgbuf[0] == E1000_VF_RESET) {
467                 return igb_vf_reset(dev, vf, msgbuf);
468         }
469
470         /* check & process VF to PF mailbox message */
471         switch ((msgbuf[0] & 0xFFFF)) {
472         case E1000_VF_SET_MAC_ADDR:
473                 retval = igb_vf_set_mac_addr(dev, vf, msgbuf);
474                 break;
475         case E1000_VF_SET_MULTICAST:
476                 retval = igb_vf_set_multicast(dev, vf, msgbuf);
477                 break;
478         case E1000_VF_SET_LPE:
479                 retval = igb_vf_set_rlpml(dev, vf, msgbuf);
480                 break;
481         case E1000_VF_SET_VLAN:
482                 retval = igb_vf_set_vlan(dev, vf, msgbuf);
483                 break;
484         default:
485                 PMD_INIT_LOG(DEBUG, "Unhandled Msg %8.8x",
486                              (unsigned) msgbuf[0]);
487                 retval = E1000_ERR_MBX;
488                 break;
489         }
490
491         /* response the VF according to the message process result */
492         if (retval)
493                 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
494         else
495                 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
496
497         msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
498
499         e1000_write_mbx(hw, msgbuf, 1, vf);
500
501         return retval;
502 }
503
504 static inline void
505 igb_rcv_ack_from_vf(struct rte_eth_dev *dev, uint16_t vf)
506 {
507         uint32_t msg = E1000_VT_MSGTYPE_NACK;
508         struct e1000_hw *hw =
509                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
510
511         e1000_write_mbx(hw, &msg, 1, vf);
512 }
513
514 void igb_pf_mbx_process(struct rte_eth_dev *eth_dev)
515 {
516         uint16_t vf;
517         struct e1000_hw *hw =
518                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
519
520         for (vf = 0; vf < dev_num_vf(eth_dev); vf++) {
521                 /* check & process vf function level reset */
522                 if (!e1000_check_for_rst(hw, vf))
523                         igb_vf_reset_event(eth_dev, vf);
524
525                 /* check & process vf mailbox messages */
526                 if (!e1000_check_for_msg(hw, vf))
527                         igb_rcv_msg_from_vf(eth_dev, vf);
528
529                 /* check & process acks from vf */
530                 if (!e1000_check_for_ack(hw, vf))
531                         igb_rcv_ack_from_vf(eth_dev, vf);
532         }
533 }