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
33 #include "en.h"
34 #include "lib/crypto.h"
35
36 /* mlx5e global resources should be placed in this file.
37 * Global resources are common to all the netdevices created on the same nic.
38 */
39
mlx5e_mkey_set_relaxed_ordering(struct mlx5_core_dev * mdev,void * mkc)40 void mlx5e_mkey_set_relaxed_ordering(struct mlx5_core_dev *mdev, void *mkc)
41 {
42 bool ro_pci_enable = pcie_relaxed_ordering_enabled(mdev->pdev);
43 bool ro_write = MLX5_CAP_GEN(mdev, relaxed_ordering_write);
44 bool ro_read = MLX5_CAP_GEN(mdev, relaxed_ordering_read);
45
46 MLX5_SET(mkc, mkc, relaxed_ordering_read, ro_pci_enable && ro_read);
47 MLX5_SET(mkc, mkc, relaxed_ordering_write, ro_pci_enable && ro_write);
48 }
49
mlx5e_create_mkey(struct mlx5_core_dev * mdev,u32 pdn,u32 * mkey)50 int mlx5e_create_mkey(struct mlx5_core_dev *mdev, u32 pdn, u32 *mkey)
51 {
52 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
53 void *mkc;
54 u32 *in;
55 int err;
56
57 in = kvzalloc(inlen, GFP_KERNEL);
58 if (!in)
59 return -ENOMEM;
60
61 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
62 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_PA);
63 MLX5_SET(mkc, mkc, lw, 1);
64 MLX5_SET(mkc, mkc, lr, 1);
65 mlx5e_mkey_set_relaxed_ordering(mdev, mkc);
66 MLX5_SET(mkc, mkc, pd, pdn);
67 MLX5_SET(mkc, mkc, length64, 1);
68 MLX5_SET(mkc, mkc, qpn, 0xffffff);
69
70 err = mlx5_core_create_mkey(mdev, mkey, in, inlen);
71
72 kvfree(in);
73 return err;
74 }
75
mlx5e_create_mdev_resources(struct mlx5_core_dev * mdev)76 int mlx5e_create_mdev_resources(struct mlx5_core_dev *mdev)
77 {
78 struct mlx5e_hw_objs *res = &mdev->mlx5e_res.hw_objs;
79 int err;
80
81 err = mlx5_core_alloc_pd(mdev, &res->pdn);
82 if (err) {
83 mlx5_core_err(mdev, "alloc pd failed, %d\n", err);
84 return err;
85 }
86
87 err = mlx5_core_alloc_transport_domain(mdev, &res->td.tdn);
88 if (err) {
89 mlx5_core_err(mdev, "alloc td failed, %d\n", err);
90 goto err_dealloc_pd;
91 }
92
93 err = mlx5e_create_mkey(mdev, res->pdn, &res->mkey);
94 if (err) {
95 mlx5_core_err(mdev, "create mkey failed, %d\n", err);
96 goto err_dealloc_transport_domain;
97 }
98
99 err = mlx5_alloc_bfreg(mdev, &res->bfreg, false, false);
100 if (err) {
101 mlx5_core_err(mdev, "alloc bfreg failed, %d\n", err);
102 goto err_destroy_mkey;
103 }
104
105 INIT_LIST_HEAD(&res->td.tirs_list);
106 mutex_init(&res->td.list_lock);
107
108 mdev->mlx5e_res.dek_priv = mlx5_crypto_dek_init(mdev);
109 if (IS_ERR(mdev->mlx5e_res.dek_priv)) {
110 mlx5_core_err(mdev, "crypto dek init failed, %ld\n",
111 PTR_ERR(mdev->mlx5e_res.dek_priv));
112 mdev->mlx5e_res.dek_priv = NULL;
113 }
114
115 return 0;
116
117 err_destroy_mkey:
118 mlx5_core_destroy_mkey(mdev, res->mkey);
119 err_dealloc_transport_domain:
120 mlx5_core_dealloc_transport_domain(mdev, res->td.tdn);
121 err_dealloc_pd:
122 mlx5_core_dealloc_pd(mdev, res->pdn);
123 return err;
124 }
125
mlx5e_destroy_mdev_resources(struct mlx5_core_dev * mdev)126 void mlx5e_destroy_mdev_resources(struct mlx5_core_dev *mdev)
127 {
128 struct mlx5e_hw_objs *res = &mdev->mlx5e_res.hw_objs;
129
130 mlx5_crypto_dek_cleanup(mdev->mlx5e_res.dek_priv);
131 mdev->mlx5e_res.dek_priv = NULL;
132 mlx5_free_bfreg(mdev, &res->bfreg);
133 mlx5_core_destroy_mkey(mdev, res->mkey);
134 mlx5_core_dealloc_transport_domain(mdev, res->td.tdn);
135 mlx5_core_dealloc_pd(mdev, res->pdn);
136 memset(res, 0, sizeof(*res));
137 }
138
mlx5e_refresh_tirs(struct mlx5e_priv * priv,bool enable_uc_lb,bool enable_mc_lb)139 int mlx5e_refresh_tirs(struct mlx5e_priv *priv, bool enable_uc_lb,
140 bool enable_mc_lb)
141 {
142 struct mlx5_core_dev *mdev = priv->mdev;
143 struct mlx5e_tir *tir;
144 u8 lb_flags = 0;
145 int err = 0;
146 u32 tirn = 0;
147 int inlen;
148 void *in;
149
150 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
151 in = kvzalloc(inlen, GFP_KERNEL);
152 if (!in) {
153 err = -ENOMEM;
154 goto out;
155 }
156
157 if (enable_uc_lb)
158 lb_flags = MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
159
160 if (enable_mc_lb)
161 lb_flags |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
162
163 if (lb_flags)
164 MLX5_SET(modify_tir_in, in, ctx.self_lb_block, lb_flags);
165
166 MLX5_SET(modify_tir_in, in, bitmask.self_lb_en, 1);
167
168 mutex_lock(&mdev->mlx5e_res.hw_objs.td.list_lock);
169 list_for_each_entry(tir, &mdev->mlx5e_res.hw_objs.td.tirs_list, list) {
170 tirn = tir->tirn;
171 err = mlx5_core_modify_tir(mdev, tirn, in);
172 if (err)
173 goto out;
174 }
175
176 out:
177 kvfree(in);
178 if (err)
179 netdev_err(priv->netdev, "refresh tir(0x%x) failed, %d\n", tirn, err);
180 mutex_unlock(&mdev->mlx5e_res.hw_objs.td.list_lock);
181
182 return err;
183 }
184