fm10k_netdev.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /* Intel(R) Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2016 Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * The full GNU General Public License is included in this distribution in
  14. * the file called "COPYING".
  15. *
  16. * Contact Information:
  17. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. */
  20. #include "fm10k.h"
  21. #include <linux/vmalloc.h>
  22. #include <net/udp_tunnel.h>
  23. /**
  24. * fm10k_setup_tx_resources - allocate Tx resources (Descriptors)
  25. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  26. *
  27. * Return 0 on success, negative on failure
  28. **/
  29. int fm10k_setup_tx_resources(struct fm10k_ring *tx_ring)
  30. {
  31. struct device *dev = tx_ring->dev;
  32. int size;
  33. size = sizeof(struct fm10k_tx_buffer) * tx_ring->count;
  34. tx_ring->tx_buffer = vzalloc(size);
  35. if (!tx_ring->tx_buffer)
  36. goto err;
  37. u64_stats_init(&tx_ring->syncp);
  38. /* round up to nearest 4K */
  39. tx_ring->size = tx_ring->count * sizeof(struct fm10k_tx_desc);
  40. tx_ring->size = ALIGN(tx_ring->size, 4096);
  41. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  42. &tx_ring->dma, GFP_KERNEL);
  43. if (!tx_ring->desc)
  44. goto err;
  45. return 0;
  46. err:
  47. vfree(tx_ring->tx_buffer);
  48. tx_ring->tx_buffer = NULL;
  49. return -ENOMEM;
  50. }
  51. /**
  52. * fm10k_setup_all_tx_resources - allocate all queues Tx resources
  53. * @interface: board private structure
  54. *
  55. * If this function returns with an error, then it's possible one or
  56. * more of the rings is populated (while the rest are not). It is the
  57. * callers duty to clean those orphaned rings.
  58. *
  59. * Return 0 on success, negative on failure
  60. **/
  61. static int fm10k_setup_all_tx_resources(struct fm10k_intfc *interface)
  62. {
  63. int i, err = 0;
  64. for (i = 0; i < interface->num_tx_queues; i++) {
  65. err = fm10k_setup_tx_resources(interface->tx_ring[i]);
  66. if (!err)
  67. continue;
  68. netif_err(interface, probe, interface->netdev,
  69. "Allocation for Tx Queue %u failed\n", i);
  70. goto err_setup_tx;
  71. }
  72. return 0;
  73. err_setup_tx:
  74. /* rewind the index freeing the rings as we go */
  75. while (i--)
  76. fm10k_free_tx_resources(interface->tx_ring[i]);
  77. return err;
  78. }
  79. /**
  80. * fm10k_setup_rx_resources - allocate Rx resources (Descriptors)
  81. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  82. *
  83. * Returns 0 on success, negative on failure
  84. **/
  85. int fm10k_setup_rx_resources(struct fm10k_ring *rx_ring)
  86. {
  87. struct device *dev = rx_ring->dev;
  88. int size;
  89. size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
  90. rx_ring->rx_buffer = vzalloc(size);
  91. if (!rx_ring->rx_buffer)
  92. goto err;
  93. u64_stats_init(&rx_ring->syncp);
  94. /* Round up to nearest 4K */
  95. rx_ring->size = rx_ring->count * sizeof(union fm10k_rx_desc);
  96. rx_ring->size = ALIGN(rx_ring->size, 4096);
  97. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  98. &rx_ring->dma, GFP_KERNEL);
  99. if (!rx_ring->desc)
  100. goto err;
  101. return 0;
  102. err:
  103. vfree(rx_ring->rx_buffer);
  104. rx_ring->rx_buffer = NULL;
  105. return -ENOMEM;
  106. }
  107. /**
  108. * fm10k_setup_all_rx_resources - allocate all queues Rx resources
  109. * @interface: board private structure
  110. *
  111. * If this function returns with an error, then it's possible one or
  112. * more of the rings is populated (while the rest are not). It is the
  113. * callers duty to clean those orphaned rings.
  114. *
  115. * Return 0 on success, negative on failure
  116. **/
  117. static int fm10k_setup_all_rx_resources(struct fm10k_intfc *interface)
  118. {
  119. int i, err = 0;
  120. for (i = 0; i < interface->num_rx_queues; i++) {
  121. err = fm10k_setup_rx_resources(interface->rx_ring[i]);
  122. if (!err)
  123. continue;
  124. netif_err(interface, probe, interface->netdev,
  125. "Allocation for Rx Queue %u failed\n", i);
  126. goto err_setup_rx;
  127. }
  128. return 0;
  129. err_setup_rx:
  130. /* rewind the index freeing the rings as we go */
  131. while (i--)
  132. fm10k_free_rx_resources(interface->rx_ring[i]);
  133. return err;
  134. }
  135. void fm10k_unmap_and_free_tx_resource(struct fm10k_ring *ring,
  136. struct fm10k_tx_buffer *tx_buffer)
  137. {
  138. if (tx_buffer->skb) {
  139. dev_kfree_skb_any(tx_buffer->skb);
  140. if (dma_unmap_len(tx_buffer, len))
  141. dma_unmap_single(ring->dev,
  142. dma_unmap_addr(tx_buffer, dma),
  143. dma_unmap_len(tx_buffer, len),
  144. DMA_TO_DEVICE);
  145. } else if (dma_unmap_len(tx_buffer, len)) {
  146. dma_unmap_page(ring->dev,
  147. dma_unmap_addr(tx_buffer, dma),
  148. dma_unmap_len(tx_buffer, len),
  149. DMA_TO_DEVICE);
  150. }
  151. tx_buffer->next_to_watch = NULL;
  152. tx_buffer->skb = NULL;
  153. dma_unmap_len_set(tx_buffer, len, 0);
  154. /* tx_buffer must be completely set up in the transmit path */
  155. }
  156. /**
  157. * fm10k_clean_tx_ring - Free Tx Buffers
  158. * @tx_ring: ring to be cleaned
  159. **/
  160. static void fm10k_clean_tx_ring(struct fm10k_ring *tx_ring)
  161. {
  162. struct fm10k_tx_buffer *tx_buffer;
  163. unsigned long size;
  164. u16 i;
  165. /* ring already cleared, nothing to do */
  166. if (!tx_ring->tx_buffer)
  167. return;
  168. /* Free all the Tx ring sk_buffs */
  169. for (i = 0; i < tx_ring->count; i++) {
  170. tx_buffer = &tx_ring->tx_buffer[i];
  171. fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer);
  172. }
  173. /* reset BQL values */
  174. netdev_tx_reset_queue(txring_txq(tx_ring));
  175. size = sizeof(struct fm10k_tx_buffer) * tx_ring->count;
  176. memset(tx_ring->tx_buffer, 0, size);
  177. /* Zero out the descriptor ring */
  178. memset(tx_ring->desc, 0, tx_ring->size);
  179. }
  180. /**
  181. * fm10k_free_tx_resources - Free Tx Resources per Queue
  182. * @tx_ring: Tx descriptor ring for a specific queue
  183. *
  184. * Free all transmit software resources
  185. **/
  186. void fm10k_free_tx_resources(struct fm10k_ring *tx_ring)
  187. {
  188. fm10k_clean_tx_ring(tx_ring);
  189. vfree(tx_ring->tx_buffer);
  190. tx_ring->tx_buffer = NULL;
  191. /* if not set, then don't free */
  192. if (!tx_ring->desc)
  193. return;
  194. dma_free_coherent(tx_ring->dev, tx_ring->size,
  195. tx_ring->desc, tx_ring->dma);
  196. tx_ring->desc = NULL;
  197. }
  198. /**
  199. * fm10k_clean_all_tx_rings - Free Tx Buffers for all queues
  200. * @interface: board private structure
  201. **/
  202. void fm10k_clean_all_tx_rings(struct fm10k_intfc *interface)
  203. {
  204. int i;
  205. for (i = 0; i < interface->num_tx_queues; i++)
  206. fm10k_clean_tx_ring(interface->tx_ring[i]);
  207. }
  208. /**
  209. * fm10k_free_all_tx_resources - Free Tx Resources for All Queues
  210. * @interface: board private structure
  211. *
  212. * Free all transmit software resources
  213. **/
  214. static void fm10k_free_all_tx_resources(struct fm10k_intfc *interface)
  215. {
  216. int i = interface->num_tx_queues;
  217. while (i--)
  218. fm10k_free_tx_resources(interface->tx_ring[i]);
  219. }
  220. /**
  221. * fm10k_clean_rx_ring - Free Rx Buffers per Queue
  222. * @rx_ring: ring to free buffers from
  223. **/
  224. static void fm10k_clean_rx_ring(struct fm10k_ring *rx_ring)
  225. {
  226. unsigned long size;
  227. u16 i;
  228. if (!rx_ring->rx_buffer)
  229. return;
  230. if (rx_ring->skb)
  231. dev_kfree_skb(rx_ring->skb);
  232. rx_ring->skb = NULL;
  233. /* Free all the Rx ring sk_buffs */
  234. for (i = 0; i < rx_ring->count; i++) {
  235. struct fm10k_rx_buffer *buffer = &rx_ring->rx_buffer[i];
  236. /* clean-up will only set page pointer to NULL */
  237. if (!buffer->page)
  238. continue;
  239. dma_unmap_page(rx_ring->dev, buffer->dma,
  240. PAGE_SIZE, DMA_FROM_DEVICE);
  241. __free_page(buffer->page);
  242. buffer->page = NULL;
  243. }
  244. size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
  245. memset(rx_ring->rx_buffer, 0, size);
  246. /* Zero out the descriptor ring */
  247. memset(rx_ring->desc, 0, rx_ring->size);
  248. rx_ring->next_to_alloc = 0;
  249. rx_ring->next_to_clean = 0;
  250. rx_ring->next_to_use = 0;
  251. }
  252. /**
  253. * fm10k_free_rx_resources - Free Rx Resources
  254. * @rx_ring: ring to clean the resources from
  255. *
  256. * Free all receive software resources
  257. **/
  258. void fm10k_free_rx_resources(struct fm10k_ring *rx_ring)
  259. {
  260. fm10k_clean_rx_ring(rx_ring);
  261. vfree(rx_ring->rx_buffer);
  262. rx_ring->rx_buffer = NULL;
  263. /* if not set, then don't free */
  264. if (!rx_ring->desc)
  265. return;
  266. dma_free_coherent(rx_ring->dev, rx_ring->size,
  267. rx_ring->desc, rx_ring->dma);
  268. rx_ring->desc = NULL;
  269. }
  270. /**
  271. * fm10k_clean_all_rx_rings - Free Rx Buffers for all queues
  272. * @interface: board private structure
  273. **/
  274. void fm10k_clean_all_rx_rings(struct fm10k_intfc *interface)
  275. {
  276. int i;
  277. for (i = 0; i < interface->num_rx_queues; i++)
  278. fm10k_clean_rx_ring(interface->rx_ring[i]);
  279. }
  280. /**
  281. * fm10k_free_all_rx_resources - Free Rx Resources for All Queues
  282. * @interface: board private structure
  283. *
  284. * Free all receive software resources
  285. **/
  286. static void fm10k_free_all_rx_resources(struct fm10k_intfc *interface)
  287. {
  288. int i = interface->num_rx_queues;
  289. while (i--)
  290. fm10k_free_rx_resources(interface->rx_ring[i]);
  291. }
  292. /**
  293. * fm10k_request_glort_range - Request GLORTs for use in configuring rules
  294. * @interface: board private structure
  295. *
  296. * This function allocates a range of glorts for this interface to use.
  297. **/
  298. static void fm10k_request_glort_range(struct fm10k_intfc *interface)
  299. {
  300. struct fm10k_hw *hw = &interface->hw;
  301. u16 mask = (~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT;
  302. /* establish GLORT base */
  303. interface->glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
  304. interface->glort_count = 0;
  305. /* nothing we can do until mask is allocated */
  306. if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
  307. return;
  308. /* we support 3 possible GLORT configurations.
  309. * 1: VFs consume all but the last 1
  310. * 2: VFs and PF split glorts with possible gap between
  311. * 3: VFs allocated first 64, all others belong to PF
  312. */
  313. if (mask <= hw->iov.total_vfs) {
  314. interface->glort_count = 1;
  315. interface->glort += mask;
  316. } else if (mask < 64) {
  317. interface->glort_count = (mask + 1) / 2;
  318. interface->glort += interface->glort_count;
  319. } else {
  320. interface->glort_count = mask - 63;
  321. interface->glort += 64;
  322. }
  323. }
  324. /**
  325. * fm10k_free_udp_port_info
  326. * @interface: board private structure
  327. *
  328. * This function frees both geneve_port and vxlan_port structures
  329. **/
  330. static void fm10k_free_udp_port_info(struct fm10k_intfc *interface)
  331. {
  332. struct fm10k_udp_port *port;
  333. /* flush all entries from vxlan list */
  334. port = list_first_entry_or_null(&interface->vxlan_port,
  335. struct fm10k_udp_port, list);
  336. while (port) {
  337. list_del(&port->list);
  338. kfree(port);
  339. port = list_first_entry_or_null(&interface->vxlan_port,
  340. struct fm10k_udp_port,
  341. list);
  342. }
  343. /* flush all entries from geneve list */
  344. port = list_first_entry_or_null(&interface->geneve_port,
  345. struct fm10k_udp_port, list);
  346. while (port) {
  347. list_del(&port->list);
  348. kfree(port);
  349. port = list_first_entry_or_null(&interface->vxlan_port,
  350. struct fm10k_udp_port,
  351. list);
  352. }
  353. }
  354. /**
  355. * fm10k_restore_udp_port_info
  356. * @interface: board private structure
  357. *
  358. * This function restores the value in the tunnel_cfg register(s) after reset
  359. **/
  360. static void fm10k_restore_udp_port_info(struct fm10k_intfc *interface)
  361. {
  362. struct fm10k_hw *hw = &interface->hw;
  363. struct fm10k_udp_port *port;
  364. /* only the PF supports configuring tunnels */
  365. if (hw->mac.type != fm10k_mac_pf)
  366. return;
  367. port = list_first_entry_or_null(&interface->vxlan_port,
  368. struct fm10k_udp_port, list);
  369. /* restore tunnel configuration register */
  370. fm10k_write_reg(hw, FM10K_TUNNEL_CFG,
  371. (port ? ntohs(port->port) : 0) |
  372. (ETH_P_TEB << FM10K_TUNNEL_CFG_NVGRE_SHIFT));
  373. port = list_first_entry_or_null(&interface->geneve_port,
  374. struct fm10k_udp_port, list);
  375. /* restore Geneve tunnel configuration register */
  376. fm10k_write_reg(hw, FM10K_TUNNEL_CFG_GENEVE,
  377. (port ? ntohs(port->port) : 0));
  378. }
  379. static struct fm10k_udp_port *
  380. fm10k_remove_tunnel_port(struct list_head *ports,
  381. struct udp_tunnel_info *ti)
  382. {
  383. struct fm10k_udp_port *port;
  384. list_for_each_entry(port, ports, list) {
  385. if ((port->port == ti->port) &&
  386. (port->sa_family == ti->sa_family)) {
  387. list_del(&port->list);
  388. return port;
  389. }
  390. }
  391. return NULL;
  392. }
  393. static void fm10k_insert_tunnel_port(struct list_head *ports,
  394. struct udp_tunnel_info *ti)
  395. {
  396. struct fm10k_udp_port *port;
  397. /* remove existing port entry from the list so that the newest items
  398. * are always at the tail of the list.
  399. */
  400. port = fm10k_remove_tunnel_port(ports, ti);
  401. if (!port) {
  402. port = kmalloc(sizeof(*port), GFP_ATOMIC);
  403. if (!port)
  404. return;
  405. port->port = ti->port;
  406. port->sa_family = ti->sa_family;
  407. }
  408. list_add_tail(&port->list, ports);
  409. }
  410. /**
  411. * fm10k_udp_tunnel_add
  412. * @netdev: network interface device structure
  413. * @ti: Tunnel endpoint information
  414. *
  415. * This function is called when a new UDP tunnel port has been added.
  416. * Due to hardware restrictions, only one port per type can be offloaded at
  417. * once.
  418. **/
  419. static void fm10k_udp_tunnel_add(struct net_device *dev,
  420. struct udp_tunnel_info *ti)
  421. {
  422. struct fm10k_intfc *interface = netdev_priv(dev);
  423. /* only the PF supports configuring tunnels */
  424. if (interface->hw.mac.type != fm10k_mac_pf)
  425. return;
  426. switch (ti->type) {
  427. case UDP_TUNNEL_TYPE_VXLAN:
  428. fm10k_insert_tunnel_port(&interface->vxlan_port, ti);
  429. break;
  430. case UDP_TUNNEL_TYPE_GENEVE:
  431. fm10k_insert_tunnel_port(&interface->geneve_port, ti);
  432. break;
  433. default:
  434. return;
  435. }
  436. fm10k_restore_udp_port_info(interface);
  437. }
  438. /**
  439. * fm10k_udp_tunnel_del
  440. * @netdev: network interface device structure
  441. * @ti: Tunnel endpoint information
  442. *
  443. * This function is called when a new UDP tunnel port is deleted. The freed
  444. * port will be removed from the list, then we reprogram the offloaded port
  445. * based on the head of the list.
  446. **/
  447. static void fm10k_udp_tunnel_del(struct net_device *dev,
  448. struct udp_tunnel_info *ti)
  449. {
  450. struct fm10k_intfc *interface = netdev_priv(dev);
  451. struct fm10k_udp_port *port = NULL;
  452. if (interface->hw.mac.type != fm10k_mac_pf)
  453. return;
  454. switch (ti->type) {
  455. case UDP_TUNNEL_TYPE_VXLAN:
  456. port = fm10k_remove_tunnel_port(&interface->vxlan_port, ti);
  457. break;
  458. case UDP_TUNNEL_TYPE_GENEVE:
  459. port = fm10k_remove_tunnel_port(&interface->geneve_port, ti);
  460. break;
  461. default:
  462. return;
  463. }
  464. /* if we did remove a port we need to free its memory */
  465. kfree(port);
  466. fm10k_restore_udp_port_info(interface);
  467. }
  468. /**
  469. * fm10k_open - Called when a network interface is made active
  470. * @netdev: network interface device structure
  471. *
  472. * Returns 0 on success, negative value on failure
  473. *
  474. * The open entry point is called when a network interface is made
  475. * active by the system (IFF_UP). At this point all resources needed
  476. * for transmit and receive operations are allocated, the interrupt
  477. * handler is registered with the OS, the watchdog timer is started,
  478. * and the stack is notified that the interface is ready.
  479. **/
  480. int fm10k_open(struct net_device *netdev)
  481. {
  482. struct fm10k_intfc *interface = netdev_priv(netdev);
  483. int err;
  484. /* allocate transmit descriptors */
  485. err = fm10k_setup_all_tx_resources(interface);
  486. if (err)
  487. goto err_setup_tx;
  488. /* allocate receive descriptors */
  489. err = fm10k_setup_all_rx_resources(interface);
  490. if (err)
  491. goto err_setup_rx;
  492. /* allocate interrupt resources */
  493. err = fm10k_qv_request_irq(interface);
  494. if (err)
  495. goto err_req_irq;
  496. /* setup GLORT assignment for this port */
  497. fm10k_request_glort_range(interface);
  498. /* Notify the stack of the actual queue counts */
  499. err = netif_set_real_num_tx_queues(netdev,
  500. interface->num_tx_queues);
  501. if (err)
  502. goto err_set_queues;
  503. err = netif_set_real_num_rx_queues(netdev,
  504. interface->num_rx_queues);
  505. if (err)
  506. goto err_set_queues;
  507. udp_tunnel_get_rx_info(netdev);
  508. fm10k_up(interface);
  509. return 0;
  510. err_set_queues:
  511. fm10k_qv_free_irq(interface);
  512. err_req_irq:
  513. fm10k_free_all_rx_resources(interface);
  514. err_setup_rx:
  515. fm10k_free_all_tx_resources(interface);
  516. err_setup_tx:
  517. return err;
  518. }
  519. /**
  520. * fm10k_close - Disables a network interface
  521. * @netdev: network interface device structure
  522. *
  523. * Returns 0, this is not allowed to fail
  524. *
  525. * The close entry point is called when an interface is de-activated
  526. * by the OS. The hardware is still under the drivers control, but
  527. * needs to be disabled. A global MAC reset is issued to stop the
  528. * hardware, and all transmit and receive resources are freed.
  529. **/
  530. int fm10k_close(struct net_device *netdev)
  531. {
  532. struct fm10k_intfc *interface = netdev_priv(netdev);
  533. fm10k_down(interface);
  534. fm10k_qv_free_irq(interface);
  535. fm10k_free_udp_port_info(interface);
  536. fm10k_free_all_tx_resources(interface);
  537. fm10k_free_all_rx_resources(interface);
  538. return 0;
  539. }
  540. static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev)
  541. {
  542. struct fm10k_intfc *interface = netdev_priv(dev);
  543. unsigned int r_idx = skb->queue_mapping;
  544. int err;
  545. if ((skb->protocol == htons(ETH_P_8021Q)) &&
  546. !skb_vlan_tag_present(skb)) {
  547. /* FM10K only supports hardware tagging, any tags in frame
  548. * are considered 2nd level or "outer" tags
  549. */
  550. struct vlan_hdr *vhdr;
  551. __be16 proto;
  552. /* make sure skb is not shared */
  553. skb = skb_share_check(skb, GFP_ATOMIC);
  554. if (!skb)
  555. return NETDEV_TX_OK;
  556. /* make sure there is enough room to move the ethernet header */
  557. if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
  558. return NETDEV_TX_OK;
  559. /* verify the skb head is not shared */
  560. err = skb_cow_head(skb, 0);
  561. if (err) {
  562. dev_kfree_skb(skb);
  563. return NETDEV_TX_OK;
  564. }
  565. /* locate VLAN header */
  566. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  567. /* pull the 2 key pieces of data out of it */
  568. __vlan_hwaccel_put_tag(skb,
  569. htons(ETH_P_8021Q),
  570. ntohs(vhdr->h_vlan_TCI));
  571. proto = vhdr->h_vlan_encapsulated_proto;
  572. skb->protocol = (ntohs(proto) >= 1536) ? proto :
  573. htons(ETH_P_802_2);
  574. /* squash it by moving the ethernet addresses up 4 bytes */
  575. memmove(skb->data + VLAN_HLEN, skb->data, 12);
  576. __skb_pull(skb, VLAN_HLEN);
  577. skb_reset_mac_header(skb);
  578. }
  579. /* The minimum packet size for a single buffer is 17B so pad the skb
  580. * in order to meet this minimum size requirement.
  581. */
  582. if (unlikely(skb->len < 17)) {
  583. int pad_len = 17 - skb->len;
  584. if (skb_pad(skb, pad_len))
  585. return NETDEV_TX_OK;
  586. __skb_put(skb, pad_len);
  587. }
  588. if (r_idx >= interface->num_tx_queues)
  589. r_idx %= interface->num_tx_queues;
  590. err = fm10k_xmit_frame_ring(skb, interface->tx_ring[r_idx]);
  591. return err;
  592. }
  593. static int fm10k_change_mtu(struct net_device *dev, int new_mtu)
  594. {
  595. if (new_mtu < 68 || new_mtu > FM10K_MAX_JUMBO_FRAME_SIZE)
  596. return -EINVAL;
  597. dev->mtu = new_mtu;
  598. return 0;
  599. }
  600. /**
  601. * fm10k_tx_timeout - Respond to a Tx Hang
  602. * @netdev: network interface device structure
  603. **/
  604. static void fm10k_tx_timeout(struct net_device *netdev)
  605. {
  606. struct fm10k_intfc *interface = netdev_priv(netdev);
  607. bool real_tx_hang = false;
  608. int i;
  609. #define TX_TIMEO_LIMIT 16000
  610. for (i = 0; i < interface->num_tx_queues; i++) {
  611. struct fm10k_ring *tx_ring = interface->tx_ring[i];
  612. if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring))
  613. real_tx_hang = true;
  614. }
  615. if (real_tx_hang) {
  616. fm10k_tx_timeout_reset(interface);
  617. } else {
  618. netif_info(interface, drv, netdev,
  619. "Fake Tx hang detected with timeout of %d seconds\n",
  620. netdev->watchdog_timeo / HZ);
  621. /* fake Tx hang - increase the kernel timeout */
  622. if (netdev->watchdog_timeo < TX_TIMEO_LIMIT)
  623. netdev->watchdog_timeo *= 2;
  624. }
  625. }
  626. static int fm10k_uc_vlan_unsync(struct net_device *netdev,
  627. const unsigned char *uc_addr)
  628. {
  629. struct fm10k_intfc *interface = netdev_priv(netdev);
  630. struct fm10k_hw *hw = &interface->hw;
  631. u16 glort = interface->glort;
  632. u16 vid = interface->vid;
  633. bool set = !!(vid / VLAN_N_VID);
  634. int err;
  635. /* drop any leading bits on the VLAN ID */
  636. vid &= VLAN_N_VID - 1;
  637. err = hw->mac.ops.update_uc_addr(hw, glort, uc_addr, vid, set, 0);
  638. if (err)
  639. return err;
  640. /* return non-zero value as we are only doing a partial sync/unsync */
  641. return 1;
  642. }
  643. static int fm10k_mc_vlan_unsync(struct net_device *netdev,
  644. const unsigned char *mc_addr)
  645. {
  646. struct fm10k_intfc *interface = netdev_priv(netdev);
  647. struct fm10k_hw *hw = &interface->hw;
  648. u16 glort = interface->glort;
  649. u16 vid = interface->vid;
  650. bool set = !!(vid / VLAN_N_VID);
  651. int err;
  652. /* drop any leading bits on the VLAN ID */
  653. vid &= VLAN_N_VID - 1;
  654. err = hw->mac.ops.update_mc_addr(hw, glort, mc_addr, vid, set);
  655. if (err)
  656. return err;
  657. /* return non-zero value as we are only doing a partial sync/unsync */
  658. return 1;
  659. }
  660. static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
  661. {
  662. struct fm10k_intfc *interface = netdev_priv(netdev);
  663. struct fm10k_hw *hw = &interface->hw;
  664. s32 err;
  665. int i;
  666. /* updates do not apply to VLAN 0 */
  667. if (!vid)
  668. return 0;
  669. if (vid >= VLAN_N_VID)
  670. return -EINVAL;
  671. /* Verify we have permission to add VLANs */
  672. if (hw->mac.vlan_override)
  673. return -EACCES;
  674. /* update active_vlans bitmask */
  675. set_bit(vid, interface->active_vlans);
  676. if (!set)
  677. clear_bit(vid, interface->active_vlans);
  678. /* disable the default VLAN ID on ring if we have an active VLAN */
  679. for (i = 0; i < interface->num_rx_queues; i++) {
  680. struct fm10k_ring *rx_ring = interface->rx_ring[i];
  681. u16 rx_vid = rx_ring->vid & (VLAN_N_VID - 1);
  682. if (test_bit(rx_vid, interface->active_vlans))
  683. rx_ring->vid |= FM10K_VLAN_CLEAR;
  684. else
  685. rx_ring->vid &= ~FM10K_VLAN_CLEAR;
  686. }
  687. /* Do not remove default VLAN ID related entries from VLAN and MAC
  688. * tables
  689. */
  690. if (!set && vid == hw->mac.default_vid)
  691. return 0;
  692. /* Do not throw an error if the interface is down. We will sync once
  693. * we come up
  694. */
  695. if (test_bit(__FM10K_DOWN, &interface->state))
  696. return 0;
  697. fm10k_mbx_lock(interface);
  698. /* only need to update the VLAN if not in promiscuous mode */
  699. if (!(netdev->flags & IFF_PROMISC)) {
  700. err = hw->mac.ops.update_vlan(hw, vid, 0, set);
  701. if (err)
  702. goto err_out;
  703. }
  704. /* update our base MAC address */
  705. err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr,
  706. vid, set, 0);
  707. if (err)
  708. goto err_out;
  709. /* set VLAN ID prior to syncing/unsyncing the VLAN */
  710. interface->vid = vid + (set ? VLAN_N_VID : 0);
  711. /* Update the unicast and multicast address list to add/drop VLAN */
  712. __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
  713. __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
  714. err_out:
  715. fm10k_mbx_unlock(interface);
  716. return err;
  717. }
  718. static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
  719. __always_unused __be16 proto, u16 vid)
  720. {
  721. /* update VLAN and address table based on changes */
  722. return fm10k_update_vid(netdev, vid, true);
  723. }
  724. static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
  725. __always_unused __be16 proto, u16 vid)
  726. {
  727. /* update VLAN and address table based on changes */
  728. return fm10k_update_vid(netdev, vid, false);
  729. }
  730. static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
  731. {
  732. struct fm10k_hw *hw = &interface->hw;
  733. u16 default_vid = hw->mac.default_vid;
  734. u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
  735. vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
  736. return vid;
  737. }
  738. static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
  739. {
  740. struct fm10k_hw *hw = &interface->hw;
  741. u32 vid, prev_vid;
  742. /* loop through and find any gaps in the table */
  743. for (vid = 0, prev_vid = 0;
  744. prev_vid < VLAN_N_VID;
  745. prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
  746. if (prev_vid == vid)
  747. continue;
  748. /* send request to clear multiple bits at a time */
  749. prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
  750. hw->mac.ops.update_vlan(hw, prev_vid, 0, false);
  751. }
  752. }
  753. static int __fm10k_uc_sync(struct net_device *dev,
  754. const unsigned char *addr, bool sync)
  755. {
  756. struct fm10k_intfc *interface = netdev_priv(dev);
  757. struct fm10k_hw *hw = &interface->hw;
  758. u16 vid, glort = interface->glort;
  759. s32 err;
  760. if (!is_valid_ether_addr(addr))
  761. return -EADDRNOTAVAIL;
  762. /* update table with current entries */
  763. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
  764. vid < VLAN_N_VID;
  765. vid = fm10k_find_next_vlan(interface, vid)) {
  766. err = hw->mac.ops.update_uc_addr(hw, glort, addr,
  767. vid, sync, 0);
  768. if (err)
  769. return err;
  770. }
  771. return 0;
  772. }
  773. static int fm10k_uc_sync(struct net_device *dev,
  774. const unsigned char *addr)
  775. {
  776. return __fm10k_uc_sync(dev, addr, true);
  777. }
  778. static int fm10k_uc_unsync(struct net_device *dev,
  779. const unsigned char *addr)
  780. {
  781. return __fm10k_uc_sync(dev, addr, false);
  782. }
  783. static int fm10k_set_mac(struct net_device *dev, void *p)
  784. {
  785. struct fm10k_intfc *interface = netdev_priv(dev);
  786. struct fm10k_hw *hw = &interface->hw;
  787. struct sockaddr *addr = p;
  788. s32 err = 0;
  789. if (!is_valid_ether_addr(addr->sa_data))
  790. return -EADDRNOTAVAIL;
  791. if (dev->flags & IFF_UP) {
  792. /* setting MAC address requires mailbox */
  793. fm10k_mbx_lock(interface);
  794. err = fm10k_uc_sync(dev, addr->sa_data);
  795. if (!err)
  796. fm10k_uc_unsync(dev, hw->mac.addr);
  797. fm10k_mbx_unlock(interface);
  798. }
  799. if (!err) {
  800. ether_addr_copy(dev->dev_addr, addr->sa_data);
  801. ether_addr_copy(hw->mac.addr, addr->sa_data);
  802. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  803. }
  804. /* if we had a mailbox error suggest trying again */
  805. return err ? -EAGAIN : 0;
  806. }
  807. static int __fm10k_mc_sync(struct net_device *dev,
  808. const unsigned char *addr, bool sync)
  809. {
  810. struct fm10k_intfc *interface = netdev_priv(dev);
  811. struct fm10k_hw *hw = &interface->hw;
  812. u16 vid, glort = interface->glort;
  813. /* update table with current entries */
  814. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
  815. vid < VLAN_N_VID;
  816. vid = fm10k_find_next_vlan(interface, vid)) {
  817. hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync);
  818. }
  819. return 0;
  820. }
  821. static int fm10k_mc_sync(struct net_device *dev,
  822. const unsigned char *addr)
  823. {
  824. return __fm10k_mc_sync(dev, addr, true);
  825. }
  826. static int fm10k_mc_unsync(struct net_device *dev,
  827. const unsigned char *addr)
  828. {
  829. return __fm10k_mc_sync(dev, addr, false);
  830. }
  831. static void fm10k_set_rx_mode(struct net_device *dev)
  832. {
  833. struct fm10k_intfc *interface = netdev_priv(dev);
  834. struct fm10k_hw *hw = &interface->hw;
  835. int xcast_mode;
  836. /* no need to update the harwdare if we are not running */
  837. if (!(dev->flags & IFF_UP))
  838. return;
  839. /* determine new mode based on flags */
  840. xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
  841. (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
  842. (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
  843. FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
  844. fm10k_mbx_lock(interface);
  845. /* update xcast mode first, but only if it changed */
  846. if (interface->xcast_mode != xcast_mode) {
  847. /* update VLAN table */
  848. if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
  849. hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true);
  850. if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
  851. fm10k_clear_unused_vlans(interface);
  852. /* update xcast mode */
  853. hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode);
  854. /* record updated xcast mode state */
  855. interface->xcast_mode = xcast_mode;
  856. }
  857. /* synchronize all of the addresses */
  858. __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
  859. __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
  860. fm10k_mbx_unlock(interface);
  861. }
  862. void fm10k_restore_rx_state(struct fm10k_intfc *interface)
  863. {
  864. struct net_device *netdev = interface->netdev;
  865. struct fm10k_hw *hw = &interface->hw;
  866. int xcast_mode;
  867. u16 vid, glort;
  868. /* record glort for this interface */
  869. glort = interface->glort;
  870. /* convert interface flags to xcast mode */
  871. if (netdev->flags & IFF_PROMISC)
  872. xcast_mode = FM10K_XCAST_MODE_PROMISC;
  873. else if (netdev->flags & IFF_ALLMULTI)
  874. xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
  875. else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
  876. xcast_mode = FM10K_XCAST_MODE_MULTI;
  877. else
  878. xcast_mode = FM10K_XCAST_MODE_NONE;
  879. fm10k_mbx_lock(interface);
  880. /* Enable logical port */
  881. hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true);
  882. /* update VLAN table */
  883. hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0,
  884. xcast_mode == FM10K_XCAST_MODE_PROMISC);
  885. /* Add filter for VLAN 0 */
  886. hw->mac.ops.update_vlan(hw, 0, 0, true);
  887. /* update table with current entries */
  888. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
  889. vid < VLAN_N_VID;
  890. vid = fm10k_find_next_vlan(interface, vid)) {
  891. hw->mac.ops.update_vlan(hw, vid, 0, true);
  892. hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr,
  893. vid, true, 0);
  894. }
  895. /* update xcast mode before synchronizing addresses */
  896. hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
  897. /* synchronize all of the addresses */
  898. __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
  899. __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
  900. fm10k_mbx_unlock(interface);
  901. /* record updated xcast mode state */
  902. interface->xcast_mode = xcast_mode;
  903. /* Restore tunnel configuration */
  904. fm10k_restore_udp_port_info(interface);
  905. }
  906. void fm10k_reset_rx_state(struct fm10k_intfc *interface)
  907. {
  908. struct net_device *netdev = interface->netdev;
  909. struct fm10k_hw *hw = &interface->hw;
  910. fm10k_mbx_lock(interface);
  911. /* clear the logical port state on lower device */
  912. hw->mac.ops.update_lport_state(hw, interface->glort,
  913. interface->glort_count, false);
  914. fm10k_mbx_unlock(interface);
  915. /* reset flags to default state */
  916. interface->xcast_mode = FM10K_XCAST_MODE_NONE;
  917. /* clear the sync flag since the lport has been dropped */
  918. __dev_uc_unsync(netdev, NULL);
  919. __dev_mc_unsync(netdev, NULL);
  920. }
  921. /**
  922. * fm10k_get_stats64 - Get System Network Statistics
  923. * @netdev: network interface device structure
  924. * @stats: storage space for 64bit statistics
  925. *
  926. * Returns 64bit statistics, for use in the ndo_get_stats64 callback. This
  927. * function replaces fm10k_get_stats for kernels which support it.
  928. */
  929. static struct rtnl_link_stats64 *fm10k_get_stats64(struct net_device *netdev,
  930. struct rtnl_link_stats64 *stats)
  931. {
  932. struct fm10k_intfc *interface = netdev_priv(netdev);
  933. struct fm10k_ring *ring;
  934. unsigned int start, i;
  935. u64 bytes, packets;
  936. rcu_read_lock();
  937. for (i = 0; i < interface->num_rx_queues; i++) {
  938. ring = READ_ONCE(interface->rx_ring[i]);
  939. if (!ring)
  940. continue;
  941. do {
  942. start = u64_stats_fetch_begin_irq(&ring->syncp);
  943. packets = ring->stats.packets;
  944. bytes = ring->stats.bytes;
  945. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  946. stats->rx_packets += packets;
  947. stats->rx_bytes += bytes;
  948. }
  949. for (i = 0; i < interface->num_tx_queues; i++) {
  950. ring = READ_ONCE(interface->tx_ring[i]);
  951. if (!ring)
  952. continue;
  953. do {
  954. start = u64_stats_fetch_begin_irq(&ring->syncp);
  955. packets = ring->stats.packets;
  956. bytes = ring->stats.bytes;
  957. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  958. stats->tx_packets += packets;
  959. stats->tx_bytes += bytes;
  960. }
  961. rcu_read_unlock();
  962. /* following stats updated by fm10k_service_task() */
  963. stats->rx_missed_errors = netdev->stats.rx_missed_errors;
  964. return stats;
  965. }
  966. int fm10k_setup_tc(struct net_device *dev, u8 tc)
  967. {
  968. struct fm10k_intfc *interface = netdev_priv(dev);
  969. int err;
  970. /* Currently only the PF supports priority classes */
  971. if (tc && (interface->hw.mac.type != fm10k_mac_pf))
  972. return -EINVAL;
  973. /* Hardware supports up to 8 traffic classes */
  974. if (tc > 8)
  975. return -EINVAL;
  976. /* Hardware has to reinitialize queues to match packet
  977. * buffer alignment. Unfortunately, the hardware is not
  978. * flexible enough to do this dynamically.
  979. */
  980. if (netif_running(dev))
  981. fm10k_close(dev);
  982. fm10k_mbx_free_irq(interface);
  983. fm10k_clear_queueing_scheme(interface);
  984. /* we expect the prio_tc map to be repopulated later */
  985. netdev_reset_tc(dev);
  986. netdev_set_num_tc(dev, tc);
  987. err = fm10k_init_queueing_scheme(interface);
  988. if (err)
  989. goto err_queueing_scheme;
  990. err = fm10k_mbx_request_irq(interface);
  991. if (err)
  992. goto err_mbx_irq;
  993. err = netif_running(dev) ? fm10k_open(dev) : 0;
  994. if (err)
  995. goto err_open;
  996. /* flag to indicate SWPRI has yet to be updated */
  997. interface->flags |= FM10K_FLAG_SWPRI_CONFIG;
  998. return 0;
  999. err_open:
  1000. fm10k_mbx_free_irq(interface);
  1001. err_mbx_irq:
  1002. fm10k_clear_queueing_scheme(interface);
  1003. err_queueing_scheme:
  1004. netif_device_detach(dev);
  1005. return err;
  1006. }
  1007. static int __fm10k_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
  1008. struct tc_to_netdev *tc)
  1009. {
  1010. if (tc->type != TC_SETUP_MQPRIO)
  1011. return -EINVAL;
  1012. return fm10k_setup_tc(dev, tc->tc);
  1013. }
  1014. static void fm10k_assign_l2_accel(struct fm10k_intfc *interface,
  1015. struct fm10k_l2_accel *l2_accel)
  1016. {
  1017. struct fm10k_ring *ring;
  1018. int i;
  1019. for (i = 0; i < interface->num_rx_queues; i++) {
  1020. ring = interface->rx_ring[i];
  1021. rcu_assign_pointer(ring->l2_accel, l2_accel);
  1022. }
  1023. interface->l2_accel = l2_accel;
  1024. }
  1025. static void *fm10k_dfwd_add_station(struct net_device *dev,
  1026. struct net_device *sdev)
  1027. {
  1028. struct fm10k_intfc *interface = netdev_priv(dev);
  1029. struct fm10k_l2_accel *l2_accel = interface->l2_accel;
  1030. struct fm10k_l2_accel *old_l2_accel = NULL;
  1031. struct fm10k_dglort_cfg dglort = { 0 };
  1032. struct fm10k_hw *hw = &interface->hw;
  1033. int size = 0, i;
  1034. u16 glort;
  1035. /* allocate l2 accel structure if it is not available */
  1036. if (!l2_accel) {
  1037. /* verify there is enough free GLORTs to support l2_accel */
  1038. if (interface->glort_count < 7)
  1039. return ERR_PTR(-EBUSY);
  1040. size = offsetof(struct fm10k_l2_accel, macvlan[7]);
  1041. l2_accel = kzalloc(size, GFP_KERNEL);
  1042. if (!l2_accel)
  1043. return ERR_PTR(-ENOMEM);
  1044. l2_accel->size = 7;
  1045. l2_accel->dglort = interface->glort;
  1046. /* update pointers */
  1047. fm10k_assign_l2_accel(interface, l2_accel);
  1048. /* do not expand if we are at our limit */
  1049. } else if ((l2_accel->count == FM10K_MAX_STATIONS) ||
  1050. (l2_accel->count == (interface->glort_count - 1))) {
  1051. return ERR_PTR(-EBUSY);
  1052. /* expand if we have hit the size limit */
  1053. } else if (l2_accel->count == l2_accel->size) {
  1054. old_l2_accel = l2_accel;
  1055. size = offsetof(struct fm10k_l2_accel,
  1056. macvlan[(l2_accel->size * 2) + 1]);
  1057. l2_accel = kzalloc(size, GFP_KERNEL);
  1058. if (!l2_accel)
  1059. return ERR_PTR(-ENOMEM);
  1060. memcpy(l2_accel, old_l2_accel,
  1061. offsetof(struct fm10k_l2_accel,
  1062. macvlan[old_l2_accel->size]));
  1063. l2_accel->size = (old_l2_accel->size * 2) + 1;
  1064. /* update pointers */
  1065. fm10k_assign_l2_accel(interface, l2_accel);
  1066. kfree_rcu(old_l2_accel, rcu);
  1067. }
  1068. /* add macvlan to accel table, and record GLORT for position */
  1069. for (i = 0; i < l2_accel->size; i++) {
  1070. if (!l2_accel->macvlan[i])
  1071. break;
  1072. }
  1073. /* record station */
  1074. l2_accel->macvlan[i] = sdev;
  1075. l2_accel->count++;
  1076. /* configure default DGLORT mapping for RSS/DCB */
  1077. dglort.idx = fm10k_dglort_pf_rss;
  1078. dglort.inner_rss = 1;
  1079. dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
  1080. dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
  1081. dglort.glort = interface->glort;
  1082. dglort.shared_l = fls(l2_accel->size);
  1083. hw->mac.ops.configure_dglort_map(hw, &dglort);
  1084. /* Add rules for this specific dglort to the switch */
  1085. fm10k_mbx_lock(interface);
  1086. glort = l2_accel->dglort + 1 + i;
  1087. hw->mac.ops.update_xcast_mode(hw, glort, FM10K_XCAST_MODE_MULTI);
  1088. hw->mac.ops.update_uc_addr(hw, glort, sdev->dev_addr, 0, true, 0);
  1089. fm10k_mbx_unlock(interface);
  1090. return sdev;
  1091. }
  1092. static void fm10k_dfwd_del_station(struct net_device *dev, void *priv)
  1093. {
  1094. struct fm10k_intfc *interface = netdev_priv(dev);
  1095. struct fm10k_l2_accel *l2_accel = READ_ONCE(interface->l2_accel);
  1096. struct fm10k_dglort_cfg dglort = { 0 };
  1097. struct fm10k_hw *hw = &interface->hw;
  1098. struct net_device *sdev = priv;
  1099. int i;
  1100. u16 glort;
  1101. if (!l2_accel)
  1102. return;
  1103. /* search table for matching interface */
  1104. for (i = 0; i < l2_accel->size; i++) {
  1105. if (l2_accel->macvlan[i] == sdev)
  1106. break;
  1107. }
  1108. /* exit if macvlan not found */
  1109. if (i == l2_accel->size)
  1110. return;
  1111. /* Remove any rules specific to this dglort */
  1112. fm10k_mbx_lock(interface);
  1113. glort = l2_accel->dglort + 1 + i;
  1114. hw->mac.ops.update_xcast_mode(hw, glort, FM10K_XCAST_MODE_NONE);
  1115. hw->mac.ops.update_uc_addr(hw, glort, sdev->dev_addr, 0, false, 0);
  1116. fm10k_mbx_unlock(interface);
  1117. /* record removal */
  1118. l2_accel->macvlan[i] = NULL;
  1119. l2_accel->count--;
  1120. /* configure default DGLORT mapping for RSS/DCB */
  1121. dglort.idx = fm10k_dglort_pf_rss;
  1122. dglort.inner_rss = 1;
  1123. dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
  1124. dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
  1125. dglort.glort = interface->glort;
  1126. dglort.shared_l = fls(l2_accel->size);
  1127. hw->mac.ops.configure_dglort_map(hw, &dglort);
  1128. /* If table is empty remove it */
  1129. if (l2_accel->count == 0) {
  1130. fm10k_assign_l2_accel(interface, NULL);
  1131. kfree_rcu(l2_accel, rcu);
  1132. }
  1133. }
  1134. static netdev_features_t fm10k_features_check(struct sk_buff *skb,
  1135. struct net_device *dev,
  1136. netdev_features_t features)
  1137. {
  1138. if (!skb->encapsulation || fm10k_tx_encap_offload(skb))
  1139. return features;
  1140. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  1141. }
  1142. static const struct net_device_ops fm10k_netdev_ops = {
  1143. .ndo_open = fm10k_open,
  1144. .ndo_stop = fm10k_close,
  1145. .ndo_validate_addr = eth_validate_addr,
  1146. .ndo_start_xmit = fm10k_xmit_frame,
  1147. .ndo_set_mac_address = fm10k_set_mac,
  1148. .ndo_change_mtu = fm10k_change_mtu,
  1149. .ndo_tx_timeout = fm10k_tx_timeout,
  1150. .ndo_vlan_rx_add_vid = fm10k_vlan_rx_add_vid,
  1151. .ndo_vlan_rx_kill_vid = fm10k_vlan_rx_kill_vid,
  1152. .ndo_set_rx_mode = fm10k_set_rx_mode,
  1153. .ndo_get_stats64 = fm10k_get_stats64,
  1154. .ndo_setup_tc = __fm10k_setup_tc,
  1155. .ndo_set_vf_mac = fm10k_ndo_set_vf_mac,
  1156. .ndo_set_vf_vlan = fm10k_ndo_set_vf_vlan,
  1157. .ndo_set_vf_rate = fm10k_ndo_set_vf_bw,
  1158. .ndo_get_vf_config = fm10k_ndo_get_vf_config,
  1159. .ndo_udp_tunnel_add = fm10k_udp_tunnel_add,
  1160. .ndo_udp_tunnel_del = fm10k_udp_tunnel_del,
  1161. .ndo_dfwd_add_station = fm10k_dfwd_add_station,
  1162. .ndo_dfwd_del_station = fm10k_dfwd_del_station,
  1163. #ifdef CONFIG_NET_POLL_CONTROLLER
  1164. .ndo_poll_controller = fm10k_netpoll,
  1165. #endif
  1166. .ndo_features_check = fm10k_features_check,
  1167. };
  1168. #define DEFAULT_DEBUG_LEVEL_SHIFT 3
  1169. struct net_device *fm10k_alloc_netdev(const struct fm10k_info *info)
  1170. {
  1171. netdev_features_t hw_features;
  1172. struct fm10k_intfc *interface;
  1173. struct net_device *dev;
  1174. dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES);
  1175. if (!dev)
  1176. return NULL;
  1177. /* set net device and ethtool ops */
  1178. dev->netdev_ops = &fm10k_netdev_ops;
  1179. fm10k_set_ethtool_ops(dev);
  1180. /* configure default debug level */
  1181. interface = netdev_priv(dev);
  1182. interface->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
  1183. /* configure default features */
  1184. dev->features |= NETIF_F_IP_CSUM |
  1185. NETIF_F_IPV6_CSUM |
  1186. NETIF_F_SG |
  1187. NETIF_F_TSO |
  1188. NETIF_F_TSO6 |
  1189. NETIF_F_TSO_ECN |
  1190. NETIF_F_RXHASH |
  1191. NETIF_F_RXCSUM;
  1192. /* Only the PF can support VXLAN and NVGRE tunnel offloads */
  1193. if (info->mac == fm10k_mac_pf) {
  1194. dev->hw_enc_features = NETIF_F_IP_CSUM |
  1195. NETIF_F_TSO |
  1196. NETIF_F_TSO6 |
  1197. NETIF_F_TSO_ECN |
  1198. NETIF_F_GSO_UDP_TUNNEL |
  1199. NETIF_F_IPV6_CSUM |
  1200. NETIF_F_SG;
  1201. dev->features |= NETIF_F_GSO_UDP_TUNNEL;
  1202. }
  1203. /* all features defined to this point should be changeable */
  1204. hw_features = dev->features;
  1205. /* allow user to enable L2 forwarding acceleration */
  1206. hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
  1207. /* configure VLAN features */
  1208. dev->vlan_features |= dev->features;
  1209. /* we want to leave these both on as we cannot disable VLAN tag
  1210. * insertion or stripping on the hardware since it is contained
  1211. * in the FTAG and not in the frame itself.
  1212. */
  1213. dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
  1214. NETIF_F_HW_VLAN_CTAG_RX |
  1215. NETIF_F_HW_VLAN_CTAG_FILTER;
  1216. dev->priv_flags |= IFF_UNICAST_FLT;
  1217. dev->hw_features |= hw_features;
  1218. return dev;
  1219. }