fm10k_netdev.c 39 KB

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