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