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/module.h> 54 #include <linux/if_vlan.h> 55 #include <linux/crc32.h> 56 57 #include "opa_vnic_internal.h" 58 59 #define OPA_TX_TIMEOUT_MS 1000 60 61 #define OPA_VNIC_SKB_HEADROOM \ 62 ALIGN((OPA_VNIC_HDR_LEN + OPA_VNIC_SKB_MDATA_LEN), 8) 63 64 /* This function is overloaded for opa_vnic specific implementation */ 65 static void opa_vnic_get_stats64(struct net_device *netdev, 66 struct rtnl_link_stats64 *stats) 67 { 68 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); 69 struct opa_vnic_stats vstats; 70 71 memset(&vstats, 0, sizeof(vstats)); 72 spin_lock(&adapter->stats_lock); 73 adapter->rn_ops->ndo_get_stats64(netdev, &vstats.netstats); 74 spin_unlock(&adapter->stats_lock); 75 memcpy(stats, &vstats.netstats, sizeof(*stats)); 76 } 77 78 /* opa_netdev_start_xmit - transmit function */ 79 static netdev_tx_t opa_netdev_start_xmit(struct sk_buff *skb, 80 struct net_device *netdev) 81 { 82 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); 83 84 v_dbg("xmit: queue %d skb len %d\n", skb->queue_mapping, skb->len); 85 /* pad to ensure mininum ethernet packet length */ 86 if (unlikely(skb->len < ETH_ZLEN)) { 87 if (skb_padto(skb, ETH_ZLEN)) 88 return NETDEV_TX_OK; 89 90 skb_put(skb, ETH_ZLEN - skb->len); 91 } 92 93 opa_vnic_encap_skb(adapter, skb); 94 return adapter->rn_ops->ndo_start_xmit(skb, netdev); 95 } 96 97 static u16 opa_vnic_select_queue(struct net_device *netdev, struct sk_buff *skb, 98 void *accel_priv, 99 select_queue_fallback_t fallback) 100 { 101 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); 102 struct opa_vnic_skb_mdata *mdata; 103 int rc; 104 105 /* pass entropy and vl as metadata in skb */ 106 mdata = skb_push(skb, sizeof(*mdata)); 107 mdata->entropy = opa_vnic_calc_entropy(adapter, skb); 108 mdata->vl = opa_vnic_get_vl(adapter, skb); 109 rc = adapter->rn_ops->ndo_select_queue(netdev, skb, 110 accel_priv, fallback); 111 skb_pull(skb, sizeof(*mdata)); 112 return rc; 113 } 114 115 /* opa_vnic_process_vema_config - process vema configuration updates */ 116 void opa_vnic_process_vema_config(struct opa_vnic_adapter *adapter) 117 { 118 struct __opa_veswport_info *info = &adapter->info; 119 struct rdma_netdev *rn = netdev_priv(adapter->netdev); 120 u8 port_num[OPA_VESW_MAX_NUM_DEF_PORT] = { 0 }; 121 struct net_device *netdev = adapter->netdev; 122 u8 i, port_count = 0; 123 u16 port_mask; 124 125 /* If the base_mac_addr is changed, update the interface mac address */ 126 if (memcmp(info->vport.base_mac_addr, adapter->vema_mac_addr, 127 ARRAY_SIZE(info->vport.base_mac_addr))) { 128 struct sockaddr saddr; 129 130 memcpy(saddr.sa_data, info->vport.base_mac_addr, 131 ARRAY_SIZE(info->vport.base_mac_addr)); 132 mutex_lock(&adapter->lock); 133 eth_mac_addr(netdev, &saddr); 134 memcpy(adapter->vema_mac_addr, 135 info->vport.base_mac_addr, ETH_ALEN); 136 mutex_unlock(&adapter->lock); 137 } 138 139 rn->set_id(netdev, info->vesw.vesw_id); 140 141 /* Handle MTU limit change */ 142 rtnl_lock(); 143 netdev->max_mtu = max_t(unsigned int, info->vesw.eth_mtu_non_vlan, 144 netdev->min_mtu); 145 if (netdev->mtu > netdev->max_mtu) 146 dev_set_mtu(netdev, netdev->max_mtu); 147 rtnl_unlock(); 148 149 /* Update flow to default port redirection table */ 150 port_mask = info->vesw.def_port_mask; 151 for (i = 0; i < OPA_VESW_MAX_NUM_DEF_PORT; i++) { 152 if (port_mask & 1) 153 port_num[port_count++] = i; 154 port_mask >>= 1; 155 } 156 157 /* 158 * Build the flow table. Flow table is required when destination LID 159 * is not available. Up to OPA_VNIC_FLOW_TBL_SIZE flows supported. 160 * Each flow need a default port number to get its dlid from the 161 * u_ucast_dlid array. 162 */ 163 for (i = 0; i < OPA_VNIC_FLOW_TBL_SIZE; i++) 164 adapter->flow_tbl[i] = port_count ? port_num[i % port_count] : 165 OPA_VNIC_INVALID_PORT; 166 167 /* Operational state can only be DROP_ALL or FORWARDING */ 168 if (info->vport.config_state == OPA_VNIC_STATE_FORWARDING) { 169 info->vport.oper_state = OPA_VNIC_STATE_FORWARDING; 170 netif_dormant_off(netdev); 171 } else { 172 info->vport.oper_state = OPA_VNIC_STATE_DROP_ALL; 173 netif_dormant_on(netdev); 174 } 175 } 176 177 /* 178 * Set the power on default values in adapter's vema interface structure. 179 */ 180 static inline void opa_vnic_set_pod_values(struct opa_vnic_adapter *adapter) 181 { 182 adapter->info.vport.max_mac_tbl_ent = OPA_VNIC_MAC_TBL_MAX_ENTRIES; 183 adapter->info.vport.max_smac_ent = OPA_VNIC_MAX_SMAC_LIMIT; 184 adapter->info.vport.config_state = OPA_VNIC_STATE_DROP_ALL; 185 adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN; 186 } 187 188 /* opa_vnic_set_mac_addr - change mac address */ 189 static int opa_vnic_set_mac_addr(struct net_device *netdev, void *addr) 190 { 191 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); 192 struct sockaddr *sa = addr; 193 int rc; 194 195 if (!memcmp(netdev->dev_addr, sa->sa_data, ETH_ALEN)) 196 return 0; 197 198 mutex_lock(&adapter->lock); 199 rc = eth_mac_addr(netdev, addr); 200 mutex_unlock(&adapter->lock); 201 if (rc) 202 return rc; 203 204 adapter->info.vport.uc_macs_gen_count++; 205 opa_vnic_vema_report_event(adapter, 206 OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE); 207 return 0; 208 } 209 210 /* 211 * opa_vnic_mac_send_event - post event on possible mac list exchange 212 * Send trap when digest from uc/mc mac list differs from previous run. 213 * Digest is evaluated similar to how cksum does. 214 */ 215 static void opa_vnic_mac_send_event(struct net_device *netdev, u8 event) 216 { 217 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); 218 struct netdev_hw_addr *ha; 219 struct netdev_hw_addr_list *hw_list; 220 u32 *ref_crc; 221 u32 l, crc = 0; 222 223 switch (event) { 224 case OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE: 225 hw_list = &netdev->uc; 226 adapter->info.vport.uc_macs_gen_count++; 227 ref_crc = &adapter->umac_hash; 228 break; 229 case OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE: 230 hw_list = &netdev->mc; 231 adapter->info.vport.mc_macs_gen_count++; 232 ref_crc = &adapter->mmac_hash; 233 break; 234 default: 235 return; 236 } 237 netdev_hw_addr_list_for_each(ha, hw_list) { 238 crc = crc32_le(crc, ha->addr, ETH_ALEN); 239 } 240 l = netdev_hw_addr_list_count(hw_list) * ETH_ALEN; 241 crc = ~crc32_le(crc, (void *)&l, sizeof(l)); 242 243 if (crc != *ref_crc) { 244 *ref_crc = crc; 245 opa_vnic_vema_report_event(adapter, event); 246 } 247 } 248 249 /* opa_vnic_set_rx_mode - handle uc/mc mac list change */ 250 static void opa_vnic_set_rx_mode(struct net_device *netdev) 251 { 252 opa_vnic_mac_send_event(netdev, 253 OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE); 254 255 opa_vnic_mac_send_event(netdev, 256 OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE); 257 } 258 259 /* opa_netdev_open - activate network interface */ 260 static int opa_netdev_open(struct net_device *netdev) 261 { 262 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); 263 int rc; 264 265 rc = adapter->rn_ops->ndo_open(adapter->netdev); 266 if (rc) { 267 v_dbg("open failed %d\n", rc); 268 return rc; 269 } 270 271 /* Update eth link status and send trap */ 272 adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_UP; 273 opa_vnic_vema_report_event(adapter, 274 OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE); 275 return 0; 276 } 277 278 /* opa_netdev_close - disable network interface */ 279 static int opa_netdev_close(struct net_device *netdev) 280 { 281 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev); 282 int rc; 283 284 rc = adapter->rn_ops->ndo_stop(adapter->netdev); 285 if (rc) { 286 v_dbg("close failed %d\n", rc); 287 return rc; 288 } 289 290 /* Update eth link status and send trap */ 291 adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN; 292 opa_vnic_vema_report_event(adapter, 293 OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE); 294 return 0; 295 } 296 297 /* netdev ops */ 298 static const struct net_device_ops opa_netdev_ops = { 299 .ndo_open = opa_netdev_open, 300 .ndo_stop = opa_netdev_close, 301 .ndo_start_xmit = opa_netdev_start_xmit, 302 .ndo_get_stats64 = opa_vnic_get_stats64, 303 .ndo_set_rx_mode = opa_vnic_set_rx_mode, 304 .ndo_select_queue = opa_vnic_select_queue, 305 .ndo_set_mac_address = opa_vnic_set_mac_addr, 306 }; 307 308 /* opa_vnic_add_netdev - create vnic netdev interface */ 309 struct opa_vnic_adapter *opa_vnic_add_netdev(struct ib_device *ibdev, 310 u8 port_num, u8 vport_num) 311 { 312 struct opa_vnic_adapter *adapter; 313 struct net_device *netdev; 314 struct rdma_netdev *rn; 315 int rc; 316 317 netdev = ibdev->alloc_rdma_netdev(ibdev, port_num, 318 RDMA_NETDEV_OPA_VNIC, 319 "veth%d", NET_NAME_UNKNOWN, 320 ether_setup); 321 if (!netdev) 322 return ERR_PTR(-ENOMEM); 323 else if (IS_ERR(netdev)) 324 return ERR_CAST(netdev); 325 326 rn = netdev_priv(netdev); 327 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL); 328 if (!adapter) { 329 rc = -ENOMEM; 330 goto adapter_err; 331 } 332 333 rn->clnt_priv = adapter; 334 rn->hca = ibdev; 335 rn->port_num = port_num; 336 adapter->netdev = netdev; 337 adapter->ibdev = ibdev; 338 adapter->port_num = port_num; 339 adapter->vport_num = vport_num; 340 adapter->rn_ops = netdev->netdev_ops; 341 342 netdev->netdev_ops = &opa_netdev_ops; 343 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 344 netdev->hard_header_len += OPA_VNIC_SKB_HEADROOM; 345 mutex_init(&adapter->lock); 346 mutex_init(&adapter->mactbl_lock); 347 spin_lock_init(&adapter->stats_lock); 348 349 SET_NETDEV_DEV(netdev, ibdev->dev.parent); 350 351 opa_vnic_set_ethtool_ops(netdev); 352 353 opa_vnic_set_pod_values(adapter); 354 355 rc = register_netdev(netdev); 356 if (rc) 357 goto netdev_err; 358 359 netif_carrier_off(netdev); 360 netif_dormant_on(netdev); 361 v_info("initialized\n"); 362 363 return adapter; 364 netdev_err: 365 mutex_destroy(&adapter->lock); 366 mutex_destroy(&adapter->mactbl_lock); 367 kfree(adapter); 368 adapter_err: 369 rn->free_rdma_netdev(netdev); 370 371 return ERR_PTR(rc); 372 } 373 374 /* opa_vnic_rem_netdev - remove vnic netdev interface */ 375 void opa_vnic_rem_netdev(struct opa_vnic_adapter *adapter) 376 { 377 struct net_device *netdev = adapter->netdev; 378 struct rdma_netdev *rn = netdev_priv(netdev); 379 380 v_info("removing\n"); 381 unregister_netdev(netdev); 382 opa_vnic_release_mac_tbl(adapter); 383 mutex_destroy(&adapter->lock); 384 mutex_destroy(&adapter->mactbl_lock); 385 kfree(adapter); 386 rn->free_rdma_netdev(netdev); 387 } 388