ixgbe_lib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2012 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include "ixgbe.h"
  21. #include "ixgbe_sriov.h"
  22. #ifdef CONFIG_IXGBE_DCB
  23. /* ixgbe_get_first_reg_idx - Return first register index associated with ring */
  24. static void ixgbe_get_first_reg_idx(struct ixgbe_adapter *adapter, u8 tc,
  25. unsigned int *tx, unsigned int *rx)
  26. {
  27. struct net_device *dev = adapter->netdev;
  28. struct ixgbe_hw *hw = &adapter->hw;
  29. u8 num_tcs = netdev_get_num_tc(dev);
  30. *tx = 0;
  31. *rx = 0;
  32. switch (hw->mac.type) {
  33. case ixgbe_mac_82598EB:
  34. /* TxQs/TC: 4 RxQs/TC: 8 */
  35. *tx = tc << 2; /* 0, 4, 8, 12, 16, 20, 24, 28 */
  36. *rx = tc << 3; /* 0, 8, 16, 24, 32, 40, 48, 56 */
  37. break;
  38. case ixgbe_mac_82599EB:
  39. case ixgbe_mac_X540:
  40. if (num_tcs > 4) {
  41. /*
  42. * TCs : TC0/1 TC2/3 TC4-7
  43. * TxQs/TC: 32 16 8
  44. * RxQs/TC: 16 16 16
  45. */
  46. *rx = tc << 4;
  47. if (tc < 3)
  48. *tx = tc << 5; /* 0, 32, 64 */
  49. else if (tc < 5)
  50. *tx = (tc + 2) << 4; /* 80, 96 */
  51. else
  52. *tx = (tc + 8) << 3; /* 104, 112, 120 */
  53. } else {
  54. /*
  55. * TCs : TC0 TC1 TC2/3
  56. * TxQs/TC: 64 32 16
  57. * RxQs/TC: 32 32 32
  58. */
  59. *rx = tc << 5;
  60. if (tc < 2)
  61. *tx = tc << 6; /* 0, 64 */
  62. else
  63. *tx = (tc + 4) << 4; /* 96, 112 */
  64. }
  65. default:
  66. break;
  67. }
  68. }
  69. /**
  70. * ixgbe_cache_ring_dcb - Descriptor ring to register mapping for DCB
  71. * @adapter: board private structure to initialize
  72. *
  73. * Cache the descriptor ring offsets for DCB to the assigned rings.
  74. *
  75. **/
  76. static bool ixgbe_cache_ring_dcb(struct ixgbe_adapter *adapter)
  77. {
  78. struct net_device *dev = adapter->netdev;
  79. unsigned int tx_idx, rx_idx;
  80. int tc, offset, rss_i, i;
  81. u8 num_tcs = netdev_get_num_tc(dev);
  82. /* verify we have DCB queueing enabled before proceeding */
  83. if (num_tcs <= 1)
  84. return false;
  85. rss_i = adapter->ring_feature[RING_F_RSS].indices;
  86. for (tc = 0, offset = 0; tc < num_tcs; tc++, offset += rss_i) {
  87. ixgbe_get_first_reg_idx(adapter, tc, &tx_idx, &rx_idx);
  88. for (i = 0; i < rss_i; i++, tx_idx++, rx_idx++) {
  89. adapter->tx_ring[offset + i]->reg_idx = tx_idx;
  90. adapter->rx_ring[offset + i]->reg_idx = rx_idx;
  91. adapter->tx_ring[offset + i]->dcb_tc = tc;
  92. adapter->rx_ring[offset + i]->dcb_tc = tc;
  93. }
  94. }
  95. return true;
  96. }
  97. #endif
  98. /**
  99. * ixgbe_cache_ring_sriov - Descriptor ring to register mapping for sriov
  100. * @adapter: board private structure to initialize
  101. *
  102. * SR-IOV doesn't use any descriptor rings but changes the default if
  103. * no other mapping is used.
  104. *
  105. */
  106. static inline bool ixgbe_cache_ring_sriov(struct ixgbe_adapter *adapter)
  107. {
  108. adapter->rx_ring[0]->reg_idx = adapter->num_vfs * 2;
  109. adapter->tx_ring[0]->reg_idx = adapter->num_vfs * 2;
  110. if (adapter->num_vfs)
  111. return true;
  112. else
  113. return false;
  114. }
  115. /**
  116. * ixgbe_cache_ring_rss - Descriptor ring to register mapping for RSS
  117. * @adapter: board private structure to initialize
  118. *
  119. * Cache the descriptor ring offsets for RSS to the assigned rings.
  120. *
  121. **/
  122. static bool ixgbe_cache_ring_rss(struct ixgbe_adapter *adapter)
  123. {
  124. int i;
  125. if (!(adapter->flags & IXGBE_FLAG_RSS_ENABLED))
  126. return false;
  127. for (i = 0; i < adapter->num_rx_queues; i++)
  128. adapter->rx_ring[i]->reg_idx = i;
  129. for (i = 0; i < adapter->num_tx_queues; i++)
  130. adapter->tx_ring[i]->reg_idx = i;
  131. return true;
  132. }
  133. /**
  134. * ixgbe_cache_ring_register - Descriptor ring to register mapping
  135. * @adapter: board private structure to initialize
  136. *
  137. * Once we know the feature-set enabled for the device, we'll cache
  138. * the register offset the descriptor ring is assigned to.
  139. *
  140. * Note, the order the various feature calls is important. It must start with
  141. * the "most" features enabled at the same time, then trickle down to the
  142. * least amount of features turned on at once.
  143. **/
  144. static void ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
  145. {
  146. /* start with default case */
  147. adapter->rx_ring[0]->reg_idx = 0;
  148. adapter->tx_ring[0]->reg_idx = 0;
  149. if (ixgbe_cache_ring_sriov(adapter))
  150. return;
  151. #ifdef CONFIG_IXGBE_DCB
  152. if (ixgbe_cache_ring_dcb(adapter))
  153. return;
  154. #endif
  155. ixgbe_cache_ring_rss(adapter);
  156. }
  157. /**
  158. * ixgbe_set_sriov_queues - Allocate queues for IOV use
  159. * @adapter: board private structure to initialize
  160. *
  161. * IOV doesn't actually use anything, so just NAK the
  162. * request for now and let the other queue routines
  163. * figure out what to do.
  164. */
  165. static inline bool ixgbe_set_sriov_queues(struct ixgbe_adapter *adapter)
  166. {
  167. return false;
  168. }
  169. #define IXGBE_RSS_16Q_MASK 0xF
  170. #define IXGBE_RSS_8Q_MASK 0x7
  171. #define IXGBE_RSS_4Q_MASK 0x3
  172. #define IXGBE_RSS_2Q_MASK 0x1
  173. #define IXGBE_RSS_DISABLED_MASK 0x0
  174. #ifdef CONFIG_IXGBE_DCB
  175. static bool ixgbe_set_dcb_queues(struct ixgbe_adapter *adapter)
  176. {
  177. struct net_device *dev = adapter->netdev;
  178. struct ixgbe_ring_feature *f;
  179. int rss_i, rss_m, i;
  180. int tcs;
  181. /* Map queue offset and counts onto allocated tx queues */
  182. tcs = netdev_get_num_tc(dev);
  183. /* verify we have DCB queueing enabled before proceeding */
  184. if (tcs <= 1)
  185. return false;
  186. /* determine the upper limit for our current DCB mode */
  187. rss_i = dev->num_tx_queues / tcs;
  188. if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
  189. /* 8 TC w/ 4 queues per TC */
  190. rss_i = min_t(u16, rss_i, 4);
  191. rss_m = IXGBE_RSS_4Q_MASK;
  192. } else if (tcs > 4) {
  193. /* 8 TC w/ 8 queues per TC */
  194. rss_i = min_t(u16, rss_i, 8);
  195. rss_m = IXGBE_RSS_8Q_MASK;
  196. } else {
  197. /* 4 TC w/ 16 queues per TC */
  198. rss_i = min_t(u16, rss_i, 16);
  199. rss_m = IXGBE_RSS_16Q_MASK;
  200. }
  201. /* set RSS mask and indices */
  202. f = &adapter->ring_feature[RING_F_RSS];
  203. rss_i = min_t(int, rss_i, f->limit);
  204. f->indices = rss_i;
  205. f->mask = rss_m;
  206. #ifdef IXGBE_FCOE
  207. /* FCoE enabled queues require special configuration indexed
  208. * by feature specific indices and offset. Here we map FCoE
  209. * indices onto the DCB queue pairs allowing FCoE to own
  210. * configuration later.
  211. */
  212. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
  213. u8 tc = ixgbe_fcoe_get_tc(adapter);
  214. f = &adapter->ring_feature[RING_F_FCOE];
  215. f->indices = min_t(u16, rss_i, f->limit);
  216. f->offset = rss_i * tc;
  217. }
  218. #endif /* IXGBE_FCOE */
  219. for (i = 0; i < tcs; i++)
  220. netdev_set_tc_queue(dev, i, rss_i, rss_i * i);
  221. adapter->num_tx_queues = rss_i * tcs;
  222. adapter->num_rx_queues = rss_i * tcs;
  223. return true;
  224. }
  225. #endif
  226. /**
  227. * ixgbe_set_rss_queues - Allocate queues for RSS
  228. * @adapter: board private structure to initialize
  229. *
  230. * This is our "base" multiqueue mode. RSS (Receive Side Scaling) will try
  231. * to allocate one Rx queue per CPU, and if available, one Tx queue per CPU.
  232. *
  233. **/
  234. static bool ixgbe_set_rss_queues(struct ixgbe_adapter *adapter)
  235. {
  236. struct ixgbe_ring_feature *f;
  237. u16 rss_i;
  238. if (!(adapter->flags & IXGBE_FLAG_RSS_ENABLED)) {
  239. adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
  240. return false;
  241. }
  242. /* set mask for 16 queue limit of RSS */
  243. f = &adapter->ring_feature[RING_F_RSS];
  244. rss_i = f->limit;
  245. f->indices = rss_i;
  246. f->mask = IXGBE_RSS_16Q_MASK;
  247. /*
  248. * Use Flow Director in addition to RSS to ensure the best
  249. * distribution of flows across cores, even when an FDIR flow
  250. * isn't matched.
  251. */
  252. if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
  253. f = &adapter->ring_feature[RING_F_FDIR];
  254. f->indices = min_t(u16, num_online_cpus(), f->limit);
  255. rss_i = max_t(u16, rss_i, f->indices);
  256. }
  257. #ifdef IXGBE_FCOE
  258. /*
  259. * FCoE can exist on the same rings as standard network traffic
  260. * however it is preferred to avoid that if possible. In order
  261. * to get the best performance we allocate as many FCoE queues
  262. * as we can and we place them at the end of the ring array to
  263. * avoid sharing queues with standard RSS on systems with 24 or
  264. * more CPUs.
  265. */
  266. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
  267. struct net_device *dev = adapter->netdev;
  268. u16 fcoe_i;
  269. f = &adapter->ring_feature[RING_F_FCOE];
  270. /* merge FCoE queues with RSS queues */
  271. fcoe_i = min_t(u16, f->limit + rss_i, num_online_cpus());
  272. fcoe_i = min_t(u16, fcoe_i, dev->num_tx_queues);
  273. /* limit indices to rss_i if MSI-X is disabled */
  274. if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
  275. fcoe_i = rss_i;
  276. /* attempt to reserve some queues for just FCoE */
  277. f->indices = min_t(u16, fcoe_i, f->limit);
  278. f->offset = fcoe_i - f->indices;
  279. rss_i = max_t(u16, fcoe_i, rss_i);
  280. }
  281. #endif /* IXGBE_FCOE */
  282. adapter->num_rx_queues = rss_i;
  283. adapter->num_tx_queues = rss_i;
  284. return true;
  285. }
  286. /**
  287. * ixgbe_set_num_queues - Allocate queues for device, feature dependent
  288. * @adapter: board private structure to initialize
  289. *
  290. * This is the top level queue allocation routine. The order here is very
  291. * important, starting with the "most" number of features turned on at once,
  292. * and ending with the smallest set of features. This way large combinations
  293. * can be allocated if they're turned on, and smaller combinations are the
  294. * fallthrough conditions.
  295. *
  296. **/
  297. static void ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
  298. {
  299. /* Start with base case */
  300. adapter->num_rx_queues = 1;
  301. adapter->num_tx_queues = 1;
  302. adapter->num_rx_pools = adapter->num_rx_queues;
  303. adapter->num_rx_queues_per_pool = 1;
  304. if (ixgbe_set_sriov_queues(adapter))
  305. return;
  306. #ifdef CONFIG_IXGBE_DCB
  307. if (ixgbe_set_dcb_queues(adapter))
  308. return;
  309. #endif
  310. ixgbe_set_rss_queues(adapter);
  311. }
  312. static void ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter,
  313. int vectors)
  314. {
  315. int err, vector_threshold;
  316. /* We'll want at least 2 (vector_threshold):
  317. * 1) TxQ[0] + RxQ[0] handler
  318. * 2) Other (Link Status Change, etc.)
  319. */
  320. vector_threshold = MIN_MSIX_COUNT;
  321. /*
  322. * The more we get, the more we will assign to Tx/Rx Cleanup
  323. * for the separate queues...where Rx Cleanup >= Tx Cleanup.
  324. * Right now, we simply care about how many we'll get; we'll
  325. * set them up later while requesting irq's.
  326. */
  327. while (vectors >= vector_threshold) {
  328. err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
  329. vectors);
  330. if (!err) /* Success in acquiring all requested vectors. */
  331. break;
  332. else if (err < 0)
  333. vectors = 0; /* Nasty failure, quit now */
  334. else /* err == number of vectors we should try again with */
  335. vectors = err;
  336. }
  337. if (vectors < vector_threshold) {
  338. /* Can't allocate enough MSI-X interrupts? Oh well.
  339. * This just means we'll go with either a single MSI
  340. * vector or fall back to legacy interrupts.
  341. */
  342. netif_printk(adapter, hw, KERN_DEBUG, adapter->netdev,
  343. "Unable to allocate MSI-X interrupts\n");
  344. adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
  345. kfree(adapter->msix_entries);
  346. adapter->msix_entries = NULL;
  347. } else {
  348. adapter->flags |= IXGBE_FLAG_MSIX_ENABLED; /* Woot! */
  349. /*
  350. * Adjust for only the vectors we'll use, which is minimum
  351. * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
  352. * vectors we were allocated.
  353. */
  354. vectors -= NON_Q_VECTORS;
  355. adapter->num_q_vectors = min(vectors, adapter->max_q_vectors);
  356. }
  357. }
  358. static void ixgbe_add_ring(struct ixgbe_ring *ring,
  359. struct ixgbe_ring_container *head)
  360. {
  361. ring->next = head->ring;
  362. head->ring = ring;
  363. head->count++;
  364. }
  365. /**
  366. * ixgbe_alloc_q_vector - Allocate memory for a single interrupt vector
  367. * @adapter: board private structure to initialize
  368. * @v_count: q_vectors allocated on adapter, used for ring interleaving
  369. * @v_idx: index of vector in adapter struct
  370. * @txr_count: total number of Tx rings to allocate
  371. * @txr_idx: index of first Tx ring to allocate
  372. * @rxr_count: total number of Rx rings to allocate
  373. * @rxr_idx: index of first Rx ring to allocate
  374. *
  375. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  376. **/
  377. static int ixgbe_alloc_q_vector(struct ixgbe_adapter *adapter,
  378. int v_count, int v_idx,
  379. int txr_count, int txr_idx,
  380. int rxr_count, int rxr_idx)
  381. {
  382. struct ixgbe_q_vector *q_vector;
  383. struct ixgbe_ring *ring;
  384. int node = -1;
  385. int cpu = -1;
  386. int ring_count, size;
  387. ring_count = txr_count + rxr_count;
  388. size = sizeof(struct ixgbe_q_vector) +
  389. (sizeof(struct ixgbe_ring) * ring_count);
  390. /* customize cpu for Flow Director mapping */
  391. if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
  392. if (cpu_online(v_idx)) {
  393. cpu = v_idx;
  394. node = cpu_to_node(cpu);
  395. }
  396. }
  397. /* allocate q_vector and rings */
  398. q_vector = kzalloc_node(size, GFP_KERNEL, node);
  399. if (!q_vector)
  400. q_vector = kzalloc(size, GFP_KERNEL);
  401. if (!q_vector)
  402. return -ENOMEM;
  403. /* setup affinity mask and node */
  404. if (cpu != -1)
  405. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  406. else
  407. cpumask_copy(&q_vector->affinity_mask, cpu_online_mask);
  408. q_vector->numa_node = node;
  409. /* initialize NAPI */
  410. netif_napi_add(adapter->netdev, &q_vector->napi,
  411. ixgbe_poll, 64);
  412. /* tie q_vector and adapter together */
  413. adapter->q_vector[v_idx] = q_vector;
  414. q_vector->adapter = adapter;
  415. q_vector->v_idx = v_idx;
  416. /* initialize work limits */
  417. q_vector->tx.work_limit = adapter->tx_work_limit;
  418. /* initialize pointer to rings */
  419. ring = q_vector->ring;
  420. while (txr_count) {
  421. /* assign generic ring traits */
  422. ring->dev = &adapter->pdev->dev;
  423. ring->netdev = adapter->netdev;
  424. /* configure backlink on ring */
  425. ring->q_vector = q_vector;
  426. /* update q_vector Tx values */
  427. ixgbe_add_ring(ring, &q_vector->tx);
  428. /* apply Tx specific ring traits */
  429. ring->count = adapter->tx_ring_count;
  430. ring->queue_index = txr_idx;
  431. /* assign ring to adapter */
  432. adapter->tx_ring[txr_idx] = ring;
  433. /* update count and index */
  434. txr_count--;
  435. txr_idx += v_count;
  436. /* push pointer to next ring */
  437. ring++;
  438. }
  439. while (rxr_count) {
  440. /* assign generic ring traits */
  441. ring->dev = &adapter->pdev->dev;
  442. ring->netdev = adapter->netdev;
  443. /* configure backlink on ring */
  444. ring->q_vector = q_vector;
  445. /* update q_vector Rx values */
  446. ixgbe_add_ring(ring, &q_vector->rx);
  447. /*
  448. * 82599 errata, UDP frames with a 0 checksum
  449. * can be marked as checksum errors.
  450. */
  451. if (adapter->hw.mac.type == ixgbe_mac_82599EB)
  452. set_bit(__IXGBE_RX_CSUM_UDP_ZERO_ERR, &ring->state);
  453. #ifdef IXGBE_FCOE
  454. if (adapter->netdev->features & NETIF_F_FCOE_MTU) {
  455. struct ixgbe_ring_feature *f;
  456. f = &adapter->ring_feature[RING_F_FCOE];
  457. if ((rxr_idx >= f->offset) &&
  458. (rxr_idx < f->offset + f->indices))
  459. set_bit(__IXGBE_RX_FCOE, &ring->state);
  460. }
  461. #endif /* IXGBE_FCOE */
  462. /* apply Rx specific ring traits */
  463. ring->count = adapter->rx_ring_count;
  464. ring->queue_index = rxr_idx;
  465. /* assign ring to adapter */
  466. adapter->rx_ring[rxr_idx] = ring;
  467. /* update count and index */
  468. rxr_count--;
  469. rxr_idx += v_count;
  470. /* push pointer to next ring */
  471. ring++;
  472. }
  473. return 0;
  474. }
  475. /**
  476. * ixgbe_free_q_vector - Free memory allocated for specific interrupt vector
  477. * @adapter: board private structure to initialize
  478. * @v_idx: Index of vector to be freed
  479. *
  480. * This function frees the memory allocated to the q_vector. In addition if
  481. * NAPI is enabled it will delete any references to the NAPI struct prior
  482. * to freeing the q_vector.
  483. **/
  484. static void ixgbe_free_q_vector(struct ixgbe_adapter *adapter, int v_idx)
  485. {
  486. struct ixgbe_q_vector *q_vector = adapter->q_vector[v_idx];
  487. struct ixgbe_ring *ring;
  488. ixgbe_for_each_ring(ring, q_vector->tx)
  489. adapter->tx_ring[ring->queue_index] = NULL;
  490. ixgbe_for_each_ring(ring, q_vector->rx)
  491. adapter->rx_ring[ring->queue_index] = NULL;
  492. adapter->q_vector[v_idx] = NULL;
  493. netif_napi_del(&q_vector->napi);
  494. /*
  495. * ixgbe_get_stats64() might access the rings on this vector,
  496. * we must wait a grace period before freeing it.
  497. */
  498. kfree_rcu(q_vector, rcu);
  499. }
  500. /**
  501. * ixgbe_alloc_q_vectors - Allocate memory for interrupt vectors
  502. * @adapter: board private structure to initialize
  503. *
  504. * We allocate one q_vector per queue interrupt. If allocation fails we
  505. * return -ENOMEM.
  506. **/
  507. static int ixgbe_alloc_q_vectors(struct ixgbe_adapter *adapter)
  508. {
  509. int q_vectors = adapter->num_q_vectors;
  510. int rxr_remaining = adapter->num_rx_queues;
  511. int txr_remaining = adapter->num_tx_queues;
  512. int rxr_idx = 0, txr_idx = 0, v_idx = 0;
  513. int err;
  514. /* only one q_vector if MSI-X is disabled. */
  515. if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
  516. q_vectors = 1;
  517. if (q_vectors >= (rxr_remaining + txr_remaining)) {
  518. for (; rxr_remaining; v_idx++) {
  519. err = ixgbe_alloc_q_vector(adapter, q_vectors, v_idx,
  520. 0, 0, 1, rxr_idx);
  521. if (err)
  522. goto err_out;
  523. /* update counts and index */
  524. rxr_remaining--;
  525. rxr_idx++;
  526. }
  527. }
  528. for (; v_idx < q_vectors; v_idx++) {
  529. int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
  530. int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
  531. err = ixgbe_alloc_q_vector(adapter, q_vectors, v_idx,
  532. tqpv, txr_idx,
  533. rqpv, rxr_idx);
  534. if (err)
  535. goto err_out;
  536. /* update counts and index */
  537. rxr_remaining -= rqpv;
  538. txr_remaining -= tqpv;
  539. rxr_idx++;
  540. txr_idx++;
  541. }
  542. return 0;
  543. err_out:
  544. adapter->num_tx_queues = 0;
  545. adapter->num_rx_queues = 0;
  546. adapter->num_q_vectors = 0;
  547. while (v_idx--)
  548. ixgbe_free_q_vector(adapter, v_idx);
  549. return -ENOMEM;
  550. }
  551. /**
  552. * ixgbe_free_q_vectors - Free memory allocated for interrupt vectors
  553. * @adapter: board private structure to initialize
  554. *
  555. * This function frees the memory allocated to the q_vectors. In addition if
  556. * NAPI is enabled it will delete any references to the NAPI struct prior
  557. * to freeing the q_vector.
  558. **/
  559. static void ixgbe_free_q_vectors(struct ixgbe_adapter *adapter)
  560. {
  561. int v_idx = adapter->num_q_vectors;
  562. adapter->num_tx_queues = 0;
  563. adapter->num_rx_queues = 0;
  564. adapter->num_q_vectors = 0;
  565. while (v_idx--)
  566. ixgbe_free_q_vector(adapter, v_idx);
  567. }
  568. static void ixgbe_reset_interrupt_capability(struct ixgbe_adapter *adapter)
  569. {
  570. if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
  571. adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
  572. pci_disable_msix(adapter->pdev);
  573. kfree(adapter->msix_entries);
  574. adapter->msix_entries = NULL;
  575. } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
  576. adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
  577. pci_disable_msi(adapter->pdev);
  578. }
  579. }
  580. /**
  581. * ixgbe_set_interrupt_capability - set MSI-X or MSI if supported
  582. * @adapter: board private structure to initialize
  583. *
  584. * Attempt to configure the interrupts using the best available
  585. * capabilities of the hardware and the kernel.
  586. **/
  587. static void ixgbe_set_interrupt_capability(struct ixgbe_adapter *adapter)
  588. {
  589. struct ixgbe_hw *hw = &adapter->hw;
  590. int vector, v_budget, err;
  591. /*
  592. * It's easy to be greedy for MSI-X vectors, but it really
  593. * doesn't do us much good if we have a lot more vectors
  594. * than CPU's. So let's be conservative and only ask for
  595. * (roughly) the same number of vectors as there are CPU's.
  596. * The default is to use pairs of vectors.
  597. */
  598. v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
  599. v_budget = min_t(int, v_budget, num_online_cpus());
  600. v_budget += NON_Q_VECTORS;
  601. /*
  602. * At the same time, hardware can only support a maximum of
  603. * hw.mac->max_msix_vectors vectors. With features
  604. * such as RSS and VMDq, we can easily surpass the number of Rx and Tx
  605. * descriptor queues supported by our device. Thus, we cap it off in
  606. * those rare cases where the cpu count also exceeds our vector limit.
  607. */
  608. v_budget = min_t(int, v_budget, hw->mac.max_msix_vectors);
  609. /* A failure in MSI-X entry allocation isn't fatal, but it does
  610. * mean we disable MSI-X capabilities of the adapter. */
  611. adapter->msix_entries = kcalloc(v_budget,
  612. sizeof(struct msix_entry), GFP_KERNEL);
  613. if (adapter->msix_entries) {
  614. for (vector = 0; vector < v_budget; vector++)
  615. adapter->msix_entries[vector].entry = vector;
  616. ixgbe_acquire_msix_vectors(adapter, v_budget);
  617. if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
  618. return;
  619. }
  620. adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
  621. adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
  622. if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
  623. e_err(probe,
  624. "ATR is not supported while multiple "
  625. "queues are disabled. Disabling Flow Director\n");
  626. }
  627. adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
  628. adapter->atr_sample_rate = 0;
  629. if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
  630. ixgbe_disable_sriov(adapter);
  631. ixgbe_set_num_queues(adapter);
  632. adapter->num_q_vectors = 1;
  633. err = pci_enable_msi(adapter->pdev);
  634. if (err) {
  635. netif_printk(adapter, hw, KERN_DEBUG, adapter->netdev,
  636. "Unable to allocate MSI interrupt, "
  637. "falling back to legacy. Error: %d\n", err);
  638. return;
  639. }
  640. adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
  641. }
  642. /**
  643. * ixgbe_init_interrupt_scheme - Determine proper interrupt scheme
  644. * @adapter: board private structure to initialize
  645. *
  646. * We determine which interrupt scheme to use based on...
  647. * - Kernel support (MSI, MSI-X)
  648. * - which can be user-defined (via MODULE_PARAM)
  649. * - Hardware queue count (num_*_queues)
  650. * - defined by miscellaneous hardware support/features (RSS, etc.)
  651. **/
  652. int ixgbe_init_interrupt_scheme(struct ixgbe_adapter *adapter)
  653. {
  654. int err;
  655. /* Number of supported queues */
  656. ixgbe_set_num_queues(adapter);
  657. /* Set interrupt mode */
  658. ixgbe_set_interrupt_capability(adapter);
  659. err = ixgbe_alloc_q_vectors(adapter);
  660. if (err) {
  661. e_dev_err("Unable to allocate memory for queue vectors\n");
  662. goto err_alloc_q_vectors;
  663. }
  664. ixgbe_cache_ring_register(adapter);
  665. e_dev_info("Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u\n",
  666. (adapter->num_rx_queues > 1) ? "Enabled" : "Disabled",
  667. adapter->num_rx_queues, adapter->num_tx_queues);
  668. set_bit(__IXGBE_DOWN, &adapter->state);
  669. return 0;
  670. err_alloc_q_vectors:
  671. ixgbe_reset_interrupt_capability(adapter);
  672. return err;
  673. }
  674. /**
  675. * ixgbe_clear_interrupt_scheme - Clear the current interrupt scheme settings
  676. * @adapter: board private structure to clear interrupt scheme on
  677. *
  678. * We go through and clear interrupt specific resources and reset the structure
  679. * to pre-load conditions
  680. **/
  681. void ixgbe_clear_interrupt_scheme(struct ixgbe_adapter *adapter)
  682. {
  683. adapter->num_tx_queues = 0;
  684. adapter->num_rx_queues = 0;
  685. ixgbe_free_q_vectors(adapter);
  686. ixgbe_reset_interrupt_capability(adapter);
  687. }
  688. void ixgbe_tx_ctxtdesc(struct ixgbe_ring *tx_ring, u32 vlan_macip_lens,
  689. u32 fcoe_sof_eof, u32 type_tucmd, u32 mss_l4len_idx)
  690. {
  691. struct ixgbe_adv_tx_context_desc *context_desc;
  692. u16 i = tx_ring->next_to_use;
  693. context_desc = IXGBE_TX_CTXTDESC(tx_ring, i);
  694. i++;
  695. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  696. /* set bits to identify this as an advanced context descriptor */
  697. type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
  698. context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
  699. context_desc->seqnum_seed = cpu_to_le32(fcoe_sof_eof);
  700. context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
  701. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  702. }