pmc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (c) 2012-2015 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/errno.h>
  18. #include <linux/fs.h>
  19. #include "wmi.h"
  20. #include "wil6210.h"
  21. #include "txrx.h"
  22. #include "pmc.h"
  23. struct desc_alloc_info {
  24. dma_addr_t pa;
  25. void *va;
  26. };
  27. static int wil_is_pmc_allocated(struct pmc_ctx *pmc)
  28. {
  29. return !!pmc->pring_va;
  30. }
  31. void wil_pmc_init(struct wil6210_priv *wil)
  32. {
  33. memset(&wil->pmc, 0, sizeof(struct pmc_ctx));
  34. mutex_init(&wil->pmc.lock);
  35. }
  36. /**
  37. * Allocate the physical ring (p-ring) and the required
  38. * number of descriptors of required size.
  39. * Initialize the descriptors as required by pmc dma.
  40. * The descriptors' buffers dwords are initialized to hold
  41. * dword's serial number in the lsw and reserved value
  42. * PCM_DATA_INVALID_DW_VAL in the msw.
  43. */
  44. void wil_pmc_alloc(struct wil6210_priv *wil,
  45. int num_descriptors,
  46. int descriptor_size)
  47. {
  48. u32 i;
  49. struct pmc_ctx *pmc = &wil->pmc;
  50. struct device *dev = wil_to_dev(wil);
  51. struct wmi_pmc_cmd pmc_cmd = {0};
  52. mutex_lock(&pmc->lock);
  53. if (wil_is_pmc_allocated(pmc)) {
  54. /* sanity check */
  55. wil_err(wil, "%s: ERROR pmc is already allocated\n", __func__);
  56. goto no_release_err;
  57. }
  58. pmc->num_descriptors = num_descriptors;
  59. pmc->descriptor_size = descriptor_size;
  60. wil_dbg_misc(wil, "%s: %d descriptors x %d bytes each\n",
  61. __func__, num_descriptors, descriptor_size);
  62. /* allocate descriptors info list in pmc context*/
  63. pmc->descriptors = kcalloc(num_descriptors,
  64. sizeof(struct desc_alloc_info),
  65. GFP_KERNEL);
  66. if (!pmc->descriptors) {
  67. wil_err(wil, "%s: ERROR allocating pmc skb list\n", __func__);
  68. goto no_release_err;
  69. }
  70. wil_dbg_misc(wil,
  71. "%s: allocated descriptors info list %p\n",
  72. __func__, pmc->descriptors);
  73. /* Allocate pring buffer and descriptors.
  74. * vring->va should be aligned on its size rounded up to power of 2
  75. * This is granted by the dma_alloc_coherent
  76. */
  77. pmc->pring_va = dma_alloc_coherent(dev,
  78. sizeof(struct vring_tx_desc) * num_descriptors,
  79. &pmc->pring_pa,
  80. GFP_KERNEL);
  81. wil_dbg_misc(wil,
  82. "%s: allocated pring %p => %pad. %zd x %d = total %zd bytes\n",
  83. __func__,
  84. pmc->pring_va, &pmc->pring_pa,
  85. sizeof(struct vring_tx_desc),
  86. num_descriptors,
  87. sizeof(struct vring_tx_desc) * num_descriptors);
  88. if (!pmc->pring_va) {
  89. wil_err(wil, "%s: ERROR allocating pmc pring\n", __func__);
  90. goto release_pmc_skb_list;
  91. }
  92. /* initially, all descriptors are SW owned
  93. * For Tx, Rx, and PMC, ownership bit is at the same location, thus
  94. * we can use any
  95. */
  96. for (i = 0; i < num_descriptors; i++) {
  97. struct vring_tx_desc *_d = &pmc->pring_va[i];
  98. struct vring_tx_desc dd = {}, *d = &dd;
  99. int j = 0;
  100. pmc->descriptors[i].va = dma_alloc_coherent(dev,
  101. descriptor_size,
  102. &pmc->descriptors[i].pa,
  103. GFP_KERNEL);
  104. if (unlikely(!pmc->descriptors[i].va)) {
  105. wil_err(wil,
  106. "%s: ERROR allocating pmc descriptor %d",
  107. __func__, i);
  108. goto release_pmc_skbs;
  109. }
  110. for (j = 0; j < descriptor_size / sizeof(u32); j++) {
  111. u32 *p = (u32 *)pmc->descriptors[i].va + j;
  112. *p = PCM_DATA_INVALID_DW_VAL | j;
  113. }
  114. /* configure dma descriptor */
  115. d->dma.addr.addr_low =
  116. cpu_to_le32(lower_32_bits(pmc->descriptors[i].pa));
  117. d->dma.addr.addr_high =
  118. cpu_to_le16((u16)upper_32_bits(pmc->descriptors[i].pa));
  119. d->dma.status = 0; /* 0 = HW_OWNED */
  120. d->dma.length = cpu_to_le16(descriptor_size);
  121. d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
  122. *_d = *d;
  123. }
  124. wil_dbg_misc(wil, "%s: allocated successfully\n", __func__);
  125. pmc_cmd.op = WMI_PMC_ALLOCATE;
  126. pmc_cmd.ring_size = cpu_to_le16(pmc->num_descriptors);
  127. pmc_cmd.mem_base = cpu_to_le64(pmc->pring_pa);
  128. wil_dbg_misc(wil, "%s: send WMI_PMC_CMD with ALLOCATE op\n", __func__);
  129. pmc->last_cmd_status = wmi_send(wil,
  130. WMI_PMC_CMDID,
  131. &pmc_cmd,
  132. sizeof(pmc_cmd));
  133. if (pmc->last_cmd_status) {
  134. wil_err(wil,
  135. "%s: WMI_PMC_CMD with ALLOCATE op failed with status %d",
  136. __func__, pmc->last_cmd_status);
  137. goto release_pmc_skbs;
  138. }
  139. mutex_unlock(&pmc->lock);
  140. return;
  141. release_pmc_skbs:
  142. wil_err(wil, "%s: exit on error: Releasing skbs...\n", __func__);
  143. for (i = 0; pmc->descriptors[i].va && i < num_descriptors; i++) {
  144. dma_free_coherent(dev,
  145. descriptor_size,
  146. pmc->descriptors[i].va,
  147. pmc->descriptors[i].pa);
  148. pmc->descriptors[i].va = NULL;
  149. }
  150. wil_err(wil, "%s: exit on error: Releasing pring...\n", __func__);
  151. dma_free_coherent(dev,
  152. sizeof(struct vring_tx_desc) * num_descriptors,
  153. pmc->pring_va,
  154. pmc->pring_pa);
  155. pmc->pring_va = NULL;
  156. release_pmc_skb_list:
  157. wil_err(wil, "%s: exit on error: Releasing descriptors info list...\n",
  158. __func__);
  159. kfree(pmc->descriptors);
  160. pmc->descriptors = NULL;
  161. no_release_err:
  162. pmc->last_cmd_status = -ENOMEM;
  163. mutex_unlock(&pmc->lock);
  164. }
  165. /**
  166. * Traverse the p-ring and release all buffers.
  167. * At the end release the p-ring memory
  168. */
  169. void wil_pmc_free(struct wil6210_priv *wil, int send_pmc_cmd)
  170. {
  171. struct pmc_ctx *pmc = &wil->pmc;
  172. struct device *dev = wil_to_dev(wil);
  173. struct wmi_pmc_cmd pmc_cmd = {0};
  174. mutex_lock(&pmc->lock);
  175. pmc->last_cmd_status = 0;
  176. if (!wil_is_pmc_allocated(pmc)) {
  177. wil_dbg_misc(wil, "%s: Error, can't free - not allocated\n",
  178. __func__);
  179. pmc->last_cmd_status = -EPERM;
  180. mutex_unlock(&pmc->lock);
  181. return;
  182. }
  183. if (send_pmc_cmd) {
  184. wil_dbg_misc(wil, "%s: send WMI_PMC_CMD with RELEASE op\n",
  185. __func__);
  186. pmc_cmd.op = WMI_PMC_RELEASE;
  187. pmc->last_cmd_status =
  188. wmi_send(wil, WMI_PMC_CMDID, &pmc_cmd,
  189. sizeof(pmc_cmd));
  190. if (pmc->last_cmd_status) {
  191. wil_err(wil,
  192. "%s WMI_PMC_CMD with RELEASE op failed, status %d",
  193. __func__, pmc->last_cmd_status);
  194. /* There's nothing we can do with this error.
  195. * Normally, it should never occur.
  196. * Continue to freeing all memory allocated for pmc.
  197. */
  198. }
  199. }
  200. if (pmc->pring_va) {
  201. size_t buf_size = sizeof(struct vring_tx_desc) *
  202. pmc->num_descriptors;
  203. wil_dbg_misc(wil, "%s: free pring va %p\n",
  204. __func__, pmc->pring_va);
  205. dma_free_coherent(dev, buf_size, pmc->pring_va, pmc->pring_pa);
  206. pmc->pring_va = NULL;
  207. } else {
  208. pmc->last_cmd_status = -ENOENT;
  209. }
  210. if (pmc->descriptors) {
  211. int i;
  212. for (i = 0;
  213. pmc->descriptors[i].va && i < pmc->num_descriptors; i++) {
  214. dma_free_coherent(dev,
  215. pmc->descriptor_size,
  216. pmc->descriptors[i].va,
  217. pmc->descriptors[i].pa);
  218. pmc->descriptors[i].va = NULL;
  219. }
  220. wil_dbg_misc(wil, "%s: free descriptor info %d/%d\n",
  221. __func__, i, pmc->num_descriptors);
  222. wil_dbg_misc(wil,
  223. "%s: free pmc descriptors info list %p\n",
  224. __func__, pmc->descriptors);
  225. kfree(pmc->descriptors);
  226. pmc->descriptors = NULL;
  227. } else {
  228. pmc->last_cmd_status = -ENOENT;
  229. }
  230. mutex_unlock(&pmc->lock);
  231. }
  232. /**
  233. * Status of the last operation requested via debugfs: alloc/free/read.
  234. * 0 - success or negative errno
  235. */
  236. int wil_pmc_last_cmd_status(struct wil6210_priv *wil)
  237. {
  238. wil_dbg_misc(wil, "%s: status %d\n", __func__,
  239. wil->pmc.last_cmd_status);
  240. return wil->pmc.last_cmd_status;
  241. }
  242. /**
  243. * Read from required position up to the end of current descriptor,
  244. * depends on descriptor size configured during alloc request.
  245. */
  246. ssize_t wil_pmc_read(struct file *filp, char __user *buf, size_t count,
  247. loff_t *f_pos)
  248. {
  249. struct wil6210_priv *wil = filp->private_data;
  250. struct pmc_ctx *pmc = &wil->pmc;
  251. size_t retval = 0;
  252. unsigned long long idx;
  253. loff_t offset;
  254. size_t pmc_size = pmc->descriptor_size * pmc->num_descriptors;
  255. mutex_lock(&pmc->lock);
  256. if (!wil_is_pmc_allocated(pmc)) {
  257. wil_err(wil, "%s: error, pmc is not allocated!\n", __func__);
  258. pmc->last_cmd_status = -EPERM;
  259. mutex_unlock(&pmc->lock);
  260. return -EPERM;
  261. }
  262. wil_dbg_misc(wil,
  263. "%s: size %u, pos %lld\n",
  264. __func__, (unsigned)count, *f_pos);
  265. pmc->last_cmd_status = 0;
  266. idx = *f_pos;
  267. do_div(idx, pmc->descriptor_size);
  268. offset = *f_pos - (idx * pmc->descriptor_size);
  269. if (*f_pos >= pmc_size) {
  270. wil_dbg_misc(wil, "%s: reached end of pmc buf: %lld >= %u\n",
  271. __func__, *f_pos, (unsigned)pmc_size);
  272. pmc->last_cmd_status = -ERANGE;
  273. goto out;
  274. }
  275. wil_dbg_misc(wil,
  276. "%s: read from pos %lld (descriptor %llu, offset %llu) %zu bytes\n",
  277. __func__, *f_pos, idx, offset, count);
  278. /* if no errors, return the copied byte count */
  279. retval = simple_read_from_buffer(buf,
  280. count,
  281. &offset,
  282. pmc->descriptors[idx].va,
  283. pmc->descriptor_size);
  284. *f_pos += retval;
  285. out:
  286. mutex_unlock(&pmc->lock);
  287. return retval;
  288. }
  289. loff_t wil_pmc_llseek(struct file *filp, loff_t off, int whence)
  290. {
  291. loff_t newpos;
  292. struct wil6210_priv *wil = filp->private_data;
  293. struct pmc_ctx *pmc = &wil->pmc;
  294. size_t pmc_size = pmc->descriptor_size * pmc->num_descriptors;
  295. switch (whence) {
  296. case 0: /* SEEK_SET */
  297. newpos = off;
  298. break;
  299. case 1: /* SEEK_CUR */
  300. newpos = filp->f_pos + off;
  301. break;
  302. case 2: /* SEEK_END */
  303. newpos = pmc_size;
  304. break;
  305. default: /* can't happen */
  306. return -EINVAL;
  307. }
  308. if (newpos < 0)
  309. return -EINVAL;
  310. if (newpos > pmc_size)
  311. newpos = pmc_size;
  312. filp->f_pos = newpos;
  313. return newpos;
  314. }