bnxt_sriov.c 24 KB

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