2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/string.h>
35 #include <linux/etherdevice.h>
37 #include <linux/mlx4/cmd.h>
38 #include <linux/export.h>
42 #define MGM_QPN_MASK 0x00FFFFFF
43 #define MGM_BLCK_LB_BIT 30
45 static const u8 zero_gid[16]; /* automatically initialized to 0 */
48 __be32 next_gid_index;
52 __be32 qp[MLX4_MAX_QP_PER_MGM];
55 int mlx4_get_mgm_entry_size(struct mlx4_dev *dev)
57 return 1 << dev->oper_log_mgm_entry_size;
60 int mlx4_get_qp_per_mgm(struct mlx4_dev *dev)
62 return 4 * (mlx4_get_mgm_entry_size(dev) / 16 - 2);
65 static int mlx4_QP_FLOW_STEERING_ATTACH(struct mlx4_dev *dev,
66 struct mlx4_cmd_mailbox *mailbox,
73 err = mlx4_cmd_imm(dev, mailbox->dma, &imm, size, 0,
74 MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
83 static int mlx4_QP_FLOW_STEERING_DETACH(struct mlx4_dev *dev, u64 regid)
87 err = mlx4_cmd(dev, regid, 0, 0,
88 MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
94 static int mlx4_READ_ENTRY(struct mlx4_dev *dev, int index,
95 struct mlx4_cmd_mailbox *mailbox)
97 return mlx4_cmd_box(dev, 0, mailbox->dma, index, 0, MLX4_CMD_READ_MCG,
98 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
101 static int mlx4_WRITE_ENTRY(struct mlx4_dev *dev, int index,
102 struct mlx4_cmd_mailbox *mailbox)
104 return mlx4_cmd(dev, mailbox->dma, index, 0, MLX4_CMD_WRITE_MCG,
105 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
108 static int mlx4_WRITE_PROMISC(struct mlx4_dev *dev, u8 port, u8 steer,
109 struct mlx4_cmd_mailbox *mailbox)
113 in_mod = (u32) port << 16 | steer << 1;
114 return mlx4_cmd(dev, mailbox->dma, in_mod, 0x1,
115 MLX4_CMD_WRITE_MCG, MLX4_CMD_TIME_CLASS_A,
119 static int mlx4_GID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
120 u16 *hash, u8 op_mod)
125 err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, op_mod,
126 MLX4_CMD_MGID_HASH, MLX4_CMD_TIME_CLASS_A,
135 static struct mlx4_promisc_qp *get_promisc_qp(struct mlx4_dev *dev, u8 port,
136 enum mlx4_steer_type steer,
139 struct mlx4_steer *s_steer = &mlx4_priv(dev)->steer[port - 1];
140 struct mlx4_promisc_qp *pqp;
142 list_for_each_entry(pqp, &s_steer->promisc_qps[steer], list) {
151 * Add new entry to steering data structure.
152 * All promisc QPs should be added as well
154 static int new_steering_entry(struct mlx4_dev *dev, u8 port,
155 enum mlx4_steer_type steer,
156 unsigned int index, u32 qpn)
158 struct mlx4_steer *s_steer;
159 struct mlx4_cmd_mailbox *mailbox;
160 struct mlx4_mgm *mgm;
162 struct mlx4_steer_index *new_entry;
163 struct mlx4_promisc_qp *pqp;
164 struct mlx4_promisc_qp *dqp = NULL;
168 s_steer = &mlx4_priv(dev)->steer[port - 1];
169 new_entry = kzalloc(sizeof *new_entry, GFP_KERNEL);
173 INIT_LIST_HEAD(&new_entry->duplicates);
174 new_entry->index = index;
175 list_add_tail(&new_entry->list, &s_steer->steer_entries[steer]);
177 /* If the given qpn is also a promisc qp,
178 * it should be inserted to duplicates list
180 pqp = get_promisc_qp(dev, port, steer, qpn);
182 dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
188 list_add_tail(&dqp->list, &new_entry->duplicates);
191 /* if no promisc qps for this vep, we are done */
192 if (list_empty(&s_steer->promisc_qps[steer]))
195 /* now need to add all the promisc qps to the new
196 * steering entry, as they should also receive the packets
197 * destined to this address */
198 mailbox = mlx4_alloc_cmd_mailbox(dev);
199 if (IS_ERR(mailbox)) {
205 err = mlx4_READ_ENTRY(dev, index, mailbox);
209 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
210 prot = be32_to_cpu(mgm->members_count) >> 30;
211 list_for_each_entry(pqp, &s_steer->promisc_qps[steer], list) {
212 /* don't add already existing qpn */
215 if (members_count == dev->caps.num_qp_per_mgm) {
222 mgm->qp[members_count++] = cpu_to_be32(pqp->qpn & MGM_QPN_MASK);
224 /* update the qps count and update the entry with all the promisc qps*/
225 mgm->members_count = cpu_to_be32(members_count | (prot << 30));
226 err = mlx4_WRITE_ENTRY(dev, index, mailbox);
229 mlx4_free_cmd_mailbox(dev, mailbox);
234 list_del(&dqp->list);
237 list_del(&new_entry->list);
242 /* update the data structures with existing steering entry */
243 static int existing_steering_entry(struct mlx4_dev *dev, u8 port,
244 enum mlx4_steer_type steer,
245 unsigned int index, u32 qpn)
247 struct mlx4_steer *s_steer;
248 struct mlx4_steer_index *tmp_entry, *entry = NULL;
249 struct mlx4_promisc_qp *pqp;
250 struct mlx4_promisc_qp *dqp;
252 s_steer = &mlx4_priv(dev)->steer[port - 1];
254 pqp = get_promisc_qp(dev, port, steer, qpn);
256 return 0; /* nothing to do */
258 list_for_each_entry(tmp_entry, &s_steer->steer_entries[steer], list) {
259 if (tmp_entry->index == index) {
264 if (unlikely(!entry)) {
265 mlx4_warn(dev, "Steering entry at index %x is not registered\n", index);
269 /* the given qpn is listed as a promisc qpn
270 * we need to add it as a duplicate to this entry
271 * for future references */
272 list_for_each_entry(dqp, &entry->duplicates, list) {
274 return 0; /* qp is already duplicated */
277 /* add the qp as a duplicate on this index */
278 dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
282 list_add_tail(&dqp->list, &entry->duplicates);
287 /* Check whether a qpn is a duplicate on steering entry
288 * If so, it should not be removed from mgm */
289 static bool check_duplicate_entry(struct mlx4_dev *dev, u8 port,
290 enum mlx4_steer_type steer,
291 unsigned int index, u32 qpn)
293 struct mlx4_steer *s_steer;
294 struct mlx4_steer_index *tmp_entry, *entry = NULL;
295 struct mlx4_promisc_qp *dqp, *tmp_dqp;
297 s_steer = &mlx4_priv(dev)->steer[port - 1];
299 /* if qp is not promisc, it cannot be duplicated */
300 if (!get_promisc_qp(dev, port, steer, qpn))
303 /* The qp is promisc qp so it is a duplicate on this index
304 * Find the index entry, and remove the duplicate */
305 list_for_each_entry(tmp_entry, &s_steer->steer_entries[steer], list) {
306 if (tmp_entry->index == index) {
311 if (unlikely(!entry)) {
312 mlx4_warn(dev, "Steering entry for index %x is not registered\n", index);
315 list_for_each_entry_safe(dqp, tmp_dqp, &entry->duplicates, list) {
316 if (dqp->qpn == qpn) {
317 list_del(&dqp->list);
324 /* I a steering entry contains only promisc QPs, it can be removed. */
325 static bool can_remove_steering_entry(struct mlx4_dev *dev, u8 port,
326 enum mlx4_steer_type steer,
327 unsigned int index, u32 tqpn)
329 struct mlx4_steer *s_steer;
330 struct mlx4_cmd_mailbox *mailbox;
331 struct mlx4_mgm *mgm;
332 struct mlx4_steer_index *entry = NULL, *tmp_entry;
338 s_steer = &mlx4_priv(dev)->steer[port - 1];
340 mailbox = mlx4_alloc_cmd_mailbox(dev);
345 if (mlx4_READ_ENTRY(dev, index, mailbox))
347 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
348 for (i = 0; i < members_count; i++) {
349 qpn = be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK;
350 if (!get_promisc_qp(dev, port, steer, qpn) && qpn != tqpn) {
351 /* the qp is not promisc, the entry can't be removed */
355 /* All the qps currently registered for this entry are promiscuous,
356 * Checking for duplicates */
358 list_for_each_entry_safe(entry, tmp_entry, &s_steer->steer_entries[steer], list) {
359 if (entry->index == index) {
360 if (list_empty(&entry->duplicates)) {
361 list_del(&entry->list);
364 /* This entry contains duplicates so it shouldn't be removed */
372 mlx4_free_cmd_mailbox(dev, mailbox);
376 static int add_promisc_qp(struct mlx4_dev *dev, u8 port,
377 enum mlx4_steer_type steer, u32 qpn)
379 struct mlx4_steer *s_steer;
380 struct mlx4_cmd_mailbox *mailbox;
381 struct mlx4_mgm *mgm;
382 struct mlx4_steer_index *entry;
383 struct mlx4_promisc_qp *pqp;
384 struct mlx4_promisc_qp *dqp;
390 struct mlx4_priv *priv = mlx4_priv(dev);
392 s_steer = &mlx4_priv(dev)->steer[port - 1];
394 mutex_lock(&priv->mcg_table.mutex);
396 if (get_promisc_qp(dev, port, steer, qpn)) {
397 err = 0; /* Noting to do, already exists */
401 pqp = kmalloc(sizeof *pqp, GFP_KERNEL);
408 mailbox = mlx4_alloc_cmd_mailbox(dev);
409 if (IS_ERR(mailbox)) {
415 /* the promisc qp needs to be added for each one of the steering
416 * entries, if it already exists, needs to be added as a duplicate
418 list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
419 err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
423 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
424 prot = be32_to_cpu(mgm->members_count) >> 30;
426 for (i = 0; i < members_count; i++) {
427 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn) {
428 /* Entry already exists, add to duplicates */
429 dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
435 list_add_tail(&dqp->list, &entry->duplicates);
440 /* Need to add the qpn to mgm */
441 if (members_count == dev->caps.num_qp_per_mgm) {
446 mgm->qp[members_count++] = cpu_to_be32(qpn & MGM_QPN_MASK);
447 mgm->members_count = cpu_to_be32(members_count | (prot << 30));
448 err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
454 /* add the new qpn to list of promisc qps */
455 list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
456 /* now need to add all the promisc qps to default entry */
457 memset(mgm, 0, sizeof *mgm);
459 list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
460 mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
461 mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
463 err = mlx4_WRITE_PROMISC(dev, port, steer, mailbox);
467 mlx4_free_cmd_mailbox(dev, mailbox);
468 mutex_unlock(&priv->mcg_table.mutex);
472 list_del(&pqp->list);
474 mlx4_free_cmd_mailbox(dev, mailbox);
478 mutex_unlock(&priv->mcg_table.mutex);
482 static int remove_promisc_qp(struct mlx4_dev *dev, u8 port,
483 enum mlx4_steer_type steer, u32 qpn)
485 struct mlx4_priv *priv = mlx4_priv(dev);
486 struct mlx4_steer *s_steer;
487 struct mlx4_cmd_mailbox *mailbox;
488 struct mlx4_mgm *mgm;
489 struct mlx4_steer_index *entry;
490 struct mlx4_promisc_qp *pqp;
491 struct mlx4_promisc_qp *dqp;
494 bool back_to_list = false;
498 s_steer = &mlx4_priv(dev)->steer[port - 1];
499 mutex_lock(&priv->mcg_table.mutex);
501 pqp = get_promisc_qp(dev, port, steer, qpn);
502 if (unlikely(!pqp)) {
503 mlx4_warn(dev, "QP %x is not promiscuous QP\n", qpn);
509 /*remove from list of promisc qps */
510 list_del(&pqp->list);
512 /* set the default entry not to include the removed one */
513 mailbox = mlx4_alloc_cmd_mailbox(dev);
514 if (IS_ERR(mailbox)) {
520 memset(mgm, 0, sizeof *mgm);
522 list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
523 mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
524 mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
526 err = mlx4_WRITE_PROMISC(dev, port, steer, mailbox);
530 /* remove the qp from all the steering entries*/
531 list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
533 list_for_each_entry(dqp, &entry->duplicates, list) {
534 if (dqp->qpn == qpn) {
540 /* a duplicate, no need to change the mgm,
541 * only update the duplicates list */
542 list_del(&dqp->list);
545 err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
548 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
549 for (loc = -1, i = 0; i < members_count; ++i)
550 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn)
553 mgm->members_count = cpu_to_be32(--members_count |
554 (MLX4_PROT_ETH << 30));
555 mgm->qp[loc] = mgm->qp[i - 1];
558 err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
566 mlx4_free_cmd_mailbox(dev, mailbox);
569 list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
573 mutex_unlock(&priv->mcg_table.mutex);
578 * Caller must hold MCG table semaphore. gid and mgm parameters must
579 * be properly aligned for command interface.
581 * Returns 0 unless a firmware command error occurs.
583 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
584 * and *mgm holds MGM entry.
586 * if GID is found in AMGM, *index = index in AMGM, *prev = index of
587 * previous entry in hash chain and *mgm holds AMGM entry.
589 * If no AMGM exists for given gid, *index = -1, *prev = index of last
590 * entry in hash chain and *mgm holds end of hash chain.
592 static int find_entry(struct mlx4_dev *dev, u8 port,
593 u8 *gid, enum mlx4_protocol prot,
594 struct mlx4_cmd_mailbox *mgm_mailbox,
595 int *prev, int *index)
597 struct mlx4_cmd_mailbox *mailbox;
598 struct mlx4_mgm *mgm = mgm_mailbox->buf;
602 u8 op_mod = (prot == MLX4_PROT_ETH) ?
603 !!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) : 0;
605 mailbox = mlx4_alloc_cmd_mailbox(dev);
610 memcpy(mgid, gid, 16);
612 err = mlx4_GID_HASH(dev, mailbox, &hash, op_mod);
613 mlx4_free_cmd_mailbox(dev, mailbox);
618 mlx4_dbg(dev, "Hash for %pI6 is %04x\n", gid, hash);
624 err = mlx4_READ_ENTRY(dev, *index, mgm_mailbox);
628 if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
629 if (*index != hash) {
630 mlx4_err(dev, "Found zero MGID in AMGM.\n");
636 if (!memcmp(mgm->gid, gid, 16) &&
637 be32_to_cpu(mgm->members_count) >> 30 == prot)
641 *index = be32_to_cpu(mgm->next_gid_index) >> 6;
648 static const u8 __promisc_mode[] = {
649 [MLX4_FS_REGULAR] = 0x0,
650 [MLX4_FS_ALL_DEFAULT] = 0x1,
651 [MLX4_FS_MC_DEFAULT] = 0x3,
652 [MLX4_FS_UC_SNIFFER] = 0x4,
653 [MLX4_FS_MC_SNIFFER] = 0x5,
656 int mlx4_map_sw_to_hw_steering_mode(struct mlx4_dev *dev,
657 enum mlx4_net_trans_promisc_mode flow_type)
659 if (flow_type >= MLX4_FS_MODE_NUM || flow_type < 0) {
660 mlx4_err(dev, "Invalid flow type. type = %d\n", flow_type);
663 return __promisc_mode[flow_type];
665 EXPORT_SYMBOL_GPL(mlx4_map_sw_to_hw_steering_mode);
667 static void trans_rule_ctrl_to_hw(struct mlx4_net_trans_rule *ctrl,
668 struct mlx4_net_trans_rule_hw_ctrl *hw)
672 flags = ctrl->queue_mode == MLX4_NET_TRANS_Q_LIFO ? 1 : 0;
673 flags |= ctrl->exclusive ? (1 << 2) : 0;
674 flags |= ctrl->allow_loopback ? (1 << 3) : 0;
677 hw->type = __promisc_mode[ctrl->promisc_mode];
678 hw->prio = cpu_to_be16(ctrl->priority);
679 hw->port = ctrl->port;
680 hw->qpn = cpu_to_be32(ctrl->qpn);
683 const u16 __sw_id_hw[] = {
684 [MLX4_NET_TRANS_RULE_ID_ETH] = 0xE001,
685 [MLX4_NET_TRANS_RULE_ID_IB] = 0xE005,
686 [MLX4_NET_TRANS_RULE_ID_IPV6] = 0xE003,
687 [MLX4_NET_TRANS_RULE_ID_IPV4] = 0xE002,
688 [MLX4_NET_TRANS_RULE_ID_TCP] = 0xE004,
689 [MLX4_NET_TRANS_RULE_ID_UDP] = 0xE006
692 int mlx4_map_sw_to_hw_steering_id(struct mlx4_dev *dev,
693 enum mlx4_net_trans_rule_id id)
695 if (id >= MLX4_NET_TRANS_RULE_NUM || id < 0) {
696 mlx4_err(dev, "Invalid network rule id. id = %d\n", id);
699 return __sw_id_hw[id];
701 EXPORT_SYMBOL_GPL(mlx4_map_sw_to_hw_steering_id);
703 static const int __rule_hw_sz[] = {
704 [MLX4_NET_TRANS_RULE_ID_ETH] =
705 sizeof(struct mlx4_net_trans_rule_hw_eth),
706 [MLX4_NET_TRANS_RULE_ID_IB] =
707 sizeof(struct mlx4_net_trans_rule_hw_ib),
708 [MLX4_NET_TRANS_RULE_ID_IPV6] = 0,
709 [MLX4_NET_TRANS_RULE_ID_IPV4] =
710 sizeof(struct mlx4_net_trans_rule_hw_ipv4),
711 [MLX4_NET_TRANS_RULE_ID_TCP] =
712 sizeof(struct mlx4_net_trans_rule_hw_tcp_udp),
713 [MLX4_NET_TRANS_RULE_ID_UDP] =
714 sizeof(struct mlx4_net_trans_rule_hw_tcp_udp)
717 int mlx4_hw_rule_sz(struct mlx4_dev *dev,
718 enum mlx4_net_trans_rule_id id)
720 if (id >= MLX4_NET_TRANS_RULE_NUM || id < 0) {
721 mlx4_err(dev, "Invalid network rule id. id = %d\n", id);
725 return __rule_hw_sz[id];
727 EXPORT_SYMBOL_GPL(mlx4_hw_rule_sz);
729 static int parse_trans_rule(struct mlx4_dev *dev, struct mlx4_spec_list *spec,
730 struct _rule_hw *rule_hw)
732 if (mlx4_hw_rule_sz(dev, spec->id) < 0)
734 memset(rule_hw, 0, mlx4_hw_rule_sz(dev, spec->id));
735 rule_hw->id = cpu_to_be16(__sw_id_hw[spec->id]);
736 rule_hw->size = mlx4_hw_rule_sz(dev, spec->id) >> 2;
739 case MLX4_NET_TRANS_RULE_ID_ETH:
740 memcpy(rule_hw->eth.dst_mac, spec->eth.dst_mac, ETH_ALEN);
741 memcpy(rule_hw->eth.dst_mac_msk, spec->eth.dst_mac_msk,
743 memcpy(rule_hw->eth.src_mac, spec->eth.src_mac, ETH_ALEN);
744 memcpy(rule_hw->eth.src_mac_msk, spec->eth.src_mac_msk,
746 if (spec->eth.ether_type_enable) {
747 rule_hw->eth.ether_type_enable = 1;
748 rule_hw->eth.ether_type = spec->eth.ether_type;
750 rule_hw->eth.vlan_tag = spec->eth.vlan_id;
751 rule_hw->eth.vlan_tag_msk = spec->eth.vlan_id_msk;
754 case MLX4_NET_TRANS_RULE_ID_IB:
755 rule_hw->ib.l3_qpn = spec->ib.l3_qpn;
756 rule_hw->ib.qpn_mask = spec->ib.qpn_msk;
757 memcpy(&rule_hw->ib.dst_gid, &spec->ib.dst_gid, 16);
758 memcpy(&rule_hw->ib.dst_gid_msk, &spec->ib.dst_gid_msk, 16);
761 case MLX4_NET_TRANS_RULE_ID_IPV6:
764 case MLX4_NET_TRANS_RULE_ID_IPV4:
765 rule_hw->ipv4.src_ip = spec->ipv4.src_ip;
766 rule_hw->ipv4.src_ip_msk = spec->ipv4.src_ip_msk;
767 rule_hw->ipv4.dst_ip = spec->ipv4.dst_ip;
768 rule_hw->ipv4.dst_ip_msk = spec->ipv4.dst_ip_msk;
771 case MLX4_NET_TRANS_RULE_ID_TCP:
772 case MLX4_NET_TRANS_RULE_ID_UDP:
773 rule_hw->tcp_udp.dst_port = spec->tcp_udp.dst_port;
774 rule_hw->tcp_udp.dst_port_msk = spec->tcp_udp.dst_port_msk;
775 rule_hw->tcp_udp.src_port = spec->tcp_udp.src_port;
776 rule_hw->tcp_udp.src_port_msk = spec->tcp_udp.src_port_msk;
783 return __rule_hw_sz[spec->id];
786 static void mlx4_err_rule(struct mlx4_dev *dev, char *str,
787 struct mlx4_net_trans_rule *rule)
790 struct mlx4_spec_list *cur;
794 mlx4_err(dev, "%s", str);
795 len += snprintf(buf + len, BUF_SIZE - len,
796 "port = %d prio = 0x%x qp = 0x%x ",
797 rule->port, rule->priority, rule->qpn);
799 list_for_each_entry(cur, &rule->list, list) {
801 case MLX4_NET_TRANS_RULE_ID_ETH:
802 len += snprintf(buf + len, BUF_SIZE - len,
803 "dmac = %pM ", &cur->eth.dst_mac);
804 if (cur->eth.ether_type)
805 len += snprintf(buf + len, BUF_SIZE - len,
807 be16_to_cpu(cur->eth.ether_type));
808 if (cur->eth.vlan_id)
809 len += snprintf(buf + len, BUF_SIZE - len,
811 be16_to_cpu(cur->eth.vlan_id));
814 case MLX4_NET_TRANS_RULE_ID_IPV4:
815 if (cur->ipv4.src_ip)
816 len += snprintf(buf + len, BUF_SIZE - len,
819 if (cur->ipv4.dst_ip)
820 len += snprintf(buf + len, BUF_SIZE - len,
825 case MLX4_NET_TRANS_RULE_ID_TCP:
826 case MLX4_NET_TRANS_RULE_ID_UDP:
827 if (cur->tcp_udp.src_port)
828 len += snprintf(buf + len, BUF_SIZE - len,
830 be16_to_cpu(cur->tcp_udp.src_port));
831 if (cur->tcp_udp.dst_port)
832 len += snprintf(buf + len, BUF_SIZE - len,
834 be16_to_cpu(cur->tcp_udp.dst_port));
837 case MLX4_NET_TRANS_RULE_ID_IB:
838 len += snprintf(buf + len, BUF_SIZE - len,
839 "dst-gid = %pI6\n", cur->ib.dst_gid);
840 len += snprintf(buf + len, BUF_SIZE - len,
841 "dst-gid-mask = %pI6\n",
842 cur->ib.dst_gid_msk);
845 case MLX4_NET_TRANS_RULE_ID_IPV6:
852 len += snprintf(buf + len, BUF_SIZE - len, "\n");
853 mlx4_err(dev, "%s", buf);
856 mlx4_err(dev, "Network rule error message was truncated, print buffer is too small.\n");
859 int mlx4_flow_attach(struct mlx4_dev *dev,
860 struct mlx4_net_trans_rule *rule, u64 *reg_id)
862 struct mlx4_cmd_mailbox *mailbox;
863 struct mlx4_spec_list *cur;
867 mailbox = mlx4_alloc_cmd_mailbox(dev);
869 return PTR_ERR(mailbox);
871 memset(mailbox->buf, 0, sizeof(struct mlx4_net_trans_rule_hw_ctrl));
872 trans_rule_ctrl_to_hw(rule, mailbox->buf);
874 size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
876 list_for_each_entry(cur, &rule->list, list) {
877 ret = parse_trans_rule(dev, cur, mailbox->buf + size);
879 mlx4_free_cmd_mailbox(dev, mailbox);
885 ret = mlx4_QP_FLOW_STEERING_ATTACH(dev, mailbox, size >> 2, reg_id);
888 "mcg table is full. Fail to register network rule.\n",
891 mlx4_err_rule(dev, "Fail to register network rule.\n", rule);
893 mlx4_free_cmd_mailbox(dev, mailbox);
897 EXPORT_SYMBOL_GPL(mlx4_flow_attach);
899 int mlx4_flow_detach(struct mlx4_dev *dev, u64 reg_id)
903 err = mlx4_QP_FLOW_STEERING_DETACH(dev, reg_id);
905 mlx4_err(dev, "Fail to detach network rule. registration id = 0x%llx\n",
909 EXPORT_SYMBOL_GPL(mlx4_flow_detach);
911 int mlx4_qp_attach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
912 int block_mcast_loopback, enum mlx4_protocol prot,
913 enum mlx4_steer_type steer)
915 struct mlx4_priv *priv = mlx4_priv(dev);
916 struct mlx4_cmd_mailbox *mailbox;
917 struct mlx4_mgm *mgm;
926 mailbox = mlx4_alloc_cmd_mailbox(dev);
928 return PTR_ERR(mailbox);
931 mutex_lock(&priv->mcg_table.mutex);
932 err = find_entry(dev, port, gid, prot,
933 mailbox, &prev, &index);
938 if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
940 memcpy(mgm->gid, gid, 16);
945 index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
947 mlx4_err(dev, "No AMGM entries left\n");
951 index += dev->caps.num_mgms;
954 memset(mgm, 0, sizeof *mgm);
955 memcpy(mgm->gid, gid, 16);
958 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
959 if (members_count == dev->caps.num_qp_per_mgm) {
960 mlx4_err(dev, "MGM at index %x is full.\n", index);
965 for (i = 0; i < members_count; ++i)
966 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
967 mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
972 if (block_mcast_loopback)
973 mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
974 (1U << MGM_BLCK_LB_BIT));
976 mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
978 mgm->members_count = cpu_to_be32(members_count | (u32) prot << 30);
980 err = mlx4_WRITE_ENTRY(dev, index, mailbox);
987 err = mlx4_READ_ENTRY(dev, prev, mailbox);
991 mgm->next_gid_index = cpu_to_be32(index << 6);
993 err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
998 if (prot == MLX4_PROT_ETH) {
999 /* manage the steering entry for promisc mode */
1001 new_steering_entry(dev, port, steer, index, qp->qpn);
1003 existing_steering_entry(dev, port, steer,
1006 if (err && link && index != -1) {
1007 if (index < dev->caps.num_mgms)
1008 mlx4_warn(dev, "Got AMGM index %d < %d",
1009 index, dev->caps.num_mgms);
1011 mlx4_bitmap_free(&priv->mcg_table.bitmap,
1012 index - dev->caps.num_mgms);
1014 mutex_unlock(&priv->mcg_table.mutex);
1016 mlx4_free_cmd_mailbox(dev, mailbox);
1020 int mlx4_qp_detach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
1021 enum mlx4_protocol prot, enum mlx4_steer_type steer)
1023 struct mlx4_priv *priv = mlx4_priv(dev);
1024 struct mlx4_cmd_mailbox *mailbox;
1025 struct mlx4_mgm *mgm;
1031 bool removed_entry = false;
1033 mailbox = mlx4_alloc_cmd_mailbox(dev);
1034 if (IS_ERR(mailbox))
1035 return PTR_ERR(mailbox);
1038 mutex_lock(&priv->mcg_table.mutex);
1040 err = find_entry(dev, port, gid, prot,
1041 mailbox, &prev, &index);
1046 mlx4_err(dev, "MGID %pI6 not found\n", gid);
1051 /* if this pq is also a promisc qp, it shouldn't be removed */
1052 if (prot == MLX4_PROT_ETH &&
1053 check_duplicate_entry(dev, port, steer, index, qp->qpn))
1056 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
1057 for (loc = -1, i = 0; i < members_count; ++i)
1058 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
1062 mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
1068 mgm->members_count = cpu_to_be32(--members_count | (u32) prot << 30);
1069 mgm->qp[loc] = mgm->qp[i - 1];
1072 if (prot == MLX4_PROT_ETH)
1073 removed_entry = can_remove_steering_entry(dev, port, steer,
1075 if (i != 1 && (prot != MLX4_PROT_ETH || !removed_entry)) {
1076 err = mlx4_WRITE_ENTRY(dev, index, mailbox);
1080 /* We are going to delete the entry, members count should be 0 */
1081 mgm->members_count = cpu_to_be32((u32) prot << 30);
1084 /* Remove entry from MGM */
1085 int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
1087 err = mlx4_READ_ENTRY(dev, amgm_index, mailbox);
1091 memset(mgm->gid, 0, 16);
1093 err = mlx4_WRITE_ENTRY(dev, index, mailbox);
1098 if (amgm_index < dev->caps.num_mgms)
1099 mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
1100 index, amgm_index, dev->caps.num_mgms);
1102 mlx4_bitmap_free(&priv->mcg_table.bitmap,
1103 amgm_index - dev->caps.num_mgms);
1106 /* Remove entry from AMGM */
1107 int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
1108 err = mlx4_READ_ENTRY(dev, prev, mailbox);
1112 mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
1114 err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
1118 if (index < dev->caps.num_mgms)
1119 mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
1120 prev, index, dev->caps.num_mgms);
1122 mlx4_bitmap_free(&priv->mcg_table.bitmap,
1123 index - dev->caps.num_mgms);
1127 mutex_unlock(&priv->mcg_table.mutex);
1129 mlx4_free_cmd_mailbox(dev, mailbox);
1133 static int mlx4_QP_ATTACH(struct mlx4_dev *dev, struct mlx4_qp *qp,
1134 u8 gid[16], u8 attach, u8 block_loopback,
1135 enum mlx4_protocol prot)
1137 struct mlx4_cmd_mailbox *mailbox;
1141 if (!mlx4_is_mfunc(dev))
1144 mailbox = mlx4_alloc_cmd_mailbox(dev);
1145 if (IS_ERR(mailbox))
1146 return PTR_ERR(mailbox);
1148 memcpy(mailbox->buf, gid, 16);
1150 qpn |= (prot << 28);
1151 if (attach && block_loopback)
1154 err = mlx4_cmd(dev, mailbox->dma, qpn, attach,
1155 MLX4_CMD_QP_ATTACH, MLX4_CMD_TIME_CLASS_A,
1158 mlx4_free_cmd_mailbox(dev, mailbox);
1162 int mlx4_trans_to_dmfs_attach(struct mlx4_dev *dev, struct mlx4_qp *qp,
1163 u8 gid[16], u8 port,
1164 int block_mcast_loopback,
1165 enum mlx4_protocol prot, u64 *reg_id)
1167 struct mlx4_spec_list spec = { {NULL} };
1168 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
1170 struct mlx4_net_trans_rule rule = {
1171 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
1173 .promisc_mode = MLX4_FS_REGULAR,
1174 .priority = MLX4_DOMAIN_NIC,
1177 rule.allow_loopback = !block_mcast_loopback;
1180 INIT_LIST_HEAD(&rule.list);
1184 spec.id = MLX4_NET_TRANS_RULE_ID_ETH;
1185 memcpy(spec.eth.dst_mac, &gid[10], ETH_ALEN);
1186 memcpy(spec.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
1189 case MLX4_PROT_IB_IPV6:
1190 spec.id = MLX4_NET_TRANS_RULE_ID_IB;
1191 memcpy(spec.ib.dst_gid, gid, 16);
1192 memset(&spec.ib.dst_gid_msk, 0xff, 16);
1197 list_add_tail(&spec.list, &rule.list);
1199 return mlx4_flow_attach(dev, &rule, reg_id);
1202 int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
1203 u8 port, int block_mcast_loopback,
1204 enum mlx4_protocol prot, u64 *reg_id)
1206 switch (dev->caps.steering_mode) {
1207 case MLX4_STEERING_MODE_A0:
1208 if (prot == MLX4_PROT_ETH)
1211 case MLX4_STEERING_MODE_B0:
1212 if (prot == MLX4_PROT_ETH)
1213 gid[7] |= (MLX4_MC_STEER << 1);
1215 if (mlx4_is_mfunc(dev))
1216 return mlx4_QP_ATTACH(dev, qp, gid, 1,
1217 block_mcast_loopback, prot);
1218 return mlx4_qp_attach_common(dev, qp, gid,
1219 block_mcast_loopback, prot,
1222 case MLX4_STEERING_MODE_DEVICE_MANAGED:
1223 return mlx4_trans_to_dmfs_attach(dev, qp, gid, port,
1224 block_mcast_loopback,
1230 EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
1232 int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
1233 enum mlx4_protocol prot, u64 reg_id)
1235 switch (dev->caps.steering_mode) {
1236 case MLX4_STEERING_MODE_A0:
1237 if (prot == MLX4_PROT_ETH)
1240 case MLX4_STEERING_MODE_B0:
1241 if (prot == MLX4_PROT_ETH)
1242 gid[7] |= (MLX4_MC_STEER << 1);
1244 if (mlx4_is_mfunc(dev))
1245 return mlx4_QP_ATTACH(dev, qp, gid, 0, 0, prot);
1247 return mlx4_qp_detach_common(dev, qp, gid, prot,
1250 case MLX4_STEERING_MODE_DEVICE_MANAGED:
1251 return mlx4_flow_detach(dev, reg_id);
1257 EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
1259 int mlx4_flow_steer_promisc_add(struct mlx4_dev *dev, u8 port,
1260 u32 qpn, enum mlx4_net_trans_promisc_mode mode)
1262 struct mlx4_net_trans_rule rule;
1266 case MLX4_FS_ALL_DEFAULT:
1267 regid_p = &dev->regid_promisc_array[port];
1269 case MLX4_FS_MC_DEFAULT:
1270 regid_p = &dev->regid_allmulti_array[port];
1279 rule.promisc_mode = mode;
1282 INIT_LIST_HEAD(&rule.list);
1283 mlx4_err(dev, "going promisc on %x\n", port);
1285 return mlx4_flow_attach(dev, &rule, regid_p);
1287 EXPORT_SYMBOL_GPL(mlx4_flow_steer_promisc_add);
1289 int mlx4_flow_steer_promisc_remove(struct mlx4_dev *dev, u8 port,
1290 enum mlx4_net_trans_promisc_mode mode)
1296 case MLX4_FS_ALL_DEFAULT:
1297 regid_p = &dev->regid_promisc_array[port];
1299 case MLX4_FS_MC_DEFAULT:
1300 regid_p = &dev->regid_allmulti_array[port];
1309 ret = mlx4_flow_detach(dev, *regid_p);
1315 EXPORT_SYMBOL_GPL(mlx4_flow_steer_promisc_remove);
1317 int mlx4_unicast_attach(struct mlx4_dev *dev,
1318 struct mlx4_qp *qp, u8 gid[16],
1319 int block_mcast_loopback, enum mlx4_protocol prot)
1321 if (prot == MLX4_PROT_ETH)
1322 gid[7] |= (MLX4_UC_STEER << 1);
1324 if (mlx4_is_mfunc(dev))
1325 return mlx4_QP_ATTACH(dev, qp, gid, 1,
1326 block_mcast_loopback, prot);
1328 return mlx4_qp_attach_common(dev, qp, gid, block_mcast_loopback,
1329 prot, MLX4_UC_STEER);
1331 EXPORT_SYMBOL_GPL(mlx4_unicast_attach);
1333 int mlx4_unicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp,
1334 u8 gid[16], enum mlx4_protocol prot)
1336 if (prot == MLX4_PROT_ETH)
1337 gid[7] |= (MLX4_UC_STEER << 1);
1339 if (mlx4_is_mfunc(dev))
1340 return mlx4_QP_ATTACH(dev, qp, gid, 0, 0, prot);
1342 return mlx4_qp_detach_common(dev, qp, gid, prot, MLX4_UC_STEER);
1344 EXPORT_SYMBOL_GPL(mlx4_unicast_detach);
1346 int mlx4_PROMISC_wrapper(struct mlx4_dev *dev, int slave,
1347 struct mlx4_vhcr *vhcr,
1348 struct mlx4_cmd_mailbox *inbox,
1349 struct mlx4_cmd_mailbox *outbox,
1350 struct mlx4_cmd_info *cmd)
1352 u32 qpn = (u32) vhcr->in_param & 0xffffffff;
1353 u8 port = vhcr->in_param >> 62;
1354 enum mlx4_steer_type steer = vhcr->in_modifier;
1356 /* Promiscuous unicast is not allowed in mfunc */
1357 if (mlx4_is_mfunc(dev) && steer == MLX4_UC_STEER)
1360 if (vhcr->op_modifier)
1361 return add_promisc_qp(dev, port, steer, qpn);
1363 return remove_promisc_qp(dev, port, steer, qpn);
1366 static int mlx4_PROMISC(struct mlx4_dev *dev, u32 qpn,
1367 enum mlx4_steer_type steer, u8 add, u8 port)
1369 return mlx4_cmd(dev, (u64) qpn | (u64) port << 62, (u32) steer, add,
1370 MLX4_CMD_PROMISC, MLX4_CMD_TIME_CLASS_A,
1374 int mlx4_multicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
1376 if (mlx4_is_mfunc(dev))
1377 return mlx4_PROMISC(dev, qpn, MLX4_MC_STEER, 1, port);
1379 return add_promisc_qp(dev, port, MLX4_MC_STEER, qpn);
1381 EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_add);
1383 int mlx4_multicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
1385 if (mlx4_is_mfunc(dev))
1386 return mlx4_PROMISC(dev, qpn, MLX4_MC_STEER, 0, port);
1388 return remove_promisc_qp(dev, port, MLX4_MC_STEER, qpn);
1390 EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_remove);
1392 int mlx4_unicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
1394 if (mlx4_is_mfunc(dev))
1395 return mlx4_PROMISC(dev, qpn, MLX4_UC_STEER, 1, port);
1397 return add_promisc_qp(dev, port, MLX4_UC_STEER, qpn);
1399 EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_add);
1401 int mlx4_unicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
1403 if (mlx4_is_mfunc(dev))
1404 return mlx4_PROMISC(dev, qpn, MLX4_UC_STEER, 0, port);
1406 return remove_promisc_qp(dev, port, MLX4_UC_STEER, qpn);
1408 EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_remove);
1410 int mlx4_init_mcg_table(struct mlx4_dev *dev)
1412 struct mlx4_priv *priv = mlx4_priv(dev);
1415 /* No need for mcg_table when fw managed the mcg table*/
1416 if (dev->caps.steering_mode ==
1417 MLX4_STEERING_MODE_DEVICE_MANAGED)
1419 err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
1420 dev->caps.num_amgms - 1, 0, 0);
1424 mutex_init(&priv->mcg_table.mutex);
1429 void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
1431 if (dev->caps.steering_mode !=
1432 MLX4_STEERING_MODE_DEVICE_MANAGED)
1433 mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);