pmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) 2012-2015,2017 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. int last_cmd_err = -ENOMEM;
  53. mutex_lock(&pmc->lock);
  54. if (wil_is_pmc_allocated(pmc)) {
  55. /* sanity check */
  56. wil_err(wil, "ERROR pmc is already allocated\n");
  57. goto no_release_err;
  58. }
  59. if ((num_descriptors <= 0) || (descriptor_size <= 0)) {
  60. wil_err(wil,
  61. "Invalid params num_descriptors(%d), descriptor_size(%d)\n",
  62. num_descriptors, descriptor_size);
  63. last_cmd_err = -EINVAL;
  64. goto no_release_err;
  65. }
  66. if (num_descriptors > (1 << WIL_RING_SIZE_ORDER_MAX)) {
  67. wil_err(wil,
  68. "num_descriptors(%d) exceeds max ring size %d\n",
  69. num_descriptors, 1 << WIL_RING_SIZE_ORDER_MAX);
  70. last_cmd_err = -EINVAL;
  71. goto no_release_err;
  72. }
  73. if (num_descriptors > INT_MAX / descriptor_size) {
  74. wil_err(wil,
  75. "Overflow in num_descriptors(%d)*descriptor_size(%d)\n",
  76. num_descriptors, descriptor_size);
  77. last_cmd_err = -EINVAL;
  78. goto no_release_err;
  79. }
  80. pmc->num_descriptors = num_descriptors;
  81. pmc->descriptor_size = descriptor_size;
  82. wil_dbg_misc(wil, "pmc_alloc: %d descriptors x %d bytes each\n",
  83. num_descriptors, descriptor_size);
  84. /* allocate descriptors info list in pmc context*/
  85. pmc->descriptors = kcalloc(num_descriptors,
  86. sizeof(struct desc_alloc_info),
  87. GFP_KERNEL);
  88. if (!pmc->descriptors) {
  89. wil_err(wil, "ERROR allocating pmc skb list\n");
  90. goto no_release_err;
  91. }
  92. wil_dbg_misc(wil, "pmc_alloc: allocated descriptors info list %p\n",
  93. pmc->descriptors);
  94. /* Allocate pring buffer and descriptors.
  95. * vring->va should be aligned on its size rounded up to power of 2
  96. * This is granted by the dma_alloc_coherent
  97. */
  98. pmc->pring_va = dma_alloc_coherent(dev,
  99. sizeof(struct vring_tx_desc) * num_descriptors,
  100. &pmc->pring_pa,
  101. GFP_KERNEL);
  102. wil_dbg_misc(wil,
  103. "pmc_alloc: allocated pring %p => %pad. %zd x %d = total %zd bytes\n",
  104. pmc->pring_va, &pmc->pring_pa,
  105. sizeof(struct vring_tx_desc),
  106. num_descriptors,
  107. sizeof(struct vring_tx_desc) * num_descriptors);
  108. if (!pmc->pring_va) {
  109. wil_err(wil, "ERROR allocating pmc pring\n");
  110. goto release_pmc_skb_list;
  111. }
  112. /* initially, all descriptors are SW owned
  113. * For Tx, Rx, and PMC, ownership bit is at the same location, thus
  114. * we can use any
  115. */
  116. for (i = 0; i < num_descriptors; i++) {
  117. struct vring_tx_desc *_d = &pmc->pring_va[i];
  118. struct vring_tx_desc dd = {}, *d = &dd;
  119. int j = 0;
  120. pmc->descriptors[i].va = dma_alloc_coherent(dev,
  121. descriptor_size,
  122. &pmc->descriptors[i].pa,
  123. GFP_KERNEL);
  124. if (unlikely(!pmc->descriptors[i].va)) {
  125. wil_err(wil, "ERROR allocating pmc descriptor %d", i);
  126. goto release_pmc_skbs;
  127. }
  128. for (j = 0; j < descriptor_size / sizeof(u32); j++) {
  129. u32 *p = (u32 *)pmc->descriptors[i].va + j;
  130. *p = PCM_DATA_INVALID_DW_VAL | j;
  131. }
  132. /* configure dma descriptor */
  133. d->dma.addr.addr_low =
  134. cpu_to_le32(lower_32_bits(pmc->descriptors[i].pa));
  135. d->dma.addr.addr_high =
  136. cpu_to_le16((u16)upper_32_bits(pmc->descriptors[i].pa));
  137. d->dma.status = 0; /* 0 = HW_OWNED */
  138. d->dma.length = cpu_to_le16(descriptor_size);
  139. d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
  140. *_d = *d;
  141. }
  142. wil_dbg_misc(wil, "pmc_alloc: allocated successfully\n");
  143. pmc_cmd.op = WMI_PMC_ALLOCATE;
  144. pmc_cmd.ring_size = cpu_to_le16(pmc->num_descriptors);
  145. pmc_cmd.mem_base = cpu_to_le64(pmc->pring_pa);
  146. wil_dbg_misc(wil, "pmc_alloc: send WMI_PMC_CMD with ALLOCATE op\n");
  147. pmc->last_cmd_status = wmi_send(wil,
  148. WMI_PMC_CMDID,
  149. &pmc_cmd,
  150. sizeof(pmc_cmd));
  151. if (pmc->last_cmd_status) {
  152. wil_err(wil,
  153. "WMI_PMC_CMD with ALLOCATE op failed with status %d",
  154. pmc->last_cmd_status);
  155. goto release_pmc_skbs;
  156. }
  157. mutex_unlock(&pmc->lock);
  158. return;
  159. release_pmc_skbs:
  160. wil_err(wil, "exit on error: Releasing skbs...\n");
  161. for (i = 0; pmc->descriptors[i].va && i < num_descriptors; i++) {
  162. dma_free_coherent(dev,
  163. descriptor_size,
  164. pmc->descriptors[i].va,
  165. pmc->descriptors[i].pa);
  166. pmc->descriptors[i].va = NULL;
  167. }
  168. wil_err(wil, "exit on error: Releasing pring...\n");
  169. dma_free_coherent(dev,
  170. sizeof(struct vring_tx_desc) * num_descriptors,
  171. pmc->pring_va,
  172. pmc->pring_pa);
  173. pmc->pring_va = NULL;
  174. release_pmc_skb_list:
  175. wil_err(wil, "exit on error: Releasing descriptors info list...\n");
  176. kfree(pmc->descriptors);
  177. pmc->descriptors = NULL;
  178. no_release_err:
  179. pmc->last_cmd_status = last_cmd_err;
  180. mutex_unlock(&pmc->lock);
  181. }
  182. /**
  183. * Traverse the p-ring and release all buffers.
  184. * At the end release the p-ring memory
  185. */
  186. void wil_pmc_free(struct wil6210_priv *wil, int send_pmc_cmd)
  187. {
  188. struct pmc_ctx *pmc = &wil->pmc;
  189. struct device *dev = wil_to_dev(wil);
  190. struct wmi_pmc_cmd pmc_cmd = {0};
  191. mutex_lock(&pmc->lock);
  192. pmc->last_cmd_status = 0;
  193. if (!wil_is_pmc_allocated(pmc)) {
  194. wil_dbg_misc(wil,
  195. "pmc_free: Error, can't free - not allocated\n");
  196. pmc->last_cmd_status = -EPERM;
  197. mutex_unlock(&pmc->lock);
  198. return;
  199. }
  200. if (send_pmc_cmd) {
  201. wil_dbg_misc(wil, "send WMI_PMC_CMD with RELEASE op\n");
  202. pmc_cmd.op = WMI_PMC_RELEASE;
  203. pmc->last_cmd_status =
  204. wmi_send(wil, WMI_PMC_CMDID, &pmc_cmd,
  205. sizeof(pmc_cmd));
  206. if (pmc->last_cmd_status) {
  207. wil_err(wil,
  208. "WMI_PMC_CMD with RELEASE op failed, status %d",
  209. pmc->last_cmd_status);
  210. /* There's nothing we can do with this error.
  211. * Normally, it should never occur.
  212. * Continue to freeing all memory allocated for pmc.
  213. */
  214. }
  215. }
  216. if (pmc->pring_va) {
  217. size_t buf_size = sizeof(struct vring_tx_desc) *
  218. pmc->num_descriptors;
  219. wil_dbg_misc(wil, "pmc_free: free pring va %p\n",
  220. pmc->pring_va);
  221. dma_free_coherent(dev, buf_size, pmc->pring_va, pmc->pring_pa);
  222. pmc->pring_va = NULL;
  223. } else {
  224. pmc->last_cmd_status = -ENOENT;
  225. }
  226. if (pmc->descriptors) {
  227. int i;
  228. for (i = 0;
  229. pmc->descriptors[i].va && i < pmc->num_descriptors; i++) {
  230. dma_free_coherent(dev,
  231. pmc->descriptor_size,
  232. pmc->descriptors[i].va,
  233. pmc->descriptors[i].pa);
  234. pmc->descriptors[i].va = NULL;
  235. }
  236. wil_dbg_misc(wil, "pmc_free: free descriptor info %d/%d\n", i,
  237. pmc->num_descriptors);
  238. wil_dbg_misc(wil,
  239. "pmc_free: free pmc descriptors info list %p\n",
  240. pmc->descriptors);
  241. kfree(pmc->descriptors);
  242. pmc->descriptors = NULL;
  243. } else {
  244. pmc->last_cmd_status = -ENOENT;
  245. }
  246. mutex_unlock(&pmc->lock);
  247. }
  248. /**
  249. * Status of the last operation requested via debugfs: alloc/free/read.
  250. * 0 - success or negative errno
  251. */
  252. int wil_pmc_last_cmd_status(struct wil6210_priv *wil)
  253. {
  254. wil_dbg_misc(wil, "pmc_last_cmd_status: status %d\n",
  255. wil->pmc.last_cmd_status);
  256. return wil->pmc.last_cmd_status;
  257. }
  258. /**
  259. * Read from required position up to the end of current descriptor,
  260. * depends on descriptor size configured during alloc request.
  261. */
  262. ssize_t wil_pmc_read(struct file *filp, char __user *buf, size_t count,
  263. loff_t *f_pos)
  264. {
  265. struct wil6210_priv *wil = filp->private_data;
  266. struct pmc_ctx *pmc = &wil->pmc;
  267. size_t retval = 0;
  268. unsigned long long idx;
  269. loff_t offset;
  270. size_t pmc_size;
  271. mutex_lock(&pmc->lock);
  272. if (!wil_is_pmc_allocated(pmc)) {
  273. wil_err(wil, "error, pmc is not allocated!\n");
  274. pmc->last_cmd_status = -EPERM;
  275. mutex_unlock(&pmc->lock);
  276. return -EPERM;
  277. }
  278. pmc_size = pmc->descriptor_size * pmc->num_descriptors;
  279. wil_dbg_misc(wil,
  280. "pmc_read: size %u, pos %lld\n",
  281. (u32)count, *f_pos);
  282. pmc->last_cmd_status = 0;
  283. idx = *f_pos;
  284. do_div(idx, pmc->descriptor_size);
  285. offset = *f_pos - (idx * pmc->descriptor_size);
  286. if (*f_pos >= pmc_size) {
  287. wil_dbg_misc(wil,
  288. "pmc_read: reached end of pmc buf: %lld >= %u\n",
  289. *f_pos, (u32)pmc_size);
  290. pmc->last_cmd_status = -ERANGE;
  291. goto out;
  292. }
  293. wil_dbg_misc(wil,
  294. "pmc_read: read from pos %lld (descriptor %llu, offset %llu) %zu bytes\n",
  295. *f_pos, idx, offset, count);
  296. /* if no errors, return the copied byte count */
  297. retval = simple_read_from_buffer(buf,
  298. count,
  299. &offset,
  300. pmc->descriptors[idx].va,
  301. pmc->descriptor_size);
  302. *f_pos += retval;
  303. out:
  304. mutex_unlock(&pmc->lock);
  305. return retval;
  306. }
  307. loff_t wil_pmc_llseek(struct file *filp, loff_t off, int whence)
  308. {
  309. loff_t newpos;
  310. struct wil6210_priv *wil = filp->private_data;
  311. struct pmc_ctx *pmc = &wil->pmc;
  312. size_t pmc_size;
  313. mutex_lock(&pmc->lock);
  314. if (!wil_is_pmc_allocated(pmc)) {
  315. wil_err(wil, "error, pmc is not allocated!\n");
  316. pmc->last_cmd_status = -EPERM;
  317. mutex_unlock(&pmc->lock);
  318. return -EPERM;
  319. }
  320. pmc_size = pmc->descriptor_size * pmc->num_descriptors;
  321. switch (whence) {
  322. case 0: /* SEEK_SET */
  323. newpos = off;
  324. break;
  325. case 1: /* SEEK_CUR */
  326. newpos = filp->f_pos + off;
  327. break;
  328. case 2: /* SEEK_END */
  329. newpos = pmc_size;
  330. break;
  331. default: /* can't happen */
  332. newpos = -EINVAL;
  333. goto out;
  334. }
  335. if (newpos < 0) {
  336. newpos = -EINVAL;
  337. goto out;
  338. }
  339. if (newpos > pmc_size)
  340. newpos = pmc_size;
  341. filp->f_pos = newpos;
  342. out:
  343. mutex_unlock(&pmc->lock);
  344. return newpos;
  345. }