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 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;
105 zdev->version = response->version;
107 switch (response->version) {
109 zdev->max_bus_speed = PCIE_SPEED_5_0GT;
112 zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
117 static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
119 struct clp_req_rsp_query_pci_grp *rrb;
122 rrb = clp_alloc_block(GFP_KERNEL);
126 memset(rrb, 0, sizeof(*rrb));
127 rrb->request.hdr.len = sizeof(rrb->request);
128 rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
129 rrb->response.hdr.len = sizeof(rrb->response);
130 rrb->request.pfgid = pfgid;
132 rc = clp_req(rrb, CLP_LPS_PCI);
133 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
134 clp_store_query_pci_fngrp(zdev, &rrb->response);
136 zpci_err("Q PCI FGRP:\n");
137 zpci_err_clp(rrb->response.hdr.rsp, rc);
144 static int clp_store_query_pci_fn(struct zpci_dev *zdev,
145 struct clp_rsp_query_pci *response)
149 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
150 zdev->bars[i].val = le32_to_cpu(response->bar[i]);
151 zdev->bars[i].size = response->bar_size[i];
153 zdev->start_dma = response->sdma;
154 zdev->end_dma = response->edma;
155 zdev->pchid = response->pchid;
156 zdev->pfgid = response->pfgid;
157 zdev->pft = response->pft;
158 zdev->vfn = response->vfn;
159 zdev->port = response->port;
160 zdev->uid = response->uid;
161 zdev->fmb_length = sizeof(u32) * response->fmb_len;
162 zdev->rid_available = response->rid_avail;
163 zdev->is_physfn = response->is_physfn;
164 if (!s390_pci_no_rid && zdev->rid_available)
165 zdev->devfn = response->rid & ZPCI_RID_MASK_DEVFN;
167 memcpy(zdev->pfip, response->pfip, sizeof(zdev->pfip));
168 if (response->util_str_avail) {
169 memcpy(zdev->util_str, response->util_str,
170 sizeof(zdev->util_str));
171 zdev->util_str_avail = 1;
173 zdev->mio_capable = response->mio_addr_avail;
174 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
175 if (!(response->mio.valid & (1 << (PCI_STD_NUM_BARS - i - 1))))
178 zdev->bars[i].mio_wb = (void __iomem *) response->mio.addr[i].wb;
179 zdev->bars[i].mio_wt = (void __iomem *) response->mio.addr[i].wt;
184 int clp_query_pci_fn(struct zpci_dev *zdev)
186 struct clp_req_rsp_query_pci *rrb;
189 rrb = clp_alloc_block(GFP_KERNEL);
193 memset(rrb, 0, sizeof(*rrb));
194 rrb->request.hdr.len = sizeof(rrb->request);
195 rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
196 rrb->response.hdr.len = sizeof(rrb->response);
197 rrb->request.fh = zdev->fh;
199 rc = clp_req(rrb, CLP_LPS_PCI);
200 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
201 rc = clp_store_query_pci_fn(zdev, &rrb->response);
204 rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
206 zpci_err("Q PCI FN:\n");
207 zpci_err_clp(rrb->response.hdr.rsp, rc);
215 static int clp_refresh_fh(u32 fid);
217 * clp_set_pci_fn() - Execute a command on a PCI function
218 * @zdev: Function that will be affected
219 * @nr_dma_as: DMA address space number
220 * @command: The command code to execute
222 * Returns: 0 on success, < 0 for Linux errors (e.g. -ENOMEM), and
223 * > 0 for non-success platform responses
225 static int clp_set_pci_fn(struct zpci_dev *zdev, u8 nr_dma_as, u8 command)
227 struct clp_req_rsp_set_pci *rrb;
228 int rc, retries = 100;
230 rrb = clp_alloc_block(GFP_KERNEL);
235 memset(rrb, 0, sizeof(*rrb));
236 rrb->request.hdr.len = sizeof(rrb->request);
237 rrb->request.hdr.cmd = CLP_SET_PCI_FN;
238 rrb->response.hdr.len = sizeof(rrb->response);
239 rrb->request.fh = zdev->fh;
240 rrb->request.oc = command;
241 rrb->request.ndas = nr_dma_as;
243 rc = clp_req(rrb, CLP_LPS_PCI);
244 if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
250 } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
252 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
253 zdev->fh = rrb->response.fh;
254 } else if (!rc && rrb->response.hdr.rsp == CLP_RC_SETPCIFN_ALRDY) {
255 /* Function is already in desired state - update handle */
256 rc = clp_refresh_fh(zdev->fid);
258 zpci_err("Set PCI FN:\n");
259 zpci_err_clp(rrb->response.hdr.rsp, rc);
261 rc = rrb->response.hdr.rsp;
267 int clp_setup_writeback_mio(void)
269 struct clp_req_rsp_slpc_pci *rrb;
273 rrb = clp_alloc_block(GFP_KERNEL);
277 memset(rrb, 0, sizeof(*rrb));
278 rrb->request.hdr.len = sizeof(rrb->request);
279 rrb->request.hdr.cmd = CLP_SLPC;
280 rrb->response.hdr.len = sizeof(rrb->response);
282 rc = clp_req(rrb, CLP_LPS_PCI);
283 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
284 if (rrb->response.vwb) {
285 wb_bit_pos = rrb->response.mio_wb;
286 set_bit_inv(wb_bit_pos, &mio_wb_bit_mask);
287 zpci_dbg(3, "wb bit: %d\n", wb_bit_pos);
289 zpci_dbg(3, "wb bit: n.a.\n");
293 zpci_err("SLPC PCI:\n");
294 zpci_err_clp(rrb->response.hdr.rsp, rc);
301 int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
305 rc = clp_set_pci_fn(zdev, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
306 zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
307 if (!rc && zpci_use_mio(zdev)) {
308 rc = clp_set_pci_fn(zdev, nr_dma_as, CLP_SET_ENABLE_MIO);
309 zpci_dbg(3, "ena mio fid:%x, fh:%x, rc:%d\n",
310 zdev->fid, zdev->fh, rc);
312 clp_disable_fh(zdev);
317 int clp_disable_fh(struct zpci_dev *zdev)
321 if (!zdev_enabled(zdev))
324 rc = clp_set_pci_fn(zdev, 0, CLP_SET_DISABLE_PCI_FN);
325 zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
329 static int clp_list_pci(struct clp_req_rsp_list_pci *rrb, void *data,
330 void (*cb)(struct clp_fh_list_entry *, void *))
332 u64 resume_token = 0;
336 memset(rrb, 0, sizeof(*rrb));
337 rrb->request.hdr.len = sizeof(rrb->request);
338 rrb->request.hdr.cmd = CLP_LIST_PCI;
339 /* store as many entries as possible */
340 rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
341 rrb->request.resume_token = resume_token;
343 /* Get PCI function handle list */
344 rc = clp_req(rrb, CLP_LPS_PCI);
345 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
346 zpci_err("List PCI FN:\n");
347 zpci_err_clp(rrb->response.hdr.rsp, rc);
352 update_uid_checking(rrb->response.uid_checking);
353 WARN_ON_ONCE(rrb->response.entry_size !=
354 sizeof(struct clp_fh_list_entry));
356 entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
357 rrb->response.entry_size;
359 resume_token = rrb->response.resume_token;
360 for (i = 0; i < entries; i++)
361 cb(&rrb->response.fh_list[i], data);
362 } while (resume_token);
367 static void __clp_add(struct clp_fh_list_entry *entry, void *data)
369 struct zpci_dev *zdev;
371 if (!entry->vendor_id)
374 zdev = get_zdev_by_fid(entry->fid);
376 zpci_create_device(entry->fid, entry->fh, entry->config_state);
379 int clp_scan_pci_devices(void)
381 struct clp_req_rsp_list_pci *rrb;
384 rrb = clp_alloc_block(GFP_KERNEL);
388 rc = clp_list_pci(rrb, NULL, __clp_add);
394 static void __clp_refresh_fh(struct clp_fh_list_entry *entry, void *data)
396 struct zpci_dev *zdev;
397 u32 fid = *((u32 *)data);
399 if (!entry->vendor_id || fid != entry->fid)
402 zdev = get_zdev_by_fid(fid);
406 zdev->fh = entry->fh;
410 * Refresh the function handle of the function matching @fid
412 static int clp_refresh_fh(u32 fid)
414 struct clp_req_rsp_list_pci *rrb;
417 rrb = clp_alloc_block(GFP_NOWAIT);
421 rc = clp_list_pci(rrb, &fid, __clp_refresh_fh);
427 struct clp_state_data {
429 enum zpci_state state;
432 static void __clp_get_state(struct clp_fh_list_entry *entry, void *data)
434 struct clp_state_data *sd = data;
436 if (entry->fid != sd->fid)
439 sd->state = entry->config_state;
442 int clp_get_state(u32 fid, enum zpci_state *state)
444 struct clp_req_rsp_list_pci *rrb;
445 struct clp_state_data sd = {fid, ZPCI_FN_STATE_RESERVED};
448 rrb = clp_alloc_block(GFP_ATOMIC);
452 rc = clp_list_pci(rrb, &sd, __clp_get_state);
460 static int clp_base_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb)
462 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
464 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
465 lpcb->response.hdr.len > limit)
467 return clp_req(lpcb, CLP_LPS_BASE) ? -EOPNOTSUPP : 0;
470 static int clp_base_command(struct clp_req *req, struct clp_req_hdr *lpcb)
473 case 0x0001: /* store logical-processor characteristics */
474 return clp_base_slpc(req, (void *) lpcb);
480 static int clp_pci_slpc(struct clp_req *req, struct clp_req_rsp_slpc_pci *lpcb)
482 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
484 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
485 lpcb->response.hdr.len > limit)
487 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
490 static int clp_pci_list(struct clp_req *req, struct clp_req_rsp_list_pci *lpcb)
492 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
494 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
495 lpcb->response.hdr.len > limit)
497 if (lpcb->request.reserved2 != 0)
499 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
502 static int clp_pci_query(struct clp_req *req,
503 struct clp_req_rsp_query_pci *lpcb)
505 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
507 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
508 lpcb->response.hdr.len > limit)
510 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0)
512 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
515 static int clp_pci_query_grp(struct clp_req *req,
516 struct clp_req_rsp_query_pci_grp *lpcb)
518 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
520 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
521 lpcb->response.hdr.len > limit)
523 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0 ||
524 lpcb->request.reserved4 != 0)
526 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
529 static int clp_pci_command(struct clp_req *req, struct clp_req_hdr *lpcb)
532 case 0x0001: /* store logical-processor characteristics */
533 return clp_pci_slpc(req, (void *) lpcb);
534 case 0x0002: /* list PCI functions */
535 return clp_pci_list(req, (void *) lpcb);
536 case 0x0003: /* query PCI function */
537 return clp_pci_query(req, (void *) lpcb);
538 case 0x0004: /* query PCI function group */
539 return clp_pci_query_grp(req, (void *) lpcb);
545 static int clp_normal_command(struct clp_req *req)
547 struct clp_req_hdr *lpcb;
552 if (req->lps != 0 && req->lps != 2)
556 lpcb = clp_alloc_block(GFP_KERNEL);
561 uptr = (void __force __user *)(unsigned long) req->data_p;
562 if (copy_from_user(lpcb, uptr, PAGE_SIZE) != 0)
566 if (lpcb->fmt != 0 || lpcb->reserved1 != 0 || lpcb->reserved2 != 0)
571 rc = clp_base_command(req, lpcb);
574 rc = clp_pci_command(req, lpcb);
581 if (copy_to_user(uptr, lpcb, PAGE_SIZE) != 0)
587 clp_free_block(lpcb);
592 static int clp_immediate_command(struct clp_req *req)
598 if (req->cmd > 1 || clp_get_ilp(&ilp) != 0)
601 uptr = (void __force __user *)(unsigned long) req->data_p;
603 /* Command code 0: test for a specific processor */
604 exists = test_bit_inv(req->lps, &ilp);
605 return put_user(exists, (int __user *) uptr);
607 /* Command code 1: return bit mask of installed processors */
608 return put_user(ilp, (unsigned long __user *) uptr);
611 static long clp_misc_ioctl(struct file *filp, unsigned int cmd,
620 argp = is_compat_task() ? compat_ptr(arg) : (void __user *) arg;
621 if (copy_from_user(&req, argp, sizeof(req)))
625 return req.c ? clp_immediate_command(&req) : clp_normal_command(&req);
628 static int clp_misc_release(struct inode *inode, struct file *filp)
633 static const struct file_operations clp_misc_fops = {
634 .owner = THIS_MODULE,
635 .open = nonseekable_open,
636 .release = clp_misc_release,
637 .unlocked_ioctl = clp_misc_ioctl,
638 .compat_ioctl = clp_misc_ioctl,
642 static struct miscdevice clp_misc_device = {
643 .minor = MISC_DYNAMIC_MINOR,
645 .fops = &clp_misc_fops,
648 static int __init clp_misc_init(void)
650 return misc_register(&clp_misc_device);
653 device_initcall(clp_misc_init);