aboutsummaryrefslogtreecommitdiff
path: root/core/pldm/pldm-requester.c
blob: 9830cda993672674dd7fd8cba7e63a93c9f99853 (plain)
1
2
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
// Copyright 2022 IBM Corp.

#define pr_fmt(fmt) "PLDM: " fmt

#include <cpu.h>
#include <stdio.h>
#include <string.h>
#include <timebase.h>
#include <ast.h>
#include <libpldm/utils.h>
#include "pldm.h"

#define TIMEOUT_MS 8000

struct pldm_request {
	struct list_node link;

	/* originating request params */
	struct pldm_header_info hdrinf;

	/* messages requested */
	struct pldm_tx_data *tx;

	/* timeout handling */
	struct timer timeout;
	uint64_t timeout_ms;
	uint64_t start_time;

	/* completion callback */
	void (*complete)(struct pldm_rx_data *rx, void *data);
	void *complete_data;
};

struct pldm_response {
	void **msg;
	size_t *msg_size;
	bool done;
	int rc;
};

/* pldm requests queue */
static struct lock pldm_requests_lock = LOCK_UNLOCKED;
static LIST_HEAD(list_pldm_requests);

static struct pldm_request *active_request;

static bool matches_request(const struct pldm_rx_data *rx,
			    const struct pldm_request *req)
{
	if (req->hdrinf.instance != rx->hdrinf.instance)
		return false;
	if (req->hdrinf.pldm_type != rx->hdrinf.pldm_type)
		return false;
	if (req->hdrinf.command != rx->hdrinf.command)
		return false;

	return true;
}

static void send_and_wait_complete(struct pldm_rx_data *rx, void *data)
{
	struct pldm_response *resp = (struct pldm_response *)data;
	int len;

	if (rx != NULL) {
		len = rx->msg_len;
		*resp->msg_size = len;
		*resp->msg = zalloc(len);
		memcpy(*resp->msg, rx->msg, len);

		resp->rc = OPAL_SUCCESS;
	} else {
		*resp->msg_size = 0;
		*resp->msg = NULL;
		resp->rc = OPAL_TIMEOUT;
	}

	resp->done = true;
}

static void handle_response(struct pldm_rx_data *rx)
{
	uint64_t now;

	if (active_request == NULL) {
		prlog(PR_ERR, "%s: No active request\n", __func__);
		return;
	}

	/* unactivate the timer */
	if (rx != NULL)
		cancel_timer(&active_request->timeout);

	if (active_request->complete)
		active_request->complete(rx, active_request->complete_data);

	now = mftb();
	prlog(PR_TRACE, "%s: Finished after %ldms, t:%d c:%d i:%d\n",
			__func__,
			tb_to_msecs(now - active_request->start_time),
			active_request->hdrinf.pldm_type,
			active_request->hdrinf.command,
			active_request->hdrinf.instance);

	free(active_request->tx);
	free(active_request);
	active_request = NULL;
}

/*
 * Timeout :(
 */
static void expiry(struct timer *t __unused, void *data, uint64_t now __unused)
{
	struct pldm_request *req = (struct pldm_request *)data;

	if (active_request == NULL) {
		prlog(PR_ERR, "request timedout! (active request NULL)\n");
		return;
	}

	prlog(PR_ERR, "PLDM: request timedout! (active request: t:0x%x c:0x%x i:%d)\n",
		      active_request->hdrinf.pldm_type,
		      active_request->hdrinf.command,
		      active_request->hdrinf.instance);

	prlog(PR_ERR, "PLDM: Original request t:0x%x c:0x%x i:%d -----\n",
			req->hdrinf.pldm_type,
			req->hdrinf.command, req->hdrinf.instance);

	/* no data received. Finish the procedure */
	handle_response(NULL);
}

/*
 * Handle PLDM message received from the PLDM terminus over MCTP
 */
int pldm_requester_handle_response(struct pldm_rx_data *rx)
{
	/* check the message received */
	if (active_request == NULL) {
		prlog(PR_ERR, "%s: No active request. "
			      "Response received t:%d c:%d i:%d\n",
			      __func__,
			      rx->hdrinf.pldm_type,
			      rx->hdrinf.command,
			      rx->hdrinf.instance);
		return OPAL_WRONG_STATE;
	}

	if (!matches_request(rx, active_request)) {
		prlog(PR_ERR, "%s: Unexpected response! t:%d c:%d i:%d want %d,%d,%d\n",
			      __func__,
			      rx->hdrinf.pldm_type,
			      rx->hdrinf.command,
			      rx->hdrinf.instance,
			      active_request->hdrinf.pldm_type,
			      active_request->hdrinf.command,
			      active_request->hdrinf.instance);
		return OPAL_WRONG_STATE;
	}

	/* The expected message seems correct */
	handle_response(rx);

	return OPAL_SUCCESS;
}

/*
 * Send the PLDM request
 */
static void requests_poller(void *data __unused)
{
	int rc = OPAL_SUCCESS;

	lock(&pldm_requests_lock);

	/* wait for the end of the processing of the current request */
	if (active_request) {
		unlock(&pldm_requests_lock);
		return;
	}

	/* no new request to handle */
	if (list_empty(&list_pldm_requests)) {
		unlock(&pldm_requests_lock);
		return;
	}

	/* remove the first entry in a list */
	active_request = list_pop(&list_pldm_requests,
				  struct pldm_request,
				  link);

	unlock(&pldm_requests_lock);

	/* Start timer to control a timeout from the PLDM terminus */
	init_timer(&active_request->timeout, expiry, active_request);
	schedule_timer(&active_request->timeout,
		       msecs_to_tb(active_request->timeout_ms));
	active_request->start_time = mftb();

	/* Send PLDM message over MCTP */
	prlog(PR_TRACE, "%s: Sending request to BMC t:%d c:%d i:%d -----\n",
			__func__,
			active_request->hdrinf.pldm_type,
			active_request->hdrinf.command,
			active_request->hdrinf.instance);

	rc = pldm_mctp_message_tx(active_request->tx);
	if (rc)
		prlog(PR_ERR, "%s: Error %d while sending request\n",
		      __func__, rc);
}

/*
 * Add PLDM request in the queue
 */
static int queue_request(struct pldm_tx_data *tx,
			 uint64_t timeout_ms,
			 void (*complete)(struct pldm_rx_data *rx, void *data),
			 void *complete_data)
{
	struct pldm_request *pending;
	struct pldm_msg *pldm_msg;
	size_t tx_size;

	tx_size = sizeof(struct pldm_tx_data) + tx->data_size;

	pending = zalloc(sizeof(struct pldm_request));
	if (!pending) {
		prlog(PR_ERR, "%s: failed to allocate request\n", __func__);
		return OPAL_NO_MEM;
	}

	pending->timeout_ms	= timeout_ms;
	pending->complete	= complete;
	pending->complete_data	= complete_data;
	pending->tx		= zalloc(tx_size);
	if (!pending->tx) {
		free(pending);
		prlog(PR_ERR, "%s: failed to allocate pldm packet (size: 0x%lx)\n",
			      __func__, tx_size);
		return OPAL_NO_MEM;
	}

	memcpy(pending->tx, tx, tx_size);

	pldm_msg = (struct pldm_msg *)tx->data;
	if (unpack_pldm_header(&pldm_msg->hdr, &pending->hdrinf)) {
		free(pending->tx);
		free(pending);
		prlog(PR_ERR, "%s: error parsing pldm header\n", __func__);
		return OPAL_PARAMETER;
	}

	/* add an entry at the end of a linked list */
	prlog(PR_TRACE, "%s: Add request t:%d c:%d i:%d -----\n",
			__func__,
			pending->hdrinf.pldm_type,
			pending->hdrinf.command,
			pending->hdrinf.instance);

	lock(&pldm_requests_lock);
	list_add_tail(&list_pldm_requests, &pending->link);
	unlock(&pldm_requests_lock);

	return OPAL_SUCCESS;
}

/*
 * Queue a PLDM request and don't wait.
 * When a response is received, call the associated callback.
 */
int pldm_requester_queue(struct pldm_tx_data *tx,
			 void (*complete)(struct pldm_rx_data *rx, void *data),
			 void *complete_data)
{
	int rc = OPAL_SUCCESS;

	/* Queue PLDM request */
	rc = queue_request(tx, TIMEOUT_MS, complete, complete_data);
	if (rc) {
		prlog(PR_ERR, "%s: error %d while queuing request\n",
			      __func__, rc);
		return rc;
	}

	return rc;
}

/*
 * Queue a PLDM request and spin until we get a response.
 */
int pldm_requester_queue_and_wait(struct pldm_tx_data *tx,
				  void **msg, size_t *msg_size)
{
	struct pldm_response *resp;
	int rc = OPAL_SUCCESS;

	resp = zalloc(sizeof(struct pldm_response));
	if (!resp) {
		prlog(PR_ERR, "%s: failed to allocate response\n", __func__);
		return OPAL_NO_MEM;
	}

	resp->msg = msg;
	resp->msg_size = msg_size;

	rc = pldm_requester_queue(tx, send_and_wait_complete, resp);
	if (rc)
		goto out;

	/* wait for a response from the BMC */
	for (;;) {
		if (resp->done)
			break;

		time_wait_ms(5);
	}
	rc = resp->rc;

out:
	free(resp);
	return rc;
}

int pldm_requester_init(void)
{
	/* requests poller */
	opal_add_poller(requests_poller, NULL);

	return OPAL_SUCCESS;
}