intf.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/slab.h>
  34. #include <linux/export.h>
  35. #include <linux/errno.h>
  36. #include <net/devlink.h>
  37. #include "mlx4.h"
  38. static DEFINE_MUTEX(intf_mutex);
  39. static DEFINE_IDA(mlx4_adev_ida);
  40. static bool is_eth_supported(struct mlx4_dev *dev)
  41. {
  42. for (int port = 1; port <= dev->caps.num_ports; port++)
  43. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  44. return true;
  45. return false;
  46. }
  47. static bool is_ib_supported(struct mlx4_dev *dev)
  48. {
  49. for (int port = 1; port <= dev->caps.num_ports; port++)
  50. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_IB)
  51. return true;
  52. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE)
  53. return true;
  54. return false;
  55. }
  56. static const struct mlx4_adev_device {
  57. const char *suffix;
  58. bool (*is_supported)(struct mlx4_dev *dev);
  59. } mlx4_adev_devices[] = {
  60. { "eth", is_eth_supported },
  61. { "ib", is_ib_supported },
  62. };
  63. int mlx4_adev_init(struct mlx4_dev *dev)
  64. {
  65. struct mlx4_priv *priv = mlx4_priv(dev);
  66. priv->adev_idx = ida_alloc(&mlx4_adev_ida, GFP_KERNEL);
  67. if (priv->adev_idx < 0)
  68. return priv->adev_idx;
  69. priv->adev = kcalloc(ARRAY_SIZE(mlx4_adev_devices),
  70. sizeof(struct mlx4_adev *), GFP_KERNEL);
  71. if (!priv->adev) {
  72. ida_free(&mlx4_adev_ida, priv->adev_idx);
  73. return -ENOMEM;
  74. }
  75. return 0;
  76. }
  77. void mlx4_adev_cleanup(struct mlx4_dev *dev)
  78. {
  79. struct mlx4_priv *priv = mlx4_priv(dev);
  80. kfree(priv->adev);
  81. ida_free(&mlx4_adev_ida, priv->adev_idx);
  82. }
  83. static void adev_release(struct device *dev)
  84. {
  85. struct mlx4_adev *mlx4_adev =
  86. container_of(dev, struct mlx4_adev, adev.dev);
  87. struct mlx4_priv *priv = mlx4_priv(mlx4_adev->mdev);
  88. int idx = mlx4_adev->idx;
  89. kfree(mlx4_adev);
  90. priv->adev[idx] = NULL;
  91. }
  92. static struct mlx4_adev *add_adev(struct mlx4_dev *dev, int idx)
  93. {
  94. struct mlx4_priv *priv = mlx4_priv(dev);
  95. const char *suffix = mlx4_adev_devices[idx].suffix;
  96. struct auxiliary_device *adev;
  97. struct mlx4_adev *madev;
  98. int ret;
  99. madev = kzalloc(sizeof(*madev), GFP_KERNEL);
  100. if (!madev)
  101. return ERR_PTR(-ENOMEM);
  102. adev = &madev->adev;
  103. adev->id = priv->adev_idx;
  104. adev->name = suffix;
  105. adev->dev.parent = &dev->persist->pdev->dev;
  106. adev->dev.release = adev_release;
  107. madev->mdev = dev;
  108. madev->idx = idx;
  109. ret = auxiliary_device_init(adev);
  110. if (ret) {
  111. kfree(madev);
  112. return ERR_PTR(ret);
  113. }
  114. ret = auxiliary_device_add(adev);
  115. if (ret) {
  116. auxiliary_device_uninit(adev);
  117. return ERR_PTR(ret);
  118. }
  119. return madev;
  120. }
  121. static void del_adev(struct auxiliary_device *adev)
  122. {
  123. auxiliary_device_delete(adev);
  124. auxiliary_device_uninit(adev);
  125. }
  126. int mlx4_register_auxiliary_driver(struct mlx4_adrv *madrv)
  127. {
  128. return auxiliary_driver_register(&madrv->adrv);
  129. }
  130. EXPORT_SYMBOL_GPL(mlx4_register_auxiliary_driver);
  131. void mlx4_unregister_auxiliary_driver(struct mlx4_adrv *madrv)
  132. {
  133. auxiliary_driver_unregister(&madrv->adrv);
  134. }
  135. EXPORT_SYMBOL_GPL(mlx4_unregister_auxiliary_driver);
  136. int mlx4_do_bond(struct mlx4_dev *dev, bool enable)
  137. {
  138. struct mlx4_priv *priv = mlx4_priv(dev);
  139. int i, ret;
  140. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PORT_REMAP))
  141. return -EOPNOTSUPP;
  142. ret = mlx4_disable_rx_port_check(dev, enable);
  143. if (ret) {
  144. mlx4_err(dev, "Fail to %s rx port check\n",
  145. enable ? "enable" : "disable");
  146. return ret;
  147. }
  148. if (enable) {
  149. dev->flags |= MLX4_FLAG_BONDED;
  150. } else {
  151. ret = mlx4_virt2phy_port_map(dev, 1, 2);
  152. if (ret) {
  153. mlx4_err(dev, "Fail to reset port map\n");
  154. return ret;
  155. }
  156. dev->flags &= ~MLX4_FLAG_BONDED;
  157. }
  158. mutex_lock(&intf_mutex);
  159. for (i = 0; i < ARRAY_SIZE(mlx4_adev_devices); i++) {
  160. struct mlx4_adev *madev = priv->adev[i];
  161. struct mlx4_adrv *madrv;
  162. enum mlx4_protocol protocol;
  163. if (!madev)
  164. continue;
  165. device_lock(&madev->adev.dev);
  166. if (!madev->adev.dev.driver) {
  167. device_unlock(&madev->adev.dev);
  168. continue;
  169. }
  170. madrv = container_of(madev->adev.dev.driver, struct mlx4_adrv,
  171. adrv.driver);
  172. if (!(madrv->flags & MLX4_INTFF_BONDING)) {
  173. device_unlock(&madev->adev.dev);
  174. continue;
  175. }
  176. if (mlx4_is_mfunc(dev)) {
  177. mlx4_dbg(dev,
  178. "SRIOV, disabled HA mode for intf proto %d\n",
  179. madrv->protocol);
  180. device_unlock(&madev->adev.dev);
  181. continue;
  182. }
  183. protocol = madrv->protocol;
  184. device_unlock(&madev->adev.dev);
  185. del_adev(&madev->adev);
  186. priv->adev[i] = add_adev(dev, i);
  187. if (IS_ERR(priv->adev[i])) {
  188. mlx4_warn(dev, "Device[%d] (%s) failed to load\n", i,
  189. mlx4_adev_devices[i].suffix);
  190. priv->adev[i] = NULL;
  191. continue;
  192. }
  193. mlx4_dbg(dev,
  194. "Interface for protocol %d restarted with bonded mode %s\n",
  195. protocol, enable ? "enabled" : "disabled");
  196. }
  197. mutex_unlock(&intf_mutex);
  198. return 0;
  199. }
  200. void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type,
  201. void *param)
  202. {
  203. struct mlx4_priv *priv = mlx4_priv(dev);
  204. atomic_notifier_call_chain(&priv->event_nh, type, param);
  205. }
  206. int mlx4_register_event_notifier(struct mlx4_dev *dev,
  207. struct notifier_block *nb)
  208. {
  209. struct mlx4_priv *priv = mlx4_priv(dev);
  210. return atomic_notifier_chain_register(&priv->event_nh, nb);
  211. }
  212. EXPORT_SYMBOL(mlx4_register_event_notifier);
  213. int mlx4_unregister_event_notifier(struct mlx4_dev *dev,
  214. struct notifier_block *nb)
  215. {
  216. struct mlx4_priv *priv = mlx4_priv(dev);
  217. return atomic_notifier_chain_unregister(&priv->event_nh, nb);
  218. }
  219. EXPORT_SYMBOL(mlx4_unregister_event_notifier);
  220. static int add_drivers(struct mlx4_dev *dev)
  221. {
  222. struct mlx4_priv *priv = mlx4_priv(dev);
  223. int i, ret = 0;
  224. for (i = 0; i < ARRAY_SIZE(mlx4_adev_devices); i++) {
  225. bool is_supported = false;
  226. if (priv->adev[i])
  227. continue;
  228. if (mlx4_adev_devices[i].is_supported)
  229. is_supported = mlx4_adev_devices[i].is_supported(dev);
  230. if (!is_supported)
  231. continue;
  232. priv->adev[i] = add_adev(dev, i);
  233. if (IS_ERR(priv->adev[i])) {
  234. mlx4_warn(dev, "Device[%d] (%s) failed to load\n", i,
  235. mlx4_adev_devices[i].suffix);
  236. /* We continue to rescan drivers and leave to the caller
  237. * to make decision if to release everything or
  238. * continue. */
  239. ret = PTR_ERR(priv->adev[i]);
  240. priv->adev[i] = NULL;
  241. }
  242. }
  243. return ret;
  244. }
  245. static void delete_drivers(struct mlx4_dev *dev)
  246. {
  247. struct mlx4_priv *priv = mlx4_priv(dev);
  248. bool delete_all;
  249. int i;
  250. delete_all = !(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP);
  251. for (i = ARRAY_SIZE(mlx4_adev_devices) - 1; i >= 0; i--) {
  252. bool is_supported = false;
  253. if (!priv->adev[i])
  254. continue;
  255. if (mlx4_adev_devices[i].is_supported && !delete_all)
  256. is_supported = mlx4_adev_devices[i].is_supported(dev);
  257. if (is_supported)
  258. continue;
  259. del_adev(&priv->adev[i]->adev);
  260. priv->adev[i] = NULL;
  261. }
  262. }
  263. /* This function is used after mlx4_dev is reconfigured.
  264. */
  265. static int rescan_drivers_locked(struct mlx4_dev *dev)
  266. {
  267. lockdep_assert_held(&intf_mutex);
  268. delete_drivers(dev);
  269. if (!(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP))
  270. return 0;
  271. return add_drivers(dev);
  272. }
  273. int mlx4_register_device(struct mlx4_dev *dev)
  274. {
  275. int ret;
  276. mutex_lock(&intf_mutex);
  277. dev->persist->interface_state |= MLX4_INTERFACE_STATE_UP;
  278. ret = rescan_drivers_locked(dev);
  279. mutex_unlock(&intf_mutex);
  280. if (ret) {
  281. mlx4_unregister_device(dev);
  282. return ret;
  283. }
  284. mlx4_start_catas_poll(dev);
  285. return ret;
  286. }
  287. void mlx4_unregister_device(struct mlx4_dev *dev)
  288. {
  289. if (!(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP))
  290. return;
  291. mlx4_stop_catas_poll(dev);
  292. if (dev->persist->interface_state & MLX4_INTERFACE_STATE_DELETION &&
  293. mlx4_is_slave(dev)) {
  294. /* In mlx4_remove_one on a VF */
  295. u32 slave_read =
  296. swab32(readl(&mlx4_priv(dev)->mfunc.comm->slave_read));
  297. if (mlx4_comm_internal_err(slave_read)) {
  298. mlx4_dbg(dev, "%s: comm channel is down, entering error state.\n",
  299. __func__);
  300. mlx4_enter_error_state(dev->persist);
  301. }
  302. }
  303. mutex_lock(&intf_mutex);
  304. dev->persist->interface_state &= ~MLX4_INTERFACE_STATE_UP;
  305. rescan_drivers_locked(dev);
  306. mutex_unlock(&intf_mutex);
  307. }
  308. struct devlink_port *mlx4_get_devlink_port(struct mlx4_dev *dev, int port)
  309. {
  310. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  311. return &info->devlink_port;
  312. }
  313. EXPORT_SYMBOL_GPL(mlx4_get_devlink_port);