fm10k_netdev.c 38 KB

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