fm10k_netdev.c 38 KB

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