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/hash.h>
49#include <linux/bitops.h>
50#include <linux/lockdep.h>
51#include <linux/vmalloc.h>
52#include <linux/slab.h>
53#include <rdma/ib_verbs.h>
54#include <rdma/ib_hdrs.h>
55#include <rdma/opa_addr.h>
56#include <rdma/uverbs_ioctl.h>
57#include "qp.h"
58#include "vt.h"
59#include "trace.h"
60
61static void rvt_rc_timeout(struct timer_list *t);
62
63
64
65
66static const u32 ib_rvt_rnr_table[32] = {
67 655360,
68 10,
69 20,
70 30,
71 40,
72 60,
73 80,
74 120,
75 160,
76 240,
77 320,
78 480,
79 640,
80 960,
81 1280,
82 1920,
83 2560,
84 3840,
85 5120,
86 7680,
87 10240,
88 15360,
89 20480,
90 30720,
91 40960,
92 61440,
93 81920,
94 122880,
95 163840,
96 245760,
97 327680,
98 491520
99};
100
101
102
103
104
105
106const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
107 [IB_QPS_RESET] = 0,
108 [IB_QPS_INIT] = RVT_POST_RECV_OK,
109 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
110 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
111 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
112 RVT_PROCESS_NEXT_SEND_OK,
113 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
114 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
115 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
116 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
117 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
118 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
119};
120EXPORT_SYMBOL(ib_rvt_state_ops);
121
122
123static int rvt_wss_llc_size(void)
124{
125
126 return boot_cpu_data.x86_cache_size;
127}
128
129
130static void cacheless_memcpy(void *dst, void *src, size_t n)
131{
132
133
134
135
136
137
138 __copy_user_nocache(dst, (void __user *)src, n, 0);
139}
140
141void rvt_wss_exit(struct rvt_dev_info *rdi)
142{
143 struct rvt_wss *wss = rdi->wss;
144
145 if (!wss)
146 return;
147
148
149 kfree(wss->entries);
150 wss->entries = NULL;
151 kfree(rdi->wss);
152 rdi->wss = NULL;
153}
154
155
156
157
158
159
160int rvt_wss_init(struct rvt_dev_info *rdi)
161{
162 unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode;
163 unsigned int wss_threshold = rdi->dparms.wss_threshold;
164 unsigned int wss_clean_period = rdi->dparms.wss_clean_period;
165 long llc_size;
166 long llc_bits;
167 long table_size;
168 long table_bits;
169 struct rvt_wss *wss;
170 int node = rdi->dparms.node;
171
172 if (sge_copy_mode != RVT_SGE_COPY_ADAPTIVE) {
173 rdi->wss = NULL;
174 return 0;
175 }
176
177 rdi->wss = kzalloc_node(sizeof(*rdi->wss), GFP_KERNEL, node);
178 if (!rdi->wss)
179 return -ENOMEM;
180 wss = rdi->wss;
181
182
183 if (wss_threshold < 1 || wss_threshold > 100)
184 wss_threshold = 80;
185
186
187 if (wss_clean_period > 1000000)
188 wss_clean_period = 256;
189
190
191 if (wss_clean_period == 0)
192 wss_clean_period = 1;
193
194
195
196
197
198 llc_size = rvt_wss_llc_size() * 1024;
199 table_size = roundup_pow_of_two(llc_size);
200
201
202 llc_bits = llc_size / PAGE_SIZE;
203 table_bits = table_size / PAGE_SIZE;
204 wss->pages_mask = table_bits - 1;
205 wss->num_entries = table_bits / BITS_PER_LONG;
206
207 wss->threshold = (llc_bits * wss_threshold) / 100;
208 if (wss->threshold == 0)
209 wss->threshold = 1;
210
211 wss->clean_period = wss_clean_period;
212 atomic_set(&wss->clean_counter, wss_clean_period);
213
214 wss->entries = kcalloc_node(wss->num_entries, sizeof(*wss->entries),
215 GFP_KERNEL, node);
216 if (!wss->entries) {
217 rvt_wss_exit(rdi);
218 return -ENOMEM;
219 }
220
221 return 0;
222}
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239static void wss_advance_clean_counter(struct rvt_wss *wss)
240{
241 int entry;
242 int weight;
243 unsigned long bits;
244
245
246 if (atomic_dec_and_test(&wss->clean_counter)) {
247
248
249
250
251
252
253
254
255
256
257
258
259
260 atomic_set(&wss->clean_counter, wss->clean_period);
261
262
263
264
265
266
267
268 entry = (atomic_inc_return(&wss->clean_entry) - 1)
269 & (wss->num_entries - 1);
270
271
272 bits = xchg(&wss->entries[entry], 0);
273 weight = hweight64((u64)bits);
274
275 if (weight)
276 atomic_sub(weight, &wss->total_count);
277 }
278}
279
280
281
282
283static void wss_insert(struct rvt_wss *wss, void *address)
284{
285 u32 page = ((unsigned long)address >> PAGE_SHIFT) & wss->pages_mask;
286 u32 entry = page / BITS_PER_LONG;
287 u32 nr = page & (BITS_PER_LONG - 1);
288
289 if (!test_and_set_bit(nr, &wss->entries[entry]))
290 atomic_inc(&wss->total_count);
291
292 wss_advance_clean_counter(wss);
293}
294
295
296
297
298static inline bool wss_exceeds_threshold(struct rvt_wss *wss)
299{
300 return atomic_read(&wss->total_count) >= wss->threshold;
301}
302
303static void get_map_page(struct rvt_qpn_table *qpt,
304 struct rvt_qpn_map *map)
305{
306 unsigned long page = get_zeroed_page(GFP_KERNEL);
307
308
309
310
311
312 spin_lock(&qpt->lock);
313 if (map->page)
314 free_page(page);
315 else
316 map->page = (void *)page;
317 spin_unlock(&qpt->lock);
318}
319
320
321
322
323
324static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
325{
326 u32 offset, i;
327 struct rvt_qpn_map *map;
328 int ret = 0;
329
330 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
331 return -EINVAL;
332
333 spin_lock_init(&qpt->lock);
334
335 qpt->last = rdi->dparms.qpn_start;
336 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
337
338
339
340
341
342
343
344
345
346 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
347
348
349 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
350
351
352 map = &qpt->map[qpt->nmaps];
353
354 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
355 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
356 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
357 if (!map->page) {
358 get_map_page(qpt, map);
359 if (!map->page) {
360 ret = -ENOMEM;
361 break;
362 }
363 }
364 set_bit(offset, map->page);
365 offset++;
366 if (offset == RVT_BITS_PER_PAGE) {
367
368 qpt->nmaps++;
369 map++;
370 offset = 0;
371 }
372 }
373 return ret;
374}
375
376
377
378
379
380static void free_qpn_table(struct rvt_qpn_table *qpt)
381{
382 int i;
383
384 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
385 free_page((unsigned long)qpt->map[i].page);
386}
387
388
389
390
391
392
393
394int rvt_driver_qp_init(struct rvt_dev_info *rdi)
395{
396 int i;
397 int ret = -ENOMEM;
398
399 if (!rdi->dparms.qp_table_size)
400 return -EINVAL;
401
402
403
404
405
406 if (!rdi->driver_f.free_all_qps ||
407 !rdi->driver_f.qp_priv_alloc ||
408 !rdi->driver_f.qp_priv_free ||
409 !rdi->driver_f.notify_qp_reset ||
410 !rdi->driver_f.notify_restart_rc)
411 return -EINVAL;
412
413
414 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
415 rdi->dparms.node);
416 if (!rdi->qp_dev)
417 return -ENOMEM;
418
419
420 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
421 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
422 rdi->qp_dev->qp_table =
423 kmalloc_array_node(rdi->qp_dev->qp_table_size,
424 sizeof(*rdi->qp_dev->qp_table),
425 GFP_KERNEL, rdi->dparms.node);
426 if (!rdi->qp_dev->qp_table)
427 goto no_qp_table;
428
429 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
430 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
431
432 spin_lock_init(&rdi->qp_dev->qpt_lock);
433
434
435 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
436 goto fail_table;
437
438 spin_lock_init(&rdi->n_qps_lock);
439
440 return 0;
441
442fail_table:
443 kfree(rdi->qp_dev->qp_table);
444 free_qpn_table(&rdi->qp_dev->qpn_table);
445
446no_qp_table:
447 kfree(rdi->qp_dev);
448
449 return ret;
450}
451
452
453
454
455
456
457
458
459static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
460{
461 unsigned long flags;
462 struct rvt_qp *qp;
463 unsigned n, qp_inuse = 0;
464 spinlock_t *ql;
465
466 if (rdi->driver_f.free_all_qps)
467 qp_inuse = rdi->driver_f.free_all_qps(rdi);
468
469 qp_inuse += rvt_mcast_tree_empty(rdi);
470
471 if (!rdi->qp_dev)
472 return qp_inuse;
473
474 ql = &rdi->qp_dev->qpt_lock;
475 spin_lock_irqsave(ql, flags);
476 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
477 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
478 lockdep_is_held(ql));
479 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
480
481 for (; qp; qp = rcu_dereference_protected(qp->next,
482 lockdep_is_held(ql)))
483 qp_inuse++;
484 }
485 spin_unlock_irqrestore(ql, flags);
486 synchronize_rcu();
487 return qp_inuse;
488}
489
490
491
492
493
494
495
496void rvt_qp_exit(struct rvt_dev_info *rdi)
497{
498 u32 qps_inuse = rvt_free_all_qps(rdi);
499
500 if (qps_inuse)
501 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
502 qps_inuse);
503 if (!rdi->qp_dev)
504 return;
505
506 kfree(rdi->qp_dev->qp_table);
507 free_qpn_table(&rdi->qp_dev->qpn_table);
508 kfree(rdi->qp_dev);
509}
510
511static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
512 struct rvt_qpn_map *map, unsigned off)
513{
514 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
515}
516
517
518
519
520
521
522
523
524
525
526static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
527 enum ib_qp_type type, u8 port_num)
528{
529 u32 i, offset, max_scan, qpn;
530 struct rvt_qpn_map *map;
531 u32 ret;
532
533 if (rdi->driver_f.alloc_qpn)
534 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
535
536 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
537 unsigned n;
538
539 ret = type == IB_QPT_GSI;
540 n = 1 << (ret + 2 * (port_num - 1));
541 spin_lock(&qpt->lock);
542 if (qpt->flags & n)
543 ret = -EINVAL;
544 else
545 qpt->flags |= n;
546 spin_unlock(&qpt->lock);
547 goto bail;
548 }
549
550 qpn = qpt->last + qpt->incr;
551 if (qpn >= RVT_QPN_MAX)
552 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
553
554 offset = qpn & RVT_BITS_PER_PAGE_MASK;
555 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
556 max_scan = qpt->nmaps - !offset;
557 for (i = 0;;) {
558 if (unlikely(!map->page)) {
559 get_map_page(qpt, map);
560 if (unlikely(!map->page))
561 break;
562 }
563 do {
564 if (!test_and_set_bit(offset, map->page)) {
565 qpt->last = qpn;
566 ret = qpn;
567 goto bail;
568 }
569 offset += qpt->incr;
570
571
572
573
574 qpn = mk_qpn(qpt, map, offset);
575 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
576
577
578
579
580
581 if (++i > max_scan) {
582 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
583 break;
584 map = &qpt->map[qpt->nmaps++];
585
586 offset = qpt->incr | (offset & 1);
587 } else if (map < &qpt->map[qpt->nmaps]) {
588 ++map;
589
590 offset = qpt->incr | (offset & 1);
591 } else {
592 map = &qpt->map[0];
593
594 offset = qpt->incr | ((offset & 1) ^ 1);
595 }
596
597 WARN_ON(rdi->dparms.qos_shift > 1 &&
598 offset & ((BIT(rdi->dparms.qos_shift - 1) - 1) << 1));
599 qpn = mk_qpn(qpt, map, offset);
600 }
601
602 ret = -ENOMEM;
603
604bail:
605 return ret;
606}
607
608
609
610
611
612
613static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
614{
615 unsigned n;
616 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
617
618 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
619 rvt_put_ss(&qp->s_rdma_read_sge);
620
621 rvt_put_ss(&qp->r_sge);
622
623 if (clr_sends) {
624 while (qp->s_last != qp->s_head) {
625 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
626
627 rvt_put_qp_swqe(qp, wqe);
628 if (++qp->s_last >= qp->s_size)
629 qp->s_last = 0;
630 smp_wmb();
631 }
632 if (qp->s_rdma_mr) {
633 rvt_put_mr(qp->s_rdma_mr);
634 qp->s_rdma_mr = NULL;
635 }
636 }
637
638 for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) {
639 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
640
641 if (e->rdma_sge.mr) {
642 rvt_put_mr(e->rdma_sge.mr);
643 e->rdma_sge.mr = NULL;
644 }
645 }
646}
647
648
649
650
651
652
653
654
655static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey)
656{
657 int i;
658
659 for (i = 0; i < wqe->wr.num_sge; i++) {
660 struct rvt_sge *sge = &wqe->sg_list[i];
661
662 if (rvt_mr_has_lkey(sge->mr, lkey))
663 return true;
664 }
665 return false;
666}
667
668
669
670
671
672
673static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey)
674{
675 u32 s_last = qp->s_last;
676
677 while (s_last != qp->s_head) {
678 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last);
679
680 if (rvt_swqe_has_lkey(wqe, lkey))
681 return true;
682
683 if (++s_last >= qp->s_size)
684 s_last = 0;
685 }
686 if (qp->s_rdma_mr)
687 if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey))
688 return true;
689 return false;
690}
691
692
693
694
695
696
697static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey)
698{
699 int i;
700 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
701
702 for (i = 0; qp->s_ack_queue && i < rvt_max_atomic(rdi); i++) {
703 struct rvt_ack_entry *e = &qp->s_ack_queue[i];
704
705 if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey))
706 return true;
707 }
708 return false;
709}
710
711
712
713
714
715
716
717
718
719
720
721
722void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey)
723{
724 bool lastwqe = false;
725
726 if (qp->ibqp.qp_type == IB_QPT_SMI ||
727 qp->ibqp.qp_type == IB_QPT_GSI)
728
729 return;
730 spin_lock_irq(&qp->r_lock);
731 spin_lock(&qp->s_hlock);
732 spin_lock(&qp->s_lock);
733
734 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
735 goto check_lwqe;
736
737 if (rvt_ss_has_lkey(&qp->r_sge, lkey) ||
738 rvt_qp_sends_has_lkey(qp, lkey) ||
739 rvt_qp_acks_has_lkey(qp, lkey))
740 lastwqe = rvt_error_qp(qp, IB_WC_LOC_PROT_ERR);
741check_lwqe:
742 spin_unlock(&qp->s_lock);
743 spin_unlock(&qp->s_hlock);
744 spin_unlock_irq(&qp->r_lock);
745 if (lastwqe) {
746 struct ib_event ev;
747
748 ev.device = qp->ibqp.device;
749 ev.element.qp = &qp->ibqp;
750 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
751 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
752 }
753}
754
755
756
757
758
759
760
761
762
763static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
764{
765 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
766 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
767 unsigned long flags;
768 int removed = 1;
769
770 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
771
772 if (rcu_dereference_protected(rvp->qp[0],
773 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
774 RCU_INIT_POINTER(rvp->qp[0], NULL);
775 } else if (rcu_dereference_protected(rvp->qp[1],
776 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
777 RCU_INIT_POINTER(rvp->qp[1], NULL);
778 } else {
779 struct rvt_qp *q;
780 struct rvt_qp __rcu **qpp;
781
782 removed = 0;
783 qpp = &rdi->qp_dev->qp_table[n];
784 for (; (q = rcu_dereference_protected(*qpp,
785 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
786 qpp = &q->next) {
787 if (q == qp) {
788 RCU_INIT_POINTER(*qpp,
789 rcu_dereference_protected(qp->next,
790 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
791 removed = 1;
792 trace_rvt_qpremove(qp, n);
793 break;
794 }
795 }
796 }
797
798 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
799 if (removed) {
800 synchronize_rcu();
801 rvt_put_qp(qp);
802 }
803}
804
805
806
807
808
809
810
811
812
813
814
815static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
816 enum ib_qp_type type)
817{
818 qp->remote_qpn = 0;
819 qp->qkey = 0;
820 qp->qp_access_flags = 0;
821 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
822 qp->s_hdrwords = 0;
823 qp->s_wqe = NULL;
824 qp->s_draining = 0;
825 qp->s_next_psn = 0;
826 qp->s_last_psn = 0;
827 qp->s_sending_psn = 0;
828 qp->s_sending_hpsn = 0;
829 qp->s_psn = 0;
830 qp->r_psn = 0;
831 qp->r_msn = 0;
832 if (type == IB_QPT_RC) {
833 qp->s_state = IB_OPCODE_RC_SEND_LAST;
834 qp->r_state = IB_OPCODE_RC_SEND_LAST;
835 } else {
836 qp->s_state = IB_OPCODE_UC_SEND_LAST;
837 qp->r_state = IB_OPCODE_UC_SEND_LAST;
838 }
839 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
840 qp->r_nak_state = 0;
841 qp->r_aflags = 0;
842 qp->r_flags = 0;
843 qp->s_head = 0;
844 qp->s_tail = 0;
845 qp->s_cur = 0;
846 qp->s_acked = 0;
847 qp->s_last = 0;
848 qp->s_ssn = 1;
849 qp->s_lsn = 0;
850 qp->s_mig_state = IB_MIG_MIGRATED;
851 qp->r_head_ack_queue = 0;
852 qp->s_tail_ack_queue = 0;
853 qp->s_acked_ack_queue = 0;
854 qp->s_num_rd_atomic = 0;
855 if (qp->r_rq.wq) {
856 qp->r_rq.wq->head = 0;
857 qp->r_rq.wq->tail = 0;
858 }
859 qp->r_sge.num_sge = 0;
860 atomic_set(&qp->s_reserved_used, 0);
861}
862
863
864
865
866
867
868
869
870static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
871 enum ib_qp_type type)
872 __must_hold(&qp->s_lock)
873 __must_hold(&qp->s_hlock)
874 __must_hold(&qp->r_lock)
875{
876 lockdep_assert_held(&qp->r_lock);
877 lockdep_assert_held(&qp->s_hlock);
878 lockdep_assert_held(&qp->s_lock);
879 if (qp->state != IB_QPS_RESET) {
880 qp->state = IB_QPS_RESET;
881
882
883 rdi->driver_f.flush_qp_waiters(qp);
884 rvt_stop_rc_timers(qp);
885 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
886 spin_unlock(&qp->s_lock);
887 spin_unlock(&qp->s_hlock);
888 spin_unlock_irq(&qp->r_lock);
889
890
891 rdi->driver_f.stop_send_queue(qp);
892 rvt_del_timers_sync(qp);
893
894 rdi->driver_f.quiesce_qp(qp);
895
896
897 rvt_remove_qp(rdi, qp);
898
899
900 spin_lock_irq(&qp->r_lock);
901 spin_lock(&qp->s_hlock);
902 spin_lock(&qp->s_lock);
903
904 rvt_clear_mr_refs(qp, 1);
905
906
907
908
909 rdi->driver_f.notify_qp_reset(qp);
910 }
911 rvt_init_qp(rdi, qp, type);
912 lockdep_assert_held(&qp->r_lock);
913 lockdep_assert_held(&qp->s_hlock);
914 lockdep_assert_held(&qp->s_lock);
915}
916
917
918
919
920
921static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
922{
923 struct rvt_qpn_map *map;
924
925 map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
926 if (map->page)
927 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
928}
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
945 struct ib_qp_init_attr *init_attr,
946 struct ib_udata *udata)
947{
948 struct rvt_qp *qp;
949 int err;
950 struct rvt_swqe *swq = NULL;
951 size_t sz;
952 size_t sg_list_sz;
953 struct ib_qp *ret = ERR_PTR(-ENOMEM);
954 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
955 void *priv = NULL;
956 size_t sqsize;
957
958 if (!rdi)
959 return ERR_PTR(-EINVAL);
960
961 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_send_sge ||
962 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
963 init_attr->create_flags)
964 return ERR_PTR(-EINVAL);
965
966
967 if (!init_attr->srq) {
968 if (init_attr->cap.max_recv_sge >
969 rdi->dparms.props.max_recv_sge ||
970 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
971 return ERR_PTR(-EINVAL);
972
973 if (init_attr->cap.max_send_sge +
974 init_attr->cap.max_send_wr +
975 init_attr->cap.max_recv_sge +
976 init_attr->cap.max_recv_wr == 0)
977 return ERR_PTR(-EINVAL);
978 }
979 sqsize =
980 init_attr->cap.max_send_wr + 1 +
981 rdi->dparms.reserved_operations;
982 switch (init_attr->qp_type) {
983 case IB_QPT_SMI:
984 case IB_QPT_GSI:
985 if (init_attr->port_num == 0 ||
986 init_attr->port_num > ibpd->device->phys_port_cnt)
987 return ERR_PTR(-EINVAL);
988
989 case IB_QPT_UC:
990 case IB_QPT_RC:
991 case IB_QPT_UD:
992 sz = sizeof(struct rvt_sge) *
993 init_attr->cap.max_send_sge +
994 sizeof(struct rvt_swqe);
995 swq = vzalloc_node(array_size(sz, sqsize), rdi->dparms.node);
996 if (!swq)
997 return ERR_PTR(-ENOMEM);
998
999 sz = sizeof(*qp);
1000 sg_list_sz = 0;
1001 if (init_attr->srq) {
1002 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
1003
1004 if (srq->rq.max_sge > 1)
1005 sg_list_sz = sizeof(*qp->r_sg_list) *
1006 (srq->rq.max_sge - 1);
1007 } else if (init_attr->cap.max_recv_sge > 1)
1008 sg_list_sz = sizeof(*qp->r_sg_list) *
1009 (init_attr->cap.max_recv_sge - 1);
1010 qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
1011 rdi->dparms.node);
1012 if (!qp)
1013 goto bail_swq;
1014
1015 RCU_INIT_POINTER(qp->next, NULL);
1016 if (init_attr->qp_type == IB_QPT_RC) {
1017 qp->s_ack_queue =
1018 kcalloc_node(rvt_max_atomic(rdi),
1019 sizeof(*qp->s_ack_queue),
1020 GFP_KERNEL,
1021 rdi->dparms.node);
1022 if (!qp->s_ack_queue)
1023 goto bail_qp;
1024 }
1025
1026 timer_setup(&qp->s_timer, rvt_rc_timeout, 0);
1027 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
1028 HRTIMER_MODE_REL);
1029 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
1030
1031
1032
1033
1034
1035 priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
1036 if (IS_ERR(priv)) {
1037 ret = priv;
1038 goto bail_qp;
1039 }
1040 qp->priv = priv;
1041 qp->timeout_jiffies =
1042 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1043 1000UL);
1044 if (init_attr->srq) {
1045 sz = 0;
1046 } else {
1047 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
1048 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
1049 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
1050 sizeof(struct rvt_rwqe);
1051 if (udata)
1052 qp->r_rq.wq = vmalloc_user(
1053 sizeof(struct rvt_rwq) +
1054 qp->r_rq.size * sz);
1055 else
1056 qp->r_rq.wq = vzalloc_node(
1057 sizeof(struct rvt_rwq) +
1058 qp->r_rq.size * sz,
1059 rdi->dparms.node);
1060 if (!qp->r_rq.wq)
1061 goto bail_driver_priv;
1062 }
1063
1064
1065
1066
1067
1068 spin_lock_init(&qp->r_lock);
1069 spin_lock_init(&qp->s_hlock);
1070 spin_lock_init(&qp->s_lock);
1071 spin_lock_init(&qp->r_rq.lock);
1072 atomic_set(&qp->refcount, 0);
1073 atomic_set(&qp->local_ops_pending, 0);
1074 init_waitqueue_head(&qp->wait);
1075 INIT_LIST_HEAD(&qp->rspwait);
1076 qp->state = IB_QPS_RESET;
1077 qp->s_wq = swq;
1078 qp->s_size = sqsize;
1079 qp->s_avail = init_attr->cap.max_send_wr;
1080 qp->s_max_sge = init_attr->cap.max_send_sge;
1081 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
1082 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
1083
1084 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
1085 init_attr->qp_type,
1086 init_attr->port_num);
1087 if (err < 0) {
1088 ret = ERR_PTR(err);
1089 goto bail_rq_wq;
1090 }
1091 qp->ibqp.qp_num = err;
1092 qp->port_num = init_attr->port_num;
1093 rvt_init_qp(rdi, qp, init_attr->qp_type);
1094 if (rdi->driver_f.qp_priv_init) {
1095 err = rdi->driver_f.qp_priv_init(rdi, qp, init_attr);
1096 if (err) {
1097 ret = ERR_PTR(err);
1098 goto bail_rq_wq;
1099 }
1100 }
1101 break;
1102
1103 default:
1104
1105 return ERR_PTR(-EINVAL);
1106 }
1107
1108 init_attr->cap.max_inline_data = 0;
1109
1110
1111
1112
1113
1114 if (udata && udata->outlen >= sizeof(__u64)) {
1115 if (!qp->r_rq.wq) {
1116 __u64 offset = 0;
1117
1118 err = ib_copy_to_udata(udata, &offset,
1119 sizeof(offset));
1120 if (err) {
1121 ret = ERR_PTR(err);
1122 goto bail_qpn;
1123 }
1124 } else {
1125 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
1126
1127 qp->ip = rvt_create_mmap_info(rdi, s, udata,
1128 qp->r_rq.wq);
1129 if (!qp->ip) {
1130 ret = ERR_PTR(-ENOMEM);
1131 goto bail_qpn;
1132 }
1133
1134 err = ib_copy_to_udata(udata, &qp->ip->offset,
1135 sizeof(qp->ip->offset));
1136 if (err) {
1137 ret = ERR_PTR(err);
1138 goto bail_ip;
1139 }
1140 }
1141 qp->pid = current->pid;
1142 }
1143
1144 spin_lock(&rdi->n_qps_lock);
1145 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
1146 spin_unlock(&rdi->n_qps_lock);
1147 ret = ERR_PTR(-ENOMEM);
1148 goto bail_ip;
1149 }
1150
1151 rdi->n_qps_allocated++;
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 if (init_attr->qp_type == IB_QPT_RC) {
1162 rdi->n_rc_qps++;
1163 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1164 }
1165 spin_unlock(&rdi->n_qps_lock);
1166
1167 if (qp->ip) {
1168 spin_lock_irq(&rdi->pending_lock);
1169 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
1170 spin_unlock_irq(&rdi->pending_lock);
1171 }
1172
1173 ret = &qp->ibqp;
1174
1175
1176
1177
1178
1179
1180 switch (init_attr->qp_type) {
1181 case IB_QPT_SMI:
1182 case IB_QPT_GSI:
1183 case IB_QPT_UD:
1184 qp->allowed_ops = IB_OPCODE_UD;
1185 break;
1186 case IB_QPT_RC:
1187 qp->allowed_ops = IB_OPCODE_RC;
1188 break;
1189 case IB_QPT_UC:
1190 qp->allowed_ops = IB_OPCODE_UC;
1191 break;
1192 default:
1193 ret = ERR_PTR(-EINVAL);
1194 goto bail_ip;
1195 }
1196
1197 return ret;
1198
1199bail_ip:
1200 if (qp->ip)
1201 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1202
1203bail_qpn:
1204 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1205
1206bail_rq_wq:
1207 if (!qp->ip)
1208 vfree(qp->r_rq.wq);
1209
1210bail_driver_priv:
1211 rdi->driver_f.qp_priv_free(rdi, qp);
1212
1213bail_qp:
1214 kfree(qp->s_ack_queue);
1215 kfree(qp);
1216
1217bail_swq:
1218 vfree(swq);
1219
1220 return ret;
1221}
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
1235{
1236 struct ib_wc wc;
1237 int ret = 0;
1238 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1239
1240 lockdep_assert_held(&qp->r_lock);
1241 lockdep_assert_held(&qp->s_lock);
1242 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
1243 goto bail;
1244
1245 qp->state = IB_QPS_ERR;
1246
1247 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1248 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1249 del_timer(&qp->s_timer);
1250 }
1251
1252 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
1253 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
1254
1255 rdi->driver_f.notify_error_qp(qp);
1256
1257
1258 if (READ_ONCE(qp->s_last) != qp->s_head)
1259 rdi->driver_f.schedule_send(qp);
1260
1261 rvt_clear_mr_refs(qp, 0);
1262
1263 memset(&wc, 0, sizeof(wc));
1264 wc.qp = &qp->ibqp;
1265 wc.opcode = IB_WC_RECV;
1266
1267 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1268 wc.wr_id = qp->r_wr_id;
1269 wc.status = err;
1270 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1271 }
1272 wc.status = IB_WC_WR_FLUSH_ERR;
1273
1274 if (qp->r_rq.wq) {
1275 struct rvt_rwq *wq;
1276 u32 head;
1277 u32 tail;
1278
1279 spin_lock(&qp->r_rq.lock);
1280
1281
1282 wq = qp->r_rq.wq;
1283 head = wq->head;
1284 if (head >= qp->r_rq.size)
1285 head = 0;
1286 tail = wq->tail;
1287 if (tail >= qp->r_rq.size)
1288 tail = 0;
1289 while (tail != head) {
1290 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1291 if (++tail >= qp->r_rq.size)
1292 tail = 0;
1293 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1294 }
1295 wq->tail = tail;
1296
1297 spin_unlock(&qp->r_rq.lock);
1298 } else if (qp->ibqp.event_handler) {
1299 ret = 1;
1300 }
1301
1302bail:
1303 return ret;
1304}
1305EXPORT_SYMBOL(rvt_error_qp);
1306
1307
1308
1309
1310
1311static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1312{
1313 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1314 unsigned long flags;
1315
1316 rvt_get_qp(qp);
1317 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1318
1319 if (qp->ibqp.qp_num <= 1) {
1320 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1321 } else {
1322 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1323
1324 qp->next = rdi->qp_dev->qp_table[n];
1325 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1326 trace_rvt_qpinsert(qp, n);
1327 }
1328
1329 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1330}
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1342 int attr_mask, struct ib_udata *udata)
1343{
1344 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1345 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1346 enum ib_qp_state cur_state, new_state;
1347 struct ib_event ev;
1348 int lastwqe = 0;
1349 int mig = 0;
1350 int pmtu = 0;
1351 int opa_ah;
1352
1353 spin_lock_irq(&qp->r_lock);
1354 spin_lock(&qp->s_hlock);
1355 spin_lock(&qp->s_lock);
1356
1357 cur_state = attr_mask & IB_QP_CUR_STATE ?
1358 attr->cur_qp_state : qp->state;
1359 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1360 opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
1361
1362 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1363 attr_mask))
1364 goto inval;
1365
1366 if (rdi->driver_f.check_modify_qp &&
1367 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1368 goto inval;
1369
1370 if (attr_mask & IB_QP_AV) {
1371 if (opa_ah) {
1372 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1373 opa_get_mcast_base(OPA_MCAST_NR))
1374 goto inval;
1375 } else {
1376 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1377 be16_to_cpu(IB_MULTICAST_LID_BASE))
1378 goto inval;
1379 }
1380
1381 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1382 goto inval;
1383 }
1384
1385 if (attr_mask & IB_QP_ALT_PATH) {
1386 if (opa_ah) {
1387 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1388 opa_get_mcast_base(OPA_MCAST_NR))
1389 goto inval;
1390 } else {
1391 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1392 be16_to_cpu(IB_MULTICAST_LID_BASE))
1393 goto inval;
1394 }
1395
1396 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1397 goto inval;
1398 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1399 goto inval;
1400 }
1401
1402 if (attr_mask & IB_QP_PKEY_INDEX)
1403 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1404 goto inval;
1405
1406 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1407 if (attr->min_rnr_timer > 31)
1408 goto inval;
1409
1410 if (attr_mask & IB_QP_PORT)
1411 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1412 qp->ibqp.qp_type == IB_QPT_GSI ||
1413 attr->port_num == 0 ||
1414 attr->port_num > ibqp->device->phys_port_cnt)
1415 goto inval;
1416
1417 if (attr_mask & IB_QP_DEST_QPN)
1418 if (attr->dest_qp_num > RVT_QPN_MASK)
1419 goto inval;
1420
1421 if (attr_mask & IB_QP_RETRY_CNT)
1422 if (attr->retry_cnt > 7)
1423 goto inval;
1424
1425 if (attr_mask & IB_QP_RNR_RETRY)
1426 if (attr->rnr_retry > 7)
1427 goto inval;
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 if (attr_mask & IB_QP_PATH_MTU) {
1438 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1439 if (pmtu < 0)
1440 goto inval;
1441 }
1442
1443 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1444 if (attr->path_mig_state == IB_MIG_REARM) {
1445 if (qp->s_mig_state == IB_MIG_ARMED)
1446 goto inval;
1447 if (new_state != IB_QPS_RTS)
1448 goto inval;
1449 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1450 if (qp->s_mig_state == IB_MIG_REARM)
1451 goto inval;
1452 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1453 goto inval;
1454 if (qp->s_mig_state == IB_MIG_ARMED)
1455 mig = 1;
1456 } else {
1457 goto inval;
1458 }
1459 }
1460
1461 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1462 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1463 goto inval;
1464
1465 switch (new_state) {
1466 case IB_QPS_RESET:
1467 if (qp->state != IB_QPS_RESET)
1468 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1469 break;
1470
1471 case IB_QPS_RTR:
1472
1473 qp->r_flags &= ~RVT_R_COMM_EST;
1474 qp->state = new_state;
1475 break;
1476
1477 case IB_QPS_SQD:
1478 qp->s_draining = qp->s_last != qp->s_cur;
1479 qp->state = new_state;
1480 break;
1481
1482 case IB_QPS_SQE:
1483 if (qp->ibqp.qp_type == IB_QPT_RC)
1484 goto inval;
1485 qp->state = new_state;
1486 break;
1487
1488 case IB_QPS_ERR:
1489 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1490 break;
1491
1492 default:
1493 qp->state = new_state;
1494 break;
1495 }
1496
1497 if (attr_mask & IB_QP_PKEY_INDEX)
1498 qp->s_pkey_index = attr->pkey_index;
1499
1500 if (attr_mask & IB_QP_PORT)
1501 qp->port_num = attr->port_num;
1502
1503 if (attr_mask & IB_QP_DEST_QPN)
1504 qp->remote_qpn = attr->dest_qp_num;
1505
1506 if (attr_mask & IB_QP_SQ_PSN) {
1507 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1508 qp->s_psn = qp->s_next_psn;
1509 qp->s_sending_psn = qp->s_next_psn;
1510 qp->s_last_psn = qp->s_next_psn - 1;
1511 qp->s_sending_hpsn = qp->s_last_psn;
1512 }
1513
1514 if (attr_mask & IB_QP_RQ_PSN)
1515 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1516
1517 if (attr_mask & IB_QP_ACCESS_FLAGS)
1518 qp->qp_access_flags = attr->qp_access_flags;
1519
1520 if (attr_mask & IB_QP_AV) {
1521 rdma_replace_ah_attr(&qp->remote_ah_attr, &attr->ah_attr);
1522 qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
1523 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1524 }
1525
1526 if (attr_mask & IB_QP_ALT_PATH) {
1527 rdma_replace_ah_attr(&qp->alt_ah_attr, &attr->alt_ah_attr);
1528 qp->s_alt_pkey_index = attr->alt_pkey_index;
1529 }
1530
1531 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1532 qp->s_mig_state = attr->path_mig_state;
1533 if (mig) {
1534 qp->remote_ah_attr = qp->alt_ah_attr;
1535 qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
1536 qp->s_pkey_index = qp->s_alt_pkey_index;
1537 }
1538 }
1539
1540 if (attr_mask & IB_QP_PATH_MTU) {
1541 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1542 qp->log_pmtu = ilog2(qp->pmtu);
1543 }
1544
1545 if (attr_mask & IB_QP_RETRY_CNT) {
1546 qp->s_retry_cnt = attr->retry_cnt;
1547 qp->s_retry = attr->retry_cnt;
1548 }
1549
1550 if (attr_mask & IB_QP_RNR_RETRY) {
1551 qp->s_rnr_retry_cnt = attr->rnr_retry;
1552 qp->s_rnr_retry = attr->rnr_retry;
1553 }
1554
1555 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1556 qp->r_min_rnr_timer = attr->min_rnr_timer;
1557
1558 if (attr_mask & IB_QP_TIMEOUT) {
1559 qp->timeout = attr->timeout;
1560 qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
1561 }
1562
1563 if (attr_mask & IB_QP_QKEY)
1564 qp->qkey = attr->qkey;
1565
1566 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1567 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1568
1569 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1570 qp->s_max_rd_atomic = attr->max_rd_atomic;
1571
1572 if (rdi->driver_f.modify_qp)
1573 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1574
1575 spin_unlock(&qp->s_lock);
1576 spin_unlock(&qp->s_hlock);
1577 spin_unlock_irq(&qp->r_lock);
1578
1579 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1580 rvt_insert_qp(rdi, qp);
1581
1582 if (lastwqe) {
1583 ev.device = qp->ibqp.device;
1584 ev.element.qp = &qp->ibqp;
1585 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1586 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1587 }
1588 if (mig) {
1589 ev.device = qp->ibqp.device;
1590 ev.element.qp = &qp->ibqp;
1591 ev.event = IB_EVENT_PATH_MIG;
1592 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1593 }
1594 return 0;
1595
1596inval:
1597 spin_unlock(&qp->s_lock);
1598 spin_unlock(&qp->s_hlock);
1599 spin_unlock_irq(&qp->r_lock);
1600 return -EINVAL;
1601}
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612int rvt_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
1613{
1614 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1615 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1616
1617 spin_lock_irq(&qp->r_lock);
1618 spin_lock(&qp->s_hlock);
1619 spin_lock(&qp->s_lock);
1620 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1621 spin_unlock(&qp->s_lock);
1622 spin_unlock(&qp->s_hlock);
1623 spin_unlock_irq(&qp->r_lock);
1624
1625 wait_event(qp->wait, !atomic_read(&qp->refcount));
1626
1627 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1628
1629 spin_lock(&rdi->n_qps_lock);
1630 rdi->n_qps_allocated--;
1631 if (qp->ibqp.qp_type == IB_QPT_RC) {
1632 rdi->n_rc_qps--;
1633 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1634 }
1635 spin_unlock(&rdi->n_qps_lock);
1636
1637 if (qp->ip)
1638 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1639 else
1640 vfree(qp->r_rq.wq);
1641 rdi->driver_f.qp_priv_free(rdi, qp);
1642 kfree(qp->s_ack_queue);
1643 rdma_destroy_ah_attr(&qp->remote_ah_attr);
1644 rdma_destroy_ah_attr(&qp->alt_ah_attr);
1645 vfree(qp->s_wq);
1646 kfree(qp);
1647 return 0;
1648}
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1660 int attr_mask, struct ib_qp_init_attr *init_attr)
1661{
1662 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1663 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1664
1665 attr->qp_state = qp->state;
1666 attr->cur_qp_state = attr->qp_state;
1667 attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
1668 attr->path_mig_state = qp->s_mig_state;
1669 attr->qkey = qp->qkey;
1670 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1671 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1672 attr->dest_qp_num = qp->remote_qpn;
1673 attr->qp_access_flags = qp->qp_access_flags;
1674 attr->cap.max_send_wr = qp->s_size - 1 -
1675 rdi->dparms.reserved_operations;
1676 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1677 attr->cap.max_send_sge = qp->s_max_sge;
1678 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1679 attr->cap.max_inline_data = 0;
1680 attr->ah_attr = qp->remote_ah_attr;
1681 attr->alt_ah_attr = qp->alt_ah_attr;
1682 attr->pkey_index = qp->s_pkey_index;
1683 attr->alt_pkey_index = qp->s_alt_pkey_index;
1684 attr->en_sqd_async_notify = 0;
1685 attr->sq_draining = qp->s_draining;
1686 attr->max_rd_atomic = qp->s_max_rd_atomic;
1687 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1688 attr->min_rnr_timer = qp->r_min_rnr_timer;
1689 attr->port_num = qp->port_num;
1690 attr->timeout = qp->timeout;
1691 attr->retry_cnt = qp->s_retry_cnt;
1692 attr->rnr_retry = qp->s_rnr_retry_cnt;
1693 attr->alt_port_num =
1694 rdma_ah_get_port_num(&qp->alt_ah_attr);
1695 attr->alt_timeout = qp->alt_timeout;
1696
1697 init_attr->event_handler = qp->ibqp.event_handler;
1698 init_attr->qp_context = qp->ibqp.qp_context;
1699 init_attr->send_cq = qp->ibqp.send_cq;
1700 init_attr->recv_cq = qp->ibqp.recv_cq;
1701 init_attr->srq = qp->ibqp.srq;
1702 init_attr->cap = attr->cap;
1703 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1704 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1705 else
1706 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1707 init_attr->qp_type = qp->ibqp.qp_type;
1708 init_attr->port_num = qp->port_num;
1709 return 0;
1710}
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722int rvt_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
1723 const struct ib_recv_wr **bad_wr)
1724{
1725 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1726 struct rvt_rwq *wq = qp->r_rq.wq;
1727 unsigned long flags;
1728 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1729 !qp->ibqp.srq;
1730
1731
1732 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1733 *bad_wr = wr;
1734 return -EINVAL;
1735 }
1736
1737 for (; wr; wr = wr->next) {
1738 struct rvt_rwqe *wqe;
1739 u32 next;
1740 int i;
1741
1742 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1743 *bad_wr = wr;
1744 return -EINVAL;
1745 }
1746
1747 spin_lock_irqsave(&qp->r_rq.lock, flags);
1748 next = wq->head + 1;
1749 if (next >= qp->r_rq.size)
1750 next = 0;
1751 if (next == wq->tail) {
1752 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1753 *bad_wr = wr;
1754 return -ENOMEM;
1755 }
1756 if (unlikely(qp_err_flush)) {
1757 struct ib_wc wc;
1758
1759 memset(&wc, 0, sizeof(wc));
1760 wc.qp = &qp->ibqp;
1761 wc.opcode = IB_WC_RECV;
1762 wc.wr_id = wr->wr_id;
1763 wc.status = IB_WC_WR_FLUSH_ERR;
1764 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1765 } else {
1766 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1767 wqe->wr_id = wr->wr_id;
1768 wqe->num_sge = wr->num_sge;
1769 for (i = 0; i < wr->num_sge; i++)
1770 wqe->sg_list[i] = wr->sg_list[i];
1771
1772
1773
1774
1775 smp_wmb();
1776 wq->head = next;
1777 }
1778 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1779 }
1780 return 0;
1781}
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801static inline int rvt_qp_valid_operation(
1802 struct rvt_qp *qp,
1803 const struct rvt_operation_params *post_parms,
1804 const struct ib_send_wr *wr)
1805{
1806 int len;
1807
1808 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1809 return -EINVAL;
1810 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1811 return -EINVAL;
1812 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1813 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1814 return -EINVAL;
1815 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1816 (wr->num_sge == 0 ||
1817 wr->sg_list[0].length < sizeof(u64) ||
1818 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1819 return -EINVAL;
1820 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1821 !qp->s_max_rd_atomic)
1822 return -EINVAL;
1823 len = post_parms[wr->opcode].length;
1824
1825 if (qp->ibqp.qp_type != IB_QPT_UC &&
1826 qp->ibqp.qp_type != IB_QPT_RC) {
1827 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1828 return -EINVAL;
1829 len = sizeof(struct ib_ud_wr);
1830 }
1831 return len;
1832}
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848static inline int rvt_qp_is_avail(
1849 struct rvt_qp *qp,
1850 struct rvt_dev_info *rdi,
1851 bool reserved_op)
1852{
1853 u32 slast;
1854 u32 avail;
1855 u32 reserved_used;
1856
1857
1858 smp_mb__before_atomic();
1859 reserved_used = atomic_read(&qp->s_reserved_used);
1860 if (unlikely(reserved_op)) {
1861
1862 smp_mb__before_atomic();
1863 if (reserved_used >= rdi->dparms.reserved_operations)
1864 return -ENOMEM;
1865 return 0;
1866 }
1867
1868 if (likely(qp->s_avail))
1869 return 0;
1870 slast = READ_ONCE(qp->s_last);
1871 if (qp->s_head >= slast)
1872 avail = qp->s_size - (qp->s_head - slast);
1873 else
1874 avail = slast - qp->s_head;
1875
1876
1877 smp_mb__before_atomic();
1878 reserved_used = atomic_read(&qp->s_reserved_used);
1879 avail = avail - 1 -
1880 (rdi->dparms.reserved_operations - reserved_used);
1881
1882 if ((s32)avail <= 0)
1883 return -ENOMEM;
1884 qp->s_avail = avail;
1885 if (WARN_ON(qp->s_avail >
1886 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1887 rvt_pr_err(rdi,
1888 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1889 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1890 qp->s_head, qp->s_tail, qp->s_cur,
1891 qp->s_acked, qp->s_last);
1892 return 0;
1893}
1894
1895
1896
1897
1898
1899
1900static int rvt_post_one_wr(struct rvt_qp *qp,
1901 const struct ib_send_wr *wr,
1902 bool *call_send)
1903{
1904 struct rvt_swqe *wqe;
1905 u32 next;
1906 int i;
1907 int j;
1908 int acc;
1909 struct rvt_lkey_table *rkt;
1910 struct rvt_pd *pd;
1911 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1912 u8 log_pmtu;
1913 int ret;
1914 size_t cplen;
1915 bool reserved_op;
1916 int local_ops_delayed = 0;
1917
1918 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1919
1920
1921 if (unlikely(wr->num_sge > qp->s_max_sge))
1922 return -EINVAL;
1923
1924 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1925 if (ret < 0)
1926 return ret;
1927 cplen = ret;
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
1941 switch (wr->opcode) {
1942 case IB_WR_REG_MR:
1943 ret = rvt_fast_reg_mr(qp,
1944 reg_wr(wr)->mr,
1945 reg_wr(wr)->key,
1946 reg_wr(wr)->access);
1947 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1948 return ret;
1949 break;
1950 case IB_WR_LOCAL_INV:
1951 if ((wr->send_flags & IB_SEND_FENCE) ||
1952 atomic_read(&qp->local_ops_pending)) {
1953 local_ops_delayed = 1;
1954 } else {
1955 ret = rvt_invalidate_rkey(
1956 qp, wr->ex.invalidate_rkey);
1957 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1958 return ret;
1959 }
1960 break;
1961 default:
1962 return -EINVAL;
1963 }
1964 }
1965
1966 reserved_op = rdi->post_parms[wr->opcode].flags &
1967 RVT_OPERATION_USE_RESERVE;
1968
1969 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1970 if (ret)
1971 return ret;
1972 next = qp->s_head + 1;
1973 if (next >= qp->s_size)
1974 next = 0;
1975
1976 rkt = &rdi->lkey_table;
1977 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1978 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1979
1980
1981 memcpy(&wqe->wr, wr, cplen);
1982
1983 wqe->length = 0;
1984 j = 0;
1985 if (wr->num_sge) {
1986 struct rvt_sge *last_sge = NULL;
1987
1988 acc = wr->opcode >= IB_WR_RDMA_READ ?
1989 IB_ACCESS_LOCAL_WRITE : 0;
1990 for (i = 0; i < wr->num_sge; i++) {
1991 u32 length = wr->sg_list[i].length;
1992
1993 if (length == 0)
1994 continue;
1995 ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
1996 &wr->sg_list[i], acc);
1997 if (unlikely(ret < 0))
1998 goto bail_inval_free;
1999 wqe->length += length;
2000 if (ret)
2001 last_sge = &wqe->sg_list[j];
2002 j += ret;
2003 }
2004 wqe->wr.num_sge = j;
2005 }
2006
2007
2008
2009
2010
2011
2012 log_pmtu = qp->log_pmtu;
2013 if (qp->allowed_ops == IB_OPCODE_UD) {
2014 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
2015
2016 log_pmtu = ah->log_pmtu;
2017 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
2018 }
2019
2020 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
2021 if (local_ops_delayed)
2022 atomic_inc(&qp->local_ops_pending);
2023 else
2024 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
2025 wqe->ssn = 0;
2026 wqe->psn = 0;
2027 wqe->lpsn = 0;
2028 } else {
2029 wqe->ssn = qp->s_ssn++;
2030 wqe->psn = qp->s_next_psn;
2031 wqe->lpsn = wqe->psn +
2032 (wqe->length ?
2033 ((wqe->length - 1) >> log_pmtu) :
2034 0);
2035 }
2036
2037
2038 if (rdi->driver_f.setup_wqe) {
2039 ret = rdi->driver_f.setup_wqe(qp, wqe, call_send);
2040 if (ret < 0)
2041 goto bail_inval_free_ref;
2042 }
2043
2044 if (!(rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL))
2045 qp->s_next_psn = wqe->lpsn + 1;
2046
2047 if (unlikely(reserved_op)) {
2048 wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
2049 rvt_qp_wqe_reserve(qp, wqe);
2050 } else {
2051 wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
2052 qp->s_avail--;
2053 }
2054 trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
2055 smp_wmb();
2056 qp->s_head = next;
2057
2058 return 0;
2059
2060bail_inval_free_ref:
2061 if (qp->allowed_ops == IB_OPCODE_UD)
2062 atomic_dec(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
2063bail_inval_free:
2064
2065 while (j) {
2066 struct rvt_sge *sge = &wqe->sg_list[--j];
2067
2068 rvt_put_mr(sge->mr);
2069 }
2070 return ret;
2071}
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083int rvt_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
2084 const struct ib_send_wr **bad_wr)
2085{
2086 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
2087 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2088 unsigned long flags = 0;
2089 bool call_send;
2090 unsigned nreq = 0;
2091 int err = 0;
2092
2093 spin_lock_irqsave(&qp->s_hlock, flags);
2094
2095
2096
2097
2098
2099 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
2100 spin_unlock_irqrestore(&qp->s_hlock, flags);
2101 return -EINVAL;
2102 }
2103
2104
2105
2106
2107
2108
2109 call_send = qp->s_head == READ_ONCE(qp->s_last) && !wr->next;
2110
2111 for (; wr; wr = wr->next) {
2112 err = rvt_post_one_wr(qp, wr, &call_send);
2113 if (unlikely(err)) {
2114 *bad_wr = wr;
2115 goto bail;
2116 }
2117 nreq++;
2118 }
2119bail:
2120 spin_unlock_irqrestore(&qp->s_hlock, flags);
2121 if (nreq) {
2122
2123
2124
2125
2126 if (nreq == 1 && call_send)
2127 rdi->driver_f.do_send(qp);
2128 else
2129 rdi->driver_f.schedule_send_no_lock(qp);
2130 }
2131 return err;
2132}
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144int rvt_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
2145 const struct ib_recv_wr **bad_wr)
2146{
2147 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
2148 struct rvt_rwq *wq;
2149 unsigned long flags;
2150
2151 for (; wr; wr = wr->next) {
2152 struct rvt_rwqe *wqe;
2153 u32 next;
2154 int i;
2155
2156 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
2157 *bad_wr = wr;
2158 return -EINVAL;
2159 }
2160
2161 spin_lock_irqsave(&srq->rq.lock, flags);
2162 wq = srq->rq.wq;
2163 next = wq->head + 1;
2164 if (next >= srq->rq.size)
2165 next = 0;
2166 if (next == wq->tail) {
2167 spin_unlock_irqrestore(&srq->rq.lock, flags);
2168 *bad_wr = wr;
2169 return -ENOMEM;
2170 }
2171
2172 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
2173 wqe->wr_id = wr->wr_id;
2174 wqe->num_sge = wr->num_sge;
2175 for (i = 0; i < wr->num_sge; i++)
2176 wqe->sg_list[i] = wr->sg_list[i];
2177
2178 smp_wmb();
2179 wq->head = next;
2180 spin_unlock_irqrestore(&srq->rq.lock, flags);
2181 }
2182 return 0;
2183}
2184
2185
2186
2187
2188
2189static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
2190{
2191 int i, j, ret;
2192 struct ib_wc wc;
2193 struct rvt_lkey_table *rkt;
2194 struct rvt_pd *pd;
2195 struct rvt_sge_state *ss;
2196 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2197
2198 rkt = &rdi->lkey_table;
2199 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
2200 ss = &qp->r_sge;
2201 ss->sg_list = qp->r_sg_list;
2202 qp->r_len = 0;
2203 for (i = j = 0; i < wqe->num_sge; i++) {
2204 if (wqe->sg_list[i].length == 0)
2205 continue;
2206
2207 ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
2208 NULL, &wqe->sg_list[i],
2209 IB_ACCESS_LOCAL_WRITE);
2210 if (unlikely(ret <= 0))
2211 goto bad_lkey;
2212 qp->r_len += wqe->sg_list[i].length;
2213 j++;
2214 }
2215 ss->num_sge = j;
2216 ss->total_len = qp->r_len;
2217 return 1;
2218
2219bad_lkey:
2220 while (j) {
2221 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
2222
2223 rvt_put_mr(sge->mr);
2224 }
2225 ss->num_sge = 0;
2226 memset(&wc, 0, sizeof(wc));
2227 wc.wr_id = wqe->wr_id;
2228 wc.status = IB_WC_LOC_PROT_ERR;
2229 wc.opcode = IB_WC_RECV;
2230 wc.qp = &qp->ibqp;
2231
2232 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
2233 return 0;
2234}
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only)
2247{
2248 unsigned long flags;
2249 struct rvt_rq *rq;
2250 struct rvt_rwq *wq;
2251 struct rvt_srq *srq;
2252 struct rvt_rwqe *wqe;
2253 void (*handler)(struct ib_event *, void *);
2254 u32 tail;
2255 int ret;
2256
2257 if (qp->ibqp.srq) {
2258 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
2259 handler = srq->ibsrq.event_handler;
2260 rq = &srq->rq;
2261 } else {
2262 srq = NULL;
2263 handler = NULL;
2264 rq = &qp->r_rq;
2265 }
2266
2267 spin_lock_irqsave(&rq->lock, flags);
2268 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
2269 ret = 0;
2270 goto unlock;
2271 }
2272
2273 wq = rq->wq;
2274 tail = wq->tail;
2275
2276 if (tail >= rq->size)
2277 tail = 0;
2278 if (unlikely(tail == wq->head)) {
2279 ret = 0;
2280 goto unlock;
2281 }
2282
2283 smp_rmb();
2284 wqe = rvt_get_rwqe_ptr(rq, tail);
2285
2286
2287
2288
2289
2290 if (++tail >= rq->size)
2291 tail = 0;
2292 wq->tail = tail;
2293 if (!wr_id_only && !init_sge(qp, wqe)) {
2294 ret = -1;
2295 goto unlock;
2296 }
2297 qp->r_wr_id = wqe->wr_id;
2298
2299 ret = 1;
2300 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
2301 if (handler) {
2302 u32 n;
2303
2304
2305
2306
2307
2308 n = wq->head;
2309 if (n >= rq->size)
2310 n = 0;
2311 if (n < tail)
2312 n += rq->size - tail;
2313 else
2314 n -= tail;
2315 if (n < srq->limit) {
2316 struct ib_event ev;
2317
2318 srq->limit = 0;
2319 spin_unlock_irqrestore(&rq->lock, flags);
2320 ev.device = qp->ibqp.device;
2321 ev.element.srq = qp->ibqp.srq;
2322 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
2323 handler(&ev, srq->ibsrq.srq_context);
2324 goto bail;
2325 }
2326 }
2327unlock:
2328 spin_unlock_irqrestore(&rq->lock, flags);
2329bail:
2330 return ret;
2331}
2332EXPORT_SYMBOL(rvt_get_rwqe);
2333
2334
2335
2336
2337
2338void rvt_comm_est(struct rvt_qp *qp)
2339{
2340 qp->r_flags |= RVT_R_COMM_EST;
2341 if (qp->ibqp.event_handler) {
2342 struct ib_event ev;
2343
2344 ev.device = qp->ibqp.device;
2345 ev.element.qp = &qp->ibqp;
2346 ev.event = IB_EVENT_COMM_EST;
2347 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2348 }
2349}
2350EXPORT_SYMBOL(rvt_comm_est);
2351
2352void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
2353{
2354 unsigned long flags;
2355 int lastwqe;
2356
2357 spin_lock_irqsave(&qp->s_lock, flags);
2358 lastwqe = rvt_error_qp(qp, err);
2359 spin_unlock_irqrestore(&qp->s_lock, flags);
2360
2361 if (lastwqe) {
2362 struct ib_event ev;
2363
2364 ev.device = qp->ibqp.device;
2365 ev.element.qp = &qp->ibqp;
2366 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2367 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2368 }
2369}
2370EXPORT_SYMBOL(rvt_rc_error);
2371
2372
2373
2374
2375
2376
2377unsigned long rvt_rnr_tbl_to_usec(u32 index)
2378{
2379 return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
2380}
2381EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
2382
2383static inline unsigned long rvt_aeth_to_usec(u32 aeth)
2384{
2385 return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
2386 IB_AETH_CREDIT_MASK];
2387}
2388
2389
2390
2391
2392
2393
2394
2395void rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift)
2396{
2397 struct ib_qp *ibqp = &qp->ibqp;
2398 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2399
2400 lockdep_assert_held(&qp->s_lock);
2401 qp->s_flags |= RVT_S_TIMER;
2402
2403 qp->s_timer.expires = jiffies + rdi->busy_jiffies +
2404 (qp->timeout_jiffies << shift);
2405 add_timer(&qp->s_timer);
2406}
2407EXPORT_SYMBOL(rvt_add_retry_timer_ext);
2408
2409
2410
2411
2412
2413
2414
2415void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
2416{
2417 u32 to;
2418
2419 lockdep_assert_held(&qp->s_lock);
2420 qp->s_flags |= RVT_S_WAIT_RNR;
2421 to = rvt_aeth_to_usec(aeth);
2422 trace_rvt_rnrnak_add(qp, to);
2423 hrtimer_start(&qp->s_rnr_timer,
2424 ns_to_ktime(1000 * to), HRTIMER_MODE_REL_PINNED);
2425}
2426EXPORT_SYMBOL(rvt_add_rnr_timer);
2427
2428
2429
2430
2431
2432
2433void rvt_stop_rc_timers(struct rvt_qp *qp)
2434{
2435 lockdep_assert_held(&qp->s_lock);
2436
2437 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2438 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2439 del_timer(&qp->s_timer);
2440 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2441 }
2442}
2443EXPORT_SYMBOL(rvt_stop_rc_timers);
2444
2445
2446
2447
2448
2449
2450
2451
2452static void rvt_stop_rnr_timer(struct rvt_qp *qp)
2453{
2454 lockdep_assert_held(&qp->s_lock);
2455
2456 if (qp->s_flags & RVT_S_WAIT_RNR) {
2457 qp->s_flags &= ~RVT_S_WAIT_RNR;
2458 trace_rvt_rnrnak_stop(qp, 0);
2459 }
2460}
2461
2462
2463
2464
2465
2466void rvt_del_timers_sync(struct rvt_qp *qp)
2467{
2468 del_timer_sync(&qp->s_timer);
2469 hrtimer_cancel(&qp->s_rnr_timer);
2470}
2471EXPORT_SYMBOL(rvt_del_timers_sync);
2472
2473
2474
2475
2476static void rvt_rc_timeout(struct timer_list *t)
2477{
2478 struct rvt_qp *qp = from_timer(qp, t, s_timer);
2479 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2480 unsigned long flags;
2481
2482 spin_lock_irqsave(&qp->r_lock, flags);
2483 spin_lock(&qp->s_lock);
2484 if (qp->s_flags & RVT_S_TIMER) {
2485 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
2486
2487 qp->s_flags &= ~RVT_S_TIMER;
2488 rvp->n_rc_timeouts++;
2489 del_timer(&qp->s_timer);
2490 trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
2491 if (rdi->driver_f.notify_restart_rc)
2492 rdi->driver_f.notify_restart_rc(qp,
2493 qp->s_last_psn + 1,
2494 1);
2495 rdi->driver_f.schedule_send(qp);
2496 }
2497 spin_unlock(&qp->s_lock);
2498 spin_unlock_irqrestore(&qp->r_lock, flags);
2499}
2500
2501
2502
2503
2504enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2505{
2506 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2507 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2508 unsigned long flags;
2509
2510 spin_lock_irqsave(&qp->s_lock, flags);
2511 rvt_stop_rnr_timer(qp);
2512 trace_rvt_rnrnak_timeout(qp, 0);
2513 rdi->driver_f.schedule_send(qp);
2514 spin_unlock_irqrestore(&qp->s_lock, flags);
2515 return HRTIMER_NORESTART;
2516}
2517EXPORT_SYMBOL(rvt_rc_rnr_retry);
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
2538 u64 v,
2539 void (*cb)(struct rvt_qp *qp, u64 v))
2540{
2541 struct rvt_qp_iter *i;
2542
2543 i = kzalloc(sizeof(*i), GFP_KERNEL);
2544 if (!i)
2545 return NULL;
2546
2547 i->rdi = rdi;
2548
2549 i->specials = rdi->ibdev.phys_port_cnt * 2;
2550 i->v = v;
2551 i->cb = cb;
2552
2553 return i;
2554}
2555EXPORT_SYMBOL(rvt_qp_iter_init);
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569int rvt_qp_iter_next(struct rvt_qp_iter *iter)
2570 __must_hold(RCU)
2571{
2572 int n = iter->n;
2573 int ret = 1;
2574 struct rvt_qp *pqp = iter->qp;
2575 struct rvt_qp *qp;
2576 struct rvt_dev_info *rdi = iter->rdi;
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592 for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
2593 if (pqp) {
2594 qp = rcu_dereference(pqp->next);
2595 } else {
2596 if (n < iter->specials) {
2597 struct rvt_ibport *rvp;
2598 int pidx;
2599
2600 pidx = n % rdi->ibdev.phys_port_cnt;
2601 rvp = rdi->ports[pidx];
2602 qp = rcu_dereference(rvp->qp[n & 1]);
2603 } else {
2604 qp = rcu_dereference(
2605 rdi->qp_dev->qp_table[
2606 (n - iter->specials)]);
2607 }
2608 }
2609 pqp = qp;
2610 if (qp) {
2611 iter->qp = qp;
2612 iter->n = n;
2613 return 0;
2614 }
2615 }
2616 return ret;
2617}
2618EXPORT_SYMBOL(rvt_qp_iter_next);
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636void rvt_qp_iter(struct rvt_dev_info *rdi,
2637 u64 v,
2638 void (*cb)(struct rvt_qp *qp, u64 v))
2639{
2640 int ret;
2641 struct rvt_qp_iter i = {
2642 .rdi = rdi,
2643 .specials = rdi->ibdev.phys_port_cnt * 2,
2644 .v = v,
2645 .cb = cb
2646 };
2647
2648 rcu_read_lock();
2649 do {
2650 ret = rvt_qp_iter_next(&i);
2651 if (!ret) {
2652 rvt_get_qp(i.qp);
2653 rcu_read_unlock();
2654 i.cb(i.qp, i.v);
2655 rcu_read_lock();
2656 rvt_put_qp(i.qp);
2657 }
2658 } while (!ret);
2659 rcu_read_unlock();
2660}
2661EXPORT_SYMBOL(rvt_qp_iter);
2662
2663
2664
2665
2666void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
2667 enum ib_wc_status status)
2668{
2669 u32 old_last, last;
2670 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2671
2672 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
2673 return;
2674
2675 last = qp->s_last;
2676 old_last = last;
2677 trace_rvt_qp_send_completion(qp, wqe, last);
2678 if (++last >= qp->s_size)
2679 last = 0;
2680 trace_rvt_qp_send_completion(qp, wqe, last);
2681 qp->s_last = last;
2682
2683 barrier();
2684 rvt_put_qp_swqe(qp, wqe);
2685
2686 rvt_qp_swqe_complete(qp,
2687 wqe,
2688 rdi->wc_opcode[wqe->wr.opcode],
2689 status);
2690
2691 if (qp->s_acked == old_last)
2692 qp->s_acked = last;
2693 if (qp->s_cur == old_last)
2694 qp->s_cur = last;
2695 if (qp->s_tail == old_last)
2696 qp->s_tail = last;
2697 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
2698 qp->s_draining = 0;
2699}
2700EXPORT_SYMBOL(rvt_send_complete);
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,
2712 void *data, u32 length,
2713 bool release, bool copy_last)
2714{
2715 struct rvt_sge *sge = &ss->sge;
2716 int i;
2717 bool in_last = false;
2718 bool cacheless_copy = false;
2719 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2720 struct rvt_wss *wss = rdi->wss;
2721 unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode;
2722
2723 if (sge_copy_mode == RVT_SGE_COPY_CACHELESS) {
2724 cacheless_copy = length >= PAGE_SIZE;
2725 } else if (sge_copy_mode == RVT_SGE_COPY_ADAPTIVE) {
2726 if (length >= PAGE_SIZE) {
2727
2728
2729
2730
2731
2732 wss_insert(wss, sge->vaddr);
2733 if (length >= (2 * PAGE_SIZE))
2734 wss_insert(wss, (sge->vaddr + PAGE_SIZE));
2735
2736 cacheless_copy = wss_exceeds_threshold(wss);
2737 } else {
2738 wss_advance_clean_counter(wss);
2739 }
2740 }
2741
2742 if (copy_last) {
2743 if (length > 8) {
2744 length -= 8;
2745 } else {
2746 copy_last = false;
2747 in_last = true;
2748 }
2749 }
2750
2751again:
2752 while (length) {
2753 u32 len = rvt_get_sge_length(sge, length);
2754
2755 WARN_ON_ONCE(len == 0);
2756 if (unlikely(in_last)) {
2757
2758 for (i = 0; i < len; i++)
2759 ((u8 *)sge->vaddr)[i] = ((u8 *)data)[i];
2760 } else if (cacheless_copy) {
2761 cacheless_memcpy(sge->vaddr, data, len);
2762 } else {
2763 memcpy(sge->vaddr, data, len);
2764 }
2765 rvt_update_sge(ss, len, release);
2766 data += len;
2767 length -= len;
2768 }
2769
2770 if (copy_last) {
2771 copy_last = false;
2772 in_last = true;
2773 length = 8;
2774 goto again;
2775 }
2776}
2777EXPORT_SYMBOL(rvt_copy_sge);
2778
2779static enum ib_wc_status loopback_qp_drop(struct rvt_ibport *rvp,
2780 struct rvt_qp *sqp)
2781{
2782 rvp->n_pkt_drops++;
2783
2784
2785
2786
2787 return sqp->ibqp.qp_type == IB_QPT_RC ?
2788 IB_WC_RETRY_EXC_ERR : IB_WC_SUCCESS;
2789}
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801void rvt_ruc_loopback(struct rvt_qp *sqp)
2802{
2803 struct rvt_ibport *rvp = NULL;
2804 struct rvt_dev_info *rdi = ib_to_rvt(sqp->ibqp.device);
2805 struct rvt_qp *qp;
2806 struct rvt_swqe *wqe;
2807 struct rvt_sge *sge;
2808 unsigned long flags;
2809 struct ib_wc wc;
2810 u64 sdata;
2811 atomic64_t *maddr;
2812 enum ib_wc_status send_status;
2813 bool release;
2814 int ret;
2815 bool copy_last = false;
2816 int local_ops = 0;
2817
2818 rcu_read_lock();
2819 rvp = rdi->ports[sqp->port_num - 1];
2820
2821
2822
2823
2824
2825
2826 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), rvp,
2827 sqp->remote_qpn);
2828
2829 spin_lock_irqsave(&sqp->s_lock, flags);
2830
2831
2832 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
2833 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
2834 goto unlock;
2835
2836 sqp->s_flags |= RVT_S_BUSY;
2837
2838again:
2839 if (sqp->s_last == READ_ONCE(sqp->s_head))
2840 goto clr_busy;
2841 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
2842
2843
2844 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
2845 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
2846 goto clr_busy;
2847
2848 send_status = IB_WC_WR_FLUSH_ERR;
2849 goto flush_send;
2850 }
2851
2852
2853
2854
2855
2856
2857 if (sqp->s_last == sqp->s_cur) {
2858 if (++sqp->s_cur >= sqp->s_size)
2859 sqp->s_cur = 0;
2860 }
2861 spin_unlock_irqrestore(&sqp->s_lock, flags);
2862
2863 if (!qp) {
2864 send_status = loopback_qp_drop(rvp, sqp);
2865 goto serr_no_r_lock;
2866 }
2867 spin_lock_irqsave(&qp->r_lock, flags);
2868 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
2869 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
2870 send_status = loopback_qp_drop(rvp, sqp);
2871 goto serr;
2872 }
2873
2874 memset(&wc, 0, sizeof(wc));
2875 send_status = IB_WC_SUCCESS;
2876
2877 release = true;
2878 sqp->s_sge.sge = wqe->sg_list[0];
2879 sqp->s_sge.sg_list = wqe->sg_list + 1;
2880 sqp->s_sge.num_sge = wqe->wr.num_sge;
2881 sqp->s_len = wqe->length;
2882 switch (wqe->wr.opcode) {
2883 case IB_WR_REG_MR:
2884 goto send_comp;
2885
2886 case IB_WR_LOCAL_INV:
2887 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
2888 if (rvt_invalidate_rkey(sqp,
2889 wqe->wr.ex.invalidate_rkey))
2890 send_status = IB_WC_LOC_PROT_ERR;
2891 local_ops = 1;
2892 }
2893 goto send_comp;
2894
2895 case IB_WR_SEND_WITH_INV:
2896 case IB_WR_SEND_WITH_IMM:
2897 case IB_WR_SEND:
2898 ret = rvt_get_rwqe(qp, false);
2899 if (ret < 0)
2900 goto op_err;
2901 if (!ret)
2902 goto rnr_nak;
2903 if (wqe->length > qp->r_len)
2904 goto inv_err;
2905 switch (wqe->wr.opcode) {
2906 case IB_WR_SEND_WITH_INV:
2907 if (!rvt_invalidate_rkey(qp,
2908 wqe->wr.ex.invalidate_rkey)) {
2909 wc.wc_flags = IB_WC_WITH_INVALIDATE;
2910 wc.ex.invalidate_rkey =
2911 wqe->wr.ex.invalidate_rkey;
2912 }
2913 break;
2914 case IB_WR_SEND_WITH_IMM:
2915 wc.wc_flags = IB_WC_WITH_IMM;
2916 wc.ex.imm_data = wqe->wr.ex.imm_data;
2917 break;
2918 default:
2919 break;
2920 }
2921 break;
2922
2923 case IB_WR_RDMA_WRITE_WITH_IMM:
2924 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2925 goto inv_err;
2926 wc.wc_flags = IB_WC_WITH_IMM;
2927 wc.ex.imm_data = wqe->wr.ex.imm_data;
2928 ret = rvt_get_rwqe(qp, true);
2929 if (ret < 0)
2930 goto op_err;
2931 if (!ret)
2932 goto rnr_nak;
2933
2934 goto do_write;
2935 case IB_WR_RDMA_WRITE:
2936 copy_last = rvt_is_user_qp(qp);
2937 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2938 goto inv_err;
2939do_write:
2940 if (wqe->length == 0)
2941 break;
2942 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
2943 wqe->rdma_wr.remote_addr,
2944 wqe->rdma_wr.rkey,
2945 IB_ACCESS_REMOTE_WRITE)))
2946 goto acc_err;
2947 qp->r_sge.sg_list = NULL;
2948 qp->r_sge.num_sge = 1;
2949 qp->r_sge.total_len = wqe->length;
2950 break;
2951
2952 case IB_WR_RDMA_READ:
2953 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2954 goto inv_err;
2955 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
2956 wqe->rdma_wr.remote_addr,
2957 wqe->rdma_wr.rkey,
2958 IB_ACCESS_REMOTE_READ)))
2959 goto acc_err;
2960 release = false;
2961 sqp->s_sge.sg_list = NULL;
2962 sqp->s_sge.num_sge = 1;
2963 qp->r_sge.sge = wqe->sg_list[0];
2964 qp->r_sge.sg_list = wqe->sg_list + 1;
2965 qp->r_sge.num_sge = wqe->wr.num_sge;
2966 qp->r_sge.total_len = wqe->length;
2967 break;
2968
2969 case IB_WR_ATOMIC_CMP_AND_SWP:
2970 case IB_WR_ATOMIC_FETCH_AND_ADD:
2971 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2972 goto inv_err;
2973 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2974 wqe->atomic_wr.remote_addr,
2975 wqe->atomic_wr.rkey,
2976 IB_ACCESS_REMOTE_ATOMIC)))
2977 goto acc_err;
2978
2979 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
2980 sdata = wqe->atomic_wr.compare_add;
2981 *(u64 *)sqp->s_sge.sge.vaddr =
2982 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
2983 (u64)atomic64_add_return(sdata, maddr) - sdata :
2984 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
2985 sdata, wqe->atomic_wr.swap);
2986 rvt_put_mr(qp->r_sge.sge.mr);
2987 qp->r_sge.num_sge = 0;
2988 goto send_comp;
2989
2990 default:
2991 send_status = IB_WC_LOC_QP_OP_ERR;
2992 goto serr;
2993 }
2994
2995 sge = &sqp->s_sge.sge;
2996 while (sqp->s_len) {
2997 u32 len = rvt_get_sge_length(sge, sqp->s_len);
2998
2999 WARN_ON_ONCE(len == 0);
3000 rvt_copy_sge(qp, &qp->r_sge, sge->vaddr,
3001 len, release, copy_last);
3002 rvt_update_sge(&sqp->s_sge, len, !release);
3003 sqp->s_len -= len;
3004 }
3005 if (release)
3006 rvt_put_ss(&qp->r_sge);
3007
3008 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
3009 goto send_comp;
3010
3011 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
3012 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
3013 else
3014 wc.opcode = IB_WC_RECV;
3015 wc.wr_id = qp->r_wr_id;
3016 wc.status = IB_WC_SUCCESS;
3017 wc.byte_len = wqe->length;
3018 wc.qp = &qp->ibqp;
3019 wc.src_qp = qp->remote_qpn;
3020 wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
3021 wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
3022 wc.port_num = 1;
3023
3024 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
3025 wqe->wr.send_flags & IB_SEND_SOLICITED);
3026
3027send_comp:
3028 spin_unlock_irqrestore(&qp->r_lock, flags);
3029 spin_lock_irqsave(&sqp->s_lock, flags);
3030 rvp->n_loop_pkts++;
3031flush_send:
3032 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
3033 rvt_send_complete(sqp, wqe, send_status);
3034 if (local_ops) {
3035 atomic_dec(&sqp->local_ops_pending);
3036 local_ops = 0;
3037 }
3038 goto again;
3039
3040rnr_nak:
3041
3042 if (qp->ibqp.qp_type == IB_QPT_UC)
3043 goto send_comp;
3044 rvp->n_rnr_naks++;
3045
3046
3047
3048
3049 if (sqp->s_rnr_retry == 0) {
3050 send_status = IB_WC_RNR_RETRY_EXC_ERR;
3051 goto serr;
3052 }
3053 if (sqp->s_rnr_retry_cnt < 7)
3054 sqp->s_rnr_retry--;
3055 spin_unlock_irqrestore(&qp->r_lock, flags);
3056 spin_lock_irqsave(&sqp->s_lock, flags);
3057 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
3058 goto clr_busy;
3059 rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
3060 IB_AETH_CREDIT_SHIFT);
3061 goto clr_busy;
3062
3063op_err:
3064 send_status = IB_WC_REM_OP_ERR;
3065 wc.status = IB_WC_LOC_QP_OP_ERR;
3066 goto err;
3067
3068inv_err:
3069 send_status =
3070 sqp->ibqp.qp_type == IB_QPT_RC ?
3071 IB_WC_REM_INV_REQ_ERR :
3072 IB_WC_SUCCESS;
3073 wc.status = IB_WC_LOC_QP_OP_ERR;
3074 goto err;
3075
3076acc_err:
3077 send_status = IB_WC_REM_ACCESS_ERR;
3078 wc.status = IB_WC_LOC_PROT_ERR;
3079err:
3080
3081 rvt_rc_error(qp, wc.status);
3082
3083serr:
3084 spin_unlock_irqrestore(&qp->r_lock, flags);
3085serr_no_r_lock:
3086 spin_lock_irqsave(&sqp->s_lock, flags);
3087 rvt_send_complete(sqp, wqe, send_status);
3088 if (sqp->ibqp.qp_type == IB_QPT_RC) {
3089 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
3090
3091 sqp->s_flags &= ~RVT_S_BUSY;
3092 spin_unlock_irqrestore(&sqp->s_lock, flags);
3093 if (lastwqe) {
3094 struct ib_event ev;
3095
3096 ev.device = sqp->ibqp.device;
3097 ev.element.qp = &sqp->ibqp;
3098 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
3099 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
3100 }
3101 goto done;
3102 }
3103clr_busy:
3104 sqp->s_flags &= ~RVT_S_BUSY;
3105unlock:
3106 spin_unlock_irqrestore(&sqp->s_lock, flags);
3107done:
3108 rcu_read_unlock();
3109}
3110EXPORT_SYMBOL(rvt_ruc_loopback);
3111