fm10k_main.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  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. /* report start of ethernet header */
  583. if (nvgre_hdr->flags & NVGRE_TNI)
  584. return (struct ethhdr *)(nvgre_hdr + 1);
  585. return (struct ethhdr *)(&nvgre_hdr->tni);
  586. }
  587. __be16 fm10k_tx_encap_offload(struct sk_buff *skb)
  588. {
  589. u8 l4_hdr = 0, inner_l4_hdr = 0, inner_l4_hlen;
  590. struct ethhdr *eth_hdr;
  591. if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  592. skb->inner_protocol != htons(ETH_P_TEB))
  593. return 0;
  594. switch (vlan_get_protocol(skb)) {
  595. case htons(ETH_P_IP):
  596. l4_hdr = ip_hdr(skb)->protocol;
  597. break;
  598. case htons(ETH_P_IPV6):
  599. l4_hdr = ipv6_hdr(skb)->nexthdr;
  600. break;
  601. default:
  602. return 0;
  603. }
  604. switch (l4_hdr) {
  605. case IPPROTO_UDP:
  606. eth_hdr = fm10k_port_is_vxlan(skb);
  607. break;
  608. case IPPROTO_GRE:
  609. eth_hdr = fm10k_gre_is_nvgre(skb);
  610. break;
  611. default:
  612. return 0;
  613. }
  614. if (!eth_hdr)
  615. return 0;
  616. switch (eth_hdr->h_proto) {
  617. case htons(ETH_P_IP):
  618. inner_l4_hdr = inner_ip_hdr(skb)->protocol;
  619. break;
  620. case htons(ETH_P_IPV6):
  621. inner_l4_hdr = inner_ipv6_hdr(skb)->nexthdr;
  622. break;
  623. default:
  624. return 0;
  625. }
  626. switch (inner_l4_hdr) {
  627. case IPPROTO_TCP:
  628. inner_l4_hlen = inner_tcp_hdrlen(skb);
  629. break;
  630. case IPPROTO_UDP:
  631. inner_l4_hlen = 8;
  632. break;
  633. default:
  634. return 0;
  635. }
  636. /* The hardware allows tunnel offloads only if the combined inner and
  637. * outer header is 184 bytes or less
  638. */
  639. if (skb_inner_transport_header(skb) + inner_l4_hlen -
  640. skb_mac_header(skb) > FM10K_TUNNEL_HEADER_LENGTH)
  641. return 0;
  642. return eth_hdr->h_proto;
  643. }
  644. static int fm10k_tso(struct fm10k_ring *tx_ring,
  645. struct fm10k_tx_buffer *first)
  646. {
  647. struct sk_buff *skb = first->skb;
  648. struct fm10k_tx_desc *tx_desc;
  649. unsigned char *th;
  650. u8 hdrlen;
  651. if (skb->ip_summed != CHECKSUM_PARTIAL)
  652. return 0;
  653. if (!skb_is_gso(skb))
  654. return 0;
  655. /* compute header lengths */
  656. if (skb->encapsulation) {
  657. if (!fm10k_tx_encap_offload(skb))
  658. goto err_vxlan;
  659. th = skb_inner_transport_header(skb);
  660. } else {
  661. th = skb_transport_header(skb);
  662. }
  663. /* compute offset from SOF to transport header and add header len */
  664. hdrlen = (th - skb->data) + (((struct tcphdr *)th)->doff << 2);
  665. first->tx_flags |= FM10K_TX_FLAGS_CSUM;
  666. /* update gso size and bytecount with header size */
  667. first->gso_segs = skb_shinfo(skb)->gso_segs;
  668. first->bytecount += (first->gso_segs - 1) * hdrlen;
  669. /* populate Tx descriptor header size and mss */
  670. tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
  671. tx_desc->hdrlen = hdrlen;
  672. tx_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
  673. return 1;
  674. err_vxlan:
  675. tx_ring->netdev->features &= ~NETIF_F_GSO_UDP_TUNNEL;
  676. if (!net_ratelimit())
  677. netdev_err(tx_ring->netdev,
  678. "TSO requested for unsupported tunnel, disabling offload\n");
  679. return -1;
  680. }
  681. static void fm10k_tx_csum(struct fm10k_ring *tx_ring,
  682. struct fm10k_tx_buffer *first)
  683. {
  684. struct sk_buff *skb = first->skb;
  685. struct fm10k_tx_desc *tx_desc;
  686. union {
  687. struct iphdr *ipv4;
  688. struct ipv6hdr *ipv6;
  689. u8 *raw;
  690. } network_hdr;
  691. __be16 protocol;
  692. u8 l4_hdr = 0;
  693. if (skb->ip_summed != CHECKSUM_PARTIAL)
  694. goto no_csum;
  695. if (skb->encapsulation) {
  696. protocol = fm10k_tx_encap_offload(skb);
  697. if (!protocol) {
  698. if (skb_checksum_help(skb)) {
  699. dev_warn(tx_ring->dev,
  700. "failed to offload encap csum!\n");
  701. tx_ring->tx_stats.csum_err++;
  702. }
  703. goto no_csum;
  704. }
  705. network_hdr.raw = skb_inner_network_header(skb);
  706. } else {
  707. protocol = vlan_get_protocol(skb);
  708. network_hdr.raw = skb_network_header(skb);
  709. }
  710. switch (protocol) {
  711. case htons(ETH_P_IP):
  712. l4_hdr = network_hdr.ipv4->protocol;
  713. break;
  714. case htons(ETH_P_IPV6):
  715. l4_hdr = network_hdr.ipv6->nexthdr;
  716. break;
  717. default:
  718. if (unlikely(net_ratelimit())) {
  719. dev_warn(tx_ring->dev,
  720. "partial checksum but ip version=%x!\n",
  721. protocol);
  722. }
  723. tx_ring->tx_stats.csum_err++;
  724. goto no_csum;
  725. }
  726. switch (l4_hdr) {
  727. case IPPROTO_TCP:
  728. case IPPROTO_UDP:
  729. break;
  730. case IPPROTO_GRE:
  731. if (skb->encapsulation)
  732. break;
  733. default:
  734. if (unlikely(net_ratelimit())) {
  735. dev_warn(tx_ring->dev,
  736. "partial checksum but l4 proto=%x!\n",
  737. l4_hdr);
  738. }
  739. tx_ring->tx_stats.csum_err++;
  740. goto no_csum;
  741. }
  742. /* update TX checksum flag */
  743. first->tx_flags |= FM10K_TX_FLAGS_CSUM;
  744. no_csum:
  745. /* populate Tx descriptor header size and mss */
  746. tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
  747. tx_desc->hdrlen = 0;
  748. tx_desc->mss = 0;
  749. }
  750. #define FM10K_SET_FLAG(_input, _flag, _result) \
  751. ((_flag <= _result) ? \
  752. ((u32)(_input & _flag) * (_result / _flag)) : \
  753. ((u32)(_input & _flag) / (_flag / _result)))
  754. static u8 fm10k_tx_desc_flags(struct sk_buff *skb, u32 tx_flags)
  755. {
  756. /* set type for advanced descriptor with frame checksum insertion */
  757. u32 desc_flags = 0;
  758. /* set timestamping bits */
  759. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  760. likely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  761. desc_flags |= FM10K_TXD_FLAG_TIME;
  762. /* set checksum offload bits */
  763. desc_flags |= FM10K_SET_FLAG(tx_flags, FM10K_TX_FLAGS_CSUM,
  764. FM10K_TXD_FLAG_CSUM);
  765. return desc_flags;
  766. }
  767. static bool fm10k_tx_desc_push(struct fm10k_ring *tx_ring,
  768. struct fm10k_tx_desc *tx_desc, u16 i,
  769. dma_addr_t dma, unsigned int size, u8 desc_flags)
  770. {
  771. /* set RS and INT for last frame in a cache line */
  772. if ((++i & (FM10K_TXD_WB_FIFO_SIZE - 1)) == 0)
  773. desc_flags |= FM10K_TXD_FLAG_RS | FM10K_TXD_FLAG_INT;
  774. /* record values to descriptor */
  775. tx_desc->buffer_addr = cpu_to_le64(dma);
  776. tx_desc->flags = desc_flags;
  777. tx_desc->buflen = cpu_to_le16(size);
  778. /* return true if we just wrapped the ring */
  779. return i == tx_ring->count;
  780. }
  781. static int __fm10k_maybe_stop_tx(struct fm10k_ring *tx_ring, u16 size)
  782. {
  783. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  784. /* Memory barrier before checking head and tail */
  785. smp_mb();
  786. /* Check again in a case another CPU has just made room available */
  787. if (likely(fm10k_desc_unused(tx_ring) < size))
  788. return -EBUSY;
  789. /* A reprieve! - use start_queue because it doesn't call schedule */
  790. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  791. ++tx_ring->tx_stats.restart_queue;
  792. return 0;
  793. }
  794. static inline int fm10k_maybe_stop_tx(struct fm10k_ring *tx_ring, u16 size)
  795. {
  796. if (likely(fm10k_desc_unused(tx_ring) >= size))
  797. return 0;
  798. return __fm10k_maybe_stop_tx(tx_ring, size);
  799. }
  800. static void fm10k_tx_map(struct fm10k_ring *tx_ring,
  801. struct fm10k_tx_buffer *first)
  802. {
  803. struct sk_buff *skb = first->skb;
  804. struct fm10k_tx_buffer *tx_buffer;
  805. struct fm10k_tx_desc *tx_desc;
  806. struct skb_frag_struct *frag;
  807. unsigned char *data;
  808. dma_addr_t dma;
  809. unsigned int data_len, size;
  810. u32 tx_flags = first->tx_flags;
  811. u16 i = tx_ring->next_to_use;
  812. u8 flags = fm10k_tx_desc_flags(skb, tx_flags);
  813. tx_desc = FM10K_TX_DESC(tx_ring, i);
  814. /* add HW VLAN tag */
  815. if (skb_vlan_tag_present(skb))
  816. tx_desc->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
  817. else
  818. tx_desc->vlan = 0;
  819. size = skb_headlen(skb);
  820. data = skb->data;
  821. dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
  822. data_len = skb->data_len;
  823. tx_buffer = first;
  824. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  825. if (dma_mapping_error(tx_ring->dev, dma))
  826. goto dma_error;
  827. /* record length, and DMA address */
  828. dma_unmap_len_set(tx_buffer, len, size);
  829. dma_unmap_addr_set(tx_buffer, dma, dma);
  830. while (unlikely(size > FM10K_MAX_DATA_PER_TXD)) {
  831. if (fm10k_tx_desc_push(tx_ring, tx_desc++, i++, dma,
  832. FM10K_MAX_DATA_PER_TXD, flags)) {
  833. tx_desc = FM10K_TX_DESC(tx_ring, 0);
  834. i = 0;
  835. }
  836. dma += FM10K_MAX_DATA_PER_TXD;
  837. size -= FM10K_MAX_DATA_PER_TXD;
  838. }
  839. if (likely(!data_len))
  840. break;
  841. if (fm10k_tx_desc_push(tx_ring, tx_desc++, i++,
  842. dma, size, flags)) {
  843. tx_desc = FM10K_TX_DESC(tx_ring, 0);
  844. i = 0;
  845. }
  846. size = skb_frag_size(frag);
  847. data_len -= size;
  848. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  849. DMA_TO_DEVICE);
  850. tx_buffer = &tx_ring->tx_buffer[i];
  851. }
  852. /* write last descriptor with LAST bit set */
  853. flags |= FM10K_TXD_FLAG_LAST;
  854. if (fm10k_tx_desc_push(tx_ring, tx_desc, i++, dma, size, flags))
  855. i = 0;
  856. /* record bytecount for BQL */
  857. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  858. /* record SW timestamp if HW timestamp is not available */
  859. skb_tx_timestamp(first->skb);
  860. /* Force memory writes to complete before letting h/w know there
  861. * are new descriptors to fetch. (Only applicable for weak-ordered
  862. * memory model archs, such as IA-64).
  863. *
  864. * We also need this memory barrier to make certain all of the
  865. * status bits have been updated before next_to_watch is written.
  866. */
  867. wmb();
  868. /* set next_to_watch value indicating a packet is present */
  869. first->next_to_watch = tx_desc;
  870. tx_ring->next_to_use = i;
  871. /* Make sure there is space in the ring for the next send. */
  872. fm10k_maybe_stop_tx(tx_ring, DESC_NEEDED);
  873. /* notify HW of packet */
  874. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  875. writel(i, tx_ring->tail);
  876. /* we need this if more than one processor can write to our tail
  877. * at a time, it synchronizes IO on IA64/Altix systems
  878. */
  879. mmiowb();
  880. }
  881. return;
  882. dma_error:
  883. dev_err(tx_ring->dev, "TX DMA map failed\n");
  884. /* clear dma mappings for failed tx_buffer map */
  885. for (;;) {
  886. tx_buffer = &tx_ring->tx_buffer[i];
  887. fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer);
  888. if (tx_buffer == first)
  889. break;
  890. if (i == 0)
  891. i = tx_ring->count;
  892. i--;
  893. }
  894. tx_ring->next_to_use = i;
  895. }
  896. netdev_tx_t fm10k_xmit_frame_ring(struct sk_buff *skb,
  897. struct fm10k_ring *tx_ring)
  898. {
  899. struct fm10k_tx_buffer *first;
  900. int tso;
  901. u32 tx_flags = 0;
  902. #if PAGE_SIZE > FM10K_MAX_DATA_PER_TXD
  903. unsigned short f;
  904. #endif
  905. u16 count = TXD_USE_COUNT(skb_headlen(skb));
  906. /* need: 1 descriptor per page * PAGE_SIZE/FM10K_MAX_DATA_PER_TXD,
  907. * + 1 desc for skb_headlen/FM10K_MAX_DATA_PER_TXD,
  908. * + 2 desc gap to keep tail from touching head
  909. * otherwise try next time
  910. */
  911. #if PAGE_SIZE > FM10K_MAX_DATA_PER_TXD
  912. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  913. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  914. #else
  915. count += skb_shinfo(skb)->nr_frags;
  916. #endif
  917. if (fm10k_maybe_stop_tx(tx_ring, count + 3)) {
  918. tx_ring->tx_stats.tx_busy++;
  919. return NETDEV_TX_BUSY;
  920. }
  921. /* record the location of the first descriptor for this packet */
  922. first = &tx_ring->tx_buffer[tx_ring->next_to_use];
  923. first->skb = skb;
  924. first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN);
  925. first->gso_segs = 1;
  926. /* record initial flags and protocol */
  927. first->tx_flags = tx_flags;
  928. tso = fm10k_tso(tx_ring, first);
  929. if (tso < 0)
  930. goto out_drop;
  931. else if (!tso)
  932. fm10k_tx_csum(tx_ring, first);
  933. fm10k_tx_map(tx_ring, first);
  934. return NETDEV_TX_OK;
  935. out_drop:
  936. dev_kfree_skb_any(first->skb);
  937. first->skb = NULL;
  938. return NETDEV_TX_OK;
  939. }
  940. static u64 fm10k_get_tx_completed(struct fm10k_ring *ring)
  941. {
  942. return ring->stats.packets;
  943. }
  944. static u64 fm10k_get_tx_pending(struct fm10k_ring *ring)
  945. {
  946. /* use SW head and tail until we have real hardware */
  947. u32 head = ring->next_to_clean;
  948. u32 tail = ring->next_to_use;
  949. return ((head <= tail) ? tail : tail + ring->count) - head;
  950. }
  951. bool fm10k_check_tx_hang(struct fm10k_ring *tx_ring)
  952. {
  953. u32 tx_done = fm10k_get_tx_completed(tx_ring);
  954. u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
  955. u32 tx_pending = fm10k_get_tx_pending(tx_ring);
  956. clear_check_for_tx_hang(tx_ring);
  957. /* Check for a hung queue, but be thorough. This verifies
  958. * that a transmit has been completed since the previous
  959. * check AND there is at least one packet pending. By
  960. * requiring this to fail twice we avoid races with
  961. * clearing the ARMED bit and conditions where we
  962. * run the check_tx_hang logic with a transmit completion
  963. * pending but without time to complete it yet.
  964. */
  965. if (!tx_pending || (tx_done_old != tx_done)) {
  966. /* update completed stats and continue */
  967. tx_ring->tx_stats.tx_done_old = tx_done;
  968. /* reset the countdown */
  969. clear_bit(__FM10K_HANG_CHECK_ARMED, &tx_ring->state);
  970. return false;
  971. }
  972. /* make sure it is true for two checks in a row */
  973. return test_and_set_bit(__FM10K_HANG_CHECK_ARMED, &tx_ring->state);
  974. }
  975. /**
  976. * fm10k_tx_timeout_reset - initiate reset due to Tx timeout
  977. * @interface: driver private struct
  978. **/
  979. void fm10k_tx_timeout_reset(struct fm10k_intfc *interface)
  980. {
  981. /* Do the reset outside of interrupt context */
  982. if (!test_bit(__FM10K_DOWN, &interface->state)) {
  983. netdev_err(interface->netdev, "Reset interface\n");
  984. interface->tx_timeout_count++;
  985. interface->flags |= FM10K_FLAG_RESET_REQUESTED;
  986. fm10k_service_event_schedule(interface);
  987. }
  988. }
  989. /**
  990. * fm10k_clean_tx_irq - Reclaim resources after transmit completes
  991. * @q_vector: structure containing interrupt and ring information
  992. * @tx_ring: tx ring to clean
  993. **/
  994. static bool fm10k_clean_tx_irq(struct fm10k_q_vector *q_vector,
  995. struct fm10k_ring *tx_ring)
  996. {
  997. struct fm10k_intfc *interface = q_vector->interface;
  998. struct fm10k_tx_buffer *tx_buffer;
  999. struct fm10k_tx_desc *tx_desc;
  1000. unsigned int total_bytes = 0, total_packets = 0;
  1001. unsigned int budget = q_vector->tx.work_limit;
  1002. unsigned int i = tx_ring->next_to_clean;
  1003. if (test_bit(__FM10K_DOWN, &interface->state))
  1004. return true;
  1005. tx_buffer = &tx_ring->tx_buffer[i];
  1006. tx_desc = FM10K_TX_DESC(tx_ring, i);
  1007. i -= tx_ring->count;
  1008. do {
  1009. struct fm10k_tx_desc *eop_desc = tx_buffer->next_to_watch;
  1010. /* if next_to_watch is not set then there is no work pending */
  1011. if (!eop_desc)
  1012. break;
  1013. /* prevent any other reads prior to eop_desc */
  1014. read_barrier_depends();
  1015. /* if DD is not set pending work has not been completed */
  1016. if (!(eop_desc->flags & FM10K_TXD_FLAG_DONE))
  1017. break;
  1018. /* clear next_to_watch to prevent false hangs */
  1019. tx_buffer->next_to_watch = NULL;
  1020. /* update the statistics for this packet */
  1021. total_bytes += tx_buffer->bytecount;
  1022. total_packets += tx_buffer->gso_segs;
  1023. /* free the skb */
  1024. dev_consume_skb_any(tx_buffer->skb);
  1025. /* unmap skb header data */
  1026. dma_unmap_single(tx_ring->dev,
  1027. dma_unmap_addr(tx_buffer, dma),
  1028. dma_unmap_len(tx_buffer, len),
  1029. DMA_TO_DEVICE);
  1030. /* clear tx_buffer data */
  1031. tx_buffer->skb = NULL;
  1032. dma_unmap_len_set(tx_buffer, len, 0);
  1033. /* unmap remaining buffers */
  1034. while (tx_desc != eop_desc) {
  1035. tx_buffer++;
  1036. tx_desc++;
  1037. i++;
  1038. if (unlikely(!i)) {
  1039. i -= tx_ring->count;
  1040. tx_buffer = tx_ring->tx_buffer;
  1041. tx_desc = FM10K_TX_DESC(tx_ring, 0);
  1042. }
  1043. /* unmap any remaining paged data */
  1044. if (dma_unmap_len(tx_buffer, len)) {
  1045. dma_unmap_page(tx_ring->dev,
  1046. dma_unmap_addr(tx_buffer, dma),
  1047. dma_unmap_len(tx_buffer, len),
  1048. DMA_TO_DEVICE);
  1049. dma_unmap_len_set(tx_buffer, len, 0);
  1050. }
  1051. }
  1052. /* move us one more past the eop_desc for start of next pkt */
  1053. tx_buffer++;
  1054. tx_desc++;
  1055. i++;
  1056. if (unlikely(!i)) {
  1057. i -= tx_ring->count;
  1058. tx_buffer = tx_ring->tx_buffer;
  1059. tx_desc = FM10K_TX_DESC(tx_ring, 0);
  1060. }
  1061. /* issue prefetch for next Tx descriptor */
  1062. prefetch(tx_desc);
  1063. /* update budget accounting */
  1064. budget--;
  1065. } while (likely(budget));
  1066. i += tx_ring->count;
  1067. tx_ring->next_to_clean = i;
  1068. u64_stats_update_begin(&tx_ring->syncp);
  1069. tx_ring->stats.bytes += total_bytes;
  1070. tx_ring->stats.packets += total_packets;
  1071. u64_stats_update_end(&tx_ring->syncp);
  1072. q_vector->tx.total_bytes += total_bytes;
  1073. q_vector->tx.total_packets += total_packets;
  1074. if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring)) {
  1075. /* schedule immediate reset if we believe we hung */
  1076. struct fm10k_hw *hw = &interface->hw;
  1077. netif_err(interface, drv, tx_ring->netdev,
  1078. "Detected Tx Unit Hang\n"
  1079. " Tx Queue <%d>\n"
  1080. " TDH, TDT <%x>, <%x>\n"
  1081. " next_to_use <%x>\n"
  1082. " next_to_clean <%x>\n",
  1083. tx_ring->queue_index,
  1084. fm10k_read_reg(hw, FM10K_TDH(tx_ring->reg_idx)),
  1085. fm10k_read_reg(hw, FM10K_TDT(tx_ring->reg_idx)),
  1086. tx_ring->next_to_use, i);
  1087. netif_stop_subqueue(tx_ring->netdev,
  1088. tx_ring->queue_index);
  1089. netif_info(interface, probe, tx_ring->netdev,
  1090. "tx hang %d detected on queue %d, resetting interface\n",
  1091. interface->tx_timeout_count + 1,
  1092. tx_ring->queue_index);
  1093. fm10k_tx_timeout_reset(interface);
  1094. /* the netdev is about to reset, no point in enabling stuff */
  1095. return true;
  1096. }
  1097. /* notify netdev of completed buffers */
  1098. netdev_tx_completed_queue(txring_txq(tx_ring),
  1099. total_packets, total_bytes);
  1100. #define TX_WAKE_THRESHOLD min_t(u16, FM10K_MIN_TXD - 1, DESC_NEEDED * 2)
  1101. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  1102. (fm10k_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
  1103. /* Make sure that anybody stopping the queue after this
  1104. * sees the new next_to_clean.
  1105. */
  1106. smp_mb();
  1107. if (__netif_subqueue_stopped(tx_ring->netdev,
  1108. tx_ring->queue_index) &&
  1109. !test_bit(__FM10K_DOWN, &interface->state)) {
  1110. netif_wake_subqueue(tx_ring->netdev,
  1111. tx_ring->queue_index);
  1112. ++tx_ring->tx_stats.restart_queue;
  1113. }
  1114. }
  1115. return !!budget;
  1116. }
  1117. /**
  1118. * fm10k_update_itr - update the dynamic ITR value based on packet size
  1119. *
  1120. * Stores a new ITR value based on strictly on packet size. The
  1121. * divisors and thresholds used by this function were determined based
  1122. * on theoretical maximum wire speed and testing data, in order to
  1123. * minimize response time while increasing bulk throughput.
  1124. *
  1125. * @ring_container: Container for rings to have ITR updated
  1126. **/
  1127. static void fm10k_update_itr(struct fm10k_ring_container *ring_container)
  1128. {
  1129. unsigned int avg_wire_size, packets;
  1130. /* Only update ITR if we are using adaptive setting */
  1131. if (!(ring_container->itr & FM10K_ITR_ADAPTIVE))
  1132. goto clear_counts;
  1133. packets = ring_container->total_packets;
  1134. if (!packets)
  1135. goto clear_counts;
  1136. avg_wire_size = ring_container->total_bytes / packets;
  1137. /* Add 24 bytes to size to account for CRC, preamble, and gap */
  1138. avg_wire_size += 24;
  1139. /* Don't starve jumbo frames */
  1140. if (avg_wire_size > 3000)
  1141. avg_wire_size = 3000;
  1142. /* Give a little boost to mid-size frames */
  1143. if ((avg_wire_size > 300) && (avg_wire_size < 1200))
  1144. avg_wire_size /= 3;
  1145. else
  1146. avg_wire_size /= 2;
  1147. /* write back value and retain adaptive flag */
  1148. ring_container->itr = avg_wire_size | FM10K_ITR_ADAPTIVE;
  1149. clear_counts:
  1150. ring_container->total_bytes = 0;
  1151. ring_container->total_packets = 0;
  1152. }
  1153. static void fm10k_qv_enable(struct fm10k_q_vector *q_vector)
  1154. {
  1155. /* Enable auto-mask and clear the current mask */
  1156. u32 itr = FM10K_ITR_ENABLE;
  1157. /* Update Tx ITR */
  1158. fm10k_update_itr(&q_vector->tx);
  1159. /* Update Rx ITR */
  1160. fm10k_update_itr(&q_vector->rx);
  1161. /* Store Tx itr in timer slot 0 */
  1162. itr |= (q_vector->tx.itr & FM10K_ITR_MAX);
  1163. /* Shift Rx itr to timer slot 1 */
  1164. itr |= (q_vector->rx.itr & FM10K_ITR_MAX) << FM10K_ITR_INTERVAL1_SHIFT;
  1165. /* Write the final value to the ITR register */
  1166. writel(itr, q_vector->itr);
  1167. }
  1168. static int fm10k_poll(struct napi_struct *napi, int budget)
  1169. {
  1170. struct fm10k_q_vector *q_vector =
  1171. container_of(napi, struct fm10k_q_vector, napi);
  1172. struct fm10k_ring *ring;
  1173. int per_ring_budget;
  1174. bool clean_complete = true;
  1175. fm10k_for_each_ring(ring, q_vector->tx)
  1176. clean_complete &= fm10k_clean_tx_irq(q_vector, ring);
  1177. /* attempt to distribute budget to each queue fairly, but don't
  1178. * allow the budget to go below 1 because we'll exit polling
  1179. */
  1180. if (q_vector->rx.count > 1)
  1181. per_ring_budget = max(budget/q_vector->rx.count, 1);
  1182. else
  1183. per_ring_budget = budget;
  1184. fm10k_for_each_ring(ring, q_vector->rx)
  1185. clean_complete &= fm10k_clean_rx_irq(q_vector, ring,
  1186. per_ring_budget);
  1187. /* If all work not completed, return budget and keep polling */
  1188. if (!clean_complete)
  1189. return budget;
  1190. /* all work done, exit the polling mode */
  1191. napi_complete(napi);
  1192. /* re-enable the q_vector */
  1193. fm10k_qv_enable(q_vector);
  1194. return 0;
  1195. }
  1196. /**
  1197. * fm10k_set_qos_queues: Allocate queues for a QOS-enabled device
  1198. * @interface: board private structure to initialize
  1199. *
  1200. * When QoS (Quality of Service) is enabled, allocate queues for
  1201. * each traffic class. If multiqueue isn't available,then abort QoS
  1202. * initialization.
  1203. *
  1204. * This function handles all combinations of Qos and RSS.
  1205. *
  1206. **/
  1207. static bool fm10k_set_qos_queues(struct fm10k_intfc *interface)
  1208. {
  1209. struct net_device *dev = interface->netdev;
  1210. struct fm10k_ring_feature *f;
  1211. int rss_i, i;
  1212. int pcs;
  1213. /* Map queue offset and counts onto allocated tx queues */
  1214. pcs = netdev_get_num_tc(dev);
  1215. if (pcs <= 1)
  1216. return false;
  1217. /* set QoS mask and indices */
  1218. f = &interface->ring_feature[RING_F_QOS];
  1219. f->indices = pcs;
  1220. f->mask = (1 << fls(pcs - 1)) - 1;
  1221. /* determine the upper limit for our current DCB mode */
  1222. rss_i = interface->hw.mac.max_queues / pcs;
  1223. rss_i = 1 << (fls(rss_i) - 1);
  1224. /* set RSS mask and indices */
  1225. f = &interface->ring_feature[RING_F_RSS];
  1226. rss_i = min_t(u16, rss_i, f->limit);
  1227. f->indices = rss_i;
  1228. f->mask = (1 << fls(rss_i - 1)) - 1;
  1229. /* configure pause class to queue mapping */
  1230. for (i = 0; i < pcs; i++)
  1231. netdev_set_tc_queue(dev, i, rss_i, rss_i * i);
  1232. interface->num_rx_queues = rss_i * pcs;
  1233. interface->num_tx_queues = rss_i * pcs;
  1234. return true;
  1235. }
  1236. /**
  1237. * fm10k_set_rss_queues: Allocate queues for RSS
  1238. * @interface: board private structure to initialize
  1239. *
  1240. * This is our "base" multiqueue mode. RSS (Receive Side Scaling) will try
  1241. * to allocate one Rx queue per CPU, and if available, one Tx queue per CPU.
  1242. *
  1243. **/
  1244. static bool fm10k_set_rss_queues(struct fm10k_intfc *interface)
  1245. {
  1246. struct fm10k_ring_feature *f;
  1247. u16 rss_i;
  1248. f = &interface->ring_feature[RING_F_RSS];
  1249. rss_i = min_t(u16, interface->hw.mac.max_queues, f->limit);
  1250. /* record indices and power of 2 mask for RSS */
  1251. f->indices = rss_i;
  1252. f->mask = (1 << fls(rss_i - 1)) - 1;
  1253. interface->num_rx_queues = rss_i;
  1254. interface->num_tx_queues = rss_i;
  1255. return true;
  1256. }
  1257. /**
  1258. * fm10k_set_num_queues: Allocate queues for device, feature dependent
  1259. * @interface: board private structure to initialize
  1260. *
  1261. * This is the top level queue allocation routine. The order here is very
  1262. * important, starting with the "most" number of features turned on at once,
  1263. * and ending with the smallest set of features. This way large combinations
  1264. * can be allocated if they're turned on, and smaller combinations are the
  1265. * fallthrough conditions.
  1266. *
  1267. **/
  1268. static void fm10k_set_num_queues(struct fm10k_intfc *interface)
  1269. {
  1270. /* Start with base case */
  1271. interface->num_rx_queues = 1;
  1272. interface->num_tx_queues = 1;
  1273. if (fm10k_set_qos_queues(interface))
  1274. return;
  1275. fm10k_set_rss_queues(interface);
  1276. }
  1277. /**
  1278. * fm10k_alloc_q_vector - Allocate memory for a single interrupt vector
  1279. * @interface: board private structure to initialize
  1280. * @v_count: q_vectors allocated on interface, used for ring interleaving
  1281. * @v_idx: index of vector in interface struct
  1282. * @txr_count: total number of Tx rings to allocate
  1283. * @txr_idx: index of first Tx ring to allocate
  1284. * @rxr_count: total number of Rx rings to allocate
  1285. * @rxr_idx: index of first Rx ring to allocate
  1286. *
  1287. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  1288. **/
  1289. static int fm10k_alloc_q_vector(struct fm10k_intfc *interface,
  1290. unsigned int v_count, unsigned int v_idx,
  1291. unsigned int txr_count, unsigned int txr_idx,
  1292. unsigned int rxr_count, unsigned int rxr_idx)
  1293. {
  1294. struct fm10k_q_vector *q_vector;
  1295. struct fm10k_ring *ring;
  1296. int ring_count, size;
  1297. ring_count = txr_count + rxr_count;
  1298. size = sizeof(struct fm10k_q_vector) +
  1299. (sizeof(struct fm10k_ring) * ring_count);
  1300. /* allocate q_vector and rings */
  1301. q_vector = kzalloc(size, GFP_KERNEL);
  1302. if (!q_vector)
  1303. return -ENOMEM;
  1304. /* initialize NAPI */
  1305. netif_napi_add(interface->netdev, &q_vector->napi,
  1306. fm10k_poll, NAPI_POLL_WEIGHT);
  1307. /* tie q_vector and interface together */
  1308. interface->q_vector[v_idx] = q_vector;
  1309. q_vector->interface = interface;
  1310. q_vector->v_idx = v_idx;
  1311. /* initialize pointer to rings */
  1312. ring = q_vector->ring;
  1313. /* save Tx ring container info */
  1314. q_vector->tx.ring = ring;
  1315. q_vector->tx.work_limit = FM10K_DEFAULT_TX_WORK;
  1316. q_vector->tx.itr = interface->tx_itr;
  1317. q_vector->tx.count = txr_count;
  1318. while (txr_count) {
  1319. /* assign generic ring traits */
  1320. ring->dev = &interface->pdev->dev;
  1321. ring->netdev = interface->netdev;
  1322. /* configure backlink on ring */
  1323. ring->q_vector = q_vector;
  1324. /* apply Tx specific ring traits */
  1325. ring->count = interface->tx_ring_count;
  1326. ring->queue_index = txr_idx;
  1327. /* assign ring to interface */
  1328. interface->tx_ring[txr_idx] = ring;
  1329. /* update count and index */
  1330. txr_count--;
  1331. txr_idx += v_count;
  1332. /* push pointer to next ring */
  1333. ring++;
  1334. }
  1335. /* save Rx ring container info */
  1336. q_vector->rx.ring = ring;
  1337. q_vector->rx.itr = interface->rx_itr;
  1338. q_vector->rx.count = rxr_count;
  1339. while (rxr_count) {
  1340. /* assign generic ring traits */
  1341. ring->dev = &interface->pdev->dev;
  1342. ring->netdev = interface->netdev;
  1343. rcu_assign_pointer(ring->l2_accel, interface->l2_accel);
  1344. /* configure backlink on ring */
  1345. ring->q_vector = q_vector;
  1346. /* apply Rx specific ring traits */
  1347. ring->count = interface->rx_ring_count;
  1348. ring->queue_index = rxr_idx;
  1349. /* assign ring to interface */
  1350. interface->rx_ring[rxr_idx] = ring;
  1351. /* update count and index */
  1352. rxr_count--;
  1353. rxr_idx += v_count;
  1354. /* push pointer to next ring */
  1355. ring++;
  1356. }
  1357. fm10k_dbg_q_vector_init(q_vector);
  1358. return 0;
  1359. }
  1360. /**
  1361. * fm10k_free_q_vector - Free memory allocated for specific interrupt vector
  1362. * @interface: board private structure to initialize
  1363. * @v_idx: Index of vector to be freed
  1364. *
  1365. * This function frees the memory allocated to the q_vector. In addition if
  1366. * NAPI is enabled it will delete any references to the NAPI struct prior
  1367. * to freeing the q_vector.
  1368. **/
  1369. static void fm10k_free_q_vector(struct fm10k_intfc *interface, int v_idx)
  1370. {
  1371. struct fm10k_q_vector *q_vector = interface->q_vector[v_idx];
  1372. struct fm10k_ring *ring;
  1373. fm10k_dbg_q_vector_exit(q_vector);
  1374. fm10k_for_each_ring(ring, q_vector->tx)
  1375. interface->tx_ring[ring->queue_index] = NULL;
  1376. fm10k_for_each_ring(ring, q_vector->rx)
  1377. interface->rx_ring[ring->queue_index] = NULL;
  1378. interface->q_vector[v_idx] = NULL;
  1379. netif_napi_del(&q_vector->napi);
  1380. kfree_rcu(q_vector, rcu);
  1381. }
  1382. /**
  1383. * fm10k_alloc_q_vectors - Allocate memory for interrupt vectors
  1384. * @interface: board private structure to initialize
  1385. *
  1386. * We allocate one q_vector per queue interrupt. If allocation fails we
  1387. * return -ENOMEM.
  1388. **/
  1389. static int fm10k_alloc_q_vectors(struct fm10k_intfc *interface)
  1390. {
  1391. unsigned int q_vectors = interface->num_q_vectors;
  1392. unsigned int rxr_remaining = interface->num_rx_queues;
  1393. unsigned int txr_remaining = interface->num_tx_queues;
  1394. unsigned int rxr_idx = 0, txr_idx = 0, v_idx = 0;
  1395. int err;
  1396. if (q_vectors >= (rxr_remaining + txr_remaining)) {
  1397. for (; rxr_remaining; v_idx++) {
  1398. err = fm10k_alloc_q_vector(interface, q_vectors, v_idx,
  1399. 0, 0, 1, rxr_idx);
  1400. if (err)
  1401. goto err_out;
  1402. /* update counts and index */
  1403. rxr_remaining--;
  1404. rxr_idx++;
  1405. }
  1406. }
  1407. for (; v_idx < q_vectors; v_idx++) {
  1408. int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
  1409. int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
  1410. err = fm10k_alloc_q_vector(interface, q_vectors, v_idx,
  1411. tqpv, txr_idx,
  1412. rqpv, rxr_idx);
  1413. if (err)
  1414. goto err_out;
  1415. /* update counts and index */
  1416. rxr_remaining -= rqpv;
  1417. txr_remaining -= tqpv;
  1418. rxr_idx++;
  1419. txr_idx++;
  1420. }
  1421. return 0;
  1422. err_out:
  1423. interface->num_tx_queues = 0;
  1424. interface->num_rx_queues = 0;
  1425. interface->num_q_vectors = 0;
  1426. while (v_idx--)
  1427. fm10k_free_q_vector(interface, v_idx);
  1428. return -ENOMEM;
  1429. }
  1430. /**
  1431. * fm10k_free_q_vectors - Free memory allocated for interrupt vectors
  1432. * @interface: board private structure to initialize
  1433. *
  1434. * This function frees the memory allocated to the q_vectors. In addition if
  1435. * NAPI is enabled it will delete any references to the NAPI struct prior
  1436. * to freeing the q_vector.
  1437. **/
  1438. static void fm10k_free_q_vectors(struct fm10k_intfc *interface)
  1439. {
  1440. int v_idx = interface->num_q_vectors;
  1441. interface->num_tx_queues = 0;
  1442. interface->num_rx_queues = 0;
  1443. interface->num_q_vectors = 0;
  1444. while (v_idx--)
  1445. fm10k_free_q_vector(interface, v_idx);
  1446. }
  1447. /**
  1448. * f10k_reset_msix_capability - reset MSI-X capability
  1449. * @interface: board private structure to initialize
  1450. *
  1451. * Reset the MSI-X capability back to its starting state
  1452. **/
  1453. static void fm10k_reset_msix_capability(struct fm10k_intfc *interface)
  1454. {
  1455. pci_disable_msix(interface->pdev);
  1456. kfree(interface->msix_entries);
  1457. interface->msix_entries = NULL;
  1458. }
  1459. /**
  1460. * f10k_init_msix_capability - configure MSI-X capability
  1461. * @interface: board private structure to initialize
  1462. *
  1463. * Attempt to configure the interrupts using the best available
  1464. * capabilities of the hardware and the kernel.
  1465. **/
  1466. static int fm10k_init_msix_capability(struct fm10k_intfc *interface)
  1467. {
  1468. struct fm10k_hw *hw = &interface->hw;
  1469. int v_budget, vector;
  1470. /* It's easy to be greedy for MSI-X vectors, but it really
  1471. * doesn't do us much good if we have a lot more vectors
  1472. * than CPU's. So let's be conservative and only ask for
  1473. * (roughly) the same number of vectors as there are CPU's.
  1474. * the default is to use pairs of vectors
  1475. */
  1476. v_budget = max(interface->num_rx_queues, interface->num_tx_queues);
  1477. v_budget = min_t(u16, v_budget, num_online_cpus());
  1478. /* account for vectors not related to queues */
  1479. v_budget += NON_Q_VECTORS(hw);
  1480. /* At the same time, hardware can only support a maximum of
  1481. * hw.mac->max_msix_vectors vectors. With features
  1482. * such as RSS and VMDq, we can easily surpass the number of Rx and Tx
  1483. * descriptor queues supported by our device. Thus, we cap it off in
  1484. * those rare cases where the cpu count also exceeds our vector limit.
  1485. */
  1486. v_budget = min_t(int, v_budget, hw->mac.max_msix_vectors);
  1487. /* A failure in MSI-X entry allocation is fatal. */
  1488. interface->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  1489. GFP_KERNEL);
  1490. if (!interface->msix_entries)
  1491. return -ENOMEM;
  1492. /* populate entry values */
  1493. for (vector = 0; vector < v_budget; vector++)
  1494. interface->msix_entries[vector].entry = vector;
  1495. /* Attempt to enable MSI-X with requested value */
  1496. v_budget = pci_enable_msix_range(interface->pdev,
  1497. interface->msix_entries,
  1498. MIN_MSIX_COUNT(hw),
  1499. v_budget);
  1500. if (v_budget < 0) {
  1501. kfree(interface->msix_entries);
  1502. interface->msix_entries = NULL;
  1503. return -ENOMEM;
  1504. }
  1505. /* record the number of queues available for q_vectors */
  1506. interface->num_q_vectors = v_budget - NON_Q_VECTORS(hw);
  1507. return 0;
  1508. }
  1509. /**
  1510. * fm10k_cache_ring_qos - Descriptor ring to register mapping for QoS
  1511. * @interface: Interface structure continaining rings and devices
  1512. *
  1513. * Cache the descriptor ring offsets for Qos
  1514. **/
  1515. static bool fm10k_cache_ring_qos(struct fm10k_intfc *interface)
  1516. {
  1517. struct net_device *dev = interface->netdev;
  1518. int pc, offset, rss_i, i, q_idx;
  1519. u16 pc_stride = interface->ring_feature[RING_F_QOS].mask + 1;
  1520. u8 num_pcs = netdev_get_num_tc(dev);
  1521. if (num_pcs <= 1)
  1522. return false;
  1523. rss_i = interface->ring_feature[RING_F_RSS].indices;
  1524. for (pc = 0, offset = 0; pc < num_pcs; pc++, offset += rss_i) {
  1525. q_idx = pc;
  1526. for (i = 0; i < rss_i; i++) {
  1527. interface->tx_ring[offset + i]->reg_idx = q_idx;
  1528. interface->tx_ring[offset + i]->qos_pc = pc;
  1529. interface->rx_ring[offset + i]->reg_idx = q_idx;
  1530. interface->rx_ring[offset + i]->qos_pc = pc;
  1531. q_idx += pc_stride;
  1532. }
  1533. }
  1534. return true;
  1535. }
  1536. /**
  1537. * fm10k_cache_ring_rss - Descriptor ring to register mapping for RSS
  1538. * @interface: Interface structure continaining rings and devices
  1539. *
  1540. * Cache the descriptor ring offsets for RSS
  1541. **/
  1542. static void fm10k_cache_ring_rss(struct fm10k_intfc *interface)
  1543. {
  1544. int i;
  1545. for (i = 0; i < interface->num_rx_queues; i++)
  1546. interface->rx_ring[i]->reg_idx = i;
  1547. for (i = 0; i < interface->num_tx_queues; i++)
  1548. interface->tx_ring[i]->reg_idx = i;
  1549. }
  1550. /**
  1551. * fm10k_assign_rings - Map rings to network devices
  1552. * @interface: Interface structure containing rings and devices
  1553. *
  1554. * This function is meant to go though and configure both the network
  1555. * devices so that they contain rings, and configure the rings so that
  1556. * they function with their network devices.
  1557. **/
  1558. static void fm10k_assign_rings(struct fm10k_intfc *interface)
  1559. {
  1560. if (fm10k_cache_ring_qos(interface))
  1561. return;
  1562. fm10k_cache_ring_rss(interface);
  1563. }
  1564. static void fm10k_init_reta(struct fm10k_intfc *interface)
  1565. {
  1566. u16 i, rss_i = interface->ring_feature[RING_F_RSS].indices;
  1567. u32 reta, base;
  1568. /* If the netdev is initialized we have to maintain table if possible */
  1569. if (interface->netdev->reg_state) {
  1570. for (i = FM10K_RETA_SIZE; i--;) {
  1571. reta = interface->reta[i];
  1572. if ((((reta << 24) >> 24) < rss_i) &&
  1573. (((reta << 16) >> 24) < rss_i) &&
  1574. (((reta << 8) >> 24) < rss_i) &&
  1575. (((reta) >> 24) < rss_i))
  1576. continue;
  1577. goto repopulate_reta;
  1578. }
  1579. /* do nothing if all of the elements are in bounds */
  1580. return;
  1581. }
  1582. repopulate_reta:
  1583. /* Populate the redirection table 4 entries at a time. To do this
  1584. * we are generating the results for n and n+2 and then interleaving
  1585. * those with the results with n+1 and n+3.
  1586. */
  1587. for (i = FM10K_RETA_SIZE; i--;) {
  1588. /* first pass generates n and n+2 */
  1589. base = ((i * 0x00040004) + 0x00020000) * rss_i;
  1590. reta = (base & 0x3F803F80) >> 7;
  1591. /* second pass generates n+1 and n+3 */
  1592. base += 0x00010001 * rss_i;
  1593. reta |= (base & 0x3F803F80) << 1;
  1594. interface->reta[i] = reta;
  1595. }
  1596. }
  1597. /**
  1598. * fm10k_init_queueing_scheme - Determine proper queueing scheme
  1599. * @interface: board private structure to initialize
  1600. *
  1601. * We determine which queueing scheme to use based on...
  1602. * - Hardware queue count (num_*_queues)
  1603. * - defined by miscellaneous hardware support/features (RSS, etc.)
  1604. **/
  1605. int fm10k_init_queueing_scheme(struct fm10k_intfc *interface)
  1606. {
  1607. int err;
  1608. /* Number of supported queues */
  1609. fm10k_set_num_queues(interface);
  1610. /* Configure MSI-X capability */
  1611. err = fm10k_init_msix_capability(interface);
  1612. if (err) {
  1613. dev_err(&interface->pdev->dev,
  1614. "Unable to initialize MSI-X capability\n");
  1615. return err;
  1616. }
  1617. /* Allocate memory for queues */
  1618. err = fm10k_alloc_q_vectors(interface);
  1619. if (err)
  1620. return err;
  1621. /* Map rings to devices, and map devices to physical queues */
  1622. fm10k_assign_rings(interface);
  1623. /* Initialize RSS redirection table */
  1624. fm10k_init_reta(interface);
  1625. return 0;
  1626. }
  1627. /**
  1628. * fm10k_clear_queueing_scheme - Clear the current queueing scheme settings
  1629. * @interface: board private structure to clear queueing scheme on
  1630. *
  1631. * We go through and clear queueing specific resources and reset the structure
  1632. * to pre-load conditions
  1633. **/
  1634. void fm10k_clear_queueing_scheme(struct fm10k_intfc *interface)
  1635. {
  1636. fm10k_free_q_vectors(interface);
  1637. fm10k_reset_msix_capability(interface);
  1638. }