fm10k_main.c 53 KB

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