2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/etherdevice.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/mlx5_ifc.h>
36 #include <linux/mlx5/vport.h>
37 #include <linux/mlx5/fs.h>
38 #include "mlx5_core.h"
41 #define UPLINK_VPORT 0xFFFF
43 #define MLX5_DEBUG_ESWITCH_MASK BIT(3)
45 #define esw_info(dev, format, ...) \
46 pr_info("(%s): E-Switch: " format, (dev)->priv.name, ##__VA_ARGS__)
48 #define esw_warn(dev, format, ...) \
49 pr_warn("(%s): E-Switch: " format, (dev)->priv.name, ##__VA_ARGS__)
51 #define esw_debug(dev, format, ...) \
52 mlx5_core_dbg_mask(dev, MLX5_DEBUG_ESWITCH_MASK, format, ##__VA_ARGS__)
60 /* E-Switch UC L2 table hash node */
62 struct l2addr_node node;
67 /* E-Switch MC FDB table hash node */
68 struct esw_mc_addr { /* SRIOV only */
69 struct l2addr_node node;
70 struct mlx5_flow_rule *uplink_rule; /* Forward to uplink rule */
74 /* Vport UC/MC hash node */
76 struct l2addr_node node;
79 struct mlx5_flow_rule *flow_rule; /* SRIOV only */
83 UC_ADDR_CHANGE = BIT(0),
84 MC_ADDR_CHANGE = BIT(1),
87 /* Vport context events */
88 #define SRIOV_VPORT_EVENTS (UC_ADDR_CHANGE | \
91 static int arm_vport_context_events_cmd(struct mlx5_core_dev *dev, u16 vport,
94 int in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)];
95 int out[MLX5_ST_SZ_DW(modify_nic_vport_context_out)];
99 memset(out, 0, sizeof(out));
100 memset(in, 0, sizeof(in));
102 MLX5_SET(modify_nic_vport_context_in, in,
103 opcode, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
104 MLX5_SET(modify_nic_vport_context_in, in, field_select.change_event, 1);
105 MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
107 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
108 nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
109 in, nic_vport_context);
111 MLX5_SET(nic_vport_context, nic_vport_ctx, arm_change_event, 1);
113 if (events_mask & UC_ADDR_CHANGE)
114 MLX5_SET(nic_vport_context, nic_vport_ctx,
115 event_on_uc_address_change, 1);
116 if (events_mask & MC_ADDR_CHANGE)
117 MLX5_SET(nic_vport_context, nic_vport_ctx,
118 event_on_mc_address_change, 1);
120 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
123 err = mlx5_cmd_status_to_err_v2(out);
131 /* E-Switch vport context HW commands */
132 static int query_esw_vport_context_cmd(struct mlx5_core_dev *mdev, u32 vport,
133 u32 *out, int outlen)
135 u32 in[MLX5_ST_SZ_DW(query_esw_vport_context_in)];
137 memset(in, 0, sizeof(in));
139 MLX5_SET(query_nic_vport_context_in, in, opcode,
140 MLX5_CMD_OP_QUERY_ESW_VPORT_CONTEXT);
142 MLX5_SET(query_esw_vport_context_in, in, vport_number, vport);
144 MLX5_SET(query_esw_vport_context_in, in, other_vport, 1);
146 return mlx5_cmd_exec_check_status(mdev, in, sizeof(in), out, outlen);
149 static int query_esw_vport_cvlan(struct mlx5_core_dev *dev, u32 vport,
152 u32 out[MLX5_ST_SZ_DW(query_esw_vport_context_out)];
157 memset(out, 0, sizeof(out));
162 if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
163 !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
166 err = query_esw_vport_context_cmd(dev, vport, out, sizeof(out));
170 cvlan_strip = MLX5_GET(query_esw_vport_context_out, out,
171 esw_vport_context.vport_cvlan_strip);
173 cvlan_insert = MLX5_GET(query_esw_vport_context_out, out,
174 esw_vport_context.vport_cvlan_insert);
176 if (cvlan_strip || cvlan_insert) {
177 *vlan = MLX5_GET(query_esw_vport_context_out, out,
178 esw_vport_context.cvlan_id);
179 *qos = MLX5_GET(query_esw_vport_context_out, out,
180 esw_vport_context.cvlan_pcp);
183 esw_debug(dev, "Query Vport[%d] cvlan: VLAN %d qos=%d\n",
189 static int modify_esw_vport_context_cmd(struct mlx5_core_dev *dev, u16 vport,
192 u32 out[MLX5_ST_SZ_DW(modify_esw_vport_context_out)];
194 memset(out, 0, sizeof(out));
196 MLX5_SET(modify_esw_vport_context_in, in, vport_number, vport);
198 MLX5_SET(modify_esw_vport_context_in, in, other_vport, 1);
200 MLX5_SET(modify_esw_vport_context_in, in, opcode,
201 MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT);
203 return mlx5_cmd_exec_check_status(dev, in, inlen,
207 static int modify_esw_vport_cvlan(struct mlx5_core_dev *dev, u32 vport,
208 u16 vlan, u8 qos, bool set)
210 u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)];
212 memset(in, 0, sizeof(in));
214 if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
215 !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
218 esw_debug(dev, "Set Vport[%d] VLAN %d qos %d set=%d\n",
219 vport, vlan, qos, set);
222 MLX5_SET(modify_esw_vport_context_in, in,
223 esw_vport_context.vport_cvlan_strip, 1);
224 /* insert only if no vlan in packet */
225 MLX5_SET(modify_esw_vport_context_in, in,
226 esw_vport_context.vport_cvlan_insert, 1);
227 MLX5_SET(modify_esw_vport_context_in, in,
228 esw_vport_context.cvlan_pcp, qos);
229 MLX5_SET(modify_esw_vport_context_in, in,
230 esw_vport_context.cvlan_id, vlan);
233 MLX5_SET(modify_esw_vport_context_in, in,
234 field_select.vport_cvlan_strip, 1);
235 MLX5_SET(modify_esw_vport_context_in, in,
236 field_select.vport_cvlan_insert, 1);
238 return modify_esw_vport_context_cmd(dev, vport, in, sizeof(in));
241 /* HW L2 Table (MPFS) management */
242 static int set_l2_table_entry_cmd(struct mlx5_core_dev *dev, u32 index,
243 u8 *mac, u8 vlan_valid, u16 vlan)
245 u32 in[MLX5_ST_SZ_DW(set_l2_table_entry_in)];
246 u32 out[MLX5_ST_SZ_DW(set_l2_table_entry_out)];
249 memset(in, 0, sizeof(in));
250 memset(out, 0, sizeof(out));
252 MLX5_SET(set_l2_table_entry_in, in, opcode,
253 MLX5_CMD_OP_SET_L2_TABLE_ENTRY);
254 MLX5_SET(set_l2_table_entry_in, in, table_index, index);
255 MLX5_SET(set_l2_table_entry_in, in, vlan_valid, vlan_valid);
256 MLX5_SET(set_l2_table_entry_in, in, vlan, vlan);
258 in_mac_addr = MLX5_ADDR_OF(set_l2_table_entry_in, in, mac_address);
259 ether_addr_copy(&in_mac_addr[2], mac);
261 return mlx5_cmd_exec_check_status(dev, in, sizeof(in),
265 static int del_l2_table_entry_cmd(struct mlx5_core_dev *dev, u32 index)
267 u32 in[MLX5_ST_SZ_DW(delete_l2_table_entry_in)];
268 u32 out[MLX5_ST_SZ_DW(delete_l2_table_entry_out)];
270 memset(in, 0, sizeof(in));
271 memset(out, 0, sizeof(out));
273 MLX5_SET(delete_l2_table_entry_in, in, opcode,
274 MLX5_CMD_OP_DELETE_L2_TABLE_ENTRY);
275 MLX5_SET(delete_l2_table_entry_in, in, table_index, index);
276 return mlx5_cmd_exec_check_status(dev, in, sizeof(in),
280 static int alloc_l2_table_index(struct mlx5_l2_table *l2_table, u32 *ix)
284 *ix = find_first_zero_bit(l2_table->bitmap, l2_table->size);
285 if (*ix >= l2_table->size)
288 __set_bit(*ix, l2_table->bitmap);
293 static void free_l2_table_index(struct mlx5_l2_table *l2_table, u32 ix)
295 __clear_bit(ix, l2_table->bitmap);
298 static int set_l2_table_entry(struct mlx5_core_dev *dev, u8 *mac,
299 u8 vlan_valid, u16 vlan,
302 struct mlx5_l2_table *l2_table = &dev->priv.eswitch->l2_table;
305 err = alloc_l2_table_index(l2_table, index);
309 err = set_l2_table_entry_cmd(dev, *index, mac, vlan_valid, vlan);
311 free_l2_table_index(l2_table, *index);
316 static void del_l2_table_entry(struct mlx5_core_dev *dev, u32 index)
318 struct mlx5_l2_table *l2_table = &dev->priv.eswitch->l2_table;
320 del_l2_table_entry_cmd(dev, index);
321 free_l2_table_index(l2_table, index);
325 static struct mlx5_flow_rule *
326 esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u8 mac[ETH_ALEN], u32 vport)
328 int match_header = MLX5_MATCH_OUTER_HEADERS;
329 struct mlx5_flow_destination dest;
330 struct mlx5_flow_rule *flow_rule = NULL;
336 match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
337 match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
338 if (!match_v || !match_c) {
339 pr_warn("FDB: Failed to alloc match parameters\n");
342 dmac_v = MLX5_ADDR_OF(fte_match_param, match_v,
343 outer_headers.dmac_47_16);
344 dmac_c = MLX5_ADDR_OF(fte_match_param, match_c,
345 outer_headers.dmac_47_16);
347 ether_addr_copy(dmac_v, mac);
348 /* Match criteria mask */
349 memset(dmac_c, 0xff, 6);
351 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
352 dest.vport_num = vport;
355 "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
356 dmac_v, dmac_c, vport);
358 mlx5_add_flow_rule(esw->fdb_table.fdb,
362 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST,
364 if (IS_ERR_OR_NULL(flow_rule)) {
366 "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
367 dmac_v, dmac_c, vport, PTR_ERR(flow_rule));
376 static int esw_create_fdb_table(struct mlx5_eswitch *esw, int nvports)
378 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
379 struct mlx5_core_dev *dev = esw->dev;
380 struct mlx5_flow_namespace *root_ns;
381 struct mlx5_flow_table *fdb;
382 struct mlx5_flow_group *g;
383 void *match_criteria;
389 esw_debug(dev, "Create FDB log_max_size(%d)\n",
390 MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size));
392 root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
394 esw_warn(dev, "Failed to get FDB flow namespace\n");
398 flow_group_in = mlx5_vzalloc(inlen);
401 memset(flow_group_in, 0, inlen);
403 table_size = BIT(MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size));
404 fdb = mlx5_create_flow_table(root_ns, 0, table_size, 0);
405 if (IS_ERR_OR_NULL(fdb)) {
407 esw_warn(dev, "Failed to create FDB Table err %d\n", err);
411 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
412 MLX5_MATCH_OUTER_HEADERS);
413 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
414 dmac = MLX5_ADDR_OF(fte_match_param, match_criteria, outer_headers.dmac_47_16);
415 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
416 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, table_size - 1);
417 eth_broadcast_addr(dmac);
419 g = mlx5_create_flow_group(fdb, flow_group_in);
420 if (IS_ERR_OR_NULL(g)) {
422 esw_warn(dev, "Failed to create flow group err(%d)\n", err);
426 esw->fdb_table.addr_grp = g;
427 esw->fdb_table.fdb = fdb;
429 kfree(flow_group_in);
430 if (err && !IS_ERR_OR_NULL(fdb))
431 mlx5_destroy_flow_table(fdb);
435 static void esw_destroy_fdb_table(struct mlx5_eswitch *esw)
437 if (!esw->fdb_table.fdb)
440 esw_debug(esw->dev, "Destroy FDB Table\n");
441 mlx5_destroy_flow_group(esw->fdb_table.addr_grp);
442 mlx5_destroy_flow_table(esw->fdb_table.fdb);
443 esw->fdb_table.fdb = NULL;
444 esw->fdb_table.addr_grp = NULL;
447 /* E-Switch vport UC/MC lists management */
448 typedef int (*vport_addr_action)(struct mlx5_eswitch *esw,
449 struct vport_addr *vaddr);
451 static int esw_add_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
453 struct hlist_head *hash = esw->l2_table.l2_hash;
454 struct esw_uc_addr *esw_uc;
455 u8 *mac = vaddr->node.addr;
456 u32 vport = vaddr->vport;
459 esw_uc = l2addr_hash_find(hash, mac, struct esw_uc_addr);
462 "Failed to set L2 mac(%pM) for vport(%d), mac is already in use by vport(%d)\n",
463 mac, vport, esw_uc->vport);
467 esw_uc = l2addr_hash_add(hash, mac, struct esw_uc_addr, GFP_KERNEL);
470 esw_uc->vport = vport;
472 err = set_l2_table_entry(esw->dev, mac, 0, 0, &esw_uc->table_index);
476 if (esw->fdb_table.fdb) /* SRIOV is enabled: Forward UC MAC to vport */
477 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
479 esw_debug(esw->dev, "\tADDED UC MAC: vport[%d] %pM index:%d fr(%p)\n",
480 vport, mac, esw_uc->table_index, vaddr->flow_rule);
483 l2addr_hash_del(esw_uc);
487 static int esw_del_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
489 struct hlist_head *hash = esw->l2_table.l2_hash;
490 struct esw_uc_addr *esw_uc;
491 u8 *mac = vaddr->node.addr;
492 u32 vport = vaddr->vport;
494 esw_uc = l2addr_hash_find(hash, mac, struct esw_uc_addr);
495 if (!esw_uc || esw_uc->vport != vport) {
497 "MAC(%pM) doesn't belong to vport (%d)\n",
501 esw_debug(esw->dev, "\tDELETE UC MAC: vport[%d] %pM index:%d fr(%p)\n",
502 vport, mac, esw_uc->table_index, vaddr->flow_rule);
504 del_l2_table_entry(esw->dev, esw_uc->table_index);
506 if (vaddr->flow_rule)
507 mlx5_del_flow_rule(vaddr->flow_rule);
508 vaddr->flow_rule = NULL;
510 l2addr_hash_del(esw_uc);
514 static int esw_add_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
516 struct hlist_head *hash = esw->mc_table;
517 struct esw_mc_addr *esw_mc;
518 u8 *mac = vaddr->node.addr;
519 u32 vport = vaddr->vport;
521 if (!esw->fdb_table.fdb)
524 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
528 esw_mc = l2addr_hash_add(hash, mac, struct esw_mc_addr, GFP_KERNEL);
532 esw_mc->uplink_rule = /* Forward MC MAC to Uplink */
533 esw_fdb_set_vport_rule(esw, mac, UPLINK_VPORT);
536 /* Forward MC MAC to vport */
537 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
539 "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
540 vport, mac, vaddr->flow_rule,
541 esw_mc->refcnt, esw_mc->uplink_rule);
545 static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
547 struct hlist_head *hash = esw->mc_table;
548 struct esw_mc_addr *esw_mc;
549 u8 *mac = vaddr->node.addr;
550 u32 vport = vaddr->vport;
552 if (!esw->fdb_table.fdb)
555 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
558 "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
563 "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
564 vport, mac, vaddr->flow_rule, esw_mc->refcnt,
565 esw_mc->uplink_rule);
567 if (vaddr->flow_rule)
568 mlx5_del_flow_rule(vaddr->flow_rule);
569 vaddr->flow_rule = NULL;
571 if (--esw_mc->refcnt)
574 if (esw_mc->uplink_rule)
575 mlx5_del_flow_rule(esw_mc->uplink_rule);
577 l2addr_hash_del(esw_mc);
581 /* Apply vport UC/MC list to HW l2 table and FDB table */
582 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
583 u32 vport_num, int list_type)
585 struct mlx5_vport *vport = &esw->vports[vport_num];
586 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
587 vport_addr_action vport_addr_add;
588 vport_addr_action vport_addr_del;
589 struct vport_addr *addr;
590 struct l2addr_node *node;
591 struct hlist_head *hash;
592 struct hlist_node *tmp;
595 vport_addr_add = is_uc ? esw_add_uc_addr :
597 vport_addr_del = is_uc ? esw_del_uc_addr :
600 hash = is_uc ? vport->uc_list : vport->mc_list;
601 for_each_l2hash_node(node, tmp, hash, hi) {
602 addr = container_of(node, struct vport_addr, node);
603 switch (addr->action) {
604 case MLX5_ACTION_ADD:
605 vport_addr_add(esw, addr);
606 addr->action = MLX5_ACTION_NONE;
608 case MLX5_ACTION_DEL:
609 vport_addr_del(esw, addr);
610 l2addr_hash_del(addr);
616 /* Sync vport UC/MC list from vport context */
617 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
618 u32 vport_num, int list_type)
620 struct mlx5_vport *vport = &esw->vports[vport_num];
621 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
622 u8 (*mac_list)[ETH_ALEN];
623 struct l2addr_node *node;
624 struct vport_addr *addr;
625 struct hlist_head *hash;
626 struct hlist_node *tmp;
632 size = is_uc ? MLX5_MAX_UC_PER_VPORT(esw->dev) :
633 MLX5_MAX_MC_PER_VPORT(esw->dev);
635 mac_list = kcalloc(size, ETH_ALEN, GFP_KERNEL);
639 hash = is_uc ? vport->uc_list : vport->mc_list;
641 for_each_l2hash_node(node, tmp, hash, hi) {
642 addr = container_of(node, struct vport_addr, node);
643 addr->action = MLX5_ACTION_DEL;
646 err = mlx5_query_nic_vport_mac_list(esw->dev, vport_num, list_type,
650 esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
651 vport_num, is_uc ? "UC" : "MC", size);
653 for (i = 0; i < size; i++) {
654 if (is_uc && !is_valid_ether_addr(mac_list[i]))
657 if (!is_uc && !is_multicast_ether_addr(mac_list[i]))
660 addr = l2addr_hash_find(hash, mac_list[i], struct vport_addr);
662 addr->action = MLX5_ACTION_NONE;
666 addr = l2addr_hash_add(hash, mac_list[i], struct vport_addr,
670 "Failed to add MAC(%pM) to vport[%d] DB\n",
671 mac_list[i], vport_num);
674 addr->vport = vport_num;
675 addr->action = MLX5_ACTION_ADD;
680 static void esw_vport_change_handler(struct work_struct *work)
682 struct mlx5_vport *vport =
683 container_of(work, struct mlx5_vport, vport_change_handler);
684 struct mlx5_core_dev *dev = vport->dev;
685 struct mlx5_eswitch *esw = dev->priv.eswitch;
688 mlx5_query_nic_vport_mac_address(dev, vport->vport, mac);
689 esw_debug(dev, "vport[%d] Context Changed: perm mac: %pM\n",
692 if (vport->enabled_events & UC_ADDR_CHANGE) {
693 esw_update_vport_addr_list(esw, vport->vport,
694 MLX5_NVPRT_LIST_TYPE_UC);
695 esw_apply_vport_addr_list(esw, vport->vport,
696 MLX5_NVPRT_LIST_TYPE_UC);
699 if (vport->enabled_events & MC_ADDR_CHANGE) {
700 esw_update_vport_addr_list(esw, vport->vport,
701 MLX5_NVPRT_LIST_TYPE_MC);
702 esw_apply_vport_addr_list(esw, vport->vport,
703 MLX5_NVPRT_LIST_TYPE_MC);
706 esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
708 arm_vport_context_events_cmd(dev, vport->vport,
709 vport->enabled_events);
712 static void esw_enable_vport(struct mlx5_eswitch *esw, int vport_num,
715 struct mlx5_vport *vport = &esw->vports[vport_num];
718 WARN_ON(vport->enabled);
720 esw_debug(esw->dev, "Enabling VPORT(%d)\n", vport_num);
721 mlx5_modify_vport_admin_state(esw->dev,
722 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
724 MLX5_ESW_VPORT_ADMIN_STATE_AUTO);
726 /* Sync with current vport context */
727 vport->enabled_events = enable_events;
728 esw_vport_change_handler(&vport->vport_change_handler);
730 spin_lock_irqsave(&vport->lock, flags);
731 vport->enabled = true;
732 spin_unlock_irqrestore(&vport->lock, flags);
734 arm_vport_context_events_cmd(esw->dev, vport_num, enable_events);
736 esw->enabled_vports++;
737 esw_debug(esw->dev, "Enabled VPORT(%d)\n", vport_num);
740 static void esw_cleanup_vport(struct mlx5_eswitch *esw, u16 vport_num)
742 struct mlx5_vport *vport = &esw->vports[vport_num];
743 struct l2addr_node *node;
744 struct vport_addr *addr;
745 struct hlist_node *tmp;
748 for_each_l2hash_node(node, tmp, vport->uc_list, hi) {
749 addr = container_of(node, struct vport_addr, node);
750 addr->action = MLX5_ACTION_DEL;
752 esw_apply_vport_addr_list(esw, vport_num, MLX5_NVPRT_LIST_TYPE_UC);
754 for_each_l2hash_node(node, tmp, vport->mc_list, hi) {
755 addr = container_of(node, struct vport_addr, node);
756 addr->action = MLX5_ACTION_DEL;
758 esw_apply_vport_addr_list(esw, vport_num, MLX5_NVPRT_LIST_TYPE_MC);
761 static void esw_disable_vport(struct mlx5_eswitch *esw, int vport_num)
763 struct mlx5_vport *vport = &esw->vports[vport_num];
769 esw_debug(esw->dev, "Disabling vport(%d)\n", vport_num);
770 /* Mark this vport as disabled to discard new events */
771 spin_lock_irqsave(&vport->lock, flags);
772 vport->enabled = false;
773 vport->enabled_events = 0;
774 spin_unlock_irqrestore(&vport->lock, flags);
776 mlx5_modify_vport_admin_state(esw->dev,
777 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
779 MLX5_ESW_VPORT_ADMIN_STATE_DOWN);
780 /* Wait for current already scheduled events to complete */
781 flush_workqueue(esw->work_queue);
782 /* Disable events from this vport */
783 arm_vport_context_events_cmd(esw->dev, vport->vport, 0);
784 /* We don't assume VFs will cleanup after themselves */
785 esw_cleanup_vport(esw, vport_num);
786 esw->enabled_vports--;
789 /* Public E-Switch API */
790 int mlx5_eswitch_enable_sriov(struct mlx5_eswitch *esw, int nvfs)
795 if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager) ||
796 MLX5_CAP_GEN(esw->dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
799 if (!MLX5_CAP_GEN(esw->dev, eswitch_flow_table) ||
800 !MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ft_support)) {
801 esw_warn(esw->dev, "E-Switch FDB is not supported, aborting ...\n");
805 esw_info(esw->dev, "E-Switch enable SRIOV: nvfs(%d)\n", nvfs);
807 esw_disable_vport(esw, 0);
809 err = esw_create_fdb_table(esw, nvfs + 1);
813 for (i = 0; i <= nvfs; i++)
814 esw_enable_vport(esw, i, SRIOV_VPORT_EVENTS);
816 esw_info(esw->dev, "SRIOV enabled: active vports(%d)\n",
817 esw->enabled_vports);
821 esw_enable_vport(esw, 0, UC_ADDR_CHANGE);
825 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw)
829 if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager) ||
830 MLX5_CAP_GEN(esw->dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
833 esw_info(esw->dev, "disable SRIOV: active vports(%d)\n",
834 esw->enabled_vports);
836 for (i = 0; i < esw->total_vports; i++)
837 esw_disable_vport(esw, i);
839 esw_destroy_fdb_table(esw);
841 /* VPORT 0 (PF) must be enabled back with non-sriov configuration */
842 esw_enable_vport(esw, 0, UC_ADDR_CHANGE);
845 int mlx5_eswitch_init(struct mlx5_core_dev *dev)
847 int l2_table_size = 1 << MLX5_CAP_GEN(dev, log_max_l2_table);
848 int total_vports = MLX5_TOTAL_VPORTS(dev);
849 struct mlx5_eswitch *esw;
853 if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
854 MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
858 "Total vports %d, l2 table size(%d), per vport: max uc(%d) max mc(%d)\n",
859 total_vports, l2_table_size,
860 MLX5_MAX_UC_PER_VPORT(dev),
861 MLX5_MAX_MC_PER_VPORT(dev));
863 esw = kzalloc(sizeof(*esw), GFP_KERNEL);
869 esw->l2_table.bitmap = kcalloc(BITS_TO_LONGS(l2_table_size),
870 sizeof(uintptr_t), GFP_KERNEL);
871 if (!esw->l2_table.bitmap) {
875 esw->l2_table.size = l2_table_size;
877 esw->work_queue = create_singlethread_workqueue("mlx5_esw_wq");
878 if (!esw->work_queue) {
883 esw->vports = kcalloc(total_vports, sizeof(struct mlx5_vport),
890 for (vport_num = 0; vport_num < total_vports; vport_num++) {
891 struct mlx5_vport *vport = &esw->vports[vport_num];
893 vport->vport = vport_num;
895 INIT_WORK(&vport->vport_change_handler,
896 esw_vport_change_handler);
897 spin_lock_init(&vport->lock);
900 esw->total_vports = total_vports;
901 esw->enabled_vports = 0;
903 dev->priv.eswitch = esw;
904 esw_enable_vport(esw, 0, UC_ADDR_CHANGE);
905 /* VF Vports will be enabled when SRIOV is enabled */
909 destroy_workqueue(esw->work_queue);
910 kfree(esw->l2_table.bitmap);
916 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
918 if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager) ||
919 MLX5_CAP_GEN(esw->dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
922 esw_info(esw->dev, "cleanup\n");
923 esw_disable_vport(esw, 0);
925 esw->dev->priv.eswitch = NULL;
926 destroy_workqueue(esw->work_queue);
927 kfree(esw->l2_table.bitmap);
932 void mlx5_eswitch_vport_event(struct mlx5_eswitch *esw, struct mlx5_eqe *eqe)
934 struct mlx5_eqe_vport_change *vc_eqe = &eqe->data.vport_change;
935 u16 vport_num = be16_to_cpu(vc_eqe->vport_num);
936 struct mlx5_vport *vport;
939 pr_warn("MLX5 E-Switch: vport %d got an event while eswitch is not initialized\n",
944 vport = &esw->vports[vport_num];
945 spin_lock(&vport->lock);
947 queue_work(esw->work_queue, &vport->vport_change_handler);
948 spin_unlock(&vport->lock);
951 /* Vport Administration */
952 #define ESW_ALLOWED(esw) \
953 (esw && MLX5_CAP_GEN(esw->dev, vport_group_manager) && mlx5_core_is_pf(esw->dev))
954 #define LEGAL_VPORT(esw, vport) (vport >= 0 && vport < esw->total_vports)
956 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
957 int vport, u8 mac[ETH_ALEN])
961 if (!ESW_ALLOWED(esw))
963 if (!LEGAL_VPORT(esw, vport))
966 err = mlx5_modify_nic_vport_mac_address(esw->dev, vport, mac);
968 mlx5_core_warn(esw->dev,
969 "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
977 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
978 int vport, int link_state)
980 if (!ESW_ALLOWED(esw))
982 if (!LEGAL_VPORT(esw, vport))
985 return mlx5_modify_vport_admin_state(esw->dev,
986 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
990 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
991 int vport, struct ifla_vf_info *ivi)
996 if (!ESW_ALLOWED(esw))
998 if (!LEGAL_VPORT(esw, vport))
1001 memset(ivi, 0, sizeof(*ivi));
1002 ivi->vf = vport - 1;
1004 mlx5_query_nic_vport_mac_address(esw->dev, vport, ivi->mac);
1005 ivi->linkstate = mlx5_query_vport_admin_state(esw->dev,
1006 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
1008 query_esw_vport_cvlan(esw->dev, vport, &vlan, &qos);
1016 int mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
1017 int vport, u16 vlan, u8 qos)
1021 if (!ESW_ALLOWED(esw))
1023 if (!LEGAL_VPORT(esw, vport) || (vlan > 4095) || (qos > 7))
1029 return modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set);
1032 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
1034 struct ifla_vf_stats *vf_stats)
1036 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
1037 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
1041 if (!ESW_ALLOWED(esw))
1043 if (!LEGAL_VPORT(esw, vport))
1046 out = mlx5_vzalloc(outlen);
1050 memset(in, 0, sizeof(in));
1052 MLX5_SET(query_vport_counter_in, in, opcode,
1053 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
1054 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
1055 MLX5_SET(query_vport_counter_in, in, vport_number, vport);
1057 MLX5_SET(query_vport_counter_in, in, other_vport, 1);
1059 memset(out, 0, outlen);
1060 err = mlx5_cmd_exec(esw->dev, in, sizeof(in), out, outlen);
1064 #define MLX5_GET_CTR(p, x) \
1065 MLX5_GET64(query_vport_counter_out, p, x)
1067 memset(vf_stats, 0, sizeof(*vf_stats));
1068 vf_stats->rx_packets =
1069 MLX5_GET_CTR(out, received_eth_unicast.packets) +
1070 MLX5_GET_CTR(out, received_eth_multicast.packets) +
1071 MLX5_GET_CTR(out, received_eth_broadcast.packets);
1073 vf_stats->rx_bytes =
1074 MLX5_GET_CTR(out, received_eth_unicast.octets) +
1075 MLX5_GET_CTR(out, received_eth_multicast.octets) +
1076 MLX5_GET_CTR(out, received_eth_broadcast.octets);
1078 vf_stats->tx_packets =
1079 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
1080 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
1081 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
1083 vf_stats->tx_bytes =
1084 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
1085 MLX5_GET_CTR(out, transmitted_eth_multicast.octets) +
1086 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
1088 vf_stats->multicast =
1089 MLX5_GET_CTR(out, received_eth_multicast.packets);
1091 vf_stats->broadcast =
1092 MLX5_GET_CTR(out, received_eth_broadcast.packets);