fm10k_main.c 52 KB

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