fm10k_main.c 55 KB

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