1 /*
2  * Huawei HiNIC PCI Express Linux driver
3  * Copyright(c) 2017 Huawei Technologies Co., Ltd
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <asm/byteorder.h>
19 
20 #include "hinic_common.h"
21 
22 /**
23  * hinic_cpu_to_be32 - convert data to big endian 32 bit format
24  * @data: the data to convert
25  * @len: length of data to convert
26  **/
27 void hinic_cpu_to_be32(void *data, int len)
28 {
29 	u32 *mem = data;
30 	int i;
31 
32 	len = len / sizeof(u32);
33 
34 	for (i = 0; i < len; i++) {
35 		*mem = cpu_to_be32(*mem);
36 		mem++;
37 	}
38 }
39 
40 /**
41  * hinic_be32_to_cpu - convert data from big endian 32 bit format
42  * @data: the data to convert
43  * @len: length of data to convert
44  **/
45 void hinic_be32_to_cpu(void *data, int len)
46 {
47 	u32 *mem = data;
48 	int i;
49 
50 	len = len / sizeof(u32);
51 
52 	for (i = 0; i < len; i++) {
53 		*mem = be32_to_cpu(*mem);
54 		mem++;
55 	}
56 }
57 
58 /**
59  * hinic_set_sge - set dma area in scatter gather entry
60  * @sge: scatter gather entry
61  * @addr: dma address
62  * @len: length of relevant data in the dma address
63  **/
64 void hinic_set_sge(struct hinic_sge *sge, dma_addr_t addr, int len)
65 {
66 	sge->hi_addr = upper_32_bits(addr);
67 	sge->lo_addr = lower_32_bits(addr);
68 	sge->len  = len;
69 }
70 
71 /**
72  * hinic_sge_to_dma - get dma address from scatter gather entry
73  * @sge: scatter gather entry
74  *
75  * Return dma address of sg entry
76  **/
77 dma_addr_t hinic_sge_to_dma(struct hinic_sge *sge)
78 {
79 	return (dma_addr_t)((((u64)sge->hi_addr) << 32) | sge->lo_addr);
80 }
81