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