Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1 | /* |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 2006, 2012 |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 3 | * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> |
| 4 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 5 | * Ralph Wuerthner <rwuerthn@de.ibm.com> |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 6 | * Felix Beck <felix.beck@de.ibm.com> |
Holger Dengler | 6bed05b | 2011-07-24 10:48:25 +0200 | [diff] [blame] | 7 | * Holger Dengler <hd@linux.vnet.ibm.com> |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 8 | * |
| 9 | * Adjunct processor bus. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2, or (at your option) |
| 14 | * any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
Martin Schwidefsky | 136f7a1 | 2008-12-25 13:39:46 +0100 | [diff] [blame] | 26 | #define KMSG_COMPONENT "ap" |
| 27 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 28 | |
Holger Dengler | 62d146f | 2011-01-05 12:47:38 +0100 | [diff] [blame] | 29 | #include <linux/kernel_stat.h> |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/err.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/workqueue.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 37 | #include <linux/notifier.h> |
| 38 | #include <linux/kthread.h> |
| 39 | #include <linux/mutex.h> |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 40 | #include <linux/suspend.h> |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 41 | #include <asm/reset.h> |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 42 | #include <asm/airq.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 43 | #include <linux/atomic.h> |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 44 | #include <asm/isc.h> |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 45 | #include <linux/hrtimer.h> |
| 46 | #include <linux/ktime.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 47 | #include <asm/facility.h> |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 48 | #include <linux/crypto.h> |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 49 | |
| 50 | #include "ap_bus.h" |
| 51 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 52 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 53 | * Module description. |
| 54 | */ |
| 55 | MODULE_AUTHOR("IBM Corporation"); |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 56 | MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \ |
| 57 | "Copyright IBM Corp. 2006, 2012"); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 58 | MODULE_LICENSE("GPL"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 59 | MODULE_ALIAS_CRYPTO("z90crypt"); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 60 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 61 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 62 | * Module parameter |
| 63 | */ |
| 64 | int ap_domain_index = -1; /* Adjunct Processor Domain Index */ |
Michael Veigel | c1a42f4 | 2014-04-14 14:28:27 +0200 | [diff] [blame] | 65 | module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 66 | MODULE_PARM_DESC(domain, "domain index for ap devices"); |
| 67 | EXPORT_SYMBOL(ap_domain_index); |
| 68 | |
Felix Beck | b90b34c | 2008-02-09 18:24:30 +0100 | [diff] [blame] | 69 | static int ap_thread_flag = 0; |
Michael Veigel | c1a42f4 | 2014-04-14 14:28:27 +0200 | [diff] [blame] | 70 | module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP); |
Felix Beck | b90b34c | 2008-02-09 18:24:30 +0100 | [diff] [blame] | 71 | MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off)."); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 72 | |
| 73 | static struct device *ap_root_device = NULL; |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 74 | static struct ap_config_info *ap_configuration; |
Christian Maaser | 43c207e6 | 2008-12-25 13:38:42 +0100 | [diff] [blame] | 75 | static DEFINE_SPINLOCK(ap_device_list_lock); |
Ralph Wuerthner | cf352ce | 2007-03-19 13:19:14 +0100 | [diff] [blame] | 76 | static LIST_HEAD(ap_device_list); |
Sascha Silbe | e387753 | 2015-10-27 18:29:52 +0100 | [diff] [blame] | 77 | static bool initialised; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 78 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 79 | /* |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 80 | * Workqueue timer for bus rescan. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 81 | */ |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 82 | static struct timer_list ap_config_timer; |
| 83 | static int ap_config_time = AP_CONFIG_TIME; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 84 | static void ap_scan_bus(struct work_struct *); |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 85 | static DECLARE_WORK(ap_scan_work, ap_scan_bus); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 86 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 87 | /* |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 88 | * Tasklet & timer for AP request polling and interrupts |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 89 | */ |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 90 | static void ap_tasklet_fn(unsigned long); |
| 91 | static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 92 | static atomic_t ap_poll_requests = ATOMIC_INIT(0); |
| 93 | static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait); |
| 94 | static struct task_struct *ap_poll_kthread = NULL; |
| 95 | static DEFINE_MUTEX(ap_poll_thread_mutex); |
Felix Beck | 9352131 | 2009-12-07 12:52:00 +0100 | [diff] [blame] | 96 | static DEFINE_SPINLOCK(ap_poll_timer_lock); |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 97 | static struct hrtimer ap_poll_timer; |
| 98 | /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds. |
| 99 | * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/ |
| 100 | static unsigned long long poll_timeout = 250000; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 101 | |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 102 | /* Suspend flag */ |
| 103 | static int ap_suspend_flag; |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 104 | /* Maximum domain id */ |
| 105 | static int ap_max_domain_id; |
Felix Beck | 5314af6 | 2009-09-22 22:58:51 +0200 | [diff] [blame] | 106 | /* Flag to check if domain was set through module parameter domain=. This is |
| 107 | * important when supsend and resume is done in a z/VM environment where the |
| 108 | * domain might change. */ |
| 109 | static int user_set_domain = 0; |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 110 | static struct bus_type ap_bus_type; |
| 111 | |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 112 | /* Adapter interrupt definitions */ |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 113 | static void ap_interrupt_handler(struct airq_struct *airq); |
| 114 | |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 115 | static int ap_airq_flag; |
| 116 | |
| 117 | static struct airq_struct ap_airq = { |
| 118 | .handler = ap_interrupt_handler, |
| 119 | .isc = AP_ISC, |
| 120 | }; |
| 121 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 122 | /** |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 123 | * ap_using_interrupts() - Returns non-zero if interrupt support is |
| 124 | * available. |
| 125 | */ |
| 126 | static inline int ap_using_interrupts(void) |
| 127 | { |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 128 | return ap_airq_flag; |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 132 | * ap_intructions_available() - Test if AP instructions are available. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 133 | * |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 134 | * Returns 0 if the AP instructions are installed. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 135 | */ |
| 136 | static inline int ap_instructions_available(void) |
| 137 | { |
| 138 | register unsigned long reg0 asm ("0") = AP_MKQID(0,0); |
| 139 | register unsigned long reg1 asm ("1") = -ENODEV; |
| 140 | register unsigned long reg2 asm ("2") = 0UL; |
| 141 | |
| 142 | asm volatile( |
| 143 | " .long 0xb2af0000\n" /* PQAP(TAPQ) */ |
| 144 | "0: la %1,0\n" |
| 145 | "1:\n" |
| 146 | EX_TABLE(0b, 1b) |
| 147 | : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" ); |
| 148 | return reg1; |
| 149 | } |
| 150 | |
| 151 | /** |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 152 | * ap_interrupts_available(): Test if AP interrupts are available. |
| 153 | * |
| 154 | * Returns 1 if AP interrupts are available. |
| 155 | */ |
| 156 | static int ap_interrupts_available(void) |
| 157 | { |
Heiko Carstens | 86cd741 | 2015-02-14 11:23:21 +0100 | [diff] [blame] | 158 | return test_facility(65); |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 162 | * ap_configuration_available(): Test if AP configuration |
| 163 | * information is available. |
| 164 | * |
| 165 | * Returns 1 if AP configuration information is available. |
| 166 | */ |
| 167 | static int ap_configuration_available(void) |
| 168 | { |
Heiko Carstens | 86cd741 | 2015-02-14 11:23:21 +0100 | [diff] [blame] | 169 | return test_facility(12); |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 172 | static inline struct ap_queue_status |
| 173 | __pqap_tapq(ap_qid_t qid, unsigned long *info) |
| 174 | { |
| 175 | register unsigned long reg0 asm ("0") = qid; |
| 176 | register struct ap_queue_status reg1 asm ("1"); |
| 177 | register unsigned long reg2 asm ("2") = 0UL; |
| 178 | |
| 179 | asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */ |
| 180 | : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc"); |
| 181 | *info = reg2; |
| 182 | return reg1; |
| 183 | } |
| 184 | |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 185 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 186 | * ap_test_queue(): Test adjunct processor queue. |
| 187 | * @qid: The AP queue number |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 188 | * @info: Pointer to queue descriptor |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 189 | * |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 190 | * Returns AP queue status structure. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 191 | */ |
| 192 | static inline struct ap_queue_status |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 193 | ap_test_queue(ap_qid_t qid, unsigned long *info) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 194 | { |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 195 | struct ap_queue_status aqs; |
| 196 | unsigned long _info; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 197 | |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 198 | if (test_facility(15)) |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 199 | qid |= 1UL << 23; /* set APFT T bit*/ |
| 200 | aqs = __pqap_tapq(qid, &_info); |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 201 | if (info) |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 202 | *info = _info; |
| 203 | return aqs; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 207 | * ap_reset_queue(): Reset adjunct processor queue. |
| 208 | * @qid: The AP queue number |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 209 | * |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 210 | * Returns AP queue status structure. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 211 | */ |
| 212 | static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid) |
| 213 | { |
| 214 | register unsigned long reg0 asm ("0") = qid | 0x01000000UL; |
| 215 | register struct ap_queue_status reg1 asm ("1"); |
| 216 | register unsigned long reg2 asm ("2") = 0UL; |
| 217 | |
| 218 | asm volatile( |
| 219 | ".long 0xb2af0000" /* PQAP(RAPQ) */ |
| 220 | : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc"); |
| 221 | return reg1; |
| 222 | } |
| 223 | |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 224 | /** |
| 225 | * ap_queue_interruption_control(): Enable interruption for a specific AP. |
| 226 | * @qid: The AP queue number |
| 227 | * @ind: The notification indicator byte |
| 228 | * |
| 229 | * Returns AP queue status. |
| 230 | */ |
| 231 | static inline struct ap_queue_status |
| 232 | ap_queue_interruption_control(ap_qid_t qid, void *ind) |
| 233 | { |
| 234 | register unsigned long reg0 asm ("0") = qid | 0x03000000UL; |
| 235 | register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC; |
| 236 | register struct ap_queue_status reg1_out asm ("1"); |
| 237 | register void *reg2 asm ("2") = ind; |
| 238 | asm volatile( |
Holger Dengler | a7475af | 2012-05-16 14:10:26 +0200 | [diff] [blame] | 239 | ".long 0xb2af0000" /* PQAP(AQIC) */ |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 240 | : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2) |
| 241 | : |
| 242 | : "cc" ); |
| 243 | return reg1_out; |
| 244 | } |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 245 | |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 246 | /** |
| 247 | * ap_query_configuration(): Get AP configuration data |
| 248 | * |
| 249 | * Returns 0 on success, or -EOPNOTSUPP. |
| 250 | */ |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 251 | static inline int __ap_query_configuration(void) |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 252 | { |
| 253 | register unsigned long reg0 asm ("0") = 0x04000000UL; |
| 254 | register unsigned long reg1 asm ("1") = -EINVAL; |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 255 | register void *reg2 asm ("2") = (void *) ap_configuration; |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 256 | |
| 257 | asm volatile( |
| 258 | ".long 0xb2af0000\n" /* PQAP(QCI) */ |
| 259 | "0: la %1,0\n" |
| 260 | "1:\n" |
| 261 | EX_TABLE(0b, 1b) |
| 262 | : "+d" (reg0), "+d" (reg1), "+d" (reg2) |
| 263 | : |
| 264 | : "cc"); |
| 265 | |
| 266 | return reg1; |
| 267 | } |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 268 | |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 269 | static inline int ap_query_configuration(void) |
| 270 | { |
| 271 | if (!ap_configuration) |
| 272 | return -EOPNOTSUPP; |
| 273 | return __ap_query_configuration(); |
| 274 | } |
| 275 | |
Holger Dengler | 6bed05b | 2011-07-24 10:48:25 +0200 | [diff] [blame] | 276 | /** |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 277 | * ap_init_configuration(): Allocate and query configuration array. |
| 278 | */ |
| 279 | static void ap_init_configuration(void) |
| 280 | { |
| 281 | if (!ap_configuration_available()) |
| 282 | return; |
| 283 | |
| 284 | ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL); |
| 285 | if (!ap_configuration) |
| 286 | return; |
| 287 | if (ap_query_configuration() != 0) { |
| 288 | kfree(ap_configuration); |
| 289 | ap_configuration = NULL; |
| 290 | return; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * ap_test_config(): helper function to extract the nrth bit |
| 296 | * within the unsigned int array field. |
| 297 | */ |
| 298 | static inline int ap_test_config(unsigned int *field, unsigned int nr) |
| 299 | { |
| 300 | return ap_test_bit((field + (nr >> 5)), (nr & 0x1f)); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * ap_test_config_card_id(): Test, whether an AP card ID is configured. |
| 305 | * @id AP card ID |
| 306 | * |
| 307 | * Returns 0 if the card is not configured |
| 308 | * 1 if the card is configured or |
| 309 | * if the configuration information is not available |
| 310 | */ |
| 311 | static inline int ap_test_config_card_id(unsigned int id) |
| 312 | { |
| 313 | if (!ap_configuration) /* QCI not supported */ |
| 314 | return 1; |
| 315 | return ap_test_config(ap_configuration->apm, id); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * ap_test_config_domain(): Test, whether an AP usage domain is configured. |
| 320 | * @domain AP usage domain ID |
| 321 | * |
| 322 | * Returns 0 if the usage domain is not configured |
| 323 | * 1 if the usage domain is configured or |
| 324 | * if the configuration information is not available |
| 325 | */ |
| 326 | static inline int ap_test_config_domain(unsigned int domain) |
| 327 | { |
| 328 | if (!ap_configuration) /* QCI not supported */ |
| 329 | return domain < 16; |
| 330 | return ap_test_config(ap_configuration->aqm, domain); |
| 331 | } |
| 332 | |
| 333 | /** |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 334 | * ap_queue_enable_interruption(): Enable interruption on an AP. |
| 335 | * @qid: The AP queue number |
| 336 | * @ind: the notification indicator byte |
| 337 | * |
| 338 | * Enables interruption on AP queue via ap_queue_interruption_control(). Based |
| 339 | * on the return value it waits a while and tests the AP queue if interrupts |
| 340 | * have been switched on using ap_test_queue(). |
| 341 | */ |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 342 | static int ap_queue_enable_interruption(struct ap_device *ap_dev, void *ind) |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 343 | { |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 344 | struct ap_queue_status status; |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 345 | |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 346 | status = ap_queue_interruption_control(ap_dev->qid, ind); |
| 347 | switch (status.response_code) { |
| 348 | case AP_RESPONSE_NORMAL: |
| 349 | case AP_RESPONSE_OTHERWISE_CHANGED: |
| 350 | return 0; |
| 351 | case AP_RESPONSE_Q_NOT_AVAIL: |
| 352 | case AP_RESPONSE_DECONFIGURED: |
| 353 | case AP_RESPONSE_CHECKSTOPPED: |
| 354 | case AP_RESPONSE_INVALID_ADDRESS: |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 355 | pr_err("Registering adapter interrupts for AP %d failed\n", |
| 356 | AP_QID_DEVICE(ap_dev->qid)); |
| 357 | return -EOPNOTSUPP; |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 358 | case AP_RESPONSE_RESET_IN_PROGRESS: |
| 359 | case AP_RESPONSE_BUSY: |
| 360 | default: |
| 361 | return -EBUSY; |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 362 | } |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 365 | static inline struct ap_queue_status |
| 366 | __nqap(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length) |
| 367 | { |
| 368 | typedef struct { char _[length]; } msgblock; |
| 369 | register unsigned long reg0 asm ("0") = qid | 0x40000000UL; |
| 370 | register struct ap_queue_status reg1 asm ("1"); |
| 371 | register unsigned long reg2 asm ("2") = (unsigned long) msg; |
| 372 | register unsigned long reg3 asm ("3") = (unsigned long) length; |
| 373 | register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32); |
| 374 | register unsigned long reg5 asm ("5") = psmid & 0xffffffff; |
| 375 | |
| 376 | asm volatile ( |
| 377 | "0: .long 0xb2ad0042\n" /* NQAP */ |
| 378 | " brc 2,0b" |
| 379 | : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3) |
| 380 | : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg) |
| 381 | : "cc"); |
| 382 | return reg1; |
| 383 | } |
| 384 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 385 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 386 | * __ap_send(): Send message to adjunct processor queue. |
| 387 | * @qid: The AP queue number |
| 388 | * @psmid: The program supplied message identifier |
| 389 | * @msg: The message text |
| 390 | * @length: The message length |
Felix Beck | a6a5d73 | 2009-12-07 12:51:55 +0100 | [diff] [blame] | 391 | * @special: Special Bit |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 392 | * |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 393 | * Returns AP queue status structure. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 394 | * Condition code 1 on NQAP can't happen because the L bit is 1. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 395 | * Condition code 2 on NQAP also means the send is incomplete, |
| 396 | * because a segment boundary was reached. The NQAP is repeated. |
| 397 | */ |
| 398 | static inline struct ap_queue_status |
Felix Beck | a6a5d73 | 2009-12-07 12:51:55 +0100 | [diff] [blame] | 399 | __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length, |
| 400 | unsigned int special) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 401 | { |
Felix Beck | a6a5d73 | 2009-12-07 12:51:55 +0100 | [diff] [blame] | 402 | if (special == 1) |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 403 | qid |= 0x400000UL; |
| 404 | return __nqap(qid, psmid, msg, length); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length) |
| 408 | { |
| 409 | struct ap_queue_status status; |
| 410 | |
Felix Beck | a6a5d73 | 2009-12-07 12:51:55 +0100 | [diff] [blame] | 411 | status = __ap_send(qid, psmid, msg, length, 0); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 412 | switch (status.response_code) { |
| 413 | case AP_RESPONSE_NORMAL: |
| 414 | return 0; |
| 415 | case AP_RESPONSE_Q_FULL: |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 416 | case AP_RESPONSE_RESET_IN_PROGRESS: |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 417 | return -EBUSY; |
Felix Beck | a6a5d73 | 2009-12-07 12:51:55 +0100 | [diff] [blame] | 418 | case AP_RESPONSE_REQ_FAC_NOT_INST: |
| 419 | return -EINVAL; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 420 | default: /* Device is gone. */ |
| 421 | return -ENODEV; |
| 422 | } |
| 423 | } |
| 424 | EXPORT_SYMBOL(ap_send); |
| 425 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 426 | /** |
| 427 | * __ap_recv(): Receive message from adjunct processor queue. |
| 428 | * @qid: The AP queue number |
| 429 | * @psmid: Pointer to program supplied message identifier |
| 430 | * @msg: The message text |
| 431 | * @length: The message length |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 432 | * |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 433 | * Returns AP queue status structure. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 434 | * Condition code 1 on DQAP means the receive has taken place |
| 435 | * but only partially. The response is incomplete, hence the |
| 436 | * DQAP is repeated. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 437 | * Condition code 2 on DQAP also means the receive is incomplete, |
| 438 | * this time because a segment boundary was reached. Again, the |
| 439 | * DQAP is repeated. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 440 | * Note that gpr2 is used by the DQAP instruction to keep track of |
| 441 | * any 'residual' length, in case the instruction gets interrupted. |
| 442 | * Hence it gets zeroed before the instruction. |
| 443 | */ |
| 444 | static inline struct ap_queue_status |
| 445 | __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length) |
| 446 | { |
| 447 | typedef struct { char _[length]; } msgblock; |
| 448 | register unsigned long reg0 asm("0") = qid | 0x80000000UL; |
| 449 | register struct ap_queue_status reg1 asm ("1"); |
| 450 | register unsigned long reg2 asm("2") = 0UL; |
| 451 | register unsigned long reg4 asm("4") = (unsigned long) msg; |
| 452 | register unsigned long reg5 asm("5") = (unsigned long) length; |
| 453 | register unsigned long reg6 asm("6") = 0UL; |
| 454 | register unsigned long reg7 asm("7") = 0UL; |
| 455 | |
| 456 | |
| 457 | asm volatile( |
Holger Dengler | a7475af | 2012-05-16 14:10:26 +0200 | [diff] [blame] | 458 | "0: .long 0xb2ae0064\n" /* DQAP */ |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 459 | " brc 6,0b\n" |
| 460 | : "+d" (reg0), "=d" (reg1), "+d" (reg2), |
| 461 | "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7), |
| 462 | "=m" (*(msgblock *) msg) : : "cc" ); |
| 463 | *psmid = (((unsigned long long) reg6) << 32) + reg7; |
| 464 | return reg1; |
| 465 | } |
| 466 | |
| 467 | int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length) |
| 468 | { |
| 469 | struct ap_queue_status status; |
| 470 | |
Ingo Tuchscherer | d6d86c5 | 2016-07-25 14:52:28 +0200 | [diff] [blame] | 471 | if (msg == NULL) |
| 472 | return -EINVAL; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 473 | status = __ap_recv(qid, psmid, msg, length); |
| 474 | switch (status.response_code) { |
| 475 | case AP_RESPONSE_NORMAL: |
| 476 | return 0; |
| 477 | case AP_RESPONSE_NO_PENDING_REPLY: |
| 478 | if (status.queue_empty) |
| 479 | return -ENOENT; |
| 480 | return -EBUSY; |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 481 | case AP_RESPONSE_RESET_IN_PROGRESS: |
| 482 | return -EBUSY; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 483 | default: |
| 484 | return -ENODEV; |
| 485 | } |
| 486 | } |
| 487 | EXPORT_SYMBOL(ap_recv); |
| 488 | |
| 489 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 490 | * ap_query_queue(): Check if an AP queue is available. |
| 491 | * @qid: The AP queue number |
| 492 | * @queue_depth: Pointer to queue depth value |
| 493 | * @device_type: Pointer to device type value |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 494 | * @facilities: Pointer to facility indicator |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 495 | */ |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 496 | static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type, |
| 497 | unsigned int *facilities) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 498 | { |
| 499 | struct ap_queue_status status; |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 500 | unsigned long info; |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 501 | int nd; |
| 502 | |
| 503 | if (!ap_test_config_card_id(AP_QID_DEVICE(qid))) |
| 504 | return -ENODEV; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 505 | |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 506 | status = ap_test_queue(qid, &info); |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 507 | switch (status.response_code) { |
| 508 | case AP_RESPONSE_NORMAL: |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 509 | *queue_depth = (int)(info & 0xff); |
| 510 | *device_type = (int)((info >> 24) & 0xff); |
| 511 | *facilities = (unsigned int)(info >> 32); |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 512 | /* Update maximum domain id */ |
| 513 | nd = (info >> 16) & 0xff; |
| 514 | if ((info & (1UL << 57)) && nd > 0) |
| 515 | ap_max_domain_id = nd; |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 516 | return 0; |
| 517 | case AP_RESPONSE_Q_NOT_AVAIL: |
| 518 | case AP_RESPONSE_DECONFIGURED: |
| 519 | case AP_RESPONSE_CHECKSTOPPED: |
| 520 | case AP_RESPONSE_INVALID_ADDRESS: |
| 521 | return -ENODEV; |
| 522 | case AP_RESPONSE_RESET_IN_PROGRESS: |
| 523 | case AP_RESPONSE_OTHERWISE_CHANGED: |
| 524 | case AP_RESPONSE_BUSY: |
| 525 | return -EBUSY; |
| 526 | default: |
| 527 | BUG(); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 528 | } |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 531 | /* State machine definitions and helpers */ |
| 532 | |
| 533 | static void ap_sm_wait(enum ap_wait wait) |
| 534 | { |
| 535 | ktime_t hr_time; |
| 536 | |
| 537 | switch (wait) { |
| 538 | case AP_WAIT_AGAIN: |
| 539 | case AP_WAIT_INTERRUPT: |
| 540 | if (ap_using_interrupts()) |
| 541 | break; |
| 542 | if (ap_poll_kthread) { |
| 543 | wake_up(&ap_poll_wait); |
| 544 | break; |
| 545 | } |
| 546 | /* Fall through */ |
| 547 | case AP_WAIT_TIMEOUT: |
| 548 | spin_lock_bh(&ap_poll_timer_lock); |
| 549 | if (!hrtimer_is_queued(&ap_poll_timer)) { |
| 550 | hr_time = ktime_set(0, poll_timeout); |
| 551 | hrtimer_forward_now(&ap_poll_timer, hr_time); |
| 552 | hrtimer_restart(&ap_poll_timer); |
| 553 | } |
| 554 | spin_unlock_bh(&ap_poll_timer_lock); |
| 555 | break; |
| 556 | case AP_WAIT_NONE: |
| 557 | default: |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | static enum ap_wait ap_sm_nop(struct ap_device *ap_dev) |
| 563 | { |
| 564 | return AP_WAIT_NONE; |
| 565 | } |
| 566 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 567 | /** |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 568 | * ap_sm_recv(): Receive pending reply messages from an AP device but do |
| 569 | * not change the state of the device. |
| 570 | * @ap_dev: pointer to the AP device |
| 571 | * |
| 572 | * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT |
| 573 | */ |
| 574 | static struct ap_queue_status ap_sm_recv(struct ap_device *ap_dev) |
| 575 | { |
| 576 | struct ap_queue_status status; |
| 577 | struct ap_message *ap_msg; |
| 578 | |
| 579 | status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid, |
| 580 | ap_dev->reply->message, ap_dev->reply->length); |
| 581 | switch (status.response_code) { |
| 582 | case AP_RESPONSE_NORMAL: |
| 583 | atomic_dec(&ap_poll_requests); |
| 584 | ap_dev->queue_count--; |
| 585 | if (ap_dev->queue_count > 0) |
| 586 | mod_timer(&ap_dev->timeout, |
| 587 | jiffies + ap_dev->drv->request_timeout); |
| 588 | list_for_each_entry(ap_msg, &ap_dev->pendingq, list) { |
| 589 | if (ap_msg->psmid != ap_dev->reply->psmid) |
| 590 | continue; |
| 591 | list_del_init(&ap_msg->list); |
| 592 | ap_dev->pendingq_count--; |
| 593 | ap_msg->receive(ap_dev, ap_msg, ap_dev->reply); |
| 594 | break; |
| 595 | } |
| 596 | case AP_RESPONSE_NO_PENDING_REPLY: |
| 597 | if (!status.queue_empty || ap_dev->queue_count <= 0) |
| 598 | break; |
| 599 | /* The card shouldn't forget requests but who knows. */ |
| 600 | atomic_sub(ap_dev->queue_count, &ap_poll_requests); |
| 601 | ap_dev->queue_count = 0; |
| 602 | list_splice_init(&ap_dev->pendingq, &ap_dev->requestq); |
| 603 | ap_dev->requestq_count += ap_dev->pendingq_count; |
| 604 | ap_dev->pendingq_count = 0; |
| 605 | break; |
| 606 | default: |
| 607 | break; |
| 608 | } |
| 609 | return status; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * ap_sm_read(): Receive pending reply messages from an AP device. |
| 614 | * @ap_dev: pointer to the AP device |
| 615 | * |
| 616 | * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT |
| 617 | */ |
| 618 | static enum ap_wait ap_sm_read(struct ap_device *ap_dev) |
| 619 | { |
| 620 | struct ap_queue_status status; |
| 621 | |
Ingo Tuchscherer | d6d86c5 | 2016-07-25 14:52:28 +0200 | [diff] [blame] | 622 | if (!ap_dev->reply) |
| 623 | return AP_WAIT_NONE; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 624 | status = ap_sm_recv(ap_dev); |
| 625 | switch (status.response_code) { |
| 626 | case AP_RESPONSE_NORMAL: |
Ingo Tuchscherer | 2bc53b8 | 2015-11-27 08:33:27 +0100 | [diff] [blame] | 627 | if (ap_dev->queue_count > 0) { |
| 628 | ap_dev->state = AP_STATE_WORKING; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 629 | return AP_WAIT_AGAIN; |
Ingo Tuchscherer | 2bc53b8 | 2015-11-27 08:33:27 +0100 | [diff] [blame] | 630 | } |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 631 | ap_dev->state = AP_STATE_IDLE; |
| 632 | return AP_WAIT_NONE; |
| 633 | case AP_RESPONSE_NO_PENDING_REPLY: |
| 634 | if (ap_dev->queue_count > 0) |
| 635 | return AP_WAIT_INTERRUPT; |
| 636 | ap_dev->state = AP_STATE_IDLE; |
| 637 | return AP_WAIT_NONE; |
| 638 | default: |
| 639 | ap_dev->state = AP_STATE_BORKED; |
| 640 | return AP_WAIT_NONE; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
Ingo Tuchscherer | d6d86c5 | 2016-07-25 14:52:28 +0200 | [diff] [blame] | 645 | * ap_sm_suspend_read(): Receive pending reply messages from an AP device |
| 646 | * without changing the device state in between. In suspend mode we don't |
| 647 | * allow sending new requests, therefore just fetch pending replies. |
| 648 | * @ap_dev: pointer to the AP device |
| 649 | * |
| 650 | * Returns AP_WAIT_NONE or AP_WAIT_AGAIN |
| 651 | */ |
| 652 | static enum ap_wait ap_sm_suspend_read(struct ap_device *ap_dev) |
| 653 | { |
| 654 | struct ap_queue_status status; |
| 655 | |
| 656 | if (!ap_dev->reply) |
| 657 | return AP_WAIT_NONE; |
| 658 | status = ap_sm_recv(ap_dev); |
| 659 | switch (status.response_code) { |
| 660 | case AP_RESPONSE_NORMAL: |
| 661 | if (ap_dev->queue_count > 0) |
| 662 | return AP_WAIT_AGAIN; |
| 663 | /* fall through */ |
| 664 | default: |
| 665 | return AP_WAIT_NONE; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /** |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 670 | * ap_sm_write(): Send messages from the request queue to an AP device. |
| 671 | * @ap_dev: pointer to the AP device |
| 672 | * |
| 673 | * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT |
| 674 | */ |
| 675 | static enum ap_wait ap_sm_write(struct ap_device *ap_dev) |
| 676 | { |
| 677 | struct ap_queue_status status; |
| 678 | struct ap_message *ap_msg; |
| 679 | |
| 680 | if (ap_dev->requestq_count <= 0) |
| 681 | return AP_WAIT_NONE; |
| 682 | /* Start the next request on the queue. */ |
| 683 | ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list); |
| 684 | status = __ap_send(ap_dev->qid, ap_msg->psmid, |
| 685 | ap_msg->message, ap_msg->length, ap_msg->special); |
| 686 | switch (status.response_code) { |
| 687 | case AP_RESPONSE_NORMAL: |
| 688 | atomic_inc(&ap_poll_requests); |
| 689 | ap_dev->queue_count++; |
| 690 | if (ap_dev->queue_count == 1) |
| 691 | mod_timer(&ap_dev->timeout, |
| 692 | jiffies + ap_dev->drv->request_timeout); |
| 693 | list_move_tail(&ap_msg->list, &ap_dev->pendingq); |
| 694 | ap_dev->requestq_count--; |
| 695 | ap_dev->pendingq_count++; |
| 696 | if (ap_dev->queue_count < ap_dev->queue_depth) { |
| 697 | ap_dev->state = AP_STATE_WORKING; |
| 698 | return AP_WAIT_AGAIN; |
| 699 | } |
| 700 | /* fall through */ |
| 701 | case AP_RESPONSE_Q_FULL: |
| 702 | ap_dev->state = AP_STATE_QUEUE_FULL; |
| 703 | return AP_WAIT_INTERRUPT; |
| 704 | case AP_RESPONSE_RESET_IN_PROGRESS: |
| 705 | ap_dev->state = AP_STATE_RESET_WAIT; |
| 706 | return AP_WAIT_TIMEOUT; |
| 707 | case AP_RESPONSE_MESSAGE_TOO_BIG: |
| 708 | case AP_RESPONSE_REQ_FAC_NOT_INST: |
| 709 | list_del_init(&ap_msg->list); |
| 710 | ap_dev->requestq_count--; |
| 711 | ap_msg->rc = -EINVAL; |
| 712 | ap_msg->receive(ap_dev, ap_msg, NULL); |
| 713 | return AP_WAIT_AGAIN; |
| 714 | default: |
| 715 | ap_dev->state = AP_STATE_BORKED; |
| 716 | return AP_WAIT_NONE; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * ap_sm_read_write(): Send and receive messages to/from an AP device. |
| 722 | * @ap_dev: pointer to the AP device |
| 723 | * |
| 724 | * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT |
| 725 | */ |
| 726 | static enum ap_wait ap_sm_read_write(struct ap_device *ap_dev) |
| 727 | { |
| 728 | return min(ap_sm_read(ap_dev), ap_sm_write(ap_dev)); |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * ap_sm_reset(): Reset an AP queue. |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 733 | * @qid: The AP queue number |
| 734 | * |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 735 | * Submit the Reset command to an AP queue. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 736 | */ |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 737 | static enum ap_wait ap_sm_reset(struct ap_device *ap_dev) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 738 | { |
| 739 | struct ap_queue_status status; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 740 | |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 741 | status = ap_reset_queue(ap_dev->qid); |
| 742 | switch (status.response_code) { |
| 743 | case AP_RESPONSE_NORMAL: |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 744 | case AP_RESPONSE_RESET_IN_PROGRESS: |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 745 | ap_dev->state = AP_STATE_RESET_WAIT; |
| 746 | ap_dev->interrupt = AP_INTR_DISABLED; |
| 747 | return AP_WAIT_TIMEOUT; |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 748 | case AP_RESPONSE_BUSY: |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 749 | return AP_WAIT_TIMEOUT; |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 750 | case AP_RESPONSE_Q_NOT_AVAIL: |
| 751 | case AP_RESPONSE_DECONFIGURED: |
| 752 | case AP_RESPONSE_CHECKSTOPPED: |
| 753 | default: |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 754 | ap_dev->state = AP_STATE_BORKED; |
| 755 | return AP_WAIT_NONE; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 756 | } |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 760 | * ap_sm_reset_wait(): Test queue for completion of the reset operation |
| 761 | * @ap_dev: pointer to the AP device |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 762 | * |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 763 | * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0. |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 764 | */ |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 765 | static enum ap_wait ap_sm_reset_wait(struct ap_device *ap_dev) |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 766 | { |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 767 | struct ap_queue_status status; |
| 768 | unsigned long info; |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 769 | |
Ingo Tuchscherer | d6d86c5 | 2016-07-25 14:52:28 +0200 | [diff] [blame] | 770 | if (ap_dev->queue_count > 0 && ap_dev->reply) |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 771 | /* Try to read a completed message and get the status */ |
| 772 | status = ap_sm_recv(ap_dev); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 773 | else |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 774 | /* Get the status with TAPQ */ |
| 775 | status = ap_test_queue(ap_dev->qid, &info); |
| 776 | |
| 777 | switch (status.response_code) { |
| 778 | case AP_RESPONSE_NORMAL: |
| 779 | if (ap_using_interrupts() && |
| 780 | ap_queue_enable_interruption(ap_dev, |
| 781 | ap_airq.lsi_ptr) == 0) |
| 782 | ap_dev->state = AP_STATE_SETIRQ_WAIT; |
| 783 | else |
| 784 | ap_dev->state = (ap_dev->queue_count > 0) ? |
| 785 | AP_STATE_WORKING : AP_STATE_IDLE; |
| 786 | return AP_WAIT_AGAIN; |
| 787 | case AP_RESPONSE_BUSY: |
| 788 | case AP_RESPONSE_RESET_IN_PROGRESS: |
| 789 | return AP_WAIT_TIMEOUT; |
| 790 | case AP_RESPONSE_Q_NOT_AVAIL: |
| 791 | case AP_RESPONSE_DECONFIGURED: |
| 792 | case AP_RESPONSE_CHECKSTOPPED: |
| 793 | default: |
| 794 | ap_dev->state = AP_STATE_BORKED; |
| 795 | return AP_WAIT_NONE; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * ap_sm_setirq_wait(): Test queue for completion of the irq enablement |
| 801 | * @ap_dev: pointer to the AP device |
| 802 | * |
| 803 | * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0. |
| 804 | */ |
| 805 | static enum ap_wait ap_sm_setirq_wait(struct ap_device *ap_dev) |
| 806 | { |
| 807 | struct ap_queue_status status; |
| 808 | unsigned long info; |
| 809 | |
Ingo Tuchscherer | d6d86c5 | 2016-07-25 14:52:28 +0200 | [diff] [blame] | 810 | if (ap_dev->queue_count > 0 && ap_dev->reply) |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 811 | /* Try to read a completed message and get the status */ |
| 812 | status = ap_sm_recv(ap_dev); |
| 813 | else |
| 814 | /* Get the status with TAPQ */ |
| 815 | status = ap_test_queue(ap_dev->qid, &info); |
| 816 | |
| 817 | if (status.int_enabled == 1) { |
| 818 | /* Irqs are now enabled */ |
| 819 | ap_dev->interrupt = AP_INTR_ENABLED; |
| 820 | ap_dev->state = (ap_dev->queue_count > 0) ? |
| 821 | AP_STATE_WORKING : AP_STATE_IDLE; |
| 822 | } |
| 823 | |
| 824 | switch (status.response_code) { |
| 825 | case AP_RESPONSE_NORMAL: |
| 826 | if (ap_dev->queue_count > 0) |
| 827 | return AP_WAIT_AGAIN; |
| 828 | /* fallthrough */ |
| 829 | case AP_RESPONSE_NO_PENDING_REPLY: |
| 830 | return AP_WAIT_TIMEOUT; |
| 831 | default: |
| 832 | ap_dev->state = AP_STATE_BORKED; |
| 833 | return AP_WAIT_NONE; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * AP state machine jump table |
| 839 | */ |
Heiko Carstens | f70a34c | 2016-05-07 12:14:29 +0200 | [diff] [blame] | 840 | static ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = { |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 841 | [AP_STATE_RESET_START] = { |
| 842 | [AP_EVENT_POLL] = ap_sm_reset, |
| 843 | [AP_EVENT_TIMEOUT] = ap_sm_nop, |
| 844 | }, |
| 845 | [AP_STATE_RESET_WAIT] = { |
| 846 | [AP_EVENT_POLL] = ap_sm_reset_wait, |
| 847 | [AP_EVENT_TIMEOUT] = ap_sm_nop, |
| 848 | }, |
| 849 | [AP_STATE_SETIRQ_WAIT] = { |
| 850 | [AP_EVENT_POLL] = ap_sm_setirq_wait, |
| 851 | [AP_EVENT_TIMEOUT] = ap_sm_nop, |
| 852 | }, |
| 853 | [AP_STATE_IDLE] = { |
| 854 | [AP_EVENT_POLL] = ap_sm_write, |
| 855 | [AP_EVENT_TIMEOUT] = ap_sm_nop, |
| 856 | }, |
| 857 | [AP_STATE_WORKING] = { |
| 858 | [AP_EVENT_POLL] = ap_sm_read_write, |
| 859 | [AP_EVENT_TIMEOUT] = ap_sm_reset, |
| 860 | }, |
| 861 | [AP_STATE_QUEUE_FULL] = { |
| 862 | [AP_EVENT_POLL] = ap_sm_read, |
| 863 | [AP_EVENT_TIMEOUT] = ap_sm_reset, |
| 864 | }, |
| 865 | [AP_STATE_SUSPEND_WAIT] = { |
Ingo Tuchscherer | d6d86c5 | 2016-07-25 14:52:28 +0200 | [diff] [blame] | 866 | [AP_EVENT_POLL] = ap_sm_suspend_read, |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 867 | [AP_EVENT_TIMEOUT] = ap_sm_nop, |
| 868 | }, |
| 869 | [AP_STATE_BORKED] = { |
| 870 | [AP_EVENT_POLL] = ap_sm_nop, |
| 871 | [AP_EVENT_TIMEOUT] = ap_sm_nop, |
| 872 | }, |
| 873 | }; |
| 874 | |
| 875 | static inline enum ap_wait ap_sm_event(struct ap_device *ap_dev, |
| 876 | enum ap_event event) |
| 877 | { |
| 878 | return ap_jumptable[ap_dev->state][event](ap_dev); |
| 879 | } |
| 880 | |
| 881 | static inline enum ap_wait ap_sm_event_loop(struct ap_device *ap_dev, |
| 882 | enum ap_event event) |
| 883 | { |
| 884 | enum ap_wait wait; |
| 885 | |
| 886 | while ((wait = ap_sm_event(ap_dev, event)) == AP_WAIT_AGAIN) |
| 887 | ; |
| 888 | return wait; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * ap_request_timeout(): Handling of request timeouts |
| 893 | * @data: Holds the AP device. |
| 894 | * |
| 895 | * Handles request timeouts. |
| 896 | */ |
| 897 | static void ap_request_timeout(unsigned long data) |
| 898 | { |
| 899 | struct ap_device *ap_dev = (struct ap_device *) data; |
| 900 | |
| 901 | if (ap_suspend_flag) |
| 902 | return; |
| 903 | spin_lock_bh(&ap_dev->lock); |
| 904 | ap_sm_wait(ap_sm_event(ap_dev, AP_EVENT_TIMEOUT)); |
| 905 | spin_unlock_bh(&ap_dev->lock); |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * ap_poll_timeout(): AP receive polling for finished AP requests. |
| 910 | * @unused: Unused pointer. |
| 911 | * |
| 912 | * Schedules the AP tasklet using a high resolution timer. |
| 913 | */ |
| 914 | static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused) |
| 915 | { |
| 916 | if (!ap_suspend_flag) |
| 917 | tasklet_schedule(&ap_tasklet); |
| 918 | return HRTIMER_NORESTART; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * ap_interrupt_handler() - Schedule ap_tasklet on interrupt |
| 923 | * @airq: pointer to adapter interrupt descriptor |
| 924 | */ |
| 925 | static void ap_interrupt_handler(struct airq_struct *airq) |
| 926 | { |
| 927 | inc_irq_stat(IRQIO_APB); |
| 928 | if (!ap_suspend_flag) |
| 929 | tasklet_schedule(&ap_tasklet); |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * ap_tasklet_fn(): Tasklet to poll all AP devices. |
| 934 | * @dummy: Unused variable |
| 935 | * |
| 936 | * Poll all AP devices on the bus. |
| 937 | */ |
| 938 | static void ap_tasklet_fn(unsigned long dummy) |
| 939 | { |
| 940 | struct ap_device *ap_dev; |
| 941 | enum ap_wait wait = AP_WAIT_NONE; |
| 942 | |
| 943 | /* Reset the indicator if interrupts are used. Thus new interrupts can |
| 944 | * be received. Doing it in the beginning of the tasklet is therefor |
| 945 | * important that no requests on any AP get lost. |
| 946 | */ |
| 947 | if (ap_using_interrupts()) |
| 948 | xchg(ap_airq.lsi_ptr, 0); |
| 949 | |
| 950 | spin_lock(&ap_device_list_lock); |
| 951 | list_for_each_entry(ap_dev, &ap_device_list, list) { |
| 952 | spin_lock_bh(&ap_dev->lock); |
| 953 | wait = min(wait, ap_sm_event_loop(ap_dev, AP_EVENT_POLL)); |
| 954 | spin_unlock_bh(&ap_dev->lock); |
| 955 | } |
| 956 | spin_unlock(&ap_device_list_lock); |
| 957 | ap_sm_wait(wait); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 958 | } |
| 959 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 960 | /** |
| 961 | * ap_poll_thread(): Thread that polls for finished requests. |
| 962 | * @data: Unused pointer |
| 963 | * |
| 964 | * AP bus poll thread. The purpose of this thread is to poll for |
| 965 | * finished requests in a loop if there is a "free" cpu - that is |
| 966 | * a cpu that doesn't have anything better to do. The polling stops |
| 967 | * as soon as there is another task or if all messages have been |
| 968 | * delivered. |
| 969 | */ |
| 970 | static int ap_poll_thread(void *data) |
| 971 | { |
| 972 | DECLARE_WAITQUEUE(wait, current); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 973 | |
| 974 | set_user_nice(current, MAX_NICE); |
| 975 | set_freezable(); |
| 976 | while (!kthread_should_stop()) { |
| 977 | add_wait_queue(&ap_poll_wait, &wait); |
| 978 | set_current_state(TASK_INTERRUPTIBLE); |
| 979 | if (ap_suspend_flag || |
| 980 | atomic_read(&ap_poll_requests) <= 0) { |
| 981 | schedule(); |
| 982 | try_to_freeze(); |
| 983 | } |
| 984 | set_current_state(TASK_RUNNING); |
| 985 | remove_wait_queue(&ap_poll_wait, &wait); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 986 | if (need_resched()) { |
| 987 | schedule(); |
| 988 | try_to_freeze(); |
| 989 | continue; |
| 990 | } |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 991 | ap_tasklet_fn(0); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 992 | } while (!kthread_should_stop()); |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | static int ap_poll_thread_start(void) |
| 997 | { |
| 998 | int rc; |
| 999 | |
| 1000 | if (ap_using_interrupts() || ap_poll_kthread) |
| 1001 | return 0; |
| 1002 | mutex_lock(&ap_poll_thread_mutex); |
| 1003 | ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll"); |
| 1004 | rc = PTR_RET(ap_poll_kthread); |
| 1005 | if (rc) |
| 1006 | ap_poll_kthread = NULL; |
| 1007 | mutex_unlock(&ap_poll_thread_mutex); |
| 1008 | return rc; |
| 1009 | } |
| 1010 | |
| 1011 | static void ap_poll_thread_stop(void) |
| 1012 | { |
| 1013 | if (!ap_poll_kthread) |
| 1014 | return; |
| 1015 | mutex_lock(&ap_poll_thread_mutex); |
| 1016 | kthread_stop(ap_poll_kthread); |
| 1017 | ap_poll_kthread = NULL; |
| 1018 | mutex_unlock(&ap_poll_thread_mutex); |
| 1019 | } |
| 1020 | |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1021 | /** |
| 1022 | * ap_queue_message(): Queue a request to an AP device. |
| 1023 | * @ap_dev: The AP device to queue the message to |
| 1024 | * @ap_msg: The message that is to be added |
| 1025 | */ |
| 1026 | void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg) |
| 1027 | { |
| 1028 | /* For asynchronous message handling a valid receive-callback |
| 1029 | * is required. */ |
| 1030 | BUG_ON(!ap_msg->receive); |
| 1031 | |
| 1032 | spin_lock_bh(&ap_dev->lock); |
| 1033 | /* Queue the message. */ |
| 1034 | list_add_tail(&ap_msg->list, &ap_dev->requestq); |
| 1035 | ap_dev->requestq_count++; |
| 1036 | ap_dev->total_request_count++; |
| 1037 | /* Send/receive as many request from the queue as possible. */ |
| 1038 | ap_sm_wait(ap_sm_event_loop(ap_dev, AP_EVENT_POLL)); |
| 1039 | spin_unlock_bh(&ap_dev->lock); |
| 1040 | } |
| 1041 | EXPORT_SYMBOL(ap_queue_message); |
| 1042 | |
| 1043 | /** |
| 1044 | * ap_cancel_message(): Cancel a crypto request. |
| 1045 | * @ap_dev: The AP device that has the message queued |
| 1046 | * @ap_msg: The message that is to be removed |
| 1047 | * |
| 1048 | * Cancel a crypto request. This is done by removing the request |
| 1049 | * from the device pending or request queue. Note that the |
| 1050 | * request stays on the AP queue. When it finishes the message |
| 1051 | * reply will be discarded because the psmid can't be found. |
| 1052 | */ |
| 1053 | void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg) |
| 1054 | { |
| 1055 | struct ap_message *tmp; |
| 1056 | |
| 1057 | spin_lock_bh(&ap_dev->lock); |
| 1058 | if (!list_empty(&ap_msg->list)) { |
| 1059 | list_for_each_entry(tmp, &ap_dev->pendingq, list) |
| 1060 | if (tmp->psmid == ap_msg->psmid) { |
| 1061 | ap_dev->pendingq_count--; |
| 1062 | goto found; |
| 1063 | } |
| 1064 | ap_dev->requestq_count--; |
| 1065 | found: |
| 1066 | list_del_init(&ap_msg->list); |
| 1067 | } |
| 1068 | spin_unlock_bh(&ap_dev->lock); |
| 1069 | } |
| 1070 | EXPORT_SYMBOL(ap_cancel_message); |
| 1071 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1072 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1073 | * AP device related attributes. |
| 1074 | */ |
| 1075 | static ssize_t ap_hwtype_show(struct device *dev, |
| 1076 | struct device_attribute *attr, char *buf) |
| 1077 | { |
| 1078 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1079 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type); |
| 1080 | } |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1081 | |
Christian Maaser | 43c207e6 | 2008-12-25 13:38:42 +0100 | [diff] [blame] | 1082 | static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL); |
Ingo Tuchscherer | 42f4dd61 | 2014-10-02 14:48:46 +0200 | [diff] [blame] | 1083 | |
| 1084 | static ssize_t ap_raw_hwtype_show(struct device *dev, |
| 1085 | struct device_attribute *attr, char *buf) |
| 1086 | { |
| 1087 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1088 | |
| 1089 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->raw_hwtype); |
| 1090 | } |
| 1091 | |
| 1092 | static DEVICE_ATTR(raw_hwtype, 0444, ap_raw_hwtype_show, NULL); |
| 1093 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1094 | static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr, |
| 1095 | char *buf) |
| 1096 | { |
| 1097 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1098 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth); |
| 1099 | } |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1100 | |
Christian Maaser | 43c207e6 | 2008-12-25 13:38:42 +0100 | [diff] [blame] | 1101 | static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1102 | static ssize_t ap_request_count_show(struct device *dev, |
| 1103 | struct device_attribute *attr, |
| 1104 | char *buf) |
| 1105 | { |
| 1106 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1107 | int rc; |
| 1108 | |
| 1109 | spin_lock_bh(&ap_dev->lock); |
| 1110 | rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count); |
| 1111 | spin_unlock_bh(&ap_dev->lock); |
| 1112 | return rc; |
| 1113 | } |
| 1114 | |
| 1115 | static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL); |
| 1116 | |
Holger Dengler | b26bd94 | 2012-08-28 16:43:48 +0200 | [diff] [blame] | 1117 | static ssize_t ap_requestq_count_show(struct device *dev, |
| 1118 | struct device_attribute *attr, char *buf) |
| 1119 | { |
| 1120 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1121 | int rc; |
| 1122 | |
| 1123 | spin_lock_bh(&ap_dev->lock); |
| 1124 | rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count); |
| 1125 | spin_unlock_bh(&ap_dev->lock); |
| 1126 | return rc; |
| 1127 | } |
| 1128 | |
| 1129 | static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL); |
| 1130 | |
| 1131 | static ssize_t ap_pendingq_count_show(struct device *dev, |
| 1132 | struct device_attribute *attr, char *buf) |
| 1133 | { |
| 1134 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1135 | int rc; |
| 1136 | |
| 1137 | spin_lock_bh(&ap_dev->lock); |
| 1138 | rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count); |
| 1139 | spin_unlock_bh(&ap_dev->lock); |
| 1140 | return rc; |
| 1141 | } |
| 1142 | |
| 1143 | static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL); |
| 1144 | |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1145 | static ssize_t ap_reset_show(struct device *dev, |
| 1146 | struct device_attribute *attr, char *buf) |
| 1147 | { |
| 1148 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1149 | int rc = 0; |
| 1150 | |
| 1151 | spin_lock_bh(&ap_dev->lock); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1152 | switch (ap_dev->state) { |
| 1153 | case AP_STATE_RESET_START: |
| 1154 | case AP_STATE_RESET_WAIT: |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1155 | rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n"); |
| 1156 | break; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1157 | case AP_STATE_WORKING: |
| 1158 | case AP_STATE_QUEUE_FULL: |
| 1159 | rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n"); |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1160 | break; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1161 | default: |
| 1162 | rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n"); |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1163 | } |
| 1164 | spin_unlock_bh(&ap_dev->lock); |
| 1165 | return rc; |
| 1166 | } |
| 1167 | |
| 1168 | static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL); |
| 1169 | |
| 1170 | static ssize_t ap_interrupt_show(struct device *dev, |
| 1171 | struct device_attribute *attr, char *buf) |
| 1172 | { |
| 1173 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1174 | int rc = 0; |
| 1175 | |
| 1176 | spin_lock_bh(&ap_dev->lock); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1177 | if (ap_dev->state == AP_STATE_SETIRQ_WAIT) |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1178 | rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n"); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1179 | else if (ap_dev->interrupt == AP_INTR_ENABLED) |
| 1180 | rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n"); |
| 1181 | else |
| 1182 | rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n"); |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1183 | spin_unlock_bh(&ap_dev->lock); |
| 1184 | return rc; |
| 1185 | } |
| 1186 | |
| 1187 | static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL); |
| 1188 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1189 | static ssize_t ap_modalias_show(struct device *dev, |
| 1190 | struct device_attribute *attr, char *buf) |
| 1191 | { |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1192 | return sprintf(buf, "ap:t%02X\n", to_ap_dev(dev)->device_type); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL); |
| 1196 | |
Holger Dengler | b26bd94 | 2012-08-28 16:43:48 +0200 | [diff] [blame] | 1197 | static ssize_t ap_functions_show(struct device *dev, |
| 1198 | struct device_attribute *attr, char *buf) |
| 1199 | { |
| 1200 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1201 | return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions); |
| 1202 | } |
| 1203 | |
| 1204 | static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL); |
| 1205 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1206 | static struct attribute *ap_dev_attrs[] = { |
| 1207 | &dev_attr_hwtype.attr, |
Ingo Tuchscherer | 42f4dd61 | 2014-10-02 14:48:46 +0200 | [diff] [blame] | 1208 | &dev_attr_raw_hwtype.attr, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1209 | &dev_attr_depth.attr, |
| 1210 | &dev_attr_request_count.attr, |
Holger Dengler | b26bd94 | 2012-08-28 16:43:48 +0200 | [diff] [blame] | 1211 | &dev_attr_requestq_count.attr, |
| 1212 | &dev_attr_pendingq_count.attr, |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1213 | &dev_attr_reset.attr, |
| 1214 | &dev_attr_interrupt.attr, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1215 | &dev_attr_modalias.attr, |
Holger Dengler | b26bd94 | 2012-08-28 16:43:48 +0200 | [diff] [blame] | 1216 | &dev_attr_ap_functions.attr, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1217 | NULL |
| 1218 | }; |
| 1219 | static struct attribute_group ap_dev_attr_group = { |
| 1220 | .attrs = ap_dev_attrs |
| 1221 | }; |
| 1222 | |
| 1223 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1224 | * ap_bus_match() |
| 1225 | * @dev: Pointer to device |
| 1226 | * @drv: Pointer to device_driver |
| 1227 | * |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1228 | * AP bus driver registration/unregistration. |
| 1229 | */ |
| 1230 | static int ap_bus_match(struct device *dev, struct device_driver *drv) |
| 1231 | { |
| 1232 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1233 | struct ap_driver *ap_drv = to_ap_drv(drv); |
| 1234 | struct ap_device_id *id; |
| 1235 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1236 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1237 | * Compare device type of the device with the list of |
| 1238 | * supported types of the device_driver. |
| 1239 | */ |
| 1240 | for (id = ap_drv->ids; id->match_flags; id++) { |
| 1241 | if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) && |
| 1242 | (id->dev_type != ap_dev->device_type)) |
| 1243 | continue; |
| 1244 | return 1; |
| 1245 | } |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1250 | * ap_uevent(): Uevent function for AP devices. |
| 1251 | * @dev: Pointer to device |
| 1252 | * @env: Pointer to kobj_uevent_env |
| 1253 | * |
| 1254 | * It sets up a single environment variable DEV_TYPE which contains the |
| 1255 | * hardware device type. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1256 | */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1257 | static int ap_uevent (struct device *dev, struct kobj_uevent_env *env) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1258 | { |
| 1259 | struct ap_device *ap_dev = to_ap_dev(dev); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1260 | int retval = 0; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1261 | |
| 1262 | if (!ap_dev) |
| 1263 | return -ENODEV; |
| 1264 | |
| 1265 | /* Set up DEV_TYPE environment variable. */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1266 | retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type); |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 1267 | if (retval) |
| 1268 | return retval; |
| 1269 | |
Cornelia Huck | 66a4263b | 2006-12-04 15:40:10 +0100 | [diff] [blame] | 1270 | /* Add MODALIAS= */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1271 | retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type); |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 1272 | |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 1273 | return retval; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1276 | static int ap_dev_suspend(struct device *dev, pm_message_t state) |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 1277 | { |
| 1278 | struct ap_device *ap_dev = to_ap_dev(dev); |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 1279 | |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 1280 | /* Poll on the device until all requests are finished. */ |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1281 | spin_lock_bh(&ap_dev->lock); |
| 1282 | ap_dev->state = AP_STATE_SUSPEND_WAIT; |
| 1283 | while (ap_sm_event(ap_dev, AP_EVENT_POLL) != AP_WAIT_NONE) |
| 1284 | ; |
| 1285 | ap_dev->state = AP_STATE_BORKED; |
| 1286 | spin_unlock_bh(&ap_dev->lock); |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 1287 | return 0; |
| 1288 | } |
| 1289 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1290 | static int ap_dev_resume(struct device *dev) |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 1291 | { |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | static void ap_bus_suspend(void) |
| 1296 | { |
| 1297 | ap_suspend_flag = 1; |
| 1298 | /* |
| 1299 | * Disable scanning for devices, thus we do not want to scan |
| 1300 | * for them after removing. |
| 1301 | */ |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 1302 | flush_work(&ap_scan_work); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1303 | tasklet_disable(&ap_tasklet); |
| 1304 | } |
| 1305 | |
| 1306 | static int __ap_devices_unregister(struct device *dev, void *dummy) |
| 1307 | { |
| 1308 | device_unregister(dev); |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | static void ap_bus_resume(void) |
| 1313 | { |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 1314 | int rc; |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 1315 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1316 | /* Unconditionally remove all AP devices */ |
| 1317 | bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister); |
| 1318 | /* Reset thin interrupt setting */ |
| 1319 | if (ap_interrupts_available() && !ap_using_interrupts()) { |
| 1320 | rc = register_adapter_interrupt(&ap_airq); |
| 1321 | ap_airq_flag = (rc == 0); |
Felix Beck | 5314af6 | 2009-09-22 22:58:51 +0200 | [diff] [blame] | 1322 | } |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1323 | if (!ap_interrupts_available() && ap_using_interrupts()) { |
| 1324 | unregister_adapter_interrupt(&ap_airq); |
| 1325 | ap_airq_flag = 0; |
| 1326 | } |
| 1327 | /* Reset domain */ |
| 1328 | if (!user_set_domain) |
| 1329 | ap_domain_index = -1; |
| 1330 | /* Get things going again */ |
| 1331 | ap_suspend_flag = 0; |
| 1332 | if (ap_airq_flag) |
| 1333 | xchg(ap_airq.lsi_ptr, 0); |
| 1334 | tasklet_enable(&ap_tasklet); |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 1335 | queue_work(system_long_wq, &ap_scan_work); |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 1336 | } |
| 1337 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1338 | static int ap_power_event(struct notifier_block *this, unsigned long event, |
| 1339 | void *ptr) |
| 1340 | { |
| 1341 | switch (event) { |
| 1342 | case PM_HIBERNATION_PREPARE: |
| 1343 | case PM_SUSPEND_PREPARE: |
| 1344 | ap_bus_suspend(); |
| 1345 | break; |
| 1346 | case PM_POST_HIBERNATION: |
| 1347 | case PM_POST_SUSPEND: |
| 1348 | ap_bus_resume(); |
| 1349 | break; |
| 1350 | default: |
| 1351 | break; |
| 1352 | } |
| 1353 | return NOTIFY_DONE; |
| 1354 | } |
| 1355 | static struct notifier_block ap_power_notifier = { |
| 1356 | .notifier_call = ap_power_event, |
| 1357 | }; |
| 1358 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1359 | static struct bus_type ap_bus_type = { |
| 1360 | .name = "ap", |
| 1361 | .match = &ap_bus_match, |
| 1362 | .uevent = &ap_uevent, |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1363 | .suspend = ap_dev_suspend, |
| 1364 | .resume = ap_dev_resume, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1365 | }; |
| 1366 | |
Ingo Tuchscherer | d6d86c5 | 2016-07-25 14:52:28 +0200 | [diff] [blame] | 1367 | void ap_device_init_reply(struct ap_device *ap_dev, |
| 1368 | struct ap_message *reply) |
| 1369 | { |
| 1370 | ap_dev->reply = reply; |
| 1371 | |
| 1372 | spin_lock_bh(&ap_dev->lock); |
| 1373 | ap_sm_wait(ap_sm_event(ap_dev, AP_EVENT_POLL)); |
| 1374 | spin_unlock_bh(&ap_dev->lock); |
| 1375 | } |
| 1376 | EXPORT_SYMBOL(ap_device_init_reply); |
| 1377 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1378 | static int ap_device_probe(struct device *dev) |
| 1379 | { |
| 1380 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1381 | struct ap_driver *ap_drv = to_ap_drv(dev->driver); |
| 1382 | int rc; |
| 1383 | |
| 1384 | ap_dev->drv = ap_drv; |
| 1385 | rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1386 | if (rc) |
| 1387 | ap_dev->drv = NULL; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1388 | return rc; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1392 | * __ap_flush_queue(): Flush requests. |
| 1393 | * @ap_dev: Pointer to the AP device |
| 1394 | * |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1395 | * Flush all requests from the request/pending queue of an AP device. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1396 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 1397 | static void __ap_flush_queue(struct ap_device *ap_dev) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1398 | { |
| 1399 | struct ap_message *ap_msg, *next; |
| 1400 | |
| 1401 | list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) { |
| 1402 | list_del_init(&ap_msg->list); |
| 1403 | ap_dev->pendingq_count--; |
Martin Schwidefsky | f58fe33 | 2015-09-14 16:59:27 +0200 | [diff] [blame] | 1404 | ap_msg->rc = -EAGAIN; |
| 1405 | ap_msg->receive(ap_dev, ap_msg, NULL); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1406 | } |
| 1407 | list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) { |
| 1408 | list_del_init(&ap_msg->list); |
| 1409 | ap_dev->requestq_count--; |
Martin Schwidefsky | f58fe33 | 2015-09-14 16:59:27 +0200 | [diff] [blame] | 1410 | ap_msg->rc = -EAGAIN; |
| 1411 | ap_msg->receive(ap_dev, ap_msg, NULL); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | void ap_flush_queue(struct ap_device *ap_dev) |
| 1416 | { |
| 1417 | spin_lock_bh(&ap_dev->lock); |
| 1418 | __ap_flush_queue(ap_dev); |
| 1419 | spin_unlock_bh(&ap_dev->lock); |
| 1420 | } |
| 1421 | EXPORT_SYMBOL(ap_flush_queue); |
| 1422 | |
| 1423 | static int ap_device_remove(struct device *dev) |
| 1424 | { |
| 1425 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 1426 | struct ap_driver *ap_drv = ap_dev->drv; |
| 1427 | |
Ralph Wuerthner | 4e56296 | 2006-10-04 20:02:05 +0200 | [diff] [blame] | 1428 | ap_flush_queue(ap_dev); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 1429 | del_timer_sync(&ap_dev->timeout); |
Christian Maaser | 43c207e6 | 2008-12-25 13:38:42 +0100 | [diff] [blame] | 1430 | spin_lock_bh(&ap_device_list_lock); |
Ralph Wuerthner | cf352ce | 2007-03-19 13:19:14 +0100 | [diff] [blame] | 1431 | list_del_init(&ap_dev->list); |
Christian Maaser | 43c207e6 | 2008-12-25 13:38:42 +0100 | [diff] [blame] | 1432 | spin_unlock_bh(&ap_device_list_lock); |
Ralph Wuerthner | faa582c | 2008-03-05 12:37:13 +0100 | [diff] [blame] | 1433 | if (ap_drv->remove) |
| 1434 | ap_drv->remove(ap_dev); |
Ralph Wuerthner | e675c0d | 2007-03-26 20:42:43 +0200 | [diff] [blame] | 1435 | spin_lock_bh(&ap_dev->lock); |
| 1436 | atomic_sub(ap_dev->queue_count, &ap_poll_requests); |
| 1437 | spin_unlock_bh(&ap_dev->lock); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1438 | return 0; |
| 1439 | } |
| 1440 | |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1441 | static void ap_device_release(struct device *dev) |
| 1442 | { |
| 1443 | kfree(to_ap_dev(dev)); |
| 1444 | } |
| 1445 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1446 | int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, |
| 1447 | char *name) |
| 1448 | { |
| 1449 | struct device_driver *drv = &ap_drv->driver; |
| 1450 | |
Sascha Silbe | e387753 | 2015-10-27 18:29:52 +0100 | [diff] [blame] | 1451 | if (!initialised) |
| 1452 | return -ENODEV; |
| 1453 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1454 | drv->bus = &ap_bus_type; |
| 1455 | drv->probe = ap_device_probe; |
| 1456 | drv->remove = ap_device_remove; |
| 1457 | drv->owner = owner; |
| 1458 | drv->name = name; |
| 1459 | return driver_register(drv); |
| 1460 | } |
| 1461 | EXPORT_SYMBOL(ap_driver_register); |
| 1462 | |
| 1463 | void ap_driver_unregister(struct ap_driver *ap_drv) |
| 1464 | { |
| 1465 | driver_unregister(&ap_drv->driver); |
| 1466 | } |
| 1467 | EXPORT_SYMBOL(ap_driver_unregister); |
| 1468 | |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 1469 | void ap_bus_force_rescan(void) |
| 1470 | { |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1471 | if (ap_suspend_flag) |
| 1472 | return; |
Ingo Tuchscherer | 56bbe68 | 2013-04-12 17:52:08 +0200 | [diff] [blame] | 1473 | /* processing a asynchronous bus rescan */ |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 1474 | del_timer(&ap_config_timer); |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 1475 | queue_work(system_long_wq, &ap_scan_work); |
| 1476 | flush_work(&ap_scan_work); |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 1477 | } |
| 1478 | EXPORT_SYMBOL(ap_bus_force_rescan); |
| 1479 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1480 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1481 | * AP bus attributes. |
| 1482 | */ |
| 1483 | static ssize_t ap_domain_show(struct bus_type *bus, char *buf) |
| 1484 | { |
| 1485 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index); |
| 1486 | } |
| 1487 | |
| 1488 | static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL); |
| 1489 | |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1490 | static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf) |
| 1491 | { |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1492 | if (!ap_configuration) /* QCI not supported */ |
| 1493 | return snprintf(buf, PAGE_SIZE, "not supported\n"); |
| 1494 | if (!test_facility(76)) |
| 1495 | /* format 0 - 16 bit domain field */ |
| 1496 | return snprintf(buf, PAGE_SIZE, "%08x%08x\n", |
| 1497 | ap_configuration->adm[0], |
| 1498 | ap_configuration->adm[1]); |
| 1499 | /* format 1 - 256 bit domain field */ |
| 1500 | return snprintf(buf, PAGE_SIZE, |
| 1501 | "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1502 | ap_configuration->adm[0], ap_configuration->adm[1], |
| 1503 | ap_configuration->adm[2], ap_configuration->adm[3], |
| 1504 | ap_configuration->adm[4], ap_configuration->adm[5], |
| 1505 | ap_configuration->adm[6], ap_configuration->adm[7]); |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static BUS_ATTR(ap_control_domain_mask, 0444, |
| 1509 | ap_control_domain_mask_show, NULL); |
| 1510 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1511 | static ssize_t ap_config_time_show(struct bus_type *bus, char *buf) |
| 1512 | { |
| 1513 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time); |
| 1514 | } |
| 1515 | |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 1516 | static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf) |
| 1517 | { |
| 1518 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 1519 | ap_using_interrupts() ? 1 : 0); |
| 1520 | } |
| 1521 | |
| 1522 | static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL); |
| 1523 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1524 | static ssize_t ap_config_time_store(struct bus_type *bus, |
| 1525 | const char *buf, size_t count) |
| 1526 | { |
| 1527 | int time; |
| 1528 | |
| 1529 | if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) |
| 1530 | return -EINVAL; |
| 1531 | ap_config_time = time; |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 1532 | mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1533 | return count; |
| 1534 | } |
| 1535 | |
| 1536 | static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store); |
| 1537 | |
| 1538 | static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf) |
| 1539 | { |
| 1540 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0); |
| 1541 | } |
| 1542 | |
| 1543 | static ssize_t ap_poll_thread_store(struct bus_type *bus, |
| 1544 | const char *buf, size_t count) |
| 1545 | { |
| 1546 | int flag, rc; |
| 1547 | |
| 1548 | if (sscanf(buf, "%d\n", &flag) != 1) |
| 1549 | return -EINVAL; |
| 1550 | if (flag) { |
| 1551 | rc = ap_poll_thread_start(); |
| 1552 | if (rc) |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1553 | count = rc; |
| 1554 | } else |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1555 | ap_poll_thread_stop(); |
| 1556 | return count; |
| 1557 | } |
| 1558 | |
| 1559 | static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store); |
| 1560 | |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1561 | static ssize_t poll_timeout_show(struct bus_type *bus, char *buf) |
| 1562 | { |
| 1563 | return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout); |
| 1564 | } |
| 1565 | |
| 1566 | static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf, |
| 1567 | size_t count) |
| 1568 | { |
| 1569 | unsigned long long time; |
| 1570 | ktime_t hr_time; |
| 1571 | |
| 1572 | /* 120 seconds = maximum poll interval */ |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 1573 | if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 || |
| 1574 | time > 120000000000ULL) |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1575 | return -EINVAL; |
| 1576 | poll_timeout = time; |
| 1577 | hr_time = ktime_set(0, poll_timeout); |
| 1578 | |
Ingo Tuchscherer | 8cc2af7 | 2015-04-28 10:31:44 +0200 | [diff] [blame] | 1579 | spin_lock_bh(&ap_poll_timer_lock); |
| 1580 | hrtimer_cancel(&ap_poll_timer); |
| 1581 | hrtimer_set_expires(&ap_poll_timer, hr_time); |
| 1582 | hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS); |
| 1583 | spin_unlock_bh(&ap_poll_timer_lock); |
| 1584 | |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1585 | return count; |
| 1586 | } |
| 1587 | |
| 1588 | static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store); |
| 1589 | |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 1590 | static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf) |
| 1591 | { |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1592 | int max_domain_id; |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 1593 | |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1594 | if (ap_configuration) |
| 1595 | max_domain_id = ap_max_domain_id ? : -1; |
| 1596 | else |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 1597 | max_domain_id = 15; |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 1598 | return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id); |
| 1599 | } |
| 1600 | |
| 1601 | static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL); |
| 1602 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1603 | static struct bus_attribute *const ap_bus_attrs[] = { |
| 1604 | &bus_attr_ap_domain, |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1605 | &bus_attr_ap_control_domain_mask, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1606 | &bus_attr_config_time, |
| 1607 | &bus_attr_poll_thread, |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 1608 | &bus_attr_ap_interrupts, |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1609 | &bus_attr_poll_timeout, |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 1610 | &bus_attr_ap_max_domain_id, |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1611 | NULL, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1612 | }; |
| 1613 | |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 1614 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1615 | * ap_select_domain(): Select an AP domain. |
| 1616 | * |
| 1617 | * Pick one of the 16 AP domains. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1618 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 1619 | static int ap_select_domain(void) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1620 | { |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 1621 | int count, max_count, best_domain; |
| 1622 | struct ap_queue_status status; |
| 1623 | int i, j; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1624 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1625 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1626 | * We want to use a single domain. Either the one specified with |
| 1627 | * the "domain=" parameter or the domain with the maximum number |
| 1628 | * of devices. |
| 1629 | */ |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1630 | if (ap_domain_index >= 0) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1631 | /* Domain has already been selected. */ |
| 1632 | return 0; |
| 1633 | best_domain = -1; |
| 1634 | max_count = 0; |
| 1635 | for (i = 0; i < AP_DOMAINS; i++) { |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 1636 | if (!ap_test_config_domain(i)) |
| 1637 | continue; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1638 | count = 0; |
| 1639 | for (j = 0; j < AP_DEVICES; j++) { |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 1640 | if (!ap_test_config_card_id(j)) |
| 1641 | continue; |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 1642 | status = ap_test_queue(AP_MKQID(j, i), NULL); |
| 1643 | if (status.response_code != AP_RESPONSE_NORMAL) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1644 | continue; |
| 1645 | count++; |
| 1646 | } |
| 1647 | if (count > max_count) { |
| 1648 | max_count = count; |
| 1649 | best_domain = i; |
| 1650 | } |
| 1651 | } |
| 1652 | if (best_domain >= 0){ |
| 1653 | ap_domain_index = best_domain; |
| 1654 | return 0; |
| 1655 | } |
| 1656 | return -ENODEV; |
| 1657 | } |
| 1658 | |
| 1659 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1660 | * __ap_scan_bus(): Scan the AP bus. |
| 1661 | * @dev: Pointer to device |
| 1662 | * @data: Pointer to data |
| 1663 | * |
| 1664 | * Scan the AP bus for new devices. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1665 | */ |
| 1666 | static int __ap_scan_bus(struct device *dev, void *data) |
| 1667 | { |
| 1668 | return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data; |
| 1669 | } |
| 1670 | |
Al Viro | 4927b3f | 2006-12-06 19:18:20 +0000 | [diff] [blame] | 1671 | static void ap_scan_bus(struct work_struct *unused) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1672 | { |
| 1673 | struct ap_device *ap_dev; |
| 1674 | struct device *dev; |
| 1675 | ap_qid_t qid; |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 1676 | int queue_depth = 0, device_type = 0; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1677 | unsigned int device_functions = 0; |
| 1678 | int rc, i, borked; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1679 | |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 1680 | ap_query_configuration(); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1681 | if (ap_select_domain() != 0) |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 1682 | goto out; |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1683 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1684 | for (i = 0; i < AP_DEVICES; i++) { |
| 1685 | qid = AP_MKQID(i, ap_domain_index); |
| 1686 | dev = bus_find_device(&ap_bus_type, NULL, |
| 1687 | (void *)(unsigned long)qid, |
| 1688 | __ap_scan_bus); |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1689 | rc = ap_query_queue(qid, &queue_depth, &device_type, |
| 1690 | &device_functions); |
Ralph Wuerthner | c6a4826 | 2007-03-26 20:42:42 +0200 | [diff] [blame] | 1691 | if (dev) { |
| 1692 | ap_dev = to_ap_dev(dev); |
| 1693 | spin_lock_bh(&ap_dev->lock); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1694 | if (rc == -ENODEV) |
| 1695 | ap_dev->state = AP_STATE_BORKED; |
| 1696 | borked = ap_dev->state == AP_STATE_BORKED; |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 1697 | spin_unlock_bh(&ap_dev->lock); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1698 | if (borked) /* Remove broken device */ |
| 1699 | device_unregister(dev); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1700 | put_device(dev); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1701 | if (!borked) |
| 1702 | continue; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1703 | } |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1704 | if (rc) |
| 1705 | continue; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1706 | ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL); |
| 1707 | if (!ap_dev) |
| 1708 | break; |
| 1709 | ap_dev->qid = qid; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1710 | ap_dev->state = AP_STATE_RESET_START; |
| 1711 | ap_dev->interrupt = AP_INTR_DISABLED; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1712 | ap_dev->queue_depth = queue_depth; |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 1713 | ap_dev->raw_hwtype = device_type; |
| 1714 | ap_dev->device_type = device_type; |
| 1715 | ap_dev->functions = device_functions; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1716 | spin_lock_init(&ap_dev->lock); |
| 1717 | INIT_LIST_HEAD(&ap_dev->pendingq); |
| 1718 | INIT_LIST_HEAD(&ap_dev->requestq); |
Ralph Wuerthner | cf352ce | 2007-03-19 13:19:14 +0100 | [diff] [blame] | 1719 | INIT_LIST_HEAD(&ap_dev->list); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 1720 | setup_timer(&ap_dev->timeout, ap_request_timeout, |
| 1721 | (unsigned long) ap_dev); |
Holger Dengler | 1e2076f | 2012-08-28 16:48:29 +0200 | [diff] [blame] | 1722 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1723 | ap_dev->device.bus = &ap_bus_type; |
| 1724 | ap_dev->device.parent = ap_root_device; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1725 | rc = dev_set_name(&ap_dev->device, "card%02x", |
| 1726 | AP_QID_DEVICE(ap_dev->qid)); |
| 1727 | if (rc) { |
Felix Beck | edc44fa | 2009-09-11 10:28:52 +0200 | [diff] [blame] | 1728 | kfree(ap_dev); |
| 1729 | continue; |
| 1730 | } |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1731 | /* Add to list of devices */ |
| 1732 | spin_lock_bh(&ap_device_list_lock); |
| 1733 | list_add(&ap_dev->list, &ap_device_list); |
| 1734 | spin_unlock_bh(&ap_device_list_lock); |
| 1735 | /* Start with a device reset */ |
| 1736 | spin_lock_bh(&ap_dev->lock); |
| 1737 | ap_sm_wait(ap_sm_event(ap_dev, AP_EVENT_POLL)); |
| 1738 | spin_unlock_bh(&ap_dev->lock); |
| 1739 | /* Register device */ |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1740 | ap_dev->device.release = ap_device_release; |
| 1741 | rc = device_register(&ap_dev->device); |
| 1742 | if (rc) { |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1743 | spin_lock_bh(&ap_dev->lock); |
| 1744 | list_del_init(&ap_dev->list); |
| 1745 | spin_unlock_bh(&ap_dev->lock); |
Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 1746 | put_device(&ap_dev->device); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1747 | continue; |
| 1748 | } |
| 1749 | /* Add device attributes. */ |
| 1750 | rc = sysfs_create_group(&ap_dev->device.kobj, |
| 1751 | &ap_dev_attr_group); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1752 | if (rc) { |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1753 | device_unregister(&ap_dev->device); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1754 | continue; |
| 1755 | } |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1756 | } |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 1757 | out: |
| 1758 | mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1759 | } |
| 1760 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1761 | static void ap_config_timeout(unsigned long ptr) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1762 | { |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1763 | if (ap_suspend_flag) |
| 1764 | return; |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 1765 | queue_work(system_long_wq, &ap_scan_work); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1766 | } |
| 1767 | |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1768 | static void ap_reset_domain(void) |
| 1769 | { |
| 1770 | int i; |
| 1771 | |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1772 | if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index)) |
| 1773 | return; |
| 1774 | for (i = 0; i < AP_DEVICES; i++) |
| 1775 | ap_reset_queue(AP_MKQID(i, ap_domain_index)); |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | static void ap_reset_all(void) |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1779 | { |
| 1780 | int i, j; |
| 1781 | |
Ingo Tuchscherer | 170387a | 2014-09-08 13:24:13 +0200 | [diff] [blame] | 1782 | for (i = 0; i < AP_DOMAINS; i++) { |
| 1783 | if (!ap_test_config_domain(i)) |
| 1784 | continue; |
| 1785 | for (j = 0; j < AP_DEVICES; j++) { |
| 1786 | if (!ap_test_config_card_id(j)) |
| 1787 | continue; |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1788 | ap_reset_queue(AP_MKQID(j, i)); |
Ingo Tuchscherer | 170387a | 2014-09-08 13:24:13 +0200 | [diff] [blame] | 1789 | } |
| 1790 | } |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | static struct reset_call ap_reset_call = { |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1794 | .fn = ap_reset_all, |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1795 | }; |
| 1796 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1797 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1798 | * ap_module_init(): The module initialization code. |
| 1799 | * |
| 1800 | * Initializes the module. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1801 | */ |
| 1802 | int __init ap_module_init(void) |
| 1803 | { |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1804 | int max_domain_id; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1805 | int rc, i; |
| 1806 | |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1807 | if (ap_instructions_available() != 0) { |
| 1808 | pr_warn("The hardware system does not support AP instructions\n"); |
| 1809 | return -ENODEV; |
| 1810 | } |
| 1811 | |
| 1812 | /* Get AP configuration data if available */ |
| 1813 | ap_init_configuration(); |
| 1814 | |
| 1815 | if (ap_configuration) |
| 1816 | max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1); |
| 1817 | else |
| 1818 | max_domain_id = 15; |
| 1819 | if (ap_domain_index < -1 || ap_domain_index > max_domain_id) { |
| 1820 | pr_warn("%d is not a valid cryptographic domain\n", |
| 1821 | ap_domain_index); |
Wei Yongjun | 33c388b | 2016-07-31 11:47:06 +0200 | [diff] [blame] | 1822 | rc = -EINVAL; |
| 1823 | goto out_free; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1824 | } |
Felix Beck | 5314af6 | 2009-09-22 22:58:51 +0200 | [diff] [blame] | 1825 | /* In resume callback we need to know if the user had set the domain. |
| 1826 | * If so, we can not just reset it. |
| 1827 | */ |
| 1828 | if (ap_domain_index >= 0) |
| 1829 | user_set_domain = 1; |
| 1830 | |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 1831 | if (ap_interrupts_available()) { |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 1832 | rc = register_adapter_interrupt(&ap_airq); |
| 1833 | ap_airq_flag = (rc == 0); |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 1834 | } |
| 1835 | |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1836 | register_reset_call(&ap_reset_call); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1837 | |
| 1838 | /* Create /sys/bus/ap. */ |
| 1839 | rc = bus_register(&ap_bus_type); |
| 1840 | if (rc) |
| 1841 | goto out; |
| 1842 | for (i = 0; ap_bus_attrs[i]; i++) { |
| 1843 | rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]); |
| 1844 | if (rc) |
| 1845 | goto out_bus; |
| 1846 | } |
| 1847 | |
| 1848 | /* Create /sys/devices/ap. */ |
Mark McLoughlin | 035da16 | 2008-12-15 12:58:29 +0000 | [diff] [blame] | 1849 | ap_root_device = root_device_register("ap"); |
Thomas Meyer | ba8da21 | 2013-06-01 11:51:13 +0200 | [diff] [blame] | 1850 | rc = PTR_RET(ap_root_device); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1851 | if (rc) |
| 1852 | goto out_bus; |
| 1853 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1854 | /* Setup the AP bus rescan timer. */ |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 1855 | setup_timer(&ap_config_timer, ap_config_timeout, 0); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1856 | |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1857 | /* |
| 1858 | * Setup the high resultion poll timer. |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1859 | * If we are running under z/VM adjust polling to z/VM polling rate. |
| 1860 | */ |
| 1861 | if (MACHINE_IS_VM) |
| 1862 | poll_timeout = 1500000; |
Felix Beck | 9352131 | 2009-12-07 12:52:00 +0100 | [diff] [blame] | 1863 | spin_lock_init(&ap_poll_timer_lock); |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1864 | hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 1865 | ap_poll_timer.function = ap_poll_timeout; |
| 1866 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1867 | /* Start the low priority AP bus poll thread. */ |
| 1868 | if (ap_thread_flag) { |
| 1869 | rc = ap_poll_thread_start(); |
| 1870 | if (rc) |
| 1871 | goto out_work; |
| 1872 | } |
| 1873 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1874 | rc = register_pm_notifier(&ap_power_notifier); |
| 1875 | if (rc) |
| 1876 | goto out_pm; |
| 1877 | |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 1878 | queue_work(system_long_wq, &ap_scan_work); |
Sascha Silbe | e387753 | 2015-10-27 18:29:52 +0100 | [diff] [blame] | 1879 | initialised = true; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1880 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1881 | return 0; |
| 1882 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1883 | out_pm: |
| 1884 | ap_poll_thread_stop(); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1885 | out_work: |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1886 | hrtimer_cancel(&ap_poll_timer); |
Mark McLoughlin | 035da16 | 2008-12-15 12:58:29 +0000 | [diff] [blame] | 1887 | root_device_unregister(ap_root_device); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1888 | out_bus: |
| 1889 | while (i--) |
| 1890 | bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); |
| 1891 | bus_unregister(&ap_bus_type); |
| 1892 | out: |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1893 | unregister_reset_call(&ap_reset_call); |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 1894 | if (ap_using_interrupts()) |
| 1895 | unregister_adapter_interrupt(&ap_airq); |
Wei Yongjun | 33c388b | 2016-07-31 11:47:06 +0200 | [diff] [blame] | 1896 | out_free: |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1897 | kfree(ap_configuration); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1898 | return rc; |
| 1899 | } |
| 1900 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1901 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1902 | * ap_modules_exit(): The module termination code |
| 1903 | * |
| 1904 | * Terminates the module. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1905 | */ |
| 1906 | void ap_module_exit(void) |
| 1907 | { |
| 1908 | int i; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1909 | |
Sascha Silbe | e387753 | 2015-10-27 18:29:52 +0100 | [diff] [blame] | 1910 | initialised = false; |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1911 | ap_reset_domain(); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1912 | ap_poll_thread_stop(); |
| 1913 | del_timer_sync(&ap_config_timer); |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1914 | hrtimer_cancel(&ap_poll_timer); |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1915 | tasklet_kill(&ap_tasklet); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1916 | bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1917 | for (i = 0; ap_bus_attrs[i]; i++) |
| 1918 | bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1919 | unregister_pm_notifier(&ap_power_notifier); |
Ingo Tuchscherer | f60b8d4 | 2015-04-30 15:36:48 +0200 | [diff] [blame] | 1920 | root_device_unregister(ap_root_device); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1921 | bus_unregister(&ap_bus_type); |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1922 | kfree(ap_configuration); |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1923 | unregister_reset_call(&ap_reset_call); |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 1924 | if (ap_using_interrupts()) |
| 1925 | unregister_adapter_interrupt(&ap_airq); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1926 | } |
| 1927 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1928 | module_init(ap_module_init); |
| 1929 | module_exit(ap_module_exit); |