xref: /freebsd/sys/dev/cxgbe/cudbg/cudbg_common.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1*f856f099SNavdeep Parhar /*-
2*f856f099SNavdeep Parhar  * Copyright (c) 2017 Chelsio Communications, Inc.
3*f856f099SNavdeep Parhar  * All rights reserved.
4*f856f099SNavdeep Parhar  *
5*f856f099SNavdeep Parhar  * Redistribution and use in source and binary forms, with or without
6*f856f099SNavdeep Parhar  * modification, are permitted provided that the following conditions
7*f856f099SNavdeep Parhar  * are met:
8*f856f099SNavdeep Parhar  * 1. Redistributions of source code must retain the above copyright
9*f856f099SNavdeep Parhar  *    notice, this list of conditions and the following disclaimer.
10*f856f099SNavdeep Parhar  * 2. Redistributions in binary form must reproduce the above copyright
11*f856f099SNavdeep Parhar  *    notice, this list of conditions and the following disclaimer in the
12*f856f099SNavdeep Parhar  *    documentation and/or other materials provided with the distribution.
13*f856f099SNavdeep Parhar  *
14*f856f099SNavdeep Parhar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*f856f099SNavdeep Parhar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*f856f099SNavdeep Parhar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*f856f099SNavdeep Parhar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*f856f099SNavdeep Parhar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*f856f099SNavdeep Parhar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*f856f099SNavdeep Parhar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*f856f099SNavdeep Parhar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*f856f099SNavdeep Parhar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*f856f099SNavdeep Parhar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*f856f099SNavdeep Parhar  * SUCH DAMAGE.
25*f856f099SNavdeep Parhar  */
26*f856f099SNavdeep Parhar 
27*f856f099SNavdeep Parhar #include <sys/types.h>
28*f856f099SNavdeep Parhar #include <sys/param.h>
29*f856f099SNavdeep Parhar 
30*f856f099SNavdeep Parhar #include "common/common.h"
31*f856f099SNavdeep Parhar #include "cudbg.h"
32*f856f099SNavdeep Parhar #include "cudbg_lib_common.h"
33*f856f099SNavdeep Parhar 
get_scratch_buff(struct cudbg_buffer * pdbg_buff,u32 size,struct cudbg_buffer * pscratch_buff)34*f856f099SNavdeep Parhar int get_scratch_buff(struct cudbg_buffer *pdbg_buff, u32 size,
35*f856f099SNavdeep Parhar 		     struct cudbg_buffer *pscratch_buff)
36*f856f099SNavdeep Parhar {
37*f856f099SNavdeep Parhar 	u32 scratch_offset;
38*f856f099SNavdeep Parhar 	int rc = 0;
39*f856f099SNavdeep Parhar 
40*f856f099SNavdeep Parhar 	scratch_offset = pdbg_buff->size - size;
41*f856f099SNavdeep Parhar 
42*f856f099SNavdeep Parhar 	if (pdbg_buff->offset > (int)scratch_offset || pdbg_buff->size < size) {
43*f856f099SNavdeep Parhar 		rc = CUDBG_STATUS_NO_SCRATCH_MEM;
44*f856f099SNavdeep Parhar 		goto err;
45*f856f099SNavdeep Parhar 	} else {
46*f856f099SNavdeep Parhar 		pscratch_buff->data = (char *)pdbg_buff->data + scratch_offset;
47*f856f099SNavdeep Parhar 		pscratch_buff->offset = 0;
48*f856f099SNavdeep Parhar 		pscratch_buff->size = size;
49*f856f099SNavdeep Parhar 		pdbg_buff->size -= size;
50*f856f099SNavdeep Parhar 	}
51*f856f099SNavdeep Parhar 
52*f856f099SNavdeep Parhar err:
53*f856f099SNavdeep Parhar 	return rc;
54*f856f099SNavdeep Parhar }
55*f856f099SNavdeep Parhar 
release_scratch_buff(struct cudbg_buffer * pscratch_buff,struct cudbg_buffer * pdbg_buff)56*f856f099SNavdeep Parhar void release_scratch_buff(struct cudbg_buffer *pscratch_buff,
57*f856f099SNavdeep Parhar 			  struct cudbg_buffer *pdbg_buff)
58*f856f099SNavdeep Parhar {
59*f856f099SNavdeep Parhar 	pdbg_buff->size += pscratch_buff->size;
60*f856f099SNavdeep Parhar 	/* Reset the used buffer to zero.
61*f856f099SNavdeep Parhar 	 * If we dont do this, then it will effect the ext entity logic.
62*f856f099SNavdeep Parhar 	 */
63*f856f099SNavdeep Parhar 	memset(pscratch_buff->data, 0, pscratch_buff->size);
64*f856f099SNavdeep Parhar 	pscratch_buff->data = NULL;
65*f856f099SNavdeep Parhar 	pscratch_buff->offset = 0;
66*f856f099SNavdeep Parhar 	pscratch_buff->size = 0;
67*f856f099SNavdeep Parhar }
68*f856f099SNavdeep Parhar 
init_cudbg_hdr(struct cudbg_init_hdr * hdr)69*f856f099SNavdeep Parhar static inline void init_cudbg_hdr(struct cudbg_init_hdr *hdr)
70*f856f099SNavdeep Parhar {
71*f856f099SNavdeep Parhar 	hdr->major_ver = CUDBG_MAJOR_VERSION;
72*f856f099SNavdeep Parhar 	hdr->minor_ver = CUDBG_MINOR_VERSION;
73*f856f099SNavdeep Parhar 	hdr->build_ver = CUDBG_BUILD_VERSION;
74*f856f099SNavdeep Parhar 	hdr->init_struct_size = sizeof(struct cudbg_init);
75*f856f099SNavdeep Parhar }
76*f856f099SNavdeep Parhar 
77*f856f099SNavdeep Parhar void *
cudbg_alloc_handle(void)78*f856f099SNavdeep Parhar cudbg_alloc_handle(void)
79*f856f099SNavdeep Parhar {
80*f856f099SNavdeep Parhar 	struct cudbg_private *handle;
81*f856f099SNavdeep Parhar 
82*f856f099SNavdeep Parhar 	handle = malloc(sizeof(*handle), M_CXGBE, M_ZERO | M_WAITOK);
83*f856f099SNavdeep Parhar 	init_cudbg_hdr(&handle->dbg_init.header);
84*f856f099SNavdeep Parhar 
85*f856f099SNavdeep Parhar 	return (handle);
86*f856f099SNavdeep Parhar }
87*f856f099SNavdeep Parhar 
88*f856f099SNavdeep Parhar void
cudbg_free_handle(void * handle)89*f856f099SNavdeep Parhar cudbg_free_handle(void *handle)
90*f856f099SNavdeep Parhar {
91*f856f099SNavdeep Parhar 
92*f856f099SNavdeep Parhar 	free(handle, M_CXGBE);
93*f856f099SNavdeep Parhar }
94