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> |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 49 | #include <linux/mod_devicetable.h> |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 50 | |
| 51 | #include "ap_bus.h" |
Martin Schwidefsky | 0db7855 | 2016-09-21 12:48:54 +0200 | [diff] [blame] | 52 | #include "ap_asm.h" |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 53 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 54 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 55 | * Module description. |
| 56 | */ |
| 57 | MODULE_AUTHOR("IBM Corporation"); |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 58 | MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \ |
| 59 | "Copyright IBM Corp. 2006, 2012"); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 60 | MODULE_LICENSE("GPL"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 61 | MODULE_ALIAS_CRYPTO("z90crypt"); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 62 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 63 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 64 | * Module parameter |
| 65 | */ |
| 66 | int ap_domain_index = -1; /* Adjunct Processor Domain Index */ |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 67 | static DEFINE_SPINLOCK(ap_domain_lock); |
Michael Veigel | c1a42f4 | 2014-04-14 14:28:27 +0200 | [diff] [blame] | 68 | module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 69 | MODULE_PARM_DESC(domain, "domain index for ap devices"); |
| 70 | EXPORT_SYMBOL(ap_domain_index); |
| 71 | |
Felix Beck | b90b34c | 2008-02-09 18:24:30 +0100 | [diff] [blame] | 72 | static int ap_thread_flag = 0; |
Michael Veigel | c1a42f4 | 2014-04-14 14:28:27 +0200 | [diff] [blame] | 73 | 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] | 74 | 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] | 75 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 76 | static struct device *ap_root_device; |
| 77 | |
| 78 | DEFINE_SPINLOCK(ap_list_lock); |
| 79 | LIST_HEAD(ap_card_list); |
| 80 | |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 81 | static struct ap_config_info *ap_configuration; |
Sascha Silbe | e387753 | 2015-10-27 18:29:52 +0100 | [diff] [blame] | 82 | static bool initialised; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 83 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 84 | /* |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 85 | * Workqueue timer for bus rescan. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 86 | */ |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 87 | static struct timer_list ap_config_timer; |
| 88 | static int ap_config_time = AP_CONFIG_TIME; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 89 | static void ap_scan_bus(struct work_struct *); |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 90 | static DECLARE_WORK(ap_scan_work, ap_scan_bus); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 91 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 92 | /* |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 93 | * Tasklet & timer for AP request polling and interrupts |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 94 | */ |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 95 | static void ap_tasklet_fn(unsigned long); |
| 96 | static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 97 | static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait); |
| 98 | static struct task_struct *ap_poll_kthread = NULL; |
| 99 | static DEFINE_MUTEX(ap_poll_thread_mutex); |
Felix Beck | 9352131 | 2009-12-07 12:52:00 +0100 | [diff] [blame] | 100 | static DEFINE_SPINLOCK(ap_poll_timer_lock); |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 101 | static struct hrtimer ap_poll_timer; |
| 102 | /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds. |
| 103 | * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/ |
| 104 | static unsigned long long poll_timeout = 250000; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 105 | |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 106 | /* Suspend flag */ |
| 107 | static int ap_suspend_flag; |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 108 | /* Maximum domain id */ |
| 109 | static int ap_max_domain_id; |
Felix Beck | 5314af6 | 2009-09-22 22:58:51 +0200 | [diff] [blame] | 110 | /* Flag to check if domain was set through module parameter domain=. This is |
| 111 | * important when supsend and resume is done in a z/VM environment where the |
| 112 | * domain might change. */ |
| 113 | static int user_set_domain = 0; |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 114 | static struct bus_type ap_bus_type; |
| 115 | |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 116 | /* Adapter interrupt definitions */ |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 117 | static void ap_interrupt_handler(struct airq_struct *airq); |
| 118 | |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 119 | static int ap_airq_flag; |
| 120 | |
| 121 | static struct airq_struct ap_airq = { |
| 122 | .handler = ap_interrupt_handler, |
| 123 | .isc = AP_ISC, |
| 124 | }; |
| 125 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 126 | /** |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 127 | * ap_using_interrupts() - Returns non-zero if interrupt support is |
| 128 | * available. |
| 129 | */ |
| 130 | static inline int ap_using_interrupts(void) |
| 131 | { |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 132 | return ap_airq_flag; |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 136 | * ap_airq_ptr() - Get the address of the adapter interrupt indicator |
| 137 | * |
| 138 | * Returns the address of the local-summary-indicator of the adapter |
| 139 | * interrupt handler for AP, or NULL if adapter interrupts are not |
| 140 | * available. |
| 141 | */ |
| 142 | void *ap_airq_ptr(void) |
| 143 | { |
| 144 | if (ap_using_interrupts()) |
| 145 | return ap_airq.lsi_ptr; |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | /** |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 150 | * ap_interrupts_available(): Test if AP interrupts are available. |
| 151 | * |
| 152 | * Returns 1 if AP interrupts are available. |
| 153 | */ |
| 154 | static int ap_interrupts_available(void) |
| 155 | { |
Heiko Carstens | 86cd741 | 2015-02-14 11:23:21 +0100 | [diff] [blame] | 156 | return test_facility(65); |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 160 | * ap_configuration_available(): Test if AP configuration |
| 161 | * information is available. |
| 162 | * |
| 163 | * Returns 1 if AP configuration information is available. |
| 164 | */ |
| 165 | static int ap_configuration_available(void) |
| 166 | { |
Heiko Carstens | 86cd741 | 2015-02-14 11:23:21 +0100 | [diff] [blame] | 167 | return test_facility(12); |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 171 | * ap_test_queue(): Test adjunct processor queue. |
| 172 | * @qid: The AP queue number |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 173 | * @info: Pointer to queue descriptor |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 174 | * |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 175 | * Returns AP queue status structure. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 176 | */ |
| 177 | static inline struct ap_queue_status |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 178 | ap_test_queue(ap_qid_t qid, unsigned long *info) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 179 | { |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 180 | if (test_facility(15)) |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 181 | qid |= 1UL << 23; /* set APFT T bit*/ |
Martin Schwidefsky | 0db7855 | 2016-09-21 12:48:54 +0200 | [diff] [blame] | 182 | return ap_tapq(qid, info); |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 183 | } |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 184 | |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 185 | static inline int ap_query_configuration(void) |
| 186 | { |
| 187 | if (!ap_configuration) |
| 188 | return -EOPNOTSUPP; |
Martin Schwidefsky | 0db7855 | 2016-09-21 12:48:54 +0200 | [diff] [blame] | 189 | return ap_qci(ap_configuration); |
Heiko Carstens | 11d3767 | 2016-06-20 14:00:27 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Holger Dengler | 6bed05b | 2011-07-24 10:48:25 +0200 | [diff] [blame] | 192 | /** |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 193 | * ap_init_configuration(): Allocate and query configuration array. |
| 194 | */ |
| 195 | static void ap_init_configuration(void) |
| 196 | { |
| 197 | if (!ap_configuration_available()) |
| 198 | return; |
| 199 | |
| 200 | ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL); |
| 201 | if (!ap_configuration) |
| 202 | return; |
| 203 | if (ap_query_configuration() != 0) { |
| 204 | kfree(ap_configuration); |
| 205 | ap_configuration = NULL; |
| 206 | return; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * ap_test_config(): helper function to extract the nrth bit |
| 212 | * within the unsigned int array field. |
| 213 | */ |
| 214 | static inline int ap_test_config(unsigned int *field, unsigned int nr) |
| 215 | { |
| 216 | return ap_test_bit((field + (nr >> 5)), (nr & 0x1f)); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * ap_test_config_card_id(): Test, whether an AP card ID is configured. |
| 221 | * @id AP card ID |
| 222 | * |
| 223 | * Returns 0 if the card is not configured |
| 224 | * 1 if the card is configured or |
| 225 | * if the configuration information is not available |
| 226 | */ |
| 227 | static inline int ap_test_config_card_id(unsigned int id) |
| 228 | { |
| 229 | if (!ap_configuration) /* QCI not supported */ |
| 230 | return 1; |
| 231 | return ap_test_config(ap_configuration->apm, id); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * ap_test_config_domain(): Test, whether an AP usage domain is configured. |
| 236 | * @domain AP usage domain ID |
| 237 | * |
| 238 | * Returns 0 if the usage domain is not configured |
| 239 | * 1 if the usage domain is configured or |
| 240 | * if the configuration information is not available |
| 241 | */ |
| 242 | static inline int ap_test_config_domain(unsigned int domain) |
| 243 | { |
| 244 | if (!ap_configuration) /* QCI not supported */ |
| 245 | return domain < 16; |
| 246 | return ap_test_config(ap_configuration->aqm, domain); |
| 247 | } |
| 248 | |
| 249 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 250 | * ap_query_queue(): Check if an AP queue is available. |
| 251 | * @qid: The AP queue number |
| 252 | * @queue_depth: Pointer to queue depth value |
| 253 | * @device_type: Pointer to device type value |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 254 | * @facilities: Pointer to facility indicator |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 255 | */ |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 256 | static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type, |
| 257 | unsigned int *facilities) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 258 | { |
| 259 | struct ap_queue_status status; |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 260 | unsigned long info; |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 261 | int nd; |
| 262 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 263 | if (!ap_test_config_card_id(AP_QID_CARD(qid))) |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 264 | return -ENODEV; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 265 | |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 266 | status = ap_test_queue(qid, &info); |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 267 | switch (status.response_code) { |
| 268 | case AP_RESPONSE_NORMAL: |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 269 | *queue_depth = (int)(info & 0xff); |
| 270 | *device_type = (int)((info >> 24) & 0xff); |
| 271 | *facilities = (unsigned int)(info >> 32); |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 272 | /* Update maximum domain id */ |
| 273 | nd = (info >> 16) & 0xff; |
Ingo Tuchscherer | c1c1368 | 2016-11-02 10:23:24 +0100 | [diff] [blame] | 274 | /* if N bit is available, z13 and newer */ |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 275 | if ((info & (1UL << 57)) && nd > 0) |
| 276 | ap_max_domain_id = nd; |
Ingo Tuchscherer | c1c1368 | 2016-11-02 10:23:24 +0100 | [diff] [blame] | 277 | else /* older machine types */ |
| 278 | ap_max_domain_id = 15; |
Harald Freudenberger | 1487842 | 2016-10-27 08:57:39 +0200 | [diff] [blame] | 279 | switch (*device_type) { |
| 280 | /* For CEX2 and CEX3 the available functions |
| 281 | * are not refrected by the facilities bits. |
| 282 | * Instead it is coded into the type. So here |
| 283 | * modify the function bits based on the type. |
| 284 | */ |
| 285 | case AP_DEVICE_TYPE_CEX2A: |
| 286 | case AP_DEVICE_TYPE_CEX3A: |
| 287 | *facilities |= 0x08000000; |
| 288 | break; |
| 289 | case AP_DEVICE_TYPE_CEX2C: |
| 290 | case AP_DEVICE_TYPE_CEX3C: |
| 291 | *facilities |= 0x10000000; |
| 292 | break; |
| 293 | default: |
| 294 | break; |
| 295 | } |
Ingo Tuchscherer | c50a160 | 2015-06-17 16:19:15 +0200 | [diff] [blame] | 296 | return 0; |
| 297 | case AP_RESPONSE_Q_NOT_AVAIL: |
| 298 | case AP_RESPONSE_DECONFIGURED: |
| 299 | case AP_RESPONSE_CHECKSTOPPED: |
| 300 | case AP_RESPONSE_INVALID_ADDRESS: |
| 301 | return -ENODEV; |
| 302 | case AP_RESPONSE_RESET_IN_PROGRESS: |
| 303 | case AP_RESPONSE_OTHERWISE_CHANGED: |
| 304 | case AP_RESPONSE_BUSY: |
| 305 | return -EBUSY; |
| 306 | default: |
| 307 | BUG(); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 308 | } |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 311 | void ap_wait(enum ap_wait wait) |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 312 | { |
| 313 | ktime_t hr_time; |
| 314 | |
| 315 | switch (wait) { |
| 316 | case AP_WAIT_AGAIN: |
| 317 | case AP_WAIT_INTERRUPT: |
| 318 | if (ap_using_interrupts()) |
| 319 | break; |
| 320 | if (ap_poll_kthread) { |
| 321 | wake_up(&ap_poll_wait); |
| 322 | break; |
| 323 | } |
| 324 | /* Fall through */ |
| 325 | case AP_WAIT_TIMEOUT: |
| 326 | spin_lock_bh(&ap_poll_timer_lock); |
| 327 | if (!hrtimer_is_queued(&ap_poll_timer)) { |
| 328 | hr_time = ktime_set(0, poll_timeout); |
| 329 | hrtimer_forward_now(&ap_poll_timer, hr_time); |
| 330 | hrtimer_restart(&ap_poll_timer); |
| 331 | } |
| 332 | spin_unlock_bh(&ap_poll_timer_lock); |
| 333 | break; |
| 334 | case AP_WAIT_NONE: |
| 335 | default: |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 340 | /** |
| 341 | * ap_request_timeout(): Handling of request timeouts |
| 342 | * @data: Holds the AP device. |
| 343 | * |
| 344 | * Handles request timeouts. |
| 345 | */ |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 346 | void ap_request_timeout(unsigned long data) |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 347 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 348 | struct ap_queue *aq = (struct ap_queue *) data; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 349 | |
| 350 | if (ap_suspend_flag) |
| 351 | return; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 352 | spin_lock_bh(&aq->lock); |
| 353 | ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT)); |
| 354 | spin_unlock_bh(&aq->lock); |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
| 358 | * ap_poll_timeout(): AP receive polling for finished AP requests. |
| 359 | * @unused: Unused pointer. |
| 360 | * |
| 361 | * Schedules the AP tasklet using a high resolution timer. |
| 362 | */ |
| 363 | static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused) |
| 364 | { |
| 365 | if (!ap_suspend_flag) |
| 366 | tasklet_schedule(&ap_tasklet); |
| 367 | return HRTIMER_NORESTART; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * ap_interrupt_handler() - Schedule ap_tasklet on interrupt |
| 372 | * @airq: pointer to adapter interrupt descriptor |
| 373 | */ |
| 374 | static void ap_interrupt_handler(struct airq_struct *airq) |
| 375 | { |
| 376 | inc_irq_stat(IRQIO_APB); |
| 377 | if (!ap_suspend_flag) |
| 378 | tasklet_schedule(&ap_tasklet); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * ap_tasklet_fn(): Tasklet to poll all AP devices. |
| 383 | * @dummy: Unused variable |
| 384 | * |
| 385 | * Poll all AP devices on the bus. |
| 386 | */ |
| 387 | static void ap_tasklet_fn(unsigned long dummy) |
| 388 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 389 | struct ap_card *ac; |
| 390 | struct ap_queue *aq; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 391 | enum ap_wait wait = AP_WAIT_NONE; |
| 392 | |
| 393 | /* Reset the indicator if interrupts are used. Thus new interrupts can |
| 394 | * be received. Doing it in the beginning of the tasklet is therefor |
| 395 | * important that no requests on any AP get lost. |
| 396 | */ |
| 397 | if (ap_using_interrupts()) |
| 398 | xchg(ap_airq.lsi_ptr, 0); |
| 399 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 400 | spin_lock_bh(&ap_list_lock); |
| 401 | for_each_ap_card(ac) { |
| 402 | for_each_ap_queue(aq, ac) { |
| 403 | spin_lock_bh(&aq->lock); |
| 404 | wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL)); |
| 405 | spin_unlock_bh(&aq->lock); |
| 406 | } |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 407 | } |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 408 | spin_unlock_bh(&ap_list_lock); |
| 409 | |
| 410 | ap_wait(wait); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Martin Schwidefsky | 9af3e04 | 2016-09-21 14:12:53 +0200 | [diff] [blame] | 413 | static int ap_pending_requests(void) |
| 414 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 415 | struct ap_card *ac; |
| 416 | struct ap_queue *aq; |
Martin Schwidefsky | 9af3e04 | 2016-09-21 14:12:53 +0200 | [diff] [blame] | 417 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 418 | spin_lock_bh(&ap_list_lock); |
| 419 | for_each_ap_card(ac) { |
| 420 | for_each_ap_queue(aq, ac) { |
| 421 | if (aq->queue_count == 0) |
| 422 | continue; |
| 423 | spin_unlock_bh(&ap_list_lock); |
| 424 | return 1; |
Martin Schwidefsky | 9af3e04 | 2016-09-21 14:12:53 +0200 | [diff] [blame] | 425 | } |
Martin Schwidefsky | 9af3e04 | 2016-09-21 14:12:53 +0200 | [diff] [blame] | 426 | } |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 427 | spin_unlock_bh(&ap_list_lock); |
| 428 | return 0; |
Martin Schwidefsky | 9af3e04 | 2016-09-21 14:12:53 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 431 | /** |
| 432 | * ap_poll_thread(): Thread that polls for finished requests. |
| 433 | * @data: Unused pointer |
| 434 | * |
| 435 | * AP bus poll thread. The purpose of this thread is to poll for |
| 436 | * finished requests in a loop if there is a "free" cpu - that is |
| 437 | * a cpu that doesn't have anything better to do. The polling stops |
| 438 | * as soon as there is another task or if all messages have been |
| 439 | * delivered. |
| 440 | */ |
| 441 | static int ap_poll_thread(void *data) |
| 442 | { |
| 443 | DECLARE_WAITQUEUE(wait, current); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 444 | |
| 445 | set_user_nice(current, MAX_NICE); |
| 446 | set_freezable(); |
| 447 | while (!kthread_should_stop()) { |
| 448 | add_wait_queue(&ap_poll_wait, &wait); |
| 449 | set_current_state(TASK_INTERRUPTIBLE); |
Martin Schwidefsky | 9af3e04 | 2016-09-21 14:12:53 +0200 | [diff] [blame] | 450 | if (ap_suspend_flag || !ap_pending_requests()) { |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 451 | schedule(); |
| 452 | try_to_freeze(); |
| 453 | } |
| 454 | set_current_state(TASK_RUNNING); |
| 455 | remove_wait_queue(&ap_poll_wait, &wait); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 456 | if (need_resched()) { |
| 457 | schedule(); |
| 458 | try_to_freeze(); |
| 459 | continue; |
| 460 | } |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 461 | ap_tasklet_fn(0); |
Martin Schwidefsky | 9af3e04 | 2016-09-21 14:12:53 +0200 | [diff] [blame] | 462 | } |
| 463 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int ap_poll_thread_start(void) |
| 468 | { |
| 469 | int rc; |
| 470 | |
| 471 | if (ap_using_interrupts() || ap_poll_kthread) |
| 472 | return 0; |
| 473 | mutex_lock(&ap_poll_thread_mutex); |
| 474 | ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll"); |
| 475 | rc = PTR_RET(ap_poll_kthread); |
| 476 | if (rc) |
| 477 | ap_poll_kthread = NULL; |
| 478 | mutex_unlock(&ap_poll_thread_mutex); |
| 479 | return rc; |
| 480 | } |
| 481 | |
| 482 | static void ap_poll_thread_stop(void) |
| 483 | { |
| 484 | if (!ap_poll_kthread) |
| 485 | return; |
| 486 | mutex_lock(&ap_poll_thread_mutex); |
| 487 | kthread_stop(ap_poll_kthread); |
| 488 | ap_poll_kthread = NULL; |
| 489 | mutex_unlock(&ap_poll_thread_mutex); |
| 490 | } |
| 491 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 492 | #define is_card_dev(x) ((x)->parent == ap_root_device) |
| 493 | #define is_queue_dev(x) ((x)->parent != ap_root_device) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 494 | |
| 495 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 496 | * ap_bus_match() |
| 497 | * @dev: Pointer to device |
| 498 | * @drv: Pointer to device_driver |
| 499 | * |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 500 | * AP bus driver registration/unregistration. |
| 501 | */ |
| 502 | static int ap_bus_match(struct device *dev, struct device_driver *drv) |
| 503 | { |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 504 | struct ap_driver *ap_drv = to_ap_drv(drv); |
| 505 | struct ap_device_id *id; |
| 506 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 507 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 508 | * Compare device type of the device with the list of |
| 509 | * supported types of the device_driver. |
| 510 | */ |
| 511 | for (id = ap_drv->ids; id->match_flags; id++) { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 512 | if (is_card_dev(dev) && |
| 513 | id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE && |
| 514 | id->dev_type == to_ap_dev(dev)->device_type) |
| 515 | return 1; |
| 516 | if (is_queue_dev(dev) && |
| 517 | id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE && |
| 518 | id->dev_type == to_ap_dev(dev)->device_type) |
| 519 | return 1; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 525 | * ap_uevent(): Uevent function for AP devices. |
| 526 | * @dev: Pointer to device |
| 527 | * @env: Pointer to kobj_uevent_env |
| 528 | * |
| 529 | * It sets up a single environment variable DEV_TYPE which contains the |
| 530 | * hardware device type. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 531 | */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 532 | static int ap_uevent (struct device *dev, struct kobj_uevent_env *env) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 533 | { |
| 534 | struct ap_device *ap_dev = to_ap_dev(dev); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 535 | int retval = 0; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 536 | |
| 537 | if (!ap_dev) |
| 538 | return -ENODEV; |
| 539 | |
| 540 | /* Set up DEV_TYPE environment variable. */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 541 | retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type); |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 542 | if (retval) |
| 543 | return retval; |
| 544 | |
Cornelia Huck | 66a4263b | 2006-12-04 15:40:10 +0100 | [diff] [blame] | 545 | /* Add MODALIAS= */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 546 | 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] | 547 | |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 548 | return retval; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 549 | } |
| 550 | |
Lars-Peter Clausen | 3e488c9 | 2016-11-22 22:06:00 +0100 | [diff] [blame] | 551 | static int ap_dev_suspend(struct device *dev) |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 552 | { |
| 553 | struct ap_device *ap_dev = to_ap_dev(dev); |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 554 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 555 | if (ap_dev->drv && ap_dev->drv->suspend) |
| 556 | ap_dev->drv->suspend(ap_dev); |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | static int ap_dev_resume(struct device *dev) |
| 561 | { |
| 562 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 563 | |
| 564 | if (ap_dev->drv && ap_dev->drv->resume) |
| 565 | ap_dev->drv->resume(ap_dev); |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 566 | return 0; |
| 567 | } |
| 568 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 569 | static void ap_bus_suspend(void) |
| 570 | { |
| 571 | ap_suspend_flag = 1; |
| 572 | /* |
| 573 | * Disable scanning for devices, thus we do not want to scan |
| 574 | * for them after removing. |
| 575 | */ |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 576 | flush_work(&ap_scan_work); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 577 | tasklet_disable(&ap_tasklet); |
| 578 | } |
| 579 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 580 | static int __ap_card_devices_unregister(struct device *dev, void *dummy) |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 581 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 582 | if (is_card_dev(dev)) |
| 583 | device_unregister(dev); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int __ap_queue_devices_unregister(struct device *dev, void *dummy) |
| 588 | { |
| 589 | if (is_queue_dev(dev)) |
| 590 | device_unregister(dev); |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data) |
| 595 | { |
| 596 | if (is_queue_dev(dev) && |
| 597 | AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data) |
| 598 | device_unregister(dev); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static void ap_bus_resume(void) |
| 603 | { |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 604 | int rc; |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 605 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 606 | /* remove all queue devices */ |
| 607 | bus_for_each_dev(&ap_bus_type, NULL, NULL, |
| 608 | __ap_queue_devices_unregister); |
| 609 | /* remove all card devices */ |
| 610 | bus_for_each_dev(&ap_bus_type, NULL, NULL, |
| 611 | __ap_card_devices_unregister); |
| 612 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 613 | /* Reset thin interrupt setting */ |
| 614 | if (ap_interrupts_available() && !ap_using_interrupts()) { |
| 615 | rc = register_adapter_interrupt(&ap_airq); |
| 616 | ap_airq_flag = (rc == 0); |
Felix Beck | 5314af6 | 2009-09-22 22:58:51 +0200 | [diff] [blame] | 617 | } |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 618 | if (!ap_interrupts_available() && ap_using_interrupts()) { |
| 619 | unregister_adapter_interrupt(&ap_airq); |
| 620 | ap_airq_flag = 0; |
| 621 | } |
| 622 | /* Reset domain */ |
| 623 | if (!user_set_domain) |
| 624 | ap_domain_index = -1; |
| 625 | /* Get things going again */ |
| 626 | ap_suspend_flag = 0; |
| 627 | if (ap_airq_flag) |
| 628 | xchg(ap_airq.lsi_ptr, 0); |
| 629 | tasklet_enable(&ap_tasklet); |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 630 | queue_work(system_long_wq, &ap_scan_work); |
Felix Beck | 772f547 | 2009-06-22 12:08:16 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 633 | static int ap_power_event(struct notifier_block *this, unsigned long event, |
| 634 | void *ptr) |
| 635 | { |
| 636 | switch (event) { |
| 637 | case PM_HIBERNATION_PREPARE: |
| 638 | case PM_SUSPEND_PREPARE: |
| 639 | ap_bus_suspend(); |
| 640 | break; |
| 641 | case PM_POST_HIBERNATION: |
| 642 | case PM_POST_SUSPEND: |
| 643 | ap_bus_resume(); |
| 644 | break; |
| 645 | default: |
| 646 | break; |
| 647 | } |
| 648 | return NOTIFY_DONE; |
| 649 | } |
| 650 | static struct notifier_block ap_power_notifier = { |
| 651 | .notifier_call = ap_power_event, |
| 652 | }; |
| 653 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 654 | static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume); |
Lars-Peter Clausen | 3e488c9 | 2016-11-22 22:06:00 +0100 | [diff] [blame] | 655 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 656 | static struct bus_type ap_bus_type = { |
| 657 | .name = "ap", |
| 658 | .match = &ap_bus_match, |
| 659 | .uevent = &ap_uevent, |
Lars-Peter Clausen | 3e488c9 | 2016-11-22 22:06:00 +0100 | [diff] [blame] | 660 | .pm = &ap_bus_pm_ops, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | static int ap_device_probe(struct device *dev) |
| 664 | { |
| 665 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 666 | struct ap_driver *ap_drv = to_ap_drv(dev->driver); |
| 667 | int rc; |
| 668 | |
| 669 | ap_dev->drv = ap_drv; |
| 670 | rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 671 | if (rc) |
| 672 | ap_dev->drv = NULL; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 673 | return rc; |
| 674 | } |
| 675 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 676 | static int ap_device_remove(struct device *dev) |
| 677 | { |
| 678 | struct ap_device *ap_dev = to_ap_dev(dev); |
| 679 | struct ap_driver *ap_drv = ap_dev->drv; |
| 680 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 681 | spin_lock_bh(&ap_list_lock); |
| 682 | if (is_card_dev(dev)) |
| 683 | list_del_init(&to_ap_card(dev)->list); |
| 684 | else |
| 685 | list_del_init(&to_ap_queue(dev)->list); |
| 686 | spin_unlock_bh(&ap_list_lock); |
Ralph Wuerthner | faa582c | 2008-03-05 12:37:13 +0100 | [diff] [blame] | 687 | if (ap_drv->remove) |
| 688 | ap_drv->remove(ap_dev); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, |
| 693 | char *name) |
| 694 | { |
| 695 | struct device_driver *drv = &ap_drv->driver; |
| 696 | |
Sascha Silbe | e387753 | 2015-10-27 18:29:52 +0100 | [diff] [blame] | 697 | if (!initialised) |
| 698 | return -ENODEV; |
| 699 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 700 | drv->bus = &ap_bus_type; |
| 701 | drv->probe = ap_device_probe; |
| 702 | drv->remove = ap_device_remove; |
| 703 | drv->owner = owner; |
| 704 | drv->name = name; |
| 705 | return driver_register(drv); |
| 706 | } |
| 707 | EXPORT_SYMBOL(ap_driver_register); |
| 708 | |
| 709 | void ap_driver_unregister(struct ap_driver *ap_drv) |
| 710 | { |
| 711 | driver_unregister(&ap_drv->driver); |
| 712 | } |
| 713 | EXPORT_SYMBOL(ap_driver_unregister); |
| 714 | |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 715 | void ap_bus_force_rescan(void) |
| 716 | { |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 717 | if (ap_suspend_flag) |
| 718 | return; |
Ingo Tuchscherer | 56bbe68 | 2013-04-12 17:52:08 +0200 | [diff] [blame] | 719 | /* processing a asynchronous bus rescan */ |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 720 | del_timer(&ap_config_timer); |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 721 | queue_work(system_long_wq, &ap_scan_work); |
| 722 | flush_work(&ap_scan_work); |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 723 | } |
| 724 | EXPORT_SYMBOL(ap_bus_force_rescan); |
| 725 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 726 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 727 | * AP bus attributes. |
| 728 | */ |
| 729 | static ssize_t ap_domain_show(struct bus_type *bus, char *buf) |
| 730 | { |
| 731 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index); |
| 732 | } |
| 733 | |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 734 | static ssize_t ap_domain_store(struct bus_type *bus, |
| 735 | const char *buf, size_t count) |
| 736 | { |
| 737 | int domain; |
| 738 | |
| 739 | if (sscanf(buf, "%i\n", &domain) != 1 || |
| 740 | domain < 0 || domain > ap_max_domain_id) |
| 741 | return -EINVAL; |
| 742 | spin_lock_bh(&ap_domain_lock); |
| 743 | ap_domain_index = domain; |
| 744 | spin_unlock_bh(&ap_domain_lock); |
| 745 | return count; |
| 746 | } |
| 747 | |
| 748 | static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 749 | |
Ingo Tuchscherer | 91f3e3e | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 750 | static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf) |
| 751 | { |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 752 | if (!ap_configuration) /* QCI not supported */ |
| 753 | return snprintf(buf, PAGE_SIZE, "not supported\n"); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 754 | |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 755 | return snprintf(buf, PAGE_SIZE, |
| 756 | "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", |
Ingo Tuchscherer | 91f3e3e | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 757 | ap_configuration->adm[0], ap_configuration->adm[1], |
| 758 | ap_configuration->adm[2], ap_configuration->adm[3], |
| 759 | ap_configuration->adm[4], ap_configuration->adm[5], |
| 760 | ap_configuration->adm[6], ap_configuration->adm[7]); |
Ingo Tuchscherer | 91f3e3e | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static BUS_ATTR(ap_control_domain_mask, 0444, |
| 764 | ap_control_domain_mask_show, NULL); |
| 765 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 766 | static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf) |
| 767 | { |
| 768 | if (!ap_configuration) /* QCI not supported */ |
| 769 | return snprintf(buf, PAGE_SIZE, "not supported\n"); |
| 770 | |
| 771 | return snprintf(buf, PAGE_SIZE, |
| 772 | "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", |
| 773 | ap_configuration->aqm[0], ap_configuration->aqm[1], |
| 774 | ap_configuration->aqm[2], ap_configuration->aqm[3], |
| 775 | ap_configuration->aqm[4], ap_configuration->aqm[5], |
| 776 | ap_configuration->aqm[6], ap_configuration->aqm[7]); |
| 777 | } |
| 778 | |
| 779 | static BUS_ATTR(ap_usage_domain_mask, 0444, |
| 780 | ap_usage_domain_mask_show, NULL); |
| 781 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 782 | static ssize_t ap_config_time_show(struct bus_type *bus, char *buf) |
| 783 | { |
| 784 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time); |
| 785 | } |
| 786 | |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 787 | static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf) |
| 788 | { |
| 789 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 790 | ap_using_interrupts() ? 1 : 0); |
| 791 | } |
| 792 | |
| 793 | static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL); |
| 794 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 795 | static ssize_t ap_config_time_store(struct bus_type *bus, |
| 796 | const char *buf, size_t count) |
| 797 | { |
| 798 | int time; |
| 799 | |
| 800 | if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) |
| 801 | return -EINVAL; |
| 802 | ap_config_time = time; |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 803 | mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 804 | return count; |
| 805 | } |
| 806 | |
| 807 | static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store); |
| 808 | |
| 809 | static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf) |
| 810 | { |
| 811 | return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0); |
| 812 | } |
| 813 | |
| 814 | static ssize_t ap_poll_thread_store(struct bus_type *bus, |
| 815 | const char *buf, size_t count) |
| 816 | { |
| 817 | int flag, rc; |
| 818 | |
| 819 | if (sscanf(buf, "%d\n", &flag) != 1) |
| 820 | return -EINVAL; |
| 821 | if (flag) { |
| 822 | rc = ap_poll_thread_start(); |
| 823 | if (rc) |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 824 | count = rc; |
| 825 | } else |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 826 | ap_poll_thread_stop(); |
| 827 | return count; |
| 828 | } |
| 829 | |
| 830 | static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store); |
| 831 | |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 832 | static ssize_t poll_timeout_show(struct bus_type *bus, char *buf) |
| 833 | { |
| 834 | return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout); |
| 835 | } |
| 836 | |
| 837 | static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf, |
| 838 | size_t count) |
| 839 | { |
| 840 | unsigned long long time; |
| 841 | ktime_t hr_time; |
| 842 | |
| 843 | /* 120 seconds = maximum poll interval */ |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 844 | if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 || |
| 845 | time > 120000000000ULL) |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 846 | return -EINVAL; |
| 847 | poll_timeout = time; |
| 848 | hr_time = ktime_set(0, poll_timeout); |
| 849 | |
Ingo Tuchscherer | 8cc2af7 | 2015-04-28 10:31:44 +0200 | [diff] [blame] | 850 | spin_lock_bh(&ap_poll_timer_lock); |
| 851 | hrtimer_cancel(&ap_poll_timer); |
| 852 | hrtimer_set_expires(&ap_poll_timer, hr_time); |
| 853 | hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS); |
| 854 | spin_unlock_bh(&ap_poll_timer_lock); |
| 855 | |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 856 | return count; |
| 857 | } |
| 858 | |
| 859 | static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store); |
| 860 | |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 861 | static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf) |
| 862 | { |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 863 | int max_domain_id; |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 864 | |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 865 | if (ap_configuration) |
| 866 | max_domain_id = ap_max_domain_id ? : -1; |
| 867 | else |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 868 | max_domain_id = 15; |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 869 | return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id); |
| 870 | } |
| 871 | |
| 872 | static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL); |
| 873 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 874 | static struct bus_attribute *const ap_bus_attrs[] = { |
| 875 | &bus_attr_ap_domain, |
Ingo Tuchscherer | 91f3e3e | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 876 | &bus_attr_ap_control_domain_mask, |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 877 | &bus_attr_ap_usage_domain_mask, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 878 | &bus_attr_config_time, |
| 879 | &bus_attr_poll_thread, |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 880 | &bus_attr_ap_interrupts, |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 881 | &bus_attr_poll_timeout, |
Ingo Tuchscherer | 5bc334b | 2015-01-23 13:27:04 +0100 | [diff] [blame] | 882 | &bus_attr_ap_max_domain_id, |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 883 | NULL, |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 884 | }; |
| 885 | |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 886 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 887 | * ap_select_domain(): Select an AP domain. |
| 888 | * |
| 889 | * Pick one of the 16 AP domains. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 890 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 891 | static int ap_select_domain(void) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 892 | { |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 893 | int count, max_count, best_domain; |
| 894 | struct ap_queue_status status; |
| 895 | int i, j; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 896 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 897 | /* |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 898 | * We want to use a single domain. Either the one specified with |
| 899 | * the "domain=" parameter or the domain with the maximum number |
| 900 | * of devices. |
| 901 | */ |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 902 | spin_lock_bh(&ap_domain_lock); |
| 903 | if (ap_domain_index >= 0) { |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 904 | /* Domain has already been selected. */ |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 905 | spin_unlock_bh(&ap_domain_lock); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 906 | return 0; |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 907 | } |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 908 | best_domain = -1; |
| 909 | max_count = 0; |
| 910 | for (i = 0; i < AP_DOMAINS; i++) { |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 911 | if (!ap_test_config_domain(i)) |
| 912 | continue; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 913 | count = 0; |
| 914 | for (j = 0; j < AP_DEVICES; j++) { |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 915 | if (!ap_test_config_card_id(j)) |
| 916 | continue; |
Martin Schwidefsky | 6acbe21 | 2015-06-26 15:40:41 +0200 | [diff] [blame] | 917 | status = ap_test_queue(AP_MKQID(j, i), NULL); |
| 918 | if (status.response_code != AP_RESPONSE_NORMAL) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 919 | continue; |
| 920 | count++; |
| 921 | } |
| 922 | if (count > max_count) { |
| 923 | max_count = count; |
| 924 | best_domain = i; |
| 925 | } |
| 926 | } |
| 927 | if (best_domain >= 0){ |
| 928 | ap_domain_index = best_domain; |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 929 | spin_unlock_bh(&ap_domain_lock); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 930 | return 0; |
| 931 | } |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 932 | spin_unlock_bh(&ap_domain_lock); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 933 | return -ENODEV; |
| 934 | } |
| 935 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 936 | /* |
| 937 | * helper function to be used with bus_find_dev |
| 938 | * matches for the card device with the given id |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 939 | */ |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 940 | static int __match_card_device_with_id(struct device *dev, void *data) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 941 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 942 | return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 945 | /* helper function to be used with bus_find_dev |
| 946 | * matches for the queue device with a given qid |
| 947 | */ |
| 948 | static int __match_queue_device_with_qid(struct device *dev, void *data) |
| 949 | { |
| 950 | return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data; |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * ap_scan_bus(): Scan the AP bus for new devices |
| 955 | * Runs periodically, workqueue timer (ap_config_time) |
| 956 | */ |
Al Viro | 4927b3f | 2006-12-06 19:18:20 +0000 | [diff] [blame] | 957 | static void ap_scan_bus(struct work_struct *unused) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 958 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 959 | struct ap_queue *aq; |
| 960 | struct ap_card *ac; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 961 | struct device *dev; |
| 962 | ap_qid_t qid; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 963 | int depth = 0, type = 0; |
| 964 | unsigned int functions = 0; |
| 965 | int rc, id, dom, borked, domains; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 966 | |
Holger Dengler | 7501455 | 2012-08-28 16:41:50 +0200 | [diff] [blame] | 967 | ap_query_configuration(); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 968 | if (ap_select_domain() != 0) |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 969 | goto out; |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 970 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 971 | for (id = 0; id < AP_DEVICES; id++) { |
| 972 | /* check if device is registered */ |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 973 | dev = bus_find_device(&ap_bus_type, NULL, |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 974 | (void *)(long) id, |
| 975 | __match_card_device_with_id); |
| 976 | ac = dev ? to_ap_card(dev) : NULL; |
| 977 | if (!ap_test_config_card_id(id)) { |
| 978 | if (dev) { |
| 979 | /* Card device has been removed from |
| 980 | * configuration, remove the belonging |
| 981 | * queue devices. |
| 982 | */ |
| 983 | bus_for_each_dev(&ap_bus_type, NULL, |
| 984 | (void *)(long) id, |
| 985 | __ap_queue_devices_with_id_unregister); |
| 986 | /* now remove the card device */ |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 987 | device_unregister(dev); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 988 | put_device(dev); |
| 989 | } |
| 990 | continue; |
| 991 | } |
| 992 | /* According to the configuration there should be a card |
| 993 | * device, so check if there is at least one valid queue |
| 994 | * and maybe create queue devices and the card device. |
| 995 | */ |
| 996 | domains = 0; |
| 997 | for (dom = 0; dom < AP_DOMAINS; dom++) { |
| 998 | qid = AP_MKQID(id, dom); |
| 999 | dev = bus_find_device(&ap_bus_type, NULL, |
| 1000 | (void *)(long) qid, |
| 1001 | __match_queue_device_with_qid); |
| 1002 | aq = dev ? to_ap_queue(dev) : NULL; |
| 1003 | if (!ap_test_config_domain(dom)) { |
| 1004 | if (dev) { |
| 1005 | /* Queue device exists but has been |
| 1006 | * removed from configuration. |
| 1007 | */ |
| 1008 | device_unregister(dev); |
| 1009 | put_device(dev); |
| 1010 | } |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1011 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1012 | } |
| 1013 | rc = ap_query_queue(qid, &depth, &type, &functions); |
| 1014 | if (dev) { |
| 1015 | spin_lock_bh(&aq->lock); |
| 1016 | if (rc == -ENODEV || |
| 1017 | /* adapter reconfiguration */ |
| 1018 | (ac && ac->functions != functions)) |
| 1019 | aq->state = AP_STATE_BORKED; |
| 1020 | borked = aq->state == AP_STATE_BORKED; |
| 1021 | spin_unlock_bh(&aq->lock); |
| 1022 | if (borked) /* Remove broken device */ |
| 1023 | device_unregister(dev); |
| 1024 | put_device(dev); |
| 1025 | if (!borked) { |
| 1026 | domains++; |
| 1027 | continue; |
| 1028 | } |
| 1029 | } |
| 1030 | if (rc) |
| 1031 | continue; |
| 1032 | /* new queue device needed */ |
| 1033 | if (!ac) { |
| 1034 | /* but first create the card device */ |
| 1035 | ac = ap_card_create(id, depth, |
| 1036 | type, functions); |
| 1037 | if (!ac) |
| 1038 | continue; |
| 1039 | ac->ap_dev.device.bus = &ap_bus_type; |
| 1040 | ac->ap_dev.device.parent = ap_root_device; |
| 1041 | dev_set_name(&ac->ap_dev.device, |
| 1042 | "card%02x", id); |
| 1043 | /* Register card with AP bus */ |
| 1044 | rc = device_register(&ac->ap_dev.device); |
| 1045 | if (rc) { |
| 1046 | put_device(&ac->ap_dev.device); |
| 1047 | ac = NULL; |
| 1048 | break; |
| 1049 | } |
| 1050 | /* get it and thus adjust reference counter */ |
| 1051 | get_device(&ac->ap_dev.device); |
| 1052 | /* Add card device to card list */ |
| 1053 | spin_lock_bh(&ap_list_lock); |
| 1054 | list_add(&ac->list, &ap_card_list); |
| 1055 | spin_unlock_bh(&ap_list_lock); |
| 1056 | } |
| 1057 | /* now create the new queue device */ |
| 1058 | aq = ap_queue_create(qid, type); |
| 1059 | if (!aq) |
| 1060 | continue; |
| 1061 | aq->card = ac; |
| 1062 | aq->ap_dev.device.bus = &ap_bus_type; |
| 1063 | aq->ap_dev.device.parent = &ac->ap_dev.device; |
| 1064 | dev_set_name(&aq->ap_dev.device, |
| 1065 | "%02x.%04x", id, dom); |
| 1066 | /* Add queue device to card queue list */ |
| 1067 | spin_lock_bh(&ap_list_lock); |
| 1068 | list_add(&aq->list, &ac->queues); |
| 1069 | spin_unlock_bh(&ap_list_lock); |
| 1070 | /* Start with a device reset */ |
| 1071 | spin_lock_bh(&aq->lock); |
| 1072 | ap_wait(ap_sm_event(aq, AP_EVENT_POLL)); |
| 1073 | spin_unlock_bh(&aq->lock); |
| 1074 | /* Register device */ |
| 1075 | rc = device_register(&aq->ap_dev.device); |
| 1076 | if (rc) { |
| 1077 | spin_lock_bh(&ap_list_lock); |
| 1078 | list_del_init(&aq->list); |
| 1079 | spin_unlock_bh(&ap_list_lock); |
| 1080 | put_device(&aq->ap_dev.device); |
| 1081 | continue; |
| 1082 | } |
| 1083 | domains++; |
| 1084 | } /* end domain loop */ |
| 1085 | if (ac) { |
| 1086 | /* remove card dev if there are no queue devices */ |
| 1087 | if (!domains) |
| 1088 | device_unregister(&ac->ap_dev.device); |
| 1089 | put_device(&ac->ap_dev.device); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1090 | } |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1091 | } /* end device loop */ |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 1092 | out: |
| 1093 | mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1096 | static void ap_config_timeout(unsigned long ptr) |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1097 | { |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1098 | if (ap_suspend_flag) |
| 1099 | return; |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 1100 | queue_work(system_long_wq, &ap_scan_work); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1103 | static void ap_reset_domain(void) |
| 1104 | { |
| 1105 | int i; |
| 1106 | |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1107 | if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index)) |
| 1108 | return; |
| 1109 | for (i = 0; i < AP_DEVICES; i++) |
Martin Schwidefsky | 0db7855 | 2016-09-21 12:48:54 +0200 | [diff] [blame] | 1110 | ap_rapq(AP_MKQID(i, ap_domain_index)); |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static void ap_reset_all(void) |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1114 | { |
| 1115 | int i, j; |
| 1116 | |
Ingo Tuchscherer | 170387a | 2014-09-08 13:24:13 +0200 | [diff] [blame] | 1117 | for (i = 0; i < AP_DOMAINS; i++) { |
| 1118 | if (!ap_test_config_domain(i)) |
| 1119 | continue; |
| 1120 | for (j = 0; j < AP_DEVICES; j++) { |
| 1121 | if (!ap_test_config_card_id(j)) |
| 1122 | continue; |
Martin Schwidefsky | 0db7855 | 2016-09-21 12:48:54 +0200 | [diff] [blame] | 1123 | ap_rapq(AP_MKQID(j, i)); |
Ingo Tuchscherer | 170387a | 2014-09-08 13:24:13 +0200 | [diff] [blame] | 1124 | } |
| 1125 | } |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | static struct reset_call ap_reset_call = { |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1129 | .fn = ap_reset_all, |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1130 | }; |
| 1131 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1132 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1133 | * ap_module_init(): The module initialization code. |
| 1134 | * |
| 1135 | * Initializes the module. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1136 | */ |
| 1137 | int __init ap_module_init(void) |
| 1138 | { |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1139 | int max_domain_id; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1140 | int rc, i; |
| 1141 | |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1142 | if (ap_instructions_available() != 0) { |
| 1143 | pr_warn("The hardware system does not support AP instructions\n"); |
| 1144 | return -ENODEV; |
| 1145 | } |
| 1146 | |
| 1147 | /* Get AP configuration data if available */ |
| 1148 | ap_init_configuration(); |
| 1149 | |
| 1150 | if (ap_configuration) |
| 1151 | max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1); |
| 1152 | else |
| 1153 | max_domain_id = 15; |
| 1154 | if (ap_domain_index < -1 || ap_domain_index > max_domain_id) { |
| 1155 | pr_warn("%d is not a valid cryptographic domain\n", |
| 1156 | ap_domain_index); |
Wei Yongjun | 33c388b | 2016-07-31 11:47:06 +0200 | [diff] [blame] | 1157 | rc = -EINVAL; |
| 1158 | goto out_free; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1159 | } |
Felix Beck | 5314af6 | 2009-09-22 22:58:51 +0200 | [diff] [blame] | 1160 | /* In resume callback we need to know if the user had set the domain. |
| 1161 | * If so, we can not just reset it. |
| 1162 | */ |
| 1163 | if (ap_domain_index >= 0) |
| 1164 | user_set_domain = 1; |
| 1165 | |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 1166 | if (ap_interrupts_available()) { |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 1167 | rc = register_adapter_interrupt(&ap_airq); |
| 1168 | ap_airq_flag = (rc == 0); |
Felix Beck | cb17a63 | 2008-12-25 13:38:41 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1171 | register_reset_call(&ap_reset_call); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1172 | |
| 1173 | /* Create /sys/bus/ap. */ |
| 1174 | rc = bus_register(&ap_bus_type); |
| 1175 | if (rc) |
| 1176 | goto out; |
| 1177 | for (i = 0; ap_bus_attrs[i]; i++) { |
| 1178 | rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]); |
| 1179 | if (rc) |
| 1180 | goto out_bus; |
| 1181 | } |
| 1182 | |
| 1183 | /* Create /sys/devices/ap. */ |
Mark McLoughlin | 035da16 | 2008-12-15 12:58:29 +0000 | [diff] [blame] | 1184 | ap_root_device = root_device_register("ap"); |
Thomas Meyer | ba8da21 | 2013-06-01 11:51:13 +0200 | [diff] [blame] | 1185 | rc = PTR_RET(ap_root_device); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1186 | if (rc) |
| 1187 | goto out_bus; |
| 1188 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1189 | /* Setup the AP bus rescan timer. */ |
Martin Schwidefsky | fcd0d1f | 2015-07-23 10:55:59 +0200 | [diff] [blame] | 1190 | setup_timer(&ap_config_timer, ap_config_timeout, 0); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1191 | |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1192 | /* |
| 1193 | * Setup the high resultion poll timer. |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1194 | * If we are running under z/VM adjust polling to z/VM polling rate. |
| 1195 | */ |
| 1196 | if (MACHINE_IS_VM) |
| 1197 | poll_timeout = 1500000; |
Felix Beck | 9352131 | 2009-12-07 12:52:00 +0100 | [diff] [blame] | 1198 | spin_lock_init(&ap_poll_timer_lock); |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1199 | hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 1200 | ap_poll_timer.function = ap_poll_timeout; |
| 1201 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1202 | /* Start the low priority AP bus poll thread. */ |
| 1203 | if (ap_thread_flag) { |
| 1204 | rc = ap_poll_thread_start(); |
| 1205 | if (rc) |
| 1206 | goto out_work; |
| 1207 | } |
| 1208 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1209 | rc = register_pm_notifier(&ap_power_notifier); |
| 1210 | if (rc) |
| 1211 | goto out_pm; |
| 1212 | |
Martin Schwidefsky | 8139b89 | 2015-07-27 12:47:40 +0200 | [diff] [blame] | 1213 | queue_work(system_long_wq, &ap_scan_work); |
Sascha Silbe | e387753 | 2015-10-27 18:29:52 +0100 | [diff] [blame] | 1214 | initialised = true; |
Martin Schwidefsky | 3f3007a | 2015-09-14 17:01:23 +0200 | [diff] [blame] | 1215 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1216 | return 0; |
| 1217 | |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1218 | out_pm: |
| 1219 | ap_poll_thread_stop(); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1220 | out_work: |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1221 | hrtimer_cancel(&ap_poll_timer); |
Mark McLoughlin | 035da16 | 2008-12-15 12:58:29 +0000 | [diff] [blame] | 1222 | root_device_unregister(ap_root_device); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1223 | out_bus: |
| 1224 | while (i--) |
| 1225 | bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); |
| 1226 | bus_unregister(&ap_bus_type); |
| 1227 | out: |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1228 | unregister_reset_call(&ap_reset_call); |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 1229 | if (ap_using_interrupts()) |
| 1230 | unregister_adapter_interrupt(&ap_airq); |
Wei Yongjun | 33c388b | 2016-07-31 11:47:06 +0200 | [diff] [blame] | 1231 | out_free: |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1232 | kfree(ap_configuration); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1233 | return rc; |
| 1234 | } |
| 1235 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1236 | /** |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1237 | * ap_modules_exit(): The module termination code |
| 1238 | * |
| 1239 | * Terminates the module. |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1240 | */ |
| 1241 | void ap_module_exit(void) |
| 1242 | { |
| 1243 | int i; |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1244 | |
Sascha Silbe | e387753 | 2015-10-27 18:29:52 +0100 | [diff] [blame] | 1245 | initialised = false; |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1246 | ap_reset_domain(); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1247 | ap_poll_thread_stop(); |
| 1248 | del_timer_sync(&ap_config_timer); |
Felix Beck | fe13723 | 2008-07-14 09:59:08 +0200 | [diff] [blame] | 1249 | hrtimer_cancel(&ap_poll_timer); |
Ralph Wuerthner | 13e742b | 2006-12-15 17:18:17 +0100 | [diff] [blame] | 1250 | tasklet_kill(&ap_tasklet); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1251 | |
| 1252 | /* first remove queue devices */ |
| 1253 | bus_for_each_dev(&ap_bus_type, NULL, NULL, |
| 1254 | __ap_queue_devices_unregister); |
| 1255 | /* now remove the card devices */ |
| 1256 | bus_for_each_dev(&ap_bus_type, NULL, NULL, |
| 1257 | __ap_card_devices_unregister); |
| 1258 | |
| 1259 | /* remove bus attributes */ |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1260 | for (i = 0; ap_bus_attrs[i]; i++) |
| 1261 | bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); |
Martin Schwidefsky | 83e9d5d | 2015-07-17 13:43:18 +0200 | [diff] [blame] | 1262 | unregister_pm_notifier(&ap_power_notifier); |
Ingo Tuchscherer | f60b8d4 | 2015-04-30 15:36:48 +0200 | [diff] [blame] | 1263 | root_device_unregister(ap_root_device); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1264 | bus_unregister(&ap_bus_type); |
Martin Schwidefsky | 889875a | 2015-06-26 16:55:35 +0200 | [diff] [blame] | 1265 | kfree(ap_configuration); |
Ralph Wuerthner | 85eca85 | 2006-12-08 15:54:07 +0100 | [diff] [blame] | 1266 | unregister_reset_call(&ap_reset_call); |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 1267 | if (ap_using_interrupts()) |
| 1268 | unregister_adapter_interrupt(&ap_airq); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1269 | } |
| 1270 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 1271 | module_init(ap_module_init); |
| 1272 | module_exit(ap_module_exit); |