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/cdefs.h> 28*f856f099SNavdeep Parhar __FBSDID("$FreeBSD$"); 29*f856f099SNavdeep Parhar 30*f856f099SNavdeep Parhar #include <sys/types.h> 31*f856f099SNavdeep Parhar #include <sys/param.h> 32*f856f099SNavdeep Parhar 33*f856f099SNavdeep Parhar #include "common/common.h" 34*f856f099SNavdeep Parhar #include "cudbg.h" 35*f856f099SNavdeep Parhar #include "cudbg_lib_common.h" 36*f856f099SNavdeep Parhar 37*f856f099SNavdeep Parhar int get_scratch_buff(struct cudbg_buffer *pdbg_buff, u32 size, 38*f856f099SNavdeep Parhar struct cudbg_buffer *pscratch_buff) 39*f856f099SNavdeep Parhar { 40*f856f099SNavdeep Parhar u32 scratch_offset; 41*f856f099SNavdeep Parhar int rc = 0; 42*f856f099SNavdeep Parhar 43*f856f099SNavdeep Parhar scratch_offset = pdbg_buff->size - size; 44*f856f099SNavdeep Parhar 45*f856f099SNavdeep Parhar if (pdbg_buff->offset > (int)scratch_offset || pdbg_buff->size < size) { 46*f856f099SNavdeep Parhar rc = CUDBG_STATUS_NO_SCRATCH_MEM; 47*f856f099SNavdeep Parhar goto err; 48*f856f099SNavdeep Parhar } else { 49*f856f099SNavdeep Parhar pscratch_buff->data = (char *)pdbg_buff->data + scratch_offset; 50*f856f099SNavdeep Parhar pscratch_buff->offset = 0; 51*f856f099SNavdeep Parhar pscratch_buff->size = size; 52*f856f099SNavdeep Parhar pdbg_buff->size -= size; 53*f856f099SNavdeep Parhar } 54*f856f099SNavdeep Parhar 55*f856f099SNavdeep Parhar err: 56*f856f099SNavdeep Parhar return rc; 57*f856f099SNavdeep Parhar } 58*f856f099SNavdeep Parhar 59*f856f099SNavdeep Parhar void release_scratch_buff(struct cudbg_buffer *pscratch_buff, 60*f856f099SNavdeep Parhar struct cudbg_buffer *pdbg_buff) 61*f856f099SNavdeep Parhar { 62*f856f099SNavdeep Parhar pdbg_buff->size += pscratch_buff->size; 63*f856f099SNavdeep Parhar /* Reset the used buffer to zero. 64*f856f099SNavdeep Parhar * If we dont do this, then it will effect the ext entity logic. 65*f856f099SNavdeep Parhar */ 66*f856f099SNavdeep Parhar memset(pscratch_buff->data, 0, pscratch_buff->size); 67*f856f099SNavdeep Parhar pscratch_buff->data = NULL; 68*f856f099SNavdeep Parhar pscratch_buff->offset = 0; 69*f856f099SNavdeep Parhar pscratch_buff->size = 0; 70*f856f099SNavdeep Parhar } 71*f856f099SNavdeep Parhar 72*f856f099SNavdeep Parhar static inline void init_cudbg_hdr(struct cudbg_init_hdr *hdr) 73*f856f099SNavdeep Parhar { 74*f856f099SNavdeep Parhar hdr->major_ver = CUDBG_MAJOR_VERSION; 75*f856f099SNavdeep Parhar hdr->minor_ver = CUDBG_MINOR_VERSION; 76*f856f099SNavdeep Parhar hdr->build_ver = CUDBG_BUILD_VERSION; 77*f856f099SNavdeep Parhar hdr->init_struct_size = sizeof(struct cudbg_init); 78*f856f099SNavdeep Parhar } 79*f856f099SNavdeep Parhar 80*f856f099SNavdeep Parhar void * 81*f856f099SNavdeep Parhar cudbg_alloc_handle(void) 82*f856f099SNavdeep Parhar { 83*f856f099SNavdeep Parhar struct cudbg_private *handle; 84*f856f099SNavdeep Parhar 85*f856f099SNavdeep Parhar handle = malloc(sizeof(*handle), M_CXGBE, M_ZERO | M_WAITOK); 86*f856f099SNavdeep Parhar init_cudbg_hdr(&handle->dbg_init.header); 87*f856f099SNavdeep Parhar 88*f856f099SNavdeep Parhar return (handle); 89*f856f099SNavdeep Parhar } 90*f856f099SNavdeep Parhar 91*f856f099SNavdeep Parhar void 92*f856f099SNavdeep Parhar cudbg_free_handle(void *handle) 93*f856f099SNavdeep Parhar { 94*f856f099SNavdeep Parhar 95*f856f099SNavdeep Parhar free(handle, M_CXGBE); 96*f856f099SNavdeep Parhar } 97