xref: /openbmc/linux/drivers/net/ethernet/huawei/hinic/hinic_common.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*2025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
253e7d6feSAviad Krawczyk /*
353e7d6feSAviad Krawczyk  * Huawei HiNIC PCI Express Linux driver
453e7d6feSAviad Krawczyk  * Copyright(c) 2017 Huawei Technologies Co., Ltd
553e7d6feSAviad Krawczyk  */
653e7d6feSAviad Krawczyk 
776baca2eSAviad Krawczyk #include <linux/kernel.h>
853e7d6feSAviad Krawczyk #include <linux/types.h>
953e7d6feSAviad Krawczyk #include <asm/byteorder.h>
1053e7d6feSAviad Krawczyk 
1153e7d6feSAviad Krawczyk #include "hinic_common.h"
1253e7d6feSAviad Krawczyk 
1353e7d6feSAviad Krawczyk /**
1453e7d6feSAviad Krawczyk  * hinic_cpu_to_be32 - convert data to big endian 32 bit format
1553e7d6feSAviad Krawczyk  * @data: the data to convert
1653e7d6feSAviad Krawczyk  * @len: length of data to convert
1753e7d6feSAviad Krawczyk  **/
hinic_cpu_to_be32(void * data,int len)1853e7d6feSAviad Krawczyk void hinic_cpu_to_be32(void *data, int len)
1953e7d6feSAviad Krawczyk {
2053e7d6feSAviad Krawczyk 	u32 *mem = data;
2153e7d6feSAviad Krawczyk 	int i;
2253e7d6feSAviad Krawczyk 
2353e7d6feSAviad Krawczyk 	len = len / sizeof(u32);
2453e7d6feSAviad Krawczyk 
2553e7d6feSAviad Krawczyk 	for (i = 0; i < len; i++) {
2653e7d6feSAviad Krawczyk 		*mem = cpu_to_be32(*mem);
2753e7d6feSAviad Krawczyk 		mem++;
2853e7d6feSAviad Krawczyk 	}
2953e7d6feSAviad Krawczyk }
3053e7d6feSAviad Krawczyk 
3153e7d6feSAviad Krawczyk /**
3253e7d6feSAviad Krawczyk  * hinic_be32_to_cpu - convert data from big endian 32 bit format
3353e7d6feSAviad Krawczyk  * @data: the data to convert
3453e7d6feSAviad Krawczyk  * @len: length of data to convert
3553e7d6feSAviad Krawczyk  **/
hinic_be32_to_cpu(void * data,int len)3653e7d6feSAviad Krawczyk void hinic_be32_to_cpu(void *data, int len)
3753e7d6feSAviad Krawczyk {
3853e7d6feSAviad Krawczyk 	u32 *mem = data;
3953e7d6feSAviad Krawczyk 	int i;
4053e7d6feSAviad Krawczyk 
4153e7d6feSAviad Krawczyk 	len = len / sizeof(u32);
4253e7d6feSAviad Krawczyk 
4353e7d6feSAviad Krawczyk 	for (i = 0; i < len; i++) {
4453e7d6feSAviad Krawczyk 		*mem = be32_to_cpu(*mem);
4553e7d6feSAviad Krawczyk 		mem++;
4653e7d6feSAviad Krawczyk 	}
4753e7d6feSAviad Krawczyk }
4876baca2eSAviad Krawczyk 
4976baca2eSAviad Krawczyk /**
5076baca2eSAviad Krawczyk  * hinic_set_sge - set dma area in scatter gather entry
5176baca2eSAviad Krawczyk  * @sge: scatter gather entry
5276baca2eSAviad Krawczyk  * @addr: dma address
5376baca2eSAviad Krawczyk  * @len: length of relevant data in the dma address
5476baca2eSAviad Krawczyk  **/
hinic_set_sge(struct hinic_sge * sge,dma_addr_t addr,int len)5576baca2eSAviad Krawczyk void hinic_set_sge(struct hinic_sge *sge, dma_addr_t addr, int len)
5676baca2eSAviad Krawczyk {
5776baca2eSAviad Krawczyk 	sge->hi_addr = upper_32_bits(addr);
5876baca2eSAviad Krawczyk 	sge->lo_addr = lower_32_bits(addr);
5976baca2eSAviad Krawczyk 	sge->len  = len;
6076baca2eSAviad Krawczyk }
6176baca2eSAviad Krawczyk 
6276baca2eSAviad Krawczyk /**
6376baca2eSAviad Krawczyk  * hinic_sge_to_dma - get dma address from scatter gather entry
6476baca2eSAviad Krawczyk  * @sge: scatter gather entry
6576baca2eSAviad Krawczyk  *
6676baca2eSAviad Krawczyk  * Return dma address of sg entry
6776baca2eSAviad Krawczyk  **/
hinic_sge_to_dma(struct hinic_sge * sge)6876baca2eSAviad Krawczyk dma_addr_t hinic_sge_to_dma(struct hinic_sge *sge)
6976baca2eSAviad Krawczyk {
7076baca2eSAviad Krawczyk 	return (dma_addr_t)((((u64)sge->hi_addr) << 32) | sge->lo_addr);
7176baca2eSAviad Krawczyk }
72