opa_vnic_netdev.c 12 KB

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