1*eff901d3SEli Cohen /* 2*eff901d3SEli Cohen * Copyright (c) 2016, Mellanox Technologies. All rights reserved. 3*eff901d3SEli Cohen * 4*eff901d3SEli Cohen * This software is available to you under a choice of one of two 5*eff901d3SEli Cohen * licenses. You may choose to be licensed under the terms of the GNU 6*eff901d3SEli Cohen * General Public License (GPL) Version 2, available from the file 7*eff901d3SEli Cohen * COPYING in the main directory of this source tree, or the 8*eff901d3SEli Cohen * OpenIB.org BSD license below: 9*eff901d3SEli Cohen * 10*eff901d3SEli Cohen * Redistribution and use in source and binary forms, with or 11*eff901d3SEli Cohen * without modification, are permitted provided that the following 12*eff901d3SEli Cohen * conditions are met: 13*eff901d3SEli Cohen * 14*eff901d3SEli Cohen * - Redistributions of source code must retain the above 15*eff901d3SEli Cohen * copyright notice, this list of conditions and the following 16*eff901d3SEli Cohen * disclaimer. 17*eff901d3SEli Cohen * 18*eff901d3SEli Cohen * - Redistributions in binary form must reproduce the above 19*eff901d3SEli Cohen * copyright notice, this list of conditions and the following 20*eff901d3SEli Cohen * disclaimer in the documentation and/or other materials 21*eff901d3SEli Cohen * provided with the distribution. 22*eff901d3SEli Cohen * 23*eff901d3SEli Cohen * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24*eff901d3SEli Cohen * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25*eff901d3SEli Cohen * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26*eff901d3SEli Cohen * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27*eff901d3SEli Cohen * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28*eff901d3SEli Cohen * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29*eff901d3SEli Cohen * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30*eff901d3SEli Cohen * SOFTWARE. 31*eff901d3SEli Cohen */ 32*eff901d3SEli Cohen 33*eff901d3SEli Cohen #include <linux/module.h> 34*eff901d3SEli Cohen #include <linux/mlx5/vport.h> 35*eff901d3SEli Cohen #include "mlx5_ib.h" 36*eff901d3SEli Cohen 37*eff901d3SEli Cohen static inline u32 mlx_to_net_policy(enum port_state_policy mlx_policy) 38*eff901d3SEli Cohen { 39*eff901d3SEli Cohen switch (mlx_policy) { 40*eff901d3SEli Cohen case MLX5_POLICY_DOWN: 41*eff901d3SEli Cohen return IFLA_VF_LINK_STATE_DISABLE; 42*eff901d3SEli Cohen case MLX5_POLICY_UP: 43*eff901d3SEli Cohen return IFLA_VF_LINK_STATE_ENABLE; 44*eff901d3SEli Cohen case MLX5_POLICY_FOLLOW: 45*eff901d3SEli Cohen return IFLA_VF_LINK_STATE_AUTO; 46*eff901d3SEli Cohen default: 47*eff901d3SEli Cohen return __IFLA_VF_LINK_STATE_MAX; 48*eff901d3SEli Cohen } 49*eff901d3SEli Cohen } 50*eff901d3SEli Cohen 51*eff901d3SEli Cohen int mlx5_ib_get_vf_config(struct ib_device *device, int vf, u8 port, 52*eff901d3SEli Cohen struct ifla_vf_info *info) 53*eff901d3SEli Cohen { 54*eff901d3SEli Cohen struct mlx5_ib_dev *dev = to_mdev(device); 55*eff901d3SEli Cohen struct mlx5_core_dev *mdev = dev->mdev; 56*eff901d3SEli Cohen struct mlx5_hca_vport_context *rep; 57*eff901d3SEli Cohen int err; 58*eff901d3SEli Cohen 59*eff901d3SEli Cohen rep = kzalloc(sizeof(*rep), GFP_KERNEL); 60*eff901d3SEli Cohen if (!rep) 61*eff901d3SEli Cohen return -ENOMEM; 62*eff901d3SEli Cohen 63*eff901d3SEli Cohen err = mlx5_query_hca_vport_context(mdev, 1, 1, vf + 1, rep); 64*eff901d3SEli Cohen if (err) { 65*eff901d3SEli Cohen mlx5_ib_warn(dev, "failed to query port policy for vf %d (%d)\n", 66*eff901d3SEli Cohen vf, err); 67*eff901d3SEli Cohen goto free; 68*eff901d3SEli Cohen } 69*eff901d3SEli Cohen memset(info, 0, sizeof(*info)); 70*eff901d3SEli Cohen info->linkstate = mlx_to_net_policy(rep->policy); 71*eff901d3SEli Cohen if (info->linkstate == __IFLA_VF_LINK_STATE_MAX) 72*eff901d3SEli Cohen err = -EINVAL; 73*eff901d3SEli Cohen 74*eff901d3SEli Cohen free: 75*eff901d3SEli Cohen kfree(rep); 76*eff901d3SEli Cohen return err; 77*eff901d3SEli Cohen } 78*eff901d3SEli Cohen 79*eff901d3SEli Cohen static inline enum port_state_policy net_to_mlx_policy(int policy) 80*eff901d3SEli Cohen { 81*eff901d3SEli Cohen switch (policy) { 82*eff901d3SEli Cohen case IFLA_VF_LINK_STATE_DISABLE: 83*eff901d3SEli Cohen return MLX5_POLICY_DOWN; 84*eff901d3SEli Cohen case IFLA_VF_LINK_STATE_ENABLE: 85*eff901d3SEli Cohen return MLX5_POLICY_UP; 86*eff901d3SEli Cohen case IFLA_VF_LINK_STATE_AUTO: 87*eff901d3SEli Cohen return MLX5_POLICY_FOLLOW; 88*eff901d3SEli Cohen default: 89*eff901d3SEli Cohen return MLX5_POLICY_INVALID; 90*eff901d3SEli Cohen } 91*eff901d3SEli Cohen } 92*eff901d3SEli Cohen 93*eff901d3SEli Cohen int mlx5_ib_set_vf_link_state(struct ib_device *device, int vf, 94*eff901d3SEli Cohen u8 port, int state) 95*eff901d3SEli Cohen { 96*eff901d3SEli Cohen struct mlx5_ib_dev *dev = to_mdev(device); 97*eff901d3SEli Cohen struct mlx5_core_dev *mdev = dev->mdev; 98*eff901d3SEli Cohen struct mlx5_hca_vport_context *in; 99*eff901d3SEli Cohen int err; 100*eff901d3SEli Cohen 101*eff901d3SEli Cohen in = kzalloc(sizeof(*in), GFP_KERNEL); 102*eff901d3SEli Cohen if (!in) 103*eff901d3SEli Cohen return -ENOMEM; 104*eff901d3SEli Cohen 105*eff901d3SEli Cohen in->policy = net_to_mlx_policy(state); 106*eff901d3SEli Cohen if (in->policy == MLX5_POLICY_INVALID) { 107*eff901d3SEli Cohen err = -EINVAL; 108*eff901d3SEli Cohen goto out; 109*eff901d3SEli Cohen } 110*eff901d3SEli Cohen in->field_select = MLX5_HCA_VPORT_SEL_STATE_POLICY; 111*eff901d3SEli Cohen err = mlx5_core_modify_hca_vport_context(mdev, 1, 1, vf + 1, in); 112*eff901d3SEli Cohen 113*eff901d3SEli Cohen out: 114*eff901d3SEli Cohen kfree(in); 115*eff901d3SEli Cohen return err; 116*eff901d3SEli Cohen } 117*eff901d3SEli Cohen 118*eff901d3SEli Cohen int mlx5_ib_get_vf_stats(struct ib_device *device, int vf, 119*eff901d3SEli Cohen u8 port, struct ifla_vf_stats *stats) 120*eff901d3SEli Cohen { 121*eff901d3SEli Cohen int out_sz = MLX5_ST_SZ_BYTES(query_vport_counter_out); 122*eff901d3SEli Cohen struct mlx5_core_dev *mdev; 123*eff901d3SEli Cohen struct mlx5_ib_dev *dev; 124*eff901d3SEli Cohen void *out; 125*eff901d3SEli Cohen int err; 126*eff901d3SEli Cohen 127*eff901d3SEli Cohen dev = to_mdev(device); 128*eff901d3SEli Cohen mdev = dev->mdev; 129*eff901d3SEli Cohen 130*eff901d3SEli Cohen out = kzalloc(out_sz, GFP_KERNEL); 131*eff901d3SEli Cohen if (!out) 132*eff901d3SEli Cohen return -ENOMEM; 133*eff901d3SEli Cohen 134*eff901d3SEli Cohen err = mlx5_core_query_vport_counter(mdev, true, vf, port, out, out_sz); 135*eff901d3SEli Cohen if (err) 136*eff901d3SEli Cohen goto ex; 137*eff901d3SEli Cohen 138*eff901d3SEli Cohen stats->rx_packets = MLX5_GET64_PR(query_vport_counter_out, out, received_ib_unicast.packets); 139*eff901d3SEli Cohen stats->tx_packets = MLX5_GET64_PR(query_vport_counter_out, out, transmitted_ib_unicast.packets); 140*eff901d3SEli Cohen stats->rx_bytes = MLX5_GET64_PR(query_vport_counter_out, out, received_ib_unicast.octets); 141*eff901d3SEli Cohen stats->tx_bytes = MLX5_GET64_PR(query_vport_counter_out, out, transmitted_ib_unicast.octets); 142*eff901d3SEli Cohen stats->multicast = MLX5_GET64_PR(query_vport_counter_out, out, received_ib_multicast.packets); 143*eff901d3SEli Cohen 144*eff901d3SEli Cohen ex: 145*eff901d3SEli Cohen kfree(out); 146*eff901d3SEli Cohen return err; 147*eff901d3SEli Cohen } 148*eff901d3SEli Cohen 149*eff901d3SEli Cohen static int set_vf_node_guid(struct ib_device *device, int vf, u8 port, u64 guid) 150*eff901d3SEli Cohen { 151*eff901d3SEli Cohen struct mlx5_ib_dev *dev = to_mdev(device); 152*eff901d3SEli Cohen struct mlx5_core_dev *mdev = dev->mdev; 153*eff901d3SEli Cohen struct mlx5_hca_vport_context *in; 154*eff901d3SEli Cohen int err; 155*eff901d3SEli Cohen 156*eff901d3SEli Cohen in = kzalloc(sizeof(*in), GFP_KERNEL); 157*eff901d3SEli Cohen if (!in) 158*eff901d3SEli Cohen return -ENOMEM; 159*eff901d3SEli Cohen 160*eff901d3SEli Cohen in->field_select = MLX5_HCA_VPORT_SEL_NODE_GUID; 161*eff901d3SEli Cohen in->node_guid = guid; 162*eff901d3SEli Cohen err = mlx5_core_modify_hca_vport_context(mdev, 1, 1, vf + 1, in); 163*eff901d3SEli Cohen kfree(in); 164*eff901d3SEli Cohen return err; 165*eff901d3SEli Cohen } 166*eff901d3SEli Cohen 167*eff901d3SEli Cohen static int set_vf_port_guid(struct ib_device *device, int vf, u8 port, u64 guid) 168*eff901d3SEli Cohen { 169*eff901d3SEli Cohen struct mlx5_ib_dev *dev = to_mdev(device); 170*eff901d3SEli Cohen struct mlx5_core_dev *mdev = dev->mdev; 171*eff901d3SEli Cohen struct mlx5_hca_vport_context *in; 172*eff901d3SEli Cohen int err; 173*eff901d3SEli Cohen 174*eff901d3SEli Cohen in = kzalloc(sizeof(*in), GFP_KERNEL); 175*eff901d3SEli Cohen if (!in) 176*eff901d3SEli Cohen return -ENOMEM; 177*eff901d3SEli Cohen 178*eff901d3SEli Cohen in->field_select = MLX5_HCA_VPORT_SEL_PORT_GUID; 179*eff901d3SEli Cohen in->port_guid = guid; 180*eff901d3SEli Cohen err = mlx5_core_modify_hca_vport_context(mdev, 1, 1, vf + 1, in); 181*eff901d3SEli Cohen kfree(in); 182*eff901d3SEli Cohen return err; 183*eff901d3SEli Cohen } 184*eff901d3SEli Cohen 185*eff901d3SEli Cohen int mlx5_ib_set_vf_guid(struct ib_device *device, int vf, u8 port, 186*eff901d3SEli Cohen u64 guid, int type) 187*eff901d3SEli Cohen { 188*eff901d3SEli Cohen if (type == IFLA_VF_IB_NODE_GUID) 189*eff901d3SEli Cohen return set_vf_node_guid(device, vf, port, guid); 190*eff901d3SEli Cohen else if (type == IFLA_VF_IB_PORT_GUID) 191*eff901d3SEli Cohen return set_vf_port_guid(device, vf, port, guid); 192*eff901d3SEli Cohen 193*eff901d3SEli Cohen return -EINVAL; 194*eff901d3SEli Cohen } 195