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
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2017, Microsoft Corporation.
* Copyright (c) 2025, Stefan Metzmacher
*/
#include "internal.h"
#include <linux/folio_queue.h>
struct smbdirect_map_sges {
struct ib_sge *sge;
size_t num_sge;
size_t max_sge;
struct ib_device *device;
u32 local_dma_lkey;
enum dma_data_direction direction;
};
static ssize_t smbdirect_map_sges_from_iter(struct iov_iter *iter, size_t len,
struct smbdirect_map_sges *state);
static void smbdirect_connection_recv_io_refill_work(struct work_struct *work);
static void smbdirect_connection_send_immediate_work(struct work_struct *work);
static void smbdirect_connection_qp_event_handler(struct ib_event *event, void *context)
{
struct smbdirect_socket *sc = context;
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
"%s on device %.*s socket %p (cm_id=%p) status %s first_error %1pe\n",
ib_event_msg(event->event),
IB_DEVICE_NAME_MAX,
event->device->name,
sc, sc->rdma.cm_id,
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error));
switch (event->event) {
case IB_EVENT_CQ_ERR:
case IB_EVENT_QP_FATAL:
smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED);
break;
default:
break;
}
}
static int smbdirect_connection_rdma_event_handler(struct rdma_cm_id *id,
struct rdma_cm_event *event)
{
struct smbdirect_socket *sc = id->context;
int ret = -ECONNRESET;
if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
ret = -ENETDOWN;
if (IS_ERR(SMBDIRECT_DEBUG_ERR_PTR(event->status)))
ret = event->status;
/*
* cma_cm_event_handler() has
* lockdep_assert_held(&id_priv->handler_mutex);
*
* Mutexes are not allowed in interrupts,
* and we rely on not being in an interrupt here.
*/
WARN_ON_ONCE(in_interrupt());
if (event->event != sc->rdma.expected_event) {
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
"%s (first_error=%1pe, expected=%s) => event=%s status=%d => ret=%1pe\n",
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
rdma_event_msg(sc->rdma.expected_event),
rdma_event_msg(event->event),
event->status,
SMBDIRECT_DEBUG_ERR_PTR(ret));
/*
* If we get RDMA_CM_EVENT_DEVICE_REMOVAL,
* we should change to SMBDIRECT_SOCKET_DISCONNECTED,
* so that rdma_disconnect() is avoided later via
* smbdirect_socket_schedule_cleanup[_status]() =>
* smbdirect_socket_cleanup_work().
*
* As otherwise we'd set SMBDIRECT_SOCKET_DISCONNECTING,
* but never ever get RDMA_CM_EVENT_DISCONNECTED and
* never reach SMBDIRECT_SOCKET_DISCONNECTED.
*/
if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
smbdirect_socket_schedule_cleanup_status(sc,
SMBDIRECT_LOG_ERR,
ret,
SMBDIRECT_SOCKET_DISCONNECTED);
else
smbdirect_socket_schedule_cleanup(sc, ret);
if (sc->ib.qp)
ib_drain_qp(sc->ib.qp);
return 0;
}
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO,
"%s (first_error=%1pe) event=%s\n",
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
rdma_event_msg(event->event));
switch (event->event) {
case RDMA_CM_EVENT_DISCONNECTED:
/*
* We need to change to SMBDIRECT_SOCKET_DISCONNECTED,
* so that rdma_disconnect() is avoided later via
* smbdirect_socket_schedule_cleanup_status() =>
* smbdirect_socket_cleanup_work().
*
* As otherwise we'd set SMBDIRECT_SOCKET_DISCONNECTING,
* but never ever get RDMA_CM_EVENT_DISCONNECTED and
* never reach SMBDIRECT_SOCKET_DISCONNECTED.
*
* This is also a normal disconnect so
* SMBDIRECT_LOG_INFO should be good enough
* and avoids spamming the default logs.
*/
smbdirect_socket_schedule_cleanup_status(sc,
SMBDIRECT_LOG_INFO,
ret,
SMBDIRECT_SOCKET_DISCONNECTED);
if (sc->ib.qp)
ib_drain_qp(sc->ib.qp);
return 0;
default:
break;
}
/*
* This is an internal error, should be handled above via
* event->event != sc->rdma.expected_event already.
*/
WARN_ON_ONCE(sc->rdma.expected_event != RDMA_CM_EVENT_DISCONNECTED);
smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED);
return 0;
}
void smbdirect_connection_rdma_established(struct smbdirect_socket *sc)
{
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO,
"rdma established: device: %.*s local: %pISpsfc remote: %pISpsfc\n",
IB_DEVICE_NAME_MAX,
sc->ib.dev->name,
&sc->rdma.cm_id->route.addr.src_addr,
&sc->rdma.cm_id->route.addr.dst_addr);
sc->rdma.cm_id->event_handler = smbdirect_connection_rdma_event_handler;
sc->rdma.expected_event = RDMA_CM_EVENT_DISCONNECTED;
}
void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc)
{
if (unlikely(sc->first_error))
return;
if (sc->status == SMBDIRECT_SOCKET_CONNECTED)
/*
* This is the accept case where
* smbdirect_socket_accept() already sets
* SMBDIRECT_SOCKET_CONNECTED
*/
goto done;
if (sc->status != SMBDIRECT_SOCKET_NEGOTIATE_RUNNING) {
/*
* Something went wrong...
*/
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
"status=%s first_error=%1pe local: %pISpsfc remote: %pISpsfc\n",
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
&sc->rdma.cm_id->route.addr.src_addr,
&sc->rdma.cm_id->route.addr.dst_addr);
return;
}
/*
* We are done, so we can wake up the waiter.
*/
WARN_ONCE(sc->status == SMBDIRECT_SOCKET_CONNECTED,
"status=%s first_error=%1pe",
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error));
sc->status = SMBDIRECT_SOCKET_CONNECTED;
/*
* We need to setup the refill and send immediate work
* in order to get a working connection.
*/
done:
INIT_WORK(&sc->recv_io.posted.refill_work, smbdirect_connection_recv_io_refill_work);
INIT_WORK(&sc->idle.immediate_work, smbdirect_connection_send_immediate_work);
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO,
"negotiated: local: %pISpsfc remote: %pISpsfc\n",
&sc->rdma.cm_id->route.addr.src_addr,
&sc->rdma.cm_id->route.addr.dst_addr);
wake_up(&sc->status_wait);
}
static u32 smbdirect_rdma_rw_send_wrs(struct ib_device *dev,
const struct ib_qp_init_attr *attr)
{
/*
* This could be split out of rdma_rw_init_qp()
* and be a helper function next to rdma_rw_mr_factor()
*
* We can't check unlikely(rdma_rw_force_mr) here,
* but that is most likely 0 anyway.
*/
u32 factor;
WARN_ON_ONCE(attr->port_num == 0);
/*
* Each context needs at least one RDMA READ or WRITE WR.
*
* For some hardware we might need more, eventually we should ask the
* HCA driver for a multiplier here.
*/
factor = 1;
/*
* If the device needs MRs to perform RDMA READ or WRITE operations,
* we'll need two additional MRs for the registrations and the
* invalidation.
*/
if (rdma_protocol_iwarp(dev, attr->port_num) || dev->attrs.max_sgl_rd)
factor += 2; /* inv + reg */
return factor * attr->cap.max_rdma_ctxs;
}
int smbdirect_connection_create_qp(struct smbdirect_socket *sc)
{
const struct smbdirect_socket_parameters *sp = &sc->parameters;
struct ib_qp_init_attr qp_attr;
struct ib_qp_cap qp_cap;
u32 rdma_send_wr;
u32 max_send_wr;
int ret;
/*
* Note that {rdma,ib}_create_qp() will call
* rdma_rw_init_qp() if max_rdma_ctxs is not 0.
* It will adjust max_send_wr to the required
* number of additional WRs for the RDMA RW operations.
* It will cap max_send_wr to the device limit.
*
* We use allocate sp->responder_resources * 2 MRs
* and each MR needs WRs for REG and INV, so
* we use '* 4'.
*
* +1 for ib_drain_qp()
*/
memset(&qp_cap, 0, sizeof(qp_cap));
qp_cap.max_send_wr = sp->send_credit_target + sp->responder_resources * 4 + 1;
qp_cap.max_recv_wr = sp->recv_credit_max + 1;
qp_cap.max_send_sge = SMBDIRECT_SEND_IO_MAX_SGE;
qp_cap.max_recv_sge = SMBDIRECT_RECV_IO_MAX_SGE;
qp_cap.max_inline_data = 0;
qp_cap.max_rdma_ctxs = sc->rw_io.credits.max;
/*
* Find out the number of max_send_wr
* after rdma_rw_init_qp() adjusted it.
*
* We only do it on a temporary variable,
* as rdma_create_qp() will trigger
* rdma_rw_init_qp() again.
*/
memset(&qp_attr, 0, sizeof(qp_attr));
qp_attr.cap = qp_cap;
qp_attr.port_num = sc->rdma.cm_id->port_num;
rdma_send_wr = smbdirect_rdma_rw_send_wrs(sc->ib.dev, &qp_attr);
max_send_wr = qp_cap.max_send_wr + rdma_send_wr;
if (qp_cap.max_send_wr > sc->ib.dev->attrs.max_cqe ||
qp_cap.max_send_wr > sc->ib.dev->attrs.max_qp_wr) {
pr_err("Possible CQE overrun: max_send_wr %d\n",
qp_cap.max_send_wr);
pr_err("device %.*s reporting max_cqe %d max_qp_wr %d\n",
IB_DEVICE_NAME_MAX,
sc->ib.dev->name,
sc->ib.dev->attrs.max_cqe,
sc->ib.dev->attrs.max_qp_wr);
pr_err("consider lowering send_credit_target = %d\n",
sp->send_credit_target);
return -EINVAL;
}
if (qp_cap.max_rdma_ctxs &&
(max_send_wr >= sc->ib.dev->attrs.max_cqe ||
max_send_wr >= sc->ib.dev->attrs.max_qp_wr)) {
pr_err("Possible CQE overrun: rdma_send_wr %d + max_send_wr %d = %d\n",
rdma_send_wr, qp_cap.max_send_wr, max_send_wr);
pr_err("device %.*s reporting max_cqe %d max_qp_wr %d\n",
IB_DEVICE_NAME_MAX,
sc->ib.dev->name,
sc->ib.dev->attrs.max_cqe,
sc->ib.dev->attrs.max_qp_wr);
pr_err("consider lowering send_credit_target = %d, max_rdma_ctxs = %d\n",
sp->send_credit_target, qp_cap.max_rdma_ctxs);
return -EINVAL;
}
if (qp_cap.max_recv_wr > sc->ib.dev->attrs.max_cqe ||
qp_cap.max_recv_wr > sc->ib.dev->attrs.max_qp_wr) {
pr_err("Possible CQE overrun: max_recv_wr %d\n",
qp_cap.max_recv_wr);
pr_err("device %.*s reporting max_cqe %d max_qp_wr %d\n",
IB_DEVICE_NAME_MAX,
sc->ib.dev->name,
sc->ib.dev->attrs.max_cqe,
sc->ib.dev->attrs.max_qp_wr);
pr_err("consider lowering receive_credit_max = %d\n",
sp->recv_credit_max);
return -EINVAL;
}
if (qp_cap.max_send_sge > sc->ib.dev->attrs.max_send_sge ||
qp_cap.max_recv_sge > sc->ib.dev->attrs.max_recv_sge) {
pr_err("device %.*s max_send_sge/max_recv_sge = %d/%d too small\n",
IB_DEVICE_NAME_MAX,
sc->ib.dev->name,
sc->ib.dev->attrs.max_send_sge,
sc->ib.dev->attrs.max_recv_sge);
return -EINVAL;
}
sc->ib.pd = ib_alloc_pd(sc->ib.dev, 0);
if (IS_ERR(sc->ib.pd)) {
pr_err("Can't create RDMA PD: %1pe\n", sc->ib.pd);
ret = PTR_ERR(sc->ib.pd);
sc->ib.pd = NULL;
return ret;
}
sc->ib.send_cq = ib_alloc_cq_any(sc->ib.dev, sc,
max_send_wr,
sc->ib.poll_ctx);
if (IS_ERR(sc->ib.send_cq)) {
pr_err("Can't create RDMA send CQ: %1pe\n", sc->ib.send_cq);
ret = PTR_ERR(sc->ib.send_cq);
sc->ib.send_cq = NULL;
goto err;
}
sc->ib.recv_cq = ib_alloc_cq_any(sc->ib.dev, sc,
qp_cap.max_recv_wr,
sc->ib.poll_ctx);
if (IS_ERR(sc->ib.recv_cq)) {
pr_err("Can't create RDMA recv CQ: %1pe\n", sc->ib.recv_cq);
ret = PTR_ERR(sc->ib.recv_cq);
sc->ib.recv_cq = NULL;
goto err;
}
/*
* We reset completely here!
* As the above use was just temporary
* to calc max_send_wr and rdma_send_wr.
*
* rdma_create_qp() will trigger rdma_rw_init_qp()
* again if max_rdma_ctxs is not 0.
*/
memset(&qp_attr, 0, sizeof(qp_attr));
qp_attr.event_handler = smbdirect_connection_qp_event_handler;
qp_attr.qp_context = sc;
qp_attr.cap = qp_cap;
qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
qp_attr.qp_type = IB_QPT_RC;
qp_attr.send_cq = sc->ib.send_cq;
qp_attr.recv_cq = sc->ib.recv_cq;
qp_attr.port_num = ~0;
ret = rdma_create_qp(sc->rdma.cm_id, sc->ib.pd, &qp_attr);
if (ret) {
pr_err("Can't create RDMA QP: %1pe\n",
SMBDIRECT_DEBUG_ERR_PTR(ret));
goto err;
}
sc->ib.qp = sc->rdma.cm_id->qp;
return 0;
err:
smbdirect_connection_destroy_qp(sc);
return ret;
}
void smbdirect_connection_destroy_qp(struct smbdirect_socket *sc)
{
if (sc->ib.qp) {
ib_drain_qp(sc->ib.qp);
sc->ib.qp = NULL;
rdma_destroy_qp(sc->rdma.cm_id);
}
if (sc->ib.recv_cq) {
ib_destroy_cq(sc->ib.recv_cq);
sc->ib.recv_cq = NULL;
}
if (sc->ib.send_cq) {
ib_destroy_cq(sc->ib.send_cq);
sc->ib.send_cq = NULL;
}
if (sc->ib.pd) {
ib_dealloc_pd(sc->ib.pd);
sc->ib.pd = NULL;
}
}
int smbdirect_connection_create_mem_pools(struct smbdirect_socket *sc)
{
const struct smbdirect_socket_parameters *sp = &sc->parameters;
char name[80];
size_t i;
/*
* We use sizeof(struct smbdirect_negotiate_resp) for the
* payload size as it is larger as
* sizeof(struct smbdirect_data_transfer).
*
* This will fit client and server usage for now.
*/
snprintf(name, sizeof(name), "smbdirect_send_io_cache_%p", sc);
struct kmem_cache_args send_io_args = {
.align = __alignof__(struct smbdirect_send_io),
};
sc->send_io.mem.cache = kmem_cache_create(name,
sizeof(struct smbdirect_send_io) +
sizeof(struct smbdirect_negotiate_resp),
&send_io_args,
SLAB_HWCACHE_ALIGN);
if (!sc->send_io.mem.cache)
goto err;
sc->send_io.mem.pool = mempool_create_slab_pool(sp->send_credit_target,
sc->send_io.mem.cache);
if (!sc->send_io.mem.pool)
goto err;
/*
* A payload size of sp->max_recv_size should fit
* any message.
*
* For smbdirect_data_transfer messages the whole
* buffer might be exposed to userspace
* (currently on the client side...)
* The documentation says data_offset = 0 would be
* strange but valid.
*/
snprintf(name, sizeof(name), "smbdirect_recv_io_cache_%p", sc);
struct kmem_cache_args recv_io_args = {
.align = __alignof__(struct smbdirect_recv_io),
.useroffset = sizeof(struct smbdirect_recv_io),
.usersize = sp->max_recv_size,
};
sc->recv_io.mem.cache = kmem_cache_create(name,
sizeof(struct smbdirect_recv_io) +
sp->max_recv_size,
&recv_io_args,
SLAB_HWCACHE_ALIGN);
if (!sc->recv_io.mem.cache)
goto err;
sc->recv_io.mem.pool = mempool_create_slab_pool(sp->recv_credit_max,
sc->recv_io.mem.cache);
if (!sc->recv_io.mem.pool)
goto err;
for (i = 0; i < sp->recv_credit_max; i++) {
struct smbdirect_recv_io *recv_io;
recv_io = mempool_alloc(sc->recv_io.mem.pool,
sc->recv_io.mem.gfp_mask);
if (!recv_io)
goto err;
recv_io->socket = sc;
recv_io->sge.length = 0;
list_add_tail(&recv_io->list, &sc->recv_io.free.list);
}
return 0;
err:
smbdirect_connection_destroy_mem_pools(sc);
return -ENOMEM;
}
void smbdirect_connection_destroy_mem_pools(struct smbdirect_socket *sc)
{
struct smbdirect_recv_io *recv_io, *next_io;
list_for_each_entry_safe(recv_io, next_io, &sc->recv_io.free.list, list) {
list_del(&recv_io->list);
mempool_free(recv_io, sc->recv_io.mem.pool);
}
/*
* Note mempool_destroy() and kmem_cache_destroy()
* work fine with a NULL pointer
*/
mempool_destroy(sc->recv_io.mem.pool);
sc->recv_io.mem.pool = NULL;
kmem_cache_destroy(sc->recv_io.mem.cache);
sc->recv_io.mem.cache = NULL;
mempool_destroy(sc->send_io.mem.pool);
sc->send_io.mem.pool = NULL;
kmem_cache_destroy(sc->send_io.mem.cache);
sc->send_io.mem.cache = NULL;
}
struct smbdirect_send_io *smbdirect_connection_alloc_send_io(struct smbdirect_socket *sc)
{
struct smbdirect_send_io *msg;
msg = mempool_alloc(sc->send_io.mem.pool, sc->send_io.mem.gfp_mask);
if (!msg)
return ERR_PTR(-ENOMEM);
msg->socket = sc;
INIT_LIST_HEAD(&msg->sibling_list);
msg->num_sge = 0;
return msg;
}
void smbdirect_connection_free_send_io(struct smbdirect_send_io *msg)
{
struct smbdirect_socket *sc = msg->socket;
size_t i;
/*
* The list needs to be empty!
* The caller should take care of it.
*/
WARN_ON_ONCE(!list_empty(&msg->sibling_list));
/*
* Note we call ib_dma_unmap_page(), even if some sges are mapped using
* ib_dma_map_single().
*
* The difference between _single() and _page() only matters for the
* ib_dma_map_*() case.
*
* For the ib_dma_unmap_*() case it does not matter as both take the
* dma_addr_t and dma_unmap_single_attrs() is just an alias to
* dma_unmap_page_attrs().
*/
for (i = 0; i < msg->num_sge; i++)
ib_dma_unmap_page(sc->ib.dev,
msg->sge[i].addr,
msg->sge[i].length,
DMA_TO_DEVICE);
mempool_free(msg, sc->send_io.mem.pool);
}
struct smbdirect_recv_io *smbdirect_connection_get_recv_io(struct smbdirect_socket *sc)
{
struct smbdirect_recv_io *msg = NULL;
unsigned long flags;
spin_lock_irqsave(&sc->recv_io.free.lock, flags);
if (likely(!sc->first_error))
msg = list_first_entry_or_null(&sc->recv_io.free.list,
struct smbdirect_recv_io,
list);
if (likely(msg)) {
list_del(&msg->list);
sc->statistics.get_receive_buffer++;
}
spin_unlock_irqrestore(&sc->recv_io.free.lock, flags);
return msg;
}
void smbdirect_connection_put_recv_io(struct smbdirect_recv_io *msg)
{
struct smbdirect_socket *sc = msg->socket;
unsigned long flags;
if (likely(msg->sge.length != 0)) {
ib_dma_unmap_single(sc->ib.dev,
msg->sge.addr,
msg->sge.length,
DMA_FROM_DEVICE);
msg->sge.length = 0;
}
spin_lock_irqsave(&sc->recv_io.free.lock, flags);
list_add_tail(&msg->list, &sc->recv_io.free.list);
sc->statistics.put_receive_buffer++;
spin_unlock_irqrestore(&sc->recv_io.free.lock, flags);
queue_work(sc->workqueues.refill, &sc->recv_io.posted.refill_work);
}
void smbdirect_connection_reassembly_append_recv_io(struct smbdirect_socket *sc,
struct smbdirect_recv_io *msg,
u32 data_length)
{
unsigned long flags;
spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
list_add_tail(&msg->list, &sc->recv_io.reassembly.list);
sc->recv_io.reassembly.queue_length++;
/*
* Make sure reassembly_data_length is updated after list and
* reassembly_queue_length are updated. On the dequeue side
* reassembly_data_length is checked without a lock to determine
* if reassembly_queue_length and list is up to date
*/
virt_wmb();
sc->recv_io.reassembly.data_length += data_length;
spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
sc->statistics.enqueue_reassembly_queue++;
}
struct smbdirect_recv_io *
smbdirect_connection_reassembly_first_recv_io(struct smbdirect_socket *sc)
{
struct smbdirect_recv_io *msg;
msg = list_first_entry_or_null(&sc->recv_io.reassembly.list,
struct smbdirect_recv_io,
list);
return msg;
}
void smbdirect_connection_negotiate_rdma_resources(struct smbdirect_socket *sc,
u8 peer_initiator_depth,
u8 peer_responder_resources,
const struct rdma_conn_param *param)
{
struct smbdirect_socket_parameters *sp = &sc->parameters;
if (rdma_protocol_iwarp(sc->ib.dev, sc->rdma.cm_id->port_num) &&
param->private_data_len == 8) {
/*
* Legacy clients with only iWarp MPA v1 support
* need a private blob in order to negotiate
* the IRD/ORD values.
*/
const __be32 *ird_ord_hdr = param->private_data;
u32 ird32 = be32_to_cpu(ird_ord_hdr[0]);
u32 ord32 = be32_to_cpu(ird_ord_hdr[1]);
/*
* cifs.ko sends the legacy IRD/ORD negotiation
* event if iWarp MPA v2 was used.
*
* Here we check that the values match and only
* mark the client as legacy if they don't match.
*/
if ((u32)param->initiator_depth != ird32 ||
(u32)param->responder_resources != ord32) {
/*
* There are broken clients (old cifs.ko)
* using little endian and also
* struct rdma_conn_param only uses u8
* for initiator_depth and responder_resources,
* so we truncate the value to U8_MAX.
*
* smb_direct_accept_client() will then
* do the real negotiation in order to
* select the minimum between client and
* server.
*/
ird32 = min_t(u32, ird32, U8_MAX);
ord32 = min_t(u32, ord32, U8_MAX);
sc->rdma.legacy_iwarp = true;
peer_initiator_depth = (u8)ird32;
peer_responder_resources = (u8)ord32;
}
}
/*
* negotiate the value by using the minimum
* between client and server if the client provided
* non 0 values.
*/
if (peer_initiator_depth != 0)
sp->initiator_depth = min_t(u8, sp->initiator_depth,
peer_initiator_depth);
if (peer_responder_resources != 0)
sp->responder_resources = min_t(u8, sp->responder_resources,
peer_responder_resources);
}
bool smbdirect_connection_is_connected(struct smbdirect_socket *sc)
{
if (unlikely(!sc || sc->first_error || sc->status != SMBDIRECT_SOCKET_CONNECTED))
return false;
return true;
}
__SMBDIRECT_EXPORT_SYMBOL__(smbdirect_connection_is_connected);
int smbdirect_connection_wait_for_connected(struct smbdirect_socket *sc)
{
const struct smbdirect_socket_parameters *sp = &sc->parameters;
union {
struct sockaddr sa;
struct sockaddr_storage ss;
} src_addr, dst_addr;
const struct sockaddr *src = NULL;
const struct sockaddr *dst = NULL;
char _devname[IB_DEVICE_NAME_MAX] = { 0, };
const char *devname = NULL;
int ret;
if (sc->rdma.cm_id) {
src_addr.ss = sc->rdma.cm_id->route.addr.src_addr;
if (src_addr.sa.sa_family != AF_UNSPEC)
src = &src_addr.sa;
dst_addr.ss = sc->rdma.cm_id->route.addr.dst_addr;
if (dst_addr.sa.sa_family != AF_UNSPEC)
dst = &dst_addr.sa;
if (sc->ib.dev) {
memcpy(_devname, sc->ib.dev->name, IB_DEVICE_NAME_MAX);
devname = _devname;
}
}
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO,
"waiting for connection: device: %.*s local: %pISpsfc remote: %pISpsfc\n",
IB_DEVICE_NAME_MAX, devname, src, dst);
ret = wait_event_interruptible_timeout(sc->status_wait,
sc->status == SMBDIRECT_SOCKET_CONNECTED ||
sc->first_error,
msecs_to_jiffies(sp->negotiate_timeout_msec));
if (sc->rdma.cm_id) {
/*
* Maybe src and dev are updated in the meantime.
*/
src_addr.ss = sc->rdma.cm_id->route.addr.src_addr;
if (src_addr.sa.sa_family != AF_UNSPEC)
src = &src_addr.sa;
dst_addr.ss = sc->rdma.cm_id->route.addr.dst_addr;
if (dst_addr.sa.sa_family != AF_UNSPEC)
dst = &dst_addr.sa;
if (sc->ib.dev) {
memcpy(_devname, sc->ib.dev->name, IB_DEVICE_NAME_MAX);
devname = _devname;
}
}
if (ret == 0)
ret = -ETIMEDOUT;
if (ret < 0)
smbdirect_socket_schedule_cleanup(sc, ret);
if (sc->first_error) {
int lvl = SMBDIRECT_LOG_ERR;
ret = sc->first_error;
if (ret == -ENODEV)
lvl = SMBDIRECT_LOG_INFO;
smbdirect_log_rdma_event(sc, lvl,
"connection failed %1pe device: %.*s local: %pISpsfc remote: %pISpsfc\n",
SMBDIRECT_DEBUG_ERR_PTR(ret),
IB_DEVICE_NAME_MAX, devname, src, dst);
return ret;
}
return 0;
}
__SMBDIRECT_EXPORT_SYMBOL__(smbdirect_connection_wait_for_connected);
void smbdirect_connection_idle_timer_work(struct work_struct *work)
{
struct smbdirect_socket *sc =
container_of(work, struct smbdirect_socket, idle.timer_work.work);
const struct smbdirect_socket_parameters *sp = &sc->parameters;
if (sc->idle.keepalive != SMBDIRECT_KEEPALIVE_NONE) {
smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_ERR,
"%s => timeout sc->idle.keepalive=%s\n",
smbdirect_socket_status_string(sc->status),
sc->idle.keepalive == SMBDIRECT_KEEPALIVE_SENT ?
"SENT" : "PENDING");
smbdirect_socket_schedule_cleanup(sc, -ETIMEDOUT);
return;
}
if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
return;
/*
* Now use the keepalive timeout (instead of keepalive interval)
* in order to wait for a response
*/
sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING;
mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work,
msecs_to_jiffies(sp->keepalive_timeout_msec));
smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_INFO,
"schedule send of empty idle message\n");
queue_work(sc->workqueues.immediate, &sc->idle.immediate_work);
}
u16 smbdirect_connection_grant_recv_credits(struct smbdirect_socket *sc)
{
int missing;
int available;
int new_credits;
if (atomic_read(&sc->recv_io.credits.count) >= sc->recv_io.credits.target)
return 0;
missing = (int)sc->recv_io.credits.target - atomic_read(&sc->recv_io.credits.count);
available = atomic_xchg(&sc->recv_io.credits.available, 0);
new_credits = min3((int)U16_MAX, missing, available);
if (new_credits <= 0) {
/*
* If credits are available, but not granted
* we need to re-add them again.
*/
if (available)
atomic_add(available, &sc->recv_io.credits.available);
return 0;
}
if (new_credits < available) {
/*
* Readd the remaining available again.
*/
available -= new_credits;
atomic_add(available, &sc->recv_io.credits.available);
}
/*
* Remember we granted the credits
*/
atomic_add(new_credits, &sc->recv_io.credits.count);
return new_credits;
}
static bool smbdirect_connection_request_keep_alive(struct smbdirect_socket *sc)
{
const struct smbdirect_socket_parameters *sp = &sc->parameters;
if (sc->idle.keepalive == SMBDIRECT_KEEPALIVE_PENDING) {
sc->idle.keepalive = SMBDIRECT_KEEPALIVE_SENT;
/*
* Now use the keepalive timeout (instead of keepalive interval)
* in order to wait for a response
*/
mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work,
msecs_to_jiffies(sp->keepalive_timeout_msec));
return true;
}
return false;
}
int smbdirect_connection_post_send_wr(struct smbdirect_socket *sc,
struct ib_send_wr *wr)
{
int ret;
if (unlikely(sc->first_error))
return sc->first_error;
atomic_inc(&sc->send_io.pending.count);
ret = ib_post_send(sc->ib.qp, wr, NULL);
if (ret) {
atomic_dec(&sc->send_io.pending.count);
smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR,
"ib_post_send() failed %1pe\n",
SMBDIRECT_DEBUG_ERR_PTR(ret));
smbdirect_socket_schedule_cleanup(sc, ret);
}
return ret;
}
static void smbdirect_connection_send_batch_init(struct smbdirect_send_batch *batch,
bool need_invalidate_rkey,
unsigned int remote_key)
{
INIT_LIST_HEAD(&batch->msg_list);
batch->wr_cnt = 0;
batch->need_invalidate_rkey = need_invalidate_rkey;
batch->remote_key = remote_key;
batch->credit = 0;
}
int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc,
struct smbdirect_send_batch *batch,
bool is_last)
{
struct smbdirect_send_io *first, *last;
int ret = 0;
if (list_empty(&batch->msg_list))
goto release_credit;
first = list_first_entry(&batch->msg_list,
struct smbdirect_send_io,
sibling_list);
last = list_last_entry(&batch->msg_list,
struct smbdirect_send_io,
sibling_list);
if (batch->need_invalidate_rkey) {
first->wr.opcode = IB_WR_SEND_WITH_INV;
first->wr.ex.invalidate_rkey = batch->remote_key;
batch->need_invalidate_rkey = false;
batch->remote_key = 0;
}
last->wr.send_flags = IB_SEND_SIGNALED;
last->wr.wr_cqe = &last->cqe;
/*
* Remove last from send_ctx->msg_list
* and splice the rest of send_ctx->msg_list
* to last->sibling_list.
*
* send_ctx->msg_list is a valid empty list
* at the end.
*/
list_del_init(&last->sibling_list);
list_splice_tail_init(&batch->msg_list, &last->sibling_list);
batch->wr_cnt = 0;
ret = smbdirect_connection_post_send_wr(sc, &first->wr);
if (ret) {
struct smbdirect_send_io *sibling, *next;
list_for_each_entry_safe(sibling, next, &last->sibling_list, sibling_list) {
list_del_init(&sibling->sibling_list);
smbdirect_connection_free_send_io(sibling);
}
smbdirect_connection_free_send_io(last);
}
release_credit:
if (is_last && !ret && batch->credit) {
atomic_add(batch->credit, &sc->send_io.bcredits.count);
batch->credit = 0;
wake_up(&sc->send_io.bcredits.wait_queue);
}
return ret;
}
__SMBDIRECT_EXPORT_SYMBOL__(smbdirect_connection_send_batch_flush);
struct smbdirect_send_batch *
smbdirect_init_send_batch_storage(struct smbdirect_send_batch_storage *storage,
bool need_invalidate_rkey,
unsigned int remote_key)
{
struct smbdirect_send_batch *batch = (struct smbdirect_send_batch *)storage;
memset(storage, 0, sizeof(*storage));
BUILD_BUG_ON(sizeof(*batch) > sizeof(*storage));
smbdirect_connection_send_batch_init(batch,
need_invalidate_rkey,
remote_key);
return batch;
}
__SMBDIRECT_EXPORT_SYMBOL__(smbdirect_init_send_batch_storage);
static int smbdirect_connection_wait_for_send_bcredit(struct smbdirect_socket *sc,
struct smbdirect_send_batch *batch)
{
int ret;
if (batch->credit)
return 0;
ret = smbdirect_socket_wait_for_credits(sc,
SMBDIRECT_SOCKET_CONNECTED,
-ENOTCONN,
&sc->send_io.bcredits.wait_queue,
&sc->send_io.bcredits.count,
1);
if (ret)
return ret;
batch->credit = 1;
return 0;
}
static int smbdirect_connection_wait_for_send_lcredit(struct smbdirect_socket *sc,
struct smbdirect_send_batch *batch)
{
if (batch && atomic_read(&sc->send_io.lcredits.count) <= 1) {
int ret;
ret = smbdirect_connection_send_batch_flush(sc, batch, false);
if (ret)
return ret;
}
return smbdirect_socket_wait_for_credits(sc,
SMBDIRECT_SOCKET_CONNECTED,
-ENOTCONN,
&sc->send_io.lcredits.wait_queue,
&sc->send_io.lcredits.count,
1);
}
static int smbdirect_connection_wait_for_send_credits(struct smbdirect_socket *sc,
struct smbdirect_send_batch *batch)
{
if (batch && (batch->wr_cnt >= 16 || atomic_read(&sc->send_io.credits.count) <= 1)) {
int ret;
ret = smbdirect_connection_send_batch_flush(sc, batch, false);
if (ret)
return ret;
}
return smbdirect_socket_wait_for_credits(sc,
SMBDIRECT_SOCKET_CONNECTED,
-ENOTCONN,
&sc->send_io.credits.wait_queue,
&sc->send_io.credits.count,
1);
}
static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc);
static int smbdirect_connection_post_send_io(struct smbdirect_socket *sc,
struct smbdirect_send_batch *batch,
struct smbdirect_send_io *msg)
{
int i;
for (i = 0; i < msg->num_sge; i++)
ib_dma_sync_single_for_device(sc->ib.dev,
msg->sge[i].addr, msg->sge[i].length,
DMA_TO_DEVICE);
msg->cqe.done = smbdirect_connection_send_io_done;
msg->wr.wr_cqe = &msg->cqe;
msg->wr.opcode = IB_WR_SEND;
msg->wr.sg_list = &msg->sge[0];
msg->wr.num_sge = msg->num_sge;
msg->wr.next = NULL;
if (batch) {
msg->wr.send_flags = 0;
if (!list_empty(&batch->msg_list)) {
struct smbdirect_send_io *last;
last = list_last_entry(&batch->msg_list,
struct smbdirect_send_io,
sibling_list);
last->wr.next = &msg->wr;
}
list_add_tail(&msg->sibling_list, &batch->msg_list);
batch->wr_cnt++;
return 0;
}
msg->wr.send_flags = IB_SEND_SIGNALED;
return smbdirect_connection_post_send_wr(sc, &msg->wr);
}
int smbdirect_connection_send_single_iter(struct smbdirect_socket *sc,
struct smbdirect_send_batch *batch,
struct iov_iter *iter,
unsigned int flags,
u32 remaining_data_length)
{
const struct smbdirect_socket_parameters *sp = &sc->parameters;
struct smbdirect_send_batch _batch;
struct smbdirect_send_io *msg;
struct smbdirect_data_transfer *packet;
size_t header_length;
u16 new_credits = 0;
u32 data_length = 0;
int ret;
if (WARN_ON_ONCE(flags))
return -EINVAL; /* no flags support for now */
if (iter) {
if (WARN_ON_ONCE(iov_iter_rw(iter) != ITER_SOURCE))
return -EINVAL; /* It's a bug in upper layer to get there */
header_length = sizeof(struct smbdirect_data_transfer);
if (WARN_ON_ONCE(remaining_data_length == 0 ||
iov_iter_count(iter) > remaining_data_length))
return -EINVAL;
} else {
/* If this is a packet without payload, don't send padding */
header_length = offsetof(struct smbdirect_data_transfer, padding);
if (WARN_ON_ONCE(remaining_data_length))
return -EINVAL;
}
if (sc->status != SMBDIRECT_SOCKET_CONNECTED) {
smbdirect_log_write(sc, SMBDIRECT_LOG_ERR,
"status=%s first_error=%1pe => %1pe\n",
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
SMBDIRECT_DEBUG_ERR_PTR(-ENOTCONN));
return -ENOTCONN;
}
if (!batch) {
smbdirect_connection_send_batch_init(&_batch, false, 0);
batch = &_batch;
}
ret = smbdirect_connection_wait_for_send_bcredit(sc, batch);
if (ret)
goto bcredit_failed;
ret = smbdirect_connection_wait_for_send_lcredit(sc, batch);
if (ret)
goto lcredit_failed;
ret = smbdirect_connection_wait_for_send_credits(sc, batch);
if (ret)
goto credit_failed;
new_credits = smbdirect_connection_grant_recv_credits(sc);
if (new_credits == 0 &&
atomic_read(&sc->send_io.credits.count) == 0 &&
atomic_read(&sc->recv_io.credits.count) == 0) {
/*
* queue the refill work in order to
* get some new recv credits we can grant to
* the peer.
*/
queue_work(sc->workqueues.refill, &sc->recv_io.posted.refill_work);
/*
* wait until either the refill work or the peer
* granted new credits
*/
ret = wait_event_interruptible(sc->send_io.credits.wait_queue,
atomic_read(&sc->send_io.credits.count) >= 1 ||
atomic_read(&sc->recv_io.credits.available) >= 1 ||
sc->status != SMBDIRECT_SOCKET_CONNECTED);
if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
ret = -ENOTCONN;
if (ret < 0)
goto credit_failed;
new_credits = smbdirect_connection_grant_recv_credits(sc);
}
msg = smbdirect_connection_alloc_send_io(sc);
if (IS_ERR(msg)) {
ret = PTR_ERR(msg);
goto alloc_failed;
}
/* Map the packet to DMA */
msg->sge[0].addr = ib_dma_map_single(sc->ib.dev,
msg->packet,
header_length,
DMA_TO_DEVICE);
ret = ib_dma_mapping_error(sc->ib.dev, msg->sge[0].addr);
if (ret)
goto err;
msg->sge[0].length = header_length;
msg->sge[0].lkey = sc->ib.pd->local_dma_lkey;
msg->num_sge = 1;
if (iter) {
struct smbdirect_map_sges extract = {
.num_sge = msg->num_sge,
.max_sge = ARRAY_SIZE(msg->sge),
.sge = msg->sge,
.device = sc->ib.dev,
.local_dma_lkey = sc->ib.pd->local_dma_lkey,
.direction = DMA_TO_DEVICE,
};
size_t payload_len = umin(iov_iter_count(iter),
sp->max_send_size - sizeof(*packet));
ret = smbdirect_map_sges_from_iter(iter, payload_len, &extract);
if (ret < 0)
goto err;
data_length = ret;
remaining_data_length -= data_length;
msg->num_sge = extract.num_sge;
}
/* Fill in the packet header */
packet = (struct smbdirect_data_transfer *)msg->packet;
packet->credits_requested = cpu_to_le16(sp->send_credit_target);
packet->credits_granted = cpu_to_le16(new_credits);
packet->flags = 0;
if (smbdirect_connection_request_keep_alive(sc))
packet->flags |= cpu_to_le16(SMBDIRECT_FLAG_RESPONSE_REQUESTED);
packet->reserved = 0;
if (!data_length)
packet->data_offset = 0;
else
packet->data_offset = cpu_to_le32(24);
packet->data_length = cpu_to_le32(data_length);
packet->remaining_data_length = cpu_to_le32(remaining_data_length);
packet->padding = 0;
smbdirect_log_outgoing(sc, SMBDIRECT_LOG_INFO,
"DataOut: %s=%u, %s=%u, %s=0x%x, %s=%u, %s=%u, %s=%u\n",
"CreditsRequested",
le16_to_cpu(packet->credits_requested),
"CreditsGranted",
le16_to_cpu(packet->credits_granted),
"Flags",
le16_to_cpu(packet->flags),
"RemainingDataLength",
le32_to_cpu(packet->remaining_data_length),
"DataOffset",
le32_to_cpu(packet->data_offset),
"DataLength",
le32_to_cpu(packet->data_length));
ret = smbdirect_connection_post_send_io(sc, batch, msg);
if (ret)
goto err;
/*
* From here msg is moved to send_ctx
* and we should not free it explicitly.
*/
if (batch == &_batch) {
ret = smbdirect_connection_send_batch_flush(sc, batch, true);
if (ret)
goto flush_failed;
}
return data_length;
err:
smbdirect_connection_free_send_io(msg);
flush_failed:
alloc_failed:
atomic_inc(&sc->send_io.credits.count);
credit_failed:
atomic_inc(&sc->send_io.lcredits.count);
lcredit_failed:
atomic_add(batch->credit, &sc->send_io.bcredits.count);
batch->credit = 0;
bcredit_failed:
return ret;
}
__SMBDIRECT_EXPORT_SYMBOL__(smbdirect_connection_send_single_iter);
int smbdirect_connection_send_wait_zero_pending(struct smbdirect_socket *sc)
{
/*
* As an optimization, we don't wait for individual I/O to finish
* before sending the next one.
* Send them all and wait for pending send count to get to 0
* that means all the I/Os have been out and we are good to return
*/
wait_event(sc->send_io.pending.zero_wait_queue,
atomic_read(&sc->send_io.pending.count) == 0 ||
sc->status != SMBDIRECT_SOCKET_CONNECTED);
if (sc->status != SMBDIRECT_SOCKET_CONNECTED) {
smbdirect_log_write(sc, SMBDIRECT_LOG_ERR,
"status=%s first_error=%1pe => %1pe\n",
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
SMBDIRECT_DEBUG_ERR_PTR(-ENOTCONN));
return -ENOTCONN;
}
return 0;
}
__SMBDIRECT_EXPORT_SYMBOL__(smbdirect_connection_send_wait_zero_pending);
int smbdirect_connection_send_iter(struct smbdirect_socket *sc,
struct iov_iter *iter,
unsigned int flags,
bool need_invalidate,
unsigned int remote_key)
{
const struct smbdirect_socket_parameters *sp = &sc->parameters;
struct smbdirect_send_batch batch;
int total_count = iov_iter_count(iter);
int ret;
int error = 0;
__be32 hdr;
if (WARN_ONCE(flags, "unexpected flags=0x%x\n", flags))
return -EINVAL; /* no flags support for now */
if (WARN_ON_ONCE(iov_iter_rw(iter) != ITER_SOURCE))
return -EINVAL; /* It's a bug in upper layer to get there */
if (sc->status != SMBDIRECT_SOCKET_CONNECTED) {
smbdirect_log_write(sc, SMBDIRECT_LOG_INFO,
"status=%s first_error=%1pe => %1pe\n",
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
SMBDIRECT_DEBUG_ERR_PTR(-ENOTCONN));
return -ENOTCONN;
}
/*
* For now we expect the iter to have the full
* message, including a 4 byte length header.
*/
if (iov_iter_count(iter) <= 4)
return -EINVAL;
if (!copy_from_iter_full(&hdr, sizeof(hdr), iter))
return -EFAULT;
if (iov_iter_count(iter) != be32_to_cpu(hdr))
return -EINVAL;
/*
* The size must fit into the negotiated
* fragmented send size.
*/
if (iov_iter_count(iter) > sp->max_fragmented_send_size)
return -EMSGSIZE;
smbdirect_log_write(sc, SMBDIRECT_LOG_INFO,
"Sending (RDMA): length=%zu\n",
iov_iter_count(iter));
smbdirect_connection_send_batch_init(&batch, need_invalidate, remote_key);
while (iov_iter_count(iter)) {
ret = smbdirect_connection_send_single_iter(sc,
&batch,
iter,
flags,
iov_iter_count(iter));
if (unlikely(ret < 0)) {
error = ret;
break;
}
}
ret = smbdirect_connection_send_batch_flush(sc, &batch, true);
if (unlikely(ret && !error))
error = ret;
/*
* As an optimization, we don't wait for individual I/O to finish
* before sending the next one.
* Send them all and wait for pending send count to get to 0
* that means all the I/Os have been out and we are good to return
*/
ret = smbdirect_connection_send_wait_zero_pending(sc);
if (unlikely(ret && !error))
error = ret;
if (unlikely(error))
return error;
return total_count;
}
__SMBDIRECT_EXPORT_SYMBOL__(smbdirect_connection_send_iter);
static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc)
{
struct smbdirect_send_io *msg =
container_of(wc->wr_cqe, struct smbdirect_send_io, cqe);
struct smbdirect_socket *sc = msg->socket;
struct smbdirect_send_io *sibling, *next;
int lcredits = 0;
smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_INFO,
"smbdirect_send_io completed. status='%s (%d)', opcode=%d\n",
ib_wc_status_msg(wc->status), wc->status, wc->opcode);
if (unlikely(!(msg->wr.send_flags & IB_SEND_SIGNALED))) {
/*
* This happens when smbdirect_send_io is a sibling
* before the final message, it is signaled on
* error anyway, so we need to skip
* smbdirect_connection_free_send_io here,
* otherwise is will destroy the memory
* of the siblings too, which will cause
* use after free problems for the others
* triggered from ib_drain_qp().
*/
if (wc->status != IB_WC_SUCCESS)
goto skip_free;
/*
* This should not happen!
* But we better just close the
* connection...
*/
smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR,
"unexpected send completion wc->status=%s (%d) wc->opcode=%d\n",
ib_wc_status_msg(wc->status), wc->status, wc->opcode);
smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED);
return;
}
/*
* Free possible siblings and then the main send_io
*/
list_for_each_entry_safe(sibling, next, &msg->sibling_list, sibling_list) {
list_del_init(&sibling->sibling_list);
smbdirect_connection_free_send_io(sibling);
lcredits += 1;
}
/* Note this frees wc->wr_cqe, but not wc */
smbdirect_connection_free_send_io(msg);
lcredits += 1;
if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_SEND))) {
skip_free:
if (wc->status != IB_WC_WR_FLUSH_ERR)
smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR,
"wc->status=%s (%d) wc->opcode=%d\n",
ib_wc_status_msg(wc->status), wc->status, wc->opcode);
smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED);
return;
}
atomic_add(lcredits, &sc->send_io.lcredits.count);
wake_up(&sc->send_io.lcredits.wait_queue);
if (atomic_dec_and_test(&sc->send_io.pending.count))
wake_up(&sc->send_io.pending.zero_wait_queue);
}
static void smbdirect_connection_send_immediate_work(struct work_struct *work)
{
struct smbdirect_socket *sc =
container_of(work, struct smbdirect_socket, idle.immediate_work);
int ret;
if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
return;
smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_INFO,
"send an empty message\n");
sc->statistics.send_empty++;
ret = smbdirect_connection_send_single_iter(sc, NULL, NULL, 0, 0);
if (ret < 0) {
smbdirect_log_write(sc, SMBDIRECT_LOG_ERR,
"smbdirect_connection_send_single_iter ret=%1pe\n",
SMBDIRECT_DEBUG_ERR_PTR(ret));
smbdirect_socket_schedule_cleanup(sc, ret);
}
}
int smbdirect_connection_post_recv_io(struct smbdirect_recv_io *msg)
{
struct smbdirect_socket *sc = msg->socket;
const struct smbdirect_socket_parameters *sp = &sc->parameters;
struct ib_recv_wr recv_wr = {
.wr_cqe = &msg->cqe,
.sg_list = &msg->sge,
.num_sge = 1,
};
int ret;
if (unlikely(sc->first_error))
return sc->first_error;
msg->sge.addr = ib_dma_map_single(sc->ib.dev,
msg->packet,
sp->max_recv_size,
DMA_FROM_DEVICE);
ret = ib_dma_mapping_error(sc->ib.dev, msg->sge.addr);
if (ret)
return ret;
msg->sge.length = sp->max_recv_size;
msg->sge.lkey = sc->ib.pd->local_dma_lkey;
ret = ib_post_recv(sc->ib.qp, &recv_wr, NULL);
if (ret) {
smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_ERR,
"ib_post_recv failed ret=%d (%1pe)\n",
ret, SMBDIRECT_DEBUG_ERR_PTR(ret));
ib_dma_unmap_single(sc->ib.dev,
msg->sge.addr,
msg->sge.length,
DMA_FROM_DEVICE);
msg->sge.length = 0;
smbdirect_socket_schedule_cleanup(sc, ret);
}
return ret;
}
void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc)
{
struct smbdirect_recv_io *recv_io =
container_of(wc->wr_cqe, struct smbdirect_recv_io, cqe);
struct smbdirect_socket *sc = recv_io->socket;
const struct smbdirect_socket_parameters *sp = &sc->parameters;
struct smbdirect_data_transfer *data_transfer;
int current_recv_credits;
u16 old_recv_credit_target;
u16 credits_requested;
u16 credits_granted;
u16 flags;
u32 data_offset;
u32 data_length;
u32 remaining_data_length;
if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_RECV))) {
if (wc->status != IB_WC_WR_FLUSH_ERR)
smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_ERR,
"wc->status=%s (%d) wc->opcode=%d\n",
ib_wc_status_msg(wc->status), wc->status, wc->opcode);
goto error;
}
smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_INFO,
"recv_io=0x%p type=%d wc status=%s wc opcode %d byte_len=%d pkey_index=%u\n",
recv_io, sc->recv_io.expected,
ib_wc_status_msg(wc->status), wc->opcode,
wc->byte_len, wc->pkey_index);
/*
* Reset timer to the keepalive interval in
* order to trigger our next keepalive message.
*/
sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE;
mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work,
msecs_to_jiffies(sp->keepalive_interval_msec));
ib_dma_sync_single_for_cpu(sc->ib.dev,
recv_io->sge.addr,
recv_io->sge.length,
DMA_FROM_DEVICE);
if (unlikely(wc->byte_len <
offsetof(struct smbdirect_data_transfer, padding))) {
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
"wc->byte_len=%u < %zu\n",
wc->byte_len,
offsetof(struct smbdirect_data_transfer, padding));
goto error;
}
data_transfer = (struct smbdirect_data_transfer *)recv_io->packet;
credits_requested = le16_to_cpu(data_transfer->credits_requested);
credits_granted = le16_to_cpu(data_transfer->credits_granted);
flags = le16_to_cpu(data_transfer->flags);
remaining_data_length = le32_to_cpu(data_transfer->remaining_data_length);
data_offset = le32_to_cpu(data_transfer->data_offset);
data_length = le32_to_cpu(data_transfer->data_length);
smbdirect_log_incoming(sc, SMBDIRECT_LOG_INFO,
"DataIn: %s=%u, %s=%u, %s=0x%x, %s=%u, %s=%u, %s=%u\n",
"CreditsRequested",
credits_requested,
"CreditsGranted",
credits_granted,
"Flags",
flags,
"RemainingDataLength",
remaining_data_length,
"DataOffset",
data_offset,
"DataLength",
data_length);
if (unlikely(credits_requested == 0)) {
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
"invalid: credits_requested == 0\n");
goto error;
}
if (unlikely(data_offset % 8 != 0)) {
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
"invalid: data_offset=%u (0x%x) not aligned to 8\n",
data_offset, data_offset);
goto error;
}
if (unlikely(wc->byte_len < data_offset ||
(u64)wc->byte_len < (u64)data_offset + data_length)) {
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
"wc->byte_len=%u < date_offset=%u + data_length=%u\n",
wc->byte_len, data_offset, data_length);
goto error;
}
if (unlikely(remaining_data_length > sp->max_fragmented_recv_size ||
data_length > sp->max_fragmented_recv_size ||
(u64)remaining_data_length + (u64)data_length > (u64)sp->max_fragmented_recv_size)) {
smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
"remaining_data_length=%u + data_length=%u > max_fragmented=%u\n",
remaining_data_length, data_length, sp->max_fragmented_recv_size);
goto error;
}
if (data_length) {
if (sc->recv_io.reassembly.full_packet_received)
recv_io->first_segment = true;
if (remaining_data_length)
sc->recv_io.reassembly.full_packet_received = false;
else
sc->recv_io.reassembly.full_packet_received = true;
}
atomic_dec(&sc->recv_io.posted.count);
current_recv_credits = atomic_dec_return(&sc->recv_io.credits.count);
/*
* We take the value from the peer, which is checked to be higher than 0,
* but we limit it to the max value we support in order to have
* the main logic simpler.
*/
old_recv_credit_target = sc->recv_io.credits.target;
sc->recv_io.credits.target = credits_requested;
sc->recv_io.credits.target = min_t(u16, sc->recv_io.credits.target,
sp->recv_credit_max);
if (credits_granted) {
atomic_add(credits_granted, &sc->send_io.credits.count);
/*
* We have new send credits granted from remote peer
* If any sender is waiting for credits, unblock it
*/
wake_up(&sc->send_io.credits.wait_queue);
}
/* Send an immediate response right away if requested */
if (flags & SMBDIRECT_FLAG_RESPONSE_REQUESTED) {
smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_INFO,
"schedule send of immediate response\n");
queue_work(sc->workqueues.immediate, &sc->idle.immediate_work);
}
/*
* If this is a packet with data playload place the data in
* reassembly queue and wake up the reading thread
*/
if (data_length) {
if (current_recv_credits <= (sc->recv_io.credits.target / 4) ||
sc->recv_io.credits.target > old_recv_credit_target)
queue_work(sc->workqueues.refill, &sc->recv_io.posted.refill_work);
smbdirect_connection_reassembly_append_recv_io(sc, recv_io, data_length);
wake_up(&sc->recv_io.reassembly.wait_queue);
} else
smbdirect_connection_put_recv_io(recv_io);
return;
error:
/*
* Make sure smbdirect_connection_put_recv_io() does not
* start recv_io.posted.refill_work.
*/
disable_work(&sc->recv_io.posted.refill_work);
smbdirect_connection_put_recv_io(recv_io);
smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED);
}
int smbdirect_connection_recv_io_refill(struct smbdirect_socket *sc)
{
int missing;
int posted = 0;
if (unlikely(sc->first_error))
return sc->first_error;
/*
* Find out how much smbdirect_recv_io buffers we should post.
*
* Note that sc->recv_io.credits.target is the value
* from the peer and it can in theory change over time,
* but it is forced to be at least 1 and at max
* sp->recv_credit_max.
*
* So it can happen that missing will be lower than 0,
* which means the peer has recently lowered its desired
* target, while be already granted a higher number of credits.
*
* Note 'posted' is the number of smbdirect_recv_io buffers
* posted within this function, while sc->recv_io.posted.count
* is the overall value of posted smbdirect_recv_io buffers.
*
* We try to post as much buffers as missing, but
* this is limited if a lot of smbdirect_recv_io buffers
* are still in the sc->recv_io.reassembly.list instead of
* the sc->recv_io.free.list.
*
*/
missing = (int)sc->recv_io.credits.target - atomic_read(&sc->recv_io.posted.count);
while (posted < missing) {
struct smbdirect_recv_io *recv_io;
int ret;
/*
* It's ok if smbdirect_connection_get_recv_io()
* returns NULL, it means smbdirect_recv_io structures
* are still be in the reassembly.list.
*/
recv_io = smbdirect_connection_get_recv_io(sc);
if (!recv_io)
break;
recv_io->first_segment = false;
ret = smbdirect_connection_post_recv_io(recv_io);
if (ret) {
smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_ERR,
"smbdirect_connection_post_recv_io failed rc=%d (%1pe)\n",
ret, SMBDIRECT_DEBUG_ERR_PTR(ret));
smbdirect_connection_put_recv_io(recv_io);
return ret;
}
atomic_inc(&sc->recv_io.posted.count);
posted += 1;
}
/* If nothing was posted we're done */
if (posted == 0)
return 0;
atomic_add(posted, &sc->recv_io.credits.available);
/*
* If the last send credit is waiting for credits
* it can grant we need to wake it up
*/
if (atomic_read(&sc->send_io.bcredits.count) == 0 &&
atomic_read(&sc->send_io.credits.count) == 0)
wake_up(&sc->send_io.credits.wait_queue);
/*
* If we posted at least one smbdirect_recv_io buffer,
* we need to inform the peer about it and grant
* additional credits.
*
* However there is one case where we don't want to
* do that.
*
* If only a single credit was missing before
* reaching the requested target, we should not
* post an immediate send, as that would cause
* endless ping pong once a keep alive exchange
* is started.
*
* However if sc->recv_io.credits.target is only 1,
* the peer has no credit left and we need to
* grant the credit anyway.
*/
if (missing == 1 && sc->recv_io.credits.target != 1)
return 0;
return posted;
}
static void smbdirect_connection_recv_io_refill_work(struct work_struct *work)
{
struct smbdirect_socket *sc =
container_of(work, struct smbdirect_socket, recv_io.posted.refill_work);
int posted;
posted = smbdirect_connection_recv_io_refill(sc);
if (unlikely(posted < 0)) {
smbdirect_socket_schedule_cleanup(sc, posted);
return;
}
if (posted > 0) {
smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_INFO,
"schedule send of an empty message\n");
queue_work(sc->workqueues.immediate, &sc->idle.immediate_work);
}
}
int smbdirect_connection_recvmsg(struct smbdirect_socket *sc,
struct msghdr *msg,
unsigned int flags)
{
struct smbdirect_recv_io *response;
struct smbdirect_data_transfer *data_transfer;
size_t size = iov_iter_count(&msg->msg_iter);
int to_copy, to_read, data_read, offset;
u32 data_length, remaining_data_length, data_offset;
int ret;
if (WARN_ONCE(flags, "unexpected flags=0x%x\n", flags))
return -EINVAL; /* no flags support for now */
if (WARN_ON_ONCE(iov_iter_rw(&msg->msg_iter) != ITER_DEST))
return -EINVAL; /* It's a bug in upper layer to get there */
again:
if (sc->status != SMBDIRECT_SOCKET_CONNECTED) {
smbdirect_log_read(sc, SMBDIRECT_LOG_INFO,
"status=%s first_error=%1pe => %1pe\n",
smbdirect_socket_status_string(sc->status),
SMBDIRECT_DEBUG_ERR_PTR(sc->first_error),
SMBDIRECT_DEBUG_ERR_PTR(-ENOTCONN));
return -ENOTCONN;
}
/*
* No need to hold the reassembly queue lock all the time as we are
* the only one reading from the front of the queue. The transport
* may add more entries to the back of the queue at the same time
*/
smbdirect_log_read(sc, SMBDIRECT_LOG_INFO,
"size=%zd sc->recv_io.reassembly.data_length=%d\n",
size, sc->recv_io.reassembly.data_length);
if (sc->recv_io.reassembly.data_length >= size) {
int queue_length;
int queue_removed = 0;
unsigned long flags;
/*
* Need to make sure reassembly_data_length is read before
* reading reassembly_queue_length and calling
* smbdirect_connection_reassembly_first_recv_io. This call is lock free
* as we never read at the end of the queue which are being
* updated in SOFTIRQ as more data is received
*/
virt_rmb();
queue_length = sc->recv_io.reassembly.queue_length;
data_read = 0;
to_read = size;
offset = sc->recv_io.reassembly.first_entry_offset;
while (data_read < size) {
response = smbdirect_connection_reassembly_first_recv_io(sc);
data_transfer = (void *)response->packet;
data_length = le32_to_cpu(data_transfer->data_length);
remaining_data_length =
le32_to_cpu(
data_transfer->remaining_data_length);
data_offset = le32_to_cpu(data_transfer->data_offset);
/*
* The upper layer expects RFC1002 length at the
* beginning of the payload. Return it to indicate
* the total length of the packet. This minimize the
* change to upper layer packet processing logic. This
* will be eventually remove when an intermediate
* transport layer is added
*/
if (response->first_segment && size == 4) {
unsigned int rfc1002_len =
data_length + remaining_data_length;
__be32 rfc1002_hdr = cpu_to_be32(rfc1002_len);
if (copy_to_iter(&rfc1002_hdr, sizeof(rfc1002_hdr),
&msg->msg_iter) != sizeof(rfc1002_hdr))
return -EFAULT;
data_read = 4;
response->first_segment = false;
smbdirect_log_read(sc, SMBDIRECT_LOG_INFO,
"returning rfc1002 length %d\n",
rfc1002_len);
goto read_rfc1002_done;
}
to_copy = min_t(int, data_length - offset, to_read);
if (copy_to_iter((u8 *)data_transfer + data_offset + offset,
to_copy, &msg->msg_iter) != to_copy)
return -EFAULT;
/* move on to the next buffer? */
if (to_copy == data_length - offset) {
queue_length--;
/*
* No need to lock if we are not at the
* end of the queue
*/
if (queue_length)
list_del(&response->list);
else {
spin_lock_irqsave(
&sc->recv_io.reassembly.lock, flags);
list_del(&response->list);
spin_unlock_irqrestore(
&sc->recv_io.reassembly.lock, flags);
}
queue_removed++;
sc->statistics.dequeue_reassembly_queue++;
smbdirect_connection_put_recv_io(response);
offset = 0;
smbdirect_log_read(sc, SMBDIRECT_LOG_INFO,
"smbdirect_connection_put_recv_io offset=0\n");
} else
offset += to_copy;
to_read -= to_copy;
data_read += to_copy;
smbdirect_log_read(sc, SMBDIRECT_LOG_INFO,
"memcpy %d bytes len-ofs=%u => todo=%u done=%u ofs=%u\n",
to_copy, data_length - offset,
to_read, data_read, offset);
}
spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags);
sc->recv_io.reassembly.data_length -= data_read;
sc->recv_io.reassembly.queue_length -= queue_removed;
spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags);
sc->recv_io.reassembly.first_entry_offset = offset;
smbdirect_log_read(sc, SMBDIRECT_LOG_INFO,
"returning data_read=%d reassembly_length=%d first_ofs=%u\n",
data_read, sc->recv_io.reassembly.data_length,
sc->recv_io.reassembly.first_entry_offset);
read_rfc1002_done:
return data_read;
}
smbdirect_log_read(sc, SMBDIRECT_LOG_INFO,
"wait_event on more data\n");
ret = wait_event_interruptible(sc->recv_io.reassembly.wait_queue,
sc->recv_io.reassembly.data_length >= size ||
sc->status != SMBDIRECT_SOCKET_CONNECTED);
/* Don't return any data if interrupted */
if (ret)
return ret;
goto again;
}
__SMBDIRECT_EXPORT_SYMBOL__(smbdirect_connection_recvmsg);
static bool smbdirect_map_sges_single_page(struct smbdirect_map_sges *state,
struct page *page, size_t off, size_t len)
{
struct ib_sge *sge;
u64 addr;
if (state->num_sge >= state->max_sge)
return false;
addr = ib_dma_map_page(state->device, page,
off, len, state->direction);
if (ib_dma_mapping_error(state->device, addr))
return false;
sge = &state->sge[state->num_sge++];
sge->addr = addr;
sge->length = len;
sge->lkey = state->local_dma_lkey;
return true;
}
/*
* Extract page fragments from a BVEC-class iterator and add them to an ib_sge
* list. The pages are not pinned.
*/
static ssize_t smbdirect_map_sges_from_bvec(struct iov_iter *iter,
struct smbdirect_map_sges *state,
ssize_t maxsize)
{
const struct bio_vec *bv = iter->bvec;
unsigned long start = iter->iov_offset;
unsigned int i;
ssize_t ret = 0;
for (i = 0; i < iter->nr_segs; i++) {
size_t off, len;
bool ok;
len = bv[i].bv_len;
if (start >= len) {
start -= len;
continue;
}
len = min_t(size_t, maxsize, len - start);
off = bv[i].bv_offset + start;
ok = smbdirect_map_sges_single_page(state,
bv[i].bv_page,
off,
len);
if (!ok)
return -EIO;
ret += len;
maxsize -= len;
if (state->num_sge >= state->max_sge || maxsize <= 0)
break;
start = 0;
}
if (ret > 0)
iov_iter_advance(iter, ret);
return ret;
}
/*
* Extract fragments from a KVEC-class iterator and add them to an ib_sge list.
* This can deal with vmalloc'd buffers as well as kmalloc'd or static buffers.
* The pages are not pinned.
*/
static ssize_t smbdirect_map_sges_from_kvec(struct iov_iter *iter,
struct smbdirect_map_sges *state,
ssize_t maxsize)
{
const struct kvec *kv = iter->kvec;
unsigned long start = iter->iov_offset;
unsigned int i;
ssize_t ret = 0;
for (i = 0; i < iter->nr_segs; i++) {
struct page *page;
unsigned long kaddr;
size_t off, len, seg;
len = kv[i].iov_len;
if (start >= len) {
start -= len;
continue;
}
kaddr = (unsigned long)kv[i].iov_base + start;
off = kaddr & ~PAGE_MASK;
len = min_t(size_t, maxsize, len - start);
kaddr &= PAGE_MASK;
maxsize -= len;
do {
bool ok;
seg = min_t(size_t, len, PAGE_SIZE - off);
if (is_vmalloc_or_module_addr((void *)kaddr))
page = vmalloc_to_page((void *)kaddr);
else
page = virt_to_page((void *)kaddr);
ok = smbdirect_map_sges_single_page(state, page, off, seg);
if (!ok)
return -EIO;
ret += seg;
len -= seg;
kaddr += PAGE_SIZE;
off = 0;
} while (len > 0 && state->num_sge < state->max_sge);
if (state->num_sge >= state->max_sge || maxsize <= 0)
break;
start = 0;
}
if (ret > 0)
iov_iter_advance(iter, ret);
return ret;
}
/*
* Extract folio fragments from a FOLIOQ-class iterator and add them to an
* ib_sge list. The folios are not pinned.
*/
static ssize_t smbdirect_map_sges_from_folioq(struct iov_iter *iter,
struct smbdirect_map_sges *state,
ssize_t maxsize)
{
const struct folio_queue *folioq = iter->folioq;
unsigned int slot = iter->folioq_slot;
ssize_t ret = 0;
size_t offset = iter->iov_offset;
if (WARN_ON_ONCE(!folioq))
return -EIO;
if (slot >= folioq_nr_slots(folioq)) {
folioq = folioq->next;
if (WARN_ON_ONCE(!folioq))
return -EIO;
slot = 0;
}
do {
struct folio *folio = folioq_folio(folioq, slot);
size_t fsize = folioq_folio_size(folioq, slot);
if (offset < fsize) {
size_t part = umin(maxsize, fsize - offset);
bool ok;
ok = smbdirect_map_sges_single_page(state,
folio_page(folio, 0),
offset,
part);
if (!ok)
return -EIO;
offset += part;
ret += part;
maxsize -= part;
}
if (offset >= fsize) {
offset = 0;
slot++;
if (slot >= folioq_nr_slots(folioq)) {
if (!folioq->next) {
WARN_ON_ONCE(ret < iter->count);
break;
}
folioq = folioq->next;
slot = 0;
}
}
} while (state->num_sge < state->max_sge && maxsize > 0);
iter->folioq = folioq;
iter->folioq_slot = slot;
iter->iov_offset = offset;
iter->count -= ret;
return ret;
}
/*
* Extract page fragments from up to the given amount of the source iterator
* and build up an ib_sge list that refers to all of those bits. The ib_sge list
* is appended to, up to the maximum number of elements set in the parameter
* block.
*
* The extracted page fragments are not pinned or ref'd in any way; if an
* IOVEC/UBUF-type iterator is to be used, it should be converted to a
* BVEC-type iterator and the pages pinned, ref'd or otherwise held in some
* way.
*/
static ssize_t smbdirect_map_sges_from_iter(struct iov_iter *iter, size_t len,
struct smbdirect_map_sges *state)
{
ssize_t ret;
size_t before = state->num_sge;
if (WARN_ON_ONCE(iov_iter_rw(iter) != ITER_SOURCE))
return -EIO;
switch (iov_iter_type(iter)) {
case ITER_BVEC:
ret = smbdirect_map_sges_from_bvec(iter, state, len);
break;
case ITER_KVEC:
ret = smbdirect_map_sges_from_kvec(iter, state, len);
break;
case ITER_FOLIOQ:
ret = smbdirect_map_sges_from_folioq(iter, state, len);
break;
default:
WARN_ONCE(1, "iov_iter_type[%u]\n", iov_iter_type(iter));
return -EIO;
}
if (ret < 0) {
while (state->num_sge > before) {
struct ib_sge *sge = &state->sge[state->num_sge--];
ib_dma_unmap_page(state->device,
sge->addr,
sge->length,
state->direction);
}
}
return ret;
}
|