14fa9c49fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a7975a2fSRahul Lakkireddy /*
3a7975a2fSRahul Lakkireddy  *  Copyright (C) 2017 Chelsio Communications.  All rights reserved.
4a7975a2fSRahul Lakkireddy  */
5a7975a2fSRahul Lakkireddy 
6a7975a2fSRahul Lakkireddy #include "cxgb4.h"
7a7975a2fSRahul Lakkireddy #include "cudbg_if.h"
8a7975a2fSRahul Lakkireddy #include "cudbg_lib_common.h"
9a7975a2fSRahul Lakkireddy 
cudbg_get_buff(struct cudbg_init * pdbg_init,struct cudbg_buffer * pdbg_buff,u32 size,struct cudbg_buffer * pin_buff)1056cf2635SRahul Lakkireddy int cudbg_get_buff(struct cudbg_init *pdbg_init,
1156cf2635SRahul Lakkireddy 		   struct cudbg_buffer *pdbg_buff, u32 size,
12a7975a2fSRahul Lakkireddy 		   struct cudbg_buffer *pin_buff)
13a7975a2fSRahul Lakkireddy {
14a7975a2fSRahul Lakkireddy 	u32 offset;
15a7975a2fSRahul Lakkireddy 
16a7975a2fSRahul Lakkireddy 	offset = pdbg_buff->offset;
17a7975a2fSRahul Lakkireddy 	if (offset + size > pdbg_buff->size)
18a7975a2fSRahul Lakkireddy 		return CUDBG_STATUS_NO_MEM;
19a7975a2fSRahul Lakkireddy 
2056cf2635SRahul Lakkireddy 	if (pdbg_init->compress_type != CUDBG_COMPRESSION_NONE) {
2156cf2635SRahul Lakkireddy 		if (size > pdbg_init->compress_buff_size)
2256cf2635SRahul Lakkireddy 			return CUDBG_STATUS_NO_MEM;
2356cf2635SRahul Lakkireddy 
2456cf2635SRahul Lakkireddy 		pin_buff->data = (char *)pdbg_init->compress_buff;
2556cf2635SRahul Lakkireddy 		pin_buff->offset = 0;
26a7975a2fSRahul Lakkireddy 		pin_buff->size = size;
27a7975a2fSRahul Lakkireddy 		return 0;
28a7975a2fSRahul Lakkireddy 	}
29a7975a2fSRahul Lakkireddy 
3056cf2635SRahul Lakkireddy 	pin_buff->data = (char *)pdbg_buff->data + offset;
3156cf2635SRahul Lakkireddy 	pin_buff->offset = offset;
3256cf2635SRahul Lakkireddy 	pin_buff->size = size;
3356cf2635SRahul Lakkireddy 	return 0;
3456cf2635SRahul Lakkireddy }
3556cf2635SRahul Lakkireddy 
cudbg_put_buff(struct cudbg_init * pdbg_init,struct cudbg_buffer * pin_buff)3656cf2635SRahul Lakkireddy void cudbg_put_buff(struct cudbg_init *pdbg_init,
3756cf2635SRahul Lakkireddy 		    struct cudbg_buffer *pin_buff)
38a7975a2fSRahul Lakkireddy {
3956cf2635SRahul Lakkireddy 	/* Clear compression buffer for re-use */
4056cf2635SRahul Lakkireddy 	if (pdbg_init->compress_type != CUDBG_COMPRESSION_NONE)
4156cf2635SRahul Lakkireddy 		memset(pdbg_init->compress_buff, 0,
4256cf2635SRahul Lakkireddy 		       pdbg_init->compress_buff_size);
4356cf2635SRahul Lakkireddy 
44a7975a2fSRahul Lakkireddy 	pin_buff->data = NULL;
45a7975a2fSRahul Lakkireddy 	pin_buff->offset = 0;
46a7975a2fSRahul Lakkireddy 	pin_buff->size = 0;
47a7975a2fSRahul Lakkireddy }
48a7975a2fSRahul Lakkireddy 
cudbg_update_buff(struct cudbg_buffer * pin_buff,struct cudbg_buffer * pout_buff)49a7975a2fSRahul Lakkireddy void cudbg_update_buff(struct cudbg_buffer *pin_buff,
50a7975a2fSRahul Lakkireddy 		       struct cudbg_buffer *pout_buff)
51a7975a2fSRahul Lakkireddy {
52a7975a2fSRahul Lakkireddy 	/* We already write to buffer provided by ethool, so just
53a7975a2fSRahul Lakkireddy 	 * increment offset to next free space.
54a7975a2fSRahul Lakkireddy 	 */
55a7975a2fSRahul Lakkireddy 	pout_buff->offset += pin_buff->size;
56a7975a2fSRahul Lakkireddy }
57