pmc.c 11 KB

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