This source file includes following definitions.
- __cm_reset
- pio_send_control
- wildcard_to_pool
- sc_type_name
- init_sc_pools_and_sizes
- init_send_contexts
- sc_hw_alloc
- sc_hw_free
- group_context
- group_size
- cr_group_addresses
- sc_halted
- sc_mtu_to_threshold
- sc_percent_to_threshold
- sc_set_cr_threshold
- set_pio_integrity
- get_buffers_allocated
- reset_buffers_allocated
- sc_alloc
- sc_free
- sc_disable
- packet_occupancy
- egress_halted
- is_sc_halted
- sc_wait_for_packet_egress
- sc_wait
- sc_restart
- pio_freeze
- pio_kernel_unfreeze
- pio_kernel_linkup
- pio_init_wait_progress
- pio_reset_all
- sc_enable
- sc_return_credits
- sc_flush
- sc_drop
- sc_stop
- sc_buffer_alloc
- sc_add_credit_return_intr
- sc_del_credit_return_intr
- hfi1_sc_wantpiobuf_intr
- sc_piobufavail
- fill_code
- sc_release_update
- sc_group_release_update
- pio_select_send_context_vl
- pio_select_send_context_sc
- pio_map_free
- pio_map_rcu_callback
- set_threshold
- pio_map_init
- free_pio_map
- init_pervl_scs
- init_credit_return
- free_credit_return
- seqfile_dump_sci
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 #include <linux/delay.h>
49 #include "hfi.h"
50 #include "qp.h"
51 #include "trace.h"
52
53 #define SC(name) SEND_CTXT_##name
54
55
56
57 static void sc_wait_for_packet_egress(struct send_context *sc, int pause);
58
59
60
61
62
63 void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl)
64 {
65 write_csr(dd, SEND_CTRL, sendctrl | SEND_CTRL_CM_RESET_SMASK);
66 while (1) {
67 udelay(1);
68 sendctrl = read_csr(dd, SEND_CTRL);
69 if ((sendctrl & SEND_CTRL_CM_RESET_SMASK) == 0)
70 break;
71 }
72 }
73
74
75 void pio_send_control(struct hfi1_devdata *dd, int op)
76 {
77 u64 reg, mask;
78 unsigned long flags;
79 int write = 1;
80 int flush = 0;
81 int i;
82
83 spin_lock_irqsave(&dd->sendctrl_lock, flags);
84
85 reg = read_csr(dd, SEND_CTRL);
86 switch (op) {
87 case PSC_GLOBAL_ENABLE:
88 reg |= SEND_CTRL_SEND_ENABLE_SMASK;
89
90 case PSC_DATA_VL_ENABLE:
91 mask = 0;
92 for (i = 0; i < ARRAY_SIZE(dd->vld); i++)
93 if (!dd->vld[i].mtu)
94 mask |= BIT_ULL(i);
95
96 mask = (mask & SEND_CTRL_UNSUPPORTED_VL_MASK) <<
97 SEND_CTRL_UNSUPPORTED_VL_SHIFT;
98 reg = (reg & ~SEND_CTRL_UNSUPPORTED_VL_SMASK) | mask;
99 break;
100 case PSC_GLOBAL_DISABLE:
101 reg &= ~SEND_CTRL_SEND_ENABLE_SMASK;
102 break;
103 case PSC_GLOBAL_VLARB_ENABLE:
104 reg |= SEND_CTRL_VL_ARBITER_ENABLE_SMASK;
105 break;
106 case PSC_GLOBAL_VLARB_DISABLE:
107 reg &= ~SEND_CTRL_VL_ARBITER_ENABLE_SMASK;
108 break;
109 case PSC_CM_RESET:
110 __cm_reset(dd, reg);
111 write = 0;
112 break;
113 case PSC_DATA_VL_DISABLE:
114 reg |= SEND_CTRL_UNSUPPORTED_VL_SMASK;
115 flush = 1;
116 break;
117 default:
118 dd_dev_err(dd, "%s: invalid control %d\n", __func__, op);
119 break;
120 }
121
122 if (write) {
123 write_csr(dd, SEND_CTRL, reg);
124 if (flush)
125 (void)read_csr(dd, SEND_CTRL);
126 }
127
128 spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
129 }
130
131
132 #define NUM_SC_POOLS 2
133
134
135 #define SCS_POOL_0 -1
136 #define SCS_POOL_1 -2
137
138
139 #define SCC_PER_VL -1
140 #define SCC_PER_CPU -2
141 #define SCC_PER_KRCVQ -3
142
143
144 #define SCS_ACK_CREDITS 32
145 #define SCS_VL15_CREDITS 102
146
147 #define PIO_THRESHOLD_CEILING 4096
148
149 #define PIO_WAIT_BATCH_SIZE 5
150
151
152 static struct sc_config_sizes sc_config_sizes[SC_MAX] = {
153 [SC_KERNEL] = { .size = SCS_POOL_0,
154 .count = SCC_PER_VL },
155 [SC_ACK] = { .size = SCS_ACK_CREDITS,
156 .count = SCC_PER_KRCVQ },
157 [SC_USER] = { .size = SCS_POOL_0,
158 .count = SCC_PER_CPU },
159 [SC_VL15] = { .size = SCS_VL15_CREDITS,
160 .count = 1 },
161
162 };
163
164
165 struct mem_pool_config {
166 int centipercent;
167 int absolute_blocks;
168 };
169
170
171 static struct mem_pool_config sc_mem_pool_config[NUM_SC_POOLS] = {
172
173 { 10000, -1 },
174 { 0, -1 },
175 };
176
177
178 struct mem_pool_info {
179 int centipercent;
180
181
182
183 int count;
184 int blocks;
185 int size;
186 };
187
188
189
190
191
192
193
194
195
196
197 static int wildcard_to_pool(int wc)
198 {
199 if (wc >= 0)
200 return -1;
201 return -wc - 1;
202 }
203
204 static const char *sc_type_names[SC_MAX] = {
205 "kernel",
206 "ack",
207 "user",
208 "vl15"
209 };
210
211 static const char *sc_type_name(int index)
212 {
213 if (index < 0 || index >= SC_MAX)
214 return "unknown";
215 return sc_type_names[index];
216 }
217
218
219
220
221
222
223 int init_sc_pools_and_sizes(struct hfi1_devdata *dd)
224 {
225 struct mem_pool_info mem_pool_info[NUM_SC_POOLS] = { { 0 } };
226 int total_blocks = (chip_pio_mem_size(dd) / PIO_BLOCK_SIZE) - 1;
227 int total_contexts = 0;
228 int fixed_blocks;
229 int pool_blocks;
230 int used_blocks;
231 int cp_total;
232 int ab_total;
233 int extra;
234 int i;
235
236
237
238
239
240
241
242
243
244
245 if (HFI1_CAP_IS_KSET(SDMA)) {
246 u16 max_pkt_size = (piothreshold < PIO_THRESHOLD_CEILING) ?
247 piothreshold : PIO_THRESHOLD_CEILING;
248 sc_config_sizes[SC_KERNEL].size =
249 3 * (max_pkt_size + 128) / PIO_BLOCK_SIZE;
250 }
251
252
253
254
255
256
257
258
259 cp_total = 0;
260 ab_total = 0;
261 for (i = 0; i < NUM_SC_POOLS; i++) {
262 int cp = sc_mem_pool_config[i].centipercent;
263 int ab = sc_mem_pool_config[i].absolute_blocks;
264
265
266
267
268
269 if (cp >= 0) {
270 cp_total += cp;
271 } else if (ab >= 0) {
272 ab_total += ab;
273 } else {
274 dd_dev_err(
275 dd,
276 "Send context memory pool %d: both the block count and centipercent are invalid\n",
277 i);
278 return -EINVAL;
279 }
280
281 mem_pool_info[i].centipercent = cp;
282 mem_pool_info[i].blocks = ab;
283 }
284
285
286 if (cp_total != 0 && ab_total != 0) {
287 dd_dev_err(
288 dd,
289 "All send context memory pools must be described as either centipercent or blocks, no mixing between pools\n");
290 return -EINVAL;
291 }
292
293
294 if (cp_total != 0 && cp_total != 10000) {
295 dd_dev_err(
296 dd,
297 "Send context memory pool centipercent is %d, expecting 10000\n",
298 cp_total);
299 return -EINVAL;
300 }
301
302
303 if (ab_total > total_blocks) {
304 dd_dev_err(
305 dd,
306 "Send context memory pool absolute block count %d is larger than the memory size %d\n",
307 ab_total, total_blocks);
308 return -EINVAL;
309 }
310
311
312
313
314
315
316
317
318 fixed_blocks = 0;
319 for (i = 0; i < SC_MAX; i++) {
320 int count = sc_config_sizes[i].count;
321 int size = sc_config_sizes[i].size;
322 int pool;
323
324
325
326
327
328
329
330 if (i == SC_ACK) {
331 count = dd->n_krcv_queues;
332 } else if (i == SC_KERNEL) {
333 count = INIT_SC_PER_VL * num_vls;
334 } else if (count == SCC_PER_CPU) {
335 count = dd->num_rcv_contexts - dd->n_krcv_queues;
336 } else if (count < 0) {
337 dd_dev_err(
338 dd,
339 "%s send context invalid count wildcard %d\n",
340 sc_type_name(i), count);
341 return -EINVAL;
342 }
343 if (total_contexts + count > chip_send_contexts(dd))
344 count = chip_send_contexts(dd) - total_contexts;
345
346 total_contexts += count;
347
348
349
350
351
352
353
354 pool = wildcard_to_pool(size);
355 if (pool == -1) {
356 fixed_blocks += size * count;
357 } else if (pool < NUM_SC_POOLS) {
358 mem_pool_info[pool].count += count;
359 } else {
360 dd_dev_err(
361 dd,
362 "%s send context invalid pool wildcard %d\n",
363 sc_type_name(i), size);
364 return -EINVAL;
365 }
366
367 dd->sc_sizes[i].count = count;
368 dd->sc_sizes[i].size = size;
369 }
370 if (fixed_blocks > total_blocks) {
371 dd_dev_err(
372 dd,
373 "Send context fixed block count, %u, larger than total block count %u\n",
374 fixed_blocks, total_blocks);
375 return -EINVAL;
376 }
377
378
379 pool_blocks = total_blocks - fixed_blocks;
380 if (ab_total > pool_blocks) {
381 dd_dev_err(
382 dd,
383 "Send context fixed pool sizes, %u, larger than pool block count %u\n",
384 ab_total, pool_blocks);
385 return -EINVAL;
386 }
387
388 pool_blocks -= ab_total;
389
390 for (i = 0; i < NUM_SC_POOLS; i++) {
391 struct mem_pool_info *pi = &mem_pool_info[i];
392
393
394 if (pi->centipercent >= 0)
395 pi->blocks = (pool_blocks * pi->centipercent) / 10000;
396
397 if (pi->blocks == 0 && pi->count != 0) {
398 dd_dev_err(
399 dd,
400 "Send context memory pool %d has %u contexts, but no blocks\n",
401 i, pi->count);
402 return -EINVAL;
403 }
404 if (pi->count == 0) {
405
406 if (pi->blocks != 0)
407 dd_dev_err(
408 dd,
409 "Send context memory pool %d has %u blocks, but zero contexts\n",
410 i, pi->blocks);
411 pi->size = 0;
412 } else {
413 pi->size = pi->blocks / pi->count;
414 }
415 }
416
417
418 used_blocks = 0;
419 for (i = 0; i < SC_MAX; i++) {
420 if (dd->sc_sizes[i].size < 0) {
421 unsigned pool = wildcard_to_pool(dd->sc_sizes[i].size);
422
423 WARN_ON_ONCE(pool >= NUM_SC_POOLS);
424 dd->sc_sizes[i].size = mem_pool_info[pool].size;
425 }
426
427 #define PIO_MAX_BLOCKS 1024
428 if (dd->sc_sizes[i].size > PIO_MAX_BLOCKS)
429 dd->sc_sizes[i].size = PIO_MAX_BLOCKS;
430
431
432 used_blocks += dd->sc_sizes[i].size * dd->sc_sizes[i].count;
433 }
434 extra = total_blocks - used_blocks;
435 if (extra != 0)
436 dd_dev_info(dd, "unused send context blocks: %d\n", extra);
437
438 return total_contexts;
439 }
440
441 int init_send_contexts(struct hfi1_devdata *dd)
442 {
443 u16 base;
444 int ret, i, j, context;
445
446 ret = init_credit_return(dd);
447 if (ret)
448 return ret;
449
450 dd->hw_to_sw = kmalloc_array(TXE_NUM_CONTEXTS, sizeof(u8),
451 GFP_KERNEL);
452 dd->send_contexts = kcalloc(dd->num_send_contexts,
453 sizeof(struct send_context_info),
454 GFP_KERNEL);
455 if (!dd->send_contexts || !dd->hw_to_sw) {
456 kfree(dd->hw_to_sw);
457 kfree(dd->send_contexts);
458 free_credit_return(dd);
459 return -ENOMEM;
460 }
461
462
463 for (i = 0; i < TXE_NUM_CONTEXTS; i++)
464 dd->hw_to_sw[i] = INVALID_SCI;
465
466
467
468
469
470 context = 0;
471 base = 1;
472 for (i = 0; i < SC_MAX; i++) {
473 struct sc_config_sizes *scs = &dd->sc_sizes[i];
474
475 for (j = 0; j < scs->count; j++) {
476 struct send_context_info *sci =
477 &dd->send_contexts[context];
478 sci->type = i;
479 sci->base = base;
480 sci->credits = scs->size;
481
482 context++;
483 base += scs->size;
484 }
485 }
486
487 return 0;
488 }
489
490
491
492
493
494
495 static int sc_hw_alloc(struct hfi1_devdata *dd, int type, u32 *sw_index,
496 u32 *hw_context)
497 {
498 struct send_context_info *sci;
499 u32 index;
500 u32 context;
501
502 for (index = 0, sci = &dd->send_contexts[0];
503 index < dd->num_send_contexts; index++, sci++) {
504 if (sci->type == type && sci->allocated == 0) {
505 sci->allocated = 1;
506
507 context = chip_send_contexts(dd) - index - 1;
508 dd->hw_to_sw[context] = index;
509 *sw_index = index;
510 *hw_context = context;
511 return 0;
512 }
513 }
514 dd_dev_err(dd, "Unable to locate a free type %d send context\n", type);
515 return -ENOSPC;
516 }
517
518
519
520
521
522
523 static void sc_hw_free(struct hfi1_devdata *dd, u32 sw_index, u32 hw_context)
524 {
525 struct send_context_info *sci;
526
527 sci = &dd->send_contexts[sw_index];
528 if (!sci->allocated) {
529 dd_dev_err(dd, "%s: sw_index %u not allocated? hw_context %u\n",
530 __func__, sw_index, hw_context);
531 }
532 sci->allocated = 0;
533 dd->hw_to_sw[hw_context] = INVALID_SCI;
534 }
535
536
537 static inline u32 group_context(u32 context, u32 group)
538 {
539 return (context >> group) << group;
540 }
541
542
543 static inline u32 group_size(u32 group)
544 {
545 return 1 << group;
546 }
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561 static void cr_group_addresses(struct send_context *sc, dma_addr_t *dma)
562 {
563 u32 gc = group_context(sc->hw_context, sc->group);
564 u32 index = sc->hw_context & 0x7;
565
566 sc->hw_free = &sc->dd->cr_base[sc->node].va[gc].cr[index];
567 *dma = (unsigned long)
568 &((struct credit_return *)sc->dd->cr_base[sc->node].dma)[gc];
569 }
570
571
572
573
574
575 static void sc_halted(struct work_struct *work)
576 {
577 struct send_context *sc;
578
579 sc = container_of(work, struct send_context, halt_work);
580 sc_restart(sc);
581 }
582
583
584
585
586
587
588
589
590
591
592
593 u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize)
594 {
595 u32 release_credits;
596 u32 threshold;
597
598
599 mtu += hdrqentsize << 2;
600 release_credits = DIV_ROUND_UP(mtu, PIO_BLOCK_SIZE);
601
602
603 if (sc->credits <= release_credits)
604 threshold = 1;
605 else
606 threshold = sc->credits - release_credits;
607
608 return threshold;
609 }
610
611
612
613
614
615
616
617
618 u32 sc_percent_to_threshold(struct send_context *sc, u32 percent)
619 {
620 return (sc->credits * percent) / 100;
621 }
622
623
624
625
626 void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold)
627 {
628 unsigned long flags;
629 u32 old_threshold;
630 int force_return = 0;
631
632 spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
633
634 old_threshold = (sc->credit_ctrl >>
635 SC(CREDIT_CTRL_THRESHOLD_SHIFT))
636 & SC(CREDIT_CTRL_THRESHOLD_MASK);
637
638 if (new_threshold != old_threshold) {
639 sc->credit_ctrl =
640 (sc->credit_ctrl
641 & ~SC(CREDIT_CTRL_THRESHOLD_SMASK))
642 | ((new_threshold
643 & SC(CREDIT_CTRL_THRESHOLD_MASK))
644 << SC(CREDIT_CTRL_THRESHOLD_SHIFT));
645 write_kctxt_csr(sc->dd, sc->hw_context,
646 SC(CREDIT_CTRL), sc->credit_ctrl);
647
648
649 force_return = 1;
650 }
651
652 spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
653
654 if (force_return)
655 sc_return_credits(sc);
656 }
657
658
659
660
661
662
663 void set_pio_integrity(struct send_context *sc)
664 {
665 struct hfi1_devdata *dd = sc->dd;
666 u32 hw_context = sc->hw_context;
667 int type = sc->type;
668
669 write_kctxt_csr(dd, hw_context,
670 SC(CHECK_ENABLE),
671 hfi1_pkt_default_send_ctxt_mask(dd, type));
672 }
673
674 static u32 get_buffers_allocated(struct send_context *sc)
675 {
676 int cpu;
677 u32 ret = 0;
678
679 for_each_possible_cpu(cpu)
680 ret += *per_cpu_ptr(sc->buffers_allocated, cpu);
681 return ret;
682 }
683
684 static void reset_buffers_allocated(struct send_context *sc)
685 {
686 int cpu;
687
688 for_each_possible_cpu(cpu)
689 (*per_cpu_ptr(sc->buffers_allocated, cpu)) = 0;
690 }
691
692
693
694
695
696 struct send_context *sc_alloc(struct hfi1_devdata *dd, int type,
697 uint hdrqentsize, int numa)
698 {
699 struct send_context_info *sci;
700 struct send_context *sc = NULL;
701 dma_addr_t dma;
702 unsigned long flags;
703 u64 reg;
704 u32 thresh;
705 u32 sw_index;
706 u32 hw_context;
707 int ret;
708 u8 opval, opmask;
709
710
711 if (dd->flags & HFI1_FROZEN)
712 return NULL;
713
714 sc = kzalloc_node(sizeof(*sc), GFP_KERNEL, numa);
715 if (!sc)
716 return NULL;
717
718 sc->buffers_allocated = alloc_percpu(u32);
719 if (!sc->buffers_allocated) {
720 kfree(sc);
721 dd_dev_err(dd,
722 "Cannot allocate buffers_allocated per cpu counters\n"
723 );
724 return NULL;
725 }
726
727 spin_lock_irqsave(&dd->sc_lock, flags);
728 ret = sc_hw_alloc(dd, type, &sw_index, &hw_context);
729 if (ret) {
730 spin_unlock_irqrestore(&dd->sc_lock, flags);
731 free_percpu(sc->buffers_allocated);
732 kfree(sc);
733 return NULL;
734 }
735
736 sci = &dd->send_contexts[sw_index];
737 sci->sc = sc;
738
739 sc->dd = dd;
740 sc->node = numa;
741 sc->type = type;
742 spin_lock_init(&sc->alloc_lock);
743 spin_lock_init(&sc->release_lock);
744 spin_lock_init(&sc->credit_ctrl_lock);
745 seqlock_init(&sc->waitlock);
746 INIT_LIST_HEAD(&sc->piowait);
747 INIT_WORK(&sc->halt_work, sc_halted);
748 init_waitqueue_head(&sc->halt_wait);
749
750
751 sc->group = 0;
752
753 sc->sw_index = sw_index;
754 sc->hw_context = hw_context;
755 cr_group_addresses(sc, &dma);
756 sc->credits = sci->credits;
757 sc->size = sc->credits * PIO_BLOCK_SIZE;
758
759
760 #define PIO_ADDR_CONTEXT_MASK 0xfful
761 #define PIO_ADDR_CONTEXT_SHIFT 16
762 sc->base_addr = dd->piobase + ((hw_context & PIO_ADDR_CONTEXT_MASK)
763 << PIO_ADDR_CONTEXT_SHIFT);
764
765
766 reg = ((sci->credits & SC(CTRL_CTXT_DEPTH_MASK))
767 << SC(CTRL_CTXT_DEPTH_SHIFT))
768 | ((sci->base & SC(CTRL_CTXT_BASE_MASK))
769 << SC(CTRL_CTXT_BASE_SHIFT));
770 write_kctxt_csr(dd, hw_context, SC(CTRL), reg);
771
772 set_pio_integrity(sc);
773
774
775 write_kctxt_csr(dd, hw_context, SC(ERR_MASK), (u64)-1);
776
777
778 write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY),
779 (SC(CHECK_PARTITION_KEY_VALUE_MASK) &
780 DEFAULT_PKEY) <<
781 SC(CHECK_PARTITION_KEY_VALUE_SHIFT));
782
783
784 if (type == SC_USER) {
785 opval = USER_OPCODE_CHECK_VAL;
786 opmask = USER_OPCODE_CHECK_MASK;
787 } else {
788 opval = OPCODE_CHECK_VAL_DISABLED;
789 opmask = OPCODE_CHECK_MASK_DISABLED;
790 }
791
792
793 write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE),
794 ((u64)opmask << SC(CHECK_OPCODE_MASK_SHIFT)) |
795 ((u64)opval << SC(CHECK_OPCODE_VALUE_SHIFT)));
796
797
798 reg = dma & SC(CREDIT_RETURN_ADDR_ADDRESS_SMASK);
799 write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), reg);
800
801
802
803
804
805
806
807
808
809
810
811
812 if (type == SC_ACK) {
813 thresh = sc_percent_to_threshold(sc, 50);
814 } else if (type == SC_USER) {
815 thresh = sc_percent_to_threshold(sc,
816 user_credit_return_threshold);
817 } else {
818 thresh = min(sc_percent_to_threshold(sc, 50),
819 sc_mtu_to_threshold(sc, hfi1_max_mtu,
820 hdrqentsize));
821 }
822 reg = thresh << SC(CREDIT_CTRL_THRESHOLD_SHIFT);
823
824 if (type == SC_USER && HFI1_CAP_IS_USET(EARLY_CREDIT_RETURN))
825 reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
826 else if (HFI1_CAP_IS_KSET(EARLY_CREDIT_RETURN))
827 reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
828
829
830 sc->credit_ctrl = reg;
831 write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), reg);
832
833
834 if (type == SC_USER) {
835 reg = 1ULL << 15;
836 write_kctxt_csr(dd, hw_context, SC(CHECK_VL), reg);
837 }
838
839 spin_unlock_irqrestore(&dd->sc_lock, flags);
840
841
842
843
844
845
846
847
848
849 if (type != SC_USER) {
850
851
852
853
854 sc->sr_size = sci->credits + 1;
855 sc->sr = kcalloc_node(sc->sr_size,
856 sizeof(union pio_shadow_ring),
857 GFP_KERNEL, numa);
858 if (!sc->sr) {
859 sc_free(sc);
860 return NULL;
861 }
862 }
863
864 hfi1_cdbg(PIO,
865 "Send context %u(%u) %s group %u credits %u credit_ctrl 0x%llx threshold %u\n",
866 sw_index,
867 hw_context,
868 sc_type_name(type),
869 sc->group,
870 sc->credits,
871 sc->credit_ctrl,
872 thresh);
873
874 return sc;
875 }
876
877
878 void sc_free(struct send_context *sc)
879 {
880 struct hfi1_devdata *dd;
881 unsigned long flags;
882 u32 sw_index;
883 u32 hw_context;
884
885 if (!sc)
886 return;
887
888 sc->flags |= SCF_IN_FREE;
889 dd = sc->dd;
890 if (!list_empty(&sc->piowait))
891 dd_dev_err(dd, "piowait list not empty!\n");
892 sw_index = sc->sw_index;
893 hw_context = sc->hw_context;
894 sc_disable(sc);
895 flush_work(&sc->halt_work);
896
897 spin_lock_irqsave(&dd->sc_lock, flags);
898 dd->send_contexts[sw_index].sc = NULL;
899
900
901 write_kctxt_csr(dd, hw_context, SC(CTRL), 0);
902 write_kctxt_csr(dd, hw_context, SC(CHECK_ENABLE), 0);
903 write_kctxt_csr(dd, hw_context, SC(ERR_MASK), 0);
904 write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY), 0);
905 write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE), 0);
906 write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), 0);
907 write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), 0);
908
909
910 sc_hw_free(dd, sw_index, hw_context);
911 spin_unlock_irqrestore(&dd->sc_lock, flags);
912
913 kfree(sc->sr);
914 free_percpu(sc->buffers_allocated);
915 kfree(sc);
916 }
917
918
919 void sc_disable(struct send_context *sc)
920 {
921 u64 reg;
922 struct pio_buf *pbuf;
923
924 if (!sc)
925 return;
926
927
928 spin_lock_irq(&sc->alloc_lock);
929 reg = read_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL));
930 reg &= ~SC(CTRL_CTXT_ENABLE_SMASK);
931 sc->flags &= ~SCF_ENABLED;
932 sc_wait_for_packet_egress(sc, 1);
933 write_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL), reg);
934
935
936
937
938
939
940
941
942 udelay(1);
943 spin_lock(&sc->release_lock);
944 if (sc->sr) {
945 while (sc->sr_tail != sc->sr_head) {
946 pbuf = &sc->sr[sc->sr_tail].pbuf;
947 if (pbuf->cb)
948 (*pbuf->cb)(pbuf->arg, PRC_SC_DISABLE);
949 sc->sr_tail++;
950 if (sc->sr_tail >= sc->sr_size)
951 sc->sr_tail = 0;
952 }
953 }
954 spin_unlock(&sc->release_lock);
955
956 write_seqlock(&sc->waitlock);
957 while (!list_empty(&sc->piowait)) {
958 struct iowait *wait;
959 struct rvt_qp *qp;
960 struct hfi1_qp_priv *priv;
961
962 wait = list_first_entry(&sc->piowait, struct iowait, list);
963 qp = iowait_to_qp(wait);
964 priv = qp->priv;
965 list_del_init(&priv->s_iowait.list);
966 priv->s_iowait.lock = NULL;
967 hfi1_qp_wakeup(qp, RVT_S_WAIT_PIO | HFI1_S_WAIT_PIO_DRAIN);
968 }
969 write_sequnlock(&sc->waitlock);
970
971 spin_unlock_irq(&sc->alloc_lock);
972 }
973
974
975 static u64 packet_occupancy(u64 reg)
976 {
977 return (reg &
978 SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SMASK)
979 >> SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SHIFT;
980 }
981
982
983 static bool egress_halted(u64 reg)
984 {
985 return !!(reg & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_HALT_STATUS_SMASK);
986 }
987
988
989 static bool is_sc_halted(struct hfi1_devdata *dd, u32 hw_context)
990 {
991 return !!(read_kctxt_csr(dd, hw_context, SC(STATUS)) &
992 SC(STATUS_CTXT_HALTED_SMASK));
993 }
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 static void sc_wait_for_packet_egress(struct send_context *sc, int pause)
1010 {
1011 struct hfi1_devdata *dd = sc->dd;
1012 u64 reg = 0;
1013 u64 reg_prev;
1014 u32 loop = 0;
1015
1016 while (1) {
1017 reg_prev = reg;
1018 reg = read_csr(dd, sc->hw_context * 8 +
1019 SEND_EGRESS_CTXT_STATUS);
1020
1021 if (sc->flags & SCF_HALTED ||
1022 is_sc_halted(dd, sc->hw_context) || egress_halted(reg))
1023 break;
1024 reg = packet_occupancy(reg);
1025 if (reg == 0)
1026 break;
1027
1028 if (reg != reg_prev)
1029 loop = 0;
1030 if (loop > 50000) {
1031
1032 dd_dev_err(dd,
1033 "%s: context %u(%u) timeout waiting for packets to egress, remaining count %u, bouncing link\n",
1034 __func__, sc->sw_index,
1035 sc->hw_context, (u32)reg);
1036 queue_work(dd->pport->link_wq,
1037 &dd->pport->link_bounce_work);
1038 break;
1039 }
1040 loop++;
1041 udelay(1);
1042 }
1043
1044 if (pause)
1045
1046 pause_for_credit_return(dd);
1047 }
1048
1049 void sc_wait(struct hfi1_devdata *dd)
1050 {
1051 int i;
1052
1053 for (i = 0; i < dd->num_send_contexts; i++) {
1054 struct send_context *sc = dd->send_contexts[i].sc;
1055
1056 if (!sc)
1057 continue;
1058 sc_wait_for_packet_egress(sc, 0);
1059 }
1060 }
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071 int sc_restart(struct send_context *sc)
1072 {
1073 struct hfi1_devdata *dd = sc->dd;
1074 u64 reg;
1075 u32 loop;
1076 int count;
1077
1078
1079 if (!(sc->flags & SCF_HALTED) || (sc->flags & SCF_IN_FREE))
1080 return -EINVAL;
1081
1082 dd_dev_info(dd, "restarting send context %u(%u)\n", sc->sw_index,
1083 sc->hw_context);
1084
1085
1086
1087
1088
1089
1090
1091 loop = 0;
1092 while (1) {
1093 reg = read_kctxt_csr(dd, sc->hw_context, SC(STATUS));
1094 if (reg & SC(STATUS_CTXT_HALTED_SMASK))
1095 break;
1096 if (loop > 100) {
1097 dd_dev_err(dd, "%s: context %u(%u) not halting, skipping\n",
1098 __func__, sc->sw_index, sc->hw_context);
1099 return -ETIME;
1100 }
1101 loop++;
1102 udelay(1);
1103 }
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115 if (sc->type != SC_USER) {
1116
1117 loop = 0;
1118 while (1) {
1119 count = get_buffers_allocated(sc);
1120 if (count == 0)
1121 break;
1122 if (loop > 100) {
1123 dd_dev_err(dd,
1124 "%s: context %u(%u) timeout waiting for PIO buffers to zero, remaining %d\n",
1125 __func__, sc->sw_index,
1126 sc->hw_context, count);
1127 }
1128 loop++;
1129 udelay(1);
1130 }
1131 }
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142 sc_disable(sc);
1143
1144
1145
1146
1147
1148
1149
1150 return sc_enable(sc);
1151 }
1152
1153
1154
1155
1156
1157
1158 void pio_freeze(struct hfi1_devdata *dd)
1159 {
1160 struct send_context *sc;
1161 int i;
1162
1163 for (i = 0; i < dd->num_send_contexts; i++) {
1164 sc = dd->send_contexts[i].sc;
1165
1166
1167
1168
1169
1170 if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
1171 continue;
1172
1173
1174 sc_disable(sc);
1175 }
1176 }
1177
1178
1179
1180
1181
1182
1183
1184
1185 void pio_kernel_unfreeze(struct hfi1_devdata *dd)
1186 {
1187 struct send_context *sc;
1188 int i;
1189
1190 for (i = 0; i < dd->num_send_contexts; i++) {
1191 sc = dd->send_contexts[i].sc;
1192 if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
1193 continue;
1194 if (sc->flags & SCF_LINK_DOWN)
1195 continue;
1196
1197 sc_enable(sc);
1198 }
1199 }
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213 void pio_kernel_linkup(struct hfi1_devdata *dd)
1214 {
1215 struct send_context *sc;
1216 int i;
1217
1218 for (i = 0; i < dd->num_send_contexts; i++) {
1219 sc = dd->send_contexts[i].sc;
1220 if (!sc || !(sc->flags & SCF_LINK_DOWN) || sc->type == SC_USER)
1221 continue;
1222
1223 sc_enable(sc);
1224 }
1225 }
1226
1227
1228
1229
1230
1231
1232
1233 static int pio_init_wait_progress(struct hfi1_devdata *dd)
1234 {
1235 u64 reg;
1236 int max, count = 0;
1237
1238
1239 max = (dd->icode == ICODE_FPGA_EMULATION) ? 120 : 5;
1240 while (1) {
1241 reg = read_csr(dd, SEND_PIO_INIT_CTXT);
1242 if (!(reg & SEND_PIO_INIT_CTXT_PIO_INIT_IN_PROGRESS_SMASK))
1243 break;
1244 if (count >= max)
1245 return -ETIMEDOUT;
1246 udelay(5);
1247 count++;
1248 }
1249
1250 return reg & SEND_PIO_INIT_CTXT_PIO_INIT_ERR_SMASK ? -EIO : 0;
1251 }
1252
1253
1254
1255
1256
1257 void pio_reset_all(struct hfi1_devdata *dd)
1258 {
1259 int ret;
1260
1261
1262 ret = pio_init_wait_progress(dd);
1263
1264 if (ret == -EIO) {
1265
1266 write_csr(dd, SEND_PIO_ERR_CLEAR,
1267 SEND_PIO_ERR_CLEAR_PIO_INIT_SM_IN_ERR_SMASK);
1268 }
1269
1270
1271 write_csr(dd, SEND_PIO_INIT_CTXT,
1272 SEND_PIO_INIT_CTXT_PIO_ALL_CTXT_INIT_SMASK);
1273 udelay(2);
1274 ret = pio_init_wait_progress(dd);
1275 if (ret < 0) {
1276 dd_dev_err(dd,
1277 "PIO send context init %s while initializing all PIO blocks\n",
1278 ret == -ETIMEDOUT ? "is stuck" : "had an error");
1279 }
1280 }
1281
1282
1283 int sc_enable(struct send_context *sc)
1284 {
1285 u64 sc_ctrl, reg, pio;
1286 struct hfi1_devdata *dd;
1287 unsigned long flags;
1288 int ret = 0;
1289
1290 if (!sc)
1291 return -EINVAL;
1292 dd = sc->dd;
1293
1294
1295
1296
1297
1298
1299
1300
1301 spin_lock_irqsave(&sc->alloc_lock, flags);
1302 sc_ctrl = read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
1303 if ((sc_ctrl & SC(CTRL_CTXT_ENABLE_SMASK)))
1304 goto unlock;
1305
1306
1307
1308 *sc->hw_free = 0;
1309 sc->free = 0;
1310 sc->alloc_free = 0;
1311 sc->fill = 0;
1312 sc->fill_wrap = 0;
1313 sc->sr_head = 0;
1314 sc->sr_tail = 0;
1315 sc->flags = 0;
1316
1317 reset_buffers_allocated(sc);
1318
1319
1320
1321
1322
1323
1324
1325 reg = read_kctxt_csr(dd, sc->hw_context, SC(ERR_STATUS));
1326 if (reg)
1327 write_kctxt_csr(dd, sc->hw_context, SC(ERR_CLEAR), reg);
1328
1329
1330
1331
1332
1333 spin_lock(&dd->sc_init_lock);
1334
1335
1336
1337
1338
1339
1340
1341 pio = ((sc->hw_context & SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_MASK) <<
1342 SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_SHIFT) |
1343 SEND_PIO_INIT_CTXT_PIO_SINGLE_CTXT_INIT_SMASK;
1344 write_csr(dd, SEND_PIO_INIT_CTXT, pio);
1345
1346
1347
1348
1349 udelay(2);
1350 ret = pio_init_wait_progress(dd);
1351 spin_unlock(&dd->sc_init_lock);
1352 if (ret) {
1353 dd_dev_err(dd,
1354 "sctxt%u(%u): Context not enabled due to init failure %d\n",
1355 sc->sw_index, sc->hw_context, ret);
1356 goto unlock;
1357 }
1358
1359
1360
1361
1362 sc_ctrl |= SC(CTRL_CTXT_ENABLE_SMASK);
1363 write_kctxt_csr(dd, sc->hw_context, SC(CTRL), sc_ctrl);
1364
1365
1366
1367
1368 read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
1369 sc->flags |= SCF_ENABLED;
1370
1371 unlock:
1372 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1373
1374 return ret;
1375 }
1376
1377
1378 void sc_return_credits(struct send_context *sc)
1379 {
1380 if (!sc)
1381 return;
1382
1383
1384 write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE),
1385 SC(CREDIT_FORCE_FORCE_RETURN_SMASK));
1386
1387
1388
1389
1390 read_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE));
1391
1392 write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE), 0);
1393 }
1394
1395
1396 void sc_flush(struct send_context *sc)
1397 {
1398 if (!sc)
1399 return;
1400
1401 sc_wait_for_packet_egress(sc, 1);
1402 }
1403
1404
1405 void sc_drop(struct send_context *sc)
1406 {
1407 if (!sc)
1408 return;
1409
1410 dd_dev_info(sc->dd, "%s: context %u(%u) - not implemented\n",
1411 __func__, sc->sw_index, sc->hw_context);
1412 }
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422 void sc_stop(struct send_context *sc, int flag)
1423 {
1424 unsigned long flags;
1425
1426
1427 spin_lock_irqsave(&sc->alloc_lock, flags);
1428
1429 sc->flags |= flag;
1430 sc->flags &= ~SCF_ENABLED;
1431 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1432 wake_up(&sc->halt_wait);
1433 }
1434
1435 #define BLOCK_DWORDS (PIO_BLOCK_SIZE / sizeof(u32))
1436 #define dwords_to_blocks(x) DIV_ROUND_UP(x, BLOCK_DWORDS)
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449 struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
1450 pio_release_cb cb, void *arg)
1451 {
1452 struct pio_buf *pbuf = NULL;
1453 unsigned long flags;
1454 unsigned long avail;
1455 unsigned long blocks = dwords_to_blocks(dw_len);
1456 u32 fill_wrap;
1457 int trycount = 0;
1458 u32 head, next;
1459
1460 spin_lock_irqsave(&sc->alloc_lock, flags);
1461 if (!(sc->flags & SCF_ENABLED)) {
1462 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1463 return ERR_PTR(-ECOMM);
1464 }
1465
1466 retry:
1467 avail = (unsigned long)sc->credits - (sc->fill - sc->alloc_free);
1468 if (blocks > avail) {
1469
1470 if (unlikely(trycount)) {
1471 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1472 goto done;
1473 }
1474
1475 sc->alloc_free = READ_ONCE(sc->free);
1476 avail =
1477 (unsigned long)sc->credits -
1478 (sc->fill - sc->alloc_free);
1479 if (blocks > avail) {
1480
1481 sc_release_update(sc);
1482 sc->alloc_free = READ_ONCE(sc->free);
1483 trycount++;
1484 goto retry;
1485 }
1486 }
1487
1488
1489
1490 preempt_disable();
1491 this_cpu_inc(*sc->buffers_allocated);
1492
1493
1494 head = sc->sr_head;
1495
1496
1497 sc->fill += blocks;
1498 fill_wrap = sc->fill_wrap;
1499 sc->fill_wrap += blocks;
1500 if (sc->fill_wrap >= sc->credits)
1501 sc->fill_wrap = sc->fill_wrap - sc->credits;
1502
1503
1504
1505
1506
1507
1508
1509
1510 pbuf = &sc->sr[head].pbuf;
1511 pbuf->sent_at = sc->fill;
1512 pbuf->cb = cb;
1513 pbuf->arg = arg;
1514 pbuf->sc = sc;
1515
1516
1517
1518 next = head + 1;
1519 if (next >= sc->sr_size)
1520 next = 0;
1521
1522
1523
1524
1525 smp_wmb();
1526 sc->sr_head = next;
1527 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1528
1529
1530 pbuf->start = sc->base_addr + fill_wrap * PIO_BLOCK_SIZE;
1531 pbuf->end = sc->base_addr + sc->size;
1532 pbuf->qw_written = 0;
1533 pbuf->carry_bytes = 0;
1534 pbuf->carry.val64 = 0;
1535 done:
1536 return pbuf;
1537 }
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550 void sc_add_credit_return_intr(struct send_context *sc)
1551 {
1552 unsigned long flags;
1553
1554
1555 spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
1556 if (sc->credit_intr_count == 0) {
1557 sc->credit_ctrl |= SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
1558 write_kctxt_csr(sc->dd, sc->hw_context,
1559 SC(CREDIT_CTRL), sc->credit_ctrl);
1560 }
1561 sc->credit_intr_count++;
1562 spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
1563 }
1564
1565
1566
1567
1568
1569 void sc_del_credit_return_intr(struct send_context *sc)
1570 {
1571 unsigned long flags;
1572
1573 WARN_ON(sc->credit_intr_count == 0);
1574
1575
1576 spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
1577 sc->credit_intr_count--;
1578 if (sc->credit_intr_count == 0) {
1579 sc->credit_ctrl &= ~SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
1580 write_kctxt_csr(sc->dd, sc->hw_context,
1581 SC(CREDIT_CTRL), sc->credit_ctrl);
1582 }
1583 spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
1584 }
1585
1586
1587
1588
1589
1590 void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint)
1591 {
1592 if (needint)
1593 sc_add_credit_return_intr(sc);
1594 else
1595 sc_del_credit_return_intr(sc);
1596 trace_hfi1_wantpiointr(sc, needint, sc->credit_ctrl);
1597 if (needint)
1598 sc_return_credits(sc);
1599 }
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609 static void sc_piobufavail(struct send_context *sc)
1610 {
1611 struct hfi1_devdata *dd = sc->dd;
1612 struct list_head *list;
1613 struct rvt_qp *qps[PIO_WAIT_BATCH_SIZE];
1614 struct rvt_qp *qp;
1615 struct hfi1_qp_priv *priv;
1616 unsigned long flags;
1617 uint i, n = 0, top_idx = 0;
1618
1619 if (dd->send_contexts[sc->sw_index].type != SC_KERNEL &&
1620 dd->send_contexts[sc->sw_index].type != SC_VL15)
1621 return;
1622 list = &sc->piowait;
1623
1624
1625
1626
1627
1628
1629 write_seqlock_irqsave(&sc->waitlock, flags);
1630 while (!list_empty(list)) {
1631 struct iowait *wait;
1632
1633 if (n == ARRAY_SIZE(qps))
1634 break;
1635 wait = list_first_entry(list, struct iowait, list);
1636 iowait_get_priority(wait);
1637 qp = iowait_to_qp(wait);
1638 priv = qp->priv;
1639 list_del_init(&priv->s_iowait.list);
1640 priv->s_iowait.lock = NULL;
1641 if (n) {
1642 priv = qps[top_idx]->priv;
1643 top_idx = iowait_priority_update_top(wait,
1644 &priv->s_iowait,
1645 n, top_idx);
1646 }
1647
1648
1649 qps[n++] = qp;
1650 }
1651
1652
1653
1654
1655 if (n) {
1656 hfi1_sc_wantpiobuf_intr(sc, 0);
1657 if (!list_empty(list))
1658 hfi1_sc_wantpiobuf_intr(sc, 1);
1659 }
1660 write_sequnlock_irqrestore(&sc->waitlock, flags);
1661
1662
1663 if (n)
1664 hfi1_qp_wakeup(qps[top_idx],
1665 RVT_S_WAIT_PIO | HFI1_S_WAIT_PIO_DRAIN);
1666 for (i = 0; i < n; i++)
1667 if (i != top_idx)
1668 hfi1_qp_wakeup(qps[i],
1669 RVT_S_WAIT_PIO | HFI1_S_WAIT_PIO_DRAIN);
1670 }
1671
1672
1673 static inline int fill_code(u64 hw_free)
1674 {
1675 int code = 0;
1676
1677 if (hw_free & CR_STATUS_SMASK)
1678 code |= PRC_STATUS_ERR;
1679 if (hw_free & CR_CREDIT_RETURN_DUE_TO_PBC_SMASK)
1680 code |= PRC_PBC;
1681 if (hw_free & CR_CREDIT_RETURN_DUE_TO_THRESHOLD_SMASK)
1682 code |= PRC_THRESHOLD;
1683 if (hw_free & CR_CREDIT_RETURN_DUE_TO_ERR_SMASK)
1684 code |= PRC_FILL_ERR;
1685 if (hw_free & CR_CREDIT_RETURN_DUE_TO_FORCE_SMASK)
1686 code |= PRC_SC_DISABLE;
1687 return code;
1688 }
1689
1690
1691 #define sent_before(a, b) time_before(a, b)
1692
1693
1694
1695
1696 void sc_release_update(struct send_context *sc)
1697 {
1698 struct pio_buf *pbuf;
1699 u64 hw_free;
1700 u32 head, tail;
1701 unsigned long old_free;
1702 unsigned long free;
1703 unsigned long extra;
1704 unsigned long flags;
1705 int code;
1706
1707 if (!sc)
1708 return;
1709
1710 spin_lock_irqsave(&sc->release_lock, flags);
1711
1712 hw_free = le64_to_cpu(*sc->hw_free);
1713 old_free = sc->free;
1714 extra = (((hw_free & CR_COUNTER_SMASK) >> CR_COUNTER_SHIFT)
1715 - (old_free & CR_COUNTER_MASK))
1716 & CR_COUNTER_MASK;
1717 free = old_free + extra;
1718 trace_hfi1_piofree(sc, extra);
1719
1720
1721 code = -1;
1722 head = READ_ONCE(sc->sr_head);
1723 tail = sc->sr_tail;
1724 while (head != tail) {
1725 pbuf = &sc->sr[tail].pbuf;
1726
1727 if (sent_before(free, pbuf->sent_at)) {
1728
1729 break;
1730 }
1731 if (pbuf->cb) {
1732 if (code < 0)
1733 code = fill_code(hw_free);
1734 (*pbuf->cb)(pbuf->arg, code);
1735 }
1736
1737 tail++;
1738 if (tail >= sc->sr_size)
1739 tail = 0;
1740 }
1741 sc->sr_tail = tail;
1742
1743 smp_wmb();
1744 sc->free = free;
1745 spin_unlock_irqrestore(&sc->release_lock, flags);
1746 sc_piobufavail(sc);
1747 }
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758 void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context)
1759 {
1760 struct send_context *sc;
1761 u32 sw_index;
1762 u32 gc, gc_end;
1763
1764 spin_lock(&dd->sc_lock);
1765 sw_index = dd->hw_to_sw[hw_context];
1766 if (unlikely(sw_index >= dd->num_send_contexts)) {
1767 dd_dev_err(dd, "%s: invalid hw (%u) to sw (%u) mapping\n",
1768 __func__, hw_context, sw_index);
1769 goto done;
1770 }
1771 sc = dd->send_contexts[sw_index].sc;
1772 if (unlikely(!sc))
1773 goto done;
1774
1775 gc = group_context(hw_context, sc->group);
1776 gc_end = gc + group_size(sc->group);
1777 for (; gc < gc_end; gc++) {
1778 sw_index = dd->hw_to_sw[gc];
1779 if (unlikely(sw_index >= dd->num_send_contexts)) {
1780 dd_dev_err(dd,
1781 "%s: invalid hw (%u) to sw (%u) mapping\n",
1782 __func__, hw_context, sw_index);
1783 continue;
1784 }
1785 sc_release_update(dd->send_contexts[sw_index].sc);
1786 }
1787 done:
1788 spin_unlock(&dd->sc_lock);
1789 }
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800 struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
1801 u32 selector, u8 vl)
1802 {
1803 struct pio_vl_map *m;
1804 struct pio_map_elem *e;
1805 struct send_context *rval;
1806
1807
1808
1809
1810
1811
1812 if (unlikely(vl >= num_vls)) {
1813 rval = NULL;
1814 goto done;
1815 }
1816
1817 rcu_read_lock();
1818 m = rcu_dereference(dd->pio_map);
1819 if (unlikely(!m)) {
1820 rcu_read_unlock();
1821 return dd->vld[0].sc;
1822 }
1823 e = m->map[vl & m->mask];
1824 rval = e->ksc[selector & e->mask];
1825 rcu_read_unlock();
1826
1827 done:
1828 rval = !rval ? dd->vld[0].sc : rval;
1829 return rval;
1830 }
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840 struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
1841 u32 selector, u8 sc5)
1842 {
1843 u8 vl = sc_to_vlt(dd, sc5);
1844
1845 return pio_select_send_context_vl(dd, selector, vl);
1846 }
1847
1848
1849
1850
1851 static void pio_map_free(struct pio_vl_map *m)
1852 {
1853 int i;
1854
1855 for (i = 0; m && i < m->actual_vls; i++)
1856 kfree(m->map[i]);
1857 kfree(m);
1858 }
1859
1860
1861
1862
1863 static void pio_map_rcu_callback(struct rcu_head *list)
1864 {
1865 struct pio_vl_map *m = container_of(list, struct pio_vl_map, list);
1866
1867 pio_map_free(m);
1868 }
1869
1870
1871
1872
1873 static void set_threshold(struct hfi1_devdata *dd, int scontext, int i)
1874 {
1875 u32 thres;
1876
1877 thres = min(sc_percent_to_threshold(dd->kernel_send_context[scontext],
1878 50),
1879 sc_mtu_to_threshold(dd->kernel_send_context[scontext],
1880 dd->vld[i].mtu,
1881 dd->rcd[0]->rcvhdrqentsize));
1882 sc_set_cr_threshold(dd->kernel_send_context[scontext], thres);
1883 }
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913 int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls, u8 *vl_scontexts)
1914 {
1915 int i, j;
1916 int extra, sc_per_vl;
1917 int scontext = 1;
1918 int num_kernel_send_contexts = 0;
1919 u8 lvl_scontexts[OPA_MAX_VLS];
1920 struct pio_vl_map *oldmap, *newmap;
1921
1922 if (!vl_scontexts) {
1923 for (i = 0; i < dd->num_send_contexts; i++)
1924 if (dd->send_contexts[i].type == SC_KERNEL)
1925 num_kernel_send_contexts++;
1926
1927 sc_per_vl = num_kernel_send_contexts / num_vls;
1928
1929 extra = num_kernel_send_contexts % num_vls;
1930 vl_scontexts = lvl_scontexts;
1931
1932 for (i = num_vls - 1; i >= 0; i--, extra--)
1933 vl_scontexts[i] = sc_per_vl + (extra > 0 ? 1 : 0);
1934 }
1935
1936 newmap = kzalloc(sizeof(*newmap) +
1937 roundup_pow_of_two(num_vls) *
1938 sizeof(struct pio_map_elem *),
1939 GFP_KERNEL);
1940 if (!newmap)
1941 goto bail;
1942 newmap->actual_vls = num_vls;
1943 newmap->vls = roundup_pow_of_two(num_vls);
1944 newmap->mask = (1 << ilog2(newmap->vls)) - 1;
1945 for (i = 0; i < newmap->vls; i++) {
1946
1947 int first_scontext = scontext;
1948
1949 if (i < newmap->actual_vls) {
1950 int sz = roundup_pow_of_two(vl_scontexts[i]);
1951
1952
1953 newmap->map[i] = kzalloc(sizeof(*newmap->map[i]) +
1954 sz * sizeof(struct
1955 send_context *),
1956 GFP_KERNEL);
1957 if (!newmap->map[i])
1958 goto bail;
1959 newmap->map[i]->mask = (1 << ilog2(sz)) - 1;
1960
1961
1962
1963
1964 for (j = 0; j < sz; j++) {
1965 if (dd->kernel_send_context[scontext]) {
1966 newmap->map[i]->ksc[j] =
1967 dd->kernel_send_context[scontext];
1968 set_threshold(dd, scontext, i);
1969 }
1970 if (++scontext >= first_scontext +
1971 vl_scontexts[i])
1972
1973 scontext = first_scontext;
1974 }
1975 } else {
1976
1977 newmap->map[i] = newmap->map[i % num_vls];
1978 }
1979 scontext = first_scontext + vl_scontexts[i];
1980 }
1981
1982 spin_lock_irq(&dd->pio_map_lock);
1983 oldmap = rcu_dereference_protected(dd->pio_map,
1984 lockdep_is_held(&dd->pio_map_lock));
1985
1986
1987 rcu_assign_pointer(dd->pio_map, newmap);
1988
1989 spin_unlock_irq(&dd->pio_map_lock);
1990
1991 if (oldmap)
1992 call_rcu(&oldmap->list, pio_map_rcu_callback);
1993 return 0;
1994 bail:
1995
1996 pio_map_free(newmap);
1997 return -ENOMEM;
1998 }
1999
2000 void free_pio_map(struct hfi1_devdata *dd)
2001 {
2002
2003 if (rcu_access_pointer(dd->pio_map)) {
2004 spin_lock_irq(&dd->pio_map_lock);
2005 pio_map_free(rcu_access_pointer(dd->pio_map));
2006 RCU_INIT_POINTER(dd->pio_map, NULL);
2007 spin_unlock_irq(&dd->pio_map_lock);
2008 synchronize_rcu();
2009 }
2010 kfree(dd->kernel_send_context);
2011 dd->kernel_send_context = NULL;
2012 }
2013
2014 int init_pervl_scs(struct hfi1_devdata *dd)
2015 {
2016 int i;
2017 u64 mask, all_vl_mask = (u64)0x80ff;
2018 u64 data_vls_mask = (u64)0x00ff;
2019 u32 ctxt;
2020 struct hfi1_pportdata *ppd = dd->pport;
2021
2022 dd->vld[15].sc = sc_alloc(dd, SC_VL15,
2023 dd->rcd[0]->rcvhdrqentsize, dd->node);
2024 if (!dd->vld[15].sc)
2025 return -ENOMEM;
2026
2027 hfi1_init_ctxt(dd->vld[15].sc);
2028 dd->vld[15].mtu = enum_to_mtu(OPA_MTU_2048);
2029
2030 dd->kernel_send_context = kcalloc_node(dd->num_send_contexts,
2031 sizeof(struct send_context *),
2032 GFP_KERNEL, dd->node);
2033 if (!dd->kernel_send_context)
2034 goto freesc15;
2035
2036 dd->kernel_send_context[0] = dd->vld[15].sc;
2037
2038 for (i = 0; i < num_vls; i++) {
2039
2040
2041
2042
2043
2044
2045
2046 dd->vld[i].sc = sc_alloc(dd, SC_KERNEL,
2047 dd->rcd[0]->rcvhdrqentsize, dd->node);
2048 if (!dd->vld[i].sc)
2049 goto nomem;
2050 dd->kernel_send_context[i + 1] = dd->vld[i].sc;
2051 hfi1_init_ctxt(dd->vld[i].sc);
2052
2053 dd->vld[i].mtu = hfi1_max_mtu;
2054 }
2055 for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
2056 dd->kernel_send_context[i + 1] =
2057 sc_alloc(dd, SC_KERNEL, dd->rcd[0]->rcvhdrqentsize, dd->node);
2058 if (!dd->kernel_send_context[i + 1])
2059 goto nomem;
2060 hfi1_init_ctxt(dd->kernel_send_context[i + 1]);
2061 }
2062
2063 sc_enable(dd->vld[15].sc);
2064 ctxt = dd->vld[15].sc->hw_context;
2065 mask = all_vl_mask & ~(1LL << 15);
2066 write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2067 dd_dev_info(dd,
2068 "Using send context %u(%u) for VL15\n",
2069 dd->vld[15].sc->sw_index, ctxt);
2070
2071 for (i = 0; i < num_vls; i++) {
2072 sc_enable(dd->vld[i].sc);
2073 ctxt = dd->vld[i].sc->hw_context;
2074 mask = all_vl_mask & ~(data_vls_mask);
2075 write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2076 }
2077 for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
2078 sc_enable(dd->kernel_send_context[i + 1]);
2079 ctxt = dd->kernel_send_context[i + 1]->hw_context;
2080 mask = all_vl_mask & ~(data_vls_mask);
2081 write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2082 }
2083
2084 if (pio_map_init(dd, ppd->port - 1, num_vls, NULL))
2085 goto nomem;
2086 return 0;
2087
2088 nomem:
2089 for (i = 0; i < num_vls; i++) {
2090 sc_free(dd->vld[i].sc);
2091 dd->vld[i].sc = NULL;
2092 }
2093
2094 for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++)
2095 sc_free(dd->kernel_send_context[i + 1]);
2096
2097 kfree(dd->kernel_send_context);
2098 dd->kernel_send_context = NULL;
2099
2100 freesc15:
2101 sc_free(dd->vld[15].sc);
2102 return -ENOMEM;
2103 }
2104
2105 int init_credit_return(struct hfi1_devdata *dd)
2106 {
2107 int ret;
2108 int i;
2109
2110 dd->cr_base = kcalloc(
2111 node_affinity.num_possible_nodes,
2112 sizeof(struct credit_return_base),
2113 GFP_KERNEL);
2114 if (!dd->cr_base) {
2115 ret = -ENOMEM;
2116 goto done;
2117 }
2118 for_each_node_with_cpus(i) {
2119 int bytes = TXE_NUM_CONTEXTS * sizeof(struct credit_return);
2120
2121 set_dev_node(&dd->pcidev->dev, i);
2122 dd->cr_base[i].va = dma_alloc_coherent(&dd->pcidev->dev,
2123 bytes,
2124 &dd->cr_base[i].dma,
2125 GFP_KERNEL);
2126 if (!dd->cr_base[i].va) {
2127 set_dev_node(&dd->pcidev->dev, dd->node);
2128 dd_dev_err(dd,
2129 "Unable to allocate credit return DMA range for NUMA %d\n",
2130 i);
2131 ret = -ENOMEM;
2132 goto done;
2133 }
2134 }
2135 set_dev_node(&dd->pcidev->dev, dd->node);
2136
2137 ret = 0;
2138 done:
2139 return ret;
2140 }
2141
2142 void free_credit_return(struct hfi1_devdata *dd)
2143 {
2144 int i;
2145
2146 if (!dd->cr_base)
2147 return;
2148 for (i = 0; i < node_affinity.num_possible_nodes; i++) {
2149 if (dd->cr_base[i].va) {
2150 dma_free_coherent(&dd->pcidev->dev,
2151 TXE_NUM_CONTEXTS *
2152 sizeof(struct credit_return),
2153 dd->cr_base[i].va,
2154 dd->cr_base[i].dma);
2155 }
2156 }
2157 kfree(dd->cr_base);
2158 dd->cr_base = NULL;
2159 }
2160
2161 void seqfile_dump_sci(struct seq_file *s, u32 i,
2162 struct send_context_info *sci)
2163 {
2164 struct send_context *sc = sci->sc;
2165 u64 reg;
2166
2167 seq_printf(s, "SCI %u: type %u base %u credits %u\n",
2168 i, sci->type, sci->base, sci->credits);
2169 seq_printf(s, " flags 0x%x sw_inx %u hw_ctxt %u grp %u\n",
2170 sc->flags, sc->sw_index, sc->hw_context, sc->group);
2171 seq_printf(s, " sr_size %u credits %u sr_head %u sr_tail %u\n",
2172 sc->sr_size, sc->credits, sc->sr_head, sc->sr_tail);
2173 seq_printf(s, " fill %lu free %lu fill_wrap %u alloc_free %lu\n",
2174 sc->fill, sc->free, sc->fill_wrap, sc->alloc_free);
2175 seq_printf(s, " credit_intr_count %u credit_ctrl 0x%llx\n",
2176 sc->credit_intr_count, sc->credit_ctrl);
2177 reg = read_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_STATUS));
2178 seq_printf(s, " *hw_free %llu CurrentFree %llu LastReturned %llu\n",
2179 (le64_to_cpu(*sc->hw_free) & CR_COUNTER_SMASK) >>
2180 CR_COUNTER_SHIFT,
2181 (reg >> SC(CREDIT_STATUS_CURRENT_FREE_COUNTER_SHIFT)) &
2182 SC(CREDIT_STATUS_CURRENT_FREE_COUNTER_MASK),
2183 reg & SC(CREDIT_STATUS_LAST_RETURNED_COUNTER_SMASK));
2184 }