1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
|
/*
* Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
* Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <linux/dma-mapping.h>
#include <net/addrconf.h>
#include "rxe.h"
#include "rxe_loc.h"
#include "rxe_queue.h"
#include "rxe_hw_counters.h"
static int rxe_query_device(struct ib_device *dev,
struct ib_device_attr *attr,
struct ib_udata *uhw)
{
struct rxe_dev *rxe = to_rdev(dev);
if (uhw->inlen || uhw->outlen)
return -EINVAL;
*attr = rxe->attr;
return 0;
}
static void rxe_eth_speed_to_ib_speed(int speed, u8 *active_speed,
u8 *active_width)
{
if (speed <= 1000) {
*active_width = IB_WIDTH_1X;
*active_speed = IB_SPEED_SDR;
} else if (speed <= 10000) {
*active_width = IB_WIDTH_1X;
*active_speed = IB_SPEED_FDR10;
} else if (speed <= 20000) {
*active_width = IB_WIDTH_4X;
*active_speed = IB_SPEED_DDR;
} else if (speed <= 30000) {
*active_width = IB_WIDTH_4X;
*active_speed = IB_SPEED_QDR;
} else if (speed <= 40000) {
*active_width = IB_WIDTH_4X;
*active_speed = IB_SPEED_FDR10;
} else {
*active_width = IB_WIDTH_4X;
*active_speed = IB_SPEED_EDR;
}
}
static int rxe_query_port(struct ib_device *dev,
u8 port_num, struct ib_port_attr *attr)
{
struct rxe_dev *rxe = to_rdev(dev);
struct rxe_port *port;
u32 speed;
if (unlikely(port_num != 1)) {
pr_warn("invalid port_number %d\n", port_num);
goto err1;
}
port = &rxe->port;
/* *attr being zeroed by the caller, avoid zeroing it here */
*attr = port->attr;
mutex_lock(&rxe->usdev_lock);
if (rxe->ndev->ethtool_ops->get_link_ksettings) {
struct ethtool_link_ksettings ks;
rxe->ndev->ethtool_ops->get_link_ksettings(rxe->ndev, &ks);
speed = ks.base.speed;
} else if (rxe->ndev->ethtool_ops->get_settings) {
struct ethtool_cmd cmd;
rxe->ndev->ethtool_ops->get_settings(rxe->ndev, &cmd);
speed = cmd.speed;
} else {
pr_warn("%s speed is unknown, defaulting to 1000\n",
rxe->ndev->name);
speed = 1000;
}
rxe_eth_speed_to_ib_speed(speed, &attr->active_speed,
&attr->active_width);
mutex_unlock(&rxe->usdev_lock);
return 0;
err1:
return -EINVAL;
}
static int rxe_query_gid(struct ib_device *device,
u8 port_num, int index, union ib_gid *gid)
{
int ret;
if (index > RXE_PORT_GID_TBL_LEN)
return -EINVAL;
ret = ib_get_cached_gid(device, port_num, index, gid, NULL);
if (ret == -EAGAIN) {
memcpy(gid, &zgid, sizeof(*gid));
return 0;
}
return ret;
}
static int rxe_add_gid(struct ib_device *device, u8 port_num, unsigned int
index, const union ib_gid *gid,
const struct ib_gid_attr *attr, void **context)
{
if (index >= RXE_PORT_GID_TBL_LEN)
return -EINVAL;
return 0;
}
static int rxe_del_gid(struct ib_device *device, u8 port_num, unsigned int
index, void **context)
{
if (index >= RXE_PORT_GID_TBL_LEN)
return -EINVAL;
return 0;
}
static struct net_device *rxe_get_netdev(struct ib_device *device,
u8 port_num)
{
struct rxe_dev *rxe = to_rdev(device);
if (rxe->ndev) {
dev_hold(rxe->ndev);
return rxe->ndev;
}
return NULL;
}
static int rxe_query_pkey(struct ib_device *device,
u8 port_num, u16 index, u16 *pkey)
{
struct rxe_dev *rxe = to_rdev(device);
struct rxe_port *port;
if (unlikely(port_num != 1)) {
dev_warn(device->dev.parent, "invalid port_num = %d\n",
port_num);
goto err1;
}
port = &rxe->port;
if (unlikely(index >= port->attr.pkey_tbl_len)) {
dev_warn(device->dev.parent, "invalid index = %d\n",
index);
goto err1;
}
*pkey = port->pkey_tbl[index];
return 0;
err1:
return -EINVAL;
}
static int rxe_modify_device(struct ib_device *dev,
int mask, struct ib_device_modify *attr)
{
struct rxe_dev *rxe = to_rdev(dev);
if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
memcpy(rxe->ib_dev.node_desc,
attr->node_desc, sizeof(rxe->ib_dev.node_desc));
}
return 0;
}
static int rxe_modify_port(struct ib_device *dev,
u8 port_num, int mask, struct ib_port_modify *attr)
{
struct rxe_dev *rxe = to_rdev(dev);
struct rxe_port *port;
if (unlikely(port_num != 1)) {
pr_warn("invalid port_num = %d\n", port_num);
goto err1;
}
port = &rxe->port;
port->attr.port_cap_flags |= attr->set_port_cap_mask;
port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
if (mask & IB_PORT_RESET_QKEY_CNTR)
port->attr.qkey_viol_cntr = 0;
return 0;
err1:
return -EINVAL;
}
static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
u8 port_num)
{
struct rxe_dev *rxe = to_rdev(dev);
return rxe_link_layer(rxe, port_num);
}
static struct ib_ucontext *rxe_alloc_ucontext(struct ib_device *dev,
struct ib_udata *udata)
{
struct rxe_dev *rxe = to_rdev(dev);
struct rxe_ucontext *uc;
uc = rxe_alloc(&rxe->uc_pool);
return uc ? &uc->ibuc : ERR_PTR(-ENOMEM);
}
static int rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
{
struct rxe_ucontext *uc = to_ruc(ibuc);
rxe_drop_ref(uc);
return 0;
}
static int rxe_port_immutable(struct ib_device *dev, u8 port_num,
struct ib_port_immutable *immutable)
{
int err;
struct ib_port_attr attr;
immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
err = ib_query_port(dev, port_num, &attr);
if (err)
return err;
immutable->pkey_tbl_len = attr.pkey_tbl_len;
immutable->gid_tbl_len = attr.gid_tbl_len;
immutable->max_mad_size = IB_MGMT_MAD_SIZE;
return 0;
}
static struct ib_pd *rxe_alloc_pd(struct ib_device *dev,
struct ib_ucontext *context,
struct ib_udata *udata)
{
struct rxe_dev *rxe = to_rdev(dev);
struct rxe_pd *pd;
pd = rxe_alloc(&rxe->pd_pool);
return pd ? &pd->ibpd : ERR_PTR(-ENOMEM);
}
static int rxe_dealloc_pd(struct ib_pd *ibpd)
{
struct rxe_pd *pd = to_rpd(ibpd);
rxe_drop_ref(pd);
return 0;
}
static int rxe_init_av(struct rxe_dev *rxe, struct rdma_ah_attr *attr,
struct rxe_av *av)
{
int err;
union ib_gid sgid;
struct ib_gid_attr sgid_attr;
err = ib_get_cached_gid(&rxe->ib_dev, rdma_ah_get_port_num(attr),
rdma_ah_read_grh(attr)->sgid_index, &sgid,
&sgid_attr);
if (err) {
pr_err("Failed to query sgid. err = %d\n", err);
return err;
}
err = rxe_av_from_attr(rxe, rdma_ah_get_port_num(attr), av, attr);
if (!err)
err = rxe_av_fill_ip_info(rxe, av, attr, &sgid_attr, &sgid);
if (sgid_attr.ndev)
dev_put(sgid_attr.ndev);
return err;
}
static struct ib_ah *rxe_create_ah(struct ib_pd *ibpd,
struct rdma_ah_attr *attr,
struct ib_udata *udata)
{
int err;
struct rxe_dev *rxe = to_rdev(ibpd->device);
struct rxe_pd *pd = to_rpd(ibpd);
struct rxe_ah *ah;
err = rxe_av_chk_attr(rxe, attr);
if (err)
goto err1;
ah = rxe_alloc(&rxe->ah_pool);
if (!ah) {
err = -ENOMEM;
goto err1;
}
rxe_add_ref(pd);
ah->pd = pd;
err = rxe_init_av(rxe, attr, &ah->av);
if (err)
goto err2;
return &ah->ibah;
err2:
rxe_drop_ref(pd);
rxe_drop_ref(ah);
err1:
return ERR_PTR(err);
}
static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
{
int err;
struct rxe_dev *rxe = to_rdev(ibah->device);
struct rxe_ah *ah = to_rah(ibah);
err = rxe_av_chk_attr(rxe, attr);
if (err)
return err;
err = rxe_init_av(rxe, attr, &ah->av);
if (err)
return err;
return 0;
}
static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
{
struct rxe_dev *rxe = to_rdev(ibah->device);
struct rxe_ah *ah = to_rah(ibah);
memset(attr, 0, sizeof(*attr));
attr->type = ibah->type;
rxe_av_to_attr(rxe, &ah->av, attr);
return 0;
}
static int rxe_destroy_ah(struct ib_ah *ibah)
{
struct rxe_ah *ah = to_rah(ibah);
rxe_drop_ref(ah->pd);
rxe_drop_ref(ah);
return 0;
}
static int post_one_recv(struct rxe_rq *rq, struct ib_recv_wr *ibwr)
{
int err;
int i;
u32 length;
struct rxe_recv_wqe *recv_wqe;
int num_sge = ibwr->num_sge;
if (unlikely(queue_full(rq->queue))) {
err = -ENOMEM;
goto err1;
}
if (unlikely(num_sge > rq->max_sge)) {
err = -EINVAL;
goto err1;
}
length = 0;
for (i = 0; i < num_sge; i++)
length += ibwr->sg_list[i].length;
recv_wqe = producer_addr(rq->queue);
recv_wqe->wr_id = ibwr->wr_id;
recv_wqe->num_sge = num_sge;
memcpy(recv_wqe->dma.sge, ibwr->sg_list,
num_sge * sizeof(struct ib_sge));
recv_wqe->dma.length = length;
recv_wqe->dma.resid = length;
recv_wqe->dma.num_sge = num_sge;
recv_wqe->dma.cur_sge = 0;
recv_wqe->dma.sge_offset = 0;
/* make sure all changes to the work queue are written before we
* update the producer pointer
*/
smp_wmb();
advance_producer(rq->queue);
return 0;
err1:
return err;
}
static struct ib_srq *rxe_create_srq(struct ib_pd *ibpd,
struct ib_srq_init_attr *init,
struct ib_udata *udata)
{
int err;
struct rxe_dev *rxe = to_rdev(ibpd->device);
struct rxe_pd *pd = to_rpd(ibpd);
struct rxe_srq *srq;
struct ib_ucontext *context = udata ? ibpd->uobject->context : NULL;
err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK);
if (err)
goto err1;
srq = rxe_alloc(&rxe->srq_pool);
if (!srq) {
err = -ENOMEM;
goto err1;
}
rxe_add_index(srq);
rxe_add_ref(pd);
srq->pd = pd;
err = rxe_srq_from_init(rxe, srq, init, context, udata);
if (err)
goto err2;
return &srq->ibsrq;
err2:
rxe_drop_ref(pd);
rxe_drop_index(srq);
rxe_drop_ref(srq);
err1:
return ERR_PTR(err);
}
static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
enum ib_srq_attr_mask mask,
struct ib_udata *udata)
{
int err;
struct rxe_srq *srq = to_rsrq(ibsrq);
struct rxe_dev *rxe = to_rdev(ibsrq->device);
err = rxe_srq_chk_attr(rxe, srq, attr, mask);
if (err)
goto err1;
err = rxe_srq_from_attr(rxe, srq, attr, mask, udata);
if (err)
goto err1;
return 0;
err1:
return err;
}
static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
{
struct rxe_srq *srq = to_rsrq(ibsrq);
if (srq->error)
return -EINVAL;
attr->max_wr = srq->rq.queue->buf->index_mask;
attr->max_sge = srq->rq.max_sge;
attr->srq_limit = srq->limit;
return 0;
}
static int rxe_destroy_srq(struct ib_srq *ibsrq)
{
struct rxe_srq *srq = to_rsrq(ibsrq);
if (srq->rq.queue)
rxe_queue_cleanup(srq->rq.queue);
rxe_drop_ref(srq->pd);
rxe_drop_index(srq);
rxe_drop_ref(srq);
return 0;
}
static int rxe_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
struct ib_recv_wr **bad_wr)
{
int err = 0;
unsigned long flags;
struct rxe_srq *srq = to_rsrq(ibsrq);
spin_lock_irqsave(&srq->rq.producer_lock, flags);
while (wr) {
err = post_one_recv(&srq->rq, wr);
if (unlikely(err))
break;
wr = wr->next;
}
spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
if (err)
*bad_wr = wr;
return err;
}
static struct ib_qp *rxe_create_qp(struct ib_pd *ibpd,
struct ib_qp_init_attr *init,
struct ib_udata *udata)
{
int err;
struct rxe_dev *rxe = to_rdev(ibpd->device);
struct rxe_pd *pd = to_rpd(ibpd);
struct rxe_qp *qp;
err = rxe_qp_chk_init(rxe, init);
if (err)
goto err1;
qp = rxe_alloc(&rxe->qp_pool);
if (!qp) {
err = -ENOMEM;
goto err1;
}
if (udata) {
if (udata->inlen) {
err = -EINVAL;
goto err2;
}
qp->is_user = 1;
}
rxe_add_index(qp);
err = rxe_qp_from_init(rxe, qp, pd, init, udata, ibpd);
if (err)
goto err3;
return &qp->ibqp;
err3:
rxe_drop_index(qp);
err2:
rxe_drop_ref(qp);
err1:
return ERR_PTR(err);
}
static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
int mask, struct ib_udata *udata)
{
int err;
struct rxe_dev *rxe = to_rdev(ibqp->device);
struct rxe_qp *qp = to_rqp(ibqp);
err = rxe_qp_chk_attr(rxe, qp, attr, mask);
if (err)
goto err1;
err = rxe_qp_from_attr(qp, attr, mask, udata);
if (err)
goto err1;
return 0;
err1:
return err;
}
static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
int mask, struct ib_qp_init_attr *init)
{
struct rxe_qp *qp = to_rqp(ibqp);
rxe_qp_to_init(qp, init);
rxe_qp_to_attr(qp, attr, mask);
return 0;
}
static int rxe_destroy_qp(struct ib_qp *ibqp)
{
struct rxe_qp *qp = to_rqp(ibqp);
rxe_qp_destroy(qp);
rxe_drop_index(qp);
rxe_drop_ref(qp);
return 0;
}
static int validate_send_wr(struct rxe_qp *qp, struct ib_send_wr *ibwr,
unsigned int mask, unsigned int length)
{
int num_sge = ibwr->num_sge;
struct rxe_sq *sq = &qp->sq;
if (unlikely(num_sge > sq->max_sge))
goto err1;
if (unlikely(mask & WR_ATOMIC_MASK)) {
if (length < 8)
goto err1;
if (atomic_wr(ibwr)->remote_addr & 0x7)
goto err1;
}
if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
(length > sq->max_inline)))
goto err1;
return 0;
err1:
return -EINVAL;
}
static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
struct ib_send_wr *ibwr)
{
wr->wr_id = ibwr->wr_id;
wr->num_sge = ibwr->num_sge;
wr->opcode = ibwr->opcode;
wr->send_flags = ibwr->send_flags;
if (qp_type(qp) == IB_QPT_UD ||
qp_type(qp) == IB_QPT_SMI ||
qp_type(qp) == IB_QPT_GSI) {
wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
if (qp_type(qp) == IB_QPT_GSI)
wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
if (wr->opcode == IB_WR_SEND_WITH_IMM)
wr->ex.imm_data = ibwr->ex.imm_data;
} else {
switch (wr->opcode) {
case IB_WR_RDMA_WRITE_WITH_IMM:
wr->ex.imm_data = ibwr->ex.imm_data;
case IB_WR_RDMA_READ:
case IB_WR_RDMA_WRITE:
wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
break;
case IB_WR_SEND_WITH_IMM:
wr->ex.imm_data = ibwr->ex.imm_data;
break;
case IB_WR_SEND_WITH_INV:
wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
break;
case IB_WR_ATOMIC_CMP_AND_SWP:
case IB_WR_ATOMIC_FETCH_AND_ADD:
wr->wr.atomic.remote_addr =
atomic_wr(ibwr)->remote_addr;
wr->wr.atomic.compare_add =
atomic_wr(ibwr)->compare_add;
wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
break;
case IB_WR_LOCAL_INV:
wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
break;
case IB_WR_REG_MR:
wr->wr.reg.mr = reg_wr(ibwr)->mr;
wr->wr.reg.key = reg_wr(ibwr)->key;
wr->wr.reg.access = reg_wr(ibwr)->access;
break;
default:
break;
}
}
}
static int init_send_wqe(struct rxe_qp *qp, struct ib_send_wr *ibwr,
unsigned int mask, unsigned int length,
struct rxe_send_wqe *wqe)
{
int num_sge = ibwr->num_sge;
struct ib_sge *sge;
int i;
u8 *p;
init_send_wr(qp, &wqe->wr, ibwr);
if (qp_type(qp) == IB_QPT_UD ||
qp_type(qp) == IB_QPT_SMI ||
qp_type(qp) == IB_QPT_GSI)
memcpy(&wqe->av, &to_rah(ud_wr(ibwr)->ah)->av, sizeof(wqe->av));
if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) {
p = wqe->dma.inline_data;
sge = ibwr->sg_list;
for (i = 0; i < num_sge; i++, sge++) {
memcpy(p, (void *)(uintptr_t)sge->addr,
sge->length);
p += sge->length;
}
} else if (mask & WR_REG_MASK) {
wqe->mask = mask;
wqe->state = wqe_state_posted;
return 0;
} else
memcpy(wqe->dma.sge, ibwr->sg_list,
num_sge * sizeof(struct ib_sge));
wqe->iova = (mask & WR_ATOMIC_MASK) ?
atomic_wr(ibwr)->remote_addr :
rdma_wr(ibwr)->remote_addr;
wqe->mask = mask;
wqe->dma.length = length;
wqe->dma.resid = length;
wqe->dma.num_sge = num_sge;
wqe->dma.cur_sge = 0;
wqe->dma.sge_offset = 0;
wqe->state = wqe_state_posted;
wqe->ssn = atomic_add_return(1, &qp->ssn);
return 0;
}
static int post_one_send(struct rxe_qp *qp, struct ib_send_wr *ibwr,
unsigned int mask, u32 length)
{
int err;
struct rxe_sq *sq = &qp->sq;
struct rxe_send_wqe *send_wqe;
unsigned long flags;
err = validate_send_wr(qp, ibwr, mask, length);
if (err)
return err;
spin_lock_irqsave(&qp->sq.sq_lock, flags);
if (unlikely(queue_full(sq->queue))) {
err = -ENOMEM;
goto err1;
}
send_wqe = producer_addr(sq->queue);
err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
if (unlikely(err))
goto err1;
/*
* make sure all changes to the work queue are
* written before we update the producer pointer
*/
smp_wmb();
advance_producer(sq->queue);
spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
return 0;
err1:
spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
return err;
}
static int rxe_post_send_kernel(struct rxe_qp *qp, struct ib_send_wr *wr,
struct ib_send_wr **bad_wr)
{
int err = 0;
unsigned int mask;
unsigned int length = 0;
int i;
int must_sched;
while (wr) {
mask = wr_opcode_mask(wr->opcode, qp);
if (unlikely(!mask)) {
err = -EINVAL;
*bad_wr = wr;
break;
}
if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
!(mask & WR_INLINE_MASK))) {
err = -EINVAL;
*bad_wr = wr;
break;
}
length = 0;
for (i = 0; i < wr->num_sge; i++)
length += wr->sg_list[i].length;
err = post_one_send(qp, wr, mask, length);
if (err) {
*bad_wr = wr;
break;
}
wr = wr->next;
}
/*
* Must sched in case of GSI QP because ib_send_mad() hold irq lock,
* and the requester call ip_local_out_sk() that takes spin_lock_bh.
*/
must_sched = (qp_type(qp) == IB_QPT_GSI) ||
(queue_count(qp->sq.queue) > 1);
rxe_run_task(&qp->req.task, must_sched);
return err;
}
static int rxe_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
struct ib_send_wr **bad_wr)
{
struct rxe_qp *qp = to_rqp(ibqp);
if (unlikely(!qp->valid)) {
*bad_wr = wr;
return -EINVAL;
}
if (unlikely(qp->req.state < QP_STATE_READY)) {
*bad_wr = wr;
return -EINVAL;
}
if (qp->is_user) {
/* Utilize process context to do protocol processing */
rxe_run_task(&qp->req.task, 0);
return 0;
} else
return rxe_post_send_kernel(qp, wr, bad_wr);
}
static int rxe_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
struct ib_recv_wr **bad_wr)
{
int err = 0;
struct rxe_qp *qp = to_rqp(ibqp);
struct rxe_rq *rq = &qp->rq;
unsigned long flags;
if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
*bad_wr = wr;
err = -EINVAL;
goto err1;
}
if (unlikely(qp->srq)) {
*bad_wr = wr;
err = -EINVAL;
goto err1;
}
spin_lock_irqsave(&rq->producer_lock, flags);
while (wr) {
err = post_one_recv(rq, wr);
if (unlikely(err)) {
*bad_wr = wr;
break;
}
wr = wr->next;
}
spin_unlock_irqrestore(&rq->producer_lock, flags);
if (qp->resp.state == QP_STATE_ERROR)
rxe_run_task(&qp->resp.task, 1);
err1:
return err;
}
static struct ib_cq *rxe_create_cq(struct ib_device *dev,
const struct ib_cq_init_attr *attr,
struct ib_ucontext *context,
struct ib_udata *udata)
{
int err;
struct rxe_dev *rxe = to_rdev(dev);
struct rxe_cq *cq;
if (attr->flags)
return ERR_PTR(-EINVAL);
err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector, udata);
if (err)
goto err1;
cq = rxe_alloc(&rxe->cq_pool);
if (!cq) {
err = -ENOMEM;
goto err1;
}
err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector,
context, udata);
if (err)
goto err2;
return &cq->ibcq;
err2:
rxe_drop_ref(cq);
err1:
return ERR_PTR(err);
}
static int rxe_destroy_cq(struct ib_cq *ibcq)
{
struct rxe_cq *cq = to_rcq(ibcq);
rxe_drop_ref(cq);
return 0;
}
static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
{
int err;
struct rxe_cq *cq = to_rcq(ibcq);
struct rxe_dev *rxe = to_rdev(ibcq->device);
err = rxe_cq_chk_attr(rxe, cq, cqe, 0, udata);
if (err)
goto err1;
err = rxe_cq_resize_queue(cq, cqe, udata);
if (err)
goto err1;
return 0;
err1:
return err;
}
static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
{
int i;
struct rxe_cq *cq = to_rcq(ibcq);
struct rxe_cqe *cqe;
unsigned long flags;
spin_lock_irqsave(&cq->cq_lock, flags);
for (i = 0; i < num_entries; i++) {
cqe = queue_head(cq->queue);
if (!cqe)
break;
memcpy(wc++, &cqe->ibwc, sizeof(*wc));
advance_consumer(cq->queue);
}
spin_unlock_irqrestore(&cq->cq_lock, flags);
return i;
}
static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
{
struct rxe_cq *cq = to_rcq(ibcq);
int count = queue_count(cq->queue);
return (count > wc_cnt) ? wc_cnt : count;
}
static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
{
struct rxe_cq *cq = to_rcq(ibcq);
unsigned long irq_flags;
int ret = 0;
spin_lock_irqsave(&cq->cq_lock, irq_flags);
if (cq->notify != IB_CQ_NEXT_COMP)
cq->notify = flags & IB_CQ_SOLICITED_MASK;
if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !queue_empty(cq->queue))
ret = 1;
spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
return ret;
}
static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
{
struct rxe_dev *rxe = to_rdev(ibpd->device);
struct rxe_pd *pd = to_rpd(ibpd);
struct rxe_mem *mr;
int err;
mr = rxe_alloc(&rxe->mr_pool);
if (!mr) {
err = -ENOMEM;
goto err1;
}
rxe_add_index(mr);
rxe_add_ref(pd);
err = rxe_mem_init_dma(rxe, pd, access, mr);
if (err)
goto err2;
return &mr->ibmr;
err2:
rxe_drop_ref(pd);
rxe_drop_index(mr);
rxe_drop_ref(mr);
err1:
return ERR_PTR(err);
}
static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
u64 start,
u64 length,
u64 iova,
int access, struct ib_udata *udata)
{
int err;
struct rxe_dev *rxe = to_rdev(ibpd->device);
struct rxe_pd *pd = to_rpd(ibpd);
struct rxe_mem *mr;
mr = rxe_alloc(&rxe->mr_pool);
if (!mr) {
err = -ENOMEM;
goto err2;
}
rxe_add_index(mr);
rxe_add_ref(pd);
err = rxe_mem_init_user(rxe, pd, start, length, iova,
access, udata, mr);
if (err)
goto err3;
return &mr->ibmr;
err3:
rxe_drop_ref(pd);
rxe_drop_index(mr);
rxe_drop_ref(mr);
err2:
return ERR_PTR(err);
}
static int rxe_dereg_mr(struct ib_mr *ibmr)
{
struct rxe_mem *mr = to_rmr(ibmr);
mr->state = RXE_MEM_STATE_ZOMBIE;
rxe_drop_ref(mr->pd);
rxe_drop_index(mr);
rxe_drop_ref(mr);
return 0;
}
static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd,
enum ib_mr_type mr_type,
u32 max_num_sg)
{
struct rxe_dev *rxe = to_rdev(ibpd->device);
struct rxe_pd *pd = to_rpd(ibpd);
struct rxe_mem *mr;
int err;
if (mr_type != IB_MR_TYPE_MEM_REG)
return ERR_PTR(-EINVAL);
mr = rxe_alloc(&rxe->mr_pool);
if (!mr) {
err = -ENOMEM;
goto err1;
}
rxe_add_index(mr);
rxe_add_ref(pd);
err = rxe_mem_init_fast(rxe, pd, max_num_sg, mr);
if (err)
goto err2;
return &mr->ibmr;
err2:
rxe_drop_ref(pd);
rxe_drop_index(mr);
rxe_drop_ref(mr);
err1:
return ERR_PTR(err);
}
static int rxe_set_page(struct ib_mr *ibmr, u64 addr)
{
struct rxe_mem *mr = to_rmr(ibmr);
struct rxe_map *map;
struct rxe_phys_buf *buf;
if (unlikely(mr->nbuf == mr->num_buf))
return -ENOMEM;
map = mr->map[mr->nbuf / RXE_BUF_PER_MAP];
buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP];
buf->addr = addr;
buf->size = ibmr->page_size;
mr->nbuf++;
return 0;
}
static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
int sg_nents, unsigned int *sg_offset)
{
struct rxe_mem *mr = to_rmr(ibmr);
int n;
mr->nbuf = 0;
n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page);
mr->va = ibmr->iova;
mr->iova = ibmr->iova;
mr->length = ibmr->length;
mr->page_shift = ilog2(ibmr->page_size);
mr->page_mask = ibmr->page_size - 1;
mr->offset = mr->iova & mr->page_mask;
return n;
}
static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
{
int err;
struct rxe_dev *rxe = to_rdev(ibqp->device);
struct rxe_qp *qp = to_rqp(ibqp);
struct rxe_mc_grp *grp;
/* takes a ref on grp if successful */
err = rxe_mcast_get_grp(rxe, mgid, &grp);
if (err)
return err;
err = rxe_mcast_add_grp_elem(rxe, qp, grp);
rxe_drop_ref(grp);
return err;
}
static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
{
struct rxe_dev *rxe = to_rdev(ibqp->device);
struct rxe_qp *qp = to_rqp(ibqp);
return rxe_mcast_drop_grp_elem(rxe, qp, mgid);
}
static ssize_t parent_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct rxe_dev *rxe = container_of(device, struct rxe_dev,
ib_dev.dev);
return snprintf(buf, 16, "%s\n", rxe_parent_name(rxe, 1));
}
static DEVICE_ATTR_RO(parent);
static struct device_attribute *rxe_dev_attributes[] = {
&dev_attr_parent,
};
int rxe_register_device(struct rxe_dev *rxe)
{
int err;
int i;
struct ib_device *dev = &rxe->ib_dev;
strlcpy(dev->name, "rxe%d", IB_DEVICE_NAME_MAX);
strlcpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
dev->owner = THIS_MODULE;
dev->node_type = RDMA_NODE_IB_CA;
dev->phys_port_cnt = 1;
dev->num_comp_vectors = num_possible_cpus();
dev->dev.parent = rxe_dma_device(rxe);
dev->local_dma_lkey = 0;
addrconf_addr_eui48((unsigned char *)&dev->node_guid,
rxe->ndev->dev_addr);
dev->dev.dma_ops = &dma_virt_ops;
dma_coerce_mask_and_coherent(&dev->dev,
dma_get_required_mask(dev->dev.parent));
dev->uverbs_abi_ver = RXE_UVERBS_ABI_VERSION;
dev->uverbs_cmd_mask = BIT_ULL(IB_USER_VERBS_CMD_GET_CONTEXT)
| BIT_ULL(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)
| BIT_ULL(IB_USER_VERBS_CMD_QUERY_DEVICE)
| BIT_ULL(IB_USER_VERBS_CMD_QUERY_PORT)
| BIT_ULL(IB_USER_VERBS_CMD_ALLOC_PD)
| BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_PD)
| BIT_ULL(IB_USER_VERBS_CMD_CREATE_SRQ)
| BIT_ULL(IB_USER_VERBS_CMD_MODIFY_SRQ)
| BIT_ULL(IB_USER_VERBS_CMD_QUERY_SRQ)
| BIT_ULL(IB_USER_VERBS_CMD_DESTROY_SRQ)
| BIT_ULL(IB_USER_VERBS_CMD_POST_SRQ_RECV)
| BIT_ULL(IB_USER_VERBS_CMD_CREATE_QP)
| BIT_ULL(IB_USER_VERBS_CMD_MODIFY_QP)
| BIT_ULL(IB_USER_VERBS_CMD_QUERY_QP)
| BIT_ULL(IB_USER_VERBS_CMD_DESTROY_QP)
| BIT_ULL(IB_USER_VERBS_CMD_POST_SEND)
| BIT_ULL(IB_USER_VERBS_CMD_POST_RECV)
| BIT_ULL(IB_USER_VERBS_CMD_CREATE_CQ)
| BIT_ULL(IB_USER_VERBS_CMD_RESIZE_CQ)
| BIT_ULL(IB_USER_VERBS_CMD_DESTROY_CQ)
| BIT_ULL(IB_USER_VERBS_CMD_POLL_CQ)
| BIT_ULL(IB_USER_VERBS_CMD_PEEK_CQ)
| BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)
| BIT_ULL(IB_USER_VERBS_CMD_REG_MR)
| BIT_ULL(IB_USER_VERBS_CMD_DEREG_MR)
| BIT_ULL(IB_USER_VERBS_CMD_CREATE_AH)
| BIT_ULL(IB_USER_VERBS_CMD_MODIFY_AH)
| BIT_ULL(IB_USER_VERBS_CMD_QUERY_AH)
| BIT_ULL(IB_USER_VERBS_CMD_DESTROY_AH)
| BIT_ULL(IB_USER_VERBS_CMD_ATTACH_MCAST)
| BIT_ULL(IB_USER_VERBS_CMD_DETACH_MCAST)
;
dev->query_device = rxe_query_device;
dev->modify_device = rxe_modify_device;
dev->query_port = rxe_query_port;
dev->modify_port = rxe_modify_port;
dev->get_link_layer = rxe_get_link_layer;
dev->query_gid = rxe_query_gid;
dev->get_netdev = rxe_get_netdev;
dev->add_gid = rxe_add_gid;
dev->del_gid = rxe_del_gid;
dev->query_pkey = rxe_query_pkey;
dev->alloc_ucontext = rxe_alloc_ucontext;
dev->dealloc_ucontext = rxe_dealloc_ucontext;
dev->mmap = rxe_mmap;
dev->get_port_immutable = rxe_port_immutable;
dev->alloc_pd = rxe_alloc_pd;
dev->dealloc_pd = rxe_dealloc_pd;
dev->create_ah = rxe_create_ah;
dev->modify_ah = rxe_modify_ah;
dev->query_ah = rxe_query_ah;
dev->destroy_ah = rxe_destroy_ah;
dev->create_srq = rxe_create_srq;
dev->modify_srq = rxe_modify_srq;
dev->query_srq = rxe_query_srq;
dev->destroy_srq = rxe_destroy_srq;
dev->post_srq_recv = rxe_post_srq_recv;
dev->create_qp = rxe_create_qp;
dev->modify_qp = rxe_modify_qp;
dev->query_qp = rxe_query_qp;
dev->destroy_qp = rxe_destroy_qp;
dev->post_send = rxe_post_send;
dev->post_recv = rxe_post_recv;
dev->create_cq = rxe_create_cq;
dev->destroy_cq = rxe_destroy_cq;
dev->resize_cq = rxe_resize_cq;
dev->poll_cq = rxe_poll_cq;
dev->peek_cq = rxe_peek_cq;
dev->req_notify_cq = rxe_req_notify_cq;
dev->get_dma_mr = rxe_get_dma_mr;
dev->reg_user_mr = rxe_reg_user_mr;
dev->dereg_mr = rxe_dereg_mr;
dev->alloc_mr = rxe_alloc_mr;
dev->map_mr_sg = rxe_map_mr_sg;
dev->attach_mcast = rxe_attach_mcast;
dev->detach_mcast = rxe_detach_mcast;
dev->get_hw_stats = rxe_ib_get_hw_stats;
dev->alloc_hw_stats = rxe_ib_alloc_hw_stats;
rxe->tfm = crypto_alloc_shash("crc32", 0, 0);
if (IS_ERR(rxe->tfm)) {
pr_err("failed to allocate crc algorithm err:%ld\n",
PTR_ERR(rxe->tfm));
return PTR_ERR(rxe->tfm);
}
err = ib_register_device(dev, NULL);
if (err) {
pr_warn("%s failed with error %d\n", __func__, err);
goto err1;
}
for (i = 0; i < ARRAY_SIZE(rxe_dev_attributes); ++i) {
err = device_create_file(&dev->dev, rxe_dev_attributes[i]);
if (err) {
pr_warn("%s failed with error %d for attr number %d\n",
__func__, err, i);
goto err2;
}
}
return 0;
err2:
ib_unregister_device(dev);
err1:
crypto_free_shash(rxe->tfm);
return err;
}
int rxe_unregister_device(struct rxe_dev *rxe)
{
int i;
struct ib_device *dev = &rxe->ib_dev;
for (i = 0; i < ARRAY_SIZE(rxe_dev_attributes); ++i)
device_remove_file(&dev->dev, rxe_dev_attributes[i]);
ib_unregister_device(dev);
return 0;
}
|