bnxt_sriov.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2014-2016 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.max_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.fid = 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.fid = 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. if (bp->hwrm_spec_code < 0x10201)
  131. return -ENOTSUPP;
  132. rc = bnxt_vf_ndo_prep(bp, vf_id);
  133. if (rc)
  134. return rc;
  135. /* TODO: needed to implement proper handling of user priority,
  136. * currently fail the command if there is valid priority
  137. */
  138. if (vlan_id > 4095 || qos)
  139. return -EINVAL;
  140. vf = &bp->pf.vf[vf_id];
  141. vlan_tag = vlan_id;
  142. if (vlan_tag == vf->vlan)
  143. return 0;
  144. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  145. req.fid = cpu_to_le16(vf->fw_fid);
  146. req.flags = cpu_to_le32(vf->func_flags);
  147. req.dflt_vlan = cpu_to_le16(vlan_tag);
  148. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
  149. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  150. if (!rc)
  151. vf->vlan = vlan_tag;
  152. return rc;
  153. }
  154. int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
  155. int max_tx_rate)
  156. {
  157. struct hwrm_func_cfg_input req = {0};
  158. struct bnxt *bp = netdev_priv(dev);
  159. struct bnxt_vf_info *vf;
  160. u32 pf_link_speed;
  161. int rc;
  162. rc = bnxt_vf_ndo_prep(bp, vf_id);
  163. if (rc)
  164. return rc;
  165. vf = &bp->pf.vf[vf_id];
  166. pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
  167. if (max_tx_rate > pf_link_speed) {
  168. netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
  169. max_tx_rate, vf_id);
  170. return -EINVAL;
  171. }
  172. if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) {
  173. netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
  174. min_tx_rate, vf_id);
  175. return -EINVAL;
  176. }
  177. if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
  178. return 0;
  179. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  180. req.fid = cpu_to_le16(vf->fw_fid);
  181. req.flags = cpu_to_le32(vf->func_flags);
  182. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
  183. req.max_bw = cpu_to_le32(max_tx_rate);
  184. req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
  185. req.min_bw = cpu_to_le32(min_tx_rate);
  186. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  187. if (!rc) {
  188. vf->min_tx_rate = min_tx_rate;
  189. vf->max_tx_rate = max_tx_rate;
  190. }
  191. return rc;
  192. }
  193. int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
  194. {
  195. struct bnxt *bp = netdev_priv(dev);
  196. struct bnxt_vf_info *vf;
  197. int rc;
  198. rc = bnxt_vf_ndo_prep(bp, vf_id);
  199. if (rc)
  200. return rc;
  201. vf = &bp->pf.vf[vf_id];
  202. vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
  203. switch (link) {
  204. case IFLA_VF_LINK_STATE_AUTO:
  205. vf->flags |= BNXT_VF_LINK_UP;
  206. break;
  207. case IFLA_VF_LINK_STATE_DISABLE:
  208. vf->flags |= BNXT_VF_LINK_FORCED;
  209. break;
  210. case IFLA_VF_LINK_STATE_ENABLE:
  211. vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
  212. break;
  213. default:
  214. netdev_err(bp->dev, "Invalid link option\n");
  215. rc = -EINVAL;
  216. break;
  217. }
  218. /* CHIMP TODO: send msg to VF to update new link state */
  219. return rc;
  220. }
  221. static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
  222. {
  223. int i;
  224. struct bnxt_vf_info *vf;
  225. for (i = 0; i < num_vfs; i++) {
  226. vf = &bp->pf.vf[i];
  227. memset(vf, 0, sizeof(*vf));
  228. vf->flags = BNXT_VF_QOS | BNXT_VF_LINK_UP;
  229. }
  230. return 0;
  231. }
  232. static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
  233. {
  234. int i, rc = 0;
  235. struct bnxt_pf_info *pf = &bp->pf;
  236. struct hwrm_func_vf_resc_free_input req = {0};
  237. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
  238. mutex_lock(&bp->hwrm_cmd_lock);
  239. for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
  240. req.vf_id = cpu_to_le16(i);
  241. rc = _hwrm_send_message(bp, &req, sizeof(req),
  242. HWRM_CMD_TIMEOUT);
  243. if (rc)
  244. break;
  245. }
  246. mutex_unlock(&bp->hwrm_cmd_lock);
  247. return rc;
  248. }
  249. static void bnxt_free_vf_resources(struct bnxt *bp)
  250. {
  251. struct pci_dev *pdev = bp->pdev;
  252. int i;
  253. kfree(bp->pf.vf_event_bmap);
  254. bp->pf.vf_event_bmap = NULL;
  255. for (i = 0; i < 4; i++) {
  256. if (bp->pf.hwrm_cmd_req_addr[i]) {
  257. dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
  258. bp->pf.hwrm_cmd_req_addr[i],
  259. bp->pf.hwrm_cmd_req_dma_addr[i]);
  260. bp->pf.hwrm_cmd_req_addr[i] = NULL;
  261. }
  262. }
  263. kfree(bp->pf.vf);
  264. bp->pf.vf = NULL;
  265. }
  266. static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
  267. {
  268. struct pci_dev *pdev = bp->pdev;
  269. u32 nr_pages, size, i, j, k = 0;
  270. bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
  271. if (!bp->pf.vf)
  272. return -ENOMEM;
  273. bnxt_set_vf_attr(bp, num_vfs);
  274. size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
  275. nr_pages = size / BNXT_PAGE_SIZE;
  276. if (size & (BNXT_PAGE_SIZE - 1))
  277. nr_pages++;
  278. for (i = 0; i < nr_pages; i++) {
  279. bp->pf.hwrm_cmd_req_addr[i] =
  280. dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
  281. &bp->pf.hwrm_cmd_req_dma_addr[i],
  282. GFP_KERNEL);
  283. if (!bp->pf.hwrm_cmd_req_addr[i])
  284. return -ENOMEM;
  285. for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
  286. struct bnxt_vf_info *vf = &bp->pf.vf[k];
  287. vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
  288. j * BNXT_HWRM_REQ_MAX_SIZE;
  289. vf->hwrm_cmd_req_dma_addr =
  290. bp->pf.hwrm_cmd_req_dma_addr[i] + j *
  291. BNXT_HWRM_REQ_MAX_SIZE;
  292. k++;
  293. }
  294. }
  295. /* Max 128 VF's */
  296. bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
  297. if (!bp->pf.vf_event_bmap)
  298. return -ENOMEM;
  299. bp->pf.hwrm_cmd_req_pages = nr_pages;
  300. return 0;
  301. }
  302. static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
  303. {
  304. struct hwrm_func_buf_rgtr_input req = {0};
  305. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
  306. req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
  307. req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
  308. req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
  309. req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
  310. req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
  311. req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
  312. req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
  313. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  314. }
  315. /* only call by PF to reserve resources for VF */
  316. static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
  317. {
  318. u32 rc = 0, mtu, i;
  319. u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
  320. u16 vf_ring_grps;
  321. struct hwrm_func_cfg_input req = {0};
  322. struct bnxt_pf_info *pf = &bp->pf;
  323. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  324. /* Remaining rings are distributed equally amongs VF's for now */
  325. /* TODO: the following workaroud is needed to restrict total number
  326. * of vf_cp_rings not exceed number of HW ring groups. This WA should
  327. * be removed once new HWRM provides HW ring groups capability in
  328. * hwrm_func_qcap.
  329. */
  330. vf_cp_rings = min_t(u16, pf->max_cp_rings, pf->max_stat_ctxs);
  331. vf_cp_rings = (vf_cp_rings - bp->cp_nr_rings) / num_vfs;
  332. /* TODO: restore this logic below once the WA above is removed */
  333. /* vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs; */
  334. vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs;
  335. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  336. vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) /
  337. num_vfs;
  338. else
  339. vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs;
  340. vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
  341. vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs;
  342. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
  343. FUNC_CFG_REQ_ENABLES_MRU |
  344. FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
  345. FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
  346. FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
  347. FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
  348. FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
  349. FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
  350. FUNC_CFG_REQ_ENABLES_NUM_VNICS |
  351. FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
  352. mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  353. req.mru = cpu_to_le16(mtu);
  354. req.mtu = cpu_to_le16(mtu);
  355. req.num_rsscos_ctxs = cpu_to_le16(1);
  356. req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
  357. req.num_tx_rings = cpu_to_le16(vf_tx_rings);
  358. req.num_rx_rings = cpu_to_le16(vf_rx_rings);
  359. req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
  360. req.num_l2_ctxs = cpu_to_le16(4);
  361. vf_vnics = 1;
  362. req.num_vnics = cpu_to_le16(vf_vnics);
  363. /* FIXME spec currently uses 1 bit for stats ctx */
  364. req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
  365. mutex_lock(&bp->hwrm_cmd_lock);
  366. for (i = 0; i < num_vfs; i++) {
  367. req.fid = cpu_to_le16(pf->first_vf_id + i);
  368. rc = _hwrm_send_message(bp, &req, sizeof(req),
  369. HWRM_CMD_TIMEOUT);
  370. if (rc)
  371. break;
  372. pf->active_vfs = i + 1;
  373. pf->vf[i].fw_fid = le16_to_cpu(req.fid);
  374. }
  375. mutex_unlock(&bp->hwrm_cmd_lock);
  376. if (!rc) {
  377. pf->max_tx_rings -= vf_tx_rings * num_vfs;
  378. pf->max_rx_rings -= vf_rx_rings * num_vfs;
  379. pf->max_hw_ring_grps -= vf_ring_grps * num_vfs;
  380. pf->max_cp_rings -= vf_cp_rings * num_vfs;
  381. pf->max_rsscos_ctxs -= num_vfs;
  382. pf->max_stat_ctxs -= vf_stat_ctx * num_vfs;
  383. pf->max_vnics -= vf_vnics * num_vfs;
  384. }
  385. return rc;
  386. }
  387. static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
  388. {
  389. int rc = 0, vfs_supported;
  390. int min_rx_rings, min_tx_rings, min_rss_ctxs;
  391. int tx_ok = 0, rx_ok = 0, rss_ok = 0;
  392. /* Check if we can enable requested num of vf's. At a mininum
  393. * we require 1 RX 1 TX rings for each VF. In this minimum conf
  394. * features like TPA will not be available.
  395. */
  396. vfs_supported = *num_vfs;
  397. while (vfs_supported) {
  398. min_rx_rings = vfs_supported;
  399. min_tx_rings = vfs_supported;
  400. min_rss_ctxs = vfs_supported;
  401. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  402. if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
  403. min_rx_rings)
  404. rx_ok = 1;
  405. } else {
  406. if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
  407. min_rx_rings)
  408. rx_ok = 1;
  409. }
  410. if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings)
  411. tx_ok = 1;
  412. if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
  413. rss_ok = 1;
  414. if (tx_ok && rx_ok && rss_ok)
  415. break;
  416. vfs_supported--;
  417. }
  418. if (!vfs_supported) {
  419. netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
  420. return -EINVAL;
  421. }
  422. if (vfs_supported != *num_vfs) {
  423. netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
  424. *num_vfs, vfs_supported);
  425. *num_vfs = vfs_supported;
  426. }
  427. rc = bnxt_alloc_vf_resources(bp, *num_vfs);
  428. if (rc)
  429. goto err_out1;
  430. /* Reserve resources for VFs */
  431. rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
  432. if (rc)
  433. goto err_out2;
  434. /* Register buffers for VFs */
  435. rc = bnxt_hwrm_func_buf_rgtr(bp);
  436. if (rc)
  437. goto err_out2;
  438. rc = pci_enable_sriov(bp->pdev, *num_vfs);
  439. if (rc)
  440. goto err_out2;
  441. return 0;
  442. err_out2:
  443. /* Free the resources reserved for various VF's */
  444. bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
  445. err_out1:
  446. bnxt_free_vf_resources(bp);
  447. return rc;
  448. }
  449. static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp,
  450. struct bnxt_vf_info *vf,
  451. u16 event_id)
  452. {
  453. int rc = 0;
  454. struct hwrm_fwd_async_event_cmpl_input req = {0};
  455. struct hwrm_fwd_async_event_cmpl_output *resp = bp->hwrm_cmd_resp_addr;
  456. struct hwrm_async_event_cmpl *async_cmpl;
  457. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1);
  458. if (vf)
  459. req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
  460. else
  461. /* broadcast this async event to all VFs */
  462. req.encap_async_event_target_id = cpu_to_le16(0xffff);
  463. async_cmpl = (struct hwrm_async_event_cmpl *)req.encap_async_event_cmpl;
  464. async_cmpl->type =
  465. cpu_to_le16(HWRM_ASYNC_EVENT_CMPL_TYPE_HWRM_ASYNC_EVENT);
  466. async_cmpl->event_id = cpu_to_le16(event_id);
  467. mutex_lock(&bp->hwrm_cmd_lock);
  468. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  469. if (rc) {
  470. netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
  471. rc);
  472. goto fwd_async_event_cmpl_exit;
  473. }
  474. if (resp->error_code) {
  475. netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n",
  476. resp->error_code);
  477. rc = -1;
  478. }
  479. fwd_async_event_cmpl_exit:
  480. mutex_unlock(&bp->hwrm_cmd_lock);
  481. return rc;
  482. }
  483. void bnxt_sriov_disable(struct bnxt *bp)
  484. {
  485. u16 num_vfs = pci_num_vf(bp->pdev);
  486. if (!num_vfs)
  487. return;
  488. if (pci_vfs_assigned(bp->pdev)) {
  489. bnxt_hwrm_fwd_async_event_cmpl(
  490. bp, NULL,
  491. HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
  492. netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
  493. num_vfs);
  494. } else {
  495. pci_disable_sriov(bp->pdev);
  496. /* Free the HW resources reserved for various VF's */
  497. bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
  498. }
  499. bnxt_free_vf_resources(bp);
  500. bp->pf.active_vfs = 0;
  501. /* Reclaim all resources for the PF. */
  502. bnxt_hwrm_func_qcaps(bp);
  503. }
  504. int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
  505. {
  506. struct net_device *dev = pci_get_drvdata(pdev);
  507. struct bnxt *bp = netdev_priv(dev);
  508. if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
  509. netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
  510. return 0;
  511. }
  512. rtnl_lock();
  513. if (!netif_running(dev)) {
  514. netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
  515. rtnl_unlock();
  516. return 0;
  517. }
  518. bp->sriov_cfg = true;
  519. rtnl_unlock();
  520. if (pci_vfs_assigned(bp->pdev)) {
  521. netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
  522. num_vfs = 0;
  523. goto sriov_cfg_exit;
  524. }
  525. /* Check if enabled VFs is same as requested */
  526. if (num_vfs && num_vfs == bp->pf.active_vfs)
  527. goto sriov_cfg_exit;
  528. /* if there are previous existing VFs, clean them up */
  529. bnxt_sriov_disable(bp);
  530. if (!num_vfs)
  531. goto sriov_cfg_exit;
  532. bnxt_sriov_enable(bp, &num_vfs);
  533. sriov_cfg_exit:
  534. bp->sriov_cfg = false;
  535. wake_up(&bp->sriov_cfg_wait);
  536. return num_vfs;
  537. }
  538. static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  539. void *encap_resp, __le64 encap_resp_addr,
  540. __le16 encap_resp_cpr, u32 msg_size)
  541. {
  542. int rc = 0;
  543. struct hwrm_fwd_resp_input req = {0};
  544. struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  545. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
  546. /* Set the new target id */
  547. req.target_id = cpu_to_le16(vf->fw_fid);
  548. req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
  549. req.encap_resp_len = cpu_to_le16(msg_size);
  550. req.encap_resp_addr = encap_resp_addr;
  551. req.encap_resp_cmpl_ring = encap_resp_cpr;
  552. memcpy(req.encap_resp, encap_resp, msg_size);
  553. mutex_lock(&bp->hwrm_cmd_lock);
  554. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  555. if (rc) {
  556. netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
  557. goto fwd_resp_exit;
  558. }
  559. if (resp->error_code) {
  560. netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
  561. resp->error_code);
  562. rc = -1;
  563. }
  564. fwd_resp_exit:
  565. mutex_unlock(&bp->hwrm_cmd_lock);
  566. return rc;
  567. }
  568. static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  569. u32 msg_size)
  570. {
  571. int rc = 0;
  572. struct hwrm_reject_fwd_resp_input req = {0};
  573. struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  574. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
  575. /* Set the new target id */
  576. req.target_id = cpu_to_le16(vf->fw_fid);
  577. req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
  578. memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
  579. mutex_lock(&bp->hwrm_cmd_lock);
  580. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  581. if (rc) {
  582. netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
  583. goto fwd_err_resp_exit;
  584. }
  585. if (resp->error_code) {
  586. netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
  587. resp->error_code);
  588. rc = -1;
  589. }
  590. fwd_err_resp_exit:
  591. mutex_unlock(&bp->hwrm_cmd_lock);
  592. return rc;
  593. }
  594. static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  595. u32 msg_size)
  596. {
  597. int rc = 0;
  598. struct hwrm_exec_fwd_resp_input req = {0};
  599. struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  600. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
  601. /* Set the new target id */
  602. req.target_id = cpu_to_le16(vf->fw_fid);
  603. req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
  604. memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
  605. mutex_lock(&bp->hwrm_cmd_lock);
  606. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  607. if (rc) {
  608. netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
  609. goto exec_fwd_resp_exit;
  610. }
  611. if (resp->error_code) {
  612. netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
  613. resp->error_code);
  614. rc = -1;
  615. }
  616. exec_fwd_resp_exit:
  617. mutex_unlock(&bp->hwrm_cmd_lock);
  618. return rc;
  619. }
  620. static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
  621. {
  622. u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
  623. struct hwrm_cfa_l2_filter_alloc_input *req =
  624. (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
  625. if (!is_valid_ether_addr(vf->mac_addr) ||
  626. ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
  627. return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
  628. else
  629. return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
  630. }
  631. static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
  632. {
  633. int rc = 0;
  634. if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
  635. /* real link */
  636. rc = bnxt_hwrm_exec_fwd_resp(
  637. bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
  638. } else {
  639. struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
  640. struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
  641. phy_qcfg_req =
  642. (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
  643. mutex_lock(&bp->hwrm_cmd_lock);
  644. memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
  645. sizeof(phy_qcfg_resp));
  646. mutex_unlock(&bp->hwrm_cmd_lock);
  647. phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
  648. if (vf->flags & BNXT_VF_LINK_UP) {
  649. /* if physical link is down, force link up on VF */
  650. if (phy_qcfg_resp.link ==
  651. PORT_PHY_QCFG_RESP_LINK_NO_LINK) {
  652. phy_qcfg_resp.link =
  653. PORT_PHY_QCFG_RESP_LINK_LINK;
  654. phy_qcfg_resp.link_speed = cpu_to_le16(
  655. PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
  656. phy_qcfg_resp.duplex =
  657. PORT_PHY_QCFG_RESP_DUPLEX_FULL;
  658. phy_qcfg_resp.pause =
  659. (PORT_PHY_QCFG_RESP_PAUSE_TX |
  660. PORT_PHY_QCFG_RESP_PAUSE_RX);
  661. }
  662. } else {
  663. /* force link down */
  664. phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
  665. phy_qcfg_resp.link_speed = 0;
  666. phy_qcfg_resp.duplex = PORT_PHY_QCFG_RESP_DUPLEX_HALF;
  667. phy_qcfg_resp.pause = 0;
  668. }
  669. rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
  670. phy_qcfg_req->resp_addr,
  671. phy_qcfg_req->cmpl_ring,
  672. sizeof(phy_qcfg_resp));
  673. }
  674. return rc;
  675. }
  676. static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
  677. {
  678. int rc = 0;
  679. struct input *encap_req = vf->hwrm_cmd_req_addr;
  680. u32 req_type = le16_to_cpu(encap_req->req_type);
  681. switch (req_type) {
  682. case HWRM_CFA_L2_FILTER_ALLOC:
  683. rc = bnxt_vf_validate_set_mac(bp, vf);
  684. break;
  685. case HWRM_FUNC_CFG:
  686. /* TODO Validate if VF is allowed to change mac address,
  687. * mtu, num of rings etc
  688. */
  689. rc = bnxt_hwrm_exec_fwd_resp(
  690. bp, vf, sizeof(struct hwrm_func_cfg_input));
  691. break;
  692. case HWRM_PORT_PHY_QCFG:
  693. rc = bnxt_vf_set_link(bp, vf);
  694. break;
  695. default:
  696. break;
  697. }
  698. return rc;
  699. }
  700. void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
  701. {
  702. u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
  703. /* Scan through VF's and process commands */
  704. while (1) {
  705. vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
  706. if (vf_id >= active_vfs)
  707. break;
  708. clear_bit(vf_id, bp->pf.vf_event_bmap);
  709. bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
  710. i = vf_id + 1;
  711. }
  712. }
  713. void bnxt_update_vf_mac(struct bnxt *bp)
  714. {
  715. struct hwrm_func_qcaps_input req = {0};
  716. struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  717. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
  718. req.fid = cpu_to_le16(0xffff);
  719. mutex_lock(&bp->hwrm_cmd_lock);
  720. if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
  721. goto update_vf_mac_exit;
  722. /* Store MAC address from the firmware. There are 2 cases:
  723. * 1. MAC address is valid. It is assigned from the PF and we
  724. * need to override the current VF MAC address with it.
  725. * 2. MAC address is zero. The VF will use a random MAC address by
  726. * default but the stored zero MAC will allow the VF user to change
  727. * the random MAC address using ndo_set_mac_address() if he wants.
  728. */
  729. if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr))
  730. memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
  731. /* overwrite netdev dev_addr with admin VF MAC */
  732. if (is_valid_ether_addr(bp->vf.mac_addr))
  733. memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
  734. update_vf_mac_exit:
  735. mutex_unlock(&bp->hwrm_cmd_lock);
  736. }
  737. int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
  738. {
  739. struct hwrm_func_vf_cfg_input req = {0};
  740. int rc = 0;
  741. if (!BNXT_VF(bp))
  742. return 0;
  743. if (bp->hwrm_spec_code < 0x10202) {
  744. if (is_valid_ether_addr(bp->vf.mac_addr))
  745. rc = -EADDRNOTAVAIL;
  746. goto mac_done;
  747. }
  748. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
  749. req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
  750. memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
  751. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  752. mac_done:
  753. if (rc) {
  754. rc = -EADDRNOTAVAIL;
  755. netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
  756. mac);
  757. }
  758. return rc;
  759. }
  760. #else
  761. void bnxt_sriov_disable(struct bnxt *bp)
  762. {
  763. }
  764. void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
  765. {
  766. netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
  767. }
  768. void bnxt_update_vf_mac(struct bnxt *bp)
  769. {
  770. }
  771. int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
  772. {
  773. return 0;
  774. }
  775. #endif