Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
/*******************************************************************************
*
* Copyright (c) 2015-2016 Intel Corporation.  All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses.  You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenFabrics.org BSD license below:
*
*   Redistribution and use in source and binary forms, with or
*   without modification, are permitted provided that the following
*   conditions are met:
*
*    - Redistributions of source code must retain the above
*	copyright notice, this list of conditions and the following
*	disclaimer.
*
*    - Redistributions in binary form must reproduce the above
*	copyright notice, this list of conditions and the following
*	disclaimer in the documentation and/or other materials
*	provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/if_vlan.h>
#include <net/addrconf.h>

#include "i40iw.h"
#include "i40iw_register.h"
#include <net/netevent.h>
#define CLIENT_IW_INTERFACE_VERSION_MAJOR 0
#define CLIENT_IW_INTERFACE_VERSION_MINOR 01
#define CLIENT_IW_INTERFACE_VERSION_BUILD 00

#define DRV_VERSION_MAJOR 0
#define DRV_VERSION_MINOR 5
#define DRV_VERSION_BUILD 123
#define DRV_VERSION	__stringify(DRV_VERSION_MAJOR) "."		\
	__stringify(DRV_VERSION_MINOR) "." __stringify(DRV_VERSION_BUILD)

static int push_mode;
module_param(push_mode, int, 0644);
MODULE_PARM_DESC(push_mode, "Low latency mode: 0=disabled (default), 1=enabled)");

static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "debug flags: 0=disabled (default), 0x7fffffff=all");

static int resource_profile;
module_param(resource_profile, int, 0644);
MODULE_PARM_DESC(resource_profile,
		 "Resource Profile: 0=no VF RDMA support (default), 1=Weighted VF, 2=Even Distribution");

static int max_rdma_vfs = 32;
module_param(max_rdma_vfs, int, 0644);
MODULE_PARM_DESC(max_rdma_vfs, "Maximum VF count: 0-32 32=default");
static int mpa_version = 2;
module_param(mpa_version, int, 0644);
MODULE_PARM_DESC(mpa_version, "MPA version to be used in MPA Req/Resp 1 or 2");

MODULE_AUTHOR("Intel Corporation, <e1000-rdma@lists.sourceforge.net>");
MODULE_DESCRIPTION("Intel(R) Ethernet Connection X722 iWARP RDMA Driver");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION(DRV_VERSION);

static struct i40e_client i40iw_client;
static char i40iw_client_name[I40E_CLIENT_STR_LENGTH] = "i40iw";

static LIST_HEAD(i40iw_handlers);
static spinlock_t i40iw_handler_lock;

static enum i40iw_status_code i40iw_virtchnl_send(struct i40iw_sc_dev *dev,
						  u32 vf_id, u8 *msg, u16 len);

static struct notifier_block i40iw_inetaddr_notifier = {
	.notifier_call = i40iw_inetaddr_event
};

static struct notifier_block i40iw_inetaddr6_notifier = {
	.notifier_call = i40iw_inet6addr_event
};

static struct notifier_block i40iw_net_notifier = {
	.notifier_call = i40iw_net_event
};

static atomic_t i40iw_notifiers_registered;

/**
 * i40iw_find_i40e_handler - find a handler given a client info
 * @ldev: pointer to a client info
 */
static struct i40iw_handler *i40iw_find_i40e_handler(struct i40e_info *ldev)
{
	struct i40iw_handler *hdl;
	unsigned long flags;

	spin_lock_irqsave(&i40iw_handler_lock, flags);
	list_for_each_entry(hdl, &i40iw_handlers, list) {
		if (hdl->ldev.netdev == ldev->netdev) {
			spin_unlock_irqrestore(&i40iw_handler_lock, flags);
			return hdl;
		}
	}
	spin_unlock_irqrestore(&i40iw_handler_lock, flags);
	return NULL;
}

/**
 * i40iw_find_netdev - find a handler given a netdev
 * @netdev: pointer to net_device
 */
struct i40iw_handler *i40iw_find_netdev(struct net_device *netdev)
{
	struct i40iw_handler *hdl;
	unsigned long flags;

	spin_lock_irqsave(&i40iw_handler_lock, flags);
	list_for_each_entry(hdl, &i40iw_handlers, list) {
		if (hdl->ldev.netdev == netdev) {
			spin_unlock_irqrestore(&i40iw_handler_lock, flags);
			return hdl;
		}
	}
	spin_unlock_irqrestore(&i40iw_handler_lock, flags);
	return NULL;
}

/**
 * i40iw_add_handler - add a handler to the list
 * @hdl: handler to be added to the handler list
 */
static void i40iw_add_handler(struct i40iw_handler *hdl)
{
	unsigned long flags;

	spin_lock_irqsave(&i40iw_handler_lock, flags);
	list_add(&hdl->list, &i40iw_handlers);
	spin_unlock_irqrestore(&i40iw_handler_lock, flags);
}

/**
 * i40iw_del_handler - delete a handler from the list
 * @hdl: handler to be deleted from the handler list
 */
static int i40iw_del_handler(struct i40iw_handler *hdl)
{
	unsigned long flags;

	spin_lock_irqsave(&i40iw_handler_lock, flags);
	list_del(&hdl->list);
	spin_unlock_irqrestore(&i40iw_handler_lock, flags);
	return 0;
}

/**
 * i40iw_enable_intr - set up device interrupts
 * @dev: hardware control device structure
 * @msix_id: id of the interrupt to be enabled
 */
static void i40iw_enable_intr(struct i40iw_sc_dev *dev, u32 msix_id)
{
	u32 val;

	val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
		I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
		(3 << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
	if (dev->is_pf)
		i40iw_wr32(dev->hw, I40E_PFINT_DYN_CTLN(msix_id - 1), val);
	else
		i40iw_wr32(dev->hw, I40E_VFINT_DYN_CTLN1(msix_id - 1), val);
}

/**
 * i40iw_dpc - tasklet for aeq and ceq 0
 * @data: iwarp device
 */
static void i40iw_dpc(unsigned long data)
{
	struct i40iw_device *iwdev = (struct i40iw_device *)data;

	if (iwdev->msix_shared)
		i40iw_process_ceq(iwdev, iwdev->ceqlist);
	i40iw_process_aeq(iwdev);
	i40iw_enable_intr(&iwdev->sc_dev, iwdev->iw_msixtbl[0].idx);
}

/**
 * i40iw_ceq_dpc - dpc handler for CEQ
 * @data: data points to CEQ
 */
static void i40iw_ceq_dpc(unsigned long data)
{
	struct i40iw_ceq *iwceq = (struct i40iw_ceq *)data;
	struct i40iw_device *iwdev = iwceq->iwdev;

	i40iw_process_ceq(iwdev, iwceq);
	i40iw_enable_intr(&iwdev->sc_dev, iwceq->msix_idx);
}

/**
 * i40iw_irq_handler - interrupt handler for aeq and ceq0
 * @irq: Interrupt request number
 * @data: iwarp device
 */
static irqreturn_t i40iw_irq_handler(int irq, void *data)
{
	struct i40iw_device *iwdev = (struct i40iw_device *)data;

	tasklet_schedule(&iwdev->dpc_tasklet);
	return IRQ_HANDLED;
}

/**
 * i40iw_destroy_cqp  - destroy control qp
 * @iwdev: iwarp device
 * @create_done: 1 if cqp create poll was success
 *
 * Issue destroy cqp request and
 * free the resources associated with the cqp
 */
static void i40iw_destroy_cqp(struct i40iw_device *iwdev, bool free_hwcqp)
{
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_cqp *cqp = &iwdev->cqp;

	if (free_hwcqp)
		dev->cqp_ops->cqp_destroy(dev->cqp);

	i40iw_free_dma_mem(dev->hw, &cqp->sq);
	kfree(cqp->scratch_array);
	iwdev->cqp.scratch_array = NULL;

	kfree(cqp->cqp_requests);
	cqp->cqp_requests = NULL;
}

/**
 * i40iw_disable_irqs - disable device interrupts
 * @dev: hardware control device structure
 * @msic_vec: msix vector to disable irq
 * @dev_id: parameter to pass to free_irq (used during irq setup)
 *
 * The function is called when destroying aeq/ceq
 */
static void i40iw_disable_irq(struct i40iw_sc_dev *dev,
			      struct i40iw_msix_vector *msix_vec,
			      void *dev_id)
{
	if (dev->is_pf)
		i40iw_wr32(dev->hw, I40E_PFINT_DYN_CTLN(msix_vec->idx - 1), 0);
	else
		i40iw_wr32(dev->hw, I40E_VFINT_DYN_CTLN1(msix_vec->idx - 1), 0);
	irq_set_affinity_hint(msix_vec->irq, NULL);
	free_irq(msix_vec->irq, dev_id);
}

/**
 * i40iw_destroy_aeq - destroy aeq
 * @iwdev: iwarp device
 * @reset: true if called before reset
 *
 * Issue a destroy aeq request and
 * free the resources associated with the aeq
 * The function is called during driver unload
 */
static void i40iw_destroy_aeq(struct i40iw_device *iwdev, bool reset)
{
	enum i40iw_status_code status = I40IW_ERR_NOT_READY;
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_aeq *aeq = &iwdev->aeq;

	if (!iwdev->msix_shared)
		i40iw_disable_irq(dev, iwdev->iw_msixtbl, (void *)iwdev);
	if (reset)
		goto exit;

	if (!dev->aeq_ops->aeq_destroy(&aeq->sc_aeq, 0, 1))
		status = dev->aeq_ops->aeq_destroy_done(&aeq->sc_aeq);
	if (status)
		i40iw_pr_err("destroy aeq failed %d\n", status);

exit:
	i40iw_free_dma_mem(dev->hw, &aeq->mem);
}

/**
 * i40iw_destroy_ceq - destroy ceq
 * @iwdev: iwarp device
 * @iwceq: ceq to be destroyed
 * @reset: true if called before reset
 *
 * Issue a destroy ceq request and
 * free the resources associated with the ceq
 */
static void i40iw_destroy_ceq(struct i40iw_device *iwdev,
			      struct i40iw_ceq *iwceq,
			      bool reset)
{
	enum i40iw_status_code status;
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;

	if (reset)
		goto exit;

	status = dev->ceq_ops->ceq_destroy(&iwceq->sc_ceq, 0, 1);
	if (status) {
		i40iw_pr_err("ceq destroy command failed %d\n", status);
		goto exit;
	}

	status = dev->ceq_ops->cceq_destroy_done(&iwceq->sc_ceq);
	if (status)
		i40iw_pr_err("ceq destroy completion failed %d\n", status);
exit:
	i40iw_free_dma_mem(dev->hw, &iwceq->mem);
}

/**
 * i40iw_dele_ceqs - destroy all ceq's
 * @iwdev: iwarp device
 * @reset: true if called before reset
 *
 * Go through all of the device ceq's and for each ceq
 * disable the ceq interrupt and destroy the ceq
 */
static void i40iw_dele_ceqs(struct i40iw_device *iwdev, bool reset)
{
	u32 i = 0;
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_ceq *iwceq = iwdev->ceqlist;
	struct i40iw_msix_vector *msix_vec = iwdev->iw_msixtbl;

	if (iwdev->msix_shared) {
		i40iw_disable_irq(dev, msix_vec, (void *)iwdev);
		i40iw_destroy_ceq(iwdev, iwceq, reset);
		iwceq++;
		i++;
	}

	for (msix_vec++; i < iwdev->ceqs_count; i++, msix_vec++, iwceq++) {
		i40iw_disable_irq(dev, msix_vec, (void *)iwceq);
		i40iw_destroy_ceq(iwdev, iwceq, reset);
	}
}

/**
 * i40iw_destroy_ccq - destroy control cq
 * @iwdev: iwarp device
 * @reset: true if called before reset
 *
 * Issue destroy ccq request and
 * free the resources associated with the ccq
 */
static void i40iw_destroy_ccq(struct i40iw_device *iwdev, bool reset)
{
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_ccq *ccq = &iwdev->ccq;
	enum i40iw_status_code status = 0;

	if (!reset)
		status = dev->ccq_ops->ccq_destroy(dev->ccq, 0, true);
	if (status)
		i40iw_pr_err("ccq destroy failed %d\n", status);
	i40iw_free_dma_mem(dev->hw, &ccq->mem_cq);
}

/* types of hmc objects */
static enum i40iw_hmc_rsrc_type iw_hmc_obj_types[] = {
	I40IW_HMC_IW_QP,
	I40IW_HMC_IW_CQ,
	I40IW_HMC_IW_HTE,
	I40IW_HMC_IW_ARP,
	I40IW_HMC_IW_APBVT_ENTRY,
	I40IW_HMC_IW_MR,
	I40IW_HMC_IW_XF,
	I40IW_HMC_IW_XFFL,
	I40IW_HMC_IW_Q1,
	I40IW_HMC_IW_Q1FL,
	I40IW_HMC_IW_TIMER,
};

/**
 * i40iw_close_hmc_objects_type - delete hmc objects of a given type
 * @iwdev: iwarp device
 * @obj_type: the hmc object type to be deleted
 * @is_pf: true if the function is PF otherwise false
 * @reset: true if called before reset
 */
static void i40iw_close_hmc_objects_type(struct i40iw_sc_dev *dev,
					 enum i40iw_hmc_rsrc_type obj_type,
					 struct i40iw_hmc_info *hmc_info,
					 bool is_pf,
					 bool reset)
{
	struct i40iw_hmc_del_obj_info info;

	memset(&info, 0, sizeof(info));
	info.hmc_info = hmc_info;
	info.rsrc_type = obj_type;
	info.count = hmc_info->hmc_obj[obj_type].cnt;
	info.is_pf = is_pf;
	if (dev->hmc_ops->del_hmc_object(dev, &info, reset))
		i40iw_pr_err("del obj of type %d failed\n", obj_type);
}

/**
 * i40iw_del_hmc_objects - remove all device hmc objects
 * @dev: iwarp device
 * @hmc_info: hmc_info to free
 * @is_pf: true if hmc_info belongs to PF, not vf nor allocated
 *	   by PF on behalf of VF
 * @reset: true if called before reset
 */
static void i40iw_del_hmc_objects(struct i40iw_sc_dev *dev,
				  struct i40iw_hmc_info *hmc_info,
				  bool is_pf,
				  bool reset)
{
	unsigned int i;

	for (i = 0; i < IW_HMC_OBJ_TYPE_NUM; i++)
		i40iw_close_hmc_objects_type(dev, iw_hmc_obj_types[i], hmc_info, is_pf, reset);
}

/**
 * i40iw_ceq_handler - interrupt handler for ceq
 * @data: ceq pointer
 */
static irqreturn_t i40iw_ceq_handler(int irq, void *data)
{
	struct i40iw_ceq *iwceq = (struct i40iw_ceq *)data;

	if (iwceq->irq != irq)
		i40iw_pr_err("expected irq = %d received irq = %d\n", iwceq->irq, irq);
	tasklet_schedule(&iwceq->dpc_tasklet);
	return IRQ_HANDLED;
}

/**
 * i40iw_create_hmc_obj_type - create hmc object of a given type
 * @dev: hardware control device structure
 * @info: information for the hmc object to create
 */
static enum i40iw_status_code i40iw_create_hmc_obj_type(struct i40iw_sc_dev *dev,
							struct i40iw_hmc_create_obj_info *info)
{
	return dev->hmc_ops->create_hmc_object(dev, info);
}

/**
 * i40iw_create_hmc_objs - create all hmc objects for the device
 * @iwdev: iwarp device
 * @is_pf: true if the function is PF otherwise false
 *
 * Create the device hmc objects and allocate hmc pages
 * Return 0 if successful, otherwise clean up and return error
 */
static enum i40iw_status_code i40iw_create_hmc_objs(struct i40iw_device *iwdev,
						    bool is_pf)
{
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_hmc_create_obj_info info;
	enum i40iw_status_code status;
	int i;

	memset(&info, 0, sizeof(info));
	info.hmc_info = dev->hmc_info;
	info.is_pf = is_pf;
	info.entry_type = iwdev->sd_type;
	for (i = 0; i < IW_HMC_OBJ_TYPE_NUM; i++) {
		info.rsrc_type = iw_hmc_obj_types[i];
		info.count = dev->hmc_info->hmc_obj[info.rsrc_type].cnt;
		status = i40iw_create_hmc_obj_type(dev, &info);
		if (status) {
			i40iw_pr_err("create obj type %d status = %d\n",
				     iw_hmc_obj_types[i], status);
			break;
		}
	}
	if (!status)
		return (dev->cqp_misc_ops->static_hmc_pages_allocated(dev->cqp, 0,
								      dev->hmc_fn_id,
								      true, true));

	while (i) {
		i--;
		/* destroy the hmc objects of a given type */
		i40iw_close_hmc_objects_type(dev,
					     iw_hmc_obj_types[i],
					     dev->hmc_info,
					     is_pf,
					     false);
	}
	return status;
}

/**
 * i40iw_obj_aligned_mem - get aligned memory from device allocated memory
 * @iwdev: iwarp device
 * @memptr: points to the memory addresses
 * @size: size of memory needed
 * @mask: mask for the aligned memory
 *
 * Get aligned memory of the requested size and
 * update the memptr to point to the new aligned memory
 * Return 0 if successful, otherwise return no memory error
 */
enum i40iw_status_code i40iw_obj_aligned_mem(struct i40iw_device *iwdev,
					     struct i40iw_dma_mem *memptr,
					     u32 size,
					     u32 mask)
{
	unsigned long va, newva;
	unsigned long extra;

	va = (unsigned long)iwdev->obj_next.va;
	newva = va;
	if (mask)
		newva = ALIGN(va, (mask + 1));
	extra = newva - va;
	memptr->va = (u8 *)va + extra;
	memptr->pa = iwdev->obj_next.pa + extra;
	memptr->size = size;
	if ((memptr->va + size) > (iwdev->obj_mem.va + iwdev->obj_mem.size))
		return I40IW_ERR_NO_MEMORY;

	iwdev->obj_next.va = memptr->va + size;
	iwdev->obj_next.pa = memptr->pa + size;
	return 0;
}

/**
 * i40iw_create_cqp - create control qp
 * @iwdev: iwarp device
 *
 * Return 0, if the cqp and all the resources associated with it
 * are successfully created, otherwise return error
 */
static enum i40iw_status_code i40iw_create_cqp(struct i40iw_device *iwdev)
{
	enum i40iw_status_code status;
	u32 sqsize = I40IW_CQP_SW_SQSIZE_2048;
	struct i40iw_dma_mem mem;
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_cqp_init_info cqp_init_info;
	struct i40iw_cqp *cqp = &iwdev->cqp;
	u16 maj_err, min_err;
	int i;

	cqp->cqp_requests = kcalloc(sqsize, sizeof(*cqp->cqp_requests), GFP_KERNEL);
	if (!cqp->cqp_requests)
		return I40IW_ERR_NO_MEMORY;
	cqp->scratch_array = kcalloc(sqsize, sizeof(*cqp->scratch_array), GFP_KERNEL);
	if (!cqp->scratch_array) {
		kfree(cqp->cqp_requests);
		return I40IW_ERR_NO_MEMORY;
	}
	dev->cqp = &cqp->sc_cqp;
	dev->cqp->dev = dev;
	memset(&cqp_init_info, 0, sizeof(cqp_init_info));
	status = i40iw_allocate_dma_mem(dev->hw, &cqp->sq,
					(sizeof(struct i40iw_cqp_sq_wqe) * sqsize),
					I40IW_CQP_ALIGNMENT);
	if (status)
		goto exit;
	status = i40iw_obj_aligned_mem(iwdev, &mem, sizeof(struct i40iw_cqp_ctx),
				       I40IW_HOST_CTX_ALIGNMENT_MASK);
	if (status)
		goto exit;
	dev->cqp->host_ctx_pa = mem.pa;
	dev->cqp->host_ctx = mem.va;
	/* populate the cqp init info */
	cqp_init_info.dev = dev;
	cqp_init_info.sq_size = sqsize;
	cqp_init_info.sq = cqp->sq.va;
	cqp_init_info.sq_pa = cqp->sq.pa;
	cqp_init_info.host_ctx_pa = mem.pa;
	cqp_init_info.host_ctx = mem.va;
	cqp_init_info.hmc_profile = iwdev->resource_profile;
	cqp_init_info.enabled_vf_count = iwdev->max_rdma_vfs;
	cqp_init_info.scratch_array = cqp->scratch_array;
	status = dev->cqp_ops->cqp_init(dev->cqp, &cqp_init_info);
	if (status) {
		i40iw_pr_err("cqp init status %d\n", status);
		goto exit;
	}
	status = dev->cqp_ops->cqp_create(dev->cqp, &maj_err, &min_err);
	if (status) {
		i40iw_pr_err("cqp create status %d maj_err %d min_err %d\n",
			     status, maj_err, min_err);
		goto exit;
	}
	spin_lock_init(&cqp->req_lock);
	INIT_LIST_HEAD(&cqp->cqp_avail_reqs);
	INIT_LIST_HEAD(&cqp->cqp_pending_reqs);
	/* init the waitq of the cqp_requests and add them to the list */
	for (i = 0; i < I40IW_CQP_SW_SQSIZE_2048; i++) {
		init_waitqueue_head(&cqp->cqp_requests[i].waitq);
		list_add_tail(&cqp->cqp_requests[i].list, &cqp->cqp_avail_reqs);
	}
	return 0;
exit:
	/* clean up the created resources */
	i40iw_destroy_cqp(iwdev, false);
	return status;
}

/**
 * i40iw_create_ccq - create control cq
 * @iwdev: iwarp device
 *
 * Return 0, if the ccq and the resources associated with it
 * are successfully created, otherwise return error
 */
static enum i40iw_status_code i40iw_create_ccq(struct i40iw_device *iwdev)
{
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_dma_mem mem;
	enum i40iw_status_code status;
	struct i40iw_ccq_init_info info;
	struct i40iw_ccq *ccq = &iwdev->ccq;

	memset(&info, 0, sizeof(info));
	dev->ccq = &ccq->sc_cq;
	dev->ccq->dev = dev;
	info.dev = dev;
	ccq->shadow_area.size = sizeof(struct i40iw_cq_shadow_area);
	ccq->mem_cq.size = sizeof(struct i40iw_cqe) * IW_CCQ_SIZE;
	status = i40iw_allocate_dma_mem(dev->hw, &ccq->mem_cq,
					ccq->mem_cq.size, I40IW_CQ0_ALIGNMENT);
	if (status)
		goto exit;
	status = i40iw_obj_aligned_mem(iwdev, &mem, ccq->shadow_area.size,
				       I40IW_SHADOWAREA_MASK);
	if (status)
		goto exit;
	ccq->sc_cq.back_cq = (void *)ccq;
	/* populate the ccq init info */
	info.cq_base = ccq->mem_cq.va;
	info.cq_pa = ccq->mem_cq.pa;
	info.num_elem = IW_CCQ_SIZE;
	info.shadow_area = mem.va;
	info.shadow_area_pa = mem.pa;
	info.ceqe_mask = false;
	info.ceq_id_valid = true;
	info.shadow_read_threshold = 16;
	status = dev->ccq_ops->ccq_init(dev->ccq, &info);
	if (!status)
		status = dev->ccq_ops->ccq_create(dev->ccq, 0, true, true);
exit:
	if (status)
		i40iw_free_dma_mem(dev->hw, &ccq->mem_cq);
	return status;
}

/**
 * i40iw_configure_ceq_vector - set up the msix interrupt vector for ceq
 * @iwdev: iwarp device
 * @msix_vec: interrupt vector information
 * @iwceq: ceq associated with the vector
 * @ceq_id: the id number of the iwceq
 *
 * Allocate interrupt resources and enable irq handling
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_configure_ceq_vector(struct i40iw_device *iwdev,
							 struct i40iw_ceq *iwceq,
							 u32 ceq_id,
							 struct i40iw_msix_vector *msix_vec)
{
	enum i40iw_status_code status;
	cpumask_t mask;

	if (iwdev->msix_shared && !ceq_id) {
		tasklet_init(&iwdev->dpc_tasklet, i40iw_dpc, (unsigned long)iwdev);
		status = request_irq(msix_vec->irq, i40iw_irq_handler, 0, "AEQCEQ", iwdev);
	} else {
		tasklet_init(&iwceq->dpc_tasklet, i40iw_ceq_dpc, (unsigned long)iwceq);
		status = request_irq(msix_vec->irq, i40iw_ceq_handler, 0, "CEQ", iwceq);
	}

	cpumask_clear(&mask);
	cpumask_set_cpu(msix_vec->cpu_affinity, &mask);
	irq_set_affinity_hint(msix_vec->irq, &mask);

	if (status) {
		i40iw_pr_err("ceq irq config fail\n");
		return I40IW_ERR_CONFIG;
	}
	msix_vec->ceq_id = ceq_id;

	return 0;
}

/**
 * i40iw_create_ceq - create completion event queue
 * @iwdev: iwarp device
 * @iwceq: pointer to the ceq resources to be created
 * @ceq_id: the id number of the iwceq
 *
 * Return 0, if the ceq and the resources associated with it
 * are successfully created, otherwise return error
 */
static enum i40iw_status_code i40iw_create_ceq(struct i40iw_device *iwdev,
					       struct i40iw_ceq *iwceq,
					       u32 ceq_id)
{
	enum i40iw_status_code status;
	struct i40iw_ceq_init_info info;
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	u64 scratch;

	memset(&info, 0, sizeof(info));
	info.ceq_id = ceq_id;
	iwceq->iwdev = iwdev;
	iwceq->mem.size = sizeof(struct i40iw_ceqe) *
		iwdev->sc_dev.hmc_info->hmc_obj[I40IW_HMC_IW_CQ].cnt;
	status = i40iw_allocate_dma_mem(dev->hw, &iwceq->mem, iwceq->mem.size,
					I40IW_CEQ_ALIGNMENT);
	if (status)
		goto exit;
	info.ceq_id = ceq_id;
	info.ceqe_base = iwceq->mem.va;
	info.ceqe_pa = iwceq->mem.pa;

	info.elem_cnt = iwdev->sc_dev.hmc_info->hmc_obj[I40IW_HMC_IW_CQ].cnt;
	iwceq->sc_ceq.ceq_id = ceq_id;
	info.dev = dev;
	scratch = (uintptr_t)&iwdev->cqp.sc_cqp;
	status = dev->ceq_ops->ceq_init(&iwceq->sc_ceq, &info);
	if (!status)
		status = dev->ceq_ops->cceq_create(&iwceq->sc_ceq, scratch);

exit:
	if (status)
		i40iw_free_dma_mem(dev->hw, &iwceq->mem);
	return status;
}

void i40iw_request_reset(struct i40iw_device *iwdev)
{
	struct i40e_info *ldev = iwdev->ldev;

	ldev->ops->request_reset(ldev, iwdev->client, 1);
}

/**
 * i40iw_setup_ceqs - manage the device ceq's and their interrupt resources
 * @iwdev: iwarp device
 * @ldev: i40e lan device
 *
 * Allocate a list for all device completion event queues
 * Create the ceq's and configure their msix interrupt vectors
 * Return 0, if at least one ceq is successfully set up, otherwise return error
 */
static enum i40iw_status_code i40iw_setup_ceqs(struct i40iw_device *iwdev,
					       struct i40e_info *ldev)
{
	u32 i;
	u32 ceq_id;
	struct i40iw_ceq *iwceq;
	struct i40iw_msix_vector *msix_vec;
	enum i40iw_status_code status = 0;
	u32 num_ceqs;

	if (ldev && ldev->ops && ldev->ops->setup_qvlist) {
		status = ldev->ops->setup_qvlist(ldev, &i40iw_client,
						 iwdev->iw_qvlist);
		if (status)
			goto exit;
	} else {
		status = I40IW_ERR_BAD_PTR;
		goto exit;
	}

	num_ceqs = min(iwdev->msix_count, iwdev->sc_dev.hmc_fpm_misc.max_ceqs);
	iwdev->ceqlist = kcalloc(num_ceqs, sizeof(*iwdev->ceqlist), GFP_KERNEL);
	if (!iwdev->ceqlist) {
		status = I40IW_ERR_NO_MEMORY;
		goto exit;
	}
	i = (iwdev->msix_shared) ? 0 : 1;
	for (ceq_id = 0; i < num_ceqs; i++, ceq_id++) {
		iwceq = &iwdev->ceqlist[ceq_id];
		status = i40iw_create_ceq(iwdev, iwceq, ceq_id);
		if (status) {
			i40iw_pr_err("create ceq status = %d\n", status);
			break;
		}

		msix_vec = &iwdev->iw_msixtbl[i];
		iwceq->irq = msix_vec->irq;
		iwceq->msix_idx = msix_vec->idx;
		status = i40iw_configure_ceq_vector(iwdev, iwceq, ceq_id, msix_vec);
		if (status) {
			i40iw_destroy_ceq(iwdev, iwceq, false);
			break;
		}
		i40iw_enable_intr(&iwdev->sc_dev, msix_vec->idx);
		iwdev->ceqs_count++;
	}

exit:
	if (status) {
		if (!iwdev->ceqs_count) {
			kfree(iwdev->ceqlist);
			iwdev->ceqlist = NULL;
		} else {
			status = 0;
		}
	}
	return status;
}

/**
 * i40iw_configure_aeq_vector - set up the msix vector for aeq
 * @iwdev: iwarp device
 *
 * Allocate interrupt resources and enable irq handling
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_configure_aeq_vector(struct i40iw_device *iwdev)
{
	struct i40iw_msix_vector *msix_vec = iwdev->iw_msixtbl;
	u32 ret = 0;

	if (!iwdev->msix_shared) {
		tasklet_init(&iwdev->dpc_tasklet, i40iw_dpc, (unsigned long)iwdev);
		ret = request_irq(msix_vec->irq, i40iw_irq_handler, 0, "i40iw", iwdev);
	}
	if (ret) {
		i40iw_pr_err("aeq irq config fail\n");
		return I40IW_ERR_CONFIG;
	}

	return 0;
}

/**
 * i40iw_create_aeq - create async event queue
 * @iwdev: iwarp device
 *
 * Return 0, if the aeq and the resources associated with it
 * are successfully created, otherwise return error
 */
static enum i40iw_status_code i40iw_create_aeq(struct i40iw_device *iwdev)
{
	enum i40iw_status_code status;
	struct i40iw_aeq_init_info info;
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_aeq *aeq = &iwdev->aeq;
	u64 scratch = 0;
	u32 aeq_size;

	aeq_size = 2 * iwdev->sc_dev.hmc_info->hmc_obj[I40IW_HMC_IW_QP].cnt +
		iwdev->sc_dev.hmc_info->hmc_obj[I40IW_HMC_IW_CQ].cnt;
	memset(&info, 0, sizeof(info));
	aeq->mem.size = sizeof(struct i40iw_sc_aeqe) * aeq_size;
	status = i40iw_allocate_dma_mem(dev->hw, &aeq->mem, aeq->mem.size,
					I40IW_AEQ_ALIGNMENT);
	if (status)
		goto exit;

	info.aeqe_base = aeq->mem.va;
	info.aeq_elem_pa = aeq->mem.pa;
	info.elem_cnt = aeq_size;
	info.dev = dev;
	status = dev->aeq_ops->aeq_init(&aeq->sc_aeq, &info);
	if (status)
		goto exit;
	status = dev->aeq_ops->aeq_create(&aeq->sc_aeq, scratch, 1);
	if (!status)
		status = dev->aeq_ops->aeq_create_done(&aeq->sc_aeq);
exit:
	if (status)
		i40iw_free_dma_mem(dev->hw, &aeq->mem);
	return status;
}

/**
 * i40iw_setup_aeq - set up the device aeq
 * @iwdev: iwarp device
 *
 * Create the aeq and configure its msix interrupt vector
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_setup_aeq(struct i40iw_device *iwdev)
{
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	enum i40iw_status_code status;

	status = i40iw_create_aeq(iwdev);
	if (status)
		return status;

	status = i40iw_configure_aeq_vector(iwdev);
	if (status) {
		i40iw_destroy_aeq(iwdev, false);
		return status;
	}

	if (!iwdev->msix_shared)
		i40iw_enable_intr(dev, iwdev->iw_msixtbl[0].idx);
	return 0;
}

/**
 * i40iw_initialize_ilq - create iwarp local queue for cm
 * @iwdev: iwarp device
 *
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_initialize_ilq(struct i40iw_device *iwdev)
{
	struct i40iw_puda_rsrc_info info;
	enum i40iw_status_code status;

	memset(&info, 0, sizeof(info));
	info.type = I40IW_PUDA_RSRC_TYPE_ILQ;
	info.cq_id = 1;
	info.qp_id = 0;
	info.count = 1;
	info.pd_id = 1;
	info.sq_size = 8192;
	info.rq_size = 8192;
	info.buf_size = 1024;
	info.tx_buf_cnt = 16384;
	info.receive = i40iw_receive_ilq;
	info.xmit_complete = i40iw_free_sqbuf;
	status = i40iw_puda_create_rsrc(&iwdev->vsi, &info);
	if (status)
		i40iw_pr_err("ilq create fail\n");
	return status;
}

/**
 * i40iw_initialize_ieq - create iwarp exception queue
 * @iwdev: iwarp device
 *
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_initialize_ieq(struct i40iw_device *iwdev)
{
	struct i40iw_puda_rsrc_info info;
	enum i40iw_status_code status;

	memset(&info, 0, sizeof(info));
	info.type = I40IW_PUDA_RSRC_TYPE_IEQ;
	info.cq_id = 2;
	info.qp_id = iwdev->sc_dev.exception_lan_queue;
	info.count = 1;
	info.pd_id = 2;
	info.sq_size = 8192;
	info.rq_size = 8192;
	info.buf_size = 2048;
	info.tx_buf_cnt = 16384;
	status = i40iw_puda_create_rsrc(&iwdev->vsi, &info);
	if (status)
		i40iw_pr_err("ieq create fail\n");
	return status;
}

/**
 * i40iw_hmc_setup - create hmc objects for the device
 * @iwdev: iwarp device
 *
 * Set up the device private memory space for the number and size of
 * the hmc objects and create the objects
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_hmc_setup(struct i40iw_device *iwdev)
{
	enum i40iw_status_code status;

	iwdev->sd_type = I40IW_SD_TYPE_DIRECT;
	status = i40iw_config_fpm_values(&iwdev->sc_dev, IW_CFG_FPM_QP_COUNT);
	if (status)
		goto exit;
	status = i40iw_create_hmc_objs(iwdev, true);
	if (status)
		goto exit;
	iwdev->init_state = HMC_OBJS_CREATED;
exit:
	return status;
}

/**
 * i40iw_del_init_mem - deallocate memory resources
 * @iwdev: iwarp device
 */
static void i40iw_del_init_mem(struct i40iw_device *iwdev)
{
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;

	i40iw_free_dma_mem(&iwdev->hw, &iwdev->obj_mem);
	kfree(dev->hmc_info->sd_table.sd_entry);
	dev->hmc_info->sd_table.sd_entry = NULL;
	kfree(iwdev->mem_resources);
	iwdev->mem_resources = NULL;
	kfree(iwdev->ceqlist);
	iwdev->ceqlist = NULL;
	kfree(iwdev->iw_msixtbl);
	iwdev->iw_msixtbl = NULL;
	kfree(iwdev->hmc_info_mem);
	iwdev->hmc_info_mem = NULL;
}

/**
 * i40iw_del_macip_entry - remove a mac ip address entry from the hw table
 * @iwdev: iwarp device
 * @idx: the index of the mac ip address to delete
 */
static void i40iw_del_macip_entry(struct i40iw_device *iwdev, u8 idx)
{
	struct i40iw_cqp *iwcqp = &iwdev->cqp;
	struct i40iw_cqp_request *cqp_request;
	struct cqp_commands_info *cqp_info;
	enum i40iw_status_code status = 0;

	cqp_request = i40iw_get_cqp_request(iwcqp, true);
	if (!cqp_request) {
		i40iw_pr_err("cqp_request memory failed\n");
		return;
	}
	cqp_info = &cqp_request->info;
	cqp_info->cqp_cmd = OP_DELETE_LOCAL_MAC_IPADDR_ENTRY;
	cqp_info->post_sq = 1;
	cqp_info->in.u.del_local_mac_ipaddr_entry.cqp = &iwcqp->sc_cqp;
	cqp_info->in.u.del_local_mac_ipaddr_entry.scratch = (uintptr_t)cqp_request;
	cqp_info->in.u.del_local_mac_ipaddr_entry.entry_idx = idx;
	cqp_info->in.u.del_local_mac_ipaddr_entry.ignore_ref_count = 0;
	status = i40iw_handle_cqp_op(iwdev, cqp_request);
	if (status)
		i40iw_pr_err("CQP-OP Del MAC Ip entry fail");
}

/**
 * i40iw_add_mac_ipaddr_entry - add a mac ip address entry to the hw table
 * @iwdev: iwarp device
 * @mac_addr: pointer to mac address
 * @idx: the index of the mac ip address to add
 */
static enum i40iw_status_code i40iw_add_mac_ipaddr_entry(struct i40iw_device *iwdev,
							 u8 *mac_addr,
							 u8 idx)
{
	struct i40iw_local_mac_ipaddr_entry_info *info;
	struct i40iw_cqp *iwcqp = &iwdev->cqp;
	struct i40iw_cqp_request *cqp_request;
	struct cqp_commands_info *cqp_info;
	enum i40iw_status_code status = 0;

	cqp_request = i40iw_get_cqp_request(iwcqp, true);
	if (!cqp_request) {
		i40iw_pr_err("cqp_request memory failed\n");
		return I40IW_ERR_NO_MEMORY;
	}

	cqp_info = &cqp_request->info;

	cqp_info->post_sq = 1;
	info = &cqp_info->in.u.add_local_mac_ipaddr_entry.info;
	ether_addr_copy(info->mac_addr, mac_addr);
	info->entry_idx = idx;
	cqp_info->in.u.add_local_mac_ipaddr_entry.scratch = (uintptr_t)cqp_request;
	cqp_info->cqp_cmd = OP_ADD_LOCAL_MAC_IPADDR_ENTRY;
	cqp_info->in.u.add_local_mac_ipaddr_entry.cqp = &iwcqp->sc_cqp;
	cqp_info->in.u.add_local_mac_ipaddr_entry.scratch = (uintptr_t)cqp_request;
	status = i40iw_handle_cqp_op(iwdev, cqp_request);
	if (status)
		i40iw_pr_err("CQP-OP Add MAC Ip entry fail");
	return status;
}

/**
 * i40iw_alloc_local_mac_ipaddr_entry - allocate a mac ip address entry
 * @iwdev: iwarp device
 * @mac_ip_tbl_idx: the index of the new mac ip address
 *
 * Allocate a mac ip address entry and update the mac_ip_tbl_idx
 * to hold the index of the newly created mac ip address
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_alloc_local_mac_ipaddr_entry(struct i40iw_device *iwdev,
								 u16 *mac_ip_tbl_idx)
{
	struct i40iw_cqp *iwcqp = &iwdev->cqp;
	struct i40iw_cqp_request *cqp_request;
	struct cqp_commands_info *cqp_info;
	enum i40iw_status_code status = 0;

	cqp_request = i40iw_get_cqp_request(iwcqp, true);
	if (!cqp_request) {
		i40iw_pr_err("cqp_request memory failed\n");
		return I40IW_ERR_NO_MEMORY;
	}

	/* increment refcount, because we need the cqp request ret value */
	atomic_inc(&cqp_request->refcount);

	cqp_info = &cqp_request->info;
	cqp_info->cqp_cmd = OP_ALLOC_LOCAL_MAC_IPADDR_ENTRY;
	cqp_info->post_sq = 1;
	cqp_info->in.u.alloc_local_mac_ipaddr_entry.cqp = &iwcqp->sc_cqp;
	cqp_info->in.u.alloc_local_mac_ipaddr_entry.scratch = (uintptr_t)cqp_request;
	status = i40iw_handle_cqp_op(iwdev, cqp_request);
	if (!status)
		*mac_ip_tbl_idx = cqp_request->compl_info.op_ret_val;
	else
		i40iw_pr_err("CQP-OP Alloc MAC Ip entry fail");
	/* decrement refcount and free the cqp request, if no longer used */
	i40iw_put_cqp_request(iwcqp, cqp_request);
	return status;
}

/**
 * i40iw_alloc_set_mac_ipaddr - set up a mac ip address table entry
 * @iwdev: iwarp device
 * @macaddr: pointer to mac address
 *
 * Allocate a mac ip address entry and add it to the hw table
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_alloc_set_mac_ipaddr(struct i40iw_device *iwdev,
							 u8 *macaddr)
{
	enum i40iw_status_code status;

	status = i40iw_alloc_local_mac_ipaddr_entry(iwdev, &iwdev->mac_ip_table_idx);
	if (!status) {
		status = i40iw_add_mac_ipaddr_entry(iwdev, macaddr,
						    (u8)iwdev->mac_ip_table_idx);
		if (status)
			i40iw_del_macip_entry(iwdev, (u8)iwdev->mac_ip_table_idx);
	}
	return status;
}

/**
 * i40iw_add_ipv6_addr - add ipv6 address to the hw arp table
 * @iwdev: iwarp device
 */
static void i40iw_add_ipv6_addr(struct i40iw_device *iwdev)
{
	struct net_device *ip_dev;
	struct inet6_dev *idev;
	struct inet6_ifaddr *ifp, *tmp;
	u32 local_ipaddr6[4];

	rcu_read_lock();
	for_each_netdev_rcu(&init_net, ip_dev) {
		if ((((rdma_vlan_dev_vlan_id(ip_dev) < 0xFFFF) &&
		      (rdma_vlan_dev_real_dev(ip_dev) == iwdev->netdev)) ||
		     (ip_dev == iwdev->netdev)) && (ip_dev->flags & IFF_UP)) {
			idev = __in6_dev_get(ip_dev);
			if (!idev) {
				i40iw_pr_err("ipv6 inet device not found\n");
				break;
			}
			list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
				i40iw_pr_info("IP=%pI6, vlan_id=%d, MAC=%pM\n", &ifp->addr,
					      rdma_vlan_dev_vlan_id(ip_dev), ip_dev->dev_addr);
				i40iw_copy_ip_ntohl(local_ipaddr6,
						    ifp->addr.in6_u.u6_addr32);
				i40iw_manage_arp_cache(iwdev,
						       ip_dev->dev_addr,
						       local_ipaddr6,
						       false,
						       I40IW_ARP_ADD);
			}
		}
	}
	rcu_read_unlock();
}

/**
 * i40iw_add_ipv4_addr - add ipv4 address to the hw arp table
 * @iwdev: iwarp device
 */
static void i40iw_add_ipv4_addr(struct i40iw_device *iwdev)
{
	struct net_device *dev;
	struct in_device *idev;
	bool got_lock = true;
	u32 ip_addr;

	if (!rtnl_trylock())
		got_lock = false;

	for_each_netdev(&init_net, dev) {
		if ((((rdma_vlan_dev_vlan_id(dev) < 0xFFFF) &&
		      (rdma_vlan_dev_real_dev(dev) == iwdev->netdev)) ||
		    (dev == iwdev->netdev)) && (dev->flags & IFF_UP)) {
			idev = in_dev_get(dev);
			for_ifa(idev) {
				i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_CM,
					    "IP=%pI4, vlan_id=%d, MAC=%pM\n", &ifa->ifa_address,
					     rdma_vlan_dev_vlan_id(dev), dev->dev_addr);

				ip_addr = ntohl(ifa->ifa_address);
				i40iw_manage_arp_cache(iwdev,
						       dev->dev_addr,
						       &ip_addr,
						       true,
						       I40IW_ARP_ADD);
			}
			endfor_ifa(idev);
			in_dev_put(idev);
		}
	}
	if (got_lock)
		rtnl_unlock();
}

/**
 * i40iw_add_mac_ip - add mac and ip addresses
 * @iwdev: iwarp device
 *
 * Create and add a mac ip address entry to the hw table and
 * ipv4/ipv6 addresses to the arp cache
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_add_mac_ip(struct i40iw_device *iwdev)
{
	struct net_device *netdev = iwdev->netdev;
	enum i40iw_status_code status;

	status = i40iw_alloc_set_mac_ipaddr(iwdev, (u8 *)netdev->dev_addr);
	if (status)
		return status;
	i40iw_add_ipv4_addr(iwdev);
	i40iw_add_ipv6_addr(iwdev);
	return 0;
}

/**
 * i40iw_wait_pe_ready - Check if firmware is ready
 * @hw: provides access to registers
 */
static void i40iw_wait_pe_ready(struct i40iw_hw *hw)
{
	u32 statusfw;
	u32 statuscpu0;
	u32 statuscpu1;
	u32 statuscpu2;
	u32 retrycount = 0;

	do {
		statusfw = i40iw_rd32(hw, I40E_GLPE_FWLDSTATUS);
		i40iw_pr_info("[%04d] fm load status[x%04X]\n", __LINE__, statusfw);
		statuscpu0 = i40iw_rd32(hw, I40E_GLPE_CPUSTATUS0);
		i40iw_pr_info("[%04d] CSR_CQP status[x%04X]\n", __LINE__, statuscpu0);
		statuscpu1 = i40iw_rd32(hw, I40E_GLPE_CPUSTATUS1);
		i40iw_pr_info("[%04d] I40E_GLPE_CPUSTATUS1 status[x%04X]\n",
			      __LINE__, statuscpu1);
		statuscpu2 = i40iw_rd32(hw, I40E_GLPE_CPUSTATUS2);
		i40iw_pr_info("[%04d] I40E_GLPE_CPUSTATUS2 status[x%04X]\n",
			      __LINE__, statuscpu2);
		if ((statuscpu0 == 0x80) && (statuscpu1 == 0x80) && (statuscpu2 == 0x80))
			break;	/* SUCCESS */
		mdelay(1000);
		retrycount++;
	} while (retrycount < 14);
	i40iw_wr32(hw, 0xb4040, 0x4C104C5);
}

/**
 * i40iw_initialize_dev - initialize device
 * @iwdev: iwarp device
 * @ldev: lan device information
 *
 * Allocate memory for the hmc objects and initialize iwdev
 * Return 0 if successful, otherwise clean up the resources
 * and return error
 */
static enum i40iw_status_code i40iw_initialize_dev(struct i40iw_device *iwdev,
						   struct i40e_info *ldev)
{
	enum i40iw_status_code status;
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	struct i40iw_device_init_info info;
	struct i40iw_vsi_init_info vsi_info;
	struct i40iw_dma_mem mem;
	struct i40iw_l2params l2params;
	u32 size;
	struct i40iw_vsi_stats_info stats_info;
	u16 last_qset = I40IW_NO_QSET;
	u16 qset;
	u32 i;

	memset(&l2params, 0, sizeof(l2params));
	memset(&info, 0, sizeof(info));
	size = sizeof(struct i40iw_hmc_pble_rsrc) + sizeof(struct i40iw_hmc_info) +
				(sizeof(struct i40iw_hmc_obj_info) * I40IW_HMC_IW_MAX);
	iwdev->hmc_info_mem = kzalloc(size, GFP_KERNEL);
	if (!iwdev->hmc_info_mem)
		return I40IW_ERR_NO_MEMORY;

	iwdev->pble_rsrc = (struct i40iw_hmc_pble_rsrc *)iwdev->hmc_info_mem;
	dev->hmc_info = &iwdev->hw.hmc;
	dev->hmc_info->hmc_obj = (struct i40iw_hmc_obj_info *)(iwdev->pble_rsrc + 1);
	status = i40iw_obj_aligned_mem(iwdev, &mem, I40IW_QUERY_FPM_BUF_SIZE,
				       I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
	if (status)
		goto exit;
	info.fpm_query_buf_pa = mem.pa;
	info.fpm_query_buf = mem.va;
	status = i40iw_obj_aligned_mem(iwdev, &mem, I40IW_COMMIT_FPM_BUF_SIZE,
				       I40IW_FPM_COMMIT_BUF_ALIGNMENT_MASK);
	if (status)
		goto exit;
	info.fpm_commit_buf_pa = mem.pa;
	info.fpm_commit_buf = mem.va;
	info.hmc_fn_id = ldev->fid;
	info.is_pf = (ldev->ftype) ? false : true;
	info.bar0 = ldev->hw_addr;
	info.hw = &iwdev->hw;
	info.debug_mask = debug;
	l2params.mss =
		(ldev->params.mtu) ? ldev->params.mtu - I40IW_MTU_TO_MSS : I40IW_DEFAULT_MSS;
	for (i = 0; i < I40E_CLIENT_MAX_USER_PRIORITY; i++) {
		qset = ldev->params.qos.prio_qos[i].qs_handle;
		l2params.qs_handle_list[i] = qset;
		if (last_qset == I40IW_NO_QSET)
			last_qset = qset;
		else if ((qset != last_qset) && (qset != I40IW_NO_QSET))
			iwdev->dcb = true;
	}
	i40iw_pr_info("DCB is set/clear = %d\n", iwdev->dcb);
	info.exception_lan_queue = 1;
	info.vchnl_send = i40iw_virtchnl_send;
	status = i40iw_device_init(&iwdev->sc_dev, &info);
exit:
	if (status) {
		kfree(iwdev->hmc_info_mem);
		iwdev->hmc_info_mem = NULL;
	}
	memset(&vsi_info, 0, sizeof(vsi_info));
	vsi_info.dev = &iwdev->sc_dev;
	vsi_info.back_vsi = (void *)iwdev;
	vsi_info.params = &l2params;
	i40iw_sc_vsi_init(&iwdev->vsi, &vsi_info);

	if (dev->is_pf) {
		memset(&stats_info, 0, sizeof(stats_info));
		stats_info.fcn_id = ldev->fid;
		stats_info.pestat = kzalloc(sizeof(*stats_info.pestat), GFP_KERNEL);
		stats_info.stats_initialize = true;
		if (stats_info.pestat)
			i40iw_vsi_stats_init(&iwdev->vsi, &stats_info);
	}
	return status;
}

/**
 * i40iw_register_notifiers - register tcp ip notifiers
 */
static void i40iw_register_notifiers(void)
{
	if (atomic_inc_return(&i40iw_notifiers_registered) == 1) {
		register_inetaddr_notifier(&i40iw_inetaddr_notifier);
		register_inet6addr_notifier(&i40iw_inetaddr6_notifier);
		register_netevent_notifier(&i40iw_net_notifier);
	}
}

/**
 * i40iw_save_msix_info - copy msix vector information to iwarp device
 * @iwdev: iwarp device
 * @ldev: lan device information
 *
 * Allocate iwdev msix table and copy the ldev msix info to the table
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_save_msix_info(struct i40iw_device *iwdev,
						   struct i40e_info *ldev)
{
	struct i40e_qvlist_info *iw_qvlist;
	struct i40e_qv_info *iw_qvinfo;
	u32 ceq_idx;
	u32 i;
	u32 size;

	iwdev->msix_count = ldev->msix_count;

	size = sizeof(struct i40iw_msix_vector) * iwdev->msix_count;
	size += sizeof(struct i40e_qvlist_info);
	size +=  sizeof(struct i40e_qv_info) * iwdev->msix_count - 1;
	iwdev->iw_msixtbl = kzalloc(size, GFP_KERNEL);

	if (!iwdev->iw_msixtbl)
		return I40IW_ERR_NO_MEMORY;
	iwdev->iw_qvlist = (struct i40e_qvlist_info *)(&iwdev->iw_msixtbl[iwdev->msix_count]);
	iw_qvlist = iwdev->iw_qvlist;
	iw_qvinfo = iw_qvlist->qv_info;
	iw_qvlist->num_vectors = iwdev->msix_count;
	if (iwdev->msix_count <= num_online_cpus())
		iwdev->msix_shared = true;
	for (i = 0, ceq_idx = 0; i < iwdev->msix_count; i++, iw_qvinfo++) {
		iwdev->iw_msixtbl[i].idx = ldev->msix_entries[i].entry;
		iwdev->iw_msixtbl[i].irq = ldev->msix_entries[i].vector;
		iwdev->iw_msixtbl[i].cpu_affinity = ceq_idx;
		if (i == 0) {
			iw_qvinfo->aeq_idx = 0;
			if (iwdev->msix_shared)
				iw_qvinfo->ceq_idx = ceq_idx++;
			else
				iw_qvinfo->ceq_idx = I40E_QUEUE_INVALID_IDX;
		} else {
			iw_qvinfo->aeq_idx = I40E_QUEUE_INVALID_IDX;
			iw_qvinfo->ceq_idx = ceq_idx++;
		}
		iw_qvinfo->itr_idx = 3;
		iw_qvinfo->v_idx = iwdev->iw_msixtbl[i].idx;
	}
	return 0;
}

/**
 * i40iw_deinit_device - clean up the device resources
 * @iwdev: iwarp device
 * @reset: true if called before reset
 *
 * Destroy the ib device interface, remove the mac ip entry and ipv4/ipv6 addresses,
 * destroy the device queues and free the pble and the hmc objects
 */
static void i40iw_deinit_device(struct i40iw_device *iwdev, bool reset)
{
	struct i40e_info *ldev = iwdev->ldev;

	struct i40iw_sc_dev *dev = &iwdev->sc_dev;

	i40iw_pr_info("state = %d\n", iwdev->init_state);
	if (iwdev->param_wq)
		destroy_workqueue(iwdev->param_wq);

	switch (iwdev->init_state) {
	case RDMA_DEV_REGISTERED:
		iwdev->iw_status = 0;
		i40iw_port_ibevent(iwdev);
		i40iw_destroy_rdma_device(iwdev->iwibdev);
		/* fallthrough */
	case IP_ADDR_REGISTERED:
		if (!reset)
			i40iw_del_macip_entry(iwdev, (u8)iwdev->mac_ip_table_idx);
		/* fallthrough */
	case INET_NOTIFIER:
		if (!atomic_dec_return(&i40iw_notifiers_registered)) {
			unregister_netevent_notifier(&i40iw_net_notifier);
			unregister_inetaddr_notifier(&i40iw_inetaddr_notifier);
			unregister_inet6addr_notifier(&i40iw_inetaddr6_notifier);
		}
		/* fallthrough */
	case CEQ_CREATED:
		i40iw_dele_ceqs(iwdev, reset);
		/* fallthrough */
	case AEQ_CREATED:
		i40iw_destroy_aeq(iwdev, reset);
		/* fallthrough */
	case IEQ_CREATED:
		i40iw_puda_dele_resources(&iwdev->vsi, I40IW_PUDA_RSRC_TYPE_IEQ, reset);
		/* fallthrough */
	case ILQ_CREATED:
		i40iw_puda_dele_resources(&iwdev->vsi, I40IW_PUDA_RSRC_TYPE_ILQ, reset);
		/* fallthrough */
	case CCQ_CREATED:
		i40iw_destroy_ccq(iwdev, reset);
		/* fallthrough */
	case PBLE_CHUNK_MEM:
		i40iw_destroy_pble_pool(dev, iwdev->pble_rsrc);
		/* fallthrough */
	case HMC_OBJS_CREATED:
		i40iw_del_hmc_objects(dev, dev->hmc_info, true, reset);
		/* fallthrough */
	case CQP_CREATED:
		i40iw_destroy_cqp(iwdev, true);
		/* fallthrough */
	case INITIAL_STATE:
		i40iw_cleanup_cm_core(&iwdev->cm_core);
		if (iwdev->vsi.pestat) {
			i40iw_vsi_stats_free(&iwdev->vsi);
			kfree(iwdev->vsi.pestat);
		}
		i40iw_del_init_mem(iwdev);
		break;
	case INVALID_STATE:
		/* fallthrough */
	default:
		i40iw_pr_err("bad init_state = %d\n", iwdev->init_state);
		break;
	}

	i40iw_del_handler(i40iw_find_i40e_handler(ldev));
	kfree(iwdev->hdl);
}

/**
 * i40iw_setup_init_state - set up the initial device struct
 * @hdl: handler for iwarp device - one per instance
 * @ldev: lan device information
 * @client: iwarp client information, provided during registration
 *
 * Initialize the iwarp device and its hdl information
 * using the ldev and client information
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_setup_init_state(struct i40iw_handler *hdl,
						     struct i40e_info *ldev,
						     struct i40e_client *client)
{
	struct i40iw_device *iwdev = &hdl->device;
	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
	enum i40iw_status_code status;

	memcpy(&hdl->ldev, ldev, sizeof(*ldev));
	if (resource_profile == 1)
		resource_profile = 2;

	iwdev->mpa_version = mpa_version;
	iwdev->resource_profile = (resource_profile < I40IW_HMC_PROFILE_EQUAL) ?
	    (u8)resource_profile + I40IW_HMC_PROFILE_DEFAULT :
	    I40IW_HMC_PROFILE_DEFAULT;
	iwdev->max_rdma_vfs =
		(iwdev->resource_profile != I40IW_HMC_PROFILE_DEFAULT) ?  max_rdma_vfs : 0;
	iwdev->max_enabled_vfs = iwdev->max_rdma_vfs;
	iwdev->netdev = ldev->netdev;
	hdl->client = client;
	if (!ldev->ftype)
		iwdev->db_start = pci_resource_start(ldev->pcidev, 0) + I40IW_DB_ADDR_OFFSET;
	else
		iwdev->db_start = pci_resource_start(ldev->pcidev, 0) + I40IW_VF_DB_ADDR_OFFSET;

	status = i40iw_save_msix_info(iwdev, ldev);
	if (status)
		goto exit;
	iwdev->hw.dev_context = (void *)ldev->pcidev;
	iwdev->hw.hw_addr = ldev->hw_addr;
	status = i40iw_allocate_dma_mem(&iwdev->hw,
					&iwdev->obj_mem, 8192, 4096);
	if (status)
		goto exit;
	iwdev->obj_next = iwdev->obj_mem;
	iwdev->push_mode = push_mode;

	init_waitqueue_head(&iwdev->vchnl_waitq);
	init_waitqueue_head(&dev->vf_reqs);
	init_waitqueue_head(&iwdev->close_wq);

	status = i40iw_initialize_dev(iwdev, ldev);
exit:
	if (status) {
		kfree(iwdev->iw_msixtbl);
		i40iw_free_dma_mem(dev->hw, &iwdev->obj_mem);
		iwdev->iw_msixtbl = NULL;
	}
	return status;
}

/**
 * i40iw_get_used_rsrc - determine resources used internally
 * @iwdev: iwarp device
 *
 * Called after internal allocations
 */
static void i40iw_get_used_rsrc(struct i40iw_device *iwdev)
{
	iwdev->used_pds = find_next_zero_bit(iwdev->allocated_pds, iwdev->max_pd, 0);
	iwdev->used_qps = find_next_zero_bit(iwdev->allocated_qps, iwdev->max_qp, 0);
	iwdev->used_cqs = find_next_zero_bit(iwdev->allocated_cqs, iwdev->max_cq, 0);
	iwdev->used_mrs = find_next_zero_bit(iwdev->allocated_mrs, iwdev->max_mr, 0);
}

/**
 * i40iw_open - client interface operation open for iwarp/uda device
 * @ldev: lan device information
 * @client: iwarp client information, provided during registration
 *
 * Called by the lan driver during the processing of client register
 * Create device resources, set up queues, pble and hmc objects and
 * register the device with the ib verbs interface
 * Return 0 if successful, otherwise return error
 */
static int i40iw_open(struct i40e_info *ldev, struct i40e_client *client)
{
	struct i40iw_device *iwdev;
	struct i40iw_sc_dev *dev;
	enum i40iw_status_code status;
	struct i40iw_handler *hdl;

	hdl = i40iw_find_netdev(ldev->netdev);
	if (hdl)
		return 0;

	hdl = kzalloc(sizeof(*hdl), GFP_KERNEL);
	if (!hdl)
		return -ENOMEM;
	iwdev = &hdl->device;
	iwdev->hdl = hdl;
	dev = &iwdev->sc_dev;
	i40iw_setup_cm_core(iwdev);

	dev->back_dev = (void *)iwdev;
	iwdev->ldev = &hdl->ldev;
	iwdev->client = client;
	mutex_init(&iwdev->pbl_mutex);
	i40iw_add_handler(hdl);

	do {
		status = i40iw_setup_init_state(hdl, ldev, client);
		if (status)
			break;
		iwdev->init_state = INITIAL_STATE;
		if (dev->is_pf)
			i40iw_wait_pe_ready(dev->hw);
		status = i40iw_create_cqp(iwdev);
		if (status)
			break;
		iwdev->init_state = CQP_CREATED;
		status = i40iw_hmc_setup(iwdev);
		if (status)
			break;
		status = i40iw_create_ccq(iwdev);
		if (status)
			break;
		iwdev->init_state = CCQ_CREATED;
		status = i40iw_initialize_ilq(iwdev);
		if (status)
			break;
		iwdev->init_state = ILQ_CREATED;
		status = i40iw_initialize_ieq(iwdev);
		if (status)
			break;
		iwdev->init_state = IEQ_CREATED;
		status = i40iw_setup_aeq(iwdev);
		if (status)
			break;
		iwdev->init_state = AEQ_CREATED;
		status = i40iw_setup_ceqs(iwdev, ldev);
		if (status)
			break;
		iwdev->init_state = CEQ_CREATED;
		status = i40iw_initialize_hw_resources(iwdev);
		if (status)
			break;
		i40iw_get_used_rsrc(iwdev);
		dev->ccq_ops->ccq_arm(dev->ccq);
		status = i40iw_hmc_init_pble(&iwdev->sc_dev, iwdev->pble_rsrc);
		if (status)
			break;
		iwdev->virtchnl_wq = alloc_ordered_workqueue("iwvch", WQ_MEM_RECLAIM);
		i40iw_register_notifiers();
		iwdev->init_state = INET_NOTIFIER;
		status = i40iw_add_mac_ip(iwdev);
		if (status)
			break;
		iwdev->init_state = IP_ADDR_REGISTERED;
		if (i40iw_register_rdma_device(iwdev)) {
			i40iw_pr_err("register rdma device fail\n");
			break;
		};

		iwdev->init_state = RDMA_DEV_REGISTERED;
		iwdev->iw_status = 1;
		i40iw_port_ibevent(iwdev);
		iwdev->param_wq = alloc_ordered_workqueue("l2params", WQ_MEM_RECLAIM);
		if(iwdev->param_wq == NULL)
			break;
		i40iw_pr_info("i40iw_open completed\n");
		return 0;
	} while (0);

	i40iw_pr_err("status = %d last completion = %d\n", status, iwdev->init_state);
	i40iw_deinit_device(iwdev, false);
	return -ERESTART;
}

/**
 * i40iw_l2params_worker - worker for l2 params change
 * @work: work pointer for l2 params
 */
static void i40iw_l2params_worker(struct work_struct *work)
{
	struct l2params_work *dwork =
	    container_of(work, struct l2params_work, work);
	struct i40iw_device *iwdev = dwork->iwdev;

	i40iw_change_l2params(&iwdev->vsi, &dwork->l2params);
	atomic_dec(&iwdev->params_busy);
	kfree(work);
}

/**
 * i40iw_l2param_change - handle qs handles for qos and mss change
 * @ldev: lan device information
 * @client: client for paramater change
 * @params: new parameters from L2
 */
static void i40iw_l2param_change(struct i40e_info *ldev, struct i40e_client *client,
				 struct i40e_params *params)
{
	struct i40iw_handler *hdl;
	struct i40iw_l2params *l2params;
	struct l2params_work *work;
	struct i40iw_device *iwdev;
	int i;

	hdl = i40iw_find_i40e_handler(ldev);
	if (!hdl)
		return;

	iwdev = &hdl->device;

	if (atomic_read(&iwdev->params_busy))
		return;


	work = kzalloc(sizeof(*work), GFP_ATOMIC);
	if (!work)
		return;

	atomic_inc(&iwdev->params_busy);

	work->iwdev = iwdev;
	l2params = &work->l2params;
	for (i = 0; i < I40E_CLIENT_MAX_USER_PRIORITY; i++)
		l2params->qs_handle_list[i] = params->qos.prio_qos[i].qs_handle;

	l2params->mss = (params->mtu) ? params->mtu - I40IW_MTU_TO_MSS : iwdev->vsi.mss;

	INIT_WORK(&work->work, i40iw_l2params_worker);
	queue_work(iwdev->param_wq, &work->work);
}

/**
 * i40iw_close - client interface operation close for iwarp/uda device
 * @ldev: lan device information
 * @client: client to close
 *
 * Called by the lan driver during the processing of client unregister
 * Destroy and clean up the driver resources
 */
static void i40iw_close(struct i40e_info *ldev, struct i40e_client *client, bool reset)
{
	struct i40iw_device *iwdev;
	struct i40iw_handler *hdl;

	hdl = i40iw_find_i40e_handler(ldev);
	if (!hdl)
		return;

	iwdev = &hdl->device;
	iwdev->closing = true;

	i40iw_cm_disconnect_all(iwdev);
	destroy_workqueue(iwdev->virtchnl_wq);
	i40iw_deinit_device(iwdev, reset);
}

/**
 * i40iw_vf_reset - process VF reset
 * @ldev: lan device information
 * @client: client interface instance
 * @vf_id: virtual function id
 *
 * Called when a VF is reset by the PF
 * Destroy and clean up the VF resources
 */
static void i40iw_vf_reset(struct i40e_info *ldev, struct i40e_client *client, u32 vf_id)
{
	struct i40iw_handler *hdl;
	struct i40iw_sc_dev *dev;
	struct i40iw_hmc_fcn_info hmc_fcn_info;
	struct i40iw_virt_mem vf_dev_mem;
	struct i40iw_vfdev *tmp_vfdev;
	unsigned int i;
	unsigned long flags;
	struct i40iw_device *iwdev;

	hdl = i40iw_find_i40e_handler(ldev);
	if (!hdl)
		return;

	dev = &hdl->device.sc_dev;
	iwdev = (struct i40iw_device *)dev->back_dev;

	for (i = 0; i < I40IW_MAX_PE_ENABLED_VF_COUNT; i++) {
		if (!dev->vf_dev[i] || (dev->vf_dev[i]->vf_id != vf_id))
			continue;
		/* free all resources allocated on behalf of vf */
		tmp_vfdev = dev->vf_dev[i];
		spin_lock_irqsave(&iwdev->vsi.pestat->lock, flags);
		dev->vf_dev[i] = NULL;
		spin_unlock_irqrestore(&iwdev->vsi.pestat->lock, flags);
		i40iw_del_hmc_objects(dev, &tmp_vfdev->hmc_info, false, false);
		/* remove vf hmc function */
		memset(&hmc_fcn_info, 0, sizeof(hmc_fcn_info));
		hmc_fcn_info.vf_id = vf_id;
		hmc_fcn_info.iw_vf_idx = tmp_vfdev->iw_vf_idx;
		hmc_fcn_info.free_fcn = true;
		i40iw_cqp_manage_hmc_fcn_cmd(dev, &hmc_fcn_info);
		/* free vf_dev */
		vf_dev_mem.va = tmp_vfdev;
		vf_dev_mem.size = sizeof(struct i40iw_vfdev) +
					sizeof(struct i40iw_hmc_obj_info) * I40IW_HMC_IW_MAX;
		i40iw_free_virt_mem(dev->hw, &vf_dev_mem);
		break;
	}
}

/**
 * i40iw_vf_enable - enable a number of VFs
 * @ldev: lan device information
 * @client: client interface instance
 * @num_vfs: number of VFs for the PF
 *
 * Called when the number of VFs changes
 */
static void i40iw_vf_enable(struct i40e_info *ldev,
			    struct i40e_client *client,
			    u32 num_vfs)
{
	struct i40iw_handler *hdl;

	hdl = i40iw_find_i40e_handler(ldev);
	if (!hdl)
		return;

	if (num_vfs > I40IW_MAX_PE_ENABLED_VF_COUNT)
		hdl->device.max_enabled_vfs = I40IW_MAX_PE_ENABLED_VF_COUNT;
	else
		hdl->device.max_enabled_vfs = num_vfs;
}

/**
 * i40iw_vf_capable - check if VF capable
 * @ldev: lan device information
 * @client: client interface instance
 * @vf_id: virtual function id
 *
 * Return 1 if a VF slot is available or if VF is already RDMA enabled
 * Return 0 otherwise
 */
static int i40iw_vf_capable(struct i40e_info *ldev,
			    struct i40e_client *client,
			    u32 vf_id)
{
	struct i40iw_handler *hdl;
	struct i40iw_sc_dev *dev;
	unsigned int i;

	hdl = i40iw_find_i40e_handler(ldev);
	if (!hdl)
		return 0;

	dev = &hdl->device.sc_dev;

	for (i = 0; i < hdl->device.max_enabled_vfs; i++) {
		if (!dev->vf_dev[i] || (dev->vf_dev[i]->vf_id == vf_id))
			return 1;
	}

	return 0;
}

/**
 * i40iw_virtchnl_receive - receive a message through the virtual channel
 * @ldev: lan device information
 * @client: client interface instance
 * @vf_id: virtual function id associated with the message
 * @msg: message buffer pointer
 * @len: length of the message
 *
 * Invoke virtual channel receive operation for the given msg
 * Return 0 if successful, otherwise return error
 */
static int i40iw_virtchnl_receive(struct i40e_info *ldev,
				  struct i40e_client *client,
				  u32 vf_id,
				  u8 *msg,
				  u16 len)
{
	struct i40iw_handler *hdl;
	struct i40iw_sc_dev *dev;
	struct i40iw_device *iwdev;
	int ret_code = I40IW_NOT_SUPPORTED;

	if (!len || !msg)
		return I40IW_ERR_PARAM;

	hdl = i40iw_find_i40e_handler(ldev);
	if (!hdl)
		return I40IW_ERR_PARAM;

	dev = &hdl->device.sc_dev;
	iwdev = dev->back_dev;

	if (dev->vchnl_if.vchnl_recv) {
		ret_code = dev->vchnl_if.vchnl_recv(dev, vf_id, msg, len);
		if (!dev->is_pf) {
			atomic_dec(&iwdev->vchnl_msgs);
			wake_up(&iwdev->vchnl_waitq);
		}
	}
	return ret_code;
}

/**
 * i40iw_vf_clear_to_send - wait to send virtual channel message
 * @dev: iwarp device *
 * Wait for until virtual channel is clear
 * before sending the next message
 *
 * Returns false if error
 * Returns true if clear to send
 */
bool i40iw_vf_clear_to_send(struct i40iw_sc_dev *dev)
{
	struct i40iw_device *iwdev;
	wait_queue_t wait;

	iwdev = dev->back_dev;

	if (!wq_has_sleeper(&dev->vf_reqs) &&
	    (atomic_read(&iwdev->vchnl_msgs) == 0))
		return true; /* virtual channel is clear */

	init_wait(&wait);
	add_wait_queue_exclusive(&dev->vf_reqs, &wait);

	if (!wait_event_timeout(dev->vf_reqs,
				(atomic_read(&iwdev->vchnl_msgs) == 0),
				I40IW_VCHNL_EVENT_TIMEOUT))
		dev->vchnl_up = false;

	remove_wait_queue(&dev->vf_reqs, &wait);

	return dev->vchnl_up;
}

/**
 * i40iw_virtchnl_send - send a message through the virtual channel
 * @dev: iwarp device
 * @vf_id: virtual function id associated with the message
 * @msg: virtual channel message buffer pointer
 * @len: length of the message
 *
 * Invoke virtual channel send operation for the given msg
 * Return 0 if successful, otherwise return error
 */
static enum i40iw_status_code i40iw_virtchnl_send(struct i40iw_sc_dev *dev,
						  u32 vf_id,
						  u8 *msg,
						  u16 len)
{
	struct i40iw_device *iwdev;
	struct i40e_info *ldev;

	if (!dev || !dev->back_dev)
		return I40IW_ERR_BAD_PTR;

	iwdev = dev->back_dev;
	ldev = iwdev->ldev;

	if (ldev && ldev->ops && ldev->ops->virtchnl_send)
		return ldev->ops->virtchnl_send(ldev, &i40iw_client, vf_id, msg, len);
	return I40IW_ERR_BAD_PTR;
}

/* client interface functions */
static const struct i40e_client_ops i40e_ops = {
	.open = i40iw_open,
	.close = i40iw_close,
	.l2_param_change = i40iw_l2param_change,
	.virtchnl_receive = i40iw_virtchnl_receive,
	.vf_reset = i40iw_vf_reset,
	.vf_enable = i40iw_vf_enable,
	.vf_capable = i40iw_vf_capable
};

/**
 * i40iw_init_module - driver initialization function
 *
 * First function to call when the driver is loaded
 * Register the driver as i40e client and port mapper client
 */
static int __init i40iw_init_module(void)
{
	int ret;

	memset(&i40iw_client, 0, sizeof(i40iw_client));
	i40iw_client.version.major = CLIENT_IW_INTERFACE_VERSION_MAJOR;
	i40iw_client.version.minor = CLIENT_IW_INTERFACE_VERSION_MINOR;
	i40iw_client.version.build = CLIENT_IW_INTERFACE_VERSION_BUILD;
	i40iw_client.ops = &i40e_ops;
	memcpy(i40iw_client.name, i40iw_client_name, I40E_CLIENT_STR_LENGTH);
	i40iw_client.type = I40E_CLIENT_IWARP;
	spin_lock_init(&i40iw_handler_lock);
	ret = i40e_register_client(&i40iw_client);
	return ret;
}

/**
 * i40iw_exit_module - driver exit clean up function
 *
 * The function is called just before the driver is unloaded
 * Unregister the driver as i40e client and port mapper client
 */
static void __exit i40iw_exit_module(void)
{
	i40e_unregister_client(&i40iw_client);
}

module_init(i40iw_init_module);
module_exit(i40iw_exit_module);