fm10k_main.c 54 KB

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