xref: /illumos-gate/usr/src/uts/common/io/cxgbe/t4nex/cudbg_common.c (revision 13b136d3061155363c62c9f6568d25b8b27da8f6)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source. A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*-
13  * Copyright (c) 2019 Chelsio Communications, Inc.
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 
41 #ifdef _KERNEL
42 #include "common/common.h"
43 #else
44 #include <stdio.h>
45 #include <string.h>
46 #endif
47 #include "cudbg.h"
48 #include "cudbg_lib_common.h"
49 
50 int
51 get_scratch_buff(struct cudbg_buffer *pdbg_buff, u32 size,
52 		 struct cudbg_buffer *pscratch_buff)
53 {
54 	u32 scratch_offset;
55 	int rc = 0;
56 
57 	scratch_offset = pdbg_buff->size - size;
58 
59 	if (pdbg_buff->offset > (int)scratch_offset || pdbg_buff->size < size) {
60 		rc = CUDBG_STATUS_NO_SCRATCH_MEM;
61 		goto err;
62 	} else {
63 		pscratch_buff->data = (char *)pdbg_buff->data + scratch_offset;
64 		pscratch_buff->offset = 0;
65 		pscratch_buff->size = size;
66 		pdbg_buff->size -= size;
67 	}
68 
69 err:
70 	return rc;
71 }
72 
73 void
74 release_scratch_buff(struct cudbg_buffer *pscratch_buff,
75 		     struct cudbg_buffer *pdbg_buff)
76 {
77 	pdbg_buff->size += pscratch_buff->size;
78 	/* Reset the used buffer to zero.
79  	 * If we dont do this, then it will effect the ext entity logic.
80  	 */
81 	memset(pscratch_buff->data, 0, pscratch_buff->size);
82 	pscratch_buff->data = NULL;
83 	pscratch_buff->offset = 0;
84 	pscratch_buff->size = 0;
85 }
86