hnae3.c 9.0 KB
Newer Older
1 2
// SPDX-License-Identifier: GPL-2.0+
// Copyright (c) 2016-2017 Hisilicon Limited.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

#include <linux/list.h>
#include <linux/spinlock.h>

#include "hnae3.h"

static LIST_HEAD(hnae3_ae_algo_list);
static LIST_HEAD(hnae3_client_list);
static LIST_HEAD(hnae3_ae_dev_list);

/* we are keeping things simple and using single lock for all the
 * list. This is a non-critical code so other updations, if happen
 * in parallel, can wait.
 */
static DEFINE_MUTEX(hnae3_common_lock);

static bool hnae3_client_match(enum hnae3_client_type client_type,
			       enum hnae3_dev_type dev_type)
{
	if ((dev_type == HNAE3_DEV_KNIC) && (client_type == HNAE3_CLIENT_KNIC ||
					     client_type == HNAE3_CLIENT_ROCE))
		return true;

	if (dev_type == HNAE3_DEV_UNIC && client_type == HNAE3_CLIENT_UNIC)
		return true;

	return false;
}

32 33
void hnae3_set_client_init_flag(struct hnae3_client *client,
				struct hnae3_ae_dev *ae_dev, int inited)
34
{
35 36 37
	if (!client || !ae_dev)
		return;

38 39
	switch (client->type) {
	case HNAE3_CLIENT_KNIC:
P
Peng Li 已提交
40
		hnae3_set_bit(ae_dev->flag, HNAE3_KNIC_CLIENT_INITED_B, inited);
41 42
		break;
	case HNAE3_CLIENT_UNIC:
P
Peng Li 已提交
43
		hnae3_set_bit(ae_dev->flag, HNAE3_UNIC_CLIENT_INITED_B, inited);
44 45
		break;
	case HNAE3_CLIENT_ROCE:
P
Peng Li 已提交
46
		hnae3_set_bit(ae_dev->flag, HNAE3_ROCE_CLIENT_INITED_B, inited);
47 48 49 50 51
		break;
	default:
		break;
	}
}
52
EXPORT_SYMBOL(hnae3_set_client_init_flag);
53 54 55 56 57 58 59 60

static int hnae3_get_client_init_flag(struct hnae3_client *client,
				       struct hnae3_ae_dev *ae_dev)
{
	int inited = 0;

	switch (client->type) {
	case HNAE3_CLIENT_KNIC:
P
Peng Li 已提交
61
		inited = hnae3_get_bit(ae_dev->flag,
62 63 64
				       HNAE3_KNIC_CLIENT_INITED_B);
		break;
	case HNAE3_CLIENT_UNIC:
P
Peng Li 已提交
65
		inited = hnae3_get_bit(ae_dev->flag,
66 67 68
				       HNAE3_UNIC_CLIENT_INITED_B);
		break;
	case HNAE3_CLIENT_ROCE:
P
Peng Li 已提交
69 70
		inited = hnae3_get_bit(ae_dev->flag,
				       HNAE3_ROCE_CLIENT_INITED_B);
71 72 73 74 75 76 77 78
		break;
	default:
		break;
	}

	return inited;
}

79 80
static int hnae3_init_client_instance(struct hnae3_client *client,
				      struct hnae3_ae_dev *ae_dev)
81 82 83 84 85
{
	int ret;

	/* check if this client matches the type of ae_dev */
	if (!(hnae3_client_match(client->type, ae_dev->dev_type) &&
P
Peng Li 已提交
86
	      hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) {
87 88 89
		return 0;
	}

90 91 92 93
	ret = ae_dev->ops->init_client_instance(client, ae_dev);
	if (ret)
		dev_err(&ae_dev->pdev->dev,
			"fail to instantiate client, ret = %d\n", ret);
94

95 96 97 98 99 100 101 102 103 104
	return ret;
}

static void hnae3_uninit_client_instance(struct hnae3_client *client,
					 struct hnae3_ae_dev *ae_dev)
{
	/* check if this client matches the type of ae_dev */
	if (!(hnae3_client_match(client->type, ae_dev->dev_type) &&
	      hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B)))
		return;
105

106
	if (hnae3_get_client_init_flag(client, ae_dev)) {
107 108
		ae_dev->ops->uninit_client_instance(client, ae_dev);

109
		hnae3_set_client_init_flag(client, ae_dev, 0);
110 111 112 113 114 115 116 117 118
	}
}

int hnae3_register_client(struct hnae3_client *client)
{
	struct hnae3_client *client_tmp;
	struct hnae3_ae_dev *ae_dev;
	int ret = 0;

119 120 121
	if (!client)
		return -ENODEV;

122 123 124 125 126 127 128 129 130 131 132 133 134 135
	mutex_lock(&hnae3_common_lock);
	/* one system should only have one client for every type */
	list_for_each_entry(client_tmp, &hnae3_client_list, node) {
		if (client_tmp->type == client->type)
			goto exit;
	}

	list_add_tail(&client->node, &hnae3_client_list);

	/* initialize the client on every matched port */
	list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
		/* if the client could not be initialized on current port, for
		 * any error reasons, move on to next available port
		 */
136
		ret = hnae3_init_client_instance(client, ae_dev);
137 138
		if (ret)
			dev_err(&ae_dev->pdev->dev,
139 140
				"match and instantiation failed for port, ret = %d\n",
				ret);
141 142 143 144 145
	}

exit:
	mutex_unlock(&hnae3_common_lock);

146
	return 0;
147 148 149 150 151 152 153
}
EXPORT_SYMBOL(hnae3_register_client);

void hnae3_unregister_client(struct hnae3_client *client)
{
	struct hnae3_ae_dev *ae_dev;

154 155 156
	if (!client)
		return;

157 158 159
	mutex_lock(&hnae3_common_lock);
	/* un-initialize the client on every matched port */
	list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
160
		hnae3_uninit_client_instance(client, ae_dev);
161 162 163 164 165 166 167 168 169 170 171
	}

	list_del(&client->node);
	mutex_unlock(&hnae3_common_lock);
}
EXPORT_SYMBOL(hnae3_unregister_client);

/* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework
 * @ae_algo: AE algorithm
 * NOTE: the duplicated name will not be checked
 */
172
void hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo)
173 174 175 176 177 178
{
	const struct pci_device_id *id;
	struct hnae3_ae_dev *ae_dev;
	struct hnae3_client *client;
	int ret = 0;

179 180 181
	if (!ae_algo)
		return;

182 183 184 185 186 187 188 189 190 191
	mutex_lock(&hnae3_common_lock);

	list_add_tail(&ae_algo->node, &hnae3_ae_algo_list);

	/* Check if this algo/ops matches the list of ae_devs */
	list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
		id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
		if (!id)
			continue;

192 193 194 195
		if (!ae_algo->ops) {
			dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
			continue;
		}
196
		ae_dev->ops = ae_algo->ops;
197

198 199
		ret = ae_algo->ops->init_ae_dev(ae_dev);
		if (ret) {
200 201
			dev_err(&ae_dev->pdev->dev,
				"init ae_dev error, ret = %d\n", ret);
202 203 204
			continue;
		}

205
		/* ae_dev init should set flag */
P
Peng Li 已提交
206
		hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
207 208 209 210 211

		/* check the client list for the match with this ae_dev type and
		 * initialize the figure out client instance
		 */
		list_for_each_entry(client, &hnae3_client_list, node) {
212
			ret = hnae3_init_client_instance(client, ae_dev);
213 214
			if (ret)
				dev_err(&ae_dev->pdev->dev,
215 216
					"match and instantiation failed, ret = %d\n",
					ret);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		}
	}

	mutex_unlock(&hnae3_common_lock);
}
EXPORT_SYMBOL(hnae3_register_ae_algo);

/* hnae3_unregister_ae_algo - unregisters a AE algorithm
 * @ae_algo: the AE algorithm to unregister
 */
void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo)
{
	const struct pci_device_id *id;
	struct hnae3_ae_dev *ae_dev;
	struct hnae3_client *client;

233 234 235
	if (!ae_algo)
		return;

236 237 238
	mutex_lock(&hnae3_common_lock);
	/* Check if there are matched ae_dev */
	list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
P
Peng Li 已提交
239
		if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
240 241
			continue;

242 243 244 245 246 247 248
		id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
		if (!id)
			continue;

		/* check the client list for the match with this ae_dev type and
		 * un-initialize the figure out client instance
		 */
249
		list_for_each_entry(client, &hnae3_client_list, node)
250
			hnae3_uninit_client_instance(client, ae_dev);
251 252

		ae_algo->ops->uninit_ae_dev(ae_dev);
P
Peng Li 已提交
253
		hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
254
		ae_dev->ops = NULL;
255 256 257 258 259 260 261 262 263 264 265
	}

	list_del(&ae_algo->node);
	mutex_unlock(&hnae3_common_lock);
}
EXPORT_SYMBOL(hnae3_unregister_ae_algo);

/* hnae3_register_ae_dev - registers a AE device to hnae3 framework
 * @ae_dev: the AE device
 * NOTE: the duplicated name will not be checked
 */
266
int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev)
267 268 269 270
{
	const struct pci_device_id *id;
	struct hnae3_ae_algo *ae_algo;
	struct hnae3_client *client;
271
	int ret = 0;
272

273 274 275
	if (!ae_dev)
		return -ENODEV;

276
	mutex_lock(&hnae3_common_lock);
277 278 279 280 281 282 283 284 285

	list_add_tail(&ae_dev->node, &hnae3_ae_dev_list);

	/* Check if there are matched ae_algo */
	list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
		id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
		if (!id)
			continue;

286 287
		if (!ae_algo->ops) {
			dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
288
			ret = -EOPNOTSUPP;
289 290
			goto out_err;
		}
291
		ae_dev->ops = ae_algo->ops;
292 293 294

		ret = ae_dev->ops->init_ae_dev(ae_dev);
		if (ret) {
295 296
			dev_err(&ae_dev->pdev->dev,
				"init ae_dev error, ret = %d\n", ret);
297 298 299
			goto out_err;
		}

300
		/* ae_dev init should set flag */
P
Peng Li 已提交
301
		hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
302 303 304 305 306 307 308
		break;
	}

	/* check the client list for the match with this ae_dev type and
	 * initialize the figure out client instance
	 */
	list_for_each_entry(client, &hnae3_client_list, node) {
309
		ret = hnae3_init_client_instance(client, ae_dev);
310 311
		if (ret)
			dev_err(&ae_dev->pdev->dev,
312 313
				"match and instantiation failed, ret = %d\n",
				ret);
314 315
	}

316 317 318 319
	mutex_unlock(&hnae3_common_lock);

	return 0;

320
out_err:
321
	list_del(&ae_dev->node);
322
	mutex_unlock(&hnae3_common_lock);
323 324

	return ret;
325 326 327 328 329 330 331 332 333 334 335 336
}
EXPORT_SYMBOL(hnae3_register_ae_dev);

/* hnae3_unregister_ae_dev - unregisters a AE device
 * @ae_dev: the AE device to unregister
 */
void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev)
{
	const struct pci_device_id *id;
	struct hnae3_ae_algo *ae_algo;
	struct hnae3_client *client;

337 338 339
	if (!ae_dev)
		return;

340 341 342
	mutex_lock(&hnae3_common_lock);
	/* Check if there are matched ae_algo */
	list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
P
Peng Li 已提交
343
		if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
344 345
			continue;

346 347 348 349
		id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
		if (!id)
			continue;

350
		list_for_each_entry(client, &hnae3_client_list, node)
351
			hnae3_uninit_client_instance(client, ae_dev);
352 353

		ae_algo->ops->uninit_ae_dev(ae_dev);
P
Peng Li 已提交
354
		hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
355
		ae_dev->ops = NULL;
356 357 358 359 360 361 362 363 364 365
	}

	list_del(&ae_dev->node);
	mutex_unlock(&hnae3_common_lock);
}
EXPORT_SYMBOL(hnae3_unregister_ae_dev);

MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework");
366
MODULE_VERSION(HNAE3_MOD_VERSION);