fm10k_main.c 54 KB

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