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