fm10k_main.c 55 KB

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