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/asm-extable.h>
21 #include <asm/pci_debug.h>
22 #include <asm/pci_clp.h>
24 #include <uapi/asm/clp.h>
30 void update_uid_checking(bool new)
32 if (zpci_unique_uid != new)
33 zpci_dbg(3, "uid checking:%d\n", new);
35 zpci_unique_uid = new;
38 static inline void zpci_err_clp(unsigned int rsp, int rc)
43 } __packed data = {rsp, rc};
45 zpci_err_hex(&data, sizeof(data));
49 * Call Logical Processor with c=1, lps=0 and command 1
50 * to get the bit mask of installed logical processors
52 static inline int clp_get_ilp(unsigned long *ilp)
58 " .insn rrf,0xb9a00000,%[mask],%[cmd],8,0\n"
63 : [cc] "+d" (cc), [mask] "=d" (mask) : [cmd] "a" (1)
70 * Call Logical Processor with c=0, the give constant lps and an lpcb request.
72 static __always_inline int clp_req(void *data, unsigned int lps)
74 struct { u8 _[CLP_BLK_SIZE]; } *req = data;
79 " .insn rrf,0xb9a00000,%[ign],%[req],0,%[lps]\n"
84 : [cc] "+d" (cc), [ign] "=d" (ignored), "+m" (*req)
85 : [req] "a" (req), [lps] "i" (lps)
90 static void *clp_alloc_block(gfp_t gfp_mask)
92 return (void *) __get_free_pages(gfp_mask, get_order(CLP_BLK_SIZE));
95 static void clp_free_block(void *ptr)
97 free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE));
100 static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
101 struct clp_rsp_query_pci_grp *response)
103 zdev->tlb_refresh = response->refresh;
104 zdev->dma_mask = response->dasm;
105 zdev->msi_addr = response->msia;
106 zdev->max_msi = response->noi;
107 zdev->fmb_update = response->mui;
108 zdev->version = response->version;
109 zdev->maxstbl = response->maxstbl;
110 zdev->dtsm = response->dtsm;
112 switch (response->version) {
114 zdev->max_bus_speed = PCIE_SPEED_5_0GT;
117 zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
122 static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
124 struct clp_req_rsp_query_pci_grp *rrb;
127 rrb = clp_alloc_block(GFP_KERNEL);
131 memset(rrb, 0, sizeof(*rrb));
132 rrb->request.hdr.len = sizeof(rrb->request);
133 rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
134 rrb->response.hdr.len = sizeof(rrb->response);
135 rrb->request.pfgid = pfgid;
137 rc = clp_req(rrb, CLP_LPS_PCI);
138 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
139 clp_store_query_pci_fngrp(zdev, &rrb->response);
141 zpci_err("Q PCI FGRP:\n");
142 zpci_err_clp(rrb->response.hdr.rsp, rc);
149 static int clp_store_query_pci_fn(struct zpci_dev *zdev,
150 struct clp_rsp_query_pci *response)
154 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
155 zdev->bars[i].val = le32_to_cpu(response->bar[i]);
156 zdev->bars[i].size = response->bar_size[i];
158 zdev->start_dma = response->sdma;
159 zdev->end_dma = response->edma;
160 zdev->pchid = response->pchid;
161 zdev->pfgid = response->pfgid;
162 zdev->pft = response->pft;
163 zdev->vfn = response->vfn;
164 zdev->port = response->port;
165 zdev->uid = response->uid;
166 zdev->fmb_length = sizeof(u32) * response->fmb_len;
167 zdev->rid_available = response->rid_avail;
168 zdev->is_physfn = response->is_physfn;
169 if (!s390_pci_no_rid && zdev->rid_available)
170 zdev->devfn = response->rid & ZPCI_RID_MASK_DEVFN;
172 memcpy(zdev->pfip, response->pfip, sizeof(zdev->pfip));
173 if (response->util_str_avail) {
174 memcpy(zdev->util_str, response->util_str,
175 sizeof(zdev->util_str));
176 zdev->util_str_avail = 1;
178 zdev->mio_capable = response->mio_addr_avail;
179 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
180 if (!(response->mio.valid & (1 << (PCI_STD_NUM_BARS - i - 1))))
183 zdev->bars[i].mio_wb = (void __iomem *) response->mio.addr[i].wb;
184 zdev->bars[i].mio_wt = (void __iomem *) response->mio.addr[i].wt;
189 int clp_query_pci_fn(struct zpci_dev *zdev)
191 struct clp_req_rsp_query_pci *rrb;
194 rrb = clp_alloc_block(GFP_KERNEL);
198 memset(rrb, 0, sizeof(*rrb));
199 rrb->request.hdr.len = sizeof(rrb->request);
200 rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
201 rrb->response.hdr.len = sizeof(rrb->response);
202 rrb->request.fh = zdev->fh;
204 rc = clp_req(rrb, CLP_LPS_PCI);
205 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
206 rc = clp_store_query_pci_fn(zdev, &rrb->response);
209 rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
211 zpci_err("Q PCI FN:\n");
212 zpci_err_clp(rrb->response.hdr.rsp, rc);
221 * clp_set_pci_fn() - Execute a command on a PCI function
222 * @zdev: Function that will be affected
223 * @fh: Out parameter for updated function handle
224 * @nr_dma_as: DMA address space number
225 * @command: The command code to execute
227 * Returns: 0 on success, < 0 for Linux errors (e.g. -ENOMEM), and
228 * > 0 for non-success platform responses
230 static int clp_set_pci_fn(struct zpci_dev *zdev, u32 *fh, u8 nr_dma_as, u8 command)
232 struct clp_req_rsp_set_pci *rrb;
233 int rc, retries = 100;
237 rrb = clp_alloc_block(GFP_KERNEL);
241 if (command != CLP_SET_DISABLE_PCI_FN)
245 memset(rrb, 0, sizeof(*rrb));
246 rrb->request.hdr.len = sizeof(rrb->request);
247 rrb->request.hdr.cmd = CLP_SET_PCI_FN;
248 rrb->response.hdr.len = sizeof(rrb->response);
249 rrb->request.fh = zdev->fh;
250 rrb->request.oc = command;
251 rrb->request.ndas = nr_dma_as;
252 rrb->request.gisa = gisa;
254 rc = clp_req(rrb, CLP_LPS_PCI);
255 if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
261 } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
263 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
264 *fh = rrb->response.fh;
266 zpci_err("Set PCI FN:\n");
267 zpci_err_clp(rrb->response.hdr.rsp, rc);
269 rc = rrb->response.hdr.rsp;
275 int clp_setup_writeback_mio(void)
277 struct clp_req_rsp_slpc_pci *rrb;
281 rrb = clp_alloc_block(GFP_KERNEL);
285 memset(rrb, 0, sizeof(*rrb));
286 rrb->request.hdr.len = sizeof(rrb->request);
287 rrb->request.hdr.cmd = CLP_SLPC;
288 rrb->response.hdr.len = sizeof(rrb->response);
290 rc = clp_req(rrb, CLP_LPS_PCI);
291 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
292 if (rrb->response.vwb) {
293 wb_bit_pos = rrb->response.mio_wb;
294 set_bit_inv(wb_bit_pos, &mio_wb_bit_mask);
295 zpci_dbg(3, "wb bit: %d\n", wb_bit_pos);
297 zpci_dbg(3, "wb bit: n.a.\n");
301 zpci_err("SLPC PCI:\n");
302 zpci_err_clp(rrb->response.hdr.rsp, rc);
309 int clp_enable_fh(struct zpci_dev *zdev, u32 *fh, u8 nr_dma_as)
313 rc = clp_set_pci_fn(zdev, fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
314 zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, *fh, rc);
315 if (!rc && zpci_use_mio(zdev)) {
316 rc = clp_set_pci_fn(zdev, fh, nr_dma_as, CLP_SET_ENABLE_MIO);
317 zpci_dbg(3, "ena mio fid:%x, fh:%x, rc:%d\n",
320 clp_disable_fh(zdev, fh);
325 int clp_disable_fh(struct zpci_dev *zdev, u32 *fh)
329 if (!zdev_enabled(zdev))
332 rc = clp_set_pci_fn(zdev, fh, 0, CLP_SET_DISABLE_PCI_FN);
333 zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, *fh, rc);
337 static int clp_list_pci_req(struct clp_req_rsp_list_pci *rrb,
338 u64 *resume_token, int *nentries)
342 memset(rrb, 0, sizeof(*rrb));
343 rrb->request.hdr.len = sizeof(rrb->request);
344 rrb->request.hdr.cmd = CLP_LIST_PCI;
345 /* store as many entries as possible */
346 rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
347 rrb->request.resume_token = *resume_token;
349 /* Get PCI function handle list */
350 rc = clp_req(rrb, CLP_LPS_PCI);
351 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
352 zpci_err("List PCI FN:\n");
353 zpci_err_clp(rrb->response.hdr.rsp, rc);
357 update_uid_checking(rrb->response.uid_checking);
358 WARN_ON_ONCE(rrb->response.entry_size !=
359 sizeof(struct clp_fh_list_entry));
361 *nentries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
362 rrb->response.entry_size;
363 *resume_token = rrb->response.resume_token;
368 static int clp_list_pci(struct clp_req_rsp_list_pci *rrb, void *data,
369 void (*cb)(struct clp_fh_list_entry *, void *))
371 u64 resume_token = 0;
375 rc = clp_list_pci_req(rrb, &resume_token, &nentries);
378 for (i = 0; i < nentries; i++)
379 cb(&rrb->response.fh_list[i], data);
380 } while (resume_token);
385 static int clp_find_pci(struct clp_req_rsp_list_pci *rrb, u32 fid,
386 struct clp_fh_list_entry *entry)
388 struct clp_fh_list_entry *fh_list;
389 u64 resume_token = 0;
393 rc = clp_list_pci_req(rrb, &resume_token, &nentries);
396 fh_list = rrb->response.fh_list;
397 for (i = 0; i < nentries; i++) {
398 if (fh_list[i].fid == fid) {
403 } while (resume_token);
408 static void __clp_add(struct clp_fh_list_entry *entry, void *data)
410 struct zpci_dev *zdev;
412 if (!entry->vendor_id)
415 zdev = get_zdev_by_fid(entry->fid);
420 zpci_create_device(entry->fid, entry->fh, entry->config_state);
423 int clp_scan_pci_devices(void)
425 struct clp_req_rsp_list_pci *rrb;
428 rrb = clp_alloc_block(GFP_KERNEL);
432 rc = clp_list_pci(rrb, NULL, __clp_add);
439 * Get the current function handle of the function matching @fid
441 int clp_refresh_fh(u32 fid, u32 *fh)
443 struct clp_req_rsp_list_pci *rrb;
444 struct clp_fh_list_entry entry;
447 rrb = clp_alloc_block(GFP_NOWAIT);
451 rc = clp_find_pci(rrb, fid, &entry);
459 int clp_get_state(u32 fid, enum zpci_state *state)
461 struct clp_req_rsp_list_pci *rrb;
462 struct clp_fh_list_entry entry;
465 rrb = clp_alloc_block(GFP_ATOMIC);
469 rc = clp_find_pci(rrb, fid, &entry);
471 *state = entry.config_state;
472 } else if (rc == -ENODEV) {
473 *state = ZPCI_FN_STATE_RESERVED;
481 static int clp_base_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb)
483 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
485 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
486 lpcb->response.hdr.len > limit)
488 return clp_req(lpcb, CLP_LPS_BASE) ? -EOPNOTSUPP : 0;
491 static int clp_base_command(struct clp_req *req, struct clp_req_hdr *lpcb)
494 case 0x0001: /* store logical-processor characteristics */
495 return clp_base_slpc(req, (void *) lpcb);
501 static int clp_pci_slpc(struct clp_req *req, struct clp_req_rsp_slpc_pci *lpcb)
503 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
505 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
506 lpcb->response.hdr.len > limit)
508 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
511 static int clp_pci_list(struct clp_req *req, struct clp_req_rsp_list_pci *lpcb)
513 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
515 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
516 lpcb->response.hdr.len > limit)
518 if (lpcb->request.reserved2 != 0)
520 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
523 static int clp_pci_query(struct clp_req *req,
524 struct clp_req_rsp_query_pci *lpcb)
526 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
528 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
529 lpcb->response.hdr.len > limit)
531 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0)
533 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
536 static int clp_pci_query_grp(struct clp_req *req,
537 struct clp_req_rsp_query_pci_grp *lpcb)
539 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
541 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
542 lpcb->response.hdr.len > limit)
544 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0 ||
545 lpcb->request.reserved4 != 0)
547 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
550 static int clp_pci_command(struct clp_req *req, struct clp_req_hdr *lpcb)
553 case 0x0001: /* store logical-processor characteristics */
554 return clp_pci_slpc(req, (void *) lpcb);
555 case 0x0002: /* list PCI functions */
556 return clp_pci_list(req, (void *) lpcb);
557 case 0x0003: /* query PCI function */
558 return clp_pci_query(req, (void *) lpcb);
559 case 0x0004: /* query PCI function group */
560 return clp_pci_query_grp(req, (void *) lpcb);
566 static int clp_normal_command(struct clp_req *req)
568 struct clp_req_hdr *lpcb;
573 if (req->lps != 0 && req->lps != 2)
577 lpcb = clp_alloc_block(GFP_KERNEL);
582 uptr = (void __force __user *)(unsigned long) req->data_p;
583 if (copy_from_user(lpcb, uptr, PAGE_SIZE) != 0)
587 if (lpcb->fmt != 0 || lpcb->reserved1 != 0 || lpcb->reserved2 != 0)
592 rc = clp_base_command(req, lpcb);
595 rc = clp_pci_command(req, lpcb);
602 if (copy_to_user(uptr, lpcb, PAGE_SIZE) != 0)
608 clp_free_block(lpcb);
613 static int clp_immediate_command(struct clp_req *req)
619 if (req->cmd > 1 || clp_get_ilp(&ilp) != 0)
622 uptr = (void __force __user *)(unsigned long) req->data_p;
624 /* Command code 0: test for a specific processor */
625 exists = test_bit_inv(req->lps, &ilp);
626 return put_user(exists, (int __user *) uptr);
628 /* Command code 1: return bit mask of installed processors */
629 return put_user(ilp, (unsigned long __user *) uptr);
632 static long clp_misc_ioctl(struct file *filp, unsigned int cmd,
641 argp = is_compat_task() ? compat_ptr(arg) : (void __user *) arg;
642 if (copy_from_user(&req, argp, sizeof(req)))
646 return req.c ? clp_immediate_command(&req) : clp_normal_command(&req);
649 static int clp_misc_release(struct inode *inode, struct file *filp)
654 static const struct file_operations clp_misc_fops = {
655 .owner = THIS_MODULE,
656 .open = nonseekable_open,
657 .release = clp_misc_release,
658 .unlocked_ioctl = clp_misc_ioctl,
659 .compat_ioctl = clp_misc_ioctl,
663 static struct miscdevice clp_misc_device = {
664 .minor = MISC_DYNAMIC_MINOR,
666 .fops = &clp_misc_fops,
669 builtin_misc_device(clp_misc_device);