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 if (dev->caps.steering_mode ==
58 MLX4_STEERING_MODE_DEVICE_MANAGED)
59 return 1 << MLX4_FS_MGM_LOG_ENTRY_SIZE;
61 return min((1 << mlx4_log_num_mgm_entry_size),
62 MLX4_MAX_MGM_ENTRY_SIZE);
65 int mlx4_get_qp_per_mgm(struct mlx4_dev *dev)
67 return 4 * (mlx4_get_mgm_entry_size(dev) / 16 - 2);
70 static int mlx4_QP_FLOW_STEERING_ATTACH(struct mlx4_dev *dev,
71 struct mlx4_cmd_mailbox *mailbox,
78 err = mlx4_cmd_imm(dev, mailbox->dma, &imm, size, 0,
79 MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
88 static int mlx4_QP_FLOW_STEERING_DETACH(struct mlx4_dev *dev, u64 regid)
92 err = mlx4_cmd(dev, regid, 0, 0,
93 MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
99 static int mlx4_READ_ENTRY(struct mlx4_dev *dev, int index,
100 struct mlx4_cmd_mailbox *mailbox)
102 return mlx4_cmd_box(dev, 0, mailbox->dma, index, 0, MLX4_CMD_READ_MCG,
103 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
106 static int mlx4_WRITE_ENTRY(struct mlx4_dev *dev, int index,
107 struct mlx4_cmd_mailbox *mailbox)
109 return mlx4_cmd(dev, mailbox->dma, index, 0, MLX4_CMD_WRITE_MCG,
110 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
113 static int mlx4_WRITE_PROMISC(struct mlx4_dev *dev, u8 port, u8 steer,
114 struct mlx4_cmd_mailbox *mailbox)
118 in_mod = (u32) port << 16 | steer << 1;
119 return mlx4_cmd(dev, mailbox->dma, in_mod, 0x1,
120 MLX4_CMD_WRITE_MCG, MLX4_CMD_TIME_CLASS_A,
124 static int mlx4_GID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
125 u16 *hash, u8 op_mod)
130 err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, op_mod,
131 MLX4_CMD_MGID_HASH, MLX4_CMD_TIME_CLASS_A,
140 static struct mlx4_promisc_qp *get_promisc_qp(struct mlx4_dev *dev, u8 port,
141 enum mlx4_steer_type steer,
144 struct mlx4_steer *s_steer = &mlx4_priv(dev)->steer[port - 1];
145 struct mlx4_promisc_qp *pqp;
147 list_for_each_entry(pqp, &s_steer->promisc_qps[steer], list) {
156 * Add new entry to steering data structure.
157 * All promisc QPs should be added as well
159 static int new_steering_entry(struct mlx4_dev *dev, u8 port,
160 enum mlx4_steer_type steer,
161 unsigned int index, u32 qpn)
163 struct mlx4_steer *s_steer;
164 struct mlx4_cmd_mailbox *mailbox;
165 struct mlx4_mgm *mgm;
167 struct mlx4_steer_index *new_entry;
168 struct mlx4_promisc_qp *pqp;
169 struct mlx4_promisc_qp *dqp = NULL;
173 s_steer = &mlx4_priv(dev)->steer[port - 1];
174 new_entry = kzalloc(sizeof *new_entry, GFP_KERNEL);
178 INIT_LIST_HEAD(&new_entry->duplicates);
179 new_entry->index = index;
180 list_add_tail(&new_entry->list, &s_steer->steer_entries[steer]);
182 /* If the given qpn is also a promisc qp,
183 * it should be inserted to duplicates list
185 pqp = get_promisc_qp(dev, port, steer, qpn);
187 dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
193 list_add_tail(&dqp->list, &new_entry->duplicates);
196 /* if no promisc qps for this vep, we are done */
197 if (list_empty(&s_steer->promisc_qps[steer]))
200 /* now need to add all the promisc qps to the new
201 * steering entry, as they should also receive the packets
202 * destined to this address */
203 mailbox = mlx4_alloc_cmd_mailbox(dev);
204 if (IS_ERR(mailbox)) {
210 err = mlx4_READ_ENTRY(dev, index, mailbox);
214 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
215 prot = be32_to_cpu(mgm->members_count) >> 30;
216 list_for_each_entry(pqp, &s_steer->promisc_qps[steer], list) {
217 /* don't add already existing qpn */
220 if (members_count == dev->caps.num_qp_per_mgm) {
227 mgm->qp[members_count++] = cpu_to_be32(pqp->qpn & MGM_QPN_MASK);
229 /* update the qps count and update the entry with all the promisc qps*/
230 mgm->members_count = cpu_to_be32(members_count | (prot << 30));
231 err = mlx4_WRITE_ENTRY(dev, index, mailbox);
234 mlx4_free_cmd_mailbox(dev, mailbox);
239 list_del(&dqp->list);
242 list_del(&new_entry->list);
247 /* update the data structures with existing steering entry */
248 static int existing_steering_entry(struct mlx4_dev *dev, u8 port,
249 enum mlx4_steer_type steer,
250 unsigned int index, u32 qpn)
252 struct mlx4_steer *s_steer;
253 struct mlx4_steer_index *tmp_entry, *entry = NULL;
254 struct mlx4_promisc_qp *pqp;
255 struct mlx4_promisc_qp *dqp;
257 s_steer = &mlx4_priv(dev)->steer[port - 1];
259 pqp = get_promisc_qp(dev, port, steer, qpn);
261 return 0; /* nothing to do */
263 list_for_each_entry(tmp_entry, &s_steer->steer_entries[steer], list) {
264 if (tmp_entry->index == index) {
269 if (unlikely(!entry)) {
270 mlx4_warn(dev, "Steering entry at index %x is not registered\n", index);
274 /* the given qpn is listed as a promisc qpn
275 * we need to add it as a duplicate to this entry
276 * for future references */
277 list_for_each_entry(dqp, &entry->duplicates, list) {
279 return 0; /* qp is already duplicated */
282 /* add the qp as a duplicate on this index */
283 dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
287 list_add_tail(&dqp->list, &entry->duplicates);
292 /* Check whether a qpn is a duplicate on steering entry
293 * If so, it should not be removed from mgm */
294 static bool check_duplicate_entry(struct mlx4_dev *dev, u8 port,
295 enum mlx4_steer_type steer,
296 unsigned int index, u32 qpn)
298 struct mlx4_steer *s_steer;
299 struct mlx4_steer_index *tmp_entry, *entry = NULL;
300 struct mlx4_promisc_qp *dqp, *tmp_dqp;
302 s_steer = &mlx4_priv(dev)->steer[port - 1];
304 /* if qp is not promisc, it cannot be duplicated */
305 if (!get_promisc_qp(dev, port, steer, qpn))
308 /* The qp is promisc qp so it is a duplicate on this index
309 * Find the index entry, and remove the duplicate */
310 list_for_each_entry(tmp_entry, &s_steer->steer_entries[steer], list) {
311 if (tmp_entry->index == index) {
316 if (unlikely(!entry)) {
317 mlx4_warn(dev, "Steering entry for index %x is not registered\n", index);
320 list_for_each_entry_safe(dqp, tmp_dqp, &entry->duplicates, list) {
321 if (dqp->qpn == qpn) {
322 list_del(&dqp->list);
329 /* I a steering entry contains only promisc QPs, it can be removed. */
330 static bool can_remove_steering_entry(struct mlx4_dev *dev, u8 port,
331 enum mlx4_steer_type steer,
332 unsigned int index, u32 tqpn)
334 struct mlx4_steer *s_steer;
335 struct mlx4_cmd_mailbox *mailbox;
336 struct mlx4_mgm *mgm;
337 struct mlx4_steer_index *entry = NULL, *tmp_entry;
343 s_steer = &mlx4_priv(dev)->steer[port - 1];
345 mailbox = mlx4_alloc_cmd_mailbox(dev);
350 if (mlx4_READ_ENTRY(dev, index, mailbox))
352 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
353 for (i = 0; i < members_count; i++) {
354 qpn = be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK;
355 if (!get_promisc_qp(dev, port, steer, qpn) && qpn != tqpn) {
356 /* the qp is not promisc, the entry can't be removed */
360 /* All the qps currently registered for this entry are promiscuous,
361 * Checking for duplicates */
363 list_for_each_entry_safe(entry, tmp_entry, &s_steer->steer_entries[steer], list) {
364 if (entry->index == index) {
365 if (list_empty(&entry->duplicates)) {
366 list_del(&entry->list);
369 /* This entry contains duplicates so it shouldn't be removed */
377 mlx4_free_cmd_mailbox(dev, mailbox);
381 static int add_promisc_qp(struct mlx4_dev *dev, u8 port,
382 enum mlx4_steer_type steer, u32 qpn)
384 struct mlx4_steer *s_steer;
385 struct mlx4_cmd_mailbox *mailbox;
386 struct mlx4_mgm *mgm;
387 struct mlx4_steer_index *entry;
388 struct mlx4_promisc_qp *pqp;
389 struct mlx4_promisc_qp *dqp;
395 struct mlx4_priv *priv = mlx4_priv(dev);
397 s_steer = &mlx4_priv(dev)->steer[port - 1];
399 mutex_lock(&priv->mcg_table.mutex);
401 if (get_promisc_qp(dev, port, steer, qpn)) {
402 err = 0; /* Noting to do, already exists */
406 pqp = kmalloc(sizeof *pqp, GFP_KERNEL);
413 mailbox = mlx4_alloc_cmd_mailbox(dev);
414 if (IS_ERR(mailbox)) {
420 /* the promisc qp needs to be added for each one of the steering
421 * entries, if it already exists, needs to be added as a duplicate
423 list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
424 err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
428 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
429 prot = be32_to_cpu(mgm->members_count) >> 30;
431 for (i = 0; i < members_count; i++) {
432 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn) {
433 /* Entry already exists, add to duplicates */
434 dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
440 list_add_tail(&dqp->list, &entry->duplicates);
445 /* Need to add the qpn to mgm */
446 if (members_count == dev->caps.num_qp_per_mgm) {
451 mgm->qp[members_count++] = cpu_to_be32(qpn & MGM_QPN_MASK);
452 mgm->members_count = cpu_to_be32(members_count | (prot << 30));
453 err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
459 /* add the new qpn to list of promisc qps */
460 list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
461 /* now need to add all the promisc qps to default entry */
462 memset(mgm, 0, sizeof *mgm);
464 list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
465 mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
466 mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
468 err = mlx4_WRITE_PROMISC(dev, port, steer, mailbox);
472 mlx4_free_cmd_mailbox(dev, mailbox);
473 mutex_unlock(&priv->mcg_table.mutex);
477 list_del(&pqp->list);
479 mlx4_free_cmd_mailbox(dev, mailbox);
483 mutex_unlock(&priv->mcg_table.mutex);
487 static int remove_promisc_qp(struct mlx4_dev *dev, u8 port,
488 enum mlx4_steer_type steer, u32 qpn)
490 struct mlx4_priv *priv = mlx4_priv(dev);
491 struct mlx4_steer *s_steer;
492 struct mlx4_cmd_mailbox *mailbox;
493 struct mlx4_mgm *mgm;
494 struct mlx4_steer_index *entry;
495 struct mlx4_promisc_qp *pqp;
496 struct mlx4_promisc_qp *dqp;
499 bool back_to_list = false;
503 s_steer = &mlx4_priv(dev)->steer[port - 1];
504 mutex_lock(&priv->mcg_table.mutex);
506 pqp = get_promisc_qp(dev, port, steer, qpn);
507 if (unlikely(!pqp)) {
508 mlx4_warn(dev, "QP %x is not promiscuous QP\n", qpn);
514 /*remove from list of promisc qps */
515 list_del(&pqp->list);
517 /* set the default entry not to include the removed one */
518 mailbox = mlx4_alloc_cmd_mailbox(dev);
519 if (IS_ERR(mailbox)) {
525 memset(mgm, 0, sizeof *mgm);
527 list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
528 mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
529 mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
531 err = mlx4_WRITE_PROMISC(dev, port, steer, mailbox);
535 /* remove the qp from all the steering entries*/
536 list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
538 list_for_each_entry(dqp, &entry->duplicates, list) {
539 if (dqp->qpn == qpn) {
545 /* a duplicate, no need to change the mgm,
546 * only update the duplicates list */
547 list_del(&dqp->list);
550 err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
553 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
554 for (loc = -1, i = 0; i < members_count; ++i)
555 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn)
558 mgm->members_count = cpu_to_be32(--members_count |
559 (MLX4_PROT_ETH << 30));
560 mgm->qp[loc] = mgm->qp[i - 1];
563 err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
571 mlx4_free_cmd_mailbox(dev, mailbox);
574 list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
578 mutex_unlock(&priv->mcg_table.mutex);
583 * Caller must hold MCG table semaphore. gid and mgm parameters must
584 * be properly aligned for command interface.
586 * Returns 0 unless a firmware command error occurs.
588 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
589 * and *mgm holds MGM entry.
591 * if GID is found in AMGM, *index = index in AMGM, *prev = index of
592 * previous entry in hash chain and *mgm holds AMGM entry.
594 * If no AMGM exists for given gid, *index = -1, *prev = index of last
595 * entry in hash chain and *mgm holds end of hash chain.
597 static int find_entry(struct mlx4_dev *dev, u8 port,
598 u8 *gid, enum mlx4_protocol prot,
599 struct mlx4_cmd_mailbox *mgm_mailbox,
600 int *prev, int *index)
602 struct mlx4_cmd_mailbox *mailbox;
603 struct mlx4_mgm *mgm = mgm_mailbox->buf;
607 u8 op_mod = (prot == MLX4_PROT_ETH) ?
608 !!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) : 0;
610 mailbox = mlx4_alloc_cmd_mailbox(dev);
615 memcpy(mgid, gid, 16);
617 err = mlx4_GID_HASH(dev, mailbox, &hash, op_mod);
618 mlx4_free_cmd_mailbox(dev, mailbox);
623 mlx4_dbg(dev, "Hash for %pI6 is %04x\n", gid, hash);
629 err = mlx4_READ_ENTRY(dev, *index, mgm_mailbox);
633 if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
634 if (*index != hash) {
635 mlx4_err(dev, "Found zero MGID in AMGM.\n");
641 if (!memcmp(mgm->gid, gid, 16) &&
642 be32_to_cpu(mgm->members_count) >> 30 == prot)
646 *index = be32_to_cpu(mgm->next_gid_index) >> 6;
653 static void trans_rule_ctrl_to_hw(struct mlx4_net_trans_rule *ctrl,
654 struct mlx4_net_trans_rule_hw_ctrl *hw)
656 static const u8 __promisc_mode[] = {
657 [MLX4_FS_PROMISC_NONE] = 0x0,
658 [MLX4_FS_PROMISC_UPLINK] = 0x1,
659 [MLX4_FS_PROMISC_FUNCTION_PORT] = 0x2,
660 [MLX4_FS_PROMISC_ALL_MULTI] = 0x3,
665 dw = ctrl->queue_mode == MLX4_NET_TRANS_Q_LIFO ? 1 : 0;
666 dw |= ctrl->exclusive ? (1 << 2) : 0;
667 dw |= ctrl->allow_loopback ? (1 << 3) : 0;
668 dw |= __promisc_mode[ctrl->promisc_mode] << 8;
669 dw |= ctrl->priority << 16;
671 hw->ctrl = cpu_to_be32(dw);
672 hw->vf_vep_port = cpu_to_be32(ctrl->port);
673 hw->qpn = cpu_to_be32(ctrl->qpn);
676 const u16 __sw_id_hw[] = {
677 [MLX4_NET_TRANS_RULE_ID_ETH] = 0xE001,
678 [MLX4_NET_TRANS_RULE_ID_IB] = 0xE005,
679 [MLX4_NET_TRANS_RULE_ID_IPV6] = 0xE003,
680 [MLX4_NET_TRANS_RULE_ID_IPV4] = 0xE002,
681 [MLX4_NET_TRANS_RULE_ID_TCP] = 0xE004,
682 [MLX4_NET_TRANS_RULE_ID_UDP] = 0xE006
685 static int parse_trans_rule(struct mlx4_dev *dev, struct mlx4_spec_list *spec,
686 struct _rule_hw *rule_hw)
688 static const size_t __rule_hw_sz[] = {
689 [MLX4_NET_TRANS_RULE_ID_ETH] =
690 sizeof(struct mlx4_net_trans_rule_hw_eth),
691 [MLX4_NET_TRANS_RULE_ID_IB] =
692 sizeof(struct mlx4_net_trans_rule_hw_ib),
693 [MLX4_NET_TRANS_RULE_ID_IPV6] = 0,
694 [MLX4_NET_TRANS_RULE_ID_IPV4] =
695 sizeof(struct mlx4_net_trans_rule_hw_ipv4),
696 [MLX4_NET_TRANS_RULE_ID_TCP] =
697 sizeof(struct mlx4_net_trans_rule_hw_tcp_udp),
698 [MLX4_NET_TRANS_RULE_ID_UDP] =
699 sizeof(struct mlx4_net_trans_rule_hw_tcp_udp)
701 if (spec->id >= MLX4_NET_TRANS_RULE_NUM) {
702 mlx4_err(dev, "Invalid network rule id. id = %d\n", spec->id);
705 memset(rule_hw, 0, __rule_hw_sz[spec->id]);
706 rule_hw->id = cpu_to_be16(__sw_id_hw[spec->id]);
707 rule_hw->size = __rule_hw_sz[spec->id] >> 2;
710 case MLX4_NET_TRANS_RULE_ID_ETH:
711 memcpy(rule_hw->eth.dst_mac, spec->eth.dst_mac, ETH_ALEN);
712 memcpy(rule_hw->eth.dst_mac_msk, spec->eth.dst_mac_msk,
714 memcpy(rule_hw->eth.src_mac, spec->eth.src_mac, ETH_ALEN);
715 memcpy(rule_hw->eth.src_mac_msk, spec->eth.src_mac_msk,
717 if (spec->eth.ether_type_enable) {
718 rule_hw->eth.ether_type_enable = 1;
719 rule_hw->eth.ether_type = spec->eth.ether_type;
721 rule_hw->eth.vlan_id = spec->eth.vlan_id;
722 rule_hw->eth.vlan_id_msk = spec->eth.vlan_id_msk;
725 case MLX4_NET_TRANS_RULE_ID_IB:
726 rule_hw->ib.qpn = spec->ib.r_qpn;
727 rule_hw->ib.qpn_mask = spec->ib.qpn_msk;
728 memcpy(&rule_hw->ib.dst_gid, &spec->ib.dst_gid, 16);
729 memcpy(&rule_hw->ib.dst_gid_msk, &spec->ib.dst_gid_msk, 16);
732 case MLX4_NET_TRANS_RULE_ID_IPV6:
735 case MLX4_NET_TRANS_RULE_ID_IPV4:
736 rule_hw->ipv4.src_ip = spec->ipv4.src_ip;
737 rule_hw->ipv4.src_ip_msk = spec->ipv4.src_ip_msk;
738 rule_hw->ipv4.dst_ip = spec->ipv4.dst_ip;
739 rule_hw->ipv4.dst_ip_msk = spec->ipv4.dst_ip_msk;
742 case MLX4_NET_TRANS_RULE_ID_TCP:
743 case MLX4_NET_TRANS_RULE_ID_UDP:
744 rule_hw->tcp_udp.dst_port = spec->tcp_udp.dst_port;
745 rule_hw->tcp_udp.dst_port_msk = spec->tcp_udp.dst_port_msk;
746 rule_hw->tcp_udp.src_port = spec->tcp_udp.src_port;
747 rule_hw->tcp_udp.src_port_msk = spec->tcp_udp.src_port_msk;
754 return __rule_hw_sz[spec->id];
757 static void mlx4_err_rule(struct mlx4_dev *dev, char *str,
758 struct mlx4_net_trans_rule *rule)
761 struct mlx4_spec_list *cur;
765 mlx4_err(dev, "%s", str);
766 len += snprintf(buf + len, BUF_SIZE - len,
767 "port = %d prio = 0x%x qp = 0x%x ",
768 rule->port, rule->priority, rule->qpn);
770 list_for_each_entry(cur, &rule->list, list) {
772 case MLX4_NET_TRANS_RULE_ID_ETH:
773 len += snprintf(buf + len, BUF_SIZE - len,
774 "dmac = %pM ", &cur->eth.dst_mac);
775 if (cur->eth.ether_type)
776 len += snprintf(buf + len, BUF_SIZE - len,
778 be16_to_cpu(cur->eth.ether_type));
779 if (cur->eth.vlan_id)
780 len += snprintf(buf + len, BUF_SIZE - len,
782 be16_to_cpu(cur->eth.vlan_id));
785 case MLX4_NET_TRANS_RULE_ID_IPV4:
786 if (cur->ipv4.src_ip)
787 len += snprintf(buf + len, BUF_SIZE - len,
790 if (cur->ipv4.dst_ip)
791 len += snprintf(buf + len, BUF_SIZE - len,
796 case MLX4_NET_TRANS_RULE_ID_TCP:
797 case MLX4_NET_TRANS_RULE_ID_UDP:
798 if (cur->tcp_udp.src_port)
799 len += snprintf(buf + len, BUF_SIZE - len,
801 be16_to_cpu(cur->tcp_udp.src_port));
802 if (cur->tcp_udp.dst_port)
803 len += snprintf(buf + len, BUF_SIZE - len,
805 be16_to_cpu(cur->tcp_udp.dst_port));
808 case MLX4_NET_TRANS_RULE_ID_IB:
809 len += snprintf(buf + len, BUF_SIZE - len,
810 "dst-gid = %pI6\n", cur->ib.dst_gid);
811 len += snprintf(buf + len, BUF_SIZE - len,
812 "dst-gid-mask = %pI6\n",
813 cur->ib.dst_gid_msk);
816 case MLX4_NET_TRANS_RULE_ID_IPV6:
823 len += snprintf(buf + len, BUF_SIZE - len, "\n");
824 mlx4_err(dev, "%s", buf);
827 mlx4_err(dev, "Network rule error message was truncated, print buffer is too small.\n");
830 int mlx4_flow_attach(struct mlx4_dev *dev,
831 struct mlx4_net_trans_rule *rule, u64 *reg_id)
833 struct mlx4_cmd_mailbox *mailbox;
834 struct mlx4_spec_list *cur;
838 mailbox = mlx4_alloc_cmd_mailbox(dev);
840 return PTR_ERR(mailbox);
842 memset(mailbox->buf, 0, sizeof(struct mlx4_net_trans_rule_hw_ctrl));
843 trans_rule_ctrl_to_hw(rule, mailbox->buf);
845 size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
847 list_for_each_entry(cur, &rule->list, list) {
848 ret = parse_trans_rule(dev, cur, mailbox->buf + size);
850 mlx4_free_cmd_mailbox(dev, mailbox);
856 ret = mlx4_QP_FLOW_STEERING_ATTACH(dev, mailbox, size >> 2, reg_id);
859 "mcg table is full. Fail to register network rule.\n",
862 mlx4_err_rule(dev, "Fail to register network rule.\n", rule);
864 mlx4_free_cmd_mailbox(dev, mailbox);
868 EXPORT_SYMBOL_GPL(mlx4_flow_attach);
870 int mlx4_flow_detach(struct mlx4_dev *dev, u64 reg_id)
874 err = mlx4_QP_FLOW_STEERING_DETACH(dev, reg_id);
876 mlx4_err(dev, "Fail to detach network rule. registration id = 0x%llx\n",
880 EXPORT_SYMBOL_GPL(mlx4_flow_detach);
882 int mlx4_qp_attach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
883 int block_mcast_loopback, enum mlx4_protocol prot,
884 enum mlx4_steer_type steer)
886 struct mlx4_priv *priv = mlx4_priv(dev);
887 struct mlx4_cmd_mailbox *mailbox;
888 struct mlx4_mgm *mgm;
897 mailbox = mlx4_alloc_cmd_mailbox(dev);
899 return PTR_ERR(mailbox);
902 mutex_lock(&priv->mcg_table.mutex);
903 err = find_entry(dev, port, gid, prot,
904 mailbox, &prev, &index);
909 if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
911 memcpy(mgm->gid, gid, 16);
916 index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
918 mlx4_err(dev, "No AMGM entries left\n");
922 index += dev->caps.num_mgms;
925 memset(mgm, 0, sizeof *mgm);
926 memcpy(mgm->gid, gid, 16);
929 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
930 if (members_count == dev->caps.num_qp_per_mgm) {
931 mlx4_err(dev, "MGM at index %x is full.\n", index);
936 for (i = 0; i < members_count; ++i)
937 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
938 mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
943 if (block_mcast_loopback)
944 mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
945 (1U << MGM_BLCK_LB_BIT));
947 mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
949 mgm->members_count = cpu_to_be32(members_count | (u32) prot << 30);
951 err = mlx4_WRITE_ENTRY(dev, index, mailbox);
958 err = mlx4_READ_ENTRY(dev, prev, mailbox);
962 mgm->next_gid_index = cpu_to_be32(index << 6);
964 err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
969 if (prot == MLX4_PROT_ETH) {
970 /* manage the steering entry for promisc mode */
972 new_steering_entry(dev, port, steer, index, qp->qpn);
974 existing_steering_entry(dev, port, steer,
977 if (err && link && index != -1) {
978 if (index < dev->caps.num_mgms)
979 mlx4_warn(dev, "Got AMGM index %d < %d",
980 index, dev->caps.num_mgms);
982 mlx4_bitmap_free(&priv->mcg_table.bitmap,
983 index - dev->caps.num_mgms);
985 mutex_unlock(&priv->mcg_table.mutex);
987 mlx4_free_cmd_mailbox(dev, mailbox);
991 int mlx4_qp_detach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
992 enum mlx4_protocol prot, enum mlx4_steer_type steer)
994 struct mlx4_priv *priv = mlx4_priv(dev);
995 struct mlx4_cmd_mailbox *mailbox;
996 struct mlx4_mgm *mgm;
1002 bool removed_entry = false;
1004 mailbox = mlx4_alloc_cmd_mailbox(dev);
1005 if (IS_ERR(mailbox))
1006 return PTR_ERR(mailbox);
1009 mutex_lock(&priv->mcg_table.mutex);
1011 err = find_entry(dev, port, gid, prot,
1012 mailbox, &prev, &index);
1017 mlx4_err(dev, "MGID %pI6 not found\n", gid);
1022 /* if this pq is also a promisc qp, it shouldn't be removed */
1023 if (prot == MLX4_PROT_ETH &&
1024 check_duplicate_entry(dev, port, steer, index, qp->qpn))
1027 members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
1028 for (loc = -1, i = 0; i < members_count; ++i)
1029 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
1033 mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
1039 mgm->members_count = cpu_to_be32(--members_count | (u32) prot << 30);
1040 mgm->qp[loc] = mgm->qp[i - 1];
1043 if (prot == MLX4_PROT_ETH)
1044 removed_entry = can_remove_steering_entry(dev, port, steer,
1046 if (i != 1 && (prot != MLX4_PROT_ETH || !removed_entry)) {
1047 err = mlx4_WRITE_ENTRY(dev, index, mailbox);
1051 /* We are going to delete the entry, members count should be 0 */
1052 mgm->members_count = cpu_to_be32((u32) prot << 30);
1055 /* Remove entry from MGM */
1056 int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
1058 err = mlx4_READ_ENTRY(dev, amgm_index, mailbox);
1062 memset(mgm->gid, 0, 16);
1064 err = mlx4_WRITE_ENTRY(dev, index, mailbox);
1069 if (amgm_index < dev->caps.num_mgms)
1070 mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
1071 index, amgm_index, dev->caps.num_mgms);
1073 mlx4_bitmap_free(&priv->mcg_table.bitmap,
1074 amgm_index - dev->caps.num_mgms);
1077 /* Remove entry from AMGM */
1078 int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
1079 err = mlx4_READ_ENTRY(dev, prev, mailbox);
1083 mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
1085 err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
1089 if (index < dev->caps.num_mgms)
1090 mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
1091 prev, index, dev->caps.num_mgms);
1093 mlx4_bitmap_free(&priv->mcg_table.bitmap,
1094 index - dev->caps.num_mgms);
1098 mutex_unlock(&priv->mcg_table.mutex);
1100 mlx4_free_cmd_mailbox(dev, mailbox);
1104 static int mlx4_QP_ATTACH(struct mlx4_dev *dev, struct mlx4_qp *qp,
1105 u8 gid[16], u8 attach, u8 block_loopback,
1106 enum mlx4_protocol prot)
1108 struct mlx4_cmd_mailbox *mailbox;
1112 if (!mlx4_is_mfunc(dev))
1115 mailbox = mlx4_alloc_cmd_mailbox(dev);
1116 if (IS_ERR(mailbox))
1117 return PTR_ERR(mailbox);
1119 memcpy(mailbox->buf, gid, 16);
1121 qpn |= (prot << 28);
1122 if (attach && block_loopback)
1125 err = mlx4_cmd(dev, mailbox->dma, qpn, attach,
1126 MLX4_CMD_QP_ATTACH, MLX4_CMD_TIME_CLASS_A,
1129 mlx4_free_cmd_mailbox(dev, mailbox);
1133 int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
1134 u8 port, int block_mcast_loopback,
1135 enum mlx4_protocol prot, u64 *reg_id)
1138 switch (dev->caps.steering_mode) {
1139 case MLX4_STEERING_MODE_A0:
1140 if (prot == MLX4_PROT_ETH)
1143 case MLX4_STEERING_MODE_B0:
1144 if (prot == MLX4_PROT_ETH)
1145 gid[7] |= (MLX4_MC_STEER << 1);
1147 if (mlx4_is_mfunc(dev))
1148 return mlx4_QP_ATTACH(dev, qp, gid, 1,
1149 block_mcast_loopback, prot);
1150 return mlx4_qp_attach_common(dev, qp, gid,
1151 block_mcast_loopback, prot,
1154 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
1155 struct mlx4_spec_list spec = { {NULL} };
1156 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
1158 struct mlx4_net_trans_rule rule = {
1159 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
1161 .promisc_mode = MLX4_FS_PROMISC_NONE,
1162 .priority = MLX4_DOMAIN_NIC,
1165 rule.allow_loopback = ~block_mcast_loopback;
1168 INIT_LIST_HEAD(&rule.list);
1172 spec.id = MLX4_NET_TRANS_RULE_ID_ETH;
1173 memcpy(spec.eth.dst_mac, &gid[10], ETH_ALEN);
1174 memcpy(spec.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
1177 case MLX4_PROT_IB_IPV6:
1178 spec.id = MLX4_NET_TRANS_RULE_ID_IB;
1179 memcpy(spec.ib.dst_gid, gid, 16);
1180 memset(&spec.ib.dst_gid_msk, 0xff, 16);
1185 list_add_tail(&spec.list, &rule.list);
1187 return mlx4_flow_attach(dev, &rule, reg_id);
1194 EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
1196 int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
1197 enum mlx4_protocol prot, u64 reg_id)
1199 switch (dev->caps.steering_mode) {
1200 case MLX4_STEERING_MODE_A0:
1201 if (prot == MLX4_PROT_ETH)
1204 case MLX4_STEERING_MODE_B0:
1205 if (prot == MLX4_PROT_ETH)
1206 gid[7] |= (MLX4_MC_STEER << 1);
1208 if (mlx4_is_mfunc(dev))
1209 return mlx4_QP_ATTACH(dev, qp, gid, 0, 0, prot);
1211 return mlx4_qp_detach_common(dev, qp, gid, prot,
1214 case MLX4_STEERING_MODE_DEVICE_MANAGED:
1215 return mlx4_flow_detach(dev, reg_id);
1221 EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
1223 int mlx4_flow_steer_promisc_add(struct mlx4_dev *dev, u8 port,
1224 u32 qpn, enum mlx4_net_trans_promisc_mode mode)
1226 struct mlx4_net_trans_rule rule;
1230 case MLX4_FS_PROMISC_UPLINK:
1231 case MLX4_FS_PROMISC_FUNCTION_PORT:
1232 regid_p = &dev->regid_promisc_array[port];
1234 case MLX4_FS_PROMISC_ALL_MULTI:
1235 regid_p = &dev->regid_allmulti_array[port];
1244 rule.promisc_mode = mode;
1247 INIT_LIST_HEAD(&rule.list);
1248 mlx4_err(dev, "going promisc on %x\n", port);
1250 return mlx4_flow_attach(dev, &rule, regid_p);
1252 EXPORT_SYMBOL_GPL(mlx4_flow_steer_promisc_add);
1254 int mlx4_flow_steer_promisc_remove(struct mlx4_dev *dev, u8 port,
1255 enum mlx4_net_trans_promisc_mode mode)
1261 case MLX4_FS_PROMISC_UPLINK:
1262 case MLX4_FS_PROMISC_FUNCTION_PORT:
1263 regid_p = &dev->regid_promisc_array[port];
1265 case MLX4_FS_PROMISC_ALL_MULTI:
1266 regid_p = &dev->regid_allmulti_array[port];
1275 ret = mlx4_flow_detach(dev, *regid_p);
1281 EXPORT_SYMBOL_GPL(mlx4_flow_steer_promisc_remove);
1283 int mlx4_unicast_attach(struct mlx4_dev *dev,
1284 struct mlx4_qp *qp, u8 gid[16],
1285 int block_mcast_loopback, enum mlx4_protocol prot)
1287 if (prot == MLX4_PROT_ETH)
1288 gid[7] |= (MLX4_UC_STEER << 1);
1290 if (mlx4_is_mfunc(dev))
1291 return mlx4_QP_ATTACH(dev, qp, gid, 1,
1292 block_mcast_loopback, prot);
1294 return mlx4_qp_attach_common(dev, qp, gid, block_mcast_loopback,
1295 prot, MLX4_UC_STEER);
1297 EXPORT_SYMBOL_GPL(mlx4_unicast_attach);
1299 int mlx4_unicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp,
1300 u8 gid[16], enum mlx4_protocol prot)
1302 if (prot == MLX4_PROT_ETH)
1303 gid[7] |= (MLX4_UC_STEER << 1);
1305 if (mlx4_is_mfunc(dev))
1306 return mlx4_QP_ATTACH(dev, qp, gid, 0, 0, prot);
1308 return mlx4_qp_detach_common(dev, qp, gid, prot, MLX4_UC_STEER);
1310 EXPORT_SYMBOL_GPL(mlx4_unicast_detach);
1312 int mlx4_PROMISC_wrapper(struct mlx4_dev *dev, int slave,
1313 struct mlx4_vhcr *vhcr,
1314 struct mlx4_cmd_mailbox *inbox,
1315 struct mlx4_cmd_mailbox *outbox,
1316 struct mlx4_cmd_info *cmd)
1318 u32 qpn = (u32) vhcr->in_param & 0xffffffff;
1319 u8 port = vhcr->in_param >> 62;
1320 enum mlx4_steer_type steer = vhcr->in_modifier;
1322 /* Promiscuous unicast is not allowed in mfunc */
1323 if (mlx4_is_mfunc(dev) && steer == MLX4_UC_STEER)
1326 if (vhcr->op_modifier)
1327 return add_promisc_qp(dev, port, steer, qpn);
1329 return remove_promisc_qp(dev, port, steer, qpn);
1332 static int mlx4_PROMISC(struct mlx4_dev *dev, u32 qpn,
1333 enum mlx4_steer_type steer, u8 add, u8 port)
1335 return mlx4_cmd(dev, (u64) qpn | (u64) port << 62, (u32) steer, add,
1336 MLX4_CMD_PROMISC, MLX4_CMD_TIME_CLASS_A,
1340 int mlx4_multicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
1342 if (mlx4_is_mfunc(dev))
1343 return mlx4_PROMISC(dev, qpn, MLX4_MC_STEER, 1, port);
1345 return add_promisc_qp(dev, port, MLX4_MC_STEER, qpn);
1347 EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_add);
1349 int mlx4_multicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
1351 if (mlx4_is_mfunc(dev))
1352 return mlx4_PROMISC(dev, qpn, MLX4_MC_STEER, 0, port);
1354 return remove_promisc_qp(dev, port, MLX4_MC_STEER, qpn);
1356 EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_remove);
1358 int mlx4_unicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
1360 if (mlx4_is_mfunc(dev))
1361 return mlx4_PROMISC(dev, qpn, MLX4_UC_STEER, 1, port);
1363 return add_promisc_qp(dev, port, MLX4_UC_STEER, qpn);
1365 EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_add);
1367 int mlx4_unicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
1369 if (mlx4_is_mfunc(dev))
1370 return mlx4_PROMISC(dev, qpn, MLX4_UC_STEER, 0, port);
1372 return remove_promisc_qp(dev, port, MLX4_UC_STEER, qpn);
1374 EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_remove);
1376 int mlx4_init_mcg_table(struct mlx4_dev *dev)
1378 struct mlx4_priv *priv = mlx4_priv(dev);
1381 /* No need for mcg_table when fw managed the mcg table*/
1382 if (dev->caps.steering_mode ==
1383 MLX4_STEERING_MODE_DEVICE_MANAGED)
1385 err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
1386 dev->caps.num_amgms - 1, 0, 0);
1390 mutex_init(&priv->mcg_table.mutex);
1395 void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
1397 if (dev->caps.steering_mode !=
1398 MLX4_STEERING_MODE_DEVICE_MANAGED)
1399 mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);