fm10k_netdev.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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. #ifdef 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. #ifdef 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 VLAN ID 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 VLAN ID related entries from VLAN and MAC
  660. * tables
  661. */
  662. if (!set && vid == hw->mac.default_vid)
  663. return 0;
  664. /* Do not throw an error if the interface is down. We will sync once
  665. * we come up
  666. */
  667. if (test_bit(__FM10K_DOWN, &interface->state))
  668. return 0;
  669. fm10k_mbx_lock(interface);
  670. /* only need to update the VLAN if not in promiscuous mode */
  671. if (!(netdev->flags & IFF_PROMISC)) {
  672. err = hw->mac.ops.update_vlan(hw, vid, 0, set);
  673. if (err)
  674. goto err_out;
  675. }
  676. /* update our base MAC address */
  677. err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr,
  678. vid, set, 0);
  679. if (err)
  680. goto err_out;
  681. /* set VLAN ID prior to syncing/unsyncing the VLAN */
  682. interface->vid = vid + (set ? VLAN_N_VID : 0);
  683. /* Update the unicast and multicast address list to add/drop VLAN */
  684. __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
  685. __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
  686. err_out:
  687. fm10k_mbx_unlock(interface);
  688. return err;
  689. }
  690. static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
  691. __always_unused __be16 proto, u16 vid)
  692. {
  693. /* update VLAN and address table based on changes */
  694. return fm10k_update_vid(netdev, vid, true);
  695. }
  696. static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
  697. __always_unused __be16 proto, u16 vid)
  698. {
  699. /* update VLAN and address table based on changes */
  700. return fm10k_update_vid(netdev, vid, false);
  701. }
  702. static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
  703. {
  704. struct fm10k_hw *hw = &interface->hw;
  705. u16 default_vid = hw->mac.default_vid;
  706. u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
  707. vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
  708. return vid;
  709. }
  710. static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
  711. {
  712. struct fm10k_hw *hw = &interface->hw;
  713. u32 vid, prev_vid;
  714. /* loop through and find any gaps in the table */
  715. for (vid = 0, prev_vid = 0;
  716. prev_vid < VLAN_N_VID;
  717. prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
  718. if (prev_vid == vid)
  719. continue;
  720. /* send request to clear multiple bits at a time */
  721. prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
  722. hw->mac.ops.update_vlan(hw, prev_vid, 0, false);
  723. }
  724. }
  725. static int __fm10k_uc_sync(struct net_device *dev,
  726. const unsigned char *addr, bool sync)
  727. {
  728. struct fm10k_intfc *interface = netdev_priv(dev);
  729. struct fm10k_hw *hw = &interface->hw;
  730. u16 vid, glort = interface->glort;
  731. s32 err;
  732. if (!is_valid_ether_addr(addr))
  733. return -EADDRNOTAVAIL;
  734. /* update table with current entries */
  735. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
  736. vid < VLAN_N_VID;
  737. vid = fm10k_find_next_vlan(interface, vid)) {
  738. err = hw->mac.ops.update_uc_addr(hw, glort, addr,
  739. vid, sync, 0);
  740. if (err)
  741. return err;
  742. }
  743. return 0;
  744. }
  745. static int fm10k_uc_sync(struct net_device *dev,
  746. const unsigned char *addr)
  747. {
  748. return __fm10k_uc_sync(dev, addr, true);
  749. }
  750. static int fm10k_uc_unsync(struct net_device *dev,
  751. const unsigned char *addr)
  752. {
  753. return __fm10k_uc_sync(dev, addr, false);
  754. }
  755. static int fm10k_set_mac(struct net_device *dev, void *p)
  756. {
  757. struct fm10k_intfc *interface = netdev_priv(dev);
  758. struct fm10k_hw *hw = &interface->hw;
  759. struct sockaddr *addr = p;
  760. s32 err = 0;
  761. if (!is_valid_ether_addr(addr->sa_data))
  762. return -EADDRNOTAVAIL;
  763. if (dev->flags & IFF_UP) {
  764. /* setting MAC address requires mailbox */
  765. fm10k_mbx_lock(interface);
  766. err = fm10k_uc_sync(dev, addr->sa_data);
  767. if (!err)
  768. fm10k_uc_unsync(dev, hw->mac.addr);
  769. fm10k_mbx_unlock(interface);
  770. }
  771. if (!err) {
  772. ether_addr_copy(dev->dev_addr, addr->sa_data);
  773. ether_addr_copy(hw->mac.addr, addr->sa_data);
  774. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  775. }
  776. /* if we had a mailbox error suggest trying again */
  777. return err ? -EAGAIN : 0;
  778. }
  779. static int __fm10k_mc_sync(struct net_device *dev,
  780. const unsigned char *addr, bool sync)
  781. {
  782. struct fm10k_intfc *interface = netdev_priv(dev);
  783. struct fm10k_hw *hw = &interface->hw;
  784. u16 vid, glort = interface->glort;
  785. /* update table with current entries */
  786. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
  787. vid < VLAN_N_VID;
  788. vid = fm10k_find_next_vlan(interface, vid)) {
  789. hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync);
  790. }
  791. return 0;
  792. }
  793. static int fm10k_mc_sync(struct net_device *dev,
  794. const unsigned char *addr)
  795. {
  796. return __fm10k_mc_sync(dev, addr, true);
  797. }
  798. static int fm10k_mc_unsync(struct net_device *dev,
  799. const unsigned char *addr)
  800. {
  801. return __fm10k_mc_sync(dev, addr, false);
  802. }
  803. static void fm10k_set_rx_mode(struct net_device *dev)
  804. {
  805. struct fm10k_intfc *interface = netdev_priv(dev);
  806. struct fm10k_hw *hw = &interface->hw;
  807. int xcast_mode;
  808. /* no need to update the harwdare if we are not running */
  809. if (!(dev->flags & IFF_UP))
  810. return;
  811. /* determine new mode based on flags */
  812. xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
  813. (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
  814. (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
  815. FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
  816. fm10k_mbx_lock(interface);
  817. /* update xcast mode first, but only if it changed */
  818. if (interface->xcast_mode != xcast_mode) {
  819. /* update VLAN table */
  820. if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
  821. hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true);
  822. if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
  823. fm10k_clear_unused_vlans(interface);
  824. /* update xcast mode */
  825. hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode);
  826. /* record updated xcast mode state */
  827. interface->xcast_mode = xcast_mode;
  828. }
  829. /* synchronize all of the addresses */
  830. if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
  831. __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
  832. if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
  833. __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
  834. }
  835. fm10k_mbx_unlock(interface);
  836. }
  837. void fm10k_restore_rx_state(struct fm10k_intfc *interface)
  838. {
  839. struct net_device *netdev = interface->netdev;
  840. struct fm10k_hw *hw = &interface->hw;
  841. int xcast_mode;
  842. u16 vid, glort;
  843. /* record glort for this interface */
  844. glort = interface->glort;
  845. /* convert interface flags to xcast mode */
  846. if (netdev->flags & IFF_PROMISC)
  847. xcast_mode = FM10K_XCAST_MODE_PROMISC;
  848. else if (netdev->flags & IFF_ALLMULTI)
  849. xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
  850. else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
  851. xcast_mode = FM10K_XCAST_MODE_MULTI;
  852. else
  853. xcast_mode = FM10K_XCAST_MODE_NONE;
  854. fm10k_mbx_lock(interface);
  855. /* Enable logical port */
  856. hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true);
  857. /* update VLAN table */
  858. hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0,
  859. xcast_mode == FM10K_XCAST_MODE_PROMISC);
  860. /* Add filter for VLAN 0 */
  861. hw->mac.ops.update_vlan(hw, 0, 0, true);
  862. /* update table with current entries */
  863. for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
  864. vid < VLAN_N_VID;
  865. vid = fm10k_find_next_vlan(interface, vid)) {
  866. hw->mac.ops.update_vlan(hw, vid, 0, true);
  867. hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr,
  868. vid, true, 0);
  869. }
  870. /* update xcast mode before synchronizing addresses */
  871. hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
  872. /* synchronize all of the addresses */
  873. if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
  874. __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
  875. if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
  876. __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
  877. }
  878. fm10k_mbx_unlock(interface);
  879. /* record updated xcast mode state */
  880. interface->xcast_mode = xcast_mode;
  881. /* Restore tunnel configuration */
  882. fm10k_restore_vxlan_port(interface);
  883. }
  884. void fm10k_reset_rx_state(struct fm10k_intfc *interface)
  885. {
  886. struct net_device *netdev = interface->netdev;
  887. struct fm10k_hw *hw = &interface->hw;
  888. fm10k_mbx_lock(interface);
  889. /* clear the logical port state on lower device */
  890. hw->mac.ops.update_lport_state(hw, interface->glort,
  891. interface->glort_count, false);
  892. fm10k_mbx_unlock(interface);
  893. /* reset flags to default state */
  894. interface->xcast_mode = FM10K_XCAST_MODE_NONE;
  895. /* clear the sync flag since the lport has been dropped */
  896. __dev_uc_unsync(netdev, NULL);
  897. __dev_mc_unsync(netdev, NULL);
  898. }
  899. /**
  900. * fm10k_get_stats64 - Get System Network Statistics
  901. * @netdev: network interface device structure
  902. * @stats: storage space for 64bit statistics
  903. *
  904. * Returns 64bit statistics, for use in the ndo_get_stats64 callback. This
  905. * function replaces fm10k_get_stats for kernels which support it.
  906. */
  907. static struct rtnl_link_stats64 *fm10k_get_stats64(struct net_device *netdev,
  908. struct rtnl_link_stats64 *stats)
  909. {
  910. struct fm10k_intfc *interface = netdev_priv(netdev);
  911. struct fm10k_ring *ring;
  912. unsigned int start, i;
  913. u64 bytes, packets;
  914. rcu_read_lock();
  915. for (i = 0; i < interface->num_rx_queues; i++) {
  916. ring = ACCESS_ONCE(interface->rx_ring[i]);
  917. if (!ring)
  918. continue;
  919. do {
  920. start = u64_stats_fetch_begin_irq(&ring->syncp);
  921. packets = ring->stats.packets;
  922. bytes = ring->stats.bytes;
  923. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  924. stats->rx_packets += packets;
  925. stats->rx_bytes += bytes;
  926. }
  927. for (i = 0; i < interface->num_tx_queues; i++) {
  928. ring = ACCESS_ONCE(interface->tx_ring[i]);
  929. if (!ring)
  930. continue;
  931. do {
  932. start = u64_stats_fetch_begin_irq(&ring->syncp);
  933. packets = ring->stats.packets;
  934. bytes = ring->stats.bytes;
  935. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  936. stats->tx_packets += packets;
  937. stats->tx_bytes += bytes;
  938. }
  939. rcu_read_unlock();
  940. /* following stats updated by fm10k_service_task() */
  941. stats->rx_missed_errors = netdev->stats.rx_missed_errors;
  942. return stats;
  943. }
  944. int fm10k_setup_tc(struct net_device *dev, u8 tc)
  945. {
  946. struct fm10k_intfc *interface = netdev_priv(dev);
  947. int err;
  948. /* Currently only the PF supports priority classes */
  949. if (tc && (interface->hw.mac.type != fm10k_mac_pf))
  950. return -EINVAL;
  951. /* Hardware supports up to 8 traffic classes */
  952. if (tc > 8)
  953. return -EINVAL;
  954. /* Hardware has to reinitialize queues to match packet
  955. * buffer alignment. Unfortunately, the hardware is not
  956. * flexible enough to do this dynamically.
  957. */
  958. if (netif_running(dev))
  959. fm10k_close(dev);
  960. fm10k_mbx_free_irq(interface);
  961. fm10k_clear_queueing_scheme(interface);
  962. /* we expect the prio_tc map to be repopulated later */
  963. netdev_reset_tc(dev);
  964. netdev_set_num_tc(dev, tc);
  965. err = fm10k_init_queueing_scheme(interface);
  966. if (err)
  967. goto err_queueing_scheme;
  968. err = fm10k_mbx_request_irq(interface);
  969. if (err)
  970. goto err_mbx_irq;
  971. err = netif_running(dev) ? fm10k_open(dev) : 0;
  972. if (err)
  973. goto err_open;
  974. /* flag to indicate SWPRI has yet to be updated */
  975. interface->flags |= FM10K_FLAG_SWPRI_CONFIG;
  976. return 0;
  977. err_open:
  978. fm10k_mbx_free_irq(interface);
  979. err_mbx_irq:
  980. fm10k_clear_queueing_scheme(interface);
  981. err_queueing_scheme:
  982. netif_device_detach(dev);
  983. return err;
  984. }
  985. static int __fm10k_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
  986. struct tc_to_netdev *tc)
  987. {
  988. if (tc->type != TC_SETUP_MQPRIO)
  989. return -EINVAL;
  990. return fm10k_setup_tc(dev, tc->tc);
  991. }
  992. static int fm10k_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  993. {
  994. switch (cmd) {
  995. case SIOCGHWTSTAMP:
  996. return fm10k_get_ts_config(netdev, ifr);
  997. case SIOCSHWTSTAMP:
  998. return fm10k_set_ts_config(netdev, ifr);
  999. default:
  1000. return -EOPNOTSUPP;
  1001. }
  1002. }
  1003. static void fm10k_assign_l2_accel(struct fm10k_intfc *interface,
  1004. struct fm10k_l2_accel *l2_accel)
  1005. {
  1006. struct fm10k_ring *ring;
  1007. int i;
  1008. for (i = 0; i < interface->num_rx_queues; i++) {
  1009. ring = interface->rx_ring[i];
  1010. rcu_assign_pointer(ring->l2_accel, l2_accel);
  1011. }
  1012. interface->l2_accel = l2_accel;
  1013. }
  1014. static void *fm10k_dfwd_add_station(struct net_device *dev,
  1015. struct net_device *sdev)
  1016. {
  1017. struct fm10k_intfc *interface = netdev_priv(dev);
  1018. struct fm10k_l2_accel *l2_accel = interface->l2_accel;
  1019. struct fm10k_l2_accel *old_l2_accel = NULL;
  1020. struct fm10k_dglort_cfg dglort = { 0 };
  1021. struct fm10k_hw *hw = &interface->hw;
  1022. int size = 0, i;
  1023. u16 glort;
  1024. /* allocate l2 accel structure if it is not available */
  1025. if (!l2_accel) {
  1026. /* verify there is enough free GLORTs to support l2_accel */
  1027. if (interface->glort_count < 7)
  1028. return ERR_PTR(-EBUSY);
  1029. size = offsetof(struct fm10k_l2_accel, macvlan[7]);
  1030. l2_accel = kzalloc(size, GFP_KERNEL);
  1031. if (!l2_accel)
  1032. return ERR_PTR(-ENOMEM);
  1033. l2_accel->size = 7;
  1034. l2_accel->dglort = interface->glort;
  1035. /* update pointers */
  1036. fm10k_assign_l2_accel(interface, l2_accel);
  1037. /* do not expand if we are at our limit */
  1038. } else if ((l2_accel->count == FM10K_MAX_STATIONS) ||
  1039. (l2_accel->count == (interface->glort_count - 1))) {
  1040. return ERR_PTR(-EBUSY);
  1041. /* expand if we have hit the size limit */
  1042. } else if (l2_accel->count == l2_accel->size) {
  1043. old_l2_accel = l2_accel;
  1044. size = offsetof(struct fm10k_l2_accel,
  1045. macvlan[(l2_accel->size * 2) + 1]);
  1046. l2_accel = kzalloc(size, GFP_KERNEL);
  1047. if (!l2_accel)
  1048. return ERR_PTR(-ENOMEM);
  1049. memcpy(l2_accel, old_l2_accel,
  1050. offsetof(struct fm10k_l2_accel,
  1051. macvlan[old_l2_accel->size]));
  1052. l2_accel->size = (old_l2_accel->size * 2) + 1;
  1053. /* update pointers */
  1054. fm10k_assign_l2_accel(interface, l2_accel);
  1055. kfree_rcu(old_l2_accel, rcu);
  1056. }
  1057. /* add macvlan to accel table, and record GLORT for position */
  1058. for (i = 0; i < l2_accel->size; i++) {
  1059. if (!l2_accel->macvlan[i])
  1060. break;
  1061. }
  1062. /* record station */
  1063. l2_accel->macvlan[i] = sdev;
  1064. l2_accel->count++;
  1065. /* configure default DGLORT mapping for RSS/DCB */
  1066. dglort.idx = fm10k_dglort_pf_rss;
  1067. dglort.inner_rss = 1;
  1068. dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
  1069. dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
  1070. dglort.glort = interface->glort;
  1071. dglort.shared_l = fls(l2_accel->size);
  1072. hw->mac.ops.configure_dglort_map(hw, &dglort);
  1073. /* Add rules for this specific dglort to the switch */
  1074. fm10k_mbx_lock(interface);
  1075. glort = l2_accel->dglort + 1 + i;
  1076. hw->mac.ops.update_xcast_mode(hw, glort, FM10K_XCAST_MODE_MULTI);
  1077. hw->mac.ops.update_uc_addr(hw, glort, sdev->dev_addr, 0, true, 0);
  1078. fm10k_mbx_unlock(interface);
  1079. return sdev;
  1080. }
  1081. static void fm10k_dfwd_del_station(struct net_device *dev, void *priv)
  1082. {
  1083. struct fm10k_intfc *interface = netdev_priv(dev);
  1084. struct fm10k_l2_accel *l2_accel = ACCESS_ONCE(interface->l2_accel);
  1085. struct fm10k_dglort_cfg dglort = { 0 };
  1086. struct fm10k_hw *hw = &interface->hw;
  1087. struct net_device *sdev = priv;
  1088. int i;
  1089. u16 glort;
  1090. if (!l2_accel)
  1091. return;
  1092. /* search table for matching interface */
  1093. for (i = 0; i < l2_accel->size; i++) {
  1094. if (l2_accel->macvlan[i] == sdev)
  1095. break;
  1096. }
  1097. /* exit if macvlan not found */
  1098. if (i == l2_accel->size)
  1099. return;
  1100. /* Remove any rules specific to this dglort */
  1101. fm10k_mbx_lock(interface);
  1102. glort = l2_accel->dglort + 1 + i;
  1103. hw->mac.ops.update_xcast_mode(hw, glort, FM10K_XCAST_MODE_NONE);
  1104. hw->mac.ops.update_uc_addr(hw, glort, sdev->dev_addr, 0, false, 0);
  1105. fm10k_mbx_unlock(interface);
  1106. /* record removal */
  1107. l2_accel->macvlan[i] = NULL;
  1108. l2_accel->count--;
  1109. /* configure default DGLORT mapping for RSS/DCB */
  1110. dglort.idx = fm10k_dglort_pf_rss;
  1111. dglort.inner_rss = 1;
  1112. dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
  1113. dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
  1114. dglort.glort = interface->glort;
  1115. dglort.shared_l = fls(l2_accel->size);
  1116. hw->mac.ops.configure_dglort_map(hw, &dglort);
  1117. /* If table is empty remove it */
  1118. if (l2_accel->count == 0) {
  1119. fm10k_assign_l2_accel(interface, NULL);
  1120. kfree_rcu(l2_accel, rcu);
  1121. }
  1122. }
  1123. static netdev_features_t fm10k_features_check(struct sk_buff *skb,
  1124. struct net_device *dev,
  1125. netdev_features_t features)
  1126. {
  1127. if (!skb->encapsulation || fm10k_tx_encap_offload(skb))
  1128. return features;
  1129. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  1130. }
  1131. static const struct net_device_ops fm10k_netdev_ops = {
  1132. .ndo_open = fm10k_open,
  1133. .ndo_stop = fm10k_close,
  1134. .ndo_validate_addr = eth_validate_addr,
  1135. .ndo_start_xmit = fm10k_xmit_frame,
  1136. .ndo_set_mac_address = fm10k_set_mac,
  1137. .ndo_change_mtu = fm10k_change_mtu,
  1138. .ndo_tx_timeout = fm10k_tx_timeout,
  1139. .ndo_vlan_rx_add_vid = fm10k_vlan_rx_add_vid,
  1140. .ndo_vlan_rx_kill_vid = fm10k_vlan_rx_kill_vid,
  1141. .ndo_set_rx_mode = fm10k_set_rx_mode,
  1142. .ndo_get_stats64 = fm10k_get_stats64,
  1143. .ndo_setup_tc = __fm10k_setup_tc,
  1144. .ndo_set_vf_mac = fm10k_ndo_set_vf_mac,
  1145. .ndo_set_vf_vlan = fm10k_ndo_set_vf_vlan,
  1146. .ndo_set_vf_rate = fm10k_ndo_set_vf_bw,
  1147. .ndo_get_vf_config = fm10k_ndo_get_vf_config,
  1148. .ndo_add_vxlan_port = fm10k_add_vxlan_port,
  1149. .ndo_del_vxlan_port = fm10k_del_vxlan_port,
  1150. .ndo_do_ioctl = fm10k_ioctl,
  1151. .ndo_dfwd_add_station = fm10k_dfwd_add_station,
  1152. .ndo_dfwd_del_station = fm10k_dfwd_del_station,
  1153. #ifdef CONFIG_NET_POLL_CONTROLLER
  1154. .ndo_poll_controller = fm10k_netpoll,
  1155. #endif
  1156. .ndo_features_check = fm10k_features_check,
  1157. };
  1158. #define DEFAULT_DEBUG_LEVEL_SHIFT 3
  1159. struct net_device *fm10k_alloc_netdev(const struct fm10k_info *info)
  1160. {
  1161. netdev_features_t hw_features;
  1162. struct fm10k_intfc *interface;
  1163. struct net_device *dev;
  1164. dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES);
  1165. if (!dev)
  1166. return NULL;
  1167. /* set net device and ethtool ops */
  1168. dev->netdev_ops = &fm10k_netdev_ops;
  1169. fm10k_set_ethtool_ops(dev);
  1170. /* configure default debug level */
  1171. interface = netdev_priv(dev);
  1172. interface->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
  1173. /* configure default features */
  1174. dev->features |= NETIF_F_IP_CSUM |
  1175. NETIF_F_IPV6_CSUM |
  1176. NETIF_F_SG |
  1177. NETIF_F_TSO |
  1178. NETIF_F_TSO6 |
  1179. NETIF_F_TSO_ECN |
  1180. NETIF_F_RXHASH |
  1181. NETIF_F_RXCSUM;
  1182. /* Only the PF can support VXLAN and NVGRE tunnel offloads */
  1183. if (info->mac == fm10k_mac_pf) {
  1184. dev->hw_enc_features = NETIF_F_IP_CSUM |
  1185. NETIF_F_TSO |
  1186. NETIF_F_TSO6 |
  1187. NETIF_F_TSO_ECN |
  1188. NETIF_F_GSO_UDP_TUNNEL |
  1189. NETIF_F_IPV6_CSUM |
  1190. NETIF_F_SG;
  1191. dev->features |= NETIF_F_GSO_UDP_TUNNEL;
  1192. }
  1193. /* all features defined to this point should be changeable */
  1194. hw_features = dev->features;
  1195. /* allow user to enable L2 forwarding acceleration */
  1196. hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
  1197. /* configure VLAN features */
  1198. dev->vlan_features |= dev->features;
  1199. /* we want to leave these both on as we cannot disable VLAN tag
  1200. * insertion or stripping on the hardware since it is contained
  1201. * in the FTAG and not in the frame itself.
  1202. */
  1203. dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
  1204. NETIF_F_HW_VLAN_CTAG_RX |
  1205. NETIF_F_HW_VLAN_CTAG_FILTER;
  1206. dev->priv_flags |= IFF_UNICAST_FLT;
  1207. dev->hw_features |= hw_features;
  1208. return dev;
  1209. }