fm10k_netdev.c 44 KB

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