ixgbe_lib.c 23 KB

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