fm10k_main.c 54 KB

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