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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/machsystm.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/kmem.h>
#include <sys/strsun.h>
#include <sys/callb.h>
#include <sys/sdt.h>
#include <sys/ethernet.h>
#include <sys/mach_descrip.h>
#include <sys/mdeg.h>
#include <sys/vnet.h>
#include <sys/vio_mailbox.h>
#include <sys/vio_common.h>
#include <sys/vnet_common.h>
#include <sys/vnet_mailbox.h>
#include <sys/vio_util.h>
#include <sys/vnet_gen.h>
/*
* This file contains the implementation of RxDringData transfer mode of VIO
* Protocol in vnet. The functions in this file are invoked from vnet_gen.c
* after RxDringData mode is negotiated with the peer during attribute phase of
* handshake. This file contains functions that setup the transmit and receive
* descriptor rings, and associated resources in RxDringData mode. It also
* contains the transmit and receive data processing functions that are invoked
* in RxDringData mode. The data processing routines in this file have the
* suffix '_shm' to indicate the shared memory mechanism used in RxDringData
* mode.
*/
/* Functions exported to vnet_gen.c */
int vgen_create_rx_dring(vgen_ldc_t *ldcp);
void vgen_destroy_rx_dring(vgen_ldc_t *ldcp);
int vgen_map_tx_dring(vgen_ldc_t *ldcp, void *pkt);
void vgen_unmap_tx_dring(vgen_ldc_t *ldcp);
int vgen_map_data(vgen_ldc_t *ldcp, void *pkt);
int vgen_dringsend_shm(void *arg, mblk_t *mp);
int vgen_handle_dringdata_shm(void *arg1, void *arg2);
mblk_t *vgen_poll_rcv_shm(vgen_ldc_t *ldcp, int bytes_to_pickup);
int vgen_send_dringack_shm(vgen_ldc_t *ldcp, vio_msg_tag_t *tagp,
uint32_t start, int32_t end, uint8_t pstate);
/* Internal functions */
static int vgen_handle_dringdata_info_shm(vgen_ldc_t *ldcp, vio_msg_tag_t *tp);
static int vgen_handle_dringdata_ack_shm(vgen_ldc_t *ldcp, vio_msg_tag_t *tagp);
static int vgen_handle_dringdata_nack_shm(vgen_ldc_t *ldcp, vio_msg_tag_t *tp);
static int vgen_intr_rcv_shm(vgen_ldc_t *ldcp);
static int vgen_receive_packet(vgen_ldc_t *ldcp, mblk_t **bp, uint_t *size);
static int vgen_send_dringdata_shm(vgen_ldc_t *ldcp, uint32_t start,
int32_t end);
static int vgen_sendmsg_shm(vgen_ldc_t *ldcp, caddr_t msg, size_t msglen);
/* Functions imported from vnet_gen.c */
extern int vgen_handle_evt_read(vgen_ldc_t *ldcp, vgen_caller_t caller);
extern int vgen_handle_evt_reset(vgen_ldc_t *ldcp, vgen_caller_t caller);
extern void vgen_handle_pkt_data(void *arg1, void *arg2, uint32_t msglen);
extern void vgen_destroy_rxpools(void *arg);
/* Tunables */
extern uint32_t vnet_num_descriptors;
extern uint32_t vgen_chain_len;
extern uint32_t vgen_ldcwr_retries;
extern uint32_t vgen_recv_delay;
extern uint32_t vgen_recv_retries;
extern uint32_t vgen_nrbufs_factor;
#ifdef DEBUG
#define DEBUG_PRINTF vgen_debug_printf
extern int vnet_dbglevel;
extern int vgen_inject_err_flag;
extern void vgen_debug_printf(const char *fname, vgen_t *vgenp,
vgen_ldc_t *ldcp, const char *fmt, ...);
extern boolean_t vgen_inject_error(vgen_ldc_t *ldcp, int error);
#endif
/*
* Allocate receive resources for the channel. The resources consist of a
* receive descriptor ring and an associated receive buffer area.
*/
int
vgen_create_rx_dring(vgen_ldc_t *ldcp)
{
int i, j;
int rv;
uint32_t ncookies;
ldc_mem_info_t minfo;
vnet_rx_dringdata_desc_t *rxdp;
size_t data_sz;
vio_mblk_t *vmp;
vio_mblk_t **rxdp_to_vmp;
uint32_t rxdsize;
caddr_t datap = NULL;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
rxdsize = sizeof (vnet_rx_dringdata_desc_t);
ldcp->num_rxds = vnet_num_descriptors;
ldcp->num_rbufs = VGEN_RXDRING_NRBUFS;
/* Create the receive descriptor ring */
rv = ldc_mem_dring_create(ldcp->num_rxds, rxdsize,
&ldcp->rx_dring_handle);
if (rv != 0) {
DWARN(vgenp, ldcp, "ldc_mem_dring_create() failed\n");
goto fail;
}
/* Get the addr of descriptor ring */
rv = ldc_mem_dring_info(ldcp->rx_dring_handle, &minfo);
if (rv != 0) {
DWARN(vgenp, ldcp, "ldc_mem_dring_info() failed\n");
goto fail;
}
ldcp->rxdp = (vnet_rx_dringdata_desc_t *)(minfo.vaddr);
bzero(ldcp->rxdp, sizeof (*rxdp) * (ldcp->num_rxds));
/*
* Allocate a table that maps descriptor to its associated buffer;
* used while receiving to validate that the peer has not changed the
* buffer offset provided in the descriptor.
*/
rxdp_to_vmp = kmem_zalloc(ldcp->num_rxds * sizeof (uintptr_t),
KM_SLEEP);
ldcp->rxdp_to_vmp = rxdp_to_vmp;
/*
* Allocate a single large buffer that serves as the rx buffer area.
* We allocate a ldc memory handle and export the buffer area as shared
* memory. We send the ldc memcookie for this buffer space to the peer,
* as part of dring registration phase during handshake. We manage this
* buffer area as individual buffers of max_frame_size and provide
* specific buffer offsets in each descriptor to the peer. Note that
* the factor used to compute the # of buffers (above) must be > 1 to
* ensure that there are more buffers than the # of descriptors. This
* is needed because, while the shared memory buffers are sent up our
* stack during receive, the sender needs additional buffers that can
* be used for further transmits. This also means there is no one to
* one correspondence between the descriptor index and buffer offset.
* The sender has to read the buffer offset in the descriptor and use
* the specified offset to copy the tx data into the shared buffer. We
* (receiver) manage the individual buffers and their state (see
* VIO_MBLK_STATEs in vio_util.h).
*/
data_sz = RXDRING_DBLK_SZ(vgenp->max_frame_size);
ldcp->rx_data_sz = data_sz * ldcp->num_rbufs;
ldcp->rx_dblk_sz = data_sz;
datap = kmem_zalloc(ldcp->rx_data_sz, KM_SLEEP);
ldcp->rx_datap = datap;
/* Allocate a ldc memhandle for the entire rx data area */
rv = ldc_mem_alloc_handle(ldcp->ldc_handle, &ldcp->rx_data_handle);
if (rv) {
ldcp->rx_data_handle = 0;
goto fail;
}
/* Allocate memory for the data cookies */
ldcp->rx_data_cookie = kmem_zalloc(VNET_DATA_AREA_COOKIES *
sizeof (ldc_mem_cookie_t), KM_SLEEP);
/*
* Bind ldc memhandle to the corresponding rx data area.
*/
ncookies = 0;
rv = ldc_mem_bind_handle(ldcp->rx_data_handle, (caddr_t)datap,
ldcp->rx_data_sz, LDC_DIRECT_MAP, LDC_MEM_W,
ldcp->rx_data_cookie, &ncookies);
if (rv != 0) {
goto fail;
}
if ((ncookies == 0) || (ncookies > VNET_DATA_AREA_COOKIES)) {
goto fail;
}
ldcp->rx_data_ncookies = ncookies;
for (j = 1; j < ncookies; j++) {
rv = ldc_mem_nextcookie(ldcp->rx_data_handle,
&(ldcp->rx_data_cookie[j]));
if (rv != 0) {
DERR(vgenp, ldcp, "ldc_mem_nextcookie "
"failed rv (%d)", rv);
goto fail;
}
}
/*
* Successful in binding the handle to rx data area. Now setup mblks
* around each data buffer and setup the descriptors to point to these
* rx data buffers. We associate each descriptor with a buffer
* by specifying the buffer offset in the descriptor. When the peer
* needs to transmit data, this offset is read by the peer to determine
* the buffer in the mapped buffer area where the data to be
* transmitted should be copied, for a specific descriptor.
*/
rv = vio_create_mblks(ldcp->num_rbufs, data_sz, (uint8_t *)datap,
&ldcp->rx_vmp);
if (rv != 0) {
goto fail;
}
for (i = 0; i < ldcp->num_rxds; i++) {
rxdp = &(ldcp->rxdp[i]);
/* allocate an mblk around this data buffer */
vmp = vio_allocb(ldcp->rx_vmp);
ASSERT(vmp != NULL);
rxdp->data_buf_offset = VIO_MBLK_DATA_OFF(vmp) + VNET_IPALIGN;
rxdp->dstate = VIO_DESC_FREE;
rxdp_to_vmp[i] = vmp;
}
/*
* The descriptors and the associated buffers are all ready;
* now bind descriptor ring to the channel.
*/
rv = ldc_mem_dring_bind(ldcp->ldc_handle, ldcp->rx_dring_handle,
LDC_DIRECT_MAP | LDC_SHADOW_MAP, LDC_MEM_RW,
&ldcp->rx_dring_cookie, &ncookies);
if (rv != 0) {
DWARN(vgenp, ldcp, "ldc_mem_dring_bind failed "
"rv(%x)\n", rv);
goto fail;
}
ASSERT(ncookies == 1);
ldcp->rx_dring_ncookies = ncookies;
/* initialize rx seqnum and index */
ldcp->next_rxseq = VNET_ISS;
ldcp->next_rxi = 0;
return (VGEN_SUCCESS);
fail:
vgen_destroy_rx_dring(ldcp);
return (VGEN_FAILURE);
}
/*
* Free receive resources for the channel.
*/
void
vgen_destroy_rx_dring(vgen_ldc_t *ldcp)
{
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
/* We first unbind the descriptor ring */
if (ldcp->rx_dring_ncookies != 0) {
(void) ldc_mem_dring_unbind(ldcp->rx_dring_handle);
ldcp->rx_dring_ncookies = 0;
}
/* Destroy the mblks that are wrapped around the rx data buffers */
if (ldcp->rx_vmp != NULL) {
vio_clobber_pool(ldcp->rx_vmp);
if (vio_destroy_mblks(ldcp->rx_vmp) != 0) {
/*
* If we can't destroy the rx pool for this channel,
* dispatch a task to retry and clean up. Note that we
* don't need to wait for the task to complete. If the
* vnet device itself gets detached, it will wait for
* the task to complete implicitly in
* ddi_taskq_destroy().
*/
(void) ddi_taskq_dispatch(vgenp->rxp_taskq,
vgen_destroy_rxpools, ldcp->rx_vmp, DDI_SLEEP);
}
ldcp->rx_vmp = NULL;
}
/* Free rx data area cookies */
if (ldcp->rx_data_cookie != NULL) {
kmem_free(ldcp->rx_data_cookie, VNET_DATA_AREA_COOKIES *
sizeof (ldc_mem_cookie_t));
ldcp->rx_data_cookie = NULL;
}
/* Unbind rx data area memhandle */
if (ldcp->rx_data_ncookies != 0) {
(void) ldc_mem_unbind_handle(ldcp->rx_data_handle);
ldcp->rx_data_ncookies = 0;
}
/* Free rx data area memhandle */
if (ldcp->rx_data_handle != 0) {
(void) ldc_mem_free_handle(ldcp->rx_data_handle);
ldcp->rx_data_handle = 0;
}
/* Now free the rx data area itself */
if (ldcp->rx_datap != NULL) {
/* prealloc'd rx data buffer */
kmem_free(ldcp->rx_datap, ldcp->rx_data_sz);
ldcp->rx_datap = NULL;
ldcp->rx_data_sz = 0;
}
/* Finally, free the receive descriptor ring */
if (ldcp->rx_dring_handle != 0) {
(void) ldc_mem_dring_destroy(ldcp->rx_dring_handle);
ldcp->rx_dring_handle = 0;
ldcp->rxdp = NULL;
}
if (ldcp->rxdp_to_vmp != NULL) {
kmem_free(ldcp->rxdp_to_vmp,
ldcp->num_rxds * sizeof (uintptr_t));
ldcp->rxdp_to_vmp = NULL;
}
/* Reset rx index and seqnum */
ldcp->next_rxi = 0;
ldcp->next_rxseq = VNET_ISS;
}
/*
* Map the receive descriptor ring exported
* by the peer, as our transmit descriptor ring.
*/
int
vgen_map_tx_dring(vgen_ldc_t *ldcp, void *pkt)
{
int i;
int rv;
ldc_mem_info_t minfo;
ldc_mem_cookie_t dcookie;
uint32_t ncookies;
uint32_t num_desc;
uint32_t desc_size;
vnet_rx_dringdata_desc_t *txdp;
on_trap_data_t otd;
vio_dring_reg_msg_t *msg = pkt;
ncookies = msg->ncookies;
num_desc = msg->num_descriptors;
desc_size = msg->descriptor_size;
/*
* Sanity check.
*/
if (num_desc < VGEN_NUM_DESCRIPTORS_MIN ||
desc_size < sizeof (vnet_rx_dringdata_desc_t) ||
ncookies > 1) {
goto fail;
}
bcopy(&msg->cookie[0], &dcookie, sizeof (ldc_mem_cookie_t));
/* Map the remote dring */
rv = ldc_mem_dring_map(ldcp->ldc_handle, &dcookie, ncookies, num_desc,
desc_size, LDC_DIRECT_MAP, &(ldcp->tx_dring_handle));
if (rv != 0) {
goto fail;
}
/*
* Sucessfully mapped; now try to get info about the mapped dring
*/
rv = ldc_mem_dring_info(ldcp->tx_dring_handle, &minfo);
if (rv != 0) {
goto fail;
}
/*
* Save ring address, number of descriptors.
*/
ldcp->mtxdp = (vnet_rx_dringdata_desc_t *)(minfo.vaddr);
bcopy(&dcookie, &(ldcp->tx_dring_cookie), sizeof (dcookie));
ldcp->tx_dring_ncookies = ncookies;
ldcp->num_txds = num_desc;
/* Initialize tx dring indexes and seqnum */
ldcp->next_txi = ldcp->cur_txi = ldcp->resched_peer_txi = 0;
ldcp->next_txseq = VNET_ISS - 1;
ldcp->resched_peer = B_TRUE;
ldcp->dring_mtype = minfo.mtype;
ldcp->dringdata_msgid = 0;
/* Save peer's dring_info values */
bcopy(&dcookie, &(ldcp->peer_hparams.dring_cookie),
sizeof (ldc_mem_cookie_t));
ldcp->peer_hparams.num_desc = num_desc;
ldcp->peer_hparams.desc_size = desc_size;
ldcp->peer_hparams.dring_ncookies = ncookies;
/* Set dring_ident for the peer */
ldcp->peer_hparams.dring_ident = (uint64_t)ldcp->mtxdp;
/* Return the dring_ident in ack msg */
msg->dring_ident = (uint64_t)ldcp->mtxdp;
/*
* Mark the descriptor state as 'done'. This is implementation specific
* and not required by the protocol. In our implementation, we only
* need the descripor to be in 'done' state to be used by the transmit
* function and the peer is not aware of it. As the protocol requires
* that during initial registration the exporting end point mark the
* dstate as 'free', we change it 'done' here. After this, the dstate
* in our implementation will keep moving between 'ready', set by our
* transmit function; and and 'done', set by the peer (per protocol)
* after receiving data.
* Setup on_trap() protection before accessing dring shared memory area.
*/
rv = LDC_ON_TRAP(&otd);
if (rv != 0) {
/*
* Data access fault occured down the code path below while
* accessing the descriptors. Return failure.
*/
goto fail;
}
for (i = 0; i < num_desc; i++) {
txdp = &ldcp->mtxdp[i];
txdp->dstate = VIO_DESC_DONE;
}
(void) LDC_NO_TRAP();
return (VGEN_SUCCESS);
fail:
if (ldcp->tx_dring_handle != 0) {
(void) ldc_mem_dring_unmap(ldcp->tx_dring_handle);
ldcp->tx_dring_handle = 0;
}
return (VGEN_FAILURE);
}
/*
* Unmap the transmit descriptor ring.
*/
void
vgen_unmap_tx_dring(vgen_ldc_t *ldcp)
{
/* Unmap mapped tx data area */
if (ldcp->tx_datap != NULL) {
(void) ldc_mem_unmap(ldcp->tx_data_handle);
ldcp->tx_datap = NULL;
}
/* Free tx data area handle */
if (ldcp->tx_data_handle != 0) {
(void) ldc_mem_free_handle(ldcp->tx_data_handle);
ldcp->tx_data_handle = 0;
}
/* Free tx data area cookies */
if (ldcp->tx_data_cookie != NULL) {
kmem_free(ldcp->tx_data_cookie, ldcp->tx_data_ncookies *
sizeof (ldc_mem_cookie_t));
ldcp->tx_data_cookie = NULL;
ldcp->tx_data_ncookies = 0;
}
/* Unmap peer's dring */
if (ldcp->tx_dring_handle != 0) {
(void) ldc_mem_dring_unmap(ldcp->tx_dring_handle);
ldcp->tx_dring_handle = 0;
}
/* clobber tx ring members */
bzero(&ldcp->tx_dring_cookie, sizeof (ldcp->tx_dring_cookie));
ldcp->mtxdp = NULL;
ldcp->next_txi = ldcp->cur_txi = ldcp->resched_peer_txi = 0;
ldcp->num_txds = 0;
ldcp->next_txseq = VNET_ISS - 1;
ldcp->resched_peer = B_TRUE;
}
/*
* Map the shared memory data buffer area exported by the peer.
*/
int
vgen_map_data(vgen_ldc_t *ldcp, void *pkt)
{
int rv;
vio_dring_reg_ext_msg_t *emsg;
vio_dring_reg_msg_t *msg = (vio_dring_reg_msg_t *)pkt;
uint8_t *buf = (uint8_t *)msg->cookie;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
ldc_mem_info_t minfo;
/* skip over dring cookies */
ASSERT(msg->ncookies == 1);
buf += (msg->ncookies * sizeof (ldc_mem_cookie_t));
emsg = (vio_dring_reg_ext_msg_t *)buf;
if (emsg->data_ncookies > VNET_DATA_AREA_COOKIES) {
return (VGEN_FAILURE);
}
/* save # of data area cookies */
ldcp->tx_data_ncookies = emsg->data_ncookies;
/* save data area size */
ldcp->tx_data_sz = emsg->data_area_size;
/* allocate ldc mem handle for data area */
rv = ldc_mem_alloc_handle(ldcp->ldc_handle, &ldcp->tx_data_handle);
if (rv != 0) {
DWARN(vgenp, ldcp, "ldc_mem_alloc_handle() failed: %d\n", rv);
return (VGEN_FAILURE);
}
/* map the data area */
rv = ldc_mem_map(ldcp->tx_data_handle, emsg->data_cookie,
emsg->data_ncookies, LDC_DIRECT_MAP, LDC_MEM_W,
(caddr_t *)&ldcp->tx_datap, NULL);
if (rv != 0) {
DWARN(vgenp, ldcp, "ldc_mem_map() failed: %d\n", rv);
return (VGEN_FAILURE);
}
/* get the map info */
rv = ldc_mem_info(ldcp->tx_data_handle, &minfo);
if (rv != 0) {
DWARN(vgenp, ldcp, "ldc_mem_info() failed: %d\n", rv);
return (VGEN_FAILURE);
}
if (minfo.mtype != LDC_DIRECT_MAP) {
DWARN(vgenp, ldcp, "mtype(%d) is not direct map\n",
minfo.mtype);
return (VGEN_FAILURE);
}
/* allocate memory for data area cookies */
ldcp->tx_data_cookie = kmem_zalloc(emsg->data_ncookies *
sizeof (ldc_mem_cookie_t), KM_SLEEP);
/* save data area cookies */
bcopy(emsg->data_cookie, ldcp->tx_data_cookie,
emsg->data_ncookies * sizeof (ldc_mem_cookie_t));
return (VGEN_SUCCESS);
}
/*
* This function transmits normal data frames (non-priority) over the channel.
* It queues the frame into the transmit descriptor ring and sends a
* VIO_DRING_DATA message if needed, to wake up the peer to (re)start
* processing.
*/
int
vgen_dringsend_shm(void *arg, mblk_t *mp)
{
uint32_t next_txi;
uint32_t txi;
vnet_rx_dringdata_desc_t *txdp;
struct ether_header *ehp;
size_t mblksz;
caddr_t dst;
mblk_t *bp;
size_t size;
uint32_t buf_offset;
on_trap_data_t otd;
int rv = 0;
boolean_t is_bcast = B_FALSE;
boolean_t is_mcast = B_FALSE;
vgen_ldc_t *ldcp = (vgen_ldc_t *)arg;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
vgen_stats_t *statsp = &ldcp->stats;
vgen_hparams_t *lp = &ldcp->local_hparams;
boolean_t resched_peer = B_FALSE;
boolean_t tx_update = B_FALSE;
/* Drop the packet if ldc is not up or handshake is not done */
if (ldcp->ldc_status != LDC_UP) {
DBG2(vgenp, ldcp, "status(%d), dropping packet\n",
ldcp->ldc_status);
goto dringsend_shm_exit;
}
if (ldcp->hphase != VH_DONE) {
DWARN(vgenp, ldcp, "hphase(%x), dropping packet\n",
ldcp->hphase);
goto dringsend_shm_exit;
}
size = msgsize(mp);
if (size > (size_t)lp->mtu) {
DWARN(vgenp, ldcp, "invalid size(%d)\n", size);
goto dringsend_shm_exit;
}
if (size < ETHERMIN)
size = ETHERMIN;
ehp = (struct ether_header *)mp->b_rptr;
is_bcast = IS_BROADCAST(ehp);
is_mcast = IS_MULTICAST(ehp);
/*
* Setup on_trap() protection before accessing shared memory areas
* (descriptor and data buffer). Note that we enable this protection a
* little early and turn it off slightly later, than keeping it enabled
* strictly at the points in code below where the descriptor and data
* buffer are accessed. This is done for performance reasons:
* (a) to avoid calling the trap protection code while holding mutex.
* (b) to avoid multiple on/off steps for descriptor and data accesses.
*/
rv = LDC_ON_TRAP(&otd);
if (rv != 0) {
/*
* Data access fault occured down the code path below while
* accessing either the descriptor or the data buffer. Release
* any locks that we might have acquired in the code below and
* return failure.
*/
DERR(vgenp, ldcp, "data access fault occured\n");
statsp->oerrors++;
if (mutex_owned(&ldcp->txlock)) {
mutex_exit(&ldcp->txlock);
}
if (mutex_owned(&ldcp->wrlock)) {
mutex_exit(&ldcp->wrlock);
}
goto dringsend_shm_exit;
}
/*
* Allocate a descriptor
*/
mutex_enter(&ldcp->txlock);
txi = next_txi = ldcp->next_txi;
INCR_TXI(next_txi, ldcp);
txdp = &(ldcp->mtxdp[txi]);
if (txdp->dstate != VIO_DESC_DONE) { /* out of descriptors */
if (ldcp->tx_blocked == B_FALSE) {
ldcp->tx_blocked_lbolt = ddi_get_lbolt();
ldcp->tx_blocked = B_TRUE;
}
statsp->tx_no_desc++;
mutex_exit(&ldcp->txlock);
(void) LDC_NO_TRAP();
return (VGEN_TX_NORESOURCES);
} else {
txdp->dstate = VIO_DESC_INITIALIZING;
}
if (ldcp->tx_blocked == B_TRUE) {
ldcp->tx_blocked = B_FALSE;
tx_update = B_TRUE;
}
/* Update descriptor ring index */
ldcp->next_txi = next_txi;
mutex_exit(&ldcp->txlock);
if (tx_update == B_TRUE) {
vio_net_tx_update_t vtx_update =
ldcp->portp->vcb.vio_net_tx_update;
vtx_update(ldcp->portp->vhp);
}
/* Ensure load ordering of dstate (above) and data_buf_offset. */
MEMBAR_CONSUMER();
/* Get the offset of the buffer to be used */
buf_offset = txdp->data_buf_offset;
/* Access the buffer using the offset */
dst = (caddr_t)ldcp->tx_datap + buf_offset;
/* Copy data into mapped transmit buffer */
for (bp = mp; bp != NULL; bp = bp->b_cont) {
mblksz = MBLKL(bp);
bcopy(bp->b_rptr, dst, mblksz);
dst += mblksz;
}
/* Set the size of data in the descriptor */
txdp->nbytes = size;
/*
* Ensure store ordering of nbytes and dstate (below); so that the peer
* sees the right nbytes value after it checks that the dstate is READY.
*/
MEMBAR_PRODUCER();
mutex_enter(&ldcp->wrlock);
ASSERT(txdp->dstate == VIO_DESC_INITIALIZING);
/* Mark the descriptor ready */
txdp->dstate = VIO_DESC_READY;
/* Check if peer needs wake up (handled below) */
if (ldcp->resched_peer == B_TRUE && ldcp->resched_peer_txi == txi) {
resched_peer = B_TRUE;
ldcp->resched_peer = B_FALSE;
}
/* Update tx stats */
statsp->opackets++;
statsp->obytes += size;
if (is_bcast)
statsp->brdcstxmt++;
else if (is_mcast)
statsp->multixmt++;
mutex_exit(&ldcp->wrlock);
/*
* We are done accessing shared memory; clear trap protection.
*/
(void) LDC_NO_TRAP();
/*
* Need to wake up the peer ?
*/
if (resched_peer == B_TRUE) {
rv = vgen_send_dringdata_shm(ldcp, (uint32_t)txi, -1);
if (rv != 0) {
/* error: drop the packet */
DWARN(vgenp, ldcp, "failed sending dringdata msg "
"rv(%d) len(%d)\n", rv, size);
mutex_enter(&ldcp->wrlock);
statsp->oerrors++;
ldcp->resched_peer = B_TRUE;
mutex_exit(&ldcp->wrlock);
}
}
dringsend_shm_exit:
if (rv == ECONNRESET || rv == EACCES) {
(void) vgen_handle_evt_reset(ldcp, VGEN_OTHER);
}
freemsg(mp);
return (VGEN_TX_SUCCESS);
}
/*
* Process dring data messages (info/ack/nack)
*/
int
vgen_handle_dringdata_shm(void *arg1, void *arg2)
{
vgen_ldc_t *ldcp = (vgen_ldc_t *)arg1;
vio_msg_tag_t *tagp = (vio_msg_tag_t *)arg2;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
int rv = 0;
switch (tagp->vio_subtype) {
case VIO_SUBTYPE_INFO:
/*
* To reduce the locking contention, release the
* cblock here and re-acquire it once we are done
* receiving packets.
*/
mutex_exit(&ldcp->cblock);
mutex_enter(&ldcp->rxlock);
rv = vgen_handle_dringdata_info_shm(ldcp, tagp);
mutex_exit(&ldcp->rxlock);
mutex_enter(&ldcp->cblock);
if (rv != 0) {
DWARN(vgenp, ldcp, "handle_data_info failed(%d)\n", rv);
}
break;
case VIO_SUBTYPE_ACK:
rv = vgen_handle_dringdata_ack_shm(ldcp, tagp);
if (rv != 0) {
DWARN(vgenp, ldcp, "handle_data_ack failed(%d)\n", rv);
}
break;
case VIO_SUBTYPE_NACK:
rv = vgen_handle_dringdata_nack_shm(ldcp, tagp);
if (rv != 0) {
DWARN(vgenp, ldcp, "handle_data_nack failed(%d)\n", rv);
}
break;
}
return (rv);
}
static int
vgen_handle_dringdata_info_shm(vgen_ldc_t *ldcp, vio_msg_tag_t *tagp)
{
uint32_t start;
int32_t end;
int rv = 0;
vio_dring_msg_t *dringmsg = (vio_dring_msg_t *)tagp;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
vgen_stats_t *statsp = &ldcp->stats;
start = dringmsg->start_idx;
end = dringmsg->end_idx;
DBG1(vgenp, ldcp, "INFO: start(%d), end(%d)\n",
start, end);
if (!(CHECK_RXI(start, ldcp)) ||
((end != -1) && !(CHECK_RXI(end, ldcp)))) {
DWARN(vgenp, ldcp, "Invalid Rx start(%d) or end(%d)\n",
start, end);
/* drop the message if invalid index */
return (0);
}
/* validate dring_ident */
if (dringmsg->dring_ident != ldcp->peer_hparams.dring_ident) {
DWARN(vgenp, ldcp, "Invalid dring ident 0x%x\n",
dringmsg->dring_ident);
/* invalid dring_ident, drop the msg */
return (0);
}
statsp->dring_data_msgs_rcvd++;
/*
* If we are in polling mode, return from here without processing the
* dring. We will process the dring in the context of polling thread.
*/
if (ldcp->polling_on == B_TRUE) {
return (0);
}
/*
* Process the dring and receive packets in intr context.
*/
rv = vgen_intr_rcv_shm(ldcp);
if (rv != 0) {
DWARN(vgenp, ldcp, "vgen_intr_rcv_shm() failed\n");
}
return (rv);
}
/*
* Process the rx descriptor ring in the context of interrupt thread
* (vgen_ldc_cb() callback) and send the received packets up the stack.
*/
static int
vgen_intr_rcv_shm(vgen_ldc_t *ldcp)
{
int rv;
uint32_t end_ix;
vio_dring_msg_t msg;
uint_t mblk_sz;
int count = 0;
int total_count = 0;
mblk_t *bp = NULL;
mblk_t *bpt = NULL;
mblk_t *mp = NULL;
vio_net_rx_cb_t vrx_cb = ldcp->portp->vcb.vio_net_rx_cb;
ASSERT(MUTEX_HELD(&ldcp->rxlock));
do {
rv = vgen_receive_packet(ldcp, &mp, &mblk_sz);
if (rv != 0) {
if (rv == EINVAL) {
/* Invalid descriptor error; get next */
continue;
}
DTRACE_PROBE1(vgen_intr_nopkts, vgen_ldc_t *, ldcp);
break;
}
/* Build a chain of received packets */
if (bp == NULL) {
/* first pkt */
bp = mp;
bpt = bp;
bpt->b_next = NULL;
} else {
mp->b_next = NULL;
bpt->b_next = mp;
bpt = mp;
}
total_count++;
count++;
/*
* We are receiving the packets in interrupt context. If we
* have gathered vgen_chain_len (tunable) # of packets in the
* chain, send them up. (See vgen_poll_rcv_shm() for receiving
* in polling thread context).
*/
if (count == vgen_chain_len) {
DTRACE_PROBE2(vgen_intr_pkts, vgen_ldc_t *, ldcp,
int, count);
mutex_exit(&ldcp->rxlock);
vrx_cb(ldcp->portp->vhp, bp);
mutex_enter(&ldcp->rxlock);
bp = bpt = NULL;
count = 0;
}
/*
* Stop further processing if we processed the entire dring
* once; otherwise continue.
*/
} while (total_count < ldcp->num_rxds);
if (bp != NULL) {
DTRACE_PROBE2(vgen_intr_pkts, vgen_ldc_t *, ldcp, int, count);
mutex_exit(&ldcp->rxlock);
vrx_cb(ldcp->portp->vhp, bp);
mutex_enter(&ldcp->rxlock);
}
if (ldcp->polling_on == B_FALSE) {
/*
* We send a stopped message to peer (sender) while we are in
* intr mode only; allowing the peer to send further data intrs
* (dring data msgs) to us.
*/
end_ix = ldcp->next_rxi;
DECR_RXI(end_ix, ldcp);
msg.dring_ident = ldcp->peer_hparams.dring_ident;
rv = vgen_send_dringack_shm(ldcp, (vio_msg_tag_t *)&msg,
VNET_START_IDX_UNSPEC, end_ix, VIO_DP_STOPPED);
return (rv);
}
return (0);
}
/*
* Process the rx descriptor ring in the context of mac polling thread. Receive
* packets upto the limit specified by bytes_to_pickup or until there are no
* more packets, whichever occurs first. Return the chain of received packets.
*/
mblk_t *
vgen_poll_rcv_shm(vgen_ldc_t *ldcp, int bytes_to_pickup)
{
uint_t mblk_sz = 0;
uint_t sz = 0;
mblk_t *bp = NULL;
mblk_t *bpt = NULL;
mblk_t *mp = NULL;
int count = 0;
int rv;
mutex_enter(&ldcp->rxlock);
if (ldcp->hphase != VH_DONE) {
/* Channel is being reset and handshake not complete */
mutex_exit(&ldcp->rxlock);
return (NULL);
}
do {
rv = vgen_receive_packet(ldcp, &mp, &mblk_sz);
if (rv != 0) {
if (rv == EINVAL) {
/* Invalid descriptor error; get next */
continue;
}
DTRACE_PROBE1(vgen_poll_nopkts, vgen_ldc_t *, ldcp);
break;
}
/* Build a chain of received packets */
if (bp == NULL) {
/* first pkt */
bp = mp;
bpt = bp;
bpt->b_next = NULL;
} else {
mp->b_next = NULL;
bpt->b_next = mp;
bpt = mp;
}
/* Compute total size accumulated */
sz += mblk_sz;
count++;
/* Reached the bytes limit; we are done. */
if (sz >= bytes_to_pickup) {
break;
}
_NOTE(CONSTCOND)
} while (1);
/*
* We prepend any high priority packets to the chain of packets; note
* that if we are already at the bytes_to_pickup limit, we might
* slightly exceed that in such cases. That should be ok, as these pkts
* are expected to be small in size and arrive at an interval in the
* the order of a few seconds.
*/
if (ldcp->rx_pktdata == vgen_handle_pkt_data &&
ldcp->rx_pri_head != NULL) {
ldcp->rx_pri_tail->b_next = bp;
bp = ldcp->rx_pri_head;
ldcp->rx_pri_head = ldcp->rx_pri_tail = NULL;
}
mutex_exit(&ldcp->rxlock);
DTRACE_PROBE2(vgen_poll_pkts, vgen_ldc_t *, ldcp, int, count);
DTRACE_PROBE2(vgen_poll_bytes, vgen_ldc_t *, ldcp, uint_t, sz);
return (bp);
}
/*
* Process the next index in the rx dring and receive the associated packet.
*
* Returns:
* bp: Success: The received packet.
* Failure: NULL
* size: Success: Size of received packet.
* Failure: 0
* retval:
* Success: 0
* Failure: EAGAIN: Descriptor not ready
* EIO: Descriptor contents invalid.
*/
static int
vgen_receive_packet(vgen_ldc_t *ldcp, mblk_t **bp, uint_t *size)
{
uint32_t rxi;
vio_mblk_t *vmp;
vio_mblk_t *new_vmp;
struct ether_header *ehp;
vnet_rx_dringdata_desc_t *rxdp;
int err = 0;
uint32_t nbytes = 0;
mblk_t *mp = NULL;
mblk_t *dmp = NULL;
vgen_stats_t *statsp = &ldcp->stats;
vgen_hparams_t *lp = &ldcp->local_hparams;
rxi = ldcp->next_rxi;
rxdp = &(ldcp->rxdp[rxi]);
vmp = ldcp->rxdp_to_vmp[rxi];
if (rxdp->dstate != VIO_DESC_READY) {
/*
* Descriptor is not ready.
*/
DTRACE_PROBE1(vgen_noready_rxds, vgen_ldc_t *, ldcp);
return (EAGAIN);
}
/*
* Ensure load ordering of dstate and nbytes.
*/
MEMBAR_CONSUMER();
nbytes = rxdp->nbytes;
if ((nbytes < ETHERMIN) ||
(nbytes > lp->mtu) ||
(rxdp->data_buf_offset !=
(VIO_MBLK_DATA_OFF(vmp) + VNET_IPALIGN))) {
/*
* Descriptor contents invalid.
*/
statsp->ierrors++;
rxdp->dstate = VIO_DESC_DONE;
err = EIO;
goto done;
}
/*
* Now allocate a new buffer for this descriptor before sending up the
* buffer being processed. If that fails, stop processing; as we are
* out of receive buffers.
*/
new_vmp = vio_allocb(ldcp->rx_vmp);
/*
* Process the current buffer being received.
*/
mp = vmp->mp;
if (new_vmp == NULL) {
/*
* We failed to get a new mapped buffer that is needed to
* refill the descriptor. In that case, leave the current
* buffer bound to the descriptor; allocate an mblk dynamically
* and copy the contents of the buffer to the mblk. Then send
* up this mblk. This way the sender has the same buffer as
* before that can be used to send new data.
*/
statsp->norcvbuf++;
dmp = allocb(nbytes + VNET_IPALIGN, BPRI_MED);
if (dmp == NULL) {
statsp->ierrors++;
return (ENOMEM);
}
bcopy(mp->b_rptr + VNET_IPALIGN,
dmp->b_rptr + VNET_IPALIGN, nbytes);
mp = dmp;
} else {
/* Mark the status of the current rbuf */
vmp->state = VIO_MBLK_HAS_DATA;
/* Set the offset of the new buffer in the descriptor */
rxdp->data_buf_offset =
VIO_MBLK_DATA_OFF(new_vmp) + VNET_IPALIGN;
ldcp->rxdp_to_vmp[rxi] = new_vmp;
}
mp->b_rptr += VNET_IPALIGN;
mp->b_wptr = mp->b_rptr + nbytes;
/*
* Ensure store ordering of data_buf_offset and dstate; so that the
* peer sees the right data_buf_offset after it checks that the dstate
* is DONE.
*/
MEMBAR_PRODUCER();
/* Now mark the descriptor 'done' */
rxdp->dstate = VIO_DESC_DONE;
/* Update stats */
statsp->ipackets++;
statsp->rbytes += rxdp->nbytes;
ehp = (struct ether_header *)mp->b_rptr;
if (IS_BROADCAST(ehp))
statsp->brdcstrcv++;
else if (IS_MULTICAST(ehp))
statsp->multircv++;
done:
/* Update the next index to be processed */
INCR_RXI(rxi, ldcp);
/* Save the new recv index */
ldcp->next_rxi = rxi;
/* Return the packet received */
*size = nbytes;
*bp = mp;
return (err);
}
static int
vgen_handle_dringdata_ack_shm(vgen_ldc_t *ldcp, vio_msg_tag_t *tagp)
{
uint32_t start;
int32_t end;
uint32_t txi;
vgen_stats_t *statsp;
vnet_rx_dringdata_desc_t *txdp;
on_trap_data_t otd;
int rv = 0;
boolean_t ready_txd = B_FALSE;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
vio_dring_msg_t *dringmsg = (vio_dring_msg_t *)tagp;
start = dringmsg->start_idx;
end = dringmsg->end_idx;
statsp = &ldcp->stats;
/*
* Received an ack for our transmits upto a certain dring index. This
* enables us to reclaim descriptors. We also send a new dring data msg
* to the peer to restart processing if there are pending transmit pkts.
*/
DBG2(vgenp, ldcp, "ACK: start(%d), end(%d)\n", start, end);
/*
* In RxDringData mode (v1.6), start index of -1 can be used by the
* peer to indicate that it is unspecified. However, the end index
* must be set correctly indicating the last descriptor index processed.
*/
if (((start != VNET_START_IDX_UNSPEC) && !(CHECK_TXI(start, ldcp))) ||
!(CHECK_TXI(end, ldcp))) {
/* drop the message if invalid index */
DWARN(vgenp, ldcp, "Invalid Tx ack start(%d) or end(%d)\n",
start, end);
return (rv);
}
/* Validate dring_ident */
if (dringmsg->dring_ident != ldcp->local_hparams.dring_ident) {
/* invalid dring_ident, drop the msg */
DWARN(vgenp, ldcp, "Invalid dring ident 0x%x\n",
dringmsg->dring_ident);
return (rv);
}
statsp->dring_data_acks_rcvd++;
/*
* Clear transmit flow control condition
* as some descriptors should be free now.
*/
mutex_enter(&ldcp->txlock);
if (ldcp->tx_blocked == B_TRUE) {
vio_net_tx_update_t vtx_update =
ldcp->portp->vcb.vio_net_tx_update;
ldcp->tx_blocked = B_FALSE;
vtx_update(ldcp->portp->vhp);
}
mutex_exit(&ldcp->txlock);
if (dringmsg->dring_process_state != VIO_DP_STOPPED) {
/*
* Receiver continued processing
* dring after sending us the ack.
*/
return (rv);
}
/*
* Receiver stopped processing descriptors.
*/
statsp->dring_stopped_acks_rcvd++;
/*
* Setup on_trap() protection before accessing dring shared memory area.
*/
rv = LDC_ON_TRAP(&otd);
if (rv != 0) {
/*
* Data access fault occured down the code path below while
* accessing the descriptors. Release any locks that we might
* have acquired in the code below and return failure.
*/
if (mutex_owned(&ldcp->wrlock)) {
mutex_exit(&ldcp->wrlock);
}
return (ECONNRESET);
}
/*
* Determine if there are any pending tx descriptors ready to be
* processed by the receiver(peer) and if so, send a message to the
* peer to restart receiving.
*/
mutex_enter(&ldcp->wrlock);
ready_txd = B_FALSE;
txi = end;
INCR_TXI(txi, ldcp);
txdp = &ldcp->mtxdp[txi];
if (txdp->dstate == VIO_DESC_READY) {
ready_txd = B_TRUE;
}
/*
* We are done accessing shared memory; clear trap protection.
*/
(void) LDC_NO_TRAP();
if (ready_txd == B_FALSE) {
/*
* No ready tx descriptors. Set the flag to send a message to
* the peer when tx descriptors are ready in transmit routine.
*/
ldcp->resched_peer = B_TRUE;
ldcp->resched_peer_txi = txi;
mutex_exit(&ldcp->wrlock);
return (rv);
}
/*
* We have some tx descriptors ready to be processed by the receiver.
* Send a dring data message to the peer to restart processing.
*/
ldcp->resched_peer = B_FALSE;
mutex_exit(&ldcp->wrlock);
rv = vgen_send_dringdata_shm(ldcp, txi, -1);
if (rv != VGEN_SUCCESS) {
mutex_enter(&ldcp->wrlock);
ldcp->resched_peer = B_TRUE;
mutex_exit(&ldcp->wrlock);
}
return (rv);
}
static int
vgen_handle_dringdata_nack_shm(vgen_ldc_t *ldcp, vio_msg_tag_t *tagp)
{
uint32_t start;
int32_t end;
uint32_t txi;
vnet_rx_dringdata_desc_t *txdp;
on_trap_data_t otd;
int rv = 0;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
vio_dring_msg_t *dringmsg = (vio_dring_msg_t *)tagp;
DBG1(vgenp, ldcp, "enter\n");
start = dringmsg->start_idx;
end = dringmsg->end_idx;
/*
* Peer sent a NACK msg (to indicate bad descriptors ?). The start and
* end correspond to the range of descriptors which are being nack'd.
*/
DWARN(vgenp, ldcp, "NACK: start(%d), end(%d)\n", start, end);
/*
* In RxDringData mode (v1.6), start index of -1 can be used by
* the peer to indicate that it is unspecified. However, the end index
* must be set correctly indicating the last descriptor index processed.
*/
if (((start != VNET_START_IDX_UNSPEC) && !(CHECK_TXI(start, ldcp))) ||
!(CHECK_TXI(end, ldcp))) {
/* drop the message if invalid index */
DWARN(vgenp, ldcp, "Invalid Tx nack start(%d) or end(%d)\n",
start, end);
return (rv);
}
/* Validate dring_ident */
if (dringmsg->dring_ident != ldcp->local_hparams.dring_ident) {
/* invalid dring_ident, drop the msg */
DWARN(vgenp, ldcp, "Invalid dring ident 0x%x\n",
dringmsg->dring_ident);
return (rv);
}
/*
* Setup on_trap() protection before accessing dring shared memory area.
*/
rv = LDC_ON_TRAP(&otd);
if (rv != 0) {
/*
* Data access fault occured down the code path below while
* accessing the descriptors. Release any locks that we might
* have acquired in the code below and return failure.
*/
mutex_exit(&ldcp->txlock);
return (ECONNRESET);
}
/* We just mark the descrs as free so they can be reused */
mutex_enter(&ldcp->txlock);
for (txi = start; txi <= end; ) {
txdp = &(ldcp->mtxdp[txi]);
if (txdp->dstate == VIO_DESC_READY)
txdp->dstate = VIO_DESC_DONE;
INCR_TXI(txi, ldcp);
}
/*
* We are done accessing shared memory; clear trap protection.
*/
(void) LDC_NO_TRAP();
mutex_exit(&ldcp->txlock);
return (rv);
}
/*
* Send descriptor ring data message to the peer over LDC.
*/
static int
vgen_send_dringdata_shm(vgen_ldc_t *ldcp, uint32_t start, int32_t end)
{
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
vio_dring_msg_t dringmsg, *msgp = &dringmsg;
vio_msg_tag_t *tagp = &msgp->tag;
vgen_stats_t *statsp = &ldcp->stats;
int rv;
#ifdef DEBUG
if (vgen_inject_error(ldcp, VGEN_ERR_TXTIMEOUT)) {
return (VGEN_SUCCESS);
}
#endif
bzero(msgp, sizeof (*msgp));
tagp->vio_msgtype = VIO_TYPE_DATA;
tagp->vio_subtype = VIO_SUBTYPE_INFO;
tagp->vio_subtype_env = VIO_DRING_DATA;
tagp->vio_sid = ldcp->local_sid;
msgp->dring_ident = ldcp->local_hparams.dring_ident;
msgp->start_idx = start;
msgp->end_idx = end;
msgp->seq_num = atomic_inc_32_nv(&ldcp->dringdata_msgid);
rv = vgen_sendmsg_shm(ldcp, (caddr_t)tagp, sizeof (dringmsg));
if (rv != VGEN_SUCCESS) {
DWARN(vgenp, ldcp, "vgen_sendmsg_shm() failed\n");
return (rv);
}
statsp->dring_data_msgs_sent++;
DBG2(vgenp, ldcp, "DRING_DATA_SENT \n");
return (VGEN_SUCCESS);
}
/*
* Send dring data ack message.
*/
int
vgen_send_dringack_shm(vgen_ldc_t *ldcp, vio_msg_tag_t *tagp, uint32_t start,
int32_t end, uint8_t pstate)
{
int rv = 0;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
vio_dring_msg_t *msgp = (vio_dring_msg_t *)tagp;
vgen_stats_t *statsp = &ldcp->stats;
tagp->vio_msgtype = VIO_TYPE_DATA;
tagp->vio_subtype = VIO_SUBTYPE_ACK;
tagp->vio_subtype_env = VIO_DRING_DATA;
tagp->vio_sid = ldcp->local_sid;
msgp->start_idx = start;
msgp->end_idx = end;
msgp->dring_process_state = pstate;
msgp->seq_num = atomic_inc_32_nv(&ldcp->dringdata_msgid);
rv = vgen_sendmsg_shm(ldcp, (caddr_t)tagp, sizeof (*msgp));
if (rv != VGEN_SUCCESS) {
DWARN(vgenp, ldcp, "vgen_sendmsg_shm() failed\n");
}
statsp->dring_data_acks_sent++;
if (pstate == VIO_DP_STOPPED) {
statsp->dring_stopped_acks_sent++;
}
return (rv);
}
/*
* Send dring data msgs (info/ack/nack) over LDC.
*/
static int
vgen_sendmsg_shm(vgen_ldc_t *ldcp, caddr_t msg, size_t msglen)
{
int rv;
size_t len;
uint32_t retries = 0;
vgen_t *vgenp = LDC_TO_VGEN(ldcp);
len = msglen;
if ((len == 0) || (msg == NULL))
return (VGEN_FAILURE);
do {
len = msglen;
rv = ldc_write(ldcp->ldc_handle, (caddr_t)msg, &len);
if (retries++ >= vgen_ldcwr_retries)
break;
} while (rv == EWOULDBLOCK);
if (rv != 0) {
DWARN(vgenp, ldcp, "ldc_write failed: rv(%d) msglen(%d)\n",
rv, msglen);
return (rv);
}
if (len != msglen) {
DWARN(vgenp, ldcp, "ldc_write failed: rv(%d) msglen (%d)\n",
rv, msglen);
return (VGEN_FAILURE);
}
return (VGEN_SUCCESS);
}
|