fm10k_main.c 54 KB

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