lag.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/netdevice.h>
  33. #include <linux/mlx5/driver.h>
  34. #include <linux/mlx5/vport.h>
  35. #include "mlx5_core.h"
  36. enum {
  37. MLX5_LAG_FLAG_BONDED = 1 << 0,
  38. };
  39. struct lag_func {
  40. struct mlx5_core_dev *dev;
  41. struct net_device *netdev;
  42. };
  43. /* Used for collection of netdev event info. */
  44. struct lag_tracker {
  45. enum netdev_lag_tx_type tx_type;
  46. struct netdev_lag_lower_state_info netdev_state[MLX5_MAX_PORTS];
  47. bool is_bonded;
  48. };
  49. /* LAG data of a ConnectX card.
  50. * It serves both its phys functions.
  51. */
  52. struct mlx5_lag {
  53. u8 flags;
  54. u8 v2p_map[MLX5_MAX_PORTS];
  55. struct lag_func pf[MLX5_MAX_PORTS];
  56. struct lag_tracker tracker;
  57. struct delayed_work bond_work;
  58. struct notifier_block nb;
  59. };
  60. /* General purpose, use for short periods of time.
  61. * Beware of lock dependencies (preferably, no locks should be acquired
  62. * under it).
  63. */
  64. static DEFINE_MUTEX(lag_mutex);
  65. static int mlx5_cmd_create_lag(struct mlx5_core_dev *dev, u8 remap_port1,
  66. u8 remap_port2)
  67. {
  68. u32 in[MLX5_ST_SZ_DW(create_lag_in)] = {0};
  69. u32 out[MLX5_ST_SZ_DW(create_lag_out)] = {0};
  70. void *lag_ctx = MLX5_ADDR_OF(create_lag_in, in, ctx);
  71. MLX5_SET(create_lag_in, in, opcode, MLX5_CMD_OP_CREATE_LAG);
  72. MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
  73. MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
  74. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  75. }
  76. static int mlx5_cmd_modify_lag(struct mlx5_core_dev *dev, u8 remap_port1,
  77. u8 remap_port2)
  78. {
  79. u32 in[MLX5_ST_SZ_DW(modify_lag_in)] = {0};
  80. u32 out[MLX5_ST_SZ_DW(modify_lag_out)] = {0};
  81. void *lag_ctx = MLX5_ADDR_OF(modify_lag_in, in, ctx);
  82. MLX5_SET(modify_lag_in, in, opcode, MLX5_CMD_OP_MODIFY_LAG);
  83. MLX5_SET(modify_lag_in, in, field_select, 0x1);
  84. MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
  85. MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
  86. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  87. }
  88. static int mlx5_cmd_destroy_lag(struct mlx5_core_dev *dev)
  89. {
  90. u32 in[MLX5_ST_SZ_DW(destroy_lag_in)] = {0};
  91. u32 out[MLX5_ST_SZ_DW(destroy_lag_out)] = {0};
  92. MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
  93. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  94. }
  95. int mlx5_cmd_create_vport_lag(struct mlx5_core_dev *dev)
  96. {
  97. u32 in[MLX5_ST_SZ_DW(create_vport_lag_in)] = {0};
  98. u32 out[MLX5_ST_SZ_DW(create_vport_lag_out)] = {0};
  99. MLX5_SET(create_vport_lag_in, in, opcode, MLX5_CMD_OP_CREATE_VPORT_LAG);
  100. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  101. }
  102. EXPORT_SYMBOL(mlx5_cmd_create_vport_lag);
  103. int mlx5_cmd_destroy_vport_lag(struct mlx5_core_dev *dev)
  104. {
  105. u32 in[MLX5_ST_SZ_DW(destroy_vport_lag_in)] = {0};
  106. u32 out[MLX5_ST_SZ_DW(destroy_vport_lag_out)] = {0};
  107. MLX5_SET(destroy_vport_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_VPORT_LAG);
  108. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  109. }
  110. EXPORT_SYMBOL(mlx5_cmd_destroy_vport_lag);
  111. static struct mlx5_lag *mlx5_lag_dev_get(struct mlx5_core_dev *dev)
  112. {
  113. return dev->priv.lag;
  114. }
  115. static int mlx5_lag_dev_get_netdev_idx(struct mlx5_lag *ldev,
  116. struct net_device *ndev)
  117. {
  118. int i;
  119. for (i = 0; i < MLX5_MAX_PORTS; i++)
  120. if (ldev->pf[i].netdev == ndev)
  121. return i;
  122. return -1;
  123. }
  124. static bool mlx5_lag_is_bonded(struct mlx5_lag *ldev)
  125. {
  126. return !!(ldev->flags & MLX5_LAG_FLAG_BONDED);
  127. }
  128. static void mlx5_infer_tx_affinity_mapping(struct lag_tracker *tracker,
  129. u8 *port1, u8 *port2)
  130. {
  131. *port1 = 1;
  132. *port2 = 2;
  133. if (!tracker->netdev_state[0].tx_enabled ||
  134. !tracker->netdev_state[0].link_up) {
  135. *port1 = 2;
  136. return;
  137. }
  138. if (!tracker->netdev_state[1].tx_enabled ||
  139. !tracker->netdev_state[1].link_up)
  140. *port2 = 1;
  141. }
  142. static void mlx5_activate_lag(struct mlx5_lag *ldev,
  143. struct lag_tracker *tracker)
  144. {
  145. struct mlx5_core_dev *dev0 = ldev->pf[0].dev;
  146. int err;
  147. ldev->flags |= MLX5_LAG_FLAG_BONDED;
  148. mlx5_infer_tx_affinity_mapping(tracker, &ldev->v2p_map[0],
  149. &ldev->v2p_map[1]);
  150. err = mlx5_cmd_create_lag(dev0, ldev->v2p_map[0], ldev->v2p_map[1]);
  151. if (err)
  152. mlx5_core_err(dev0,
  153. "Failed to create LAG (%d)\n",
  154. err);
  155. }
  156. static void mlx5_deactivate_lag(struct mlx5_lag *ldev)
  157. {
  158. struct mlx5_core_dev *dev0 = ldev->pf[0].dev;
  159. int err;
  160. ldev->flags &= ~MLX5_LAG_FLAG_BONDED;
  161. err = mlx5_cmd_destroy_lag(dev0);
  162. if (err)
  163. mlx5_core_err(dev0,
  164. "Failed to destroy LAG (%d)\n",
  165. err);
  166. }
  167. static void mlx5_do_bond(struct mlx5_lag *ldev)
  168. {
  169. struct mlx5_core_dev *dev0 = ldev->pf[0].dev;
  170. struct mlx5_core_dev *dev1 = ldev->pf[1].dev;
  171. struct lag_tracker tracker;
  172. u8 v2p_port1, v2p_port2;
  173. int i, err;
  174. if (!dev0 || !dev1)
  175. return;
  176. mutex_lock(&lag_mutex);
  177. tracker = ldev->tracker;
  178. mutex_unlock(&lag_mutex);
  179. if (tracker.is_bonded && !mlx5_lag_is_bonded(ldev)) {
  180. if (mlx5_sriov_is_enabled(dev0) ||
  181. mlx5_sriov_is_enabled(dev1)) {
  182. mlx5_core_warn(dev0, "LAG is not supported with SRIOV");
  183. return;
  184. }
  185. for (i = 0; i < MLX5_MAX_PORTS; i++)
  186. mlx5_remove_dev_by_protocol(ldev->pf[i].dev,
  187. MLX5_INTERFACE_PROTOCOL_IB);
  188. mlx5_activate_lag(ldev, &tracker);
  189. mlx5_add_dev_by_protocol(dev0, MLX5_INTERFACE_PROTOCOL_IB);
  190. mlx5_nic_vport_enable_roce(dev1);
  191. } else if (tracker.is_bonded && mlx5_lag_is_bonded(ldev)) {
  192. mlx5_infer_tx_affinity_mapping(&tracker, &v2p_port1,
  193. &v2p_port2);
  194. if ((v2p_port1 != ldev->v2p_map[0]) ||
  195. (v2p_port2 != ldev->v2p_map[1])) {
  196. ldev->v2p_map[0] = v2p_port1;
  197. ldev->v2p_map[1] = v2p_port2;
  198. err = mlx5_cmd_modify_lag(dev0, v2p_port1, v2p_port2);
  199. if (err)
  200. mlx5_core_err(dev0,
  201. "Failed to modify LAG (%d)\n",
  202. err);
  203. }
  204. } else if (!tracker.is_bonded && mlx5_lag_is_bonded(ldev)) {
  205. mlx5_remove_dev_by_protocol(dev0, MLX5_INTERFACE_PROTOCOL_IB);
  206. mlx5_nic_vport_disable_roce(dev1);
  207. mlx5_deactivate_lag(ldev);
  208. for (i = 0; i < MLX5_MAX_PORTS; i++)
  209. if (ldev->pf[i].dev)
  210. mlx5_add_dev_by_protocol(ldev->pf[i].dev,
  211. MLX5_INTERFACE_PROTOCOL_IB);
  212. }
  213. }
  214. static void mlx5_queue_bond_work(struct mlx5_lag *ldev, unsigned long delay)
  215. {
  216. schedule_delayed_work(&ldev->bond_work, delay);
  217. }
  218. static void mlx5_do_bond_work(struct work_struct *work)
  219. {
  220. struct delayed_work *delayed_work = to_delayed_work(work);
  221. struct mlx5_lag *ldev = container_of(delayed_work, struct mlx5_lag,
  222. bond_work);
  223. int status;
  224. status = mlx5_dev_list_trylock();
  225. if (!status) {
  226. /* 1 sec delay. */
  227. mlx5_queue_bond_work(ldev, HZ);
  228. return;
  229. }
  230. mlx5_do_bond(ldev);
  231. mlx5_dev_list_unlock();
  232. }
  233. static int mlx5_handle_changeupper_event(struct mlx5_lag *ldev,
  234. struct lag_tracker *tracker,
  235. struct net_device *ndev,
  236. struct netdev_notifier_changeupper_info *info)
  237. {
  238. struct net_device *upper = info->upper_dev, *ndev_tmp;
  239. struct netdev_lag_upper_info *lag_upper_info = NULL;
  240. bool is_bonded;
  241. int bond_status = 0;
  242. int num_slaves = 0;
  243. int idx;
  244. if (!netif_is_lag_master(upper))
  245. return 0;
  246. if (info->linking)
  247. lag_upper_info = info->upper_info;
  248. /* The event may still be of interest if the slave does not belong to
  249. * us, but is enslaved to a master which has one or more of our netdevs
  250. * as slaves (e.g., if a new slave is added to a master that bonds two
  251. * of our netdevs, we should unbond).
  252. */
  253. rcu_read_lock();
  254. for_each_netdev_in_bond_rcu(upper, ndev_tmp) {
  255. idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev_tmp);
  256. if (idx > -1)
  257. bond_status |= (1 << idx);
  258. num_slaves++;
  259. }
  260. rcu_read_unlock();
  261. /* None of this lagdev's netdevs are slaves of this master. */
  262. if (!(bond_status & 0x3))
  263. return 0;
  264. if (lag_upper_info)
  265. tracker->tx_type = lag_upper_info->tx_type;
  266. /* Determine bonding status:
  267. * A device is considered bonded if both its physical ports are slaves
  268. * of the same lag master, and only them.
  269. * Lag mode must be activebackup or hash.
  270. */
  271. is_bonded = (num_slaves == MLX5_MAX_PORTS) &&
  272. (bond_status == 0x3) &&
  273. ((tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) ||
  274. (tracker->tx_type == NETDEV_LAG_TX_TYPE_HASH));
  275. if (tracker->is_bonded != is_bonded) {
  276. tracker->is_bonded = is_bonded;
  277. return 1;
  278. }
  279. return 0;
  280. }
  281. static int mlx5_handle_changelowerstate_event(struct mlx5_lag *ldev,
  282. struct lag_tracker *tracker,
  283. struct net_device *ndev,
  284. struct netdev_notifier_changelowerstate_info *info)
  285. {
  286. struct netdev_lag_lower_state_info *lag_lower_info;
  287. int idx;
  288. if (!netif_is_lag_port(ndev))
  289. return 0;
  290. idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev);
  291. if (idx == -1)
  292. return 0;
  293. /* This information is used to determine virtual to physical
  294. * port mapping.
  295. */
  296. lag_lower_info = info->lower_state_info;
  297. if (!lag_lower_info)
  298. return 0;
  299. tracker->netdev_state[idx] = *lag_lower_info;
  300. return 1;
  301. }
  302. static int mlx5_lag_netdev_event(struct notifier_block *this,
  303. unsigned long event, void *ptr)
  304. {
  305. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  306. struct lag_tracker tracker;
  307. struct mlx5_lag *ldev;
  308. int changed = 0;
  309. if (!net_eq(dev_net(ndev), &init_net))
  310. return NOTIFY_DONE;
  311. if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
  312. return NOTIFY_DONE;
  313. ldev = container_of(this, struct mlx5_lag, nb);
  314. tracker = ldev->tracker;
  315. switch (event) {
  316. case NETDEV_CHANGEUPPER:
  317. changed = mlx5_handle_changeupper_event(ldev, &tracker, ndev,
  318. ptr);
  319. break;
  320. case NETDEV_CHANGELOWERSTATE:
  321. changed = mlx5_handle_changelowerstate_event(ldev, &tracker,
  322. ndev, ptr);
  323. break;
  324. }
  325. mutex_lock(&lag_mutex);
  326. ldev->tracker = tracker;
  327. mutex_unlock(&lag_mutex);
  328. if (changed)
  329. mlx5_queue_bond_work(ldev, 0);
  330. return NOTIFY_DONE;
  331. }
  332. static struct mlx5_lag *mlx5_lag_dev_alloc(void)
  333. {
  334. struct mlx5_lag *ldev;
  335. ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
  336. if (!ldev)
  337. return NULL;
  338. INIT_DELAYED_WORK(&ldev->bond_work, mlx5_do_bond_work);
  339. return ldev;
  340. }
  341. static void mlx5_lag_dev_free(struct mlx5_lag *ldev)
  342. {
  343. kfree(ldev);
  344. }
  345. static void mlx5_lag_dev_add_pf(struct mlx5_lag *ldev,
  346. struct mlx5_core_dev *dev,
  347. struct net_device *netdev)
  348. {
  349. unsigned int fn = PCI_FUNC(dev->pdev->devfn);
  350. if (fn >= MLX5_MAX_PORTS)
  351. return;
  352. mutex_lock(&lag_mutex);
  353. ldev->pf[fn].dev = dev;
  354. ldev->pf[fn].netdev = netdev;
  355. ldev->tracker.netdev_state[fn].link_up = 0;
  356. ldev->tracker.netdev_state[fn].tx_enabled = 0;
  357. dev->priv.lag = ldev;
  358. mutex_unlock(&lag_mutex);
  359. }
  360. static void mlx5_lag_dev_remove_pf(struct mlx5_lag *ldev,
  361. struct mlx5_core_dev *dev)
  362. {
  363. int i;
  364. for (i = 0; i < MLX5_MAX_PORTS; i++)
  365. if (ldev->pf[i].dev == dev)
  366. break;
  367. if (i == MLX5_MAX_PORTS)
  368. return;
  369. mutex_lock(&lag_mutex);
  370. memset(&ldev->pf[i], 0, sizeof(*ldev->pf));
  371. dev->priv.lag = NULL;
  372. mutex_unlock(&lag_mutex);
  373. }
  374. /* Must be called with intf_mutex held */
  375. void mlx5_lag_add(struct mlx5_core_dev *dev, struct net_device *netdev)
  376. {
  377. struct mlx5_lag *ldev = NULL;
  378. struct mlx5_core_dev *tmp_dev;
  379. if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
  380. !MLX5_CAP_GEN(dev, lag_master) ||
  381. (MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_MAX_PORTS))
  382. return;
  383. tmp_dev = mlx5_get_next_phys_dev(dev);
  384. if (tmp_dev)
  385. ldev = tmp_dev->priv.lag;
  386. if (!ldev) {
  387. ldev = mlx5_lag_dev_alloc();
  388. if (!ldev) {
  389. mlx5_core_err(dev, "Failed to alloc lag dev\n");
  390. return;
  391. }
  392. }
  393. mlx5_lag_dev_add_pf(ldev, dev, netdev);
  394. if (!ldev->nb.notifier_call) {
  395. ldev->nb.notifier_call = mlx5_lag_netdev_event;
  396. if (register_netdevice_notifier(&ldev->nb)) {
  397. ldev->nb.notifier_call = NULL;
  398. mlx5_core_err(dev, "Failed to register LAG netdev notifier\n");
  399. }
  400. }
  401. }
  402. /* Must be called with intf_mutex held */
  403. void mlx5_lag_remove(struct mlx5_core_dev *dev)
  404. {
  405. struct mlx5_lag *ldev;
  406. int i;
  407. ldev = mlx5_lag_dev_get(dev);
  408. if (!ldev)
  409. return;
  410. if (mlx5_lag_is_bonded(ldev))
  411. mlx5_deactivate_lag(ldev);
  412. mlx5_lag_dev_remove_pf(ldev, dev);
  413. for (i = 0; i < MLX5_MAX_PORTS; i++)
  414. if (ldev->pf[i].dev)
  415. break;
  416. if (i == MLX5_MAX_PORTS) {
  417. if (ldev->nb.notifier_call)
  418. unregister_netdevice_notifier(&ldev->nb);
  419. cancel_delayed_work_sync(&ldev->bond_work);
  420. mlx5_lag_dev_free(ldev);
  421. }
  422. }
  423. bool mlx5_lag_is_active(struct mlx5_core_dev *dev)
  424. {
  425. struct mlx5_lag *ldev;
  426. bool res;
  427. mutex_lock(&lag_mutex);
  428. ldev = mlx5_lag_dev_get(dev);
  429. res = ldev && mlx5_lag_is_bonded(ldev);
  430. mutex_unlock(&lag_mutex);
  431. return res;
  432. }
  433. EXPORT_SYMBOL(mlx5_lag_is_active);
  434. struct net_device *mlx5_lag_get_roce_netdev(struct mlx5_core_dev *dev)
  435. {
  436. struct net_device *ndev = NULL;
  437. struct mlx5_lag *ldev;
  438. mutex_lock(&lag_mutex);
  439. ldev = mlx5_lag_dev_get(dev);
  440. if (!(ldev && mlx5_lag_is_bonded(ldev)))
  441. goto unlock;
  442. if (ldev->tracker.tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) {
  443. ndev = ldev->tracker.netdev_state[0].tx_enabled ?
  444. ldev->pf[0].netdev : ldev->pf[1].netdev;
  445. } else {
  446. ndev = ldev->pf[0].netdev;
  447. }
  448. if (ndev)
  449. dev_hold(ndev);
  450. unlock:
  451. mutex_unlock(&lag_mutex);
  452. return ndev;
  453. }
  454. EXPORT_SYMBOL(mlx5_lag_get_roce_netdev);
  455. bool mlx5_lag_intf_add(struct mlx5_interface *intf, struct mlx5_priv *priv)
  456. {
  457. struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev,
  458. priv);
  459. struct mlx5_lag *ldev;
  460. if (intf->protocol != MLX5_INTERFACE_PROTOCOL_IB)
  461. return true;
  462. ldev = mlx5_lag_dev_get(dev);
  463. if (!ldev || !mlx5_lag_is_bonded(ldev) || ldev->pf[0].dev == dev)
  464. return true;
  465. /* If bonded, we do not add an IB device for PF1. */
  466. return false;
  467. }