summaryrefslogtreecommitdiff
path: root/mac/fproc_multi.c
blob: 2c7b2fabac61f037dec76cfd5c43451c5217a91d (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * This file is part of the UWB stack for linux.
 *
 * Copyright (c) 2020-2021 Qorvo US, Inc.
 *
 * This software is provided under the GNU General Public License, version 2
 * (GPLv2), as well as under a Qorvo commercial license.
 *
 * You may choose to use this software under the terms of the GPLv2 License,
 * version 2 ("GPLv2"), as published by the Free Software Foundation.
 * You should have received a copy of the GPLv2 along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * This program is distributed under the GPLv2 in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GPLv2 for more
 * details.
 *
 * If you cannot meet the requirements of the GPLv2, you may not use this
 * software for any purpose without first obtaining a commercial license from
 * Qorvo. Please contact Qorvo to inquire about licensing terms.
 */

#include <linux/errno.h>

#include "mcps802154_fproc.h"
#include "mcps802154_i.h"
#include "llhw-ops.h"

static int mcps802154_fproc_multi_handle_frame(struct mcps802154_local *local,
					       struct mcps802154_access *access,
					       size_t frame_idx);

static int
mcps802154_fproc_multi_restore_filter(struct mcps802154_local *local,
				      struct mcps802154_access *access)
{
	int r = 0;

	if (access->promiscuous) {
		r = llhw_set_promiscuous_mode(local,
					      local->pib.mac_promiscuous);
	} else if (access->hw_addr_filt_changed) {
		struct ieee802154_hw_addr_filt hw_addr_filt;

		hw_addr_filt.pan_id = local->pib.mac_pan_id;
		hw_addr_filt.short_addr = local->pib.mac_short_addr;
		hw_addr_filt.ieee_addr = local->pib.mac_extended_addr;
		hw_addr_filt.pan_coord = local->mac_pan_coord;
		r = llhw_set_hw_addr_filt(local, &hw_addr_filt,
					  access->hw_addr_filt_changed);
	}

	return r;
}

static int mcps802154_fproc_multi_restore(struct mcps802154_local *local,
					  struct mcps802154_access *access)
{
	if (access->channel) {
		int r;
		const struct mcps802154_channel *channel =
			&local->pib.phy_current_channel;

		r = llhw_set_channel(local, channel->page, channel->channel,
				     channel->preamble_code);
		if (r)
			return r;
	}

	return mcps802154_fproc_multi_restore_filter(local, access);
}

/**
 * mcps802154_fproc_multi_check_frames() - Check absence of Rx without timeout.
 * @local: MCPS private data.
 * @access: Current access to handle.
 * @frame_idx: Start frame index in the frames array.
 *
 * Returns: 0 on success, -errno otherwise.
 */
static int
mcps802154_fproc_multi_check_frames(struct mcps802154_local *local,
				    const struct mcps802154_access *access,
				    int frame_idx)
{
	if (access->n_frames && !access->frames)
		return -EINVAL;
	for (; frame_idx < access->n_frames; frame_idx++) {
		const struct mcps802154_access_frame *frame =
			&access->frames[frame_idx];
		/* Only first Rx can be without timeout. */
		if (!frame->is_tx && frame->rx.frame_config.timeout_dtu == -1)
			return -EINVAL;
	}
	return 0;
}

/**
 * mcps802154_fproc_multi_next() - Continue with the next frame, or next
 * access.
 * @local: MCPS private data.
 * @access: Current access to handle.
 * @frame_idx: Frame index in current access, must be valid, will be
 *             incremented.
 */
static void mcps802154_fproc_multi_next(struct mcps802154_local *local,
					struct mcps802154_access *access,
					size_t frame_idx)
{
	frame_idx++;
	if (access->ops->access_extend && frame_idx == access->n_frames) {
		frame_idx = 0;
		access->n_frames = 0;
		access->ops->access_extend(access);
		access->error = mcps802154_fproc_multi_check_frames(local, access, 0);
		if (access->error) {
			if (mcps802154_fproc_is_non_recoverable_error(access)) {
				mcps802154_fproc_access_done(local, true);
				mcps802154_fproc_broken_handle(local);
				return;
			}
		}
	}
	if (frame_idx < access->n_frames) {
		/* Next frame. */
		access->error = mcps802154_fproc_multi_handle_frame(local, access,
							frame_idx);
		if (access->error) {
			if (mcps802154_fproc_is_non_recoverable_error(access)) {
				mcps802154_fproc_access_done(local, true);
				mcps802154_fproc_broken_handle(local);
			} else {
				mcps802154_fproc_access_done(local, false);
				mcps802154_fproc_access_now(local);
			}
		}
	} else {
		access->error = mcps802154_fproc_multi_restore(local, access);
		mcps802154_fproc_access_done(local, !!access->error);
		if (access->error) {
			if (mcps802154_fproc_is_non_recoverable_error(access)) {
				mcps802154_fproc_broken_handle(local);
				return;
			}
		}
		/* Next access. */
		if (access->duration_dtu) {
			u32 next_access_dtu =
				access->timestamp_dtu + access->duration_dtu;
			mcps802154_fproc_access(local, next_access_dtu);
		} else {
			mcps802154_fproc_access_now(local);
		}
	}
}

static void mcps802154_fproc_multi_rx_rx_frame(struct mcps802154_local *local)
{
	struct mcps802154_access *access = local->fproc.access;
	size_t frame_idx = local->fproc.frame_idx;
	struct mcps802154_access_frame *frame = &access->frames[frame_idx];

	/* Read frame. */
	struct sk_buff *skb = NULL;
	struct mcps802154_rx_frame_info info = {
		.flags = frame->rx.frame_info_flags_request,
	};
	access->error =  llhw_rx_get_frame(local, &skb, &info);
	if (!access->error)
		access->ops->rx_frame(access, frame_idx, skb, &info,
				      MCPS802154_RX_ERROR_NONE);

	if (access->error) {
		if (mcps802154_fproc_is_non_recoverable_error(access)) {
			mcps802154_fproc_access_done(local, true);
			mcps802154_fproc_broken_handle(local);
			return;
		}
	}
	mcps802154_fproc_multi_next(local, access, frame_idx);
}

static void mcps802154_fproc_multi_rx_rx_timeout(struct mcps802154_local *local)
{
	struct mcps802154_access *access = local->fproc.access;
	size_t frame_idx = local->fproc.frame_idx;

	access->ops->rx_frame(access, frame_idx, NULL, NULL,
			      MCPS802154_RX_ERROR_TIMEOUT);

	/* Next. */
	mcps802154_fproc_multi_next(local, access, frame_idx);
}

static void
mcps802154_fproc_multi_rx_rx_error(struct mcps802154_local *local,
				   enum mcps802154_rx_error_type error)
{
	struct mcps802154_access *access = local->fproc.access;
	size_t frame_idx = local->fproc.frame_idx;
	struct mcps802154_rx_frame_info info = {
		.flags = MCPS802154_RX_FRAME_INFO_TIMESTAMP_DTU,
	};

	llhw_rx_get_error_frame(local, &info);
	access->ops->rx_frame(access, frame_idx, NULL, &info, error);

	/* Next. */
	mcps802154_fproc_multi_next(local, access, frame_idx);
}

static void
mcps802154_fproc_multi_rx_schedule_change(struct mcps802154_local *local)
{
	/* If the Rx is done without a timeout, disable RX and change the access. */
	struct mcps802154_access *access = local->fproc.access;
	int frame_idx = local->fproc.frame_idx;
	struct mcps802154_access_frame *frame = &access->frames[frame_idx];

	if (frame->rx.frame_config.timeout_dtu == -1) {
		/* Disable RX. */
		access->error = llhw_rx_disable(local);
		if (access->error == -EBUSY)
			/* Wait for RX result. */
			return;

		access->ops->rx_frame(access, frame_idx, NULL, NULL,
				      MCPS802154_RX_ERROR_TIMEOUT);
		if (access->error) {
			if (mcps802154_fproc_is_non_recoverable_error(access)) {
				mcps802154_fproc_access_done(local, true);
				mcps802154_fproc_broken_handle(local);
				return;
			}
		}
		/* Next. */
		mcps802154_fproc_multi_next(local, access, frame_idx);
	}
}

static void mcps802154_fproc_multi_rx_too_late(struct mcps802154_local *local)
{
	struct mcps802154_access *access = local->fproc.access;
	size_t frame_idx = local->fproc.frame_idx;

	access->ops->rx_frame(access, frame_idx, NULL, NULL,
			      MCPS802154_RX_ERROR_HPDWARN);

	/* Next. */
	mcps802154_fproc_multi_next(local, access, frame_idx);
}

static const struct mcps802154_fproc_state mcps802154_fproc_multi_rx = {
	.name = "multi_rx",
	.rx_frame = mcps802154_fproc_multi_rx_rx_frame,
	.rx_timeout = mcps802154_fproc_multi_rx_rx_timeout,
	.rx_error = mcps802154_fproc_multi_rx_rx_error,
	.rx_too_late = mcps802154_fproc_multi_rx_too_late,
	.schedule_change = mcps802154_fproc_multi_rx_schedule_change,
};

static void mcps802154_fproc_multi_tx_tx_done(struct mcps802154_local *local)
{
	struct mcps802154_access *access = local->fproc.access;
	size_t frame_idx = local->fproc.frame_idx;

	access->ops->tx_return(access, frame_idx, local->fproc.tx_skb,
			       MCPS802154_ACCESS_TX_RETURN_REASON_CONSUMED);
	local->fproc.tx_skb = NULL;

	/* Next. */
	mcps802154_fproc_multi_next(local, access, frame_idx);
}

static void mcps802154_fproc_multi_tx_too_late(struct mcps802154_local *local)
{
	struct mcps802154_access *access = local->fproc.access;
	size_t frame_idx = local->fproc.frame_idx;

	access->ops->tx_return(access, frame_idx, local->fproc.tx_skb,
			       MCPS802154_TX_ERROR_HPDWARN);
	local->fproc.tx_skb = NULL;

	/* Next. */
	mcps802154_fproc_multi_next(local, access, frame_idx);
}

static void
mcps802154_fproc_multi_tx_schedule_change(struct mcps802154_local *local)
{
	/* Wait for end of current frame. */
}

static const struct mcps802154_fproc_state mcps802154_fproc_multi_tx = {
	.name = "multi_tx",
	.tx_done = mcps802154_fproc_multi_tx_tx_done,
	.tx_too_late = mcps802154_fproc_multi_tx_too_late,
	.schedule_change = mcps802154_fproc_multi_tx_schedule_change,
};

/**
 * mcps802154_fproc_multi_handle_frame() - Handle a single frame and change
 * state.
 * @local: MCPS private data.
 * @access: Current access to handle.
 * @frame_idx: Frame index in current access, must be valid.
 *
 * Return: 0 or error.
 */
static int mcps802154_fproc_multi_handle_frame(struct mcps802154_local *local,
					       struct mcps802154_access *access,
					       size_t frame_idx)
{
	struct mcps802154_access_frame *frame;
	struct sk_buff *skb;
	int r;

	local->fproc.frame_idx = frame_idx;

	frame = &access->frames[frame_idx];
	if (!frame->is_tx) {
		if (frame->rx.frame_config.flags &
		    MCPS802154_RX_FRAME_CONFIG_AACK)
			return -EINVAL;

		if (frame->sts_params) {
			r = llhw_set_sts_params(local, frame->sts_params);
			if (r)
				return r;
		}

		r = llhw_rx_enable(local, &frame->rx.frame_config, frame_idx,
				   0);
		if (r)
			return r;

		mcps802154_fproc_change_state(local,
					      &mcps802154_fproc_multi_rx);
	} else {
		if (frame->tx_frame_config.rx_enable_after_tx_dtu)
			return -EINVAL;

		skb = access->ops->tx_get_frame(access, frame_idx);

		/* TODO: prepare next RX directly. */

		if (frame->sts_params) {
			r = llhw_set_sts_params(local, frame->sts_params);
			if (r) {
				access->ops->tx_return(
					access, frame_idx, skb,
					MCPS802154_ACCESS_TX_RETURN_REASON_CANCEL);
				return r;
			}
		}

		r = llhw_tx_frame(local, skb, &frame->tx_frame_config,
				  frame_idx, 0);
		if (r) {
			access->ops->tx_return(
				access, frame_idx, skb,
				MCPS802154_ACCESS_TX_RETURN_REASON_CANCEL);
			return r;
		}

		local->fproc.tx_skb = skb;
		mcps802154_ca_access_hold(local);
		mcps802154_fproc_change_state(local,
					      &mcps802154_fproc_multi_tx);
	}

	return 0;
}

int mcps802154_fproc_multi_handle(struct mcps802154_local *local,
				  struct mcps802154_access *access)
{
	int r;

	if (access->n_frames == 0)
		return -EINVAL;
	r = mcps802154_fproc_multi_check_frames(local, access, 1);
	if (r)
		return r;

	if (access->promiscuous) {
		r = llhw_set_promiscuous_mode(local, true);
		if (r)
			return r;
	} else if (access->hw_addr_filt_changed) {
		r = llhw_set_hw_addr_filt(local, &access->hw_addr_filt,
					  access->hw_addr_filt_changed);
		if (r)
			return r;
	}

	if (access->channel) {
		r = llhw_set_channel(local, access->channel->page,
				     access->channel->channel,
				     access->channel->preamble_code);
		if (r) {
			mcps802154_fproc_multi_restore_filter(local, access);
			return r;
		}
	}

	if (access->hrp_uwb_params) {
		r = llhw_set_hrp_uwb_params(local, access->hrp_uwb_params);
		if (r)
			return r;
	}

	return mcps802154_fproc_multi_handle_frame(local, access, 0);
}