fm10k_netdev.c 38 KB

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