net/bnxt: fix assignment instead of comparison
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_utils.c
index e1d6528..a13a3bb 100644 (file)
@@ -273,6 +273,69 @@ ulp_blob_push(struct ulp_blob *blob,
        return datalen;
 }
 
+/*
+ * Insert data into the binary blob at the given offset.
+ *
+ * blob [in] The blob that data is added to.  The blob must
+ * be initialized prior to pushing data.
+ *
+ * offset [in] The offset where the data needs to be inserted.
+ *
+ * data [in/out] A pointer to bytes to be added to the blob.
+ *
+ * datalen [in] The number of bits to be added to the blob.
+ *
+ * The offset of the data is updated after each push of data.
+ * NULL returned on error.
+ */
+uint32_t
+ulp_blob_insert(struct ulp_blob *blob, uint32_t offset,
+               uint8_t *data, uint32_t datalen)
+{
+       uint32_t rc;
+       uint8_t local_data[BNXT_ULP_FLMP_BLOB_SIZE];
+       uint16_t mov_len;
+
+       /* validate the arguments */
+       if (!blob || datalen > (uint32_t)(blob->bitlen - blob->write_idx) ||
+           offset > blob->write_idx) {
+               BNXT_TF_DBG(ERR, "invalid argument\n");
+               return 0; /* failure */
+       }
+
+       mov_len = blob->write_idx - offset;
+       /* If offset and data len are not 8 bit aligned then return error */
+       if (ULP_BITS_IS_BYTE_NOT_ALIGNED(offset) ||
+           ULP_BITS_IS_BYTE_NOT_ALIGNED(datalen)) {
+               BNXT_TF_DBG(ERR, "invalid argument, not aligned\n");
+               return 0; /* failure */
+       }
+
+       /* copy the data so we can move the data */
+       memcpy(local_data, &blob->data[ULP_BITS_2_BYTE_NR(offset)],
+              ULP_BITS_2_BYTE(mov_len));
+       blob->write_idx = offset;
+       if (blob->byte_order == BNXT_ULP_BYTE_ORDER_BE)
+               rc = ulp_bs_push_msb(blob->data,
+                                    blob->write_idx,
+                                    datalen,
+                                    data);
+       else
+               rc = ulp_bs_push_lsb(blob->data,
+                                    blob->write_idx,
+                                    datalen,
+                                    data);
+       if (!rc) {
+               BNXT_TF_DBG(ERR, "Failed ro write blob\n");
+               return 0;
+       }
+       /* copy the previously stored data */
+       memcpy(&blob->data[ULP_BITS_2_BYTE_NR(offset + datalen)], local_data,
+              ULP_BITS_2_BYTE(mov_len));
+       blob->write_idx += (mov_len + datalen);
+       return datalen;
+}
+
 /*
  * Add data to the binary blob at the current offset.
  *
@@ -309,6 +372,40 @@ ulp_blob_push_64(struct ulp_blob *blob,
        return &val[8 - size];
 }
 
+/*
+ * Add data to the binary blob at the current offset.
+ *
+ * blob [in] The blob that data is added to.  The blob must
+ * be initialized prior to pushing data.
+ *
+ * data [in] 32-bit value to be added to the blob.
+ *
+ * datalen [in] The number of bits to be added ot the blob.
+ *
+ * The offset of the data is updated after each push of data.
+ * NULL returned on error, pointer pushed value otherwise.
+ */
+uint8_t *
+ulp_blob_push_32(struct ulp_blob *blob,
+                uint32_t *data,
+                uint32_t datalen)
+{
+       uint8_t *val = (uint8_t *)data;
+       uint32_t rc;
+       uint32_t size = ULP_BITS_2_BYTE(datalen);
+
+       if (!data || size > sizeof(uint32_t)) {
+               BNXT_TF_DBG(ERR, "invalid argument\n");
+               return 0;
+       }
+
+       rc = ulp_blob_push(blob, &val[sizeof(uint32_t) - size], datalen);
+       if (!rc)
+               return 0;
+
+       return &val[sizeof(uint32_t) - size];
+}
+
 /*
  * Add encap data to the binary blob at the current offset.
  *
@@ -369,9 +466,9 @@ ulp_blob_push_encap(struct ulp_blob *blob,
  *
  * datalen [in] The number of bits of pad to add
  *
- * returns the number of pad bits added, zero on failure
+ * returns the number of pad bits added, -1 on failure
  */
-uint32_t
+int32_t
 ulp_blob_pad_push(struct ulp_blob *blob,
                  uint32_t datalen)
 {
@@ -384,6 +481,82 @@ ulp_blob_pad_push(struct ulp_blob *blob,
        return datalen;
 }
 
+/* Get data from src and put into dst using little-endian format */
+static void
+ulp_bs_get_lsb(uint8_t *src, uint16_t bitpos, uint8_t bitlen, uint8_t *dst)
+{
+       uint8_t bitoffs = bitpos % ULP_BLOB_BYTE;
+       uint16_t index  = ULP_BITS_2_BYTE_NR(bitpos);
+       uint8_t mask, partial, shift;
+
+       shift = bitoffs;
+       partial = ULP_BLOB_BYTE - bitoffs;
+       if (bitoffs + bitlen <= ULP_BLOB_BYTE) {
+               mask = ((1 << bitlen) - 1) << shift;
+               *dst = (src[index] & mask) >> shift;
+       } else {
+               mask = ((1 << partial) - 1) << shift;
+               *dst = (src[index] & mask) >> shift;
+               index++;
+               partial = bitlen - partial;
+               mask = ((1 << partial) - 1);
+               *dst |= (src[index] & mask) << (ULP_BLOB_BYTE - bitoffs);
+       }
+}
+
+/* Assuming that src is in little-Endian Format */
+static void
+ulp_bs_pull_lsb(uint8_t *src, uint8_t *dst, uint32_t size,
+               uint32_t offset, uint32_t len)
+{
+       uint32_t idx;
+       uint32_t cnt = ULP_BITS_2_BYTE_NR(len);
+
+       /* iterate bytewise to get data */
+       for (idx = 0; idx < cnt; idx++) {
+               ulp_bs_get_lsb(src, offset, ULP_BLOB_BYTE,
+                              &dst[size - 1 - idx]);
+               offset += ULP_BLOB_BYTE;
+               len -= ULP_BLOB_BYTE;
+       }
+
+       /* Extract the last reminder data that is not 8 byte boundary */
+       if (len)
+               ulp_bs_get_lsb(src, offset, len, &dst[size - 1 - idx]);
+}
+
+/*
+ * Extract data from the binary blob using given offset.
+ *
+ * blob [in] The blob that data is extracted from. The blob must
+ * be initialized prior to pulling data.
+ *
+ * data [in] A pointer to put the data.
+ * data_size [in] size of the data buffer in bytes.
+ *offset [in] - Offset in the blob to extract the data in bits format.
+ * len [in] The number of bits to be pulled from the blob.
+ *
+ * Output: zero on success, -1 on failure
+ */
+int32_t
+ulp_blob_pull(struct ulp_blob *blob, uint8_t *data, uint32_t data_size,
+             uint16_t offset, uint16_t len)
+{
+       /* validate the arguments */
+       if (!blob || (offset + len) > blob->bitlen ||
+           ULP_BYTE_2_BITS(data_size) < len) {
+               BNXT_TF_DBG(ERR, "invalid argument\n");
+               return -1; /* failure */
+       }
+
+       if (blob->byte_order == BNXT_ULP_BYTE_ORDER_BE) {
+               BNXT_TF_DBG(ERR, "Big endian pull not implemented\n");
+               return -1; /* failure */
+       }
+       ulp_bs_pull_lsb(blob->data, data, data_size, offset, len);
+       return 0;
+}
+
 /*
  * Get the data portion of the binary blob.
  *
@@ -436,17 +609,21 @@ ulp_blob_encap_swap_idx_set(struct ulp_blob *blob)
 void
 ulp_blob_perform_encap_swap(struct ulp_blob *blob)
 {
-       uint32_t                i, idx = 0, end_idx = 0;
-       uint8_t         temp_val_1, temp_val_2;
+       uint32_t i, idx = 0, end_idx = 0, roundoff;
+       uint8_t temp_val_1, temp_val_2;
 
        /* validate the arguments */
        if (!blob) {
                BNXT_TF_DBG(ERR, "invalid argument\n");
                return; /* failure */
        }
-       idx = ULP_BITS_2_BYTE_NR(blob->encap_swap_idx + 1);
+       idx = ULP_BITS_2_BYTE_NR(blob->encap_swap_idx);
        end_idx = ULP_BITS_2_BYTE(blob->write_idx);
-
+       roundoff = ULP_BYTE_2_BITS(ULP_BITS_2_BYTE(end_idx));
+       if (roundoff > end_idx) {
+               blob->write_idx += ULP_BYTE_2_BITS(roundoff - end_idx);
+               end_idx = roundoff;
+       }
        while (idx <= end_idx) {
                for (i = 0; i < 4; i = i + 2) {
                        temp_val_1 = blob->data[idx + i];
@@ -460,6 +637,97 @@ ulp_blob_perform_encap_swap(struct ulp_blob *blob)
        }
 }
 
+/*
+ * Perform the blob buffer reversal byte wise.
+ * This api makes the first byte the last and
+ * vice-versa.
+ *
+ * blob [in] The blob's data to be used for swap.
+ *
+ * returns void.
+ */
+void
+ulp_blob_perform_byte_reverse(struct ulp_blob *blob)
+{
+       uint32_t idx = 0, num = 0;
+       uint8_t xchar;
+
+       /* validate the arguments */
+       if (!blob) {
+               BNXT_TF_DBG(ERR, "invalid argument\n");
+               return; /* failure */
+       }
+
+       num = ULP_BITS_2_BYTE_NR(blob->write_idx);
+       for (idx = 0; idx < (num / 2); idx++) {
+               xchar = blob->data[idx];
+               blob->data[idx] = blob->data[(num - 1) - idx];
+               blob->data[(num - 1) - idx] = xchar;
+       }
+}
+
+/*
+ * Perform the blob buffer 64 bit word swap.
+ * This api makes the first 4 bytes the last in
+ * a given 64 bit value and vice-versa.
+ *
+ * blob [in] The blob's data to be used for swap.
+ *
+ * returns void.
+ */
+void
+ulp_blob_perform_64B_word_swap(struct ulp_blob *blob)
+{
+       uint32_t i, j, num;
+       uint8_t xchar;
+       uint32_t word_size = ULP_64B_IN_BYTES / 2;
+
+       /* validate the arguments */
+       if (!blob) {
+               BNXT_TF_DBG(ERR, "invalid argument\n");
+               return; /* failure */
+       }
+       num = ULP_BITS_2_BYTE(blob->write_idx);
+       for (i = 0; i < num; i = i + ULP_64B_IN_BYTES) {
+               for (j = 0; j < word_size; j++) {
+                       xchar = blob->data[i + j];
+                       blob->data[i + j] = blob->data[i + j + word_size];
+                       blob->data[i + j + word_size] = xchar;
+               }
+       }
+}
+
+/*
+ * Perform the blob buffer 64 bit byte swap.
+ * This api makes the first byte the last in
+ * a given 64 bit value and vice-versa.
+ *
+ * blob [in] The blob's data to be used for swap.
+ *
+ * returns void.
+ */
+void
+ulp_blob_perform_64B_byte_swap(struct ulp_blob *blob)
+{
+       uint32_t i, j, num;
+       uint8_t xchar;
+       uint32_t offset = ULP_64B_IN_BYTES - 1;
+
+       /* validate the arguments */
+       if (!blob) {
+               BNXT_TF_DBG(ERR, "invalid argument\n");
+               return; /* failure */
+       }
+       num = ULP_BITS_2_BYTE(blob->write_idx);
+       for (i = 0; i < num; i = i + ULP_64B_IN_BYTES) {
+               for (j = 0; j < (ULP_64B_IN_BYTES / 2); j++) {
+                       xchar = blob->data[i + j];
+                       blob->data[i + j] = blob->data[i + offset - j];
+                       blob->data[i + offset - j] = xchar;
+               }
+       }
+}
+
 /*
  * Read data from the operand
  *
@@ -492,20 +760,35 @@ ulp_operand_read(uint8_t *operand,
  * dst [out] The destination buffer
  * src [in] The source buffer dst
  * size[in] size of the buffer.
+ * align[in] The alignment is either 8 or 16.
  */
 void
 ulp_encap_buffer_copy(uint8_t *dst,
                      const uint8_t *src,
-                     uint16_t size)
+                     uint16_t size,
+                     uint16_t align)
 {
-       uint16_t        idx = 0;
-
-       /* copy 2 bytes at a time. Write MSB to LSB */
-       while ((idx + sizeof(uint16_t)) <= size) {
-               memcpy(&dst[idx], &src[size - idx - sizeof(uint16_t)],
-                      sizeof(uint16_t));
-               idx += sizeof(uint16_t);
-       }
+       uint16_t        idx, tmp_size = 0;
+
+       do {
+               dst += tmp_size;
+               src += tmp_size;
+               idx = 0;
+               if (size > align) {
+                       tmp_size = align;
+                       size -= align;
+               } else {
+                       tmp_size = size;
+                       size = 0;
+               }
+               /* copy 2 bytes at a time. Write MSB to LSB */
+               while ((idx + sizeof(uint16_t)) <= tmp_size) {
+                       memcpy(&dst[idx],
+                              &src[tmp_size - idx - sizeof(uint16_t)],
+                              sizeof(uint16_t));
+                       idx += sizeof(uint16_t);
+               }
+       } while (size);
 }
 
 /*
@@ -519,3 +802,36 @@ int32_t ulp_buffer_is_empty(const uint8_t *buf, uint32_t size)
 {
        return buf[0] == 0 && !memcmp(buf, buf + 1, size - 1);
 }
+
+/* Function to check if bitmap is zero.Return 1 on success */
+uint32_t ulp_bitmap_is_zero(uint8_t *bitmap, int32_t size)
+{
+       while (size-- > 0) {
+               if (*bitmap != 0)
+                       return 0;
+               bitmap++;
+       }
+       return 1;
+}
+
+/* Function to check if bitmap is ones. Return 1 on success */
+uint32_t ulp_bitmap_is_ones(uint8_t *bitmap, int32_t size)
+{
+       while (size-- > 0) {
+               if (*bitmap != 0xFF)
+                       return 0;
+               bitmap++;
+       }
+       return 1;
+}
+
+/* Function to check if bitmap is not zero. Return 1 on success */
+uint32_t ulp_bitmap_notzero(uint8_t *bitmap, int32_t size)
+{
+       while (size-- > 0) {
+               if (*bitmap != 0)
+                       return 1;
+               bitmap++;
+       }
+       return 0;
+}