blob: 77df9cb00688feeda6cad1fbfd4623fc5a26134e [file] [log] [blame]
Heiko Carstens08d07962008-01-26 14:10:56 +01001/*
Jan Glauber7441b062012-11-29 14:35:47 +01002 * Copyright IBM Corp. 2007,2012
Heiko Carstens08d07962008-01-26 14:10:56 +01003 *
Gerald Schaefer039979042009-06-16 10:30:48 +02004 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
5 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
Heiko Carstens08d07962008-01-26 14:10:56 +01006 */
7
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +01008#define KMSG_COMPONENT "sclp_cmd"
9#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
Heiko Carstens08d07962008-01-26 14:10:56 +010011#include <linux/completion.h>
12#include <linux/init.h>
13#include <linux/errno.h>
Gerald Schaefer039979042009-06-16 10:30:48 +020014#include <linux/err.h>
Jan Glauber7441b062012-11-29 14:35:47 +010015#include <linux/export.h>
Heiko Carstens08d07962008-01-26 14:10:56 +010016#include <linux/slab.h>
17#include <linux/string.h>
Heiko Carstense0bc2492008-07-14 09:59:19 +020018#include <linux/mm.h>
19#include <linux/mmzone.h>
20#include <linux/memory.h>
Christian Borntraeger1526bf92012-05-15 14:15:25 +020021#include <linux/module.h>
Gerald Schaefer039979042009-06-16 10:30:48 +020022#include <linux/platform_device.h>
David Howellsa0616cd2012-03-28 18:30:02 +010023#include <asm/ctl_reg.h>
Heiko Carstens6b70a922012-11-02 12:56:43 +010024#include <asm/chpid.h>
25#include <asm/setup.h>
26#include <asm/page.h>
27#include <asm/sclp.h>
Heiko Carstens08d07962008-01-26 14:10:56 +010028
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +010029#include "sclp.h"
Heiko Carstens08d07962008-01-26 14:10:56 +010030
31#define SCLP_CMDW_READ_SCP_INFO 0x00020001
32#define SCLP_CMDW_READ_SCP_INFO_FORCED 0x00120001
33
34struct read_info_sccb {
35 struct sccb_header header; /* 0-7 */
36 u16 rnmax; /* 8-9 */
37 u8 rnsize; /* 10 */
38 u8 _reserved0[24 - 11]; /* 11-15 */
39 u8 loadparm[8]; /* 24-31 */
40 u8 _reserved1[48 - 32]; /* 32-47 */
41 u64 facilities; /* 48-55 */
42 u8 _reserved2[84 - 56]; /* 56-83 */
43 u8 fac84; /* 84 */
Christian Borntraeger1526bf92012-05-15 14:15:25 +020044 u8 fac85; /* 85 */
45 u8 _reserved3[91 - 86]; /* 86-90 */
Heiko Carstens08d07962008-01-26 14:10:56 +010046 u8 flags; /* 91 */
47 u8 _reserved4[100 - 92]; /* 92-99 */
48 u32 rnsize2; /* 100-103 */
49 u64 rnmax2; /* 104-111 */
50 u8 _reserved5[4096 - 112]; /* 112-4095 */
51} __attribute__((packed, aligned(PAGE_SIZE)));
52
Heinz Graalfscd183452012-06-11 16:06:59 +020053static struct init_sccb __initdata early_event_mask_sccb __aligned(PAGE_SIZE);
Heiko Carstens08d07962008-01-26 14:10:56 +010054static struct read_info_sccb __initdata early_read_info_sccb;
55static int __initdata early_read_info_sccb_valid;
56
57u64 sclp_facilities;
58static u8 sclp_fac84;
Heiko Carstense0bc2492008-07-14 09:59:19 +020059static unsigned long long rzm;
60static unsigned long long rnmax;
Heiko Carstens08d07962008-01-26 14:10:56 +010061
62static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb)
63{
64 int rc;
65
66 __ctl_set_bit(0, 9);
67 rc = sclp_service_call(cmd, sccb);
68 if (rc)
69 goto out;
Martin Schwidefskyb50511e2011-10-30 15:16:50 +010070 __load_psw_mask(PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA |
71 PSW_MASK_BA | PSW_MASK_EXT | PSW_MASK_WAIT);
Heiko Carstens08d07962008-01-26 14:10:56 +010072 local_irq_disable();
73out:
74 /* Contents of the sccb might have changed. */
75 barrier();
76 __ctl_clear_bit(0, 9);
77 return rc;
78}
79
Heiko Carstens23d17422008-07-14 09:59:21 +020080static void __init sclp_read_info_early(void)
Heiko Carstens08d07962008-01-26 14:10:56 +010081{
82 int rc;
83 int i;
84 struct read_info_sccb *sccb;
85 sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
86 SCLP_CMDW_READ_SCP_INFO};
87
88 sccb = &early_read_info_sccb;
89 for (i = 0; i < ARRAY_SIZE(commands); i++) {
90 do {
91 memset(sccb, 0, sizeof(*sccb));
92 sccb->header.length = sizeof(*sccb);
Martin Schwidefskycb9d7162009-12-07 12:52:12 +010093 sccb->header.function_code = 0x80;
Heiko Carstens08d07962008-01-26 14:10:56 +010094 sccb->header.control_mask[2] = 0x80;
95 rc = sclp_cmd_sync_early(commands[i], sccb);
96 } while (rc == -EBUSY);
97
98 if (rc)
99 break;
100 if (sccb->header.response_code == 0x10) {
101 early_read_info_sccb_valid = 1;
102 break;
103 }
104 if (sccb->header.response_code != 0x1f0)
105 break;
106 }
107}
108
Heinz Graalfscd183452012-06-11 16:06:59 +0200109static void __init sclp_event_mask_early(void)
110{
111 struct init_sccb *sccb = &early_event_mask_sccb;
112 int rc;
113
114 do {
115 memset(sccb, 0, sizeof(*sccb));
116 sccb->header.length = sizeof(*sccb);
117 sccb->mask_length = sizeof(sccb_mask_t);
118 rc = sclp_cmd_sync_early(SCLP_CMDW_WRITE_EVENT_MASK, sccb);
119 } while (rc == -EBUSY);
120}
121
Heiko Carstens08d07962008-01-26 14:10:56 +0100122void __init sclp_facilities_detect(void)
123{
Heiko Carstens08d07962008-01-26 14:10:56 +0100124 struct read_info_sccb *sccb;
125
Heiko Carstens23d17422008-07-14 09:59:21 +0200126 sclp_read_info_early();
Heiko Carstens08d07962008-01-26 14:10:56 +0100127 if (!early_read_info_sccb_valid)
Heiko Carstens23d17422008-07-14 09:59:21 +0200128 return;
129
Heiko Carstens08d07962008-01-26 14:10:56 +0100130 sccb = &early_read_info_sccb;
Heiko Carstens23d17422008-07-14 09:59:21 +0200131 sclp_facilities = sccb->facilities;
132 sclp_fac84 = sccb->fac84;
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100133 if (sccb->fac85 & 0x02)
134 S390_lowcore.machine_flags |= MACHINE_FLAG_ESOP;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200135 rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
136 rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
137 rzm <<= 20;
Heinz Graalfscd183452012-06-11 16:06:59 +0200138
139 sclp_event_mask_early();
140}
141
142bool __init sclp_has_linemode(void)
143{
144 struct init_sccb *sccb = &early_event_mask_sccb;
145
146 if (sccb->header.response_code != 0x20)
147 return 0;
Christian Borntraeger22bfda62013-09-17 13:28:50 +0200148 if (!(sccb->sclp_send_mask & (EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK)))
149 return 0;
150 if (!(sccb->sclp_receive_mask & (EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK)))
151 return 0;
152 return 1;
Heinz Graalfscd183452012-06-11 16:06:59 +0200153}
154
155bool __init sclp_has_vt220(void)
156{
157 struct init_sccb *sccb = &early_event_mask_sccb;
158
159 if (sccb->header.response_code != 0x20)
160 return 0;
161 if (sccb->sclp_send_mask & EVTYP_VT220MSG_MASK)
162 return 1;
163 return 0;
Heiko Carstens23d17422008-07-14 09:59:21 +0200164}
165
166unsigned long long sclp_get_rnmax(void)
167{
168 return rnmax;
169}
170
171unsigned long long sclp_get_rzm(void)
172{
173 return rzm;
Heiko Carstens08d07962008-01-26 14:10:56 +0100174}
175
176/*
Heiko Carstens23d17422008-07-14 09:59:21 +0200177 * This function will be called after sclp_facilities_detect(), which gets
178 * called from early.c code. Therefore the sccb should have valid contents.
Heiko Carstens08d07962008-01-26 14:10:56 +0100179 */
180void __init sclp_get_ipl_info(struct sclp_ipl_info *info)
181{
182 struct read_info_sccb *sccb;
183
184 if (!early_read_info_sccb_valid)
185 return;
186 sccb = &early_read_info_sccb;
187 info->is_valid = 1;
188 if (sccb->flags & 0x2)
189 info->has_dump = 1;
190 memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN);
191}
192
193static void sclp_sync_callback(struct sclp_req *req, void *data)
194{
195 struct completion *completion = data;
196
197 complete(completion);
198}
199
Michael Holzheud475f942013-06-06 09:52:08 +0200200int sclp_sync_request(sclp_cmdw_t cmd, void *sccb)
Heiko Carstens08d07962008-01-26 14:10:56 +0100201{
202 struct completion completion;
203 struct sclp_req *request;
204 int rc;
205
206 request = kzalloc(sizeof(*request), GFP_KERNEL);
207 if (!request)
208 return -ENOMEM;
209 request->command = cmd;
210 request->sccb = sccb;
211 request->status = SCLP_REQ_FILLED;
212 request->callback = sclp_sync_callback;
213 request->callback_data = &completion;
214 init_completion(&completion);
215
216 /* Perform sclp request. */
217 rc = sclp_add_request(request);
218 if (rc)
219 goto out;
220 wait_for_completion(&completion);
221
222 /* Check response. */
223 if (request->status != SCLP_REQ_DONE) {
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100224 pr_warning("sync request failed (cmd=0x%08x, "
225 "status=0x%02x)\n", cmd, request->status);
Heiko Carstens08d07962008-01-26 14:10:56 +0100226 rc = -EIO;
227 }
228out:
229 kfree(request);
230 return rc;
231}
232
233/*
234 * CPU configuration related functions.
235 */
236
237#define SCLP_CMDW_READ_CPU_INFO 0x00010001
238#define SCLP_CMDW_CONFIGURE_CPU 0x00110001
239#define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
240
241struct read_cpu_info_sccb {
242 struct sccb_header header;
243 u16 nr_configured;
244 u16 offset_configured;
245 u16 nr_standby;
246 u16 offset_standby;
247 u8 reserved[4096 - 16];
248} __attribute__((packed, aligned(PAGE_SIZE)));
249
Heiko Carstens08d07962008-01-26 14:10:56 +0100250static void sclp_fill_cpu_info(struct sclp_cpu_info *info,
251 struct read_cpu_info_sccb *sccb)
252{
253 char *page = (char *) sccb;
254
255 memset(info, 0, sizeof(*info));
256 info->configured = sccb->nr_configured;
257 info->standby = sccb->nr_standby;
258 info->combined = sccb->nr_configured + sccb->nr_standby;
259 info->has_cpu_type = sclp_fac84 & 0x1;
260 memcpy(&info->cpu, page + sccb->offset_configured,
261 info->combined * sizeof(struct sclp_cpu_entry));
262}
263
Heiko Carstens48483b32008-01-26 14:11:05 +0100264int sclp_get_cpu_info(struct sclp_cpu_info *info)
Heiko Carstens08d07962008-01-26 14:10:56 +0100265{
266 int rc;
267 struct read_cpu_info_sccb *sccb;
268
269 if (!SCLP_HAS_CPU_INFO)
270 return -EOPNOTSUPP;
Heiko Carstens48483b32008-01-26 14:11:05 +0100271 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Heiko Carstens08d07962008-01-26 14:10:56 +0100272 if (!sccb)
273 return -ENOMEM;
Heiko Carstens08d07962008-01-26 14:10:56 +0100274 sccb->header.length = sizeof(*sccb);
Michael Holzheud475f942013-06-06 09:52:08 +0200275 rc = sclp_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb);
Heiko Carstens08d07962008-01-26 14:10:56 +0100276 if (rc)
277 goto out;
278 if (sccb->header.response_code != 0x0010) {
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100279 pr_warning("readcpuinfo failed (response=0x%04x)\n",
280 sccb->header.response_code);
Heiko Carstens08d07962008-01-26 14:10:56 +0100281 rc = -EIO;
282 goto out;
283 }
284 sclp_fill_cpu_info(info, sccb);
285out:
286 free_page((unsigned long) sccb);
287 return rc;
288}
289
Heiko Carstens08d07962008-01-26 14:10:56 +0100290struct cpu_configure_sccb {
291 struct sccb_header header;
292} __attribute__((packed, aligned(8)));
293
294static int do_cpu_configure(sclp_cmdw_t cmd)
295{
296 struct cpu_configure_sccb *sccb;
297 int rc;
298
299 if (!SCLP_HAS_CPU_RECONFIG)
300 return -EOPNOTSUPP;
301 /*
302 * This is not going to cross a page boundary since we force
303 * kmalloc to have a minimum alignment of 8 bytes on s390.
304 */
305 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
306 if (!sccb)
307 return -ENOMEM;
308 sccb->header.length = sizeof(*sccb);
Michael Holzheud475f942013-06-06 09:52:08 +0200309 rc = sclp_sync_request(cmd, sccb);
Heiko Carstens08d07962008-01-26 14:10:56 +0100310 if (rc)
311 goto out;
312 switch (sccb->header.response_code) {
313 case 0x0020:
314 case 0x0120:
315 break;
316 default:
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100317 pr_warning("configure cpu failed (cmd=0x%08x, "
318 "response=0x%04x)\n", cmd,
319 sccb->header.response_code);
Heiko Carstens08d07962008-01-26 14:10:56 +0100320 rc = -EIO;
321 break;
322 }
323out:
324 kfree(sccb);
325 return rc;
326}
327
328int sclp_cpu_configure(u8 cpu)
329{
330 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8);
331}
332
333int sclp_cpu_deconfigure(u8 cpu)
334{
335 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8);
336}
Heiko Carstens4b28a8f2008-01-26 14:10:57 +0100337
Heiko Carstense0bc2492008-07-14 09:59:19 +0200338#ifdef CONFIG_MEMORY_HOTPLUG
339
340static DEFINE_MUTEX(sclp_mem_mutex);
341static LIST_HEAD(sclp_mem_list);
342static u8 sclp_max_storage_id;
343static unsigned long sclp_storage_ids[256 / BITS_PER_LONG];
Gerald Schaefer039979042009-06-16 10:30:48 +0200344static int sclp_mem_state_changed;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200345
346struct memory_increment {
347 struct list_head list;
348 u16 rn;
349 int standby;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200350};
351
352struct assign_storage_sccb {
353 struct sccb_header header;
354 u16 rn;
355} __packed;
356
Heiko Carstens9c952582010-03-24 11:49:55 +0100357int arch_get_memory_phys_device(unsigned long start_pfn)
358{
359 if (!rzm)
360 return 0;
361 return PFN_PHYS(start_pfn) >> ilog2(rzm);
362}
363
Heiko Carstense0bc2492008-07-14 09:59:19 +0200364static unsigned long long rn2addr(u16 rn)
365{
366 return (unsigned long long) (rn - 1) * rzm;
367}
368
369static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
370{
371 struct assign_storage_sccb *sccb;
372 int rc;
373
374 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
375 if (!sccb)
376 return -ENOMEM;
377 sccb->header.length = PAGE_SIZE;
378 sccb->rn = rn;
Michael Holzheud475f942013-06-06 09:52:08 +0200379 rc = sclp_sync_request(cmd, sccb);
Heiko Carstense0bc2492008-07-14 09:59:19 +0200380 if (rc)
381 goto out;
382 switch (sccb->header.response_code) {
383 case 0x0020:
384 case 0x0120:
385 break;
386 default:
Heiko Carstens675be972008-11-14 18:18:01 +0100387 pr_warning("assign storage failed (cmd=0x%08x, "
388 "response=0x%04x, rn=0x%04x)\n", cmd,
389 sccb->header.response_code, rn);
Heiko Carstense0bc2492008-07-14 09:59:19 +0200390 rc = -EIO;
391 break;
392 }
393out:
394 free_page((unsigned long) sccb);
395 return rc;
396}
397
398static int sclp_assign_storage(u16 rn)
399{
Heiko Carstens6b70a922012-11-02 12:56:43 +0100400 unsigned long long start;
Heiko Carstens0cd2f622012-05-14 11:04:10 +0200401 int rc;
402
403 rc = do_assign_storage(0x000d0001, rn);
404 if (rc)
Heiko Carstens6b70a922012-11-02 12:56:43 +0100405 return rc;
406 start = rn2addr(rn);
407 storage_key_init_range(start, start + rzm);
408 return 0;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200409}
410
411static int sclp_unassign_storage(u16 rn)
412{
413 return do_assign_storage(0x000c0001, rn);
414}
415
416struct attach_storage_sccb {
417 struct sccb_header header;
418 u16 :16;
419 u16 assigned;
420 u32 :32;
421 u32 entries[0];
422} __packed;
423
424static int sclp_attach_storage(u8 id)
425{
426 struct attach_storage_sccb *sccb;
427 int rc;
428 int i;
429
430 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
431 if (!sccb)
432 return -ENOMEM;
433 sccb->header.length = PAGE_SIZE;
Michael Holzheud475f942013-06-06 09:52:08 +0200434 rc = sclp_sync_request(0x00080001 | id << 8, sccb);
Heiko Carstense0bc2492008-07-14 09:59:19 +0200435 if (rc)
436 goto out;
437 switch (sccb->header.response_code) {
438 case 0x0020:
439 set_bit(id, sclp_storage_ids);
Heiko Carstens8adb4ca2011-08-24 17:15:13 +0200440 for (i = 0; i < sccb->assigned; i++) {
441 if (sccb->entries[i])
442 sclp_unassign_storage(sccb->entries[i] >> 16);
443 }
Heiko Carstense0bc2492008-07-14 09:59:19 +0200444 break;
445 default:
446 rc = -EIO;
447 break;
448 }
449out:
450 free_page((unsigned long) sccb);
451 return rc;
452}
453
454static int sclp_mem_change_state(unsigned long start, unsigned long size,
455 int online)
456{
457 struct memory_increment *incr;
458 unsigned long long istart;
459 int rc = 0;
460
461 list_for_each_entry(incr, &sclp_mem_list, list) {
462 istart = rn2addr(incr->rn);
463 if (start + size - 1 < istart)
464 break;
465 if (start > istart + rzm - 1)
466 continue;
Heiko Carstens15535562013-05-02 09:20:30 +0200467 if (online)
Heiko Carstense0bc2492008-07-14 09:59:19 +0200468 rc |= sclp_assign_storage(incr->rn);
Heiko Carstens15535562013-05-02 09:20:30 +0200469 else
Heiko Carstense0bc2492008-07-14 09:59:19 +0200470 sclp_unassign_storage(incr->rn);
Heiko Carstense0bc2492008-07-14 09:59:19 +0200471 }
472 return rc ? -EIO : 0;
473}
474
475static int sclp_mem_notifier(struct notifier_block *nb,
476 unsigned long action, void *data)
477{
478 unsigned long start, size;
479 struct memory_notify *arg;
480 unsigned char id;
481 int rc = 0;
482
483 arg = data;
484 start = arg->start_pfn << PAGE_SHIFT;
485 size = arg->nr_pages << PAGE_SHIFT;
486 mutex_lock(&sclp_mem_mutex);
Akinobu Mitafc89db42012-03-23 15:02:05 -0700487 for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
488 sclp_attach_storage(id);
Heiko Carstense0bc2492008-07-14 09:59:19 +0200489 switch (action) {
490 case MEM_ONLINE:
Gerald Schaefer7e9238f2008-08-01 16:39:16 +0200491 case MEM_GOING_OFFLINE:
492 case MEM_CANCEL_OFFLINE:
Heiko Carstense0bc2492008-07-14 09:59:19 +0200493 break;
494 case MEM_GOING_ONLINE:
495 rc = sclp_mem_change_state(start, size, 1);
496 break;
497 case MEM_CANCEL_ONLINE:
498 sclp_mem_change_state(start, size, 0);
499 break;
Gerald Schaefer7e9238f2008-08-01 16:39:16 +0200500 case MEM_OFFLINE:
501 sclp_mem_change_state(start, size, 0);
502 break;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200503 default:
504 rc = -EINVAL;
505 break;
506 }
Gerald Schaefer039979042009-06-16 10:30:48 +0200507 if (!rc)
508 sclp_mem_state_changed = 1;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200509 mutex_unlock(&sclp_mem_mutex);
510 return rc ? NOTIFY_BAD : NOTIFY_OK;
511}
512
513static struct notifier_block sclp_mem_nb = {
514 .notifier_call = sclp_mem_notifier,
515};
516
517static void __init add_memory_merged(u16 rn)
518{
519 static u16 first_rn, num;
520 unsigned long long start, size;
521
522 if (rn && first_rn && (first_rn + num == rn)) {
523 num++;
524 return;
525 }
526 if (!first_rn)
527 goto skip_add;
528 start = rn2addr(first_rn);
529 size = (unsigned long long ) num * rzm;
530 if (start >= VMEM_MAX_PHYS)
531 goto skip_add;
532 if (start + size > VMEM_MAX_PHYS)
533 size = VMEM_MAX_PHYS - start;
Heiko Carstens23d75d92009-02-19 15:19:01 +0100534 if (memory_end_set && (start >= memory_end))
535 goto skip_add;
536 if (memory_end_set && (start + size > memory_end))
537 size = memory_end - start;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200538 add_memory(0, start, size);
539skip_add:
540 first_rn = rn;
541 num = 1;
542}
543
544static void __init sclp_add_standby_memory(void)
545{
546 struct memory_increment *incr;
547
548 list_for_each_entry(incr, &sclp_mem_list, list)
549 if (incr->standby)
550 add_memory_merged(incr->rn);
551 add_memory_merged(0);
552}
553
554static void __init insert_increment(u16 rn, int standby, int assigned)
555{
556 struct memory_increment *incr, *new_incr;
557 struct list_head *prev;
558 u16 last_rn;
559
560 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
561 if (!new_incr)
562 return;
563 new_incr->rn = rn;
564 new_incr->standby = standby;
565 last_rn = 0;
566 prev = &sclp_mem_list;
567 list_for_each_entry(incr, &sclp_mem_list, list) {
568 if (assigned && incr->rn > rn)
569 break;
570 if (!assigned && incr->rn - last_rn > 1)
571 break;
572 last_rn = incr->rn;
573 prev = &incr->list;
574 }
575 if (!assigned)
576 new_incr->rn = last_rn + 1;
577 if (new_incr->rn > rnmax) {
578 kfree(new_incr);
579 return;
580 }
581 list_add(&new_incr->list, prev);
582}
583
Gerald Schaefer039979042009-06-16 10:30:48 +0200584static int sclp_mem_freeze(struct device *dev)
585{
586 if (!sclp_mem_state_changed)
587 return 0;
588 pr_err("Memory hotplug state changed, suspend refused.\n");
589 return -EPERM;
590}
591
Heiko Carstense0bc2492008-07-14 09:59:19 +0200592struct read_storage_sccb {
593 struct sccb_header header;
594 u16 max_id;
595 u16 assigned;
596 u16 standby;
597 u16 :16;
598 u32 entries[0];
599} __packed;
600
Alexey Dobriyan47145212009-12-14 18:00:08 -0800601static const struct dev_pm_ops sclp_mem_pm_ops = {
Gerald Schaefer039979042009-06-16 10:30:48 +0200602 .freeze = sclp_mem_freeze,
603};
604
605static struct platform_driver sclp_mem_pdrv = {
606 .driver = {
607 .name = "sclp_mem",
608 .pm = &sclp_mem_pm_ops,
609 },
610};
611
Heiko Carstense0bc2492008-07-14 09:59:19 +0200612static int __init sclp_detect_standby_memory(void)
613{
Gerald Schaefer039979042009-06-16 10:30:48 +0200614 struct platform_device *sclp_pdev;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200615 struct read_storage_sccb *sccb;
616 int i, id, assigned, rc;
617
Michael Holzheu52319b42013-03-08 09:29:34 +0100618 if (OLDMEM_BASE) /* No standby memory in kdump mode */
619 return 0;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200620 if (!early_read_info_sccb_valid)
621 return 0;
622 if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
623 return 0;
624 rc = -ENOMEM;
625 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
626 if (!sccb)
627 goto out;
628 assigned = 0;
629 for (id = 0; id <= sclp_max_storage_id; id++) {
630 memset(sccb, 0, PAGE_SIZE);
631 sccb->header.length = PAGE_SIZE;
Michael Holzheud475f942013-06-06 09:52:08 +0200632 rc = sclp_sync_request(0x00040001 | id << 8, sccb);
Heiko Carstense0bc2492008-07-14 09:59:19 +0200633 if (rc)
634 goto out;
635 switch (sccb->header.response_code) {
636 case 0x0010:
637 set_bit(id, sclp_storage_ids);
638 for (i = 0; i < sccb->assigned; i++) {
639 if (!sccb->entries[i])
640 continue;
641 assigned++;
642 insert_increment(sccb->entries[i] >> 16, 0, 1);
643 }
644 break;
645 case 0x0310:
646 break;
647 case 0x0410:
648 for (i = 0; i < sccb->assigned; i++) {
649 if (!sccb->entries[i])
650 continue;
651 assigned++;
652 insert_increment(sccb->entries[i] >> 16, 1, 1);
653 }
654 break;
655 default:
656 rc = -EIO;
657 break;
658 }
659 if (!rc)
660 sclp_max_storage_id = sccb->max_id;
661 }
662 if (rc || list_empty(&sclp_mem_list))
663 goto out;
664 for (i = 1; i <= rnmax - assigned; i++)
665 insert_increment(0, 1, 0);
666 rc = register_memory_notifier(&sclp_mem_nb);
667 if (rc)
668 goto out;
Gerald Schaefer039979042009-06-16 10:30:48 +0200669 rc = platform_driver_register(&sclp_mem_pdrv);
670 if (rc)
671 goto out;
672 sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
Thomas Meyer4bdb6132013-06-01 11:58:09 +0200673 rc = PTR_RET(sclp_pdev);
Gerald Schaefer039979042009-06-16 10:30:48 +0200674 if (rc)
675 goto out_driver;
Heiko Carstense0bc2492008-07-14 09:59:19 +0200676 sclp_add_standby_memory();
Gerald Schaefer039979042009-06-16 10:30:48 +0200677 goto out;
678out_driver:
679 platform_driver_unregister(&sclp_mem_pdrv);
Heiko Carstense0bc2492008-07-14 09:59:19 +0200680out:
681 free_page((unsigned long) sccb);
682 return rc;
683}
684__initcall(sclp_detect_standby_memory);
685
686#endif /* CONFIG_MEMORY_HOTPLUG */
687
Heiko Carstens4b28a8f2008-01-26 14:10:57 +0100688/*
Jan Glauber7441b062012-11-29 14:35:47 +0100689 * PCI I/O adapter configuration related functions.
690 */
691#define SCLP_CMDW_CONFIGURE_PCI 0x001a0001
692#define SCLP_CMDW_DECONFIGURE_PCI 0x001b0001
693
694#define SCLP_RECONFIG_PCI_ATPYE 2
695
696struct pci_cfg_sccb {
697 struct sccb_header header;
698 u8 atype; /* adapter type */
699 u8 reserved1;
700 u16 reserved2;
701 u32 aid; /* adapter identifier */
702} __packed;
703
704static int do_pci_configure(sclp_cmdw_t cmd, u32 fid)
705{
706 struct pci_cfg_sccb *sccb;
707 int rc;
708
709 if (!SCLP_HAS_PCI_RECONFIG)
710 return -EOPNOTSUPP;
711
712 sccb = (struct pci_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
713 if (!sccb)
714 return -ENOMEM;
715
716 sccb->header.length = PAGE_SIZE;
717 sccb->atype = SCLP_RECONFIG_PCI_ATPYE;
718 sccb->aid = fid;
Michael Holzheud475f942013-06-06 09:52:08 +0200719 rc = sclp_sync_request(cmd, sccb);
Jan Glauber7441b062012-11-29 14:35:47 +0100720 if (rc)
721 goto out;
722 switch (sccb->header.response_code) {
723 case 0x0020:
724 case 0x0120:
725 break;
726 default:
727 pr_warn("configure PCI I/O adapter failed: cmd=0x%08x response=0x%04x\n",
728 cmd, sccb->header.response_code);
729 rc = -EIO;
730 break;
731 }
732out:
733 free_page((unsigned long) sccb);
734 return rc;
735}
736
737int sclp_pci_configure(u32 fid)
738{
739 return do_pci_configure(SCLP_CMDW_CONFIGURE_PCI, fid);
740}
741EXPORT_SYMBOL(sclp_pci_configure);
742
743int sclp_pci_deconfigure(u32 fid)
744{
745 return do_pci_configure(SCLP_CMDW_DECONFIGURE_PCI, fid);
746}
747EXPORT_SYMBOL(sclp_pci_deconfigure);
748
749/*
Heiko Carstens4b28a8f2008-01-26 14:10:57 +0100750 * Channel path configuration related functions.
751 */
752
753#define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
754#define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
755#define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
756
757struct chp_cfg_sccb {
758 struct sccb_header header;
759 u8 ccm;
760 u8 reserved[6];
761 u8 cssid;
762} __attribute__((packed));
763
764static int do_chp_configure(sclp_cmdw_t cmd)
765{
766 struct chp_cfg_sccb *sccb;
767 int rc;
768
769 if (!SCLP_HAS_CHP_RECONFIG)
770 return -EOPNOTSUPP;
771 /* Prepare sccb. */
772 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
773 if (!sccb)
774 return -ENOMEM;
775 sccb->header.length = sizeof(*sccb);
Michael Holzheud475f942013-06-06 09:52:08 +0200776 rc = sclp_sync_request(cmd, sccb);
Heiko Carstens4b28a8f2008-01-26 14:10:57 +0100777 if (rc)
778 goto out;
779 switch (sccb->header.response_code) {
780 case 0x0020:
781 case 0x0120:
782 case 0x0440:
783 case 0x0450:
784 break;
785 default:
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100786 pr_warning("configure channel-path failed "
787 "(cmd=0x%08x, response=0x%04x)\n", cmd,
788 sccb->header.response_code);
Heiko Carstens4b28a8f2008-01-26 14:10:57 +0100789 rc = -EIO;
790 break;
791 }
792out:
793 free_page((unsigned long) sccb);
794 return rc;
795}
796
797/**
798 * sclp_chp_configure - perform configure channel-path sclp command
799 * @chpid: channel-path ID
800 *
801 * Perform configure channel-path command sclp command for specified chpid.
802 * Return 0 after command successfully finished, non-zero otherwise.
803 */
804int sclp_chp_configure(struct chp_id chpid)
805{
806 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
807}
808
809/**
810 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
811 * @chpid: channel-path ID
812 *
813 * Perform deconfigure channel-path command sclp command for specified chpid
814 * and wait for completion. On success return 0. Return non-zero otherwise.
815 */
816int sclp_chp_deconfigure(struct chp_id chpid)
817{
818 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
819}
820
821struct chp_info_sccb {
822 struct sccb_header header;
823 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
824 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
825 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
826 u8 ccm;
827 u8 reserved[6];
828 u8 cssid;
829} __attribute__((packed));
830
831/**
832 * sclp_chp_read_info - perform read channel-path information sclp command
833 * @info: resulting channel-path information data
834 *
835 * Perform read channel-path information sclp command and wait for completion.
836 * On success, store channel-path information in @info and return 0. Return
837 * non-zero otherwise.
838 */
839int sclp_chp_read_info(struct sclp_chp_info *info)
840{
841 struct chp_info_sccb *sccb;
842 int rc;
843
844 if (!SCLP_HAS_CHP_INFO)
845 return -EOPNOTSUPP;
846 /* Prepare sccb. */
847 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
848 if (!sccb)
849 return -ENOMEM;
850 sccb->header.length = sizeof(*sccb);
Michael Holzheud475f942013-06-06 09:52:08 +0200851 rc = sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
Heiko Carstens4b28a8f2008-01-26 14:10:57 +0100852 if (rc)
853 goto out;
854 if (sccb->header.response_code != 0x0010) {
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100855 pr_warning("read channel-path info failed "
856 "(response=0x%04x)\n", sccb->header.response_code);
Heiko Carstens4b28a8f2008-01-26 14:10:57 +0100857 rc = -EIO;
858 goto out;
859 }
860 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
861 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
862 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
863out:
864 free_page((unsigned long) sccb);
865 return rc;
866}