fm10k_netdev.c 38 KB

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