bnxt_sriov.c 25 KB

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