From: Valentine Fatiev <valentinef@nvidia.com>
Subject: [PATCH] BACKPORT: drivers/infiniband/hw/mlx5/main.c

Change-Id: Ie2350248fdf416846ea77cb7bf09eba52a8bb581
---
 drivers/infiniband/hw/mlx5/main.c | 79 ++++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 1 deletion(-)

--- a/drivers/infiniband/hw/mlx5/main.c
+++ b/drivers/infiniband/hw/mlx5/main.c
@@ -61,6 +61,9 @@ MODULE_ALIAS("auxiliary:mlx5_core.rdma")
 MODULE_ALIAS("auxiliary:mlx5_core.multiport");
 MODULE_ALIAS("auxiliary:mlx5_core.rdma-rep");
 
+#ifdef RETPOLINE_MLNX
+MODULE_INFO(retpoline, "Y");
+#endif
 struct mlx5_ib_event_work {
 	struct work_struct	work;
 	union {
@@ -2121,6 +2124,12 @@ static void mlx5_ib_dealloc_ucontext(str
 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
 	struct mlx5_bfreg_info *bfregi;
 
+#ifndef HAVE_MMU_NOTIFIER_OPS_HAS_FREE_NOTIFIER
+	mutex_lock(&ibcontext->per_mm_list_lock);
+	WARN_ON(!list_empty(&ibcontext->per_mm_list));
+	mutex_unlock(&ibcontext->per_mm_list_lock);
+#endif
+
 	bfregi = &context->bfregi;
 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
 
@@ -2175,9 +2184,11 @@ static int get_extended_index(unsigned l
 }
 
 
+#if defined(HAVE_PUT_TASK_STRUCT_EXPORTED) && defined (HAVE_GET_TASK_PID_EXPORTED) && defined(HAVE_GET_PID_TASK_EXPORTED)
 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
 {
 }
+#endif
 
 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
 {
@@ -2208,7 +2219,11 @@ static int mlx5_ib_mmap_clock_info_page(
 
 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
 		return -EPERM;
+#ifdef HAVE_VM_FLAGS_CLEAR
 	vm_flags_clear(vma, VM_MAYWRITE);
+#else
+	vma->vm_flags &= ~VM_MAYWRITE;
+#endif
 
 	if (!dev->mdev->clock_info)
 		return -EOPNOTSUPP;
@@ -2434,7 +2449,11 @@ static int mlx5_ib_mmap(struct ib_uconte
 
 		if (vma->vm_flags & VM_WRITE)
 			return -EPERM;
+#ifdef HAVE_VM_FLAGS_CLEAR
 		vm_flags_clear(vma, VM_MAYWRITE);
+#else
+		vma->vm_flags &= ~VM_MAYWRITE;
+#endif
 
 		/* Don't expose to user-space information it shouldn't have */
 		if (PAGE_SIZE > 4096)
@@ -3336,6 +3355,7 @@ static void mlx5_eth_lag_cleanup(struct
 	}
 }
 
+#ifdef HAVE_REGISTER_NETDEVICE_NOTIFIER_DEV_NET
 static void mlx5_netdev_notifier_register(struct mlx5_roce *roce,
 					  struct net_device *netdev)
 {
@@ -3395,6 +3415,30 @@ static void mlx5_mdev_netdev_untrack(str
 	mlx5_netdev_notifier_unregister(roce);
 }
 
+#else /* !(HAVE_REGISTER_NETDEVICE_NOTIFIER_DEV_NET)*/
+static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num)
+{
+	int err;
+
+	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
+	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
+	if (err) {
+		dev->port[port_num].roce.nb.notifier_call = NULL;
+		return err;
+	}
+
+	return 0;
+}
+
+static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num)
+{
+	if (dev->port[port_num].roce.nb.notifier_call) {
+		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
+		dev->port[port_num].roce.nb.notifier_call = NULL;
+	}
+}
+#endif /* HAVE_REGISTER_NETDEVICE_NOTIFIER_DEV_NET */
+
 static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
 {
 	int err;
@@ -3506,7 +3550,11 @@ static void mlx5_ib_unbind_slave_port(st
 	if (mpi->mdev_events.notifier_call)
 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
 	mpi->mdev_events.notifier_call = NULL;
+#ifdef HAVE_REGISTER_NETDEVICE_NOTIFIER_DEV_NET
 	mlx5_mdev_netdev_untrack(ibdev, port_num);
+#else
+	mlx5_remove_netdev_notifier(ibdev, port_num);
+#endif
 	spin_lock(&port->mp.mpi_lock);
 
 	comps = mpi->mdev_refcnt;
@@ -3565,7 +3613,16 @@ static bool mlx5_ib_bind_slave_port(stru
 	if (err)
 		goto unbind;
 
+#ifdef HAVE_REGISTER_NETDEVICE_NOTIFIER_DEV_NET
 	mlx5_mdev_netdev_track(ibdev, port_num);
+#else
+	err = mlx5_add_netdev_notifier(ibdev, port_num);
+	if (err) {
+		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
+			    port_num + 1);
+		goto unbind;
+	}
+#endif
 
 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
@@ -4024,7 +4081,9 @@ static void mlx5_ib_stage_init_cleanup(s
 {
 	mlx5_ib_data_direct_cleanup(dev);
 	mlx5_ib_cleanup_multiport_master(dev);
+#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
 	WARN_ON(!xa_empty(&dev->odp_mkeys));
+#endif
 	mutex_destroy(&dev->cap_mask_mutex);
 	WARN_ON(!xa_empty(&dev->sig_mrs));
 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
@@ -4130,7 +4189,9 @@ static const struct ib_device_ops mlx5_i
 	.destroy_qp = mlx5_ib_destroy_qp,
 	.destroy_srq = mlx5_ib_destroy_srq,
 	.detach_mcast = mlx5_ib_mcg_detach,
+#if defined(HAVE_PUT_TASK_STRUCT_EXPORTED) && defined (HAVE_GET_TASK_PID_EXPORTED) && defined(HAVE_GET_PID_TASK_EXPORTED)
 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
+#endif
 	.drain_rq = mlx5_ib_drain_rq,
 	.drain_sq = mlx5_ib_drain_sq,
 	.device_group = &mlx5_attr_group,
@@ -4159,7 +4220,9 @@ static const struct ib_device_ops mlx5_i
 	.query_srq = mlx5_ib_query_srq,
 	.query_ucontext = mlx5_ib_query_ucontext,
 	.reg_user_mr = mlx5_ib_reg_user_mr,
+#ifdef HAVE_DMA_BUF_DYNAMIC_ATTACH_GET_4_PARAMS
 	.reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf,
+#endif
 	.req_notify_cq = mlx5_ib_arm_cq,
 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
 	.resize_cq = mlx5_ib_resize_cq,
@@ -4190,8 +4253,8 @@ static const struct ib_device_ops mlx5_i
 
 static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
 	.get_vf_config = mlx5_ib_get_vf_config,
-	.get_vf_guid = mlx5_ib_get_vf_guid,
 	.get_vf_stats = mlx5_ib_get_vf_stats,
+	.get_vf_guid = mlx5_ib_get_vf_guid,
 	.set_vf_guid = mlx5_ib_set_vf_guid,
 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
 };
@@ -4351,7 +4414,13 @@ static int mlx5_ib_roce_init(struct mlx5
 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
 
 		/* Register only for native ports */
+#ifdef HAVE_REGISTER_NETDEVICE_NOTIFIER_DEV_NET
 		mlx5_mdev_netdev_track(dev, port_num);
+#else
+		err = mlx5_add_netdev_notifier(dev, port_num);
+		if (err)
+			return err;
+#endif
 
 		err = mlx5_enable_eth(dev);
 		if (err)
@@ -4360,7 +4429,11 @@ static int mlx5_ib_roce_init(struct mlx5
 
 	return 0;
 cleanup:
+#ifdef HAVE_REGISTER_NETDEVICE_NOTIFIER_DEV_NET
 	mlx5_mdev_netdev_untrack(dev, port_num);
+#else
+	mlx5_remove_netdev_notifier(dev, port_num);
+#endif
 	return err;
 }
 
@@ -4378,7 +4451,11 @@ static void mlx5_ib_roce_cleanup(struct
 		mlx5_disable_eth(dev);
 
 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
+#ifdef HAVE_REGISTER_NETDEVICE_NOTIFIER_DEV_NET
 		mlx5_mdev_netdev_untrack(dev, port_num);
+#else
+		mlx5_remove_netdev_notifier(dev, port_num);
+#endif
 	}
 }
 
