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 /* Notify the VF before its enablement to let it set 91 * some stuff. 92 */ 93 blocking_notifier_call_chain(&sriov->vfs_ctx[vf].notifier, 94 MLX5_PF_NOTIFY_ENABLE_VF, dev); 95 err = mlx5_core_enable_hca(dev, vf + 1); 96 if (err) { 97 mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err); 98 continue; 99 } 100 101 err = mlx5_set_msix_vec_count(dev, vf + 1, num_msix_count); 102 if (err) { 103 mlx5_core_warn(dev, 104 "failed to set MSI-X vector counts VF %d, err %d\n", 105 vf, err); 106 continue; 107 } 108 109 sriov->vfs_ctx[vf].enabled = 1; 110 if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) { 111 err = sriov_restore_guids(dev, vf); 112 if (err) { 113 mlx5_core_warn(dev, 114 "failed to restore VF %d settings, err %d\n", 115 vf, err); 116 continue; 117 } 118 } 119 mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf); 120 } 121 122 return 0; 123 } 124 125 static void 126 mlx5_device_disable_sriov(struct mlx5_core_dev *dev, int num_vfs, bool clear_vf) 127 { 128 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 129 int err; 130 int vf; 131 132 for (vf = num_vfs - 1; vf >= 0; vf--) { 133 if (!sriov->vfs_ctx[vf].enabled) 134 continue; 135 /* Notify the VF before its disablement to let it clean 136 * some resources. 137 */ 138 blocking_notifier_call_chain(&sriov->vfs_ctx[vf].notifier, 139 MLX5_PF_NOTIFY_DISABLE_VF, dev); 140 err = mlx5_core_disable_hca(dev, vf + 1); 141 if (err) { 142 mlx5_core_warn(dev, "failed to disable VF %d\n", vf); 143 continue; 144 } 145 sriov->vfs_ctx[vf].enabled = 0; 146 } 147 148 mlx5_eswitch_disable_sriov(dev->priv.eswitch, clear_vf); 149 150 if (mlx5_wait_for_pages(dev, &dev->priv.vfs_pages)) 151 mlx5_core_warn(dev, "timeout reclaiming VFs pages\n"); 152 } 153 154 static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs) 155 { 156 struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 157 struct devlink *devlink = priv_to_devlink(dev); 158 int err; 159 160 devl_lock(devlink); 161 err = mlx5_device_enable_sriov(dev, num_vfs); 162 devl_unlock(devlink); 163 if (err) { 164 mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err); 165 return err; 166 } 167 168 err = pci_enable_sriov(pdev, num_vfs); 169 if (err) { 170 mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err); 171 mlx5_device_disable_sriov(dev, num_vfs, true); 172 } 173 return err; 174 } 175 176 void mlx5_sriov_disable(struct pci_dev *pdev) 177 { 178 struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 179 struct devlink *devlink = priv_to_devlink(dev); 180 int num_vfs = pci_num_vf(dev->pdev); 181 182 pci_disable_sriov(pdev); 183 devl_lock(devlink); 184 mlx5_device_disable_sriov(dev, num_vfs, true); 185 devl_unlock(devlink); 186 } 187 188 int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs) 189 { 190 struct mlx5_core_dev *dev = pci_get_drvdata(pdev); 191 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 192 int err = 0; 193 194 mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs); 195 196 if (num_vfs) 197 err = mlx5_sriov_enable(pdev, num_vfs); 198 else 199 mlx5_sriov_disable(pdev); 200 201 if (!err) 202 sriov->num_vfs = num_vfs; 203 return err ? err : num_vfs; 204 } 205 206 int mlx5_core_sriov_set_msix_vec_count(struct pci_dev *vf, int msix_vec_count) 207 { 208 struct pci_dev *pf = pci_physfn(vf); 209 struct mlx5_core_sriov *sriov; 210 struct mlx5_core_dev *dev; 211 int num_vf_msix, id; 212 213 dev = pci_get_drvdata(pf); 214 num_vf_msix = MLX5_CAP_GEN_MAX(dev, num_total_dynamic_vf_msix); 215 if (!num_vf_msix) 216 return -EOPNOTSUPP; 217 218 if (!msix_vec_count) 219 msix_vec_count = 220 mlx5_get_default_msix_vec_count(dev, pci_num_vf(pf)); 221 222 sriov = &dev->priv.sriov; 223 id = pci_iov_vf_id(vf); 224 if (id < 0 || !sriov->vfs_ctx[id].enabled) 225 return -EINVAL; 226 227 return mlx5_set_msix_vec_count(dev, id + 1, msix_vec_count); 228 } 229 230 int mlx5_sriov_attach(struct mlx5_core_dev *dev) 231 { 232 if (!mlx5_core_is_pf(dev) || !pci_num_vf(dev->pdev)) 233 return 0; 234 235 /* If sriov VFs exist in PCI level, enable them in device level */ 236 return mlx5_device_enable_sriov(dev, pci_num_vf(dev->pdev)); 237 } 238 239 void mlx5_sriov_detach(struct mlx5_core_dev *dev) 240 { 241 if (!mlx5_core_is_pf(dev)) 242 return; 243 244 mlx5_device_disable_sriov(dev, pci_num_vf(dev->pdev), false); 245 } 246 247 static u16 mlx5_get_max_vfs(struct mlx5_core_dev *dev) 248 { 249 u16 host_total_vfs; 250 const u32 *out; 251 252 if (mlx5_core_is_ecpf_esw_manager(dev)) { 253 out = mlx5_esw_query_functions(dev); 254 255 /* Old FW doesn't support getting total_vfs from esw func 256 * but supports getting it from pci_sriov. 257 */ 258 if (IS_ERR(out)) 259 goto done; 260 host_total_vfs = MLX5_GET(query_esw_functions_out, out, 261 host_params_context.host_total_vfs); 262 kvfree(out); 263 if (host_total_vfs) 264 return host_total_vfs; 265 } 266 267 done: 268 return pci_sriov_get_totalvfs(dev->pdev); 269 } 270 271 int mlx5_sriov_init(struct mlx5_core_dev *dev) 272 { 273 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 274 struct pci_dev *pdev = dev->pdev; 275 int total_vfs, i; 276 277 if (!mlx5_core_is_pf(dev)) 278 return 0; 279 280 total_vfs = pci_sriov_get_totalvfs(pdev); 281 sriov->max_vfs = mlx5_get_max_vfs(dev); 282 sriov->num_vfs = pci_num_vf(pdev); 283 sriov->vfs_ctx = kcalloc(total_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL); 284 if (!sriov->vfs_ctx) 285 return -ENOMEM; 286 287 for (i = 0; i < total_vfs; i++) 288 BLOCKING_INIT_NOTIFIER_HEAD(&sriov->vfs_ctx[i].notifier); 289 290 return 0; 291 } 292 293 void mlx5_sriov_cleanup(struct mlx5_core_dev *dev) 294 { 295 struct mlx5_core_sriov *sriov = &dev->priv.sriov; 296 297 if (!mlx5_core_is_pf(dev)) 298 return; 299 300 kfree(sriov->vfs_ctx); 301 } 302 303 /** 304 * mlx5_sriov_blocking_notifier_unregister - Unregister a VF from 305 * a notification block chain. 306 * 307 * @mdev: The mlx5 core device. 308 * @vf_id: The VF id. 309 * @nb: The notifier block to be unregistered. 310 */ 311 void mlx5_sriov_blocking_notifier_unregister(struct mlx5_core_dev *mdev, 312 int vf_id, 313 struct notifier_block *nb) 314 { 315 struct mlx5_vf_context *vfs_ctx; 316 struct mlx5_core_sriov *sriov; 317 318 sriov = &mdev->priv.sriov; 319 if (WARN_ON(vf_id < 0 || vf_id >= sriov->num_vfs)) 320 return; 321 322 vfs_ctx = &sriov->vfs_ctx[vf_id]; 323 blocking_notifier_chain_unregister(&vfs_ctx->notifier, nb); 324 } 325 EXPORT_SYMBOL(mlx5_sriov_blocking_notifier_unregister); 326 327 /** 328 * mlx5_sriov_blocking_notifier_register - Register a VF notification 329 * block chain. 330 * 331 * @mdev: The mlx5 core device. 332 * @vf_id: The VF id. 333 * @nb: The notifier block to be called upon the VF events. 334 * 335 * Returns 0 on success or an error code. 336 */ 337 int mlx5_sriov_blocking_notifier_register(struct mlx5_core_dev *mdev, 338 int vf_id, 339 struct notifier_block *nb) 340 { 341 struct mlx5_vf_context *vfs_ctx; 342 struct mlx5_core_sriov *sriov; 343 344 sriov = &mdev->priv.sriov; 345 if (vf_id < 0 || vf_id >= sriov->num_vfs) 346 return -EINVAL; 347 348 vfs_ctx = &sriov->vfs_ctx[vf_id]; 349 return blocking_notifier_chain_register(&vfs_ctx->notifier, nb); 350 } 351 EXPORT_SYMBOL(mlx5_sriov_blocking_notifier_register); 352