fm10k_netdev.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  1. /* Intel(R) Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2017 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. int num_tx_queues = READ_ONCE(interface->num_tx_queues);
  544. unsigned int r_idx = skb->queue_mapping;
  545. int err;
  546. if (!num_tx_queues)
  547. return NETDEV_TX_BUSY;
  548. if ((skb->protocol == htons(ETH_P_8021Q)) &&
  549. !skb_vlan_tag_present(skb)) {
  550. /* FM10K only supports hardware tagging, any tags in frame
  551. * are considered 2nd level or "outer" tags
  552. */
  553. struct vlan_hdr *vhdr;
  554. __be16 proto;
  555. /* make sure skb is not shared */
  556. skb = skb_share_check(skb, GFP_ATOMIC);
  557. if (!skb)
  558. return NETDEV_TX_OK;
  559. /* make sure there is enough room to move the ethernet header */
  560. if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
  561. return NETDEV_TX_OK;
  562. /* verify the skb head is not shared */
  563. err = skb_cow_head(skb, 0);
  564. if (err) {
  565. dev_kfree_skb(skb);
  566. return NETDEV_TX_OK;
  567. }
  568. /* locate VLAN header */
  569. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  570. /* pull the 2 key pieces of data out of it */
  571. __vlan_hwaccel_put_tag(skb,
  572. htons(ETH_P_8021Q),
  573. ntohs(vhdr->h_vlan_TCI));
  574. proto = vhdr->h_vlan_encapsulated_proto;
  575. skb->protocol = (ntohs(proto) >= 1536) ? proto :
  576. htons(ETH_P_802_2);
  577. /* squash it by moving the ethernet addresses up 4 bytes */
  578. memmove(skb->data + VLAN_HLEN, skb->data, 12);
  579. __skb_pull(skb, VLAN_HLEN);
  580. skb_reset_mac_header(skb);
  581. }
  582. /* The minimum packet size for a single buffer is 17B so pad the skb
  583. * in order to meet this minimum size requirement.
  584. */
  585. if (unlikely(skb->len < 17)) {
  586. int pad_len = 17 - skb->len;
  587. if (skb_pad(skb, pad_len))
  588. return NETDEV_TX_OK;
  589. __skb_put(skb, pad_len);
  590. }
  591. if (r_idx >= num_tx_queues)
  592. r_idx %= num_tx_queues;
  593. err = fm10k_xmit_frame_ring(skb, interface->tx_ring[r_idx]);
  594. return err;
  595. }
  596. /**
  597. * fm10k_tx_timeout - Respond to a Tx Hang
  598. * @netdev: network interface device structure
  599. **/
  600. static void fm10k_tx_timeout(struct net_device *netdev)
  601. {
  602. struct fm10k_intfc *interface = netdev_priv(netdev);
  603. bool real_tx_hang = false;
  604. int i;
  605. #define TX_TIMEO_LIMIT 16000
  606. for (i = 0; i < interface->num_tx_queues; i++) {
  607. struct fm10k_ring *tx_ring = interface->tx_ring[i];
  608. if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring))
  609. real_tx_hang = true;
  610. }
  611. if (real_tx_hang) {
  612. fm10k_tx_timeout_reset(interface);
  613. } else {
  614. netif_info(interface, drv, netdev,
  615. "Fake Tx hang detected with timeout of %d seconds\n",
  616. netdev->watchdog_timeo / HZ);
  617. /* fake Tx hang - increase the kernel timeout */
  618. if (netdev->watchdog_timeo < TX_TIMEO_LIMIT)
  619. netdev->watchdog_timeo *= 2;
  620. }
  621. }
  622. /**
  623. * fm10k_host_mbx_ready - Check PF interface's mailbox readiness
  624. * @interface: board private structure
  625. *
  626. * This function checks if the PF interface's mailbox is ready before queueing
  627. * mailbox messages for transmission. This will prevent filling the TX mailbox
  628. * queue when the receiver is not ready. VF interfaces are exempt from this
  629. * check since it will block all PF-VF mailbox messages from being sent from
  630. * the VF to the PF at initialization.
  631. **/
  632. static bool fm10k_host_mbx_ready(struct fm10k_intfc *interface)
  633. {
  634. struct fm10k_hw *hw = &interface->hw;
  635. return (hw->mac.type == fm10k_mac_vf || interface->host_ready);
  636. }
  637. /**
  638. * fm10k_queue_vlan_request - Queue a VLAN update request
  639. * @interface: the fm10k interface structure
  640. * @vid: the VLAN vid
  641. * @vsi: VSI index number
  642. * @set: whether to set or clear
  643. *
  644. * This function queues up a VLAN update. For VFs, this must be sent to the
  645. * managing PF over the mailbox. For PFs, we'll use the same handling so that
  646. * it's similar to the VF. This avoids storming the PF<->VF mailbox with too
  647. * many VLAN updates during reset.
  648. */
  649. int fm10k_queue_vlan_request(struct fm10k_intfc *interface,
  650. u32 vid, u8 vsi, bool set)
  651. {
  652. struct fm10k_macvlan_request *request;
  653. unsigned long flags;
  654. /* This must be atomic since we may be called while the netdev
  655. * addr_list_lock is held
  656. */
  657. request = kzalloc(sizeof(*request), GFP_ATOMIC);
  658. if (!request)
  659. return -ENOMEM;
  660. request->type = FM10K_VLAN_REQUEST;
  661. request->vlan.vid = vid;
  662. request->vlan.vsi = vsi;
  663. request->set = set;
  664. spin_lock_irqsave(&interface->macvlan_lock, flags);
  665. list_add_tail(&request->list, &interface->macvlan_requests);
  666. spin_unlock_irqrestore(&interface->macvlan_lock, flags);
  667. fm10k_macvlan_schedule(interface);
  668. return 0;
  669. }
  670. /**
  671. * fm10k_queue_mac_request - Queue a MAC update request
  672. * @interface: the fm10k interface structure
  673. * @glort: the target glort for this update
  674. * @addr: the address to update
  675. * @vid: the vid to update
  676. * @sync: whether to add or remove
  677. *
  678. * This function queues up a MAC request for sending to the switch manager.
  679. * A separate thread monitors the queue and sends updates to the switch
  680. * manager. Return 0 on success, and negative error code on failure.
  681. **/
  682. int fm10k_queue_mac_request(struct fm10k_intfc *interface, u16 glort,
  683. const unsigned char *addr, u16 vid, bool set)
  684. {
  685. struct fm10k_macvlan_request *request;
  686. unsigned long flags;
  687. /* This must be atomic since we may be called while the netdev
  688. * addr_list_lock is held
  689. */
  690. request = kzalloc(sizeof(*request), GFP_ATOMIC);
  691. if (!request)
  692. return -ENOMEM;
  693. if (is_multicast_ether_addr(addr))
  694. request->type = FM10K_MC_MAC_REQUEST;
  695. else
  696. request->type = FM10K_UC_MAC_REQUEST;
  697. ether_addr_copy(request->mac.addr, addr);
  698. request->mac.glort = glort;
  699. request->mac.vid = vid;
  700. request->set = set;
  701. spin_lock_irqsave(&interface->macvlan_lock, flags);
  702. list_add_tail(&request->list, &interface->macvlan_requests);
  703. spin_unlock_irqrestore(&interface->macvlan_lock, flags);
  704. fm10k_macvlan_schedule(interface);
  705. return 0;
  706. }
  707. /**
  708. * fm10k_clear_macvlan_queue - Cancel pending updates for a given glort
  709. * @interface: the fm10k interface structure
  710. * @glort: the target glort to clear
  711. * @vlans: true to clear VLAN messages, false to ignore them
  712. *
  713. * Cancel any outstanding MAC/VLAN requests for a given glort. This is
  714. * expected to be called when a logical port goes down.
  715. **/
  716. void fm10k_clear_macvlan_queue(struct fm10k_intfc *interface,
  717. u16 glort, bool vlans)
  718. {
  719. struct fm10k_macvlan_request *r, *tmp;
  720. unsigned long flags;
  721. spin_lock_irqsave(&interface->macvlan_lock, flags);
  722. /* Free any outstanding MAC/VLAN requests for this interface */
  723. list_for_each_entry_safe(r, tmp, &interface->macvlan_requests, list) {
  724. switch (r->type) {
  725. case FM10K_MC_MAC_REQUEST:
  726. case FM10K_UC_MAC_REQUEST:
  727. /* Don't free requests for other interfaces */
  728. if (r->mac.glort != glort)
  729. break;
  730. /* fall through */
  731. case FM10K_VLAN_REQUEST:
  732. if (vlans) {
  733. list_del(&r->list);
  734. kfree(r);
  735. }
  736. break;
  737. }
  738. }
  739. spin_unlock_irqrestore(&interface->macvlan_lock, flags);
  740. }
  741. static int fm10k_uc_vlan_unsync(struct net_device *netdev,
  742. const unsigned char *uc_addr)
  743. {
  744. struct fm10k_intfc *interface = netdev_priv(netdev);
  745. u16 glort = interface->glort;
  746. u16 vid = interface->vid;
  747. bool set = !!(vid / VLAN_N_VID);
  748. int err = -EHOSTDOWN;
  749. /* drop any leading bits on the VLAN ID */
  750. vid &= VLAN_N_VID - 1;
  751. err = fm10k_queue_mac_request(interface, glort, uc_addr, vid, set);
  752. if (err)
  753. return err;
  754. /* return non-zero value as we are only doing a partial sync/unsync */
  755. return 1;
  756. }
  757. static int fm10k_mc_vlan_unsync(struct net_device *netdev,
  758. const unsigned char *mc_addr)
  759. {
  760. struct fm10k_intfc *interface = netdev_priv(netdev);
  761. u16 glort = interface->glort;
  762. u16 vid = interface->vid;
  763. bool set = !!(vid / VLAN_N_VID);
  764. int err = -EHOSTDOWN;
  765. /* drop any leading bits on the VLAN ID */
  766. vid &= VLAN_N_VID - 1;
  767. err = fm10k_queue_mac_request(interface, glort, mc_addr, vid, set);
  768. if (err)
  769. return err;
  770. /* return non-zero value as we are only doing a partial sync/unsync */
  771. return 1;
  772. }
  773. static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
  774. {
  775. struct fm10k_intfc *interface = netdev_priv(netdev);
  776. struct fm10k_hw *hw = &interface->hw;
  777. s32 err;
  778. int i;
  779. /* updates do not apply to VLAN 0 */
  780. if (!vid)
  781. return 0;
  782. if (vid >= VLAN_N_VID)
  783. return -EINVAL;
  784. /* Verify we have permission to add VLANs */
  785. if (hw->mac.vlan_override)
  786. return -EACCES;
  787. /* update active_vlans bitmask */
  788. set_bit(vid, interface->active_vlans);
  789. if (!set)
  790. clear_bit(vid, interface->active_vlans);
  791. /* disable the default VLAN ID on ring if we have an active VLAN */
  792. for (i = 0; i < interface->num_rx_queues; i++) {
  793. struct fm10k_ring *rx_ring = interface->rx_ring[i];
  794. u16 rx_vid = rx_ring->vid & (VLAN_N_VID - 1);
  795. if (test_bit(rx_vid, interface->active_vlans))
  796. rx_ring->vid |= FM10K_VLAN_CLEAR;
  797. else
  798. rx_ring->vid &= ~FM10K_VLAN_CLEAR;
  799. }
  800. /* Do not remove default VLAN ID related entries from VLAN and MAC
  801. * tables
  802. */
  803. if (!set && vid == hw->mac.default_vid)
  804. return 0;
  805. /* Do not throw an error if the interface is down. We will sync once
  806. * we come up
  807. */
  808. if (test_bit(__FM10K_DOWN, interface->state))
  809. return 0;
  810. fm10k_mbx_lock(interface);
  811. /* only need to update the VLAN if not in promiscuous mode */
  812. if (!(netdev->flags & IFF_PROMISC)) {
  813. err = fm10k_queue_vlan_request(interface, vid, 0, set);
  814. if (err)
  815. goto err_out;
  816. }
  817. /* Update our base MAC address */
  818. err = fm10k_queue_mac_request(interface, interface->glort,
  819. hw->mac.addr, vid, set);
  820. if (err)
  821. goto err_out;
  822. /* set VLAN ID prior to syncing/unsyncing the VLAN */
  823. interface->vid = vid + (set ? VLAN_N_VID : 0);
  824. /* Update the unicast and multicast address list to add/drop VLAN */
  825. __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
  826. __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
  827. err_out:
  828. fm10k_mbx_unlock(interface);
  829. return err;
  830. }
  831. static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
  832. __always_unused __be16 proto, u16 vid)
  833. {
  834. /* update VLAN and address table based on changes */
  835. return fm10k_update_vid(netdev, vid, true);
  836. }
  837. static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
  838. __always_unused __be16 proto, u16 vid)
  839. {
  840. /* update VLAN and address table based on changes */
  841. return fm10k_update_vid(netdev, vid, false);
  842. }
  843. static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
  844. {
  845. struct fm10k_hw *hw = &interface->hw;
  846. u16 default_vid = hw->mac.default_vid;
  847. u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
  848. vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
  849. return vid;
  850. }
  851. static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
  852. {
  853. u32 vid, prev_vid;
  854. /* loop through and find any gaps in the table */
  855. for (vid = 0, prev_vid = 0;
  856. prev_vid < VLAN_N_VID;
  857. prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
  858. if (prev_vid == vid)
  859. continue;
  860. /* send request to clear multiple bits at a time */
  861. prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
  862. fm10k_queue_vlan_request(interface, prev_vid, 0, false);
  863. }
  864. }
  865. static int __fm10k_uc_sync(struct net_device *dev,
  866. const unsigned char *addr, bool sync)
  867. {
  868. struct fm10k_intfc *interface = netdev_priv(dev);
  869. struct fm10k_hw *hw = &interface->hw;
  870. u16 vid, glort = interface->glort;
  871. s32 err;
  872. if (!is_valid_ether_addr(addr))
  873. return -EADDRNOTAVAIL;
  874. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
  875. vid < VLAN_N_VID;
  876. vid = fm10k_find_next_vlan(interface, vid)) {
  877. err = fm10k_queue_mac_request(interface, glort,
  878. addr, vid, sync);
  879. if (err)
  880. return err;
  881. }
  882. return 0;
  883. }
  884. static int fm10k_uc_sync(struct net_device *dev,
  885. const unsigned char *addr)
  886. {
  887. return __fm10k_uc_sync(dev, addr, true);
  888. }
  889. static int fm10k_uc_unsync(struct net_device *dev,
  890. const unsigned char *addr)
  891. {
  892. return __fm10k_uc_sync(dev, addr, false);
  893. }
  894. static int fm10k_set_mac(struct net_device *dev, void *p)
  895. {
  896. struct fm10k_intfc *interface = netdev_priv(dev);
  897. struct fm10k_hw *hw = &interface->hw;
  898. struct sockaddr *addr = p;
  899. s32 err = 0;
  900. if (!is_valid_ether_addr(addr->sa_data))
  901. return -EADDRNOTAVAIL;
  902. if (dev->flags & IFF_UP) {
  903. /* setting MAC address requires mailbox */
  904. fm10k_mbx_lock(interface);
  905. err = fm10k_uc_sync(dev, addr->sa_data);
  906. if (!err)
  907. fm10k_uc_unsync(dev, hw->mac.addr);
  908. fm10k_mbx_unlock(interface);
  909. }
  910. if (!err) {
  911. ether_addr_copy(dev->dev_addr, addr->sa_data);
  912. ether_addr_copy(hw->mac.addr, addr->sa_data);
  913. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  914. }
  915. /* if we had a mailbox error suggest trying again */
  916. return err ? -EAGAIN : 0;
  917. }
  918. static int __fm10k_mc_sync(struct net_device *dev,
  919. const unsigned char *addr, bool sync)
  920. {
  921. struct fm10k_intfc *interface = netdev_priv(dev);
  922. struct fm10k_hw *hw = &interface->hw;
  923. u16 vid, glort = interface->glort;
  924. s32 err;
  925. if (!is_multicast_ether_addr(addr))
  926. return -EADDRNOTAVAIL;
  927. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
  928. vid < VLAN_N_VID;
  929. vid = fm10k_find_next_vlan(interface, vid)) {
  930. err = fm10k_queue_mac_request(interface, glort,
  931. addr, vid, sync);
  932. if (err)
  933. return err;
  934. }
  935. return 0;
  936. }
  937. static int fm10k_mc_sync(struct net_device *dev,
  938. const unsigned char *addr)
  939. {
  940. return __fm10k_mc_sync(dev, addr, true);
  941. }
  942. static int fm10k_mc_unsync(struct net_device *dev,
  943. const unsigned char *addr)
  944. {
  945. return __fm10k_mc_sync(dev, addr, false);
  946. }
  947. static void fm10k_set_rx_mode(struct net_device *dev)
  948. {
  949. struct fm10k_intfc *interface = netdev_priv(dev);
  950. struct fm10k_hw *hw = &interface->hw;
  951. int xcast_mode;
  952. /* no need to update the harwdare if we are not running */
  953. if (!(dev->flags & IFF_UP))
  954. return;
  955. /* determine new mode based on flags */
  956. xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
  957. (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
  958. (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
  959. FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
  960. fm10k_mbx_lock(interface);
  961. /* update xcast mode first, but only if it changed */
  962. if (interface->xcast_mode != xcast_mode) {
  963. /* update VLAN table */
  964. if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
  965. fm10k_queue_vlan_request(interface, FM10K_VLAN_ALL,
  966. 0, true);
  967. if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
  968. fm10k_clear_unused_vlans(interface);
  969. /* update xcast mode if host's mailbox is ready */
  970. if (fm10k_host_mbx_ready(interface))
  971. hw->mac.ops.update_xcast_mode(hw, interface->glort,
  972. xcast_mode);
  973. /* record updated xcast mode state */
  974. interface->xcast_mode = xcast_mode;
  975. }
  976. /* synchronize all of the addresses */
  977. __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
  978. __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
  979. fm10k_mbx_unlock(interface);
  980. }
  981. void fm10k_restore_rx_state(struct fm10k_intfc *interface)
  982. {
  983. struct net_device *netdev = interface->netdev;
  984. struct fm10k_hw *hw = &interface->hw;
  985. int xcast_mode;
  986. u16 vid, glort;
  987. /* record glort for this interface */
  988. glort = interface->glort;
  989. /* convert interface flags to xcast mode */
  990. if (netdev->flags & IFF_PROMISC)
  991. xcast_mode = FM10K_XCAST_MODE_PROMISC;
  992. else if (netdev->flags & IFF_ALLMULTI)
  993. xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
  994. else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
  995. xcast_mode = FM10K_XCAST_MODE_MULTI;
  996. else
  997. xcast_mode = FM10K_XCAST_MODE_NONE;
  998. fm10k_mbx_lock(interface);
  999. /* Enable logical port if host's mailbox is ready */
  1000. if (fm10k_host_mbx_ready(interface))
  1001. hw->mac.ops.update_lport_state(hw, glort,
  1002. interface->glort_count, true);
  1003. /* update VLAN table */
  1004. fm10k_queue_vlan_request(interface, FM10K_VLAN_ALL, 0,
  1005. xcast_mode == FM10K_XCAST_MODE_PROMISC);
  1006. /* Add filter for VLAN 0 */
  1007. fm10k_queue_vlan_request(interface, 0, 0, true);
  1008. /* update table with current entries */
  1009. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
  1010. vid < VLAN_N_VID;
  1011. vid = fm10k_find_next_vlan(interface, vid)) {
  1012. fm10k_queue_vlan_request(interface, vid, 0, true);
  1013. fm10k_queue_mac_request(interface, glort,
  1014. hw->mac.addr, vid, true);
  1015. }
  1016. /* update xcast mode before synchronizing addresses if host's mailbox
  1017. * is ready
  1018. */
  1019. if (fm10k_host_mbx_ready(interface))
  1020. hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
  1021. /* synchronize all of the addresses */
  1022. __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
  1023. __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
  1024. fm10k_mbx_unlock(interface);
  1025. /* record updated xcast mode state */
  1026. interface->xcast_mode = xcast_mode;
  1027. /* Restore tunnel configuration */
  1028. fm10k_restore_udp_port_info(interface);
  1029. }
  1030. void fm10k_reset_rx_state(struct fm10k_intfc *interface)
  1031. {
  1032. struct net_device *netdev = interface->netdev;
  1033. struct fm10k_hw *hw = &interface->hw;
  1034. /* Wait for MAC/VLAN work to finish */
  1035. while (test_bit(__FM10K_MACVLAN_SCHED, interface->state))
  1036. usleep_range(1000, 2000);
  1037. /* Cancel pending MAC/VLAN requests */
  1038. fm10k_clear_macvlan_queue(interface, interface->glort, true);
  1039. fm10k_mbx_lock(interface);
  1040. /* clear the logical port state on lower device if host's mailbox is
  1041. * ready
  1042. */
  1043. if (fm10k_host_mbx_ready(interface))
  1044. hw->mac.ops.update_lport_state(hw, interface->glort,
  1045. interface->glort_count, false);
  1046. fm10k_mbx_unlock(interface);
  1047. /* reset flags to default state */
  1048. interface->xcast_mode = FM10K_XCAST_MODE_NONE;
  1049. /* clear the sync flag since the lport has been dropped */
  1050. __dev_uc_unsync(netdev, NULL);
  1051. __dev_mc_unsync(netdev, NULL);
  1052. }
  1053. /**
  1054. * fm10k_get_stats64 - Get System Network Statistics
  1055. * @netdev: network interface device structure
  1056. * @stats: storage space for 64bit statistics
  1057. *
  1058. * Obtain 64bit statistics in a way that is safe for both 32bit and 64bit
  1059. * architectures.
  1060. */
  1061. static void fm10k_get_stats64(struct net_device *netdev,
  1062. struct rtnl_link_stats64 *stats)
  1063. {
  1064. struct fm10k_intfc *interface = netdev_priv(netdev);
  1065. struct fm10k_ring *ring;
  1066. unsigned int start, i;
  1067. u64 bytes, packets;
  1068. rcu_read_lock();
  1069. for (i = 0; i < interface->num_rx_queues; i++) {
  1070. ring = READ_ONCE(interface->rx_ring[i]);
  1071. if (!ring)
  1072. continue;
  1073. do {
  1074. start = u64_stats_fetch_begin_irq(&ring->syncp);
  1075. packets = ring->stats.packets;
  1076. bytes = ring->stats.bytes;
  1077. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  1078. stats->rx_packets += packets;
  1079. stats->rx_bytes += bytes;
  1080. }
  1081. for (i = 0; i < interface->num_tx_queues; i++) {
  1082. ring = READ_ONCE(interface->tx_ring[i]);
  1083. if (!ring)
  1084. continue;
  1085. do {
  1086. start = u64_stats_fetch_begin_irq(&ring->syncp);
  1087. packets = ring->stats.packets;
  1088. bytes = ring->stats.bytes;
  1089. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  1090. stats->tx_packets += packets;
  1091. stats->tx_bytes += bytes;
  1092. }
  1093. rcu_read_unlock();
  1094. /* following stats updated by fm10k_service_task() */
  1095. stats->rx_missed_errors = netdev->stats.rx_missed_errors;
  1096. }
  1097. int fm10k_setup_tc(struct net_device *dev, u8 tc)
  1098. {
  1099. struct fm10k_intfc *interface = netdev_priv(dev);
  1100. int err;
  1101. /* Currently only the PF supports priority classes */
  1102. if (tc && (interface->hw.mac.type != fm10k_mac_pf))
  1103. return -EINVAL;
  1104. /* Hardware supports up to 8 traffic classes */
  1105. if (tc > 8)
  1106. return -EINVAL;
  1107. /* Hardware has to reinitialize queues to match packet
  1108. * buffer alignment. Unfortunately, the hardware is not
  1109. * flexible enough to do this dynamically.
  1110. */
  1111. if (netif_running(dev))
  1112. fm10k_close(dev);
  1113. fm10k_mbx_free_irq(interface);
  1114. fm10k_clear_queueing_scheme(interface);
  1115. /* we expect the prio_tc map to be repopulated later */
  1116. netdev_reset_tc(dev);
  1117. netdev_set_num_tc(dev, tc);
  1118. err = fm10k_init_queueing_scheme(interface);
  1119. if (err)
  1120. goto err_queueing_scheme;
  1121. err = fm10k_mbx_request_irq(interface);
  1122. if (err)
  1123. goto err_mbx_irq;
  1124. err = netif_running(dev) ? fm10k_open(dev) : 0;
  1125. if (err)
  1126. goto err_open;
  1127. /* flag to indicate SWPRI has yet to be updated */
  1128. set_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags);
  1129. return 0;
  1130. err_open:
  1131. fm10k_mbx_free_irq(interface);
  1132. err_mbx_irq:
  1133. fm10k_clear_queueing_scheme(interface);
  1134. err_queueing_scheme:
  1135. netif_device_detach(dev);
  1136. return err;
  1137. }
  1138. static int __fm10k_setup_tc(struct net_device *dev, enum tc_setup_type type,
  1139. void *type_data)
  1140. {
  1141. struct tc_mqprio_qopt *mqprio = type_data;
  1142. if (type != TC_SETUP_MQPRIO)
  1143. return -EOPNOTSUPP;
  1144. mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  1145. return fm10k_setup_tc(dev, mqprio->num_tc);
  1146. }
  1147. static void fm10k_assign_l2_accel(struct fm10k_intfc *interface,
  1148. struct fm10k_l2_accel *l2_accel)
  1149. {
  1150. struct fm10k_ring *ring;
  1151. int i;
  1152. for (i = 0; i < interface->num_rx_queues; i++) {
  1153. ring = interface->rx_ring[i];
  1154. rcu_assign_pointer(ring->l2_accel, l2_accel);
  1155. }
  1156. interface->l2_accel = l2_accel;
  1157. }
  1158. static void *fm10k_dfwd_add_station(struct net_device *dev,
  1159. struct net_device *sdev)
  1160. {
  1161. struct fm10k_intfc *interface = netdev_priv(dev);
  1162. struct fm10k_l2_accel *l2_accel = interface->l2_accel;
  1163. struct fm10k_l2_accel *old_l2_accel = NULL;
  1164. struct fm10k_dglort_cfg dglort = { 0 };
  1165. struct fm10k_hw *hw = &interface->hw;
  1166. int size = 0, i;
  1167. u16 glort;
  1168. /* allocate l2 accel structure if it is not available */
  1169. if (!l2_accel) {
  1170. /* verify there is enough free GLORTs to support l2_accel */
  1171. if (interface->glort_count < 7)
  1172. return ERR_PTR(-EBUSY);
  1173. size = offsetof(struct fm10k_l2_accel, macvlan[7]);
  1174. l2_accel = kzalloc(size, GFP_KERNEL);
  1175. if (!l2_accel)
  1176. return ERR_PTR(-ENOMEM);
  1177. l2_accel->size = 7;
  1178. l2_accel->dglort = interface->glort;
  1179. /* update pointers */
  1180. fm10k_assign_l2_accel(interface, l2_accel);
  1181. /* do not expand if we are at our limit */
  1182. } else if ((l2_accel->count == FM10K_MAX_STATIONS) ||
  1183. (l2_accel->count == (interface->glort_count - 1))) {
  1184. return ERR_PTR(-EBUSY);
  1185. /* expand if we have hit the size limit */
  1186. } else if (l2_accel->count == l2_accel->size) {
  1187. old_l2_accel = l2_accel;
  1188. size = offsetof(struct fm10k_l2_accel,
  1189. macvlan[(l2_accel->size * 2) + 1]);
  1190. l2_accel = kzalloc(size, GFP_KERNEL);
  1191. if (!l2_accel)
  1192. return ERR_PTR(-ENOMEM);
  1193. memcpy(l2_accel, old_l2_accel,
  1194. offsetof(struct fm10k_l2_accel,
  1195. macvlan[old_l2_accel->size]));
  1196. l2_accel->size = (old_l2_accel->size * 2) + 1;
  1197. /* update pointers */
  1198. fm10k_assign_l2_accel(interface, l2_accel);
  1199. kfree_rcu(old_l2_accel, rcu);
  1200. }
  1201. /* add macvlan to accel table, and record GLORT for position */
  1202. for (i = 0; i < l2_accel->size; i++) {
  1203. if (!l2_accel->macvlan[i])
  1204. break;
  1205. }
  1206. /* record station */
  1207. l2_accel->macvlan[i] = sdev;
  1208. l2_accel->count++;
  1209. /* configure default DGLORT mapping for RSS/DCB */
  1210. dglort.idx = fm10k_dglort_pf_rss;
  1211. dglort.inner_rss = 1;
  1212. dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
  1213. dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
  1214. dglort.glort = interface->glort;
  1215. dglort.shared_l = fls(l2_accel->size);
  1216. hw->mac.ops.configure_dglort_map(hw, &dglort);
  1217. /* Add rules for this specific dglort to the switch */
  1218. fm10k_mbx_lock(interface);
  1219. glort = l2_accel->dglort + 1 + i;
  1220. if (fm10k_host_mbx_ready(interface)) {
  1221. hw->mac.ops.update_xcast_mode(hw, glort,
  1222. FM10K_XCAST_MODE_MULTI);
  1223. fm10k_queue_mac_request(interface, glort, sdev->dev_addr,
  1224. 0, true);
  1225. }
  1226. fm10k_mbx_unlock(interface);
  1227. return sdev;
  1228. }
  1229. static void fm10k_dfwd_del_station(struct net_device *dev, void *priv)
  1230. {
  1231. struct fm10k_intfc *interface = netdev_priv(dev);
  1232. struct fm10k_l2_accel *l2_accel = READ_ONCE(interface->l2_accel);
  1233. struct fm10k_dglort_cfg dglort = { 0 };
  1234. struct fm10k_hw *hw = &interface->hw;
  1235. struct net_device *sdev = priv;
  1236. int i;
  1237. u16 glort;
  1238. if (!l2_accel)
  1239. return;
  1240. /* search table for matching interface */
  1241. for (i = 0; i < l2_accel->size; i++) {
  1242. if (l2_accel->macvlan[i] == sdev)
  1243. break;
  1244. }
  1245. /* exit if macvlan not found */
  1246. if (i == l2_accel->size)
  1247. return;
  1248. /* Remove any rules specific to this dglort */
  1249. fm10k_mbx_lock(interface);
  1250. glort = l2_accel->dglort + 1 + i;
  1251. if (fm10k_host_mbx_ready(interface)) {
  1252. hw->mac.ops.update_xcast_mode(hw, glort,
  1253. FM10K_XCAST_MODE_NONE);
  1254. fm10k_queue_mac_request(interface, glort, sdev->dev_addr,
  1255. 0, false);
  1256. }
  1257. fm10k_mbx_unlock(interface);
  1258. /* record removal */
  1259. l2_accel->macvlan[i] = NULL;
  1260. l2_accel->count--;
  1261. /* configure default DGLORT mapping for RSS/DCB */
  1262. dglort.idx = fm10k_dglort_pf_rss;
  1263. dglort.inner_rss = 1;
  1264. dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
  1265. dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
  1266. dglort.glort = interface->glort;
  1267. dglort.shared_l = fls(l2_accel->size);
  1268. hw->mac.ops.configure_dglort_map(hw, &dglort);
  1269. /* If table is empty remove it */
  1270. if (l2_accel->count == 0) {
  1271. fm10k_assign_l2_accel(interface, NULL);
  1272. kfree_rcu(l2_accel, rcu);
  1273. }
  1274. }
  1275. static netdev_features_t fm10k_features_check(struct sk_buff *skb,
  1276. struct net_device *dev,
  1277. netdev_features_t features)
  1278. {
  1279. if (!skb->encapsulation || fm10k_tx_encap_offload(skb))
  1280. return features;
  1281. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  1282. }
  1283. static const struct net_device_ops fm10k_netdev_ops = {
  1284. .ndo_open = fm10k_open,
  1285. .ndo_stop = fm10k_close,
  1286. .ndo_validate_addr = eth_validate_addr,
  1287. .ndo_start_xmit = fm10k_xmit_frame,
  1288. .ndo_set_mac_address = fm10k_set_mac,
  1289. .ndo_tx_timeout = fm10k_tx_timeout,
  1290. .ndo_vlan_rx_add_vid = fm10k_vlan_rx_add_vid,
  1291. .ndo_vlan_rx_kill_vid = fm10k_vlan_rx_kill_vid,
  1292. .ndo_set_rx_mode = fm10k_set_rx_mode,
  1293. .ndo_get_stats64 = fm10k_get_stats64,
  1294. .ndo_setup_tc = __fm10k_setup_tc,
  1295. .ndo_set_vf_mac = fm10k_ndo_set_vf_mac,
  1296. .ndo_set_vf_vlan = fm10k_ndo_set_vf_vlan,
  1297. .ndo_set_vf_rate = fm10k_ndo_set_vf_bw,
  1298. .ndo_get_vf_config = fm10k_ndo_get_vf_config,
  1299. .ndo_udp_tunnel_add = fm10k_udp_tunnel_add,
  1300. .ndo_udp_tunnel_del = fm10k_udp_tunnel_del,
  1301. .ndo_dfwd_add_station = fm10k_dfwd_add_station,
  1302. .ndo_dfwd_del_station = fm10k_dfwd_del_station,
  1303. #ifdef CONFIG_NET_POLL_CONTROLLER
  1304. .ndo_poll_controller = fm10k_netpoll,
  1305. #endif
  1306. .ndo_features_check = fm10k_features_check,
  1307. };
  1308. #define DEFAULT_DEBUG_LEVEL_SHIFT 3
  1309. struct net_device *fm10k_alloc_netdev(const struct fm10k_info *info)
  1310. {
  1311. netdev_features_t hw_features;
  1312. struct fm10k_intfc *interface;
  1313. struct net_device *dev;
  1314. dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES);
  1315. if (!dev)
  1316. return NULL;
  1317. /* set net device and ethtool ops */
  1318. dev->netdev_ops = &fm10k_netdev_ops;
  1319. fm10k_set_ethtool_ops(dev);
  1320. /* configure default debug level */
  1321. interface = netdev_priv(dev);
  1322. interface->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
  1323. /* configure default features */
  1324. dev->features |= NETIF_F_IP_CSUM |
  1325. NETIF_F_IPV6_CSUM |
  1326. NETIF_F_SG |
  1327. NETIF_F_TSO |
  1328. NETIF_F_TSO6 |
  1329. NETIF_F_TSO_ECN |
  1330. NETIF_F_RXHASH |
  1331. NETIF_F_RXCSUM;
  1332. /* Only the PF can support VXLAN and NVGRE tunnel offloads */
  1333. if (info->mac == fm10k_mac_pf) {
  1334. dev->hw_enc_features = NETIF_F_IP_CSUM |
  1335. NETIF_F_TSO |
  1336. NETIF_F_TSO6 |
  1337. NETIF_F_TSO_ECN |
  1338. NETIF_F_GSO_UDP_TUNNEL |
  1339. NETIF_F_IPV6_CSUM |
  1340. NETIF_F_SG;
  1341. dev->features |= NETIF_F_GSO_UDP_TUNNEL;
  1342. }
  1343. /* all features defined to this point should be changeable */
  1344. hw_features = dev->features;
  1345. /* allow user to enable L2 forwarding acceleration */
  1346. hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
  1347. /* configure VLAN features */
  1348. dev->vlan_features |= dev->features;
  1349. /* we want to leave these both on as we cannot disable VLAN tag
  1350. * insertion or stripping on the hardware since it is contained
  1351. * in the FTAG and not in the frame itself.
  1352. */
  1353. dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
  1354. NETIF_F_HW_VLAN_CTAG_RX |
  1355. NETIF_F_HW_VLAN_CTAG_FILTER;
  1356. dev->priv_flags |= IFF_UNICAST_FLT;
  1357. dev->hw_features |= hw_features;
  1358. /* MTU range: 68 - 15342 */
  1359. dev->min_mtu = ETH_MIN_MTU;
  1360. dev->max_mtu = FM10K_MAX_JUMBO_FRAME_SIZE;
  1361. return dev;
  1362. }