fm10k_main.c 53 KB

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