0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/miscdevice.h>
0018 #include <linux/fs.h>
0019 #include <linux/proc_fs.h>
0020 #include <linux/seq_file.h>
0021 #include <linux/compat.h>
0022 #include <linux/slab.h>
0023 #include <linux/atomic.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/hw_random.h>
0026 #include <linux/debugfs.h>
0027 #include <asm/debug.h>
0028
0029 #include "zcrypt_debug.h"
0030 #include "zcrypt_api.h"
0031
0032 #include "zcrypt_msgtype6.h"
0033 #include "zcrypt_msgtype50.h"
0034
0035
0036
0037
0038
0039 static ssize_t type_show(struct device *dev,
0040 struct device_attribute *attr, char *buf)
0041 {
0042 struct zcrypt_card *zc = dev_get_drvdata(dev);
0043
0044 return scnprintf(buf, PAGE_SIZE, "%s\n", zc->type_string);
0045 }
0046
0047 static DEVICE_ATTR_RO(type);
0048
0049 static ssize_t online_show(struct device *dev,
0050 struct device_attribute *attr,
0051 char *buf)
0052 {
0053 struct zcrypt_card *zc = dev_get_drvdata(dev);
0054 struct ap_card *ac = to_ap_card(dev);
0055 int online = ac->config && zc->online ? 1 : 0;
0056
0057 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
0058 }
0059
0060 static ssize_t online_store(struct device *dev,
0061 struct device_attribute *attr,
0062 const char *buf, size_t count)
0063 {
0064 struct zcrypt_card *zc = dev_get_drvdata(dev);
0065 struct ap_card *ac = to_ap_card(dev);
0066 struct zcrypt_queue *zq;
0067 int online, id, i = 0, maxzqs = 0;
0068 struct zcrypt_queue **zq_uelist = NULL;
0069
0070 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
0071 return -EINVAL;
0072
0073 if (online && !ac->config)
0074 return -ENODEV;
0075
0076 zc->online = online;
0077 id = zc->card->id;
0078
0079 ZCRYPT_DBF_INFO("%s card=%02x online=%d\n", __func__, id, online);
0080
0081 ap_send_online_uevent(&ac->ap_dev, online);
0082
0083 spin_lock(&zcrypt_list_lock);
0084
0085
0086
0087
0088
0089
0090 list_for_each_entry(zq, &zc->zqueues, list)
0091 maxzqs++;
0092 if (maxzqs > 0)
0093 zq_uelist = kcalloc(maxzqs + 1, sizeof(*zq_uelist), GFP_ATOMIC);
0094 list_for_each_entry(zq, &zc->zqueues, list)
0095 if (zcrypt_queue_force_online(zq, online))
0096 if (zq_uelist) {
0097 zcrypt_queue_get(zq);
0098 zq_uelist[i++] = zq;
0099 }
0100 spin_unlock(&zcrypt_list_lock);
0101 if (zq_uelist) {
0102 for (i = 0; zq_uelist[i]; i++) {
0103 zq = zq_uelist[i];
0104 ap_send_online_uevent(&zq->queue->ap_dev, online);
0105 zcrypt_queue_put(zq);
0106 }
0107 kfree(zq_uelist);
0108 }
0109
0110 return count;
0111 }
0112
0113 static DEVICE_ATTR_RW(online);
0114
0115 static ssize_t load_show(struct device *dev,
0116 struct device_attribute *attr,
0117 char *buf)
0118 {
0119 struct zcrypt_card *zc = dev_get_drvdata(dev);
0120
0121 return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
0122 }
0123
0124 static DEVICE_ATTR_RO(load);
0125
0126 static struct attribute *zcrypt_card_attrs[] = {
0127 &dev_attr_type.attr,
0128 &dev_attr_online.attr,
0129 &dev_attr_load.attr,
0130 NULL,
0131 };
0132
0133 static const struct attribute_group zcrypt_card_attr_group = {
0134 .attrs = zcrypt_card_attrs,
0135 };
0136
0137 struct zcrypt_card *zcrypt_card_alloc(void)
0138 {
0139 struct zcrypt_card *zc;
0140
0141 zc = kzalloc(sizeof(*zc), GFP_KERNEL);
0142 if (!zc)
0143 return NULL;
0144 INIT_LIST_HEAD(&zc->list);
0145 INIT_LIST_HEAD(&zc->zqueues);
0146 kref_init(&zc->refcount);
0147 return zc;
0148 }
0149 EXPORT_SYMBOL(zcrypt_card_alloc);
0150
0151 void zcrypt_card_free(struct zcrypt_card *zc)
0152 {
0153 kfree(zc);
0154 }
0155 EXPORT_SYMBOL(zcrypt_card_free);
0156
0157 static void zcrypt_card_release(struct kref *kref)
0158 {
0159 struct zcrypt_card *zdev =
0160 container_of(kref, struct zcrypt_card, refcount);
0161 zcrypt_card_free(zdev);
0162 }
0163
0164 void zcrypt_card_get(struct zcrypt_card *zc)
0165 {
0166 kref_get(&zc->refcount);
0167 }
0168 EXPORT_SYMBOL(zcrypt_card_get);
0169
0170 int zcrypt_card_put(struct zcrypt_card *zc)
0171 {
0172 return kref_put(&zc->refcount, zcrypt_card_release);
0173 }
0174 EXPORT_SYMBOL(zcrypt_card_put);
0175
0176
0177
0178
0179
0180
0181
0182 int zcrypt_card_register(struct zcrypt_card *zc)
0183 {
0184 int rc;
0185
0186 spin_lock(&zcrypt_list_lock);
0187 list_add_tail(&zc->list, &zcrypt_card_list);
0188 spin_unlock(&zcrypt_list_lock);
0189
0190 zc->online = 1;
0191
0192 ZCRYPT_DBF_INFO("%s card=%02x register online=1\n",
0193 __func__, zc->card->id);
0194
0195 rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
0196 &zcrypt_card_attr_group);
0197 if (rc) {
0198 spin_lock(&zcrypt_list_lock);
0199 list_del_init(&zc->list);
0200 spin_unlock(&zcrypt_list_lock);
0201 }
0202
0203 return rc;
0204 }
0205 EXPORT_SYMBOL(zcrypt_card_register);
0206
0207
0208
0209
0210
0211
0212
0213 void zcrypt_card_unregister(struct zcrypt_card *zc)
0214 {
0215 ZCRYPT_DBF_INFO("%s card=%02x unregister\n",
0216 __func__, zc->card->id);
0217
0218 spin_lock(&zcrypt_list_lock);
0219 list_del_init(&zc->list);
0220 spin_unlock(&zcrypt_list_lock);
0221 sysfs_remove_group(&zc->card->ap_dev.device.kobj,
0222 &zcrypt_card_attr_group);
0223 zcrypt_card_put(zc);
0224 }
0225 EXPORT_SYMBOL(zcrypt_card_unregister);