bnxt_sriov.c 24 KB

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