1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * HND generic pktq operation primitives 4 * 5 * Copyright (C) 1999-2019, Broadcom. 6 * 7 * Unless you and Broadcom execute a separate written software license 8 * agreement governing use of this software, this software is licensed to you 9 * under the terms of the GNU General Public License version 2 (the "GPL"), 10 * available at http://www.broadcom.com/licenses/GPLv2.php, with the 11 * following added to such license: 12 * 13 * As a special exception, the copyright holders of this software give you 14 * permission to link this software with independent modules, and to copy and 15 * distribute the resulting executable under terms of your choice, provided that 16 * you also meet, for each linked independent module, the terms and conditions of 17 * the license of that module. An independent module is a module which is not 18 * derived from this software. The special exception does not apply to any 19 * modifications of the software. 20 * 21 * Notwithstanding the above, under no circumstances may you combine this 22 * software in any way with any other Broadcom software provided under a license 23 * other than the GPL, without Broadcom's express prior written consent. 24 * 25 * 26 * <<Broadcom-WL-IPTag/Open:>> 27 * 28 * $Id: hnd_pktq.h 698847 2017-05-11 00:10:48Z $ 29 */ 30 31 #ifndef _hnd_pktq_h_ 32 #define _hnd_pktq_h_ 33 34 #include <osl_ext.h> 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif // endif 39 40 /* mutex macros for thread safe */ 41 #ifdef HND_PKTQ_THREAD_SAFE 42 #define HND_PKTQ_MUTEX_DECL(mutex) OSL_EXT_MUTEX_DECL(mutex) 43 #else 44 #define HND_PKTQ_MUTEX_DECL(mutex) 45 #endif // endif 46 47 /* osl multi-precedence packet queue */ 48 #define PKTQ_LEN_MAX 0xFFFF /* Max uint16 65535 packets */ 49 #ifndef PKTQ_LEN_DEFAULT 50 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */ 51 #endif // endif 52 #ifndef PKTQ_MAX_PREC 53 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */ 54 #endif // endif 55 56 /** Queue for a single precedence level */ 57 typedef struct pktq_prec { 58 void *head; /**< first packet to dequeue */ 59 void *tail; /**< last packet to dequeue */ 60 uint16 n_pkts; /**< number of queued packets */ 61 uint16 max_pkts; /**< maximum number of queued packets */ 62 uint16 stall_count; /**< # seconds since no packets are dequeued */ 63 uint16 dequeue_count; /**< # of packets dequeued in last 1 second */ 64 } pktq_prec_t; 65 66 #ifdef PKTQ_LOG 67 typedef struct { 68 uint32 requested; /**< packets requested to be stored */ 69 uint32 stored; /**< packets stored */ 70 uint32 saved; /**< packets saved, 71 because a lowest priority queue has given away one packet 72 */ 73 uint32 selfsaved; /**< packets saved, 74 because an older packet from the same queue has been dropped 75 */ 76 uint32 full_dropped; /**< packets dropped, 77 because pktq is full with higher precedence packets 78 */ 79 uint32 dropped; /**< packets dropped because pktq per that precedence is full */ 80 uint32 sacrificed; /**< packets dropped, 81 in order to save one from a queue of a highest priority 82 */ 83 uint32 busy; /**< packets droped because of hardware/transmission error */ 84 uint32 retry; /**< packets re-sent because they were not received */ 85 uint32 ps_retry; /**< packets retried again prior to moving power save mode */ 86 uint32 suppress; /**< packets which were suppressed and not transmitted */ 87 uint32 retry_drop; /**< packets finally dropped after retry limit */ 88 uint32 max_avail; /**< the high-water mark of the queue capacity for packets - 89 goes to zero as queue fills 90 */ 91 uint32 max_used; /**< the high-water mark of the queue utilisation for packets - 92 increases with use ('inverse' of max_avail) 93 */ 94 uint32 queue_capacity; /**< the maximum capacity of the queue */ 95 uint32 rtsfail; /**< count of rts attempts that failed to receive cts */ 96 uint32 acked; /**< count of packets sent (acked) successfully */ 97 uint32 txrate_succ; /**< running total of phy rate of packets sent successfully */ 98 uint32 txrate_main; /**< running totoal of primary phy rate of all packets */ 99 uint32 throughput; /**< actual data transferred successfully */ 100 uint32 airtime; /**< cumulative total medium access delay in useconds */ 101 uint32 _logtime; /**< timestamp of last counter clear */ 102 } pktq_counters_t; 103 104 #define PKTQ_LOG_COMMON \ 105 uint32 pps_time; /**< time spent in ps pretend state */ \ 106 uint32 _prec_log; 107 108 typedef struct { 109 PKTQ_LOG_COMMON 110 pktq_counters_t* _prec_cnt[PKTQ_MAX_PREC]; /**< Counters per queue */ 111 } pktq_log_t; 112 #else 113 typedef struct pktq_log pktq_log_t; 114 #endif /* PKTQ_LOG */ 115 116 #define PKTQ_COMMON \ 117 HND_PKTQ_MUTEX_DECL(mutex) \ 118 pktq_log_t *pktqlog; \ 119 uint16 num_prec; /**< number of precedences in use */ \ 120 uint16 hi_prec; /**< rapid dequeue hint (>= highest non-empty prec) */ \ 121 uint16 max_pkts; /**< max packets */ \ 122 uint16 n_pkts_tot; /**< total (cummulative over all precedences) number of packets */ 123 124 /** multi-priority packet queue */ 125 struct pktq { 126 PKTQ_COMMON 127 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */ 128 struct pktq_prec q[PKTQ_MAX_PREC]; 129 }; 130 131 /** simple, non-priority packet queue */ 132 struct spktq { 133 HND_PKTQ_MUTEX_DECL(mutex) 134 struct pktq_prec q; 135 }; 136 137 #define PKTQ_PREC_ITER(pq, prec) for (prec = (pq)->num_prec - 1; prec >= 0; prec--) 138 139 /* fn(pkt, arg). return true if pkt belongs to bsscfg */ 140 typedef bool (*ifpkt_cb_t)(void*, int); 141 142 /* 143 * pktq filter support 144 */ 145 146 /** filter function return values */ 147 typedef enum { 148 PKT_FILTER_NOACTION = 0, /**< restore the pkt to its position in the queue */ 149 PKT_FILTER_DELETE = 1, /**< delete the pkt */ 150 PKT_FILTER_REMOVE = 2, /**< do not restore the pkt to the queue, 151 * filter fn has taken ownership of the pkt 152 */ 153 } pktq_filter_result_t; 154 155 /** 156 * Caller supplied filter function to pktq_pfilter(), pktq_filter(). 157 * Function filter(ctx, pkt) is called with its ctx pointer on each pkt in the 158 * pktq. When the filter function is called, the supplied pkt will have been 159 * unlinked from the pktq. The filter function returns a pktq_filter_result_t 160 * result specifying the action pktq_filter()/pktq_pfilter() should take for 161 * the pkt. 162 * Here are the actions taken by pktq_filter/pfilter() based on the supplied 163 * filter function's return value: 164 * 165 * PKT_FILTER_NOACTION - The filter will re-link the pkt at its 166 * previous location. 167 * 168 * PKT_FILTER_DELETE - The filter will not relink the pkt and will 169 * call the user supplied defer_free_pkt fn on the packet. 170 * 171 * PKT_FILTER_REMOVE - The filter will not relink the pkt. The supplied 172 * filter fn took ownership (or deleted) the pkt. 173 * 174 * WARNING: pkts inserted by the user (in pkt_filter and/or flush callbacks 175 * and chains) in the prec queue will not be seen by the filter, and the prec 176 * queue will be temporarily be removed from the queue hence there're side 177 * effects including pktq_n_pkts_tot() on the queue won't reflect the correct number 178 * of packets in the queue. 179 */ 180 181 typedef pktq_filter_result_t (*pktq_filter_t)(void* ctx, void* pkt); 182 183 /** 184 * The defer_free_pkt callback is invoked when the the pktq_filter callback 185 * returns PKT_FILTER_DELETE decision, which allows the user to deposite 186 * the packet appropriately based on the situation (free the packet or 187 * save it in a temporary queue etc.). 188 */ 189 typedef void (*defer_free_pkt_fn_t)(void *ctx, void *pkt); 190 191 /** 192 * The flush_free_pkt callback is invoked when all packets in the pktq 193 * are processed. 194 */ 195 typedef void (*flush_free_pkt_fn_t)(void *ctx); 196 197 #if defined(WLAMPDU_MAC) && defined(PROP_TXSTATUS) 198 /* this callback will be invoked when in low_txq_scb flush() 199 * two back-to-back pkts has same epoch value. 200 */ 201 typedef void (*flip_epoch_t)(void *ctx, void *pkt, uint8 *flipEpoch, uint8 *lastEpoch); 202 #endif /* defined(WLAMPDU_MAC) && defined(PROP_TXSTATUS) */ 203 204 /** filter a pktq, using the caller supplied filter/deposition/flush functions */ 205 extern void pktq_filter(struct pktq *pq, pktq_filter_t fn, void* arg, 206 defer_free_pkt_fn_t defer, void *defer_ctx, flush_free_pkt_fn_t flush, void *flush_ctx); 207 /** filter a particular precedence in pktq, using the caller supplied filter function */ 208 extern void pktq_pfilter(struct pktq *pq, int prec, pktq_filter_t fn, void* arg, 209 defer_free_pkt_fn_t defer, void *defer_ctx, flush_free_pkt_fn_t flush, void *flush_ctx); 210 /** filter a simple non-precedence in spktq, using the caller supplied filter function */ 211 extern void spktq_filter(struct spktq *spq, pktq_filter_t fltr, void* fltr_ctx, 212 defer_free_pkt_fn_t defer, void *defer_ctx, flush_free_pkt_fn_t flush, void *flush_ctx); 213 214 /* operations on a specific precedence in packet queue */ 215 #define pktqprec_max_pkts(pq, prec) ((pq)->q[prec].max_pkts) 216 #define pktqprec_n_pkts(pq, prec) ((pq)->q[prec].n_pkts) 217 #define pktqprec_empty(pq, prec) ((pq)->q[prec].n_pkts == 0) 218 #define pktqprec_peek(pq, prec) ((pq)->q[prec].head) 219 #define pktqprec_peek_tail(pq, prec) ((pq)->q[prec].tail) 220 #define spktq_peek_tail(pq) ((pq)->q.tail) 221 #ifdef HND_PKTQ_THREAD_SAFE 222 extern int pktqprec_avail_pkts(struct pktq *pq, int prec); 223 extern bool pktqprec_full(struct pktq *pq, int prec); 224 #else 225 #define pktqprec_avail_pkts(pq, prec) ((pq)->q[prec].max_pkts - (pq)->q[prec].n_pkts) 226 #define pktqprec_full(pq, prec) ((pq)->q[prec].n_pkts >= (pq)->q[prec].max_pkts) 227 #endif /* HND_PKTQ_THREAD_SAFE */ 228 229 extern void pktq_append(struct pktq *pq, int prec, struct spktq *list); 230 extern void spktq_append(struct spktq *spq, struct spktq *list); 231 extern void pktq_prepend(struct pktq *pq, int prec, struct spktq *list); 232 extern void spktq_prepend(struct spktq *spq, struct spktq *list); 233 extern void *pktq_penq(struct pktq *pq, int prec, void *p); 234 extern void *pktq_penq_head(struct pktq *pq, int prec, void *p); 235 extern void *pktq_pdeq(struct pktq *pq, int prec); 236 extern void *pktq_pdeq_prev(struct pktq *pq, int prec, void *prev_p); 237 extern void *pktq_pdeq_with_fn(struct pktq *pq, int prec, ifpkt_cb_t fn, int arg); 238 extern void *pktq_pdeq_tail(struct pktq *pq, int prec); 239 /** Remove a specified packet from its queue */ 240 extern bool pktq_pdel(struct pktq *pq, void *p, int prec); 241 242 /* For single precedence queues */ 243 extern void *spktq_enq(struct spktq *spq, void *p); 244 extern void *spktq_enq_head(struct spktq *spq, void *p); 245 extern void *spktq_deq(struct spktq *spq); 246 extern void *spktq_deq_tail(struct spktq *spq); 247 248 /* operations on a set of precedences in packet queue */ 249 250 extern int pktq_mlen(struct pktq *pq, uint prec_bmp); 251 extern void *pktq_mdeq(struct pktq *pq, uint prec_bmp, int *prec_out); 252 extern void *pktq_mpeek(struct pktq *pq, uint prec_bmp, int *prec_out); 253 254 /* operations on packet queue as a whole */ 255 256 #define pktq_n_pkts_tot(pq) ((int)(pq)->n_pkts_tot) 257 #define pktq_max(pq) ((int)(pq)->max_pkts) 258 #define pktq_empty(pq) ((pq)->n_pkts_tot == 0) 259 #define spktq_n_pkts(spq) ((int)(spq)->q.n_pkts) 260 #define spktq_empty(spq) ((spq)->q.n_pkts == 0) 261 262 #define spktq_max(spq) ((int)(spq)->q.max_pkts) 263 #define spktq_empty(spq) ((spq)->q.n_pkts == 0) 264 #ifdef HND_PKTQ_THREAD_SAFE 265 extern int pktq_avail(struct pktq *pq); 266 extern bool pktq_full(struct pktq *pq); 267 extern int spktq_avail(struct spktq *spq); 268 extern bool spktq_full(struct spktq *spq); 269 #else 270 #define pktq_avail(pq) ((int)((pq)->max_pkts - (pq)->n_pkts_tot)) 271 #define pktq_full(pq) ((pq)->n_pkts_tot >= (pq)->max_pkts) 272 #define spktq_avail(spq) ((int)((spq)->q.max_pkts - (spq)->q.n_pkts)) 273 #define spktq_full(spq) ((spq)->q.n_pkts >= (spq)->q.max_pkts) 274 #endif /* HND_PKTQ_THREAD_SAFE */ 275 276 /* operations for single precedence queues */ 277 #define pktenq(pq, p) pktq_penq((pq), 0, (p)) 278 #define pktenq_head(pq, p) pktq_penq_head((pq), 0, (p)) 279 #define pktdeq(pq) pktq_pdeq((pq), 0) 280 #define pktdeq_tail(pq) pktq_pdeq_tail((pq), 0) 281 #define pktqflush(osh, pq, dir) pktq_pflush(osh, (pq), 0, (dir)) 282 #define pktqinit(pq, max_pkts) pktq_init((pq), 1, (max_pkts)) 283 #define pktqdeinit(pq) pktq_deinit((pq)) 284 #define pktqavail(pq) pktq_avail((pq)) 285 #define pktqfull(pq) pktq_full((pq)) 286 #define pktqfilter(pq, fltr, fltr_ctx, defer, defer_ctx, flush, flush_ctx) \ 287 pktq_pfilter((pq), 0, (fltr), (fltr_ctx), (defer), (defer_ctx), (flush), (flush_ctx)) 288 289 /* operations for simple non-precedence queues */ 290 #define spktenq(spq, p) spktq_enq((spq), (p)) 291 #define spktenq_head(spq, p) spktq_enq_head((spq), (p)) 292 #define spktdeq(spq) spktq_deq((spq)) 293 #define spktdeq_tail(spq) spktq_deq_tail((spq)) 294 #define spktqflush(osh, spq, dir) spktq_flush((osh), (spq), (dir)) 295 #define spktqinit(spq, max_pkts) spktq_init((spq), (max_pkts)) 296 #define spktqdeinit(spq) spktq_deinit((spq)) 297 #define spktqavail(spq) spktq_avail((spq)) 298 #define spktqfull(spq) spktq_full((spq)) 299 300 #define spktqfilter(spq, fltr, fltr_ctx, defer, defer_ctx, flush, flush_ctx) \ 301 spktq_filter((spq), (fltr), (fltr_ctx), (defer), (defer_ctx), (flush), (flush_ctx)) 302 extern bool pktq_init(struct pktq *pq, int num_prec, int max_pkts); 303 extern bool pktq_deinit(struct pktq *pq); 304 extern bool spktq_init(struct spktq *spq, int max_pkts); 305 extern bool spktq_deinit(struct spktq *spq); 306 307 extern void pktq_set_max_plen(struct pktq *pq, int prec, int max_pkts); 308 309 /* prec_out may be NULL if caller is not interested in return value */ 310 extern void *pktq_deq(struct pktq *pq, int *prec_out); 311 extern void *pktq_deq_tail(struct pktq *pq, int *prec_out); 312 extern void *pktq_peek(struct pktq *pq, int *prec_out); 313 extern void *spktq_peek(struct spktq *spq); 314 extern void *pktq_peek_tail(struct pktq *pq, int *prec_out); 315 316 /** flush pktq */ 317 extern void pktq_flush(osl_t *osh, struct pktq *pq, bool dir); 318 extern void spktq_flush(osl_t *osh, struct spktq *spq, bool dir); 319 /** Empty the queue at particular precedence level */ 320 extern void pktq_pflush(osl_t *osh, struct pktq *pq, int prec, bool dir); 321 322 #ifdef __cplusplus 323 } 324 #endif // endif 325 326 #endif /* _hnd_pktq_h_ */ 327