Add basic code of PF-VF mailbox implementation to serve the communication
between VF and PF devices.

Signed-off-by: Zaiyu Wang <zaiyuw...@trustnetic.com>
---
 drivers/net/ngbe/base/ngbe_mbx.c  | 338 ++++++++++++++++++++++++++++++
 drivers/net/ngbe/base/ngbe_mbx.h  |  29 +++
 drivers/net/ngbe/base/ngbe_type.h |   7 +
 drivers/net/ngbe/base/ngbe_vf.c   |  56 +++++
 drivers/net/ngbe/base/ngbe_vf.h   |   1 +
 5 files changed, 431 insertions(+)

diff --git a/drivers/net/ngbe/base/ngbe_mbx.c b/drivers/net/ngbe/base/ngbe_mbx.c
index bc0adbb3ec..a96a4aced7 100644
--- a/drivers/net/ngbe/base/ngbe_mbx.c
+++ b/drivers/net/ngbe/base/ngbe_mbx.c
@@ -109,6 +109,344 @@ s32 ngbe_check_for_rst(struct ngbe_hw *hw, u16 mbx_id)
        return ret_val;
 }
 
+/**
+ *  ngbe_poll_for_msg - Wait for message notification
+ *  @hw: pointer to the HW structure
+ *  @mbx_id: id of mailbox to write
+ *
+ *  returns SUCCESS if it successfully received a message notification
+ **/
+STATIC s32 ngbe_poll_for_msg(struct ngbe_hw *hw, u16 mbx_id)
+{
+       struct ngbe_mbx_info *mbx = &hw->mbx;
+       int countdown = mbx->timeout;
+
+       if (!countdown || !mbx->check_for_msg)
+               goto out;
+
+       while (countdown && mbx->check_for_msg(hw, mbx_id)) {
+               countdown--;
+               if (!countdown)
+                       break;
+               usec_delay(mbx->usec_delay);
+       }
+
+       if (countdown == 0)
+               DEBUGOUT("Polling for VF%d mailbox message timedout", mbx_id);
+
+out:
+       return countdown ? 0 : NGBE_ERR_MBX;
+}
+
+/**
+ *  ngbe_poll_for_ack - Wait for message acknowledgment
+ *  @hw: pointer to the HW structure
+ *  @mbx_id: id of mailbox to write
+ *
+ *  returns SUCCESS if it successfully received a message acknowledgment
+ **/
+STATIC s32 ngbe_poll_for_ack(struct ngbe_hw *hw, u16 mbx_id)
+{
+       struct ngbe_mbx_info *mbx = &hw->mbx;
+       int countdown = mbx->timeout;
+
+       if (!countdown || !mbx->check_for_ack)
+               goto out;
+
+       while (countdown && mbx->check_for_ack(hw, mbx_id)) {
+               countdown--;
+               if (!countdown)
+                       break;
+               usec_delay(mbx->usec_delay);
+       }
+
+       if (countdown == 0)
+               DEBUGOUT("Polling for VF%d mailbox ack timedout", mbx_id);
+
+out:
+       return countdown ? 0 : NGBE_ERR_MBX;
+}
+
+/**
+ *  ngbe_read_posted_mbx - Wait for message notification and receive message
+ *  @hw: pointer to the HW structure
+ *  @msg: The message buffer
+ *  @size: Length of buffer
+ *  @mbx_id: id of mailbox to write
+ *
+ *  returns SUCCESS if it successfully received a message notification and
+ *  copied it into the receive buffer.
+ **/
+s32 ngbe_read_posted_mbx(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
+{
+       struct ngbe_mbx_info *mbx = &hw->mbx;
+       s32 ret_val = NGBE_ERR_MBX;
+
+       if (!mbx->read)
+               goto out;
+
+       ret_val = ngbe_poll_for_msg(hw, mbx_id);
+
+       /* if ack received read message, otherwise we timed out */
+       if (!ret_val)
+               ret_val = mbx->read(hw, msg, size, mbx_id);
+out:
+       return ret_val;
+}
+
+/**
+ *  ngbe_write_posted_mbx - Write a message to the mailbox, wait for ack
+ *  @hw: pointer to the HW structure
+ *  @msg: The message buffer
+ *  @size: Length of buffer
+ *  @mbx_id: id of mailbox to write
+ *
+ *  returns SUCCESS if it successfully copied message into the buffer and
+ *  received an ack to that message within delay * timeout period
+ **/
+s32 ngbe_write_posted_mbx(struct ngbe_hw *hw, u32 *msg, u16 size,
+                          u16 mbx_id)
+{
+       struct ngbe_mbx_info *mbx = &hw->mbx;
+       s32 ret_val = NGBE_ERR_MBX;
+
+       /* exit if either we can't write or there isn't a defined timeout */
+       if (!mbx->write || !mbx->timeout)
+               goto out;
+
+       /* send msg */
+       ret_val = mbx->write(hw, msg, size, mbx_id);
+
+       /* if msg sent wait until we receive an ack */
+       if (!ret_val)
+               ret_val = ngbe_poll_for_ack(hw, mbx_id);
+out:
+       return ret_val;
+}
+
+/**
+ *  ngbe_read_v2p_mailbox - read v2p mailbox
+ *  @hw: pointer to the HW structure
+ *
+ *  This function is used to read the v2p mailbox without losing the read to
+ *  clear status bits.
+ **/
+STATIC u32 ngbe_read_v2p_mailbox(struct ngbe_hw *hw)
+{
+       u32 v2p_mailbox = rd32(hw, NGBE_VFMBCTL);
+
+       v2p_mailbox |= hw->mbx.v2p_mailbox;
+       hw->mbx.v2p_mailbox |= v2p_mailbox & NGBE_VFMBCTL_R2C_BITS;
+
+       return v2p_mailbox;
+}
+
+/**
+ *  ngbe_check_for_bit_vf - Determine if a status bit was set
+ *  @hw: pointer to the HW structure
+ *  @mask: bitmask for bits to be tested and cleared
+ *
+ *  This function is used to check for the read to clear bits within
+ *  the V2P mailbox.
+ **/
+STATIC s32 ngbe_check_for_bit_vf(struct ngbe_hw *hw, u32 mask)
+{
+       u32 v2p_mailbox = ngbe_read_v2p_mailbox(hw);
+       s32 ret_val = NGBE_ERR_MBX;
+
+       if (v2p_mailbox & mask)
+               ret_val = 0;
+
+       hw->mbx.v2p_mailbox &= ~mask;
+
+       return ret_val;
+}
+
+/**
+ *  ngbe_check_for_msg_vf - checks to see if the PF has sent mail
+ *  @hw: pointer to the HW structure
+ *  @mbx_id: id of mailbox to check
+ *
+ *  returns SUCCESS if the PF has set the Status bit or else ERR_MBX
+ **/
+s32 ngbe_check_for_msg_vf(struct ngbe_hw *hw, u16 mbx_id)
+{
+       s32 ret_val = NGBE_ERR_MBX;
+
+       UNREFERENCED_PARAMETER(mbx_id);
+
+       if (!ngbe_check_for_bit_vf(hw, NGBE_VFMBCTL_PFSTS)) {
+               ret_val = 0;
+               hw->mbx.stats.reqs++;
+       }
+
+       return ret_val;
+}
+
+/**
+ *  ngbe_check_for_ack_vf - checks to see if the PF has ACK'd
+ *  @hw: pointer to the HW structure
+ *  @mbx_id: id of mailbox to check
+ *
+ *  returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
+ **/
+s32 ngbe_check_for_ack_vf(struct ngbe_hw *hw, u16 mbx_id)
+{
+       s32 ret_val = NGBE_ERR_MBX;
+
+       UNREFERENCED_PARAMETER(mbx_id);
+
+       if (!ngbe_check_for_bit_vf(hw, NGBE_VFMBCTL_PFACK)) {
+               ret_val = 0;
+               hw->mbx.stats.acks++;
+       }
+
+       return ret_val;
+}
+
+/**
+ *  ngbe_check_for_rst_vf - checks to see if the PF has reset
+ *  @hw: pointer to the HW structure
+ *  @mbx_id: id of mailbox to check
+ *
+ *  returns true if the PF has set the reset done bit or else false
+ **/
+s32 ngbe_check_for_rst_vf(struct ngbe_hw *hw, u16 mbx_id)
+{
+       s32 ret_val = NGBE_ERR_MBX;
+
+       UNREFERENCED_PARAMETER(mbx_id);
+
+       if (!ngbe_check_for_bit_vf(hw, (NGBE_VFMBCTL_RSTD |
+           NGBE_VFMBCTL_RSTI))) {
+               ret_val = 0;
+               hw->mbx.stats.rsts++;
+       }
+
+       return ret_val;
+}
+
+/**
+ *  ngbe_obtain_mbx_lock_vf - obtain mailbox lock
+ *  @hw: pointer to the HW structure
+ *
+ *  return SUCCESS if we obtained the mailbox lock
+ **/
+STATIC s32 ngbe_obtain_mbx_lock_vf(struct ngbe_hw *hw)
+{
+       s32 ret_val = NGBE_ERR_MBX;
+
+       /* Take ownership of the buffer */
+       wr32(hw, NGBE_VFMBCTL, NGBE_VFMBCTL_VFU);
+
+       /* reserve mailbox for vf use */
+       if (ngbe_read_v2p_mailbox(hw) & NGBE_VFMBCTL_VFU)
+               ret_val = 0;
+
+       return ret_val;
+}
+
+/**
+ *  ngbe_write_mbx_vf - Write a message to the mailbox
+ *  @hw: pointer to the HW structure
+ *  @msg: The message buffer
+ *  @size: Length of buffer
+ *  @mbx_id: id of mailbox to write
+ *
+ *  returns SUCCESS if it successfully copied message into the buffer
+ **/
+s32 ngbe_write_mbx_vf(struct ngbe_hw *hw, u32 *msg, u16 size,
+                             u16 mbx_id)
+{
+       s32 ret_val;
+       u16 i;
+
+       UNREFERENCED_PARAMETER(mbx_id);
+
+       /* lock the mailbox to prevent pf/vf race condition */
+       ret_val = ngbe_obtain_mbx_lock_vf(hw);
+       if (ret_val)
+               goto out_no_write;
+
+       /* flush msg and acks as we are overwriting the message buffer */
+       ngbe_check_for_msg_vf(hw, 0);
+       ngbe_check_for_ack_vf(hw, 0);
+
+       /* copy the caller specified message to the mailbox memory buffer */
+       for (i = 0; i < size; i++)
+               wr32a(hw, NGBE_VFMBX, i, msg[i]);
+
+       /* update stats */
+       hw->mbx.stats.msgs_tx++;
+
+       /* Drop VFU and interrupt the PF to tell it a message has been sent */
+       wr32(hw, NGBE_VFMBCTL, NGBE_VFMBCTL_REQ);
+
+out_no_write:
+       return ret_val;
+}
+
+/**
+ *  ngbe_read_mbx_vf - Reads a message from the inbox intended for vf
+ *  @hw: pointer to the HW structure
+ *  @msg: The message buffer
+ *  @size: Length of buffer
+ *  @mbx_id: id of mailbox to read
+ *
+ *  returns SUCCESS if it successfully read message from buffer
+ **/
+s32 ngbe_read_mbx_vf(struct ngbe_hw *hw, u32 *msg, u16 size,
+                            u16 mbx_id)
+{
+       s32 ret_val = 0;
+       u16 i;
+
+       UNREFERENCED_PARAMETER(mbx_id);
+
+       /* lock the mailbox to prevent pf/vf race condition */
+       ret_val = ngbe_obtain_mbx_lock_vf(hw);
+       if (ret_val)
+               goto out_no_read;
+
+       /* copy the message from the mailbox memory buffer */
+       for (i = 0; i < size; i++)
+               msg[i] = rd32a(hw, NGBE_VFMBX, i);
+
+       /* Acknowledge receipt and release mailbox, then we're done */
+       wr32(hw, NGBE_VFMBCTL, NGBE_VFMBCTL_ACK);
+
+       /* update stats */
+       hw->mbx.stats.msgs_rx++;
+
+out_no_read:
+       return ret_val;
+}
+
+/**
+ *  ngbe_init_mbx_params_vf - set initial values for vf mailbox
+ *  @hw: pointer to the HW structure
+ *
+ *  Initializes the hw->mbx struct to correct values for vf mailbox
+ */
+void ngbe_init_mbx_params_vf(struct ngbe_hw *hw)
+{
+       struct ngbe_mbx_info *mbx = &hw->mbx;
+
+       /* start mailbox as timed out and let the reset_hw call set the timeout
+        * value to begin communications
+        */
+       mbx->timeout = 0;
+       mbx->usec_delay = NGBE_VF_MBX_INIT_DELAY;
+
+       mbx->size = NGBE_P2VMBX_SIZE;
+
+       mbx->stats.msgs_tx = 0;
+       mbx->stats.msgs_rx = 0;
+       mbx->stats.reqs = 0;
+       mbx->stats.acks = 0;
+       mbx->stats.rsts = 0;
+}
+
 STATIC s32 ngbe_check_for_bit_pf(struct ngbe_hw *hw, u32 mask)
 {
        u32 mbvficr = rd32(hw, NGBE_MBVFICR);
diff --git a/drivers/net/ngbe/base/ngbe_mbx.h b/drivers/net/ngbe/base/ngbe_mbx.h
index d47da2718c..83561fd4cf 100644
--- a/drivers/net/ngbe/base/ngbe_mbx.h
+++ b/drivers/net/ngbe/base/ngbe_mbx.h
@@ -73,11 +73,34 @@ enum ngbevf_xcast_modes {
 
 /* length of permanent address message returned from PF */
 #define NGBE_VF_PERMADDR_MSG_LEN       4
+/* word in permanent address message with the current multicast type */
+#define NGBE_VF_MC_TYPE_WORD           3
+
+#define NGBE_PF_CONTROL_MSG            0x0100 /* PF control message */
+
+/* mailbox API, version 2.0 VF requests */
+#define NGBE_VF_API_NEGOTIATE          0x08 /* negotiate API version */
+#define NGBE_VF_GET_QUEUES             0x09 /* get queue configuration */
+#define NGBE_VF_ENABLE_MACADDR         0x0A /* enable MAC address */
+#define NGBE_VF_DISABLE_MACADDR        0x0B /* disable MAC address */
+#define NGBE_VF_GET_MACADDRS           0x0C /* get all configured MAC addrs */
+#define NGBE_VF_SET_MCAST_PROMISC      0x0D /* enable multicast promiscuous */
+#define NGBE_VF_GET_MTU                0x0E /* get bounds on MTU */
+#define NGBE_VF_SET_MTU                0x0F /* set a specific MTU */
+
+/* mailbox API, version 2.0 PF requests */
+#define NGBE_PF_TRANSPARENT_VLAN       0x0101 /* enable transparent vlan */
+
+#define NGBE_VF_MBX_INIT_TIMEOUT       2000 /* number of retries on mailbox */
+#define NGBE_VF_MBX_INIT_DELAY         500  /* microseconds between retries */
 s32 ngbe_read_mbx(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id);
 s32 ngbe_write_mbx(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id);
+s32 ngbe_read_posted_mbx(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id);
+s32 ngbe_write_posted_mbx(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id);
 s32 ngbe_check_for_msg(struct ngbe_hw *hw, u16 mbx_id);
 s32 ngbe_check_for_ack(struct ngbe_hw *hw, u16 mbx_id);
 s32 ngbe_check_for_rst(struct ngbe_hw *hw, u16 mbx_id);
+void ngbe_init_mbx_params_vf(struct ngbe_hw *hw);
 void ngbe_init_mbx_params_pf(struct ngbe_hw *hw);
 
 s32 ngbe_read_mbx_pf(struct ngbe_hw *hw, u32 *msg, u16 size, u16 vf_number);
@@ -86,4 +109,10 @@ s32 ngbe_check_for_msg_pf(struct ngbe_hw *hw, u16 
vf_number);
 s32 ngbe_check_for_ack_pf(struct ngbe_hw *hw, u16 vf_number);
 s32 ngbe_check_for_rst_pf(struct ngbe_hw *hw, u16 vf_number);
 
+s32 ngbe_read_mbx_vf(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id);
+s32 ngbe_write_mbx_vf(struct ngbe_hw *hw, u32 *msg, u16 size, u16 mbx_id);
+s32 ngbe_check_for_msg_vf(struct ngbe_hw *hw, u16 mbx_id);
+s32 ngbe_check_for_ack_vf(struct ngbe_hw *hw, u16 mbx_id);
+s32 ngbe_check_for_rst_vf(struct ngbe_hw *hw, u16 mbx_id);
+
 #endif /* _NGBE_MBX_H_ */
diff --git a/drivers/net/ngbe/base/ngbe_type.h 
b/drivers/net/ngbe/base/ngbe_type.h
index a987bbe25b..35ebb7208a 100644
--- a/drivers/net/ngbe/base/ngbe_type.h
+++ b/drivers/net/ngbe/base/ngbe_type.h
@@ -311,6 +311,7 @@ struct ngbe_mac_info {
        s32 (*enable_sec_rx_path)(struct ngbe_hw *hw);
        s32 (*acquire_swfw_sync)(struct ngbe_hw *hw, u32 mask);
        void (*release_swfw_sync)(struct ngbe_hw *hw, u32 mask);
+       s32 (*negotiate_api_version)(struct ngbe_hw *hw, int api);
 
        /* Link */
        s32 (*setup_link)(struct ngbe_hw *hw, u32 speed,
@@ -422,6 +423,10 @@ struct ngbe_mbx_info {
        void (*init_params)(struct ngbe_hw *hw);
        s32  (*read)(struct ngbe_hw *hw, u32 *msg, u16 size, u16 vf_number);
        s32  (*write)(struct ngbe_hw *hw, u32 *msg, u16 size, u16 vf_number);
+       s32  (*read_posted)(struct ngbe_hw *hw, u32 *msg, u16 size,
+                               u16 mbx_id);
+       s32  (*write_posted)(struct ngbe_hw *hw, u32 *msg, u16 size,
+                               u16 mbx_id);
        s32  (*check_for_msg)(struct ngbe_hw *hw, u16 mbx_id);
        s32  (*check_for_ack)(struct ngbe_hw *hw, u16 mbx_id);
        s32  (*check_for_rst)(struct ngbe_hw *hw, u16 mbx_id);
@@ -429,6 +434,7 @@ struct ngbe_mbx_info {
        struct ngbe_mbx_stats stats;
        u32 timeout;
        u32 usec_delay;
+       u32 v2p_mailbox;
        u16 size;
 };
 
@@ -458,6 +464,7 @@ struct ngbe_hw {
        u8 port_id;
        u8 revision_id;
        bool adapter_stopped;
+       int api_version;
        bool wol_enabled;
        bool ncsi_enabled;
        bool lldp_enabled;
diff --git a/drivers/net/ngbe/base/ngbe_vf.c b/drivers/net/ngbe/base/ngbe_vf.c
index 91b8d1bbe2..12aa3bca1a 100644
--- a/drivers/net/ngbe/base/ngbe_vf.c
+++ b/drivers/net/ngbe/base/ngbe_vf.c
@@ -4,8 +4,52 @@
  */
 
 #include "ngbe_type.h"
+#include "ngbe_mbx.h"
 #include "ngbe_vf.h"
 
+STATIC s32 ngbevf_write_msg_read_ack(struct ngbe_hw *hw, u32 *msg,
+                                     u32 *retmsg, u16 size)
+{
+       struct ngbe_mbx_info *mbx = &hw->mbx;
+       s32 retval = mbx->write_posted(hw, msg, size, 0);
+
+       if (retval)
+               return retval;
+
+       return mbx->read_posted(hw, retmsg, size, 0);
+}
+
+/**
+ *  ngbevf_negotiate_api_version - Negotiate supported API version
+ *  @hw: pointer to the HW structure
+ *  @api: integer containing requested API version
+ **/
+int ngbevf_negotiate_api_version(struct ngbe_hw *hw, int api)
+{
+       int err;
+       u32 msg[3];
+
+       /* Negotiate the mailbox API version */
+       msg[0] = NGBE_VF_API_NEGOTIATE;
+       msg[1] = api;
+       msg[2] = 0;
+
+       err = ngbevf_write_msg_read_ack(hw, msg, msg, 3);
+       if (!err) {
+               msg[0] &= ~NGBE_VT_MSGTYPE_CTS;
+
+               /* Store value and return 0 on success */
+               if (msg[0] == (NGBE_VF_API_NEGOTIATE | NGBE_VT_MSGTYPE_ACK)) {
+                       hw->api_version = api;
+                       return 0;
+               }
+
+               err = NGBE_ERR_INVALID_ARGUMENT;
+       }
+
+       return err;
+}
+
 /**
  *  ngbe_init_ops_vf - Initialize the pointers for vf
  *  @hw: pointer to hardware structure
@@ -18,9 +62,21 @@
 s32 ngbe_init_ops_vf(struct ngbe_hw *hw)
 {
        struct ngbe_mac_info *mac = &hw->mac;
+       struct ngbe_mbx_info *mbx = &hw->mbx;
+
+       mac->negotiate_api_version = ngbevf_negotiate_api_version;
 
        mac->max_tx_queues = 1;
        mac->max_rx_queues = 1;
 
+       mbx->init_params = ngbe_init_mbx_params_vf;
+       mbx->read = ngbe_read_mbx_vf;
+       mbx->write = ngbe_write_mbx_vf;
+       mbx->read_posted = ngbe_read_posted_mbx;
+       mbx->write_posted = ngbe_write_posted_mbx;
+       mbx->check_for_msg = ngbe_check_for_msg_vf;
+       mbx->check_for_ack = ngbe_check_for_ack_vf;
+       mbx->check_for_rst = ngbe_check_for_rst_vf;
+
        return 0;
 }
diff --git a/drivers/net/ngbe/base/ngbe_vf.h b/drivers/net/ngbe/base/ngbe_vf.h
index 7982ea231e..23e5b0d8a3 100644
--- a/drivers/net/ngbe/base/ngbe_vf.h
+++ b/drivers/net/ngbe/base/ngbe_vf.h
@@ -9,5 +9,6 @@
 #include "ngbe_type.h"
 
 s32 ngbe_init_ops_vf(struct ngbe_hw *hw);
+int ngbevf_negotiate_api_version(struct ngbe_hw *hw, int api);
 
 #endif /* __NGBE_VF_H__ */
-- 
2.21.0.windows.1

Reply via email to