opa_vnic_netdev.c 12 KB

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