26 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30 #include <linux/module.h>
36 #include <linux/slab.h>
44 #include <linux/hrtimer.h>
52 static void ap_poll_all(
unsigned long);
54 static int ap_poll_thread_start(
void);
55 static void ap_poll_thread_stop(
void);
56 static void ap_request_timeout(
unsigned long);
57 static inline void ap_schedule_poll_timer(
void);
58 static int __ap_poll_device(
struct ap_device *ap_dev,
unsigned long *
flags);
59 static int ap_device_remove(
struct device *
dev);
60 static int ap_device_probe(
struct device *
dev);
62 static void ap_reset(
struct ap_device *ap_dev);
63 static void ap_config_timeout(
unsigned long ptr);
64 static int ap_select_domain(
void);
65 static void ap_query_configuration(
void);
72 "Copyright IBM Corp. 2006, 2012");
83 static int ap_thread_flag = 0;
85 MODULE_PARM_DESC(poll_thread,
"Turn on/off poll thread, default is 0 (off).");
109 static void *ap_interrupt_indicator;
110 static struct hrtimer ap_poll_timer;
113 static unsigned long long poll_timeout = 250000;
116 static int ap_suspend_flag;
120 static int user_set_domain = 0;
127 static inline int ap_using_interrupts(
void)
129 return ap_interrupt_indicator !=
NULL;
137 static inline int ap_instructions_available(
void)
140 register unsigned long reg1 asm (
"1") = -
ENODEV;
141 register unsigned long reg2 asm (
"2") = 0
UL;
144 " .long 0xb2af0000\n"
157 static int ap_interrupts_available(
void)
159 return test_facility(2) && test_facility(65);
169 static int ap_configuration_available(
void)
171 return test_facility(2) && test_facility(12);
186 register unsigned long reg0 asm (
"0") =
qid;
188 register unsigned long reg2 asm (
"2") = 0
UL;
190 asm volatile(
".long 0xb2af0000"
193 *queue_depth = (
int) (
reg2 & 0xff);
205 register unsigned long reg0 asm (
"0") =
qid | 0x01000000UL;
207 register unsigned long reg2 asm (
"2") = 0
UL;
226 register unsigned long reg0 asm (
"0") =
qid | 0x03000000UL;
227 register unsigned long reg1_in
asm (
"1") = 0x0000800000000000UL |
AP_ISC;
229 register void *
reg2 asm (
"2") =
ind;
232 :
"+d" (
reg0),
"+d" (reg1_in),
"=d" (reg1_out),
"+d" (
reg2)
243 register unsigned long reg0 asm (
"0") = 0
UL |
qid | (1
UL << 23);
245 register unsigned long reg2 asm (
"2");
255 *functions = (
unsigned int)(
reg2 >> 32);
263 register unsigned long reg0 asm (
"0") = 0x04000000UL;
264 register unsigned long reg1 asm (
"1") = -
EINVAL;
265 register unsigned char *
reg2 asm (
"2") = (
unsigned char *)
config;
291 static int ap_query_functions(
ap_qid_t qid,
unsigned int *functions)
296 status = __ap_query_functions(qid, functions);
299 if (ap_queue_status_invalid_test(&
status))
302 switch (
status.response_code) {
318 if (i < AP_MAX_RESET - 1) {
320 status = __ap_query_functions(qid, functions);
338 static int ap_queue_enable_interruption(
ap_qid_t qid,
void *
ind)
342 int t_depth, t_device_type,
rc,
i;
345 status = ap_queue_interruption_control(qid, ind);
348 switch (
status.response_code) {
355 if (i < AP_MAX_RESET - 1) {
357 status = ap_queue_interruption_control(qid,
374 if (i < AP_MAX_RESET - 1) {
376 status = ap_test_queue(qid, &t_depth, &t_device_type);
400 unsigned int special)
402 typedef struct {
char _[
length]; } msgblock;
403 register unsigned long reg0 asm (
"0") = qid | 0x40000000UL;
405 register unsigned long reg2 asm (
"2") = (
unsigned long)
msg;
406 register unsigned long reg3 asm (
"3") = (
unsigned long)
length;
407 register unsigned long reg4 asm (
"4") = (
unsigned int) (psmid >> 32);
408 register unsigned long reg5
asm (
"5") = (
unsigned int) psmid;
414 "0: .long 0xb2ad0042\n"
417 :
"d" (
reg4),
"d" (reg5),
"m" (*(msgblock *) msg)
426 status = __ap_send(qid, psmid, msg, length, 0);
427 switch (status.response_code) {
462 typedef struct {
char _[
length]; } msgblock;
463 register unsigned long reg0 asm(
"0") = qid | 0x80000000UL;
465 register unsigned long reg2 asm(
"2") = 0
UL;
466 register unsigned long reg4 asm(
"4") = (
unsigned long)
msg;
467 register unsigned long reg5
asm(
"5") = (
unsigned long)
length;
468 register unsigned long reg6
asm(
"6") = 0
UL;
469 register unsigned long reg7
asm(
"7") = 0
UL;
473 "0: .long 0xb2ae0064\n"
476 "+d" (
reg4),
"+d" (reg5),
"+d" (reg6),
"+d" (reg7),
477 "=m" (*(msgblock *)
msg) : :
"cc" );
478 *psmid = (((
unsigned long long) reg6) << 32) + reg7;
486 status = __ap_recv(qid, psmid, msg, length);
487 switch (status.response_code) {
491 if (status.queue_empty)
513 int t_depth, t_device_type,
rc,
i;
517 status = ap_test_queue(qid, &t_depth, &t_device_type);
518 switch (
status.response_code) {
520 *queue_depth = t_depth + 1;
521 *device_type = t_device_type;
547 if (i < AP_MAX_RESET - 1)
559 static int ap_init_queue(
ap_qid_t qid)
565 status = ap_reset_queue(qid);
567 switch (
status.response_code) {
585 if (i < AP_MAX_RESET - 1) {
587 status = ap_test_queue(qid, &dummy, &dummy);
590 if (rc == 0 && ap_using_interrupts()) {
591 rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
596 pr_err(
"Registering adapter interrupts for "
608 static void ap_increase_queue_count(
struct ap_device *ap_dev)
626 static void ap_decrease_queue_count(
struct ap_device *ap_dev)
628 int timeout = ap_dev->
drv->request_timeout;
668 spin_lock_bh(&ap_dev->
lock);
670 spin_unlock_bh(&ap_dev->
lock);
682 spin_lock_bh(&ap_dev->
lock);
684 spin_unlock_bh(&ap_dev->
lock);
696 spin_lock_bh(&ap_dev->
lock);
698 spin_unlock_bh(&ap_dev->
lock);
721 static struct attribute *ap_dev_attrs[] = {
722 &dev_attr_hwtype.attr,
723 &dev_attr_depth.attr,
724 &dev_attr_request_count.attr,
725 &dev_attr_requestq_count.attr,
726 &dev_attr_pendingq_count.attr,
727 &dev_attr_modalias.attr,
728 &dev_attr_ap_functions.attr,
732 .attrs = ap_dev_attrs
793 if (!ap_suspend_flag) {
800 if (ap_work_queue !=
NULL) {
802 ap_work_queue =
NULL;
805 tasklet_disable(&ap_tasklet);
810 spin_lock_bh(&ap_dev->
lock);
811 __ap_poll_device(ap_dev, &flags);
812 spin_unlock_bh(&ap_dev->
lock);
813 }
while ((flags & 1) || (flags & 2));
815 spin_lock_bh(&ap_dev->
lock);
817 spin_unlock_bh(&ap_dev->
lock);
822 static int ap_bus_resume(
struct device *dev)
827 if (ap_suspend_flag) {
829 if (!ap_interrupts_available())
830 ap_interrupt_indicator =
NULL;
831 ap_query_configuration();
832 if (!user_set_domain) {
837 ap_config_timer.function = ap_config_timeout;
838 ap_config_timer.data = 0;
839 ap_config_timer.expires =
jiffies + ap_config_time *
HZ;
844 tasklet_enable(&ap_tasklet);
845 if (!ap_using_interrupts())
846 ap_schedule_poll_timer();
848 tasklet_schedule(&ap_tasklet);
850 rc = ap_poll_thread_start();
853 spin_lock_bh(&ap_dev->
lock);
856 spin_unlock_bh(&ap_dev->
lock);
863 static struct bus_type ap_bus_type = {
865 .match = &ap_bus_match,
866 .uevent = &ap_uevent,
867 .suspend = ap_bus_suspend,
868 .resume = ap_bus_resume
871 static int ap_device_probe(
struct device *dev)
877 ap_dev->
drv = ap_drv;
880 spin_lock_bh(&ap_device_list_lock);
881 list_add(&ap_dev->
list, &ap_device_list);
882 spin_unlock_bh(&ap_device_list_lock);
893 static void __ap_flush_queue(
struct ap_device *ap_dev)
898 list_del_init(&ap_msg->
list);
903 list_del_init(&ap_msg->
list);
911 spin_lock_bh(&ap_dev->
lock);
912 __ap_flush_queue(ap_dev);
913 spin_unlock_bh(&ap_dev->
lock);
917 static int ap_device_remove(
struct device *dev)
924 spin_lock_bh(&ap_device_list_lock);
925 list_del_init(&ap_dev->
list);
926 spin_unlock_bh(&ap_device_list_lock);
929 spin_lock_bh(&ap_dev->
lock);
931 spin_unlock_bh(&ap_dev->
lock);
940 drv->
bus = &ap_bus_type;
941 drv->
probe = ap_device_probe;
942 drv->
remove = ap_device_remove;
964 ap_config_timer.expires =
jiffies + ap_config_time *
HZ;
987 ap_using_interrupts() ? 1 : 0);
990 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show,
NULL);
993 const char *buf,
size_t count)
997 if (
sscanf(buf,
"%d\n", &time) != 1 || time < 5 || time > 120)
999 ap_config_time =
time;
1000 if (!timer_pending(&ap_config_timer) ||
1001 !
mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
1002 ap_config_timer.expires =
jiffies + ap_config_time *
HZ;
1008 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
1016 const char *buf,
size_t count)
1020 if (
sscanf(buf,
"%d\n", &flag) != 1)
1023 rc = ap_poll_thread_start();
1028 ap_poll_thread_stop();
1032 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
1039 static ssize_t poll_timeout_store(
struct bus_type *bus,
const char *buf,
1042 unsigned long long time;
1046 if (
sscanf(buf,
"%llu\n", &time) != 1 || time < 1 ||
1047 time > 120000000000ULL)
1049 poll_timeout =
time;
1050 hr_time = ktime_set(0, poll_timeout);
1052 if (!hrtimer_is_queued(&ap_poll_timer) ||
1053 !
hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
1054 hrtimer_set_expires(&ap_poll_timer, hr_time);
1060 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
1063 &bus_attr_ap_domain,
1064 &bus_attr_config_time,
1065 &bus_attr_poll_thread,
1066 &bus_attr_ap_interrupts,
1067 &bus_attr_poll_timeout,
1071 static inline int ap_test_config(
unsigned int *
field,
unsigned int nr)
1075 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
1086 static inline int ap_test_config_card_id(
unsigned int id)
1088 if (!ap_configuration)
1090 return ap_test_config(ap_configuration->
apm,
id);
1101 static inline int ap_test_config_domain(
unsigned int domain)
1103 if (!ap_configuration)
1105 return ap_test_config(ap_configuration->
aqm, domain);
1113 static void ap_query_configuration(
void)
1116 if (ap_configuration_available()) {
1117 if (!ap_configuration)
1121 if (ap_configuration)
1122 __ap_query_configuration(ap_configuration);
1124 ap_configuration =
NULL;
1126 ap_configuration =
NULL;
1135 static int ap_select_domain(
void)
1152 if (!ap_test_config_domain(i))
1156 if (!ap_test_config_card_id(j))
1159 rc = ap_query_queue(qid, &queue_depth, &device_type);
1164 if (count > max_count) {
1169 if (best_domain >= 0){
1182 static int ap_probe_device_type(
struct ap_device *ap_dev)
1184 static unsigned char msg[] = {
1185 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1186 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1187 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1188 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1189 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1190 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1191 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1192 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1193 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1194 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1195 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1196 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1197 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1198 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1199 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1200 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1201 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1202 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1203 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1204 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1205 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1206 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1207 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1208 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1209 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1210 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1211 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1212 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1213 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1214 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1215 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1216 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1217 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1218 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1219 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1220 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1221 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1222 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1223 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1224 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1225 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1226 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1227 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1228 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1229 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1232 unsigned long long psmid;
1242 status = __ap_send(ap_dev->
qid, 0x0102030405060708ULL,
1243 msg,
sizeof(msg), 0);
1250 for (i = 0; i < 6; i++) {
1252 status = __ap_recv(ap_dev->
qid, &psmid, reply, 4096);
1254 psmid == 0x0102030405060708ULL)
1259 if (reply[0] == 0x00 && reply[1] == 0
x86)
1276 tasklet_schedule(&ap_tasklet);
1286 static int __ap_scan_bus(
struct device *dev,
void *
data)
1291 static void ap_device_release(
struct device *dev)
1304 unsigned int device_functions;
1307 ap_query_configuration();
1308 if (ap_select_domain() != 0)
1313 (
void *)(
unsigned long)qid,
1315 if (ap_test_config_card_id(i))
1316 rc = ap_query_queue(qid, &queue_depth, &device_type);
1323 rc = ap_query_queue(qid, &queue_depth,
1327 spin_lock_bh(&ap_dev->
lock);
1329 spin_unlock_bh(&ap_dev->
lock);
1336 spin_unlock_bh(&ap_dev->
lock);
1342 rc = ap_init_queue(qid);
1345 ap_dev = kzalloc(
sizeof(*ap_dev),
GFP_KERNEL);
1354 INIT_LIST_HEAD(&ap_dev->
list);
1356 (
unsigned long) ap_dev);
1357 switch (device_type) {
1360 if (ap_probe_device_type(ap_dev)) {
1369 rc = ap_query_functions(qid, &device_functions);
1375 ap_dev->
device.bus = &ap_bus_type;
1376 ap_dev->
device.parent = ap_root_device;
1382 ap_dev->
device.release = ap_device_release;
1390 &ap_dev_attr_group);
1392 spin_lock_bh(&ap_dev->
lock);
1394 spin_unlock_bh(&ap_dev->
lock);
1402 ap_config_timeout(
unsigned long ptr)
1405 ap_config_timer.expires =
jiffies + ap_config_time *
HZ;
1414 static inline void __ap_schedule_poll_timer(
void)
1418 spin_lock_bh(&ap_poll_timer_lock);
1419 if (hrtimer_is_queued(&ap_poll_timer) || ap_suspend_flag)
1421 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1422 hr_time = ktime_set(0, poll_timeout);
1423 hrtimer_forward_now(&ap_poll_timer, hr_time);
1427 spin_unlock_bh(&ap_poll_timer_lock);
1435 static inline void ap_schedule_poll_timer(
void)
1437 if (ap_using_interrupts())
1439 __ap_schedule_poll_timer();
1450 static int ap_poll_read(
struct ap_device *ap_dev,
unsigned long *flags)
1458 ap_dev->
reply->message, ap_dev->
reply->length);
1459 switch (
status.response_code) {
1462 ap_decrease_queue_count(ap_dev);
1466 list_del_init(&ap_msg->
list);
1475 if (
status.queue_empty) {
1499 static int ap_poll_write(
struct ap_device *ap_dev,
unsigned long *flags)
1511 switch (
status.response_code) {
1514 ap_increase_queue_count(ap_dev);
1524 __ap_schedule_poll_timer();
1547 static inline int ap_poll_queue(
struct ap_device *ap_dev,
unsigned long *flags)
1551 rc = ap_poll_read(ap_dev, flags);
1554 return ap_poll_write(ap_dev, flags);
1568 if (list_empty(&ap_dev->
requestq) &&
1573 switch (
status.response_code) {
1578 ap_increase_queue_count(ap_dev);
1601 ap_schedule_poll_timer();
1607 unsigned long flags;
1614 spin_lock_bh(&ap_dev->
lock);
1617 rc = ap_poll_queue(ap_dev, &flags);
1619 rc = __ap_queue_message(ap_dev, ap_msg);
1628 spin_unlock_bh(&ap_dev->
lock);
1648 spin_lock_bh(&ap_dev->
lock);
1649 if (!list_empty(&ap_msg->
list)) {
1657 list_del_init(&ap_msg->
list);
1659 spin_unlock_bh(&ap_dev->
lock);
1671 tasklet_schedule(&ap_tasklet);
1682 static void ap_reset(
struct ap_device *ap_dev)
1692 rc = ap_init_queue(ap_dev->
qid);
1696 __ap_schedule_poll_timer();
1699 static int __ap_poll_device(
struct ap_device *ap_dev,
unsigned long *flags)
1702 if (ap_poll_queue(ap_dev, flags))
1718 static void ap_poll_all(
unsigned long dummy)
1720 unsigned long flags;
1727 if (ap_using_interrupts())
1728 xchg((
u8 *)ap_interrupt_indicator, 0);
1731 spin_lock(&ap_device_list_lock);
1733 spin_lock(&ap_dev->
lock);
1734 __ap_poll_device(ap_dev, &flags);
1735 spin_unlock(&ap_dev->
lock);
1737 spin_unlock(&ap_device_list_lock);
1738 }
while (flags & 1);
1740 ap_schedule_poll_timer();
1753 static int ap_poll_thread(
void *data)
1756 unsigned long flags;
1762 if (ap_suspend_flag)
1764 if (need_resched()) {
1779 spin_lock_bh(&ap_device_list_lock);
1781 spin_lock(&ap_dev->
lock);
1782 __ap_poll_device(ap_dev, &flags);
1783 spin_unlock(&ap_dev->
lock);
1785 spin_unlock_bh(&ap_device_list_lock);
1792 static int ap_poll_thread_start(
void)
1796 if (ap_using_interrupts() || ap_suspend_flag)
1799 if (!ap_poll_kthread) {
1801 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1803 ap_poll_kthread =
NULL;
1811 static void ap_poll_thread_stop(
void)
1814 if (ap_poll_kthread) {
1816 ap_poll_kthread =
NULL;
1827 static void ap_request_timeout(
unsigned long data)
1834 if (ap_using_interrupts())
1835 tasklet_schedule(&ap_tasklet);
1839 static void ap_reset_domain(
void)
1848 static void ap_reset_all(
void)
1870 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1871 pr_warning(
"%d is not a valid cryptographic domain\n",
1879 user_set_domain = 1;
1881 if (ap_instructions_available() != 0) {
1882 pr_warning(
"The hardware system does not support "
1883 "AP instructions\n");
1886 if (ap_interrupts_available()) {
1890 if (IS_ERR(ap_interrupt_indicator)) {
1891 ap_interrupt_indicator =
NULL;
1902 for (i = 0; ap_bus_attrs[
i]; i++) {
1910 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1915 if (!ap_work_queue) {
1920 ap_query_configuration();
1921 if (ap_select_domain() == 0)
1926 ap_config_timer.function = ap_config_timeout;
1927 ap_config_timer.data = 0;
1928 ap_config_timer.expires =
jiffies + ap_config_time *
HZ;
1935 poll_timeout = 1500000;
1938 ap_poll_timer.function = ap_poll_timeout;
1941 if (ap_thread_flag) {
1942 rc = ap_poll_thread_start();
1961 if (ap_using_interrupts()) {
1968 static int __ap_match_all(
struct device *dev,
void *data)
1984 ap_poll_thread_stop();
1996 for (i = 0; ap_bus_attrs[
i]; i++)
2000 if (ap_using_interrupts()) {