zcrypt_api.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/fs.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/compat.h>
  36. #include <linux/slab.h>
  37. #include <linux/atomic.h>
  38. #include <asm/uaccess.h>
  39. #include <linux/hw_random.h>
  40. #include <linux/debugfs.h>
  41. #include <asm/debug.h>
  42. #include "zcrypt_debug.h"
  43. #include "zcrypt_api.h"
  44. #include "zcrypt_msgtype6.h"
  45. /*
  46. * Module description.
  47. */
  48. MODULE_AUTHOR("IBM Corporation");
  49. MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
  50. "Copyright IBM Corp. 2001, 2012");
  51. MODULE_LICENSE("GPL");
  52. static DEFINE_SPINLOCK(zcrypt_device_lock);
  53. static LIST_HEAD(zcrypt_device_list);
  54. static int zcrypt_device_count = 0;
  55. static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  56. static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
  57. atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
  58. EXPORT_SYMBOL(zcrypt_rescan_req);
  59. static int zcrypt_rng_device_add(void);
  60. static void zcrypt_rng_device_remove(void);
  61. static DEFINE_SPINLOCK(zcrypt_ops_list_lock);
  62. static LIST_HEAD(zcrypt_ops_list);
  63. static debug_info_t *zcrypt_dbf_common;
  64. static debug_info_t *zcrypt_dbf_devices;
  65. static struct dentry *debugfs_root;
  66. /*
  67. * Device attributes common for all crypto devices.
  68. */
  69. static ssize_t zcrypt_type_show(struct device *dev,
  70. struct device_attribute *attr, char *buf)
  71. {
  72. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  73. return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
  74. }
  75. static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
  76. static ssize_t zcrypt_online_show(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  80. return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
  81. }
  82. static ssize_t zcrypt_online_store(struct device *dev,
  83. struct device_attribute *attr,
  84. const char *buf, size_t count)
  85. {
  86. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  87. int online;
  88. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  89. return -EINVAL;
  90. zdev->online = online;
  91. ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dman", zdev->ap_dev->qid,
  92. zdev->online);
  93. if (!online)
  94. ap_flush_queue(zdev->ap_dev);
  95. return count;
  96. }
  97. static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
  98. static struct attribute * zcrypt_device_attrs[] = {
  99. &dev_attr_type.attr,
  100. &dev_attr_online.attr,
  101. NULL,
  102. };
  103. static struct attribute_group zcrypt_device_attr_group = {
  104. .attrs = zcrypt_device_attrs,
  105. };
  106. /**
  107. * Process a rescan of the transport layer.
  108. *
  109. * Returns 1, if the rescan has been processed, otherwise 0.
  110. */
  111. static inline int zcrypt_process_rescan(void)
  112. {
  113. if (atomic_read(&zcrypt_rescan_req)) {
  114. atomic_set(&zcrypt_rescan_req, 0);
  115. atomic_inc(&zcrypt_rescan_count);
  116. ap_bus_force_rescan();
  117. ZCRYPT_DBF_COMMON(DBF_INFO, "rescan%07d",
  118. atomic_inc_return(&zcrypt_rescan_count));
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. /**
  124. * __zcrypt_increase_preference(): Increase preference of a crypto device.
  125. * @zdev: Pointer the crypto device
  126. *
  127. * Move the device towards the head of the device list.
  128. * Need to be called while holding the zcrypt device list lock.
  129. * Note: cards with speed_rating of 0 are kept at the end of the list.
  130. */
  131. static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
  132. {
  133. struct zcrypt_device *tmp;
  134. struct list_head *l;
  135. if (zdev->speed_rating == 0)
  136. return;
  137. for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
  138. tmp = list_entry(l, struct zcrypt_device, list);
  139. if ((tmp->request_count + 1) * tmp->speed_rating <=
  140. (zdev->request_count + 1) * zdev->speed_rating &&
  141. tmp->speed_rating != 0)
  142. break;
  143. }
  144. if (l == zdev->list.prev)
  145. return;
  146. /* Move zdev behind l */
  147. list_move(&zdev->list, l);
  148. }
  149. /**
  150. * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
  151. * @zdev: Pointer to a crypto device.
  152. *
  153. * Move the device towards the tail of the device list.
  154. * Need to be called while holding the zcrypt device list lock.
  155. * Note: cards with speed_rating of 0 are kept at the end of the list.
  156. */
  157. static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
  158. {
  159. struct zcrypt_device *tmp;
  160. struct list_head *l;
  161. if (zdev->speed_rating == 0)
  162. return;
  163. for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
  164. tmp = list_entry(l, struct zcrypt_device, list);
  165. if ((tmp->request_count + 1) * tmp->speed_rating >
  166. (zdev->request_count + 1) * zdev->speed_rating ||
  167. tmp->speed_rating == 0)
  168. break;
  169. }
  170. if (l == zdev->list.next)
  171. return;
  172. /* Move zdev before l */
  173. list_move_tail(&zdev->list, l);
  174. }
  175. static void zcrypt_device_release(struct kref *kref)
  176. {
  177. struct zcrypt_device *zdev =
  178. container_of(kref, struct zcrypt_device, refcount);
  179. zcrypt_device_free(zdev);
  180. }
  181. void zcrypt_device_get(struct zcrypt_device *zdev)
  182. {
  183. kref_get(&zdev->refcount);
  184. }
  185. EXPORT_SYMBOL(zcrypt_device_get);
  186. int zcrypt_device_put(struct zcrypt_device *zdev)
  187. {
  188. return kref_put(&zdev->refcount, zcrypt_device_release);
  189. }
  190. EXPORT_SYMBOL(zcrypt_device_put);
  191. struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
  192. {
  193. struct zcrypt_device *zdev;
  194. zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
  195. if (!zdev)
  196. return NULL;
  197. zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
  198. if (!zdev->reply.message)
  199. goto out_free;
  200. zdev->reply.length = max_response_size;
  201. spin_lock_init(&zdev->lock);
  202. INIT_LIST_HEAD(&zdev->list);
  203. zdev->dbf_area = zcrypt_dbf_devices;
  204. return zdev;
  205. out_free:
  206. kfree(zdev);
  207. return NULL;
  208. }
  209. EXPORT_SYMBOL(zcrypt_device_alloc);
  210. void zcrypt_device_free(struct zcrypt_device *zdev)
  211. {
  212. kfree(zdev->reply.message);
  213. kfree(zdev);
  214. }
  215. EXPORT_SYMBOL(zcrypt_device_free);
  216. /**
  217. * zcrypt_device_register() - Register a crypto device.
  218. * @zdev: Pointer to a crypto device
  219. *
  220. * Register a crypto device. Returns 0 if successful.
  221. */
  222. int zcrypt_device_register(struct zcrypt_device *zdev)
  223. {
  224. int rc;
  225. if (!zdev->ops)
  226. return -ENODEV;
  227. rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
  228. &zcrypt_device_attr_group);
  229. if (rc)
  230. goto out;
  231. get_device(&zdev->ap_dev->device);
  232. kref_init(&zdev->refcount);
  233. spin_lock_bh(&zcrypt_device_lock);
  234. zdev->online = 1; /* New devices are online by default. */
  235. ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dreg", zdev->ap_dev->qid,
  236. zdev->online);
  237. list_add_tail(&zdev->list, &zcrypt_device_list);
  238. __zcrypt_increase_preference(zdev);
  239. zcrypt_device_count++;
  240. spin_unlock_bh(&zcrypt_device_lock);
  241. if (zdev->ops->rng) {
  242. rc = zcrypt_rng_device_add();
  243. if (rc)
  244. goto out_unregister;
  245. }
  246. return 0;
  247. out_unregister:
  248. spin_lock_bh(&zcrypt_device_lock);
  249. zcrypt_device_count--;
  250. list_del_init(&zdev->list);
  251. spin_unlock_bh(&zcrypt_device_lock);
  252. sysfs_remove_group(&zdev->ap_dev->device.kobj,
  253. &zcrypt_device_attr_group);
  254. put_device(&zdev->ap_dev->device);
  255. zcrypt_device_put(zdev);
  256. out:
  257. return rc;
  258. }
  259. EXPORT_SYMBOL(zcrypt_device_register);
  260. /**
  261. * zcrypt_device_unregister(): Unregister a crypto device.
  262. * @zdev: Pointer to crypto device
  263. *
  264. * Unregister a crypto device.
  265. */
  266. void zcrypt_device_unregister(struct zcrypt_device *zdev)
  267. {
  268. if (zdev->ops->rng)
  269. zcrypt_rng_device_remove();
  270. spin_lock_bh(&zcrypt_device_lock);
  271. zcrypt_device_count--;
  272. list_del_init(&zdev->list);
  273. spin_unlock_bh(&zcrypt_device_lock);
  274. sysfs_remove_group(&zdev->ap_dev->device.kobj,
  275. &zcrypt_device_attr_group);
  276. put_device(&zdev->ap_dev->device);
  277. zcrypt_device_put(zdev);
  278. }
  279. EXPORT_SYMBOL(zcrypt_device_unregister);
  280. void zcrypt_msgtype_register(struct zcrypt_ops *zops)
  281. {
  282. if (zops->owner) {
  283. spin_lock_bh(&zcrypt_ops_list_lock);
  284. list_add_tail(&zops->list, &zcrypt_ops_list);
  285. spin_unlock_bh(&zcrypt_ops_list_lock);
  286. }
  287. }
  288. EXPORT_SYMBOL(zcrypt_msgtype_register);
  289. void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
  290. {
  291. spin_lock_bh(&zcrypt_ops_list_lock);
  292. list_del_init(&zops->list);
  293. spin_unlock_bh(&zcrypt_ops_list_lock);
  294. }
  295. EXPORT_SYMBOL(zcrypt_msgtype_unregister);
  296. static inline
  297. struct zcrypt_ops *__ops_lookup(unsigned char *name, int variant)
  298. {
  299. struct zcrypt_ops *zops;
  300. int found = 0;
  301. spin_lock_bh(&zcrypt_ops_list_lock);
  302. list_for_each_entry(zops, &zcrypt_ops_list, list) {
  303. if ((zops->variant == variant) &&
  304. (!strncmp(zops->owner->name, name, MODULE_NAME_LEN))) {
  305. found = 1;
  306. break;
  307. }
  308. }
  309. spin_unlock_bh(&zcrypt_ops_list_lock);
  310. if (!found)
  311. return NULL;
  312. return zops;
  313. }
  314. struct zcrypt_ops *zcrypt_msgtype_request(unsigned char *name, int variant)
  315. {
  316. struct zcrypt_ops *zops = NULL;
  317. zops = __ops_lookup(name, variant);
  318. if (!zops) {
  319. request_module("%s", name);
  320. zops = __ops_lookup(name, variant);
  321. }
  322. if ((!zops) || (!try_module_get(zops->owner)))
  323. return NULL;
  324. return zops;
  325. }
  326. EXPORT_SYMBOL(zcrypt_msgtype_request);
  327. void zcrypt_msgtype_release(struct zcrypt_ops *zops)
  328. {
  329. if (zops)
  330. module_put(zops->owner);
  331. }
  332. EXPORT_SYMBOL(zcrypt_msgtype_release);
  333. /**
  334. * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
  335. *
  336. * This function is not supported beyond zcrypt 1.3.1.
  337. */
  338. static ssize_t zcrypt_read(struct file *filp, char __user *buf,
  339. size_t count, loff_t *f_pos)
  340. {
  341. return -EPERM;
  342. }
  343. /**
  344. * zcrypt_write(): Not allowed.
  345. *
  346. * Write is is not allowed
  347. */
  348. static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
  349. size_t count, loff_t *f_pos)
  350. {
  351. return -EPERM;
  352. }
  353. /**
  354. * zcrypt_open(): Count number of users.
  355. *
  356. * Device open function to count number of users.
  357. */
  358. static int zcrypt_open(struct inode *inode, struct file *filp)
  359. {
  360. atomic_inc(&zcrypt_open_count);
  361. return nonseekable_open(inode, filp);
  362. }
  363. /**
  364. * zcrypt_release(): Count number of users.
  365. *
  366. * Device close function to count number of users.
  367. */
  368. static int zcrypt_release(struct inode *inode, struct file *filp)
  369. {
  370. atomic_dec(&zcrypt_open_count);
  371. return 0;
  372. }
  373. /*
  374. * zcrypt ioctls.
  375. */
  376. static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
  377. {
  378. struct zcrypt_device *zdev;
  379. int rc;
  380. if (mex->outputdatalength < mex->inputdatalength)
  381. return -EINVAL;
  382. /*
  383. * As long as outputdatalength is big enough, we can set the
  384. * outputdatalength equal to the inputdatalength, since that is the
  385. * number of bytes we will copy in any case
  386. */
  387. mex->outputdatalength = mex->inputdatalength;
  388. spin_lock_bh(&zcrypt_device_lock);
  389. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  390. if (!zdev->online ||
  391. !zdev->ops->rsa_modexpo ||
  392. zdev->min_mod_size > mex->inputdatalength ||
  393. zdev->max_mod_size < mex->inputdatalength)
  394. continue;
  395. zcrypt_device_get(zdev);
  396. get_device(&zdev->ap_dev->device);
  397. zdev->request_count++;
  398. __zcrypt_decrease_preference(zdev);
  399. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  400. spin_unlock_bh(&zcrypt_device_lock);
  401. rc = zdev->ops->rsa_modexpo(zdev, mex);
  402. spin_lock_bh(&zcrypt_device_lock);
  403. module_put(zdev->ap_dev->drv->driver.owner);
  404. }
  405. else
  406. rc = -EAGAIN;
  407. zdev->request_count--;
  408. __zcrypt_increase_preference(zdev);
  409. put_device(&zdev->ap_dev->device);
  410. zcrypt_device_put(zdev);
  411. spin_unlock_bh(&zcrypt_device_lock);
  412. return rc;
  413. }
  414. spin_unlock_bh(&zcrypt_device_lock);
  415. return -ENODEV;
  416. }
  417. static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
  418. {
  419. struct zcrypt_device *zdev;
  420. unsigned long long z1, z2, z3;
  421. int rc, copied;
  422. if (crt->outputdatalength < crt->inputdatalength ||
  423. (crt->inputdatalength & 1))
  424. return -EINVAL;
  425. /*
  426. * As long as outputdatalength is big enough, we can set the
  427. * outputdatalength equal to the inputdatalength, since that is the
  428. * number of bytes we will copy in any case
  429. */
  430. crt->outputdatalength = crt->inputdatalength;
  431. copied = 0;
  432. restart:
  433. spin_lock_bh(&zcrypt_device_lock);
  434. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  435. if (!zdev->online ||
  436. !zdev->ops->rsa_modexpo_crt ||
  437. zdev->min_mod_size > crt->inputdatalength ||
  438. zdev->max_mod_size < crt->inputdatalength)
  439. continue;
  440. if (zdev->short_crt && crt->inputdatalength > 240) {
  441. /*
  442. * Check inputdata for leading zeros for cards
  443. * that can't handle np_prime, bp_key, or
  444. * u_mult_inv > 128 bytes.
  445. */
  446. if (copied == 0) {
  447. unsigned int len;
  448. spin_unlock_bh(&zcrypt_device_lock);
  449. /* len is max 256 / 2 - 120 = 8
  450. * For bigger device just assume len of leading
  451. * 0s is 8 as stated in the requirements for
  452. * ica_rsa_modexpo_crt struct in zcrypt.h.
  453. */
  454. if (crt->inputdatalength <= 256)
  455. len = crt->inputdatalength / 2 - 120;
  456. else
  457. len = 8;
  458. if (len > sizeof(z1))
  459. return -EFAULT;
  460. z1 = z2 = z3 = 0;
  461. if (copy_from_user(&z1, crt->np_prime, len) ||
  462. copy_from_user(&z2, crt->bp_key, len) ||
  463. copy_from_user(&z3, crt->u_mult_inv, len))
  464. return -EFAULT;
  465. z1 = z2 = z3 = 0;
  466. copied = 1;
  467. /*
  468. * We have to restart device lookup -
  469. * the device list may have changed by now.
  470. */
  471. goto restart;
  472. }
  473. if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
  474. /* The device can't handle this request. */
  475. continue;
  476. }
  477. zcrypt_device_get(zdev);
  478. get_device(&zdev->ap_dev->device);
  479. zdev->request_count++;
  480. __zcrypt_decrease_preference(zdev);
  481. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  482. spin_unlock_bh(&zcrypt_device_lock);
  483. rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
  484. spin_lock_bh(&zcrypt_device_lock);
  485. module_put(zdev->ap_dev->drv->driver.owner);
  486. }
  487. else
  488. rc = -EAGAIN;
  489. zdev->request_count--;
  490. __zcrypt_increase_preference(zdev);
  491. put_device(&zdev->ap_dev->device);
  492. zcrypt_device_put(zdev);
  493. spin_unlock_bh(&zcrypt_device_lock);
  494. return rc;
  495. }
  496. spin_unlock_bh(&zcrypt_device_lock);
  497. return -ENODEV;
  498. }
  499. static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  500. {
  501. struct zcrypt_device *zdev;
  502. int rc;
  503. spin_lock_bh(&zcrypt_device_lock);
  504. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  505. if (!zdev->online || !zdev->ops->send_cprb ||
  506. (zdev->ops->variant == MSGTYPE06_VARIANT_EP11) ||
  507. (xcRB->user_defined != AUTOSELECT &&
  508. AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined))
  509. continue;
  510. zcrypt_device_get(zdev);
  511. get_device(&zdev->ap_dev->device);
  512. zdev->request_count++;
  513. __zcrypt_decrease_preference(zdev);
  514. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  515. spin_unlock_bh(&zcrypt_device_lock);
  516. rc = zdev->ops->send_cprb(zdev, xcRB);
  517. spin_lock_bh(&zcrypt_device_lock);
  518. module_put(zdev->ap_dev->drv->driver.owner);
  519. }
  520. else
  521. rc = -EAGAIN;
  522. zdev->request_count--;
  523. __zcrypt_increase_preference(zdev);
  524. put_device(&zdev->ap_dev->device);
  525. zcrypt_device_put(zdev);
  526. spin_unlock_bh(&zcrypt_device_lock);
  527. return rc;
  528. }
  529. spin_unlock_bh(&zcrypt_device_lock);
  530. return -ENODEV;
  531. }
  532. struct ep11_target_dev_list {
  533. unsigned short targets_num;
  534. struct ep11_target_dev *targets;
  535. };
  536. static bool is_desired_ep11dev(unsigned int dev_qid,
  537. struct ep11_target_dev_list dev_list)
  538. {
  539. int n;
  540. for (n = 0; n < dev_list.targets_num; n++, dev_list.targets++) {
  541. if ((AP_QID_DEVICE(dev_qid) == dev_list.targets->ap_id) &&
  542. (AP_QID_QUEUE(dev_qid) == dev_list.targets->dom_id)) {
  543. return true;
  544. }
  545. }
  546. return false;
  547. }
  548. static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
  549. {
  550. struct zcrypt_device *zdev;
  551. bool autoselect = false;
  552. int rc;
  553. struct ep11_target_dev_list ep11_dev_list = {
  554. .targets_num = 0x00,
  555. .targets = NULL,
  556. };
  557. ep11_dev_list.targets_num = (unsigned short) xcrb->targets_num;
  558. /* empty list indicates autoselect (all available targets) */
  559. if (ep11_dev_list.targets_num == 0)
  560. autoselect = true;
  561. else {
  562. ep11_dev_list.targets = kcalloc((unsigned short)
  563. xcrb->targets_num,
  564. sizeof(struct ep11_target_dev),
  565. GFP_KERNEL);
  566. if (!ep11_dev_list.targets)
  567. return -ENOMEM;
  568. if (copy_from_user(ep11_dev_list.targets,
  569. (struct ep11_target_dev __force __user *)
  570. xcrb->targets, xcrb->targets_num *
  571. sizeof(struct ep11_target_dev)))
  572. return -EFAULT;
  573. }
  574. spin_lock_bh(&zcrypt_device_lock);
  575. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  576. /* check if device is eligible */
  577. if (!zdev->online ||
  578. zdev->ops->variant != MSGTYPE06_VARIANT_EP11)
  579. continue;
  580. /* check if device is selected as valid target */
  581. if (!is_desired_ep11dev(zdev->ap_dev->qid, ep11_dev_list) &&
  582. !autoselect)
  583. continue;
  584. zcrypt_device_get(zdev);
  585. get_device(&zdev->ap_dev->device);
  586. zdev->request_count++;
  587. __zcrypt_decrease_preference(zdev);
  588. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  589. spin_unlock_bh(&zcrypt_device_lock);
  590. rc = zdev->ops->send_ep11_cprb(zdev, xcrb);
  591. spin_lock_bh(&zcrypt_device_lock);
  592. module_put(zdev->ap_dev->drv->driver.owner);
  593. } else {
  594. rc = -EAGAIN;
  595. }
  596. zdev->request_count--;
  597. __zcrypt_increase_preference(zdev);
  598. put_device(&zdev->ap_dev->device);
  599. zcrypt_device_put(zdev);
  600. spin_unlock_bh(&zcrypt_device_lock);
  601. return rc;
  602. }
  603. spin_unlock_bh(&zcrypt_device_lock);
  604. return -ENODEV;
  605. }
  606. static long zcrypt_rng(char *buffer)
  607. {
  608. struct zcrypt_device *zdev;
  609. int rc;
  610. spin_lock_bh(&zcrypt_device_lock);
  611. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  612. if (!zdev->online || !zdev->ops->rng)
  613. continue;
  614. zcrypt_device_get(zdev);
  615. get_device(&zdev->ap_dev->device);
  616. zdev->request_count++;
  617. __zcrypt_decrease_preference(zdev);
  618. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  619. spin_unlock_bh(&zcrypt_device_lock);
  620. rc = zdev->ops->rng(zdev, buffer);
  621. spin_lock_bh(&zcrypt_device_lock);
  622. module_put(zdev->ap_dev->drv->driver.owner);
  623. } else
  624. rc = -EAGAIN;
  625. zdev->request_count--;
  626. __zcrypt_increase_preference(zdev);
  627. put_device(&zdev->ap_dev->device);
  628. zcrypt_device_put(zdev);
  629. spin_unlock_bh(&zcrypt_device_lock);
  630. return rc;
  631. }
  632. spin_unlock_bh(&zcrypt_device_lock);
  633. return -ENODEV;
  634. }
  635. static void zcrypt_status_mask(char status[AP_DEVICES])
  636. {
  637. struct zcrypt_device *zdev;
  638. memset(status, 0, sizeof(char) * AP_DEVICES);
  639. spin_lock_bh(&zcrypt_device_lock);
  640. list_for_each_entry(zdev, &zcrypt_device_list, list)
  641. status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  642. zdev->online ? zdev->user_space_type : 0x0d;
  643. spin_unlock_bh(&zcrypt_device_lock);
  644. }
  645. static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
  646. {
  647. struct zcrypt_device *zdev;
  648. memset(qdepth, 0, sizeof(char) * AP_DEVICES);
  649. spin_lock_bh(&zcrypt_device_lock);
  650. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  651. spin_lock(&zdev->ap_dev->lock);
  652. qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  653. zdev->ap_dev->pendingq_count +
  654. zdev->ap_dev->requestq_count;
  655. spin_unlock(&zdev->ap_dev->lock);
  656. }
  657. spin_unlock_bh(&zcrypt_device_lock);
  658. }
  659. static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
  660. {
  661. struct zcrypt_device *zdev;
  662. memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
  663. spin_lock_bh(&zcrypt_device_lock);
  664. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  665. spin_lock(&zdev->ap_dev->lock);
  666. reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  667. zdev->ap_dev->total_request_count;
  668. spin_unlock(&zdev->ap_dev->lock);
  669. }
  670. spin_unlock_bh(&zcrypt_device_lock);
  671. }
  672. static int zcrypt_pendingq_count(void)
  673. {
  674. struct zcrypt_device *zdev;
  675. int pendingq_count = 0;
  676. spin_lock_bh(&zcrypt_device_lock);
  677. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  678. spin_lock(&zdev->ap_dev->lock);
  679. pendingq_count += zdev->ap_dev->pendingq_count;
  680. spin_unlock(&zdev->ap_dev->lock);
  681. }
  682. spin_unlock_bh(&zcrypt_device_lock);
  683. return pendingq_count;
  684. }
  685. static int zcrypt_requestq_count(void)
  686. {
  687. struct zcrypt_device *zdev;
  688. int requestq_count = 0;
  689. spin_lock_bh(&zcrypt_device_lock);
  690. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  691. spin_lock(&zdev->ap_dev->lock);
  692. requestq_count += zdev->ap_dev->requestq_count;
  693. spin_unlock(&zdev->ap_dev->lock);
  694. }
  695. spin_unlock_bh(&zcrypt_device_lock);
  696. return requestq_count;
  697. }
  698. static int zcrypt_count_type(int type)
  699. {
  700. struct zcrypt_device *zdev;
  701. int device_count = 0;
  702. spin_lock_bh(&zcrypt_device_lock);
  703. list_for_each_entry(zdev, &zcrypt_device_list, list)
  704. if (zdev->user_space_type == type)
  705. device_count++;
  706. spin_unlock_bh(&zcrypt_device_lock);
  707. return device_count;
  708. }
  709. /**
  710. * zcrypt_ica_status(): Old, depracted combi status call.
  711. *
  712. * Old, deprecated combi status call.
  713. */
  714. static long zcrypt_ica_status(struct file *filp, unsigned long arg)
  715. {
  716. struct ica_z90_status *pstat;
  717. int ret;
  718. pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
  719. if (!pstat)
  720. return -ENOMEM;
  721. pstat->totalcount = zcrypt_device_count;
  722. pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
  723. pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
  724. pstat->requestqWaitCount = zcrypt_requestq_count();
  725. pstat->pendingqWaitCount = zcrypt_pendingq_count();
  726. pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
  727. pstat->cryptoDomain = ap_domain_index;
  728. zcrypt_status_mask(pstat->status);
  729. zcrypt_qdepth_mask(pstat->qdepth);
  730. ret = 0;
  731. if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
  732. ret = -EFAULT;
  733. kfree(pstat);
  734. return ret;
  735. }
  736. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  737. unsigned long arg)
  738. {
  739. int rc;
  740. switch (cmd) {
  741. case ICARSAMODEXPO: {
  742. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  743. struct ica_rsa_modexpo mex;
  744. if (copy_from_user(&mex, umex, sizeof(mex)))
  745. return -EFAULT;
  746. do {
  747. rc = zcrypt_rsa_modexpo(&mex);
  748. } while (rc == -EAGAIN);
  749. /* on failure: retry once again after a requested rescan */
  750. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  751. do {
  752. rc = zcrypt_rsa_modexpo(&mex);
  753. } while (rc == -EAGAIN);
  754. if (rc)
  755. return rc;
  756. return put_user(mex.outputdatalength, &umex->outputdatalength);
  757. }
  758. case ICARSACRT: {
  759. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  760. struct ica_rsa_modexpo_crt crt;
  761. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  762. return -EFAULT;
  763. do {
  764. rc = zcrypt_rsa_crt(&crt);
  765. } while (rc == -EAGAIN);
  766. /* on failure: retry once again after a requested rescan */
  767. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  768. do {
  769. rc = zcrypt_rsa_crt(&crt);
  770. } while (rc == -EAGAIN);
  771. if (rc)
  772. return rc;
  773. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  774. }
  775. case ZSECSENDCPRB: {
  776. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  777. struct ica_xcRB xcRB;
  778. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  779. return -EFAULT;
  780. do {
  781. rc = zcrypt_send_cprb(&xcRB);
  782. } while (rc == -EAGAIN);
  783. /* on failure: retry once again after a requested rescan */
  784. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  785. do {
  786. rc = zcrypt_send_cprb(&xcRB);
  787. } while (rc == -EAGAIN);
  788. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  789. return -EFAULT;
  790. return rc;
  791. }
  792. case ZSENDEP11CPRB: {
  793. struct ep11_urb __user *uxcrb = (void __user *)arg;
  794. struct ep11_urb xcrb;
  795. if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
  796. return -EFAULT;
  797. do {
  798. rc = zcrypt_send_ep11_cprb(&xcrb);
  799. } while (rc == -EAGAIN);
  800. /* on failure: retry once again after a requested rescan */
  801. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  802. do {
  803. rc = zcrypt_send_ep11_cprb(&xcrb);
  804. } while (rc == -EAGAIN);
  805. if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
  806. return -EFAULT;
  807. return rc;
  808. }
  809. case Z90STAT_STATUS_MASK: {
  810. char status[AP_DEVICES];
  811. zcrypt_status_mask(status);
  812. if (copy_to_user((char __user *) arg, status,
  813. sizeof(char) * AP_DEVICES))
  814. return -EFAULT;
  815. return 0;
  816. }
  817. case Z90STAT_QDEPTH_MASK: {
  818. char qdepth[AP_DEVICES];
  819. zcrypt_qdepth_mask(qdepth);
  820. if (copy_to_user((char __user *) arg, qdepth,
  821. sizeof(char) * AP_DEVICES))
  822. return -EFAULT;
  823. return 0;
  824. }
  825. case Z90STAT_PERDEV_REQCNT: {
  826. int reqcnt[AP_DEVICES];
  827. zcrypt_perdev_reqcnt(reqcnt);
  828. if (copy_to_user((int __user *) arg, reqcnt,
  829. sizeof(int) * AP_DEVICES))
  830. return -EFAULT;
  831. return 0;
  832. }
  833. case Z90STAT_REQUESTQ_COUNT:
  834. return put_user(zcrypt_requestq_count(), (int __user *) arg);
  835. case Z90STAT_PENDINGQ_COUNT:
  836. return put_user(zcrypt_pendingq_count(), (int __user *) arg);
  837. case Z90STAT_TOTALOPEN_COUNT:
  838. return put_user(atomic_read(&zcrypt_open_count),
  839. (int __user *) arg);
  840. case Z90STAT_DOMAIN_INDEX:
  841. return put_user(ap_domain_index, (int __user *) arg);
  842. /*
  843. * Deprecated ioctls. Don't add another device count ioctl,
  844. * you can count them yourself in the user space with the
  845. * output of the Z90STAT_STATUS_MASK ioctl.
  846. */
  847. case ICAZ90STATUS:
  848. return zcrypt_ica_status(filp, arg);
  849. case Z90STAT_TOTALCOUNT:
  850. return put_user(zcrypt_device_count, (int __user *) arg);
  851. case Z90STAT_PCICACOUNT:
  852. return put_user(zcrypt_count_type(ZCRYPT_PCICA),
  853. (int __user *) arg);
  854. case Z90STAT_PCICCCOUNT:
  855. return put_user(zcrypt_count_type(ZCRYPT_PCICC),
  856. (int __user *) arg);
  857. case Z90STAT_PCIXCCMCL2COUNT:
  858. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
  859. (int __user *) arg);
  860. case Z90STAT_PCIXCCMCL3COUNT:
  861. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  862. (int __user *) arg);
  863. case Z90STAT_PCIXCCCOUNT:
  864. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
  865. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  866. (int __user *) arg);
  867. case Z90STAT_CEX2CCOUNT:
  868. return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
  869. (int __user *) arg);
  870. case Z90STAT_CEX2ACOUNT:
  871. return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
  872. (int __user *) arg);
  873. default:
  874. /* unknown ioctl number */
  875. return -ENOIOCTLCMD;
  876. }
  877. }
  878. #ifdef CONFIG_COMPAT
  879. /*
  880. * ioctl32 conversion routines
  881. */
  882. struct compat_ica_rsa_modexpo {
  883. compat_uptr_t inputdata;
  884. unsigned int inputdatalength;
  885. compat_uptr_t outputdata;
  886. unsigned int outputdatalength;
  887. compat_uptr_t b_key;
  888. compat_uptr_t n_modulus;
  889. };
  890. static long trans_modexpo32(struct file *filp, unsigned int cmd,
  891. unsigned long arg)
  892. {
  893. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  894. struct compat_ica_rsa_modexpo mex32;
  895. struct ica_rsa_modexpo mex64;
  896. long rc;
  897. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  898. return -EFAULT;
  899. mex64.inputdata = compat_ptr(mex32.inputdata);
  900. mex64.inputdatalength = mex32.inputdatalength;
  901. mex64.outputdata = compat_ptr(mex32.outputdata);
  902. mex64.outputdatalength = mex32.outputdatalength;
  903. mex64.b_key = compat_ptr(mex32.b_key);
  904. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  905. do {
  906. rc = zcrypt_rsa_modexpo(&mex64);
  907. } while (rc == -EAGAIN);
  908. /* on failure: retry once again after a requested rescan */
  909. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  910. do {
  911. rc = zcrypt_rsa_modexpo(&mex64);
  912. } while (rc == -EAGAIN);
  913. if (rc)
  914. return rc;
  915. return put_user(mex64.outputdatalength,
  916. &umex32->outputdatalength);
  917. }
  918. struct compat_ica_rsa_modexpo_crt {
  919. compat_uptr_t inputdata;
  920. unsigned int inputdatalength;
  921. compat_uptr_t outputdata;
  922. unsigned int outputdatalength;
  923. compat_uptr_t bp_key;
  924. compat_uptr_t bq_key;
  925. compat_uptr_t np_prime;
  926. compat_uptr_t nq_prime;
  927. compat_uptr_t u_mult_inv;
  928. };
  929. static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
  930. unsigned long arg)
  931. {
  932. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  933. struct compat_ica_rsa_modexpo_crt crt32;
  934. struct ica_rsa_modexpo_crt crt64;
  935. long rc;
  936. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  937. return -EFAULT;
  938. crt64.inputdata = compat_ptr(crt32.inputdata);
  939. crt64.inputdatalength = crt32.inputdatalength;
  940. crt64.outputdata= compat_ptr(crt32.outputdata);
  941. crt64.outputdatalength = crt32.outputdatalength;
  942. crt64.bp_key = compat_ptr(crt32.bp_key);
  943. crt64.bq_key = compat_ptr(crt32.bq_key);
  944. crt64.np_prime = compat_ptr(crt32.np_prime);
  945. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  946. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  947. do {
  948. rc = zcrypt_rsa_crt(&crt64);
  949. } while (rc == -EAGAIN);
  950. /* on failure: retry once again after a requested rescan */
  951. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  952. do {
  953. rc = zcrypt_rsa_crt(&crt64);
  954. } while (rc == -EAGAIN);
  955. if (rc)
  956. return rc;
  957. return put_user(crt64.outputdatalength,
  958. &ucrt32->outputdatalength);
  959. }
  960. struct compat_ica_xcRB {
  961. unsigned short agent_ID;
  962. unsigned int user_defined;
  963. unsigned short request_ID;
  964. unsigned int request_control_blk_length;
  965. unsigned char padding1[16 - sizeof (compat_uptr_t)];
  966. compat_uptr_t request_control_blk_addr;
  967. unsigned int request_data_length;
  968. char padding2[16 - sizeof (compat_uptr_t)];
  969. compat_uptr_t request_data_address;
  970. unsigned int reply_control_blk_length;
  971. char padding3[16 - sizeof (compat_uptr_t)];
  972. compat_uptr_t reply_control_blk_addr;
  973. unsigned int reply_data_length;
  974. char padding4[16 - sizeof (compat_uptr_t)];
  975. compat_uptr_t reply_data_addr;
  976. unsigned short priority_window;
  977. unsigned int status;
  978. } __attribute__((packed));
  979. static long trans_xcRB32(struct file *filp, unsigned int cmd,
  980. unsigned long arg)
  981. {
  982. struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
  983. struct compat_ica_xcRB xcRB32;
  984. struct ica_xcRB xcRB64;
  985. long rc;
  986. if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
  987. return -EFAULT;
  988. xcRB64.agent_ID = xcRB32.agent_ID;
  989. xcRB64.user_defined = xcRB32.user_defined;
  990. xcRB64.request_ID = xcRB32.request_ID;
  991. xcRB64.request_control_blk_length =
  992. xcRB32.request_control_blk_length;
  993. xcRB64.request_control_blk_addr =
  994. compat_ptr(xcRB32.request_control_blk_addr);
  995. xcRB64.request_data_length =
  996. xcRB32.request_data_length;
  997. xcRB64.request_data_address =
  998. compat_ptr(xcRB32.request_data_address);
  999. xcRB64.reply_control_blk_length =
  1000. xcRB32.reply_control_blk_length;
  1001. xcRB64.reply_control_blk_addr =
  1002. compat_ptr(xcRB32.reply_control_blk_addr);
  1003. xcRB64.reply_data_length = xcRB32.reply_data_length;
  1004. xcRB64.reply_data_addr =
  1005. compat_ptr(xcRB32.reply_data_addr);
  1006. xcRB64.priority_window = xcRB32.priority_window;
  1007. xcRB64.status = xcRB32.status;
  1008. do {
  1009. rc = zcrypt_send_cprb(&xcRB64);
  1010. } while (rc == -EAGAIN);
  1011. /* on failure: retry once again after a requested rescan */
  1012. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1013. do {
  1014. rc = zcrypt_send_cprb(&xcRB64);
  1015. } while (rc == -EAGAIN);
  1016. xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
  1017. xcRB32.reply_data_length = xcRB64.reply_data_length;
  1018. xcRB32.status = xcRB64.status;
  1019. if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
  1020. return -EFAULT;
  1021. return rc;
  1022. }
  1023. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  1024. unsigned long arg)
  1025. {
  1026. if (cmd == ICARSAMODEXPO)
  1027. return trans_modexpo32(filp, cmd, arg);
  1028. if (cmd == ICARSACRT)
  1029. return trans_modexpo_crt32(filp, cmd, arg);
  1030. if (cmd == ZSECSENDCPRB)
  1031. return trans_xcRB32(filp, cmd, arg);
  1032. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  1033. }
  1034. #endif
  1035. /*
  1036. * Misc device file operations.
  1037. */
  1038. static const struct file_operations zcrypt_fops = {
  1039. .owner = THIS_MODULE,
  1040. .read = zcrypt_read,
  1041. .write = zcrypt_write,
  1042. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  1043. #ifdef CONFIG_COMPAT
  1044. .compat_ioctl = zcrypt_compat_ioctl,
  1045. #endif
  1046. .open = zcrypt_open,
  1047. .release = zcrypt_release,
  1048. .llseek = no_llseek,
  1049. };
  1050. /*
  1051. * Misc device.
  1052. */
  1053. static struct miscdevice zcrypt_misc_device = {
  1054. .minor = MISC_DYNAMIC_MINOR,
  1055. .name = "z90crypt",
  1056. .fops = &zcrypt_fops,
  1057. };
  1058. /*
  1059. * Deprecated /proc entry support.
  1060. */
  1061. static struct proc_dir_entry *zcrypt_entry;
  1062. static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
  1063. {
  1064. int i;
  1065. for (i = 0; i < len; i++)
  1066. seq_printf(m, "%01x", (unsigned int) addr[i]);
  1067. seq_putc(m, ' ');
  1068. }
  1069. static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
  1070. {
  1071. int inl, c, cx;
  1072. seq_printf(m, " ");
  1073. inl = 0;
  1074. for (c = 0; c < (len / 16); c++) {
  1075. sprintcl(m, addr+inl, 16);
  1076. inl += 16;
  1077. }
  1078. cx = len%16;
  1079. if (cx) {
  1080. sprintcl(m, addr+inl, cx);
  1081. inl += cx;
  1082. }
  1083. seq_putc(m, '\n');
  1084. }
  1085. static void sprinthx(unsigned char *title, struct seq_file *m,
  1086. unsigned char *addr, unsigned int len)
  1087. {
  1088. int inl, r, rx;
  1089. seq_printf(m, "\n%s\n", title);
  1090. inl = 0;
  1091. for (r = 0; r < (len / 64); r++) {
  1092. sprintrw(m, addr+inl, 64);
  1093. inl += 64;
  1094. }
  1095. rx = len % 64;
  1096. if (rx) {
  1097. sprintrw(m, addr+inl, rx);
  1098. inl += rx;
  1099. }
  1100. seq_putc(m, '\n');
  1101. }
  1102. static void sprinthx4(unsigned char *title, struct seq_file *m,
  1103. unsigned int *array, unsigned int len)
  1104. {
  1105. int r;
  1106. seq_printf(m, "\n%s\n", title);
  1107. for (r = 0; r < len; r++) {
  1108. if ((r % 8) == 0)
  1109. seq_printf(m, " ");
  1110. seq_printf(m, "%08X ", array[r]);
  1111. if ((r % 8) == 7)
  1112. seq_putc(m, '\n');
  1113. }
  1114. seq_putc(m, '\n');
  1115. }
  1116. static int zcrypt_proc_show(struct seq_file *m, void *v)
  1117. {
  1118. char workarea[sizeof(int) * AP_DEVICES];
  1119. seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
  1120. ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
  1121. seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
  1122. seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
  1123. seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
  1124. seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
  1125. seq_printf(m, "PCIXCC MCL2 count: %d\n",
  1126. zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
  1127. seq_printf(m, "PCIXCC MCL3 count: %d\n",
  1128. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
  1129. seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
  1130. seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
  1131. seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
  1132. seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
  1133. seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
  1134. seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
  1135. seq_printf(m, "Total open handles: %d\n\n",
  1136. atomic_read(&zcrypt_open_count));
  1137. zcrypt_status_mask(workarea);
  1138. sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  1139. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
  1140. m, workarea, AP_DEVICES);
  1141. zcrypt_qdepth_mask(workarea);
  1142. sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
  1143. zcrypt_perdev_reqcnt((int *) workarea);
  1144. sprinthx4("Per-device successfully completed request counts",
  1145. m, (unsigned int *) workarea, AP_DEVICES);
  1146. return 0;
  1147. }
  1148. static int zcrypt_proc_open(struct inode *inode, struct file *file)
  1149. {
  1150. return single_open(file, zcrypt_proc_show, NULL);
  1151. }
  1152. static void zcrypt_disable_card(int index)
  1153. {
  1154. struct zcrypt_device *zdev;
  1155. spin_lock_bh(&zcrypt_device_lock);
  1156. list_for_each_entry(zdev, &zcrypt_device_list, list)
  1157. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  1158. zdev->online = 0;
  1159. ap_flush_queue(zdev->ap_dev);
  1160. break;
  1161. }
  1162. spin_unlock_bh(&zcrypt_device_lock);
  1163. }
  1164. static void zcrypt_enable_card(int index)
  1165. {
  1166. struct zcrypt_device *zdev;
  1167. spin_lock_bh(&zcrypt_device_lock);
  1168. list_for_each_entry(zdev, &zcrypt_device_list, list)
  1169. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  1170. zdev->online = 1;
  1171. break;
  1172. }
  1173. spin_unlock_bh(&zcrypt_device_lock);
  1174. }
  1175. static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
  1176. size_t count, loff_t *pos)
  1177. {
  1178. unsigned char *lbuf, *ptr;
  1179. size_t local_count;
  1180. int j;
  1181. if (count <= 0)
  1182. return 0;
  1183. #define LBUFSIZE 1200UL
  1184. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  1185. if (!lbuf)
  1186. return 0;
  1187. local_count = min(LBUFSIZE - 1, count);
  1188. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  1189. kfree(lbuf);
  1190. return -EFAULT;
  1191. }
  1192. lbuf[local_count] = '\0';
  1193. ptr = strstr(lbuf, "Online devices");
  1194. if (!ptr)
  1195. goto out;
  1196. ptr = strstr(ptr, "\n");
  1197. if (!ptr)
  1198. goto out;
  1199. ptr++;
  1200. if (strstr(ptr, "Waiting work element counts") == NULL)
  1201. goto out;
  1202. for (j = 0; j < 64 && *ptr; ptr++) {
  1203. /*
  1204. * '0' for no device, '1' for PCICA, '2' for PCICC,
  1205. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  1206. * '5' for CEX2C and '6' for CEX2A'
  1207. * '7' for CEX3C and '8' for CEX3A
  1208. */
  1209. if (*ptr >= '0' && *ptr <= '8')
  1210. j++;
  1211. else if (*ptr == 'd' || *ptr == 'D')
  1212. zcrypt_disable_card(j++);
  1213. else if (*ptr == 'e' || *ptr == 'E')
  1214. zcrypt_enable_card(j++);
  1215. else if (*ptr != ' ' && *ptr != '\t')
  1216. break;
  1217. }
  1218. out:
  1219. kfree(lbuf);
  1220. return count;
  1221. }
  1222. static const struct file_operations zcrypt_proc_fops = {
  1223. .owner = THIS_MODULE,
  1224. .open = zcrypt_proc_open,
  1225. .read = seq_read,
  1226. .llseek = seq_lseek,
  1227. .release = single_release,
  1228. .write = zcrypt_proc_write,
  1229. };
  1230. static int zcrypt_rng_device_count;
  1231. static u32 *zcrypt_rng_buffer;
  1232. static int zcrypt_rng_buffer_index;
  1233. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1234. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1235. {
  1236. int rc;
  1237. /*
  1238. * We don't need locking here because the RNG API guarantees serialized
  1239. * read method calls.
  1240. */
  1241. if (zcrypt_rng_buffer_index == 0) {
  1242. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1243. /* on failure: retry once again after a requested rescan */
  1244. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1245. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1246. if (rc < 0)
  1247. return -EIO;
  1248. zcrypt_rng_buffer_index = rc / sizeof *data;
  1249. }
  1250. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1251. return sizeof *data;
  1252. }
  1253. static struct hwrng zcrypt_rng_dev = {
  1254. .name = "zcrypt",
  1255. .data_read = zcrypt_rng_data_read,
  1256. };
  1257. static int zcrypt_rng_device_add(void)
  1258. {
  1259. int rc = 0;
  1260. mutex_lock(&zcrypt_rng_mutex);
  1261. if (zcrypt_rng_device_count == 0) {
  1262. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1263. if (!zcrypt_rng_buffer) {
  1264. rc = -ENOMEM;
  1265. goto out;
  1266. }
  1267. zcrypt_rng_buffer_index = 0;
  1268. rc = hwrng_register(&zcrypt_rng_dev);
  1269. if (rc)
  1270. goto out_free;
  1271. zcrypt_rng_device_count = 1;
  1272. } else
  1273. zcrypt_rng_device_count++;
  1274. mutex_unlock(&zcrypt_rng_mutex);
  1275. return 0;
  1276. out_free:
  1277. free_page((unsigned long) zcrypt_rng_buffer);
  1278. out:
  1279. mutex_unlock(&zcrypt_rng_mutex);
  1280. return rc;
  1281. }
  1282. static void zcrypt_rng_device_remove(void)
  1283. {
  1284. mutex_lock(&zcrypt_rng_mutex);
  1285. zcrypt_rng_device_count--;
  1286. if (zcrypt_rng_device_count == 0) {
  1287. hwrng_unregister(&zcrypt_rng_dev);
  1288. free_page((unsigned long) zcrypt_rng_buffer);
  1289. }
  1290. mutex_unlock(&zcrypt_rng_mutex);
  1291. }
  1292. int __init zcrypt_debug_init(void)
  1293. {
  1294. debugfs_root = debugfs_create_dir("zcrypt", NULL);
  1295. zcrypt_dbf_common = debug_register("zcrypt_common", 1, 1, 16);
  1296. debug_register_view(zcrypt_dbf_common, &debug_hex_ascii_view);
  1297. debug_set_level(zcrypt_dbf_common, DBF_ERR);
  1298. zcrypt_dbf_devices = debug_register("zcrypt_devices", 1, 1, 16);
  1299. debug_register_view(zcrypt_dbf_devices, &debug_hex_ascii_view);
  1300. debug_set_level(zcrypt_dbf_devices, DBF_ERR);
  1301. return 0;
  1302. }
  1303. void zcrypt_debug_exit(void)
  1304. {
  1305. debugfs_remove(debugfs_root);
  1306. if (zcrypt_dbf_common)
  1307. debug_unregister(zcrypt_dbf_common);
  1308. if (zcrypt_dbf_devices)
  1309. debug_unregister(zcrypt_dbf_devices);
  1310. }
  1311. /**
  1312. * zcrypt_api_init(): Module initialization.
  1313. *
  1314. * The module initialization code.
  1315. */
  1316. int __init zcrypt_api_init(void)
  1317. {
  1318. int rc;
  1319. rc = zcrypt_debug_init();
  1320. if (rc)
  1321. goto out;
  1322. atomic_set(&zcrypt_rescan_req, 0);
  1323. /* Register the request sprayer. */
  1324. rc = misc_register(&zcrypt_misc_device);
  1325. if (rc < 0)
  1326. goto out;
  1327. /* Set up the proc file system */
  1328. zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
  1329. if (!zcrypt_entry) {
  1330. rc = -ENOMEM;
  1331. goto out_misc;
  1332. }
  1333. return 0;
  1334. out_misc:
  1335. misc_deregister(&zcrypt_misc_device);
  1336. out:
  1337. return rc;
  1338. }
  1339. /**
  1340. * zcrypt_api_exit(): Module termination.
  1341. *
  1342. * The module termination code.
  1343. */
  1344. void zcrypt_api_exit(void)
  1345. {
  1346. remove_proc_entry("driver/z90crypt", NULL);
  1347. misc_deregister(&zcrypt_misc_device);
  1348. zcrypt_debug_exit();
  1349. }
  1350. module_init(zcrypt_api_init);
  1351. module_exit(zcrypt_api_exit);