]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c
nfp: turn NSP port entry into a union
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / netronome / nfp / nfpcore / nfp_nsp_eth.c
index 07b4ded0151459541e113a3b48c2134d1bbbf26e..ca5c041e64a46aa6e72fbb554a55f8105cd4dfdc 100644 (file)
 #include <linux/module.h>
 
 #include "nfp.h"
-#include "nfp_nsp_eth.h"
+#include "nfp_nsp.h"
 #include "nfp6000/nfp6000.h"
 
 #define NSP_ETH_NBI_PORT_COUNT         24
 #define NSP_ETH_MAX_COUNT              (2 * NSP_ETH_NBI_PORT_COUNT)
 #define NSP_ETH_TABLE_SIZE             (NSP_ETH_MAX_COUNT *            \
-                                        sizeof(struct eth_table_entry))
+                                        sizeof(union eth_table_entry))
 
 #define NSP_ETH_PORT_LANES             GENMASK_ULL(3, 0)
 #define NSP_ETH_PORT_INDEX             GENMASK_ULL(15, 8)
 #define NSP_ETH_CTRL_TX_ENABLED                BIT_ULL(2)
 #define NSP_ETH_CTRL_RX_ENABLED                BIT_ULL(3)
 
+enum nfp_eth_raw {
+       NSP_ETH_RAW_PORT = 0,
+       NSP_ETH_RAW_STATE,
+       NSP_ETH_RAW_MAC,
+       NSP_ETH_RAW_CONTROL,
+
+       NSP_ETH_NUM_RAW
+};
+
 enum nfp_eth_rate {
        RATE_INVALID = 0,
        RATE_10M,
@@ -80,12 +89,15 @@ enum nfp_eth_rate {
        RATE_25G,
 };
 
-struct eth_table_entry {
-       __le64 port;
-       __le64 state;
-       u8 mac_addr[6];
-       u8 resv[2];
-       __le64 control;
+union eth_table_entry {
+       struct {
+               __le64 port;
+               __le64 state;
+               u8 mac_addr[6];
+               u8 resv[2];
+               __le64 control;
+       };
+       __le64 raw[NSP_ETH_NUM_RAW];
 };
 
 static unsigned int nfp_eth_rate(enum nfp_eth_rate rate)
@@ -114,7 +126,7 @@ static void nfp_eth_copy_mac_reverse(u8 *dst, const u8 *src)
 }
 
 static void
-nfp_eth_port_translate(struct nfp_nsp *nsp, const struct eth_table_entry *src,
+nfp_eth_port_translate(struct nfp_nsp *nsp, const union eth_table_entry *src,
                       unsigned int index, struct nfp_eth_table_port *dst)
 {
        unsigned int rate;
@@ -216,7 +228,7 @@ struct nfp_eth_table *nfp_eth_read_ports(struct nfp_cpp *cpp)
 struct nfp_eth_table *
 __nfp_eth_read_ports(struct nfp_cpp *cpp, struct nfp_nsp *nsp)
 {
-       struct eth_table_entry *entries;
+       union eth_table_entry *entries;
        struct nfp_eth_table *table;
        int i, j, ret, cnt = 0;
 
@@ -268,63 +280,115 @@ err:
        return NULL;
 }
 
-/**
- * nfp_eth_set_mod_enable() - set PHY module enable control bit
- * @cpp:       NFP CPP handle
- * @idx:       NFP chip-wide port index
- * @enable:    Desired state
- *
- * Enable or disable PHY module (this usually means setting the TX lanes
- * disable bits).
- *
- * Return: 0 or -ERRNO.
- */
-int nfp_eth_set_mod_enable(struct nfp_cpp *cpp, unsigned int idx, bool enable)
+struct nfp_nsp *nfp_eth_config_start(struct nfp_cpp *cpp, unsigned int idx)
 {
-       struct eth_table_entry *entries;
+       union eth_table_entry *entries;
        struct nfp_nsp *nsp;
-       u64 reg;
        int ret;
 
        entries = kzalloc(NSP_ETH_TABLE_SIZE, GFP_KERNEL);
        if (!entries)
-               return -ENOMEM;
+               return ERR_PTR(-ENOMEM);
 
        nsp = nfp_nsp_open(cpp);
        if (IS_ERR(nsp)) {
                kfree(entries);
-               return PTR_ERR(nsp);
+               return nsp;
        }
 
        ret = nfp_nsp_read_eth_table(nsp, entries, NSP_ETH_TABLE_SIZE);
        if (ret < 0) {
                nfp_err(cpp, "reading port table failed %d\n", ret);
-               goto exit_close_nsp;
+               goto err;
        }
 
        if (!(entries[idx].port & NSP_ETH_PORT_LANES_MASK)) {
                nfp_warn(cpp, "trying to set port state on disabled port %d\n",
                         idx);
-               ret = -EINVAL;
-               goto exit_close_nsp;
+               goto err;
        }
 
-       /* Check if we are already in requested state */
-       reg = le64_to_cpu(entries[idx].state);
-       if (enable == FIELD_GET(NSP_ETH_CTRL_ENABLED, reg)) {
-               ret = 0;
-               goto exit_close_nsp;
-       }
+       nfp_nsp_config_set_state(nsp, entries, idx);
+       return nsp;
 
-       reg = le64_to_cpu(entries[idx].control);
-       reg &= ~NSP_ETH_CTRL_ENABLED;
-       reg |= FIELD_PREP(NSP_ETH_CTRL_ENABLED, enable);
-       entries[idx].control = cpu_to_le64(reg);
+err:
+       nfp_nsp_close(nsp);
+       kfree(entries);
+       return ERR_PTR(-EIO);
+}
+
+void nfp_eth_config_cleanup_end(struct nfp_nsp *nsp)
+{
+       union eth_table_entry *entries = nfp_nsp_config_entries(nsp);
 
-       ret = nfp_nsp_write_eth_table(nsp, entries, NSP_ETH_TABLE_SIZE);
-exit_close_nsp:
+       nfp_nsp_config_set_modified(nsp, false);
+       nfp_nsp_config_clear_state(nsp);
        nfp_nsp_close(nsp);
        kfree(entries);
+}
+
+/**
+ * nfp_eth_config_commit_end() - perform recorded configuration changes
+ * @nsp:       NFP NSP handle returned from nfp_eth_config_start()
+ *
+ * Perform the configuration which was requested with __nfp_eth_set_*()
+ * helpers and recorded in @nsp state.  If device was already configured
+ * as requested or no __nfp_eth_set_*() operations were made no NSP command
+ * will be performed.
+ *
+ * Return:
+ * 0 - configuration successful;
+ * 1 - no changes were needed;
+ * -ERRNO - configuration failed.
+ */
+int nfp_eth_config_commit_end(struct nfp_nsp *nsp)
+{
+       union eth_table_entry *entries = nfp_nsp_config_entries(nsp);
+       int ret = 1;
+
+       if (nfp_nsp_config_modified(nsp)) {
+               ret = nfp_nsp_write_eth_table(nsp, entries, NSP_ETH_TABLE_SIZE);
+               ret = ret < 0 ? ret : 0;
+       }
+
+       nfp_eth_config_cleanup_end(nsp);
+
+       return ret;
+}
+
+/**
+ * nfp_eth_set_mod_enable() - set PHY module enable control bit
+ * @cpp:       NFP CPP handle
+ * @idx:       NFP chip-wide port index
+ * @enable:    Desired state
+ *
+ * Enable or disable PHY module (this usually means setting the TX lanes
+ * disable bits).
+ *
+ * Return: 0 or -ERRNO.
+ */
+int nfp_eth_set_mod_enable(struct nfp_cpp *cpp, unsigned int idx, bool enable)
+{
+       union eth_table_entry *entries;
+       struct nfp_nsp *nsp;
+       u64 reg;
+
+       nsp = nfp_eth_config_start(cpp, idx);
+       if (IS_ERR(nsp))
+               return PTR_ERR(nsp);
+
+       entries = nfp_nsp_config_entries(nsp);
+
+       /* Check if we are already in requested state */
+       reg = le64_to_cpu(entries[idx].state);
+       if (enable != FIELD_GET(NSP_ETH_CTRL_ENABLED, reg)) {
+               reg = le64_to_cpu(entries[idx].control);
+               reg &= ~NSP_ETH_CTRL_ENABLED;
+               reg |= FIELD_PREP(NSP_ETH_CTRL_ENABLED, enable);
+               entries[idx].control = cpu_to_le64(reg);
+
+               nfp_nsp_config_set_modified(nsp, true);
+       }
 
-       return ret < 0 ? ret : 0;
+       return nfp_eth_config_commit_end(nsp);
 }