Linux 4.13.16
[linux/fpc-iii.git] / arch / s390 / pci / pci_clp.c
blobbd534b4d40e3f3a5db21db39f1f1d20e714d7bd5
1 /*
2 * Copyright IBM Corp. 2012
4 * Author(s):
5 * Jan Glauber <jang@linux.vnet.ibm.com>
6 */
8 #define KMSG_COMPONENT "zpci"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/compat.h>
12 #include <linux/kernel.h>
13 #include <linux/miscdevice.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/pci.h>
18 #include <linux/uaccess.h>
19 #include <asm/pci_debug.h>
20 #include <asm/pci_clp.h>
21 #include <asm/compat.h>
22 #include <asm/clp.h>
23 #include <uapi/asm/clp.h>
25 bool zpci_unique_uid;
27 static inline void zpci_err_clp(unsigned int rsp, int rc)
29 struct {
30 unsigned int rsp;
31 int rc;
32 } __packed data = {rsp, rc};
34 zpci_err_hex(&data, sizeof(data));
38 * Call Logical Processor with c=1, lps=0 and command 1
39 * to get the bit mask of installed logical processors
41 static inline int clp_get_ilp(unsigned long *ilp)
43 unsigned long mask;
44 int cc = 3;
46 asm volatile (
47 " .insn rrf,0xb9a00000,%[mask],%[cmd],8,0\n"
48 "0: ipm %[cc]\n"
49 " srl %[cc],28\n"
50 "1:\n"
51 EX_TABLE(0b, 1b)
52 : [cc] "+d" (cc), [mask] "=d" (mask) : [cmd] "a" (1)
53 : "cc");
54 *ilp = mask;
55 return cc;
59 * Call Logical Processor with c=0, the give constant lps and an lpcb request.
61 static inline int clp_req(void *data, unsigned int lps)
63 struct { u8 _[CLP_BLK_SIZE]; } *req = data;
64 u64 ignored;
65 int cc = 3;
67 asm volatile (
68 " .insn rrf,0xb9a00000,%[ign],%[req],0,%[lps]\n"
69 "0: ipm %[cc]\n"
70 " srl %[cc],28\n"
71 "1:\n"
72 EX_TABLE(0b, 1b)
73 : [cc] "+d" (cc), [ign] "=d" (ignored), "+m" (*req)
74 : [req] "a" (req), [lps] "i" (lps)
75 : "cc");
76 return cc;
79 static void *clp_alloc_block(gfp_t gfp_mask)
81 return (void *) __get_free_pages(gfp_mask, get_order(CLP_BLK_SIZE));
84 static void clp_free_block(void *ptr)
86 free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE));
89 static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
90 struct clp_rsp_query_pci_grp *response)
92 zdev->tlb_refresh = response->refresh;
93 zdev->dma_mask = response->dasm;
94 zdev->msi_addr = response->msia;
95 zdev->max_msi = response->noi;
96 zdev->fmb_update = response->mui;
98 switch (response->version) {
99 case 1:
100 zdev->max_bus_speed = PCIE_SPEED_5_0GT;
101 break;
102 default:
103 zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
104 break;
108 static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
110 struct clp_req_rsp_query_pci_grp *rrb;
111 int rc;
113 rrb = clp_alloc_block(GFP_KERNEL);
114 if (!rrb)
115 return -ENOMEM;
117 memset(rrb, 0, sizeof(*rrb));
118 rrb->request.hdr.len = sizeof(rrb->request);
119 rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
120 rrb->response.hdr.len = sizeof(rrb->response);
121 rrb->request.pfgid = pfgid;
123 rc = clp_req(rrb, CLP_LPS_PCI);
124 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
125 clp_store_query_pci_fngrp(zdev, &rrb->response);
126 else {
127 zpci_err("Q PCI FGRP:\n");
128 zpci_err_clp(rrb->response.hdr.rsp, rc);
129 rc = -EIO;
131 clp_free_block(rrb);
132 return rc;
135 static int clp_store_query_pci_fn(struct zpci_dev *zdev,
136 struct clp_rsp_query_pci *response)
138 int i;
140 for (i = 0; i < PCI_BAR_COUNT; i++) {
141 zdev->bars[i].val = le32_to_cpu(response->bar[i]);
142 zdev->bars[i].size = response->bar_size[i];
144 zdev->start_dma = response->sdma;
145 zdev->end_dma = response->edma;
146 zdev->pchid = response->pchid;
147 zdev->pfgid = response->pfgid;
148 zdev->pft = response->pft;
149 zdev->vfn = response->vfn;
150 zdev->uid = response->uid;
151 zdev->fmb_length = sizeof(u32) * response->fmb_len;
153 memcpy(zdev->pfip, response->pfip, sizeof(zdev->pfip));
154 if (response->util_str_avail) {
155 memcpy(zdev->util_str, response->util_str,
156 sizeof(zdev->util_str));
159 return 0;
162 static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
164 struct clp_req_rsp_query_pci *rrb;
165 int rc;
167 rrb = clp_alloc_block(GFP_KERNEL);
168 if (!rrb)
169 return -ENOMEM;
171 memset(rrb, 0, sizeof(*rrb));
172 rrb->request.hdr.len = sizeof(rrb->request);
173 rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
174 rrb->response.hdr.len = sizeof(rrb->response);
175 rrb->request.fh = fh;
177 rc = clp_req(rrb, CLP_LPS_PCI);
178 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
179 rc = clp_store_query_pci_fn(zdev, &rrb->response);
180 if (rc)
181 goto out;
182 rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
183 } else {
184 zpci_err("Q PCI FN:\n");
185 zpci_err_clp(rrb->response.hdr.rsp, rc);
186 rc = -EIO;
188 out:
189 clp_free_block(rrb);
190 return rc;
193 int clp_add_pci_device(u32 fid, u32 fh, int configured)
195 struct zpci_dev *zdev;
196 int rc = -ENOMEM;
198 zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, configured);
199 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
200 if (!zdev)
201 goto error;
203 zdev->fh = fh;
204 zdev->fid = fid;
206 /* Query function properties and update zdev */
207 rc = clp_query_pci_fn(zdev, fh);
208 if (rc)
209 goto error;
211 if (configured)
212 zdev->state = ZPCI_FN_STATE_CONFIGURED;
213 else
214 zdev->state = ZPCI_FN_STATE_STANDBY;
216 rc = zpci_create_device(zdev);
217 if (rc)
218 goto error;
219 return 0;
221 error:
222 zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
223 kfree(zdev);
224 return rc;
228 * Enable/Disable a given PCI function defined by its function handle.
230 static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
232 struct clp_req_rsp_set_pci *rrb;
233 int rc, retries = 100;
235 rrb = clp_alloc_block(GFP_KERNEL);
236 if (!rrb)
237 return -ENOMEM;
239 do {
240 memset(rrb, 0, sizeof(*rrb));
241 rrb->request.hdr.len = sizeof(rrb->request);
242 rrb->request.hdr.cmd = CLP_SET_PCI_FN;
243 rrb->response.hdr.len = sizeof(rrb->response);
244 rrb->request.fh = *fh;
245 rrb->request.oc = command;
246 rrb->request.ndas = nr_dma_as;
248 rc = clp_req(rrb, CLP_LPS_PCI);
249 if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
250 retries--;
251 if (retries < 0)
252 break;
253 msleep(20);
255 } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
257 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
258 *fh = rrb->response.fh;
259 else {
260 zpci_err("Set PCI FN:\n");
261 zpci_err_clp(rrb->response.hdr.rsp, rc);
262 rc = -EIO;
264 clp_free_block(rrb);
265 return rc;
268 int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
270 u32 fh = zdev->fh;
271 int rc;
273 rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
274 if (!rc)
275 /* Success -> store enabled handle in zdev */
276 zdev->fh = fh;
278 zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
279 return rc;
282 int clp_disable_fh(struct zpci_dev *zdev)
284 u32 fh = zdev->fh;
285 int rc;
287 if (!zdev_enabled(zdev))
288 return 0;
290 rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
291 if (!rc)
292 /* Success -> store disabled handle in zdev */
293 zdev->fh = fh;
295 zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
296 return rc;
299 static int clp_list_pci(struct clp_req_rsp_list_pci *rrb, void *data,
300 void (*cb)(struct clp_fh_list_entry *, void *))
302 u64 resume_token = 0;
303 int entries, i, rc;
305 do {
306 memset(rrb, 0, sizeof(*rrb));
307 rrb->request.hdr.len = sizeof(rrb->request);
308 rrb->request.hdr.cmd = CLP_LIST_PCI;
309 /* store as many entries as possible */
310 rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
311 rrb->request.resume_token = resume_token;
313 /* Get PCI function handle list */
314 rc = clp_req(rrb, CLP_LPS_PCI);
315 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
316 zpci_err("List PCI FN:\n");
317 zpci_err_clp(rrb->response.hdr.rsp, rc);
318 rc = -EIO;
319 goto out;
322 zpci_unique_uid = rrb->response.uid_checking;
323 WARN_ON_ONCE(rrb->response.entry_size !=
324 sizeof(struct clp_fh_list_entry));
326 entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
327 rrb->response.entry_size;
329 resume_token = rrb->response.resume_token;
330 for (i = 0; i < entries; i++)
331 cb(&rrb->response.fh_list[i], data);
332 } while (resume_token);
333 out:
334 return rc;
337 static void __clp_add(struct clp_fh_list_entry *entry, void *data)
339 struct zpci_dev *zdev;
341 if (!entry->vendor_id)
342 return;
344 zdev = get_zdev_by_fid(entry->fid);
345 if (!zdev)
346 clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
349 static void __clp_update(struct clp_fh_list_entry *entry, void *data)
351 struct zpci_dev *zdev;
353 if (!entry->vendor_id)
354 return;
356 zdev = get_zdev_by_fid(entry->fid);
357 if (!zdev)
358 return;
360 zdev->fh = entry->fh;
363 int clp_scan_pci_devices(void)
365 struct clp_req_rsp_list_pci *rrb;
366 int rc;
368 rrb = clp_alloc_block(GFP_KERNEL);
369 if (!rrb)
370 return -ENOMEM;
372 rc = clp_list_pci(rrb, NULL, __clp_add);
374 clp_free_block(rrb);
375 return rc;
378 int clp_rescan_pci_devices(void)
380 struct clp_req_rsp_list_pci *rrb;
381 int rc;
383 zpci_remove_reserved_devices();
385 rrb = clp_alloc_block(GFP_KERNEL);
386 if (!rrb)
387 return -ENOMEM;
389 rc = clp_list_pci(rrb, NULL, __clp_add);
391 clp_free_block(rrb);
392 return rc;
395 int clp_rescan_pci_devices_simple(void)
397 struct clp_req_rsp_list_pci *rrb;
398 int rc;
400 rrb = clp_alloc_block(GFP_NOWAIT);
401 if (!rrb)
402 return -ENOMEM;
404 rc = clp_list_pci(rrb, NULL, __clp_update);
406 clp_free_block(rrb);
407 return rc;
410 struct clp_state_data {
411 u32 fid;
412 enum zpci_state state;
415 static void __clp_get_state(struct clp_fh_list_entry *entry, void *data)
417 struct clp_state_data *sd = data;
419 if (entry->fid != sd->fid)
420 return;
422 sd->state = entry->config_state;
425 int clp_get_state(u32 fid, enum zpci_state *state)
427 struct clp_req_rsp_list_pci *rrb;
428 struct clp_state_data sd = {fid, ZPCI_FN_STATE_RESERVED};
429 int rc;
431 rrb = clp_alloc_block(GFP_KERNEL);
432 if (!rrb)
433 return -ENOMEM;
435 rc = clp_list_pci(rrb, &sd, __clp_get_state);
436 if (!rc)
437 *state = sd.state;
439 clp_free_block(rrb);
440 return rc;
443 static int clp_base_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb)
445 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
447 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
448 lpcb->response.hdr.len > limit)
449 return -EINVAL;
450 return clp_req(lpcb, CLP_LPS_BASE) ? -EOPNOTSUPP : 0;
453 static int clp_base_command(struct clp_req *req, struct clp_req_hdr *lpcb)
455 switch (lpcb->cmd) {
456 case 0x0001: /* store logical-processor characteristics */
457 return clp_base_slpc(req, (void *) lpcb);
458 default:
459 return -EINVAL;
463 static int clp_pci_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb)
465 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
467 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
468 lpcb->response.hdr.len > limit)
469 return -EINVAL;
470 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
473 static int clp_pci_list(struct clp_req *req, struct clp_req_rsp_list_pci *lpcb)
475 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
477 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
478 lpcb->response.hdr.len > limit)
479 return -EINVAL;
480 if (lpcb->request.reserved2 != 0)
481 return -EINVAL;
482 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
485 static int clp_pci_query(struct clp_req *req,
486 struct clp_req_rsp_query_pci *lpcb)
488 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
490 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
491 lpcb->response.hdr.len > limit)
492 return -EINVAL;
493 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0)
494 return -EINVAL;
495 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
498 static int clp_pci_query_grp(struct clp_req *req,
499 struct clp_req_rsp_query_pci_grp *lpcb)
501 unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
503 if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
504 lpcb->response.hdr.len > limit)
505 return -EINVAL;
506 if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0 ||
507 lpcb->request.reserved4 != 0)
508 return -EINVAL;
509 return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
512 static int clp_pci_command(struct clp_req *req, struct clp_req_hdr *lpcb)
514 switch (lpcb->cmd) {
515 case 0x0001: /* store logical-processor characteristics */
516 return clp_pci_slpc(req, (void *) lpcb);
517 case 0x0002: /* list PCI functions */
518 return clp_pci_list(req, (void *) lpcb);
519 case 0x0003: /* query PCI function */
520 return clp_pci_query(req, (void *) lpcb);
521 case 0x0004: /* query PCI function group */
522 return clp_pci_query_grp(req, (void *) lpcb);
523 default:
524 return -EINVAL;
528 static int clp_normal_command(struct clp_req *req)
530 struct clp_req_hdr *lpcb;
531 void __user *uptr;
532 int rc;
534 rc = -EINVAL;
535 if (req->lps != 0 && req->lps != 2)
536 goto out;
538 rc = -ENOMEM;
539 lpcb = clp_alloc_block(GFP_KERNEL);
540 if (!lpcb)
541 goto out;
543 rc = -EFAULT;
544 uptr = (void __force __user *)(unsigned long) req->data_p;
545 if (copy_from_user(lpcb, uptr, PAGE_SIZE) != 0)
546 goto out_free;
548 rc = -EINVAL;
549 if (lpcb->fmt != 0 || lpcb->reserved1 != 0 || lpcb->reserved2 != 0)
550 goto out_free;
552 switch (req->lps) {
553 case 0:
554 rc = clp_base_command(req, lpcb);
555 break;
556 case 2:
557 rc = clp_pci_command(req, lpcb);
558 break;
560 if (rc)
561 goto out_free;
563 rc = -EFAULT;
564 if (copy_to_user(uptr, lpcb, PAGE_SIZE) != 0)
565 goto out_free;
567 rc = 0;
569 out_free:
570 clp_free_block(lpcb);
571 out:
572 return rc;
575 static int clp_immediate_command(struct clp_req *req)
577 void __user *uptr;
578 unsigned long ilp;
579 int exists;
581 if (req->cmd > 1 || clp_get_ilp(&ilp) != 0)
582 return -EINVAL;
584 uptr = (void __force __user *)(unsigned long) req->data_p;
585 if (req->cmd == 0) {
586 /* Command code 0: test for a specific processor */
587 exists = test_bit_inv(req->lps, &ilp);
588 return put_user(exists, (int __user *) uptr);
590 /* Command code 1: return bit mask of installed processors */
591 return put_user(ilp, (unsigned long __user *) uptr);
594 static long clp_misc_ioctl(struct file *filp, unsigned int cmd,
595 unsigned long arg)
597 struct clp_req req;
598 void __user *argp;
600 if (cmd != CLP_SYNC)
601 return -EINVAL;
603 argp = is_compat_task() ? compat_ptr(arg) : (void __user *) arg;
604 if (copy_from_user(&req, argp, sizeof(req)))
605 return -EFAULT;
606 if (req.r != 0)
607 return -EINVAL;
608 return req.c ? clp_immediate_command(&req) : clp_normal_command(&req);
611 static int clp_misc_release(struct inode *inode, struct file *filp)
613 return 0;
616 static const struct file_operations clp_misc_fops = {
617 .owner = THIS_MODULE,
618 .open = nonseekable_open,
619 .release = clp_misc_release,
620 .unlocked_ioctl = clp_misc_ioctl,
621 .compat_ioctl = clp_misc_ioctl,
622 .llseek = no_llseek,
625 static struct miscdevice clp_misc_device = {
626 .minor = MISC_DYNAMIC_MINOR,
627 .name = "clp",
628 .fops = &clp_misc_fops,
631 static int __init clp_misc_init(void)
633 return misc_register(&clp_misc_device);
636 device_initcall(clp_misc_init);