1 /* 2 * zcrypt 2.1.0 3 * 4 * Copyright IBM Corp. 2001, 2012 5 * Author(s): Robert Burroughs 6 * Eric Rossman (edrossma@us.ibm.com) 7 * Cornelia Huck <cornelia.huck@de.ibm.com> 8 * 9 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com) 10 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com> 11 * Ralph Wuerthner <rwuerthn@de.ibm.com> 12 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com> 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2, or (at your option) 17 * any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU General Public License for more details. 23 */ 24 25 #include <linux/module.h> 26 #include <linux/init.h> 27 #include <linux/interrupt.h> 28 #include <linux/miscdevice.h> 29 #include <linux/fs.h> 30 #include <linux/proc_fs.h> 31 #include <linux/seq_file.h> 32 #include <linux/compat.h> 33 #include <linux/slab.h> 34 #include <linux/atomic.h> 35 #include <linux/uaccess.h> 36 #include <linux/hw_random.h> 37 #include <linux/debugfs.h> 38 #include <asm/debug.h> 39 40 #include "zcrypt_debug.h" 41 #include "zcrypt_api.h" 42 43 #include "zcrypt_msgtype6.h" 44 #include "zcrypt_msgtype50.h" 45 46 /* 47 * Device attributes common for all crypto card devices. 48 */ 49 50 static ssize_t zcrypt_card_type_show(struct device *dev, 51 struct device_attribute *attr, char *buf) 52 { 53 struct zcrypt_card *zc = to_ap_card(dev)->private; 54 55 return snprintf(buf, PAGE_SIZE, "%s\n", zc->type_string); 56 } 57 58 static DEVICE_ATTR(type, 0444, zcrypt_card_type_show, NULL); 59 60 static ssize_t zcrypt_card_online_show(struct device *dev, 61 struct device_attribute *attr, 62 char *buf) 63 { 64 struct zcrypt_card *zc = to_ap_card(dev)->private; 65 66 return snprintf(buf, PAGE_SIZE, "%d\n", zc->online); 67 } 68 69 static ssize_t zcrypt_card_online_store(struct device *dev, 70 struct device_attribute *attr, 71 const char *buf, size_t count) 72 { 73 struct zcrypt_card *zc = to_ap_card(dev)->private; 74 struct zcrypt_queue *zq; 75 int online, id; 76 77 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1) 78 return -EINVAL; 79 80 zc->online = online; 81 id = zc->card->id; 82 83 ZCRYPT_DBF(DBF_INFO, "card=%02x online=%d\n", id, online); 84 85 spin_lock(&zcrypt_list_lock); 86 list_for_each_entry(zq, &zc->zqueues, list) 87 zcrypt_queue_force_online(zq, online); 88 spin_unlock(&zcrypt_list_lock); 89 return count; 90 } 91 92 static DEVICE_ATTR(online, 0644, zcrypt_card_online_show, 93 zcrypt_card_online_store); 94 95 static struct attribute *zcrypt_card_attrs[] = { 96 &dev_attr_type.attr, 97 &dev_attr_online.attr, 98 NULL, 99 }; 100 101 static struct attribute_group zcrypt_card_attr_group = { 102 .attrs = zcrypt_card_attrs, 103 }; 104 105 struct zcrypt_card *zcrypt_card_alloc(void) 106 { 107 struct zcrypt_card *zc; 108 109 zc = kzalloc(sizeof(struct zcrypt_card), GFP_KERNEL); 110 if (!zc) 111 return NULL; 112 INIT_LIST_HEAD(&zc->list); 113 INIT_LIST_HEAD(&zc->zqueues); 114 kref_init(&zc->refcount); 115 return zc; 116 } 117 EXPORT_SYMBOL(zcrypt_card_alloc); 118 119 void zcrypt_card_free(struct zcrypt_card *zc) 120 { 121 kfree(zc); 122 } 123 EXPORT_SYMBOL(zcrypt_card_free); 124 125 static void zcrypt_card_release(struct kref *kref) 126 { 127 struct zcrypt_card *zdev = 128 container_of(kref, struct zcrypt_card, refcount); 129 zcrypt_card_free(zdev); 130 } 131 132 void zcrypt_card_get(struct zcrypt_card *zc) 133 { 134 kref_get(&zc->refcount); 135 } 136 EXPORT_SYMBOL(zcrypt_card_get); 137 138 int zcrypt_card_put(struct zcrypt_card *zc) 139 { 140 return kref_put(&zc->refcount, zcrypt_card_release); 141 } 142 EXPORT_SYMBOL(zcrypt_card_put); 143 144 /** 145 * zcrypt_card_register() - Register a crypto card device. 146 * @zc: Pointer to a crypto card device 147 * 148 * Register a crypto card device. Returns 0 if successful. 149 */ 150 int zcrypt_card_register(struct zcrypt_card *zc) 151 { 152 int rc; 153 154 rc = sysfs_create_group(&zc->card->ap_dev.device.kobj, 155 &zcrypt_card_attr_group); 156 if (rc) 157 return rc; 158 159 spin_lock(&zcrypt_list_lock); 160 list_add_tail(&zc->list, &zcrypt_card_list); 161 spin_unlock(&zcrypt_list_lock); 162 163 zc->online = 1; 164 165 ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id); 166 167 return rc; 168 } 169 EXPORT_SYMBOL(zcrypt_card_register); 170 171 /** 172 * zcrypt_card_unregister(): Unregister a crypto card device. 173 * @zc: Pointer to crypto card device 174 * 175 * Unregister a crypto card device. 176 */ 177 void zcrypt_card_unregister(struct zcrypt_card *zc) 178 { 179 ZCRYPT_DBF(DBF_INFO, "card=%02x unregister\n", zc->card->id); 180 181 spin_lock(&zcrypt_list_lock); 182 list_del_init(&zc->list); 183 spin_unlock(&zcrypt_list_lock); 184 sysfs_remove_group(&zc->card->ap_dev.device.kobj, 185 &zcrypt_card_attr_group); 186 } 187 EXPORT_SYMBOL(zcrypt_card_unregister); 188