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 17 #include <linux/types.h> 18 #include <linux/errno.h> 19 #include <linux/fs.h> 20 #include "wmi.h" 21 #include "wil6210.h" 22 #include "txrx.h" 23 #include "pmc.h" 24 25 struct desc_alloc_info { 26 dma_addr_t pa; 27 void *va; 28 }; 29 30 static int wil_is_pmc_allocated(struct pmc_ctx *pmc) 31 { 32 return !!pmc->pring_va; 33 } 34 35 void wil_pmc_init(struct wil6210_priv *wil) 36 { 37 memset(&wil->pmc, 0, sizeof(struct pmc_ctx)); 38 mutex_init(&wil->pmc.lock); 39 } 40 41 /** 42 * Allocate the physical ring (p-ring) and the required 43 * number of descriptors of required size. 44 * Initialize the descriptors as required by pmc dma. 45 * The descriptors' buffers dwords are initialized to hold 46 * dword's serial number in the lsw and reserved value 47 * PCM_DATA_INVALID_DW_VAL in the msw. 48 */ 49 void wil_pmc_alloc(struct wil6210_priv *wil, 50 int num_descriptors, 51 int descriptor_size) 52 { 53 u32 i; 54 struct pmc_ctx *pmc = &wil->pmc; 55 struct device *dev = wil_to_dev(wil); 56 struct wmi_pmc_cmd pmc_cmd = {0}; 57 int last_cmd_err = -ENOMEM; 58 59 mutex_lock(&pmc->lock); 60 61 if (wil_is_pmc_allocated(pmc)) { 62 /* sanity check */ 63 wil_err(wil, "%s: ERROR pmc is already allocated\n", __func__); 64 goto no_release_err; 65 } 66 if ((num_descriptors <= 0) || (descriptor_size <= 0)) { 67 wil_err(wil, 68 "Invalid params num_descriptors(%d), descriptor_size(%d)\n", 69 num_descriptors, descriptor_size); 70 last_cmd_err = -EINVAL; 71 goto no_release_err; 72 } 73 74 if (num_descriptors > (1 << WIL_RING_SIZE_ORDER_MAX)) { 75 wil_err(wil, 76 "num_descriptors(%d) exceeds max ring size %d\n", 77 num_descriptors, 1 << WIL_RING_SIZE_ORDER_MAX); 78 last_cmd_err = -EINVAL; 79 goto no_release_err; 80 } 81 82 if (num_descriptors > INT_MAX / descriptor_size) { 83 wil_err(wil, 84 "Overflow in num_descriptors(%d)*descriptor_size(%d)\n", 85 num_descriptors, descriptor_size); 86 last_cmd_err = -EINVAL; 87 goto no_release_err; 88 } 89 90 pmc->num_descriptors = num_descriptors; 91 pmc->descriptor_size = descriptor_size; 92 93 wil_dbg_misc(wil, "%s: %d descriptors x %d bytes each\n", 94 __func__, num_descriptors, descriptor_size); 95 96 /* allocate descriptors info list in pmc context*/ 97 pmc->descriptors = kcalloc(num_descriptors, 98 sizeof(struct desc_alloc_info), 99 GFP_KERNEL); 100 if (!pmc->descriptors) { 101 wil_err(wil, "%s: ERROR allocating pmc skb list\n", __func__); 102 goto no_release_err; 103 } 104 105 wil_dbg_misc(wil, 106 "%s: allocated descriptors info list %p\n", 107 __func__, pmc->descriptors); 108 109 /* Allocate pring buffer and descriptors. 110 * vring->va should be aligned on its size rounded up to power of 2 111 * This is granted by the dma_alloc_coherent 112 */ 113 pmc->pring_va = dma_alloc_coherent(dev, 114 sizeof(struct vring_tx_desc) * num_descriptors, 115 &pmc->pring_pa, 116 GFP_KERNEL); 117 118 wil_dbg_misc(wil, 119 "%s: allocated pring %p => %pad. %zd x %d = total %zd bytes\n", 120 __func__, 121 pmc->pring_va, &pmc->pring_pa, 122 sizeof(struct vring_tx_desc), 123 num_descriptors, 124 sizeof(struct vring_tx_desc) * num_descriptors); 125 126 if (!pmc->pring_va) { 127 wil_err(wil, "%s: ERROR allocating pmc pring\n", __func__); 128 goto release_pmc_skb_list; 129 } 130 131 /* initially, all descriptors are SW owned 132 * For Tx, Rx, and PMC, ownership bit is at the same location, thus 133 * we can use any 134 */ 135 for (i = 0; i < num_descriptors; i++) { 136 struct vring_tx_desc *_d = &pmc->pring_va[i]; 137 struct vring_tx_desc dd = {}, *d = ⅆ 138 int j = 0; 139 140 pmc->descriptors[i].va = dma_alloc_coherent(dev, 141 descriptor_size, 142 &pmc->descriptors[i].pa, 143 GFP_KERNEL); 144 145 if (unlikely(!pmc->descriptors[i].va)) { 146 wil_err(wil, 147 "%s: ERROR allocating pmc descriptor %d", 148 __func__, i); 149 goto release_pmc_skbs; 150 } 151 152 for (j = 0; j < descriptor_size / sizeof(u32); j++) { 153 u32 *p = (u32 *)pmc->descriptors[i].va + j; 154 *p = PCM_DATA_INVALID_DW_VAL | j; 155 } 156 157 /* configure dma descriptor */ 158 d->dma.addr.addr_low = 159 cpu_to_le32(lower_32_bits(pmc->descriptors[i].pa)); 160 d->dma.addr.addr_high = 161 cpu_to_le16((u16)upper_32_bits(pmc->descriptors[i].pa)); 162 d->dma.status = 0; /* 0 = HW_OWNED */ 163 d->dma.length = cpu_to_le16(descriptor_size); 164 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT; 165 *_d = *d; 166 } 167 168 wil_dbg_misc(wil, "%s: allocated successfully\n", __func__); 169 170 pmc_cmd.op = WMI_PMC_ALLOCATE; 171 pmc_cmd.ring_size = cpu_to_le16(pmc->num_descriptors); 172 pmc_cmd.mem_base = cpu_to_le64(pmc->pring_pa); 173 174 wil_dbg_misc(wil, "%s: send WMI_PMC_CMD with ALLOCATE op\n", __func__); 175 pmc->last_cmd_status = wmi_send(wil, 176 WMI_PMC_CMDID, 177 &pmc_cmd, 178 sizeof(pmc_cmd)); 179 if (pmc->last_cmd_status) { 180 wil_err(wil, 181 "%s: WMI_PMC_CMD with ALLOCATE op failed with status %d", 182 __func__, pmc->last_cmd_status); 183 goto release_pmc_skbs; 184 } 185 186 mutex_unlock(&pmc->lock); 187 188 return; 189 190 release_pmc_skbs: 191 wil_err(wil, "%s: exit on error: Releasing skbs...\n", __func__); 192 for (i = 0; pmc->descriptors[i].va && i < num_descriptors; i++) { 193 dma_free_coherent(dev, 194 descriptor_size, 195 pmc->descriptors[i].va, 196 pmc->descriptors[i].pa); 197 198 pmc->descriptors[i].va = NULL; 199 } 200 wil_err(wil, "%s: exit on error: Releasing pring...\n", __func__); 201 202 dma_free_coherent(dev, 203 sizeof(struct vring_tx_desc) * num_descriptors, 204 pmc->pring_va, 205 pmc->pring_pa); 206 207 pmc->pring_va = NULL; 208 209 release_pmc_skb_list: 210 wil_err(wil, "%s: exit on error: Releasing descriptors info list...\n", 211 __func__); 212 kfree(pmc->descriptors); 213 pmc->descriptors = NULL; 214 215 no_release_err: 216 pmc->last_cmd_status = last_cmd_err; 217 mutex_unlock(&pmc->lock); 218 } 219 220 /** 221 * Traverse the p-ring and release all buffers. 222 * At the end release the p-ring memory 223 */ 224 void wil_pmc_free(struct wil6210_priv *wil, int send_pmc_cmd) 225 { 226 struct pmc_ctx *pmc = &wil->pmc; 227 struct device *dev = wil_to_dev(wil); 228 struct wmi_pmc_cmd pmc_cmd = {0}; 229 230 mutex_lock(&pmc->lock); 231 232 pmc->last_cmd_status = 0; 233 234 if (!wil_is_pmc_allocated(pmc)) { 235 wil_dbg_misc(wil, "%s: Error, can't free - not allocated\n", 236 __func__); 237 pmc->last_cmd_status = -EPERM; 238 mutex_unlock(&pmc->lock); 239 return; 240 } 241 242 if (send_pmc_cmd) { 243 wil_dbg_misc(wil, "%s: send WMI_PMC_CMD with RELEASE op\n", 244 __func__); 245 pmc_cmd.op = WMI_PMC_RELEASE; 246 pmc->last_cmd_status = 247 wmi_send(wil, WMI_PMC_CMDID, &pmc_cmd, 248 sizeof(pmc_cmd)); 249 if (pmc->last_cmd_status) { 250 wil_err(wil, 251 "%s WMI_PMC_CMD with RELEASE op failed, status %d", 252 __func__, pmc->last_cmd_status); 253 /* There's nothing we can do with this error. 254 * Normally, it should never occur. 255 * Continue to freeing all memory allocated for pmc. 256 */ 257 } 258 } 259 260 if (pmc->pring_va) { 261 size_t buf_size = sizeof(struct vring_tx_desc) * 262 pmc->num_descriptors; 263 264 wil_dbg_misc(wil, "%s: free pring va %p\n", 265 __func__, pmc->pring_va); 266 dma_free_coherent(dev, buf_size, pmc->pring_va, pmc->pring_pa); 267 268 pmc->pring_va = NULL; 269 } else { 270 pmc->last_cmd_status = -ENOENT; 271 } 272 273 if (pmc->descriptors) { 274 int i; 275 276 for (i = 0; 277 pmc->descriptors[i].va && i < pmc->num_descriptors; i++) { 278 dma_free_coherent(dev, 279 pmc->descriptor_size, 280 pmc->descriptors[i].va, 281 pmc->descriptors[i].pa); 282 pmc->descriptors[i].va = NULL; 283 } 284 wil_dbg_misc(wil, "%s: free descriptor info %d/%d\n", 285 __func__, i, pmc->num_descriptors); 286 wil_dbg_misc(wil, 287 "%s: free pmc descriptors info list %p\n", 288 __func__, pmc->descriptors); 289 kfree(pmc->descriptors); 290 pmc->descriptors = NULL; 291 } else { 292 pmc->last_cmd_status = -ENOENT; 293 } 294 295 mutex_unlock(&pmc->lock); 296 } 297 298 /** 299 * Status of the last operation requested via debugfs: alloc/free/read. 300 * 0 - success or negative errno 301 */ 302 int wil_pmc_last_cmd_status(struct wil6210_priv *wil) 303 { 304 wil_dbg_misc(wil, "%s: status %d\n", __func__, 305 wil->pmc.last_cmd_status); 306 307 return wil->pmc.last_cmd_status; 308 } 309 310 /** 311 * Read from required position up to the end of current descriptor, 312 * depends on descriptor size configured during alloc request. 313 */ 314 ssize_t wil_pmc_read(struct file *filp, char __user *buf, size_t count, 315 loff_t *f_pos) 316 { 317 struct wil6210_priv *wil = filp->private_data; 318 struct pmc_ctx *pmc = &wil->pmc; 319 size_t retval = 0; 320 unsigned long long idx; 321 loff_t offset; 322 size_t pmc_size; 323 324 mutex_lock(&pmc->lock); 325 326 if (!wil_is_pmc_allocated(pmc)) { 327 wil_err(wil, "%s: error, pmc is not allocated!\n", __func__); 328 pmc->last_cmd_status = -EPERM; 329 mutex_unlock(&pmc->lock); 330 return -EPERM; 331 } 332 333 pmc_size = pmc->descriptor_size * pmc->num_descriptors; 334 335 wil_dbg_misc(wil, 336 "%s: size %u, pos %lld\n", 337 __func__, (unsigned)count, *f_pos); 338 339 pmc->last_cmd_status = 0; 340 341 idx = *f_pos; 342 do_div(idx, pmc->descriptor_size); 343 offset = *f_pos - (idx * pmc->descriptor_size); 344 345 if (*f_pos >= pmc_size) { 346 wil_dbg_misc(wil, "%s: reached end of pmc buf: %lld >= %u\n", 347 __func__, *f_pos, (unsigned)pmc_size); 348 pmc->last_cmd_status = -ERANGE; 349 goto out; 350 } 351 352 wil_dbg_misc(wil, 353 "%s: read from pos %lld (descriptor %llu, offset %llu) %zu bytes\n", 354 __func__, *f_pos, idx, offset, count); 355 356 /* if no errors, return the copied byte count */ 357 retval = simple_read_from_buffer(buf, 358 count, 359 &offset, 360 pmc->descriptors[idx].va, 361 pmc->descriptor_size); 362 *f_pos += retval; 363 out: 364 mutex_unlock(&pmc->lock); 365 366 return retval; 367 } 368 369 loff_t wil_pmc_llseek(struct file *filp, loff_t off, int whence) 370 { 371 loff_t newpos; 372 struct wil6210_priv *wil = filp->private_data; 373 struct pmc_ctx *pmc = &wil->pmc; 374 size_t pmc_size; 375 376 mutex_lock(&pmc->lock); 377 378 if (!wil_is_pmc_allocated(pmc)) { 379 wil_err(wil, "error, pmc is not allocated!\n"); 380 pmc->last_cmd_status = -EPERM; 381 mutex_unlock(&pmc->lock); 382 return -EPERM; 383 } 384 385 pmc_size = pmc->descriptor_size * pmc->num_descriptors; 386 387 switch (whence) { 388 case 0: /* SEEK_SET */ 389 newpos = off; 390 break; 391 392 case 1: /* SEEK_CUR */ 393 newpos = filp->f_pos + off; 394 break; 395 396 case 2: /* SEEK_END */ 397 newpos = pmc_size; 398 break; 399 400 default: /* can't happen */ 401 newpos = -EINVAL; 402 goto out; 403 } 404 405 if (newpos < 0) { 406 newpos = -EINVAL; 407 goto out; 408 } 409 if (newpos > pmc_size) 410 newpos = pmc_size; 411 412 filp->f_pos = newpos; 413 414 out: 415 mutex_unlock(&pmc->lock); 416 417 return newpos; 418 } 419