forked from t2linux/apple-bce-drv
-
Notifications
You must be signed in to change notification settings - Fork 4
/
queue.c
390 lines (355 loc) · 12.4 KB
/
queue.c
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
#include "queue.h"
#include "apple_bce.h"
#define REG_DOORBELL_BASE 0x44000
struct bce_queue_cq *bce_alloc_cq(struct apple_bce_device *dev, int qid, u32 el_count)
{
struct bce_queue_cq *q;
q = kzalloc(sizeof(struct bce_queue_cq), GFP_KERNEL);
q->qid = qid;
q->type = BCE_QUEUE_CQ;
q->el_count = el_count;
q->data = dma_alloc_coherent(&dev->pci->dev, el_count * sizeof(struct bce_qe_completion),
&q->dma_handle, GFP_KERNEL);
if (!q->data) {
pr_err("DMA queue memory alloc failed\n");
kfree(q);
return NULL;
}
return q;
}
void bce_get_cq_memcfg(struct bce_queue_cq *cq, struct bce_queue_memcfg *cfg)
{
cfg->qid = (u16) cq->qid;
cfg->el_count = (u16) cq->el_count;
cfg->vector_or_cq = 0;
cfg->_pad = 0;
cfg->addr = cq->dma_handle;
cfg->length = cq->el_count * sizeof(struct bce_qe_completion);
}
void bce_free_cq(struct apple_bce_device *dev, struct bce_queue_cq *cq)
{
dma_free_coherent(&dev->pci->dev, cq->el_count * sizeof(struct bce_qe_completion), cq->data, cq->dma_handle);
kfree(cq);
}
static void bce_handle_cq_completion(struct apple_bce_device *dev, struct bce_qe_completion *e, size_t *ce)
{
struct bce_queue *target;
struct bce_queue_sq *target_sq;
struct bce_sq_completion_data *cmpl;
if (e->qid >= BCE_MAX_QUEUE_COUNT) {
pr_err("Device sent a response for qid (%u) >= BCE_MAX_QUEUE_COUNT\n", e->qid);
return;
}
target = dev->queues[e->qid];
if (!target || target->type != BCE_QUEUE_SQ) {
pr_err("Device sent a response for qid (%u), which does not exist\n", e->qid);
return;
}
target_sq = (struct bce_queue_sq *) target;
if (target_sq->completion_tail != e->completion_index) {
pr_err("Completion index mismatch; this is likely going to make this driver unusable\n");
return;
}
if (!target_sq->has_pending_completions) {
target_sq->has_pending_completions = true;
dev->int_sq_list[(*ce)++] = target_sq;
}
cmpl = &target_sq->completion_data[e->completion_index];
cmpl->status = e->status;
cmpl->data_size = e->data_size;
cmpl->result = e->result;
wmb();
target_sq->completion_tail = (target_sq->completion_tail + 1) % target_sq->el_count;
}
void bce_handle_cq_completions(struct apple_bce_device *dev, struct bce_queue_cq *cq)
{
size_t ce = 0;
struct bce_qe_completion *e;
struct bce_queue_sq *sq;
e = bce_cq_element(cq, cq->index);
if (!(e->flags & BCE_COMPLETION_FLAG_PENDING))
return;
mb();
while (true) {
e = bce_cq_element(cq, cq->index);
if (!(e->flags & BCE_COMPLETION_FLAG_PENDING))
break;
// pr_info("apple-bce: compl: %i: %i %llx %llx", e->qid, e->status, e->data_size, e->result);
bce_handle_cq_completion(dev, e, &ce);
e->flags = 0;
cq->index = (cq->index + 1) % cq->el_count;
}
mb();
iowrite32(cq->index, (u32 *) ((u8 *) dev->reg_mem_dma + REG_DOORBELL_BASE) + cq->qid);
while (ce) {
--ce;
sq = dev->int_sq_list[ce];
sq->completion(sq);
sq->has_pending_completions = false;
}
}
struct bce_queue_sq *bce_alloc_sq(struct apple_bce_device *dev, int qid, u32 el_size, u32 el_count,
bce_sq_completion compl, void *userdata)
{
struct bce_queue_sq *q;
q = kzalloc(sizeof(struct bce_queue_sq), GFP_KERNEL);
q->qid = qid;
q->type = BCE_QUEUE_SQ;
q->el_size = el_size;
q->el_count = el_count;
q->data = dma_alloc_coherent(&dev->pci->dev, el_count * el_size,
&q->dma_handle, GFP_KERNEL);
q->completion = compl;
q->userdata = userdata;
q->completion_data = kzalloc(sizeof(struct bce_sq_completion_data) * el_count, GFP_KERNEL);
q->reg_mem_dma = dev->reg_mem_dma;
atomic_set(&q->available_commands, el_count - 1);
init_completion(&q->available_command_completion);
atomic_set(&q->available_command_completion_waiting_count, 0);
if (!q->data) {
pr_err("DMA queue memory alloc failed\n");
kfree(q);
return NULL;
}
return q;
}
void bce_get_sq_memcfg(struct bce_queue_sq *sq, struct bce_queue_cq *cq, struct bce_queue_memcfg *cfg)
{
cfg->qid = (u16) sq->qid;
cfg->el_count = (u16) sq->el_count;
cfg->vector_or_cq = (u16) cq->qid;
cfg->_pad = 0;
cfg->addr = sq->dma_handle;
cfg->length = sq->el_count * sq->el_size;
}
void bce_free_sq(struct apple_bce_device *dev, struct bce_queue_sq *sq)
{
dma_free_coherent(&dev->pci->dev, sq->el_count * sq->el_size, sq->data, sq->dma_handle);
kfree(sq);
}
int bce_reserve_submission(struct bce_queue_sq *sq, unsigned long *timeout)
{
while (atomic_dec_if_positive(&sq->available_commands) < 0) {
if (!timeout || !*timeout)
return -EAGAIN;
atomic_inc(&sq->available_command_completion_waiting_count);
*timeout = wait_for_completion_timeout(&sq->available_command_completion, *timeout);
if (!*timeout) {
if (atomic_dec_if_positive(&sq->available_command_completion_waiting_count) < 0)
try_wait_for_completion(&sq->available_command_completion); /* consume the pending completion */
}
}
return 0;
}
void bce_cancel_submission_reservation(struct bce_queue_sq *sq)
{
atomic_inc(&sq->available_commands);
}
void *bce_next_submission(struct bce_queue_sq *sq)
{
void *ret = bce_sq_element(sq, sq->tail);
sq->tail = (sq->tail + 1) % sq->el_count;
return ret;
}
void bce_submit_to_device(struct bce_queue_sq *sq)
{
mb();
iowrite32(sq->tail, (u32 *) ((u8 *) sq->reg_mem_dma + REG_DOORBELL_BASE) + sq->qid);
}
void bce_notify_submission_complete(struct bce_queue_sq *sq)
{
sq->head = (sq->head + 1) % sq->el_count;
atomic_inc(&sq->available_commands);
if (atomic_dec_if_positive(&sq->available_command_completion_waiting_count) >= 0) {
complete(&sq->available_command_completion);
}
}
void bce_set_submission_single(struct bce_qe_submission *element, dma_addr_t addr, size_t size)
{
element->addr = addr;
element->length = size;
element->segl_addr = element->segl_length = 0;
}
static void bce_cmdq_completion(struct bce_queue_sq *q);
struct bce_queue_cmdq *bce_alloc_cmdq(struct apple_bce_device *dev, int qid, u32 el_count)
{
struct bce_queue_cmdq *q;
q = kzalloc(sizeof(struct bce_queue_cmdq), GFP_KERNEL);
q->sq = bce_alloc_sq(dev, qid, BCE_CMD_SIZE, el_count, bce_cmdq_completion, q);
if (!q->sq) {
kfree(q);
return NULL;
}
spin_lock_init(&q->lck);
q->tres = kzalloc(sizeof(struct bce_queue_cmdq_result_el*) * el_count, GFP_KERNEL);
if (!q->tres) {
kfree(q);
return NULL;
}
return q;
}
void bce_free_cmdq(struct apple_bce_device *dev, struct bce_queue_cmdq *cmdq)
{
bce_free_sq(dev, cmdq->sq);
kfree(cmdq->tres);
kfree(cmdq);
}
void bce_cmdq_completion(struct bce_queue_sq *q)
{
struct bce_queue_cmdq_result_el *el;
struct bce_queue_cmdq *cmdq = q->userdata;
struct bce_sq_completion_data *result;
spin_lock(&cmdq->lck);
while ((result = bce_next_completion(q))) {
el = cmdq->tres[cmdq->sq->head];
if (el) {
el->result = result->result;
el->status = result->status;
mb();
complete(&el->cmpl);
} else {
pr_err("apple-bce: Unexpected command queue completion\n");
}
cmdq->tres[cmdq->sq->head] = NULL;
bce_notify_submission_complete(q);
}
spin_unlock(&cmdq->lck);
}
static __always_inline void *bce_cmd_start(struct bce_queue_cmdq *cmdq, struct bce_queue_cmdq_result_el *res)
{
void *ret;
unsigned long timeout;
init_completion(&res->cmpl);
mb();
timeout = msecs_to_jiffies(1000L * 60 * 5); /* wait for up to ~5 minutes */
if (bce_reserve_submission(cmdq->sq, &timeout))
return NULL;
spin_lock(&cmdq->lck);
cmdq->tres[cmdq->sq->tail] = res;
ret = bce_next_submission(cmdq->sq);
return ret;
}
static __always_inline void bce_cmd_finish(struct bce_queue_cmdq *cmdq, struct bce_queue_cmdq_result_el *res)
{
bce_submit_to_device(cmdq->sq);
spin_unlock(&cmdq->lck);
wait_for_completion(&res->cmpl);
mb();
}
u32 bce_cmd_register_queue(struct bce_queue_cmdq *cmdq, struct bce_queue_memcfg *cfg, const char *name, bool isdirout)
{
struct bce_queue_cmdq_result_el res;
struct bce_cmdq_register_memory_queue_cmd *cmd = bce_cmd_start(cmdq, &res);
if (!cmd)
return (u32) -1;
cmd->cmd = BCE_CMD_REGISTER_MEMORY_QUEUE;
cmd->flags = (u16) ((name ? 2 : 0) | (isdirout ? 1 : 0));
cmd->qid = cfg->qid;
cmd->el_count = cfg->el_count;
cmd->vector_or_cq = cfg->vector_or_cq;
memset(cmd->name, 0, sizeof(cmd->name));
if (name) {
cmd->name_len = (u16) min(strlen(name), (size_t) sizeof(cmd->name));
memcpy(cmd->name, name, cmd->name_len);
} else {
cmd->name_len = 0;
}
cmd->addr = cfg->addr;
cmd->length = cfg->length;
bce_cmd_finish(cmdq, &res);
return res.status;
}
u32 bce_cmd_unregister_memory_queue(struct bce_queue_cmdq *cmdq, u16 qid)
{
struct bce_queue_cmdq_result_el res;
struct bce_cmdq_simple_memory_queue_cmd *cmd = bce_cmd_start(cmdq, &res);
if (!cmd)
return (u32) -1;
cmd->cmd = BCE_CMD_UNREGISTER_MEMORY_QUEUE;
cmd->flags = 0;
cmd->qid = qid;
bce_cmd_finish(cmdq, &res);
return res.status;
}
u32 bce_cmd_flush_memory_queue(struct bce_queue_cmdq *cmdq, u16 qid)
{
struct bce_queue_cmdq_result_el res;
struct bce_cmdq_simple_memory_queue_cmd *cmd = bce_cmd_start(cmdq, &res);
if (!cmd)
return (u32) -1;
cmd->cmd = BCE_CMD_FLUSH_MEMORY_QUEUE;
cmd->flags = 0;
cmd->qid = qid;
bce_cmd_finish(cmdq, &res);
return res.status;
}
struct bce_queue_cq *bce_create_cq(struct apple_bce_device *dev, u32 el_count)
{
struct bce_queue_cq *cq;
struct bce_queue_memcfg cfg;
int qid = ida_simple_get(&dev->queue_ida, BCE_QUEUE_USER_MIN, BCE_QUEUE_USER_MAX, GFP_KERNEL);
if (qid < 0)
return NULL;
cq = bce_alloc_cq(dev, qid, el_count);
if (!cq)
return NULL;
bce_get_cq_memcfg(cq, &cfg);
if (bce_cmd_register_queue(dev->cmd_cmdq, &cfg, NULL, false) != 0) {
pr_err("apple-bce: CQ registration failed (%i)", qid);
bce_free_cq(dev, cq);
ida_simple_remove(&dev->queue_ida, (uint) qid);
return NULL;
}
dev->queues[qid] = (struct bce_queue *) cq;
return cq;
}
struct bce_queue_sq *bce_create_sq(struct apple_bce_device *dev, struct bce_queue_cq *cq, const char *name, u32 el_count,
int direction, bce_sq_completion compl, void *userdata)
{
struct bce_queue_sq *sq;
struct bce_queue_memcfg cfg;
int qid;
if (cq == NULL)
return NULL; /* cq can not be null */
if (name == NULL)
return NULL; /* name can not be null */
if (direction != DMA_TO_DEVICE && direction != DMA_FROM_DEVICE)
return NULL; /* unsupported direction */
qid = ida_simple_get(&dev->queue_ida, BCE_QUEUE_USER_MIN, BCE_QUEUE_USER_MAX, GFP_KERNEL);
if (qid < 0)
return NULL;
sq = bce_alloc_sq(dev, qid, sizeof(struct bce_qe_submission), el_count, compl, userdata);
if (!sq)
return NULL;
bce_get_sq_memcfg(sq, cq, &cfg);
if (bce_cmd_register_queue(dev->cmd_cmdq, &cfg, name, direction != DMA_FROM_DEVICE) != 0) {
pr_err("apple-bce: SQ registration failed (%i)", qid);
bce_free_sq(dev, sq);
ida_simple_remove(&dev->queue_ida, (uint) qid);
return NULL;
}
spin_lock(&dev->queues_lock);
dev->queues[qid] = (struct bce_queue *) sq;
spin_unlock(&dev->queues_lock);
return sq;
}
void bce_destroy_cq(struct apple_bce_device *dev, struct bce_queue_cq *cq)
{
if (!dev->is_being_removed && bce_cmd_unregister_memory_queue(dev->cmd_cmdq, (u16) cq->qid))
pr_err("apple-bce: CQ unregister failed");
spin_lock(&dev->queues_lock);
dev->queues[cq->qid] = NULL;
spin_unlock(&dev->queues_lock);
ida_simple_remove(&dev->queue_ida, (uint) cq->qid);
bce_free_cq(dev, cq);
}
void bce_destroy_sq(struct apple_bce_device *dev, struct bce_queue_sq *sq)
{
if (!dev->is_being_removed && bce_cmd_unregister_memory_queue(dev->cmd_cmdq, (u16) sq->qid))
pr_err("apple-bce: CQ unregister failed");
spin_lock(&dev->queues_lock);
dev->queues[sq->qid] = NULL;
spin_unlock(&dev->queues_lock);
ida_simple_remove(&dev->queue_ida, (uint) sq->qid);
bce_free_sq(dev, sq);
}