]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/s390/char/sclp_cmd.c
[S390] Add support for memory hot-add via sclp.
[mirror_ubuntu-hirsute-kernel.git] / drivers / s390 / char / sclp_cmd.c
1 /*
2 * drivers/s390/char/sclp_cmd.c
3 *
4 * Copyright IBM Corp. 2007
5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
6 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
7 */
8
9 #include <linux/completion.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/mm.h>
15 #include <linux/mmzone.h>
16 #include <linux/memory.h>
17 #include <asm/chpid.h>
18 #include <asm/sclp.h>
19 #include "sclp.h"
20
21 #define TAG "sclp_cmd: "
22
23 #define SCLP_CMDW_READ_SCP_INFO 0x00020001
24 #define SCLP_CMDW_READ_SCP_INFO_FORCED 0x00120001
25
26 struct read_info_sccb {
27 struct sccb_header header; /* 0-7 */
28 u16 rnmax; /* 8-9 */
29 u8 rnsize; /* 10 */
30 u8 _reserved0[24 - 11]; /* 11-15 */
31 u8 loadparm[8]; /* 24-31 */
32 u8 _reserved1[48 - 32]; /* 32-47 */
33 u64 facilities; /* 48-55 */
34 u8 _reserved2[84 - 56]; /* 56-83 */
35 u8 fac84; /* 84 */
36 u8 _reserved3[91 - 85]; /* 85-90 */
37 u8 flags; /* 91 */
38 u8 _reserved4[100 - 92]; /* 92-99 */
39 u32 rnsize2; /* 100-103 */
40 u64 rnmax2; /* 104-111 */
41 u8 _reserved5[4096 - 112]; /* 112-4095 */
42 } __attribute__((packed, aligned(PAGE_SIZE)));
43
44 static struct read_info_sccb __initdata early_read_info_sccb;
45 static int __initdata early_read_info_sccb_valid;
46
47 u64 sclp_facilities;
48 static u8 sclp_fac84;
49 static unsigned long long rzm;
50 static unsigned long long rnmax;
51
52 static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb)
53 {
54 int rc;
55
56 __ctl_set_bit(0, 9);
57 rc = sclp_service_call(cmd, sccb);
58 if (rc)
59 goto out;
60 __load_psw_mask(PSW_BASE_BITS | PSW_MASK_EXT |
61 PSW_MASK_WAIT | PSW_DEFAULT_KEY);
62 local_irq_disable();
63 out:
64 /* Contents of the sccb might have changed. */
65 barrier();
66 __ctl_clear_bit(0, 9);
67 return rc;
68 }
69
70 void __init sclp_read_info_early(void)
71 {
72 int rc;
73 int i;
74 struct read_info_sccb *sccb;
75 sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
76 SCLP_CMDW_READ_SCP_INFO};
77
78 sccb = &early_read_info_sccb;
79 for (i = 0; i < ARRAY_SIZE(commands); i++) {
80 do {
81 memset(sccb, 0, sizeof(*sccb));
82 sccb->header.length = sizeof(*sccb);
83 sccb->header.control_mask[2] = 0x80;
84 rc = sclp_cmd_sync_early(commands[i], sccb);
85 } while (rc == -EBUSY);
86
87 if (rc)
88 break;
89 if (sccb->header.response_code == 0x10) {
90 early_read_info_sccb_valid = 1;
91 break;
92 }
93 if (sccb->header.response_code != 0x1f0)
94 break;
95 }
96 }
97
98 void __init sclp_facilities_detect(void)
99 {
100 if (!early_read_info_sccb_valid)
101 return;
102 sclp_facilities = early_read_info_sccb.facilities;
103 sclp_fac84 = early_read_info_sccb.fac84;
104 }
105
106 unsigned long long __init sclp_memory_detect(void)
107 {
108 unsigned long long memsize;
109 struct read_info_sccb *sccb;
110
111 if (!early_read_info_sccb_valid)
112 return 0;
113 sccb = &early_read_info_sccb;
114 rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
115 rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
116 rzm <<= 20;
117 memsize = rzm * rnmax;
118 return memsize;
119 }
120
121 /*
122 * This function will be called after sclp_memory_detect(), which gets called
123 * early from early.c code. Therefore the sccb should have valid contents.
124 */
125 void __init sclp_get_ipl_info(struct sclp_ipl_info *info)
126 {
127 struct read_info_sccb *sccb;
128
129 if (!early_read_info_sccb_valid)
130 return;
131 sccb = &early_read_info_sccb;
132 info->is_valid = 1;
133 if (sccb->flags & 0x2)
134 info->has_dump = 1;
135 memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN);
136 }
137
138 static void sclp_sync_callback(struct sclp_req *req, void *data)
139 {
140 struct completion *completion = data;
141
142 complete(completion);
143 }
144
145 static int do_sync_request(sclp_cmdw_t cmd, void *sccb)
146 {
147 struct completion completion;
148 struct sclp_req *request;
149 int rc;
150
151 request = kzalloc(sizeof(*request), GFP_KERNEL);
152 if (!request)
153 return -ENOMEM;
154 request->command = cmd;
155 request->sccb = sccb;
156 request->status = SCLP_REQ_FILLED;
157 request->callback = sclp_sync_callback;
158 request->callback_data = &completion;
159 init_completion(&completion);
160
161 /* Perform sclp request. */
162 rc = sclp_add_request(request);
163 if (rc)
164 goto out;
165 wait_for_completion(&completion);
166
167 /* Check response. */
168 if (request->status != SCLP_REQ_DONE) {
169 printk(KERN_WARNING TAG "sync request failed "
170 "(cmd=0x%08x, status=0x%02x)\n", cmd, request->status);
171 rc = -EIO;
172 }
173 out:
174 kfree(request);
175 return rc;
176 }
177
178 /*
179 * CPU configuration related functions.
180 */
181
182 #define SCLP_CMDW_READ_CPU_INFO 0x00010001
183 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
184 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
185
186 struct read_cpu_info_sccb {
187 struct sccb_header header;
188 u16 nr_configured;
189 u16 offset_configured;
190 u16 nr_standby;
191 u16 offset_standby;
192 u8 reserved[4096 - 16];
193 } __attribute__((packed, aligned(PAGE_SIZE)));
194
195 static void sclp_fill_cpu_info(struct sclp_cpu_info *info,
196 struct read_cpu_info_sccb *sccb)
197 {
198 char *page = (char *) sccb;
199
200 memset(info, 0, sizeof(*info));
201 info->configured = sccb->nr_configured;
202 info->standby = sccb->nr_standby;
203 info->combined = sccb->nr_configured + sccb->nr_standby;
204 info->has_cpu_type = sclp_fac84 & 0x1;
205 memcpy(&info->cpu, page + sccb->offset_configured,
206 info->combined * sizeof(struct sclp_cpu_entry));
207 }
208
209 int sclp_get_cpu_info(struct sclp_cpu_info *info)
210 {
211 int rc;
212 struct read_cpu_info_sccb *sccb;
213
214 if (!SCLP_HAS_CPU_INFO)
215 return -EOPNOTSUPP;
216 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
217 if (!sccb)
218 return -ENOMEM;
219 sccb->header.length = sizeof(*sccb);
220 rc = do_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb);
221 if (rc)
222 goto out;
223 if (sccb->header.response_code != 0x0010) {
224 printk(KERN_WARNING TAG "readcpuinfo failed "
225 "(response=0x%04x)\n", sccb->header.response_code);
226 rc = -EIO;
227 goto out;
228 }
229 sclp_fill_cpu_info(info, sccb);
230 out:
231 free_page((unsigned long) sccb);
232 return rc;
233 }
234
235 struct cpu_configure_sccb {
236 struct sccb_header header;
237 } __attribute__((packed, aligned(8)));
238
239 static int do_cpu_configure(sclp_cmdw_t cmd)
240 {
241 struct cpu_configure_sccb *sccb;
242 int rc;
243
244 if (!SCLP_HAS_CPU_RECONFIG)
245 return -EOPNOTSUPP;
246 /*
247 * This is not going to cross a page boundary since we force
248 * kmalloc to have a minimum alignment of 8 bytes on s390.
249 */
250 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
251 if (!sccb)
252 return -ENOMEM;
253 sccb->header.length = sizeof(*sccb);
254 rc = do_sync_request(cmd, sccb);
255 if (rc)
256 goto out;
257 switch (sccb->header.response_code) {
258 case 0x0020:
259 case 0x0120:
260 break;
261 default:
262 printk(KERN_WARNING TAG "configure cpu failed (cmd=0x%08x, "
263 "response=0x%04x)\n", cmd, sccb->header.response_code);
264 rc = -EIO;
265 break;
266 }
267 out:
268 kfree(sccb);
269 return rc;
270 }
271
272 int sclp_cpu_configure(u8 cpu)
273 {
274 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8);
275 }
276
277 int sclp_cpu_deconfigure(u8 cpu)
278 {
279 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8);
280 }
281
282 #ifdef CONFIG_MEMORY_HOTPLUG
283
284 static DEFINE_MUTEX(sclp_mem_mutex);
285 static LIST_HEAD(sclp_mem_list);
286 static u8 sclp_max_storage_id;
287 static unsigned long sclp_storage_ids[256 / BITS_PER_LONG];
288
289 struct memory_increment {
290 struct list_head list;
291 u16 rn;
292 int standby;
293 int usecount;
294 };
295
296 struct assign_storage_sccb {
297 struct sccb_header header;
298 u16 rn;
299 } __packed;
300
301 static unsigned long long rn2addr(u16 rn)
302 {
303 return (unsigned long long) (rn - 1) * rzm;
304 }
305
306 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
307 {
308 struct assign_storage_sccb *sccb;
309 int rc;
310
311 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
312 if (!sccb)
313 return -ENOMEM;
314 sccb->header.length = PAGE_SIZE;
315 sccb->rn = rn;
316 rc = do_sync_request(cmd, sccb);
317 if (rc)
318 goto out;
319 switch (sccb->header.response_code) {
320 case 0x0020:
321 case 0x0120:
322 break;
323 default:
324 rc = -EIO;
325 break;
326 }
327 out:
328 free_page((unsigned long) sccb);
329 return rc;
330 }
331
332 static int sclp_assign_storage(u16 rn)
333 {
334 return do_assign_storage(0x000d0001, rn);
335 }
336
337 static int sclp_unassign_storage(u16 rn)
338 {
339 return do_assign_storage(0x000c0001, rn);
340 }
341
342 struct attach_storage_sccb {
343 struct sccb_header header;
344 u16 :16;
345 u16 assigned;
346 u32 :32;
347 u32 entries[0];
348 } __packed;
349
350 static int sclp_attach_storage(u8 id)
351 {
352 struct attach_storage_sccb *sccb;
353 int rc;
354 int i;
355
356 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
357 if (!sccb)
358 return -ENOMEM;
359 sccb->header.length = PAGE_SIZE;
360 rc = do_sync_request(0x00080001 | id << 8, sccb);
361 if (rc)
362 goto out;
363 switch (sccb->header.response_code) {
364 case 0x0020:
365 set_bit(id, sclp_storage_ids);
366 for (i = 0; i < sccb->assigned; i++)
367 sclp_unassign_storage(sccb->entries[i] >> 16);
368 break;
369 default:
370 rc = -EIO;
371 break;
372 }
373 out:
374 free_page((unsigned long) sccb);
375 return rc;
376 }
377
378 static int sclp_mem_change_state(unsigned long start, unsigned long size,
379 int online)
380 {
381 struct memory_increment *incr;
382 unsigned long long istart;
383 int rc = 0;
384
385 list_for_each_entry(incr, &sclp_mem_list, list) {
386 istart = rn2addr(incr->rn);
387 if (start + size - 1 < istart)
388 break;
389 if (start > istart + rzm - 1)
390 continue;
391 if (online) {
392 if (incr->usecount++)
393 continue;
394 /*
395 * Don't break the loop if one assign fails. Loop may
396 * be walked again on CANCEL and we can't save
397 * information if state changed before or not.
398 * So continue and increase usecount for all increments.
399 */
400 rc |= sclp_assign_storage(incr->rn);
401 } else {
402 if (--incr->usecount)
403 continue;
404 sclp_unassign_storage(incr->rn);
405 }
406 }
407 return rc ? -EIO : 0;
408 }
409
410 static int sclp_mem_notifier(struct notifier_block *nb,
411 unsigned long action, void *data)
412 {
413 unsigned long start, size;
414 struct memory_notify *arg;
415 unsigned char id;
416 int rc = 0;
417
418 arg = data;
419 start = arg->start_pfn << PAGE_SHIFT;
420 size = arg->nr_pages << PAGE_SHIFT;
421 mutex_lock(&sclp_mem_mutex);
422 for (id = 0; id <= sclp_max_storage_id; id++)
423 if (!test_bit(id, sclp_storage_ids))
424 sclp_attach_storage(id);
425 switch (action) {
426 case MEM_ONLINE:
427 break;
428 case MEM_GOING_ONLINE:
429 rc = sclp_mem_change_state(start, size, 1);
430 break;
431 case MEM_CANCEL_ONLINE:
432 sclp_mem_change_state(start, size, 0);
433 break;
434 default:
435 rc = -EINVAL;
436 break;
437 }
438 mutex_unlock(&sclp_mem_mutex);
439 return rc ? NOTIFY_BAD : NOTIFY_OK;
440 }
441
442 static struct notifier_block sclp_mem_nb = {
443 .notifier_call = sclp_mem_notifier,
444 };
445
446 static void __init add_memory_merged(u16 rn)
447 {
448 static u16 first_rn, num;
449 unsigned long long start, size;
450
451 if (rn && first_rn && (first_rn + num == rn)) {
452 num++;
453 return;
454 }
455 if (!first_rn)
456 goto skip_add;
457 start = rn2addr(first_rn);
458 size = (unsigned long long ) num * rzm;
459 if (start >= VMEM_MAX_PHYS)
460 goto skip_add;
461 if (start + size > VMEM_MAX_PHYS)
462 size = VMEM_MAX_PHYS - start;
463 add_memory(0, start, size);
464 skip_add:
465 first_rn = rn;
466 num = 1;
467 }
468
469 static void __init sclp_add_standby_memory(void)
470 {
471 struct memory_increment *incr;
472
473 list_for_each_entry(incr, &sclp_mem_list, list)
474 if (incr->standby)
475 add_memory_merged(incr->rn);
476 add_memory_merged(0);
477 }
478
479 static void __init insert_increment(u16 rn, int standby, int assigned)
480 {
481 struct memory_increment *incr, *new_incr;
482 struct list_head *prev;
483 u16 last_rn;
484
485 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
486 if (!new_incr)
487 return;
488 new_incr->rn = rn;
489 new_incr->standby = standby;
490 last_rn = 0;
491 prev = &sclp_mem_list;
492 list_for_each_entry(incr, &sclp_mem_list, list) {
493 if (assigned && incr->rn > rn)
494 break;
495 if (!assigned && incr->rn - last_rn > 1)
496 break;
497 last_rn = incr->rn;
498 prev = &incr->list;
499 }
500 if (!assigned)
501 new_incr->rn = last_rn + 1;
502 if (new_incr->rn > rnmax) {
503 kfree(new_incr);
504 return;
505 }
506 list_add(&new_incr->list, prev);
507 }
508
509 struct read_storage_sccb {
510 struct sccb_header header;
511 u16 max_id;
512 u16 assigned;
513 u16 standby;
514 u16 :16;
515 u32 entries[0];
516 } __packed;
517
518 static int __init sclp_detect_standby_memory(void)
519 {
520 struct read_storage_sccb *sccb;
521 int i, id, assigned, rc;
522
523 if (!early_read_info_sccb_valid)
524 return 0;
525 if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
526 return 0;
527 rc = -ENOMEM;
528 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
529 if (!sccb)
530 goto out;
531 assigned = 0;
532 for (id = 0; id <= sclp_max_storage_id; id++) {
533 memset(sccb, 0, PAGE_SIZE);
534 sccb->header.length = PAGE_SIZE;
535 rc = do_sync_request(0x00040001 | id << 8, sccb);
536 if (rc)
537 goto out;
538 switch (sccb->header.response_code) {
539 case 0x0010:
540 set_bit(id, sclp_storage_ids);
541 for (i = 0; i < sccb->assigned; i++) {
542 if (!sccb->entries[i])
543 continue;
544 assigned++;
545 insert_increment(sccb->entries[i] >> 16, 0, 1);
546 }
547 break;
548 case 0x0310:
549 break;
550 case 0x0410:
551 for (i = 0; i < sccb->assigned; i++) {
552 if (!sccb->entries[i])
553 continue;
554 assigned++;
555 insert_increment(sccb->entries[i] >> 16, 1, 1);
556 }
557 break;
558 default:
559 rc = -EIO;
560 break;
561 }
562 if (!rc)
563 sclp_max_storage_id = sccb->max_id;
564 }
565 if (rc || list_empty(&sclp_mem_list))
566 goto out;
567 for (i = 1; i <= rnmax - assigned; i++)
568 insert_increment(0, 1, 0);
569 rc = register_memory_notifier(&sclp_mem_nb);
570 if (rc)
571 goto out;
572 sclp_add_standby_memory();
573 out:
574 free_page((unsigned long) sccb);
575 return rc;
576 }
577 __initcall(sclp_detect_standby_memory);
578
579 #endif /* CONFIG_MEMORY_HOTPLUG */
580
581 /*
582 * Channel path configuration related functions.
583 */
584
585 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
586 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
587 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
588
589 struct chp_cfg_sccb {
590 struct sccb_header header;
591 u8 ccm;
592 u8 reserved[6];
593 u8 cssid;
594 } __attribute__((packed));
595
596 static int do_chp_configure(sclp_cmdw_t cmd)
597 {
598 struct chp_cfg_sccb *sccb;
599 int rc;
600
601 if (!SCLP_HAS_CHP_RECONFIG)
602 return -EOPNOTSUPP;
603 /* Prepare sccb. */
604 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
605 if (!sccb)
606 return -ENOMEM;
607 sccb->header.length = sizeof(*sccb);
608 rc = do_sync_request(cmd, sccb);
609 if (rc)
610 goto out;
611 switch (sccb->header.response_code) {
612 case 0x0020:
613 case 0x0120:
614 case 0x0440:
615 case 0x0450:
616 break;
617 default:
618 printk(KERN_WARNING TAG "configure channel-path failed "
619 "(cmd=0x%08x, response=0x%04x)\n", cmd,
620 sccb->header.response_code);
621 rc = -EIO;
622 break;
623 }
624 out:
625 free_page((unsigned long) sccb);
626 return rc;
627 }
628
629 /**
630 * sclp_chp_configure - perform configure channel-path sclp command
631 * @chpid: channel-path ID
632 *
633 * Perform configure channel-path command sclp command for specified chpid.
634 * Return 0 after command successfully finished, non-zero otherwise.
635 */
636 int sclp_chp_configure(struct chp_id chpid)
637 {
638 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
639 }
640
641 /**
642 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
643 * @chpid: channel-path ID
644 *
645 * Perform deconfigure channel-path command sclp command for specified chpid
646 * and wait for completion. On success return 0. Return non-zero otherwise.
647 */
648 int sclp_chp_deconfigure(struct chp_id chpid)
649 {
650 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
651 }
652
653 struct chp_info_sccb {
654 struct sccb_header header;
655 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
656 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
657 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
658 u8 ccm;
659 u8 reserved[6];
660 u8 cssid;
661 } __attribute__((packed));
662
663 /**
664 * sclp_chp_read_info - perform read channel-path information sclp command
665 * @info: resulting channel-path information data
666 *
667 * Perform read channel-path information sclp command and wait for completion.
668 * On success, store channel-path information in @info and return 0. Return
669 * non-zero otherwise.
670 */
671 int sclp_chp_read_info(struct sclp_chp_info *info)
672 {
673 struct chp_info_sccb *sccb;
674 int rc;
675
676 if (!SCLP_HAS_CHP_INFO)
677 return -EOPNOTSUPP;
678 /* Prepare sccb. */
679 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
680 if (!sccb)
681 return -ENOMEM;
682 sccb->header.length = sizeof(*sccb);
683 rc = do_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
684 if (rc)
685 goto out;
686 if (sccb->header.response_code != 0x0010) {
687 printk(KERN_WARNING TAG "read channel-path info failed "
688 "(response=0x%04x)\n", sccb->header.response_code);
689 rc = -EIO;
690 goto out;
691 }
692 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
693 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
694 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
695 out:
696 free_page((unsigned long) sccb);
697 return rc;
698 }