IB/mlx4: Create paravirt contexts for VFs when master IB driver initializes
[cascardo/linux.git] / drivers / infiniband / hw / mlx4 / mad.c
index 2f13894..21a7941 100644 (file)
@@ -35,6 +35,7 @@
 #include <rdma/ib_sa.h>
 #include <rdma/ib_cache.h>
 
+#include <linux/random.h>
 #include <linux/mlx4/cmd.h>
 #include <linux/gfp.h>
 #include <rdma/ib_pma.h>
@@ -54,6 +55,15 @@ enum {
 #define MLX4_TUN_IS_RECV(a)  (((a) >>  MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
 #define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
 
+ /* Port mgmt change event handling */
+
+#define GET_BLK_PTR_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.block_ptr)
+#define GET_MASK_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.tbl_entries_mask)
+#define NUM_IDX_IN_PKEY_TBL_BLK 32
+#define GUID_TBL_ENTRY_SIZE 8     /* size in bytes */
+#define GUID_TBL_BLK_NUM_ENTRIES 8
+#define GUID_TBL_BLK_SIZE (GUID_TBL_ENTRY_SIZE * GUID_TBL_BLK_NUM_ENTRIES)
+
 struct mlx4_mad_rcv_buf {
        struct ib_grh grh;
        u8 payload[256];
@@ -76,6 +86,15 @@ struct mlx4_rcv_tunnel_mad {
 } __packed;
 
 static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num);
+static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num);
+static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
+                               int block, u32 change_bitmap);
+
+__be64 mlx4_ib_gen_node_guid(void)
+{
+#define NODE_GUID_HI   ((u64) (((u64)IB_OPENIB_OUI) << 40))
+       return cpu_to_be64(NODE_GUID_HI | random32());
+}
 
 __be64 mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx *ctx)
 {
@@ -220,8 +239,7 @@ static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
                                handle_client_rereg_event(dev, port_num);
 
                        if (prev_lid != lid)
-                               mlx4_ib_dispatch_event(dev, port_num,
-                                                      IB_EVENT_LID_CHANGE);
+                               handle_lid_change_event(dev, port_num);
                        break;
 
                case IB_SMP_ATTR_PKEY_TABLE:
@@ -231,6 +249,9 @@ static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
                                break;
                        }
 
+                       /* at this point, we are running in the master.
+                        * Slaves do not receive SMPs.
+                        */
                        bn  = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
                        base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
                        pkey_change_bitmap = 0;
@@ -248,10 +269,13 @@ static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
                                 "block=0x%x, change_bitmap=0x%x\n",
                                 port_num, bn, pkey_change_bitmap);
 
-                       if (pkey_change_bitmap)
+                       if (pkey_change_bitmap) {
                                mlx4_ib_dispatch_event(dev, port_num,
                                                       IB_EVENT_PKEY_CHANGE);
-
+                               if (!dev->sriov.is_going_down)
+                                       __propagate_pkey_ev(dev, port_num, bn,
+                                                           pkey_change_bitmap);
+                       }
                        break;
 
                case IB_SMP_ATTR_GUID_INFO:
@@ -259,12 +283,56 @@ static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
                        if (!mlx4_is_master(dev->dev))
                                mlx4_ib_dispatch_event(dev, port_num,
                                                       IB_EVENT_GID_CHANGE);
+                       /*if master, notify relevant slaves*/
+                       if (mlx4_is_master(dev->dev) &&
+                           !dev->sriov.is_going_down) {
+                               bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod);
+                               mlx4_ib_update_cache_on_guid_change(dev, bn, port_num,
+                                                                   (u8 *)(&((struct ib_smp *)mad)->data));
+                               mlx4_ib_notify_slaves_on_guid_change(dev, bn, port_num,
+                                                                    (u8 *)(&((struct ib_smp *)mad)->data));
+                       }
                        break;
+
                default:
                        break;
                }
 }
 
+static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
+                               int block, u32 change_bitmap)
+{
+       int i, ix, slave, err;
+       int have_event = 0;
+
+       for (slave = 0; slave < dev->dev->caps.sqp_demux; slave++) {
+               if (slave == mlx4_master_func_num(dev->dev))
+                       continue;
+               if (!mlx4_is_slave_active(dev->dev, slave))
+                       continue;
+
+               have_event = 0;
+               for (i = 0; i < 32; i++) {
+                       if (!(change_bitmap & (1 << i)))
+                               continue;
+                       for (ix = 0;
+                            ix < dev->dev->caps.pkey_table_len[port_num]; ix++) {
+                               if (dev->pkeys.virt2phys_pkey[slave][port_num - 1]
+                                   [ix] == i + 32 * block) {
+                                       err = mlx4_gen_pkey_eqe(dev->dev, slave, port_num);
+                                       pr_debug("propagate_pkey_ev: slave %d,"
+                                                " port %d, ix %d (%d)\n",
+                                                slave, port_num, ix, err);
+                                       have_event = 1;
+                                       break;
+                               }
+                       }
+                       if (have_event)
+                               break;
+               }
+       }
+}
+
 static void node_desc_override(struct ib_device *dev,
                               struct ib_mad *mad)
 {
@@ -437,7 +505,7 @@ int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u8 port,
        } else
                tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
 
-       dqpn = dev->dev->caps.sqp_start + 8 * slave + port + (dest_qpt * 2) - 1;
+       dqpn = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave + port + (dest_qpt * 2) - 1;
 
        /* get tunnel tx data buf for slave */
        src_qp = tun_qp->qp;
@@ -648,7 +716,9 @@ static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
        if (!out_mad->mad_hdr.status) {
                if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
                        smp_snoop(ibdev, port_num, in_mad, prev_lid);
-               node_desc_override(ibdev, out_mad);
+               /* slaves get node desc from FW */
+               if (!mlx4_is_slave(to_mdev(ibdev)->dev))
+                       node_desc_override(ibdev, out_mad);
        }
 
        /* set return bit in status of directed route responses */
@@ -731,6 +801,8 @@ int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
 static void send_handler(struct ib_mad_agent *agent,
                         struct ib_mad_send_wc *mad_send_wc)
 {
+       if (mad_send_wc->send_buf->context[0])
+               ib_destroy_ah(mad_send_wc->send_buf->context[0]);
        ib_free_send_mad(mad_send_wc->send_buf);
 }
 
@@ -789,16 +861,90 @@ void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
        }
 }
 
+static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num)
+{
+       mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_LID_CHANGE);
+
+       if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
+               mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
+                                           MLX4_EQ_PORT_INFO_LID_CHANGE_MASK);
+}
+
 static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num)
 {
-       /* re-configure the mcg's */
+       /* re-configure the alias-guid and mcg's */
        if (mlx4_is_master(dev->dev)) {
-               if (!dev->sriov.is_going_down)
+               mlx4_ib_invalidate_all_guid_record(dev, port_num);
+
+               if (!dev->sriov.is_going_down) {
                        mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
+                       mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
+                                                   MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK);
+               }
        }
        mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
 }
 
+static void propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
+                             struct mlx4_eqe *eqe)
+{
+       __propagate_pkey_ev(dev, port_num, GET_BLK_PTR_FROM_EQE(eqe),
+                           GET_MASK_FROM_EQE(eqe));
+}
+
+static void handle_slaves_guid_change(struct mlx4_ib_dev *dev, u8 port_num,
+                                     u32 guid_tbl_blk_num, u32 change_bitmap)
+{
+       struct ib_smp *in_mad  = NULL;
+       struct ib_smp *out_mad  = NULL;
+       u16 i;
+
+       if (!mlx4_is_mfunc(dev->dev) || !mlx4_is_master(dev->dev))
+               return;
+
+       in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
+       out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
+       if (!in_mad || !out_mad) {
+               mlx4_ib_warn(&dev->ib_dev, "failed to allocate memory for guid info mads\n");
+               goto out;
+       }
+
+       guid_tbl_blk_num  *= 4;
+
+       for (i = 0; i < 4; i++) {
+               if (change_bitmap && (!((change_bitmap >> (8 * i)) & 0xff)))
+                       continue;
+               memset(in_mad, 0, sizeof *in_mad);
+               memset(out_mad, 0, sizeof *out_mad);
+
+               in_mad->base_version  = 1;
+               in_mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
+               in_mad->class_version = 1;
+               in_mad->method        = IB_MGMT_METHOD_GET;
+               in_mad->attr_id       = IB_SMP_ATTR_GUID_INFO;
+               in_mad->attr_mod      = cpu_to_be32(guid_tbl_blk_num + i);
+
+               if (mlx4_MAD_IFC(dev,
+                                MLX4_MAD_IFC_IGNORE_KEYS | MLX4_MAD_IFC_NET_VIEW,
+                                port_num, NULL, NULL, in_mad, out_mad)) {
+                       mlx4_ib_warn(&dev->ib_dev, "Failed in get GUID INFO MAD_IFC\n");
+                       goto out;
+               }
+
+               mlx4_ib_update_cache_on_guid_change(dev, guid_tbl_blk_num + i,
+                                                   port_num,
+                                                   (u8 *)(&((struct ib_smp *)out_mad)->data));
+               mlx4_ib_notify_slaves_on_guid_change(dev, guid_tbl_blk_num + i,
+                                                    port_num,
+                                                    (u8 *)(&((struct ib_smp *)out_mad)->data));
+       }
+
+out:
+       kfree(in_mad);
+       kfree(out_mad);
+       return;
+}
+
 void handle_port_mgmt_change_event(struct work_struct *work)
 {
        struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
@@ -806,6 +952,8 @@ void handle_port_mgmt_change_event(struct work_struct *work)
        struct mlx4_eqe *eqe = &(ew->ib_eqe);
        u8 port = eqe->event.port_mgmt_change.port;
        u32 changed_attr;
+       u32 tbl_block;
+       u32 change_bitmap;
 
        switch (eqe->subtype) {
        case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
@@ -821,11 +969,16 @@ void handle_port_mgmt_change_event(struct work_struct *work)
 
                /* Check if it is a lid change event */
                if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
-                       mlx4_ib_dispatch_event(dev, port, IB_EVENT_LID_CHANGE);
+                       handle_lid_change_event(dev, port);
 
                /* Generate GUID changed event */
-               if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK)
+               if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK) {
                        mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
+                       /*if master, notify all slaves*/
+                       if (mlx4_is_master(dev->dev))
+                               mlx4_gen_slaves_port_mgt_ev(dev->dev, port,
+                                                           MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK);
+               }
 
                if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
                        handle_client_rereg_event(dev, port);
@@ -833,11 +986,19 @@ void handle_port_mgmt_change_event(struct work_struct *work)
 
        case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
                mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
+               if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
+                       propagate_pkey_ev(dev, port, eqe);
                break;
        case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
                /* paravirtualized master's guid is guid 0 -- does not change */
                if (!mlx4_is_master(dev->dev))
                        mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
+               /*if master, notify relevant slaves*/
+               else if (!dev->sriov.is_going_down) {
+                       tbl_block = GET_BLK_PTR_FROM_EQE(eqe);
+                       change_bitmap = GET_MASK_FROM_EQE(eqe);
+                       handle_slaves_guid_change(dev, port, tbl_block, change_bitmap);
+               }
                break;
        default:
                pr_warn("Unsupported subtype 0x%x for "
@@ -913,9 +1074,9 @@ static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
 
 static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
 {
-       int slave_start = dev->dev->caps.sqp_start + 8 * slave;
+       int proxy_start = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave;
 
-       return (qpn >= slave_start && qpn <= slave_start + 1);
+       return (qpn >= proxy_start && qpn <= proxy_start + 1);
 }
 
 
@@ -1030,14 +1191,14 @@ static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc
        int slave;
 
        /* Get slave that sent this packet */
-       if (wc->src_qp < dev->dev->caps.sqp_start ||
-           wc->src_qp >= dev->dev->caps.base_tunnel_sqpn ||
+       if (wc->src_qp < dev->dev->phys_caps.base_proxy_sqpn ||
+           wc->src_qp >= dev->dev->phys_caps.base_proxy_sqpn + 8 * MLX4_MFUNC_MAX ||
            (wc->src_qp & 0x1) != ctx->port - 1 ||
            wc->src_qp & 0x4) {
                mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
                return;
        }
-       slave = ((wc->src_qp & ~0x7) - dev->dev->caps.sqp_start) / 8;
+       slave = ((wc->src_qp & ~0x7) - dev->dev->phys_caps.base_proxy_sqpn) / 8;
        if (slave != ctx->slave) {
                mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
                             "belongs to another slave\n", wc->src_qp);
@@ -1491,6 +1652,9 @@ static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
 {
        int ret, cq_size;
 
+       if (ctx->state != DEMUX_PV_STATE_DOWN)
+               return -EEXIST;
+
        ctx->state = DEMUX_PV_STATE_STARTING;
        /* have QP0 only on port owner, and only if link layer is IB */
        if (ctx->slave == mlx4_master_func_num(to_mdev(ctx->ib_dev)->dev) &&
@@ -1808,9 +1972,32 @@ int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
                return 0;
        }
 
+       for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
+               if (i == mlx4_master_func_num(dev->dev))
+                       mlx4_put_slave_node_guid(dev->dev, i, dev->ib_dev.node_guid);
+               else
+                       mlx4_put_slave_node_guid(dev->dev, i, mlx4_ib_gen_node_guid());
+       }
+
+       err = mlx4_ib_init_alias_guid_service(dev);
+       if (err) {
+               mlx4_ib_warn(&dev->ib_dev, "Failed init alias guid process.\n");
+               goto paravirt_err;
+       }
+       err = mlx4_ib_device_register_sysfs(dev);
+       if (err) {
+               mlx4_ib_warn(&dev->ib_dev, "Failed to register sysfs\n");
+               goto sysfs_err;
+       }
+
        mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
                     dev->dev->caps.sqp_demux);
        for (i = 0; i < dev->num_ports; i++) {
+               union ib_gid gid;
+               err = __mlx4_ib_query_gid(&dev->ib_dev, i + 1, 0, &gid, 1);
+               if (err)
+                       goto demux_err;
+               dev->sriov.demux[i].guid_cache[0] = gid.global.interface_id;
                err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
                                      &dev->sriov.sqps[i]);
                if (err)
@@ -1828,6 +2015,12 @@ demux_err:
                mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
                --i;
        }
+       mlx4_ib_device_unregister_sysfs(dev);
+
+sysfs_err:
+       mlx4_ib_destroy_alias_guid_service(dev);
+
+paravirt_err:
        mlx4_ib_cm_paravirt_clean(dev, -1);
 
        return err;
@@ -1854,5 +2047,7 @@ void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
                }
 
                mlx4_ib_cm_paravirt_clean(dev, -1);
+               mlx4_ib_destroy_alias_guid_service(dev);
+               mlx4_ib_device_unregister_sysfs(dev);
        }
 }