ixgbe_lib.c 24 KB

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