Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright IBM Corp. 2007,2012
0004  *
0005  * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
0006  */
0007 
0008 #define KMSG_COMPONENT "sclp_cmd"
0009 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0010 
0011 #include <linux/completion.h>
0012 #include <linux/init.h>
0013 #include <linux/errno.h>
0014 #include <linux/err.h>
0015 #include <linux/export.h>
0016 #include <linux/slab.h>
0017 #include <linux/string.h>
0018 #include <linux/mm.h>
0019 #include <linux/mmzone.h>
0020 #include <linux/memory.h>
0021 #include <linux/module.h>
0022 #include <asm/ctl_reg.h>
0023 #include <asm/chpid.h>
0024 #include <asm/setup.h>
0025 #include <asm/page.h>
0026 #include <asm/sclp.h>
0027 #include <asm/numa.h>
0028 #include <asm/facility.h>
0029 
0030 #include "sclp.h"
0031 
0032 static void sclp_sync_callback(struct sclp_req *req, void *data)
0033 {
0034     struct completion *completion = data;
0035 
0036     complete(completion);
0037 }
0038 
0039 int sclp_sync_request(sclp_cmdw_t cmd, void *sccb)
0040 {
0041     return sclp_sync_request_timeout(cmd, sccb, 0);
0042 }
0043 
0044 int sclp_sync_request_timeout(sclp_cmdw_t cmd, void *sccb, int timeout)
0045 {
0046     struct completion completion;
0047     struct sclp_req *request;
0048     int rc;
0049 
0050     request = kzalloc(sizeof(*request), GFP_KERNEL);
0051     if (!request)
0052         return -ENOMEM;
0053     if (timeout)
0054         request->queue_timeout = timeout;
0055     request->command = cmd;
0056     request->sccb = sccb;
0057     request->status = SCLP_REQ_FILLED;
0058     request->callback = sclp_sync_callback;
0059     request->callback_data = &completion;
0060     init_completion(&completion);
0061 
0062     /* Perform sclp request. */
0063     rc = sclp_add_request(request);
0064     if (rc)
0065         goto out;
0066     wait_for_completion(&completion);
0067 
0068     /* Check response. */
0069     if (request->status != SCLP_REQ_DONE) {
0070         pr_warn("sync request failed (cmd=0x%08x, status=0x%02x)\n",
0071             cmd, request->status);
0072         rc = -EIO;
0073     }
0074 out:
0075     kfree(request);
0076     return rc;
0077 }
0078 
0079 /*
0080  * CPU configuration related functions.
0081  */
0082 
0083 #define SCLP_CMDW_CONFIGURE_CPU     0x00110001
0084 #define SCLP_CMDW_DECONFIGURE_CPU   0x00100001
0085 
0086 int _sclp_get_core_info(struct sclp_core_info *info)
0087 {
0088     int rc;
0089     int length = test_facility(140) ? EXT_SCCB_READ_CPU : PAGE_SIZE;
0090     struct read_cpu_info_sccb *sccb;
0091 
0092     if (!SCLP_HAS_CPU_INFO)
0093         return -EOPNOTSUPP;
0094 
0095     sccb = (void *)__get_free_pages(GFP_KERNEL | GFP_DMA | __GFP_ZERO, get_order(length));
0096     if (!sccb)
0097         return -ENOMEM;
0098     sccb->header.length = length;
0099     sccb->header.control_mask[2] = 0x80;
0100     rc = sclp_sync_request_timeout(SCLP_CMDW_READ_CPU_INFO, sccb,
0101                        SCLP_QUEUE_INTERVAL);
0102     if (rc)
0103         goto out;
0104     if (sccb->header.response_code != 0x0010) {
0105         pr_warn("readcpuinfo failed (response=0x%04x)\n",
0106             sccb->header.response_code);
0107         rc = -EIO;
0108         goto out;
0109     }
0110     sclp_fill_core_info(info, sccb);
0111 out:
0112     free_pages((unsigned long) sccb, get_order(length));
0113     return rc;
0114 }
0115 
0116 struct cpu_configure_sccb {
0117     struct sccb_header header;
0118 } __attribute__((packed, aligned(8)));
0119 
0120 static int do_core_configure(sclp_cmdw_t cmd)
0121 {
0122     struct cpu_configure_sccb *sccb;
0123     int rc;
0124 
0125     if (!SCLP_HAS_CPU_RECONFIG)
0126         return -EOPNOTSUPP;
0127     /*
0128      * This is not going to cross a page boundary since we force
0129      * kmalloc to have a minimum alignment of 8 bytes on s390.
0130      */
0131     sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
0132     if (!sccb)
0133         return -ENOMEM;
0134     sccb->header.length = sizeof(*sccb);
0135     rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL);
0136     if (rc)
0137         goto out;
0138     switch (sccb->header.response_code) {
0139     case 0x0020:
0140     case 0x0120:
0141         break;
0142     default:
0143         pr_warn("configure cpu failed (cmd=0x%08x, response=0x%04x)\n",
0144             cmd, sccb->header.response_code);
0145         rc = -EIO;
0146         break;
0147     }
0148 out:
0149     kfree(sccb);
0150     return rc;
0151 }
0152 
0153 int sclp_core_configure(u8 core)
0154 {
0155     return do_core_configure(SCLP_CMDW_CONFIGURE_CPU | core << 8);
0156 }
0157 
0158 int sclp_core_deconfigure(u8 core)
0159 {
0160     return do_core_configure(SCLP_CMDW_DECONFIGURE_CPU | core << 8);
0161 }
0162 
0163 #ifdef CONFIG_MEMORY_HOTPLUG
0164 
0165 static DEFINE_MUTEX(sclp_mem_mutex);
0166 static LIST_HEAD(sclp_mem_list);
0167 static u8 sclp_max_storage_id;
0168 static DECLARE_BITMAP(sclp_storage_ids, 256);
0169 
0170 struct memory_increment {
0171     struct list_head list;
0172     u16 rn;
0173     int standby;
0174 };
0175 
0176 struct assign_storage_sccb {
0177     struct sccb_header header;
0178     u16 rn;
0179 } __packed;
0180 
0181 int arch_get_memory_phys_device(unsigned long start_pfn)
0182 {
0183     if (!sclp.rzm)
0184         return 0;
0185     return PFN_PHYS(start_pfn) >> ilog2(sclp.rzm);
0186 }
0187 
0188 static unsigned long long rn2addr(u16 rn)
0189 {
0190     return (unsigned long long) (rn - 1) * sclp.rzm;
0191 }
0192 
0193 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
0194 {
0195     struct assign_storage_sccb *sccb;
0196     int rc;
0197 
0198     sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
0199     if (!sccb)
0200         return -ENOMEM;
0201     sccb->header.length = PAGE_SIZE;
0202     sccb->rn = rn;
0203     rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL);
0204     if (rc)
0205         goto out;
0206     switch (sccb->header.response_code) {
0207     case 0x0020:
0208     case 0x0120:
0209         break;
0210     default:
0211         pr_warn("assign storage failed (cmd=0x%08x, response=0x%04x, rn=0x%04x)\n",
0212             cmd, sccb->header.response_code, rn);
0213         rc = -EIO;
0214         break;
0215     }
0216 out:
0217     free_page((unsigned long) sccb);
0218     return rc;
0219 }
0220 
0221 static int sclp_assign_storage(u16 rn)
0222 {
0223     unsigned long long start;
0224     int rc;
0225 
0226     rc = do_assign_storage(0x000d0001, rn);
0227     if (rc)
0228         return rc;
0229     start = rn2addr(rn);
0230     storage_key_init_range(start, start + sclp.rzm);
0231     return 0;
0232 }
0233 
0234 static int sclp_unassign_storage(u16 rn)
0235 {
0236     return do_assign_storage(0x000c0001, rn);
0237 }
0238 
0239 struct attach_storage_sccb {
0240     struct sccb_header header;
0241     u16 :16;
0242     u16 assigned;
0243     u32 :32;
0244     u32 entries[0];
0245 } __packed;
0246 
0247 static int sclp_attach_storage(u8 id)
0248 {
0249     struct attach_storage_sccb *sccb;
0250     int rc;
0251     int i;
0252 
0253     sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
0254     if (!sccb)
0255         return -ENOMEM;
0256     sccb->header.length = PAGE_SIZE;
0257     sccb->header.function_code = 0x40;
0258     rc = sclp_sync_request_timeout(0x00080001 | id << 8, sccb,
0259                        SCLP_QUEUE_INTERVAL);
0260     if (rc)
0261         goto out;
0262     switch (sccb->header.response_code) {
0263     case 0x0020:
0264         set_bit(id, sclp_storage_ids);
0265         for (i = 0; i < sccb->assigned; i++) {
0266             if (sccb->entries[i])
0267                 sclp_unassign_storage(sccb->entries[i] >> 16);
0268         }
0269         break;
0270     default:
0271         rc = -EIO;
0272         break;
0273     }
0274 out:
0275     free_page((unsigned long) sccb);
0276     return rc;
0277 }
0278 
0279 static int sclp_mem_change_state(unsigned long start, unsigned long size,
0280                  int online)
0281 {
0282     struct memory_increment *incr;
0283     unsigned long long istart;
0284     int rc = 0;
0285 
0286     list_for_each_entry(incr, &sclp_mem_list, list) {
0287         istart = rn2addr(incr->rn);
0288         if (start + size - 1 < istart)
0289             break;
0290         if (start > istart + sclp.rzm - 1)
0291             continue;
0292         if (online)
0293             rc |= sclp_assign_storage(incr->rn);
0294         else
0295             sclp_unassign_storage(incr->rn);
0296         if (rc == 0)
0297             incr->standby = online ? 0 : 1;
0298     }
0299     return rc ? -EIO : 0;
0300 }
0301 
0302 static bool contains_standby_increment(unsigned long start, unsigned long end)
0303 {
0304     struct memory_increment *incr;
0305     unsigned long istart;
0306 
0307     list_for_each_entry(incr, &sclp_mem_list, list) {
0308         istart = rn2addr(incr->rn);
0309         if (end - 1 < istart)
0310             continue;
0311         if (start > istart + sclp.rzm - 1)
0312             continue;
0313         if (incr->standby)
0314             return true;
0315     }
0316     return false;
0317 }
0318 
0319 static int sclp_mem_notifier(struct notifier_block *nb,
0320                  unsigned long action, void *data)
0321 {
0322     unsigned long start, size;
0323     struct memory_notify *arg;
0324     unsigned char id;
0325     int rc = 0;
0326 
0327     arg = data;
0328     start = arg->start_pfn << PAGE_SHIFT;
0329     size = arg->nr_pages << PAGE_SHIFT;
0330     mutex_lock(&sclp_mem_mutex);
0331     for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
0332         sclp_attach_storage(id);
0333     switch (action) {
0334     case MEM_GOING_OFFLINE:
0335         /*
0336          * We do not allow to set memory blocks offline that contain
0337          * standby memory. This is done to simplify the "memory online"
0338          * case.
0339          */
0340         if (contains_standby_increment(start, start + size))
0341             rc = -EPERM;
0342         break;
0343     case MEM_ONLINE:
0344     case MEM_CANCEL_OFFLINE:
0345         break;
0346     case MEM_GOING_ONLINE:
0347         rc = sclp_mem_change_state(start, size, 1);
0348         break;
0349     case MEM_CANCEL_ONLINE:
0350         sclp_mem_change_state(start, size, 0);
0351         break;
0352     case MEM_OFFLINE:
0353         sclp_mem_change_state(start, size, 0);
0354         break;
0355     default:
0356         rc = -EINVAL;
0357         break;
0358     }
0359     mutex_unlock(&sclp_mem_mutex);
0360     return rc ? NOTIFY_BAD : NOTIFY_OK;
0361 }
0362 
0363 static struct notifier_block sclp_mem_nb = {
0364     .notifier_call = sclp_mem_notifier,
0365 };
0366 
0367 static void __init align_to_block_size(unsigned long long *start,
0368                        unsigned long long *size,
0369                        unsigned long long alignment)
0370 {
0371     unsigned long long start_align, size_align;
0372 
0373     start_align = roundup(*start, alignment);
0374     size_align = rounddown(*start + *size, alignment) - start_align;
0375 
0376     pr_info("Standby memory at 0x%llx (%lluM of %lluM usable)\n",
0377         *start, size_align >> 20, *size >> 20);
0378     *start = start_align;
0379     *size = size_align;
0380 }
0381 
0382 static void __init add_memory_merged(u16 rn)
0383 {
0384     unsigned long long start, size, addr, block_size;
0385     static u16 first_rn, num;
0386 
0387     if (rn && first_rn && (first_rn + num == rn)) {
0388         num++;
0389         return;
0390     }
0391     if (!first_rn)
0392         goto skip_add;
0393     start = rn2addr(first_rn);
0394     size = (unsigned long long) num * sclp.rzm;
0395     if (start >= VMEM_MAX_PHYS)
0396         goto skip_add;
0397     if (start + size > VMEM_MAX_PHYS)
0398         size = VMEM_MAX_PHYS - start;
0399     if (start >= ident_map_size)
0400         goto skip_add;
0401     if (start + size > ident_map_size)
0402         size = ident_map_size - start;
0403     block_size = memory_block_size_bytes();
0404     align_to_block_size(&start, &size, block_size);
0405     if (!size)
0406         goto skip_add;
0407     for (addr = start; addr < start + size; addr += block_size)
0408         add_memory(0, addr, block_size, MHP_NONE);
0409 skip_add:
0410     first_rn = rn;
0411     num = 1;
0412 }
0413 
0414 static void __init sclp_add_standby_memory(void)
0415 {
0416     struct memory_increment *incr;
0417 
0418     list_for_each_entry(incr, &sclp_mem_list, list)
0419         if (incr->standby)
0420             add_memory_merged(incr->rn);
0421     add_memory_merged(0);
0422 }
0423 
0424 static void __init insert_increment(u16 rn, int standby, int assigned)
0425 {
0426     struct memory_increment *incr, *new_incr;
0427     struct list_head *prev;
0428     u16 last_rn;
0429 
0430     new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
0431     if (!new_incr)
0432         return;
0433     new_incr->rn = rn;
0434     new_incr->standby = standby;
0435     last_rn = 0;
0436     prev = &sclp_mem_list;
0437     list_for_each_entry(incr, &sclp_mem_list, list) {
0438         if (assigned && incr->rn > rn)
0439             break;
0440         if (!assigned && incr->rn - last_rn > 1)
0441             break;
0442         last_rn = incr->rn;
0443         prev = &incr->list;
0444     }
0445     if (!assigned)
0446         new_incr->rn = last_rn + 1;
0447     if (new_incr->rn > sclp.rnmax) {
0448         kfree(new_incr);
0449         return;
0450     }
0451     list_add(&new_incr->list, prev);
0452 }
0453 
0454 static int __init sclp_detect_standby_memory(void)
0455 {
0456     struct read_storage_sccb *sccb;
0457     int i, id, assigned, rc;
0458 
0459     if (oldmem_data.start) /* No standby memory in kdump mode */
0460         return 0;
0461     if ((sclp.facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
0462         return 0;
0463     rc = -ENOMEM;
0464     sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
0465     if (!sccb)
0466         goto out;
0467     assigned = 0;
0468     for (id = 0; id <= sclp_max_storage_id; id++) {
0469         memset(sccb, 0, PAGE_SIZE);
0470         sccb->header.length = PAGE_SIZE;
0471         rc = sclp_sync_request(SCLP_CMDW_READ_STORAGE_INFO | id << 8, sccb);
0472         if (rc)
0473             goto out;
0474         switch (sccb->header.response_code) {
0475         case 0x0010:
0476             set_bit(id, sclp_storage_ids);
0477             for (i = 0; i < sccb->assigned; i++) {
0478                 if (!sccb->entries[i])
0479                     continue;
0480                 assigned++;
0481                 insert_increment(sccb->entries[i] >> 16, 0, 1);
0482             }
0483             break;
0484         case 0x0310:
0485             break;
0486         case 0x0410:
0487             for (i = 0; i < sccb->assigned; i++) {
0488                 if (!sccb->entries[i])
0489                     continue;
0490                 assigned++;
0491                 insert_increment(sccb->entries[i] >> 16, 1, 1);
0492             }
0493             break;
0494         default:
0495             rc = -EIO;
0496             break;
0497         }
0498         if (!rc)
0499             sclp_max_storage_id = sccb->max_id;
0500     }
0501     if (rc || list_empty(&sclp_mem_list))
0502         goto out;
0503     for (i = 1; i <= sclp.rnmax - assigned; i++)
0504         insert_increment(0, 1, 0);
0505     rc = register_memory_notifier(&sclp_mem_nb);
0506     if (rc)
0507         goto out;
0508     sclp_add_standby_memory();
0509 out:
0510     free_page((unsigned long) sccb);
0511     return rc;
0512 }
0513 __initcall(sclp_detect_standby_memory);
0514 
0515 #endif /* CONFIG_MEMORY_HOTPLUG */
0516 
0517 /*
0518  * Channel path configuration related functions.
0519  */
0520 
0521 #define SCLP_CMDW_CONFIGURE_CHPATH      0x000f0001
0522 #define SCLP_CMDW_DECONFIGURE_CHPATH        0x000e0001
0523 #define SCLP_CMDW_READ_CHPATH_INFORMATION   0x00030001
0524 
0525 struct chp_cfg_sccb {
0526     struct sccb_header header;
0527     u8 ccm;
0528     u8 reserved[6];
0529     u8 cssid;
0530 } __attribute__((packed));
0531 
0532 static int do_chp_configure(sclp_cmdw_t cmd)
0533 {
0534     struct chp_cfg_sccb *sccb;
0535     int rc;
0536 
0537     if (!SCLP_HAS_CHP_RECONFIG)
0538         return -EOPNOTSUPP;
0539     /* Prepare sccb. */
0540     sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
0541     if (!sccb)
0542         return -ENOMEM;
0543     sccb->header.length = sizeof(*sccb);
0544     rc = sclp_sync_request(cmd, sccb);
0545     if (rc)
0546         goto out;
0547     switch (sccb->header.response_code) {
0548     case 0x0020:
0549     case 0x0120:
0550     case 0x0440:
0551     case 0x0450:
0552         break;
0553     default:
0554         pr_warn("configure channel-path failed (cmd=0x%08x, response=0x%04x)\n",
0555             cmd, sccb->header.response_code);
0556         rc = -EIO;
0557         break;
0558     }
0559 out:
0560     free_page((unsigned long) sccb);
0561     return rc;
0562 }
0563 
0564 /**
0565  * sclp_chp_configure - perform configure channel-path sclp command
0566  * @chpid: channel-path ID
0567  *
0568  * Perform configure channel-path command sclp command for specified chpid.
0569  * Return 0 after command successfully finished, non-zero otherwise.
0570  */
0571 int sclp_chp_configure(struct chp_id chpid)
0572 {
0573     return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
0574 }
0575 
0576 /**
0577  * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
0578  * @chpid: channel-path ID
0579  *
0580  * Perform deconfigure channel-path command sclp command for specified chpid
0581  * and wait for completion. On success return 0. Return non-zero otherwise.
0582  */
0583 int sclp_chp_deconfigure(struct chp_id chpid)
0584 {
0585     return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
0586 }
0587 
0588 struct chp_info_sccb {
0589     struct sccb_header header;
0590     u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
0591     u8 standby[SCLP_CHP_INFO_MASK_SIZE];
0592     u8 configured[SCLP_CHP_INFO_MASK_SIZE];
0593     u8 ccm;
0594     u8 reserved[6];
0595     u8 cssid;
0596 } __attribute__((packed));
0597 
0598 /**
0599  * sclp_chp_read_info - perform read channel-path information sclp command
0600  * @info: resulting channel-path information data
0601  *
0602  * Perform read channel-path information sclp command and wait for completion.
0603  * On success, store channel-path information in @info and return 0. Return
0604  * non-zero otherwise.
0605  */
0606 int sclp_chp_read_info(struct sclp_chp_info *info)
0607 {
0608     struct chp_info_sccb *sccb;
0609     int rc;
0610 
0611     if (!SCLP_HAS_CHP_INFO)
0612         return -EOPNOTSUPP;
0613     /* Prepare sccb. */
0614     sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
0615     if (!sccb)
0616         return -ENOMEM;
0617     sccb->header.length = sizeof(*sccb);
0618     rc = sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
0619     if (rc)
0620         goto out;
0621     if (sccb->header.response_code != 0x0010) {
0622         pr_warn("read channel-path info failed (response=0x%04x)\n",
0623             sccb->header.response_code);
0624         rc = -EIO;
0625         goto out;
0626     }
0627     memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
0628     memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
0629     memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
0630 out:
0631     free_page((unsigned long) sccb);
0632     return rc;
0633 }