Eli Cohen | fc50db9 | 2015-12-01 18:03:09 +0200 | [diff] [blame] | 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 "mlx5_core.h" |
Saeed Mahameed | 8184873 | 2015-12-01 18:03:20 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_MLX5_CORE_EN |
| 37 | #include "eswitch.h" |
| 38 | #endif |
Eli Cohen | fc50db9 | 2015-12-01 18:03:09 +0200 | [diff] [blame] | 39 | |
| 40 | static void enable_vfs(struct mlx5_core_dev *dev, int num_vfs) |
| 41 | { |
| 42 | struct mlx5_core_sriov *sriov = &dev->priv.sriov; |
| 43 | int err; |
| 44 | int vf; |
| 45 | |
| 46 | for (vf = 1; vf <= num_vfs; vf++) { |
| 47 | err = mlx5_core_enable_hca(dev, vf); |
| 48 | if (err) { |
| 49 | mlx5_core_warn(dev, "failed to enable VF %d\n", vf - 1); |
| 50 | } else { |
| 51 | sriov->vfs_ctx[vf - 1].enabled = 1; |
| 52 | mlx5_core_dbg(dev, "successfully enabled VF %d\n", vf - 1); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static void disable_vfs(struct mlx5_core_dev *dev, int num_vfs) |
| 58 | { |
| 59 | struct mlx5_core_sriov *sriov = &dev->priv.sriov; |
| 60 | int vf; |
| 61 | |
| 62 | for (vf = 1; vf <= num_vfs; vf++) { |
| 63 | if (sriov->vfs_ctx[vf - 1].enabled) { |
| 64 | if (mlx5_core_disable_hca(dev, vf)) |
| 65 | mlx5_core_warn(dev, "failed to disable VF %d\n", vf - 1); |
| 66 | else |
| 67 | sriov->vfs_ctx[vf - 1].enabled = 0; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static int mlx5_core_create_vfs(struct pci_dev *pdev, int num_vfs) |
| 73 | { |
| 74 | struct mlx5_core_dev *dev = pci_get_drvdata(pdev); |
| 75 | int err; |
| 76 | |
| 77 | if (pci_num_vf(pdev)) |
| 78 | pci_disable_sriov(pdev); |
| 79 | |
| 80 | enable_vfs(dev, num_vfs); |
| 81 | |
| 82 | err = pci_enable_sriov(pdev, num_vfs); |
| 83 | if (err) { |
| 84 | dev_warn(&pdev->dev, "enable sriov failed %d\n", err); |
| 85 | goto ex; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | |
| 90 | ex: |
| 91 | disable_vfs(dev, num_vfs); |
| 92 | return err; |
| 93 | } |
| 94 | |
| 95 | static int mlx5_core_sriov_enable(struct pci_dev *pdev, int num_vfs) |
| 96 | { |
| 97 | struct mlx5_core_dev *dev = pci_get_drvdata(pdev); |
| 98 | struct mlx5_core_sriov *sriov = &dev->priv.sriov; |
| 99 | int err; |
| 100 | |
| 101 | kfree(sriov->vfs_ctx); |
| 102 | sriov->vfs_ctx = kcalloc(num_vfs, sizeof(*sriov->vfs_ctx), GFP_ATOMIC); |
| 103 | if (!sriov->vfs_ctx) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | sriov->enabled_vfs = num_vfs; |
| 107 | err = mlx5_core_create_vfs(pdev, num_vfs); |
| 108 | if (err) { |
| 109 | kfree(sriov->vfs_ctx); |
| 110 | sriov->vfs_ctx = NULL; |
| 111 | return err; |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static void mlx5_core_init_vfs(struct mlx5_core_dev *dev, int num_vfs) |
| 118 | { |
| 119 | struct mlx5_core_sriov *sriov = &dev->priv.sriov; |
| 120 | |
| 121 | sriov->num_vfs = num_vfs; |
| 122 | } |
| 123 | |
| 124 | static void mlx5_core_cleanup_vfs(struct mlx5_core_dev *dev) |
| 125 | { |
| 126 | struct mlx5_core_sriov *sriov; |
| 127 | |
| 128 | sriov = &dev->priv.sriov; |
| 129 | disable_vfs(dev, sriov->num_vfs); |
| 130 | |
| 131 | if (mlx5_wait_for_vf_pages(dev)) |
| 132 | mlx5_core_warn(dev, "timeout claiming VFs pages\n"); |
| 133 | |
| 134 | sriov->num_vfs = 0; |
| 135 | } |
| 136 | |
| 137 | int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs) |
| 138 | { |
| 139 | struct mlx5_core_dev *dev = pci_get_drvdata(pdev); |
| 140 | struct mlx5_core_sriov *sriov = &dev->priv.sriov; |
| 141 | int err; |
| 142 | |
Masanari Iida | c19ca6c | 2016-02-08 20:53:12 +0900 | [diff] [blame] | 143 | mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs); |
Eli Cohen | fc50db9 | 2015-12-01 18:03:09 +0200 | [diff] [blame] | 144 | if (!mlx5_core_is_pf(dev)) |
| 145 | return -EPERM; |
| 146 | |
| 147 | mlx5_core_cleanup_vfs(dev); |
| 148 | |
| 149 | if (!num_vfs) { |
Saeed Mahameed | 8184873 | 2015-12-01 18:03:20 +0200 | [diff] [blame] | 150 | #ifdef CONFIG_MLX5_CORE_EN |
| 151 | mlx5_eswitch_disable_sriov(dev->priv.eswitch); |
| 152 | #endif |
Eli Cohen | fc50db9 | 2015-12-01 18:03:09 +0200 | [diff] [blame] | 153 | kfree(sriov->vfs_ctx); |
| 154 | sriov->vfs_ctx = NULL; |
| 155 | if (!pci_vfs_assigned(pdev)) |
| 156 | pci_disable_sriov(pdev); |
| 157 | else |
| 158 | pr_info("unloading PF driver while leaving orphan VFs\n"); |
Eli Cohen | fc50db9 | 2015-12-01 18:03:09 +0200 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | err = mlx5_core_sriov_enable(pdev, num_vfs); |
| 163 | if (err) { |
| 164 | dev_warn(&pdev->dev, "mlx5_core_sriov_enable failed %d\n", err); |
| 165 | return err; |
| 166 | } |
| 167 | |
| 168 | mlx5_core_init_vfs(dev, num_vfs); |
Saeed Mahameed | 8184873 | 2015-12-01 18:03:20 +0200 | [diff] [blame] | 169 | #ifdef CONFIG_MLX5_CORE_EN |
Or Gerlitz | 6ab36e3 | 2016-07-01 14:50:54 +0300 | [diff] [blame] | 170 | mlx5_eswitch_enable_sriov(dev->priv.eswitch, num_vfs, SRIOV_LEGACY); |
Saeed Mahameed | 8184873 | 2015-12-01 18:03:20 +0200 | [diff] [blame] | 171 | #endif |
Eli Cohen | fc50db9 | 2015-12-01 18:03:09 +0200 | [diff] [blame] | 172 | |
| 173 | return num_vfs; |
| 174 | } |
| 175 | |
| 176 | static int sync_required(struct pci_dev *pdev) |
| 177 | { |
| 178 | struct mlx5_core_dev *dev = pci_get_drvdata(pdev); |
| 179 | struct mlx5_core_sriov *sriov = &dev->priv.sriov; |
| 180 | int cur_vfs = pci_num_vf(pdev); |
| 181 | |
| 182 | if (cur_vfs != sriov->num_vfs) { |
| 183 | pr_info("current VFs %d, registered %d - sync needed\n", cur_vfs, sriov->num_vfs); |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | int mlx5_sriov_init(struct mlx5_core_dev *dev) |
| 191 | { |
| 192 | struct mlx5_core_sriov *sriov = &dev->priv.sriov; |
| 193 | struct pci_dev *pdev = dev->pdev; |
| 194 | int cur_vfs; |
| 195 | |
| 196 | if (!mlx5_core_is_pf(dev)) |
| 197 | return 0; |
| 198 | |
| 199 | if (!sync_required(dev->pdev)) |
| 200 | return 0; |
| 201 | |
| 202 | cur_vfs = pci_num_vf(pdev); |
| 203 | sriov->vfs_ctx = kcalloc(cur_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL); |
| 204 | if (!sriov->vfs_ctx) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | sriov->enabled_vfs = cur_vfs; |
| 208 | |
| 209 | mlx5_core_init_vfs(dev, cur_vfs); |
Saeed Mahameed | 8184873 | 2015-12-01 18:03:20 +0200 | [diff] [blame] | 210 | #ifdef CONFIG_MLX5_CORE_EN |
| 211 | if (cur_vfs) |
Or Gerlitz | 6ab36e3 | 2016-07-01 14:50:54 +0300 | [diff] [blame] | 212 | mlx5_eswitch_enable_sriov(dev->priv.eswitch, cur_vfs, |
| 213 | SRIOV_LEGACY); |
Saeed Mahameed | 8184873 | 2015-12-01 18:03:20 +0200 | [diff] [blame] | 214 | #endif |
Eli Cohen | fc50db9 | 2015-12-01 18:03:09 +0200 | [diff] [blame] | 215 | |
| 216 | enable_vfs(dev, cur_vfs); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | int mlx5_sriov_cleanup(struct mlx5_core_dev *dev) |
| 222 | { |
| 223 | struct pci_dev *pdev = dev->pdev; |
| 224 | int err; |
| 225 | |
| 226 | if (!mlx5_core_is_pf(dev)) |
| 227 | return 0; |
| 228 | |
| 229 | err = mlx5_core_sriov_configure(pdev, 0); |
| 230 | if (err) |
| 231 | return err; |
| 232 | |
| 233 | return 0; |
| 234 | } |