pmc.c 11 KB

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