| 1 |
#include "xframe_queue.h" |
| 2 |
#include "xbus-core.h" |
| 3 |
#include "dahdi_debug.h" |
| 4 |
|
| 5 |
extern int debug; |
| 6 |
|
| 7 |
static xframe_t *transport_alloc_xframe(xbus_t *xbus, gfp_t gfp_flags); |
| 8 |
static void transport_free_xframe(xbus_t *xbus, xframe_t *xframe); |
| 9 |
|
| 10 |
void xframe_queue_init(struct xframe_queue *q, |
| 11 |
unsigned int steady_state_count, unsigned int max_count, |
| 12 |
const char *name, void *priv) |
| 13 |
{ |
| 14 |
memset(q, 0, sizeof(*q)); |
| 15 |
spin_lock_init(&q->lock); |
| 16 |
INIT_LIST_HEAD(&q->head); |
| 17 |
q->max_count = XFRAME_QUEUE_MARGIN + max_count; |
| 18 |
q->steady_state_count = XFRAME_QUEUE_MARGIN + steady_state_count; |
| 19 |
q->name = name; |
| 20 |
q->priv = priv; |
| 21 |
} |
| 22 |
EXPORT_SYMBOL(xframe_queue_init); |
| 23 |
|
| 24 |
void xframe_queue_clearstats(struct xframe_queue *q) |
| 25 |
{ |
| 26 |
q->worst_count = 0; |
| 27 |
|
| 28 |
q->worst_lag_usec = 0L; |
| 29 |
} |
| 30 |
EXPORT_SYMBOL(xframe_queue_clearstats); |
| 31 |
|
| 32 |
static void __xframe_dump_queue(struct xframe_queue *q) |
| 33 |
{ |
| 34 |
xframe_t *xframe; |
| 35 |
int i = 0; |
| 36 |
char prefix[30]; |
| 37 |
ktime_t now = ktime_get(); |
| 38 |
|
| 39 |
printk(KERN_DEBUG "%s: dump queue '%s' (first packet in each frame)\n", |
| 40 |
THIS_MODULE->name, q->name); |
| 41 |
list_for_each_entry_reverse(xframe, &q->head, frame_list) { |
| 42 |
xpacket_t *pack = (xpacket_t *)&xframe->packets[0]; |
| 43 |
s64 usec = ktime_us_delta(now, xframe->kt_queued); |
| 44 |
|
| 45 |
snprintf(prefix, ARRAY_SIZE(prefix), " %3d> %5lld.%03lld msec", |
| 46 |
i++, usec / 1000, usec % 1000); |
| 47 |
dump_packet(prefix, pack, 1); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
static bool __xframe_enqueue(struct xframe_queue *q, xframe_t *xframe) |
| 52 |
{ |
| 53 |
int ret = 1; |
| 54 |
static int overflow_cnt; |
| 55 |
|
| 56 |
if (unlikely(q->disabled)) { |
| 57 |
ret = 0; |
| 58 |
goto out; |
| 59 |
} |
| 60 |
if (q->count >= q->max_count) { |
| 61 |
q->overflows++; |
| 62 |
if ((overflow_cnt++ % 1000) < 5) { |
| 63 |
NOTICE("Overflow of %-15s: counts %3d, %3d, %3d worst %3d, overflows %3d worst_lag %02lld.%lld ms\n", |
| 64 |
q->name, q->steady_state_count, q->count, |
| 65 |
q->max_count, q->worst_count, q->overflows, |
| 66 |
q->worst_lag_usec / 1000, |
| 67 |
q->worst_lag_usec % 1000); |
| 68 |
__xframe_dump_queue(q); |
| 69 |
} |
| 70 |
ret = 0; |
| 71 |
goto out; |
| 72 |
} |
| 73 |
if (++q->count > q->worst_count) |
| 74 |
q->worst_count = q->count; |
| 75 |
list_add_tail(&xframe->frame_list, &q->head); |
| 76 |
xframe->kt_queued = ktime_get(); |
| 77 |
out: |
| 78 |
return ret; |
| 79 |
} |
| 80 |
|
| 81 |
bool xframe_enqueue(struct xframe_queue *q, xframe_t *xframe) |
| 82 |
{ |
| 83 |
unsigned long flags; |
| 84 |
int ret; |
| 85 |
|
| 86 |
spin_lock_irqsave(&q->lock, flags); |
| 87 |
ret = __xframe_enqueue(q, xframe); |
| 88 |
spin_unlock_irqrestore(&q->lock, flags); |
| 89 |
return ret; |
| 90 |
} |
| 91 |
EXPORT_SYMBOL(xframe_enqueue); |
| 92 |
|
| 93 |
static xframe_t *__xframe_dequeue(struct xframe_queue *q) |
| 94 |
{ |
| 95 |
xframe_t *frm = NULL; |
| 96 |
struct list_head *h; |
| 97 |
ktime_t now; |
| 98 |
s64 usec_lag; |
| 99 |
|
| 100 |
if (list_empty(&q->head)) |
| 101 |
goto out; |
| 102 |
h = q->head.next; |
| 103 |
list_del_init(h); |
| 104 |
--q->count; |
| 105 |
frm = list_entry(h, xframe_t, frame_list); |
| 106 |
now = ktime_get(); |
| 107 |
usec_lag = ktime_us_delta(now, frm->kt_queued); |
| 108 |
if (q->worst_lag_usec < usec_lag) |
| 109 |
q->worst_lag_usec = usec_lag; |
| 110 |
out: |
| 111 |
return frm; |
| 112 |
} |
| 113 |
|
| 114 |
xframe_t *xframe_dequeue(struct xframe_queue *q) |
| 115 |
{ |
| 116 |
unsigned long flags; |
| 117 |
xframe_t *frm; |
| 118 |
|
| 119 |
spin_lock_irqsave(&q->lock, flags); |
| 120 |
frm = __xframe_dequeue(q); |
| 121 |
spin_unlock_irqrestore(&q->lock, flags); |
| 122 |
return frm; |
| 123 |
} |
| 124 |
EXPORT_SYMBOL(xframe_dequeue); |
| 125 |
|
| 126 |
void xframe_queue_disable(struct xframe_queue *q, bool disabled) |
| 127 |
{ |
| 128 |
q->disabled = disabled; |
| 129 |
} |
| 130 |
EXPORT_SYMBOL(xframe_queue_disable); |
| 131 |
|
| 132 |
void xframe_queue_clear(struct xframe_queue *q) |
| 133 |
{ |
| 134 |
xframe_t *xframe; |
| 135 |
xbus_t *xbus = q->priv; |
| 136 |
int i = 0; |
| 137 |
|
| 138 |
xframe_queue_disable(q, 1); |
| 139 |
while ((xframe = xframe_dequeue(q)) != NULL) { |
| 140 |
transport_free_xframe(xbus, xframe); |
| 141 |
i++; |
| 142 |
} |
| 143 |
XBUS_DBG(DEVICES, xbus, "%s: finished queue clear (%d items)\n", |
| 144 |
q->name, i); |
| 145 |
} |
| 146 |
EXPORT_SYMBOL(xframe_queue_clear); |
| 147 |
|
| 148 |
uint xframe_queue_count(struct xframe_queue *q) |
| 149 |
{ |
| 150 |
return q->count; |
| 151 |
} |
| 152 |
EXPORT_SYMBOL(xframe_queue_count); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static xframe_t *transport_alloc_xframe(xbus_t *xbus, gfp_t gfp_flags) |
| 157 |
{ |
| 158 |
struct xbus_ops *ops; |
| 159 |
xframe_t *xframe; |
| 160 |
unsigned long flags; |
| 161 |
|
| 162 |
BUG_ON(!xbus); |
| 163 |
ops = transportops_get(xbus); |
| 164 |
if (unlikely(!ops)) { |
| 165 |
XBUS_ERR(xbus, "Missing transport\n"); |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
spin_lock_irqsave(&xbus->transport.lock, flags); |
| 169 |
#if 0 |
| 170 |
XBUS_INFO(xbus, "%s (transport_refcount=%d)\n", |
| 171 |
__func__, atomic_read(&xbus->transport.transport_refcount)); |
| 172 |
#endif |
| 173 |
xframe = ops->alloc_xframe(xbus, gfp_flags); |
| 174 |
if (!xframe) { |
| 175 |
static int rate_limit; |
| 176 |
|
| 177 |
if ((rate_limit++ % 3001) == 0) |
| 178 |
XBUS_ERR(xbus, |
| 179 |
"Failed xframe allocation from transport (%d)\n", |
| 180 |
rate_limit); |
| 181 |
transportops_put(xbus); |
| 182 |
|
| 183 |
} |
| 184 |
spin_unlock_irqrestore(&xbus->transport.lock, flags); |
| 185 |
return xframe; |
| 186 |
} |
| 187 |
|
| 188 |
static void transport_free_xframe(xbus_t *xbus, xframe_t *xframe) |
| 189 |
{ |
| 190 |
struct xbus_ops *ops; |
| 191 |
unsigned long flags; |
| 192 |
|
| 193 |
BUG_ON(!xbus); |
| 194 |
ops = xbus->transport.ops; |
| 195 |
BUG_ON(!ops); |
| 196 |
spin_lock_irqsave(&xbus->transport.lock, flags); |
| 197 |
#if 0 |
| 198 |
XBUS_INFO(xbus, "%s (transport_refcount=%d)\n", |
| 199 |
__func__, atomic_read(&xbus->transport.transport_refcount)); |
| 200 |
#endif |
| 201 |
ops->free_xframe(xbus, xframe); |
| 202 |
transportops_put(xbus); |
| 203 |
spin_unlock_irqrestore(&xbus->transport.lock, flags); |
| 204 |
} |
| 205 |
|
| 206 |
static bool xframe_queue_adjust(struct xframe_queue *q) |
| 207 |
{ |
| 208 |
xbus_t *xbus; |
| 209 |
xframe_t *xframe; |
| 210 |
int delta; |
| 211 |
unsigned long flags; |
| 212 |
int ret = 0; |
| 213 |
|
| 214 |
BUG_ON(!q); |
| 215 |
xbus = q->priv; |
| 216 |
BUG_ON(!xbus); |
| 217 |
spin_lock_irqsave(&q->lock, flags); |
| 218 |
delta = q->count - q->steady_state_count; |
| 219 |
if (delta < -XFRAME_QUEUE_MARGIN) { |
| 220 |
|
| 221 |
|
| 222 |
xframe = transport_alloc_xframe(xbus, GFP_ATOMIC); |
| 223 |
if (!xframe) { |
| 224 |
static int rate_limit; |
| 225 |
|
| 226 |
if ((rate_limit++ % 3001) == 0) |
| 227 |
XBUS_ERR(xbus, "%s: failed frame allocation\n", |
| 228 |
q->name); |
| 229 |
goto out; |
| 230 |
} |
| 231 |
if (!__xframe_enqueue(q, xframe)) { |
| 232 |
static int rate_limit; |
| 233 |
|
| 234 |
if ((rate_limit++ % 3001) == 0) |
| 235 |
XBUS_ERR(xbus, "%s: failed enqueueing frame\n", |
| 236 |
q->name); |
| 237 |
transport_free_xframe(xbus, xframe); |
| 238 |
goto out; |
| 239 |
} |
| 240 |
} else if (delta > XFRAME_QUEUE_MARGIN) { |
| 241 |
|
| 242 |
|
| 243 |
xframe = __xframe_dequeue(q); |
| 244 |
if (!xframe) { |
| 245 |
static int rate_limit; |
| 246 |
|
| 247 |
if ((rate_limit++ % 3001) == 0) |
| 248 |
XBUS_ERR(xbus, "%s: failed dequeueing frame\n", |
| 249 |
q->name); |
| 250 |
goto out; |
| 251 |
} |
| 252 |
transport_free_xframe(xbus, xframe); |
| 253 |
} |
| 254 |
ret = 1; |
| 255 |
out: |
| 256 |
spin_unlock_irqrestore(&q->lock, flags); |
| 257 |
return ret; |
| 258 |
} |
| 259 |
|
| 260 |
xframe_t *get_xframe(struct xframe_queue *q) |
| 261 |
{ |
| 262 |
xframe_t *xframe; |
| 263 |
xbus_t *xbus; |
| 264 |
|
| 265 |
BUG_ON(!q); |
| 266 |
xbus = (xbus_t *)q->priv; |
| 267 |
BUG_ON(!xbus); |
| 268 |
xframe_queue_adjust(q); |
| 269 |
xframe = xframe_dequeue(q); |
| 270 |
if (!xframe) { |
| 271 |
static int rate_limit; |
| 272 |
|
| 273 |
if ((rate_limit++ % 3001) == 0) |
| 274 |
XBUS_ERR(xbus, "%s STILL EMPTY (%d)\n", q->name, |
| 275 |
rate_limit); |
| 276 |
return NULL; |
| 277 |
} |
| 278 |
BUG_ON(xframe->xframe_magic != XFRAME_MAGIC); |
| 279 |
atomic_set(&xframe->frame_len, 0); |
| 280 |
xframe->first_free = xframe->packets; |
| 281 |
xframe->kt_created = ktime_get(); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
return xframe; |
| 292 |
} |
| 293 |
EXPORT_SYMBOL(get_xframe); |
| 294 |
|
| 295 |
void put_xframe(struct xframe_queue *q, xframe_t *xframe) |
| 296 |
{ |
| 297 |
xbus_t *xbus; |
| 298 |
|
| 299 |
BUG_ON(!q); |
| 300 |
xbus = (xbus_t *)q->priv; |
| 301 |
BUG_ON(!xbus); |
| 302 |
|
| 303 |
BUG_ON(!TRANSPORT_EXIST(xbus)); |
| 304 |
if (unlikely(!xframe_enqueue(q, xframe))) { |
| 305 |
XBUS_ERR(xbus, "Failed returning xframe to %s\n", q->name); |
| 306 |
transport_free_xframe(xbus, xframe); |
| 307 |
return; |
| 308 |
} |
| 309 |
xframe_queue_adjust(q); |
| 310 |
} |
| 311 |
EXPORT_SYMBOL(put_xframe); |