bnxt_sriov.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2014-2015 Broadcom Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/etherdevice.h>
  15. #include "bnxt_hsi.h"
  16. #include "bnxt.h"
  17. #include "bnxt_sriov.h"
  18. #include "bnxt_ethtool.h"
  19. #ifdef CONFIG_BNXT_SRIOV
  20. static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id)
  21. {
  22. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  23. netdev_err(bp->dev, "vf ndo called though PF is down\n");
  24. return -EINVAL;
  25. }
  26. if (!bp->pf.active_vfs) {
  27. netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
  28. return -EINVAL;
  29. }
  30. if (vf_id >= bp->pf.active_vfs) {
  31. netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
  32. return -EINVAL;
  33. }
  34. return 0;
  35. }
  36. int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
  37. {
  38. struct hwrm_func_cfg_input req = {0};
  39. struct bnxt *bp = netdev_priv(dev);
  40. struct bnxt_vf_info *vf;
  41. bool old_setting = false;
  42. u32 func_flags;
  43. int rc;
  44. rc = bnxt_vf_ndo_prep(bp, vf_id);
  45. if (rc)
  46. return rc;
  47. vf = &bp->pf.vf[vf_id];
  48. if (vf->flags & BNXT_VF_SPOOFCHK)
  49. old_setting = true;
  50. if (old_setting == setting)
  51. return 0;
  52. func_flags = vf->func_flags;
  53. if (setting)
  54. func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
  55. else
  56. func_flags &= ~FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
  57. /*TODO: if the driver supports VLAN filter on guest VLAN,
  58. * the spoof check should also include vlan anti-spoofing
  59. */
  60. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  61. req.vf_id = cpu_to_le16(vf->fw_fid);
  62. req.flags = cpu_to_le32(func_flags);
  63. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  64. if (!rc) {
  65. vf->func_flags = func_flags;
  66. if (setting)
  67. vf->flags |= BNXT_VF_SPOOFCHK;
  68. else
  69. vf->flags &= ~BNXT_VF_SPOOFCHK;
  70. }
  71. return rc;
  72. }
  73. int bnxt_get_vf_config(struct net_device *dev, int vf_id,
  74. struct ifla_vf_info *ivi)
  75. {
  76. struct bnxt *bp = netdev_priv(dev);
  77. struct bnxt_vf_info *vf;
  78. int rc;
  79. rc = bnxt_vf_ndo_prep(bp, vf_id);
  80. if (rc)
  81. return rc;
  82. ivi->vf = vf_id;
  83. vf = &bp->pf.vf[vf_id];
  84. memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN);
  85. ivi->max_tx_rate = vf->max_tx_rate;
  86. ivi->min_tx_rate = vf->min_tx_rate;
  87. ivi->vlan = vf->vlan;
  88. ivi->qos = vf->flags & BNXT_VF_QOS;
  89. ivi->spoofchk = vf->flags & BNXT_VF_SPOOFCHK;
  90. if (!(vf->flags & BNXT_VF_LINK_FORCED))
  91. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  92. else if (vf->flags & BNXT_VF_LINK_UP)
  93. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  94. else
  95. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  96. return 0;
  97. }
  98. int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
  99. {
  100. struct hwrm_func_cfg_input req = {0};
  101. struct bnxt *bp = netdev_priv(dev);
  102. struct bnxt_vf_info *vf;
  103. int rc;
  104. rc = bnxt_vf_ndo_prep(bp, vf_id);
  105. if (rc)
  106. return rc;
  107. /* reject bc or mc mac addr, zero mac addr means allow
  108. * VF to use its own mac addr
  109. */
  110. if (is_multicast_ether_addr(mac)) {
  111. netdev_err(dev, "Invalid VF ethernet address\n");
  112. return -EINVAL;
  113. }
  114. vf = &bp->pf.vf[vf_id];
  115. memcpy(vf->mac_addr, mac, ETH_ALEN);
  116. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  117. req.vf_id = cpu_to_le16(vf->fw_fid);
  118. req.flags = cpu_to_le32(vf->func_flags);
  119. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
  120. memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
  121. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  122. }
  123. int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos)
  124. {
  125. struct hwrm_func_cfg_input req = {0};
  126. struct bnxt *bp = netdev_priv(dev);
  127. struct bnxt_vf_info *vf;
  128. u16 vlan_tag;
  129. int rc;
  130. rc = bnxt_vf_ndo_prep(bp, vf_id);
  131. if (rc)
  132. return rc;
  133. /* TODO: needed to implement proper handling of user priority,
  134. * currently fail the command if there is valid priority
  135. */
  136. if (vlan_id > 4095 || qos)
  137. return -EINVAL;
  138. vf = &bp->pf.vf[vf_id];
  139. vlan_tag = vlan_id;
  140. if (vlan_tag == vf->vlan)
  141. return 0;
  142. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  143. req.vf_id = cpu_to_le16(vf->fw_fid);
  144. req.flags = cpu_to_le32(vf->func_flags);
  145. req.dflt_vlan = cpu_to_le16(vlan_tag);
  146. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
  147. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  148. if (!rc)
  149. vf->vlan = vlan_tag;
  150. return rc;
  151. }
  152. int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
  153. int max_tx_rate)
  154. {
  155. struct hwrm_func_cfg_input req = {0};
  156. struct bnxt *bp = netdev_priv(dev);
  157. struct bnxt_vf_info *vf;
  158. u32 pf_link_speed;
  159. int rc;
  160. rc = bnxt_vf_ndo_prep(bp, vf_id);
  161. if (rc)
  162. return rc;
  163. vf = &bp->pf.vf[vf_id];
  164. pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
  165. if (max_tx_rate > pf_link_speed) {
  166. netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
  167. max_tx_rate, vf_id);
  168. return -EINVAL;
  169. }
  170. if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) {
  171. netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
  172. min_tx_rate, vf_id);
  173. return -EINVAL;
  174. }
  175. if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
  176. return 0;
  177. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  178. req.vf_id = cpu_to_le16(vf->fw_fid);
  179. req.flags = cpu_to_le32(vf->func_flags);
  180. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
  181. req.max_bw = cpu_to_le32(max_tx_rate);
  182. req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
  183. req.min_bw = cpu_to_le32(min_tx_rate);
  184. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  185. if (!rc) {
  186. vf->min_tx_rate = min_tx_rate;
  187. vf->max_tx_rate = max_tx_rate;
  188. }
  189. return rc;
  190. }
  191. int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
  192. {
  193. struct bnxt *bp = netdev_priv(dev);
  194. struct bnxt_vf_info *vf;
  195. int rc;
  196. rc = bnxt_vf_ndo_prep(bp, vf_id);
  197. if (rc)
  198. return rc;
  199. vf = &bp->pf.vf[vf_id];
  200. vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
  201. switch (link) {
  202. case IFLA_VF_LINK_STATE_AUTO:
  203. vf->flags |= BNXT_VF_LINK_UP;
  204. break;
  205. case IFLA_VF_LINK_STATE_DISABLE:
  206. vf->flags |= BNXT_VF_LINK_FORCED;
  207. break;
  208. case IFLA_VF_LINK_STATE_ENABLE:
  209. vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
  210. break;
  211. default:
  212. netdev_err(bp->dev, "Invalid link option\n");
  213. rc = -EINVAL;
  214. break;
  215. }
  216. /* CHIMP TODO: send msg to VF to update new link state */
  217. return rc;
  218. }
  219. static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
  220. {
  221. int i;
  222. struct bnxt_vf_info *vf;
  223. for (i = 0; i < num_vfs; i++) {
  224. vf = &bp->pf.vf[i];
  225. memset(vf, 0, sizeof(*vf));
  226. vf->flags = BNXT_VF_QOS | BNXT_VF_LINK_UP;
  227. }
  228. return 0;
  229. }
  230. static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
  231. {
  232. int i, rc = 0;
  233. struct bnxt_pf_info *pf = &bp->pf;
  234. struct hwrm_func_vf_resc_free_input req = {0};
  235. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
  236. mutex_lock(&bp->hwrm_cmd_lock);
  237. for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
  238. req.vf_id = cpu_to_le16(i);
  239. rc = _hwrm_send_message(bp, &req, sizeof(req),
  240. HWRM_CMD_TIMEOUT);
  241. if (rc)
  242. break;
  243. }
  244. mutex_unlock(&bp->hwrm_cmd_lock);
  245. return rc;
  246. }
  247. static void bnxt_free_vf_resources(struct bnxt *bp)
  248. {
  249. struct pci_dev *pdev = bp->pdev;
  250. int i;
  251. kfree(bp->pf.vf_event_bmap);
  252. bp->pf.vf_event_bmap = NULL;
  253. for (i = 0; i < 4; i++) {
  254. if (bp->pf.hwrm_cmd_req_addr[i]) {
  255. dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
  256. bp->pf.hwrm_cmd_req_addr[i],
  257. bp->pf.hwrm_cmd_req_dma_addr[i]);
  258. bp->pf.hwrm_cmd_req_addr[i] = NULL;
  259. }
  260. }
  261. kfree(bp->pf.vf);
  262. bp->pf.vf = NULL;
  263. }
  264. static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
  265. {
  266. struct pci_dev *pdev = bp->pdev;
  267. u32 nr_pages, size, i, j, k = 0;
  268. bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
  269. if (!bp->pf.vf)
  270. return -ENOMEM;
  271. bnxt_set_vf_attr(bp, num_vfs);
  272. size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
  273. nr_pages = size / BNXT_PAGE_SIZE;
  274. if (size & (BNXT_PAGE_SIZE - 1))
  275. nr_pages++;
  276. for (i = 0; i < nr_pages; i++) {
  277. bp->pf.hwrm_cmd_req_addr[i] =
  278. dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
  279. &bp->pf.hwrm_cmd_req_dma_addr[i],
  280. GFP_KERNEL);
  281. if (!bp->pf.hwrm_cmd_req_addr[i])
  282. return -ENOMEM;
  283. for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
  284. struct bnxt_vf_info *vf = &bp->pf.vf[k];
  285. vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
  286. j * BNXT_HWRM_REQ_MAX_SIZE;
  287. vf->hwrm_cmd_req_dma_addr =
  288. bp->pf.hwrm_cmd_req_dma_addr[i] + j *
  289. BNXT_HWRM_REQ_MAX_SIZE;
  290. k++;
  291. }
  292. }
  293. /* Max 128 VF's */
  294. bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
  295. if (!bp->pf.vf_event_bmap)
  296. return -ENOMEM;
  297. bp->pf.hwrm_cmd_req_pages = nr_pages;
  298. return 0;
  299. }
  300. static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
  301. {
  302. struct hwrm_func_buf_rgtr_input req = {0};
  303. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
  304. req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
  305. req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
  306. req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
  307. req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
  308. req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
  309. req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
  310. req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
  311. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  312. }
  313. /* only call by PF to reserve resources for VF */
  314. static int bnxt_hwrm_func_cfg(struct bnxt *bp, int *num_vfs)
  315. {
  316. u32 rc = 0, mtu, i;
  317. u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
  318. struct hwrm_func_cfg_input req = {0};
  319. struct bnxt_pf_info *pf = &bp->pf;
  320. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  321. /* Remaining rings are distributed equally amongs VF's for now */
  322. /* TODO: the following workaroud is needed to restrict total number
  323. * of vf_cp_rings not exceed number of HW ring groups. This WA should
  324. * be removed once new HWRM provides HW ring groups capability in
  325. * hwrm_func_qcap.
  326. */
  327. vf_cp_rings = min_t(u16, bp->pf.max_cp_rings, bp->pf.max_stat_ctxs);
  328. vf_cp_rings = (vf_cp_rings - bp->cp_nr_rings) / *num_vfs;
  329. /* TODO: restore this logic below once the WA above is removed */
  330. /* vf_cp_rings = (bp->pf.max_cp_rings - bp->cp_nr_rings) / *num_vfs; */
  331. vf_stat_ctx = (bp->pf.max_stat_ctxs - bp->num_stat_ctxs) / *num_vfs;
  332. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  333. vf_rx_rings = (bp->pf.max_rx_rings - bp->rx_nr_rings * 2) /
  334. *num_vfs;
  335. else
  336. vf_rx_rings = (bp->pf.max_rx_rings - bp->rx_nr_rings) /
  337. *num_vfs;
  338. vf_tx_rings = (bp->pf.max_tx_rings - bp->tx_nr_rings) / *num_vfs;
  339. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
  340. FUNC_CFG_REQ_ENABLES_MRU |
  341. FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
  342. FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
  343. FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
  344. FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
  345. FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
  346. FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
  347. FUNC_CFG_REQ_ENABLES_NUM_VNICS);
  348. mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  349. req.mru = cpu_to_le16(mtu);
  350. req.mtu = cpu_to_le16(mtu);
  351. req.num_rsscos_ctxs = cpu_to_le16(1);
  352. req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
  353. req.num_tx_rings = cpu_to_le16(vf_tx_rings);
  354. req.num_rx_rings = cpu_to_le16(vf_rx_rings);
  355. req.num_l2_ctxs = cpu_to_le16(4);
  356. vf_vnics = 1;
  357. req.num_vnics = cpu_to_le16(vf_vnics);
  358. /* FIXME spec currently uses 1 bit for stats ctx */
  359. req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
  360. mutex_lock(&bp->hwrm_cmd_lock);
  361. for (i = 0; i < *num_vfs; i++) {
  362. req.vf_id = cpu_to_le16(pf->first_vf_id + i);
  363. rc = _hwrm_send_message(bp, &req, sizeof(req),
  364. HWRM_CMD_TIMEOUT);
  365. if (rc)
  366. break;
  367. bp->pf.active_vfs = i + 1;
  368. bp->pf.vf[i].fw_fid = le16_to_cpu(req.vf_id);
  369. }
  370. mutex_unlock(&bp->hwrm_cmd_lock);
  371. if (!rc) {
  372. bp->pf.max_pf_tx_rings = bp->tx_nr_rings;
  373. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  374. bp->pf.max_pf_rx_rings = bp->rx_nr_rings * 2;
  375. else
  376. bp->pf.max_pf_rx_rings = bp->rx_nr_rings;
  377. }
  378. return rc;
  379. }
  380. static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
  381. {
  382. int rc = 0, vfs_supported;
  383. int min_rx_rings, min_tx_rings, min_rss_ctxs;
  384. int tx_ok = 0, rx_ok = 0, rss_ok = 0;
  385. /* Check if we can enable requested num of vf's. At a mininum
  386. * we require 1 RX 1 TX rings for each VF. In this minimum conf
  387. * features like TPA will not be available.
  388. */
  389. vfs_supported = *num_vfs;
  390. while (vfs_supported) {
  391. min_rx_rings = vfs_supported;
  392. min_tx_rings = vfs_supported;
  393. min_rss_ctxs = vfs_supported;
  394. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  395. if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
  396. min_rx_rings)
  397. rx_ok = 1;
  398. } else {
  399. if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
  400. min_rx_rings)
  401. rx_ok = 1;
  402. }
  403. if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings)
  404. tx_ok = 1;
  405. if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
  406. rss_ok = 1;
  407. if (tx_ok && rx_ok && rss_ok)
  408. break;
  409. vfs_supported--;
  410. }
  411. if (!vfs_supported) {
  412. netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
  413. return -EINVAL;
  414. }
  415. if (vfs_supported != *num_vfs) {
  416. netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
  417. *num_vfs, vfs_supported);
  418. *num_vfs = vfs_supported;
  419. }
  420. rc = bnxt_alloc_vf_resources(bp, *num_vfs);
  421. if (rc)
  422. goto err_out1;
  423. /* Reserve resources for VFs */
  424. rc = bnxt_hwrm_func_cfg(bp, num_vfs);
  425. if (rc)
  426. goto err_out2;
  427. /* Register buffers for VFs */
  428. rc = bnxt_hwrm_func_buf_rgtr(bp);
  429. if (rc)
  430. goto err_out2;
  431. rc = pci_enable_sriov(bp->pdev, *num_vfs);
  432. if (rc)
  433. goto err_out2;
  434. return 0;
  435. err_out2:
  436. /* Free the resources reserved for various VF's */
  437. bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
  438. err_out1:
  439. bnxt_free_vf_resources(bp);
  440. return rc;
  441. }
  442. void bnxt_sriov_disable(struct bnxt *bp)
  443. {
  444. u16 num_vfs = pci_num_vf(bp->pdev);
  445. if (!num_vfs)
  446. return;
  447. if (pci_vfs_assigned(bp->pdev)) {
  448. netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
  449. num_vfs);
  450. } else {
  451. pci_disable_sriov(bp->pdev);
  452. /* Free the HW resources reserved for various VF's */
  453. bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
  454. }
  455. bnxt_free_vf_resources(bp);
  456. bp->pf.active_vfs = 0;
  457. bp->pf.max_pf_rx_rings = bp->pf.max_rx_rings;
  458. bp->pf.max_pf_tx_rings = bp->pf.max_tx_rings;
  459. }
  460. int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
  461. {
  462. struct net_device *dev = pci_get_drvdata(pdev);
  463. struct bnxt *bp = netdev_priv(dev);
  464. if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
  465. netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
  466. return 0;
  467. }
  468. rtnl_lock();
  469. if (!netif_running(dev)) {
  470. netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
  471. rtnl_unlock();
  472. return 0;
  473. }
  474. bp->sriov_cfg = true;
  475. rtnl_unlock();
  476. if (pci_vfs_assigned(bp->pdev)) {
  477. netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
  478. num_vfs = 0;
  479. goto sriov_cfg_exit;
  480. }
  481. /* Check if enabled VFs is same as requested */
  482. if (num_vfs && num_vfs == bp->pf.active_vfs)
  483. goto sriov_cfg_exit;
  484. /* if there are previous existing VFs, clean them up */
  485. bnxt_sriov_disable(bp);
  486. if (!num_vfs)
  487. goto sriov_cfg_exit;
  488. bnxt_sriov_enable(bp, &num_vfs);
  489. sriov_cfg_exit:
  490. bp->sriov_cfg = false;
  491. wake_up(&bp->sriov_cfg_wait);
  492. return num_vfs;
  493. }
  494. static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  495. void *encap_resp, __le64 encap_resp_addr,
  496. __le16 encap_resp_cpr, u32 msg_size)
  497. {
  498. int rc = 0;
  499. struct hwrm_fwd_resp_input req = {0};
  500. struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  501. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
  502. /* Set the new target id */
  503. req.target_id = cpu_to_le16(vf->fw_fid);
  504. req.encap_resp_len = cpu_to_le16(msg_size);
  505. req.encap_resp_addr = encap_resp_addr;
  506. req.encap_resp_cmpl_ring = encap_resp_cpr;
  507. memcpy(req.encap_resp, encap_resp, msg_size);
  508. mutex_lock(&bp->hwrm_cmd_lock);
  509. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  510. if (rc) {
  511. netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
  512. goto fwd_resp_exit;
  513. }
  514. if (resp->error_code) {
  515. netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
  516. resp->error_code);
  517. rc = -1;
  518. }
  519. fwd_resp_exit:
  520. mutex_unlock(&bp->hwrm_cmd_lock);
  521. return rc;
  522. }
  523. static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  524. u32 msg_size)
  525. {
  526. int rc = 0;
  527. struct hwrm_reject_fwd_resp_input req = {0};
  528. struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  529. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
  530. /* Set the new target id */
  531. req.target_id = cpu_to_le16(vf->fw_fid);
  532. memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
  533. mutex_lock(&bp->hwrm_cmd_lock);
  534. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  535. if (rc) {
  536. netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
  537. goto fwd_err_resp_exit;
  538. }
  539. if (resp->error_code) {
  540. netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
  541. resp->error_code);
  542. rc = -1;
  543. }
  544. fwd_err_resp_exit:
  545. mutex_unlock(&bp->hwrm_cmd_lock);
  546. return rc;
  547. }
  548. static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  549. u32 msg_size)
  550. {
  551. int rc = 0;
  552. struct hwrm_exec_fwd_resp_input req = {0};
  553. struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  554. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
  555. /* Set the new target id */
  556. req.target_id = cpu_to_le16(vf->fw_fid);
  557. memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
  558. mutex_lock(&bp->hwrm_cmd_lock);
  559. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  560. if (rc) {
  561. netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
  562. goto exec_fwd_resp_exit;
  563. }
  564. if (resp->error_code) {
  565. netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
  566. resp->error_code);
  567. rc = -1;
  568. }
  569. exec_fwd_resp_exit:
  570. mutex_unlock(&bp->hwrm_cmd_lock);
  571. return rc;
  572. }
  573. static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
  574. {
  575. u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
  576. struct hwrm_cfa_l2_filter_alloc_input *req =
  577. (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
  578. if (!is_valid_ether_addr(vf->mac_addr) ||
  579. ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
  580. return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
  581. else
  582. return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
  583. }
  584. static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
  585. {
  586. int rc = 0;
  587. if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
  588. /* real link */
  589. rc = bnxt_hwrm_exec_fwd_resp(
  590. bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
  591. } else {
  592. struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
  593. struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
  594. phy_qcfg_req =
  595. (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
  596. mutex_lock(&bp->hwrm_cmd_lock);
  597. memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
  598. sizeof(phy_qcfg_resp));
  599. mutex_unlock(&bp->hwrm_cmd_lock);
  600. phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
  601. if (vf->flags & BNXT_VF_LINK_UP) {
  602. /* if physical link is down, force link up on VF */
  603. if (phy_qcfg_resp.link ==
  604. PORT_PHY_QCFG_RESP_LINK_NO_LINK) {
  605. phy_qcfg_resp.link =
  606. PORT_PHY_QCFG_RESP_LINK_LINK;
  607. if (phy_qcfg_resp.auto_link_speed)
  608. phy_qcfg_resp.link_speed =
  609. phy_qcfg_resp.auto_link_speed;
  610. else
  611. phy_qcfg_resp.link_speed =
  612. phy_qcfg_resp.force_link_speed;
  613. phy_qcfg_resp.duplex =
  614. PORT_PHY_QCFG_RESP_DUPLEX_FULL;
  615. phy_qcfg_resp.pause =
  616. (PORT_PHY_QCFG_RESP_PAUSE_TX |
  617. PORT_PHY_QCFG_RESP_PAUSE_RX);
  618. }
  619. } else {
  620. /* force link down */
  621. phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
  622. phy_qcfg_resp.link_speed = 0;
  623. phy_qcfg_resp.duplex = PORT_PHY_QCFG_RESP_DUPLEX_HALF;
  624. phy_qcfg_resp.pause = 0;
  625. }
  626. rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
  627. phy_qcfg_req->resp_addr,
  628. phy_qcfg_req->cmpl_ring,
  629. sizeof(phy_qcfg_resp));
  630. }
  631. return rc;
  632. }
  633. static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
  634. {
  635. int rc = 0;
  636. struct hwrm_cmd_req_hdr *encap_req = vf->hwrm_cmd_req_addr;
  637. u32 req_type = le32_to_cpu(encap_req->cmpl_ring_req_type) & 0xffff;
  638. switch (req_type) {
  639. case HWRM_CFA_L2_FILTER_ALLOC:
  640. rc = bnxt_vf_validate_set_mac(bp, vf);
  641. break;
  642. case HWRM_FUNC_CFG:
  643. /* TODO Validate if VF is allowed to change mac address,
  644. * mtu, num of rings etc
  645. */
  646. rc = bnxt_hwrm_exec_fwd_resp(
  647. bp, vf, sizeof(struct hwrm_func_cfg_input));
  648. break;
  649. case HWRM_PORT_PHY_QCFG:
  650. rc = bnxt_vf_set_link(bp, vf);
  651. break;
  652. default:
  653. break;
  654. }
  655. return rc;
  656. }
  657. void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
  658. {
  659. u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
  660. /* Scan through VF's and process commands */
  661. while (1) {
  662. vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
  663. if (vf_id >= active_vfs)
  664. break;
  665. clear_bit(vf_id, bp->pf.vf_event_bmap);
  666. bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
  667. i = vf_id + 1;
  668. }
  669. }
  670. void bnxt_update_vf_mac(struct bnxt *bp)
  671. {
  672. struct hwrm_func_qcaps_input req = {0};
  673. struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  674. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
  675. req.fid = cpu_to_le16(0xffff);
  676. mutex_lock(&bp->hwrm_cmd_lock);
  677. if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
  678. goto update_vf_mac_exit;
  679. if (!is_valid_ether_addr(resp->perm_mac_address))
  680. goto update_vf_mac_exit;
  681. if (!ether_addr_equal(resp->perm_mac_address, bp->vf.mac_addr))
  682. memcpy(bp->vf.mac_addr, resp->perm_mac_address, ETH_ALEN);
  683. /* overwrite netdev dev_adr with admin VF MAC */
  684. memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
  685. update_vf_mac_exit:
  686. mutex_unlock(&bp->hwrm_cmd_lock);
  687. }
  688. #else
  689. void bnxt_sriov_disable(struct bnxt *bp)
  690. {
  691. }
  692. void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
  693. {
  694. netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
  695. }
  696. void bnxt_update_vf_mac(struct bnxt *bp)
  697. {
  698. }
  699. #endif