1 /*
2  * Copyright (c) 2014-2016, 2018-2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: cds_packet.c
22  *      Connectivity driver services (CDS) network Packet APIs
23  *      Network Protocol packet/buffer support interfaces
24  */
25 
26 #include <cds_packet.h>
27 #include <i_cds_packet.h>
28 #include <qdf_mc_timer.h>
29 #include <qdf_trace.h>
30 #include <wlan_hdd_main.h>
31 #include "qdf_nbuf.h"
32 #include "qdf_mem.h"
33 #include "cds_utils.h"
34 
35 #define TX_PKT_MIN_HEADROOM          (64)
36 
cds_pkt_return_packet(cds_pkt_t * packet)37 QDF_STATUS cds_pkt_return_packet(cds_pkt_t *packet)
38 {
39 	/* Validate the input parameter pointer */
40 	if (unlikely(!packet)) {
41 		return QDF_STATUS_E_INVAL;
42 	}
43 
44 	/* Free up the qdf nbuf */
45 	qdf_nbuf_free(packet->pkt_buf);
46 
47 	packet->pkt_buf = NULL;
48 
49 	/* Free up the Rx packet */
50 	qdf_mem_free(packet);
51 
52 	return QDF_STATUS_SUCCESS;
53 }
54 
55 QDF_STATUS
cds_pkt_get_packet_length(cds_pkt_t * pPacket,uint16_t * pPacketSize)56 cds_pkt_get_packet_length(cds_pkt_t *pPacket, uint16_t *pPacketSize)
57 {
58 	/* Validate the parameter pointers */
59 	if (unlikely((!pPacket) || (!pPacketSize)) ||
60 	    (!pPacket->pkt_buf)) {
61 		cds_alert("NULL pointer");
62 		return QDF_STATUS_E_INVAL;
63 	}
64 	/* return the requested information */
65 	*pPacketSize = qdf_nbuf_len(pPacket->pkt_buf);
66 	return QDF_STATUS_SUCCESS;
67 }
68 
69 #ifdef MEMORY_DEBUG
cds_packet_alloc_debug(uint16_t size,void ** data,void ** ppPacket,const char * func_name,uint32_t line_num)70 QDF_STATUS cds_packet_alloc_debug(uint16_t size, void **data, void **ppPacket,
71 				  const char *func_name, uint32_t line_num)
72 {
73 	QDF_STATUS qdf_ret_status = QDF_STATUS_E_FAILURE;
74 	qdf_nbuf_t nbuf;
75 
76 	nbuf = qdf_nbuf_alloc_debug(NULL,
77 		roundup(size + TX_PKT_MIN_HEADROOM, 4),
78 		TX_PKT_MIN_HEADROOM, sizeof(uint32_t), false,
79 				     func_name, line_num);
80 
81 	if (nbuf) {
82 		qdf_nbuf_put_tail(nbuf, size);
83 		qdf_nbuf_set_protocol(nbuf, ETH_P_CONTROL);
84 		*ppPacket = nbuf;
85 		*data = qdf_nbuf_data(nbuf);
86 		qdf_ret_status = QDF_STATUS_SUCCESS;
87 	}
88 
89 	return qdf_ret_status;
90 }
91 #else
cds_packet_alloc(uint16_t size,void ** data,void ** ppPacket)92 QDF_STATUS cds_packet_alloc(uint16_t size, void **data, void **ppPacket)
93 {
94 	QDF_STATUS qdf_ret_status = QDF_STATUS_E_FAILURE;
95 	qdf_nbuf_t nbuf;
96 
97 	nbuf = qdf_nbuf_alloc(NULL, roundup(size + TX_PKT_MIN_HEADROOM, 4),
98 			      TX_PKT_MIN_HEADROOM, sizeof(uint32_t), false);
99 
100 	if (nbuf) {
101 		qdf_nbuf_put_tail(nbuf, size);
102 		qdf_nbuf_set_protocol(nbuf, ETH_P_CONTROL);
103 		*ppPacket = nbuf;
104 		*data = qdf_nbuf_data(nbuf);
105 		qdf_ret_status = QDF_STATUS_SUCCESS;
106 	}
107 
108 	return qdf_ret_status;
109 }
110 
111 #endif
112 
cds_packet_free(void * pPacket)113 void cds_packet_free(void *pPacket)
114 {
115 	qdf_nbuf_free((qdf_nbuf_t) pPacket);
116 }
117