xref: /openbmc/linux/drivers/s390/crypto/ap_card.c (revision 4f2fcccd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2016
4  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
5  *
6  * Adjunct processor bus, card related code.
7  */
8 
9 #define KMSG_COMPONENT "ap"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <asm/facility.h>
15 
16 #include "ap_bus.h"
17 
18 /*
19  * AP card related attributes.
20  */
21 static ssize_t hwtype_show(struct device *dev,
22 			   struct device_attribute *attr, char *buf)
23 {
24 	struct ap_card *ac = to_ap_card(dev);
25 
26 	return scnprintf(buf, PAGE_SIZE, "%d\n", ac->ap_dev.device_type);
27 }
28 
29 static DEVICE_ATTR_RO(hwtype);
30 
31 static ssize_t raw_hwtype_show(struct device *dev,
32 			       struct device_attribute *attr, char *buf)
33 {
34 	struct ap_card *ac = to_ap_card(dev);
35 
36 	return scnprintf(buf, PAGE_SIZE, "%d\n", ac->raw_hwtype);
37 }
38 
39 static DEVICE_ATTR_RO(raw_hwtype);
40 
41 static ssize_t depth_show(struct device *dev, struct device_attribute *attr,
42 			  char *buf)
43 {
44 	struct ap_card *ac = to_ap_card(dev);
45 
46 	return scnprintf(buf, PAGE_SIZE, "%d\n", ac->queue_depth);
47 }
48 
49 static DEVICE_ATTR_RO(depth);
50 
51 static ssize_t ap_functions_show(struct device *dev,
52 				 struct device_attribute *attr, char *buf)
53 {
54 	struct ap_card *ac = to_ap_card(dev);
55 
56 	return scnprintf(buf, PAGE_SIZE, "0x%08X\n", ac->functions);
57 }
58 
59 static DEVICE_ATTR_RO(ap_functions);
60 
61 static ssize_t request_count_show(struct device *dev,
62 				  struct device_attribute *attr,
63 				  char *buf)
64 {
65 	struct ap_card *ac = to_ap_card(dev);
66 	u64 req_cnt;
67 
68 	req_cnt = 0;
69 	spin_lock_bh(&ap_queues_lock);
70 	req_cnt = atomic64_read(&ac->total_request_count);
71 	spin_unlock_bh(&ap_queues_lock);
72 	return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
73 }
74 
75 static ssize_t request_count_store(struct device *dev,
76 				   struct device_attribute *attr,
77 				   const char *buf, size_t count)
78 {
79 	int bkt;
80 	struct ap_queue *aq;
81 	struct ap_card *ac = to_ap_card(dev);
82 
83 	spin_lock_bh(&ap_queues_lock);
84 	hash_for_each(ap_queues, bkt, aq, hnode)
85 		if (ac == aq->card)
86 			aq->total_request_count = 0;
87 	spin_unlock_bh(&ap_queues_lock);
88 	atomic64_set(&ac->total_request_count, 0);
89 
90 	return count;
91 }
92 
93 static DEVICE_ATTR_RW(request_count);
94 
95 static ssize_t requestq_count_show(struct device *dev,
96 				   struct device_attribute *attr, char *buf)
97 {
98 	int bkt;
99 	struct ap_queue *aq;
100 	unsigned int reqq_cnt;
101 	struct ap_card *ac = to_ap_card(dev);
102 
103 	reqq_cnt = 0;
104 	spin_lock_bh(&ap_queues_lock);
105 	hash_for_each(ap_queues, bkt, aq, hnode)
106 		if (ac == aq->card)
107 			reqq_cnt += aq->requestq_count;
108 	spin_unlock_bh(&ap_queues_lock);
109 	return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
110 }
111 
112 static DEVICE_ATTR_RO(requestq_count);
113 
114 static ssize_t pendingq_count_show(struct device *dev,
115 				   struct device_attribute *attr, char *buf)
116 {
117 	int bkt;
118 	struct ap_queue *aq;
119 	unsigned int penq_cnt;
120 	struct ap_card *ac = to_ap_card(dev);
121 
122 	penq_cnt = 0;
123 	spin_lock_bh(&ap_queues_lock);
124 	hash_for_each(ap_queues, bkt, aq, hnode)
125 		if (ac == aq->card)
126 			penq_cnt += aq->pendingq_count;
127 	spin_unlock_bh(&ap_queues_lock);
128 	return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
129 }
130 
131 static DEVICE_ATTR_RO(pendingq_count);
132 
133 static ssize_t modalias_show(struct device *dev,
134 			     struct device_attribute *attr, char *buf)
135 {
136 	return scnprintf(buf, PAGE_SIZE, "ap:t%02X\n",
137 			 to_ap_dev(dev)->device_type);
138 }
139 
140 static DEVICE_ATTR_RO(modalias);
141 
142 static ssize_t config_show(struct device *dev,
143 			   struct device_attribute *attr, char *buf)
144 {
145 	struct ap_card *ac = to_ap_card(dev);
146 
147 	return scnprintf(buf, PAGE_SIZE, "%d\n", ac->config ? 1 : 0);
148 }
149 
150 static DEVICE_ATTR_RO(config);
151 
152 static struct attribute *ap_card_dev_attrs[] = {
153 	&dev_attr_hwtype.attr,
154 	&dev_attr_raw_hwtype.attr,
155 	&dev_attr_depth.attr,
156 	&dev_attr_ap_functions.attr,
157 	&dev_attr_request_count.attr,
158 	&dev_attr_requestq_count.attr,
159 	&dev_attr_pendingq_count.attr,
160 	&dev_attr_modalias.attr,
161 	&dev_attr_config.attr,
162 	NULL
163 };
164 
165 static struct attribute_group ap_card_dev_attr_group = {
166 	.attrs = ap_card_dev_attrs
167 };
168 
169 static const struct attribute_group *ap_card_dev_attr_groups[] = {
170 	&ap_card_dev_attr_group,
171 	NULL
172 };
173 
174 static struct device_type ap_card_type = {
175 	.name = "ap_card",
176 	.groups = ap_card_dev_attr_groups,
177 };
178 
179 static void ap_card_device_release(struct device *dev)
180 {
181 	struct ap_card *ac = to_ap_card(dev);
182 
183 	kfree(ac);
184 }
185 
186 struct ap_card *ap_card_create(int id, int queue_depth, int raw_type,
187 			       int comp_type, unsigned int functions)
188 {
189 	struct ap_card *ac;
190 
191 	ac = kzalloc(sizeof(*ac), GFP_KERNEL);
192 	if (!ac)
193 		return NULL;
194 	ac->ap_dev.device.release = ap_card_device_release;
195 	ac->ap_dev.device.type = &ap_card_type;
196 	ac->ap_dev.device_type = comp_type;
197 	ac->raw_hwtype = raw_type;
198 	ac->queue_depth = queue_depth;
199 	ac->functions = functions;
200 	ac->id = id;
201 	return ac;
202 }
203