ixgbe_lib.c 25 KB

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