fm10k_main.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. /* Intel Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2014 Intel Corporation.
  3. *
  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. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * The full GNU General Public License is included in this distribution in
  14. * the file called "COPYING".
  15. *
  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 <linux/types.h>
  21. #include <linux/module.h>
  22. #include <net/ipv6.h>
  23. #include <net/ip.h>
  24. #include <net/tcp.h>
  25. #include <linux/if_macvlan.h>
  26. #include <linux/prefetch.h>
  27. #include "fm10k.h"
  28. #define DRV_VERSION "0.12.2-k"
  29. const char fm10k_driver_version[] = DRV_VERSION;
  30. char fm10k_driver_name[] = "fm10k";
  31. static const char fm10k_driver_string[] =
  32. "Intel(R) Ethernet Switch Host Interface Driver";
  33. static const char fm10k_copyright[] =
  34. "Copyright (c) 2013 Intel Corporation.";
  35. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  36. MODULE_DESCRIPTION("Intel(R) Ethernet Switch Host Interface Driver");
  37. MODULE_LICENSE("GPL");
  38. MODULE_VERSION(DRV_VERSION);
  39. /**
  40. * fm10k_init_module - Driver Registration Routine
  41. *
  42. * fm10k_init_module is the first routine called when the driver is
  43. * loaded. All it does is register with the PCI subsystem.
  44. **/
  45. static int __init fm10k_init_module(void)
  46. {
  47. pr_info("%s - version %s\n", fm10k_driver_string, fm10k_driver_version);
  48. pr_info("%s\n", fm10k_copyright);
  49. fm10k_dbg_init();
  50. return fm10k_register_pci_driver();
  51. }
  52. module_init(fm10k_init_module);
  53. /**
  54. * fm10k_exit_module - Driver Exit Cleanup Routine
  55. *
  56. * fm10k_exit_module is called just before the driver is removed
  57. * from memory.
  58. **/
  59. static void __exit fm10k_exit_module(void)
  60. {
  61. fm10k_unregister_pci_driver();
  62. fm10k_dbg_exit();
  63. }
  64. module_exit(fm10k_exit_module);
  65. static bool fm10k_alloc_mapped_page(struct fm10k_ring *rx_ring,
  66. struct fm10k_rx_buffer *bi)
  67. {
  68. struct page *page = bi->page;
  69. dma_addr_t dma;
  70. /* Only page will be NULL if buffer was consumed */
  71. if (likely(page))
  72. return true;
  73. /* alloc new page for storage */
  74. page = alloc_page(GFP_ATOMIC | __GFP_COLD);
  75. if (unlikely(!page)) {
  76. rx_ring->rx_stats.alloc_failed++;
  77. return false;
  78. }
  79. /* map page for use */
  80. dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  81. /* if mapping failed free memory back to system since
  82. * there isn't much point in holding memory we can't use
  83. */
  84. if (dma_mapping_error(rx_ring->dev, dma)) {
  85. __free_page(page);
  86. bi->page = NULL;
  87. rx_ring->rx_stats.alloc_failed++;
  88. return false;
  89. }
  90. bi->dma = dma;
  91. bi->page = page;
  92. bi->page_offset = 0;
  93. return true;
  94. }
  95. /**
  96. * fm10k_alloc_rx_buffers - Replace used receive buffers
  97. * @rx_ring: ring to place buffers on
  98. * @cleaned_count: number of buffers to replace
  99. **/
  100. void fm10k_alloc_rx_buffers(struct fm10k_ring *rx_ring, u16 cleaned_count)
  101. {
  102. union fm10k_rx_desc *rx_desc;
  103. struct fm10k_rx_buffer *bi;
  104. u16 i = rx_ring->next_to_use;
  105. /* nothing to do */
  106. if (!cleaned_count)
  107. return;
  108. rx_desc = FM10K_RX_DESC(rx_ring, i);
  109. bi = &rx_ring->rx_buffer[i];
  110. i -= rx_ring->count;
  111. do {
  112. if (!fm10k_alloc_mapped_page(rx_ring, bi))
  113. break;
  114. /* Refresh the desc even if buffer_addrs didn't change
  115. * because each write-back erases this info.
  116. */
  117. rx_desc->q.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  118. rx_desc++;
  119. bi++;
  120. i++;
  121. if (unlikely(!i)) {
  122. rx_desc = FM10K_RX_DESC(rx_ring, 0);
  123. bi = rx_ring->rx_buffer;
  124. i -= rx_ring->count;
  125. }
  126. /* clear the hdr_addr for the next_to_use descriptor */
  127. rx_desc->q.hdr_addr = 0;
  128. cleaned_count--;
  129. } while (cleaned_count);
  130. i += rx_ring->count;
  131. if (rx_ring->next_to_use != i) {
  132. /* record the next descriptor to use */
  133. rx_ring->next_to_use = i;
  134. /* update next to alloc since we have filled the ring */
  135. rx_ring->next_to_alloc = i;
  136. /* Force memory writes to complete before letting h/w
  137. * know there are new descriptors to fetch. (Only
  138. * applicable for weak-ordered memory model archs,
  139. * such as IA-64).
  140. */
  141. wmb();
  142. /* notify hardware of new descriptors */
  143. writel(i, rx_ring->tail);
  144. }
  145. }
  146. /**
  147. * fm10k_reuse_rx_page - page flip buffer and store it back on the ring
  148. * @rx_ring: rx descriptor ring to store buffers on
  149. * @old_buff: donor buffer to have page reused
  150. *
  151. * Synchronizes page for reuse by the interface
  152. **/
  153. static void fm10k_reuse_rx_page(struct fm10k_ring *rx_ring,
  154. struct fm10k_rx_buffer *old_buff)
  155. {
  156. struct fm10k_rx_buffer *new_buff;
  157. u16 nta = rx_ring->next_to_alloc;
  158. new_buff = &rx_ring->rx_buffer[nta];
  159. /* update, and store next to alloc */
  160. nta++;
  161. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  162. /* transfer page from old buffer to new buffer */
  163. memcpy(new_buff, old_buff, sizeof(struct fm10k_rx_buffer));
  164. /* sync the buffer for use by the device */
  165. dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
  166. old_buff->page_offset,
  167. FM10K_RX_BUFSZ,
  168. DMA_FROM_DEVICE);
  169. }
  170. static bool fm10k_can_reuse_rx_page(struct fm10k_rx_buffer *rx_buffer,
  171. struct page *page,
  172. unsigned int truesize)
  173. {
  174. /* avoid re-using remote pages */
  175. if (unlikely(page_to_nid(page) != numa_mem_id()))
  176. return false;
  177. #if (PAGE_SIZE < 8192)
  178. /* if we are only owner of page we can reuse it */
  179. if (unlikely(page_count(page) != 1))
  180. return false;
  181. /* flip page offset to other buffer */
  182. rx_buffer->page_offset ^= FM10K_RX_BUFSZ;
  183. /* Even if we own the page, we are not allowed to use atomic_set()
  184. * This would break get_page_unless_zero() users.
  185. */
  186. atomic_inc(&page->_count);
  187. #else
  188. /* move offset up to the next cache line */
  189. rx_buffer->page_offset += truesize;
  190. if (rx_buffer->page_offset > (PAGE_SIZE - FM10K_RX_BUFSZ))
  191. return false;
  192. /* bump ref count on page before it is given to the stack */
  193. get_page(page);
  194. #endif
  195. return true;
  196. }
  197. /**
  198. * fm10k_add_rx_frag - Add contents of Rx buffer to sk_buff
  199. * @rx_ring: rx descriptor ring to transact packets on
  200. * @rx_buffer: buffer containing page to add
  201. * @rx_desc: descriptor containing length of buffer written by hardware
  202. * @skb: sk_buff to place the data into
  203. *
  204. * This function will add the data contained in rx_buffer->page to the skb.
  205. * This is done either through a direct copy if the data in the buffer is
  206. * less than the skb header size, otherwise it will just attach the page as
  207. * a frag to the skb.
  208. *
  209. * The function will then update the page offset if necessary and return
  210. * true if the buffer can be reused by the interface.
  211. **/
  212. static bool fm10k_add_rx_frag(struct fm10k_ring *rx_ring,
  213. struct fm10k_rx_buffer *rx_buffer,
  214. union fm10k_rx_desc *rx_desc,
  215. struct sk_buff *skb)
  216. {
  217. struct page *page = rx_buffer->page;
  218. unsigned int size = le16_to_cpu(rx_desc->w.length);
  219. #if (PAGE_SIZE < 8192)
  220. unsigned int truesize = FM10K_RX_BUFSZ;
  221. #else
  222. unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
  223. #endif
  224. if ((size <= FM10K_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
  225. unsigned char *va = page_address(page) + rx_buffer->page_offset;
  226. memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
  227. /* we can reuse buffer as-is, just make sure it is local */
  228. if (likely(page_to_nid(page) == numa_mem_id()))
  229. return true;
  230. /* this page cannot be reused so discard it */
  231. put_page(page);
  232. return false;
  233. }
  234. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  235. rx_buffer->page_offset, size, truesize);
  236. return fm10k_can_reuse_rx_page(rx_buffer, page, truesize);
  237. }
  238. static struct sk_buff *fm10k_fetch_rx_buffer(struct fm10k_ring *rx_ring,
  239. union fm10k_rx_desc *rx_desc,
  240. struct sk_buff *skb)
  241. {
  242. struct fm10k_rx_buffer *rx_buffer;
  243. struct page *page;
  244. rx_buffer = &rx_ring->rx_buffer[rx_ring->next_to_clean];
  245. page = rx_buffer->page;
  246. prefetchw(page);
  247. if (likely(!skb)) {
  248. void *page_addr = page_address(page) +
  249. rx_buffer->page_offset;
  250. /* prefetch first cache line of first page */
  251. prefetch(page_addr);
  252. #if L1_CACHE_BYTES < 128
  253. prefetch(page_addr + L1_CACHE_BYTES);
  254. #endif
  255. /* allocate a skb to store the frags */
  256. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  257. FM10K_RX_HDR_LEN);
  258. if (unlikely(!skb)) {
  259. rx_ring->rx_stats.alloc_failed++;
  260. return NULL;
  261. }
  262. /* we will be copying header into skb->data in
  263. * pskb_may_pull so it is in our interest to prefetch
  264. * it now to avoid a possible cache miss
  265. */
  266. prefetchw(skb->data);
  267. }
  268. /* we are reusing so sync this buffer for CPU use */
  269. dma_sync_single_range_for_cpu(rx_ring->dev,
  270. rx_buffer->dma,
  271. rx_buffer->page_offset,
  272. FM10K_RX_BUFSZ,
  273. DMA_FROM_DEVICE);
  274. /* pull page into skb */
  275. if (fm10k_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
  276. /* hand second half of page back to the ring */
  277. fm10k_reuse_rx_page(rx_ring, rx_buffer);
  278. } else {
  279. /* we are not reusing the buffer so unmap it */
  280. dma_unmap_page(rx_ring->dev, rx_buffer->dma,
  281. PAGE_SIZE, DMA_FROM_DEVICE);
  282. }
  283. /* clear contents of rx_buffer */
  284. rx_buffer->page = NULL;
  285. return skb;
  286. }
  287. static inline void fm10k_rx_checksum(struct fm10k_ring *ring,
  288. union fm10k_rx_desc *rx_desc,
  289. struct sk_buff *skb)
  290. {
  291. skb_checksum_none_assert(skb);
  292. /* Rx checksum disabled via ethtool */
  293. if (!(ring->netdev->features & NETIF_F_RXCSUM))
  294. return;
  295. /* TCP/UDP checksum error bit is set */
  296. if (fm10k_test_staterr(rx_desc,
  297. FM10K_RXD_STATUS_L4E |
  298. FM10K_RXD_STATUS_L4E2 |
  299. FM10K_RXD_STATUS_IPE |
  300. FM10K_RXD_STATUS_IPE2)) {
  301. ring->rx_stats.csum_err++;
  302. return;
  303. }
  304. /* It must be a TCP or UDP packet with a valid checksum */
  305. if (fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_L4CS2))
  306. skb->encapsulation = true;
  307. else if (!fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_L4CS))
  308. return;
  309. skb->ip_summed = CHECKSUM_UNNECESSARY;
  310. }
  311. #define FM10K_RSS_L4_TYPES_MASK \
  312. ((1ul << FM10K_RSSTYPE_IPV4_TCP) | \
  313. (1ul << FM10K_RSSTYPE_IPV4_UDP) | \
  314. (1ul << FM10K_RSSTYPE_IPV6_TCP) | \
  315. (1ul << FM10K_RSSTYPE_IPV6_UDP))
  316. static inline void fm10k_rx_hash(struct fm10k_ring *ring,
  317. union fm10k_rx_desc *rx_desc,
  318. struct sk_buff *skb)
  319. {
  320. u16 rss_type;
  321. if (!(ring->netdev->features & NETIF_F_RXHASH))
  322. return;
  323. rss_type = le16_to_cpu(rx_desc->w.pkt_info) & FM10K_RXD_RSSTYPE_MASK;
  324. if (!rss_type)
  325. return;
  326. skb_set_hash(skb, le32_to_cpu(rx_desc->d.rss),
  327. (FM10K_RSS_L4_TYPES_MASK & (1ul << rss_type)) ?
  328. PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
  329. }
  330. static void fm10k_rx_hwtstamp(struct fm10k_ring *rx_ring,
  331. union fm10k_rx_desc *rx_desc,
  332. struct sk_buff *skb)
  333. {
  334. struct fm10k_intfc *interface = rx_ring->q_vector->interface;
  335. FM10K_CB(skb)->tstamp = rx_desc->q.timestamp;
  336. if (unlikely(interface->flags & FM10K_FLAG_RX_TS_ENABLED))
  337. fm10k_systime_to_hwtstamp(interface, skb_hwtstamps(skb),
  338. le64_to_cpu(rx_desc->q.timestamp));
  339. }
  340. static void fm10k_type_trans(struct fm10k_ring *rx_ring,
  341. union fm10k_rx_desc *rx_desc,
  342. struct sk_buff *skb)
  343. {
  344. struct net_device *dev = rx_ring->netdev;
  345. struct fm10k_l2_accel *l2_accel = rcu_dereference_bh(rx_ring->l2_accel);
  346. /* check to see if DGLORT belongs to a MACVLAN */
  347. if (l2_accel) {
  348. u16 idx = le16_to_cpu(FM10K_CB(skb)->fi.w.dglort) - 1;
  349. idx -= l2_accel->dglort;
  350. if (idx < l2_accel->size && l2_accel->macvlan[idx])
  351. dev = l2_accel->macvlan[idx];
  352. else
  353. l2_accel = NULL;
  354. }
  355. skb->protocol = eth_type_trans(skb, dev);
  356. if (!l2_accel)
  357. return;
  358. /* update MACVLAN statistics */
  359. macvlan_count_rx(netdev_priv(dev), skb->len + ETH_HLEN, 1,
  360. !!(rx_desc->w.hdr_info &
  361. cpu_to_le16(FM10K_RXD_HDR_INFO_XC_MASK)));
  362. }
  363. /**
  364. * fm10k_process_skb_fields - Populate skb header fields from Rx descriptor
  365. * @rx_ring: rx descriptor ring packet is being transacted on
  366. * @rx_desc: pointer to the EOP Rx descriptor
  367. * @skb: pointer to current skb being populated
  368. *
  369. * This function checks the ring, descriptor, and packet information in
  370. * order to populate the hash, checksum, VLAN, timestamp, protocol, and
  371. * other fields within the skb.
  372. **/
  373. static unsigned int fm10k_process_skb_fields(struct fm10k_ring *rx_ring,
  374. union fm10k_rx_desc *rx_desc,
  375. struct sk_buff *skb)
  376. {
  377. unsigned int len = skb->len;
  378. fm10k_rx_hash(rx_ring, rx_desc, skb);
  379. fm10k_rx_checksum(rx_ring, rx_desc, skb);
  380. fm10k_rx_hwtstamp(rx_ring, rx_desc, skb);
  381. FM10K_CB(skb)->fi.w.vlan = rx_desc->w.vlan;
  382. skb_record_rx_queue(skb, rx_ring->queue_index);
  383. FM10K_CB(skb)->fi.d.glort = rx_desc->d.glort;
  384. if (rx_desc->w.vlan) {
  385. u16 vid = le16_to_cpu(rx_desc->w.vlan);
  386. if (vid != rx_ring->vid)
  387. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  388. }
  389. fm10k_type_trans(rx_ring, rx_desc, skb);
  390. return len;
  391. }
  392. /**
  393. * fm10k_is_non_eop - process handling of non-EOP buffers
  394. * @rx_ring: Rx ring being processed
  395. * @rx_desc: Rx descriptor for current buffer
  396. *
  397. * This function updates next to clean. If the buffer is an EOP buffer
  398. * this function exits returning false, otherwise it will place the
  399. * sk_buff in the next buffer to be chained and return true indicating
  400. * that this is in fact a non-EOP buffer.
  401. **/
  402. static bool fm10k_is_non_eop(struct fm10k_ring *rx_ring,
  403. union fm10k_rx_desc *rx_desc)
  404. {
  405. u32 ntc = rx_ring->next_to_clean + 1;
  406. /* fetch, update, and store next to clean */
  407. ntc = (ntc < rx_ring->count) ? ntc : 0;
  408. rx_ring->next_to_clean = ntc;
  409. prefetch(FM10K_RX_DESC(rx_ring, ntc));
  410. if (likely(fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_EOP)))
  411. return false;
  412. return true;
  413. }
  414. /**
  415. * fm10k_pull_tail - fm10k specific version of skb_pull_tail
  416. * @rx_ring: rx descriptor ring packet is being transacted on
  417. * @rx_desc: pointer to the EOP Rx descriptor
  418. * @skb: pointer to current skb being adjusted
  419. *
  420. * This function is an fm10k specific version of __pskb_pull_tail. The
  421. * main difference between this version and the original function is that
  422. * this function can make several assumptions about the state of things
  423. * that allow for significant optimizations versus the standard function.
  424. * As a result we can do things like drop a frag and maintain an accurate
  425. * truesize for the skb.
  426. */
  427. static void fm10k_pull_tail(struct fm10k_ring *rx_ring,
  428. union fm10k_rx_desc *rx_desc,
  429. struct sk_buff *skb)
  430. {
  431. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
  432. unsigned char *va;
  433. unsigned int pull_len;
  434. /* it is valid to use page_address instead of kmap since we are
  435. * working with pages allocated out of the lomem pool per
  436. * alloc_page(GFP_ATOMIC)
  437. */
  438. va = skb_frag_address(frag);
  439. /* we need the header to contain the greater of either ETH_HLEN or
  440. * 60 bytes if the skb->len is less than 60 for skb_pad.
  441. */
  442. pull_len = eth_get_headlen(va, FM10K_RX_HDR_LEN);
  443. /* align pull length to size of long to optimize memcpy performance */
  444. skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
  445. /* update all of the pointers */
  446. skb_frag_size_sub(frag, pull_len);
  447. frag->page_offset += pull_len;
  448. skb->data_len -= pull_len;
  449. skb->tail += pull_len;
  450. }
  451. /**
  452. * fm10k_cleanup_headers - Correct corrupted or empty headers
  453. * @rx_ring: rx descriptor ring packet is being transacted on
  454. * @rx_desc: pointer to the EOP Rx descriptor
  455. * @skb: pointer to current skb being fixed
  456. *
  457. * Address the case where we are pulling data in on pages only
  458. * and as such no data is present in the skb header.
  459. *
  460. * In addition if skb is not at least 60 bytes we need to pad it so that
  461. * it is large enough to qualify as a valid Ethernet frame.
  462. *
  463. * Returns true if an error was encountered and skb was freed.
  464. **/
  465. static bool fm10k_cleanup_headers(struct fm10k_ring *rx_ring,
  466. union fm10k_rx_desc *rx_desc,
  467. struct sk_buff *skb)
  468. {
  469. if (unlikely((fm10k_test_staterr(rx_desc,
  470. FM10K_RXD_STATUS_RXE)))) {
  471. dev_kfree_skb_any(skb);
  472. rx_ring->rx_stats.errors++;
  473. return true;
  474. }
  475. /* place header in linear portion of buffer */
  476. if (skb_is_nonlinear(skb))
  477. fm10k_pull_tail(rx_ring, rx_desc, skb);
  478. /* if skb_pad returns an error the skb was freed */
  479. if (unlikely(skb->len < 60)) {
  480. int pad_len = 60 - skb->len;
  481. if (skb_pad(skb, pad_len))
  482. return true;
  483. __skb_put(skb, pad_len);
  484. }
  485. return false;
  486. }
  487. /**
  488. * fm10k_receive_skb - helper function to handle rx indications
  489. * @q_vector: structure containing interrupt and ring information
  490. * @skb: packet to send up
  491. **/
  492. static void fm10k_receive_skb(struct fm10k_q_vector *q_vector,
  493. struct sk_buff *skb)
  494. {
  495. napi_gro_receive(&q_vector->napi, skb);
  496. }
  497. static bool fm10k_clean_rx_irq(struct fm10k_q_vector *q_vector,
  498. struct fm10k_ring *rx_ring,
  499. int budget)
  500. {
  501. struct sk_buff *skb = rx_ring->skb;
  502. unsigned int total_bytes = 0, total_packets = 0;
  503. u16 cleaned_count = fm10k_desc_unused(rx_ring);
  504. do {
  505. union fm10k_rx_desc *rx_desc;
  506. /* return some buffers to hardware, one at a time is too slow */
  507. if (cleaned_count >= FM10K_RX_BUFFER_WRITE) {
  508. fm10k_alloc_rx_buffers(rx_ring, cleaned_count);
  509. cleaned_count = 0;
  510. }
  511. rx_desc = FM10K_RX_DESC(rx_ring, rx_ring->next_to_clean);
  512. if (!fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_DD))
  513. break;
  514. /* This memory barrier is needed to keep us from reading
  515. * any other fields out of the rx_desc until we know the
  516. * RXD_STATUS_DD bit is set
  517. */
  518. rmb();
  519. /* retrieve a buffer from the ring */
  520. skb = fm10k_fetch_rx_buffer(rx_ring, rx_desc, skb);
  521. /* exit if we failed to retrieve a buffer */
  522. if (!skb)
  523. break;
  524. cleaned_count++;
  525. /* fetch next buffer in frame if non-eop */
  526. if (fm10k_is_non_eop(rx_ring, rx_desc))
  527. continue;
  528. /* verify the packet layout is correct */
  529. if (fm10k_cleanup_headers(rx_ring, rx_desc, skb)) {
  530. skb = NULL;
  531. continue;
  532. }
  533. /* populate checksum, timestamp, VLAN, and protocol */
  534. total_bytes += fm10k_process_skb_fields(rx_ring, rx_desc, skb);
  535. fm10k_receive_skb(q_vector, skb);
  536. /* reset skb pointer */
  537. skb = NULL;
  538. /* update budget accounting */
  539. total_packets++;
  540. } while (likely(total_packets < budget));
  541. /* place incomplete frames back on ring for completion */
  542. rx_ring->skb = skb;
  543. u64_stats_update_begin(&rx_ring->syncp);
  544. rx_ring->stats.packets += total_packets;
  545. rx_ring->stats.bytes += total_bytes;
  546. u64_stats_update_end(&rx_ring->syncp);
  547. q_vector->rx.total_packets += total_packets;
  548. q_vector->rx.total_bytes += total_bytes;
  549. return total_packets < budget;
  550. }
  551. #define VXLAN_HLEN (sizeof(struct udphdr) + 8)
  552. static struct ethhdr *fm10k_port_is_vxlan(struct sk_buff *skb)
  553. {
  554. struct fm10k_intfc *interface = netdev_priv(skb->dev);
  555. struct fm10k_vxlan_port *vxlan_port;
  556. /* we can only offload a vxlan if we recognize it as such */
  557. vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
  558. struct fm10k_vxlan_port, list);
  559. if (!vxlan_port)
  560. return NULL;
  561. if (vxlan_port->port != udp_hdr(skb)->dest)
  562. return NULL;
  563. /* return offset of udp_hdr plus 8 bytes for VXLAN header */
  564. return (struct ethhdr *)(skb_transport_header(skb) + VXLAN_HLEN);
  565. }
  566. #define FM10K_NVGRE_RESERVED0_FLAGS htons(0x9FFF)
  567. #define NVGRE_TNI htons(0x2000)
  568. struct fm10k_nvgre_hdr {
  569. __be16 flags;
  570. __be16 proto;
  571. __be32 tni;
  572. };
  573. static struct ethhdr *fm10k_gre_is_nvgre(struct sk_buff *skb)
  574. {
  575. struct fm10k_nvgre_hdr *nvgre_hdr;
  576. int hlen = ip_hdrlen(skb);
  577. /* currently only IPv4 is supported due to hlen above */
  578. if (vlan_get_protocol(skb) != htons(ETH_P_IP))
  579. return NULL;
  580. /* our transport header should be NVGRE */
  581. nvgre_hdr = (struct fm10k_nvgre_hdr *)(skb_network_header(skb) + hlen);
  582. /* verify all reserved flags are 0 */
  583. if (nvgre_hdr->flags & FM10K_NVGRE_RESERVED0_FLAGS)
  584. return NULL;
  585. /* verify protocol is transparent Ethernet bridging */
  586. if (nvgre_hdr->proto != htons(ETH_P_TEB))
  587. return NULL;
  588. /* report start of ethernet header */
  589. if (nvgre_hdr->flags & NVGRE_TNI)
  590. return (struct ethhdr *)(nvgre_hdr + 1);
  591. return (struct ethhdr *)(&nvgre_hdr->tni);
  592. }
  593. static __be16 fm10k_tx_encap_offload(struct sk_buff *skb)
  594. {
  595. struct ethhdr *eth_hdr;
  596. u8 l4_hdr = 0;
  597. switch (vlan_get_protocol(skb)) {
  598. case htons(ETH_P_IP):
  599. l4_hdr = ip_hdr(skb)->protocol;
  600. break;
  601. case htons(ETH_P_IPV6):
  602. l4_hdr = ipv6_hdr(skb)->nexthdr;
  603. break;
  604. default:
  605. return 0;
  606. }
  607. switch (l4_hdr) {
  608. case IPPROTO_UDP:
  609. eth_hdr = fm10k_port_is_vxlan(skb);
  610. break;
  611. case IPPROTO_GRE:
  612. eth_hdr = fm10k_gre_is_nvgre(skb);
  613. break;
  614. default:
  615. return 0;
  616. }
  617. if (!eth_hdr)
  618. return 0;
  619. switch (eth_hdr->h_proto) {
  620. case htons(ETH_P_IP):
  621. case htons(ETH_P_IPV6):
  622. break;
  623. default:
  624. return 0;
  625. }
  626. return eth_hdr->h_proto;
  627. }
  628. static int fm10k_tso(struct fm10k_ring *tx_ring,
  629. struct fm10k_tx_buffer *first)
  630. {
  631. struct sk_buff *skb = first->skb;
  632. struct fm10k_tx_desc *tx_desc;
  633. unsigned char *th;
  634. u8 hdrlen;
  635. if (skb->ip_summed != CHECKSUM_PARTIAL)
  636. return 0;
  637. if (!skb_is_gso(skb))
  638. return 0;
  639. /* compute header lengths */
  640. if (skb->encapsulation) {
  641. if (!fm10k_tx_encap_offload(skb))
  642. goto err_vxlan;
  643. th = skb_inner_transport_header(skb);
  644. } else {
  645. th = skb_transport_header(skb);
  646. }
  647. /* compute offset from SOF to transport header and add header len */
  648. hdrlen = (th - skb->data) + (((struct tcphdr *)th)->doff << 2);
  649. first->tx_flags |= FM10K_TX_FLAGS_CSUM;
  650. /* update gso size and bytecount with header size */
  651. first->gso_segs = skb_shinfo(skb)->gso_segs;
  652. first->bytecount += (first->gso_segs - 1) * hdrlen;
  653. /* populate Tx descriptor header size and mss */
  654. tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
  655. tx_desc->hdrlen = hdrlen;
  656. tx_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
  657. return 1;
  658. err_vxlan:
  659. tx_ring->netdev->features &= ~NETIF_F_GSO_UDP_TUNNEL;
  660. if (!net_ratelimit())
  661. netdev_err(tx_ring->netdev,
  662. "TSO requested for unsupported tunnel, disabling offload\n");
  663. return -1;
  664. }
  665. static void fm10k_tx_csum(struct fm10k_ring *tx_ring,
  666. struct fm10k_tx_buffer *first)
  667. {
  668. struct sk_buff *skb = first->skb;
  669. struct fm10k_tx_desc *tx_desc;
  670. union {
  671. struct iphdr *ipv4;
  672. struct ipv6hdr *ipv6;
  673. u8 *raw;
  674. } network_hdr;
  675. __be16 protocol;
  676. u8 l4_hdr = 0;
  677. if (skb->ip_summed != CHECKSUM_PARTIAL)
  678. goto no_csum;
  679. if (skb->encapsulation) {
  680. protocol = fm10k_tx_encap_offload(skb);
  681. if (!protocol) {
  682. if (skb_checksum_help(skb)) {
  683. dev_warn(tx_ring->dev,
  684. "failed to offload encap csum!\n");
  685. tx_ring->tx_stats.csum_err++;
  686. }
  687. goto no_csum;
  688. }
  689. network_hdr.raw = skb_inner_network_header(skb);
  690. } else {
  691. protocol = vlan_get_protocol(skb);
  692. network_hdr.raw = skb_network_header(skb);
  693. }
  694. switch (protocol) {
  695. case htons(ETH_P_IP):
  696. l4_hdr = network_hdr.ipv4->protocol;
  697. break;
  698. case htons(ETH_P_IPV6):
  699. l4_hdr = network_hdr.ipv6->nexthdr;
  700. break;
  701. default:
  702. if (unlikely(net_ratelimit())) {
  703. dev_warn(tx_ring->dev,
  704. "partial checksum but ip version=%x!\n",
  705. protocol);
  706. }
  707. tx_ring->tx_stats.csum_err++;
  708. goto no_csum;
  709. }
  710. switch (l4_hdr) {
  711. case IPPROTO_TCP:
  712. case IPPROTO_UDP:
  713. break;
  714. case IPPROTO_GRE:
  715. if (skb->encapsulation)
  716. break;
  717. default:
  718. if (unlikely(net_ratelimit())) {
  719. dev_warn(tx_ring->dev,
  720. "partial checksum but l4 proto=%x!\n",
  721. l4_hdr);
  722. }
  723. tx_ring->tx_stats.csum_err++;
  724. goto no_csum;
  725. }
  726. /* update TX checksum flag */
  727. first->tx_flags |= FM10K_TX_FLAGS_CSUM;
  728. no_csum:
  729. /* populate Tx descriptor header size and mss */
  730. tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
  731. tx_desc->hdrlen = 0;
  732. tx_desc->mss = 0;
  733. }
  734. #define FM10K_SET_FLAG(_input, _flag, _result) \
  735. ((_flag <= _result) ? \
  736. ((u32)(_input & _flag) * (_result / _flag)) : \
  737. ((u32)(_input & _flag) / (_flag / _result)))
  738. static u8 fm10k_tx_desc_flags(struct sk_buff *skb, u32 tx_flags)
  739. {
  740. /* set type for advanced descriptor with frame checksum insertion */
  741. u32 desc_flags = 0;
  742. /* set timestamping bits */
  743. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  744. likely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  745. desc_flags |= FM10K_TXD_FLAG_TIME;
  746. /* set checksum offload bits */
  747. desc_flags |= FM10K_SET_FLAG(tx_flags, FM10K_TX_FLAGS_CSUM,
  748. FM10K_TXD_FLAG_CSUM);
  749. return desc_flags;
  750. }
  751. static bool fm10k_tx_desc_push(struct fm10k_ring *tx_ring,
  752. struct fm10k_tx_desc *tx_desc, u16 i,
  753. dma_addr_t dma, unsigned int size, u8 desc_flags)
  754. {
  755. /* set RS and INT for last frame in a cache line */
  756. if ((++i & (FM10K_TXD_WB_FIFO_SIZE - 1)) == 0)
  757. desc_flags |= FM10K_TXD_FLAG_RS | FM10K_TXD_FLAG_INT;
  758. /* record values to descriptor */
  759. tx_desc->buffer_addr = cpu_to_le64(dma);
  760. tx_desc->flags = desc_flags;
  761. tx_desc->buflen = cpu_to_le16(size);
  762. /* return true if we just wrapped the ring */
  763. return i == tx_ring->count;
  764. }
  765. static int __fm10k_maybe_stop_tx(struct fm10k_ring *tx_ring, u16 size)
  766. {
  767. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  768. smp_mb();
  769. /* We need to check again in a case another CPU has just
  770. * made room available. */
  771. if (likely(fm10k_desc_unused(tx_ring) < size))
  772. return -EBUSY;
  773. /* A reprieve! - use start_queue because it doesn't call schedule */
  774. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  775. ++tx_ring->tx_stats.restart_queue;
  776. return 0;
  777. }
  778. static inline int fm10k_maybe_stop_tx(struct fm10k_ring *tx_ring, u16 size)
  779. {
  780. if (likely(fm10k_desc_unused(tx_ring) >= size))
  781. return 0;
  782. return __fm10k_maybe_stop_tx(tx_ring, size);
  783. }
  784. static void fm10k_tx_map(struct fm10k_ring *tx_ring,
  785. struct fm10k_tx_buffer *first)
  786. {
  787. struct sk_buff *skb = first->skb;
  788. struct fm10k_tx_buffer *tx_buffer;
  789. struct fm10k_tx_desc *tx_desc;
  790. struct skb_frag_struct *frag;
  791. unsigned char *data;
  792. dma_addr_t dma;
  793. unsigned int data_len, size;
  794. u32 tx_flags = first->tx_flags;
  795. u16 i = tx_ring->next_to_use;
  796. u8 flags = fm10k_tx_desc_flags(skb, tx_flags);
  797. tx_desc = FM10K_TX_DESC(tx_ring, i);
  798. /* add HW VLAN tag */
  799. if (vlan_tx_tag_present(skb))
  800. tx_desc->vlan = cpu_to_le16(vlan_tx_tag_get(skb));
  801. else
  802. tx_desc->vlan = 0;
  803. size = skb_headlen(skb);
  804. data = skb->data;
  805. dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
  806. data_len = skb->data_len;
  807. tx_buffer = first;
  808. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  809. if (dma_mapping_error(tx_ring->dev, dma))
  810. goto dma_error;
  811. /* record length, and DMA address */
  812. dma_unmap_len_set(tx_buffer, len, size);
  813. dma_unmap_addr_set(tx_buffer, dma, dma);
  814. while (unlikely(size > FM10K_MAX_DATA_PER_TXD)) {
  815. if (fm10k_tx_desc_push(tx_ring, tx_desc++, i++, dma,
  816. FM10K_MAX_DATA_PER_TXD, flags)) {
  817. tx_desc = FM10K_TX_DESC(tx_ring, 0);
  818. i = 0;
  819. }
  820. dma += FM10K_MAX_DATA_PER_TXD;
  821. size -= FM10K_MAX_DATA_PER_TXD;
  822. }
  823. if (likely(!data_len))
  824. break;
  825. if (fm10k_tx_desc_push(tx_ring, tx_desc++, i++,
  826. dma, size, flags)) {
  827. tx_desc = FM10K_TX_DESC(tx_ring, 0);
  828. i = 0;
  829. }
  830. size = skb_frag_size(frag);
  831. data_len -= size;
  832. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  833. DMA_TO_DEVICE);
  834. tx_buffer = &tx_ring->tx_buffer[i];
  835. }
  836. /* write last descriptor with LAST bit set */
  837. flags |= FM10K_TXD_FLAG_LAST;
  838. if (fm10k_tx_desc_push(tx_ring, tx_desc, i++, dma, size, flags))
  839. i = 0;
  840. /* record bytecount for BQL */
  841. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  842. /* record SW timestamp if HW timestamp is not available */
  843. skb_tx_timestamp(first->skb);
  844. /* Force memory writes to complete before letting h/w know there
  845. * are new descriptors to fetch. (Only applicable for weak-ordered
  846. * memory model archs, such as IA-64).
  847. *
  848. * We also need this memory barrier to make certain all of the
  849. * status bits have been updated before next_to_watch is written.
  850. */
  851. wmb();
  852. /* set next_to_watch value indicating a packet is present */
  853. first->next_to_watch = tx_desc;
  854. tx_ring->next_to_use = i;
  855. /* Make sure there is space in the ring for the next send. */
  856. fm10k_maybe_stop_tx(tx_ring, DESC_NEEDED);
  857. /* notify HW of packet */
  858. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  859. writel(i, tx_ring->tail);
  860. /* we need this if more than one processor can write to our tail
  861. * at a time, it synchronizes IO on IA64/Altix systems
  862. */
  863. mmiowb();
  864. }
  865. return;
  866. dma_error:
  867. dev_err(tx_ring->dev, "TX DMA map failed\n");
  868. /* clear dma mappings for failed tx_buffer map */
  869. for (;;) {
  870. tx_buffer = &tx_ring->tx_buffer[i];
  871. fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer);
  872. if (tx_buffer == first)
  873. break;
  874. if (i == 0)
  875. i = tx_ring->count;
  876. i--;
  877. }
  878. tx_ring->next_to_use = i;
  879. }
  880. netdev_tx_t fm10k_xmit_frame_ring(struct sk_buff *skb,
  881. struct fm10k_ring *tx_ring)
  882. {
  883. struct fm10k_tx_buffer *first;
  884. int tso;
  885. u32 tx_flags = 0;
  886. #if PAGE_SIZE > FM10K_MAX_DATA_PER_TXD
  887. unsigned short f;
  888. #endif
  889. u16 count = TXD_USE_COUNT(skb_headlen(skb));
  890. /* need: 1 descriptor per page * PAGE_SIZE/FM10K_MAX_DATA_PER_TXD,
  891. * + 1 desc for skb_headlen/FM10K_MAX_DATA_PER_TXD,
  892. * + 2 desc gap to keep tail from touching head
  893. * otherwise try next time
  894. */
  895. #if PAGE_SIZE > FM10K_MAX_DATA_PER_TXD
  896. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  897. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  898. #else
  899. count += skb_shinfo(skb)->nr_frags;
  900. #endif
  901. if (fm10k_maybe_stop_tx(tx_ring, count + 3)) {
  902. tx_ring->tx_stats.tx_busy++;
  903. return NETDEV_TX_BUSY;
  904. }
  905. /* record the location of the first descriptor for this packet */
  906. first = &tx_ring->tx_buffer[tx_ring->next_to_use];
  907. first->skb = skb;
  908. first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN);
  909. first->gso_segs = 1;
  910. /* record initial flags and protocol */
  911. first->tx_flags = tx_flags;
  912. tso = fm10k_tso(tx_ring, first);
  913. if (tso < 0)
  914. goto out_drop;
  915. else if (!tso)
  916. fm10k_tx_csum(tx_ring, first);
  917. fm10k_tx_map(tx_ring, first);
  918. return NETDEV_TX_OK;
  919. out_drop:
  920. dev_kfree_skb_any(first->skb);
  921. first->skb = NULL;
  922. return NETDEV_TX_OK;
  923. }
  924. static u64 fm10k_get_tx_completed(struct fm10k_ring *ring)
  925. {
  926. return ring->stats.packets;
  927. }
  928. static u64 fm10k_get_tx_pending(struct fm10k_ring *ring)
  929. {
  930. /* use SW head and tail until we have real hardware */
  931. u32 head = ring->next_to_clean;
  932. u32 tail = ring->next_to_use;
  933. return ((head <= tail) ? tail : tail + ring->count) - head;
  934. }
  935. bool fm10k_check_tx_hang(struct fm10k_ring *tx_ring)
  936. {
  937. u32 tx_done = fm10k_get_tx_completed(tx_ring);
  938. u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
  939. u32 tx_pending = fm10k_get_tx_pending(tx_ring);
  940. clear_check_for_tx_hang(tx_ring);
  941. /* Check for a hung queue, but be thorough. This verifies
  942. * that a transmit has been completed since the previous
  943. * check AND there is at least one packet pending. By
  944. * requiring this to fail twice we avoid races with
  945. * clearing the ARMED bit and conditions where we
  946. * run the check_tx_hang logic with a transmit completion
  947. * pending but without time to complete it yet.
  948. */
  949. if (!tx_pending || (tx_done_old != tx_done)) {
  950. /* update completed stats and continue */
  951. tx_ring->tx_stats.tx_done_old = tx_done;
  952. /* reset the countdown */
  953. clear_bit(__FM10K_HANG_CHECK_ARMED, &tx_ring->state);
  954. return false;
  955. }
  956. /* make sure it is true for two checks in a row */
  957. return test_and_set_bit(__FM10K_HANG_CHECK_ARMED, &tx_ring->state);
  958. }
  959. /**
  960. * fm10k_tx_timeout_reset - initiate reset due to Tx timeout
  961. * @interface: driver private struct
  962. **/
  963. void fm10k_tx_timeout_reset(struct fm10k_intfc *interface)
  964. {
  965. /* Do the reset outside of interrupt context */
  966. if (!test_bit(__FM10K_DOWN, &interface->state)) {
  967. netdev_err(interface->netdev, "Reset interface\n");
  968. interface->tx_timeout_count++;
  969. interface->flags |= FM10K_FLAG_RESET_REQUESTED;
  970. fm10k_service_event_schedule(interface);
  971. }
  972. }
  973. /**
  974. * fm10k_clean_tx_irq - Reclaim resources after transmit completes
  975. * @q_vector: structure containing interrupt and ring information
  976. * @tx_ring: tx ring to clean
  977. **/
  978. static bool fm10k_clean_tx_irq(struct fm10k_q_vector *q_vector,
  979. struct fm10k_ring *tx_ring)
  980. {
  981. struct fm10k_intfc *interface = q_vector->interface;
  982. struct fm10k_tx_buffer *tx_buffer;
  983. struct fm10k_tx_desc *tx_desc;
  984. unsigned int total_bytes = 0, total_packets = 0;
  985. unsigned int budget = q_vector->tx.work_limit;
  986. unsigned int i = tx_ring->next_to_clean;
  987. if (test_bit(__FM10K_DOWN, &interface->state))
  988. return true;
  989. tx_buffer = &tx_ring->tx_buffer[i];
  990. tx_desc = FM10K_TX_DESC(tx_ring, i);
  991. i -= tx_ring->count;
  992. do {
  993. struct fm10k_tx_desc *eop_desc = tx_buffer->next_to_watch;
  994. /* if next_to_watch is not set then there is no work pending */
  995. if (!eop_desc)
  996. break;
  997. /* prevent any other reads prior to eop_desc */
  998. read_barrier_depends();
  999. /* if DD is not set pending work has not been completed */
  1000. if (!(eop_desc->flags & FM10K_TXD_FLAG_DONE))
  1001. break;
  1002. /* clear next_to_watch to prevent false hangs */
  1003. tx_buffer->next_to_watch = NULL;
  1004. /* update the statistics for this packet */
  1005. total_bytes += tx_buffer->bytecount;
  1006. total_packets += tx_buffer->gso_segs;
  1007. /* free the skb */
  1008. dev_consume_skb_any(tx_buffer->skb);
  1009. /* unmap skb header data */
  1010. dma_unmap_single(tx_ring->dev,
  1011. dma_unmap_addr(tx_buffer, dma),
  1012. dma_unmap_len(tx_buffer, len),
  1013. DMA_TO_DEVICE);
  1014. /* clear tx_buffer data */
  1015. tx_buffer->skb = NULL;
  1016. dma_unmap_len_set(tx_buffer, len, 0);
  1017. /* unmap remaining buffers */
  1018. while (tx_desc != eop_desc) {
  1019. tx_buffer++;
  1020. tx_desc++;
  1021. i++;
  1022. if (unlikely(!i)) {
  1023. i -= tx_ring->count;
  1024. tx_buffer = tx_ring->tx_buffer;
  1025. tx_desc = FM10K_TX_DESC(tx_ring, 0);
  1026. }
  1027. /* unmap any remaining paged data */
  1028. if (dma_unmap_len(tx_buffer, len)) {
  1029. dma_unmap_page(tx_ring->dev,
  1030. dma_unmap_addr(tx_buffer, dma),
  1031. dma_unmap_len(tx_buffer, len),
  1032. DMA_TO_DEVICE);
  1033. dma_unmap_len_set(tx_buffer, len, 0);
  1034. }
  1035. }
  1036. /* move us one more past the eop_desc for start of next pkt */
  1037. tx_buffer++;
  1038. tx_desc++;
  1039. i++;
  1040. if (unlikely(!i)) {
  1041. i -= tx_ring->count;
  1042. tx_buffer = tx_ring->tx_buffer;
  1043. tx_desc = FM10K_TX_DESC(tx_ring, 0);
  1044. }
  1045. /* issue prefetch for next Tx descriptor */
  1046. prefetch(tx_desc);
  1047. /* update budget accounting */
  1048. budget--;
  1049. } while (likely(budget));
  1050. i += tx_ring->count;
  1051. tx_ring->next_to_clean = i;
  1052. u64_stats_update_begin(&tx_ring->syncp);
  1053. tx_ring->stats.bytes += total_bytes;
  1054. tx_ring->stats.packets += total_packets;
  1055. u64_stats_update_end(&tx_ring->syncp);
  1056. q_vector->tx.total_bytes += total_bytes;
  1057. q_vector->tx.total_packets += total_packets;
  1058. if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring)) {
  1059. /* schedule immediate reset if we believe we hung */
  1060. struct fm10k_hw *hw = &interface->hw;
  1061. netif_err(interface, drv, tx_ring->netdev,
  1062. "Detected Tx Unit Hang\n"
  1063. " Tx Queue <%d>\n"
  1064. " TDH, TDT <%x>, <%x>\n"
  1065. " next_to_use <%x>\n"
  1066. " next_to_clean <%x>\n",
  1067. tx_ring->queue_index,
  1068. fm10k_read_reg(hw, FM10K_TDH(tx_ring->reg_idx)),
  1069. fm10k_read_reg(hw, FM10K_TDT(tx_ring->reg_idx)),
  1070. tx_ring->next_to_use, i);
  1071. netif_stop_subqueue(tx_ring->netdev,
  1072. tx_ring->queue_index);
  1073. netif_info(interface, probe, tx_ring->netdev,
  1074. "tx hang %d detected on queue %d, resetting interface\n",
  1075. interface->tx_timeout_count + 1,
  1076. tx_ring->queue_index);
  1077. fm10k_tx_timeout_reset(interface);
  1078. /* the netdev is about to reset, no point in enabling stuff */
  1079. return true;
  1080. }
  1081. /* notify netdev of completed buffers */
  1082. netdev_tx_completed_queue(txring_txq(tx_ring),
  1083. total_packets, total_bytes);
  1084. #define TX_WAKE_THRESHOLD min_t(u16, FM10K_MIN_TXD - 1, DESC_NEEDED * 2)
  1085. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  1086. (fm10k_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
  1087. /* Make sure that anybody stopping the queue after this
  1088. * sees the new next_to_clean.
  1089. */
  1090. smp_mb();
  1091. if (__netif_subqueue_stopped(tx_ring->netdev,
  1092. tx_ring->queue_index) &&
  1093. !test_bit(__FM10K_DOWN, &interface->state)) {
  1094. netif_wake_subqueue(tx_ring->netdev,
  1095. tx_ring->queue_index);
  1096. ++tx_ring->tx_stats.restart_queue;
  1097. }
  1098. }
  1099. return !!budget;
  1100. }
  1101. /**
  1102. * fm10k_update_itr - update the dynamic ITR value based on packet size
  1103. *
  1104. * Stores a new ITR value based on strictly on packet size. The
  1105. * divisors and thresholds used by this function were determined based
  1106. * on theoretical maximum wire speed and testing data, in order to
  1107. * minimize response time while increasing bulk throughput.
  1108. *
  1109. * @ring_container: Container for rings to have ITR updated
  1110. **/
  1111. static void fm10k_update_itr(struct fm10k_ring_container *ring_container)
  1112. {
  1113. unsigned int avg_wire_size, packets;
  1114. /* Only update ITR if we are using adaptive setting */
  1115. if (!(ring_container->itr & FM10K_ITR_ADAPTIVE))
  1116. goto clear_counts;
  1117. packets = ring_container->total_packets;
  1118. if (!packets)
  1119. goto clear_counts;
  1120. avg_wire_size = ring_container->total_bytes / packets;
  1121. /* Add 24 bytes to size to account for CRC, preamble, and gap */
  1122. avg_wire_size += 24;
  1123. /* Don't starve jumbo frames */
  1124. if (avg_wire_size > 3000)
  1125. avg_wire_size = 3000;
  1126. /* Give a little boost to mid-size frames */
  1127. if ((avg_wire_size > 300) && (avg_wire_size < 1200))
  1128. avg_wire_size /= 3;
  1129. else
  1130. avg_wire_size /= 2;
  1131. /* write back value and retain adaptive flag */
  1132. ring_container->itr = avg_wire_size | FM10K_ITR_ADAPTIVE;
  1133. clear_counts:
  1134. ring_container->total_bytes = 0;
  1135. ring_container->total_packets = 0;
  1136. }
  1137. static void fm10k_qv_enable(struct fm10k_q_vector *q_vector)
  1138. {
  1139. /* Enable auto-mask and clear the current mask */
  1140. u32 itr = FM10K_ITR_ENABLE;
  1141. /* Update Tx ITR */
  1142. fm10k_update_itr(&q_vector->tx);
  1143. /* Update Rx ITR */
  1144. fm10k_update_itr(&q_vector->rx);
  1145. /* Store Tx itr in timer slot 0 */
  1146. itr |= (q_vector->tx.itr & FM10K_ITR_MAX);
  1147. /* Shift Rx itr to timer slot 1 */
  1148. itr |= (q_vector->rx.itr & FM10K_ITR_MAX) << FM10K_ITR_INTERVAL1_SHIFT;
  1149. /* Write the final value to the ITR register */
  1150. writel(itr, q_vector->itr);
  1151. }
  1152. static int fm10k_poll(struct napi_struct *napi, int budget)
  1153. {
  1154. struct fm10k_q_vector *q_vector =
  1155. container_of(napi, struct fm10k_q_vector, napi);
  1156. struct fm10k_ring *ring;
  1157. int per_ring_budget;
  1158. bool clean_complete = true;
  1159. fm10k_for_each_ring(ring, q_vector->tx)
  1160. clean_complete &= fm10k_clean_tx_irq(q_vector, ring);
  1161. /* attempt to distribute budget to each queue fairly, but don't
  1162. * allow the budget to go below 1 because we'll exit polling
  1163. */
  1164. if (q_vector->rx.count > 1)
  1165. per_ring_budget = max(budget/q_vector->rx.count, 1);
  1166. else
  1167. per_ring_budget = budget;
  1168. fm10k_for_each_ring(ring, q_vector->rx)
  1169. clean_complete &= fm10k_clean_rx_irq(q_vector, ring,
  1170. per_ring_budget);
  1171. /* If all work not completed, return budget and keep polling */
  1172. if (!clean_complete)
  1173. return budget;
  1174. /* all work done, exit the polling mode */
  1175. napi_complete(napi);
  1176. /* re-enable the q_vector */
  1177. fm10k_qv_enable(q_vector);
  1178. return 0;
  1179. }
  1180. /**
  1181. * fm10k_set_qos_queues: Allocate queues for a QOS-enabled device
  1182. * @interface: board private structure to initialize
  1183. *
  1184. * When QoS (Quality of Service) is enabled, allocate queues for
  1185. * each traffic class. If multiqueue isn't available,then abort QoS
  1186. * initialization.
  1187. *
  1188. * This function handles all combinations of Qos and RSS.
  1189. *
  1190. **/
  1191. static bool fm10k_set_qos_queues(struct fm10k_intfc *interface)
  1192. {
  1193. struct net_device *dev = interface->netdev;
  1194. struct fm10k_ring_feature *f;
  1195. int rss_i, i;
  1196. int pcs;
  1197. /* Map queue offset and counts onto allocated tx queues */
  1198. pcs = netdev_get_num_tc(dev);
  1199. if (pcs <= 1)
  1200. return false;
  1201. /* set QoS mask and indices */
  1202. f = &interface->ring_feature[RING_F_QOS];
  1203. f->indices = pcs;
  1204. f->mask = (1 << fls(pcs - 1)) - 1;
  1205. /* determine the upper limit for our current DCB mode */
  1206. rss_i = interface->hw.mac.max_queues / pcs;
  1207. rss_i = 1 << (fls(rss_i) - 1);
  1208. /* set RSS mask and indices */
  1209. f = &interface->ring_feature[RING_F_RSS];
  1210. rss_i = min_t(u16, rss_i, f->limit);
  1211. f->indices = rss_i;
  1212. f->mask = (1 << fls(rss_i - 1)) - 1;
  1213. /* configure pause class to queue mapping */
  1214. for (i = 0; i < pcs; i++)
  1215. netdev_set_tc_queue(dev, i, rss_i, rss_i * i);
  1216. interface->num_rx_queues = rss_i * pcs;
  1217. interface->num_tx_queues = rss_i * pcs;
  1218. return true;
  1219. }
  1220. /**
  1221. * fm10k_set_rss_queues: Allocate queues for RSS
  1222. * @interface: board private structure to initialize
  1223. *
  1224. * This is our "base" multiqueue mode. RSS (Receive Side Scaling) will try
  1225. * to allocate one Rx queue per CPU, and if available, one Tx queue per CPU.
  1226. *
  1227. **/
  1228. static bool fm10k_set_rss_queues(struct fm10k_intfc *interface)
  1229. {
  1230. struct fm10k_ring_feature *f;
  1231. u16 rss_i;
  1232. f = &interface->ring_feature[RING_F_RSS];
  1233. rss_i = min_t(u16, interface->hw.mac.max_queues, f->limit);
  1234. /* record indices and power of 2 mask for RSS */
  1235. f->indices = rss_i;
  1236. f->mask = (1 << fls(rss_i - 1)) - 1;
  1237. interface->num_rx_queues = rss_i;
  1238. interface->num_tx_queues = rss_i;
  1239. return true;
  1240. }
  1241. /**
  1242. * fm10k_set_num_queues: Allocate queues for device, feature dependent
  1243. * @interface: board private structure to initialize
  1244. *
  1245. * This is the top level queue allocation routine. The order here is very
  1246. * important, starting with the "most" number of features turned on at once,
  1247. * and ending with the smallest set of features. This way large combinations
  1248. * can be allocated if they're turned on, and smaller combinations are the
  1249. * fallthrough conditions.
  1250. *
  1251. **/
  1252. static void fm10k_set_num_queues(struct fm10k_intfc *interface)
  1253. {
  1254. /* Start with base case */
  1255. interface->num_rx_queues = 1;
  1256. interface->num_tx_queues = 1;
  1257. if (fm10k_set_qos_queues(interface))
  1258. return;
  1259. fm10k_set_rss_queues(interface);
  1260. }
  1261. /**
  1262. * fm10k_alloc_q_vector - Allocate memory for a single interrupt vector
  1263. * @interface: board private structure to initialize
  1264. * @v_count: q_vectors allocated on interface, used for ring interleaving
  1265. * @v_idx: index of vector in interface struct
  1266. * @txr_count: total number of Tx rings to allocate
  1267. * @txr_idx: index of first Tx ring to allocate
  1268. * @rxr_count: total number of Rx rings to allocate
  1269. * @rxr_idx: index of first Rx ring to allocate
  1270. *
  1271. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  1272. **/
  1273. static int fm10k_alloc_q_vector(struct fm10k_intfc *interface,
  1274. unsigned int v_count, unsigned int v_idx,
  1275. unsigned int txr_count, unsigned int txr_idx,
  1276. unsigned int rxr_count, unsigned int rxr_idx)
  1277. {
  1278. struct fm10k_q_vector *q_vector;
  1279. struct fm10k_ring *ring;
  1280. int ring_count, size;
  1281. ring_count = txr_count + rxr_count;
  1282. size = sizeof(struct fm10k_q_vector) +
  1283. (sizeof(struct fm10k_ring) * ring_count);
  1284. /* allocate q_vector and rings */
  1285. q_vector = kzalloc(size, GFP_KERNEL);
  1286. if (!q_vector)
  1287. return -ENOMEM;
  1288. /* initialize NAPI */
  1289. netif_napi_add(interface->netdev, &q_vector->napi,
  1290. fm10k_poll, NAPI_POLL_WEIGHT);
  1291. /* tie q_vector and interface together */
  1292. interface->q_vector[v_idx] = q_vector;
  1293. q_vector->interface = interface;
  1294. q_vector->v_idx = v_idx;
  1295. /* initialize pointer to rings */
  1296. ring = q_vector->ring;
  1297. /* save Tx ring container info */
  1298. q_vector->tx.ring = ring;
  1299. q_vector->tx.work_limit = FM10K_DEFAULT_TX_WORK;
  1300. q_vector->tx.itr = interface->tx_itr;
  1301. q_vector->tx.count = txr_count;
  1302. while (txr_count) {
  1303. /* assign generic ring traits */
  1304. ring->dev = &interface->pdev->dev;
  1305. ring->netdev = interface->netdev;
  1306. /* configure backlink on ring */
  1307. ring->q_vector = q_vector;
  1308. /* apply Tx specific ring traits */
  1309. ring->count = interface->tx_ring_count;
  1310. ring->queue_index = txr_idx;
  1311. /* assign ring to interface */
  1312. interface->tx_ring[txr_idx] = ring;
  1313. /* update count and index */
  1314. txr_count--;
  1315. txr_idx += v_count;
  1316. /* push pointer to next ring */
  1317. ring++;
  1318. }
  1319. /* save Rx ring container info */
  1320. q_vector->rx.ring = ring;
  1321. q_vector->rx.itr = interface->rx_itr;
  1322. q_vector->rx.count = rxr_count;
  1323. while (rxr_count) {
  1324. /* assign generic ring traits */
  1325. ring->dev = &interface->pdev->dev;
  1326. ring->netdev = interface->netdev;
  1327. rcu_assign_pointer(ring->l2_accel, interface->l2_accel);
  1328. /* configure backlink on ring */
  1329. ring->q_vector = q_vector;
  1330. /* apply Rx specific ring traits */
  1331. ring->count = interface->rx_ring_count;
  1332. ring->queue_index = rxr_idx;
  1333. /* assign ring to interface */
  1334. interface->rx_ring[rxr_idx] = ring;
  1335. /* update count and index */
  1336. rxr_count--;
  1337. rxr_idx += v_count;
  1338. /* push pointer to next ring */
  1339. ring++;
  1340. }
  1341. fm10k_dbg_q_vector_init(q_vector);
  1342. return 0;
  1343. }
  1344. /**
  1345. * fm10k_free_q_vector - Free memory allocated for specific interrupt vector
  1346. * @interface: board private structure to initialize
  1347. * @v_idx: Index of vector to be freed
  1348. *
  1349. * This function frees the memory allocated to the q_vector. In addition if
  1350. * NAPI is enabled it will delete any references to the NAPI struct prior
  1351. * to freeing the q_vector.
  1352. **/
  1353. static void fm10k_free_q_vector(struct fm10k_intfc *interface, int v_idx)
  1354. {
  1355. struct fm10k_q_vector *q_vector = interface->q_vector[v_idx];
  1356. struct fm10k_ring *ring;
  1357. fm10k_dbg_q_vector_exit(q_vector);
  1358. fm10k_for_each_ring(ring, q_vector->tx)
  1359. interface->tx_ring[ring->queue_index] = NULL;
  1360. fm10k_for_each_ring(ring, q_vector->rx)
  1361. interface->rx_ring[ring->queue_index] = NULL;
  1362. interface->q_vector[v_idx] = NULL;
  1363. netif_napi_del(&q_vector->napi);
  1364. kfree_rcu(q_vector, rcu);
  1365. }
  1366. /**
  1367. * fm10k_alloc_q_vectors - Allocate memory for interrupt vectors
  1368. * @interface: board private structure to initialize
  1369. *
  1370. * We allocate one q_vector per queue interrupt. If allocation fails we
  1371. * return -ENOMEM.
  1372. **/
  1373. static int fm10k_alloc_q_vectors(struct fm10k_intfc *interface)
  1374. {
  1375. unsigned int q_vectors = interface->num_q_vectors;
  1376. unsigned int rxr_remaining = interface->num_rx_queues;
  1377. unsigned int txr_remaining = interface->num_tx_queues;
  1378. unsigned int rxr_idx = 0, txr_idx = 0, v_idx = 0;
  1379. int err;
  1380. if (q_vectors >= (rxr_remaining + txr_remaining)) {
  1381. for (; rxr_remaining; v_idx++) {
  1382. err = fm10k_alloc_q_vector(interface, q_vectors, v_idx,
  1383. 0, 0, 1, rxr_idx);
  1384. if (err)
  1385. goto err_out;
  1386. /* update counts and index */
  1387. rxr_remaining--;
  1388. rxr_idx++;
  1389. }
  1390. }
  1391. for (; v_idx < q_vectors; v_idx++) {
  1392. int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
  1393. int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
  1394. err = fm10k_alloc_q_vector(interface, q_vectors, v_idx,
  1395. tqpv, txr_idx,
  1396. rqpv, rxr_idx);
  1397. if (err)
  1398. goto err_out;
  1399. /* update counts and index */
  1400. rxr_remaining -= rqpv;
  1401. txr_remaining -= tqpv;
  1402. rxr_idx++;
  1403. txr_idx++;
  1404. }
  1405. return 0;
  1406. err_out:
  1407. interface->num_tx_queues = 0;
  1408. interface->num_rx_queues = 0;
  1409. interface->num_q_vectors = 0;
  1410. while (v_idx--)
  1411. fm10k_free_q_vector(interface, v_idx);
  1412. return -ENOMEM;
  1413. }
  1414. /**
  1415. * fm10k_free_q_vectors - Free memory allocated for interrupt vectors
  1416. * @interface: board private structure to initialize
  1417. *
  1418. * This function frees the memory allocated to the q_vectors. In addition if
  1419. * NAPI is enabled it will delete any references to the NAPI struct prior
  1420. * to freeing the q_vector.
  1421. **/
  1422. static void fm10k_free_q_vectors(struct fm10k_intfc *interface)
  1423. {
  1424. int v_idx = interface->num_q_vectors;
  1425. interface->num_tx_queues = 0;
  1426. interface->num_rx_queues = 0;
  1427. interface->num_q_vectors = 0;
  1428. while (v_idx--)
  1429. fm10k_free_q_vector(interface, v_idx);
  1430. }
  1431. /**
  1432. * f10k_reset_msix_capability - reset MSI-X capability
  1433. * @interface: board private structure to initialize
  1434. *
  1435. * Reset the MSI-X capability back to its starting state
  1436. **/
  1437. static void fm10k_reset_msix_capability(struct fm10k_intfc *interface)
  1438. {
  1439. pci_disable_msix(interface->pdev);
  1440. kfree(interface->msix_entries);
  1441. interface->msix_entries = NULL;
  1442. }
  1443. /**
  1444. * f10k_init_msix_capability - configure MSI-X capability
  1445. * @interface: board private structure to initialize
  1446. *
  1447. * Attempt to configure the interrupts using the best available
  1448. * capabilities of the hardware and the kernel.
  1449. **/
  1450. static int fm10k_init_msix_capability(struct fm10k_intfc *interface)
  1451. {
  1452. struct fm10k_hw *hw = &interface->hw;
  1453. int v_budget, vector;
  1454. /* It's easy to be greedy for MSI-X vectors, but it really
  1455. * doesn't do us much good if we have a lot more vectors
  1456. * than CPU's. So let's be conservative and only ask for
  1457. * (roughly) the same number of vectors as there are CPU's.
  1458. * the default is to use pairs of vectors
  1459. */
  1460. v_budget = max(interface->num_rx_queues, interface->num_tx_queues);
  1461. v_budget = min_t(u16, v_budget, num_online_cpus());
  1462. /* account for vectors not related to queues */
  1463. v_budget += NON_Q_VECTORS(hw);
  1464. /* At the same time, hardware can only support a maximum of
  1465. * hw.mac->max_msix_vectors vectors. With features
  1466. * such as RSS and VMDq, we can easily surpass the number of Rx and Tx
  1467. * descriptor queues supported by our device. Thus, we cap it off in
  1468. * those rare cases where the cpu count also exceeds our vector limit.
  1469. */
  1470. v_budget = min_t(int, v_budget, hw->mac.max_msix_vectors);
  1471. /* A failure in MSI-X entry allocation is fatal. */
  1472. interface->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  1473. GFP_KERNEL);
  1474. if (!interface->msix_entries)
  1475. return -ENOMEM;
  1476. /* populate entry values */
  1477. for (vector = 0; vector < v_budget; vector++)
  1478. interface->msix_entries[vector].entry = vector;
  1479. /* Attempt to enable MSI-X with requested value */
  1480. v_budget = pci_enable_msix_range(interface->pdev,
  1481. interface->msix_entries,
  1482. MIN_MSIX_COUNT(hw),
  1483. v_budget);
  1484. if (v_budget < 0) {
  1485. kfree(interface->msix_entries);
  1486. interface->msix_entries = NULL;
  1487. return -ENOMEM;
  1488. }
  1489. /* record the number of queues available for q_vectors */
  1490. interface->num_q_vectors = v_budget - NON_Q_VECTORS(hw);
  1491. return 0;
  1492. }
  1493. /**
  1494. * fm10k_cache_ring_qos - Descriptor ring to register mapping for QoS
  1495. * @interface: Interface structure continaining rings and devices
  1496. *
  1497. * Cache the descriptor ring offsets for Qos
  1498. **/
  1499. static bool fm10k_cache_ring_qos(struct fm10k_intfc *interface)
  1500. {
  1501. struct net_device *dev = interface->netdev;
  1502. int pc, offset, rss_i, i, q_idx;
  1503. u16 pc_stride = interface->ring_feature[RING_F_QOS].mask + 1;
  1504. u8 num_pcs = netdev_get_num_tc(dev);
  1505. if (num_pcs <= 1)
  1506. return false;
  1507. rss_i = interface->ring_feature[RING_F_RSS].indices;
  1508. for (pc = 0, offset = 0; pc < num_pcs; pc++, offset += rss_i) {
  1509. q_idx = pc;
  1510. for (i = 0; i < rss_i; i++) {
  1511. interface->tx_ring[offset + i]->reg_idx = q_idx;
  1512. interface->tx_ring[offset + i]->qos_pc = pc;
  1513. interface->rx_ring[offset + i]->reg_idx = q_idx;
  1514. interface->rx_ring[offset + i]->qos_pc = pc;
  1515. q_idx += pc_stride;
  1516. }
  1517. }
  1518. return true;
  1519. }
  1520. /**
  1521. * fm10k_cache_ring_rss - Descriptor ring to register mapping for RSS
  1522. * @interface: Interface structure continaining rings and devices
  1523. *
  1524. * Cache the descriptor ring offsets for RSS
  1525. **/
  1526. static void fm10k_cache_ring_rss(struct fm10k_intfc *interface)
  1527. {
  1528. int i;
  1529. for (i = 0; i < interface->num_rx_queues; i++)
  1530. interface->rx_ring[i]->reg_idx = i;
  1531. for (i = 0; i < interface->num_tx_queues; i++)
  1532. interface->tx_ring[i]->reg_idx = i;
  1533. }
  1534. /**
  1535. * fm10k_assign_rings - Map rings to network devices
  1536. * @interface: Interface structure containing rings and devices
  1537. *
  1538. * This function is meant to go though and configure both the network
  1539. * devices so that they contain rings, and configure the rings so that
  1540. * they function with their network devices.
  1541. **/
  1542. static void fm10k_assign_rings(struct fm10k_intfc *interface)
  1543. {
  1544. if (fm10k_cache_ring_qos(interface))
  1545. return;
  1546. fm10k_cache_ring_rss(interface);
  1547. }
  1548. static void fm10k_init_reta(struct fm10k_intfc *interface)
  1549. {
  1550. u16 i, rss_i = interface->ring_feature[RING_F_RSS].indices;
  1551. u32 reta, base;
  1552. /* If the netdev is initialized we have to maintain table if possible */
  1553. if (interface->netdev->reg_state) {
  1554. for (i = FM10K_RETA_SIZE; i--;) {
  1555. reta = interface->reta[i];
  1556. if ((((reta << 24) >> 24) < rss_i) &&
  1557. (((reta << 16) >> 24) < rss_i) &&
  1558. (((reta << 8) >> 24) < rss_i) &&
  1559. (((reta) >> 24) < rss_i))
  1560. continue;
  1561. goto repopulate_reta;
  1562. }
  1563. /* do nothing if all of the elements are in bounds */
  1564. return;
  1565. }
  1566. repopulate_reta:
  1567. /* Populate the redirection table 4 entries at a time. To do this
  1568. * we are generating the results for n and n+2 and then interleaving
  1569. * those with the results with n+1 and n+3.
  1570. */
  1571. for (i = FM10K_RETA_SIZE; i--;) {
  1572. /* first pass generates n and n+2 */
  1573. base = ((i * 0x00040004) + 0x00020000) * rss_i;
  1574. reta = (base & 0x3F803F80) >> 7;
  1575. /* second pass generates n+1 and n+3 */
  1576. base += 0x00010001 * rss_i;
  1577. reta |= (base & 0x3F803F80) << 1;
  1578. interface->reta[i] = reta;
  1579. }
  1580. }
  1581. /**
  1582. * fm10k_init_queueing_scheme - Determine proper queueing scheme
  1583. * @interface: board private structure to initialize
  1584. *
  1585. * We determine which queueing scheme to use based on...
  1586. * - Hardware queue count (num_*_queues)
  1587. * - defined by miscellaneous hardware support/features (RSS, etc.)
  1588. **/
  1589. int fm10k_init_queueing_scheme(struct fm10k_intfc *interface)
  1590. {
  1591. int err;
  1592. /* Number of supported queues */
  1593. fm10k_set_num_queues(interface);
  1594. /* Configure MSI-X capability */
  1595. err = fm10k_init_msix_capability(interface);
  1596. if (err) {
  1597. dev_err(&interface->pdev->dev,
  1598. "Unable to initialize MSI-X capability\n");
  1599. return err;
  1600. }
  1601. /* Allocate memory for queues */
  1602. err = fm10k_alloc_q_vectors(interface);
  1603. if (err)
  1604. return err;
  1605. /* Map rings to devices, and map devices to physical queues */
  1606. fm10k_assign_rings(interface);
  1607. /* Initialize RSS redirection table */
  1608. fm10k_init_reta(interface);
  1609. return 0;
  1610. }
  1611. /**
  1612. * fm10k_clear_queueing_scheme - Clear the current queueing scheme settings
  1613. * @interface: board private structure to clear queueing scheme on
  1614. *
  1615. * We go through and clear queueing specific resources and reset the structure
  1616. * to pre-load conditions
  1617. **/
  1618. void fm10k_clear_queueing_scheme(struct fm10k_intfc *interface)
  1619. {
  1620. fm10k_free_q_vectors(interface);
  1621. fm10k_reset_msix_capability(interface);
  1622. }