1 /* 2 * Copyright (c) 2014, Mellanox Technologies inc. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 */ 32 33 #include <linux/pci.h> 34 #include <linux/mlx5/driver.h> 35 #include <linux/mlx5/vport.h> 36 #include "mlx5_core.h" 37 #include "mlx5_irq.h" 38 #include "eswitch.h" 39 40 static int sriov_restore_guids(struct mlx5_core_dev *dev, int vf) 41 { 42 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 43 struct mlx5_hca_vport_context *in; 44 int err = 0; 45 46 /* Restore sriov guid and policy settings */ 47 if (sriov->vfs_ctx[vf].node_guid || 48 sriov->vfs_ctx[vf].port_guid || 49 sriov->vfs_ctx[vf].policy != MLX5_POLICY_INVALID) { 50 in = kzalloc(sizeof(*in), GFP_KERNEL); 51 if (!in) 52 return -ENOMEM; 53 54 in->node_guid = sriov->vfs_ctx[vf].node_guid; 55 in->port_guid = sriov->vfs_ctx[vf].port_guid; 56 in->policy = sriov->vfs_ctx[vf].policy; 57 in->field_select = 58 !!(in->port_guid) * MLX5_HCA_VPORT_SEL_PORT_GUID | 59 !!(in->node_guid) * MLX5_HCA_VPORT_SEL_NODE_GUID | 60 !!(in->policy) * MLX5_HCA_VPORT_SEL_STATE_POLICY; 61 62 err = mlx5_core_modify_hca_vport_context(dev, 1, 1, vf + 1, in); 63 if (err) 64 mlx5_core_warn(dev, "modify vport context failed, unable to restore VF %d settings\n", vf); 65 66 kfree(in); 67 } 68 69 return err; 70 } 71 72 static int mlx5_device_enable_sriov(struct mlx5_core_dev *dev, int num_vfs) 73 { 74 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 75 int err, vf, num_msix_count; 76 77 if (!MLX5_ESWITCH_MANAGER(dev)) 78 goto enable_vfs_hca; 79 80 err = mlx5_eswitch_enable(dev->priv.eswitch, num_vfs); 81 if (err) { 82 mlx5_core_warn(dev, 83 "failed to enable eswitch SRIOV (%d)\n", err); 84 return err; 85 } 86 87 enable_vfs_hca: 88 num_msix_count = mlx5_get_default_msix_vec_count(dev, num_vfs); 89 for (vf = 0; vf < num_vfs; vf++) { 90 err = mlx5_core_enable_hca(dev, vf + 1); 91 if (err) { 92 mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err); 93 continue; 94 } 95 96 err = mlx5_set_msix_vec_count(dev, vf + 1, num_msix_count); 97 if (err) { 98 mlx5_core_warn(dev, 99 "failed to set MSI-X vector counts VF %d, err %d\n", 100 vf, err); 101 continue; 102 } 103 104 sriov->vfs_ctx[vf].enabled = 1; 105 if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) { 106 err = sriov_restore_guids(dev, vf); 107 if (err) { 108 mlx5_core_warn(dev, 109 "failed to restore VF %d settings, err %d\n", 110 vf, err); 111 continue; 112 } 113 } 114 mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf); 115 } 116 117 return 0; 118 } 119 120 static void 121 mlx5_device_disable_sriov(struct mlx5_core_dev *dev, int num_vfs, bool clear_vf) 122 { 123 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 124 int err; 125 int vf; 126 127 for (vf = num_vfs - 1; vf >= 0; vf--) { 128 if (!sriov->vfs_ctx[vf].enabled) 129 continue; 130 err = mlx5_core_disable_hca(dev, vf + 1); 131 if (err) { 132 mlx5_core_warn(dev, "failed to disable VF %d\n", vf); 133 continue; 134 } 135 sriov->vfs_ctx[vf].enabled = 0; 136 } 137 138 if (MLX5_ESWITCH_MANAGER(dev)) 139 mlx5_eswitch_disable(dev->priv.eswitch, clear_vf); 140 141 if (mlx5_wait_for_pages(dev, &dev->priv.vfs_pages)) 142 mlx5_core_warn(dev, "timeout reclaiming VFs pages\n"); 143 } 144 145 static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs) 146 { 147 struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 148 int err; 149 150 err = mlx5_device_enable_sriov(dev, num_vfs); 151 if (err) { 152 mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err); 153 return err; 154 } 155 156 err = pci_enable_sriov(pdev, num_vfs); 157 if (err) { 158 mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err); 159 mlx5_device_disable_sriov(dev, num_vfs, true); 160 } 161 return err; 162 } 163 164 static void mlx5_sriov_disable(struct pci_dev *pdev) 165 { 166 struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 167 int num_vfs = pci_num_vf(dev->pdev); 168 169 pci_disable_sriov(pdev); 170 mlx5_device_disable_sriov(dev, num_vfs, true); 171 } 172 173 int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs) 174 { 175 struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 176 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 177 int err = 0; 178 179 mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs); 180 181 if (num_vfs) 182 err = mlx5_sriov_enable(pdev, num_vfs); 183 else 184 mlx5_sriov_disable(pdev); 185 186 if (!err) 187 sriov->num_vfs = num_vfs; 188 return err ? err : num_vfs; 189 } 190 191 int mlx5_core_sriov_set_msix_vec_count(struct pci_dev *vf, int msix_vec_count) 192 { 193 struct pci_dev *pf = pci_physfn(vf); 194 struct mlx5_core_sriov *sriov; 195 struct mlx5_core_dev *dev; 196 int num_vf_msix, id; 197 198 dev = pci_get_drvdata(pf); 199 num_vf_msix = MLX5_CAP_GEN_MAX(dev, num_total_dynamic_vf_msix); 200 if (!num_vf_msix) 201 return -EOPNOTSUPP; 202 203 if (!msix_vec_count) 204 msix_vec_count = 205 mlx5_get_default_msix_vec_count(dev, pci_num_vf(pf)); 206 207 sriov = &dev->priv.sriov; 208 209 /* Reversed translation of PCI VF function number to the internal 210 * function_id, which exists in the name of virtfn symlink. 211 */ 212 for (id = 0; id < pci_num_vf(pf); id++) { 213 if (!sriov->vfs_ctx[id].enabled) 214 continue; 215 216 if (vf->devfn == pci_iov_virtfn_devfn(pf, id)) 217 break; 218 } 219 220 if (id == pci_num_vf(pf) || !sriov->vfs_ctx[id].enabled) 221 return -EINVAL; 222 223 return mlx5_set_msix_vec_count(dev, id + 1, msix_vec_count); 224 } 225 226 int mlx5_sriov_attach(struct mlx5_core_dev *dev) 227 { 228 if (!mlx5_core_is_pf(dev) || !pci_num_vf(dev->pdev)) 229 return 0; 230 231 /* If sriov VFs exist in PCI level, enable them in device level */ 232 return mlx5_device_enable_sriov(dev, pci_num_vf(dev->pdev)); 233 } 234 235 void mlx5_sriov_detach(struct mlx5_core_dev *dev) 236 { 237 if (!mlx5_core_is_pf(dev)) 238 return; 239 240 mlx5_device_disable_sriov(dev, pci_num_vf(dev->pdev), false); 241 } 242 243 static u16 mlx5_get_max_vfs(struct mlx5_core_dev *dev) 244 { 245 u16 host_total_vfs; 246 const u32 *out; 247 248 if (mlx5_core_is_ecpf_esw_manager(dev)) { 249 out = mlx5_esw_query_functions(dev); 250 251 /* Old FW doesn't support getting total_vfs from esw func 252 * but supports getting it from pci_sriov. 253 */ 254 if (IS_ERR(out)) 255 goto done; 256 host_total_vfs = MLX5_GET(query_esw_functions_out, out, 257 host_params_context.host_total_vfs); 258 kvfree(out); 259 if (host_total_vfs) 260 return host_total_vfs; 261 } 262 263 done: 264 return pci_sriov_get_totalvfs(dev->pdev); 265 } 266 267 int mlx5_sriov_init(struct mlx5_core_dev *dev) 268 { 269 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 270 struct pci_dev *pdev = dev->pdev; 271 int total_vfs; 272 273 if (!mlx5_core_is_pf(dev)) 274 return 0; 275 276 total_vfs = pci_sriov_get_totalvfs(pdev); 277 sriov->max_vfs = mlx5_get_max_vfs(dev); 278 sriov->num_vfs = pci_num_vf(pdev); 279 sriov->vfs_ctx = kcalloc(total_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL); 280 if (!sriov->vfs_ctx) 281 return -ENOMEM; 282 283 return 0; 284 } 285 286 void mlx5_sriov_cleanup(struct mlx5_core_dev *dev) 287 { 288 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 289 290 if (!mlx5_core_is_pf(dev)) 291 return; 292 293 kfree(sriov->vfs_ctx); 294 } 295