2 * linux/drivers/s390/crypto/z90main.c
6 * Copyright (C) 2001, 2004 IBM Corporation
7 * Author(s): Robert Burroughs (burrough@us.ibm.com)
8 * Eric Rossman (edrossma@us.ibm.com)
10 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <asm/uaccess.h> // copy_(from|to)_user
28 #include <linux/compat.h>
29 #include <linux/compiler.h>
30 #include <linux/delay.h> // mdelay
31 #include <linux/init.h>
32 #include <linux/interrupt.h> // for tasklets
33 #include <linux/ioctl32.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/kobject_uevent.h>
37 #include <linux/proc_fs.h>
38 #include <linux/syscalls.h>
39 #include <linux/version.h>
41 #include "z90common.h"
42 #ifndef Z90CRYPT_USE_HOTPLUG
43 #include <linux/miscdevice.h>
46 #define VERSION_CODE(vers, rel, seq) (((vers)<<16) | ((rel)<<8) | (seq))
47 #if LINUX_VERSION_CODE < VERSION_CODE(2,4,0) /* version < 2.4 */
48 # error "This kernel is too old: not supported"
50 #if LINUX_VERSION_CODE > VERSION_CODE(2,7,0) /* version > 2.6 */
51 # error "This kernel is too recent: not supported by this file"
54 #define VERSION_Z90MAIN_C "$Revision: 1.57 $"
56 static char z90main_version
[] __initdata
=
57 "z90main.o (" VERSION_Z90MAIN_C
"/"
58 VERSION_Z90COMMON_H
"/" VERSION_Z90CRYPT_H
")";
60 extern char z90hardware_version
[];
63 * Defaults that may be modified.
66 #ifndef Z90CRYPT_USE_HOTPLUG
68 * You can specify a different minor at compile time.
70 #ifndef Z90CRYPT_MINOR
71 #define Z90CRYPT_MINOR MISC_DYNAMIC_MINOR
75 * You can specify a different major at compile time.
77 #ifndef Z90CRYPT_MAJOR
78 #define Z90CRYPT_MAJOR 0
83 * You can specify a different domain at compile time or on the insmod
87 #define DOMAIN_INDEX -1
91 * This is the name under which the device is registered in /proc/modules.
93 #define REG_NAME "z90crypt"
96 * Cleanup should run every CLEANUPTIME seconds and should clean up requests
97 * older than CLEANUPTIME seconds in the past.
100 #define CLEANUPTIME 20
104 * Config should run every CONFIGTIME seconds
107 #define CONFIGTIME 30
111 * The first execution of the config task should take place
112 * immediately after initialization
114 #ifndef INITIAL_CONFIGTIME
115 #define INITIAL_CONFIGTIME 1
119 * Reader should run every READERTIME milliseconds
120 * With the 100Hz patch for s390, z90crypt can lock the system solid while
121 * under heavy load. We'll try to avoid that.
127 #define READERTIME 10
132 * turn long device array index into device pointer
134 #define LONG2DEVPTR(ndx) (z90crypt.device_p[(ndx)])
137 * turn short device array index into long device array index
139 #define SHRT2LONG(ndx) (z90crypt.overall_device_x.device_index[(ndx)])
142 * turn short device array index into device pointer
144 #define SHRT2DEVPTR(ndx) LONG2DEVPTR(SHRT2LONG(ndx))
147 * Status for a work-element
149 #define STAT_DEFAULT 0x00 // request has not been processed
151 #define STAT_ROUTED 0x80 // bit 7: requests get routed to specific device
152 // else, device is determined each write
153 #define STAT_FAILED 0x40 // bit 6: this bit is set if the request failed
154 // before being sent to the hardware.
155 #define STAT_WRITTEN 0x30 // bits 5-4: work to be done, not sent to device
156 // 0x20 // UNUSED state
157 #define STAT_READPEND 0x10 // bits 5-4: work done, we're returning data now
158 #define STAT_NOWORK 0x00 // bits off: no work on any queue
159 #define STAT_RDWRMASK 0x30 // mask for bits 5-4
162 * Macros to check the status RDWRMASK
164 #define CHK_RDWRMASK(statbyte) ((statbyte) & STAT_RDWRMASK)
165 #define SET_RDWRMASK(statbyte, newval) \
166 {(statbyte) &= ~STAT_RDWRMASK; (statbyte) |= newval;}
169 * Audit Trail. Progress of a Work element
170 * audit[0]: Unless noted otherwise, these bits are all set by the process
172 #define FP_COPYFROM 0x80 // Caller's buffer has been copied to work element
173 #define FP_BUFFREQ 0x40 // Low Level buffer requested
174 #define FP_BUFFGOT 0x20 // Low Level buffer obtained
175 #define FP_SENT 0x10 // Work element sent to a crypto device
176 // (may be set by process or by reader task)
177 #define FP_PENDING 0x08 // Work element placed on pending queue
178 // (may be set by process or by reader task)
179 #define FP_REQUEST 0x04 // Work element placed on request queue
180 #define FP_ASLEEP 0x02 // Work element about to sleep
181 #define FP_AWAKE 0x01 // Work element has been awakened
184 * audit[1]: These bits are set by the reader task and/or the cleanup task
186 #define FP_NOTPENDING 0x80 // Work element removed from pending queue
187 #define FP_AWAKENING 0x40 // Caller about to be awakened
188 #define FP_TIMEDOUT 0x20 // Caller timed out
189 #define FP_RESPSIZESET 0x10 // Response size copied to work element
190 #define FP_RESPADDRCOPIED 0x08 // Response address copied to work element
191 #define FP_RESPBUFFCOPIED 0x04 // Response buffer copied to work element
192 #define FP_REMREQUEST 0x02 // Work element removed from request queue
193 #define FP_SIGNALED 0x01 // Work element was awakened by a signal
200 * state of the file handle in private_data.status
203 #define STAT_CLOSED 1
206 * PID() expands to the process ID of the current process
208 #define PID() (current->pid)
211 * Selected Constants. The number of APs and the number of devices
213 #ifndef Z90CRYPT_NUM_APS
214 #define Z90CRYPT_NUM_APS 64
216 #ifndef Z90CRYPT_NUM_DEVS
217 #define Z90CRYPT_NUM_DEVS Z90CRYPT_NUM_APS
221 * Buffer size for receiving responses. The maximum Response Size
222 * is actually the maximum request size, since in an error condition
223 * the request itself may be returned unchanged.
225 #define MAX_RESPONSE_SIZE 0x0000077C
228 * A count and status-byte mask
231 int st_count
; // # of enabled devices
232 int disabled_count
; // # of disabled devices
233 int user_disabled_count
; // # of devices disabled via proc fs
234 unsigned char st_mask
[Z90CRYPT_NUM_APS
]; // current status mask
238 * The array of device indexes is a mechanism for fast indexing into
239 * a long (and sparse) array. For instance, if APs 3, 9 and 47 are
240 * installed, z90CDeviceIndex[0] is 3, z90CDeviceIndex[1] is 9, and
241 * z90CDeviceIndex[2] is 47.
244 int device_index
[Z90CRYPT_NUM_DEVS
];
248 * All devices are arranged in a single array: 64 APs
251 int dev_type
; // PCICA, PCICC, PCIXCC_MCL2,
252 // PCIXCC_MCL3, CEX2C
253 enum devstat dev_stat
; // current device status
254 int dev_self_x
; // Index in array
255 int disabled
; // Set when device is in error
256 int user_disabled
; // Set when device is disabled by user
257 int dev_q_depth
; // q depth
258 unsigned char * dev_resp_p
; // Response buffer address
259 int dev_resp_l
; // Response Buffer length
260 int dev_caller_count
; // Number of callers
261 int dev_total_req_cnt
; // # requests for device since load
262 struct list_head dev_caller_list
; // List of callers
266 * There's a struct status and a struct device_x for each device type.
268 struct hdware_block
{
269 struct status hdware_mask
;
270 struct status type_mask
[Z90CRYPT_NUM_TYPES
];
271 struct device_x type_x_addr
[Z90CRYPT_NUM_TYPES
];
272 unsigned char device_type_array
[Z90CRYPT_NUM_APS
];
276 * z90crypt is the topmost data structure in the hierarchy.
279 int max_count
; // Nr of possible crypto devices
281 int q_depth_array
[Z90CRYPT_NUM_DEVS
];
282 int dev_type_array
[Z90CRYPT_NUM_DEVS
];
283 struct device_x overall_device_x
; // array device indexes
284 struct device
* device_p
[Z90CRYPT_NUM_DEVS
];
286 int domain_established
;// TRUE: domain has been found
287 int cdx
; // Crypto Domain Index
288 int len
; // Length of this data structure
289 struct hdware_block
*hdware_info
;
293 * An array of these structures is pointed to from dev_caller
294 * The length of the array depends on the device type. For APs,
297 * The caller buffer is allocated to the user at OPEN. At WRITE,
298 * it contains the request; at READ, the response. The function
299 * send_to_crypto_device converts the request to device-dependent
300 * form and use the caller's OPEN-allocated buffer for the response.
303 int caller_buf_l
; // length of original request
304 unsigned char * caller_buf_p
; // Original request on WRITE
305 int caller_dev_dep_req_l
; // len device dependent request
306 unsigned char * caller_dev_dep_req_p
; // Device dependent form
307 unsigned char caller_id
[8]; // caller-supplied message id
308 struct list_head caller_liste
;
309 unsigned char caller_dev_dep_req
[MAX_RESPONSE_SIZE
];
313 * Function prototypes from z90hardware.c
315 enum hdstat
query_online(int, int, int, int *, int *);
316 enum devstat
reset_device(int, int, int);
317 enum devstat
send_to_AP(int, int, int, unsigned char *);
318 enum devstat
receive_from_AP(int, int, int, unsigned char *, unsigned char *);
319 int convert_request(unsigned char *, int, short, int, int, int *,
321 int convert_response(unsigned char *, unsigned char *, int *, unsigned char *);
324 * Low level function prototypes
326 static int create_z90crypt(int *);
327 static int refresh_z90crypt(int *);
328 static int find_crypto_devices(struct status
*);
329 static int create_crypto_device(int);
330 static int destroy_crypto_device(int);
331 static void destroy_z90crypt(void);
332 static int refresh_index_array(struct status
*, struct device_x
*);
333 static int probe_device_type(struct device
*);
334 static int probe_PCIXCC_type(struct device
*);
337 * proc fs definitions
339 static struct proc_dir_entry
*z90crypt_entry
;
346 * work_element.opener points back to this structure
350 unsigned char status
; // 0: open 1: closed
354 * A work element is allocated for each request
356 struct work_element
{
357 struct priv_data
*priv_data
;
359 int devindex
; // index of device processing this w_e
360 // (If request did not specify device,
361 // -1 until placed onto a queue)
363 struct list_head liste
; // used for requestq and pendingq
364 char buffer
[128]; // local copy of user request
365 int buff_size
; // size of the buffer for the request
366 char resp_buff
[RESPBUFFSIZE
];
368 char __user
* resp_addr
; // address of response in user space
369 unsigned int funccode
; // function code of request
370 wait_queue_head_t waitq
;
371 unsigned long requestsent
; // time at which the request was sent
372 atomic_t alarmrung
; // wake-up signal
373 unsigned char caller_id
[8]; // pid + counter, for this w_e
374 unsigned char status
[1]; // bits to mark status of the request
375 unsigned char audit
[3]; // record of work element's progress
376 unsigned char * requestptr
; // address of request buffer
377 int retcode
; // return code of request
381 * High level function prototypes
383 static int z90crypt_open(struct inode
*, struct file
*);
384 static int z90crypt_release(struct inode
*, struct file
*);
385 static ssize_t
z90crypt_read(struct file
*, char __user
*, size_t, loff_t
*);
386 static ssize_t
z90crypt_write(struct file
*, const char __user
*,
388 static int z90crypt_ioctl(struct inode
*, struct file
*,
389 unsigned int, unsigned long);
391 static void z90crypt_reader_task(unsigned long);
392 static void z90crypt_schedule_reader_task(unsigned long);
393 static void z90crypt_config_task(unsigned long);
394 static void z90crypt_cleanup_task(unsigned long);
396 static int z90crypt_status(char *, char **, off_t
, int, int *, void *);
397 static int z90crypt_status_write(struct file
*, const char __user
*,
398 unsigned long, void *);
404 #ifdef Z90CRYPT_USE_HOTPLUG
405 #define Z90CRYPT_HOTPLUG_ADD 1
406 #define Z90CRYPT_HOTPLUG_REMOVE 2
408 static void z90crypt_hotplug_event(int, int, int);
412 * Storage allocated at initialization and used throughout the life of
415 #ifdef Z90CRYPT_USE_HOTPLUG
416 static int z90crypt_major
= Z90CRYPT_MAJOR
;
419 static int domain
= DOMAIN_INDEX
;
420 static struct z90crypt z90crypt
;
421 static int quiesce_z90crypt
;
422 static spinlock_t queuespinlock
;
423 static struct list_head request_list
;
424 static int requestq_count
;
425 static struct list_head pending_list
;
426 static int pendingq_count
;
428 static struct tasklet_struct reader_tasklet
;
429 static struct timer_list reader_timer
;
430 static struct timer_list config_timer
;
431 static struct timer_list cleanup_timer
;
432 static atomic_t total_open
;
433 static atomic_t z90crypt_step
;
435 static struct file_operations z90crypt_fops
= {
436 .owner
= THIS_MODULE
,
437 .read
= z90crypt_read
,
438 .write
= z90crypt_write
,
439 .ioctl
= z90crypt_ioctl
,
440 .open
= z90crypt_open
,
441 .release
= z90crypt_release
444 #ifndef Z90CRYPT_USE_HOTPLUG
445 static struct miscdevice z90crypt_misc_device
= {
446 .minor
= Z90CRYPT_MINOR
,
448 .fops
= &z90crypt_fops
,
449 .devfs_name
= DEV_NAME
454 * Documentation values.
456 MODULE_AUTHOR("zSeries Linux Crypto Team: Robert H. Burroughs, Eric D. Rossman"
457 "and Jochen Roehrig");
458 MODULE_DESCRIPTION("zSeries Linux Cryptographic Coprocessor device driver, "
459 "Copyright 2001, 2004 IBM Corporation");
460 MODULE_LICENSE("GPL");
461 module_param(domain
, int, 0);
462 MODULE_PARM_DESC(domain
, "domain index for device");
466 * ioctl32 conversion routines
468 struct ica_rsa_modexpo_32
{ // For 32-bit callers
469 compat_uptr_t inputdata
;
470 unsigned int inputdatalength
;
471 compat_uptr_t outputdata
;
472 unsigned int outputdatalength
;
474 compat_uptr_t n_modulus
;
478 trans_modexpo32(unsigned int fd
, unsigned int cmd
, unsigned long arg
,
481 struct ica_rsa_modexpo_32 __user
*mex32u
= compat_ptr(arg
);
482 struct ica_rsa_modexpo_32 mex32k
;
483 struct ica_rsa_modexpo __user
*mex64
;
487 if (!access_ok(VERIFY_WRITE
, mex32u
, sizeof(struct ica_rsa_modexpo_32
)))
489 mex64
= compat_alloc_user_space(sizeof(struct ica_rsa_modexpo
));
490 if (!access_ok(VERIFY_WRITE
, mex64
, sizeof(struct ica_rsa_modexpo
)))
492 if (copy_from_user(&mex32k
, mex32u
, sizeof(struct ica_rsa_modexpo_32
)))
494 if (__put_user(compat_ptr(mex32k
.inputdata
), &mex64
->inputdata
) ||
495 __put_user(mex32k
.inputdatalength
, &mex64
->inputdatalength
) ||
496 __put_user(compat_ptr(mex32k
.outputdata
), &mex64
->outputdata
) ||
497 __put_user(mex32k
.outputdatalength
, &mex64
->outputdatalength
) ||
498 __put_user(compat_ptr(mex32k
.b_key
), &mex64
->b_key
) ||
499 __put_user(compat_ptr(mex32k
.n_modulus
), &mex64
->n_modulus
))
501 ret
= sys_ioctl(fd
, cmd
, (unsigned long)mex64
);
503 if (__get_user(i
, &mex64
->outputdatalength
) ||
504 __put_user(i
, &mex32u
->outputdatalength
))
509 struct ica_rsa_modexpo_crt_32
{ // For 32-bit callers
510 compat_uptr_t inputdata
;
511 unsigned int inputdatalength
;
512 compat_uptr_t outputdata
;
513 unsigned int outputdatalength
;
514 compat_uptr_t bp_key
;
515 compat_uptr_t bq_key
;
516 compat_uptr_t np_prime
;
517 compat_uptr_t nq_prime
;
518 compat_uptr_t u_mult_inv
;
522 trans_modexpo_crt32(unsigned int fd
, unsigned int cmd
, unsigned long arg
,
525 struct ica_rsa_modexpo_crt_32 __user
*crt32u
= compat_ptr(arg
);
526 struct ica_rsa_modexpo_crt_32 crt32k
;
527 struct ica_rsa_modexpo_crt __user
*crt64
;
531 if (!access_ok(VERIFY_WRITE
, crt32u
,
532 sizeof(struct ica_rsa_modexpo_crt_32
)))
534 crt64
= compat_alloc_user_space(sizeof(struct ica_rsa_modexpo_crt
));
535 if (!access_ok(VERIFY_WRITE
, crt64
, sizeof(struct ica_rsa_modexpo_crt
)))
537 if (copy_from_user(&crt32k
, crt32u
,
538 sizeof(struct ica_rsa_modexpo_crt_32
)))
540 if (__put_user(compat_ptr(crt32k
.inputdata
), &crt64
->inputdata
) ||
541 __put_user(crt32k
.inputdatalength
, &crt64
->inputdatalength
) ||
542 __put_user(compat_ptr(crt32k
.outputdata
), &crt64
->outputdata
) ||
543 __put_user(crt32k
.outputdatalength
, &crt64
->outputdatalength
) ||
544 __put_user(compat_ptr(crt32k
.bp_key
), &crt64
->bp_key
) ||
545 __put_user(compat_ptr(crt32k
.bq_key
), &crt64
->bq_key
) ||
546 __put_user(compat_ptr(crt32k
.np_prime
), &crt64
->np_prime
) ||
547 __put_user(compat_ptr(crt32k
.nq_prime
), &crt64
->nq_prime
) ||
548 __put_user(compat_ptr(crt32k
.u_mult_inv
), &crt64
->u_mult_inv
))
551 ret
= sys_ioctl(fd
, cmd
, (unsigned long)crt64
);
553 if (__get_user(i
, &crt64
->outputdatalength
) ||
554 __put_user(i
, &crt32u
->outputdatalength
))
559 static int compatible_ioctls
[] = {
560 ICAZ90STATUS
, Z90QUIESCE
, Z90STAT_TOTALCOUNT
, Z90STAT_PCICACOUNT
,
561 Z90STAT_PCICCCOUNT
, Z90STAT_PCIXCCCOUNT
, Z90STAT_PCIXCCMCL2COUNT
,
562 Z90STAT_PCIXCCMCL3COUNT
, Z90STAT_CEX2CCOUNT
, Z90STAT_REQUESTQ_COUNT
,
563 Z90STAT_PENDINGQ_COUNT
, Z90STAT_TOTALOPEN_COUNT
, Z90STAT_DOMAIN_INDEX
,
564 Z90STAT_STATUS_MASK
, Z90STAT_QDEPTH_MASK
, Z90STAT_PERDEV_REQCNT
,
567 static void z90_unregister_ioctl32s(void)
571 unregister_ioctl32_conversion(ICARSAMODEXPO
);
572 unregister_ioctl32_conversion(ICARSACRT
);
574 for(i
= 0; i
< ARRAY_SIZE(compatible_ioctls
); i
++)
575 unregister_ioctl32_conversion(compatible_ioctls
[i
]);
578 static int z90_register_ioctl32s(void)
582 result
= register_ioctl32_conversion(ICARSAMODEXPO
, trans_modexpo32
);
583 if (result
== -EBUSY
) {
584 unregister_ioctl32_conversion(ICARSAMODEXPO
);
585 result
= register_ioctl32_conversion(ICARSAMODEXPO
,
590 result
= register_ioctl32_conversion(ICARSACRT
, trans_modexpo_crt32
);
591 if (result
== -EBUSY
) {
592 unregister_ioctl32_conversion(ICARSACRT
);
593 result
= register_ioctl32_conversion(ICARSACRT
,
594 trans_modexpo_crt32
);
599 for(i
= 0; i
< ARRAY_SIZE(compatible_ioctls
); i
++) {
600 result
= register_ioctl32_conversion(compatible_ioctls
[i
], 0);
601 if (result
== -EBUSY
) {
602 unregister_ioctl32_conversion(compatible_ioctls
[i
]);
603 result
= register_ioctl32_conversion(
604 compatible_ioctls
[i
], 0);
611 #else // !CONFIG_COMPAT
612 static inline void z90_unregister_ioctl32s(void)
616 static inline int z90_register_ioctl32s(void)
623 * The module initialization code.
626 z90crypt_init_module(void)
629 struct proc_dir_entry
*entry
;
631 PDEBUG("PID %d\n", PID());
633 if ((domain
< -1) || (domain
> 15)) {
634 PRINTKW("Invalid param: domain = %d. Not loading.\n", domain
);
638 #ifndef Z90CRYPT_USE_HOTPLUG
639 /* Register as misc device with given minor (or get a dynamic one). */
640 result
= misc_register(&z90crypt_misc_device
);
642 PRINTKW(KERN_ERR
"misc_register (minor %d) failed with %d\n",
643 z90crypt_misc_device
.minor
, result
);
647 /* Register the major (or get a dynamic one). */
648 result
= register_chrdev(z90crypt_major
, REG_NAME
, &z90crypt_fops
);
650 PRINTKW("register_chrdev (major %d) failed with %d.\n",
651 z90crypt_major
, result
);
655 if (z90crypt_major
== 0)
656 z90crypt_major
= result
;
659 PDEBUG("Registered " DEV_NAME
" with result %d\n", result
);
661 result
= create_z90crypt(&domain
);
663 PRINTKW("create_z90crypt (domain index %d) failed with %d.\n",
666 goto init_module_cleanup
;
670 PRINTKN("Version %d.%d.%d loaded, built on %s %s\n",
671 z90crypt_VERSION
, z90crypt_RELEASE
, z90crypt_VARIANT
,
673 PRINTKN("%s\n", z90main_version
);
674 PRINTKN("%s\n", z90hardware_version
);
675 PDEBUG("create_z90crypt (domain index %d) successful.\n",
678 PRINTK("No devices at startup\n");
680 #ifdef Z90CRYPT_USE_HOTPLUG
681 /* generate hotplug event for device node generation */
682 z90crypt_hotplug_event(z90crypt_major
, 0, Z90CRYPT_HOTPLUG_ADD
);
685 /* Initialize globals. */
686 spin_lock_init(&queuespinlock
);
688 INIT_LIST_HEAD(&pending_list
);
691 INIT_LIST_HEAD(&request_list
);
694 quiesce_z90crypt
= 0;
696 atomic_set(&total_open
, 0);
697 atomic_set(&z90crypt_step
, 0);
699 /* Set up the cleanup task. */
700 init_timer(&cleanup_timer
);
701 cleanup_timer
.function
= z90crypt_cleanup_task
;
702 cleanup_timer
.data
= 0;
703 cleanup_timer
.expires
= jiffies
+ (CLEANUPTIME
* HZ
);
704 add_timer(&cleanup_timer
);
706 /* Set up the proc file system */
707 entry
= create_proc_entry("driver/z90crypt", 0644, 0);
711 entry
->read_proc
= z90crypt_status
;
712 entry
->write_proc
= z90crypt_status_write
;
715 PRINTK("Couldn't create z90crypt proc entry\n");
716 z90crypt_entry
= entry
;
718 /* Set up the configuration task. */
719 init_timer(&config_timer
);
720 config_timer
.function
= z90crypt_config_task
;
721 config_timer
.data
= 0;
722 config_timer
.expires
= jiffies
+ (INITIAL_CONFIGTIME
* HZ
);
723 add_timer(&config_timer
);
725 /* Set up the reader task */
726 tasklet_init(&reader_tasklet
, z90crypt_reader_task
, 0);
727 init_timer(&reader_timer
);
728 reader_timer
.function
= z90crypt_schedule_reader_task
;
729 reader_timer
.data
= 0;
730 reader_timer
.expires
= jiffies
+ (READERTIME
* HZ
/ 1000);
731 add_timer(&reader_timer
);
733 if ((result
= z90_register_ioctl32s()))
734 goto init_module_cleanup
;
739 z90_unregister_ioctl32s();
741 #ifndef Z90CRYPT_USE_HOTPLUG
742 if ((nresult
= misc_deregister(&z90crypt_misc_device
)))
743 PRINTK("misc_deregister failed with %d.\n", nresult
);
745 PDEBUG("misc_deregister successful.\n");
747 if ((nresult
= unregister_chrdev(z90crypt_major
, REG_NAME
)))
748 PRINTK("unregister_chrdev failed with %d.\n", nresult
);
750 PDEBUG("unregister_chrdev successful.\n");
753 return result
; // failure
757 * The module termination code
760 z90crypt_cleanup_module(void)
764 PDEBUG("PID %d\n", PID());
766 z90_unregister_ioctl32s();
768 remove_proc_entry("driver/z90crypt", 0);
770 #ifndef Z90CRYPT_USE_HOTPLUG
771 if ((nresult
= misc_deregister(&z90crypt_misc_device
)))
772 PRINTK("misc_deregister failed with %d.\n", nresult
);
774 PDEBUG("misc_deregister successful.\n");
776 z90crypt_hotplug_event(z90crypt_major
, 0, Z90CRYPT_HOTPLUG_REMOVE
);
778 if ((nresult
= unregister_chrdev(z90crypt_major
, REG_NAME
)))
779 PRINTK("unregister_chrdev failed with %d.\n", nresult
);
781 PDEBUG("unregister_chrdev successful.\n");
784 /* Remove the tasks */
785 tasklet_kill(&reader_tasklet
);
786 del_timer(&reader_timer
);
787 del_timer(&config_timer
);
788 del_timer(&cleanup_timer
);
792 PRINTKN("Unloaded.\n");
796 * Functions running under a process id
805 * z90crypt_status_write
815 * z90crypt_process_results
819 z90crypt_open(struct inode
*inode
, struct file
*filp
)
821 struct priv_data
*private_data_p
;
823 if (quiesce_z90crypt
)
826 private_data_p
= kmalloc(sizeof(struct priv_data
), GFP_KERNEL
);
827 if (!private_data_p
) {
828 PRINTK("Memory allocate failed\n");
832 memset((void *)private_data_p
, 0, sizeof(struct priv_data
));
833 private_data_p
->status
= STAT_OPEN
;
834 private_data_p
->opener_pid
= PID();
835 filp
->private_data
= private_data_p
;
836 atomic_inc(&total_open
);
842 z90crypt_release(struct inode
*inode
, struct file
*filp
)
844 struct priv_data
*private_data_p
= filp
->private_data
;
846 PDEBUG("PID %d (filp %p)\n", PID(), filp
);
848 private_data_p
->status
= STAT_CLOSED
;
849 memset(private_data_p
, 0, sizeof(struct priv_data
));
850 kfree(private_data_p
);
851 atomic_dec(&total_open
);
857 * there are two read functions, of which compile options will choose one
858 * without USE_GET_RANDOM_BYTES
859 * => read() always returns -EPERM;
861 * => read() uses get_random_bytes() kernel function
863 #ifndef USE_GET_RANDOM_BYTES
865 * z90crypt_read will not be supported beyond z90crypt 1.3.1
868 z90crypt_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*f_pos
)
870 PDEBUG("filp %p (PID %d)\n", filp
, PID());
873 #else // we want to use get_random_bytes
875 * read() just returns a string of random bytes. Since we have no way
876 * to generate these cryptographically, we just execute get_random_bytes
877 * for the length specified.
879 #include <linux/random.h>
881 z90crypt_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*f_pos
)
883 unsigned char *temp_buff
;
885 PDEBUG("filp %p (PID %d)\n", filp
, PID());
887 if (quiesce_z90crypt
)
890 PRINTK("Requested random byte count negative: %ld\n", count
);
893 if (count
> RESPBUFFSIZE
) {
894 PDEBUG("count[%d] > RESPBUFFSIZE", count
);
899 temp_buff
= kmalloc(RESPBUFFSIZE
, GFP_KERNEL
);
901 PRINTK("Memory allocate failed\n");
904 get_random_bytes(temp_buff
, count
);
906 if (copy_to_user(buf
, temp_buff
, count
) != 0) {
916 * Write is is not allowed
919 z90crypt_write(struct file
*filp
, const char __user
*buf
, size_t count
, loff_t
*f_pos
)
921 PDEBUG("filp %p (PID %d)\n", filp
, PID());
926 * New status functions
929 get_status_totalcount(void)
931 return z90crypt
.hdware_info
->hdware_mask
.st_count
;
935 get_status_PCICAcount(void)
937 return z90crypt
.hdware_info
->type_mask
[PCICA
].st_count
;
941 get_status_PCICCcount(void)
943 return z90crypt
.hdware_info
->type_mask
[PCICC
].st_count
;
947 get_status_PCIXCCcount(void)
949 return z90crypt
.hdware_info
->type_mask
[PCIXCC_MCL2
].st_count
+
950 z90crypt
.hdware_info
->type_mask
[PCIXCC_MCL3
].st_count
;
954 get_status_PCIXCCMCL2count(void)
956 return z90crypt
.hdware_info
->type_mask
[PCIXCC_MCL2
].st_count
;
960 get_status_PCIXCCMCL3count(void)
962 return z90crypt
.hdware_info
->type_mask
[PCIXCC_MCL3
].st_count
;
966 get_status_CEX2Ccount(void)
968 return z90crypt
.hdware_info
->type_mask
[CEX2C
].st_count
;
972 get_status_requestq_count(void)
974 return requestq_count
;
978 get_status_pendingq_count(void)
980 return pendingq_count
;
984 get_status_totalopen_count(void)
986 return atomic_read(&total_open
);
990 get_status_domain_index(void)
995 static inline unsigned char *
996 get_status_status_mask(unsigned char status
[Z90CRYPT_NUM_APS
])
1000 memcpy(status
, z90crypt
.hdware_info
->device_type_array
,
1003 for (i
= 0; i
< get_status_totalcount(); i
++) {
1005 if (LONG2DEVPTR(ix
)->user_disabled
)
1012 static inline unsigned char *
1013 get_status_qdepth_mask(unsigned char qdepth
[Z90CRYPT_NUM_APS
])
1017 memset(qdepth
, 0, Z90CRYPT_NUM_APS
);
1019 for (i
= 0; i
< get_status_totalcount(); i
++) {
1021 qdepth
[ix
] = LONG2DEVPTR(ix
)->dev_caller_count
;
1027 static inline unsigned int *
1028 get_status_perdevice_reqcnt(unsigned int reqcnt
[Z90CRYPT_NUM_APS
])
1032 memset(reqcnt
, 0, Z90CRYPT_NUM_APS
* sizeof(int));
1034 for (i
= 0; i
< get_status_totalcount(); i
++) {
1036 reqcnt
[ix
] = LONG2DEVPTR(ix
)->dev_total_req_cnt
;
1043 init_work_element(struct work_element
*we_p
,
1044 struct priv_data
*priv_data
, pid_t pid
)
1048 we_p
->requestptr
= (unsigned char *)we_p
+ sizeof(struct work_element
);
1049 /* Come up with a unique id for this caller. */
1050 step
= atomic_inc_return(&z90crypt_step
);
1051 memcpy(we_p
->caller_id
+0, (void *) &pid
, sizeof(pid
));
1052 memcpy(we_p
->caller_id
+4, (void *) &step
, sizeof(step
));
1054 we_p
->priv_data
= priv_data
;
1055 we_p
->status
[0] = STAT_DEFAULT
;
1056 we_p
->audit
[0] = 0x00;
1057 we_p
->audit
[1] = 0x00;
1058 we_p
->audit
[2] = 0x00;
1059 we_p
->resp_buff_size
= 0;
1061 we_p
->devindex
= -1;
1063 atomic_set(&we_p
->alarmrung
, 0);
1064 init_waitqueue_head(&we_p
->waitq
);
1065 INIT_LIST_HEAD(&(we_p
->liste
));
1069 allocate_work_element(struct work_element
**we_pp
,
1070 struct priv_data
*priv_data_p
, pid_t pid
)
1072 struct work_element
*we_p
;
1074 we_p
= (struct work_element
*) get_zeroed_page(GFP_KERNEL
);
1077 init_work_element(we_p
, priv_data_p
, pid
);
1083 remove_device(struct device
*device_p
)
1085 if (!device_p
|| (device_p
->disabled
!= 0))
1087 device_p
->disabled
= 1;
1088 z90crypt
.hdware_info
->type_mask
[device_p
->dev_type
].disabled_count
++;
1089 z90crypt
.hdware_info
->hdware_mask
.disabled_count
++;
1093 * Bitlength limits for each card
1095 * There are new MCLs which allow more bitlengths. See the table for details.
1096 * The MCL must be applied and the newer bitlengths enabled for these to work.
1098 * Card Type Old limit New limit
1099 * PCICC 512-1024 512-2048
1100 * PCIXCC_MCL2 512-2048 no change (applying this MCL == card is MCL3+)
1101 * PCIXCC_MCL3 512-2048 128-2048
1102 * CEX2C 512-2048 128-2048
1104 * ext_bitlens (extended bitlengths) is a global, since you should not apply an
1105 * MCL to just one card in a machine. We assume, at first, that all cards have
1106 * these capabilities.
1108 int ext_bitlens
= 1; // This is global
1109 #define PCIXCC_MIN_MOD_SIZE 16 // 128 bits
1110 #define OLD_PCIXCC_MIN_MOD_SIZE 64 // 512 bits
1111 #define PCICC_MIN_MOD_SIZE 64 // 512 bits
1112 #define OLD_PCICC_MAX_MOD_SIZE 128 // 1024 bits
1113 #define MAX_MOD_SIZE 256 // 2048 bits
1116 select_device_type(int *dev_type_p
, int bytelength
)
1118 static int count
= 0;
1119 int PCICA_avail
, PCIXCC_MCL3_avail
, CEX2C_avail
, index_to_use
;
1120 struct status
*stat
;
1121 if ((*dev_type_p
!= PCICC
) && (*dev_type_p
!= PCICA
) &&
1122 (*dev_type_p
!= PCIXCC_MCL2
) && (*dev_type_p
!= PCIXCC_MCL3
) &&
1123 (*dev_type_p
!= CEX2C
) && (*dev_type_p
!= ANYDEV
))
1125 if (*dev_type_p
!= ANYDEV
) {
1126 stat
= &z90crypt
.hdware_info
->type_mask
[*dev_type_p
];
1127 if (stat
->st_count
>
1128 (stat
->disabled_count
+ stat
->user_disabled_count
))
1133 /* Assumption: PCICA, PCIXCC_MCL3, and CEX2C are all similar in speed */
1134 stat
= &z90crypt
.hdware_info
->type_mask
[PCICA
];
1135 PCICA_avail
= stat
->st_count
-
1136 (stat
->disabled_count
+ stat
->user_disabled_count
);
1137 stat
= &z90crypt
.hdware_info
->type_mask
[PCIXCC_MCL3
];
1138 PCIXCC_MCL3_avail
= stat
->st_count
-
1139 (stat
->disabled_count
+ stat
->user_disabled_count
);
1140 stat
= &z90crypt
.hdware_info
->type_mask
[CEX2C
];
1141 CEX2C_avail
= stat
->st_count
-
1142 (stat
->disabled_count
+ stat
->user_disabled_count
);
1143 if (PCICA_avail
|| PCIXCC_MCL3_avail
|| CEX2C_avail
) {
1145 * bitlength is a factor, PCICA is the most capable, even with
1148 if ((bytelength
< PCIXCC_MIN_MOD_SIZE
) ||
1149 (!ext_bitlens
&& (bytelength
< OLD_PCIXCC_MIN_MOD_SIZE
))) {
1153 *dev_type_p
= PCICA
;
1158 index_to_use
= count
% (PCICA_avail
+ PCIXCC_MCL3_avail
+
1160 if (index_to_use
< PCICA_avail
)
1161 *dev_type_p
= PCICA
;
1162 else if (index_to_use
< (PCICA_avail
+ PCIXCC_MCL3_avail
))
1163 *dev_type_p
= PCIXCC_MCL3
;
1165 *dev_type_p
= CEX2C
;
1170 /* Less than OLD_PCIXCC_MIN_MOD_SIZE cannot go to a PCIXCC_MCL2 */
1171 if (bytelength
< OLD_PCIXCC_MIN_MOD_SIZE
)
1173 stat
= &z90crypt
.hdware_info
->type_mask
[PCIXCC_MCL2
];
1174 if (stat
->st_count
>
1175 (stat
->disabled_count
+ stat
->user_disabled_count
)) {
1176 *dev_type_p
= PCIXCC_MCL2
;
1181 * Less than PCICC_MIN_MOD_SIZE or more than OLD_PCICC_MAX_MOD_SIZE
1182 * (if we don't have the MCL applied and the newer bitlengths enabled)
1183 * cannot go to a PCICC
1185 if ((bytelength
< PCICC_MIN_MOD_SIZE
) ||
1186 (!ext_bitlens
&& (bytelength
> OLD_PCICC_MAX_MOD_SIZE
))) {
1189 stat
= &z90crypt
.hdware_info
->type_mask
[PCICC
];
1190 if (stat
->st_count
>
1191 (stat
->disabled_count
+ stat
->user_disabled_count
)) {
1192 *dev_type_p
= PCICC
;
1200 * Try the selected number, then the selected type (can be ANYDEV)
1203 select_device(int *dev_type_p
, int *device_nr_p
, int bytelength
)
1205 int i
, indx
, devTp
, low_count
, low_indx
;
1206 struct device_x
*index_p
;
1207 struct device
*dev_ptr
;
1209 PDEBUG("device type = %d, index = %d\n", *dev_type_p
, *device_nr_p
);
1210 if ((*device_nr_p
>= 0) && (*device_nr_p
< Z90CRYPT_NUM_DEVS
)) {
1211 PDEBUG("trying index = %d\n", *device_nr_p
);
1212 dev_ptr
= z90crypt
.device_p
[*device_nr_p
];
1215 (dev_ptr
->dev_stat
!= DEV_GONE
) &&
1216 (dev_ptr
->disabled
== 0) &&
1217 (dev_ptr
->user_disabled
== 0)) {
1218 PDEBUG("selected by number, index = %d\n",
1220 *dev_type_p
= dev_ptr
->dev_type
;
1221 return *device_nr_p
;
1225 PDEBUG("trying type = %d\n", *dev_type_p
);
1226 devTp
= *dev_type_p
;
1227 if (select_device_type(&devTp
, bytelength
) == -1) {
1228 PDEBUG("failed to select by type\n");
1231 PDEBUG("selected type = %d\n", devTp
);
1232 index_p
= &z90crypt
.hdware_info
->type_x_addr
[devTp
];
1233 low_count
= 0x0000FFFF;
1235 for (i
= 0; i
< z90crypt
.hdware_info
->type_mask
[devTp
].st_count
; i
++) {
1236 indx
= index_p
->device_index
[i
];
1237 dev_ptr
= z90crypt
.device_p
[indx
];
1239 (dev_ptr
->dev_stat
!= DEV_GONE
) &&
1240 (dev_ptr
->disabled
== 0) &&
1241 (dev_ptr
->user_disabled
== 0) &&
1242 (devTp
== dev_ptr
->dev_type
) &&
1243 (low_count
> dev_ptr
->dev_caller_count
)) {
1244 low_count
= dev_ptr
->dev_caller_count
;
1248 *device_nr_p
= low_indx
;
1253 send_to_crypto_device(struct work_element
*we_p
)
1255 struct caller
*caller_p
;
1256 struct device
*device_p
;
1258 int bytelen
= ((struct ica_rsa_modexpo
*)we_p
->buffer
)->inputdatalength
;
1260 if (!we_p
->requestptr
)
1261 return SEN_FATAL_ERROR
;
1262 caller_p
= (struct caller
*)we_p
->requestptr
;
1263 dev_nr
= we_p
->devindex
;
1264 if (select_device(&we_p
->devtype
, &dev_nr
, bytelen
) == -1) {
1265 if (z90crypt
.hdware_info
->hdware_mask
.st_count
!= 0)
1268 return SEN_NOT_AVAIL
;
1270 we_p
->devindex
= dev_nr
;
1271 device_p
= z90crypt
.device_p
[dev_nr
];
1273 return SEN_NOT_AVAIL
;
1274 if (device_p
->dev_type
!= we_p
->devtype
)
1276 if (device_p
->dev_caller_count
>= device_p
->dev_q_depth
)
1277 return SEN_QUEUE_FULL
;
1278 PDEBUG("device number prior to send: %d\n", dev_nr
);
1279 switch (send_to_AP(dev_nr
, z90crypt
.cdx
,
1280 caller_p
->caller_dev_dep_req_l
,
1281 caller_p
->caller_dev_dep_req_p
)) {
1282 case DEV_SEN_EXCEPTION
:
1283 PRINTKC("Exception during send to device %d\n", dev_nr
);
1284 z90crypt
.terminating
= 1;
1285 return SEN_FATAL_ERROR
;
1287 PRINTK("Device %d not available\n", dev_nr
);
1288 remove_device(device_p
);
1289 return SEN_NOT_AVAIL
;
1291 return SEN_NOT_AVAIL
;
1293 return SEN_FATAL_ERROR
;
1294 case DEV_BAD_MESSAGE
:
1295 return SEN_USER_ERROR
;
1296 case DEV_QUEUE_FULL
:
1297 return SEN_QUEUE_FULL
;
1302 list_add_tail(&(caller_p
->caller_liste
), &(device_p
->dev_caller_list
));
1303 device_p
->dev_caller_count
++;
1308 * Send puts the user's work on one of two queues:
1309 * the pending queue if the send was successful
1310 * the request queue if the send failed because device full or busy
1313 z90crypt_send(struct work_element
*we_p
, const char *buf
)
1317 PDEBUG("PID %d\n", PID());
1319 if (CHK_RDWRMASK(we_p
->status
[0]) != STAT_NOWORK
) {
1320 PDEBUG("PID %d tried to send more work but has outstanding "
1324 we_p
->devindex
= -1; // Reset device number
1325 spin_lock_irq(&queuespinlock
);
1326 rv
= send_to_crypto_device(we_p
);
1329 we_p
->requestsent
= jiffies
;
1330 we_p
->audit
[0] |= FP_SENT
;
1331 list_add_tail(&we_p
->liste
, &pending_list
);
1333 we_p
->audit
[0] |= FP_PENDING
;
1336 case SEN_QUEUE_FULL
:
1338 we_p
->devindex
= -1; // any device will do
1339 we_p
->requestsent
= jiffies
;
1340 list_add_tail(&we_p
->liste
, &request_list
);
1342 we_p
->audit
[0] |= FP_REQUEST
;
1348 PRINTK("*** No devices available.\n");
1349 rv
= we_p
->retcode
= -ENODEV
;
1350 we_p
->status
[0] |= STAT_FAILED
;
1352 case REC_OPERAND_INV
:
1353 case REC_OPERAND_SIZE
:
1355 case REC_INVALID_PAD
:
1356 rv
= we_p
->retcode
= -EINVAL
;
1357 we_p
->status
[0] |= STAT_FAILED
;
1361 we_p
->status
[0] |= STAT_FAILED
;
1364 if (rv
!= -ERESTARTSYS
)
1365 SET_RDWRMASK(we_p
->status
[0], STAT_WRITTEN
);
1366 spin_unlock_irq(&queuespinlock
);
1368 tasklet_schedule(&reader_tasklet
);
1373 * process_results copies the user's work from kernel space.
1376 z90crypt_process_results(struct work_element
*we_p
, char __user
*buf
)
1380 PDEBUG("we_p %p (PID %d)\n", we_p
, PID());
1382 LONG2DEVPTR(we_p
->devindex
)->dev_total_req_cnt
++;
1383 SET_RDWRMASK(we_p
->status
[0], STAT_READPEND
);
1386 if (!we_p
->buffer
) {
1387 PRINTK("we_p %p PID %d in STAT_READPEND: buffer NULL.\n",
1393 if ((rv
= copy_to_user(buf
, we_p
->buffer
, we_p
->buff_size
))) {
1394 PDEBUG("copy_to_user failed: rv = %d\n", rv
);
1401 if (we_p
->resp_buff_size
1402 && copy_to_user(we_p
->resp_addr
, we_p
->resp_buff
,
1403 we_p
->resp_buff_size
))
1406 SET_RDWRMASK(we_p
->status
[0], STAT_NOWORK
);
1410 static unsigned char NULL_psmid
[8] =
1411 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1414 * Used in device configuration functions
1416 #define MAX_RESET 90
1419 * This is used only for PCICC support
1422 is_PKCS11_padded(unsigned char *buffer
, int length
)
1425 if ((buffer
[0] != 0x00) || (buffer
[1] != 0x01))
1427 for (i
= 2; i
< length
; i
++)
1428 if (buffer
[i
] != 0xFF)
1430 if ((i
< 10) || (i
== length
))
1432 if (buffer
[i
] != 0x00)
1438 * This is used only for PCICC support
1441 is_PKCS12_padded(unsigned char *buffer
, int length
)
1444 if ((buffer
[0] != 0x00) || (buffer
[1] != 0x02))
1446 for (i
= 2; i
< length
; i
++)
1447 if (buffer
[i
] == 0x00)
1449 if ((i
< 10) || (i
== length
))
1451 if (buffer
[i
] != 0x00)
1457 * builds struct caller and converts message from generic format to
1458 * device-dependent format
1459 * func is ICARSAMODEXPO or ICARSACRT
1460 * function is PCI_FUNC_KEY_ENCRYPT or PCI_FUNC_KEY_DECRYPT
1463 build_caller(struct work_element
*we_p
, short function
)
1466 struct caller
*caller_p
= (struct caller
*)we_p
->requestptr
;
1468 if ((we_p
->devtype
!= PCICC
) && (we_p
->devtype
!= PCICA
) &&
1469 (we_p
->devtype
!= PCIXCC_MCL2
) && (we_p
->devtype
!= PCIXCC_MCL3
) &&
1470 (we_p
->devtype
!= CEX2C
))
1471 return SEN_NOT_AVAIL
;
1473 memcpy(caller_p
->caller_id
, we_p
->caller_id
,
1474 sizeof(caller_p
->caller_id
));
1475 caller_p
->caller_dev_dep_req_p
= caller_p
->caller_dev_dep_req
;
1476 caller_p
->caller_dev_dep_req_l
= MAX_RESPONSE_SIZE
;
1477 caller_p
->caller_buf_p
= we_p
->buffer
;
1478 INIT_LIST_HEAD(&(caller_p
->caller_liste
));
1480 rv
= convert_request(we_p
->buffer
, we_p
->funccode
, function
,
1481 z90crypt
.cdx
, we_p
->devtype
,
1482 &caller_p
->caller_dev_dep_req_l
,
1483 caller_p
->caller_dev_dep_req_p
);
1485 if (rv
== SEN_NOT_AVAIL
)
1486 PDEBUG("request can't be processed on hdwr avail\n");
1488 PRINTK("Error from convert_request: %d\n", rv
);
1491 memcpy(&(caller_p
->caller_dev_dep_req_p
[4]), we_p
->caller_id
,8);
1496 unbuild_caller(struct device
*device_p
, struct caller
*caller_p
)
1500 if (caller_p
->caller_liste
.next
&& caller_p
->caller_liste
.prev
)
1501 if (!list_empty(&caller_p
->caller_liste
)) {
1502 list_del_init(&caller_p
->caller_liste
);
1503 device_p
->dev_caller_count
--;
1505 memset(caller_p
->caller_id
, 0, sizeof(caller_p
->caller_id
));
1509 get_crypto_request_buffer(struct work_element
*we_p
)
1511 struct ica_rsa_modexpo
*mex_p
;
1512 struct ica_rsa_modexpo_crt
*crt_p
;
1513 unsigned char *temp_buffer
;
1517 mex_p
= (struct ica_rsa_modexpo
*) we_p
->buffer
;
1518 crt_p
= (struct ica_rsa_modexpo_crt
*) we_p
->buffer
;
1520 PDEBUG("device type input = %d\n", we_p
->devtype
);
1522 if (z90crypt
.terminating
)
1523 return REC_NO_RESPONSE
;
1524 if (memcmp(we_p
->caller_id
, NULL_psmid
, 8) == 0) {
1525 PRINTK("psmid zeroes\n");
1526 return SEN_FATAL_ERROR
;
1528 if (!we_p
->buffer
) {
1529 PRINTK("buffer pointer NULL\n");
1530 return SEN_USER_ERROR
;
1532 if (!we_p
->requestptr
) {
1533 PRINTK("caller pointer NULL\n");
1534 return SEN_USER_ERROR
;
1537 if ((we_p
->devtype
!= PCICA
) && (we_p
->devtype
!= PCICC
) &&
1538 (we_p
->devtype
!= PCIXCC_MCL2
) && (we_p
->devtype
!= PCIXCC_MCL3
) &&
1539 (we_p
->devtype
!= CEX2C
) && (we_p
->devtype
!= ANYDEV
)) {
1540 PRINTK("invalid device type\n");
1541 return SEN_USER_ERROR
;
1544 if ((mex_p
->inputdatalength
< 1) ||
1545 (mex_p
->inputdatalength
> MAX_MOD_SIZE
)) {
1546 PRINTK("inputdatalength[%d] is not valid\n",
1547 mex_p
->inputdatalength
);
1548 return SEN_USER_ERROR
;
1551 if (mex_p
->outputdatalength
< mex_p
->inputdatalength
) {
1552 PRINTK("outputdatalength[%d] < inputdatalength[%d]\n",
1553 mex_p
->outputdatalength
, mex_p
->inputdatalength
);
1554 return SEN_USER_ERROR
;
1557 if (!mex_p
->inputdata
|| !mex_p
->outputdata
) {
1558 PRINTK("inputdata[%p] or outputdata[%p] is NULL\n",
1559 mex_p
->outputdata
, mex_p
->inputdata
);
1560 return SEN_USER_ERROR
;
1564 * As long as outputdatalength is big enough, we can set the
1565 * outputdatalength equal to the inputdatalength, since that is the
1566 * number of bytes we will copy in any case
1568 mex_p
->outputdatalength
= mex_p
->inputdatalength
;
1571 switch (we_p
->funccode
) {
1573 if (!mex_p
->b_key
|| !mex_p
->n_modulus
)
1574 rv
= SEN_USER_ERROR
;
1577 if (!IS_EVEN(crt_p
->inputdatalength
)) {
1578 PRINTK("inputdatalength[%d] is odd, CRT form\n",
1579 crt_p
->inputdatalength
);
1580 rv
= SEN_USER_ERROR
;
1583 if (!crt_p
->bp_key
||
1587 !crt_p
->u_mult_inv
) {
1588 PRINTK("CRT form, bad data: %p/%p/%p/%p/%p\n",
1589 crt_p
->bp_key
, crt_p
->bq_key
,
1590 crt_p
->np_prime
, crt_p
->nq_prime
,
1592 rv
= SEN_USER_ERROR
;
1596 PRINTK("bad func = %d\n", we_p
->funccode
);
1597 rv
= SEN_USER_ERROR
;
1603 if (select_device_type(&we_p
->devtype
, mex_p
->inputdatalength
) < 0)
1604 return SEN_NOT_AVAIL
;
1606 temp_buffer
= (unsigned char *)we_p
+ sizeof(struct work_element
) +
1607 sizeof(struct caller
);
1608 if (copy_from_user(temp_buffer
, mex_p
->inputdata
,
1609 mex_p
->inputdatalength
) != 0)
1610 return SEN_RELEASED
;
1612 function
= PCI_FUNC_KEY_ENCRYPT
;
1613 switch (we_p
->devtype
) {
1614 /* PCICA does everything with a simple RSA mod-expo operation */
1616 function
= PCI_FUNC_KEY_ENCRYPT
;
1619 * PCIXCC_MCL2 does all Mod-Expo form with a simple RSA mod-expo
1620 * operation, and all CRT forms with a PKCS-1.2 format decrypt.
1621 * PCIXCC_MCL3 and CEX2C do all Mod-Expo and CRT forms with a simple RSA
1622 * mod-expo operation
1625 if (we_p
->funccode
== ICARSAMODEXPO
)
1626 function
= PCI_FUNC_KEY_ENCRYPT
;
1628 function
= PCI_FUNC_KEY_DECRYPT
;
1632 if (we_p
->funccode
== ICARSAMODEXPO
)
1633 function
= PCI_FUNC_KEY_ENCRYPT
;
1635 function
= PCI_FUNC_KEY_DECRYPT
;
1638 * PCICC does everything as a PKCS-1.2 format request
1641 /* PCICC cannot handle input that is is PKCS#1.1 padded */
1642 if (is_PKCS11_padded(temp_buffer
, mex_p
->inputdatalength
)) {
1643 return SEN_NOT_AVAIL
;
1645 if (we_p
->funccode
== ICARSAMODEXPO
) {
1646 if (is_PKCS12_padded(temp_buffer
,
1647 mex_p
->inputdatalength
))
1648 function
= PCI_FUNC_KEY_ENCRYPT
;
1650 function
= PCI_FUNC_KEY_DECRYPT
;
1652 /* all CRT forms are decrypts */
1653 function
= PCI_FUNC_KEY_DECRYPT
;
1656 PDEBUG("function: %04x\n", function
);
1657 rv
= build_caller(we_p
, function
);
1658 PDEBUG("rv from build_caller = %d\n", rv
);
1663 z90crypt_prepare(struct work_element
*we_p
, unsigned int funccode
,
1664 const char __user
*buffer
)
1668 we_p
->devindex
= -1;
1669 if (funccode
== ICARSAMODEXPO
)
1670 we_p
->buff_size
= sizeof(struct ica_rsa_modexpo
);
1672 we_p
->buff_size
= sizeof(struct ica_rsa_modexpo_crt
);
1674 if (copy_from_user(we_p
->buffer
, buffer
, we_p
->buff_size
))
1677 we_p
->audit
[0] |= FP_COPYFROM
;
1678 SET_RDWRMASK(we_p
->status
[0], STAT_WRITTEN
);
1679 we_p
->funccode
= funccode
;
1681 we_p
->audit
[0] |= FP_BUFFREQ
;
1682 rv
= get_crypto_request_buffer(we_p
);
1685 we_p
->audit
[0] |= FP_BUFFGOT
;
1687 case SEN_USER_ERROR
:
1690 case SEN_QUEUE_FULL
:
1696 case REC_NO_RESPONSE
:
1704 PRINTK("rv = %d\n", rv
);
1708 if (CHK_RDWRMASK(we_p
->status
[0]) == STAT_WRITTEN
)
1709 SET_RDWRMASK(we_p
->status
[0], STAT_DEFAULT
);
1714 purge_work_element(struct work_element
*we_p
)
1716 struct list_head
*lptr
;
1718 spin_lock_irq(&queuespinlock
);
1719 list_for_each(lptr
, &request_list
) {
1720 if (lptr
== &we_p
->liste
) {
1721 list_del_init(lptr
);
1726 list_for_each(lptr
, &pending_list
) {
1727 if (lptr
== &we_p
->liste
) {
1728 list_del_init(lptr
);
1733 spin_unlock_irq(&queuespinlock
);
1737 * Build the request and send it.
1740 z90crypt_rsa(struct priv_data
*private_data_p
, pid_t pid
,
1741 unsigned int cmd
, unsigned long arg
)
1743 struct work_element
*we_p
;
1746 if ((rv
= allocate_work_element(&we_p
, private_data_p
, pid
))) {
1747 PDEBUG("PID %d: allocate_work_element returned ENOMEM\n", pid
);
1750 if ((rv
= z90crypt_prepare(we_p
, cmd
, (const char __user
*)arg
)))
1751 PDEBUG("PID %d: rv = %d from z90crypt_prepare\n", pid
, rv
);
1753 if ((rv
= z90crypt_send(we_p
, (const char *)arg
)))
1754 PDEBUG("PID %d: rv %d from z90crypt_send.\n", pid
, rv
);
1756 we_p
->audit
[0] |= FP_ASLEEP
;
1757 wait_event(we_p
->waitq
, atomic_read(&we_p
->alarmrung
));
1758 we_p
->audit
[0] |= FP_AWAKE
;
1762 rv
= z90crypt_process_results(we_p
, (char __user
*)arg
);
1764 if ((we_p
->status
[0] & STAT_FAILED
)) {
1767 * EINVAL *after* receive is almost always a padding error or
1768 * length error issued by a coprocessor (not an accelerator).
1769 * We convert this return value to -EGETBUFF which should
1770 * trigger a fallback to software.
1773 if (we_p
->devtype
!= PCICA
)
1777 if (z90crypt
.mask
.st_count
> 0)
1778 rv
= -ERESTARTSYS
; // retry with another
1780 rv
= -ENODEV
; // no cards left
1781 /* fall through to clean up request queue */
1784 switch (CHK_RDWRMASK(we_p
->status
[0])) {
1786 purge_work_element(we_p
);
1795 we_p
->status
[0] ^= STAT_FAILED
;
1799 free_page((long)we_p
);
1804 * This function is a little long, but it's really just one large switch
1808 z90crypt_ioctl(struct inode
*inode
, struct file
*filp
,
1809 unsigned int cmd
, unsigned long arg
)
1811 struct priv_data
*private_data_p
= filp
->private_data
;
1812 unsigned char *status
;
1813 unsigned char *qdepth
;
1814 unsigned int *reqcnt
;
1815 struct ica_z90_status
*pstat
;
1816 int ret
, i
, loopLim
, tempstat
;
1817 static int deprecated_msg_count1
= 0;
1818 static int deprecated_msg_count2
= 0;
1820 PDEBUG("filp %p (PID %d), cmd 0x%08X\n", filp
, PID(), cmd
);
1821 PDEBUG("cmd 0x%08X: dir %s, size 0x%04X, type 0x%02X, nr 0x%02X\n",
1823 !_IOC_DIR(cmd
) ? "NO"
1824 : ((_IOC_DIR(cmd
) == (_IOC_READ
|_IOC_WRITE
)) ? "RW"
1825 : ((_IOC_DIR(cmd
) == _IOC_READ
) ? "RD"
1827 _IOC_SIZE(cmd
), _IOC_TYPE(cmd
), _IOC_NR(cmd
));
1829 if (_IOC_TYPE(cmd
) != Z90_IOCTL_MAGIC
) {
1830 PRINTK("cmd 0x%08X contains bad magic\n", cmd
);
1838 if (quiesce_z90crypt
) {
1842 ret
= -ENODEV
; // Default if no devices
1843 loopLim
= z90crypt
.hdware_info
->hdware_mask
.st_count
-
1844 (z90crypt
.hdware_info
->hdware_mask
.disabled_count
+
1845 z90crypt
.hdware_info
->hdware_mask
.user_disabled_count
);
1846 for (i
= 0; i
< loopLim
; i
++) {
1847 ret
= z90crypt_rsa(private_data_p
, PID(), cmd
, arg
);
1848 if (ret
!= -ERESTARTSYS
)
1851 if (ret
== -ERESTARTSYS
)
1855 case Z90STAT_TOTALCOUNT
:
1856 tempstat
= get_status_totalcount();
1857 if (copy_to_user((int __user
*)arg
, &tempstat
,sizeof(int)) != 0)
1861 case Z90STAT_PCICACOUNT
:
1862 tempstat
= get_status_PCICAcount();
1863 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1867 case Z90STAT_PCICCCOUNT
:
1868 tempstat
= get_status_PCICCcount();
1869 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1873 case Z90STAT_PCIXCCMCL2COUNT
:
1874 tempstat
= get_status_PCIXCCMCL2count();
1875 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1879 case Z90STAT_PCIXCCMCL3COUNT
:
1880 tempstat
= get_status_PCIXCCMCL3count();
1881 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1885 case Z90STAT_CEX2CCOUNT
:
1886 tempstat
= get_status_CEX2Ccount();
1887 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1891 case Z90STAT_REQUESTQ_COUNT
:
1892 tempstat
= get_status_requestq_count();
1893 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1897 case Z90STAT_PENDINGQ_COUNT
:
1898 tempstat
= get_status_pendingq_count();
1899 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1903 case Z90STAT_TOTALOPEN_COUNT
:
1904 tempstat
= get_status_totalopen_count();
1905 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1909 case Z90STAT_DOMAIN_INDEX
:
1910 tempstat
= get_status_domain_index();
1911 if (copy_to_user((int __user
*)arg
, &tempstat
, sizeof(int)) != 0)
1915 case Z90STAT_STATUS_MASK
:
1916 status
= kmalloc(Z90CRYPT_NUM_APS
, GFP_KERNEL
);
1918 PRINTK("kmalloc for status failed!\n");
1922 get_status_status_mask(status
);
1923 if (copy_to_user((char __user
*) arg
, status
, Z90CRYPT_NUM_APS
)
1929 case Z90STAT_QDEPTH_MASK
:
1930 qdepth
= kmalloc(Z90CRYPT_NUM_APS
, GFP_KERNEL
);
1932 PRINTK("kmalloc for qdepth failed!\n");
1936 get_status_qdepth_mask(qdepth
);
1937 if (copy_to_user((char __user
*) arg
, qdepth
, Z90CRYPT_NUM_APS
) != 0)
1942 case Z90STAT_PERDEV_REQCNT
:
1943 reqcnt
= kmalloc(sizeof(int) * Z90CRYPT_NUM_APS
, GFP_KERNEL
);
1945 PRINTK("kmalloc for reqcnt failed!\n");
1949 get_status_perdevice_reqcnt(reqcnt
);
1950 if (copy_to_user((char __user
*) arg
, reqcnt
,
1951 Z90CRYPT_NUM_APS
* sizeof(int)) != 0)
1956 /* THIS IS DEPRECATED. USE THE NEW STATUS CALLS */
1958 if (deprecated_msg_count1
< 20) {
1959 PRINTK("deprecated call to ioctl (ICAZ90STATUS)!\n");
1960 deprecated_msg_count1
++;
1961 if (deprecated_msg_count1
== 20)
1962 PRINTK("No longer issuing messages related to "
1963 "deprecated call to ICAZ90STATUS.\n");
1966 pstat
= kmalloc(sizeof(struct ica_z90_status
), GFP_KERNEL
);
1968 PRINTK("kmalloc for pstat failed!\n");
1973 pstat
->totalcount
= get_status_totalcount();
1974 pstat
->leedslitecount
= get_status_PCICAcount();
1975 pstat
->leeds2count
= get_status_PCICCcount();
1976 pstat
->requestqWaitCount
= get_status_requestq_count();
1977 pstat
->pendingqWaitCount
= get_status_pendingq_count();
1978 pstat
->totalOpenCount
= get_status_totalopen_count();
1979 pstat
->cryptoDomain
= get_status_domain_index();
1980 get_status_status_mask(pstat
->status
);
1981 get_status_qdepth_mask(pstat
->qdepth
);
1983 if (copy_to_user((struct ica_z90_status __user
*) arg
, pstat
,
1984 sizeof(struct ica_z90_status
)) != 0)
1989 /* THIS IS DEPRECATED. USE THE NEW STATUS CALLS */
1990 case Z90STAT_PCIXCCCOUNT
:
1991 if (deprecated_msg_count2
< 20) {
1992 PRINTK("deprecated ioctl (Z90STAT_PCIXCCCOUNT)!\n");
1993 deprecated_msg_count2
++;
1994 if (deprecated_msg_count2
== 20)
1995 PRINTK("No longer issuing messages about depre"
1996 "cated ioctl Z90STAT_PCIXCCCOUNT.\n");
1999 tempstat
= get_status_PCIXCCcount();
2000 if (copy_to_user((int *)arg
, &tempstat
, sizeof(int)) != 0)
2005 if (current
->euid
!= 0) {
2006 PRINTK("QUIESCE fails: euid %d\n",
2010 PRINTK("QUIESCE device from PID %d\n", PID());
2011 quiesce_z90crypt
= 1;
2016 /* user passed an invalid IOCTL number */
2017 PDEBUG("cmd 0x%08X contains invalid ioctl code\n", cmd
);
2026 sprintcl(unsigned char *outaddr
, unsigned char *addr
, unsigned int len
)
2031 for (i
= 0; i
< len
; i
++)
2032 hl
+= sprintf(outaddr
+hl
, "%01x", (unsigned int) addr
[i
]);
2033 hl
+= sprintf(outaddr
+hl
, " ");
2039 sprintrw(unsigned char *outaddr
, unsigned char *addr
, unsigned int len
)
2043 hl
= sprintf(outaddr
, " ");
2045 for (c
= 0; c
< (len
/ 16); c
++) {
2046 hl
+= sprintcl(outaddr
+hl
, addr
+inl
, 16);
2052 hl
+= sprintcl(outaddr
+hl
, addr
+inl
, cx
);
2056 hl
+= sprintf(outaddr
+hl
, "\n");
2062 sprinthx(unsigned char *title
, unsigned char *outaddr
,
2063 unsigned char *addr
, unsigned int len
)
2067 hl
= sprintf(outaddr
, "\n%s\n", title
);
2069 for (r
= 0; r
< (len
/ 64); r
++) {
2070 hl
+= sprintrw(outaddr
+hl
, addr
+inl
, 64);
2075 hl
+= sprintrw(outaddr
+hl
, addr
+inl
, rx
);
2079 hl
+= sprintf(outaddr
+hl
, "\n");
2085 sprinthx4(unsigned char *title
, unsigned char *outaddr
,
2086 unsigned int *array
, unsigned int len
)
2090 hl
= sprintf(outaddr
, "\n%s\n", title
);
2092 for (r
= 0; r
< len
; r
++) {
2094 hl
+= sprintf(outaddr
+hl
, " ");
2095 hl
+= sprintf(outaddr
+hl
, "%08X ", array
[r
]);
2097 hl
+= sprintf(outaddr
+hl
, "\n");
2100 hl
+= sprintf(outaddr
+hl
, "\n");
2106 z90crypt_status(char *resp_buff
, char **start
, off_t offset
,
2107 int count
, int *eof
, void *data
)
2109 unsigned char *workarea
;
2112 /* resp_buff is a page. Use the right half for a work area */
2113 workarea
= resp_buff
+2000;
2115 len
+= sprintf(resp_buff
+len
, "\nz90crypt version: %d.%d.%d\n",
2116 z90crypt_VERSION
, z90crypt_RELEASE
, z90crypt_VARIANT
);
2117 len
+= sprintf(resp_buff
+len
, "Cryptographic domain: %d\n",
2118 get_status_domain_index());
2119 len
+= sprintf(resp_buff
+len
, "Total device count: %d\n",
2120 get_status_totalcount());
2121 len
+= sprintf(resp_buff
+len
, "PCICA count: %d\n",
2122 get_status_PCICAcount());
2123 len
+= sprintf(resp_buff
+len
, "PCICC count: %d\n",
2124 get_status_PCICCcount());
2125 len
+= sprintf(resp_buff
+len
, "PCIXCC MCL2 count: %d\n",
2126 get_status_PCIXCCMCL2count());
2127 len
+= sprintf(resp_buff
+len
, "PCIXCC MCL3 count: %d\n",
2128 get_status_PCIXCCMCL3count());
2129 len
+= sprintf(resp_buff
+len
, "CEX2C count: %d\n",
2130 get_status_CEX2Ccount());
2131 len
+= sprintf(resp_buff
+len
, "requestq count: %d\n",
2132 get_status_requestq_count());
2133 len
+= sprintf(resp_buff
+len
, "pendingq count: %d\n",
2134 get_status_pendingq_count());
2135 len
+= sprintf(resp_buff
+len
, "Total open handles: %d\n\n",
2136 get_status_totalopen_count());
2138 "Online devices: 1: PCICA, 2: PCICC, 3: PCIXCC (MCL2), "
2139 "4: PCIXCC (MCL3), 5: CEX2C",
2141 get_status_status_mask(workarea
),
2143 len
+= sprinthx("Waiting work element counts",
2145 get_status_qdepth_mask(workarea
),
2148 "Per-device successfully completed request counts",
2150 get_status_perdevice_reqcnt((unsigned int *)workarea
),
2153 memset(workarea
, 0, Z90CRYPT_NUM_APS
* sizeof(unsigned int));
2158 disable_card(int card_index
)
2160 struct device
*devp
;
2162 devp
= LONG2DEVPTR(card_index
);
2163 if (!devp
|| devp
->user_disabled
)
2165 devp
->user_disabled
= 1;
2166 z90crypt
.hdware_info
->hdware_mask
.user_disabled_count
++;
2167 if (devp
->dev_type
== -1)
2169 z90crypt
.hdware_info
->type_mask
[devp
->dev_type
].user_disabled_count
++;
2173 enable_card(int card_index
)
2175 struct device
*devp
;
2177 devp
= LONG2DEVPTR(card_index
);
2178 if (!devp
|| !devp
->user_disabled
)
2180 devp
->user_disabled
= 0;
2181 z90crypt
.hdware_info
->hdware_mask
.user_disabled_count
--;
2182 if (devp
->dev_type
== -1)
2184 z90crypt
.hdware_info
->type_mask
[devp
->dev_type
].user_disabled_count
--;
2188 scan_char(unsigned char *bf
, unsigned int len
,
2189 unsigned int *offs
, unsigned int *p_eof
, unsigned char c
)
2191 unsigned int i
, found
;
2194 for (i
= 0; i
< len
; i
++) {
2199 if (bf
[i
] == '\0') {
2203 if (bf
[i
] == '\n') {
2212 scan_string(unsigned char *bf
, unsigned int len
,
2213 unsigned int *offs
, unsigned int *p_eof
, unsigned char *s
)
2215 unsigned int temp_len
, temp_offs
, found
, eof
;
2217 temp_len
= temp_offs
= found
= eof
= 0;
2218 while (!eof
&& !found
) {
2219 found
= scan_char(bf
+temp_len
, len
-temp_len
,
2220 &temp_offs
, &eof
, *s
);
2222 temp_len
+= temp_offs
;
2229 if (len
>= temp_offs
+strlen(s
)) {
2230 found
= !strncmp(bf
+temp_len
-1, s
, strlen(s
));
2232 *offs
= temp_len
+strlen(s
)-1;
2246 z90crypt_status_write(struct file
*file
, const char __user
*buffer
,
2247 unsigned long count
, void *data
)
2249 int i
, j
, len
, offs
, found
, eof
;
2250 unsigned char *lbuf
;
2251 unsigned int local_count
;
2253 #define LBUFSIZE 600
2254 lbuf
= kmalloc(LBUFSIZE
, GFP_KERNEL
);
2256 PRINTK("kmalloc failed!\n");
2263 local_count
= UMIN((unsigned int)count
, LBUFSIZE
-1);
2265 if (copy_from_user(lbuf
, buffer
, local_count
) != 0) {
2270 lbuf
[local_count
-1] = '\0';
2276 found
= scan_string(lbuf
+len
, local_count
-len
, &offs
, &eof
,
2289 found
= scan_char(lbuf
+len
, local_count
-len
, &offs
, &eof
, '\n');
2291 if (!found
|| eof
) {
2298 for (i
= 0; i
< 80; i
++) {
2299 switch (*(lbuf
+len
+i
)) {
2335 * Functions that run under a timer, with no process id
2337 * The task functions:
2338 * z90crypt_reader_task
2340 * helper_handle_work_element
2342 * z90crypt_config_task
2343 * z90crypt_cleanup_task
2346 * z90crypt_schedule_reader_timer
2347 * z90crypt_schedule_reader_task
2348 * z90crypt_schedule_config_task
2349 * z90crypt_schedule_cleanup_task
2352 receive_from_crypto_device(int index
, unsigned char *psmid
, int *buff_len_p
,
2353 unsigned char *buff
, unsigned char __user
**dest_p_p
)
2356 struct device
*dev_ptr
;
2357 struct caller
*caller_p
;
2358 struct ica_rsa_modexpo
*icaMsg_p
;
2359 struct list_head
*ptr
, *tptr
;
2361 memcpy(psmid
, NULL_psmid
, sizeof(NULL_psmid
));
2363 if (z90crypt
.terminating
)
2364 return REC_FATAL_ERROR
;
2367 dev_ptr
= z90crypt
.device_p
[index
];
2370 if (!dev_ptr
|| dev_ptr
->disabled
) {
2371 rv
= REC_NO_WORK
; // a disabled device can't return work
2374 if (dev_ptr
->dev_self_x
!= index
) {
2375 PRINTKC("Corrupt dev ptr\n");
2376 z90crypt
.terminating
= 1;
2377 rv
= REC_FATAL_ERROR
;
2380 if (!dev_ptr
->dev_resp_l
|| !dev_ptr
->dev_resp_p
) {
2381 dv
= DEV_REC_EXCEPTION
;
2382 PRINTK("dev_resp_l = %d, dev_resp_p = %p\n",
2383 dev_ptr
->dev_resp_l
, dev_ptr
->dev_resp_p
);
2385 PDEBUG("Dequeue called for device %d\n", index
);
2386 dv
= receive_from_AP(index
, z90crypt
.cdx
,
2387 dev_ptr
->dev_resp_l
,
2388 dev_ptr
->dev_resp_p
, psmid
);
2391 case DEV_REC_EXCEPTION
:
2392 rv
= REC_FATAL_ERROR
;
2393 z90crypt
.terminating
= 1;
2394 PRINTKC("Exception in receive from device %d\n",
2406 case DEV_BAD_MESSAGE
:
2408 case REC_HARDWAR_ERR
:
2410 rv
= REC_NO_RESPONSE
;
2415 if (dev_ptr
->dev_caller_count
<= 0) {
2420 list_for_each_safe(ptr
, tptr
, &dev_ptr
->dev_caller_list
) {
2421 caller_p
= list_entry(ptr
, struct caller
, caller_liste
);
2422 if (!memcmp(caller_p
->caller_id
, psmid
,
2423 sizeof(caller_p
->caller_id
))) {
2424 if (!list_empty(&caller_p
->caller_liste
)) {
2426 dev_ptr
->dev_caller_count
--;
2433 PRINTKW("Unable to locate PSMID %02X%02X%02X%02X%02X"
2434 "%02X%02X%02X in device list\n",
2435 psmid
[0], psmid
[1], psmid
[2], psmid
[3],
2436 psmid
[4], psmid
[5], psmid
[6], psmid
[7]);
2441 PDEBUG("caller_p after successful receive: %p\n", caller_p
);
2442 rv
= convert_response(dev_ptr
->dev_resp_p
,
2443 caller_p
->caller_buf_p
, buff_len_p
, buff
);
2447 case REC_OPERAND_INV
:
2448 case REC_OPERAND_SIZE
:
2450 case REC_INVALID_PAD
:
2451 PDEBUG("device %d: 'user error' %d\n", index
, rv
);
2453 case WRONG_DEVICE_TYPE
:
2454 case REC_HARDWAR_ERR
:
2455 case REC_BAD_MESSAGE
:
2456 PRINTKW("device %d: hardware error %d\n", index
, rv
);
2457 rv
= REC_NO_RESPONSE
;
2460 PDEBUG("device %d: rv = %d\n", index
, rv
);
2467 PDEBUG("Successful receive from device %d\n", index
);
2468 icaMsg_p
= (struct ica_rsa_modexpo
*)caller_p
->caller_buf_p
;
2469 *dest_p_p
= icaMsg_p
->outputdata
;
2470 if (*buff_len_p
== 0)
2471 PRINTK("Zero *buff_len_p\n");
2473 case REC_NO_RESPONSE
:
2474 PRINTKW("Removing device %d from availability\n", index
);
2475 remove_device(dev_ptr
);
2480 unbuild_caller(dev_ptr
, caller_p
);
2486 helper_send_work(int index
)
2488 struct work_element
*rq_p
;
2491 if (list_empty(&request_list
))
2494 rq_p
= list_entry(request_list
.next
, struct work_element
, liste
);
2495 list_del_init(&rq_p
->liste
);
2496 rq_p
->audit
[1] |= FP_REMREQUEST
;
2497 if (rq_p
->devtype
== SHRT2DEVPTR(index
)->dev_type
) {
2498 rq_p
->devindex
= SHRT2LONG(index
);
2499 rv
= send_to_crypto_device(rq_p
);
2501 rq_p
->requestsent
= jiffies
;
2502 rq_p
->audit
[0] |= FP_SENT
;
2503 list_add_tail(&rq_p
->liste
, &pending_list
);
2505 rq_p
->audit
[0] |= FP_PENDING
;
2508 case REC_OPERAND_INV
:
2509 case REC_OPERAND_SIZE
:
2511 case REC_INVALID_PAD
:
2512 rq_p
->retcode
= -EINVAL
;
2516 case REC_NO_RESPONSE
:
2518 if (z90crypt
.mask
.st_count
> 1)
2522 rq_p
->retcode
= -ENODEV
;
2525 rq_p
->status
[0] |= STAT_FAILED
;
2526 rq_p
->audit
[1] |= FP_AWAKENING
;
2527 atomic_set(&rq_p
->alarmrung
, 1);
2528 wake_up(&rq_p
->waitq
);
2531 if (z90crypt
.mask
.st_count
> 1)
2532 rq_p
->retcode
= -ERESTARTSYS
;
2534 rq_p
->retcode
= -ENODEV
;
2535 rq_p
->status
[0] |= STAT_FAILED
;
2536 rq_p
->audit
[1] |= FP_AWAKENING
;
2537 atomic_set(&rq_p
->alarmrung
, 1);
2538 wake_up(&rq_p
->waitq
);
2543 helper_handle_work_element(int index
, unsigned char psmid
[8], int rc
,
2544 int buff_len
, unsigned char *buff
,
2545 unsigned char __user
*resp_addr
)
2547 struct work_element
*pq_p
;
2548 struct list_head
*lptr
, *tptr
;
2551 list_for_each_safe(lptr
, tptr
, &pending_list
) {
2552 pq_p
= list_entry(lptr
, struct work_element
, liste
);
2553 if (!memcmp(pq_p
->caller_id
, psmid
, sizeof(pq_p
->caller_id
))) {
2554 list_del_init(lptr
);
2556 pq_p
->audit
[1] |= FP_NOTPENDING
;
2563 PRINTK("device %d has work but no caller exists on pending Q\n",
2570 pq_p
->resp_buff_size
= buff_len
;
2571 pq_p
->audit
[1] |= FP_RESPSIZESET
;
2573 pq_p
->resp_addr
= resp_addr
;
2574 pq_p
->audit
[1] |= FP_RESPADDRCOPIED
;
2575 memcpy(pq_p
->resp_buff
, buff
, buff_len
);
2576 pq_p
->audit
[1] |= FP_RESPBUFFCOPIED
;
2579 case REC_OPERAND_INV
:
2580 case REC_OPERAND_SIZE
:
2582 case REC_INVALID_PAD
:
2583 PDEBUG("-EINVAL after application error %d\n", rc
);
2584 pq_p
->retcode
= -EINVAL
;
2585 pq_p
->status
[0] |= STAT_FAILED
;
2588 pq_p
->retcode
= -ERESTARTSYS
;
2589 pq_p
->status
[0] |= STAT_FAILED
;
2591 case REC_NO_RESPONSE
:
2593 if (z90crypt
.mask
.st_count
> 1)
2594 pq_p
->retcode
= -ERESTARTSYS
;
2596 pq_p
->retcode
= -ENODEV
;
2597 pq_p
->status
[0] |= STAT_FAILED
;
2600 if ((pq_p
->status
[0] != STAT_FAILED
) || (pq_p
->retcode
!= -ERELEASED
)) {
2601 pq_p
->audit
[1] |= FP_AWAKENING
;
2602 atomic_set(&pq_p
->alarmrung
, 1);
2603 wake_up(&pq_p
->waitq
);
2608 * return TRUE if the work element should be removed from the queue
2611 helper_receive_rc(int index
, int *rc_p
)
2615 case REC_OPERAND_INV
:
2616 case REC_OPERAND_SIZE
:
2618 case REC_INVALID_PAD
:
2626 case REC_FATAL_ERROR
:
2629 case REC_NO_RESPONSE
:
2633 PRINTK("rc %d, device %d converted to REC_NO_RESPONSE\n",
2634 *rc_p
, SHRT2LONG(index
));
2635 *rc_p
= REC_NO_RESPONSE
;
2642 z90crypt_schedule_reader_timer(void)
2644 if (timer_pending(&reader_timer
))
2646 if (mod_timer(&reader_timer
, jiffies
+(READERTIME
*HZ
/1000)) != 0)
2647 PRINTK("Timer pending while modifying reader timer\n");
2651 z90crypt_reader_task(unsigned long ptr
)
2653 int workavail
, index
, rc
, buff_len
;
2654 unsigned char psmid
[8];
2655 unsigned char __user
*resp_addr
;
2656 static unsigned char buff
[1024];
2659 * we use workavail = 2 to ensure 2 passes with nothing dequeued before
2660 * exiting the loop. If (pendingq_count+requestq_count) == 0 after the
2661 * loop, there is no work remaining on the queues.
2669 spin_lock_irq(&queuespinlock
);
2670 memset(buff
, 0x00, sizeof(buff
));
2672 /* Dequeue once from each device in round robin. */
2673 for (index
= 0; index
< z90crypt
.mask
.st_count
; index
++) {
2674 PDEBUG("About to receive.\n");
2675 rc
= receive_from_crypto_device(SHRT2LONG(index
),
2680 PDEBUG("Dequeued: rc = %d.\n", rc
);
2682 if (helper_receive_rc(index
, &rc
)) {
2683 if (rc
!= REC_NO_RESPONSE
) {
2684 helper_send_work(index
);
2688 helper_handle_work_element(index
, psmid
, rc
,
2693 if (rc
== REC_FATAL_ERROR
)
2694 PRINTKW("REC_FATAL_ERROR from device %d!\n",
2697 spin_unlock_irq(&queuespinlock
);
2700 if (pendingq_count
+ requestq_count
)
2701 z90crypt_schedule_reader_timer();
2705 z90crypt_schedule_config_task(unsigned int expiration
)
2707 if (timer_pending(&config_timer
))
2709 if (mod_timer(&config_timer
, jiffies
+(expiration
*HZ
)) != 0)
2710 PRINTK("Timer pending while modifying config timer\n");
2714 z90crypt_config_task(unsigned long ptr
)
2718 PDEBUG("jiffies %ld\n", jiffies
);
2720 if ((rc
= refresh_z90crypt(&z90crypt
.cdx
)))
2721 PRINTK("Error %d detected in refresh_z90crypt.\n", rc
);
2722 /* If return was fatal, don't bother reconfiguring */
2723 if ((rc
!= TSQ_FATAL_ERROR
) && (rc
!= RSQ_FATAL_ERROR
))
2724 z90crypt_schedule_config_task(CONFIGTIME
);
2728 z90crypt_schedule_cleanup_task(void)
2730 if (timer_pending(&cleanup_timer
))
2732 if (mod_timer(&cleanup_timer
, jiffies
+(CLEANUPTIME
*HZ
)) != 0)
2733 PRINTK("Timer pending while modifying cleanup timer\n");
2737 helper_drain_queues(void)
2739 struct work_element
*pq_p
;
2740 struct list_head
*lptr
, *tptr
;
2742 list_for_each_safe(lptr
, tptr
, &pending_list
) {
2743 pq_p
= list_entry(lptr
, struct work_element
, liste
);
2744 pq_p
->retcode
= -ENODEV
;
2745 pq_p
->status
[0] |= STAT_FAILED
;
2746 unbuild_caller(LONG2DEVPTR(pq_p
->devindex
),
2747 (struct caller
*)pq_p
->requestptr
);
2748 list_del_init(lptr
);
2750 pq_p
->audit
[1] |= FP_NOTPENDING
;
2751 pq_p
->audit
[1] |= FP_AWAKENING
;
2752 atomic_set(&pq_p
->alarmrung
, 1);
2753 wake_up(&pq_p
->waitq
);
2756 list_for_each_safe(lptr
, tptr
, &request_list
) {
2757 pq_p
= list_entry(lptr
, struct work_element
, liste
);
2758 pq_p
->retcode
= -ENODEV
;
2759 pq_p
->status
[0] |= STAT_FAILED
;
2760 list_del_init(lptr
);
2762 pq_p
->audit
[1] |= FP_REMREQUEST
;
2763 pq_p
->audit
[1] |= FP_AWAKENING
;
2764 atomic_set(&pq_p
->alarmrung
, 1);
2765 wake_up(&pq_p
->waitq
);
2770 helper_timeout_requests(void)
2772 struct work_element
*pq_p
;
2773 struct list_head
*lptr
, *tptr
;
2776 timelimit
= jiffies
- (CLEANUPTIME
* HZ
);
2777 /* The list is in strict chronological order */
2778 list_for_each_safe(lptr
, tptr
, &pending_list
) {
2779 pq_p
= list_entry(lptr
, struct work_element
, liste
);
2780 if (pq_p
->requestsent
>= timelimit
)
2782 PRINTKW("Purging(PQ) PSMID %02X%02X%02X%02X%02X%02X%02X%02X\n",
2783 ((struct caller
*)pq_p
->requestptr
)->caller_id
[0],
2784 ((struct caller
*)pq_p
->requestptr
)->caller_id
[1],
2785 ((struct caller
*)pq_p
->requestptr
)->caller_id
[2],
2786 ((struct caller
*)pq_p
->requestptr
)->caller_id
[3],
2787 ((struct caller
*)pq_p
->requestptr
)->caller_id
[4],
2788 ((struct caller
*)pq_p
->requestptr
)->caller_id
[5],
2789 ((struct caller
*)pq_p
->requestptr
)->caller_id
[6],
2790 ((struct caller
*)pq_p
->requestptr
)->caller_id
[7]);
2791 pq_p
->retcode
= -ETIMEOUT
;
2792 pq_p
->status
[0] |= STAT_FAILED
;
2793 /* get this off any caller queue it may be on */
2794 unbuild_caller(LONG2DEVPTR(pq_p
->devindex
),
2795 (struct caller
*) pq_p
->requestptr
);
2796 list_del_init(lptr
);
2798 pq_p
->audit
[1] |= FP_TIMEDOUT
;
2799 pq_p
->audit
[1] |= FP_NOTPENDING
;
2800 pq_p
->audit
[1] |= FP_AWAKENING
;
2801 atomic_set(&pq_p
->alarmrung
, 1);
2802 wake_up(&pq_p
->waitq
);
2806 * If pending count is zero, items left on the request queue may
2807 * never be processed.
2809 if (pendingq_count
<= 0) {
2810 list_for_each_safe(lptr
, tptr
, &request_list
) {
2811 pq_p
= list_entry(lptr
, struct work_element
, liste
);
2812 if (pq_p
->requestsent
>= timelimit
)
2814 PRINTKW("Purging(RQ) PSMID %02X%02X%02X%02X%02X%02X%02X%02X\n",
2815 ((struct caller
*)pq_p
->requestptr
)->caller_id
[0],
2816 ((struct caller
*)pq_p
->requestptr
)->caller_id
[1],
2817 ((struct caller
*)pq_p
->requestptr
)->caller_id
[2],
2818 ((struct caller
*)pq_p
->requestptr
)->caller_id
[3],
2819 ((struct caller
*)pq_p
->requestptr
)->caller_id
[4],
2820 ((struct caller
*)pq_p
->requestptr
)->caller_id
[5],
2821 ((struct caller
*)pq_p
->requestptr
)->caller_id
[6],
2822 ((struct caller
*)pq_p
->requestptr
)->caller_id
[7]);
2823 pq_p
->retcode
= -ETIMEOUT
;
2824 pq_p
->status
[0] |= STAT_FAILED
;
2825 list_del_init(lptr
);
2827 pq_p
->audit
[1] |= FP_TIMEDOUT
;
2828 pq_p
->audit
[1] |= FP_REMREQUEST
;
2829 pq_p
->audit
[1] |= FP_AWAKENING
;
2830 atomic_set(&pq_p
->alarmrung
, 1);
2831 wake_up(&pq_p
->waitq
);
2837 z90crypt_cleanup_task(unsigned long ptr
)
2839 PDEBUG("jiffies %ld\n", jiffies
);
2840 spin_lock_irq(&queuespinlock
);
2841 if (z90crypt
.mask
.st_count
<= 0) // no devices!
2842 helper_drain_queues();
2844 helper_timeout_requests();
2845 spin_unlock_irq(&queuespinlock
);
2846 z90crypt_schedule_cleanup_task();
2850 z90crypt_schedule_reader_task(unsigned long ptr
)
2852 tasklet_schedule(&reader_tasklet
);
2856 * Lowlevel Functions:
2858 * create_z90crypt: creates and initializes basic data structures
2859 * refresh_z90crypt: re-initializes basic data structures
2860 * find_crypto_devices: returns a count and mask of hardware status
2861 * create_crypto_device: builds the descriptor for a device
2862 * destroy_crypto_device: unallocates the descriptor for a device
2863 * destroy_z90crypt: drains all work, unallocates structs
2867 * build the z90crypt root structure using the given domain index
2870 create_z90crypt(int *cdx_p
)
2872 struct hdware_block
*hdware_blk_p
;
2874 memset(&z90crypt
, 0x00, sizeof(struct z90crypt
));
2875 z90crypt
.domain_established
= 0;
2876 z90crypt
.len
= sizeof(struct z90crypt
);
2877 z90crypt
.max_count
= Z90CRYPT_NUM_DEVS
;
2878 z90crypt
.cdx
= *cdx_p
;
2880 hdware_blk_p
= (struct hdware_block
*)
2881 kmalloc(sizeof(struct hdware_block
), GFP_ATOMIC
);
2882 if (!hdware_blk_p
) {
2883 PDEBUG("kmalloc for hardware block failed\n");
2886 memset(hdware_blk_p
, 0x00, sizeof(struct hdware_block
));
2887 z90crypt
.hdware_info
= hdware_blk_p
;
2893 helper_scan_devices(int cdx_array
[16], int *cdx_p
, int *correct_cdx_found
)
2895 enum hdstat hd_stat
;
2896 int q_depth
, dev_type
;
2897 int indx
, chkdom
, numdomains
;
2899 q_depth
= dev_type
= numdomains
= 0;
2900 for (chkdom
= 0; chkdom
<= 15; cdx_array
[chkdom
++] = -1);
2901 for (indx
= 0; indx
< z90crypt
.max_count
; indx
++) {
2902 hd_stat
= HD_NOT_THERE
;
2904 for (chkdom
= 0; chkdom
<= 15; chkdom
++) {
2905 hd_stat
= query_online(indx
, chkdom
, MAX_RESET
,
2906 &q_depth
, &dev_type
);
2907 if (hd_stat
== HD_TSQ_EXCEPTION
) {
2908 z90crypt
.terminating
= 1;
2909 PRINTKC("exception taken!\n");
2912 if (hd_stat
== HD_ONLINE
) {
2913 cdx_array
[numdomains
++] = chkdom
;
2914 if (*cdx_p
== chkdom
) {
2915 *correct_cdx_found
= 1;
2920 if ((*correct_cdx_found
== 1) || (numdomains
!= 0))
2922 if (z90crypt
.terminating
)
2929 probe_crypto_domain(int *cdx_p
)
2932 char cdx_array_text
[53], temp
[5];
2933 int correct_cdx_found
, numdomains
;
2935 correct_cdx_found
= 0;
2936 numdomains
= helper_scan_devices(cdx_array
, cdx_p
, &correct_cdx_found
);
2938 if (z90crypt
.terminating
)
2939 return TSQ_FATAL_ERROR
;
2941 if (correct_cdx_found
)
2944 if (numdomains
== 0) {
2945 PRINTKW("Unable to find crypto domain: No devices found\n");
2946 return Z90C_NO_DEVICES
;
2949 if (numdomains
== 1) {
2951 *cdx_p
= cdx_array
[0];
2954 PRINTKW("incorrect domain: specified = %d, found = %d\n",
2955 *cdx_p
, cdx_array
[0]);
2956 return Z90C_INCORRECT_DOMAIN
;
2960 sprintf(cdx_array_text
, "%d", cdx_array
[numdomains
]);
2961 while (numdomains
) {
2963 sprintf(temp
, ", %d", cdx_array
[numdomains
]);
2964 strcat(cdx_array_text
, temp
);
2967 PRINTKW("ambiguous domain detected: specified = %d, found array = %s\n",
2968 *cdx_p
, cdx_array_text
);
2969 return Z90C_AMBIGUOUS_DOMAIN
;
2973 refresh_z90crypt(int *cdx_p
)
2976 static struct status local_mask
;
2977 struct device
*devPtr
;
2978 unsigned char oldStat
, newStat
;
2979 int return_unchanged
;
2981 if (z90crypt
.len
!= sizeof(z90crypt
))
2983 if (z90crypt
.terminating
)
2984 return TSQ_FATAL_ERROR
;
2986 if (!z90crypt
.hdware_info
->hdware_mask
.st_count
&&
2987 !z90crypt
.domain_established
) {
2988 rv
= probe_crypto_domain(cdx_p
);
2989 if (z90crypt
.terminating
)
2990 return TSQ_FATAL_ERROR
;
2991 if (rv
== Z90C_NO_DEVICES
)
2992 return 0; // try later
2995 z90crypt
.cdx
= *cdx_p
;
2996 z90crypt
.domain_established
= 1;
2998 rv
= find_crypto_devices(&local_mask
);
3000 PRINTK("find crypto devices returned %d\n", rv
);
3003 if (!memcmp(&local_mask
, &z90crypt
.hdware_info
->hdware_mask
,
3004 sizeof(struct status
))) {
3005 return_unchanged
= 1;
3006 for (i
= 0; i
< Z90CRYPT_NUM_TYPES
; i
++) {
3008 * Check for disabled cards. If any device is marked
3009 * disabled, destroy it.
3012 j
< z90crypt
.hdware_info
->type_mask
[i
].st_count
;
3014 indx
= z90crypt
.hdware_info
->type_x_addr
[i
].
3016 devPtr
= z90crypt
.device_p
[indx
];
3017 if (devPtr
&& devPtr
->disabled
) {
3018 local_mask
.st_mask
[indx
] = HD_NOT_THERE
;
3019 return_unchanged
= 0;
3023 if (return_unchanged
== 1)
3027 spin_lock_irq(&queuespinlock
);
3028 for (i
= 0; i
< z90crypt
.max_count
; i
++) {
3029 oldStat
= z90crypt
.hdware_info
->hdware_mask
.st_mask
[i
];
3030 newStat
= local_mask
.st_mask
[i
];
3031 if ((oldStat
== HD_ONLINE
) && (newStat
!= HD_ONLINE
))
3032 destroy_crypto_device(i
);
3033 else if ((oldStat
!= HD_ONLINE
) && (newStat
== HD_ONLINE
)) {
3034 rv
= create_crypto_device(i
);
3035 if (rv
>= REC_FATAL_ERROR
)
3038 local_mask
.st_mask
[i
] = HD_NOT_THERE
;
3039 local_mask
.st_count
--;
3043 memcpy(z90crypt
.hdware_info
->hdware_mask
.st_mask
, local_mask
.st_mask
,
3044 sizeof(local_mask
.st_mask
));
3045 z90crypt
.hdware_info
->hdware_mask
.st_count
= local_mask
.st_count
;
3046 z90crypt
.hdware_info
->hdware_mask
.disabled_count
=
3047 local_mask
.disabled_count
;
3048 refresh_index_array(&z90crypt
.mask
, &z90crypt
.overall_device_x
);
3049 for (i
= 0; i
< Z90CRYPT_NUM_TYPES
; i
++)
3050 refresh_index_array(&(z90crypt
.hdware_info
->type_mask
[i
]),
3051 &(z90crypt
.hdware_info
->type_x_addr
[i
]));
3052 spin_unlock_irq(&queuespinlock
);
3058 find_crypto_devices(struct status
*deviceMask
)
3060 int i
, q_depth
, dev_type
;
3061 enum hdstat hd_stat
;
3063 deviceMask
->st_count
= 0;
3064 deviceMask
->disabled_count
= 0;
3065 deviceMask
->user_disabled_count
= 0;
3067 for (i
= 0; i
< z90crypt
.max_count
; i
++) {
3068 hd_stat
= query_online(i
, z90crypt
.cdx
, MAX_RESET
, &q_depth
,
3070 if (hd_stat
== HD_TSQ_EXCEPTION
) {
3071 z90crypt
.terminating
= 1;
3072 PRINTKC("Exception during probe for crypto devices\n");
3073 return TSQ_FATAL_ERROR
;
3075 deviceMask
->st_mask
[i
] = hd_stat
;
3076 if (hd_stat
== HD_ONLINE
) {
3077 PDEBUG("Got an online crypto!: %d\n", i
);
3078 PDEBUG("Got a queue depth of %d\n", q_depth
);
3079 PDEBUG("Got a device type of %d\n", dev_type
);
3081 return TSQ_FATAL_ERROR
;
3082 deviceMask
->st_count
++;
3083 z90crypt
.q_depth_array
[i
] = q_depth
;
3084 z90crypt
.dev_type_array
[i
] = dev_type
;
3092 refresh_index_array(struct status
*status_str
, struct device_x
*index_array
)
3100 stat
= status_str
->st_mask
[++i
];
3101 if (stat
== DEV_ONLINE
)
3102 index_array
->device_index
[count
++] = i
;
3103 } while ((i
< Z90CRYPT_NUM_DEVS
) && (count
< status_str
->st_count
));
3109 create_crypto_device(int index
)
3111 int rv
, devstat
, total_size
;
3112 struct device
*dev_ptr
;
3113 struct status
*type_str_p
;
3116 dev_ptr
= z90crypt
.device_p
[index
];
3118 total_size
= sizeof(struct device
) +
3119 z90crypt
.q_depth_array
[index
] * sizeof(int);
3121 dev_ptr
= (struct device
*) kmalloc(total_size
, GFP_ATOMIC
);
3123 PRINTK("kmalloc device %d failed\n", index
);
3126 memset(dev_ptr
, 0, total_size
);
3127 dev_ptr
->dev_resp_p
= kmalloc(MAX_RESPONSE_SIZE
, GFP_ATOMIC
);
3128 if (!dev_ptr
->dev_resp_p
) {
3130 PRINTK("kmalloc device %d rec buffer failed\n", index
);
3133 dev_ptr
->dev_resp_l
= MAX_RESPONSE_SIZE
;
3134 INIT_LIST_HEAD(&(dev_ptr
->dev_caller_list
));
3137 devstat
= reset_device(index
, z90crypt
.cdx
, MAX_RESET
);
3138 if (devstat
== DEV_RSQ_EXCEPTION
) {
3139 PRINTK("exception during reset device %d\n", index
);
3140 kfree(dev_ptr
->dev_resp_p
);
3142 return RSQ_FATAL_ERROR
;
3144 if (devstat
== DEV_ONLINE
) {
3145 dev_ptr
->dev_self_x
= index
;
3146 dev_ptr
->dev_type
= z90crypt
.dev_type_array
[index
];
3147 if (dev_ptr
->dev_type
== NILDEV
) {
3148 rv
= probe_device_type(dev_ptr
);
3150 PRINTK("rv = %d from probe_device_type %d\n",
3152 kfree(dev_ptr
->dev_resp_p
);
3157 if (dev_ptr
->dev_type
== PCIXCC_UNK
) {
3158 rv
= probe_PCIXCC_type(dev_ptr
);
3160 PRINTK("rv = %d from probe_PCIXCC_type %d\n",
3162 kfree(dev_ptr
->dev_resp_p
);
3167 deviceType
= dev_ptr
->dev_type
;
3168 z90crypt
.dev_type_array
[index
] = deviceType
;
3169 if (deviceType
== PCICA
)
3170 z90crypt
.hdware_info
->device_type_array
[index
] = 1;
3171 else if (deviceType
== PCICC
)
3172 z90crypt
.hdware_info
->device_type_array
[index
] = 2;
3173 else if (deviceType
== PCIXCC_MCL2
)
3174 z90crypt
.hdware_info
->device_type_array
[index
] = 3;
3175 else if (deviceType
== PCIXCC_MCL3
)
3176 z90crypt
.hdware_info
->device_type_array
[index
] = 4;
3177 else if (deviceType
== CEX2C
)
3178 z90crypt
.hdware_info
->device_type_array
[index
] = 5;
3180 z90crypt
.hdware_info
->device_type_array
[index
] = -1;
3184 * 'q_depth' returned by the hardware is one less than
3187 dev_ptr
->dev_q_depth
= z90crypt
.q_depth_array
[index
];
3188 dev_ptr
->dev_type
= z90crypt
.dev_type_array
[index
];
3189 dev_ptr
->dev_stat
= devstat
;
3190 dev_ptr
->disabled
= 0;
3191 z90crypt
.device_p
[index
] = dev_ptr
;
3193 if (devstat
== DEV_ONLINE
) {
3194 if (z90crypt
.mask
.st_mask
[index
] != DEV_ONLINE
) {
3195 z90crypt
.mask
.st_mask
[index
] = DEV_ONLINE
;
3196 z90crypt
.mask
.st_count
++;
3198 deviceType
= dev_ptr
->dev_type
;
3199 type_str_p
= &z90crypt
.hdware_info
->type_mask
[deviceType
];
3200 if (type_str_p
->st_mask
[index
] != DEV_ONLINE
) {
3201 type_str_p
->st_mask
[index
] = DEV_ONLINE
;
3202 type_str_p
->st_count
++;
3210 destroy_crypto_device(int index
)
3212 struct device
*dev_ptr
;
3213 int t
, disabledFlag
;
3215 dev_ptr
= z90crypt
.device_p
[index
];
3217 /* remember device type; get rid of device struct */
3219 disabledFlag
= dev_ptr
->disabled
;
3220 t
= dev_ptr
->dev_type
;
3221 if (dev_ptr
->dev_resp_p
)
3222 kfree(dev_ptr
->dev_resp_p
);
3228 z90crypt
.device_p
[index
] = 0;
3230 /* if the type is valid, remove the device from the type_mask */
3231 if ((t
!= -1) && z90crypt
.hdware_info
->type_mask
[t
].st_mask
[index
]) {
3232 z90crypt
.hdware_info
->type_mask
[t
].st_mask
[index
] = 0x00;
3233 z90crypt
.hdware_info
->type_mask
[t
].st_count
--;
3234 if (disabledFlag
== 1)
3235 z90crypt
.hdware_info
->type_mask
[t
].disabled_count
--;
3237 if (z90crypt
.mask
.st_mask
[index
] != DEV_GONE
) {
3238 z90crypt
.mask
.st_mask
[index
] = DEV_GONE
;
3239 z90crypt
.mask
.st_count
--;
3241 z90crypt
.hdware_info
->device_type_array
[index
] = 0;
3247 destroy_z90crypt(void)
3250 for (i
= 0; i
< z90crypt
.max_count
; i
++)
3251 if (z90crypt
.device_p
[i
])
3252 destroy_crypto_device(i
);
3253 if (z90crypt
.hdware_info
)
3254 kfree((void *)z90crypt
.hdware_info
);
3255 memset((void *)&z90crypt
, 0, sizeof(z90crypt
));
3258 static unsigned char static_testmsg
[384] = {
3259 0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x00,0x06,0x00,0x00,
3260 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x58,
3261 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x43,0x43,
3262 0x41,0x2d,0x41,0x50,0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,0x00,0x00,0x00,0x00,
3263 0x50,0x4b,0x00,0x00,0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3264 0x00,0x00,0x00,0x00,0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3265 0x00,0x00,0x00,0x00,0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x54,0x32,
3266 0x01,0x00,0xa0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3267 0xb8,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3268 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3269 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3270 0x00,0x00,0x00,0x00,0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3271 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x49,0x43,0x53,0x46,
3272 0x20,0x20,0x20,0x20,0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,0x2d,0x31,0x2e,0x32,
3273 0x37,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
3274 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
3275 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
3276 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,0x88,0x1e,0x00,0x00,
3277 0x57,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,0x03,0x02,0x00,0x00,
3278 0x40,0x01,0x00,0x01,0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,0xf6,0xd2,0x7b,0x58,
3279 0x4b,0xf9,0x28,0x68,0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,0x63,0x42,0xef,0xf8,
3280 0xfd,0xa4,0xf8,0xb0,0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,0x53,0x8c,0x6f,0x4e,
3281 0x72,0x8f,0x6c,0x04,0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,0xf7,0xdd,0xfd,0x4f,
3282 0x11,0x36,0x95,0x5d,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
3286 probe_device_type(struct device
*devPtr
)
3288 int rv
, dv
, i
, index
, length
;
3289 unsigned char psmid
[8];
3290 static unsigned char loc_testmsg
[sizeof(static_testmsg
)];
3292 index
= devPtr
->dev_self_x
;
3295 memcpy(loc_testmsg
, static_testmsg
, sizeof(static_testmsg
));
3296 length
= sizeof(static_testmsg
) - 24;
3297 /* the -24 allows for the header */
3298 dv
= send_to_AP(index
, z90crypt
.cdx
, length
, loc_testmsg
);
3300 PDEBUG("dv returned by send during probe: %d\n", dv
);
3301 if (dv
== DEV_SEN_EXCEPTION
) {
3302 rv
= SEN_FATAL_ERROR
;
3303 PRINTKC("exception in send to AP %d\n", index
);
3306 PDEBUG("return value from send_to_AP: %d\n", rv
);
3309 PDEBUG("dev %d not available\n", index
);
3319 rv
= SEN_FATAL_ERROR
;
3321 case DEV_BAD_MESSAGE
:
3322 rv
= SEN_USER_ERROR
;
3324 case DEV_QUEUE_FULL
:
3325 rv
= SEN_QUEUE_FULL
;
3328 PRINTK("unknown dv=%d for dev %d\n", dv
, index
);
3337 for (i
= 0; i
< 6; i
++) {
3339 dv
= receive_from_AP(index
, z90crypt
.cdx
,
3341 devPtr
->dev_resp_p
, psmid
);
3342 PDEBUG("dv returned by DQ = %d\n", dv
);
3343 if (dv
== DEV_REC_EXCEPTION
) {
3344 rv
= REC_FATAL_ERROR
;
3345 PRINTKC("exception in dequeue %d\n",
3359 case DEV_BAD_MESSAGE
:
3362 rv
= REC_NO_RESPONSE
;
3365 if ((rv
!= 0) && (rv
!= REC_NO_WORK
))
3372 rv
= (devPtr
->dev_resp_p
[0] == 0x00) &&
3373 (devPtr
->dev_resp_p
[1] == 0x86);
3375 devPtr
->dev_type
= PCICC
;
3377 devPtr
->dev_type
= PCICA
;
3380 /* In a general error case, the card is not marked online */
3384 static unsigned char MCL3_testmsg
[] = {
3385 0x00,0x00,0x00,0x00,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,
3386 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3387 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3388 0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3389 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x00,0x00,0x00,0x01,0xC4,0x00,0x00,0x00,0x00,
3390 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,0x00,0x00,0x00,0x00,
3391 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xDC,0x02,0x00,0x00,0x00,0x54,0x32,
3392 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE8,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,
3393 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3394 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3395 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3396 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3397 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3398 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3399 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3400 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3401 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3402 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3403 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3404 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
3405 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x0A,0x4D,0x52,0x50,0x20,0x20,0x20,0x20,0x20,
3406 0x00,0x42,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
3407 0x0E,0x0F,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,
3408 0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,
3409 0x11,0x00,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0xFE,0xDC,0xBA,0x98,0x76,0x54,
3410 0x32,0x10,0x00,0x9A,0x00,0x98,0x00,0x00,0x1E,0x00,0x00,0x94,0x00,0x00,0x00,0x00,
3411 0x04,0x00,0x00,0x8C,0x00,0x00,0x00,0x40,0x02,0x00,0x00,0x40,0xBA,0xE8,0x23,0x3C,
3412 0x75,0xF3,0x91,0x61,0xD6,0x73,0x39,0xCF,0x7B,0x6D,0x8E,0x61,0x97,0x63,0x9E,0xD9,
3413 0x60,0x55,0xD6,0xC7,0xEF,0xF8,0x1E,0x63,0x95,0x17,0xCC,0x28,0x45,0x60,0x11,0xC5,
3414 0xC4,0x4E,0x66,0xC6,0xE6,0xC3,0xDE,0x8A,0x19,0x30,0xCF,0x0E,0xD7,0xAA,0xDB,0x01,
3415 0xD8,0x00,0xBB,0x8F,0x39,0x9F,0x64,0x28,0xF5,0x7A,0x77,0x49,0xCC,0x6B,0xA3,0x91,
3416 0x97,0x70,0xE7,0x60,0x1E,0x39,0xE1,0xE5,0x33,0xE1,0x15,0x63,0x69,0x08,0x80,0x4C,
3417 0x67,0xC4,0x41,0x8F,0x48,0xDF,0x26,0x98,0xF1,0xD5,0x8D,0x88,0xD9,0x6A,0xA4,0x96,
3418 0xC5,0x84,0xD9,0x30,0x49,0x67,0x7D,0x19,0xB1,0xB3,0x45,0x4D,0xB2,0x53,0x9A,0x47,
3419 0x3C,0x7C,0x55,0xBF,0xCC,0x85,0x00,0x36,0xF1,0x3D,0x93,0x53
3423 probe_PCIXCC_type(struct device
*devPtr
)
3425 int rv
, dv
, i
, index
, length
;
3426 unsigned char psmid
[8];
3427 static unsigned char loc_testmsg
[548];
3428 struct CPRBX
*cprbx_p
;
3430 index
= devPtr
->dev_self_x
;
3433 memcpy(loc_testmsg
, MCL3_testmsg
, sizeof(MCL3_testmsg
));
3434 length
= sizeof(MCL3_testmsg
) - 0x0C;
3435 dv
= send_to_AP(index
, z90crypt
.cdx
, length
, loc_testmsg
);
3437 PDEBUG("dv returned = %d\n", dv
);
3438 if (dv
== DEV_SEN_EXCEPTION
) {
3439 rv
= SEN_FATAL_ERROR
;
3440 PRINTKC("exception in send to AP %d\n", index
);
3443 PDEBUG("return value from send_to_AP: %d\n", rv
);
3446 PDEBUG("dev %d not available\n", index
);
3456 rv
= SEN_FATAL_ERROR
;
3458 case DEV_BAD_MESSAGE
:
3459 rv
= SEN_USER_ERROR
;
3461 case DEV_QUEUE_FULL
:
3462 rv
= SEN_QUEUE_FULL
;
3465 PRINTK("unknown dv=%d for dev %d\n", dv
, index
);
3474 for (i
= 0; i
< 6; i
++) {
3476 dv
= receive_from_AP(index
, z90crypt
.cdx
,
3478 devPtr
->dev_resp_p
, psmid
);
3479 PDEBUG("dv returned by DQ = %d\n", dv
);
3480 if (dv
== DEV_REC_EXCEPTION
) {
3481 rv
= REC_FATAL_ERROR
;
3482 PRINTKC("exception in dequeue %d\n",
3496 case DEV_BAD_MESSAGE
:
3499 rv
= REC_NO_RESPONSE
;
3502 if ((rv
!= 0) && (rv
!= REC_NO_WORK
))
3509 cprbx_p
= (struct CPRBX
*) (devPtr
->dev_resp_p
+ 48);
3510 if ((cprbx_p
->ccp_rtcode
== 8) && (cprbx_p
->ccp_rscode
== 33)) {
3511 devPtr
->dev_type
= PCIXCC_MCL2
;
3512 PDEBUG("device %d is MCL2\n", index
);
3514 devPtr
->dev_type
= PCIXCC_MCL3
;
3515 PDEBUG("device %d is MCL3\n", index
);
3518 /* In a general error case, the card is not marked online */
3522 #ifdef Z90CRYPT_USE_HOTPLUG
3524 z90crypt_hotplug_event(int dev_major
, int dev_minor
, int action
)
3526 #ifdef CONFIG_HOTPLUG
3532 sprintf(major
, "MAJOR=%d", dev_major
);
3533 sprintf(minor
, "MINOR=%d", dev_minor
);
3535 argv
[0] = hotplug_path
;
3536 argv
[1] = "z90crypt";
3540 envp
[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3543 case Z90CRYPT_HOTPLUG_ADD
:
3544 envp
[2] = "ACTION=add";
3546 case Z90CRYPT_HOTPLUG_REMOVE
:
3547 envp
[2] = "ACTION=remove";
3557 call_usermodehelper(argv
[0], argv
, envp
, 0);
3562 module_init(z90crypt_init_module
);
3563 module_exit(z90crypt_cleanup_module
);