xref: /dpdk/lib/eal/common/hotplug_mp.c (revision e8dc971b)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2018 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson #include <string.h>
599a2dd95SBruce Richardson 
699a2dd95SBruce Richardson #include <rte_eal.h>
799a2dd95SBruce Richardson #include <rte_errno.h>
899a2dd95SBruce Richardson #include <rte_alarm.h>
999a2dd95SBruce Richardson #include <rte_string_fns.h>
1099a2dd95SBruce Richardson #include <rte_devargs.h>
1199a2dd95SBruce Richardson 
1299a2dd95SBruce Richardson #include "hotplug_mp.h"
1399a2dd95SBruce Richardson #include "eal_private.h"
1499a2dd95SBruce Richardson 
1599a2dd95SBruce Richardson #define MP_TIMEOUT_S 5 /**< 5 seconds timeouts */
1699a2dd95SBruce Richardson 
1799a2dd95SBruce Richardson struct mp_reply_bundle {
1899a2dd95SBruce Richardson 	struct rte_mp_msg msg;
1999a2dd95SBruce Richardson 	void *peer;
2099a2dd95SBruce Richardson };
2199a2dd95SBruce Richardson 
cmp_dev_name(const struct rte_device * dev,const void * _name)2299a2dd95SBruce Richardson static int cmp_dev_name(const struct rte_device *dev, const void *_name)
2399a2dd95SBruce Richardson {
2499a2dd95SBruce Richardson 	const char *name = _name;
2599a2dd95SBruce Richardson 
2699a2dd95SBruce Richardson 	return strcmp(dev->name, name);
2799a2dd95SBruce Richardson }
2899a2dd95SBruce Richardson 
2999a2dd95SBruce Richardson /**
3099a2dd95SBruce Richardson  * Secondary to primary request.
3199a2dd95SBruce Richardson  * start from function eal_dev_hotplug_request_to_primary.
3299a2dd95SBruce Richardson  *
3399a2dd95SBruce Richardson  * device attach on secondary:
3499a2dd95SBruce Richardson  * a) secondary send sync request to the primary.
3599a2dd95SBruce Richardson  * b) primary receive the request and attach the new device if
3699a2dd95SBruce Richardson  *    failed goto i).
3799a2dd95SBruce Richardson  * c) primary forward attach sync request to all secondary.
3899a2dd95SBruce Richardson  * d) secondary receive the request and attach the device and send a reply.
3999a2dd95SBruce Richardson  * e) primary check the reply if all success goes to j).
4099a2dd95SBruce Richardson  * f) primary send attach rollback sync request to all secondary.
4199a2dd95SBruce Richardson  * g) secondary receive the request and detach the device and send a reply.
4299a2dd95SBruce Richardson  * h) primary receive the reply and detach device as rollback action.
4399a2dd95SBruce Richardson  * i) send attach fail to secondary as a reply of step a), goto k).
4499a2dd95SBruce Richardson  * j) send attach success to secondary as a reply of step a).
4599a2dd95SBruce Richardson  * k) secondary receive reply and return.
4699a2dd95SBruce Richardson  *
4799a2dd95SBruce Richardson  * device detach on secondary:
4899a2dd95SBruce Richardson  * a) secondary send sync request to the primary.
4999a2dd95SBruce Richardson  * b) primary send detach sync request to all secondary.
5099a2dd95SBruce Richardson  * c) secondary detach the device and send a reply.
5199a2dd95SBruce Richardson  * d) primary check the reply if all success goes to g).
5299a2dd95SBruce Richardson  * e) primary send detach rollback sync request to all secondary.
5399a2dd95SBruce Richardson  * f) secondary receive the request and attach back device. goto h).
5499a2dd95SBruce Richardson  * g) primary detach the device if success goto i), else goto e).
5599a2dd95SBruce Richardson  * h) primary send detach fail to secondary as a reply of step a), goto j).
5699a2dd95SBruce Richardson  * i) primary send detach success to secondary as a reply of step a).
5799a2dd95SBruce Richardson  * j) secondary receive reply and return.
5899a2dd95SBruce Richardson  */
5999a2dd95SBruce Richardson 
6099a2dd95SBruce Richardson static int
send_response_to_secondary(const struct eal_dev_mp_req * req,int result,const void * peer)6199a2dd95SBruce Richardson send_response_to_secondary(const struct eal_dev_mp_req *req,
6299a2dd95SBruce Richardson 			int result,
6399a2dd95SBruce Richardson 			const void *peer)
6499a2dd95SBruce Richardson {
6599a2dd95SBruce Richardson 	struct rte_mp_msg mp_resp;
6699a2dd95SBruce Richardson 	struct eal_dev_mp_req *resp =
6799a2dd95SBruce Richardson 		(struct eal_dev_mp_req *)mp_resp.param;
6899a2dd95SBruce Richardson 	int ret;
6999a2dd95SBruce Richardson 
7099a2dd95SBruce Richardson 	memset(&mp_resp, 0, sizeof(mp_resp));
7199a2dd95SBruce Richardson 	mp_resp.len_param = sizeof(*resp);
7299a2dd95SBruce Richardson 	strlcpy(mp_resp.name, EAL_DEV_MP_ACTION_REQUEST, sizeof(mp_resp.name));
7399a2dd95SBruce Richardson 	memcpy(resp, req, sizeof(*req));
7499a2dd95SBruce Richardson 	resp->result = result;
7599a2dd95SBruce Richardson 
7699a2dd95SBruce Richardson 	ret = rte_mp_reply(&mp_resp, peer);
7799a2dd95SBruce Richardson 	if (ret != 0)
7899a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "failed to send response to secondary\n");
7999a2dd95SBruce Richardson 
8099a2dd95SBruce Richardson 	return ret;
8199a2dd95SBruce Richardson }
8299a2dd95SBruce Richardson 
8399a2dd95SBruce Richardson static void
__handle_secondary_request(void * param)8499a2dd95SBruce Richardson __handle_secondary_request(void *param)
8599a2dd95SBruce Richardson {
8699a2dd95SBruce Richardson 	struct mp_reply_bundle *bundle = param;
8799a2dd95SBruce Richardson 		const struct rte_mp_msg *msg = &bundle->msg;
8899a2dd95SBruce Richardson 	const struct eal_dev_mp_req *req =
8999a2dd95SBruce Richardson 		(const struct eal_dev_mp_req *)msg->param;
9099a2dd95SBruce Richardson 	struct eal_dev_mp_req tmp_req;
9199a2dd95SBruce Richardson 	struct rte_devargs da;
9299a2dd95SBruce Richardson 	struct rte_device *dev;
9399a2dd95SBruce Richardson 	struct rte_bus *bus;
9499a2dd95SBruce Richardson 	int ret = 0;
9599a2dd95SBruce Richardson 
9699a2dd95SBruce Richardson 	tmp_req = *req;
9799a2dd95SBruce Richardson 
9899a2dd95SBruce Richardson 	memset(&da, 0, sizeof(da));
9999a2dd95SBruce Richardson 	if (req->t == EAL_DEV_REQ_TYPE_ATTACH) {
10099a2dd95SBruce Richardson 		ret = local_dev_probe(req->devargs, &dev);
1017bc7bc35SChangpeng Liu 		if (ret != 0 && ret != -EEXIST) {
10299a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Failed to hotplug add device on primary\n");
10399a2dd95SBruce Richardson 			goto finish;
10499a2dd95SBruce Richardson 		}
10599a2dd95SBruce Richardson 		ret = eal_dev_hotplug_request_to_secondary(&tmp_req);
10699a2dd95SBruce Richardson 		if (ret != 0) {
10799a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Failed to send hotplug request to secondary\n");
10899a2dd95SBruce Richardson 			ret = -ENOMSG;
10999a2dd95SBruce Richardson 			goto rollback;
11099a2dd95SBruce Richardson 		}
11199a2dd95SBruce Richardson 		if (tmp_req.result != 0) {
11299a2dd95SBruce Richardson 			ret = tmp_req.result;
11399a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Failed to hotplug add device on secondary\n");
11499a2dd95SBruce Richardson 			if (ret != -EEXIST)
11599a2dd95SBruce Richardson 				goto rollback;
11699a2dd95SBruce Richardson 		}
11799a2dd95SBruce Richardson 	} else if (req->t == EAL_DEV_REQ_TYPE_DETACH) {
11899a2dd95SBruce Richardson 		ret = rte_devargs_parse(&da, req->devargs);
11999a2dd95SBruce Richardson 		if (ret != 0)
12099a2dd95SBruce Richardson 			goto finish;
12199a2dd95SBruce Richardson 
12299a2dd95SBruce Richardson 		ret = eal_dev_hotplug_request_to_secondary(&tmp_req);
12399a2dd95SBruce Richardson 		if (ret != 0) {
12499a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Failed to send hotplug request to secondary\n");
12599a2dd95SBruce Richardson 			ret = -ENOMSG;
12699a2dd95SBruce Richardson 			goto rollback;
12799a2dd95SBruce Richardson 		}
12899a2dd95SBruce Richardson 
12999a2dd95SBruce Richardson 		bus = rte_bus_find_by_name(da.bus->name);
13099a2dd95SBruce Richardson 		if (bus == NULL) {
13199a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", da.bus->name);
13299a2dd95SBruce Richardson 			ret = -ENOENT;
13399a2dd95SBruce Richardson 			goto finish;
13499a2dd95SBruce Richardson 		}
13599a2dd95SBruce Richardson 
13699a2dd95SBruce Richardson 		dev = bus->find_device(NULL, cmp_dev_name, da.name);
13799a2dd95SBruce Richardson 		if (dev == NULL) {
13899a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", da.name);
13999a2dd95SBruce Richardson 			ret = -ENOENT;
14099a2dd95SBruce Richardson 			goto finish;
14199a2dd95SBruce Richardson 		}
14299a2dd95SBruce Richardson 
14399a2dd95SBruce Richardson 		if (tmp_req.result != 0) {
14499a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Failed to hotplug remove device on secondary\n");
14599a2dd95SBruce Richardson 			ret = tmp_req.result;
14699a2dd95SBruce Richardson 			if (ret != -ENOENT)
14799a2dd95SBruce Richardson 				goto rollback;
14899a2dd95SBruce Richardson 		}
14999a2dd95SBruce Richardson 
15099a2dd95SBruce Richardson 		ret = local_dev_remove(dev);
15199a2dd95SBruce Richardson 		if (ret != 0) {
15299a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Failed to hotplug remove device on primary\n");
15399a2dd95SBruce Richardson 			if (ret != -ENOENT)
15499a2dd95SBruce Richardson 				goto rollback;
15599a2dd95SBruce Richardson 		}
15699a2dd95SBruce Richardson 	} else {
15799a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "unsupported secondary to primary request\n");
15899a2dd95SBruce Richardson 		ret = -ENOTSUP;
15999a2dd95SBruce Richardson 	}
16099a2dd95SBruce Richardson 	goto finish;
16199a2dd95SBruce Richardson 
16299a2dd95SBruce Richardson rollback:
16399a2dd95SBruce Richardson 	if (req->t == EAL_DEV_REQ_TYPE_ATTACH) {
16499a2dd95SBruce Richardson 		tmp_req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
16599a2dd95SBruce Richardson 		eal_dev_hotplug_request_to_secondary(&tmp_req);
16699a2dd95SBruce Richardson 		local_dev_remove(dev);
16799a2dd95SBruce Richardson 	} else {
16899a2dd95SBruce Richardson 		tmp_req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
16999a2dd95SBruce Richardson 		eal_dev_hotplug_request_to_secondary(&tmp_req);
17099a2dd95SBruce Richardson 	}
17199a2dd95SBruce Richardson 
17299a2dd95SBruce Richardson finish:
17399a2dd95SBruce Richardson 	ret = send_response_to_secondary(&tmp_req, ret, bundle->peer);
17499a2dd95SBruce Richardson 	if (ret)
17599a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "failed to send response to secondary\n");
17699a2dd95SBruce Richardson 
17799a2dd95SBruce Richardson 	rte_devargs_reset(&da);
17899a2dd95SBruce Richardson 	free(bundle->peer);
17999a2dd95SBruce Richardson 	free(bundle);
18099a2dd95SBruce Richardson }
18199a2dd95SBruce Richardson 
18299a2dd95SBruce Richardson static int
handle_secondary_request(const struct rte_mp_msg * msg,const void * peer)18399a2dd95SBruce Richardson handle_secondary_request(const struct rte_mp_msg *msg, const void *peer)
18499a2dd95SBruce Richardson {
18599a2dd95SBruce Richardson 	struct mp_reply_bundle *bundle;
18699a2dd95SBruce Richardson 	const struct eal_dev_mp_req *req =
18799a2dd95SBruce Richardson 		(const struct eal_dev_mp_req *)msg->param;
18899a2dd95SBruce Richardson 	int ret = 0;
18999a2dd95SBruce Richardson 
19099a2dd95SBruce Richardson 	bundle = malloc(sizeof(*bundle));
19199a2dd95SBruce Richardson 	if (bundle == NULL) {
19299a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "not enough memory\n");
19399a2dd95SBruce Richardson 		return send_response_to_secondary(req, -ENOMEM, peer);
19499a2dd95SBruce Richardson 	}
19599a2dd95SBruce Richardson 
19699a2dd95SBruce Richardson 	bundle->msg = *msg;
19799a2dd95SBruce Richardson 	/**
19899a2dd95SBruce Richardson 	 * We need to send reply on interrupt thread, but peer can't be
19999a2dd95SBruce Richardson 	 * parsed directly, so this is a temporal hack, need to be fixed
20099a2dd95SBruce Richardson 	 * when it is ready.
20199a2dd95SBruce Richardson 	 */
20299a2dd95SBruce Richardson 	bundle->peer = strdup(peer);
20399a2dd95SBruce Richardson 	if (bundle->peer == NULL) {
20499a2dd95SBruce Richardson 		free(bundle);
20599a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "not enough memory\n");
20699a2dd95SBruce Richardson 		return send_response_to_secondary(req, -ENOMEM, peer);
20799a2dd95SBruce Richardson 	}
20899a2dd95SBruce Richardson 
20999a2dd95SBruce Richardson 	/**
21099a2dd95SBruce Richardson 	 * We are at IPC callback thread, sync IPC is not allowed due to
21199a2dd95SBruce Richardson 	 * dead lock, so we delegate the task to interrupt thread.
21299a2dd95SBruce Richardson 	 */
21399a2dd95SBruce Richardson 	ret = rte_eal_alarm_set(1, __handle_secondary_request, bundle);
21499a2dd95SBruce Richardson 	if (ret != 0) {
21599a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "failed to add mp task\n");
21699a2dd95SBruce Richardson 		free(bundle->peer);
21799a2dd95SBruce Richardson 		free(bundle);
21899a2dd95SBruce Richardson 		return send_response_to_secondary(req, ret, peer);
21999a2dd95SBruce Richardson 	}
22099a2dd95SBruce Richardson 	return 0;
22199a2dd95SBruce Richardson }
22299a2dd95SBruce Richardson 
__handle_primary_request(void * param)22399a2dd95SBruce Richardson static void __handle_primary_request(void *param)
22499a2dd95SBruce Richardson {
22599a2dd95SBruce Richardson 	struct mp_reply_bundle *bundle = param;
22699a2dd95SBruce Richardson 	struct rte_mp_msg *msg = &bundle->msg;
22799a2dd95SBruce Richardson 	const struct eal_dev_mp_req *req =
22899a2dd95SBruce Richardson 		(const struct eal_dev_mp_req *)msg->param;
22999a2dd95SBruce Richardson 	struct rte_mp_msg mp_resp;
23099a2dd95SBruce Richardson 	struct eal_dev_mp_req *resp =
23199a2dd95SBruce Richardson 		(struct eal_dev_mp_req *)mp_resp.param;
23299a2dd95SBruce Richardson 	struct rte_devargs *da;
23399a2dd95SBruce Richardson 	struct rte_device *dev;
23499a2dd95SBruce Richardson 	struct rte_bus *bus;
23599a2dd95SBruce Richardson 	int ret = 0;
23699a2dd95SBruce Richardson 
23799a2dd95SBruce Richardson 	memset(&mp_resp, 0, sizeof(mp_resp));
23899a2dd95SBruce Richardson 
23999a2dd95SBruce Richardson 	switch (req->t) {
24099a2dd95SBruce Richardson 	case EAL_DEV_REQ_TYPE_ATTACH:
24199a2dd95SBruce Richardson 	case EAL_DEV_REQ_TYPE_DETACH_ROLLBACK:
24299a2dd95SBruce Richardson 		ret = local_dev_probe(req->devargs, &dev);
24399a2dd95SBruce Richardson 		break;
24499a2dd95SBruce Richardson 	case EAL_DEV_REQ_TYPE_DETACH:
24599a2dd95SBruce Richardson 	case EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK:
24699a2dd95SBruce Richardson 		da = calloc(1, sizeof(*da));
24799a2dd95SBruce Richardson 		if (da == NULL) {
24899a2dd95SBruce Richardson 			ret = -ENOMEM;
24999a2dd95SBruce Richardson 			break;
25099a2dd95SBruce Richardson 		}
25199a2dd95SBruce Richardson 
25299a2dd95SBruce Richardson 		ret = rte_devargs_parse(da, req->devargs);
25399a2dd95SBruce Richardson 		if (ret != 0)
25499a2dd95SBruce Richardson 			goto quit;
25599a2dd95SBruce Richardson 
25699a2dd95SBruce Richardson 		bus = rte_bus_find_by_name(da->bus->name);
25799a2dd95SBruce Richardson 		if (bus == NULL) {
25899a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", da->bus->name);
25999a2dd95SBruce Richardson 			ret = -ENOENT;
26099a2dd95SBruce Richardson 			goto quit;
26199a2dd95SBruce Richardson 		}
26299a2dd95SBruce Richardson 
26399a2dd95SBruce Richardson 		dev = bus->find_device(NULL, cmp_dev_name, da->name);
26499a2dd95SBruce Richardson 		if (dev == NULL) {
26599a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", da->name);
26699a2dd95SBruce Richardson 			ret = -ENOENT;
26799a2dd95SBruce Richardson 			goto quit;
26899a2dd95SBruce Richardson 		}
26999a2dd95SBruce Richardson 
27099a2dd95SBruce Richardson 		if (!rte_dev_is_probed(dev)) {
27199a2dd95SBruce Richardson 			if (req->t == EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK) {
27299a2dd95SBruce Richardson 				/**
27399a2dd95SBruce Richardson 				 * Don't fail the rollback just because there's
27499a2dd95SBruce Richardson 				 * nothing to do.
27599a2dd95SBruce Richardson 				 */
27699a2dd95SBruce Richardson 				ret = 0;
27799a2dd95SBruce Richardson 			} else
27899a2dd95SBruce Richardson 				ret = -ENODEV;
27999a2dd95SBruce Richardson 
28099a2dd95SBruce Richardson 			goto quit;
28199a2dd95SBruce Richardson 		}
28299a2dd95SBruce Richardson 
28399a2dd95SBruce Richardson 		ret = local_dev_remove(dev);
28499a2dd95SBruce Richardson quit:
28599a2dd95SBruce Richardson 		rte_devargs_reset(da);
28699a2dd95SBruce Richardson 		free(da);
28799a2dd95SBruce Richardson 		break;
28899a2dd95SBruce Richardson 	default:
28999a2dd95SBruce Richardson 		ret = -EINVAL;
29099a2dd95SBruce Richardson 	}
29199a2dd95SBruce Richardson 
29299a2dd95SBruce Richardson 	strlcpy(mp_resp.name, EAL_DEV_MP_ACTION_REQUEST, sizeof(mp_resp.name));
29399a2dd95SBruce Richardson 	mp_resp.len_param = sizeof(*req);
29499a2dd95SBruce Richardson 	memcpy(resp, req, sizeof(*resp));
29599a2dd95SBruce Richardson 	resp->result = ret;
29699a2dd95SBruce Richardson 	if (rte_mp_reply(&mp_resp, bundle->peer) < 0)
29799a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "failed to send reply to primary request\n");
29899a2dd95SBruce Richardson 
29999a2dd95SBruce Richardson 	free(bundle->peer);
30099a2dd95SBruce Richardson 	free(bundle);
30199a2dd95SBruce Richardson }
30299a2dd95SBruce Richardson 
30399a2dd95SBruce Richardson static int
handle_primary_request(const struct rte_mp_msg * msg,const void * peer)30499a2dd95SBruce Richardson handle_primary_request(const struct rte_mp_msg *msg, const void *peer)
30599a2dd95SBruce Richardson {
30699a2dd95SBruce Richardson 	struct rte_mp_msg mp_resp;
30799a2dd95SBruce Richardson 	const struct eal_dev_mp_req *req =
30899a2dd95SBruce Richardson 		(const struct eal_dev_mp_req *)msg->param;
30999a2dd95SBruce Richardson 	struct eal_dev_mp_req *resp =
31099a2dd95SBruce Richardson 		(struct eal_dev_mp_req *)mp_resp.param;
31199a2dd95SBruce Richardson 	struct mp_reply_bundle *bundle;
31299a2dd95SBruce Richardson 	int ret = 0;
31399a2dd95SBruce Richardson 
31499a2dd95SBruce Richardson 	memset(&mp_resp, 0, sizeof(mp_resp));
31599a2dd95SBruce Richardson 	strlcpy(mp_resp.name, EAL_DEV_MP_ACTION_REQUEST, sizeof(mp_resp.name));
31699a2dd95SBruce Richardson 	mp_resp.len_param = sizeof(*req);
31799a2dd95SBruce Richardson 	memcpy(resp, req, sizeof(*resp));
31899a2dd95SBruce Richardson 
31999a2dd95SBruce Richardson 	bundle = calloc(1, sizeof(*bundle));
32099a2dd95SBruce Richardson 	if (bundle == NULL) {
32199a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "not enough memory\n");
32299a2dd95SBruce Richardson 		resp->result = -ENOMEM;
32399a2dd95SBruce Richardson 		ret = rte_mp_reply(&mp_resp, peer);
32499a2dd95SBruce Richardson 		if (ret)
32599a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "failed to send reply to primary request\n");
32699a2dd95SBruce Richardson 		return ret;
32799a2dd95SBruce Richardson 	}
32899a2dd95SBruce Richardson 
32999a2dd95SBruce Richardson 	bundle->msg = *msg;
33099a2dd95SBruce Richardson 	/**
33199a2dd95SBruce Richardson 	 * We need to send reply on interrupt thread, but peer can't be
33299a2dd95SBruce Richardson 	 * parsed directly, so this is a temporal hack, need to be fixed
33399a2dd95SBruce Richardson 	 * when it is ready.
33499a2dd95SBruce Richardson 	 */
33599a2dd95SBruce Richardson 	bundle->peer = (void *)strdup(peer);
33699a2dd95SBruce Richardson 	if (bundle->peer == NULL) {
33799a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "not enough memory\n");
33899a2dd95SBruce Richardson 		free(bundle);
33999a2dd95SBruce Richardson 		resp->result = -ENOMEM;
34099a2dd95SBruce Richardson 		ret = rte_mp_reply(&mp_resp, peer);
34199a2dd95SBruce Richardson 		if (ret)
34299a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "failed to send reply to primary request\n");
34399a2dd95SBruce Richardson 		return ret;
34499a2dd95SBruce Richardson 	}
34599a2dd95SBruce Richardson 
34699a2dd95SBruce Richardson 	/**
34799a2dd95SBruce Richardson 	 * We are at IPC callback thread, sync IPC is not allowed due to
34899a2dd95SBruce Richardson 	 * dead lock, so we delegate the task to interrupt thread.
34999a2dd95SBruce Richardson 	 */
35099a2dd95SBruce Richardson 	ret = rte_eal_alarm_set(1, __handle_primary_request, bundle);
35199a2dd95SBruce Richardson 	if (ret != 0) {
35299a2dd95SBruce Richardson 		free(bundle->peer);
35399a2dd95SBruce Richardson 		free(bundle);
35499a2dd95SBruce Richardson 		resp->result = ret;
35599a2dd95SBruce Richardson 		ret = rte_mp_reply(&mp_resp, peer);
35699a2dd95SBruce Richardson 		if  (ret != 0) {
35799a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "failed to send reply to primary request\n");
35899a2dd95SBruce Richardson 			return ret;
35999a2dd95SBruce Richardson 		}
36099a2dd95SBruce Richardson 	}
36199a2dd95SBruce Richardson 	return 0;
36299a2dd95SBruce Richardson }
36399a2dd95SBruce Richardson 
eal_dev_hotplug_request_to_primary(struct eal_dev_mp_req * req)36499a2dd95SBruce Richardson int eal_dev_hotplug_request_to_primary(struct eal_dev_mp_req *req)
36599a2dd95SBruce Richardson {
36699a2dd95SBruce Richardson 	struct rte_mp_msg mp_req;
36799a2dd95SBruce Richardson 	struct rte_mp_reply mp_reply;
36899a2dd95SBruce Richardson 	struct timespec ts = {.tv_sec = MP_TIMEOUT_S, .tv_nsec = 0};
36999a2dd95SBruce Richardson 	struct eal_dev_mp_req *resp;
37099a2dd95SBruce Richardson 	int ret;
37199a2dd95SBruce Richardson 
37299a2dd95SBruce Richardson 	memset(&mp_req, 0, sizeof(mp_req));
37399a2dd95SBruce Richardson 	memcpy(mp_req.param, req, sizeof(*req));
37499a2dd95SBruce Richardson 	mp_req.len_param = sizeof(*req);
37599a2dd95SBruce Richardson 	strlcpy(mp_req.name, EAL_DEV_MP_ACTION_REQUEST, sizeof(mp_req.name));
37699a2dd95SBruce Richardson 
37799a2dd95SBruce Richardson 	ret = rte_mp_request_sync(&mp_req, &mp_reply, &ts);
37899a2dd95SBruce Richardson 	if (ret || mp_reply.nb_received != 1) {
37999a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "Cannot send request to primary\n");
38099a2dd95SBruce Richardson 		if (!ret)
38199a2dd95SBruce Richardson 			return -1;
38299a2dd95SBruce Richardson 		return ret;
38399a2dd95SBruce Richardson 	}
38499a2dd95SBruce Richardson 
38599a2dd95SBruce Richardson 	resp = (struct eal_dev_mp_req *)mp_reply.msgs[0].param;
38699a2dd95SBruce Richardson 	req->result = resp->result;
38799a2dd95SBruce Richardson 
38899a2dd95SBruce Richardson 	free(mp_reply.msgs);
38999a2dd95SBruce Richardson 	return ret;
39099a2dd95SBruce Richardson }
39199a2dd95SBruce Richardson 
eal_dev_hotplug_request_to_secondary(struct eal_dev_mp_req * req)39299a2dd95SBruce Richardson int eal_dev_hotplug_request_to_secondary(struct eal_dev_mp_req *req)
39399a2dd95SBruce Richardson {
39499a2dd95SBruce Richardson 	struct rte_mp_msg mp_req;
39599a2dd95SBruce Richardson 	struct rte_mp_reply mp_reply;
39699a2dd95SBruce Richardson 	struct timespec ts = {.tv_sec = MP_TIMEOUT_S, .tv_nsec = 0};
39799a2dd95SBruce Richardson 	int ret;
39899a2dd95SBruce Richardson 	int i;
39999a2dd95SBruce Richardson 
40099a2dd95SBruce Richardson 	memset(&mp_req, 0, sizeof(mp_req));
40199a2dd95SBruce Richardson 	memcpy(mp_req.param, req, sizeof(*req));
40299a2dd95SBruce Richardson 	mp_req.len_param = sizeof(*req);
40399a2dd95SBruce Richardson 	strlcpy(mp_req.name, EAL_DEV_MP_ACTION_REQUEST, sizeof(mp_req.name));
40499a2dd95SBruce Richardson 
40599a2dd95SBruce Richardson 	ret = rte_mp_request_sync(&mp_req, &mp_reply, &ts);
40699a2dd95SBruce Richardson 	if (ret != 0) {
40799a2dd95SBruce Richardson 		/* if IPC is not supported, behave as if the call succeeded */
40899a2dd95SBruce Richardson 		if (rte_errno != ENOTSUP)
40999a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "rte_mp_request_sync failed\n");
41099a2dd95SBruce Richardson 		else
41199a2dd95SBruce Richardson 			ret = 0;
41299a2dd95SBruce Richardson 		return ret;
41399a2dd95SBruce Richardson 	}
41499a2dd95SBruce Richardson 
41599a2dd95SBruce Richardson 	if (mp_reply.nb_sent != mp_reply.nb_received) {
41699a2dd95SBruce Richardson 		RTE_LOG(ERR, EAL, "not all secondary reply\n");
41799a2dd95SBruce Richardson 		free(mp_reply.msgs);
41899a2dd95SBruce Richardson 		return -1;
41999a2dd95SBruce Richardson 	}
42099a2dd95SBruce Richardson 
42199a2dd95SBruce Richardson 	req->result = 0;
42299a2dd95SBruce Richardson 	for (i = 0; i < mp_reply.nb_received; i++) {
42399a2dd95SBruce Richardson 		struct eal_dev_mp_req *resp =
42499a2dd95SBruce Richardson 			(struct eal_dev_mp_req *)mp_reply.msgs[i].param;
42599a2dd95SBruce Richardson 		if (resp->result != 0) {
42699a2dd95SBruce Richardson 			if (req->t == EAL_DEV_REQ_TYPE_ATTACH &&
42799a2dd95SBruce Richardson 				resp->result == -EEXIST)
42899a2dd95SBruce Richardson 				continue;
42999a2dd95SBruce Richardson 			if (req->t == EAL_DEV_REQ_TYPE_DETACH &&
43099a2dd95SBruce Richardson 				resp->result == -ENOENT)
43199a2dd95SBruce Richardson 				continue;
43299a2dd95SBruce Richardson 			req->result = resp->result;
43399a2dd95SBruce Richardson 		}
43499a2dd95SBruce Richardson 	}
43599a2dd95SBruce Richardson 
43699a2dd95SBruce Richardson 	free(mp_reply.msgs);
43799a2dd95SBruce Richardson 	return 0;
43899a2dd95SBruce Richardson }
43999a2dd95SBruce Richardson 
eal_mp_dev_hotplug_init(void)44099a2dd95SBruce Richardson int eal_mp_dev_hotplug_init(void)
44199a2dd95SBruce Richardson {
44299a2dd95SBruce Richardson 	int ret;
44399a2dd95SBruce Richardson 
44499a2dd95SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
44599a2dd95SBruce Richardson 		ret = rte_mp_action_register(EAL_DEV_MP_ACTION_REQUEST,
44699a2dd95SBruce Richardson 					handle_secondary_request);
44799a2dd95SBruce Richardson 		/* primary is allowed to not support IPC */
44899a2dd95SBruce Richardson 		if (ret != 0 && rte_errno != ENOTSUP) {
44999a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Couldn't register '%s' action\n",
45099a2dd95SBruce Richardson 				EAL_DEV_MP_ACTION_REQUEST);
45199a2dd95SBruce Richardson 			return ret;
45299a2dd95SBruce Richardson 		}
45399a2dd95SBruce Richardson 	} else {
45499a2dd95SBruce Richardson 		ret = rte_mp_action_register(EAL_DEV_MP_ACTION_REQUEST,
45599a2dd95SBruce Richardson 					handle_primary_request);
45699a2dd95SBruce Richardson 		if (ret != 0) {
45799a2dd95SBruce Richardson 			RTE_LOG(ERR, EAL, "Couldn't register '%s' action\n",
45899a2dd95SBruce Richardson 				EAL_DEV_MP_ACTION_REQUEST);
45999a2dd95SBruce Richardson 			return ret;
46099a2dd95SBruce Richardson 		}
46199a2dd95SBruce Richardson 	}
46299a2dd95SBruce Richardson 
46399a2dd95SBruce Richardson 	return 0;
46499a2dd95SBruce Richardson }
465*e8dc971bSStephen Hemminger 
eal_mp_dev_hotplug_cleanup(void)466*e8dc971bSStephen Hemminger void eal_mp_dev_hotplug_cleanup(void)
467*e8dc971bSStephen Hemminger {
468*e8dc971bSStephen Hemminger 	rte_mp_action_unregister(EAL_DEV_MP_ACTION_REQUEST);
469*e8dc971bSStephen Hemminger }
470