#include "enetc_pf_common.h"
#define ENETC_PF_MSG_SUCCESS FIELD_PREP(ENETC_PF_MSG_CLASS_ID, \
ENETC_MSG_CLASS_ID_CMD_SUCCESS)
#define ENETC_PF_MSG_NOTSUPP FIELD_PREP(ENETC_PF_MSG_CLASS_ID, \
ENETC_MSG_CLASS_ID_CMD_NOT_SUPPORT)
#define ENETC_PF_MSG_PERM_DENY FIELD_PREP(ENETC_PF_MSG_CLASS_ID, \
ENETC_MSG_CLASS_ID_PERMISSION_DENY)
#define ENETC_PF_MSG_INV_LEN FIELD_PREP(ENETC_PF_MSG_CLASS_ID, \
ENETC_MSG_CLASS_ID_INVALID_MSG_LEN)
#define ENETC_PF_MSG_MF(code) (FIELD_PREP(ENETC_PF_MSG_CLASS_ID, \
ENETC_MSG_CLASS_ID_MAC_FILTER) | \
FIELD_PREP(ENETC_PF_MSG_CLASS_CODE, (code)))
static void enetc_disable_psiier_interrupts(struct enetc_pf *pf)
{
struct enetc_hw *hw = &pf->si->hw;
enetc_wr(hw, ENETC_PSIIER, 0);
}
static void enetc_enable_psiier_interrupts(struct enetc_pf *pf)
{
u32 psiier = ENETC_PSIMR_MASK(pf->num_vfs);
struct enetc_hw *hw = &pf->si->hw;
if (pf->ops->vf_flr_handler)
psiier |= ENETC_VFFLR_MASK(pf->num_vfs);
enetc_wr(hw, ENETC_PSIIER, psiier);
}
static irqreturn_t enetc_msg_psi_msix(int irq, void *data)
{
struct enetc_si *si = (struct enetc_si *)data;
struct enetc_pf *pf = enetc_si_priv(si);
enetc_disable_psiier_interrupts(pf);
schedule_work(&si->msg_task);
return IRQ_HANDLED;
}
static bool enetc_msg_check_crc16(void *msg_addr, u32 msg_size)
{
u32 data_size = msg_size - 2;
u8 *data_buf = msg_addr + 2;
u16 verify_val;
verify_val = crc_itu_t(ENETC_CRC_INIT, data_buf, data_size);
verify_val = crc_itu_t(verify_val, msg_addr, 2);
if (verify_val)
return false;
return true;
}
static u16 enetc_msg_set_vf_primary_mac_addr(struct enetc_pf *pf, int vf_id,
void *vf_msg)
{
struct enetc_vf_state *vf_state = &pf->vf_state[vf_id];
struct enetc_msg_mac_exact_filter *msg = vf_msg;
struct device *dev = &pf->si->pdev->dev;
u16 pf_msg = ENETC_PF_MSG_SUCCESS;
char *addr = msg->mac[0].addr;
mutex_lock(&vf_state->lock);
if (!(vf_state->flags & ENETC_VF_FLAG_TRUSTED)) {
pf_msg = ENETC_PF_MSG_PERM_DENY;
goto vf_state_unlock;
}
if (!is_valid_ether_addr(addr)) {
dev_err_ratelimited(dev, "VF%d attempted to set invalid MAC\n",
vf_id);
pf_msg = ENETC_PF_MSG_MF(ENETC_MF_CLASS_CODE_INVALID_MAC);
goto vf_state_unlock;
}
if (vf_state->flags & ENETC_VF_FLAG_PF_SET_MAC) {
dev_err_ratelimited(dev,
"VF%d attempted to override PF set MAC\n",
vf_id);
pf_msg = FIELD_PREP(ENETC_PF_MSG_CLASS_ID,
ENETC_MSG_CLASS_ID_CMD_NOT_PERMITTED);
goto vf_state_unlock;
}
enetc_set_si_hw_addr(pf, vf_id + 1, addr);
vf_state_unlock:
mutex_unlock(&vf_state->lock);
return pf_msg;
}
static u16 enetc_msg_set_vf_mac_hash_filter(struct enetc_pf *pf, int vf_id,
void *vf_msg)
{
struct enetc_vf_state *vf_state = &pf->vf_state[vf_id];
struct enetc_msg_mac_hash_filter *msg = vf_msg;
u16 pf_msg = ENETC_PF_MSG_SUCCESS;
struct enetc_si *si = pf->si;
int si_id = vf_id + 1;
u64 uc_hash, mc_hash;
bool trusted;
int type;
if (FIELD_GET(ENETC_MSG_MAC_HASH_SIZE, msg->sz_type) !=
ENETC_MAC_HASH_TABLE_SIZE_64)
return ENETC_PF_MSG_NOTSUPP;
mutex_lock(&vf_state->lock);
trusted = !!(vf_state->flags & ENETC_VF_FLAG_TRUSTED);
type = FIELD_GET(ENETC_MSG_MAC_TYPE, msg->sz_type);
switch (type) {
case ENETC_MAC_FILTER_TYPE_UC:
if (!trusted) {
pf_msg = ENETC_PF_MSG_PERM_DENY;
goto vf_state_unlock;
}
uc_hash = (u64)msg->hash_tbl[1] << 32 | msg->hash_tbl[0];
enetc_set_si_uc_hash_filter(si, si_id, uc_hash);
break;
case ENETC_MAC_FILTER_TYPE_MC:
mc_hash = (u64)msg->hash_tbl[1] << 32 | msg->hash_tbl[0];
if (!trusted &&
hweight64(mc_hash) > ENETC_VF_MC_HASH_BITS_MAX) {
pf_msg = ENETC_PF_MSG_PERM_DENY;
goto vf_state_unlock;
}
enetc_set_si_mc_hash_filter(si, si_id, mc_hash);
break;
case ENETC_MAC_FILTER_TYPE_ALL:
if (!msg->hdr.len) {
pf_msg = ENETC_PF_MSG_INV_LEN;
goto vf_state_unlock;
}
uc_hash = (u64)msg->hash_tbl[1] << 32 | msg->hash_tbl[0];
mc_hash = (u64)msg->hash_tbl[3] << 32 | msg->hash_tbl[2];
if (!trusted &&
(hweight64(mc_hash) <= ENETC_VF_MC_HASH_BITS_MAX)) {
enetc_set_si_mc_hash_filter(si, si_id, mc_hash);
pf_msg = ENETC_PF_MSG_MF(ENETC_MF_CLASS_CODE_UCF_DENY);
goto vf_state_unlock;
}
if (!trusted) {
pf_msg = ENETC_PF_MSG_PERM_DENY;
goto vf_state_unlock;
}
enetc_set_si_uc_hash_filter(si, si_id, uc_hash);
enetc_set_si_mc_hash_filter(si, si_id, mc_hash);
break;
default:
pf_msg = ENETC_PF_MSG_MF(ENETC_MF_CLASS_CODE_INVALID_TYPE);
}
vf_state_unlock:
mutex_unlock(&vf_state->lock);
return pf_msg;
}
static u16 enetc_msg_set_vf_mac_promisc_mode(struct enetc_pf *pf, int vf_id,
void *vf_msg)
{
struct enetc_vf_state *vf_state = &pf->vf_state[vf_id];
struct enetc_msg_mac_promisc_mode *msg = vf_msg;
u16 pf_msg = ENETC_PF_MSG_SUCCESS;
struct enetc_si *si = pf->si;
bool promisc, flush_macs;
int si_id = vf_id + 1;
int type;
flush_macs = !!(msg->config & ENETC_MSG_MAC_FLUSH_MACS);
type = FIELD_GET(ENETC_MSG_MAC_TYPE, msg->config);
if (!type)
return ENETC_PF_MSG_MF(ENETC_MF_CLASS_CODE_INVALID_TYPE);
mutex_lock(&vf_state->lock);
promisc = !!(msg->config & ENETC_MSG_MAC_PROMISC_MODE);
if (promisc && !(vf_state->flags & ENETC_VF_FLAG_TRUSTED)) {
pf_msg = ENETC_PF_MSG_PERM_DENY;
goto vf_state_unlock;
}
if (type & ENETC_MAC_FILTER_TYPE_UC) {
if (promisc)
vf_state->flags |= ENETC_VF_FLAG_UC_PROMISC;
else
vf_state->flags &= ~ENETC_VF_FLAG_UC_PROMISC;
}
if (type & ENETC_MAC_FILTER_TYPE_MC) {
if (promisc)
vf_state->flags |= ENETC_VF_FLAG_MC_PROMISC;
else
vf_state->flags &= ~ENETC_VF_FLAG_MC_PROMISC;
}
spin_lock(&si->gen_lock);
if (type & ENETC_MAC_FILTER_TYPE_UC)
enetc_set_si_uc_promisc(si, si_id, promisc);
if (type & ENETC_MAC_FILTER_TYPE_MC)
enetc_set_si_mc_promisc(si, si_id, promisc);
spin_unlock(&si->gen_lock);
if ((type & ENETC_MAC_FILTER_TYPE_UC) && flush_macs)
enetc_set_si_uc_hash_filter(si, si_id, 0);
if ((type & ENETC_MAC_FILTER_TYPE_MC) && flush_macs)
enetc_set_si_mc_hash_filter(si, si_id, 0);
vf_state_unlock:
mutex_unlock(&vf_state->lock);
return pf_msg;
}
static u16 enetc_msg_handle_mac_filter(struct enetc_pf *pf, int vf_id,
void *vf_msg)
{
struct enetc_msg_header *msg_hdr = vf_msg;
switch (msg_hdr->cmd_id) {
case ENETC_MSG_SET_PRIMARY_MAC:
return enetc_msg_set_vf_primary_mac_addr(pf, vf_id, vf_msg);
case ENETC_MSG_SET_MAC_HASH_TABLE:
return enetc_msg_set_vf_mac_hash_filter(pf, vf_id, vf_msg);
case ENETC_MSG_SET_MAC_PROMISC_MODE:
return enetc_msg_set_vf_mac_promisc_mode(pf, vf_id, vf_msg);
default:
return ENETC_PF_MSG_NOTSUPP;
}
}
static u16 enetc_msg_handle_ip_revision(struct enetc_pf *pf, void *vf_msg)
{
struct enetc_msg_header *msg_hdr = vf_msg;
switch (msg_hdr->cmd_id) {
case ENETC_MSG_GET_IP_MN:
return (FIELD_PREP(ENETC_PF_MSG_CLASS_ID,
ENETC_MSG_CLASS_ID_IP_REVISION) |
FIELD_PREP(ENETC_PF_MSG_CLASS_CODE_U8,
pf->si->revision));
default:
return ENETC_PF_MSG_NOTSUPP;
}
}
static void enetc_pf_reply_msg(struct enetc_hw *hw, int vf_id, u16 pf_msg)
{
enetc_wr(hw, ENETC_PSIIDR, ENETC_PSIMR_BIT(vf_id));
enetc_wr(hw, ENETC_PSIMSGRR, ENETC_SIMSGSR_SET_MC(pf_msg) |
ENETC_PSIMR_BIT(vf_id));
}
static u16 enetc_build_link_status_msg(struct enetc_ndev_priv *priv,
bool link_up)
{
u8 status = 0;
if (link_up) {
if (test_bit(ENETC_RXBDR_CM, &priv->flags))
status |= ENETC_CLASS_CODE_TX_PAUSE_EN;
} else {
status |= ENETC_CLASS_CODE_LINK_DOWN;
}
return FIELD_PREP(ENETC_PF_MSG_CLASS_ID,
ENETC_MSG_CLASS_ID_LINK_STATUS) |
FIELD_PREP(ENETC_PF_MSG_CLASS_CODE_U8, status);
}
static void enetc_msg_get_link_status(struct enetc_pf *pf, int vf_id)
{
struct enetc_ndev_priv *priv = netdev_priv(pf->si->ndev);
struct enetc_si *si = pf->si;
u16 pf_msg;
spin_lock(&si->gen_lock);
pf_msg = enetc_build_link_status_msg(priv, pf->link_up);
enetc_pf_reply_msg(&si->hw, vf_id, pf_msg);
spin_unlock(&si->gen_lock);
}
static void enetc_msg_register_link_status_notifier(struct enetc_pf *pf,
int vf_id)
{
struct enetc_si *si = pf->si;
spin_lock(&si->gen_lock);
enetc_pf_reply_msg(&si->hw, vf_id, ENETC_PF_MSG_SUCCESS);
if (!pf->sriov_enabled) {
spin_unlock(&si->gen_lock);
return;
}
pf->link_status_ms_mask |= PSIMSGSR_MS(vf_id);
spin_unlock(&si->gen_lock);
queue_work(si->workqueue, &pf->link_status_task);
}
static void enetc_msg_unregister_link_status_notifier(struct enetc_pf *pf,
int vf_id)
{
spin_lock(&pf->si->gen_lock);
pf->link_status_ms_mask &= ~PSIMSGSR_MS(vf_id);
enetc_pf_reply_msg(&pf->si->hw, vf_id, ENETC_PF_MSG_SUCCESS);
spin_unlock(&pf->si->gen_lock);
}
static u16 enetc_msg_handle_link_status(struct enetc_pf *pf, int vf_id,
void *vf_msg)
{
struct enetc_msg_header *msg_hdr = vf_msg;
switch (msg_hdr->cmd_id) {
case ENETC_MSG_GET_CURRENT_LINK_STATUS:
enetc_msg_get_link_status(pf, vf_id);
break;
case ENETC_MSG_REGISTER_LINK_CHANGE_NOTIFIER:
enetc_msg_register_link_status_notifier(pf, vf_id);
break;
case ENETC_MSG_UNREGISTER_LINK_CHANGE_NOTIFIER:
enetc_msg_unregister_link_status_notifier(pf, vf_id);
break;
default:
return ENETC_PF_MSG_NOTSUPP;
}
return 0;
}
static u16 enetc_build_link_speed_msg(int speed, int duplex)
{
u32 speed_code = ENETC_MSG_SPEED_UNKNOWN;
switch (speed) {
case SPEED_10:
if (duplex == DUPLEX_HALF)
speed_code = ENETC_MSG_SPEED_10M_HD;
else if (duplex == DUPLEX_FULL)
speed_code = ENETC_MSG_SPEED_10M_FD;
break;
case SPEED_100:
if (duplex == DUPLEX_HALF)
speed_code = ENETC_MSG_SPEED_100M_HD;
else if (duplex == DUPLEX_FULL)
speed_code = ENETC_MSG_SPEED_100M_FD;
break;
case SPEED_1000:
speed_code = ENETC_MSG_SPEED_1000M;
break;
case SPEED_2500:
speed_code = ENETC_MSG_SPEED_2500M;
break;
case SPEED_5000:
speed_code = ENETC_MSG_SPEED_5G;
break;
default:
if (speed < SPEED_5000)
break;
speed_code = (speed - SPEED_5000) / SPEED_1000 +
ENETC_MSG_SPEED_5G;
if (speed_code > ENETC_MSG_SPEED_MAX)
speed_code = ENETC_MSG_SPEED_UNKNOWN;
}
return FIELD_PREP(ENETC_PF_MSG_CLASS_ID,
ENETC_MSG_CLASS_ID_LINK_SPEED) |
FIELD_PREP(ENETC_PF_MSG_CLASS_CODE_U8, speed_code);
}
static u16 enetc_msg_get_link_speed(struct enetc_pf *pf, int vf_id)
{
struct enetc_ndev_priv *priv = netdev_priv(pf->si->ndev);
struct enetc_vf_state *vf_state = &pf->vf_state[vf_id];
struct ethtool_link_ksettings link_info = {};
mutex_lock(&vf_state->lock);
if (!(vf_state->flags & ENETC_VF_FLAG_TRUSTED)) {
mutex_unlock(&vf_state->lock);
return ENETC_PF_MSG_PERM_DENY;
}
mutex_unlock(&vf_state->lock);
rtnl_lock();
phylink_ethtool_ksettings_get(priv->phylink, &link_info);
rtnl_unlock();
return enetc_build_link_speed_msg(link_info.base.speed,
link_info.base.duplex);
}
static u16 enetc_msg_handle_link_speed(struct enetc_pf *pf, int vf_id,
void *vf_msg)
{
struct enetc_msg_header *msg_hdr = vf_msg;
switch (msg_hdr->cmd_id) {
case ENETC_MSG_GET_CURRENT_LINK_SPEED:
return enetc_msg_get_link_speed(pf, vf_id);
case ENETC_MSG_REGISTER_SPEED_CHANGE_NOTIFIER:
case ENETC_MSG_UNREGISTER_SPEED_CHANGE_NOTIFIER:
default:
return ENETC_PF_MSG_NOTSUPP;
}
}
static void enetc_msg_handle_rxmsg(struct enetc_pf *pf, int vf_id,
u16 *pf_msg)
{
struct enetc_msg_swbd *msg_swbd = &pf->rxmsg[vf_id];
struct enetc_msg_header *msg_hdr = msg_swbd->vaddr;
u32 msg_size = ENETC_MSG_SIZE(msg_hdr->len);
struct device *dev = &pf->si->pdev->dev;
u8 *msg;
if (msg_size > ENETC_DEFAULT_MSG_SIZE) {
dev_err_ratelimited(dev,
"Invalid message size: %u\n", msg_size);
*pf_msg = ENETC_PF_MSG_INV_LEN;
return;
}
msg = kzalloc_objs(*msg, msg_size);
if (!msg) {
dev_err_ratelimited(dev,
"Failed to allocate message buffer\n");
*pf_msg = FIELD_PREP(ENETC_PF_MSG_CLASS_ID,
ENETC_MSG_CLASS_ID_CMD_FAIL);
return;
}
memcpy(msg, msg_swbd->vaddr, msg_size);
msg_hdr = (struct enetc_msg_header *)msg;
if (ENETC_MSG_SIZE(msg_hdr->len) != msg_size) {
*pf_msg = ENETC_PF_MSG_INV_LEN;
goto free_msg;
}
if (!enetc_msg_check_crc16(msg, msg_size)) {
dev_err_ratelimited(dev, "VSI to PSI Message CRC16 error\n");
*pf_msg = FIELD_PREP(ENETC_PF_MSG_CLASS_ID,
ENETC_MSG_CLASS_ID_CRC_ERROR);
goto free_msg;
}
*pf_msg = ENETC_PF_MSG_NOTSUPP;
if (FIELD_GET(ENETC_VF_MSG_COOKIE, msg_hdr->cookie)) {
dev_err_ratelimited(dev,
"Cookie field is not supported yet\n");
goto free_msg;
}
if (msg_hdr->proto_ver) {
dev_err_ratelimited(dev, "Unsupported protocol version %u\n",
msg_hdr->proto_ver);
goto free_msg;
}
if (is_enetc_rev1(pf->si) &&
!(msg_hdr->class_id == ENETC_MSG_CLASS_ID_MAC_FILTER &&
msg_hdr->cmd_id == ENETC_MSG_SET_PRIMARY_MAC)) {
dev_err_ratelimited(dev, "Unsupported message for ENETC v1\n");
goto free_msg;
}
switch (msg_hdr->class_id) {
case ENETC_MSG_CLASS_ID_MAC_FILTER:
*pf_msg = enetc_msg_handle_mac_filter(pf, vf_id, msg);
break;
case ENETC_MSG_CLASS_ID_IP_REVISION:
*pf_msg = enetc_msg_handle_ip_revision(pf, msg);
break;
case ENETC_MSG_CLASS_ID_LINK_STATUS:
*pf_msg = enetc_msg_handle_link_status(pf, vf_id, msg);
break;
case ENETC_MSG_CLASS_ID_LINK_SPEED:
*pf_msg = enetc_msg_handle_link_speed(pf, vf_id, msg);
break;
default:
dev_err_ratelimited(dev,
"Unsupported message class ID: 0x%x\n",
msg_hdr->class_id);
}
free_msg:
kfree(msg);
}
static void enetc_vf_flr_handler(struct enetc_pf *pf)
{
u32 flr_mask = ENETC_VFFLR_MASK(pf->num_vfs);
struct enetc_hw *hw = &pf->si->hw;
u32 flr_status;
if (!pf->ops->vf_flr_handler)
return;
flr_status = enetc_rd(hw, ENETC_PSIIDR) & flr_mask;
if (!flr_status)
return;
for (int i = 0; i < pf->num_vfs; i++) {
if (!(ENETC_VFFLR_BIT(i) & flr_status))
continue;
enetc_wr(hw, ENETC_PSIIDR, ENETC_VFFLR_BIT(i));
pf->ops->vf_flr_handler(pf, i);
}
}
static void enetc_msg_task(struct work_struct *work)
{
struct enetc_si *si = container_of(work, struct enetc_si, msg_task);
struct enetc_pf *pf = enetc_si_priv(si);
struct enetc_hw *hw = &si->hw;
u32 mr_status, mr_mask;
int i;
enetc_vf_flr_handler(pf);
mr_mask = ENETC_PSIMR_MASK(pf->num_vfs);
mr_status = (enetc_rd(hw, ENETC_PSIMSGRR) & mr_mask) |
(enetc_rd(hw, ENETC_PSIIDR) & mr_mask);
if (!mr_status)
goto out;
for (i = 0; i < pf->num_vfs; i++) {
u16 msg_code;
if (!(ENETC_PSIMR_BIT(i) & mr_status))
continue;
enetc_msg_handle_rxmsg(pf, i, &msg_code);
if (!msg_code)
continue;
enetc_pf_reply_msg(hw, i, msg_code);
}
out:
enetc_enable_psiier_interrupts(pf);
}
static int enetc_msg_alloc_mbx(struct enetc_si *si, int idx)
{
struct enetc_pf *pf = enetc_si_priv(si);
struct device *dev = &si->pdev->dev;
struct enetc_hw *hw = &si->hw;
struct enetc_msg_swbd *msg;
u32 val;
msg = &pf->rxmsg[idx];
msg->size = ENETC_DEFAULT_MSG_SIZE;
msg->vaddr = dma_alloc_coherent(dev, msg->size, &msg->dma,
GFP_KERNEL);
if (!msg->vaddr) {
dev_err(dev, "msg: fail to alloc dma buffer of size: %d\n",
msg->size);
return -ENOMEM;
}
val = lower_32_bits(msg->dma);
enetc_wr(hw, ENETC_PSIVMSGRCVAR0(idx), val);
val = upper_32_bits(msg->dma);
enetc_wr(hw, ENETC_PSIVMSGRCVAR1(idx), val);
return 0;
}
static void enetc_msg_free_mbx(struct enetc_si *si, int idx)
{
struct enetc_pf *pf = enetc_si_priv(si);
struct enetc_hw *hw = &si->hw;
struct enetc_msg_swbd *msg;
enetc_wr(hw, ENETC_PSIVMSGRCVAR0(idx), 0);
enetc_wr(hw, ENETC_PSIVMSGRCVAR1(idx), 0);
msg = &pf->rxmsg[idx];
dma_free_coherent(&si->pdev->dev, msg->size, msg->vaddr, msg->dma);
memset(msg, 0, sizeof(*msg));
}
static int enetc_msg_psi_init(struct enetc_pf *pf)
{
struct enetc_si *si = pf->si;
int vector, i, err;
for (i = 0; i < pf->num_vfs; i++) {
err = enetc_msg_alloc_mbx(si, i);
if (err)
goto free_mbx;
}
INIT_WORK(&si->msg_task, enetc_msg_task);
snprintf(si->msg_int_name, sizeof(si->msg_int_name), "%s-vfmsg",
si->ndev->name);
vector = pci_irq_vector(si->pdev, ENETC_SI_INT_IDX);
err = request_irq(vector, enetc_msg_psi_msix, 0, si->msg_int_name, si);
if (err) {
dev_err(&si->pdev->dev,
"PSI messaging: request_irq() failed!\n");
goto free_mbx;
}
enetc_wr(&si->hw, ENETC_SIMSIVR, ENETC_SI_INT_IDX);
enetc_enable_psiier_interrupts(pf);
return 0;
free_mbx:
for (i--; i >= 0; i--)
enetc_msg_free_mbx(si, i);
return err;
}
static void enetc_msg_clear_vf_config(struct enetc_pf *pf, int vf_id)
{
struct enetc_vf_state *vf_state = &pf->vf_state[vf_id];
struct enetc_si *si = pf->si;
int si_id = vf_id + 1;
if (is_enetc_rev1(si))
return;
mutex_lock(&vf_state->lock);
vf_state->flags &= ~(ENETC_VF_FLAG_UC_PROMISC |
ENETC_VF_FLAG_MC_PROMISC);
spin_lock(&si->gen_lock);
vf_state->msg_fail_cnt = 0;
enetc_set_si_uc_promisc(si, si_id, false);
enetc_set_si_mc_promisc(si, si_id, false);
spin_unlock(&si->gen_lock);
enetc_set_si_uc_hash_filter(si, si_id, 0);
enetc_set_si_mc_hash_filter(si, si_id, 0);
mutex_unlock(&vf_state->lock);
}
static void enetc_msg_psi_free(struct enetc_pf *pf)
{
struct enetc_si *si = pf->si;
int i;
enetc_disable_psiier_interrupts(pf);
free_irq(pci_irq_vector(si->pdev, ENETC_SI_INT_IDX), si);
cancel_work_sync(&si->msg_task);
enetc_disable_psiier_interrupts(pf);
for (i = 0; i < pf->num_vfs; i++) {
enetc_msg_free_mbx(si, i);
enetc_msg_clear_vf_config(pf, i);
}
}
int enetc_sriov_configure(struct pci_dev *pdev, int num_vfs)
{
struct enetc_si *si = pci_get_drvdata(pdev);
struct enetc_pf *pf = enetc_si_priv(si);
int err;
if (!num_vfs) {
spin_lock(&si->gen_lock);
pf->sriov_enabled = false;
pf->link_status_ms_mask = 0;
spin_unlock(&si->gen_lock);
pci_disable_sriov(pdev);
enetc_msg_psi_free(pf);
pf->num_vfs = 0;
} else {
pf->num_vfs = num_vfs;
err = enetc_msg_psi_init(pf);
if (err) {
dev_err(&pdev->dev, "enetc_msg_psi_init (%d)\n", err);
goto err_msg_psi;
}
pf->sriov_enabled = true;
err = pci_enable_sriov(pdev, num_vfs);
if (err) {
dev_err(&pdev->dev, "pci_enable_sriov err %d\n", err);
goto err_en_sriov;
}
}
return num_vfs;
err_en_sriov:
spin_lock(&si->gen_lock);
pf->sriov_enabled = false;
pf->link_status_ms_mask = 0;
spin_unlock(&si->gen_lock);
enetc_msg_psi_free(pf);
err_msg_psi:
pf->num_vfs = 0;
return err;
}
EXPORT_SYMBOL_GPL(enetc_sriov_configure);
void enetc_pf_send_link_status_msg(struct enetc_pf *pf)
{
struct enetc_ndev_priv *priv = netdev_priv(pf->si->ndev);
u16 pf_msg, ms_mask, new_ms_msk, ms_status;
struct enetc_si *si = pf->si;
int retry_num = 0;
retry:
spin_lock(&si->gen_lock);
ms_mask = pf->link_status_ms_mask;
if (!ms_mask)
goto unlock;
ms_status = enetc_rd(&si->hw, ENETC_PSIMSGSR) & 0xfffe;
if ((ms_mask & ms_status) && retry_num++ < 200) {
spin_unlock(&si->gen_lock);
usleep_range(1000, 1020);
goto retry;
}
new_ms_msk = ms_mask & (~ms_status);
if (!new_ms_msk) {
dev_err_ratelimited(&si->pdev->dev,
"All registered VFs (MS: 0x%x) are busy\n",
ms_mask);
goto ms_status_check;
}
if (new_ms_msk != ms_mask)
dev_warn_ratelimited(&si->pdev->dev,
"Failed to notify link status to VFs (MS: 0x%x)\n",
ms_mask ^ new_ms_msk);
pf_msg = enetc_build_link_status_msg(priv, pf->link_up);
enetc_wr(&si->hw, ENETC_PSIMSGSR,
FIELD_PREP(PSIMSGSR_MC, pf_msg) | new_ms_msk);
ms_status_check:
for (int i = 0; i < pf->num_vfs; i++) {
struct enetc_vf_state *vf_state = &pf->vf_state[i];
if (!(PSIMSGSR_MS(i) & ms_mask))
continue;
if (!(PSIMSGSR_MS(i) & ms_status)) {
vf_state->msg_fail_cnt = 0;
continue;
}
if (vf_state->msg_fail_cnt++ < 10)
continue;
vf_state->msg_fail_cnt = 0;
pf->link_status_ms_mask &= ~PSIMSGSR_MS(i);
dev_warn_ratelimited(&si->pdev->dev,
"Clear VF%d's link status MS bit\n", i);
}
unlock:
spin_unlock(&si->gen_lock);
}
EXPORT_SYMBOL_GPL(enetc_pf_send_link_status_msg);
static void enetc_pf_notify_vf_link_status(struct enetc_pf *pf,
bool link_up)
{
struct enetc_si *si = pf->si;
if (!pf->total_vfs || is_enetc_rev1(si))
return;
spin_lock(&si->gen_lock);
pf->link_up = link_up;
if (!pf->link_status_ms_mask) {
spin_unlock(&si->gen_lock);
return;
}
spin_unlock(&si->gen_lock);
queue_work(si->workqueue, &pf->link_status_task);
}
void enetc_pf_notify_vf_link_up(struct enetc_pf *pf)
{
enetc_pf_notify_vf_link_status(pf, true);
}
EXPORT_SYMBOL_GPL(enetc_pf_notify_vf_link_up);
void enetc_pf_notify_vf_link_down(struct enetc_pf *pf)
{
enetc_pf_notify_vf_link_status(pf, false);
}
EXPORT_SYMBOL_GPL