1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/types.h>
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/vmalloc.h>
33#include <linux/string.h>
34#include <linux/in.h>
35#include <linux/ip.h>
36#include <linux/tcp.h>
37#include <linux/ipv6.h>
38#ifdef NETIF_F_HW_VLAN_TX
39#include <linux/if_vlan.h>
40#endif
41
42#include "ixgbe.h"
43#include "ixgbe_type.h"
44#include "ixgbe_sriov.h"
45
46#ifdef CONFIG_PCI_IOV
47static int ixgbe_find_enabled_vfs(struct ixgbe_adapter *adapter)
48{
49 struct pci_dev *pdev = adapter->pdev;
50 struct pci_dev *pvfdev;
51 u16 vf_devfn = 0;
52 int device_id;
53 int vfs_found = 0;
54
55 switch (adapter->hw.mac.type) {
56 case ixgbe_mac_82599EB:
57 device_id = IXGBE_DEV_ID_82599_VF;
58 break;
59 case ixgbe_mac_X540:
60 device_id = IXGBE_DEV_ID_X540_VF;
61 break;
62 default:
63 device_id = 0;
64 break;
65 }
66
67 vf_devfn = pdev->devfn + 0x80;
68 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
69 while (pvfdev) {
70 if (pvfdev->devfn == vf_devfn &&
71 (pvfdev->bus->number >= pdev->bus->number))
72 vfs_found++;
73 vf_devfn += 2;
74 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
75 device_id, pvfdev);
76 }
77
78 return vfs_found;
79}
80
81void ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
82 const struct ixgbe_info *ii)
83{
84 struct ixgbe_hw *hw = &adapter->hw;
85 int err = 0;
86 int num_vf_macvlans, i;
87 struct vf_macvlans *mv_list;
88 int pre_existing_vfs = 0;
89
90 pre_existing_vfs = ixgbe_find_enabled_vfs(adapter);
91 if (!pre_existing_vfs && !adapter->num_vfs)
92 return;
93
94
95
96
97
98
99
100
101
102
103 if (pre_existing_vfs) {
104 adapter->num_vfs = pre_existing_vfs;
105 dev_warn(&adapter->pdev->dev, "Virtual Functions already "
106 "enabled for this device - Please reload all "
107 "VF drivers to avoid spoofed packet errors\n");
108 } else {
109 err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
110 }
111 if (err) {
112 e_err(probe, "Failed to enable PCI sriov: %d\n", err);
113 goto err_novfs;
114 }
115 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
116
117 e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
118
119 num_vf_macvlans = hw->mac.num_rar_entries -
120 (IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
121
122 adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
123 sizeof(struct vf_macvlans),
124 GFP_KERNEL);
125 if (mv_list) {
126
127 INIT_LIST_HEAD(&adapter->vf_mvs.l);
128 for (i = 0; i < num_vf_macvlans; i++) {
129 mv_list->vf = -1;
130 mv_list->free = true;
131 mv_list->rar_entry = hw->mac.num_rar_entries -
132 (i + adapter->num_vfs + 1);
133 list_add(&mv_list->l, &adapter->vf_mvs.l);
134 mv_list++;
135 }
136 }
137
138
139
140
141 adapter->vfinfo =
142 kcalloc(adapter->num_vfs,
143 sizeof(struct vf_data_storage), GFP_KERNEL);
144 if (adapter->vfinfo) {
145
146
147
148 ixgbe_init_mbx_params_pf(hw);
149 memcpy(&hw->mbx.ops, ii->mbx_ops,
150 sizeof(hw->mbx.ops));
151
152
153 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
154 IXGBE_FLAG2_RSC_ENABLED);
155 for (i = 0; i < adapter->num_vfs; i++)
156 adapter->vfinfo[i].spoofchk_enabled = true;
157 return;
158 }
159
160
161 e_err(probe, "Unable to allocate memory for VF Data Storage - "
162 "SRIOV disabled\n");
163 pci_disable_sriov(adapter->pdev);
164
165err_novfs:
166 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
167 adapter->num_vfs = 0;
168}
169#endif
170
171void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
172{
173 struct ixgbe_hw *hw = &adapter->hw;
174 u32 gcr;
175 u32 gpie;
176 u32 vmdctl;
177 int i;
178
179#ifdef CONFIG_PCI_IOV
180
181 pci_disable_sriov(adapter->pdev);
182#endif
183
184
185 gcr = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
186 gcr &= ~(IXGBE_GCR_EXT_SRIOV);
187 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr);
188 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
189 gpie &= ~IXGBE_GPIE_VTMODE_MASK;
190 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
191
192
193 vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
194 vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
195 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
196 IXGBE_WRITE_FLUSH(hw);
197
198
199 msleep(100);
200
201
202 for (i = 0; i < adapter->num_vfs; i++) {
203 if (adapter->vfinfo[i].vfdev)
204 pci_dev_put(adapter->vfinfo[i].vfdev);
205 }
206 kfree(adapter->vfinfo);
207 kfree(adapter->mv_list);
208 adapter->vfinfo = NULL;
209
210 adapter->num_vfs = 0;
211 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
212}
213
214static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
215 int entries, u16 *hash_list, u32 vf)
216{
217 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
218 struct ixgbe_hw *hw = &adapter->hw;
219 int i;
220 u32 vector_bit;
221 u32 vector_reg;
222 u32 mta_reg;
223
224
225 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
226
227
228
229
230
231
232 vfinfo->num_vf_mc_hashes = entries;
233
234
235
236
237
238 for (i = 0; i < entries; i++) {
239 vfinfo->vf_mc_hashes[i] = hash_list[i];
240 }
241
242 for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
243 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
244 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
245 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
246 mta_reg |= (1 << vector_bit);
247 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
248 }
249
250 return 0;
251}
252
253static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
254{
255 struct ixgbe_hw *hw = &adapter->hw;
256 struct list_head *pos;
257 struct vf_macvlans *entry;
258
259 list_for_each(pos, &adapter->vf_mvs.l) {
260 entry = list_entry(pos, struct vf_macvlans, l);
261 if (entry->free == false)
262 hw->mac.ops.set_rar(hw, entry->rar_entry,
263 entry->vf_macvlan,
264 entry->vf, IXGBE_RAH_AV);
265 }
266}
267
268void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
269{
270 struct ixgbe_hw *hw = &adapter->hw;
271 struct vf_data_storage *vfinfo;
272 int i, j;
273 u32 vector_bit;
274 u32 vector_reg;
275 u32 mta_reg;
276
277 for (i = 0; i < adapter->num_vfs; i++) {
278 vfinfo = &adapter->vfinfo[i];
279 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
280 hw->addr_ctrl.mta_in_use++;
281 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
282 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
283 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
284 mta_reg |= (1 << vector_bit);
285 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
286 }
287 }
288
289
290 ixgbe_restore_vf_macvlans(adapter);
291}
292
293static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
294 u32 vf)
295{
296 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
297}
298
299static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
300{
301 struct ixgbe_hw *hw = &adapter->hw;
302 int new_mtu = msgbuf[1];
303 u32 max_frs;
304 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
305
306
307 if (adapter->hw.mac.type != ixgbe_mac_X540)
308 return;
309
310
311 if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE)) {
312 e_err(drv, "VF mtu %d out of range\n", new_mtu);
313 return;
314 }
315
316 max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
317 IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
318 if (max_frs < new_mtu) {
319 max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
320 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
321 }
322
323 e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
324}
325
326static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
327{
328 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
329 vmolr |= (IXGBE_VMOLR_ROMPE |
330 IXGBE_VMOLR_BAM);
331 if (aupe)
332 vmolr |= IXGBE_VMOLR_AUPE;
333 else
334 vmolr &= ~IXGBE_VMOLR_AUPE;
335 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
336}
337
338static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
339{
340 struct ixgbe_hw *hw = &adapter->hw;
341
342 if (vid)
343 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
344 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
345 else
346 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
347}
348
349static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
350{
351 struct ixgbe_hw *hw = &adapter->hw;
352 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
353
354
355 if (adapter->vfinfo[vf].pf_vlan) {
356 ixgbe_set_vf_vlan(adapter, true,
357 adapter->vfinfo[vf].pf_vlan, vf);
358 ixgbe_set_vmvir(adapter,
359 (adapter->vfinfo[vf].pf_vlan |
360 (adapter->vfinfo[vf].pf_qos <<
361 VLAN_PRIO_SHIFT)), vf);
362 ixgbe_set_vmolr(hw, vf, false);
363 } else {
364 ixgbe_set_vmvir(adapter, 0, vf);
365 ixgbe_set_vmolr(hw, vf, true);
366 }
367
368
369 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
370
371
372 ixgbe_set_rx_mode(adapter->netdev);
373
374 hw->mac.ops.clear_rar(hw, rar_entry);
375}
376
377static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
378 int vf, unsigned char *mac_addr)
379{
380 struct ixgbe_hw *hw = &adapter->hw;
381 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
382
383 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
384 hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
385
386 return 0;
387}
388
389static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
390 int vf, int index, unsigned char *mac_addr)
391{
392 struct ixgbe_hw *hw = &adapter->hw;
393 struct list_head *pos;
394 struct vf_macvlans *entry;
395
396 if (index <= 1) {
397 list_for_each(pos, &adapter->vf_mvs.l) {
398 entry = list_entry(pos, struct vf_macvlans, l);
399 if (entry->vf == vf) {
400 entry->vf = -1;
401 entry->free = true;
402 entry->is_macvlan = false;
403 hw->mac.ops.clear_rar(hw, entry->rar_entry);
404 }
405 }
406 }
407
408
409
410
411
412 if (!index)
413 return 0;
414
415 entry = NULL;
416
417 list_for_each(pos, &adapter->vf_mvs.l) {
418 entry = list_entry(pos, struct vf_macvlans, l);
419 if (entry->free)
420 break;
421 }
422
423
424
425
426
427
428
429
430
431 if (!entry || !entry->free)
432 return -ENOSPC;
433
434 entry->free = false;
435 entry->is_macvlan = true;
436 entry->vf = vf;
437 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
438
439 hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
440
441 return 0;
442}
443
444int ixgbe_check_vf_assignment(struct ixgbe_adapter *adapter)
445{
446#ifdef CONFIG_PCI_IOV
447 int i;
448 for (i = 0; i < adapter->num_vfs; i++) {
449 if (adapter->vfinfo[i].vfdev->dev_flags &
450 PCI_DEV_FLAGS_ASSIGNED)
451 return true;
452 }
453#endif
454 return false;
455}
456
457int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
458{
459 unsigned char vf_mac_addr[6];
460 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
461 unsigned int vfn = (event_mask & 0x3f);
462 struct pci_dev *pvfdev;
463 unsigned int device_id;
464 u16 thisvf_devfn = (pdev->devfn + 0x80 + (vfn << 1)) |
465 (pdev->devfn & 1);
466
467 bool enable = ((event_mask & 0x10000000U) != 0);
468
469 if (enable) {
470 random_ether_addr(vf_mac_addr);
471 e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
472 vfn, vf_mac_addr);
473
474
475
476
477 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
478
479 switch (adapter->hw.mac.type) {
480 case ixgbe_mac_82599EB:
481 device_id = IXGBE_DEV_ID_82599_VF;
482 break;
483 case ixgbe_mac_X540:
484 device_id = IXGBE_DEV_ID_X540_VF;
485 break;
486 default:
487 device_id = 0;
488 break;
489 }
490
491 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
492 while (pvfdev) {
493 if (pvfdev->devfn == thisvf_devfn)
494 break;
495 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
496 device_id, pvfdev);
497 }
498 if (pvfdev)
499 adapter->vfinfo[vfn].vfdev = pvfdev;
500 else
501 e_err(drv, "Couldn't find pci dev ptr for VF %4.4x\n",
502 thisvf_devfn);
503 }
504
505 return 0;
506}
507
508static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
509{
510 struct ixgbe_hw *hw = &adapter->hw;
511 u32 reg;
512 u32 reg_offset, vf_shift;
513
514 vf_shift = vf % 32;
515 reg_offset = vf / 32;
516
517
518 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
519 reg |= (reg | (1 << vf_shift));
520 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
521
522 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
523 reg |= (reg | (1 << vf_shift));
524 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
525
526
527 reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
528 reg |= (1 << vf_shift);
529 IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
530
531 ixgbe_vf_reset_event(adapter, vf);
532}
533
534static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
535{
536 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
537 u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
538 struct ixgbe_hw *hw = &adapter->hw;
539 s32 retval;
540 int entries;
541 u16 *hash_list;
542 int add, vid, index;
543 u8 *new_mac;
544
545 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
546
547 if (retval)
548 pr_err("Error receiving message from VF\n");
549
550
551 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
552 return retval;
553
554
555
556
557
558
559 if (msgbuf[0] == IXGBE_VF_RESET) {
560 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
561 new_mac = (u8 *)(&msgbuf[1]);
562 e_info(probe, "VF Reset msg received from vf %d\n", vf);
563 adapter->vfinfo[vf].clear_to_send = false;
564 ixgbe_vf_reset_msg(adapter, vf);
565 adapter->vfinfo[vf].clear_to_send = true;
566
567 if (is_valid_ether_addr(new_mac) &&
568 !adapter->vfinfo[vf].pf_set_mac)
569 ixgbe_set_vf_mac(adapter, vf, vf_mac);
570 else
571 ixgbe_set_vf_mac(adapter,
572 vf, adapter->vfinfo[vf].vf_mac_addresses);
573
574
575 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
576 memcpy(new_mac, vf_mac, IXGBE_ETH_LENGTH_OF_ADDRESS);
577
578
579
580
581 msgbuf[3] = hw->mac.mc_filter_type;
582 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
583
584 return retval;
585 }
586
587 if (!adapter->vfinfo[vf].clear_to_send) {
588 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
589 ixgbe_write_mbx(hw, msgbuf, 1, vf);
590 return retval;
591 }
592
593 switch ((msgbuf[0] & 0xFFFF)) {
594 case IXGBE_VF_SET_MAC_ADDR:
595 new_mac = ((u8 *)(&msgbuf[1]));
596 if (is_valid_ether_addr(new_mac) &&
597 !adapter->vfinfo[vf].pf_set_mac) {
598 ixgbe_set_vf_mac(adapter, vf, new_mac);
599 } else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
600 new_mac, ETH_ALEN)) {
601 e_warn(drv, "VF %d attempted to override "
602 "administratively set MAC address\nReload "
603 "the VF driver to resume operations\n", vf);
604 retval = -1;
605 }
606 break;
607 case IXGBE_VF_SET_MULTICAST:
608 entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
609 >> IXGBE_VT_MSGINFO_SHIFT;
610 hash_list = (u16 *)&msgbuf[1];
611 retval = ixgbe_set_vf_multicasts(adapter, entries,
612 hash_list, vf);
613 break;
614 case IXGBE_VF_SET_LPE:
615 ixgbe_set_vf_lpe(adapter, msgbuf);
616 break;
617 case IXGBE_VF_SET_VLAN:
618 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
619 >> IXGBE_VT_MSGINFO_SHIFT;
620 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
621 if (adapter->vfinfo[vf].pf_vlan) {
622 e_warn(drv, "VF %d attempted to override "
623 "administratively set VLAN configuration\n"
624 "Reload the VF driver to resume operations\n",
625 vf);
626 retval = -1;
627 } else {
628 if (add)
629 adapter->vfinfo[vf].vlan_count++;
630 else if (adapter->vfinfo[vf].vlan_count)
631 adapter->vfinfo[vf].vlan_count--;
632 retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
633 if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
634 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
635 }
636 break;
637 case IXGBE_VF_SET_MACVLAN:
638 index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
639 IXGBE_VT_MSGINFO_SHIFT;
640
641
642
643
644
645
646 if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
647 ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
648 retval = ixgbe_set_vf_macvlan(adapter, vf, index,
649 (unsigned char *)(&msgbuf[1]));
650 break;
651 default:
652 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
653 retval = IXGBE_ERR_MBX;
654 break;
655 }
656
657
658 if (retval)
659 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
660 else
661 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
662
663 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
664
665 ixgbe_write_mbx(hw, msgbuf, 1, vf);
666
667 return retval;
668}
669
670static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
671{
672 struct ixgbe_hw *hw = &adapter->hw;
673 u32 msg = IXGBE_VT_MSGTYPE_NACK;
674
675
676 if (!adapter->vfinfo[vf].clear_to_send)
677 ixgbe_write_mbx(hw, &msg, 1, vf);
678}
679
680void ixgbe_msg_task(struct ixgbe_adapter *adapter)
681{
682 struct ixgbe_hw *hw = &adapter->hw;
683 u32 vf;
684
685 for (vf = 0; vf < adapter->num_vfs; vf++) {
686
687 if (!ixgbe_check_for_rst(hw, vf))
688 ixgbe_vf_reset_event(adapter, vf);
689
690
691 if (!ixgbe_check_for_msg(hw, vf))
692 ixgbe_rcv_msg_from_vf(adapter, vf);
693
694
695 if (!ixgbe_check_for_ack(hw, vf))
696 ixgbe_rcv_ack_from_vf(adapter, vf);
697 }
698}
699
700void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
701{
702 struct ixgbe_hw *hw = &adapter->hw;
703
704
705 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
706 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
707
708 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
709 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
710}
711
712void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
713{
714 struct ixgbe_hw *hw = &adapter->hw;
715 u32 ping;
716 int i;
717
718 for (i = 0 ; i < adapter->num_vfs; i++) {
719 ping = IXGBE_PF_CONTROL_MSG;
720 if (adapter->vfinfo[i].clear_to_send)
721 ping |= IXGBE_VT_MSGTYPE_CTS;
722 ixgbe_write_mbx(hw, &ping, 1, i);
723 }
724}
725
726int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
727{
728 struct ixgbe_adapter *adapter = netdev_priv(netdev);
729 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
730 return -EINVAL;
731 adapter->vfinfo[vf].pf_set_mac = true;
732 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
733 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
734 " change effective.");
735 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
736 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
737 " but the PF device is not up.\n");
738 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
739 " attempting to use the VF device.\n");
740 }
741 return ixgbe_set_vf_mac(adapter, vf, mac);
742}
743
744int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
745{
746 int err = 0;
747 struct ixgbe_adapter *adapter = netdev_priv(netdev);
748 struct ixgbe_hw *hw = &adapter->hw;
749
750 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
751 return -EINVAL;
752 if (vlan || qos) {
753 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
754 if (err)
755 goto out;
756 ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
757 ixgbe_set_vmolr(hw, vf, false);
758 if (adapter->vfinfo[vf].spoofchk_enabled)
759 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
760 adapter->vfinfo[vf].vlan_count++;
761 adapter->vfinfo[vf].pf_vlan = vlan;
762 adapter->vfinfo[vf].pf_qos = qos;
763 dev_info(&adapter->pdev->dev,
764 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
765 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
766 dev_warn(&adapter->pdev->dev,
767 "The VF VLAN has been set,"
768 " but the PF device is not up.\n");
769 dev_warn(&adapter->pdev->dev,
770 "Bring the PF device up before"
771 " attempting to use the VF device.\n");
772 }
773 } else {
774 err = ixgbe_set_vf_vlan(adapter, false,
775 adapter->vfinfo[vf].pf_vlan, vf);
776 ixgbe_set_vmvir(adapter, vlan, vf);
777 ixgbe_set_vmolr(hw, vf, true);
778 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
779 if (adapter->vfinfo[vf].vlan_count)
780 adapter->vfinfo[vf].vlan_count--;
781 adapter->vfinfo[vf].pf_vlan = 0;
782 adapter->vfinfo[vf].pf_qos = 0;
783 }
784out:
785 return err;
786}
787
788static int ixgbe_link_mbps(int internal_link_speed)
789{
790 switch (internal_link_speed) {
791 case IXGBE_LINK_SPEED_100_FULL:
792 return 100;
793 case IXGBE_LINK_SPEED_1GB_FULL:
794 return 1000;
795 case IXGBE_LINK_SPEED_10GB_FULL:
796 return 10000;
797 default:
798 return 0;
799 }
800}
801
802static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
803 int link_speed)
804{
805 int rf_dec, rf_int;
806 u32 bcnrc_val;
807
808 if (tx_rate != 0) {
809
810 rf_int = link_speed / tx_rate;
811 rf_dec = (link_speed - (rf_int * tx_rate));
812 rf_dec = (rf_dec * (1<<IXGBE_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
813
814 bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
815 bcnrc_val |= ((rf_int<<IXGBE_RTTBCNRC_RF_INT_SHIFT) &
816 IXGBE_RTTBCNRC_RF_INT_MASK);
817 bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
818 } else {
819 bcnrc_val = 0;
820 }
821
822 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf);
823
824
825
826
827
828 switch (hw->mac.type) {
829 case ixgbe_mac_82599EB:
830 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
831 break;
832 case ixgbe_mac_X540:
833 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
834 break;
835 default:
836 break;
837 }
838
839 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
840}
841
842void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
843{
844 int actual_link_speed, i;
845 bool reset_rate = false;
846
847
848 if (adapter->vf_rate_link_speed == 0)
849 return;
850
851 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
852 if (actual_link_speed != adapter->vf_rate_link_speed) {
853 reset_rate = true;
854 adapter->vf_rate_link_speed = 0;
855 dev_info(&adapter->pdev->dev,
856 "Link speed has been changed. VF Transmit rate "
857 "is disabled\n");
858 }
859
860 for (i = 0; i < adapter->num_vfs; i++) {
861 if (reset_rate)
862 adapter->vfinfo[i].tx_rate = 0;
863
864 ixgbe_set_vf_rate_limit(&adapter->hw, i,
865 adapter->vfinfo[i].tx_rate,
866 actual_link_speed);
867 }
868}
869
870int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
871{
872 struct ixgbe_adapter *adapter = netdev_priv(netdev);
873 struct ixgbe_hw *hw = &adapter->hw;
874 int actual_link_speed;
875
876 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
877 if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
878 (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
879 ((tx_rate != 0) && (tx_rate <= 10)))
880
881 return -EINVAL;
882
883 adapter->vf_rate_link_speed = actual_link_speed;
884 adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
885 ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
886
887 return 0;
888}
889
890int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
891{
892 struct ixgbe_adapter *adapter = netdev_priv(netdev);
893 int vf_target_reg = vf >> 3;
894 int vf_target_shift = vf % 8;
895 struct ixgbe_hw *hw = &adapter->hw;
896 u32 regval;
897
898 adapter->vfinfo[vf].spoofchk_enabled = setting;
899
900 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
901 regval &= ~(1 << vf_target_shift);
902 regval |= (setting << vf_target_shift);
903 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
904
905 if (adapter->vfinfo[vf].vlan_count) {
906 vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
907 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
908 regval &= ~(1 << vf_target_shift);
909 regval |= (setting << vf_target_shift);
910 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
911 }
912
913 return 0;
914}
915
916int ixgbe_ndo_get_vf_config(struct net_device *netdev,
917 int vf, struct ifla_vf_info *ivi)
918{
919 struct ixgbe_adapter *adapter = netdev_priv(netdev);
920 if (vf >= adapter->num_vfs)
921 return -EINVAL;
922 ivi->vf = vf;
923 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
924 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
925 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
926 ivi->qos = adapter->vfinfo[vf].pf_qos;
927 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
928 return 0;
929}
930