summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/ib/ibtl/ibtl_hca.c
blob: 7d08be1b01b4697da9659a65102990666348b1fb (plain)
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
/*
 * 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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * ibtl_hca.c
 *
 * This file contains Transport API functions related to
 * Host Channel Adapter (HCA) Verbs.
 */

#include <sys/ib/ibtl/impl/ibtl.h>

static char ibtf_hca[] = "ibtl_hca";

/* Prototype declarations. */
static ibt_status_t ibtl_query_hca_ports(ibtl_hca_devinfo_t *hca_devp,
    uint8_t port, ibt_hca_portinfo_t **port_info_p, uint_t *ports_p,
    uint_t *size_p, int use_cache);

/*
 * Function:
 *      ibt_open_hca
 * Input:
 *      ibt_hdl    - IBT Client Handle
 *      hca_guid   - HCA's node GUID.
 * Output:
 *      hca_hdl_p  - IBT HCA Handle.
 * Returns:
 *      IBT_SUCCESS
 *      IBT_HCA_IN_USE
 *      IBT_HCA_INVALID
 * Description:
 *      Open a HCA. HCA can only be opened/closed once. This routine allocates
 *      and returns a unique IBT Client HCA handle. Clients passes this
 *      handle on its subsequent references to this device. Once opened by a
 *      client, a specific HCA cannot be opened again until after it is closed.
 *      The IBT_HCA_IN_USE error is returned if client tries to open multiple
 *      times. In this case, previously allocated IBT HCA handle is returned to
 *      the client. Opening the HCA prepares the HCA for use by the client.
 */
ibt_status_t
ibt_open_hca(ibt_clnt_hdl_t ibt_hdl, ib_guid_t hca_guid,
    ibt_hca_hdl_t *hca_hdl_p)
{
	ibtl_hca_t  		*hca_infop;
	ibtl_hca_devinfo_t	*hca_devp;		/* HCA Dev Info */

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_open_hca(%p, %llX)", ibt_hdl, hca_guid);


	/*
	 * Get HCA Device Info Structure, referenced by HCA GUID.
	 */
	mutex_enter(&ibtl_clnt_list_mutex);
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		/*
		 * If we are here, then the requested HCA device is not present.
		 * Return the status as Invalid HCA GUID.
		 */
		mutex_exit(&ibtl_clnt_list_mutex);

		IBTF_DPRINTF_L2(ibtf_hca, "ibt_open_hca: "
		    "HCA Device Not Found: Invalid HCA GUID");

		*hca_hdl_p = NULL;
		return (IBT_HCA_INVALID);
	}

	/*
	 * Check whether open is allowed for this dip
	 */
	if (ibt_hdl->clnt_dip) {
		if (ddi_get_parent(ibt_hdl->clnt_dip) == hca_devp->hd_hca_dip) {
			if (hca_guid != hca_devp->hd_hca_attr->hca_node_guid) {
				mutex_exit(&ibtl_clnt_list_mutex);
				return (IBT_FAILURE);
			}
		}
	}

	if (hca_devp->hd_state != IBTL_HCA_DEV_ATTACHED) {
		/*
		 * If we are here, then the requested HCA device has detached,
		 * or is in the process of detaching.
		 */
		mutex_exit(&ibtl_clnt_list_mutex);

		IBTF_DPRINTF_L2(ibtf_hca, "ibt_open_hca: "
		    "HCA is busy trying to detach");

		*hca_hdl_p = NULL;
		return (IBT_HCA_BUSY_DETACHING);
	}

	/*
	 * Yes, we found a HCA Device registered with IBTF, which matches with
	 * the requested HCA_GUID.
	 *
	 * Check out whether this client has already opened this HCA device,
	 * if yes return the status as IBT_HCA_IN_USE.
	 */
	hca_infop = hca_devp->hd_clnt_list;

	while (hca_infop != NULL) {
		if (ibt_hdl == hca_infop->ha_clnt_devp) {
			IBTF_DPRINTF_L3(ibtf_hca,
			    "ibt_open_hca: Already Open");

			if (hca_infop->ha_flags & IBTL_HA_CLOSING) {
				mutex_exit(&ibtl_clnt_list_mutex);
				*hca_hdl_p = NULL;
				return (IBT_HCA_BUSY_CLOSING);
			}
			mutex_exit(&ibtl_clnt_list_mutex);

			/* Already Opened. Return back old HCA Handle. */
			*hca_hdl_p = hca_infop;

			return (IBT_HCA_IN_USE);
		}
		hca_infop = hca_infop->ha_clnt_link;
	}

	/* Create a new HCA Info entity. */
	hca_infop = kmem_zalloc(sizeof (ibtl_hca_t), KM_SLEEP);

	/* Update the HCA Info entity */
	hca_infop->ha_hca_devp  = hca_devp;	/* HCA Device Info */
	hca_infop->ha_clnt_devp = ibt_hdl;	/* Client Info */

	/* Update the HCA List, to keep track about the clients using it. */
	hca_infop->ha_clnt_link = hca_devp->hd_clnt_list;
	hca_devp->hd_clnt_list = hca_infop;


	/* Update the client's list to depict that it uses this HCA device. */
	hca_infop->ha_hca_link = ibt_hdl->clnt_hca_list;
	ibt_hdl->clnt_hca_list = hca_infop;

	mutex_exit(&ibtl_clnt_list_mutex);

	/*
	 * Return back the address of ibtl_hca_t structure as an opaque
	 * IBT HCA handle for the clients, to be used in future calls.
	 */
	*hca_hdl_p = hca_infop;

	return (IBT_SUCCESS);
}

static char *ibtl_close_error_fmt = "IBT CLOSE HCA failed: %d '%s' "
	"resources not yet freed by client '%s'\n";

#define	IBTL_CLOSE_RESOURCE_CHECK(counter, resource_type) \
	if ((cntr = atomic_add_32_nv(&(counter), 0)) != 0) {		\
		cmn_err(CE_CONT, ibtl_close_error_fmt,			\
		    cntr, resource_type,				\
		    hca_hdl->ha_clnt_devp->clnt_modinfop->mi_clnt_name); \
	}								\
	error |= cntr

/*
 * Function:
 *      ibt_close_hca
 * Input:
 *      hca_hdl  - The HCA handle as returned during its open.
 * Output:
 *      none
 * Returns:
 *      IBT_SUCCESS
 *      IBT_HCA_HDL_INVALID
 *      IBT_HCA_RESOURCES_NOT_FREED
 * Description:
 *      Close a HCA.
 */
ibt_status_t
ibt_close_hca(ibt_hca_hdl_t hca_hdl)
{
	ibtl_hca_devinfo_t	*hca_devp, *tmp_devp;
	ibtl_hca_t		**hcapp;
	ibtl_clnt_t		*clntp = hca_hdl->ha_clnt_devp;
	uint32_t		cntr, error;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_close_hca(%p)", hca_hdl);

	/*
	 * Verify the Input HCA Handle, if fake return error as
	 * invalid HCA Handle.
	 */
	mutex_enter(&ibtl_clnt_list_mutex);
	hca_devp = hca_hdl->ha_hca_devp;
	tmp_devp = ibtl_hca_list;

	for (; tmp_devp != NULL; tmp_devp = tmp_devp->hd_hca_dev_link)
		if (tmp_devp == hca_devp)
			break;

	if (tmp_devp == NULL) {
		mutex_exit(&ibtl_clnt_list_mutex);
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_close_hca: "
		    "Unable to find this on global HCA list");
		return (IBT_HCA_HDL_INVALID);
	}

	/* Make sure resources have been freed. */
	error = 0;
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_qp_cnt, "QP/Channel");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_eec_cnt, "EEC");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_cq_cnt, "CQ");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_pd_cnt, "Protection Domain");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_ah_cnt, "AH");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_mr_cnt, "Memory Region");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_mw_cnt, "Memory Window");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_qpn_cnt, "QPN");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_srq_cnt, "SRQ");
	IBTL_CLOSE_RESOURCE_CHECK(hca_hdl->ha_fmr_pool_cnt, "FMR Pool");
	if (error) {
		mutex_exit(&ibtl_clnt_list_mutex);
		return (IBT_HCA_RESOURCES_NOT_FREED);
	}

	/* we are now committed to closing the HCA */
	hca_hdl->ha_flags |= IBTL_HA_CLOSING;
	while (hca_hdl->ha_qpn_cnt > 0)
		cv_wait(&ibtl_close_hca_cv, &ibtl_clnt_list_mutex);

	/*
	 * Remove this HCA Device entry form Client's current list of HCA
	 * Device Instances being used by it.
	 */
	hcapp = &clntp->clnt_hca_list;

	for (; *hcapp != NULL; hcapp = &(*hcapp)->ha_hca_link)
		if (*hcapp == hca_hdl)
			break;

	if (*hcapp == NULL) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_close_hca: "
		    "Unable to find this HCA on client list");
		mutex_exit(&ibtl_clnt_list_mutex);
		return (IBT_HCA_HDL_INVALID);
	}

	/* hcapp now points to a link that points to us */
	*hcapp = hca_hdl->ha_hca_link;		/* remove us */

	/*
	 * Remove this Client's entry from this HCA Device's Clients list.
	 */
	hcapp = &hca_devp->hd_clnt_list;

	for (; *hcapp != NULL; hcapp = &(*hcapp)->ha_clnt_link)
		if (*hcapp == hca_hdl)
			break;

	if (*hcapp == NULL) {
		mutex_exit(&ibtl_clnt_list_mutex);
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_close_hca: "
		    "Unable to find this HCA on the client's HCA list");
		return (IBT_HCA_HDL_INVALID);
	}

	/* hcapp now points to a link that points to us */
	*hcapp = hca_hdl->ha_clnt_link;		/* remove us */
	mutex_exit(&ibtl_clnt_list_mutex);

	/* Free memory for this HCA Handle */
	ibtl_free_hca_async_check(hca_hdl);

	return (IBT_SUCCESS);
}

void
ibtl_close_hca_check(ibt_hca_hdl_t hca_hdl)
{
	IBTF_DPRINTF_L3(ibtf_hca, "ibtl_close_hca_check(%p)", hca_hdl);

	mutex_enter(&ibtl_clnt_list_mutex);
	if ((--hca_hdl->ha_qpn_cnt == 0) &&
	    (hca_hdl->ha_flags & IBTL_HA_CLOSING)) {
		cv_signal(&ibtl_close_hca_cv);
	}
	mutex_exit(&ibtl_clnt_list_mutex);
}

/*
 * Function:
 *      ibt_get_hca_list
 * Input:
 *      hca_list_p -  Address of pointer updated here.
 * Output:
 *      hca_list_p -  Points to an array of ib_guid_t's allocated here.
 * Returns:
 *      The actual number of valid ib_guid_t's returned.
 * Description:
 *	If hca_list_p is not NULL then the memory for the array of GUIDs is
 *	allocated here and should be freed by the caller using
 *	ibt_free_hca_list(). If hca_list_p is NULL then no memory is allocated
 *	by ibt_get_hca_list and only the number of HCAs in a system is returned.
 */
uint_t
ibt_get_hca_list(ib_guid_t **hca_list_p)
{
	uint_t			hca_count = 0;
	ibtl_hca_devinfo_t	*hca_devp;
	ib_guid_t		*hca_listp;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_get_hca_list(%p)", hca_list_p);

	mutex_enter(&ibtl_clnt_list_mutex);

	hca_devp = ibtl_hca_list;
	while (hca_devp != NULL) {
		hca_count++;
		hca_devp = hca_devp->hd_hca_dev_link;
	}

	if (hca_count == 0)
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_get_hca_list: "
		    "HCA device not found");

	if ((hca_count == 0) || (hca_list_p == NULL)) {
		mutex_exit(&ibtl_clnt_list_mutex);
		return (hca_count);
	}

	hca_listp = kmem_alloc(hca_count * sizeof (ib_guid_t), KM_SLEEP);
	*hca_list_p = hca_listp;

	hca_devp = ibtl_hca_list;
	while (hca_devp != NULL) {
		/* Traverse Global HCA List & retrieve HCA Node GUIDs. */
		*hca_listp++ = hca_devp->hd_hca_attr->hca_node_guid;
		hca_devp = hca_devp->hd_hca_dev_link;
	}
	mutex_exit(&ibtl_clnt_list_mutex);

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_get_hca_list: "
	    "Returned <%d> entries @0x%p", hca_count, *hca_list_p);

	return (hca_count);
}

/*
 * Function:
 *      ibt_free_hca_list
 * Input:
 *      hca_list  - The address of an ib_guid_t pointer.
 *      entries   - The number of ib_guid_t entries to be freed.
 * Output:
 *      none.
 * Returns:
 *      none.
 * Description:
 *      The memory allocated in ibt_get_hca_list() is freed in this function.
 */
void
ibt_free_hca_list(ib_guid_t *hca_list, uint_t entries)
{
	IBTF_DPRINTF_L3(ibtf_hca, "ibt_free_hca_list: "
	    "Free <%d> entries from 0x%p", entries, hca_list);

	if ((hca_list != NULL) && (entries > 0))
		kmem_free(hca_list, entries * sizeof (ib_guid_t));
}

/*
 * ibtl_portinfo_locked() is called when the portinfo cache is being
 * updated.  If this port's info update is in progress, we return 0
 * immediately and have the c
 * unless it's already in progress (distinguished by return value).
 * When done updating the portinfo, they call ibtl_portinfo_unlock().
 */

static int
ibtl_portinfo_locked(ibtl_hca_devinfo_t *hca_devp, uint8_t port)
{
	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));

	for (;;) {
		if (hca_devp->hd_portinfo_locked_port == 0) {
			hca_devp->hd_portinfo_locked_port = port;
			return (1); /* not busy, so OK to initiate update */
		} else if (hca_devp->hd_portinfo_locked_port == port) {
			IBTF_DPRINTF_L3(ibtf_hca, "ibtl_portinfo_locked: "
			    "HCA %p port %d is already locked",
			    hca_devp, port);
			hca_devp->hd_portinfo_waiters = 1;
			cv_wait(&hca_devp->hd_portinfo_cv,
			    &ibtl_clnt_list_mutex);
			return (0); /* it's now done, so no need to initiate */
		} else {
			/* need to wait for other port before we try again */
			hca_devp->hd_portinfo_waiters = 1;
			cv_wait(&hca_devp->hd_portinfo_cv,
			    &ibtl_clnt_list_mutex);
		}
	}
}

static void
ibtl_portinfo_unlock(ibtl_hca_devinfo_t *hca_devp, uint8_t port)
{
	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));
	ASSERT(hca_devp->hd_portinfo_locked_port == port);
	hca_devp->hd_portinfo_locked_port = 0;
	if (hca_devp->hd_portinfo_waiters) {
		hca_devp->hd_portinfo_waiters = 0;
		cv_broadcast(&hca_devp->hd_portinfo_cv);
		IBTF_DPRINTF_L3(ibtf_hca, "ibtl_portinfo_unlock: "
		    "waking up waiters for port %d info on HCA %p",
		    port, hca_devp);
	}
}

/*
 * Function:
 *      ibt_get_port_state
 * Input:
 *      hca_devp    - The HCA Dev Info pointer.
 *	port        - Port number to query.
 * Output:
 *      sgid_p	    - Returned sgid[0], NULL implies no return value.
 *      base_lid_p  - Returned base_lid, NULL implies no return value.
 * Returns:
 *      IBT_SUCCESS
 *	IBT_HCA_PORT_INVALID
 * Description:
 *      Returns HCA port attributes for one of the HCA ports.
 */
static ibt_status_t
ibtl_get_port_state(ibtl_hca_devinfo_t *hca_devp, uint8_t port,
    ib_gid_t *sgid_p, ib_lid_t *base_lid_p)
{
	ibt_hca_portinfo_t *portinfop;

	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));

	if ((port < 1) || (port > hca_devp->hd_hca_attr->hca_nports)) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibtl_get_port_state: "
		    "invalid port %d, nports = %d", port,
		    hca_devp->hd_hca_attr->hca_nports);
		return (IBT_HCA_PORT_INVALID);
	}
	portinfop = hca_devp->hd_portinfop + port - 1;
	if (portinfop->p_linkstate != IBT_PORT_ACTIVE)
		ibtl_reinit_hca_portinfo(hca_devp, port);

	if (sgid_p)
		*sgid_p = portinfop->p_sgid_tbl[0];
	if (base_lid_p)
		*base_lid_p = portinfop->p_base_lid;
	if (portinfop->p_linkstate != IBT_PORT_ACTIVE) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibtl_get_port_state: "
		    "port %d, port_state %d, base_lid %d",
		    port, portinfop->p_linkstate, portinfop->p_base_lid);
		return (IBT_HCA_PORT_NOT_ACTIVE);
	}
	IBTF_DPRINTF_L3(ibtf_hca, "ibtl_get_port_state: "
	    "port %d, port_state %d, base_lid %d",
	    port, portinfop->p_linkstate, portinfop->p_base_lid);
	return (IBT_SUCCESS);
}

/*
 * Function:
 *      ibt_get_port_state
 * Input:
 *      hca_hdl	    - The HCA handle.
 *	port        - Port number to query.
 * Output:
 *      sgid_p	    - Returned sgid[0], NULL implies no return value.
 *      base_lid_p  - Returned base_lid, NULL implies no return value.
 * Returns:
 *      IBT_SUCCESS
 *	IBT_HCA_PORT_INVALID
 * Description:
 *      Returns HCA port attributes for one of the HCA ports.
 */
ibt_status_t
ibt_get_port_state(ibt_hca_hdl_t hca_hdl, uint8_t port,
    ib_gid_t *sgid_p, ib_lid_t *base_lid_p)
{
	ibt_status_t		retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_get_port_state(%p, %d, %p, %p)",
	    hca_hdl, port, sgid_p, base_lid_p);
	mutex_enter(&ibtl_clnt_list_mutex);
	retval = ibtl_get_port_state(hca_hdl->ha_hca_devp, port, sgid_p,
	    base_lid_p);
	mutex_exit(&ibtl_clnt_list_mutex);
	return (retval);
}


/*
 * Function:
 *      ibt_get_port_state_byguid
 * Input:
 *      hca_guid    - The HCA node GUID.
 *	port        - Port number to query.
 * Output:
 *      sgid_p	    - Returned sgid[0], NULL implies no return value.
 *      base_lid_p  - Returned base_lid, NULL implies no return value.
 * Returns:
 *      IBT_SUCCESS
 *	IBT_HCA_PORT_INVALID
 *      IBT_HCA_INVALID
 * Description:
 *      Returns HCA port attributes for one of the HCA ports.
 */
ibt_status_t
ibt_get_port_state_byguid(ib_guid_t hca_guid, uint8_t port,
    ib_gid_t *sgid_p, ib_lid_t *base_lid_p)
{
	ibtl_hca_devinfo_t	*hca_devp;		/* HCA Dev Info */
	ibt_status_t		retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_get_port_state_byguid(%llx, %d, %p, "
	    "%p)", (longlong_t)hca_guid, port, sgid_p, base_lid_p);
	mutex_enter(&ibtl_clnt_list_mutex);
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL)
		retval = IBT_HCA_INVALID;
	else
		retval = ibtl_get_port_state(hca_devp, port, sgid_p,
		    base_lid_p);
	mutex_exit(&ibtl_clnt_list_mutex);
	return (retval);
}


/*
 * Function:
 *      ibt_query_hca_byguid
 * Input:
 *      hca_guid  - The HCA node GUID.
 * Output:
 *      hca_attrs - A pointer to a ibt_hca_attr_t allocated by the caller,
 *                  into which the HCA Attributes are copied.
 * Returns:
 *      IBT_SUCCESS
 *      IBT_INVALID_PARAM
 *      IBT_HCA_INVALID
 * Description:
 *      Returns the static attributes of the specified HCA.
 */
ibt_status_t
ibt_query_hca_byguid(ib_guid_t hca_guid, ibt_hca_attr_t *hca_attrs)
{
	ibtl_hca_devinfo_t	*hca_devp;	/* HCA Dev Info. */

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_query_hca_byguid(%llX)", hca_guid);

	mutex_enter(&ibtl_clnt_list_mutex);
	/* Get HCA Dev Info Structure, referenced by HCA GUID. */
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		/*
		 * If we are here, then the requested HCA device is not present.
		 */
		mutex_exit(&ibtl_clnt_list_mutex);
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_query_hca_byguid: "
		    "Device Not Found");
		return (IBT_HCA_INVALID);
	}

	/* Return back the static HCA attributes */
	bcopy(hca_devp->hd_hca_attr, hca_attrs, sizeof (ibt_hca_attr_t));

	mutex_exit(&ibtl_clnt_list_mutex);

	return (IBT_SUCCESS);
}


/*
 * Function:
 *      ibt_query_hca
 * Input:
 *      hca_hdl   - The HCA handle.
 * Output:
 *      hca_attrs - A pointer to a ibt_hca_attr_t allocated by the caller,
 *                  into which the HCA Attributes are copied.
 * Returns:
 *      IBT_SUCCESS
 *
 * Description:
 *      Returns the static attributes of the specified HCA.
 */
ibt_status_t
ibt_query_hca(ibt_hca_hdl_t hca_hdl, ibt_hca_attr_t *hca_attrs)
{
	IBTF_DPRINTF_L3(ibtf_hca, "ibt_query_hca(%p)", hca_hdl);

	/* Return back the static HCA attributes */
	bcopy(hca_hdl->ha_hca_devp->hd_hca_attr, hca_attrs,
	    sizeof (ibt_hca_attr_t));

	return (IBT_SUCCESS);
}

#define	ROUNDUP(x, y)	((((x)+((y)-1))/(y))*(y))

/*
 * Function:
 *      ibt_query_hca_ports
 * Input:
 *      hca_hdl	    - The HCA handle.
 *	port        - Port number.  If "0", then query ALL Ports.
 * Output:
 *      port_info_p - The address of a pointer to a ibt_hca_portinfo_t struct.
 *      ports_p     - The number of hca ports on the specified HCA.
 *      size_p      - Size of the memory allocated by IBTL to get portinfo,
 *                   to be freed by calling ibt_free_portinfo().
 * Returns:
 *      IBT_SUCCESS
 *      IBT_HCA_HDL_INVALID
 *      IBT_HCA_INVALID
 * Description:
 *      Returns HCA port attributes for either "one", or "all" of the HCA ports.
 */
ibt_status_t
ibt_query_hca_ports(ibt_hca_hdl_t hca_hdl, uint8_t port,
    ibt_hca_portinfo_t **port_info_p, uint_t *ports_p, uint_t *size_p)
{
	ibt_status_t	retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_query_hca_ports(%p, %d)",
	    hca_hdl, port);

	mutex_enter(&ibtl_clnt_list_mutex);

	retval = ibtl_query_hca_ports(hca_hdl->ha_hca_devp, port, port_info_p,
	    ports_p, size_p, 0);

	mutex_exit(&ibtl_clnt_list_mutex);

	return (retval);
}

/*
 * Function:
 *      ibt_query_hca_ports_byguid
 * Input:
 *      hca_guid    - The HCA node GUID.
 *	port        - Port number.  If "0", then query ALL Ports.
 * Output:
 *      port_info_p - The address of a pointer to a ibt_hca_portinfo_t struct.
 *      ports_p     - The number of hca ports on the specified HCA.
 *      size_p      - Size of the memory allocated by IBTL to get portinfo,
 *                   to be freed by calling ibt_free_portinfo().
 * Returns:
 *      IBT_SUCCESS
 *      IBT_HCA_HDL_INVALID
 *      IBT_HCA_INVALID
 * Description:
 *      Returns HCA port attributes for either "one", or "all" of the HCA ports.
 */
ibt_status_t
ibt_query_hca_ports_byguid(ib_guid_t hca_guid, uint8_t port,
    ibt_hca_portinfo_t **port_info_p, uint_t *ports_p, uint_t *size_p)
{
	ibtl_hca_devinfo_t	*hca_devp;	/* HCA Dev Info */
	ibt_status_t		retval;

	mutex_enter(&ibtl_clnt_list_mutex);
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		/*
		 * If we are here, then the requested HCA device is not present.
		 * Return the status as Invalid HCA GUID.
		 */
		*ports_p = *size_p = 0;
		*port_info_p = NULL;
		mutex_exit(&ibtl_clnt_list_mutex);
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_query_hca_ports_byguid: "
		    "HCA Device Not Found. ");
		return (IBT_HCA_INVALID);
	}

	retval = ibtl_query_hca_ports(hca_devp, port, port_info_p, ports_p,
	    size_p, 0);

	mutex_exit(&ibtl_clnt_list_mutex);

	return (retval);
}

/*
 * Define the above function for CM's use that uses the cached copy.
 */
ibt_status_t
ibtl_cm_query_hca_ports_byguid(ib_guid_t hca_guid, uint8_t port,
    ibt_hca_portinfo_t **port_info_p, uint_t *ports_p, uint_t *size_p)
{
	ibtl_hca_devinfo_t	*hca_devp;	/* HCA Dev Info */
	ibt_status_t		retval;

	mutex_enter(&ibtl_clnt_list_mutex);
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		/*
		 * If we are here, then the requested HCA device is not present.
		 * Return the status as Invalid HCA GUID.
		 */
		*ports_p = *size_p = 0;
		*port_info_p = NULL;
		mutex_exit(&ibtl_clnt_list_mutex);
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_query_hca_ports_byguid: "
		    "HCA Device Not Found. ");
		return (IBT_HCA_INVALID);
	}

	retval = ibtl_query_hca_ports(hca_devp, port, port_info_p, ports_p,
	    size_p, 1);

	mutex_exit(&ibtl_clnt_list_mutex);

	return (retval);
}


/*
 * ibtl_query_one_port - fill in portinfo for one port.
 */
static ibt_status_t
ibtl_query_one_port(ibtl_hca_devinfo_t *hca_devp, uint8_t port,
    ibt_hca_portinfo_t **port_info_p, uint_t *ports_p, uint_t *size_p,
    int use_cache)
{
	ibt_hca_portinfo_t	*sp1;	/* src */
	ibt_hca_portinfo_t	*p1;	/* dst */
	caddr_t			p2;
	uint_t			len;
	uint_t			sgid_tbl_len, pkey_tbl_len;

	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));

	IBTF_DPRINTF_L3(ibtf_hca, "ibtl_query_one_port(%p, %d)",
	    hca_devp, port);

	if (port > hca_devp->hd_hca_attr->hca_nports) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibtl_query_one_port: "
		    "invalid port %d", port);
		return (IBT_HCA_PORT_INVALID);
	}

	/* If the PORT_UP event is not supported, we need to query */
	sp1 = hca_devp->hd_portinfop + port - 1;
	if (use_cache == 0)
		ibtl_reinit_hca_portinfo(hca_devp, port);

	*ports_p = 1;

	/*
	 * Calculate how much memory we need for one port, and allocate it.
	 */
	sgid_tbl_len = ROUNDUP(sp1->p_sgid_tbl_sz * sizeof (ib_gid_t),
	    _LONG_LONG_ALIGNMENT);
	pkey_tbl_len = ROUNDUP(sp1->p_pkey_tbl_sz * sizeof (ib_pkey_t),
	    _LONG_LONG_ALIGNMENT);

	len = sizeof (ibt_hca_portinfo_t) + sgid_tbl_len + pkey_tbl_len;
	*size_p = len;

	p1 = kmem_zalloc(len, KM_SLEEP);
	*port_info_p = p1;
	bcopy(sp1, p1, sizeof (ibt_hca_portinfo_t));

	/* initialize the p_pkey_tbl & p_sgid_tbl pointers. */
	p2 = (caddr_t)(p1 + 1);	/* pkeys follow the struct ibt_hca_portinfo_s */
	bcopy(sp1->p_pkey_tbl, p2, pkey_tbl_len);
	p1->p_pkey_tbl = (ib_pkey_t *)p2;

	p2 += pkey_tbl_len;	/* sgids follow the pkeys */
	bcopy(sp1->p_sgid_tbl, p2, sgid_tbl_len);
	p1->p_sgid_tbl = (ib_gid_t *)p2;

	return (IBT_SUCCESS);
}

/*
 * ibtl_query_hca_ports - worker routine to get port_info for clients.
 */
static ibt_status_t
ibtl_query_hca_ports(ibtl_hca_devinfo_t *hca_devp, uint8_t port,
    ibt_hca_portinfo_t **port_info_p, uint_t *ports_p, uint_t *size_p,
    int use_cache)
{
	ibt_hca_portinfo_t	*sp1;	/* src */
	ibt_hca_portinfo_t	*p1;	/* dst */
	uint_t			i, nports;
	caddr_t			p2;
	uint_t			len;
	uint_t			sgid_tbl_len, pkey_tbl_len;

	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));

	/*
	 * If user has specified the port num, then query only that port,
	 * else query all ports.
	 */
	if (port)
		return (ibtl_query_one_port(hca_devp, port, port_info_p,
		    ports_p, size_p, use_cache));

	IBTF_DPRINTF_L3(ibtf_hca, "ibtl_query_hca_ports(%p, ALL)", hca_devp);

	nports = hca_devp->hd_hca_attr->hca_nports;
	*ports_p = nports;

	/* If the PORT_UP event is not supported, we need to query */
	if (use_cache == 0)
		for (i = 0; i < nports; i++)
			ibtl_reinit_hca_portinfo(hca_devp, i + 1);

	sp1 = hca_devp->hd_portinfop;

	/*
	 * Calculate how much memory we need for all ports, and allocate it.
	 */
	sgid_tbl_len = ROUNDUP(sp1->p_sgid_tbl_sz * sizeof (ib_gid_t),
	    _LONG_LONG_ALIGNMENT);
	pkey_tbl_len = ROUNDUP(sp1->p_pkey_tbl_sz * sizeof (ib_pkey_t),
	    _LONG_LONG_ALIGNMENT);

	len = (sizeof (ibt_hca_portinfo_t) + sgid_tbl_len + pkey_tbl_len) *
	    nports;
	*size_p = len;

	ASSERT(len == hca_devp->hd_portinfo_len);

	p1 = kmem_zalloc(len, KM_SLEEP);
	*port_info_p = p1;
	bcopy(sp1, p1, len);	/* start with an exact copy of our cache */

	p2 = (caddr_t)(p1 + nports);

	/* For each port, update the p_pkey_tbl & p_sgid_tbl ptrs. */
	for (i = 0; i < nports; i++) {
		p1->p_pkey_tbl = (ib_pkey_t *)p2;
		p2 += pkey_tbl_len;
		p1->p_sgid_tbl = (ib_gid_t *)p2;
		p2 += sgid_tbl_len;
		p1++;
	}
	return (IBT_SUCCESS);
}

/*
 *	Search for a Full pkey.  Use the pkey at index 0 if not found.
 */
static void
ibtl_set_default_pkey_ix(ibt_hca_portinfo_t *p1)
{
	uint16_t	pkey_ix;

	for (pkey_ix = 0; pkey_ix < p1->p_pkey_tbl_sz; pkey_ix++) {
		if ((p1->p_pkey_tbl[pkey_ix] & 0x8000) &&
		    (p1->p_pkey_tbl[pkey_ix] != IB_PKEY_INVALID_FULL)) {
			p1->p_def_pkey_ix = pkey_ix;
			IBTF_DPRINTF_L3(ibtf_hca,
			    "ibtl_set_default_pkey_ix: portinfop %p, "
			    "FULL PKEY 0x%x found, pkey_ix is %d",
			    p1, p1->p_pkey_tbl[pkey_ix], pkey_ix);
			return;
		}
	}
	IBTF_DPRINTF_L2(ibtf_hca,
	    "ibtl_set_default_pkey_ix: portinfop %p: failed "
	    "to find a default PKEY in the table, using PKey 0x%x",
	    p1, p1->p_pkey_tbl[0]);
	p1->p_def_pkey_ix = 0;
}

/*
 * ibtl_reinit_hca_portinfo - update the portinfo cache for use by IBTL.
 *
 * We have the HCA driver fill in a temporary portinfo, then we bcopy
 * it into our cache while holding the appropriate lock.
 */
void
ibtl_reinit_hca_portinfo(ibtl_hca_devinfo_t *hca_devp, uint8_t port)
{
	ibt_status_t		status;
	ibt_hca_portinfo_t	*p1, *sp1;
	ibt_port_state_t	old_linkstate;
	uint_t			len, sgid_tbl_len, pkey_tbl_len;
	ib_pkey_t		*saved_pkey_tbl;
	ib_gid_t		*saved_sgid_tbl;
	ib_sn_prefix_t		sn_pfx = 0;
	uint_t			multiSM;
	int			i;

	IBTF_DPRINTF_L3(ibtf_hca, "ibtl_reinit_hca_portinfo(%p, %d)",
	    hca_devp, port);

	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));
	ASSERT(port != 0);

	if (ibtl_portinfo_locked(hca_devp, port)) {
		/* we got the lock, so we need to do the portinfo update */

		/* invalidate fast_gid_cache */
		ibtl_fast_gid_cache_valid = B_FALSE;

		p1 = hca_devp->hd_portinfop + port - 1;
		sgid_tbl_len = ROUNDUP(p1->p_sgid_tbl_sz * sizeof (ib_gid_t),
		    _LONG_LONG_ALIGNMENT);
		pkey_tbl_len = ROUNDUP(p1->p_pkey_tbl_sz * sizeof (ib_pkey_t),
		    _LONG_LONG_ALIGNMENT);
		len = sizeof (ibt_hca_portinfo_t) + sgid_tbl_len + pkey_tbl_len;

		/* update was NOT in progress, so we do it here */
		mutex_exit(&ibtl_clnt_list_mutex);

		IBTF_DPRINTF_L3(ibtf_hca, "ibtl_reinit_hca_portinfo(%p, %d): "
		    "calling ibc_query_hca_ports", hca_devp, port);

		sp1 = kmem_zalloc(len, KM_SLEEP);
		sp1->p_pkey_tbl = (ib_pkey_t *)(sp1 + 1);
		sp1->p_sgid_tbl =
		    (ib_gid_t *)((caddr_t)sp1->p_pkey_tbl + pkey_tbl_len);
		status = IBTL_HDIP2CIHCAOPS_P(hca_devp)->ibc_query_hca_ports(
		    IBTL_HDIP2CIHCA(hca_devp), port, sp1);

		mutex_enter(&ibtl_clnt_list_mutex);
		if (status != IBT_SUCCESS) {
			IBTF_DPRINTF_L2(ibtf_hca,
			    "ibtl_reinit_hca_portinfo(%p, %d): "
			    "ibc_query_hca_ports() failed: status = %d",
			    hca_devp, port, status);
		} else {
			old_linkstate = p1->p_linkstate;
			bcopy(sp1->p_pkey_tbl, p1->p_pkey_tbl, pkey_tbl_len);
			bcopy(sp1->p_sgid_tbl, p1->p_sgid_tbl, sgid_tbl_len);
			saved_pkey_tbl = p1->p_pkey_tbl;
			saved_sgid_tbl = p1->p_sgid_tbl;
			bcopy(sp1, p1, sizeof (ibt_hca_portinfo_t));
			p1->p_pkey_tbl = saved_pkey_tbl;
			p1->p_sgid_tbl = saved_sgid_tbl;
			if (p1->p_linkstate == IBT_PORT_ACTIVE) {
				ibtl_set_default_pkey_ix(p1);
				if (p1->p_linkstate != old_linkstate)
					IBTF_DPRINTF_L2(ibtf_hca,
					    "ibtl_reinit_hca_portinfo(%p, %d): "
					    "PORT UP", hca_devp, port);
			} else {
				if (p1->p_linkstate != IBT_PORT_ARM)
					p1->p_base_lid = 0;
				if (p1->p_linkstate != old_linkstate)
					IBTF_DPRINTF_L2(ibtf_hca,
					    "ibtl_reinit_hca_portinfo(%p, %d): "
					    "PORT DOWN", hca_devp, port);
			}
		}
		kmem_free(sp1, len);

		/* Set multism bit accordingly. */
		multiSM = 0;
		p1 = hca_devp->hd_portinfop;
		for (i = 0; i < hca_devp->hd_hca_attr->hca_nports; i++) {
			if (p1->p_linkstate == IBT_PORT_ACTIVE) {
				if (sn_pfx == 0) {
					sn_pfx = p1->p_sgid_tbl[0].gid_prefix;
				} else if (sn_pfx !=
				    p1->p_sgid_tbl[0].gid_prefix) {
					multiSM = 1;
					IBTF_DPRINTF_L3(ibtf_hca,
					    "ibtl_reinit_hca_portinfo: "
					    "MULTI SM, Port1 SnPfx=0x%llX, "
					    "Port2 SnPfx=0x%llX", sn_pfx,
					    p1->p_sgid_tbl[0].gid_prefix);
				}
			}
			p1++;
		}
		hca_devp->hd_multism = multiSM;

		ibtl_portinfo_unlock(hca_devp, port);
	}
}

/*
 * ibtl_init_hca_portinfo - fill in the portinfo cache for use by IBTL.
 */
ibt_status_t
ibtl_init_hca_portinfo(ibtl_hca_devinfo_t *hca_devp)
{
	ibt_hca_portinfo_t	*p1;
	ibt_status_t		retval;
	uint_t			i, nports;
	caddr_t			p2;
	uint_t			len;
	uint_t			sgid_tbl_len, pkey_tbl_len;
	uint_t			sgid_tbl_sz, pkey_tbl_sz;
	ib_sn_prefix_t		sn_pfx = 0;
	uint_t			multiSM;

	IBTF_DPRINTF_L2(ibtf_hca, "ibtl_init_hca_portinfo(%p)", hca_devp);

	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));

	nports = hca_devp->hd_hca_attr->hca_nports;

	/*
	 * Calculate how much memory we need for all ports, and allocate it.
	 */
	pkey_tbl_sz = IBTL_HDIP2PKEYTBLSZ(hca_devp);
	sgid_tbl_sz = IBTL_HDIP2SGIDTBLSZ(hca_devp);
	pkey_tbl_len = ROUNDUP(pkey_tbl_sz * sizeof (ib_pkey_t),
	    _LONG_LONG_ALIGNMENT);
	sgid_tbl_len = ROUNDUP(sgid_tbl_sz * sizeof (ib_gid_t),
	    _LONG_LONG_ALIGNMENT);

	len = (sizeof (ibt_hca_portinfo_t) + sgid_tbl_len + pkey_tbl_len) *
	    nports;

	p1 = kmem_zalloc(len, KM_SLEEP);
	p2 = (caddr_t)(p1 + nports);

	hca_devp->hd_portinfop = p1;
	hca_devp->hd_portinfo_len = len;

	/* For each port initialize the p_pkey_tbl & p_sgid_tbl ptrs. */
	for (i = 0; i < nports; i++) {
		p1->p_pkey_tbl_sz = pkey_tbl_sz;
		p1->p_sgid_tbl_sz = sgid_tbl_sz;
		p1->p_pkey_tbl = (ib_pkey_t *)p2;
		p2 += pkey_tbl_len;
		p1->p_sgid_tbl = (ib_gid_t *)p2;
		p2 += sgid_tbl_len;
		p1++;
	}
	p1 = hca_devp->hd_portinfop;
	mutex_exit(&ibtl_clnt_list_mutex);

	/* re-direct the call to CI's call */
	retval = IBTL_HDIP2CIHCAOPS_P(hca_devp)->ibc_query_hca_ports(
	    IBTL_HDIP2CIHCA(hca_devp), 0, p1);

	mutex_enter(&ibtl_clnt_list_mutex);
	if (retval != IBT_SUCCESS) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibtl_init_hca_portinfo(%p): "
		    "ibc_query_hca_ports() failed: status = %d",
		    hca_devp, retval);
		kmem_free(hca_devp->hd_portinfop, len);
		hca_devp->hd_portinfop = NULL;
		hca_devp->hd_portinfo_len = 0;
		return (retval);
	}

	p1 = hca_devp->hd_portinfop;
	multiSM = 0;
	for (i = 0; i < nports; i++) {
		if (p1->p_linkstate == IBT_PORT_ACTIVE) {
			ibtl_set_default_pkey_ix(p1);
			if (sn_pfx == 0) {
				sn_pfx = p1->p_sgid_tbl[0].gid_prefix;
			} else if (p1->p_sgid_tbl[0].gid_prefix != sn_pfx) {
				multiSM = 1;
				IBTF_DPRINTF_L3(ibtf_hca,
				    "ibtl_init_hca_portinfo: MULTI SM, "
				    "Port1 SnPfx=0x%llX, Port2 SnPfx=0x%llX",
				    sn_pfx, p1->p_sgid_tbl[0].gid_prefix);
			}
		} else {
			if (p1->p_linkstate != IBT_PORT_ARM)
				p1->p_base_lid = 0;
		}
		p1++;
	}
	hca_devp->hd_multism = multiSM;

	return (IBT_SUCCESS);
}

/*
 * Function:
 *	ibt_modify_system_image
 * Input:
 *	hca_hdl	 - The HCA handle.
 *	sys_guid - The New system image GUID.
 * Description:
 *	Modify specified HCA's system image GUID.
 */
ibt_status_t
ibt_modify_system_image(ibt_hca_hdl_t hca_hdl, ib_guid_t sys_guid)
{
	ibt_status_t		retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_modify_system_image(%p, %llX)",
	    hca_hdl, sys_guid);

	mutex_enter(&ibtl_clnt_list_mutex);
	/* Get HCA Dev Info Structure, referenced by HCA GUID. */

	/* re-direct the call to CI's call */
	retval = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_modify_system_image(
	    IBTL_HCA2CIHCA(hca_hdl), sys_guid);

	mutex_exit(&ibtl_clnt_list_mutex);
	return (retval);
}

/*
 * Function:
 *	ibt_modify_system_image_byguid
 *
 * Input:
 *	hca_guid - The HCA Node GUID.
 *	sys_guid - The New system image GUID.
 * Description:
 *	Modify specified HCA's system image GUID.
 */
ibt_status_t
ibt_modify_system_image_byguid(ib_guid_t hca_guid, ib_guid_t sys_guid)
{
	ibtl_hca_devinfo_t	*hca_devp;	/* HCA Dev Info. */
	ibt_status_t		retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_modify_system_image_byguid(%llX, %llX)",
	    hca_guid, sys_guid);

	mutex_enter(&ibtl_clnt_list_mutex);
	/* Get HCA Dev Info Structure, referenced by HCA GUID. */
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		/*
		 * If we are here, then the requested HCA device is not present.
		 */
		mutex_exit(&ibtl_clnt_list_mutex);
		return (IBT_HCA_INVALID);
	}

	/* re-direct the call to CI's call */
	retval = IBTL_HDIP2CIHCAOPS_P(hca_devp)->ibc_modify_system_image(
	    IBTL_HDIP2CIHCA(hca_devp), sys_guid);

	mutex_exit(&ibtl_clnt_list_mutex);
	return (retval);
}

/*
 * Function:
 *      ibt_modify_port_byguid
 * Input:
 *      hca_guid - The HCA Guid.
 *      cmds     - A pointer to an array of ibt_port_modify_t cmds. The
 *                 pmod_port field specifies the port to modify (all ports if 0)
 *                 and the pmod_flags field specifies which attribute to reset.
 *      num_cmds - The number of commands in the cmds array.
 * Output:
 *      none.
 * Returns:
 *      IBT_SUCCESS
 *      IBT_HCA_HDL_INVALID
 *      IBT_HCA_CNTR_INVALID
 *      IBT_HCA_CNTR_VAL_INVALID
 * Description:
 *      Reset the specified port, or all ports attribute(s).
 */
ibt_status_t
ibt_modify_port_byguid(ib_guid_t hca_guid,  uint8_t port,
    ibt_port_modify_flags_t flags, uint8_t init_type)
{
	ibtl_hca_devinfo_t	*hca_devp;	/* HCA Dev Info. */
	ibt_status_t		retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_modify_port_byguid(%llX, %d, %X, %X)",
	    hca_guid, port, flags, init_type);

	mutex_enter(&ibtl_clnt_list_mutex);
	/* Get HCA Dev Info Structure, referenced by HCA GUID. */
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		/*
		 * If we are here, then the requested HCA device is not present.
		 */
		mutex_exit(&ibtl_clnt_list_mutex);
		return (IBT_HCA_INVALID);
	}

	/* re-direct the call to CI's call */
	retval = IBTL_HDIP2CIHCAOPS_P(hca_devp)->ibc_modify_ports(
	    IBTL_HDIP2CIHCA(hca_devp), port, flags, init_type);

	mutex_exit(&ibtl_clnt_list_mutex);
	return (retval);
}

/*
 * Function:
 *      ibt_modify_port
 * Input:
 *      hca_hdl  - The HCA handle.
 *      cmds     - A pointer to an array of ibt_port_modify_t cmds. The
 *                 pmod_port field specifies the port to modify (all ports if 0)
 *                 and the pmod_flags field specifies which attribute to reset.
 *      num_cmds - The number of commands in the cmds array.
 * Output:
 *      none.
 * Returns:
 *      IBT_SUCCESS
 *      IBT_HCA_HDL_INVALID
 *      IBT_HCA_CNTR_INVALID
 *      IBT_HCA_CNTR_VAL_INVALID
 * Description:
 *      Reset the specified port, or all ports attribute(s).
 */
ibt_status_t
ibt_modify_port(ibt_hca_hdl_t hca_hdl, uint8_t port,
    ibt_port_modify_flags_t flags, uint8_t init_type)

{
	ibt_status_t		retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_modify_port(%p, %d, %X, %X)",
	    hca_hdl, port, flags, init_type);

	mutex_enter(&ibtl_clnt_list_mutex);

	/* re-direct the call to CI's call */
	retval = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_modify_ports(
	    IBTL_HCA2CIHCA(hca_hdl), port, flags, init_type);

	mutex_exit(&ibtl_clnt_list_mutex);
	return (retval);
}

/*
 * Function:
 *      ibt_free_portinfo
 * Input:
 *      port_info  - The address of an array to a ibt_hca_portinfo_t struct.
 *	size	   - Memory Size as returned from ibt_query_hca_ports().
 * Output:
 *      none
 * Returns:
 *      none
 * Description:
 *      Frees the memory allocated for a specified ibt_hca_portinfo_t struct.
 */
void
ibt_free_portinfo(ibt_hca_portinfo_t *port_info, uint_t size)
{
	IBTF_DPRINTF_L3(ibtf_hca, "ibt_free_portinfo(%p, %d)",
	    port_info, size);

	if ((port_info == NULL) || (size == 0)) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_free_portinfo: NULL Pointer");
	} else {
		kmem_free(port_info, size);
	}
}


/*
 * Function:
 *      ibt_get_hcadevinfo
 * Input:
 *      hca_guid - The HCA's node GUID.
 * Output:
 *      none.
 * Returns:
 *      Pointer to HCA Device Info structure whose HCA GUID is requested or NULL
 * Description:
 *      Get a pointer to HCA Device Info Structure for the requested HCA GUID.
 *      If no matching HCA GUID Device info is found, NULL is returned.
 */
ibtl_hca_devinfo_t *
ibtl_get_hcadevinfo(ib_guid_t hca_guid)
{
	ibtl_hca_devinfo_t	*hca_devp;	/* HCA Dev Info */

	IBTF_DPRINTF_L3(ibtf_hca, "ibtl_get_hcadevinfo(%llX)", hca_guid);

	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));

	hca_devp = ibtl_hca_list;

	/*
	 * Check whether a HCA device with requested Node GUID is available.
	 * This is done, by searching the global HCA devinfo list and
	 * comparing the Node GUID from the device attribute info.
	 */
	while (hca_devp != NULL) {
		if (hca_devp->hd_hca_attr->hca_node_guid == hca_guid) {
			/* Match Found. */
			break;
		}
		hca_devp = hca_devp->hd_hca_dev_link;
	}
	return (hca_devp);
}


/*
 * Function:
 *      ibtl_pkey2index
 * Input:
 *      hca_devp     - The IBTL HCA Device Info.
 *      port_num     - The HCA port number.
 *      pkey         - The input PKey value, whose index we are interested in.
 * Output:
 *      pkey_ix      - The PKey index returned for the specified PKey.
 * Returns:
 *      IBT_SUCCESS/IBT_HCA_PORT_INVALID/IBT_INVALID_PARAM
 * Description:
 *      Returns the PKey Index for the specified PKey, the device as specified
 *      by IBT HCA Handle.
 */
static ibt_status_t
ibtl_pkey2index(ibtl_hca_devinfo_t *hca_devp, uint8_t port_num,
    ib_pkey_t pkey, uint16_t *pkey_ix)
{
	ibt_hca_portinfo_t 	*port_infop;
	uint_t			ports;
	uint_t			i;

	IBTF_DPRINTF_L3(ibtf_hca, "ibtl_pkey2index(%p, %d, %d)",
	    hca_devp, port_num, pkey);

	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));

	if ((pkey == IB_PKEY_INVALID_FULL) ||
	    (pkey == IB_PKEY_INVALID_LIMITED))
		return (IBT_INVALID_PARAM);

	ports = hca_devp->hd_hca_attr->hca_nports;
	if ((port_num == 0) || (port_num > ports)) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibtl_pkey2index: "
		    "Invalid port_num %d, range is (1 to %d)", port_num, ports);
		return (IBT_HCA_PORT_INVALID);
	}

	port_infop = hca_devp->hd_portinfop + port_num - 1;
	for (i = 0; i < port_infop->p_pkey_tbl_sz; i++) {
		if (pkey == port_infop->p_pkey_tbl[i]) {
			*pkey_ix = i;
			return (IBT_SUCCESS);
		}
	}
	return (IBT_INVALID_PARAM);
}

/*
 * Function:
 *      ibtl_index2pkey
 * Input:
 *      hca_devp     - The IBTL HCA Device Info.
 *      port_num     - The HCA port
 *      pkey_ix      - The input PKey index, whose PKey we are interested in.
 * Output:
 *      pkey         - The returned PKey value.
 * Returns:
 *      IBT_SUCCESS/IBT_PKEY_IX_ILLEGAL/IBT_PKEY_IX_INVALID/IBT_HCA_PORT_INVALID
 * Description:
 *      Returns the PKey value for the specified PKey index, the device as
 *      specified by IBT HCA Handle.
 */
static ibt_status_t
ibtl_index2pkey(ibtl_hca_devinfo_t *hca_devp, uint8_t port_num,
    uint16_t pkey_ix, ib_pkey_t *pkey)
{
	ibt_hca_portinfo_t 	*port_infop;
	uint_t			ports;

	IBTF_DPRINTF_L3(ibtf_hca, "ibtl_index2pkey(%p, %d, %d)",
	    hca_devp, port_num, pkey_ix);

	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));

	ports = hca_devp->hd_hca_attr->hca_nports;
	if ((port_num == 0) || (port_num > ports)) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibtl_index2pkey: "
		    "Invalid port_num %d, range is (1 to %d)", port_num, ports);
		return (IBT_HCA_PORT_INVALID);
	}

	port_infop = hca_devp->hd_portinfop + port_num - 1;
	if (pkey_ix >= port_infop->p_pkey_tbl_sz) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibtl_index2pkey: "
		    "pkey index %d out of range (0, %d)",
		    pkey_ix, port_infop->p_pkey_tbl_sz - 1);
		return (IBT_PKEY_IX_ILLEGAL);
	}

	*pkey = port_infop->p_pkey_tbl[pkey_ix];
	if ((*pkey == IB_PKEY_INVALID_FULL) ||
	    (*pkey == IB_PKEY_INVALID_LIMITED))
		return (IBT_PKEY_IX_INVALID);
	return (IBT_SUCCESS);
}

/*
 * Function:
 *      ibt_pkey2index
 * Input:
 *      hca_hdl      - The IBT HCA handle.
 *      port_num     - The HCA port number.
 *      pkey         - The input PKey value, whose index we are interested in.
 * Output:
 *      pkey_ix      - The PKey index returned for the specified PKey.
 * Returns:
 *      IBT_SUCCESS/IBT_HCA_PORT_INVALID/IBT_INVALID_PARAM
 * Description:
 *      Returns the PKey Index for the specified PKey, the device as specified
 *      by IBT HCA Handle.
 */
ibt_status_t
ibt_pkey2index(ibt_hca_hdl_t hca_hdl, uint8_t port_num, ib_pkey_t pkey,
    uint16_t *pkey_ix)
{
	ibt_status_t		retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_pkey2index(%p, %d, %d)",
	    hca_hdl, port_num, pkey);

	mutex_enter(&ibtl_clnt_list_mutex);
	retval = ibtl_pkey2index(hca_hdl->ha_hca_devp, port_num, pkey, pkey_ix);
	mutex_exit(&ibtl_clnt_list_mutex);

	return (retval);
}

/*
 * Function:
 *      ibt_pkey2index_byguid
 * Input:
 *      hca_guid     - The HCA's node GUID.
 *      port_num     - The HCA port number.
 *      pkey         - The input PKey value, whose index we are interested in.
 * Output:
 *      pkey_ix      - The PKey Index returned for the specified PKey.
 * Returns:
 *      IBT_SUCCESS/IBT_HCA_PORT_INVALID/IBT_INVALID_PARAM/IBT_HCA_INVALID
 * Description:
 *      Returns the PKey Index for the specified PKey, the device as specified
 *      by HCA GUID Info.
 */
ibt_status_t
ibt_pkey2index_byguid(ib_guid_t hca_guid, uint8_t port_num, ib_pkey_t pkey,
    uint16_t *pkey_ix)
{
	ibt_status_t		retval;
	ibtl_hca_devinfo_t	*hca_devp;	/* HCA Dev Info */

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_pkey2index_byguid(%llX, %d, %d)",
	    hca_guid, port_num, pkey);

	mutex_enter(&ibtl_clnt_list_mutex);
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_pkey2index_byguid: "
		    "Invalid HCA GUID 0x%llx", hca_guid);
		mutex_exit(&ibtl_clnt_list_mutex);
		return (IBT_HCA_INVALID);
	}
	retval = ibtl_pkey2index(hca_devp, port_num, pkey, pkey_ix);
	mutex_exit(&ibtl_clnt_list_mutex);

	return (retval);
}


/*
 * Function:
 *      ibt_index2pkey
 * Input:
 *      hca_hdl      - The IBT HCA handle.
 *      port_num     - The HCA port
 *      pkey_ix      - The input PKey index, whose PKey we are interested in.
 * Output:
 *      pkey         - The returned PKey value.
 * Returns:
 *      IBT_SUCCESS/IBT_PKEY_IX_ILLEGAL/IBT_PKEY_IX_INVALID/IBT_HCA_PORT_INVALID
 * Description:
 *      Returns the PKey value for the specified PKey index, the device as
 *      specified by IBT HCA Handle.
 */
ibt_status_t
ibt_index2pkey(ibt_hca_hdl_t hca_hdl, uint8_t port_num, uint16_t pkey_ix,
    ib_pkey_t *pkey)
{
	ibt_status_t		retval;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_index2pkey(%p, %d, %d)",
	    hca_hdl, port_num, pkey_ix);

	mutex_enter(&ibtl_clnt_list_mutex);
	retval = ibtl_index2pkey(hca_hdl->ha_hca_devp, port_num, pkey_ix, pkey);
	mutex_exit(&ibtl_clnt_list_mutex);

	return (retval);
}

/*
 * Function:
 *      ibt_index2pkey_byguid
 * Input:
 *      hca_guid     - The HCA's node GUID.
 *      port_num     - The HCA port
 *      pkey_ix      - The input PKey index, whose PKey we are interested in.
 * Output:
 *      pkey         - The returned PKey value, for the specified index.
 * Returns:
 *      IBT_SUCCESS/IBT_PKEY_IX_ILLEGAL/IBT_PKEY_IX_INVALID/
 *	IBT_HCA_PORT_INVALID/IBT_HCA_INVALID
 * Description:
 *      Returns the PKey Index for the specified PKey, the device as specified
 *      by HCA GUID Info.
 */
ibt_status_t
ibt_index2pkey_byguid(ib_guid_t hca_guid, uint8_t port_num, uint16_t pkey_ix,
    ib_pkey_t *pkey)
{
	ibt_status_t		retval;
	ibtl_hca_devinfo_t	*hca_devp;	/* HCA Dev Info */

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_index2pkey_byguid(%llX, %d, %d)",
	    hca_guid, port_num, pkey_ix);

	mutex_enter(&ibtl_clnt_list_mutex);
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		IBTF_DPRINTF_L2(ibtf_hca, "ibt_index2pkey_byguid: "
		    "Invalid HCA GUID 0x%llx", hca_guid);
		mutex_exit(&ibtl_clnt_list_mutex);
		return (IBT_HCA_INVALID);
	}
	retval = ibtl_index2pkey(hca_devp, port_num, pkey_ix, pkey);
	mutex_exit(&ibtl_clnt_list_mutex);

	return (retval);
}


_NOTE(SCHEME_PROTECTS_DATA("client managed", ibtl_hca_s::ha_clnt_private))

/*
 * Function:
 *      ibt_set_hca_private
 * Input:
 *      hca_hdl		The ibt_hca_hdl_t of the opened HCA.
 *      clnt_private	The client private data.
 * Output:
 *	none.
 * Returns:
 *      none
 * Description:
 *      Sets the client private data.
 */
void
ibt_set_hca_private(ibt_hca_hdl_t hca_hdl, void *clnt_private)
{
	hca_hdl->ha_clnt_private = clnt_private;
}


/*
 * Function:
 *      ibt_get_hca_private
 * Input:
 *      hca_hdl		The ibt_hca_hdl_t of the opened HCA.
 * Output:
 *      none
 * Returns:
 *      The client private data.
 * Description:
 *      Retrieves the private data from a specified HCA.
 */
void *
ibt_get_hca_private(ibt_hca_hdl_t hca_hdl)
{
	return (hca_hdl->ha_clnt_private);
}

/*
 * Function:
 *	ibt_hca_handle_to_guid
 * Input:
 *	hca		HCA Handle.
 * Output:
 *	none.
 * Returns:
 *	hca_guid	Returned HCA GUID on which the specified Channel is
 *			allocated. Valid if it is non-NULL on return.
 * Description:
 *	A helper function to retrieve HCA GUID for the specified handle.
 */
ib_guid_t
ibt_hca_handle_to_guid(ibt_hca_hdl_t hca)
{
	IBTF_DPRINTF_L3(ibtf_hca, "ibt_hca_handle_to_guid(%p)", hca);
	return (IBTL_HCA2HCAGUID(hca));
}

/*
 * Function:
 *	ibt_hca_guid_to_handle
 * Input:
 *	ibt_hdl		The handle returned to the client by the IBTF from
 *                      an ibt_attach() call.
 *	hca_guid	HCA GUID
 * Output:
 *	hca_hdl		Returned ibt_hca_hdl_t.
 * Returns:
 *      IBT_SUCCESS
 *      IBT_HCA_INVALID
 * Description:
 *	A helper function to retrieve a hca handle from a HCA GUID.
 */
ibt_status_t
ibt_hca_guid_to_handle(ibt_clnt_hdl_t ibt_hdl, ib_guid_t hca_guid,
    ibt_hca_hdl_t *hca_hdl)
{
	ibtl_hca_t  		*hca_infop;
	ibtl_hca_devinfo_t	*hca_devp;		/* HCA Dev Info */
	ibt_status_t		rval = IBT_HCA_INVALID;

	IBTF_DPRINTF_L3(ibtf_hca, "ibt_hca_guid_to_handle(%p, %llX)",
	    ibt_hdl, hca_guid);

	mutex_enter(&ibtl_clnt_list_mutex);

	/*
	 * Get HCA Device Info Structure, referenced by HCA GUID.
	 */
	hca_devp = ibtl_get_hcadevinfo(hca_guid);
	if (hca_devp == NULL) {
		/*
		 * If we are here, then the requested HCA device is not present.
		 * Return the status as Invalid HCA GUID.
		 */
		mutex_exit(&ibtl_clnt_list_mutex);

		IBTF_DPRINTF_L2(ibtf_hca, "ibt_hca_guid_to_handle: "
		    "HCA Device Not Found: Invalid HCA GUID");

		*hca_hdl = NULL;
		return (rval);
	}

	/*
	 * Yes, we found a HCA Device registered with IBTF, which matches with
	 * the requested HCA_GUID.
	 */
	hca_infop = hca_devp->hd_clnt_list;

	while (hca_infop != NULL) {
		if (ibt_hdl == hca_infop->ha_clnt_devp) {
			rval = IBT_SUCCESS;
			break;
		}
		hca_infop = hca_infop->ha_clnt_link;
	}

	mutex_exit(&ibtl_clnt_list_mutex);
	*hca_hdl = hca_infop;
	return (rval);
}