1 /* Intel(R) Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2017 Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
16 * Contact Information:
17 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 static s32
fm10k_iov_msg_error(struct fm10k_hw
*hw
, u32
**results
,
26 struct fm10k_mbx_info
*mbx
)
28 struct fm10k_vf_info
*vf_info
= (struct fm10k_vf_info
*)mbx
;
29 struct fm10k_intfc
*interface
= hw
->back
;
30 struct pci_dev
*pdev
= interface
->pdev
;
32 dev_err(&pdev
->dev
, "Unknown message ID %u on VF %d\n",
33 **results
& FM10K_TLV_ID_MASK
, vf_info
->vf_idx
);
35 return fm10k_tlv_msg_error(hw
, results
, mbx
);
39 * fm10k_iov_msg_queue_mac_vlan - Message handler for MAC/VLAN request from VF
40 * @hw: Pointer to hardware structure
41 * @results: Pointer array to message, results[0] is pointer to message
42 * @mbx: Pointer to mailbox information structure
44 * This function is a custom handler for MAC/VLAN requests from the VF. The
45 * assumption is that it is acceptable to directly hand off the message from
46 * the VF to the PF's switch manager. However, we use a MAC/VLAN message
47 * queue to avoid overloading the mailbox when a large number of requests
50 static s32
fm10k_iov_msg_queue_mac_vlan(struct fm10k_hw
*hw
, u32
**results
,
51 struct fm10k_mbx_info
*mbx
)
53 struct fm10k_vf_info
*vf_info
= (struct fm10k_vf_info
*)mbx
;
54 struct fm10k_intfc
*interface
= hw
->back
;
62 /* we shouldn't be updating rules on a disabled interface */
63 if (!FM10K_VF_FLAG_ENABLED(vf_info
))
64 err
= FM10K_ERR_PARAM
;
66 if (!err
&& !!results
[FM10K_MAC_VLAN_MSG_VLAN
]) {
67 result
= results
[FM10K_MAC_VLAN_MSG_VLAN
];
69 /* record VLAN id requested */
70 err
= fm10k_tlv_attr_get_u32(result
, &vid
);
74 set
= !(vid
& FM10K_VLAN_CLEAR
);
75 vid
&= ~FM10K_VLAN_CLEAR
;
77 /* if the length field has been set, this is a multi-bit
78 * update request. For multi-bit requests, simply disallow
79 * them when the pf_vid has been set. In this case, the PF
80 * should have already cleared the VLAN_TABLE, and if we
81 * allowed them, it could allow a rogue VF to receive traffic
82 * on a VLAN it was not assigned. In the single-bit case, we
83 * need to modify requests for VLAN 0 to use the default PF or
84 * SW vid when assigned.
88 /* prevent multi-bit requests when PF has
89 * administratively set the VLAN for this VF
92 return FM10K_ERR_PARAM
;
94 err
= fm10k_iov_select_vid(vf_info
, (u16
)vid
);
101 /* update VSI info for VF in regards to VLAN table */
102 err
= hw
->mac
.ops
.update_vlan(hw
, vid
, vf_info
->vsi
, set
);
105 if (!err
&& !!results
[FM10K_MAC_VLAN_MSG_MAC
]) {
106 result
= results
[FM10K_MAC_VLAN_MSG_MAC
];
108 /* record unicast MAC address requested */
109 err
= fm10k_tlv_attr_get_mac_vlan(result
, mac
, &vlan
);
113 /* block attempts to set MAC for a locked device */
114 if (is_valid_ether_addr(vf_info
->mac
) &&
115 !ether_addr_equal(mac
, vf_info
->mac
))
116 return FM10K_ERR_PARAM
;
118 set
= !(vlan
& FM10K_VLAN_CLEAR
);
119 vlan
&= ~FM10K_VLAN_CLEAR
;
121 err
= fm10k_iov_select_vid(vf_info
, vlan
);
127 /* Add this request to the MAC/VLAN queue */
128 err
= fm10k_queue_mac_request(interface
, vf_info
->glort
,
132 if (!err
&& !!results
[FM10K_MAC_VLAN_MSG_MULTICAST
]) {
133 result
= results
[FM10K_MAC_VLAN_MSG_MULTICAST
];
135 /* record multicast MAC address requested */
136 err
= fm10k_tlv_attr_get_mac_vlan(result
, mac
, &vlan
);
140 /* verify that the VF is allowed to request multicast */
141 if (!(vf_info
->vf_flags
& FM10K_VF_FLAG_MULTI_ENABLED
))
142 return FM10K_ERR_PARAM
;
144 set
= !(vlan
& FM10K_VLAN_CLEAR
);
145 vlan
&= ~FM10K_VLAN_CLEAR
;
147 err
= fm10k_iov_select_vid(vf_info
, vlan
);
153 /* Add this request to the MAC/VLAN queue */
154 err
= fm10k_queue_mac_request(interface
, vf_info
->glort
,
161 static const struct fm10k_msg_data iov_mbx_data
[] = {
162 FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test
),
163 FM10K_VF_MSG_MSIX_HANDLER(fm10k_iov_msg_msix_pf
),
164 FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_iov_msg_queue_mac_vlan
),
165 FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_iov_msg_lport_state_pf
),
166 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_iov_msg_error
),
169 s32
fm10k_iov_event(struct fm10k_intfc
*interface
)
171 struct fm10k_hw
*hw
= &interface
->hw
;
172 struct fm10k_iov_data
*iov_data
;
176 /* if there is no iov_data then there is no mailbox to process */
177 if (!READ_ONCE(interface
->iov_data
))
182 iov_data
= interface
->iov_data
;
184 /* check again now that we are in the RCU block */
188 if (!(fm10k_read_reg(hw
, FM10K_EICR
) & FM10K_EICR_VFLR
))
191 /* read VFLRE to determine if any VFs have been reset */
192 vflre
= fm10k_read_reg(hw
, FM10K_PFVFLRE(1));
194 vflre
|= fm10k_read_reg(hw
, FM10K_PFVFLRE(0));
196 i
= iov_data
->num_vfs
;
198 for (vflre
<<= 64 - i
; vflre
&& i
--; vflre
+= vflre
) {
199 struct fm10k_vf_info
*vf_info
= &iov_data
->vf_info
[i
];
204 hw
->iov
.ops
.reset_resources(hw
, vf_info
);
205 vf_info
->mbx
.ops
.connect(hw
, &vf_info
->mbx
);
214 s32
fm10k_iov_mbx(struct fm10k_intfc
*interface
)
216 struct fm10k_hw
*hw
= &interface
->hw
;
217 struct fm10k_iov_data
*iov_data
;
220 /* if there is no iov_data then there is no mailbox to process */
221 if (!READ_ONCE(interface
->iov_data
))
226 iov_data
= interface
->iov_data
;
228 /* check again now that we are in the RCU block */
232 /* lock the mailbox for transmit and receive */
233 fm10k_mbx_lock(interface
);
235 /* Most VF messages sent to the PF cause the PF to respond by
236 * requesting from the SM mailbox. This means that too many VF
237 * messages processed at once could cause a mailbox timeout on the PF.
238 * To prevent this, store a pointer to the next VF mbx to process. Use
239 * that as the start of the loop so that we don't starve whichever VF
240 * got ignored on the previous run.
243 for (i
= iov_data
->next_vf_mbx
? : iov_data
->num_vfs
; i
--;) {
244 struct fm10k_vf_info
*vf_info
= &iov_data
->vf_info
[i
];
245 struct fm10k_mbx_info
*mbx
= &vf_info
->mbx
;
246 u16 glort
= vf_info
->glort
;
248 /* process the SM mailbox first to drain outgoing messages */
249 hw
->mbx
.ops
.process(hw
, &hw
->mbx
);
251 /* verify port mapping is valid, if not reset port */
252 if (vf_info
->vf_flags
&& !fm10k_glort_valid_pf(hw
, glort
)) {
253 hw
->iov
.ops
.reset_lport(hw
, vf_info
);
254 fm10k_clear_macvlan_queue(interface
, glort
, false);
257 /* reset VFs that have mailbox timed out */
259 hw
->iov
.ops
.reset_resources(hw
, vf_info
);
260 mbx
->ops
.connect(hw
, mbx
);
263 /* guarantee we have free space in the SM mailbox */
264 if (!hw
->mbx
.ops
.tx_ready(&hw
->mbx
, FM10K_VFMBX_MSG_MTU
)) {
265 /* keep track of how many times this occurs */
266 interface
->hw_sm_mbx_full
++;
268 /* make sure we try again momentarily */
269 fm10k_service_event_schedule(interface
);
274 /* cleanup mailbox and process received messages */
275 mbx
->ops
.process(hw
, mbx
);
278 /* if we stopped processing mailboxes early, update next_vf_mbx.
279 * Otherwise, reset next_vf_mbx, and restart loop so that we process
280 * the remaining mailboxes we skipped at the start.
283 iov_data
->next_vf_mbx
= i
+ 1;
284 } else if (iov_data
->next_vf_mbx
) {
285 iov_data
->next_vf_mbx
= 0;
290 fm10k_mbx_unlock(interface
);
298 void fm10k_iov_suspend(struct pci_dev
*pdev
)
300 struct fm10k_intfc
*interface
= pci_get_drvdata(pdev
);
301 struct fm10k_iov_data
*iov_data
= interface
->iov_data
;
302 struct fm10k_hw
*hw
= &interface
->hw
;
305 /* pull out num_vfs from iov_data */
306 num_vfs
= iov_data
? iov_data
->num_vfs
: 0;
308 /* shut down queue mapping for VFs */
309 fm10k_write_reg(hw
, FM10K_DGLORTMAP(fm10k_dglort_vf_rss
),
310 FM10K_DGLORTMAP_NONE
);
312 /* Stop any active VFs and reset their resources */
313 for (i
= 0; i
< num_vfs
; i
++) {
314 struct fm10k_vf_info
*vf_info
= &iov_data
->vf_info
[i
];
316 hw
->iov
.ops
.reset_resources(hw
, vf_info
);
317 hw
->iov
.ops
.reset_lport(hw
, vf_info
);
318 fm10k_clear_macvlan_queue(interface
, vf_info
->glort
, false);
322 int fm10k_iov_resume(struct pci_dev
*pdev
)
324 struct fm10k_intfc
*interface
= pci_get_drvdata(pdev
);
325 struct fm10k_iov_data
*iov_data
= interface
->iov_data
;
326 struct fm10k_dglort_cfg dglort
= { 0 };
327 struct fm10k_hw
*hw
= &interface
->hw
;
330 /* pull out num_vfs from iov_data */
331 num_vfs
= iov_data
? iov_data
->num_vfs
: 0;
333 /* return error if iov_data is not already populated */
337 /* allocate hardware resources for the VFs */
338 hw
->iov
.ops
.assign_resources(hw
, num_vfs
, num_vfs
);
340 /* configure DGLORT mapping for RSS */
341 dglort
.glort
= hw
->mac
.dglort_map
& FM10K_DGLORTMAP_NONE
;
342 dglort
.idx
= fm10k_dglort_vf_rss
;
343 dglort
.inner_rss
= 1;
344 dglort
.rss_l
= fls(fm10k_queues_per_pool(hw
) - 1);
345 dglort
.queue_b
= fm10k_vf_queue_index(hw
, 0);
346 dglort
.vsi_l
= fls(hw
->iov
.total_vfs
- 1);
349 hw
->mac
.ops
.configure_dglort_map(hw
, &dglort
);
351 /* assign resources to the device */
352 for (i
= 0; i
< num_vfs
; i
++) {
353 struct fm10k_vf_info
*vf_info
= &iov_data
->vf_info
[i
];
355 /* allocate all but the last GLORT to the VFs */
356 if (i
== (~hw
->mac
.dglort_map
>> FM10K_DGLORTMAP_MASK_SHIFT
))
359 /* assign GLORT to VF, and restrict it to multicast */
360 hw
->iov
.ops
.set_lport(hw
, vf_info
, i
,
361 FM10K_VF_FLAG_MULTI_CAPABLE
);
363 /* mailbox is disconnected so we don't send a message */
364 hw
->iov
.ops
.assign_default_mac_vlan(hw
, vf_info
);
366 /* now we are ready so we can connect */
367 vf_info
->mbx
.ops
.connect(hw
, &vf_info
->mbx
);
373 s32
fm10k_iov_update_pvid(struct fm10k_intfc
*interface
, u16 glort
, u16 pvid
)
375 struct fm10k_iov_data
*iov_data
= interface
->iov_data
;
376 struct fm10k_hw
*hw
= &interface
->hw
;
377 struct fm10k_vf_info
*vf_info
;
378 u16 vf_idx
= (glort
- hw
->mac
.dglort_map
) & FM10K_DGLORTMAP_NONE
;
380 /* no IOV support, not our message to process */
382 return FM10K_ERR_PARAM
;
384 /* glort outside our range, not our message to process */
385 if (vf_idx
>= iov_data
->num_vfs
)
386 return FM10K_ERR_PARAM
;
388 /* determine if an update has occurred and if so notify the VF */
389 vf_info
= &iov_data
->vf_info
[vf_idx
];
390 if (vf_info
->sw_vid
!= pvid
) {
391 vf_info
->sw_vid
= pvid
;
392 hw
->iov
.ops
.assign_default_mac_vlan(hw
, vf_info
);
398 static void fm10k_iov_free_data(struct pci_dev
*pdev
)
400 struct fm10k_intfc
*interface
= pci_get_drvdata(pdev
);
402 if (!interface
->iov_data
)
405 /* reclaim hardware resources */
406 fm10k_iov_suspend(pdev
);
408 /* drop iov_data from interface */
409 kfree_rcu(interface
->iov_data
, rcu
);
410 interface
->iov_data
= NULL
;
413 static s32
fm10k_iov_alloc_data(struct pci_dev
*pdev
, int num_vfs
)
415 struct fm10k_intfc
*interface
= pci_get_drvdata(pdev
);
416 struct fm10k_iov_data
*iov_data
= interface
->iov_data
;
417 struct fm10k_hw
*hw
= &interface
->hw
;
421 /* return error if iov_data is already populated */
425 /* The PF should always be able to assign resources */
426 if (!hw
->iov
.ops
.assign_resources
)
429 /* nothing to do if no VFs are requested */
433 /* allocate memory for VF storage */
434 size
= offsetof(struct fm10k_iov_data
, vf_info
[num_vfs
]);
435 iov_data
= kzalloc(size
, GFP_KERNEL
);
439 /* record number of VFs */
440 iov_data
->num_vfs
= num_vfs
;
442 /* loop through vf_info structures initializing each entry */
443 for (i
= 0; i
< num_vfs
; i
++) {
444 struct fm10k_vf_info
*vf_info
= &iov_data
->vf_info
[i
];
446 /* Record VF VSI value */
447 vf_info
->vsi
= i
+ 1;
450 /* initialize mailbox memory */
451 err
= fm10k_pfvf_mbx_init(hw
, &vf_info
->mbx
, iov_mbx_data
, i
);
454 "Unable to initialize SR-IOV mailbox\n");
460 /* assign iov_data to interface */
461 interface
->iov_data
= iov_data
;
463 /* allocate hardware resources for the VFs */
464 fm10k_iov_resume(pdev
);
469 void fm10k_iov_disable(struct pci_dev
*pdev
)
471 if (pci_num_vf(pdev
) && pci_vfs_assigned(pdev
))
473 "Cannot disable SR-IOV while VFs are assigned\n");
475 pci_disable_sriov(pdev
);
477 fm10k_iov_free_data(pdev
);
480 static void fm10k_disable_aer_comp_abort(struct pci_dev
*pdev
)
485 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_ERR
);
489 pci_read_config_dword(pdev
, pos
+ PCI_ERR_UNCOR_SEVER
, &err_sev
);
490 err_sev
&= ~PCI_ERR_UNC_COMP_ABORT
;
491 pci_write_config_dword(pdev
, pos
+ PCI_ERR_UNCOR_SEVER
, err_sev
);
494 int fm10k_iov_configure(struct pci_dev
*pdev
, int num_vfs
)
496 int current_vfs
= pci_num_vf(pdev
);
499 if (current_vfs
&& pci_vfs_assigned(pdev
)) {
501 "Cannot modify SR-IOV while VFs are assigned\n");
502 num_vfs
= current_vfs
;
504 pci_disable_sriov(pdev
);
505 fm10k_iov_free_data(pdev
);
508 /* allocate resources for the VFs */
509 err
= fm10k_iov_alloc_data(pdev
, num_vfs
);
513 /* allocate VFs if not already allocated */
514 if (num_vfs
&& num_vfs
!= current_vfs
) {
515 /* Disable completer abort error reporting as
516 * the VFs can trigger this any time they read a queue
517 * that they don't own.
519 fm10k_disable_aer_comp_abort(pdev
);
521 err
= pci_enable_sriov(pdev
, num_vfs
);
524 "Enable PCI SR-IOV failed: %d\n", err
);
532 static inline void fm10k_reset_vf_info(struct fm10k_intfc
*interface
,
533 struct fm10k_vf_info
*vf_info
)
535 struct fm10k_hw
*hw
= &interface
->hw
;
537 /* assigning the MAC address will send a mailbox message */
538 fm10k_mbx_lock(interface
);
540 /* disable LPORT for this VF which clears switch rules */
541 hw
->iov
.ops
.reset_lport(hw
, vf_info
);
543 fm10k_clear_macvlan_queue(interface
, vf_info
->glort
, false);
545 /* assign new MAC+VLAN for this VF */
546 hw
->iov
.ops
.assign_default_mac_vlan(hw
, vf_info
);
548 /* re-enable the LPORT for this VF */
549 hw
->iov
.ops
.set_lport(hw
, vf_info
, vf_info
->vf_idx
,
550 FM10K_VF_FLAG_MULTI_CAPABLE
);
552 fm10k_mbx_unlock(interface
);
555 int fm10k_ndo_set_vf_mac(struct net_device
*netdev
, int vf_idx
, u8
*mac
)
557 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
558 struct fm10k_iov_data
*iov_data
= interface
->iov_data
;
559 struct fm10k_vf_info
*vf_info
;
561 /* verify SR-IOV is active and that vf idx is valid */
562 if (!iov_data
|| vf_idx
>= iov_data
->num_vfs
)
565 /* verify MAC addr is valid */
566 if (!is_zero_ether_addr(mac
) && !is_valid_ether_addr(mac
))
569 /* record new MAC address */
570 vf_info
= &iov_data
->vf_info
[vf_idx
];
571 ether_addr_copy(vf_info
->mac
, mac
);
573 fm10k_reset_vf_info(interface
, vf_info
);
578 int fm10k_ndo_set_vf_vlan(struct net_device
*netdev
, int vf_idx
, u16 vid
,
579 u8 qos
, __be16 vlan_proto
)
581 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
582 struct fm10k_iov_data
*iov_data
= interface
->iov_data
;
583 struct fm10k_hw
*hw
= &interface
->hw
;
584 struct fm10k_vf_info
*vf_info
;
586 /* verify SR-IOV is active and that vf idx is valid */
587 if (!iov_data
|| vf_idx
>= iov_data
->num_vfs
)
590 /* QOS is unsupported and VLAN IDs accepted range 0-4094 */
591 if (qos
|| (vid
> (VLAN_VID_MASK
- 1)))
594 /* VF VLAN Protocol part to default is unsupported */
595 if (vlan_proto
!= htons(ETH_P_8021Q
))
596 return -EPROTONOSUPPORT
;
598 vf_info
= &iov_data
->vf_info
[vf_idx
];
600 /* exit if there is nothing to do */
601 if (vf_info
->pf_vid
== vid
)
604 /* record default VLAN ID for VF */
605 vf_info
->pf_vid
= vid
;
607 /* Clear the VLAN table for the VF */
608 hw
->mac
.ops
.update_vlan(hw
, FM10K_VLAN_ALL
, vf_info
->vsi
, false);
610 fm10k_reset_vf_info(interface
, vf_info
);
615 int fm10k_ndo_set_vf_bw(struct net_device
*netdev
, int vf_idx
,
616 int __always_unused min_rate
, int max_rate
)
618 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
619 struct fm10k_iov_data
*iov_data
= interface
->iov_data
;
620 struct fm10k_hw
*hw
= &interface
->hw
;
622 /* verify SR-IOV is active and that vf idx is valid */
623 if (!iov_data
|| vf_idx
>= iov_data
->num_vfs
)
626 /* rate limit cannot be less than 10Mbs or greater than link speed */
628 (max_rate
< FM10K_VF_TC_MIN
|| max_rate
> FM10K_VF_TC_MAX
))
632 iov_data
->vf_info
[vf_idx
].rate
= max_rate
;
634 /* update hardware configuration */
635 hw
->iov
.ops
.configure_tc(hw
, vf_idx
, max_rate
);
640 int fm10k_ndo_get_vf_config(struct net_device
*netdev
,
641 int vf_idx
, struct ifla_vf_info
*ivi
)
643 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
644 struct fm10k_iov_data
*iov_data
= interface
->iov_data
;
645 struct fm10k_vf_info
*vf_info
;
647 /* verify SR-IOV is active and that vf idx is valid */
648 if (!iov_data
|| vf_idx
>= iov_data
->num_vfs
)
651 vf_info
= &iov_data
->vf_info
[vf_idx
];
654 ivi
->max_tx_rate
= vf_info
->rate
;
655 ivi
->min_tx_rate
= 0;
656 ether_addr_copy(ivi
->mac
, vf_info
->mac
);
657 ivi
->vlan
= vf_info
->pf_vid
;