ixgbe_sriov.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2014 Intel Corporation.
  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. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. Linux NICS <linux.nics@intel.com>
  18. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  19. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  20. *******************************************************************************/
  21. #include <linux/types.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/string.h>
  27. #include <linux/in.h>
  28. #include <linux/ip.h>
  29. #include <linux/tcp.h>
  30. #include <linux/ipv6.h>
  31. #include <linux/if_bridge.h>
  32. #ifdef NETIF_F_HW_VLAN_CTAG_TX
  33. #include <linux/if_vlan.h>
  34. #endif
  35. #include "ixgbe.h"
  36. #include "ixgbe_type.h"
  37. #include "ixgbe_sriov.h"
  38. #ifdef CONFIG_PCI_IOV
  39. static int __ixgbe_enable_sriov(struct ixgbe_adapter *adapter)
  40. {
  41. struct ixgbe_hw *hw = &adapter->hw;
  42. int num_vf_macvlans, i;
  43. struct vf_macvlans *mv_list;
  44. adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
  45. e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
  46. /* Enable VMDq flag so device will be set in VM mode */
  47. adapter->flags |= IXGBE_FLAG_VMDQ_ENABLED;
  48. if (!adapter->ring_feature[RING_F_VMDQ].limit)
  49. adapter->ring_feature[RING_F_VMDQ].limit = 1;
  50. adapter->ring_feature[RING_F_VMDQ].offset = adapter->num_vfs;
  51. num_vf_macvlans = hw->mac.num_rar_entries -
  52. (IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
  53. adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
  54. sizeof(struct vf_macvlans),
  55. GFP_KERNEL);
  56. if (mv_list) {
  57. /* Initialize list of VF macvlans */
  58. INIT_LIST_HEAD(&adapter->vf_mvs.l);
  59. for (i = 0; i < num_vf_macvlans; i++) {
  60. mv_list->vf = -1;
  61. mv_list->free = true;
  62. list_add(&mv_list->l, &adapter->vf_mvs.l);
  63. mv_list++;
  64. }
  65. }
  66. /* Initialize default switching mode VEB */
  67. IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
  68. adapter->bridge_mode = BRIDGE_MODE_VEB;
  69. /* If call to enable VFs succeeded then allocate memory
  70. * for per VF control structures.
  71. */
  72. adapter->vfinfo =
  73. kcalloc(adapter->num_vfs,
  74. sizeof(struct vf_data_storage), GFP_KERNEL);
  75. if (adapter->vfinfo) {
  76. /* limit trafffic classes based on VFs enabled */
  77. if ((adapter->hw.mac.type == ixgbe_mac_82599EB) &&
  78. (adapter->num_vfs < 16)) {
  79. adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS;
  80. adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS;
  81. } else if (adapter->num_vfs < 32) {
  82. adapter->dcb_cfg.num_tcs.pg_tcs = 4;
  83. adapter->dcb_cfg.num_tcs.pfc_tcs = 4;
  84. } else {
  85. adapter->dcb_cfg.num_tcs.pg_tcs = 1;
  86. adapter->dcb_cfg.num_tcs.pfc_tcs = 1;
  87. }
  88. /* Disable RSC when in SR-IOV mode */
  89. adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
  90. IXGBE_FLAG2_RSC_ENABLED);
  91. for (i = 0; i < adapter->num_vfs; i++) {
  92. /* enable spoof checking for all VFs */
  93. adapter->vfinfo[i].spoofchk_enabled = true;
  94. /* We support VF RSS querying only for 82599 and x540
  95. * devices at the moment. These devices share RSS
  96. * indirection table and RSS hash key with PF therefore
  97. * we want to disable the querying by default.
  98. */
  99. adapter->vfinfo[i].rss_query_enabled = 0;
  100. /* Untrust all VFs */
  101. adapter->vfinfo[i].trusted = false;
  102. /* set the default xcast mode */
  103. adapter->vfinfo[i].xcast_mode = IXGBEVF_XCAST_MODE_NONE;
  104. }
  105. return 0;
  106. }
  107. return -ENOMEM;
  108. }
  109. /* Note this function is called when the user wants to enable SR-IOV
  110. * VFs using the now deprecated module parameter
  111. */
  112. void ixgbe_enable_sriov(struct ixgbe_adapter *adapter)
  113. {
  114. int pre_existing_vfs = 0;
  115. pre_existing_vfs = pci_num_vf(adapter->pdev);
  116. if (!pre_existing_vfs && !adapter->num_vfs)
  117. return;
  118. /* If there are pre-existing VFs then we have to force
  119. * use of that many - over ride any module parameter value.
  120. * This may result from the user unloading the PF driver
  121. * while VFs were assigned to guest VMs or because the VFs
  122. * have been created via the new PCI SR-IOV sysfs interface.
  123. */
  124. if (pre_existing_vfs) {
  125. adapter->num_vfs = pre_existing_vfs;
  126. dev_warn(&adapter->pdev->dev,
  127. "Virtual Functions already enabled for this device - Please reload all VF drivers to avoid spoofed packet errors\n");
  128. } else {
  129. int err;
  130. /*
  131. * The 82599 supports up to 64 VFs per physical function
  132. * but this implementation limits allocation to 63 so that
  133. * basic networking resources are still available to the
  134. * physical function. If the user requests greater than
  135. * 63 VFs then it is an error - reset to default of zero.
  136. */
  137. adapter->num_vfs = min_t(unsigned int, adapter->num_vfs, IXGBE_MAX_VFS_DRV_LIMIT);
  138. err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
  139. if (err) {
  140. e_err(probe, "Failed to enable PCI sriov: %d\n", err);
  141. adapter->num_vfs = 0;
  142. return;
  143. }
  144. }
  145. if (!__ixgbe_enable_sriov(adapter))
  146. return;
  147. /* If we have gotten to this point then there is no memory available
  148. * to manage the VF devices - print message and bail.
  149. */
  150. e_err(probe, "Unable to allocate memory for VF Data Storage - "
  151. "SRIOV disabled\n");
  152. ixgbe_disable_sriov(adapter);
  153. }
  154. #endif /* #ifdef CONFIG_PCI_IOV */
  155. int ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
  156. {
  157. struct ixgbe_hw *hw = &adapter->hw;
  158. u32 gpie;
  159. u32 vmdctl;
  160. int rss;
  161. /* set num VFs to 0 to prevent access to vfinfo */
  162. adapter->num_vfs = 0;
  163. /* free VF control structures */
  164. kfree(adapter->vfinfo);
  165. adapter->vfinfo = NULL;
  166. /* free macvlan list */
  167. kfree(adapter->mv_list);
  168. adapter->mv_list = NULL;
  169. /* if SR-IOV is already disabled then there is nothing to do */
  170. if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
  171. return 0;
  172. #ifdef CONFIG_PCI_IOV
  173. /*
  174. * If our VFs are assigned we cannot shut down SR-IOV
  175. * without causing issues, so just leave the hardware
  176. * available but disabled
  177. */
  178. if (pci_vfs_assigned(adapter->pdev)) {
  179. e_dev_warn("Unloading driver while VFs are assigned - VFs will not be deallocated\n");
  180. return -EPERM;
  181. }
  182. /* disable iov and allow time for transactions to clear */
  183. pci_disable_sriov(adapter->pdev);
  184. #endif
  185. /* turn off device IOV mode */
  186. IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, 0);
  187. gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
  188. gpie &= ~IXGBE_GPIE_VTMODE_MASK;
  189. IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
  190. /* set default pool back to 0 */
  191. vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
  192. vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
  193. IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
  194. IXGBE_WRITE_FLUSH(hw);
  195. /* Disable VMDq flag so device will be set in VM mode */
  196. if (adapter->ring_feature[RING_F_VMDQ].limit == 1) {
  197. adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
  198. adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
  199. rss = min_t(int, ixgbe_max_rss_indices(adapter),
  200. num_online_cpus());
  201. } else {
  202. rss = min_t(int, IXGBE_MAX_L2A_QUEUES, num_online_cpus());
  203. }
  204. adapter->ring_feature[RING_F_VMDQ].offset = 0;
  205. adapter->ring_feature[RING_F_RSS].limit = rss;
  206. /* take a breather then clean up driver data */
  207. msleep(100);
  208. return 0;
  209. }
  210. static int ixgbe_pci_sriov_enable(struct pci_dev *dev, int num_vfs)
  211. {
  212. #ifdef CONFIG_PCI_IOV
  213. struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
  214. int err = 0;
  215. int i;
  216. int pre_existing_vfs = pci_num_vf(dev);
  217. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  218. err = ixgbe_disable_sriov(adapter);
  219. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  220. return num_vfs;
  221. if (err)
  222. return err;
  223. /* While the SR-IOV capability structure reports total VFs to be 64,
  224. * we have to limit the actual number allocated based on two factors.
  225. * First, we reserve some transmit/receive resources for the PF.
  226. * Second, VMDQ also uses the same pools that SR-IOV does. We need to
  227. * account for this, so that we don't accidentally allocate more VFs
  228. * than we have available pools. The PCI bus driver already checks for
  229. * other values out of range.
  230. */
  231. if ((num_vfs + adapter->num_rx_pools) > IXGBE_MAX_VF_FUNCTIONS)
  232. return -EPERM;
  233. adapter->num_vfs = num_vfs;
  234. err = __ixgbe_enable_sriov(adapter);
  235. if (err)
  236. return err;
  237. for (i = 0; i < adapter->num_vfs; i++)
  238. ixgbe_vf_configuration(dev, (i | 0x10000000));
  239. err = pci_enable_sriov(dev, num_vfs);
  240. if (err) {
  241. e_dev_warn("Failed to enable PCI sriov: %d\n", err);
  242. return err;
  243. }
  244. ixgbe_sriov_reinit(adapter);
  245. return num_vfs;
  246. #else
  247. return 0;
  248. #endif
  249. }
  250. static int ixgbe_pci_sriov_disable(struct pci_dev *dev)
  251. {
  252. struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
  253. int err;
  254. #ifdef CONFIG_PCI_IOV
  255. u32 current_flags = adapter->flags;
  256. #endif
  257. err = ixgbe_disable_sriov(adapter);
  258. /* Only reinit if no error and state changed */
  259. #ifdef CONFIG_PCI_IOV
  260. if (!err && current_flags != adapter->flags)
  261. ixgbe_sriov_reinit(adapter);
  262. #endif
  263. return err;
  264. }
  265. int ixgbe_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
  266. {
  267. if (num_vfs == 0)
  268. return ixgbe_pci_sriov_disable(dev);
  269. else
  270. return ixgbe_pci_sriov_enable(dev, num_vfs);
  271. }
  272. static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
  273. u32 *msgbuf, u32 vf)
  274. {
  275. int entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
  276. >> IXGBE_VT_MSGINFO_SHIFT;
  277. u16 *hash_list = (u16 *)&msgbuf[1];
  278. struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
  279. struct ixgbe_hw *hw = &adapter->hw;
  280. int i;
  281. u32 vector_bit;
  282. u32 vector_reg;
  283. u32 mta_reg;
  284. u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
  285. /* only so many hash values supported */
  286. entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
  287. /*
  288. * salt away the number of multi cast addresses assigned
  289. * to this VF for later use to restore when the PF multi cast
  290. * list changes
  291. */
  292. vfinfo->num_vf_mc_hashes = entries;
  293. /*
  294. * VFs are limited to using the MTA hash table for their multicast
  295. * addresses
  296. */
  297. for (i = 0; i < entries; i++) {
  298. vfinfo->vf_mc_hashes[i] = hash_list[i];
  299. }
  300. for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
  301. vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
  302. vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
  303. mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
  304. mta_reg |= (1 << vector_bit);
  305. IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
  306. }
  307. vmolr |= IXGBE_VMOLR_ROMPE;
  308. IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
  309. return 0;
  310. }
  311. #ifdef CONFIG_PCI_IOV
  312. void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
  313. {
  314. struct ixgbe_hw *hw = &adapter->hw;
  315. struct vf_data_storage *vfinfo;
  316. int i, j;
  317. u32 vector_bit;
  318. u32 vector_reg;
  319. u32 mta_reg;
  320. for (i = 0; i < adapter->num_vfs; i++) {
  321. u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(i));
  322. vfinfo = &adapter->vfinfo[i];
  323. for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
  324. hw->addr_ctrl.mta_in_use++;
  325. vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
  326. vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
  327. mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
  328. mta_reg |= (1 << vector_bit);
  329. IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
  330. }
  331. if (vfinfo->num_vf_mc_hashes)
  332. vmolr |= IXGBE_VMOLR_ROMPE;
  333. else
  334. vmolr &= ~IXGBE_VMOLR_ROMPE;
  335. IXGBE_WRITE_REG(hw, IXGBE_VMOLR(i), vmolr);
  336. }
  337. /* Restore any VF macvlans */
  338. ixgbe_full_sync_mac_table(adapter);
  339. }
  340. #endif
  341. static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
  342. u32 vf)
  343. {
  344. /* VLAN 0 is a special case, don't allow it to be removed */
  345. if (!vid && !add)
  346. return 0;
  347. return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
  348. }
  349. static s32 ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
  350. {
  351. struct ixgbe_hw *hw = &adapter->hw;
  352. int max_frame = msgbuf[1];
  353. u32 max_frs;
  354. /*
  355. * For 82599EB we have to keep all PFs and VFs operating with
  356. * the same max_frame value in order to avoid sending an oversize
  357. * frame to a VF. In order to guarantee this is handled correctly
  358. * for all cases we have several special exceptions to take into
  359. * account before we can enable the VF for receive
  360. */
  361. if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
  362. struct net_device *dev = adapter->netdev;
  363. int pf_max_frame = dev->mtu + ETH_HLEN;
  364. u32 reg_offset, vf_shift, vfre;
  365. s32 err = 0;
  366. #ifdef CONFIG_FCOE
  367. if (dev->features & NETIF_F_FCOE_MTU)
  368. pf_max_frame = max_t(int, pf_max_frame,
  369. IXGBE_FCOE_JUMBO_FRAME_SIZE);
  370. #endif /* CONFIG_FCOE */
  371. switch (adapter->vfinfo[vf].vf_api) {
  372. case ixgbe_mbox_api_11:
  373. case ixgbe_mbox_api_12:
  374. /*
  375. * Version 1.1 supports jumbo frames on VFs if PF has
  376. * jumbo frames enabled which means legacy VFs are
  377. * disabled
  378. */
  379. if (pf_max_frame > ETH_FRAME_LEN)
  380. break;
  381. default:
  382. /*
  383. * If the PF or VF are running w/ jumbo frames enabled
  384. * we need to shut down the VF Rx path as we cannot
  385. * support jumbo frames on legacy VFs
  386. */
  387. if ((pf_max_frame > ETH_FRAME_LEN) ||
  388. (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)))
  389. err = -EINVAL;
  390. break;
  391. }
  392. /* determine VF receive enable location */
  393. vf_shift = vf % 32;
  394. reg_offset = vf / 32;
  395. /* enable or disable receive depending on error */
  396. vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
  397. if (err)
  398. vfre &= ~(1 << vf_shift);
  399. else
  400. vfre |= 1 << vf_shift;
  401. IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), vfre);
  402. if (err) {
  403. e_err(drv, "VF max_frame %d out of range\n", max_frame);
  404. return err;
  405. }
  406. }
  407. /* MTU < 68 is an error and causes problems on some kernels */
  408. if (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE) {
  409. e_err(drv, "VF max_frame %d out of range\n", max_frame);
  410. return -EINVAL;
  411. }
  412. /* pull current max frame size from hardware */
  413. max_frs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
  414. max_frs &= IXGBE_MHADD_MFS_MASK;
  415. max_frs >>= IXGBE_MHADD_MFS_SHIFT;
  416. if (max_frs < max_frame) {
  417. max_frs = max_frame << IXGBE_MHADD_MFS_SHIFT;
  418. IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
  419. }
  420. e_info(hw, "VF requests change max MTU to %d\n", max_frame);
  421. return 0;
  422. }
  423. static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
  424. {
  425. u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
  426. vmolr |= IXGBE_VMOLR_BAM;
  427. if (aupe)
  428. vmolr |= IXGBE_VMOLR_AUPE;
  429. else
  430. vmolr &= ~IXGBE_VMOLR_AUPE;
  431. IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
  432. }
  433. static void ixgbe_clear_vmvir(struct ixgbe_adapter *adapter, u32 vf)
  434. {
  435. struct ixgbe_hw *hw = &adapter->hw;
  436. IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
  437. }
  438. static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
  439. {
  440. struct ixgbe_hw *hw = &adapter->hw;
  441. struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
  442. u8 num_tcs = netdev_get_num_tc(adapter->netdev);
  443. /* add PF assigned VLAN or VLAN 0 */
  444. ixgbe_set_vf_vlan(adapter, true, vfinfo->pf_vlan, vf);
  445. /* reset offloads to defaults */
  446. ixgbe_set_vmolr(hw, vf, !vfinfo->pf_vlan);
  447. /* set outgoing tags for VFs */
  448. if (!vfinfo->pf_vlan && !vfinfo->pf_qos && !num_tcs) {
  449. ixgbe_clear_vmvir(adapter, vf);
  450. } else {
  451. if (vfinfo->pf_qos || !num_tcs)
  452. ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
  453. vfinfo->pf_qos, vf);
  454. else
  455. ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
  456. adapter->default_up, vf);
  457. if (vfinfo->spoofchk_enabled)
  458. hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
  459. }
  460. /* reset multicast table array for vf */
  461. adapter->vfinfo[vf].num_vf_mc_hashes = 0;
  462. /* Flush and reset the mta with the new values */
  463. ixgbe_set_rx_mode(adapter->netdev);
  464. ixgbe_del_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf);
  465. /* reset VF api back to unknown */
  466. adapter->vfinfo[vf].vf_api = ixgbe_mbox_api_10;
  467. }
  468. static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
  469. int vf, unsigned char *mac_addr)
  470. {
  471. ixgbe_del_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf);
  472. memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
  473. ixgbe_add_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf);
  474. return 0;
  475. }
  476. static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
  477. int vf, int index, unsigned char *mac_addr)
  478. {
  479. struct list_head *pos;
  480. struct vf_macvlans *entry;
  481. if (index <= 1) {
  482. list_for_each(pos, &adapter->vf_mvs.l) {
  483. entry = list_entry(pos, struct vf_macvlans, l);
  484. if (entry->vf == vf) {
  485. entry->vf = -1;
  486. entry->free = true;
  487. entry->is_macvlan = false;
  488. ixgbe_del_mac_filter(adapter,
  489. entry->vf_macvlan, vf);
  490. }
  491. }
  492. }
  493. /*
  494. * If index was zero then we were asked to clear the uc list
  495. * for the VF. We're done.
  496. */
  497. if (!index)
  498. return 0;
  499. entry = NULL;
  500. list_for_each(pos, &adapter->vf_mvs.l) {
  501. entry = list_entry(pos, struct vf_macvlans, l);
  502. if (entry->free)
  503. break;
  504. }
  505. /*
  506. * If we traversed the entire list and didn't find a free entry
  507. * then we're out of space on the RAR table. Also entry may
  508. * be NULL because the original memory allocation for the list
  509. * failed, which is not fatal but does mean we can't support
  510. * VF requests for MACVLAN because we couldn't allocate
  511. * memory for the list management required.
  512. */
  513. if (!entry || !entry->free)
  514. return -ENOSPC;
  515. entry->free = false;
  516. entry->is_macvlan = true;
  517. entry->vf = vf;
  518. memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
  519. ixgbe_add_mac_filter(adapter, mac_addr, vf);
  520. return 0;
  521. }
  522. int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
  523. {
  524. struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
  525. unsigned int vfn = (event_mask & 0x3f);
  526. bool enable = ((event_mask & 0x10000000U) != 0);
  527. if (enable)
  528. eth_zero_addr(adapter->vfinfo[vfn].vf_mac_addresses);
  529. return 0;
  530. }
  531. static inline void ixgbe_write_qde(struct ixgbe_adapter *adapter, u32 vf,
  532. u32 qde)
  533. {
  534. struct ixgbe_hw *hw = &adapter->hw;
  535. struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
  536. u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
  537. int i;
  538. for (i = vf * q_per_pool; i < ((vf + 1) * q_per_pool); i++) {
  539. u32 reg;
  540. /* flush previous write */
  541. IXGBE_WRITE_FLUSH(hw);
  542. /* indicate to hardware that we want to set drop enable */
  543. reg = IXGBE_QDE_WRITE | IXGBE_QDE_ENABLE;
  544. reg |= i << IXGBE_QDE_IDX_SHIFT;
  545. IXGBE_WRITE_REG(hw, IXGBE_QDE, reg);
  546. }
  547. }
  548. static int ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
  549. {
  550. struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
  551. struct ixgbe_hw *hw = &adapter->hw;
  552. unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
  553. u32 reg, reg_offset, vf_shift;
  554. u32 msgbuf[4] = {0, 0, 0, 0};
  555. u8 *addr = (u8 *)(&msgbuf[1]);
  556. u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
  557. int i;
  558. e_info(probe, "VF Reset msg received from vf %d\n", vf);
  559. /* reset the filters for the device */
  560. ixgbe_vf_reset_event(adapter, vf);
  561. /* set vf mac address */
  562. if (!is_zero_ether_addr(vf_mac))
  563. ixgbe_set_vf_mac(adapter, vf, vf_mac);
  564. vf_shift = vf % 32;
  565. reg_offset = vf / 32;
  566. /* enable transmit for vf */
  567. reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
  568. reg |= 1 << vf_shift;
  569. IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
  570. /* force drop enable for all VF Rx queues */
  571. ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE);
  572. /* enable receive for vf */
  573. reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
  574. reg |= 1 << vf_shift;
  575. /*
  576. * The 82599 cannot support a mix of jumbo and non-jumbo PF/VFs.
  577. * For more info take a look at ixgbe_set_vf_lpe
  578. */
  579. if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
  580. struct net_device *dev = adapter->netdev;
  581. int pf_max_frame = dev->mtu + ETH_HLEN;
  582. #ifdef CONFIG_FCOE
  583. if (dev->features & NETIF_F_FCOE_MTU)
  584. pf_max_frame = max_t(int, pf_max_frame,
  585. IXGBE_FCOE_JUMBO_FRAME_SIZE);
  586. #endif /* CONFIG_FCOE */
  587. if (pf_max_frame > ETH_FRAME_LEN)
  588. reg &= ~(1 << vf_shift);
  589. }
  590. IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
  591. /* enable VF mailbox for further messages */
  592. adapter->vfinfo[vf].clear_to_send = true;
  593. /* Enable counting of spoofed packets in the SSVPC register */
  594. reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
  595. reg |= (1 << vf_shift);
  596. IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
  597. /*
  598. * Reset the VFs TDWBAL and TDWBAH registers
  599. * which are not cleared by an FLR
  600. */
  601. for (i = 0; i < q_per_pool; i++) {
  602. IXGBE_WRITE_REG(hw, IXGBE_PVFTDWBAHn(q_per_pool, vf, i), 0);
  603. IXGBE_WRITE_REG(hw, IXGBE_PVFTDWBALn(q_per_pool, vf, i), 0);
  604. }
  605. /* reply to reset with ack and vf mac address */
  606. msgbuf[0] = IXGBE_VF_RESET;
  607. if (!is_zero_ether_addr(vf_mac)) {
  608. msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
  609. memcpy(addr, vf_mac, ETH_ALEN);
  610. } else {
  611. msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
  612. dev_warn(&adapter->pdev->dev,
  613. "VF %d has no MAC address assigned, you may have to assign one manually\n",
  614. vf);
  615. }
  616. /*
  617. * Piggyback the multicast filter type so VF can compute the
  618. * correct vectors
  619. */
  620. msgbuf[3] = hw->mac.mc_filter_type;
  621. ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
  622. return 0;
  623. }
  624. static int ixgbe_set_vf_mac_addr(struct ixgbe_adapter *adapter,
  625. u32 *msgbuf, u32 vf)
  626. {
  627. u8 *new_mac = ((u8 *)(&msgbuf[1]));
  628. if (!is_valid_ether_addr(new_mac)) {
  629. e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
  630. return -1;
  631. }
  632. if (adapter->vfinfo[vf].pf_set_mac &&
  633. !ether_addr_equal(adapter->vfinfo[vf].vf_mac_addresses, new_mac)) {
  634. e_warn(drv,
  635. "VF %d attempted to override administratively set MAC address\n"
  636. "Reload the VF driver to resume operations\n",
  637. vf);
  638. return -1;
  639. }
  640. return ixgbe_set_vf_mac(adapter, vf, new_mac) < 0;
  641. }
  642. static int ixgbe_find_vlvf_entry(struct ixgbe_hw *hw, u32 vlan)
  643. {
  644. u32 vlvf;
  645. s32 regindex;
  646. /* short cut the special case */
  647. if (vlan == 0)
  648. return 0;
  649. /* Search for the vlan id in the VLVF entries */
  650. for (regindex = 1; regindex < IXGBE_VLVF_ENTRIES; regindex++) {
  651. vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
  652. if ((vlvf & VLAN_VID_MASK) == vlan)
  653. break;
  654. }
  655. /* Return a negative value if not found */
  656. if (regindex >= IXGBE_VLVF_ENTRIES)
  657. regindex = -1;
  658. return regindex;
  659. }
  660. static int ixgbe_set_vf_vlan_msg(struct ixgbe_adapter *adapter,
  661. u32 *msgbuf, u32 vf)
  662. {
  663. struct ixgbe_hw *hw = &adapter->hw;
  664. int add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >> IXGBE_VT_MSGINFO_SHIFT;
  665. int vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
  666. int err;
  667. s32 reg_ndx;
  668. u32 vlvf;
  669. u32 bits;
  670. u8 tcs = netdev_get_num_tc(adapter->netdev);
  671. if (adapter->vfinfo[vf].pf_vlan || tcs) {
  672. e_warn(drv,
  673. "VF %d attempted to override administratively set VLAN configuration\n"
  674. "Reload the VF driver to resume operations\n",
  675. vf);
  676. return -1;
  677. }
  678. if (add)
  679. adapter->vfinfo[vf].vlan_count++;
  680. else if (adapter->vfinfo[vf].vlan_count)
  681. adapter->vfinfo[vf].vlan_count--;
  682. /* in case of promiscuous mode any VLAN filter set for a VF must
  683. * also have the PF pool added to it.
  684. */
  685. if (add && adapter->netdev->flags & IFF_PROMISC)
  686. err = ixgbe_set_vf_vlan(adapter, add, vid, VMDQ_P(0));
  687. err = ixgbe_set_vf_vlan(adapter, add, vid, vf);
  688. if (!err && adapter->vfinfo[vf].spoofchk_enabled)
  689. hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
  690. /* Go through all the checks to see if the VLAN filter should
  691. * be wiped completely.
  692. */
  693. if (!add && adapter->netdev->flags & IFF_PROMISC) {
  694. reg_ndx = ixgbe_find_vlvf_entry(hw, vid);
  695. if (reg_ndx < 0)
  696. return err;
  697. vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(reg_ndx));
  698. /* See if any other pools are set for this VLAN filter
  699. * entry other than the PF.
  700. */
  701. if (VMDQ_P(0) < 32) {
  702. bits = IXGBE_READ_REG(hw, IXGBE_VLVFB(reg_ndx * 2));
  703. bits &= ~(1 << VMDQ_P(0));
  704. bits |= IXGBE_READ_REG(hw,
  705. IXGBE_VLVFB(reg_ndx * 2) + 1);
  706. } else {
  707. bits = IXGBE_READ_REG(hw,
  708. IXGBE_VLVFB(reg_ndx * 2) + 1);
  709. bits &= ~(1 << (VMDQ_P(0) - 32));
  710. bits |= IXGBE_READ_REG(hw, IXGBE_VLVFB(reg_ndx * 2));
  711. }
  712. /* If the filter was removed then ensure PF pool bit
  713. * is cleared if the PF only added itself to the pool
  714. * because the PF is in promiscuous mode.
  715. */
  716. if ((vlvf & VLAN_VID_MASK) == vid &&
  717. !test_bit(vid, adapter->active_vlans) && !bits)
  718. ixgbe_set_vf_vlan(adapter, add, vid, VMDQ_P(0));
  719. }
  720. return err;
  721. }
  722. static int ixgbe_set_vf_macvlan_msg(struct ixgbe_adapter *adapter,
  723. u32 *msgbuf, u32 vf)
  724. {
  725. u8 *new_mac = ((u8 *)(&msgbuf[1]));
  726. int index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
  727. IXGBE_VT_MSGINFO_SHIFT;
  728. int err;
  729. if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
  730. e_warn(drv,
  731. "VF %d requested MACVLAN filter but is administratively denied\n",
  732. vf);
  733. return -1;
  734. }
  735. /* An non-zero index indicates the VF is setting a filter */
  736. if (index) {
  737. if (!is_valid_ether_addr(new_mac)) {
  738. e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
  739. return -1;
  740. }
  741. /*
  742. * If the VF is allowed to set MAC filters then turn off
  743. * anti-spoofing to avoid false positives.
  744. */
  745. if (adapter->vfinfo[vf].spoofchk_enabled)
  746. ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
  747. }
  748. err = ixgbe_set_vf_macvlan(adapter, vf, index, new_mac);
  749. if (err == -ENOSPC)
  750. e_warn(drv,
  751. "VF %d has requested a MACVLAN filter but there is no space for it\n",
  752. vf);
  753. return err < 0;
  754. }
  755. static int ixgbe_negotiate_vf_api(struct ixgbe_adapter *adapter,
  756. u32 *msgbuf, u32 vf)
  757. {
  758. int api = msgbuf[1];
  759. switch (api) {
  760. case ixgbe_mbox_api_10:
  761. case ixgbe_mbox_api_11:
  762. case ixgbe_mbox_api_12:
  763. adapter->vfinfo[vf].vf_api = api;
  764. return 0;
  765. default:
  766. break;
  767. }
  768. e_info(drv, "VF %d requested invalid api version %u\n", vf, api);
  769. return -1;
  770. }
  771. static int ixgbe_get_vf_queues(struct ixgbe_adapter *adapter,
  772. u32 *msgbuf, u32 vf)
  773. {
  774. struct net_device *dev = adapter->netdev;
  775. struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
  776. unsigned int default_tc = 0;
  777. u8 num_tcs = netdev_get_num_tc(dev);
  778. /* verify the PF is supporting the correct APIs */
  779. switch (adapter->vfinfo[vf].vf_api) {
  780. case ixgbe_mbox_api_20:
  781. case ixgbe_mbox_api_11:
  782. case ixgbe_mbox_api_12:
  783. break;
  784. default:
  785. return -1;
  786. }
  787. /* only allow 1 Tx queue for bandwidth limiting */
  788. msgbuf[IXGBE_VF_TX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
  789. msgbuf[IXGBE_VF_RX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
  790. /* if TCs > 1 determine which TC belongs to default user priority */
  791. if (num_tcs > 1)
  792. default_tc = netdev_get_prio_tc_map(dev, adapter->default_up);
  793. /* notify VF of need for VLAN tag stripping, and correct queue */
  794. if (num_tcs)
  795. msgbuf[IXGBE_VF_TRANS_VLAN] = num_tcs;
  796. else if (adapter->vfinfo[vf].pf_vlan || adapter->vfinfo[vf].pf_qos)
  797. msgbuf[IXGBE_VF_TRANS_VLAN] = 1;
  798. else
  799. msgbuf[IXGBE_VF_TRANS_VLAN] = 0;
  800. /* notify VF of default queue */
  801. msgbuf[IXGBE_VF_DEF_QUEUE] = default_tc;
  802. return 0;
  803. }
  804. static int ixgbe_get_vf_reta(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
  805. {
  806. u32 i, j;
  807. u32 *out_buf = &msgbuf[1];
  808. const u8 *reta = adapter->rss_indir_tbl;
  809. u32 reta_size = ixgbe_rss_indir_tbl_entries(adapter);
  810. /* Check if operation is permitted */
  811. if (!adapter->vfinfo[vf].rss_query_enabled)
  812. return -EPERM;
  813. /* verify the PF is supporting the correct API */
  814. if (adapter->vfinfo[vf].vf_api != ixgbe_mbox_api_12)
  815. return -EOPNOTSUPP;
  816. /* This mailbox command is supported (required) only for 82599 and x540
  817. * VFs which support up to 4 RSS queues. Therefore we will compress the
  818. * RETA by saving only 2 bits from each entry. This way we will be able
  819. * to transfer the whole RETA in a single mailbox operation.
  820. */
  821. for (i = 0; i < reta_size / 16; i++) {
  822. out_buf[i] = 0;
  823. for (j = 0; j < 16; j++)
  824. out_buf[i] |= (u32)(reta[16 * i + j] & 0x3) << (2 * j);
  825. }
  826. return 0;
  827. }
  828. static int ixgbe_get_vf_rss_key(struct ixgbe_adapter *adapter,
  829. u32 *msgbuf, u32 vf)
  830. {
  831. u32 *rss_key = &msgbuf[1];
  832. /* Check if the operation is permitted */
  833. if (!adapter->vfinfo[vf].rss_query_enabled)
  834. return -EPERM;
  835. /* verify the PF is supporting the correct API */
  836. if (adapter->vfinfo[vf].vf_api != ixgbe_mbox_api_12)
  837. return -EOPNOTSUPP;
  838. memcpy(rss_key, adapter->rss_key, sizeof(adapter->rss_key));
  839. return 0;
  840. }
  841. static int ixgbe_update_vf_xcast_mode(struct ixgbe_adapter *adapter,
  842. u32 *msgbuf, u32 vf)
  843. {
  844. struct ixgbe_hw *hw = &adapter->hw;
  845. int xcast_mode = msgbuf[1];
  846. u32 vmolr, disable, enable;
  847. /* verify the PF is supporting the correct APIs */
  848. switch (adapter->vfinfo[vf].vf_api) {
  849. case ixgbe_mbox_api_12:
  850. break;
  851. default:
  852. return -EOPNOTSUPP;
  853. }
  854. if (xcast_mode > IXGBEVF_XCAST_MODE_MULTI &&
  855. !adapter->vfinfo[vf].trusted) {
  856. xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
  857. }
  858. if (adapter->vfinfo[vf].xcast_mode == xcast_mode)
  859. goto out;
  860. switch (xcast_mode) {
  861. case IXGBEVF_XCAST_MODE_NONE:
  862. disable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_MPE;
  863. enable = 0;
  864. break;
  865. case IXGBEVF_XCAST_MODE_MULTI:
  866. disable = IXGBE_VMOLR_MPE;
  867. enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE;
  868. break;
  869. case IXGBEVF_XCAST_MODE_ALLMULTI:
  870. disable = 0;
  871. enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_MPE;
  872. break;
  873. default:
  874. return -EOPNOTSUPP;
  875. }
  876. vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
  877. vmolr &= ~disable;
  878. vmolr |= enable;
  879. IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
  880. adapter->vfinfo[vf].xcast_mode = xcast_mode;
  881. out:
  882. msgbuf[1] = xcast_mode;
  883. return 0;
  884. }
  885. static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
  886. {
  887. u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
  888. u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
  889. struct ixgbe_hw *hw = &adapter->hw;
  890. s32 retval;
  891. retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
  892. if (retval) {
  893. pr_err("Error receiving message from VF\n");
  894. return retval;
  895. }
  896. /* this is a message we already processed, do nothing */
  897. if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
  898. return 0;
  899. /* flush the ack before we write any messages back */
  900. IXGBE_WRITE_FLUSH(hw);
  901. if (msgbuf[0] == IXGBE_VF_RESET)
  902. return ixgbe_vf_reset_msg(adapter, vf);
  903. /*
  904. * until the vf completes a virtual function reset it should not be
  905. * allowed to start any configuration.
  906. */
  907. if (!adapter->vfinfo[vf].clear_to_send) {
  908. msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
  909. ixgbe_write_mbx(hw, msgbuf, 1, vf);
  910. return 0;
  911. }
  912. switch ((msgbuf[0] & 0xFFFF)) {
  913. case IXGBE_VF_SET_MAC_ADDR:
  914. retval = ixgbe_set_vf_mac_addr(adapter, msgbuf, vf);
  915. break;
  916. case IXGBE_VF_SET_MULTICAST:
  917. retval = ixgbe_set_vf_multicasts(adapter, msgbuf, vf);
  918. break;
  919. case IXGBE_VF_SET_VLAN:
  920. retval = ixgbe_set_vf_vlan_msg(adapter, msgbuf, vf);
  921. break;
  922. case IXGBE_VF_SET_LPE:
  923. retval = ixgbe_set_vf_lpe(adapter, msgbuf, vf);
  924. break;
  925. case IXGBE_VF_SET_MACVLAN:
  926. retval = ixgbe_set_vf_macvlan_msg(adapter, msgbuf, vf);
  927. break;
  928. case IXGBE_VF_API_NEGOTIATE:
  929. retval = ixgbe_negotiate_vf_api(adapter, msgbuf, vf);
  930. break;
  931. case IXGBE_VF_GET_QUEUES:
  932. retval = ixgbe_get_vf_queues(adapter, msgbuf, vf);
  933. break;
  934. case IXGBE_VF_GET_RETA:
  935. retval = ixgbe_get_vf_reta(adapter, msgbuf, vf);
  936. break;
  937. case IXGBE_VF_GET_RSS_KEY:
  938. retval = ixgbe_get_vf_rss_key(adapter, msgbuf, vf);
  939. break;
  940. case IXGBE_VF_UPDATE_XCAST_MODE:
  941. retval = ixgbe_update_vf_xcast_mode(adapter, msgbuf, vf);
  942. break;
  943. default:
  944. e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
  945. retval = IXGBE_ERR_MBX;
  946. break;
  947. }
  948. /* notify the VF of the results of what it sent us */
  949. if (retval)
  950. msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
  951. else
  952. msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
  953. msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
  954. ixgbe_write_mbx(hw, msgbuf, mbx_size, vf);
  955. return retval;
  956. }
  957. static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
  958. {
  959. struct ixgbe_hw *hw = &adapter->hw;
  960. u32 msg = IXGBE_VT_MSGTYPE_NACK;
  961. /* if device isn't clear to send it shouldn't be reading either */
  962. if (!adapter->vfinfo[vf].clear_to_send)
  963. ixgbe_write_mbx(hw, &msg, 1, vf);
  964. }
  965. void ixgbe_msg_task(struct ixgbe_adapter *adapter)
  966. {
  967. struct ixgbe_hw *hw = &adapter->hw;
  968. u32 vf;
  969. for (vf = 0; vf < adapter->num_vfs; vf++) {
  970. /* process any reset requests */
  971. if (!ixgbe_check_for_rst(hw, vf))
  972. ixgbe_vf_reset_event(adapter, vf);
  973. /* process any messages pending */
  974. if (!ixgbe_check_for_msg(hw, vf))
  975. ixgbe_rcv_msg_from_vf(adapter, vf);
  976. /* process any acks */
  977. if (!ixgbe_check_for_ack(hw, vf))
  978. ixgbe_rcv_ack_from_vf(adapter, vf);
  979. }
  980. }
  981. void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
  982. {
  983. struct ixgbe_hw *hw = &adapter->hw;
  984. /* disable transmit and receive for all vfs */
  985. IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
  986. IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
  987. IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
  988. IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
  989. }
  990. static inline void ixgbe_ping_vf(struct ixgbe_adapter *adapter, int vf)
  991. {
  992. struct ixgbe_hw *hw = &adapter->hw;
  993. u32 ping;
  994. ping = IXGBE_PF_CONTROL_MSG;
  995. if (adapter->vfinfo[vf].clear_to_send)
  996. ping |= IXGBE_VT_MSGTYPE_CTS;
  997. ixgbe_write_mbx(hw, &ping, 1, vf);
  998. }
  999. void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
  1000. {
  1001. struct ixgbe_hw *hw = &adapter->hw;
  1002. u32 ping;
  1003. int i;
  1004. for (i = 0 ; i < adapter->num_vfs; i++) {
  1005. ping = IXGBE_PF_CONTROL_MSG;
  1006. if (adapter->vfinfo[i].clear_to_send)
  1007. ping |= IXGBE_VT_MSGTYPE_CTS;
  1008. ixgbe_write_mbx(hw, &ping, 1, i);
  1009. }
  1010. }
  1011. int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  1012. {
  1013. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  1014. if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
  1015. return -EINVAL;
  1016. adapter->vfinfo[vf].pf_set_mac = true;
  1017. dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
  1018. dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
  1019. " change effective.");
  1020. if (test_bit(__IXGBE_DOWN, &adapter->state)) {
  1021. dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
  1022. " but the PF device is not up.\n");
  1023. dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
  1024. " attempting to use the VF device.\n");
  1025. }
  1026. return ixgbe_set_vf_mac(adapter, vf, mac);
  1027. }
  1028. static int ixgbe_enable_port_vlan(struct ixgbe_adapter *adapter, int vf,
  1029. u16 vlan, u8 qos)
  1030. {
  1031. struct ixgbe_hw *hw = &adapter->hw;
  1032. int err;
  1033. err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
  1034. if (err)
  1035. goto out;
  1036. ixgbe_set_vmvir(adapter, vlan, qos, vf);
  1037. ixgbe_set_vmolr(hw, vf, false);
  1038. if (adapter->vfinfo[vf].spoofchk_enabled)
  1039. hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
  1040. adapter->vfinfo[vf].vlan_count++;
  1041. /* enable hide vlan on X550 */
  1042. if (hw->mac.type >= ixgbe_mac_X550)
  1043. ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE |
  1044. IXGBE_QDE_HIDE_VLAN);
  1045. adapter->vfinfo[vf].pf_vlan = vlan;
  1046. adapter->vfinfo[vf].pf_qos = qos;
  1047. dev_info(&adapter->pdev->dev,
  1048. "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
  1049. if (test_bit(__IXGBE_DOWN, &adapter->state)) {
  1050. dev_warn(&adapter->pdev->dev,
  1051. "The VF VLAN has been set, but the PF device is not up.\n");
  1052. dev_warn(&adapter->pdev->dev,
  1053. "Bring the PF device up before attempting to use the VF device.\n");
  1054. }
  1055. out:
  1056. return err;
  1057. }
  1058. static int ixgbe_disable_port_vlan(struct ixgbe_adapter *adapter, int vf)
  1059. {
  1060. struct ixgbe_hw *hw = &adapter->hw;
  1061. int err;
  1062. err = ixgbe_set_vf_vlan(adapter, false,
  1063. adapter->vfinfo[vf].pf_vlan, vf);
  1064. ixgbe_clear_vmvir(adapter, vf);
  1065. ixgbe_set_vmolr(hw, vf, true);
  1066. hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
  1067. if (adapter->vfinfo[vf].vlan_count)
  1068. adapter->vfinfo[vf].vlan_count--;
  1069. /* disable hide VLAN on X550 */
  1070. if (hw->mac.type >= ixgbe_mac_X550)
  1071. ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE);
  1072. adapter->vfinfo[vf].pf_vlan = 0;
  1073. adapter->vfinfo[vf].pf_qos = 0;
  1074. return err;
  1075. }
  1076. int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
  1077. {
  1078. int err = 0;
  1079. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  1080. if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
  1081. return -EINVAL;
  1082. if (vlan || qos) {
  1083. /* Check if there is already a port VLAN set, if so
  1084. * we have to delete the old one first before we
  1085. * can set the new one. The usage model had
  1086. * previously assumed the user would delete the
  1087. * old port VLAN before setting a new one but this
  1088. * is not necessarily the case.
  1089. */
  1090. if (adapter->vfinfo[vf].pf_vlan)
  1091. err = ixgbe_disable_port_vlan(adapter, vf);
  1092. if (err)
  1093. goto out;
  1094. err = ixgbe_enable_port_vlan(adapter, vf, vlan, qos);
  1095. } else {
  1096. err = ixgbe_disable_port_vlan(adapter, vf);
  1097. }
  1098. out:
  1099. return err;
  1100. }
  1101. static int ixgbe_link_mbps(struct ixgbe_adapter *adapter)
  1102. {
  1103. switch (adapter->link_speed) {
  1104. case IXGBE_LINK_SPEED_100_FULL:
  1105. return 100;
  1106. case IXGBE_LINK_SPEED_1GB_FULL:
  1107. return 1000;
  1108. case IXGBE_LINK_SPEED_10GB_FULL:
  1109. return 10000;
  1110. default:
  1111. return 0;
  1112. }
  1113. }
  1114. static void ixgbe_set_vf_rate_limit(struct ixgbe_adapter *adapter, int vf)
  1115. {
  1116. struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
  1117. struct ixgbe_hw *hw = &adapter->hw;
  1118. u32 bcnrc_val = 0;
  1119. u16 queue, queues_per_pool;
  1120. u16 tx_rate = adapter->vfinfo[vf].tx_rate;
  1121. if (tx_rate) {
  1122. /* start with base link speed value */
  1123. bcnrc_val = adapter->vf_rate_link_speed;
  1124. /* Calculate the rate factor values to set */
  1125. bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT;
  1126. bcnrc_val /= tx_rate;
  1127. /* clear everything but the rate factor */
  1128. bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK |
  1129. IXGBE_RTTBCNRC_RF_DEC_MASK;
  1130. /* enable the rate scheduler */
  1131. bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA;
  1132. }
  1133. /*
  1134. * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
  1135. * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
  1136. * and 0x004 otherwise.
  1137. */
  1138. switch (hw->mac.type) {
  1139. case ixgbe_mac_82599EB:
  1140. IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
  1141. break;
  1142. case ixgbe_mac_X540:
  1143. IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
  1144. break;
  1145. default:
  1146. break;
  1147. }
  1148. /* determine how many queues per pool based on VMDq mask */
  1149. queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
  1150. /* write value for all Tx queues belonging to VF */
  1151. for (queue = 0; queue < queues_per_pool; queue++) {
  1152. unsigned int reg_idx = (vf * queues_per_pool) + queue;
  1153. IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, reg_idx);
  1154. IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
  1155. }
  1156. }
  1157. void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
  1158. {
  1159. int i;
  1160. /* VF Tx rate limit was not set */
  1161. if (!adapter->vf_rate_link_speed)
  1162. return;
  1163. if (ixgbe_link_mbps(adapter) != adapter->vf_rate_link_speed) {
  1164. adapter->vf_rate_link_speed = 0;
  1165. dev_info(&adapter->pdev->dev,
  1166. "Link speed has been changed. VF Transmit rate is disabled\n");
  1167. }
  1168. for (i = 0; i < adapter->num_vfs; i++) {
  1169. if (!adapter->vf_rate_link_speed)
  1170. adapter->vfinfo[i].tx_rate = 0;
  1171. ixgbe_set_vf_rate_limit(adapter, i);
  1172. }
  1173. }
  1174. int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int min_tx_rate,
  1175. int max_tx_rate)
  1176. {
  1177. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  1178. int link_speed;
  1179. /* verify VF is active */
  1180. if (vf >= adapter->num_vfs)
  1181. return -EINVAL;
  1182. /* verify link is up */
  1183. if (!adapter->link_up)
  1184. return -EINVAL;
  1185. /* verify we are linked at 10Gbps */
  1186. link_speed = ixgbe_link_mbps(adapter);
  1187. if (link_speed != 10000)
  1188. return -EINVAL;
  1189. if (min_tx_rate)
  1190. return -EINVAL;
  1191. /* rate limit cannot be less than 10Mbs or greater than link speed */
  1192. if (max_tx_rate && ((max_tx_rate <= 10) || (max_tx_rate > link_speed)))
  1193. return -EINVAL;
  1194. /* store values */
  1195. adapter->vf_rate_link_speed = link_speed;
  1196. adapter->vfinfo[vf].tx_rate = max_tx_rate;
  1197. /* update hardware configuration */
  1198. ixgbe_set_vf_rate_limit(adapter, vf);
  1199. return 0;
  1200. }
  1201. int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
  1202. {
  1203. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  1204. int vf_target_reg = vf >> 3;
  1205. int vf_target_shift = vf % 8;
  1206. struct ixgbe_hw *hw = &adapter->hw;
  1207. u32 regval;
  1208. if (vf >= adapter->num_vfs)
  1209. return -EINVAL;
  1210. adapter->vfinfo[vf].spoofchk_enabled = setting;
  1211. regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
  1212. regval &= ~(1 << vf_target_shift);
  1213. regval |= (setting << vf_target_shift);
  1214. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
  1215. if (adapter->vfinfo[vf].vlan_count) {
  1216. vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
  1217. regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
  1218. regval &= ~(1 << vf_target_shift);
  1219. regval |= (setting << vf_target_shift);
  1220. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
  1221. }
  1222. return 0;
  1223. }
  1224. int ixgbe_ndo_set_vf_rss_query_en(struct net_device *netdev, int vf,
  1225. bool setting)
  1226. {
  1227. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  1228. /* This operation is currently supported only for 82599 and x540
  1229. * devices.
  1230. */
  1231. if (adapter->hw.mac.type < ixgbe_mac_82599EB ||
  1232. adapter->hw.mac.type >= ixgbe_mac_X550)
  1233. return -EOPNOTSUPP;
  1234. if (vf >= adapter->num_vfs)
  1235. return -EINVAL;
  1236. adapter->vfinfo[vf].rss_query_enabled = setting;
  1237. return 0;
  1238. }
  1239. int ixgbe_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
  1240. {
  1241. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  1242. if (vf >= adapter->num_vfs)
  1243. return -EINVAL;
  1244. /* nothing to do */
  1245. if (adapter->vfinfo[vf].trusted == setting)
  1246. return 0;
  1247. adapter->vfinfo[vf].trusted = setting;
  1248. /* reset VF to reconfigure features */
  1249. adapter->vfinfo[vf].clear_to_send = false;
  1250. ixgbe_ping_vf(adapter, vf);
  1251. e_info(drv, "VF %u is %strusted\n", vf, setting ? "" : "not ");
  1252. return 0;
  1253. }
  1254. int ixgbe_ndo_get_vf_config(struct net_device *netdev,
  1255. int vf, struct ifla_vf_info *ivi)
  1256. {
  1257. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  1258. if (vf >= adapter->num_vfs)
  1259. return -EINVAL;
  1260. ivi->vf = vf;
  1261. memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
  1262. ivi->max_tx_rate = adapter->vfinfo[vf].tx_rate;
  1263. ivi->min_tx_rate = 0;
  1264. ivi->vlan = adapter->vfinfo[vf].pf_vlan;
  1265. ivi->qos = adapter->vfinfo[vf].pf_qos;
  1266. ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
  1267. ivi->rss_query_en = adapter->vfinfo[vf].rss_query_enabled;
  1268. ivi->trusted = adapter->vfinfo[vf].trusted;
  1269. return 0;
  1270. }