1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
4 #include <linux/lockdep.h>
5 #include <linux/netdevice.h>
7 #include "nfpcore/nfp_cpp.h"
8 #include "nfpcore/nfp_nsp.h"
14 struct nfp_port
*nfp_port_from_netdev(struct net_device
*netdev
)
16 if (nfp_netdev_is_nfp_net(netdev
)) {
17 struct nfp_net
*nn
= netdev_priv(netdev
);
22 if (nfp_netdev_is_nfp_repr(netdev
)) {
23 struct nfp_repr
*repr
= netdev_priv(netdev
);
28 WARN(1, "Unknown netdev type for nfp_port\n");
33 int nfp_port_get_port_parent_id(struct net_device
*netdev
,
34 struct netdev_phys_item_id
*ppid
)
36 struct nfp_port
*port
;
39 port
= nfp_port_from_netdev(netdev
);
43 ppid
->id_len
= nfp_cpp_serial(port
->app
->cpp
, &serial
);
44 memcpy(&ppid
->id
, serial
, ppid
->id_len
);
49 int nfp_port_setup_tc(struct net_device
*netdev
, enum tc_setup_type type
,
52 struct nfp_port
*port
;
54 port
= nfp_port_from_netdev(netdev
);
58 return nfp_app_setup_tc(port
->app
, netdev
, type
, type_data
);
61 int nfp_port_set_features(struct net_device
*netdev
, netdev_features_t features
)
63 struct nfp_port
*port
;
65 port
= nfp_port_from_netdev(netdev
);
69 if ((netdev
->features
& NETIF_F_HW_TC
) > (features
& NETIF_F_HW_TC
) &&
70 port
->tc_offload_cnt
) {
71 netdev_err(netdev
, "Cannot disable HW TC offload while offloads active\n");
79 nfp_port_from_id(struct nfp_pf
*pf
, enum nfp_port_type type
, unsigned int id
)
81 struct nfp_port
*port
;
83 lockdep_assert_held(&pf
->lock
);
85 if (type
!= NFP_PORT_PHYS_PORT
)
88 list_for_each_entry(port
, &pf
->ports
, port_list
)
89 if (port
->eth_id
== id
)
95 struct nfp_eth_table_port
*__nfp_port_get_eth_port(struct nfp_port
*port
)
99 if (port
->type
!= NFP_PORT_PHYS_PORT
)
102 return port
->eth_port
;
105 struct nfp_eth_table_port
*nfp_port_get_eth_port(struct nfp_port
*port
)
107 if (!__nfp_port_get_eth_port(port
))
110 if (test_bit(NFP_PORT_CHANGED
, &port
->flags
))
111 if (nfp_net_refresh_eth_port(port
))
114 return __nfp_port_get_eth_port(port
);
118 nfp_port_get_phys_port_name(struct net_device
*netdev
, char *name
, size_t len
)
120 struct nfp_eth_table_port
*eth_port
;
121 struct nfp_port
*port
;
124 port
= nfp_port_from_netdev(netdev
);
128 switch (port
->type
) {
129 case NFP_PORT_PHYS_PORT
:
130 eth_port
= __nfp_port_get_eth_port(port
);
134 if (!eth_port
->is_split
)
135 n
= snprintf(name
, len
, "p%d", eth_port
->label_port
);
137 n
= snprintf(name
, len
, "p%ds%d", eth_port
->label_port
,
138 eth_port
->label_subport
);
140 case NFP_PORT_PF_PORT
:
142 n
= snprintf(name
, len
, "pf%d", port
->pf_id
);
144 n
= snprintf(name
, len
, "pf%ds%d", port
->pf_id
,
147 case NFP_PORT_VF_PORT
:
148 n
= snprintf(name
, len
, "pf%dvf%d", port
->pf_id
, port
->vf_id
);
161 * nfp_port_configure() - helper to set the interface configured bit
162 * @netdev: net_device instance
163 * @configed: Desired state
165 * Helper to set the ifup/ifdown state on the PHY only if there is a physical
166 * interface associated with the netdev.
169 * 0 - configuration successful (or no change);
170 * -ERRNO - configuration failed.
172 int nfp_port_configure(struct net_device
*netdev
, bool configed
)
174 struct nfp_eth_table_port
*eth_port
;
175 struct nfp_port
*port
;
178 port
= nfp_port_from_netdev(netdev
);
179 eth_port
= __nfp_port_get_eth_port(port
);
182 if (port
->eth_forced
)
185 err
= nfp_eth_set_configured(port
->app
->cpp
, eth_port
->index
, configed
);
186 return err
< 0 && err
!= -EOPNOTSUPP
? err
: 0;
189 int nfp_port_init_phy_port(struct nfp_pf
*pf
, struct nfp_app
*app
,
190 struct nfp_port
*port
, unsigned int id
)
192 /* Check if vNIC has external port associated and cfg is OK */
193 if (!pf
->eth_tbl
|| id
>= pf
->eth_tbl
->count
) {
195 "NSP port entries don't match vNICs (no entry %d)\n",
199 if (pf
->eth_tbl
->ports
[id
].override_changed
) {
201 "Config changed for port #%d, reboot required before port will be operational\n",
202 pf
->eth_tbl
->ports
[id
].index
);
203 port
->type
= NFP_PORT_INVALID
;
207 port
->eth_port
= &pf
->eth_tbl
->ports
[id
];
208 port
->eth_id
= pf
->eth_tbl
->ports
[id
].index
;
209 if (pf
->mac_stats_mem
)
211 pf
->mac_stats_mem
+ port
->eth_id
* NFP_MAC_STATS_SIZE
;
217 nfp_port_alloc(struct nfp_app
*app
, enum nfp_port_type type
,
218 struct net_device
*netdev
)
220 struct nfp_port
*port
;
222 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
224 return ERR_PTR(-ENOMEM
);
226 port
->netdev
= netdev
;
230 list_add_tail(&port
->port_list
, &app
->pf
->ports
);
235 void nfp_port_free(struct nfp_port
*port
)
239 list_del(&port
->port_list
);