fm10k_main.c 53 KB

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