1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Huawei HiNIC PCI Express Linux driver
4  * Copyright(c) 2017 Huawei Technologies Co., Ltd
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <asm/byteorder.h>
10 
11 #include "hinic_common.h"
12 
13 /**
14  * hinic_cpu_to_be32 - convert data to big endian 32 bit format
15  * @data: the data to convert
16  * @len: length of data to convert
17  **/
hinic_cpu_to_be32(void * data,int len)18 void hinic_cpu_to_be32(void *data, int len)
19 {
20 	u32 *mem = data;
21 	int i;
22 
23 	len = len / sizeof(u32);
24 
25 	for (i = 0; i < len; i++) {
26 		*mem = cpu_to_be32(*mem);
27 		mem++;
28 	}
29 }
30 
31 /**
32  * hinic_be32_to_cpu - convert data from big endian 32 bit format
33  * @data: the data to convert
34  * @len: length of data to convert
35  **/
hinic_be32_to_cpu(void * data,int len)36 void hinic_be32_to_cpu(void *data, int len)
37 {
38 	u32 *mem = data;
39 	int i;
40 
41 	len = len / sizeof(u32);
42 
43 	for (i = 0; i < len; i++) {
44 		*mem = be32_to_cpu(*mem);
45 		mem++;
46 	}
47 }
48 
49 /**
50  * hinic_set_sge - set dma area in scatter gather entry
51  * @sge: scatter gather entry
52  * @addr: dma address
53  * @len: length of relevant data in the dma address
54  **/
hinic_set_sge(struct hinic_sge * sge,dma_addr_t addr,int len)55 void hinic_set_sge(struct hinic_sge *sge, dma_addr_t addr, int len)
56 {
57 	sge->hi_addr = upper_32_bits(addr);
58 	sge->lo_addr = lower_32_bits(addr);
59 	sge->len  = len;
60 }
61 
62 /**
63  * hinic_sge_to_dma - get dma address from scatter gather entry
64  * @sge: scatter gather entry
65  *
66  * Return dma address of sg entry
67  **/
hinic_sge_to_dma(struct hinic_sge * sge)68 dma_addr_t hinic_sge_to_dma(struct hinic_sge *sge)
69 {
70 	return (dma_addr_t)((((u64)sge->hi_addr) << 32) | sge->lo_addr);
71 }
72