dhd_dbg_ring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * DHD debug ring API and structures
  3. *
  4. * <<Broadcom-WL-IPTag/Open:>>
  5. *
  6. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  7. *
  8. * Copyright (C) 1999-2020, Broadcom Corporation
  9. *
  10. * Unless you and Broadcom execute a separate written software license
  11. * agreement governing use of this software, this software is licensed to you
  12. * under the terms of the GNU General Public License version 2 (the "GPL"),
  13. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  14. * following added to such license:
  15. *
  16. * As a special exception, the copyright holders of this software give you
  17. * permission to link this software with independent modules, and to copy and
  18. * distribute the resulting executable under terms of your choice, provided that
  19. * you also meet, for each linked independent module, the terms and conditions of
  20. * the license of that module. An independent module is a module which is not
  21. * derived from this software. The special exception does not apply to any
  22. * modifications of the software.
  23. *
  24. * Notwithstanding the above, under no circumstances may you combine this
  25. * software in any way with any other Broadcom software provided under a license
  26. * other than the GPL, without Broadcom's express prior written consent.
  27. *
  28. * $Id: dhd_dbg_ring.c 716964 2019-06-06 20:35:46Z $
  29. */
  30. #include <typedefs.h>
  31. #include <osl.h>
  32. #include <bcmutils.h>
  33. #include <bcmendian.h>
  34. #include <dngl_stats.h>
  35. #include <dhd.h>
  36. #include <dhd_dbg.h>
  37. #include <dhd_dbg_ring.h>
  38. int
  39. dhd_dbg_ring_init(dhd_pub_t *dhdp, dhd_dbg_ring_t *ring, uint16 id, uint8 *name,
  40. uint32 ring_sz, void *allocd_buf, bool pull_inactive)
  41. {
  42. void *buf;
  43. unsigned long flags = 0;
  44. if (allocd_buf == NULL) {
  45. return BCME_NOMEM;
  46. } else {
  47. buf = allocd_buf;
  48. }
  49. ring->lock = DHD_DBG_RING_LOCK_INIT(dhdp->osh);
  50. if (!ring->lock)
  51. return BCME_NOMEM;
  52. DHD_DBG_RING_LOCK(ring->lock, flags);
  53. ring->id = id;
  54. strncpy(ring->name, name, DBGRING_NAME_MAX);
  55. ring->name[DBGRING_NAME_MAX - 1] = 0;
  56. ring->ring_size = ring_sz;
  57. ring->wp = ring->rp = 0;
  58. ring->ring_buf = buf;
  59. ring->threshold = DBGRING_FLUSH_THRESHOLD(ring);
  60. ring->state = RING_SUSPEND;
  61. ring->rem_len = 0;
  62. ring->sched_pull = TRUE;
  63. ring->pull_inactive = pull_inactive;
  64. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  65. return BCME_OK;
  66. }
  67. void
  68. dhd_dbg_ring_deinit(dhd_pub_t *dhdp, dhd_dbg_ring_t *ring)
  69. {
  70. unsigned long flags = 0;
  71. DHD_DBG_RING_LOCK(ring->lock, flags);
  72. ring->id = 0;
  73. ring->name[0] = 0;
  74. ring->wp = ring->rp = 0;
  75. memset(&ring->stat, 0, sizeof(ring->stat));
  76. ring->threshold = 0;
  77. ring->state = RING_STOP;
  78. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  79. DHD_DBG_RING_LOCK_DEINIT(dhdp->osh, ring->lock);
  80. }
  81. void
  82. dhd_dbg_ring_sched_pull(dhd_dbg_ring_t *ring, uint32 pending_len,
  83. os_pullreq_t pull_fn, void *os_pvt, const int id)
  84. {
  85. unsigned long flags = 0;
  86. DHD_DBG_RING_LOCK(ring->lock, flags);
  87. /* if the current pending size is bigger than threshold and
  88. * threshold is set
  89. */
  90. if (ring->threshold > 0 &&
  91. (pending_len >= ring->threshold) && ring->sched_pull) {
  92. /*
  93. * Update the state and release the lock before calling
  94. * the pull_fn. Do not transfer control to other layers
  95. * with locks held. If the call back again calls into
  96. * the same layer fro this context, can lead to deadlock.
  97. */
  98. ring->sched_pull = FALSE;
  99. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  100. pull_fn(os_pvt, id);
  101. } else {
  102. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  103. }
  104. }
  105. uint32
  106. dhd_dbg_ring_get_pending_len(dhd_dbg_ring_t *ring)
  107. {
  108. uint32 pending_len = 0;
  109. unsigned long flags = 0;
  110. DHD_DBG_RING_LOCK(ring->lock, flags);
  111. if (ring->stat.written_bytes > ring->stat.read_bytes) {
  112. pending_len = ring->stat.written_bytes - ring->stat.read_bytes;
  113. } else if (ring->stat.written_bytes < ring->stat.read_bytes) {
  114. pending_len = PENDING_LEN_MAX - ring->stat.read_bytes + ring->stat.written_bytes;
  115. } else {
  116. pending_len = 0;
  117. }
  118. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  119. return pending_len;
  120. }
  121. int
  122. dhd_dbg_ring_push(dhd_dbg_ring_t *ring, dhd_dbg_ring_entry_t *hdr, void *data)
  123. {
  124. unsigned long flags;
  125. uint32 w_len;
  126. uint32 avail_size;
  127. dhd_dbg_ring_entry_t *w_entry, *r_entry;
  128. if (!ring || !hdr || !data) {
  129. return BCME_BADARG;
  130. }
  131. DHD_DBG_RING_LOCK(ring->lock, flags);
  132. if (ring->state != RING_ACTIVE) {
  133. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  134. return BCME_OK;
  135. }
  136. w_len = ENTRY_LENGTH(hdr);
  137. DHD_DBGIF(("%s: RING%d[%s] hdr->len=%u, w_len=%u, wp=%d, rp=%d, ring_start=0x%p;"
  138. " ring_size=%u\n",
  139. __FUNCTION__, ring->id, ring->name, hdr->len, w_len, ring->wp, ring->rp,
  140. ring->ring_buf, ring->ring_size));
  141. if (w_len > ring->ring_size) {
  142. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  143. DHD_ERROR(("%s: RING%d[%s] w_len=%u, ring_size=%u,"
  144. " write size exceeds ring size !\n",
  145. __FUNCTION__, ring->id, ring->name, w_len, ring->ring_size));
  146. return BCME_BUFTOOLONG;
  147. }
  148. /* Claim the space */
  149. do {
  150. avail_size = DBG_RING_CHECK_WRITE_SPACE(ring->rp, ring->wp, ring->ring_size);
  151. if (avail_size <= w_len) {
  152. /* Prepare the space */
  153. if (ring->rp <= ring->wp) {
  154. ring->tail_padded = TRUE;
  155. ring->rem_len = ring->ring_size - ring->wp;
  156. DHD_DBGIF(("%s: RING%d[%s] Insuffient tail space,"
  157. " rp=%d, wp=%d, rem_len=%d, ring_size=%d,"
  158. " avail_size=%d, w_len=%d\n", __FUNCTION__,
  159. ring->id, ring->name, ring->rp, ring->wp,
  160. ring->rem_len, ring->ring_size, avail_size,
  161. w_len));
  162. /* 0 pad insufficient tail space */
  163. memset((uint8 *)ring->ring_buf + ring->wp, 0, ring->rem_len);
  164. /* If read pointer is still at the beginning, make some room */
  165. if (ring->rp == 0) {
  166. r_entry = (dhd_dbg_ring_entry_t *)((uint8 *)ring->ring_buf +
  167. ring->rp);
  168. ring->rp += ENTRY_LENGTH(r_entry);
  169. ring->stat.read_bytes += ENTRY_LENGTH(r_entry);
  170. DHD_DBGIF(("%s: rp at 0, move by one entry length"
  171. " (%u bytes)\n",
  172. __FUNCTION__, (uint32)ENTRY_LENGTH(r_entry)));
  173. }
  174. if (ring->rp == ring->wp) {
  175. ring->rp = 0;
  176. }
  177. ring->wp = 0;
  178. DHD_DBGIF(("%s: new rp=%u, wp=%u\n",
  179. __FUNCTION__, ring->rp, ring->wp));
  180. } else {
  181. /* Not enough space for new entry, free some up */
  182. r_entry = (dhd_dbg_ring_entry_t *)((uint8 *)ring->ring_buf +
  183. ring->rp);
  184. /* check bounds before incrementing read ptr */
  185. if (ring->rp + ENTRY_LENGTH(r_entry) >= ring->ring_size) {
  186. DHD_ERROR(("%s: RING%d[%s] rp points out of boundary, "
  187. "ring->wp=%u, ring->rp=%u, ring->ring_size=%d\n",
  188. __FUNCTION__, ring->id, ring->name, ring->wp,
  189. ring->rp, ring->ring_size));
  190. ASSERT(0);
  191. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  192. return BCME_BUFTOOSHORT;
  193. }
  194. ring->rp += ENTRY_LENGTH(r_entry);
  195. /* skip padding if there is one */
  196. if (ring->tail_padded &&
  197. ((ring->rp + ring->rem_len) == ring->ring_size)) {
  198. DHD_DBGIF(("%s: RING%d[%s] Found padding,"
  199. " avail_size=%d, w_len=%d, set rp=0\n",
  200. __FUNCTION__, ring->id, ring->name,
  201. avail_size, w_len));
  202. ring->rp = 0;
  203. ring->tail_padded = FALSE;
  204. ring->rem_len = 0;
  205. }
  206. ring->stat.read_bytes += ENTRY_LENGTH(r_entry);
  207. DHD_DBGIF(("%s: RING%d[%s] read_bytes=%d, wp=%d, rp=%d\n",
  208. __FUNCTION__, ring->id, ring->name, ring->stat.read_bytes,
  209. ring->wp, ring->rp));
  210. }
  211. } else {
  212. break;
  213. }
  214. } while (TRUE);
  215. /* check before writing to the ring */
  216. if (ring->wp + w_len >= ring->ring_size) {
  217. DHD_ERROR(("%s: RING%d[%s] wp pointed out of ring boundary, "
  218. "wp=%d, ring_size=%d, w_len=%u\n", __FUNCTION__, ring->id,
  219. ring->name, ring->wp, ring->ring_size, w_len));
  220. ASSERT(0);
  221. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  222. return BCME_BUFTOOLONG;
  223. }
  224. w_entry = (dhd_dbg_ring_entry_t *)((uint8 *)ring->ring_buf + ring->wp);
  225. /* header */
  226. memcpy(w_entry, hdr, DBG_RING_ENTRY_SIZE);
  227. w_entry->len = hdr->len;
  228. /* payload */
  229. memcpy((char *)w_entry + DBG_RING_ENTRY_SIZE, data, w_entry->len);
  230. /* update write pointer */
  231. ring->wp += w_len;
  232. /* update statistics */
  233. ring->stat.written_records++;
  234. ring->stat.written_bytes += w_len;
  235. DHD_DBGIF(("%s : RING%d[%s] written_records %d, written_bytes %d, read_bytes=%d,"
  236. " ring->threshold=%d, wp=%d, rp=%d\n", __FUNCTION__, ring->id, ring->name,
  237. ring->stat.written_records, ring->stat.written_bytes, ring->stat.read_bytes,
  238. ring->threshold, ring->wp, ring->rp));
  239. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  240. return BCME_OK;
  241. }
  242. /*
  243. * This function folds ring->lock, so callers of this function
  244. * should not hold ring->lock.
  245. */
  246. int
  247. dhd_dbg_ring_pull_single(dhd_dbg_ring_t *ring, void *data, uint32 buf_len, bool strip_header)
  248. {
  249. dhd_dbg_ring_entry_t *r_entry = NULL;
  250. uint32 rlen = 0;
  251. char *buf = NULL;
  252. unsigned long flags;
  253. if (!ring || !data || buf_len <= 0) {
  254. return 0;
  255. }
  256. DHD_DBG_RING_LOCK(ring->lock, flags);
  257. /* pull from ring is allowed for inactive (suspended) ring
  258. * in case of ecounters only, this is because, for ecounters
  259. * when a trap occurs the ring is suspended and data is then
  260. * pulled to dump it to a file. For other rings if ring is
  261. * not in active state return without processing (as before)
  262. */
  263. if (!ring->pull_inactive && (ring->state != RING_ACTIVE)) {
  264. goto exit;
  265. }
  266. if (ring->rp == ring->wp) {
  267. goto exit;
  268. }
  269. DHD_DBGIF(("%s: RING%d[%s] buf_len=%u, wp=%d, rp=%d, ring_start=0x%p; ring_size=%u\n",
  270. __FUNCTION__, ring->id, ring->name, buf_len, ring->wp, ring->rp,
  271. ring->ring_buf, ring->ring_size));
  272. r_entry = (dhd_dbg_ring_entry_t *)((uint8 *)ring->ring_buf + ring->rp);
  273. /* Boundary Check */
  274. rlen = ENTRY_LENGTH(r_entry);
  275. if ((ring->rp + rlen) > ring->ring_size) {
  276. DHD_ERROR(("%s: entry len %d is out of boundary of ring size %d,"
  277. " current ring %d[%s] - rp=%d\n", __FUNCTION__, rlen,
  278. ring->ring_size, ring->id, ring->name, ring->rp));
  279. rlen = 0;
  280. goto exit;
  281. }
  282. if (strip_header) {
  283. rlen = r_entry->len;
  284. buf = (char *)r_entry + DBG_RING_ENTRY_SIZE;
  285. } else {
  286. rlen = ENTRY_LENGTH(r_entry);
  287. buf = (char *)r_entry;
  288. }
  289. if (rlen > buf_len) {
  290. DHD_ERROR(("%s: buf len %d is too small for entry len %d\n",
  291. __FUNCTION__, buf_len, rlen));
  292. DHD_ERROR(("%s: ring %d[%s] - ring size=%d, wp=%d, rp=%d\n",
  293. __FUNCTION__, ring->id, ring->name, ring->ring_size,
  294. ring->wp, ring->rp));
  295. ASSERT(0);
  296. rlen = 0;
  297. goto exit;
  298. }
  299. memcpy(data, buf, rlen);
  300. /* update ring context */
  301. ring->rp += ENTRY_LENGTH(r_entry);
  302. /* don't pass wp but skip padding if there is one */
  303. if (ring->rp != ring->wp &&
  304. ring->tail_padded && ((ring->rp + ring->rem_len) >= ring->ring_size)) {
  305. DHD_DBGIF(("%s: RING%d[%s] Found padding, rp=%d, wp=%d\n",
  306. __FUNCTION__, ring->id, ring->name, ring->rp, ring->wp));
  307. ring->rp = 0;
  308. ring->tail_padded = FALSE;
  309. ring->rem_len = 0;
  310. }
  311. if (ring->rp >= ring->ring_size) {
  312. DHD_ERROR(("%s: RING%d[%s] rp pointed out of ring boundary,"
  313. " rp=%d, ring_size=%d\n", __FUNCTION__, ring->id,
  314. ring->name, ring->rp, ring->ring_size));
  315. ASSERT(0);
  316. rlen = 0;
  317. goto exit;
  318. }
  319. ring->stat.read_bytes += ENTRY_LENGTH(r_entry);
  320. DHD_DBGIF(("%s RING%d[%s]read_bytes %d, wp=%d, rp=%d\n", __FUNCTION__,
  321. ring->id, ring->name, ring->stat.read_bytes, ring->wp, ring->rp));
  322. exit:
  323. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  324. return rlen;
  325. }
  326. int
  327. dhd_dbg_ring_pull(dhd_dbg_ring_t *ring, void *data, uint32 buf_len, bool strip_hdr)
  328. {
  329. int32 r_len, total_r_len = 0;
  330. unsigned long flags;
  331. if (!ring || !data)
  332. return 0;
  333. DHD_DBG_RING_LOCK(ring->lock, flags);
  334. if (!ring->pull_inactive && (ring->state != RING_ACTIVE)) {
  335. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  336. return 0;
  337. }
  338. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  339. while (buf_len > 0) {
  340. r_len = dhd_dbg_ring_pull_single(ring, data, buf_len, strip_hdr);
  341. if (r_len == 0)
  342. break;
  343. data = (uint8 *)data + r_len;
  344. buf_len -= r_len;
  345. total_r_len += r_len;
  346. }
  347. return total_r_len;
  348. }
  349. int
  350. dhd_dbg_ring_config(dhd_dbg_ring_t *ring, int log_level, uint32 threshold)
  351. {
  352. unsigned long flags = 0;
  353. if (!ring)
  354. return BCME_BADADDR;
  355. if (ring->state == RING_STOP)
  356. return BCME_UNSUPPORTED;
  357. DHD_DBG_RING_LOCK(ring->lock, flags);
  358. if (log_level == 0)
  359. ring->state = RING_SUSPEND;
  360. else
  361. ring->state = RING_ACTIVE;
  362. ring->log_level = log_level;
  363. ring->threshold = MIN(threshold, DBGRING_FLUSH_THRESHOLD(ring));
  364. DHD_DBG_RING_UNLOCK(ring->lock, flags);
  365. return BCME_OK;
  366. }
  367. void
  368. dhd_dbg_ring_start(dhd_dbg_ring_t *ring)
  369. {
  370. if (!ring)
  371. return;
  372. /* Initialize the information for the ring */
  373. ring->state = RING_SUSPEND;
  374. ring->log_level = 0;
  375. ring->rp = ring->wp = 0;
  376. ring->threshold = 0;
  377. memset(&ring->stat, 0, sizeof(struct ring_statistics));
  378. memset(ring->ring_buf, 0, ring->ring_size);
  379. }