1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020 Mellanox Technologies Ltd */
3
4 #include <linux/mlx5/driver.h>
5 #include "eswitch.h"
6 #include "priv.h"
7 #include "sf/dev/dev.h"
8 #include "mlx5_ifc_vhca_event.h"
9 #include "vhca_event.h"
10 #include "ecpf.h"
11 #define CREATE_TRACE_POINTS
12 #include "diag/sf_tracepoint.h"
13
14 struct mlx5_sf {
15 struct mlx5_devlink_port dl_port;
16 unsigned int port_index;
17 u32 controller;
18 u16 id;
19 u16 hw_fn_id;
20 u16 hw_state;
21 };
22
mlx5_sf_by_dl_port(struct devlink_port * dl_port)23 static void *mlx5_sf_by_dl_port(struct devlink_port *dl_port)
24 {
25 struct mlx5_devlink_port *mlx5_dl_port = mlx5_devlink_port_get(dl_port);
26
27 return container_of(mlx5_dl_port, struct mlx5_sf, dl_port);
28 }
29
30 struct mlx5_sf_table {
31 struct mlx5_core_dev *dev; /* To refer from notifier context. */
32 struct xarray function_ids; /* function id based lookup. */
33 struct mutex sf_state_lock; /* Serializes sf state among user cmds & vhca event handler. */
34 struct notifier_block esw_nb;
35 struct notifier_block vhca_nb;
36 struct notifier_block mdev_nb;
37 };
38
39 static struct mlx5_sf *
mlx5_sf_lookup_by_function_id(struct mlx5_sf_table * table,unsigned int fn_id)40 mlx5_sf_lookup_by_function_id(struct mlx5_sf_table *table, unsigned int fn_id)
41 {
42 return xa_load(&table->function_ids, fn_id);
43 }
44
mlx5_sf_function_id_insert(struct mlx5_sf_table * table,struct mlx5_sf * sf)45 static int mlx5_sf_function_id_insert(struct mlx5_sf_table *table, struct mlx5_sf *sf)
46 {
47 return xa_insert(&table->function_ids, sf->hw_fn_id, sf, GFP_KERNEL);
48 }
49
mlx5_sf_function_id_erase(struct mlx5_sf_table * table,struct mlx5_sf * sf)50 static void mlx5_sf_function_id_erase(struct mlx5_sf_table *table, struct mlx5_sf *sf)
51 {
52 xa_erase(&table->function_ids, sf->hw_fn_id);
53 }
54
55 static struct mlx5_sf *
mlx5_sf_alloc(struct mlx5_sf_table * table,struct mlx5_eswitch * esw,u32 controller,u32 sfnum,struct netlink_ext_ack * extack)56 mlx5_sf_alloc(struct mlx5_sf_table *table, struct mlx5_eswitch *esw,
57 u32 controller, u32 sfnum, struct netlink_ext_ack *extack)
58 {
59 unsigned int dl_port_index;
60 struct mlx5_sf *sf;
61 u16 hw_fn_id;
62 int id_err;
63 int err;
64
65 if (!mlx5_esw_offloads_controller_valid(esw, controller)) {
66 NL_SET_ERR_MSG_MOD(extack, "Invalid controller number");
67 return ERR_PTR(-EINVAL);
68 }
69
70 id_err = mlx5_sf_hw_table_sf_alloc(table->dev, controller, sfnum);
71 if (id_err < 0) {
72 err = id_err;
73 goto id_err;
74 }
75
76 sf = kzalloc(sizeof(*sf), GFP_KERNEL);
77 if (!sf) {
78 err = -ENOMEM;
79 goto alloc_err;
80 }
81 sf->id = id_err;
82 hw_fn_id = mlx5_sf_sw_to_hw_id(table->dev, controller, sf->id);
83 dl_port_index = mlx5_esw_vport_to_devlink_port_index(table->dev, hw_fn_id);
84 sf->port_index = dl_port_index;
85 sf->hw_fn_id = hw_fn_id;
86 sf->hw_state = MLX5_VHCA_STATE_ALLOCATED;
87 sf->controller = controller;
88
89 err = mlx5_sf_function_id_insert(table, sf);
90 if (err)
91 goto insert_err;
92
93 return sf;
94
95 insert_err:
96 kfree(sf);
97 alloc_err:
98 mlx5_sf_hw_table_sf_free(table->dev, controller, id_err);
99 id_err:
100 if (err == -EEXIST)
101 NL_SET_ERR_MSG_MOD(extack, "SF already exist. Choose different sfnum");
102 return ERR_PTR(err);
103 }
104
mlx5_sf_free(struct mlx5_sf_table * table,struct mlx5_sf * sf)105 static void mlx5_sf_free(struct mlx5_sf_table *table, struct mlx5_sf *sf)
106 {
107 mlx5_sf_hw_table_sf_free(table->dev, sf->controller, sf->id);
108 trace_mlx5_sf_free(table->dev, sf->port_index, sf->controller, sf->hw_fn_id);
109 kfree(sf);
110 }
111
mlx5_sf_to_devlink_state(u8 hw_state)112 static enum devlink_port_fn_state mlx5_sf_to_devlink_state(u8 hw_state)
113 {
114 switch (hw_state) {
115 case MLX5_VHCA_STATE_ACTIVE:
116 case MLX5_VHCA_STATE_IN_USE:
117 return DEVLINK_PORT_FN_STATE_ACTIVE;
118 case MLX5_VHCA_STATE_INVALID:
119 case MLX5_VHCA_STATE_ALLOCATED:
120 case MLX5_VHCA_STATE_TEARDOWN_REQUEST:
121 default:
122 return DEVLINK_PORT_FN_STATE_INACTIVE;
123 }
124 }
125
mlx5_sf_to_devlink_opstate(u8 hw_state)126 static enum devlink_port_fn_opstate mlx5_sf_to_devlink_opstate(u8 hw_state)
127 {
128 switch (hw_state) {
129 case MLX5_VHCA_STATE_IN_USE:
130 case MLX5_VHCA_STATE_TEARDOWN_REQUEST:
131 return DEVLINK_PORT_FN_OPSTATE_ATTACHED;
132 case MLX5_VHCA_STATE_INVALID:
133 case MLX5_VHCA_STATE_ALLOCATED:
134 case MLX5_VHCA_STATE_ACTIVE:
135 default:
136 return DEVLINK_PORT_FN_OPSTATE_DETACHED;
137 }
138 }
139
mlx5_sf_is_active(const struct mlx5_sf * sf)140 static bool mlx5_sf_is_active(const struct mlx5_sf *sf)
141 {
142 return sf->hw_state == MLX5_VHCA_STATE_ACTIVE || sf->hw_state == MLX5_VHCA_STATE_IN_USE;
143 }
144
mlx5_devlink_sf_port_fn_state_get(struct devlink_port * dl_port,enum devlink_port_fn_state * state,enum devlink_port_fn_opstate * opstate,struct netlink_ext_ack * extack)145 int mlx5_devlink_sf_port_fn_state_get(struct devlink_port *dl_port,
146 enum devlink_port_fn_state *state,
147 enum devlink_port_fn_opstate *opstate,
148 struct netlink_ext_ack *extack)
149 {
150 struct mlx5_core_dev *dev = devlink_priv(dl_port->devlink);
151 struct mlx5_sf_table *table = dev->priv.sf_table;
152 struct mlx5_sf *sf = mlx5_sf_by_dl_port(dl_port);
153
154 mutex_lock(&table->sf_state_lock);
155 *state = mlx5_sf_to_devlink_state(sf->hw_state);
156 *opstate = mlx5_sf_to_devlink_opstate(sf->hw_state);
157 mutex_unlock(&table->sf_state_lock);
158 return 0;
159 }
160
mlx5_sf_activate(struct mlx5_core_dev * dev,struct mlx5_sf * sf,struct netlink_ext_ack * extack)161 static int mlx5_sf_activate(struct mlx5_core_dev *dev, struct mlx5_sf *sf,
162 struct netlink_ext_ack *extack)
163 {
164 struct mlx5_vport *vport;
165 int err;
166
167 if (mlx5_sf_is_active(sf))
168 return 0;
169 if (sf->hw_state != MLX5_VHCA_STATE_ALLOCATED) {
170 NL_SET_ERR_MSG_MOD(extack, "SF is inactivated but it is still attached");
171 return -EBUSY;
172 }
173
174 vport = mlx5_devlink_port_vport_get(&sf->dl_port.dl_port);
175 if (!vport->max_eqs_set && MLX5_CAP_GEN_2(dev, max_num_eqs_24b)) {
176 err = mlx5_devlink_port_fn_max_io_eqs_set_sf_default(&sf->dl_port.dl_port,
177 extack);
178 if (err)
179 return err;
180 }
181 err = mlx5_cmd_sf_enable_hca(dev, sf->hw_fn_id);
182 if (err)
183 return err;
184
185 sf->hw_state = MLX5_VHCA_STATE_ACTIVE;
186 trace_mlx5_sf_activate(dev, sf->port_index, sf->controller, sf->hw_fn_id);
187 return 0;
188 }
189
mlx5_sf_deactivate(struct mlx5_core_dev * dev,struct mlx5_sf * sf)190 static int mlx5_sf_deactivate(struct mlx5_core_dev *dev, struct mlx5_sf *sf)
191 {
192 int err;
193
194 if (!mlx5_sf_is_active(sf))
195 return 0;
196
197 err = mlx5_cmd_sf_disable_hca(dev, sf->hw_fn_id);
198 if (err)
199 return err;
200
201 sf->hw_state = MLX5_VHCA_STATE_TEARDOWN_REQUEST;
202 trace_mlx5_sf_deactivate(dev, sf->port_index, sf->controller, sf->hw_fn_id);
203 return 0;
204 }
205
mlx5_sf_state_set(struct mlx5_core_dev * dev,struct mlx5_sf_table * table,struct mlx5_sf * sf,enum devlink_port_fn_state state,struct netlink_ext_ack * extack)206 static int mlx5_sf_state_set(struct mlx5_core_dev *dev, struct mlx5_sf_table *table,
207 struct mlx5_sf *sf,
208 enum devlink_port_fn_state state,
209 struct netlink_ext_ack *extack)
210 {
211 int err = 0;
212
213 mutex_lock(&table->sf_state_lock);
214 if (state == mlx5_sf_to_devlink_state(sf->hw_state))
215 goto out;
216 if (state == DEVLINK_PORT_FN_STATE_ACTIVE)
217 err = mlx5_sf_activate(dev, sf, extack);
218 else if (state == DEVLINK_PORT_FN_STATE_INACTIVE)
219 err = mlx5_sf_deactivate(dev, sf);
220 else
221 err = -EINVAL;
222 out:
223 mutex_unlock(&table->sf_state_lock);
224 return err;
225 }
226
mlx5_devlink_sf_port_fn_state_set(struct devlink_port * dl_port,enum devlink_port_fn_state state,struct netlink_ext_ack * extack)227 int mlx5_devlink_sf_port_fn_state_set(struct devlink_port *dl_port,
228 enum devlink_port_fn_state state,
229 struct netlink_ext_ack *extack)
230 {
231 struct mlx5_core_dev *dev = devlink_priv(dl_port->devlink);
232 struct mlx5_sf_table *table = dev->priv.sf_table;
233 struct mlx5_sf *sf = mlx5_sf_by_dl_port(dl_port);
234
235 return mlx5_sf_state_set(dev, table, sf, state, extack);
236 }
237
mlx5_sf_add(struct mlx5_core_dev * dev,struct mlx5_sf_table * table,const struct devlink_port_new_attrs * new_attr,struct netlink_ext_ack * extack,struct devlink_port ** dl_port)238 static int mlx5_sf_add(struct mlx5_core_dev *dev, struct mlx5_sf_table *table,
239 const struct devlink_port_new_attrs *new_attr,
240 struct netlink_ext_ack *extack,
241 struct devlink_port **dl_port)
242 {
243 struct mlx5_eswitch *esw = dev->priv.eswitch;
244 struct mlx5_sf *sf;
245 int err;
246
247 sf = mlx5_sf_alloc(table, esw, new_attr->controller, new_attr->sfnum, extack);
248 if (IS_ERR(sf))
249 return PTR_ERR(sf);
250
251 err = mlx5_eswitch_load_sf_vport(esw, sf->hw_fn_id, MLX5_VPORT_UC_ADDR_CHANGE,
252 &sf->dl_port, new_attr->controller, new_attr->sfnum);
253 if (err)
254 goto esw_err;
255 *dl_port = &sf->dl_port.dl_port;
256 trace_mlx5_sf_add(dev, sf->port_index, sf->controller, sf->hw_fn_id, new_attr->sfnum);
257 return 0;
258
259 esw_err:
260 mlx5_sf_free(table, sf);
261 return err;
262 }
263
264 static int
mlx5_sf_new_check_attr(struct mlx5_core_dev * dev,const struct devlink_port_new_attrs * new_attr,struct netlink_ext_ack * extack)265 mlx5_sf_new_check_attr(struct mlx5_core_dev *dev, const struct devlink_port_new_attrs *new_attr,
266 struct netlink_ext_ack *extack)
267 {
268 if (new_attr->flavour != DEVLINK_PORT_FLAVOUR_PCI_SF) {
269 NL_SET_ERR_MSG_MOD(extack, "Driver supports only SF port addition");
270 return -EOPNOTSUPP;
271 }
272 if (new_attr->port_index_valid) {
273 NL_SET_ERR_MSG_MOD(extack,
274 "Driver does not support user defined port index assignment");
275 return -EOPNOTSUPP;
276 }
277 if (!new_attr->sfnum_valid) {
278 NL_SET_ERR_MSG_MOD(extack,
279 "User must provide unique sfnum. Driver does not support auto assignment");
280 return -EOPNOTSUPP;
281 }
282 if (new_attr->controller_valid && new_attr->controller &&
283 !mlx5_core_is_ecpf_esw_manager(dev)) {
284 NL_SET_ERR_MSG_MOD(extack, "External controller is unsupported");
285 return -EOPNOTSUPP;
286 }
287 if (new_attr->pfnum != mlx5_get_dev_index(dev)) {
288 NL_SET_ERR_MSG_MOD(extack, "Invalid pfnum supplied");
289 return -EOPNOTSUPP;
290 }
291 return 0;
292 }
293
mlx5_sf_table_supported(const struct mlx5_core_dev * dev)294 static bool mlx5_sf_table_supported(const struct mlx5_core_dev *dev)
295 {
296 return dev->priv.eswitch && MLX5_ESWITCH_MANAGER(dev) &&
297 mlx5_sf_hw_table_supported(dev);
298 }
299
mlx5_devlink_sf_port_new(struct devlink * devlink,const struct devlink_port_new_attrs * new_attr,struct netlink_ext_ack * extack,struct devlink_port ** dl_port)300 int mlx5_devlink_sf_port_new(struct devlink *devlink,
301 const struct devlink_port_new_attrs *new_attr,
302 struct netlink_ext_ack *extack,
303 struct devlink_port **dl_port)
304 {
305 struct mlx5_core_dev *dev = devlink_priv(devlink);
306 struct mlx5_sf_table *table = dev->priv.sf_table;
307 int err;
308
309 err = mlx5_sf_new_check_attr(dev, new_attr, extack);
310 if (err)
311 return err;
312
313 if (!mlx5_sf_table_supported(dev)) {
314 NL_SET_ERR_MSG_MOD(extack, "SF ports are not supported.");
315 return -EOPNOTSUPP;
316 }
317
318 if (!is_mdev_switchdev_mode(dev)) {
319 NL_SET_ERR_MSG_MOD(extack,
320 "SF ports are only supported in eswitch switchdev mode.");
321 return -EOPNOTSUPP;
322 }
323
324 return mlx5_sf_add(dev, table, new_attr, extack, dl_port);
325 }
326
mlx5_sf_dealloc(struct mlx5_sf_table * table,struct mlx5_sf * sf)327 static void mlx5_sf_dealloc(struct mlx5_sf_table *table, struct mlx5_sf *sf)
328 {
329 struct mlx5_vport *vport;
330
331 mutex_lock(&table->sf_state_lock);
332 vport = mlx5_devlink_port_vport_get(&sf->dl_port.dl_port);
333 vport->max_eqs_set = false;
334
335 mlx5_sf_function_id_erase(table, sf);
336
337 if (sf->hw_state == MLX5_VHCA_STATE_ALLOCATED) {
338 mlx5_sf_free(table, sf);
339 } else if (mlx5_sf_is_active(sf)) {
340 /* Even if its active, it is treated as in_use because by the time,
341 * it is disabled here, it may getting used. So it is safe to
342 * always look for the event to ensure that it is recycled only after
343 * firmware gives confirmation that it is detached by the driver.
344 */
345 mlx5_cmd_sf_disable_hca(table->dev, sf->hw_fn_id);
346 mlx5_sf_hw_table_sf_deferred_free(table->dev, sf->controller, sf->id);
347 kfree(sf);
348 } else {
349 mlx5_sf_hw_table_sf_deferred_free(table->dev, sf->controller, sf->id);
350 kfree(sf);
351 }
352
353 mutex_unlock(&table->sf_state_lock);
354 }
355
mlx5_sf_del(struct mlx5_sf_table * table,struct mlx5_sf * sf)356 static void mlx5_sf_del(struct mlx5_sf_table *table, struct mlx5_sf *sf)
357 {
358 struct mlx5_eswitch *esw = table->dev->priv.eswitch;
359
360 mlx5_eswitch_unload_sf_vport(esw, sf->hw_fn_id);
361 mlx5_sf_dealloc(table, sf);
362 }
363
mlx5_devlink_sf_port_del(struct devlink * devlink,struct devlink_port * dl_port,struct netlink_ext_ack * extack)364 int mlx5_devlink_sf_port_del(struct devlink *devlink,
365 struct devlink_port *dl_port,
366 struct netlink_ext_ack *extack)
367 {
368 struct mlx5_core_dev *dev = devlink_priv(devlink);
369 struct mlx5_sf_table *table = dev->priv.sf_table;
370 struct mlx5_sf *sf = mlx5_sf_by_dl_port(dl_port);
371
372 mlx5_sf_del(table, sf);
373 return 0;
374 }
375
mlx5_sf_state_update_check(const struct mlx5_sf * sf,u8 new_state)376 static bool mlx5_sf_state_update_check(const struct mlx5_sf *sf, u8 new_state)
377 {
378 if (sf->hw_state == MLX5_VHCA_STATE_ACTIVE && new_state == MLX5_VHCA_STATE_IN_USE)
379 return true;
380
381 if (sf->hw_state == MLX5_VHCA_STATE_IN_USE && new_state == MLX5_VHCA_STATE_ACTIVE)
382 return true;
383
384 if (sf->hw_state == MLX5_VHCA_STATE_TEARDOWN_REQUEST &&
385 new_state == MLX5_VHCA_STATE_ALLOCATED)
386 return true;
387
388 return false;
389 }
390
mlx5_sf_vhca_event(struct notifier_block * nb,unsigned long opcode,void * data)391 static int mlx5_sf_vhca_event(struct notifier_block *nb, unsigned long opcode, void *data)
392 {
393 struct mlx5_sf_table *table = container_of(nb, struct mlx5_sf_table, vhca_nb);
394 const struct mlx5_vhca_state_event *event = data;
395 bool update = false;
396 struct mlx5_sf *sf;
397
398 mutex_lock(&table->sf_state_lock);
399 sf = mlx5_sf_lookup_by_function_id(table, event->function_id);
400 if (!sf)
401 goto unlock;
402
403 /* When driver is attached or detached to a function, an event
404 * notifies such state change.
405 */
406 update = mlx5_sf_state_update_check(sf, event->new_vhca_state);
407 if (update)
408 sf->hw_state = event->new_vhca_state;
409 trace_mlx5_sf_update_state(table->dev, sf->port_index, sf->controller,
410 sf->hw_fn_id, sf->hw_state);
411 unlock:
412 mutex_unlock(&table->sf_state_lock);
413 return 0;
414 }
415
mlx5_sf_del_all(struct mlx5_sf_table * table)416 static void mlx5_sf_del_all(struct mlx5_sf_table *table)
417 {
418 unsigned long index;
419 struct mlx5_sf *sf;
420
421 xa_for_each(&table->function_ids, index, sf)
422 mlx5_sf_del(table, sf);
423 }
424
mlx5_sf_esw_event(struct notifier_block * nb,unsigned long event,void * data)425 static int mlx5_sf_esw_event(struct notifier_block *nb, unsigned long event, void *data)
426 {
427 struct mlx5_sf_table *table = container_of(nb, struct mlx5_sf_table, esw_nb);
428 const struct mlx5_esw_event_info *mode = data;
429
430 switch (mode->new_mode) {
431 case MLX5_ESWITCH_LEGACY:
432 mlx5_sf_del_all(table);
433 break;
434 default:
435 break;
436 }
437
438 return 0;
439 }
440
mlx5_sf_mdev_event(struct notifier_block * nb,unsigned long event,void * data)441 static int mlx5_sf_mdev_event(struct notifier_block *nb, unsigned long event, void *data)
442 {
443 struct mlx5_sf_table *table = container_of(nb, struct mlx5_sf_table, mdev_nb);
444 struct mlx5_sf_peer_devlink_event_ctx *event_ctx = data;
445 int ret = NOTIFY_DONE;
446 struct mlx5_sf *sf;
447
448 if (event != MLX5_DRIVER_EVENT_SF_PEER_DEVLINK)
449 return NOTIFY_DONE;
450
451
452 mutex_lock(&table->sf_state_lock);
453 sf = mlx5_sf_lookup_by_function_id(table, event_ctx->fn_id);
454 if (!sf)
455 goto out;
456
457 event_ctx->err = devl_port_fn_devlink_set(&sf->dl_port.dl_port,
458 event_ctx->devlink);
459
460 ret = NOTIFY_OK;
461 out:
462 mutex_unlock(&table->sf_state_lock);
463 return ret;
464 }
465
mlx5_sf_table_init(struct mlx5_core_dev * dev)466 int mlx5_sf_table_init(struct mlx5_core_dev *dev)
467 {
468 struct mlx5_sf_table *table;
469 int err;
470
471 if (!mlx5_sf_table_supported(dev) || !mlx5_vhca_event_supported(dev))
472 return 0;
473
474 table = kzalloc(sizeof(*table), GFP_KERNEL);
475 if (!table)
476 return -ENOMEM;
477
478 mutex_init(&table->sf_state_lock);
479 table->dev = dev;
480 xa_init(&table->function_ids);
481 dev->priv.sf_table = table;
482 table->esw_nb.notifier_call = mlx5_sf_esw_event;
483 err = mlx5_esw_event_notifier_register(dev->priv.eswitch, &table->esw_nb);
484 if (err)
485 goto reg_err;
486
487 table->vhca_nb.notifier_call = mlx5_sf_vhca_event;
488 err = mlx5_vhca_event_notifier_register(table->dev, &table->vhca_nb);
489 if (err)
490 goto vhca_err;
491
492 table->mdev_nb.notifier_call = mlx5_sf_mdev_event;
493 mlx5_blocking_notifier_register(dev, &table->mdev_nb);
494
495 return 0;
496
497 vhca_err:
498 mlx5_esw_event_notifier_unregister(dev->priv.eswitch, &table->esw_nb);
499 reg_err:
500 mutex_destroy(&table->sf_state_lock);
501 kfree(table);
502 dev->priv.sf_table = NULL;
503 return err;
504 }
505
mlx5_sf_table_cleanup(struct mlx5_core_dev * dev)506 void mlx5_sf_table_cleanup(struct mlx5_core_dev *dev)
507 {
508 struct mlx5_sf_table *table = dev->priv.sf_table;
509
510 if (!table)
511 return;
512
513 mlx5_blocking_notifier_unregister(dev, &table->mdev_nb);
514 mlx5_vhca_event_notifier_unregister(table->dev, &table->vhca_nb);
515 mlx5_esw_event_notifier_unregister(dev->priv.eswitch, &table->esw_nb);
516 mutex_destroy(&table->sf_state_lock);
517 WARN_ON(!xa_empty(&table->function_ids));
518 kfree(table);
519 }
520