fm10k_netdev.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /* Intel Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2015 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. #if IS_ENABLED(CONFIG_FM10K_VXLAN)
  23. #include <net/vxlan.h>
  24. #endif /* CONFIG_FM10K_VXLAN */
  25. /**
  26. * fm10k_setup_tx_resources - allocate Tx resources (Descriptors)
  27. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  28. *
  29. * Return 0 on success, negative on failure
  30. **/
  31. int fm10k_setup_tx_resources(struct fm10k_ring *tx_ring)
  32. {
  33. struct device *dev = tx_ring->dev;
  34. int size;
  35. size = sizeof(struct fm10k_tx_buffer) * tx_ring->count;
  36. tx_ring->tx_buffer = vzalloc(size);
  37. if (!tx_ring->tx_buffer)
  38. goto err;
  39. u64_stats_init(&tx_ring->syncp);
  40. /* round up to nearest 4K */
  41. tx_ring->size = tx_ring->count * sizeof(struct fm10k_tx_desc);
  42. tx_ring->size = ALIGN(tx_ring->size, 4096);
  43. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  44. &tx_ring->dma, GFP_KERNEL);
  45. if (!tx_ring->desc)
  46. goto err;
  47. return 0;
  48. err:
  49. vfree(tx_ring->tx_buffer);
  50. tx_ring->tx_buffer = NULL;
  51. return -ENOMEM;
  52. }
  53. /**
  54. * fm10k_setup_all_tx_resources - allocate all queues Tx resources
  55. * @interface: board private structure
  56. *
  57. * If this function returns with an error, then it's possible one or
  58. * more of the rings is populated (while the rest are not). It is the
  59. * callers duty to clean those orphaned rings.
  60. *
  61. * Return 0 on success, negative on failure
  62. **/
  63. static int fm10k_setup_all_tx_resources(struct fm10k_intfc *interface)
  64. {
  65. int i, err = 0;
  66. for (i = 0; i < interface->num_tx_queues; i++) {
  67. err = fm10k_setup_tx_resources(interface->tx_ring[i]);
  68. if (!err)
  69. continue;
  70. netif_err(interface, probe, interface->netdev,
  71. "Allocation for Tx Queue %u failed\n", i);
  72. goto err_setup_tx;
  73. }
  74. return 0;
  75. err_setup_tx:
  76. /* rewind the index freeing the rings as we go */
  77. while (i--)
  78. fm10k_free_tx_resources(interface->tx_ring[i]);
  79. return err;
  80. }
  81. /**
  82. * fm10k_setup_rx_resources - allocate Rx resources (Descriptors)
  83. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  84. *
  85. * Returns 0 on success, negative on failure
  86. **/
  87. int fm10k_setup_rx_resources(struct fm10k_ring *rx_ring)
  88. {
  89. struct device *dev = rx_ring->dev;
  90. int size;
  91. size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
  92. rx_ring->rx_buffer = vzalloc(size);
  93. if (!rx_ring->rx_buffer)
  94. goto err;
  95. u64_stats_init(&rx_ring->syncp);
  96. /* Round up to nearest 4K */
  97. rx_ring->size = rx_ring->count * sizeof(union fm10k_rx_desc);
  98. rx_ring->size = ALIGN(rx_ring->size, 4096);
  99. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  100. &rx_ring->dma, GFP_KERNEL);
  101. if (!rx_ring->desc)
  102. goto err;
  103. return 0;
  104. err:
  105. vfree(rx_ring->rx_buffer);
  106. rx_ring->rx_buffer = NULL;
  107. return -ENOMEM;
  108. }
  109. /**
  110. * fm10k_setup_all_rx_resources - allocate all queues Rx resources
  111. * @interface: board private structure
  112. *
  113. * If this function returns with an error, then it's possible one or
  114. * more of the rings is populated (while the rest are not). It is the
  115. * callers duty to clean those orphaned rings.
  116. *
  117. * Return 0 on success, negative on failure
  118. **/
  119. static int fm10k_setup_all_rx_resources(struct fm10k_intfc *interface)
  120. {
  121. int i, err = 0;
  122. for (i = 0; i < interface->num_rx_queues; i++) {
  123. err = fm10k_setup_rx_resources(interface->rx_ring[i]);
  124. if (!err)
  125. continue;
  126. netif_err(interface, probe, interface->netdev,
  127. "Allocation for Rx Queue %u failed\n", i);
  128. goto err_setup_rx;
  129. }
  130. return 0;
  131. err_setup_rx:
  132. /* rewind the index freeing the rings as we go */
  133. while (i--)
  134. fm10k_free_rx_resources(interface->rx_ring[i]);
  135. return err;
  136. }
  137. void fm10k_unmap_and_free_tx_resource(struct fm10k_ring *ring,
  138. struct fm10k_tx_buffer *tx_buffer)
  139. {
  140. if (tx_buffer->skb) {
  141. dev_kfree_skb_any(tx_buffer->skb);
  142. if (dma_unmap_len(tx_buffer, len))
  143. dma_unmap_single(ring->dev,
  144. dma_unmap_addr(tx_buffer, dma),
  145. dma_unmap_len(tx_buffer, len),
  146. DMA_TO_DEVICE);
  147. } else if (dma_unmap_len(tx_buffer, len)) {
  148. dma_unmap_page(ring->dev,
  149. dma_unmap_addr(tx_buffer, dma),
  150. dma_unmap_len(tx_buffer, len),
  151. DMA_TO_DEVICE);
  152. }
  153. tx_buffer->next_to_watch = NULL;
  154. tx_buffer->skb = NULL;
  155. dma_unmap_len_set(tx_buffer, len, 0);
  156. /* tx_buffer must be completely set up in the transmit path */
  157. }
  158. /**
  159. * fm10k_clean_tx_ring - Free Tx Buffers
  160. * @tx_ring: ring to be cleaned
  161. **/
  162. static void fm10k_clean_tx_ring(struct fm10k_ring *tx_ring)
  163. {
  164. struct fm10k_tx_buffer *tx_buffer;
  165. unsigned long size;
  166. u16 i;
  167. /* ring already cleared, nothing to do */
  168. if (!tx_ring->tx_buffer)
  169. return;
  170. /* Free all the Tx ring sk_buffs */
  171. for (i = 0; i < tx_ring->count; i++) {
  172. tx_buffer = &tx_ring->tx_buffer[i];
  173. fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer);
  174. }
  175. /* reset BQL values */
  176. netdev_tx_reset_queue(txring_txq(tx_ring));
  177. size = sizeof(struct fm10k_tx_buffer) * tx_ring->count;
  178. memset(tx_ring->tx_buffer, 0, size);
  179. /* Zero out the descriptor ring */
  180. memset(tx_ring->desc, 0, tx_ring->size);
  181. }
  182. /**
  183. * fm10k_free_tx_resources - Free Tx Resources per Queue
  184. * @tx_ring: Tx descriptor ring for a specific queue
  185. *
  186. * Free all transmit software resources
  187. **/
  188. void fm10k_free_tx_resources(struct fm10k_ring *tx_ring)
  189. {
  190. fm10k_clean_tx_ring(tx_ring);
  191. vfree(tx_ring->tx_buffer);
  192. tx_ring->tx_buffer = NULL;
  193. /* if not set, then don't free */
  194. if (!tx_ring->desc)
  195. return;
  196. dma_free_coherent(tx_ring->dev, tx_ring->size,
  197. tx_ring->desc, tx_ring->dma);
  198. tx_ring->desc = NULL;
  199. }
  200. /**
  201. * fm10k_clean_all_tx_rings - Free Tx Buffers for all queues
  202. * @interface: board private structure
  203. **/
  204. void fm10k_clean_all_tx_rings(struct fm10k_intfc *interface)
  205. {
  206. int i;
  207. for (i = 0; i < interface->num_tx_queues; i++)
  208. fm10k_clean_tx_ring(interface->tx_ring[i]);
  209. /* remove any stale timestamp buffers and free them */
  210. skb_queue_purge(&interface->ts_tx_skb_queue);
  211. }
  212. /**
  213. * fm10k_free_all_tx_resources - Free Tx Resources for All Queues
  214. * @interface: board private structure
  215. *
  216. * Free all transmit software resources
  217. **/
  218. static void fm10k_free_all_tx_resources(struct fm10k_intfc *interface)
  219. {
  220. int i = interface->num_tx_queues;
  221. while (i--)
  222. fm10k_free_tx_resources(interface->tx_ring[i]);
  223. }
  224. /**
  225. * fm10k_clean_rx_ring - Free Rx Buffers per Queue
  226. * @rx_ring: ring to free buffers from
  227. **/
  228. static void fm10k_clean_rx_ring(struct fm10k_ring *rx_ring)
  229. {
  230. unsigned long size;
  231. u16 i;
  232. if (!rx_ring->rx_buffer)
  233. return;
  234. if (rx_ring->skb)
  235. dev_kfree_skb(rx_ring->skb);
  236. rx_ring->skb = NULL;
  237. /* Free all the Rx ring sk_buffs */
  238. for (i = 0; i < rx_ring->count; i++) {
  239. struct fm10k_rx_buffer *buffer = &rx_ring->rx_buffer[i];
  240. /* clean-up will only set page pointer to NULL */
  241. if (!buffer->page)
  242. continue;
  243. dma_unmap_page(rx_ring->dev, buffer->dma,
  244. PAGE_SIZE, DMA_FROM_DEVICE);
  245. __free_page(buffer->page);
  246. buffer->page = NULL;
  247. }
  248. size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
  249. memset(rx_ring->rx_buffer, 0, size);
  250. /* Zero out the descriptor ring */
  251. memset(rx_ring->desc, 0, rx_ring->size);
  252. rx_ring->next_to_alloc = 0;
  253. rx_ring->next_to_clean = 0;
  254. rx_ring->next_to_use = 0;
  255. }
  256. /**
  257. * fm10k_free_rx_resources - Free Rx Resources
  258. * @rx_ring: ring to clean the resources from
  259. *
  260. * Free all receive software resources
  261. **/
  262. void fm10k_free_rx_resources(struct fm10k_ring *rx_ring)
  263. {
  264. fm10k_clean_rx_ring(rx_ring);
  265. vfree(rx_ring->rx_buffer);
  266. rx_ring->rx_buffer = NULL;
  267. /* if not set, then don't free */
  268. if (!rx_ring->desc)
  269. return;
  270. dma_free_coherent(rx_ring->dev, rx_ring->size,
  271. rx_ring->desc, rx_ring->dma);
  272. rx_ring->desc = NULL;
  273. }
  274. /**
  275. * fm10k_clean_all_rx_rings - Free Rx Buffers for all queues
  276. * @interface: board private structure
  277. **/
  278. void fm10k_clean_all_rx_rings(struct fm10k_intfc *interface)
  279. {
  280. int i;
  281. for (i = 0; i < interface->num_rx_queues; i++)
  282. fm10k_clean_rx_ring(interface->rx_ring[i]);
  283. }
  284. /**
  285. * fm10k_free_all_rx_resources - Free Rx Resources for All Queues
  286. * @interface: board private structure
  287. *
  288. * Free all receive software resources
  289. **/
  290. static void fm10k_free_all_rx_resources(struct fm10k_intfc *interface)
  291. {
  292. int i = interface->num_rx_queues;
  293. while (i--)
  294. fm10k_free_rx_resources(interface->rx_ring[i]);
  295. }
  296. /**
  297. * fm10k_request_glort_range - Request GLORTs for use in configuring rules
  298. * @interface: board private structure
  299. *
  300. * This function allocates a range of glorts for this interface to use.
  301. **/
  302. static void fm10k_request_glort_range(struct fm10k_intfc *interface)
  303. {
  304. struct fm10k_hw *hw = &interface->hw;
  305. u16 mask = (~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT;
  306. /* establish GLORT base */
  307. interface->glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
  308. interface->glort_count = 0;
  309. /* nothing we can do until mask is allocated */
  310. if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
  311. return;
  312. /* we support 3 possible GLORT configurations.
  313. * 1: VFs consume all but the last 1
  314. * 2: VFs and PF split glorts with possible gap between
  315. * 3: VFs allocated first 64, all others belong to PF
  316. */
  317. if (mask <= hw->iov.total_vfs) {
  318. interface->glort_count = 1;
  319. interface->glort += mask;
  320. } else if (mask < 64) {
  321. interface->glort_count = (mask + 1) / 2;
  322. interface->glort += interface->glort_count;
  323. } else {
  324. interface->glort_count = mask - 63;
  325. interface->glort += 64;
  326. }
  327. }
  328. /**
  329. * fm10k_del_vxlan_port_all
  330. * @interface: board private structure
  331. *
  332. * This function frees the entire vxlan_port list
  333. **/
  334. static void fm10k_del_vxlan_port_all(struct fm10k_intfc *interface)
  335. {
  336. struct fm10k_vxlan_port *vxlan_port;
  337. /* flush all entries from list */
  338. vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
  339. struct fm10k_vxlan_port, list);
  340. while (vxlan_port) {
  341. list_del(&vxlan_port->list);
  342. kfree(vxlan_port);
  343. vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
  344. struct fm10k_vxlan_port,
  345. list);
  346. }
  347. }
  348. /**
  349. * fm10k_restore_vxlan_port
  350. * @interface: board private structure
  351. *
  352. * This function restores the value in the tunnel_cfg register after reset
  353. **/
  354. static void fm10k_restore_vxlan_port(struct fm10k_intfc *interface)
  355. {
  356. struct fm10k_hw *hw = &interface->hw;
  357. struct fm10k_vxlan_port *vxlan_port;
  358. /* only the PF supports configuring tunnels */
  359. if (hw->mac.type != fm10k_mac_pf)
  360. return;
  361. vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
  362. struct fm10k_vxlan_port, list);
  363. /* restore tunnel configuration register */
  364. fm10k_write_reg(hw, FM10K_TUNNEL_CFG,
  365. (vxlan_port ? ntohs(vxlan_port->port) : 0) |
  366. (ETH_P_TEB << FM10K_TUNNEL_CFG_NVGRE_SHIFT));
  367. }
  368. /**
  369. * fm10k_add_vxlan_port
  370. * @netdev: network interface device structure
  371. * @sa_family: Address family of new port
  372. * @port: port number used for VXLAN
  373. *
  374. * This funciton is called when a new VXLAN interface has added a new port
  375. * number to the range that is currently in use for VXLAN. The new port
  376. * number is always added to the tail so that the port number list should
  377. * match the order in which the ports were allocated. The head of the list
  378. * is always used as the VXLAN port number for offloads.
  379. **/
  380. static void fm10k_add_vxlan_port(struct net_device *dev,
  381. sa_family_t sa_family, __be16 port) {
  382. struct fm10k_intfc *interface = netdev_priv(dev);
  383. struct fm10k_vxlan_port *vxlan_port;
  384. /* only the PF supports configuring tunnels */
  385. if (interface->hw.mac.type != fm10k_mac_pf)
  386. return;
  387. /* existing ports are pulled out so our new entry is always last */
  388. fm10k_vxlan_port_for_each(vxlan_port, interface) {
  389. if ((vxlan_port->port == port) &&
  390. (vxlan_port->sa_family == sa_family)) {
  391. list_del(&vxlan_port->list);
  392. goto insert_tail;
  393. }
  394. }
  395. /* allocate memory to track ports */
  396. vxlan_port = kmalloc(sizeof(*vxlan_port), GFP_ATOMIC);
  397. if (!vxlan_port)
  398. return;
  399. vxlan_port->port = port;
  400. vxlan_port->sa_family = sa_family;
  401. insert_tail:
  402. /* add new port value to list */
  403. list_add_tail(&vxlan_port->list, &interface->vxlan_port);
  404. fm10k_restore_vxlan_port(interface);
  405. }
  406. /**
  407. * fm10k_del_vxlan_port
  408. * @netdev: network interface device structure
  409. * @sa_family: Address family of freed port
  410. * @port: port number used for VXLAN
  411. *
  412. * This funciton is called when a new VXLAN interface has freed a port
  413. * number from the range that is currently in use for VXLAN. The freed
  414. * port is removed from the list and the new head is used to determine
  415. * the port number for offloads.
  416. **/
  417. static void fm10k_del_vxlan_port(struct net_device *dev,
  418. sa_family_t sa_family, __be16 port) {
  419. struct fm10k_intfc *interface = netdev_priv(dev);
  420. struct fm10k_vxlan_port *vxlan_port;
  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 == port) &&
  426. (vxlan_port->sa_family == 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. #if IS_ENABLED(CONFIG_FM10K_VXLAN)
  474. /* update VXLAN port configuration */
  475. vxlan_get_rx_port(netdev);
  476. #endif
  477. fm10k_up(interface);
  478. return 0;
  479. err_set_queues:
  480. fm10k_qv_free_irq(interface);
  481. err_req_irq:
  482. fm10k_free_all_rx_resources(interface);
  483. err_setup_rx:
  484. fm10k_free_all_tx_resources(interface);
  485. err_setup_tx:
  486. return err;
  487. }
  488. /**
  489. * fm10k_close - Disables a network interface
  490. * @netdev: network interface device structure
  491. *
  492. * Returns 0, this is not allowed to fail
  493. *
  494. * The close entry point is called when an interface is de-activated
  495. * by the OS. The hardware is still under the drivers control, but
  496. * needs to be disabled. A global MAC reset is issued to stop the
  497. * hardware, and all transmit and receive resources are freed.
  498. **/
  499. int fm10k_close(struct net_device *netdev)
  500. {
  501. struct fm10k_intfc *interface = netdev_priv(netdev);
  502. fm10k_down(interface);
  503. fm10k_qv_free_irq(interface);
  504. fm10k_del_vxlan_port_all(interface);
  505. fm10k_free_all_tx_resources(interface);
  506. fm10k_free_all_rx_resources(interface);
  507. return 0;
  508. }
  509. static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev)
  510. {
  511. struct fm10k_intfc *interface = netdev_priv(dev);
  512. unsigned int r_idx = skb->queue_mapping;
  513. int err;
  514. if ((skb->protocol == htons(ETH_P_8021Q)) &&
  515. !skb_vlan_tag_present(skb)) {
  516. /* FM10K only supports hardware tagging, any tags in frame
  517. * are considered 2nd level or "outer" tags
  518. */
  519. struct vlan_hdr *vhdr;
  520. __be16 proto;
  521. /* make sure skb is not shared */
  522. skb = skb_share_check(skb, GFP_ATOMIC);
  523. if (!skb)
  524. return NETDEV_TX_OK;
  525. /* make sure there is enough room to move the ethernet header */
  526. if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
  527. return NETDEV_TX_OK;
  528. /* verify the skb head is not shared */
  529. err = skb_cow_head(skb, 0);
  530. if (err) {
  531. dev_kfree_skb(skb);
  532. return NETDEV_TX_OK;
  533. }
  534. /* locate vlan header */
  535. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  536. /* pull the 2 key pieces of data out of it */
  537. __vlan_hwaccel_put_tag(skb,
  538. htons(ETH_P_8021Q),
  539. ntohs(vhdr->h_vlan_TCI));
  540. proto = vhdr->h_vlan_encapsulated_proto;
  541. skb->protocol = (ntohs(proto) >= 1536) ? proto :
  542. htons(ETH_P_802_2);
  543. /* squash it by moving the ethernet addresses up 4 bytes */
  544. memmove(skb->data + VLAN_HLEN, skb->data, 12);
  545. __skb_pull(skb, VLAN_HLEN);
  546. skb_reset_mac_header(skb);
  547. }
  548. /* The minimum packet size for a single buffer is 17B so pad the skb
  549. * in order to meet this minimum size requirement.
  550. */
  551. if (unlikely(skb->len < 17)) {
  552. int pad_len = 17 - skb->len;
  553. if (skb_pad(skb, pad_len))
  554. return NETDEV_TX_OK;
  555. __skb_put(skb, pad_len);
  556. }
  557. /* prepare packet for hardware time stamping */
  558. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
  559. fm10k_ts_tx_enqueue(interface, skb);
  560. if (r_idx >= interface->num_tx_queues)
  561. r_idx %= interface->num_tx_queues;
  562. err = fm10k_xmit_frame_ring(skb, interface->tx_ring[r_idx]);
  563. return err;
  564. }
  565. static int fm10k_change_mtu(struct net_device *dev, int new_mtu)
  566. {
  567. if (new_mtu < 68 || new_mtu > FM10K_MAX_JUMBO_FRAME_SIZE)
  568. return -EINVAL;
  569. dev->mtu = new_mtu;
  570. return 0;
  571. }
  572. /**
  573. * fm10k_tx_timeout - Respond to a Tx Hang
  574. * @netdev: network interface device structure
  575. **/
  576. static void fm10k_tx_timeout(struct net_device *netdev)
  577. {
  578. struct fm10k_intfc *interface = netdev_priv(netdev);
  579. bool real_tx_hang = false;
  580. int i;
  581. #define TX_TIMEO_LIMIT 16000
  582. for (i = 0; i < interface->num_tx_queues; i++) {
  583. struct fm10k_ring *tx_ring = interface->tx_ring[i];
  584. if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring))
  585. real_tx_hang = true;
  586. }
  587. if (real_tx_hang) {
  588. fm10k_tx_timeout_reset(interface);
  589. } else {
  590. netif_info(interface, drv, netdev,
  591. "Fake Tx hang detected with timeout of %d seconds\n",
  592. netdev->watchdog_timeo/HZ);
  593. /* fake Tx hang - increase the kernel timeout */
  594. if (netdev->watchdog_timeo < TX_TIMEO_LIMIT)
  595. netdev->watchdog_timeo *= 2;
  596. }
  597. }
  598. static int fm10k_uc_vlan_unsync(struct net_device *netdev,
  599. const unsigned char *uc_addr)
  600. {
  601. struct fm10k_intfc *interface = netdev_priv(netdev);
  602. struct fm10k_hw *hw = &interface->hw;
  603. u16 glort = interface->glort;
  604. u16 vid = interface->vid;
  605. bool set = !!(vid / VLAN_N_VID);
  606. int err;
  607. /* drop any leading bits on the VLAN ID */
  608. vid &= VLAN_N_VID - 1;
  609. err = hw->mac.ops.update_uc_addr(hw, glort, uc_addr, vid, set, 0);
  610. if (err)
  611. return err;
  612. /* return non-zero value as we are only doing a partial sync/unsync */
  613. return 1;
  614. }
  615. static int fm10k_mc_vlan_unsync(struct net_device *netdev,
  616. const unsigned char *mc_addr)
  617. {
  618. struct fm10k_intfc *interface = netdev_priv(netdev);
  619. struct fm10k_hw *hw = &interface->hw;
  620. u16 glort = interface->glort;
  621. u16 vid = interface->vid;
  622. bool set = !!(vid / VLAN_N_VID);
  623. int err;
  624. /* drop any leading bits on the VLAN ID */
  625. vid &= VLAN_N_VID - 1;
  626. err = hw->mac.ops.update_mc_addr(hw, glort, mc_addr, vid, set);
  627. if (err)
  628. return err;
  629. /* return non-zero value as we are only doing a partial sync/unsync */
  630. return 1;
  631. }
  632. static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
  633. {
  634. struct fm10k_intfc *interface = netdev_priv(netdev);
  635. struct fm10k_hw *hw = &interface->hw;
  636. s32 err;
  637. int i;
  638. /* updates do not apply to VLAN 0 */
  639. if (!vid)
  640. return 0;
  641. if (vid >= VLAN_N_VID)
  642. return -EINVAL;
  643. /* Verify we have permission to add VLANs */
  644. if (hw->mac.vlan_override)
  645. return -EACCES;
  646. /* update active_vlans bitmask */
  647. set_bit(vid, interface->active_vlans);
  648. if (!set)
  649. clear_bit(vid, interface->active_vlans);
  650. /* disable the default VID on ring if we have an active VLAN */
  651. for (i = 0; i < interface->num_rx_queues; i++) {
  652. struct fm10k_ring *rx_ring = interface->rx_ring[i];
  653. u16 rx_vid = rx_ring->vid & (VLAN_N_VID - 1);
  654. if (test_bit(rx_vid, interface->active_vlans))
  655. rx_ring->vid |= FM10K_VLAN_CLEAR;
  656. else
  657. rx_ring->vid &= ~FM10K_VLAN_CLEAR;
  658. }
  659. /* Do not remove default VID related entries from VLAN and MAC tables */
  660. if (!set && vid == hw->mac.default_vid)
  661. return 0;
  662. /* Do not throw an error if the interface is down. We will sync once
  663. * we come up
  664. */
  665. if (test_bit(__FM10K_DOWN, &interface->state))
  666. return 0;
  667. fm10k_mbx_lock(interface);
  668. /* only need to update the VLAN if not in promiscuous mode */
  669. if (!(netdev->flags & IFF_PROMISC)) {
  670. err = hw->mac.ops.update_vlan(hw, vid, 0, set);
  671. if (err)
  672. goto err_out;
  673. }
  674. /* update our base MAC address */
  675. err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr,
  676. vid, set, 0);
  677. if (err)
  678. goto err_out;
  679. /* set vid prior to syncing/unsyncing the VLAN */
  680. interface->vid = vid + (set ? VLAN_N_VID : 0);
  681. /* Update the unicast and multicast address list to add/drop VLAN */
  682. __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
  683. __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
  684. err_out:
  685. fm10k_mbx_unlock(interface);
  686. return err;
  687. }
  688. static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
  689. __always_unused __be16 proto, u16 vid)
  690. {
  691. /* update VLAN and address table based on changes */
  692. return fm10k_update_vid(netdev, vid, true);
  693. }
  694. static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
  695. __always_unused __be16 proto, u16 vid)
  696. {
  697. /* update VLAN and address table based on changes */
  698. return fm10k_update_vid(netdev, vid, false);
  699. }
  700. static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
  701. {
  702. struct fm10k_hw *hw = &interface->hw;
  703. u16 default_vid = hw->mac.default_vid;
  704. u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
  705. vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
  706. return vid;
  707. }
  708. static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
  709. {
  710. struct fm10k_hw *hw = &interface->hw;
  711. u32 vid, prev_vid;
  712. /* loop through and find any gaps in the table */
  713. for (vid = 0, prev_vid = 0;
  714. prev_vid < VLAN_N_VID;
  715. prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
  716. if (prev_vid == vid)
  717. continue;
  718. /* send request to clear multiple bits at a time */
  719. prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
  720. hw->mac.ops.update_vlan(hw, prev_vid, 0, false);
  721. }
  722. }
  723. static int __fm10k_uc_sync(struct net_device *dev,
  724. const unsigned char *addr, bool sync)
  725. {
  726. struct fm10k_intfc *interface = netdev_priv(dev);
  727. struct fm10k_hw *hw = &interface->hw;
  728. u16 vid, glort = interface->glort;
  729. s32 err;
  730. if (!is_valid_ether_addr(addr))
  731. return -EADDRNOTAVAIL;
  732. /* update table with current entries */
  733. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
  734. vid < VLAN_N_VID;
  735. vid = fm10k_find_next_vlan(interface, vid)) {
  736. err = hw->mac.ops.update_uc_addr(hw, glort, addr,
  737. vid, sync, 0);
  738. if (err)
  739. return err;
  740. }
  741. return 0;
  742. }
  743. static int fm10k_uc_sync(struct net_device *dev,
  744. const unsigned char *addr)
  745. {
  746. return __fm10k_uc_sync(dev, addr, true);
  747. }
  748. static int fm10k_uc_unsync(struct net_device *dev,
  749. const unsigned char *addr)
  750. {
  751. return __fm10k_uc_sync(dev, addr, false);
  752. }
  753. static int fm10k_set_mac(struct net_device *dev, void *p)
  754. {
  755. struct fm10k_intfc *interface = netdev_priv(dev);
  756. struct fm10k_hw *hw = &interface->hw;
  757. struct sockaddr *addr = p;
  758. s32 err = 0;
  759. if (!is_valid_ether_addr(addr->sa_data))
  760. return -EADDRNOTAVAIL;
  761. if (dev->flags & IFF_UP) {
  762. /* setting MAC address requires mailbox */
  763. fm10k_mbx_lock(interface);
  764. err = fm10k_uc_sync(dev, addr->sa_data);
  765. if (!err)
  766. fm10k_uc_unsync(dev, hw->mac.addr);
  767. fm10k_mbx_unlock(interface);
  768. }
  769. if (!err) {
  770. ether_addr_copy(dev->dev_addr, addr->sa_data);
  771. ether_addr_copy(hw->mac.addr, addr->sa_data);
  772. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  773. }
  774. /* if we had a mailbox error suggest trying again */
  775. return err ? -EAGAIN : 0;
  776. }
  777. static int __fm10k_mc_sync(struct net_device *dev,
  778. const unsigned char *addr, bool sync)
  779. {
  780. struct fm10k_intfc *interface = netdev_priv(dev);
  781. struct fm10k_hw *hw = &interface->hw;
  782. u16 vid, glort = interface->glort;
  783. /* update table with current entries */
  784. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
  785. vid < VLAN_N_VID;
  786. vid = fm10k_find_next_vlan(interface, vid)) {
  787. hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync);
  788. }
  789. return 0;
  790. }
  791. static int fm10k_mc_sync(struct net_device *dev,
  792. const unsigned char *addr)
  793. {
  794. return __fm10k_mc_sync(dev, addr, true);
  795. }
  796. static int fm10k_mc_unsync(struct net_device *dev,
  797. const unsigned char *addr)
  798. {
  799. return __fm10k_mc_sync(dev, addr, false);
  800. }
  801. static void fm10k_set_rx_mode(struct net_device *dev)
  802. {
  803. struct fm10k_intfc *interface = netdev_priv(dev);
  804. struct fm10k_hw *hw = &interface->hw;
  805. int xcast_mode;
  806. /* no need to update the harwdare if we are not running */
  807. if (!(dev->flags & IFF_UP))
  808. return;
  809. /* determine new mode based on flags */
  810. xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
  811. (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
  812. (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
  813. FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
  814. fm10k_mbx_lock(interface);
  815. /* update xcast mode first, but only if it changed */
  816. if (interface->xcast_mode != xcast_mode) {
  817. /* update VLAN table */
  818. if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
  819. hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true);
  820. if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
  821. fm10k_clear_unused_vlans(interface);
  822. /* update xcast mode */
  823. hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode);
  824. /* record updated xcast mode state */
  825. interface->xcast_mode = xcast_mode;
  826. }
  827. /* synchronize all of the addresses */
  828. if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
  829. __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
  830. if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
  831. __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
  832. }
  833. fm10k_mbx_unlock(interface);
  834. }
  835. void fm10k_restore_rx_state(struct fm10k_intfc *interface)
  836. {
  837. struct net_device *netdev = interface->netdev;
  838. struct fm10k_hw *hw = &interface->hw;
  839. int xcast_mode;
  840. u16 vid, glort;
  841. /* record glort for this interface */
  842. glort = interface->glort;
  843. /* convert interface flags to xcast mode */
  844. if (netdev->flags & IFF_PROMISC)
  845. xcast_mode = FM10K_XCAST_MODE_PROMISC;
  846. else if (netdev->flags & IFF_ALLMULTI)
  847. xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
  848. else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
  849. xcast_mode = FM10K_XCAST_MODE_MULTI;
  850. else
  851. xcast_mode = FM10K_XCAST_MODE_NONE;
  852. fm10k_mbx_lock(interface);
  853. /* Enable logical port */
  854. hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true);
  855. /* update VLAN table */
  856. hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0,
  857. xcast_mode == FM10K_XCAST_MODE_PROMISC);
  858. /* Add filter for VLAN 0 */
  859. hw->mac.ops.update_vlan(hw, 0, 0, true);
  860. /* update table with current entries */
  861. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
  862. vid < VLAN_N_VID;
  863. vid = fm10k_find_next_vlan(interface, vid)) {
  864. hw->mac.ops.update_vlan(hw, vid, 0, true);
  865. hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr,
  866. vid, true, 0);
  867. }
  868. /* update xcast mode before synchronizing addresses */
  869. hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
  870. /* synchronize all of the addresses */
  871. if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
  872. __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
  873. if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
  874. __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
  875. }
  876. fm10k_mbx_unlock(interface);
  877. /* record updated xcast mode state */
  878. interface->xcast_mode = xcast_mode;
  879. /* Restore tunnel configuration */
  880. fm10k_restore_vxlan_port(interface);
  881. }
  882. void fm10k_reset_rx_state(struct fm10k_intfc *interface)
  883. {
  884. struct net_device *netdev = interface->netdev;
  885. struct fm10k_hw *hw = &interface->hw;
  886. fm10k_mbx_lock(interface);
  887. /* clear the logical port state on lower device */
  888. hw->mac.ops.update_lport_state(hw, interface->glort,
  889. interface->glort_count, false);
  890. fm10k_mbx_unlock(interface);
  891. /* reset flags to default state */
  892. interface->xcast_mode = FM10K_XCAST_MODE_NONE;
  893. /* clear the sync flag since the lport has been dropped */
  894. __dev_uc_unsync(netdev, NULL);
  895. __dev_mc_unsync(netdev, NULL);
  896. }
  897. /**
  898. * fm10k_get_stats64 - Get System Network Statistics
  899. * @netdev: network interface device structure
  900. * @stats: storage space for 64bit statistics
  901. *
  902. * Returns 64bit statistics, for use in the ndo_get_stats64 callback. This
  903. * function replaces fm10k_get_stats for kernels which support it.
  904. */
  905. static struct rtnl_link_stats64 *fm10k_get_stats64(struct net_device *netdev,
  906. struct rtnl_link_stats64 *stats)
  907. {
  908. struct fm10k_intfc *interface = netdev_priv(netdev);
  909. struct fm10k_ring *ring;
  910. unsigned int start, i;
  911. u64 bytes, packets;
  912. rcu_read_lock();
  913. for (i = 0; i < interface->num_rx_queues; i++) {
  914. ring = ACCESS_ONCE(interface->rx_ring[i]);
  915. if (!ring)
  916. continue;
  917. do {
  918. start = u64_stats_fetch_begin_irq(&ring->syncp);
  919. packets = ring->stats.packets;
  920. bytes = ring->stats.bytes;
  921. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  922. stats->rx_packets += packets;
  923. stats->rx_bytes += bytes;
  924. }
  925. for (i = 0; i < interface->num_tx_queues; i++) {
  926. ring = ACCESS_ONCE(interface->tx_ring[i]);
  927. if (!ring)
  928. continue;
  929. do {
  930. start = u64_stats_fetch_begin_irq(&ring->syncp);
  931. packets = ring->stats.packets;
  932. bytes = ring->stats.bytes;
  933. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  934. stats->tx_packets += packets;
  935. stats->tx_bytes += bytes;
  936. }
  937. rcu_read_unlock();
  938. /* following stats updated by fm10k_service_task() */
  939. stats->rx_missed_errors = netdev->stats.rx_missed_errors;
  940. return stats;
  941. }
  942. int fm10k_setup_tc(struct net_device *dev, u8 tc)
  943. {
  944. struct fm10k_intfc *interface = netdev_priv(dev);
  945. /* Currently only the PF supports priority classes */
  946. if (tc && (interface->hw.mac.type != fm10k_mac_pf))
  947. return -EINVAL;
  948. /* Hardware supports up to 8 traffic classes */
  949. if (tc > 8)
  950. return -EINVAL;
  951. /* Hardware has to reinitialize queues to match packet
  952. * buffer alignment. Unfortunately, the hardware is not
  953. * flexible enough to do this dynamically.
  954. */
  955. if (netif_running(dev))
  956. fm10k_close(dev);
  957. fm10k_mbx_free_irq(interface);
  958. fm10k_clear_queueing_scheme(interface);
  959. /* we expect the prio_tc map to be repopulated later */
  960. netdev_reset_tc(dev);
  961. netdev_set_num_tc(dev, tc);
  962. fm10k_init_queueing_scheme(interface);
  963. fm10k_mbx_request_irq(interface);
  964. if (netif_running(dev))
  965. fm10k_open(dev);
  966. /* flag to indicate SWPRI has yet to be updated */
  967. interface->flags |= FM10K_FLAG_SWPRI_CONFIG;
  968. return 0;
  969. }
  970. static int fm10k_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  971. {
  972. switch (cmd) {
  973. case SIOCGHWTSTAMP:
  974. return fm10k_get_ts_config(netdev, ifr);
  975. case SIOCSHWTSTAMP:
  976. return fm10k_set_ts_config(netdev, ifr);
  977. default:
  978. return -EOPNOTSUPP;
  979. }
  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_ALL_CSUM | 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_add_vxlan_port = fm10k_add_vxlan_port,
  1127. .ndo_del_vxlan_port = fm10k_del_vxlan_port,
  1128. .ndo_do_ioctl = fm10k_ioctl,
  1129. .ndo_dfwd_add_station = fm10k_dfwd_add_station,
  1130. .ndo_dfwd_del_station = fm10k_dfwd_del_station,
  1131. #ifdef CONFIG_NET_POLL_CONTROLLER
  1132. .ndo_poll_controller = fm10k_netpoll,
  1133. #endif
  1134. .ndo_features_check = fm10k_features_check,
  1135. };
  1136. #define DEFAULT_DEBUG_LEVEL_SHIFT 3
  1137. struct net_device *fm10k_alloc_netdev(void)
  1138. {
  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 = (1 << 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_GSO_UDP_TUNNEL |
  1158. NETIF_F_RXHASH |
  1159. NETIF_F_RXCSUM;
  1160. /* all features defined to this point should be changeable */
  1161. dev->hw_features |= dev->features;
  1162. /* allow user to enable L2 forwarding acceleration */
  1163. dev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
  1164. /* configure VLAN features */
  1165. dev->vlan_features |= dev->features;
  1166. /* configure tunnel offloads */
  1167. dev->hw_enc_features |= NETIF_F_IP_CSUM |
  1168. NETIF_F_TSO |
  1169. NETIF_F_TSO6 |
  1170. NETIF_F_TSO_ECN |
  1171. NETIF_F_GSO_UDP_TUNNEL |
  1172. NETIF_F_IPV6_CSUM;
  1173. /* we want to leave these both on as we cannot disable VLAN tag
  1174. * insertion or stripping on the hardware since it is contained
  1175. * in the FTAG and not in the frame itself.
  1176. */
  1177. dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
  1178. NETIF_F_HW_VLAN_CTAG_RX |
  1179. NETIF_F_HW_VLAN_CTAG_FILTER;
  1180. dev->priv_flags |= IFF_UNICAST_FLT;
  1181. return dev;
  1182. }