Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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