fm10k_main.c 53 KB

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