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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/*
* Copyright (c) 2026 Abdelkader Boudih <freebsd@seuros.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Apple BCE queue management -- CQ, SQ, and command queue operations.
*/
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/sema.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <machine/atomic.h>
#include "apple_bce.h"
#include "apple_bce_queue.h"
static MALLOC_DEFINE(M_BCE, "apple_bce", "Apple BCE driver");
#define BCE_CMDQ_TIMEOUT_MS 5000
/*
* DMA callback for bus_dmamap_load.
*/
struct bce_dma_cb_arg {
bus_addr_t addr;
int error;
};
static int
bce_cmdq_wait(struct bce_queue_cmdq *cmdq, uint32_t slot,
struct bce_queue_cmdq_result *res)
{
int error;
error = sema_timedwait(&res->cmpl, hz * BCE_CMDQ_TIMEOUT_MS / 1000);
if (error == 0)
return (0);
mtx_lock(&cmdq->lck);
if (cmdq->tres[slot] == res)
cmdq->tres[slot] = NULL;
mtx_unlock(&cmdq->lck);
return (ETIMEDOUT);
}
static void
bce_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
struct bce_dma_cb_arg *cb = arg;
cb->error = error;
if (error == 0)
cb->addr = segs[0].ds_addr;
}
/*
* Allocate a completion queue with DMA-coherent memory.
*/
struct bce_queue_cq *
bce_alloc_cq(struct apple_bce_softc *sc, int qid, uint32_t el_count)
{
struct bce_queue_cq *cq;
struct bce_dma_cb_arg cb;
int error;
cq = malloc(sizeof(*cq), M_BCE, M_WAITOK | M_ZERO);
cq->qid = qid;
cq->el_count = el_count;
cq->index = 0;
error = bus_dma_tag_create(sc->sc_dma_tag,
4, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter */
el_count * sizeof(struct bce_qe_completion),
1, /* nsegments */
el_count * sizeof(struct bce_qe_completion),
BUS_DMA_WAITOK, /* flags */
NULL, NULL, /* lockfunc */
&cq->dma_tag);
if (error) {
free(cq, M_BCE);
return (NULL);
}
error = bus_dmamem_alloc(cq->dma_tag, (void **)&cq->data,
BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT, &cq->dma_map);
if (error) {
bus_dma_tag_destroy(cq->dma_tag);
free(cq, M_BCE);
return (NULL);
}
error = bus_dmamap_load(cq->dma_tag, cq->dma_map, cq->data,
el_count * sizeof(struct bce_qe_completion),
bce_dma_cb, &cb, BUS_DMA_WAITOK);
if (error || cb.error) {
bus_dmamem_free(cq->dma_tag, cq->data, cq->dma_map);
bus_dma_tag_destroy(cq->dma_tag);
free(cq, M_BCE);
return (NULL);
}
cq->dma_addr = cb.addr;
return (cq);
}
void
bce_free_cq(struct apple_bce_softc *sc, struct bce_queue_cq *cq)
{
if (cq == NULL)
return;
bus_dmamap_unload(cq->dma_tag, cq->dma_map);
bus_dmamem_free(cq->dma_tag, cq->data, cq->dma_map);
bus_dma_tag_destroy(cq->dma_tag);
free(cq, M_BCE);
}
void
bce_get_cq_memcfg(struct bce_queue_cq *cq, struct bce_queue_memcfg *cfg)
{
cfg->qid = (uint16_t)cq->qid;
cfg->el_count = (uint16_t)cq->el_count;
cfg->vector_or_cq = 0;
cfg->_pad = 0;
cfg->addr = cq->dma_addr;
cfg->length = cq->el_count * sizeof(struct bce_qe_completion);
}
/*
* Process completions from a CQ and dispatch to the target SQs.
*/
void
bce_handle_cq_completions(struct apple_bce_softc *sc, struct bce_queue_cq *cq)
{
struct bce_qe_completion *e;
struct bce_queue_sq *sq;
int i;
bus_dmamap_sync(cq->dma_tag, cq->dma_map, BUS_DMASYNC_POSTREAD);
e = bce_cq_element(cq, cq->index);
if (!(e->flags & BCE_CQ_FLAG_PENDING))
return;
while ((e = bce_cq_element(cq, cq->index))->flags &
BCE_CQ_FLAG_PENDING) {
/* Route completion to target SQ (skip qid 0 which is the CQ) */
if (e->qid > 0 && e->qid < BCE_MAX_QUEUE_COUNT) {
sq = (struct bce_queue_sq *)sc->sc_queues[e->qid];
if (sq != NULL &&
e->completion_index < sq->el_count) {
sq->completion_data[e->completion_index].status =
e->status;
sq->completion_data[e->completion_index].data_size =
e->data_size;
sq->completion_data[e->completion_index].result =
e->result;
/* Advance tail so completion callback sees it */
sq->completion_tail =
(e->completion_index + 1) % sq->el_count;
sq->has_pending = 1;
}
}
e->flags = 0;
cq->index = (cq->index + 1) % cq->el_count;
}
bus_dmamap_sync(cq->dma_tag, cq->dma_map, BUS_DMASYNC_PREWRITE);
/* Ring doorbell with updated consumer index */
bus_write_4(sc->sc_bar2, BCE_REG_DOORBELL_BASE + cq->qid * 4,
cq->index);
/* Fire callbacks on SQs that received completions */
for (i = 0; i < BCE_MAX_QUEUE_COUNT; i++) {
sq = sc->sc_int_sq_list[i];
if (sq != NULL && sq->has_pending) {
sq->has_pending = 0;
if (sq->completion != NULL)
sq->completion(sq);
}
}
}
/*
* Allocate a submission queue with DMA-coherent memory.
*/
struct bce_queue_sq *
bce_alloc_sq(struct apple_bce_softc *sc, int qid, uint32_t el_size,
uint32_t el_count, bce_sq_completion_fn compl, void *userdata)
{
struct bce_queue_sq *sq;
struct bce_dma_cb_arg cb;
int error;
sq = malloc(sizeof(*sq), M_BCE, M_WAITOK | M_ZERO);
sq->qid = qid;
sq->el_size = el_size;
sq->el_count = el_count;
sq->completion = compl;
sq->userdata = userdata;
sq->available_commands = el_count - 1;
error = bus_dma_tag_create(sc->sc_dma_tag,
4, 0,
BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
NULL, NULL,
el_count * el_size, 1, el_count * el_size,
BUS_DMA_WAITOK,
NULL, NULL,
&sq->dma_tag);
if (error) {
free(sq, M_BCE);
return (NULL);
}
error = bus_dmamem_alloc(sq->dma_tag, &sq->data,
BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT, &sq->dma_map);
if (error) {
bus_dma_tag_destroy(sq->dma_tag);
free(sq, M_BCE);
return (NULL);
}
error = bus_dmamap_load(sq->dma_tag, sq->dma_map, sq->data,
el_count * el_size, bce_dma_cb, &cb, BUS_DMA_WAITOK);
if (error || cb.error) {
bus_dmamem_free(sq->dma_tag, sq->data, sq->dma_map);
bus_dma_tag_destroy(sq->dma_tag);
free(sq, M_BCE);
return (NULL);
}
sq->dma_addr = cb.addr;
sq->completion_data = malloc(sizeof(*sq->completion_data) * el_count,
M_BCE, M_WAITOK | M_ZERO);
return (sq);
}
void
bce_free_sq(struct apple_bce_softc *sc, struct bce_queue_sq *sq)
{
if (sq == NULL)
return;
free(sq->completion_data, M_BCE);
bus_dmamap_unload(sq->dma_tag, sq->dma_map);
bus_dmamem_free(sq->dma_tag, sq->data, sq->dma_map);
bus_dma_tag_destroy(sq->dma_tag);
free(sq, M_BCE);
}
void
bce_get_sq_memcfg(struct bce_queue_sq *sq, struct bce_queue_cq *cq,
struct bce_queue_memcfg *cfg)
{
cfg->qid = (uint16_t)sq->qid;
cfg->el_count = (uint16_t)sq->el_count;
cfg->vector_or_cq = (uint16_t)cq->qid;
cfg->_pad = 0;
cfg->addr = sq->dma_addr;
cfg->length = sq->el_count * sq->el_size;
}
int
bce_reserve_submission(struct bce_queue_sq *sq)
{
int old, new;
do {
old = atomic_load_int(&sq->available_commands);
if (old <= 0)
return (EAGAIN);
new = old - 1;
} while (!atomic_cmpset_int(&sq->available_commands, old, new));
return (0);
}
void *
bce_next_submission(struct bce_queue_sq *sq)
{
void *ret;
ret = bce_sq_element(sq, sq->tail);
sq->tail = (sq->tail + 1) % sq->el_count;
return (ret);
}
void
bce_submit_to_device(struct apple_bce_softc *sc, struct bce_queue_sq *sq)
{
bus_dmamap_sync(sq->dma_tag, sq->dma_map, BUS_DMASYNC_PREWRITE);
bus_write_4(sc->sc_bar2, BCE_REG_DOORBELL_BASE + sq->qid * 4,
sq->tail);
}
void
bce_notify_submission_complete(struct bce_queue_sq *sq)
{
sq->head = (sq->head + 1) % sq->el_count;
atomic_add_int(&sq->available_commands, 1);
}
/*
* Command queue -- wraps an SQ for synchronous control operations.
*/
static void bce_cmdq_completion(struct bce_queue_sq *sq);
struct bce_queue_cmdq *
bce_alloc_cmdq(struct apple_bce_softc *sc, struct bce_queue_sq *sq)
{
struct bce_queue_cmdq *cmdq;
cmdq = malloc(sizeof(*cmdq), M_BCE, M_WAITOK | M_ZERO);
cmdq->sq = sq;
mtx_init(&cmdq->lck, "bce_cmdq", NULL, MTX_DEF);
cmdq->tres = malloc(sizeof(void *) * sq->el_count, M_BCE,
M_WAITOK | M_ZERO);
/* Wire up completion callback */
sq->completion = bce_cmdq_completion;
sq->userdata = cmdq;
return (cmdq);
}
void
bce_free_cmdq(struct bce_queue_cmdq *cmdq)
{
if (cmdq == NULL)
return;
mtx_destroy(&cmdq->lck);
free(cmdq->tres, M_BCE);
free(cmdq, M_BCE);
}
/*
* Command queue completion callback -- wake waiters.
*/
static void
bce_cmdq_completion(struct bce_queue_sq *sq)
{
struct bce_queue_cmdq *cmdq = sq->userdata;
struct bce_queue_cmdq_result *res;
mtx_lock(&cmdq->lck);
while (sq->completion_cidx != sq->completion_tail) {
struct bce_sq_completion_data *cd;
cd = &sq->completion_data[sq->completion_cidx];
res = cmdq->tres[sq->completion_cidx];
if (res != NULL) {
res->status = cd->status;
res->result = cd->result;
sema_post(&res->cmpl);
cmdq->tres[sq->completion_cidx] = NULL;
}
sq->completion_cidx = (sq->completion_cidx + 1) %
sq->el_count;
bce_notify_submission_complete(sq);
}
mtx_unlock(&cmdq->lck);
}
uint32_t
bce_cmd_register_queue(struct bce_queue_cmdq *cmdq,
struct apple_bce_softc *sc, struct bce_queue_memcfg *cfg,
const char *name, int isdirout)
{
struct bce_queue_cmdq_result res;
struct bce_cmdq_reg_cmd *cmd;
uint32_t slot;
int error;
sema_init(&res.cmpl, 0, "bce_cmd");
if (bce_reserve_submission(cmdq->sq) != 0) {
sema_destroy(&res.cmpl);
return (EAGAIN);
}
mtx_lock(&cmdq->lck);
slot = cmdq->sq->tail;
cmdq->tres[slot] = &res;
cmd = bce_next_submission(cmdq->sq);
memset(cmd, 0, BCE_CMD_SIZE);
cmd->cmd = BCE_CMD_REGISTER_QUEUE;
cmd->flags = (name ? BCE_CMDQ_FLAG_NAMED : 0) |
(isdirout ? BCE_CMDQ_FLAG_OUT : 0);
cmd->qid = cfg->qid;
cmd->el_count = cfg->el_count;
cmd->vector_or_cq = cfg->vector_or_cq;
if (name != NULL) {
cmd->name_len = (uint16_t)MIN(strlen(name), sizeof(cmd->name));
memcpy(cmd->name, name, cmd->name_len);
}
cmd->addr = cfg->addr;
cmd->length = cfg->length;
bce_submit_to_device(sc, cmdq->sq);
mtx_unlock(&cmdq->lck);
error = bce_cmdq_wait(cmdq, slot, &res);
sema_destroy(&res.cmpl);
if (error != 0)
return (error);
return (res.status);
}
uint32_t
bce_cmd_unregister_queue(struct bce_queue_cmdq *cmdq,
struct apple_bce_softc *sc, int qid)
{
struct bce_queue_cmdq_result res;
struct bce_cmdq_simple_cmd *cmd;
uint32_t slot;
int error;
sema_init(&res.cmpl, 0, "bce_cmd");
if (bce_reserve_submission(cmdq->sq) != 0) {
sema_destroy(&res.cmpl);
return (EAGAIN);
}
mtx_lock(&cmdq->lck);
slot = cmdq->sq->tail;
cmdq->tres[slot] = &res;
cmd = bce_next_submission(cmdq->sq);
memset(cmd, 0, BCE_CMD_SIZE);
cmd->cmd = BCE_CMD_UNREGISTER_QUEUE;
cmd->qid = (uint16_t)qid;
bce_submit_to_device(sc, cmdq->sq);
mtx_unlock(&cmdq->lck);
error = bce_cmdq_wait(cmdq, slot, &res);
sema_destroy(&res.cmpl);
if (error != 0)
return (error);
return (res.status);
}
uint32_t
bce_cmd_flush_queue(struct bce_queue_cmdq *cmdq,
struct apple_bce_softc *sc, int qid)
{
struct bce_queue_cmdq_result res;
struct bce_cmdq_simple_cmd *cmd;
uint32_t slot;
int error;
sema_init(&res.cmpl, 0, "bce_cmd");
if (bce_reserve_submission(cmdq->sq) != 0) {
sema_destroy(&res.cmpl);
return (EAGAIN);
}
mtx_lock(&cmdq->lck);
slot = cmdq->sq->tail;
cmdq->tres[slot] = &res;
cmd = bce_next_submission(cmdq->sq);
memset(cmd, 0, BCE_CMD_SIZE);
cmd->cmd = BCE_CMD_FLUSH_QUEUE;
cmd->qid = (uint16_t)qid;
bce_submit_to_device(sc, cmdq->sq);
mtx_unlock(&cmdq->lck);
error = bce_cmdq_wait(cmdq, slot, &res);
sema_destroy(&res.cmpl);
if (error != 0)
return (error);
return (res.status);
}
|