1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2012 4 * 5 * Author(s): 6 * Jan Glauber <jang@linux.vnet.ibm.com> 7 */ 8 9 #define KMSG_COMPONENT "zpci" 10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 11 12 #include <linux/compat.h> 13 #include <linux/kernel.h> 14 #include <linux/miscdevice.h> 15 #include <linux/slab.h> 16 #include <linux/err.h> 17 #include <linux/delay.h> 18 #include <linux/pci.h> 19 #include <linux/uaccess.h> 20 #include <asm/asm-extable.h> 21 #include <asm/pci_debug.h> 22 #include <asm/pci_clp.h> 23 #include <asm/asm.h> 24 #include <asm/clp.h> 25 #include <uapi/asm/clp.h> 26 27 #include "pci_bus.h" 28 29 bool zpci_unique_uid; 30 31 void update_uid_checking(bool new) 32 { 33 if (zpci_unique_uid != new) 34 zpci_dbg(3, "uid checking:%d\n", new); 35 36 zpci_unique_uid = new; 37 } 38 39 static inline void zpci_err_clp(unsigned int rsp, int rc) 40 { 41 struct { 42 unsigned int rsp; 43 int rc; 44 } __packed data = {rsp, rc}; 45 46 zpci_err_hex(&data, sizeof(data)); 47 } 48 49 /* 50 * Call Logical Processor with c=1, lps=0 and command 1 51 * to get the bit mask of installed logical processors 52 */ 53 static inline int clp_get_ilp(unsigned long *ilp) 54 { 55 unsigned long mask; 56 int cc, exception; 57 58 exception = 1; 59 asm_inline volatile ( 60 " .insn rrf,0xb9a00000,%[mask],%[cmd],8,0\n" 61 "0: lhi %[exc],0\n" 62 "1:\n" 63 CC_IPM(cc) 64 EX_TABLE(0b, 1b) 65 : CC_OUT(cc, cc), [mask] "=d" (mask), [exc] "+d" (exception) 66 : [cmd] "a" (1) 67 : CC_CLOBBER); 68 *ilp = mask; 69 return exception ? 3 : CC_TRANSFORM(cc); 70 } 71 72 /* 73 * Call Logical Processor with c=0, the give constant lps and an lpcb request. 74 */ 75 static __always_inline int clp_req(void *data, unsigned int lps) 76 { 77 struct { u8 _[CLP_BLK_SIZE]; } *req = data; 78 int cc, exception; 79 u64 ignored; 80 81 exception = 1; 82 asm_inline volatile ( 83 " .insn rrf,0xb9a00000,%[ign],%[req],0,%[lps]\n" 84 "0: lhi %[exc],0\n" 85 "1:\n" 86 CC_IPM(cc) 87 EX_TABLE(0b, 1b) 88 : CC_OUT(cc, cc), [ign] "=d" (ignored), "+m" (*req), [exc] "+d" (exception) 89 : [req] "a" (req), [lps] "i" (lps) 90 : CC_CLOBBER); 91 return exception ? 3 : CC_TRANSFORM(cc); 92 } 93 94 static void *clp_alloc_block(gfp_t gfp_mask) 95 { 96 return (void *) __get_free_pages(gfp_mask, get_order(CLP_BLK_SIZE)); 97 } 98 99 static void clp_free_block(void *ptr) 100 { 101 free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE)); 102 } 103 104 static void clp_store_query_pci_fngrp(struct zpci_dev *zdev, 105 struct clp_rsp_query_pci_grp *response) 106 { 107 zdev->tlb_refresh = response->refresh; 108 zdev->dma_mask = response->dasm; 109 zdev->msi_addr = response->msia; 110 zdev->max_msi = response->noi; 111 zdev->fmb_update = response->mui; 112 zdev->version = response->version; 113 zdev->maxstbl = response->maxstbl; 114 zdev->dtsm = response->dtsm; 115 zdev->rtr_avail = response->rtr; 116 117 switch (response->version) { 118 case 1: 119 zdev->max_bus_speed = PCIE_SPEED_5_0GT; 120 break; 121 default: 122 zdev->max_bus_speed = PCI_SPEED_UNKNOWN; 123 break; 124 } 125 } 126 127 static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid) 128 { 129 struct clp_req_rsp_query_pci_grp *rrb; 130 int rc; 131 132 rrb = clp_alloc_block(GFP_KERNEL); 133 if (!rrb) 134 return -ENOMEM; 135 136 memset(rrb, 0, sizeof(*rrb)); 137 rrb->request.hdr.len = sizeof(rrb->request); 138 rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP; 139 rrb->response.hdr.len = sizeof(rrb->response); 140 rrb->request.pfgid = pfgid; 141 142 rc = clp_req(rrb, CLP_LPS_PCI); 143 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) 144 clp_store_query_pci_fngrp(zdev, &rrb->response); 145 else { 146 zpci_err("Q PCI FGRP:\n"); 147 zpci_err_clp(rrb->response.hdr.rsp, rc); 148 rc = -EIO; 149 } 150 clp_free_block(rrb); 151 return rc; 152 } 153 154 static int clp_store_query_pci_fn(struct zpci_dev *zdev, 155 struct clp_rsp_query_pci *response) 156 { 157 int i; 158 159 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 160 zdev->bars[i].val = le32_to_cpu(response->bar[i]); 161 zdev->bars[i].size = response->bar_size[i]; 162 } 163 zdev->start_dma = response->sdma; 164 zdev->end_dma = response->edma; 165 zdev->pchid = response->pchid; 166 zdev->pfgid = response->pfgid; 167 zdev->pft = response->pft; 168 zdev->vfn = response->vfn; 169 zdev->port = response->port; 170 zdev->fidparm = response->fidparm; 171 zdev->uid = response->uid; 172 zdev->fmb_length = sizeof(u32) * response->fmb_len; 173 zdev->is_physfn = response->is_physfn; 174 zdev->rid_available = response->rid_avail; 175 if (zdev->rid_available) 176 zdev->rid = response->rid; 177 zdev->tid_avail = response->tid_avail; 178 if (zdev->tid_avail) 179 zdev->tid = response->tid; 180 181 memcpy(zdev->pfip, response->pfip, sizeof(zdev->pfip)); 182 if (response->util_str_avail) { 183 memcpy(zdev->util_str, response->util_str, 184 sizeof(zdev->util_str)); 185 zdev->util_str_avail = 1; 186 } 187 zdev->mio_capable = response->mio_addr_avail; 188 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 189 if (!(response->mio.valid & (1 << (PCI_STD_NUM_BARS - i - 1)))) 190 continue; 191 192 zdev->bars[i].mio_wb = (void __iomem *) response->mio.addr[i].wb; 193 zdev->bars[i].mio_wt = (void __iomem *) response->mio.addr[i].wt; 194 } 195 return 0; 196 } 197 198 int clp_query_pci_fn(struct zpci_dev *zdev) 199 { 200 struct clp_req_rsp_query_pci *rrb; 201 int rc; 202 203 rrb = clp_alloc_block(GFP_KERNEL); 204 if (!rrb) 205 return -ENOMEM; 206 207 memset(rrb, 0, sizeof(*rrb)); 208 rrb->request.hdr.len = sizeof(rrb->request); 209 rrb->request.hdr.cmd = CLP_QUERY_PCI_FN; 210 rrb->response.hdr.len = sizeof(rrb->response); 211 rrb->request.fh = zdev->fh; 212 213 rc = clp_req(rrb, CLP_LPS_PCI); 214 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) { 215 rc = clp_store_query_pci_fn(zdev, &rrb->response); 216 if (rc) 217 goto out; 218 rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid); 219 } else { 220 zpci_err("Q PCI FN:\n"); 221 zpci_err_clp(rrb->response.hdr.rsp, rc); 222 rc = -EIO; 223 } 224 out: 225 clp_free_block(rrb); 226 return rc; 227 } 228 229 /** 230 * clp_set_pci_fn() - Execute a command on a PCI function 231 * @zdev: Function that will be affected 232 * @fh: Out parameter for updated function handle 233 * @nr_dma_as: DMA address space number 234 * @command: The command code to execute 235 * 236 * Returns: 0 on success, < 0 for Linux errors (e.g. -ENOMEM), and 237 * > 0 for non-success platform responses 238 */ 239 static int clp_set_pci_fn(struct zpci_dev *zdev, u32 *fh, u8 nr_dma_as, u8 command) 240 { 241 struct clp_req_rsp_set_pci *rrb; 242 int rc, retries = 100; 243 u32 gisa = 0; 244 245 *fh = 0; 246 rrb = clp_alloc_block(GFP_KERNEL); 247 if (!rrb) 248 return -ENOMEM; 249 250 if (command != CLP_SET_DISABLE_PCI_FN) 251 gisa = zdev->gisa; 252 253 do { 254 memset(rrb, 0, sizeof(*rrb)); 255 rrb->request.hdr.len = sizeof(rrb->request); 256 rrb->request.hdr.cmd = CLP_SET_PCI_FN; 257 rrb->response.hdr.len = sizeof(rrb->response); 258 rrb->request.fh = zdev->fh; 259 rrb->request.oc = command; 260 rrb->request.ndas = nr_dma_as; 261 rrb->request.gisa = gisa; 262 263 rc = clp_req(rrb, CLP_LPS_PCI); 264 if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) { 265 retries--; 266 if (retries < 0) 267 break; 268 msleep(20); 269 } 270 } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY); 271 272 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) { 273 *fh = rrb->response.fh; 274 } else { 275 zpci_err("Set PCI FN:\n"); 276 zpci_err_clp(rrb->response.hdr.rsp, rc); 277 if (!rc) 278 rc = rrb->response.hdr.rsp; 279 } 280 clp_free_block(rrb); 281 return rc; 282 } 283 284 int clp_setup_writeback_mio(void) 285 { 286 struct clp_req_rsp_slpc_pci *rrb; 287 u8 wb_bit_pos; 288 int rc; 289 290 rrb = clp_alloc_block(GFP_KERNEL); 291 if (!rrb) 292 return -ENOMEM; 293 294 memset(rrb, 0, sizeof(*rrb)); 295 rrb->request.hdr.len = sizeof(rrb->request); 296 rrb->request.hdr.cmd = CLP_SLPC; 297 rrb->response.hdr.len = sizeof(rrb->response); 298 299 rc = clp_req(rrb, CLP_LPS_PCI); 300 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) { 301 if (rrb->response.vwb) { 302 wb_bit_pos = rrb->response.mio_wb; 303 set_bit_inv(wb_bit_pos, &mio_wb_bit_mask); 304 zpci_dbg(3, "wb bit: %d\n", wb_bit_pos); 305 } else { 306 zpci_dbg(3, "wb bit: n.a.\n"); 307 } 308 309 } else { 310 zpci_err("SLPC PCI:\n"); 311 zpci_err_clp(rrb->response.hdr.rsp, rc); 312 rc = -EIO; 313 } 314 clp_free_block(rrb); 315 return rc; 316 } 317 318 int clp_enable_fh(struct zpci_dev *zdev, u32 *fh, u8 nr_dma_as) 319 { 320 int rc; 321 322 rc = clp_set_pci_fn(zdev, fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN); 323 zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, *fh, rc); 324 if (!rc && zpci_use_mio(zdev)) { 325 rc = clp_set_pci_fn(zdev, fh, nr_dma_as, CLP_SET_ENABLE_MIO); 326 zpci_dbg(3, "ena mio fid:%x, fh:%x, rc:%d\n", 327 zdev->fid, *fh, rc); 328 if (rc) 329 clp_disable_fh(zdev, fh); 330 } 331 return rc; 332 } 333 334 int clp_disable_fh(struct zpci_dev *zdev, u32 *fh) 335 { 336 int rc; 337 338 if (!zdev_enabled(zdev)) 339 return 0; 340 341 rc = clp_set_pci_fn(zdev, fh, 0, CLP_SET_DISABLE_PCI_FN); 342 zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, *fh, rc); 343 return rc; 344 } 345 346 static int clp_list_pci_req(struct clp_req_rsp_list_pci *rrb, 347 u64 *resume_token, int *nentries) 348 { 349 int rc; 350 351 memset(rrb, 0, sizeof(*rrb)); 352 rrb->request.hdr.len = sizeof(rrb->request); 353 rrb->request.hdr.cmd = CLP_LIST_PCI; 354 /* store as many entries as possible */ 355 rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN; 356 rrb->request.resume_token = *resume_token; 357 358 /* Get PCI function handle list */ 359 rc = clp_req(rrb, CLP_LPS_PCI); 360 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) { 361 zpci_err("List PCI FN:\n"); 362 zpci_err_clp(rrb->response.hdr.rsp, rc); 363 return -EIO; 364 } 365 366 update_uid_checking(rrb->response.uid_checking); 367 WARN_ON_ONCE(rrb->response.entry_size != 368 sizeof(struct clp_fh_list_entry)); 369 370 *nentries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) / 371 rrb->response.entry_size; 372 *resume_token = rrb->response.resume_token; 373 374 return rc; 375 } 376 377 static int clp_list_pci(struct clp_req_rsp_list_pci *rrb, void *data, 378 void (*cb)(struct clp_fh_list_entry *, void *)) 379 { 380 u64 resume_token = 0; 381 int nentries, i, rc; 382 383 do { 384 rc = clp_list_pci_req(rrb, &resume_token, &nentries); 385 if (rc) 386 return rc; 387 for (i = 0; i < nentries; i++) 388 cb(&rrb->response.fh_list[i], data); 389 } while (resume_token); 390 391 return rc; 392 } 393 394 static int clp_find_pci(struct clp_req_rsp_list_pci *rrb, u32 fid, 395 struct clp_fh_list_entry *entry) 396 { 397 struct clp_fh_list_entry *fh_list; 398 u64 resume_token = 0; 399 int nentries, i, rc; 400 401 do { 402 rc = clp_list_pci_req(rrb, &resume_token, &nentries); 403 if (rc) 404 return rc; 405 fh_list = rrb->response.fh_list; 406 for (i = 0; i < nentries; i++) { 407 if (fh_list[i].fid == fid) { 408 *entry = fh_list[i]; 409 return 0; 410 } 411 } 412 } while (resume_token); 413 414 return -ENODEV; 415 } 416 417 static void __clp_add(struct clp_fh_list_entry *entry, void *data) 418 { 419 struct list_head *scan_list = data; 420 struct zpci_dev *zdev; 421 422 if (!entry->vendor_id) 423 return; 424 425 zdev = get_zdev_by_fid(entry->fid); 426 if (zdev) { 427 zpci_zdev_put(zdev); 428 return; 429 } 430 zdev = zpci_create_device(entry->fid, entry->fh, entry->config_state); 431 list_add_tail(&zdev->entry, scan_list); 432 } 433 434 int clp_scan_pci_devices(struct list_head *scan_list) 435 { 436 struct clp_req_rsp_list_pci *rrb; 437 int rc; 438 439 rrb = clp_alloc_block(GFP_KERNEL); 440 if (!rrb) 441 return -ENOMEM; 442 443 rc = clp_list_pci(rrb, scan_list, __clp_add); 444 445 clp_free_block(rrb); 446 return rc; 447 } 448 449 /* 450 * Get the current function handle of the function matching @fid 451 */ 452 int clp_refresh_fh(u32 fid, u32 *fh) 453 { 454 struct clp_req_rsp_list_pci *rrb; 455 struct clp_fh_list_entry entry; 456 int rc; 457 458 rrb = clp_alloc_block(GFP_NOWAIT); 459 if (!rrb) 460 return -ENOMEM; 461 462 rc = clp_find_pci(rrb, fid, &entry); 463 if (!rc) 464 *fh = entry.fh; 465 466 clp_free_block(rrb); 467 return rc; 468 } 469 470 int clp_get_state(u32 fid, enum zpci_state *state) 471 { 472 struct clp_req_rsp_list_pci *rrb; 473 struct clp_fh_list_entry entry; 474 int rc; 475 476 rrb = clp_alloc_block(GFP_ATOMIC); 477 if (!rrb) 478 return -ENOMEM; 479 480 rc = clp_find_pci(rrb, fid, &entry); 481 if (!rc) { 482 *state = entry.config_state; 483 } else if (rc == -ENODEV) { 484 *state = ZPCI_FN_STATE_RESERVED; 485 rc = 0; 486 } 487 488 clp_free_block(rrb); 489 return rc; 490 } 491 492 static int clp_base_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb) 493 { 494 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request); 495 496 if (lpcb->request.hdr.len != sizeof(lpcb->request) || 497 lpcb->response.hdr.len > limit) 498 return -EINVAL; 499 return clp_req(lpcb, CLP_LPS_BASE) ? -EOPNOTSUPP : 0; 500 } 501 502 static int clp_base_command(struct clp_req *req, struct clp_req_hdr *lpcb) 503 { 504 switch (lpcb->cmd) { 505 case 0x0001: /* store logical-processor characteristics */ 506 return clp_base_slpc(req, (void *) lpcb); 507 default: 508 return -EINVAL; 509 } 510 } 511 512 static int clp_pci_slpc(struct clp_req *req, struct clp_req_rsp_slpc_pci *lpcb) 513 { 514 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request); 515 516 if (lpcb->request.hdr.len != sizeof(lpcb->request) || 517 lpcb->response.hdr.len > limit) 518 return -EINVAL; 519 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0; 520 } 521 522 static int clp_pci_list(struct clp_req *req, struct clp_req_rsp_list_pci *lpcb) 523 { 524 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request); 525 526 if (lpcb->request.hdr.len != sizeof(lpcb->request) || 527 lpcb->response.hdr.len > limit) 528 return -EINVAL; 529 if (lpcb->request.reserved2 != 0) 530 return -EINVAL; 531 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0; 532 } 533 534 static int clp_pci_query(struct clp_req *req, 535 struct clp_req_rsp_query_pci *lpcb) 536 { 537 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request); 538 539 if (lpcb->request.hdr.len != sizeof(lpcb->request) || 540 lpcb->response.hdr.len > limit) 541 return -EINVAL; 542 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0) 543 return -EINVAL; 544 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0; 545 } 546 547 static int clp_pci_query_grp(struct clp_req *req, 548 struct clp_req_rsp_query_pci_grp *lpcb) 549 { 550 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request); 551 552 if (lpcb->request.hdr.len != sizeof(lpcb->request) || 553 lpcb->response.hdr.len > limit) 554 return -EINVAL; 555 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0 || 556 lpcb->request.reserved4 != 0) 557 return -EINVAL; 558 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0; 559 } 560 561 static int clp_pci_command(struct clp_req *req, struct clp_req_hdr *lpcb) 562 { 563 switch (lpcb->cmd) { 564 case 0x0001: /* store logical-processor characteristics */ 565 return clp_pci_slpc(req, (void *) lpcb); 566 case 0x0002: /* list PCI functions */ 567 return clp_pci_list(req, (void *) lpcb); 568 case 0x0003: /* query PCI function */ 569 return clp_pci_query(req, (void *) lpcb); 570 case 0x0004: /* query PCI function group */ 571 return clp_pci_query_grp(req, (void *) lpcb); 572 default: 573 return -EINVAL; 574 } 575 } 576 577 static int clp_normal_command(struct clp_req *req) 578 { 579 struct clp_req_hdr *lpcb; 580 void __user *uptr; 581 int rc; 582 583 rc = -EINVAL; 584 if (req->lps != 0 && req->lps != 2) 585 goto out; 586 587 rc = -ENOMEM; 588 lpcb = clp_alloc_block(GFP_KERNEL); 589 if (!lpcb) 590 goto out; 591 592 rc = -EFAULT; 593 uptr = (void __force __user *)(unsigned long) req->data_p; 594 if (copy_from_user(lpcb, uptr, PAGE_SIZE) != 0) 595 goto out_free; 596 597 rc = -EINVAL; 598 if (lpcb->fmt != 0 || lpcb->reserved1 != 0 || lpcb->reserved2 != 0) 599 goto out_free; 600 601 switch (req->lps) { 602 case 0: 603 rc = clp_base_command(req, lpcb); 604 break; 605 case 2: 606 rc = clp_pci_command(req, lpcb); 607 break; 608 } 609 if (rc) 610 goto out_free; 611 612 rc = -EFAULT; 613 if (copy_to_user(uptr, lpcb, PAGE_SIZE) != 0) 614 goto out_free; 615 616 rc = 0; 617 618 out_free: 619 clp_free_block(lpcb); 620 out: 621 return rc; 622 } 623 624 static int clp_immediate_command(struct clp_req *req) 625 { 626 void __user *uptr; 627 unsigned long ilp; 628 int exists; 629 630 if (req->cmd > 1 || clp_get_ilp(&ilp) != 0) 631 return -EINVAL; 632 633 uptr = (void __force __user *)(unsigned long) req->data_p; 634 if (req->cmd == 0) { 635 /* Command code 0: test for a specific processor */ 636 exists = test_bit_inv(req->lps, &ilp); 637 return put_user(exists, (int __user *) uptr); 638 } 639 /* Command code 1: return bit mask of installed processors */ 640 return put_user(ilp, (unsigned long __user *) uptr); 641 } 642 643 static long clp_misc_ioctl(struct file *filp, unsigned int cmd, 644 unsigned long arg) 645 { 646 struct clp_req req; 647 void __user *argp; 648 649 if (cmd != CLP_SYNC) 650 return -EINVAL; 651 652 argp = is_compat_task() ? compat_ptr(arg) : (void __user *) arg; 653 if (copy_from_user(&req, argp, sizeof(req))) 654 return -EFAULT; 655 if (req.r != 0) 656 return -EINVAL; 657 return req.c ? clp_immediate_command(&req) : clp_normal_command(&req); 658 } 659 660 static int clp_misc_release(struct inode *inode, struct file *filp) 661 { 662 return 0; 663 } 664 665 static const struct file_operations clp_misc_fops = { 666 .owner = THIS_MODULE, 667 .open = nonseekable_open, 668 .release = clp_misc_release, 669 .unlocked_ioctl = clp_misc_ioctl, 670 .compat_ioctl = clp_misc_ioctl, 671 }; 672 673 static struct miscdevice clp_misc_device = { 674 .minor = MISC_DYNAMIC_MINOR, 675 .name = "clp", 676 .fops = &clp_misc_fops, 677 }; 678 679 builtin_misc_device(clp_misc_device); 680