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