opa_vnic_netdev.c 12 KB

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