bnxt_sriov.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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.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. 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.fid = 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.fid = 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. u16 vf_ring_grps;
  319. struct hwrm_func_cfg_input req = {0};
  320. struct bnxt_pf_info *pf = &bp->pf;
  321. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  322. /* Remaining rings are distributed equally amongs VF's for now */
  323. /* TODO: the following workaroud is needed to restrict total number
  324. * of vf_cp_rings not exceed number of HW ring groups. This WA should
  325. * be removed once new HWRM provides HW ring groups capability in
  326. * hwrm_func_qcap.
  327. */
  328. vf_cp_rings = min_t(u16, pf->max_cp_rings, pf->max_stat_ctxs);
  329. vf_cp_rings = (vf_cp_rings - bp->cp_nr_rings) / num_vfs;
  330. /* TODO: restore this logic below once the WA above is removed */
  331. /* vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs; */
  332. vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs;
  333. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  334. vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) /
  335. num_vfs;
  336. else
  337. vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs;
  338. vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
  339. vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs;
  340. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
  341. FUNC_CFG_REQ_ENABLES_MRU |
  342. FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
  343. FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
  344. FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
  345. FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
  346. FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
  347. FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
  348. FUNC_CFG_REQ_ENABLES_NUM_VNICS |
  349. FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
  350. mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  351. req.mru = cpu_to_le16(mtu);
  352. req.mtu = cpu_to_le16(mtu);
  353. req.num_rsscos_ctxs = cpu_to_le16(1);
  354. req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
  355. req.num_tx_rings = cpu_to_le16(vf_tx_rings);
  356. req.num_rx_rings = cpu_to_le16(vf_rx_rings);
  357. req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
  358. req.num_l2_ctxs = cpu_to_le16(4);
  359. vf_vnics = 1;
  360. req.num_vnics = cpu_to_le16(vf_vnics);
  361. /* FIXME spec currently uses 1 bit for stats ctx */
  362. req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
  363. mutex_lock(&bp->hwrm_cmd_lock);
  364. for (i = 0; i < num_vfs; i++) {
  365. req.fid = cpu_to_le16(pf->first_vf_id + i);
  366. rc = _hwrm_send_message(bp, &req, sizeof(req),
  367. HWRM_CMD_TIMEOUT);
  368. if (rc)
  369. break;
  370. pf->active_vfs = i + 1;
  371. pf->vf[i].fw_fid = le16_to_cpu(req.fid);
  372. }
  373. mutex_unlock(&bp->hwrm_cmd_lock);
  374. if (!rc) {
  375. pf->max_tx_rings -= vf_tx_rings * num_vfs;
  376. pf->max_rx_rings -= vf_rx_rings * num_vfs;
  377. pf->max_hw_ring_grps -= vf_ring_grps * num_vfs;
  378. pf->max_cp_rings -= vf_cp_rings * num_vfs;
  379. pf->max_rsscos_ctxs -= num_vfs;
  380. pf->max_stat_ctxs -= vf_stat_ctx * num_vfs;
  381. pf->max_vnics -= vf_vnics * num_vfs;
  382. }
  383. return rc;
  384. }
  385. static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
  386. {
  387. int rc = 0, vfs_supported;
  388. int min_rx_rings, min_tx_rings, min_rss_ctxs;
  389. int tx_ok = 0, rx_ok = 0, rss_ok = 0;
  390. /* Check if we can enable requested num of vf's. At a mininum
  391. * we require 1 RX 1 TX rings for each VF. In this minimum conf
  392. * features like TPA will not be available.
  393. */
  394. vfs_supported = *num_vfs;
  395. while (vfs_supported) {
  396. min_rx_rings = vfs_supported;
  397. min_tx_rings = vfs_supported;
  398. min_rss_ctxs = vfs_supported;
  399. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  400. if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
  401. min_rx_rings)
  402. rx_ok = 1;
  403. } else {
  404. if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
  405. min_rx_rings)
  406. rx_ok = 1;
  407. }
  408. if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings)
  409. tx_ok = 1;
  410. if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
  411. rss_ok = 1;
  412. if (tx_ok && rx_ok && rss_ok)
  413. break;
  414. vfs_supported--;
  415. }
  416. if (!vfs_supported) {
  417. netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
  418. return -EINVAL;
  419. }
  420. if (vfs_supported != *num_vfs) {
  421. netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
  422. *num_vfs, vfs_supported);
  423. *num_vfs = vfs_supported;
  424. }
  425. rc = bnxt_alloc_vf_resources(bp, *num_vfs);
  426. if (rc)
  427. goto err_out1;
  428. /* Reserve resources for VFs */
  429. rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
  430. if (rc)
  431. goto err_out2;
  432. /* Register buffers for VFs */
  433. rc = bnxt_hwrm_func_buf_rgtr(bp);
  434. if (rc)
  435. goto err_out2;
  436. rc = pci_enable_sriov(bp->pdev, *num_vfs);
  437. if (rc)
  438. goto err_out2;
  439. return 0;
  440. err_out2:
  441. /* Free the resources reserved for various VF's */
  442. bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
  443. err_out1:
  444. bnxt_free_vf_resources(bp);
  445. return rc;
  446. }
  447. static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp,
  448. struct bnxt_vf_info *vf,
  449. u16 event_id)
  450. {
  451. int rc = 0;
  452. struct hwrm_fwd_async_event_cmpl_input req = {0};
  453. struct hwrm_fwd_async_event_cmpl_output *resp = bp->hwrm_cmd_resp_addr;
  454. struct hwrm_async_event_cmpl *async_cmpl;
  455. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1);
  456. if (vf)
  457. req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
  458. else
  459. /* broadcast this async event to all VFs */
  460. req.encap_async_event_target_id = cpu_to_le16(0xffff);
  461. async_cmpl = (struct hwrm_async_event_cmpl *)req.encap_async_event_cmpl;
  462. async_cmpl->type =
  463. cpu_to_le16(HWRM_ASYNC_EVENT_CMPL_TYPE_HWRM_ASYNC_EVENT);
  464. async_cmpl->event_id = cpu_to_le16(event_id);
  465. mutex_lock(&bp->hwrm_cmd_lock);
  466. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  467. if (rc) {
  468. netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
  469. rc);
  470. goto fwd_async_event_cmpl_exit;
  471. }
  472. if (resp->error_code) {
  473. netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n",
  474. resp->error_code);
  475. rc = -1;
  476. }
  477. fwd_async_event_cmpl_exit:
  478. mutex_unlock(&bp->hwrm_cmd_lock);
  479. return rc;
  480. }
  481. void bnxt_sriov_disable(struct bnxt *bp)
  482. {
  483. u16 num_vfs = pci_num_vf(bp->pdev);
  484. if (!num_vfs)
  485. return;
  486. if (pci_vfs_assigned(bp->pdev)) {
  487. bnxt_hwrm_fwd_async_event_cmpl(
  488. bp, NULL,
  489. HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
  490. netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
  491. num_vfs);
  492. } else {
  493. pci_disable_sriov(bp->pdev);
  494. /* Free the HW resources reserved for various VF's */
  495. bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
  496. }
  497. bnxt_free_vf_resources(bp);
  498. bp->pf.active_vfs = 0;
  499. /* Reclaim all resources for the PF. */
  500. bnxt_hwrm_func_qcaps(bp);
  501. }
  502. int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
  503. {
  504. struct net_device *dev = pci_get_drvdata(pdev);
  505. struct bnxt *bp = netdev_priv(dev);
  506. if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
  507. netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
  508. return 0;
  509. }
  510. rtnl_lock();
  511. if (!netif_running(dev)) {
  512. netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
  513. rtnl_unlock();
  514. return 0;
  515. }
  516. bp->sriov_cfg = true;
  517. rtnl_unlock();
  518. if (pci_vfs_assigned(bp->pdev)) {
  519. netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
  520. num_vfs = 0;
  521. goto sriov_cfg_exit;
  522. }
  523. /* Check if enabled VFs is same as requested */
  524. if (num_vfs && num_vfs == bp->pf.active_vfs)
  525. goto sriov_cfg_exit;
  526. /* if there are previous existing VFs, clean them up */
  527. bnxt_sriov_disable(bp);
  528. if (!num_vfs)
  529. goto sriov_cfg_exit;
  530. bnxt_sriov_enable(bp, &num_vfs);
  531. sriov_cfg_exit:
  532. bp->sriov_cfg = false;
  533. wake_up(&bp->sriov_cfg_wait);
  534. return num_vfs;
  535. }
  536. static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  537. void *encap_resp, __le64 encap_resp_addr,
  538. __le16 encap_resp_cpr, u32 msg_size)
  539. {
  540. int rc = 0;
  541. struct hwrm_fwd_resp_input req = {0};
  542. struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  543. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
  544. /* Set the new target id */
  545. req.target_id = cpu_to_le16(vf->fw_fid);
  546. req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
  547. req.encap_resp_len = cpu_to_le16(msg_size);
  548. req.encap_resp_addr = encap_resp_addr;
  549. req.encap_resp_cmpl_ring = encap_resp_cpr;
  550. memcpy(req.encap_resp, encap_resp, msg_size);
  551. mutex_lock(&bp->hwrm_cmd_lock);
  552. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  553. if (rc) {
  554. netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
  555. goto fwd_resp_exit;
  556. }
  557. if (resp->error_code) {
  558. netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
  559. resp->error_code);
  560. rc = -1;
  561. }
  562. fwd_resp_exit:
  563. mutex_unlock(&bp->hwrm_cmd_lock);
  564. return rc;
  565. }
  566. static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  567. u32 msg_size)
  568. {
  569. int rc = 0;
  570. struct hwrm_reject_fwd_resp_input req = {0};
  571. struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  572. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
  573. /* Set the new target id */
  574. req.target_id = cpu_to_le16(vf->fw_fid);
  575. req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
  576. memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
  577. mutex_lock(&bp->hwrm_cmd_lock);
  578. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  579. if (rc) {
  580. netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
  581. goto fwd_err_resp_exit;
  582. }
  583. if (resp->error_code) {
  584. netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
  585. resp->error_code);
  586. rc = -1;
  587. }
  588. fwd_err_resp_exit:
  589. mutex_unlock(&bp->hwrm_cmd_lock);
  590. return rc;
  591. }
  592. static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
  593. u32 msg_size)
  594. {
  595. int rc = 0;
  596. struct hwrm_exec_fwd_resp_input req = {0};
  597. struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
  598. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
  599. /* Set the new target id */
  600. req.target_id = cpu_to_le16(vf->fw_fid);
  601. req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
  602. memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
  603. mutex_lock(&bp->hwrm_cmd_lock);
  604. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  605. if (rc) {
  606. netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
  607. goto exec_fwd_resp_exit;
  608. }
  609. if (resp->error_code) {
  610. netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
  611. resp->error_code);
  612. rc = -1;
  613. }
  614. exec_fwd_resp_exit:
  615. mutex_unlock(&bp->hwrm_cmd_lock);
  616. return rc;
  617. }
  618. static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
  619. {
  620. u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
  621. struct hwrm_cfa_l2_filter_alloc_input *req =
  622. (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
  623. if (!is_valid_ether_addr(vf->mac_addr) ||
  624. ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
  625. return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
  626. else
  627. return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
  628. }
  629. static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
  630. {
  631. int rc = 0;
  632. if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
  633. /* real link */
  634. rc = bnxt_hwrm_exec_fwd_resp(
  635. bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
  636. } else {
  637. struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
  638. struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
  639. phy_qcfg_req =
  640. (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
  641. mutex_lock(&bp->hwrm_cmd_lock);
  642. memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
  643. sizeof(phy_qcfg_resp));
  644. mutex_unlock(&bp->hwrm_cmd_lock);
  645. phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
  646. if (vf->flags & BNXT_VF_LINK_UP) {
  647. /* if physical link is down, force link up on VF */
  648. if (phy_qcfg_resp.link ==
  649. PORT_PHY_QCFG_RESP_LINK_NO_LINK) {
  650. phy_qcfg_resp.link =
  651. PORT_PHY_QCFG_RESP_LINK_LINK;
  652. if (phy_qcfg_resp.auto_link_speed)
  653. phy_qcfg_resp.link_speed =
  654. phy_qcfg_resp.auto_link_speed;
  655. else
  656. phy_qcfg_resp.link_speed =
  657. phy_qcfg_resp.force_link_speed;
  658. phy_qcfg_resp.duplex =
  659. PORT_PHY_QCFG_RESP_DUPLEX_FULL;
  660. phy_qcfg_resp.pause =
  661. (PORT_PHY_QCFG_RESP_PAUSE_TX |
  662. PORT_PHY_QCFG_RESP_PAUSE_RX);
  663. }
  664. } else {
  665. /* force link down */
  666. phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
  667. phy_qcfg_resp.link_speed = 0;
  668. phy_qcfg_resp.duplex = PORT_PHY_QCFG_RESP_DUPLEX_HALF;
  669. phy_qcfg_resp.pause = 0;
  670. }
  671. rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
  672. phy_qcfg_req->resp_addr,
  673. phy_qcfg_req->cmpl_ring,
  674. sizeof(phy_qcfg_resp));
  675. }
  676. return rc;
  677. }
  678. static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
  679. {
  680. int rc = 0;
  681. struct input *encap_req = vf->hwrm_cmd_req_addr;
  682. u32 req_type = le16_to_cpu(encap_req->req_type);
  683. switch (req_type) {
  684. case HWRM_CFA_L2_FILTER_ALLOC:
  685. rc = bnxt_vf_validate_set_mac(bp, vf);
  686. break;
  687. case HWRM_FUNC_CFG:
  688. /* TODO Validate if VF is allowed to change mac address,
  689. * mtu, num of rings etc
  690. */
  691. rc = bnxt_hwrm_exec_fwd_resp(
  692. bp, vf, sizeof(struct hwrm_func_cfg_input));
  693. break;
  694. case HWRM_PORT_PHY_QCFG:
  695. rc = bnxt_vf_set_link(bp, vf);
  696. break;
  697. default:
  698. break;
  699. }
  700. return rc;
  701. }
  702. void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
  703. {
  704. u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
  705. /* Scan through VF's and process commands */
  706. while (1) {
  707. vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
  708. if (vf_id >= active_vfs)
  709. break;
  710. clear_bit(vf_id, bp->pf.vf_event_bmap);
  711. bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
  712. i = vf_id + 1;
  713. }
  714. }
  715. void bnxt_update_vf_mac(struct bnxt *bp)
  716. {
  717. struct hwrm_func_qcaps_input req = {0};
  718. struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  719. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
  720. req.fid = cpu_to_le16(0xffff);
  721. mutex_lock(&bp->hwrm_cmd_lock);
  722. if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
  723. goto update_vf_mac_exit;
  724. /* Store MAC address from the firmware. There are 2 cases:
  725. * 1. MAC address is valid. It is assigned from the PF and we
  726. * need to override the current VF MAC address with it.
  727. * 2. MAC address is zero. The VF will use a random MAC address by
  728. * default but the stored zero MAC will allow the VF user to change
  729. * the random MAC address using ndo_set_mac_address() if he wants.
  730. */
  731. if (!ether_addr_equal(resp->perm_mac_address, bp->vf.mac_addr))
  732. memcpy(bp->vf.mac_addr, resp->perm_mac_address, ETH_ALEN);
  733. /* overwrite netdev dev_addr with admin VF MAC */
  734. if (is_valid_ether_addr(bp->vf.mac_addr))
  735. memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
  736. update_vf_mac_exit:
  737. mutex_unlock(&bp->hwrm_cmd_lock);
  738. }
  739. #else
  740. void bnxt_sriov_disable(struct bnxt *bp)
  741. {
  742. }
  743. void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
  744. {
  745. netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
  746. }
  747. void bnxt_update_vf_mac(struct bnxt *bp)
  748. {
  749. }
  750. #endif