Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 15 | #include <asm/sclp.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 16 | |
| 17 | #include "ap_bus.h" |
| 18 | |
| 19 | /* |
| 20 | * AP card related attributes. |
| 21 | */ |
| 22 | static ssize_t hwtype_show(struct device *dev, |
| 23 | struct device_attribute *attr, char *buf) |
| 24 | { |
| 25 | struct ap_card *ac = to_ap_card(dev); |
| 26 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 27 | return scnprintf(buf, PAGE_SIZE, "%d\n", ac->ap_dev.device_type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static DEVICE_ATTR_RO(hwtype); |
| 31 | |
| 32 | static ssize_t raw_hwtype_show(struct device *dev, |
| 33 | struct device_attribute *attr, char *buf) |
| 34 | { |
| 35 | struct ap_card *ac = to_ap_card(dev); |
| 36 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 37 | return scnprintf(buf, PAGE_SIZE, "%d\n", ac->raw_hwtype); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static DEVICE_ATTR_RO(raw_hwtype); |
| 41 | |
| 42 | static ssize_t depth_show(struct device *dev, struct device_attribute *attr, |
| 43 | char *buf) |
| 44 | { |
| 45 | struct ap_card *ac = to_ap_card(dev); |
| 46 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 47 | return scnprintf(buf, PAGE_SIZE, "%d\n", ac->queue_depth); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static DEVICE_ATTR_RO(depth); |
| 51 | |
| 52 | static ssize_t ap_functions_show(struct device *dev, |
| 53 | struct device_attribute *attr, char *buf) |
| 54 | { |
| 55 | struct ap_card *ac = to_ap_card(dev); |
| 56 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 57 | return scnprintf(buf, PAGE_SIZE, "0x%08X\n", ac->functions); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static DEVICE_ATTR_RO(ap_functions); |
| 61 | |
| 62 | static ssize_t request_count_show(struct device *dev, |
| 63 | struct device_attribute *attr, |
| 64 | char *buf) |
| 65 | { |
| 66 | struct ap_card *ac = to_ap_card(dev); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 67 | u64 req_cnt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 68 | |
| 69 | req_cnt = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 70 | spin_lock_bh(&ap_queues_lock); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 71 | req_cnt = atomic64_read(&ac->total_request_count); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 72 | spin_unlock_bh(&ap_queues_lock); |
| 73 | return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static ssize_t request_count_store(struct device *dev, |
| 77 | struct device_attribute *attr, |
| 78 | const char *buf, size_t count) |
| 79 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 80 | int bkt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | struct ap_queue *aq; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 82 | struct ap_card *ac = to_ap_card(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 83 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 84 | spin_lock_bh(&ap_queues_lock); |
| 85 | hash_for_each(ap_queues, bkt, aq, hnode) |
| 86 | if (ac == aq->card) |
| 87 | aq->total_request_count = 0; |
| 88 | spin_unlock_bh(&ap_queues_lock); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 89 | atomic64_set(&ac->total_request_count, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 90 | |
| 91 | return count; |
| 92 | } |
| 93 | |
| 94 | static DEVICE_ATTR_RW(request_count); |
| 95 | |
| 96 | static ssize_t requestq_count_show(struct device *dev, |
| 97 | struct device_attribute *attr, char *buf) |
| 98 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 99 | int bkt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 100 | struct ap_queue *aq; |
| 101 | unsigned int reqq_cnt; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 102 | struct ap_card *ac = to_ap_card(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 103 | |
| 104 | reqq_cnt = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 105 | spin_lock_bh(&ap_queues_lock); |
| 106 | hash_for_each(ap_queues, bkt, aq, hnode) |
| 107 | if (ac == aq->card) |
| 108 | reqq_cnt += aq->requestq_count; |
| 109 | spin_unlock_bh(&ap_queues_lock); |
| 110 | return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static DEVICE_ATTR_RO(requestq_count); |
| 114 | |
| 115 | static ssize_t pendingq_count_show(struct device *dev, |
| 116 | struct device_attribute *attr, char *buf) |
| 117 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 118 | int bkt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 119 | struct ap_queue *aq; |
| 120 | unsigned int penq_cnt; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 121 | struct ap_card *ac = to_ap_card(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 122 | |
| 123 | penq_cnt = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 124 | spin_lock_bh(&ap_queues_lock); |
| 125 | hash_for_each(ap_queues, bkt, aq, hnode) |
| 126 | if (ac == aq->card) |
| 127 | penq_cnt += aq->pendingq_count; |
| 128 | spin_unlock_bh(&ap_queues_lock); |
| 129 | return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static DEVICE_ATTR_RO(pendingq_count); |
| 133 | |
| 134 | static ssize_t modalias_show(struct device *dev, |
| 135 | struct device_attribute *attr, char *buf) |
| 136 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 137 | return scnprintf(buf, PAGE_SIZE, "ap:t%02X\n", |
| 138 | to_ap_dev(dev)->device_type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static DEVICE_ATTR_RO(modalias); |
| 142 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 143 | static ssize_t config_show(struct device *dev, |
| 144 | struct device_attribute *attr, char *buf) |
| 145 | { |
| 146 | struct ap_card *ac = to_ap_card(dev); |
| 147 | |
| 148 | return scnprintf(buf, PAGE_SIZE, "%d\n", ac->config ? 1 : 0); |
| 149 | } |
| 150 | |
| 151 | static ssize_t config_store(struct device *dev, |
| 152 | struct device_attribute *attr, |
| 153 | const char *buf, size_t count) |
| 154 | { |
| 155 | int rc = 0, cfg; |
| 156 | struct ap_card *ac = to_ap_card(dev); |
| 157 | |
| 158 | if (sscanf(buf, "%d\n", &cfg) != 1 || cfg < 0 || cfg > 1) |
| 159 | return -EINVAL; |
| 160 | |
| 161 | if (cfg && !ac->config) |
| 162 | rc = sclp_ap_configure(ac->id); |
| 163 | else if (!cfg && ac->config) |
| 164 | rc = sclp_ap_deconfigure(ac->id); |
| 165 | if (rc) |
| 166 | return rc; |
| 167 | |
| 168 | ac->config = cfg ? true : false; |
| 169 | |
| 170 | return count; |
| 171 | } |
| 172 | |
| 173 | static DEVICE_ATTR_RW(config); |
| 174 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 175 | static struct attribute *ap_card_dev_attrs[] = { |
| 176 | &dev_attr_hwtype.attr, |
| 177 | &dev_attr_raw_hwtype.attr, |
| 178 | &dev_attr_depth.attr, |
| 179 | &dev_attr_ap_functions.attr, |
| 180 | &dev_attr_request_count.attr, |
| 181 | &dev_attr_requestq_count.attr, |
| 182 | &dev_attr_pendingq_count.attr, |
| 183 | &dev_attr_modalias.attr, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 184 | &dev_attr_config.attr, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 185 | NULL |
| 186 | }; |
| 187 | |
| 188 | static struct attribute_group ap_card_dev_attr_group = { |
| 189 | .attrs = ap_card_dev_attrs |
| 190 | }; |
| 191 | |
| 192 | static const struct attribute_group *ap_card_dev_attr_groups[] = { |
| 193 | &ap_card_dev_attr_group, |
| 194 | NULL |
| 195 | }; |
| 196 | |
| 197 | static struct device_type ap_card_type = { |
| 198 | .name = "ap_card", |
| 199 | .groups = ap_card_dev_attr_groups, |
| 200 | }; |
| 201 | |
| 202 | static void ap_card_device_release(struct device *dev) |
| 203 | { |
| 204 | struct ap_card *ac = to_ap_card(dev); |
| 205 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 206 | kfree(ac); |
| 207 | } |
| 208 | |
| 209 | struct ap_card *ap_card_create(int id, int queue_depth, int raw_type, |
| 210 | int comp_type, unsigned int functions) |
| 211 | { |
| 212 | struct ap_card *ac; |
| 213 | |
| 214 | ac = kzalloc(sizeof(*ac), GFP_KERNEL); |
| 215 | if (!ac) |
| 216 | return NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 217 | ac->ap_dev.device.release = ap_card_device_release; |
| 218 | ac->ap_dev.device.type = &ap_card_type; |
| 219 | ac->ap_dev.device_type = comp_type; |
| 220 | ac->raw_hwtype = raw_type; |
| 221 | ac->queue_depth = queue_depth; |
| 222 | ac->functions = functions; |
| 223 | ac->id = id; |
| 224 | return ac; |
| 225 | } |