Linux Audio

Check our new training course

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