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#include "qman_test.h"
32
33#include <linux/dma-mapping.h>
34#include <linux/delay.h>
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
85struct bstrap {
86 int (*fn)(void);
87 atomic_t started;
88};
89static int bstrap_fn(void *bs)
90{
91 struct bstrap *bstrap = bs;
92 int err;
93
94 atomic_inc(&bstrap->started);
95 err = bstrap->fn();
96 if (err)
97 return err;
98 while (!kthread_should_stop())
99 msleep(20);
100 return 0;
101}
102static int on_all_cpus(int (*fn)(void))
103{
104 int cpu;
105
106 for_each_cpu(cpu, cpu_online_mask) {
107 struct bstrap bstrap = {
108 .fn = fn,
109 .started = ATOMIC_INIT(0)
110 };
111 struct task_struct *k = kthread_create(bstrap_fn, &bstrap,
112 "hotpotato%d", cpu);
113 int ret;
114
115 if (IS_ERR(k))
116 return -ENOMEM;
117 kthread_bind(k, cpu);
118 wake_up_process(k);
119
120
121
122
123
124
125 while (!atomic_read(&bstrap.started))
126 msleep(20);
127 ret = kthread_stop(k);
128 if (ret)
129 return ret;
130 }
131 return 0;
132}
133
134struct hp_handler {
135
136
137
138
139 struct qman_fq rx;
140
141 struct qman_fq tx;
142
143 u32 rx_mixer;
144
145 u32 tx_mixer;
146
147 dma_addr_t addr;
148 u32 *frame_ptr;
149
150
151
152 u32 fqid_rx, fqid_tx;
153
154 struct list_head node;
155
156 unsigned int processor_id;
157} ____cacheline_aligned;
158
159struct hp_cpu {
160
161 unsigned int processor_id;
162
163 struct list_head handlers;
164
165 struct list_head node;
166
167
168
169
170 struct hp_handler *iterator;
171};
172
173
174static DEFINE_PER_CPU(struct hp_cpu, hp_cpus);
175
176
177static LIST_HEAD(hp_cpu_list);
178static DEFINE_SPINLOCK(hp_lock);
179
180static unsigned int hp_cpu_list_length;
181
182
183static struct hp_handler *special_handler;
184static int loop_counter;
185
186
187static struct kmem_cache *hp_handler_slab;
188
189
190static void *__frame_ptr;
191static u32 *frame_ptr;
192static dma_addr_t frame_dma;
193
194
195static const struct qm_portal_config *pcfg;
196
197
198static DECLARE_WAIT_QUEUE_HEAD(queue);
199
200#define HP_PER_CPU 2
201#define HP_LOOPS 8
202
203#define HP_NUM_WORDS 80
204
205#define HP_FIRST_WORD 0xabbaf00d
206
207static inline u32 do_lfsr(u32 prev)
208{
209 return (prev >> 1) ^ (-(prev & 1u) & 0xd0000001u);
210}
211
212static int allocate_frame_data(void)
213{
214 u32 lfsr = HP_FIRST_WORD;
215 int loop;
216
217 if (!qman_dma_portal) {
218 pr_crit("portal not available\n");
219 return -EIO;
220 }
221
222 pcfg = qman_get_qm_portal_config(qman_dma_portal);
223
224 __frame_ptr = kmalloc(4 * HP_NUM_WORDS, GFP_KERNEL);
225 if (!__frame_ptr)
226 return -ENOMEM;
227
228 frame_ptr = PTR_ALIGN(__frame_ptr, 64);
229 for (loop = 0; loop < HP_NUM_WORDS; loop++) {
230 frame_ptr[loop] = lfsr;
231 lfsr = do_lfsr(lfsr);
232 }
233
234 frame_dma = dma_map_single(pcfg->dev, frame_ptr, 4 * HP_NUM_WORDS,
235 DMA_BIDIRECTIONAL);
236 if (dma_mapping_error(pcfg->dev, frame_dma)) {
237 pr_crit("dma mapping failure\n");
238 kfree(__frame_ptr);
239 return -EIO;
240 }
241
242 return 0;
243}
244
245static void deallocate_frame_data(void)
246{
247 dma_unmap_single(pcfg->dev, frame_dma, 4 * HP_NUM_WORDS,
248 DMA_BIDIRECTIONAL);
249 kfree(__frame_ptr);
250}
251
252static inline int process_frame_data(struct hp_handler *handler,
253 const struct qm_fd *fd)
254{
255 u32 *p = handler->frame_ptr;
256 u32 lfsr = HP_FIRST_WORD;
257 int loop;
258
259 if (qm_fd_addr_get64(fd) != handler->addr) {
260 pr_crit("bad frame address, [%llX != %llX]\n",
261 qm_fd_addr_get64(fd), handler->addr);
262 return -EIO;
263 }
264 for (loop = 0; loop < HP_NUM_WORDS; loop++, p++) {
265 *p ^= handler->rx_mixer;
266 if (*p != lfsr) {
267 pr_crit("corrupt frame data");
268 return -EIO;
269 }
270 *p ^= handler->tx_mixer;
271 lfsr = do_lfsr(lfsr);
272 }
273 return 0;
274}
275
276static enum qman_cb_dqrr_result normal_dqrr(struct qman_portal *portal,
277 struct qman_fq *fq,
278 const struct qm_dqrr_entry *dqrr)
279{
280 struct hp_handler *handler = (struct hp_handler *)fq;
281
282 if (process_frame_data(handler, &dqrr->fd)) {
283 WARN_ON(1);
284 goto skip;
285 }
286 if (qman_enqueue(&handler->tx, &dqrr->fd)) {
287 pr_crit("qman_enqueue() failed");
288 WARN_ON(1);
289 }
290skip:
291 return qman_cb_dqrr_consume;
292}
293
294static enum qman_cb_dqrr_result special_dqrr(struct qman_portal *portal,
295 struct qman_fq *fq,
296 const struct qm_dqrr_entry *dqrr)
297{
298 struct hp_handler *handler = (struct hp_handler *)fq;
299
300 process_frame_data(handler, &dqrr->fd);
301 if (++loop_counter < HP_LOOPS) {
302 if (qman_enqueue(&handler->tx, &dqrr->fd)) {
303 pr_crit("qman_enqueue() failed");
304 WARN_ON(1);
305 goto skip;
306 }
307 } else {
308 pr_info("Received final (%dth) frame\n", loop_counter);
309 wake_up(&queue);
310 }
311skip:
312 return qman_cb_dqrr_consume;
313}
314
315static int create_per_cpu_handlers(void)
316{
317 struct hp_handler *handler;
318 int loop;
319 struct hp_cpu *hp_cpu = this_cpu_ptr(&hp_cpus);
320
321 hp_cpu->processor_id = smp_processor_id();
322 spin_lock(&hp_lock);
323 list_add_tail(&hp_cpu->node, &hp_cpu_list);
324 hp_cpu_list_length++;
325 spin_unlock(&hp_lock);
326 INIT_LIST_HEAD(&hp_cpu->handlers);
327 for (loop = 0; loop < HP_PER_CPU; loop++) {
328 handler = kmem_cache_alloc(hp_handler_slab, GFP_KERNEL);
329 if (!handler) {
330 pr_crit("kmem_cache_alloc() failed");
331 WARN_ON(1);
332 return -EIO;
333 }
334 handler->processor_id = hp_cpu->processor_id;
335 handler->addr = frame_dma;
336 handler->frame_ptr = frame_ptr;
337 list_add_tail(&handler->node, &hp_cpu->handlers);
338 }
339 return 0;
340}
341
342static int destroy_per_cpu_handlers(void)
343{
344 struct list_head *loop, *tmp;
345 struct hp_cpu *hp_cpu = this_cpu_ptr(&hp_cpus);
346
347 spin_lock(&hp_lock);
348 list_del(&hp_cpu->node);
349 spin_unlock(&hp_lock);
350 list_for_each_safe(loop, tmp, &hp_cpu->handlers) {
351 u32 flags = 0;
352 struct hp_handler *handler = list_entry(loop, struct hp_handler,
353 node);
354 if (qman_retire_fq(&handler->rx, &flags) ||
355 (flags & QMAN_FQ_STATE_BLOCKOOS)) {
356 pr_crit("qman_retire_fq(rx) failed, flags: %x", flags);
357 WARN_ON(1);
358 return -EIO;
359 }
360 if (qman_oos_fq(&handler->rx)) {
361 pr_crit("qman_oos_fq(rx) failed");
362 WARN_ON(1);
363 return -EIO;
364 }
365 qman_destroy_fq(&handler->rx);
366 qman_destroy_fq(&handler->tx);
367 qman_release_fqid(handler->fqid_rx);
368 list_del(&handler->node);
369 kmem_cache_free(hp_handler_slab, handler);
370 }
371 return 0;
372}
373
374static inline u8 num_cachelines(u32 offset)
375{
376 u8 res = (offset + (L1_CACHE_BYTES - 1))
377 / (L1_CACHE_BYTES);
378 if (res > 3)
379 return 3;
380 return res;
381}
382#define STASH_DATA_CL \
383 num_cachelines(HP_NUM_WORDS * 4)
384#define STASH_CTX_CL \
385 num_cachelines(offsetof(struct hp_handler, fqid_rx))
386
387static int init_handler(void *h)
388{
389 struct qm_mcc_initfq opts;
390 struct hp_handler *handler = h;
391 int err;
392
393 if (handler->processor_id != smp_processor_id()) {
394 err = -EIO;
395 goto failed;
396 }
397
398 memset(&handler->rx, 0, sizeof(handler->rx));
399 if (handler == special_handler)
400 handler->rx.cb.dqrr = special_dqrr;
401 else
402 handler->rx.cb.dqrr = normal_dqrr;
403 err = qman_create_fq(handler->fqid_rx, 0, &handler->rx);
404 if (err) {
405 pr_crit("qman_create_fq(rx) failed");
406 goto failed;
407 }
408 memset(&opts, 0, sizeof(opts));
409 opts.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL |
410 QM_INITFQ_WE_CONTEXTA);
411 opts.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_CTXASTASHING);
412 qm_fqd_set_stashing(&opts.fqd, 0, STASH_DATA_CL, STASH_CTX_CL);
413 err = qman_init_fq(&handler->rx, QMAN_INITFQ_FLAG_SCHED |
414 QMAN_INITFQ_FLAG_LOCAL, &opts);
415 if (err) {
416 pr_crit("qman_init_fq(rx) failed");
417 goto failed;
418 }
419
420 memset(&handler->tx, 0, sizeof(handler->tx));
421 err = qman_create_fq(handler->fqid_tx, QMAN_FQ_FLAG_NO_MODIFY,
422 &handler->tx);
423 if (err) {
424 pr_crit("qman_create_fq(tx) failed");
425 goto failed;
426 }
427
428 return 0;
429failed:
430 return err;
431}
432
433static void init_handler_cb(void *h)
434{
435 if (init_handler(h))
436 WARN_ON(1);
437}
438
439static int init_phase2(void)
440{
441 int loop;
442 u32 fqid = 0;
443 u32 lfsr = 0xdeadbeef;
444 struct hp_cpu *hp_cpu;
445 struct hp_handler *handler;
446
447 for (loop = 0; loop < HP_PER_CPU; loop++) {
448 list_for_each_entry(hp_cpu, &hp_cpu_list, node) {
449 int err;
450
451 if (!loop)
452 hp_cpu->iterator = list_first_entry(
453 &hp_cpu->handlers,
454 struct hp_handler, node);
455 else
456 hp_cpu->iterator = list_entry(
457 hp_cpu->iterator->node.next,
458 struct hp_handler, node);
459
460 hp_cpu->iterator->fqid_rx = fqid;
461
462 err = qman_alloc_fqid(&fqid);
463 if (err) {
464 pr_crit("qman_alloc_fqid() failed");
465 return err;
466 }
467 hp_cpu->iterator->fqid_tx = fqid;
468
469 hp_cpu->iterator->rx_mixer = lfsr;
470
471 lfsr = do_lfsr(lfsr);
472 hp_cpu->iterator->tx_mixer = lfsr;
473 }
474 }
475
476 hp_cpu = list_first_entry(&hp_cpu_list, struct hp_cpu, node);
477 handler = list_first_entry(&hp_cpu->handlers, struct hp_handler, node);
478 if (handler->fqid_rx != 0 || handler->rx_mixer != 0xdeadbeef)
479 return 1;
480 handler->fqid_rx = fqid;
481 handler->rx_mixer = lfsr;
482
483 special_handler = handler;
484 return 0;
485}
486
487static int init_phase3(void)
488{
489 int loop, err;
490 struct hp_cpu *hp_cpu;
491
492 for (loop = 0; loop < HP_PER_CPU; loop++) {
493 list_for_each_entry(hp_cpu, &hp_cpu_list, node) {
494 if (!loop)
495 hp_cpu->iterator = list_first_entry(
496 &hp_cpu->handlers,
497 struct hp_handler, node);
498 else
499 hp_cpu->iterator = list_entry(
500 hp_cpu->iterator->node.next,
501 struct hp_handler, node);
502 preempt_disable();
503 if (hp_cpu->processor_id == smp_processor_id()) {
504 err = init_handler(hp_cpu->iterator);
505 if (err)
506 return err;
507 } else {
508 smp_call_function_single(hp_cpu->processor_id,
509 init_handler_cb, hp_cpu->iterator, 1);
510 }
511 preempt_enable();
512 }
513 }
514 return 0;
515}
516
517static int send_first_frame(void *ignore)
518{
519 u32 *p = special_handler->frame_ptr;
520 u32 lfsr = HP_FIRST_WORD;
521 int loop, err;
522 struct qm_fd fd;
523
524 if (special_handler->processor_id != smp_processor_id()) {
525 err = -EIO;
526 goto failed;
527 }
528 memset(&fd, 0, sizeof(fd));
529 qm_fd_addr_set64(&fd, special_handler->addr);
530 qm_fd_set_contig_big(&fd, HP_NUM_WORDS * 4);
531 for (loop = 0; loop < HP_NUM_WORDS; loop++, p++) {
532 if (*p != lfsr) {
533 err = -EIO;
534 pr_crit("corrupt frame data");
535 goto failed;
536 }
537 *p ^= special_handler->tx_mixer;
538 lfsr = do_lfsr(lfsr);
539 }
540 pr_info("Sending first frame\n");
541 err = qman_enqueue(&special_handler->tx, &fd);
542 if (err) {
543 pr_crit("qman_enqueue() failed");
544 goto failed;
545 }
546
547 return 0;
548failed:
549 return err;
550}
551
552static void send_first_frame_cb(void *ignore)
553{
554 if (send_first_frame(NULL))
555 WARN_ON(1);
556}
557
558int qman_test_stash(void)
559{
560 int err;
561
562 if (cpumask_weight(cpu_online_mask) < 2) {
563 pr_info("%s(): skip - only 1 CPU\n", __func__);
564 return 0;
565 }
566
567 pr_info("%s(): Starting\n", __func__);
568
569 hp_cpu_list_length = 0;
570 loop_counter = 0;
571 hp_handler_slab = kmem_cache_create("hp_handler_slab",
572 sizeof(struct hp_handler), L1_CACHE_BYTES,
573 SLAB_HWCACHE_ALIGN, NULL);
574 if (!hp_handler_slab) {
575 err = -EIO;
576 pr_crit("kmem_cache_create() failed");
577 goto failed;
578 }
579
580 err = allocate_frame_data();
581 if (err)
582 goto failed;
583
584
585 pr_info("Creating %d handlers per cpu...\n", HP_PER_CPU);
586 if (on_all_cpus(create_per_cpu_handlers)) {
587 err = -EIO;
588 pr_crit("on_each_cpu() failed");
589 goto failed;
590 }
591 pr_info("Number of cpus: %d, total of %d handlers\n",
592 hp_cpu_list_length, hp_cpu_list_length * HP_PER_CPU);
593
594 err = init_phase2();
595 if (err)
596 goto failed;
597
598 err = init_phase3();
599 if (err)
600 goto failed;
601
602 preempt_disable();
603 if (special_handler->processor_id == smp_processor_id()) {
604 err = send_first_frame(NULL);
605 if (err)
606 goto failed;
607 } else {
608 smp_call_function_single(special_handler->processor_id,
609 send_first_frame_cb, NULL, 1);
610 }
611 preempt_enable();
612
613 wait_event(queue, loop_counter == HP_LOOPS);
614 deallocate_frame_data();
615 if (on_all_cpus(destroy_per_cpu_handlers)) {
616 err = -EIO;
617 pr_crit("on_each_cpu() failed");
618 goto failed;
619 }
620 kmem_cache_destroy(hp_handler_slab);
621 pr_info("%s(): Finished\n", __func__);
622
623 return 0;
624failed:
625 WARN_ON(1);
626 return err;
627}
628