ixgbe_lib.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include "ixgbe.h"
  4. #include "ixgbe_sriov.h"
  5. #ifdef CONFIG_IXGBE_DCB
  6. /**
  7. * ixgbe_cache_ring_dcb_sriov - Descriptor ring to register mapping for SR-IOV
  8. * @adapter: board private structure to initialize
  9. *
  10. * Cache the descriptor ring offsets for SR-IOV to the assigned rings. It
  11. * will also try to cache the proper offsets if RSS/FCoE are enabled along
  12. * with VMDq.
  13. *
  14. **/
  15. static bool ixgbe_cache_ring_dcb_sriov(struct ixgbe_adapter *adapter)
  16. {
  17. #ifdef IXGBE_FCOE
  18. struct ixgbe_ring_feature *fcoe = &adapter->ring_feature[RING_F_FCOE];
  19. #endif /* IXGBE_FCOE */
  20. struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
  21. int i;
  22. u16 reg_idx, pool;
  23. u8 tcs = adapter->hw_tcs;
  24. /* verify we have DCB queueing enabled before proceeding */
  25. if (tcs <= 1)
  26. return false;
  27. /* verify we have VMDq enabled before proceeding */
  28. if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
  29. return false;
  30. /* start at VMDq register offset for SR-IOV enabled setups */
  31. reg_idx = vmdq->offset * __ALIGN_MASK(1, ~vmdq->mask);
  32. for (i = 0, pool = 0; i < adapter->num_rx_queues; i++, reg_idx++) {
  33. /* If we are greater than indices move to next pool */
  34. if ((reg_idx & ~vmdq->mask) >= tcs) {
  35. pool++;
  36. reg_idx = __ALIGN_MASK(reg_idx, ~vmdq->mask);
  37. }
  38. adapter->rx_ring[i]->reg_idx = reg_idx;
  39. adapter->rx_ring[i]->netdev = pool ? NULL : adapter->netdev;
  40. }
  41. reg_idx = vmdq->offset * __ALIGN_MASK(1, ~vmdq->mask);
  42. for (i = 0; i < adapter->num_tx_queues; i++, reg_idx++) {
  43. /* If we are greater than indices move to next pool */
  44. if ((reg_idx & ~vmdq->mask) >= tcs)
  45. reg_idx = __ALIGN_MASK(reg_idx, ~vmdq->mask);
  46. adapter->tx_ring[i]->reg_idx = reg_idx;
  47. }
  48. #ifdef IXGBE_FCOE
  49. /* nothing to do if FCoE is disabled */
  50. if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
  51. return true;
  52. /* The work is already done if the FCoE ring is shared */
  53. if (fcoe->offset < tcs)
  54. return true;
  55. /* The FCoE rings exist separately, we need to move their reg_idx */
  56. if (fcoe->indices) {
  57. u16 queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
  58. u8 fcoe_tc = ixgbe_fcoe_get_tc(adapter);
  59. reg_idx = (vmdq->offset + vmdq->indices) * queues_per_pool;
  60. for (i = fcoe->offset; i < adapter->num_rx_queues; i++) {
  61. reg_idx = __ALIGN_MASK(reg_idx, ~vmdq->mask) + fcoe_tc;
  62. adapter->rx_ring[i]->reg_idx = reg_idx;
  63. adapter->rx_ring[i]->netdev = adapter->netdev;
  64. reg_idx++;
  65. }
  66. reg_idx = (vmdq->offset + vmdq->indices) * queues_per_pool;
  67. for (i = fcoe->offset; i < adapter->num_tx_queues; i++) {
  68. reg_idx = __ALIGN_MASK(reg_idx, ~vmdq->mask) + fcoe_tc;
  69. adapter->tx_ring[i]->reg_idx = reg_idx;
  70. reg_idx++;
  71. }
  72. }
  73. #endif /* IXGBE_FCOE */
  74. return true;
  75. }
  76. /* ixgbe_get_first_reg_idx - Return first register index associated with ring */
  77. static void ixgbe_get_first_reg_idx(struct ixgbe_adapter *adapter, u8 tc,
  78. unsigned int *tx, unsigned int *rx)
  79. {
  80. struct ixgbe_hw *hw = &adapter->hw;
  81. u8 num_tcs = adapter->hw_tcs;
  82. *tx = 0;
  83. *rx = 0;
  84. switch (hw->mac.type) {
  85. case ixgbe_mac_82598EB:
  86. /* TxQs/TC: 4 RxQs/TC: 8 */
  87. *tx = tc << 2; /* 0, 4, 8, 12, 16, 20, 24, 28 */
  88. *rx = tc << 3; /* 0, 8, 16, 24, 32, 40, 48, 56 */
  89. break;
  90. case ixgbe_mac_82599EB:
  91. case ixgbe_mac_X540:
  92. case ixgbe_mac_X550:
  93. case ixgbe_mac_X550EM_x:
  94. case ixgbe_mac_x550em_a:
  95. if (num_tcs > 4) {
  96. /*
  97. * TCs : TC0/1 TC2/3 TC4-7
  98. * TxQs/TC: 32 16 8
  99. * RxQs/TC: 16 16 16
  100. */
  101. *rx = tc << 4;
  102. if (tc < 3)
  103. *tx = tc << 5; /* 0, 32, 64 */
  104. else if (tc < 5)
  105. *tx = (tc + 2) << 4; /* 80, 96 */
  106. else
  107. *tx = (tc + 8) << 3; /* 104, 112, 120 */
  108. } else {
  109. /*
  110. * TCs : TC0 TC1 TC2/3
  111. * TxQs/TC: 64 32 16
  112. * RxQs/TC: 32 32 32
  113. */
  114. *rx = tc << 5;
  115. if (tc < 2)
  116. *tx = tc << 6; /* 0, 64 */
  117. else
  118. *tx = (tc + 4) << 4; /* 96, 112 */
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. /**
  126. * ixgbe_cache_ring_dcb - Descriptor ring to register mapping for DCB
  127. * @adapter: board private structure to initialize
  128. *
  129. * Cache the descriptor ring offsets for DCB to the assigned rings.
  130. *
  131. **/
  132. static bool ixgbe_cache_ring_dcb(struct ixgbe_adapter *adapter)
  133. {
  134. u8 num_tcs = adapter->hw_tcs;
  135. unsigned int tx_idx, rx_idx;
  136. int tc, offset, rss_i, i;
  137. /* verify we have DCB queueing enabled before proceeding */
  138. if (num_tcs <= 1)
  139. return false;
  140. rss_i = adapter->ring_feature[RING_F_RSS].indices;
  141. for (tc = 0, offset = 0; tc < num_tcs; tc++, offset += rss_i) {
  142. ixgbe_get_first_reg_idx(adapter, tc, &tx_idx, &rx_idx);
  143. for (i = 0; i < rss_i; i++, tx_idx++, rx_idx++) {
  144. adapter->tx_ring[offset + i]->reg_idx = tx_idx;
  145. adapter->rx_ring[offset + i]->reg_idx = rx_idx;
  146. adapter->rx_ring[offset + i]->netdev = adapter->netdev;
  147. adapter->tx_ring[offset + i]->dcb_tc = tc;
  148. adapter->rx_ring[offset + i]->dcb_tc = tc;
  149. }
  150. }
  151. return true;
  152. }
  153. #endif
  154. /**
  155. * ixgbe_cache_ring_sriov - Descriptor ring to register mapping for sriov
  156. * @adapter: board private structure to initialize
  157. *
  158. * SR-IOV doesn't use any descriptor rings but changes the default if
  159. * no other mapping is used.
  160. *
  161. */
  162. static bool ixgbe_cache_ring_sriov(struct ixgbe_adapter *adapter)
  163. {
  164. #ifdef IXGBE_FCOE
  165. struct ixgbe_ring_feature *fcoe = &adapter->ring_feature[RING_F_FCOE];
  166. #endif /* IXGBE_FCOE */
  167. struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
  168. struct ixgbe_ring_feature *rss = &adapter->ring_feature[RING_F_RSS];
  169. u16 reg_idx, pool;
  170. int i;
  171. /* only proceed if VMDq is enabled */
  172. if (!(adapter->flags & IXGBE_FLAG_VMDQ_ENABLED))
  173. return false;
  174. /* start at VMDq register offset for SR-IOV enabled setups */
  175. pool = 0;
  176. reg_idx = vmdq->offset * __ALIGN_MASK(1, ~vmdq->mask);
  177. for (i = 0; i < adapter->num_rx_queues; i++, reg_idx++) {
  178. #ifdef IXGBE_FCOE
  179. /* Allow first FCoE queue to be mapped as RSS */
  180. if (fcoe->offset && (i > fcoe->offset))
  181. break;
  182. #endif
  183. /* If we are greater than indices move to next pool */
  184. if ((reg_idx & ~vmdq->mask) >= rss->indices) {
  185. pool++;
  186. reg_idx = __ALIGN_MASK(reg_idx, ~vmdq->mask);
  187. }
  188. adapter->rx_ring[i]->reg_idx = reg_idx;
  189. adapter->rx_ring[i]->netdev = pool ? NULL : adapter->netdev;
  190. }
  191. #ifdef IXGBE_FCOE
  192. /* FCoE uses a linear block of queues so just assigning 1:1 */
  193. for (; i < adapter->num_rx_queues; i++, reg_idx++) {
  194. adapter->rx_ring[i]->reg_idx = reg_idx;
  195. adapter->rx_ring[i]->netdev = adapter->netdev;
  196. }
  197. #endif
  198. reg_idx = vmdq->offset * __ALIGN_MASK(1, ~vmdq->mask);
  199. for (i = 0; i < adapter->num_tx_queues; i++, reg_idx++) {
  200. #ifdef IXGBE_FCOE
  201. /* Allow first FCoE queue to be mapped as RSS */
  202. if (fcoe->offset && (i > fcoe->offset))
  203. break;
  204. #endif
  205. /* If we are greater than indices move to next pool */
  206. if ((reg_idx & rss->mask) >= rss->indices)
  207. reg_idx = __ALIGN_MASK(reg_idx, ~vmdq->mask);
  208. adapter->tx_ring[i]->reg_idx = reg_idx;
  209. }
  210. #ifdef IXGBE_FCOE
  211. /* FCoE uses a linear block of queues so just assigning 1:1 */
  212. for (; i < adapter->num_tx_queues; i++, reg_idx++)
  213. adapter->tx_ring[i]->reg_idx = reg_idx;
  214. #endif
  215. return true;
  216. }
  217. /**
  218. * ixgbe_cache_ring_rss - Descriptor ring to register mapping for RSS
  219. * @adapter: board private structure to initialize
  220. *
  221. * Cache the descriptor ring offsets for RSS to the assigned rings.
  222. *
  223. **/
  224. static bool ixgbe_cache_ring_rss(struct ixgbe_adapter *adapter)
  225. {
  226. int i, reg_idx;
  227. for (i = 0; i < adapter->num_rx_queues; i++) {
  228. adapter->rx_ring[i]->reg_idx = i;
  229. adapter->rx_ring[i]->netdev = adapter->netdev;
  230. }
  231. for (i = 0, reg_idx = 0; i < adapter->num_tx_queues; i++, reg_idx++)
  232. adapter->tx_ring[i]->reg_idx = reg_idx;
  233. for (i = 0; i < adapter->num_xdp_queues; i++, reg_idx++)
  234. adapter->xdp_ring[i]->reg_idx = reg_idx;
  235. return true;
  236. }
  237. /**
  238. * ixgbe_cache_ring_register - Descriptor ring to register mapping
  239. * @adapter: board private structure to initialize
  240. *
  241. * Once we know the feature-set enabled for the device, we'll cache
  242. * the register offset the descriptor ring is assigned to.
  243. *
  244. * Note, the order the various feature calls is important. It must start with
  245. * the "most" features enabled at the same time, then trickle down to the
  246. * least amount of features turned on at once.
  247. **/
  248. static void ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
  249. {
  250. /* start with default case */
  251. adapter->rx_ring[0]->reg_idx = 0;
  252. adapter->tx_ring[0]->reg_idx = 0;
  253. #ifdef CONFIG_IXGBE_DCB
  254. if (ixgbe_cache_ring_dcb_sriov(adapter))
  255. return;
  256. if (ixgbe_cache_ring_dcb(adapter))
  257. return;
  258. #endif
  259. if (ixgbe_cache_ring_sriov(adapter))
  260. return;
  261. ixgbe_cache_ring_rss(adapter);
  262. }
  263. static int ixgbe_xdp_queues(struct ixgbe_adapter *adapter)
  264. {
  265. int queues;
  266. queues = min_t(int, IXGBE_MAX_XDP_QS, nr_cpu_ids);
  267. return adapter->xdp_prog ? queues : 0;
  268. }
  269. #define IXGBE_RSS_64Q_MASK 0x3F
  270. #define IXGBE_RSS_16Q_MASK 0xF
  271. #define IXGBE_RSS_8Q_MASK 0x7
  272. #define IXGBE_RSS_4Q_MASK 0x3
  273. #define IXGBE_RSS_2Q_MASK 0x1
  274. #define IXGBE_RSS_DISABLED_MASK 0x0
  275. #ifdef CONFIG_IXGBE_DCB
  276. /**
  277. * ixgbe_set_dcb_sriov_queues: Allocate queues for SR-IOV devices w/ DCB
  278. * @adapter: board private structure to initialize
  279. *
  280. * When SR-IOV (Single Root IO Virtualiztion) is enabled, allocate queues
  281. * and VM pools where appropriate. Also assign queues based on DCB
  282. * priorities and map accordingly..
  283. *
  284. **/
  285. static bool ixgbe_set_dcb_sriov_queues(struct ixgbe_adapter *adapter)
  286. {
  287. int i;
  288. u16 vmdq_i = adapter->ring_feature[RING_F_VMDQ].limit;
  289. u16 vmdq_m = 0;
  290. #ifdef IXGBE_FCOE
  291. u16 fcoe_i = 0;
  292. #endif
  293. u8 tcs = adapter->hw_tcs;
  294. /* verify we have DCB queueing enabled before proceeding */
  295. if (tcs <= 1)
  296. return false;
  297. /* verify we have VMDq enabled before proceeding */
  298. if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
  299. return false;
  300. /* limit VMDq instances on the PF by number of Tx queues */
  301. vmdq_i = min_t(u16, vmdq_i, MAX_TX_QUEUES / tcs);
  302. /* Add starting offset to total pool count */
  303. vmdq_i += adapter->ring_feature[RING_F_VMDQ].offset;
  304. /* 16 pools w/ 8 TC per pool */
  305. if (tcs > 4) {
  306. vmdq_i = min_t(u16, vmdq_i, 16);
  307. vmdq_m = IXGBE_82599_VMDQ_8Q_MASK;
  308. /* 32 pools w/ 4 TC per pool */
  309. } else {
  310. vmdq_i = min_t(u16, vmdq_i, 32);
  311. vmdq_m = IXGBE_82599_VMDQ_4Q_MASK;
  312. }
  313. #ifdef IXGBE_FCOE
  314. /* queues in the remaining pools are available for FCoE */
  315. fcoe_i = (128 / __ALIGN_MASK(1, ~vmdq_m)) - vmdq_i;
  316. #endif
  317. /* remove the starting offset from the pool count */
  318. vmdq_i -= adapter->ring_feature[RING_F_VMDQ].offset;
  319. /* save features for later use */
  320. adapter->ring_feature[RING_F_VMDQ].indices = vmdq_i;
  321. adapter->ring_feature[RING_F_VMDQ].mask = vmdq_m;
  322. /*
  323. * We do not support DCB, VMDq, and RSS all simultaneously
  324. * so we will disable RSS since it is the lowest priority
  325. */
  326. adapter->ring_feature[RING_F_RSS].indices = 1;
  327. adapter->ring_feature[RING_F_RSS].mask = IXGBE_RSS_DISABLED_MASK;
  328. /* disable ATR as it is not supported when VMDq is enabled */
  329. adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
  330. adapter->num_rx_pools = vmdq_i;
  331. adapter->num_rx_queues_per_pool = tcs;
  332. adapter->num_tx_queues = vmdq_i * tcs;
  333. adapter->num_xdp_queues = 0;
  334. adapter->num_rx_queues = vmdq_i * tcs;
  335. #ifdef IXGBE_FCOE
  336. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
  337. struct ixgbe_ring_feature *fcoe;
  338. fcoe = &adapter->ring_feature[RING_F_FCOE];
  339. /* limit ourselves based on feature limits */
  340. fcoe_i = min_t(u16, fcoe_i, fcoe->limit);
  341. if (fcoe_i) {
  342. /* alloc queues for FCoE separately */
  343. fcoe->indices = fcoe_i;
  344. fcoe->offset = vmdq_i * tcs;
  345. /* add queues to adapter */
  346. adapter->num_tx_queues += fcoe_i;
  347. adapter->num_rx_queues += fcoe_i;
  348. } else if (tcs > 1) {
  349. /* use queue belonging to FcoE TC */
  350. fcoe->indices = 1;
  351. fcoe->offset = ixgbe_fcoe_get_tc(adapter);
  352. } else {
  353. adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
  354. fcoe->indices = 0;
  355. fcoe->offset = 0;
  356. }
  357. }
  358. #endif /* IXGBE_FCOE */
  359. /* configure TC to queue mapping */
  360. for (i = 0; i < tcs; i++)
  361. netdev_set_tc_queue(adapter->netdev, i, 1, i);
  362. return true;
  363. }
  364. static bool ixgbe_set_dcb_queues(struct ixgbe_adapter *adapter)
  365. {
  366. struct net_device *dev = adapter->netdev;
  367. struct ixgbe_ring_feature *f;
  368. int rss_i, rss_m, i;
  369. int tcs;
  370. /* Map queue offset and counts onto allocated tx queues */
  371. tcs = adapter->hw_tcs;
  372. /* verify we have DCB queueing enabled before proceeding */
  373. if (tcs <= 1)
  374. return false;
  375. /* determine the upper limit for our current DCB mode */
  376. rss_i = dev->num_tx_queues / tcs;
  377. if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
  378. /* 8 TC w/ 4 queues per TC */
  379. rss_i = min_t(u16, rss_i, 4);
  380. rss_m = IXGBE_RSS_4Q_MASK;
  381. } else if (tcs > 4) {
  382. /* 8 TC w/ 8 queues per TC */
  383. rss_i = min_t(u16, rss_i, 8);
  384. rss_m = IXGBE_RSS_8Q_MASK;
  385. } else {
  386. /* 4 TC w/ 16 queues per TC */
  387. rss_i = min_t(u16, rss_i, 16);
  388. rss_m = IXGBE_RSS_16Q_MASK;
  389. }
  390. /* set RSS mask and indices */
  391. f = &adapter->ring_feature[RING_F_RSS];
  392. rss_i = min_t(int, rss_i, f->limit);
  393. f->indices = rss_i;
  394. f->mask = rss_m;
  395. /* disable ATR as it is not supported when multiple TCs are enabled */
  396. adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
  397. #ifdef IXGBE_FCOE
  398. /* FCoE enabled queues require special configuration indexed
  399. * by feature specific indices and offset. Here we map FCoE
  400. * indices onto the DCB queue pairs allowing FCoE to own
  401. * configuration later.
  402. */
  403. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
  404. u8 tc = ixgbe_fcoe_get_tc(adapter);
  405. f = &adapter->ring_feature[RING_F_FCOE];
  406. f->indices = min_t(u16, rss_i, f->limit);
  407. f->offset = rss_i * tc;
  408. }
  409. #endif /* IXGBE_FCOE */
  410. for (i = 0; i < tcs; i++)
  411. netdev_set_tc_queue(dev, i, rss_i, rss_i * i);
  412. adapter->num_tx_queues = rss_i * tcs;
  413. adapter->num_xdp_queues = 0;
  414. adapter->num_rx_queues = rss_i * tcs;
  415. return true;
  416. }
  417. #endif
  418. /**
  419. * ixgbe_set_sriov_queues - Allocate queues for SR-IOV devices
  420. * @adapter: board private structure to initialize
  421. *
  422. * When SR-IOV (Single Root IO Virtualiztion) is enabled, allocate queues
  423. * and VM pools where appropriate. If RSS is available, then also try and
  424. * enable RSS and map accordingly.
  425. *
  426. **/
  427. static bool ixgbe_set_sriov_queues(struct ixgbe_adapter *adapter)
  428. {
  429. u16 vmdq_i = adapter->ring_feature[RING_F_VMDQ].limit;
  430. u16 vmdq_m = 0;
  431. u16 rss_i = adapter->ring_feature[RING_F_RSS].limit;
  432. u16 rss_m = IXGBE_RSS_DISABLED_MASK;
  433. #ifdef IXGBE_FCOE
  434. u16 fcoe_i = 0;
  435. #endif
  436. /* only proceed if SR-IOV is enabled */
  437. if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
  438. return false;
  439. /* limit l2fwd RSS based on total Tx queue limit */
  440. rss_i = min_t(u16, rss_i, MAX_TX_QUEUES / vmdq_i);
  441. /* Add starting offset to total pool count */
  442. vmdq_i += adapter->ring_feature[RING_F_VMDQ].offset;
  443. /* double check we are limited to maximum pools */
  444. vmdq_i = min_t(u16, IXGBE_MAX_VMDQ_INDICES, vmdq_i);
  445. /* 64 pool mode with 2 queues per pool */
  446. if (vmdq_i > 32) {
  447. vmdq_m = IXGBE_82599_VMDQ_2Q_MASK;
  448. rss_m = IXGBE_RSS_2Q_MASK;
  449. rss_i = min_t(u16, rss_i, 2);
  450. /* 32 pool mode with up to 4 queues per pool */
  451. } else {
  452. vmdq_m = IXGBE_82599_VMDQ_4Q_MASK;
  453. rss_m = IXGBE_RSS_4Q_MASK;
  454. /* We can support 4, 2, or 1 queues */
  455. rss_i = (rss_i > 3) ? 4 : (rss_i > 1) ? 2 : 1;
  456. }
  457. #ifdef IXGBE_FCOE
  458. /* queues in the remaining pools are available for FCoE */
  459. fcoe_i = 128 - (vmdq_i * __ALIGN_MASK(1, ~vmdq_m));
  460. #endif
  461. /* remove the starting offset from the pool count */
  462. vmdq_i -= adapter->ring_feature[RING_F_VMDQ].offset;
  463. /* save features for later use */
  464. adapter->ring_feature[RING_F_VMDQ].indices = vmdq_i;
  465. adapter->ring_feature[RING_F_VMDQ].mask = vmdq_m;
  466. /* limit RSS based on user input and save for later use */
  467. adapter->ring_feature[RING_F_RSS].indices = rss_i;
  468. adapter->ring_feature[RING_F_RSS].mask = rss_m;
  469. adapter->num_rx_pools = vmdq_i;
  470. adapter->num_rx_queues_per_pool = rss_i;
  471. adapter->num_rx_queues = vmdq_i * rss_i;
  472. adapter->num_tx_queues = vmdq_i * rss_i;
  473. adapter->num_xdp_queues = 0;
  474. /* disable ATR as it is not supported when VMDq is enabled */
  475. adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
  476. #ifdef IXGBE_FCOE
  477. /*
  478. * FCoE can use rings from adjacent buffers to allow RSS
  479. * like behavior. To account for this we need to add the
  480. * FCoE indices to the total ring count.
  481. */
  482. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
  483. struct ixgbe_ring_feature *fcoe;
  484. fcoe = &adapter->ring_feature[RING_F_FCOE];
  485. /* limit ourselves based on feature limits */
  486. fcoe_i = min_t(u16, fcoe_i, fcoe->limit);
  487. if (vmdq_i > 1 && fcoe_i) {
  488. /* alloc queues for FCoE separately */
  489. fcoe->indices = fcoe_i;
  490. fcoe->offset = vmdq_i * rss_i;
  491. } else {
  492. /* merge FCoE queues with RSS queues */
  493. fcoe_i = min_t(u16, fcoe_i + rss_i, num_online_cpus());
  494. /* limit indices to rss_i if MSI-X is disabled */
  495. if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
  496. fcoe_i = rss_i;
  497. /* attempt to reserve some queues for just FCoE */
  498. fcoe->indices = min_t(u16, fcoe_i, fcoe->limit);
  499. fcoe->offset = fcoe_i - fcoe->indices;
  500. fcoe_i -= rss_i;
  501. }
  502. /* add queues to adapter */
  503. adapter->num_tx_queues += fcoe_i;
  504. adapter->num_rx_queues += fcoe_i;
  505. }
  506. #endif
  507. /* To support macvlan offload we have to use num_tc to
  508. * restrict the queues that can be used by the device.
  509. * By doing this we can avoid reporting a false number of
  510. * queues.
  511. */
  512. if (vmdq_i > 1)
  513. netdev_set_num_tc(adapter->netdev, 1);
  514. /* populate TC0 for use by pool 0 */
  515. netdev_set_tc_queue(adapter->netdev, 0,
  516. adapter->num_rx_queues_per_pool, 0);
  517. return true;
  518. }
  519. /**
  520. * ixgbe_set_rss_queues - Allocate queues for RSS
  521. * @adapter: board private structure to initialize
  522. *
  523. * This is our "base" multiqueue mode. RSS (Receive Side Scaling) will try
  524. * to allocate one Rx queue per CPU, and if available, one Tx queue per CPU.
  525. *
  526. **/
  527. static bool ixgbe_set_rss_queues(struct ixgbe_adapter *adapter)
  528. {
  529. struct ixgbe_hw *hw = &adapter->hw;
  530. struct ixgbe_ring_feature *f;
  531. u16 rss_i;
  532. /* set mask for 16 queue limit of RSS */
  533. f = &adapter->ring_feature[RING_F_RSS];
  534. rss_i = f->limit;
  535. f->indices = rss_i;
  536. if (hw->mac.type < ixgbe_mac_X550)
  537. f->mask = IXGBE_RSS_16Q_MASK;
  538. else
  539. f->mask = IXGBE_RSS_64Q_MASK;
  540. /* disable ATR by default, it will be configured below */
  541. adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
  542. /*
  543. * Use Flow Director in addition to RSS to ensure the best
  544. * distribution of flows across cores, even when an FDIR flow
  545. * isn't matched.
  546. */
  547. if (rss_i > 1 && adapter->atr_sample_rate) {
  548. f = &adapter->ring_feature[RING_F_FDIR];
  549. rss_i = f->indices = f->limit;
  550. if (!(adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE))
  551. adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
  552. }
  553. #ifdef IXGBE_FCOE
  554. /*
  555. * FCoE can exist on the same rings as standard network traffic
  556. * however it is preferred to avoid that if possible. In order
  557. * to get the best performance we allocate as many FCoE queues
  558. * as we can and we place them at the end of the ring array to
  559. * avoid sharing queues with standard RSS on systems with 24 or
  560. * more CPUs.
  561. */
  562. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
  563. struct net_device *dev = adapter->netdev;
  564. u16 fcoe_i;
  565. f = &adapter->ring_feature[RING_F_FCOE];
  566. /* merge FCoE queues with RSS queues */
  567. fcoe_i = min_t(u16, f->limit + rss_i, num_online_cpus());
  568. fcoe_i = min_t(u16, fcoe_i, dev->num_tx_queues);
  569. /* limit indices to rss_i if MSI-X is disabled */
  570. if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
  571. fcoe_i = rss_i;
  572. /* attempt to reserve some queues for just FCoE */
  573. f->indices = min_t(u16, fcoe_i, f->limit);
  574. f->offset = fcoe_i - f->indices;
  575. rss_i = max_t(u16, fcoe_i, rss_i);
  576. }
  577. #endif /* IXGBE_FCOE */
  578. adapter->num_rx_queues = rss_i;
  579. adapter->num_tx_queues = rss_i;
  580. adapter->num_xdp_queues = ixgbe_xdp_queues(adapter);
  581. return true;
  582. }
  583. /**
  584. * ixgbe_set_num_queues - Allocate queues for device, feature dependent
  585. * @adapter: board private structure to initialize
  586. *
  587. * This is the top level queue allocation routine. The order here is very
  588. * important, starting with the "most" number of features turned on at once,
  589. * and ending with the smallest set of features. This way large combinations
  590. * can be allocated if they're turned on, and smaller combinations are the
  591. * fallthrough conditions.
  592. *
  593. **/
  594. static void ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
  595. {
  596. /* Start with base case */
  597. adapter->num_rx_queues = 1;
  598. adapter->num_tx_queues = 1;
  599. adapter->num_xdp_queues = 0;
  600. adapter->num_rx_pools = 1;
  601. adapter->num_rx_queues_per_pool = 1;
  602. #ifdef CONFIG_IXGBE_DCB
  603. if (ixgbe_set_dcb_sriov_queues(adapter))
  604. return;
  605. if (ixgbe_set_dcb_queues(adapter))
  606. return;
  607. #endif
  608. if (ixgbe_set_sriov_queues(adapter))
  609. return;
  610. ixgbe_set_rss_queues(adapter);
  611. }
  612. /**
  613. * ixgbe_acquire_msix_vectors - acquire MSI-X vectors
  614. * @adapter: board private structure
  615. *
  616. * Attempts to acquire a suitable range of MSI-X vector interrupts. Will
  617. * return a negative error code if unable to acquire MSI-X vectors for any
  618. * reason.
  619. */
  620. static int ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter)
  621. {
  622. struct ixgbe_hw *hw = &adapter->hw;
  623. int i, vectors, vector_threshold;
  624. /* We start by asking for one vector per queue pair with XDP queues
  625. * being stacked with TX queues.
  626. */
  627. vectors = max(adapter->num_rx_queues, adapter->num_tx_queues);
  628. vectors = max(vectors, adapter->num_xdp_queues);
  629. /* It is easy to be greedy for MSI-X vectors. However, it really
  630. * doesn't do much good if we have a lot more vectors than CPUs. We'll
  631. * be somewhat conservative and only ask for (roughly) the same number
  632. * of vectors as there are CPUs.
  633. */
  634. vectors = min_t(int, vectors, num_online_cpus());
  635. /* Some vectors are necessary for non-queue interrupts */
  636. vectors += NON_Q_VECTORS;
  637. /* Hardware can only support a maximum of hw.mac->max_msix_vectors.
  638. * With features such as RSS and VMDq, we can easily surpass the
  639. * number of Rx and Tx descriptor queues supported by our device.
  640. * Thus, we cap the maximum in the rare cases where the CPU count also
  641. * exceeds our vector limit
  642. */
  643. vectors = min_t(int, vectors, hw->mac.max_msix_vectors);
  644. /* We want a minimum of two MSI-X vectors for (1) a TxQ[0] + RxQ[0]
  645. * handler, and (2) an Other (Link Status Change, etc.) handler.
  646. */
  647. vector_threshold = MIN_MSIX_COUNT;
  648. adapter->msix_entries = kcalloc(vectors,
  649. sizeof(struct msix_entry),
  650. GFP_KERNEL);
  651. if (!adapter->msix_entries)
  652. return -ENOMEM;
  653. for (i = 0; i < vectors; i++)
  654. adapter->msix_entries[i].entry = i;
  655. vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
  656. vector_threshold, vectors);
  657. if (vectors < 0) {
  658. /* A negative count of allocated vectors indicates an error in
  659. * acquiring within the specified range of MSI-X vectors
  660. */
  661. e_dev_warn("Failed to allocate MSI-X interrupts. Err: %d\n",
  662. vectors);
  663. adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
  664. kfree(adapter->msix_entries);
  665. adapter->msix_entries = NULL;
  666. return vectors;
  667. }
  668. /* we successfully allocated some number of vectors within our
  669. * requested range.
  670. */
  671. adapter->flags |= IXGBE_FLAG_MSIX_ENABLED;
  672. /* Adjust for only the vectors we'll use, which is minimum
  673. * of max_q_vectors, or the number of vectors we were allocated.
  674. */
  675. vectors -= NON_Q_VECTORS;
  676. adapter->num_q_vectors = min_t(int, vectors, adapter->max_q_vectors);
  677. return 0;
  678. }
  679. static void ixgbe_add_ring(struct ixgbe_ring *ring,
  680. struct ixgbe_ring_container *head)
  681. {
  682. ring->next = head->ring;
  683. head->ring = ring;
  684. head->count++;
  685. head->next_update = jiffies + 1;
  686. }
  687. /**
  688. * ixgbe_alloc_q_vector - Allocate memory for a single interrupt vector
  689. * @adapter: board private structure to initialize
  690. * @v_count: q_vectors allocated on adapter, used for ring interleaving
  691. * @v_idx: index of vector in adapter struct
  692. * @txr_count: total number of Tx rings to allocate
  693. * @txr_idx: index of first Tx ring to allocate
  694. * @xdp_count: total number of XDP rings to allocate
  695. * @xdp_idx: index of first XDP ring to allocate
  696. * @rxr_count: total number of Rx rings to allocate
  697. * @rxr_idx: index of first Rx ring to allocate
  698. *
  699. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  700. **/
  701. static int ixgbe_alloc_q_vector(struct ixgbe_adapter *adapter,
  702. int v_count, int v_idx,
  703. int txr_count, int txr_idx,
  704. int xdp_count, int xdp_idx,
  705. int rxr_count, int rxr_idx)
  706. {
  707. int node = dev_to_node(&adapter->pdev->dev);
  708. struct ixgbe_q_vector *q_vector;
  709. struct ixgbe_ring *ring;
  710. int cpu = -1;
  711. int ring_count;
  712. u8 tcs = adapter->hw_tcs;
  713. ring_count = txr_count + rxr_count + xdp_count;
  714. /* customize cpu for Flow Director mapping */
  715. if ((tcs <= 1) && !(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)) {
  716. u16 rss_i = adapter->ring_feature[RING_F_RSS].indices;
  717. if (rss_i > 1 && adapter->atr_sample_rate) {
  718. cpu = cpumask_local_spread(v_idx, node);
  719. node = cpu_to_node(cpu);
  720. }
  721. }
  722. /* allocate q_vector and rings */
  723. q_vector = kzalloc_node(struct_size(q_vector, ring, ring_count),
  724. GFP_KERNEL, node);
  725. if (!q_vector)
  726. q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
  727. GFP_KERNEL);
  728. if (!q_vector)
  729. return -ENOMEM;
  730. /* setup affinity mask and node */
  731. if (cpu != -1)
  732. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  733. q_vector->numa_node = node;
  734. #ifdef CONFIG_IXGBE_DCA
  735. /* initialize CPU for DCA */
  736. q_vector->cpu = -1;
  737. #endif
  738. /* initialize NAPI */
  739. netif_napi_add(adapter->netdev, &q_vector->napi, ixgbe_poll);
  740. /* tie q_vector and adapter together */
  741. adapter->q_vector[v_idx] = q_vector;
  742. q_vector->adapter = adapter;
  743. q_vector->v_idx = v_idx;
  744. /* initialize work limits */
  745. q_vector->tx.work_limit = adapter->tx_work_limit;
  746. /* Initialize setting for adaptive ITR */
  747. q_vector->tx.itr = IXGBE_ITR_ADAPTIVE_MAX_USECS |
  748. IXGBE_ITR_ADAPTIVE_LATENCY;
  749. q_vector->rx.itr = IXGBE_ITR_ADAPTIVE_MAX_USECS |
  750. IXGBE_ITR_ADAPTIVE_LATENCY;
  751. /* intialize ITR */
  752. if (txr_count && !rxr_count) {
  753. /* tx only vector */
  754. if (adapter->tx_itr_setting == 1)
  755. q_vector->itr = IXGBE_12K_ITR;
  756. else
  757. q_vector->itr = adapter->tx_itr_setting;
  758. } else {
  759. /* rx or rx/tx vector */
  760. if (adapter->rx_itr_setting == 1)
  761. q_vector->itr = IXGBE_20K_ITR;
  762. else
  763. q_vector->itr = adapter->rx_itr_setting;
  764. }
  765. /* initialize pointer to rings */
  766. ring = q_vector->ring;
  767. while (txr_count) {
  768. /* assign generic ring traits */
  769. ring->dev = &adapter->pdev->dev;
  770. ring->netdev = adapter->netdev;
  771. /* configure backlink on ring */
  772. ring->q_vector = q_vector;
  773. /* update q_vector Tx values */
  774. ixgbe_add_ring(ring, &q_vector->tx);
  775. /* apply Tx specific ring traits */
  776. ring->count = adapter->tx_ring_count;
  777. ring->queue_index = txr_idx;
  778. /* assign ring to adapter */
  779. WRITE_ONCE(adapter->tx_ring[txr_idx], ring);
  780. /* update count and index */
  781. txr_count--;
  782. txr_idx += v_count;
  783. /* push pointer to next ring */
  784. ring++;
  785. }
  786. while (xdp_count) {
  787. /* assign generic ring traits */
  788. ring->dev = &adapter->pdev->dev;
  789. ring->netdev = adapter->netdev;
  790. /* configure backlink on ring */
  791. ring->q_vector = q_vector;
  792. /* update q_vector Tx values */
  793. ixgbe_add_ring(ring, &q_vector->tx);
  794. /* apply Tx specific ring traits */
  795. ring->count = adapter->tx_ring_count;
  796. ring->queue_index = xdp_idx;
  797. set_ring_xdp(ring);
  798. spin_lock_init(&ring->tx_lock);
  799. /* assign ring to adapter */
  800. WRITE_ONCE(adapter->xdp_ring[xdp_idx], ring);
  801. /* update count and index */
  802. xdp_count--;
  803. xdp_idx++;
  804. /* push pointer to next ring */
  805. ring++;
  806. }
  807. while (rxr_count) {
  808. /* assign generic ring traits */
  809. ring->dev = &adapter->pdev->dev;
  810. ring->netdev = adapter->netdev;
  811. /* configure backlink on ring */
  812. ring->q_vector = q_vector;
  813. /* update q_vector Rx values */
  814. ixgbe_add_ring(ring, &q_vector->rx);
  815. /*
  816. * 82599 errata, UDP frames with a 0 checksum
  817. * can be marked as checksum errors.
  818. */
  819. if (adapter->hw.mac.type == ixgbe_mac_82599EB)
  820. set_bit(__IXGBE_RX_CSUM_UDP_ZERO_ERR, &ring->state);
  821. #ifdef IXGBE_FCOE
  822. if (adapter->netdev->fcoe_mtu) {
  823. struct ixgbe_ring_feature *f;
  824. f = &adapter->ring_feature[RING_F_FCOE];
  825. if ((rxr_idx >= f->offset) &&
  826. (rxr_idx < f->offset + f->indices))
  827. set_bit(__IXGBE_RX_FCOE, &ring->state);
  828. }
  829. #endif /* IXGBE_FCOE */
  830. /* apply Rx specific ring traits */
  831. ring->count = adapter->rx_ring_count;
  832. ring->queue_index = rxr_idx;
  833. /* assign ring to adapter */
  834. WRITE_ONCE(adapter->rx_ring[rxr_idx], ring);
  835. /* update count and index */
  836. rxr_count--;
  837. rxr_idx += v_count;
  838. /* push pointer to next ring */
  839. ring++;
  840. }
  841. return 0;
  842. }
  843. /**
  844. * ixgbe_free_q_vector - Free memory allocated for specific interrupt vector
  845. * @adapter: board private structure to initialize
  846. * @v_idx: Index of vector to be freed
  847. *
  848. * This function frees the memory allocated to the q_vector. In addition if
  849. * NAPI is enabled it will delete any references to the NAPI struct prior
  850. * to freeing the q_vector.
  851. **/
  852. static void ixgbe_free_q_vector(struct ixgbe_adapter *adapter, int v_idx)
  853. {
  854. struct ixgbe_q_vector *q_vector = adapter->q_vector[v_idx];
  855. struct ixgbe_ring *ring;
  856. ixgbe_for_each_ring(ring, q_vector->tx) {
  857. if (ring_is_xdp(ring))
  858. WRITE_ONCE(adapter->xdp_ring[ring->queue_index], NULL);
  859. else
  860. WRITE_ONCE(adapter->tx_ring[ring->queue_index], NULL);
  861. }
  862. ixgbe_for_each_ring(ring, q_vector->rx)
  863. WRITE_ONCE(adapter->rx_ring[ring->queue_index], NULL);
  864. adapter->q_vector[v_idx] = NULL;
  865. __netif_napi_del(&q_vector->napi);
  866. /*
  867. * after a call to __netif_napi_del() napi may still be used and
  868. * ixgbe_get_stats64() might access the rings on this vector,
  869. * we must wait a grace period before freeing it.
  870. */
  871. kfree_rcu(q_vector, rcu);
  872. }
  873. /**
  874. * ixgbe_alloc_q_vectors - Allocate memory for interrupt vectors
  875. * @adapter: board private structure to initialize
  876. *
  877. * We allocate one q_vector per queue interrupt. If allocation fails we
  878. * return -ENOMEM.
  879. **/
  880. static int ixgbe_alloc_q_vectors(struct ixgbe_adapter *adapter)
  881. {
  882. int q_vectors = adapter->num_q_vectors;
  883. int rxr_remaining = adapter->num_rx_queues;
  884. int txr_remaining = adapter->num_tx_queues;
  885. int xdp_remaining = adapter->num_xdp_queues;
  886. int rxr_idx = 0, txr_idx = 0, xdp_idx = 0, v_idx = 0;
  887. int err, i;
  888. /* only one q_vector if MSI-X is disabled. */
  889. if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
  890. q_vectors = 1;
  891. if (q_vectors >= (rxr_remaining + txr_remaining + xdp_remaining)) {
  892. for (; rxr_remaining; v_idx++) {
  893. err = ixgbe_alloc_q_vector(adapter, q_vectors, v_idx,
  894. 0, 0, 0, 0, 1, rxr_idx);
  895. if (err)
  896. goto err_out;
  897. /* update counts and index */
  898. rxr_remaining--;
  899. rxr_idx++;
  900. }
  901. }
  902. for (; v_idx < q_vectors; v_idx++) {
  903. int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
  904. int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
  905. int xqpv = DIV_ROUND_UP(xdp_remaining, q_vectors - v_idx);
  906. err = ixgbe_alloc_q_vector(adapter, q_vectors, v_idx,
  907. tqpv, txr_idx,
  908. xqpv, xdp_idx,
  909. rqpv, rxr_idx);
  910. if (err)
  911. goto err_out;
  912. /* update counts and index */
  913. rxr_remaining -= rqpv;
  914. txr_remaining -= tqpv;
  915. xdp_remaining -= xqpv;
  916. rxr_idx++;
  917. txr_idx++;
  918. xdp_idx += xqpv;
  919. }
  920. for (i = 0; i < adapter->num_rx_queues; i++) {
  921. if (adapter->rx_ring[i])
  922. adapter->rx_ring[i]->ring_idx = i;
  923. }
  924. for (i = 0; i < adapter->num_tx_queues; i++) {
  925. if (adapter->tx_ring[i])
  926. adapter->tx_ring[i]->ring_idx = i;
  927. }
  928. for (i = 0; i < adapter->num_xdp_queues; i++) {
  929. if (adapter->xdp_ring[i])
  930. adapter->xdp_ring[i]->ring_idx = i;
  931. }
  932. return 0;
  933. err_out:
  934. adapter->num_tx_queues = 0;
  935. adapter->num_xdp_queues = 0;
  936. adapter->num_rx_queues = 0;
  937. adapter->num_q_vectors = 0;
  938. while (v_idx--)
  939. ixgbe_free_q_vector(adapter, v_idx);
  940. return -ENOMEM;
  941. }
  942. /**
  943. * ixgbe_free_q_vectors - Free memory allocated for interrupt vectors
  944. * @adapter: board private structure to initialize
  945. *
  946. * This function frees the memory allocated to the q_vectors. In addition if
  947. * NAPI is enabled it will delete any references to the NAPI struct prior
  948. * to freeing the q_vector.
  949. **/
  950. static void ixgbe_free_q_vectors(struct ixgbe_adapter *adapter)
  951. {
  952. int v_idx = adapter->num_q_vectors;
  953. adapter->num_tx_queues = 0;
  954. adapter->num_xdp_queues = 0;
  955. adapter->num_rx_queues = 0;
  956. adapter->num_q_vectors = 0;
  957. while (v_idx--)
  958. ixgbe_free_q_vector(adapter, v_idx);
  959. }
  960. static void ixgbe_reset_interrupt_capability(struct ixgbe_adapter *adapter)
  961. {
  962. if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
  963. adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
  964. pci_disable_msix(adapter->pdev);
  965. kfree(adapter->msix_entries);
  966. adapter->msix_entries = NULL;
  967. } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
  968. adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
  969. pci_disable_msi(adapter->pdev);
  970. }
  971. }
  972. /**
  973. * ixgbe_set_interrupt_capability - set MSI-X or MSI if supported
  974. * @adapter: board private structure to initialize
  975. *
  976. * Attempt to configure the interrupts using the best available
  977. * capabilities of the hardware and the kernel.
  978. **/
  979. static void ixgbe_set_interrupt_capability(struct ixgbe_adapter *adapter)
  980. {
  981. int err;
  982. /* We will try to get MSI-X interrupts first */
  983. if (!ixgbe_acquire_msix_vectors(adapter))
  984. return;
  985. /* At this point, we do not have MSI-X capabilities. We need to
  986. * reconfigure or disable various features which require MSI-X
  987. * capability.
  988. */
  989. /* Disable DCB unless we only have a single traffic class */
  990. if (adapter->hw_tcs > 1) {
  991. e_dev_warn("Number of DCB TCs exceeds number of available queues. Disabling DCB support.\n");
  992. netdev_reset_tc(adapter->netdev);
  993. if (adapter->hw.mac.type == ixgbe_mac_82598EB)
  994. adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
  995. adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
  996. adapter->temp_dcb_cfg.pfc_mode_enable = false;
  997. adapter->dcb_cfg.pfc_mode_enable = false;
  998. }
  999. adapter->hw_tcs = 0;
  1000. adapter->dcb_cfg.num_tcs.pg_tcs = 1;
  1001. adapter->dcb_cfg.num_tcs.pfc_tcs = 1;
  1002. /* Disable SR-IOV support */
  1003. e_dev_warn("Disabling SR-IOV support\n");
  1004. ixgbe_disable_sriov(adapter);
  1005. /* Disable RSS */
  1006. e_dev_warn("Disabling RSS support\n");
  1007. adapter->ring_feature[RING_F_RSS].limit = 1;
  1008. /* recalculate number of queues now that many features have been
  1009. * changed or disabled.
  1010. */
  1011. ixgbe_set_num_queues(adapter);
  1012. adapter->num_q_vectors = 1;
  1013. err = pci_enable_msi(adapter->pdev);
  1014. if (err)
  1015. e_dev_warn("Failed to allocate MSI interrupt, falling back to legacy. Error: %d\n",
  1016. err);
  1017. else
  1018. adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
  1019. }
  1020. /**
  1021. * ixgbe_init_interrupt_scheme - Determine proper interrupt scheme
  1022. * @adapter: board private structure to initialize
  1023. *
  1024. * We determine which interrupt scheme to use based on...
  1025. * - Kernel support (MSI, MSI-X)
  1026. * - which can be user-defined (via MODULE_PARAM)
  1027. * - Hardware queue count (num_*_queues)
  1028. * - defined by miscellaneous hardware support/features (RSS, etc.)
  1029. **/
  1030. int ixgbe_init_interrupt_scheme(struct ixgbe_adapter *adapter)
  1031. {
  1032. int err;
  1033. /* Number of supported queues */
  1034. ixgbe_set_num_queues(adapter);
  1035. /* Set interrupt mode */
  1036. ixgbe_set_interrupt_capability(adapter);
  1037. err = ixgbe_alloc_q_vectors(adapter);
  1038. if (err) {
  1039. e_dev_err("Unable to allocate memory for queue vectors\n");
  1040. goto err_alloc_q_vectors;
  1041. }
  1042. ixgbe_cache_ring_register(adapter);
  1043. e_dev_info("Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u XDP Queue count = %u\n",
  1044. (adapter->num_rx_queues > 1) ? "Enabled" : "Disabled",
  1045. adapter->num_rx_queues, adapter->num_tx_queues,
  1046. adapter->num_xdp_queues);
  1047. set_bit(__IXGBE_DOWN, &adapter->state);
  1048. return 0;
  1049. err_alloc_q_vectors:
  1050. ixgbe_reset_interrupt_capability(adapter);
  1051. return err;
  1052. }
  1053. /**
  1054. * ixgbe_clear_interrupt_scheme - Clear the current interrupt scheme settings
  1055. * @adapter: board private structure to clear interrupt scheme on
  1056. *
  1057. * We go through and clear interrupt specific resources and reset the structure
  1058. * to pre-load conditions
  1059. **/
  1060. void ixgbe_clear_interrupt_scheme(struct ixgbe_adapter *adapter)
  1061. {
  1062. adapter->num_tx_queues = 0;
  1063. adapter->num_xdp_queues = 0;
  1064. adapter->num_rx_queues = 0;
  1065. ixgbe_free_q_vectors(adapter);
  1066. ixgbe_reset_interrupt_capability(adapter);
  1067. }
  1068. void ixgbe_tx_ctxtdesc(struct ixgbe_ring *tx_ring, u32 vlan_macip_lens,
  1069. u32 fceof_saidx, u32 type_tucmd, u32 mss_l4len_idx)
  1070. {
  1071. struct ixgbe_adv_tx_context_desc *context_desc;
  1072. u16 i = tx_ring->next_to_use;
  1073. context_desc = IXGBE_TX_CTXTDESC(tx_ring, i);
  1074. i++;
  1075. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1076. /* set bits to identify this as an advanced context descriptor */
  1077. type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
  1078. context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
  1079. context_desc->fceof_saidx = cpu_to_le32(fceof_saidx);
  1080. context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
  1081. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  1082. }