Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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