| 1 |
/* |
| 2 |
* Copyright(c) 2017 Intel Corporation. |
| 3 |
* |
| 4 |
* This file is provided under a dual BSD/GPLv2 license. When using or |
| 5 |
* redistributing this file, you may do so under either license. |
| 6 |
* |
| 7 |
* GPL LICENSE SUMMARY |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of version 2 of the GNU General Public License as |
| 11 |
* published by the Free Software Foundation. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* BSD LICENSE |
| 19 |
* |
| 20 |
* Redistribution and use in source and binary forms, with or without |
| 21 |
* modification, are permitted provided that the following conditions |
| 22 |
* are met: |
| 23 |
* |
| 24 |
* - Redistributions of source code must retain the above copyright |
| 25 |
* notice, this list of conditions and the following disclaimer. |
| 26 |
* - Redistributions in binary form must reproduce the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer in |
| 28 |
* the documentation and/or other materials provided with the |
| 29 |
* distribution. |
| 30 |
* - Neither the name of Intel Corporation nor the names of its |
| 31 |
* contributors may be used to endorse or promote products derived |
| 32 |
* from this software without specific prior written permission. |
| 33 |
* |
| 34 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 35 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 36 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 37 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 38 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 39 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 40 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 41 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 42 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 43 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 44 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 |
* |
| 46 |
*/ |
| 47 |
|
| 48 |
/* |
| 49 |
* This file contains OPA Virtual Network Interface Controller (VNIC) driver |
| 50 |
* netdev functionality. |
| 51 |
*/ |
| 52 |
|
| 53 |
#include <linux/if_vlan.h> |
| 54 |
#include <linux/crc32.h> |
| 55 |
|
| 56 |
#include "opa_vnic_internal.h" |
| 57 |
|
| 58 |
#define OPA_TX_TIMEOUT_MS 1000 |
| 59 |
|
| 60 |
#define OPA_VNIC_SKB_HEADROOM \ |
| 61 |
ALIGN((OPA_VNIC_HDR_LEN + OPA_VNIC_SKB_MDATA_LEN), 8) |
| 62 |
|
| 63 |
/* This function is overloaded for opa_vnic specific implementation */ |
| 64 |
static void opa_vnic_get_stats64(struct net_device *netdev, |
| 65 |
struct rtnl_link_stats64 *stats) |
| 66 |
{ |
| 67 |
struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); |
| 68 |
struct opa_vnic_stats vstats; |
| 69 |
|
| 70 |
memset(&vstats, 0, sizeof(vstats)); |
| 71 |
spin_lock(&adapter->stats_lock); |
| 72 |
adapter->rn_ops->ndo_get_stats64(netdev, &vstats.netstats); |
| 73 |
spin_unlock(&adapter->stats_lock); |
| 74 |
memcpy(stats, &vstats.netstats, sizeof(*stats)); |
| 75 |
} |
| 76 |
|
| 77 |
/* opa_netdev_start_xmit - transmit function */ |
| 78 |
static netdev_tx_t opa_netdev_start_xmit(struct sk_buff *skb, |
| 79 |
struct net_device *netdev) |
| 80 |
{ |
| 81 |
struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); |
| 82 |
|
| 83 |
v_dbg("xmit: queue %d skb len %d\n", skb->queue_mapping, skb->len); |
| 84 |
/* pad to ensure mininum ethernet packet length */ |
| 85 |
if (unlikely(skb->len < ETH_ZLEN)) { |
| 86 |
if (skb_padto(skb, ETH_ZLEN)) |
| 87 |
return NETDEV_TX_OK; |
| 88 |
|
| 89 |
skb_put(skb, ETH_ZLEN - skb->len); |
| 90 |
} |
| 91 |
|
| 92 |
opa_vnic_encap_skb(adapter, skb); |
| 93 |
return adapter->rn_ops->ndo_start_xmit(skb, netdev); |
| 94 |
} |
| 95 |
|
| 96 |
static u16 opa_vnic_select_queue(struct net_device *netdev, struct sk_buff *skb, |
| 97 |
struct net_device *sb_dev) |
| 98 |
{ |
| 99 |
struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); |
| 100 |
struct opa_vnic_skb_mdata *mdata; |
| 101 |
int rc; |
| 102 |
|
| 103 |
/* pass entropy and vl as metadata in skb */ |
| 104 |
mdata = skb_push(skb, sizeof(*mdata)); |
| 105 |
mdata->entropy = opa_vnic_calc_entropy(skb); |
| 106 |
mdata->vl = opa_vnic_get_vl(adapter, skb); |
| 107 |
rc = adapter->rn_ops->ndo_select_queue(netdev, skb, sb_dev); |
| 108 |
skb_pull(skb, sizeof(*mdata)); |
| 109 |
return rc; |
| 110 |
} |
| 111 |
|
| 112 |
static void opa_vnic_update_state(struct opa_vnic_adapter *adapter, bool up) |
| 113 |
{ |
| 114 |
struct __opa_veswport_info *info = &adapter->info; |
| 115 |
|
| 116 |
mutex_lock(&adapter->lock); |
| 117 |
/* Operational state can only be DROP_ALL or FORWARDING */ |
| 118 |
if ((info->vport.config_state == OPA_VNIC_STATE_FORWARDING) && up) { |
| 119 |
info->vport.oper_state = OPA_VNIC_STATE_FORWARDING; |
| 120 |
info->vport.eth_link_status = OPA_VNIC_ETH_LINK_UP; |
| 121 |
} else { |
| 122 |
info->vport.oper_state = OPA_VNIC_STATE_DROP_ALL; |
| 123 |
info->vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN; |
| 124 |
} |
| 125 |
|
| 126 |
if (info->vport.config_state == OPA_VNIC_STATE_FORWARDING) |
| 127 |
netif_dormant_off(adapter->netdev); |
| 128 |
else |
| 129 |
netif_dormant_on(adapter->netdev); |
| 130 |
mutex_unlock(&adapter->lock); |
| 131 |
} |
| 132 |
|
| 133 |
/* opa_vnic_process_vema_config - process vema configuration updates */ |
| 134 |
void opa_vnic_process_vema_config(struct opa_vnic_adapter *adapter) |
| 135 |
{ |
| 136 |
struct __opa_veswport_info *info = &adapter->info; |
| 137 |
struct rdma_netdev *rn = netdev_priv(adapter->netdev); |
| 138 |
u8 port_num[OPA_VESW_MAX_NUM_DEF_PORT] = { 0 }; |
| 139 |
struct net_device *netdev = adapter->netdev; |
| 140 |
u8 i, port_count = 0; |
| 141 |
u16 port_mask; |
| 142 |
|
| 143 |
/* If the base_mac_addr is changed, update the interface mac address */ |
| 144 |
if (memcmp(info->vport.base_mac_addr, adapter->vema_mac_addr, |
| 145 |
ARRAY_SIZE(info->vport.base_mac_addr))) { |
| 146 |
struct sockaddr saddr; |
| 147 |
|
| 148 |
memcpy(saddr.sa_data, info->vport.base_mac_addr, |
| 149 |
ARRAY_SIZE(info->vport.base_mac_addr)); |
| 150 |
mutex_lock(&adapter->lock); |
| 151 |
eth_commit_mac_addr_change(netdev, &saddr); |
| 152 |
memcpy(adapter->vema_mac_addr, |
| 153 |
info->vport.base_mac_addr, ETH_ALEN); |
| 154 |
mutex_unlock(&adapter->lock); |
| 155 |
} |
| 156 |
|
| 157 |
rn->set_id(netdev, info->vesw.vesw_id); |
| 158 |
|
| 159 |
/* Handle MTU limit change */ |
| 160 |
rtnl_lock(); |
| 161 |
netdev->max_mtu = max_t(unsigned int, info->vesw.eth_mtu, |
| 162 |
netdev->min_mtu); |
| 163 |
if (netdev->mtu > netdev->max_mtu) |
| 164 |
dev_set_mtu(netdev, netdev->max_mtu); |
| 165 |
rtnl_unlock(); |
| 166 |
|
| 167 |
/* Update flow to default port redirection table */ |
| 168 |
port_mask = info->vesw.def_port_mask; |
| 169 |
for (i = 0; i < OPA_VESW_MAX_NUM_DEF_PORT; i++) { |
| 170 |
if (port_mask & 1) |
| 171 |
port_num[port_count++] = i; |
| 172 |
port_mask >>= 1; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* Build the flow table. Flow table is required when destination LID |
| 177 |
* is not available. Up to OPA_VNIC_FLOW_TBL_SIZE flows supported. |
| 178 |
* Each flow need a default port number to get its dlid from the |
| 179 |
* u_ucast_dlid array. |
| 180 |
*/ |
| 181 |
for (i = 0; i < OPA_VNIC_FLOW_TBL_SIZE; i++) |
| 182 |
adapter->flow_tbl[i] = port_count ? port_num[i % port_count] : |
| 183 |
OPA_VNIC_INVALID_PORT; |
| 184 |
|
| 185 |
/* update state */ |
| 186 |
opa_vnic_update_state(adapter, !!(netdev->flags & IFF_UP)); |
| 187 |
} |
| 188 |
|
| 189 |
/* |
| 190 |
* Set the power on default values in adapter's vema interface structure. |
| 191 |
*/ |
| 192 |
static inline void opa_vnic_set_pod_values(struct opa_vnic_adapter *adapter) |
| 193 |
{ |
| 194 |
adapter->info.vport.max_mac_tbl_ent = OPA_VNIC_MAC_TBL_MAX_ENTRIES; |
| 195 |
adapter->info.vport.max_smac_ent = OPA_VNIC_MAX_SMAC_LIMIT; |
| 196 |
adapter->info.vport.config_state = OPA_VNIC_STATE_DROP_ALL; |
| 197 |
adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN; |
| 198 |
adapter->info.vesw.eth_mtu = ETH_DATA_LEN; |
| 199 |
} |
| 200 |
|
| 201 |
/* opa_vnic_set_mac_addr - change mac address */ |
| 202 |
static int opa_vnic_set_mac_addr(struct net_device *netdev, void *addr) |
| 203 |
{ |
| 204 |
struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); |
| 205 |
struct sockaddr *sa = addr; |
| 206 |
int rc; |
| 207 |
|
| 208 |
if (!memcmp(netdev->dev_addr, sa->sa_data, ETH_ALEN)) |
| 209 |
return 0; |
| 210 |
|
| 211 |
mutex_lock(&adapter->lock); |
| 212 |
rc = eth_mac_addr(netdev, addr); |
| 213 |
mutex_unlock(&adapter->lock); |
| 214 |
if (rc) |
| 215 |
return rc; |
| 216 |
|
| 217 |
adapter->info.vport.uc_macs_gen_count++; |
| 218 |
opa_vnic_vema_report_event(adapter, |
| 219 |
OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
/* |
| 224 |
* opa_vnic_mac_send_event - post event on possible mac list exchange |
| 225 |
* Send trap when digest from uc/mc mac list differs from previous run. |
| 226 |
* Digest is evaluated similar to how cksum does. |
| 227 |
*/ |
| 228 |
static void opa_vnic_mac_send_event(struct net_device *netdev, u8 event) |
| 229 |
{ |
| 230 |
struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); |
| 231 |
struct netdev_hw_addr *ha; |
| 232 |
struct netdev_hw_addr_list *hw_list; |
| 233 |
u32 *ref_crc; |
| 234 |
u32 l, crc = 0; |
| 235 |
|
| 236 |
switch (event) { |
| 237 |
case OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE: |
| 238 |
hw_list = &netdev->uc; |
| 239 |
adapter->info.vport.uc_macs_gen_count++; |
| 240 |
ref_crc = &adapter->umac_hash; |
| 241 |
break; |
| 242 |
case OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE: |
| 243 |
hw_list = &netdev->mc; |
| 244 |
adapter->info.vport.mc_macs_gen_count++; |
| 245 |
ref_crc = &adapter->mmac_hash; |
| 246 |
break; |
| 247 |
default: |
| 248 |
return; |
| 249 |
} |
| 250 |
netdev_hw_addr_list_for_each(ha, hw_list) { |
| 251 |
crc = crc32_le(crc, ha->addr, ETH_ALEN); |
| 252 |
} |
| 253 |
l = netdev_hw_addr_list_count(hw_list) * ETH_ALEN; |
| 254 |
crc = ~crc32_le(crc, (void *)&l, sizeof(l)); |
| 255 |
|
| 256 |
if (crc != *ref_crc) { |
| 257 |
*ref_crc = crc; |
| 258 |
opa_vnic_vema_report_event(adapter, event); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/* opa_vnic_set_rx_mode - handle uc/mc mac list change */ |
| 263 |
static void opa_vnic_set_rx_mode(struct net_device *netdev) |
| 264 |
{ |
| 265 |
opa_vnic_mac_send_event(netdev, |
| 266 |
OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE); |
| 267 |
|
| 268 |
opa_vnic_mac_send_event(netdev, |
| 269 |
OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE); |
| 270 |
} |
| 271 |
|
| 272 |
/* opa_netdev_open - activate network interface */ |
| 273 |
static int opa_netdev_open(struct net_device *netdev) |
| 274 |
{ |
| 275 |
struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); |
| 276 |
int rc; |
| 277 |
|
| 278 |
rc = adapter->rn_ops->ndo_open(adapter->netdev); |
| 279 |
if (rc) { |
| 280 |
v_dbg("open failed %d\n", rc); |
| 281 |
return rc; |
| 282 |
} |
| 283 |
|
| 284 |
/* Update status and send trap */ |
| 285 |
opa_vnic_update_state(adapter, true); |
| 286 |
opa_vnic_vema_report_event(adapter, |
| 287 |
OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE); |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
/* opa_netdev_close - disable network interface */ |
| 292 |
static int opa_netdev_close(struct net_device *netdev) |
| 293 |
{ |
| 294 |
struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); |
| 295 |
int rc; |
| 296 |
|
| 297 |
rc = adapter->rn_ops->ndo_stop(adapter->netdev); |
| 298 |
if (rc) { |
| 299 |
v_dbg("close failed %d\n", rc); |
| 300 |
return rc; |
| 301 |
} |
| 302 |
|
| 303 |
/* Update status and send trap */ |
| 304 |
opa_vnic_update_state(adapter, false); |
| 305 |
opa_vnic_vema_report_event(adapter, |
| 306 |
OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE); |
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
/* netdev ops */ |
| 311 |
static const struct net_device_ops opa_netdev_ops = { |
| 312 |
.ndo_open = opa_netdev_open, |
| 313 |
.ndo_stop = opa_netdev_close, |
| 314 |
.ndo_start_xmit = opa_netdev_start_xmit, |
| 315 |
.ndo_get_stats64 = opa_vnic_get_stats64, |
| 316 |
.ndo_set_rx_mode = opa_vnic_set_rx_mode, |
| 317 |
.ndo_select_queue = opa_vnic_select_queue, |
| 318 |
.ndo_set_mac_address = opa_vnic_set_mac_addr, |
| 319 |
}; |
| 320 |
|
| 321 |
/* opa_vnic_add_netdev - create vnic netdev interface */ |
| 322 |
struct opa_vnic_adapter *opa_vnic_add_netdev(struct ib_device *ibdev, |
| 323 |
u8 port_num, u8 vport_num) |
| 324 |
{ |
| 325 |
struct opa_vnic_adapter *adapter; |
| 326 |
struct net_device *netdev; |
| 327 |
struct rdma_netdev *rn; |
| 328 |
int rc; |
| 329 |
|
| 330 |
netdev = ibdev->ops.alloc_rdma_netdev(ibdev, port_num, |
| 331 |
RDMA_NETDEV_OPA_VNIC, |
| 332 |
"veth%d", NET_NAME_UNKNOWN, |
| 333 |
ether_setup); |
| 334 |
if (!netdev) |
| 335 |
return ERR_PTR(-ENOMEM); |
| 336 |
else if (IS_ERR(netdev)) |
| 337 |
return ERR_CAST(netdev); |
| 338 |
|
| 339 |
rn = netdev_priv(netdev); |
| 340 |
adapter = kzalloc_obj(*adapter); |
| 341 |
if (!adapter) { |
| 342 |
rc = -ENOMEM; |
| 343 |
goto adapter_err; |
| 344 |
} |
| 345 |
|
| 346 |
rn->clnt_priv = adapter; |
| 347 |
rn->hca = ibdev; |
| 348 |
rn->port_num = port_num; |
| 349 |
adapter->netdev = netdev; |
| 350 |
adapter->ibdev = ibdev; |
| 351 |
adapter->port_num = port_num; |
| 352 |
adapter->vport_num = vport_num; |
| 353 |
adapter->rn_ops = netdev->netdev_ops; |
| 354 |
|
| 355 |
netdev->netdev_ops = &opa_netdev_ops; |
| 356 |
netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
| 357 |
netdev->hard_header_len += OPA_VNIC_SKB_HEADROOM; |
| 358 |
mutex_init(&adapter->lock); |
| 359 |
mutex_init(&adapter->mactbl_lock); |
| 360 |
spin_lock_init(&adapter->stats_lock); |
| 361 |
|
| 362 |
SET_NETDEV_DEV(netdev, ibdev->dev.parent); |
| 363 |
|
| 364 |
opa_vnic_set_ethtool_ops(netdev); |
| 365 |
|
| 366 |
opa_vnic_set_pod_values(adapter); |
| 367 |
|
| 368 |
rc = register_netdev(netdev); |
| 369 |
if (rc) |
| 370 |
goto netdev_err; |
| 371 |
|
| 372 |
netif_carrier_off(netdev); |
| 373 |
netif_dormant_on(netdev); |
| 374 |
v_info("initialized\n"); |
| 375 |
|
| 376 |
return adapter; |
| 377 |
netdev_err: |
| 378 |
mutex_destroy(&adapter->lock); |
| 379 |
mutex_destroy(&adapter->mactbl_lock); |
| 380 |
kfree(adapter); |
| 381 |
adapter_err: |
| 382 |
rn->free_rdma_netdev(netdev); |
| 383 |
|
| 384 |
return ERR_PTR(rc); |
| 385 |
} |
| 386 |
|
| 387 |
/* opa_vnic_rem_netdev - remove vnic netdev interface */ |
| 388 |
void opa_vnic_rem_netdev(struct opa_vnic_adapter *adapter) |
| 389 |
{ |
| 390 |
struct net_device *netdev = adapter->netdev; |
| 391 |
struct rdma_netdev *rn = netdev_priv(netdev); |
| 392 |
|
| 393 |
v_info("removing\n"); |
| 394 |
unregister_netdev(netdev); |
| 395 |
opa_vnic_release_mac_tbl(adapter); |
| 396 |
mutex_destroy(&adapter->lock); |
| 397 |
mutex_destroy(&adapter->mactbl_lock); |
| 398 |
kfree(adapter); |
| 399 |
rn->free_rdma_netdev(netdev); |
| 400 |
} |