1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright (c) 2012-2015,2017 Qualcomm Atheros, Inc. 4 * Copyright (c) 2018, The Linux Foundation. All rights reserved. 5 */ 6 7 #include <linux/types.h> 8 #include <linux/errno.h> 9 #include <linux/fs.h> 10 #include <linux/seq_file.h> 11 #include "wmi.h" 12 #include "wil6210.h" 13 #include "txrx.h" 14 #include "pmc.h" 15 16 struct desc_alloc_info { 17 dma_addr_t pa; 18 void *va; 19 }; 20 21 static int wil_is_pmc_allocated(struct pmc_ctx *pmc) 22 { 23 return !!pmc->pring_va; 24 } 25 26 void wil_pmc_init(struct wil6210_priv *wil) 27 { 28 memset(&wil->pmc, 0, sizeof(struct pmc_ctx)); 29 mutex_init(&wil->pmc.lock); 30 } 31 32 /* Allocate the physical ring (p-ring) and the required 33 * number of descriptors of required size. 34 * Initialize the descriptors as required by pmc dma. 35 * The descriptors' buffers dwords are initialized to hold 36 * dword's serial number in the lsw and reserved value 37 * PCM_DATA_INVALID_DW_VAL in the msw. 38 */ 39 void wil_pmc_alloc(struct wil6210_priv *wil, 40 int num_descriptors, 41 int descriptor_size) 42 { 43 u32 i; 44 struct pmc_ctx *pmc = &wil->pmc; 45 struct device *dev = wil_to_dev(wil); 46 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev); 47 struct wmi_pmc_cmd pmc_cmd = {0}; 48 int last_cmd_err = -ENOMEM; 49 50 mutex_lock(&pmc->lock); 51 52 if (wil_is_pmc_allocated(pmc)) { 53 /* sanity check */ 54 wil_err(wil, "ERROR pmc is already allocated\n"); 55 goto no_release_err; 56 } 57 if ((num_descriptors <= 0) || (descriptor_size <= 0)) { 58 wil_err(wil, 59 "Invalid params num_descriptors(%d), descriptor_size(%d)\n", 60 num_descriptors, descriptor_size); 61 last_cmd_err = -EINVAL; 62 goto no_release_err; 63 } 64 65 if (num_descriptors > (1 << WIL_RING_SIZE_ORDER_MAX)) { 66 wil_err(wil, 67 "num_descriptors(%d) exceeds max ring size %d\n", 68 num_descriptors, 1 << WIL_RING_SIZE_ORDER_MAX); 69 last_cmd_err = -EINVAL; 70 goto no_release_err; 71 } 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 81 pmc->num_descriptors = num_descriptors; 82 pmc->descriptor_size = descriptor_size; 83 84 wil_dbg_misc(wil, "pmc_alloc: %d descriptors x %d bytes each\n", 85 num_descriptors, descriptor_size); 86 87 /* allocate descriptors info list in pmc context*/ 88 pmc->descriptors = kzalloc_objs(struct desc_alloc_info, num_descriptors); 89 if (!pmc->descriptors) { 90 wil_err(wil, "ERROR allocating pmc skb list\n"); 91 goto no_release_err; 92 } 93 94 wil_dbg_misc(wil, "pmc_alloc: allocated descriptors info list %p\n", 95 pmc->descriptors); 96 97 /* Allocate pring buffer and descriptors. 98 * vring->va should be aligned on its size rounded up to power of 2 99 * This is granted by the dma_alloc_coherent. 100 * 101 * HW has limitation that all vrings addresses must share the same 102 * upper 16 msb bits part of 48 bits address. To workaround that, 103 * if we are using more than 32 bit addresses switch to 32 bit 104 * allocation before allocating vring memory. 105 * 106 * There's no check for the return value of dma_set_mask_and_coherent, 107 * since we assume if we were able to set the mask during 108 * initialization in this system it will not fail if we set it again 109 */ 110 if (wil->dma_addr_size > 32) 111 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); 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 if (wil->dma_addr_size > 32) 119 dma_set_mask_and_coherent(dev, 120 DMA_BIT_MASK(wil->dma_addr_size)); 121 122 wil_dbg_misc(wil, 123 "pmc_alloc: allocated pring %p => %pad. %zd x %d = total %zd bytes\n", 124 pmc->pring_va, &pmc->pring_pa, 125 sizeof(struct vring_tx_desc), 126 num_descriptors, 127 sizeof(struct vring_tx_desc) * num_descriptors); 128 129 if (!pmc->pring_va) { 130 wil_err(wil, "ERROR allocating pmc pring\n"); 131 goto release_pmc_skb_list; 132 } 133 134 /* initially, all descriptors are SW owned 135 * For Tx, Rx, and PMC, ownership bit is at the same location, thus 136 * we can use any 137 */ 138 for (i = 0; i < num_descriptors; i++) { 139 struct vring_tx_desc *_d = &pmc->pring_va[i]; 140 struct vring_tx_desc dd = {}, *d = ⅆ 141 int j = 0; 142 143 pmc->descriptors[i].va = dma_alloc_coherent(dev, 144 descriptor_size, 145 &pmc->descriptors[i].pa, 146 GFP_KERNEL); 147 148 if (unlikely(!pmc->descriptors[i].va)) { 149 wil_err(wil, "ERROR allocating pmc descriptor %d", i); 150 goto release_pmc_skbs; 151 } 152 153 for (j = 0; j < descriptor_size / sizeof(u32); j++) { 154 u32 *p = (u32 *)pmc->descriptors[i].va + j; 155 *p = PCM_DATA_INVALID_DW_VAL | j; 156 } 157 158 /* configure dma descriptor */ 159 d->dma.addr.addr_low = 160 cpu_to_le32(lower_32_bits(pmc->descriptors[i].pa)); 161 d->dma.addr.addr_high = 162 cpu_to_le16((u16)upper_32_bits(pmc->descriptors[i].pa)); 163 d->dma.status = 0; /* 0 = HW_OWNED */ 164 d->dma.length = cpu_to_le16(descriptor_size); 165 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT; 166 *_d = *d; 167 } 168 169 wil_dbg_misc(wil, "pmc_alloc: allocated successfully\n"); 170 171 pmc_cmd.op = WMI_PMC_ALLOCATE; 172 pmc_cmd.ring_size = cpu_to_le16(pmc->num_descriptors); 173 pmc_cmd.mem_base = cpu_to_le64(pmc->pring_pa); 174 175 wil_dbg_misc(wil, "pmc_alloc: send WMI_PMC_CMD with ALLOCATE op\n"); 176 pmc->last_cmd_status = wmi_send(wil, 177 WMI_PMC_CMDID, 178 vif->mid, 179 &pmc_cmd, 180 sizeof(pmc_cmd)); 181 if (pmc->last_cmd_status) { 182 wil_err(wil, 183 "WMI_PMC_CMD with ALLOCATE op failed with status %d", 184 pmc->last_cmd_status); 185 goto release_pmc_skbs; 186 } 187 188 mutex_unlock(&pmc->lock); 189 190 return; 191 192 release_pmc_skbs: 193 wil_err(wil, "exit on error: Releasing skbs...\n"); 194 for (i = 0; i < num_descriptors && pmc->descriptors[i].va; i++) { 195 dma_free_coherent(dev, 196 descriptor_size, 197 pmc->descriptors[i].va, 198 pmc->descriptors[i].pa); 199 200 pmc->descriptors[i].va = NULL; 201 } 202 wil_err(wil, "exit on error: Releasing pring...\n"); 203 204 dma_free_coherent(dev, 205 sizeof(struct vring_tx_desc) * num_descriptors, 206 pmc->pring_va, 207 pmc->pring_pa); 208 209 pmc->pring_va = NULL; 210 211 release_pmc_skb_list: 212 wil_err(wil, "exit on error: Releasing descriptors info list...\n"); 213 kfree(pmc->descriptors); 214 pmc->descriptors = NULL; 215 216 no_release_err: 217 pmc->last_cmd_status = last_cmd_err; 218 mutex_unlock(&pmc->lock); 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 wil6210_vif *vif = ndev_to_vif(wil->main_ndev); 229 struct wmi_pmc_cmd pmc_cmd = {0}; 230 231 mutex_lock(&pmc->lock); 232 233 pmc->last_cmd_status = 0; 234 235 if (!wil_is_pmc_allocated(pmc)) { 236 wil_dbg_misc(wil, 237 "pmc_free: Error, can't free - not allocated\n"); 238 pmc->last_cmd_status = -EPERM; 239 mutex_unlock(&pmc->lock); 240 return; 241 } 242 243 if (send_pmc_cmd) { 244 wil_dbg_misc(wil, "send WMI_PMC_CMD with RELEASE op\n"); 245 pmc_cmd.op = WMI_PMC_RELEASE; 246 pmc->last_cmd_status = 247 wmi_send(wil, WMI_PMC_CMDID, vif->mid, 248 &pmc_cmd, sizeof(pmc_cmd)); 249 if (pmc->last_cmd_status) { 250 wil_err(wil, 251 "WMI_PMC_CMD with RELEASE op failed, status %d", 252 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, "pmc_free: free pring va %p\n", 265 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 i < pmc->num_descriptors && pmc->descriptors[i].va; 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, "pmc_free: free descriptor info %d/%d\n", i, 285 pmc->num_descriptors); 286 wil_dbg_misc(wil, 287 "pmc_free: free pmc descriptors info list %p\n", 288 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 /* Status of the last operation requested via debugfs: alloc/free/read. 299 * 0 - success or negative errno 300 */ 301 int wil_pmc_last_cmd_status(struct wil6210_priv *wil) 302 { 303 wil_dbg_misc(wil, "pmc_last_cmd_status: status %d\n", 304 wil->pmc.last_cmd_status); 305 306 return wil->pmc.last_cmd_status; 307 } 308 309 /* Read from required position up to the end of current descriptor, 310 * depends on descriptor size configured during alloc request. 311 */ 312 ssize_t wil_pmc_read(struct file *filp, char __user *buf, size_t count, 313 loff_t *f_pos) 314 { 315 struct wil6210_priv *wil = filp->private_data; 316 struct pmc_ctx *pmc = &wil->pmc; 317 size_t retval = 0; 318 unsigned long long idx; 319 loff_t offset; 320 size_t pmc_size; 321 322 mutex_lock(&pmc->lock); 323 324 if (!wil_is_pmc_allocated(pmc)) { 325 wil_err(wil, "error, pmc is not allocated!\n"); 326 pmc->last_cmd_status = -EPERM; 327 mutex_unlock(&pmc->lock); 328 return -EPERM; 329 } 330 331 pmc_size = pmc->descriptor_size * pmc->num_descriptors; 332 333 wil_dbg_misc(wil, 334 "pmc_read: size %u, pos %lld\n", 335 (u32)count, *f_pos); 336 337 pmc->last_cmd_status = 0; 338 339 idx = *f_pos; 340 do_div(idx, pmc->descriptor_size); 341 offset = *f_pos - (idx * pmc->descriptor_size); 342 343 if (*f_pos >= pmc_size) { 344 wil_dbg_misc(wil, 345 "pmc_read: reached end of pmc buf: %lld >= %u\n", 346 *f_pos, (u32)pmc_size); 347 pmc->last_cmd_status = -ERANGE; 348 goto out; 349 } 350 351 wil_dbg_misc(wil, 352 "pmc_read: read from pos %lld (descriptor %llu, offset %llu) %zu bytes\n", 353 *f_pos, idx, offset, count); 354 355 /* if no errors, return the copied byte count */ 356 retval = simple_read_from_buffer(buf, 357 count, 358 &offset, 359 pmc->descriptors[idx].va, 360 pmc->descriptor_size); 361 *f_pos += retval; 362 out: 363 mutex_unlock(&pmc->lock); 364 365 return retval; 366 } 367 368 loff_t wil_pmc_llseek(struct file *filp, loff_t off, int whence) 369 { 370 loff_t newpos; 371 struct wil6210_priv *wil = filp->private_data; 372 struct pmc_ctx *pmc = &wil->pmc; 373 size_t pmc_size; 374 375 mutex_lock(&pmc->lock); 376 377 if (!wil_is_pmc_allocated(pmc)) { 378 wil_err(wil, "error, pmc is not allocated!\n"); 379 pmc->last_cmd_status = -EPERM; 380 mutex_unlock(&pmc->lock); 381 return -EPERM; 382 } 383 384 pmc_size = pmc->descriptor_size * pmc->num_descriptors; 385 386 switch (whence) { 387 case 0: /* SEEK_SET */ 388 newpos = off; 389 break; 390 391 case 1: /* SEEK_CUR */ 392 newpos = filp->f_pos + off; 393 break; 394 395 case 2: /* SEEK_END */ 396 newpos = pmc_size; 397 break; 398 399 default: /* can't happen */ 400 newpos = -EINVAL; 401 goto out; 402 } 403 404 if (newpos < 0) { 405 newpos = -EINVAL; 406 goto out; 407 } 408 if (newpos > pmc_size) 409 newpos = pmc_size; 410 411 filp->f_pos = newpos; 412 413 out: 414 mutex_unlock(&pmc->lock); 415 416 return newpos; 417 } 418 419 int wil_pmcring_read(struct seq_file *s, void *data) 420 { 421 struct wil6210_priv *wil = s->private; 422 struct pmc_ctx *pmc = &wil->pmc; 423 size_t pmc_ring_size = 424 sizeof(struct vring_rx_desc) * pmc->num_descriptors; 425 426 mutex_lock(&pmc->lock); 427 428 if (!wil_is_pmc_allocated(pmc)) { 429 wil_err(wil, "error, pmc is not allocated!\n"); 430 pmc->last_cmd_status = -EPERM; 431 mutex_unlock(&pmc->lock); 432 return -EPERM; 433 } 434 435 wil_dbg_misc(wil, "pmcring_read: size %zu\n", pmc_ring_size); 436 437 seq_write(s, pmc->pring_va, pmc_ring_size); 438 439 mutex_unlock(&pmc->lock); 440 441 return 0; 442 } 443