1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2012
6 * Jan Glauber <jang@linux.vnet.ibm.com>
9 #define KMSG_COMPONENT "zpci"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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/pci_debug.h>
21 #include <asm/pci_clp.h>
23 #include <uapi/asm/clp.h>
27 static void update_uid_checking(bool new)
29 if (zpci_unique_uid != new)
30 zpci_dbg(1, "uid checking:%d\n", new);
32 zpci_unique_uid = new;
35 static inline void zpci_err_clp(unsigned int rsp, int rc)
40 } __packed data = {rsp, rc};
42 zpci_err_hex(&data, sizeof(data));
46 * Call Logical Processor with c=1, lps=0 and command 1
47 * to get the bit mask of installed logical processors
49 static inline int clp_get_ilp(unsigned long *ilp)
55 " .insn rrf,0xb9a00000,%[mask],%[cmd],8,0\n"
60 : [cc] "+d" (cc), [mask] "=d" (mask) : [cmd] "a" (1)
67 * Call Logical Processor with c=0, the give constant lps and an lpcb request.
69 static __always_inline int clp_req(void *data, unsigned int lps)
71 struct { u8 _[CLP_BLK_SIZE]; } *req = data;
76 " .insn rrf,0xb9a00000,%[ign],%[req],0,%[lps]\n"
81 : [cc] "+d" (cc), [ign] "=d" (ignored), "+m" (*req)
82 : [req] "a" (req), [lps] "i" (lps)
87 static void *clp_alloc_block(gfp_t gfp_mask)
89 return (void *) __get_free_pages(gfp_mask, get_order(CLP_BLK_SIZE));
92 static void clp_free_block(void *ptr)
94 free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE));
97 static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
98 struct clp_rsp_query_pci_grp *response)
100 zdev->tlb_refresh = response->refresh;
101 zdev->dma_mask = response->dasm;
102 zdev->msi_addr = response->msia;
103 zdev->max_msi = response->noi;
104 zdev->fmb_update = response->mui;
106 switch (response->version) {
108 zdev->max_bus_speed = PCIE_SPEED_5_0GT;
111 zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
116 static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
118 struct clp_req_rsp_query_pci_grp *rrb;
121 rrb = clp_alloc_block(GFP_KERNEL);
125 memset(rrb, 0, sizeof(*rrb));
126 rrb->request.hdr.len = sizeof(rrb->request);
127 rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
128 rrb->response.hdr.len = sizeof(rrb->response);
129 rrb->request.pfgid = pfgid;
131 rc = clp_req(rrb, CLP_LPS_PCI);
132 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
133 clp_store_query_pci_fngrp(zdev, &rrb->response);
135 zpci_err("Q PCI FGRP:\n");
136 zpci_err_clp(rrb->response.hdr.rsp, rc);
143 static int clp_store_query_pci_fn(struct zpci_dev *zdev,
144 struct clp_rsp_query_pci *response)
148 for (i = 0; i < PCI_BAR_COUNT; i++) {
149 zdev->bars[i].val = le32_to_cpu(response->bar[i]);
150 zdev->bars[i].size = response->bar_size[i];
152 zdev->start_dma = response->sdma;
153 zdev->end_dma = response->edma;
154 zdev->pchid = response->pchid;
155 zdev->pfgid = response->pfgid;
156 zdev->pft = response->pft;
157 zdev->vfn = response->vfn;
158 zdev->uid = response->uid;
159 zdev->fmb_length = sizeof(u32) * response->fmb_len;
161 memcpy(zdev->pfip, response->pfip, sizeof(zdev->pfip));
162 if (response->util_str_avail) {
163 memcpy(zdev->util_str, response->util_str,
164 sizeof(zdev->util_str));
166 zdev->mio_capable = response->mio_addr_avail;
167 for (i = 0; i < PCI_BAR_COUNT; i++) {
168 if (!(response->mio.valid & (1 << (PCI_BAR_COUNT - i - 1))))
171 zdev->bars[i].mio_wb = (void __iomem *) response->mio.addr[i].wb;
172 zdev->bars[i].mio_wt = (void __iomem *) response->mio.addr[i].wt;
177 static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
179 struct clp_req_rsp_query_pci *rrb;
182 rrb = clp_alloc_block(GFP_KERNEL);
186 memset(rrb, 0, sizeof(*rrb));
187 rrb->request.hdr.len = sizeof(rrb->request);
188 rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
189 rrb->response.hdr.len = sizeof(rrb->response);
190 rrb->request.fh = fh;
192 rc = clp_req(rrb, CLP_LPS_PCI);
193 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
194 rc = clp_store_query_pci_fn(zdev, &rrb->response);
197 rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
199 zpci_err("Q PCI FN:\n");
200 zpci_err_clp(rrb->response.hdr.rsp, rc);
208 int clp_add_pci_device(u32 fid, u32 fh, int configured)
210 struct zpci_dev *zdev;
213 zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, configured);
214 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
221 /* Query function properties and update zdev */
222 rc = clp_query_pci_fn(zdev, fh);
227 zdev->state = ZPCI_FN_STATE_CONFIGURED;
229 zdev->state = ZPCI_FN_STATE_STANDBY;
231 rc = zpci_create_device(zdev);
237 zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
243 * Enable/Disable a given PCI function and update its function handle if
246 static int clp_set_pci_fn(struct zpci_dev *zdev, u8 nr_dma_as, u8 command)
248 struct clp_req_rsp_set_pci *rrb;
249 int rc, retries = 100;
252 rrb = clp_alloc_block(GFP_KERNEL);
257 memset(rrb, 0, sizeof(*rrb));
258 rrb->request.hdr.len = sizeof(rrb->request);
259 rrb->request.hdr.cmd = CLP_SET_PCI_FN;
260 rrb->response.hdr.len = sizeof(rrb->response);
261 rrb->request.fh = zdev->fh;
262 rrb->request.oc = command;
263 rrb->request.ndas = nr_dma_as;
265 rc = clp_req(rrb, CLP_LPS_PCI);
266 if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
272 } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
274 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
275 zpci_err("Set PCI FN:\n");
276 zpci_err_clp(rrb->response.hdr.rsp, rc);
279 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
280 zdev->fh = rrb->response.fh;
281 } else if (!rc && rrb->response.hdr.rsp == CLP_RC_SETPCIFN_ALRDY &&
282 rrb->response.fh == 0) {
283 /* Function is already in desired state - update handle */
284 rc = clp_rescan_pci_devices_simple(&fid);
290 int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
294 rc = clp_set_pci_fn(zdev, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
295 zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
299 if (zpci_use_mio(zdev)) {
300 rc = clp_set_pci_fn(zdev, nr_dma_as, CLP_SET_ENABLE_MIO);
301 zpci_dbg(3, "ena mio fid:%x, fh:%x, rc:%d\n",
302 zdev->fid, zdev->fh, rc);
304 clp_disable_fh(zdev);
310 int clp_disable_fh(struct zpci_dev *zdev)
315 if (!zdev_enabled(zdev))
318 rc = clp_set_pci_fn(zdev, 0, CLP_SET_DISABLE_PCI_FN);
319 zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, fh, rc);
323 static int clp_list_pci(struct clp_req_rsp_list_pci *rrb, void *data,
324 void (*cb)(struct clp_fh_list_entry *, void *))
326 u64 resume_token = 0;
330 memset(rrb, 0, sizeof(*rrb));
331 rrb->request.hdr.len = sizeof(rrb->request);
332 rrb->request.hdr.cmd = CLP_LIST_PCI;
333 /* store as many entries as possible */
334 rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
335 rrb->request.resume_token = resume_token;
337 /* Get PCI function handle list */
338 rc = clp_req(rrb, CLP_LPS_PCI);
339 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
340 zpci_err("List PCI FN:\n");
341 zpci_err_clp(rrb->response.hdr.rsp, rc);
346 update_uid_checking(rrb->response.uid_checking);
347 WARN_ON_ONCE(rrb->response.entry_size !=
348 sizeof(struct clp_fh_list_entry));
350 entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
351 rrb->response.entry_size;
353 resume_token = rrb->response.resume_token;
354 for (i = 0; i < entries; i++)
355 cb(&rrb->response.fh_list[i], data);
356 } while (resume_token);
361 static void __clp_add(struct clp_fh_list_entry *entry, void *data)
363 struct zpci_dev *zdev;
365 if (!entry->vendor_id)
368 zdev = get_zdev_by_fid(entry->fid);
370 clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
373 static void __clp_update(struct clp_fh_list_entry *entry, void *data)
375 struct zpci_dev *zdev;
378 if (!entry->vendor_id)
381 if (fid && *fid != entry->fid)
384 zdev = get_zdev_by_fid(entry->fid);
388 zdev->fh = entry->fh;
391 int clp_scan_pci_devices(void)
393 struct clp_req_rsp_list_pci *rrb;
396 rrb = clp_alloc_block(GFP_KERNEL);
400 rc = clp_list_pci(rrb, NULL, __clp_add);
406 int clp_rescan_pci_devices(void)
408 struct clp_req_rsp_list_pci *rrb;
411 zpci_remove_reserved_devices();
413 rrb = clp_alloc_block(GFP_KERNEL);
417 rc = clp_list_pci(rrb, NULL, __clp_add);
423 /* Rescan PCI functions and refresh function handles. If fid is non-NULL only
424 * refresh the handle of the function matching @fid
426 int clp_rescan_pci_devices_simple(u32 *fid)
428 struct clp_req_rsp_list_pci *rrb;
431 rrb = clp_alloc_block(GFP_NOWAIT);
435 rc = clp_list_pci(rrb, fid, __clp_update);
441 struct clp_state_data {
443 enum zpci_state state;
446 static void __clp_get_state(struct clp_fh_list_entry *entry, void *data)
448 struct clp_state_data *sd = data;
450 if (entry->fid != sd->fid)
453 sd->state = entry->config_state;
456 int clp_get_state(u32 fid, enum zpci_state *state)
458 struct clp_req_rsp_list_pci *rrb;
459 struct clp_state_data sd = {fid, ZPCI_FN_STATE_RESERVED};
462 rrb = clp_alloc_block(GFP_ATOMIC);
466 rc = clp_list_pci(rrb, &sd, __clp_get_state);
474 static int clp_base_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb)
476 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
478 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
479 lpcb->response.hdr.len > limit)
481 return clp_req(lpcb, CLP_LPS_BASE) ? -EOPNOTSUPP : 0;
484 static int clp_base_command(struct clp_req *req, struct clp_req_hdr *lpcb)
487 case 0x0001: /* store logical-processor characteristics */
488 return clp_base_slpc(req, (void *) lpcb);
494 static int clp_pci_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb)
496 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
498 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
499 lpcb->response.hdr.len > limit)
501 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
504 static int clp_pci_list(struct clp_req *req, struct clp_req_rsp_list_pci *lpcb)
506 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
508 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
509 lpcb->response.hdr.len > limit)
511 if (lpcb->request.reserved2 != 0)
513 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
516 static int clp_pci_query(struct clp_req *req,
517 struct clp_req_rsp_query_pci *lpcb)
519 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
521 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
522 lpcb->response.hdr.len > limit)
524 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0)
526 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
529 static int clp_pci_query_grp(struct clp_req *req,
530 struct clp_req_rsp_query_pci_grp *lpcb)
532 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
534 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
535 lpcb->response.hdr.len > limit)
537 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0 ||
538 lpcb->request.reserved4 != 0)
540 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
543 static int clp_pci_command(struct clp_req *req, struct clp_req_hdr *lpcb)
546 case 0x0001: /* store logical-processor characteristics */
547 return clp_pci_slpc(req, (void *) lpcb);
548 case 0x0002: /* list PCI functions */
549 return clp_pci_list(req, (void *) lpcb);
550 case 0x0003: /* query PCI function */
551 return clp_pci_query(req, (void *) lpcb);
552 case 0x0004: /* query PCI function group */
553 return clp_pci_query_grp(req, (void *) lpcb);
559 static int clp_normal_command(struct clp_req *req)
561 struct clp_req_hdr *lpcb;
566 if (req->lps != 0 && req->lps != 2)
570 lpcb = clp_alloc_block(GFP_KERNEL);
575 uptr = (void __force __user *)(unsigned long) req->data_p;
576 if (copy_from_user(lpcb, uptr, PAGE_SIZE) != 0)
580 if (lpcb->fmt != 0 || lpcb->reserved1 != 0 || lpcb->reserved2 != 0)
585 rc = clp_base_command(req, lpcb);
588 rc = clp_pci_command(req, lpcb);
595 if (copy_to_user(uptr, lpcb, PAGE_SIZE) != 0)
601 clp_free_block(lpcb);
606 static int clp_immediate_command(struct clp_req *req)
612 if (req->cmd > 1 || clp_get_ilp(&ilp) != 0)
615 uptr = (void __force __user *)(unsigned long) req->data_p;
617 /* Command code 0: test for a specific processor */
618 exists = test_bit_inv(req->lps, &ilp);
619 return put_user(exists, (int __user *) uptr);
621 /* Command code 1: return bit mask of installed processors */
622 return put_user(ilp, (unsigned long __user *) uptr);
625 static long clp_misc_ioctl(struct file *filp, unsigned int cmd,
634 argp = is_compat_task() ? compat_ptr(arg) : (void __user *) arg;
635 if (copy_from_user(&req, argp, sizeof(req)))
639 return req.c ? clp_immediate_command(&req) : clp_normal_command(&req);
642 static int clp_misc_release(struct inode *inode, struct file *filp)
647 static const struct file_operations clp_misc_fops = {
648 .owner = THIS_MODULE,
649 .open = nonseekable_open,
650 .release = clp_misc_release,
651 .unlocked_ioctl = clp_misc_ioctl,
652 .compat_ioctl = clp_misc_ioctl,
656 static struct miscdevice clp_misc_device = {
657 .minor = MISC_DYNAMIC_MINOR,
659 .fops = &clp_misc_fops,
662 static int __init clp_misc_init(void)
664 return misc_register(&clp_misc_device);
667 device_initcall(clp_misc_init);