fm10k_netdev.c 44 KB

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