1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2016-2017 Broadcom Limited
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/rtnetlink.h>
13 #include <linux/jhash.h>
14 #include <net/pkt_cls.h>
19 #include "bnxt_devlink.h"
22 #ifdef CONFIG_BNXT_SRIOV
24 #define CFA_HANDLE_INVALID 0xffff
25 #define VF_IDX_INVALID 0xffff
27 static int hwrm_cfa_vfr_alloc(struct bnxt
*bp
, u16 vf_idx
,
28 u16
*tx_cfa_action
, u16
*rx_cfa_code
)
30 struct hwrm_cfa_vfr_alloc_output
*resp
= bp
->hwrm_cmd_resp_addr
;
31 struct hwrm_cfa_vfr_alloc_input req
= { 0 };
34 bnxt_hwrm_cmd_hdr_init(bp
, &req
, HWRM_CFA_VFR_ALLOC
, -1, -1);
35 req
.vf_id
= cpu_to_le16(vf_idx
);
36 sprintf(req
.vfr_name
, "vfr%d", vf_idx
);
38 mutex_lock(&bp
->hwrm_cmd_lock
);
39 rc
= _hwrm_send_message(bp
, &req
, sizeof(req
), HWRM_CMD_TIMEOUT
);
41 *tx_cfa_action
= le16_to_cpu(resp
->tx_cfa_action
);
42 *rx_cfa_code
= le16_to_cpu(resp
->rx_cfa_code
);
43 netdev_dbg(bp
->dev
, "tx_cfa_action=0x%x, rx_cfa_code=0x%x",
44 *tx_cfa_action
, *rx_cfa_code
);
46 netdev_info(bp
->dev
, "%s error rc=%d", __func__
, rc
);
49 mutex_unlock(&bp
->hwrm_cmd_lock
);
53 static int hwrm_cfa_vfr_free(struct bnxt
*bp
, u16 vf_idx
)
55 struct hwrm_cfa_vfr_free_input req
= { 0 };
58 bnxt_hwrm_cmd_hdr_init(bp
, &req
, HWRM_CFA_VFR_FREE
, -1, -1);
59 sprintf(req
.vfr_name
, "vfr%d", vf_idx
);
61 rc
= hwrm_send_message(bp
, &req
, sizeof(req
), HWRM_CMD_TIMEOUT
);
63 netdev_info(bp
->dev
, "%s error rc=%d", __func__
, rc
);
67 static int bnxt_vf_rep_open(struct net_device
*dev
)
69 struct bnxt_vf_rep
*vf_rep
= netdev_priv(dev
);
70 struct bnxt
*bp
= vf_rep
->bp
;
72 /* Enable link and TX only if the parent PF is open. */
73 if (netif_running(bp
->dev
)) {
74 netif_carrier_on(dev
);
75 netif_tx_start_all_queues(dev
);
80 static int bnxt_vf_rep_close(struct net_device
*dev
)
82 netif_carrier_off(dev
);
83 netif_tx_disable(dev
);
88 static netdev_tx_t
bnxt_vf_rep_xmit(struct sk_buff
*skb
,
89 struct net_device
*dev
)
91 struct bnxt_vf_rep
*vf_rep
= netdev_priv(dev
);
92 int rc
, len
= skb
->len
;
95 dst_hold((struct dst_entry
*)vf_rep
->dst
);
96 skb_dst_set(skb
, (struct dst_entry
*)vf_rep
->dst
);
97 skb
->dev
= vf_rep
->dst
->u
.port_info
.lower_dev
;
99 rc
= dev_queue_xmit(skb
);
101 vf_rep
->tx_stats
.packets
++;
102 vf_rep
->tx_stats
.bytes
+= len
;
108 bnxt_vf_rep_get_stats64(struct net_device
*dev
,
109 struct rtnl_link_stats64
*stats
)
111 struct bnxt_vf_rep
*vf_rep
= netdev_priv(dev
);
113 stats
->rx_packets
= vf_rep
->rx_stats
.packets
;
114 stats
->rx_bytes
= vf_rep
->rx_stats
.bytes
;
115 stats
->tx_packets
= vf_rep
->tx_stats
.packets
;
116 stats
->tx_bytes
= vf_rep
->tx_stats
.bytes
;
119 static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type
,
123 struct bnxt_vf_rep
*vf_rep
= cb_priv
;
124 struct bnxt
*bp
= vf_rep
->bp
;
125 int vf_fid
= bp
->pf
.vf
[vf_rep
->vf_idx
].fw_fid
;
127 if (!bnxt_tc_flower_enabled(vf_rep
->bp
) ||
128 !tc_cls_can_offload_and_chain0(bp
->dev
, type_data
))
132 case TC_SETUP_CLSFLOWER
:
133 return bnxt_tc_setup_flower(bp
, vf_fid
, type_data
);
139 static int bnxt_vf_rep_setup_tc_block(struct net_device
*dev
,
140 struct tc_block_offload
*f
)
142 struct bnxt_vf_rep
*vf_rep
= netdev_priv(dev
);
144 if (f
->binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
)
147 switch (f
->command
) {
149 return tcf_block_cb_register(f
->block
,
150 bnxt_vf_rep_setup_tc_block_cb
,
152 case TC_BLOCK_UNBIND
:
153 tcf_block_cb_unregister(f
->block
,
154 bnxt_vf_rep_setup_tc_block_cb
, vf_rep
);
161 static int bnxt_vf_rep_setup_tc(struct net_device
*dev
, enum tc_setup_type type
,
166 return bnxt_vf_rep_setup_tc_block(dev
, type_data
);
172 struct net_device
*bnxt_get_vf_rep(struct bnxt
*bp
, u16 cfa_code
)
176 if (cfa_code
&& bp
->cfa_code_map
&& BNXT_PF(bp
)) {
177 vf_idx
= bp
->cfa_code_map
[cfa_code
];
178 if (vf_idx
!= VF_IDX_INVALID
)
179 return bp
->vf_reps
[vf_idx
]->dev
;
184 void bnxt_vf_rep_rx(struct bnxt
*bp
, struct sk_buff
*skb
)
186 struct bnxt_vf_rep
*vf_rep
= netdev_priv(skb
->dev
);
187 struct bnxt_vf_rep_stats
*rx_stats
;
189 rx_stats
= &vf_rep
->rx_stats
;
190 vf_rep
->rx_stats
.bytes
+= skb
->len
;
191 vf_rep
->rx_stats
.packets
++;
193 netif_receive_skb(skb
);
196 static int bnxt_vf_rep_get_phys_port_name(struct net_device
*dev
, char *buf
,
199 struct bnxt_vf_rep
*vf_rep
= netdev_priv(dev
);
200 struct pci_dev
*pf_pdev
= vf_rep
->bp
->pdev
;
203 rc
= snprintf(buf
, len
, "pf%dvf%d", PCI_FUNC(pf_pdev
->devfn
),
210 static void bnxt_vf_rep_get_drvinfo(struct net_device
*dev
,
211 struct ethtool_drvinfo
*info
)
213 strlcpy(info
->driver
, DRV_MODULE_NAME
, sizeof(info
->driver
));
214 strlcpy(info
->version
, DRV_MODULE_VERSION
, sizeof(info
->version
));
217 static int bnxt_vf_rep_port_attr_get(struct net_device
*dev
,
218 struct switchdev_attr
*attr
)
220 struct bnxt_vf_rep
*vf_rep
= netdev_priv(dev
);
222 /* as only PORT_PARENT_ID is supported currently use common code
223 * between PF and VF-rep for now.
225 return bnxt_port_attr_get(vf_rep
->bp
, attr
);
228 static const struct switchdev_ops bnxt_vf_rep_switchdev_ops
= {
229 .switchdev_port_attr_get
= bnxt_vf_rep_port_attr_get
232 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops
= {
233 .get_drvinfo
= bnxt_vf_rep_get_drvinfo
236 static const struct net_device_ops bnxt_vf_rep_netdev_ops
= {
237 .ndo_open
= bnxt_vf_rep_open
,
238 .ndo_stop
= bnxt_vf_rep_close
,
239 .ndo_start_xmit
= bnxt_vf_rep_xmit
,
240 .ndo_get_stats64
= bnxt_vf_rep_get_stats64
,
241 .ndo_setup_tc
= bnxt_vf_rep_setup_tc
,
242 .ndo_get_phys_port_name
= bnxt_vf_rep_get_phys_port_name
245 bool bnxt_dev_is_vf_rep(struct net_device
*dev
)
247 return dev
->netdev_ops
== &bnxt_vf_rep_netdev_ops
;
250 /* Called when the parent PF interface is closed:
251 * As the mode transition from SWITCHDEV to LEGACY
252 * happens under the rtnl_lock() this routine is safe
253 * under the rtnl_lock()
255 void bnxt_vf_reps_close(struct bnxt
*bp
)
257 struct bnxt_vf_rep
*vf_rep
;
260 if (bp
->eswitch_mode
!= DEVLINK_ESWITCH_MODE_SWITCHDEV
)
263 num_vfs
= pci_num_vf(bp
->pdev
);
264 for (i
= 0; i
< num_vfs
; i
++) {
265 vf_rep
= bp
->vf_reps
[i
];
266 if (netif_running(vf_rep
->dev
))
267 bnxt_vf_rep_close(vf_rep
->dev
);
271 /* Called when the parent PF interface is opened (re-opened):
272 * As the mode transition from SWITCHDEV to LEGACY
273 * happen under the rtnl_lock() this routine is safe
274 * under the rtnl_lock()
276 void bnxt_vf_reps_open(struct bnxt
*bp
)
280 if (bp
->eswitch_mode
!= DEVLINK_ESWITCH_MODE_SWITCHDEV
)
283 for (i
= 0; i
< pci_num_vf(bp
->pdev
); i
++)
284 bnxt_vf_rep_open(bp
->vf_reps
[i
]->dev
);
287 static void __bnxt_vf_reps_destroy(struct bnxt
*bp
)
289 u16 num_vfs
= pci_num_vf(bp
->pdev
);
290 struct bnxt_vf_rep
*vf_rep
;
293 for (i
= 0; i
< num_vfs
; i
++) {
294 vf_rep
= bp
->vf_reps
[i
];
296 dst_release((struct dst_entry
*)vf_rep
->dst
);
298 if (vf_rep
->tx_cfa_action
!= CFA_HANDLE_INVALID
)
299 hwrm_cfa_vfr_free(bp
, vf_rep
->vf_idx
);
302 /* if register_netdev failed, then netdev_ops
303 * would have been set to NULL
305 if (vf_rep
->dev
->netdev_ops
)
306 unregister_netdev(vf_rep
->dev
);
307 free_netdev(vf_rep
->dev
);
316 void bnxt_vf_reps_destroy(struct bnxt
*bp
)
320 if (bp
->eswitch_mode
!= DEVLINK_ESWITCH_MODE_SWITCHDEV
)
326 /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
327 * before proceeding with VF-rep cleanup.
330 if (netif_running(bp
->dev
)) {
331 bnxt_close_nic(bp
, false, false);
334 /* un-publish cfa_code_map so that RX path can't see it anymore */
335 kfree(bp
->cfa_code_map
);
336 bp
->cfa_code_map
= NULL
;
337 bp
->eswitch_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
340 bnxt_open_nic(bp
, false, false);
343 /* Need to call vf_reps_destroy() outside of rntl_lock
344 * as unregister_netdev takes rtnl_lock
346 __bnxt_vf_reps_destroy(bp
);
349 /* Use the OUI of the PF's perm addr and report the same mac addr
350 * for the same VF-rep each time
352 static void bnxt_vf_rep_eth_addr_gen(u8
*src_mac
, u16 vf_idx
, u8
*mac
)
356 ether_addr_copy(mac
, src_mac
);
358 addr
= jhash(src_mac
, ETH_ALEN
, 0) + vf_idx
;
359 mac
[3] = (u8
)(addr
& 0xFF);
360 mac
[4] = (u8
)((addr
>> 8) & 0xFF);
361 mac
[5] = (u8
)((addr
>> 16) & 0xFF);
364 static void bnxt_vf_rep_netdev_init(struct bnxt
*bp
, struct bnxt_vf_rep
*vf_rep
,
365 struct net_device
*dev
)
367 struct net_device
*pf_dev
= bp
->dev
;
369 dev
->netdev_ops
= &bnxt_vf_rep_netdev_ops
;
370 dev
->ethtool_ops
= &bnxt_vf_rep_ethtool_ops
;
371 SWITCHDEV_SET_OPS(dev
, &bnxt_vf_rep_switchdev_ops
);
372 /* Just inherit all the featues of the parent PF as the VF-R
373 * uses the RX/TX rings of the parent PF
375 dev
->hw_features
= pf_dev
->hw_features
;
376 dev
->gso_partial_features
= pf_dev
->gso_partial_features
;
377 dev
->vlan_features
= pf_dev
->vlan_features
;
378 dev
->hw_enc_features
= pf_dev
->hw_enc_features
;
379 dev
->features
|= pf_dev
->features
;
380 bnxt_vf_rep_eth_addr_gen(bp
->pf
.mac_addr
, vf_rep
->vf_idx
,
382 ether_addr_copy(dev
->dev_addr
, dev
->perm_addr
);
385 static int bnxt_pcie_dsn_get(struct bnxt
*bp
, u8 dsn
[])
387 struct pci_dev
*pdev
= bp
->pdev
;
388 int pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_DSN
);
392 netdev_info(bp
->dev
, "Unable do read adapter's DSN");
396 /* DSN (two dw) is at an offset of 4 from the cap pos */
398 pci_read_config_dword(pdev
, pos
, &dw
);
399 put_unaligned_le32(dw
, &dsn
[0]);
400 pci_read_config_dword(pdev
, pos
+ 4, &dw
);
401 put_unaligned_le32(dw
, &dsn
[4]);
405 static int bnxt_vf_reps_create(struct bnxt
*bp
)
407 u16
*cfa_code_map
= NULL
, num_vfs
= pci_num_vf(bp
->pdev
);
408 struct bnxt_vf_rep
*vf_rep
;
409 struct net_device
*dev
;
412 bp
->vf_reps
= kcalloc(num_vfs
, sizeof(vf_rep
), GFP_KERNEL
);
416 /* storage for cfa_code to vf-idx mapping */
417 cfa_code_map
= kmalloc(sizeof(*bp
->cfa_code_map
) * MAX_CFA_CODE
,
423 for (i
= 0; i
< MAX_CFA_CODE
; i
++)
424 cfa_code_map
[i
] = VF_IDX_INVALID
;
426 for (i
= 0; i
< num_vfs
; i
++) {
427 dev
= alloc_etherdev(sizeof(*vf_rep
));
433 vf_rep
= netdev_priv(dev
);
434 bp
->vf_reps
[i
] = vf_rep
;
438 vf_rep
->tx_cfa_action
= CFA_HANDLE_INVALID
;
440 /* get cfa handles from FW */
441 rc
= hwrm_cfa_vfr_alloc(bp
, vf_rep
->vf_idx
,
442 &vf_rep
->tx_cfa_action
,
443 &vf_rep
->rx_cfa_code
);
448 cfa_code_map
[vf_rep
->rx_cfa_code
] = vf_rep
->vf_idx
;
450 vf_rep
->dst
= metadata_dst_alloc(0, METADATA_HW_PORT_MUX
,
456 /* only cfa_action is needed to mux a packet while TXing */
457 vf_rep
->dst
->u
.port_info
.port_id
= vf_rep
->tx_cfa_action
;
458 vf_rep
->dst
->u
.port_info
.lower_dev
= bp
->dev
;
460 bnxt_vf_rep_netdev_init(bp
, vf_rep
, dev
);
461 rc
= register_netdev(dev
);
463 /* no need for unregister_netdev in cleanup */
464 dev
->netdev_ops
= NULL
;
469 /* Read the adapter's DSN to use as the eswitch switch_id */
470 rc
= bnxt_pcie_dsn_get(bp
, bp
->switch_id
);
474 /* publish cfa_code_map only after all VF-reps have been initialized */
475 bp
->cfa_code_map
= cfa_code_map
;
476 bp
->eswitch_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
477 netif_keep_dst(bp
->dev
);
481 netdev_info(bp
->dev
, "%s error=%d", __func__
, rc
);
483 __bnxt_vf_reps_destroy(bp
);
487 /* Devlink related routines */
488 int bnxt_dl_eswitch_mode_get(struct devlink
*devlink
, u16
*mode
)
490 struct bnxt
*bp
= bnxt_get_bp_from_dl(devlink
);
492 *mode
= bp
->eswitch_mode
;
496 int bnxt_dl_eswitch_mode_set(struct devlink
*devlink
, u16 mode
)
498 struct bnxt
*bp
= bnxt_get_bp_from_dl(devlink
);
501 mutex_lock(&bp
->sriov_lock
);
502 if (bp
->eswitch_mode
== mode
) {
503 netdev_info(bp
->dev
, "already in %s eswitch mode",
504 mode
== DEVLINK_ESWITCH_MODE_LEGACY
?
505 "legacy" : "switchdev");
511 case DEVLINK_ESWITCH_MODE_LEGACY
:
512 bnxt_vf_reps_destroy(bp
);
515 case DEVLINK_ESWITCH_MODE_SWITCHDEV
:
516 if (pci_num_vf(bp
->pdev
) == 0) {
518 "Enable VFs before setting switchdev mode");
522 rc
= bnxt_vf_reps_create(bp
);
530 mutex_unlock(&bp
->sriov_lock
);