Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 2 | /* |
Jan Glauber | 7441b06 | 2012-11-29 14:35:47 +0100 | [diff] [blame] | 3 | * Copyright IBM Corp. 2007,2012 |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 4 | * |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 5 | * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>, |
| 6 | * Peter Oberparleiter <peter.oberparleiter@de.ibm.com> |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Martin Schwidefsky | b3ff088 | 2008-12-25 13:39:48 +0100 | [diff] [blame] | 9 | #define KMSG_COMPONENT "sclp_cmd" |
| 10 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 11 | |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 12 | #include <linux/completion.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/errno.h> |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 15 | #include <linux/err.h> |
Jan Glauber | 7441b06 | 2012-11-29 14:35:47 +0100 | [diff] [blame] | 16 | #include <linux/export.h> |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/string.h> |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 19 | #include <linux/mm.h> |
| 20 | #include <linux/mmzone.h> |
| 21 | #include <linux/memory.h> |
Christian Borntraeger | 1526bf9 | 2012-05-15 14:15:25 +0200 | [diff] [blame] | 22 | #include <linux/module.h> |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 24 | #include <asm/ctl_reg.h> |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 25 | #include <asm/chpid.h> |
| 26 | #include <asm/setup.h> |
| 27 | #include <asm/page.h> |
| 28 | #include <asm/sclp.h> |
Michael Holzheu | c29a7ba | 2014-03-06 18:47:21 +0100 | [diff] [blame] | 29 | #include <asm/numa.h> |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 30 | |
Martin Schwidefsky | b3ff088 | 2008-12-25 13:39:48 +0100 | [diff] [blame] | 31 | #include "sclp.h" |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 32 | |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 33 | static void sclp_sync_callback(struct sclp_req *req, void *data) |
| 34 | { |
| 35 | struct completion *completion = data; |
| 36 | |
| 37 | complete(completion); |
| 38 | } |
| 39 | |
Michael Holzheu | d475f94 | 2013-06-06 09:52:08 +0200 | [diff] [blame] | 40 | int sclp_sync_request(sclp_cmdw_t cmd, void *sccb) |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 41 | { |
Gerald Schaefer | 9f0128f | 2014-03-31 16:18:29 +0200 | [diff] [blame] | 42 | return sclp_sync_request_timeout(cmd, sccb, 0); |
| 43 | } |
| 44 | |
| 45 | int sclp_sync_request_timeout(sclp_cmdw_t cmd, void *sccb, int timeout) |
| 46 | { |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 47 | struct completion completion; |
| 48 | struct sclp_req *request; |
| 49 | int rc; |
| 50 | |
| 51 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 52 | if (!request) |
| 53 | return -ENOMEM; |
Gerald Schaefer | 9f0128f | 2014-03-31 16:18:29 +0200 | [diff] [blame] | 54 | if (timeout) |
| 55 | request->queue_timeout = timeout; |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 56 | request->command = cmd; |
| 57 | request->sccb = sccb; |
| 58 | request->status = SCLP_REQ_FILLED; |
| 59 | request->callback = sclp_sync_callback; |
| 60 | request->callback_data = &completion; |
| 61 | init_completion(&completion); |
| 62 | |
| 63 | /* Perform sclp request. */ |
| 64 | rc = sclp_add_request(request); |
| 65 | if (rc) |
| 66 | goto out; |
| 67 | wait_for_completion(&completion); |
| 68 | |
| 69 | /* Check response. */ |
| 70 | if (request->status != SCLP_REQ_DONE) { |
Joe Perches | baebc70 | 2016-03-03 20:49:57 -0800 | [diff] [blame] | 71 | pr_warn("sync request failed (cmd=0x%08x, status=0x%02x)\n", |
| 72 | cmd, request->status); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 73 | rc = -EIO; |
| 74 | } |
| 75 | out: |
| 76 | kfree(request); |
| 77 | return rc; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * CPU configuration related functions. |
| 82 | */ |
| 83 | |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 84 | #define SCLP_CMDW_CONFIGURE_CPU 0x00110001 |
| 85 | #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001 |
| 86 | |
Heiko Carstens | af51160e | 2016-12-03 09:48:01 +0100 | [diff] [blame] | 87 | int _sclp_get_core_info(struct sclp_core_info *info) |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 88 | { |
| 89 | int rc; |
| 90 | struct read_cpu_info_sccb *sccb; |
| 91 | |
| 92 | if (!SCLP_HAS_CPU_INFO) |
| 93 | return -EOPNOTSUPP; |
Heiko Carstens | 48483b3 | 2008-01-26 14:11:05 +0100 | [diff] [blame] | 94 | sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 95 | if (!sccb) |
| 96 | return -ENOMEM; |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 97 | sccb->header.length = sizeof(*sccb); |
Gerald Schaefer | 9f0128f | 2014-03-31 16:18:29 +0200 | [diff] [blame] | 98 | rc = sclp_sync_request_timeout(SCLP_CMDW_READ_CPU_INFO, sccb, |
| 99 | SCLP_QUEUE_INTERVAL); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 100 | if (rc) |
| 101 | goto out; |
| 102 | if (sccb->header.response_code != 0x0010) { |
Joe Perches | baebc70 | 2016-03-03 20:49:57 -0800 | [diff] [blame] | 103 | pr_warn("readcpuinfo failed (response=0x%04x)\n", |
| 104 | sccb->header.response_code); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 105 | rc = -EIO; |
| 106 | goto out; |
| 107 | } |
Martin Schwidefsky | d08d943 | 2015-06-18 14:23:00 +0200 | [diff] [blame] | 108 | sclp_fill_core_info(info, sccb); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 109 | out: |
| 110 | free_page((unsigned long) sccb); |
| 111 | return rc; |
| 112 | } |
| 113 | |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 114 | struct cpu_configure_sccb { |
| 115 | struct sccb_header header; |
| 116 | } __attribute__((packed, aligned(8))); |
| 117 | |
Martin Schwidefsky | d08d943 | 2015-06-18 14:23:00 +0200 | [diff] [blame] | 118 | static int do_core_configure(sclp_cmdw_t cmd) |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 119 | { |
| 120 | struct cpu_configure_sccb *sccb; |
| 121 | int rc; |
| 122 | |
| 123 | if (!SCLP_HAS_CPU_RECONFIG) |
| 124 | return -EOPNOTSUPP; |
| 125 | /* |
| 126 | * This is not going to cross a page boundary since we force |
| 127 | * kmalloc to have a minimum alignment of 8 bytes on s390. |
| 128 | */ |
| 129 | sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA); |
| 130 | if (!sccb) |
| 131 | return -ENOMEM; |
| 132 | sccb->header.length = sizeof(*sccb); |
Gerald Schaefer | 9f0128f | 2014-03-31 16:18:29 +0200 | [diff] [blame] | 133 | rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 134 | if (rc) |
| 135 | goto out; |
| 136 | switch (sccb->header.response_code) { |
| 137 | case 0x0020: |
| 138 | case 0x0120: |
| 139 | break; |
| 140 | default: |
Joe Perches | baebc70 | 2016-03-03 20:49:57 -0800 | [diff] [blame] | 141 | pr_warn("configure cpu failed (cmd=0x%08x, response=0x%04x)\n", |
| 142 | cmd, sccb->header.response_code); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 143 | rc = -EIO; |
| 144 | break; |
| 145 | } |
| 146 | out: |
| 147 | kfree(sccb); |
| 148 | return rc; |
| 149 | } |
| 150 | |
Martin Schwidefsky | d08d943 | 2015-06-18 14:23:00 +0200 | [diff] [blame] | 151 | int sclp_core_configure(u8 core) |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 152 | { |
Martin Schwidefsky | d08d943 | 2015-06-18 14:23:00 +0200 | [diff] [blame] | 153 | return do_core_configure(SCLP_CMDW_CONFIGURE_CPU | core << 8); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Martin Schwidefsky | d08d943 | 2015-06-18 14:23:00 +0200 | [diff] [blame] | 156 | int sclp_core_deconfigure(u8 core) |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 157 | { |
Martin Schwidefsky | d08d943 | 2015-06-18 14:23:00 +0200 | [diff] [blame] | 158 | return do_core_configure(SCLP_CMDW_DECONFIGURE_CPU | core << 8); |
Heiko Carstens | 08d0796 | 2008-01-26 14:10:56 +0100 | [diff] [blame] | 159 | } |
Heiko Carstens | 4b28a8f | 2008-01-26 14:10:57 +0100 | [diff] [blame] | 160 | |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 161 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 162 | |
| 163 | static DEFINE_MUTEX(sclp_mem_mutex); |
| 164 | static LIST_HEAD(sclp_mem_list); |
| 165 | static u8 sclp_max_storage_id; |
Joe Perches | b8a43b0 | 2015-05-19 18:37:56 -0700 | [diff] [blame] | 166 | static DECLARE_BITMAP(sclp_storage_ids, 256); |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 167 | static int sclp_mem_state_changed; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 168 | |
| 169 | struct memory_increment { |
| 170 | struct list_head list; |
| 171 | u16 rn; |
| 172 | int standby; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | struct assign_storage_sccb { |
| 176 | struct sccb_header header; |
| 177 | u16 rn; |
| 178 | } __packed; |
| 179 | |
Heiko Carstens | 9c95258 | 2010-03-24 11:49:55 +0100 | [diff] [blame] | 180 | int arch_get_memory_phys_device(unsigned long start_pfn) |
| 181 | { |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 182 | if (!sclp.rzm) |
Heiko Carstens | 9c95258 | 2010-03-24 11:49:55 +0100 | [diff] [blame] | 183 | return 0; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 184 | return PFN_PHYS(start_pfn) >> ilog2(sclp.rzm); |
Heiko Carstens | 9c95258 | 2010-03-24 11:49:55 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 187 | static unsigned long long rn2addr(u16 rn) |
| 188 | { |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 189 | return (unsigned long long) (rn - 1) * sclp.rzm; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int do_assign_storage(sclp_cmdw_t cmd, u16 rn) |
| 193 | { |
| 194 | struct assign_storage_sccb *sccb; |
| 195 | int rc; |
| 196 | |
| 197 | sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 198 | if (!sccb) |
| 199 | return -ENOMEM; |
| 200 | sccb->header.length = PAGE_SIZE; |
| 201 | sccb->rn = rn; |
Gerald Schaefer | 9f0128f | 2014-03-31 16:18:29 +0200 | [diff] [blame] | 202 | rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL); |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 203 | if (rc) |
| 204 | goto out; |
| 205 | switch (sccb->header.response_code) { |
| 206 | case 0x0020: |
| 207 | case 0x0120: |
| 208 | break; |
| 209 | default: |
Joe Perches | baebc70 | 2016-03-03 20:49:57 -0800 | [diff] [blame] | 210 | pr_warn("assign storage failed (cmd=0x%08x, response=0x%04x, rn=0x%04x)\n", |
| 211 | cmd, sccb->header.response_code, rn); |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 212 | rc = -EIO; |
| 213 | break; |
| 214 | } |
| 215 | out: |
| 216 | free_page((unsigned long) sccb); |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | static int sclp_assign_storage(u16 rn) |
| 221 | { |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 222 | unsigned long long start; |
Heiko Carstens | 0cd2f62 | 2012-05-14 11:04:10 +0200 | [diff] [blame] | 223 | int rc; |
| 224 | |
| 225 | rc = do_assign_storage(0x000d0001, rn); |
| 226 | if (rc) |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 227 | return rc; |
| 228 | start = rn2addr(rn); |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 229 | storage_key_init_range(start, start + sclp.rzm); |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 230 | return 0; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int sclp_unassign_storage(u16 rn) |
| 234 | { |
| 235 | return do_assign_storage(0x000c0001, rn); |
| 236 | } |
| 237 | |
| 238 | struct attach_storage_sccb { |
| 239 | struct sccb_header header; |
| 240 | u16 :16; |
| 241 | u16 assigned; |
| 242 | u32 :32; |
| 243 | u32 entries[0]; |
| 244 | } __packed; |
| 245 | |
| 246 | static int sclp_attach_storage(u8 id) |
| 247 | { |
| 248 | struct attach_storage_sccb *sccb; |
| 249 | int rc; |
| 250 | int i; |
| 251 | |
| 252 | sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 253 | if (!sccb) |
| 254 | return -ENOMEM; |
| 255 | sccb->header.length = PAGE_SIZE; |
Martin Schwidefsky | 45be0a02 | 2017-02-06 15:56:14 +0100 | [diff] [blame] | 256 | sccb->header.function_code = 0x40; |
Gerald Schaefer | 9f0128f | 2014-03-31 16:18:29 +0200 | [diff] [blame] | 257 | rc = sclp_sync_request_timeout(0x00080001 | id << 8, sccb, |
| 258 | SCLP_QUEUE_INTERVAL); |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 259 | if (rc) |
| 260 | goto out; |
| 261 | switch (sccb->header.response_code) { |
| 262 | case 0x0020: |
| 263 | set_bit(id, sclp_storage_ids); |
Heiko Carstens | 8adb4ca | 2011-08-24 17:15:13 +0200 | [diff] [blame] | 264 | for (i = 0; i < sccb->assigned; i++) { |
| 265 | if (sccb->entries[i]) |
| 266 | sclp_unassign_storage(sccb->entries[i] >> 16); |
| 267 | } |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 268 | break; |
| 269 | default: |
| 270 | rc = -EIO; |
| 271 | break; |
| 272 | } |
| 273 | out: |
| 274 | free_page((unsigned long) sccb); |
| 275 | return rc; |
| 276 | } |
| 277 | |
| 278 | static int sclp_mem_change_state(unsigned long start, unsigned long size, |
| 279 | int online) |
| 280 | { |
| 281 | struct memory_increment *incr; |
| 282 | unsigned long long istart; |
| 283 | int rc = 0; |
| 284 | |
| 285 | list_for_each_entry(incr, &sclp_mem_list, list) { |
| 286 | istart = rn2addr(incr->rn); |
| 287 | if (start + size - 1 < istart) |
| 288 | break; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 289 | if (start > istart + sclp.rzm - 1) |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 290 | continue; |
Heiko Carstens | 1553556 | 2013-05-02 09:20:30 +0200 | [diff] [blame] | 291 | if (online) |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 292 | rc |= sclp_assign_storage(incr->rn); |
Heiko Carstens | 1553556 | 2013-05-02 09:20:30 +0200 | [diff] [blame] | 293 | else |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 294 | sclp_unassign_storage(incr->rn); |
Michael Holzheu | 8741ce6 | 2015-04-01 18:49:11 +0200 | [diff] [blame] | 295 | if (rc == 0) |
| 296 | incr->standby = online ? 0 : 1; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 297 | } |
| 298 | return rc ? -EIO : 0; |
| 299 | } |
| 300 | |
Michael Holzheu | 8741ce6 | 2015-04-01 18:49:11 +0200 | [diff] [blame] | 301 | static bool contains_standby_increment(unsigned long start, unsigned long end) |
| 302 | { |
| 303 | struct memory_increment *incr; |
| 304 | unsigned long istart; |
| 305 | |
| 306 | list_for_each_entry(incr, &sclp_mem_list, list) { |
| 307 | istart = rn2addr(incr->rn); |
| 308 | if (end - 1 < istart) |
| 309 | continue; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 310 | if (start > istart + sclp.rzm - 1) |
Michael Holzheu | 8741ce6 | 2015-04-01 18:49:11 +0200 | [diff] [blame] | 311 | continue; |
| 312 | if (incr->standby) |
| 313 | return true; |
| 314 | } |
| 315 | return false; |
| 316 | } |
| 317 | |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 318 | static int sclp_mem_notifier(struct notifier_block *nb, |
| 319 | unsigned long action, void *data) |
| 320 | { |
| 321 | unsigned long start, size; |
| 322 | struct memory_notify *arg; |
| 323 | unsigned char id; |
| 324 | int rc = 0; |
| 325 | |
| 326 | arg = data; |
| 327 | start = arg->start_pfn << PAGE_SHIFT; |
| 328 | size = arg->nr_pages << PAGE_SHIFT; |
| 329 | mutex_lock(&sclp_mem_mutex); |
Akinobu Mita | fc89db4 | 2012-03-23 15:02:05 -0700 | [diff] [blame] | 330 | for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1) |
| 331 | sclp_attach_storage(id); |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 332 | switch (action) { |
Gerald Schaefer | 7e9238f | 2008-08-01 16:39:16 +0200 | [diff] [blame] | 333 | case MEM_GOING_OFFLINE: |
Michael Holzheu | 8741ce6 | 2015-04-01 18:49:11 +0200 | [diff] [blame] | 334 | /* |
| 335 | * We do not allow to set memory blocks offline that contain |
| 336 | * standby memory. This is done to simplify the "memory online" |
| 337 | * case. |
| 338 | */ |
| 339 | if (contains_standby_increment(start, start + size)) |
| 340 | rc = -EPERM; |
| 341 | break; |
| 342 | case MEM_ONLINE: |
Gerald Schaefer | 7e9238f | 2008-08-01 16:39:16 +0200 | [diff] [blame] | 343 | case MEM_CANCEL_OFFLINE: |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 344 | break; |
| 345 | case MEM_GOING_ONLINE: |
| 346 | rc = sclp_mem_change_state(start, size, 1); |
| 347 | break; |
| 348 | case MEM_CANCEL_ONLINE: |
| 349 | sclp_mem_change_state(start, size, 0); |
| 350 | break; |
Gerald Schaefer | 7e9238f | 2008-08-01 16:39:16 +0200 | [diff] [blame] | 351 | case MEM_OFFLINE: |
| 352 | sclp_mem_change_state(start, size, 0); |
| 353 | break; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 354 | default: |
| 355 | rc = -EINVAL; |
| 356 | break; |
| 357 | } |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 358 | if (!rc) |
| 359 | sclp_mem_state_changed = 1; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 360 | mutex_unlock(&sclp_mem_mutex); |
| 361 | return rc ? NOTIFY_BAD : NOTIFY_OK; |
| 362 | } |
| 363 | |
| 364 | static struct notifier_block sclp_mem_nb = { |
| 365 | .notifier_call = sclp_mem_notifier, |
| 366 | }; |
| 367 | |
Michael Holzheu | 8741ce6 | 2015-04-01 18:49:11 +0200 | [diff] [blame] | 368 | static void __init align_to_block_size(unsigned long long *start, |
Michael Holzheu | c29a7ba | 2014-03-06 18:47:21 +0100 | [diff] [blame] | 369 | unsigned long long *size, |
| 370 | unsigned long long alignment) |
Michael Holzheu | 8741ce6 | 2015-04-01 18:49:11 +0200 | [diff] [blame] | 371 | { |
Michael Holzheu | c29a7ba | 2014-03-06 18:47:21 +0100 | [diff] [blame] | 372 | unsigned long long start_align, size_align; |
Michael Holzheu | 8741ce6 | 2015-04-01 18:49:11 +0200 | [diff] [blame] | 373 | |
Michael Holzheu | 8741ce6 | 2015-04-01 18:49:11 +0200 | [diff] [blame] | 374 | start_align = roundup(*start, alignment); |
| 375 | size_align = rounddown(*start + *size, alignment) - start_align; |
| 376 | |
| 377 | pr_info("Standby memory at 0x%llx (%lluM of %lluM usable)\n", |
| 378 | *start, size_align >> 20, *size >> 20); |
| 379 | *start = start_align; |
| 380 | *size = size_align; |
| 381 | } |
| 382 | |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 383 | static void __init add_memory_merged(u16 rn) |
| 384 | { |
Michael Holzheu | c29a7ba | 2014-03-06 18:47:21 +0100 | [diff] [blame] | 385 | unsigned long long start, size, addr, block_size; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 386 | static u16 first_rn, num; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 387 | |
| 388 | if (rn && first_rn && (first_rn + num == rn)) { |
| 389 | num++; |
| 390 | return; |
| 391 | } |
| 392 | if (!first_rn) |
| 393 | goto skip_add; |
| 394 | start = rn2addr(first_rn); |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 395 | size = (unsigned long long) num * sclp.rzm; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 396 | if (start >= VMEM_MAX_PHYS) |
| 397 | goto skip_add; |
| 398 | if (start + size > VMEM_MAX_PHYS) |
| 399 | size = VMEM_MAX_PHYS - start; |
Heiko Carstens | 23d75d9 | 2009-02-19 15:19:01 +0100 | [diff] [blame] | 400 | if (memory_end_set && (start >= memory_end)) |
| 401 | goto skip_add; |
| 402 | if (memory_end_set && (start + size > memory_end)) |
| 403 | size = memory_end - start; |
Michael Holzheu | c29a7ba | 2014-03-06 18:47:21 +0100 | [diff] [blame] | 404 | block_size = memory_block_size_bytes(); |
| 405 | align_to_block_size(&start, &size, block_size); |
| 406 | if (!size) |
| 407 | goto skip_add; |
| 408 | for (addr = start; addr < start + size; addr += block_size) |
Heiko Carstens | 701dc81 | 2020-02-19 13:29:15 +0100 | [diff] [blame] | 409 | add_memory(0, addr, block_size); |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 410 | skip_add: |
| 411 | first_rn = rn; |
| 412 | num = 1; |
| 413 | } |
| 414 | |
| 415 | static void __init sclp_add_standby_memory(void) |
| 416 | { |
| 417 | struct memory_increment *incr; |
| 418 | |
| 419 | list_for_each_entry(incr, &sclp_mem_list, list) |
| 420 | if (incr->standby) |
| 421 | add_memory_merged(incr->rn); |
| 422 | add_memory_merged(0); |
| 423 | } |
| 424 | |
| 425 | static void __init insert_increment(u16 rn, int standby, int assigned) |
| 426 | { |
| 427 | struct memory_increment *incr, *new_incr; |
| 428 | struct list_head *prev; |
| 429 | u16 last_rn; |
| 430 | |
| 431 | new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL); |
| 432 | if (!new_incr) |
| 433 | return; |
| 434 | new_incr->rn = rn; |
| 435 | new_incr->standby = standby; |
| 436 | last_rn = 0; |
| 437 | prev = &sclp_mem_list; |
| 438 | list_for_each_entry(incr, &sclp_mem_list, list) { |
| 439 | if (assigned && incr->rn > rn) |
| 440 | break; |
| 441 | if (!assigned && incr->rn - last_rn > 1) |
| 442 | break; |
| 443 | last_rn = incr->rn; |
| 444 | prev = &incr->list; |
| 445 | } |
| 446 | if (!assigned) |
| 447 | new_incr->rn = last_rn + 1; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 448 | if (new_incr->rn > sclp.rnmax) { |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 449 | kfree(new_incr); |
| 450 | return; |
| 451 | } |
| 452 | list_add(&new_incr->list, prev); |
| 453 | } |
| 454 | |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 455 | static int sclp_mem_freeze(struct device *dev) |
| 456 | { |
| 457 | if (!sclp_mem_state_changed) |
| 458 | return 0; |
| 459 | pr_err("Memory hotplug state changed, suspend refused.\n"); |
| 460 | return -EPERM; |
| 461 | } |
| 462 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 463 | static const struct dev_pm_ops sclp_mem_pm_ops = { |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 464 | .freeze = sclp_mem_freeze, |
| 465 | }; |
| 466 | |
| 467 | static struct platform_driver sclp_mem_pdrv = { |
| 468 | .driver = { |
| 469 | .name = "sclp_mem", |
| 470 | .pm = &sclp_mem_pm_ops, |
| 471 | }, |
| 472 | }; |
| 473 | |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 474 | static int __init sclp_detect_standby_memory(void) |
| 475 | { |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 476 | struct platform_device *sclp_pdev; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 477 | struct read_storage_sccb *sccb; |
| 478 | int i, id, assigned, rc; |
| 479 | |
Michael Holzheu | 52319b4 | 2013-03-08 09:29:34 +0100 | [diff] [blame] | 480 | if (OLDMEM_BASE) /* No standby memory in kdump mode */ |
| 481 | return 0; |
David Hildenbrand | 78335a3 | 2015-05-06 09:17:51 +0200 | [diff] [blame] | 482 | if ((sclp.facilities & 0xe00000000000ULL) != 0xe00000000000ULL) |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 483 | return 0; |
| 484 | rc = -ENOMEM; |
| 485 | sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA); |
| 486 | if (!sccb) |
| 487 | goto out; |
| 488 | assigned = 0; |
| 489 | for (id = 0; id <= sclp_max_storage_id; id++) { |
| 490 | memset(sccb, 0, PAGE_SIZE); |
| 491 | sccb->header.length = PAGE_SIZE; |
Vasily Gorbik | fddbaa5 | 2018-04-11 18:42:37 +0200 | [diff] [blame] | 492 | rc = sclp_sync_request(SCLP_CMDW_READ_STORAGE_INFO | id << 8, sccb); |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 493 | if (rc) |
| 494 | goto out; |
| 495 | switch (sccb->header.response_code) { |
| 496 | case 0x0010: |
| 497 | set_bit(id, sclp_storage_ids); |
| 498 | for (i = 0; i < sccb->assigned; i++) { |
| 499 | if (!sccb->entries[i]) |
| 500 | continue; |
| 501 | assigned++; |
| 502 | insert_increment(sccb->entries[i] >> 16, 0, 1); |
| 503 | } |
| 504 | break; |
| 505 | case 0x0310: |
| 506 | break; |
| 507 | case 0x0410: |
| 508 | for (i = 0; i < sccb->assigned; i++) { |
| 509 | if (!sccb->entries[i]) |
| 510 | continue; |
| 511 | assigned++; |
| 512 | insert_increment(sccb->entries[i] >> 16, 1, 1); |
| 513 | } |
| 514 | break; |
| 515 | default: |
| 516 | rc = -EIO; |
| 517 | break; |
| 518 | } |
| 519 | if (!rc) |
| 520 | sclp_max_storage_id = sccb->max_id; |
| 521 | } |
| 522 | if (rc || list_empty(&sclp_mem_list)) |
| 523 | goto out; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 524 | for (i = 1; i <= sclp.rnmax - assigned; i++) |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 525 | insert_increment(0, 1, 0); |
| 526 | rc = register_memory_notifier(&sclp_mem_nb); |
| 527 | if (rc) |
| 528 | goto out; |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 529 | rc = platform_driver_register(&sclp_mem_pdrv); |
| 530 | if (rc) |
| 531 | goto out; |
| 532 | sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0); |
Duan Jiong | 5896f8f | 2014-04-11 13:41:54 +0200 | [diff] [blame] | 533 | rc = PTR_ERR_OR_ZERO(sclp_pdev); |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 534 | if (rc) |
| 535 | goto out_driver; |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 536 | sclp_add_standby_memory(); |
Gerald Schaefer | 0399790 | 2009-06-16 10:30:48 +0200 | [diff] [blame] | 537 | goto out; |
| 538 | out_driver: |
| 539 | platform_driver_unregister(&sclp_mem_pdrv); |
Heiko Carstens | e0bc249 | 2008-07-14 09:59:19 +0200 | [diff] [blame] | 540 | out: |
| 541 | free_page((unsigned long) sccb); |
| 542 | return rc; |
| 543 | } |
| 544 | __initcall(sclp_detect_standby_memory); |
| 545 | |
| 546 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
| 547 | |
Heiko Carstens | 4b28a8f | 2008-01-26 14:10:57 +0100 | [diff] [blame] | 548 | /* |
| 549 | * Channel path configuration related functions. |
| 550 | */ |
| 551 | |
| 552 | #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001 |
| 553 | #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001 |
| 554 | #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001 |
| 555 | |
| 556 | struct chp_cfg_sccb { |
| 557 | struct sccb_header header; |
| 558 | u8 ccm; |
| 559 | u8 reserved[6]; |
| 560 | u8 cssid; |
| 561 | } __attribute__((packed)); |
| 562 | |
| 563 | static int do_chp_configure(sclp_cmdw_t cmd) |
| 564 | { |
| 565 | struct chp_cfg_sccb *sccb; |
| 566 | int rc; |
| 567 | |
| 568 | if (!SCLP_HAS_CHP_RECONFIG) |
| 569 | return -EOPNOTSUPP; |
| 570 | /* Prepare sccb. */ |
| 571 | sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 572 | if (!sccb) |
| 573 | return -ENOMEM; |
| 574 | sccb->header.length = sizeof(*sccb); |
Michael Holzheu | d475f94 | 2013-06-06 09:52:08 +0200 | [diff] [blame] | 575 | rc = sclp_sync_request(cmd, sccb); |
Heiko Carstens | 4b28a8f | 2008-01-26 14:10:57 +0100 | [diff] [blame] | 576 | if (rc) |
| 577 | goto out; |
| 578 | switch (sccb->header.response_code) { |
| 579 | case 0x0020: |
| 580 | case 0x0120: |
| 581 | case 0x0440: |
| 582 | case 0x0450: |
| 583 | break; |
| 584 | default: |
Joe Perches | baebc70 | 2016-03-03 20:49:57 -0800 | [diff] [blame] | 585 | pr_warn("configure channel-path failed (cmd=0x%08x, response=0x%04x)\n", |
| 586 | cmd, sccb->header.response_code); |
Heiko Carstens | 4b28a8f | 2008-01-26 14:10:57 +0100 | [diff] [blame] | 587 | rc = -EIO; |
| 588 | break; |
| 589 | } |
| 590 | out: |
| 591 | free_page((unsigned long) sccb); |
| 592 | return rc; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * sclp_chp_configure - perform configure channel-path sclp command |
| 597 | * @chpid: channel-path ID |
| 598 | * |
| 599 | * Perform configure channel-path command sclp command for specified chpid. |
| 600 | * Return 0 after command successfully finished, non-zero otherwise. |
| 601 | */ |
| 602 | int sclp_chp_configure(struct chp_id chpid) |
| 603 | { |
| 604 | return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * sclp_chp_deconfigure - perform deconfigure channel-path sclp command |
| 609 | * @chpid: channel-path ID |
| 610 | * |
| 611 | * Perform deconfigure channel-path command sclp command for specified chpid |
| 612 | * and wait for completion. On success return 0. Return non-zero otherwise. |
| 613 | */ |
| 614 | int sclp_chp_deconfigure(struct chp_id chpid) |
| 615 | { |
| 616 | return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8); |
| 617 | } |
| 618 | |
| 619 | struct chp_info_sccb { |
| 620 | struct sccb_header header; |
| 621 | u8 recognized[SCLP_CHP_INFO_MASK_SIZE]; |
| 622 | u8 standby[SCLP_CHP_INFO_MASK_SIZE]; |
| 623 | u8 configured[SCLP_CHP_INFO_MASK_SIZE]; |
| 624 | u8 ccm; |
| 625 | u8 reserved[6]; |
| 626 | u8 cssid; |
| 627 | } __attribute__((packed)); |
| 628 | |
| 629 | /** |
| 630 | * sclp_chp_read_info - perform read channel-path information sclp command |
| 631 | * @info: resulting channel-path information data |
| 632 | * |
| 633 | * Perform read channel-path information sclp command and wait for completion. |
| 634 | * On success, store channel-path information in @info and return 0. Return |
| 635 | * non-zero otherwise. |
| 636 | */ |
| 637 | int sclp_chp_read_info(struct sclp_chp_info *info) |
| 638 | { |
| 639 | struct chp_info_sccb *sccb; |
| 640 | int rc; |
| 641 | |
| 642 | if (!SCLP_HAS_CHP_INFO) |
| 643 | return -EOPNOTSUPP; |
| 644 | /* Prepare sccb. */ |
| 645 | sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 646 | if (!sccb) |
| 647 | return -ENOMEM; |
| 648 | sccb->header.length = sizeof(*sccb); |
Michael Holzheu | d475f94 | 2013-06-06 09:52:08 +0200 | [diff] [blame] | 649 | rc = sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb); |
Heiko Carstens | 4b28a8f | 2008-01-26 14:10:57 +0100 | [diff] [blame] | 650 | if (rc) |
| 651 | goto out; |
| 652 | if (sccb->header.response_code != 0x0010) { |
Joe Perches | baebc70 | 2016-03-03 20:49:57 -0800 | [diff] [blame] | 653 | pr_warn("read channel-path info failed (response=0x%04x)\n", |
| 654 | sccb->header.response_code); |
Heiko Carstens | 4b28a8f | 2008-01-26 14:10:57 +0100 | [diff] [blame] | 655 | rc = -EIO; |
| 656 | goto out; |
| 657 | } |
| 658 | memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE); |
| 659 | memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE); |
| 660 | memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE); |
| 661 | out: |
| 662 | free_page((unsigned long) sccb); |
| 663 | return rc; |
| 664 | } |